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
fd520d04326f10a1dd00093bd6b465fdc683ec87
0c1546a496eccfb56620165cad015f88d56190c5
/library/init/category/monad.lean
cb7fb54437ebe2333ccc894b961e5aab3d52f5c0
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,314
lean
/- Copyright (c) Luke Nelson and Jared Roesch. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luke Nelson and Jared Roesch -/ prelude import init.category.applicative universe variables u v class pre_monad (m : Type u β†’ Type v) := (bind : Ξ  {a b : Type u}, m a β†’ (a β†’ m b) β†’ m b) @[inline] def bind {m : Type u β†’ Type v} [pre_monad m] {a b : Type u} : m a β†’ (a β†’ m b) β†’ m b := pre_monad.bind @[inline] def pre_monad.and_then {a b : Type u} {m : Type u β†’ Type v} [pre_monad m] (x : m a) (y : m b) : m b := do x, y class monad (m : Type u β†’ Type v) extends functor m, pre_monad m : Type (max u+1 v) := (ret : Ξ  {a : Type u}, a β†’ m a) @[inline] def return {m : Type u β†’ Type v} [monad m] {a : Type u} : a β†’ m a := monad.ret m def fapp {m : Type u β†’ Type v} [monad m] {a b : Type u} (f : m (a β†’ b)) (a : m a) : m b := do g ← f, b ← a, return (g b) @[inline] instance monad_is_applicative (m : Type u β†’ Type v) [monad m] : applicative m := ⟨@fmap _ _, @return _ _, @fapp _ _⟩ infixl ` >>= `:2 := bind infixl ` >> `:2 := pre_monad.and_then /- Identical to pre_monad.and_then, but it is not inlined. -/ def pre_monad.seq {a b : Type u} {m : Type u β†’ Type v} [pre_monad m] (x : m a) (y : m b) : m b := do x, y
6be4920acd24f20f5d31903e116eb477092061b5
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/measure_theory/measure/stieltjes.lean
a3c89a5a418443fc2da319284bace038e52f2f01
[ "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
16,744
lean
/- Copyright (c) 2021 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Yury Kudryashov, SΓ©bastien GouΓ«zel -/ import measure_theory.constructions.borel_space /-! # Stieltjes measures on the real line Consider a function `f : ℝ β†’ ℝ` which is monotone and right-continuous. Then one can define a corrresponding measure, giving mass `f b - f a` to the interval `(a, b]`. ## Main definitions * `stieltjes_function` is a structure containing a function from `ℝ β†’ ℝ`, together with the assertions that it is monotone and right-continuous. To `f : stieltjes_function`, one associates a Borel measure `f.measure`. * `f.left_lim x` is the limit of `f` to the left of `x`. * `f.measure_Ioc` asserts that `f.measure (Ioc a b) = of_real (f b - f a)` * `f.measure_Ioo` asserts that `f.measure (Ioo a b) = of_real (f.left_lim b - f a)`. * `f.measure_Icc` and `f.measure_Ico` are analogous. -/ noncomputable theory open classical set filter open ennreal (of_real) open_locale big_operators ennreal nnreal topological_space /-! ### Basic properties of Stieltjes functions -/ /-- Bundled monotone right-continuous real functions, used to construct Stieltjes measures. -/ structure stieltjes_function := (to_fun : ℝ β†’ ℝ) (mono' : monotone to_fun) (right_continuous' : βˆ€ x, continuous_within_at to_fun (Ici x) x) namespace stieltjes_function instance : has_coe_to_fun stieltjes_function := ⟨_, to_fun⟩ initialize_simps_projections stieltjes_function (to_fun β†’ apply) variable (f : stieltjes_function) lemma mono : monotone f := f.mono' lemma right_continuous (x : ℝ) : continuous_within_at f (Ici x) x := f.right_continuous' x /-- The limit of a Stieltjes function to the left of `x` (it exists by monotonicity). The fact that it is indeed a left limit is asserted in `tendsto_left_lim` -/ @[irreducible] def left_lim (x : ℝ) := Sup (f '' (Iio x)) lemma tendsto_left_lim (x : ℝ) : tendsto f (𝓝[Iio x] x) (𝓝 (f.left_lim x)) := by { rw left_lim, exact f.mono.tendsto_nhds_within_Iio x } lemma left_lim_le {x y : ℝ} (h : x ≀ y) : f.left_lim x ≀ f y := begin apply le_of_tendsto (f.tendsto_left_lim x), filter_upwards [self_mem_nhds_within], assume z hz, exact (f.mono (le_of_lt hz)).trans (f.mono h) end lemma le_left_lim {x y : ℝ} (h : x < y) : f x ≀ f.left_lim y := begin apply ge_of_tendsto (f.tendsto_left_lim y), apply mem_nhds_within_Iio_iff_exists_Ioo_subset.2 ⟨x, h, _⟩, assume z hz, exact f.mono hz.1.le, end lemma left_lim_le_left_lim {x y : ℝ} (h : x ≀ y) : f.left_lim x ≀ f.left_lim y := begin rcases eq_or_lt_of_le h with rfl|hxy, { exact le_rfl }, { exact (f.left_lim_le le_rfl).trans (f.le_left_lim hxy) } end /-- The identity of `ℝ` as a Stieltjes function, used to construct Lebesgue measure. -/ @[simps] protected def id : stieltjes_function := { to_fun := id, mono' := Ξ» x y, id, right_continuous' := Ξ» x, continuous_within_at_id } @[simp] lemma id_left_lim (x : ℝ) : stieltjes_function.id.left_lim x = x := tendsto_nhds_unique (stieltjes_function.id.tendsto_left_lim x) $ (continuous_at_id).tendsto.mono_left nhds_within_le_nhds instance : inhabited stieltjes_function := ⟨stieltjes_function.id⟩ /-! ### The outer measure associated to a Stieltjes function -/ /-- Length of an interval. This is the largest monotonic function which correctly measures all intervals. -/ def length (s : set ℝ) : ℝβ‰₯0∞ := β¨…a b (h : s βŠ† Ioc a b), of_real (f b - f a) @[simp] lemma length_empty : f.length βˆ… = 0 := nonpos_iff_eq_zero.1 $ infi_le_of_le 0 $ infi_le_of_le 0 $ by simp @[simp] lemma length_Ioc (a b : ℝ) : f.length (Ioc a b) = of_real (f b - f a) := begin refine le_antisymm (infi_le_of_le a $ binfi_le b (subset.refl _)) (le_infi $ Ξ» a', le_infi $ Ξ» b', le_infi $ Ξ» h, ennreal.coe_le_coe.2 _), cases le_or_lt b a with ab ab, { rw real.to_nnreal_of_nonpos (sub_nonpos.2 (f.mono ab)), apply zero_le, }, cases (Ioc_subset_Ioc_iff ab).1 h with h₁ hβ‚‚, exact real.to_nnreal_le_to_nnreal (sub_le_sub (f.mono h₁) (f.mono hβ‚‚)) end lemma length_mono {s₁ sβ‚‚ : set ℝ} (h : s₁ βŠ† sβ‚‚) : f.length s₁ ≀ f.length sβ‚‚ := infi_le_infi $ Ξ» a, infi_le_infi $ Ξ» b, infi_le_infi2 $ Ξ» h', ⟨subset.trans h h', le_refl _⟩ open measure_theory /-- The Stieltjes outer measure associated to a Stieltjes function. -/ protected def outer : outer_measure ℝ := outer_measure.of_function f.length f.length_empty lemma outer_le_length (s : set ℝ) : f.outer s ≀ f.length s := outer_measure.of_function_le _ /-- If a compact interval `[a, b]` is covered by a union of open interval `(c i, d i)`, then `f b - f a ≀ βˆ‘ f (d i) - f (c i)`. This is an auxiliary technical statement to prove the same statement for half-open intervals, the point of the current statement being that one can use compactness to reduce it to a finite sum, and argue by induction on the size of the covering set. -/ lemma length_subadditive_Icc_Ioo {a b : ℝ} {c d : β„• β†’ ℝ} (ss : Icc a b βŠ† ⋃ i, Ioo (c i) (d i)) : of_real (f b - f a) ≀ βˆ‘' i, of_real (f (d i) - f (c i)) := begin suffices : βˆ€ (s:finset β„•) b (cv : Icc a b βŠ† ⋃ i ∈ (↑s:set β„•), Ioo (c i) (d i)), (of_real (f b - f a) : ℝβ‰₯0∞) ≀ βˆ‘ i in s, of_real (f (d i) - f (c i)), { rcases is_compact_Icc.elim_finite_subcover_image (Ξ» (i : β„•) (_ : i ∈ univ), @is_open_Ioo _ _ _ _ (c i) (d i)) (by simpa using ss) with ⟨s, su, hf, hs⟩, have e : (⋃ i ∈ (↑hf.to_finset:set β„•), Ioo (c i) (d i)) = (⋃ i ∈ s, Ioo (c i) (d i)), by simp only [ext_iff, exists_prop, finset.set_bUnion_coe, mem_Union, forall_const, iff_self, finite.mem_to_finset], rw ennreal.tsum_eq_supr_sum, refine le_trans _ (le_supr _ hf.to_finset), exact this hf.to_finset _ (by simpa only [e]) }, clear ss b, refine Ξ» s, finset.strong_induction_on s (Ξ» s IH b cv, _), cases le_total b a with ab ab, { rw ennreal.of_real_eq_zero.2 (sub_nonpos.2 (f.mono ab)), exact zero_le _, }, have := cv ⟨ab, le_refl _⟩, simp at this, rcases this with ⟨i, is, cb, bd⟩, rw [← finset.insert_erase is] at cv ⊒, rw [finset.coe_insert, bUnion_insert] at cv, rw [finset.sum_insert (finset.not_mem_erase _ _)], refine le_trans _ (add_le_add_left (IH _ (finset.erase_ssubset is) (c i) _) _), { refine le_trans (ennreal.of_real_le_of_real _) ennreal.of_real_add_le, rw sub_add_sub_cancel, exact sub_le_sub_right (f.mono bd.le) _ }, { rintro x ⟨h₁, hβ‚‚βŸ©, refine (cv ⟨h₁, le_trans hβ‚‚ (le_of_lt cb)⟩).resolve_left (mt and.left (not_lt_of_le hβ‚‚)) } end @[simp] lemma outer_Ioc (a b : ℝ) : f.outer (Ioc a b) = of_real (f b - f a) := begin /- It suffices to show that, if `(a, b]` is covered by sets `s i`, then `f b - f a` is bounded by `βˆ‘ f.length (s i) + Ξ΅`. The difficulty is that `f.length` is expressed in terms of half-open intervals, while we would like to have a compact interval covered by open intervals to use compactness and finite sums, as provided by `length_subadditive_Icc_Ioo`. The trick is to use the right-continuity of `f`. If `a'` is close enough to `a` on its right, then `[a', b]` is still covered by the sets `s i` and moreover `f b - f a'` is very close to `f b - f a` (up to `Ξ΅/2`). Also, by definition one can cover `s i` by a half-closed interval `(p i, q i]` with `f`-length very close to that of `s i` (within a suitably small `Ξ΅' i`, say). If one moves `q i` very slightly to the right, then the `f`-length will change very little by right continuity, and we will get an open interval `(p i, q' i)` covering `s i` with `f (q' i) - f (p i)` within `Ξ΅' i` of the `f`-length of `s i`. -/ refine le_antisymm (by { rw ← f.length_Ioc, apply outer_le_length }) (le_binfi $ Ξ» s hs, ennreal.le_of_forall_pos_le_add $ Ξ» Ξ΅ Ξ΅pos h, _), let Ξ΄ := Ξ΅/2, have Ξ΄pos : 0 < Ξ΄ := nnreal.half_pos Ξ΅pos, rcases ennreal.exists_pos_sum_of_encodable (ennreal.zero_lt_coe_iff.2 Ξ΄pos) β„• with ⟨Ρ', Ξ΅'0, hΡ⟩, obtain ⟨a', ha', aa'⟩ : βˆƒ a', f a' - f a < Ξ΄ ∧ a < a', { have A : continuous_within_at (Ξ» r, f r - f a) (Ioi a) a, { refine continuous_within_at.sub _ continuous_within_at_const, exact (f.right_continuous a).mono Ioi_subset_Ici_self }, have B : f a - f a < Ξ΄, by rwa [sub_self], exact (((tendsto_order.1 A).2 _ B).and self_mem_nhds_within).exists }, have : βˆ€ i, βˆƒ p:ℝ×ℝ, s i βŠ† Ioo p.1 p.2 ∧ (of_real (f p.2 - f p.1) : ℝβ‰₯0∞) < f.length (s i) + Ξ΅' i, { intro i, have := (ennreal.lt_add_right ((ennreal.le_tsum i).trans_lt h).ne (ennreal.zero_lt_coe_iff.2 (Ξ΅'0 i))), conv at this { to_lhs, rw length }, simp only [infi_lt_iff, exists_prop] at this, rcases this with ⟨p, q', spq, hq'⟩, have : continuous_within_at (Ξ» r, of_real (f r - f p)) (Ioi q') q', { apply ennreal.continuous_of_real.continuous_at.comp_continuous_within_at, refine continuous_within_at.sub _ continuous_within_at_const, exact (f.right_continuous q').mono Ioi_subset_Ici_self }, rcases (((tendsto_order.1 this).2 _ hq').and self_mem_nhds_within).exists with ⟨q, hq, q'q⟩, exact ⟨⟨p, q⟩, spq.trans (Ioc_subset_Ioo_right q'q), hq⟩ }, choose g hg using this, have I_subset : Icc a' b βŠ† ⋃ i, Ioo (g i).1 (g i).2 := calc Icc a' b βŠ† Ioc a b : Ξ» x hx, ⟨aa'.trans_le hx.1, hx.2⟩ ... βŠ† ⋃ i, s i : hs ... βŠ† ⋃ i, Ioo (g i).1 (g i).2 : Union_subset_Union (Ξ» i, (hg i).1), calc of_real (f b - f a) = of_real ((f b - f a') + (f a' - f a)) : by rw sub_add_sub_cancel ... ≀ of_real (f b - f a') + of_real (f a' - f a) : ennreal.of_real_add_le ... ≀ (βˆ‘' i, of_real (f (g i).2 - f (g i).1)) + of_real Ξ΄ : add_le_add (f.length_subadditive_Icc_Ioo I_subset) (ennreal.of_real_le_of_real ha'.le) ... ≀ (βˆ‘' i, (f.length (s i) + Ξ΅' i)) + Ξ΄ : add_le_add (ennreal.tsum_le_tsum (Ξ» i, (hg i).2.le)) (by simp only [ennreal.of_real_coe_nnreal, le_rfl]) ... = (βˆ‘' i, f.length (s i)) + (βˆ‘' i, Ξ΅' i) + Ξ΄ : by rw [ennreal.tsum_add] ... ≀ (βˆ‘' i, f.length (s i)) + Ξ΄ + Ξ΄ : add_le_add (add_le_add le_rfl hΞ΅.le) le_rfl ... = βˆ‘' (i : β„•), f.length (s i) + Ξ΅ : by simp [add_assoc, ennreal.add_halves] end lemma measurable_set_Ioi {c : ℝ} : f.outer.caratheodory.measurable_set' (Ioi c) := begin apply outer_measure.of_function_caratheodory (Ξ» t, _), refine le_infi (Ξ» a, le_infi (Ξ» b, le_infi (Ξ» h, _))), refine le_trans (add_le_add (f.length_mono $ inter_subset_inter_left _ h) (f.length_mono $ diff_subset_diff_left h)) _, cases le_total a c with hac hac; cases le_total b c with hbc hbc, { simp only [Ioc_inter_Ioi, f.length_Ioc, hac, sup_eq_max, hbc, le_refl, Ioc_eq_empty, max_eq_right, min_eq_left, Ioc_diff_Ioi, f.length_empty, zero_add, not_lt] }, { simp only [hac, hbc, Ioc_inter_Ioi, Ioc_diff_Ioi, f.length_Ioc, min_eq_right, sup_eq_max, ←ennreal.of_real_add, f.mono hac, f.mono hbc, sub_nonneg, sub_add_sub_cancel, le_refl, max_eq_right] }, { simp only [hbc, le_refl, Ioc_eq_empty, Ioc_inter_Ioi, min_eq_left, Ioc_diff_Ioi, f.length_empty, zero_add, or_true, le_sup_iff, f.length_Ioc, not_lt] }, { simp only [hac, hbc, Ioc_inter_Ioi, Ioc_diff_Ioi, f.length_Ioc, min_eq_right, sup_eq_max, le_refl, Ioc_eq_empty, add_zero, max_eq_left, f.length_empty, not_lt] } end theorem outer_trim : f.outer.trim = f.outer := begin refine le_antisymm (Ξ» s, _) (outer_measure.le_trim _), rw outer_measure.trim_eq_infi, refine le_infi (Ξ» t, le_infi $ Ξ» ht, ennreal.le_of_forall_pos_le_add $ Ξ» Ξ΅ Ξ΅0 h, _), rcases ennreal.exists_pos_sum_of_encodable (ennreal.zero_lt_coe_iff.2 Ξ΅0) β„• with ⟨Ρ', Ξ΅'0, hΡ⟩, refine le_trans _ (add_le_add_left (le_of_lt hΞ΅) _), rw ← ennreal.tsum_add, choose g hg using show βˆ€ i, βˆƒ s, t i βŠ† s ∧ measurable_set s ∧ f.outer s ≀ f.length (t i) + of_real (Ξ΅' i), { intro i, have := (ennreal.lt_add_right ((ennreal.le_tsum i).trans_lt h).ne (ennreal.zero_lt_coe_iff.2 (Ξ΅'0 i))), conv at this {to_lhs, rw length}, simp only [infi_lt_iff] at this, rcases this with ⟨a, b, h₁, hβ‚‚βŸ©, rw ← f.outer_Ioc at hβ‚‚, exact ⟨_, h₁, measurable_set_Ioc, le_of_lt $ by simpa using hβ‚‚βŸ© }, simp at hg, apply infi_le_of_le (Union g) _, apply infi_le_of_le (subset.trans ht $ Union_subset_Union (Ξ» i, (hg i).1)) _, apply infi_le_of_le (measurable_set.Union (Ξ» i, (hg i).2.1)) _, exact le_trans (f.outer.Union _) (ennreal.tsum_le_tsum $ Ξ» i, (hg i).2.2) end lemma borel_le_measurable : borel ℝ ≀ f.outer.caratheodory := begin rw borel_eq_generate_Ioi, refine measurable_space.generate_from_le _, simp [f.measurable_set_Ioi] { contextual := tt } end /-! ### The measure associated to a Stieltjes function -/ /-- The measure associated to a Stieltjes function, giving mass `f b - f a` to the interval `(a, b]`. -/ @[irreducible] protected def measure : measure ℝ := { to_outer_measure := f.outer, m_Union := Ξ» s hs, f.outer.Union_eq_of_caratheodory $ Ξ» i, f.borel_le_measurable _ (hs i), trimmed := f.outer_trim } @[simp] lemma measure_Ioc (a b : ℝ) : f.measure (Ioc a b) = of_real (f b - f a) := by { rw stieltjes_function.measure, exact f.outer_Ioc a b } @[simp] lemma measure_singleton (a : ℝ) : f.measure {a} = of_real (f a - f.left_lim a) := begin obtain ⟨u, u_mono, u_lt_a, u_lim⟩ : βˆƒ (u : β„• β†’ ℝ), strict_mono u ∧ (βˆ€ (n : β„•), u n < a) ∧ tendsto u at_top (𝓝 a) := exists_seq_strict_mono_tendsto a, have A : {a} = β‹‚ n, Ioc (u n) a, { refine subset.antisymm (Ξ» x hx, by simp [mem_singleton_iff.1 hx, u_lt_a]) (Ξ» x hx, _), simp at hx, have : a ≀ x := le_of_tendsto' u_lim (Ξ» n, (hx n).1.le), simp [le_antisymm this (hx 0).2] }, have L1 : tendsto (Ξ» n, f.measure (Ioc (u n) a)) at_top (𝓝 (f.measure {a})), { rw A, refine tendsto_measure_Inter (Ξ» n, measurable_set_Ioc) (Ξ» m n hmn, _) _, { exact Ioc_subset_Ioc (u_mono.monotone hmn) le_rfl }, { exact ⟨0, by simp only [measure_Ioc, ennreal.of_real_lt_top]⟩ } }, have L2 : tendsto (Ξ» n, f.measure (Ioc (u n) a)) at_top (𝓝 (of_real (f a - f.left_lim a))), { simp only [measure_Ioc], have : tendsto (Ξ» n, f (u n)) at_top (𝓝 (f.left_lim a)), { apply (f.tendsto_left_lim a).comp, exact tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ u_lim (eventually_of_forall (Ξ» n, u_lt_a n)) }, exact ennreal.continuous_of_real.continuous_at.tendsto.comp (tendsto_const_nhds.sub this) }, exact tendsto_nhds_unique L1 L2 end @[simp] lemma measure_Icc (a b : ℝ) : f.measure (Icc a b) = of_real (f b - f.left_lim a) := begin rcases le_or_lt a b with hab|hab, { have A : disjoint {a} (Ioc a b), by simp, simp [← Icc_union_Ioc_eq_Icc le_rfl hab, -singleton_union, ← ennreal.of_real_add, f.left_lim_le, measure_union A (measurable_set_singleton a) measurable_set_Ioc, f.mono hab] }, { simp only [hab, measure_empty, Icc_eq_empty, not_le], symmetry, simp [ennreal.of_real_eq_zero, f.le_left_lim hab] } end @[simp] lemma measure_Ioo {a b : ℝ} : f.measure (Ioo a b) = of_real (f.left_lim b - f a) := begin rcases le_or_lt b a with hab|hab, { simp only [hab, measure_empty, Ioo_eq_empty, not_lt], symmetry, simp [ennreal.of_real_eq_zero, f.left_lim_le hab] }, { have A : disjoint (Ioo a b) {b}, by simp, have D : f b - f a = (f b - f.left_lim b) + (f.left_lim b - f a), by abel, have := f.measure_Ioc a b, simp only [←Ioo_union_Icc_eq_Ioc hab le_rfl, measure_singleton, measure_union A measurable_set_Ioo (measurable_set_singleton b), Icc_self] at this, rw [D, ennreal.of_real_add, add_comm] at this, { simpa only [ennreal.add_right_inj, ennreal.of_real_lt_top] }, { simp only [f.left_lim_le, sub_nonneg] }, { simp only [f.le_left_lim hab, sub_nonneg] } }, end @[simp] lemma measure_Ico (a b : ℝ) : f.measure (Ico a b) = of_real (f.left_lim b - f.left_lim a) := begin rcases le_or_lt b a with hab|hab, { simp only [hab, measure_empty, Ico_eq_empty, not_lt], symmetry, simp [ennreal.of_real_eq_zero, f.left_lim_le_left_lim hab] }, { have A : disjoint {a} (Ioo a b) := by simp, simp [← Icc_union_Ioo_eq_Ico le_rfl hab, -singleton_union, hab.ne, f.left_lim_le, measure_union A (measurable_set_singleton a) measurable_set_Ioo, f.le_left_lim hab, ← ennreal.of_real_add] } end end stieltjes_function
77935da4eb294605c847fdea6a6a70a92a27480e
bb31430994044506fa42fd667e2d556327e18dfe
/src/number_theory/pell.lean
5fed9482d43e136a838dd9c688b9b2dad93e23ca
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
36,709
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.nat.modeq import number_theory.zsqrtd.basic /-! # Pell's equation and Matiyasevic's theorem This file solves Pell's equation, i.e. integer solutions to `x ^ 2 - d * y ^ 2 = 1` in the special case that `d = a ^ 2 - 1`. This is then applied to prove Matiyasevic's theorem that the power function is Diophantine, which is the last key ingredient in the solution to Hilbert's tenth problem. For the definition of Diophantine function, see `dioph.lean`. ## Main definition * `pell` is a function assigning to a natural number `n` the `n`-th solution to Pell's equation constructed recursively from the initial solution `(0, 1)`. ## Main statements * `eq_pell` shows that every solution to Pell's equation is recursively obtained using `pell` * `matiyasevic` shows that a certain system of Diophantine equations has a solution if and only if the first variable is the `x`-component in a solution to Pell's equation - the key step towards Hilbert's tenth problem in Davis' version of Matiyasevic's theorem. * `eq_pow_of_pell` shows that the power function is Diophantine. ## Implementation notes The proof of Matiyasevic's theorem doesn't follow Matiyasevic's original account of using Fibonacci numbers but instead Davis' variant of using solutions to Pell's equation. ## References * [M. Carneiro, _A Lean formalization of Matiyasevič's theorem_][carneiro2018matiyasevic] * [M. Davis, _Hilbert's tenth problem is unsolvable_][MR317916] ## Tags Pell's equation, Matiyasevic's theorem, Hilbert's tenth problem ## TODO * Provide solutions to Pell's equation for the case of arbitrary `d` (not just `d = a ^ 2 - 1` like in the current version) and furthermore also for `x ^ 2 - d * y ^ 2 = -1`. * Connect solutions to the continued fraction expansion of `√d`. -/ namespace pell open nat section parameters {a : β„•} (a1 : 1 < a) include a1 private def d := a*a - 1 @[simp] theorem d_pos : 0 < d := tsub_pos_of_lt (mul_lt_mul a1 (le_of_lt a1) dec_trivial dec_trivial : 1*1<a*a) /-- The Pell sequences, i.e. the sequence of integer solutions to `x ^ 2 - d * y ^ 2 = 1`, where `d = a ^ 2 - 1`, defined together in mutual recursion. -/ -- TODO(lint): Fix double namespace issue @[nolint dup_namespace] def pell : β„• β†’ β„• Γ— β„• := Ξ»n, nat.rec_on n (1, 0) (Ξ»n xy, (xy.1*a + d*xy.2, xy.1 + xy.2*a)) /-- The Pell `x` sequence. -/ def xn (n : β„•) : β„• := (pell n).1 /-- The Pell `y` sequence. -/ def yn (n : β„•) : β„• := (pell n).2 @[simp] theorem pell_val (n : β„•) : pell n = (xn n, yn n) := show pell n = ((pell n).1, (pell n).2), from match pell n with (a, b) := rfl end @[simp] theorem xn_zero : xn 0 = 1 := rfl @[simp] theorem yn_zero : yn 0 = 0 := rfl @[simp] theorem xn_succ (n : β„•) : xn (n+1) = xn n * a + d * yn n := rfl @[simp] theorem yn_succ (n : β„•) : yn (n+1) = xn n + yn n * a := rfl @[simp] theorem xn_one : xn 1 = a := by simp @[simp] theorem yn_one : yn 1 = 1 := by simp /-- The Pell `x` sequence, considered as an integer sequence.-/ def xz (n : β„•) : β„€ := xn n /-- The Pell `y` sequence, considered as an integer sequence.-/ def yz (n : β„•) : β„€ := yn n section omit a1 /-- The element `a` such that `d = a ^ 2 - 1`, considered as an integer.-/ def az : β„€ := a end theorem asq_pos : 0 < a*a := le_trans (le_of_lt a1) (by have := @nat.mul_le_mul_left 1 a a (le_of_lt a1); rwa mul_one at this) theorem dz_val : ↑d = az*az - 1 := have 1 ≀ a*a, from asq_pos, show ↑(a*a - 1) = _, by rw int.coe_nat_sub this; refl @[simp] theorem xz_succ (n : β„•) : xz (n+1) = xz n * az + ↑d * yz n := rfl @[simp] theorem yz_succ (n : β„•) : yz (n+1) = xz n + yz n * az := rfl /-- The Pell sequence can also be viewed as an element of `β„€βˆšd` -/ def pell_zd (n : β„•) : β„€βˆšd := ⟨xn n, yn n⟩ @[simp] theorem pell_zd_re (n : β„•) : (pell_zd n).re = xn n := rfl @[simp] theorem pell_zd_im (n : β„•) : (pell_zd n).im = yn n := rfl /-- The property of being a solution to the Pell equation, expressed as a property of elements of `β„€βˆšd`. -/ def is_pell : β„€βˆšd β†’ Prop | ⟨x, y⟩ := x*x - d*y*y = 1 theorem is_pell_nat {x y : β„•} : is_pell ⟨x, y⟩ ↔ x*x - d*y*y = 1 := ⟨λh, int.coe_nat_inj (by rw int.coe_nat_sub (int.le_of_coe_nat_le_coe_nat $ int.le.intro_sub h); exact h), Ξ»h, show ((x*x : β„•) - (d*y*y:β„•) : β„€) = 1, by rw [← int.coe_nat_sub $ le_of_lt $ nat.lt_of_sub_eq_succ h, h]; refl⟩ theorem is_pell_norm : Ξ  {b : β„€βˆšd}, is_pell b ↔ b * b.conj = 1 | ⟨x, y⟩ := by simp [zsqrtd.ext, is_pell, mul_comm]; ring_nf theorem is_pell_mul {b c : β„€βˆšd} (hb : is_pell b) (hc : is_pell c) : is_pell (b * c) := is_pell_norm.2 (by simp [mul_comm, mul_left_comm, zsqrtd.conj_mul, pell.is_pell_norm.1 hb, pell.is_pell_norm.1 hc]) theorem is_pell_conj : βˆ€ {b : β„€βˆšd}, is_pell b ↔ is_pell b.conj | ⟨x, y⟩ := by simp [is_pell, zsqrtd.conj] @[simp] theorem pell_zd_succ (n : β„•) : pell_zd (n+1) = pell_zd n * ⟨a, 1⟩ := by simp [zsqrtd.ext] theorem is_pell_one : is_pell ⟨a, 1⟩ := show az*az-d*1*1=1, by simp [dz_val]; ring theorem is_pell_pell_zd : βˆ€ (n : β„•), is_pell (pell_zd n) | 0 := rfl | (n+1) := let o := is_pell_one in by simp; exact pell.is_pell_mul (is_pell_pell_zd n) o @[simp] theorem pell_eqz (n : β„•) : xz n * xz n - d * yz n * yz n = 1 := is_pell_pell_zd n @[simp] theorem pell_eq (n : β„•) : xn n * xn n - d * yn n * yn n = 1 := let pn := pell_eqz n in have h : (↑(xn n * xn n) : β„€) - ↑(d * yn n * yn n) = 1, by repeat {rw int.coe_nat_mul}; exact pn, have hl : d * yn n * yn n ≀ xn n * xn n, from int.le_of_coe_nat_le_coe_nat $ int.le.intro $ add_eq_of_eq_sub' $ eq.symm h, int.coe_nat_inj (by rw int.coe_nat_sub hl; exact h) instance dnsq : zsqrtd.nonsquare d := ⟨λn h, have n*n + 1 = a*a, by rw ← h; exact nat.succ_pred_eq_of_pos (asq_pos a1), have na : n < a, from nat.mul_self_lt_mul_self_iff.2 (by rw ← this; exact nat.lt_succ_self _), have (n+1)*(n+1) ≀ n*n + 1, by rw this; exact nat.mul_self_le_mul_self na, have n+n ≀ 0, from @nat.le_of_add_le_add_right (n*n + 1) _ _ (by ring_nf at this ⊒; assumption), ne_of_gt d_pos $ by rwa nat.eq_zero_of_le_zero ((nat.le_add_left _ _).trans this) at h⟩ theorem xn_ge_a_pow : βˆ€ (n : β„•), a^n ≀ xn n | 0 := le_refl 1 | (n+1) := by simp [pow_succ']; exact le_trans (nat.mul_le_mul_right _ (xn_ge_a_pow n)) (nat.le_add_right _ _) theorem n_lt_a_pow : βˆ€ (n : β„•), n < a^n | 0 := nat.le_refl 1 | (n+1) := begin have IH := n_lt_a_pow n, have : a^n + a^n ≀ a^n * a, { rw ← mul_two, exact nat.mul_le_mul_left _ a1 }, simp [pow_succ'], refine lt_of_lt_of_le _ this, exact add_lt_add_of_lt_of_le IH (lt_of_le_of_lt (nat.zero_le _) IH) end theorem n_lt_xn (n) : n < xn n := lt_of_lt_of_le (n_lt_a_pow n) (xn_ge_a_pow n) theorem x_pos (n) : 0 < xn n := lt_of_le_of_lt (nat.zero_le n) (n_lt_xn n) lemma eq_pell_lem : βˆ€n (b:β„€βˆšd), 1 ≀ b β†’ is_pell b β†’ b ≀ pell_zd n β†’ βˆƒn, b = pell_zd n | 0 b := Ξ»h1 hp hl, ⟨0, @zsqrtd.le_antisymm _ dnsq _ _ hl h1⟩ | (n+1) b := Ξ»h1 hp h, have a1p : (0:β„€βˆšd) ≀ ⟨a, 1⟩, from trivial, have am1p : (0:β„€βˆšd) ≀ ⟨a, -1⟩, from show (_:nat) ≀ _, by simp; exact nat.pred_le _, have a1m : (⟨a, 1⟩ * ⟨a, -1⟩ : β„€βˆšd) = 1, from is_pell_norm.1 is_pell_one, if ha : (βŸ¨β†‘a, 1⟩ : β„€βˆšd) ≀ b then let ⟨m, e⟩ := eq_pell_lem n (b * ⟨a, -1⟩) (by rw ← a1m; exact mul_le_mul_of_nonneg_right ha am1p) (is_pell_mul hp (is_pell_conj.1 is_pell_one)) (by have t := mul_le_mul_of_nonneg_right h am1p; rwa [pell_zd_succ, mul_assoc, a1m, mul_one] at t) in ⟨m+1, by rw [show b = b * ⟨a, -1⟩ * ⟨a, 1⟩, by rw [mul_assoc, eq.trans (mul_comm _ _) a1m]; simp, pell_zd_succ, e]⟩ else suffices Β¬1 < b, from ⟨0, show b = 1, from (or.resolve_left (lt_or_eq_of_le h1) this).symm⟩, Ξ» h1l, by cases b with x y; exact have bm : (_*⟨_,_⟩ :β„€βˆš(d a1)) = 1, from pell.is_pell_norm.1 hp, have y0l : (0:β„€βˆš(d a1)) < ⟨x - x, y - -y⟩, from sub_lt_sub h1l $ Ξ»(hn : (1:β„€βˆš(d a1)) ≀ ⟨x, -y⟩), by have t := mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1); rw [bm, mul_one] at t; exact h1l t, have yl2 : (⟨_, _⟩ : β„€βˆš_) < ⟨_, _⟩, from show (⟨x, y⟩ - ⟨x, -y⟩ : β„€βˆš(d a1)) < ⟨a, 1⟩ - ⟨a, -1⟩, from sub_lt_sub (by exact ha) $ Ξ»(hn : (⟨x, -y⟩ : β„€βˆš(d a1)) ≀ ⟨a, -1⟩), by have t := mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1)) a1p; rw [bm, one_mul, mul_assoc, eq.trans (mul_comm _ _) a1m, mul_one] at t; exact ha t, by simp at y0l; simp at yl2; exact match y, y0l, (yl2 : (⟨_, _⟩ : β„€βˆš_) < ⟨_, _⟩) with | 0, y0l, yl2 := y0l (le_refl 0) | (y+1 : β„•), y0l, yl2 := yl2 (zsqrtd.le_of_le_le (le_refl 0) (let t := int.coe_nat_le_coe_nat_of_le (nat.succ_pos y) in add_le_add t t)) | -[1+y], y0l, yl2 := y0l trivial end theorem eq_pell_zd (b : β„€βˆšd) (b1 : 1 ≀ b) (hp : is_pell b) : βˆƒn, b = pell_zd n := let ⟨n, h⟩ := @zsqrtd.le_arch d b in eq_pell_lem n b b1 hp $ h.trans $ by rw zsqrtd.coe_nat_val; exact zsqrtd.le_of_le_le (int.coe_nat_le_coe_nat_of_le $ le_of_lt $ n_lt_xn _ _) (int.coe_zero_le _) /-- Every solution to **Pell's equation** is recursively obtained from the initial solution `(1,0)` using the recursion `pell`. -/ theorem eq_pell {x y : β„•} (hp : x*x - d*y*y = 1) : βˆƒn, x = xn n ∧ y = yn n := have (1:β„€βˆšd) ≀ ⟨x, y⟩, from match x, hp with | 0, (hp : 0 - _ = 1) := by rw zero_tsub at hp; contradiction | (x+1), hp := zsqrtd.le_of_le_le (int.coe_nat_le_coe_nat_of_le $ nat.succ_pos x) (int.coe_zero_le _) end, let ⟨m, e⟩ := eq_pell_zd ⟨x, y⟩ this (is_pell_nat.2 hp) in ⟨m, match x, y, e with ._, ._, rfl := ⟨rfl, rfl⟩ end⟩ theorem pell_zd_add (m) : βˆ€ n, pell_zd (m + n) = pell_zd m * pell_zd n | 0 := (mul_one _).symm | (n+1) := by rw[← add_assoc, pell_zd_succ, pell_zd_succ, pell_zd_add n, ← mul_assoc] theorem xn_add (m n) : xn (m + n) = xn m * xn n + d * yn m * yn n := by injection (pell_zd_add _ m n) with h _; repeat {rw ← int.coe_nat_add at h <|> rw ← int.coe_nat_mul at h}; exact int.coe_nat_inj h theorem yn_add (m n) : yn (m + n) = xn m * yn n + yn m * xn n := by injection (pell_zd_add _ m n) with _ h; repeat {rw ← int.coe_nat_add at h <|> rw ← int.coe_nat_mul at h}; exact int.coe_nat_inj h theorem pell_zd_sub {m n} (h : n ≀ m) : pell_zd (m - n) = pell_zd m * (pell_zd n).conj := let t := pell_zd_add n (m - n) in by rw [add_tsub_cancel_of_le h] at t; rw [t, mul_comm (pell_zd _ n) _, mul_assoc, (is_pell_norm _).1 (is_pell_pell_zd _ _), mul_one] theorem xz_sub {m n} (h : n ≀ m) : xz (m - n) = xz m * xz n - d * yz m * yz n := by { rw [sub_eq_add_neg, ←mul_neg], exact congr_arg zsqrtd.re (pell_zd_sub a1 h) } theorem yz_sub {m n} (h : n ≀ m) : yz (m - n) = xz n * yz m - xz m * yz n := by { rw [sub_eq_add_neg, ←mul_neg, mul_comm, add_comm], exact congr_arg zsqrtd.im (pell_zd_sub a1 h) } theorem xy_coprime (n) : (xn n).coprime (yn n) := nat.coprime_of_dvd' $ Ξ»k kp kx ky, let p := pell_eq n in by rw ← p; exact nat.dvd_sub (le_of_lt $ nat.lt_of_sub_eq_succ p) (kx.mul_left _) (ky.mul_left _) theorem strict_mono_y : strict_mono yn | m 0 h := absurd h $ nat.not_lt_zero _ | m (n+1) h := have yn m ≀ yn n, from or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ h) (Ξ»hl, le_of_lt $ strict_mono_y hl) (Ξ»e, by rw e), by simp; refine lt_of_le_of_lt _ (nat.lt_add_of_pos_left $ x_pos a1 n); rw ← mul_one (yn a1 m); exact mul_le_mul this (le_of_lt a1) (nat.zero_le _) (nat.zero_le _) theorem strict_mono_x : strict_mono xn | m 0 h := absurd h $ nat.not_lt_zero _ | m (n+1) h := have xn m ≀ xn n, from or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ h) (Ξ»hl, le_of_lt $ strict_mono_x hl) (Ξ»e, by rw e), by simp; refine lt_of_lt_of_le (lt_of_le_of_lt this _) (nat.le_add_right _ _); have t := nat.mul_lt_mul_of_pos_left a1 (x_pos a1 n); rwa mul_one at t theorem yn_ge_n : Ξ  n, n ≀ yn n | 0 := nat.zero_le _ | (n+1) := show n < yn (n+1), from lt_of_le_of_lt (yn_ge_n n) (strict_mono_y $ nat.lt_succ_self n) theorem y_mul_dvd (n) : βˆ€k, yn n ∣ yn (n * k) | 0 := dvd_zero _ | (k+1) := by rw [nat.mul_succ, yn_add]; exact dvd_add (dvd_mul_left _ _) ((y_mul_dvd k).mul_right _) theorem y_dvd_iff (m n) : yn m ∣ yn n ↔ m ∣ n := ⟨λh, nat.dvd_of_mod_eq_zero $ (nat.eq_zero_or_pos _).resolve_right $ Ξ»hp, have co : nat.coprime (yn m) (xn (m * (n / m))), from nat.coprime.symm $ (xy_coprime _).coprime_dvd_right (y_mul_dvd m (n / m)), have m0 : 0 < m, from m.eq_zero_or_pos.resolve_left $ Ξ»e, by rw [e, nat.mod_zero] at hp; rw [e] at h; exact ne_of_lt (strict_mono_y a1 hp) (eq_zero_of_zero_dvd h).symm, by rw [← nat.mod_add_div n m, yn_add] at h; exact not_le_of_gt (strict_mono_y _ $ nat.mod_lt n m0) (nat.le_of_dvd (strict_mono_y _ hp) $ co.dvd_of_dvd_mul_right $ (nat.dvd_add_iff_right $ (y_mul_dvd _ _ _).mul_left _).2 h), λ⟨k, e⟩, by rw e; apply y_mul_dvd⟩ theorem xy_modeq_yn (n) : βˆ€ k, xn (n * k) ≑ (xn n)^k [MOD (yn n)^2] ∧ yn (n * k) ≑ k * (xn n)^(k-1) * yn n [MOD (yn n)^3] | 0 := by constructor; simp | (k+1) := let ⟨hx, hy⟩ := xy_modeq_yn k in have L : xn (n * k) * xn n + d * yn (n * k) * yn n ≑ xn n^k * xn n + 0 [MOD yn n^2], from (hx.mul_right _ ).add $ modeq_zero_iff_dvd.2 $ by rw pow_succ'; exact mul_dvd_mul_right (dvd_mul_of_dvd_right (modeq_zero_iff_dvd.1 $ (hy.of_dvd $ by simp [pow_succ']).trans $ modeq_zero_iff_dvd.2 $ by simp [-mul_comm, -mul_assoc]) _) _, have R : xn (n * k) * yn n + yn (n * k) * xn n ≑ xn n^k * yn n + k * xn n^k * yn n [MOD yn n^3], from modeq.add (by { rw pow_succ', exact hx.mul_right' _ }) $ have k * xn n^(k - 1) * yn n * xn n = k * xn n^k * yn n, by clear _let_match; cases k with k; simp [pow_succ', mul_comm, mul_left_comm], by { rw ← this, exact hy.mul_right _ }, by { rw [add_tsub_cancel_right, nat.mul_succ, xn_add, yn_add, pow_succ' (xn _ n), nat.succ_mul, add_comm (k * xn _ n^k) (xn _ n^k), right_distrib], exact ⟨L, R⟩ } theorem ysq_dvd_yy (n) : yn n * yn n ∣ yn (n * yn n) := modeq_zero_iff_dvd.1 $ ((xy_modeq_yn n (yn n)).right.of_dvd $ by simp [pow_succ]).trans (modeq_zero_iff_dvd.2 $ by simp [mul_dvd_mul_left, mul_assoc]) theorem dvd_of_ysq_dvd {n t} (h : yn n * yn n ∣ yn t) : yn n ∣ t := have nt : n ∣ t, from (y_dvd_iff n t).1 $ dvd_of_mul_left_dvd h, n.eq_zero_or_pos.elim (Ξ» n0, by rwa n0 at ⊒ nt) $ Ξ» (n0l : 0 < n), let ⟨k, ke⟩ := nt in have yn n ∣ k * (xn n)^(k-1), from nat.dvd_of_mul_dvd_mul_right (strict_mono_y n0l) $ modeq_zero_iff_dvd.1 $ by have xm := (xy_modeq_yn a1 n k).right; rw ← ke at xm; exact (xm.of_dvd $ by simp [pow_succ]).symm.trans h.modeq_zero_nat, by rw ke; exact dvd_mul_of_dvd_right (((xy_coprime _ _).pow_left _).symm.dvd_of_dvd_mul_right this) _ theorem pell_zd_succ_succ (n) : pell_zd (n + 2) + pell_zd n = (2 * a : β„•) * pell_zd (n + 1) := have (1:β„€βˆšd) + ⟨a, 1⟩ * ⟨a, 1⟩ = ⟨a, 1⟩ * (2 * a), by { rw zsqrtd.coe_nat_val, change (⟨_,_⟩:β„€βˆš(d a1))=⟨_,_⟩, rw dz_val, dsimp [az], rw zsqrtd.ext, dsimp, split; ring }, by simpa [mul_add, mul_comm, mul_left_comm, add_comm] using congr_arg (* pell_zd a1 n) this theorem xy_succ_succ (n) : xn (n + 2) + xn n = (2 * a) * xn (n + 1) ∧ yn (n + 2) + yn n = (2 * a) * yn (n + 1) := begin have := pell_zd_succ_succ a1 n, unfold pell_zd at this, erw [zsqrtd.smul_val (2 * a : β„•)] at this, injection this with h₁ hβ‚‚, split; apply int.coe_nat_inj; [simpa using h₁, simpa using hβ‚‚] end theorem xn_succ_succ (n) : xn (n + 2) + xn n = (2 * a) * xn (n + 1) := (xy_succ_succ n).1 theorem yn_succ_succ (n) : yn (n + 2) + yn n = (2 * a) * yn (n + 1) := (xy_succ_succ n).2 theorem xz_succ_succ (n) : xz (n + 2) = (2 * a : β„•) * xz (n + 1) - xz n := eq_sub_of_add_eq $ by delta xz; rw [← int.coe_nat_add, ← int.coe_nat_mul, xn_succ_succ] theorem yz_succ_succ (n) : yz (n + 2) = (2 * a : β„•) * yz (n + 1) - yz n := eq_sub_of_add_eq $ by delta yz; rw [← int.coe_nat_add, ← int.coe_nat_mul, yn_succ_succ] theorem yn_modeq_a_sub_one : βˆ€ n, yn n ≑ n [MOD a-1] | 0 := by simp | 1 := by simp | (n+2) := (yn_modeq_a_sub_one n).add_right_cancel $ begin rw [yn_succ_succ, (by ring : n + 2 + n = 2 * (n + 1))], exact ((modeq_sub a1.le).mul_left 2).mul (yn_modeq_a_sub_one (n+1)), end theorem yn_modeq_two : βˆ€ n, yn n ≑ n [MOD 2] | 0 := by simp | 1 := by simp | (n+2) := (yn_modeq_two n).add_right_cancel $ begin rw [yn_succ_succ, mul_assoc, (by ring : n + 2 + n = 2 * (n + 1))], exact (dvd_mul_right 2 _).modeq_zero_nat.trans (dvd_mul_right 2 _).zero_modeq_nat, end section omit a1 lemma x_sub_y_dvd_pow_lem (y2 y1 y0 yn1 yn0 xn1 xn0 ay a2 : β„€) : (a2 * yn1 - yn0) * ay + y2 - (a2 * xn1 - xn0) = y2 - a2 * y1 + y0 + a2 * (yn1 * ay + y1 - xn1) - (yn0 * ay + y0 - xn0) := by ring end theorem x_sub_y_dvd_pow (y : β„•) : βˆ€ n, (2*a*y - y*y - 1 : β„€) ∣ yz n * (a - y) + ↑(y^n) - xz n | 0 := by simp [xz, yz, int.coe_nat_zero, int.coe_nat_one] | 1 := by simp [xz, yz, int.coe_nat_zero, int.coe_nat_one] | (n+2) := have (2*a*y - y*y - 1 : β„€) ∣ ↑(y^(n + 2)) - ↑(2 * a) * ↑(y^(n + 1)) + ↑(y^n), from ⟨-↑(y^n), by { simp [pow_succ, mul_add, int.coe_nat_mul, show ((2:β„•):β„€) = 2, from rfl, mul_comm, mul_left_comm], ring }⟩, by { rw [xz_succ_succ, yz_succ_succ, x_sub_y_dvd_pow_lem ↑(y^(n+2)) ↑(y^(n+1)) ↑(y^n)], exact dvd_sub (dvd_add this $ (x_sub_y_dvd_pow (n+1)).mul_left _) (x_sub_y_dvd_pow n) } theorem xn_modeq_x2n_add_lem (n j) : xn n ∣ d * yn n * (yn n * xn j) + xn j := have h1 : d * yn n * (yn n * xn j) + xn j = (d * yn n * yn n + 1) * xn j, by simp [add_mul, mul_assoc], have h2 : d * yn n * yn n + 1 = xn n * xn n, by apply int.coe_nat_inj; repeat {rw int.coe_nat_add <|> rw int.coe_nat_mul}; exact add_eq_of_eq_sub' (eq.symm $ pell_eqz _ _), by rw h2 at h1; rw [h1, mul_assoc]; exact dvd_mul_right _ _ theorem xn_modeq_x2n_add (n j) : xn (2 * n + j) + xn j ≑ 0 [MOD xn n] := begin rw [two_mul, add_assoc, xn_add, add_assoc, ←zero_add 0], refine (dvd_mul_right (xn a1 n) (xn a1 (n + j))).modeq_zero_nat.add _, rw [yn_add, left_distrib, add_assoc, ←zero_add 0], exact ((dvd_mul_right _ _).mul_left _).modeq_zero_nat.add (xn_modeq_x2n_add_lem _ _ _).modeq_zero_nat, end lemma xn_modeq_x2n_sub_lem {n j} (h : j ≀ n) : xn (2 * n - j) + xn j ≑ 0 [MOD xn n] := have h1 : xz n ∣ ↑d * yz n * yz (n - j) + xz j, by rw [yz_sub _ h, mul_sub_left_distrib, sub_add_eq_add_sub]; exact dvd_sub (by delta xz; delta yz; repeat {rw ← int.coe_nat_add <|> rw ← int.coe_nat_mul}; rw mul_comm (xn a1 j) (yn a1 n); exact int.coe_nat_dvd.2 (xn_modeq_x2n_add_lem _ _ _)) ((dvd_mul_right _ _).mul_left _), begin rw [two_mul, add_tsub_assoc_of_le h, xn_add, add_assoc, ←zero_add 0], exact (dvd_mul_right _ _).modeq_zero_nat.add (int.coe_nat_dvd.1 $ by simpa [xz, yz] using h1).modeq_zero_nat, end theorem xn_modeq_x2n_sub {n j} (h : j ≀ 2 * n) : xn (2 * n - j) + xn j ≑ 0 [MOD xn n] := (le_total j n).elim xn_modeq_x2n_sub_lem (Ξ»jn, have 2 * n - j + j ≀ n + j, by rw [tsub_add_cancel_of_le h, two_mul]; exact nat.add_le_add_left jn _, let t := xn_modeq_x2n_sub_lem (nat.le_of_add_le_add_right this) in by rwa [tsub_tsub_cancel_of_le h, add_comm] at t) theorem xn_modeq_x4n_add (n j) : xn (4 * n + j) ≑ xn j [MOD xn n] := modeq.add_right_cancel' (xn (2 * n + j)) $ by refine @modeq.trans _ _ 0 _ _ (by rw add_comm; exact (xn_modeq_x2n_add _ _ _).symm); rw [show 4*n = 2*n + 2*n, from right_distrib 2 2 n, add_assoc]; apply xn_modeq_x2n_add theorem xn_modeq_x4n_sub {n j} (h : j ≀ 2 * n) : xn (4 * n - j) ≑ xn j [MOD xn n] := have h' : j ≀ 2*n, from le_trans h (by rw nat.succ_mul; apply nat.le_add_left), modeq.add_right_cancel' (xn (2 * n - j)) $ by refine @modeq.trans _ _ 0 _ _ (by rw add_comm; exact (xn_modeq_x2n_sub _ h).symm); rw [show 4*n = 2*n + 2*n, from right_distrib 2 2 n, add_tsub_assoc_of_le h']; apply xn_modeq_x2n_add theorem eq_of_xn_modeq_lem1 {i n} : Ξ  {j}, i < j β†’ j < n β†’ xn i % xn n < xn j % xn n | 0 ij _ := absurd ij (nat.not_lt_zero _) | (j+1) ij jn := suffices xn j % xn n < xn (j + 1) % xn n, from (lt_or_eq_of_le (nat.le_of_succ_le_succ ij)).elim (Ξ»h, lt_trans (eq_of_xn_modeq_lem1 h (le_of_lt jn)) this) (Ξ»h, by rw h; exact this), by rw [nat.mod_eq_of_lt (strict_mono_x _ (nat.lt_of_succ_lt jn)), nat.mod_eq_of_lt (strict_mono_x _ jn)]; exact strict_mono_x _ (nat.lt_succ_self _) theorem eq_of_xn_modeq_lem2 {n} (h : 2 * xn n = xn (n + 1)) : a = 2 ∧ n = 0 := by rw [xn_succ, mul_comm] at h; exact have n = 0, from n.eq_zero_or_pos.resolve_right $ Ξ»np, ne_of_lt (lt_of_le_of_lt (nat.mul_le_mul_left _ a1) (nat.lt_add_of_pos_right $ mul_pos (d_pos a1) (strict_mono_y a1 np))) h, by cases this; simp at h; exact ⟨h.symm, rfl⟩ theorem eq_of_xn_modeq_lem3 {i n} (npos : 0 < n) : Ξ  {j}, i < j β†’ j ≀ 2 * n β†’ j β‰  n β†’ Β¬(a = 2 ∧ n = 1 ∧ i = 0 ∧ j = 2) β†’ xn i % xn n < xn j % xn n | 0 ij _ _ _ := absurd ij (nat.not_lt_zero _) | (j+1) ij j2n jnn ntriv := have lem2 : βˆ€k > n, k ≀ 2*n β†’ (↑(xn k % xn n) : β„€) = xn n - xn (2 * n - k), from Ξ»k kn k2n, let k2nl := lt_of_add_lt_add_right $ show 2*n-k+k < n+k, by {rw tsub_add_cancel_of_le, rw two_mul; exact (add_lt_add_left kn n), exact k2n } in have xle : xn (2 * n - k) ≀ xn n, from le_of_lt $ strict_mono_x k2nl, suffices xn k % xn n = xn n - xn (2 * n - k), by rw [this, int.coe_nat_sub xle], by { rw ← nat.mod_eq_of_lt (nat.sub_lt (x_pos a1 n) (x_pos a1 (2 * n - k))), apply modeq.add_right_cancel' (xn a1 (2 * n - k)), rw [tsub_add_cancel_of_le xle], have t := xn_modeq_x2n_sub_lem a1 k2nl.le, rw tsub_tsub_cancel_of_le k2n at t, exact t.trans dvd_rfl.zero_modeq_nat }, (lt_trichotomy j n).elim (Ξ» (jn : j < n), eq_of_xn_modeq_lem1 ij (lt_of_le_of_ne jn jnn)) $ Ξ» o, o.elim (Ξ» (jn : j = n), by { cases jn, apply int.lt_of_coe_nat_lt_coe_nat, rw [lem2 (n+1) (nat.lt_succ_self _) j2n, show 2 * n - (n + 1) = n - 1, by rw[two_mul, tsub_add_eq_tsub_tsub, add_tsub_cancel_right]], refine lt_sub_left_of_add_lt (int.coe_nat_lt_coe_nat_of_lt _), cases (lt_or_eq_of_le $ nat.le_of_succ_le_succ ij) with lin ein, { rw nat.mod_eq_of_lt (strict_mono_x _ lin), have ll : xn a1 (n-1) + xn a1 (n-1) ≀ xn a1 n, { rw [← two_mul, mul_comm, show xn a1 n = xn a1 (n-1+1), by rw [tsub_add_cancel_of_le (succ_le_of_lt npos)], xn_succ], exact le_trans (nat.mul_le_mul_left _ a1) (nat.le_add_right _ _) }, have npm : (n-1).succ = n := nat.succ_pred_eq_of_pos npos, have il : i ≀ n - 1, { apply nat.le_of_succ_le_succ, rw npm, exact lin }, cases lt_or_eq_of_le il with ill ile, { exact lt_of_lt_of_le (nat.add_lt_add_left (strict_mono_x a1 ill) _) ll }, { rw ile, apply lt_of_le_of_ne ll, rw ← two_mul, exact Ξ»e, ntriv $ let ⟨a2, s1⟩ := @eq_of_xn_modeq_lem2 _ a1 (n-1) (by rwa [tsub_add_cancel_of_le (succ_le_of_lt npos)]) in have n1 : n = 1, from le_antisymm (tsub_eq_zero_iff_le.mp s1) npos, by rw [ile, a2, n1]; exact ⟨rfl, rfl, rfl, rfl⟩ } }, { rw [ein, nat.mod_self, add_zero], exact strict_mono_x _ (nat.pred_lt npos.ne') } }) (Ξ» (jn : j > n), have lem1 : j β‰  n β†’ xn j % xn n < xn (j + 1) % xn n β†’ xn i % xn n < xn (j + 1) % xn n, from Ξ»jn s, (lt_or_eq_of_le (nat.le_of_succ_le_succ ij)).elim (Ξ»h, lt_trans (eq_of_xn_modeq_lem3 h (le_of_lt j2n) jn $ λ⟨a1, n1, i0, j2⟩, by rw [n1, j2] at j2n; exact absurd j2n dec_trivial) s) (Ξ»h, by rw h; exact s), lem1 (ne_of_gt jn) $ int.lt_of_coe_nat_lt_coe_nat $ by { rw [lem2 j jn (le_of_lt j2n), lem2 (j+1) (nat.le_succ_of_le jn) j2n], refine sub_lt_sub_left (int.coe_nat_lt_coe_nat_of_lt $ strict_mono_x _ _) _, rw [nat.sub_succ], exact nat.pred_lt (ne_of_gt $ tsub_pos_of_lt j2n) }) theorem eq_of_xn_modeq_le {i j n} (ij : i ≀ j) (j2n : j ≀ 2 * n) (h : xn i ≑ xn j [MOD xn n]) (ntriv : Β¬(a = 2 ∧ n = 1 ∧ i = 0 ∧ j = 2)) : i = j := if npos : n = 0 then by simp [*] at * else (lt_or_eq_of_le ij).resolve_left $ Ξ»ij', if jn : j = n then by { refine ne_of_gt _ h, rw [jn, nat.mod_self], have x0 : 0 < xn a1 0 % xn a1 n := by rw [nat.mod_eq_of_lt (strict_mono_x a1 (nat.pos_of_ne_zero npos))]; exact dec_trivial, cases i with i, exact x0, rw jn at ij', exact x0.trans (eq_of_xn_modeq_lem3 _ (nat.pos_of_ne_zero npos) (nat.succ_pos _) (le_trans ij j2n) (ne_of_lt ij') $ λ⟨a1, n1, _, i2⟩, by rw [n1, i2] at ij'; exact absurd ij' dec_trivial) } else ne_of_lt (eq_of_xn_modeq_lem3 (nat.pos_of_ne_zero npos) ij' j2n jn ntriv) h theorem eq_of_xn_modeq {i j n} (i2n : i ≀ 2 * n) (j2n : j ≀ 2 * n) (h : xn i ≑ xn j [MOD xn n]) (ntriv : a = 2 β†’ n = 1 β†’ (i = 0 β†’ j β‰  2) ∧ (i = 2 β†’ j β‰  0)) : i = j := (le_total i j).elim (Ξ»ij, eq_of_xn_modeq_le ij j2n h $ λ⟨a2, n1, i0, j2⟩, (ntriv a2 n1).left i0 j2) (Ξ»ij, (eq_of_xn_modeq_le ij i2n h.symm $ λ⟨a2, n1, j0, i2⟩, (ntriv a2 n1).right i2 j0).symm) theorem eq_of_xn_modeq' {i j n} (ipos : 0 < i) (hin : i ≀ n) (j4n : j ≀ 4 * n) (h : xn j ≑ xn i [MOD xn n]) : j = i ∨ j + i = 4 * n := have i2n : i ≀ 2*n, by apply le_trans hin; rw two_mul; apply nat.le_add_left, (le_or_gt j (2 * n)).imp (Ξ»j2n : j ≀ 2 * n, eq_of_xn_modeq j2n i2n h $ Ξ»a2 n1, ⟨λj0 i2, by rw [n1, i2] at hin; exact absurd hin dec_trivial, Ξ»j2 i0, ne_of_gt ipos i0⟩) (Ξ»j2n : 2 * n < j, suffices i = 4*n - j, by rw [this, add_tsub_cancel_of_le j4n], have j42n : 4*n - j ≀ 2*n, from @nat.le_of_add_le_add_right j _ _ $ by rw [tsub_add_cancel_of_le j4n, show 4*n = 2*n + 2*n, from right_distrib 2 2 n]; exact nat.add_le_add_left (le_of_lt j2n) _, eq_of_xn_modeq i2n j42n (h.symm.trans $ let t := xn_modeq_x4n_sub j42n in by rwa [tsub_tsub_cancel_of_le j4n] at t) (Ξ»a2 n1, ⟨λi0, absurd i0 (ne_of_gt ipos), Ξ»i2, by { rw [n1, i2] at hin, exact absurd hin dec_trivial }⟩)) theorem modeq_of_xn_modeq {i j n} (ipos : 0 < i) (hin : i ≀ n) (h : xn j ≑ xn i [MOD xn n]) : j ≑ i [MOD 4 * n] ∨ j + i ≑ 0 [MOD 4 * n] := let j' := j % (4 * n) in have n4 : 0 < 4 * n, from mul_pos dec_trivial (ipos.trans_le hin), have jl : j' < 4 * n, from nat.mod_lt _ n4, have jj : j ≑ j' [MOD 4 * n], by delta modeq; rw nat.mod_eq_of_lt jl, have βˆ€j q, xn (j + 4 * n * q) ≑ xn j [MOD xn n], begin intros j q, induction q with q IH, { simp }, rw [nat.mul_succ, ← add_assoc, add_comm], exact (xn_modeq_x4n_add _ _ _).trans IH end, or.imp (Ξ»(ji : j' = i), by rwa ← ji) (Ξ»(ji : j' + i = 4 * n), (jj.add_right _).trans $ by { rw ji, exact dvd_rfl.modeq_zero_nat }) (eq_of_xn_modeq' ipos hin jl.le $ (h.symm.trans $ by { rw ← nat.mod_add_div j (4*n), exact this j' _ }).symm) end theorem xy_modeq_of_modeq {a b c} (a1 : 1 < a) (b1 : 1 < b) (h : a ≑ b [MOD c]) : βˆ€ n, xn a1 n ≑ xn b1 n [MOD c] ∧ yn a1 n ≑ yn b1 n [MOD c] | 0 := by constructor; refl | 1 := by simp; exact ⟨h, modeq.refl 1⟩ | (n+2) := ⟨ (xy_modeq_of_modeq n).left.add_right_cancel $ by { rw [xn_succ_succ a1, xn_succ_succ b1], exact (h.mul_left _ ).mul (xy_modeq_of_modeq (n+1)).left }, (xy_modeq_of_modeq n).right.add_right_cancel $ by { rw [yn_succ_succ a1, yn_succ_succ b1], exact (h.mul_left _ ).mul (xy_modeq_of_modeq (n+1)).right }⟩ theorem matiyasevic {a k x y} : (βˆƒ a1 : 1 < a, xn a1 k = x ∧ yn a1 k = y) ↔ 1 < a ∧ k ≀ y ∧ (x = 1 ∧ y = 0 ∨ βˆƒ (u v s t b : β„•), x * x - (a * a - 1) * y * y = 1 ∧ u * u - (a * a - 1) * v * v = 1 ∧ s * s - (b * b - 1) * t * t = 1 ∧ 1 < b ∧ b ≑ 1 [MOD 4 * y] ∧ b ≑ a [MOD u] ∧ 0 < v ∧ y * y ∣ v ∧ s ≑ x [MOD u] ∧ t ≑ k [MOD 4 * y]) := ⟨λ⟨a1, hx, hy⟩, by rw [← hx, ← hy]; refine ⟨a1, (nat.eq_zero_or_pos k).elim (Ξ»k0, by rw k0; exact ⟨le_rfl, or.inl ⟨rfl, rfl⟩⟩) (Ξ»kpos, _)⟩; exact let x := xn a1 k, y := yn a1 k, m := 2 * (k * y), u := xn a1 m, v := yn a1 m in have ky : k ≀ y, from yn_ge_n a1 k, have yv : y * y ∣ v, from (ysq_dvd_yy a1 k).trans $ (y_dvd_iff _ _ _).2 $ dvd_mul_left _ _, have uco : nat.coprime u (4 * y), from have 2 ∣ v, from modeq_zero_iff_dvd.1 $ (yn_modeq_two _ _).trans (dvd_mul_right _ _).modeq_zero_nat, have nat.coprime u 2, from (xy_coprime a1 m).coprime_dvd_right this, (this.mul_right this).mul_right $ (xy_coprime _ _).coprime_dvd_right (dvd_of_mul_left_dvd yv), let ⟨b, ba, bm1⟩ := chinese_remainder uco a 1 in have m1 : 1 < m, from have 0 < k * y, from mul_pos kpos (strict_mono_y a1 kpos), nat.mul_le_mul_left 2 this, have vp : 0 < v, from strict_mono_y a1 (lt_trans zero_lt_one m1), have b1 : 1 < b, from have xn a1 1 < u, from strict_mono_x a1 m1, have a < u, by simp at this; exact this, lt_of_lt_of_le a1 $ by delta modeq at ba; rw nat.mod_eq_of_lt this at ba; rw ← ba; apply nat.mod_le, let s := xn b1 k, t := yn b1 k in have sx : s ≑ x [MOD u], from (xy_modeq_of_modeq b1 a1 ba k).left, have tk : t ≑ k [MOD 4 * y], from have 4 * y ∣ b - 1, from int.coe_nat_dvd.1 $ by rw int.coe_nat_sub (le_of_lt b1); exact bm1.symm.dvd, (yn_modeq_a_sub_one _ _).of_dvd this, ⟨ky, or.inr ⟨u, v, s, t, b, pell_eq _ _, pell_eq _ _, pell_eq _ _, b1, bm1, ba, vp, yv, sx, tk⟩⟩, λ⟨a1, ky, o⟩, ⟨a1, match o with | or.inl ⟨x1, y0⟩ := by rw y0 at ky; rw [nat.eq_zero_of_le_zero ky, x1, y0]; exact ⟨rfl, rfl⟩ | or.inr ⟨u, v, s, t, b, xy, uv, st, b1, rem⟩ := match x, y, eq_pell a1 xy, u, v, eq_pell a1 uv, s, t, eq_pell b1 st, rem, ky with | ._, ._, ⟨i, rfl, rfl⟩, ._, ._, ⟨n, rfl, rfl⟩, ._, ._, ⟨j, rfl, rfl⟩, ⟨(bm1 : b ≑ 1 [MOD 4 * yn a1 i]), (ba : b ≑ a [MOD xn a1 n]), (vp : 0 < yn a1 n), (yv : yn a1 i * yn a1 i ∣ yn a1 n), (sx : xn b1 j ≑ xn a1 i [MOD xn a1 n]), (tk : yn b1 j ≑ k [MOD 4 * yn a1 i])⟩, (ky : k ≀ yn a1 i) := (nat.eq_zero_or_pos i).elim (Ξ»i0, by simp [i0] at ky; rw [i0, ky]; exact ⟨rfl, rfl⟩) $ Ξ»ipos, suffices i = k, by rw this; exact ⟨rfl, rfl⟩, by clear _x o rem xy uv st _match _match _fun_match; exact have iln : i ≀ n, from le_of_not_gt $ Ξ»hin, not_lt_of_ge (nat.le_of_dvd vp (dvd_of_mul_left_dvd yv)) (strict_mono_y a1 hin), have yd : 4 * yn a1 i ∣ 4 * n, from mul_dvd_mul_left _ $ dvd_of_ysq_dvd a1 yv, have jk : j ≑ k [MOD 4 * yn a1 i], from have 4 * yn a1 i ∣ b - 1, from int.coe_nat_dvd.1 $ by rw int.coe_nat_sub (le_of_lt b1); exact bm1.symm.dvd, ((yn_modeq_a_sub_one b1 _).of_dvd this).symm.trans tk, have ki : k + i < 4 * yn a1 i, from lt_of_le_of_lt (add_le_add ky (yn_ge_n a1 i)) $ by rw ← two_mul; exact nat.mul_lt_mul_of_pos_right dec_trivial (strict_mono_y a1 ipos), have ji : j ≑ i [MOD 4 * n], from have xn a1 j ≑ xn a1 i [MOD xn a1 n], from (xy_modeq_of_modeq b1 a1 ba j).left.symm.trans sx, (modeq_of_xn_modeq a1 ipos iln this).resolve_right $ Ξ» (ji : j + i ≑ 0 [MOD 4 * n]), not_le_of_gt ki $ nat.le_of_dvd (lt_of_lt_of_le ipos $ nat.le_add_left _ _) $ modeq_zero_iff_dvd.1 $ (jk.symm.add_right i).trans $ ji.of_dvd yd, by have : i % (4 * yn a1 i) = k % (4 * yn a1 i) := (ji.of_dvd yd).symm.trans jk; rwa [nat.mod_eq_of_lt (lt_of_le_of_lt (nat.le_add_left _ _) ki), nat.mod_eq_of_lt (lt_of_le_of_lt (nat.le_add_right _ _) ki)] at this end end⟩⟩ lemma eq_pow_of_pell_lem {a y k} (hy0 : y β‰  0) (hk0 : k β‰  0) (hyk : y^k < a) : (↑(y^k) : β„€) < 2*a*y - y*y - 1 := have hya : y < a, from (nat.le_self_pow hk0 _).trans_lt hyk, calc (↑(y ^ k) : β„€) < a : nat.cast_lt.2 hyk ... ≀ a ^ 2 - (a - 1) ^ 2 - 1 : begin rw [sub_sq, mul_one, one_pow, sub_add, sub_sub_cancel, two_mul, sub_sub, ← add_sub, le_add_iff_nonneg_right, ← bit0, sub_nonneg, ← nat.cast_two, nat.cast_le, nat.succ_le_iff], exact (one_le_iff_ne_zero.2 hy0).trans_lt hya end ... ≀ a ^ 2 - (a - y) ^ 2 - 1 : have _ := hya.le, by { mono*; simpa only [sub_nonneg, nat.cast_le, nat.one_le_cast, nat.one_le_iff_ne_zero] } ... = 2*a*y - y*y - 1 : by ring theorem eq_pow_of_pell {m n k} : n^k = m ↔ k = 0 ∧ m = 1 ∨ 0 < k ∧ (n = 0 ∧ m = 0 ∨ 0 < n ∧ βˆƒ (w a t z : β„•) (a1 : 1 < a), xn a1 k ≑ yn a1 k * (a - n) + m [MOD t] ∧ 2 * a * n = t + (n * n + 1) ∧ m < t ∧ n ≀ w ∧ k ≀ w ∧ a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1) := begin split, { rintro rfl, refine k.eq_zero_or_pos.imp (Ξ» k0, k0.symm β–Έ ⟨rfl, rfl⟩) (Ξ» hk, ⟨hk, _⟩), refine n.eq_zero_or_pos.imp (Ξ» n0, n0.symm β–Έ ⟨rfl, zero_pow hk⟩) (Ξ» hn, ⟨hn, _⟩), set w := max n k, have nw : n ≀ w, from le_max_left _ _, have kw : k ≀ w, from le_max_right _ _, have wpos : 0 < w, from hn.trans_le nw, have w1 : 1 < w + 1, from nat.succ_lt_succ wpos, set a := xn w1 w, have a1 : 1 < a, from strict_mono_x w1 wpos, have na : n ≀ a, from nw.trans (n_lt_xn w1 w).le, set x := xn a1 k, set y := yn a1 k, obtain ⟨z, ze⟩ : w ∣ yn w1 w, from modeq_zero_iff_dvd.1 ((yn_modeq_a_sub_one w1 w).trans dvd_rfl.modeq_zero_nat), have nt : (↑(n^k) : β„€) < 2 * a * n - n * n - 1, { refine eq_pow_of_pell_lem hn.ne' hk.ne' _, calc n^k ≀ n^w : nat.pow_le_pow_of_le_right hn kw ... < (w + 1)^w : nat.pow_lt_pow_of_lt_left (nat.lt_succ_of_le nw) wpos ... ≀ a : xn_ge_a_pow w1 w }, lift (2 * a * n - n * n - 1 : β„€) to β„• using ((nat.cast_nonneg _).trans nt.le) with t te, have tm : x ≑ y * (a - n) + n^k [MOD t], { apply modeq_of_dvd, rw [int.coe_nat_add, int.coe_nat_mul, int.coe_nat_sub na, te], exact x_sub_y_dvd_pow a1 n k }, have ta : 2 * a * n = t + (n * n + 1), { rw [← @nat.cast_inj β„€, int.coe_nat_add, te, sub_sub], repeat { rw nat.cast_add <|> rw nat.cast_mul }, rw [nat.cast_one, sub_add_cancel, nat.cast_two] }, have zp : a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1, from ze β–Έ pell_eq w1 w, exact ⟨w, a, t, z, a1, tm, ta, nat.cast_lt.1 nt, nw, kw, zp⟩ }, { rintro (⟨rfl, rfl⟩ | ⟨hk0, ⟨rfl, rfl⟩ | ⟨hn0, w, a, t, z, a1, tm, ta, mt, nw, kw, zp⟩⟩), { exact pow_zero n }, { exact zero_pow hk0 }, have hw0 : 0 < w, from hn0.trans_le nw, have hw1 : 1 < w + 1, from nat.succ_lt_succ hw0, rcases eq_pell hw1 zp with ⟨j, rfl, yj⟩, have hj0 : 0 < j, { apply nat.pos_of_ne_zero, rintro rfl, exact lt_irrefl 1 a1 }, have wj : w ≀ j := nat.le_of_dvd hj0 (modeq_zero_iff_dvd.1 $ (yn_modeq_a_sub_one hw1 j).symm.trans $ modeq_zero_iff_dvd.2 ⟨z, yj.symm⟩), have hnka : n ^ k < xn hw1 j, calc n^k ≀ n^j : nat.pow_le_pow_of_le_right hn0 (le_trans kw wj) ... < (w + 1)^j : nat.pow_lt_pow_of_lt_left (nat.lt_succ_of_le nw) hj0 ... ≀ xn hw1 j : xn_ge_a_pow hw1 j, have nt : (↑(n^k) : β„€) < 2 * xn hw1 j * n - n * n - 1, from eq_pow_of_pell_lem hn0.ne' hk0.ne' hnka, have na : n ≀ xn hw1 j, from (nat.le_self_pow hk0.ne' _).trans hnka.le, have te : (t : β„€) = 2 * xn hw1 j * n - n * n - 1, { rw [sub_sub, eq_sub_iff_add_eq], exact_mod_cast ta.symm }, have : xn a1 k ≑ yn a1 k * (xn hw1 j - n) + n^k [MOD t], { apply modeq_of_dvd, rw [te, nat.cast_add, nat.cast_mul, int.coe_nat_sub na], exact x_sub_y_dvd_pow a1 n k }, have : n^k % t = m % t, from (this.symm.trans tm).add_left_cancel' _, rw [← te] at nt, rwa [nat.mod_eq_of_lt (nat.cast_lt.1 nt), nat.mod_eq_of_lt mt] at this } end end pell
043f50e2104d77e1b7510133bf1635f6c69994ac
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/control/id.lean
95d289a03df26a7bb124890e174369014bbb7e0d
[ "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
678
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 identity Monad. -/ prelude import init.control.lift universe u def Id (type : Type u) : Type u := type @[inline] def Id.pure {Ξ± : Type u} (x : Ξ±) : Id Ξ± := x @[inline] def Id.bind {Ξ± Ξ² : Type u} (x : Id Ξ±) (f : Ξ± β†’ Id Ξ²) : Id Ξ² := f x @[inline] def Id.map {Ξ± Ξ² : Type u} (f : Ξ± β†’ Ξ²) (x : Id Ξ±) : Id Ξ² := f x instance : Monad Id := { pure := @Id.pure, bind := @Id.bind, map := @Id.map } @[inline] def Id.run {Ξ± : Type u} (x : Id Ξ±) : Ξ± := x instance : MonadRun id Id := ⟨@Id.run⟩
e81ecb76b9bd502a3f0cc83d971d56cc9108b654
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/data/set/finite.lean
30b1a0de30fac6f072e84f21287c8ddb00819a89
[ "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
18,082
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 Finite sets. -/ import logic.function import data.nat.basic data.fintype data.set.lattice data.set.function open set lattice function universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {ΞΉ : Sort w} namespace set /-- A set is finite if the subtype is a fintype, i.e. there is a list that enumerates its members. -/ def finite (s : set Ξ±) : Prop := nonempty (fintype s) /-- A set is infinite if it is not finite. -/ def infinite (s : set Ξ±) : Prop := Β¬ finite s /-- Construct a fintype from a finset with the same elements. -/ def fintype_of_finset {p : set Ξ±} (s : finset Ξ±) (H : βˆ€ x, x ∈ s ↔ x ∈ p) : fintype p := fintype.subtype s H @[simp] theorem card_fintype_of_finset {p : set Ξ±} (s : finset Ξ±) (H : βˆ€ x, x ∈ s ↔ x ∈ p) : @fintype.card p (fintype_of_finset s H) = s.card := fintype.subtype_card s H theorem card_fintype_of_finset' {p : set Ξ±} (s : finset Ξ±) (H : βˆ€ x, x ∈ s ↔ x ∈ p) [fintype p] : fintype.card p = s.card := by rw ← card_fintype_of_finset s H; congr /-- Construct a finset enumerating a set `s`, given a `fintype` instance. -/ def to_finset (s : set Ξ±) [fintype s] : finset Ξ± := ⟨(@finset.univ s _).1.map subtype.val, multiset.nodup_map (Ξ» a b, subtype.eq) finset.univ.2⟩ @[simp] theorem mem_to_finset {s : set Ξ±} [fintype s] {a : Ξ±} : a ∈ s.to_finset ↔ a ∈ s := by simp [to_finset] @[simp] theorem mem_to_finset_val {s : set Ξ±} [fintype s] {a : Ξ±} : a ∈ s.to_finset.1 ↔ a ∈ s := mem_to_finset noncomputable instance finite.fintype {s : set Ξ±} (h : finite s) : fintype s := classical.choice h /-- Get a finset from a finite set -/ noncomputable def finite.to_finset {s : set Ξ±} (h : finite s) : finset Ξ± := @set.to_finset _ _ (finite.fintype h) @[simp] theorem finite.mem_to_finset {s : set Ξ±} {h : finite s} {a : Ξ±} : a ∈ h.to_finset ↔ a ∈ s := @mem_to_finset _ _ (finite.fintype h) _ theorem finite.exists_finset {s : set Ξ±} : finite s β†’ βˆƒ s' : finset Ξ±, βˆ€ a : Ξ±, a ∈ s' ↔ a ∈ s | ⟨h⟩ := by exactI ⟨to_finset s, Ξ» _, mem_to_finset⟩ theorem finite.exists_finset_coe {s : set Ξ±} (hs : finite s) : βˆƒ s' : finset Ξ±, ↑s' = s := let ⟨s', h⟩ := hs.exists_finset in ⟨s', set.ext h⟩ theorem finite_mem_finset (s : finset Ξ±) : finite {a | a ∈ s} := ⟨fintype_of_finset s (Ξ» _, iff.rfl)⟩ theorem finite.of_fintype [fintype Ξ±] (s : set Ξ±) : finite s := by classical; exact ⟨set_fintype s⟩ instance decidable_mem_of_fintype [decidable_eq Ξ±] (s : set Ξ±) [fintype s] (a) : decidable (a ∈ s) := decidable_of_iff _ mem_to_finset instance fintype_empty : fintype (βˆ… : set Ξ±) := fintype_of_finset βˆ… $ by simp theorem empty_card : fintype.card (βˆ… : set Ξ±) = 0 := rfl @[simp] theorem empty_card' {h : fintype.{u} (βˆ… : set Ξ±)} : @fintype.card (βˆ… : set Ξ±) h = 0 := eq.trans (by congr) empty_card @[simp] theorem finite_empty : @finite Ξ± βˆ… := ⟨set.fintype_empty⟩ def fintype_insert' {a : Ξ±} (s : set Ξ±) [fintype s] (h : a βˆ‰ s) : fintype (insert a s : set Ξ±) := fintype_of_finset ⟨a :: s.to_finset.1, multiset.nodup_cons_of_nodup (by simp [h]) s.to_finset.2⟩ $ by simp theorem card_fintype_insert' {a : Ξ±} (s : set Ξ±) [fintype s] (h : a βˆ‰ s) : @fintype.card _ (fintype_insert' s h) = fintype.card s + 1 := by rw [fintype_insert', card_fintype_of_finset]; simp [finset.card, to_finset]; refl @[simp] theorem card_insert {a : Ξ±} (s : set Ξ±) [fintype s] (h : a βˆ‰ s) {d : fintype.{u} (insert a s : set Ξ±)} : @fintype.card _ d = fintype.card s + 1 := by rw ← card_fintype_insert' s h; congr lemma card_image_of_inj_on {s : set Ξ±} [fintype s] {f : Ξ± β†’ Ξ²} [fintype (f '' s)] (H : βˆ€x∈s, βˆ€y∈s, f x = f y β†’ x = y) : fintype.card (f '' s) = fintype.card s := by haveI := classical.prop_decidable; exact calc fintype.card (f '' s) = (s.to_finset.image f).card : card_fintype_of_finset' _ (by simp) ... = s.to_finset.card : finset.card_image_of_inj_on (Ξ» x hx y hy hxy, H x (mem_to_finset.1 hx) y (mem_to_finset.1 hy) hxy) ... = fintype.card s : (card_fintype_of_finset' _ (Ξ» a, mem_to_finset)).symm lemma card_image_of_injective (s : set Ξ±) [fintype s] {f : Ξ± β†’ Ξ²} [fintype (f '' s)] (H : function.injective f) : fintype.card (f '' s) = fintype.card s := card_image_of_inj_on $ Ξ» _ _ _ _ h, H h instance fintype_insert [decidable_eq Ξ±] (a : Ξ±) (s : set Ξ±) [fintype s] : fintype (insert a s : set Ξ±) := if h : a ∈ s then by rwa [insert_eq, union_eq_self_of_subset_left (singleton_subset_iff.2 h)] else fintype_insert' _ h @[simp] theorem finite_insert (a : Ξ±) {s : set Ξ±} : finite s β†’ finite (insert a s) | ⟨h⟩ := ⟨@set.fintype_insert _ (classical.dec_eq Ξ±) _ _ h⟩ lemma to_finset_insert [decidable_eq Ξ±] {a : Ξ±} {s : set Ξ±} (hs : finite s) : (finite_insert a hs).to_finset = insert a hs.to_finset := finset.ext.mpr $ by simp @[elab_as_eliminator] theorem finite.induction_on {C : set Ξ± β†’ Prop} {s : set Ξ±} (h : finite s) (H0 : C βˆ…) (H1 : βˆ€ {a s}, a βˆ‰ s β†’ finite s β†’ C s β†’ C (insert a s)) : C s := let ⟨t⟩ := h in by exactI match s.to_finset, @mem_to_finset _ s _ with | ⟨l, nd⟩, al := begin change βˆ€ a, a ∈ l ↔ a ∈ s at al, clear _let_match _match t h, revert s nd al, refine multiset.induction_on l _ (Ξ» a l IH, _); intros s nd al, { rw show s = βˆ…, from eq_empty_iff_forall_not_mem.2 (by simpa using al), exact H0 }, { rw ← show insert a {x | x ∈ l} = s, from set.ext (by simpa using al), cases multiset.nodup_cons.1 nd with m nd', refine H1 _ ⟨finset.subtype.fintype ⟨l, nd'⟩⟩ (IH nd' (Ξ» _, iff.rfl)), exact m } end end @[elab_as_eliminator] theorem finite.dinduction_on {C : βˆ€s:set Ξ±, finite s β†’ Prop} {s : set Ξ±} (h : finite s) (H0 : C βˆ… finite_empty) (H1 : βˆ€ {a s}, a βˆ‰ s β†’ βˆ€h:finite s, C s h β†’ C (insert a s) (finite_insert a h)) : C s h := have βˆ€h:finite s, C s h, from finite.induction_on h (assume h, H0) (assume a s has hs ih h, H1 has hs (ih _)), this h instance fintype_singleton (a : Ξ±) : fintype ({a} : set Ξ±) := fintype_insert' _ (not_mem_empty _) @[simp] theorem card_singleton (a : Ξ±) : fintype.card ({a} : set Ξ±) = 1 := by rw [show fintype.card ({a} : set Ξ±) = _, from card_fintype_insert' βˆ… (not_mem_empty a)]; refl @[simp] theorem finite_singleton (a : Ξ±) : finite ({a} : set Ξ±) := ⟨set.fintype_singleton _⟩ instance fintype_pure : βˆ€ a : Ξ±, fintype (pure a : set Ξ±) := set.fintype_singleton theorem finite_pure (a : Ξ±) : finite (pure a : set Ξ±) := ⟨set.fintype_pure a⟩ instance fintype_univ [fintype Ξ±] : fintype (@univ Ξ±) := fintype_of_finset finset.univ $ Ξ» _, iff_true_intro (finset.mem_univ _) theorem finite_univ [fintype Ξ±] : finite (@univ Ξ±) := ⟨set.fintype_univ⟩ instance fintype_union [decidable_eq Ξ±] (s t : set Ξ±) [fintype s] [fintype t] : fintype (s βˆͺ t : set Ξ±) := fintype_of_finset (s.to_finset βˆͺ t.to_finset) $ by simp theorem finite_union {s t : set Ξ±} : finite s β†’ finite t β†’ finite (s βˆͺ t) | ⟨hs⟩ ⟨ht⟩ := ⟨@set.fintype_union _ (classical.dec_eq Ξ±) _ _ hs ht⟩ instance fintype_sep (s : set Ξ±) (p : Ξ± β†’ Prop) [fintype s] [decidable_pred p] : fintype ({a ∈ s | p a} : set Ξ±) := fintype_of_finset (s.to_finset.filter p) $ by simp instance fintype_inter (s t : set Ξ±) [fintype s] [decidable_pred t] : fintype (s ∩ t : set Ξ±) := set.fintype_sep s t def fintype_subset (s : set Ξ±) {t : set Ξ±} [fintype s] [decidable_pred t] (h : t βŠ† s) : fintype t := by rw ← inter_eq_self_of_subset_right h; apply_instance theorem finite_subset {s : set Ξ±} : finite s β†’ βˆ€ {t : set Ξ±}, t βŠ† s β†’ finite t | ⟨hs⟩ t h := ⟨@set.fintype_subset _ _ _ hs (classical.dec_pred t) h⟩ instance fintype_image [decidable_eq Ξ²] (s : set Ξ±) (f : Ξ± β†’ Ξ²) [fintype s] : fintype (f '' s) := fintype_of_finset (s.to_finset.image f) $ by simp instance fintype_range [decidable_eq Ξ²] (f : Ξ± β†’ Ξ²) [fintype Ξ±] : fintype (range f) := fintype_of_finset (finset.univ.image f) $ by simp [range] theorem finite_range (f : Ξ± β†’ Ξ²) [fintype Ξ±] : finite (range f) := by haveI := classical.dec_eq Ξ²; exact ⟨by apply_instance⟩ theorem finite_image {s : set Ξ±} (f : Ξ± β†’ Ξ²) : finite s β†’ finite (f '' s) | ⟨h⟩ := ⟨@set.fintype_image _ _ (classical.dec_eq Ξ²) _ _ h⟩ instance fintype_map {Ξ± Ξ²} [decidable_eq Ξ²] : βˆ€ (s : set Ξ±) (f : Ξ± β†’ Ξ²) [fintype s], fintype (f <$> s) := set.fintype_image theorem finite_map {Ξ± Ξ²} {s : set Ξ±} : βˆ€ (f : Ξ± β†’ Ξ²), finite s β†’ finite (f <$> s) := finite_image def fintype_of_fintype_image [decidable_eq Ξ²] (s : set Ξ±) {f : Ξ± β†’ Ξ²} {g} (I : is_partial_inv f g) [fintype (f '' s)] : fintype s := fintype_of_finset ⟨_, @multiset.nodup_filter_map Ξ² Ξ± g _ (@injective_of_partial_inv_right _ _ f g I) (f '' s).to_finset.2⟩ $ Ξ» a, begin suffices : (βˆƒ b x, f x = b ∧ g b = some a ∧ x ∈ s) ↔ a ∈ s, by simpa [exists_and_distrib_left.symm, and.comm, and.left_comm, and.assoc], rw exists_swap, suffices : (βˆƒ x, x ∈ s ∧ g (f x) = some a) ↔ a ∈ s, {simpa [and.comm, and.left_comm, and.assoc]}, simp [I _, (injective_of_partial_inv I).eq_iff] end theorem finite_of_finite_image_on {s : set Ξ±} {f : Ξ± β†’ Ξ²} (hi : set.inj_on f s) : finite (f '' s) β†’ finite s | ⟨h⟩ := ⟨@fintype.of_injective _ _ h (Ξ»a:s, ⟨f a.1, mem_image_of_mem f a.2⟩) $ assume a b eq, subtype.eq $ hi a.2 b.2 $ subtype.ext.1 eq⟩ theorem finite_image_iff_on {s : set Ξ±} {f : Ξ± β†’ Ξ²} (hi : inj_on f s) : finite (f '' s) ↔ finite s := ⟨finite_of_finite_image_on hi, finite_image _⟩ theorem finite_of_finite_image {s : set Ξ±} {f : Ξ± β†’ Ξ²} (I : injective f) : finite (f '' s) β†’ finite s := finite_of_finite_image_on (assume _ _ _ _ eq, I eq) theorem finite_preimage {s : set Ξ²} {f : Ξ± β†’ Ξ²} (I : injective f) (h : finite s) : finite (f ⁻¹' s) := finite_of_finite_image I (finite_subset h (image_preimage_subset f s)) instance fintype_Union [decidable_eq Ξ±] {ΞΉ : Type*} [fintype ΞΉ] (f : ΞΉ β†’ set Ξ±) [βˆ€ i, fintype (f i)] : fintype (⋃ i, f i) := fintype_of_finset (finset.univ.bind (Ξ» i, (f i).to_finset)) $ by simp theorem finite_Union {ΞΉ : Type*} [fintype ΞΉ] {f : ΞΉ β†’ set Ξ±} (H : βˆ€i, finite (f i)) : finite (⋃ i, f i) := ⟨@set.fintype_Union _ (classical.dec_eq Ξ±) _ _ _ (Ξ» i, finite.fintype (H i))⟩ def fintype_bUnion [decidable_eq Ξ±] {ΞΉ : Type*} {s : set ΞΉ} [fintype s] (f : ΞΉ β†’ set Ξ±) (H : βˆ€ i ∈ s, fintype (f i)) : fintype (⋃ i ∈ s, f i) := by rw bUnion_eq_Union; exact @set.fintype_Union _ _ _ _ _ (by rintro ⟨i, hi⟩; exact H i hi) instance fintype_bUnion' [decidable_eq Ξ±] {ΞΉ : Type*} {s : set ΞΉ} [fintype s] (f : ΞΉ β†’ set Ξ±) [H : βˆ€ i, fintype (f i)] : fintype (⋃ i ∈ s, f i) := fintype_bUnion _ (Ξ» i _, H i) theorem finite_sUnion {s : set (set Ξ±)} (h : finite s) (H : βˆ€t∈s, finite t) : finite (⋃₀ s) := by rw sUnion_eq_Union; haveI := finite.fintype h; apply finite_Union; simpa using H theorem finite_bUnion {Ξ±} {ΞΉ : Type*} {s : set ΞΉ} {f : ΞΉ β†’ set Ξ±} : finite s β†’ (βˆ€i, finite (f i)) β†’ finite (⋃ i∈s, f i) | ⟨hs⟩ h := by rw [bUnion_eq_Union]; exactI finite_Union (Ξ» i, h _) instance fintype_lt_nat (n : β„•) : fintype {i | i < n} := fintype_of_finset (finset.range n) $ by simp instance fintype_le_nat (n : β„•) : fintype {i | i ≀ n} := by simpa [nat.lt_succ_iff] using set.fintype_lt_nat (n+1) lemma finite_le_nat (n : β„•) : finite {i | i ≀ n} := ⟨set.fintype_le_nat _⟩ lemma finite_lt_nat (n : β„•) : finite {i | i < n} := ⟨set.fintype_lt_nat _⟩ instance fintype_prod (s : set Ξ±) (t : set Ξ²) [fintype s] [fintype t] : fintype (set.prod s t) := fintype_of_finset (s.to_finset.product t.to_finset) $ by simp lemma finite_prod {s : set Ξ±} {t : set Ξ²} : finite s β†’ finite t β†’ finite (set.prod s t) | ⟨hs⟩ ⟨ht⟩ := by exactI ⟨set.fintype_prod s t⟩ def fintype_bind {Ξ± Ξ²} [decidable_eq Ξ²] (s : set Ξ±) [fintype s] (f : Ξ± β†’ set Ξ²) (H : βˆ€ a ∈ s, fintype (f a)) : fintype (s >>= f) := set.fintype_bUnion _ H instance fintype_bind' {Ξ± Ξ²} [decidable_eq Ξ²] (s : set Ξ±) [fintype s] (f : Ξ± β†’ set Ξ²) [H : βˆ€ a, fintype (f a)] : fintype (s >>= f) := fintype_bind _ _ (Ξ» i _, H i) theorem finite_bind {Ξ± Ξ²} {s : set Ξ±} {f : Ξ± β†’ set Ξ²} : finite s β†’ (βˆ€ a ∈ s, finite (f a)) β†’ finite (s >>= f) | ⟨hs⟩ H := ⟨@fintype_bind _ _ (classical.dec_eq Ξ²) _ hs _ (Ξ» a ha, (H a ha).fintype)⟩ def fintype_seq {Ξ± Ξ² : Type u} [decidable_eq Ξ²] (f : set (Ξ± β†’ Ξ²)) (s : set Ξ±) [fintype f] [fintype s] : fintype (f <*> s) := by rw seq_eq_bind_map; apply set.fintype_bind' theorem finite_seq {Ξ± Ξ² : Type u} {f : set (Ξ± β†’ Ξ²)} {s : set Ξ±} : finite f β†’ finite s β†’ finite (f <*> s) | ⟨hf⟩ ⟨hs⟩ := by haveI := classical.dec_eq Ξ²; exactI ⟨fintype_seq _ _⟩ /-- There are finitely many subsets of a given finite set -/ lemma finite_subsets_of_finite {Ξ± : Type u} {a : set Ξ±} (h : finite a) : finite {b | b βŠ† a} := begin -- we just need to translate the result, already known for finsets, -- to the language of finite sets let s := coe '' ((finset.powerset (finite.to_finset h)).to_set), have : finite s := finite_image _ (finite_mem_finset _), have : {b | b βŠ† a} βŠ† s := begin assume b hb, rw [set.mem_image], rw [set.mem_set_of_eq] at hb, let b' : finset Ξ± := finite.to_finset (finite_subset h hb), have : b' ∈ (finset.powerset (finite.to_finset h)).to_set := show b' ∈ (finset.powerset (finite.to_finset h)), by simp [b', finset.subset_iff]; exact hb, have : coe b' = b := by ext; simp, exact ⟨b', by assumption, by assumption⟩ end, exact finite_subset β€Ήfinite sβ€Ί this end end set namespace finset variables [decidable_eq Ξ²] variables {s t u : finset Ξ±} {f : Ξ± β†’ Ξ²} {a : Ξ±} lemma finite_to_set (s : finset Ξ±) : set.finite (↑s : set Ξ±) := set.finite_mem_finset s @[simp] lemma coe_bind {f : Ξ± β†’ finset Ξ²} : ↑(s.bind f) = (⋃x ∈ (↑s : set Ξ±), ↑(f x) : set Ξ²) := by simp [set.ext_iff] @[simp] lemma coe_to_finset {s : set Ξ±} {hs : set.finite s} : ↑(hs.to_finset) = s := by simp [set.ext_iff] @[simp] lemma coe_to_finset' [decidable_eq Ξ±] (s : set Ξ±) [fintype s] : (↑s.to_finset : set Ξ±) = s := by ext; simp end finset namespace set lemma finite_subset_Union {s : set Ξ±} (hs : finite s) {ΞΉ} {t : ΞΉ β†’ set Ξ±} (h : s βŠ† ⋃ i, t i) : βˆƒ I : set ΞΉ, finite I ∧ s βŠ† ⋃ i ∈ I, t i := begin unfreezeI, cases hs, choose f hf using show βˆ€ x : s, βˆƒ i, x.1 ∈ t i, {simpa [subset_def] using h}, refine ⟨range f, finite_range f, _⟩, rintro x hx, simp, exact ⟨_, ⟨_, hx, rfl⟩, hf ⟨x, hx⟩⟩ end lemma infinite_univ_nat : infinite (univ : set β„•) := assume (h : finite (univ : set β„•)), let ⟨n, hn⟩ := finset.exists_nat_subset_range h.to_finset in have n ∈ finset.range n, from finset.subset_iff.mpr hn $ by simp, by simp * at * lemma not_injective_nat_fintype [fintype Ξ±] [decidable_eq Ξ±] {f : β„• β†’ Ξ±} : Β¬ injective f := assume (h : injective f), have finite (f '' univ), from finite_subset (finset.finite_to_set $ fintype.elems Ξ±) (assume a h, fintype.complete a), have finite (univ : set β„•), from finite_of_finite_image h this, infinite_univ_nat this lemma not_injective_int_fintype [fintype Ξ±] [decidable_eq Ξ±] {f : β„€ β†’ Ξ±} : Β¬ injective f := assume hf, have injective (f ∘ (coe : β„• β†’ β„€)), from injective_comp hf $ assume i j, int.of_nat_inj, not_injective_nat_fintype this lemma card_lt_card {s t : set Ξ±} [fintype s] [fintype t] (h : s βŠ‚ t) : fintype.card s < fintype.card t := begin haveI := classical.prop_decidable, rw [← finset.coe_to_finset' s, ← finset.coe_to_finset' t, finset.coe_ssubset] at h, rw [card_fintype_of_finset' _ (Ξ» x, mem_to_finset), card_fintype_of_finset' _ (Ξ» x, mem_to_finset)], exact finset.card_lt_card h, end lemma card_le_of_subset {s t : set Ξ±} [fintype s] [fintype t] (hsub : s βŠ† t) : fintype.card s ≀ fintype.card t := calc fintype.card s = s.to_finset.card : set.card_fintype_of_finset' _ (by simp) ... ≀ t.to_finset.card : finset.card_le_of_subset (Ξ» x hx, by simp [set.subset_def, *] at *) ... = fintype.card t : eq.symm (set.card_fintype_of_finset' _ (by simp)) lemma eq_of_subset_of_card_le {s t : set Ξ±} [fintype s] [fintype t] (hsub : s βŠ† t) (hcard : fintype.card t ≀ fintype.card s) : s = t := classical.by_contradiction (Ξ» h, lt_irrefl (fintype.card t) (have fintype.card s < fintype.card t := set.card_lt_card ⟨hsub, h⟩, by rwa [le_antisymm (card_le_of_subset hsub) hcard] at this)) lemma card_range_of_injective [fintype Ξ±] {f : Ξ± β†’ Ξ²} (hf : injective f) [fintype (range f)] : fintype.card (range f) = fintype.card Ξ± := eq.symm $ fintype.card_congr (@equiv.of_bijective _ _ (Ξ» a : Ξ±, show range f, from ⟨f a, a, rfl⟩) ⟨λ x y h, hf $ subtype.mk.inj h, Ξ» b, let ⟨a, ha⟩ := b.2 in ⟨a, by simp *⟩⟩) lemma finite.exists_maximal_wrt [partial_order Ξ²] (f : Ξ± β†’ Ξ²) (s : set Ξ±) (h : set.finite s) : s β‰  βˆ… β†’ βˆƒa∈s, βˆ€a'∈s, f a ≀ f a' β†’ f a = f a' := begin classical, refine h.induction_on _ _, { assume h, contradiction }, assume a s his _ ih _, by_cases s = βˆ…, { use a, simp [h] }, rcases ih h with ⟨b, hb, ih⟩, by_cases f b ≀ f a, { refine ⟨a, set.mem_insert _ _, assume c hc hac, le_antisymm hac _⟩, rcases set.mem_insert_iff.1 hc with rfl | hcs, { refl }, { rwa [← ih c hcs (le_trans h hac)] } }, { refine ⟨b, set.mem_insert_of_mem _ hb, assume c hc hbc, _⟩, rcases set.mem_insert_iff.1 hc with rfl | hcs, { exact (h hbc).elim }, { exact ih c hcs hbc } } end end set
a0b3e4dda1c29c163ef28252e37cef6c2df2284e
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/PrettyPrinter/Formatter.lean
fa407525f7e862023352f84496fed4467c64745d
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
23,174
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.CoreM import Lean.Parser.Extension import Lean.KeyedDeclsAttribute import Lean.ParserCompiler.Attribute import Lean.PrettyPrinter.Basic /-! The formatter turns a `Syntax` tree into a `Format` object, inserting both mandatory whitespace (to separate adjacent tokens) as well as "pretty" optional whitespace. The basic approach works much like the parenthesizer: A right-to-left traversal over the syntax tree, driven by parser-specific handlers registered via attributes. The traversal is right-to-left so that when emitting a token, we already know the text following it and can decide whether or not whitespace between the two is necessary. -/ namespace Lean namespace PrettyPrinter namespace Formatter structure Context where options : Options table : Parser.TokenTable structure State where stxTrav : Syntax.Traverser -- Textual content of `stack` up to the first whitespace (not enclosed in an escaped ident). We assume that the textual -- content of `stack` is modified only by `pushText` and `pushLine`, so `leadWord` is adjusted there accordingly. leadWord : String := "" -- Whether the generated format begins with the result of an ungrouped category formatter. isUngrouped : Bool := false -- Whether the resulting format must be grouped when used in a category formatter. -- If the flag is set to false, then categoryParser omits the fill+nest operation. mustBeGrouped : Bool := true -- Stack of generated Format objects, analogous to the Syntax stack in the parser. -- Note, however, that the stack is reversed because of the right-to-left traversal. stack : Array Format := #[] end Formatter abbrev FormatterM := ReaderT Formatter.Context $ StateRefT Formatter.State CoreM @[inline] def FormatterM.orElse {Ξ±} (p₁ : FormatterM Ξ±) (pβ‚‚ : Unit β†’ FormatterM Ξ±) : FormatterM Ξ± := do let s ← get catchInternalId backtrackExceptionId p₁ (fun _ => do set s; pβ‚‚ ()) instance {Ξ±} : OrElse (FormatterM Ξ±) := ⟨FormatterM.orElse⟩ abbrev Formatter := FormatterM Unit unsafe def mkFormatterAttribute : IO (KeyedDeclsAttribute Formatter) := KeyedDeclsAttribute.init { builtinName := `builtinFormatter, name := `formatter, descr := "Register a formatter for a parser. [formatter k] registers a declaration of type `Lean.PrettyPrinter.Formatter` for the `SyntaxNodeKind` `k`.", valueTypeName := `Lean.PrettyPrinter.Formatter, evalKey := fun builtin stx => do let env ← getEnv let id ← Attribute.Builtin.getId stx -- `isValidSyntaxNodeKind` is updated only in the next stage for new `[builtin*Parser]`s, but we try to -- synthesize a formatter for it immediately, so we just check for a declaration in this case if (builtin && (env.find? id).isSome) || Parser.isValidSyntaxNodeKind env id then pure id else throwError "invalid [formatter] argument, unknown syntax kind '{id}'" } `Lean.PrettyPrinter.formatterAttribute @[builtinInit mkFormatterAttribute] constant formatterAttribute : KeyedDeclsAttribute Formatter unsafe def mkCombinatorFormatterAttribute : IO ParserCompiler.CombinatorAttribute := ParserCompiler.registerCombinatorAttribute `combinatorFormatter "Register a formatter for a parser combinator. [combinatorFormatter c] registers a declaration of type `Lean.PrettyPrinter.Formatter` for the `Parser` declaration `c`. Note that, unlike with [formatter], this is not a node kind since combinators usually do not introduce their own node kinds. The tagged declaration may optionally accept parameters corresponding to (a prefix of) those of `c`, where `Parser` is replaced with `Formatter` in the parameter types." @[builtinInit mkCombinatorFormatterAttribute] constant combinatorFormatterAttribute : ParserCompiler.CombinatorAttribute namespace Formatter open Lean.Core open Lean.Parser def throwBacktrack {Ξ±} : FormatterM Ξ± := throw $ Exception.internal backtrackExceptionId instance : Syntax.MonadTraverser FormatterM := ⟨{ get := State.stxTrav <$> get, set := fun t => modify (fun st => { st with stxTrav := t }), modifyGet := fun f => modifyGet (fun st => let (a, t) := f st.stxTrav; (a, { st with stxTrav := t })) }⟩ open Syntax.MonadTraverser def getStack : FormatterM (Array Format) := do let st ← get pure st.stack def getStackSize : FormatterM Nat := do let stack ← getStack; pure stack.size def setStack (stack : Array Format) : FormatterM Unit := modify fun st => { st with stack := stack } private def push (f : Format) : FormatterM Unit := modify fun st => { st with stack := st.stack.push f, isUngrouped := false } def pushWhitespace (f : Format) : FormatterM Unit := do push f modify fun st => { st with leadWord := "", isUngrouped := false } def pushLine : FormatterM Unit := pushWhitespace Format.line /-- Execute `x` at the right-most child of the current node, if any, then advance to the left. -/ def visitArgs (x : FormatterM Unit) : FormatterM Unit := do let stx ← getCur if stx.getArgs.size > 0 then goDown (stx.getArgs.size - 1) *> x <* goUp goLeft /-- Execute `x`, pass array of generated Format objects to `fn`, and push result. -/ def fold (fn : Array Format β†’ Format) (x : FormatterM Unit) : FormatterM Unit := do let sp ← getStackSize x let stack ← getStack let f := fn $ stack.extract sp stack.size setStack $ (stack.shrink sp).push f /-- Execute `x` and concatenate generated Format objects. -/ def concat (x : FormatterM Unit) : FormatterM Unit := do fold (Array.foldl (fun acc f => if acc.isNil then f else f ++ acc) Format.nil) x def indent (x : Formatter) (indent : Option Int := none) : Formatter := do concat x let ctx ← read let indent := indent.getD $ Std.Format.getIndent ctx.options modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) (Format.nest indent) } def fill (x : Formatter) : Formatter := do concat x modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) Format.fill isUngrouped := false } def group (x : Formatter) : Formatter := do concat x modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) Format.group isUngrouped := false } /-- If `pos?` has a position, run `x` and tag its results with that position. Otherwise just run `x`. -/ def withMaybeTag (pos? : Option String.Pos) (x : FormatterM Unit) : Formatter := do if let some p := pos? then concat x modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) (Format.tag p) } else x @[combinatorFormatter Lean.Parser.orelse] def orelse.formatter (p1 p2 : Formatter) : Formatter := -- HACK: We have no (immediate) information on which side of the orelse could have produced the current node, so try -- them in turn. Uses the syntax traverser non-linearly! p1 <|> p2 -- `mkAntiquot` is quite complex, so we'd rather have its formatter synthesized below the actual parser definition. -- Note that there is a mutual recursion -- `categoryParser -> mkAntiquot -> termParser -> categoryParser`, so we need to introduce an indirection somewhere -- anyway. @[extern "lean_mk_antiquot_formatter"] constant mkAntiquot.formatter' (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Formatter -- break up big mutual recursion @[extern "lean_pretty_printer_formatter_interpret_parser_descr"] constant interpretParserDescr' : ParserDescr β†’ CoreM Formatter private def SourceInfo.getExprPos? : SourceInfo β†’ Option Nat | SourceInfo.synthetic pos _ => pos | _ => none private def getExprPos? : Syntax β†’ Option Nat | Syntax.node info _ _ => SourceInfo.getExprPos? info | Syntax.atom info _ => SourceInfo.getExprPos? info | Syntax.ident info _ _ _ => SourceInfo.getExprPos? info | Syntax.missing => none unsafe def formatterForKindUnsafe (k : SyntaxNodeKind) : Formatter := do if k == `missing then push "<missing>" goLeft else let stx ← getCur let f ← runForNodeKind formatterAttribute k interpretParserDescr' withMaybeTag (getExprPos? stx) f @[implementedBy formatterForKindUnsafe] constant formatterForKind (k : SyntaxNodeKind) : Formatter @[combinatorFormatter Lean.Parser.withAntiquot] def withAntiquot.formatter (antiP p : Formatter) : Formatter := -- TODO: could be optimized using `isAntiquot` (which would have to be moved), but I'd rather -- fix the backtracking hack outright. orelse.formatter antiP p @[combinatorFormatter Lean.Parser.withAntiquotSuffixSplice] def withAntiquotSuffixSplice.formatter (k : SyntaxNodeKind) (p suffix : Formatter) : Formatter := do if (← getCur).isAntiquotSuffixSplice then visitArgs <| suffix *> p else p @[combinatorFormatter Lean.Parser.tokenWithAntiquot] def tokenWithAntiquot.formatter (p : Formatter) : Formatter := do if (← getCur).isTokenAntiquot then visitArgs p else p def categoryFormatterCore (cat : Name) : Formatter := do modify fun st => { st with mustBeGrouped := true, isUngrouped := false } let stx ← getCur trace[PrettyPrinter.format] "formatting {indentD (format stx)}" if stx.getKind == `choice then visitArgs do -- format only last choice -- TODO: We could use elaborator data here to format the chosen child when available formatterForKind (← getCur).getKind else withAntiquot.formatter (mkAntiquot.formatter' cat.toString none) (formatterForKind stx.getKind) modify fun st => { st with mustBeGrouped := true, isUngrouped := !st.mustBeGrouped } @[combinatorFormatter Lean.Parser.categoryParser] def categoryParser.formatter (cat : Name) : Formatter := do concat <| categoryFormatterCore cat unless (← get).isUngrouped do let indent := Std.Format.getIndent (← read).options modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) fun fmt => fmt.nest indent |>.fill } def categoryFormatter (cat : Name) : Formatter := fill <| indent <| categoryFormatterCore cat @[combinatorFormatter Lean.Parser.categoryParserOfStack] def categoryParserOfStack.formatter (offset : Nat) : Formatter := do let st ← get let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset) categoryParser.formatter stx.getId @[combinatorFormatter Lean.Parser.parserOfStack] def parserOfStack.formatter (offset : Nat) (prec : Nat := 0) : Formatter := do let st ← get let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset) formatterForKind stx.getKind @[combinatorFormatter Lean.Parser.error] def error.formatter (msg : String) : Formatter := pure () @[combinatorFormatter Lean.Parser.errorAtSavedPos] def errorAtSavedPos.formatter (msg : String) (delta : Bool) : Formatter := pure () @[combinatorFormatter Lean.Parser.atomic] def atomic.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.lookahead] def lookahead.formatter (p : Formatter) : Formatter := pure () @[combinatorFormatter Lean.Parser.notFollowedBy] def notFollowedBy.formatter (p : Formatter) : Formatter := pure () @[combinatorFormatter Lean.Parser.andthen] def andthen.formatter (p1 p2 : Formatter) : Formatter := p2 *> p1 def checkKind (k : SyntaxNodeKind) : FormatterM Unit := do let stx ← getCur if k != stx.getKind then trace[PrettyPrinter.format.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'" throwBacktrack @[combinatorFormatter Lean.Parser.node] def node.formatter (k : SyntaxNodeKind) (p : Formatter) : Formatter := do checkKind k; visitArgs p @[combinatorFormatter Lean.Parser.trailingNode] def trailingNode.formatter (k : SyntaxNodeKind) (_ _ : Nat) (p : Formatter) : Formatter := do checkKind k visitArgs do p; -- leading term, not actually produced by `p` categoryParser.formatter `foo def parseToken (s : String) : FormatterM ParserState := do -- include comment tokens, e.g. when formatting `- -0` (Parser.andthenFn Parser.whitespace (Parser.tokenFn [])) { input := s, fileName := "", fileMap := FileMap.ofString "", prec := 0, env := ← getEnv, options := ← getOptions, tokens := (← read).table } (Parser.mkParserState s) def pushToken (info : SourceInfo) (tk : String) : FormatterM Unit := do match info with | SourceInfo.original _ _ ss _ => -- preserve non-whitespace content (i.e. comments) let ss' := ss.trim if !ss'.isEmpty then let ws := { ss with startPos := ss'.stopPos } if ws.contains '\n' then push s!"\n{ss'}" else push s!" {ss'}" modify fun st => { st with leadWord := "" } | _ => pure () let st ← get -- If there is no space between `tk` and the next word, see if we would parse more than `tk` as a single token if st.leadWord != "" && tk.trimRight == tk then let tk' := tk.trimLeft let t ← parseToken $ tk' ++ st.leadWord if t.pos <= tk'.bsize then -- stopped within `tk` => use it as is, extend `leadWord` if not prefixed by whitespace push tk modify fun st => { st with leadWord := if tk.trimLeft == tk then tk ++ st.leadWord else "" } else -- stopped after `tk` => add space push $ tk ++ " " modify fun st => { st with leadWord := if tk.trimLeft == tk then tk else "" } else -- already separated => use `tk` as is if st.leadWord == "" then push tk.trimRight else if tk.endsWith " " then pushLine push tk.trimRight else push tk -- preserve special whitespace for tokens like ":=\n" modify fun st => { st with leadWord := if tk.trimLeft == tk then tk else "" } match info with | SourceInfo.original ss _ _ _ => -- preserve non-whitespace content (i.e. comments) let ss' := ss.trim if !ss'.isEmpty then let ws := { ss with startPos := ss'.stopPos } if ws.contains '\n' then do -- Indentation is automatically increased when entering a category, but comments should be aligned -- with the actual token, so dedent indent (push s!"{ss'}\n") (some ((0:Int) - Std.Format.getIndent (← getOptions))) else pushLine push ss'.toString modify fun st => { st with leadWord := "" } | _ => pure () @[combinatorFormatter Lean.Parser.symbolNoAntiquot] def symbolNoAntiquot.formatter (sym : String) : Formatter := do let stx ← getCur if stx.isToken sym then do let (Syntax.atom info _) ← pure stx | unreachable! pushToken info sym goLeft else do trace[PrettyPrinter.format.backtrack] "unexpected syntax '{format stx}', expected symbol '{sym}'" throwBacktrack @[combinatorFormatter Lean.Parser.nonReservedSymbolNoAntiquot] def nonReservedSymbolNoAntiquot.formatter := symbolNoAntiquot.formatter @[combinatorFormatter Lean.Parser.rawCh] def rawCh.formatter (ch : Char) := symbolNoAntiquot.formatter ch.toString @[combinatorFormatter Lean.Parser.unicodeSymbolNoAntiquot] def unicodeSymbolNoAntiquot.formatter (sym asciiSym : String) : Formatter := do let Syntax.atom info val ← getCur | throwError m!"not an atom: {← getCur}" if val == sym.trim then pushToken info sym else pushToken info asciiSym; goLeft @[combinatorFormatter Lean.Parser.identNoAntiquot] def identNoAntiquot.formatter : Formatter := do checkKind identKind let Syntax.ident info _ id _ ← getCur | throwError m!"not an ident: {← getCur}" let id := id.simpMacroScopes pushToken info id.toString goLeft @[combinatorFormatter Lean.Parser.rawIdentNoAntiquot] def rawIdentNoAntiquot.formatter : Formatter := do checkKind identKind let Syntax.ident info _ id _ ← getCur | throwError m!"not an ident: {← getCur}" pushToken info id.toString goLeft @[combinatorFormatter Lean.Parser.identEq] def identEq.formatter (id : Name) := rawIdentNoAntiquot.formatter def visitAtom (k : SyntaxNodeKind) : Formatter := do let stx ← getCur if k != Name.anonymous then checkKind k let Syntax.atom info val ← pure $ stx.ifNode (fun n => n.getArg 0) (fun _ => stx) | throwError m!"not an atom: {stx}" pushToken info val goLeft @[combinatorFormatter Lean.Parser.charLitNoAntiquot] def charLitNoAntiquot.formatter := visitAtom charLitKind @[combinatorFormatter Lean.Parser.strLitNoAntiquot] def strLitNoAntiquot.formatter := visitAtom strLitKind @[combinatorFormatter Lean.Parser.nameLitNoAntiquot] def nameLitNoAntiquot.formatter := visitAtom nameLitKind @[combinatorFormatter Lean.Parser.numLitNoAntiquot] def numLitNoAntiquot.formatter := visitAtom numLitKind @[combinatorFormatter Lean.Parser.scientificLitNoAntiquot] def scientificLitNoAntiquot.formatter := visitAtom scientificLitKind @[combinatorFormatter Lean.Parser.fieldIdx] def fieldIdx.formatter := visitAtom fieldIdxKind @[combinatorFormatter Lean.Parser.manyNoAntiquot] def manyNoAntiquot.formatter (p : Formatter) : Formatter := do let stx ← getCur visitArgs $ stx.getArgs.size.forM fun _ => p @[combinatorFormatter Lean.Parser.many1NoAntiquot] def many1NoAntiquot.formatter (p : Formatter) : Formatter := manyNoAntiquot.formatter p @[combinatorFormatter Lean.Parser.optionalNoAntiquot] def optionalNoAntiquot.formatter (p : Formatter) : Formatter := visitArgs p @[combinatorFormatter Lean.Parser.many1Unbox] def many1Unbox.formatter (p : Formatter) : Formatter := do let stx ← getCur if stx.getKind == nullKind then do manyNoAntiquot.formatter p else p @[combinatorFormatter Lean.Parser.sepByNoAntiquot] def sepByNoAntiquot.formatter (p pSep : Formatter) : Formatter := do let stx ← getCur visitArgs $ (List.range stx.getArgs.size).reverse.forM $ fun i => if i % 2 == 0 then p else pSep @[combinatorFormatter Lean.Parser.sepBy1NoAntiquot] def sepBy1NoAntiquot.formatter := sepByNoAntiquot.formatter @[combinatorFormatter Lean.Parser.withPosition] def withPosition.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withoutPosition] def withoutPosition.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withForbidden] def withForbidden.formatter (tk : Token) (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withoutForbidden] def withoutForbidden.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withoutInfo] def withoutInfo.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.setExpected] def setExpected.formatter (expected : List String) (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.incQuotDepth] def incQuotDepth.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.decQuotDepth] def decQuotDepth.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.suppressInsideQuot] def suppressInsideQuot.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.evalInsideQuot] def evalInsideQuot.formatter (declName : Name) (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.checkWsBefore] def checkWsBefore.formatter : Formatter := do let st ← get if st.leadWord != "" then pushLine @[combinatorFormatter Lean.Parser.checkPrec] def checkPrec.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkLhsPrec] def checkLhsPrec.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.setLhsPrec] def setLhsPrec.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkStackTop] def checkStackTop.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkNoWsBefore] def checkNoWsBefore.formatter : Formatter := -- prevent automatic whitespace insertion modify fun st => { st with leadWord := "" } @[combinatorFormatter Lean.Parser.checkLinebreakBefore] def checkLinebreakBefore.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkTailWs] def checkTailWs.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkColGe] def checkColGe.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkColGt] def checkColGt.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkLineEq] def checkLineEq.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.eoi] def eoi.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.notFollowedByCategoryToken] def notFollowedByCategoryToken.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkNoImmediateColon] def checkNoImmediateColon.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkInsideQuot] def checkInsideQuot.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkOutsideQuot] def checkOutsideQuot.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.skip] def skip.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.pushNone] def pushNone.formatter : Formatter := goLeft @[combinatorFormatter Lean.Parser.withOpenDecl] def withOpenDecl.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withOpen] def withOpen.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.interpolatedStr] def interpolatedStr.formatter (p : Formatter) : Formatter := do visitArgs $ (← getCur).getArgs.reverse.forM fun chunk => match chunk.isLit? interpolatedStrLitKind with | some str => push str *> goLeft | none => p @[combinatorFormatter Lean.Parser.dbgTraceState] def dbgTraceState.formatter (label : String) (p : Formatter) : Formatter := p @[combinatorFormatter ite, macroInline] def ite {Ξ± : Type} (c : Prop) [h : Decidable c] (t e : Formatter) : Formatter := if c then t else e abbrev FormatterAliasValue := AliasValue Formatter builtin_initialize formatterAliasesRef : IO.Ref (NameMap FormatterAliasValue) ← IO.mkRef {} def registerAlias (aliasName : Name) (v : FormatterAliasValue) : IO Unit := do Parser.registerAliasCore formatterAliasesRef aliasName v instance : Coe Formatter FormatterAliasValue := { coe := AliasValue.const } instance : Coe (Formatter β†’ Formatter) FormatterAliasValue := { coe := AliasValue.unary } instance : Coe (Formatter β†’ Formatter β†’ Formatter) FormatterAliasValue := { coe := AliasValue.binary } end Formatter open Formatter def format (formatter : Formatter) (stx : Syntax) : CoreM Format := do trace[PrettyPrinter.format.input] "{Std.format stx}" let options ← getOptions let table ← Parser.builtinTokenTable.get catchInternalId backtrackExceptionId (do let (_, st) ← (concat formatter { table := table, options := options }).run { stxTrav := Syntax.Traverser.fromSyntax stx }; pure $ Format.fill $ st.stack.get! 0) (fun _ => throwError "format: uncaught backtrack exception") def formatCategory (cat : Name) := format $ categoryFormatter cat def formatTerm := formatCategory `term def formatTactic := formatCategory `tactic def formatCommand := formatCategory `command builtin_initialize registerTraceClass `PrettyPrinter.format; end PrettyPrinter end Lean
e1062bbde692931890f0c13f799bd493a52399f9
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Init.lean
d87f6604ad35fb2bb260d467bd460c62a363b409
[ "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
441
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Prelude import Init.Notation import Init.Core import Init.Control import Init.Data.Basic import Init.WF import Init.Data import Init.System import Init.Util import Init.Fix import Init.Meta import Init.NotationExtra import Init.SimpLemmas import Init.Hints
b0e2cfc9ddafc83ee5bef4f1caa9b74715db6b9e
453dcd7c0d1ef170b0843a81d7d8caedc9741dce
/data/array/lemmas.lean
4ceaf6dc574f55e982a4a98886be96dc867eab6d
[ "Apache-2.0" ]
permissive
amswerdlow/mathlib
9af77a1f08486d8fa059448ae2d97795bd12ec0c
27f96e30b9c9bf518341705c99d641c38638dfd0
refs/heads/master
1,585,200,953,598
1,534,275,532,000
1,534,275,532,000
144,564,700
0
0
null
1,534,156,197,000
1,534,156,197,000
null
UTF-8
Lean
false
false
9,086
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, Mario Carneiro -/ import data.list.basic data.buffer category.traversable.equiv data.vector2 universes u w namespace d_array variables {n : nat} {Ξ± : fin n β†’ Type u} {Ξ² : Type w} instance [βˆ€ i, inhabited (Ξ± i)] : inhabited (d_array n Ξ±) := ⟨⟨λ _, default _⟩⟩ end d_array namespace array variables {n m : nat} {Ξ± : Type u} {Ξ² : Type w} instance [inhabited Ξ±] : inhabited (array n Ξ±) := d_array.inhabited theorem rev_list_foldr_aux (a : array n Ξ±) (b : Ξ²) (f : Ξ± β†’ Ξ² β†’ Ξ²) : Ξ  (i : nat) (h : i ≀ n), (d_array.iterate_aux a (Ξ» _ v l, v :: l) i h []).foldr f b = d_array.iterate_aux a (Ξ» _, f) i h b | 0 h := rfl | (j+1) h := congr_arg (f (read a ⟨j, h⟩)) (rev_list_foldr_aux j _) theorem rev_list_foldr (a : array n Ξ±) (b : Ξ²) (f : Ξ± β†’ Ξ² β†’ Ξ²) : a.rev_list.foldr f b = a.foldl b f := rev_list_foldr_aux a b f _ _ theorem mem.def (v : Ξ±) (a : array n Ξ±) : v ∈ a ↔ βˆƒ i : fin n, read a i = v := iff.rfl theorem rev_list_reverse_core (a : array n Ξ±) : Ξ  i (h : i ≀ n) (t : list Ξ±), (a.iterate_aux (Ξ» _ v l, v :: l) i h []).reverse_core t = a.rev_iterate_aux (Ξ» _ v l, v :: l) i h t | 0 h t := rfl | (i+1) h t := rev_list_reverse_core i _ _ @[simp] theorem rev_list_reverse (a : array n Ξ±) : a.rev_list.reverse = a.to_list := rev_list_reverse_core a _ _ _ @[simp] theorem to_list_reverse (a : array n Ξ±) : a.to_list.reverse = a.rev_list := by rw [← rev_list_reverse, list.reverse_reverse] theorem rev_list_length_aux (a : array n Ξ±) (i h) : (a.iterate_aux (Ξ» _ v l, v :: l) i h []).length = i := by induction i; simp [*, d_array.iterate_aux] @[simp] theorem rev_list_length (a : array n Ξ±) : a.rev_list.length = n := rev_list_length_aux a _ _ @[simp] theorem to_list_length (a : array n Ξ±) : a.to_list.length = n := by rw[← rev_list_reverse, list.length_reverse, rev_list_length] theorem to_list_nth_le_core (a : array n Ξ±) (i : β„•) (ih : i < n) : Ξ  (j) {jh t h'}, (βˆ€k tl, j + k = i β†’ list.nth_le t k tl = a.read ⟨i, ih⟩) β†’ (a.rev_iterate_aux (Ξ» _ v l, v :: l) j jh t).nth_le i h' = a.read ⟨i, ih⟩ | 0 _ _ _ al := al i _ $ zero_add _ | (j+1) jh t h' al := to_list_nth_le_core j $ Ξ»k tl hjk, show list.nth_le (a.read ⟨j, jh⟩ :: t) k tl = a.read ⟨i, ih⟩, from match k, hjk, tl with | 0, e, tl := match i, e, ih with ._, rfl, _ := rfl end | k'+1, _, tl := by simp[list.nth_le]; exact al _ _ (by simp [*]) end theorem to_list_nth_le (a : array n Ξ±) (i : β„•) (h h') : list.nth_le a.to_list i h' = a.read ⟨i, h⟩ := to_list_nth_le_core _ _ _ _ (Ξ»k tl, absurd tl $ nat.not_lt_zero _) @[simp] theorem to_list_nth_le' (a : array n Ξ±) (i : fin n) (h') : list.nth_le a.to_list i.1 h' = a.read i := by cases i; apply to_list_nth_le theorem to_list_nth {a : array n Ξ±} {i : β„•} {v} : list.nth a.to_list i = some v ↔ βˆƒ h, a.read ⟨i, h⟩ = v := begin rw list.nth_eq_some, have ll := to_list_length a, split; intro h; cases h with h e; subst v, { exact ⟨ll β–Έ h, (to_list_nth_le _ _ _ _).symm⟩ }, { exact ⟨ll.symm β–Έ h, to_list_nth_le _ _ _ _⟩ } end theorem to_list_foldl (a : array n Ξ±) (b : Ξ²) (f : Ξ² β†’ Ξ± β†’ Ξ²) : a.to_list.foldl f b = a.foldl b (function.swap f) := by rw [← rev_list_reverse, list.foldl_reverse, rev_list_foldr] theorem write_to_list {a : array n Ξ±} {i v} : (a.write i v).to_list = a.to_list.update_nth i.1 v := list.ext_le (by simp) $ Ξ» j h₁ hβ‚‚, begin have h₃ : j < n, {simpa using h₁}, rw [to_list_nth_le _ _ h₃], refine let ⟨_, e⟩ := list.nth_eq_some.1 _ in e.symm, by_cases ij : i.1 = j, { subst j, rw [show fin.mk i.val h₃ = i, from fin.eq_of_veq rfl, array.read_write, list.nth_update_nth_of_lt], simp [h₃] }, { rw [list.nth_update_nth_ne _ _ ij, a.read_write_of_ne, to_list_nth.2 ⟨h₃, rfl⟩], exact fin.ne_of_vne ij } end theorem mem_rev_list_core (a : array n Ξ±) (v : Ξ±) : Ξ  i (h : i ≀ n), (βˆƒ (j : fin n), j.1 < i ∧ read a j = v) ↔ v ∈ a.iterate_aux (Ξ» _ v l, v :: l) i h [] | 0 _ := ⟨λ⟨_, n, _⟩, absurd n $ nat.not_lt_zero _, false.elim⟩ | (i+1) h := let IH := mem_rev_list_core i (le_of_lt h) in ⟨λ⟨j, ji1, e⟩, or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ ji1) (Ξ»ji, list.mem_cons_of_mem _ $ IH.1 ⟨j, ji, e⟩) (Ξ»je, by simp [d_array.iterate_aux]; apply or.inl; unfold read at e; have H : j = ⟨i, h⟩ := fin.eq_of_veq je; rwa [← H, e]), Ξ»m, begin simp [d_array.iterate_aux, list.mem] at m, cases m with e m', exact ⟨⟨i, h⟩, nat.lt_succ_self _, eq.symm e⟩, exact let ⟨j, ji, e⟩ := IH.2 m' in ⟨j, nat.le_succ_of_le ji, e⟩ end⟩ @[simp] theorem mem_rev_list (a : array n Ξ±) (v : Ξ±) : v ∈ a.rev_list ↔ v ∈ a := iff.symm $ iff.trans (exists_congr $ Ξ»j, iff.symm $ show j.1 < n ∧ read a j = v ↔ read a j = v, from and_iff_right j.2) (mem_rev_list_core a v _ _) @[simp] theorem mem_to_list (a : array n Ξ±) (v : Ξ±) : v ∈ a.to_list ↔ v ∈ a := by rw ← rev_list_reverse; simp [-rev_list_reverse] theorem mem_to_list_enum (a : array n Ξ±) {i v} : (i, v) ∈ a.to_list.enum ↔ βˆƒ h, a.read ⟨i, h⟩ = v := by simp [list.mem_iff_nth, to_list_nth, and.comm, and.assoc, and.left_comm] @[simp] theorem to_list_to_array (a : array n Ξ±) : a.to_list.to_array == a := heq_of_heq_of_eq (@@eq.drec_on (Ξ» m (e : a.to_list.length = m), (d_array.mk (Ξ»v, a.to_list.nth_le v.1 v.2)) == (@d_array.mk m (Ξ»_, Ξ±) $ Ξ»v, a.to_list.nth_le v.1 $ e.symm β–Έ v.2)) a.to_list_length heq.rfl) $ d_array.ext $ λ⟨i, h⟩, to_list_nth_le _ i h _ @[simp] theorem to_array_to_list (l : list Ξ±) : l.to_array.to_list = l := list.ext_le (to_list_length _) $ Ξ»n h1 h2, to_list_nth_le _ _ _ _ theorem to_list_of_heq {aβ‚€ : array n Ξ±} {a₁ : array m Ξ±} (h : n = m) (h' : aβ‚€ == a₁) : aβ‚€.to_list = a₁.to_list := by congr; assumption lemma push_back_rev_list_core (a : array n Ξ±) (v : Ξ±) : βˆ€ i h h', d_array.iterate_aux (a.push_back v) (Ξ»_, list.cons) i h [] = d_array.iterate_aux a (Ξ»_, list.cons) i h' [] | 0 h h' := rfl | (i+1) h h' := begin simp [d_array.iterate_aux], refine ⟨_, push_back_rev_list_core _ _ _⟩, dsimp [read, d_array.read, push_back], rw [dif_neg], refl, exact ne_of_lt h', end @[simp] theorem push_back_rev_list (a : array n Ξ±) (v : Ξ±) : (a.push_back v).rev_list = v :: a.rev_list := begin unfold push_back rev_list foldl iterate d_array.iterate, dsimp [d_array.iterate_aux, read, d_array.read, push_back], rw [dif_pos (eq.refl n)], apply congr_arg, apply push_back_rev_list_core end @[simp] theorem push_back_to_list (a : array n Ξ±) (v : Ξ±) : (a.push_back v).to_list = a.to_list ++ [v] := by rw [← rev_list_reverse, ← rev_list_reverse, push_back_rev_list, list.reverse_cons] theorem read_foreach_aux (f : fin n β†’ Ξ± β†’ Ξ±) (ai : array n Ξ±) : βˆ€ i h (a : array n Ξ±) (j : fin n), j.1 < i β†’ (d_array.iterate_aux ai (Ξ» i v a', write a' i (f i v)) i h a).read j = f j (ai.read j) | 0 hi a ⟨j, hj⟩ ji := absurd ji (nat.not_lt_zero _) | (i+1) hi a ⟨j, hj⟩ ji := begin dsimp [d_array.iterate_aux], dsimp at ji, by_cases e : (⟨i, hi⟩ : fin _) = ⟨j, hj⟩, { rw [e], simp, refl }, { rw [read_write_of_ne _ _ e, read_foreach_aux _ _ _ ⟨j, hj⟩], exact (lt_or_eq_of_le (nat.le_of_lt_succ ji)).resolve_right (ne.symm $ mt (@fin.eq_of_veq _ ⟨i, hi⟩ ⟨j, hj⟩) e) } end theorem read_foreach (a : array n Ξ±) (f : fin n β†’ Ξ± β†’ Ξ±) (i : fin n) : (foreach a f).read i = f i (a.read i) := read_foreach_aux _ _ _ _ _ _ i.2 theorem read_map (f : Ξ± β†’ Ξ±) (a : array n Ξ±) (i : fin n) : (map f a).read i = f (a.read i) := read_foreach _ _ _ theorem read_mapβ‚‚ (f : Ξ± β†’ Ξ± β†’ Ξ±) (a b : array n Ξ±) (i : fin n) : (mapβ‚‚ f a b).read i = f (a.read i) (b.read i) := read_foreach _ _ _ end array instance (Ξ±) [decidable_eq Ξ±] : decidable_eq (buffer Ξ±) := by tactic.mk_dec_eq_instance namespace equiv def d_array_equiv_fin {n : β„•} (Ξ± : fin n β†’ Type*) : d_array n Ξ± ≃ (Ξ  i, Ξ± i) := ⟨d_array.read, d_array.mk, Ξ» ⟨f⟩, rfl, Ξ» f, rfl⟩ def array_equiv_fin (n : β„•) (Ξ± : Type*) : array n Ξ± ≃ (fin n β†’ Ξ±) := d_array_equiv_fin _ def vector_equiv_fin (Ξ± : Type*) (n : β„•) : vector Ξ± n ≃ (fin n β†’ Ξ±) := ⟨vector.nth, vector.of_fn, vector.of_fn_nth, Ξ» f, funext $ vector.nth_of_fn f⟩ def vector_equiv_array (Ξ± : Type*) (n : β„•) : vector Ξ± n ≃ array n Ξ± := (vector_equiv_fin _ _).trans (array_equiv_fin _ _).symm end equiv namespace array open function variable {n : β„•} instance : traversable.{u} (array n) := @equiv.traversable (flip vector n) _ (Ξ» Ξ±, equiv.vector_equiv_array Ξ± n) _ instance : is_lawful_traversable.{u} (array n) := @equiv.is_lawful_traversable (flip vector n) _ (Ξ» Ξ±, equiv.vector_equiv_array Ξ± n) _ _ end array
4c8a0ee509fc0efd9c680c42b9962b0b8a797006
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/homology/homology.lean
30166c8f1f5ef7fc4d46bcd70c0ce58872b1f31a
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
10,408
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.homology.image_to_kernel import algebra.homology.homological_complex import category_theory.graded_object /-! # The homology of a complex Given `C : homological_complex V c`, we have `C.cycles i` and `C.boundaries i`, both defined as subobjects of `C.X i`. We show these are functorial with respect to chain maps, as `C.cycles_map f i` and `C.boundaries_map f i`. As a consequence we construct `homology_functor i : homological_complex V c β₯€ V`, computing the `i`-th homology. -/ universes v u open category_theory category_theory.limits variables {ΞΉ : Type*} variables {V : Type u} [category.{v} V] [has_zero_morphisms V] variables {c : complex_shape ΞΉ} (C : homological_complex V c) open_locale classical zero_object noncomputable theory namespace homological_complex section cycles variables [has_kernels V] /-- The cycles at index `i`, as a subobject. -/ abbreviation cycles (i : ΞΉ) : subobject (C.X i) := kernel_subobject (C.d_from i) lemma cycles_eq_kernel_subobject {i j : ΞΉ} (r : c.rel i j) : C.cycles i = kernel_subobject (C.d i j) := C.kernel_from_eq_kernel r /-- The underlying object of `C.cycles i` is isomorphic to `kernel (C.d i j)`, for any `j` such that `rel i j`. -/ def cycles_iso_kernel {i j : ΞΉ} (r : c.rel i j) : (C.cycles i : V) β‰… kernel (C.d i j) := subobject.iso_of_eq _ _ (C.cycles_eq_kernel_subobject r) β‰ͺ≫ kernel_subobject_iso (C.d i j) lemma cycles_eq_top {i} (h : Β¬c.rel i (c.next i)) : C.cycles i = ⊀ := begin rw eq_top_iff, apply le_kernel_subobject, rw [C.d_from_eq_zero h, comp_zero], end end cycles section boundaries variables [has_images V] /-- The boundaries at index `i`, as a subobject. -/ abbreviation boundaries (C : homological_complex V c) (j : ΞΉ) : subobject (C.X j) := image_subobject (C.d_to j) lemma boundaries_eq_image_subobject [has_equalizers V] {i j : ΞΉ} (r : c.rel i j) : C.boundaries j = image_subobject (C.d i j) := C.image_to_eq_image r /-- The underlying object of `C.boundaries j` is isomorphic to `image (C.d i j)`, for any `i` such that `rel i j`. -/ def boundaries_iso_image [has_equalizers V] {i j : ΞΉ} (r : c.rel i j) : (C.boundaries j : V) β‰… image (C.d i j) := subobject.iso_of_eq _ _ (C.boundaries_eq_image_subobject r) β‰ͺ≫ image_subobject_iso (C.d i j) lemma boundaries_eq_bot [has_zero_object V] {j} (h : Β¬c.rel (c.prev j) j) : C.boundaries j = βŠ₯ := begin rw eq_bot_iff, refine image_subobject_le _ 0 _, rw [C.d_to_eq_zero h, zero_comp], end end boundaries section variables [has_kernels V] [has_images V] lemma boundaries_le_cycles (C : homological_complex V c) (i : ΞΉ) : C.boundaries i ≀ C.cycles i := image_le_kernel _ _ (C.d_to_comp_d_from i) /-- The canonical map from `boundaries i` to `cycles i`. -/ abbreviation boundaries_to_cycles (C : homological_complex V c) (i : ΞΉ) : (C.boundaries i : V) ⟢ (C.cycles i : V) := image_to_kernel _ _ (C.d_to_comp_d_from i) /-- Prefer `boundaries_to_cycles`. -/ @[simp] lemma image_to_kernel_as_boundaries_to_cycles (C : homological_complex V c) (i : ΞΉ) (h) : (C.boundaries i).of_le (C.cycles i) h = C.boundaries_to_cycles i := rfl variables [has_cokernels V] /-- The homology of a complex at index `i`. -/ abbreviation homology (C : homological_complex V c) (i : ΞΉ) : V := homology (C.d_to i) (C.d_from i) (C.d_to_comp_d_from i) /-- The `j`th homology of a homological complex (as kernel of 'the differential from `Cβ±Ό`' modulo the image of 'the differential to `Cβ±Ό`') is isomorphic to the kernel of `d : Cβ±Ό β†’ Cβ‚–` modulo the image of `d : Cα΅’ β†’ Cβ±Ό` when `rel i j` and `rel j k`. -/ def homology_iso (C : homological_complex V c) {i j k : ΞΉ} (hij : c.rel i j) (hjk : c.rel j k) : C.homology j β‰… _root_.homology (C.d i j) (C.d j k) (C.d_comp_d i j k) := homology.map_iso _ _ (arrow.iso_mk (C.X_prev_iso hij) (iso.refl _) $ by dsimp; rw [C.d_to_eq hij, category.comp_id]) (arrow.iso_mk (iso.refl _) (C.X_next_iso hjk) $ by dsimp; rw [C.d_from_comp_X_next_iso hjk, category.id_comp]) rfl end end homological_complex /-- The 0th homology of a chain complex is isomorphic to the cokernel of `d : C₁ ⟢ Cβ‚€`. -/ def chain_complex.homology_zero_iso [has_kernels V] [has_images V] [has_cokernels V] (C : chain_complex V β„•) [epi (factor_thru_image (C.d 1 0))] : C.homology 0 β‰… cokernel (C.d 1 0) := (homology.map_iso _ _ (arrow.iso_mk (C.X_prev_iso rfl) (iso.refl _) $ by rw C.d_to_eq rfl; exact (category.comp_id _).symm : arrow.mk (C.d_to 0) β‰… arrow.mk (C.d 1 0)) (arrow.iso_mk (iso.refl _) (iso.refl _) $ by simp [C.d_from_eq_zero (Ξ» (h : _ = _), one_ne_zero $ by rwa chain_complex.next_nat_zero at h)] : arrow.mk (C.d_from 0) β‰… arrow.mk 0) rfl).trans $ homology_of_zero_right _ /-- The 0th cohomology of a cochain complex is isomorphic to the kernel of `d : Cβ‚€ β†’ C₁`. -/ def cochain_complex.homology_zero_iso [has_zero_object V] [has_kernels V] [has_images V] [has_cokernels V] (C : cochain_complex V β„•) : C.homology 0 β‰… kernel (C.d 0 1) := (homology.map_iso _ _ (arrow.iso_mk (C.X_prev_iso_self (by rw cochain_complex.prev_nat_zero; exact one_ne_zero)) (iso.refl _) (by simp) : arrow.mk (C.d_to 0) β‰… arrow.mk 0) (arrow.iso_mk (iso.refl _) (C.X_next_iso rfl) (by simp) : arrow.mk (C.d_from 0) β‰… arrow.mk (C.d 0 1)) $ by simpa).trans $ homology_of_zero_left _ /-- The `n + 1`th homology of a chain complex (as kernel of 'the differential from `Cβ‚™β‚Šβ‚`' modulo the image of 'the differential to `Cβ‚™β‚Šβ‚`') is isomorphic to the kernel of `d : Cβ‚™β‚Šβ‚ β†’ Cβ‚™` modulo the image of `d : Cβ‚™β‚Šβ‚‚ β†’ Cβ‚™β‚Šβ‚`. -/ def chain_complex.homology_succ_iso [has_kernels V] [has_images V] [has_cokernels V] (C : chain_complex V β„•) (n : β„•) : C.homology (n + 1) β‰… homology (C.d (n + 2) (n + 1)) (C.d (n + 1) n) (C.d_comp_d _ _ _) := C.homology_iso rfl rfl /-- The `n + 1`th cohomology of a cochain complex (as kernel of 'the differential from `Cβ‚™β‚Šβ‚`' modulo the image of 'the differential to `Cβ‚™β‚Šβ‚`') is isomorphic to the kernel of `d : Cβ‚™β‚Šβ‚ β†’ Cβ‚™β‚Šβ‚‚` modulo the image of `d : Cβ‚™ β†’ Cβ‚™β‚Šβ‚`. -/ def cochain_complex.homology_succ_iso [has_kernels V] [has_images V] [has_cokernels V] (C : cochain_complex V β„•) (n : β„•) : C.homology (n + 1) β‰… homology (C.d n (n + 1)) (C.d (n + 1) (n + 2)) (C.d_comp_d _ _ _) := C.homology_iso rfl rfl open homological_complex /-! Computing the cycles is functorial. -/ section variables [has_kernels V] variables {C₁ Cβ‚‚ C₃ : homological_complex V c} (f : C₁ ⟢ Cβ‚‚) /-- The morphism between cycles induced by a chain map. -/ abbreviation cycles_map (f : C₁ ⟢ Cβ‚‚) (i : ΞΉ) : (C₁.cycles i : V) ⟢ (Cβ‚‚.cycles i : V) := subobject.factor_thru _ ((C₁.cycles i).arrow ≫ f.f i) (kernel_subobject_factors _ _ (by simp)) @[simp, reassoc, elementwise] lemma cycles_map_arrow (f : C₁ ⟢ Cβ‚‚) (i : ΞΉ) : (cycles_map f i) ≫ (Cβ‚‚.cycles i).arrow = (C₁.cycles i).arrow ≫ f.f i := by { simp, } @[simp] lemma cycles_map_id (i : ΞΉ) : cycles_map (πŸ™ C₁) i = πŸ™ _ := by { dunfold cycles_map, simp, } @[simp] lemma cycles_map_comp (f : C₁ ⟢ Cβ‚‚) (g : Cβ‚‚ ⟢ C₃) (i : ΞΉ) : cycles_map (f ≫ g) i = cycles_map f i ≫ cycles_map g i := by { dunfold cycles_map, simp [subobject.factor_thru_right], } variables (V c) /-- Cycles as a functor. -/ @[simps] def cycles_functor (i : ΞΉ) : homological_complex V c β₯€ V := { obj := Ξ» C, C.cycles i, map := Ξ» C₁ Cβ‚‚ f, cycles_map f i, } end /-! Computing the boundaries is functorial. -/ section variables [has_images V] [has_image_maps V] variables {C₁ Cβ‚‚ C₃ : homological_complex V c} (f : C₁ ⟢ Cβ‚‚) /-- The morphism between boundaries induced by a chain map. -/ abbreviation boundaries_map (f : C₁ ⟢ Cβ‚‚) (i : ΞΉ) : (C₁.boundaries i : V) ⟢ (Cβ‚‚.boundaries i : V) := image_subobject_map (f.sq_to i) variables (V c) /-- Boundaries as a functor. -/ @[simps] def boundaries_functor (i : ΞΉ) : homological_complex V c β₯€ V := { obj := Ξ» C, C.boundaries i, map := Ξ» C₁ Cβ‚‚ f, image_subobject_map (f.sq_to i), } end section /-! The `boundaries_to_cycles` morphisms are natural. -/ variables [has_equalizers V] [has_images V] [has_image_maps V] variables {C₁ Cβ‚‚ : homological_complex V c} (f : C₁ ⟢ Cβ‚‚) @[simp, reassoc] lemma boundaries_to_cycles_naturality (i : ΞΉ) : boundaries_map f i ≫ Cβ‚‚.boundaries_to_cycles i = C₁.boundaries_to_cycles i ≫ cycles_map f i := by { ext, simp, } variables (V c) /-- The natural transformation from the boundaries functor to the cycles functor. -/ @[simps] def boundaries_to_cycles_nat_trans (i : ΞΉ) : boundaries_functor V c i ⟢ cycles_functor V c i := { app := Ξ» C, C.boundaries_to_cycles i, naturality' := Ξ» C₁ Cβ‚‚ f, boundaries_to_cycles_naturality f i, } /-- The `i`-th homology, as a functor to `V`. -/ @[simps] def homology_functor [has_cokernels V] (i : ΞΉ) : homological_complex V c β₯€ V := -- It would be nice if we could just write -- `cokernel (boundaries_to_cycles_nat_trans V c i)` -- here, but universe implementation details get in the way... { obj := Ξ» C, C.homology i, map := Ξ» C₁ Cβ‚‚ f, _root_.homology.map _ _ (f.sq_to i) (f.sq_from i) rfl, map_id' := begin intros, ext1, simp only [homology.Ο€_map, kernel_subobject_map_id, hom.sq_from_id, category.id_comp, category.comp_id] end, map_comp' := begin intros, ext1, simp only [hom.sq_from_comp, kernel_subobject_map_comp, homology.Ο€_map_assoc, homology.Ο€_map, category.assoc] end } /-- The homology functor from `ΞΉ`-indexed complexes to `ΞΉ`-graded objects in `V`. -/ @[simps] def graded_homology_functor [has_cokernels V] : homological_complex V c β₯€ graded_object ΞΉ V := { obj := Ξ» C i, C.homology i, map := Ξ» C C' f i, (homology_functor V c i).map f, map_id' := begin intros, ext, simp only [pi.id_apply, homology.Ο€_map, homology_functor_map, kernel_subobject_map_id, hom.sq_from_id, category.id_comp, category.comp_id] end, map_comp' := begin intros, ext, simp only [hom.sq_from_comp, kernel_subobject_map_comp, homology.Ο€_map_assoc, pi.comp_apply, homology.Ο€_map, homology_functor_map, category.assoc] end } end
2988e315b6d2a6c5ea2996f04589bb19ca63ef4d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/set/prod.lean
be7358befe8d0ed370fc9986525df8919c06677c
[ "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,452
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes HΓΆlzl, Patrick Massot -/ import data.set.basic /-! # Sets in product and pi types This file defines the product of sets in `Ξ± Γ— Ξ²` and in `Ξ  i, Ξ± i` along with the diagonal of a type. ## Main declarations * `set.prod`: Binary product of sets. For `s : set Ξ±`, `t : set Ξ²`, we have `s.prod t : set (Ξ± Γ— Ξ²)`. * `set.diagonal`: Diagonal of a type. `set.diagonal Ξ± = {(x, x) | x : Ξ±}`. * `set.off_diag`: Off-diagonal. `s Γ—Λ’ s` without the diagonal. * `set.pi`: Arbitrary product of sets. -/ open function namespace set /-! ### Cartesian binary product of sets -/ section prod variables {Ξ± Ξ² Ξ³ Ξ΄ : Type*} {s s₁ sβ‚‚ : set Ξ±} {t t₁ tβ‚‚ : set Ξ²} {a : Ξ±} {b : Ξ²} /-- The cartesian product `prod s t` is the set of `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ def prod (s : set Ξ±) (t : set Ξ²) : set (Ξ± Γ— Ξ²) := {p | p.1 ∈ s ∧ p.2 ∈ t} /- This notation binds more strongly than (pre)images, unions and intersections. -/ infixr (name := set.prod) ` Γ—Λ’ `:82 := set.prod lemma prod_eq (s : set Ξ±) (t : set Ξ²) : s Γ—Λ’ t = prod.fst ⁻¹' s ∩ prod.snd ⁻¹' t := rfl lemma mem_prod_eq {p : Ξ± Γ— Ξ²} : p ∈ s Γ—Λ’ t = (p.1 ∈ s ∧ p.2 ∈ t) := rfl @[simp] lemma mem_prod {p : Ξ± Γ— Ξ²} : p ∈ s Γ—Λ’ t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[simp] lemma prod_mk_mem_set_prod_eq : (a, b) ∈ s Γ—Λ’ t = (a ∈ s ∧ b ∈ t) := rfl lemma mk_mem_prod (ha : a ∈ s) (hb : b ∈ t) : (a, b) ∈ s Γ—Λ’ t := ⟨ha, hb⟩ instance decidable_mem_prod [hs : decidable_pred (∈ s)] [ht : decidable_pred (∈ t)] : decidable_pred (∈ (s Γ—Λ’ t)) := Ξ» _, and.decidable lemma prod_mono (hs : s₁ βŠ† sβ‚‚) (ht : t₁ βŠ† tβ‚‚) : s₁ Γ—Λ’ t₁ βŠ† sβ‚‚ Γ—Λ’ tβ‚‚ := Ξ» x ⟨h₁, hβ‚‚βŸ©, ⟨hs h₁, ht hβ‚‚βŸ© @[simp] lemma prod_self_subset_prod_self : s₁ Γ—Λ’ s₁ βŠ† sβ‚‚ Γ—Λ’ sβ‚‚ ↔ s₁ βŠ† sβ‚‚ := ⟨λ h x hx, (h (mk_mem_prod hx hx)).1, Ξ» h x hx, ⟨h hx.1, h hx.2⟩⟩ @[simp] lemma prod_self_ssubset_prod_self : s₁ Γ—Λ’ s₁ βŠ‚ sβ‚‚ Γ—Λ’ sβ‚‚ ↔ s₁ βŠ‚ sβ‚‚ := and_congr prod_self_subset_prod_self $ not_congr prod_self_subset_prod_self lemma prod_subset_iff {P : set (Ξ± Γ— Ξ²)} : s Γ—Λ’ t βŠ† P ↔ βˆ€ (x ∈ s) (y ∈ t), (x, y) ∈ P := ⟨λ h _ hx _ hy, h (mk_mem_prod hx hy), Ξ» h ⟨_, _⟩ hp, h _ hp.1 _ hp.2⟩ lemma forall_prod_set {p : Ξ± Γ— Ξ² β†’ Prop} : (βˆ€ x ∈ s Γ—Λ’ t, p x) ↔ βˆ€ (x ∈ s) (y ∈ t), p (x, y) := prod_subset_iff lemma exists_prod_set {p : Ξ± Γ— Ξ² β†’ Prop} : (βˆƒ x ∈ s Γ—Λ’ t, p x) ↔ βˆƒ (x ∈ s) (y ∈ t), p (x, y) := by simp [and_assoc] @[simp] lemma prod_empty : s Γ—Λ’ (βˆ… : set Ξ²) = βˆ… := by { ext, exact and_false _ } @[simp] lemma empty_prod : (βˆ… : set Ξ±) Γ—Λ’ t = βˆ… := by { ext, exact false_and _ } @[simp] lemma univ_prod_univ : @univ Ξ± Γ—Λ’ @univ Ξ² = univ := by { ext, exact true_and _ } lemma univ_prod {t : set Ξ²} : (univ : set Ξ±) Γ—Λ’ t = prod.snd ⁻¹' t := by simp [prod_eq] lemma prod_univ {s : set Ξ±} : s Γ—Λ’ (univ : set Ξ²) = prod.fst ⁻¹' s := by simp [prod_eq] @[simp] lemma singleton_prod : ({a} : set Ξ±) Γ—Λ’ t = prod.mk a '' t := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } @[simp] lemma prod_singleton : s Γ—Λ’ ({b} : set Ξ²) = (Ξ» a, (a, b)) '' s := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } lemma singleton_prod_singleton : ({a} : set Ξ±) Γ—Λ’ ({b} : set Ξ²) = {(a, b)} :=by simp @[simp] lemma union_prod : (s₁ βˆͺ sβ‚‚) Γ—Λ’ t = s₁ Γ—Λ’ t βˆͺ sβ‚‚ Γ—Λ’ t := by { ext ⟨x, y⟩, simp [or_and_distrib_right] } @[simp] lemma prod_union : s Γ—Λ’ (t₁ βˆͺ tβ‚‚) = s Γ—Λ’ t₁ βˆͺ s Γ—Λ’ tβ‚‚ := by { ext ⟨x, y⟩, simp [and_or_distrib_left] } lemma inter_prod : (s₁ ∩ sβ‚‚) Γ—Λ’ t = s₁ Γ—Λ’ t ∩ sβ‚‚ Γ—Λ’ t := by { ext ⟨x, y⟩, simp only [←and_and_distrib_right, mem_inter_iff, mem_prod] } lemma prod_inter : s Γ—Λ’ (t₁ ∩ tβ‚‚) = s Γ—Λ’ t₁ ∩ s Γ—Λ’ tβ‚‚ := by { ext ⟨x, y⟩, simp only [←and_and_distrib_left, mem_inter_iff, mem_prod] } lemma prod_inter_prod : s₁ Γ—Λ’ t₁ ∩ sβ‚‚ Γ—Λ’ tβ‚‚ = (s₁ ∩ sβ‚‚) Γ—Λ’ (t₁ ∩ tβ‚‚) := by { ext ⟨x, y⟩, simp [and_assoc, and.left_comm] } lemma disjoint_prod : disjoint (s₁ Γ—Λ’ t₁) (sβ‚‚ Γ—Λ’ tβ‚‚) ↔ disjoint s₁ sβ‚‚ ∨ disjoint t₁ tβ‚‚ := begin simp_rw [disjoint_left, mem_prod, not_and_distrib, prod.forall, and_imp, ←@forall_or_distrib_right Ξ±, ←@forall_or_distrib_left Ξ², ←@forall_or_distrib_right (_ ∈ s₁), ←@forall_or_distrib_left (_ ∈ t₁)], end lemma insert_prod : insert a s Γ—Λ’ t = (prod.mk a '' t) βˆͺ s Γ—Λ’ t := by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} } lemma prod_insert : s Γ—Λ’ (insert b t) = ((Ξ»a, (a, b)) '' s) βˆͺ s Γ—Λ’ t := by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} } lemma prod_preimage_eq {f : Ξ³ β†’ Ξ±} {g : Ξ΄ β†’ Ξ²} : (f ⁻¹' s) Γ—Λ’ (g ⁻¹' t) = (Ξ» p : Ξ³ Γ— Ξ΄, (f p.1, g p.2)) ⁻¹' s Γ—Λ’ t := rfl lemma prod_preimage_left {f : Ξ³ β†’ Ξ±} : (f ⁻¹' s) Γ—Λ’ t = (Ξ» p : Ξ³ Γ— Ξ², (f p.1, p.2)) ⁻¹' s Γ—Λ’ t := rfl lemma prod_preimage_right {g : Ξ΄ β†’ Ξ²} : s Γ—Λ’ (g ⁻¹' t) = (Ξ» p : Ξ± Γ— Ξ΄, (p.1, g p.2)) ⁻¹' s Γ—Λ’ t := rfl lemma preimage_prod_map_prod (f : Ξ± β†’ Ξ²) (g : Ξ³ β†’ Ξ΄) (s : set Ξ²) (t : set Ξ΄) : prod.map f g ⁻¹' s Γ—Λ’ t = (f ⁻¹' s) Γ—Λ’ (g ⁻¹' t) := rfl lemma mk_preimage_prod (f : Ξ³ β†’ Ξ±) (g : Ξ³ β†’ Ξ²) : (Ξ» x, (f x, g x)) ⁻¹' s Γ—Λ’ t = f ⁻¹' s ∩ g ⁻¹' t := rfl @[simp] lemma mk_preimage_prod_left (hb : b ∈ t) : (Ξ» a, (a, b)) ⁻¹' s Γ—Λ’ t = s := by { ext a, simp [hb] } @[simp] lemma mk_preimage_prod_right (ha : a ∈ s) : prod.mk a ⁻¹' s Γ—Λ’ t = t := by { ext b, simp [ha] } @[simp] lemma mk_preimage_prod_left_eq_empty (hb : b βˆ‰ t) : (Ξ» a, (a, b)) ⁻¹' s Γ—Λ’ t = βˆ… := by { ext a, simp [hb] } @[simp] lemma mk_preimage_prod_right_eq_empty (ha : a βˆ‰ s) : prod.mk a ⁻¹' s Γ—Λ’ t = βˆ… := by { ext b, simp [ha] } lemma mk_preimage_prod_left_eq_if [decidable_pred (∈ t)] : (Ξ» a, (a, b)) ⁻¹' s Γ—Λ’ t = if b ∈ t then s else βˆ… := by split_ifs; simp [h] lemma mk_preimage_prod_right_eq_if [decidable_pred (∈ s)] : prod.mk a ⁻¹' s Γ—Λ’ t = if a ∈ s then t else βˆ… := by split_ifs; simp [h] lemma mk_preimage_prod_left_fn_eq_if [decidable_pred (∈ t)] (f : Ξ³ β†’ Ξ±) : (Ξ» a, (f a, b)) ⁻¹' s Γ—Λ’ t = if b ∈ t then f ⁻¹' s else βˆ… := by rw [← mk_preimage_prod_left_eq_if, prod_preimage_left, preimage_preimage] lemma mk_preimage_prod_right_fn_eq_if [decidable_pred (∈ s)] (g : Ξ΄ β†’ Ξ²) : (Ξ» b, (a, g b)) ⁻¹' s Γ—Λ’ t = if a ∈ s then g ⁻¹' t else βˆ… := by rw [← mk_preimage_prod_right_eq_if, prod_preimage_right, preimage_preimage] @[simp] lemma preimage_swap_prod (s : set Ξ±) (t : set Ξ²) : prod.swap ⁻¹' s Γ—Λ’ t = t Γ—Λ’ s := by { ext ⟨x, y⟩, simp [and_comm] } @[simp] lemma image_swap_prod (s : set Ξ±) (t : set Ξ²) : prod.swap '' s Γ—Λ’ t = t Γ—Λ’ s := by rw [image_swap_eq_preimage_swap, preimage_swap_prod] lemma prod_image_image_eq {m₁ : Ξ± β†’ Ξ³} {mβ‚‚ : Ξ² β†’ Ξ΄} : (m₁ '' s) Γ—Λ’ (mβ‚‚ '' t) = (Ξ» p : Ξ± Γ— Ξ², (m₁ p.1, mβ‚‚ p.2)) '' s Γ—Λ’ t := ext $ by simp [-exists_and_distrib_right, exists_and_distrib_right.symm, and.left_comm, and.assoc, and.comm] lemma prod_range_range_eq {m₁ : Ξ± β†’ Ξ³} {mβ‚‚ : Ξ² β†’ Ξ΄} : (range m₁) Γ—Λ’ (range mβ‚‚) = range (Ξ» p : Ξ± Γ— Ξ², (m₁ p.1, mβ‚‚ p.2)) := ext $ by simp [range] @[simp] lemma range_prod_map {m₁ : Ξ± β†’ Ξ³} {mβ‚‚ : Ξ² β†’ Ξ΄} : range (prod.map m₁ mβ‚‚) = (range m₁) Γ—Λ’ (range mβ‚‚) := prod_range_range_eq.symm lemma prod_range_univ_eq {m₁ : Ξ± β†’ Ξ³} : (range m₁) Γ—Λ’ (univ : set Ξ²) = range (Ξ» p : Ξ± Γ— Ξ², (m₁ p.1, p.2)) := ext $ by simp [range] lemma prod_univ_range_eq {mβ‚‚ : Ξ² β†’ Ξ΄} : (univ : set Ξ±) Γ—Λ’ (range mβ‚‚) = range (Ξ» p : Ξ± Γ— Ξ², (p.1, mβ‚‚ p.2)) := ext $ by simp [range] lemma range_pair_subset (f : Ξ± β†’ Ξ²) (g : Ξ± β†’ Ξ³) : range (Ξ» x, (f x, g x)) βŠ† (range f) Γ—Λ’ (range g) := have (Ξ» x, (f x, g x)) = prod.map f g ∘ (Ξ» x, (x, x)), from funext (Ξ» x, rfl), by { rw [this, ← range_prod_map], apply range_comp_subset_range } lemma nonempty.prod : s.nonempty β†’ t.nonempty β†’ (s Γ—Λ’ t).nonempty := Ξ» ⟨x, hx⟩ ⟨y, hy⟩, ⟨(x, y), ⟨hx, hy⟩⟩ lemma nonempty.fst : (s Γ—Λ’ t).nonempty β†’ s.nonempty := Ξ» ⟨x, hx⟩, ⟨x.1, hx.1⟩ lemma nonempty.snd : (s Γ—Λ’ t).nonempty β†’ t.nonempty := Ξ» ⟨x, hx⟩, ⟨x.2, hx.2⟩ lemma prod_nonempty_iff : (s Γ—Λ’ t).nonempty ↔ s.nonempty ∧ t.nonempty := ⟨λ h, ⟨h.fst, h.snd⟩, Ξ» h, h.1.prod h.2⟩ lemma prod_eq_empty_iff : s Γ—Λ’ t = βˆ… ↔ s = βˆ… ∨ t = βˆ… := by simp only [not_nonempty_iff_eq_empty.symm, prod_nonempty_iff, not_and_distrib] lemma prod_sub_preimage_iff {W : set Ξ³} {f : Ξ± Γ— Ξ² β†’ Ξ³} : s Γ—Λ’ t βŠ† f ⁻¹' W ↔ βˆ€ a b, a ∈ s β†’ b ∈ t β†’ f (a, b) ∈ W := by simp [subset_def] lemma image_prod_mk_subset_prod {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {s : set Ξ±} : (Ξ» x, (f x, g x)) '' s βŠ† (f '' s) Γ—Λ’ (g '' s) := by { rintros _ ⟨x, hx, rfl⟩, exact mk_mem_prod (mem_image_of_mem f hx) (mem_image_of_mem g hx) } lemma image_prod_mk_subset_prod_left (hb : b ∈ t) : (Ξ» a, (a, b)) '' s βŠ† s Γ—Λ’ t := by { rintro _ ⟨a, ha, rfl⟩, exact ⟨ha, hb⟩ } lemma image_prod_mk_subset_prod_right (ha : a ∈ s) : prod.mk a '' t βŠ† s Γ—Λ’ t := by { rintro _ ⟨b, hb, rfl⟩, exact ⟨ha, hb⟩ } lemma prod_subset_preimage_fst (s : set Ξ±) (t : set Ξ²) : s Γ—Λ’ t βŠ† prod.fst ⁻¹' s := inter_subset_left _ _ lemma fst_image_prod_subset (s : set Ξ±) (t : set Ξ²) : prod.fst '' s Γ—Λ’ t βŠ† s := image_subset_iff.2 $ prod_subset_preimage_fst s t lemma fst_image_prod (s : set Ξ²) {t : set Ξ±} (ht : t.nonempty) : prod.fst '' s Γ—Λ’ t = s := (fst_image_prod_subset _ _).antisymm $ Ξ» y hy, let ⟨x, hx⟩ := ht in ⟨(y, x), ⟨hy, hx⟩, rfl⟩ lemma prod_subset_preimage_snd (s : set Ξ±) (t : set Ξ²) : s Γ—Λ’ t βŠ† prod.snd ⁻¹' t := inter_subset_right _ _ lemma snd_image_prod_subset (s : set Ξ±) (t : set Ξ²) : prod.snd '' s Γ—Λ’ t βŠ† t := image_subset_iff.2 $ prod_subset_preimage_snd s t lemma snd_image_prod {s : set Ξ±} (hs : s.nonempty) (t : set Ξ²) : prod.snd '' s Γ—Λ’ t = t := (snd_image_prod_subset _ _).antisymm $ Ξ» y y_in, let ⟨x, x_in⟩ := hs in ⟨(x, y), ⟨x_in, y_in⟩, rfl⟩ lemma prod_diff_prod : s Γ—Λ’ t \ s₁ Γ—Λ’ t₁ = s Γ—Λ’ (t \ t₁) βˆͺ (s \ s₁) Γ—Λ’ t := by { ext x, by_cases h₁ : x.1 ∈ s₁; by_cases hβ‚‚ : x.2 ∈ t₁; simp * } /-- A product set is included in a product set if and only factors are included, or a factor of the first set is empty. -/ lemma prod_subset_prod_iff : s Γ—Λ’ t βŠ† s₁ Γ—Λ’ t₁ ↔ s βŠ† s₁ ∧ t βŠ† t₁ ∨ s = βˆ… ∨ t = βˆ… := begin cases (s Γ—Λ’ t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.1 h] }, have st : s.nonempty ∧ t.nonempty, by rwa [prod_nonempty_iff] at h, refine ⟨λ H, or.inl ⟨_, _⟩, _⟩, { have := image_subset (prod.fst : Ξ± Γ— Ξ² β†’ Ξ±) H, rwa [fst_image_prod _ st.2, fst_image_prod _ (h.mono H).snd] at this }, { have := image_subset (prod.snd : Ξ± Γ— Ξ² β†’ Ξ²) H, rwa [snd_image_prod st.1, snd_image_prod (h.mono H).fst] at this }, { intro H, simp only [st.1.ne_empty, st.2.ne_empty, or_false] at H, exact prod_mono H.1 H.2 } end lemma prod_eq_prod_iff_of_nonempty (h : (s Γ—Λ’ t).nonempty) : s Γ—Λ’ t = s₁ Γ—Λ’ t₁ ↔ s = s₁ ∧ t = t₁ := begin split, { intro heq, have h₁ : (s₁ Γ—Λ’ t₁ : set _).nonempty, { rwa [← heq] }, rw [prod_nonempty_iff] at h h₁, rw [← fst_image_prod s h.2, ← fst_image_prod s₁ h₁.2, heq, eq_self_iff_true, true_and, ← snd_image_prod h.1 t, ← snd_image_prod h₁.1 t₁, heq] }, { rintro ⟨rfl, rfl⟩, refl } end lemma prod_eq_prod_iff : s Γ—Λ’ t = s₁ Γ—Λ’ t₁ ↔ s = s₁ ∧ t = t₁ ∨ (s = βˆ… ∨ t = βˆ…) ∧ (s₁ = βˆ… ∨ t₁ = βˆ…) := begin symmetry, cases eq_empty_or_nonempty (s Γ—Λ’ t) with h h, { simp_rw [h, @eq_comm _ βˆ…, prod_eq_empty_iff, prod_eq_empty_iff.mp h, true_and, or_iff_right_iff_imp], rintro ⟨rfl, rfl⟩, exact prod_eq_empty_iff.mp h }, rw [prod_eq_prod_iff_of_nonempty h], rw [← ne_empty_iff_nonempty, ne.def, prod_eq_empty_iff] at h, simp_rw [h, false_and, or_false], end @[simp] lemma prod_eq_iff_eq (ht : t.nonempty) : s Γ—Λ’ t = s₁ Γ—Λ’ t ↔ s = s₁ := begin simp_rw [prod_eq_prod_iff, ht.ne_empty, eq_self_iff_true, and_true, or_iff_left_iff_imp, or_false], rintro ⟨rfl, rfl⟩, refl, end @[simp] lemma image_prod (f : Ξ± β†’ Ξ² β†’ Ξ³) : (Ξ» x : Ξ± Γ— Ξ², f x.1 x.2) '' s Γ—Λ’ t = image2 f s t := set.ext $ Ξ» a, ⟨ by { rintro ⟨_, _, rfl⟩, exact ⟨_, _, (mem_prod.mp β€Ή_β€Ί).1, (mem_prod.mp β€Ή_β€Ί).2, rfl⟩ }, by { rintro ⟨_, _, _, _, rfl⟩, exact ⟨(_, _), mem_prod.mpr βŸ¨β€Ή_β€Ί, β€Ή_β€ΊβŸ©, rfl⟩ }⟩ @[simp] lemma image2_mk_eq_prod : image2 prod.mk s t = s Γ—Λ’ t := ext $ by simp @[simp] lemma image2_curry (f : Ξ± Γ— Ξ² β†’ Ξ³) (s : set Ξ±) (t : set Ξ²) : image2 (Ξ» a b, f (a, b)) s t = (s Γ—Λ’ t).image f := by rw [←image2_mk_eq_prod, image_image2] @[simp] lemma image_uncurry_prod (f : Ξ± β†’ Ξ² β†’ Ξ³) (s : set Ξ±) (t : set Ξ²) : uncurry f '' s Γ—Λ’ t = image2 f s t := by { rw ←image2_curry, refl } section mono variables [preorder Ξ±] {f : Ξ± β†’ set Ξ²} {g : Ξ± β†’ set Ξ³} theorem _root_.monotone.set_prod (hf : monotone f) (hg : monotone g) : monotone (Ξ» x, f x Γ—Λ’ g x) := Ξ» a b h, prod_mono (hf h) (hg h) theorem _root_.antitone.set_prod (hf : antitone f) (hg : antitone g) : antitone (Ξ» x, f x Γ—Λ’ g x) := Ξ» a b h, prod_mono (hf h) (hg h) theorem _root_.monotone_on.set_prod (hf : monotone_on f s) (hg : monotone_on g s) : monotone_on (Ξ» x, f x Γ—Λ’ g x) s := Ξ» a ha b hb h, prod_mono (hf ha hb h) (hg ha hb h) theorem _root_.antitone_on.set_prod (hf : antitone_on f s) (hg : antitone_on g s) : antitone_on (Ξ» x, f x Γ—Λ’ g x) s := Ξ» a ha b hb h, prod_mono (hf ha hb h) (hg ha hb h) end mono end prod /-! ### Diagonal In this section we prove some lemmas about the diagonal set `{p | p.1 = p.2}` and the diagonal map `Ξ» x, (x, x)`. -/ section diagonal variables {Ξ± : Type*} {s t : set Ξ±} /-- `diagonal Ξ±` is the set of `Ξ± Γ— Ξ±` consisting of all pairs of the form `(a, a)`. -/ def diagonal (Ξ± : Type*) : set (Ξ± Γ— Ξ±) := {p | p.1 = p.2} lemma mem_diagonal (x : Ξ±) : (x, x) ∈ diagonal Ξ± := by simp [diagonal] @[simp] lemma mem_diagonal_iff {x : Ξ± Γ— Ξ±} : x ∈ diagonal Ξ± ↔ x.1 = x.2 := iff.rfl instance decidable_mem_diagonal [h : decidable_eq Ξ±] (x : Ξ± Γ— Ξ±) : decidable (x ∈ diagonal Ξ±) := h x.1 x.2 lemma preimage_coe_coe_diagonal (s : set Ξ±) : (prod.map coe coe) ⁻¹' (diagonal Ξ±) = diagonal s := by { ext ⟨⟨x, hx⟩, ⟨y, hy⟩⟩, simp [set.diagonal] } @[simp] lemma range_diag : range (Ξ» x, (x, x)) = diagonal Ξ± := by { ext ⟨x, y⟩, simp [diagonal, eq_comm] } @[simp] lemma prod_subset_compl_diagonal_iff_disjoint : s Γ—Λ’ t βŠ† (diagonal Ξ±)ᢜ ↔ disjoint s t := subset_compl_comm.trans $ by simp_rw [← range_diag, range_subset_iff, disjoint_left, mem_compl_iff, prod_mk_mem_set_prod_eq, not_and] @[simp] lemma diag_preimage_prod (s t : set Ξ±) : (Ξ» x, (x, x)) ⁻¹' (s Γ—Λ’ t) = s ∩ t := rfl lemma diag_preimage_prod_self (s : set Ξ±) : (Ξ» x, (x, x)) ⁻¹' (s Γ—Λ’ s) = s := inter_self s end diagonal section off_diag variables {Ξ± : Type*} {s t : set Ξ±} {x : Ξ± Γ— Ξ±} {a : Ξ±} /-- The off-diagonal of a set `s` is the set of pairs `(a, b)` with `a, b ∈ s` and `a β‰  b`. -/ def off_diag (s : set Ξ±) : set (Ξ± Γ— Ξ±) := {x | x.1 ∈ s ∧ x.2 ∈ s ∧ x.1 β‰  x.2} @[simp] lemma mem_off_diag : x ∈ s.off_diag ↔ x.1 ∈ s ∧ x.2 ∈ s ∧ x.1 β‰  x.2 := iff.rfl lemma off_diag_mono : monotone (off_diag : set Ξ± β†’ set (Ξ± Γ— Ξ±)) := Ξ» s t h x, and.imp (@h _) $ and.imp_left $ @h _ @[simp] lemma off_diag_nonempty : s.off_diag.nonempty ↔ s.nontrivial := by simp [off_diag, set.nonempty, set.nontrivial] @[simp] lemma off_diag_eq_empty : s.off_diag = βˆ… ↔ s.subsingleton := by rw [←not_nonempty_iff_eq_empty, ←not_nontrivial_iff, off_diag_nonempty.not] alias off_diag_nonempty ↔ _ nontrivial.off_diag_nonempty alias off_diag_nonempty ↔ _ subsingleton.off_diag_eq_empty variables (s t) lemma off_diag_subset_prod : s.off_diag βŠ† s Γ—Λ’ s := Ξ» x hx, ⟨hx.1, hx.2.1⟩ lemma off_diag_eq_sep_prod : s.off_diag = {x ∈ s Γ—Λ’ s | x.1 β‰  x.2} := ext $ Ξ» _, and.assoc.symm @[simp] lemma off_diag_empty : (βˆ… : set Ξ±).off_diag = βˆ… := by simp @[simp] lemma off_diag_singleton (a : Ξ±) : ({a} : set Ξ±).off_diag = βˆ… := by simp @[simp] lemma off_diag_univ : (univ : set Ξ±).off_diag = (diagonal Ξ±)ᢜ := ext $ by simp @[simp] lemma prod_sdiff_diagonal : s Γ—Λ’ s \ diagonal Ξ± = s.off_diag := ext $ Ξ» _, and.assoc @[simp] lemma disjoint_diagonal_off_diag : disjoint (diagonal Ξ±) s.off_diag := disjoint_left.mpr $ Ξ» x hd ho, ho.2.2 hd lemma off_diag_inter : (s ∩ t).off_diag = s.off_diag ∩ t.off_diag := ext $ Ξ» x, by { simp only [mem_off_diag, mem_inter_iff], tauto } variables {s t} lemma off_diag_union (h : disjoint s t) : (s βˆͺ t).off_diag = s.off_diag βˆͺ t.off_diag βˆͺ s Γ—Λ’ t βˆͺ t Γ—Λ’ s := begin rw [off_diag_eq_sep_prod, union_prod, prod_union, prod_union, union_comm _ (t Γ—Λ’ t), union_assoc, union_left_comm (s Γ—Λ’ t), ←union_assoc, sep_union, sep_union, ←off_diag_eq_sep_prod, ←off_diag_eq_sep_prod, sep_eq_self_iff_mem_true.2, ←union_assoc], simp only [mem_union, mem_prod, ne.def, prod.forall], rintro i j (⟨hi, hj⟩ | ⟨hi, hj⟩) rfl; exact h.le_bot βŸ¨β€Ή_β€Ί, β€Ή_β€ΊβŸ©, end lemma off_diag_insert (ha : a βˆ‰ s) : (insert a s).off_diag = s.off_diag βˆͺ {a} Γ—Λ’ s βˆͺ s Γ—Λ’ {a} := begin rw [insert_eq, union_comm, off_diag_union, off_diag_singleton, union_empty, union_right_comm], rw disjoint_left, rintro b hb (rfl : b = a), exact ha hb end end off_diag /-! ### Cartesian set-indexed product of sets -/ section pi variables {ΞΉ : Type*} {Ξ± Ξ² : ΞΉ β†’ Type*} {s s₁ sβ‚‚ : set ΞΉ} {t t₁ tβ‚‚ : Ξ  i, set (Ξ± i)} {i : ΞΉ} /-- Given an index set `ΞΉ` and a family of sets `t : Ξ  i, set (Ξ± i)`, `pi s t` is the set of dependent functions `f : Ξ a, Ο€ a` such that `f a` belongs to `t a` whenever `a ∈ s`. -/ def pi (s : set ΞΉ) (t : Ξ  i, set (Ξ± i)) : set (Ξ  i, Ξ± i) := {f | βˆ€ i ∈ s, f i ∈ t i} @[simp] lemma mem_pi {f : Ξ  i, Ξ± i} : f ∈ s.pi t ↔ βˆ€ i ∈ s, f i ∈ t i := iff.rfl @[simp] lemma mem_univ_pi {f : Ξ  i, Ξ± i} : f ∈ pi univ t ↔ βˆ€ i, f i ∈ t i := by simp @[simp] lemma empty_pi (s : Ξ  i, set (Ξ± i)) : pi βˆ… s = univ := by { ext, simp [pi] } @[simp] lemma pi_univ (s : set ΞΉ) : pi s (Ξ» i, (univ : set (Ξ± i))) = univ := eq_univ_of_forall $ Ξ» f i hi, mem_univ _ lemma pi_mono (h : βˆ€ i ∈ s, t₁ i βŠ† tβ‚‚ i) : pi s t₁ βŠ† pi s tβ‚‚ := Ξ» x hx i hi, (h i hi $ hx i hi) lemma pi_inter_distrib : s.pi (Ξ» i, t i ∩ t₁ i) = s.pi t ∩ s.pi t₁ := ext $ Ξ» x, by simp only [forall_and_distrib, mem_pi, mem_inter_iff] lemma pi_congr (h : s₁ = sβ‚‚) (h' : βˆ€ i ∈ s₁, t₁ i = tβ‚‚ i) : s₁.pi t₁ = sβ‚‚.pi tβ‚‚ := h β–Έ (ext $ Ξ» x, forallβ‚‚_congr $ Ξ» i hi, h' i hi β–Έ iff.rfl) lemma pi_eq_empty (hs : i ∈ s) (ht : t i = βˆ…) : s.pi t = βˆ… := by { ext f, simp only [mem_empty_iff_false, not_forall, iff_false, mem_pi, not_imp], exact ⟨i, hs, by simp [ht]⟩ } lemma univ_pi_eq_empty (ht : t i = βˆ…) : pi univ t = βˆ… := pi_eq_empty (mem_univ i) ht lemma pi_nonempty_iff : (s.pi t).nonempty ↔ βˆ€ i, βˆƒ x, i ∈ s β†’ x ∈ t i := by simp [classical.skolem, set.nonempty] lemma univ_pi_nonempty_iff : (pi univ t).nonempty ↔ βˆ€ i, (t i).nonempty := by simp [classical.skolem, set.nonempty] lemma pi_eq_empty_iff : s.pi t = βˆ… ↔ βˆƒ i, is_empty (Ξ± i) ∨ i ∈ s ∧ t i = βˆ… := begin rw [← not_nonempty_iff_eq_empty, pi_nonempty_iff], push_neg, refine exists_congr (Ξ» i, _), casesI is_empty_or_nonempty (Ξ± i); simp [*, forall_and_distrib, eq_empty_iff_forall_not_mem], end @[simp] lemma univ_pi_eq_empty_iff : pi univ t = βˆ… ↔ βˆƒ i, t i = βˆ… := by simp [← not_nonempty_iff_eq_empty, univ_pi_nonempty_iff] @[simp] lemma univ_pi_empty [h : nonempty ΞΉ] : pi univ (Ξ» i, βˆ… : Ξ  i, set (Ξ± i)) = βˆ… := univ_pi_eq_empty_iff.2 $ h.elim $ Ξ» x, ⟨x, rfl⟩ @[simp] lemma disjoint_univ_pi : disjoint (pi univ t₁) (pi univ tβ‚‚) ↔ βˆƒ i, disjoint (t₁ i) (tβ‚‚ i) := by simp only [disjoint_iff_inter_eq_empty, ← pi_inter_distrib, univ_pi_eq_empty_iff] @[simp] lemma range_dcomp (f : Ξ  i, Ξ± i β†’ Ξ² i) : range (Ξ» (g : Ξ  i, Ξ± i), (Ξ» i, f i (g i))) = pi univ (Ξ» i, range (f i)) := begin apply subset.antisymm _ (Ξ» x hx, _), { rintro _ ⟨x, rfl⟩ i -, exact ⟨x i, rfl⟩ }, { choose y hy using hx, exact ⟨λ i, y i trivial, funext $ Ξ» i, hy i trivial⟩ } end @[simp] lemma insert_pi (i : ΞΉ) (s : set ΞΉ) (t : Ξ  i, set (Ξ± i)) : pi (insert i s) t = (eval i ⁻¹' t i) ∩ pi s t := by { ext, simp [pi, or_imp_distrib, forall_and_distrib] } @[simp] lemma singleton_pi (i : ΞΉ) (t : Ξ  i, set (Ξ± i)) : pi {i} t = (eval i ⁻¹' t i) := by { ext, simp [pi] } lemma singleton_pi' (i : ΞΉ) (t : Ξ  i, set (Ξ± i)) : pi {i} t = {x | x i ∈ t i} := singleton_pi i t lemma univ_pi_singleton (f : Ξ  i, Ξ± i) : pi univ (Ξ» i, {f i}) = ({f} : set (Ξ  i, Ξ± i)) := ext $ Ξ» g, by simp [funext_iff] lemma preimage_pi (s : set ΞΉ) (t : Ξ  i, set (Ξ² i)) (f : Ξ  i, Ξ± i β†’ Ξ² i) : (Ξ» (g : Ξ  i, Ξ± i) i, f _ (g i)) ⁻¹' s.pi t = s.pi (Ξ» i, f i ⁻¹' t i) := rfl lemma pi_if {p : ΞΉ β†’ Prop} [h : decidable_pred p] (s : set ΞΉ) (t₁ tβ‚‚ : Ξ  i, set (Ξ± i)) : pi s (Ξ» i, if p i then t₁ i else tβ‚‚ i) = pi {i ∈ s | p i} t₁ ∩ pi {i ∈ s | Β¬ p i} tβ‚‚ := begin ext f, refine ⟨λ h, _, _⟩, { split; { rintro i ⟨his, hpi⟩, simpa [*] using h i } }, { rintro ⟨ht₁, htβ‚‚βŸ© i his, by_cases p i; simp * at * } end lemma union_pi : (s₁ βˆͺ sβ‚‚).pi t = s₁.pi t ∩ sβ‚‚.pi t := by simp [pi, or_imp_distrib, forall_and_distrib, set_of_and] @[simp] lemma pi_inter_compl (s : set ΞΉ) : pi s t ∩ pi sᢜ t = pi univ t := by rw [← union_pi, union_compl_self] lemma pi_update_of_not_mem [decidable_eq ΞΉ] (hi : i βˆ‰ s) (f : Ξ  j, Ξ± j) (a : Ξ± i) (t : Ξ  j, Ξ± j β†’ set (Ξ² j)) : s.pi (Ξ» j, t j (update f i a j)) = s.pi (Ξ» j, t j (f j)) := pi_congr rfl $ Ξ» j hj, by { rw update_noteq, exact Ξ» h, hi (h β–Έ hj) } lemma pi_update_of_mem [decidable_eq ΞΉ] (hi : i ∈ s) (f : Ξ  j, Ξ± j) (a : Ξ± i) (t : Ξ  j, Ξ± j β†’ set (Ξ² j)) : s.pi (Ξ» j, t j (update f i a j)) = {x | x i ∈ t i a} ∩ (s \ {i}).pi (Ξ» j, t j (f j)) := calc s.pi (Ξ» j, t j (update f i a j)) = ({i} βˆͺ s \ {i}).pi (Ξ» j, t j (update f i a j)) : by rw [union_diff_self, union_eq_self_of_subset_left (singleton_subset_iff.2 hi)] ... = {x | x i ∈ t i a} ∩ (s \ {i}).pi (Ξ» j, t j (f j)) : by { rw [union_pi, singleton_pi', update_same, pi_update_of_not_mem], simp } lemma univ_pi_update [decidable_eq ΞΉ] {Ξ² : Ξ  i, Type*} (i : ΞΉ) (f : Ξ  j, Ξ± j) (a : Ξ± i) (t : Ξ  j, Ξ± j β†’ set (Ξ² j)) : pi univ (Ξ» j, t j (update f i a j)) = {x | x i ∈ t i a} ∩ pi {i}ᢜ (Ξ» j, t j (f j)) := by rw [compl_eq_univ_diff, ← pi_update_of_mem (mem_univ _)] lemma univ_pi_update_univ [decidable_eq ΞΉ] (i : ΞΉ) (s : set (Ξ± i)) : pi univ (update (Ξ» j : ΞΉ, (univ : set (Ξ± j))) i s) = eval i ⁻¹' s := by rw [univ_pi_update i (Ξ» j, (univ : set (Ξ± j))) s (Ξ» j t, t), pi_univ, inter_univ, preimage] lemma eval_image_pi_subset (hs : i ∈ s) : eval i '' s.pi t βŠ† t i := image_subset_iff.2 $ Ξ» f hf, hf i hs lemma eval_image_univ_pi_subset : eval i '' pi univ t βŠ† t i := eval_image_pi_subset (mem_univ i) lemma subset_eval_image_pi (ht : (s.pi t).nonempty) (i : ΞΉ) : t i βŠ† eval i '' s.pi t := begin classical, obtain ⟨f, hf⟩ := ht, refine Ξ» y hy, ⟨update f i y, Ξ» j hj, _, update_same _ _ _⟩, obtain rfl | hji := eq_or_ne j i; simp [*, hf _ hj] end lemma eval_image_pi (hs : i ∈ s) (ht : (s.pi t).nonempty) : eval i '' s.pi t = t i := (eval_image_pi_subset hs).antisymm (subset_eval_image_pi ht i) @[simp] lemma eval_image_univ_pi (ht : (pi univ t).nonempty) : (Ξ» f : Ξ  i, Ξ± i, f i) '' pi univ t = t i := eval_image_pi (mem_univ i) ht lemma pi_subset_pi_iff : pi s t₁ βŠ† pi s tβ‚‚ ↔ (βˆ€ i ∈ s, t₁ i βŠ† tβ‚‚ i) ∨ pi s t₁ = βˆ… := begin refine ⟨λ h, or_iff_not_imp_right.2 _, Ξ» h, h.elim pi_mono (Ξ» h', h'.symm β–Έ empty_subset _)⟩, rw [← ne.def, ne_empty_iff_nonempty], intros hne i hi, simpa only [eval_image_pi hi hne, eval_image_pi hi (hne.mono h)] using image_subset (Ξ» f : Ξ  i, Ξ± i, f i) h end lemma univ_pi_subset_univ_pi_iff : pi univ t₁ βŠ† pi univ tβ‚‚ ↔ (βˆ€ i, t₁ i βŠ† tβ‚‚ i) ∨ βˆƒ i, t₁ i = βˆ… := by simp [pi_subset_pi_iff] lemma eval_preimage [decidable_eq ΞΉ] {s : set (Ξ± i)} : eval i ⁻¹' s = pi univ (update (Ξ» i, univ) i s) := by { ext x, simp [@forall_update_iff _ (Ξ» i, set (Ξ± i)) _ _ _ _ (Ξ» i' y, x i' ∈ y)] } lemma eval_preimage' [decidable_eq ΞΉ] {s : set (Ξ± i)} : eval i ⁻¹' s = pi {i} (update (Ξ» i, univ) i s) := by { ext, simp } lemma update_preimage_pi [decidable_eq ΞΉ] {f : Ξ  i, Ξ± i} (hi : i ∈ s) (hf : βˆ€ j ∈ s, j β‰  i β†’ f j ∈ t j) : (update f i) ⁻¹' s.pi t = t i := begin ext x, refine ⟨λ h, _, Ξ» hx j hj, _⟩, { convert h i hi, simp }, { obtain rfl | h := eq_or_ne j i, { simpa }, { rw update_noteq h, exact hf j hj h } } end lemma update_preimage_univ_pi [decidable_eq ΞΉ] {f : Ξ  i, Ξ± i} (hf : βˆ€ j β‰  i, f j ∈ t j) : (update f i) ⁻¹' pi univ t = t i := update_preimage_pi (mem_univ i) (Ξ» j _, hf j) lemma subset_pi_eval_image (s : set ΞΉ) (u : set (Ξ  i, Ξ± i)) : u βŠ† pi s (Ξ» i, eval i '' u) := Ξ» f hf i hi, ⟨f, hf, rfl⟩ lemma univ_pi_ite (s : set ΞΉ) [decidable_pred (∈ s)] (t : Ξ  i, set (Ξ± i)) : pi univ (Ξ» i, if i ∈ s then t i else univ) = s.pi t := by { ext, simp_rw [mem_univ_pi], refine forall_congr (Ξ» i, _), split_ifs; simp [h] } end pi end set
ec83899c081e20e1c1bb5a405abc888222ce7db0
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/group/with_one.lean
947969d1a122b0089185e42f9beda47ac0be2863
[ "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
12,725
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johan Commelin -/ import algebra.hom.equiv import algebra.ring.basic import logic.equiv.basic import logic.equiv.option /-! # Adjoining a zero/one to semigroups and related algebraic structures This file contains different results about adjoining an element to an algebraic structure which then behaves like a zero or a one. An example is adjoining a one to a semigroup to obtain a monoid. That this provides an example of an adjunction is proved in `algebra.category.Mon.adjunctions`. Another result says that adjoining to a group an element `zero` gives a `group_with_zero`. For more information about these structures (which are not that standard in informal mathematics, see `algebra.group_with_zero.basic`) -/ universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} /-- Add an extra element `1` to a type -/ @[to_additive "Add an extra element `0` to a type"] def with_one (Ξ±) := option Ξ± namespace with_one instance [has_repr Ξ±] : has_repr (with_zero Ξ±) := ⟨λ o, match o with | none := "0" | (some a) := "↑" ++ repr a end⟩ @[to_additive] instance [has_repr Ξ±] : has_repr (with_one Ξ±) := ⟨λ o, match o with | none := "1" | (some a) := "↑" ++ repr a end⟩ @[to_additive] instance : monad with_one := option.monad @[to_additive] instance : has_one (with_one Ξ±) := ⟨none⟩ @[to_additive] instance [has_mul Ξ±] : has_mul (with_one Ξ±) := ⟨option.lift_or_get (*)⟩ @[to_additive] instance [has_inv Ξ±] : has_inv (with_one Ξ±) := ⟨λ a, option.map has_inv.inv a⟩ @[to_additive] instance : inhabited (with_one Ξ±) := ⟨1⟩ @[to_additive] instance [nonempty Ξ±] : nontrivial (with_one Ξ±) := option.nontrivial @[to_additive] instance : has_coe_t Ξ± (with_one Ξ±) := ⟨some⟩ @[to_additive] lemma some_eq_coe {a : Ξ±} : (some a : with_one Ξ±) = ↑a := rfl @[simp, to_additive] lemma coe_ne_one {a : Ξ±} : (a : with_one Ξ±) β‰  (1 : with_one Ξ±) := option.some_ne_none a @[simp, to_additive] lemma one_ne_coe {a : Ξ±} : (1 : with_one Ξ±) β‰  a := coe_ne_one.symm @[to_additive] lemma ne_one_iff_exists {x : with_one Ξ±} : x β‰  1 ↔ βˆƒ (a : Ξ±), ↑a = x := option.ne_none_iff_exists @[to_additive] instance : can_lift (with_one Ξ±) Ξ± := { coe := coe, cond := Ξ» a, a β‰  1, prf := Ξ» a, ne_one_iff_exists.1 } @[simp, norm_cast, to_additive] lemma coe_inj {a b : Ξ±} : (a : with_one Ξ±) = b ↔ a = b := option.some_inj @[elab_as_eliminator, to_additive] protected lemma cases_on {P : with_one Ξ± β†’ Prop} : βˆ€ (x : with_one Ξ±), P 1 β†’ (βˆ€ a : Ξ±, P a) β†’ P x := option.cases_on -- the `show` statements in the proofs are important, because otherwise the generated lemmas -- `with_one.mul_one_class._proof_{1,2}` have an ill-typed statement after `with_one` is made -- irreducible. @[to_additive] instance [has_mul Ξ±] : mul_one_class (with_one Ξ±) := { mul := (*), one := (1), one_mul := show βˆ€ x : with_one Ξ±, 1 * x = x, from (option.lift_or_get_is_left_id _).1, mul_one := show βˆ€ x : with_one Ξ±, x * 1 = x, from (option.lift_or_get_is_right_id _).1 } @[to_additive] instance [semigroup Ξ±] : monoid (with_one Ξ±) := { mul_assoc := (option.lift_or_get_assoc _).1, ..with_one.mul_one_class } example [semigroup Ξ±] : @monoid.to_mul_one_class _ (@with_one.monoid Ξ± _) = @with_one.mul_one_class Ξ± _ := rfl @[to_additive] instance [comm_semigroup Ξ±] : comm_monoid (with_one Ξ±) := { mul_comm := (option.lift_or_get_comm _).1, ..with_one.monoid } section -- workaround: we make `with_one`/`with_zero` irreducible for this definition, otherwise `simps` -- will unfold it in the statement of the lemma it generates. local attribute [irreducible] with_one with_zero /-- `coe` as a bundled morphism -/ @[to_additive "`coe` as a bundled morphism", simps apply] def coe_mul_hom [has_mul Ξ±] : Ξ± β†’β‚™* (with_one Ξ±) := { to_fun := coe, map_mul' := Ξ» x y, rfl } end section lift variables [has_mul Ξ±] [mul_one_class Ξ²] /-- Lift a semigroup homomorphism `f` to a bundled monoid homorphism. -/ @[to_additive "Lift an add_semigroup homomorphism `f` to a bundled add_monoid homorphism."] def lift : (Ξ± β†’β‚™* Ξ²) ≃ (with_one Ξ± β†’* Ξ²) := { to_fun := Ξ» f, { to_fun := Ξ» x, option.cases_on x 1 f, map_one' := rfl, map_mul' := Ξ» x y, with_one.cases_on x (by { rw one_mul, exact (one_mul _).symm }) $ Ξ» x, with_one.cases_on y (by { rw mul_one, exact (mul_one _).symm }) $ Ξ» y, f.map_mul x y }, inv_fun := Ξ» F, F.to_mul_hom.comp coe_mul_hom, left_inv := Ξ» f, mul_hom.ext $ Ξ» x, rfl, right_inv := Ξ» F, monoid_hom.ext $ Ξ» x, with_one.cases_on x F.map_one.symm $ Ξ» x, rfl } variables (f : Ξ± β†’β‚™* Ξ²) @[simp, to_additive] lemma lift_coe (x : Ξ±) : lift f x = f x := rfl @[simp, to_additive] lemma lift_one : lift f 1 = 1 := rfl @[to_additive] theorem lift_unique (f : with_one Ξ± β†’* Ξ²) : f = lift (f.to_mul_hom.comp coe_mul_hom) := (lift.apply_symm_apply f).symm end lift attribute [irreducible] with_one section map variables [has_mul Ξ±] [has_mul Ξ²] [has_mul Ξ³] /-- Given a multiplicative map from `Ξ± β†’ Ξ²` returns a monoid homomorphism from `with_one Ξ±` to `with_one Ξ²` -/ @[to_additive "Given an additive map from `Ξ± β†’ Ξ²` returns an add_monoid homomorphism from `with_zero Ξ±` to `with_zero Ξ²`"] def map (f : Ξ± β†’β‚™* Ξ²) : with_one Ξ± β†’* with_one Ξ² := lift (coe_mul_hom.comp f) @[simp, to_additive] lemma map_coe (f : Ξ± β†’β‚™* Ξ²) (a : Ξ±) : map f (a : with_one Ξ±) = f a := lift_coe _ _ @[simp, to_additive] lemma map_id : map (mul_hom.id Ξ±) = monoid_hom.id (with_one Ξ±) := by { ext, induction x using with_one.cases_on; refl } @[to_additive] lemma map_map (f : Ξ± β†’β‚™* Ξ²) (g : Ξ² β†’β‚™* Ξ³) (x) : map g (map f x) = map (g.comp f) x := by { induction x using with_one.cases_on; refl } @[simp, to_additive] lemma map_comp (f : Ξ± β†’β‚™* Ξ²) (g : Ξ² β†’β‚™* Ξ³) : map (g.comp f) = (map g).comp (map f) := monoid_hom.ext $ Ξ» x, (map_map f g x).symm /-- A version of `equiv.option_congr` for `with_one`. -/ @[to_additive "A version of `equiv.option_congr` for `with_zero`.", simps apply] def _root_.mul_equiv.with_one_congr (e : Ξ± ≃* Ξ²) : with_one Ξ± ≃* with_one Ξ² := { to_fun := map e.to_mul_hom, inv_fun := map e.symm.to_mul_hom, left_inv := Ξ» x, (map_map _ _ _).trans $ by induction x using with_one.cases_on; { simp }, right_inv := Ξ» x, (map_map _ _ _).trans $ by induction x using with_one.cases_on; { simp }, .. map e.to_mul_hom } @[simp] lemma _root_.mul_equiv.with_one_congr_refl : (mul_equiv.refl Ξ±).with_one_congr = mul_equiv.refl _ := mul_equiv.to_monoid_hom_injective map_id @[simp] lemma _root_.mul_equiv.with_one_congr_symm (e : Ξ± ≃* Ξ²) : e.with_one_congr.symm = e.symm.with_one_congr := rfl @[simp] lemma _root_.mul_equiv.with_one_congr_trans (e₁ : Ξ± ≃* Ξ²) (eβ‚‚ : Ξ² ≃* Ξ³) : e₁.with_one_congr.trans eβ‚‚.with_one_congr = (e₁.trans eβ‚‚).with_one_congr := mul_equiv.to_monoid_hom_injective (map_comp _ _).symm end map @[simp, norm_cast, to_additive] lemma coe_mul [has_mul Ξ±] (a b : Ξ±) : ((a * b : Ξ±) : with_one Ξ±) = a * b := rfl @[simp, norm_cast, to_additive] lemma coe_inv [has_inv Ξ±] (a : Ξ±) : ((a⁻¹ : Ξ±) : with_one Ξ±) = a⁻¹ := rfl end with_one namespace with_zero instance [one : has_one Ξ±] : has_one (with_zero Ξ±) := { ..one } @[simp, norm_cast] lemma coe_one [has_one Ξ±] : ((1 : Ξ±) : with_zero Ξ±) = 1 := rfl instance [has_mul Ξ±] : mul_zero_class (with_zero Ξ±) := { mul := Ξ» o₁ oβ‚‚, o₁.bind (Ξ» a, option.map (Ξ» b, a * b) oβ‚‚), zero_mul := Ξ» a, rfl, mul_zero := Ξ» a, by cases a; refl, ..with_zero.has_zero } @[simp, norm_cast] lemma coe_mul {Ξ± : Type u} [has_mul Ξ±] {a b : Ξ±} : ((a * b : Ξ±) : with_zero Ξ±) = a * b := rfl @[simp] lemma zero_mul {Ξ± : Type u} [has_mul Ξ±] (a : with_zero Ξ±) : 0 * a = 0 := rfl @[simp] lemma mul_zero {Ξ± : Type u} [has_mul Ξ±] (a : with_zero Ξ±) : a * 0 = 0 := by cases a; refl instance [semigroup Ξ±] : semigroup_with_zero (with_zero Ξ±) := { mul_assoc := Ξ» a b c, match a, b, c with | none, _, _ := rfl | some a, none, _ := rfl | some a, some b, none := rfl | some a, some b, some c := congr_arg some (mul_assoc _ _ _) end, ..with_zero.mul_zero_class } instance [comm_semigroup Ξ±] : comm_semigroup (with_zero Ξ±) := { mul_comm := Ξ» a b, match a, b with | none, _ := (mul_zero _).symm | some a, none := rfl | some a, some b := congr_arg some (mul_comm _ _) end, ..with_zero.semigroup_with_zero } instance [mul_one_class Ξ±] : mul_zero_one_class (with_zero Ξ±) := { one_mul := Ξ» a, match a with | none := rfl | some a := congr_arg some $ one_mul _ end, mul_one := Ξ» a, match a with | none := rfl | some a := congr_arg some $ mul_one _ end, ..with_zero.mul_zero_class, ..with_zero.has_one } instance [has_one Ξ±] [has_pow Ξ± β„•] : has_pow (with_zero Ξ±) β„• := ⟨λ x n, match x, n with | none, 0 := 1 | none, n + 1 := 0 | some x, n := ↑(x ^ n) end⟩ @[simp, norm_cast] lemma coe_pow [has_one Ξ±] [has_pow Ξ± β„•] {a : Ξ±} (n : β„•) : ↑(a ^ n : Ξ±) = (↑a ^ n : with_zero Ξ±) := rfl instance [monoid Ξ±] : monoid_with_zero (with_zero Ξ±) := { npow := Ξ» n x, x ^ n, npow_zero' := Ξ» x, match x with | none := rfl | some x := congr_arg some $ pow_zero _ end, npow_succ' := Ξ» n x, match x with | none := rfl | some x := congr_arg some $ pow_succ _ _ end, .. with_zero.mul_zero_one_class, .. with_zero.semigroup_with_zero } instance [comm_monoid Ξ±] : comm_monoid_with_zero (with_zero Ξ±) := { ..with_zero.monoid_with_zero, ..with_zero.comm_semigroup } /-- Given an inverse operation on `Ξ±` there is an inverse operation on `with_zero Ξ±` sending `0` to `0`-/ instance [has_inv Ξ±] : has_inv (with_zero Ξ±) := ⟨λ a, option.map has_inv.inv a⟩ @[simp, norm_cast] lemma coe_inv [has_inv Ξ±] (a : Ξ±) : ((a⁻¹ : Ξ±) : with_zero Ξ±) = a⁻¹ := rfl @[simp] lemma inv_zero [has_inv Ξ±] : (0 : with_zero Ξ±)⁻¹ = 0 := rfl instance [has_div Ξ±] : has_div (with_zero Ξ±) := ⟨λ o₁ oβ‚‚, o₁.bind (Ξ» a, option.map (Ξ» b, a / b) oβ‚‚)⟩ @[norm_cast] lemma coe_div [has_div Ξ±] (a b : Ξ±) : ↑(a / b : Ξ±) = (a / b : with_zero Ξ±) := rfl instance [has_one Ξ±] [has_pow Ξ± β„€] : has_pow (with_zero Ξ±) β„€ := ⟨λ x n, match x, n with | none, int.of_nat 0 := 1 | none, int.of_nat (nat.succ n) := 0 | none, int.neg_succ_of_nat n := 0 | some x, n := ↑(x ^ n) end⟩ @[simp, norm_cast] lemma coe_zpow [div_inv_monoid Ξ±] {a : Ξ±} (n : β„€) : ↑(a ^ n : Ξ±) = (↑a ^ n : with_zero Ξ±) := rfl instance [div_inv_monoid Ξ±] : div_inv_monoid (with_zero Ξ±) := { div_eq_mul_inv := Ξ» a b, match a, b with | none, _ := rfl | some a, none := rfl | some a, some b := congr_arg some (div_eq_mul_inv _ _) end, zpow := Ξ» n x, x ^ n, zpow_zero' := Ξ» x, match x with | none := rfl | some x := congr_arg some $ zpow_zero _ end, zpow_succ' := Ξ» n x, match x with | none := rfl | some x := congr_arg some $ div_inv_monoid.zpow_succ' _ _ end, zpow_neg' := Ξ» n x, match x with | none := rfl | some x := congr_arg some $ div_inv_monoid.zpow_neg' _ _ end, .. with_zero.has_div, .. with_zero.has_inv, .. with_zero.monoid_with_zero, } section group variables [group Ξ±] @[simp] lemma inv_one : (1 : with_zero Ξ±)⁻¹ = 1 := show ((1⁻¹ : Ξ±) : with_zero Ξ±) = 1, by simp /-- if `G` is a group then `with_zero G` is a group with zero. -/ instance : group_with_zero (with_zero Ξ±) := { inv_zero := inv_zero, mul_inv_cancel := Ξ» a ha, by { lift a to Ξ± using ha, norm_cast, apply mul_right_inv }, .. with_zero.monoid_with_zero, .. with_zero.div_inv_monoid, .. with_zero.nontrivial } end group instance [comm_group Ξ±] : comm_group_with_zero (with_zero Ξ±) := { .. with_zero.group_with_zero, .. with_zero.comm_monoid_with_zero } instance [semiring Ξ±] : semiring (with_zero Ξ±) := { left_distrib := Ξ» a b c, begin cases a with a, {refl}, cases b with b; cases c with c; try {refl}, exact congr_arg some (left_distrib _ _ _) end, right_distrib := Ξ» a b c, begin cases c with c, { change (a + b) * 0 = a * 0 + b * 0, simp }, cases a with a; cases b with b; try {refl}, exact congr_arg some (right_distrib _ _ _) end, ..with_zero.add_comm_monoid, ..with_zero.mul_zero_class, ..with_zero.monoid_with_zero } attribute [irreducible] with_zero end with_zero
483ad605b026e9c5d2b88f50f0e31d422c378faa
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/algebra/order_auto.lean
371d24082a9b887dbe796badc4269635e3a17829
[]
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
12,060
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.logic import Mathlib.Lean3Lib.init.classical import Mathlib.Lean3Lib.init.meta.name import Mathlib.Lean3Lib.init.algebra.classes universes u l u_1 namespace Mathlib /- Make sure instances defined in this file have lower priority than the ones defined for concrete structures -/ /-! ### Definition of `preorder` and lemmas about types with a `preorder` -/ /-- A preorder is a reflexive, transitive relation `≀` with `a < b` defined in the obvious way. -/ class preorder (Ξ± : Type u) extends HasLess Ξ±, HasLessEq Ξ± where le : Ξ± β†’ Ξ± β†’ Prop lt : Ξ± β†’ Ξ± β†’ Prop le_refl : βˆ€ (a : Ξ±), a ≀ a le_trans : βˆ€ (a b c : Ξ±), a ≀ b β†’ b ≀ c β†’ a ≀ c lt_iff_le_not_le : autoParam (βˆ€ (a b : Ξ±), a < b ↔ a ≀ b ∧ Β¬b ≀ a) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.order_laws_tac") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "order_laws_tac") []) /-- The relation `≀` on a preorder is reflexive. -/ theorem le_refl {Ξ± : Type u} [preorder Ξ±] (a : Ξ±) : a ≀ a := preorder.le_refl /-- The relation `≀` on a preorder is transitive. -/ theorem le_trans {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} {c : Ξ±} : a ≀ b β†’ b ≀ c β†’ a ≀ c := preorder.le_trans theorem lt_iff_le_not_le {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} : a < b ↔ a ≀ b ∧ Β¬b ≀ a := preorder.lt_iff_le_not_le theorem lt_of_le_not_le {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} : a ≀ b β†’ Β¬b ≀ a β†’ a < b := fun (αΎ° : a ≀ b) (αΎ°_1 : Β¬b ≀ a) => idRhs (a < b) (iff.mpr lt_iff_le_not_le { left := αΎ°, right := αΎ°_1 }) theorem le_not_le_of_lt {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} : a < b β†’ a ≀ b ∧ Β¬b ≀ a := fun (αΎ° : a < b) => idRhs (a ≀ b ∧ Β¬b ≀ a) (iff.mp lt_iff_le_not_le αΎ°) theorem le_of_eq {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} : a = b β†’ a ≀ b := fun (h : a = b) => h β–Έ le_refl a theorem ge_trans {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} {c : Ξ±} : a β‰₯ b β†’ b β‰₯ c β†’ a β‰₯ c := fun (h₁ : a β‰₯ b) (hβ‚‚ : b β‰₯ c) => le_trans hβ‚‚ h₁ theorem lt_irrefl {Ξ± : Type u} [preorder Ξ±] (a : Ξ±) : Β¬a < a := sorry theorem gt_irrefl {Ξ± : Type u} [preorder Ξ±] (a : Ξ±) : Β¬a > a := lt_irrefl theorem lt_trans {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} {c : Ξ±} : a < b β†’ b < c β†’ a < c := sorry theorem gt_trans {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} {c : Ξ±} : a > b β†’ b > c β†’ a > c := fun (h₁ : a > b) (hβ‚‚ : b > c) => lt_trans hβ‚‚ h₁ theorem ne_of_lt {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} (h : a < b) : a β‰  b := fun (he : a = b) => absurd h (he β–Έ lt_irrefl a) theorem ne_of_gt {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} (h : a > b) : a β‰  b := fun (he : a = b) => absurd h (he β–Έ lt_irrefl a) theorem lt_asymm {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} (h : a < b) : Β¬b < a := fun (h1 : b < a) => lt_irrefl a (lt_trans h h1) theorem le_of_lt {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} : a < b β†’ a ≀ b := fun (αΎ° : a < b) => idRhs (a ≀ b) (and.left (le_not_le_of_lt αΎ°)) theorem lt_of_lt_of_le {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} {c : Ξ±} : a < b β†’ b ≀ c β†’ a < c := sorry theorem lt_of_le_of_lt {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} {c : Ξ±} : a ≀ b β†’ b < c β†’ a < c := sorry theorem gt_of_gt_of_ge {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} {c : Ξ±} (h₁ : a > b) (hβ‚‚ : b β‰₯ c) : a > c := lt_of_le_of_lt hβ‚‚ h₁ theorem gt_of_ge_of_gt {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} {c : Ξ±} (h₁ : a β‰₯ b) (hβ‚‚ : b > c) : a > c := lt_of_lt_of_le hβ‚‚ h₁ theorem not_le_of_gt {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} (h : a > b) : Β¬a ≀ b := and.right (le_not_le_of_lt h) theorem not_lt_of_ge {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} (h : a β‰₯ b) : Β¬a < b := fun (hab : a < b) => not_le_of_gt hab h theorem le_of_lt_or_eq {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} : a < b ∨ a = b β†’ a ≀ b := fun (αΎ° : a < b ∨ a = b) => or.dcases_on αΎ° (fun (αΎ° : a < b) => idRhs (a ≀ b) (le_of_lt αΎ°)) fun (αΎ° : a = b) => idRhs ((fun (_x : Ξ±) => a ≀ _x) b) (αΎ° β–Έ le_refl a) theorem le_of_eq_or_lt {Ξ± : Type u} [preorder Ξ±] {a : Ξ±} {b : Ξ±} (h : a = b ∨ a < b) : a ≀ b := or.elim h le_of_eq le_of_lt protected instance decidable_lt_of_decidable_le {Ξ± : Type u} [preorder Ξ±] [DecidableRel LessEq] : DecidableRel Less := sorry /-! ### Definition of `partial_order` and lemmas about types with a partial order -/ /-- A partial order is a reflexive, transitive, antisymmetric relation `≀`. -/ class partial_order (Ξ± : Type u) extends preorder Ξ± where le_antisymm : βˆ€ (a b : Ξ±), a ≀ b β†’ b ≀ a β†’ a = b theorem le_antisymm {Ξ± : Type u} [partial_order Ξ±] {a : Ξ±} {b : Ξ±} : a ≀ b β†’ b ≀ a β†’ a = b := partial_order.le_antisymm theorem le_antisymm_iff {Ξ± : Type u} [partial_order Ξ±] {a : Ξ±} {b : Ξ±} : a = b ↔ a ≀ b ∧ b ≀ a := sorry theorem lt_or_eq_of_le {Ξ± : Type u} [partial_order Ξ±] {a : Ξ±} {b : Ξ±} : a ≀ b β†’ a < b ∨ a = b := sorry theorem le_iff_lt_or_eq {Ξ± : Type u} [partial_order Ξ±] {a : Ξ±} {b : Ξ±} : a ≀ b ↔ a < b ∨ a = b := idRhs (a ≀ b ↔ a < b ∨ a = b) { mp := lt_or_eq_of_le, mpr := le_of_lt_or_eq } theorem lt_of_le_of_ne {Ξ± : Type u} [partial_order Ξ±] {a : Ξ±} {b : Ξ±} : a ≀ b β†’ a β‰  b β†’ a < b := fun (h₁ : a ≀ b) (hβ‚‚ : a β‰  b) => lt_of_le_not_le h₁ (mt (le_antisymm h₁) hβ‚‚) protected instance decidable_eq_of_decidable_le {Ξ± : Type u} [partial_order Ξ±] [DecidableRel LessEq] : DecidableEq Ξ± := sorry /-! ### Definition of `linear_order` and lemmas about types with a linear order -/ /-- A linear order is reflexive, transitive, antisymmetric and total relation `≀`. We assume that every linear ordered type has decidable `(≀)`, `(<)`, and `(=)`. -/ class linear_order (Ξ± : Type u) extends partial_order Ξ± where le_total : βˆ€ (a b : Ξ±), a ≀ b ∨ b ≀ a decidable_le : DecidableRel LessEq decidable_eq : DecidableEq Ξ± decidable_lt : DecidableRel Less theorem le_total {Ξ± : Type u} [linear_order Ξ±] (a : Ξ±) (b : Ξ±) : a ≀ b ∨ b ≀ a := linear_order.le_total theorem le_of_not_ge {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} : Β¬a β‰₯ b β†’ a ≀ b := or.resolve_left (le_total b a) theorem le_of_not_le {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} : Β¬a ≀ b β†’ b ≀ a := or.resolve_left (le_total a b) theorem not_lt_of_gt {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} (h : a > b) : Β¬a < b := lt_asymm h theorem lt_trichotomy {Ξ± : Type u} [linear_order Ξ±] (a : Ξ±) (b : Ξ±) : a < b ∨ a = b ∨ b < a := sorry theorem le_of_not_gt {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} (h : Β¬a > b) : a ≀ b := sorry theorem lt_of_not_ge {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} (h : Β¬a β‰₯ b) : a < b := lt_of_le_not_le (or.resolve_right (le_total a b) h) h theorem lt_or_ge {Ξ± : Type u} [linear_order Ξ±] (a : Ξ±) (b : Ξ±) : a < b ∨ a β‰₯ b := sorry theorem le_or_gt {Ξ± : Type u} [linear_order Ξ±] (a : Ξ±) (b : Ξ±) : a ≀ b ∨ a > b := or.swap (lt_or_ge b a) theorem lt_or_gt_of_ne {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} (h : a β‰  b) : a < b ∨ a > b := sorry theorem ne_iff_lt_or_gt {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} : a β‰  b ↔ a < b ∨ a > b := { mp := lt_or_gt_of_ne, mpr := fun (o : a < b ∨ a > b) => or.elim o ne_of_lt ne_of_gt } theorem lt_iff_not_ge {Ξ± : Type u} [linear_order Ξ±] (x : Ξ±) (y : Ξ±) : x < y ↔ Β¬x β‰₯ y := { mp := not_le_of_gt, mpr := lt_of_not_ge } @[simp] theorem not_lt {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} : Β¬a < b ↔ b ≀ a := { mp := le_of_not_gt, mpr := not_lt_of_ge } @[simp] theorem not_le {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} : Β¬a ≀ b ↔ b < a := iff.symm (lt_iff_not_ge b a) protected instance has_lt.lt.decidable {Ξ± : Type u} [linear_order Ξ±] (a : Ξ±) (b : Ξ±) : Decidable (a < b) := linear_order.decidable_lt a b protected instance has_le.le.decidable {Ξ± : Type u} [linear_order Ξ±] (a : Ξ±) (b : Ξ±) : Decidable (a ≀ b) := linear_order.decidable_le a b protected instance eq.decidable {Ξ± : Type u} [linear_order Ξ±] (a : Ξ±) (b : Ξ±) : Decidable (a = b) := linear_order.decidable_eq a b theorem eq_or_lt_of_not_lt {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} (h : Β¬a < b) : a = b ∨ b < a := dite (a = b) (fun (h₁ : a = b) => Or.inl h₁) fun (h₁ : Β¬a = b) => Or.inr (lt_of_not_ge fun (hge : b β‰₯ a) => h (lt_of_le_of_ne hge h₁)) protected instance has_le.le.is_total_preorder {Ξ± : Type u} [linear_order Ξ±] : is_total_preorder Ξ± LessEq := is_total_preorder.mk /- TODO(Leo): decide whether we should keep this instance or not -/ protected instance is_strict_weak_order_of_linear_order {Ξ± : Type u} [linear_order Ξ±] : is_strict_weak_order Ξ± Less := is_strict_weak_order_of_is_total_preorder lt_iff_not_ge /- TODO(Leo): decide whether we should keep this instance or not -/ protected instance is_strict_total_order_of_linear_order {Ξ± : Type u} [linear_order Ξ±] : is_strict_total_order Ξ± Less := is_strict_total_order.mk namespace decidable theorem lt_or_eq_of_le {Ξ± : Type u} [partial_order Ξ±] [DecidableRel LessEq] {a : Ξ±} {b : Ξ±} (hab : a ≀ b) : a < b ∨ a = b := dite (b ≀ a) (fun (hba : b ≀ a) => Or.inr (le_antisymm hab hba)) fun (hba : Β¬b ≀ a) => Or.inl (lt_of_le_not_le hab hba) theorem eq_or_lt_of_le {Ξ± : Type u} [partial_order Ξ±] [DecidableRel LessEq] {a : Ξ±} {b : Ξ±} (hab : a ≀ b) : a = b ∨ a < b := or.swap (lt_or_eq_of_le hab) theorem le_iff_lt_or_eq {Ξ± : Type u} [partial_order Ξ±] [DecidableRel LessEq] {a : Ξ±} {b : Ξ±} : a ≀ b ↔ a < b ∨ a = b := { mp := lt_or_eq_of_le, mpr := le_of_lt_or_eq } theorem le_of_not_lt {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} (h : Β¬b < a) : a ≀ b := by_contradiction fun (h' : Β¬a ≀ b) => h (lt_of_le_not_le (or.resolve_right (le_total b a) h') h') theorem not_lt {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} : Β¬a < b ↔ b ≀ a := { mp := le_of_not_lt, mpr := not_lt_of_ge } theorem lt_or_le {Ξ± : Type u} [linear_order Ξ±] (a : Ξ±) (b : Ξ±) : a < b ∨ b ≀ a := dite (b ≀ a) (fun (hba : b ≀ a) => Or.inr hba) fun (hba : Β¬b ≀ a) => Or.inl (lt_of_not_ge hba) theorem le_or_lt {Ξ± : Type u} [linear_order Ξ±] (a : Ξ±) (b : Ξ±) : a ≀ b ∨ b < a := or.swap (lt_or_le b a) theorem lt_trichotomy {Ξ± : Type u} [linear_order Ξ±] (a : Ξ±) (b : Ξ±) : a < b ∨ a = b ∨ b < a := or.imp_right (fun (h : b ≀ a) => or.imp_left Eq.symm (eq_or_lt_of_le h)) (lt_or_le a b) theorem lt_or_gt_of_ne {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} (h : a β‰  b) : a < b ∨ b < a := or.imp_right (fun (h' : a = b ∨ b < a) => or.resolve_left h' h) (lt_trichotomy a b) /-- Perform a case-split on the ordering of `x` and `y` in a decidable linear order. -/ def lt_by_cases {Ξ± : Type u} [linear_order Ξ±] (x : Ξ±) (y : Ξ±) {P : Sort u_1} (h₁ : x < y β†’ P) (hβ‚‚ : x = y β†’ P) (h₃ : y < x β†’ P) : P := dite (x < y) (fun (h : x < y) => h₁ h) fun (h : Β¬x < y) => dite (y < x) (fun (h' : y < x) => h₃ h') fun (h' : Β¬y < x) => hβ‚‚ sorry theorem ne_iff_lt_or_gt {Ξ± : Type u} [linear_order Ξ±] {a : Ξ±} {b : Ξ±} : a β‰  b ↔ a < b ∨ b < a := { mp := lt_or_gt_of_ne, mpr := fun (o : a < b ∨ b < a) => or.elim o ne_of_lt ne_of_gt } theorem le_imp_le_of_lt_imp_lt {Ξ± : Type u} {Ξ² : Type u_1} [preorder Ξ±] [linear_order Ξ²] {a : Ξ±} {b : Ξ±} {c : Ξ²} {d : Ξ²} (H : d < c β†’ b < a) (h : a ≀ b) : c ≀ d := le_of_not_lt fun (h' : d < c) => not_le_of_gt (H h') h end Mathlib
ab794ab27e9b96a58f93fb555da3076d4446d429
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Lean/KeyedDeclsAttribute.lean
be7634ffbb1f26913f55e797afa64a7df7520b95
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
7,198
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Compiler.InitAttr import Lean.ScopedEnvExtension import Lean.Compiler.IR.CompilerM /-! A builder for attributes that are applied to declarations of a common type and group them by the given attribute argument (an arbitrary `Name`, currently). Also creates a second "builtin" attribute used for bootstrapping, which saves the applied declarations in an `IO.Ref` instead of an environment extension. Used to register elaborators, macros, tactics, and delaborators. -/ namespace Lean namespace KeyedDeclsAttribute -- could be a parameter as well, but right now it's all names abbrev Key := Name /-- `KeyedDeclsAttribute` definition. Important: `mkConst valueTypeName` and `Ξ³` must be definitionally equal. -/ structure Def (Ξ³ : Type) where /-- Builtin attribute name, if any (e.g., `builtinTermElab) -/ builtinName : Name := Name.anonymous /-- Attribute name (e.g., `termElab) -/ name : Name /-- Attribute description -/ descr : String valueTypeName : Name /-- Convert `Syntax` into a `Key`, the default implementation expects an identifier. -/ evalKey (builtin : Bool) (stx : Syntax) : AttrM Key := Attribute.Builtin.getId stx onAdded (builtin : Bool) (declName : Name) : AttrM Unit := pure () deriving Inhabited structure OLeanEntry where key : Key /-- Name of a declaration stored in the environment which has type `mkConst Def.valueTypeName`. -/ declName : Name deriving Inhabited structure AttributeEntry (Ξ³ : Type) extends OLeanEntry where /-- Recall that we cannot store `Ξ³` into .olean files because it is a closure. Given `OLeanEntry.declName`, we convert it into a `Ξ³` by using the unsafe function `evalConstCheck`. -/ value : Ξ³ abbrev Table (Ξ³ : Type) := SMap Key (List (AttributeEntry Ξ³)) structure ExtensionState (Ξ³ : Type) where newEntries : List OLeanEntry := [] table : Table Ξ³ := {} declNames : PHashSet Name := {} erased : PHashSet Name := {} deriving Inhabited abbrev Extension (Ξ³ : Type) := ScopedEnvExtension OLeanEntry (AttributeEntry Ξ³) (ExtensionState Ξ³) end KeyedDeclsAttribute structure KeyedDeclsAttribute (Ξ³ : Type) where defn : KeyedDeclsAttribute.Def Ξ³ -- imported/builtin instances tableRef : IO.Ref (KeyedDeclsAttribute.Table Ξ³) -- instances from current module ext : KeyedDeclsAttribute.Extension Ξ³ instance : Nonempty (KeyedDeclsAttribute Ξ³) := Nonempty.intro { defn := default, tableRef := Classical.ofNonempty, ext := default } namespace KeyedDeclsAttribute private def Table.insert (table : Table Ξ³) (v : AttributeEntry Ξ³) : Table Ξ³ := match table.find? v.key with | some vs => SMap.insert table v.key (v::vs) | none => SMap.insert table v.key [v] def ExtensionState.insert (s : ExtensionState Ξ³) (v : AttributeEntry Ξ³) : ExtensionState Ξ³ := { table := s.table.insert v newEntries := v.toOLeanEntry :: s.newEntries declNames := s.declNames.insert v.declName erased := s.erased.erase v.declName } def addBuiltin (attr : KeyedDeclsAttribute Ξ³) (key : Key) (declName : Name) (value : Ξ³) : IO Unit := attr.tableRef.modify fun m => m.insert { key, declName, value } def mkStateOfTable (table : Table Ξ³) : ExtensionState Ξ³ := { table declNames := table.fold (init := {}) fun s _ es => es.foldl (init := s) fun s e => s.insert e.declName } def ExtensionState.erase (s : ExtensionState Ξ³) (attrName : Name) (declName : Name) : CoreM (ExtensionState Ξ³) := do unless s.declNames.contains declName do throwError "'{declName}' does not have [{attrName}] attribute" return { s with erased := s.erased.insert declName, declNames := s.declNames.erase declName } protected unsafe def init {Ξ³} (df : Def Ξ³) (attrDeclName : Name) : IO (KeyedDeclsAttribute Ξ³) := do let tableRef ← IO.mkRef ({} : Table Ξ³) let ext : Extension Ξ³ ← registerScopedEnvExtension { name := df.name mkInitial := return mkStateOfTable (← tableRef.get) ofOLeanEntry := fun _ entry => do let ctx ← read match ctx.env.evalConstCheck Ξ³ ctx.opts df.valueTypeName entry.declName with | Except.ok f => return { toOLeanEntry := entry, value := f } | Except.error ex => throw (IO.userError ex) addEntry := fun s e => s.insert e toOLeanEntry := (Β·.toOLeanEntry) } unless df.builtinName.isAnonymous do registerBuiltinAttribute { ref := attrDeclName name := df.builtinName descr := "(builtin) " ++ df.descr add := fun declName stx kind => do unless kind == AttributeKind.global do throwError "invalid attribute '{df.builtinName}', must be global" let key ← df.evalKey true stx let decl ← getConstInfo declName match decl.type with | Expr.const c _ => if c != df.valueTypeName then throwError "unexpected type at '{declName}', '{df.valueTypeName}' expected" else /- builtin_initialize @addBuiltin $(mkConst valueTypeName) $(mkConst attrDeclName) $(key) $(declName) $(mkConst declName) -/ let val := mkAppN (mkConst `Lean.KeyedDeclsAttribute.addBuiltin) #[mkConst df.valueTypeName, mkConst attrDeclName, toExpr key, toExpr declName, mkConst declName] declareBuiltin declName val df.onAdded true declName | _ => throwError "unexpected type at '{declName}', '{df.valueTypeName}' expected" applicationTime := AttributeApplicationTime.afterCompilation } registerBuiltinAttribute { ref := attrDeclName name := df.name descr := df.descr erase := fun declName => do let s := ext.getState (← getEnv) let s ← s.erase df.name declName modifyEnv fun env => ext.modifyState env fun _ => s add := fun declName stx attrKind => do let key ← df.evalKey false stx match IR.getSorryDep (← getEnv) declName with | none => let val ← evalConstCheck Ξ³ df.valueTypeName declName ext.add { key := key, declName := declName, value := val } attrKind df.onAdded false declName | _ => -- If the declaration contains `sorry`, we skip `evalConstCheck` to avoid unnecessary bizarre error message pure () applicationTime := AttributeApplicationTime.afterCompilation } pure { defn := df, tableRef := tableRef, ext := ext } /-- Retrieve entries tagged with `[attr key]` or `[builtinAttr key]`. -/ def getEntries {Ξ³} (attr : KeyedDeclsAttribute Ξ³) (env : Environment) (key : Name) : List (AttributeEntry Ξ³) := let s := attr.ext.getState env let attrs := s.table.findD key [] if s.erased.isEmpty then attrs else attrs.filter fun attr => !s.erased.contains attr.declName /-- Retrieve values tagged with `[attr key]` or `[builtinAttr key]`. -/ def getValues {Ξ³} (attr : KeyedDeclsAttribute Ξ³) (env : Environment) (key : Name) : List Ξ³ := (getEntries attr env key).map AttributeEntry.value end KeyedDeclsAttribute end Lean
9dc1068420c6cb5d41446d4e22c91726230436d5
59a4b050600ed7b3d5826a8478db0a9bdc190252
/src/category_theory/preorder.lean
800fabf37736c53498266258452e55b7914361ff
[]
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
351
lean
import category_theory.category import category_theory.tactics.obviously universes u₁ namespace category_theory variables (Ξ± : Type u₁) instance [preorder Ξ±] : small_category Ξ± := { hom := Ξ» U V, ulift (plift (U ≀ V)), id := by tidy, comp := begin tidy, transitivity Y; assumption end } -- automate, via mono? end category_theory
a01e7a5a35ab2293625423e35de8f74ca8ae6d0e
46125763b4dbf50619e8846a1371029346f4c3db
/src/geometry/manifold/mfderiv.lean
a1ba5f37e421420e1c24bc633a9ad4904476b359
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
68,471
lean
/- Copyright (c) 2020 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel -/ import geometry.manifold.basic_smooth_bundle /-! # The derivative of functions between smooth manifolds Let `M` and `M'` be two smooth manifolds with corners over a field `π•œ` (with respective models with corners `I` on `(E, H)` and `I'` on `(E', H')`), and let `f : M β†’ M'`. We define the derivative of the function at a point, within a set or along the whole space, mimicking the API for (FrΓ©chet) derivatives. It is denoted by `mfderiv I I' f x`, where "m" stands for "manifold" and "f" for "FrΓ©chet" (as in the usual derivative `fderiv π•œ f x`). ## Main definitions * `unique_mdiff_on I s` : predicate saying that, at each point of the set `s`, a function can have at most one derivative. This technical condition is important when we define `mfderiv_within` below, as otherwise there is an arbitrary choice in the derivative, and many properties will fail (for instance the chain rule). This is analogous to `unique_diff_on π•œ s` in a vector space. Let `f` be a map between smooth manifolds. The following definitions follow the `fderiv` API. * `mfderiv I I' f x` : the derivative of `f` at `x`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. If the map is not differentiable, this is `0`. * `mfderiv_within I I' f s x` : the derivative of `f` at `x` within `s`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. If the map is not differentiable within `s`, this is `0`. * `mdifferentiable_at I I' f x` : Prop expressing whether `f` is differentiable at `x`. * `mdifferentiable_within_at π•œ f s x` : Prop expressing whether `f` is differentiable within `s` at `x`. * `has_mfderiv_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative at `x`. * `has_mfderiv_within_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative within `s` at `x`. * `mdifferentiable_on I I' f s` : Prop expressing that `f` is differentiable on the set `s`. * `mdifferentiable I I' f` : Prop expressing that `f` is differentiable everywhere. * `bundle_mfderiv I I' f` : the derivative of `f`, as a map from the tangent bundle of `M` to the tangent bundle of `M'`. We also establish results on the differential of the identity, constant functions, charts, extended charts. For functions between vector spaces, we show that the usual notions and the manifold notions coincide. ## Implementation notes The tangent bundle is constructed using the machinery of topological fiber bundles, for which one can define bundled morphisms and construct canonically maps from the total space of one bundle to the total space of another one. One could use this mechanism to construct directly the derivative of a smooth map. However, we want to define the derivative of any map (and let it be zero if the map is not differentiable) to avoid proof arguments everywhere. This means we have to go back to the details of the definition of the total space of a fiber bundle constructed from core, to cook up a suitable definition of the derivative. It is the following: at each point, we have a preferred chart (used to identify the fiber above the point with the model vector space in fiber bundles). Then one should read the function using these preferred charts at `x` and `f x`, and take the derivative of `f` in these charts. Due to the fact that we are working in a model with corners, with an additional embedding `I` of the model space `H` in the model vector space `E`, the charts taking values in `E` are not the original charts of the manifold, but those ones composed with `I`, called extended charts. We define `written_in_ext_chart I I' x f` for the function `f` written in the preferred extended charts. Then the manifold derivative of `f`, at `x`, is just the usual derivative of `written_in_ext_chart I I' x f`, at the point `(ext_chart_at I x).to_fun x`. There is a subtelty with respect to continuity: if the function is not continuous, then the image of a small open set around `x` will not be contained in the source of the preferred chart around `f x`, which means that when reading `f` in the chart one is losing some information. To avoid this, we include continuity in the definition of differentiablity (which is reasonable since with any definition, differentiability implies continuity). *Warning*: the derivative (even within a subset) is a linear map on the whole tangent space. Suppose that one is given a smooth submanifold `N`, and a function which is smooth on `N` (i.e., its restriction to the subtype `N` is smooth). Then, in the whole manifold `M`, the property `mdifferentiable_on I I' f N` holds. However, `mfderiv_within I I' f N` is not uniquely defined (what values would one choose for vectors that are transverse to `N`?), which can create issues down the road. The problem here is that knowing the value of `f` along `N` does not determine the differential of `f` in all directions. This is in contrast to the case where `N` would be an open subset, or a submanifold with boundary of maximal dimension, where this issue does not appear. The predicate `unique_mdiff_on I N` indicates that the derivative along `N` is unique if it exists, and is an assumption in most statements requiring a form of uniqueness. On a vector space, the manifold derivative and the usual derivative are equal. This means in particular that they live on the same space, i.e., the tangent space is defeq to the original vector space. To get this property is a motivation for our definition of the tangent space as a single copy of the vector space, instead of more usual definitions such as the space of derivations, or the space of equivalence classes of smooth curves in the manifold. ## Notations For the composition of local homeomorphisms and local equivs, we use respectively ` ≫ₕ` and ` ≫`. ## Tags Derivative, manifold -/ noncomputable theory open_locale classical topological_space open set local infixr ` ≫ₕ `:100 := local_homeomorph.trans local infixr ` ≫ `:100 := local_equiv.trans universe u section derivatives_definitions /-! ### Derivative of maps between manifolds The derivative of a smooth map `f` between smooth manifold `M` and `M'` at `x` is a bounded linear map from the tangent space to `M` at `x`, to the tangent space to `M'` at `f x`. Since we defined the tangent space using one specific chart, the formula for the derivative is written in terms of this specific chart. We use the names `mdifferentiable` and `mfderiv`, where the prefix letter `m` means "manifold". -/ variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {H : Type*} [topological_space H] (I : model_with_corners π•œ E H) {M : Type*} [topological_space M] [manifold H M] {E' : Type*} [normed_group E'] [normed_space π•œ E'] {H' : Type*} [topological_space H'] (I' : model_with_corners π•œ E' H') {M' : Type*} [topological_space M'] [manifold H' M'] /-- Predicate ensuring that, at a point and within a set, a function can have at most one derivative. This is expressed using the preferred chart at the considered point. -/ def unique_mdiff_within_at (s : set M) (x : M) := unique_diff_within_at π•œ ((ext_chart_at I x).inv_fun ⁻¹' s ∩ range I.to_fun) ((ext_chart_at I x).to_fun x) /-- Predicate ensuring that, at all points of a set, a function can have at most one derivative. -/ def unique_mdiff_on (s : set M) := βˆ€x∈s, unique_mdiff_within_at I s x /-- Conjugating a function to write it in the preferred charts around `x`. The manifold derivative of `f` will just be the derivative of this conjugated function. -/ def written_in_ext_chart_at (x : M) (f : M β†’ M') : E β†’ E' := (ext_chart_at I' (f x)).to_fun ∘ f ∘ (ext_chart_at I x).inv_fun /-- `mdifferentiable_within_at I I' f s x` indicates that the function `f` between manifolds has a derivative at the point `x` within the set `s`. This is a generalization of `differentiable_within_at` to manifolds. We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def mdifferentiable_within_at (f : M β†’ M') (s : set M) (x : M) := continuous_within_at f s x ∧ differentiable_within_at π•œ (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).inv_fun ⁻¹' s ∩ range I.to_fun) ((ext_chart_at I x).to_fun x) /-- `mdifferentiable_at I I' f x` indicates that the function `f` between manifolds has a derivative at the point `x`. This is a generalization of `differentiable_at` to manifolds. We require continuity in the definition, as otherwise points close to `x` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def mdifferentiable_at (f : M β†’ M') (x : M) := continuous_at f x ∧ differentiable_within_at π•œ (written_in_ext_chart_at I I' x f) (range I.to_fun) ((ext_chart_at I x).to_fun x) /-- `mdifferentiable_on I I' f s` indicates that the function `f` between manifolds has a derivative within `s` at all points of `s`. This is a generalization of `differentiable_on` to manifolds. -/ def mdifferentiable_on (f : M β†’ M') (s : set M) := βˆ€x ∈ s, mdifferentiable_within_at I I' f s x /-- `mdifferentiable I I' f` indicates that the function `f` between manifolds has a derivative everywhere. This is a generalization of `differentiable` to manifolds. -/ def mdifferentiable (f : M β†’ M') := βˆ€x, mdifferentiable_at I I' f x /-- Prop registering if a local homeomorphism is a local diffeomorphism on its source -/ def local_homeomorph.mdifferentiable (f : local_homeomorph M M') := (mdifferentiable_on I I' f.to_fun f.source) ∧ (mdifferentiable_on I' I f.inv_fun f.target) variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M'] /-- `has_mfderiv_within_at I I' f s x f'` indicates that the function `f` between manifolds has, at the point `x` and within the set `s`, the derivative `f'`. Here, `f'` is a continuous linear map from the tangent space at `x` to the tangent space at `f x`. This is a generalization of `has_fderiv_within_at` to manifolds (as indicated by the prefix `m`). The order of arguments is changed as the type of the derivative `f'` depends on the choice of `x`. We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def has_mfderiv_within_at (f : M β†’ M') (s : set M) (x : M) (f' : tangent_space I x β†’L[π•œ] tangent_space I' (f x)) := continuous_within_at f s x ∧ has_fderiv_within_at (written_in_ext_chart_at I I' x f : E β†’ E') f' ((ext_chart_at I x).inv_fun ⁻¹' s ∩ range I.to_fun) ((ext_chart_at I x).to_fun x) /-- `has_mfderiv_at I I' f x f'` indicates that the function `f` between manifolds has, at the point `x`, the derivative `f'`. Here, `f'` is a continuous linear map from the tangent space at `x` to the tangent space at `f x`. We require continuity in the definition, as otherwise points close to `x` `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def has_mfderiv_at (f : M β†’ M') (x : M) (f' : tangent_space I x β†’L[π•œ] tangent_space I' (f x)) := continuous_at f x ∧ has_fderiv_within_at (written_in_ext_chart_at I I' x f : E β†’ E') f' (range I.to_fun) ((ext_chart_at I x).to_fun x) /-- Let `f` be a function between two smooth manifolds. Then `mfderiv_within I I' f s x` is the derivative of `f` at `x` within `s`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. -/ def mfderiv_within (f : M β†’ M') (s : set M) (x : M) : tangent_space I x β†’L[π•œ] tangent_space I' (f x) := if h : mdifferentiable_within_at I I' f s x then (fderiv_within π•œ (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).inv_fun ⁻¹' s ∩ range I.to_fun) ((ext_chart_at I x).to_fun x) : _) else continuous_linear_map.zero /-- Let `f` be a function between two smooth manifolds. Then `mfderiv I I' f x` is the derivative of `f` at `x`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. -/ def mfderiv (f : M β†’ M') (x : M) : tangent_space I x β†’L[π•œ] tangent_space I' (f x) := if h : mdifferentiable_at I I' f x then (fderiv_within π•œ (written_in_ext_chart_at I I' x f : E β†’ E') (range I.to_fun) ((ext_chart_at I x).to_fun x) : _) else continuous_linear_map.zero set_option class.instance_max_depth 60 /-- The derivative within a set, as a map between the tangent bundles -/ def bundle_mfderiv_within (f : M β†’ M') (s : set M) : tangent_bundle I M β†’ tangent_bundle I' M' := Ξ»p, ⟨f p.1, (mfderiv_within I I' f s p.1 : tangent_space I p.1 β†’ tangent_space I' (f p.1)) p.2⟩ /-- The derivative, as a map between the tangent bundles -/ def bundle_mfderiv (f : M β†’ M') : tangent_bundle I M β†’ tangent_bundle I' M' := Ξ»p, ⟨f p.1, (mfderiv I I' f p.1 : tangent_space I p.1 β†’ tangent_space I' (f p.1)) p.2⟩ end derivatives_definitions section derivatives_properties /-! ### Unique differentiability sets in manifolds -/ variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {H : Type*} [topological_space H] (I : model_with_corners π•œ E H) {M : Type*} [topological_space M] [manifold H M] -- {E' : Type*} [normed_group E'] [normed_space π•œ E'] {H' : Type*} [topological_space H'] {I' : model_with_corners π•œ E' H'} {M' : Type*} [topological_space M'] [manifold H' M'] {E'' : Type*} [normed_group E''] [normed_space π•œ E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners π•œ E'' H''} {M'' : Type*} [topological_space M''] [manifold H'' M''] {f fβ‚€ f₁ : M β†’ M'} {x : M} {s t : set M} {g : M' β†’ M''} {u : set M'} lemma unique_mdiff_within_at_univ : unique_mdiff_within_at I univ x := begin unfold unique_mdiff_within_at, simp only [preimage_univ, univ_inter], exact I.unique_diff _ (mem_range_self _) end variable {I} lemma unique_mdiff_within_at_iff {s : set M} {x : M} : unique_mdiff_within_at I s x ↔ unique_diff_within_at π•œ ((ext_chart_at I x).inv_fun ⁻¹' s ∩ (ext_chart_at I x).target) ((ext_chart_at I x).to_fun x) := begin apply unique_diff_within_at_congr, rw [nhds_within_inter, nhds_within_inter, nhds_within_ext_chart_target_eq] end lemma unique_mdiff_within_at.mono (h : unique_mdiff_within_at I s x) (st : s βŠ† t) : unique_mdiff_within_at I t x := unique_diff_within_at.mono h $ inter_subset_inter (preimage_mono st) (subset.refl _) lemma unique_mdiff_within_at.inter' (hs : unique_mdiff_within_at I s x) (ht : t ∈ nhds_within x s) : unique_mdiff_within_at I (s ∩ t) x := begin rw [unique_mdiff_within_at, ext_chart_preimage_inter_eq], exact unique_diff_within_at.inter' hs (ext_chart_preimage_mem_nhds_within I x ht) end lemma unique_mdiff_within_at.inter (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝 x) : unique_mdiff_within_at I (s ∩ t) x := begin rw [unique_mdiff_within_at, ext_chart_preimage_inter_eq], exact unique_diff_within_at.inter hs (ext_chart_preimage_mem_nhds I x ht) end lemma is_open.unique_mdiff_within_at (xs : x ∈ s) (hs : is_open s) : unique_mdiff_within_at I s x := begin have := unique_mdiff_within_at.inter (unique_mdiff_within_at_univ I) (mem_nhds_sets hs xs), rwa univ_inter at this end lemma unique_mdiff_on.inter (hs : unique_mdiff_on I s) (ht : is_open t) : unique_mdiff_on I (s ∩ t) := Ξ»x hx, unique_mdiff_within_at.inter (hs _ hx.1) (mem_nhds_sets ht hx.2) lemma is_open.unique_mdiff_on (hs : is_open s) : unique_mdiff_on I s := Ξ»x hx, is_open.unique_mdiff_within_at hx hs /- We name the typeclass variables related to `smooth_manifold_with_corners` structure as they are necessary in lemmas mentioning the derivative, but not in lemmas about differentiability, so we want to include them or omit them when necessary. -/ variables [Is : smooth_manifold_with_corners I M] [I's : smooth_manifold_with_corners I' M'] [I''s : smooth_manifold_with_corners I'' M''] {f' fβ‚€' f₁' : tangent_space I x β†’L[π•œ] tangent_space I' (f x)} {g' : tangent_space I' (f x) β†’L[π•œ] tangent_space I'' (g (f x))} /-- `unique_mdiff_within_at` achieves its goal: it implies the uniqueness of the derivative. -/ theorem unique_mdiff_within_at.eq (U : unique_mdiff_within_at I s x) (h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') : f' = f₁' := U.eq h.2 h₁.2 theorem unique_mdiff_on.eq (U : unique_mdiff_on I s) (hx : x ∈ s) (h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') : f' = f₁' := unique_mdiff_within_at.eq (U _ hx) h h₁ /-! ### General lemmas on derivatives of functions between manifolds We mimick the API for functions between vector spaces -/ lemma mdifferentiable_within_at_iff {f : M β†’ M'} {s : set M} {x : M} : mdifferentiable_within_at I I' f s x ↔ continuous_within_at f s x ∧ differentiable_within_at π•œ (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).target ∩ (ext_chart_at I x).inv_fun ⁻¹' s) ((ext_chart_at I x).to_fun x) := begin refine and_congr iff.rfl (exists_congr $ Ξ» f', _), rw [inter_comm], simp only [has_fderiv_within_at, nhds_within_inter, nhds_within_ext_chart_target_eq] end include Is I's set_option class.instance_max_depth 60 lemma mfderiv_within_zero_of_not_mdifferentiable_within_at (h : Β¬ mdifferentiable_within_at I I' f s x) : mfderiv_within I I' f s x = 0 := by { simp [mfderiv_within, h], refl } lemma mfderiv_zero_of_not_mdifferentiable_at (h : Β¬ mdifferentiable_at I I' f x) : mfderiv I I' f x = 0 := by { simp [mfderiv, h], refl } theorem has_mfderiv_within_at.mono (h : has_mfderiv_within_at I I' f t x f') (hst : s βŠ† t) : has_mfderiv_within_at I I' f s x f' := ⟨ continuous_within_at.mono h.1 hst, has_fderiv_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩ theorem has_mfderiv_at.has_mfderiv_within_at (h : has_mfderiv_at I I' f x f') : has_mfderiv_within_at I I' f s x f' := ⟨ continuous_at.continuous_within_at h.1, has_fderiv_within_at.mono h.2 (inter_subset_right _ _) ⟩ lemma has_mfderiv_within_at.mdifferentiable_within_at (h : has_mfderiv_within_at I I' f s x f') : mdifferentiable_within_at I I' f s x := ⟨h.1, ⟨f', h.2⟩⟩ lemma has_mfderiv_at.mdifferentiable_at (h : has_mfderiv_at I I' f x f') : mdifferentiable_at I I' f x := ⟨h.1, ⟨f', h.2⟩⟩ @[simp] lemma has_mfderiv_within_at_univ : has_mfderiv_within_at I I' f univ x f' ↔ has_mfderiv_at I I' f x f' := by simp [has_mfderiv_within_at, has_mfderiv_at, continuous_within_at_univ] theorem has_mfderiv_at_unique (hβ‚€ : has_mfderiv_at I I' f x fβ‚€') (h₁ : has_mfderiv_at I I' f x f₁') : fβ‚€' = f₁' := begin rw ← has_mfderiv_within_at_univ at hβ‚€ h₁, exact (unique_mdiff_within_at_univ I).eq hβ‚€ h₁ end lemma has_mfderiv_within_at_inter' (h : t ∈ nhds_within x s) : has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' := begin rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_preimage_inter_eq, has_fderiv_within_at_inter', continuous_within_at_inter' h], exact ext_chart_preimage_mem_nhds_within I x h, end lemma has_mfderiv_within_at_inter (h : t ∈ 𝓝 x) : has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' := begin rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_preimage_inter_eq, has_fderiv_within_at_inter, continuous_within_at_inter h], exact ext_chart_preimage_mem_nhds I x h, end lemma has_mfderiv_within_at.union (hs : has_mfderiv_within_at I I' f s x f') (ht : has_mfderiv_within_at I I' f t x f') : has_mfderiv_within_at I I' f (s βˆͺ t) x f' := begin split, { exact continuous_within_at.union hs.1 ht.1 }, { convert has_fderiv_within_at.union hs.2 ht.2, simp [union_inter_distrib_right] } end lemma has_mfderiv_within_at.nhds_within (h : has_mfderiv_within_at I I' f s x f') (ht : s ∈ nhds_within x t) : has_mfderiv_within_at I I' f t x f' := (has_mfderiv_within_at_inter' ht).1 (h.mono (inter_subset_right _ _)) lemma has_mfderiv_within_at.has_mfderiv_at (h : has_mfderiv_within_at I I' f s x f') (hs : s ∈ 𝓝 x) : has_mfderiv_at I I' f x f' := by rwa [← univ_inter s, has_mfderiv_within_at_inter hs, has_mfderiv_within_at_univ] at h lemma mdifferentiable_within_at.has_mfderiv_within_at (h : mdifferentiable_within_at I I' f s x) : has_mfderiv_within_at I I' f s x (mfderiv_within I I' f s x) := begin refine ⟨h.1, _⟩, simp [mfderiv_within, h], exact differentiable_within_at.has_fderiv_within_at h.2 end lemma mdifferentiable_within_at.mfderiv_within (h : mdifferentiable_within_at I I' f s x) : (mfderiv_within I I' f s x) = fderiv_within π•œ (written_in_ext_chart_at I I' x f : _) ((ext_chart_at I x).inv_fun ⁻¹' s ∩ range I.to_fun) ((ext_chart_at I x).to_fun x) := by simp [mfderiv_within, h] lemma mdifferentiable_at.has_mfderiv_at (h : mdifferentiable_at I I' f x) : has_mfderiv_at I I' f x (mfderiv I I' f x) := begin refine ⟨h.1, _⟩, simp [mfderiv, h], exact differentiable_within_at.has_fderiv_within_at h.2 end lemma mdifferentiable_at.mfderiv (h : mdifferentiable_at I I' f x) : (mfderiv I I' f x) = fderiv_within π•œ (written_in_ext_chart_at I I' x f : _) (range I.to_fun) ((ext_chart_at I x).to_fun x) := by simp [mfderiv, h] lemma has_mfderiv_at.mfderiv (h : has_mfderiv_at I I' f x f') : mfderiv I I' f x = f' := by { ext, rw has_mfderiv_at_unique h h.mdifferentiable_at.has_mfderiv_at } lemma has_mfderiv_within_at.mfderiv_within (h : has_mfderiv_within_at I I' f s x f') (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' f s x = f' := by { ext, rw hxs.eq h h.mdifferentiable_within_at.has_mfderiv_within_at } lemma mdifferentiable.mfderiv_within (h : mdifferentiable_at I I' f x) (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' f s x = mfderiv I I' f x := begin apply has_mfderiv_within_at.mfderiv_within _ hxs, exact h.has_mfderiv_at.has_mfderiv_within_at end lemma mfderiv_within_subset (st : s βŠ† t) (hs : unique_mdiff_within_at I s x) (h : mdifferentiable_within_at I I' f t x) : mfderiv_within I I' f s x = mfderiv_within I I' f t x := ((mdifferentiable_within_at.has_mfderiv_within_at h).mono st).mfderiv_within hs omit Is I's lemma mdifferentiable_within_at.mono (hst : s βŠ† t) (h : mdifferentiable_within_at I I' f t x) : mdifferentiable_within_at I I' f s x := ⟨ continuous_within_at.mono h.1 hst, differentiable_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩ lemma mdifferentiable_within_at_univ : mdifferentiable_within_at I I' f univ x ↔ mdifferentiable_at I I' f x := by simp [mdifferentiable_within_at, mdifferentiable_at, continuous_within_at_univ] lemma mdifferentiable_within_at_inter (ht : t ∈ 𝓝 x) : mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x := begin rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_preimage_inter_eq, differentiable_within_at_inter, continuous_within_at_inter ht], exact ext_chart_preimage_mem_nhds I x ht end lemma mdifferentiable_within_at_inter' (ht : t ∈ nhds_within x s) : mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x := begin rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_preimage_inter_eq, differentiable_within_at_inter', continuous_within_at_inter' ht], exact ext_chart_preimage_mem_nhds_within I x ht end lemma mdifferentiable_at.mdifferentiable_within_at (h : mdifferentiable_at I I' f x) : mdifferentiable_within_at I I' f s x := mdifferentiable_within_at.mono (subset_univ _) (mdifferentiable_within_at_univ.2 h) lemma mdifferentiable_within_at.mdifferentiable_at (h : mdifferentiable_within_at I I' f s x) (hs : s ∈ 𝓝 x) : mdifferentiable_at I I' f x := begin have : s = univ ∩ s, by rw univ_inter, rwa [this, mdifferentiable_within_at_inter hs, mdifferentiable_within_at_univ] at h, end lemma mdifferentiable_on.mono (h : mdifferentiable_on I I' f t) (st : s βŠ† t) : mdifferentiable_on I I' f s := Ξ»x hx, (h x (st hx)).mono st lemma mdifferentiable_on_univ : mdifferentiable_on I I' f univ ↔ mdifferentiable I I' f := by { simp [mdifferentiable_on, mdifferentiable_within_at_univ], refl } lemma mdifferentiable.mdifferentiable_on (h : mdifferentiable I I' f) : mdifferentiable_on I I' f s := (mdifferentiable_on_univ.2 h).mono (subset_univ _) lemma mdifferentiable_on_of_locally_mdifferentiable_on (h : βˆ€x∈s, βˆƒu, is_open u ∧ x ∈ u ∧ mdifferentiable_on I I' f (s ∩ u)) : mdifferentiable_on I I' f s := begin assume x xs, rcases h x xs with ⟨t, t_open, xt, ht⟩, exact (mdifferentiable_within_at_inter (mem_nhds_sets t_open xt)).1 (ht x ⟨xs, xt⟩) end include Is I's @[simp] lemma mfderiv_within_univ : mfderiv_within I I' f univ = mfderiv I I' f := begin ext x : 1, simp [mfderiv_within, mfderiv], erw mdifferentiable_within_at_univ end lemma mfderiv_within_inter (ht : t ∈ 𝓝 x) (hs : unique_mdiff_within_at I s x) : mfderiv_within I I' f (s ∩ t) x = mfderiv_within I I' f s x := by erw [mfderiv_within, mfderiv_within, ext_chart_preimage_inter_eq, mdifferentiable_within_at_inter ht, fderiv_within_inter (ext_chart_preimage_mem_nhds I x ht) hs] omit Is I's /-! ### Deriving continuity from differentiability on manifolds -/ theorem has_mfderiv_within_at.continuous_within_at (h : mdifferentiable_within_at I I' f s x) : continuous_within_at f s x := h.1 theorem has_mfderiv_at.continuous_at (h : has_mfderiv_at I I' f x f') : continuous_at f x := h.1 lemma mdifferentiable_within_at.continuous_within_at (h : mdifferentiable_within_at I I' f s x) : continuous_within_at f s x := h.1 lemma mdifferentiable_at.continuous_at (h : mdifferentiable_at I I' f x) : continuous_at f x := h.1 lemma mdifferentiable_on.continuous_on (h : mdifferentiable_on I I' f s) : continuous_on f s := Ξ»x hx, (h x hx).continuous_within_at lemma mdifferentiable.continuous (h : mdifferentiable I I' f) : continuous f := continuous_iff_continuous_at.2 $ Ξ»x, (h x).continuous_at include Is I's lemma bundle_mfderiv_within_subset {p : tangent_bundle I M} (st : s βŠ† t) (hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_within_at I I' f t p.1) : bundle_mfderiv_within I I' f s p = bundle_mfderiv_within I I' f t p := by { simp [bundle_mfderiv_within], rw mfderiv_within_subset st hs h } lemma bundle_mfderiv_within_univ : bundle_mfderiv_within I I' f univ = bundle_mfderiv I I' f := by { ext p : 1, simp [bundle_mfderiv_within, bundle_mfderiv], rw mfderiv_within_univ } lemma bundle_mfderiv_within_eq_bundle_mfderiv {p : tangent_bundle I M} (hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_at I I' f p.1) : bundle_mfderiv_within I I' f s p = bundle_mfderiv I I' f p := begin rw ← mdifferentiable_within_at_univ at h, rw ← bundle_mfderiv_within_univ, exact bundle_mfderiv_within_subset (subset_univ _) hs h, end @[simp] lemma bundle_mfderiv_within_tangent_bundle_proj {p : tangent_bundle I M} : tangent_bundle.proj I' M' (bundle_mfderiv_within I I' f s p) = f (tangent_bundle.proj I M p) := rfl @[simp] lemma bundle_mfderiv_within_proj {p : tangent_bundle I M} : (bundle_mfderiv_within I I' f s p).1 = f p.1 := rfl @[simp] lemma bundle_mfderiv_tangent_bundle_proj {p : tangent_bundle I M} : tangent_bundle.proj I' M' (bundle_mfderiv I I' f p) = f (tangent_bundle.proj I M p) := rfl @[simp] lemma bundle_mfderiv_proj {p : tangent_bundle I M} : (bundle_mfderiv I I' f p).1 = f p.1 := rfl omit Is I's /-! ### Congruence lemmas for derivatives on manifolds -/ lemma has_mfderiv_within_at.congr_of_mem_nhds_within (h : has_mfderiv_within_at I I' f s x f') (h₁ : βˆ€αΆ  y in nhds_within x s, f₁ y = f y) (hx : f₁ x = f x) : has_mfderiv_within_at I I' f₁ s x f' := begin refine ⟨continuous_within_at.congr_of_mem_nhds_within h.1 h₁ hx, _⟩, apply has_fderiv_within_at.congr_of_mem_nhds_within h.2, { have : (ext_chart_at I x).inv_fun ⁻¹' {y | f₁ y = f y} ∈ nhds_within ((ext_chart_at I x).to_fun x) ((ext_chart_at I x).inv_fun ⁻¹' s ∩ range I.to_fun) := ext_chart_preimage_mem_nhds_within I x h₁, apply filter.mem_sets_of_superset this (Ξ»y, _), simp [written_in_ext_chart_at, hx] {contextual := tt} }, { simp [written_in_ext_chart_at, hx] }, end lemma has_mfderiv_within_at.congr_mono (h : has_mfderiv_within_at I I' f s x f') (ht : βˆ€x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t βŠ† s) : has_mfderiv_within_at I I' f₁ t x f' := (h.mono h₁).congr_of_mem_nhds_within (filter.mem_inf_sets_of_right ht) hx lemma has_mfderiv_at.congr_of_mem_nhds (h : has_mfderiv_at I I' f x f') (h₁ : βˆ€αΆ  y in 𝓝 x, f₁ y = f y) : has_mfderiv_at I I' f₁ x f' := begin erw ← has_mfderiv_within_at_univ at ⊒ h, apply h.congr_of_mem_nhds_within _ (mem_of_nhds h₁ : _), rwa nhds_within_univ end include Is I's lemma mdifferentiable_within_at.congr_of_mem_nhds_within (h : mdifferentiable_within_at I I' f s x) (h₁ : βˆ€αΆ  y in nhds_within x s, f₁ y = f y) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x := (h.has_mfderiv_within_at.congr_of_mem_nhds_within h₁ hx).mdifferentiable_within_at variables (I I') lemma mdifferentiable_within_at_congr_of_mem_nhds_within (h₁ : βˆ€αΆ  y in nhds_within x s, f₁ y = f y) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f s x ↔ mdifferentiable_within_at I I' f₁ s x := begin split, { assume h, apply h.congr_of_mem_nhds_within h₁ hx }, { assume h, apply h.congr_of_mem_nhds_within _ hx.symm, apply h₁.mono, intro y, apply eq.symm } end variables {I I'} lemma mdifferentiable_within_at.congr_mono (h : mdifferentiable_within_at I I' f s x) (ht : βˆ€x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t βŠ† s) : mdifferentiable_within_at I I' f₁ t x := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx h₁).mdifferentiable_within_at lemma mdifferentiable_within_at.congr (h : mdifferentiable_within_at I I' f s x) (ht : βˆ€x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx (subset.refl _)).mdifferentiable_within_at lemma mdifferentiable_on.congr_mono (h : mdifferentiable_on I I' f s) (h' : βˆ€x ∈ t, f₁ x = f x) (h₁ : t βŠ† s) : mdifferentiable_on I I' f₁ t := Ξ» x hx, (h x (h₁ hx)).congr_mono h' (h' x hx) h₁ lemma mdifferentiable_at.congr_of_mem_nhds (h : mdifferentiable_at I I' f x) (hL : βˆ€αΆ  y in 𝓝 x, f₁ y = f y) : mdifferentiable_at I I' f₁ x := ((h.has_mfderiv_at).congr_of_mem_nhds hL).mdifferentiable_at lemma mdifferentiable_within_at.mfderiv_within_congr_mono (h : mdifferentiable_within_at I I' f s x) (hs : βˆ€x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (hxt : unique_mdiff_within_at I t x) (h₁ : t βŠ† s) : mfderiv_within I I' f₁ t x = (mfderiv_within I I' f s x : _) := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at hs hx h₁).mfderiv_within hxt lemma mfderiv_within_congr_of_mem_nhds_within (hs : unique_mdiff_within_at I s x) (hL : βˆ€αΆ  y in nhds_within x s, f₁ y = f y) (hx : f₁ x = f x) : mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) := begin by_cases h : mdifferentiable_within_at I I' f s x, { exact ((h.has_mfderiv_within_at).congr_of_mem_nhds_within hL hx).mfderiv_within hs }, { unfold mfderiv_within, rw [dif_neg, dif_neg], assumption, rwa ← mdifferentiable_within_at_congr_of_mem_nhds_within I I' hL hx } end lemma mfderiv_congr_of_mem_nhds (hL : βˆ€αΆ  y in 𝓝 x, f₁ y = f y) : mfderiv I I' f₁ x = (mfderiv I I' f x : _) := begin have A : f₁ x = f x := (mem_of_nhds hL : _), rw [← mfderiv_within_univ, ← mfderiv_within_univ], rw ← nhds_within_univ at hL, exact mfderiv_within_congr_of_mem_nhds_within (unique_mdiff_within_at_univ I) hL A end /-! ### Composition lemmas -/ omit Is I's lemma written_in_ext_chart_comp (h : continuous_within_at f s x) : {y | written_in_ext_chart_at I I'' x (g ∘ f) y = ((written_in_ext_chart_at I' I'' (f x) g) ∘ (written_in_ext_chart_at I I' x f)) y} ∈ nhds_within ((ext_chart_at I x).to_fun x) ((ext_chart_at I x).inv_fun ⁻¹' s ∩ range I.to_fun) := begin apply @filter.mem_sets_of_superset _ _ ((f ∘ (ext_chart_at I x).inv_fun)⁻¹' (ext_chart_at I' (f x)).source) _ (ext_chart_preimage_mem_nhds_within I x (h.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))), assume y hy, simp only [ext_chart_at, written_in_ext_chart_at, model_with_corners_left_inv, mem_set_of_eq, function.comp_app, local_equiv.trans_to_fun, local_equiv.trans_inv_fun], rw (chart_at H' (f x)).left_inv, simpa [ext_chart_at_source] using hy end variable (x) include Is I's I''s theorem has_mfderiv_within_at.comp (hg : has_mfderiv_within_at I' I'' g u (f x) g') (hf : has_mfderiv_within_at I I' f s x f') (hst : s βŠ† f ⁻¹' u) : has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') := begin refine ⟨continuous_within_at.comp hg.1 hf.1 hst, _⟩, have A : has_fderiv_within_at ((written_in_ext_chart_at I' I'' (f x) g) ∘ (written_in_ext_chart_at I I' x f)) (continuous_linear_map.comp g' f' : E β†’L[π•œ] E'') ((ext_chart_at I x).inv_fun ⁻¹' s ∩ range (I.to_fun)) ((ext_chart_at I x).to_fun x), { have : (ext_chart_at I x).inv_fun ⁻¹' (f ⁻¹' (ext_chart_at I' (f x)).source) ∈ nhds_within ((ext_chart_at I x).to_fun x) ((ext_chart_at I x).inv_fun ⁻¹' s ∩ range I.to_fun) := (ext_chart_preimage_mem_nhds_within I x (hf.1.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))), unfold has_mfderiv_within_at at *, rw [← has_fderiv_within_at_inter' this, ← ext_chart_preimage_inter_eq] at hf ⊒, have : written_in_ext_chart_at I I' x f ((ext_chart_at I x).to_fun x) = (ext_chart_at I' (f x)).to_fun (f x), by simp [written_in_ext_chart_at, local_equiv.left_inv, mem_chart_source], rw ← this at hg, apply has_fderiv_within_at.comp ((ext_chart_at I x).to_fun x) hg.2 hf.2 _, assume y hy, simp [ext_chart_at, local_equiv.trans_source, -mem_range] at hy, have : f ((chart_at H x).inv_fun (I.inv_fun y)) ∈ u := hst hy.1.1, simp [written_in_ext_chart_at, ext_chart_at, -mem_range, hy, this, mem_range_self] }, apply A.congr_of_mem_nhds_within (written_in_ext_chart_comp hf.1), simp [written_in_ext_chart_at, ext_chart_at, local_equiv.left_inv, mem_chart_source] end /-- The chain rule. -/ theorem has_mfderiv_at.comp (hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_at I I' f x f') : has_mfderiv_at I I'' (g ∘ f) x (g'.comp f') := begin rw ← has_mfderiv_within_at_univ at *, exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ end theorem has_mfderiv_at.comp_has_mfderiv_within_at (hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_within_at I I' f s x f') : has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') := begin rw ← has_mfderiv_within_at_univ at *, exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ end lemma mdifferentiable_within_at.comp (hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x) (h : s βŠ† f ⁻¹' u) : mdifferentiable_within_at I I'' (g ∘ f) s x := begin rcases hf.2 with ⟨f', hf'⟩, have F : has_mfderiv_within_at I I' f s x f' := ⟨hf.1, hf'⟩, rcases hg.2 with ⟨g', hg'⟩, have G : has_mfderiv_within_at I' I'' g u (f x) g' := ⟨hg.1, hg'⟩, exact (has_mfderiv_within_at.comp x G F h).mdifferentiable_within_at end lemma mdifferentiable_at.comp (hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) : mdifferentiable_at I I'' (g ∘ f) x := (hg.has_mfderiv_at.comp x hf.has_mfderiv_at).mdifferentiable_at lemma mfderiv_within_comp (hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x) (h : s βŠ† f ⁻¹' u) (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I'' (g ∘ f) s x = (mfderiv_within I' I'' g u (f x)).comp (mfderiv_within I I' f s x) := begin apply has_mfderiv_within_at.mfderiv_within _ hxs, exact has_mfderiv_within_at.comp x hg.has_mfderiv_within_at hf.has_mfderiv_within_at h end lemma mfderiv_comp (hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) : mfderiv I I'' (g ∘ f) x = (mfderiv I' I'' g (f x)).comp (mfderiv I I' f x) := begin apply has_mfderiv_at.mfderiv, exact has_mfderiv_at.comp x hg.has_mfderiv_at hf.has_mfderiv_at end lemma mdifferentiable_on.comp (hg : mdifferentiable_on I' I'' g u) (hf : mdifferentiable_on I I' f s) (st : s βŠ† f ⁻¹' u) : mdifferentiable_on I I'' (g ∘ f) s := Ξ»x hx, mdifferentiable_within_at.comp x (hg (f x) (st hx)) (hf x hx) st lemma mdifferentiable.comp (hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) : mdifferentiable I I'' (g ∘ f) := Ξ»x, mdifferentiable_at.comp x (hg (f x)) (hf x) lemma bundle_mfderiv_within_comp_at (p : tangent_bundle I M) (hg : mdifferentiable_within_at I' I'' g u (f p.1)) (hf : mdifferentiable_within_at I I' f s p.1) (h : s βŠ† f ⁻¹' u) (hps : unique_mdiff_within_at I s p.1) : bundle_mfderiv_within I I'' (g ∘ f) s p = bundle_mfderiv_within I' I'' g u (bundle_mfderiv_within I I' f s p) := begin simp [bundle_mfderiv_within], rw mfderiv_within_comp p.1 hg hf h hps, refl end lemma bundle_mfderiv_comp_at (p : tangent_bundle I M) (hg : mdifferentiable_at I' I'' g (f p.1)) (hf : mdifferentiable_at I I' f p.1) : bundle_mfderiv I I'' (g ∘ f) p = bundle_mfderiv I' I'' g (bundle_mfderiv I I' f p) := begin rcases p with ⟨x, v⟩, simp [bundle_mfderiv], rw mfderiv_comp x hg hf, refl end lemma bundle_mfderiv_comp (hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) : bundle_mfderiv I I'' (g ∘ f) = (bundle_mfderiv I' I'' g) ∘ (bundle_mfderiv I I' f) := by { ext p : 1, exact bundle_mfderiv_comp_at _ (hg _) (hf _) } end derivatives_properties section specific_functions /-! ### Differentiability of specific functions -/ variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {H : Type*} [topological_space H] (I : model_with_corners π•œ E H) {M : Type*} [topological_space M] [manifold H M] [smooth_manifold_with_corners I M] {s : set M} {x : M} section id /-! #### Identity -/ lemma has_mfderiv_at_id (x : M) : has_mfderiv_at I I (@_root_.id M) x (continuous_linear_map.id : tangent_space I x β†’L[π•œ] tangent_space I x) := begin refine ⟨continuous_id.continuous_at, _⟩, have : βˆ€αΆ  y in nhds_within ((ext_chart_at I x).to_fun x) (range (I.to_fun)), ((ext_chart_at I x).to_fun ∘ (ext_chart_at I x).inv_fun) y = id y, { apply filter.mem_sets_of_superset (ext_chart_at_target_mem_nhds_within I x), assume y hy, simp [(ext_chart_at I x).right_inv hy] }, apply has_fderiv_within_at.congr_of_mem_nhds_within (has_fderiv_within_at_id _ _) this, simp [(ext_chart_at I x).left_inv, mem_ext_chart_source I x] end theorem has_mfderiv_within_at_id (s : set M) (x : M) : has_mfderiv_within_at I I (@_root_.id M) s x (continuous_linear_map.id : tangent_space I x β†’L[π•œ] tangent_space I x) := (has_mfderiv_at_id I x).has_mfderiv_within_at lemma mdifferentiable_at_id : mdifferentiable_at I I (@_root_.id M) x := (has_mfderiv_at_id I x).mdifferentiable_at lemma mdifferentiable_within_at_id : mdifferentiable_within_at I I (@_root_.id M) s x := (mdifferentiable_at_id I).mdifferentiable_within_at lemma mdifferentiable_id : mdifferentiable I I (@_root_.id M) := Ξ»x, mdifferentiable_at_id I lemma mdifferentiable_on_id : mdifferentiable_on I I (@_root_.id M) s := (mdifferentiable_id I).mdifferentiable_on @[simp] lemma mfderiv_id : mfderiv I I (@_root_.id M) x = (continuous_linear_map.id : tangent_space I x β†’L[π•œ] tangent_space I x) := has_mfderiv_at.mfderiv (has_mfderiv_at_id I x) lemma mfderiv_within_id (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I (@_root_.id M) s x = (continuous_linear_map.id : tangent_space I x β†’L[π•œ] tangent_space I x) := begin rw mdifferentiable.mfderiv_within (mdifferentiable_at_id I) hxs, exact mfderiv_id I end end id section const /-! #### Constants -/ variables {E' : Type*} [normed_group E'] [normed_space π•œ E'] {H' : Type*} [topological_space H'] (I' : model_with_corners π•œ E' H') {M' : Type*} [topological_space M'] [manifold H' M'] [smooth_manifold_with_corners I' M'] {c : M'} lemma has_mfderiv_at_const (c : M') (x : M) : has_mfderiv_at I I' (Ξ»y : M, c) x (continuous_linear_map.zero : tangent_space I x β†’L[π•œ] tangent_space I' c) := begin refine ⟨continuous_const.continuous_at, _⟩, have : (ext_chart_at I' c).to_fun ∘ (Ξ» (y : M), c) ∘ (ext_chart_at I x).inv_fun = (Ξ»y, (ext_chart_at I' c).to_fun c) := rfl, rw [written_in_ext_chart_at, this], apply has_fderiv_within_at_const end theorem has_mfderiv_within_at_const (c : M') (s : set M) (x : M) : has_mfderiv_within_at I I' (Ξ»y : M, c) s x (continuous_linear_map.zero : tangent_space I x β†’L[π•œ] tangent_space I' c) := (has_mfderiv_at_const I I' c x).has_mfderiv_within_at lemma mdifferentiable_at_const : mdifferentiable_at I I' (Ξ»y : M, c) x := (has_mfderiv_at_const I I' c x).mdifferentiable_at lemma mdifferentiable_within_at_const : mdifferentiable_within_at I I' (Ξ»y : M, c) s x := (mdifferentiable_at_const I I').mdifferentiable_within_at lemma mdifferentiable_const : mdifferentiable I I' (Ξ»y : M, c) := Ξ»x, mdifferentiable_at_const I I' lemma mdifferentiable_on_const : mdifferentiable_on I I' (Ξ»y : M, c) s := (mdifferentiable_const I I').mdifferentiable_on @[simp] lemma mfderiv_const : mfderiv I I' (Ξ»y : M, c) x = (continuous_linear_map.zero : tangent_space I x β†’L[π•œ] tangent_space I' c) := has_mfderiv_at.mfderiv (has_mfderiv_at_const I I' c x) lemma mfderiv_within_const (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' (Ξ»y : M, c) s x = (continuous_linear_map.zero : tangent_space I x β†’L[π•œ] tangent_space I' c) := begin rw mdifferentiable.mfderiv_within (mdifferentiable_at_const I I') hxs, { exact mfderiv_const I I' }, { apply_instance } end end const section model_with_corners /-! #### Model with corners -/ lemma model_with_corners_mdifferentiable_on_to_fun : mdifferentiable I (model_with_corners_self π•œ E) I.to_fun := begin simp only [mdifferentiable, mdifferentiable_at, written_in_ext_chart_at, ext_chart_at, local_equiv.refl_trans, local_equiv.refl_to_fun, model_with_corners_self_local_equiv, chart_at_model_space_eq, local_homeomorph.refl_local_equiv, function.comp.left_id], assume x, refine ⟨I.continuous_to_fun.continuous_at, _⟩, have : differentiable_within_at π•œ id (range I.to_fun) (I.to_fun x) := differentiable_at_id.differentiable_within_at, apply this.congr, { simp [model_with_corners_right_inv] {contextual := tt} }, { simp [model_with_corners_left_inv] } end lemma model_with_corners_mdifferentiable_on_inv_fun : mdifferentiable_on (model_with_corners_self π•œ E) I I.inv_fun (range I.to_fun) := begin simp only [mdifferentiable_on, -mem_range, mdifferentiable_within_at, written_in_ext_chart_at, ext_chart_at, local_equiv.refl_trans, local_equiv.refl_to_fun, preimage_id, id.def, inter_univ, model_with_corners_self_local_equiv, local_equiv.refl_inv_fun, range_id, function.comp.right_id, chart_at_model_space_eq, local_homeomorph.refl_local_equiv], assume x hx, refine ⟨I.continuous_inv_fun.continuous_at.continuous_within_at, _⟩, have : differentiable_within_at π•œ id (range I.to_fun) x := differentiable_at_id.differentiable_within_at, apply this.congr, { simp [model_with_corners_right_inv] {contextual := tt} }, { simp [model_with_corners_right_inv, hx] } end end model_with_corners section charts variable {e : local_homeomorph M H} lemma mdifferentiable_at_atlas_to_fun (h : e ∈ atlas H M) {x : M} (hx : x ∈ e.source) : mdifferentiable_at I I e.to_fun x := begin refine ⟨(e.continuous_to_fun x hx).continuous_at (mem_nhds_sets e.open_source hx), _⟩, have zero_one : ((0 : β„•) : with_top β„•) < ⊀, by simp, have mem : I.to_fun ((chart_at H x).to_fun x) ∈ I.inv_fun ⁻¹' ((chart_at H x).symm ≫ₕ e).source ∩ range I.to_fun, { simp only [mem_preimage, mem_inter_eq, model_with_corners_left_inv, mem_range_self, local_homeomorph.trans_source, local_homeomorph.symm_source, local_homeomorph.symm_to_fun, and_true], split, { exact local_equiv.map_source _ (mem_chart_source _ _) }, { rw local_equiv.left_inv _ (mem_chart_source _ _), exact hx } }, have : (chart_at H x).symm.trans e ∈ times_cont_diff_groupoid ⊀ I := has_groupoid.compatible _ (chart_mem_atlas H x) h, have A : times_cont_diff_on π•œ ⊀ (I.to_fun ∘ ((chart_at H x).symm.trans e).to_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' ((chart_at H x).symm.trans e).source ∩ range I.to_fun) := this.1, have B := A.differentiable_on (by simp) (I.to_fun ((chart_at H x).to_fun x)) mem, simp only [local_homeomorph.trans_to_fun, local_homeomorph.symm_to_fun] at B, rw [inter_comm, differentiable_within_at_inter (mem_nhds_sets (I.continuous_inv_fun _ (local_homeomorph.open_source _)) mem.1)] at B, simpa [written_in_ext_chart_at, ext_chart_at] end lemma mdifferentiable_on_atlas_to_fun (h : e ∈ atlas H M) : mdifferentiable_on I I e.to_fun e.source := Ξ»x hx, (mdifferentiable_at_atlas_to_fun I h hx).mdifferentiable_within_at lemma mdifferentiable_at_atlas_inv_fun (h : e ∈ atlas H M) {x : H} (hx : x ∈ e.target) : mdifferentiable_at I I e.inv_fun x := begin refine ⟨(e.continuous_inv_fun x hx).continuous_at (mem_nhds_sets e.open_target hx), _⟩, have zero_one : ((0 : β„•) : with_top β„•) < ⊀, by simp, have mem : I.to_fun x ∈ I.inv_fun ⁻¹' (e.symm ≫ₕ chart_at H (e.inv_fun x)).source ∩ range (I.to_fun), by simp only [local_homeomorph.trans_source, local_homeomorph.symm_source, mem_preimage, mem_inter_eq, model_with_corners_left_inv, preimage_inter, and_true, hx, true_and, local_homeomorph.symm_to_fun, mem_range_self, mem_chart_source], have : e.symm.trans (chart_at H (e.inv_fun x)) ∈ times_cont_diff_groupoid ⊀ I := has_groupoid.compatible _ h (chart_mem_atlas H _), have A : times_cont_diff_on π•œ ⊀ (I.to_fun ∘ (e.symm.trans (chart_at H (e.inv_fun x))).to_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' (e.symm.trans (chart_at H (e.inv_fun x))).source ∩ range I.to_fun) := this.1, have B := A.differentiable_on (by simp) (I.to_fun x) mem, simp only [local_homeomorph.trans_to_fun, local_homeomorph.symm_to_fun] at B, rw [inter_comm, differentiable_within_at_inter (mem_nhds_sets (I.continuous_inv_fun _ (local_homeomorph.open_source _)) mem.1)] at B, simpa [written_in_ext_chart_at, ext_chart_at] end lemma mdifferentiable_on_atlas_inv_fun (h : e ∈ atlas H M) : mdifferentiable_on I I e.inv_fun e.target := Ξ»x hx, (mdifferentiable_at_atlas_inv_fun I h hx).mdifferentiable_within_at lemma mdifferentiable_of_mem_atlas (h : e ∈ atlas H M) : e.mdifferentiable I I := ⟨mdifferentiable_on_atlas_to_fun I h, mdifferentiable_on_atlas_inv_fun I h⟩ lemma mdifferentiable_chart (x : M) : (chart_at H x).mdifferentiable I I := mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _) /-- The derivative of the chart at a base point is the chart of the tangent bundle. -/ lemma bundle_mfderiv_chart_to_fun {p q : tangent_bundle I M} (h : q.1 ∈ (chart_at H p.1).source) : bundle_mfderiv I I (chart_at H p.1).to_fun q = (chart_at (H Γ— E) p).to_fun q := begin dsimp [bundle_mfderiv], rw mdifferentiable_at.mfderiv, { refl }, { exact mdifferentiable_at_atlas_to_fun _ (chart_mem_atlas _ _) h } end /-- The derivative of the inverse of the chart at a base point is the inverse of the chart of the tangent bundle. -/ lemma bundle_mfderiv_chart_inv_fun {p : tangent_bundle I M} {q : H Γ— E} (h : q.1 ∈ (chart_at H p.1).target) : bundle_mfderiv I I (chart_at H p.1).inv_fun q = (chart_at (H Γ— E) p).inv_fun q := begin dsimp only [bundle_mfderiv], rw mdifferentiable_at.mfderiv (mdifferentiable_at_atlas_inv_fun _ (chart_mem_atlas _ _) h), -- a trivial instance is needed after the rewrite, handle it right now. rotate, { apply_instance }, dsimp [written_in_ext_chart_at, ext_chart_at, chart_at, manifold.chart_at, basic_smooth_bundle_core.chart, basic_smooth_bundle_core.to_topological_fiber_bundle_core, topological_fiber_bundle_core.local_triv, topological_fiber_bundle_core.local_triv', tangent_bundle_core], rw local_equiv.right_inv, exact h end end charts end specific_functions section mfderiv_fderiv /-! ### Relations between vector space derivative and manifold derivative The manifold derivative `mfderiv`, when considered on the model vector space with its trivial manifold structure, coincides with the usual Frechet derivative `fderiv`. In this section, we prove this and related statements. -/ variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {E' : Type*} [normed_group E'] [normed_space π•œ E'] {f : E β†’ E'} {s : set E} {x : E} lemma unique_mdiff_within_at_iff_unique_diff_within_at : unique_mdiff_within_at (model_with_corners_self π•œ E) s x ↔ unique_diff_within_at π•œ s x := by simp [unique_mdiff_within_at] lemma unique_mdiff_on_iff_unique_diff_on : unique_mdiff_on (model_with_corners_self π•œ E) s ↔ unique_diff_on π•œ s := by simp [unique_mdiff_on, unique_diff_on, unique_mdiff_within_at_iff_unique_diff_within_at] @[simp] lemma written_in_ext_chart_model_space : written_in_ext_chart_at (model_with_corners_self π•œ E) (model_with_corners_self π•œ E') x f = f := by { ext y, simp [written_in_ext_chart_at] } /-- For maps between vector spaces, mdifferentiable_within_at and fdifferentiable_within_at coincide -/ theorem mdifferentiable_within_at_iff_differentiable_within_at : mdifferentiable_within_at (model_with_corners_self π•œ E) (model_with_corners_self π•œ E') f s x ↔ differentiable_within_at π•œ f s x := begin simp [mdifferentiable_within_at], exact ⟨λH, H.2, Ξ»H, ⟨H.continuous_within_at, H⟩⟩ end /-- For maps between vector spaces, mdifferentiable_at and differentiable_at coincide -/ theorem mdifferentiable_at_iff_differentiable_at : mdifferentiable_at (model_with_corners_self π•œ E) (model_with_corners_self π•œ E') f x ↔ differentiable_at π•œ f x := begin simp [mdifferentiable_at, differentiable_within_at_univ], exact ⟨λH, H.2, Ξ»H, ⟨H.continuous_at, H⟩⟩ end /-- For maps between vector spaces, mdifferentiable_on and differentiable_on coincide -/ theorem mdifferentiable_on_iff_differentiable_on : mdifferentiable_on (model_with_corners_self π•œ E) (model_with_corners_self π•œ E') f s ↔ differentiable_on π•œ f s := by simp [mdifferentiable_on, differentiable_on, mdifferentiable_within_at_iff_differentiable_within_at] /-- For maps between vector spaces, mdifferentiable and differentiable coincide -/ theorem mdifferentiable_iff_differentiable : mdifferentiable (model_with_corners_self π•œ E) (model_with_corners_self π•œ E') f ↔ differentiable π•œ f := by simp [mdifferentiable, differentiable, mdifferentiable_at_iff_differentiable_at] /-- For maps between vector spaces, mfderiv_within and fderiv_within coincide -/ theorem mfderiv_within_eq_fderiv_within : mfderiv_within (model_with_corners_self π•œ E) (model_with_corners_self π•œ E') f s x = fderiv_within π•œ f s x := begin by_cases h : mdifferentiable_within_at (model_with_corners_self π•œ E) (model_with_corners_self π•œ E') f s x, { simp [mfderiv_within, h] }, { simp [mfderiv_within, h], rw [mdifferentiable_within_at_iff_differentiable_within_at, differentiable_within_at] at h, change Β¬(βˆƒ(f' : tangent_space (model_with_corners_self π•œ E) x β†’L[π•œ] tangent_space (model_with_corners_self π•œ E') (f x)), has_fderiv_within_at f f' s x) at h, simp [fderiv_within, h], refl } end /-- For maps between vector spaces, mfderiv and fderiv coincide -/ theorem mfderiv_eq_fderiv : mfderiv (model_with_corners_self π•œ E) (model_with_corners_self π•œ E') f x = fderiv π•œ f x := begin rw [← mfderiv_within_univ, ← fderiv_within_univ], exact mfderiv_within_eq_fderiv_within end end mfderiv_fderiv /-! ### Differentiable local homeomorphisms -/ namespace local_homeomorph.mdifferentiable variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {H : Type*} [topological_space H] {I : model_with_corners π•œ E H} {M : Type*} [topological_space M] [manifold H M] {E' : Type*} [normed_group E'] [normed_space π•œ E'] {H' : Type*} [topological_space H'] {I' : model_with_corners π•œ E' H'} {M' : Type*} [topological_space M'] [manifold H' M'] {E'' : Type*} [normed_group E''] [normed_space π•œ E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners π•œ E'' H''} {M'' : Type*} [topological_space M''] [manifold H'' M''] {e : local_homeomorph M M'} (he : e.mdifferentiable I I') {e' : local_homeomorph M' M''} include he lemma symm : e.symm.mdifferentiable I' I := ⟨he.2, he.1⟩ lemma mdifferentiable_at_to_fun {x : M} (hx : x ∈ e.source) : mdifferentiable_at I I' e.to_fun x := (he.1 x hx).mdifferentiable_at (mem_nhds_sets e.open_source hx) lemma mdifferentiable_at_inv_fun {x : M'} (hx : x ∈ e.target) : mdifferentiable_at I' I e.inv_fun x := (he.2 x hx).mdifferentiable_at (mem_nhds_sets e.open_target hx) variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M'] [smooth_manifold_with_corners I'' M''] lemma inv_fun_to_fun_deriv {x : M} (hx : x ∈ e.source) : (mfderiv I' I e.inv_fun (e.to_fun x)).comp (mfderiv I I' e.to_fun x) = continuous_linear_map.id := begin have : (mfderiv I I (e.inv_fun ∘ e.to_fun) x) = (mfderiv I' I e.inv_fun (e.to_fun x)).comp (mfderiv I I' e.to_fun x) := mfderiv_comp x (he.mdifferentiable_at_inv_fun (e.map_source hx)) (he.mdifferentiable_at_to_fun hx), rw ← this, have : mfderiv I I (_root_.id : M β†’ M) x = continuous_linear_map.id := mfderiv_id I, rw ← this, apply mfderiv_congr_of_mem_nhds, have : e.source ∈ 𝓝 x := mem_nhds_sets e.open_source hx, apply filter.mem_sets_of_superset this, assume p hp, simp [e.left_inv, hp] end lemma to_fun_inv_fun_deriv {x : M'} (hx : x ∈ e.target) : (mfderiv I I' e.to_fun (e.inv_fun x)).comp (mfderiv I' I e.inv_fun x) = continuous_linear_map.id := he.symm.inv_fun_to_fun_deriv hx set_option class.instance_max_depth 60 /-- The derivative of a differentiable local homeomorphism, as a continuous linear equivalence between the tangent spaces at `x` and `e.to_fun x`. -/ protected def mfderiv {x : M} (hx : x ∈ e.source) : tangent_space I x ≃L[π•œ] tangent_space I' (e.to_fun x) := { inv_fun := (mfderiv I' I e.inv_fun (e.to_fun x)).to_fun, continuous_to_fun := (mfderiv I I' e.to_fun x).cont, continuous_inv_fun := (mfderiv I' I e.inv_fun (e.to_fun x)).cont, left_inv := Ξ»y, begin have : (continuous_linear_map.id : tangent_space I x β†’L[π•œ] tangent_space I x) y = y := rfl, conv_rhs { rw [← this, ← he.inv_fun_to_fun_deriv hx] }, refl end, right_inv := Ξ»y, begin have : (continuous_linear_map.id : tangent_space I' (e.to_fun x) β†’L[π•œ] tangent_space I' (e.to_fun x)) y = y := rfl, conv_rhs { rw [← this, ← he.to_fun_inv_fun_deriv (e.map_source hx)] }, rw e.to_local_equiv.left_inv hx, refl end, .. mfderiv I I' e.to_fun x } set_option class.instance_max_depth 100 lemma range_mfderiv_eq_univ {x : M} (hx : x ∈ e.source) : range (mfderiv I I' e.to_fun x) = univ := (he.mfderiv hx).to_linear_equiv.to_equiv.range_eq_univ lemma trans (he': e'.mdifferentiable I' I'') : (e.trans e').mdifferentiable I I'' := begin split, { assume x hx, simp [local_equiv.trans_source] at hx, exact ((he'.mdifferentiable_at_to_fun hx.2).comp _ (he.mdifferentiable_at_to_fun hx.1)).mdifferentiable_within_at }, { assume x hx, simp [local_equiv.trans_target] at hx, exact ((he.mdifferentiable_at_inv_fun hx.2).comp _ (he'.mdifferentiable_at_inv_fun hx.1)).mdifferentiable_within_at } end end local_homeomorph.mdifferentiable /-! ### Unique derivative sets in manifolds -/ section unique_mdiff variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {H : Type*} [topological_space H] {I : model_with_corners π•œ E H} {M : Type*} [topological_space M] [manifold H M] [smooth_manifold_with_corners I M] {E' : Type*} [normed_group E'] [normed_space π•œ E'] {H' : Type*} [topological_space H'] {I' : model_with_corners π•œ E' H'} {M' : Type*} [topological_space M'] [manifold H' M'] {s : set M} /-- If a set has the unique differential property, then its image under a local diffeomorphism also has the unique differential property. -/ lemma unique_mdiff_on.unique_mdiff_on_preimage [smooth_manifold_with_corners I' M'] (hs : unique_mdiff_on I s) {e : local_homeomorph M M'} (he : e.mdifferentiable I I') : unique_mdiff_on I' (e.target ∩ e.inv_fun ⁻¹' s) := begin /- Start from a point `x` in the image, and let `z` be its preimage. Then the unique derivative property at `x` is expressed through `ext_chart_at I' x`, and the unique derivative property at `z` is expressed through `ext_chart_at I z`. We will argue that the composition of these two charts with `e` is a local diffeomorphism in vector spaces, and therefore preserves the unique differential property thanks to lemma `has_fderiv_within_at.unique_diff_within_at`, saying that a differentiable function with onto derivative preserves the unique derivative property.-/ assume x hx, let z := e.inv_fun x, have z_source : z ∈ e.source, by simp [hx.1, local_equiv.map_target], have zx : e.to_fun z = x, by simp [z, hx.1], let F := ext_chart_at I z, -- the unique derivative property at `z` is expressed through its preferred chart, that we call `F`. have B : unique_diff_within_at π•œ (F.inv_fun ⁻¹' (s ∩ (e.source ∩ e.to_fun ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F.to_fun z), { have : unique_mdiff_within_at I s z := hs _ hx.2, have S : e.source ∩ e.to_fun ⁻¹' ((ext_chart_at I' x).source) ∈ 𝓝 z, { apply mem_nhds_sets, apply e.continuous_to_fun.preimage_open_of_open e.open_source (ext_chart_at_open_source I' x), simp [z_source, zx] }, have := this.inter S, rw [unique_mdiff_within_at_iff] at this, exact this }, -- denote by `G` the change of coordinate, i.e., the composition of the two extended charts and -- of `e` let G := F.symm ≫ e.to_local_equiv ≫ (ext_chart_at I' x), -- `G` is differentiable have M : ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).mdifferentiable I I', { have A := mdifferentiable_of_mem_atlas I (chart_mem_atlas H z), have B := mdifferentiable_of_mem_atlas I' (chart_mem_atlas H' x), exact A.symm.trans (he.trans B) }, have Mmem : (chart_at H z).to_fun z ∈ ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).source, by simp [local_equiv.trans_source, local_equiv.map_source, z_source, zx], have A : differentiable_within_at π•œ G.to_fun (range I.to_fun) (F.to_fun z), { refine (M.mdifferentiable_at_to_fun Mmem).2.congr (Ξ»p hp, _) _; simp [G, written_in_ext_chart_at, ext_chart_at, F] }, -- let `G'` be its derivative let G' := fderiv_within π•œ G.to_fun (range I.to_fun) (F.to_fun z), have D₁ : has_fderiv_within_at G.to_fun G' (range I.to_fun) (F.to_fun z) := A.has_fderiv_within_at, have Dβ‚‚ : has_fderiv_within_at G.to_fun G' (F.inv_fun ⁻¹' (s ∩ (e.source ∩ e.to_fun ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F.to_fun z), { apply D₁.mono, refine subset.trans (inter_subset_right _ _) _, simp [F, ext_chart_at, local_equiv.trans_target] }, -- The derivative `G'` is onto, as it is the derivative of a local diffeomorphism, the composition -- of the two charts and of `e`. have C₁ : range (G' : E β†’ E') = univ, { have : G' = mfderiv I I' ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).to_fun ((chart_at H z).to_fun z), by { rw (M.mdifferentiable_at_to_fun Mmem).mfderiv, refl }, rw this, exact M.range_mfderiv_eq_univ Mmem }, have Cβ‚‚ : closure (range (G' : E β†’ E')) = univ, by rw [C₁, closure_univ], -- key step: thanks to what we have proved about it, `G` preserves the unique derivative property have key : unique_diff_within_at π•œ (G.to_fun '' (F.inv_fun ⁻¹' (s ∩ (e.source ∩ e.to_fun ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target)) (G.to_fun (F.to_fun z)) := Dβ‚‚.unique_diff_within_at B Cβ‚‚, have : G.to_fun (F.to_fun z) = (ext_chart_at I' x).to_fun x, by { dsimp [G, F], simp [hx.1] }, rw this at key, apply key.mono, show G.to_fun '' (F.inv_fun ⁻¹' (s ∩ (e.source ∩ e.to_fun ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) βŠ† (ext_chart_at I' x).inv_fun ⁻¹' e.target ∩ (ext_chart_at I' x).inv_fun ⁻¹' (e.inv_fun ⁻¹' s) ∩ range (I'.to_fun), rw image_subset_iff, rintros p ⟨⟨hp₁, ⟨hpβ‚‚, hpβ‚„βŸ©βŸ©, hpβ‚ƒβŸ©, simp [G, local_equiv.map_source, hpβ‚‚, hp₁, mem_preimage.1 hpβ‚„, -mem_range, mem_range_self], exact mem_range_self _ end /-- If a set in a manifold has the unique derivative property, then its pullback by any extended chart, in the vector space, also has the unique derivative property. -/ lemma unique_mdiff_on.unique_diff_on (hs : unique_mdiff_on I s) (x : M) : unique_diff_on π•œ ((ext_chart_at I x).target ∩ ((ext_chart_at I x).inv_fun ⁻¹' s)) := begin -- this is just a reformulation of `unique_mdiff_on.unique_mdiff_on_preimage`, using as `e` -- the local chart at `x`. assume z hz, simp [ext_chart_at, local_equiv.trans_target, -mem_range] at hz, have : (chart_at H x).mdifferentiable I I := mdifferentiable_chart _ _, have T := (hs.unique_mdiff_on_preimage this) (I.inv_fun z), simp only [ext_chart_at, (hz.left).left, (hz.left).right, hz.right, local_equiv.trans_target, unique_mdiff_on, unique_mdiff_within_at, local_equiv.refl_trans, forall_prop_of_true, model_with_corners_target, mem_inter_eq, preimage_inter, mem_preimage, chart_at_model_space_eq, local_homeomorph.refl_local_equiv, and_self, model_with_corners_right_inv, local_equiv.trans_inv_fun] at ⊒ T, convert T using 1, rw @preimage_comp _ _ _ _ (chart_at H x).inv_fun, -- it remains to show that `(a ∩ b) ∩ c` = `(b ∩ c) ∩ a`, which finish can do but very slowly ext p, split; { assume hp, simp at hp, simp [hp] } end /-- When considering functions between manifolds, this statement shows up often. It entails the unique differential of the pullback in extended charts of the set where the function can be read in the charts. -/ lemma unique_mdiff_on.unique_diff_on_inter_preimage (hs : unique_mdiff_on I s) (x : M) (y : M') {f : M β†’ M'} (hf : continuous_on f s) : unique_diff_on π•œ ((ext_chart_at I x).target ∩ ((ext_chart_at I x).inv_fun ⁻¹' (s ∩ f⁻¹' (ext_chart_at I' y).source))) := begin have : unique_mdiff_on I (s ∩ f ⁻¹' (ext_chart_at I' y).source), { assume z hz, apply (hs z hz.1).inter', apply (hf z hz.1).preimage_mem_nhds_within, exact mem_nhds_sets (ext_chart_at_open_source I' y) hz.2 }, exact this.unique_diff_on _ end variables {F : Type*} [normed_group F] [normed_space π•œ F] (Z : basic_smooth_bundle_core I M F) /-- In a smooth fiber bundle constructed from core, the preimage under the projection of a set with unique differential in the basis also has unique differential. -/ lemma unique_mdiff_on.smooth_bundle_preimage (hs : unique_mdiff_on I s) : unique_mdiff_on (I.prod (model_with_corners_self π•œ F)) (Z.to_topological_fiber_bundle_core.proj ⁻¹' s) := begin /- Using a chart (and the fact that unique differentiability is invariant under charts), we reduce the situation to the model space, where we can use the fact that products respect unique differentiability. -/ assume p hp, replace hp : p.fst ∈ s, by simpa using hp, let eβ‚€ := chart_at H p.1, let e := chart_at (H Γ— F) p, -- It suffices to prove unique differentiability in a chart suffices h : unique_mdiff_on (I.prod (model_with_corners_self π•œ F)) (e.target ∩ e.inv_fun⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s)), { have A : unique_mdiff_on (I.prod (model_with_corners_self π•œ F)) (e.symm.target ∩ e.symm.inv_fun ⁻¹' (e.target ∩ e.inv_fun⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s))), { apply h.unique_mdiff_on_preimage, exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)).symm, apply_instance }, have : p ∈ e.symm.target ∩ e.symm.inv_fun ⁻¹' (e.target ∩ e.inv_fun⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s)), by simp [e, hp], apply (A _ this).mono, assume q hq, simp [e, local_equiv.left_inv _ hq.1] at hq, simp [hq] }, -- rewrite the relevant set in the chart as a direct product have : (Ξ» (p : E Γ— F), (I.inv_fun p.1, p.snd)) ⁻¹' e.target ∩ (Ξ» (p : E Γ— F), (I.inv_fun p.1, p.snd)) ⁻¹' (e.inv_fun ⁻¹' (prod.fst ⁻¹' s)) ∩ range (Ξ» (p : H Γ— F), (I.to_fun p.1, p.snd)) = set.prod (I.inv_fun ⁻¹' (eβ‚€.target ∩ eβ‚€.inv_fun⁻¹' s) ∩ range I.to_fun) univ, { ext q, split; { assume hq, simp [-mem_range, mem_range_self, prod_range_univ_eq.symm] at hq, simp [-mem_range, mem_range_self, hq, prod_range_univ_eq.symm] } }, assume q hq, replace hq : q.1 ∈ (chart_at H p.1).target ∧ (chart_at H p.1).inv_fun q.1 ∈ s, by simpa using hq, simp only [unique_mdiff_within_at, ext_chart_at, model_with_corners.prod, local_equiv.refl_trans, local_equiv.refl_to_fun, topological_fiber_bundle_core.proj, id.def, range_id, model_with_corners_self_local_equiv, local_equiv.refl_inv_fun, preimage_inter, chart_at_model_space_eq, local_homeomorph.refl_local_equiv, this], -- apply unique differentiability of products to conclude apply unique_diff_on.prod _ is_open_univ.unique_diff_on, { simp [-mem_range, mem_range_self, hq] }, { assume x hx, have A : unique_mdiff_on I (eβ‚€.target ∩ eβ‚€.inv_fun⁻¹' s), { apply hs.unique_mdiff_on_preimage, exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)), apply_instance }, simp [unique_mdiff_on, unique_mdiff_within_at, ext_chart_at] at A, have B := A (I.inv_fun x) hx.1.1 hx.1.2, rwa [← preimage_inter, model_with_corners_right_inv _ hx.2] at B } end lemma unique_mdiff_on.tangent_bundle_proj_preimage (hs : unique_mdiff_on I s): unique_mdiff_on I.tangent ((tangent_bundle.proj I M) ⁻¹' s) := hs.smooth_bundle_preimage _ end unique_mdiff
e64a42cd36bf3a7a467cc7779c5a074f6e533d8e
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/endomorphism.lean
fffb8c1fac69225a649ec5a9c1cde51a033461ba
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
2,824
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Scott Morrison, Simon Hudon Definition and basic properties of endomorphisms and automorphisms of an object in a category. -/ import category_theory.category import category_theory.groupoid import data.equiv.mul_add universes v v' u u' namespace category_theory /-- Endomorphisms of an object in a category. Arguments order in multiplication agrees with `function.comp`, not with `category.comp`. -/ def End {C : Type u} [π’ž_struct : category_struct.{v} C] (X : C) := X ⟢ X namespace End section struct variables {C : Type u} [π’ž_struct : category_struct.{v} C] (X : C) include π’ž_struct instance has_one : has_one (End X) := βŸ¨πŸ™ X⟩ /-- Multiplication of endomorphisms agrees with `function.comp`, not `category_struct.comp`. -/ instance has_mul : has_mul (End X) := ⟨λ x y, y ≫ x⟩ variable {X} @[simp] lemma one_def : (1 : End X) = πŸ™ X := rfl @[simp] lemma mul_def (xs ys : End X) : xs * ys = ys ≫ xs := rfl end struct /-- Endomorphisms of an object form a monoid -/ instance monoid {C : Type u} [category.{v} C] {X : C} : monoid (End X) := { mul_one := category.id_comp, one_mul := category.comp_id, mul_assoc := Ξ» x y z, (category.assoc z y x).symm, ..End.has_mul X, ..End.has_one X } /-- In a groupoid, endomorphisms form a group -/ instance group {C : Type u} [groupoid.{v} C] (X : C) : group (End X) := { mul_left_inv := groupoid.comp_inv, inv := groupoid.inv, ..End.monoid } end End variables {C : Type u} [category.{v} C] (X : C) def Aut (X : C) := X β‰… X attribute [ext Aut] iso.ext namespace Aut instance : group (Aut X) := by refine { one := iso.refl X, inv := iso.symm, mul := flip iso.trans, .. } ; simp [flip, (*), has_one.one] /-- Units in the monoid of endomorphisms of an object are (multiplicatively) equivalent to automorphisms of that object. -/ def units_End_equiv_Aut : units (End X) ≃* Aut X := { to_fun := Ξ» f, ⟨f.1, f.2, f.4, f.3⟩, inv_fun := Ξ» f, ⟨f.1, f.2, f.4, f.3⟩, left_inv := Ξ» ⟨f₁, fβ‚‚, f₃, fβ‚„βŸ©, rfl, right_inv := Ξ» ⟨f₁, fβ‚‚, f₃, fβ‚„βŸ©, rfl, map_mul' := Ξ» f g, by rcases f; rcases g; refl } end Aut namespace functor variables {D : Type u'} [category.{v'} D] (f : C β₯€ D) (X) /-- `f.map` as a monoid hom between endomorphism monoids. -/ def map_End : End X β†’* End (f.obj X) := { to_fun := functor.map f, map_mul' := Ξ» x y, f.map_comp y x, map_one' := f.map_id X } /-- `f.map_iso` as a group hom between automorphism groups. -/ def map_Aut : Aut X β†’* Aut (f.obj X) := { to_fun := f.map_iso, map_mul' := Ξ» x y, f.map_iso_trans y x, map_one' := f.map_iso_refl X } end functor end category_theory
1db224692451509207556508664717e5d4248e76
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/slow/tactic1.lean
4aa9f5fe6fcf48cb696743abe82ead836e8b1be6
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
1,329
lean
import Int. definition double {A : Type} (f : A -> A) : A -> A := fun x, f (f x). definition big {A : Type} (f : A -> A) : A -> A := (double (double (double (double (double (double (double f))))))). (* -- Tactic for trying to prove goal using Reflexivity, Congruence and available assumptions local congr_tac = Repeat(OrElse(apply_tac("refl"), apply_tac("congr"), assumption_tac())) -- Create an eager tactic that only tries to prove goal after unfolding everything eager_tac = Then(-- unfold homogeneous equality Try(unfold_tac("eq")), -- keep unfolding defintions above and beta-reducing Repeat(unfold_tac() .. Repeat(beta_tac())), congr_tac) -- The 'lazy' version tries first to prove without unfolding anything lazy_tac = OrElse(Then(Try(unfold_tac("eq")), congr_tac, now_tac()), eager_tac) *) exit -- temporarily disable the follwoing tests theorem T1 (a b : Int) (f : Int -> Int) (H : a = b) : (big f a) = (big f b). eager_tac. done. theorem T2 (a b : Int) (f : Int -> Int) (H : a = b) : (big f a) = (big f b). lazy_tac. done. theorem T3 (a b : Int) (f : Int -> Int) (H : a = b) : (big f a) = ((double (double (double (double (double (double (double f))))))) b). lazy_tac. done.
74442bd9ac966fa93e6f13fccea51a51e6e82828
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/data/real/liouville.lean
71c6e558a4a9c48a5683387c3b5f94e79c98aba1
[ "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,343
lean
/- Copyright (c) 2020 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jujian Zhang -/ import analysis.calculus.mean_value import data.polynomial.denoms_clearable import data.real.irrational import ring_theory.algebraic import topology.algebra.polynomial /-! # Liouville's theorem This file contains a proof of Liouville's theorem stating that all Liouville numbers are transcendental. To obtain this result, there is first a proof that Liouville numbers are irrational and two technical lemmas. These lemmas exploit the fact that a polynomial with integer coefficients takes integer values at integers. When evaluating at a rational number, we can clear denominators and obtain precise inequalities that ultimately allow us to prove transcendence of Liouville numbers. -/ /-- A Liouville number is a real number `x` such that for every natural number `n`, there exist `a, b ∈ β„€` with `1 < b` such that `0 < |x - a/b| < 1/bⁿ`. In the implementation, the condition `x β‰  a/b` replaces the traditional equivalent `0 < |x - a/b|`. -/ def liouville (x : ℝ) := βˆ€ n : β„•, βˆƒ a b : β„€, 1 < b ∧ x β‰  a / b ∧ abs (x - a / b) < 1 / b ^ n namespace liouville @[protected] lemma irrational {x : ℝ} (h : liouville x) : irrational x := begin -- By contradiction, `x = a / b`, with `a ∈ β„€`, `0 < b ∈ β„•` is a Liouville number, rintros ⟨⟨a, b, bN0, cop⟩, rfl⟩, -- clear up the mess of constructions of rationals change (liouville (a / b)) at h, -- Since `a / b` is a Liouville number, there are `p, q ∈ β„€`, with `q1 : 1 < q`, -- `a0 : a / b β‰  p / q` and `a1 : abs (a / b - p / q) < 1 / q ^ (b + 1)` rcases h (b + 1) with ⟨p, q, q1, a0, a1⟩, -- A few useful inequalities have qR0 : (0 : ℝ) < q := int.cast_pos.mpr (zero_lt_one.trans q1), have b0 : (b : ℝ) β‰  0 := ne_of_gt (nat.cast_pos.mpr bN0), have bq0 : (0 : ℝ) < b * q := mul_pos (nat.cast_pos.mpr bN0) qR0, -- At a1, clear denominators... replace a1 : abs (a * q - b * p) * q ^ (b + 1) < b * q, by rwa [div_sub_div _ _ b0 (ne_of_gt qR0), abs_div, div_lt_div_iff (abs_pos.mpr (ne_of_gt bq0)) (pow_pos qR0 _), abs_of_pos bq0, one_mul, -- ... and revert to integers ← int.cast_pow, ← int.cast_mul, ← int.cast_coe_nat, ← int.cast_mul, ← int.cast_mul, ← int.cast_sub, ← int.cast_abs, ← int.cast_mul, int.cast_lt] at a1, -- At a0, clear denominators... replace a0 : Β¬a * q - ↑b * p = 0, by rwa [ne.def, div_eq_div_iff b0 (ne_of_gt qR0), mul_comm ↑p, ← sub_eq_zero, -- ... and revert to integers ← int.cast_coe_nat, ← int.cast_mul, ← int.cast_mul, ← int.cast_sub, int.cast_eq_zero] at a0, -- Actually, `q` is a natural number lift q to β„• using (zero_lt_one.trans q1).le, -- Looks innocuous, but we now have an integer with non-zero absolute value: this is at -- least one away from zero. The gain here is what gets the proof going. have ap : 0 < abs (a * ↑q - ↑b * p) := abs_pos.mpr a0, -- Actually, the absolute value of an integer is a natural number lift (abs (a * ↑q - ↑b * p)) to β„• using (abs_nonneg (a * ↑q - ↑b * p)), -- At a1, revert to natural numbers rw [← int.coe_nat_mul, ← int.coe_nat_pow, ← int.coe_nat_mul, int.coe_nat_lt] at a1, -- Recall this is by contradiction: we obtained the inequality `b * q ≀ x * q ^ (b + 1)`, so -- we are done. exact not_le.mpr a1 (nat.mul_lt_mul_pow_succ (int.coe_nat_pos.mp ap) (int.coe_nat_lt.mp q1)).le, end open polynomial metric set real ring_hom /-- Let `Z, N` be types, let `R` be a metric space, let `Ξ± : R` be a point and let `j : Z β†’ N β†’ R` be a function. We aim to estimate how close we can get to `Ξ±`, while staying in the image of `j`. The points `j z a` of `R` in the image of `j` come with a "cost" equal to `d a`. As we get closer to `Ξ±` while staying in the image of `j`, we are interested in bounding the quantity `d a * dist Ξ± (j z a)` from below by a strictly positive amount `1 / A`: the intuition is that approximating well `Ξ±` with the points in the image of `j` should come at a high cost. The hypotheses on the function `f : R β†’ R` provide us with sufficient conditions to ensure our goal. The first hypothesis is that `f` is Lipschitz at `Ξ±`: this yields a bound on the distance. The second hypothesis is specific to the Liouville argument and provides the missing bound involving the cost function `d`. This lemma collects the properties used in the proof of `exists_pos_real_of_irrational_root`. It is stated in more general form than needed: in the intended application, `Z = β„€`, `N = β„•`, `R = ℝ`, `d a = (a + 1) ^ f.nat_degree`, `j z a = z / (a + 1)`, `f ∈ β„€[x]`, `Ξ±` is an irrational root of `f`, `Ξ΅` is small, `M` is a bound on the Lipschitz constant of `f` near `Ξ±`, `n` is the degree of the polynomial `f`. -/ lemma exists_one_le_pow_mul_dist {Z N R : Type*} [metric_space R] {d : N β†’ ℝ} {j : Z β†’ N β†’ R} {f : R β†’ R} {Ξ± : R} {Ξ΅ M : ℝ} -- denominators are positive (d0 : βˆ€ (a : N), 1 ≀ d a) (e0 : 0 < Ξ΅) -- function is Lipschitz at Ξ± (B : βˆ€ ⦃y : R⦄, y ∈ closed_ball Ξ± Ξ΅ β†’ dist (f Ξ±) (f y) ≀ (dist Ξ± y) * M) -- clear denominators (L : βˆ€ ⦃z : Z⦄, βˆ€ ⦃a : N⦄, j z a ∈ closed_ball Ξ± Ξ΅ β†’ 1 ≀ (d a) * dist (f Ξ±) (f (j z a))) : βˆƒ A : ℝ, 0 < A ∧ βˆ€ (z : Z), βˆ€ (a : N), 1 ≀ (d a) * (dist Ξ± (j z a) * A) := begin -- A useful inequality to keep at hand have me0 : 0 < max (1 / Ξ΅) M := lt_max_iff.mpr (or.inl (one_div_pos.mpr e0)), -- The maximum between `1 / Ξ΅` and `M` works refine ⟨max (1 / Ξ΅) M, me0, Ξ» z a, _⟩, -- First, let's deal with the easy case in which we are far away from `Ξ±` by_cases dm1 : 1 ≀ (dist Ξ± (j z a) * max (1 / Ξ΅) M), { exact one_le_mul_of_one_le_of_one_le (d0 a) dm1 }, { -- `j z a = z / (a + 1)`: we prove that this ratio is close to `Ξ±` have : j z a ∈ closed_ball Ξ± Ξ΅, { refine mem_closed_ball'.mp (le_trans _ ((one_div_le me0 e0).mpr (le_max_left _ _))), exact ((le_div_iff me0).mpr (not_le.mp dm1).le) }, -- use the "separation from `1`" (assumption `L`) for numerators, refine (L this).trans _, -- remove a common factor and use the Lipschitz assumption `B` refine mul_le_mul_of_nonneg_left ((B this).trans _) (zero_le_one.trans (d0 a)), exact mul_le_mul_of_nonneg_left (le_max_right _ M) dist_nonneg } end lemma exists_pos_real_of_irrational_root {Ξ± : ℝ} (ha : irrational Ξ±) {f : polynomial β„€} (f0 : f β‰  0) (fa : eval Ξ± (map (algebra_map β„€ ℝ) f) = 0): βˆƒ A : ℝ, 0 < A ∧ βˆ€ (a : β„€), βˆ€ (b : β„•), (1 : ℝ) ≀ (b + 1) ^ f.nat_degree * (abs (Ξ± - (a / (b + 1))) * A) := begin -- `fR` is `f` viewed as a polynomial with `ℝ` coefficients. set fR : polynomial ℝ := map (algebra_map β„€ ℝ) f, -- `fR` is non-zero, since `f` is non-zero. obtain fR0 : fR β‰  0 := Ξ» fR0, (map_injective (algebra_map β„€ ℝ) (Ξ» _ _ A, int.cast_inj.mp A)).ne f0 (fR0.trans (polynomial.map_zero _).symm), -- reformulating assumption `fa`: `Ξ±` is a root of `fR`. have ar : Ξ± ∈ (fR.roots.to_finset : set ℝ) := finset.mem_coe.mpr (multiset.mem_to_finset.mpr ((mem_roots fR0).mpr (is_root.def.mpr fa))), -- Since the polynomial `fR` has finitely many roots, there is a closed interval centered at `Ξ±` -- such that `Ξ±` is the only root of `fR` in the interval. obtain ⟨΢, z0, U⟩ : βˆƒ ΞΆ > 0, closed_ball Ξ± ΞΆ ∩ (fR.roots.to_finset) = {Ξ±} := @exists_closed_ball_inter_eq_singleton_of_discrete _ _ _ discrete_of_t1_of_finite _ ar, -- Since `fR` is continuous, it is bounded on the interval above. obtain ⟨xm, -, hM⟩ : βˆƒ (xm : ℝ) (H : xm ∈ Icc (Ξ± - ΞΆ) (Ξ± + ΞΆ)), βˆ€ (y : ℝ), y ∈ Icc (Ξ± - ΞΆ) (Ξ± + ΞΆ) β†’ abs (fR.derivative.eval y) ≀ abs (fR.derivative.eval xm) := is_compact.exists_forall_ge compact_Icc ⟨α, (sub_lt_self Ξ± z0).le, (lt_add_of_pos_right Ξ± z0).le⟩ (continuous_abs.comp fR.derivative.continuous_aeval).continuous_on, -- Use the key lemma `exists_one_le_pow_mul_dist`: we are left to show that ... refine @exists_one_le_pow_mul_dist β„€ β„• ℝ _ _ _ (Ξ» y, fR.eval y) Ξ± ΞΆ (abs (fR.derivative.eval xm)) _ z0 (Ξ» y hy, _) (Ξ» z a hq, _), -- 1: the denominators are positive -- essentially by definition; { exact Ξ» a, one_le_pow_of_one_le ((le_add_iff_nonneg_left 1).mpr a.cast_nonneg) _ }, -- 2: the polynomial `fR` is Lipschitz at `Ξ±` -- as its derivative continuous; { rw mul_comm, rw closed_ball_Icc at hy, -- apply the Mean Value Theorem: the bound on the derivative comes from differentiability. refine convex.norm_image_sub_le_of_norm_deriv_le (Ξ» _ _, fR.differentiable_at) (Ξ» y h, by { rw fR.deriv, exact hM _ h }) (convex_Icc _ _) hy (mem_Icc_iff_abs_le.mp _), exact @mem_closed_ball_self ℝ _ Ξ± ΞΆ (le_of_lt z0) }, -- 3: the weird inequality of Liouville type with powers of the denominators. { show 1 ≀ (a + 1 : ℝ) ^ f.nat_degree * abs (eval Ξ± fR - eval (z / (a + 1)) fR), rw [fa, zero_sub, abs_neg], -- key observation: the right-hand side of the inequality is an *integer*. Therefore, -- if its absolute value is not at least one, then it vanishes. Proceed by contradiction refine one_le_pow_mul_abs_eval_div (int.coe_nat_succ_pos a) (Ξ» hy, _), -- As the evaluation of the polynomial vanishes, we found a root of `fR` that is rational. -- We know that `Ξ±` is the only root of `fR` in our interval, and `Ξ±` is irrational: -- follow your nose. refine (irrational_iff_ne_rational Ξ±).mp ha z (a + 1) ((mem_singleton_iff.mp _).symm), refine U.subset _, refine ⟨hq, finset.mem_coe.mp (multiset.mem_to_finset.mpr _)⟩, exact (mem_roots fR0).mpr (is_root.def.mpr hy) } end theorem transcendental {x : ℝ} (lx : liouville x) : transcendental β„€ x := begin -- Proceed by contradiction: if `x` is algebraic, then `x` is the root (`ef0`) of a -- non-zero (`f0`) polynomial `f` rintros ⟨f : polynomial β„€, f0, ef0⟩, -- Change `aeval x f = 0` to `eval (map _ f) = 0`, who knew. replace ef0 : (f.map (algebra_map β„€ ℝ)).eval x = 0, { rwa [aeval_def, ← eval_map] at ef0 }, -- There is a "large" real number `A` such that `(b + 1) ^ (deg f) * |f (x - a / (b + 1))| * A` -- is at least one. This is obtained from lemma `exists_pos_real_of_irrational_root`. obtain ⟨A, hA, h⟩ : βˆƒ (A : ℝ), 0 < A ∧ βˆ€ (a : β„€) (b : β„•), (1 : ℝ) ≀ (b.succ) ^ f.nat_degree * (abs (x - a / (b.succ)) * A) := exists_pos_real_of_irrational_root lx.irrational f0 ef0, -- Since the real numbers are Archimedean, a power of `2` exceeds `A`: `hn : A < 2 ^ r`. rcases pow_unbounded_of_one_lt A (lt_add_one 1) with ⟨r, hn⟩, -- Use the Liouville property, with exponent `r + deg f`. obtain ⟨a, b, b1, -, a1⟩ : βˆƒ (a b : β„€), 1 < b ∧ x β‰  a / b ∧ abs (x - a / b) < 1 / b ^ (r + f.nat_degree) := lx (r + f.nat_degree), have b0 : (0 : ℝ) < b := zero_lt_one.trans (by { rw ← int.cast_one, exact int.cast_lt.mpr b1 }), -- Prove that `b ^ f.nat_degree * abs (x - a / b)` is strictly smaller than itself -- recall, this is a proof by contradiction! refine lt_irrefl ((b : ℝ) ^ f.nat_degree * abs (x - ↑a / ↑b)) _, -- clear denominators at `a1` rw [lt_div_iff' (pow_pos b0 _), pow_add, mul_assoc] at a1, -- split the inequality via `1 / A`. refine ((_ : (b : ℝ) ^ f.nat_degree * abs (x - a / b) < 1 / A).trans_le _), -- This branch of the proof uses the Liouville condition and the Archimedean property { refine (lt_div_iff' hA).mpr _, refine lt_of_le_of_lt _ a1, refine mul_le_mul_of_nonneg_right _ (mul_nonneg (pow_nonneg b0.le _) (abs_nonneg _)), refine hn.le.trans _, refine pow_le_pow_of_le_left zero_le_two _ _, exact int.cast_two.symm.le.trans (int.cast_le.mpr (int.add_one_le_iff.mpr b1)) }, -- this branch of the proof exploits the "integrality" of evaluations of polynomials -- at ratios of integers. { lift b to β„• using zero_le_one.trans b1.le, specialize h a b.pred, rwa [nat.succ_pred_eq_of_pos (zero_lt_one.trans _), ← mul_assoc, ← (div_le_iff hA)] at h, exact int.coe_nat_lt.mp b1 } end end liouville
61009c43e0365637e1450578d132fe8a997a16da
a339bc2ac96174381fb610f4b2e1ba42df2be819
/hott/homotopy/join.hlean
dce4ed436fc9434ba7d6d1858beb542b4a007115
[ "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
22,760
hlean
/- Copyright (c) 2015 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Ulrik Buchholtz Declaration of a join as a special case of a pushout -/ import hit.pushout .sphere cubical.cube open eq function prod equiv is_trunc bool sigma.ops pointed definition join (A B : Type) : Type := @pushout.pushout (A Γ— B) A B pr1 pr2 namespace join section variables {A B : Type} definition inl (a : A) : join A B := @pushout.inl (A Γ— B) A B pr1 pr2 a definition inr (b : B) : join A B := @pushout.inr (A Γ— B) A B pr1 pr2 b definition glue (a : A) (b : B) : inl a = inr b := @pushout.glue (A Γ— B) A B pr1 pr2 (a, b) protected definition rec {P : join A B β†’ Type} (Pinl : Ξ (x : A), P (inl x)) (Pinr : Ξ (y : B), P (inr y)) (Pglue : Ξ (x : A)(y : B), Pinl x =[glue x y] Pinr y) (z : join A B) : P z := pushout.rec Pinl Pinr (prod.rec Pglue) z protected definition rec_glue {P : join A B β†’ Type} (Pinl : Ξ (x : A), P (inl x)) (Pinr : Ξ (y : B), P (inr y)) (Pglue : Ξ (x : A)(y : B), Pinl x =[glue x y] Pinr y) (x : A) (y : B) : apd (join.rec Pinl Pinr Pglue) (glue x y) = Pglue x y := !quotient.rec_eq_of_rel protected definition elim {P : Type} (Pinl : A β†’ P) (Pinr : B β†’ P) (Pglue : Ξ (x : A)(y : B), Pinl x = Pinr y) (z : join A B) : P := join.rec Pinl Pinr (Ξ»x y, pathover_of_eq _ (Pglue x y)) z protected definition elim_glue {P : Type} (Pinl : A β†’ P) (Pinr : B β†’ P) (Pglue : Ξ (x : A)(y : B), Pinl x = Pinr y) (x : A) (y : B) : ap (join.elim Pinl Pinr Pglue) (glue x y) = Pglue x y := begin apply equiv.eq_of_fn_eq_fn_inv !(pathover_constant (glue x y)), rewrite [β–Έ*,-apd_eq_pathover_of_eq_ap,↑join.elim], apply join.rec_glue end protected definition elim_ap_inl {P : Type} (Pinl : A β†’ P) (Pinr : B β†’ P) (Pglue : Ξ (x : A)(y : B), Pinl x = Pinr y) {a a' : A} (p : a = a') : ap (join.elim Pinl Pinr Pglue) (ap inl p) = ap Pinl p := by cases p; reflexivity protected definition hsquare {a a' : A} {b b' : B} (p : a = a') (q : b = b') : square (ap inl p) (ap inr q) (glue a b) (glue a' b') := eq.rec_on p (eq.rec_on q hrfl) protected definition vsquare {a a' : A} {b b' : B} (p : a = a') (q : b = b') : square (glue a b) (glue a' b') (ap inl p) (ap inr q) := eq.rec_on p (eq.rec_on q vrfl) end end join open join definition pjoin [constructor] (A B : Type*) : Type* := pointed.MK (join A B) (inl pt) attribute join.inl join.inr [constructor] attribute join.rec [recursor] attribute join.elim [recursor 7] attribute join.rec join.elim [unfold 7] /- Diamonds in joins -/ namespace join variables {A B : Type} protected definition diamond (a a' : A) (b b' : B) := square (glue a b) (glue a' b')⁻¹ (glue a b') (glue a' b)⁻¹ protected definition hdiamond {a a' : A} (b b' : B) (p : a = a') : join.diamond a a' b b' := begin cases p, unfold join.diamond, assert H : (glue a b' ⬝ (glue a b')⁻¹ ⬝ (glue a b)⁻¹⁻¹) = glue a b, { rewrite [con.right_inv,inv_inv,idp_con] }, exact H β–Έ top_deg_square (glue a b') (glue a b')⁻¹ (glue a b)⁻¹, end protected definition vdiamond (a a' : A) {b b' : B} (q : b = b') : join.diamond a a' b b' := begin cases q, unfold join.diamond, assert H : (glue a b ⬝ (glue a' b)⁻¹ ⬝ (glue a' b)⁻¹⁻¹) = glue a b, { rewrite [con.assoc,con.right_inv] }, exact H β–Έ top_deg_square (glue a b) (glue a' b)⁻¹ (glue a' b)⁻¹ end protected definition symm_diamond (a : A) (b : B) : join.vdiamond a a idp = join.hdiamond b b idp := begin unfold join.hdiamond, unfold join.vdiamond, assert H : Ξ {X : Type} ⦃x y : X⦄ (p : x = y), eq.rec (eq.rec (refl p) (symm (con.right_inv p⁻¹))) (symm (con.assoc p p⁻¹ p⁻¹⁻¹)) β–Έ top_deg_square p p⁻¹ p⁻¹ = eq.rec (eq.rec (eq.rec (refl p) (symm (idp_con p))) (symm (inv_inv p))) (symm (con.right_inv p)) β–Έ top_deg_square p p⁻¹ p⁻¹ :> square p p⁻¹ p p⁻¹, { intros X x y p, cases p, reflexivity }, apply H (glue a b) end end join namespace join variables {A₁ Aβ‚‚ B₁ Bβ‚‚ : Type} protected definition functor [reducible] (f : A₁ β†’ Aβ‚‚) (g : B₁ β†’ Bβ‚‚) : join A₁ B₁ β†’ join Aβ‚‚ Bβ‚‚ := begin intro x, induction x with a b a b, { exact inl (f a) }, { exact inr (g b) }, { apply glue } end protected definition ap_diamond (f : A₁ β†’ Aβ‚‚) (g : B₁ β†’ Bβ‚‚) {a a' : A₁} {b b' : B₁} : join.diamond a a' b b' β†’ join.diamond (f a) (f a') (g b) (g b') := begin unfold join.diamond, intro s, note s' := aps (join.functor f g) s, do 2 rewrite eq.ap_inv at s', do 4 rewrite join.elim_glue at s', exact s' end protected definition equiv_closed : A₁ ≃ Aβ‚‚ β†’ B₁ ≃ Bβ‚‚ β†’ join A₁ B₁ ≃ join Aβ‚‚ Bβ‚‚ := begin intros H K, fapply equiv.MK, { intro x, induction x with a b a b, { exact inl (to_fun H a) }, { exact inr (to_fun K b) }, { apply glue } }, { intro y, induction y with a b a b, { exact inl (to_inv H a) }, { exact inr (to_inv K b) }, { apply glue } }, { intro y, induction y with a b a b, { apply ap inl, apply to_right_inv }, { apply ap inr, apply to_right_inv }, { apply eq_pathover, rewrite ap_id, rewrite (ap_compose' (join.elim _ _ _)), do 2 krewrite join.elim_glue, apply join.hsquare } }, { intro x, induction x with a b a b, { apply ap inl, apply to_left_inv }, { apply ap inr, apply to_left_inv }, { apply eq_pathover, rewrite ap_id, rewrite (ap_compose' (join.elim _ _ _)), do 2 krewrite join.elim_glue, apply join.hsquare } } end protected definition twist_diamond {A : Type} {a a' : A} (p : a = a') : pathover (Ξ»x, join.diamond a' x a x) (join.vdiamond a' a idp) p (join.hdiamond a a' idp) := begin cases p, apply pathover_idp_of_eq, apply join.symm_diamond end protected definition empty (A : Type) : join empty A ≃ A := begin fapply equiv.MK, { intro x, induction x with z a z a, { induction z }, { exact a }, { induction z } }, { intro a, exact inr a }, { intro a, reflexivity }, { intro x, induction x with z a z a, { induction z }, { reflexivity }, { induction z } } end protected definition bool (A : Type) : join bool A ≃ susp A := begin fapply equiv.MK, { intro ba, induction ba with [b, a, b, a], { induction b, exact susp.south, exact susp.north }, { exact susp.north }, { induction b, esimp, { apply inverse, apply susp.merid, exact a }, { reflexivity } } }, { intro s, induction s with a, { exact inl tt }, { exact inl ff }, { exact (glue tt a) ⬝ (glue ff a)⁻¹ } }, { intro s, induction s with a, { reflexivity }, { reflexivity }, { esimp, apply eq_pathover, rewrite ap_id, rewrite (ap_compose' (join.elim _ _ _)), rewrite [susp.elim_merid,ap_con,ap_inv], krewrite [join.elim_glue,join.elim_glue], esimp, rewrite [inv_inv,idp_con], apply hdeg_square, reflexivity } }, { intro ba, induction ba with [b, a, b, a], esimp, { induction b, do 2 reflexivity }, { apply glue }, { induction b, { esimp, apply eq_pathover, rewrite ap_id, rewrite (ap_compose' (susp.elim _ _ _)), krewrite join.elim_glue, rewrite ap_inv, krewrite susp.elim_merid, apply square_of_eq_top, apply inverse, rewrite con.assoc, apply con.left_inv }, { esimp, apply eq_pathover, rewrite ap_id, rewrite (ap_compose' (susp.elim _ _ _)), krewrite join.elim_glue, esimp, apply square_of_eq_top, rewrite [idp_con,con.right_inv] } } } end end join namespace join variables (A B C : Type) protected definition is_contr [HA : is_contr A] : is_contr (join A B) := begin fapply is_contr.mk, exact inl (center A), intro x, induction x with a b a b, apply ap inl, apply center_eq, apply glue, apply pathover_of_tr_eq, apply concat, apply eq_transport_Fr, esimp, rewrite ap_id, generalize center_eq a, intro p, cases p, apply idp_con, end protected definition swap : join A B β†’ join B A := begin intro x, induction x with a b a b, exact inr a, exact inl b, apply !glue⁻¹ end protected definition swap_involutive (x : join A B) : join.swap B A (join.swap A B x) = x := begin induction x with a b a b, do 2 reflexivity, apply eq_pathover, rewrite ap_id, apply hdeg_square, esimp[join.swap], apply concat, apply ap_compose' (join.elim _ _ _), krewrite [join.elim_glue, ap_inv, join.elim_glue], apply inv_inv, end protected definition symm : join A B ≃ join B A := by fapply equiv.MK; do 2 apply join.swap; do 2 apply join.swap_involutive end join /- This proves that the join operator is associative. The proof is more or less ported from Evan Cavallo's agda version: https://github.com/HoTT/HoTT-Agda/blob/master/homotopy/JoinAssocCubical.agda -/ namespace join section join_switch private definition massage_sq' {A : Type} {aβ‚€β‚€ aβ‚‚β‚€ aβ‚€β‚‚ aβ‚‚β‚‚ : A} {p₁₀ : aβ‚€β‚€ = aβ‚‚β‚€} {p₁₂ : aβ‚€β‚‚ = aβ‚‚β‚‚} {p₀₁ : aβ‚€β‚€ = aβ‚€β‚‚} {p₂₁ : aβ‚‚β‚€ = aβ‚‚β‚‚} (sq : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀⁻¹ p₀₁⁻¹ (p₂₁ ⬝ p₁₂⁻¹) idp := by induction sq; exact ids private definition massage_sq {A : Type} {aβ‚€β‚€ aβ‚‚β‚€ aβ‚€β‚‚ : A} {p₁₀ : aβ‚€β‚€ = aβ‚‚β‚€} {p₁₂ : aβ‚€β‚‚ = aβ‚‚β‚€} {p₀₁ : aβ‚€β‚€ = aβ‚€β‚‚} (sq : square p₁₀ p₁₂ p₀₁ idp) : square p₁₀⁻¹ p₀₁⁻¹ p₁₂⁻¹ idp := !idp_con⁻¹ ⬝ph (massage_sq' sq) private definition ap_square_massage {A B : Type} (f : A β†’ B) {aβ‚€β‚€ aβ‚€β‚‚ aβ‚‚β‚€ : A} {p₀₁ : aβ‚€β‚€ = aβ‚€β‚‚} {p₁₀ : aβ‚€β‚€ = aβ‚‚β‚€} {p₁₁ : aβ‚‚β‚€ = aβ‚€β‚‚} (sq : square p₀₁ p₁₁ p₁₀ idp) : cube (hdeg_square (ap_inv f p₁₁)) ids (aps f (massage_sq sq)) (massage_sq (aps f sq)) (hdeg_square !ap_inv) (hdeg_square !ap_inv) := by apply rec_on_r sq; apply idc private definition massage_cube' {A : Type} {aβ‚€β‚€β‚€ aβ‚‚β‚€β‚€ aβ‚€β‚‚β‚€ aβ‚‚β‚‚β‚€ aβ‚€β‚€β‚‚ aβ‚‚β‚€β‚‚ aβ‚€β‚‚β‚‚ aβ‚‚β‚‚β‚‚ : A} {p₁₀₀ : aβ‚€β‚€β‚€ = aβ‚‚β‚€β‚€} {p₀₁₀ : aβ‚€β‚€β‚€ = aβ‚€β‚‚β‚€} {p₀₀₁ : aβ‚€β‚€β‚€ = aβ‚€β‚€β‚‚} {p₁₂₀ : aβ‚€β‚‚β‚€ = aβ‚‚β‚‚β‚€} {p₂₁₀ : aβ‚‚β‚€β‚€ = aβ‚‚β‚‚β‚€} {p₂₀₁ : aβ‚‚β‚€β‚€ = aβ‚‚β‚€β‚‚} {p₁₀₂ : aβ‚€β‚€β‚‚ = aβ‚‚β‚€β‚‚} {p₀₁₂ : aβ‚€β‚€β‚‚ = aβ‚€β‚‚β‚‚} {p₀₂₁ : aβ‚€β‚‚β‚€ = aβ‚€β‚‚β‚‚} {p₁₂₂ : aβ‚€β‚‚β‚‚ = aβ‚‚β‚‚β‚‚} {p₂₁₂ : aβ‚‚β‚€β‚‚ = aβ‚‚β‚‚β‚‚} {p₂₂₁ : aβ‚‚β‚‚β‚€ = aβ‚‚β‚‚β‚‚} {s₁₁₀ : square p₀₁₀ p₂₁₀ p₁₀₀ p₁₂₀} {s₁₁₂ : square p₀₁₂ p₂₁₂ p₁₀₂ p₁₂₂} {s₀₁₁ : square p₀₁₀ p₀₁₂ p₀₀₁ p₀₂₁} {s₂₁₁ : square p₂₁₀ p₂₁₂ p₂₀₁ p₂₂₁} {s₁₀₁ : square p₁₀₀ p₁₀₂ p₀₀₁ p₂₀₁} {s₁₂₁ : square p₁₂₀ p₁₂₂ p₀₂₁ p₂₂₁} (c : cube s₀₁₁ s₂₁₁ s₁₀₁ s₁₂₁ s₁₁₀ s₁₁₂) : cube (s₂₁₁ ⬝v s₁₁₂⁻¹ᡛ) vrfl (massage_sq' s₁₀₁) (massage_sq' s₁₂₁) s₁₁₀⁻¹ᡛ s₀₁₁⁻¹ᡛ := by cases c; apply idc private definition massage_cube {A : Type} {aβ‚€β‚€β‚€ aβ‚‚β‚€β‚€ aβ‚€β‚‚β‚€ aβ‚‚β‚‚β‚€ aβ‚€β‚€β‚‚ aβ‚€β‚‚β‚‚ : A} {p₁₀₀ : aβ‚€β‚€β‚€ = aβ‚‚β‚€β‚€} {p₀₁₀ : aβ‚€β‚€β‚€ = aβ‚€β‚‚β‚€} {p₀₀₁ : aβ‚€β‚€β‚€ = aβ‚€β‚€β‚‚} {p₁₂₀ : aβ‚€β‚‚β‚€ = aβ‚‚β‚‚β‚€} {p₂₁₀ : aβ‚‚β‚€β‚€ = aβ‚‚β‚‚β‚€} {p₁₀₂ : aβ‚€β‚€β‚‚ = aβ‚‚β‚€β‚€} {p₀₁₂ : aβ‚€β‚€β‚‚ = aβ‚€β‚‚β‚‚} {p₀₂₁ : aβ‚€β‚‚β‚€ = aβ‚€β‚‚β‚‚} {p₁₂₂ : aβ‚€β‚‚β‚‚ = aβ‚‚β‚‚β‚€} {s₁₁₀ : square p₀₁₀ _ _ _} {s₁₁₂ : square p₀₁₂ p₂₁₀ p₁₀₂ p₁₂₂} {s₀₁₁ : square p₀₁₀ p₀₁₂ p₀₀₁ p₀₂₁} --{s₂₁₁ : square p₂₁₀ p₂₁₀ idp idp} {s₁₀₁ : square p₁₀₀ p₁₀₂ p₀₀₁ idp} {s₁₂₁ : square p₁₂₀ p₁₂₂ p₀₂₁ idp} (c : cube s₀₁₁ vrfl s₁₀₁ s₁₂₁ s₁₁₀ s₁₁₂) : cube s₁₁₂⁻¹ᡛ vrfl (massage_sq s₁₀₁) (massage_sq s₁₂₁) s₁₁₀⁻¹ᡛ s₀₁₁⁻¹ᡛ := begin cases p₁₀₀, cases p₁₀₂, cases p₁₂₂, note c' := massage_cube' c, esimp[massage_sq], krewrite vdeg_v_eq_ph_pv_hp at c', exact c', end private definition massage_massage {A : Type} {aβ‚€β‚€ aβ‚€β‚‚ aβ‚‚β‚€ : A} {p₀₁ : aβ‚€β‚€ = aβ‚€β‚‚} {p₁₀ : aβ‚€β‚€ = aβ‚‚β‚€} {p₁₁ : aβ‚‚β‚€ = aβ‚€β‚‚} (sq : square p₀₁ p₁₁ p₁₀ idp) : cube (hdeg_square !inv_inv) ids (massage_sq (massage_sq sq)) sq (hdeg_square !inv_inv) (hdeg_square !inv_inv) := by apply rec_on_r sq; apply idc private definition square_Flr_ap_idp_cube {A B : Type} {b : B} {f : A β†’ B} {p₁ pβ‚‚ : Ξ  a, f a = b} (Ξ± : Ξ  a, p₁ a = pβ‚‚ a) {a₁ aβ‚‚ : A} (q : a₁ = aβ‚‚) : cube hrfl hrfl (square_Flr_ap_idp p₁ q) (square_Flr_ap_idp pβ‚‚ q) (hdeg_square (Ξ± _)) (hdeg_square (Ξ± _)) := by cases q; esimp[square_Flr_ap_idp]; apply deg3_cube; esimp variables {A B C : Type} definition switch_left [reducible] : join A B β†’ join (join C B) A := begin intro x, induction x with a b a b, exact inr a, exact inl (inr b), apply !glue⁻¹, end private definition switch_coh_fill_square (a : A) (b : B) (c : C) := square (glue (inl c) a)⁻¹ (ap inl (glue c b))⁻¹ (ap switch_left (glue a b)) idp private definition switch_coh_fill_cube (a : A) (b : B) (c : C) (sq : switch_coh_fill_square a b c) := cube (hdeg_square !join.elim_glue) ids sq (massage_sq !square_Flr_ap_idp) hrfl hrfl private definition switch_coh_fill_type (a : A) (b : B) (c : C) := Ξ£ sq : switch_coh_fill_square a b c, switch_coh_fill_cube a b c sq private definition switch_coh_fill (a : A) (b : B) (c : C) : switch_coh_fill_type a b c := by esimp; apply cube_fill101 private definition switch_coh (ab : join A B) (c : C) : switch_left ab = inl (inl c) := begin induction ab with a b a b, apply !glue⁻¹, apply (ap inl !glue)⁻¹, apply eq_pathover, refine _ ⬝hp !ap_constant⁻¹, apply !switch_coh_fill.1, end protected definition switch [reducible] : join (join A B) C β†’ join (join C B) A := begin intro x, induction x with ab c ab c, exact switch_left ab, exact inl (inl c), exact switch_coh ab c, end private definition switch_inv_left_square (a : A) (b : B) : square idp idp (ap (!(@join.switch C) ∘ switch_left) (glue a b)) (ap inl (glue a b)) := begin refine hdeg_square !ap_compose ⬝h _, refine aps join.switch (hdeg_square !join.elim_glue) ⬝h _, esimp, refine hdeg_square !(ap_inv join.switch) ⬝h _, refine hrfl⁻¹ʰ⁻¹ᡛ ⬝h _, esimp[join.switch,switch_left,switch_coh], refine (hdeg_square !join.elim_glue)⁻¹ᡛ ⬝h _, esimp, refine hrfl⁻¹ᡛ ⬝h _, apply hdeg_square !inv_inv, end private definition switch_inv_coh_left (c : C) (a : A) : square idp idp (ap !(@join.switch C B) (switch_coh (inl a) c)) (glue (inl a) c) := begin refine hrfl ⬝h _, refine aps join.switch hrfl ⬝h _, esimp[switch_coh], refine hdeg_square !ap_inv ⬝h _, refine hrfl⁻¹ʰ⁻¹ᡛ ⬝h _, esimp[join.switch,switch_left], refine (hdeg_square !join.elim_glue)⁻¹ᡛ ⬝h _, refine hrfl⁻¹ᡛ ⬝h _, apply hdeg_square !inv_inv, end private definition switch_inv_coh_right (c : C) (b : B) : square idp idp (ap !(@join.switch _ _ A) (switch_coh (inr b) c)) (glue (inr b) c) := begin refine hrfl ⬝h _, refine aps join.switch hrfl ⬝h _, esimp[switch_coh], refine hdeg_square !ap_inv ⬝h _, refine (hdeg_square !ap_compose)⁻¹ʰ⁻¹ᡛ ⬝h _, refine hrfl⁻¹ᡛ ⬝h _, esimp[join.switch,switch_left], refine (hdeg_square !join.elim_glue)⁻¹ᡛ ⬝h _, apply hdeg_square !inv_inv, end private definition switch_inv_left (ab : join A B) : !(@join.switch C) (join.switch (inl ab)) = inl ab := begin induction ab with a b a b, do 2 reflexivity, apply eq_pathover, exact !switch_inv_left_square, end section variables (a : A) (b : B) (c : C) private definition switch_inv_cube_aux1 {A B C : Type} {b : B} {f : A β†’ B} (h : B β†’ C) (g : Ξ  a, f a = b) {x y : A} (p : x = y) : cube (hdeg_square (ap_compose h f p)) ids (square_Flr_ap_idp (Ξ» a, ap h (g a)) p) (aps h (square_Flr_ap_idp _ _)) hrfl hrfl := by cases p; esimp[square_Flr_ap_idp]; apply deg2_cube; cases (g x); esimp private definition switch_inv_cube_aux2 {A B : Type} {b : B} {f : A β†’ B} (g : Ξ  a, f a = b) {x y : A} (p : x = y) {sq : square (g x) (g y) (ap f p) idp} (q : apd g p = eq_pathover (sq ⬝hp !ap_constant⁻¹)) : square_Flr_ap_idp _ _ = sq := begin cases p, esimp at *, apply concat, apply inverse, apply vdeg_square_idp, apply concat, apply ap vdeg_square, exact ap eq_of_pathover_idp q, krewrite (is_equiv.right_inv (equiv.to_fun !pathover_idp)), exact is_equiv.left_inv (equiv.to_fun (vdeg_square_equiv _ _)) sq, end private definition switch_inv_cube (a : A) (b : B) (c : C) : cube (switch_inv_left_square a b) ids (square_Flr_ap_idp _ _) (square_Flr_ap_idp _ _) (switch_inv_coh_left c a) (switch_inv_coh_right c b) := begin esimp [switch_inv_coh_left, switch_inv_coh_right, switch_inv_left_square], apply cube_concat2, apply switch_inv_cube_aux1, apply cube_concat2, apply cube_transport101, apply inverse, apply ap (Ξ» x, aps join.switch x), apply switch_inv_cube_aux2, apply join.rec_glue, apply apc, apply (switch_coh_fill a b c).2, apply cube_concat2, esimp, apply ap_square_massage, apply cube_concat2, apply massage_cube, apply cube_inverse2, apply switch_inv_cube_aux1, apply cube_concat2, apply massage_cube, apply square_Flr_ap_idp_cube, apply cube_concat2, apply massage_cube, apply cube_transport101, apply inverse, apply switch_inv_cube_aux2, esimp[switch_coh], apply join.rec_glue, apply (switch_coh_fill c b a).2, apply massage_massage, end end private definition pathover_of_triangle_cube {A B : Type} {bβ‚€ b₁ : A β†’ B} {b : B} {p₀₁ : Ξ  a, bβ‚€ a = b₁ a} {pβ‚€ : Ξ  a, bβ‚€ a = b} {p₁ : Ξ  a, b₁ a = b} {x y : A} {q : x = y} {sqx : square (p₀₁ x) idp (pβ‚€ x) (p₁ x)} {sqy : square (p₀₁ y) idp (pβ‚€ y) (p₁ y)} (c : cube (natural_square _ _) ids (square_Flr_ap_idp pβ‚€ q) (square_Flr_ap_idp p₁ q) sqx sqy) : sqx =[q] sqy := by cases q; apply pathover_of_eq_tr; apply eq_of_deg12_cube; exact c private definition pathover_of_ap_ap_square {A : Type} {x y : A} {p : x = y} (g : B β†’ A) (f : A β†’ B) {u : g (f x) = x} {v : g (f y) = y} (sq : square (ap g (ap f p)) p u v) : u =[p] v := by cases p; apply eq_pathover; apply transpose; exact sq private definition natural_square_beta {A B : Type} {f₁ fβ‚‚ : A β†’ B} (p : Ξ  a, f₁ a = fβ‚‚ a) {x y : A} (q : x = y) {sq : square (p x) (p y) (ap f₁ q) (ap fβ‚‚ q)} (e : apd p q = eq_pathover sq) : natural_square p q = sq := begin cases q, esimp at *, apply concat, apply inverse, apply vdeg_square_idp, apply concat, apply ap vdeg_square, apply ap eq_of_pathover_idp e, krewrite (is_equiv.right_inv (equiv.to_fun !pathover_idp)), exact is_equiv.left_inv (equiv.to_fun (vdeg_square_equiv _ _)) sq, end private definition switch_inv_coh (c : C) (k : join A B) : square (switch_inv_left k) idp (ap join.switch (switch_coh k c)) (glue k c) := begin induction k with a b a b, apply switch_inv_coh_left, apply switch_inv_coh_right, refine pathover_of_triangle_cube _, esimp, apply cube_transport011, apply inverse, rotate 1, apply switch_inv_cube, apply natural_square_beta, apply join.rec_glue, end protected definition switch_involutive (x : join (join A B) C) : join.switch (join.switch x) = x := begin induction x with ab c ab c, apply switch_inv_left, reflexivity, apply pathover_of_ap_ap_square join.switch join.switch, krewrite join.elim_glue, esimp, apply transpose, exact !switch_inv_coh, end end join_switch protected definition switch_equiv (A B C : Type) : join (join A B) C ≃ join (join C B) A := by apply equiv.MK; do 2 apply join.switch_involutive protected definition assoc (A B C : Type) : join (join A B) C ≃ join A (join B C) := calc join (join A B) C ≃ join (join C B) A : join.switch_equiv ... ≃ join A (join C B) : join.symm ... ≃ join A (join B C) : join.equiv_closed erfl (join.symm C B) protected definition ap_assoc_inv_glue_inl {A B : Type} (C : Type) (a : A) (b : B) : ap (to_inv (join.assoc A B C)) (glue a (inl b)) = ap inl (glue a b) := begin unfold join.assoc, rewrite ap_compose, krewrite join.elim_glue, rewrite ap_compose, krewrite join.elim_glue, rewrite ap_inv, krewrite join.elim_glue, unfold switch_coh, unfold join.symm, unfold join.swap, esimp, rewrite eq.inv_inv end protected definition ap_assoc_inv_glue_inr {A C : Type} (B : Type) (a : A) (c : C) : ap (to_inv (join.assoc A B C)) (glue a (inr c)) = glue (inl a) c := begin unfold join.assoc, rewrite ap_compose, krewrite join.elim_glue, rewrite ap_compose, krewrite join.elim_glue, rewrite ap_inv, krewrite join.elim_glue, unfold switch_coh, unfold join.symm, unfold join.swap, esimp, rewrite eq.inv_inv end end join namespace join open sphere sphere_index sphere.ops protected definition spheres (n m : ℕ₋₁) : join (S n) (S m) ≃ S (n+1+m) := begin apply equiv.trans (join.symm (S n) (S m)), induction m with m IH, { exact join.empty (S n) }, { calc join (S m.+1) (S n) ≃ join (join bool (S m)) (S n) : join.equiv_closed (equiv.symm (join.bool (S m))) erfl ... ≃ join bool (join (S m) (S n)) : join.assoc ... ≃ join bool (S (n+1+m)) : join.equiv_closed erfl IH ... ≃ sphere (n+1+m.+1) : join.bool (S (n+1+m)) } end end join
a5d598f7b90d27d16ffd7994d411f70d5f8082ea
d31b9f832ff922a603f76cf32e0f3aa822640508
/src/hott/init/pointed.lean
bbcf4d20d921db37dc55493d2345f1fcdcd80305
[ "Apache-2.0" ]
permissive
javra/hott3
6e7a9e72a991a2fae32e5764982e521dca617b16
cd51f2ab2aa48c1246a188f9b525b30f76c3d651
refs/heads/master
1,585,819,679,148
1,531,232,382,000
1,536,682,965,000
154,294,022
0
0
Apache-2.0
1,540,284,376,000
1,540,284,375,000
null
UTF-8
Lean
false
false
7,694
lean
/- Copyright (c) 2016 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn The definition of pointed types. This file is here to avoid circularities in the import graph -/ import .trunc universes u v w namespace hott hott_theory open eq equiv is_equiv is_trunc class pointed (A : Type u) := (point : A) structure pType := (carrier : Type u) (Point : carrier) notation `Type*` := pType namespace pointed instance : has_coe_to_sort pType.{u} := { S := Type u, coe := pType.carrier, } variables {A : Type _} @[hott, reducible] def pt [H : pointed A] := point A @[hott, reducible] def Point (A : Type*) := pType.Point A @[hott, reducible] def carrier (A : Type*) := pType.carrier A @[hott, reducible] protected def Mk {A : Type _} (a : A) := pType.mk A a @[hott, reducible] protected def MK (A : Type _) (a : A) := pType.mk A a @[hott, reducible] protected def mk' (A : Type _) [H : pointed A] : Type* := pType.mk A (point A) @[hott, instance, priority 1900] def pointed_carrier (A : Type*) : pointed A := pointed.mk (Point A) @[hott, hsimp] def coe_pType_mk (A : Type _) (a : A) : @coe_sort _ pointed.has_coe_to_sort {pType . carrier := A, Point := a} = A := by refl @[hott, hsimp] def pt_pointed_mk (A : Type _) (a : A) : @pt A (pointed.mk a) = a := by refl @[hott, hsimp] def Point_pType_mk (A : Type _) (a : A) : Point (pType.mk A a) = a := by refl end pointed open pointed set_option old_structure_cmd true section /-- Todo: ptrunctype should have pType as a *field*, because otherwise it's annoying that Lean doesn't have definitional eta for structures -/ structure ptrunctype (n : β„•β‚‹β‚‚) extends trunctype.{u} n, pType.{u} notation n `-Type*` := ptrunctype n @[hott] abbreviation pSet := 0-Type* notation `Set*` := pSet @[hott] instance pType_of_ptrunctype (n : β„•β‚‹β‚‚) : has_coe (n-Type*) Type* := ⟨λx, x.to_pType⟩ @[hott] instance pType_of_pSet : has_coe Set* Type* := hott.pType_of_ptrunctype 0 @[hott] instance trunctype_of_ptrunctype (n : β„•β‚‹β‚‚) : has_coe (n-Type*) (n-Type) := ⟨λx, x.to_trunctype⟩ @[hott, hsimp] def coe_ptrunctype_mk (A : Type _) {n : β„•β‚‹β‚‚} (H : is_trunc n A) (a : A) : coe_sort {ptrunctype . carrier := A, Point := a, struct := H} = A := by refl @[hott, hsimp] def to_pType_ptrunctype_mk (A : Type _) {n : β„•β‚‹β‚‚} (H : is_trunc n A) (a : A) : {ptrunctype . carrier := A, Point := a, struct := H}.to_pType = {carrier := A, Point := a} := by refl -- @[hott] instance ptrunctype.has_coe_to_sort (n) : has_coe_to_sort (ptrunctype n) := -- ⟨_, ptrunctype.carrier⟩ @[hott] instance is_trunc_ptrunctype {n : β„•β‚‹β‚‚} (X : ptrunctype n) : is_trunc n X := X.struct @[hott] instance is_trunc_pointed {n : β„•β‚‹β‚‚} (X : ptrunctype n) : pointed X := pointed_carrier X.to_pType end namespace pointed @[hott] protected def ptrunctype.mk' (n : β„•β‚‹β‚‚) (A : Type _) [pointed A] [is_trunc n A] : n-Type* := ptrunctype.mk A (by apply_instance) pt @[hott] protected def pSet.mk := @ptrunctype.mk (-1.+1) @[hott] protected def pSet.mk' := ptrunctype.mk' (-1.+1) @[hott] def ptrunctype_of_trunctype {n : β„•β‚‹β‚‚} (A : n-Type) (a : A) : n-Type* := ptrunctype.mk A (by apply_instance) a @[hott] def ptrunctype_of_pType {n : β„•β‚‹β‚‚} (A : Type*) (H : is_trunc n A) : n-Type* := ptrunctype.mk A (by apply_instance) pt @[hott] def pSet_of_Set (A : Set) (a : A) : Set* := ptrunctype.mk A (by apply_instance) a @[hott] def pSet_of_pType (A : Type*) (H : is_set A) : Set* := ptrunctype.mk A (by apply_instance) pt -- Any contractible type is pointed @[hott, instance] def pointed_of_is_contr (A : Type _) [H : is_contr A] : pointed A := pointed.mk (center _) end pointed /- pointed maps -/ variables {A : Type*} structure ppi (P : A β†’ Type _) (xβ‚€ : P pt) := (to_fun : Ξ  a : A, P a) (resp_pt : to_fun (Point A) = xβ‚€) @[hott] def pppi' (P : A β†’ Type*) : Type _ := ppi (Ξ» a, P a) pt @[hott] def ppi_const (P : A β†’ Type*) : pppi' P := ppi.mk (Ξ»a, pt) idp @[hott] def pppi (P : A β†’ Type*) : Type* := pointed.MK (pppi' P) (ppi_const P) -- do we want to make this already pointed? @[hott] def pmap (A B : Type*) : Type _ := @pppi A (Ξ»a, B) @[hott] instance (P : A β†’ Type _) (xβ‚€): has_coe_to_fun (ppi P xβ‚€) := { F := Ξ» f, Ξ  a, P a, coe := Ξ» f a, f.to_fun a } @[hott, hsimp] def coe_fn_ppi {P : A β†’ Type _} {xβ‚€} (f : Ξ a, P a) (p : f (Point A) = xβ‚€) : @coe_fn _ (hott.has_coe_to_fun P xβ‚€) {ppi . to_fun := f, resp_pt := p} = f := by refl infix ` β†’* `:28 := pmap notation `Ξ *` binders `, ` r:(scoped P, pppi P) := r namespace pointed @[hott] def pppi.mk {P : A β†’ Type*} (f : Ξ a, P a) (p : f pt = pt) : pppi P := ppi.mk f p @[hott] def pppi.to_fun {P : A β†’ Type*} (f : pppi' P) (a : A) : P a := ppi.to_fun f a @[hott] instance {P : A β†’ Type*}: has_coe_to_fun (pppi' P) := { F := Ξ» f, Ξ  a, P a, coe := Ξ» f a, f.to_fun a, } @[hott] def pmap.mk {A B : Type*} (f : A β†’ B) (p : f (Point A) = Point B) : A β†’* B := pppi.mk f p @[reducible] def pmap.to_fun {A B : Type*} (f : A β†’* B) : A β†’ B := pppi.to_fun f @[hott] instance pmap.has_coe_to_fun {A B : Type*}: has_coe_to_fun (A β†’* B) := { F := Ξ» f, A β†’ B, coe := pmap.to_fun } @[hott] def respect_pt {P : A β†’ Type _} {pβ‚€ : P pt} (f : ppi P pβ‚€) : f pt = pβ‚€ := ppi.resp_pt f @[hott, hsimp] def mk_to_fun {P : A β†’ Type _} {pβ‚€ : P pt} (f : Ξ a, P a) (p : f pt = pβ‚€) (a : A) : (ppi.mk f p).to_fun a = f a := by refl @[hott, hsimp] def mk_to_fun' {P : A β†’ Type _} {pβ‚€ : P pt} (f : Ξ a, P a) (p : f pt = pβ‚€) (a : A) : ppi.mk f p a = f a := by refl @[hott, hsimp] def pmap_mk_to_fun {A B : Type*} (f : A β†’ B) (p : f pt = pt) (a : A) : (pmap.mk f p).to_fun a = f a := by refl @[hott, hsimp] def pmap_mk_to_fun' {A B : Type*} (f : A β†’ B) (p : f pt = pt) (a : A) : pmap.mk f p a = f a := by refl @[hott, hsimp] def respect_pt_mk {P : A β†’ Type _} {pβ‚€ : P pt} (f : Ξ a, P a) (p : f pt = pβ‚€) : respect_pt (ppi.mk f p) = p := by refl @[hott, hsimp] def respect_pt_pmap_mk {A B : Type*} (f : A β†’ B) (p : f pt = pt) : respect_pt (pmap.mk f p) = p := by refl -- notation `Ξ *` binders `, ` r:(scoped P, ppi _ P) := r -- @[hott] def pmxap.mk [constructor] {A B : Type*} (f : A β†’ B) (p : f pt = pt) : A β†’* B := -- ppi.mk f p -- @[hott] def pmap.to_fun [coercion] [unfold 3] {A B : Type*} (f : A β†’* B) : A β†’ B := f /- pointed homotopies -/ @[hott] def phomotopy {P : A β†’ Type _} {pβ‚€ : P pt} (f g : ppi P pβ‚€) : Type _ := ppi (Ξ»a, f a = g a) (respect_pt f ⬝ (respect_pt g)⁻¹) -- structure phomotopy {A B : Type*} (f g : A β†’* B) : Type _ := -- (homotopy : f ~ g) -- (homotopy_pt : homotopy pt ⬝ respect_pt g = respect_pt f) variables {P : A β†’ Type _} {pβ‚€ : P pt} {f g : ppi P pβ‚€} infix ` ~* `:50 := phomotopy @[hott] def phomotopy.mk (h : f ~ g) (p : h pt ⬝ respect_pt g = respect_pt f) : f ~* g := ppi.mk h (eq_con_inv_of_con_eq p) @[hott] protected def phomotopy.to_fun (h : f ~* g) : Ξ  a : A, f a = g a := ppi.to_fun h @[hott] instance phomotopy.has_coe_to_fun: has_coe_to_fun (f ~* g) := { F := _, coe := phomotopy.to_fun, } @[hott] def to_homotopy (p : f ~* g) : Ξ a, f a = g a := p @[hott] def to_homotopy_pt (p : f ~* g) : p pt ⬝ respect_pt g = respect_pt f := con_eq_of_eq_con_inv (respect_pt p) end pointed end hott
8992566bfa56c7374191993190bfe555671b8c3a
1437b3495ef9020d5413178aa33c0a625f15f15f
/tactic/simpa.lean
2f29e66e11fcb3a603371e72745f7aa961dd34b1
[ "Apache-2.0" ]
permissive
jean002/mathlib
c66bbb2d9fdc9c03ae07f869acac7ddbfce67a30
dc6c38a765799c99c4d9c8d5207d9e6c9e0e2cfd
refs/heads/master
1,587,027,806,375
1,547,306,358,000
1,547,306,358,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,681
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ namespace tactic namespace interactive open interactive interactive.types expr lean.parser local postfix `?`:9001 := optional /-- This is a "finishing" tactic modification of `simp`. The tactic `simpa [rules, ...] using e` will simplify the hypothesis `e` using `rules`, then simplify the goal using `rules`, and try to close the goal using `assumption`. If `e` is a term instead of a local constant, it is first added to the local context using `have`. -/ meta def simpa (use_iota_eqn : parse $ (tk "!")?) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (tgt : parse (tk "using" *> texpr)?) (cfg : simp_config_ext := {}) : tactic unit := let simp_at (lc) := try (simp use_iota_eqn no_dflt hs attr_names (loc.ns lc) cfg) >> (assumption <|> trivial) in match tgt with | none := get_local `this >> simp_at [some `this, none] <|> simp_at [none] | some e := do e ← i_to_expr e <|> do { ty ← target, e ← i_to_expr_strict ``(%%e : %%ty), -- for positional error messages, don't care about the result pty ← pp ty, ptgt ← pp e, -- Fail deliberately, to advise regarding `simp; exact` usage fail ("simpa failed, 'using' expression type not directly " ++ "inferrable. Try:\n\nsimpa ... using\nshow " ++ to_fmt pty ++ ",\nfrom " ++ ptgt : format) }, match e with | local_const _ lc _ _ := simp_at [some lc, none] | e := do t ← infer_type e, assertv `this t e >> simp_at [some `this, none] end end end interactive end tactic
c75e0158fde1baecacf48ca226caf5fd837952eb
fcf3ffa92a3847189ca669cb18b34ef6b2ec2859
/src/world6/level7.lean
fa2a0b5e50a0da19e68b5de7ef08f8817c2f3b24
[ "Apache-2.0" ]
permissive
nomoid/lean-proofs
4a80a97888699dee42b092b7b959b22d9aa0c066
b9f03a24623d1a1d111d6c2bbf53c617e2596d6a
refs/heads/master
1,674,955,317,080
1,607,475,706,000
1,607,475,706,000
314,104,281
0
0
null
null
null
null
UTF-8
Lean
false
false
139
lean
example (P Q R : Prop) : (P β†’ Q) β†’ ((Q β†’ R) β†’ (P β†’ R)) := begin intros pq qf p, apply qf, apply pq, exact p, end
7032b0b70c907e6a31ac96d838fb007bad8a7a49
05b503addd423dd68145d68b8cde5cd595d74365
/src/tactic/linarith.lean
015d63d7ff8990f0f818522f9580a83df1c3ef4e
[ "Apache-2.0" ]
permissive
aestriplex/mathlib
77513ff2b176d74a3bec114f33b519069788811d
e2fa8b2b1b732d7c25119229e3cdfba8370cb00f
refs/heads/master
1,621,969,960,692
1,586,279,279,000
1,586,279,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
35,581
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis -/ import tactic.ring data.nat.gcd data.list.defs meta.rb_map data.tree /-! # `linarith` A tactic for discharging linear arithmetic goals using Fourier-Motzkin elimination. `linarith` is (in principle) complete for β„š and ℝ. It is not complete for non-dense orders, i.e. β„€. - @TODO: investigate storing comparisons in a list instead of a set, for possible efficiency gains - @TODO: delay proofs of denominator normalization and nat casting until after contradiction is found -/ meta def nat.to_pexpr : β„• β†’ pexpr | 0 := ``(0) | 1 := ``(1) | n := if n % 2 = 0 then ``(bit0 %%(nat.to_pexpr (n/2))) else ``(bit1 %%(nat.to_pexpr (n/2))) open native namespace linarith section lemmas lemma int.coe_nat_bit0 (n : β„•) : (↑(bit0 n : β„•) : β„€) = bit0 (↑n : β„€) := by simp [bit0] lemma int.coe_nat_bit1 (n : β„•) : (↑(bit1 n : β„•) : β„€) = bit1 (↑n : β„€) := by simp [bit1, bit0] lemma int.coe_nat_bit0_mul (n : β„•) (x : β„•) : (↑(bit0 n * x) : β„€) = (↑(bit0 n) : β„€) * (↑x : β„€) := by simp lemma int.coe_nat_bit1_mul (n : β„•) (x : β„•) : (↑(bit1 n * x) : β„€) = (↑(bit1 n) : β„€) * (↑x : β„€) := by simp lemma int.coe_nat_one_mul (x : β„•) : (↑(1 * x) : β„€) = 1 * (↑x : β„€) := by simp lemma int.coe_nat_zero_mul (x : β„•) : (↑(0 * x) : β„€) = 0 * (↑x : β„€) := by simp lemma int.coe_nat_mul_bit0 (n : β„•) (x : β„•) : (↑(x * bit0 n) : β„€) = (↑x : β„€) * (↑(bit0 n) : β„€) := by simp lemma int.coe_nat_mul_bit1 (n : β„•) (x : β„•) : (↑(x * bit1 n) : β„€) = (↑x : β„€) * (↑(bit1 n) : β„€) := by simp lemma int.coe_nat_mul_one (x : β„•) : (↑(x * 1) : β„€) = (↑x : β„€) * 1 := by simp lemma int.coe_nat_mul_zero (x : β„•) : (↑(x * 0) : β„€) = (↑x : β„€) * 0 := by simp lemma nat_eq_subst {n1 n2 : β„•} {z1 z2 : β„€} (hn : n1 = n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 = z2 := by simpa [eq.symm h1, eq.symm h2, int.coe_nat_eq_coe_nat_iff] lemma nat_le_subst {n1 n2 : β„•} {z1 z2 : β„€} (hn : n1 ≀ n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 ≀ z2 := by simpa [eq.symm h1, eq.symm h2, int.coe_nat_le] lemma nat_lt_subst {n1 n2 : β„•} {z1 z2 : β„€} (hn : n1 < n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 < z2 := by simpa [eq.symm h1, eq.symm h2, int.coe_nat_lt] lemma eq_of_eq_of_eq {Ξ±} [ordered_semiring Ξ±] {a b : Ξ±} (ha : a = 0) (hb : b = 0) : a + b = 0 := by simp * lemma le_of_eq_of_le {Ξ±} [ordered_semiring Ξ±] {a b : Ξ±} (ha : a = 0) (hb : b ≀ 0) : a + b ≀ 0 := by simp * lemma lt_of_eq_of_lt {Ξ±} [ordered_semiring Ξ±] {a b : Ξ±} (ha : a = 0) (hb : b < 0) : a + b < 0 := by simp * lemma le_of_le_of_eq {Ξ±} [ordered_semiring Ξ±] {a b : Ξ±} (ha : a ≀ 0) (hb : b = 0) : a + b ≀ 0 := by simp * lemma lt_of_lt_of_eq {Ξ±} [ordered_semiring Ξ±] {a b : Ξ±} (ha : a < 0) (hb : b = 0) : a + b < 0 := by simp * lemma mul_neg {Ξ±} [ordered_ring Ξ±] {a b : Ξ±} (ha : a < 0) (hb : b > 0) : b * a < 0 := have (-b)*a > 0, from mul_pos_of_neg_of_neg (neg_neg_of_pos hb) ha, neg_of_neg_pos (by simpa) lemma mul_nonpos {Ξ±} [ordered_ring Ξ±] {a b : Ξ±} (ha : a ≀ 0) (hb : b > 0) : b * a ≀ 0 := have (-b)*a β‰₯ 0, from mul_nonneg_of_nonpos_of_nonpos (le_of_lt (neg_neg_of_pos hb)) ha, (by simpa) lemma mul_eq {Ξ±} [ordered_semiring Ξ±] {a b : Ξ±} (ha : a = 0) (hb : b > 0) : b * a = 0 := by simp * lemma eq_of_not_lt_of_not_gt {Ξ±} [linear_order Ξ±] (a b : Ξ±) (h1 : Β¬ a < b) (h2 : Β¬ b < a) : a = b := le_antisymm (le_of_not_gt h2) (le_of_not_gt h1) lemma add_subst {Ξ±} [ring Ξ±] {n e1 e2 t1 t2 : Ξ±} (h1 : n * e1 = t1) (h2 : n * e2 = t2) : n * (e1 + e2) = t1 + t2 := by simp [left_distrib, *] lemma sub_subst {Ξ±} [ring Ξ±] {n e1 e2 t1 t2 : Ξ±} (h1 : n * e1 = t1) (h2 : n * e2 = t2) : n * (e1 - e2) = t1 - t2 := by simp [left_distrib, *, sub_eq_add_neg] lemma neg_subst {Ξ±} [ring Ξ±] {n e t : Ξ±} (h1 : n * e = t) : n * (-e) = -t := by simp * private meta def apnn : tactic unit := `[norm_num] lemma mul_subst {Ξ±} [comm_ring Ξ±] {n1 n2 k e1 e2 t1 t2 : Ξ±} (h1 : n1 * e1 = t1) (h2 : n2 * e2 = t2) (h3 : n1*n2 = k . apnn) : k * (e1 * e2) = t1 * t2 := have h3 : n1 * n2 = k, from h3, by rw [←h3, mul_comm n1, mul_assoc n2, ←mul_assoc n1, h1, ←mul_assoc n2, mul_comm n2, mul_assoc, h2] -- OUCH lemma div_subst {Ξ±} [field Ξ±] {n1 n2 k e1 e2 t1 : Ξ±} (h1 : n1 * e1 = t1) (h2 : n2 / e2 = 1) (h3 : n1*n2 = k) : k * (e1 / e2) = t1 := by rw [←h3, mul_assoc, mul_div_comm, h2, ←mul_assoc, h1, mul_comm, one_mul] end lemmas section datatypes @[derive decidable_eq, derive inhabited] inductive ineq | eq | le | lt open ineq def ineq.max : ineq β†’ ineq β†’ ineq | eq a := a | le a := a | lt a := lt def ineq.is_lt : ineq β†’ ineq β†’ bool | eq le := tt | eq lt := tt | le lt := tt | _ _ := ff def ineq.to_string : ineq β†’ string | eq := "=" | le := "≀" | lt := "<" instance : has_to_string ineq := ⟨ineq.to_string⟩ /-- The main datatype for FM elimination. Variables are represented by natural numbers, each of which has an integer coefficient. Index 0 is reserved for constants, i.e. `coeffs.find 0` is the coefficient of 1. The represented term is `coeffs.keys.sum (Ξ» i, coeffs.find i * Var[i])`. str determines the direction of the comparison -- is it < 0, ≀ 0, or = 0? -/ @[derive _root_.inhabited] meta structure comp := (str : ineq) (coeffs : rb_map β„• int) meta inductive comp_source | assump : β„• β†’ comp_source | add : comp_source β†’ comp_source β†’ comp_source | scale : β„• β†’ comp_source β†’ comp_source meta def comp_source.flatten : comp_source β†’ rb_map β„• β„• | (comp_source.assump n) := mk_rb_map.insert n 1 | (comp_source.add c1 c2) := (comp_source.flatten c1).add (comp_source.flatten c2) | (comp_source.scale n c) := (comp_source.flatten c).map (Ξ» v, v * n) meta def comp_source.to_string : comp_source β†’ string | (comp_source.assump e) := to_string e | (comp_source.add c1 c2) := comp_source.to_string c1 ++ " + " ++ comp_source.to_string c2 | (comp_source.scale n c) := to_string n ++ " * " ++ comp_source.to_string c meta instance comp_source.has_to_format : has_to_format comp_source := ⟨λ a, comp_source.to_string a⟩ meta structure pcomp := (c : comp) (src : comp_source) meta def map_lt (m1 m2 : rb_map β„• int) : bool := list.lex (prod.lex (<) (<)) m1.to_list m2.to_list -- make more efficient meta def comp.lt (c1 c2 : comp) : bool := (c1.str.is_lt c2.str) || (c1.str = c2.str) && map_lt c1.coeffs c2.coeffs meta instance comp.has_lt : has_lt comp := ⟨λ a b, comp.lt a b⟩ meta instance pcomp.has_lt : has_lt pcomp := ⟨λ p1 p2, p1.c < p2.c⟩ -- short-circuit type class inference meta instance pcomp.has_lt_dec : decidable_rel ((<) : pcomp β†’ pcomp β†’ Prop) := by apply_instance meta def comp.coeff_of (c : comp) (a : β„•) : β„€ := c.coeffs.zfind a meta def comp.scale (c : comp) (n : β„•) : comp := { c with coeffs := c.coeffs.map ((*) (n : β„€)) } meta def comp.add (c1 c2 : comp) : comp := ⟨c1.str.max c2.str, c1.coeffs.add c2.coeffs⟩ meta def pcomp.scale (c : pcomp) (n : β„•) : pcomp := ⟨c.c.scale n, comp_source.scale n c.src⟩ meta def pcomp.add (c1 c2 : pcomp) : pcomp := ⟨c1.c.add c2.c, comp_source.add c1.src c2.src⟩ meta instance pcomp.to_format : has_to_format pcomp := ⟨λ p, to_fmt p.c.coeffs ++ to_string p.c.str ++ "0"⟩ meta instance comp.to_format : has_to_format comp := ⟨λ p, to_fmt p.coeffs⟩ end datatypes section fm_elim /-- If `c1` and `c2` both contain variable `a` with opposite coefficients, produces `v1`, `v2`, and `c` such that `a` has been cancelled in `c := v1*c1 + v2*c2`. -/ meta def elim_var (c1 c2 : comp) (a : β„•) : option (β„• Γ— β„• Γ— comp) := let v1 := c1.coeff_of a, v2 := c2.coeff_of a in if v1 * v2 < 0 then let vlcm := nat.lcm v1.nat_abs v2.nat_abs, v1' := vlcm / v1.nat_abs, v2' := vlcm / v2.nat_abs in some ⟨v1', v2', comp.add (c1.scale v1') (c2.scale v2')⟩ else none meta def pelim_var (p1 p2 : pcomp) (a : β„•) : option pcomp := do (n1, n2, c) ← elim_var p1.c p2.c a, return ⟨c, comp_source.add (p1.src.scale n1) (p2.src.scale n2)⟩ meta def comp.is_contr (c : comp) : bool := c.coeffs.empty ∧ c.str = ineq.lt meta def pcomp.is_contr (p : pcomp) : bool := p.c.is_contr meta def elim_with_set (a : β„•) (p : pcomp) (comps : rb_set pcomp) : rb_set pcomp := if Β¬ p.c.coeffs.contains a then mk_rb_set.insert p else comps.fold mk_rb_set $ Ξ» pc s, match pelim_var p pc a with | some pc := s.insert pc | none := s end /-- The state for the elimination monad. * `vars`: the set of variables present in `comps` * `comps`: a set of comparisons * `inputs`: a set of pairs of exprs `(t, pf)`, where `t` is a term and `pf` is a proof that `t {<, ≀, =} 0`, indexed by `β„•`. * `has_false`: stores a `pcomp` of `0 < 0` if one has been found TODO: is it more efficient to store comps as a list, to avoid comparisons? -/ meta structure linarith_structure := (vars : rb_set β„•) (comps : rb_set pcomp) @[reducible] meta def linarith_monad := state_t linarith_structure (except_t pcomp id) meta instance : monad linarith_monad := state_t.monad meta instance : monad_except pcomp linarith_monad := state_t.monad_except pcomp meta def get_vars : linarith_monad (rb_set β„•) := linarith_structure.vars <$> get meta def get_var_list : linarith_monad (list β„•) := rb_set.to_list <$> get_vars meta def get_comps : linarith_monad (rb_set pcomp) := linarith_structure.comps <$> get meta def validate : linarith_monad unit := do ⟨_, comps⟩ ← get, match comps.to_list.find (Ξ» p : pcomp, p.is_contr) with | none := return () | some c := throw c end meta def update (vars : rb_set β„•) (comps : rb_set pcomp) : linarith_monad unit := state_t.put ⟨vars, comps⟩ >> validate meta def monad.elim_var (a : β„•) : linarith_monad unit := do vs ← get_vars, when (vs.contains a) $ do comps ← get_comps, let cs' := comps.fold mk_rb_set (Ξ» p s, s.union (elim_with_set a p comps)), update (vs.erase a) cs' meta def elim_all_vars : linarith_monad unit := get_var_list >>= list.mmap' monad.elim_var end fm_elim section parse open ineq tactic meta def map_of_expr_mul_aux (c1 c2 : rb_map β„• β„€) : option (rb_map β„• β„€) := match c1.keys, c2.keys with | [0], _ := some $ c2.scale (c1.zfind 0) | _, [0] := some $ c1.scale (c2.zfind 0) | [], _ := some mk_rb_map | _, [] := some mk_rb_map | _, _ := none end meta def list.mfind {Ξ±} (tac : Ξ± β†’ tactic unit) : list Ξ± β†’ tactic Ξ± | [] := failed | (h::t) := tac h >> return h <|> list.mfind t meta def rb_map.find_defeq (red : transparency) {v} (m : expr_map v) (e : expr) : tactic v := prod.snd <$> list.mfind (Ξ» p, is_def_eq e p.1 red) m.to_list /-- Turns an expression into a map from `β„•` to `β„€`, for use in a `comp` object. The `expr_map` `β„•` argument identifies which expressions have already been assigned numbers. Returns a new map. -/ meta def map_of_expr (red : transparency) : expr_map β„• β†’ expr β†’ tactic (expr_map β„• Γ— rb_map β„• β„€) | m e@`(%%e1 * %%e2) := (do (m', comp1) ← map_of_expr m e1, (m', comp2) ← map_of_expr m' e2, mp ← map_of_expr_mul_aux comp1 comp2, return (m', mp)) <|> (do k ← rb_map.find_defeq red m e, return (m, mk_rb_map.insert k 1)) <|> (let n := m.size + 1 in return (m.insert e n, mk_rb_map.insert n 1)) | m `(%%e1 + %%e2) := do (m', comp1) ← map_of_expr m e1, (m', comp2) ← map_of_expr m' e2, return (m', comp1.add comp2) | m `(%%e1 - %%e2) := do (m', comp1) ← map_of_expr m e1, (m', comp2) ← map_of_expr m' e2, return (m', comp1.add (comp2.scale (-1))) | m `(-%%e) := do (m', comp) ← map_of_expr m e, return (m', comp.scale (-1)) | m e := match e.to_int with | some 0 := return ⟨m, mk_rb_map⟩ | some z := return ⟨m, mk_rb_map.insert 0 z⟩ | none := (do k ← rb_map.find_defeq red m e, return (m, mk_rb_map.insert k 1)) <|> (let n := m.size + 1 in return (m.insert e n, mk_rb_map.insert n 1)) end meta def parse_into_comp_and_expr : expr β†’ option (ineq Γ— expr) | `(%%e < 0) := (ineq.lt, e) | `(%%e ≀ 0) := (ineq.le, e) | `(%%e = 0) := (ineq.eq, e) | _ := none meta def to_comp (red : transparency) (e : expr) (m : expr_map β„•) : tactic (comp Γ— expr_map β„•) := do (iq, e) ← parse_into_comp_and_expr e, (m', comp') ← map_of_expr red m e, return ⟨⟨iq, comp'⟩, m'⟩ meta def to_comp_fold (red : transparency) : expr_map β„• β†’ list expr β†’ tactic (list (option comp) Γ— expr_map β„•) | m [] := return ([], m) | m (h::t) := (do (c, m') ← to_comp red h m, (l, mp) ← to_comp_fold m' t, return (c::l, mp)) <|> (do (l, mp) ← to_comp_fold m t, return (none::l, mp)) /-- Takes a list of proofs of props of the form `t {<, ≀, =} 0`, and creates a `linarith_structure`. -/ meta def mk_linarith_structure (red : transparency) (l : list expr) : tactic (linarith_structure Γ— rb_map β„• (expr Γ— expr)) := do pftps ← l.mmap infer_type, (l', map) ← to_comp_fold red mk_rb_map pftps, let lz := list.enum $ ((l.zip pftps).zip l').filter_map (Ξ» ⟨a, b⟩, prod.mk a <$> b), let prmap := rb_map.of_list $ lz.map (Ξ» ⟨n, x⟩, (n, x.1)), let vars : rb_set β„• := rb_map.set_of_list $ list.range map.size.succ, let pc : rb_set pcomp := rb_map.set_of_list $ lz.map (Ξ» ⟨n, x⟩, ⟨x.2, comp_source.assump n⟩), return (⟨vars, pc⟩, prmap) meta def linarith_monad.run (red : transparency) {Ξ±} (tac : linarith_monad Ξ±) (l : list expr) : tactic ((pcomp βŠ• Ξ±) Γ— rb_map β„• (expr Γ— expr)) := do (struct, inputs) ← mk_linarith_structure red l, match (state_t.run (validate >> tac) struct).run with | (except.ok (a, _)) := return (sum.inr a, inputs) | (except.error contr) := return (sum.inl contr, inputs) end end parse section prove open ineq tactic meta def get_rel_sides : expr β†’ tactic (expr Γ— expr) | `(%%a < %%b) := return (a, b) | `(%%a ≀ %%b) := return (a, b) | `(%%a = %%b) := return (a, b) | `(%%a β‰₯ %%b) := return (a, b) | `(%%a > %%b) := return (a, b) | _ := failed meta def mul_expr (n : β„•) (e : expr) : pexpr := if n = 1 then ``(%%e) else ``(%%(nat.to_pexpr n) * %%e) meta def add_exprs_aux : pexpr β†’ list pexpr β†’ pexpr | p [] := p | p [a] := ``(%%p + %%a) | p (h::t) := add_exprs_aux ``(%%p + %%h) t meta def add_exprs : list pexpr β†’ pexpr | [] := ``(0) | (h::t) := add_exprs_aux h t meta def find_contr (m : rb_set pcomp) : option pcomp := m.keys.find (Ξ» p, p.c.is_contr) meta def ineq_const_mul_nm : ineq β†’ name | lt := ``mul_neg | le := ``mul_nonpos | eq := ``mul_eq meta def ineq_const_nm : ineq β†’ ineq β†’ (name Γ— ineq) | eq eq := (``eq_of_eq_of_eq, eq) | eq le := (``le_of_eq_of_le, le) | eq lt := (``lt_of_eq_of_lt, lt) | le eq := (``le_of_le_of_eq, le) | le le := (`add_nonpos, le) | le lt := (`add_neg_of_nonpos_of_neg, lt) | lt eq := (``lt_of_lt_of_eq, lt) | lt le := (`add_neg_of_neg_of_nonpos, lt) | lt lt := (`add_neg, lt) meta def mk_single_comp_zero_pf (c : β„•) (h : expr) : tactic (ineq Γ— expr) := do tp ← infer_type h, some (iq, e) ← return $ parse_into_comp_and_expr tp, if c = 0 then do e' ← mk_app ``zero_mul [e], return (eq, e') else if c = 1 then return (iq, h) else do nm ← resolve_name (ineq_const_mul_nm iq), tp ← (prod.snd <$> (infer_type h >>= get_rel_sides)) >>= infer_type, cpos ← to_expr ``((%%c.to_pexpr : %%tp) > 0), (_, ex) ← solve_aux cpos `[norm_num, done], -- e' ← mk_app (ineq_const_mul_nm iq) [h, ex], -- this takes many seconds longer in some examples! why? e' ← to_expr ``(%%nm %%h %%ex) ff, return (iq, e') meta def mk_lt_zero_pf_aux (c : ineq) (pf npf : expr) (coeff : β„•) : tactic (ineq Γ— expr) := do (iq, h') ← mk_single_comp_zero_pf coeff npf, let (nm, niq) := ineq_const_nm c iq, n ← resolve_name nm, e' ← to_expr ``(%%n %%pf %%h'), return (niq, e') /-- Takes a list of coefficients `[c]` and list of expressions, of equal length. Each expression is a proof of a prop of the form `t {<, ≀, =} 0`. Produces a proof that the sum of `(c*t) {<, ≀, =} 0`, where the `comp` is as strong as possible. -/ meta def mk_lt_zero_pf : list β„• β†’ list expr β†’ tactic expr | _ [] := fail "no linear hypotheses found" | [c] [h] := prod.snd <$> mk_single_comp_zero_pf c h | (c::ct) (h::t) := do (iq, h') ← mk_single_comp_zero_pf c h, prod.snd <$> (ct.zip t).mfoldl (Ξ» pr ce, mk_lt_zero_pf_aux pr.1 pr.2 ce.2 ce.1) (iq, h') | _ _ := fail "not enough args to mk_lt_zero_pf" meta def term_of_ineq_prf (prf : expr) : tactic expr := do (lhs, _) ← infer_type prf >>= get_rel_sides, return lhs meta structure linarith_config := (discharger : tactic unit := `[ring]) (restrict_type : option Type := none) (restrict_type_reflect : reflected restrict_type . apply_instance) (exfalso : bool := tt) (transparency : transparency := reducible) (split_hypotheses : bool := tt) meta def ineq_pf_tp (pf : expr) : tactic expr := do (_, z) ← infer_type pf >>= get_rel_sides, infer_type z meta def mk_neg_one_lt_zero_pf (tp : expr) : tactic expr := to_expr ``((neg_neg_of_pos zero_lt_one : -1 < (0 : %%tp))) /-- Assumes `e` is a proof that `t = 0`. Creates a proof that `-t = 0`. -/ meta def mk_neg_eq_zero_pf (e : expr) : tactic expr := to_expr ``(neg_eq_zero.mpr %%e) meta def add_neg_eq_pfs : list expr β†’ tactic (list expr) | [] := return [] | (h::t) := do some (iq, tp) ← parse_into_comp_and_expr <$> infer_type h, match iq with | ineq.eq := do nep ← mk_neg_eq_zero_pf h, tl ← add_neg_eq_pfs t, return $ h::nep::tl | _ := list.cons h <$> add_neg_eq_pfs t end /-- Takes a list of proofs of propositions of the form `t {<, ≀, =} 0`, and tries to prove the goal `false`. -/ meta def prove_false_by_linarith1 (cfg : linarith_config) : list expr β†’ tactic unit | [] := fail "no args to linarith" | l@(h::t) := do l' ← add_neg_eq_pfs l, hz ← ineq_pf_tp h >>= mk_neg_one_lt_zero_pf, (sum.inl contr, inputs) ← elim_all_vars.run cfg.transparency (hz::l') | fail "linarith failed to find a contradiction", let coeffs := inputs.keys.map (Ξ» k, (contr.src.flatten.ifind k)), let pfs : list expr := inputs.keys.map (Ξ» k, (inputs.ifind k).1), let zip := (coeffs.zip pfs).filter (Ξ» pr, pr.1 β‰  0), let (coeffs, pfs) := zip.unzip, mls ← zip.mmap (Ξ» pr, do e ← term_of_ineq_prf pr.2, return (mul_expr pr.1 e)), sm ← to_expr $ add_exprs mls, tgt ← to_expr ``(%%sm = 0), (a, b) ← solve_aux tgt (cfg.discharger >> done), pf ← mk_lt_zero_pf coeffs pfs, pftp ← infer_type pf, (_, nep, _) ← rewrite_core b pftp, pf' ← mk_eq_mp nep pf, mk_app `lt_irrefl [pf'] >>= exact end prove section normalize open tactic set_option eqn_compiler.max_steps 50000 meta def rem_neg (prf : expr) : expr β†’ tactic expr | `(_ ≀ _) := to_expr ``(lt_of_not_ge %%prf) | `(_ < _) := to_expr ``(le_of_not_gt %%prf) | `(_ > _) := to_expr ``(le_of_not_gt %%prf) | `(_ β‰₯ _) := to_expr ``(lt_of_not_ge %%prf) | e := failed meta def rearr_comp : expr β†’ expr β†’ tactic expr | prf `(%%a ≀ 0) := return prf | prf `(%%a < 0) := return prf | prf `(%%a = 0) := return prf | prf `(%%a β‰₯ 0) := to_expr ``(neg_nonpos.mpr %%prf) | prf `(%%a > 0) := to_expr ``(neg_neg_of_pos %%prf) | prf `(0 β‰₯ %%a) := to_expr ``(show %%a ≀ 0, from %%prf) | prf `(0 > %%a) := to_expr ``(show %%a < 0, from %%prf) | prf `(0 = %%a) := to_expr ``(eq.symm %%prf) | prf `(0 ≀ %%a) := to_expr ``(neg_nonpos.mpr %%prf) | prf `(0 < %%a) := to_expr ``(neg_neg_of_pos %%prf) | prf `(%%a ≀ %%b) := to_expr ``(sub_nonpos.mpr %%prf) | prf `(%%a < %%b) := to_expr ``(sub_neg_of_lt %%prf) | prf `(%%a = %%b) := to_expr ``(sub_eq_zero.mpr %%prf) | prf `(%%a > %%b) := to_expr ``(sub_neg_of_lt %%prf) | prf `(%%a β‰₯ %%b) := to_expr ``(sub_nonpos.mpr %%prf) | prf `(Β¬ %%t) := do nprf ← rem_neg prf t, tp ← infer_type nprf, rearr_comp nprf tp | prf _ := fail "couldn't rearrange comp" meta def is_numeric : expr β†’ option β„š | `(%%e1 + %%e2) := (+) <$> is_numeric e1 <*> is_numeric e2 | `(%%e1 - %%e2) := has_sub.sub <$> is_numeric e1 <*> is_numeric e2 | `(%%e1 * %%e2) := (*) <$> is_numeric e1 <*> is_numeric e2 | `(%%e1 / %%e2) := (/) <$> is_numeric e1 <*> is_numeric e2 | `(-%%e) := rat.neg <$> is_numeric e | e := e.to_rat meta def find_cancel_factor : expr β†’ β„• Γ— tree β„• | `(%%e1 + %%e2) := let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, lcm := v1.lcm v2 in (lcm, tree.node lcm t1 t2) | `(%%e1 - %%e2) := let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, lcm := v1.lcm v2 in (lcm, tree.node lcm t1 t2) | `(%%e1 * %%e2) := match is_numeric e1, is_numeric e2 with | none, none := (1, tree.node 1 tree.nil tree.nil) | _, _ := let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, pd := v1*v2 in (pd, tree.node pd t1 t2) end | `(%%e1 / %%e2) := match is_numeric e2 with | some q := let (v1, t1) := find_cancel_factor e1, n := v1.lcm q.num.nat_abs in (n, tree.node n t1 (tree.node q.num.nat_abs tree.nil tree.nil)) | none := (1, tree.node 1 tree.nil tree.nil) end | `(-%%e) := find_cancel_factor e | _ := (1, tree.node 1 tree.nil tree.nil) open tree meta def mk_prod_prf : β„• β†’ tree β„• β†’ expr β†’ tactic expr | v (node _ lhs rhs) `(%%e1 + %%e2) := do v1 ← mk_prod_prf v lhs e1, v2 ← mk_prod_prf v rhs e2, mk_app ``add_subst [v1, v2] | v (node _ lhs rhs) `(%%e1 - %%e2) := do v1 ← mk_prod_prf v lhs e1, v2 ← mk_prod_prf v rhs e2, mk_app ``sub_subst [v1, v2] | v (node n lhs@(node ln _ _) rhs) `(%%e1 * %%e2) := do tp ← infer_type e1, v1 ← mk_prod_prf ln lhs e1, v2 ← mk_prod_prf (v/ln) rhs e2, ln' ← tp.of_nat ln, vln' ← tp.of_nat (v/ln), v' ← tp.of_nat v, ntp ← to_expr ``(%%ln' * %%vln' = %%v'), (_, npf) ← solve_aux ntp `[norm_num, done], mk_app ``mul_subst [v1, v2, npf] | v (node n lhs rhs@(node rn _ _)) `(%%e1 / %%e2) := do tp ← infer_type e1, v1 ← mk_prod_prf (v/rn) lhs e1, rn' ← tp.of_nat rn, vrn' ← tp.of_nat (v/rn), n' ← tp.of_nat n, v' ← tp.of_nat v, ntp ← to_expr ``(%%rn' / %%e2 = 1), (_, npf) ← solve_aux ntp `[norm_num, done], ntp2 ← to_expr ``(%%vrn' * %%n' = %%v'), (_, npf2) ← solve_aux ntp2 `[norm_num, done], mk_app ``div_subst [v1, npf, npf2] | v t `(-%%e) := do v' ← mk_prod_prf v t e, mk_app ``neg_subst [v'] | v _ e := do tp ← infer_type e, v' ← tp.of_nat v, e' ← to_expr ``(%%v' * %%e), mk_app `eq.refl [e'] /-- Given `e`, a term with rational division, produces a natural number `n` and a proof of `n*e = e'`, where `e'` has no division. -/ meta def kill_factors (e : expr) : tactic (β„• Γ— expr) := let (n, t) := find_cancel_factor e in do e' ← mk_prod_prf n t e, return (n, e') open expr meta def expr_contains (n : name) : expr β†’ bool | (const nm _) := nm = n | (lam _ _ _ bd) := expr_contains bd | (pi _ _ _ bd) := expr_contains bd | (app e1 e2) := expr_contains e1 || expr_contains e2 | _ := ff lemma sub_into_lt {Ξ±} [ordered_semiring Ξ±] {a b : Ξ±} (he : a = b) (hl : a ≀ 0) : b ≀ 0 := by rwa he at hl meta def norm_hyp_aux (h' lhs : expr) : tactic expr := do (v, lhs') ← kill_factors lhs, if v = 1 then return h' else do (ih, h'') ← mk_single_comp_zero_pf v h', (_, nep, _) ← infer_type h'' >>= rewrite_core lhs', mk_eq_mp nep h'' meta def norm_hyp (h : expr) : tactic expr := do htp ← infer_type h, h' ← rearr_comp h htp, some (c, lhs) ← parse_into_comp_and_expr <$> infer_type h', if expr_contains `has_div.div lhs then norm_hyp_aux h' lhs else return h' meta def get_contr_lemma_name : expr β†’ option name | `(%%a < %%b) := return `lt_of_not_ge | `(%%a ≀ %%b) := return `le_of_not_gt | `(%%a = %%b) := return ``eq_of_not_lt_of_not_gt | `(%%a β‰  %%b) := return `not.intro | `(%%a β‰₯ %%b) := return `le_of_not_gt | `(%%a > %%b) := return `lt_of_not_ge | `(Β¬ %%a < %%b) := return `not.intro | `(Β¬ %%a ≀ %%b) := return `not.intro | `(Β¬ %%a = %%b) := return `not.intro | `(Β¬ %%a β‰₯ %%b) := return `not.intro | `(Β¬ %%a > %%b) := return `not.intro | _ := none /-- Assumes the input `t` is of type `β„•`. Produces `t'` of type `β„€` such that `↑t = t'` and a proof of equality. -/ meta def cast_expr (e : expr) : tactic (expr Γ— expr) := do s ← [`int.coe_nat_add, `int.coe_nat_zero, `int.coe_nat_one, ``int.coe_nat_bit0_mul, ``int.coe_nat_bit1_mul, ``int.coe_nat_zero_mul, ``int.coe_nat_one_mul, ``int.coe_nat_mul_bit0, ``int.coe_nat_mul_bit1, ``int.coe_nat_mul_zero, ``int.coe_nat_mul_one, ``int.coe_nat_bit0, ``int.coe_nat_bit1].mfoldl simp_lemmas.add_simp simp_lemmas.mk, ce ← to_expr ``(↑%%e : β„€), simplify s [] ce {fail_if_unchanged := ff} meta def is_nat_int_coe : expr β†’ option expr | `((↑(%%n : β„•) : β„€)) := some n | _ := none meta def mk_coe_nat_nonneg_prf (e : expr) : tactic expr := mk_app `int.coe_nat_nonneg [e] meta def get_nat_comps : expr β†’ list expr | `(%%a + %%b) := (get_nat_comps a).append (get_nat_comps b) | `(%%a * %%b) := (get_nat_comps a).append (get_nat_comps b) | e := match is_nat_int_coe e with | some e' := [e'] | none := [] end meta def mk_coe_nat_nonneg_prfs (e : expr) : tactic (list expr) := (get_nat_comps e).mmap mk_coe_nat_nonneg_prf meta def mk_cast_eq_and_nonneg_prfs (pf a b : expr) (ln : name) : tactic (list expr) := do (a', prfa) ← cast_expr a, (b', prfb) ← cast_expr b, la ← mk_coe_nat_nonneg_prfs a', lb ← mk_coe_nat_nonneg_prfs b', pf' ← mk_app ln [pf, prfa, prfb], return $ pf'::(la.append lb) meta def mk_int_pfs_of_nat_pf (pf : expr) : tactic (list expr) := do tp ← infer_type pf, match tp with | `(%%a = %%b) := mk_cast_eq_and_nonneg_prfs pf a b ``nat_eq_subst | `(%%a ≀ %%b) := mk_cast_eq_and_nonneg_prfs pf a b ``nat_le_subst | `(%%a < %%b) := mk_cast_eq_and_nonneg_prfs pf a b ``nat_lt_subst | `(%%a β‰₯ %%b) := mk_cast_eq_and_nonneg_prfs pf b a ``nat_le_subst | `(%%a > %%b) := mk_cast_eq_and_nonneg_prfs pf b a ``nat_lt_subst | `(Β¬ %%a ≀ %%b) := do pf' ← mk_app ``lt_of_not_ge [pf], mk_cast_eq_and_nonneg_prfs pf' b a ``nat_lt_subst | `(Β¬ %%a < %%b) := do pf' ← mk_app ``le_of_not_gt [pf], mk_cast_eq_and_nonneg_prfs pf' b a ``nat_le_subst | `(Β¬ %%a β‰₯ %%b) := do pf' ← mk_app ``lt_of_not_ge [pf], mk_cast_eq_and_nonneg_prfs pf' a b ``nat_lt_subst | `(Β¬ %%a > %%b) := do pf' ← mk_app ``le_of_not_gt [pf], mk_cast_eq_and_nonneg_prfs pf' a b ``nat_le_subst | _ := fail "mk_int_pfs_of_nat_pf failed: proof is not an inequality" end meta def mk_non_strict_int_pf_of_strict_int_pf (pf : expr) : tactic expr := do tp ← infer_type pf, match tp with | `(%%a < %%b) := to_expr ``(@cast (%%a < %%b) (%%a + 1 ≀ %%b) (by refl) %%pf) | `(%%a > %%b) := to_expr ``(@cast (%%a > %%b) (%%a β‰₯ %%b + 1) (by refl) %%pf) | `(Β¬ %%a ≀ %%b) := to_expr ``(@cast (%%a > %%b) (%%a β‰₯ %%b + 1) (by refl) (lt_of_not_ge %%pf)) | `(Β¬ %%a β‰₯ %%b) := to_expr ``(@cast (%%a < %%b) (%%a + 1 ≀ %%b) (by refl) (lt_of_not_ge %%pf)) | _ := fail "mk_non_strict_int_pf_of_strict_int_pf failed: proof is not an inequality" end meta def guard_is_nat_prop : expr β†’ tactic unit | `(%%a = _) := infer_type a >>= unify `(β„•) | `(%%a ≀ _) := infer_type a >>= unify `(β„•) | `(%%a < _) := infer_type a >>= unify `(β„•) | `(%%a β‰₯ _) := infer_type a >>= unify `(β„•) | `(%%a > _) := infer_type a >>= unify `(β„•) | `(Β¬ %%p) := guard_is_nat_prop p | _ := failed meta def guard_is_strict_int_prop : expr β†’ tactic unit | `(%%a < _) := infer_type a >>= unify `(β„€) | `(%%a > _) := infer_type a >>= unify `(β„€) | `(Β¬ %%a ≀ _) := infer_type a >>= unify `(β„€) | `(Β¬ %%a β‰₯ _) := infer_type a >>= unify `(β„€) | _ := failed meta def replace_nat_pfs : list expr β†’ tactic (list expr) | [] := return [] | (h::t) := (do infer_type h >>= guard_is_nat_prop, ls ← mk_int_pfs_of_nat_pf h, list.append ls <$> replace_nat_pfs t) <|> list.cons h <$> replace_nat_pfs t meta def replace_strict_int_pfs : list expr β†’ tactic (list expr) | [] := return [] | (h::t) := (do infer_type h >>= guard_is_strict_int_prop, l ← mk_non_strict_int_pf_of_strict_int_pf h, list.cons l <$> replace_strict_int_pfs t) <|> list.cons h <$> replace_strict_int_pfs t meta def partition_by_type_aux : rb_lmap expr expr β†’ list expr β†’ tactic (rb_lmap expr expr) | m [] := return m | m (h::t) := do tp ← ineq_pf_tp h, partition_by_type_aux (m.insert tp h) t meta def partition_by_type (l : list expr) : tactic (rb_lmap expr expr) := partition_by_type_aux mk_rb_map l private meta def try_linarith_on_lists (cfg : linarith_config) (ls : list (list expr)) : tactic unit := (first $ ls.map $ prove_false_by_linarith1 cfg) <|> fail "linarith failed" /-- Takes a list of proofs of propositions. Filters out the proofs of linear (in)equalities, and tries to use them to prove `false`. If `pref_type` is given, starts by working over this type. -/ meta def prove_false_by_linarith (cfg : linarith_config) (pref_type : option expr) (l : list expr) : tactic unit := do l' ← replace_nat_pfs l, l'' ← replace_strict_int_pfs l', ls ← list.reduce_option <$> l''.mmap (Ξ» h, (do s ← norm_hyp h, return (some s)) <|> return none) >>= partition_by_type, pref_type ← (unify pref_type.iget `(β„•) >> return (some `(β„€) : option expr)) <|> return pref_type, match cfg.restrict_type, rb_map.values ls, pref_type with | some rtp, _, _ := do m ← mk_mvar, unify `(some %%m : option Type) cfg.restrict_type_reflect, m ← instantiate_mvars m, prove_false_by_linarith1 cfg (ls.ifind m) | none, [ls'], _ := prove_false_by_linarith1 cfg ls' | none, ls', none := try_linarith_on_lists cfg ls' | none, _, (some t) := prove_false_by_linarith1 cfg (ls.ifind t) <|> try_linarith_on_lists cfg (rb_map.values (ls.erase t)) end end normalize end linarith section open tactic linarith open lean lean.parser interactive tactic interactive.types local postfix `?`:9001 := optional local postfix *:9001 := many meta def linarith.elab_arg_list : option (list pexpr) β†’ tactic (list expr) | none := return [] | (some l) := l.mmap i_to_expr meta def linarith.preferred_type_of_goal : option expr β†’ tactic (option expr) | none := return none | (some e) := some <$> ineq_pf_tp e /-- `linarith.interactive_aux cfg o_goal restrict_hyps args`: * `cfg` is a `linarith_config` object * `o_goal : option expr` is the local constant corresponding to the former goal, if there was one * `restrict_hyps : bool` is `tt` if `linarith only [...]` was used * `args : option (list pexpr)` is the optional list of arguments in `linarith [...]` -/ meta def linarith.interactive_aux (cfg : linarith_config) : option expr β†’ bool β†’ option (list pexpr) β†’ tactic unit | none tt none := fail "linarith only called with no arguments" | none tt (some l) := l.mmap i_to_expr >>= prove_false_by_linarith cfg none | (some e) tt l := do tp ← ineq_pf_tp e, list.cons e <$> linarith.elab_arg_list l >>= prove_false_by_linarith cfg (some tp) | oe ff l := do otp ← linarith.preferred_type_of_goal oe, list.append <$> local_context <*> (list.filter (Ξ» a, bnot $ expr.is_local_constant a) <$> linarith.elab_arg_list l) >>= prove_false_by_linarith cfg otp /-- Tries to prove a goal of `false` by linear arithmetic on hypotheses. If the goal is a linear (in)equality, tries to prove it by contradiction. If the goal is not `false` or an inequality, applies `exfalso` and tries linarith on the hypotheses. * `linarith` will use all relevant hypotheses in the local context. * `linarith [t1, t2, t3]` will add proof terms t1, t2, t3 to the local context. * `linarith only [h1, h2, h3, t1, t2, t3]` will use only the goal (if relevant), local hypotheses `h1`, `h2`, `h3`, and proofs `t1`, `t2`, `t3`. It will ignore the rest of the local context. * `linarith!` will use a stronger reducibility setting to identify atoms. Config options: * `linarith {exfalso := ff}` will fail on a goal that is neither an inequality nor `false` * `linarith {restrict_type := T}` will run only on hypotheses that are inequalities over `T` * `linarith {discharger := tac}` will use `tac` instead of `ring` for normalization. Options: `ring2`, `ring SOP`, `simp` --- `linarith` attempts to find a contradiction between hypotheses that are linear (in)equalities. Equivalently, it can prove a linear inequality by assuming its negation and proving `false`. In theory, `linarith` should prove any goal that is true in the theory of linear arithmetic over the rationals. While there is some special handling for non-dense orders like `nat` and `int`, this tactic is not complete for these theories and will not prove every true goal. An example: ```lean example (x y z : β„š) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : 12*y - 4* z < 0) : false := by linarith ``` `linarith` will use all appropriate hypotheses and the negation of the goal, if applicable. `linarith [t1, t2, t3]` will additionally use proof terms `t1, t2, t3`. `linarith only [h1, h2, h3, t1, t2, t3]` will use only the goal (if relevant), local hypotheses `h1`, `h2`, `h3`, and proofs `t1`, `t2`, `t3`. It will ignore the rest of the local context. `linarith!` will use a stronger reducibility setting to try to identify atoms. For example, ```lean example (x : β„š) : id x β‰₯ x := by linarith ``` will fail, because `linarith` will not identify `x` and `id x`. `linarith!` will. This can sometimes be expensive. `linarith {discharger := tac, restrict_type := tp, exfalso := ff}` takes a config object with five optional arguments: * `discharger` specifies a tactic to be used for reducing an algebraic equation in the proof stage. The default is `ring`. Other options currently include `ring SOP` or `simp` for basic problems. * `restrict_type` will only use hypotheses that are inequalities over `tp`. This is useful if you have e.g. both integer and rational valued inequalities in the local context, which can sometimes confuse the tactic. * `transparency` controls how hard `linarith` will try to match atoms to each other. By default it will only unfold `reducible` definitions. * If `split_hypotheses` is true, `linarith` will split conjunctions in the context into separate hypotheses. * If `exfalso` is false, `linarith` will fail when the goal is neither an inequality nor `false`. (True by default.) -/ meta def tactic.interactive.linarith (red : parse ((tk "!")?)) (restr : parse ((tk "only")?)) (hyps : parse pexpr_list?) (cfg : linarith_config := {}) : tactic unit := let cfg := if red.is_some then {cfg with transparency := semireducible, discharger := `[ring!]} else cfg in do t ← target, when cfg.split_hypotheses (try auto.split_hyps), match get_contr_lemma_name t with | some nm := seq (applyc nm) $ do t ← intro1, linarith.interactive_aux cfg (some t) restr.is_some hyps | none := if cfg.exfalso then exfalso >> linarith.interactive_aux cfg none restr.is_some hyps else fail "linarith failed: target type is not an inequality." end add_hint_tactic "linarith" add_tactic_doc { name := "linarith", category := doc_category.tactic, decl_names := [`tactic.interactive.linarith], tags := ["arithmetic", "decision procedure", "finishing"] } end
02d9a4f63d4434c550c8a36bca3518ea67dc312e
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/undergraduate/MAS114/fiber.lean
e3a60df9eb1ffe176434c0b74853ea7431fc1cff
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,658
lean
import data.fintype import tactic.squeeze namespace MAS114 universes u v variables {Ξ± : Type u} {Ξ² : Type v} (p : Ξ± β†’ Ξ²) variables [fintype Ξ±] [fintype Ξ²] [decidable_eq Ξ±] [decidable_eq Ξ²] def fiber (b : Ξ²) : Type* := { a : Ξ± // p a = b } instance (b : Ξ²) : fintype (fiber p b) := by { dsimp[fiber], apply_instance } def fiber' (b : Ξ²) : finset Ξ± := finset.univ.filter (Ξ» a, p a = b) lemma mem_fiber' (b : Ξ²) (a : Ξ±) : a ∈ fiber' p b ↔ p a = b := ⟨λ h,(finset.mem_filter.mp h).right, Ξ» h,finset.mem_filter.mpr ⟨finset.mem_univ a,h⟩⟩ lemma card_fiber (b : Ξ²) : fintype.card (fiber p b) = (fiber' p b).card := fintype.subtype_card (fiber' p b) (mem_fiber' p b) lemma equiv_fibre_sigma : Ξ± ≃ Ξ£ (b : Ξ²), (fiber p b) := begin let to_fun : Ξ± β†’ Ξ£ (b : Ξ²), (fiber p b) := Ξ» a, ⟨p a,⟨a,rfl⟩⟩, let inv_fun : βˆ€ x : Ξ£ (b : Ξ²), (fiber p b), Ξ± := Ξ» x, x.2.val, let left_inv : function.left_inverse inv_fun to_fun := Ξ» a, by { dsimp[to_fun,inv_fun],refl }, let right_inv : function.right_inverse inv_fun to_fun := Ξ» ⟨b,⟨a,e⟩⟩, begin rcases e, dsimp[inv_fun,to_fun],simp only[heq_iff_eq],split; refl, end, exact ⟨to_fun,inv_fun,left_inv,right_inv⟩, end lemma card_eq_fiber_sum : fintype.card Ξ± = finset.univ.sum (Ξ» b, fintype.card (fiber p b)) := (fintype.card_congr (equiv_fibre_sigma p)).trans (fintype.card_sigma (fiber p)) lemma card_eq_fiber_sum' : fintype.card Ξ± = finset.univ.sum (Ξ» b, finset.card (fiber' p b)) := begin let e0 := card_eq_fiber_sum p, let e1 : βˆ€ b : Ξ², b ∈ finset.univ β†’ fintype.card (fiber p b) = finset.card (fiber' p b) := Ξ» b _, card_fiber p b, let e2 := @finset.sum_congr Ξ² β„• finset.univ _ _ _ _ rfl e1, exact e0.trans e2, end variable {p} lemma fiber_nonempty_of_surjective (p_surj : function.surjective p) (b : Ξ²) : nonempty (fiber p b) := begin rcases p_surj b with ⟨a,e⟩, exact ⟨⟨a,e⟩⟩, end lemma card_le_of_surjective : function.surjective p β†’ (fintype.card Ξ²) ≀ (fintype.card Ξ±) := begin intro p_surj, have h0 : βˆ€ b, b ∈ finset.univ β†’ 1 ≀ fintype.card (fiber p b) := Ξ» b _, fintype.card_pos_iff.mpr (fiber_nonempty_of_surjective p_surj b), let h1 := @finset.sum_le_sum Ξ² β„• finset.univ _ _ _ _ h0, let h2 := calc finset.sum finset.univ (Ξ» b : Ξ², 1) = add_monoid.smul finset.univ.card 1 : @finset.sum_const Ξ² β„• finset.univ _ 1 ... = ↑finset.univ.card : @add_monoid.smul_one β„• _ _ finset.univ.card ... = finset.univ.card : nat.cast_id _ ... = fintype.card Ξ² : rfl, rw[h2,← card_eq_fiber_sum p] at h1, exact h1 end end MAS114
adcde7a097f1433ecdfe7a82fc5ac71672d005bf
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/polynomial/derivative.lean
4b71af02ec0ae64e9350bc1933191229ab1c5646
[ "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
15,239
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.eval /-! # The derivative map on polynomials ## Main definitions * `polynomial.derivative`: The formal derivative of polynomials, expressed as a linear map. -/ noncomputable theory open finset open_locale big_operators classical namespace polynomial universes u v w y z variables {R : Type u} {S : Type v} {T : Type w} {ΞΉ : Type y} {A : Type z} {a b : R} {n : β„•} section derivative section semiring variables [semiring R] /-- `derivative p` is the formal derivative of the polynomial `p` -/ def derivative : polynomial R β†’β‚—[R] polynomial R := { to_fun := Ξ» p, p.sum (Ξ» n a, C (a * n) * X^(n-1)), map_add' := Ξ» p q, by rw sum_add_index; simp only [add_mul, forall_const, ring_hom.map_add, eq_self_iff_true, zero_mul, ring_hom.map_zero], map_smul' := Ξ» a p, by dsimp; rw sum_smul_index; simp only [mul_sum, ← C_mul', mul_assoc, coeff_C_mul, ring_hom.map_mul, forall_const, zero_mul, ring_hom.map_zero, sum] } lemma derivative_apply (p : polynomial R) : derivative p = p.sum (Ξ»n a, C (a * n) * X^(n - 1)) := rfl lemma coeff_derivative (p : polynomial R) (n : β„•) : coeff (derivative p) n = coeff p (n + 1) * (n + 1) := begin rw [derivative_apply], simp only [coeff_X_pow, coeff_sum, coeff_C_mul], rw [sum, finset.sum_eq_single (n + 1)], simp only [nat.add_succ_sub_one, add_zero, mul_one, if_true, eq_self_iff_true], norm_cast, { assume b, cases b, { intros, rw [nat.cast_zero, mul_zero, zero_mul], }, { intros _ H, rw [nat.succ_sub_one b, if_neg (mt (congr_arg nat.succ) H.symm), mul_zero] } }, { rw [if_pos (add_tsub_cancel_right n 1).symm, mul_one, nat.cast_add, nat.cast_one, mem_support_iff], intro h, push_neg at h, simp [h], }, end @[simp] lemma derivative_zero : derivative (0 : polynomial R) = 0 := derivative.map_zero @[simp] lemma iterate_derivative_zero {k : β„•} : derivative^[k] (0 : polynomial R) = 0 := begin induction k with k ih, { simp, }, { simp [ih], }, end @[simp] lemma derivative_monomial (a : R) (n : β„•) : derivative (monomial n a) = monomial (n - 1) (a * n) := by { rw [derivative_apply, sum_monomial_index, C_mul_X_pow_eq_monomial], simp } lemma derivative_C_mul_X_pow (a : R) (n : β„•) : derivative (C a * X ^ n) = C (a * n) * X^(n - 1) := by rw [C_mul_X_pow_eq_monomial, C_mul_X_pow_eq_monomial, derivative_monomial] @[simp] lemma derivative_X_pow (n : β„•) : derivative (X ^ n : polynomial R) = (n : polynomial R) * X ^ (n - 1) := by convert derivative_C_mul_X_pow (1 : R) n; simp @[simp] lemma derivative_C {a : R} : derivative (C a) = 0 := by simp [derivative_apply] @[simp] lemma derivative_X : derivative (X : polynomial R) = 1 := (derivative_monomial _ _).trans $ by simp @[simp] lemma derivative_one : derivative (1 : polynomial R) = 0 := derivative_C @[simp] lemma derivative_bit0 {a : polynomial R} : derivative (bit0 a) = bit0 (derivative a) := by simp [bit0] @[simp] lemma derivative_bit1 {a : polynomial R} : derivative (bit1 a) = bit0 (derivative a) := by simp [bit1] @[simp] lemma derivative_add {f g : polynomial R} : derivative (f + g) = derivative f + derivative g := derivative.map_add f g @[simp] lemma iterate_derivative_add {f g : polynomial R} {k : β„•} : derivative^[k] (f + g) = (derivative^[k] f) + (derivative^[k] g) := derivative.to_add_monoid_hom.iterate_map_add _ _ _ @[simp] lemma derivative_neg {R : Type*} [ring R] (f : polynomial R) : derivative (-f) = - derivative f := linear_map.map_neg derivative f @[simp] lemma iterate_derivative_neg {R : Type*} [ring R] {f : polynomial R} {k : β„•} : derivative^[k] (-f) = - (derivative^[k] f) := (@derivative R _).to_add_monoid_hom.iterate_map_neg _ _ @[simp] lemma derivative_sub {R : Type*} [ring R] {f g : polynomial R} : derivative (f - g) = derivative f - derivative g := linear_map.map_sub derivative f g @[simp] lemma iterate_derivative_sub {R : Type*} [ring R] {k : β„•} {f g : polynomial R} : derivative^[k] (f - g) = (derivative^[k] f) - (derivative^[k] g) := begin induction k with k ih generalizing f g, { simp [nat.iterate], }, { simp [nat.iterate, ih], } end @[simp] lemma derivative_sum {s : finset ΞΉ} {f : ΞΉ β†’ polynomial R} : derivative (βˆ‘ b in s, f b) = βˆ‘ b in s, derivative (f b) := derivative.map_sum @[simp] lemma derivative_smul (r : R) (p : polynomial R) : derivative (r β€’ p) = r β€’ derivative p := derivative.map_smul _ _ @[simp] lemma iterate_derivative_smul (r : R) (p : polynomial R) (k : β„•) : derivative^[k] (r β€’ p) = r β€’ (derivative^[k] p) := begin induction k with k ih generalizing p, { simp, }, { simp [ih], }, end /-- We can't use `derivative_mul` here because we want to prove this statement also for noncommutative rings.-/ @[simp] lemma derivative_C_mul (a : R) (p : polynomial R) : derivative (C a * p) = C a * derivative p := by convert derivative_smul a p; apply C_mul' @[simp] lemma iterate_derivative_C_mul (a : R) (p : polynomial R) (k : β„•) : derivative^[k] (C a * p) = C a * (derivative^[k] p) := by convert iterate_derivative_smul a p k; apply C_mul' end semiring section comm_semiring variables [comm_semiring R] lemma derivative_eval (p : polynomial R) (x : R) : p.derivative.eval x = p.sum (Ξ» n a, (a * n)*x^(n-1)) := by simp only [derivative_apply, eval_sum, eval_pow, eval_C, eval_X, eval_nat_cast, eval_mul] @[simp] lemma derivative_mul {f g : polynomial R} : derivative (f * g) = derivative f * g + f * derivative g := calc derivative (f * g) = f.sum (Ξ»n a, g.sum (Ξ»m b, C ((a * b) * (n + m : β„•)) * X^((n + m) - 1))) : begin rw mul_eq_sum_sum, transitivity, exact derivative_sum, transitivity, { apply finset.sum_congr rfl, assume x hx, exact derivative_sum }, apply finset.sum_congr rfl, assume n hn, apply finset.sum_congr rfl, assume m hm, transitivity, { apply congr_arg, exact monomial_eq_C_mul_X }, exact derivative_C_mul_X_pow _ _ end ... = f.sum (Ξ»n a, g.sum (Ξ»m b, (C (a * n) * X^(n - 1)) * (C b * X^m) + (C a * X^n) * (C (b * m) * X^(m - 1)))) : sum_congr rfl $ assume n hn, sum_congr rfl $ assume m hm, by simp only [nat.cast_add, mul_add, add_mul, C_add, C_mul]; cases n; simp only [nat.succ_sub_succ, pow_zero]; cases m; simp only [nat.cast_zero, C_0, nat.succ_sub_succ, zero_mul, mul_zero, nat.add_succ, tsub_zero, pow_zero, pow_add, one_mul, pow_succ, mul_comm, mul_left_comm] ... = derivative f * g + f * derivative g : begin conv { to_rhs, congr, { rw [← sum_C_mul_X_eq g] }, { rw [← sum_C_mul_X_eq f] } }, simp only [sum, sum_add_distrib, finset.mul_sum, finset.sum_mul, derivative_apply] end theorem derivative_pow_succ (p : polynomial R) (n : β„•) : (p ^ (n + 1)).derivative = (n + 1) * (p ^ n) * p.derivative := nat.rec_on n (by rw [pow_one, nat.cast_zero, zero_add, one_mul, pow_zero, one_mul]) $ Ξ» n ih, by rw [pow_succ', derivative_mul, ih, mul_right_comm, ← add_mul, add_mul (n.succ : polynomial R), one_mul, pow_succ', mul_assoc, n.cast_succ] theorem derivative_pow (p : polynomial R) (n : β„•) : (p ^ n).derivative = n * (p ^ (n - 1)) * p.derivative := nat.cases_on n (by rw [pow_zero, derivative_one, nat.cast_zero, zero_mul, zero_mul]) $ Ξ» n, by rw [p.derivative_pow_succ n, n.succ_sub_one, n.cast_succ] lemma derivative_comp (p q : polynomial R) : (p.comp q).derivative = q.derivative * p.derivative.comp q := begin apply polynomial.induction_on' p, { intros p₁ pβ‚‚ h₁ hβ‚‚, simp [h₁, hβ‚‚, mul_add], }, { intros n r, simp only [derivative_pow, derivative_mul, monomial_comp, derivative_monomial, derivative_C, zero_mul, C_eq_nat_cast, zero_add, ring_hom.map_mul], -- is there a tactic for this? (a multiplicative `abel`): rw [mul_comm (derivative q)], simp only [mul_assoc], } end @[simp] theorem derivative_map [comm_semiring S] (p : polynomial R) (f : R β†’+* S) : (p.map f).derivative = p.derivative.map f := polynomial.induction_on p (Ξ» r, by rw [map_C, derivative_C, derivative_C, map_zero]) (Ξ» p q ihp ihq, by rw [map_add, derivative_add, ihp, ihq, derivative_add, map_add]) (Ξ» n r ih, by rw [map_mul, map_C, map_pow, map_X, derivative_mul, derivative_pow_succ, derivative_C, zero_mul, zero_add, derivative_X, mul_one, derivative_mul, derivative_pow_succ, derivative_C, zero_mul, zero_add, derivative_X, mul_one, map_mul, map_C, map_mul, map_pow, map_add, map_nat_cast, map_one, map_X]) @[simp] theorem iterate_derivative_map [comm_semiring S] (p : polynomial R) (f : R β†’+* S) (k : β„•): polynomial.derivative^[k] (p.map f) = (polynomial.derivative^[k] p).map f := begin induction k with k ih generalizing p, { simp, }, { simp [ih], }, end /-- Chain rule for formal derivative of polynomials. -/ theorem derivative_evalβ‚‚_C (p q : polynomial R) : (p.evalβ‚‚ C q).derivative = p.derivative.evalβ‚‚ C q * q.derivative := polynomial.induction_on p (Ξ» r, by rw [evalβ‚‚_C, derivative_C, evalβ‚‚_zero, zero_mul]) (Ξ» p₁ pβ‚‚ ih₁ ihβ‚‚, by rw [evalβ‚‚_add, derivative_add, ih₁, ihβ‚‚, derivative_add, evalβ‚‚_add, add_mul]) (Ξ» n r ih, by rw [pow_succ', ← mul_assoc, evalβ‚‚_mul, evalβ‚‚_X, derivative_mul, ih, @derivative_mul _ _ _ X, derivative_X, mul_one, evalβ‚‚_add, @evalβ‚‚_mul _ _ _ _ X, evalβ‚‚_X, add_mul, mul_right_comm]) theorem derivative_prod {s : multiset ΞΉ} {f : ΞΉ β†’ polynomial R} : (multiset.map f s).prod.derivative = (multiset.map (Ξ» i, (multiset.map f (s.erase i)).prod * (f i).derivative) s).sum := begin refine multiset.induction_on s (by simp) (Ξ» i s h, _), rw [multiset.map_cons, multiset.prod_cons, derivative_mul, multiset.map_cons _ i s, multiset.sum_cons, multiset.erase_cons_head, mul_comm (f i).derivative], congr, rw [h, ← add_monoid_hom.coe_mul_left, (add_monoid_hom.mul_left (f i)).map_multiset_sum _, add_monoid_hom.coe_mul_left], simp only [function.comp_app, multiset.map_map], congr' 1, refine multiset.map_congr (Ξ» j hj, _), simp only [function.comp_app], rw [← mul_assoc, ← multiset.prod_cons, ← multiset.map_cons], congr' 1, by_cases hij : i = j, { simp [hij, ← multiset.prod_cons, ← multiset.map_cons, multiset.cons_erase hj] }, { simp [hij] } end theorem of_mem_support_derivative {p : polynomial R} {n : β„•} (h : n ∈ p.derivative.support) : n + 1 ∈ p.support := mem_support_iff.2 $ Ξ» (h1 : p.coeff (n+1) = 0), mem_support_iff.1 h $ show p.derivative.coeff n = 0, by rw [coeff_derivative, h1, zero_mul] theorem degree_derivative_lt {p : polynomial R} (hp : p β‰  0) : p.derivative.degree < p.degree := (finset.sup_lt_iff $ bot_lt_iff_ne_bot.2 $ mt degree_eq_bot.1 hp).2 $ Ξ» n hp, lt_of_lt_of_le (with_bot.some_lt_some.2 n.lt_succ_self) $ finset.le_sup $ of_mem_support_derivative hp theorem nat_degree_derivative_lt {p : polynomial R} (hp : p.derivative β‰  0) : p.derivative.nat_degree < p.nat_degree := have hp1 : p β‰  0, from Ξ» h, hp $ by rw [h, derivative_zero], with_bot.some_lt_some.1 $ begin rw [nat_degree, option.get_or_else_of_ne_none $ mt degree_eq_bot.1 hp, nat_degree, option.get_or_else_of_ne_none $ mt degree_eq_bot.1 hp1], exact degree_derivative_lt hp1 end theorem degree_derivative_le {p : polynomial R} : p.derivative.degree ≀ p.degree := if H : p = 0 then le_of_eq $ by rw [H, derivative_zero] else le_of_lt $ degree_derivative_lt H /-- The formal derivative of polynomials, as linear homomorphism. -/ def derivative_lhom (R : Type*) [comm_ring R] : polynomial R β†’β‚—[R] polynomial R := { to_fun := derivative, map_add' := Ξ» p q, derivative_add, map_smul' := Ξ» r p, derivative_smul r p } @[simp] lemma derivative_lhom_coe {R : Type*} [comm_ring R] : (polynomial.derivative_lhom R : polynomial R β†’ polynomial R) = polynomial.derivative := rfl @[simp] lemma derivative_cast_nat {n : β„•} : derivative (n : polynomial R) = 0 := begin rw ← C.map_nat_cast n, exact derivative_C, end @[simp] lemma iterate_derivative_cast_nat_mul {n k : β„•} {f : polynomial R} : derivative^[k] (n * f) = n * (derivative^[k] f) := begin induction k with k ih generalizing f, { simp [nat.iterate], }, { simp [nat.iterate, ih], } end end comm_semiring section comm_ring variables [comm_ring R] lemma derivative_comp_one_sub_X (p : polynomial R) : (p.comp (1-X)).derivative = -p.derivative.comp (1-X) := by simp [derivative_comp] @[simp] lemma iterate_derivative_comp_one_sub_X (p : polynomial R) (k : β„•) : derivative^[k] (p.comp (1-X)) = (-1)^k * (derivative^[k] p).comp (1-X) := begin induction k with k ih generalizing p, { simp, }, { simp [ih p.derivative, iterate_derivative_neg, derivative_comp, pow_succ], }, end lemma eval_multiset_prod_X_sub_C_derivative {S : multiset R} {r : R} (hr : r ∈ S) : eval r (multiset.map (Ξ» a, X - C a) S).prod.derivative = (multiset.map (Ξ» a, r - a) (S.erase r)).prod := begin nth_rewrite 0 [← multiset.cons_erase hr], simpa using (eval_ring_hom r).map_multiset_prod (multiset.map (Ξ» a, X - C a) (S.erase r)), end end comm_ring section is_domain variables [ring R] [is_domain R] lemma mem_support_derivative [char_zero R] (p : polynomial R) (n : β„•) : n ∈ (derivative p).support ↔ n + 1 ∈ p.support := suffices (Β¬(coeff p (n + 1) = 0 ∨ ((n + 1:β„•) : R) = 0)) ↔ coeff p (n + 1) β‰  0, by simpa only [mem_support_iff, coeff_derivative, ne.def, mul_eq_zero], by { rw [nat.cast_eq_zero], simp only [nat.succ_ne_zero, or_false] } @[simp] lemma degree_derivative_eq [char_zero R] (p : polynomial R) (hp : 0 < nat_degree p) : degree (derivative p) = (nat_degree p - 1 : β„•) := begin have h0 : p β‰  0, { contrapose! hp, simp [hp] }, apply le_antisymm, { rw derivative_apply, apply le_trans (degree_sum_le _ _) (sup_le (Ξ» n hn, _)), apply le_trans (degree_C_mul_X_pow_le _ _) (with_bot.coe_le_coe.2 (tsub_le_tsub_right _ _)), apply le_nat_degree_of_mem_supp _ hn }, { refine le_sup _, rw [mem_support_derivative, tsub_add_cancel_of_le, mem_support_iff], { show Β¬ leading_coeff p = 0, rw [leading_coeff_eq_zero], assume h, rw [h, nat_degree_zero] at hp, exact lt_irrefl 0 (lt_of_le_of_lt (zero_le _) hp), }, exact hp } end theorem nat_degree_eq_zero_of_derivative_eq_zero [char_zero R] {f : polynomial R} (h : f.derivative = 0) : f.nat_degree = 0 := begin by_cases hf : f = 0, { exact (congr_arg polynomial.nat_degree hf).trans rfl }, { rw nat_degree_eq_zero_iff_degree_le_zero, by_contra absurd, have f_nat_degree_pos : 0 < f.nat_degree, { rwa [not_le, ←nat_degree_pos_iff_degree_pos] at absurd }, let m := f.nat_degree - 1, have hm : m + 1 = f.nat_degree := tsub_add_cancel_of_le f_nat_degree_pos, have h2 := coeff_derivative f m, rw polynomial.ext_iff at h, rw [h m, coeff_zero, zero_eq_mul] at h2, cases h2, { rw [hm, ←leading_coeff, leading_coeff_eq_zero] at h2, exact hf h2, }, { norm_cast at h2 } } end end is_domain end derivative end polynomial
aa38183195c40d2e70795fe77d921a4f8d10f1f9
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Compiler/Specialize.lean
5626752321841b0093563b900d1686062751ccd8
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,462
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.Attributes import Lean.Compiler.Util namespace Lean namespace Compiler inductive SpecializeAttributeKind | specialize | nospecialize namespace SpecializeAttributeKind instance : Inhabited SpecializeAttributeKind := ⟨SpecializeAttributeKind.specialize⟩ protected def beq : SpecializeAttributeKind β†’ SpecializeAttributeKind β†’ Bool | specialize, specialize => true | nospecialize, nospecialize => true | _, _ => false instance : HasBeq SpecializeAttributeKind := ⟨SpecializeAttributeKind.beq⟩ end SpecializeAttributeKind def mkSpecializeAttrs : IO (EnumAttributes SpecializeAttributeKind) := registerEnumAttributes `specializeAttrs [(`specialize, "mark definition to always be specialized", SpecializeAttributeKind.specialize), (`nospecialize, "mark definition to never be specialized", SpecializeAttributeKind.nospecialize) ] /- TODO: fix the following hack. We need to use the following hack because the equation compiler generates auxiliary definitions that are compiled before we even finish the elaboration of the current command. So, if the current command is a `@[specialize] def foo ...`, we must set the attribute `[specialize]` before we start elaboration, otherwise when we compile the auxiliary definitions we will not be able to test whether `@[specialize]` has been set or not. In the new equation compiler we should pass all attributes and allow it to apply them to auxiliary definitions. In the current implementation, we workaround this issue by using functions such as `hasSpecializeAttrAux`. -/ (fun declName _ => pure ()) AttributeApplicationTime.beforeElaboration @[init mkSpecializeAttrs] constant specializeAttrs : EnumAttributes SpecializeAttributeKind := arbitrary _ private partial def hasSpecializeAttrAux (env : Environment) (kind : SpecializeAttributeKind) : Name β†’ Bool | n => match specializeAttrs.getValue env n with | some k => kind == k | none => if n.isInternal then hasSpecializeAttrAux n.getPrefix else false @[export lean_has_specialize_attribute] def hasSpecializeAttribute (env : Environment) (n : Name) : Bool := hasSpecializeAttrAux env SpecializeAttributeKind.specialize n @[export lean_has_nospecialize_attribute] def hasNospecializeAttribute (env : Environment) (n : Name) : Bool := hasSpecializeAttrAux env SpecializeAttributeKind.nospecialize n inductive SpecArgKind | fixed | fixedNeutral -- computationally neutral | fixedHO -- higher order | fixedInst -- type class instance | other structure SpecInfo := (mutualDecls : List Name) (argKinds : SpecArgKind) structure SpecState := (specInfo : SMap Name SpecInfo := {}) (cache : SMap Expr Name := {}) inductive SpecEntry | info (name : Name) (info : SpecInfo) | cache (key : Expr) (fn : Name) namespace SpecState instance : Inhabited SpecState := ⟨{}⟩ def addEntry (s : SpecState) (e : SpecEntry) : SpecState := match e with | SpecEntry.info name info => { s with specInfo := s.specInfo.insert name info } | SpecEntry.cache key fn => { s with cache := s.cache.insert key fn } def switch : SpecState β†’ SpecState | ⟨m₁, mβ‚‚βŸ© => ⟨m₁.switch, mβ‚‚.switch⟩ end SpecState def mkSpecExtension : IO (SimplePersistentEnvExtension SpecEntry SpecState) := registerSimplePersistentEnvExtension { name := `specialize, addEntryFn := SpecState.addEntry, addImportedFn := fun es => (mkStateFromImportedEntries SpecState.addEntry {} es).switch } @[init mkSpecExtension] constant specExtension : SimplePersistentEnvExtension SpecEntry SpecState := arbitrary _ @[export lean_add_specialization_info] def addSpecializationInfo (env : Environment) (fn : Name) (info : SpecInfo) : Environment := specExtension.addEntry env (SpecEntry.info fn info) @[export lean_get_specialization_info] def getSpecializationInfo (env : Environment) (fn : Name) : Option SpecInfo := (specExtension.getState env).specInfo.find? fn @[export lean_cache_specialization] def cacheSpecialization (env : Environment) (e : Expr) (fn : Name) : Environment := specExtension.addEntry env (SpecEntry.cache e fn) @[export lean_get_cached_specialization] def getCachedSpecialization (env : Environment) (e : Expr) : Option Name := (specExtension.getState env).cache.find? e end Compiler end Lean
046c585fc0e6289eeb78f64f69eb64842fe27a1e
ad0c7d243dc1bd563419e2767ed42fb323d7beea
/data/set/lattice.lean
e3254a623d182e1e453aa76ea3b48fb00f6b171a
[ "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
26,270
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors Jeremy Avigad, Leonardo de Moura, Johannes HΓΆlzl, Mario Carneiro -- QUESTION: can make the first argument in βˆ€ x ∈ a, ... implicit? -/ import logic.basic data.set.basic data.equiv.basic tactic import order.complete_boolean_algebra category.basic import tactic.finish data.sigma.basic order.galois_connection open function tactic set lattice auto universes u v w x variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {ΞΉ : Sort x} namespace set instance lattice_set : complete_lattice (set Ξ±) := { lattice.complete_lattice . le := (βŠ†), le_refl := subset.refl, le_trans := assume a b c, subset.trans, le_antisymm := assume a b, subset.antisymm, lt := Ξ» x y, x βŠ† y ∧ Β¬ y βŠ† x, lt_iff_le_not_le := Ξ» x y, iff.refl _, sup := (βˆͺ), le_sup_left := subset_union_left, le_sup_right := subset_union_right, sup_le := assume a b c, union_subset, inf := (∩), inf_le_left := inter_subset_left, inf_le_right := inter_subset_right, le_inf := assume a b c, subset_inter, top := {a | true }, le_top := assume s a h, trivial, bot := βˆ…, bot_le := assume s a, false.elim, Sup := Ξ»s, {a | βˆƒ t ∈ s, a ∈ t }, le_Sup := assume s t t_in a a_in, ⟨t, ⟨t_in, a_in⟩⟩, Sup_le := assume s t h a ⟨t', ⟨t'_in, a_in⟩⟩, h t' t'_in a_in, Inf := Ξ»s, {a | βˆ€ t ∈ s, a ∈ t }, le_Inf := assume s t h a a_in t' t'_in, h t' t'_in a_in, Inf_le := assume s t t_in a h, h _ t_in } instance : distrib_lattice (set Ξ±) := { le_sup_inf := Ξ» s t u x, or_and_distrib_left.2, ..set.lattice_set } lemma monotone_image {f : Ξ± β†’ Ξ²} : monotone (image f) := assume s t, assume h : s βŠ† t, image_subset _ h section galois_connection variables {f : Ξ± β†’ Ξ²} protected lemma image_preimage : galois_connection (image f) (preimage f) := assume a b, image_subset_iff def kern_image (f : Ξ± β†’ Ξ²) (s : set Ξ±) : set Ξ² := {y | βˆ€x, f x = y β†’ x ∈ s} protected lemma preimage_kern_image : galois_connection (preimage f) (kern_image f) := assume a b, ⟨ assume h x hx y hy, have f y ∈ a, from hy.symm β–Έ hx, h this, assume h x (hx : f x ∈ a), h hx x rfl⟩ end galois_connection /- union and intersection over a family of sets indexed by a type -/ /-- Indexed union of a family of sets -/ @[reducible] def Union (s : ΞΉ β†’ set Ξ²) : set Ξ² := supr s /-- Indexed intersection of a family of sets -/ @[reducible] def Inter (s : ΞΉ β†’ set Ξ²) : set Ξ² := infi s notation `⋃` binders `, ` r:(scoped f, Union f) := r notation `β‹‚` binders `, ` r:(scoped f, Inter f) := r @[simp] theorem mem_Union {x : Ξ²} {s : ΞΉ β†’ set Ξ²} : x ∈ Union s ↔ βˆƒ i, x ∈ s i := ⟨assume ⟨t, ⟨⟨a, (t_eq : t = s a)⟩, (h : x ∈ t)⟩⟩, ⟨a, t_eq β–Έ h⟩, assume ⟨a, h⟩, ⟨s a, ⟨⟨a, rfl⟩, h⟩⟩⟩ /- alternative proof: dsimp [Union, supr, Sup]; simp -/ -- TODO: more rewrite rules wrt forall / existentials and logical connectives -- TODO: also eliminate βˆƒi, ... ∧ i = t ∧ ... @[simp] theorem mem_Inter {x : Ξ²} {s : ΞΉ β†’ set Ξ²} : x ∈ Inter s ↔ βˆ€ i, x ∈ s i := ⟨assume (h : βˆ€a ∈ {a : set Ξ² | βˆƒi, a = s i}, x ∈ a) a, h (s a) ⟨a, rfl⟩, assume h t ⟨a, (eq : t = s a)⟩, eq.symm β–Έ h a⟩ theorem Union_subset {s : ΞΉ β†’ set Ξ²} {t : set Ξ²} (h : βˆ€ i, s i βŠ† t) : (⋃ i, s i) βŠ† t := -- TODO: should be simpler when sets' order is based on lattices @supr_le (set Ξ²) _ set.lattice_set _ _ h theorem Union_subset_iff {Ξ± : Sort u} {s : Ξ± β†’ set Ξ²} {t : set Ξ²} : (⋃ i, s i) βŠ† t ↔ (βˆ€ i, s i βŠ† t):= ⟨assume h i, subset.trans (le_supr s _) h, Union_subset⟩ theorem mem_Inter_of_mem {Ξ± : Sort u} {x : Ξ²} {s : Ξ± β†’ set Ξ²} : (βˆ€ i, x ∈ s i) β†’ (x ∈ β‹‚ i, s i) := assume h t ⟨a, (eq : t = s a)⟩, eq.symm β–Έ h a theorem subset_Inter {t : set Ξ²} {s : Ξ± β†’ set Ξ²} (h : βˆ€ i, t βŠ† s i) : t βŠ† β‹‚ i, s i := -- TODO: should be simpler when sets' order is based on lattices @le_infi (set Ξ²) _ set.lattice_set _ _ h theorem subset_Union : βˆ€ (s : ΞΉ β†’ set Ξ²) (i : ΞΉ), s i βŠ† (⋃ i, s i) := le_supr theorem Inter_subset : βˆ€ (s : ΞΉ β†’ set Ξ²) (i : ΞΉ), (β‹‚ i, s i) βŠ† s i := infi_le theorem Union_const [inhabited ΞΉ] (s : set Ξ²) : (⋃ i:ΞΉ, s) = s := ext $ by simp theorem Inter_const [inhabited ΞΉ] (s : set Ξ²) : (β‹‚ i:ΞΉ, s) = s := ext $ by simp @[simp] -- complete_boolean_algebra theorem compl_Union (s : ΞΉ β†’ set Ξ²) : - (⋃ i, s i) = (β‹‚ i, - s i) := ext (by simp) -- classical -- complete_boolean_algebra theorem compl_Inter (s : ΞΉ β†’ set Ξ²) : -(β‹‚ i, s i) = (⋃ i, - s i) := ext (Ξ» x, by simp [classical.not_forall]) -- classical -- complete_boolean_algebra theorem Union_eq_comp_Inter_comp (s : ΞΉ β†’ set Ξ²) : (⋃ i, s i) = - (β‹‚ i, - s i) := by simp [compl_Inter, compl_compl] -- classical -- complete_boolean_algebra theorem Inter_eq_comp_Union_comp (s : ΞΉ β†’ set Ξ²) : (β‹‚ i, s i) = - (⋃ i, -s i) := by simp [compl_compl] theorem inter_Union_left (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s ∩ (⋃ i, t i) = ⋃ i, s ∩ t i := ext $ by simp theorem inter_Union_right (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : (⋃ i, t i) ∩ s = ⋃ i, t i ∩ s := ext $ by simp theorem Union_union_distrib (s : ΞΉ β†’ set Ξ²) (t : ΞΉ β†’ set Ξ²) : (⋃ i, s i βˆͺ t i) = (⋃ i, s i) βˆͺ (⋃ i, t i) := ext $ by simp [exists_or_distrib] theorem Inter_inter_distrib (s : ΞΉ β†’ set Ξ²) (t : ΞΉ β†’ set Ξ²) : (β‹‚ i, s i ∩ t i) = (β‹‚ i, s i) ∩ (β‹‚ i, t i) := ext $ by simp [forall_and_distrib] theorem union_Union_left [inhabited ΞΉ] (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s βˆͺ (⋃ i, t i) = ⋃ i, s βˆͺ t i := by rw [Union_union_distrib, Union_const] theorem union_Union_right [inhabited ΞΉ] (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : (⋃ i, t i) βˆͺ s = ⋃ i, t i βˆͺ s := by rw [Union_union_distrib, Union_const] theorem inter_Inter_left [inhabited ΞΉ] (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s ∩ (β‹‚ i, t i) = β‹‚ i, s ∩ t i := by rw [Inter_inter_distrib, Inter_const] theorem inter_Inter_right [inhabited ΞΉ] (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : (β‹‚ i, t i) ∩ s = β‹‚ i, t i ∩ s := by rw [Inter_inter_distrib, Inter_const] -- classical theorem union_Inter_left (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s βˆͺ (β‹‚ i, t i) = β‹‚ i, s βˆͺ t i := ext $ assume x, by simp [classical.forall_or_distrib_left] theorem diff_Union_right (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : (⋃ i, t i) \ s = ⋃ i, t i \ s := inter_Union_right _ _ theorem diff_Union_left [inhabited ΞΉ] (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s \ (⋃ i, t i) = β‹‚ i, s \ t i := by rw [diff_eq, compl_Union, inter_Inter_left]; refl theorem diff_Inter_left (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s \ (β‹‚ i, t i) = ⋃ i, s \ t i := by rw [diff_eq, compl_Inter, inter_Union_left]; refl /- bounded unions and intersections -/ theorem mem_bUnion_iff {s : set Ξ±} {t : Ξ± β†’ set Ξ²} {y : Ξ²} : y ∈ (⋃ x ∈ s, t x) ↔ βˆƒ x, x ∈ s ∧ y ∈ t x := by simp theorem mem_bInter_iff {s : set Ξ±} {t : Ξ± β†’ set Ξ²} {y : Ξ²} : y ∈ (β‹‚ x ∈ s, t x) ↔ βˆ€ x ∈ s, y ∈ t x := by simp theorem mem_bUnion {s : set Ξ±} {t : Ξ± β†’ set Ξ²} {x : Ξ±} {y : Ξ²} (xs : x ∈ s) (ytx : y ∈ t x) : y ∈ ⋃ x ∈ s, t x := by simp; exact ⟨x, ⟨xs, ytx⟩⟩ theorem mem_bInter {s : set Ξ±} {t : Ξ± β†’ set Ξ²} {y : Ξ²} (h : βˆ€ x ∈ s, y ∈ t x) : y ∈ β‹‚ x ∈ s, t x := by simp; assumption theorem bUnion_subset {s : set Ξ±} {t : set Ξ²} {u : Ξ± β†’ set Ξ²} (h : βˆ€ x ∈ s, u x βŠ† t) : (⋃ x ∈ s, u x) βŠ† t := show (⨆ x ∈ s, u x) ≀ t, -- TODO: should not be necessary when sets' order is based on lattices from supr_le $ assume x, supr_le (h x) theorem subset_bInter {s : set Ξ±} {t : set Ξ²} {u : Ξ± β†’ set Ξ²} (h : βˆ€ x ∈ s, t βŠ† u x) : t βŠ† (β‹‚ x ∈ s, u x) := show t ≀ (β¨… x ∈ s, u x), -- TODO: should not be necessary when sets' order is based on lattices from le_infi $ assume x, le_infi (h x) theorem subset_bUnion_of_mem {s : set Ξ±} {u : Ξ± β†’ set Ξ²} {x : Ξ±} (xs : x ∈ s) : u x βŠ† (⋃ x ∈ s, u x) := show u x ≀ (⨆ x ∈ s, u x), from le_supr_of_le x $ le_supr _ xs theorem bInter_subset_of_mem {s : set Ξ±} {t : Ξ± β†’ set Ξ²} {x : Ξ±} (xs : x ∈ s) : (β‹‚ x ∈ s, t x) βŠ† t x := show (β¨…x ∈ s, t x) ≀ t x, from infi_le_of_le x $ infi_le _ xs theorem bUnion_subset_bUnion_left {s s' : set Ξ±} {t : Ξ± β†’ set Ξ²} (h : s βŠ† s') : (⋃ x ∈ s, t x) βŠ† (⋃ x ∈ s', t x) := bUnion_subset (Ξ» x xs, subset_bUnion_of_mem (h xs)) theorem bInter_subset_bInter_left {s s' : set Ξ±} {t : Ξ± β†’ set Ξ²} (h : s' βŠ† s) : (β‹‚ x ∈ s, t x) βŠ† (β‹‚ x ∈ s', t x) := subset_bInter (Ξ» x xs, bInter_subset_of_mem (h xs)) theorem bUnion_subset_bUnion_right {s : set Ξ±} {t1 t2 : Ξ± β†’ set Ξ²} (h : βˆ€ x ∈ s, t1 x βŠ† t2 x) : (⋃ x ∈ s, t1 x) βŠ† (⋃ x ∈ s, t2 x) := bUnion_subset (Ξ» x xs, subset.trans (h x xs) (subset_bUnion_of_mem xs)) theorem bInter_subset_bInter_right {s : set Ξ±} {t1 t2 : Ξ± β†’ set Ξ²} (h : βˆ€ x ∈ s, t1 x βŠ† t2 x) : (β‹‚ x ∈ s, t1 x) βŠ† (β‹‚ x ∈ s, t2 x) := subset_bInter (Ξ» x xs, subset.trans (bInter_subset_of_mem xs) (h x xs)) theorem bUnion_eq_Union (s : set Ξ±) (t : Ξ± β†’ set Ξ²) : (⋃ x ∈ s, t x) = (⋃ x : s, t x.1) := set.ext $ by simp theorem bInter_eq_Inter (s : set Ξ±) (t : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ s, t x) = (β‹‚ x : s, t x.1) := set.ext $ by simp @[simp] theorem bInter_empty (u : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ (βˆ… : set Ξ±), u x) = univ := show (β¨…x ∈ (βˆ… : set Ξ±), u x) = ⊀, -- simplifier should be able to rewrite x ∈ βˆ… to false. from infi_emptyset @[simp] theorem bInter_univ (u : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ @univ Ξ±, u x) = β‹‚ x, u x := infi_univ -- TODO(Jeremy): here is an artifact of the the encoding of bounded intersection: -- without dsimp, the next theorem fails to type check, because there is a lambda -- in a type that needs to be contracted. Using simp [eq_of_mem_singleton xa] also works. @[simp] theorem bInter_singleton (a : Ξ±) (s : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ ({a} : set Ξ±), s x) = s a := show (β¨… x ∈ ({a} : set Ξ±), s x) = s a, by simp theorem bInter_union (s t : set Ξ±) (u : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ s βˆͺ t, u x) = (β‹‚ x ∈ s, u x) ∩ (β‹‚ x ∈ t, u x) := show (β¨… x ∈ s βˆͺ t, u x) = (β¨… x ∈ s, u x) βŠ“ (β¨… x ∈ t, u x), from infi_union -- TODO(Jeremy): simp [insert_eq, bInter_union] doesn't work @[simp] theorem bInter_insert (a : Ξ±) (s : set Ξ±) (t : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ insert a s, t x) = t a ∩ (β‹‚ x ∈ s, t x) := begin rw insert_eq, simp [bInter_union] end -- TODO(Jeremy): another example of where an annotation is needed theorem bInter_pair (a b : Ξ±) (s : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ ({a, b} : set Ξ±), s x) = s a ∩ s b := by rw insert_of_has_insert; simp [inter_comm] @[simp] theorem bUnion_empty (s : Ξ± β†’ set Ξ²) : (⋃ x ∈ (βˆ… : set Ξ±), s x) = βˆ… := supr_emptyset @[simp] theorem bUnion_univ (s : Ξ± β†’ set Ξ²) : (⋃ x ∈ @univ Ξ±, s x) = ⋃ x, s x := supr_univ @[simp] theorem bUnion_singleton (a : Ξ±) (s : Ξ± β†’ set Ξ²) : (⋃ x ∈ ({a} : set Ξ±), s x) = s a := supr_singleton theorem bUnion_union (s t : set Ξ±) (u : Ξ± β†’ set Ξ²) : (⋃ x ∈ s βˆͺ t, u x) = (⋃ x ∈ s, u x) βˆͺ (⋃ x ∈ t, u x) := supr_union -- TODO(Jeremy): once again, simp doesn't do it alone. @[simp] theorem bUnion_insert (a : Ξ±) (s : set Ξ±) (t : Ξ± β†’ set Ξ²) : (⋃ x ∈ insert a s, t x) = t a βˆͺ (⋃ x ∈ s, t x) := begin rw [insert_eq], simp [bUnion_union] end theorem bUnion_pair (a b : Ξ±) (s : Ξ± β†’ set Ξ²) : (⋃ x ∈ ({a, b} : set Ξ±), s x) = s a βˆͺ s b := by rw insert_of_has_insert; simp [union_comm] @[simp] -- complete_boolean_algebra theorem compl_bUnion (s : set Ξ±) (t : Ξ± β†’ set Ξ²) : - (⋃ i ∈ s, t i) = (β‹‚ i ∈ s, - t i) := ext (Ξ» x, by simp) -- classical -- complete_boolean_algebra theorem compl_bInter (s : set Ξ±) (t : Ξ± β†’ set Ξ²) : -(β‹‚ i ∈ s, t i) = (⋃ i ∈ s, - t i) := ext (Ξ» x, by simp [classical.not_forall]) /-- Intersection of a set of sets. -/ @[reducible] def sInter (S : set (set Ξ±)) : set Ξ± := Inf S prefix `β‹‚β‚€`:110 := sInter theorem mem_sUnion_of_mem {x : Ξ±} {t : set Ξ±} {S : set (set Ξ±)} (hx : x ∈ t) (ht : t ∈ S) : x ∈ ⋃₀ S := ⟨t, ⟨ht, hx⟩⟩ @[simp] theorem mem_sUnion {x : Ξ±} {S : set (set Ξ±)} : x ∈ ⋃₀ S ↔ βˆƒt ∈ S, x ∈ t := iff.rfl -- is this theorem really necessary? theorem not_mem_of_not_mem_sUnion {x : Ξ±} {t : set Ξ±} {S : set (set Ξ±)} (hx : x βˆ‰ ⋃₀ S) (ht : t ∈ S) : x βˆ‰ t := Ξ» h, hx ⟨t, ht, h⟩ @[simp] theorem mem_sInter {x : Ξ±} {S : set (set Ξ±)} : x ∈ β‹‚β‚€ S ↔ βˆ€ t ∈ S, x ∈ t := iff.rfl theorem sInter_subset_of_mem {S : set (set Ξ±)} {t : set Ξ±} (tS : t ∈ S) : β‹‚β‚€ S βŠ† t := Inf_le tS theorem subset_sUnion_of_mem {S : set (set Ξ±)} {t : set Ξ±} (tS : t ∈ S) : t βŠ† ⋃₀ S := le_Sup tS theorem sUnion_subset {S : set (set Ξ±)} {t : set Ξ±} (h : βˆ€t' ∈ S, t' βŠ† t) : (⋃₀ S) βŠ† t := Sup_le h theorem sUnion_subset_iff {s : set (set Ξ±)} {t : set Ξ±} : ⋃₀ s βŠ† t ↔ βˆ€t' ∈ s, t' βŠ† t := ⟨assume h t' ht', subset.trans (subset_sUnion_of_mem ht') h, sUnion_subset⟩ theorem subset_sInter {S : set (set Ξ±)} {t : set Ξ±} (h : βˆ€t' ∈ S, t βŠ† t') : t βŠ† (β‹‚β‚€ S) := le_Inf h theorem sUnion_subset_sUnion {S T : set (set Ξ±)} (h : S βŠ† T) : ⋃₀ S βŠ† ⋃₀ T := sUnion_subset $ Ξ» s hs, subset_sUnion_of_mem (h hs) theorem sInter_subset_sInter {S T : set (set Ξ±)} (h : S βŠ† T) : β‹‚β‚€ T βŠ† β‹‚β‚€ S := subset_sInter $ Ξ» s hs, sInter_subset_of_mem (h hs) @[simp] theorem sUnion_empty : ⋃₀ βˆ… = (βˆ… : set Ξ±) := Sup_empty @[simp] theorem sInter_empty : β‹‚β‚€ βˆ… = (univ : set Ξ±) := Inf_empty @[simp] theorem sUnion_singleton (s : set Ξ±) : ⋃₀ {s} = s := Sup_singleton @[simp] theorem sInter_singleton (s : set Ξ±) : β‹‚β‚€ {s} = s := Inf_singleton theorem sUnion_union (S T : set (set Ξ±)) : ⋃₀ (S βˆͺ T) = ⋃₀ S βˆͺ ⋃₀ T := Sup_union theorem sInter_union (S T : set (set Ξ±)) : β‹‚β‚€ (S βˆͺ T) = β‹‚β‚€ S ∩ β‹‚β‚€ T := Inf_union @[simp] theorem sUnion_insert (s : set Ξ±) (T : set (set Ξ±)) : ⋃₀ (insert s T) = s βˆͺ ⋃₀ T := Sup_insert @[simp] theorem sInter_insert (s : set Ξ±) (T : set (set Ξ±)) : β‹‚β‚€ (insert s T) = s ∩ β‹‚β‚€ T := Inf_insert @[simp] theorem sUnion_image (f : Ξ± β†’ set Ξ²) (s : set Ξ±) : ⋃₀ (f '' s) = ⋃ x ∈ s, f x := Sup_image @[simp] theorem sInter_image (f : Ξ± β†’ set Ξ²) (s : set Ξ±) : β‹‚β‚€ (f '' s) = β‹‚ x ∈ s, f x := Inf_image theorem compl_sUnion (S : set (set Ξ±)) : - ⋃₀ S = β‹‚β‚€ (compl '' S) := set.ext $ assume x, ⟨assume : Β¬ (βˆƒs∈S, x ∈ s), assume s h, match s, h with ._, ⟨t, hs, rfl⟩ := assume h, this ⟨t, hs, h⟩ end, assume : βˆ€s, s ∈ compl '' S β†’ x ∈ s, assume ⟨t, tS, xt⟩, this (compl t) (mem_image_of_mem _ tS) xt⟩ -- classical theorem sUnion_eq_compl_sInter_compl (S : set (set Ξ±)) : ⋃₀ S = - β‹‚β‚€ (compl '' S) := by rw [←compl_compl (⋃₀ S), compl_sUnion] -- classical theorem compl_sInter (S : set (set Ξ±)) : - β‹‚β‚€ S = ⋃₀ (compl '' S) := by rw [sUnion_eq_compl_sInter_compl, compl_compl_image] -- classical theorem sInter_eq_comp_sUnion_compl (S : set (set Ξ±)) : β‹‚β‚€ S = -(⋃₀ (compl '' S)) := by rw [←compl_compl (β‹‚β‚€ S), compl_sInter] theorem inter_empty_of_inter_sUnion_empty {s t : set Ξ±} {S : set (set Ξ±)} (hs : t ∈ S) (h : s ∩ ⋃₀ S = βˆ…) : s ∩ t = βˆ… := eq_empty_of_subset_empty $ by rw ← h; exact inter_subset_inter_right _ (subset_sUnion_of_mem hs) theorem Union_eq_sUnion_range (s : Ξ± β†’ set Ξ²) : (⋃ i, s i) = ⋃₀ (range s) := by rw [← image_univ, sUnion_image]; simp theorem Inter_eq_sInter_range {Ξ± I : Type} (s : I β†’ set Ξ±) : (β‹‚ i, s i) = β‹‚β‚€ (range s) := by rw [← image_univ, sInter_image]; simp theorem range_sigma_eq_Union_range {Ξ³ : Ξ± β†’ Type*} (f : sigma Ξ³ β†’ Ξ²) : range f = ⋃ a, range (Ξ» b, f ⟨a, b⟩) := set.ext $ by simp theorem Union_eq_range_sigma (s : Ξ± β†’ set Ξ²) : (⋃ i, s i) = range (Ξ» a : Ξ£ i, s i, a.2) := by simp [set.ext_iff] lemma sUnion_mono {s t : set (set Ξ±)} (h : s βŠ† t) : (⋃₀ s) βŠ† (⋃₀ t) := sUnion_subset $ assume t' ht', subset_sUnion_of_mem $ h ht' lemma Union_subset_Union {s t : ΞΉ β†’ set Ξ±} (h : βˆ€i, s i βŠ† t i) : (⋃i, s i) βŠ† (⋃i, t i) := @supr_le_supr (set Ξ±) ΞΉ _ s t h lemma Union_subset_Union2 {ΞΉβ‚‚ : Sort*} {s : ΞΉ β†’ set Ξ±} {t : ΞΉβ‚‚ β†’ set Ξ±} (h : βˆ€i, βˆƒj, s i βŠ† t j) : (⋃i, s i) βŠ† (⋃i, t i) := @supr_le_supr2 (set Ξ±) ΞΉ ΞΉβ‚‚ _ s t h lemma Union_subset_Union_const {ΞΉβ‚‚ : Sort x} {s : set Ξ±} (h : ΞΉ β†’ ΞΉβ‚‚) : (⋃ i:ΞΉ, s) βŠ† (⋃ j:ΞΉβ‚‚, s) := @supr_le_supr_const (set Ξ±) ΞΉ ΞΉβ‚‚ _ s h lemma sUnion_eq_bUnion {s : set (set Ξ±)} : (⋃₀ s) = (⋃ (i : set Ξ±) (h : i ∈ s), i) := set.ext $ by simp lemma sInter_eq_bInter {s : set (set Ξ±)} : (β‹‚β‚€ s) = (β‹‚ (i : set Ξ±) (h : i ∈ s), i) := set.ext $ by simp lemma sUnion_eq_Union {s : set (set Ξ±)} : (⋃₀ s) = (⋃ (i : s), i.1) := set.ext $ Ξ» x, by simp lemma sInter_eq_Inter {s : set (set Ξ±)} : (β‹‚β‚€ s) = (β‹‚ (i : s), i.1) := set.ext $ Ξ» x, by simp lemma union_eq_Union {s₁ sβ‚‚ : set Ξ±} : s₁ βˆͺ sβ‚‚ = ⋃ b : bool, cond b s₁ sβ‚‚ := set.ext $ Ξ» x, by simp [bool.exists_bool, or_comm] lemma inter_eq_Inter {s₁ sβ‚‚ : set Ξ±} : s₁ ∩ sβ‚‚ = β‹‚ b : bool, cond b s₁ sβ‚‚ := set.ext $ Ξ» x, by simp [bool.forall_bool, and_comm] instance : complete_boolean_algebra (set Ξ±) := { neg := compl, sub := (\), inf_neg_eq_bot := assume s, ext $ assume x, ⟨assume ⟨h, nh⟩, nh h, false.elim⟩, sup_neg_eq_top := assume s, ext $ assume x, ⟨assume h, trivial, assume _, classical.em $ x ∈ s⟩, le_sup_inf := distrib_lattice.le_sup_inf, sub_eq := assume x y, rfl, infi_sup_le_sup_Inf := assume s t x, show x ∈ (β‹‚ b ∈ t, s βˆͺ b) β†’ x ∈ s βˆͺ (β‹‚β‚€ t), by simp; exact assume h, or.imp_right (assume hn : x βˆ‰ s, assume i hi, or.resolve_left (h i hi) hn) (classical.em $ x ∈ s), inf_Sup_le_supr_inf := assume s t x, show x ∈ s ∩ (⋃₀ t) β†’ x ∈ (⋃ b ∈ t, s ∩ b), by simp [-and_imp, and.left_comm], ..set.lattice_set } @[simp] theorem sub_eq_diff (s t : set Ξ±) : s - t = s \ t := rfl section variables {p : Prop} {ΞΌ : p β†’ set Ξ±} @[simp] lemma Inter_pos (hp : p) : (β‹‚h:p, ΞΌ h) = ΞΌ hp := infi_pos hp @[simp] lemma Inter_neg (hp : Β¬ p) : (β‹‚h:p, ΞΌ h) = univ := infi_neg hp @[simp] lemma Union_pos (hp : p) : (⋃h:p, ΞΌ h) = ΞΌ hp := supr_pos hp @[simp] lemma Union_neg (hp : Β¬ p) : (⋃h:p, ΞΌ h) = βˆ… := supr_neg hp @[simp] lemma Union_empty {ΞΉ : Sort*} : (⋃i:ΞΉ, βˆ…:set Ξ±) = βˆ… := supr_bot @[simp] lemma Inter_univ {ΞΉ : Sort*} : (β‹‚i:ΞΉ, univ:set Ξ±) = univ := infi_top end section image lemma image_Union {f : Ξ± β†’ Ξ²} {s : ΞΉ β†’ set Ξ±} : f '' (⋃ i, s i) = (⋃i, f '' s i) := begin apply set.ext, intro x, simp [image, exists_and_distrib_right.symm, -exists_and_distrib_right], exact exists_swap end lemma univ_subtype {p : Ξ± β†’ Prop} : (univ : set (subtype p)) = (⋃x (h : p x), {⟨x, h⟩}) := set.ext $ assume ⟨x, h⟩, by simp [h] end image section preimage theorem monotone_preimage {f : Ξ± β†’ Ξ²} : monotone (preimage f) := assume a b h, preimage_mono h @[simp] theorem preimage_Union {ΞΉ : Sort w} {f : Ξ± β†’ Ξ²} {s : ΞΉ β†’ set Ξ²} : preimage f (⋃i, s i) = (⋃i, preimage f (s i)) := set.ext $ by simp [preimage] @[simp] theorem preimage_sUnion {f : Ξ± β†’ Ξ²} {s : set (set Ξ²)} : preimage f (⋃₀ s) = (⋃t ∈ s, preimage f t) := set.ext $ by simp [preimage] end preimage section seq def seq (s : set (Ξ± β†’ Ξ²)) (t : set Ξ±) : set Ξ² := {b | βˆƒf∈s, βˆƒa∈t, (f : Ξ± β†’ Ξ²) a = b} lemma seq_def {s : set (Ξ± β†’ Ξ²)} {t : set Ξ±} : seq s t = ⋃f∈s, f '' t := set.ext $ by simp [seq] lemma mem_seq_iff {s : set (Ξ± β†’ Ξ²)} {t : set Ξ±} {b : Ξ²} : b ∈ seq s t ↔ (βˆƒf ∈ s, βˆƒa∈t, (f : Ξ± β†’ Ξ²) a = b) := iff.refl _ lemma seq_subset {s : set (Ξ± β†’ Ξ²)} {t : set Ξ±} {u : set Ξ²} : seq s t βŠ† u ↔ (βˆ€f∈s, βˆ€a∈t, (f : Ξ± β†’ Ξ²) a ∈ u) := iff.intro (assume h f hf a ha, h ⟨f, hf, a, ha, rfl⟩) (assume h b ⟨f, hf, a, ha, eq⟩, eq β–Έ h f hf a ha) lemma seq_mono {sβ‚€ s₁ : set (Ξ± β†’ Ξ²)} {tβ‚€ t₁ : set Ξ±} (hs : sβ‚€ βŠ† s₁) (ht : tβ‚€ βŠ† t₁) : seq sβ‚€ tβ‚€ βŠ† seq s₁ t₁ := assume b ⟨f, hf, a, ha, eq⟩, ⟨f, hs hf, a, ht ha, eq⟩ lemma singleton_seq {f : Ξ± β†’ Ξ²} {t : set Ξ±} : set.seq {f} t = f '' t := set.ext $ by simp [seq] lemma seq_singleton {s : set (Ξ± β†’ Ξ²)} {a : Ξ±} : set.seq s {a} = (Ξ»f:Ξ±β†’Ξ², f a) '' s := set.ext $ by simp [seq] lemma seq_seq {s : set (Ξ² β†’ Ξ³)} {t : set (Ξ± β†’ Ξ²)} {u : set Ξ±} : seq s (seq t u) = seq (seq ((∘) '' s) t) u := begin refine (set.ext $ assume c, iff.intro _ _), { rintros ⟨f, hfs, b, ⟨g, hg, a, hau, rfl⟩, rfl⟩, exact ⟨f ∘ g, ⟨(∘) f, mem_image_of_mem _ hfs, g, hg, rfl⟩, a, hau, rfl⟩ }, { rintros ⟨fg, ⟨fc, ⟨f, hfs, rfl⟩, g, hgt, rfl⟩, a, ha, rfl⟩, exact ⟨f, hfs, g a, ⟨g, hgt, a, ha, rfl⟩ , rfl⟩ } end lemma image_seq {f : Ξ² β†’ Ξ³} {s : set (Ξ± β†’ Ξ²)} {t : set Ξ±} : f '' seq s t = seq ((∘) f '' s) t := by rw [← singleton_seq, ← singleton_seq, seq_seq, image_singleton] lemma prod_eq_seq {s : set Ξ±} {t : set Ξ²} : set.prod s t = (prod.mk '' s).seq t := begin ext ⟨a, b⟩, split, { rintros ⟨ha, hb⟩, exact ⟨prod.mk a, ⟨a, ha, rfl⟩, b, hb, rfl⟩ }, { rintros ⟨f, ⟨x, hx, rfl⟩, y, hy, eq⟩, rw ← eq, exact ⟨hx, hy⟩ } end lemma prod_image_seq_comm (s : set Ξ±) (t : set Ξ²) : (prod.mk '' s).seq t = seq ((Ξ»b a, (a, b)) '' t) s := by rw [← prod_eq_seq, ← image_swap_prod, prod_eq_seq, image_seq, ← image_comp] end seq theorem monotone_prod [preorder Ξ±] {f : Ξ± β†’ set Ξ²} {g : Ξ± β†’ set Ξ³} (hf : monotone f) (hg : monotone g) : monotone (Ξ»x, set.prod (f x) (g x)) := assume a b h, prod_mono (hf h) (hg h) instance : monad set := { pure := Ξ»(Ξ± : Type u) a, {a}, bind := Ξ»(Ξ± Ξ² : Type u) s f, ⋃i∈s, f i, seq := Ξ»(Ξ± Ξ² : Type u), set.seq, map := Ξ»(Ξ± Ξ² : Type u), set.image } instance : is_lawful_monad set := { pure_bind := assume Ξ± Ξ² x f, by simp, bind_assoc := assume Ξ± Ξ² Ξ³ s f g, set.ext $ assume a, by simp [exists_and_distrib_right.symm, -exists_and_distrib_right, exists_and_distrib_left.symm, -exists_and_distrib_left, and_assoc]; exact exists_swap, id_map := assume Ξ±, id_map, bind_pure_comp_eq_map := assume Ξ± Ξ² f s, set.ext $ by simp [set.image, eq_comm], bind_map_eq_seq := assume Ξ± Ξ² s t, by simp [seq_def] } instance : is_comm_applicative (set : Type u β†’ Type u) := ⟨ assume Ξ± Ξ² s t, prod_image_seq_comm s t ⟩ section monad variables {Ξ±' Ξ²' : Type u} {s : set Ξ±'} {f : Ξ±' β†’ set Ξ²'} {g : set (Ξ±' β†’ Ξ²')} @[simp] lemma bind_def : s >>= f = ⋃i∈s, f i := rfl lemma fmap_eq_image : f <$> s = f '' s := rfl lemma seq_eq_set_seq {Ξ± Ξ² : Type*} (s : set (Ξ± β†’ Ξ²)) (t : set Ξ±) : s <*> t = s.seq t := rfl @[simp] lemma pure_def (a : Ξ±): (pure a : set Ξ±) = {a} := rfl end monad section pi lemma pi_def {Ξ± : Type*} {Ο€ : Ξ± β†’ Type*} (i : set Ξ±) (s : Ξ a, set (Ο€ a)) : pi i s = (β‹‚ a∈i, ((Ξ»f:(Ξ a, Ο€ a), f a) ⁻¹' (s a))) := by ext; simp [pi] end pi end set /- disjoint sets -/ section disjoint variable [semilattice_inf_bot Ξ±] /-- Two elements of a lattice are disjoint if their inf is the bottom element. (This generalizes disjoint sets, viewed as members of the subset lattice.) -/ def disjoint (a b : Ξ±) : Prop := a βŠ“ b ≀ βŠ₯ theorem disjoint.eq_bot {a b : Ξ±} (h : disjoint a b) : a βŠ“ b = βŠ₯ := eq_bot_iff.2 h theorem disjoint_iff {a b : Ξ±} : disjoint a b ↔ a βŠ“ b = βŠ₯ := eq_bot_iff.symm theorem disjoint.comm {a b : Ξ±} : disjoint a b ↔ disjoint b a := by rw [disjoint, disjoint, inf_comm] theorem disjoint.symm {a b : Ξ±} : disjoint a b β†’ disjoint b a := disjoint.comm.1 theorem disjoint_bot_left {a : Ξ±} : disjoint βŠ₯ a := disjoint_iff.2 bot_inf_eq theorem disjoint_bot_right {a : Ξ±} : disjoint a βŠ₯ := disjoint_iff.2 inf_bot_eq theorem disjoint_mono {a b c d : Ξ±} (h₁ : a ≀ b) (hβ‚‚ : c ≀ d) : disjoint b d β†’ disjoint a c := le_trans (inf_le_inf h₁ hβ‚‚) theorem disjoint_mono_left {a b c : Ξ±} (h : a ≀ b) : disjoint b c β†’ disjoint a c := disjoint_mono h (le_refl _) theorem disjoint_mono_right {a b c : Ξ±} (h : b ≀ c) : disjoint a c β†’ disjoint a b := disjoint_mono (le_refl _) h end disjoint theorem set.disjoint_diff {a b : set Ξ±} : disjoint a (b \ a) := disjoint_iff.2 (inter_diff_self _ _) section open set set_option eqn_compiler.zeta true noncomputable def set.bUnion_eq_sigma_of_disjoint {Ξ± Ξ²} {s : set Ξ±} {t : Ξ± β†’ set Ξ²} (h : pairwise_on s (disjoint on t)) : (⋃i∈s, t i) ≃ (Ξ£i:s, t i.val) := let f : (Ξ£i:s, t i.val) β†’ (⋃i∈s, t i) := λ⟨⟨a, ha⟩, ⟨b, hb⟩⟩, ⟨b, mem_bUnion ha hb⟩ in have injective f, from assume ⟨⟨a₁, haβ‚βŸ©, ⟨b₁, hbβ‚βŸ©βŸ© ⟨⟨aβ‚‚, haβ‚‚βŸ©, ⟨bβ‚‚, hbβ‚‚βŸ©βŸ© eq, have b_eq : b₁ = bβ‚‚, from congr_arg subtype.val eq, have a_eq : a₁ = aβ‚‚, from classical.by_contradiction $ assume ne, have b₁ ∈ t a₁ ∩ t aβ‚‚, from ⟨hb₁, b_eq.symm β–Έ hbβ‚‚βŸ©, h _ ha₁ _ haβ‚‚ ne this, sigma.eq (subtype.eq a_eq) (subtype.eq $ by subst b_eq; subst a_eq), have surjective f, from assume ⟨b, hb⟩, have βˆƒa∈s, b ∈ t a, by simpa using hb, let ⟨a, ha, hb⟩ := this in ⟨⟨⟨a, ha⟩, ⟨b, hb⟩⟩, rfl⟩, (equiv.of_bijective βŸ¨β€Ήinjective fβ€Ί, β€Ήsurjective fβ€ΊβŸ©).symm end
cf82ab6925f25cce788fcdc7d36afaba64602561
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/standard/tools/tactic.lean
eb68a7a19e0875b220042c3b66de9188ce920722
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,963
lean
---------------------------------------------------------------------------------------------------- -- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura ---------------------------------------------------------------------------------------------------- import data.string data.num using string using num namespace tactic -- This is just a trick to embed the 'tactic language' as a -- Lean expression. We should view 'tactic' as automation -- that when execute produces a term. -- builtin_tactic is just a "dummy" for creating the -- definitions that are actually implemented in C++ inductive tactic : Type := | builtin_tactic : tactic -- Remark the following names are not arbitrary, the tactic module -- uses them when converting Lean expressions into actual tactic objects. -- The bultin 'by' construct triggers the process of converting a -- a term of type 'tactic' into a tactic that sythesizes a term definition and_then (t1 t2 : tactic) : tactic := builtin_tactic definition or_else (t1 t2 : tactic) : tactic := builtin_tactic definition append (t1 t2 : tactic) : tactic := builtin_tactic definition interleave (t1 t2 : tactic) : tactic := builtin_tactic definition par (t1 t2 : tactic) : tactic := builtin_tactic definition fixpoint (f : tactic β†’ tactic) : tactic := builtin_tactic definition repeat (t : tactic) : tactic := builtin_tactic definition at_most (t : tactic) (k : num) : tactic := builtin_tactic definition discard (t : tactic) (k : num) : tactic := builtin_tactic definition focus_at (t : tactic) (i : num) : tactic := builtin_tactic definition try_for (t : tactic) (ms : num) : tactic := builtin_tactic definition now : tactic := builtin_tactic definition assumption : tactic := builtin_tactic definition eassumption : tactic := builtin_tactic definition state : tactic := builtin_tactic definition fail : tactic := builtin_tactic definition id : tactic := builtin_tactic definition beta : tactic := builtin_tactic definition apply {B : Type} (b : B) : tactic := builtin_tactic definition unfold {B : Type} (b : B) : tactic := builtin_tactic definition exact {B : Type} (b : B) : tactic := builtin_tactic definition trace (s : string) : tactic := builtin_tactic precedence `;`:200 infixl ; := and_then notation `!` t:max := repeat t -- [ t_1 | ... | t_n ] notation notation `[` h:100 `|` r:(foldl 100 `|` (e r, or_else r e) h) `]` := r -- [ t_1 || ... || t_n ] notation notation `[` h:100 `||` r:(foldl 100 `||` (e r, par r e) h) `]` := r definition try (t : tactic) : tactic := [ t | id ] notation `?` t:max := try t definition repeat1 (t : tactic) : tactic := t ; !t definition focus (t : tactic) : tactic := focus_at t 0 definition determ (t : tactic) : tactic := at_most t 1 end
2f2a8f73e641805c0964b1b6b52326a3cc1c0337
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/specbug.lean
f410424247615b1dbf9648785875527d6c3cc001
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
801
lean
@[noinline] def f (x : Bool) := x @[noinline] def g (x y : Bool) := x def h (x : Bool) (xs : List Nat) : List Bool := match x with | true => let z := f true let y := f false xs.map fun x => g y z | false => let y := f false let z := f true xs.map fun x => g y z theorem ex1 : h true [1] = h false [1] := rfl #eval h true [1] #eval h false [1] theorem ex2 : (h true [1] == h false [1]) = true := by nativeDecide @[noinline] def f2 (a : String) := a @[noinline] def g2 (a : String) (x : Bool) := a def h2 (x : Bool) (xs : List Nat) : List String := match x with | false => let a := f2 "a" let y := f false xs.map fun x => g2 a y | true => let y := f false let a := f2 "a" xs.map fun x => g2 a y #eval h2 true [1] #eval h2 false [1]
36a3caaa94d6d2a2ca20043aa6af8faa9e934897
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/category/Profinite/basic.lean
8f71fdef069dc9198d985061843f12737aa8db1e
[ "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,440
lean
/- Copyright (c) 2020 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Calle SΓΆnne -/ import topology.category.CompHaus.basic import topology.connected import topology.subset_properties import topology.locally_constant.basic import category_theory.adjunction.reflective import category_theory.monad.limits import category_theory.Fintype /-! # The category of Profinite Types > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We construct the category of profinite topological spaces, often called profinite sets -- perhaps they could be called profinite types in Lean. The type of profinite topological spaces is called `Profinite`. It has a category instance and is a fully faithful subcategory of `Top`. The fully faithful functor is called `Profinite_to_Top`. ## Implementation notes A profinite type is defined to be a topological space which is compact, Hausdorff and totally disconnected. ## TODO 0. Link to category of projective limits of finite discrete sets. 1. finite coproducts 2. Clausen/Scholze topology on the category `Profinite`. ## Tags profinite -/ universe u open category_theory open_locale topology /-- The type of profinite topological spaces. -/ structure Profinite := (to_CompHaus : CompHaus) [is_totally_disconnected : totally_disconnected_space to_CompHaus] namespace Profinite /-- Construct a term of `Profinite` from a type endowed with the structure of a compact, Hausdorff and totally disconnected topological space. -/ def of (X : Type*) [topological_space X] [compact_space X] [t2_space X] [totally_disconnected_space X] : Profinite := ⟨⟨⟨X⟩⟩⟩ instance : inhabited Profinite := ⟨Profinite.of pempty⟩ instance category : category Profinite := induced_category.category to_CompHaus instance concrete_category : concrete_category Profinite := induced_category.concrete_category _ instance has_forgetβ‚‚ : has_forgetβ‚‚ Profinite Top := induced_category.has_forgetβ‚‚ _ instance : has_coe_to_sort Profinite Type* := ⟨λ X, X.to_CompHaus⟩ instance {X : Profinite} : totally_disconnected_space X := X.is_totally_disconnected -- We check that we automatically infer that Profinite sets are compact and Hausdorff. example {X : Profinite} : compact_space X := infer_instance example {X : Profinite} : t2_space X := infer_instance @[simp] lemma coe_to_CompHaus {X : Profinite} : (X.to_CompHaus : Type*) = X := rfl @[simp] lemma coe_id (X : Profinite) : (πŸ™ X : X β†’ X) = id := rfl @[simp] lemma coe_comp {X Y Z : Profinite} (f : X ⟢ Y) (g : Y ⟢ Z) : (f ≫ g : X β†’ Z) = g ∘ f := rfl end Profinite /-- The fully faithful embedding of `Profinite` in `CompHaus`. -/ @[simps, derive [full, faithful]] def Profinite_to_CompHaus : Profinite β₯€ CompHaus := induced_functor _ /-- The fully faithful embedding of `Profinite` in `Top`. This is definitionally the same as the obvious composite. -/ @[simps, derive [full, faithful]] def Profinite.to_Top : Profinite β₯€ Top := forgetβ‚‚ _ _ @[simp] lemma Profinite.to_CompHaus_to_Top : Profinite_to_CompHaus β‹™ CompHaus_to_Top = Profinite.to_Top := rfl section Profinite /-- (Implementation) The object part of the connected_components functor from compact Hausdorff spaces to Profinite spaces, given by quotienting a space by its connected components. See: https://stacks.math.columbia.edu/tag/0900 -/ -- Without explicit universe annotations here, Lean introduces two universe variables and -- unhelpfully defines a function `CompHaus.{max u₁ uβ‚‚} β†’ Profinite.{max u₁ uβ‚‚}`. def CompHaus.to_Profinite_obj (X : CompHaus.{u}) : Profinite.{u} := { to_CompHaus := { to_Top := Top.of (connected_components X), is_compact := quotient.compact_space, is_hausdorff := connected_components.t2 }, is_totally_disconnected := connected_components.totally_disconnected_space } /-- (Implementation) The bijection of homsets to establish the reflective adjunction of Profinite spaces in compact Hausdorff spaces. -/ def Profinite.to_CompHaus_equivalence (X : CompHaus.{u}) (Y : Profinite.{u}) : (CompHaus.to_Profinite_obj X ⟢ Y) ≃ (X ⟢ Profinite_to_CompHaus.obj Y) := { to_fun := Ξ» f, f.comp ⟨quotient.mk', continuous_quotient_mk⟩, inv_fun := Ξ» g, { to_fun := continuous.connected_components_lift g.2, continuous_to_fun := continuous.connected_components_lift_continuous g.2}, left_inv := Ξ» f, continuous_map.ext $ connected_components.surjective_coe.forall.2 $ Ξ» a, rfl, right_inv := Ξ» f, continuous_map.ext $ Ξ» x, rfl } /-- The connected_components functor from compact Hausdorff spaces to profinite spaces, left adjoint to the inclusion functor. -/ def CompHaus.to_Profinite : CompHaus β₯€ Profinite := adjunction.left_adjoint_of_equiv Profinite.to_CompHaus_equivalence (Ξ» _ _ _ _ _, rfl) lemma CompHaus.to_Profinite_obj' (X : CompHaus) : β†₯(CompHaus.to_Profinite.obj X) = connected_components X := rfl /-- Finite types are given the discrete topology. -/ def Fintype.bot_topology (A : Fintype) : topological_space A := βŠ₯ section discrete_topology local attribute [instance] Fintype.bot_topology local attribute [instance] lemma Fintype.discrete_topology (A : Fintype) : discrete_topology A := ⟨rfl⟩ /-- The natural functor from `Fintype` to `Profinite`, endowing a finite type with the discrete topology. -/ @[simps] def Fintype.to_Profinite : Fintype β₯€ Profinite := { obj := Ξ» A, Profinite.of A, map := Ξ» _ _ f, ⟨f⟩ } end discrete_topology end Profinite namespace Profinite -- TODO the following construction of limits could be generalised -- to allow diagrams in lower universes. /-- An explicit limit cone for a functor `F : J β₯€ Profinite`, defined in terms of `Top.limit_cone`. -/ def limit_cone {J : Type u} [small_category J] (F : J β₯€ Profinite.{u}) : limits.cone F := { X := { to_CompHaus := (CompHaus.limit_cone.{u u} (F β‹™ Profinite_to_CompHaus)).X, is_totally_disconnected := begin change totally_disconnected_space β†₯{u : Ξ  (j : J), (F.obj j) | _}, exact subtype.totally_disconnected_space, end }, Ο€ := { app := (CompHaus.limit_cone.{u u} (F β‹™ Profinite_to_CompHaus)).Ο€.app } } /-- The limit cone `Profinite.limit_cone F` is indeed a limit cone. -/ def limit_cone_is_limit {J : Type u} [small_category J] (F : J β₯€ Profinite.{u}) : limits.is_limit (limit_cone F) := { lift := Ξ» S, (CompHaus.limit_cone_is_limit.{u u} (F β‹™ Profinite_to_CompHaus)).lift (Profinite_to_CompHaus.map_cone S), uniq' := Ξ» S m h, (CompHaus.limit_cone_is_limit.{u u} _).uniq (Profinite_to_CompHaus.map_cone S) _ h } /-- The adjunction between CompHaus.to_Profinite and Profinite.to_CompHaus -/ def to_Profinite_adj_to_CompHaus : CompHaus.to_Profinite ⊣ Profinite_to_CompHaus := adjunction.adjunction_of_equiv_left _ _ /-- The category of profinite sets is reflective in the category of compact hausdroff spaces -/ instance to_CompHaus.reflective : reflective Profinite_to_CompHaus := { to_is_right_adjoint := ⟨CompHaus.to_Profinite, Profinite.to_Profinite_adj_to_CompHaus⟩ } noncomputable instance to_CompHaus.creates_limits : creates_limits Profinite_to_CompHaus := monadic_creates_limits _ noncomputable instance to_Top.reflective : reflective Profinite.to_Top := reflective.comp Profinite_to_CompHaus CompHaus_to_Top noncomputable instance to_Top.creates_limits : creates_limits Profinite.to_Top := monadic_creates_limits _ instance has_limits : limits.has_limits Profinite := has_limits_of_has_limits_creates_limits Profinite.to_Top instance has_colimits : limits.has_colimits Profinite := has_colimits_of_reflective Profinite_to_CompHaus noncomputable instance forget_preserves_limits : limits.preserves_limits (forget Profinite) := by apply limits.comp_preserves_limits Profinite.to_Top (forget Top) variables {X Y : Profinite.{u}} (f : X ⟢ Y) /-- Any morphism of profinite spaces is a closed map. -/ lemma is_closed_map : is_closed_map f := CompHaus.is_closed_map _ /-- Any continuous bijection of profinite spaces induces an isomorphism. -/ lemma is_iso_of_bijective (bij : function.bijective f) : is_iso f := begin haveI := CompHaus.is_iso_of_bijective (Profinite_to_CompHaus.map f) bij, exact is_iso_of_fully_faithful Profinite_to_CompHaus _ end /-- Any continuous bijection of profinite spaces induces an isomorphism. -/ noncomputable def iso_of_bijective (bij : function.bijective f) : X β‰… Y := by letI := Profinite.is_iso_of_bijective f bij; exact as_iso f instance forget_reflects_isomorphisms : reflects_isomorphisms (forget Profinite) := ⟨by introsI A B f hf; exact Profinite.is_iso_of_bijective _ ((is_iso_iff_bijective f).mp hf)⟩ /-- Construct an isomorphism from a homeomorphism. -/ @[simps hom inv] def iso_of_homeo (f : X β‰ƒβ‚œ Y) : X β‰… Y := { hom := ⟨f, f.continuous⟩, inv := ⟨f.symm, f.symm.continuous⟩, hom_inv_id' := by { ext x, exact f.symm_apply_apply x }, inv_hom_id' := by { ext x, exact f.apply_symm_apply x } } /-- Construct a homeomorphism from an isomorphism. -/ @[simps] def homeo_of_iso (f : X β‰… Y) : X β‰ƒβ‚œ Y := { to_fun := f.hom, inv_fun := f.inv, left_inv := Ξ» x, by { change (f.hom ≫ f.inv) x = x, rw [iso.hom_inv_id, coe_id, id.def] }, right_inv := Ξ» x, by { change (f.inv ≫ f.hom) x = x, rw [iso.inv_hom_id, coe_id, id.def] }, continuous_to_fun := f.hom.continuous, continuous_inv_fun := f.inv.continuous } /-- The equivalence between isomorphisms in `Profinite` and homeomorphisms of topological spaces. -/ @[simps] def iso_equiv_homeo : (X β‰… Y) ≃ (X β‰ƒβ‚œ Y) := { to_fun := homeo_of_iso, inv_fun := iso_of_homeo, left_inv := Ξ» f, by { ext, refl }, right_inv := Ξ» f, by { ext, refl } } lemma epi_iff_surjective {X Y : Profinite.{u}} (f : X ⟢ Y) : epi f ↔ function.surjective f := begin split, { contrapose!, rintros ⟨y, hy⟩ hf, resetI, let C := set.range f, have hC : is_closed C := (is_compact_range f.continuous).is_closed, let U := Cᢜ, have hyU : y ∈ U, { refine set.mem_compl _, rintro ⟨y', hy'⟩, exact hy y' hy' }, have hUy : U ∈ 𝓝 y := hC.compl_mem_nhds hyU, obtain ⟨V, hV, hyV, hVU⟩ := is_topological_basis_clopen.mem_nhds_iff.mp hUy, classical, let Z := of (ulift.{u} $ fin 2), let g : Y ⟢ Z := ⟨(locally_constant.of_clopen hV).map ulift.up, locally_constant.continuous _⟩, let h : Y ⟢ Z := ⟨λ _, ⟨1⟩, continuous_const⟩, have H : h = g, { rw ← cancel_epi f, ext x, dsimp [locally_constant.of_clopen], rw if_neg, { refl }, refine mt (Ξ» Ξ±, hVU Ξ±) _, simp only [set.mem_range_self, not_true, not_false_iff, set.mem_compl_iff], }, apply_fun (Ξ» e, (e y).down) at H, dsimp [locally_constant.of_clopen] at H, rw if_pos hyV at H, exact top_ne_bot H }, { rw ← category_theory.epi_iff_surjective, apply (forget Profinite).epi_of_epi_map } end lemma mono_iff_injective {X Y : Profinite.{u}} (f : X ⟢ Y) : mono f ↔ function.injective f := begin split, { intro h, haveI : limits.preserves_limits Profinite_to_CompHaus := infer_instance, haveI : mono (Profinite_to_CompHaus.map f) := infer_instance, rwa ← CompHaus.mono_iff_injective }, { rw ← category_theory.mono_iff_injective, apply (forget Profinite).mono_of_mono_map } end end Profinite
e41446590375f49eec852a7643a30b07a893520a
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/continuous_function/weierstrass.lean
87b9b01092a8fb4f822820d1d30e51235ec00376
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
5,412
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import analysis.special_functions.bernstein import topology.algebra.algebra /-! # The Weierstrass approximation theorem for continuous functions on `[a,b]` We've already proved the Weierstrass approximation theorem in the sense that we've shown that the Bernstein approximations to a continuous function on `[0,1]` converge uniformly. Here we rephrase this more abstractly as `polynomial_functions_closure_eq_top' : (polynomial_functions I).topological_closure = ⊀` and then, by precomposing with suitable affine functions, `polynomial_functions_closure_eq_top : (polynomial_functions (set.Icc a b)).topological_closure = ⊀` -/ open continuous_map filter open_locale unit_interval /-- The special case of the Weierstrass approximation theorem for the interval `[0,1]`. This is just a matter of unravelling definitions and using the Bernstein approximations. -/ theorem polynomial_functions_closure_eq_top' : (polynomial_functions I).topological_closure = ⊀ := begin apply eq_top_iff.mpr, rintros f -, refine filter.frequently.mem_closure _, refine filter.tendsto.frequently (bernstein_approximation_uniform f) _, apply frequently_of_forall, intro n, simp only [set_like.mem_coe], apply subalgebra.sum_mem, rintro n -, apply subalgebra.smul_mem, dsimp [bernstein, polynomial_functions], simp, end /-- The **Weierstrass Approximation Theorem**: polynomials functions on `[a, b] βŠ† ℝ` are dense in `C([a,b],ℝ)` (While we could deduce this as an application of the Stone-Weierstrass theorem, our proof of that relies on the fact that `abs` is in the closure of polynomials on `[-M, M]`, so we may as well get this done first.) -/ theorem polynomial_functions_closure_eq_top (a b : ℝ) : (polynomial_functions (set.Icc a b)).topological_closure = ⊀ := begin by_cases h : a < b, -- (Otherwise it's easy; we'll deal with that later.) { -- We can pullback continuous functions on `[a,b]` to continuous functions on `[0,1]`, -- by precomposing with an affine map. let W : C(set.Icc a b, ℝ) →ₐ[ℝ] C(I, ℝ) := comp_right_alg_hom ℝ (Icc_homeo_I a b h).symm.to_continuous_map, -- This operation is itself a homeomorphism -- (with respect to the norm topologies on continuous functions). let W' : C(set.Icc a b, ℝ) β‰ƒβ‚œ C(I, ℝ) := comp_right_homeomorph ℝ (Icc_homeo_I a b h).symm, have w : (W : C(set.Icc a b, ℝ) β†’ C(I, ℝ)) = W' := rfl, -- Thus we take the statement of the Weierstrass approximation theorem for `[0,1]`, have p := polynomial_functions_closure_eq_top', -- and pullback both sides, obtaining an equation between subalgebras of `C([a,b], ℝ)`. apply_fun (Ξ» s, s.comap' W) at p, simp only [algebra.comap_top] at p, -- Since the pullback operation is continuous, it commutes with taking `topological_closure`, rw subalgebra.topological_closure_comap'_homeomorph _ W W' w at p, -- and precomposing with an affine map takes polynomial functions to polynomial functions. rw polynomial_functions.comap'_comp_right_alg_hom_Icc_homeo_I at p, -- πŸŽ‰ exact p }, { -- Otherwise, `b ≀ a`, and the interval is a subsingleton, -- so all subalgebras are the same anyway. haveI : subsingleton (set.Icc a b) := ⟨λ x y, le_antisymm ((x.2.2.trans (not_lt.mp h)).trans y.2.1) ((y.2.2.trans (not_lt.mp h)).trans x.2.1)⟩, haveI := (continuous_map.subsingleton_subalgebra (set.Icc a b) ℝ), apply subsingleton.elim, } end /-- An alternative statement of Weierstrass' theorem. Every real-valued continuous function on `[a,b]` is a uniform limit of polynomials. -/ theorem continuous_map_mem_polynomial_functions_closure (a b : ℝ) (f : C(set.Icc a b, ℝ)) : f ∈ (polynomial_functions (set.Icc a b)).topological_closure := begin rw polynomial_functions_closure_eq_top _ _, simp, end /-- An alternative statement of Weierstrass' theorem, for those who like their epsilons. Every real-valued continuous function on `[a,b]` is within any `Ξ΅ > 0` of some polynomial. -/ theorem exists_polynomial_near_continuous_map (a b : ℝ) (f : C(set.Icc a b, ℝ)) (Ξ΅ : ℝ) (pos : 0 < Ξ΅) : βˆƒ (p : polynomial ℝ), βˆ₯p.to_continuous_map_on _ - fβˆ₯ < Ξ΅ := begin have w := mem_closure_iff_frequently.mp (continuous_map_mem_polynomial_functions_closure _ _ f), rw metric.nhds_basis_ball.frequently_iff at w, obtain ⟨-, H, ⟨m, ⟨-, rfl⟩⟩⟩ := w Ξ΅ pos, rw [metric.mem_ball, dist_eq_norm] at H, exact ⟨m, H⟩, end /-- Another alternative statement of Weierstrass's theorem, for those who like epsilons, but not bundled continuous functions. Every real-valued function `ℝ β†’ ℝ` which is continuous on `[a,b]` can be approximated to within any `Ξ΅ > 0` on `[a,b]` by some polynomial. -/ theorem exists_polynomial_near_of_continuous_on (a b : ℝ) (f : ℝ β†’ ℝ) (c : continuous_on f (set.Icc a b)) (Ξ΅ : ℝ) (pos : 0 < Ξ΅) : βˆƒ (p : polynomial ℝ), βˆ€ x ∈ set.Icc a b, |p.eval x - f x| < Ξ΅ := begin let f' : C(set.Icc a b, ℝ) := ⟨λ x, f x, continuous_on_iff_continuous_restrict.mp c⟩, obtain ⟨p, b⟩ := exists_polynomial_near_continuous_map a b f' Ξ΅ pos, use p, rw norm_lt_iff _ pos at b, intros x m, exact b ⟨x, m⟩, end
69b420e95a86795762c0465d1a1c02929a6a17ff
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/algebra/group_bigops.lean
dde566b5c38e50b8cc5ff76cb8cbd516552068ac
[ "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
8,979
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad Finite products on a monoid, and finite sums on an additive monoid. We have to be careful with dependencies. This theory imports files from finset and list, which import basic files from nat. Then nat imports this file to instantiate finite products and sums. Bigops based on finsets go in the namespace algebra.finset. There are also versions based on sets, defined in group_set_bigops.lean. -/ import .group .group_power data.list.basic data.list.perm data.finset.basic open algebra function binary quot subtype list finset namespace algebra variables {A B : Type} variable [deceqA : decidable_eq A] /- Prodl: product indexed by a list -/ section monoid variable [mB : monoid B] include mB definition mulf (f : A β†’ B) : B β†’ A β†’ B := Ξ» b a, b * f a definition Prodl (l : list A) (f : A β†’ B) : B := list.foldl (mulf f) 1 l -- ∏ x ← l, f x notation `∏` binders `←` l, r:(scoped f, Prodl l f) := r private theorem foldl_const (f : A β†’ B) : βˆ€ (l : list A) (b : B), foldl (mulf f) b l = b * foldl (mulf f) 1 l | [] b := by rewrite [*foldl_nil, mul_one] | (a::l) b := by rewrite [*foldl_cons, foldl_const, {foldl _ (mulf f 1 a) _}foldl_const, ↑mulf, one_mul, mul.assoc] theorem Prodl_nil (f : A β†’ B) : Prodl [] f = 1 := rfl theorem Prodl_cons (f : A β†’ B) (a : A) (l : list A) : Prodl (a::l) f = f a * Prodl l f := by rewrite [↑Prodl, foldl_cons, foldl_const, ↑mulf, one_mul] theorem Prodl_append : βˆ€ (l₁ lβ‚‚ : list A) (f : A β†’ B), Prodl (l₁++lβ‚‚) f = Prodl l₁ f * Prodl lβ‚‚ f | [] lβ‚‚ f := by rewrite [append_nil_left, Prodl_nil, one_mul] | (a::l) lβ‚‚ f := by rewrite [append_cons, *Prodl_cons, Prodl_append, mul.assoc] section deceqA include deceqA theorem Prodl_insert_of_mem (f : A β†’ B) {a : A} {l : list A} : a ∈ l β†’ Prodl (insert a l) f = Prodl l f := assume ainl, by rewrite [insert_eq_of_mem ainl] theorem Prodl_insert_of_not_mem (f : A β†’ B) {a : A} {l : list A} : a βˆ‰ l β†’ Prodl (insert a l) f = f a * Prodl l f := assume nainl, by rewrite [insert_eq_of_not_mem nainl, Prodl_cons] theorem Prodl_union {l₁ lβ‚‚ : list A} (f : A β†’ B) (d : disjoint l₁ lβ‚‚) : Prodl (union l₁ lβ‚‚) f = Prodl l₁ f * Prodl lβ‚‚ f := by rewrite [union_eq_append d, Prodl_append] end deceqA theorem Prodl_one : βˆ€(l : list A), Prodl l (Ξ» x, 1) = (1:B) | [] := rfl | (a::l) := by rewrite [Prodl_cons, Prodl_one, mul_one] lemma Prodl_singleton {a : A} {f : A β†’ B} : Prodl [a] f = f a := !one_mul lemma Prodl_map {f : A β†’ B} : βˆ€ {l : list A}, Prodl l f = Prodl (map f l) id | nil := by rewrite [map_nil] | (a::l) := begin rewrite [map_cons, Prodl_cons f, Prodl_cons id (f a), Prodl_map] end open nat lemma Prodl_eq_pow_of_const {f : A β†’ B} : βˆ€ {l : list A} b, (βˆ€ a, a ∈ l β†’ f a = b) β†’ Prodl l f = b ^ length l | nil := take b, assume Pconst, by rewrite [length_nil, {b^0}algebra.pow_zero] | (a::l) := take b, assume Pconst, assert Pconstl : βˆ€ a', a' ∈ l β†’ f a' = b, from take a' Pa'in, Pconst a' (mem_cons_of_mem a Pa'in), by rewrite [Prodl_cons f, Pconst a !mem_cons, Prodl_eq_pow_of_const b Pconstl, length_cons, add_one, pow_succ b] end monoid section comm_monoid variable [cmB : comm_monoid B] include cmB theorem Prodl_mul (l : list A) (f g : A β†’ B) : Prodl l (Ξ»x, f x * g x) = Prodl l f * Prodl l g := list.induction_on l (by rewrite [*Prodl_nil, mul_one]) (take a l, assume IH, by rewrite [*Prodl_cons, IH, *mul.assoc, mul.left_comm (Prodl l f)]) end comm_monoid /- Prod: product indexed by a finset -/ namespace finset variable [cmB : comm_monoid B] include cmB theorem mulf_rcomm (f : A β†’ B) : right_commutative (mulf f) := right_commutative_compose_right (@has_mul.mul B cmB) f (@mul.right_comm B cmB) theorem Prodl_eq_Prodl_of_perm (f : A β†’ B) {l₁ lβ‚‚ : list A} : perm l₁ lβ‚‚ β†’ Prodl l₁ f = Prodl lβ‚‚ f := Ξ» p, perm.foldl_eq_of_perm (mulf_rcomm f) p 1 definition Prod (s : finset A) (f : A β†’ B) : B := quot.lift_on s (Ξ» l, Prodl (elt_of l) f) (Ξ» l₁ lβ‚‚ p, Prodl_eq_Prodl_of_perm f p) -- ∏ x ∈ s, f x notation `∏` binders `∈` s, r:(scoped f, prod s f) := r theorem Prod_empty (f : A β†’ B) : Prod βˆ… f = 1 := Prodl_nil f theorem Prod_mul (s : finset A) (f g : A β†’ B) : Prod s (Ξ»x, f x * g x) = Prod s f * Prod s g := quot.induction_on s (take u, !Prodl_mul) section deceqA include deceqA theorem Prod_insert_of_mem (f : A β†’ B) {a : A} {s : finset A} : a ∈ s β†’ Prod (insert a s) f = Prod s f := quot.induction_on s (Ξ» l ainl, Prodl_insert_of_mem f ainl) theorem Prod_insert_of_not_mem (f : A β†’ B) {a : A} {s : finset A} : a βˆ‰ s β†’ Prod (insert a s) f = f a * Prod s f := quot.induction_on s (Ξ» l nainl, Prodl_insert_of_not_mem f nainl) theorem Prod_union (f : A β†’ B) {s₁ sβ‚‚ : finset A} (disj : s₁ ∩ sβ‚‚ = βˆ…) : Prod (s₁ βˆͺ sβ‚‚) f = Prod s₁ f * Prod sβ‚‚ f := have H1 : disjoint s₁ sβ‚‚ β†’ Prod (s₁ βˆͺ sβ‚‚) f = Prod s₁ f * Prod sβ‚‚ f, from quot.induction_onβ‚‚ s₁ sβ‚‚ (Ξ» l₁ lβ‚‚ d, Prodl_union f d), H1 (disjoint_of_inter_eq_empty disj) theorem Prod_ext {s : finset A} {f g : A β†’ B} : (βˆ€{x}, x ∈ s β†’ f x = g x) β†’ Prod s f = Prod s g := finset.induction_on s (assume H, rfl) (take x s', assume H1 : x βˆ‰ s', assume IH : (βˆ€ {x : A}, x ∈ s' β†’ f x = g x) β†’ Prod s' f = Prod s' g, assume H2 : βˆ€{y}, y ∈ insert x s' β†’ f y = g y, assert H3 : βˆ€y, y ∈ s' β†’ f y = g y, from take y, assume H', H2 (mem_insert_of_mem _ H'), assert H4 : f x = g x, from H2 !mem_insert, by rewrite [Prod_insert_of_not_mem f H1, Prod_insert_of_not_mem g H1, IH H3, H4]) end deceqA theorem Prod_one (s : finset A) : Prod s (Ξ» x, 1) = (1:B) := quot.induction_on s (take u, !Prodl_one) end finset section add_monoid variable [amB : add_monoid B] include amB local attribute add_monoid.to_monoid [trans-instance] definition Suml (l : list A) (f : A β†’ B) : B := Prodl l f -- βˆ‘ x ← l, f x notation `βˆ‘` binders `←` l, r:(scoped f, Suml l f) := r theorem Suml_nil (f : A β†’ B) : Suml [] f = 0 := Prodl_nil f theorem Suml_cons (f : A β†’ B) (a : A) (l : list A) : Suml (a::l) f = f a + Suml l f := Prodl_cons f a l theorem Suml_append (l₁ lβ‚‚ : list A) (f : A β†’ B) : Suml (l₁++lβ‚‚) f = Suml l₁ f + Suml lβ‚‚ f := Prodl_append l₁ lβ‚‚ f section deceqA include deceqA theorem Suml_insert_of_mem (f : A β†’ B) {a : A} {l : list A} (H : a ∈ l) : Suml (insert a l) f = Suml l f := Prodl_insert_of_mem f H theorem Suml_insert_of_not_mem (f : A β†’ B) {a : A} {l : list A} (H : a βˆ‰ l) : Suml (insert a l) f = f a + Suml l f := Prodl_insert_of_not_mem f H theorem Suml_union {l₁ lβ‚‚ : list A} (f : A β†’ B) (d : disjoint l₁ lβ‚‚) : Suml (union l₁ lβ‚‚) f = Suml l₁ f + Suml lβ‚‚ f := Prodl_union f d end deceqA theorem Suml_zero (l : list A) : Suml l (Ξ» x, 0) = (0:B) := Prodl_one l end add_monoid section add_comm_monoid variable [acmB : add_comm_monoid B] include acmB local attribute add_comm_monoid.to_comm_monoid [trans-instance] theorem Suml_add (l : list A) (f g : A β†’ B) : Suml l (Ξ»x, f x + g x) = Suml l f + Suml l g := Prodl_mul l f g end add_comm_monoid /- Sum -/ namespace finset variable [acmB : add_comm_monoid B] include acmB local attribute add_comm_monoid.to_comm_monoid [trans-instance] definition Sum (s : finset A) (f : A β†’ B) : B := Prod s f -- βˆ‘ x ∈ s, f x notation `βˆ‘` binders `∈` s, r:(scoped f, Sum s f) := r theorem Sum_empty (f : A β†’ B) : Sum βˆ… f = 0 := Prod_empty f theorem Sum_add (s : finset A) (f g : A β†’ B) : Sum s (Ξ»x, f x + g x) = Sum s f + Sum s g := Prod_mul s f g section deceqA include deceqA theorem Sum_insert_of_mem (f : A β†’ B) {a : A} {s : finset A} (H : a ∈ s) : Sum (insert a s) f = Sum s f := Prod_insert_of_mem f H theorem Sum_insert_of_not_mem (f : A β†’ B) {a : A} {s : finset A} (H : a βˆ‰ s) : Sum (insert a s) f = f a + Sum s f := Prod_insert_of_not_mem f H theorem Sum_union (f : A β†’ B) {s₁ sβ‚‚ : finset A} (disj : s₁ ∩ sβ‚‚ = βˆ…) : Sum (s₁ βˆͺ sβ‚‚) f = Sum s₁ f + Sum sβ‚‚ f := Prod_union f disj theorem Sum_ext {s : finset A} {f g : A β†’ B} (H : βˆ€x, x ∈ s β†’ f x = g x) : Sum s f = Sum s g := Prod_ext H end deceqA theorem Sum_zero (s : finset A) : Sum s (Ξ» x, 0) = (0:B) := Prod_one s end finset end algebra
a4e912ce7111484e4bd3811bf34916a540a05cda
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/struc_names.lean
feaa79d0fcfc566f7188af80d0494c723b9aede4
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
187
lean
namespace foo structure [class] structA := mk :: (a : nat) structure [class] structB extends structA := mk :: (b : nat) check @structA.a check @structB.to_structA end foo
2deea58c1a8452eb3d8e7f025ba8d9c04b0db3e8
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/linear_algebra/quadratic_form.lean
397ea4af81010eb1f959be027352697716530844
[ "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
38,956
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Kexing Ying -/ import algebra.invertible import linear_algebra.bilinear_form import linear_algebra.matrix.determinant import linear_algebra.special_linear_group import analysis.special_functions.pow import data.real.sign /-! # Quadratic forms This file defines quadratic forms over a `R`-module `M`. A quadratic form is a map `Q : M β†’ R` such that (`to_fun_smul`) `Q (a β€’ x) = a * a * Q x` (`polar_...`) The map `polar Q := Ξ» x y, Q (x + y) - Q x - Q y` is bilinear. They come with a scalar multiplication, `(a β€’ Q) x = Q (a β€’ x) = a * a * Q x`, and composition with linear maps `f`, `Q.comp f x = Q (f x)`. ## Main definitions * `quadratic_form.associated`: associated bilinear form * `quadratic_form.pos_def`: positive definite quadratic forms * `quadratic_form.anisotropic`: anisotropic quadratic forms * `quadratic_form.discr`: discriminant of a quadratic form ## Main statements * `quadratic_form.associated_left_inverse`, * `quadratic_form.associated_right_inverse`: in a commutative ring where 2 has an inverse, there is a correspondence between quadratic forms and symmetric bilinear forms * `bilin_form.exists_orthogonal_basis`: There exists an orthogonal basis with respect to any nondegenerate, symmetric bilinear form `B`. ## Notation In this file, the variable `R` is used when a `ring` structure is sufficient and `R₁` is used when specifically a `comm_ring` is required. This allows us to keep `[module R M]` and `[module R₁ M]` assumptions in the variables without confusion between `*` from `ring` and `*` from `comm_ring`. The variable `S` is used when `R` itself has a `β€’` action. ## References * https://en.wikipedia.org/wiki/Quadratic_form * https://en.wikipedia.org/wiki/Discriminant#Quadratic_forms ## Tags quadratic form, homogeneous polynomial, quadratic polynomial -/ universes u v w variables {S : Type*} variables {R : Type*} {M : Type*} [add_comm_group M] [ring R] variables {R₁ : Type*} [comm_ring R₁] namespace quadratic_form /-- Up to a factor 2, `Q.polar` is the associated bilinear form for a quadratic form `Q`.d Source of this name: https://en.wikipedia.org/wiki/Quadratic_form#Generalization -/ def polar (f : M β†’ R) (x y : M) := f (x + y) - f x - f y lemma polar_add (f g : M β†’ R) (x y : M) : polar (f + g) x y = polar f x y + polar g x y := by { simp only [polar, pi.add_apply], abel } lemma polar_neg (f : M β†’ R) (x y : M) : polar (-f) x y = - polar f x y := by { simp only [polar, pi.neg_apply, sub_eq_add_neg, neg_add] } lemma polar_smul [monoid S] [distrib_mul_action S R] (f : M β†’ R) (s : S) (x y : M) : polar (s β€’ f) x y = s β€’ polar f x y := by { simp only [polar, pi.smul_apply, smul_sub] } lemma polar_comm (f : M β†’ R) (x y : M) : polar f x y = polar f y x := by rw [polar, polar, add_comm, sub_sub, sub_sub, add_comm (f x) (f y)] end quadratic_form variables [module R M] [module R₁ M] open quadratic_form /-- A quadratic form over a module. -/ structure quadratic_form (R : Type u) (M : Type v) [ring R] [add_comm_group M] [module R M] := (to_fun : M β†’ R) (to_fun_smul : βˆ€ (a : R) (x : M), to_fun (a β€’ x) = a * a * to_fun x) (polar_add_left' : βˆ€ (x x' y : M), polar to_fun (x + x') y = polar to_fun x y + polar to_fun x' y) (polar_smul_left' : βˆ€ (a : R) (x y : M), polar to_fun (a β€’ x) y = a β€’ polar to_fun x y) (polar_add_right' : βˆ€ (x y y' : M), polar to_fun x (y + y') = polar to_fun x y + polar to_fun x y') (polar_smul_right' : βˆ€ (a : R) (x y : M), polar to_fun x (a β€’ y) = a β€’ polar to_fun x y) namespace quadratic_form variables {Q : quadratic_form R M} instance : has_coe_to_fun (quadratic_form R M) := ⟨_, to_fun⟩ /-- The `simp` normal form for a quadratic form is `coe_fn`, not `to_fun`. -/ @[simp] lemma to_fun_eq_apply : Q.to_fun = ⇑ Q := rfl lemma map_smul (a : R) (x : M) : Q (a β€’ x) = a * a * Q x := Q.to_fun_smul a x lemma map_add_self (x : M) : Q (x + x) = 4 * Q x := by { rw [←one_smul R x, ←add_smul, map_smul], norm_num } @[simp] lemma map_zero : Q 0 = 0 := by rw [←@zero_smul R _ _ _ _ (0 : M), map_smul, zero_mul, zero_mul] @[simp] lemma map_neg (x : M) : Q (-x) = Q x := by rw [←@neg_one_smul R _ _ _ _ x, map_smul, neg_one_mul, neg_neg, one_mul] lemma map_sub (x y : M) : Q (x - y) = Q (y - x) := by rw [←neg_sub, map_neg] @[simp] lemma polar_zero_left (y : M) : polar Q 0 y = 0 := by simp [polar] @[simp] lemma polar_add_left (x x' y : M) : polar Q (x + x') y = polar Q x y + polar Q x' y := Q.polar_add_left' x x' y @[simp] lemma polar_smul_left (a : R) (x y : M) : polar Q (a β€’ x) y = a * polar Q x y := Q.polar_smul_left' a x y @[simp] lemma polar_neg_left (x y : M) : polar Q (-x) y = -polar Q x y := by rw [←neg_one_smul R x, polar_smul_left, neg_one_mul] @[simp] lemma polar_sub_left (x x' y : M) : polar Q (x - x') y = polar Q x y - polar Q x' y := by rw [sub_eq_add_neg, sub_eq_add_neg, polar_add_left, polar_neg_left] @[simp] lemma polar_zero_right (y : M) : polar Q y 0 = 0 := by simp [polar] @[simp] lemma polar_add_right (x y y' : M) : polar Q x (y + y') = polar Q x y + polar Q x y' := Q.polar_add_right' x y y' @[simp] lemma polar_smul_right (a : R) (x y : M) : polar Q x (a β€’ y) = a * polar Q x y := Q.polar_smul_right' a x y @[simp] lemma polar_neg_right (x y : M) : polar Q x (-y) = -polar Q x y := by rw [←neg_one_smul R y, polar_smul_right, neg_one_mul] @[simp] lemma polar_sub_right (x y y' : M) : polar Q x (y - y') = polar Q x y - polar Q x y' := by rw [sub_eq_add_neg, sub_eq_add_neg, polar_add_right, polar_neg_right] @[simp] lemma polar_self (x : M) : polar Q x x = 2 * Q x := begin rw [polar, map_add_self, sub_sub, sub_eq_iff_eq_add, ←two_mul, ←two_mul, ←mul_assoc], norm_num end section of_tower variables [comm_semiring S] [algebra S R] [module S M] [is_scalar_tower S R M] @[simp] lemma polar_smul_left_of_tower (a : S) (x y : M) : polar Q (a β€’ x) y = a β€’ polar Q x y := by rw [←is_scalar_tower.algebra_map_smul R a x, polar_smul_left, algebra.smul_def] @[simp] lemma polar_smul_right_of_tower (a : S) (x y : M) : polar Q x (a β€’ y) = a β€’ polar Q x y := by rw [←is_scalar_tower.algebra_map_smul R a y, polar_smul_right, algebra.smul_def] end of_tower variable {Q' : quadratic_form R M} @[ext] lemma ext (H : βˆ€ (x : M), Q x = Q' x) : Q = Q' := by { cases Q, cases Q', congr, funext, apply H } instance : has_zero (quadratic_form R M) := ⟨ { to_fun := Ξ» x, 0, to_fun_smul := Ξ» a x, by simp, polar_add_left' := Ξ» x x' y, by simp [polar], polar_smul_left' := Ξ» a x y, by simp [polar], polar_add_right' := Ξ» x y y', by simp [polar], polar_smul_right' := Ξ» a x y, by simp [polar] } ⟩ @[simp] lemma coe_fn_zero : ⇑(0 : quadratic_form R M) = 0 := rfl @[simp] lemma zero_apply (x : M) : (0 : quadratic_form R M) x = 0 := rfl instance : inhabited (quadratic_form R M) := ⟨0⟩ instance : has_add (quadratic_form R M) := ⟨ Ξ» Q Q', { to_fun := Q + Q', to_fun_smul := Ξ» a x, by simp only [pi.add_apply, map_smul, mul_add], polar_add_left' := Ξ» x x' y, by simp only [polar_add, polar_add_left, add_assoc, add_left_comm], polar_smul_left' := Ξ» a x y, by simp only [polar_add, smul_eq_mul, mul_add, polar_smul_left], polar_add_right' := Ξ» x y y', by simp only [polar_add, polar_add_right, add_assoc, add_left_comm], polar_smul_right' := Ξ» a x y, by simp only [polar_add, smul_eq_mul, mul_add, polar_smul_right] } ⟩ @[simp] lemma coe_fn_add (Q Q' : quadratic_form R M) : ⇑(Q + Q') = Q + Q' := rfl @[simp] lemma add_apply (Q Q' : quadratic_form R M) (x : M) : (Q + Q') x = Q x + Q' x := rfl instance : has_neg (quadratic_form R M) := ⟨ Ξ» Q, { to_fun := -Q, to_fun_smul := Ξ» a x, by simp only [pi.neg_apply, map_smul, mul_neg_eq_neg_mul_symm], polar_add_left' := Ξ» x x' y, by simp only [polar_neg, polar_add_left, neg_add], polar_smul_left' := Ξ» a x y, by simp only [polar_neg, polar_smul_left, mul_neg_eq_neg_mul_symm, smul_eq_mul], polar_add_right' := Ξ» x y y', by simp only [polar_neg, polar_add_right, neg_add], polar_smul_right' := Ξ» a x y, by simp only [polar_neg, polar_smul_right, mul_neg_eq_neg_mul_symm, smul_eq_mul] } ⟩ @[simp] lemma coe_fn_neg (Q : quadratic_form R M) : ⇑(-Q) = -Q := rfl @[simp] lemma neg_apply (Q : quadratic_form R M) (x : M) : (-Q) x = -Q x := rfl instance : add_comm_group (quadratic_form R M) := { add := (+), zero := 0, neg := has_neg.neg, add_comm := Ξ» Q Q', by { ext, simp only [add_apply, add_comm] }, add_assoc := Ξ» Q Q' Q'', by { ext, simp only [add_apply, add_assoc] }, add_left_neg := Ξ» Q, by { ext, simp only [add_apply, neg_apply, zero_apply, add_left_neg] }, add_zero := Ξ» Q, by { ext, simp only [zero_apply, add_apply, add_zero] }, zero_add := Ξ» Q, by { ext, simp only [zero_apply, add_apply, zero_add] } } @[simp] lemma coe_fn_sub (Q Q' : quadratic_form R M) : ⇑(Q - Q') = Q - Q' := by simp [sub_eq_add_neg] @[simp] lemma sub_apply (Q Q' : quadratic_form R M) (x : M) : (Q - Q') x = Q x - Q' x := by simp [sub_eq_add_neg] /-- `@coe_fn (quadratic_form R M)` as an `add_monoid_hom`. This API mirrors `add_monoid_hom.coe_fn`. -/ @[simps apply] def coe_fn_add_monoid_hom : quadratic_form R M β†’+ (M β†’ R) := { to_fun := coe_fn, map_zero' := coe_fn_zero, map_add' := coe_fn_add } /-- Evaluation on a particular element of the module `M` is an additive map over quadratic forms. -/ @[simps apply] def eval_add_monoid_hom (m : M) : quadratic_form R M β†’+ R := (pi.eval_add_monoid_hom _ m).comp coe_fn_add_monoid_hom section sum open_locale big_operators @[simp] lemma coe_fn_sum {ΞΉ : Type*} (Q : ΞΉ β†’ quadratic_form R M) (s : finset ΞΉ) : ⇑(βˆ‘ i in s, Q i) = βˆ‘ i in s, Q i := (coe_fn_add_monoid_hom : _ β†’+ (M β†’ R)).map_sum Q s @[simp] lemma sum_apply {ΞΉ : Type*} (Q : ΞΉ β†’ quadratic_form R M) (s : finset ΞΉ) (x : M) : (βˆ‘ i in s, Q i) x = βˆ‘ i in s, Q i x := (eval_add_monoid_hom x : _ β†’+ R).map_sum Q s end sum section has_scalar variables [monoid S] [distrib_mul_action S R] [smul_comm_class S R R] /-- `quadratic_form R M` inherits the scalar action from any algebra over `R`. When `R` is commutative, this provides an `R`-action via `algebra.id`. -/ instance : has_scalar S (quadratic_form R M) := ⟨ Ξ» a Q, { to_fun := a β€’ Q, to_fun_smul := Ξ» b x, by rw [pi.smul_apply, map_smul, pi.smul_apply, mul_smul_comm], polar_add_left' := Ξ» x x' y, by simp only [polar_smul, polar_add_left, smul_add], polar_smul_left' := Ξ» b x y, begin simp only [polar_smul, polar_smul_left, ←mul_smul_comm, smul_eq_mul], end, polar_add_right' := Ξ» x y y', by simp only [polar_smul, polar_add_right, smul_add], polar_smul_right' := Ξ» b x y, begin simp only [polar_smul, polar_smul_right, ←mul_smul_comm, smul_eq_mul], end } ⟩ @[simp] lemma coe_fn_smul (a : S) (Q : quadratic_form R M) : ⇑(a β€’ Q) = a β€’ Q := rfl @[simp] lemma smul_apply (a : S) (Q : quadratic_form R M) (x : M) : (a β€’ Q) x = a β€’ Q x := rfl instance : distrib_mul_action S (quadratic_form R M) := { mul_smul := Ξ» a b Q, ext (Ξ» x, by simp only [smul_apply, mul_smul]), one_smul := Ξ» Q, ext (Ξ» x, by simp), smul_add := Ξ» a Q Q', by { ext, simp only [add_apply, smul_apply, smul_add] }, smul_zero := Ξ» a, by { ext, simp only [zero_apply, smul_apply, smul_zero] }, } end has_scalar section module instance [semiring S] [module S R] [smul_comm_class S R R] : module S (quadratic_form R M) := { zero_smul := Ξ» Q, by { ext, simp only [zero_apply, smul_apply, zero_smul] }, add_smul := Ξ» a b Q, by { ext, simp only [add_apply, smul_apply, add_smul] } } end module section comp variables {N : Type v} [add_comm_group N] [module R N] /-- Compose the quadratic form with a linear function. -/ def comp (Q : quadratic_form R N) (f : M β†’β‚—[R] N) : quadratic_form R M := { to_fun := Ξ» x, Q (f x), to_fun_smul := Ξ» a x, by simp only [map_smul, f.map_smul], polar_add_left' := Ξ» x x' y, by convert polar_add_left (f x) (f x') (f y) using 1; simp only [polar, f.map_add], polar_smul_left' := Ξ» a x y, by convert polar_smul_left a (f x) (f y) using 1; simp only [polar, f.map_smul, f.map_add, smul_eq_mul], polar_add_right' := Ξ» x y y', by convert polar_add_right (f x) (f y) (f y') using 1; simp only [polar, f.map_add], polar_smul_right' := Ξ» a x y, by convert polar_smul_right a (f x) (f y) using 1; simp only [polar, f.map_smul, f.map_add, smul_eq_mul] } @[simp] lemma comp_apply (Q : quadratic_form R N) (f : M β†’β‚—[R] N) (x : M) : (Q.comp f) x = Q (f x) := rfl end comp section comm_ring /-- Create a quadratic form in a commutative ring by proving only one side of the bilinearity. -/ def mk_left (f : M β†’ R₁) (to_fun_smul : βˆ€ a x, f (a β€’ x) = a * a * f x) (polar_add_left : βˆ€ x x' y, polar f (x + x') y = polar f x y + polar f x' y) (polar_smul_left : βˆ€ a x y, polar f (a β€’ x) y = a * polar f x y) : quadratic_form R₁ M := { to_fun := f, to_fun_smul := to_fun_smul, polar_add_left' := polar_add_left, polar_smul_left' := polar_smul_left, polar_add_right' := Ξ» x y y', by rw [polar_comm, polar_add_left, polar_comm f y x, polar_comm f y' x], polar_smul_right' := Ξ» a x y, by rw [polar_comm, polar_smul_left, polar_comm f y x, smul_eq_mul] } /-- The product of linear forms is a quadratic form. -/ def lin_mul_lin (f g : M β†’β‚—[R₁] R₁) : quadratic_form R₁ M := mk_left (f * g) (Ξ» a x, by { simp, ring }) (Ξ» x x' y, by { simp [polar], ring }) (Ξ» a x y, by { simp [polar], ring }) @[simp] lemma lin_mul_lin_apply (f g : M β†’β‚—[R₁] R₁) (x) : lin_mul_lin f g x = f x * g x := rfl @[simp] lemma add_lin_mul_lin (f g h : M β†’β‚—[R₁] R₁) : lin_mul_lin (f + g) h = lin_mul_lin f h + lin_mul_lin g h := ext (Ξ» x, add_mul _ _ _) @[simp] lemma lin_mul_lin_add (f g h : M β†’β‚—[R₁] R₁) : lin_mul_lin f (g + h) = lin_mul_lin f g + lin_mul_lin f h := ext (Ξ» x, mul_add _ _ _) variables {N : Type v} [add_comm_group N] [module R₁ N] @[simp] lemma lin_mul_lin_comp (f g : M β†’β‚—[R₁] R₁) (h : N β†’β‚—[R₁] M) : (lin_mul_lin f g).comp h = lin_mul_lin (f.comp h) (g.comp h) := rfl variables {n : Type*} /-- `proj i j` is the quadratic form mapping the vector `x : n β†’ R₁` to `x i * x j` -/ def proj (i j : n) : quadratic_form R₁ (n β†’ R₁) := lin_mul_lin (@linear_map.proj _ _ _ (Ξ» _, R₁) _ _ i) (@linear_map.proj _ _ _ (Ξ» _, R₁) _ _ j) @[simp] lemma proj_apply (i j : n) (x : n β†’ R₁) : proj i j x = x i * x j := rfl end comm_ring end quadratic_form /-! ### Associated bilinear forms Over a commutative ring with an inverse of 2, the theory of quadratic forms is basically identical to that of symmetric bilinear forms. The map from quadratic forms to bilinear forms giving this identification is called the `associated` quadratic form. -/ variables {B : bilin_form R M} namespace bilin_form open quadratic_form lemma polar_to_quadratic_form (x y : M) : polar (Ξ» x, B x x) x y = B x y + B y x := by simp [polar, add_left, add_right, sub_eq_add_neg _ (B y y), add_comm (B y x) _, add_assoc] /-- A bilinear form gives a quadratic form by applying the argument twice. -/ def to_quadratic_form (B : bilin_form R M) : quadratic_form R M := ⟨ Ξ» x, B x x, Ξ» a x, by simp [smul_left, smul_right, mul_assoc], Ξ» x x' y, by simp [polar_to_quadratic_form, add_left, add_right, add_left_comm, add_assoc], Ξ» a x y, by simp [polar_to_quadratic_form, smul_left, smul_right, mul_add], Ξ» x y y', by simp [polar_to_quadratic_form, add_left, add_right, add_left_comm, add_assoc], Ξ» a x y, by simp [polar_to_quadratic_form, smul_left, smul_right, mul_add] ⟩ @[simp] lemma to_quadratic_form_apply (B : bilin_form R M) (x : M) : B.to_quadratic_form x = B x x := rfl end bilin_form namespace quadratic_form open bilin_form sym_bilin_form section associated_hom variables (S) [comm_semiring S] [algebra S R] variables [invertible (2 : R)] {B₁ : bilin_form R M} /-- `associated_hom` is the map that sends a quadratic form on a module `M` over `R` to its associated symmetric bilinear form. As provided here, this has the structure of an `S`-linear map where `S` is a commutative subring of `R`. Over a commutative ring, use `associated`, which gives an `R`-linear map. Over a general ring with no nontrivial distinguished commutative subring, use `associated'`, which gives an additive homomorphism (or more precisely a `β„€`-linear map.) -/ def associated_hom : quadratic_form R M β†’β‚—[S] bilin_form R M := { to_fun := Ξ» Q, { bilin := Ξ» x y, β…Ÿ2 * polar Q x y, bilin_add_left := Ξ» x y z, by rw [← mul_add, polar_add_left], bilin_smul_left := Ξ» x y z, begin have htwo : x * β…Ÿ2 = β…Ÿ2 * x := (commute.one_right x).bit0_right.inv_of_right, simp [polar_smul_left, ← mul_assoc, htwo] end, bilin_add_right := Ξ» x y z, by rw [← mul_add, polar_add_right], bilin_smul_right := Ξ» x y z, begin have htwo : x * β…Ÿ2 = β…Ÿ2 * x := (commute.one_right x).bit0_right.inv_of_right, simp [polar_smul_left, ← mul_assoc, htwo] end }, map_add' := Ξ» Q Q', by { ext, simp [bilin_form.add_apply, polar_add, mul_add] }, map_smul' := Ξ» s Q, by { ext, simp [polar_smul, algebra.mul_smul_comm] } } variables {Q : quadratic_form R M} {S} @[simp] lemma associated_apply (x y : M) : associated_hom S Q x y = β…Ÿ2 * (Q (x + y) - Q x - Q y) := rfl lemma associated_is_sym : is_sym (associated_hom S Q) := Ξ» x y, by simp only [associated_apply, add_comm, add_left_comm, sub_eq_add_neg] @[simp] lemma associated_comp {N : Type v} [add_comm_group N] [module R N] (f : N β†’β‚—[R] M) : associated_hom S (Q.comp f) = (associated_hom S Q).comp f f := by { ext, simp } lemma associated_to_quadratic_form (B : bilin_form R M) (x y : M) : associated_hom S B.to_quadratic_form x y = β…Ÿ2 * (B x y + B y x) := by simp [associated_apply, ←polar_to_quadratic_form, polar] lemma associated_left_inverse (h : is_sym B₁) : associated_hom S (B₁.to_quadratic_form) = B₁ := bilin_form.ext $ Ξ» x y, by rw [associated_to_quadratic_form, sym h x y, ←two_mul, ←mul_assoc, inv_of_mul_self, one_mul] lemma associated_right_inverse : (associated_hom S Q).to_quadratic_form = Q := quadratic_form.ext $ Ξ» x, calc (associated_hom S Q).to_quadratic_form x = β…Ÿ2 * (Q x + Q x) : by simp [map_add_self, bit0, add_mul, add_assoc] ... = Q x : by rw [← two_mul (Q x), ←mul_assoc, inv_of_mul_self, one_mul] lemma associated_eq_self_apply (x : M) : associated_hom S Q x x = Q x := begin rw [associated_apply, map_add_self], suffices : (β…Ÿ2) * (2 * Q x) = Q x, { convert this, simp only [bit0, add_mul, one_mul], abel }, simp [← mul_assoc], end /-- `associated'` is the `β„€`-linear map that sends a quadratic form on a module `M` over `R` to its associated symmetric bilinear form. -/ abbreviation associated' : quadratic_form R M β†’β‚—[β„€] bilin_form R M := associated_hom β„€ /-- There exists a non-null vector with respect to any quadratic form `Q` whose associated bilinear form is non-degenerate, i.e. there exists `x` such that `Q x β‰  0`. -/ lemma exists_quadratic_form_neq_zero [nontrivial M] {Q : quadratic_form R M} (hB₁ : Q.associated'.nondegenerate) : βˆƒ x, Q x β‰  0 := begin rw nondegenerate at hB₁, contrapose! hB₁, obtain ⟨x, hx⟩ := exists_ne (0 : M), refine ⟨x, Ξ» y, _, hx⟩, have : Q = 0 := quadratic_form.ext hB₁, simp [this] end end associated_hom section associated variables [invertible (2 : R₁)] -- Note: When possible, rather than writing lemmas about `associated`, write a lemma applying to -- the more general `associated_hom` and place it in the previous section. /-- `associated` is the linear map that sends a quadratic form over a commutative ring to its associated symmetric bilinear form. -/ abbreviation associated : quadratic_form R₁ M β†’β‚—[R₁] bilin_form R₁ M := associated_hom R₁ @[simp] lemma associated_lin_mul_lin (f g : M β†’β‚—[R₁] R₁) : (lin_mul_lin f g).associated = β…Ÿ(2 : R₁) β€’ (bilin_form.lin_mul_lin f g + bilin_form.lin_mul_lin g f) := by { ext, simp [bilin_form.add_apply, bilin_form.smul_apply], ring } end associated section anisotropic /-- An anisotropic quadratic form is zero only on zero vectors. -/ def anisotropic (Q : quadratic_form R M) : Prop := βˆ€ x, Q x = 0 β†’ x = 0 lemma not_anisotropic_iff_exists (Q : quadratic_form R M) : Β¬anisotropic Q ↔ βˆƒ x β‰  0, Q x = 0 := by simp only [anisotropic, not_forall, exists_prop, and_comm] /-- The associated bilinear form of an anisotropic quadratic form is nondegenerate. -/ lemma nondegenerate_of_anisotropic [invertible (2 : R)] (Q : quadratic_form R M) (hB : Q.anisotropic) : Q.associated'.nondegenerate := begin intros x hx, refine hB _ _, rw ← hx x, exact (associated_eq_self_apply x).symm, end end anisotropic section pos_def variables {Rβ‚‚ : Type u} [ordered_ring Rβ‚‚] [module Rβ‚‚ M] {Qβ‚‚ : quadratic_form Rβ‚‚ M} /-- A positive definite quadratic form is positive on nonzero vectors. -/ def pos_def (Qβ‚‚ : quadratic_form Rβ‚‚ M) : Prop := βˆ€ x β‰  0, 0 < Qβ‚‚ x lemma pos_def.smul {R} [linear_ordered_comm_ring R] [module R M] {Q : quadratic_form R M} (h : pos_def Q) {a : R} (a_pos : 0 < a) : pos_def (a β€’ Q) := Ξ» x hx, mul_pos a_pos (h x hx) variables {n : Type*} lemma pos_def.add (Q Q' : quadratic_form Rβ‚‚ M) (hQ : pos_def Q) (hQ' : pos_def Q') : pos_def (Q + Q') := Ξ» x hx, add_pos (hQ x hx) (hQ' x hx) lemma lin_mul_lin_self_pos_def {R} [linear_ordered_comm_ring R] [module R M] (f : M β†’β‚—[R] R) (hf : linear_map.ker f = βŠ₯) : pos_def (lin_mul_lin f f) := Ξ» x hx, mul_self_pos (Ξ» h, hx (linear_map.ker_eq_bot.mp hf (by rw [h, linear_map.map_zero]))) end pos_def end quadratic_form section /-! ### Quadratic forms and matrices Connect quadratic forms and matrices, in order to explicitly compute with them. The convention is twos out, so there might be a factor 2⁻¹ in the entries of the matrix. The determinant of the matrix is the discriminant of the quadratic form. -/ variables {n : Type w} [fintype n] [decidable_eq n] /-- `M.to_quadratic_form` is the map `Ξ» x, col x ⬝ M ⬝ row x` as a quadratic form. -/ def matrix.to_quadratic_form' (M : matrix n n R₁) : quadratic_form R₁ (n β†’ R₁) := M.to_bilin'.to_quadratic_form variables [invertible (2 : R₁)] /-- A matrix representation of the quadratic form. -/ def quadratic_form.to_matrix' (Q : quadratic_form R₁ (n β†’ R₁)) : matrix n n R₁ := Q.associated.to_matrix' open quadratic_form lemma quadratic_form.to_matrix'_smul (a : R₁) (Q : quadratic_form R₁ (n β†’ R₁)) : (a β€’ Q).to_matrix' = a β€’ Q.to_matrix' := by simp only [to_matrix', linear_equiv.map_smul, linear_map.map_smul] end namespace quadratic_form variables {n : Type w} [fintype n] variables [decidable_eq n] [invertible (2 : R₁)] variables {m : Type w} [decidable_eq m] [fintype m] open_locale matrix @[simp] lemma to_matrix'_comp (Q : quadratic_form R₁ (m β†’ R₁)) (f : (n β†’ R₁) β†’β‚—[R₁] (m β†’ R₁)) : (Q.comp f).to_matrix' = f.to_matrix'α΅€ ⬝ Q.to_matrix' ⬝ f.to_matrix' := by { ext, simp [to_matrix', bilin_form.to_matrix'_comp] } section discriminant variables {Q : quadratic_form R₁ (n β†’ R₁)} /-- The discriminant of a quadratic form generalizes the discriminant of a quadratic polynomial. -/ def discr (Q : quadratic_form R₁ (n β†’ R₁)) : R₁ := Q.to_matrix'.det lemma discr_smul (a : R₁) : (a β€’ Q).discr = a ^ fintype.card n * Q.discr := by simp only [discr, to_matrix'_smul, matrix.det_smul] lemma discr_comp (f : (n β†’ R₁) β†’β‚—[R₁] (n β†’ R₁)) : (Q.comp f).discr = f.to_matrix'.det * f.to_matrix'.det * Q.discr := by simp [discr, mul_left_comm, mul_comm] end discriminant end quadratic_form namespace quadratic_form variables {M₁ : Type*} {Mβ‚‚ : Type*} {M₃ : Type*} variables [add_comm_group M₁] [add_comm_group Mβ‚‚] [add_comm_group M₃] variables [module R M₁] [module R Mβ‚‚] [module R M₃] /-- An isometry between two quadratic spaces `M₁, Q₁` and `Mβ‚‚, Qβ‚‚` over a ring `R`, is a linear equivalence between `M₁` and `Mβ‚‚` that commutes with the quadratic forms. -/ @[nolint has_inhabited_instance] structure isometry (Q₁ : quadratic_form R M₁) (Qβ‚‚ : quadratic_form R Mβ‚‚) extends M₁ ≃ₗ[R] Mβ‚‚ := (map_app' : βˆ€ m, Qβ‚‚ (to_fun m) = Q₁ m) /-- Two quadratic forms over a ring `R` are equivalent if there exists an isometry between them: a linear equivalence that transforms one quadratic form into the other. -/ def equivalent (Q₁ : quadratic_form R M₁) (Qβ‚‚ : quadratic_form R Mβ‚‚) := nonempty (Q₁.isometry Qβ‚‚) namespace isometry variables {Q₁ : quadratic_form R M₁} {Qβ‚‚ : quadratic_form R Mβ‚‚} {Q₃ : quadratic_form R M₃} instance : has_coe (Q₁.isometry Qβ‚‚) (M₁ ≃ₗ[R] Mβ‚‚) := ⟨isometry.to_linear_equiv⟩ instance : has_coe_to_fun (Q₁.isometry Qβ‚‚) := { F := Ξ» _, M₁ β†’ Mβ‚‚, coe := Ξ» f, ⇑(f : M₁ ≃ₗ[R] Mβ‚‚) } @[simp] lemma map_app (f : Q₁.isometry Qβ‚‚) (m : M₁) : Qβ‚‚ (f m) = Q₁ m := f.map_app' m /-- The identity isometry from a quadratic form to itself. -/ @[refl] def refl (Q : quadratic_form R M) : Q.isometry Q := { map_app' := Ξ» m, rfl, .. linear_equiv.refl R M } /-- The inverse isometry of an isometry between two quadratic forms. -/ @[symm] def symm (f : Q₁.isometry Qβ‚‚) : Qβ‚‚.isometry Q₁ := { map_app' := by { intro m, rw ← f.map_app, congr, exact f.to_linear_equiv.apply_symm_apply m }, .. (f : M₁ ≃ₗ[R] Mβ‚‚).symm } /-- The composition of two isometries between quadratic forms. -/ @[trans] def trans (f : Q₁.isometry Qβ‚‚) (g : Qβ‚‚.isometry Q₃) : Q₁.isometry Q₃ := { map_app' := by { intro m, rw [← f.map_app, ← g.map_app], refl }, .. (f : M₁ ≃ₗ[R] Mβ‚‚).trans (g : Mβ‚‚ ≃ₗ[R] M₃) } end isometry namespace equivalent variables {Q₁ : quadratic_form R M₁} {Qβ‚‚ : quadratic_form R Mβ‚‚} {Q₃ : quadratic_form R M₃} @[refl] lemma refl (Q : quadratic_form R M) : Q.equivalent Q := ⟨isometry.refl Q⟩ @[symm] lemma symm (h : Q₁.equivalent Qβ‚‚) : Qβ‚‚.equivalent Q₁ := h.elim $ Ξ» f, ⟨f.symm⟩ @[trans] lemma trans (h : Q₁.equivalent Qβ‚‚) (h' : Qβ‚‚.equivalent Q₃) : Q₁.equivalent Q₃ := h'.elim $ h.elim $ Ξ» f g, ⟨f.trans g⟩ end equivalent end quadratic_form namespace bilin_form /-- A bilinear form is nondegenerate if the quadratic form it is associated with is anisotropic. -/ lemma nondegenerate_of_anisotropic {B : bilin_form R M} (hB : B.to_quadratic_form.anisotropic) : B.nondegenerate := Ξ» x hx, hB _ (hx x) /-- There exists a non-null vector with respect to any symmetric, nondegenerate bilinear form `B` on a nontrivial module `M` over a ring `R` with invertible `2`, i.e. there exists some `x : M` such that `B x x β‰  0`. -/ lemma exists_bilin_form_self_neq_zero [htwo : invertible (2 : R)] [nontrivial M] {B : bilin_form R M} (hB₁ : B.nondegenerate) (hBβ‚‚ : sym_bilin_form.is_sym B) : βˆƒ x, Β¬ B.is_ortho x x := begin have : B.to_quadratic_form.associated'.nondegenerate, { simpa [quadratic_form.associated_left_inverse hBβ‚‚] using hB₁ }, obtain ⟨x, hx⟩ := quadratic_form.exists_quadratic_form_neq_zero this, refine ⟨x, Ξ» h, hx (B.to_quadratic_form_apply x β–Έ h)⟩, end open finite_dimensional variables {V : Type u} {K : Type v} [field K] [add_comm_group V] [module K V] variable [finite_dimensional K V] -- We start proving that symmetric nondegenerate bilinear forms are diagonalisable, or equivalently -- there exists a orthogonal basis with respect to any symmetric nondegenerate bilinear form. lemma exists_orthogonal_basis' [hK : invertible (2 : K)] {B : bilin_form K V} (hB₁ : B.nondegenerate) (hBβ‚‚ : sym_bilin_form.is_sym B) : βˆƒ (v : basis (fin (finrank K V)) K V), B.is_Ortho v ∧ βˆ€ i, B (v i) (v i) β‰  0 := begin tactic.unfreeze_local_instances, induction hd : finrank K V with d ih generalizing V, { exact ⟨basis_of_finrank_zero hd, Ξ» _ _ _, zero_left _, fin.elim0⟩ }, haveI := finrank_pos_iff.1 (hd.symm β–Έ nat.succ_pos d : 0 < finrank K V), cases exists_bilin_form_self_neq_zero hB₁ hBβ‚‚ with x hx, have hd' := hd, rw [← submodule.finrank_add_eq_of_is_compl (is_compl_span_singleton_orthogonal hx).symm, finrank_span_singleton (ne_zero_of_not_is_ortho_self x hx)] at hd, rcases @ih (B.orthogonal $ K βˆ™ x) _ _ _ (B.restrict _) (B.restrict_orthogonal_span_singleton_nondegenerate hB₁ hBβ‚‚ hx) (B.restrict_sym hBβ‚‚ _) (nat.succ.inj hd) with ⟨v', hv₁, hvβ‚ƒβŸ©, set v := (Ξ» (i : fin _), if h : i = 0 then x else coe (v' (i.pred h))) with v_def, have : βˆ€ i j (hij : i β‰  j), B.is_ortho (v i) (v j), { intros i j hij, simp only [v_def], split_ifs with hi hj hj, { have : i = j := hi.trans hj.symm, contradiction }, { exact (v' (j.pred hj)).2 _ (submodule.mem_span_singleton_self x) }, { rw [is_ortho, hBβ‚‚], exact (v' (i.pred hi)).2 _ (submodule.mem_span_singleton_self x) }, { exact hv₁ (j.pred hj) (i.pred hi) (by simpa using hij.symm) } }, refine ⟨@basis_of_linear_independent_of_card_eq_finrank _ _ _ _ _ _ _ _ v (@linear_independent_of_is_Ortho _ _ _ _ _ _ B v (Ξ» i j hij, this j i hij.symm) _) (by rw [hd', fintype.card_fin]), _, _⟩, { intro i, simp only [v_def], split_ifs with hi, { exact hx }, { exact hv₃ (i.pred hi) } }, { intros i j hij, simp only [v_def, basis_of_linear_independent_of_card_eq_finrank, basis.mk_apply], exact this j i hij.symm }, { intro i, simp only [v_def, basis_of_linear_independent_of_card_eq_finrank, basis.mk_apply], split_ifs with hi, { exact hx }, { exact hv₃ (i.pred hi) } } end . /-- Given a nondegenerate symmetric bilinear form `B` on some vector space `V` over the field `K` with invertible `2`, there exists an orthogonal basis with respect to `B`. -/ theorem exists_orthogonal_basis [hK : invertible (2 : K)] {B : bilin_form K V} (hB₁ : B.nondegenerate) (hBβ‚‚ : sym_bilin_form.is_sym B) : βˆƒ v : basis (fin (finrank K V)) K V, B.is_Ortho v := let ⟨v, hv₁, _⟩ := exists_orthogonal_basis' hB₁ hBβ‚‚ in ⟨v, hvβ‚βŸ© end bilin_form namespace quadratic_form open_locale big_operators open finset bilin_form variables {M₁ : Type*} [add_comm_group M₁] [module R M₁] variables {ΞΉ : Type*} [fintype ΞΉ] {v : basis ΞΉ R M} /-- A quadratic form composed with a `linear_equiv` is isometric to itself. -/ def isometry_of_comp_linear_equiv (Q : quadratic_form R M) (f : M₁ ≃ₗ[R] M) : Q.isometry (Q.comp (f : M₁ β†’β‚—[R] M)) := { map_app' := begin intro, simp only [comp_apply, linear_equiv.coe_coe, linear_equiv.to_fun_eq_coe, linear_equiv.apply_symm_apply, f.apply_symm_apply], end, .. f.symm } /-- Given a quadratic form `Q` and a basis, `basis_repr` is the basis representation of `Q`. -/ noncomputable def basis_repr (Q : quadratic_form R M) (v : basis ΞΉ R M) : quadratic_form R (ΞΉ β†’ R) := Q.comp v.equiv_fun.symm @[simp] lemma basis_repr_apply (Q : quadratic_form R M) (w : ΞΉ β†’ R) : Q.basis_repr v w = Q (βˆ‘ i : ΞΉ, w i β€’ v i) := by { rw ← v.equiv_fun_symm_apply, refl } /-- A quadratic form is isometric to its bases representations. -/ noncomputable def isometry_basis_repr (Q : quadratic_form R M) (v : basis ΞΉ R M): isometry Q (Q.basis_repr v) := isometry_of_comp_linear_equiv Q v.equiv_fun.symm lemma isometry_of_is_Ortho_apply [invertible (2 : R₁)] (Q : quadratic_form R₁ M) (v : basis ΞΉ R₁ M) (hvβ‚‚ : (associated Q).is_Ortho v) (w : ΞΉ β†’ R₁) : Q.basis_repr v w = βˆ‘ i : ΞΉ, associated Q (v i) (v i) * (w i * w i) := begin rw [basis_repr_apply, ← @associated_eq_self_apply R₁, sum_left], refine sum_congr rfl (Ξ» j hj, _), rw [sum_right, sum_eq_single j], { rw [smul_left, smul_right], ring }, { intros i _ hij, rw [smul_left, smul_right, show (associated_hom R₁) Q (v j) (v i) = 0, by exact hvβ‚‚ i j hij, mul_zero, mul_zero] }, { contradiction } end section variable (R₁) /-- The weighted sum of squares with respect to some weight as a quadratic form. The weights are applied using `β€’`; typically this definition is used either with `S = R₁` or `[algebra S R₁]`, although this is stated more generally. -/ def weighted_sum_squares [monoid S] [distrib_mul_action S R₁] [smul_comm_class S R₁ R₁] (w : ΞΉ β†’ S) : quadratic_form R₁ (ΞΉ β†’ R₁) := βˆ‘ i : ΞΉ, w i β€’ proj i i end @[simp] lemma weighted_sum_squares_apply [monoid S] [distrib_mul_action S R₁] [smul_comm_class S R₁ R₁] (w : ΞΉ β†’ S) (v : ΞΉ β†’ R₁) : weighted_sum_squares R₁ w v = βˆ‘ i : ΞΉ, w i β€’ (v i * v i) := quadratic_form.sum_apply _ _ _ variables {V : Type*} {K : Type*} [field K] [invertible (2 : K)] variables [add_comm_group V] [module K V] [finite_dimensional K V] lemma equivalent_weighted_sum_squares_of_nondegenerate' (Q : quadratic_form K V) (hQ : (associated Q).nondegenerate) : βˆƒ w : fin (finite_dimensional.finrank K V) β†’ units K, equivalent Q (weighted_sum_squares K w) := begin obtain ⟨v, hv₁, hvβ‚‚βŸ© := exists_orthogonal_basis' hQ associated_is_sym, refine ⟨λ i, units.mk0 _ (hvβ‚‚ i), nonempty.intro _⟩, convert Q.isometry_basis_repr v, ext w, rw [isometry_of_is_Ortho_apply Q v hv₁, weighted_sum_squares_apply], refl end section complex /-- The isometry between a weighted sum of squares on the complex numbers and the sum of squares, i.e. `weighted_sum_squares` with weight `Ξ» i : ΞΉ, 1`. -/ noncomputable def isometry_sum_squares [decidable_eq ΞΉ] (w : ΞΉ β†’ units β„‚) : isometry (weighted_sum_squares β„‚ w) (weighted_sum_squares β„‚ (1 : ΞΉ β†’ β„‚)) := begin have hw' : βˆ€ i : ΞΉ, (w i : β„‚) ^ - (1 / 2 : β„‚) β‰  0, { intros i hi, exact (w i).ne_zero ((complex.cpow_eq_zero_iff _ _).1 hi).1 }, convert (weighted_sum_squares β„‚ w).isometry_basis_repr ((pi.basis_fun β„‚ ΞΉ).units_smul (Ξ» i, (is_unit_iff_ne_zero.2 $ hw' i).unit)), ext1 v, erw [basis_repr_apply, weighted_sum_squares_apply, weighted_sum_squares_apply], refine sum_congr rfl (Ξ» j hj, _), have hsum : (βˆ‘ (i : ΞΉ), v i β€’ ((is_unit_iff_ne_zero.2 $ hw' i).unit : β„‚) β€’ (pi.basis_fun β„‚ ΞΉ) i) j = v j β€’ w j ^ - (1 / 2 : β„‚), { rw [finset.sum_apply, sum_eq_single j, pi.basis_fun_apply, is_unit.unit_spec, linear_map.std_basis_apply, pi.smul_apply, pi.smul_apply, function.update_same, smul_eq_mul, smul_eq_mul, smul_eq_mul, mul_one], intros i _ hij, rw [pi.basis_fun_apply, linear_map.std_basis_apply, pi.smul_apply, pi.smul_apply, function.update_noteq hij.symm, pi.zero_apply, smul_eq_mul, smul_eq_mul, mul_zero, mul_zero], intro hj', exact false.elim (hj' hj) }, simp_rw basis.units_smul_apply, erw [hsum, smul_eq_mul], suffices : 1 * v j * v j = w j ^ - (1 / 2 : β„‚) * w j ^ - (1 / 2 : β„‚) * w j * v j * v j, { erw [pi.one_apply, ← mul_assoc, this, smul_eq_mul, smul_eq_mul], ring }, rw [← complex.cpow_add _ _ (w j).ne_zero, show - (1 / 2 : β„‚) + - (1 / 2) = -1, by ring, complex.cpow_neg_one, inv_mul_cancel (w j).ne_zero], end /-- A nondegenerate quadratic form on the complex numbers is equivalent to the sum of squares, i.e. `weighted_sum_squares` with weight `Ξ» i : ΞΉ, 1`. -/ theorem equivalent_sum_squares {M : Type*} [add_comm_group M] [module β„‚ M] [finite_dimensional β„‚ M] (Q : quadratic_form β„‚ M) (hQ : (associated Q).nondegenerate) : equivalent Q (weighted_sum_squares β„‚ (1 : fin (finite_dimensional.finrank β„‚ M) β†’ β„‚)) := let ⟨w, ⟨hwβ‚βŸ©βŸ© := Q.equivalent_weighted_sum_squares_of_nondegenerate' hQ in ⟨hw₁.trans (isometry_sum_squares w)⟩ end complex section real open real /-- The isometry between a weighted sum of squares with weights `u` on the (non-zero) real numbers and the weighted sum of squares with weights `sign ∘ u`. -/ noncomputable def isometry_sign_weighted_sum_squares [decidable_eq ΞΉ] (u : ΞΉ β†’ units ℝ) : isometry (weighted_sum_squares ℝ u) (weighted_sum_squares ℝ (sign ∘ coe ∘ u)) := begin have hu' : βˆ€ i : ΞΉ, (sign (u i) * u i) ^ - (1 / 2 : ℝ) β‰  0, { intro i, refine (ne_of_lt (real.rpow_pos_of_pos (sign_mul_pos_of_ne_zero _ $ units.ne_zero _) _)).symm}, convert ((weighted_sum_squares ℝ u).isometry_basis_repr ((pi.basis_fun ℝ ΞΉ).units_smul (Ξ» i, (is_unit_iff_ne_zero.2 $ hu' i).unit))), ext1 v, rw [basis_repr_apply, weighted_sum_squares_apply, weighted_sum_squares_apply], refine sum_congr rfl (Ξ» j hj, _), have hsum : (βˆ‘ (i : ΞΉ), v i β€’ ((is_unit_iff_ne_zero.2 $ hu' i).unit : ℝ) β€’ (pi.basis_fun ℝ ΞΉ) i) j = v j β€’ (sign (u j) * u j) ^ - (1 / 2 : ℝ), { rw [finset.sum_apply, sum_eq_single j, pi.basis_fun_apply, is_unit.unit_spec, linear_map.std_basis_apply, pi.smul_apply, pi.smul_apply, function.update_same, smul_eq_mul, smul_eq_mul, smul_eq_mul, mul_one], intros i _ hij, rw [pi.basis_fun_apply, linear_map.std_basis_apply, pi.smul_apply, pi.smul_apply, function.update_noteq hij.symm, pi.zero_apply, smul_eq_mul, smul_eq_mul, mul_zero, mul_zero], intro hj', exact false.elim (hj' hj) }, simp_rw basis.units_smul_apply, erw [hsum, smul_eq_mul], suffices : (sign ∘ coe ∘ u) j * v j * v j = (sign (u j) * u j) ^ - (1 / 2 : ℝ) * (sign (u j) * u j) ^ - (1 / 2 : ℝ) * u j * v j * v j, { erw [← mul_assoc, this, smul_eq_mul, smul_eq_mul], ring }, rw [← real.rpow_add (sign_mul_pos_of_ne_zero _ $ units.ne_zero _), show - (1 / 2 : ℝ) + - (1 / 2) = -1, by ring, real.rpow_neg_one, _root_.mul_inv', inv_sign, mul_assoc (sign (u j)) (u j)⁻¹, inv_mul_cancel (units.ne_zero _), mul_one], apply_instance end /-- **Sylvester's law of inertia**: A nondegenerate real quadratic form is equivalent to a weighted sum of squares with the weights being Β±1. -/ theorem equivalent_one_neg_one_weighted_sum_squared {M : Type*} [add_comm_group M] [module ℝ M] [finite_dimensional ℝ M] (Q : quadratic_form ℝ M) (hQ : (associated Q).nondegenerate) : βˆƒ w : fin (finite_dimensional.finrank ℝ M) β†’ ℝ, (βˆ€ i, w i = -1 ∨ w i = 1) ∧ equivalent Q (weighted_sum_squares ℝ w) := let ⟨w, ⟨hwβ‚βŸ©βŸ© := Q.equivalent_weighted_sum_squares_of_nondegenerate' hQ in ⟨sign ∘ coe ∘ w, Ξ» i, sign_apply_eq_of_ne_zero (w i) (w i).ne_zero, ⟨hw₁.trans (isometry_sign_weighted_sum_squares w)⟩⟩ end real end quadratic_form
e61fe5a5a0eb9fc846425979ae0202df38791d7b
b3fced0f3ff82d577384fe81653e47df68bb2fa1
/src/data/matrix/basic.lean
c7a30600fd6f51d6e9e1c9f7fbfcaa24ae10caf1
[ "Apache-2.0" ]
permissive
ratmice/mathlib
93b251ef5df08b6fd55074650ff47fdcc41a4c75
3a948a6a4cd5968d60e15ed914b1ad2f4423af8d
refs/heads/master
1,599,240,104,318
1,572,981,183,000
1,572,981,183,000
219,830,178
0
0
Apache-2.0
1,572,980,897,000
1,572,980,896,000
null
UTF-8
Lean
false
false
12,054
lean
/- Copyright (c) 2018 Ellen Arlt. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin Matrices -/ import algebra.module algebra.pi_instances import data.fintype universes u v def matrix (m n : Type u) [fintype m] [fintype n] (Ξ± : Type v) : Type (max u v) := m β†’ n β†’ Ξ± namespace matrix variables {l m n o : Type u} [fintype l] [fintype m] [fintype n] [fintype o] variables {Ξ± : Type v} section ext variables {M N : matrix m n Ξ±} theorem ext_iff : (βˆ€ i j, M i j = N i j) ↔ M = N := ⟨λ h, funext $ Ξ» i, funext $ h i, Ξ» h, by simp [h]⟩ @[extensionality] theorem ext : (βˆ€ i j, M i j = N i j) β†’ M = N := ext_iff.mp end ext def transpose (M : matrix m n Ξ±) : matrix n m Ξ± | x y := M y x localized "postfix `α΅€`:1500 := matrix.transpose" in matrix def col (w : m β†’ Ξ±) : matrix m punit Ξ± | x y := w x def row (v : n β†’ Ξ±) : matrix punit n Ξ± | x y := v y instance [has_add Ξ±] : has_add (matrix m n Ξ±) := pi.has_add instance [add_semigroup Ξ±] : add_semigroup (matrix m n Ξ±) := pi.add_semigroup instance [add_comm_semigroup Ξ±] : add_comm_semigroup (matrix m n Ξ±) := pi.add_comm_semigroup instance [has_zero Ξ±] : has_zero (matrix m n Ξ±) := pi.has_zero instance [add_monoid Ξ±] : add_monoid (matrix m n Ξ±) := pi.add_monoid instance [add_comm_monoid Ξ±] : add_comm_monoid (matrix m n Ξ±) := pi.add_comm_monoid instance [has_neg Ξ±] : has_neg (matrix m n Ξ±) := pi.has_neg instance [add_group Ξ±] : add_group (matrix m n Ξ±) := pi.add_group instance [add_comm_group Ξ±] : add_comm_group (matrix m n Ξ±) := pi.add_comm_group @[simp] theorem zero_val [has_zero Ξ±] (i j) : (0 : matrix m n Ξ±) i j = 0 := rfl @[simp] theorem neg_val [has_neg Ξ±] (M : matrix m n Ξ±) (i j) : (- M) i j = - M i j := rfl @[simp] theorem add_val [has_add Ξ±] (M N : matrix m n Ξ±) (i j) : (M + N) i j = M i j + N i j := rfl section diagonal variables [decidable_eq n] def diagonal [has_zero Ξ±] (d : n β†’ Ξ±) : matrix n n Ξ± := Ξ» i j, if i = j then d i else 0 @[simp] theorem diagonal_val_eq [has_zero Ξ±] {d : n β†’ Ξ±} (i : n) : (diagonal d) i i = d i := by simp [diagonal] @[simp] theorem diagonal_val_ne [has_zero Ξ±] {d : n β†’ Ξ±} {i j : n} (h : i β‰  j) : (diagonal d) i j = 0 := by simp [diagonal, h] theorem diagonal_val_ne' [has_zero Ξ±] {d : n β†’ Ξ±} {i j : n} (h : j β‰  i) : (diagonal d) i j = 0 := diagonal_val_ne h.symm @[simp] theorem diagonal_zero [has_zero Ξ±] : (diagonal (Ξ» _, 0) : matrix n n Ξ±) = 0 := by simp [diagonal]; refl section one variables [has_zero Ξ±] [has_one Ξ±] instance : has_one (matrix n n Ξ±) := ⟨diagonal (Ξ» _, 1)⟩ @[simp] theorem diagonal_one : (diagonal (Ξ» _, 1) : matrix n n Ξ±) = 1 := rfl theorem one_val {i j} : (1 : matrix n n Ξ±) i j = if i = j then 1 else 0 := rfl @[simp] theorem one_val_eq (i) : (1 : matrix n n Ξ±) i i = 1 := diagonal_val_eq i @[simp] theorem one_val_ne {i j} : i β‰  j β†’ (1 : matrix n n Ξ±) i j = 0 := diagonal_val_ne theorem one_val_ne' {i j} : j β‰  i β†’ (1 : matrix n n Ξ±) i j = 0 := diagonal_val_ne' end one end diagonal @[simp] theorem diagonal_add [decidable_eq n] [add_monoid Ξ±] (d₁ dβ‚‚ : n β†’ Ξ±) : diagonal d₁ + diagonal dβ‚‚ = diagonal (Ξ» i, d₁ i + dβ‚‚ i) := by ext i j; by_cases i = j; simp [h] protected def mul [has_mul Ξ±] [add_comm_monoid Ξ±] (M : matrix l m Ξ±) (N : matrix m n Ξ±) : matrix l n Ξ± := Ξ» i k, finset.univ.sum (Ξ» j, M i j * N j k) localized "infixl ` ⬝ `:75 := matrix.mul" in matrix theorem mul_val [has_mul Ξ±] [add_comm_monoid Ξ±] {M : matrix l m Ξ±} {N : matrix m n Ξ±} {i k} : (M ⬝ N) i k = finset.univ.sum (Ξ» j, M i j * N j k) := rfl local attribute [simp] mul_val instance [has_mul Ξ±] [add_comm_monoid Ξ±] : has_mul (matrix n n Ξ±) := ⟨matrix.mul⟩ @[simp] theorem mul_eq_mul [has_mul Ξ±] [add_comm_monoid Ξ±] (M N : matrix n n Ξ±) : M * N = M ⬝ N := rfl theorem mul_val' [has_mul Ξ±] [add_comm_monoid Ξ±] {M N : matrix n n Ξ±} {i k} : (M * N) i k = finset.univ.sum (Ξ» j, M i j * N j k) := rfl section semigroup variables [semiring Ξ±] protected theorem mul_assoc (L : matrix l m Ξ±) (M : matrix m n Ξ±) (N : matrix n o Ξ±) : (L ⬝ M) ⬝ N = L ⬝ (M ⬝ N) := by classical; funext i k; simp [finset.mul_sum, finset.sum_mul, mul_assoc]; rw finset.sum_comm instance : semigroup (matrix n n Ξ±) := { mul_assoc := matrix.mul_assoc, ..matrix.has_mul } end semigroup @[simp] theorem diagonal_neg [decidable_eq n] [add_group Ξ±] (d : n β†’ Ξ±) : -diagonal d = diagonal (Ξ» i, -d i) := by ext i j; by_cases i = j; simp [h] section semiring variables [semiring Ξ±] @[simp] protected theorem mul_zero (M : matrix m n Ξ±) : M ⬝ (0 : matrix n o Ξ±) = 0 := by ext i j; simp @[simp] protected theorem zero_mul (M : matrix m n Ξ±) : (0 : matrix l m Ξ±) ⬝ M = 0 := by ext i j; simp protected theorem mul_add (L : matrix m n Ξ±) (M N : matrix n o Ξ±) : L ⬝ (M + N) = L ⬝ M + L ⬝ N := by ext i j; simp [finset.sum_add_distrib, mul_add] protected theorem add_mul (L M : matrix l m Ξ±) (N : matrix m n Ξ±) : (L + M) ⬝ N = L ⬝ N + M ⬝ N := by ext i j; simp [finset.sum_add_distrib, add_mul] @[simp] theorem diagonal_mul [decidable_eq m] (d : m β†’ Ξ±) (M : matrix m n Ξ±) (i j) : (diagonal d).mul M i j = d i * M i j := by simp; rw finset.sum_eq_single i; simp [diagonal_val_ne'] {contextual := tt} @[simp] theorem mul_diagonal [decidable_eq n] (d : n β†’ Ξ±) (M : matrix m n Ξ±) (i j) : (M ⬝ diagonal d) i j = M i j * d j := by simp; rw finset.sum_eq_single j; simp {contextual := tt} @[simp] protected theorem one_mul [decidable_eq m] (M : matrix m n Ξ±) : (1 : matrix m m Ξ±) ⬝ M = M := by ext i j; rw [← diagonal_one, diagonal_mul, one_mul] @[simp] protected theorem mul_one [decidable_eq n] (M : matrix m n Ξ±) : M ⬝ (1 : matrix n n Ξ±) = M := by ext i j; rw [← diagonal_one, mul_diagonal, mul_one] instance [decidable_eq n] : monoid (matrix n n Ξ±) := { one_mul := matrix.one_mul, mul_one := matrix.mul_one, ..matrix.has_one, ..matrix.semigroup } instance [decidable_eq n] : semiring (matrix n n Ξ±) := { mul_zero := matrix.mul_zero, zero_mul := matrix.zero_mul, left_distrib := matrix.mul_add, right_distrib := matrix.add_mul, ..matrix.add_comm_monoid, ..matrix.monoid } @[simp] theorem diagonal_mul_diagonal' [decidable_eq n] (d₁ dβ‚‚ : n β†’ Ξ±) : (diagonal d₁) ⬝ (diagonal dβ‚‚) = diagonal (Ξ» i, d₁ i * dβ‚‚ i) := by ext i j; by_cases i = j; simp [h] theorem diagonal_mul_diagonal [decidable_eq n] (d₁ dβ‚‚ : n β†’ Ξ±) : diagonal d₁ * diagonal dβ‚‚ = diagonal (Ξ» i, d₁ i * dβ‚‚ i) := diagonal_mul_diagonal' _ _ lemma is_add_monoid_hom_mul_left (M : matrix l m Ξ±) : is_add_monoid_hom (Ξ» x : matrix m n Ξ±, M ⬝ x) := { to_is_add_hom := ⟨matrix.mul_add _⟩, map_zero := matrix.mul_zero _ } lemma is_add_monoid_hom_mul_right (M : matrix m n Ξ±) : is_add_monoid_hom (Ξ» x : matrix l m Ξ±, x ⬝ M) := { to_is_add_hom := ⟨λ _ _, matrix.add_mul _ _ _⟩, map_zero := matrix.zero_mul _ } protected lemma sum_mul {Ξ² : Type*} (s : finset Ξ²) (f : Ξ² β†’ matrix l m Ξ±) (M : matrix m n Ξ±) : s.sum f ⬝ M = s.sum (Ξ» a, f a ⬝ M) := (@finset.sum_hom _ _ _ s f _ _ (Ξ» x, x ⬝ M) /- This line does not type-check without `id` and `: _`. Lean did not recognize that two different `add_monoid` instances were def-eq -/ (id (@is_add_monoid_hom_mul_right l _ _ _ _ _ _ _ M) : _)).symm protected lemma mul_sum {Ξ² : Type*} (s : finset Ξ²) (f : Ξ² β†’ matrix m n Ξ±) (M : matrix l m Ξ±) : M ⬝ s.sum f = s.sum (Ξ» a, M ⬝ f a) := (@finset.sum_hom _ _ _ s f _ _ (Ξ» x, M ⬝ x) /- This line does not type-check without `id` and `: _`. Lean did not recognize that two different `add_monoid` instances were def-eq -/ (id (@is_add_monoid_hom_mul_left _ _ n _ _ _ _ _ M) : _)).symm end semiring section ring variables [ring Ξ±] @[simp] theorem neg_mul (M : matrix m n Ξ±) (N : matrix n o Ξ±) : (-M) ⬝ N = -(M ⬝ N) := by ext; simp [matrix.mul] @[simp] theorem mul_neg (M : matrix m n Ξ±) (N : matrix n o Ξ±) : M ⬝ (-N) = -(M ⬝ N) := by ext; simp [matrix.mul] end ring instance [decidable_eq n] [ring Ξ±] : ring (matrix n n Ξ±) := { ..matrix.add_comm_group, ..matrix.semiring } instance [semiring Ξ±] : has_scalar Ξ± (matrix m n Ξ±) := pi.has_scalar instance [ring Ξ±] : module Ξ± (matrix m n Ξ±) := pi.module _ @[simp] lemma smul_val [semiring Ξ±] (a : Ξ±) (A : matrix m n Ξ±) (i : m) (j : n) : (a β€’ A) i j = a * A i j := rfl section comm_ring variables [comm_ring Ξ±] @[simp] lemma mul_smul (M : matrix m n Ξ±) (a : Ξ±) (N : matrix n l Ξ±) : M ⬝ (a β€’ N) = a β€’ M ⬝ N := begin ext i j, unfold matrix.mul has_scalar.smul, rw finset.mul_sum, congr, ext, ac_refl end @[simp] lemma smul_mul (M : matrix m n Ξ±) (a : Ξ±) (N : matrix n l Ξ±) : (a β€’ M) ⬝ N = a β€’ M ⬝ N := begin ext i j, unfold matrix.mul has_scalar.smul, rw finset.mul_sum, congr, ext, ac_refl end end comm_ring section semiring variables [semiring Ξ±] def vec_mul_vec (w : m β†’ Ξ±) (v : n β†’ Ξ±) : matrix m n Ξ± | x y := w x * v y def mul_vec (M : matrix m n Ξ±) (v : n β†’ Ξ±) : m β†’ Ξ± | x := finset.univ.sum (Ξ»y:n, M x y * v y) def vec_mul (v : m β†’ Ξ±) (M : matrix m n Ξ±) : n β†’ Ξ± | y := finset.univ.sum (Ξ»x:m, v x * M x y) instance mul_vec.is_add_monoid_hom_left (v : n β†’ Ξ±) : is_add_monoid_hom (Ξ»M:matrix m n Ξ±, mul_vec M v) := { map_zero := by ext; simp [mul_vec]; refl, map_add := begin intros x y, ext m, rw pi.add_apply (mul_vec x v) (mul_vec y v) m, simp [mul_vec, finset.sum_add_distrib, right_distrib] end } lemma mul_vec_diagonal [decidable_eq m] (v w : m β†’ Ξ±) (x : m) : mul_vec (diagonal v) w x = v x * w x := begin transitivity, refine finset.sum_eq_single x _ _, { assume b _ ne, simp [diagonal, ne.symm] }, { simp }, { rw [diagonal_val_eq] } end lemma vec_mul_vec_eq (w : m β†’ Ξ±) (v : n β†’ Ξ±) : vec_mul_vec w v = (col w) ⬝ (row v) := by simp [matrix.mul]; refl end semiring section transpose open_locale matrix @[simp] lemma transpose_transpose (M : matrix m n Ξ±) : Mα΅€α΅€ = M := by ext; refl @[simp] lemma transpose_zero [has_zero Ξ±] : (0 : matrix m n Ξ±)α΅€ = 0 := by ext i j; refl @[simp] lemma transpose_add [has_add Ξ±] (M : matrix m n Ξ±) (N : matrix m n Ξ±) : (M + N)α΅€ = Mα΅€ + Nα΅€ := begin ext i j, dsimp [transpose], refl end @[simp] lemma transpose_mul [comm_ring Ξ±] (M : matrix m n Ξ±) (N : matrix n l Ξ±) : (M ⬝ N)α΅€ = Nα΅€ ⬝ Mα΅€ := begin ext i j, unfold matrix.mul transpose, congr, ext, ac_refl end @[simp] lemma transpose_neg [comm_ring Ξ±] (M : matrix m n Ξ±) : (- M)α΅€ = - Mα΅€ := by ext i j; refl end transpose def minor (A : matrix m n Ξ±) (row : l β†’ m) (col : o β†’ n) : matrix l o Ξ± := Ξ» i j, A (row i) (col j) @[reducible] def sub_left {m l r : nat} (A : matrix (fin m) (fin (l + r)) Ξ±) : matrix (fin m) (fin l) Ξ± := minor A id (fin.cast_add r) @[reducible] def sub_right {m l r : nat} (A : matrix (fin m) (fin (l + r)) Ξ±) : matrix (fin m) (fin r) Ξ± := minor A id (fin.nat_add l) @[reducible] def sub_up {d u n : nat} (A : matrix (fin (u + d)) (fin n) Ξ±) : matrix (fin u) (fin n) Ξ± := minor A (fin.cast_add d) id @[reducible] def sub_down {d u n : nat} (A : matrix (fin (u + d)) (fin n) Ξ±) : matrix (fin d) (fin n) Ξ± := minor A (fin.nat_add u) id @[reducible] def sub_up_right {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) Ξ±) : matrix (fin u) (fin r) Ξ± := sub_up (sub_right A) @[reducible] def sub_down_right {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) Ξ±) : matrix (fin d) (fin r) Ξ± := sub_down (sub_right A) @[reducible] def sub_up_left {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) Ξ±) : matrix (fin u) (fin (l)) Ξ± := sub_up (sub_left A) @[reducible] def sub_down_left {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) Ξ±) : matrix (fin d) (fin (l)) Ξ± := sub_down (sub_left A) end matrix
d5461aabb0a5ec4fac1e42cb69f7f3862db60ea7
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/robinson.lean
97230e854fa28fefd93f9d2f0b400d9082e3c56d
[ "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
2,037
lean
inductive Term | Var (i : Nat) | Cons (l : Term) (r : Term) def Subst := Nat β†’ Nat def depth : Term β†’ Nat | .Var _ => 0 | .Cons l r => 1 + depth l + depth r def act (f : Subst) (t : Term) := match t with | .Var i => Term.Var (f i) | .Cons l r => Term.Cons (act f l) (act f r) def strangers (u v : Term) := βˆ€ f : Subst, act f u β‰  act f v abbrev P (c : Option Subst) u v := match c with | none => strangers u v | some f => act f u = act f v def rel : WellFoundedRelation (Term Γ— Term) := measure (Ξ» (u, v) => depth u + depth v) theorem decr_left (l₁ r₁ lβ‚‚ rβ‚‚ : Term) : rel.rel (l₁, lβ‚‚) (Term.Cons l₁ r₁, Term.Cons lβ‚‚ rβ‚‚) := by suffices h : depth l₁ + depth lβ‚‚ < depth (Term.Cons l₁ r₁) + depth (Term.Cons lβ‚‚ rβ‚‚) from h admit theorem decr_right (l₁ r₁ lβ‚‚ rβ‚‚ : Term) (f : Subst) : rel.rel (act f r₁, act f rβ‚‚) (Term.Cons l₁ r₁, Term.Cons lβ‚‚ rβ‚‚) := by suffices h : depth (act f r₁) + depth (act f rβ‚‚) < depth (Term.Cons l₁ r₁) + depth (Term.Cons lβ‚‚ rβ‚‚) from h admit def robinson (u v : Term) : { f : Option Subst // P f u v } := match u, v with | .Cons l₁ r₁, .Cons lβ‚‚ rβ‚‚ => match robinson l₁ lβ‚‚ with | ⟨ none, h ⟩ => ⟨ none, sorry ⟩ | ⟨ some f, h ⟩ => match robinson (act f r₁) (act f rβ‚‚) with | ⟨ none, h ⟩ => ⟨ none, sorry ⟩ | ⟨ some g, h ⟩ => ⟨ some (g ∘ f), sorry ⟩ | .Var i, .Cons l r => ⟨ none, sorry ⟩ | .Cons l r, .Var i => ⟨ none, sorry ⟩ | .Var i, .Var j => if i = j then ⟨ some id, sorry ⟩ else ⟨ some Ξ» n => if n = i then j else n, sorry ⟩ termination_by' invImage (Ξ» ⟨ u, v ⟩ => (u, v)) rel decreasing_by first | apply decr_left _ _ _ _ | apply decr_right _ _ _ _ _ attribute [simp] robinson set_option pp.proofs true #check robinson._eq_1 #check robinson._eq_2 #check robinson._eq_3 #check robinson._eq_4 theorem ex : (robinson (Term.Var 0) (Term.Var 0)).1 = some id := by unfold robinson admit
37a7ef77e72722051e07c3e1bbf92ff7c87f5b88
37a833c924892ee3ecb911484775a6d6ebb8984d
/src/category_theory/presheaves/sheaves.lean
3b4afe1d7b88003984cf201747067d033db4cb41
[]
no_license
silky/lean-category-theory
28126e80564a1f99e9c322d86b3f7d750da0afa1
0f029a2364975f56ac727d31d867a18c95c22fd8
refs/heads/master
1,589,555,811,646
1,554,673,665,000
1,554,673,665,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,773
lean
import category_theory.opposites import category_theory.full_subcategory import category_theory.limits.types import category_theory.examples.topological_spaces import category_theory.limits.obviously open category_theory open category_theory.limits open category_theory.examples open topological_space universes u v u₁ v₁ uβ‚‚ vβ‚‚ variable (X : Top.{v}) local attribute [back] topological_space.is_open_inter -- local attribute [back] opens.property instance has_inter_open_set : has_inter (opens X) := { inter := Ξ» U V, ⟨ U.val ∩ V.val, by obviously ⟩ } instance has_inter_open_set_op : has_inter ((opens X)α΅’α΅–) := has_inter_open_set X -- def cover_intersections_index (I : Type v) : grothendieck_category (ParallelPair_functor (@prod.fst I I) (@prod.snd I I)) -- def cover_intersections (c : cover X) : (cover_intersections_index c.I) β₯€ open_set X := -- { obj := Ξ» p, match p.1 with -- | _1 := c.U p.2.1 ∩ c.U p.2.2 -- | _2 := c.U p.2 -- end, -- map := Ξ» p q f, sorry -- } -- @[tidy] meta def sbe := `[solve_by_elim [sum.inl, sum.inr, ulift.up, plift.up, trivial] {max_rep := 5}] -- instance (I : Type v) : category (I Γ— I βŠ• I) := -- { hom := Ξ» X Y, match (X, Y) with -- | (sum.inl (i, j), sum.inr k) := ulift (plift (i = k)) βŠ• ulift (plift (j = k)) -- | (sum.inl (i, j), sum.inl (i', j')) := ulift (plift (i = i' ∧ j = j')) -- | (sum.inr k, sum.inr k') := ulift (plift (k = k')) -- | (sum.inr k, sum.inl (i, j)) := pempty -- end, -- id := by tidy, -- comp := by tidy, -- } structure cover := (I : Type v) (U : I β†’ (opens X)) variables {X} def cover.union (c : cover X) : opens X := ⟨ set.Union (Ξ» i : c.I, (c.U i).1), begin apply topological_space.is_open_sUnion, tidy, subst H_h, exact (c.U H_w).2 end ⟩ def cover.sub (c : cover X) (i : c.I) : c.U i ⟢ c.union := sorry definition cover.left (c : cover X) (i j : c.I) : (c.U i ∩ c.U j) ⟢ (c.U i) := by obviously definition cover.right (c : cover X) (i j : c.I) : (c.U i ∩ c.U j) ⟢ (c.U j) := by obviously section variables {D : Type uβ‚‚} [π’Ÿ : category.{uβ‚‚ vβ‚‚} D] variables {c : cover X} (i j : c.I) (F : (opens X)α΅’α΅– β₯€ D) include π’Ÿ definition res_left : (F.obj (c.U i)) ⟢ (F.obj ((c.U i) ∩ (c.U j))) := F.map (c.left i j) definition res_right := F.map (c.right i j) definition res_union : (F.obj (c.union)) ⟢ (F.obj ((c.U i))) := F.map (c.sub i) @[simp] lemma res_left_right : res_union i F ≫ res_left i j F = res_union j F ≫ res_right i j F := begin dsimp [res_union, res_left, res_right], rw ← functor.map_comp, rw ← functor.map_comp, refl, end end section variables {V : Type u} [𝒱 : category.{u v} V] [has_products.{u v} V] include 𝒱 variables (c : cover X) (F : (opens X)α΅’α΅– β₯€ V) def sections : V := limits.pi.{u v} (Ξ» i : c.I, F.obj (c.U i)) def overlaps : V := limits.pi.{u v} (Ξ» p : c.I Γ— c.I, F.obj (c.U p.1 ∩ c.U p.2)) def left : (sections c F) ⟢ (overlaps c F) := pi.pre _ (Ξ» p : c.I Γ— c.I, p.1) ≫ pi.map (Ξ» p, res_left p.1 p.2 F) def right : (sections c F) ⟢ (overlaps c F) := pi.pre _ (Ξ» p : c.I Γ— c.I, p.2) ≫ pi.map (Ξ» p, res_right p.1 p.2 F) def res : F.obj (c.union) ⟢ (sections c F) := pi.lift (Ξ» i, res_union i F) @[simp] lemma res_left_right' : res c F ≫ left c F = res c F ≫ right c F := begin dsimp [left, right, res], rw ← category.assoc, simp, rw ← category.assoc, simp, end def cover_fork : fork (left c F) (right c F) := fork.of_ΞΉ (res c F) (by tidy) class is_sheaf (presheaf : (opens X)α΅’α΅– β₯€ V) := (sheaf_condition : Ξ  (c : cover X), is_equalizer (cover_fork c presheaf)) variables (X V) structure sheaf := (presheaf : (opens X)α΅’α΅– β₯€ V) (sheaf_condition : is_sheaf presheaf) end
00b5920ed1a25ea87eb1c1128f46b1ecee3db470
367134ba5a65885e863bdc4507601606690974c1
/src/ring_theory/integral_closure.lean
5d08130adab5bf65e674dc00a89948634b038b5a
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
25,900
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 ring_theory.adjoin.basic import ring_theory.polynomial.scale_roots import ring_theory.polynomial.tower /-! # Integral closure of a subring. If A is an R-algebra then `a : A` is integral over R if it is a root of a monic polynomial with coefficients in R. Enough theory is developed to prove that integral elements form a sub-R-algebra of A. ## Main definitions Let `R` be a `comm_ring` and let `A` be an R-algebra. * `ring_hom.is_integral_elem (f : R β†’+* A) (x : A)` : `x` is integral with respect to the map `f`, * `is_integral (x : A)` : `x` is integral over `R`, i.e., is a root of a monic polynomial with coefficients in `R`. * `integral_closure R A` : the integral closure of `R` in `A`, regarded as a sub-`R`-algebra of `A`. -/ open_locale classical open_locale big_operators open polynomial submodule section ring variables {R S A : Type*} variables [comm_ring R] [ring A] [ring S] (f : R β†’+* S) /-- An element `x` of `A` is said to be integral over `R` with respect to `f` if it is a root of a monic polynomial `p : polynomial R` evaluated under `f` -/ def ring_hom.is_integral_elem (f : R β†’+* A) (x : A) := βˆƒ p : polynomial R, monic p ∧ evalβ‚‚ f x p = 0 /-- A ring homomorphism `f : R β†’+* A` is said to be integral if every element `A` is integral with respect to the map `f` -/ def ring_hom.is_integral (f : R β†’+* A) := βˆ€ x : A, f.is_integral_elem x variables [algebra R A] (R) /-- An element `x` of an algebra `A` over a commutative ring `R` is said to be *integral*, if it is a root of some monic polynomial `p : polynomial R`. Equivalently, the element is integral over `R` with respect to the induced `algebra_map` -/ def is_integral (x : A) : Prop := (algebra_map R A).is_integral_elem x variable (A) /-- An algebra is integral if every element of the extension is integral over the base ring -/ def algebra.is_integral : Prop := (algebra_map R A).is_integral variables {R A} lemma ring_hom.is_integral_map {x : R} : f.is_integral_elem (f x) := ⟨X - C x, monic_X_sub_C _, by simp⟩ theorem is_integral_algebra_map {x : R} : is_integral R (algebra_map R A x) := (algebra_map R A).is_integral_map theorem is_integral_of_noetherian (H : is_noetherian R A) (x : A) : is_integral R x := begin let leval : @linear_map R (polynomial R) A _ _ _ _ _ := (aeval x).to_linear_map, let D : β„• β†’ submodule R A := Ξ» n, (degree_le R n).map leval, let M := well_founded.min (is_noetherian_iff_well_founded.1 H) (set.range D) ⟨_, ⟨0, rfl⟩⟩, have HM : M ∈ set.range D := well_founded.min_mem _ _ _, cases HM with N HN, have HM : Β¬M < D (N+1) := well_founded.not_lt_min (is_noetherian_iff_well_founded.1 H) (set.range D) _ ⟨N+1, rfl⟩, rw ← HN at HM, have HN2 : D (N+1) ≀ D N := classical.by_contradiction (Ξ» H, HM (lt_of_le_not_le (map_mono (degree_le_mono (with_bot.coe_le_coe.2 (nat.le_succ N)))) H)), have HN3 : leval (X^(N+1)) ∈ D N, { exact HN2 (mem_map_of_mem (mem_degree_le.2 (degree_X_pow_le _))) }, rcases HN3 with ⟨p, hdp, hpe⟩, refine ⟨X^(N+1) - p, monic_X_pow_sub (mem_degree_le.1 hdp), _⟩, show leval (X ^ (N + 1) - p) = 0, rw [linear_map.map_sub, hpe, sub_self] end theorem is_integral_of_submodule_noetherian (S : subalgebra R A) (H : is_noetherian R (S : submodule R A)) (x : A) (hx : x ∈ S) : is_integral R x := begin suffices : is_integral R (show S, from ⟨x, hx⟩), { rcases this with ⟨p, hpm, hpx⟩, replace hpx := congr_arg S.val hpx, refine ⟨p, hpm, eq.trans _ hpx⟩, simp only [aeval_def, evalβ‚‚, finsupp.sum], rw S.val.map_sum, refine finset.sum_congr rfl (Ξ» n hn, _), rw [S.val.map_mul, S.val.map_pow, S.val.commutes, S.val_apply, subtype.coe_mk], }, refine is_integral_of_noetherian H ⟨x, hx⟩ end end ring section variables {R A B S : Type*} variables [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring S] variables [algebra R A] [algebra R B] (f : R β†’+* S) theorem is_integral_alg_hom (f : A →ₐ[R] B) {x : A} (hx : is_integral R x) : is_integral R (f x) := let ⟨p, hp, hpx⟩ := hx in ⟨p, hp, by rw [← aeval_def, aeval_alg_hom_apply, aeval_def, hpx, f.map_zero]⟩ theorem is_integral_of_is_scalar_tower [algebra A B] [is_scalar_tower R A B] (x : B) (hx : is_integral R x) : is_integral A x := let ⟨p, hp, hpx⟩ := hx in ⟨p.map $ algebra_map R A, monic_map _ hp, by rw [← aeval_def, ← is_scalar_tower.aeval_apply, aeval_def, hpx]⟩ section local attribute [instance] subset.comm_ring algebra.of_is_subring theorem is_integral_of_subring {x : A} (T : set R) [is_subring T] (hx : is_integral T x) : is_integral R x := is_integral_of_is_scalar_tower x hx lemma is_integral_algebra_map_iff [algebra A B] [is_scalar_tower R A B] {x : A} (hAB : function.injective (algebra_map A B)) : is_integral R (algebra_map A B x) ↔ is_integral R x := begin split; rintros ⟨f, hf, hx⟩; use [f, hf], { exact is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero R A B hAB hx }, { rw [is_scalar_tower.algebra_map_eq R A B, ← hom_evalβ‚‚, hx, ring_hom.map_zero] } end theorem is_integral_iff_is_integral_closure_finite {r : A} : is_integral R r ↔ βˆƒ s : set R, s.finite ∧ is_integral (ring.closure s) r := begin split; intro hr, { rcases hr with ⟨p, hmp, hpr⟩, refine ⟨_, set.finite_mem_finset _, p.restriction, subtype.eq hmp, _⟩, erw [← aeval_def, is_scalar_tower.aeval_apply _ R, map_restriction, aeval_def, hpr] }, rcases hr with ⟨s, hs, hsr⟩, exact is_integral_of_subring _ hsr end end theorem fg_adjoin_singleton_of_integral (x : A) (hx : is_integral R x) : (algebra.adjoin R ({x} : set A) : submodule R A).fg := begin rcases hx with ⟨f, hfm, hfx⟩, existsi finset.image ((^) x) (finset.range (nat_degree f + 1)), apply le_antisymm, { rw span_le, intros s hs, rw finset.mem_coe at hs, rcases finset.mem_image.1 hs with ⟨k, hk, rfl⟩, clear hk, exact is_submonoid.pow_mem (algebra.subset_adjoin (set.mem_singleton _)) }, intros r hr, change r ∈ algebra.adjoin R ({x} : set A) at hr, rw algebra.adjoin_singleton_eq_range at hr, rcases (aeval x).mem_range.mp hr with ⟨p, rfl⟩, rw ← mod_by_monic_add_div p hfm, rw ← aeval_def at hfx, rw [alg_hom.map_add, alg_hom.map_mul, hfx, zero_mul, add_zero], have : degree (p %β‚˜ f) ≀ degree f := degree_mod_by_monic_le p hfm, generalize_hyp : p %β‚˜ f = q at this ⊒, rw [← sum_C_mul_X_eq q, aeval_def, evalβ‚‚_sum, finsupp.sum], refine sum_mem _ (Ξ» k hkq, _), rw [evalβ‚‚_mul, evalβ‚‚_C, evalβ‚‚_pow, evalβ‚‚_X, ← algebra.smul_def], refine smul_mem _ _ (subset_span _), rw finset.mem_coe, refine finset.mem_image.2 ⟨_, _, rfl⟩, rw [finset.mem_range, nat.lt_succ_iff], refine le_of_not_lt (Ξ» hk, _), rw [degree_le_iff_coeff_zero] at this, rw [finsupp.mem_support_iff] at hkq, apply hkq, apply this, exact lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 hk) end theorem fg_adjoin_of_finite {s : set A} (hfs : s.finite) (his : βˆ€ x ∈ s, is_integral R x) : (algebra.adjoin R s : submodule R A).fg := set.finite.induction_on hfs (Ξ» _, ⟨{1}, submodule.ext $ Ξ» x, by { erw [algebra.adjoin_empty, finset.coe_singleton, ← one_eq_span, one_eq_map_top, map_top, linear_map.mem_range, algebra.mem_bot], refl }⟩) (Ξ» a s has hs ih his, by rw [← set.union_singleton, algebra.adjoin_union_coe_submodule]; exact fg_mul _ _ (ih $ Ξ» i hi, his i $ set.mem_insert_of_mem a hi) (fg_adjoin_singleton_of_integral _ $ his a $ set.mem_insert a s)) his theorem is_integral_of_mem_of_fg (S : subalgebra R A) (HS : (S : submodule R A).fg) (x : A) (hx : x ∈ S) : is_integral R x := begin cases HS with y hy, obtain ⟨lx, hlx1, hlx2⟩ : βˆƒ (l : A β†’β‚€ R) (H : l ∈ finsupp.supported R R ↑y), (finsupp.total A A R id) l = x, { rwa [←(@finsupp.mem_span_iff_total A A R _ _ _ id ↑y x), set.image_id ↑y, hy] }, have hyS : βˆ€ {p}, p ∈ y β†’ p ∈ S := Ξ» p hp, show p ∈ (S : submodule R A), by { rw ← hy, exact subset_span hp }, have : βˆ€ (jk : (↑(y.product y) : set (A Γ— A))), jk.1.1 * jk.1.2 ∈ (S : submodule R A) := Ξ» jk, S.mul_mem (hyS (finset.mem_product.1 jk.2).1) (hyS (finset.mem_product.1 jk.2).2), rw [← hy, ← set.image_id ↑y] at this, simp only [finsupp.mem_span_iff_total] at this, choose ly hly1 hly2, let Sβ‚€ : set R := ring.closure ↑(lx.frange βˆͺ finset.bUnion finset.univ (finsupp.frange ∘ ly)), refine is_integral_of_subring Sβ‚€ _, letI : comm_ring Sβ‚€ := @subtype.comm_ring _ _ _ ring.closure.is_subring, letI : algebra Sβ‚€ A := algebra.of_is_subring _, have : span Sβ‚€ (insert 1 ↑y : set A) * span Sβ‚€ (insert 1 ↑y : set A) ≀ span Sβ‚€ (insert 1 ↑y : set A), { rw span_mul_span, refine span_le.2 (Ξ» z hz, _), rcases set.mem_mul.1 hz with ⟨p, q, rfl | hp, hq, rfl⟩, { rw one_mul, exact subset_span hq }, rcases hq with rfl | hq, { rw mul_one, exact subset_span (or.inr hp) }, erw ← hly2 ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩, rw [finsupp.total_apply, finsupp.sum], refine (span Sβ‚€ (insert 1 ↑y : set A)).sum_mem (Ξ» t ht, _), have : ly ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩ t ∈ Sβ‚€ := ring.subset_closure (finset.mem_union_right _ $ finset.mem_bUnion.2 ⟨⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩, finset.mem_univ _, finsupp.mem_frange.2 ⟨finsupp.mem_support_iff.1 ht, _, rfl⟩⟩), change (⟨_, this⟩ : Sβ‚€) β€’ t ∈ _, exact smul_mem _ _ (subset_span $ or.inr $ hly1 _ ht) }, haveI : is_subring (span Sβ‚€ (insert 1 ↑y : set A) : set A) := { one_mem := subset_span $ or.inl rfl, mul_mem := Ξ» p q hp hq, this $ mul_mem_mul hp hq, zero_mem := (span Sβ‚€ (insert 1 ↑y : set A)).zero_mem, add_mem := Ξ» _ _, (span Sβ‚€ (insert 1 ↑y : set A)).add_mem, neg_mem := Ξ» _, (span Sβ‚€ (insert 1 ↑y : set A)).neg_mem }, have : span Sβ‚€ (insert 1 ↑y : set A) = algebra.adjoin Sβ‚€ (↑y : set A), { refine le_antisymm (span_le.2 $ set.insert_subset.2 ⟨(algebra.adjoin Sβ‚€ ↑y).one_mem, algebra.subset_adjoin⟩) (Ξ» z hz, _), rw [subalgebra.mem_to_submodule, algebra.mem_adjoin_iff] at hz, rw ← submodule.mem_coe, refine ring.closure_subset (set.union_subset (set.range_subset_iff.2 $ Ξ» t, _) (Ξ» t ht, subset_span $ or.inr ht)) hz, rw algebra.algebra_map_eq_smul_one, exact smul_mem (span Sβ‚€ (insert 1 ↑y : set A)) _ (subset_span $ or.inl rfl) }, haveI : is_noetherian_ring β†₯Sβ‚€ := is_noetherian_ring_closure _ (finset.finite_to_set _), refine is_integral_of_submodule_noetherian (algebra.adjoin Sβ‚€ ↑y) (is_noetherian_of_fg_of_noetherian _ ⟨insert 1 y, by rw [finset.coe_insert, this]⟩) _ _, rw [← hlx2, finsupp.total_apply, finsupp.sum], refine subalgebra.sum_mem _ (Ξ» r hr, _), have : lx r ∈ Sβ‚€ := ring.subset_closure (finset.mem_union_left _ (finset.mem_image_of_mem _ hr)), change (⟨_, this⟩ : Sβ‚€) β€’ r ∈ _, rw finsupp.mem_supported at hlx1, exact subalgebra.smul_mem _ (algebra.subset_adjoin $ hlx1 hr) _ end lemma ring_hom.is_integral_of_mem_closure {x y z : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) (hz : z ∈ ring.closure ({x, y} : set S)) : f.is_integral_elem z := begin letI : algebra R S := f.to_algebra, have := fg_mul _ _ (fg_adjoin_singleton_of_integral x hx) (fg_adjoin_singleton_of_integral y hy), rw [← algebra.adjoin_union_coe_submodule, set.singleton_union] at this, exact is_integral_of_mem_of_fg (algebra.adjoin R {x, y}) this z (algebra.mem_adjoin_iff.2 $ ring.closure_mono (set.subset_union_right _ _) hz), end theorem is_integral_of_mem_closure {x y z : A} (hx : is_integral R x) (hy : is_integral R y) (hz : z ∈ ring.closure ({x, y} : set A)) : is_integral R z := (algebra_map R A).is_integral_of_mem_closure hx hy hz lemma ring_hom.is_integral_zero : f.is_integral_elem 0 := f.map_zero β–Έ f.is_integral_map theorem is_integral_zero : is_integral R (0:A) := (algebra_map R A).is_integral_zero lemma ring_hom.is_integral_one : f.is_integral_elem 1 := f.map_one β–Έ f.is_integral_map theorem is_integral_one : is_integral R (1:A) := (algebra_map R A).is_integral_one lemma ring_hom.is_integral_add {x y : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x + y) := f.is_integral_of_mem_closure hx hy (is_add_submonoid.add_mem (ring.subset_closure (or.inl rfl)) (ring.subset_closure (or.inr rfl))) theorem is_integral_add {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x + y) := (algebra_map R A).is_integral_add hx hy lemma ring_hom.is_integral_neg {x : S} (hx : f.is_integral_elem x) : f.is_integral_elem (-x) := f.is_integral_of_mem_closure hx hx (is_add_subgroup.neg_mem (ring.subset_closure (or.inl rfl))) theorem is_integral_neg {x : A} (hx : is_integral R x) : is_integral R (-x) := (algebra_map R A).is_integral_neg hx lemma ring_hom.is_integral_sub {x y : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x - y) := by simpa only [sub_eq_add_neg] using f.is_integral_add hx (f.is_integral_neg hy) theorem is_integral_sub {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x - y) := (algebra_map R A).is_integral_sub hx hy lemma ring_hom.is_integral_mul {x y : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x * y) := f.is_integral_of_mem_closure hx hy (is_submonoid.mul_mem (ring.subset_closure (or.inl rfl)) (ring.subset_closure (or.inr rfl))) theorem is_integral_mul {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x * y) := (algebra_map R A).is_integral_mul hx hy variables (R A) /-- The integral closure of R in an R-algebra A. -/ def integral_closure : subalgebra R A := { carrier := { r | is_integral R r }, zero_mem' := is_integral_zero, one_mem' := is_integral_one, add_mem' := Ξ» _ _, is_integral_add, mul_mem' := Ξ» _ _, is_integral_mul, algebra_map_mem' := Ξ» x, is_integral_algebra_map } theorem mem_integral_closure_iff_mem_fg {r : A} : r ∈ integral_closure R A ↔ βˆƒ M : subalgebra R A, (M : submodule R A).fg ∧ r ∈ M := ⟨λ hr, ⟨algebra.adjoin R {r}, fg_adjoin_singleton_of_integral _ hr, algebra.subset_adjoin rfl⟩, Ξ» ⟨M, Hf, hrM⟩, is_integral_of_mem_of_fg M Hf _ hrM⟩ variables {R} {A} /-- Mapping an integral closure along an `alg_equiv` gives the integral closure. -/ lemma integral_closure_map_alg_equiv (f : A ≃ₐ[R] B) : (integral_closure R A).map (f : A →ₐ[R] B) = integral_closure R B := begin ext y, rw subalgebra.mem_map, split, { rintros ⟨x, hx, rfl⟩, exact is_integral_alg_hom f hx }, { intro hy, use [f.symm y, is_integral_alg_hom (f.symm : B →ₐ[R] A) hy], simp } end lemma integral_closure.is_integral (x : integral_closure R A) : is_integral R x := let ⟨p, hpm, hpx⟩ := x.2 in ⟨p, hpm, subtype.eq $ by rwa [← aeval_def, subtype.val_eq_coe, ← subalgebra.val_apply, aeval_alg_hom_apply] at hpx⟩ lemma ring_hom.is_integral_of_is_integral_mul_unit (x y : S) (r : R) (hr : f r * y = 1) (hx : f.is_integral_elem (x * y)) : f.is_integral_elem x := begin obtain ⟨p, ⟨p_monic, hp⟩⟩ := hx, refine ⟨scale_roots p r, ⟨(monic_scale_roots_iff r).2 p_monic, _⟩⟩, convert scale_roots_evalβ‚‚_eq_zero f hp, rw [mul_comm x y, ← mul_assoc, hr, one_mul], end theorem is_integral_of_is_integral_mul_unit {x y : A} {r : R} (hr : algebra_map R A r * y = 1) (hx : is_integral R (x * y)) : is_integral R x := (algebra_map R A).is_integral_of_is_integral_mul_unit x y r hr hx /-- Generalization of `is_integral_of_mem_closure` bootstrapped up from that lemma -/ lemma is_integral_of_mem_closure' (G : set A) (hG : βˆ€ x ∈ G, is_integral R x) : βˆ€ x ∈ (subring.closure G), is_integral R x := Ξ» x hx, subring.closure_induction hx hG is_integral_zero is_integral_one (Ξ» _ _, is_integral_add) (Ξ» _, is_integral_neg) (Ξ» _ _, is_integral_mul) lemma is_integral_of_mem_closure'' {S : Type*} [comm_ring S] {f : R β†’+* S} (G : set S) (hG : βˆ€ x ∈ G, f.is_integral_elem x) : βˆ€ x ∈ (subring.closure G), f.is_integral_elem x := Ξ» x hx, @is_integral_of_mem_closure' R S _ _ f.to_algebra G hG x hx end section algebra open algebra variables {R A B S T : Type*} variables [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring S] [comm_ring T] variables [algebra A B] [algebra R B] (f : R β†’+* S) (g : S β†’+* T) lemma is_integral_trans_aux (x : B) {p : polynomial A} (pmonic : monic p) (hp : aeval x p = 0) : is_integral (adjoin R (↑(p.map $ algebra_map A B).frange : set B)) x := begin generalize hS : (↑(p.map $ algebra_map A B).frange : set B) = S, have coeffs_mem : βˆ€ i, (p.map $ algebra_map A B).coeff i ∈ adjoin R S, { intro i, by_cases hi : (p.map $ algebra_map A B).coeff i = 0, { rw hi, exact subalgebra.zero_mem _ }, rw ← hS, exact subset_adjoin (finsupp.mem_frange.2 ⟨hi, i, rfl⟩) }, obtain ⟨q, hq⟩ : βˆƒ q : polynomial (adjoin R S), q.map (algebra_map (adjoin R S) B) = (p.map $ algebra_map A B), { rw ← set.mem_range, exact (polynomial.mem_map_range _).2 (Ξ» i, ⟨⟨_, coeffs_mem i⟩, rfl⟩) }, use q, split, { suffices h : (q.map (algebra_map (adjoin R S) B)).monic, { refine monic_of_injective _ h, exact subtype.val_injective }, { rw hq, exact monic_map _ pmonic } }, { convert hp using 1, replace hq := congr_arg (eval x) hq, convert hq using 1; symmetry; apply eval_map }, end variables [algebra R A] [is_scalar_tower R A B] /-- If A is an R-algebra all of whose elements are integral over R, and x is an element of an A-algebra that is integral over A, then x is integral over R.-/ lemma is_integral_trans (A_int : is_integral R A) (x : B) (hx : is_integral A x) : is_integral R x := begin rcases hx with ⟨p, pmonic, hp⟩, let S : set B := ↑(p.map $ algebra_map A B).frange, refine is_integral_of_mem_of_fg (adjoin R (S βˆͺ {x})) _ _ (subset_adjoin $ or.inr rfl), refine fg_trans (fg_adjoin_of_finite (finset.finite_to_set _) (Ξ» x hx, _)) _, { rw [finset.mem_coe, finsupp.mem_frange] at hx, rcases hx with ⟨_, i, rfl⟩, show is_integral R ((p.map $ algebra_map A B).coeff i), rw coeff_map, convert is_integral_alg_hom (is_scalar_tower.to_alg_hom R A B) (A_int _) }, { apply fg_adjoin_singleton_of_integral, exact is_integral_trans_aux _ pmonic hp } end /-- If A is an R-algebra all of whose elements are integral over R, and B is an A-algebra all of whose elements are integral over A, then all elements of B are integral over R.-/ lemma algebra.is_integral_trans (hA : is_integral R A) (hB : is_integral A B) : is_integral R B := Ξ» x, is_integral_trans hA x (hB x) lemma ring_hom.is_integral_trans (hf : f.is_integral) (hg : g.is_integral) : (g.comp f).is_integral := @algebra.is_integral_trans R S T _ _ _ g.to_algebra (g.comp f).to_algebra f.to_algebra (@is_scalar_tower.of_algebra_map_eq R S T _ _ _ f.to_algebra g.to_algebra (g.comp f).to_algebra (ring_hom.comp_apply g f)) hf hg lemma ring_hom.is_integral_of_surjective (hf : function.surjective f) : f.is_integral := Ξ» x, (hf x).rec_on (Ξ» y hy, (hy β–Έ f.is_integral_map : f.is_integral_elem x)) lemma is_integral_of_surjective (h : function.surjective (algebra_map R A)) : is_integral R A := (algebra_map R A).is_integral_of_surjective h /-- If `R β†’ A β†’ B` is an algebra tower with `A β†’ B` injective, then if the entire tower is an integral extension so is `R β†’ A` -/ lemma is_integral_tower_bot_of_is_integral (H : function.injective (algebra_map A B)) {x : A} (h : is_integral R (algebra_map A B x)) : is_integral R x := begin rcases h with ⟨p, ⟨hp, hp'⟩⟩, refine ⟨p, ⟨hp, _⟩⟩, rw [is_scalar_tower.algebra_map_eq R A B, ← evalβ‚‚_map, evalβ‚‚_hom, ← ring_hom.map_zero (algebra_map A B)] at hp', rw [evalβ‚‚_eq_eval_map], exact H hp', end lemma ring_hom.is_integral_tower_bot_of_is_integral (hg : function.injective g) (hfg : (g.comp f).is_integral) : f.is_integral := Ξ» x, @is_integral_tower_bot_of_is_integral R S T _ _ _ g.to_algebra (g.comp f).to_algebra f.to_algebra (@is_scalar_tower.of_algebra_map_eq R S T _ _ _ f.to_algebra g.to_algebra (g.comp f).to_algebra (ring_hom.comp_apply g f)) hg x (hfg (g x)) lemma is_integral_tower_bot_of_is_integral_field {R A B : Type*} [comm_ring R] [field A] [comm_ring B] [nontrivial B] [algebra R A] [algebra A B] [algebra R B] [is_scalar_tower R A B] {x : A} (h : is_integral R (algebra_map A B x)) : is_integral R x := is_integral_tower_bot_of_is_integral (algebra_map A B).injective h lemma ring_hom.is_integral_elem_of_is_integral_elem_comp {x : T} (h : (g.comp f).is_integral_elem x) : g.is_integral_elem x := let ⟨p, ⟨hp, hp'⟩⟩ := h in ⟨p.map f, monic_map f hp, by rwa ← evalβ‚‚_map at hp'⟩ lemma ring_hom.is_integral_tower_top_of_is_integral (h : (g.comp f).is_integral) : g.is_integral := Ξ» x, ring_hom.is_integral_elem_of_is_integral_elem_comp f g (h x) /-- If `R β†’ A β†’ B` is an algebra tower, then if the entire tower is an integral extension so is `A β†’ B`. -/ lemma is_integral_tower_top_of_is_integral {x : B} (h : is_integral R x) : is_integral A x := begin rcases h with ⟨p, ⟨hp, hp'⟩⟩, refine ⟨p.map (algebra_map R A), ⟨monic_map (algebra_map R A) hp, _⟩⟩, rw [is_scalar_tower.algebra_map_eq R A B, ← evalβ‚‚_map] at hp', exact hp', end lemma ring_hom.is_integral_quotient_of_is_integral {I : ideal S} (hf : f.is_integral) : (ideal.quotient_map I f le_rfl).is_integral := begin rintros ⟨x⟩, obtain ⟨p, ⟨p_monic, hpx⟩⟩ := hf x, refine ⟨p.map (ideal.quotient.mk _), ⟨monic_map _ p_monic, _⟩⟩, simpa only [hom_evalβ‚‚, evalβ‚‚_map] using congr_arg (ideal.quotient.mk I) hpx end lemma is_integral_quotient_of_is_integral {I : ideal A} (hRA : is_integral R A) : is_integral (I.comap (algebra_map R A)).quotient I.quotient := (algebra_map R A).is_integral_quotient_of_is_integral hRA lemma is_integral_quotient_map_iff {I : ideal S} : (ideal.quotient_map I f le_rfl).is_integral ↔ ((ideal.quotient.mk I).comp f : R β†’+* I.quotient).is_integral := begin let g := ideal.quotient.mk (I.comap f), have := ideal.quotient_map_comp_mk le_rfl, refine ⟨λ h, _, Ξ» h, ring_hom.is_integral_tower_top_of_is_integral g _ (this β–Έ h)⟩, refine this β–Έ ring_hom.is_integral_trans g (ideal.quotient_map I f le_rfl) _ h, exact ring_hom.is_integral_of_surjective g ideal.quotient.mk_surjective, end /-- If the integral extension `R β†’ S` is injective, and `S` is a field, then `R` is also a field. -/ lemma is_field_of_is_integral_of_is_field {R S : Type*} [integral_domain R] [integral_domain S] [algebra R S] (H : is_integral R S) (hRS : function.injective (algebra_map R S)) (hS : is_field S) : is_field R := begin refine ⟨⟨0, 1, zero_ne_one⟩, mul_comm, Ξ» a ha, _⟩, -- Let `a_inv` be the inverse of `algebra_map R S a`, -- then we need to show that `a_inv` is of the form `algebra_map R S b`. obtain ⟨a_inv, ha_inv⟩ := hS.mul_inv_cancel (Ξ» h, ha (hRS (trans h (ring_hom.map_zero _).symm))), -- Let `p : polynomial R` be monic with root `a_inv`, -- and `q` be `p` with coefficients reversed (so `q(a) = q'(a) * a + 1`). -- We claim that `q(a) = 0`, so `-q'(a)` is the inverse of `a`. obtain ⟨p, p_monic, hp⟩ := H a_inv, use -βˆ‘ (i : β„•) in finset.range p.nat_degree, (p.coeff i) * a ^ (p.nat_degree - i - 1), -- `q(a) = 0`, because multiplying everything with `a_inv^n` gives `p(a_inv) = 0`. -- TODO: this could be a lemma for `polynomial.reverse`. have hq : βˆ‘ (i : β„•) in finset.range (p.nat_degree + 1), (p.coeff i) * a ^ (p.nat_degree - i) = 0, { apply (algebra_map R S).injective_iff.mp hRS, have a_inv_ne_zero : a_inv β‰  0 := right_ne_zero_of_mul (mt ha_inv.symm.trans one_ne_zero), refine (mul_eq_zero.mp _).resolve_right (pow_ne_zero p.nat_degree a_inv_ne_zero), rw [evalβ‚‚_eq_sum_range] at hp, rw [ring_hom.map_sum, finset.sum_mul], refine (finset.sum_congr rfl (Ξ» i hi, _)).trans hp, rw [ring_hom.map_mul, mul_assoc], congr, have : a_inv ^ p.nat_degree = a_inv ^ (p.nat_degree - i) * a_inv ^ i, { rw [← pow_add a_inv, nat.sub_add_cancel (nat.le_of_lt_succ (finset.mem_range.mp hi))] }, rw [ring_hom.map_pow, this, ← mul_assoc, ← mul_pow, ha_inv, one_pow, one_mul] }, -- Since `q(a) = 0` and `q(a) = q'(a) * a + 1`, we have `a * -q'(a) = 1`. -- TODO: we could use a lemma for `polynomial.div_X` here. rw [finset.sum_range_succ, p_monic.coeff_nat_degree, one_mul, nat.sub_self, pow_zero, add_eq_zero_iff_eq_neg, eq_comm] at hq, rw [mul_comm, ← neg_mul_eq_neg_mul, finset.sum_mul], convert hq using 2, refine finset.sum_congr rfl (Ξ» i hi, _), have : 1 ≀ p.nat_degree - i := nat.le_sub_left_of_add_le (finset.mem_range.mp hi), rw [mul_assoc, ← pow_succ', nat.sub_add_cancel this] end end algebra section local attribute [instance] subset.comm_ring algebra.of_is_subring theorem integral_closure_idem {R : Type*} {A : Type*} [comm_ring R] [comm_ring A] [algebra R A] : integral_closure (integral_closure R A : set A) A = βŠ₯ := eq_bot_iff.2 $ Ξ» x hx, algebra.mem_bot.2 ⟨⟨x, @is_integral_trans _ _ _ _ _ _ _ _ (integral_closure R A).algebra _ integral_closure.is_integral x hx⟩, rfl⟩ end section integral_domain variables {R S : Type*} [comm_ring R] [integral_domain S] [algebra R S] instance : integral_domain (integral_closure R S) := infer_instance end integral_domain
83709345878c8339edfdb0ce5bc95032fbe55714
d5bef83c55d40cb88f9a01b755c882a93348a847
/library/init/algebra/classes.lean
b829e618bef84b131363630eeacc7a30116e9959
[ "Apache-2.0" ]
permissive
urkud/lean
587d78216e1f0c7f651566e9e92cf8ade285d58d
3526539070ea6268df5dd373deeb3ac8b9621952
refs/heads/master
1,660,171,634,921
1,657,873,466,000
1,657,873,466,000
249,789,456
0
0
Apache-2.0
1,585,075,263,000
1,585,075,263,000
null
UTF-8
Lean
false
false
13,788
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 -/ prelude import init.logic init.data.ordering.basic universes u v /-! # Unbundled algebra classes These classes and the `@[algebra]` attribute are part of an incomplete refactor described [here on the github Wiki](https://github.com/leanprover/lean/wiki/Refactoring-structures#encoding-the-algebraic-hierarchy-1). By themselves, these classes are not good replacements for the `monoid` / `group` etc structures provided by mathlib, as they are not discoverable by `simp` unlike the current lemmas due to there being little to index on. The Wiki page linked above describes an algebraic normalizer, but it is not implemented. -/ @[algebra] class is_symm_op (Ξ± : Type u) (Ξ² : out_param (Type v)) (op : Ξ± β†’ Ξ± β†’ Ξ²) : Prop := (symm_op : βˆ€ a b, op a b = op b a) @[algebra] class is_commutative (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) : Prop := (comm : βˆ€ a b, op a b = op b a) @[priority 100] instance is_symm_op_of_is_commutative (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) [is_commutative Ξ± op] : is_symm_op Ξ± Ξ± op := {symm_op := is_commutative.comm} @[algebra] class is_associative (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) : Prop := (assoc : βˆ€ a b c, op (op a b) c = op a (op b c)) @[algebra] class is_left_id (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) (o : out_param Ξ±) : Prop := (left_id : βˆ€ a, op o a = a) @[algebra] class is_right_id (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) (o : out_param Ξ±) : Prop := (right_id : βˆ€ a, op a o = a) @[algebra] class is_left_null (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) (o : out_param Ξ±) : Prop := (left_null : βˆ€ a, op o a = o) @[algebra] class is_right_null (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) (o : out_param Ξ±) : Prop := (right_null : βˆ€ a, op a o = o) @[algebra] class is_left_cancel (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) : Prop := (left_cancel : βˆ€ a b c, op a b = op a c β†’ b = c) @[algebra] class is_right_cancel (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) : Prop := (right_cancel : βˆ€ a b c, op a b = op c b β†’ a = c) @[algebra] class is_idempotent (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) : Prop := (idempotent : βˆ€ a, op a a = a) @[algebra] class is_left_distrib (Ξ± : Type u) (op₁ : Ξ± β†’ Ξ± β†’ Ξ±) (opβ‚‚ : out_param $ Ξ± β†’ Ξ± β†’ Ξ±) : Prop := (left_distrib : βˆ€ a b c, op₁ a (opβ‚‚ b c) = opβ‚‚ (op₁ a b) (op₁ a c)) @[algebra] class is_right_distrib (Ξ± : Type u) (op₁ : Ξ± β†’ Ξ± β†’ Ξ±) (opβ‚‚ : out_param $ Ξ± β†’ Ξ± β†’ Ξ±) : Prop := (right_distrib : βˆ€ a b c, op₁ (opβ‚‚ a b) c = opβ‚‚ (op₁ a c) (op₁ b c)) @[algebra] class is_left_inv (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) (inv : out_param $ Ξ± β†’ Ξ±) (o : out_param Ξ±) : Prop := (left_inv : βˆ€ a, op (inv a) a = o) @[algebra] class is_right_inv (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) (inv : out_param $ Ξ± β†’ Ξ±) (o : out_param Ξ±) : Prop := (right_inv : βˆ€ a, op a (inv a) = o) @[algebra] class is_cond_left_inv (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) (inv : out_param $ Ξ± β†’ Ξ±) (o : out_param Ξ±) (p : out_param $ Ξ± β†’ Prop) : Prop := (left_inv : βˆ€ a, p a β†’ op (inv a) a = o) @[algebra] class is_cond_right_inv (Ξ± : Type u) (op : Ξ± β†’ Ξ± β†’ Ξ±) (inv : out_param $ Ξ± β†’ Ξ±) (o : out_param Ξ±) (p : out_param $ Ξ± β†’ Prop) : Prop := (right_inv : βˆ€ a, p a β†’ op a (inv a) = o) @[algebra] class is_distinct (Ξ± : Type u) (a : Ξ±) (b : Ξ±) : Prop := (distinct : a β‰  b) /- -- The following type class doesn't seem very useful, a regular simp lemma should work for this. class is_inv (Ξ± : Type u) (Ξ² : Type v) (f : Ξ± β†’ Ξ²) (g : out Ξ² β†’ Ξ±) : Prop := (inv : βˆ€ a, g (f a) = a) -- The following one can also be handled using a regular simp lemma class is_idempotent (Ξ± : Type u) (f : Ξ± β†’ Ξ±) : Prop := (idempotent : βˆ€ a, f (f a) = f a) -/ /-- `is_irrefl X r` means the binary relation `r` on `X` is irreflexive (that is, `r x x` never holds). -/ @[algebra] class is_irrefl (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) : Prop := (irrefl : βˆ€ a, Β¬ r a a) /-- `is_refl X r` means the binary relation `r` on `X` is reflexive. -/ @[algebra] class is_refl (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) : Prop := (refl : βˆ€ a, r a a) /-- `is_symm X r` means the binary relation `r` on `X` is symmetric. -/ @[algebra] class is_symm (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) : Prop := (symm : βˆ€ a b, r a b β†’ r b a) /-- The opposite of a symmetric relation is symmetric. -/ @[priority 100] instance is_symm_op_of_is_symm (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) [is_symm Ξ± r] : is_symm_op Ξ± Prop r := {symm_op := Ξ» a b, propext $ iff.intro (is_symm.symm a b) (is_symm.symm b a)} /-- `is_asymm X r` means that the binary relation `r` on `X` is asymmetric, that is, `r a b β†’ Β¬ r b a`. -/ @[algebra] class is_asymm (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) : Prop := (asymm : βˆ€ a b, r a b β†’ Β¬ r b a) /-- `is_antisymm X r` means the binary relation `r` on `X` is antisymmetric. -/ @[algebra] class is_antisymm (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) : Prop := (antisymm : βˆ€ a b, r a b β†’ r b a β†’ a = b) /-- `is_trans X r` means the binary relation `r` on `X` is transitive. -/ @[algebra] class is_trans (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) : Prop := (trans : βˆ€ a b c, r a b β†’ r b c β†’ r a c) /-- `is_total X r` means that the binary relation `r` on `X` is total, that is, that for any `x y : X` we have `r x y` or `r y x`.-/ @[algebra] class is_total (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) : Prop := (total : βˆ€ a b, r a b ∨ r b a) /-- `is_preorder X r` means that the binary relation `r` on `X` is a pre-order, that is, reflexive and transitive. -/ @[algebra] class is_preorder (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) extends is_refl Ξ± r, is_trans Ξ± r : Prop. /-- `is_total_preorder X r` means that the binary relation `r` on `X` is total and a preorder. -/ @[algebra] class is_total_preorder (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) extends is_trans Ξ± r, is_total Ξ± r : Prop. /-- Every total pre-order is a pre-order. -/ instance is_total_preorder_is_preorder (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) [s : is_total_preorder Ξ± r] : is_preorder Ξ± r := {trans := s.trans, refl := Ξ» a, or.elim (@is_total.total _ r _ a a) id id} @[algebra] class is_partial_order (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) extends is_preorder Ξ± r, is_antisymm Ξ± r : Prop. @[algebra] class is_linear_order (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) extends is_partial_order Ξ± r, is_total Ξ± r : Prop. @[algebra] class is_equiv (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) extends is_preorder Ξ± r, is_symm Ξ± r : Prop. @[algebra] class is_per (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) extends is_symm Ξ± r, is_trans Ξ± r : Prop. @[algebra] class is_strict_order (Ξ± : Type u) (r : Ξ± β†’ Ξ± β†’ Prop) extends is_irrefl Ξ± r, is_trans Ξ± r : Prop. @[algebra] class is_incomp_trans (Ξ± : Type u) (lt : Ξ± β†’ Ξ± β†’ Prop) : Prop := (incomp_trans : βˆ€ a b c, (Β¬ lt a b ∧ Β¬ lt b a) β†’ (Β¬ lt b c ∧ Β¬ lt c b) β†’ (Β¬ lt a c ∧ Β¬ lt c a)) @[algebra] class is_strict_weak_order (Ξ± : Type u) (lt : Ξ± β†’ Ξ± β†’ Prop) extends is_strict_order Ξ± lt, is_incomp_trans Ξ± lt : Prop. @[algebra] class is_trichotomous (Ξ± : Type u) (lt : Ξ± β†’ Ξ± β†’ Prop) : Prop := (trichotomous : βˆ€ a b, lt a b ∨ a = b ∨ lt b a) @[algebra] class is_strict_total_order (Ξ± : Type u) (lt : Ξ± β†’ Ξ± β†’ Prop) extends is_trichotomous Ξ± lt, is_strict_order Ξ± lt : Prop. instance eq_is_equiv (Ξ± : Type u) : is_equiv Ξ± (=) := {symm := @eq.symm _, trans := @eq.trans _, refl := eq.refl} section variables {Ξ± : Type u} {r : Ξ± β†’ Ξ± β†’ Prop} local infix `β‰Ί`:50 := r lemma irrefl [is_irrefl Ξ± r] (a : Ξ±) : Β¬ a β‰Ί a := is_irrefl.irrefl a lemma refl [is_refl Ξ± r] (a : Ξ±) : a β‰Ί a := is_refl.refl a lemma trans [is_trans Ξ± r] {a b c : Ξ±} : a β‰Ί b β†’ b β‰Ί c β†’ a β‰Ί c := is_trans.trans _ _ _ lemma symm [is_symm Ξ± r] {a b : Ξ±} : a β‰Ί b β†’ b β‰Ί a := is_symm.symm _ _ lemma antisymm [is_antisymm Ξ± r] {a b : Ξ±} : a β‰Ί b β†’ b β‰Ί a β†’ a = b := is_antisymm.antisymm _ _ lemma asymm [is_asymm Ξ± r] {a b : Ξ±} : a β‰Ί b β†’ Β¬ b β‰Ί a := is_asymm.asymm _ _ lemma trichotomous [is_trichotomous Ξ± r] : βˆ€ (a b : Ξ±), a β‰Ί b ∨ a = b ∨ b β‰Ί a := is_trichotomous.trichotomous lemma incomp_trans [is_incomp_trans Ξ± r] {a b c : Ξ±} : (Β¬ a β‰Ί b ∧ Β¬ b β‰Ί a) β†’ (Β¬ b β‰Ί c ∧ Β¬ c β‰Ί b) β†’ (Β¬ a β‰Ί c ∧ Β¬ c β‰Ί a) := is_incomp_trans.incomp_trans _ _ _ @[priority 90] instance is_asymm_of_is_trans_of_is_irrefl [is_trans Ξ± r] [is_irrefl Ξ± r] : is_asymm Ξ± r := ⟨λ a b h₁ hβ‚‚, absurd (trans h₁ hβ‚‚) (irrefl a)⟩ section explicit_relation_variants variable (r) @[elab_simple] lemma irrefl_of [is_irrefl Ξ± r] (a : Ξ±) : Β¬ a β‰Ί a := irrefl a @[elab_simple] lemma refl_of [is_refl Ξ± r] (a : Ξ±) : a β‰Ί a := refl a @[elab_simple] lemma trans_of [is_trans Ξ± r] {a b c : Ξ±} : a β‰Ί b β†’ b β‰Ί c β†’ a β‰Ί c := trans @[elab_simple] lemma symm_of [is_symm Ξ± r] {a b : Ξ±} : a β‰Ί b β†’ b β‰Ί a := symm @[elab_simple] lemma asymm_of [is_asymm Ξ± r] {a b : Ξ±} : a β‰Ί b β†’ Β¬ b β‰Ί a := asymm @[elab_simple] lemma total_of [is_total Ξ± r] (a b : Ξ±) : a β‰Ί b ∨ b β‰Ί a := is_total.total _ _ @[elab_simple] lemma trichotomous_of [is_trichotomous Ξ± r] : βˆ€ (a b : Ξ±), a β‰Ί b ∨ a = b ∨ b β‰Ί a := trichotomous @[elab_simple] lemma incomp_trans_of [is_incomp_trans Ξ± r] {a b c : Ξ±} : (Β¬ a β‰Ί b ∧ Β¬ b β‰Ί a) β†’ (Β¬ b β‰Ί c ∧ Β¬ c β‰Ί b) β†’ (Β¬ a β‰Ί c ∧ Β¬ c β‰Ί a) := incomp_trans end explicit_relation_variants end namespace strict_weak_order section parameters {Ξ± : Type u} {r : Ξ± β†’ Ξ± β†’ Prop} local infix `β‰Ί`:50 := r def equiv (a b : Ξ±) : Prop := Β¬ a β‰Ί b ∧ Β¬ b β‰Ί a parameter [is_strict_weak_order Ξ± r] local infix ` β‰ˆ `:50 := equiv lemma erefl (a : Ξ±) : a β‰ˆ a := ⟨irrefl a, irrefl a⟩ lemma esymm {a b : Ξ±} : a β‰ˆ b β†’ b β‰ˆ a := Ξ» ⟨h₁, hβ‚‚βŸ©, ⟨hβ‚‚, hβ‚βŸ© lemma etrans {a b c : Ξ±} : a β‰ˆ b β†’ b β‰ˆ c β†’ a β‰ˆ c := incomp_trans lemma not_lt_of_equiv {a b : Ξ±} : a β‰ˆ b β†’ Β¬ a β‰Ί b := Ξ» h, h.1 lemma not_lt_of_equiv' {a b : Ξ±} : a β‰ˆ b β†’ Β¬ b β‰Ί a := Ξ» h, h.2 instance is_equiv : is_equiv Ξ± equiv := {refl := erefl, trans := @etrans, symm := @esymm} end /- Notation for the equivalence relation induced by lt -/ notation a ` β‰ˆ[`:50 lt `]` b:50 := @equiv _ lt a b end strict_weak_order lemma is_strict_weak_order_of_is_total_preorder {Ξ± : Type u} {le : Ξ± β†’ Ξ± β†’ Prop} {lt : Ξ± β†’ Ξ± β†’ Prop} [decidable_rel le] [s : is_total_preorder Ξ± le] (h : βˆ€ a b, lt a b ↔ Β¬ le b a) : is_strict_weak_order Ξ± lt := { trans := Ξ» a b c hab hbc, have nba : Β¬ le b a, from iff.mp (h _ _) hab, have ncb : Β¬ le c b, from iff.mp (h _ _) hbc, have hab : le a b, from or.resolve_left (total_of le b a) nba, have nca : Β¬ le c a, from Ξ» hca : le c a, have hcb : le c b, from trans_of le hca hab, absurd hcb ncb, iff.mpr (h _ _) nca, irrefl := Ξ» a hlt, absurd (refl_of le a) (iff.mp (h _ _) hlt), incomp_trans := Ξ» a b c ⟨nab, nba⟩ ⟨nbc, ncb⟩, have hba : le b a, from decidable.of_not_not (iff.mp (not_iff_not_of_iff (h _ _)) nab), have hab : le a b, from decidable.of_not_not (iff.mp (not_iff_not_of_iff (h _ _)) nba), have hcb : le c b, from decidable.of_not_not (iff.mp (not_iff_not_of_iff (h _ _)) nbc), have hbc : le b c, from decidable.of_not_not (iff.mp (not_iff_not_of_iff (h _ _)) ncb), have hac : le a c, from trans_of le hab hbc, have hca : le c a, from trans_of le hcb hba, and.intro (Ξ» n, absurd hca (iff.mp (h _ _) n)) (Ξ» n, absurd hac (iff.mp (h _ _) n)) } lemma lt_of_lt_of_incomp {Ξ± : Type u} {lt : Ξ± β†’ Ξ± β†’ Prop} [is_strict_weak_order Ξ± lt] [decidable_rel lt] : βˆ€ {a b c}, lt a b β†’ (Β¬ lt b c ∧ Β¬ lt c b) β†’ lt a c := Ξ» a b c hab ⟨nbc, ncb⟩, have nca : Β¬ lt c a, from Ξ» hca, absurd (trans_of lt hca hab) ncb, decidable.by_contradiction $ Ξ» nac : Β¬ lt a c, have Β¬ lt a b ∧ Β¬ lt b a, from incomp_trans_of lt ⟨nac, nca⟩ ⟨ncb, nbc⟩, absurd hab this.1 lemma lt_of_incomp_of_lt {Ξ± : Type u} {lt : Ξ± β†’ Ξ± β†’ Prop} [is_strict_weak_order Ξ± lt] [decidable_rel lt] : βˆ€ {a b c}, (Β¬ lt a b ∧ Β¬ lt b a) β†’ lt b c β†’ lt a c := Ξ» a b c ⟨nab, nba⟩ hbc, have nca : Β¬ lt c a, from Ξ» hca, absurd (trans_of lt hbc hca) nba, decidable.by_contradiction $ Ξ» nac : Β¬ lt a c, have Β¬ lt b c ∧ Β¬ lt c b, from incomp_trans_of lt ⟨nba, nab⟩ ⟨nac, nca⟩, absurd hbc this.1 lemma eq_of_incomp {Ξ± : Type u} {lt : Ξ± β†’ Ξ± β†’ Prop} [is_trichotomous Ξ± lt] {a b} : (Β¬ lt a b ∧ Β¬ lt b a) β†’ a = b := Ξ» ⟨nab, nba⟩, match trichotomous_of lt a b with | or.inl hab := absurd hab nab | or.inr (or.inl hab) := hab | or.inr (or.inr hba) := absurd hba nba end lemma eq_of_eqv_lt {Ξ± : Type u} {lt : Ξ± β†’ Ξ± β†’ Prop} [is_trichotomous Ξ± lt] {a b} : a β‰ˆ[lt] b β†’ a = b := eq_of_incomp lemma incomp_iff_eq {Ξ± : Type u} {lt : Ξ± β†’ Ξ± β†’ Prop} [is_trichotomous Ξ± lt] [is_irrefl Ξ± lt] (a b) : (Β¬ lt a b ∧ Β¬ lt b a) ↔ a = b := iff.intro eq_of_incomp (Ξ» hab, eq.subst hab (and.intro (irrefl_of lt a) (irrefl_of lt a))) lemma eqv_lt_iff_eq {Ξ± : Type u} {lt : Ξ± β†’ Ξ± β†’ Prop} [is_trichotomous Ξ± lt] [is_irrefl Ξ± lt] (a b) : a β‰ˆ[lt] b ↔ a = b := incomp_iff_eq a b lemma not_lt_of_lt {Ξ± : Type u} {lt : Ξ± β†’ Ξ± β†’ Prop} [is_strict_order Ξ± lt] {a b} : lt a b β†’ Β¬ lt b a := Ξ» h₁ hβ‚‚, absurd (trans_of lt h₁ hβ‚‚) (irrefl_of lt _)
70b9e6a608f46ff56381d437d41a47f13bff0344
3aad12fe82645d2d3173fbedc2e5c2ba945a4d75
/src/tactic/nursery.lean
cf7c477dce4b752d3f9ac48b1768aa8a6def2ca6
[]
no_license
seanpm2001/LeanProver-Community_MathLIB-Nursery
4f88d539cb18d73a94af983092896b851e6640b5
0479b31fa5b4d39f41e89b8584c9f5bf5271e8ec
refs/heads/master
1,688,730,786,645
1,572,070,026,000
1,572,070,026,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,911
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon -/ import data.list.basic import tactic.basic namespace level meta def fold_mvar {Ξ±} : level β†’ (name β†’ Ξ± β†’ Ξ±) β†’ Ξ± β†’ Ξ± | zero f := id | (succ a) f := fold_mvar a f | (param a) f := id | (mvar a) f := f a | (max a b) f := fold_mvar a f ∘ fold_mvar b f | (imax a b) f := fold_mvar a f ∘ fold_mvar b f end level namespace expr meta def replace_all (e : expr) (p : expr β†’ Prop) [decidable_pred p] (r : expr) : expr := e.replace $ Ξ» e i, guard (p e) >> pure (r.lift_vars 0 i) meta def const_params : expr β†’ list level | (const _ ls) := ls | _ := [] meta def collect_meta_univ (e : expr) : list name := native.rb_set.to_list $ e.fold native.mk_rb_set $ Ξ» e' i s, match e' with | (sort u) := u.fold_mvar (flip native.rb_set.insert) s | (const _ ls) := ls.foldl (Ξ» s' l, l.fold_mvar (flip native.rb_set.insert) s') s | _ := s end end expr namespace tactic meta def unify_univ (u u' : level) : tactic unit := unify (expr.sort u) (expr.sort u') meta def add_decl' (d : declaration) : tactic expr := do add_decl d, pure $ expr.const d.to_name $ d.univ_params.map level.param meta def renew : expr β†’ tactic expr | (expr.local_const uniq pp bi t) := mk_local' pp bi t | e := fail format!"{e} is not a local constant" meta def is_type (e : expr) : tactic bool := do (expr.sort _) ← infer_type e | pure ff, pure tt meta def list_macros : expr β†’ list (name Γ— list expr) | e := e.fold [] (Ξ» m i s, match m with | (expr.macro m args) := (expr.macro_def_name m, args) :: s | _ := s end) meta def expand_untrusted (tac : tactic unit) : tactic unit := do tgt ← target, mv ← mk_meta_var tgt, gs ← get_goals, set_goals [mv], tac, env ← get_env, pr ← env.unfold_untrusted_macros <$> instantiate_mvars mv, set_goals gs, exact pr meta def binders : expr β†’ tactic (list expr) | (expr.pi n bi d b) := do v ← mk_local' n bi d, (::) v <$> binders (b.instantiate_var v) | _ := pure [] meta def rec_args_count (t c : name) : tactic β„• := do ct ← mk_const c >>= infer_type, (list.length ∘ list.filter (Ξ» v : expr, v.local_type.is_app_of t)) <$> binders ct meta def match_induct_hyp (n : name) : list expr β†’ list expr β†’ tactic (list $ expr Γ— option expr) | [] [] := pure [] | [] _ := fail "wrong number of inductive hypotheses" | (x :: xs) [] := (::) (x,none) <$> match_induct_hyp xs [] | (x :: xs) (h :: hs) := do t ← infer_type x, if t.is_app_of n then (::) (x,h) <$> match_induct_hyp xs hs else (::) (x,none) <$> match_induct_hyp xs (h :: hs) meta def is_recursive_type (n : name) : tactic bool := do e ← get_env, let cs := e.constructors_of n, rs ← cs.mmap (rec_args_count n), pure $ rs.any (Ξ» r, r > 0) meta def better_induction (e : expr) : tactic $ list (name Γ— list (expr Γ— option expr) Γ— list (name Γ— expr)) := do t ← infer_type e, let tn := t.get_app_fn.const_name, focus1 $ do vs ← induction e, gs ← get_goals, vs' ← mzip_with (Ξ» g (pat : name Γ— list expr Γ— list (name Γ— expr)), do let ⟨n,args,ΟƒβŸ© := pat, set_goals [g], nrec ← rec_args_count tn n, let ⟨args,rec⟩ := args.split_at (args.length - nrec), args ← match_induct_hyp tn args rec, pure ((n,args,Οƒ))) gs vs, set_goals gs, pure vs' meta def extract_def' {Ξ±} (n : name) (trusted : bool) (elab_def : tactic Ξ±) : tactic Ξ± := do cxt ← list.map to_implicit <$> local_context, t ← target, (r,d) ← solve_aux t elab_def, d ← instantiate_mvars d, t' ← pis cxt t, d' ← lambdas cxt d, let univ := t'.collect_univ_params, add_decl $ declaration.defn n univ t' d' (reducibility_hints.regular 1 tt) trusted, r <$ (applyc n; assumption) open expr list nat meta def remove_intl_const : expr β†’ tactic expr | v@(local_const uniq pp bi _) := do t ← infer_type v, pure $ local_const uniq pp bi t | e := pure e meta def intron' : β„• β†’ tactic (list expr) | 0 := pure [] | (succ n) := (::) <$> intro1 <*> intron' n meta def unpi : expr β†’ tactic (list expr Γ— expr) | (pi n bi d b) := do v ← mk_local' n bi d, prod.map (cons v) id <$> unpi (b.instantiate_var v) | e := pure ([],e) meta def unify_app_aux : expr β†’ expr β†’ list expr β†’ tactic expr | e (pi _ _ d b) (a :: as) := do t ← infer_type a, unify t d, e' ← head_beta (e a), b' ← whnf (b.instantiate_var a), unify_app_aux e' b' as | e t (_ :: _) := fail "too many arguments" | e _ [] := pure e meta def unify_app (e : expr) (args : list expr) : tactic expr := do t ← infer_type e >>= whnf, unify_app_aux e t args end tactic namespace tactic.interactive meta def splita := split; [skip, assumption] end tactic.interactive
925b215339437ccd7ffdc5aeb95f7e19fa3f6c74
1a61aba1b67cddccce19532a9596efe44be4285f
/library/algebra/field.lean
5599bad8fcc88c580904aa70e57694356c5f34a5
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
19,648
lean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis Structures with multiplicative and additive components, including division rings and fields. The development is modeled after Isabelle's library. -/ import logic.eq logic.connectives data.unit data.sigma data.prod import algebra.binary algebra.group algebra.ring open eq eq.ops namespace algebra variable {A : Type} -- in division rings, 1 / 0 = 0 structure division_ring [class] (A : Type) extends ring A, has_inv A, zero_ne_one_class A := (mul_inv_cancel : βˆ€{a}, a β‰  zero β†’ mul a (inv a) = one) (inv_mul_cancel : βˆ€{a}, a β‰  zero β†’ mul (inv a) a = one) --(inv_zero : inv zero = zero) section division_ring variables [s : division_ring A] {a b c : A} include s definition divide (a b : A) : A := a * b⁻¹ infix [priority algebra.prio] `/` := divide -- only in this file local attribute divide [reducible] theorem mul_inv_cancel (H : a β‰  0) : a * a⁻¹ = 1 := division_ring.mul_inv_cancel H theorem inv_mul_cancel (H : a β‰  0) : a⁻¹ * a = 1 := division_ring.inv_mul_cancel H theorem inv_eq_one_div (a : A) : a⁻¹ = 1 / a := !one_mul⁻¹ theorem div_eq_mul_one_div (a b : A) : a / b = a * (1 / b) := by rewrite [↑divide, one_mul] theorem mul_one_div_cancel (H : a β‰  0) : a * (1 / a) = 1 := by rewrite [-inv_eq_one_div, (mul_inv_cancel H)] theorem one_div_mul_cancel (H : a β‰  0) : (1 / a) * a = 1 := by rewrite [-inv_eq_one_div, (inv_mul_cancel H)] theorem div_self (H : a β‰  0) : a / a = 1 := mul_inv_cancel H theorem one_div_one : 1 / 1 = (1:A) := div_self (ne.symm zero_ne_one) theorem mul_div_assoc (a b : A) : (a * b) / c = a * (b / c) := !mul.assoc theorem one_div_ne_zero (H : a β‰  0) : 1 / a β‰  0 := assume H2 : 1 / a = 0, have C1 : 0 = (1:A), from symm (by rewrite [-(mul_one_div_cancel H), H2, mul_zero]), absurd C1 zero_ne_one theorem one_inv_eq : 1⁻¹ = (1:A) := by rewrite [-mul_one, inv_mul_cancel (ne.symm (@zero_ne_one A _))] theorem div_one (a : A) : a / 1 = a := by rewrite [↑divide, one_inv_eq, mul_one] theorem zero_div (a : A) : 0 / a = 0 := !zero_mul -- note: integral domain has a "mul_ne_zero". A commutative division ring is an integral -- domain, but let's not define that class for now. theorem division_ring.mul_ne_zero (Ha : a β‰  0) (Hb : b β‰  0) : a * b β‰  0 := assume H : a * b = 0, have C1 : a = 0, by rewrite [-mul_one, -(mul_one_div_cancel Hb), -mul.assoc, H, zero_mul], absurd C1 Ha theorem mul_ne_zero_comm (H : a * b β‰  0) : b * a β‰  0 := have H2 : a β‰  0 ∧ b β‰  0, from ne_zero_and_ne_zero_of_mul_ne_zero H, division_ring.mul_ne_zero (and.right H2) (and.left H2) theorem eq_one_div_of_mul_eq_one (H : a * b = 1) : b = 1 / a := have a β‰  0, from (suppose a = 0, have 0 = (1:A), by rewrite [-(zero_mul b), -this, H], absurd this zero_ne_one), show b = 1 / a, from symm (calc 1 / a = (1 / a) * 1 : mul_one ... = (1 / a) * (a * b) : H ... = (1 / a) * a * b : mul.assoc ... = 1 * b : one_div_mul_cancel this ... = b : one_mul) theorem eq_one_div_of_mul_eq_one_left (H : b * a = 1) : b = 1 / a := have a β‰  0, from (suppose a = 0, have 0 = 1, from symm (calc 1 = b * a : symm H ... = b * 0 : this ... = 0 : mul_zero), absurd this zero_ne_one), show b = 1 / a, from symm (calc 1 / a = 1 * (1 / a) : one_mul ... = b * a * (1 / a) : H ... = b * (a * (1 / a)) : mul.assoc ... = b * 1 : mul_one_div_cancel this ... = b : mul_one) theorem division_ring.one_div_mul_one_div (Ha : a β‰  0) (Hb : b β‰  0) : (1 / a) * (1 / b) = 1 / (b * a) := have (b * a) * ((1 / a) * (1 / b)) = 1, by rewrite [mul.assoc, -(mul.assoc a), (mul_one_div_cancel Ha), one_mul, (mul_one_div_cancel Hb)], eq_one_div_of_mul_eq_one this theorem one_div_neg_one_eq_neg_one : (1:A) / (-1) = -1 := have (-1) * (-1) = (1:A), by rewrite [-neg_eq_neg_one_mul, neg_neg], symm (eq_one_div_of_mul_eq_one this) theorem division_ring.one_div_neg_eq_neg_one_div (H : a β‰  0) : 1 / (- a) = - (1 / a) := have -1 β‰  0, from (suppose -1 = 0, absurd (symm (calc 1 = -(-1) : neg_neg ... = -0 : this ... = (0:A) : neg_zero)) zero_ne_one), calc 1 / (- a) = 1 / ((-1) * a) : neg_eq_neg_one_mul ... = (1 / a) * (1 / (- 1)) : division_ring.one_div_mul_one_div H this ... = (1 / a) * (-1) : one_div_neg_one_eq_neg_one ... = - (1 / a) : mul_neg_one_eq_neg theorem div_neg_eq_neg_div (b : A) (Ha : a β‰  0) : b / (- a) = - (b / a) := calc b / (- a) = b * (1 / (- a)) : inv_eq_one_div ... = b * -(1 / a) : division_ring.one_div_neg_eq_neg_one_div Ha ... = -(b * (1 / a)) : neg_mul_eq_mul_neg ... = - (b * a⁻¹) : inv_eq_one_div theorem neg_div (a b : A) : (-b) / a = - (b / a) := by rewrite [neg_eq_neg_one_mul, mul_div_assoc, -neg_eq_neg_one_mul] theorem division_ring.neg_div_neg_eq (a : A) {b : A} (Hb : b β‰  0) : (-a) / (-b) = a / b := by rewrite [(div_neg_eq_neg_div _ Hb), neg_div, neg_neg] theorem division_ring.one_div_one_div (H : a β‰  0) : 1 / (1 / a) = a := symm (eq_one_div_of_mul_eq_one_left (mul_one_div_cancel H)) theorem division_ring.eq_of_one_div_eq_one_div (Ha : a β‰  0) (Hb : b β‰  0) (H : 1 / a = 1 / b) : a = b := by rewrite [-(division_ring.one_div_one_div Ha), H, (division_ring.one_div_one_div Hb)] theorem mul_inv_eq (Ha : a β‰  0) (Hb : b β‰  0) : (b * a)⁻¹ = a⁻¹ * b⁻¹ := eq.symm (calc a⁻¹ * b⁻¹ = (1 / a) * b⁻¹ : inv_eq_one_div ... = (1 / a) * (1 / b) : inv_eq_one_div ... = (1 / (b * a)) : division_ring.one_div_mul_one_div Ha Hb ... = (b * a)⁻¹ : inv_eq_one_div) theorem mul_div_cancel (a : A) {b : A} (Hb : b β‰  0) : a * b / b = a := by rewrite [↑divide, mul.assoc, (mul_inv_cancel Hb), mul_one] theorem div_mul_cancel (a : A) {b : A} (Hb : b β‰  0) : a / b * b = a := by rewrite [↑divide, mul.assoc, (inv_mul_cancel Hb), mul_one] theorem div_add_div_same (a b c : A) : a / c + b / c = (a + b) / c := !right_distrib⁻¹ theorem div_sub_div_same (a b c : A) : (a / c) - (b / c) = (a - b) / c := by rewrite [sub_eq_add_neg, -neg_div, div_add_div_same] theorem one_div_mul_add_mul_one_div_eq_one_div_add_one_div (Ha : a β‰  0) (Hb : b β‰  0) : (1 / a) * (a + b) * (1 / b) = 1 / a + 1 / b := by rewrite [(left_distrib (1 / a)), (one_div_mul_cancel Ha), right_distrib, one_mul, mul.assoc, (mul_one_div_cancel Hb), mul_one, add.comm] theorem one_div_mul_sub_mul_one_div_eq_one_div_add_one_div (Ha : a β‰  0) (Hb : b β‰  0) : (1 / a) * (b - a) * (1 / b) = 1 / a - 1 / b := by rewrite [(mul_sub_left_distrib (1 / a)), (one_div_mul_cancel Ha), mul_sub_right_distrib, one_mul, mul.assoc, (mul_one_div_cancel Hb), mul_one, one_mul] theorem div_eq_one_iff_eq (a : A) {b : A} (Hb : b β‰  0) : a / b = 1 ↔ a = b := iff.intro (suppose a / b = 1, symm (calc b = 1 * b : one_mul ... = a / b * b : this ... = a : div_mul_cancel _ Hb)) (suppose a = b, calc a / b = b / b : this ... = 1 : div_self Hb) theorem eq_of_div_eq_one (a : A) {b : A} (Hb : b β‰  0) : a / b = 1 β†’ a = b := iff.mp (!div_eq_one_iff_eq Hb) theorem eq_div_iff_mul_eq (a : A) {b : A} (Hc : c β‰  0) : a = b / c ↔ a * c = b := iff.intro (suppose a = b / c, by rewrite [this, (!div_mul_cancel Hc)]) (suppose a * c = b, by rewrite [-(!mul_div_cancel Hc), this]) theorem eq_div_of_mul_eq (a b : A) {c : A} (Hc : c β‰  0) : a * c = b β†’ a = b / c := iff.mpr (!eq_div_iff_mul_eq Hc) theorem mul_eq_of_eq_div (a b: A) {c : A} (Hc : c β‰  0) : a = b / c β†’ a * c = b := iff.mp (!eq_div_iff_mul_eq Hc) theorem add_div_eq_mul_add_div (a b : A) {c : A} (Hc : c β‰  0) : a + b / c = (a * c + b) / c := have (a + b / c) * c = a * c + b, by rewrite [right_distrib, (!div_mul_cancel Hc)], (iff.elim_right (!eq_div_iff_mul_eq Hc)) this theorem mul_mul_div (a : A) {c : A} (Hc : c β‰  0) : a = a * c * (1 / c) := calc a = a * 1 : mul_one ... = a * (c * (1 / c)) : mul_one_div_cancel Hc ... = a * c * (1 / c) : mul.assoc -- There are many similar rules to these last two in the Isabelle library -- that haven't been ported yet. Do as necessary. end division_ring structure field [class] (A : Type) extends division_ring A, comm_ring A section field variables [s : field A] {a b c d: A} include s local attribute divide [reducible] theorem field.one_div_mul_one_div (Ha : a β‰  0) (Hb : b β‰  0) : (1 / a) * (1 / b) = 1 / (a * b) := by rewrite [(division_ring.one_div_mul_one_div Ha Hb), mul.comm b] theorem field.div_mul_right (Hb : b β‰  0) (H : a * b β‰  0) : a / (a * b) = 1 / b := have a β‰  0, from and.left (ne_zero_and_ne_zero_of_mul_ne_zero H), symm (calc 1 / b = 1 * (1 / b) : one_mul ... = (a * a⁻¹) * (1 / b) : mul_inv_cancel this ... = a * (a⁻¹ * (1 / b)) : mul.assoc ... = a * ((1 / a) * (1 / b)) : inv_eq_one_div ... = a * (1 / (b * a)) : division_ring.one_div_mul_one_div this Hb ... = a * (1 / (a * b)) : mul.comm ... = a * (a * b)⁻¹ : inv_eq_one_div) theorem field.div_mul_left (Ha : a β‰  0) (H : a * b β‰  0) : b / (a * b) = 1 / a := let H1 : b * a β‰  0 := mul_ne_zero_comm H in by rewrite [mul.comm a, (field.div_mul_right Ha H1)] theorem mul_div_cancel_left (Ha : a β‰  0) : a * b / a = b := by rewrite [mul.comm a, (!mul_div_cancel Ha)] theorem mul_div_cancel' (Hb : b β‰  0) : b * (a / b) = a := by rewrite [mul.comm, (!div_mul_cancel Hb)] theorem one_div_add_one_div (Ha : a β‰  0) (Hb : b β‰  0) : 1 / a + 1 / b = (a + b) / (a * b) := assert a * b β‰  0, from (division_ring.mul_ne_zero Ha Hb), by rewrite [add.comm, -(field.div_mul_left Ha this), -(field.div_mul_right Hb this), ↑divide, -right_distrib] theorem field.div_mul_div (a : A) {b : A} (c : A) {d : A} (Hb : b β‰  0) (Hd : d β‰  0) : (a / b) * (c / d) = (a * c) / (b * d) := by rewrite [↑divide, 2 mul.assoc, (mul.comm b⁻¹), mul.assoc, (mul_inv_eq Hd Hb)] theorem mul_div_mul_left (a : A) {b c : A} (Hb : b β‰  0) (Hc : c β‰  0) : (c * a) / (c * b) = a / b := by rewrite [-(!field.div_mul_div Hc Hb), (div_self Hc), one_mul] theorem mul_div_mul_right (a : A) {b c : A} (Hb : b β‰  0) (Hc : c β‰  0) : (a * c) / (b * c) = a / b := by rewrite [(mul.comm a), (mul.comm b), (!mul_div_mul_left Hb Hc)] theorem div_mul_eq_mul_div (a b c : A) : (b / c) * a = (b * a) / c := by rewrite [↑divide, mul.assoc, (mul.comm c⁻¹), -mul.assoc] theorem field.div_mul_eq_mul_div_comm (a b : A) {c : A} (Hc : c β‰  0) : (b / c) * a = b * (a / c) := by rewrite [(div_mul_eq_mul_div), -(one_mul c), -(!field.div_mul_div (ne.symm zero_ne_one) Hc), div_one, one_mul] theorem div_add_div (a : A) {b : A} (c : A) {d : A} (Hb : b β‰  0) (Hd : d β‰  0) : (a / b) + (c / d) = ((a * d) + (b * c)) / (b * d) := by rewrite [-(!mul_div_mul_right Hb Hd), -(!mul_div_mul_left Hd Hb), div_add_div_same] theorem div_sub_div (a : A) {b : A} (c : A) {d : A} (Hb : b β‰  0) (Hd : d β‰  0) : (a / b) - (c / d) = ((a * d) - (b * c)) / (b * d) := by rewrite [↑sub, neg_eq_neg_one_mul, -mul_div_assoc, (!div_add_div Hb Hd), -mul.assoc, (mul.comm b), mul.assoc, -neg_eq_neg_one_mul] theorem mul_eq_mul_of_div_eq_div (a : A) {b : A} (c : A) {d : A} (Hb : b β‰  0) (Hd : d β‰  0) (H : a / b = c / d) : a * d = c * b := by rewrite [-mul_one, mul.assoc, (mul.comm d), -mul.assoc, -(div_self Hb), -(!field.div_mul_eq_mul_div_comm Hb), H, (div_mul_eq_mul_div), (!div_mul_cancel Hd)] theorem field.one_div_div (Ha : a β‰  0) (Hb : b β‰  0) : 1 / (a / b) = b / a := have (a / b) * (b / a) = 1, from calc (a / b) * (b / a) = (a * b) / (b * a) : !field.div_mul_div Hb Ha ... = (a * b) / (a * b) : mul.comm ... = 1 : div_self (division_ring.mul_ne_zero Ha Hb), symm (eq_one_div_of_mul_eq_one this) theorem field.div_div_eq_mul_div (a : A) {b c : A} (Hb : b β‰  0) (Hc : c β‰  0) : a / (b / c) = (a * c) / b := by rewrite [div_eq_mul_one_div, (field.one_div_div Hb Hc), -mul_div_assoc] theorem field.div_div_eq_div_mul (a : A) {b c : A} (Hb : b β‰  0) (Hc : c β‰  0) : (a / b) / c = a / (b * c) := by rewrite [div_eq_mul_one_div, (!field.div_mul_div Hb Hc), mul_one] theorem field.div_div_div_div_eq (a : A) {b c d : A} (Hb : b β‰  0) (Hc : c β‰  0) (Hd : d β‰  0) : (a / b) / (c / d) = (a * d) / (b * c) := by rewrite [(!field.div_div_eq_mul_div Hc Hd), (div_mul_eq_mul_div), (!field.div_div_eq_div_mul Hb Hc)] theorem field.div_mul_eq_div_mul_one_div (a : A) {b c : A} (Hb : b β‰  0) (Hc : c β‰  0) : a / (b * c) = (a / b) * (1 / c) := by rewrite [-!field.div_div_eq_div_mul Hb Hc, -div_eq_mul_one_div] end field structure discrete_field [class] (A : Type) extends field A := (has_decidable_eq : decidable_eq A) (inv_zero : inv zero = zero) attribute discrete_field.has_decidable_eq [instance] section discrete_field variable [s : discrete_field A] include s variables {a b c d : A} -- many of the theorems in discrete_field are the same as theorems in field or division ring, -- but with fewer hypotheses since 0⁻¹ = 0 and equality is decidable. theorem discrete_field.eq_zero_or_eq_zero_of_mul_eq_zero (x y : A) (H : x * y = 0) : x = 0 ∨ y = 0 := decidable.by_cases (suppose x = 0, or.inl this) (suppose x β‰  0, or.inr (by rewrite [-one_mul, -(inv_mul_cancel this), mul.assoc, H, mul_zero])) definition discrete_field.to_integral_domain [trans-instance] [reducible] [coercion] : integral_domain A := ⦃ integral_domain, s, eq_zero_or_eq_zero_of_mul_eq_zero := discrete_field.eq_zero_or_eq_zero_of_mul_eq_zero⦄ theorem inv_zero : 0⁻¹ = (0:A) := !discrete_field.inv_zero theorem one_div_zero : 1 / 0 = (0:A) := calc 1 / 0 = 1 * 0⁻¹ : refl ... = 1 * 0 : discrete_field.inv_zero A ... = 0 : mul_zero theorem div_zero (a : A) : a / 0 = 0 := by rewrite [div_eq_mul_one_div, one_div_zero, mul_zero] theorem ne_zero_of_one_div_ne_zero (H : 1 / a β‰  0) : a β‰  0 := assume Ha : a = 0, absurd (Ha⁻¹ β–Έ one_div_zero) H theorem eq_zero_of_one_div_eq_zero (H : 1 / a = 0) : a = 0 := decidable.by_cases (assume Ha, Ha) (assume Ha, false.elim ((one_div_ne_zero Ha) H)) variables (a b) theorem one_div_mul_one_div' : (1 / a) * (1 / b) = 1 / (b * a) := decidable.by_cases (suppose a = 0, by rewrite [this, div_zero, zero_mul, -(@div_zero A s 1), mul_zero b]) (assume Ha : a β‰  0, decidable.by_cases (suppose b = 0, by rewrite [this, div_zero, mul_zero, -(@div_zero A s 1), zero_mul a]) (suppose b β‰  0, division_ring.one_div_mul_one_div Ha this)) theorem one_div_neg_eq_neg_one_div : 1 / (- a) = - (1 / a) := decidable.by_cases (suppose a = 0, by rewrite [this, neg_zero, 2 div_zero, neg_zero]) (suppose a β‰  0, division_ring.one_div_neg_eq_neg_one_div this) theorem neg_div_neg_eq : (-a) / (-b) = a / b := decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, neg_zero, 2 div_zero]) (assume Hb : b β‰  0, !division_ring.neg_div_neg_eq Hb) theorem one_div_one_div : 1 / (1 / a) = a := decidable.by_cases (assume Ha : a = 0, by rewrite [Ha, 2 div_zero]) (assume Ha : a β‰  0, division_ring.one_div_one_div Ha) variables {a b} theorem eq_of_one_div_eq_one_div (H : 1 / a = 1 / b) : a = b := decidable.by_cases (assume Ha : a = 0, have Hb : b = 0, from eq_zero_of_one_div_eq_zero (by rewrite [-H, Ha, div_zero]), Hb⁻¹ β–Έ Ha) (assume Ha : a β‰  0, have Hb : b β‰  0, from ne_zero_of_one_div_ne_zero (H β–Έ (one_div_ne_zero Ha)), division_ring.eq_of_one_div_eq_one_div Ha Hb H) variables (a b) theorem mul_inv' : (b * a)⁻¹ = a⁻¹ * b⁻¹ := decidable.by_cases (assume Ha : a = 0, by rewrite [Ha, mul_zero, 2 inv_zero, zero_mul]) (assume Ha : a β‰  0, decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, zero_mul, 2 inv_zero, mul_zero]) (assume Hb : b β‰  0, mul_inv_eq Ha Hb)) -- the following are specifically for fields theorem one_div_mul_one_div : (1 / a) * (1 / b) = 1 / (a * b) := by rewrite [one_div_mul_one_div', mul.comm b] variable {a} theorem div_mul_right (Ha : a β‰  0) : a / (a * b) = 1 / b := decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, mul_zero, 2 div_zero]) (assume Hb : b β‰  0, field.div_mul_right Hb (mul_ne_zero Ha Hb)) variables (a) {b} theorem div_mul_left (Hb : b β‰  0) : b / (a * b) = 1 / a := by rewrite [mul.comm a, div_mul_right _ Hb] variables (a b c) theorem div_mul_div : (a / b) * (c / d) = (a * c) / (b * d) := decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, div_zero, zero_mul, -(@div_zero A s (a * c)), zero_mul]) (assume Hb : b β‰  0, decidable.by_cases (assume Hd : d = 0, by rewrite [Hd, div_zero, mul_zero, -(@div_zero A s (a * c)), mul_zero]) (assume Hd : d β‰  0, !field.div_mul_div Hb Hd)) variable {c} theorem mul_div_mul_left' (Hc : c β‰  0) : (c * a) / (c * b) = a / b := decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, mul_zero, 2 div_zero]) (assume Hb : b β‰  0, !mul_div_mul_left Hb Hc) theorem mul_div_mul_right' (Hc : c β‰  0) : (a * c) / (b * c) = a / b := by rewrite [(mul.comm a), (mul.comm b), (!mul_div_mul_left' Hc)] variables (a b c d) theorem div_mul_eq_mul_div_comm : (b / c) * a = b * (a / c) := decidable.by_cases (assume Hc : c = 0, by rewrite [Hc, div_zero, zero_mul, -(mul_zero b), -(@div_zero A s a)]) (assume Hc : c β‰  0, !field.div_mul_eq_mul_div_comm Hc) theorem one_div_div : 1 / (a / b) = b / a := decidable.by_cases (assume Ha : a = 0, by rewrite [Ha, zero_div, 2 div_zero]) (assume Ha : a β‰  0, decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, 2 div_zero, zero_div]) (assume Hb : b β‰  0, field.one_div_div Ha Hb)) theorem div_div_eq_mul_div : a / (b / c) = (a * c) / b := by rewrite [div_eq_mul_one_div, one_div_div, -mul_div_assoc] theorem div_div_eq_div_mul : (a / b) / c = a / (b * c) := by rewrite [div_eq_mul_one_div, div_mul_div, mul_one] theorem div_div_div_div_eq : (a / b) / (c / d) = (a * d) / (b * c) := by rewrite [div_div_eq_mul_div, div_mul_eq_mul_div, div_div_eq_div_mul] variable {a} theorem div_helper (H : a β‰  0) : (1 / (a * b)) * a = 1 / b := by rewrite [div_mul_eq_mul_div, one_mul, !div_mul_right H] variable (a) theorem div_mul_eq_div_mul_one_div : a / (b * c) = (a / b) * (1 / c) := by rewrite [-div_div_eq_div_mul, -div_eq_mul_one_div] end discrete_field end algebra
e7f577b09c00f5cf7bf227dfec0cfb82f7e55e73
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/rat/cast.lean
db2a8da7fe2577808e8f5d796b831e07987420ca
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
13,290
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 data.rat.order import data.int.char_zero /-! # Casts for Rational Numbers ## Summary We define the canonical injection from β„š into an arbitrary division ring and prove various casting lemmas showing the well-behavedness of this injection. ## Notations - `/.` is infix notation for `rat.mk`. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, cast, coercion, casting -/ namespace rat variable {Ξ± : Type*} open_locale rat section with_div_ring variable [division_ring Ξ±] /-- Construct the canonical injection from `β„š` into an arbitrary division ring. If the field has positive characteristic `p`, we define `1 / p = 1 / 0 = 0` for consistency with our division by zero convention. -/ -- see Note [coercion into rings] @[priority 900] instance cast_coe : has_coe_t β„š Ξ± := ⟨λ r, r.1 / r.2⟩ theorem cast_def (r : β„š) : (r : Ξ±) = r.num / r.denom := rfl @[simp] theorem cast_of_int (n : β„€) : (of_int n : Ξ±) = n := show (n / (1:β„•) : Ξ±) = n, by rw [nat.cast_one, div_one] @[simp, norm_cast] theorem cast_coe_int (n : β„€) : ((n : β„š) : Ξ±) = n := by rw [coe_int_eq_of_int, cast_of_int] @[simp, norm_cast] theorem cast_coe_nat (n : β„•) : ((n : β„š) : Ξ±) = n := cast_coe_int n @[simp, norm_cast] theorem cast_zero : ((0 : β„š) : Ξ±) = 0 := (cast_of_int _).trans int.cast_zero @[simp, norm_cast] theorem cast_one : ((1 : β„š) : Ξ±) = 1 := (cast_of_int _).trans int.cast_one theorem cast_commute (r : β„š) (a : Ξ±) : commute ↑r a := (r.1.cast_commute a).div_left (r.2.cast_commute a) theorem cast_comm (r : β„š) (a : Ξ±) : (r : Ξ±) * a = a * r := (cast_commute r a).eq theorem commute_cast (a : Ξ±) (r : β„š) : commute a r := (r.cast_commute a).symm @[norm_cast] theorem cast_mk_of_ne_zero (a b : β„€) (b0 : (b:Ξ±) β‰  0) : (a /. b : Ξ±) = a / b := begin have b0' : b β‰  0, { refine mt _ b0, simp {contextual := tt} }, cases e : a /. b with n d h c, have d0 : (d:Ξ±) β‰  0, { intro d0, have dd := denom_dvd a b, cases (show (d:β„€) ∣ b, by rwa e at dd) with k ke, have : (b:Ξ±) = (d:Ξ±) * (k:Ξ±), {rw [ke, int.cast_mul], refl}, rw [d0, zero_mul] at this, contradiction }, rw [num_denom'] at e, have := congr_arg (coe : β„€ β†’ Ξ±) ((mk_eq b0' $ ne_of_gt $ int.coe_nat_pos.2 h).1 e), rw [int.cast_mul, int.cast_mul, int.cast_coe_nat] at this, symmetry, change (a / b : Ξ±) = n / d, rw [div_eq_mul_inv, eq_div_iff_mul_eq d0, mul_assoc, (d.commute_cast _).eq, ← mul_assoc, this, mul_assoc, mul_inv_cancel b0, mul_one] end @[norm_cast] theorem cast_add_of_ne_zero : βˆ€ {m n : β„š}, (m.denom : Ξ±) β‰  0 β†’ (n.denom : Ξ±) β‰  0 β†’ ((m + n : β„š) : Ξ±) = m + n | ⟨n₁, d₁, h₁, cβ‚βŸ© ⟨nβ‚‚, dβ‚‚, hβ‚‚, cβ‚‚βŸ© := Ξ» (d₁0 : (d₁:Ξ±) β‰  0) (dβ‚‚0 : (dβ‚‚:Ξ±) β‰  0), begin have d₁0' : (d₁:β„€) β‰  0 := int.coe_nat_ne_zero.2 (Ξ» e, by rw e at d₁0; exact d₁0 rfl), have dβ‚‚0' : (dβ‚‚:β„€) β‰  0 := int.coe_nat_ne_zero.2 (Ξ» e, by rw e at dβ‚‚0; exact dβ‚‚0 rfl), rw [num_denom', num_denom', add_def d₁0' dβ‚‚0'], suffices : (n₁ * (dβ‚‚ * (d₂⁻¹ * d₁⁻¹)) + nβ‚‚ * (d₁ * d₂⁻¹) * d₁⁻¹ : Ξ±) = n₁ * d₁⁻¹ + nβ‚‚ * d₂⁻¹, { rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero, cast_mk_of_ne_zero], { simpa [division_def, left_distrib, right_distrib, mul_inv_revβ‚€, d₁0, dβ‚‚0, mul_assoc] }, all_goals {simp [d₁0, dβ‚‚0]} }, rw [← mul_assoc (dβ‚‚:Ξ±), mul_inv_cancel dβ‚‚0, one_mul, (nat.cast_commute _ _).eq], simp [d₁0, mul_assoc] end @[simp, norm_cast] theorem cast_neg : βˆ€ n, ((-n : β„š) : Ξ±) = -n | ⟨n, d, h, c⟩ := show (↑-n / d : Ξ±) = -(n / d), by rw [div_eq_mul_inv, div_eq_mul_inv, int.cast_neg, neg_mul_eq_neg_mul] @[norm_cast] theorem cast_sub_of_ne_zero {m n : β„š} (m0 : (m.denom : Ξ±) β‰  0) (n0 : (n.denom : Ξ±) β‰  0) : ((m - n : β„š) : Ξ±) = m - n := have ((-n).denom : Ξ±) β‰  0, by cases n; exact n0, by simp [sub_eq_add_neg, (cast_add_of_ne_zero m0 this)] @[norm_cast] theorem cast_mul_of_ne_zero : βˆ€ {m n : β„š}, (m.denom : Ξ±) β‰  0 β†’ (n.denom : Ξ±) β‰  0 β†’ ((m * n : β„š) : Ξ±) = m * n | ⟨n₁, d₁, h₁, cβ‚βŸ© ⟨nβ‚‚, dβ‚‚, hβ‚‚, cβ‚‚βŸ© := Ξ» (d₁0 : (d₁:Ξ±) β‰  0) (dβ‚‚0 : (dβ‚‚:Ξ±) β‰  0), begin have d₁0' : (d₁:β„€) β‰  0 := int.coe_nat_ne_zero.2 (Ξ» e, by rw e at d₁0; exact d₁0 rfl), have dβ‚‚0' : (dβ‚‚:β„€) β‰  0 := int.coe_nat_ne_zero.2 (Ξ» e, by rw e at dβ‚‚0; exact dβ‚‚0 rfl), rw [num_denom', num_denom', mul_def d₁0' dβ‚‚0'], suffices : (n₁ * ((nβ‚‚ * d₂⁻¹) * d₁⁻¹) : Ξ±) = n₁ * (d₁⁻¹ * (nβ‚‚ * d₂⁻¹)), { rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero, cast_mk_of_ne_zero], { simpa [division_def, mul_inv_revβ‚€, d₁0, dβ‚‚0, mul_assoc] }, all_goals {simp [d₁0, dβ‚‚0]} }, rw [(d₁.commute_cast (_:Ξ±)).inv_rightβ‚€.eq] end @[simp] theorem cast_inv_nat (n : β„•) : ((n⁻¹ : β„š) : Ξ±) = n⁻¹ := begin cases n, { simp }, simp_rw [coe_nat_eq_mk, inv_def, mk, mk_nat, dif_neg n.succ_ne_zero, mk_pnat], simp [cast_def] end @[simp] theorem cast_inv_int (n : β„€) : ((n⁻¹ : β„š) : Ξ±) = n⁻¹ := begin cases n, { exact cast_inv_nat _ }, { simp only [int.cast_neg_succ_of_nat, ← nat.cast_succ, cast_neg, inv_neg, cast_inv_nat] } end @[norm_cast] theorem cast_inv_of_ne_zero : βˆ€ {n : β„š}, (n.num : Ξ±) β‰  0 β†’ (n.denom : Ξ±) β‰  0 β†’ ((n⁻¹ : β„š) : Ξ±) = n⁻¹ | ⟨n, d, h, c⟩ := Ξ» (n0 : (n:Ξ±) β‰  0) (d0 : (d:Ξ±) β‰  0), begin have n0' : (n:β„€) β‰  0 := Ξ» e, by rw e at n0; exact n0 rfl, have d0' : (d:β„€) β‰  0 := int.coe_nat_ne_zero.2 (Ξ» e, by rw e at d0; exact d0 rfl), rw [num_denom', inv_def], rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero, inv_div]; simp [n0, d0] end @[norm_cast] theorem cast_div_of_ne_zero {m n : β„š} (md : (m.denom : Ξ±) β‰  0) (nn : (n.num : Ξ±) β‰  0) (nd : (n.denom : Ξ±) β‰  0) : ((m / n : β„š) : Ξ±) = m / n := have (n⁻¹.denom : β„€) ∣ n.num, by conv in n⁻¹.denom { rw [←(@num_denom n), inv_def] }; apply denom_dvd, have (n⁻¹.denom : Ξ±) = 0 β†’ (n.num : Ξ±) = 0, from Ξ» h, let ⟨k, e⟩ := this in by have := congr_arg (coe : β„€ β†’ Ξ±) e; rwa [int.cast_mul, int.cast_coe_nat, h, zero_mul] at this, by rw [division_def, cast_mul_of_ne_zero md (mt this nn), cast_inv_of_ne_zero nn nd, division_def] @[simp, norm_cast] theorem cast_inj [char_zero Ξ±] : βˆ€ {m n : β„š}, (m : Ξ±) = n ↔ m = n | ⟨n₁, d₁, h₁, cβ‚βŸ© ⟨nβ‚‚, dβ‚‚, hβ‚‚, cβ‚‚βŸ© := begin refine ⟨λ h, _, congr_arg _⟩, have d₁0 : d₁ β‰  0 := ne_of_gt h₁, have dβ‚‚0 : dβ‚‚ β‰  0 := ne_of_gt hβ‚‚, have d₁a : (d₁:Ξ±) β‰  0 := nat.cast_ne_zero.2 d₁0, have dβ‚‚a : (dβ‚‚:Ξ±) β‰  0 := nat.cast_ne_zero.2 dβ‚‚0, rw [num_denom', num_denom'] at h ⊒, rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero] at h; simp [d₁0, dβ‚‚0] at h ⊒, rwa [eq_div_iff_mul_eq dβ‚‚a, division_def, mul_assoc, (d₁.cast_commute (dβ‚‚:Ξ±)).inv_leftβ‚€.eq, ← mul_assoc, ← division_def, eq_comm, eq_div_iff_mul_eq d₁a, eq_comm, ← int.cast_coe_nat, ← int.cast_mul, ← int.cast_coe_nat, ← int.cast_mul, int.cast_inj, ← mk_eq (int.coe_nat_ne_zero.2 d₁0) (int.coe_nat_ne_zero.2 dβ‚‚0)] at h end theorem cast_injective [char_zero Ξ±] : function.injective (coe : β„š β†’ Ξ±) | m n := cast_inj.1 @[simp] theorem cast_eq_zero [char_zero Ξ±] {n : β„š} : (n : Ξ±) = 0 ↔ n = 0 := by rw [← cast_zero, cast_inj] theorem cast_ne_zero [char_zero Ξ±] {n : β„š} : (n : Ξ±) β‰  0 ↔ n β‰  0 := not_congr cast_eq_zero @[simp, norm_cast] theorem cast_add [char_zero Ξ±] (m n) : ((m + n : β„š) : Ξ±) = m + n := cast_add_of_ne_zero (nat.cast_ne_zero.2 $ ne_of_gt m.pos) (nat.cast_ne_zero.2 $ ne_of_gt n.pos) @[simp, norm_cast] theorem cast_sub [char_zero Ξ±] (m n) : ((m - n : β„š) : Ξ±) = m - n := cast_sub_of_ne_zero (nat.cast_ne_zero.2 $ ne_of_gt m.pos) (nat.cast_ne_zero.2 $ ne_of_gt n.pos) @[simp, norm_cast] theorem cast_mul [char_zero Ξ±] (m n) : ((m * n : β„š) : Ξ±) = m * n := cast_mul_of_ne_zero (nat.cast_ne_zero.2 $ ne_of_gt m.pos) (nat.cast_ne_zero.2 $ ne_of_gt n.pos) @[simp, norm_cast] theorem cast_bit0 [char_zero Ξ±] (n : β„š) : ((bit0 n : β„š) : Ξ±) = bit0 n := cast_add _ _ @[simp, norm_cast] theorem cast_bit1 [char_zero Ξ±] (n : β„š) : ((bit1 n : β„š) : Ξ±) = bit1 n := by rw [bit1, cast_add, cast_one, cast_bit0]; refl variable (Ξ±) /-- Coercion `β„š β†’ Ξ±` as a `ring_hom`. -/ def cast_hom [char_zero Ξ±] : β„š β†’+* Ξ± := ⟨coe, cast_one, cast_mul, cast_zero, cast_add⟩ variable {Ξ±} @[simp] lemma coe_cast_hom [char_zero Ξ±] : ⇑(cast_hom Ξ±) = coe := rfl @[simp, norm_cast] theorem cast_inv [char_zero Ξ±] (n) : ((n⁻¹ : β„š) : Ξ±) = n⁻¹ := (cast_hom Ξ±).map_inv _ @[simp, norm_cast] theorem cast_div [char_zero Ξ±] (m n) : ((m / n : β„š) : Ξ±) = m / n := (cast_hom Ξ±).map_div _ _ @[norm_cast] theorem cast_mk [char_zero Ξ±] (a b : β„€) : ((a /. b) : Ξ±) = a / b := by simp only [mk_eq_div, cast_div, cast_coe_int] @[simp, norm_cast] theorem cast_pow [char_zero Ξ±] (q) (k : β„•) : ((q ^ k : β„š) : Ξ±) = q ^ k := (cast_hom Ξ±).map_pow q k end with_div_ring @[simp, norm_cast] theorem cast_nonneg [linear_ordered_field Ξ±] : βˆ€ {n : β„š}, 0 ≀ (n : Ξ±) ↔ 0 ≀ n | ⟨n, d, h, c⟩ := by { rw [num_denom', cast_mk, mk_eq_div, div_nonneg_iff, div_nonneg_iff], norm_cast } @[simp, norm_cast] theorem cast_le [linear_ordered_field Ξ±] {m n : β„š} : (m : Ξ±) ≀ n ↔ m ≀ n := by rw [← sub_nonneg, ← cast_sub, cast_nonneg, sub_nonneg] @[simp, norm_cast] theorem cast_lt [linear_ordered_field Ξ±] {m n : β„š} : (m : Ξ±) < n ↔ m < n := by simpa [-cast_le] using not_congr (@cast_le Ξ± _ n m) @[simp] theorem cast_nonpos [linear_ordered_field Ξ±] {n : β„š} : (n : Ξ±) ≀ 0 ↔ n ≀ 0 := by rw [← cast_zero, cast_le] @[simp] theorem cast_pos [linear_ordered_field Ξ±] {n : β„š} : (0 : Ξ±) < n ↔ 0 < n := by rw [← cast_zero, cast_lt] @[simp] theorem cast_lt_zero [linear_ordered_field Ξ±] {n : β„š} : (n : Ξ±) < 0 ↔ n < 0 := by rw [← cast_zero, cast_lt] @[simp, norm_cast] theorem cast_id : βˆ€ n : β„š, ↑n = n | ⟨n, d, h, c⟩ := by rw [num_denom', cast_mk, mk_eq_div] @[simp, norm_cast] theorem cast_min [linear_ordered_field Ξ±] {a b : β„š} : (↑(min a b) : Ξ±) = min a b := by by_cases a ≀ b; simp [h, min_def] @[simp, norm_cast] theorem cast_max [linear_ordered_field Ξ±] {a b : β„š} : (↑(max a b) : Ξ±) = max a b := by by_cases b ≀ a; simp [h, max_def] @[simp, norm_cast] theorem cast_abs [linear_ordered_field Ξ±] {q : β„š} : ((|q| : β„š) : Ξ±) = |q| := by simp [abs_eq_max_neg] end rat open rat ring_hom lemma ring_hom.eq_rat_cast {k} [division_ring k] (f : β„š β†’+* k) (r : β„š) : f r = r := calc f r = f (r.1 / r.2) : by rw [← int.cast_coe_nat, ← mk_eq_div, num_denom] ... = f r.1 / f r.2 : f.map_div _ _ ... = r.1 / r.2 : by rw [map_nat_cast, map_int_cast] -- This seems to be true for a `[char_p k]` too because `k'` must have the same characteristic -- but the proof would be much longer lemma ring_hom.map_rat_cast {k k'} [division_ring k] [char_zero k] [division_ring k'] (f : k β†’+* k') (r : β„š) : f r = r := (f.comp (cast_hom k)).eq_rat_cast r lemma ring_hom.ext_rat {R : Type*} [semiring R] (f g : β„š β†’+* R) : f = g := begin ext r, refine rat.num_denom_cases_on' r _, intros a b b0, let Ο† : β„€ β†’+* R := f.comp (int.cast_ring_hom β„š), let ψ : β„€ β†’+* R := g.comp (int.cast_ring_hom β„š), rw [rat.mk_eq_div, int.cast_coe_nat], have b0' : (b:β„š) β‰  0 := nat.cast_ne_zero.2 b0, have : βˆ€ n : β„€, f n = g n := Ξ» n, show Ο† n = ψ n, by rw [Ο†.ext_int ψ], calc f (a * b⁻¹) = f a * f b⁻¹ * (g (b:β„€) * g b⁻¹) : by rw [int.cast_coe_nat, ← g.map_mul, mul_inv_cancel b0', g.map_one, mul_one, f.map_mul] ... = g a * f b⁻¹ * (f (b:β„€) * g b⁻¹) : by rw [this a, ← this b] ... = g (a * b⁻¹) : by rw [int.cast_coe_nat, mul_assoc, ← mul_assoc (f b⁻¹), ← f.map_mul, inv_mul_cancel b0', f.map_one, one_mul, g.map_mul] end instance rat.subsingleton_ring_hom {R : Type*} [semiring R] : subsingleton (β„š β†’+* R) := ⟨ring_hom.ext_rat⟩ namespace monoid_with_zero_hom variables {M : Type*} [group_with_zero M] /-- If `f` and `g` agree on the integers then they are equal `Ο†`. See note [partially-applied ext lemmas] for why `comp` is used here. -/ @[ext] theorem ext_rat {f g : β„š β†’*β‚€ M} (same_on_int : f.comp (int.cast_ring_hom β„š).to_monoid_with_zero_hom = g.comp (int.cast_ring_hom β„š).to_monoid_with_zero_hom) : f = g := begin have same_on_int' : βˆ€ k : β„€, f k = g k := congr_fun same_on_int, ext x, rw [← @rat.num_denom x, rat.mk_eq_div, f.map_div, g.map_div, same_on_int' x.num, same_on_int' x.denom], end /-- Positive integer values of a morphism `Ο†` and its value on `-1` completely determine `Ο†`. -/ theorem ext_rat_on_pnat {f g : β„š β†’*β‚€ M} (same_on_neg_one : f (-1) = g (-1)) (same_on_pnat : βˆ€ n : β„•, 0 < n β†’ f n = g n) : f = g := ext_rat $ ext_int' (by simpa) β€Ή_β€Ί end monoid_with_zero_hom
93140dc7d3e25a5fbe55b06b1785149523422c5b
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/topology/metric_space/baire.lean
b364d06fb2005247a6eba4c52555b3eb2897f416
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
17,152
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 analysis.specific_limits import order.filter.countable_Inter /-! # Baire theorem In a complete metric space, a countable intersection of dense open subsets is dense. The good concept underlying the theorem is that of a GΞ΄ set, i.e., a countable intersection of open sets. Then Baire theorem can also be formulated as the fact that a countable intersection of dense GΞ΄ sets is a dense GΞ΄ set. We prove Baire theorem, giving several different formulations that can be handy. We also prove the important consequence that, if the space is covered by a countable union of closed sets, then the union of their interiors is dense. The names of the theorems do not contain the string "Baire", but are instead built from the form of the statement. "Baire" is however in the docstring of all the theorems, to facilitate grep searches. We also define the filter `residual Ξ±` generated by dense `GΞ΄` sets and prove that this filter has the countable intersection property. -/ noncomputable theory open_locale classical topological_space filter open filter encodable set variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {ΞΉ : Type*} section is_GΞ΄ variable [topological_space Ξ±] /-- A GΞ΄ set is a countable intersection of open sets. -/ def is_GΞ΄ (s : set Ξ±) : Prop := βˆƒT : set (set Ξ±), (βˆ€t ∈ T, is_open t) ∧ countable T ∧ s = (β‹‚β‚€ T) /-- An open set is a GΞ΄ set. -/ lemma is_open.is_GΞ΄ {s : set Ξ±} (h : is_open s) : is_GΞ΄ s := ⟨{s}, by simp [h], countable_singleton _, (set.sInter_singleton _).symm⟩ lemma is_GΞ΄_univ : is_GΞ΄ (univ : set Ξ±) := is_open_univ.is_GΞ΄ lemma is_GΞ΄_bInter_of_open {I : set ΞΉ} (hI : countable I) {f : ΞΉ β†’ set Ξ±} (hf : βˆ€i ∈ I, is_open (f i)) : is_GΞ΄ (β‹‚i∈I, f i) := ⟨f '' I, by rwa ball_image_iff, hI.image _, by rw sInter_image⟩ lemma is_GΞ΄_Inter_of_open [encodable ΞΉ] {f : ΞΉ β†’ set Ξ±} (hf : βˆ€i, is_open (f i)) : is_GΞ΄ (β‹‚i, f i) := ⟨range f, by rwa forall_range_iff, countable_range _, by rw sInter_range⟩ /-- A countable intersection of GΞ΄ sets is a GΞ΄ set. -/ lemma is_GΞ΄_sInter {S : set (set Ξ±)} (h : βˆ€s∈S, is_GΞ΄ s) (hS : countable S) : is_GΞ΄ (β‹‚β‚€ S) := begin choose T hT using h, refine ⟨_, _, _, (sInter_bUnion (Ξ» s hs, (hT s hs).2.2)).symm⟩, { simp only [mem_Union], rintros t ⟨s, hs, tTs⟩, exact (hT s hs).1 t tTs }, { exact hS.bUnion (Ξ»s hs, (hT s hs).2.1) }, end lemma is_GΞ΄_Inter [encodable ΞΉ] {s : ΞΉ β†’ set Ξ±} (hs : βˆ€ i, is_GΞ΄ (s i)) : is_GΞ΄ (β‹‚ i, s i) := is_GΞ΄_sInter (forall_range_iff.2 hs) $ countable_range s lemma is_GΞ΄_bInter {s : set ΞΉ} (hs : countable s) {t : Ξ  i ∈ s, set Ξ±} (ht : βˆ€ i ∈ s, is_GΞ΄ (t i β€Ή_β€Ί)) : is_GΞ΄ (β‹‚ i ∈ s, t i β€Ή_β€Ί) := begin rw [bInter_eq_Inter], haveI := hs.to_encodable, exact is_GΞ΄_Inter (Ξ» x, ht x x.2) end lemma is_GΞ΄.inter {s t : set Ξ±} (hs : is_GΞ΄ s) (ht : is_GΞ΄ t) : is_GΞ΄ (s ∩ t) := by { rw inter_eq_Inter, exact is_GΞ΄_Inter (bool.forall_bool.2 ⟨ht, hs⟩) } /-- The union of two GΞ΄ sets is a GΞ΄ set. -/ lemma is_GΞ΄.union {s t : set Ξ±} (hs : is_GΞ΄ s) (ht : is_GΞ΄ t) : is_GΞ΄ (s βˆͺ t) := begin rcases hs with ⟨S, Sopen, Scount, rfl⟩, rcases ht with ⟨T, Topen, Tcount, rfl⟩, rw [sInter_union_sInter], apply is_GΞ΄_bInter_of_open (countable_prod Scount Tcount), rintros ⟨a, b⟩ hab, exact is_open_union (Sopen a hab.1) (Topen b hab.2) end end is_GΞ΄ /-- A set `s` is called *residual* if it includes a dense `GΞ΄` set. If `Ξ±` is a Baire space (e.g., a complete metric space), then residual sets form a filter, see `mem_residual`. For technical reasons we define the filter `residual` in any topological space but in a non-Baire space it is not useful because it may contain some non-residual sets. -/ def residual (Ξ± : Type*) [topological_space Ξ±] : filter Ξ± := β¨… t (ht : is_GΞ΄ t) (ht' : closure t = univ), π“Ÿ t section Baire_theorem open emetric ennreal variables [emetric_space Ξ±] [complete_space Ξ±] /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here when the source space is β„• (and subsumed below by `dense_Inter_of_open` working with any encodable source space). -/ theorem dense_Inter_of_open_nat {f : β„• β†’ set Ξ±} (ho : βˆ€n, is_open (f n)) (hd : βˆ€n, closure (f n) = univ) : closure (β‹‚n, f n) = univ := begin let B : β„• β†’ ennreal := Ξ»n, 1/2^n, have Bpos : βˆ€n, 0 < B n, { intro n, simp only [B, div_def, one_mul, ennreal.inv_pos], exact pow_ne_top two_ne_top }, /- Translate the density assumption into two functions `center` and `radius` associating to any n, x, Ξ΄, Ξ΄pos a center and a positive radius such that `closed_ball center radius` is included both in `f n` and in `closed_ball x Ξ΄`. We can also require `radius ≀ (1/2)^(n+1), to ensure we get a Cauchy sequence later. -/ have : βˆ€n x Ξ΄, βˆƒy r, Ξ΄ > 0 β†’ (r > 0 ∧ r ≀ B (n+1) ∧ closed_ball y r βŠ† (closed_ball x Ξ΄) ∩ f n), { assume n x Ξ΄, by_cases Ξ΄pos : Ξ΄ > 0, { have : x ∈ closure (f n) := by simpa only [(hd n).symm] using mem_univ x, rcases emetric.mem_closure_iff.1 this (Ξ΄/2) (ennreal.half_pos Ξ΄pos) with ⟨y, ys, xy⟩, rw edist_comm at xy, obtain ⟨r, rpos, hr⟩ : βˆƒ r > 0, closed_ball y r βŠ† f n := nhds_basis_closed_eball.mem_iff.1 (is_open_iff_mem_nhds.1 (ho n) y ys), refine ⟨y, min (min (Ξ΄/2) r) (B (n+1)), Ξ»_, ⟨_, _, Ξ»z hz, ⟨_, _⟩⟩⟩, show 0 < min (min (Ξ΄ / 2) r) (B (n+1)), from lt_min (lt_min (ennreal.half_pos Ξ΄pos) rpos) (Bpos (n+1)), show min (min (Ξ΄ / 2) r) (B (n+1)) ≀ B (n+1), from min_le_right _ _, show z ∈ closed_ball x Ξ΄, from calc edist z x ≀ edist z y + edist y x : edist_triangle _ _ _ ... ≀ (min (min (Ξ΄ / 2) r) (B (n+1))) + (Ξ΄/2) : add_le_add hz (le_of_lt xy) ... ≀ Ξ΄/2 + Ξ΄/2 : add_le_add (le_trans (min_le_left _ _) (min_le_left _ _)) (le_refl _) ... = Ξ΄ : ennreal.add_halves Ξ΄, show z ∈ f n, from hr (calc edist z y ≀ min (min (Ξ΄ / 2) r) (B (n+1)) : hz ... ≀ r : le_trans (min_le_left _ _) (min_le_right _ _)) }, { use [x, 0] }}, choose center radius H using this, refine subset.antisymm (subset_univ _) (Ξ»x hx, _), refine (mem_closure_iff_nhds_basis nhds_basis_closed_eball).2 (Ξ» Ξ΅ Ξ΅pos, _), /- Ξ΅ is positive. We have to find a point in the ball of radius Ξ΅ around x belonging to all `f n`. For this, we construct inductively a sequence `F n = (c n, r n)` such that the closed ball `closed_ball (c n) (r n)` is included in the previous ball and in `f n`, and such that `r n` is small enough to ensure that `c n` is a Cauchy sequence. Then `c n` converges to a limit which belongs to all the `f n`. -/ let F : β„• β†’ (Ξ± Γ— ennreal) := Ξ»n, nat.rec_on n (prod.mk x (min Ξ΅ (B 0))) (Ξ»n p, prod.mk (center n p.1 p.2) (radius n p.1 p.2)), let c : β„• β†’ Ξ± := Ξ»n, (F n).1, let r : β„• β†’ ennreal := Ξ»n, (F n).2, have rpos : βˆ€n, r n > 0, { assume n, induction n with n hn, exact lt_min Ξ΅pos (Bpos 0), exact (H n (c n) (r n) hn).1 }, have rB : βˆ€n, r n ≀ B n, { assume n, induction n with n hn, exact min_le_right _ _, exact (H n (c n) (r n) (rpos n)).2.1 }, have incl : βˆ€n, closed_ball (c (n+1)) (r (n+1)) βŠ† (closed_ball (c n) (r n)) ∩ (f n) := Ξ»n, (H n (c n) (r n) (rpos n)).2.2, have cdist : βˆ€n, edist (c n) (c (n+1)) ≀ B n, { assume n, rw edist_comm, have A : c (n+1) ∈ closed_ball (c (n+1)) (r (n+1)) := mem_closed_ball_self, have I := calc closed_ball (c (n+1)) (r (n+1)) βŠ† closed_ball (c n) (r n) : subset.trans (incl n) (inter_subset_left _ _) ... βŠ† closed_ball (c n) (B n) : closed_ball_subset_closed_ball (rB n), exact I A }, have : cauchy_seq c := cauchy_seq_of_edist_le_geometric_two _ one_ne_top cdist, -- as the sequence `c n` is Cauchy in a complete space, it converges to a limit `y`. rcases cauchy_seq_tendsto_of_complete this with ⟨y, ylim⟩, -- this point `y` will be the desired point. We will check that it belongs to all -- `f n` and to `ball x Ξ΅`. use y, simp only [exists_prop, set.mem_Inter], have I : βˆ€n, βˆ€m β‰₯ n, closed_ball (c m) (r m) βŠ† closed_ball (c n) (r n), { assume n, refine nat.le_induction _ (Ξ»m hnm h, _), { exact subset.refl _ }, { exact subset.trans (incl m) (subset.trans (inter_subset_left _ _) h) }}, have yball : βˆ€n, y ∈ closed_ball (c n) (r n), { assume n, refine mem_of_closed_of_tendsto (by simp) ylim is_closed_ball _, simp only [filter.mem_at_top_sets, nonempty_of_inhabited, set.mem_preimage], exact ⟨n, Ξ»m hm, I n m hm mem_closed_ball_self⟩ }, split, show βˆ€n, y ∈ f n, { assume n, have : closed_ball (c (n+1)) (r (n+1)) βŠ† f n := subset.trans (incl n) (inter_subset_right _ _), exact this (yball (n+1)) }, show edist y x ≀ Ξ΅, from le_trans (yball 0) (min_le_left _ _), end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with β‹‚β‚€. -/ theorem dense_sInter_of_open {S : set (set Ξ±)} (ho : βˆ€s∈S, is_open s) (hS : countable S) (hd : βˆ€s∈S, closure s = univ) : closure (β‹‚β‚€S) = univ := begin cases S.eq_empty_or_nonempty with h h, { simp [h] }, { rcases hS.exists_surjective h with ⟨f, hf⟩, have F : βˆ€n, f n ∈ S := Ξ»n, by rw hf; exact mem_range_self _, rw [hf, sInter_range], exact dense_Inter_of_open_nat (Ξ»n, ho _ (F n)) (Ξ»n, hd _ (F n)) } end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bInter_of_open {S : set Ξ²} {f : Ξ² β†’ set Ξ±} (ho : βˆ€s∈S, is_open (f s)) (hS : countable S) (hd : βˆ€s∈S, closure (f s) = univ) : closure (β‹‚s∈S, f s) = univ := begin rw ← sInter_image, apply dense_sInter_of_open, { rwa ball_image_iff }, { exact hS.image _ }, { rwa ball_image_iff } end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Inter_of_open [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (ho : βˆ€s, is_open (f s)) (hd : βˆ€s, closure (f s) = univ) : closure (β‹‚s, f s) = univ := begin rw ← sInter_range, apply dense_sInter_of_open, { rwa forall_range_iff }, { exact countable_range _ }, { rwa forall_range_iff } end /-- Baire theorem: a countable intersection of dense GΞ΄ sets is dense. Formulated here with β‹‚β‚€. -/ theorem dense_sInter_of_GΞ΄ {S : set (set Ξ±)} (ho : βˆ€s∈S, is_GΞ΄ s) (hS : countable S) (hd : βˆ€s∈S, closure s = univ) : closure (β‹‚β‚€S) = univ := begin -- the result follows from the result for a countable intersection of dense open sets, -- by rewriting each set as a countable intersection of open sets, which are of course dense. choose T hT using ho, have : β‹‚β‚€ S = β‹‚β‚€ (⋃s∈S, T s β€Ή_β€Ί) := (sInter_bUnion (Ξ»s hs, (hT s hs).2.2)).symm, rw this, refine dense_sInter_of_open _ (hS.bUnion (Ξ»s hs, (hT s hs).2.1)) _; simp only [set.mem_Union, exists_prop]; rintro t ⟨s, hs, tTs⟩, show is_open t, { exact (hT s hs).1 t tTs }, show closure t = univ, { apply eq_univ_of_univ_subset, rw [← hd s hs, (hT s hs).2.2], exact closure_mono (sInter_subset_of_mem tTs) } end /-- Baire theorem: a countable intersection of dense GΞ΄ sets is dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Inter_of_GΞ΄ [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (ho : βˆ€s, is_GΞ΄ (f s)) (hd : βˆ€s, closure (f s) = univ) : closure (β‹‚s, f s) = univ := begin rw ← sInter_range, exact dense_sInter_of_GΞ΄ (forall_range_iff.2 β€Ή_β€Ί) (countable_range _) (forall_range_iff.2 β€Ή_β€Ί) end /-- Baire theorem: a countable intersection of dense GΞ΄ sets is dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bInter_of_GΞ΄ {S : set Ξ²} {f : Ξ  x ∈ S, set Ξ±} (ho : βˆ€s∈S, is_GΞ΄ (f s β€Ή_β€Ί)) (hS : countable S) (hd : βˆ€s∈S, closure (f s β€Ή_β€Ί) = univ) : closure (β‹‚s∈S, f s β€Ή_β€Ί) = univ := begin rw bInter_eq_Inter, haveI := hS.to_encodable, exact dense_Inter_of_GΞ΄ (Ξ» s, ho s s.2) (Ξ» s, hd s s.2) end /-- Baire theorem: the intersection of two dense GΞ΄ sets is dense. -/ theorem dense_inter_of_GΞ΄ {s t : set Ξ±} (hs : is_GΞ΄ s) (ht : is_GΞ΄ t) (hsc : closure s = univ) (htc : closure t = univ) : closure (s ∩ t) = univ := begin rw [inter_eq_Inter], apply dense_Inter_of_GΞ΄; simp [bool.forall_bool, *] end /-- A property holds on a residual (comeagre) set if and only if it holds on some dense `GΞ΄` set. -/ lemma eventually_residual {p : Ξ± β†’ Prop} : (βˆ€αΆ  x in residual Ξ±, p x) ↔ βˆƒ (t : set Ξ±), is_GΞ΄ t ∧ closure t = univ ∧ βˆ€ x ∈ t, p x := calc (βˆ€αΆ  x in residual Ξ±, p x) ↔ βˆ€αΆ  x in β¨… (t : set Ξ±) (ht : is_GΞ΄ t ∧ closure t = univ), π“Ÿ t, p x : by simp only [residual, infi_and] ... ↔ βˆƒ (t : set Ξ±) (ht : is_GΞ΄ t ∧ closure t = univ), βˆ€αΆ  x in π“Ÿ t, p x : mem_binfi (Ξ» t₁ h₁ tβ‚‚ hβ‚‚, ⟨t₁ ∩ tβ‚‚, ⟨h₁.1.inter hβ‚‚.1, dense_inter_of_GΞ΄ h₁.1 hβ‚‚.1 h₁.2 hβ‚‚.2⟩, by simp⟩) ⟨univ, is_GΞ΄_univ, closure_univ⟩ ... ↔ _ : by simp [and_assoc] /-- A set is residual (comeagre) if and only if it includes a dense `GΞ΄` set. -/ lemma mem_residual {s : set Ξ±} : s ∈ residual Ξ± ↔ βˆƒ t βŠ† s, is_GΞ΄ t ∧ closure t = univ := (@eventually_residual Ξ± _ _ (Ξ» x, x ∈ s)).trans $ exists_congr $ Ξ» t, by rw [exists_prop, and_comm (t βŠ† s), subset_def, and_assoc] instance : countable_Inter_filter (residual Ξ±) := ⟨begin intros S hSc hS, simp only [mem_residual] at *, choose T hTs hT using hS, refine βŸ¨β‹‚ s ∈ S, T s β€Ή_β€Ί, _, _, _⟩, { rw [sInter_eq_bInter], exact Inter_subset_Inter (Ξ» s, Inter_subset_Inter $ hTs s) }, { exact is_GΞ΄_bInter hSc (Ξ» s hs, (hT s hs).1) }, { exact dense_bInter_of_GΞ΄ (Ξ» s hs, (hT s hs).1) hSc (Ξ» s hs, (hT s hs).2) } end⟩ /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bUnion_interior_of_closed {S : set Ξ²} {f : Ξ² β†’ set Ξ±} (hc : βˆ€s∈S, is_closed (f s)) (hS : countable S) (hU : (⋃s∈S, f s) = univ) : closure (⋃s∈S, interior (f s)) = univ := begin let g := Ξ»s, (frontier (f s))ᢜ, have clos_g : closure (β‹‚s∈S, g s) = univ, { refine dense_bInter_of_open (Ξ»s hs, _) hS (Ξ»s hs, _), show is_open (g s), from is_open_compl_iff.2 is_closed_frontier, show closure (g s) = univ, { apply subset.antisymm (subset_univ _), simp [interior_frontier (hc s hs)] }}, have : (β‹‚s∈S, g s) βŠ† (⋃s∈S, interior (f s)), { assume x hx, have : x ∈ ⋃s∈S, f s, { have := mem_univ x, rwa ← hU at this }, rcases mem_bUnion_iff.1 this with ⟨s, hs, xs⟩, have : x ∈ g s := mem_bInter_iff.1 hx s hs, have : x ∈ interior (f s), { have : x ∈ f s \ (frontier (f s)) := mem_inter xs this, simpa [frontier, xs, (hc s hs).closure_eq] using this }, exact mem_bUnion_iff.2 ⟨s, ⟨hs, this⟩⟩ }, have := closure_mono this, rw clos_g at this, exact subset.antisymm (subset_univ _) this end /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with ⋃₀. -/ theorem dense_sUnion_interior_of_closed {S : set (set Ξ±)} (hc : βˆ€s∈S, is_closed s) (hS : countable S) (hU : (⋃₀ S) = univ) : closure (⋃s∈S, interior s) = univ := by rw sUnion_eq_bUnion at hU; exact dense_bUnion_interior_of_closed hc hS hU /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Union_interior_of_closed [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hc : βˆ€s, is_closed (f s)) (hU : (⋃s, f s) = univ) : closure (⋃s, interior (f s)) = univ := begin rw ← bUnion_univ, apply dense_bUnion_interior_of_closed, { simp [hc] }, { apply countable_encodable }, { rwa ← bUnion_univ at hU } end /-- One of the most useful consequences of Baire theorem: if a countable union of closed sets covers the space, then one of the sets has nonempty interior. -/ theorem nonempty_interior_of_Union_of_closed [nonempty Ξ±] [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hc : βˆ€s, is_closed (f s)) (hU : (⋃s, f s) = univ) : βˆƒs, (interior $ f s).nonempty := begin by_contradiction h, simp only [not_exists, not_nonempty_iff_eq_empty] at h, have := calc βˆ… = closure (⋃s, interior (f s)) : by simp [h] ... = univ : dense_Union_interior_of_closed hc hU, exact univ_nonempty.ne_empty this.symm end end Baire_theorem
f7542e474077170c83f20eb9a1c5c8725f401cea
c777c32c8e484e195053731103c5e52af26a25d1
/src/topology/subset_properties.lean
d37fdbf1062bcc44cb4d0bb2eb5b2a0714235c18
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
89,273
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, Yury Kudryashov -/ import order.filter.pi import topology.bases import data.finset.order import data.set.accumulate import data.set.bool_indicator import topology.bornology.basic import topology.locally_finite import order.minimal /-! # Properties of subsets of topological spaces > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define various properties of subsets of a topological space, and some classes on topological spaces. ## Main definitions We define the following properties for sets in a topological space: * `is_compact`: each open cover has a finite subcover. This is defined in mathlib using filters. The main property of a compact set is `is_compact.elim_finite_subcover`. * `is_clopen`: a set that is both open and closed. * `is_irreducible`: a nonempty set that has contains no non-trivial pair of disjoint opens. See also the section below in the module doc. For each of these definitions (except for `is_clopen`), we also have a class stating that the whole space satisfies that property: `compact_space`, `irreducible_space` Furthermore, we have three more classes: * `locally_compact_space`: for every point `x`, every open neighborhood of `x` contains a compact neighborhood of `x`. The definition is formulated in terms of the neighborhood filter. * `sigma_compact_space`: a space that is the union of a countably many compact subspaces; * `noncompact_space`: a space that is not a compact space. ## On the definition of irreducible and connected sets/spaces In informal mathematics, irreducible spaces are assumed to be nonempty. We formalise the predicate without that assumption as `is_preirreducible`. In other words, the only difference is whether the empty space counts as irreducible. There are good reasons to consider the empty space to be β€œtoo simple to be simple” See also https://ncatlab.org/nlab/show/too+simple+to+be+simple, and in particular https://ncatlab.org/nlab/show/too+simple+to+be+simple#relationship_to_biased_definitions. -/ open set filter classical topological_space open_locale classical topology filter universes u v variables {Ξ± : Type u} {Ξ² : Type v} {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} variables [topological_space Ξ±] [topological_space Ξ²] {s t : set Ξ±} /- compact sets -/ section compact /-- A set `s` is compact if for every nontrivial filter `f` that contains `s`, there exists `a ∈ s` such that every set of `f` meets every neighborhood of `a`. -/ def is_compact (s : set Ξ±) := βˆ€ ⦃f⦄ [ne_bot f], f ≀ π“Ÿ s β†’ βˆƒ a ∈ s, cluster_pt a f /-- The complement to a compact set belongs to a filter `f` if it belongs to each filter `𝓝 a βŠ“ f`, `a ∈ s`. -/ lemma is_compact.compl_mem_sets (hs : is_compact s) {f : filter Ξ±} (hf : βˆ€ a ∈ s, sᢜ ∈ 𝓝 a βŠ“ f) : sᢜ ∈ f := begin contrapose! hf, simp only [not_mem_iff_inf_principal_compl, compl_compl, inf_assoc, ← exists_prop] at hf ⊒, exact @hs _ hf inf_le_right end /-- The complement to a compact set belongs to a filter `f` if each `a ∈ s` has a neighborhood `t` within `s` such that `tᢜ` belongs to `f`. -/ lemma is_compact.compl_mem_sets_of_nhds_within (hs : is_compact s) {f : filter Ξ±} (hf : βˆ€ a ∈ s, βˆƒ t ∈ 𝓝[s] a, tᢜ ∈ f) : sᢜ ∈ f := begin refine hs.compl_mem_sets (Ξ» a ha, _), rcases hf a ha with ⟨t, ht, hst⟩, replace ht := mem_inf_principal.1 ht, apply mem_inf_of_inter ht hst, rintros x ⟨h₁, hβ‚‚βŸ© hs, exact hβ‚‚ (h₁ hs) end /-- If `p : set Ξ± β†’ Prop` is stable under restriction and union, and each point `x` of a compact set `s` has a neighborhood `t` within `s` such that `p t`, then `p s` holds. -/ @[elab_as_eliminator] lemma is_compact.induction_on {s : set Ξ±} (hs : is_compact s) {p : set Ξ± β†’ Prop} (he : p βˆ…) (hmono : βˆ€ ⦃s t⦄, s βŠ† t β†’ p t β†’ p s) (hunion : βˆ€ ⦃s t⦄, p s β†’ p t β†’ p (s βˆͺ t)) (hnhds : βˆ€ x ∈ s, βˆƒ t ∈ 𝓝[s] x, p t) : p s := let f : filter Ξ± := { sets := {t | p tᢜ}, univ_sets := by simpa, sets_of_superset := Ξ» t₁ tβ‚‚ ht₁ ht, hmono (compl_subset_compl.2 ht) ht₁, inter_sets := Ξ» t₁ tβ‚‚ ht₁ htβ‚‚, by simp [compl_inter, hunion ht₁ htβ‚‚] } in have sᢜ ∈ f, from hs.compl_mem_sets_of_nhds_within (by simpa using hnhds), by simpa /-- The intersection of a compact set and a closed set is a compact set. -/ lemma is_compact.inter_right (hs : is_compact s) (ht : is_closed t) : is_compact (s ∩ t) := begin introsI f hnf hstf, obtain ⟨a, hsa, ha⟩ : βˆƒ a ∈ s, cluster_pt a f := hs (le_trans hstf (le_principal_iff.2 (inter_subset_left _ _))), have : a ∈ t := (ht.mem_of_nhds_within_ne_bot $ ha.mono $ le_trans hstf (le_principal_iff.2 (inter_subset_right _ _))), exact ⟨a, ⟨hsa, this⟩, ha⟩ end /-- The intersection of a closed set and a compact set is a compact set. -/ lemma is_compact.inter_left (ht : is_compact t) (hs : is_closed s) : is_compact (s ∩ t) := inter_comm t s β–Έ ht.inter_right hs /-- The set difference of a compact set and an open set is a compact set. -/ lemma is_compact.diff (hs : is_compact s) (ht : is_open t) : is_compact (s \ t) := hs.inter_right (is_closed_compl_iff.mpr ht) /-- A closed subset of a compact set is a compact set. -/ lemma is_compact_of_is_closed_subset (hs : is_compact s) (ht : is_closed t) (h : t βŠ† s) : is_compact t := inter_eq_self_of_subset_right h β–Έ hs.inter_right ht lemma is_compact.image_of_continuous_on {f : Ξ± β†’ Ξ²} (hs : is_compact s) (hf : continuous_on f s) : is_compact (f '' s) := begin intros l lne ls, have : ne_bot (l.comap f βŠ“ π“Ÿ s) := comap_inf_principal_ne_bot_of_image_mem lne (le_principal_iff.1 ls), obtain ⟨a, has, ha⟩ : βˆƒ a ∈ s, cluster_pt a (l.comap f βŠ“ π“Ÿ s) := @@hs this inf_le_right, use [f a, mem_image_of_mem f has], have : tendsto f (𝓝 a βŠ“ (comap f l βŠ“ π“Ÿ s)) (𝓝 (f a) βŠ“ l), { convert (hf a has).inf (@tendsto_comap _ _ f l) using 1, rw nhds_within, ac_refl }, exact @@tendsto.ne_bot _ this ha, end lemma is_compact.image {f : Ξ± β†’ Ξ²} (hs : is_compact s) (hf : continuous f) : is_compact (f '' s) := hs.image_of_continuous_on hf.continuous_on lemma is_compact.adherence_nhdset {f : filter Ξ±} (hs : is_compact s) (hfβ‚‚ : f ≀ π“Ÿ s) (ht₁ : is_open t) (htβ‚‚ : βˆ€ a ∈ s, cluster_pt a f β†’ a ∈ t) : t ∈ f := classical.by_cases mem_of_eq_bot $ assume : f βŠ“ π“Ÿ tᢜ β‰  βŠ₯, let ⟨a, ha, (hfa : cluster_pt a $ f βŠ“ π“Ÿ tᢜ)⟩ := @@hs ⟨this⟩ $ inf_le_of_left_le hfβ‚‚ in have a ∈ t, from htβ‚‚ a ha (hfa.of_inf_left), have tᢜ ∩ t ∈ 𝓝[tᢜ] a, from inter_mem_nhds_within _ (is_open.mem_nhds ht₁ this), have A : 𝓝[tᢜ] a = βŠ₯, from empty_mem_iff_bot.1 $ compl_inter_self t β–Έ this, have 𝓝[tᢜ] a β‰  βŠ₯, from hfa.of_inf_right.ne, absurd A this lemma is_compact_iff_ultrafilter_le_nhds : is_compact s ↔ (βˆ€ f : ultrafilter Ξ±, ↑f ≀ π“Ÿ s β†’ βˆƒ a ∈ s, ↑f ≀ 𝓝 a) := begin refine (forall_ne_bot_le_iff _).trans _, { rintro f g hle ⟨a, has, haf⟩, exact ⟨a, has, haf.mono hle⟩ }, { simp only [ultrafilter.cluster_pt_iff] } end alias is_compact_iff_ultrafilter_le_nhds ↔ is_compact.ultrafilter_le_nhds _ /-- For every open directed cover of a compact set, there exists a single element of the cover which itself includes the set. -/ lemma is_compact.elim_directed_cover {ΞΉ : Type v} [hΞΉ : nonempty ΞΉ] (hs : is_compact s) (U : ΞΉ β†’ set Ξ±) (hUo : βˆ€ i, is_open (U i)) (hsU : s βŠ† ⋃ i, U i) (hdU : directed (βŠ†) U) : βˆƒ i, s βŠ† U i := hΞΉ.elim $ Ξ» iβ‚€, is_compact.induction_on hs ⟨iβ‚€, empty_subset _⟩ (Ξ» s₁ sβ‚‚ hs ⟨i, hi⟩, ⟨i, subset.trans hs hi⟩) (Ξ» s₁ sβ‚‚ ⟨i, hi⟩ ⟨j, hj⟩, let ⟨k, hki, hkj⟩ := hdU i j in ⟨k, union_subset (subset.trans hi hki) (subset.trans hj hkj)⟩) (Ξ» x hx, let ⟨i, hi⟩ := mem_Union.1 (hsU hx) in ⟨U i, mem_nhds_within_of_mem_nhds (is_open.mem_nhds (hUo i) hi), i, subset.refl _⟩) /-- For every open cover of a compact set, there exists a finite subcover. -/ lemma is_compact.elim_finite_subcover {ΞΉ : Type v} (hs : is_compact s) (U : ΞΉ β†’ set Ξ±) (hUo : βˆ€ i, is_open (U i)) (hsU : s βŠ† ⋃ i, U i) : βˆƒ t : finset ΞΉ, s βŠ† ⋃ i ∈ t, U i := hs.elim_directed_cover _ (Ξ» t, is_open_bUnion $ Ξ» i _, hUo i) (Union_eq_Union_finset U β–Έ hsU) (directed_of_sup $ Ξ» t₁ tβ‚‚ h, bUnion_subset_bUnion_left h) lemma is_compact.elim_nhds_subcover' (hs : is_compact s) (U : Ξ  x ∈ s, set Ξ±) (hU : βˆ€ x ∈ s, U x β€Ήx ∈ sβ€Ί ∈ 𝓝 x) : βˆƒ t : finset s, s βŠ† ⋃ x ∈ t, U (x : s) x.2 := (hs.elim_finite_subcover (Ξ» x : s, interior (U x x.2)) (Ξ» x, is_open_interior) (Ξ» x hx, mem_Union.2 ⟨⟨x, hx⟩, mem_interior_iff_mem_nhds.2 $ hU _ _⟩)).imp $ Ξ» t ht, subset.trans ht $ Unionβ‚‚_mono $ Ξ» _ _, interior_subset lemma is_compact.elim_nhds_subcover (hs : is_compact s) (U : Ξ± β†’ set Ξ±) (hU : βˆ€ x ∈ s, U x ∈ 𝓝 x) : βˆƒ t : finset Ξ±, (βˆ€ x ∈ t, x ∈ s) ∧ s βŠ† ⋃ x ∈ t, U x := let ⟨t, ht⟩ := hs.elim_nhds_subcover' (Ξ» x _, U x) hU in ⟨t.image coe, Ξ» x hx, let ⟨y, hyt, hyx⟩ := finset.mem_image.1 hx in hyx β–Έ y.2, by rwa finset.set_bUnion_finset_image⟩ /-- The neighborhood filter of a compact set is disjoint with a filter `l` if and only if the neighborhood filter of each point of this set is disjoint with `l`. -/ lemma is_compact.disjoint_nhds_set_left {l : filter Ξ±} (hs : is_compact s) : disjoint (𝓝˒ s) l ↔ βˆ€ x ∈ s, disjoint (𝓝 x) l := begin refine ⟨λ h x hx, h.mono_left $ nhds_le_nhds_set hx, Ξ» H, _⟩, choose! U hxU hUl using Ξ» x hx, (nhds_basis_opens x).disjoint_iff_left.1 (H x hx), choose hxU hUo using hxU, rcases hs.elim_nhds_subcover U (Ξ» x hx, (hUo x hx).mem_nhds (hxU x hx)) with ⟨t, hts, hst⟩, refine (has_basis_nhds_set _).disjoint_iff_left.2 βŸ¨β‹ƒ x ∈ t, U x, ⟨is_open_bUnion $ Ξ» x hx, hUo x (hts x hx), hst⟩, _⟩, rw [compl_Unionβ‚‚, bInter_finset_mem], exact Ξ» x hx, hUl x (hts x hx) end /-- A filter `l` is disjoint with the neighborhood filter of a compact set if and only if it is disjoint with the neighborhood filter of each point of this set. -/ lemma is_compact.disjoint_nhds_set_right {l : filter Ξ±} (hs : is_compact s) : disjoint l (𝓝˒ s) ↔ βˆ€ x ∈ s, disjoint l (𝓝 x) := by simpa only [disjoint.comm] using hs.disjoint_nhds_set_left /-- For every family of closed sets whose intersection avoids a compact set, there exists a finite subfamily whose intersection avoids this compact set. -/ lemma is_compact.elim_finite_subfamily_closed {s : set Ξ±} {ΞΉ : Type v} (hs : is_compact s) (Z : ΞΉ β†’ set Ξ±) (hZc : βˆ€ i, is_closed (Z i)) (hsZ : s ∩ (β‹‚ i, Z i) = βˆ…) : βˆƒ t : finset ΞΉ, s ∩ (β‹‚ i ∈ t, Z i) = βˆ… := let ⟨t, ht⟩ := hs.elim_finite_subcover (Ξ» i, (Z i)ᢜ) (Ξ» i, (hZc i).is_open_compl) (by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_iff, not_and, iff_self, mem_Inter, mem_compl_iff] using hsZ) in ⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_iff, not_and, iff_self, mem_Inter, mem_compl_iff] using ht⟩ /-- If `s` is a compact set in a topological space `Ξ±` and `f : ΞΉ β†’ set Ξ±` is a locally finite family of sets, then `f i ∩ s` is nonempty only for a finitely many `i`. -/ lemma locally_finite.finite_nonempty_inter_compact {ΞΉ : Type*} {f : ΞΉ β†’ set Ξ±} (hf : locally_finite f) {s : set Ξ±} (hs : is_compact s) : {i | (f i ∩ s).nonempty}.finite := begin choose U hxU hUf using hf, rcases hs.elim_nhds_subcover U (Ξ» x _, hxU x) with ⟨t, -, hsU⟩, refine (t.finite_to_set.bUnion (Ξ» x _, hUf x)).subset _, rintro i ⟨x, hx⟩, rcases mem_Unionβ‚‚.1 (hsU hx.2) with ⟨c, hct, hcx⟩, exact mem_bUnion hct ⟨x, hx.1, hcx⟩ end /-- To show that a compact set intersects the intersection of a family of closed sets, it is sufficient to show that it intersects every finite subfamily. -/ lemma is_compact.inter_Inter_nonempty {s : set Ξ±} {ΞΉ : Type v} (hs : is_compact s) (Z : ΞΉ β†’ set Ξ±) (hZc : βˆ€ i, is_closed (Z i)) (hsZ : βˆ€ t : finset ΞΉ, (s ∩ β‹‚ i ∈ t, Z i).nonempty) : (s ∩ β‹‚ i, Z i).nonempty := begin simp only [nonempty_iff_ne_empty] at hsZ ⊒, apply mt (hs.elim_finite_subfamily_closed Z hZc), push_neg, exact hsZ end /-- Cantor's intersection theorem: the intersection of a directed family of nonempty compact closed sets is nonempty. -/ lemma is_compact.nonempty_Inter_of_directed_nonempty_compact_closed {ΞΉ : Type v} [hΞΉ : nonempty ΞΉ] (Z : ΞΉ β†’ set Ξ±) (hZd : directed (βŠ‡) Z) (hZn : βˆ€ i, (Z i).nonempty) (hZc : βˆ€ i, is_compact (Z i)) (hZcl : βˆ€ i, is_closed (Z i)) : (β‹‚ i, Z i).nonempty := begin apply hΞΉ.elim, intro iβ‚€, let Z' := Ξ» i, Z i ∩ Z iβ‚€, suffices : (β‹‚ i, Z' i).nonempty, { exact this.mono (Inter_mono $ Ξ» i, inter_subset_left (Z i) (Z iβ‚€)) }, rw nonempty_iff_ne_empty, intro H, obtain ⟨t, ht⟩ : βˆƒ (t : finset ΞΉ), ((Z iβ‚€) ∩ β‹‚ (i ∈ t), Z' i) = βˆ…, from (hZc iβ‚€).elim_finite_subfamily_closed Z' (assume i, is_closed.inter (hZcl i) (hZcl iβ‚€)) (by rw [H, inter_empty]), obtain ⟨i₁, hiβ‚βŸ© : βˆƒ i₁ : ΞΉ, Z i₁ βŠ† Z iβ‚€ ∧ βˆ€ i ∈ t, Z i₁ βŠ† Z' i, { rcases directed.finset_le hZd t with ⟨i, hi⟩, rcases hZd i iβ‚€ with ⟨i₁, hi₁, hiβ‚β‚€βŸ©, use [i₁, hi₁₀], intros j hj, exact subset_inter (subset.trans hi₁ (hi j hj)) hi₁₀ }, suffices : ((Z iβ‚€) ∩ β‹‚ (i ∈ t), Z' i).nonempty, { rw nonempty_iff_ne_empty at this, contradiction }, exact (hZn i₁).mono (subset_inter hi₁.left $ subset_Interβ‚‚ hi₁.right), end /-- Cantor's intersection theorem for sequences indexed by `β„•`: the intersection of a decreasing sequence of nonempty compact closed sets is nonempty. -/ lemma is_compact.nonempty_Inter_of_sequence_nonempty_compact_closed (Z : β„• β†’ set Ξ±) (hZd : βˆ€ i, Z (i+1) βŠ† Z i) (hZn : βˆ€ i, (Z i).nonempty) (hZ0 : is_compact (Z 0)) (hZcl : βˆ€ i, is_closed (Z i)) : (β‹‚ i, Z i).nonempty := have Zmono : antitone Z := antitone_nat_of_succ_le hZd, have hZd : directed (βŠ‡) Z, from directed_of_sup Zmono, have βˆ€ i, Z i βŠ† Z 0, from assume i, Zmono $ zero_le i, have hZc : βˆ€ i, is_compact (Z i), from assume i, is_compact_of_is_closed_subset hZ0 (hZcl i) (this i), is_compact.nonempty_Inter_of_directed_nonempty_compact_closed Z hZd hZn hZc hZcl /-- For every open cover of a compact set, there exists a finite subcover. -/ lemma is_compact.elim_finite_subcover_image {b : set ΞΉ} {c : ΞΉ β†’ set Ξ±} (hs : is_compact s) (hc₁ : βˆ€ i ∈ b, is_open (c i)) (hcβ‚‚ : s βŠ† ⋃ i ∈ b, c i) : βˆƒ b' βŠ† b, set.finite b' ∧ s βŠ† ⋃ i ∈ b', c i := begin rcases hs.elim_finite_subcover (Ξ» i, c i : b β†’ set Ξ±) _ _ with ⟨d, hd⟩; [skip, simpa using hc₁, simpa using hcβ‚‚], refine βŸ¨β†‘(d.image coe), _, finset.finite_to_set _, _⟩, { simp }, { rwa [finset.coe_image, bUnion_image] } end /-- A set `s` is compact if for every family of closed sets whose intersection avoids `s`, there exists a finite subfamily whose intersection avoids `s`. -/ theorem is_compact_of_finite_subfamily_closed (h : Ξ  {ΞΉ : Type u} (Z : ΞΉ β†’ (set Ξ±)), (βˆ€ i, is_closed (Z i)) β†’ s ∩ (β‹‚ i, Z i) = βˆ… β†’ (βˆƒ (t : finset ΞΉ), s ∩ (β‹‚ i ∈ t, Z i) = βˆ…)) : is_compact s := assume f hfn hfs, classical.by_contradiction $ assume : Β¬ (βˆƒ x ∈ s, cluster_pt x f), have hf : βˆ€ x ∈ s, 𝓝 x βŠ“ f = βŠ₯, by simpa only [cluster_pt, not_exists, not_not, ne_bot_iff], have Β¬ βˆƒ x ∈ s, βˆ€ t ∈ f.sets, x ∈ closure t, from assume ⟨x, hxs, hx⟩, have βˆ… ∈ 𝓝 x βŠ“ f, by rw [empty_mem_iff_bot, hf x hxs], let ⟨t₁, ht₁, tβ‚‚, htβ‚‚, ht⟩ := by rw [mem_inf_iff] at this; exact this in have βˆ… ∈ 𝓝[tβ‚‚] x, by { rw [ht, inter_comm], exact inter_mem_nhds_within _ ht₁ }, have 𝓝[tβ‚‚] x = βŠ₯, by rwa [empty_mem_iff_bot] at this, by simp only [closure_eq_cluster_pts] at hx; exact (hx tβ‚‚ htβ‚‚).ne this, let ⟨t, ht⟩ := h (Ξ» i : f.sets, closure i.1) (Ξ» i, is_closed_closure) (by simpa [eq_empty_iff_forall_not_mem, not_exists]) in have (β‹‚ i ∈ t, subtype.val i) ∈ f, from t.Inter_mem_sets.2 $ assume i hi, i.2, have s ∩ (β‹‚ i ∈ t, subtype.val i) ∈ f, from inter_mem (le_principal_iff.1 hfs) this, have βˆ… ∈ f, from mem_of_superset this $ assume x ⟨hxs, hx⟩, let ⟨i, hit, hxi⟩ := (show βˆƒ i ∈ t, x βˆ‰ closure (subtype.val i), by { rw [eq_empty_iff_forall_not_mem] at ht, simpa [hxs, not_forall] using ht x }) in have x ∈ closure i.val, from subset_closure (by { rw mem_Interβ‚‚ at hx, exact hx i hit }), show false, from hxi this, hfn.ne $ by rwa [empty_mem_iff_bot] at this /-- A set `s` is compact if for every open cover of `s`, there exists a finite subcover. -/ lemma is_compact_of_finite_subcover (h : Ξ  {ΞΉ : Type u} (U : ΞΉ β†’ (set Ξ±)), (βˆ€ i, is_open (U i)) β†’ s βŠ† (⋃ i, U i) β†’ (βˆƒ (t : finset ΞΉ), s βŠ† (⋃ i ∈ t, U i))) : is_compact s := is_compact_of_finite_subfamily_closed $ assume ΞΉ Z hZc hsZ, let ⟨t, ht⟩ := h (Ξ» i, (Z i)ᢜ) (assume i, is_open_compl_iff.mpr $ hZc i) (by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_iff, not_and, iff_self, mem_Inter, mem_compl_iff] using hsZ) in ⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_iff, not_and, iff_self, mem_Inter, mem_compl_iff] using ht⟩ /-- A set `s` is compact if and only if for every open cover of `s`, there exists a finite subcover. -/ lemma is_compact_iff_finite_subcover : is_compact s ↔ (Ξ  {ΞΉ : Type u} (U : ΞΉ β†’ (set Ξ±)), (βˆ€ i, is_open (U i)) β†’ s βŠ† (⋃ i, U i) β†’ (βˆƒ (t : finset ΞΉ), s βŠ† (⋃ i ∈ t, U i))) := ⟨assume hs ΞΉ, hs.elim_finite_subcover, is_compact_of_finite_subcover⟩ /-- A set `s` is compact if and only if for every family of closed sets whose intersection avoids `s`, there exists a finite subfamily whose intersection avoids `s`. -/ theorem is_compact_iff_finite_subfamily_closed : is_compact s ↔ (Ξ  {ΞΉ : Type u} (Z : ΞΉ β†’ (set Ξ±)), (βˆ€ i, is_closed (Z i)) β†’ s ∩ (β‹‚ i, Z i) = βˆ… β†’ (βˆƒ (t : finset ΞΉ), s ∩ (β‹‚ i ∈ t, Z i) = βˆ…)) := ⟨assume hs ΞΉ, hs.elim_finite_subfamily_closed, is_compact_of_finite_subfamily_closed⟩ /-- To show that `βˆ€ y ∈ K, P x y` holds for `x` close enough to `xβ‚€` when `K` is compact, it is sufficient to show that for all `yβ‚€ ∈ K` there `P x y` holds for `(x, y)` close enough to `(xβ‚€, yβ‚€)`. -/ lemma is_compact.eventually_forall_of_forall_eventually {xβ‚€ : Ξ±} {K : set Ξ²} (hK : is_compact K) {P : Ξ± β†’ Ξ² β†’ Prop} (hP : βˆ€ y ∈ K, βˆ€αΆ  (z : Ξ± Γ— Ξ²) in 𝓝 (xβ‚€, y), P z.1 z.2): βˆ€αΆ  x in 𝓝 xβ‚€, βˆ€ y ∈ K, P x y := begin refine hK.induction_on _ _ _ _, { exact eventually_of_forall (Ξ» x y, false.elim) }, { intros s t hst ht, refine ht.mono (Ξ» x h y hys, h y $ hst hys) }, { intros s t hs ht, filter_upwards [hs, ht], rintro x h1 h2 y (hys|hyt), exacts [h1 y hys, h2 y hyt] }, { intros y hyK, specialize hP y hyK, rw [nhds_prod_eq, eventually_prod_iff] at hP, rcases hP with ⟨p, hp, q, hq, hpq⟩, exact ⟨{y | q y}, mem_nhds_within_of_mem_nhds hq, eventually_of_mem hp @hpq⟩ } end @[simp] lemma is_compact_empty : is_compact (βˆ… : set Ξ±) := assume f hnf hsf, not.elim hnf.ne $ empty_mem_iff_bot.1 $ le_principal_iff.1 hsf @[simp] lemma is_compact_singleton {a : Ξ±} : is_compact ({a} : set Ξ±) := Ξ» f hf hfa, ⟨a, rfl, cluster_pt.of_le_nhds' (hfa.trans $ by simpa only [principal_singleton] using pure_le_nhds a) hf⟩ lemma set.subsingleton.is_compact {s : set Ξ±} (hs : s.subsingleton) : is_compact s := subsingleton.induction_on hs is_compact_empty $ Ξ» x, is_compact_singleton lemma set.finite.is_compact_bUnion {s : set ΞΉ} {f : ΞΉ β†’ set Ξ±} (hs : s.finite) (hf : βˆ€ i ∈ s, is_compact (f i)) : is_compact (⋃ i ∈ s, f i) := is_compact_of_finite_subcover $ assume ΞΉ U hUo hsU, have βˆ€ i : subtype s, βˆƒ t : finset ΞΉ, f i βŠ† (⋃ j ∈ t, U j), from assume ⟨i, hi⟩, (hf i hi).elim_finite_subcover _ hUo (calc f i βŠ† ⋃ i ∈ s, f i : subset_bUnion_of_mem hi ... βŠ† ⋃ j, U j : hsU), let ⟨finite_subcovers, h⟩ := axiom_of_choice this in by haveI : fintype (subtype s) := hs.fintype; exact let t := finset.bUnion finset.univ finite_subcovers in have (⋃ i ∈ s, f i) βŠ† (⋃ i ∈ t, U i), from Unionβ‚‚_subset $ assume i hi, calc f i βŠ† (⋃ j ∈ finite_subcovers ⟨i, hi⟩, U j) : (h ⟨i, hi⟩) ... βŠ† (⋃ j ∈ t, U j) : bUnion_subset_bUnion_left $ assume j hj, finset.mem_bUnion.mpr ⟨_, finset.mem_univ _, hj⟩, ⟨t, this⟩ lemma finset.is_compact_bUnion (s : finset ΞΉ) {f : ΞΉ β†’ set Ξ±} (hf : βˆ€ i ∈ s, is_compact (f i)) : is_compact (⋃ i ∈ s, f i) := s.finite_to_set.is_compact_bUnion hf lemma is_compact_accumulate {K : β„• β†’ set Ξ±} (hK : βˆ€ n, is_compact (K n)) (n : β„•) : is_compact (accumulate K n) := (finite_le_nat n).is_compact_bUnion $ Ξ» k _, hK k lemma is_compact_Union {f : ΞΉ β†’ set Ξ±} [finite ΞΉ] (h : βˆ€ i, is_compact (f i)) : is_compact (⋃ i, f i) := by rw ← bUnion_univ; exact finite_univ.is_compact_bUnion (Ξ» i _, h i) lemma set.finite.is_compact (hs : s.finite) : is_compact s := bUnion_of_singleton s β–Έ hs.is_compact_bUnion (Ξ» _ _, is_compact_singleton) lemma is_compact.finite_of_discrete [discrete_topology Ξ±] {s : set Ξ±} (hs : is_compact s) : s.finite := begin have : βˆ€ x : Ξ±, ({x} : set Ξ±) ∈ 𝓝 x, by simp [nhds_discrete], rcases hs.elim_nhds_subcover (Ξ» x, {x}) (Ξ» x hx, this x) with ⟨t, hts, hst⟩, simp only [← t.set_bUnion_coe, bUnion_of_singleton] at hst, exact t.finite_to_set.subset hst end lemma is_compact_iff_finite [discrete_topology Ξ±] {s : set Ξ±} : is_compact s ↔ s.finite := ⟨λ h, h.finite_of_discrete, Ξ» h, h.is_compact⟩ lemma is_compact.union (hs : is_compact s) (ht : is_compact t) : is_compact (s βˆͺ t) := by rw union_eq_Union; exact is_compact_Union (Ξ» b, by cases b; assumption) lemma is_compact.insert (hs : is_compact s) (a) : is_compact (insert a s) := is_compact_singleton.union hs /-- If `V : ΞΉ β†’ set Ξ±` is a decreasing family of closed compact sets then any neighborhood of `β‹‚ i, V i` contains some `V i`. We assume each `V i` is compact *and* closed because `Ξ±` is not assumed to be Hausdorff. See `exists_subset_nhd_of_compact` for version assuming this. -/ lemma exists_subset_nhds_of_is_compact' {ΞΉ : Type*} [nonempty ΞΉ] {V : ΞΉ β†’ set Ξ±} (hV : directed (βŠ‡) V) (hV_cpct : βˆ€ i, is_compact (V i)) (hV_closed : βˆ€ i, is_closed (V i)) {U : set Ξ±} (hU : βˆ€ x ∈ β‹‚ i, V i, U ∈ 𝓝 x) : βˆƒ i, V i βŠ† U := begin obtain ⟨W, hsubW, W_op, hWU⟩ := exists_open_set_nhds hU, rsuffices ⟨i, hi⟩ : βˆƒ i, V i βŠ† W, { exact ⟨i, hi.trans hWU⟩ }, by_contra' H, replace H : βˆ€ i, (V i ∩ Wᢜ).nonempty := Ξ» i, set.inter_compl_nonempty_iff.mpr (H i), have : (β‹‚ i, V i ∩ Wᢜ).nonempty, { refine is_compact.nonempty_Inter_of_directed_nonempty_compact_closed _ (Ξ» i j, _) H (Ξ» i, (hV_cpct i).inter_right W_op.is_closed_compl) (Ξ» i, (hV_closed i).inter W_op.is_closed_compl), rcases hV i j with ⟨k, hki, hkj⟩, refine ⟨k, ⟨λ x, _, Ξ» x, _⟩⟩ ; simp only [and_imp, mem_inter_iff, mem_compl_iff] ; tauto }, have : Β¬ (β‹‚ (i : ΞΉ), V i) βŠ† W, by simpa [← Inter_inter, inter_compl_nonempty_iff], contradiction end /-- If `Ξ±` has a basis consisting of compact opens, then an open set in `Ξ±` is compact open iff it is a finite union of some elements in the basis -/ lemma is_compact_open_iff_eq_finite_Union_of_is_topological_basis (b : ΞΉ β†’ set Ξ±) (hb : is_topological_basis (set.range b)) (hb' : βˆ€ i, is_compact (b i)) (U : set Ξ±) : is_compact U ∧ is_open U ↔ βˆƒ (s : set ΞΉ), s.finite ∧ U = ⋃ i ∈ s, b i := begin classical, split, { rintro ⟨h₁, hβ‚‚βŸ©, obtain ⟨β, f, e, hf⟩ := hb.open_eq_Union hβ‚‚, choose f' hf' using hf, have : b ∘ f' = f := funext hf', subst this, obtain ⟨t, ht⟩ := h₁.elim_finite_subcover (b ∘ f') (Ξ» i, hb.is_open (set.mem_range_self _)) (by rw e), refine ⟨t.image f', set.finite.intro infer_instance, le_antisymm _ _⟩, { refine set.subset.trans ht _, simp only [set.Union_subset_iff, coe_coe], intros i hi, erw ← set.Union_subtype (Ξ» x : ΞΉ, x ∈ t.image f') (Ξ» i, b i.1), exact set.subset_Union (Ξ» i : t.image f', b i) ⟨_, finset.mem_image_of_mem _ hi⟩ }, { apply set.Unionβ‚‚_subset, rintro i hi, obtain ⟨j, hj, rfl⟩ := finset.mem_image.mp hi, rw e, exact set.subset_Union (b ∘ f') j } }, { rintro ⟨s, hs, rfl⟩, split, { exact hs.is_compact_bUnion (Ξ» i _, hb' i) }, { apply is_open_bUnion, intros i hi, exact hb.is_open (set.mem_range_self _) } }, end namespace filter /-- `filter.cocompact` is the filter generated by complements to compact sets. -/ def cocompact (Ξ± : Type*) [topological_space Ξ±] : filter Ξ± := β¨… (s : set Ξ±) (hs : is_compact s), π“Ÿ (sᢜ) lemma has_basis_cocompact : (cocompact Ξ±).has_basis is_compact compl := has_basis_binfi_principal' (Ξ» s hs t ht, ⟨s βˆͺ t, hs.union ht, compl_subset_compl.2 (subset_union_left s t), compl_subset_compl.2 (subset_union_right s t)⟩) βŸ¨βˆ…, is_compact_empty⟩ lemma mem_cocompact : s ∈ cocompact Ξ± ↔ βˆƒ t, is_compact t ∧ tᢜ βŠ† s := has_basis_cocompact.mem_iff.trans $ exists_congr $ Ξ» t, exists_prop lemma mem_cocompact' : s ∈ cocompact Ξ± ↔ βˆƒ t, is_compact t ∧ sᢜ βŠ† t := mem_cocompact.trans $ exists_congr $ Ξ» t, and_congr_right $ Ξ» ht, compl_subset_comm lemma _root_.is_compact.compl_mem_cocompact (hs : is_compact s) : sᢜ ∈ filter.cocompact Ξ± := has_basis_cocompact.mem_of_mem hs lemma cocompact_le_cofinite : cocompact Ξ± ≀ cofinite := Ξ» s hs, compl_compl s β–Έ hs.is_compact.compl_mem_cocompact lemma cocompact_eq_cofinite (Ξ± : Type*) [topological_space Ξ±] [discrete_topology Ξ±] : cocompact Ξ± = cofinite := has_basis_cocompact.eq_of_same_basis $ by { convert has_basis_cofinite, ext s, exact is_compact_iff_finite } @[simp] lemma _root_.nat.cocompact_eq : cocompact β„• = at_top := (cocompact_eq_cofinite β„•).trans nat.cofinite_eq_at_top lemma tendsto.is_compact_insert_range_of_cocompact {f : Ξ± β†’ Ξ²} {b} (hf : tendsto f (cocompact Ξ±) (𝓝 b)) (hfc : continuous f) : is_compact (insert b (range f)) := begin introsI l hne hle, by_cases hb : cluster_pt b l, { exact ⟨b, or.inl rfl, hb⟩ }, simp only [cluster_pt_iff, not_forall, ← not_disjoint_iff_nonempty_inter, not_not] at hb, rcases hb with ⟨s, hsb, t, htl, hd⟩, rcases mem_cocompact.1 (hf hsb) with ⟨K, hKc, hKs⟩, have : f '' K ∈ l, { filter_upwards [htl, le_principal_iff.1 hle] with y hyt hyf, rcases hyf with (rfl|⟨x, rfl⟩), exacts [(hd.le_bot ⟨mem_of_mem_nhds hsb, hyt⟩).elim, mem_image_of_mem _ (not_not.1 $ Ξ» hxK, hd.le_bot ⟨hKs hxK, hyt⟩)] }, rcases hKc.image hfc (le_principal_iff.2 this) with ⟨y, hy, hyl⟩, exact ⟨y, or.inr $ image_subset_range _ _ hy, hyl⟩ end lemma tendsto.is_compact_insert_range_of_cofinite {f : ΞΉ β†’ Ξ±} {a} (hf : tendsto f cofinite (𝓝 a)) : is_compact (insert a (range f)) := begin letI : topological_space ΞΉ := βŠ₯, haveI := discrete_topology_bot ΞΉ, rw ← cocompact_eq_cofinite at hf, exact hf.is_compact_insert_range_of_cocompact continuous_of_discrete_topology end lemma tendsto.is_compact_insert_range {f : β„• β†’ Ξ±} {a} (hf : tendsto f at_top (𝓝 a)) : is_compact (insert a (range f)) := filter.tendsto.is_compact_insert_range_of_cofinite $ nat.cofinite_eq_at_top.symm β–Έ hf /-- `filter.coclosed_compact` is the filter generated by complements to closed compact sets. In a Hausdorff space, this is the same as `filter.cocompact`. -/ def coclosed_compact (Ξ± : Type*) [topological_space Ξ±] : filter Ξ± := β¨… (s : set Ξ±) (h₁ : is_closed s) (hβ‚‚ : is_compact s), π“Ÿ (sᢜ) lemma has_basis_coclosed_compact : (filter.coclosed_compact Ξ±).has_basis (Ξ» s, is_closed s ∧ is_compact s) compl := begin simp only [filter.coclosed_compact, infi_and'], refine has_basis_binfi_principal' _ βŸ¨βˆ…, is_closed_empty, is_compact_empty⟩, rintro s ⟨hs₁, hsβ‚‚βŸ© t ⟨ht₁, htβ‚‚βŸ©, exact ⟨s βˆͺ t, ⟨⟨hs₁.union ht₁, hsβ‚‚.union htβ‚‚βŸ©, compl_subset_compl.2 (subset_union_left _ _), compl_subset_compl.2 (subset_union_right _ _)⟩⟩ end lemma mem_coclosed_compact : s ∈ coclosed_compact Ξ± ↔ βˆƒ t, is_closed t ∧ is_compact t ∧ tᢜ βŠ† s := by simp [has_basis_coclosed_compact.mem_iff, and_assoc] lemma mem_coclosed_compact' : s ∈ coclosed_compact Ξ± ↔ βˆƒ t, is_closed t ∧ is_compact t ∧ sᢜ βŠ† t := by simp only [mem_coclosed_compact, compl_subset_comm] lemma cocompact_le_coclosed_compact : cocompact Ξ± ≀ coclosed_compact Ξ± := infi_mono $ Ξ» s, le_infi $ Ξ» _, le_rfl lemma _root_.is_compact.compl_mem_coclosed_compact_of_is_closed (hs : is_compact s) (hs' : is_closed s) : sᢜ ∈ filter.coclosed_compact Ξ± := has_basis_coclosed_compact.mem_of_mem ⟨hs', hs⟩ end filter namespace bornology variable (Ξ±) /-- Sets that are contained in a compact set form a bornology. Its `cobounded` filter is `filter.cocompact`. See also `bornology.relatively_compact` the bornology of sets with compact closure. -/ def in_compact : bornology Ξ± := { cobounded := filter.cocompact Ξ±, le_cofinite := filter.cocompact_le_cofinite } variable {Ξ±} lemma in_compact.is_bounded_iff : @is_bounded _ (in_compact Ξ±) s ↔ βˆƒ t, is_compact t ∧ s βŠ† t := begin change sᢜ ∈ filter.cocompact Ξ± ↔ _, rw filter.mem_cocompact, simp end end bornology section tube_lemma /-- `nhds_contain_boxes s t` means that any open neighborhood of `s Γ— t` in `Ξ± Γ— Ξ²` includes a product of an open neighborhood of `s` by an open neighborhood of `t`. -/ def nhds_contain_boxes (s : set Ξ±) (t : set Ξ²) : Prop := βˆ€ (n : set (Ξ± Γ— Ξ²)) (hn : is_open n) (hp : s Γ—Λ’ t βŠ† n), βˆƒ (u : set Ξ±) (v : set Ξ²), is_open u ∧ is_open v ∧ s βŠ† u ∧ t βŠ† v ∧ u Γ—Λ’ v βŠ† n lemma nhds_contain_boxes.symm {s : set Ξ±} {t : set Ξ²} : nhds_contain_boxes s t β†’ nhds_contain_boxes t s := assume H n hn hp, let ⟨u, v, uo, vo, su, tv, p⟩ := H (prod.swap ⁻¹' n) (hn.preimage continuous_swap) (by rwa [←image_subset_iff, image_swap_prod]) in ⟨v, u, vo, uo, tv, su, by rwa [←image_subset_iff, image_swap_prod] at p⟩ lemma nhds_contain_boxes.comm {s : set Ξ±} {t : set Ξ²} : nhds_contain_boxes s t ↔ nhds_contain_boxes t s := iff.intro nhds_contain_boxes.symm nhds_contain_boxes.symm lemma nhds_contain_boxes_of_singleton {x : Ξ±} {y : Ξ²} : nhds_contain_boxes ({x} : set Ξ±) ({y} : set Ξ²) := assume n hn hp, let ⟨u, v, uo, vo, xu, yv, hp'⟩ := is_open_prod_iff.mp hn x y (hp $ by simp) in ⟨u, v, uo, vo, by simpa, by simpa, hp'⟩ lemma nhds_contain_boxes_of_compact {s : set Ξ±} (hs : is_compact s) (t : set Ξ²) (H : βˆ€ x ∈ s, nhds_contain_boxes ({x} : set Ξ±) t) : nhds_contain_boxes s t := assume n hn hp, have βˆ€ x : s, βˆƒ uv : set Ξ± Γ— set Ξ², is_open uv.1 ∧ is_open uv.2 ∧ {↑x} βŠ† uv.1 ∧ t βŠ† uv.2 ∧ uv.1 Γ—Λ’ uv.2 βŠ† n, from assume ⟨x, hx⟩, have ({x} : set Ξ±) Γ—Λ’ t βŠ† n, from subset.trans (prod_mono (by simpa) subset.rfl) hp, let ⟨ux,vx,H1⟩ := H x hx n hn this in ⟨⟨ux,vx⟩,H1⟩, let ⟨uvs, h⟩ := classical.axiom_of_choice this in have us_cover : s βŠ† ⋃ i, (uvs i).1, from assume x hx, subset_Union _ ⟨x,hx⟩ (by simpa using (h ⟨x,hx⟩).2.2.1), let ⟨s0, s0_cover⟩ := hs.elim_finite_subcover _ (Ξ»i, (h i).1) us_cover in let u := ⋃(i ∈ s0), (uvs i).1 in let v := β‹‚(i ∈ s0), (uvs i).2 in have is_open u, from is_open_bUnion (Ξ»i _, (h i).1), have is_open v, from is_open_bInter s0.finite_to_set (Ξ»i _, (h i).2.1), have t βŠ† v, from subset_Interβ‚‚ (Ξ»i _, (h i).2.2.2.1), have u Γ—Λ’ v βŠ† n, from assume ⟨x',y'⟩ ⟨hx',hy'⟩, have βˆƒ i ∈ s0, x' ∈ (uvs i).1, by simpa using hx', let ⟨i,is0,hi⟩ := this in (h i).2.2.2.2 ⟨hi, (bInter_subset_of_mem is0 : v βŠ† (uvs i).2) hy'⟩, ⟨u, v, β€Ήis_open uβ€Ί, β€Ήis_open vβ€Ί, s0_cover, β€Ήt βŠ† vβ€Ί, β€Ήu Γ—Λ’ v βŠ† nβ€ΊβŸ© /-- If `s` and `t` are compact sets and `n` is an open neighborhood of `s Γ— t`, then there exist open neighborhoods `u βŠ‡ s` and `v βŠ‡ t` such that `u Γ— v βŠ† n`. -/ lemma generalized_tube_lemma {s : set Ξ±} (hs : is_compact s) {t : set Ξ²} (ht : is_compact t) {n : set (Ξ± Γ— Ξ²)} (hn : is_open n) (hp : s Γ—Λ’ t βŠ† n) : βˆƒ (u : set Ξ±) (v : set Ξ²), is_open u ∧ is_open v ∧ s βŠ† u ∧ t βŠ† v ∧ u Γ—Λ’ v βŠ† n := have _, from nhds_contain_boxes_of_compact hs t $ assume x _, nhds_contain_boxes.symm $ nhds_contain_boxes_of_compact ht {x} $ assume y _, nhds_contain_boxes_of_singleton, this n hn hp end tube_lemma /-- Type class for compact spaces. Separation is sometimes included in the definition, especially in the French literature, but we do not include it here. -/ class compact_space (Ξ± : Type*) [topological_space Ξ±] : Prop := (is_compact_univ : is_compact (univ : set Ξ±)) @[priority 10] -- see Note [lower instance priority] instance subsingleton.compact_space [subsingleton Ξ±] : compact_space Ξ± := ⟨subsingleton_univ.is_compact⟩ lemma is_compact_univ_iff : is_compact (univ : set Ξ±) ↔ compact_space Ξ± := ⟨λ h, ⟨h⟩, Ξ» h, h.1⟩ lemma is_compact_univ [h : compact_space Ξ±] : is_compact (univ : set Ξ±) := h.is_compact_univ lemma cluster_point_of_compact [compact_space Ξ±] (f : filter Ξ±) [ne_bot f] : βˆƒ x, cluster_pt x f := by simpa using is_compact_univ (show f ≀ π“Ÿ univ, by simp) lemma compact_space.elim_nhds_subcover [compact_space Ξ±] (U : Ξ± β†’ set Ξ±) (hU : βˆ€ x, U x ∈ 𝓝 x) : βˆƒ t : finset Ξ±, (⋃ x ∈ t, U x) = ⊀ := begin obtain ⟨t, -, s⟩ := is_compact.elim_nhds_subcover is_compact_univ U (Ξ» x m, hU x), exact ⟨t, by { rw eq_top_iff, exact s }⟩, end theorem compact_space_of_finite_subfamily_closed (h : Ξ  {ΞΉ : Type u} (Z : ΞΉ β†’ (set Ξ±)), (βˆ€ i, is_closed (Z i)) β†’ (β‹‚ i, Z i) = βˆ… β†’ βˆƒ (t : finset ΞΉ), (β‹‚ i ∈ t, Z i) = βˆ…) : compact_space Ξ± := { is_compact_univ := begin apply is_compact_of_finite_subfamily_closed, intros ΞΉ Z, specialize h Z, simpa using h end } lemma is_closed.is_compact [compact_space Ξ±] {s : set Ξ±} (h : is_closed s) : is_compact s := is_compact_of_is_closed_subset is_compact_univ h (subset_univ _) /-- `Ξ±` is a noncompact topological space if it not a compact space. -/ class noncompact_space (Ξ± : Type*) [topological_space Ξ±] : Prop := (noncompact_univ [] : Β¬is_compact (univ : set Ξ±)) export noncompact_space (noncompact_univ) lemma is_compact.ne_univ [noncompact_space Ξ±] {s : set Ξ±} (hs : is_compact s) : s β‰  univ := Ξ» h, noncompact_univ Ξ± (h β–Έ hs) instance [noncompact_space Ξ±] : ne_bot (filter.cocompact Ξ±) := begin refine filter.has_basis_cocompact.ne_bot_iff.2 (Ξ» s hs, _), contrapose hs, rw [not_nonempty_iff_eq_empty, compl_empty_iff] at hs, rw hs, exact noncompact_univ Ξ± end @[simp] lemma filter.cocompact_eq_bot [compact_space Ξ±] : filter.cocompact Ξ± = βŠ₯ := filter.has_basis_cocompact.eq_bot_iff.mpr ⟨set.univ, is_compact_univ, set.compl_univ⟩ instance [noncompact_space Ξ±] : ne_bot (filter.coclosed_compact Ξ±) := ne_bot_of_le filter.cocompact_le_coclosed_compact lemma noncompact_space_of_ne_bot (h : ne_bot (filter.cocompact Ξ±)) : noncompact_space Ξ± := ⟨λ h', (filter.nonempty_of_mem h'.compl_mem_cocompact).ne_empty compl_univ⟩ lemma filter.cocompact_ne_bot_iff : ne_bot (filter.cocompact Ξ±) ↔ noncompact_space Ξ± := ⟨noncompact_space_of_ne_bot, @filter.cocompact.filter.ne_bot _ _⟩ lemma not_compact_space_iff : Β¬compact_space Ξ± ↔ noncompact_space Ξ± := ⟨λ h₁, ⟨λ hβ‚‚, h₁ ⟨hβ‚‚βŸ©βŸ©, Ξ» ⟨hβ‚βŸ© ⟨hβ‚‚βŸ©, h₁ hβ‚‚βŸ© instance : noncompact_space β„€ := noncompact_space_of_ne_bot $ by simp only [filter.cocompact_eq_cofinite, filter.cofinite_ne_bot] -- Note: We can't make this into an instance because it loops with `finite.compact_space`. /-- A compact discrete space is finite. -/ lemma finite_of_compact_of_discrete [compact_space Ξ±] [discrete_topology Ξ±] : finite Ξ± := finite.of_finite_univ $ is_compact_univ.finite_of_discrete lemma exists_nhds_ne_ne_bot (Ξ± : Type*) [topological_space Ξ±] [compact_space Ξ±] [infinite Ξ±] : βˆƒ z : Ξ±, (𝓝[β‰ ] z).ne_bot := begin by_contra' H, simp_rw not_ne_bot at H, haveI := discrete_topology_iff_nhds_ne.mpr H, exact infinite.not_finite (finite_of_compact_of_discrete : finite Ξ±), end lemma finite_cover_nhds_interior [compact_space Ξ±] {U : Ξ± β†’ set Ξ±} (hU : βˆ€ x, U x ∈ 𝓝 x) : βˆƒ t : finset Ξ±, (⋃ x ∈ t, interior (U x)) = univ := let ⟨t, ht⟩ := is_compact_univ.elim_finite_subcover (Ξ» x, interior (U x)) (Ξ» x, is_open_interior) (Ξ» x _, mem_Union.2 ⟨x, mem_interior_iff_mem_nhds.2 (hU x)⟩) in ⟨t, univ_subset_iff.1 ht⟩ lemma finite_cover_nhds [compact_space Ξ±] {U : Ξ± β†’ set Ξ±} (hU : βˆ€ x, U x ∈ 𝓝 x) : βˆƒ t : finset Ξ±, (⋃ x ∈ t, U x) = univ := let ⟨t, ht⟩ := finite_cover_nhds_interior hU in ⟨t, univ_subset_iff.1 $ ht.symm.subset.trans $ Unionβ‚‚_mono $ Ξ» x hx, interior_subset⟩ /-- If `Ξ±` is a compact space, then a locally finite family of sets of `Ξ±` can have only finitely many nonempty elements. -/ lemma locally_finite.finite_nonempty_of_compact {ΞΉ : Type*} [compact_space Ξ±] {f : ΞΉ β†’ set Ξ±} (hf : locally_finite f) : {i | (f i).nonempty}.finite := by simpa only [inter_univ] using hf.finite_nonempty_inter_compact is_compact_univ /-- If `Ξ±` is a compact space, then a locally finite family of nonempty sets of `Ξ±` can have only finitely many elements, `set.finite` version. -/ lemma locally_finite.finite_of_compact {ΞΉ : Type*} [compact_space Ξ±] {f : ΞΉ β†’ set Ξ±} (hf : locally_finite f) (hne : βˆ€ i, (f i).nonempty) : (univ : set ΞΉ).finite := by simpa only [hne] using hf.finite_nonempty_of_compact /-- If `Ξ±` is a compact space, then a locally finite family of nonempty sets of `Ξ±` can have only finitely many elements, `fintype` version. -/ noncomputable def locally_finite.fintype_of_compact {ΞΉ : Type*} [compact_space Ξ±] {f : ΞΉ β†’ set Ξ±} (hf : locally_finite f) (hne : βˆ€ i, (f i).nonempty) : fintype ΞΉ := fintype_of_finite_univ (hf.finite_of_compact hne) /-- The comap of the cocompact filter on `Ξ²` by a continuous function `f : Ξ± β†’ Ξ²` is less than or equal to the cocompact filter on `Ξ±`. This is a reformulation of the fact that images of compact sets are compact. -/ lemma filter.comap_cocompact_le {f : Ξ± β†’ Ξ²} (hf : continuous f) : (filter.cocompact Ξ²).comap f ≀ filter.cocompact Ξ± := begin rw (filter.has_basis_cocompact.comap f).le_basis_iff filter.has_basis_cocompact, intros t ht, refine ⟨f '' t, ht.image hf, _⟩, simpa using t.subset_preimage_image f end lemma is_compact_range [compact_space Ξ±] {f : Ξ± β†’ Ξ²} (hf : continuous f) : is_compact (range f) := by rw ← image_univ; exact is_compact_univ.image hf lemma is_compact_diagonal [compact_space Ξ±] : is_compact (diagonal Ξ±) := @range_diag Ξ± β–Έ is_compact_range (continuous_id.prod_mk continuous_id) /-- If X is is_compact then prβ‚‚ : X Γ— Y β†’ Y is a closed map -/ theorem is_closed_proj_of_is_compact {X : Type*} [topological_space X] [compact_space X] {Y : Type*} [topological_space Y] : is_closed_map (prod.snd : X Γ— Y β†’ Y) := begin set Ο€X := (prod.fst : X Γ— Y β†’ X), set Ο€Y := (prod.snd : X Γ— Y β†’ Y), assume C (hC : is_closed C), rw is_closed_iff_cluster_pt at hC ⊒, assume y (y_closure : cluster_pt y $ π“Ÿ (Ο€Y '' C)), haveI : ne_bot (map Ο€X (comap Ο€Y (𝓝 y) βŠ“ π“Ÿ C)), { suffices : ne_bot (map Ο€Y (comap Ο€Y (𝓝 y) βŠ“ π“Ÿ C)), by simpa only [map_ne_bot_iff], convert y_closure, calc map Ο€Y (comap Ο€Y (𝓝 y) βŠ“ π“Ÿ C) = 𝓝 y βŠ“ map Ο€Y (π“Ÿ C) : filter.push_pull' _ _ _ ... = 𝓝 y βŠ“ π“Ÿ (Ο€Y '' C) : by rw map_principal }, obtain ⟨x, hx⟩ : βˆƒ x, cluster_pt x (map Ο€X (comap Ο€Y (𝓝 y) βŠ“ π“Ÿ C)), from cluster_point_of_compact _, refine ⟨⟨x, y⟩, _, by simp [Ο€Y]⟩, apply hC, rw [cluster_pt, ← filter.map_ne_bot_iff Ο€X], convert hx, calc map Ο€X (𝓝 (x, y) βŠ“ π“Ÿ C) = map Ο€X (comap Ο€X (𝓝 x) βŠ“ comap Ο€Y (𝓝 y) βŠ“ π“Ÿ C) : by rw [nhds_prod_eq, filter.prod] ... = map Ο€X (comap Ο€Y (𝓝 y) βŠ“ π“Ÿ C βŠ“ comap Ο€X (𝓝 x)) : by ac_refl ... = map Ο€X (comap Ο€Y (𝓝 y) βŠ“ π“Ÿ C) βŠ“ 𝓝 x : by rw filter.push_pull ... = 𝓝 x βŠ“ map Ο€X (comap Ο€Y (𝓝 y) βŠ“ π“Ÿ C) : by rw inf_comm end lemma exists_subset_nhds_of_compact_space [compact_space Ξ±] {ΞΉ : Type*} [nonempty ΞΉ] {V : ΞΉ β†’ set Ξ±} (hV : directed (βŠ‡) V) (hV_closed : βˆ€ i, is_closed (V i)) {U : set Ξ±} (hU : βˆ€ x ∈ β‹‚ i, V i, U ∈ 𝓝 x) : βˆƒ i, V i βŠ† U := exists_subset_nhds_of_is_compact' hV (Ξ» i, (hV_closed i).is_compact) hV_closed hU /-- If `f : Ξ± β†’ Ξ²` is an `inducing` map, then the image `f '' s` of a set `s` is compact if and only if the set `s` is closed. -/ lemma inducing.is_compact_iff {f : Ξ± β†’ Ξ²} (hf : inducing f) {s : set Ξ±} : is_compact (f '' s) ↔ is_compact s := begin refine ⟨_, Ξ» hs, hs.image hf.continuous⟩, introsI hs F F_ne_bot F_le, obtain ⟨_, ⟨x, x_in : x ∈ s, rfl⟩, hx : cluster_pt (f x) (map f F)⟩ := hs (calc map f F ≀ map f (π“Ÿ s) : map_mono F_le ... = π“Ÿ (f '' s) : map_principal), use [x, x_in], suffices : (map f (𝓝 x βŠ“ F)).ne_bot, by simpa [filter.map_ne_bot_iff], rwa calc map f (𝓝 x βŠ“ F) = map f ((comap f $ 𝓝 $ f x) βŠ“ F) : by rw hf.nhds_eq_comap ... = 𝓝 (f x) βŠ“ map f F : filter.push_pull' _ _ _ end /-- If `f : Ξ± β†’ Ξ²` is an `embedding` (or more generally, an `inducing` map, see `inducing.is_compact_iff`), then the image `f '' s` of a set `s` is compact if and only if the set `s` is closed. -/ lemma embedding.is_compact_iff_is_compact_image {f : Ξ± β†’ Ξ²} (hf : embedding f) : is_compact s ↔ is_compact (f '' s) := hf.to_inducing.is_compact_iff.symm /-- The preimage of a compact set under a closed embedding is a compact set. -/ lemma closed_embedding.is_compact_preimage {f : Ξ± β†’ Ξ²} (hf : closed_embedding f) {K : set Ξ²} (hK : is_compact K) : is_compact (f ⁻¹' K) := begin replace hK := hK.inter_right hf.closed_range, rwa [← hf.to_inducing.is_compact_iff, image_preimage_eq_inter_range] end /-- A closed embedding is proper, ie, inverse images of compact sets are contained in compacts. Moreover, the preimage of a compact set is compact, see `closed_embedding.is_compact_preimage`. -/ lemma closed_embedding.tendsto_cocompact {f : Ξ± β†’ Ξ²} (hf : closed_embedding f) : tendsto f (filter.cocompact Ξ±) (filter.cocompact Ξ²) := filter.has_basis_cocompact.tendsto_right_iff.mpr $ Ξ» K hK, (hf.is_compact_preimage hK).compl_mem_cocompact lemma is_compact_iff_is_compact_in_subtype {p : Ξ± β†’ Prop} {s : set {a // p a}} : is_compact s ↔ is_compact ((coe : _ β†’ Ξ±) '' s) := embedding_subtype_coe.is_compact_iff_is_compact_image lemma is_compact_iff_is_compact_univ {s : set Ξ±} : is_compact s ↔ is_compact (univ : set s) := by rw [is_compact_iff_is_compact_in_subtype, image_univ, subtype.range_coe]; refl lemma is_compact_iff_compact_space {s : set Ξ±} : is_compact s ↔ compact_space s := is_compact_iff_is_compact_univ.trans ⟨λ h, ⟨h⟩, @compact_space.is_compact_univ _ _⟩ lemma is_compact.finite {s : set Ξ±} (hs : is_compact s) (hs' : discrete_topology s) : s.finite := finite_coe_iff.mp (@finite_of_compact_of_discrete _ _ (is_compact_iff_compact_space.mp hs) hs') lemma exists_nhds_ne_inf_principal_ne_bot {s : set Ξ±} (hs : is_compact s) (hs' : s.infinite) : βˆƒ z ∈ s, (𝓝[β‰ ] z βŠ“ π“Ÿ s).ne_bot := begin by_contra' H, simp_rw not_ne_bot at H, exact hs' (hs.finite $ discrete_topology_subtype_iff.mpr H), end protected lemma closed_embedding.noncompact_space [noncompact_space Ξ±] {f : Ξ± β†’ Ξ²} (hf : closed_embedding f) : noncompact_space Ξ² := noncompact_space_of_ne_bot hf.tendsto_cocompact.ne_bot protected lemma closed_embedding.compact_space [h : compact_space Ξ²] {f : Ξ± β†’ Ξ²} (hf : closed_embedding f) : compact_space Ξ± := by { unfreezingI { contrapose! h, rw not_compact_space_iff at h ⊒ }, exact hf.noncompact_space } lemma is_compact.prod {s : set Ξ±} {t : set Ξ²} (hs : is_compact s) (ht : is_compact t) : is_compact (s Γ—Λ’ t) := begin rw is_compact_iff_ultrafilter_le_nhds at hs ht ⊒, intros f hfs, rw le_principal_iff at hfs, obtain ⟨a : Ξ±, sa : a ∈ s, ha : map prod.fst ↑f ≀ 𝓝 a⟩ := hs (f.map prod.fst) (le_principal_iff.2 $ mem_map.2 $ mem_of_superset hfs (Ξ» x, and.left)), obtain ⟨b : Ξ², tb : b ∈ t, hb : map prod.snd ↑f ≀ 𝓝 b⟩ := ht (f.map prod.snd) (le_principal_iff.2 $ mem_map.2 $ mem_of_superset hfs (Ξ» x, and.right)), rw map_le_iff_le_comap at ha hb, refine ⟨⟨a, b⟩, ⟨sa, tb⟩, _⟩, rw nhds_prod_eq, exact le_inf ha hb end /-- Finite topological spaces are compact. -/ @[priority 100] instance finite.compact_space [finite Ξ±] : compact_space Ξ± := { is_compact_univ := finite_univ.is_compact } /-- The product of two compact spaces is compact. -/ instance [compact_space Ξ±] [compact_space Ξ²] : compact_space (Ξ± Γ— Ξ²) := ⟨by { rw ← univ_prod_univ, exact is_compact_univ.prod is_compact_univ }⟩ /-- The disjoint union of two compact spaces is compact. -/ instance [compact_space Ξ±] [compact_space Ξ²] : compact_space (Ξ± βŠ• Ξ²) := ⟨begin rw ← range_inl_union_range_inr, exact (is_compact_range continuous_inl).union (is_compact_range continuous_inr) end⟩ instance [finite ΞΉ] [Ξ  i, topological_space (Ο€ i)] [βˆ€ i, compact_space (Ο€ i)] : compact_space (Ξ£ i, Ο€ i) := begin refine ⟨_⟩, rw sigma.univ, exact is_compact_Union (Ξ» i, is_compact_range continuous_sigma_mk), end /-- The coproduct of the cocompact filters on two topological spaces is the cocompact filter on their product. -/ lemma filter.coprod_cocompact : (filter.cocompact Ξ±).coprod (filter.cocompact Ξ²) = filter.cocompact (Ξ± Γ— Ξ²) := begin ext S, simp only [mem_coprod_iff, exists_prop, mem_comap, filter.mem_cocompact], split, { rintro ⟨⟨A, ⟨t, ht, hAt⟩, hAS⟩, B, ⟨t', ht', hBt'⟩, hBS⟩, refine ⟨t Γ—Λ’ t', ht.prod ht', _⟩, refine subset.trans _ (union_subset hAS hBS), rw compl_subset_comm at ⊒ hAt hBt', refine subset.trans _ (set.prod_mono hAt hBt'), intros x, simp only [compl_union, mem_inter_iff, mem_prod, mem_preimage, mem_compl_iff], tauto }, { rintros ⟨t, ht, htS⟩, refine ⟨⟨(prod.fst '' t)ᢜ, _, _⟩, ⟨(prod.snd '' t)ᢜ, _, _⟩⟩, { exact ⟨prod.fst '' t, ht.image continuous_fst, subset.rfl⟩ }, { rw preimage_compl, rw compl_subset_comm at ⊒ htS, exact subset.trans htS (subset_preimage_image prod.fst _) }, { exact ⟨prod.snd '' t, ht.image continuous_snd, subset.rfl⟩ }, { rw preimage_compl, rw compl_subset_comm at ⊒ htS, exact subset.trans htS (subset_preimage_image prod.snd _) } } end lemma prod.noncompact_space_iff : noncompact_space (Ξ± Γ— Ξ²) ↔ noncompact_space Ξ± ∧ nonempty Ξ² ∨ nonempty Ξ± ∧ noncompact_space Ξ² := by simp [← filter.cocompact_ne_bot_iff, ← filter.coprod_cocompact, filter.coprod_ne_bot_iff] @[priority 100] -- See Note [lower instance priority] instance prod.noncompact_space_left [noncompact_space Ξ±] [nonempty Ξ²] : noncompact_space (Ξ± Γ— Ξ²) := prod.noncompact_space_iff.2 (or.inl βŸ¨β€Ή_β€Ί, β€Ή_β€ΊβŸ©) @[priority 100] -- See Note [lower instance priority] instance prod.noncompact_space_right [nonempty Ξ±] [noncompact_space Ξ²] : noncompact_space (Ξ± Γ— Ξ²) := prod.noncompact_space_iff.2 (or.inr βŸ¨β€Ή_β€Ί, β€Ή_β€ΊβŸ©) section tychonoff variables [Ξ  i, topological_space (Ο€ i)] /-- **Tychonoff's theorem**: product of compact sets is compact. -/ lemma is_compact_pi_infinite {s : Ξ  i, set (Ο€ i)} : (βˆ€ i, is_compact (s i)) β†’ is_compact {x : Ξ  i, Ο€ i | βˆ€ i, x i ∈ s i} := begin simp only [is_compact_iff_ultrafilter_le_nhds, nhds_pi, filter.pi, exists_prop, mem_set_of_eq, le_infi_iff, le_principal_iff], intros h f hfs, have : βˆ€ i:ΞΉ, βˆƒ a, a ∈ s i ∧ tendsto (Ξ»x:Ξ i:ΞΉ, Ο€ i, x i) f (𝓝 a), { refine Ξ» i, h i (f.map _) (mem_map.2 _), exact mem_of_superset hfs (Ξ» x hx, hx i) }, choose a ha, exact ⟨a, assume i, (ha i).left, assume i, (ha i).right.le_comap⟩ end /-- **Tychonoff's theorem** formulated using `set.pi`: product of compact sets is compact. -/ lemma is_compact_univ_pi {s : Ξ  i, set (Ο€ i)} (h : βˆ€ i, is_compact (s i)) : is_compact (pi univ s) := by { convert is_compact_pi_infinite h, simp only [← mem_univ_pi, set_of_mem_eq] } instance pi.compact_space [βˆ€ i, compact_space (Ο€ i)] : compact_space (Ξ i, Ο€ i) := ⟨by { rw [← pi_univ univ], exact is_compact_univ_pi (Ξ» i, is_compact_univ) }⟩ /-- **Tychonoff's theorem** formulated in terms of filters: `filter.cocompact` on an indexed product type `Ξ  d, ΞΊ d` the `filter.Coprod` of filters `filter.cocompact` on `ΞΊ d`. -/ lemma filter.Coprod_cocompact {Ξ΄ : Type*} {ΞΊ : Ξ΄ β†’ Type*} [Ξ  d, topological_space (ΞΊ d)] : filter.Coprod (Ξ» d, filter.cocompact (ΞΊ d)) = filter.cocompact (Ξ  d, ΞΊ d) := begin refine le_antisymm (supr_le $ Ξ» i, filter.comap_cocompact_le (continuous_apply i)) _, refine compl_surjective.forall.2 (Ξ» s H, _), simp only [compl_mem_Coprod, filter.mem_cocompact, compl_subset_compl, image_subset_iff] at H ⊒, choose K hKc htK using H, exact ⟨set.pi univ K, is_compact_univ_pi hKc, Ξ» f hf i hi, htK i hf⟩ end end tychonoff instance quot.compact_space {r : Ξ± β†’ Ξ± β†’ Prop} [compact_space Ξ±] : compact_space (quot r) := ⟨by { rw ← range_quot_mk, exact is_compact_range continuous_quot_mk }⟩ instance quotient.compact_space {s : setoid Ξ±} [compact_space Ξ±] : compact_space (quotient s) := quot.compact_space /-- There are various definitions of "locally compact space" in the literature, which agree for Hausdorff spaces but not in general. This one is the precise condition on X needed for the evaluation `map C(X, Y) Γ— X β†’ Y` to be continuous for all `Y` when `C(X, Y)` is given the compact-open topology. -/ class locally_compact_space (Ξ± : Type*) [topological_space Ξ±] : Prop := (local_compact_nhds : βˆ€ (x : Ξ±) (n ∈ 𝓝 x), βˆƒ s ∈ 𝓝 x, s βŠ† n ∧ is_compact s) lemma compact_basis_nhds [locally_compact_space Ξ±] (x : Ξ±) : (𝓝 x).has_basis (Ξ» s, s ∈ 𝓝 x ∧ is_compact s) (Ξ» s, s) := has_basis_self.2 $ by simpa only [and_comm] using locally_compact_space.local_compact_nhds x lemma local_compact_nhds [locally_compact_space Ξ±] {x : Ξ±} {n : set Ξ±} (h : n ∈ 𝓝 x) : βˆƒ s ∈ 𝓝 x, s βŠ† n ∧ is_compact s := locally_compact_space.local_compact_nhds _ _ h lemma locally_compact_space_of_has_basis {ΞΉ : Ξ± β†’ Type*} {p : Ξ  x, ΞΉ x β†’ Prop} {s : Ξ  x, ΞΉ x β†’ set Ξ±} (h : βˆ€ x, (𝓝 x).has_basis (p x) (s x)) (hc : βˆ€ x i, p x i β†’ is_compact (s x i)) : locally_compact_space Ξ± := ⟨λ x t ht, let ⟨i, hp, ht⟩ := (h x).mem_iff.1 ht in ⟨s x i, (h x).mem_of_mem hp, ht, hc x i hp⟩⟩ instance locally_compact_space.prod (Ξ± : Type*) (Ξ² : Type*) [topological_space Ξ±] [topological_space Ξ²] [locally_compact_space Ξ±] [locally_compact_space Ξ²] : locally_compact_space (Ξ± Γ— Ξ²) := have _ := Ξ» x : Ξ± Γ— Ξ², (compact_basis_nhds x.1).prod_nhds' (compact_basis_nhds x.2), locally_compact_space_of_has_basis this $ Ξ» x s ⟨⟨_, hβ‚βŸ©, _, hβ‚‚βŸ©, h₁.prod hβ‚‚ section pi variables [Ξ  i, topological_space (Ο€ i)] [βˆ€ i, locally_compact_space (Ο€ i)] /--In general it suffices that all but finitely many of the spaces are compact, but that's not straightforward to state and use. -/ instance locally_compact_space.pi_finite [finite ΞΉ] : locally_compact_space (Ξ  i, Ο€ i) := ⟨λ t n hn, begin rw [nhds_pi, filter.mem_pi] at hn, obtain ⟨s, hs, n', hn', hsub⟩ := hn, choose n'' hn'' hsub' hc using Ξ» i, locally_compact_space.local_compact_nhds (t i) (n' i) (hn' i), refine ⟨(set.univ : set ΞΉ).pi n'', _, subset_trans (Ξ» _ h, _) hsub, is_compact_univ_pi hc⟩, { exact (set_pi_mem_nhds_iff (@set.finite_univ ΞΉ _) _).mpr (Ξ» i hi, hn'' i), }, { exact Ξ» i hi, hsub' i (h i trivial), }, end⟩ /-- For spaces that are not Hausdorff. -/ instance locally_compact_space.pi [βˆ€ i, compact_space (Ο€ i)] : locally_compact_space (Ξ  i, Ο€ i) := ⟨λ t n hn, begin rw [nhds_pi, filter.mem_pi] at hn, obtain ⟨s, hs, n', hn', hsub⟩ := hn, choose n'' hn'' hsub' hc using Ξ» i, locally_compact_space.local_compact_nhds (t i) (n' i) (hn' i), refine ⟨s.pi n'', _, subset_trans (Ξ» _, _) hsub, _⟩, { exact (set_pi_mem_nhds_iff hs _).mpr (Ξ» i _, hn'' i), }, { exact forallβ‚‚_imp (Ξ» i hi hi', hsub' i hi'), }, { rw ← set.univ_pi_ite, refine is_compact_univ_pi (Ξ» i, _), by_cases i ∈ s, { rw if_pos h, exact hc i, }, { rw if_neg h, exact compact_space.is_compact_univ, } }, end⟩ end pi /-- A reformulation of the definition of locally compact space: In a locally compact space, every open set containing `x` has a compact subset containing `x` in its interior. -/ lemma exists_compact_subset [locally_compact_space Ξ±] {x : Ξ±} {U : set Ξ±} (hU : is_open U) (hx : x ∈ U) : βˆƒ (K : set Ξ±), is_compact K ∧ x ∈ interior K ∧ K βŠ† U := begin rcases locally_compact_space.local_compact_nhds x U (hU.mem_nhds hx) with ⟨K, h1K, h2K, h3K⟩, exact ⟨K, h3K, mem_interior_iff_mem_nhds.2 h1K, h2K⟩, end /-- In a locally compact space every point has a compact neighborhood. -/ lemma exists_compact_mem_nhds [locally_compact_space Ξ±] (x : Ξ±) : βˆƒ K, is_compact K ∧ K ∈ 𝓝 x := let ⟨K, hKc, hx, H⟩ := exists_compact_subset is_open_univ (mem_univ x) in ⟨K, hKc, mem_interior_iff_mem_nhds.1 hx⟩ /-- In a locally compact space, for every containement `K βŠ† U` of a compact set `K` in an open set `U`, there is a compact neighborhood `L` such that `K βŠ† L βŠ† U`: equivalently, there is a compact `L` such that `K βŠ† interior L` and `L βŠ† U`. -/ lemma exists_compact_between [hΞ± : locally_compact_space Ξ±] {K U : set Ξ±} (hK : is_compact K) (hU : is_open U) (h_KU : K βŠ† U) : βˆƒ L, is_compact L ∧ K βŠ† interior L ∧ L βŠ† U := begin choose V hVc hxV hKV using Ξ» x : K, exists_compact_subset hU (h_KU x.2), have : K βŠ† ⋃ x, interior (V x), from Ξ» x hx, mem_Union.2 ⟨⟨x, hx⟩, hxV _⟩, rcases hK.elim_finite_subcover _ (Ξ» x, @is_open_interior Ξ± _ (V x)) this with ⟨t, ht⟩, refine ⟨_, t.is_compact_bUnion (Ξ» x _, hVc x), Ξ» x hx, _, set.Unionβ‚‚_subset (Ξ» i _, hKV i)⟩, rcases mem_Unionβ‚‚.1 (ht hx) with ⟨y, hyt, hy⟩, exact interior_mono (subset_bUnion_of_mem hyt) hy, end /-- In a locally compact space, every compact set is contained in the interior of a compact set. -/ lemma exists_compact_superset [locally_compact_space Ξ±] {K : set Ξ±} (hK : is_compact K) : βˆƒ K', is_compact K' ∧ K βŠ† interior K' := let ⟨L, hLc, hKL, _⟩ := exists_compact_between hK is_open_univ K.subset_univ in ⟨L, hLc, hKL⟩ protected lemma closed_embedding.locally_compact_space [locally_compact_space Ξ²] {f : Ξ± β†’ Ξ²} (hf : closed_embedding f) : locally_compact_space Ξ± := begin have : βˆ€ x : Ξ±, (𝓝 x).has_basis (Ξ» s, s ∈ 𝓝 (f x) ∧ is_compact s) (Ξ» s, f ⁻¹' s), { intro x, rw hf.to_embedding.to_inducing.nhds_eq_comap, exact (compact_basis_nhds _).comap _ }, exact locally_compact_space_of_has_basis this (Ξ» x s hs, hf.is_compact_preimage hs.2) end protected lemma is_closed.locally_compact_space [locally_compact_space Ξ±] {s : set Ξ±} (hs : is_closed s) : locally_compact_space s := (closed_embedding_subtype_coe hs).locally_compact_space protected lemma open_embedding.locally_compact_space [locally_compact_space Ξ²] {f : Ξ± β†’ Ξ²} (hf : open_embedding f) : locally_compact_space Ξ± := begin have : βˆ€ x : Ξ±, (𝓝 x).has_basis (Ξ» s, (s ∈ 𝓝 (f x) ∧ is_compact s) ∧ s βŠ† range f) (Ξ» s, f ⁻¹' s), { intro x, rw hf.to_embedding.to_inducing.nhds_eq_comap, exact ((compact_basis_nhds _).restrict_subset $ hf.open_range.mem_nhds $ mem_range_self _).comap _ }, refine locally_compact_space_of_has_basis this (Ξ» x s hs, _), rw [← hf.to_inducing.is_compact_iff, image_preimage_eq_of_subset hs.2], exact hs.1.2 end protected lemma is_open.locally_compact_space [locally_compact_space Ξ±] {s : set Ξ±} (hs : is_open s) : locally_compact_space s := hs.open_embedding_subtype_coe.locally_compact_space lemma ultrafilter.le_nhds_Lim [compact_space Ξ±] (F : ultrafilter Ξ±) : ↑F ≀ 𝓝 (@Lim _ _ (F : filter Ξ±).nonempty_of_ne_bot F) := begin rcases is_compact_univ.ultrafilter_le_nhds F (by simp) with ⟨x, -, h⟩, exact le_nhds_Lim ⟨x,h⟩, end theorem is_closed.exists_minimal_nonempty_closed_subset [compact_space Ξ±] {S : set Ξ±} (hS : is_closed S) (hne : S.nonempty) : βˆƒ (V : set Ξ±), V βŠ† S ∧ V.nonempty ∧ is_closed V ∧ (βˆ€ (V' : set Ξ±), V' βŠ† V β†’ V'.nonempty β†’ is_closed V' β†’ V' = V) := begin let opens := {U : set Ξ± | Sᢜ βŠ† U ∧ is_open U ∧ Uᢜ.nonempty}, obtain ⟨U, ⟨Uc, Uo, Ucne⟩, h⟩ := zorn_subset opens (Ξ» c hc hz, begin by_cases hcne : c.nonempty, { obtain ⟨Uβ‚€, hUβ‚€βŸ© := hcne, haveI : nonempty {U // U ∈ c} := ⟨⟨Uβ‚€, hUβ‚€βŸ©βŸ©, obtain ⟨Uβ‚€compl, Uβ‚€opn, Uβ‚€ne⟩ := hc hUβ‚€, use ⋃₀ c, refine ⟨⟨_, _, _⟩, Ξ» U hU a ha, ⟨U, hU, ha⟩⟩, { exact Ξ» a ha, ⟨Uβ‚€, hUβ‚€, Uβ‚€compl ha⟩ }, { exact is_open_sUnion (Ξ» _ h, (hc h).2.1) }, { convert_to (β‹‚(U : {U // U ∈ c}), U.1ᢜ).nonempty, { ext, simp only [not_exists, exists_prop, not_and, set.mem_Inter, subtype.forall, mem_set_of_eq, mem_compl_iff, mem_sUnion] }, apply is_compact.nonempty_Inter_of_directed_nonempty_compact_closed, { rintros ⟨U, hU⟩ ⟨U', hU'⟩, obtain ⟨V, hVc, hVU, hVU'⟩ := hz.directed_on U hU U' hU', exact ⟨⟨V, hVc⟩, set.compl_subset_compl.mpr hVU, set.compl_subset_compl.mpr hVU'⟩, }, { exact Ξ» U, (hc U.2).2.2, }, { exact Ξ» U, (is_closed_compl_iff.mpr (hc U.2).2.1).is_compact, }, { exact Ξ» U, (is_closed_compl_iff.mpr (hc U.2).2.1), } } }, { use Sᢜ, refine ⟨⟨set.subset.refl _, is_open_compl_iff.mpr hS, _⟩, Ξ» U Uc, (hcne ⟨U, Uc⟩).elim⟩, rw compl_compl, exact hne, } end), refine ⟨Uᢜ, set.compl_subset_comm.mp Uc, Ucne, is_closed_compl_iff.mpr Uo, _⟩, intros V' V'sub V'ne V'cls, have : V'ᢜ = U, { refine h V'ᢜ ⟨_, is_open_compl_iff.mpr V'cls, _⟩ (set.subset_compl_comm.mp V'sub), exact set.subset.trans Uc (set.subset_compl_comm.mp V'sub), simp only [compl_compl, V'ne], }, rw [←this, compl_compl], end /-- A Οƒ-compact space is a space that is the union of a countable collection of compact subspaces. Note that a locally compact separable Tβ‚‚ space need not be Οƒ-compact. The sequence can be extracted using `topological_space.compact_covering`. -/ class sigma_compact_space (Ξ± : Type*) [topological_space Ξ±] : Prop := (exists_compact_covering : βˆƒ K : β„• β†’ set Ξ±, (βˆ€ n, is_compact (K n)) ∧ (⋃ n, K n) = univ) @[priority 200] -- see Note [lower instance priority] instance compact_space.sigma_compact [compact_space Ξ±] : sigma_compact_space Ξ± := ⟨⟨λ _, univ, Ξ» _, is_compact_univ, Union_const _⟩⟩ lemma sigma_compact_space.of_countable (S : set (set Ξ±)) (Hc : S.countable) (Hcomp : βˆ€ s ∈ S, is_compact s) (HU : ⋃₀ S = univ) : sigma_compact_space Ξ± := ⟨(exists_seq_cover_iff_countable ⟨_, is_compact_empty⟩).2 ⟨S, Hc, Hcomp, HU⟩⟩ @[priority 100] -- see Note [lower instance priority] instance sigma_compact_space_of_locally_compact_second_countable [locally_compact_space Ξ±] [second_countable_topology Ξ±] : sigma_compact_space Ξ± := begin choose K hKc hxK using Ξ» x : Ξ±, exists_compact_mem_nhds x, rcases countable_cover_nhds hxK with ⟨s, hsc, hsU⟩, refine sigma_compact_space.of_countable _ (hsc.image K) (ball_image_iff.2 $ Ξ» x _, hKc x) _, rwa sUnion_image end variables (Ξ±) [sigma_compact_space Ξ±] open sigma_compact_space /-- A choice of compact covering for a `Οƒ`-compact space, chosen to be monotone. -/ def compact_covering : β„• β†’ set Ξ± := accumulate exists_compact_covering.some lemma is_compact_compact_covering (n : β„•) : is_compact (compact_covering Ξ± n) := is_compact_accumulate (classical.some_spec sigma_compact_space.exists_compact_covering).1 n lemma Union_compact_covering : (⋃ n, compact_covering Ξ± n) = univ := begin rw [compact_covering, Union_accumulate], exact (classical.some_spec sigma_compact_space.exists_compact_covering).2 end @[mono] lemma compact_covering_subset ⦃m n : ℕ⦄ (h : m ≀ n) : compact_covering Ξ± m βŠ† compact_covering Ξ± n := monotone_accumulate h variable {Ξ±} lemma exists_mem_compact_covering (x : Ξ±) : βˆƒ n, x ∈ compact_covering Ξ± n := Union_eq_univ_iff.mp (Union_compact_covering Ξ±) x /-- If `Ξ±` is a `Οƒ`-compact space, then a locally finite family of nonempty sets of `Ξ±` can have only countably many elements, `set.countable` version. -/ protected lemma locally_finite.countable_univ {ΞΉ : Type*} {f : ΞΉ β†’ set Ξ±} (hf : locally_finite f) (hne : βˆ€ i, (f i).nonempty) : (univ : set ΞΉ).countable := begin have := Ξ» n, hf.finite_nonempty_inter_compact (is_compact_compact_covering Ξ± n), refine (countable_Union (Ξ» n, (this n).countable)).mono (Ξ» i hi, _), rcases hne i with ⟨x, hx⟩, rcases Union_eq_univ_iff.1 (Union_compact_covering Ξ±) x with ⟨n, hn⟩, exact mem_Union.2 ⟨n, x, hx, hn⟩ end /-- If `f : ΞΉ β†’ set Ξ±` is a locally finite covering of a Οƒ-compact topological space by nonempty sets, then the index type `ΞΉ` is encodable. -/ protected noncomputable def locally_finite.encodable {ΞΉ : Type*} {f : ΞΉ β†’ set Ξ±} (hf : locally_finite f) (hne : βˆ€ i, (f i).nonempty) : encodable ΞΉ := @encodable.of_equiv _ _ (hf.countable_univ hne).to_encodable (equiv.set.univ _).symm /-- In a topological space with sigma compact topology, if `f` is a function that sends each point `x` of a closed set `s` to a neighborhood of `x` within `s`, then for some countable set `t βŠ† s`, the neighborhoods `f x`, `x ∈ t`, cover the whole set `s`. -/ lemma countable_cover_nhds_within_of_sigma_compact {f : Ξ± β†’ set Ξ±} {s : set Ξ±} (hs : is_closed s) (hf : βˆ€ x ∈ s, f x ∈ 𝓝[s] x) : βˆƒ t βŠ† s, t.countable ∧ s βŠ† ⋃ x ∈ t, f x := begin simp only [nhds_within, mem_inf_principal] at hf, choose t ht hsub using Ξ» n, ((is_compact_compact_covering Ξ± n).inter_right hs).elim_nhds_subcover _ (Ξ» x hx, hf x hx.right), refine βŸ¨β‹ƒ n, (t n : set Ξ±), Union_subset $ Ξ» n x hx, (ht n x hx).2, countable_Union $ Ξ» n, (t n).countable_to_set, Ξ» x hx, mem_Unionβ‚‚.2 _⟩, rcases exists_mem_compact_covering x with ⟨n, hn⟩, rcases mem_Unionβ‚‚.1 (hsub n ⟨hn, hx⟩) with ⟨y, hyt : y ∈ t n, hyf : x ∈ s β†’ x ∈ f y⟩, exact ⟨y, mem_Union.2 ⟨n, hyt⟩, hyf hx⟩ end /-- In a topological space with sigma compact topology, if `f` is a function that sends each point `x` to a neighborhood of `x`, then for some countable set `s`, the neighborhoods `f x`, `x ∈ s`, cover the whole space. -/ lemma countable_cover_nhds_of_sigma_compact {f : Ξ± β†’ set Ξ±} (hf : βˆ€ x, f x ∈ 𝓝 x) : βˆƒ s : set Ξ±, s.countable ∧ (⋃ x ∈ s, f x) = univ := begin simp only [← nhds_within_univ] at hf, rcases countable_cover_nhds_within_of_sigma_compact is_closed_univ (Ξ» x _, hf x) with ⟨s, -, hsc, hsU⟩, exact ⟨s, hsc, univ_subset_iff.1 hsU⟩ end end compact /-- An [exhaustion by compact sets](https://en.wikipedia.org/wiki/Exhaustion_by_compact_sets) of a topological space is a sequence of compact sets `K n` such that `K n βŠ† interior (K (n + 1))` and `(⋃ n, K n) = univ`. If `X` is a locally compact sigma compact space, then `compact_exhaustion.choice X` provides a choice of an exhaustion by compact sets. This choice is also available as `(default : compact_exhaustion X)`. -/ structure compact_exhaustion (X : Type*) [topological_space X] := (to_fun : β„• β†’ set X) (is_compact' : βˆ€ n, is_compact (to_fun n)) (subset_interior_succ' : βˆ€ n, to_fun n βŠ† interior (to_fun (n + 1))) (Union_eq' : (⋃ n, to_fun n) = univ) namespace compact_exhaustion instance : has_coe_to_fun (compact_exhaustion Ξ±) (Ξ» _, β„• β†’ set Ξ±) := ⟨to_fun⟩ variables {Ξ±} (K : compact_exhaustion Ξ±) protected lemma is_compact (n : β„•) : is_compact (K n) := K.is_compact' n lemma subset_interior_succ (n : β„•) : K n βŠ† interior (K (n + 1)) := K.subset_interior_succ' n lemma subset_succ (n : β„•) : K n βŠ† K (n + 1) := subset.trans (K.subset_interior_succ n) interior_subset @[mono] protected lemma subset ⦃m n : ℕ⦄ (h : m ≀ n) : K m βŠ† K n := show K m ≀ K n, from monotone_nat_of_le_succ K.subset_succ h lemma subset_interior ⦃m n : ℕ⦄ (h : m < n) : K m βŠ† interior (K n) := subset.trans (K.subset_interior_succ m) $ interior_mono $ K.subset h lemma Union_eq : (⋃ n, K n) = univ := K.Union_eq' lemma exists_mem (x : Ξ±) : βˆƒ n, x ∈ K n := Union_eq_univ_iff.1 K.Union_eq x /-- The minimal `n` such that `x ∈ K n`. -/ protected noncomputable def find (x : Ξ±) : β„• := nat.find (K.exists_mem x) lemma mem_find (x : Ξ±) : x ∈ K (K.find x) := nat.find_spec (K.exists_mem x) lemma mem_iff_find_le {x : Ξ±} {n : β„•} : x ∈ K n ↔ K.find x ≀ n := ⟨λ h, nat.find_min' (K.exists_mem x) h, Ξ» h, K.subset h $ K.mem_find x⟩ /-- Prepend the empty set to a compact exhaustion `K n`. -/ def shiftr : compact_exhaustion Ξ± := { to_fun := Ξ» n, nat.cases_on n βˆ… K, is_compact' := Ξ» n, nat.cases_on n is_compact_empty K.is_compact, subset_interior_succ' := Ξ» n, nat.cases_on n (empty_subset _) K.subset_interior_succ, Union_eq' := Union_eq_univ_iff.2 $ Ξ» x, ⟨K.find x + 1, K.mem_find x⟩ } @[simp] lemma find_shiftr (x : Ξ±) : K.shiftr.find x = K.find x + 1 := nat.find_comp_succ _ _ (not_mem_empty _) lemma mem_diff_shiftr_find (x : Ξ±) : x ∈ K.shiftr (K.find x + 1) \ K.shiftr (K.find x) := ⟨K.mem_find _, mt K.shiftr.mem_iff_find_le.1 $ by simp only [find_shiftr, not_le, nat.lt_succ_self]⟩ /-- A choice of an [exhaustion by compact sets](https://en.wikipedia.org/wiki/Exhaustion_by_compact_sets) of a locally compact sigma compact space. -/ noncomputable def choice (X : Type*) [topological_space X] [locally_compact_space X] [sigma_compact_space X] : compact_exhaustion X := begin apply classical.choice, let K : β„• β†’ {s : set X // is_compact s} := Ξ» n, nat.rec_on n βŸ¨βˆ…, is_compact_empty⟩ (Ξ» n s, ⟨(exists_compact_superset s.2).some βˆͺ compact_covering X n, (exists_compact_superset s.2).some_spec.1.union (is_compact_compact_covering _ _)⟩), refine ⟨⟨λ n, K n, Ξ» n, (K n).2, Ξ» n, _, _⟩⟩, { exact subset.trans (exists_compact_superset (K n).2).some_spec.2 (interior_mono $ subset_union_left _ _) }, { refine univ_subset_iff.1 (Union_compact_covering X β–Έ _), exact Union_mono' (Ξ» n, ⟨n + 1, subset_union_right _ _⟩) } end noncomputable instance [locally_compact_space Ξ±] [sigma_compact_space Ξ±] : inhabited (compact_exhaustion Ξ±) := ⟨compact_exhaustion.choice α⟩ end compact_exhaustion section clopen /-- A set is clopen if it is both open and closed. -/ def is_clopen (s : set Ξ±) : Prop := is_open s ∧ is_closed s protected lemma is_clopen.is_open (hs : is_clopen s) : is_open s := hs.1 protected lemma is_clopen.is_closed (hs : is_clopen s) : is_closed s := hs.2 lemma is_clopen_iff_frontier_eq_empty {s : set Ξ±} : is_clopen s ↔ frontier s = βˆ… := begin rw [is_clopen, ← closure_eq_iff_is_closed, ← interior_eq_iff_is_open, frontier, diff_eq_empty], refine ⟨λ h, (h.2.trans h.1.symm).subset, Ξ» h, _⟩, exact ⟨interior_subset.antisymm (subset_closure.trans h), (h.trans interior_subset).antisymm subset_closure⟩ end alias is_clopen_iff_frontier_eq_empty ↔ is_clopen.frontier_eq _ theorem is_clopen.union {s t : set Ξ±} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s βˆͺ t) := ⟨hs.1.union ht.1, hs.2.union ht.2⟩ theorem is_clopen.inter {s t : set Ξ±} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∩ t) := ⟨hs.1.inter ht.1, hs.2.inter ht.2⟩ @[simp] theorem is_clopen_empty : is_clopen (βˆ… : set Ξ±) := ⟨is_open_empty, is_closed_empty⟩ @[simp] theorem is_clopen_univ : is_clopen (univ : set Ξ±) := ⟨is_open_univ, is_closed_univ⟩ theorem is_clopen.compl {s : set Ξ±} (hs : is_clopen s) : is_clopen sᢜ := ⟨hs.2.is_open_compl, hs.1.is_closed_compl⟩ @[simp] theorem is_clopen_compl_iff {s : set Ξ±} : is_clopen sᢜ ↔ is_clopen s := ⟨λ h, compl_compl s β–Έ is_clopen.compl h, is_clopen.compl⟩ theorem is_clopen.diff {s t : set Ξ±} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s \ t) := hs.inter ht.compl lemma is_clopen.prod {s : set Ξ±} {t : set Ξ²} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s Γ—Λ’ t) := ⟨hs.1.prod ht.1, hs.2.prod ht.2⟩ lemma is_clopen_Union {Ξ² : Type*} [finite Ξ²] {s : Ξ² β†’ set Ξ±} (h : βˆ€ i, is_clopen (s i)) : is_clopen (⋃ i, s i) := ⟨is_open_Union (forall_and_distrib.1 h).1, is_closed_Union (forall_and_distrib.1 h).2⟩ lemma is_clopen_bUnion {Ξ² : Type*} {s : set Ξ²} {f : Ξ² β†’ set Ξ±} (hs : s.finite) (h : βˆ€ i ∈ s, is_clopen $ f i) : is_clopen (⋃ i ∈ s, f i) := ⟨is_open_bUnion (Ξ» i hi, (h i hi).1), is_closed_bUnion hs (Ξ» i hi, (h i hi).2)⟩ lemma is_clopen_bUnion_finset {Ξ² : Type*} {s : finset Ξ²} {f : Ξ² β†’ set Ξ±} (h : βˆ€ i ∈ s, is_clopen $ f i) : is_clopen (⋃ i ∈ s, f i) := is_clopen_bUnion s.finite_to_set h lemma is_clopen_Inter {Ξ² : Type*} [finite Ξ²] {s : Ξ² β†’ set Ξ±} (h : βˆ€ i, is_clopen (s i)) : is_clopen (β‹‚ i, s i) := ⟨(is_open_Inter (forall_and_distrib.1 h).1), (is_closed_Inter (forall_and_distrib.1 h).2)⟩ lemma is_clopen_bInter {Ξ² : Type*} {s : set Ξ²} (hs : s.finite) {f : Ξ² β†’ set Ξ±} (h : βˆ€ i ∈ s, is_clopen (f i)) : is_clopen (β‹‚ i ∈ s, f i) := ⟨is_open_bInter hs (Ξ» i hi, (h i hi).1), is_closed_bInter (Ξ» i hi, (h i hi).2)⟩ lemma is_clopen_bInter_finset {Ξ² : Type*} {s : finset Ξ²} {f : Ξ² β†’ set Ξ±} (h : βˆ€ i ∈ s, is_clopen (f i)) : is_clopen (β‹‚ i ∈ s, f i) := is_clopen_bInter s.finite_to_set h lemma is_clopen.preimage {s : set Ξ²} (h : is_clopen s) {f : Ξ± β†’ Ξ²} (hf : continuous f) : is_clopen (f ⁻¹' s) := ⟨h.1.preimage hf, h.2.preimage hf⟩ lemma continuous_on.preimage_clopen_of_clopen {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (hf : continuous_on f s) (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∩ f⁻¹' t) := ⟨continuous_on.preimage_open_of_open hf hs.1 ht.1, continuous_on.preimage_closed_of_closed hf hs.2 ht.2⟩ /-- The intersection of a disjoint covering by two open sets of a clopen set will be clopen. -/ theorem is_clopen_inter_of_disjoint_cover_clopen {Z a b : set Ξ±} (h : is_clopen Z) (cover : Z βŠ† a βˆͺ b) (ha : is_open a) (hb : is_open b) (hab : disjoint a b) : is_clopen (Z ∩ a) := begin refine ⟨is_open.inter h.1 ha, _⟩, have : is_closed (Z ∩ bᢜ) := is_closed.inter h.2 (is_closed_compl_iff.2 hb), convert this using 1, refine (inter_subset_inter_right Z hab.subset_compl_right).antisymm _, rintro x ⟨hx₁, hxβ‚‚βŸ©, exact ⟨hx₁, by simpa [not_mem_of_mem_compl hxβ‚‚] using cover hxβ‚βŸ©, end @[simp] lemma is_clopen_discrete [discrete_topology Ξ±] (x : set Ξ±) : is_clopen x := ⟨is_open_discrete _, is_closed_discrete _⟩ lemma clopen_range_sigma_mk {ΞΉ : Type*} {Οƒ : ΞΉ β†’ Type*} [Ξ  i, topological_space (Οƒ i)] {i : ΞΉ} : is_clopen (set.range (@sigma.mk ΞΉ Οƒ i)) := ⟨open_embedding_sigma_mk.open_range, closed_embedding_sigma_mk.closed_range⟩ protected lemma quotient_map.is_clopen_preimage {f : Ξ± β†’ Ξ²} (hf : quotient_map f) {s : set Ξ²} : is_clopen (f ⁻¹' s) ↔ is_clopen s := and_congr hf.is_open_preimage hf.is_closed_preimage variables {X : Type*} [topological_space X] lemma continuous_bool_indicator_iff_clopen (U : set X) : continuous U.bool_indicator ↔ is_clopen U := begin split, { intros hc, rw ← U.preimage_bool_indicator_tt, exact ⟨hc.is_open_preimage _ trivial, continuous_iff_is_closed.mp hc _ (is_closed_discrete _)⟩ }, { refine Ξ» hU, ⟨λ s hs, _⟩, rcases U.preimage_bool_indicator s with (h|h|h|h) ; rw h, exacts [is_open_univ, hU.1, hU.2.is_open_compl, is_open_empty] }, end lemma continuous_on_indicator_iff_clopen (s U : set X) : continuous_on U.bool_indicator s ↔ is_clopen ((coe : s β†’ X) ⁻¹' U) := begin rw [continuous_on_iff_continuous_restrict, ← continuous_bool_indicator_iff_clopen], refl end end clopen section preirreducible /-- A preirreducible set `s` is one where there is no non-trivial pair of disjoint opens on `s`. -/ def is_preirreducible (s : set Ξ±) : Prop := βˆ€ (u v : set Ξ±), is_open u β†’ is_open v β†’ (s ∩ u).nonempty β†’ (s ∩ v).nonempty β†’ (s ∩ (u ∩ v)).nonempty /-- An irreducible set `s` is one that is nonempty and where there is no non-trivial pair of disjoint opens on `s`. -/ def is_irreducible (s : set Ξ±) : Prop := s.nonempty ∧ is_preirreducible s lemma is_irreducible.nonempty {s : set Ξ±} (h : is_irreducible s) : s.nonempty := h.1 lemma is_irreducible.is_preirreducible {s : set Ξ±} (h : is_irreducible s) : is_preirreducible s := h.2 theorem is_preirreducible_empty : is_preirreducible (βˆ… : set Ξ±) := Ξ» _ _ _ _ _ ⟨x, h1, h2⟩, h1.elim lemma set.subsingleton.is_preirreducible {s : set Ξ±} (hs : s.subsingleton) : is_preirreducible s := Ξ» u v hu hv ⟨x, hxs, hxu⟩ ⟨y, hys, hyv⟩, ⟨y, hys, hs hxs hys β–Έ hxu, hyv⟩ theorem is_irreducible_singleton {x} : is_irreducible ({x} : set Ξ±) := ⟨singleton_nonempty x, subsingleton_singleton.is_preirreducible⟩ theorem is_preirreducible_iff_closure {s : set Ξ±} : is_preirreducible (closure s) ↔ is_preirreducible s := forallβ‚„_congr $ Ξ» u v hu hv, by { iterate 3 { rw closure_inter_open_nonempty_iff }, exacts [hu.inter hv, hv, hu] } theorem is_irreducible_iff_closure {s : set Ξ±} : is_irreducible (closure s) ↔ is_irreducible s := and_congr closure_nonempty_iff is_preirreducible_iff_closure alias is_preirreducible_iff_closure ↔ _ is_preirreducible.closure alias is_irreducible_iff_closure ↔ _ is_irreducible.closure theorem exists_preirreducible (s : set Ξ±) (H : is_preirreducible s) : βˆƒ t : set Ξ±, is_preirreducible t ∧ s βŠ† t ∧ βˆ€ u, is_preirreducible u β†’ t βŠ† u β†’ u = t := let ⟨m, hm, hsm, hmm⟩ := zorn_subset_nonempty {t : set Ξ± | is_preirreducible t} (Ξ» c hc hcc hcn, let ⟨t, htc⟩ := hcn in βŸ¨β‹ƒβ‚€ c, Ξ» u v hu hv ⟨y, hy, hyu⟩ ⟨z, hz, hzv⟩, let ⟨p, hpc, hyp⟩ := mem_sUnion.1 hy, ⟨q, hqc, hzq⟩ := mem_sUnion.1 hz in or.cases_on (hcc.total hpc hqc) (assume hpq : p βŠ† q, let ⟨x, hxp, hxuv⟩ := hc hqc u v hu hv ⟨y, hpq hyp, hyu⟩ ⟨z, hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hqc, hxuv⟩) (assume hqp : q βŠ† p, let ⟨x, hxp, hxuv⟩ := hc hpc u v hu hv ⟨y, hyp, hyu⟩ ⟨z, hqp hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hpc, hxuv⟩), Ξ» x hxc, subset_sUnion_of_mem hxc⟩) s H in ⟨m, hm, hsm, Ξ» u hu hmu, hmm _ hu hmu⟩ /-- The set of irreducible components of a topological space. -/ def irreducible_components (Ξ± : Type*) [topological_space Ξ±] : set (set Ξ±) := maximals (≀) { s : set Ξ± | is_irreducible s } lemma is_closed_of_mem_irreducible_components (s ∈ irreducible_components Ξ±) : is_closed s := begin rw [← closure_eq_iff_is_closed, eq_comm], exact subset_closure.antisymm (H.2 H.1.closure subset_closure), end lemma irreducible_components_eq_maximals_closed (Ξ± : Type*) [topological_space Ξ±] : irreducible_components Ξ± = maximals (≀) { s : set Ξ± | is_closed s ∧ is_irreducible s } := begin ext s, split, { intro H, exact ⟨⟨is_closed_of_mem_irreducible_components _ H, H.1⟩, Ξ» x h e, H.2 h.2 e⟩ }, { intro H, refine ⟨H.1.2, Ξ» x h e, _⟩, have : closure x ≀ s, { exact H.2 ⟨is_closed_closure, h.closure⟩ (e.trans subset_closure) }, exact le_trans subset_closure this } end /-- A maximal irreducible set that contains a given point. -/ def irreducible_component (x : Ξ±) : set Ξ± := classical.some (exists_preirreducible {x} is_irreducible_singleton.is_preirreducible) lemma irreducible_component_property (x : Ξ±) : is_preirreducible (irreducible_component x) ∧ {x} βŠ† (irreducible_component x) ∧ βˆ€ u, is_preirreducible u β†’ (irreducible_component x) βŠ† u β†’ u = (irreducible_component x) := classical.some_spec (exists_preirreducible {x} is_irreducible_singleton.is_preirreducible) theorem mem_irreducible_component {x : Ξ±} : x ∈ irreducible_component x := singleton_subset_iff.1 (irreducible_component_property x).2.1 theorem is_irreducible_irreducible_component {x : Ξ±} : is_irreducible (irreducible_component x) := ⟨⟨x, mem_irreducible_component⟩, (irreducible_component_property x).1⟩ theorem eq_irreducible_component {x : Ξ±} : βˆ€ {s : set Ξ±}, is_preirreducible s β†’ irreducible_component x βŠ† s β†’ s = irreducible_component x := (irreducible_component_property x).2.2 lemma irreducible_component_mem_irreducible_components (x : Ξ±) : irreducible_component x ∈ irreducible_components Ξ± := ⟨is_irreducible_irreducible_component, Ξ» s h₁ hβ‚‚,(eq_irreducible_component h₁.2 hβ‚‚).le⟩ theorem is_closed_irreducible_component {x : Ξ±} : is_closed (irreducible_component x) := is_closed_of_mem_irreducible_components _ (irreducible_component_mem_irreducible_components x) /-- A preirreducible space is one where there is no non-trivial pair of disjoint opens. -/ class preirreducible_space (Ξ± : Type u) [topological_space Ξ±] : Prop := (is_preirreducible_univ [] : is_preirreducible (univ : set Ξ±)) /-- An irreducible space is one that is nonempty and where there is no non-trivial pair of disjoint opens. -/ class irreducible_space (Ξ± : Type u) [topological_space Ξ±] extends preirreducible_space Ξ± : Prop := (to_nonempty [] : nonempty Ξ±) -- see Note [lower instance priority] attribute [instance, priority 50] irreducible_space.to_nonempty lemma irreducible_space.is_irreducible_univ (Ξ± : Type u) [topological_space Ξ±] [irreducible_space Ξ±] : is_irreducible (⊀ : set Ξ±) := ⟨by simp, preirreducible_space.is_preirreducible_univ α⟩ lemma irreducible_space_def (Ξ± : Type u) [topological_space Ξ±] : irreducible_space Ξ± ↔ is_irreducible (⊀ : set Ξ±) := ⟨@@irreducible_space.is_irreducible_univ Ξ± _, Ξ» h, by { haveI : preirreducible_space Ξ± := ⟨h.2⟩, exact ⟨⟨h.1.some⟩⟩ }⟩ theorem nonempty_preirreducible_inter [preirreducible_space Ξ±] {s t : set Ξ±} : is_open s β†’ is_open t β†’ s.nonempty β†’ t.nonempty β†’ (s ∩ t).nonempty := by simpa only [univ_inter, univ_subset_iff] using @preirreducible_space.is_preirreducible_univ Ξ± _ _ s t /-- In a (pre)irreducible space, a nonempty open set is dense. -/ protected theorem is_open.dense [preirreducible_space Ξ±] {s : set Ξ±} (ho : is_open s) (hne : s.nonempty) : dense s := dense_iff_inter_open.2 $ Ξ» t hto htne, nonempty_preirreducible_inter hto ho htne hne theorem is_preirreducible.image {s : set Ξ±} (H : is_preirreducible s) (f : Ξ± β†’ Ξ²) (hf : continuous_on f s) : is_preirreducible (f '' s) := begin rintros u v hu hv ⟨_, ⟨⟨x, hx, rfl⟩, hxu⟩⟩ ⟨_, ⟨⟨y, hy, rfl⟩, hyv⟩⟩, rw ← mem_preimage at hxu hyv, rcases continuous_on_iff'.1 hf u hu with ⟨u', hu', u'_eq⟩, rcases continuous_on_iff'.1 hf v hv with ⟨v', hv', v'_eq⟩, have := H u' v' hu' hv', rw [inter_comm s u', ← u'_eq] at this, rw [inter_comm s v', ← v'_eq] at this, rcases this ⟨x, hxu, hx⟩ ⟨y, hyv, hy⟩ with ⟨z, hzs, hzu', hzv'⟩, refine ⟨f z, mem_image_of_mem f hzs, _, _⟩, all_goals { rw ← mem_preimage, apply mem_of_mem_inter_left, show z ∈ _ ∩ s, simp [*] } end theorem is_irreducible.image {s : set Ξ±} (H : is_irreducible s) (f : Ξ± β†’ Ξ²) (hf : continuous_on f s) : is_irreducible (f '' s) := ⟨H.nonempty.image _, H.is_preirreducible.image f hf⟩ lemma subtype.preirreducible_space {s : set Ξ±} (h : is_preirreducible s) : preirreducible_space s := { is_preirreducible_univ := begin intros u v hu hv hsu hsv, rw is_open_induced_iff at hu hv, rcases hu with ⟨u, hu, rfl⟩, rcases hv with ⟨v, hv, rfl⟩, rcases hsu with ⟨⟨x, hxs⟩, hxs', hxu⟩, rcases hsv with ⟨⟨y, hys⟩, hys', hyv⟩, rcases h u v hu hv ⟨x, hxs, hxu⟩ ⟨y, hys, hyv⟩ with ⟨z, hzs, ⟨hzu, hzv⟩⟩, exact ⟨⟨z, hzs⟩, ⟨set.mem_univ _, ⟨hzu, hzv⟩⟩⟩ end } lemma subtype.irreducible_space {s : set Ξ±} (h : is_irreducible s) : irreducible_space s := { is_preirreducible_univ := (subtype.preirreducible_space h.is_preirreducible).is_preirreducible_univ, to_nonempty := h.nonempty.to_subtype } /-- An infinite type with cofinite topology is an irreducible topological space. -/ @[priority 100] instance {Ξ±} [infinite Ξ±] : irreducible_space (cofinite_topology Ξ±) := { is_preirreducible_univ := Ξ» u v, begin haveI : infinite (cofinite_topology Ξ±) := β€Ή_β€Ί, simp only [cofinite_topology.is_open_iff, univ_inter], intros hu hv hu' hv', simpa only [compl_union, compl_compl] using ((hu hu').union (hv hv')).infinite_compl.nonempty end, to_nonempty := (infer_instance : nonempty Ξ±) } /-- A set `s` is irreducible if and only if for every finite collection of open sets all of whose members intersect `s`, `s` also intersects the intersection of the entire collection (i.e., there is an element of `s` contained in every member of the collection). -/ lemma is_irreducible_iff_sInter {s : set Ξ±} : is_irreducible s ↔ βˆ€ (U : finset (set Ξ±)) (hU : βˆ€ u ∈ U, is_open u) (H : βˆ€ u ∈ U, (s ∩ u).nonempty), (s ∩ β‹‚β‚€ ↑U).nonempty := begin split; intro h, { intro U, apply finset.induction_on U, { intros, simpa using h.nonempty }, { intros u U hu IH hU H, rw [finset.coe_insert, sInter_insert], apply h.2, { solve_by_elim [finset.mem_insert_self] }, { apply is_open_sInter (finset.finite_to_set U), intros, solve_by_elim [finset.mem_insert_of_mem] }, { solve_by_elim [finset.mem_insert_self] }, { apply IH, all_goals { intros, solve_by_elim [finset.mem_insert_of_mem] } } } }, { split, { simpa using h βˆ… _ _; intro u; simp }, intros u v hu hv hu' hv', simpa using h {u,v} _ _, all_goals { intro t, rw [finset.mem_insert, finset.mem_singleton], rintro (rfl|rfl); assumption } } end /-- A set is preirreducible if and only if for every cover by two closed sets, it is contained in one of the two covering sets. -/ lemma is_preirreducible_iff_closed_union_closed {s : set Ξ±} : is_preirreducible s ↔ βˆ€ (z₁ zβ‚‚ : set Ξ±), is_closed z₁ β†’ is_closed zβ‚‚ β†’ s βŠ† z₁ βˆͺ zβ‚‚ β†’ s βŠ† z₁ ∨ s βŠ† zβ‚‚ := begin split, all_goals { intros h t₁ tβ‚‚ ht₁ htβ‚‚, specialize h tβ‚αΆœ tβ‚‚αΆœ, simp only [is_open_compl_iff, is_closed_compl_iff] at h, specialize h ht₁ htβ‚‚ }, { contrapose!, simp only [not_subset], rintro ⟨⟨x, hx, hx'⟩, ⟨y, hy, hy'⟩⟩, rcases h ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩ with ⟨z, hz, hz'⟩, rw ← compl_union at hz', exact ⟨z, hz, hz'⟩ }, { rintro ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩, rw ← compl_inter at h, delta set.nonempty, rw imp_iff_not_or at h, contrapose! h, split, { intros z hz hz', exact h z ⟨hz, hz'⟩ }, { split; intro H; refine H _ β€Ή_β€Ί; assumption } } end /-- A set is irreducible if and only if for every cover by a finite collection of closed sets, it is contained in one of the members of the collection. -/ lemma is_irreducible_iff_sUnion_closed {s : set Ξ±} : is_irreducible s ↔ βˆ€ (Z : finset (set Ξ±)) (hZ : βˆ€ z ∈ Z, is_closed z) (H : s βŠ† ⋃₀ ↑Z), βˆƒ z ∈ Z, s βŠ† z := begin rw [is_irreducible, is_preirreducible_iff_closed_union_closed], split; intro h, { intro Z, apply finset.induction_on Z, { intros, rw [finset.coe_empty, sUnion_empty] at H, rcases h.1 with ⟨x, hx⟩, exfalso, tauto }, { intros z Z hz IH hZ H, cases h.2 z (⋃₀ ↑Z) _ _ _ with h' h', { exact ⟨z, finset.mem_insert_self _ _, h'⟩ }, { rcases IH _ h' with ⟨z', hz', hsz'⟩, { exact ⟨z', finset.mem_insert_of_mem hz', hsz'⟩ }, { intros, solve_by_elim [finset.mem_insert_of_mem] } }, { solve_by_elim [finset.mem_insert_self] }, { rw sUnion_eq_bUnion, apply is_closed_bUnion (finset.finite_to_set Z), { intros, solve_by_elim [finset.mem_insert_of_mem] } }, { simpa using H } } }, { split, { by_contradiction hs, simpa using h βˆ… _ _, { intro z, simp }, { simpa [set.nonempty] using hs } }, intros z₁ zβ‚‚ hz₁ hzβ‚‚ H, have := h {z₁, zβ‚‚} _ _, simp only [exists_prop, finset.mem_insert, finset.mem_singleton] at this, { rcases this with ⟨z, rfl|rfl, hz⟩; tauto }, { intro t, rw [finset.mem_insert, finset.mem_singleton], rintro (rfl|rfl); assumption }, { simpa using H } } end /-- A nonemtpy open subset of a preirreducible subspace is dense in the subspace. -/ lemma subset_closure_inter_of_is_preirreducible_of_is_open {S U : set Ξ±} (hS : is_preirreducible S) (hU : is_open U) (h : (S ∩ U).nonempty) : S βŠ† closure (S ∩ U) := begin by_contra h', obtain ⟨x, h₁, hβ‚‚, hβ‚ƒβŸ© := hS _ (closure (S ∩ U))ᢜ hU (is_open_compl_iff.mpr is_closed_closure) h (set.inter_compl_nonempty_iff.mpr h'), exact h₃ (subset_closure ⟨h₁, hβ‚‚βŸ©) end /-- If `βˆ… β‰  U βŠ† S βŠ† Z` such that `U` is open and `Z` is preirreducible, then `S` is irreducible. -/ lemma is_preirreducible.subset_irreducible {S U Z : set Ξ±} (hZ : is_preirreducible Z) (hU : U.nonempty) (hU' : is_open U) (h₁ : U βŠ† S) (hβ‚‚ : S βŠ† Z) : is_irreducible S := begin classical, obtain ⟨z, hz⟩ := hU, replace hZ : is_irreducible Z := ⟨⟨z, hβ‚‚ (h₁ hz)⟩, hZ⟩, refine ⟨⟨z, h₁ hz⟩, _⟩, rintros u v hu hv ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩, obtain ⟨a, -, ha'⟩ := is_irreducible_iff_sInter.mp hZ {U, u, v} (by tidy) _, replace ha' : a ∈ U ∧ a ∈ u ∧ a ∈ v := by simpa using ha', exact ⟨a, h₁ ha'.1, ha'.2⟩, { intros U H, simp only [finset.mem_insert, finset.mem_singleton] at H, rcases H with (rfl|rfl|rfl), exacts [⟨z, hβ‚‚ (h₁ hz), hz⟩, ⟨x, hβ‚‚ hx, hx'⟩, ⟨y, hβ‚‚ hy, hy'⟩] } end lemma is_preirreducible.open_subset {Z U : set Ξ±} (hZ : is_preirreducible Z) (hU : is_open U) (hU' : U βŠ† Z) : is_preirreducible U := U.eq_empty_or_nonempty.elim (Ξ» h, h.symm β–Έ is_preirreducible_empty) (Ξ» h, (hZ.subset_irreducible h hU (Ξ» _, id) hU').2) lemma is_preirreducible.interior {Z : set Ξ±} (hZ : is_preirreducible Z) : is_preirreducible (interior Z) := hZ.open_subset is_open_interior interior_subset lemma is_preirreducible.preimage {Z : set Ξ±} (hZ : is_preirreducible Z) {f : Ξ² β†’ Ξ±} (hf : open_embedding f) : is_preirreducible (f ⁻¹' Z) := begin rintros U V hU hV ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩, obtain ⟨_, h₁, ⟨z, hβ‚‚, rfl⟩, ⟨z', h₃, hβ‚„βŸ©βŸ© := hZ _ _ (hf.is_open_map _ hU) (hf.is_open_map _ hV) ⟨f x, hx, set.mem_image_of_mem f hx'⟩ ⟨f y, hy, set.mem_image_of_mem f hy'⟩, cases hf.inj hβ‚„, exact ⟨z, h₁, hβ‚‚, hβ‚ƒβŸ© end end preirreducible
91294c5d3d6cdf633957c7c04766ad117bc5aeb6
137c667471a40116a7afd7261f030b30180468c2
/src/analysis/mean_inequalities.lean
033a4cbfafa35cd9d9758f93c9f602775d7726c8
[ "Apache-2.0" ]
permissive
bragadeesh153/mathlib
46bf814cfb1eecb34b5d1549b9117dc60f657792
b577bb2cd1f96eb47031878256856020b76f73cd
refs/heads/master
1,687,435,188,334
1,626,384,207,000
1,626,384,207,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
33,176
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, SΓ©bastien GouΓ«zel, RΓ©my Degenne -/ import analysis.convex.specific_functions import analysis.special_functions.pow import data.real.conjugate_exponents import tactic.nth_rewrite /-! # Mean value inequalities In this file we prove several inequalities for finite sums, including AM-GM inequality, Young's inequality, HΓΆlder inequality, and Minkowski inequality. Versions for integrals of some of these inequalities are available in `measure_theory.mean_inequalities`. ## Main theorems ### AM-GM inequality: The inequality says that the geometric mean of a tuple of non-negative numbers is less than or equal to their arithmetic mean. We prove the weighted version of this inequality: if $w$ and $z$ are two non-negative vectors and $\sum_{i\in s} w_i=1$, then $$ \prod_{i\in s} z_i^{w_i} ≀ \sum_{i\in s} w_iz_i. $$ The classical version is a special case of this inequality for $w_i=\frac{1}{n}$. We prove a few versions of this inequality. Each of the following lemmas comes in two versions: a version for real-valued non-negative functions is in the `real` namespace, and a version for `nnreal`-valued functions is in the `nnreal` namespace. - `geom_mean_le_arith_mean_weighted` : weighted version for functions on `finset`s; - `geom_mean_le_arith_mean2_weighted` : weighted version for two numbers; - `geom_mean_le_arith_mean3_weighted` : weighted version for three numbers; - `geom_mean_le_arith_mean4_weighted` : weighted version for four numbers. ### Generalized mean inequality The inequality says that for two non-negative vectors $w$ and $z$ with $\sum_{i\in s} w_i=1$ and $p ≀ q$ we have $$ \sqrt[p]{\sum_{i\in s} w_i z_i^p} ≀ \sqrt[q]{\sum_{i\in s} w_i z_i^q}. $$ Currently we only prove this inequality for $p=1$. As in the rest of `mathlib`, we provide different theorems for natural exponents (`pow_arith_mean_le_arith_mean_pow`), integer exponents (`fpow_arith_mean_le_arith_mean_fpow`), and real exponents (`rpow_arith_mean_le_arith_mean_rpow` and `arith_mean_le_rpow_mean`). In the first two cases we prove $$ \left(\sum_{i\in s} w_i z_i\right)^n ≀ \sum_{i\in s} w_i z_i^n $$ in order to avoid using real exponents. For real exponents we prove both this and standard versions. ### Young's inequality Young's inequality says that for non-negative numbers `a`, `b`, `p`, `q` such that $\frac{1}{p}+\frac{1}{q}=1$ we have $$ ab ≀ \frac{a^p}{p} + \frac{b^q}{q}. $$ This inequality is a special case of the AM-GM inequality. It can be used to prove HΓΆlder's inequality (see below) but we use a different proof. ### HΓΆlder's inequality The inequality says that for two conjugate exponents `p` and `q` (i.e., for two positive numbers such that $\frac{1}{p}+\frac{1}{q}=1$) and any two non-negative vectors their inner product is less than or equal to the product of the $L_p$ norm of the first vector and the $L_q$ norm of the second vector: $$ \sum_{i\in s} a_ib_i ≀ \sqrt[p]{\sum_{i\in s} a_i^p}\sqrt[q]{\sum_{i\in s} b_i^q}. $$ We give versions of this result in `ℝ`, `ℝβ‰₯0` and `ℝβ‰₯0∞`. There are at least two short proofs of this inequality. In one proof we prenormalize both vectors, then apply Young's inequality to each $a_ib_i$. We use a different proof deducing this inequality from the generalized mean inequality for well-chosen vectors and weights. ### Minkowski's inequality The inequality says that for `p β‰₯ 1` the function $$ \|a\|_p=\sqrt[p]{\sum_{i\in s} a_i^p} $$ satisfies the triangle inequality $\|a+b\|_p\le \|a\|_p+\|b\|_p$. We give versions of this result in `real`, `ℝβ‰₯0` and `ℝβ‰₯0∞`. We deduce this inequality from HΓΆlder's inequality. Namely, HΓΆlder inequality implies that $\|a\|_p$ is the maximum of the inner product $\sum_{i\in s}a_ib_i$ over `b` such that $\|b\|_q\le 1$. Now Minkowski's inequality follows from the fact that the maximum value of the sum of two functions is less than or equal to the sum of the maximum values of the summands. ## TODO - each inequality `A ≀ B` should come with a theorem `A = B ↔ _`; one of the ways to prove them is to define `strict_convex_on` functions. - generalized mean inequality with any `p ≀ q`, including negative numbers; - prove that the power mean tends to the geometric mean as the exponent tends to zero. -/ universes u v open finset open_locale classical big_operators nnreal ennreal noncomputable theory variables {ΞΉ : Type u} (s : finset ΞΉ) namespace real /-- AM-GM inequality: the **geometric mean is less than or equal to the arithmetic mean**, weighted version for real-valued nonnegative functions. -/ theorem geom_mean_le_arith_mean_weighted (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) (hz : βˆ€ i ∈ s, 0 ≀ z i) : (∏ i in s, (z i) ^ (w i)) ≀ βˆ‘ i in s, w i * z i := begin -- If some number `z i` equals zero and has non-zero weight, then LHS is 0 and RHS is nonnegative. by_cases A : βˆƒ i ∈ s, z i = 0 ∧ w i β‰  0, { rcases A with ⟨i, his, hzi, hwi⟩, rw [prod_eq_zero his], { exact sum_nonneg (Ξ» j hj, mul_nonneg (hw j hj) (hz j hj)) }, { rw hzi, exact zero_rpow hwi } }, -- If all numbers `z i` with non-zero weight are positive, then we apply Jensen's inequality -- for `exp` and numbers `log (z i)` with weights `w i`. { simp only [not_exists, not_and, ne.def, not_not] at A, have := convex_on_exp.map_sum_le hw hw' (Ξ» i _, set.mem_univ $ log (z i)), simp only [exp_sum, (∘), smul_eq_mul, mul_comm (w _) (log _)] at this, convert this using 1; [apply prod_congr rfl, apply sum_congr rfl]; intros i hi, { cases eq_or_lt_of_le (hz i hi) with hz hz, { simp [A i hi hz.symm] }, { exact rpow_def_of_pos hz _ } }, { cases eq_or_lt_of_le (hz i hi) with hz hz, { simp [A i hi hz.symm] }, { rw [exp_log hz] } } } end theorem pow_arith_mean_le_arith_mean_pow (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) (hz : βˆ€ i ∈ s, 0 ≀ z i) (n : β„•) : (βˆ‘ i in s, w i * z i) ^ n ≀ βˆ‘ i in s, (w i * z i ^ n) := (convex_on_pow n).map_sum_le hw hw' hz theorem pow_arith_mean_le_arith_mean_pow_of_even (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) {n : β„•} (hn : even n) : (βˆ‘ i in s, w i * z i) ^ n ≀ βˆ‘ i in s, (w i * z i ^ n) := (convex_on_pow_of_even hn).map_sum_le hw hw' (Ξ» _ _, trivial) theorem fpow_arith_mean_le_arith_mean_fpow (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) (hz : βˆ€ i ∈ s, 0 < z i) (m : β„€) : (βˆ‘ i in s, w i * z i) ^ m ≀ βˆ‘ i in s, (w i * z i ^ m) := (convex_on_fpow m).map_sum_le hw hw' hz theorem rpow_arith_mean_le_arith_mean_rpow (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) (hz : βˆ€ i ∈ s, 0 ≀ z i) {p : ℝ} (hp : 1 ≀ p) : (βˆ‘ i in s, w i * z i) ^ p ≀ βˆ‘ i in s, (w i * z i ^ p) := (convex_on_rpow hp).map_sum_le hw hw' hz theorem arith_mean_le_rpow_mean (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : βˆ‘ i in s, w i = 1) (hz : βˆ€ i ∈ s, 0 ≀ z i) {p : ℝ} (hp : 1 ≀ p) : βˆ‘ i in s, w i * z i ≀ (βˆ‘ i in s, (w i * z i ^ p)) ^ (1 / p) := begin have : 0 < p := lt_of_lt_of_le zero_lt_one hp, rw [← rpow_le_rpow_iff _ _ this, ← rpow_mul, one_div_mul_cancel (ne_of_gt this), rpow_one], exact rpow_arith_mean_le_arith_mean_rpow s w z hw hw' hz hp, all_goals { apply_rules [sum_nonneg, rpow_nonneg_of_nonneg], intros i hi, apply_rules [mul_nonneg, rpow_nonneg_of_nonneg, hw i hi, hz i hi] }, end end real namespace nnreal /-- The geometric mean is less than or equal to the arithmetic mean, weighted version for `nnreal`-valued functions. -/ theorem geom_mean_le_arith_mean_weighted (w z : ΞΉ β†’ ℝβ‰₯0) (hw' : βˆ‘ i in s, w i = 1) : (∏ i in s, (z i) ^ (w i:ℝ)) ≀ βˆ‘ i in s, w i * z i := by exact_mod_cast real.geom_mean_le_arith_mean_weighted _ _ _ (Ξ» i _, (w i).coe_nonneg) (by assumption_mod_cast) (Ξ» i _, (z i).coe_nonneg) /-- The geometric mean is less than or equal to the arithmetic mean, weighted version for two `nnreal` numbers. -/ theorem geom_mean_le_arith_mean2_weighted (w₁ wβ‚‚ p₁ pβ‚‚ : ℝβ‰₯0) : w₁ + wβ‚‚ = 1 β†’ p₁ ^ (w₁:ℝ) * pβ‚‚ ^ (wβ‚‚:ℝ) ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one] using geom_mean_le_arith_mean_weighted (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons wβ‚‚ fin_zero_elim) (fin.cons p₁ $ fin.cons pβ‚‚ $ fin_zero_elim) theorem geom_mean_le_arith_mean3_weighted (w₁ wβ‚‚ w₃ p₁ pβ‚‚ p₃ : ℝβ‰₯0) : w₁ + wβ‚‚ + w₃ = 1 β†’ p₁ ^ (w₁:ℝ) * pβ‚‚ ^ (wβ‚‚:ℝ) * p₃ ^ (w₃:ℝ) ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ + w₃ * p₃ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted (univ : finset (fin 3)) (fin.cons w₁ $ fin.cons wβ‚‚ $ fin.cons w₃ fin_zero_elim) (fin.cons p₁ $ fin.cons pβ‚‚ $ fin.cons p₃ fin_zero_elim) theorem geom_mean_le_arith_mean4_weighted (w₁ wβ‚‚ w₃ wβ‚„ p₁ pβ‚‚ p₃ pβ‚„ : ℝβ‰₯0) : w₁ + wβ‚‚ + w₃ + wβ‚„ = 1 β†’ p₁ ^ (w₁:ℝ) * pβ‚‚ ^ (wβ‚‚:ℝ) * p₃ ^ (w₃:ℝ)* pβ‚„ ^ (wβ‚„:ℝ) ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ + w₃ * p₃ + wβ‚„ * pβ‚„ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted (univ : finset (fin 4)) (fin.cons w₁ $ fin.cons wβ‚‚ $ fin.cons w₃ $ fin.cons wβ‚„ fin_zero_elim) (fin.cons p₁ $ fin.cons pβ‚‚ $ fin.cons p₃ $ fin.cons pβ‚„ fin_zero_elim) /-- Weighted generalized mean inequality, version sums over finite sets, with `ℝβ‰₯0`-valued functions and natural exponent. -/ theorem pow_arith_mean_le_arith_mean_pow (w z : ΞΉ β†’ ℝβ‰₯0) (hw' : βˆ‘ i in s, w i = 1) (n : β„•) : (βˆ‘ i in s, w i * z i) ^ n ≀ βˆ‘ i in s, (w i * z i ^ n) := by exact_mod_cast real.pow_arith_mean_le_arith_mean_pow s _ _ (Ξ» i _, (w i).coe_nonneg) (by exact_mod_cast hw') (Ξ» i _, (z i).coe_nonneg) n /-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝβ‰₯0`-valued functions and real exponents. -/ theorem rpow_arith_mean_le_arith_mean_rpow (w z : ΞΉ β†’ ℝβ‰₯0) (hw' : βˆ‘ i in s, w i = 1) {p : ℝ} (hp : 1 ≀ p) : (βˆ‘ i in s, w i * z i) ^ p ≀ βˆ‘ i in s, (w i * z i ^ p) := by exact_mod_cast real.rpow_arith_mean_le_arith_mean_rpow s _ _ (Ξ» i _, (w i).coe_nonneg) (by exact_mod_cast hw') (Ξ» i _, (z i).coe_nonneg) hp /-- Weighted generalized mean inequality, version for two elements of `ℝβ‰₯0` and real exponents. -/ theorem rpow_arith_mean_le_arith_mean2_rpow (w₁ wβ‚‚ z₁ zβ‚‚ : ℝβ‰₯0) (hw' : w₁ + wβ‚‚ = 1) {p : ℝ} (hp : 1 ≀ p) : (w₁ * z₁ + wβ‚‚ * zβ‚‚) ^ p ≀ w₁ * z₁ ^ p + wβ‚‚ * zβ‚‚ ^ p := begin have h := rpow_arith_mean_le_arith_mean_rpow (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons wβ‚‚ fin_zero_elim) (fin.cons z₁ $ fin.cons zβ‚‚ $ fin_zero_elim) _ hp, { simpa [fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero] using h, }, { simp [hw', fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero], }, end /-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝβ‰₯0`-valued functions and real exponents. -/ theorem arith_mean_le_rpow_mean (w z : ΞΉ β†’ ℝβ‰₯0) (hw' : βˆ‘ i in s, w i = 1) {p : ℝ} (hp : 1 ≀ p) : βˆ‘ i in s, w i * z i ≀ (βˆ‘ i in s, (w i * z i ^ p)) ^ (1 / p) := by exact_mod_cast real.arith_mean_le_rpow_mean s _ _ (Ξ» i _, (w i).coe_nonneg) (by exact_mod_cast hw') (Ξ» i _, (z i).coe_nonneg) hp end nnreal namespace ennreal /-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝβ‰₯0∞`-valued functions and real exponents. -/ theorem rpow_arith_mean_le_arith_mean_rpow (w z : ΞΉ β†’ ℝβ‰₯0∞) (hw' : βˆ‘ i in s, w i = 1) {p : ℝ} (hp : 1 ≀ p) : (βˆ‘ i in s, w i * z i) ^ p ≀ βˆ‘ i in s, (w i * z i ^ p) := begin have hp_pos : 0 < p, from lt_of_lt_of_le zero_lt_one hp, have hp_nonneg : 0 ≀ p, from le_of_lt hp_pos, have hp_not_nonpos : Β¬ p ≀ 0, by simp [hp_pos], have hp_not_neg : Β¬ p < 0, by simp [hp_nonneg], have h_top_iff_rpow_top : βˆ€ (i : ΞΉ) (hi : i ∈ s), w i * z i = ⊀ ↔ w i * (z i) ^ p = ⊀, by simp [hp_pos, hp_nonneg, hp_not_nonpos, hp_not_neg], refine le_of_top_imp_top_of_to_nnreal_le _ _, { -- first, prove `(βˆ‘ i in s, w i * z i) ^ p = ⊀ β†’ βˆ‘ i in s, (w i * z i ^ p) = ⊀` rw [rpow_eq_top_iff, sum_eq_top_iff, sum_eq_top_iff], intro h, simp only [and_false, hp_not_neg, false_or] at h, rcases h.left with ⟨a, H, ha⟩, use [a, H], rwa ←h_top_iff_rpow_top a H, }, { -- second, suppose both `(βˆ‘ i in s, w i * z i) ^ p β‰  ⊀` and `βˆ‘ i in s, (w i * z i ^ p) β‰  ⊀`, -- and prove `((βˆ‘ i in s, w i * z i) ^ p).to_nnreal ≀ (βˆ‘ i in s, (w i * z i ^ p)).to_nnreal`, -- by using `nnreal.rpow_arith_mean_le_arith_mean_rpow`. intros h_top_rpow_sum _, -- show hypotheses needed to put the `.to_nnreal` inside the sums. have h_top : βˆ€ (a : ΞΉ), a ∈ s β†’ w a * z a < ⊀, { have h_top_sum : βˆ‘ (i : ΞΉ) in s, w i * z i < ⊀, { by_contra h, rw [lt_top_iff_ne_top, not_not] at h, rw [h, top_rpow_of_pos hp_pos] at h_top_rpow_sum, exact h_top_rpow_sum rfl, }, rwa sum_lt_top_iff at h_top_sum, }, have h_top_rpow : βˆ€ (a : ΞΉ), a ∈ s β†’ w a * z a ^ p < ⊀, { intros i hi, specialize h_top i hi, rw lt_top_iff_ne_top at h_top ⊒, rwa [ne.def, ←h_top_iff_rpow_top i hi], }, -- put the `.to_nnreal` inside the sums. simp_rw [to_nnreal_sum h_top_rpow, ←to_nnreal_rpow, to_nnreal_sum h_top, to_nnreal_mul, ←to_nnreal_rpow], -- use corresponding nnreal result refine nnreal.rpow_arith_mean_le_arith_mean_rpow s (Ξ» i, (w i).to_nnreal) (Ξ» i, (z i).to_nnreal) _ hp, -- verify the hypothesis `βˆ‘ i in s, (w i).to_nnreal = 1`, using `βˆ‘ i in s, w i = 1` . have h_sum_nnreal : (βˆ‘ i in s, w i) = ↑(βˆ‘ i in s, (w i).to_nnreal), { have hw_top : βˆ‘ i in s, w i < ⊀, by { rw hw', exact one_lt_top, }, rw ←to_nnreal_sum, { rw coe_to_nnreal, rwa ←lt_top_iff_ne_top, }, { rwa sum_lt_top_iff at hw_top, }, }, rwa [←coe_eq_coe, ←h_sum_nnreal], }, end /-- Weighted generalized mean inequality, version for two elements of `ℝβ‰₯0∞` and real exponents. -/ theorem rpow_arith_mean_le_arith_mean2_rpow (w₁ wβ‚‚ z₁ zβ‚‚ : ℝβ‰₯0∞) (hw' : w₁ + wβ‚‚ = 1) {p : ℝ} (hp : 1 ≀ p) : (w₁ * z₁ + wβ‚‚ * zβ‚‚) ^ p ≀ w₁ * z₁ ^ p + wβ‚‚ * zβ‚‚ ^ p := begin have h := rpow_arith_mean_le_arith_mean_rpow (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons wβ‚‚ fin_zero_elim) (fin.cons z₁ $ fin.cons zβ‚‚ $ fin_zero_elim) _ hp, { simpa [fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero] using h, }, { simp [hw', fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero], }, end end ennreal namespace real theorem geom_mean_le_arith_mean2_weighted {w₁ wβ‚‚ p₁ pβ‚‚ : ℝ} (hw₁ : 0 ≀ w₁) (hwβ‚‚ : 0 ≀ wβ‚‚) (hp₁ : 0 ≀ p₁) (hpβ‚‚ : 0 ≀ pβ‚‚) (hw : w₁ + wβ‚‚ = 1) : p₁ ^ w₁ * pβ‚‚ ^ wβ‚‚ ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ := nnreal.geom_mean_le_arith_mean2_weighted ⟨w₁, hwβ‚βŸ© ⟨wβ‚‚, hwβ‚‚βŸ© ⟨p₁, hpβ‚βŸ© ⟨pβ‚‚, hpβ‚‚βŸ© $ nnreal.coe_eq.1 $ by assumption theorem geom_mean_le_arith_mean3_weighted {w₁ wβ‚‚ w₃ p₁ pβ‚‚ p₃ : ℝ} (hw₁ : 0 ≀ w₁) (hwβ‚‚ : 0 ≀ wβ‚‚) (hw₃ : 0 ≀ w₃) (hp₁ : 0 ≀ p₁) (hpβ‚‚ : 0 ≀ pβ‚‚) (hp₃ : 0 ≀ p₃) (hw : w₁ + wβ‚‚ + w₃ = 1) : p₁ ^ w₁ * pβ‚‚ ^ wβ‚‚ * p₃ ^ w₃ ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ + w₃ * p₃ := nnreal.geom_mean_le_arith_mean3_weighted ⟨w₁, hwβ‚βŸ© ⟨wβ‚‚, hwβ‚‚βŸ© ⟨w₃, hwβ‚ƒβŸ© ⟨p₁, hpβ‚βŸ© ⟨pβ‚‚, hpβ‚‚βŸ© ⟨p₃, hpβ‚ƒβŸ© $ nnreal.coe_eq.1 hw theorem geom_mean_le_arith_mean4_weighted {w₁ wβ‚‚ w₃ wβ‚„ p₁ pβ‚‚ p₃ pβ‚„ : ℝ} (hw₁ : 0 ≀ w₁) (hwβ‚‚ : 0 ≀ wβ‚‚) (hw₃ : 0 ≀ w₃) (hwβ‚„ : 0 ≀ wβ‚„) (hp₁ : 0 ≀ p₁) (hpβ‚‚ : 0 ≀ pβ‚‚) (hp₃ : 0 ≀ p₃) (hpβ‚„ : 0 ≀ pβ‚„) (hw : w₁ + wβ‚‚ + w₃ + wβ‚„ = 1) : p₁ ^ w₁ * pβ‚‚ ^ wβ‚‚ * p₃ ^ w₃ * pβ‚„ ^ wβ‚„ ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ + w₃ * p₃ + wβ‚„ * pβ‚„ := nnreal.geom_mean_le_arith_mean4_weighted ⟨w₁, hwβ‚βŸ© ⟨wβ‚‚, hwβ‚‚βŸ© ⟨w₃, hwβ‚ƒβŸ© ⟨wβ‚„, hwβ‚„βŸ© ⟨p₁, hpβ‚βŸ© ⟨pβ‚‚, hpβ‚‚βŸ© ⟨p₃, hpβ‚ƒβŸ© ⟨pβ‚„, hpβ‚„βŸ© $ nnreal.coe_eq.1 $ by assumption /-- Young's inequality, a version for nonnegative real numbers. -/ theorem young_inequality_of_nonneg {a b p q : ℝ} (ha : 0 ≀ a) (hb : 0 ≀ b) (hpq : p.is_conjugate_exponent q) : a * b ≀ a^p / p + b^q / q := by simpa [← rpow_mul, ha, hb, hpq.ne_zero, hpq.symm.ne_zero, div_eq_inv_mul] using geom_mean_le_arith_mean2_weighted hpq.one_div_nonneg hpq.symm.one_div_nonneg (rpow_nonneg_of_nonneg ha p) (rpow_nonneg_of_nonneg hb q) hpq.inv_add_inv_conj /-- Young's inequality, a version for arbitrary real numbers. -/ theorem young_inequality (a b : ℝ) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≀ (abs a)^p / p + (abs b)^q / q := calc a * b ≀ abs (a * b) : le_abs_self (a * b) ... = abs a * abs b : abs_mul a b ... ≀ (abs a)^p / p + (abs b)^q / q : real.young_inequality_of_nonneg (abs_nonneg a) (abs_nonneg b) hpq end real namespace nnreal /-- Young's inequality, `ℝβ‰₯0` version. We use `{p q : ℝβ‰₯0}` in order to avoid constructing witnesses of `0 ≀ p` and `0 ≀ q` for the denominators. -/ theorem young_inequality (a b : ℝβ‰₯0) {p q : ℝβ‰₯0} (hp : 1 < p) (hpq : 1 / p + 1 / q = 1) : a * b ≀ a^(p:ℝ) / p + b^(q:ℝ) / q := real.young_inequality_of_nonneg a.coe_nonneg b.coe_nonneg ⟨hp, nnreal.coe_eq.2 hpq⟩ /-- Young's inequality, `ℝβ‰₯0` version with real conjugate exponents. -/ theorem young_inequality_real (a b : ℝβ‰₯0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≀ a ^ p / real.to_nnreal p + b ^ q / real.to_nnreal q := begin nth_rewrite 0 ← real.coe_to_nnreal p hpq.nonneg, nth_rewrite 0 ← real.coe_to_nnreal q hpq.symm.nonneg, exact young_inequality a b hpq.one_lt_nnreal hpq.inv_add_inv_conj_nnreal, end /-- HΓΆlder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with `ℝβ‰₯0`-valued functions. -/ theorem inner_le_Lp_mul_Lq (f g : ΞΉ β†’ ℝβ‰₯0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : βˆ‘ i in s, f i * g i ≀ (βˆ‘ i in s, (f i) ^ p) ^ (1 / p) * (βˆ‘ i in s, (g i) ^ q) ^ (1 / q) := begin -- Let `G=βˆ₯gβˆ₯_q` be the `L_q`-norm of `g`. set G := (βˆ‘ i in s, (g i) ^ q) ^ (1 / q), have hGq : G ^ q = βˆ‘ i in s, (g i) ^ q, { rw [← rpow_mul, one_div_mul_cancel hpq.symm.ne_zero, rpow_one], }, -- First consider the trivial case `βˆ₯gβˆ₯_q=0` by_cases hG : G = 0, { rw [hG, sum_eq_zero, mul_zero], intros i hi, simp only [rpow_eq_zero_iff, sum_eq_zero_iff] at hG, simp [(hG.1 i hi).1] }, { -- Move power from right to left rw [← div_le_iff hG, sum_div], -- Now the inequality follows from the weighted generalized mean inequality -- with weights `w_i` and numbers `z_i` given by the following formulas. set w : ΞΉ β†’ ℝβ‰₯0 := Ξ» i, (g i) ^ q / G ^ q, set z : ΞΉ β†’ ℝβ‰₯0 := Ξ» i, f i * (G / g i) ^ (q / p), -- Show that the sum of weights equals one have A : βˆ‘ i in s, w i = 1, { rw [← sum_div, hGq, div_self], simpa [rpow_eq_zero_iff, hpq.symm.ne_zero] using hG }, -- LHS of the goal equals LHS of the weighted generalized mean inequality calc (βˆ‘ i in s, f i * g i / G) = (βˆ‘ i in s, w i * z i) : begin refine sum_congr rfl (Ξ» i hi, _), have : q - q / p = 1, by field_simp [hpq.ne_zero, hpq.symm.mul_eq_add], dsimp only [w, z], rw [← div_rpow, mul_left_comm, mul_div_assoc, ← @inv_div _ _ _ G, inv_rpow, ← div_eq_mul_inv, ← rpow_sub']; simp [this] end -- Apply the generalized mean inequality ... ≀ (βˆ‘ i in s, w i * (z i) ^ p) ^ (1 / p) : nnreal.arith_mean_le_rpow_mean s w z A (le_of_lt hpq.one_lt) -- Simplify the right hand side. Terms with `g i β‰  0` are equal to `(f i) ^ p`, -- the others are zeros. ... ≀ (βˆ‘ i in s, (f i) ^ p) ^ (1 / p) : begin refine rpow_le_rpow (sum_le_sum (Ξ» i hi, _)) hpq.one_div_nonneg, dsimp only [w, z], rw [mul_rpow, mul_left_comm, ← rpow_mul _ _ p, div_mul_cancel _ hpq.ne_zero, div_rpow, div_mul_div, mul_comm (G ^ q), mul_div_mul_right], { nth_rewrite 1 [← mul_one ((f i) ^ p)], exact mul_le_mul_left' (div_self_le _) _ }, { simpa [hpq.symm.ne_zero] using hG } end } end /-- The `L_p` seminorm of a vector `f` is the greatest value of the inner product `βˆ‘ i in s, f i * g i` over functions `g` of `L_q` seminorm less than or equal to one. -/ theorem is_greatest_Lp (f : ΞΉ β†’ ℝβ‰₯0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : is_greatest ((Ξ» g : ΞΉ β†’ ℝβ‰₯0, βˆ‘ i in s, f i * g i) '' {g | βˆ‘ i in s, (g i)^q ≀ 1}) ((βˆ‘ i in s, (f i)^p) ^ (1 / p)) := begin split, { use Ξ» i, ((f i) ^ p / f i / (βˆ‘ i in s, (f i) ^ p) ^ (1 / q)), by_cases hf : βˆ‘ i in s, (f i)^p = 0, { simp [hf, hpq.ne_zero, hpq.symm.ne_zero] }, { have A : p + q - q β‰  0, by simp [hpq.ne_zero], have B : βˆ€ y : ℝβ‰₯0, y * y^p / y = y^p, { refine Ξ» y, mul_div_cancel_left_of_imp (Ξ» h, _), simpa [h, hpq.ne_zero] }, simp only [set.mem_set_of_eq, div_rpow, ← sum_div, ← rpow_mul, div_mul_cancel _ hpq.symm.ne_zero, rpow_one, div_le_iff hf, one_mul, hpq.mul_eq_add, ← rpow_sub' _ A, _root_.add_sub_cancel, le_refl, true_and, ← mul_div_assoc, B], rw [div_eq_iff, ← rpow_add hf, hpq.inv_add_inv_conj, rpow_one], simpa [hpq.symm.ne_zero] using hf } }, { rintros _ ⟨g, hg, rfl⟩, apply le_trans (inner_le_Lp_mul_Lq s f g hpq), simpa only [mul_one] using mul_le_mul_left' (nnreal.rpow_le_one hg (le_of_lt hpq.symm.one_div_pos)) _ } end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `nnreal`-valued functions. -/ theorem Lp_add_le (f g : ΞΉ β†’ ℝβ‰₯0) {p : ℝ} (hp : 1 ≀ p) : (βˆ‘ i in s, (f i + g i) ^ p) ^ (1 / p) ≀ (βˆ‘ i in s, (f i) ^ p) ^ (1 / p) + (βˆ‘ i in s, (g i) ^ p) ^ (1 / p) := begin -- The result is trivial when `p = 1`, so we can assume `1 < p`. rcases eq_or_lt_of_le hp with rfl|hp, { simp [finset.sum_add_distrib] }, have hpq := real.is_conjugate_exponent_conjugate_exponent hp, have := is_greatest_Lp s (f + g) hpq, simp only [pi.add_apply, add_mul, sum_add_distrib] at this, rcases this.1 with βŸ¨Ο†, hΟ†, H⟩, rw ← H, exact add_le_add ((is_greatest_Lp s f hpq).2 βŸ¨Ο†, hΟ†, rfl⟩) ((is_greatest_Lp s g hpq).2 βŸ¨Ο†, hΟ†, rfl⟩) end end nnreal namespace real variables (f g : ΞΉ β†’ ℝ) {p q : ℝ} /-- HΓΆlder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with real-valued functions. -/ theorem inner_le_Lp_mul_Lq (hpq : is_conjugate_exponent p q) : βˆ‘ i in s, f i * g i ≀ (βˆ‘ i in s, (abs $ f i)^p) ^ (1 / p) * (βˆ‘ i in s, (abs $ g i)^q) ^ (1 / q) := begin have := nnreal.coe_le_coe.2 (nnreal.inner_le_Lp_mul_Lq s (Ξ» i, ⟨_, abs_nonneg (f i)⟩) (Ξ» i, ⟨_, abs_nonneg (g i)⟩) hpq), push_cast at this, refine le_trans (sum_le_sum $ Ξ» i hi, _) this, simp only [← abs_mul, le_abs_self] end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `real`-valued functions. -/ theorem Lp_add_le (hp : 1 ≀ p) : (βˆ‘ i in s, (abs $ f i + g i) ^ p) ^ (1 / p) ≀ (βˆ‘ i in s, (abs $ f i) ^ p) ^ (1 / p) + (βˆ‘ i in s, (abs $ g i) ^ p) ^ (1 / p) := begin have := nnreal.coe_le_coe.2 (nnreal.Lp_add_le s (Ξ» i, ⟨_, abs_nonneg (f i)⟩) (Ξ» i, ⟨_, abs_nonneg (g i)⟩) hp), push_cast at this, refine le_trans (rpow_le_rpow _ (sum_le_sum $ Ξ» i hi, _) _) this; simp [sum_nonneg, rpow_nonneg_of_nonneg, abs_nonneg, le_trans zero_le_one hp, abs_add, rpow_le_rpow] end variables {f g} /-- HΓΆlder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with real-valued nonnegative functions. -/ theorem inner_le_Lp_mul_Lq_of_nonneg (hpq : is_conjugate_exponent p q) (hf : βˆ€ i ∈ s, 0 ≀ f i) (hg : βˆ€ i ∈ s, 0 ≀ g i) : βˆ‘ i in s, f i * g i ≀ (βˆ‘ i in s, (f i)^p) ^ (1 / p) * (βˆ‘ i in s, (g i)^q) ^ (1 / q) := by convert inner_le_Lp_mul_Lq s f g hpq using 3; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi, hg i hi] /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `real`-valued nonnegative functions. -/ theorem Lp_add_le_of_nonneg (hp : 1 ≀ p) (hf : βˆ€ i ∈ s, 0 ≀ f i) (hg : βˆ€ i ∈ s, 0 ≀ g i) : (βˆ‘ i in s, (f i + g i) ^ p) ^ (1 / p) ≀ (βˆ‘ i in s, (f i) ^ p) ^ (1 / p) + (βˆ‘ i in s, (g i) ^ p) ^ (1 / p) := by convert Lp_add_le s f g hp using 2 ; [skip, congr' 1, congr' 1]; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi, hg i hi, add_nonneg] end real namespace ennreal /-- Young's inequality, `ℝβ‰₯0∞` version with real conjugate exponents. -/ theorem young_inequality (a b : ℝβ‰₯0∞) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≀ a ^ p / ennreal.of_real p + b ^ q / ennreal.of_real q := begin by_cases h : a = ⊀ ∨ b = ⊀, { refine le_trans le_top (le_of_eq _), repeat { rw div_eq_mul_inv }, cases h; rw h; simp [h, hpq.pos, hpq.symm.pos], }, push_neg at h, -- if a β‰  ⊀ and b β‰  ⊀, use the nnreal version: nnreal.young_inequality_real rw [←coe_to_nnreal h.left, ←coe_to_nnreal h.right, ←coe_mul, coe_rpow_of_nonneg _ hpq.nonneg, coe_rpow_of_nonneg _ hpq.symm.nonneg, ennreal.of_real, ennreal.of_real, ←@coe_div (real.to_nnreal p) _ (by simp [hpq.pos]), ←@coe_div (real.to_nnreal q) _ (by simp [hpq.symm.pos]), ←coe_add, coe_le_coe], exact nnreal.young_inequality_real a.to_nnreal b.to_nnreal hpq, end variables (f g : ΞΉ β†’ ℝβ‰₯0∞) {p q : ℝ} /-- HΓΆlder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with `ℝβ‰₯0∞`-valued functions. -/ theorem inner_le_Lp_mul_Lq (hpq : p.is_conjugate_exponent q) : (βˆ‘ i in s, f i * g i) ≀ (βˆ‘ i in s, (f i)^p) ^ (1/p) * (βˆ‘ i in s, (g i)^q) ^ (1/q) := begin by_cases H : (βˆ‘ i in s, (f i)^p) ^ (1/p) = 0 ∨ (βˆ‘ i in s, (g i)^q) ^ (1/q) = 0, { replace H : (βˆ€ i ∈ s, f i = 0) ∨ (βˆ€ i ∈ s, g i = 0), by simpa [ennreal.rpow_eq_zero_iff, hpq.pos, hpq.symm.pos, asymm hpq.pos, asymm hpq.symm.pos, sum_eq_zero_iff_of_nonneg] using H, have : βˆ€ i ∈ s, f i * g i = 0 := Ξ» i hi, by cases H; simp [H i hi], have : (βˆ‘ i in s, f i * g i) = (βˆ‘ i in s, 0) := sum_congr rfl this, simp [this] }, push_neg at H, by_cases H' : (βˆ‘ i in s, (f i)^p) ^ (1/p) = ⊀ ∨ (βˆ‘ i in s, (g i)^q) ^ (1/q) = ⊀, { cases H'; simp [H', -one_div, H] }, replace H' : (βˆ€ i ∈ s, f i β‰  ⊀) ∧ (βˆ€ i ∈ s, g i β‰  ⊀), by simpa [ennreal.rpow_eq_top_iff, asymm hpq.pos, asymm hpq.symm.pos, hpq.pos, hpq.symm.pos, ennreal.sum_eq_top_iff, not_or_distrib] using H', have := ennreal.coe_le_coe.2 (@nnreal.inner_le_Lp_mul_Lq _ s (Ξ» i, ennreal.to_nnreal (f i)) (Ξ» i, ennreal.to_nnreal (g i)) _ _ hpq), simp [← ennreal.coe_rpow_of_nonneg, le_of_lt (hpq.pos), le_of_lt (hpq.one_div_pos), le_of_lt (hpq.symm.pos), le_of_lt (hpq.symm.one_div_pos)] at this, convert this using 1; [skip, congr' 2]; [skip, skip, simp, skip, simp]; { apply finset.sum_congr rfl (Ξ» i hi, _), simp [H'.1 i hi, H'.2 i hi, -with_zero.coe_mul, with_top.coe_mul.symm] }, end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `ℝβ‰₯0∞` valued nonnegative functions. -/ theorem Lp_add_le (hp : 1 ≀ p) : (βˆ‘ i in s, (f i + g i) ^ p)^(1/p) ≀ (βˆ‘ i in s, (f i)^p) ^ (1/p) + (βˆ‘ i in s, (g i)^p) ^ (1/p) := begin by_cases H' : (βˆ‘ i in s, (f i)^p) ^ (1/p) = ⊀ ∨ (βˆ‘ i in s, (g i)^p) ^ (1/p) = ⊀, { cases H'; simp [H', -one_div] }, have pos : 0 < p := lt_of_lt_of_le zero_lt_one hp, replace H' : (βˆ€ i ∈ s, f i β‰  ⊀) ∧ (βˆ€ i ∈ s, g i β‰  ⊀), by simpa [ennreal.rpow_eq_top_iff, asymm pos, pos, ennreal.sum_eq_top_iff, not_or_distrib] using H', have := ennreal.coe_le_coe.2 (@nnreal.Lp_add_le _ s (Ξ» i, ennreal.to_nnreal (f i)) (Ξ» i, ennreal.to_nnreal (g i)) _ hp), push_cast [← ennreal.coe_rpow_of_nonneg, le_of_lt (pos), le_of_lt (one_div_pos.2 pos)] at this, convert this using 2; [skip, congr' 1, congr' 1]; { apply finset.sum_congr rfl (Ξ» i hi, _), simp [H'.1 i hi, H'.2 i hi] } end private lemma add_rpow_le_one_of_add_le_one {p : ℝ} (a b : ℝβ‰₯0∞) (hab : a + b ≀ 1) (hp1 : 1 ≀ p) : a ^ p + b ^ p ≀ 1 := begin have h_le_one : βˆ€ x : ℝβ‰₯0∞, x ≀ 1 β†’ x ^ p ≀ x, from Ξ» x hx, rpow_le_self_of_le_one hx hp1, have ha : a ≀ 1, from (self_le_add_right a b).trans hab, have hb : b ≀ 1, from (self_le_add_left b a).trans hab, exact (add_le_add (h_le_one a ha) (h_le_one b hb)).trans hab, end lemma add_rpow_le_rpow_add {p : ℝ} (a b : ℝβ‰₯0∞) (hp1 : 1 ≀ p) : a ^ p + b ^ p ≀ (a + b) ^ p := begin have hp_pos : 0 < p := lt_of_lt_of_le zero_lt_one hp1, by_cases h_top : a + b = ⊀, { rw ←@ennreal.rpow_eq_top_iff_of_pos (a + b) p hp_pos at h_top, rw h_top, exact le_top, }, obtain ⟨ha_top, hb_top⟩ := add_ne_top.mp h_top, by_cases h_zero : a + b = 0, { simp [add_eq_zero_iff.mp h_zero, ennreal.zero_rpow_of_pos hp_pos], }, have h_nonzero : Β¬(a = 0 ∧ b = 0), by rwa add_eq_zero_iff at h_zero, have h_add : a/(a+b) + b/(a+b) = 1, by rw [div_add_div_same, div_self h_zero h_top], have h := add_rpow_le_one_of_add_le_one (a/(a+b)) (b/(a+b)) h_add.le hp1, rw [div_rpow_of_nonneg a (a+b) hp_pos.le, div_rpow_of_nonneg b (a+b) hp_pos.le] at h, have hab_0 : (a + b)^p β‰  0, by simp [ha_top, hb_top, hp_pos, h_nonzero], have hab_top : (a + b)^p β‰  ⊀, by simp [ha_top, hb_top, hp_pos, h_nonzero], have h_mul : (a + b)^p * (a ^ p / (a + b) ^ p + b ^ p / (a + b) ^ p) ≀ (a + b)^p, { nth_rewrite 3 ←mul_one ((a + b)^p), exact (mul_le_mul_left hab_0 hab_top).mpr h, }, rwa [div_eq_mul_inv, div_eq_mul_inv, mul_add, mul_comm (a^p), mul_comm (b^p), ←mul_assoc, ←mul_assoc, mul_inv_cancel hab_0 hab_top, one_mul, one_mul] at h_mul, end lemma rpow_add_rpow_le_add {p : ℝ} (a b : ℝβ‰₯0∞) (hp1 : 1 ≀ p) : (a ^ p + b ^ p) ^ (1/p) ≀ a + b := begin rw ←@ennreal.le_rpow_one_div_iff _ _ (1/p) (by simp [lt_of_lt_of_le zero_lt_one hp1]), rw one_div_one_div, exact add_rpow_le_rpow_add _ _ hp1, end theorem rpow_add_rpow_le {p q : ℝ} (a b : ℝβ‰₯0∞) (hp_pos : 0 < p) (hpq : p ≀ q) : (a ^ q + b ^ q) ^ (1/q) ≀ (a ^ p + b ^ p) ^ (1/p) := begin have h_rpow : βˆ€ a : ℝβ‰₯0∞, a^q = (a^p)^(q/p), from Ξ» a, by rw [←ennreal.rpow_mul, div_eq_inv_mul, ←mul_assoc, _root_.mul_inv_cancel hp_pos.ne.symm, one_mul], have h_rpow_add_rpow_le_add : ((a^p)^(q/p) + (b^p)^(q/p)) ^ (1/(q/p)) ≀ a^p + b^p, { refine rpow_add_rpow_le_add (a^p) (b^p) _, rwa one_le_div hp_pos, }, rw [h_rpow a, h_rpow b, ennreal.le_rpow_one_div_iff hp_pos, ←ennreal.rpow_mul, mul_comm, mul_one_div], rwa one_div_div at h_rpow_add_rpow_le_add, end lemma rpow_add_le_add_rpow {p : ℝ} (a b : ℝβ‰₯0∞) (hp_pos : 0 < p) (hp1 : p ≀ 1) : (a + b) ^ p ≀ a ^ p + b ^ p := begin have h := rpow_add_rpow_le a b hp_pos hp1, rw one_div_one at h, repeat { rw ennreal.rpow_one at h }, exact (ennreal.le_rpow_one_div_iff hp_pos).mp h, end end ennreal
83756b90d7f6e130001f4f441fbc5d8466432bfc
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/tactic/wlog.lean
1de77cf9c167af3a23abd8181f1fdfb526a913ce
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,146
lean
/- Copyright (c) 2018 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl Without loss of generality tactic. -/ import data.list.perm open expr tactic lean lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic private meta def update_pp_name : expr β†’ name β†’ expr | (local_const n _ bi d) pp := local_const n pp bi d | e n := e private meta def elim_or : β„• β†’ expr β†’ tactic (list expr) | 0 h := fail "zero cases" | 1 h := return [h] | (n + 1) h := do [(_, [hl], []), (_, [hr], [])] ← induction h, -- there should be no dependent terms [gl, gr] ← get_goals, set_goals [gr], hsr ← elim_or n hr, gsr ← get_goals, set_goals (gl :: gsr), return (hl :: hsr) private meta def dest_or : expr β†’ tactic (list expr) | e := do `(%%a ∨ %%b) ← whnf e | return [e], lb ← dest_or b, return (a :: lb) private meta def match_perms (pat : pattern) : expr β†’ tactic (list $ list expr) | t := (do m ← match_pattern pat t, guard (m.2.all expr.is_local_constant), return [m.2]) <|> (do `(%%l ∨ %%r) ← whnf t, m ← match_pattern pat l, rs ← match_perms r, return (m.2 :: rs)) meta def wlog (vars' : list expr) (h_cases fst_case : expr) (perms : list (list expr)) : tactic unit := do guard h_cases.is_local_constant, -- reorder s.t. context is Ξ“ ⬝ vars ⬝ cases ⊒ βˆ€deps, … nr ← revert_lst (vars' ++ [h_cases]), vars ← intron' vars'.length, h_cases ← intro h_cases.local_pp_name, cases ← infer_type h_cases, h_fst_case ← mk_local_def h_cases.local_pp_name (fst_case.instantiate_locals $ (vars'.zip vars).map $ λ⟨o, n⟩, (o.local_uniq_name, n)), ((), pr) ← solve_aux cases (repeat $ exact h_fst_case <|> left >> skip), t ← target, fixed_vars ← vars.mmap update_type, let t' := (instantiate_local h_cases.local_uniq_name pr t).pis (fixed_vars ++ [h_fst_case]), (h, [g]) ← local_proof `this t' (do clear h_cases, vars.mmap clear, intron nr), hβ‚€ :: hs ← elim_or perms.length h_cases, solve1 (do exact (h.mk_app $ vars ++ [hβ‚€])), focus ((hs.zip perms.tail).map $ λ⟨h_case, perm⟩, do let p_v := (vars'.zip vars).map (λ⟨p, v⟩, (p.local_uniq_name, v)), let p := perm.map (Ξ»p, p.instantiate_locals p_v), note `this none (h.mk_app $ p ++ [h_case]), clear h, return ()), gs ← get_goals, set_goals (g :: gs) namespace interactive open interactive interactive.types expr private meta def parse_permutations : option (list (list name)) β†’ tactic (list (list expr)) | none := return [] | (some []) := return [] | (some perms@(pβ‚€ :: ps)) := do (guard pβ‚€.nodup <|> fail "No permutation `xs_i` in `using [xs_1, …, xs_n]` should contain the same variable twice."), (guard (perms.all $ Ξ»p, p.perm pβ‚€) <|> fail ("The permutations `xs_i` in `using [xs_1, …, xs_n]` must be permutations of the same" ++ " variables.")), perms.mmap (Ξ»p, p.mmap get_local) /-- Without loss of generality: reduces to one goal under variables permutations. Given a goal of the form `g xs`, a predicate `p` over a set of variables, as well as variable permutations `xs_i`. Then `wlog` produces goals of the form The case goal, i.e. the permutation `xs_i` covers all possible cases: `⊒ p xs_0 ∨ β‹― ∨ p xs_n` The main goal, i.e. the goal reduced to `xs_0`: `(h : p xs_0) ⊒ g xs_0` The invariant goals, i.e. `g` is invariant under `xs_i`: `(h : p xs_i) (this : g xs_0) ⊒ gs xs_i` Either the permutation is provided, or a proof of the disjunction is provided to compute the permutation. The disjunction need to be in assoc normal form, e.g. `pβ‚€ ∨ (p₁ ∨ pβ‚‚)`. In many cases the invariant goals can be solved by AC rewriting using `cc` etc. Example: On a state `(n m : β„•) ⊒ p n m` the tactic `wlog h : n ≀ m using [n m, m n]` produces the following states: `(n m : β„•) ⊒ n ≀ m ∨ m ≀ n` `(n m : β„•) (h : n ≀ m) ⊒ p n m` `(n m : β„•) (h : m ≀ n) (this : p n m) ⊒ p m n` `wlog` supports different calling conventions. The name `h` is used to give a name to the introduced case hypothesis. If the name is avoided, the default will be `case`. (1) `wlog : p xs0 using [xs0, …, xsn]` Results in the case goal `p xs0 ∨ β‹― ∨ ps xsn`, the main goal `(case : p xs0) ⊒ g xs0` and the invariance goals `(case : p xsi) (this : g xs0) ⊒ g xsi`. (2) `wlog : p xs0 := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ β‹― ∨ p xsi`, it is also used to compute the variable permutations. (3) `wlog := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ β‹― ∨ p xsi`, it is also used to compute the variable permutations. This is not as stable as (2), for example `p` cannot be a disjunction. (4) `wlog : R x y using x y` and `wlog : R x y` Produces the case `R x y ∨ R y x`. If `R` is ≀, then the disjunction discharged using linearity. If `using x y` is avoided then `x` and `y` are the last two variables appearing in the expression `R x y`. -/ meta def wlog (h : parse ident?) (pat : parse (tk ":" *> texpr)?) (cases : parse (tk ":=" *> texpr)?) (perms : parse (tk "using" *> (list_of (ident*) <|> (Ξ»x, [x]) <$> ident*))?) (discharger : tactic unit := (tactic.solve_by_elim <|> tactic.tautology {classical := tt} <|> using_smt (smt_tactic.intros >> smt_tactic.solve_goals))) : tactic unit := do perms ← parse_permutations perms, (pat, cases_pr, cases_goal, vars, perms) ← (match cases with | some r := do vars::_ ← return perms | fail "At least one set of variables expected, i.e. `using x y` or `using [x y, y x]`.", cases_pr ← to_expr r, cases_pr ← (if cases_pr.is_local_constant then return $ match h with some n := update_pp_name cases_pr n | none := cases_pr end else do note (h.get_or_else `case) none cases_pr), cases ← infer_type cases_pr, (pat, perms') ← match pat with | some pat := do pat ← to_expr pat, let vars' := vars.filter $ Ξ»v, v.occurs pat, case_pat ← mk_pattern [] vars' pat [] vars', perms' ← match_perms case_pat cases, return (pat, perms') | none := do (p :: ps) ← dest_or cases, let vars' := vars.filter $ Ξ»v, v.occurs p, case_pat ← mk_pattern [] vars' p [] vars', perms' ← (p :: ps).mmap (Ξ»p, do m ← match_pattern case_pat p, return m.2), return (p, perms') end, let vars_name := vars.map local_uniq_name, guard (perms'.all $ Ξ»p, p.all $ Ξ»v, v.is_local_constant ∧ v.local_uniq_name ∈ vars_name) <|> fail "Cases contains variables not declared in `using x y z`", perms ← (if perms.length = 1 then do return (perms'.map $ Ξ» p, p ++ vars.filter (Ξ» v, p.all (Ξ» v', v'.local_uniq_name β‰  v.local_uniq_name))) else do guard (perms.length = perms'.length) <|> fail "The provided permutation list has a different length then the provided cases.", return perms), return (pat, cases_pr, @none expr, vars, perms) | none := do let name_h := h.get_or_else `case, some pat ← return pat | fail "Either specify cases or a pattern with permutations", pat ← to_expr pat, (do [x, y] ← match perms with | [] := return pat.list_local_consts | [l] := return l | _ := failed end, let cases := mk_or_lst [pat, pat.instantiate_locals [(x.local_uniq_name, y), (y.local_uniq_name, x)]], (do `(%%x' ≀ %%y') ← return pat, (cases_pr, []) ← local_proof name_h cases (exact ``(le_total %%x' %%y')), return (pat, cases_pr, none, [x, y], [[x, y], [y, x]])) <|> (do (cases_pr, [g]) ← local_proof name_h cases skip, return (pat, cases_pr, some g, [x, y], [[x, y], [y, x]]))) <|> (do guard (perms.length β‰₯ 2) <|> fail ("To generate cases at least two permutations are required, i.e. `using [x y, y x]`" ++ " or exactly 0 or 2 variables"), (vars :: perms') ← return perms, let names := vars.map local_uniq_name, let cases := mk_or_lst (pat :: perms'.map (Ξ»p, pat.instantiate_locals (names.zip p))), (cases_pr, [g]) ← local_proof name_h cases skip, return (pat, cases_pr, some g, vars, perms)) end), let name_fn := if perms.length = 2 then Ξ» _, `invariant else Ξ» i, mk_simple_name ("invariant_" ++ to_string (i + 1)), with_enable_tags $ tactic.focus1 $ do t ← get_main_tag, tactic.wlog vars cases_pr pat perms, tactic.focus (set_main_tag (mk_num_name `_case 0 :: `main :: t) :: (list.range (perms.length - 1)).map (Ξ»i, do set_main_tag (mk_num_name `_case 0 :: name_fn i :: t), try discharger)), match cases_goal with | some g := do set_tag g (mk_num_name `_case 0 :: `cases :: t), gs ← get_goals, set_goals (g :: gs) | none := skip end add_tactic_doc { name := "wlog", category := doc_category.tactic, decl_names := [``wlog], tags := ["logic"] } end interactive end tactic
bd50765ba06fc516c6394c0816a87bfc01d99bc3
e21db629d2e37a833531fdcb0b37ce4d71825408
/alt/hoare.lean
61bb1fa35c06ac93d5bc0cb00e82e9f7c95b51af
[]
no_license
fischerman/GPU-transformation-verifier
614a28cb4606a05a0eb27e8d4eab999f4f5ea60c
75a5016f05382738ff93ce5859c4cfa47ccb63c1
refs/heads/master
1,586,985,789,300
1,579,290,514,000
1,579,290,514,000
165,031,073
1
0
null
null
null
null
UTF-8
Lean
false
false
3,540
lean
import aux import parlang.def import parlang.lemmas_exec import data.bool namespace parlang variables {Οƒ : Type} {ΞΉ : Type} {Ο„ : ΞΉ β†’ Type} [decidable_eq ΞΉ] /- because this holds for all n and ac the pre- and postcondition probably contain ite or forall quantifiers -/ def hoare (P : thread_state Οƒ Ο„ β†’ Prop) (k : kernel Οƒ Ο„) (Q : thread_state Οƒ Ο„ β†’ Prop) : Prop := βˆ€ {n} {s u : state n Οƒ Ο„} {ac : vector bool n}, (βˆ€i : fin n, ac.nth i β†’ P (s.threads.nth i)) β†’ exec_state k ac s u β†’ (βˆ€i : fin n, ac.nth i β†’ Q (u.threads.nth i)) notation `{* ` P : 1 ` *} ` k : 1 ` {* ` Q : 1 ` *}` := hoare P k Q example (P Q : thread_state Οƒ Ο„ β†’ Prop) (k₁ kβ‚‚ : kernel Οƒ Ο„) (c : Οƒ β†’ bool) : {* Ξ» (t : thread_state Οƒ Ο„), P t ∧ c t.tlocal *} k₁ {* Q *} β†’ -- how is it possible that by loosening up the assumptions (i. e. and something to P) the proof gets possible? {* Ξ» (t : thread_state Οƒ Ο„), P t ∧ Β¬c t.tlocal *} kβ‚‚ {* Q *} β†’ {* P *} kernel.ite c k₁ kβ‚‚ {* Q *} := begin intros h_then h_else n s u ac hp he i hac, cases he, have : _ := exec_state_comm_distinct_ac _ he_a he_a_1, cases this with t' this, -- we reorder the execution (and state transition respectively) to macht the hoare triplets using exec_state_comm_distinct_ac -- either the condition holds or not for any thread i -- ?? in either case we have to go through both executions ?? by_cases hc : (c (vector.nth (s.threads) i).tlocal = tt), { apply h_then, tactic.swap, exact this.right, intros i' hh, have hh' : _ := deactivate_threads_alive hh, have heqst' : vector.nth (s.threads) i' = vector.nth (t'.threads) i' := begin apply exec_state_inactive_threads_untouched this.left, apply deactivate_threads_complement hh, end, rw ← heqst', apply and.intro, { exact hp i' hh', }, { exact deactivate_threads_condition hh, }, { apply active_map_deactivate_threads hac (bool.eq_tt_coe.mpr hc), }, }, { apply h_else, tactic.swap, exact he_a_1, intros i' hh, have hh' : _ := deactivate_threads_alive hh, have heqst' : vector.nth (s.threads) i' = vector.nth (he_t.threads) i' := begin apply exec_state_inactive_threads_untouched he_a, apply deactivate_threads_complement, rw bool.bnot_bnot, exact hh, end, rw ← heqst', apply and.intro, { exact hp i' hh', }, { exact deactivate_threads_condition' hh, }, { rw ← bool.eq_tt_coe at hc, apply active_map_deactivate_threads' hac hc, } }, { sorry, } end example : {* Ξ» (t : thread_state (β„• Γ— β„•) Ο„), t.tlocal.1 = 0 ∧ t.tlocal.1 = 0 *} kernel.compute (Ξ» l, (l.1, 1)) {* Ξ» (t : thread_state (β„• Γ— β„•) Ο„), t.tlocal.2 = 1 *} β†’ {* Ξ» (t : thread_state (β„• Γ— β„•) Ο„), t.tlocal.1 = 0 ∧ Β¬t.tlocal.1 = 0 *} kernel.compute (Ξ» l, (l.1, 2)) {* Ξ» (t : thread_state (β„• Γ— β„•) Ο„), t.tlocal.2 = 1 *} β†’ {* Ξ» (t : thread_state (β„• Γ— β„•) Ο„), t.tlocal.1 = 0 *} kernel.ite (Ξ»t, t.1 = 0) (kernel.compute (Ξ» l, (l.1, 1))) (kernel.compute (Ξ» l, (l.1, 2))) {* Ξ» (t : thread_state (β„• Γ— β„•) Ο„), t.tlocal.2 = 1 *} := begin intros h_then h_else n s u ac hp he i hac, cases he, sorry, end end parlang
81c17bb1f4416f202311d6a2c12de69c3fdb1789
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/limits/shapes/constructions/pullbacks.lean
f800d846ca1f7cfbee2e01f658b4db6922361cc7
[ "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,933
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.binary_products import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.pullbacks universes v u /-! # Constructing pullbacks from binary products and equalizers If a category as binary products and equalizers, then it has pullbacks. Also, if a category has binary coproducts and coequalizers, then it has pushouts -/ open category_theory namespace category_theory.limits /-- If the product `X β¨― Y` and the equalizer of `π₁ ≫ f` and `Ο€β‚‚ ≫ g` exist, then the pullback of `f` and `g` exists: It is given by composing the equalizer with the projections. -/ lemma has_limit_cospan_of_has_limit_pair_of_has_limit_parallel_pair {C : Type u} [π’ž : category.{v} C] {X Y Z : C} (f : X ⟢ Z) (g : Y ⟢ Z) [has_limit (pair X Y)] [has_limit (parallel_pair (prod.fst ≫ f) (prod.snd ≫ g))] : has_limit (cospan f g) := let π₁ : X β¨― Y ⟢ X := prod.fst, Ο€β‚‚ : X β¨― Y ⟢ Y := prod.snd, e := equalizer.ΞΉ (π₁ ≫ f) (Ο€β‚‚ ≫ g) in has_limit.mk { cone := pullback_cone.mk (e ≫ π₁) (e ≫ Ο€β‚‚) $ by simp only [category.assoc, equalizer.condition], is_limit := pullback_cone.is_limit.mk _ _ _ (Ξ» s, equalizer.lift (prod.lift (s.Ο€.app walking_cospan.left) (s.Ο€.app walking_cospan.right)) $ by rw [←category.assoc, limit.lift_Ο€, ←category.assoc, limit.lift_Ο€]; exact pullback_cone.condition _) (by simp) (by simp) $ Ξ» s m h₁ hβ‚‚, by { ext, { simpa using h₁ }, { simpa using hβ‚‚ } } } section local attribute [instance] has_limit_cospan_of_has_limit_pair_of_has_limit_parallel_pair /-- If a category has all binary products and all equalizers, then it also has all pullbacks. As usual, this is not an instance, since there may be a more direct way to construct pullbacks. -/ lemma has_pullbacks_of_has_binary_products_of_has_equalizers (C : Type u) [π’ž : category.{v} C] [has_binary_products C] [has_equalizers C] : has_pullbacks C := { has_limit := Ξ» F, has_limit_of_iso (diagram_iso_cospan F).symm } end /-- If the coproduct `Y β¨Ώ Z` and the coequalizer of `f ≫ ι₁` and `g ≫ ΞΉβ‚‚` exist, then the pushout of `f` and `g` exists: It is given by composing the inclusions with the coequalizer. -/ lemma has_colimit_span_of_has_colimit_pair_of_has_colimit_parallel_pair {C : Type u} [π’ž : category.{v} C] {X Y Z : C} (f : X ⟢ Y) (g : X ⟢ Z) [has_colimit (pair Y Z)] [has_colimit (parallel_pair (f ≫ coprod.inl) (g ≫ coprod.inr))] : has_colimit (span f g) := let ι₁ : Y ⟢ Y β¨Ώ Z := coprod.inl, ΞΉβ‚‚ : Z ⟢ Y β¨Ώ Z := coprod.inr, c := coequalizer.Ο€ (f ≫ ι₁) (g ≫ ΞΉβ‚‚) in has_colimit.mk { cocone := pushout_cocone.mk (ι₁ ≫ c) (ΞΉβ‚‚ ≫ c) $ by rw [←category.assoc, ←category.assoc, coequalizer.condition], is_colimit := pushout_cocone.is_colimit.mk _ _ _ (Ξ» s, coequalizer.desc (coprod.desc (s.ΞΉ.app walking_span.left) (s.ΞΉ.app walking_span.right)) $ by rw [category.assoc, colimit.ΞΉ_desc, category.assoc, colimit.ΞΉ_desc]; exact pushout_cocone.condition _) (by simp) (by simp) $ Ξ» s m h₁ hβ‚‚, by { ext, { simpa using h₁ }, { simpa using hβ‚‚ } } } section local attribute [instance] has_colimit_span_of_has_colimit_pair_of_has_colimit_parallel_pair /-- If a category has all binary coproducts and all coequalizers, then it also has all pushouts. As usual, this is not an instance, since there may be a more direct way to construct pushouts. -/ lemma has_pushouts_of_has_binary_coproducts_of_has_coequalizers (C : Type u) [π’ž : category.{v} C] [has_binary_coproducts C] [has_coequalizers C] : has_pushouts C := has_pushouts_of_has_colimit_span C end end category_theory.limits
b081f42f0f1cc58967336664ffbdea0f50017b95
b87641ffb6358d6508ccbfa54e67c87070cb28d8
/ap_cat/src/Preorders.lean
e370c218ab46e3119efc525672372c2e58c3a8bd
[]
no_license
Nolrai/LeanGiggle
a4b628745ae3f5a36ae79b673ee8543e18ed4899
8326b2a6685b60a3529ee0fe26bd86f5d849b071
refs/heads/master
1,545,396,766,168
1,538,238,458,000
1,538,238,458,000
105,524,473
0
0
null
null
null
null
UTF-8
Lean
false
false
5,424
lean
namespace preorder section preorder variable T : Type def is_upper_bound [preorder T] : set T -> T -> Prop := Ξ» S x, βˆ€ y, y ∈ S -> y ≀ x structure Op := op :: (unop : T) variable {T} def op (x : T) : Op T := Op.op x def unop (x : Op T) : T := Op.unop x def op_rel (R : T -> T -> Prop) (x y : Op T) := R (unop y) (unop x) def op_refl {R : T -> T -> Prop} (H : βˆ€ x : T, R x x) : (βˆ€ x, op_rel R x x) := begin unfold op_rel, exact (Ξ» x, H (unop x)) end def op_trans {R : T -> T -> Prop} : transitive R -> transitive (op_rel R) := begin intros H a b c , unfold op_rel , intros , apply H , all_goals {assumption} end def op_le [H : preorder T] := op_rel H.le instance op_preorder [H : preorder T] : preorder (Op T) := { le := op_le , le_refl := op_refl H.le_refl , le_trans := op_trans H.le_trans } variable {T} def pointwise (R : T -> T -> Prop) {A} (f g : A -> T) := βˆ€ x, R (f x) (g x) private def pointwise_relf {R : T -> T -> Prop} (H : reflexive R) {A : Type} : reflexive (@pointwise _ R A) := begin revert H, unfold reflexive pointwise, intros, apply H, end private def pointwise_trans_helper (R : T -> T -> Prop) (H : βˆ€ ⦃ x y z ⦄, R x y -> R y z -> R x z) {A} {f h g : A -> T} (FG : βˆ€ x, R (f x) (g x)) (GH : βˆ€ x, R (g x) (h x)) := Ξ» (a : A), H (FG a) (GH a) private def pointwise_trans {R : T -> T -> Prop} (H : transitive R) {A : Type} : @transitive (A β†’ T) (@pointwise T R A) := begin intros , unfold transitive pointwise at * , intros , apply (pointwise_trans_helper R H a a_1) end instance pointwise_preorder {A : Type} [H : preorder T] : preorder (A β†’ T) := { le := pointwise (Ξ» a b, a ≀ b) , le_refl := begin apply (pointwise_relf H.le_refl) end , le_trans := pointwise_trans H.le_trans} section functions_between variables {A B : Type} [preorder A] [preorder B] variables (f : A β†’ B) (g : B -> A) def monotonic := βˆ€ x y, x ≀ y -> f x ≀ f y variables (mf : monotonic f) (mg : monotonic g) def are_adjoint := βˆ€ a b, f a ≀ b <-> a ≀ g b infix `⊒`:30 := are_adjoint def equiv [preorder T] (a b : T) := (a ≀ b) ∧ (b ≀ a) infix `≃`:50 := equiv def injective [preorder T] (R : A -> T -> Prop) := βˆ€ a b c, R a b -> R a c -> b ≃ c lemma right_adjoints_unique : injective (@are_adjoint A B _ _) := begin intros f g h , unfold are_adjoint , intros fg fh , split; intro , { rewrite ← fh, rewrite fg} , { rewrite ← fg, rewrite fh} end lemma left_adjoints_unique : injective (Ξ» x y, @are_adjoint B A _ _ y x) := begin intros h f g , unfold are_adjoint , intros fh gh , split; intro , { rewrite fh, rewrite ← gh} , { rewrite gh, rewrite ← fh} end end functions_between definition bounds_above [preorder T] (S : set T) (x : T) : Prop := βˆ€ y, y ∈ S β†’ y ≀ x definition bounds_below [preorder T] (S : set T) (x : T) : Prop := βˆ€ y : T, y ∈ S -> x ≀ y end preorder end preorder open preorder section variable T : Type def inclusion_trans (a b c : set T) (AB : a βŠ† b) (BC : b βŠ† c) : a βŠ† c := Ξ» t tInA, BC (AB tInA) instance set_inclusion : preorder (set T) := { preorder . le := set.subset , le_refl := Ξ» A t inA, inA , le_trans := inclusion_trans T } structure newtype (name : string) : Type := ( old : T ) infix `!`:50 := newtype structure equiv extends preorder T := (symmetric : symmetric le) structure partition := (box : T -> set T) (correct : βˆ€ t s, t ∈ box s <-> box t = box s) infix `⊞` : 60 := Ξ» P x, partition.box P x variable {T} instance partition.part_box : has_coe_to_fun (partition T) := { F := Ξ» _, T β†’ set T , coe := Ξ» (P : partition T) x, P ⊞ x } -- Note the switch of P and Q. def refined_by (P Q : partition T) : Prop := forall x, Q.box x ≀ P.box x def partition.indit (P : partition T) (x y : T) : Prop := y ∈ P x def partition.dit (P : partition T) (x y : T) : Prop := y βˆ‰ P x instance partition_refinement : preorder (partition T) := { le := refined_by , le_refl := Ξ» _ _ _ H, H , le_trans := Ξ» _ _ _ f g t _ x, f t (g t x) } class has_meet (T : Type) := (meet : T -> T -> T) def meet [m : has_meet T] (a b : T) : T := has_meet.meet a b notation `⊻`:50 := meet -- lean uses ∨ for the or class has_top (T : Type) := (top : T) def top {T} [t : has_top T] : T := t.top class meet_lattice (T : Type) extends preorder T, has_meet T, has_top T := (meet_greater : βˆ€ a b z : T, z ≀ a -> z ≀ b -> z ≀ meet a b) (meet_less_l : βˆ€ a b, meet a b ≀ a) (meet_less_r : βˆ€ a b, meet a b ≀ b) (top_greater : βˆ€ a, a ≀ top) class has_join (T : Type) := {join : T -> T -> T} def join [m : has_join T] (a b : T) : T := has_join.join a b notation `⊼`:50 := join -- lean uses ∧ for the and Proposition class has_bottom (T : Type) := (bottom : T) def bottom {T:Type} [has_bottom T] : T := @has_bottom.bottom T _ class join_lattice (T : Type) extends preorder T, has_join T, has_bottom T := (join_less : βˆ€ a b z, a ≀ z β†’ b ≀ z -> join a b ≀ z) (join_greater_l : βˆ€ a b, a ≀ join a b ) (join_greater_r : βˆ€ a b, b ≀ join a b ) (bottom_less : βˆ€ a , bottom ≀ a) class lattice extends preorder T: Type := (to_meet_lattice : meet_lattice T) (to_join_lattice : join_lattice T) end
07829b14dbbaa59f190c30682778bf2497ff3878
54d7e71c3616d331b2ec3845d31deb08f3ff1dea
/library/init/category/functor.lean
4db4ec3fa17d845dddaf09f049ccfae51e451819
[ "Apache-2.0" ]
permissive
pachugupta/lean
6f3305c4292288311cc4ab4550060b17d49ffb1d
0d02136a09ac4cf27b5c88361750e38e1f485a1a
refs/heads/master
1,611,110,653,606
1,493,130,117,000
1,493,167,649,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,158
lean
/- Copyright (c) Luke Nelson and Jared Roesch. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luke Nelson, Jared Roesch, Sebastian Ullrich -/ prelude import init.core init.function init.meta.name open function universes u v section set_option auto_param.check_exists false class functor (f : Type u β†’ Type v) : Type (max u+1 v) := (map : Ξ  {Ξ± Ξ² : Type u}, (Ξ± β†’ Ξ²) β†’ f Ξ± β†’ f Ξ²) (infixr ` <$> `:100 := map) -- ` <$ ` (map_const : Ξ  {Ξ± : Type u} (Ξ² : Type u), Ξ± β†’ f Ξ² β†’ f Ξ± := Ξ» Ξ± Ξ², map ∘ const Ξ²) (map_const_eq : βˆ€ {Ξ± Ξ² : Type u}, @map_const Ξ± Ξ² = map ∘ const Ξ² . control_laws_tac) -- `functor` is indeed a categorical functor (id_map : Ξ  {Ξ± : Type u} (x : f Ξ±), id <$> x = x) (map_comp : Ξ  {Ξ± Ξ² Ξ³ : Type u} (g : Ξ± β†’ Ξ²) (h : Ξ² β†’ Ξ³) (x : f Ξ±), (h ∘ g) <$> x = h <$> g <$> x) end infixr ` <$> `:100 := functor.map infixr ` <$ `:100 := functor.map_const infixr ` $> `:100 := Ξ» Ξ± a b, b <$ a -- TODO(sullrich): remove? @[reducible,inline] def fmap {f : Type u β†’ Type v} [functor f] {Ξ± Ξ² : Type u} : (Ξ± β†’ Ξ²) β†’ f Ξ± β†’ f Ξ² := functor.map
9b6552bcb9ee14da89f093dbcc0a13d95707516c
9dc8cecdf3c4634764a18254e94d43da07142918
/src/probability/martingale/optional_stopping.lean
86c7f5f882d4b32d1344e9ee49b6eff340608cd7
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
12,122
lean
/- Copyright (c) 2022 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import probability.martingale.basic /-! # Optional stopping theorem (fair game theorem) The optional stopping theorem states that an adapted integrable process `f` is a submartingale if and only if for all bounded stopping times `Ο„` and `Ο€` such that `Ο„ ≀ Ο€`, the stopped value of `f` at `Ο„` has expectation smaller than its stopped value at `Ο€`. This file also contains Doob's maximal inequality: given a non-negative submartingale `f`, for all `Ξ΅ : ℝβ‰₯0`, we have `Ξ΅ β€’ ΞΌ {Ξ΅ ≀ f* n} ≀ ∫ Ο‰ in {Ξ΅ ≀ f* n}, f n` where `f* n Ο‰ = max_{k ≀ n}, f k Ο‰`. ### Main results * `measure_theory.submartingale_iff_expected_stopped_value_mono`: the optional stopping theorem. * `measure_theory.submartingale.stopped_process`: the stopped process of a submartingale with respect to a stopping time is a submartingale. * `measure_theory.maximal_ineq`: Doob's maximal inequality. -/ open_locale nnreal ennreal measure_theory probability_theory namespace measure_theory variables {Ξ© : Type*} {m0 : measurable_space Ξ©} {ΞΌ : measure Ξ©} {𝒒 : filtration β„• m0} {f : β„• β†’ Ξ© β†’ ℝ} {Ο„ Ο€ : Ξ© β†’ β„•} -- We may generalize the below lemma to functions taking value in a `normed_lattice_add_comm_group`. -- Similarly, generalize `(super/)submartingale.set_integral_le`. /-- Given a submartingale `f` and bounded stopping times `Ο„` and `Ο€` such that `Ο„ ≀ Ο€`, the expectation of `stopped_value f Ο„` is less than or equal to the expectation of `stopped_value f Ο€`. This is the forward direction of the optional stopping theorem. -/ lemma submartingale.expected_stopped_value_mono [sigma_finite_filtration ΞΌ 𝒒] (hf : submartingale f 𝒒 ΞΌ) (hΟ„ : is_stopping_time 𝒒 Ο„) (hΟ€ : is_stopping_time 𝒒 Ο€) (hle : Ο„ ≀ Ο€) {N : β„•} (hbdd : βˆ€ Ο‰, Ο€ Ο‰ ≀ N) : ΞΌ[stopped_value f Ο„] ≀ ΞΌ[stopped_value f Ο€] := begin rw [← sub_nonneg, ← integral_sub', stopped_value_sub_eq_sum' hle hbdd], { simp only [finset.sum_apply], have : βˆ€ i, measurable_set[𝒒 i] {Ο‰ : Ξ© | Ο„ Ο‰ ≀ i ∧ i < Ο€ Ο‰}, { intro i, refine (hΟ„ i).inter _, convert (hΟ€ i).compl, ext x, simpa }, rw integral_finset_sum, { refine finset.sum_nonneg (Ξ» i hi, _), rw [integral_indicator (𝒒.le _ _ (this _)), integral_sub', sub_nonneg], { exact hf.set_integral_le (nat.le_succ i) (this _) }, { exact (hf.integrable _).integrable_on }, { exact (hf.integrable _).integrable_on } }, intros i hi, exact integrable.indicator (integrable.sub (hf.integrable _) (hf.integrable _)) (𝒒.le _ _ (this _)) }, { exact hf.integrable_stopped_value hΟ€ hbdd }, { exact hf.integrable_stopped_value hΟ„ (Ξ» Ο‰, le_trans (hle Ο‰) (hbdd Ο‰)) } end /-- The converse direction of the optional stopping theorem, i.e. an adapted integrable process `f` is a submartingale if for all bounded stopping times `Ο„` and `Ο€` such that `Ο„ ≀ Ο€`, the stopped value of `f` at `Ο„` has expectation smaller than its stopped value at `Ο€`. -/ lemma submartingale_of_expected_stopped_value_mono [is_finite_measure ΞΌ] (hadp : adapted 𝒒 f) (hint : βˆ€ i, integrable (f i) ΞΌ) (hf : βˆ€ Ο„ Ο€ : Ξ© β†’ β„•, is_stopping_time 𝒒 Ο„ β†’ is_stopping_time 𝒒 Ο€ β†’ Ο„ ≀ Ο€ β†’ (βˆƒ N, βˆ€ Ο‰, Ο€ Ο‰ ≀ N) β†’ ΞΌ[stopped_value f Ο„] ≀ ΞΌ[stopped_value f Ο€]) : submartingale f 𝒒 ΞΌ := begin refine submartingale_of_set_integral_le hadp hint (Ξ» i j hij s hs, _), classical, specialize hf (s.piecewise (Ξ» _, i) (Ξ» _, j)) _ (is_stopping_time_piecewise_const hij hs) (is_stopping_time_const 𝒒 j) (Ξ» x, (ite_le_sup _ _ _).trans (max_eq_right hij).le) ⟨j, Ξ» x, le_rfl⟩, rwa [stopped_value_const, stopped_value_piecewise_const, integral_piecewise (𝒒.le _ _ hs) (hint _).integrable_on (hint _).integrable_on, ← integral_add_compl (𝒒.le _ _ hs) (hint j), add_le_add_iff_right] at hf, end /-- **The optional stopping theorem** (fair game theorem): an adapted integrable process `f` is a submartingale if and only if for all bounded stopping times `Ο„` and `Ο€` such that `Ο„ ≀ Ο€`, the stopped value of `f` at `Ο„` has expectation smaller than its stopped value at `Ο€`. -/ lemma submartingale_iff_expected_stopped_value_mono [is_finite_measure ΞΌ] (hadp : adapted 𝒒 f) (hint : βˆ€ i, integrable (f i) ΞΌ) : submartingale f 𝒒 ΞΌ ↔ βˆ€ Ο„ Ο€ : Ξ© β†’ β„•, is_stopping_time 𝒒 Ο„ β†’ is_stopping_time 𝒒 Ο€ β†’ Ο„ ≀ Ο€ β†’ (βˆƒ N, βˆ€ x, Ο€ x ≀ N) β†’ ΞΌ[stopped_value f Ο„] ≀ ΞΌ[stopped_value f Ο€] := ⟨λ hf _ _ hΟ„ hΟ€ hle ⟨N, hN⟩, hf.expected_stopped_value_mono hΟ„ hΟ€ hle hN, submartingale_of_expected_stopped_value_mono hadp hint⟩ /-- The stopped process of a submartingale with respect to a stopping time is a submartingale. -/ @[protected] lemma submartingale.stopped_process [is_finite_measure ΞΌ] (h : submartingale f 𝒒 ΞΌ) (hΟ„ : is_stopping_time 𝒒 Ο„) : submartingale (stopped_process f Ο„) 𝒒 ΞΌ := begin rw submartingale_iff_expected_stopped_value_mono, { intros Οƒ Ο€ hΟƒ hΟ€ hΟƒ_le_Ο€ hΟ€_bdd, simp_rw stopped_value_stopped_process, obtain ⟨n, hΟ€_le_n⟩ := hΟ€_bdd, exact h.expected_stopped_value_mono (hΟƒ.min hΟ„) (hΟ€.min hΟ„) (Ξ» Ο‰, min_le_min (hΟƒ_le_Ο€ Ο‰) le_rfl) (Ξ» Ο‰, (min_le_left _ _).trans (hΟ€_le_n Ο‰)), }, { exact adapted.stopped_process_of_nat h.adapted hΟ„, }, { exact Ξ» i, h.integrable_stopped_value ((is_stopping_time_const _ i).min hΟ„) (Ξ» Ο‰, min_le_left _ _), }, end section maximal open finset lemma smul_le_stopped_value_hitting [is_finite_measure ΞΌ] (hsub : submartingale f 𝒒 ΞΌ) {Ξ΅ : ℝβ‰₯0} (n : β„•) : Ξ΅ β€’ ΞΌ {Ο‰ | (Ξ΅ : ℝ) ≀ (range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)} ≀ ennreal.of_real (∫ Ο‰ in {Ο‰ | (Ξ΅ : ℝ) ≀ (range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)}, stopped_value f (hitting f {y : ℝ | ↑Ρ ≀ y} 0 n) Ο‰ βˆ‚ΞΌ) := begin have hn : set.Icc 0 n = {k | k ≀ n}, { ext x, simp }, have : βˆ€ Ο‰, ((Ξ΅ : ℝ) ≀ (range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)) β†’ (Ξ΅ : ℝ) ≀ stopped_value f (hitting f {y : ℝ | ↑Ρ ≀ y} 0 n) Ο‰, { intros x hx, simp_rw [le_sup'_iff, mem_range, nat.lt_succ_iff] at hx, refine stopped_value_hitting_mem _, simp only [set.mem_set_of_eq, exists_prop, hn], exact let ⟨j, hj₁, hjβ‚‚βŸ© := hx in ⟨j, hj₁, hjβ‚‚βŸ© }, have h := set_integral_ge_of_const_le (measurable_set_le measurable_const (finset.measurable_range_sup'' (Ξ» n _, (hsub.strongly_measurable n).measurable.le (𝒒.le n)))) (measure_ne_top _ _) this (integrable.integrable_on (hsub.integrable_stopped_value (hitting_is_stopping_time hsub.adapted measurable_set_Ici) hitting_le)), rw [ennreal.le_of_real_iff_to_real_le, ennreal.to_real_smul], { exact h }, { exact ennreal.mul_ne_top (by simp) (measure_ne_top _ _) }, { exact le_trans (mul_nonneg Ξ΅.coe_nonneg ennreal.to_real_nonneg) h } end /-- **Doob's maximal inequality**: Given a non-negative submartingale `f`, for all `Ξ΅ : ℝβ‰₯0`, we have `Ξ΅ β€’ ΞΌ {Ξ΅ ≀ f* n} ≀ ∫ Ο‰ in {Ξ΅ ≀ f* n}, f n` where `f* n Ο‰ = max_{k ≀ n}, f k Ο‰`. In some literature, the Doob's maximal inequality refers to what we call Doob's Lp inequality (which is a corollary of this lemma and will be proved in an upcomming PR). -/ lemma maximal_ineq [is_finite_measure ΞΌ] (hsub : submartingale f 𝒒 ΞΌ) (hnonneg : 0 ≀ f) {Ξ΅ : ℝβ‰₯0} (n : β„•) : Ξ΅ β€’ ΞΌ {Ο‰ | (Ξ΅ : ℝ) ≀ (range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)} ≀ ennreal.of_real (∫ Ο‰ in {Ο‰ | (Ξ΅ : ℝ) ≀ (range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)}, f n Ο‰ βˆ‚ΞΌ) := begin suffices : Ξ΅ β€’ ΞΌ {Ο‰ | (Ξ΅ : ℝ) ≀ (range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)} + ennreal.of_real (∫ Ο‰ in {Ο‰ | ((range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)) < Ξ΅}, f n Ο‰ βˆ‚ΞΌ) ≀ ennreal.of_real (ΞΌ[f n]), { have hadd : ennreal.of_real (∫ Ο‰, f n Ο‰ βˆ‚ΞΌ) = ennreal.of_real (∫ Ο‰ in {Ο‰ | ↑Ρ ≀ ((range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰))}, f n Ο‰ βˆ‚ΞΌ) + ennreal.of_real (∫ Ο‰ in {Ο‰ | ((range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)) < ↑Ρ}, f n Ο‰ βˆ‚ΞΌ), { rw [← ennreal.of_real_add, ← integral_union], { conv_lhs { rw ← integral_univ }, convert rfl, ext Ο‰, change (Ξ΅ : ℝ) ≀ _ ∨ _ < (Ξ΅ : ℝ) ↔ _, simp only [le_or_lt, true_iff] }, { rintro Ο‰ ⟨hω₁ : _ ≀ _, hΟ‰β‚‚ : _ < _⟩, exact (not_le.2 hΟ‰β‚‚) hω₁ }, { exact (measurable_set_lt (finset.measurable_range_sup'' (Ξ» n _, (hsub.strongly_measurable n).measurable.le (𝒒.le n))) measurable_const) }, exacts [(hsub.integrable _).integrable_on, (hsub.integrable _).integrable_on, integral_nonneg (hnonneg _), integral_nonneg (hnonneg _)] }, rwa [hadd, ennreal.add_le_add_iff_right ennreal.of_real_ne_top] at this }, calc Ξ΅ β€’ ΞΌ {Ο‰ | (Ξ΅ : ℝ) ≀ (range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)} + ennreal.of_real (∫ Ο‰ in {Ο‰ | ((range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)) < Ξ΅}, f n Ο‰ βˆ‚ΞΌ) ≀ ennreal.of_real (∫ Ο‰ in {Ο‰ | (Ξ΅ : ℝ) ≀ (range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)}, stopped_value f (hitting f {y : ℝ | ↑Ρ ≀ y} 0 n) Ο‰ βˆ‚ΞΌ) + ennreal.of_real (∫ Ο‰ in {Ο‰ | ((range (n + 1)).sup' nonempty_range_succ (Ξ» k, f k Ο‰)) < Ξ΅}, stopped_value f (hitting f {y : ℝ | ↑Ρ ≀ y} 0 n) Ο‰ βˆ‚ΞΌ) : begin refine add_le_add (smul_le_stopped_value_hitting hsub _) (ennreal.of_real_le_of_real (set_integral_mono_on (hsub.integrable n).integrable_on (integrable.integrable_on (hsub.integrable_stopped_value (hitting_is_stopping_time hsub.adapted measurable_set_Ici) hitting_le)) (measurable_set_lt (finset.measurable_range_sup'' (Ξ» n _, (hsub.strongly_measurable n).measurable.le (𝒒.le n))) measurable_const) _)), intros Ο‰ hΟ‰, rw set.mem_set_of_eq at hΟ‰, have : hitting f {y : ℝ | ↑Ρ ≀ y} 0 n Ο‰ = n, { simp only [hitting, set.mem_set_of_eq, exists_prop, pi.coe_nat, nat.cast_id, ite_eq_right_iff, forall_exists_index, and_imp], intros m hm hΞ΅m, exact false.elim ((not_le.2 hΟ‰) ((le_sup'_iff _).2 ⟨m, mem_range.2 (nat.lt_succ_of_le hm.2), hΞ΅m⟩)) }, simp_rw [stopped_value, this], end ... = ennreal.of_real (∫ Ο‰, stopped_value f (hitting f {y : ℝ | ↑Ρ ≀ y} 0 n) Ο‰ βˆ‚ΞΌ) : begin rw [← ennreal.of_real_add, ← integral_union], { conv_rhs { rw ← integral_univ }, convert rfl, ext Ο‰, change _ ↔ (Ξ΅ : ℝ) ≀ _ ∨ _ < (Ξ΅ : ℝ), simp only [le_or_lt, iff_true] }, { rintro Ο‰ ⟨hω₁ : _ ≀ _, hΟ‰β‚‚ : _ < _⟩, exact (not_le.2 hΟ‰β‚‚) hω₁ }, { exact (measurable_set_lt (finset.measurable_range_sup'' (Ξ» n _, (hsub.strongly_measurable n).measurable.le (𝒒.le n))) measurable_const) }, { exact (integrable.integrable_on (hsub.integrable_stopped_value (hitting_is_stopping_time hsub.adapted measurable_set_Ici) hitting_le)) }, { exact (integrable.integrable_on (hsub.integrable_stopped_value (hitting_is_stopping_time hsub.adapted measurable_set_Ici) hitting_le)) }, exacts [integral_nonneg (Ξ» x, hnonneg _ _), integral_nonneg (Ξ» x, hnonneg _ _)], end ... ≀ ennreal.of_real (ΞΌ[f n]) : begin refine ennreal.of_real_le_of_real _, rw ← stopped_value_const f n, exact hsub.expected_stopped_value_mono (hitting_is_stopping_time hsub.adapted measurable_set_Ici) (is_stopping_time_const _ _) (Ξ» Ο‰, hitting_le Ο‰) (Ξ» Ο‰, le_rfl : βˆ€ Ο‰, n ≀ n), end end end maximal end measure_theory
7ead96be57f95284853508a4ee26315ecd6f1fb8
7cef822f3b952965621309e88eadf618da0c8ae9
/src/topology/algebra/uniform_ring.lean
e253b4d355bbc0ae2321bc4b87056ac358ba865e
[ "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
7,240
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes HΓΆlzl Theory of topological rings with uniform structure. -/ import topology.algebra.group_completion topology.algebra.ring open classical set lattice filter topological_space add_comm_group open_locale classical noncomputable theory namespace uniform_space.completion open dense_inducing uniform_space function variables (Ξ± : Type*) [ring Ξ±] [uniform_space Ξ±] instance : has_one (completion Ξ±) := ⟨(1:Ξ±)⟩ instance : has_mul (completion Ξ±) := ⟨curry $ (dense_inducing_coe.prod dense_inducing_coe).extend (coe ∘ uncurry' (*))⟩ @[elim_cast] lemma coe_one : ((1 : Ξ±) : completion Ξ±) = 1 := rfl variables {Ξ±} [topological_ring Ξ±] @[move_cast] lemma coe_mul (a b : Ξ±) : ((a * b : Ξ±) : completion Ξ±) = a * b := ((dense_inducing_coe.prod dense_inducing_coe).extend_eq_of_cont ((continuous_coe Ξ±).comp continuous_mul) (a, b)).symm variables [uniform_add_group Ξ±] lemma continuous_mul : continuous (Ξ» p : completion Ξ± Γ— completion Ξ±, p.1 * p.2) := begin haveI : is_Z_bilin ((coe ∘ uncurry' (*)) : Ξ± Γ— Ξ± β†’ completion Ξ±) := { add_left := begin introv, change coe ((a + a')*b) = coe (a*b) + coe (a'*b), rw_mod_cast add_mul end, add_right := begin introv, change coe (a*(b + b')) = coe (a*b) + coe (a*b'), rw_mod_cast mul_add end }, have : continuous ((coe ∘ uncurry' (*)) : Ξ± Γ— Ξ± β†’ completion Ξ±), from (continuous_coe Ξ±).comp continuous_mul, convert dense_inducing_coe.extend_Z_bilin dense_inducing_coe this, simp only [(*), curry, prod.mk.eta] end lemma continuous.mul {Ξ² : Type*} [topological_space Ξ²] {f g : Ξ² β†’ completion Ξ±} (hf : continuous f) (hg : continuous g) : continuous (Ξ»b, f b * g b) := continuous_mul.comp (continuous.prod_mk hf hg) instance : ring (completion Ξ±) := { one_mul := assume a, completion.induction_on a (is_closed_eq (continuous.mul continuous_const continuous_id) continuous_id) (assume a, by rw [← coe_one, ← coe_mul, one_mul]), mul_one := assume a, completion.induction_on a (is_closed_eq (continuous.mul continuous_id continuous_const) continuous_id) (assume a, by rw [← coe_one, ← coe_mul, mul_one]), mul_assoc := assume a b c, completion.induction_on₃ a b c (is_closed_eq (continuous.mul (continuous.mul continuous_fst (continuous_fst.comp continuous_snd)) (continuous_snd.comp continuous_snd)) (continuous.mul continuous_fst (continuous.mul (continuous_fst.comp continuous_snd) (continuous_snd.comp continuous_snd)))) (assume a b c, by rw [← coe_mul, ← coe_mul, ← coe_mul, ← coe_mul, mul_assoc]), left_distrib := assume a b c, completion.induction_on₃ a b c (is_closed_eq (continuous.mul continuous_fst (continuous.add (continuous_fst.comp continuous_snd) (continuous_snd.comp continuous_snd))) (continuous.add (continuous.mul continuous_fst (continuous_fst.comp continuous_snd)) (continuous.mul continuous_fst (continuous_snd.comp continuous_snd)))) (assume a b c, by rw [← coe_add, ← coe_mul, ← coe_mul, ← coe_mul, ←coe_add, mul_add]), right_distrib := assume a b c, completion.induction_on₃ a b c (is_closed_eq (continuous.mul (continuous.add continuous_fst (continuous_fst.comp continuous_snd)) (continuous_snd.comp continuous_snd)) (continuous.add (continuous.mul continuous_fst (continuous_snd.comp continuous_snd)) (continuous.mul (continuous_fst.comp continuous_snd) (continuous_snd.comp continuous_snd)))) (assume a b c, by rw [← coe_add, ← coe_mul, ← coe_mul, ← coe_mul, ←coe_add, add_mul]), ..completion.add_comm_group, ..completion.has_mul Ξ±, ..completion.has_one Ξ± } instance is_ring_hom_coe : is_ring_hom (coe : Ξ± β†’ completion Ξ±) := ⟨coe_one Ξ±, assume a b, coe_mul a b, assume a b, coe_add a b⟩ universes u variables {Ξ² : Type u} [uniform_space Ξ²] [ring Ξ²] [uniform_add_group Ξ²] [topological_ring Ξ²] {f : Ξ± β†’ Ξ²} [is_ring_hom f] (hf : continuous f) instance is_ring_hom_extension [complete_space Ξ²] [separated Ξ²] : is_ring_hom (completion.extension f) := have hf : uniform_continuous f, from uniform_continuous_of_continuous hf, { map_one := by rw [← coe_one, extension_coe hf, is_ring_hom.map_one f], map_add := assume a b, completion.induction_onβ‚‚ a b (is_closed_eq (continuous_extension.comp continuous_add) ((continuous_extension.comp continuous_fst).add (continuous_extension.comp continuous_snd))) (assume a b, by rw [← coe_add, extension_coe hf, extension_coe hf, extension_coe hf, is_add_hom.map_add f]), map_mul := assume a b, completion.induction_onβ‚‚ a b (is_closed_eq (continuous_extension.comp continuous_mul) ((continuous_extension.comp continuous_fst).mul (continuous_extension.comp continuous_snd))) (assume a b, by rw [← coe_mul, extension_coe hf, extension_coe hf, extension_coe hf, is_ring_hom.map_mul f]) } instance top_ring_compl : topological_ring (completion Ξ±) := { continuous_add := continuous_add, continuous_mul := continuous_mul, continuous_neg := continuous_neg } instance is_ring_hom_map : is_ring_hom (completion.map f) := (completion.is_ring_hom_extension $ (continuous_coe Ξ²).comp hf : _) variables (R : Type*) [comm_ring R] [uniform_space R] [uniform_add_group R] [topological_ring R] instance : comm_ring (completion R) := { mul_comm := assume a b, completion.induction_onβ‚‚ a b (is_closed_eq (continuous_fst.mul continuous_snd) (continuous_snd.mul continuous_fst)) (assume a b, by rw [← coe_mul, ← coe_mul, mul_comm]), ..completion.ring } end uniform_space.completion namespace uniform_space variables {Ξ± : Type*} lemma ring_sep_rel (Ξ±) [comm_ring Ξ±] [uniform_space Ξ±] [uniform_add_group Ξ±] [topological_ring Ξ±] : separation_setoid Ξ± = submodule.quotient_rel (ideal.closure βŠ₯) := setoid.ext $ assume x y, group_separation_rel x y lemma ring_sep_quot (Ξ±) [r : comm_ring Ξ±] [uniform_space Ξ±] [uniform_add_group Ξ±] [topological_ring Ξ±] : quotient (separation_setoid Ξ±) = (βŠ₯ : ideal Ξ±).closure.quotient := by rw [@ring_sep_rel Ξ± r]; refl def sep_quot_equiv_ring_quot (Ξ±) [r : comm_ring Ξ±] [uniform_space Ξ±] [uniform_add_group Ξ±] [topological_ring Ξ±] : quotient (separation_setoid Ξ±) ≃ (βŠ₯ : ideal Ξ±).closure.quotient := quotient.congr_right $ assume x y, group_separation_rel x y /- TODO: use a form of transport a.k.a. lift definition a.k.a. transfer -/ instance [comm_ring Ξ±] [uniform_space Ξ±] [uniform_add_group Ξ±] [topological_ring Ξ±] : comm_ring (quotient (separation_setoid Ξ±)) := by rw ring_sep_quot Ξ±; apply_instance instance [comm_ring Ξ±] [uniform_space Ξ±] [uniform_add_group Ξ±] [topological_ring Ξ±] : topological_ring (quotient (separation_setoid Ξ±)) := begin convert topological_ring_quotient (βŠ₯ : ideal Ξ±).closure; try {apply ring_sep_rel}, simp [uniform_space.comm_ring] end end uniform_space
8ac3ce54a5646956bac0c07459fbe251ceef872a
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/getline_crash.lean
a98170c8e8f18411d05330a50a912c28478175fe
[ "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
2,227
lean
def tstGetLine (str : String) : IO Unit := do let path := "tmp_file"; IO.FS.withFile path IO.FS.Mode.write $ Ξ» (h : IO.FS.Handle) => h.putStrLn str; IO.FS.withFile path IO.FS.Mode.read $ Ξ» (h : IO.FS.Handle) => do str' ← h.getLine; IO.println str.length; IO.println str'.length; IO.print str'; unless (str'.length == str.length + 1) $ throw (IO.userError ("unexpected length: " ++ toString str'.trim.length)); unless (str'.trim == str) $ throw (IO.userError ("unexpected result: " ++ str')) def tstGetLine2 (str1 str2 : String) : IO Unit := do let path := "tmp_file"; IO.FS.withFile path IO.FS.Mode.write $ Ξ» (h : IO.FS.Handle) => do { h.putStrLn str1; h.putStr str2 }; IO.FS.withFile path IO.FS.Mode.read $ Ξ» (h : IO.FS.Handle) => do str1' ← h.getLine; str2' ← h.getLine; unless (str1'.length == str1.length + 1) $ throw (IO.userError ("unexpected length: " ++ toString str1'.trim.length)); unless (str1'.trim == str1) $ throw (IO.userError ("unexpected result: " ++ str1')); unless (str2'.length == str2.length) $ throw (IO.userError ("unexpected length: " ++ toString str2'.trim.length)); unless (str2'.trim == str2) $ throw (IO.userError ("unexpected result: " ++ str2')) def tstGetLine3 (str : String) : IO Unit := do let path := "tmp_file"; IO.FS.withFile path IO.FS.Mode.write $ Ξ» (h : IO.FS.Handle) => do { h.putStrLn str }; IO.FS.withFile path IO.FS.Mode.read $ Ξ» (h : IO.FS.Handle) => do (h.getLine >>= IO.println); (h.getLine >>= IO.println); (h.getLine >>= IO.println); IO.print "done"; pure () #eval tstGetLine3 "abc" #eval tstGetLine ("".pushn 'Ξ±' 40) #eval tstGetLine "a" #eval tstGetLine "" #eval tstGetLine ("".pushn 'Ξ±' 20) #eval tstGetLine ("".pushn 'a' 61) #eval tstGetLine ("".pushn 'Ξ±' 61) #eval tstGetLine ("".pushn 'a' 62) #eval tstGetLine ("".pushn 'a' 63) #eval tstGetLine ("".pushn 'a' 64) #eval tstGetLine ("".pushn 'a' 65) #eval tstGetLine ("".pushn 'a' 66) #eval tstGetLine ("".pushn 'a' 128) #eval tstGetLine2 ("".pushn 'Ξ±' 20) ("".pushn 'Ξ²' 20) #eval tstGetLine2 ("".pushn 'Ξ±' 40) ("".pushn 'Ξ²' 40) #eval tstGetLine2 ("".pushn 'a' 61) ("".pushn 'b' 61) #eval tstGetLine2 ("".pushn 'a' 61) ("".pushn 'b' 62)
9e99191a13e4dabfe4ce296c734602e0a9b0023c
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Meta/CollectFVars.lean
f787070eea90ea92dc81deedc11a608b5b461005
[ "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
2,385
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Util.CollectFVars import Lean.Meta.Basic namespace Lean open Meta def Expr.collectFVars (e : Expr) : StateRefT CollectFVars.State MetaM Unit := do let e ← instantiateMVars e modify fun used => Lean.collectFVars used e def LocalDecl.collectFVars (localDecl : LocalDecl) : StateRefT CollectFVars.State MetaM Unit := do match localDecl with | .cdecl (type := type) .. => type.collectFVars | .ldecl (type := type) (value := value) .. => type.collectFVars; value.collectFVars /-- For each variable in `s.fvarSet`, include its dependencies. -/ partial def CollectFVars.State.addDependencies (s : CollectFVars.State) : MetaM CollectFVars.State := do let (_, s) ← go |>.run 0 |>.run s return s where getNext? : StateRefT Nat (StateRefT CollectFVars.State MetaM) (Option FVarId) := do let s ← getThe CollectFVars.State let i ← get if h : i < s.fvarIds.size then let r := s.fvarIds.get ⟨i, h⟩ modify (Β· + 1) return some r else return none go : StateRefT Nat (StateRefT CollectFVars.State MetaM) Unit := do let some fvarId ← getNext? | return () /- We don't use `getLocalDecl` because `CollectFVars.State` may contains local variables that are not in the current local context. Recall that we use this method to process match-expressions, and each AltLHS has each own its extra local declarations. -/ let some localDecl := (← getLCtx).find? fvarId | return () localDecl.collectFVars go namespace Meta def removeUnused (vars : Array Expr) (used : CollectFVars.State) : MetaM (LocalContext Γ— LocalInstances Γ— Array Expr) := do let localInsts ← getLocalInstances let lctx ← getLCtx let (lctx, localInsts, newVars, _) ← vars.foldrM (fun var (lctx, localInsts, newVars, used) => do if used.fvarSet.contains var.fvarId! then let varType ← inferType var let (_, used) ← varType.collectFVars.run used pure (lctx, localInsts, newVars.push var, used) else pure (lctx.erase var.fvarId!, localInsts.erase var.fvarId!, newVars, used)) (lctx, localInsts, #[], used) pure (lctx, localInsts, newVars.reverse) end Meta end Lean
ab1c5239e9a1bad0dc2366457fc65f7a2994cbdf
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/limits/constructions/limits_of_products_and_equalizers.lean
c022171b26cb247bc841b17ed09a2d415a24c9bf
[ "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
18,283
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Scott Morrison -/ import data.fintype.prod import data.fintype.sigma import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.finite_products import category_theory.limits.preserves.shapes.products import category_theory.limits.preserves.shapes.equalizers import category_theory.limits.preserves.finite import category_theory.limits.constructions.finite_products_of_binary_products import category_theory.limits.constructions.equalizers import category_theory.limits.constructions.binary_products /-! # Constructing limits from products and equalizers. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. If a category has all products, and all equalizers, then it has all limits. Similarly, if it has all finite products, and all equalizers, then it has all finite limits. If a functor preserves all products and equalizers, then it preserves all limits. Similarly, if it preserves all finite products and equalizers, then it preserves all finite limits. # TODO Provide the dual results. Show the analogous results for functors which reflect or create (co)limits. -/ open category_theory open opposite namespace category_theory.limits universes w v vβ‚‚ u uβ‚‚ variables {C : Type u} [category.{v} C] variables {J : Type w} [small_category J] -- We hide the "implementation details" inside a namespace namespace has_limit_of_has_products_of_has_equalizers variables {F : J β₯€ C} {c₁ : fan F.obj} {cβ‚‚ : fan (Ξ» f : (Ξ£ p : J Γ— J, p.1 ⟢ p.2), F.obj f.1.2)} (s t : c₁.X ⟢ cβ‚‚.X) (hs : βˆ€ (f : Ξ£ p : J Γ— J, p.1 ⟢ p.2), s ≫ cβ‚‚.Ο€.app ⟨f⟩ = c₁.Ο€.app ⟨f.1.1⟩ ≫ F.map f.2) (ht : βˆ€ (f : Ξ£ p : J Γ— J, p.1 ⟢ p.2), t ≫ cβ‚‚.Ο€.app ⟨f⟩ = c₁.Ο€.app ⟨f.1.2⟩) (i : fork s t) include hs ht /-- (Implementation) Given the appropriate product and equalizer cones, build the cone for `F` which is limiting if the given cones are also. -/ @[simps] def build_limit : cone F := { X := i.X, Ο€ := { app := Ξ» j, i.ΞΉ ≫ c₁.Ο€.app ⟨_⟩, naturality' := Ξ» j₁ jβ‚‚ f, begin dsimp, rw [category.id_comp, category.assoc, ← hs ⟨⟨_, _⟩, f⟩, i.condition_assoc, ht], end} } variable {i} /-- (Implementation) Show the cone constructed in `build_limit` is limiting, provided the cones used in its construction are. -/ def build_is_limit (t₁ : is_limit c₁) (tβ‚‚ : is_limit cβ‚‚) (hi : is_limit i) : is_limit (build_limit s t hs ht i) := { lift := Ξ» q, begin refine hi.lift (fork.of_ΞΉ _ _), { refine t₁.lift (fan.mk _ (Ξ» j, _)), apply q.Ο€.app j }, { apply tβ‚‚.hom_ext, intro j, discrete_cases, simp [hs, ht] }, end, uniq' := Ξ» q m w, hi.hom_ext (i.equalizer_ext (t₁.hom_ext (Ξ» j, by { cases j, simpa using w j }))) } end has_limit_of_has_products_of_has_equalizers open has_limit_of_has_products_of_has_equalizers /-- Given the existence of the appropriate (possibly finite) products and equalizers, we can construct a limit cone for `F`. (This assumes the existence of all equalizers, which is technically stronger than needed.) -/ noncomputable def limit_cone_of_equalizer_and_product (F : J β₯€ C) [has_limit (discrete.functor F.obj)] [has_limit (discrete.functor (Ξ» f : (Ξ£ p : J Γ— J, p.1 ⟢ p.2), F.obj f.1.2))] [has_equalizers C] : limit_cone F := { cone := _, is_limit := build_is_limit (pi.lift (Ξ» f, limit.Ο€ (discrete.functor F.obj) ⟨_⟩ ≫ F.map f.2)) (pi.lift (Ξ» f, limit.Ο€ (discrete.functor F.obj) ⟨f.1.2⟩)) (by simp) (by simp) (limit.is_limit _) (limit.is_limit _) (limit.is_limit _) } /-- Given the existence of the appropriate (possibly finite) products and equalizers, we know a limit of `F` exists. (This assumes the existence of all equalizers, which is technically stronger than needed.) -/ lemma has_limit_of_equalizer_and_product (F : J β₯€ C) [has_limit (discrete.functor F.obj)] [has_limit (discrete.functor (Ξ» f : (Ξ£ p : J Γ— J, p.1 ⟢ p.2), F.obj f.1.2))] [has_equalizers C] : has_limit F := has_limit.mk (limit_cone_of_equalizer_and_product F) /-- A limit can be realised as a subobject of a product. -/ noncomputable def limit_subobject_product [has_limits_of_size.{w w} C] (F : J β₯€ C) : limit F ⟢ ∏ (Ξ» j, F.obj j) := (limit.iso_limit_cone (limit_cone_of_equalizer_and_product F)).hom ≫ equalizer.ΞΉ _ _ instance limit_subobject_product_mono [has_limits_of_size.{w w} C] (F : J β₯€ C) : mono (limit_subobject_product F) := mono_comp _ _ /-- Any category with products and equalizers has all limits. See <https://stacks.math.columbia.edu/tag/002N>. -/ lemma has_limits_of_has_equalizers_and_products [has_products.{w} C] [has_equalizers C] : has_limits_of_size.{w w} C := { has_limits_of_shape := Ξ» J π’₯, { has_limit := Ξ» F, by exactI has_limit_of_equalizer_and_product F } } /-- Any category with finite products and equalizers has all finite limits. See <https://stacks.math.columbia.edu/tag/002O>. -/ lemma has_finite_limits_of_has_equalizers_and_finite_products [has_finite_products C] [has_equalizers C] : has_finite_limits C := ⟨λ J _ _, { has_limit := Ξ» F, by exactI has_limit_of_equalizer_and_product F }⟩ variables {D : Type uβ‚‚} [category.{vβ‚‚} D] noncomputable theory section variables [has_limits_of_shape (discrete J) C] [has_limits_of_shape (discrete (Ξ£ p : J Γ— J, p.1 ⟢ p.2)) C] [has_equalizers C] variables (G : C β₯€ D) [preserves_limits_of_shape walking_parallel_pair G] [preserves_limits_of_shape (discrete.{w} J) G] [preserves_limits_of_shape (discrete.{w} (Ξ£ p : J Γ— J, p.1 ⟢ p.2)) G] /-- If a functor preserves equalizers and the appropriate products, it preserves limits. -/ def preserves_limit_of_preserves_equalizers_and_product : preserves_limits_of_shape J G := { preserves_limit := Ξ» K, begin let P := ∏ K.obj, let Q := ∏ (Ξ» (f : (Ξ£ (p : J Γ— J), p.fst ⟢ p.snd)), K.obj f.1.2), let s : P ⟢ Q := pi.lift (Ξ» f, limit.Ο€ (discrete.functor K.obj) ⟨_⟩ ≫ K.map f.2), let t : P ⟢ Q := pi.lift (Ξ» f, limit.Ο€ (discrete.functor K.obj) ⟨f.1.2⟩), let I := equalizer s t, let i : I ⟢ P := equalizer.ΞΉ s t, apply preserves_limit_of_preserves_limit_cone (build_is_limit s t (by simp) (by simp) (limit.is_limit _) (limit.is_limit _) (limit.is_limit _)), refine is_limit.of_iso_limit (build_is_limit _ _ _ _ _ _ _) _, { exact fan.mk _ (Ξ» j, G.map (pi.Ο€ _ j)) }, { exact fan.mk (G.obj Q) (Ξ» f, G.map (pi.Ο€ _ f)) }, { apply G.map s }, { apply G.map t }, { intro f, dsimp, simp only [←G.map_comp, limit.lift_Ο€, fan.mk_Ο€_app] }, { intro f, dsimp, simp only [←G.map_comp, limit.lift_Ο€, fan.mk_Ο€_app] }, { apply fork.of_ΞΉ (G.map i) _, simp only [← G.map_comp, equalizer.condition] }, { apply is_limit_of_has_product_of_preserves_limit }, { apply is_limit_of_has_product_of_preserves_limit }, { apply is_limit_fork_map_of_is_limit, apply equalizer_is_equalizer }, refine cones.ext (iso.refl _) _, intro j, dsimp, simp, -- See note [dsimp, simp]. end } end /-- If G preserves equalizers and finite products, it preserves finite limits. -/ def preserves_finite_limits_of_preserves_equalizers_and_finite_products [has_equalizers C] [has_finite_products C] (G : C β₯€ D) [preserves_limits_of_shape walking_parallel_pair G] [βˆ€ (J : Type) [fintype J], preserves_limits_of_shape (discrete J) G] : preserves_finite_limits G := ⟨λ _ _ _, by exactI preserves_limit_of_preserves_equalizers_and_product G⟩ /-- If G preserves equalizers and products, it preserves all limits. -/ def preserves_limits_of_preserves_equalizers_and_products [has_equalizers C] [has_products.{w} C] (G : C β₯€ D) [preserves_limits_of_shape walking_parallel_pair G] [βˆ€ J, preserves_limits_of_shape (discrete.{w} J) G] : preserves_limits_of_size.{w w} G := { preserves_limits_of_shape := Ξ» J π’₯, by exactI preserves_limit_of_preserves_equalizers_and_product G } lemma has_finite_limits_of_has_terminal_and_pullbacks [has_terminal C] [has_pullbacks C] : has_finite_limits C := @@has_finite_limits_of_has_equalizers_and_finite_products _ (@@has_finite_products_of_has_binary_and_terminal _ (has_binary_products_of_has_terminal_and_pullbacks C) infer_instance) (@@has_equalizers_of_has_pullbacks_and_binary_products _ (has_binary_products_of_has_terminal_and_pullbacks C) infer_instance) /-- If G preserves terminal objects and pullbacks, it preserves all finite limits. -/ def preserves_finite_limits_of_preserves_terminal_and_pullbacks [has_terminal C] [has_pullbacks C] (G : C β₯€ D) [preserves_limits_of_shape (discrete.{0} pempty) G] [preserves_limits_of_shape walking_cospan G] : preserves_finite_limits G := begin haveI : has_finite_limits C := has_finite_limits_of_has_terminal_and_pullbacks, haveI : preserves_limits_of_shape (discrete walking_pair) G := preserves_binary_products_of_preserves_terminal_and_pullbacks G, exact @@preserves_finite_limits_of_preserves_equalizers_and_finite_products _ _ _ _ G (preserves_equalizers_of_preserves_pullbacks_and_binary_products G) (preserves_finite_products_of_preserves_binary_and_terminal G), end /-! We now dualize the above constructions, resorting to copy-paste. -/ -- We hide the "implementation details" inside a namespace namespace has_colimit_of_has_coproducts_of_has_coequalizers variables {F : J β₯€ C} {c₁ : cofan (Ξ» f : (Ξ£ p : J Γ— J, p.1 ⟢ p.2), F.obj f.1.1)} {cβ‚‚ : cofan F.obj} (s t : c₁.X ⟢ cβ‚‚.X) (hs : βˆ€ (f : Ξ£ p : J Γ— J, p.1 ⟢ p.2), c₁.ΞΉ.app ⟨f⟩ ≫ s = F.map f.2 ≫ cβ‚‚.ΞΉ.app ⟨f.1.2⟩) (ht : βˆ€ (f : Ξ£ p : J Γ— J, p.1 ⟢ p.2), c₁.ΞΉ.app ⟨f⟩ ≫ t = cβ‚‚.ΞΉ.app ⟨f.1.1⟩) (i : cofork s t) include hs ht /-- (Implementation) Given the appropriate coproduct and coequalizer cocones, build the cocone for `F` which is colimiting if the given cocones are also. -/ @[simps] def build_colimit : cocone F := { X := i.X, ΞΉ := { app := Ξ» j, cβ‚‚.ΞΉ.app ⟨_⟩ ≫ i.Ο€, naturality' := Ξ» j₁ jβ‚‚ f, begin dsimp, rw [category.comp_id, ←reassoc_of (hs ⟨⟨_, _⟩, f⟩), i.condition, ←category.assoc, ht], end} } variable {i} /-- (Implementation) Show the cocone constructed in `build_colimit` is colimiting, provided the cocones used in its construction are. -/ def build_is_colimit (t₁ : is_colimit c₁) (tβ‚‚ : is_colimit cβ‚‚) (hi : is_colimit i) : is_colimit (build_colimit s t hs ht i) := { desc := Ξ» q, begin refine hi.desc (cofork.of_Ο€ _ _), { refine tβ‚‚.desc (cofan.mk _ (Ξ» j, _)), apply q.ΞΉ.app j }, { apply t₁.hom_ext, intro j, discrete_cases, simp [reassoc_of hs, reassoc_of ht] }, end, uniq' := Ξ» q m w, hi.hom_ext (i.coequalizer_ext (tβ‚‚.hom_ext (Ξ» j, by { cases j, simpa using w j }))) } end has_colimit_of_has_coproducts_of_has_coequalizers open has_colimit_of_has_coproducts_of_has_coequalizers /-- Given the existence of the appropriate (possibly finite) coproducts and coequalizers, we can construct a colimit cocone for `F`. (This assumes the existence of all coequalizers, which is technically stronger than needed.) -/ noncomputable def colimit_cocone_of_coequalizer_and_coproduct (F : J β₯€ C) [has_colimit (discrete.functor F.obj)] [has_colimit (discrete.functor (Ξ» f : (Ξ£ p : J Γ— J, p.1 ⟢ p.2), F.obj f.1.1))] [has_coequalizers C] : colimit_cocone F := { cocone := _, is_colimit := build_is_colimit (sigma.desc (Ξ» f, F.map f.2 ≫ colimit.ΞΉ (discrete.functor F.obj) ⟨f.1.2⟩)) (sigma.desc (Ξ» f, colimit.ΞΉ (discrete.functor F.obj) ⟨f.1.1⟩)) (by simp) (by simp) (colimit.is_colimit _) (colimit.is_colimit _) (colimit.is_colimit _) } /-- Given the existence of the appropriate (possibly finite) coproducts and coequalizers, we know a colimit of `F` exists. (This assumes the existence of all coequalizers, which is technically stronger than needed.) -/ lemma has_colimit_of_coequalizer_and_coproduct (F : J β₯€ C) [has_colimit (discrete.functor F.obj)] [has_colimit (discrete.functor (Ξ» f : (Ξ£ p : J Γ— J, p.1 ⟢ p.2), F.obj f.1.1))] [has_coequalizers C] : has_colimit F := has_colimit.mk (colimit_cocone_of_coequalizer_and_coproduct F) /-- A colimit can be realised as a quotient of a coproduct. -/ noncomputable def colimit_quotient_coproduct [has_colimits_of_size.{w w} C] (F : J β₯€ C) : ∐ (Ξ» j, F.obj j) ⟢ colimit F := coequalizer.Ο€ _ _ ≫ (colimit.iso_colimit_cocone (colimit_cocone_of_coequalizer_and_coproduct F)).inv instance colimit_quotient_coproduct_epi [has_colimits_of_size.{w w} C] (F : J β₯€ C) : epi (colimit_quotient_coproduct F) := epi_comp _ _ /-- Any category with coproducts and coequalizers has all colimits. See <https://stacks.math.columbia.edu/tag/002P>. -/ lemma has_colimits_of_has_coequalizers_and_coproducts [has_coproducts.{w} C] [has_coequalizers C] : has_colimits_of_size.{w w} C := { has_colimits_of_shape := Ξ» J π’₯, { has_colimit := Ξ» F, by exactI has_colimit_of_coequalizer_and_coproduct F } } /-- Any category with finite coproducts and coequalizers has all finite colimits. See <https://stacks.math.columbia.edu/tag/002Q>. -/ lemma has_finite_colimits_of_has_coequalizers_and_finite_coproducts [has_finite_coproducts C] [has_coequalizers C] : has_finite_colimits C := ⟨λ J _ _, { has_colimit := Ξ» F, by exactI has_colimit_of_coequalizer_and_coproduct F }⟩ noncomputable theory section variables [has_colimits_of_shape (discrete.{w} J) C] [has_colimits_of_shape (discrete.{w} (Ξ£ p : J Γ— J, p.1 ⟢ p.2)) C] [has_coequalizers C] variables (G : C β₯€ D) [preserves_colimits_of_shape walking_parallel_pair G] [preserves_colimits_of_shape (discrete.{w} J) G] [preserves_colimits_of_shape (discrete.{w} (Ξ£ p : J Γ— J, p.1 ⟢ p.2)) G] /-- If a functor preserves coequalizers and the appropriate coproducts, it preserves colimits. -/ def preserves_colimit_of_preserves_coequalizers_and_coproduct : preserves_colimits_of_shape J G := { preserves_colimit := Ξ» K, begin let P := ∐ K.obj, let Q := ∐ (Ξ» (f : (Ξ£ (p : J Γ— J), p.fst ⟢ p.snd)), K.obj f.1.1), let s : Q ⟢ P := sigma.desc (Ξ» f, K.map f.2 ≫ colimit.ΞΉ (discrete.functor K.obj) ⟨_⟩), let t : Q ⟢ P := sigma.desc (Ξ» f, colimit.ΞΉ (discrete.functor K.obj) ⟨f.1.1⟩), let I := coequalizer s t, let i : P ⟢ I := coequalizer.Ο€ s t, apply preserves_colimit_of_preserves_colimit_cocone (build_is_colimit s t (by simp) (by simp) (colimit.is_colimit _) (colimit.is_colimit _) (colimit.is_colimit _)), refine is_colimit.of_iso_colimit (build_is_colimit _ _ _ _ _ _ _) _, { exact cofan.mk (G.obj Q) (Ξ» j, G.map (sigma.ΞΉ _ j)) }, { exact cofan.mk _ (Ξ» f, G.map (sigma.ΞΉ _ f)) }, { apply G.map s }, { apply G.map t }, { intro f, dsimp, simp only [←G.map_comp, colimit.ΞΉ_desc, cofan.mk_ΞΉ_app] }, { intro f, dsimp, simp only [←G.map_comp, colimit.ΞΉ_desc, cofan.mk_ΞΉ_app] }, { apply cofork.of_Ο€ (G.map i) _, simp only [← G.map_comp, coequalizer.condition] }, { apply is_colimit_of_has_coproduct_of_preserves_colimit }, { apply is_colimit_of_has_coproduct_of_preserves_colimit }, { apply is_colimit_cofork_map_of_is_colimit, apply coequalizer_is_coequalizer }, refine cocones.ext (iso.refl _) _, intro j, dsimp, simp, -- See note [dsimp, simp]. end } end /-- If G preserves coequalizers and finite coproducts, it preserves finite colimits. -/ def preserves_finite_colimits_of_preserves_coequalizers_and_finite_coproducts [has_coequalizers C] [has_finite_coproducts C] (G : C β₯€ D) [preserves_colimits_of_shape walking_parallel_pair G] [βˆ€ J [fintype J], preserves_colimits_of_shape (discrete.{0} J) G] : preserves_finite_colimits G := ⟨λ _ _ _, by exactI preserves_colimit_of_preserves_coequalizers_and_coproduct G⟩ /-- If G preserves coequalizers and coproducts, it preserves all colimits. -/ def preserves_colimits_of_preserves_coequalizers_and_coproducts [has_coequalizers C] [has_coproducts.{w} C] (G : C β₯€ D) [preserves_colimits_of_shape walking_parallel_pair G] [βˆ€ J, preserves_colimits_of_shape (discrete.{w} J) G] : preserves_colimits_of_size.{w} G := { preserves_colimits_of_shape := Ξ» J π’₯, by exactI preserves_colimit_of_preserves_coequalizers_and_coproduct G } lemma has_finite_colimits_of_has_initial_and_pushouts [has_initial C] [has_pushouts C] : has_finite_colimits C := @@has_finite_colimits_of_has_coequalizers_and_finite_coproducts _ (@@has_finite_coproducts_of_has_binary_and_initial _ (has_binary_coproducts_of_has_initial_and_pushouts C) infer_instance) (@@has_coequalizers_of_has_pushouts_and_binary_coproducts _ (has_binary_coproducts_of_has_initial_and_pushouts C) infer_instance) /-- If G preserves initial objects and pushouts, it preserves all finite colimits. -/ def preserves_finite_colimits_of_preserves_initial_and_pushouts [has_initial C] [has_pushouts C] (G : C β₯€ D) [preserves_colimits_of_shape (discrete.{0} pempty) G] [preserves_colimits_of_shape walking_span G] : preserves_finite_colimits G := begin haveI : has_finite_colimits C := has_finite_colimits_of_has_initial_and_pushouts, haveI : preserves_colimits_of_shape (discrete walking_pair) G := preserves_binary_coproducts_of_preserves_initial_and_pushouts G, exact @@preserves_finite_colimits_of_preserves_coequalizers_and_finite_coproducts _ _ _ _ G (preserves_coequalizers_of_preserves_pushouts_and_binary_coproducts G) (preserves_finite_coproducts_of_preserves_binary_and_initial G), end end category_theory.limits
642bd43573d9f8199b156e107c730d421ac27872
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/real/pi.lean
a8cdff6ec19757d43fdf5449a320991c831c9fdd
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
7,374
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import analysis.special_functions.trigonometric namespace real lemma pi_gt_sqrt_two_add_series (n : β„•) : 2 ^ (n+1) * sqrt (2 - sqrt_two_add_series 0 n) < pi := begin have : sqrt (2 - sqrt_two_add_series 0 n) / 2 * 2 ^ (n+2) < pi, { rw [← lt_div_iff, ←sin_pi_over_two_pow_succ], apply sin_lt, apply div_pos pi_pos, all_goals { apply pow_pos, norm_num } }, apply lt_of_le_of_lt (le_of_eq _) this, rw [pow_succ _ (n+1), ←mul_assoc, div_mul_cancel, mul_comm], norm_num end lemma pi_lt_sqrt_two_add_series (n : β„•) : pi < 2 ^ (n+1) * sqrt (2 - sqrt_two_add_series 0 n) + 1 / 4 ^ n := begin have : pi < (sqrt (2 - sqrt_two_add_series 0 n) / 2 + 1 / (2 ^ n) ^ 3 / 4) * 2 ^ (n+2), { rw [← div_lt_iff, ← sin_pi_over_two_pow_succ], refine lt_of_lt_of_le (lt_add_of_sub_right_lt (sin_gt_sub_cube _ _)) _, { apply div_pos pi_pos, apply pow_pos, norm_num }, { rw div_le_iff', { refine le_trans pi_le_four _, simp only [show ((4 : ℝ) = 2 ^ 2), by norm_num, mul_one], apply pow_le_pow, norm_num, apply le_add_of_nonneg_left, apply nat.zero_le }, { apply pow_pos, norm_num }}, apply add_le_add_left, rw div_le_div_right, rw [le_div_iff, ←mul_pow], refine le_trans _ (le_of_eq (one_pow 3)), apply pow_le_pow_of_le_left, { apply le_of_lt, apply mul_pos, apply div_pos pi_pos, apply pow_pos, norm_num, apply pow_pos, norm_num }, rw ← le_div_iff, refine le_trans ((div_le_div_right _).mpr pi_le_four) _, apply pow_pos, norm_num, rw [pow_succ, pow_succ, ←mul_assoc, ←div_div_eq_div_mul], convert le_refl _, all_goals { repeat {apply pow_pos}, norm_num }}, apply lt_of_lt_of_le this (le_of_eq _), rw [add_mul], congr' 1, { rw [pow_succ _ (n+1), ←mul_assoc, div_mul_cancel, mul_comm], norm_num }, rw [pow_succ, ←pow_mul, mul_comm n 2, pow_mul, show (2 : ℝ) ^ 2 = 4, by norm_num, pow_succ, pow_succ, ←mul_assoc (2 : ℝ), show (2 : ℝ) * 2 = 4, by norm_num, ←mul_assoc, div_mul_cancel, mul_comm ((2 : ℝ) ^ n), ←div_div_eq_div_mul, div_mul_cancel], apply pow_ne_zero, norm_num, norm_num end /-- From an upper bound on `sqrt_two_add_series 0 n = 2 cos (pi / 2 ^ (n+1))` of the form `sqrt_two_add_series 0 n ≀ 2 - (a / 2 ^ (n + 1)) ^ 2)`, one can deduce the lower bound `a < pi` thanks to basic trigonometric inequalities as expressed in `pi_gt_sqrt_two_add_series`. -/ theorem pi_lower_bound_start (n : β„•) {a} (h : sqrt_two_add_series ((0:β„•) / (1:β„•)) n ≀ 2 - (a / 2 ^ (n + 1)) ^ 2) : a < pi := begin refine lt_of_le_of_lt _ (pi_gt_sqrt_two_add_series n), rw [mul_comm], refine (div_le_iff (pow_pos (by norm_num) _ : (0 : ℝ) < _)).mp (le_sqrt_of_sqr_le _), rwa [le_sub, show (0:ℝ) = (0:β„•)/(1:β„•), by rw [nat.cast_zero, zero_div]], end lemma sqrt_two_add_series_step_up (c d : β„•) {a b n : β„•} {z : ℝ} (hz : sqrt_two_add_series (c/d) n ≀ z) (hb : 0 < b) (hd : 0 < d) (h : (2 * b + a) * d ^ 2 ≀ c ^ 2 * b) : sqrt_two_add_series (a/b) (n+1) ≀ z := begin refine le_trans _ hz, rw sqrt_two_add_series_succ, apply sqrt_two_add_series_monotone_left, have hb' : 0 < (b:ℝ) := nat.cast_pos.2 hb, have hd' : 0 < (d:ℝ) := nat.cast_pos.2 hd, rw [sqrt_le_left (div_nonneg c.cast_nonneg d.cast_nonneg), div_pow, add_div_eq_mul_add_div _ _ (ne_of_gt hb'), div_le_div_iff hb' (pow_pos hd' _)], exact_mod_cast h end /-- Create a proof of `a < pi` for a fixed rational number `a`, given a witness, which is a sequence of rational numbers `sqrt 2 < r 1 < r 2 < ... < r n < 2` satisfying the property that `sqrt (2 + r i) ≀ r(i+1)`, where `r 0 = 0` and `sqrt (2 - r n) β‰₯ a/2^(n+1)`. -/ meta def pi_lower_bound (l : list β„š) : tactic unit := do let n := l.length, tactic.apply `(@pi_lower_bound_start %%(reflect n)), l.mmap' (Ξ» r, do let a := r.num.to_nat, let b := r.denom, (() <$ tactic.apply `(@sqrt_two_add_series_step_up %%(reflect a) %%(reflect b))); [tactic.skip, `[norm_num1], `[norm_num1], `[norm_num1]]), `[simp only [sqrt_two_add_series, nat.cast_bit0, nat.cast_bit1, nat.cast_one, nat.cast_zero]], `[norm_num1] /-- From a lower bound on `sqrt_two_add_series 0 n = 2 cos (pi / 2 ^ (n+1))` of the form `2 - ((a - 1 / 4 ^ n) / 2 ^ (n + 1)) ^ 2 ≀ sqrt_two_add_series 0 n`, one can deduce the upper bound `pi < a` thanks to basic trigonometric formulas as expressed in `pi_lt_sqrt_two_add_series`. -/ theorem pi_upper_bound_start (n : β„•) {a} (h : 2 - ((a - 1 / 4 ^ n) / 2 ^ (n + 1)) ^ 2 ≀ sqrt_two_add_series ((0:β„•) / (1:β„•)) n) (hβ‚‚ : 1 / 4 ^ n ≀ a) : pi < a := begin refine lt_of_lt_of_le (pi_lt_sqrt_two_add_series n) _, rw [← le_sub_iff_add_le, ← le_div_iff', sqrt_le_left, sub_le], { rwa [nat.cast_zero, zero_div] at h }, { exact div_nonneg (sub_nonneg.2 hβ‚‚) (pow_nonneg (le_of_lt two_pos) _) }, { exact pow_pos two_pos _ } end lemma sqrt_two_add_series_step_down (a b : β„•) {c d n : β„•} {z : ℝ} (hz : z ≀ sqrt_two_add_series (a/b) n) (hb : 0 < b) (hd : 0 < d) (h : a ^ 2 * d ≀ (2 * d + c) * b ^ 2) : z ≀ sqrt_two_add_series (c/d) (n+1) := begin apply le_trans hz, rw sqrt_two_add_series_succ, apply sqrt_two_add_series_monotone_left, apply le_sqrt_of_sqr_le, have hb' : 0 < (b:ℝ) := nat.cast_pos.2 hb, have hd' : 0 < (d:ℝ) := nat.cast_pos.2 hd, rw [div_pow, add_div_eq_mul_add_div _ _ (ne_of_gt hd'), div_le_div_iff (pow_pos hb' _) hd'], exact_mod_cast h end /-- Create a proof of `pi < a` for a fixed rational number `a`, given a witness, which is a sequence of rational numbers `sqrt 2 < r 1 < r 2 < ... < r n < 2` satisfying the property that `sqrt (2 + r i) β‰₯ r(i+1)`, where `r 0 = 0` and `sqrt (2 - r n) β‰₯ (a - 1/4^n) / 2^(n+1)`. -/ meta def pi_upper_bound (l : list β„š) : tactic unit := do let n := l.length, (() <$ tactic.apply `(@pi_upper_bound_start %%(reflect n))); [pure (), `[norm_num1]], l.mmap' (Ξ» r, do let a := r.num.to_nat, let b := r.denom, (() <$ tactic.apply `(@sqrt_two_add_series_step_down %%(reflect a) %%(reflect b))); [pure (), `[norm_num1], `[norm_num1], `[norm_num1]]), `[simp only [sqrt_two_add_series, nat.cast_bit0, nat.cast_bit1, nat.cast_one, nat.cast_zero]], `[norm_num] lemma pi_gt_three : 3 < pi := by pi_lower_bound [23/16] lemma pi_gt_314 : 3.14 < pi := by pi_lower_bound [99/70, 874/473, 1940/989, 1447/727] lemma pi_lt_315 : pi < 3.15 := by pi_upper_bound [140/99, 279/151, 51/26, 412/207] lemma pi_gt_31415 : 3.1415 < pi := by pi_lower_bound [ 11482/8119, 5401/2923, 2348/1197, 11367/5711, 25705/12868, 23235/11621] lemma pi_lt_31416 : pi < 3.1416 := by pi_upper_bound [ 4756/3363, 101211/54775, 505534/257719, 83289/41846, 411278/205887, 438142/219137, 451504/225769, 265603/132804, 849938/424971] lemma pi_gt_3141592 : 3.141592 < pi := by pi_lower_bound [ 11482/8119, 7792/4217, 54055/27557, 949247/476920, 3310126/1657059, 2635492/1318143, 1580265/790192, 1221775/610899, 3612247/1806132, 849943/424972] lemma pi_lt_3141593 : pi < 3.141593 := by pi_upper_bound [ 27720/19601, 56935/30813, 49359/25163, 258754/130003, 113599/56868, 1101994/551163, 8671537/4336095, 3877807/1938940, 52483813/26242030, 56946167/28473117, 23798415/11899211] end real
4a67d0df43ef7861e62bda1704d3d8f436bae728
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/lean/position.lean
847a9e1a839e6a6d42b35a1255cc83aa8cd292e6
[ "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
2,787
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ prelude import init.data.nat init.data.rbmap init.lean.format namespace Lean structure Position := (line : Nat) (column : Nat) namespace Position instance : DecidableEq Position := {decEq := fun ⟨l₁, cβ‚βŸ© ⟨lβ‚‚, cβ‚‚βŸ© => if h₁ : l₁ = lβ‚‚ then if hβ‚‚ : c₁ = cβ‚‚ then isTrue (Eq.recOn h₁ (Eq.recOn hβ‚‚ rfl)) else isFalse (fun contra => Position.noConfusion contra (fun e₁ eβ‚‚ => absurd eβ‚‚ hβ‚‚)) else isFalse (fun contra => Position.noConfusion contra (fun e₁ eβ‚‚ => absurd e₁ h₁))} protected def lt : Position β†’ Position β†’ Bool | ⟨l₁, cβ‚βŸ© ⟨lβ‚‚, cβ‚‚βŸ© := (l₁, c₁) < (lβ‚‚, cβ‚‚) instance : HasFormat Position := ⟨fun ⟨l, c⟩ => "⟨" ++ fmt l ++ ", " ++ fmt c ++ "⟩"⟩ instance : HasToString Position := ⟨fun ⟨l, c⟩ => "⟨" ++ toString l ++ ", " ++ toString c ++ "⟩"⟩ instance : Inhabited Position := ⟨⟨1, 0⟩⟩ end Position structure FileMap := (source : String) (positions : Array String.Pos) (lines : Array Nat) namespace FileMap instance : Inhabited FileMap := ⟨{ source := "", positions := Array.empty, lines := Array.empty }⟩ private partial def ofStringAux (s : String) : String.Pos β†’ Nat β†’ Array String.Pos β†’ Array Nat β†’ FileMap | i line ps lines := if s.atEnd i then { source := s, positions := ps.push i, lines := lines.push line } else let c := s.get i; let i := s.next i; if c == '\n' then ofStringAux i (line+1) (ps.push i) (lines.push (line+1)) else ofStringAux i line ps lines def ofString (s : String) : FileMap := ofStringAux s 0 1 (Array.empty.push 0) (Array.empty.push 1) private partial def toColumnAux (str : String) (lineBeginPos : String.Pos) (pos : String.Pos) : String.Pos β†’ Nat β†’ Nat | i c := if i == pos || str.atEnd i then c else toColumnAux (str.next i) (c+1) /- Remark: `pos` is in `[ps.get b, ps.get e]` and `b < e` -/ private partial def toPositionAux (str : String) (ps : Array Nat) (lines : Array Nat) (pos : String.Pos) : Nat β†’ Nat β†’ Position | b e := let posB := ps.get b; if e == b + 1 then { line := lines.get b, column := toColumnAux str posB pos posB 0 } else let m := (b + e) / 2; let posM := ps.get m; if pos == posM then { line := lines.get m, column := 0 } else if pos > posM then toPositionAux m e else toPositionAux b m def toPosition : FileMap β†’ String.Pos β†’ Position | { source := str, positions := ps, lines := lines } pos := toPositionAux str ps lines pos 0 (ps.size-1) end FileMap end Lean def String.toFileMap (s : String) : Lean.FileMap := Lean.FileMap.ofString s
c3c008284196888b4a248f22e3c629b1713006e0
968e2f50b755d3048175f176376eff7139e9df70
/examples/prop_logic_theory/unnamed_1520.lean
95790f8c7df4a5dca44941b57356e3ac2b681bae
[]
no_license
gihanmarasingha/mth1001_sphinx
190a003269ba5e54717b448302a27ca26e31d491
05126586cbf5786e521be1ea2ef5b4ba3c44e74a
refs/heads/master
1,672,913,933,677
1,604,516,583,000
1,604,516,583,000
309,245,750
1
0
null
null
null
null
UTF-8
Lean
false
false
158
lean
import data.nat.basic variables x y z : β„• -- BEGIN example : x * y + x * z = (z + y) * x := begin rw ←mul_add, rw add_comm, rw mul_comm, end -- END
f856213f6c33567cef02489edbd5b2da4529c8b1
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/sheaves/sheaf_condition/equalizer_products_auto.lean
e1e7baa0269f66ac12335d005ff5cf33f98b89ce
[]
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
12,684
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.sheaves.presheaf import Mathlib.category_theory.limits.punit import Mathlib.category_theory.limits.shapes.products import Mathlib.category_theory.limits.shapes.equalizers import Mathlib.category_theory.full_subcategory import Mathlib.PostPort universes u v namespace Mathlib /-! # The sheaf condition in terms of an equalizer of products Here we set up the machinery for the "usual" definition of the sheaf condition, e.g. as in https://stacks.math.columbia.edu/tag/0072 in terms of an equalizer diagram where the two objects are `∏ F.obj (U i)` and `∏ F.obj (U i) βŠ“ (U j)`. -/ namespace Top namespace presheaf namespace sheaf_condition_equalizer_products /-- The product of the sections of a presheaf over a family of open sets. -/ /-- def pi_opens {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : C := ∏ fun (i : ΞΉ) => category_theory.functor.obj F (opposite.op (U i)) The product of the sections of a presheaf over the pairwise intersections of a family of open sets. -/ def pi_inters {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : C := ∏ fun (p : ΞΉ Γ— ΞΉ) => category_theory.functor.obj F (opposite.op (U (prod.fst p) βŠ“ U (prod.snd p))) /-- The morphism `Ξ  F.obj (U i) ⟢ Ξ  F.obj (U i) βŠ“ (U j)` whose components are given by the restriction maps from `U i` to `U i βŠ“ U j`. -/ def left_res {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : pi_opens F U ⟢ pi_inters F U := category_theory.limits.pi.lift fun (p : ΞΉ Γ— ΞΉ) => category_theory.limits.pi.Ο€ (fun (i : ΞΉ) => category_theory.functor.obj F (opposite.op (U i))) (prod.fst p) ≫ category_theory.functor.map F (category_theory.has_hom.hom.op (topological_space.opens.inf_le_left (U (prod.fst p)) (U (prod.snd p)))) /-- The morphism `Ξ  F.obj (U i) ⟢ Ξ  F.obj (U i) βŠ“ (U j)` whose components are given by the restriction maps from `U j` to `U i βŠ“ U j`. -/ def right_res {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : pi_opens F U ⟢ pi_inters F U := category_theory.limits.pi.lift fun (p : ΞΉ Γ— ΞΉ) => category_theory.limits.pi.Ο€ (fun (i : ΞΉ) => category_theory.functor.obj F (opposite.op (U i))) (prod.snd p) ≫ category_theory.functor.map F (category_theory.has_hom.hom.op (topological_space.opens.inf_le_right (U (prod.fst p)) (U (prod.snd p)))) /-- The morphism `F.obj U ⟢ Ξ  F.obj (U i)` whose components are given by the restriction maps from `U j` to `U i βŠ“ U j`. -/ def res {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : category_theory.functor.obj F (opposite.op (supr U)) ⟢ pi_opens F U := category_theory.limits.pi.lift fun (i : ΞΉ) => category_theory.functor.map F (category_theory.has_hom.hom.op (topological_space.opens.le_supr U i)) theorem w {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : res F U ≫ left_res F U = res F U ≫ right_res F U := sorry /-- The equalizer diagram for the sheaf condition. -/ def diagram {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : category_theory.limits.walking_parallel_pair β₯€ C := category_theory.limits.parallel_pair (left_res F U) (right_res F U) /-- The restriction map `F.obj U ⟢ Ξ  F.obj (U i)` gives a cone over the equalizer diagram for the sheaf condition. The sheaf condition asserts this cone is a limit cone. -/ def fork {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : category_theory.limits.fork (left_res F U) (right_res F U) := category_theory.limits.fork.of_ΞΉ (res F U) sorry @[simp] theorem fork_X {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : category_theory.limits.cone.X (fork F U) = category_theory.functor.obj F (opposite.op (supr U)) := rfl @[simp] theorem fork_ΞΉ {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : category_theory.limits.fork.ΞΉ (fork F U) = res F U := rfl @[simp] theorem fork_Ο€_app_walking_parallel_pair_zero {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : category_theory.nat_trans.app (category_theory.limits.cone.Ο€ (fork F U)) category_theory.limits.walking_parallel_pair.zero = res F U := rfl @[simp] theorem fork_Ο€_app_walking_parallel_pair_one {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} (F : presheaf C X) {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) : category_theory.nat_trans.app (category_theory.limits.cone.Ο€ (fork F U)) category_theory.limits.walking_parallel_pair.one = res F U ≫ left_res F U := rfl /-- Isomorphic presheaves have isomorphic `pi_opens` for any cover `U`. -/ @[simp] def pi_opens.iso_of_iso {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} {F : presheaf C X} {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) {G : presheaf C X} (Ξ± : F β‰… G) : pi_opens F U β‰… pi_opens G U := category_theory.limits.pi.map_iso fun (X_1 : ΞΉ) => category_theory.iso.app Ξ± (opposite.op (U X_1)) /-- Isomorphic presheaves have isomorphic `pi_inters` for any cover `U`. -/ @[simp] def pi_inters.iso_of_iso {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} {F : presheaf C X} {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) {G : presheaf C X} (Ξ± : F β‰… G) : pi_inters F U β‰… pi_inters G U := category_theory.limits.pi.map_iso fun (X_1 : ΞΉ Γ— ΞΉ) => category_theory.iso.app Ξ± (opposite.op (U (prod.fst X_1) βŠ“ U (prod.snd X_1))) /-- Isomorphic presheaves have isomorphic sheaf condition diagrams. -/ def diagram.iso_of_iso {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} {F : presheaf C X} {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) {G : presheaf C X} (Ξ± : F β‰… G) : diagram F U β‰… diagram G U := category_theory.nat_iso.of_components (fun (X_1 : category_theory.limits.walking_parallel_pair) => category_theory.limits.walking_parallel_pair.cases_on X_1 (pi_opens.iso_of_iso U Ξ±) (pi_inters.iso_of_iso U Ξ±)) sorry /-- If `F G : presheaf C X` are isomorphic presheaves, then the `fork F U`, the canonical cone of the sheaf condition diagram for `F`, is isomorphic to `fork F G` postcomposed with the corresponding isomorphism between sheaf condition diagrams. -/ def fork.iso_of_iso {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} {F : presheaf C X} {ΞΉ : Type v} (U : ΞΉ β†’ topological_space.opens β†₯X) {G : presheaf C X} (Ξ± : F β‰… G) : fork F U β‰… category_theory.functor.obj (category_theory.limits.cones.postcompose (category_theory.iso.inv (diagram.iso_of_iso U Ξ±))) (fork G U) := category_theory.limits.fork.ext (category_theory.iso.app Ξ± (opposite.op (supr U))) sorry /-- Push forward a cover along an open embedding. -/ @[simp] def cover.of_open_embedding {X : Top} {ΞΉ : Type v} {V : Top} {j : V ⟢ X} (oe : open_embedding ⇑j) (𝒰 : ΞΉ β†’ topological_space.opens β†₯V) : ΞΉ β†’ topological_space.opens β†₯X := fun (i : ΞΉ) => category_theory.functor.obj (is_open_map.functor sorry) (𝒰 i) /-- The isomorphism between `pi_opens` corresponding to an open embedding. -/ @[simp] def pi_opens.iso_of_open_embedding {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} {F : presheaf C X} {ΞΉ : Type v} {V : Top} {j : V ⟢ X} (oe : open_embedding ⇑j) (𝒰 : ΞΉ β†’ topological_space.opens β†₯V) : pi_opens (category_theory.functor.op (is_open_map.functor (pi_opens.iso_of_open_embedding._proof_2 oe)) β‹™ F) 𝒰 β‰… pi_opens F (cover.of_open_embedding oe 𝒰) := category_theory.limits.pi.map_iso fun (X_1 : ΞΉ) => category_theory.functor.map_iso F (category_theory.iso.refl (category_theory.functor.obj (category_theory.functor.op (is_open_map.functor sorry)) (opposite.op (𝒰 X_1)))) /-- The isomorphism between `pi_inters` corresponding to an open embedding. -/ @[simp] def pi_inters.iso_of_open_embedding {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} {F : presheaf C X} {ΞΉ : Type v} {V : Top} {j : V ⟢ X} (oe : open_embedding ⇑j) (𝒰 : ΞΉ β†’ topological_space.opens β†₯V) : pi_inters (category_theory.functor.op (is_open_map.functor (pi_inters.iso_of_open_embedding._proof_2 oe)) β‹™ F) 𝒰 β‰… pi_inters F (cover.of_open_embedding oe 𝒰) := category_theory.limits.pi.map_iso fun (X_1 : ΞΉ Γ— ΞΉ) => category_theory.functor.map_iso F (id (category_theory.iso.op (category_theory.iso.mk (category_theory.hom_of_le sorry) (category_theory.hom_of_le sorry)))) /-- The isomorphism of sheaf condition diagrams corresponding to an open embedding. -/ def diagram.iso_of_open_embedding {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} {F : presheaf C X} {ΞΉ : Type v} {V : Top} {j : V ⟢ X} (oe : open_embedding ⇑j) (𝒰 : ΞΉ β†’ topological_space.opens β†₯V) : diagram (category_theory.functor.op (is_open_map.functor (diagram.iso_of_open_embedding._proof_2 oe)) β‹™ F) 𝒰 β‰… diagram F (cover.of_open_embedding oe 𝒰) := category_theory.nat_iso.of_components (fun (X_1 : category_theory.limits.walking_parallel_pair) => category_theory.limits.walking_parallel_pair.cases_on X_1 (pi_opens.iso_of_open_embedding oe 𝒰) (pi_inters.iso_of_open_embedding oe 𝒰)) sorry /-- If `F : presheaf C X` is a presheaf, and `oe : U ⟢ X` is an open embedding, then the sheaf condition fork for a cover `𝒰` in `U` for the composition of `oe` and `F` is isomorphic to sheaf condition fork for `oe '' 𝒰`, precomposed with the isomorphism of indexing diagrams `diagram.iso_of_open_embedding`. We use this to show that the restriction of sheaf along an open embedding is still a sheaf. -/ def fork.iso_of_open_embedding {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : Top} {F : presheaf C X} {ΞΉ : Type v} {V : Top} {j : V ⟢ X} (oe : open_embedding ⇑j) (𝒰 : ΞΉ β†’ topological_space.opens β†₯V) : fork (category_theory.functor.op (is_open_map.functor (fork.iso_of_open_embedding._proof_2 oe)) β‹™ F) 𝒰 β‰… category_theory.functor.obj (category_theory.limits.cones.postcompose (category_theory.iso.inv (diagram.iso_of_open_embedding oe 𝒰))) (fork F (cover.of_open_embedding oe 𝒰)) := category_theory.limits.fork.ext (id (category_theory.functor.map_iso F (category_theory.iso.op (category_theory.iso.mk (category_theory.hom_of_le sorry) (category_theory.hom_of_le sorry))))) sorry end Mathlib
d0106ff4a806be742294e20864c39c5db7e46b45
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/special_functions/trigonometric/complex_deriv.lean
2ab19256c1269f15cb38c234799cf9898e1e83f5
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
2,652
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle SΓΆnne, Benjamin Davidson -/ import analysis.special_functions.trigonometric.complex /-! # Complex trigonometric functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Basic facts and derivatives for the complex trigonometric functions. -/ noncomputable theory namespace complex open set filter open_locale real lemma has_strict_deriv_at_tan {x : β„‚} (h : cos x β‰  0) : has_strict_deriv_at tan (1 / (cos x)^2) x := begin convert (has_strict_deriv_at_sin x).div (has_strict_deriv_at_cos x) h, rw ← sin_sq_add_cos_sq x, ring, end lemma has_deriv_at_tan {x : β„‚} (h : cos x β‰  0) : has_deriv_at tan (1 / (cos x)^2) x := (has_strict_deriv_at_tan h).has_deriv_at open_locale topology lemma tendsto_abs_tan_of_cos_eq_zero {x : β„‚} (hx : cos x = 0) : tendsto (Ξ» x, abs (tan x)) (𝓝[β‰ ] x) at_top := begin simp only [tan_eq_sin_div_cos, ← norm_eq_abs, norm_div], have A : sin x β‰  0 := Ξ» h, by simpa [*, sq] using sin_sq_add_cos_sq x, have B : tendsto cos (𝓝[β‰ ] (x)) (𝓝[β‰ ] 0), from hx β–Έ (has_deriv_at_cos x).tendsto_punctured_nhds (neg_ne_zero.2 A), exact continuous_sin.continuous_within_at.norm.mul_at_top (norm_pos_iff.2 A) (tendsto_norm_nhds_within_zero.comp B).inv_tendsto_zero, end lemma tendsto_abs_tan_at_top (k : β„€) : tendsto (Ξ» x, abs (tan x)) (𝓝[β‰ ] ((2 * k + 1) * Ο€ / 2)) at_top := tendsto_abs_tan_of_cos_eq_zero $ cos_eq_zero_iff.2 ⟨k, rfl⟩ @[simp] lemma continuous_at_tan {x : β„‚} : continuous_at tan x ↔ cos x β‰  0 := begin refine ⟨λ hc hβ‚€, _, Ξ» h, (has_deriv_at_tan h).continuous_at⟩, exact not_tendsto_nhds_of_tendsto_at_top (tendsto_abs_tan_of_cos_eq_zero hβ‚€) _ (hc.norm.tendsto.mono_left inf_le_left) end @[simp] lemma differentiable_at_tan {x : β„‚} : differentiable_at β„‚ tan x ↔ cos x β‰  0 := ⟨λ h, continuous_at_tan.1 h.continuous_at, Ξ» h, (has_deriv_at_tan h).differentiable_at⟩ @[simp] lemma deriv_tan (x : β„‚) : deriv tan x = 1 / (cos x)^2 := if h : cos x = 0 then have Β¬differentiable_at β„‚ tan x := mt differentiable_at_tan.1 (not_not.2 h), by simp [deriv_zero_of_not_differentiable_at this, h, sq] else (has_deriv_at_tan h).deriv @[simp] lemma cont_diff_at_tan {x : β„‚} {n : β„•βˆž} : cont_diff_at β„‚ n tan x ↔ cos x β‰  0 := ⟨λ h, continuous_at_tan.1 h.continuous_at, cont_diff_sin.cont_diff_at.div cont_diff_cos.cont_diff_at⟩ end complex
9412649adafbaf84030ea5d38b51cd14608b752c
c8d830ce6c7de4840cf0c892d8b58e7e8df97e37
/src/property_catalogue/LTL/pattern_meta.lean
fcda87a7f588b00fe5247b32a8f9105a470c644e
[]
no_license
loganrjmurphy/lean-strategies
4b8dd54771bb421c929a8bcb93a528ce6c1a70f1
020e2a65dc2ab475696dfea5ad8935a0a4085918
refs/heads/main
1,682,732,168,860
1,614,820,630,000
1,614,820,630,000
278,458,841
3
0
null
1,613,755,728,000
1,594,324,763,000
Lean
UTF-8
Lean
false
false
1,141
lean
import property_catalogue.LTL.sat.absent import property_catalogue.LTL.sat.precedes import common_meta variable {Ξ± : Type} meta def switch (s : string) : tactic unit := do tgt ← tactic.target, ctx ← tactic.local_context, match tgt with | `(sat (precedes.globally %%e₁ %%eβ‚‚) _) := precedes.globally.solve e₁ eβ‚‚ s ctx | `(sat (absent.globally %%e₁) _) := absent.globally.solve e₁ s ctx | _ := return () end meta def debug_inductive : tactic (string) := do tgt ← tactic.target, match tgt with | `(Β¬(%%path ⊨ %%e1)) := do let e3 : expr := expr.const `transitions_safe [], let e4 : expr := expr.mk_app e3 [e1], e2 ← tactic_format_expr e1, e4 ← tactic_format_expr e4, return $ to_string e4 ++ " " ++ to_string e2 | _ := return string.empty end meta def solve_inductive (ps : PROOF_STATE Ξ±) : tactic (PROOF_STATE Ξ±) := do let s₁ := string.empty, p ← by_induction ps, b ← is_solved, if !b then do h ← debug_inductive, return {hints := p.hints ++ [h] ..p} else return {solved := b, ..p}
7cf216f0600177d4f2d5af8a8cbc6a4c1cc4d272
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/order/level09.lean
848d9ccd12db21aebb766d5f4864f1bf06f72b37
[ "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,806
lean
import game.order.level08 open real namespace xena -- hide /- # Chapter 2 : Order ## Level 9 This level invites you to work out a property of the absolute value. In Lean the absolute value of $x$ is denoted by `abs x`. For ease of use, a notation can be used around that definition as below. Feel free to use the triangle inequality on the real numbers, `abs_add : βˆ€ (a b : ?M_1), |a + b| ≀ |a| + |b|` together with the `linarith` and `norm_num` tactics. -/ notation `|` x `|` := abs x -- begin hide -- this to go in the side bar lemma eq_sqr_to_eq (a b : ℝ) (ha : 0 ≀ a) (hb : 0 ≀ b) : a^2 = b^2 β†’ a = b := begin intro H, have A : sqrt (a ^ 2) = sqrt (a ^ 2), refl, rw H at A {occs := occurrences.pos [2]}, have G := sqrt_sqr ha, rw G at A, have F := sqrt_sqr hb, rw F at A, exact A, done end -- end hide /- Lemma For any two real numbers $a$ and $b$, we have that $$|a + b| = |a| + |b|$$ if and only if $ab \ge 0$ . -/ theorem abs_sub_eq_sum_abs (a b : ℝ) : |a + b| = |a| + |b| ↔ a * b β‰₯ 0 := begin have H0 : (a+b)^2 = |a+b|^2, have h01 := abs_mul_abs_self (a+b), rw pow_two _, rw pow_two _, symmetry, exact h01, have H1 : 0 ≀ (a + b) ^ 2, exact pow_two_nonneg (a+b), have H2 : (a+b) ^ 2 = a ^2 + 2 * a * b + b^2, ring, have H3 : ( |a| + |b| )^2 = |a|^2 + 2*|a|*|b| + |b|^2, ring, rw H0 at H2, have Ha : a^2 = |a|^2, have h01 := abs_mul_abs_self a, rw pow_two _, rw pow_two _, symmetry, exact h01, have Hb : b^2 = |b|^2, have h01 := abs_mul_abs_self b, rw pow_two _, rw pow_two _, symmetry, exact h01, rw [Ha, Hb] at H2, split, intro h, rw h at H2, rw H3 at H2, simp at H2, rw mul_assoc at H2, rw mul_assoc at H2, have g1 : ( |a| * |b| ) = (a * b), linarith, have g2 : |a * b| = ( |a| * |b| ), exact abs_mul _ _, rw ← g2 at g1, by_contradiction hn, push_neg at hn, have g3 : | a * b | = - (a *b), exact abs_of_neg hn, rw g1 at g3, linarith, -- the right-left direction intro h, have g1 : |a * b| = a * b, exact abs_of_nonneg h, have g2 : |a * b| = ( |a| * |b| ), exact abs_mul _ _, rw g2 at g1, rw mul_assoc 2 a b at H2, rw ← g1 at H2, have g3 : |a| ^ 2 + 2 * ( |a| * |b| ) + |b| ^ 2 = ( |a| + |b| )^2, ring, rw g3 at H2, have g4 : sqrt ( |a + b| ^ 2 ) = sqrt ( |a + b| ^ 2), refl, rw H2 at g4 {occs := occurrences.pos [2]}, have hab : 0 ≀ |a + b|, exact is_absolute_value.abv_nonneg abs (a+b), have ha : 0 ≀ |a|, exact is_absolute_value.abv_nonneg abs a, have hb : 0 ≀ |b|, exact is_absolute_value.abv_nonneg abs b, have hc : 0 ≀ |a| + |b|, linarith, have G := eq_sqr_to_eq ( |a + b| ) ( |a| + |b| ) hab hc H2, exact G, done end end xena -- hide
b0c651da3df85f530bc72adb34a29a112473cb78
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/zmod/basic.lean
a475e39eee6bc4d1d07c3bfef1162d629ca75286
[ "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
33,981
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 algebra.char_p.basic import algebra.group.conj_finite import tactic.fin_cases /-! # Integers mod `n` Definition of the integers mod n, and the field structure on the integers mod p. ## Definitions * `zmod n`, which is for integers modulo a nat `n : β„•` * `val a` is defined as a natural number: - for `a : zmod 0` it is the absolute value of `a` - for `a : zmod n` with `0 < n` it is the least natural number in the equivalence class * `val_min_abs` returns the integer closest to zero in the equivalence class. * A coercion `cast` is defined from `zmod n` into any ring. This is a ring hom if the ring has characteristic dividing `n` -/ namespace zmod instance : char_zero (zmod 0) := (by apply_instance : char_zero β„€) /-- `val a` is a natural number defined as: - for `a : zmod 0` it is the absolute value of `a` - for `a : zmod n` with `0 < n` it is the least natural number in the equivalence class See `zmod.val_min_abs` for a variant that takes values in the integers. -/ def val : Ξ  {n : β„•}, zmod n β†’ β„• | 0 := int.nat_abs | (n+1) := (coe : fin (n + 1) β†’ β„•) lemma val_lt {n : β„•} [ne_zero n] (a : zmod n) : a.val < n := begin casesI n, { cases ne_zero.ne 0 rfl }, exact fin.is_lt a end lemma val_le {n : β„•} [ne_zero n] (a : zmod n) : a.val ≀ n := a.val_lt.le @[simp] lemma val_zero : βˆ€ {n}, (0 : zmod n).val = 0 | 0 := rfl | (n+1) := rfl @[simp] lemma val_one' : (1 : zmod 0).val = 1 := rfl @[simp] lemma val_neg' {n : zmod 0} : (-n).val = n.val := by simp [val] @[simp] lemma val_mul' {m n : zmod 0} : (m * n).val = m.val * n.val := by simp [val, int.nat_abs_mul] lemma val_nat_cast {n : β„•} (a : β„•) : (a : zmod n).val = a % n := begin casesI n, { rw [nat.mod_zero], exact int.nat_abs_of_nat a, }, rw ← fin.of_nat_eq_coe, refl end instance (n : β„•) : char_p (zmod n) n := { cast_eq_zero_iff := begin intro k, cases n, { simp only [zero_dvd_iff, int.coe_nat_eq_zero], }, rw [fin.eq_iff_veq], show (k : zmod (n+1)).val = (0 : zmod (n+1)).val ↔ _, rw [val_nat_cast, val_zero, nat.dvd_iff_mod_eq_zero], end } @[simp] lemma add_order_of_one (n : β„•) : add_order_of (1 : zmod n) = n := char_p.eq _ (char_p.add_order_of_one _) (zmod.char_p n) /-- This lemma works in the case in which `zmod n` is not infinite, i.e. `n β‰  0`. The version where `a β‰  0` is `add_order_of_coe'`. -/ @[simp] lemma add_order_of_coe (a : β„•) {n : β„•} (n0 : n β‰  0) : add_order_of (a : zmod n) = n / n.gcd a := begin cases a, simp [nat.pos_of_ne_zero n0], rw [← nat.smul_one_eq_coe, add_order_of_nsmul' _ a.succ_ne_zero, zmod.add_order_of_one], end /-- This lemma works in the case in which `a β‰  0`. The version where `zmod n` is not infinite, i.e. `n β‰  0`, is `add_order_of_coe`. -/ @[simp] lemma add_order_of_coe' {a : β„•} (n : β„•) (a0 : a β‰  0) : add_order_of (a : zmod n) = n / n.gcd a := by rw [← nat.smul_one_eq_coe, add_order_of_nsmul' _ a0, zmod.add_order_of_one] /-- We have that `ring_char (zmod n) = n`. -/ lemma ring_char_zmod_n (n : β„•) : ring_char (zmod n) = n := by { rw ring_char.eq_iff, exact zmod.char_p n, } @[simp] lemma nat_cast_self (n : β„•) : (n : zmod n) = 0 := char_p.cast_eq_zero (zmod n) n @[simp] lemma nat_cast_self' (n : β„•) : (n + 1 : zmod (n + 1)) = 0 := by rw [← nat.cast_add_one, nat_cast_self (n + 1)] section universal_property variables {n : β„•} {R : Type*} section variables [add_group_with_one R] /-- Cast an integer modulo `n` to another semiring. This function is a morphism if the characteristic of `R` divides `n`. See `zmod.cast_hom` for a bundled version. -/ def cast : Ξ  {n : β„•}, zmod n β†’ R | 0 := int.cast | (n+1) := Ξ» i, i.val -- see Note [coercion into rings] @[priority 900] instance (n : β„•) : has_coe_t (zmod n) R := ⟨cast⟩ @[simp] lemma cast_zero : ((0 : zmod n) : R) = 0 := by cases n; simp lemma cast_eq_val [ne_zero n] (a : zmod n) : (a : R) = a.val := begin casesI n, { cases ne_zero.ne 0 rfl }, refl, end variables {S : Type*} [add_group_with_one S] @[simp] lemma _root_.prod.fst_zmod_cast (a : zmod n) : (a : R Γ— S).fst = a := by cases n; simp @[simp] lemma _root_.prod.snd_zmod_cast (a : zmod n) : (a : R Γ— S).snd = a := by cases n; simp end /-- So-named because the coercion is `nat.cast` into `zmod`. For `nat.cast` into an arbitrary ring, see `zmod.nat_cast_val`. -/ lemma nat_cast_zmod_val {n : β„•} [ne_zero n] (a : zmod n) : (a.val : zmod n) = a := begin casesI n, { cases ne_zero.ne 0 rfl }, { apply fin.coe_coe_eq_self } end lemma nat_cast_right_inverse [ne_zero n] : function.right_inverse val (coe : β„• β†’ zmod n) := nat_cast_zmod_val lemma nat_cast_zmod_surjective [ne_zero n] : function.surjective (coe : β„• β†’ zmod n) := nat_cast_right_inverse.surjective /-- So-named because the outer coercion is `int.cast` into `zmod`. For `int.cast` into an arbitrary ring, see `zmod.int_cast_cast`. -/ @[norm_cast] lemma int_cast_zmod_cast (a : zmod n) : ((a : β„€) : zmod n) = a := begin cases n, { rw [int.cast_id a, int.cast_id a], }, { rw [coe_coe, int.cast_coe_nat, fin.coe_coe_eq_self] } end lemma int_cast_right_inverse : function.right_inverse (coe : zmod n β†’ β„€) (coe : β„€ β†’ zmod n) := int_cast_zmod_cast lemma int_cast_surjective : function.surjective (coe : β„€ β†’ zmod n) := int_cast_right_inverse.surjective @[norm_cast] lemma cast_id : βˆ€ n (i : zmod n), ↑i = i | 0 i := int.cast_id i | (n+1) i := nat_cast_zmod_val i @[simp] lemma cast_id' : (coe : zmod n β†’ zmod n) = id := funext (cast_id n) variables (R) [ring R] /-- The coercions are respectively `nat.cast` and `zmod.cast`. -/ @[simp] lemma nat_cast_comp_val [ne_zero n] : (coe : β„• β†’ R) ∘ (val : zmod n β†’ β„•) = coe := begin casesI n, { cases ne_zero.ne 0 rfl }, refl end /-- The coercions are respectively `int.cast`, `zmod.cast`, and `zmod.cast`. -/ @[simp] lemma int_cast_comp_cast : (coe : β„€ β†’ R) ∘ (coe : zmod n β†’ β„€) = coe := begin cases n, { exact congr_arg ((∘) int.cast) zmod.cast_id', }, { ext, simp } end variables {R} @[simp] lemma nat_cast_val [ne_zero n] (i : zmod n) : (i.val : R) = i := congr_fun (nat_cast_comp_val R) i @[simp] lemma int_cast_cast (i : zmod n) : ((i : β„€) : R) = i := congr_fun (int_cast_comp_cast R) i lemma coe_add_eq_ite {n : β„•} (a b : zmod n) : (↑(a + b) : β„€) = if (n : β„€) ≀ a + b then a + b - n else a + b := begin cases n, { simp }, simp only [coe_coe, fin.coe_add_eq_ite, ← int.coe_nat_add, ← int.coe_nat_succ, int.coe_nat_le], split_ifs with h, { exact int.coe_nat_sub h }, { refl } end section char_dvd /-! If the characteristic of `R` divides `n`, then `cast` is a homomorphism. -/ variables {n} {m : β„•} [char_p R m] @[simp] lemma cast_one (h : m ∣ n) : ((1 : zmod n) : R) = 1 := begin casesI n, { exact int.cast_one }, show ((1 % (n+1) : β„•) : R) = 1, cases n, { rw [nat.dvd_one] at h, substI m, apply subsingleton.elim }, rw nat.mod_eq_of_lt, { exact nat.cast_one }, exact nat.lt_of_sub_eq_succ rfl end lemma cast_add (h : m ∣ n) (a b : zmod n) : ((a + b : zmod n) : R) = a + b := begin casesI n, { apply int.cast_add }, simp only [coe_coe], symmetry, erw [fin.coe_add, ← nat.cast_add, ← sub_eq_zero, ← nat.cast_sub (nat.mod_le _ _), @char_p.cast_eq_zero_iff R _ m], exact h.trans (nat.dvd_sub_mod _), end lemma cast_mul (h : m ∣ n) (a b : zmod n) : ((a * b : zmod n) : R) = a * b := begin casesI n, { apply int.cast_mul }, simp only [coe_coe], symmetry, erw [fin.coe_mul, ← nat.cast_mul, ← sub_eq_zero, ← nat.cast_sub (nat.mod_le _ _), @char_p.cast_eq_zero_iff R _ m], exact h.trans (nat.dvd_sub_mod _), end /-- The canonical ring homomorphism from `zmod n` to a ring of characteristic `n`. See also `zmod.lift` (in `data.zmod.quotient`) for a generalized version working in `add_group`s. -/ def cast_hom (h : m ∣ n) (R : Type*) [ring R] [char_p R m] : zmod n β†’+* R := { to_fun := coe, map_zero' := cast_zero, map_one' := cast_one h, map_add' := cast_add h, map_mul' := cast_mul h } @[simp] lemma cast_hom_apply {h : m ∣ n} (i : zmod n) : cast_hom h R i = i := rfl @[simp, norm_cast] lemma cast_sub (h : m ∣ n) (a b : zmod n) : ((a - b : zmod n) : R) = a - b := (cast_hom h R).map_sub a b @[simp, norm_cast] lemma cast_neg (h : m ∣ n) (a : zmod n) : ((-a : zmod n) : R) = -a := (cast_hom h R).map_neg a @[simp, norm_cast] lemma cast_pow (h : m ∣ n) (a : zmod n) (k : β„•) : ((a ^ k : zmod n) : R) = a ^ k := (cast_hom h R).map_pow a k @[simp, norm_cast] lemma cast_nat_cast (h : m ∣ n) (k : β„•) : ((k : zmod n) : R) = k := map_nat_cast (cast_hom h R) k @[simp, norm_cast] lemma cast_int_cast (h : m ∣ n) (k : β„€) : ((k : zmod n) : R) = k := map_int_cast (cast_hom h R) k end char_dvd section char_eq /-! Some specialised simp lemmas which apply when `R` has characteristic `n`. -/ variable [char_p R n] @[simp] lemma cast_one' : ((1 : zmod n) : R) = 1 := cast_one dvd_rfl @[simp] lemma cast_add' (a b : zmod n) : ((a + b : zmod n) : R) = a + b := cast_add dvd_rfl a b @[simp] lemma cast_mul' (a b : zmod n) : ((a * b : zmod n) : R) = a * b := cast_mul dvd_rfl a b @[simp] lemma cast_sub' (a b : zmod n) : ((a - b : zmod n) : R) = a - b := cast_sub dvd_rfl a b @[simp] lemma cast_pow' (a : zmod n) (k : β„•) : ((a ^ k : zmod n) : R) = a ^ k := cast_pow dvd_rfl a k @[simp, norm_cast] lemma cast_nat_cast' (k : β„•) : ((k : zmod n) : R) = k := cast_nat_cast dvd_rfl k @[simp, norm_cast] lemma cast_int_cast' (k : β„€) : ((k : zmod n) : R) = k := cast_int_cast dvd_rfl k variables (R) lemma cast_hom_injective : function.injective (zmod.cast_hom (dvd_refl n) R) := begin rw injective_iff_map_eq_zero, intro x, obtain ⟨k, rfl⟩ := zmod.int_cast_surjective x, rw [map_int_cast, char_p.int_cast_eq_zero_iff R n, char_p.int_cast_eq_zero_iff (zmod n) n], exact id end lemma cast_hom_bijective [fintype R] (h : fintype.card R = n) : function.bijective (zmod.cast_hom (dvd_refl n) R) := begin haveI : ne_zero n := ⟨begin intro hn, rw hn at h, exact (fintype.card_eq_zero_iff.mp h).elim' 0 end⟩, rw [fintype.bijective_iff_injective_and_card, zmod.card, h, eq_self_iff_true, and_true], apply zmod.cast_hom_injective end /-- The unique ring isomorphism between `zmod n` and a ring `R` of characteristic `n` and cardinality `n`. -/ noncomputable def ring_equiv [fintype R] (h : fintype.card R = n) : zmod n ≃+* R := ring_equiv.of_bijective _ (zmod.cast_hom_bijective R h) /-- The identity between `zmod m` and `zmod n` when `m = n`, as a ring isomorphism. -/ def ring_equiv_congr {m n : β„•} (h : m = n) : zmod m ≃+* zmod n := begin cases m; cases n, { exact ring_equiv.refl _ }, { exfalso, exact n.succ_ne_zero h.symm }, { exfalso, exact m.succ_ne_zero h }, { exact { map_mul' := Ξ» a b, begin rw [order_iso.to_fun_eq_coe], ext, rw [fin.coe_cast, fin.coe_mul, fin.coe_mul, fin.coe_cast, fin.coe_cast, ← h] end, map_add' := Ξ» a b, begin rw [order_iso.to_fun_eq_coe], ext, rw [fin.coe_cast, fin.coe_add, fin.coe_add, fin.coe_cast, fin.coe_cast, ← h] end, ..fin.cast h } } end end char_eq end universal_property lemma int_coe_eq_int_coe_iff (a b : β„€) (c : β„•) : (a : zmod c) = (b : zmod c) ↔ a ≑ b [ZMOD c] := char_p.int_coe_eq_int_coe_iff (zmod c) c a b lemma int_coe_eq_int_coe_iff' (a b : β„€) (c : β„•) : (a : zmod c) = (b : zmod c) ↔ a % c = b % c := zmod.int_coe_eq_int_coe_iff a b c lemma nat_coe_eq_nat_coe_iff (a b c : β„•) : (a : zmod c) = (b : zmod c) ↔ a ≑ b [MOD c] := by simpa [int.coe_nat_modeq_iff] using zmod.int_coe_eq_int_coe_iff a b c lemma nat_coe_eq_nat_coe_iff' (a b c : β„•) : (a : zmod c) = (b : zmod c) ↔ a % c = b % c := zmod.nat_coe_eq_nat_coe_iff a b c lemma int_coe_zmod_eq_zero_iff_dvd (a : β„€) (b : β„•) : (a : zmod b) = 0 ↔ (b : β„€) ∣ a := by rw [← int.cast_zero, zmod.int_coe_eq_int_coe_iff, int.modeq_zero_iff_dvd] lemma int_coe_eq_int_coe_iff_dvd_sub (a b : β„€) (c : β„•) : (a : zmod c) = ↑b ↔ ↑c ∣ b-a := begin rw [zmod.int_coe_eq_int_coe_iff, int.modeq_iff_dvd], end lemma nat_coe_zmod_eq_zero_iff_dvd (a b : β„•) : (a : zmod b) = 0 ↔ b ∣ a := by rw [← nat.cast_zero, zmod.nat_coe_eq_nat_coe_iff, nat.modeq_zero_iff_dvd] lemma val_int_cast {n : β„•} (a : β„€) [ne_zero n] : ↑(a : zmod n).val = a % n := begin have hle : (0 : β„€) ≀ ↑(a : zmod n).val := int.coe_nat_nonneg _, have hlt : ↑(a : zmod n).val < (n : β„€) := int.coe_nat_lt.mpr (zmod.val_lt a), refine (int.mod_eq_of_lt hle hlt).symm.trans _, rw [←zmod.int_coe_eq_int_coe_iff', int.cast_coe_nat, zmod.nat_cast_val, zmod.cast_id], end lemma coe_int_cast {n : β„•} (a : β„€) : ↑(a : zmod n) = a % n := begin cases n, { rw [int.coe_nat_zero, int.mod_zero, int.cast_id, int.cast_id] }, { rw [←val_int_cast, val, coe_coe] }, end @[simp] lemma val_neg_one (n : β„•) : (-1 : zmod n.succ).val = n := begin rw [val, fin.coe_neg], cases n, { rw [nat.mod_one] }, { rw [fin.coe_one, nat.succ_add_sub_one, nat.mod_eq_of_lt (nat.lt.base _)] }, end /-- `-1 : zmod n` lifts to `n - 1 : R`. This avoids the characteristic assumption in `cast_neg`. -/ lemma cast_neg_one {R : Type*} [ring R] (n : β„•) : ↑(-1 : zmod n) = (n - 1 : R) := begin cases n, { rw [int.cast_neg, int.cast_one, nat.cast_zero, zero_sub] }, { rw [←nat_cast_val, val_neg_one, nat.cast_succ, add_sub_cancel] }, end lemma cast_sub_one {R : Type*} [ring R] {n : β„•} (k : zmod n) : ((k - 1 : zmod n) : R) = (if k = 0 then n else k) - 1 := begin split_ifs with hk, { rw [hk, zero_sub, zmod.cast_neg_one] }, { cases n, { rw [int.cast_sub, int.cast_one] }, { rw [←zmod.nat_cast_val, zmod.val, fin.coe_sub_one, if_neg], { rw [nat.cast_sub, nat.cast_one, coe_coe], rwa [fin.ext_iff, fin.coe_zero, ←ne, ←nat.one_le_iff_ne_zero] at hk }, { exact hk } } }, end lemma nat_coe_zmod_eq_iff (p : β„•) (n : β„•) (z : zmod p) [ne_zero p] : ↑n = z ↔ βˆƒ k, n = z.val + p * k := begin split, { rintro rfl, refine ⟨n / p, _⟩, rw [val_nat_cast, nat.mod_add_div] }, { rintro ⟨k, rfl⟩, rw [nat.cast_add, nat_cast_zmod_val, nat.cast_mul, nat_cast_self, zero_mul, add_zero] } end lemma int_coe_zmod_eq_iff (p : β„•) (n : β„€) (z : zmod p) [ne_zero p] : ↑n = z ↔ βˆƒ k, n = z.val + p * k := begin split, { rintro rfl, refine ⟨n / p, _⟩, rw [val_int_cast, int.mod_add_div] }, { rintro ⟨k, rfl⟩, rw [int.cast_add, int.cast_mul, int.cast_coe_nat, int.cast_coe_nat, nat_cast_val, zmod.nat_cast_self, zero_mul, add_zero, cast_id] } end @[push_cast, simp] lemma int_cast_mod (a : β„€) (b : β„•) : ((a % b : β„€) : zmod b) = (a : zmod b) := begin rw zmod.int_coe_eq_int_coe_iff, apply int.mod_modeq, end lemma ker_int_cast_add_hom (n : β„•) : (int.cast_add_hom (zmod n)).ker = add_subgroup.zmultiples n := by { ext, rw [int.mem_zmultiples_iff, add_monoid_hom.mem_ker, int.coe_cast_add_hom, int_coe_zmod_eq_zero_iff_dvd] } lemma ker_int_cast_ring_hom (n : β„•) : (int.cast_ring_hom (zmod n)).ker = ideal.span ({n} : set β„€) := by { ext, rw [ideal.mem_span_singleton, ring_hom.mem_ker, int.coe_cast_ring_hom, int_coe_zmod_eq_zero_iff_dvd] } local attribute [semireducible] int.nonneg @[simp] lemma nat_cast_to_nat (p : β„•) : βˆ€ {z : β„€} (h : 0 ≀ z), (z.to_nat : zmod p) = z | (n : β„•) h := by simp only [int.cast_coe_nat, int.to_nat_coe_nat] | -[1+n] h := false.elim h lemma val_injective (n : β„•) [ne_zero n] : function.injective (zmod.val : zmod n β†’ β„•) := begin casesI n, { cases ne_zero.ne 0 rfl }, assume a b h, ext, exact h end lemma val_one_eq_one_mod (n : β„•) : (1 : zmod n).val = 1 % n := by rw [← nat.cast_one, val_nat_cast] lemma val_one (n : β„•) [fact (1 < n)] : (1 : zmod n).val = 1 := by { rw val_one_eq_one_mod, exact nat.mod_eq_of_lt (fact.out _) } lemma val_add {n : β„•} [ne_zero n] (a b : zmod n) : (a + b).val = (a.val + b.val) % n := begin casesI n, { cases ne_zero.ne 0 rfl }, { apply fin.val_add } end lemma val_mul {n : β„•} (a b : zmod n) : (a * b).val = (a.val * b.val) % n := begin cases n, { rw nat.mod_zero, apply int.nat_abs_mul }, { apply fin.val_mul } end instance nontrivial (n : β„•) [fact (1 < n)] : nontrivial (zmod n) := ⟨⟨0, 1, assume h, zero_ne_one $ calc 0 = (0 : zmod n).val : by rw val_zero ... = (1 : zmod n).val : congr_arg zmod.val h ... = 1 : val_one n ⟩⟩ instance nontrivial' : nontrivial (zmod 0) := int.nontrivial /-- The inversion on `zmod n`. It is setup in such a way that `a * a⁻¹` is equal to `gcd a.val n`. In particular, if `a` is coprime to `n`, and hence a unit, `a * a⁻¹ = 1`. -/ def inv : Ξ  (n : β„•), zmod n β†’ zmod n | 0 i := int.sign i | (n+1) i := nat.gcd_a i.val (n+1) instance (n : β„•) : has_inv (zmod n) := ⟨inv n⟩ lemma inv_zero : βˆ€ (n : β„•), (0 : zmod n)⁻¹ = 0 | 0 := int.sign_zero | (n+1) := show (nat.gcd_a _ (n+1) : zmod (n+1)) = 0, by { rw val_zero, unfold nat.gcd_a nat.xgcd nat.xgcd_aux, refl } lemma mul_inv_eq_gcd {n : β„•} (a : zmod n) : a * a⁻¹ = nat.gcd a.val n := begin cases n, { calc a * a⁻¹ = a * int.sign a : rfl ... = a.nat_abs : by rw int.mul_sign ... = a.val.gcd 0 : by rw nat.gcd_zero_right; refl }, { set k := n.succ, calc a * a⁻¹ = a * a⁻¹ + k * nat.gcd_b (val a) k : by rw [nat_cast_self, zero_mul, add_zero] ... = ↑(↑a.val * nat.gcd_a (val a) k + k * nat.gcd_b (val a) k) : by { push_cast, rw nat_cast_zmod_val, refl } ... = nat.gcd a.val k : (congr_arg coe (nat.gcd_eq_gcd_ab a.val k)).symm, } end @[simp] lemma nat_cast_mod (a : β„•) (n : β„•) : ((a % n : β„•) : zmod n) = a := by conv {to_rhs, rw ← nat.mod_add_div a n}; simp lemma eq_iff_modeq_nat (n : β„•) {a b : β„•} : (a : zmod n) = b ↔ a ≑ b [MOD n] := begin cases n, { simp only [nat.modeq, int.coe_nat_inj', nat.mod_zero], }, { rw [fin.ext_iff, nat.modeq, ← val_nat_cast, ← val_nat_cast], exact iff.rfl, } end lemma coe_mul_inv_eq_one {n : β„•} (x : β„•) (h : nat.coprime x n) : (x * x⁻¹ : zmod n) = 1 := begin rw [nat.coprime, nat.gcd_comm, nat.gcd_rec] at h, rw [mul_inv_eq_gcd, val_nat_cast, h, nat.cast_one], end /-- `unit_of_coprime` makes an element of `(zmod n)Λ£` given a natural number `x` and a proof that `x` is coprime to `n` -/ def unit_of_coprime {n : β„•} (x : β„•) (h : nat.coprime x n) : (zmod n)Λ£ := ⟨x, x⁻¹, coe_mul_inv_eq_one x h, by rw [mul_comm, coe_mul_inv_eq_one x h]⟩ @[simp] lemma coe_unit_of_coprime {n : β„•} (x : β„•) (h : nat.coprime x n) : (unit_of_coprime x h : zmod n) = x := rfl lemma val_coe_unit_coprime {n : β„•} (u : (zmod n)Λ£) : nat.coprime (u : zmod n).val n := begin cases n, { rcases int.units_eq_one_or u with rfl|rfl; simp }, apply nat.coprime_of_mul_modeq_one ((u⁻¹ : units (zmod (n+1))) : zmod (n+1)).val, have := units.ext_iff.1 (mul_right_inv u), rw [units.coe_one] at this, rw [← eq_iff_modeq_nat, nat.cast_one, ← this], clear this, rw [← nat_cast_zmod_val ((u * u⁻¹ : units (zmod (n+1))) : zmod (n+1))], rw [units.coe_mul, val_mul, nat_cast_mod], end @[simp] lemma inv_coe_unit {n : β„•} (u : (zmod n)Λ£) : (u : zmod n)⁻¹ = (u⁻¹ : (zmod n)Λ£) := begin have := congr_arg (coe : β„• β†’ zmod n) (val_coe_unit_coprime u), rw [← mul_inv_eq_gcd, nat.cast_one] at this, let u' : (zmod n)Λ£ := ⟨u, (u : zmod n)⁻¹, this, by rwa mul_comm⟩, have h : u = u', { apply units.ext, refl }, rw h, refl end lemma mul_inv_of_unit {n : β„•} (a : zmod n) (h : is_unit a) : a * a⁻¹ = 1 := begin rcases h with ⟨u, rfl⟩, rw [inv_coe_unit, u.mul_inv], end lemma inv_mul_of_unit {n : β„•} (a : zmod n) (h : is_unit a) : a⁻¹ * a = 1 := by rw [mul_comm, mul_inv_of_unit a h] -- TODO: this equivalence is true for `zmod 0 = β„€`, but needs to use different functions. /-- Equivalence between the units of `zmod n` and the subtype of terms `x : zmod n` for which `x.val` is comprime to `n` -/ def units_equiv_coprime {n : β„•} [ne_zero n] : (zmod n)Λ£ ≃ {x : zmod n // nat.coprime x.val n} := { to_fun := Ξ» x, ⟨x, val_coe_unit_coprime x⟩, inv_fun := Ξ» x, unit_of_coprime x.1.val x.2, left_inv := Ξ» ⟨_, _, _, _⟩, units.ext (nat_cast_zmod_val _), right_inv := Ξ» ⟨_, _⟩, by simp } /-- The **Chinese remainder theorem**. For a pair of coprime natural numbers, `m` and `n`, the rings `zmod (m * n)` and `zmod m Γ— zmod n` are isomorphic. See `ideal.quotient_inf_ring_equiv_pi_quotient` for the Chinese remainder theorem for ideals in any ring. -/ def chinese_remainder {m n : β„•} (h : m.coprime n) : zmod (m * n) ≃+* zmod m Γ— zmod n := let to_fun : zmod (m * n) β†’ zmod m Γ— zmod n := zmod.cast_hom (show m.lcm n ∣ m * n, by simp [nat.lcm_dvd_iff]) (zmod m Γ— zmod n) in let inv_fun : zmod m Γ— zmod n β†’ zmod (m * n) := Ξ» x, if m * n = 0 then if m = 1 then ring_hom.snd _ _ x else ring_hom.fst _ _ x else nat.chinese_remainder h x.1.val x.2.val in have inv : function.left_inverse inv_fun to_fun ∧ function.right_inverse inv_fun to_fun := if hmn0 : m * n = 0 then begin rcases h.eq_of_mul_eq_zero hmn0 with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩; simp [inv_fun, to_fun, function.left_inverse, function.right_inverse, eq_int_cast, prod.ext_iff] end else begin haveI : ne_zero (m * n) := ⟨hmn0⟩, haveI : ne_zero m := ⟨left_ne_zero_of_mul hmn0⟩, haveI : ne_zero n := ⟨right_ne_zero_of_mul hmn0⟩, have left_inv : function.left_inverse inv_fun to_fun, { intro x, dsimp only [dvd_mul_left, dvd_mul_right, zmod.cast_hom_apply, coe_coe, inv_fun, to_fun], conv_rhs { rw ← zmod.nat_cast_zmod_val x }, rw [if_neg hmn0, zmod.eq_iff_modeq_nat, ← nat.modeq_and_modeq_iff_modeq_mul h, prod.fst_zmod_cast, prod.snd_zmod_cast], refine ⟨(nat.chinese_remainder h (x : zmod m).val (x : zmod n).val).2.left.trans _, (nat.chinese_remainder h (x : zmod m).val (x : zmod n).val).2.right.trans _⟩, { rw [← zmod.eq_iff_modeq_nat, zmod.nat_cast_zmod_val, zmod.nat_cast_val] }, { rw [← zmod.eq_iff_modeq_nat, zmod.nat_cast_zmod_val, zmod.nat_cast_val] } }, exact ⟨left_inv, left_inv.right_inverse_of_card_le (by simp)⟩, end, { to_fun := to_fun, inv_fun := inv_fun, map_mul' := ring_hom.map_mul _, map_add' := ring_hom.map_add _, left_inv := inv.1, right_inv := inv.2 } -- todo: this can be made a `unique` instance. instance subsingleton_units : subsingleton ((zmod 2)Λ£) := ⟨dec_trivial⟩ lemma le_div_two_iff_lt_neg (n : β„•) [hn : fact ((n : β„•) % 2 = 1)] {x : zmod n} (hx0 : x β‰  0) : x.val ≀ (n / 2 : β„•) ↔ (n / 2 : β„•) < (-x).val := begin haveI npos : ne_zero n := ⟨by { unfreezingI { rintro rfl }, simpa [fact_iff] using hn, }⟩, have hn2 : (n : β„•) / 2 < n := nat.div_lt_of_lt_mul ((lt_mul_iff_one_lt_left $ ne_zero.pos n).2 dec_trivial), have hn2' : (n : β„•) - n / 2 = n / 2 + 1, { conv {to_lhs, congr, rw [← nat.succ_sub_one n, nat.succ_sub $ ne_zero.pos n]}, rw [← nat.two_mul_odd_div_two hn.1, two_mul, ← nat.succ_add, add_tsub_cancel_right], }, have hxn : (n : β„•) - x.val < n, { rw [tsub_lt_iff_tsub_lt x.val_le le_rfl, tsub_self], rw ← zmod.nat_cast_zmod_val x at hx0, exact nat.pos_of_ne_zero (Ξ» h, by simpa [h] using hx0) }, by conv {to_rhs, rw [← nat.succ_le_iff, nat.succ_eq_add_one, ← hn2', ← zero_add (- x), ← zmod.nat_cast_self, ← sub_eq_add_neg, ← zmod.nat_cast_zmod_val x, ← nat.cast_sub x.val_le, zmod.val_nat_cast, nat.mod_eq_of_lt hxn, tsub_le_tsub_iff_left x.val_le] } end lemma ne_neg_self (n : β„•) [hn : fact ((n : β„•) % 2 = 1)] {a : zmod n} (ha : a β‰  0) : a β‰  -a := Ξ» h, have a.val ≀ n / 2 ↔ (n : β„•) / 2 < (-a).val := le_div_two_iff_lt_neg n ha, by rwa [← h, ← not_lt, not_iff_self] at this lemma neg_one_ne_one {n : β„•} [fact (2 < n)] : (-1 : zmod n) β‰  1 := char_p.neg_one_ne_one (zmod n) n lemma neg_eq_self_mod_two (a : zmod 2) : -a = a := by fin_cases a; ext; simp [fin.coe_neg, int.nat_mod]; norm_num @[simp] lemma nat_abs_mod_two (a : β„€) : (a.nat_abs : zmod 2) = a := begin cases a, { simp only [int.nat_abs_of_nat, int.cast_coe_nat, int.of_nat_eq_coe] }, { simp only [neg_eq_self_mod_two, nat.cast_succ, int.nat_abs, int.cast_neg_succ_of_nat] } end @[simp] lemma val_eq_zero : βˆ€ {n : β„•} (a : zmod n), a.val = 0 ↔ a = 0 | 0 a := int.nat_abs_eq_zero | (n+1) a := by { rw fin.ext_iff, exact iff.rfl } lemma val_cast_of_lt {n : β„•} {a : β„•} (h : a < n) : (a : zmod n).val = a := by rw [val_nat_cast, nat.mod_eq_of_lt h] lemma neg_val' {n : β„•} [ne_zero n] (a : zmod n) : (-a).val = (n - a.val) % n := calc (-a).val = val (-a) % n : by rw nat.mod_eq_of_lt ((-a).val_lt) ... = (n - val a) % n : nat.modeq.add_right_cancel' _ (by rw [nat.modeq, ←val_add, add_left_neg, tsub_add_cancel_of_le a.val_le, nat.mod_self, val_zero]) lemma neg_val {n : β„•} [ne_zero n] (a : zmod n) : (-a).val = if a = 0 then 0 else n - a.val := begin rw neg_val', by_cases h : a = 0, { rw [if_pos h, h, val_zero, tsub_zero, nat.mod_self] }, rw if_neg h, apply nat.mod_eq_of_lt, apply nat.sub_lt (ne_zero.pos n), contrapose! h, rwa [le_zero_iff, val_eq_zero] at h, end /-- `val_min_abs x` returns the integer in the same equivalence class as `x` that is closest to `0`, The result will be in the interval `(-n/2, n/2]`. -/ def val_min_abs : Ξ  {n : β„•}, zmod n β†’ β„€ | 0 x := x | n@(_+1) x := if x.val ≀ n / 2 then x.val else (x.val : β„€) - n @[simp] lemma val_min_abs_def_zero (x : zmod 0) : val_min_abs x = x := rfl lemma val_min_abs_def_pos {n : β„•} [ne_zero n] (x : zmod n) : val_min_abs x = if x.val ≀ n / 2 then x.val else x.val - n := begin casesI n, { cases ne_zero.ne 0 rfl }, { refl } end @[simp] lemma coe_val_min_abs : βˆ€ {n : β„•} (x : zmod n), (x.val_min_abs : zmod n) = x | 0 x := int.cast_id x | k@(n+1) x := begin rw val_min_abs_def_pos, split_ifs, { rw [int.cast_coe_nat, nat_cast_zmod_val] }, { rw [int.cast_sub, int.cast_coe_nat, nat_cast_zmod_val, int.cast_coe_nat, nat_cast_self, sub_zero] } end lemma nat_abs_val_min_abs_le {n : β„•} [ne_zero n] (x : zmod n) : x.val_min_abs.nat_abs ≀ n / 2 := begin rw zmod.val_min_abs_def_pos, split_ifs with h, { exact h }, have : (x.val - n : β„€) ≀ 0, { rw [sub_nonpos, int.coe_nat_le], exact x.val_le, }, rw [← int.coe_nat_le, int.of_nat_nat_abs_of_nonpos this, neg_sub], conv_lhs { congr, rw [← nat.mod_add_div n 2, int.coe_nat_add, int.coe_nat_mul, int.coe_nat_bit0, int.coe_nat_one] }, suffices : ((n % 2 : β„•) + (n / 2) : β„€) ≀ (val x), { rw ← sub_nonneg at this ⊒, apply le_trans this (le_of_eq _), ring }, norm_cast, calc (n : β„•) % 2 + n / 2 ≀ 1 + n / 2 : nat.add_le_add_right (nat.le_of_lt_succ (nat.mod_lt _ dec_trivial)) _ ... ≀ x.val : by { rw add_comm, exact nat.succ_le_of_lt (lt_of_not_ge h) } end @[simp] lemma val_min_abs_zero : βˆ€ n, (0 : zmod n).val_min_abs = 0 | 0 := by simp only [val_min_abs_def_zero] | (n+1) := by simp only [val_min_abs_def_pos, if_true, int.coe_nat_zero, zero_le, val_zero] @[simp] lemma val_min_abs_eq_zero {n : β„•} (x : zmod n) : x.val_min_abs = 0 ↔ x = 0 := begin cases n, { simp }, split, { simp only [val_min_abs_def_pos, int.coe_nat_succ], split_ifs with h h; assume h0, { apply val_injective, rwa [int.coe_nat_eq_zero] at h0, }, { apply absurd h0, rw sub_eq_zero, apply ne_of_lt, exact_mod_cast x.val_lt } }, { rintro rfl, rw val_min_abs_zero } end lemma nat_cast_nat_abs_val_min_abs {n : β„•} [ne_zero n] (a : zmod n) : (a.val_min_abs.nat_abs : zmod n) = if a.val ≀ (n : β„•) / 2 then a else -a := begin have : (a.val : β„€) - n ≀ 0, by { erw [sub_nonpos, int.coe_nat_le], exact a.val_le, }, rw [zmod.val_min_abs_def_pos], split_ifs, { rw [int.nat_abs_of_nat, nat_cast_zmod_val] }, { rw [← int.cast_coe_nat, int.of_nat_nat_abs_of_nonpos this, int.cast_neg, int.cast_sub], rw [int.cast_coe_nat, int.cast_coe_nat, nat_cast_self, sub_zero, nat_cast_zmod_val], } end @[simp] lemma nat_abs_val_min_abs_neg {n : β„•} (a : zmod n) : (-a).val_min_abs.nat_abs = a.val_min_abs.nat_abs := begin cases n, { simp only [int.nat_abs_neg, val_min_abs_def_zero], }, by_cases ha0 : a = 0, { rw [ha0, neg_zero] }, by_cases haa : -a = a, { rw [haa] }, suffices hpa : (n+1 : β„•) - a.val ≀ (n+1) / 2 ↔ (n+1 : β„•) / 2 < a.val, { rw [val_min_abs_def_pos, val_min_abs_def_pos], rw ← not_le at hpa, simp only [if_neg ha0, neg_val, hpa, int.coe_nat_sub a.val_le], split_ifs, all_goals { rw [← int.nat_abs_neg], congr' 1, ring } }, suffices : (((n+1 : β„•) % 2) + 2 * ((n + 1) / 2)) - a.val ≀ (n+1) / 2 ↔ (n+1 : β„•) / 2 < a.val, by rwa [nat.mod_add_div] at this, suffices : (n + 1) % 2 + (n + 1) / 2 ≀ val a ↔ (n + 1) / 2 < val a, by rw [tsub_le_iff_tsub_le, two_mul, ← add_assoc, add_tsub_cancel_right, this], cases (n + 1 : β„•).mod_two_eq_zero_or_one with hn0 hn1, { split, { assume h, apply lt_of_le_of_ne (le_trans (nat.le_add_left _ _) h), contrapose! haa, rw [← zmod.nat_cast_zmod_val a, ← haa, neg_eq_iff_add_eq_zero, ← nat.cast_add], rw [char_p.cast_eq_zero_iff (zmod (n+1)) (n+1)], rw [← two_mul, ← zero_add (2 * _), ← hn0, nat.mod_add_div] }, { rw [hn0, zero_add], exact le_of_lt } }, { rw [hn1, add_comm, nat.succ_le_iff] } end lemma val_eq_ite_val_min_abs {n : β„•} [ne_zero n] (a : zmod n) : (a.val : β„€) = a.val_min_abs + if a.val ≀ n / 2 then 0 else n := by { rw [zmod.val_min_abs_def_pos], split_ifs; simp only [add_zero, sub_add_cancel] } lemma prime_ne_zero (p q : β„•) [hp : fact p.prime] [hq : fact q.prime] (hpq : p β‰  q) : (q : zmod p) β‰  0 := by rwa [← nat.cast_zero, ne.def, eq_iff_modeq_nat, nat.modeq_zero_iff_dvd, ← hp.1.coprime_iff_not_dvd, nat.coprime_primes hp.1 hq.1] end zmod namespace zmod variables (p : β„•) [fact p.prime] private lemma mul_inv_cancel_aux (a : zmod p) (h : a β‰  0) : a * a⁻¹ = 1 := begin obtain ⟨k, rfl⟩ := nat_cast_zmod_surjective a, apply coe_mul_inv_eq_one, apply nat.coprime.symm, rwa [nat.prime.coprime_iff_not_dvd (fact.out p.prime), ← char_p.cast_eq_zero_iff (zmod p)] end /-- Field structure on `zmod p` if `p` is prime. -/ instance : field (zmod p) := { mul_inv_cancel := mul_inv_cancel_aux p, inv_zero := inv_zero p, .. zmod.comm_ring p, .. zmod.has_inv p, .. zmod.nontrivial p } /-- `zmod p` is an integral domain when `p` is prime. -/ instance (p : β„•) [hp : fact p.prime] : is_domain (zmod p) := begin -- We need `cases p` here in order to resolve which `comm_ring` instance is being used. unfreezingI { cases p, { exact (nat.not_prime_zero hp.out).elim }, }, exact @field.is_domain (zmod _) (zmod.field _) end end zmod lemma ring_hom.ext_zmod {n : β„•} {R : Type*} [semiring R] (f g : (zmod n) β†’+* R) : f = g := begin ext a, obtain ⟨k, rfl⟩ := zmod.int_cast_surjective a, let Ο† : β„€ β†’+* R := f.comp (int.cast_ring_hom (zmod n)), let ψ : β„€ β†’+* R := g.comp (int.cast_ring_hom (zmod n)), show Ο† k = ψ k, rw Ο†.ext_int ψ, end namespace zmod variables {n : β„•} {R : Type*} instance subsingleton_ring_hom [semiring R] : subsingleton ((zmod n) β†’+* R) := ⟨ring_hom.ext_zmod⟩ instance subsingleton_ring_equiv [semiring R] : subsingleton (zmod n ≃+* R) := ⟨λ f g, by { rw ring_equiv.coe_ring_hom_inj_iff, apply ring_hom.ext_zmod _ _ }⟩ @[simp] lemma ring_hom_map_cast [ring R] (f : R β†’+* (zmod n)) (k : zmod n) : f k = k := by { cases n; simp } lemma ring_hom_right_inverse [ring R] (f : R β†’+* (zmod n)) : function.right_inverse (coe : zmod n β†’ R) f := ring_hom_map_cast f lemma ring_hom_surjective [ring R] (f : R β†’+* (zmod n)) : function.surjective f := (ring_hom_right_inverse f).surjective lemma ring_hom_eq_of_ker_eq [comm_ring R] (f g : R β†’+* (zmod n)) (h : f.ker = g.ker) : f = g := begin have := f.lift_of_right_inverse_comp _ (zmod.ring_hom_right_inverse f) ⟨g, le_of_eq h⟩, rw subtype.coe_mk at this, rw [←this, ring_hom.ext_zmod (f.lift_of_right_inverse _ _ ⟨g, _⟩) _, ring_hom.id_comp], end section lift variables (n) {A : Type*} [add_group A] /-- The map from `zmod n` induced by `f : β„€ β†’+ A` that maps `n` to `0`. -/ @[simps] def lift : {f : β„€ β†’+ A // f n = 0} ≃ (zmod n β†’+ A) := (equiv.subtype_equiv_right $ begin intro f, rw ker_int_cast_add_hom, split, { rintro hf _ ⟨x, rfl⟩, simp only [f.map_zsmul, zsmul_zero, f.mem_ker, hf] }, { intro h, refine h (add_subgroup.mem_zmultiples _) } end).trans $ ((int.cast_add_hom (zmod n)).lift_of_right_inverse coe int_cast_zmod_cast) variables (f : {f : β„€ β†’+ A // f n = 0}) @[simp] lemma lift_coe (x : β„€) : lift n f (x : zmod n) = f x := add_monoid_hom.lift_of_right_inverse_comp_apply _ _ _ _ _ lemma lift_cast_add_hom (x : β„€) : lift n f (int.cast_add_hom (zmod n) x) = f x := add_monoid_hom.lift_of_right_inverse_comp_apply _ _ _ _ _ @[simp] lemma lift_comp_coe : zmod.lift n f ∘ coe = f := funext $ lift_coe _ _ @[simp] lemma lift_comp_cast_add_hom : (zmod.lift n f).comp (int.cast_add_hom (zmod n)) = f := add_monoid_hom.ext $ lift_cast_add_hom _ _ end lift end zmod
329ba1fb78789a08b1d5d5bc2eda9d8366601d1b
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/src/Lean/Elab/Deriving/BEq.lean
09abe7097a8b72cde9ecbd72026ad37f19011dce
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,438
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Transform import Lean.Elab.Deriving.Basic import Lean.Elab.Deriving.Util namespace Lean.Elab.Deriving.BEq open Lean.Parser.Term open Meta def mkBEqHeader (ctx : Context) (indVal : InductiveVal) : TermElabM Header := do mkHeader ctx `BEq 2 indVal def mkMatch (ctx : Context) (header : Header) (indVal : InductiveVal) (auxFunName : Name) : TermElabM Syntax := do let discrs ← mkDiscrs header indVal let alts ← mkAlts `(match $[$discrs],* with $alts:matchAlt*) where mkElseAlt : TermElabM Syntax := do let mut patterns := #[] -- add `_` pattern for indices for i in [:indVal.nindices] do patterns := patterns.push (← `(_)) patterns := patterns.push (← `(_)) patterns := patterns.push (← `(_)) let altRhs ← `(false) `(matchAltExpr| | $[$patterns:term],* => $altRhs:term) mkAlts : TermElabM (Array Syntax) := do let mut alts := #[] for ctorName in indVal.ctors do let ctorInfo ← getConstInfoCtor ctorName let alt ← forallTelescopeReducing ctorInfo.type fun xs type => do let type ← Core.betaReduce type -- we 'beta-reduce' to eliminate "artificial" dependencies let mut patterns := #[] -- add `_` pattern for indices for i in [:indVal.nindices] do patterns := patterns.push (← `(_)) let mut ctorArgs1 := #[] let mut ctorArgs2 := #[] let mut rhs ← `(true) -- add `_` for inductive parameters, they are inaccessible for i in [:indVal.nparams] do ctorArgs1 := ctorArgs1.push (← `(_)) ctorArgs2 := ctorArgs2.push (← `(_)) for i in [:ctorInfo.nfields] do let x := xs[indVal.nparams + i] if type.containsFVar x.fvarId! then -- If resulting type depends on this field, we don't need to compare ctorArgs1 := ctorArgs1.push (← `(_)) ctorArgs2 := ctorArgs2.push (← `(_)) else let a := mkIdent (← mkFreshUserName `a) let b := mkIdent (← mkFreshUserName `b) ctorArgs1 := ctorArgs1.push a ctorArgs2 := ctorArgs2.push b if (← inferType x).isAppOf indVal.name then rhs ← `($rhs && $(mkIdent auxFunName):ident $a:ident $b:ident) else rhs ← `($rhs && $a:ident == $b:ident) patterns := patterns.push (← `(@$(mkIdent ctorName):ident $ctorArgs1:term*)) patterns := patterns.push (← `(@$(mkIdent ctorName):ident $ctorArgs2:term*)) `(matchAltExpr| | $[$patterns:term],* => $rhs:term) alts := alts.push alt alts := alts.push (← mkElseAlt) return alts def mkAuxFunction (ctx : Context) (i : Nat) : TermElabM Syntax := do let auxFunName ← ctx.auxFunNames[i] let indVal ← ctx.typeInfos[i] let header ← mkBEqHeader ctx indVal let mut body ← mkMatch ctx header indVal auxFunName if ctx.usePartial then let letDecls ← mkLocalInstanceLetDecls ctx `BEq header.argNames body ← mkLet letDecls body let binders := header.binders if ctx.usePartial then `(private partial def $(mkIdent auxFunName):ident $binders:explicitBinder* : Bool := $body:term) else `(private def $(mkIdent auxFunName):ident $binders:explicitBinder* : Bool := $body:term) def mkMutualBlock (ctx : Context) : TermElabM Syntax := do let mut auxDefs := #[] for i in [:ctx.typeInfos.size] do auxDefs := auxDefs.push (← mkAuxFunction ctx i) `(mutual set_option match.ignoreUnusedAlts true $auxDefs:command* end) private def mkBEqInstanceCmds (declNames : Array Name) : TermElabM (Array Syntax) := do let ctx ← mkContext "beq" declNames[0] let cmds := #[← mkMutualBlock ctx] ++ (← mkInstanceCmds ctx `BEq declNames) trace[Elab.Deriving.beq]! "\n{cmds}" return cmds open Command def mkBEqInstanceHandler (declNames : Array Name) : CommandElabM Bool := do if (← declNames.allM isInductive) && declNames.size > 0 then let cmds ← liftTermElabM none <| mkBEqInstanceCmds declNames cmds.forM elabCommand return true else return false builtin_initialize registerBuiltinDerivingHandler `BEq mkBEqInstanceHandler registerTraceClass `Elab.Deriving.beq end Lean.Elab.Deriving.BEq
b0c30f1c2a5dc80459066ee04656ea445791a64a
9dc8cecdf3c4634764a18254e94d43da07142918
/src/probability/martingale/convergence.lean
d29bc0750b90a236c41bc94fe5ec365f516c43f5
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
25,477
lean
/- Copyright (c) 2022 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import probability.martingale.upcrossing import measure_theory.function.uniform_integrable import measure_theory.constructions.polish /-! # Martingale convergence theorems The martingale convergence theorems are a collection of theorems characterizing the convergence of a martingale provided it satisfies some boundedness conditions. This file contains the almost everywhere martingale convergence theorem which provides an almost everywhere limit to an LΒΉ bounded submartingale. It also contains the LΒΉ martingale convergence theorem which provides an LΒΉ limit to a uniformly integrable submartingale. Finally, it also contains the LΓ©vy upwards theorems. ## Main results * `measure_theory.submartingale.ae_tendsto_limit_process`: the almost everywhere martingale convergence theorem: an LΒΉ-bounded submartingale adapted to the filtration `β„±` converges almost everywhere to its limit process. * `measure_theory.submartingale.mem_β„’p_limit_process`: the limit process of an Lα΅–-bounded submartingale is Lα΅–. * `measure_theory.submartingale.tendsto_snorm_one_limit_process`: part a of the LΒΉ martingale convergence theorem: a uniformly integrable submartingale adapted to the filtration `β„±` converges almost everywhere and in LΒΉ to an integrable function which is measurable with respect to the Οƒ-algebra `⨆ n, β„± n`. * `measure_theory.martingale.ae_eq_condexp_limit_process`: part b the LΒΉ martingale convergence theorem: if `f` is a uniformly integrable martingale adapted to the filtration `β„±`, then `f n` equals `𝔼[g | β„± n]` almost everywhere where `g` is the limiting process of `f`. * `measure_theory.integrable.tendsto_ae_condexp`: part c the LΒΉ martingale convergence theorem: given a `⨆ n, β„± n`-measurable function `g` where `β„±` is a filtration, `𝔼[g | β„± n]` converges almost everywhere to `g`. * `measure_theory.integrable.tendsto_snorm_condexp`: part c the LΒΉ martingale convergence theorem: given a `⨆ n, β„± n`-measurable function `g` where `β„±` is a filtration, `𝔼[g | β„± n]` converges in LΒΉ to `g`. -/ open topological_space filter measure_theory.filtration open_locale nnreal ennreal measure_theory probability_theory big_operators topological_space namespace measure_theory variables {Ξ© ΞΉ : Type*} {m0 : measurable_space Ξ©} {ΞΌ : measure Ξ©} {β„± : filtration β„• m0} variables {a b : ℝ} {f : β„• β†’ Ξ© β†’ ℝ} {Ο‰ : Ξ©} {R : ℝβ‰₯0} section ae_convergence /-! ### Almost everywhere martingale convergence theorem We will now prove the almost everywhere martingale convergence theorem. The a.e. martingale convergence theorem states: if `f` is an LΒΉ-bounded `β„±`-submartingale, then it converges almost everywhere to an integrable function which is measurable with respect to the Οƒ-algebra `β„±βˆž := ⨆ n, β„± n`. Mathematically, we proceed by first noting that a real sequence $(x_n)$ converges if (a) $\limsup_{n \to \infty} |x_n| < \infty$, (b) for all $a < b \in \mathbb{Q}$ we have the number of upcrossings of $(x_n)$ from below $a$ to above $b$ is finite. Thus, for all $\omega$ satisfying $\limsup_{n \to \infty} |f_n(\omega)| < \infty$ and the number of upcrossings of $(f_n(\omega))$ from below $a$ to above $b$ is finite for all $a < b \in \mathbb{Q}$, we have $(f_n(\omega))$ is convergent. Hence, assuming $(f_n)$ is LΒΉ-bounded, using Fatou's lemma, we have $$ \mathbb{E} \limsup_{n \to \infty} |f_n| \le \limsup_{n \to \infty} \mathbb{E}|f_n| < \infty $$ implying $\limsup_{n \to \infty} |f_n| < \infty$ a.e. Furthermore, by the upcrossing estimate, the number of upcrossings is finite almost everywhere implying $f$ converges pointwise almost everywhere. Thus, denoting $g$ the a.e. limit of $(f_n)$, $g$ is $\mathcal{F}_\infty$-measurable as for all $n$, $f_n$ is $\mathcal{F}_n$-measurable and $\mathcal{F}_n \le \mathcal{F}_\infty$. Finally, $g$ is integrable as $|g| \le \liminf_{n \to \infty} |f_n|$ so $$ \mathbb{E}|g| \le \mathbb{E} \limsup_{n \to \infty} |f_n| \le \limsup_{n \to \infty} \mathbb{E}|f_n| < \infty $$ as required. Implementation wise, we have `tendsto_of_no_upcrossings` which showed that a bounded sequence converges if it does not visit below $a$ and above $b$ infinitely often for all $a, b ∈ s$ for some dense set $s$. So, we may skip the first step provided we can prove that the realizations are bounded almost everywhere. Indeed, suppose $(|f_n(\omega)|)$ is not bounded, then either $f_n(\omega) \to \pm \infty$ or one of $\limsup f_n(\omega)$ or $\liminf f_n(\omega)$ equals $\pm \infty$ while the other is finite. But the first case contradicts $\liminf |f_n(\omega)| < \infty$ while the second case contradicts finite upcrossings. Furthermore, we introduced `filtration.limit_process` which chooses the limiting random variable of a stochastic process if it exists, otherwise it returns 0. Hence, instead of showing an existence statement, we phrased the a.e. martingale convergence theorem by showed that a submartingale converges to its `limit_process` almost everywhere. -/ /-- If a stochastic process has bounded upcrossing from below `a` to above `b`, then it does not frequently visit both below `a` and above `b`. -/ lemma not_frequently_of_upcrossings_lt_top (hab : a < b) (hΟ‰ : upcrossings a b f Ο‰ β‰  ∞) : Β¬((βˆƒαΆ  n in at_top, f n Ο‰ < a) ∧ (βˆƒαΆ  n in at_top, b < f n Ο‰)) := begin rw [← lt_top_iff_ne_top, upcrossings_lt_top_iff] at hΟ‰, replace hΟ‰ : βˆƒ k, βˆ€ N, upcrossings_before a b f N Ο‰ < k, { obtain ⟨k, hk⟩ := hΟ‰, exact ⟨k + 1, Ξ» N, lt_of_le_of_lt (hk N) k.lt_succ_self⟩ }, rintro ⟨h₁, hβ‚‚βŸ©, rw frequently_at_top at h₁ hβ‚‚, refine not_not.2 hΟ‰ _, push_neg, intro k, induction k with k ih, { simp only [zero_le', exists_const] }, { obtain ⟨N, hN⟩ := ih, obtain ⟨N₁, hN₁, hN₁'⟩ := h₁ N, obtain ⟨Nβ‚‚, hNβ‚‚, hNβ‚‚'⟩ := hβ‚‚ N₁, exact ⟨(Nβ‚‚ + 1), nat.succ_le_of_lt $ lt_of_le_of_lt hN (upcrossings_before_lt_of_exists_upcrossing hab hN₁ hN₁' hNβ‚‚ hNβ‚‚')⟩ } end /-- A stochastic process that frequently visits below `a` and above `b` have infinite upcrossings. -/ lemma upcrossings_eq_top_of_frequently_lt (hab : a < b) (h₁ : βˆƒαΆ  n in at_top, f n Ο‰ < a) (hβ‚‚ : βˆƒαΆ  n in at_top, b < f n Ο‰) : upcrossings a b f Ο‰ = ∞ := classical.by_contradiction (Ξ» h, not_frequently_of_upcrossings_lt_top hab h ⟨h₁, hβ‚‚βŸ©) /-- A realization of a stochastic process with bounded upcrossings and bounded liminfs is convergent. We use the spelling `< ∞` instead of the standard `β‰  ∞` in the assumptions since it is not as easy to change `<` to `β‰ ` under binders. -/ lemma tendsto_of_uncrossing_lt_top (hf₁ : liminf at_top (Ξ» n, (βˆ₯f n Ο‰βˆ₯β‚Š : ℝβ‰₯0∞)) < ∞) (hfβ‚‚ : βˆ€ a b : β„š, a < b β†’ upcrossings a b f Ο‰ < ∞) : βˆƒ c, tendsto (Ξ» n, f n Ο‰) at_top (𝓝 c) := begin by_cases h : is_bounded_under (≀) at_top (Ξ» n, |f n Ο‰|), { rw is_bounded_under_le_abs at h, refine tendsto_of_no_upcrossings rat.dense_range_cast _ h.1 h.2, { intros a ha b hb hab, obtain ⟨⟨a, rfl⟩, ⟨b, rfl⟩⟩ := ⟨ha, hb⟩, exact not_frequently_of_upcrossings_lt_top hab (hfβ‚‚ a b (rat.cast_lt.1 hab)).ne } }, { obtain ⟨a, b, hab, h₁, hβ‚‚βŸ© := ennreal.exists_upcrossings_of_not_bounded_under hf₁.ne h, exact false.elim ((hfβ‚‚ a b hab).ne (upcrossings_eq_top_of_frequently_lt (rat.cast_lt.2 hab) h₁ hβ‚‚)) } end /-- An LΒΉ-bounded submartingale has bounded upcrossings almost everywhere. -/ lemma submartingale.upcrossings_ae_lt_top' [is_finite_measure ΞΌ] (hf : submartingale f β„± ΞΌ) (hbdd : βˆ€ n, snorm (f n) 1 ΞΌ ≀ R) (hab : a < b) : βˆ€α΅ Ο‰ βˆ‚ΞΌ, upcrossings a b f Ο‰ < ∞ := begin refine ae_lt_top (hf.adapted.measurable_upcrossings hab) _, have := hf.mul_lintegral_upcrossings_le_lintegral_pos_part a b, rw [mul_comm, ← ennreal.le_div_iff_mul_le] at this, { refine (lt_of_le_of_lt this (ennreal.div_lt_top _ _)).ne, { have hR' : βˆ€ n, ∫⁻ Ο‰, βˆ₯f n Ο‰ - aβˆ₯β‚Š βˆ‚ΞΌ ≀ R + βˆ₯aβˆ₯β‚Š * ΞΌ set.univ, { simp_rw snorm_one_eq_lintegral_nnnorm at hbdd, intro n, refine (lintegral_mono _ : ∫⁻ Ο‰, βˆ₯f n Ο‰ - aβˆ₯β‚Š βˆ‚ΞΌ ≀ ∫⁻ Ο‰, βˆ₯f n Ο‰βˆ₯β‚Š + βˆ₯aβˆ₯β‚Š βˆ‚ΞΌ).trans _, { intro Ο‰, simp_rw [sub_eq_add_neg, ← nnnorm_neg a, ← ennreal.coe_add, ennreal.coe_le_coe], exact nnnorm_add_le _ _ }, { simp_rw [ lintegral_add_right _ measurable_const, lintegral_const], exact add_le_add (hbdd _) le_rfl } }, refine ne_of_lt (supr_lt_iff.2 ⟨R + βˆ₯aβˆ₯β‚Š * ΞΌ set.univ, ennreal.add_lt_top.2 ⟨ennreal.coe_lt_top, ennreal.mul_lt_top ennreal.coe_lt_top.ne (measure_ne_top _ _)⟩, Ξ» n, le_trans _ (hR' n)⟩), refine lintegral_mono (Ξ» Ο‰, _), rw [ennreal.of_real_le_iff_le_to_real, ennreal.coe_to_real, coe_nnnorm], by_cases hnonneg : 0 ≀ f n Ο‰ - a, { rw [lattice_ordered_comm_group.pos_of_nonneg _ hnonneg, real.norm_eq_abs, abs_of_nonneg hnonneg] }, { rw lattice_ordered_comm_group.pos_of_nonpos _ (not_le.1 hnonneg).le, exact norm_nonneg _ }, { simp only [ne.def, ennreal.coe_ne_top, not_false_iff] } }, { simp only [hab, ne.def, ennreal.of_real_eq_zero, sub_nonpos, not_le] } }, { simp only [hab, ne.def, ennreal.of_real_eq_zero, sub_nonpos, not_le, true_or]}, { simp only [ne.def, ennreal.of_real_ne_top, not_false_iff, true_or] } end lemma submartingale.upcrossings_ae_lt_top [is_finite_measure ΞΌ] (hf : submartingale f β„± ΞΌ) (hbdd : βˆ€ n, snorm (f n) 1 ΞΌ ≀ R) : βˆ€α΅ Ο‰ βˆ‚ΞΌ, βˆ€ a b : β„š, a < b β†’ upcrossings a b f Ο‰ < ∞ := begin simp only [ae_all_iff, eventually_imp_distrib_left], rintro a b hab, exact hf.upcrossings_ae_lt_top' hbdd (rat.cast_lt.2 hab), end /-- An LΒΉ-bounded submartingale converges almost everywhere. -/ lemma submartingale.exists_ae_tendsto_of_bdd [is_finite_measure ΞΌ] (hf : submartingale f β„± ΞΌ) (hbdd : βˆ€ n, snorm (f n) 1 ΞΌ ≀ R) : βˆ€α΅ Ο‰ βˆ‚ΞΌ, βˆƒ c, tendsto (Ξ» n, f n Ο‰) at_top (𝓝 c) := begin filter_upwards [hf.upcrossings_ae_lt_top hbdd, ae_bdd_liminf_at_top_of_snorm_bdd one_ne_zero (Ξ» n, (hf.strongly_measurable n).measurable.mono (β„±.le n) le_rfl) hbdd] with Ο‰ h₁ hβ‚‚, exact tendsto_of_uncrossing_lt_top hβ‚‚ h₁, end lemma submartingale.exists_ae_trim_tendsto_of_bdd [is_finite_measure ΞΌ] (hf : submartingale f β„± ΞΌ) (hbdd : βˆ€ n, snorm (f n) 1 ΞΌ ≀ R) : βˆ€α΅ Ο‰ βˆ‚(ΞΌ.trim (Sup_le (Ξ» m ⟨n, hn⟩, hn β–Έ β„±.le _) : (⨆ n, β„± n) ≀ m0)), βˆƒ c, tendsto (Ξ» n, f n Ο‰) at_top (𝓝 c) := begin rw [ae_iff, trim_measurable_set_eq], { exact hf.exists_ae_tendsto_of_bdd hbdd }, { exact measurable_set.compl (@measurable_set_exists_tendsto _ _ _ _ _ _ (⨆ n, β„± n) _ _ _ _ _ (Ξ» n, ((hf.strongly_measurable n).measurable.mono (le_Sup ⟨n, rfl⟩) le_rfl))) } end /-- **Almost everywhere martingale convergence theorem**: An LΒΉ-bounded submartingale converges almost everywhere to a `⨆ n, β„± n`-measurable function. -/ lemma submartingale.ae_tendsto_limit_process [is_finite_measure ΞΌ] (hf : submartingale f β„± ΞΌ) (hbdd : βˆ€ n, snorm (f n) 1 ΞΌ ≀ R) : βˆ€α΅ Ο‰ βˆ‚ΞΌ, tendsto (Ξ» n, f n Ο‰) at_top (𝓝 (β„±.limit_process f ΞΌ Ο‰)) := begin classical, suffices : βˆƒ g, strongly_measurable[⨆ n, β„± n] g ∧ βˆ€α΅ Ο‰ βˆ‚ΞΌ, tendsto (Ξ» n, f n Ο‰) at_top (𝓝 (g Ο‰)), { rw [limit_process, dif_pos this], exact (classical.some_spec this).2 }, set g' : Ξ© β†’ ℝ := Ξ» Ο‰, if h : βˆƒ c, tendsto (Ξ» n, f n Ο‰) at_top (𝓝 c) then h.some else 0, have hle : (⨆ n, β„± n) ≀ m0 := Sup_le (Ξ» m ⟨n, hn⟩, hn β–Έ β„±.le _), have hg' : βˆ€α΅ Ο‰ βˆ‚(ΞΌ.trim hle), tendsto (Ξ» n, f n Ο‰) at_top (𝓝 (g' Ο‰)), { filter_upwards [hf.exists_ae_trim_tendsto_of_bdd hbdd] with Ο‰ hΟ‰, simp_rw [g', dif_pos hΟ‰], exact hΟ‰.some_spec }, have hg'm : @ae_strongly_measurable _ _ _ (⨆ n, β„± n) g' (ΞΌ.trim hle) := (@ae_measurable_of_tendsto_metrizable_ae' _ _ (⨆ n, β„± n) _ _ _ _ _ _ _ (Ξ» n, ((hf.strongly_measurable n).measurable.mono (le_Sup ⟨n, rfl⟩ : β„± n ≀ ⨆ n, β„± n) le_rfl).ae_measurable) hg').ae_strongly_measurable, obtain ⟨g, hgm, hae⟩ := hg'm, have hg : βˆ€α΅ Ο‰ βˆ‚ΞΌ.trim hle, tendsto (Ξ» n, f n Ο‰) at_top (𝓝 (g Ο‰)), { filter_upwards [hae, hg'] with Ο‰ hΟ‰ hg'Ο‰, exact hΟ‰ β–Έ hg'Ο‰ }, exact ⟨g, hgm, measure_eq_zero_of_trim_eq_zero hle hg⟩, end /-- The limiting process of an Lα΅–-bounded submartingale is Lα΅–. -/ lemma submartingale.mem_β„’p_limit_process {p : ℝβ‰₯0∞} (hf : submartingale f β„± ΞΌ) (hbdd : βˆ€ n, snorm (f n) p ΞΌ ≀ R) : mem_β„’p (β„±.limit_process f ΞΌ) p ΞΌ := mem_β„’p_limit_process_of_snorm_bdd (Ξ» n, ((hf.strongly_measurable n).mono (β„±.le n)).ae_strongly_measurable) hbdd end ae_convergence section L1_convergence variables [is_finite_measure ΞΌ] {g : Ξ© β†’ ℝ} /-! ### LΒΉ martingale convergence theorem We will now prove the LΒΉ martingale convergence theorems. The LΒΉ martingale convergence theorem states that: (a) if `f` is a uniformly integrable (in the probability sense) submartingale adapted to the filtration `β„±`, it converges in LΒΉ to an integrable function `g` which is measurable with respect to `β„±βˆž := ⨆ n, β„± n` and (b) if `f` is actually a martingale, `f n = 𝔼[g | β„± n]` almost everywhere. (c) Finally, if `h` is integrable and measurable with respect to `β„±βˆž`, `(𝔼[h | β„± n])β‚™` is a uniformly integrable martingale which converges to `h` almost everywhere and in LΒΉ. The proof is quite simple. (a) follows directly from the a.e. martingale convergence theorem and the Vitali convergence theorem as our definition of uniform integrability (in the probability sense) directly implies LΒΉ-uniform boundedness. We note that our definition of uniform integrability is slightly non-standard but is equivalent to the usual literary definition. This equivalence is provided by `measure_theory.uniform_integrable_iff`. (b) follows since given $n$, we have for all $m \ge n$, $$ \|f_n - \mathbb{E}[g \mid \mathcal{F}_n]\|_1 = \|\mathbb{E}[f_m - g \mid \mathcal{F}_n]\|_1 \le \|\|f_m - g\|_1. $$ Thus, taking $m \to \infty$ provides the almost everywhere equality. Finally, to prove (c), we define $f_n := \mathbb{E}[h \mid \mathcal{F}_n]$. It is clear that $(f_n)_n$ is a martingale by the tower property for conditional expectations. Furthermore, $(f_n)_n$ is uniformly integrable in the probability sense. Indeed, as a single function is uniformly integrable in the measure theory sense, for all $\epsilon > 0$, there exists some $\delta > 0$ such that for all measurable set $A$ with $\mu(A) < Ξ΄$, we have $\mathbb{E}|h|\mathbf{1}_A < \epsilon$. So, since for sufficently large $\lambda$, by the Markov inequality, we have for all $n$, $$ \mu(|f_n| \ge \lambda) \le \lambda^{-1}\mathbb{E}|f_n| \le \lambda^{-1}\mathbb|g| < \delta, $$ we have for sufficently large $\lambda$, for all $n$, $$ \mathbb{E}|f_n|\mathbf{1}_{|f_n| \ge \lambda} \le \mathbb|g|\mathbf{1}_{|f_n| \ge \lambda} < \epsilon, $$ implying $(f_n)_n$ is uniformly integrable. Now, to prove $f_n \to h$ almost everywhere and in LΒΉ, it suffices to show that $h = g$ almost everywhere where $g$ is the almost everywhere and LΒΉ limit of $(f_n)_n$ from part (b) of the theorem. By noting that, for all $s \in \mathcal{F}_n$, we have $$ \mathbb{E}g\mathbf{1}_s = \mathbb{E}[\mathbb{E}[g \mid \mathcal{F}_n]\mathbf{1}_s] = \mathbb{E}[\mathbb{E}[h \mid \mathcal{F}_n]\mathbf{1}_s] = \mathbb{E}h\mathbf{1}_s $$ where $\mathbb{E}[g \mid \mathcal{F}_n = \mathbb{E}[h \mid \mathcal{F}_n]$ almost everywhere by part (b); the equality also holds for all $s \in \mathcal{F}_\infty$ by Dynkin's theorem. Thus, as both $h$ and $g$ are $\mathcal{F}_\infty$-measurable, $h = g$ almost everywhere as required. Similar to the a.e. martingale convergence theorem, rather than showing the existence of the limiting process, we phrased the LΒΉ-martingale convergence theorem by proving that a submartingale does converge in LΒΉ to its `limit_process`. However, in contrast to the a.e. martingale convergence theorem, we do not need to introduce a LΒΉ version of `filtration.limit_process` as the LΒΉ limit and the a.e. limit of a submartingale coincide. -/ /-- Part a of the **LΒΉ martingale convergence theorem**: a uniformly integrable submartingale adapted to the filtration `β„±` converges a.e. and in LΒΉ to an integrable function which is measurable with respect to the Οƒ-algebra `⨆ n, β„± n`. -/ lemma submartingale.tendsto_snorm_one_limit_process (hf : submartingale f β„± ΞΌ) (hunif : uniform_integrable f 1 ΞΌ) : tendsto (Ξ» n, snorm (f n - β„±.limit_process f ΞΌ) 1 ΞΌ) at_top (𝓝 0) := begin obtain ⟨R, hR⟩ := hunif.2.2, have hmeas : βˆ€ n, ae_strongly_measurable (f n) ΞΌ := Ξ» n, ((hf.strongly_measurable n).mono (β„±.le _)).ae_strongly_measurable, exact tendsto_Lp_of_tendsto_in_measure _ le_rfl ennreal.one_ne_top hmeas (mem_β„’p_limit_process_of_snorm_bdd hmeas hR) hunif.2.1 (tendsto_in_measure_of_tendsto_ae hmeas $ hf.ae_tendsto_limit_process hR), end lemma submartingale.ae_tendsto_limit_process_of_uniform_integrable (hf : submartingale f β„± ΞΌ) (hunif : uniform_integrable f 1 ΞΌ) : βˆ€α΅ Ο‰ βˆ‚ΞΌ, tendsto (Ξ» n, f n Ο‰) at_top (𝓝 (β„±.limit_process f ΞΌ Ο‰)) := let ⟨R, hR⟩ := hunif.2.2 in hf.ae_tendsto_limit_process hR /-- If a martingale `f` adapted to `β„±` converges in LΒΉ to `g`, then for all `n`, `f n` is almost everywhere equal to `𝔼[g | β„± n]`. -/ lemma martingale.eq_condexp_of_tendsto_snorm {ΞΌ : measure Ξ©} (hf : martingale f β„± ΞΌ) (hg : integrable g ΞΌ) (hgtends : tendsto (Ξ» n, snorm (f n - g) 1 ΞΌ) at_top (𝓝 0)) (n : β„•) : f n =ᡐ[ΞΌ] ΞΌ[g | β„± n] := begin rw [← sub_ae_eq_zero, ← snorm_eq_zero_iff ((((hf.strongly_measurable n).mono (β„±.le _)).sub (strongly_measurable_condexp.mono (β„±.le _))).ae_strongly_measurable) one_ne_zero], have ht : tendsto (Ξ» m, snorm (ΞΌ[f m - g | β„± n]) 1 ΞΌ) at_top (𝓝 0), { have hint : βˆ€ m, integrable (f m - g) ΞΌ := Ξ» m, (hf.integrable m).sub hg, exact tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds hgtends (Ξ» m, zero_le _) (Ξ» m, snorm_one_condexp_le_snorm _) }, have hev : βˆ€ m β‰₯ n, snorm (ΞΌ[f m - g | β„± n]) 1 ΞΌ = snorm (f n - ΞΌ[g | β„± n]) 1 ΞΌ, { refine Ξ» m hm, snorm_congr_ae ((condexp_sub (hf.integrable m) hg).trans _), filter_upwards [hf.2 n m hm] with x hx, simp only [hx, pi.sub_apply] }, exact tendsto_nhds_unique (tendsto_at_top_of_eventually_const hev) ht, end /-- Part b of the **LΒΉ martingale convergence theorem**: if `f` is a uniformly integrable martingale adapted to the filtration `β„±`, then for all `n`, `f n` is almost everywhere equal to the conditional expectation of its limiting process wrt. `β„± n`. -/ lemma martingale.ae_eq_condexp_limit_process (hf : martingale f β„± ΞΌ) (hbdd : uniform_integrable f 1 ΞΌ) (n : β„•) : f n =ᡐ[ΞΌ] ΞΌ[β„±.limit_process f ΞΌ | β„± n] := let ⟨R, hR⟩ := hbdd.2.2 in hf.eq_condexp_of_tendsto_snorm ((mem_β„’p_limit_process_of_snorm_bdd hbdd.1 hR).integrable le_rfl) (hf.submartingale.tendsto_snorm_one_limit_process hbdd) n /-- Part c of the **LΒΉ martingale convergnce theorem**: Given a integrable function `g` which is measurable with respect to `⨆ n, β„± n` where `β„±` is a filtration, the martingale defined by `𝔼[g | β„± n]` converges almost everywhere to `g`. This martingale also converges to `g` in LΒΉ and this result is provided by `measure_theory.integrable.tendsto_snorm_condexp` -/ lemma integrable.tendsto_ae_condexp (hg : integrable g ΞΌ) (hgmeas : strongly_measurable[⨆ n, β„± n] g) : βˆ€α΅ x βˆ‚ΞΌ, tendsto (Ξ» n, ΞΌ[g | β„± n] x) at_top (𝓝 (g x)) := begin have hle : (⨆ n, β„± n) ≀ m0 := Sup_le (Ξ» m ⟨n, hn⟩, hn β–Έ β„±.le _), have hunif : uniform_integrable (Ξ» n, ΞΌ[g | β„± n]) 1 ΞΌ := hg.uniform_integrable_condexp_filtration, obtain ⟨R, hR⟩ := hunif.2.2, have hlimint : integrable (β„±.limit_process (Ξ» n, ΞΌ[g | β„± n]) ΞΌ) ΞΌ := (mem_β„’p_limit_process_of_snorm_bdd hunif.1 hR).integrable le_rfl, suffices : g =ᡐ[ΞΌ] β„±.limit_process (Ξ» n x, ΞΌ[g | β„± n] x) ΞΌ, { filter_upwards [this, (martingale_condexp g β„± ΞΌ).submartingale.ae_tendsto_limit_process hR] with x heq ht, rwa heq }, have : βˆ€ n s, measurable_set[β„± n] s β†’ ∫ x in s, g x βˆ‚ΞΌ = ∫ x in s, β„±.limit_process (Ξ» n x, ΞΌ[g | β„± n] x) ΞΌ x βˆ‚ΞΌ, { intros n s hs, rw [← set_integral_condexp (β„±.le n) hg hs, ← set_integral_condexp (β„±.le n) hlimint hs], refine set_integral_congr_ae (β„±.le _ _ hs) _, filter_upwards [(martingale_condexp g β„± ΞΌ).ae_eq_condexp_limit_process hunif n] with x hx _, rwa hx }, refine ae_eq_of_forall_set_integral_eq_of_sigma_finite' hle (Ξ» s _ _, hg.integrable_on) (Ξ» s _ _, hlimint.integrable_on) (Ξ» s hs, _) hgmeas.ae_strongly_measurable' strongly_measurable_limit_process.ae_strongly_measurable', refine @measurable_space.induction_on_inter _ _ _ (⨆ n, β„± n) (measurable_space.measurable_space_supr_eq β„±) _ _ _ _ _ _ hs, { rintro s ⟨n, hs⟩ t ⟨m, ht⟩ -, by_cases hnm : n ≀ m, { exact ⟨m, (β„±.mono hnm _ hs).inter ht⟩ }, { exact ⟨n, hs.inter (β„±.mono (not_le.1 hnm).le _ ht)⟩ } }, { simp only [measure_empty, with_top.zero_lt_top, measure.restrict_empty, integral_zero_measure, forall_true_left] }, { rintro t ⟨n, ht⟩ -, exact this n _ ht }, { rintro t htmeas ht -, have hgeq := @integral_add_compl _ _ (⨆ n, β„± n) _ _ _ _ _ _ htmeas (hg.trim hle hgmeas), have hheq := @integral_add_compl _ _ (⨆ n, β„± n) _ _ _ _ _ _ htmeas (hlimint.trim hle strongly_measurable_limit_process), rw [add_comm, ← eq_sub_iff_add_eq] at hgeq hheq, rw [set_integral_trim hle hgmeas htmeas.compl, set_integral_trim hle strongly_measurable_limit_process htmeas.compl, hgeq, hheq, ← set_integral_trim hle hgmeas htmeas, ← set_integral_trim hle strongly_measurable_limit_process htmeas, ← integral_trim hle hgmeas, ← integral_trim hle strongly_measurable_limit_process, ← integral_univ, this 0 _ measurable_set.univ, integral_univ, ht (measure_lt_top _ _)] }, { rintro f hf hfmeas heq -, rw [integral_Union (Ξ» n, hle _ (hfmeas n)) hf hg.integrable_on, integral_Union (Ξ» n, hle _ (hfmeas n)) hf hlimint.integrable_on], exact tsum_congr (Ξ» n, heq _ (measure_lt_top _ _)) } end /-- Part c of the **LΒΉ martingale convergnce theorem**: Given a integrable function `g` which is measurable with respect to `⨆ n, β„± n` where `β„±` is a filtration, the martingale defined by `𝔼[g | β„± n]` converges in LΒΉ to `g`. This martingale also converges to `g` almost everywhere and this result is provided by `measure_theory.integrable.tendsto_ae_condexp` -/ lemma integrable.tendsto_snorm_condexp (hg : integrable g ΞΌ) (hgmeas : strongly_measurable[⨆ n, β„± n] g) : tendsto (Ξ» n, snorm (ΞΌ[g | β„± n] - g) 1 ΞΌ) at_top (𝓝 0) := tendsto_Lp_of_tendsto_in_measure _ le_rfl ennreal.one_ne_top (Ξ» n, (strongly_measurable_condexp.mono (β„±.le n)).ae_strongly_measurable) (mem_β„’p_one_iff_integrable.2 hg) (hg.uniform_integrable_condexp_filtration).2.1 (tendsto_in_measure_of_tendsto_ae (Ξ» n,(strongly_measurable_condexp.mono (β„±.le n)).ae_strongly_measurable) (hg.tendsto_ae_condexp hgmeas)) /-- **LΓ©vy's upward theorem**, almost everywhere version: given a function `g` and a filtration `β„±`, the sequence defined by `𝔼[g | β„± n]` converges almost everywhere to `𝔼[g | ⨆ n, β„± n]`. -/ lemma tendsto_ae_condexp (g : Ξ© β†’ ℝ) : βˆ€α΅ x βˆ‚ΞΌ, tendsto (Ξ» n, ΞΌ[g | β„± n] x) at_top (𝓝 (ΞΌ[g | ⨆ n, β„± n] x)) := begin have ht : βˆ€α΅ x βˆ‚ΞΌ, tendsto (Ξ» n, ΞΌ[ΞΌ[g | ⨆ n, β„± n] | β„± n] x) at_top (𝓝 (ΞΌ[g | ⨆ n, β„± n] x)) := integrable_condexp.tendsto_ae_condexp strongly_measurable_condexp, have heq : βˆ€ n, βˆ€α΅ x βˆ‚ΞΌ, ΞΌ[ΞΌ[g | ⨆ n, β„± n] | β„± n] x = ΞΌ[g | β„± n] x := Ξ» n, condexp_condexp_of_le (le_supr _ n) (supr_le (Ξ» n, β„±.le n)), rw ← ae_all_iff at heq, filter_upwards [heq, ht] with x hxeq hxt, exact hxt.congr hxeq, end /-- **LΓ©vy's upward theorem**, LΒΉ version: given a function `g` and a filtration `β„±`, the sequence defined by `𝔼[g | β„± n]` converges in LΒΉ to `𝔼[g | ⨆ n, β„± n]`. -/ lemma tendsto_snorm_condexp (g : Ξ© β†’ ℝ) : tendsto (Ξ» n, snorm (ΞΌ[g | β„± n] - ΞΌ[g | ⨆ n, β„± n]) 1 ΞΌ) at_top (𝓝 0) := begin have ht : tendsto (Ξ» n, snorm (ΞΌ[ΞΌ[g | ⨆ n, β„± n] | β„± n] - ΞΌ[g | ⨆ n, β„± n]) 1 ΞΌ) at_top (𝓝 0) := integrable_condexp.tendsto_snorm_condexp strongly_measurable_condexp, have heq : βˆ€ n, βˆ€α΅ x βˆ‚ΞΌ, ΞΌ[ΞΌ[g | ⨆ n, β„± n] | β„± n] x = ΞΌ[g | β„± n] x := Ξ» n, condexp_condexp_of_le (le_supr _ n) (supr_le (Ξ» n, β„±.le n)), refine ht.congr (Ξ» n, snorm_congr_ae _), filter_upwards [heq n] with x hxeq, simp only [hxeq, pi.sub_apply], end end L1_convergence end measure_theory
fb59d3d7b716804f933f261201005098193e1060
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Elab/Tactic/Induction.lean
b71cac0011275a89e5a87d67f65d751d5833757a
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
26,057
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Util.CollectFVars import Lean.Parser.Term import Lean.Meta.RecursorInfo import Lean.Meta.CollectMVars import Lean.Meta.Tactic.ElimInfo import Lean.Meta.Tactic.Induction import Lean.Meta.Tactic.Cases import Lean.Meta.GeneralizeVars import Lean.Elab.App import Lean.Elab.Tactic.ElabTerm import Lean.Elab.Tactic.Generalize namespace Lean.Elab.Tactic open Meta /- Given an `inductionAlt` of the form ``` syntax inductionAltLHS := "| " (group("@"? ident) <|> hole) (ident <|> hole)* syntax inductionAlt := ppDedent(ppLine) inductionAltLHS+ " => " (hole <|> syntheticHole <|> tacticSeq) ``` -/ private def getFirstAltLhs (alt : Syntax) : Syntax := alt[0][0] /-- Return `inductionAlt` name. It assumes `alt` does not have multiple `inductionAltLHS` -/ private def getAltName (alt : Syntax) : Name := let lhs := getFirstAltLhs alt if !lhs[1].isOfKind ``Parser.Term.hole then lhs[1][1].getId.eraseMacroScopes else `_ /-- Return `true` if the first LHS of the given alternative contains `@`. -/ private def altHasExplicitModifier (alt : Syntax) : Bool := let lhs := getFirstAltLhs alt !lhs[1].isOfKind ``Parser.Term.hole && !lhs[1][0].isNone /-- Return the variables in the first LHS of the given alternative. -/ private def getAltVars (alt : Syntax) : Array Syntax := let lhs := getFirstAltLhs alt lhs[2].getArgs /-- Return the variable names in the first LHS of the given alternative. -/ private def getAltVarNames (alt : Syntax) : Array Name := getAltVars alt |>.map getNameOfIdent' private def getAltRHS (alt : Syntax) : Syntax := alt[2] private def getAltDArrow (alt : Syntax) : Syntax := alt[1] -- Return true if `stx` is a term occurring in the RHS of the induction/cases tactic def isHoleRHS (rhs : Syntax) : Bool := rhs.isOfKind ``Parser.Term.syntheticHole || rhs.isOfKind ``Parser.Term.hole def evalAlt (mvarId : MVarId) (alt : Syntax) (remainingGoals : Array MVarId) : TacticM (Array MVarId) := let rhs := getAltRHS alt withCaseRef (getAltDArrow alt) rhs do if isHoleRHS rhs then let gs' ← mvarId.withContext <| withRef rhs do let mvarDecl ← mvarId.getDecl let val ← elabTermEnsuringType rhs mvarDecl.type mvarId.assign val let gs' ← getMVarsNoDelayed val tagUntaggedGoals mvarDecl.userName `induction gs'.toList pure gs' return remainingGoals ++ gs' else setGoals [mvarId] closeUsingOrAdmit (withTacticInfoContext alt (evalTactic rhs)) return remainingGoals /-! Helper method for creating an user-defined eliminator/recursor application. -/ namespace ElimApp structure Context where elimInfo : ElimInfo targets : Array Expr -- targets provided by the user structure State where argPos : Nat := 0 -- current argument position targetPos : Nat := 0 -- current target at targetsStx f : Expr fType : Expr alts : Array (Name Γ— MVarId) := #[] insts : Array MVarId := #[] abbrev M := ReaderT Context $ StateRefT State TermElabM private def addNewArg (arg : Expr) : M Unit := modify fun s => { s with argPos := s.argPos+1, f := mkApp s.f arg, fType := s.fType.bindingBody!.instantiate1 arg } /-- Return the binder name at `fType`. This method assumes `fType` is a function type. -/ private def getBindingName : M Name := return (← get).fType.bindingName! /-- Return the next argument expected type. This method assumes `fType` is a function type. -/ private def getArgExpectedType : M Expr := return (← get).fType.bindingDomain! private def getFType : M Expr := do let fType ← whnfForall (← get).fType modify fun s => { s with fType := fType } pure fType structure Result where elimApp : Expr alts : Array (Name Γ— MVarId) := #[] others : Array MVarId := #[] /-- Construct the an eliminator/recursor application. `targets` contains the explicit and implicit targets for the eliminator. For example, the indices of builtin recursors are considered implicit targets. Remark: the method `addImplicitTargets` may be used to compute the sequence of implicit and explicit targets from the explicit ones. -/ partial def mkElimApp (elimInfo : ElimInfo) (targets : Array Expr) (tag : Name) : TermElabM Result := do let rec loop : M Unit := do match (← getFType) with | .forallE binderName _ _ c => let ctx ← read let argPos := (← get).argPos if ctx.elimInfo.motivePos == argPos then let motive ← mkFreshExprMVar (← getArgExpectedType) MetavarKind.syntheticOpaque addNewArg motive else if ctx.elimInfo.targetsPos.contains argPos then let s ← get let ctx ← read unless s.targetPos < ctx.targets.size do throwError "insufficient number of targets for '{elimInfo.name}'" let target := ctx.targets[s.targetPos]! let expectedType ← getArgExpectedType let target ← withAssignableSyntheticOpaque <| Term.ensureHasType expectedType target modify fun s => { s with targetPos := s.targetPos + 1 } addNewArg target else match c with | .implicit => let arg ← mkFreshExprMVar (← getArgExpectedType) addNewArg arg | .strictImplicit => let arg ← mkFreshExprMVar (← getArgExpectedType) addNewArg arg | .instImplicit => let arg ← mkFreshExprMVar (← getArgExpectedType) (kind := MetavarKind.synthetic) (userName := appendTag tag binderName) modify fun s => { s with insts := s.insts.push arg.mvarId! } addNewArg arg | _ => let arg ← mkFreshExprSyntheticOpaqueMVar (← getArgExpectedType) (tag := appendTag tag binderName) let x ← getBindingName modify fun s => { s with alts := s.alts.push (x, arg.mvarId!) } addNewArg arg loop | _ => pure () let f ← Term.mkConst elimInfo.name let fType ← inferType f let (_, s) ← (loop).run { elimInfo := elimInfo, targets := targets } |>.run { f := f, fType := fType } let mut others := #[] for mvarId in s.insts do try unless (← Term.synthesizeInstMVarCore mvarId) do mvarId.setKind .syntheticOpaque others := others.push mvarId catch _ => mvarId.setKind .syntheticOpaque others := others.push mvarId let alts ← s.alts.filterM fun alt => return !(← alt.2.isAssigned) return { elimApp := (← instantiateMVars s.f), alts, others := others } /-- Given a goal `... targets ... |- C[targets]` associated with `mvarId`, assign `motiveArg := fun targets => C[targets]` -/ def setMotiveArg (mvarId : MVarId) (motiveArg : MVarId) (targets : Array FVarId) : MetaM Unit := do let type ← inferType (mkMVar mvarId) let motive ← mkLambdaFVars (targets.map mkFVar) type let motiverInferredType ← inferType motive let motiveType ← inferType (mkMVar motiveArg) unless (← isDefEqGuarded motiverInferredType motiveType) do throwError "type mismatch when assigning motive{indentExpr motive}\n{← mkHasTypeButIsExpectedMsg motiverInferredType motiveType}" motiveArg.assign motive private def getAltNumFields (elimInfo : ElimInfo) (altName : Name) : TermElabM Nat := do for altInfo in elimInfo.altsInfo do if altInfo.name == altName then return altInfo.numFields throwError "unknown alternative name '{altName}'" private def checkAltNames (alts : Array (Name Γ— MVarId)) (altsSyntax : Array Syntax) : TacticM Unit := for i in [:altsSyntax.size] do let altStx := altsSyntax[i]! if getAltName altStx == `_ && i != altsSyntax.size - 1 then withRef altStx <| throwError "invalid occurrence of wildcard alternative, it must be the last alternative" let altName := getAltName altStx if altName != `_ then unless alts.any fun (n, _) => n == altName do throwErrorAt altStx "invalid alternative name '{altName}'" /-- Given the goal `altMVarId` for a given alternative that introduces `numFields` new variables, return the number of explicit variables. Recall that when the `@` is not used, only the explicit variables can be named by the user. -/ private def getNumExplicitFields (altMVarId : MVarId) (numFields : Nat) : MetaM Nat := altMVarId.withContext do let target ← altMVarId.getType withoutModifyingState do let (_, bis, _) ← forallMetaBoundedTelescope target numFields return bis.foldl (init := 0) fun r bi => if bi.isExplicit then r + 1 else r private def saveAltVarsInfo (altMVarId : MVarId) (altStx : Syntax) (fvarIds : Array FVarId) : TacticM Unit := withSaveInfoContext <| altMVarId.withContext do let useNamesForExplicitOnly := !altHasExplicitModifier altStx let mut i := 0 let altVars := getAltVars altStx for fvarId in fvarIds do if !useNamesForExplicitOnly || (← fvarId.getDecl).binderInfo.isExplicit then if i < altVars.size then Term.addLocalVarInfo altVars[i]! (mkFVar fvarId) i := i + 1 /-- If `altsSyntax` is not empty we reorder `alts` using the order the alternatives have been provided in `altsSyntax`. Motivations: 1- It improves the effectiveness of the `checkpoint` and `save` tactics. Consider the following example: ```lean example (h₁ : p ∨ q) (hβ‚‚ : p β†’ x = 0) (h₃ : q β†’ y = 0) : x * y = 0 := by cases h₁ with | inr h => sleep 5000 -- sleeps for 5 seconds save have : y = 0 := h₃ h -- We can confortably work here | inl h => stop ... ``` If we do reorder, the `inl` alternative will be executed first. Moreover, as we type in the `inr` alternative, type errors will "swallow" the `inl` alternative and affect the tactic state at `save` making it ineffective. 2- The errors are produced in the same order the appear in the code above. This is not super important when using IDEs. -/ def reorderAlts (alts : Array (Name Γ— MVarId)) (altsSyntax : Array Syntax) : Array (Name Γ— MVarId) := Id.run do if altsSyntax.isEmpty then return alts else let mut alts := alts let mut result := #[] for altStx in altsSyntax do let altName := getAltName altStx let some i := alts.findIdx? (Β·.1 == altName) | return result ++ alts result := result.push alts[i]! alts := alts.eraseIdx i return result ++ alts def evalAlts (elimInfo : ElimInfo) (alts : Array (Name Γ— MVarId)) (optPreTac : Syntax) (altsSyntax : Array Syntax) (initialInfo : Info) (numEqs : Nat := 0) (numGeneralized : Nat := 0) (toClear : Array FVarId := #[]) : TacticM Unit := do checkAltNames alts altsSyntax let hasAlts := altsSyntax.size > 0 if hasAlts then -- default to initial state outside of alts withInfoContext go (pure initialInfo) else go where go := do let alts := reorderAlts alts altsSyntax let hasAlts := altsSyntax.size > 0 let mut usedWildcard := false let mut subgoals := #[] -- when alternatives are not provided, we accumulate subgoals here let mut altsSyntax := altsSyntax for (altName, altMVarId) in alts do let numFields ← getAltNumFields elimInfo altName let mut isWildcard := false let altStx? ← match altsSyntax.findIdx? (fun alt => getAltName alt == altName) with | some idx => let altStx := altsSyntax[idx]! altsSyntax := altsSyntax.eraseIdx idx pure (some altStx) | none => match altsSyntax.findIdx? (fun alt => getAltName alt == `_) with | some idx => isWildcard := true pure (some altsSyntax[idx]!) | none => pure none match altStx? with | none => let mut (_, altMVarId) ← altMVarId.introN numFields match (← Cases.unifyEqs? numEqs altMVarId {}) with | none => pure () -- alternative is not reachable | some (altMVarId', _) => (_, altMVarId) ← altMVarId'.introNP numGeneralized for fvarId in toClear do altMVarId ← altMVarId.tryClear fvarId let altMVarIds ← applyPreTac altMVarId if !hasAlts then -- User did not provide alternatives using `|` subgoals := subgoals ++ altMVarIds.toArray else if altMVarIds.isEmpty then pure () else logError m!"alternative '{altName}' has not been provided" altMVarIds.forM fun mvarId => admitGoal mvarId | some altStx => (subgoals, usedWildcard) ← withRef altStx do let unusedAlt := if isWildcard then pure (#[], usedWildcard) else throwError "alternative '{altName}' is not needed" let altVarNames := getAltVarNames altStx let numFieldsToName ← if altHasExplicitModifier altStx then pure numFields else getNumExplicitFields altMVarId numFields if altVarNames.size > numFieldsToName then logError m!"too many variable names provided at alternative '{altName}', #{altVarNames.size} provided, but #{numFieldsToName} expected" let mut (fvarIds, altMVarId) ← altMVarId.introN numFields altVarNames.toList (useNamesForExplicitOnly := !altHasExplicitModifier altStx) saveAltVarsInfo altMVarId altStx fvarIds match (← Cases.unifyEqs? numEqs altMVarId {}) with | none => unusedAlt | some (altMVarId', _) => (_, altMVarId) ← altMVarId'.introNP numGeneralized for fvarId in toClear do altMVarId ← altMVarId.tryClear fvarId let altMVarIds ← applyPreTac altMVarId if altMVarIds.isEmpty then unusedAlt else let mut subgoals := subgoals for altMVarId' in altMVarIds do subgoals ← evalAlt altMVarId' altStx subgoals pure (subgoals, usedWildcard || isWildcard) if usedWildcard then altsSyntax := altsSyntax.filter fun alt => getAltName alt != `_ unless altsSyntax.isEmpty do logErrorAt altsSyntax[0]! "unused alternative" setGoals subgoals.toList applyPreTac (mvarId : MVarId) : TacticM (List MVarId) := if optPreTac.isNone then return [mvarId] else evalTacticAt optPreTac[0] mvarId end ElimApp /- Recall that ``` generalizingVars := optional (" generalizing " >> many1 ident) Β«inductionΒ» := leading_parser nonReservedSymbol "induction " >> majorPremise >> usingRec >> generalizingVars >> optional inductionAlts ``` `stx` is syntax for `induction`. -/ private def getUserGeneralizingFVarIds (stx : Syntax) : TacticM (Array FVarId) := withRef stx do let generalizingStx := stx[3] if generalizingStx.isNone then pure #[] else trace[Elab.induction] "{generalizingStx}" let vars := generalizingStx[1].getArgs getFVarIds vars -- process `generalizingVars` subterm of induction Syntax `stx`. private def generalizeVars (mvarId : MVarId) (stx : Syntax) (targets : Array Expr) : TacticM (Nat Γ— MVarId) := mvarId.withContext do let userFVarIds ← getUserGeneralizingFVarIds stx let forbidden ← mkGeneralizationForbiddenSet targets let mut s ← getFVarSetToGeneralize targets forbidden for userFVarId in userFVarIds do if forbidden.contains userFVarId then throwError "variable cannot be generalized because target depends on it{indentExpr (mkFVar userFVarId)}" if s.contains userFVarId then throwError "unnecessary 'generalizing' argument, variable '{mkFVar userFVarId}' is generalized automatically" s := s.insert userFVarId let fvarIds ← sortFVarIds s.toArray let (fvarIds, mvarId') ← mvarId.revert fvarIds return (fvarIds.size, mvarId') /-- Given `inductionAlts` of the fom ``` syntax inductionAlts := "with " (tactic)? withPosition( (colGe inductionAlt)+) ``` Return an array containing its alternatives. -/ private def getAltsOfInductionAlts (inductionAlts : Syntax) : Array Syntax := inductionAlts[2].getArgs private def getAltsOfOptInductionAlts (optInductionAlts : Syntax) : Array Syntax := if optInductionAlts.isNone then #[] else getAltsOfInductionAlts optInductionAlts[0] private def getOptPreTacOfOptInductionAlts (optInductionAlts : Syntax) : Syntax := if optInductionAlts.isNone then mkNullNode else optInductionAlts[0][1] private def isMultiAlt (alt : Syntax) : Bool := alt[0].getNumArgs > 1 /-- Return `some #[alt_1, ..., alt_n]` if `alt` has multiple LHSs. -/ private def expandMultiAlt? (alt : Syntax) : Option (Array Syntax) := Id.run do if isMultiAlt alt then some <| alt[0].getArgs.map fun lhs => alt.setArg 0 (mkNullNode #[lhs]) else none /-- Given `inductionAlts` of the form ``` syntax inductionAlts := "with " (tactic)? withPosition( (colGe inductionAlt)+) ``` Return `some inductionAlts'` if one of the alternatives have multiple LHSs, in the new `inductionAlts'` all alternatives have a single LHS. Remark: the `RHS` of alternatives with multi LHSs is copied. -/ private def expandInductionAlts? (inductionAlts : Syntax) : Option Syntax := Id.run do let alts := getAltsOfInductionAlts inductionAlts if alts.any isMultiAlt then let mut altsNew := #[] for alt in alts do if let some alt' := expandMultiAlt? alt then altsNew := altsNew ++ alt' else altsNew := altsNew.push alt some <| inductionAlts.setArg 2 (mkNullNode altsNew) else none /-- Expand ``` syntax "induction " term,+ (" using " ident)? ("generalizing " (colGt term:max)+)? (inductionAlts)? : tactic ``` if `inductionAlts` has an alternative with multiple LHSs. -/ private def expandInduction? (induction : Syntax) : Option Syntax := do let optInductionAlts := induction[4] guard <| !optInductionAlts.isNone let inductionAlts' ← expandInductionAlts? optInductionAlts[0] return induction.setArg 4 (mkNullNode #[inductionAlts']) /-- Expand ``` syntax "cases " casesTarget,+ (" using " ident)? (inductionAlts)? : tactic ``` if `inductionAlts` has an alternative with multiple LHSs. -/ private def expandCases? (induction : Syntax) : Option Syntax := do let optInductionAlts := induction[3] guard <| !optInductionAlts.isNone let inductionAlts' ← expandInductionAlts? optInductionAlts[0] return induction.setArg 3 (mkNullNode #[inductionAlts']) /-- We may have at most one `| _ => ...` (wildcard alternative), and it must not set variable names. The idea is to make sure users do not write unstructured tactics. -/ private def checkAltsOfOptInductionAlts (optInductionAlts : Syntax) : TacticM Unit := unless optInductionAlts.isNone do let mut found := false for alt in getAltsOfInductionAlts optInductionAlts[0] do let n := getAltName alt if n == `_ then unless (getAltVarNames alt).isEmpty do throwErrorAt alt "wildcard alternative must not specify variable names" if found then throwErrorAt alt "more than one wildcard alternative '| _ => ...' used" found := true def getInductiveValFromMajor (major : Expr) : TacticM InductiveVal := liftMetaMAtMain fun mvarId => do let majorType ← inferType major let majorType ← whnf majorType matchConstInduct majorType.getAppFn (fun _ => Meta.throwTacticEx `induction mvarId m!"major premise type is not an inductive type {indentExpr majorType}") (fun val _ => pure val) -- `optElimId` is of the form `("using" ident)?` private def getElimNameInfo (optElimId : Syntax) (targets : Array Expr) (induction : Bool): TacticM ElimInfo := do if optElimId.isNone then if let some elimInfo ← getCustomEliminator? targets then return elimInfo unless targets.size == 1 do throwError "eliminator must be provided when multiple targets are used (use 'using <eliminator-name>'), and no default eliminator has been registered using attribute `[eliminator]`" let indVal ← getInductiveValFromMajor targets[0]! if induction && indVal.all.length != 1 then throwError "'induction' tactic does not support mutually inductive types, the eliminator '{mkRecName indVal.name}' has multiple motives" if induction && indVal.isNested then throwError "'induction' tactic does not support nested inductive types, the eliminator '{mkRecName indVal.name}' has multiple motives" let elimName := if induction then mkRecName indVal.name else mkCasesOnName indVal.name getElimInfo elimName else let elimId := optElimId[1] let elimName ← withRef elimId do resolveGlobalConstNoOverloadWithInfo elimId withRef elimId <| getElimInfo elimName private def shouldGeneralizeTarget (e : Expr) : MetaM Bool := do if let .fvar fvarId .. := e then return (← fvarId.getDecl).hasValue -- must generalize let-decls else return true private def generalizeTargets (exprs : Array Expr) : TacticM (Array Expr) := do if (← withMainContext <| exprs.anyM (shouldGeneralizeTarget Β·)) then liftMetaTacticAux fun mvarId => do let (fvarIds, mvarId) ← mvarId.generalize (exprs.map fun expr => { expr }) return (fvarIds.map mkFVar, [mvarId]) else return exprs @[builtinTactic Lean.Parser.Tactic.induction] def evalInduction : Tactic := fun stx => match expandInduction? stx with | some stxNew => withMacroExpansion stx stxNew <| evalTactic stxNew | _ => focus do let optInductionAlts := stx[4] let alts := getAltsOfOptInductionAlts optInductionAlts let targets ← withMainContext <| stx[1].getSepArgs.mapM (elabTerm Β· none) let targets ← generalizeTargets targets let elimInfo ← withMainContext <| getElimNameInfo stx[2] targets (induction := true) let mvarId ← getMainGoal -- save initial info before main goal is reassigned let initInfo ← mkTacticInfo (← getMCtx) (← getUnsolvedGoals) (← getRef) let tag ← mvarId.getTag mvarId.withContext do let targets ← addImplicitTargets elimInfo targets checkTargets targets let targetFVarIds := targets.map (Β·.fvarId!) let (n, mvarId) ← generalizeVars mvarId stx targets mvarId.withContext do let result ← withRef stx[1] do -- use target position as reference ElimApp.mkElimApp elimInfo targets tag trace[Elab.induction] "elimApp: {result.elimApp}" let elimArgs := result.elimApp.getAppArgs ElimApp.setMotiveArg mvarId elimArgs[elimInfo.motivePos]!.mvarId! targetFVarIds let optPreTac := getOptPreTacOfOptInductionAlts optInductionAlts mvarId.assign result.elimApp ElimApp.evalAlts elimInfo result.alts optPreTac alts initInfo (numGeneralized := n) (toClear := targetFVarIds) appendGoals result.others.toList where checkTargets (targets : Array Expr) : MetaM Unit := do let mut foundFVars : FVarIdSet := {} for target in targets do unless target.isFVar do throwError "index in target's type is not a variable (consider using the `cases` tactic instead){indentExpr target}" if foundFVars.contains target.fvarId! then throwError "target (or one of its indices) occurs more than once{indentExpr target}" def elabCasesTargets (targets : Array Syntax) : TacticM (Array Expr) := withMainContext do let args ← targets.mapM fun target => do let hName? := if target[0].isNone then none else some target[0][0].getId let expr ← elabTerm target[1] none return { expr, hName? : GeneralizeArg } if (← withMainContext <| args.anyM fun arg => shouldGeneralizeTarget arg.expr <||> pure arg.hName?.isSome) then liftMetaTacticAux fun mvarId => do let argsToGeneralize ← args.filterM fun arg => shouldGeneralizeTarget arg.expr <||> pure arg.hName?.isSome let (fvarIdsNew, mvarId) ← mvarId.generalize argsToGeneralize let mut result := #[] let mut j := 0 for arg in args do if (← shouldGeneralizeTarget arg.expr) || arg.hName?.isSome then result := result.push (mkFVar fvarIdsNew[j]!) j := j+1 else result := result.push arg.expr return (result, [mvarId]) else return args.map (Β·.expr) @[builtinTactic Lean.Parser.Tactic.cases] def evalCases : Tactic := fun stx => match expandCases? stx with | some stxNew => withMacroExpansion stx stxNew <| evalTactic stxNew | _ => focus do -- leading_parser nonReservedSymbol "cases " >> sepBy1 (group majorPremise) ", " >> usingRec >> optInductionAlts let targets ← elabCasesTargets stx[1].getSepArgs let optInductionAlts := stx[3] let optPreTac := getOptPreTacOfOptInductionAlts optInductionAlts let alts := getAltsOfOptInductionAlts optInductionAlts let targetRef := stx[1] let elimInfo ← withMainContext <| getElimNameInfo stx[2] targets (induction := false) let mvarId ← getMainGoal -- save initial info before main goal is reassigned let initInfo ← mkTacticInfo (← getMCtx) (← getUnsolvedGoals) (← getRef) let tag ← mvarId.getTag mvarId.withContext do let targets ← addImplicitTargets elimInfo targets let result ← withRef targetRef <| ElimApp.mkElimApp elimInfo targets tag let elimArgs := result.elimApp.getAppArgs let targets ← elimInfo.targetsPos.mapM fun i => instantiateMVars elimArgs[i]! let motiveType ← inferType elimArgs[elimInfo.motivePos]! let mvarId ← generalizeTargetsEq mvarId motiveType targets let (targetsNew, mvarId) ← mvarId.introN targets.size mvarId.withContext do ElimApp.setMotiveArg mvarId elimArgs[elimInfo.motivePos]!.mvarId! targetsNew mvarId.assign result.elimApp ElimApp.evalAlts elimInfo result.alts optPreTac alts initInfo (numEqs := targets.size) (toClear := targetsNew) builtin_initialize registerTraceClass `Elab.cases registerTraceClass `Elab.induction end Lean.Elab.Tactic
842bba12d7e75ee6a49bf1bc72dfc6d1026729d9
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/combinatorics/partition/basic.lean
137ef522f907c8dcd0706740017615ecdf9c2d4c
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
72,500
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland -/ import data.fintype tactic.squeeze tactic.fin_cases import data.finset_transfer data.unique_element namespace combinatorics /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This file sets up some basic theory of partitions of finite sets. It relies on some theory of finite sets, and we need the line <span class="code">import data.fintype</span> to load that theory from the <span class="code">mathlib</span> library. We also need some auxiliary results from the files <span class="path">finset_transfer.lean</span>, <span class="path">unique_element.lean</span>, so we have import statements for these as well. Note, however, that these three files are not in a standard library; they were written in parallel with this file and live in the same directory. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ universes u v /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Universes are a mechanism used in Lean and similar systems to avoid Russell-type paradoxes. Lean will usually handle all related bookkeeping by itself, so it is rarely necessary for users to think about the details. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ variable {Ξ± : Type u} /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We want to consider partitions of a finite set, which will be called Ξ±. In Lean, this will be represented as a type Ξ± together with some extra data to be discussed shortly. The line `variable {Ξ± : Type u}` declares that for the rest of this file, the symbol Ξ± will represent a type. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ variable [fintype Ξ±] /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This line essentially declares that Ξ± is finite. In more detail, it declares an assumption that we have a term of the type `fintype Ξ±`, which is defined in <span class="mathlib">data/fintype</span>. It would be possible to give this a name, with a declaration like <span class="code">variable [P : fintype Ξ±]</span>. However, we will not need to refer to it explicitly, so we omit the name. Instead, we put the declaration in square brackets, indicating that Lean should treat the term as a typeclass instance, an supply it as an implicit argument in any situation where it is required. In a little more detail, a term of type `fintype Ξ±` consists of an equivalence class of lists of elements of Ξ±, together with a proof that no two terms in the list are the same, and a proof that every element of Ξ± appears in the list. Here lists are considered equivalent if one is a permutation of the other. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ variable [decidable_eq Ξ±] /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We also assume that we are given a term of the type <span class="code">decidable_eq Ξ±</span>, which is a procedure that decides unambigously whether two elements of Ξ± are equal. Such procedures can be defined explicitly for many types, including β„•, β„€ and β„š, and types constructed from those by finitary means such as <span class="code">list (list β„š)</span>. Below we will explain the framework in more detail when we deduce that the set of partitions itself has decidable equality. The situation is different for ℝ, for example: if two reals are presented as Cauchy sequences then we can only conclude that they are equal by examinining infinitely many terms, and there is no computational procedure for that. However, we could include the line `import classical` at the top of the file. This would cause Lean to allow classical non-constructive logic, and gives us an axiomatically posited term of class `decidable_eq Ξ±` for every type `Ξ±`, but without the ability to compute with it explicitly. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ open finset fintype /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ By opening the namespaces `finset` and `fintype`, we allow ourselves to use definitions and theorems from the files <span class="mathlib">data/finset.lean</span> <span class="mathlib">data/fintype.lean</span> without a prefix (e.g. `subset_def` rather than `finset.subset_def`). Strictly speaking, this operation works on definitions in the `finset` namespace rather than file <span class="mathlib">data/finset.lean</span>. In that file there are a few lines before `namespace finset` and a few lines after `end finset` that are not covered. Also, the namespace is reopened and extra definitions added in various other mathlib files such as <span class="mathlib">algebra/big_operators.lean</span>. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ def is_partition (block : Ξ± β†’ finset Ξ±) := (βˆ€ a : Ξ±, a ∈ block a) ∧ (βˆ€ a1 a2 : Ξ±, a1 ∈ block a2 β†’ block a1 = block a2) /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now define what it means for someting to be a partition. There are several different definitions that one could use. Below we will have some theorems showing that some other approaches are equivalent to the one that we have preferred. We have chosen to encode a partition as a map that sends each element `a` to the block of the partition that contains `a`. We have also chosen to encode the required properties as a single axiom which is a logical conjunction of two clauses. There would be some advantages and some disadvantages if we replaced this with two separate axioms. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ instance decidable_partition (block : Ξ± β†’ finset Ξ±) : decidable (is_partition block) := by dsimp[is_partition]; apply_instance /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We next need a decision procedure to decide whether the proposition `is_partition b` is true, for a given map `b : Ξ± β†’ finset Ξ±`. Lean can deal with this automatically using the assumed decision procedure for equality on Ξ± and a range of previously established results about decidability for finite sets, maps with finite domain and so on; this is achieved by the `apply_instance` tactic. However, we need to use `dsimp` to unfold the definition of `is_partition` before we can use `apply_instance`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ variable (Ξ±) /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For most definitions and theorems in this file, it is appropriate to treat the underlying type `Ξ±` as an implicit parameter, because it can be inferred from other ingredients. However, if we just want to refer to the type of all partitions of `Ξ±` then there are no other ingredients so we need to switch to treating `Ξ±` as an explicit argument. That is achieved by the current line. We will reverse this a few lines down. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ structure partition := (block : Ξ± β†’ finset Ξ±) (ispart : is_partition block) /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now define a partition of `Ξ±` to be a structure consisting of a map `Ξ± β†’ finset Ξ±` together with a proof that it has the required properties. If `p` is a partition, then the map and the proof can be referred to as `p.block` and `p.ispart` respectively. Conversely, if we have constructed an appropriate map `b` and proof `h` then we can package them as a structure using the notation `⟨b,h⟩` or `{block := b, ispart := p}`. Note that the first version uses angle brackets, not ordinary parentheses; they can be entered in VSCode as `\<` and `\>`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ variable {Ξ±} /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We revert to treating `Ξ±` as an implicit argument. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ namespace partition /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now start a new namespace. The very last line of this file is `end partition`, so everything up until then is in the `partition` namespace. Thus, in any other file the definition below will need to be referred to as `partition.block_set` rather than just `block_set`, unless that file also includes the directive `open partition`. However, there is a further wrinkle to this. If `p` is a term of type `partition Ξ±`, then we can use the "object notation" `p.block_set` as a synonym for `block_set p` or `partition.block_set p`. This works for any function in which `p` is the first non-implicit argument, and in this context, the namespace prefix is never required. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ def block_set (p : partition Ξ±) : finset (finset Ξ±) := image p.block univ /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Given a partition `p`, we define the associated block set to be the image of the associated map `Ξ± β†’ finset Ξ±`, so the block set is an element of `finset (finset Ξ±)`. For this to work properly, we need to start with the finset of all elements of `Ξ±`, which is called `univ`. This implicitly refers to the term of type `fintype Ξ±` which we posited near the top of this file. Lean finds this by the mechanism of typeclass inference, so we do not need to refer to it explicitly. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ def block_type (p : partition Ξ±) : Type* := { b : finset Ξ± // b ∈ block_set p } /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The block set `B` (as defined above) is of type `finset (finset Ξ±)`, which means that it is a permutation-equivalence class of lists with entries in `finset Ξ±`. In particular, it is not a type, so it is not meaningful to write `b : B` or to consider maps `B β†’ Ξ³` for some other type `Ξ³`. It is meaningful to write `b ∈ B` (provided that `b` is of type `finset Ξ±`), but this is not a primitive notion; it is defined in <span class="mathlib">data/finset.lean</span> in terms of other ingredients. The current line defines a type that is a counterpart of `B`. The relevant framework is described briefly in <span class="tpil">Section 7.3</span>. Note the use of `//` where in traditional notation we would have a colon or vertical bar; those symbols are already used for too many other things. If `b` is of type `p.block_type`, then `b.val` is of type `finset Ξ±`, and `b.property` is a proof that `b.val` lies in `p.block_set`. Conversely, if we have a subset `b0` and a proof `h0` that `b0` lies in the block set, then we can use the notation `⟨b0,h0⟩` to construct a term of type `p.block_type`. Note that this uses angle brackets, not ordinary parentheses; they can be entered in VSCode as `\<` and `\>`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ def block_alt (p : partition Ξ±) (a : Ξ±) : p.block_type := ⟨p.block a,mem_image_of_mem p.block (mem_univ a)⟩ /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now define a function which takes as input a partition `p` and an element `a`, and returns the block containing `a` as a term of type `p.block_type` rather than just a finite subset of `Ξ±`. For this, we need to package the finite subset `b := p.block a` together with a proof that `b` lies in `p.block_set`. Now `p.block_set` was defined as the image of `univ` under the map `p.block`. The fact that every element lies in `univ` is recorded as the theorem `mem_univ` from <span class="mathlib">data/fintype.lean</span>. (Of course the proof of `mem_univ` just consists of extracting part of the definition of `fintype Ξ±`; the real work is done by the typeclass inference mechanism that retrieves our posited instance of `fintype Ξ±`.) The theorem `mem_univ_of_mem` just refers back to the definition of image to conclude that `p.block a` lies in the image, as required. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ @[simp] lemma block_alt_val (p : partition Ξ±) (a : Ξ±) : (p.block_alt a).val = p.block a := rfl /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Clearly, the underlying finite set `(p.block_alt a).val` of `p.block_alt a` is just the same as `p.block a`. It is convenient to record this fact as a lemma and mark it with the attribute `@[simp]`. This ensures that when we use the `simp` tactic in subsequent proofs, `(p.block_alt a).val` will be replaced by `p.block a` whenever it occurs. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma block_eq_of_veq {p : partition Ξ±} {b0 b1 : p.block_type} (e : b0.val = b1.val) : b0 = b1 := subtype.eq e /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now record the fact that any two terms of type `p.block_type` are equal if their underlying sets are equal. This is because Lean implements the principle of "proof irrelevance": it treats any two proofs of the same proposition as equal. Our proof simply refers to the more general fact `subtype.eq` from <span class="library">init/data/subtype/basic.lean</span>. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma block_veq_of_eq {p : partition Ξ±} {b0 b1 : p.block_type} (e : b0 = b1) : b0.val = b1.val := congr_arg _ e /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This is the converse of the previous lemma. We are given a proof `e` of `b0 = b1`, and we need to deduce that `f b0 = f b1`, where `f` is the function sending `b` to `b.val`. For this we need `congr_arg f e`. If we wanted to name the relevant function `f` explicitly we could write `Ξ» b : p.block_type, b.val`, but it also works to just write `_` and let Lean work it out. This is certainly shorter and perhaps more natural. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma block_alt_eq {p : partition Ξ±} {a0 a1 : Ξ±} : p.block a0 = p.block a1 ↔ p.block_alt a0 = p.block_alt a1 := begin split; intro e, {exact @block_eq_of_veq Ξ± _ _ p (p.block_alt a0) (p.block_alt a1) e}, {exact @block_veq_of_eq Ξ± _ _ p (p.block_alt a0) (p.block_alt a1) e} end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For convenience, we combine the last two results into a bidirectional implication. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma mem_block_set {p : partition Ξ±} {b : finset Ξ±} : b ∈ p.block_set ↔ βˆƒ a, p.block a = b := by simp[block_set] /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now record the obvious criterion for a set `b` to lie in `p.block_set`. We prove that this criterion is correct using the `simp` tactic. This tactic tries to apply all the theorems that Lean knows that have been marked with the `@[simp]` attribute, together with any extra things that we put in square brackets as arguments for the tactic. Often the extra arguments are names of theorems. Sometimes (as here) they are the names of definitions that should be unfolded during simplification. There are numerous options for modifying the behaviour of `simp`, which are discussed in <span class="tpil">Section 5.7</span>. Note that this tactic is mildly deprecated in all cases, because it causes Lean to search through a large space of results that are mostly irrelevant. It is also more strongly deprecated in cases where it does not finish the job of proving the current goal. This is because the set of `simp` lemmas changes frequently as `mathlib` expands, so the exact amount of progress that `simp` can make is volatile. If you write additional steps on the assumption that `simp` has reached a particular point, then your proof may get broken if `simp` later learns to make more progress. One possibility is to add `import tactic.squeeze` at the top of the file, and then replace `simp` by `squeeze_simp`. This will print a message reporting a modified `simp` command that specifies the results that are actually needed. For example, in this proof `squeeze_simp` suggests replacing `simp[block_set]` by `simp only [block_set, finset.mem_univ, iff_self, exists_prop_of_true, finset.mem_image]`, which is more efficient. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma block_not_empty {p : partition Ξ±} {b : finset Ξ±} : b ∈ p.block_set β†’ b β‰  βˆ… := begin /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We state the lemma that all blocks are nonempty. Note that the only explicit argument is the proof that `b ∈ p.block_set`, because `p` and `b` can be inferred from this. The symbol βˆ… can be entered as \empty or \emptyset. The thing that it refers to here can be named more explicitly as `@finset.empty Ξ±`. However, this is an indirect reference via the typeclass `has_emptyc`. This is a fairly common pattern. Whenever a type `Ξ±` has an element that deserves to be called `0`, this will be encoded as an instance of the typeclass `has_zero Ξ±`. Similarly, elements that deserve to be called `1` are encoded as instances of `has_one Ξ±`, and elements that deserve to be called `βˆ…` are encoded as instances of `has_emptyc Ξ±`. (I am not sure why it is `has_emptyc` rather than `has_empty`.) There is a separate framework for not-necessarily-finite subsets of types, which has a separate definition of `βˆ…`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ intros b_in_blocks b_empty, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We assume given proofs that `b ∈ p.block_set` and that `b = βˆ…`; the goal then becomes to derive a contradiction, or in other words, to give a proof of `false`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ cases (mem_block_set.mp b_in_blocks) with a a_block, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Above we proved a result called `mem_block_set`, which is a bidirectional implication. The suffix `.mp` (for "modus ponens") picks out the left-to-right implication. We pass this our proof that `b ∈ p.block_set` to get a proof that `βˆƒ a, p.block a = b`. The `cases` tactic then introduces names for the element `a` and the proof that `p.block a = b`. It is important to note some restrictions on this kind of use of the `cases` tactic. At the moment we are trying to prove a proposition, not to define a function. Lean implements the principle of "proof irrelevance", so any two proofs of the same proposition are regarded as equal. Because of this, Lean does not worry that what we are doing might depend on the choice of `a`. However, if we tried to do this while defining a function to a `Type` rather than a `Prop` we would get an error message as follows: `induction tactic failed, recursor 'Exists.dcases_on' can only eliminate into Prop`. We postpone any discussion of what to do about this. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ have block_empty : p.block a = βˆ… := a_block.trans b_empty, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Given proofs `P : x = y` and `Q : y = z`, we can combine them to get a proof of `x = z` using the notation `eq.trans P Q` or `P.trans Q`. Here we use the latter to produce a proof that `p.block a = βˆ…`. (For more complicated strings of equalities one would use the `calc` tactic, which would produce terms like `P.trans Q` automatically, but here it is easy to just write the proof term explicitly.) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ have a_in_block : a ∈ p.block a := p.ispart.left a, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Recall that `p` is a structure consisting of a map `p.block : Ξ± β†’ finset Ξ±` together with a proof `p.ispart` of the required property of this map. Here `p.ispart` is a logical conjunction of two propositions, and we can select the first of these with the notation `p.ispart.left` or `p.ispart.1`. This is a universally quantified proposition like `βˆ€ x, x ∈ p.block x`. We specialise to `x = a` by simply supplying `a` as an argument. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ rw[block_empty] at a_in_block, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ At this point we have a proof `a_in_block : a ∈ p.block a` and also a proof `block_empty : p.block a = βˆ…`. The `rw` tactic (which can also be written as `rewrite`) allows us to convert `a_in_block` to a proof that `a ∈ βˆ…`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ exact not_mem_empty a a_in_block, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The library theorem `not_mem_empty` (from <span class="mathlib">data/finset.lean</span>) proves that the empty set has no elements. In more detail, it accepts as explicit arguments a term `x` and a proof that `x ∈ βˆ…`, and produces a proof of `false`. We can thus pass `a` and `a_in_block` to `not_mem_empty` to produce the required contradiction. In even more detail, the current goal is `false` and the term `not_mem_empty a a_in_block` has type false. If we were in term mode we could just write `not_mem_empty a a_in_block` to satisfy the goal. However, we are in a `begin ... end` block and thus in tactic mode, so we need to use the `exact` tactic rather than just writing the relevant term. Note also that we have an extra comma at the end of the line. This is not required, but it causes Lean to give an explicit message saying "no goals", which can be comforting. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma block_val_eq_of_mem {p : partition Ξ±} {b : p.block_type} {a : Ξ±} (a_in_b : a ∈ b.val) : b.val = p.block a := begin cases mem_block_set.mp b.property with a0 e0, rw[← e0] at a_in_b ⊒ , exact (p.ispart.right a a0 a_in_b).symm, end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If `b` is a block of `p` and `a` lies in `b` then `b` must be equal to `p.block a`. <br/><br/> The term `h := mem_block_set.mp b.property` gives a proof that there exists `a0` and `e0 : p.block a0 = b.val`, and one could hope to use `cases h with a0 e0` to get a choice of `a0` and `e0`. For reasons that are not clear to me, this does not work well if we introduce `h` explicitly. However, if we instead write `cases mem_block_set.mp b.property with a0 e0`, then things work as expected. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma block_eq_of_mem {p : partition Ξ±} {b : p.block_type} {a : Ξ±} (a_in_b : a ∈ b.val) : b = p.block_alt a := block_eq_of_veq (block_val_eq_of_mem a_in_b) /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This lemma is deduced from the previous one and is more or less the same, except that it proves an equality in `p.block_type` rather than `finset Ξ±`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ def rank (p : partition Ξ±) : β„• := card (p.block_set) /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The rank of a partition is the number of blocks. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ instance partition_repr [has_repr Ξ±] : has_repr (partition Ξ±) := ⟨λ p, has_repr.repr p.block_set⟩ /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This definition tells Lean how to produce a string representation of a partition. For example, the string `"{{1,4},{2,3}}"` represents a partition of `{1,2,3,4}` in an obvious way. For this to work, we obviously need to know how to generate string representations for individual elements of `Ξ±`. Assuming that, Lean already knows how to print finite subsets of `Ξ±`, and finite subsets of finite subsets of `Ξ±`, so it knows how to print the set of blocks of a partition. This definition tells Lean that it should use that as the string representation of the partition itself. <br/><br/> All of this is handled by the typeclass mechanism. The word `instance` is essentially the same as `def` except that it tells Lean that the thing we are defining is a typeclass instance that should be remembered and used without explicit mention in various other places. Next, `partition_repr` is the name of the instance that we are about to define. We could have just left it out and defined an anonymous instance; that is a fairly common pattern. Then `[has_repr Ξ±]` indicates that our definition will depend on an argument of type `has_repr Ξ±`, which is essentially the same as a function `Ξ± β†’ string`. We have left this argument unnamed, and enclosed it in square brackets to indicate that the argument will not be given explicitly and should be found by typeclass inference. We then declare that we are going to define a term of type `has_repr (partition Ξ±)`. If `u0` is our function `Ξ± β†’ string`, and `u1` is the resulting function `finset (finset Ξ±) β†’ string`, then the required function `partition Ξ± β†’ string` is `u2 := Ξ» p,u1 p.block_set`. However, there is a little extra wrapping and unwrapping to worry about. A term of type `has_repr (partition Ξ±)` is not actually a function `partition Ξ± β†’ string`, but rather a structure with one member (called `repr`) which is a function `partition Ξ± β†’ string`. We can turn our function into a structure just by enclosing it in angle brackets (entered as \< and \>). Also, we did not name the map `u0` or discuss exactly how `u1` is defined in terms of `u0`. It turns out that we can just write `has_repr.repr` instead of `u1` and everything works out automatically. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma mem_block (p : partition Ξ±) (a : Ξ±) : a ∈ p.block a := (p.ispart.left a) /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For convenience, we make a lemma that just restates the first half of the definition of what it means to be a partition. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma block_symm {p : partition Ξ±} {a0 a1 : Ξ±} (e : a0 ∈ p.block a1) : a1 ∈ p.block a0 := /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If `a0` lies in `a1`'s block, then `a1` lies in `a0`'s block. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ begin let h0 : p.block a0 = p.block a1 := p.ispart.right a0 a1 e, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In fact the two blocks are the same, by the second half of the definition of what it means to be a partition. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ let h1 : a1 ∈ p.block a1 := p.ispart.left a1, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `a1` lies in its own block by the first half of the definition. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ exact h0.symm.subst h1, end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Equation `h0` says that `p.block a0 = p.block a1`, and `h0.symm` is notation for the reversed equation `p.block a1 = p.block a0`. We can substitute this in fact `h1` using the notation `h0.symm.subst h1` to get the desired conclusion `a1 ∈ p.part a0`. However, we are in a `begin ... end` block and so in tactic mode, so we need to use the keyword `exact` rather than just writing `h0.symm.subst h1`. <br/><br/> (This last sentence is an oversimplification, because there are various ways in which we can switch back into term mode inside a `begin ... end` block. For example, we could write `lemma russell : 1 + 1 = 2 := <br/> begin <br/> let n : β„• := 42,<br/> refl<br/> end`<br/> Before `let n : β„• :=` we are in tactic mode. After ` := ` we are in term mode, and our current goal is to produce a term of type `β„•`. We can do that directly, by entering `42` or `2 * 3 + 6` or `min 10 11` or some other formula. Alternatively, if we needed a complex combination of logical steps to produce the required natural number, then we could write `let n : β„• := begin ... end,` and then replace the `...` by a sequence of tactics. As this illustrates, we can have fragments of term mode and fragments of tactic mode nested arbitrarily deep inside each other. ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ @[simp] theorem eq_of_block_eq : βˆ€ {p1 p2 : partition Ξ±}, p1 = p2 ↔ p1.block = p2.block | ⟨_,_⟩ ⟨_,_⟩ := by simp /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now prove that two partitions `p1` and `p2` are equal iff the maps `p1.block, p2.block : Ξ± β†’ finset Ξ±` are equal. This is just because `p1.ispart` and `p2.ispart` are then proofs of the same proposition and so automatically count as being equal, by proof irrelevance. <br/><br/> To get Lean to accept this argument, we must persuade it to take the right point of view. Rather than thinking of `p1` and `p2` as being packages that it could potentially unwrap to reveal `p1.block`, `p2.block`, `p1.ispart` and `p2.ispart`, it must suppose that it has been given these four ingredients and used them to build `p1` and `p2`. We could do this with a proof like `begin rcases p1, rcases p2, simp, end`, but here we take a slightly different approach, using pattern matching. For this to work we need to formulate the statement with an explicit `βˆ€` after the colon, rather than placing arguments before the colon as we have done previously. Note also that there is no `:=` directly after the statement. Instead, we would typically have a number of lines consisting of a vertical bar, a pattern that might or might not be matched by the arguments, then `:=` followed by a proof that works when the arguments match the relevant pattern. Many examples are given in <span class="tpil">Section 8.1</span>. The present example is rather degenerate: there is only one pattern, and the point is just that matching the pattern forces Lean to think of `p1` and `p2` as being built up from their two constituents. Given this, the `simp` tactic is enough to finish the proof. <br/><br/> Note also that we have used the annotation `@[simp]` to give this theorem the `[simp]` attribute, so that the `simp` tactic will use it by default. Attributes are discussed in more detail in <span class="tpil">Section 6.4</span>. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ @[extensionality] theorem ext {p1 p2 : partition Ξ±} (e : βˆ€ a, p1.block a = p2.block a) : p1 = p2 := eq_of_block_eq.mpr (funext e) /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This theorem is closely related to the previous one, but different in some details. It is marked with the `[extensionality]` attribute so that it will be used by the `ext` tactic, and the details are arranged so as to make that work smoothly. The previous theorem was a bidirectional implication, but here we pick out one direction only. Also, the hypothesis is that the maps `p1.block, p2.block : Ξ± β†’ finset Ξ±` are pointwise equal, so we use the theorem `funext` to deduce that `p1.block = p2.block` as functions. (There are some subtle foundational issues related to `funext` as discussed in <span class="tpil">Section 11.3</span>.) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ instance has_decidable_eq : decidable_eq (partition Ξ±) := Ξ» p1 p2, decidable_of_iff (p1.block = p2.block) eq_of_block_eq.symm /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We are now ready to provide a decision procedure for equality of partitions. In more detail, we need to define a function that accepts two partitions `p1` and `p2` as arguments, and returns a decision procedure for the proposition `p1 = p2`. We have assumed that we are given such a decision procedure for `Ξ±` itself, and the library defines a rich set of rules for deriving decision procedures for other types defined in terms of `Ξ±`. In particular, these rules suffice to give a decision procedure for a proposition of the form `p1.block = p2.block` in `Ξ± β†’ finset Ξ±`. The theorem `eq_of_block_eq` tells us that `p1 = p2` is equivalent to `p1.block = p2.block` so we can use the function `decidable_of_iff` to convert a decision procedure for the latter to a decision procedure for the former. In the conclusion of `eq_of_block_eq`, the implications are written the opposite way around from what is expected by `decidable_of_iff`, so we have two write `eq_of_block_eq.symm` to reverse them. <br/><br/> Note that in the expression we have written here, both `eq_of_block_eq` and `decidable_of_iff` have implicit arguments, and one of the implicit arguments for `decidable_of_iff` is an equality decision procedure for `Ξ± β†’ finset Ξ±` which is provided by a complex chain of typeclass inferences, so there is quite a lot going on in the background. <br/><br/> The approach that we have taken here is not in fact the preferred one. When we gave the original definition of `partition Ξ±`, we could have prefixed it with the annotation `@[derive decidable_eq]`. Lean would then have silently generated the required decision procedure in the background. However, we decided to avoid explaining all that at the relevant time. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma block_set_rep (p : partition Ξ±) : βˆ€ (b ∈ p.block_set), βˆ€ (a ∈ b), b = p.block a := /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If `b` is a block and `a ∈ b` then `b` is `a`'s block. Note that there is a little notational magic going on with the fragment `βˆ€ (b ∈ p.block_set)`: we are usually only allowed things like `βˆ€ x : X`, but the above fragment is automatically translated to `βˆ€ (b : finset Ξ±), βˆ€ (h : b ∈ p.block_set)`. Similarly, `βˆ€ (a ∈ b)` becomes `βˆ€ (a ∢ Ξ±), βˆ€ (k : a ∈ b)`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ begin intros b b_in_block_set a a_in_b, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Because of the notational magic mentioned above, applying the `intros` tactic to the first quantifier gives us two arguments: a finite set `b` and a proof that `b` lies in the block set. Similarly, the second quantifier gives a term `a` and a proof that it lies in `b`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ rcases (mem_image.mp b_in_block_set) with ⟨a1,a1_in_univ,a1_block⟩, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here `b_in_block_set` says that `b` is in the image of the map `p.block`, and we can apply the theorem `mem_image.mp` to unwrap the definition of the image and conclude that there exists `a1` with `b = p.block a1`. We can then use the `rcases` tactic to give us `a1` together with a proof that `p.block a1 = b`. <br/><br/> There is a slight wrinkle here. Images are defined for finite sets, which need not fill the whole domain type of the function whose image we consider. In the current case we define the block set to be the image of the universal finite subset of `Ξ±`, given by the posited `fintype` instance for `Ξ±`. Because of this, the `rcases` tactic also gives us a proof of the uninteresting fact that `a1 ∈ univ` as well as the interesting fact that `p.block a1 = b`. <br/><br/> As we have discussed previously, this kind of use of the `rcases` tactic is only valid when we are proving a theorem rather than defining a function, so that dependence on the choice of `a1` is harmless. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ have : a ∈ p.block a1 := a1_block.symm.subst a_in_b, exact (eq.subst a1_block (p.ispart.right a a1 this).symm) end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now have `a ∈ b = p.block a1` which gives `p.block a = p.block a1` by the partition axiom, and we can put this together to get `b = p.block a`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ instance block_set_deceq (p : partition Ξ±) : decidable_eq p.block_type := by apply_instance /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We need a decision procedure for equality in the type `p.block_type`. The `apply_instance` tactic is clever enough to deal with this automatically. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ instance block_set_fintype (p : partition Ξ±) : fintype p.block_type := { elems := attach p.block_set, complete := mem_attach p.block_set } /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now construct an instance of `fintype p.block_type`, showing that `p.block_type` is finite. This is completely formal because `p.block_type` was constructed by starting with the finite set `p.block_set` and converting it to a separate type. The relevant function `finset.attach` and theorem `finset.mem_attach` come from <span class="mathlib">data/finset.lean</span>. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma block_type_card (p : partition Ξ±) : fintype.card p.block_type = p.rank := @finset.card_attach (finset Ξ±) p.block_set /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We defined the rank to be the cardinality of `p.block_set`. Here we just record that that is the same as the cardinality of `p.block_type`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ theorem eq_of_block_set_eq {p1 p2 : partition Ξ±} : ((p1 = p2) ↔ (p1.block_set = p2.block_set)) := begin /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now show that two partitions are equal iff they have the same block set. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ split, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Our goal is to prove a bidirectional implication. The `split` tactic converts this to two goals, one for each direction. Below we will deal with the first goal in one set of curly brackets, and with the second in another set of curly brackets. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ { intro e, rw[e] }, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The left-to-right direction is trivial: the `intro` tactic gives us a term `e : p1 = p2` and leaves us with the goal of proving `p1.block_set = p2.block_set`, which we do by rewriting with `e`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ { intro block_set_eq, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We assume given a proof that `p1.block_set = p2.block_set`; the goal is now to prove that `p1 = p2`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ apply eq_of_block_eq.mpr, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We apply the right-to-left direction of the theorem `eq_of_block_eq` that we proved earlier. The `apply` tactic works backwards from the goal rather than forwards from the facts that we have already established or assumed. In the present case, it changes the goal so that we now need to prove that `p1.block = p2.block`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ funext a, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Our goal is to prove that the functions `p1.block` and `p2.block` are equal. It will suffice to show that `p1.block a = p2.block a` for all `a`, and the tactic `funext a` changes the goal in this way. (We could just have written `funext`, but then the arbitrary element of `Ξ±` would be called `x` rather than `a`, which is less natural here.) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ let b1 := p1.block a, have b1_in_block_set : b1 ∈ p1.block_set := (mem_image_of_mem p1.block (mem_univ a)), /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We define `b1` to be the block of `a` with respect to `p1`, and we prove that this lies in the block set for `p1`. The proof uses the theorem `mem_univ` (saying that everything is in the universal subset of `Ξ±`) and the theorem `mem_image_of_mem` (saying that images behave in the way that they are supposed to). ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ rw[block_set_eq] at b1_in_block_set, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We have seen that `b1` is in the block set for `p1`, and we now rewrite this fact using the assumed equality `p1.block_set = p2.block_set`. The same name `b1_in_block_set` now refers to a proof that `b1 ∈ p2.block_set`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ exact block_set_rep p2 b1 b1_in_block_set a (p1.mem_block a) } end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now want to use the lemma `block_set_rep` to prove our goal that `p1.block a = p2.block a`. This lemma needs five arguments and we have the first four of them to hand. The last argument needs to be a proof that `a ∈ p1.block a`, and this is supplied by the theorem `mem_block` that we proved earlier. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ def of_block_set (blocks : finset (finset Ξ±)) (no_empty_blocks : βˆ€ b ∈ blocks, b β‰  βˆ…) (unique_block : βˆ€ a : Ξ±, βˆƒ! b, b ∈ blocks ∧ a ∈ b) : { p : partition Ξ± // p.block_set = blocks } := /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now reconcile our approach to partitions with an obvious alternative approach. Suppose that `blocks` is a finite set of finite subsets of `Ξ±`, and we have proofs that none of the sets in `blocks` is empty, and that each element `a : Ξ±` lies in precisely on of the sets in `blocks`. It is then fairly clear that there is a partition whose block set is `blocks`. The present function is a Lean implementation of this construction. In principle we could define a function that produces the relevant partition, and then separately prove a theorem showing that the block set of this partition is the family `blocks` that we started with. However, if we proceded in that way, then the definition and the theorem would share a substantial amount of logic, which would be inefficient and inelegant. It is better to do what we do here and define a function that returns a term `x` of type `{ p : partition Ξ± // p.block_set = blocks }`, so that `x.val` is a partition and `x.property` is a proof that it has the expected block set. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ begin let blocks_for : βˆ€ (a : Ξ±), finset (finset Ξ± ) := Ξ» a, blocks.filter (Ξ» b, a ∈ b), /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We define `blocks_for a` to be the set of blocks that contain `a`. Of course, our assumptions mean that there is precisely one such block. Our definition uses the function `finset.filter`, which takes a finite set and a decidable predicate, and returns the finite subset where that predicate is satisfied. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ let block_aux : βˆ€ (a : Ξ±), { b : finset Ξ± // blocks_for a = finset.singleton b } := Ξ» a , finset.witness blocks (Ξ» b, a ∈ b) (unique_block a), /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now use the function `finset.witness`. Note that this is not in the `mathlib` library, but is taken from an auxiliary file `unique_element.lean` that was written together with the present file. Given a finite set `s`, a decidable predicate `p`, and a proof that there is a unique element satisfying `p`, the function `finset.witness` returns that element together with a proof of its key property. <br/><br/> This is a bit more subtle than it appears. In the absence of finiteness and decidability, a proof `h` of `βˆƒ! x, p x` is not enough to compute the relevant value of `x`. The reason is essentially that Lean implements proof irrelevance, so we cannot extract any of the information that went into the proof of `βˆƒ! x, p x`. If we do not care about computability then we can write `import classical` and then use `classical.some h` to produce `x`. Alternatively, if we have finiteness and decidability then we can produce `x` in a computable way by walking through the finite list of possibilities for `x` and checking whether each of them has property `p`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ let block : Ξ± β†’ finset Ξ± := Ξ» a, (block_aux a).val, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `block_aux a` is a structure consisting of a finite subset of `Ξ±` together with a proof that it has certain properties. To define the block map of the partition that we are trying to construct, we just use the finite subset and ignore the proof. However, we will use the proof later when we verify that we have constructed a partition and that it has the expected block set. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ let ispart : is_partition block := begin /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now start to construct a proof that our block map has the properties required for a partition. This is a complex proof which we will do mostly in tactic mode, so we start by opening a `begin ... end` block. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ split, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Our goal is to prove `is_partition blocks`, which is by definition a conjunction of two clauses. The `split` tactic makes these two clauses into separate goals. <br/><br/> Note that `is_partition blocks` is not visibly a conjunction, but the `split` tactic is clever enough to unwind the definitions until that form becomes apparent. One might prefer to start with `dsimp[is_partition]`. That would apply the definition of `is_partition` to give a an explicit conjunction, to which we could apply the `split` tactic. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ {intro a, let w := block_aux a, have h : w.val ∈ finset.singleton w.val := mem_singleton_self _, rw[← w.property] at h, exact (mem_filter.mp h).right }, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In this set of curly brackets, we prove the first clause, that `a ∈ block a` for all `a`. We start by defining `w := block_aux a`, so that `block a = w.val`, and `w.property` is a proof that the set of blocks containing `a` is `{w.val}`. The theorem `mem_singleton_self` says that `w.val ∈ {w.val}`. We rewrite this fact using the equation `w.property`. We want to replace the right hand side of `w.property` by the left hand side, so the appropriate syntax is `rw[← w.property]` or `rw[w.property.symm]` rather than `rw[w.property]`. (The arrow can be entered as `\left` or just `\l`.) After this rewrite, we know that `w.val` lies in `blocks_for a`, which was defined using `finset.filter`. The theorem `mem_filter.mp` tells us that `finset.filter` behaves in the expected way, so we can use it to deduce that `w.val ∈ blocks ∧ a ∈ w.val`. We can use the notation `(mem_filter.mp h).right` to extract the second half of this conjunction, and this satisfies our goal. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ {intros a1 a2, let w1 := block_aux a1, let w2 := block_aux a2, let b1 := w1.val, let b2 := w2.val, have b1_in_B1 : b1 ∈ finset.singleton b1 := mem_singleton.mpr rfl, have b2_in_B2 : b2 ∈ finset.singleton b2 := mem_singleton.mpr rfl, rw[← w1.property] at b1_in_B1, rw[← w2.property] at b2_in_B2, have b1_in_blocks : b1 ∈ blocks := (mem_filter.mp b1_in_B1).1, have b2_in_blocks : b2 ∈ blocks := (mem_filter.mp b2_in_B2).1, have a1_in_b1 : a1 ∈ b1 := (mem_filter.mp b1_in_B1).2, have a2_in_b2 : a2 ∈ b2 := (mem_filter.mp b2_in_B2).2, intro a1_in_b2, have b2_in_B1 : b2 ∈ blocks_for a1 := mem_filter.mpr ⟨ b2_in_blocks , a1_in_b2 ⟩, rw[w1.property] at b2_in_B1, exact (mem_singleton.mp b2_in_B1).symm } /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In this set of curly brackets, we prove the second clause, that if `a1 ∈ block a2` then `block a1 = block a2`. A significant part of this just repeats for `a1` and `a2` the steps that we took for `a` in the previous set of curly brackets. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ end, let p : partition Ξ± := ⟨ block , ispart ⟩, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now package together the map `block` and the proof `ispart` to construct the partition that we wanted to construct. <br/><br/> There is a point to notice here about naming. A partition `p` is a structure with two members called `p.block` and `p.ispart`. We have two local variables called `block` and `ispart`, and we construct a partition `p` such that the member `p.block` is the local variable `block` and the member `p.ispart` is the local variable `ispart`. This correspondence of names is convenient and natural but not compulsory. Instead of `let block = ...` and `let ispart = ...` and let `p = ⟨block,ispart⟩` we could equally well have written `let foo = ...` and `let bar = ...` and `let p := ⟨foo,bar⟩`. The definition of `p` could also have been written `let p := {block := foo, ispart := bar}`. (Note that in this form we have curly brackets rather than angle brackets.) Similarly, with our current names for local variables we could have written `let p := { block := block, ispart := ispart}`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ have ok : p.block_set = blocks := begin /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now start to prove that the block set of our partiton `p` is the same as the block set that we were originally given. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ ext b, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Our goal is to prove that two finite sets are equal. The tactic `ext b` converts this to an equivalent goal: a generic element `b` lies in the first set iff it lies in the second set. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ split, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Our goal is to prove a bidirectional implication. The `split` tactic divides this into two goals, one for each direction. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ { intro b_in_blocks_p, rcases mem_image.mp b_in_blocks_p with ⟨ a , a_in_univ , p_block_a_eq_b ⟩, let w := block_aux a, have w_val : w.val = p.block a := rfl, rw[p_block_a_eq_b] at w_val, have b_in_B : b ∈ finset.singleton w.val := eq.subst (congr_arg finset.singleton w_val.symm) (mem_singleton.mpr rfl), rw[← w.property] at b_in_B, exact (mem_filter.mp b_in_B).1, },{ intro b_in_blocks, have b_not_empty : b β‰  βˆ… := no_empty_blocks b b_in_blocks, rcases exists_mem_of_ne_empty b_not_empty with ⟨ a , a_in_b ⟩ , have b_in_B : b ∈ blocks_for a := mem_filter.mpr ⟨ b_in_blocks , a_in_b ⟩ , let w := block_aux a, rw[w.property] at b_in_B, have b_eq_block_a : b = block a := mem_singleton.mp b_in_B, have t : βˆƒ x, βˆƒ x_in_univ : x ∈ univ, block x = b := ⟨ a , mem_univ a , b_eq_block_a.symm ⟩, exact (@mem_image Ξ± (finset Ξ±) _ block univ b).mpr t } end, exact ⟨ p , ok ⟩ end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ def to_setoid (p : partition Ξ±) : setoid Ξ± := { r := Ξ» a1 a2 : Ξ± , p.block a1 = p.block a2, iseqv := ⟨λ a : Ξ±, rfl, Ξ» _ _ e, e.symm, Ξ» _ _ _ e1 e2, e1.trans e2 ⟩ } /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now define a function which converts a partition to a setoid structure. A setoid structure is by definition a relation on `Ξ±` (encoded as a map `r : Ξ± β†’ Ξ± β†’ Prop`) together with a proof that it is an equivalence relation. The latter is encoded as a structure with three members, which are proofs of reflexivity, symmetry and transitivity. In our case the relevant relation is just `r a1 a2 := (p.block a1 = p.block a2)`, and the equivalence relation axioms follow immediately from the reflexivity, symmetry and transitivity of the equality relation on `finset Ξ±`. There is a tiny subtlety about ensuring that we set things up with the right mix of explicit and implicit arguments. <br/><br/> (In Lean, quotient structures work in much the same way as in classical mathematics. The name "setoid" comes from other proof assistant systems in which this is not the case. In those systems, one often has to consider a pair consisting of a type and an equivalence relation rather than forming the quotient. Such a pair is called a "setoid".) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ instance decidable_to_setoid (p : partition Ξ±): decidable_rel ((to_setoid p).r) := begin dsimp[to_setoid], apply_instance end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now show that our equivalence relation is decidable. It seems that we need to tell Lean explicitly to apply the definition of `to_setoid`, for which we use the `dsimp` tactic. After that, the `apply_instance` tactic succeeds in filling in the required details automatically. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ def of_setoid (s : setoid Ξ±) [decidable_rel s.r] : partition Ξ± := begin /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now define the inverse construction, where we start with a decidable setoid structure and produce a partition. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ let block : Ξ± β†’ finset Ξ± := Ξ» a , univ.filter (@setoid.r Ξ± s a), /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We define the block map. In ordinary mathematical notation, this would be `block a = { x : a β‰ˆ x }`. Lean's notational conventions for setoid structures are consistent with the more general conventions for typeclass instances and are based on the idea that a given type `Ξ±` probably has only one interesting setoid structure that we want to work with. Of course this is not consistent with the current application. We have therefore preferred to make all arguments explicit and write `@setoid.r Ξ± s a x` for the proposition that `a β‰ˆ x` with respect to `s`. We can also leave out the final argument and write `@setoid.r Ξ± s a` for the predicate sending `x` to `a β‰ˆ x`. The set `block a` is obtained by filtering the universal set, keeping only the elements that satisfy this predicate. It would also work to write `setoid.r a` in place of `@setoid.r Ξ± s a`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ let ispart : is_partition block := begin /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now start writing the proof that the above block map satisfies the partition axioms. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ dsimp[is_partition],split, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We apply the definition of the predicate `is_partition`. The goal is then to prove a conjunction of two clauses, and the `split` tactic converts this to a pair of goals, one for each clause. It is not really necessary to do `dsimp[is_partition]` first, but this may make it slightly easier to follow the proof. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ {intro a,apply mem_filter.mpr,split,exact (mem_univ a),exact (setoid.refl a)}, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We have written the proof of the first clause in a terse style. In VS Code, you can see how the various steps work by placing your cursor after one of the commas and examining the goal state in the Lean Messages window. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ {intros a1 a2 a1_in_b2, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For the second clause, we give ourselves `a1` and `a2` with `a1 ∈ block a2`, and our goal is to prove that `block a1 = block a2`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ ext x, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Our goal was to prove that the finite sets `block a1` and `block a2` are the same. The theorem `finset.ext'` says that two finite sets are equal if an arbitrary element lies in the first iff it lies in the second. This theorem is marked with the `[extensionality]` attribute, so the `ext` tactic finds it and uses it. Our goal is now to prove that `x ∈ block a1 ↔ x ∈ block a2`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ simp only [mem_filter,mem_univ x,mem_univ a1,true_and] at a1_in_b2 ⊒, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We apply the definition of the `block` map to convert all statements like `u ∈ block v` to statements like `u ∈ univ ∧ v β‰ˆ u`. We do this both in the hypothesis `a1_in_b2` and in the goal (denoted here by `⊒`). By including `mem_univ x`, `mem_univ a` and `true_and` in the simplification rules, we also get rid of the spurious `u ∈ univ` clauses. The hypothesis `a1_in_b2` has now become `a2 β‰ˆ a1`, and the goal is `a1 β‰ˆ x ↔ a2 β‰ˆ x`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ split, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Our goal is to prove a bidirectional implication. The split tactic converts this to a pair of goals, one for each direction. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ {exact setoid.trans a1_in_b2}, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Our first goal is now to prove `a2 β‰ˆ x β†’ a1 β‰ˆ x`. We could do the proof as follows: write `intro a2_eq_x` to give a hypothesis `a2 β‰ˆ x`, then invoke the transitivity rule in the form `setoid.trans a1_in_b2 a2_eq_x` to give a proof of `a1 β‰ˆ x`, which we can use in the `exact` tactic to satisfy the goal. However, it is in fact equivalent to skip the introduction step and just write `exact setoid.trans a1_in_b2`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ {exact setoid.trans (setoid.symm a1_in_b2)}, } end, /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Our second goal is to prove `a1 β‰ˆ x β†’ a2 β‰ˆ x`. Recall that `a1_in_b2` is now a proof of `a1 β‰ˆ a2`. We can write `(setoid.symm a1_in_b2)` to get a proof of `a2 β‰ˆ a1`, and then proceed as in the first goal. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ exact ⟨ block , ispart ⟩ end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We finish the definition by combining `block` and `ispart` into a partition structure. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ def fiber_partition {Ξ± Ξ² : Type} [decidable_eq Ξ±] [fintype Ξ±] [decidable_eq Ξ²] (f : Ξ± β†’ Ξ²) : partition Ξ± := { block := Ξ» a, (@univ Ξ± _).filter (Ξ» x, f x = f a), ispart := begin dsimp[is_partition],split, {intro a,apply mem_filter.mpr,simp[mem_univ]}, {intros a1 a2, intro h, have e : f a1 = f a2 := (mem_filter.mp h).right, congr,ext,simp[e], } end } /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Given a map `f : Ξ± β†’ Ξ²`, we can partition `Ξ±` using the fibers of `f`. (We need to ignore any fibers that are empty, but with our framework of definitions we do not need any explicit logic for that.) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma fiber_partition_blocks {Ξ± Ξ² : Type} [decidable_eq Ξ±] [fintype Ξ±] [decidable_eq Ξ²] (f : Ξ± β†’ Ξ²) (a1 a2 : Ξ±) : (fiber_partition f).block a1 = (fiber_partition f).block a2 ↔ f a1 = f a2 := begin have h0 : a1 ∈ (fiber_partition f).block a2 ↔ f a1 = f a2 := by simp[fiber_partition,mem_filter], split, {intro e, exact h0.mp (e.subst ((fiber_partition f).mem_block a1)), },{ intro e, have h1 : a1 ∈ ((fiber_partition f).block_alt a2).val := h0.mpr e, exact (block_val_eq_of_mem h1).symm } end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A basic lemma showing that the block map for the fiber partition behaves as expected. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma fiber_partition_blocks_alt {Ξ± Ξ² : Type} [decidable_eq Ξ±] [fintype Ξ±] [decidable_eq Ξ²] (f : Ξ± β†’ Ξ²) (a1 a2 : Ξ±) : (fiber_partition f).block_alt a1 = (fiber_partition f).block_alt a2 ↔ f a1 = f a2 := begin split; intro e, {exact (fiber_partition_blocks f a1 a2).mp (partition.block_alt_eq.mpr e)}, {exact partition.block_alt_eq.mp ((fiber_partition_blocks f a1 a2).mpr e)} end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This is the same as the previous lemma except that blocks of `p` are regarded as terms of type `p.block_type` rather than `finset Ξ±`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ section fintype variable (Ξ±) /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now start a new section in which we will prove that `partition Ξ±` is finite. In more detail, we previously assumed that we were given a term of type `fintype Ξ±`, and we use it to construct a term of type `fintype (partition Ξ±)`. Lean already knows many rules for constructing `fintype` instances, and we just need to combine these in an appropriate way. <br/><br/> The `fintype` instance on `partition Ξ±` will not depend on any auxiliary variables from which we could infer the value of `Ξ±`. We therefore need to switch to treating `Ξ±` as an explicit rather than implicit variable. This is the effect of the line `variable (Ξ±)`. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ private def partition_elems0 : finset (Ξ± β†’ (finset Ξ±)) := (@univ (Ξ± β†’ (finset Ξ±)) _).filter(is_partition) /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We will build up a definition in several steps. The individual steps are not so interesting, so we mark the definitions as "private". This means that they will not be visible outside the current section. <br/><br/> In this first ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ private def partition_elems1 := { p : Ξ± β†’ (finset Ξ±) // p ∈ (partition_elems0 Ξ±)} private def partition_elems2 : finset (partition_elems1 Ξ±) := (partition_elems0 Ξ±).attach private def partition_of_elems1 (p : partition_elems1 Ξ±) : (partition Ξ±) := begin exact { block := p.val, ispart := (mem_filter.mp p.property).2 } end lemma partition_of_elems1_block (p : partition_elems1 Ξ±) : (partition_of_elems1 Ξ± p).block = p.val := begin rcases p with ⟨ p_block , p_in_elems ⟩, dsimp[partition_of_elems1], exact rfl end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ def partition_elems : finset (partition Ξ±) := (partition_elems2 Ξ±).image (partition_of_elems1 Ξ±) /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We are now ready to give the definition of the finset of all partitions. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ lemma partition_elems_complete : βˆ€ p : partition Ξ±, p ∈ partition_elems Ξ± := /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We now state the key fact that every partition does indeed appear in the finset that we have constructed. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ begin intro p, rcases p with ⟨ p_block , p_ispart ⟩, let p_in_elems0 : p_block ∈ (partition_elems0 Ξ±) := mem_filter.mpr ⟨mem_univ p_block,p_ispart⟩, let p1 : partition_elems1 Ξ± := ⟨p_block, p_in_elems0⟩, have p1_in_elems2 : p1 ∈ partition_elems2 Ξ± := mem_attach (partition_elems0 Ξ±) p1, let p2 : partition Ξ± := partition_of_elems1 Ξ± p1, have p2_in_elems : p2 ∈ partition_elems Ξ± := mem_image_of_mem (partition_of_elems1 Ξ±) p1_in_elems2, have p2_eq_p : p2 = ⟨ p_block , p_ispart ⟩ := begin apply eq_of_block_eq.mpr, by apply partition_of_elems1_block end, exact eq.subst p2_eq_p p2_in_elems end /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ instance partition_fintype : fintype (partition Ξ±) := { elems := partition_elems Ξ±, complete := partition_elems_complete Ξ± } /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We can now package what we have done as an instance of `fintype (partition Ξ±)`, or in other words a proof that the set of partitions is finite. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ end fintype /-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/ section cofunctor variables {Ξ±} {Ξ² : Type*} {Ξ³ : Type*} [decidable_eq Ξ²] [decidable_eq Ξ³] [fintype Ξ²] [fintype Ξ³] (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ Ξ³) def cofunctor (q : partition Ξ²) : partition Ξ± := { block := Ξ» a, univ.filter (Ξ» x, f x ∈ q.block (f a)), ispart := begin split, {intro a,simp[q.ispart.left (f a)]}, begin intros a1 a2 f_in_block, have a1_in_b2 : f a1 ∈ (q.block_alt (f a2)).val := (mem_filter.mp f_in_block).right, have b1_eq_b2 := block_val_eq_of_mem a1_in_b2, dsimp[block_alt] at b1_eq_b2, ext,simp[b1_eq_b2] end end } lemma cofunctor_id : cofunctor (@id Ξ±) = @id (partition Ξ±) := begin ext p a0 a1,simp[cofunctor], end lemma cofunctor_comp : cofunctor (g ∘ f) = (cofunctor f) ∘ (cofunctor g) := begin ext p a0 a1,simp[cofunctor], end end cofunctor section isofunctor variables {Ξ±} {Ξ² : Type*} {Ξ³ : Type*} [decidable_eq Ξ²] [decidable_eq Ξ³] [fintype Ξ²] [fintype Ξ³] (f : Ξ± ≃ Ξ²) (g : Ξ² ≃ Ξ³) def isofunctor : (partition Ξ±) ≃ (partition Ξ²) := { to_fun := cofunctor f.inv_fun, inv_fun := cofunctor f.to_fun, left_inv := begin intro p, exact calc (cofunctor f.to_fun) ((cofunctor f.inv_fun) p) = ((cofunctor f.to_fun) ∘ (cofunctor f.inv_fun)) p : rfl ... = (cofunctor (f.inv_fun ∘ f.to_fun)) p : by rw[← cofunctor_comp] ... = (cofunctor id) p : by rw[← ((funext f.left_inv) : (f.inv_fun ∘ f.to_fun = id))] ... = id p : by rw[cofunctor_id] ... = p : rfl end, right_inv := begin intro q, exact calc (cofunctor f.inv_fun) ((cofunctor f.to_fun) q) = ((cofunctor f.inv_fun) ∘ (cofunctor f.to_fun)) q : rfl ... = (cofunctor (f.to_fun ∘ f.inv_fun)) q : by rw[← cofunctor_comp] ... = (cofunctor id) q : by rw[← ((funext f.right_inv) : (f.to_fun ∘ f.inv_fun = id))] ... = id q : by rw[cofunctor_id] ... = q : rfl end, } lemma isofunctor_id : isofunctor (equiv.refl Ξ±) = equiv.refl (partition Ξ±) := begin apply equiv.eq_of_to_fun_eq, dsimp[isofunctor,equiv.refl,cofunctor], exact cofunctor_id end lemma isofunctor_comp : isofunctor (f.trans g) = (isofunctor f).trans (isofunctor g) := begin apply equiv.eq_of_to_fun_eq, dsimp[isofunctor,equiv.trans,cofunctor], apply cofunctor_comp, end end isofunctor open equiv variables {Ξ±} {Ξ² : Type*} [decidable_eq Ξ²] [fintype Ξ²] (f : Ξ± ≃ Ξ²) theorem card_partitions_eq_card_partitions_fin {n : β„•} (h : fintype.card Ξ± = n) : card (partition Ξ±) = card (partition (fin n)) := begin rw ←h, refine trunc.induction_on (fintype.equiv_fin Ξ±) _, intro e, exact eq.subst h fintype.card_congr (isofunctor e) end instance : has_le (partition Ξ±) := ⟨ Ξ» p1 p2 : partition Ξ±, βˆ€ a, p2.block a βŠ† p1.block a ⟩ instance decidable_le (p1 p2 : partition Ξ±) : decidable (p1 ≀ p2) := begin dsimp[partition.has_le], apply_instance end @[simp] theorem le.refl (p : partition Ξ±) : p ≀ p := by dsimp[partition.has_le]; simp theorem le.trans {p1 p2 p3 : partition Ξ±} : p1 ≀ p2 β†’ p2 ≀ p3 β†’ p1 ≀ p3 := begin dsimp[partition.has_le], intros e1 e2 a, exact (subset.trans (e2 a) (e1 a)) end theorem le.antisymm {p1 p2 : partition Ξ±} : p1 ≀ p2 β†’ p2 ≀ p1 β†’ p1 = p2 := begin dsimp[partition.has_le], intros e1 e2, apply eq_of_block_eq.mpr, funext a, exact subset.antisymm (e2 a) (e1 a) end def bot : partition Ξ± := { block := Ξ» a, univ, ispart := by {split;intros;simp[mem_univ]} } def top : partition Ξ± := { block := @finset.singleton Ξ±, ispart := begin split, {apply mem_singleton_self}, {intros a1 a2 h,rw[mem_singleton.mp h]} end } def sup (p1 p2 : partition Ξ±) : partition Ξ± := begin let block : Ξ± β†’ finset Ξ± := Ξ» a, (p1.block a) ∩ (p2.block a), let ispart : is_partition block := begin dsimp[is_partition],split, { intro a, exact mem_inter.mpr ⟨p1.ispart.left a,p2.ispart.left a⟩, }, {intros a1 a2, let b11 := p1.block a1, let b12 := p2.block a1, let b21 := p1.block a2, let b22 := p2.block a2, let b1 := b11 ∩ b12, let b2 := b21 ∩ b22, intro a1_in_b2, have a1_in_b21 : a1 ∈ b21 := (mem_inter.mp (a1_in_b2 : a1 ∈ b2)).1, have a1_in_b22 : a1 ∈ b22 := (mem_inter.mp (a1_in_b2 : a1 ∈ b2)).2, have b11_eq_b21 : p1.block a1 = p1.block a2 := (p1.ispart.right a1 a2) a1_in_b21, have b12_eq_b22 : p2.block a1 = p2.block a2 := (p2.ispart.right a1 a2) a1_in_b22, dsimp[block], by rw[b11_eq_b21,b12_eq_b22] } end, exact ⟨ block , ispart ⟩ end instance partial_order_of_partitions : partial_order (partition Ξ±) := { le := has_le.le, le_refl := @partition.le.refl Ξ± _ _, le_trans := @partition.le.trans Ξ± _ _, le_antisymm := @partition.le.antisymm _ _ _ } end partition end combinatorics
d8fff3c7cdcec4e1f4f9b390900027a149c931be
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/archive/sensitivity.lean
a14cead730779f7ca1ef3765ce06d97a61d02611
[ "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
15,592
lean
/- Copyright (c) 2019 Reid Barton, Johan Commelin, Jesse Han, Chris Hughes, Robert Y. Lewis, Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Johan Commelin, Jesse Han, Chris Hughes, Robert Y. Lewis, Patrick Massot -/ import tactic.fin_cases import tactic.apply_fun import linear_algebra.finite_dimensional import linear_algebra.dual import analysis.normed_space.basic import data.real.sqrt /-! # Huang's sensitivity theorem A formalization of Hao Huang's sensitivity theorem: in the hypercube of dimension n β‰₯ 1, if one colors more than half the vertices then at least one vertex has at least √n colored neighbors. A fun summer collaboration by Reid Barton, Johan Commelin, Jesse Han, Chris Hughes, Robert Y. Lewis, and Patrick Massot, based on Don Knuth's account of the story (https://www.cs.stanford.edu/~knuth/papers/huang.pdf), using the Lean theorem prover (https://leanprover.github.io/), by Leonardo de Moura at Microsoft Research, and his collaborators (https://leanprover.github.io/people/), and using Lean's user maintained mathematics library (https://github.com/leanprover-community/mathlib). The project was developed at https://github.com/leanprover-community/lean-sensitivity and is now archived at https://github.com/leanprover-community/mathlib/blob/master/archive/sensitivity.lean -/ /-! The next two lines assert we do not want to give a constructive proof, but rather use classical logic. -/ noncomputable theory open_locale classical /-! We also want to use the notation `βˆ‘` for sums. -/ open_locale big_operators notation `|`x`|` := abs x notation `√` := real.sqrt open function bool linear_map fintype finite_dimensional dual_pair /-! ### The hypercube Notations: - `β„•` denotes natural numbers (including zero). - `fin n` = {0, β‹― , n - 1}. - `bool` = {`tt`, `ff`}. -/ /-- The hypercube in dimension `n`. -/ @[derive [inhabited, fintype]] def Q (n : β„•) := fin n β†’ bool /-- The projection from `Q (n + 1)` to `Q n` forgetting the first value (ie. the image of zero). -/ def Ο€ {n : β„•} : Q (n + 1) β†’ Q n := Ξ» p, p ∘ fin.succ namespace Q /-! `n` will always denote a natural number. -/ variable (n : β„•) /-- `Q 0` has a unique element. -/ instance : unique (Q 0) := ⟨⟨λ _, tt⟩, by { intro, ext x, fin_cases x }⟩ /-- `Q n` has 2^n elements. -/ lemma card : card (Q n) = 2^n := by simp [Q] /-! Until the end of this namespace, `n` will be an implicit argument (still a natural number). -/ variable {n} lemma succ_n_eq (p q : Q (n+1)) : p = q ↔ (p 0 = q 0 ∧ Ο€ p = Ο€ q) := begin split, { rintro rfl, exact ⟨rfl, rfl⟩, }, { rintros ⟨hβ‚€, h⟩, ext x, by_cases hx : x = 0, { rwa hx }, { rw ← fin.succ_pred x hx, convert congr_fun h (fin.pred x hx) } } end /-- The adjacency relation defining the graph structure on `Q n`: `p.adjacent q` if there is an edge from `p` to `q` in `Q n`. -/ def adjacent {n : β„•} (p : Q n) : set (Q n) := Ξ» q, βˆƒ! i, p i β‰  q i /-- In `Q 0`, no two vertices are adjacent. -/ lemma not_adjacent_zero (p q : Q 0) : Β¬ p.adjacent q := by rintros ⟨v, _⟩; apply fin_zero_elim v /-- If `p` and `q` in `Q (n+1)` have different values at zero then they are adjacent iff their projections to `Q n` are equal. -/ lemma adj_iff_proj_eq {p q : Q (n+1)} (hβ‚€ : p 0 β‰  q 0) : p.adjacent q ↔ Ο€ p = Ο€ q := begin split, { rintros ⟨i, h_eq, h_uni⟩, ext x, by_contradiction hx, apply fin.succ_ne_zero x, rw [h_uni _ hx, h_uni _ hβ‚€] }, { intro heq, use [0, hβ‚€], intros y hy, contrapose! hy, rw ←fin.succ_pred _ hy, apply congr_fun heq } end /-- If `p` and `q` in `Q (n+1)` have the same value at zero then they are adjacent iff their projections to `Q n` are adjacent. -/ lemma adj_iff_proj_adj {p q : Q (n+1)} (hβ‚€ : p 0 = q 0) : p.adjacent q ↔ (Ο€ p).adjacent (Ο€ q) := begin split, { rintros ⟨i, h_eq, h_uni⟩, have h_i : i β‰  0, from Ξ» h_i, absurd hβ‚€ (by rwa h_i at h_eq), use [i.pred h_i, show p (fin.succ (fin.pred i _)) β‰  q (fin.succ (fin.pred i _)), by rwa fin.succ_pred], intros y hy, simp [eq.symm (h_uni _ hy)] }, { rintros ⟨i, h_eq, h_uni⟩, use [i.succ, h_eq], intros y hy, rw [←fin.pred_inj, fin.pred_succ], { apply h_uni, change p (fin.pred _ _).succ β‰  q (fin.pred _ _).succ, simp [hy] }, { contrapose! hy, rw [hy, hβ‚€] }, { apply fin.succ_ne_zero } } end @[symm] lemma adjacent.symm {p q : Q n} : p.adjacent q ↔ q.adjacent p := by simp only [adjacent, ne_comm] end Q /-! ### The vector space -/ /-- The free vector space on vertices of a hypercube, defined inductively. -/ def V : β„• β†’ Type | 0 := ℝ | (n+1) := V n Γ— V n namespace V variables (n : β„•) /-! `V n` is a real vector space whose equality relation is computable. -/ instance : decidable_eq (V n) := by { induction n ; { dunfold V, resetI, apply_instance } } instance : add_comm_group (V n) := by { induction n ; { dunfold V, resetI, apply_instance } } instance : module ℝ (V n) := by { induction n ; { dunfold V, resetI, apply_instance } } end V /-- The basis of `V` indexed by the hypercube, defined inductively. -/ noncomputable def e : Ξ  {n}, Q n β†’ V n | 0 := Ξ» _, (1:ℝ) | (n+1) := Ξ» x, cond (x 0) (e (Ο€ x), 0) (0, e (Ο€ x)) @[simp] lemma e_zero_apply (x : Q 0) : e x = (1 : ℝ) := rfl /-- The dual basis to `e`, defined inductively. -/ noncomputable def Ξ΅ : Ξ  {n : β„•} (p : Q n), V n β†’β‚—[ℝ] ℝ | 0 _ := linear_map.id | (n+1) p := cond (p 0) ((Ξ΅ $ Ο€ p).comp $ linear_map.fst _ _ _) ((Ξ΅ $ Ο€ p).comp $ linear_map.snd _ _ _) variable {n : β„•} lemma duality (p q : Q n) : Ξ΅ p (e q) = if p = q then 1 else 0 := begin induction n with n IH, { rw (show p = q, from subsingleton.elim p q), dsimp [Ξ΅, e], simp }, { dsimp [Ξ΅, e], cases hp : p 0 ; cases hq : q 0, all_goals { repeat {rw cond_tt}, repeat {rw cond_ff}, simp only [linear_map.fst_apply, linear_map.snd_apply, linear_map.comp_apply, IH], try { congr' 1, rw Q.succ_n_eq, finish }, try { erw (Ξ΅ _).map_zero, have : p β‰  q, { intro h, rw p.succ_n_eq q at h, finish }, simp [this] } } } end /-- Any vector in `V n` annihilated by all `Ξ΅ p`'s is zero. -/ lemma epsilon_total {v : V n} (h : βˆ€ p : Q n, (Ξ΅ p) v = 0) : v = 0 := begin induction n with n ih, { dsimp [Ξ΅] at h, exact h (Ξ» _, tt) }, { cases v with v₁ vβ‚‚, ext ; change _ = (0 : V n) ; simp only ; apply ih ; intro p ; [ let q : Q (n+1) := Ξ» i, if h : i = 0 then tt else p (i.pred h), let q : Q (n+1) := Ξ» i, if h : i = 0 then ff else p (i.pred h)], all_goals { specialize h q, rw [Ξ΅, show q 0 = tt, from rfl, cond_tt] at h <|> rw [Ξ΅, show q 0 = ff, from rfl, cond_ff] at h, rwa show p = Ο€ q, by { ext, simp [q, fin.succ_ne_zero, Ο€] } } } end /-- `e` and `Ξ΅` are dual families of vectors. It implies that `e` is indeed a basis and `Ξ΅` computes coefficients of decompositions of vectors on that basis. -/ def dual_pair_e_Ξ΅ (n : β„•) : dual_pair (@e n) (@Ξ΅ n) := { eval := duality, total := @epsilon_total _ } /-! We will now derive the dimension of `V`, first as a cardinal in `dim_V` and, since this cardinal is finite, as a natural number in `finrank_V` -/ lemma dim_V : module.rank ℝ (V n) = 2^n := have module.rank ℝ (V n) = (2^n : β„•), by { rw [dim_eq_card_basis (dual_pair_e_Ξ΅ _).basis, Q.card]; apply_instance }, by assumption_mod_cast instance : finite_dimensional ℝ (V n) := finite_dimensional.of_fintype_basis (dual_pair_e_Ξ΅ _).basis lemma finrank_V : finrank ℝ (V n) = 2^n := have _ := @dim_V n, by rw ←finrank_eq_dim at this; assumption_mod_cast /-! ### The linear map -/ /-- The linear operator $f_n$ corresponding to Huang's matrix $A_n$, defined inductively as a ℝ-linear map from `V n` to `V n`. -/ noncomputable def f : Ξ  n, V n β†’β‚—[ℝ] V n | 0 := 0 | (n+1) := linear_map.prod (linear_map.coprod (f n) linear_map.id) (linear_map.coprod linear_map.id (-f n)) /-! The preceding definition uses linear map constructions to automatically get that `f` is linear, but its values are somewhat buried as a side-effect. The next two lemmas unbury them. -/ @[simp] lemma f_zero : f 0 = 0 := rfl lemma f_succ_apply (v : V (n+1)) : f (n+1) v = (f n v.1 + v.2, v.1 - f n v.2) := begin cases v, rw f, simp only [linear_map.id_apply, linear_map.prod_apply, prod.mk.inj_iff, linear_map.neg_apply, sub_eq_add_neg, linear_map.coprod_apply], exact ⟨rfl, rfl⟩ end /-! In the next statement, the explicit conversion `(n : ℝ)` of `n` to a real number is necessary since otherwise `n β€’ v` refers to the multiplication defined using only the addition of `V`. -/ lemma f_squared : βˆ€ v : V n, (f n) (f n v) = (n : ℝ) β€’ v := begin induction n with n IH; intro, { simpa only [nat.cast_zero, zero_smul] }, { cases v, simp [f_succ_apply, IH, add_smul, add_assoc], abel } end /-! We now compute the matrix of `f` in the `e` basis (`p` is the line index, `q` the column index). -/ lemma f_matrix : βˆ€ p q : Q n, |Ξ΅ q (f n (e p))| = if q.adjacent p then 1 else 0 := begin induction n with n IH, { intros p q, dsimp [f], simp [Q.not_adjacent_zero] }, { intros p q, have ite_nonneg : ite (Ο€ q = Ο€ p) (1 : ℝ) 0 β‰₯ 0, { split_ifs ; norm_num }, have f_map_zero := (show linear_map ℝ (V (n+0)) (V n), from f n).map_zero, dsimp [e, Ξ΅, f], cases hp : p 0 ; cases hq : q 0, all_goals { repeat {rw cond_tt}, repeat {rw cond_ff}, simp [f_map_zero, hp, hq, IH, duality, abs_of_nonneg ite_nonneg, Q.adj_iff_proj_eq, Q.adj_iff_proj_adj] } } end /-- The linear operator $g_m$ corresponding to Knuth's matrix $B_m$. -/ noncomputable def g (m : β„•) : V m β†’β‚—[ℝ] V (m+1) := linear_map.prod (f m + √(m+1) β€’ linear_map.id) linear_map.id /-! In the following lemmas, `m` will denote a natural number. -/ variables {m : β„•} /-! Again we unpack what are the values of `g`. -/ lemma g_apply : βˆ€ v, g m v = (f m v + √(m+1) β€’ v, v) := by delta g; simp lemma g_injective : injective (g m) := begin rw g, intros x₁ xβ‚‚ h, simp only [linear_map.prod_apply, linear_map.id_apply, prod.mk.inj_iff] at h, exact h.right end lemma f_image_g (w : V (m + 1)) (hv : βˆƒ v, g m v = w) : f (m + 1) w = √(m + 1) β€’ w := begin rcases hv with ⟨v, rfl⟩, have : √(m+1) * √(m+1) = m+1 := real.mul_self_sqrt (by exact_mod_cast zero_le _), simp [this, f_succ_apply, g_apply, f_squared, smul_add, add_smul, smul_smul], abel end /-! ### The main proof In this section, in order to enforce that `n` is positive, we write it as `m + 1` for some natural number `m`. -/ /-! `dim X` will denote the dimension of a subspace `X` as a cardinal. -/ notation `dim` X:70 := module.rank ℝ β†₯X /-! `fdim X` will denote the (finite) dimension of a subspace `X` as a natural number. -/ notation `fdim` := finrank ℝ /-! `Span S` will denote the ℝ-subspace spanned by `S`. -/ notation `Span` := submodule.span ℝ /-! `Card X` will denote the cardinal of a subset of a finite type, as a natural number. -/ notation `Card` X:70 := X.to_finset.card /-! In the following, `βŠ“` and `βŠ”` will denote intersection and sums of ℝ-subspaces, equipped with their subspace structures. The notations come from the general theory of lattices, with inf and sup (also known as meet and join). -/ /-- If a subset `H` of `Q (m+1)` has cardinal at least `2^m + 1` then the subspace of `V (m+1)` spanned by the corresponding basis vectors non-trivially intersects the range of `g m`. -/ lemma exists_eigenvalue (H : set (Q (m + 1))) (hH : Card H β‰₯ 2^m + 1) : βˆƒ y ∈ Span (e '' H) βŠ“ (g m).range, y β‰  (0 : _) := begin let W := Span (e '' H), let img := (g m).range, suffices : 0 < dim (W βŠ“ img), { simp only [exists_prop], exact_mod_cast exists_mem_ne_zero_of_dim_pos this }, have dim_le : dim (W βŠ” img) ≀ 2^(m + 1), { convert ← dim_submodule_le (W βŠ” img), apply dim_V }, have dim_add : dim (W βŠ” img) + dim (W βŠ“ img) = dim W + 2^m, { convert ← dim_sup_add_dim_inf_eq W img, rw ← dim_eq_of_injective (g m) g_injective, apply dim_V }, have dimW : dim W = card H, { have li : linear_independent ℝ (set.restrict e H), { convert (dual_pair_e_Ξ΅ _).basis.linear_independent.comp _ subtype.val_injective, rw (dual_pair_e_Ξ΅ _).coe_basis }, have hdW := dim_span li, rw set.range_restrict at hdW, convert hdW, rw [← (dual_pair_e_Ξ΅ _).coe_basis, cardinal.mk_image_eq (dual_pair_e_Ξ΅ _).basis.injective, cardinal.fintype_card] }, rw ← finrank_eq_dim ℝ at ⊒ dim_le dim_add dimW, rw [← finrank_eq_dim ℝ, ← finrank_eq_dim ℝ] at dim_add, norm_cast at ⊒ dim_le dim_add dimW, rw pow_succ' at dim_le, rw set.to_finset_card at hH, linarith end theorem huang_degree_theorem (H : set (Q (m + 1))) (hH : Card H β‰₯ 2^m + 1) : βˆƒ q, q ∈ H ∧ √(m + 1) ≀ Card (H ∩ q.adjacent) := begin rcases exists_eigenvalue H hH with ⟨y, ⟨⟨y_mem_H, y_mem_g⟩, y_ne⟩⟩, have coeffs_support : ((dual_pair_e_Ξ΅ (m+1)).coeffs y).support βŠ† H.to_finset, { intros p p_in, rw finsupp.mem_support_iff at p_in, rw set.mem_to_finset, exact (dual_pair_e_Ξ΅ _).mem_of_mem_span y_mem_H p p_in }, obtain ⟨q, H_max⟩ : βˆƒ q : Q (m+1), βˆ€ q' : Q (m+1), |(Ξ΅ q' : _) y| ≀ |Ξ΅ q y|, from fintype.exists_max _, have H_q_pos : 0 < |Ξ΅ q y|, { contrapose! y_ne, exact epsilon_total (Ξ» p, abs_nonpos_iff.mp (le_trans (H_max p) y_ne)) }, refine ⟨q, (dual_pair_e_Ξ΅ _).mem_of_mem_span y_mem_H q (abs_pos.mp H_q_pos), _⟩, let s := √(m+1), suffices : s * |Ξ΅ q y| ≀ ↑(_) * |Ξ΅ q y|, from (mul_le_mul_right H_q_pos).mp β€Ή_β€Ί, let coeffs := (dual_pair_e_Ξ΅ (m+1)).coeffs, calc s * |Ξ΅ q y| = |Ξ΅ q (s β€’ y)| : by rw [map_smul, smul_eq_mul, abs_mul, abs_of_nonneg (real.sqrt_nonneg _)] ... = |Ξ΅ q (f (m+1) y)| : by rw [← f_image_g y (by simpa using y_mem_g)] ... = |Ξ΅ q (f (m+1) (lc _ (coeffs y)))| : by rw (dual_pair_e_Ξ΅ _).lc_coeffs y ... = |(coeffs y).sum (Ξ» (i : Q (m + 1)) (a : ℝ), a β€’ ((Ξ΅ q) ∘ (f (m + 1)) ∘ Ξ» (i : Q (m + 1)), e i) i)| : by erw [(f $ m + 1).map_finsupp_total, (Ξ΅ q).map_finsupp_total, finsupp.total_apply] ... ≀ βˆ‘ p in (coeffs y).support, |(coeffs y p) * (Ξ΅ q $ f (m+1) $ e p)| : norm_sum_le _ $ Ξ» p, coeffs y p * _ ... = βˆ‘ p in (coeffs y).support, |coeffs y p| * ite (q.adjacent p) 1 0 : by simp only [abs_mul, f_matrix] ... = βˆ‘ p in (coeffs y).support.filter (Q.adjacent q), |coeffs y p| : by simp [finset.sum_filter] ... ≀ βˆ‘ p in (coeffs y).support.filter (Q.adjacent q), |coeffs y q| : finset.sum_le_sum (Ξ» p _, H_max p) ... = (((coeffs y).support.filter (Q.adjacent q)).card : ℝ) * |coeffs y q| : by rw [finset.sum_const, nsmul_eq_mul] ... = (((coeffs y).support ∩ (Q.adjacent q).to_finset).card : ℝ) * |coeffs y q| : by { congr' with x, simp, refl } ... ≀ (finset.card ((H ∩ Q.adjacent q).to_finset )) * |Ξ΅ q y| : begin refine (mul_le_mul_right H_q_pos).2 _, norm_cast, apply finset.card_le_of_subset, rw set.to_finset_inter, convert finset.inter_subset_inter_right coeffs_support end end
2853bf1e50520a75ece7b892718d7b182f47800c
d450724ba99f5b50b57d244eb41fef9f6789db81
/src/mywork/lectures/lecture_21.lean
50f150910ab553f48f719cbfd44c187981d932a0
[]
no_license
jakekauff/CS2120F21
4f009adeb4ce4a148442b562196d66cc6c04530c
e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad
refs/heads/main
1,693,841,880,030
1,637,604,848,000
1,637,604,848,000
399,946,698
0
0
null
null
null
null
UTF-8
Lean
false
false
10,661
lean
import data.set namespace hidden /- PROPERTIES OF RELATIONS -/ section relation /- For any types, Ξ± and Ξ² we will refine a relation, r, to be a predicate on values of these types. It will implicitly define the set of all such pairs, also called a relation, that satisfy the predicate (by yielding a proposition for which there is a proof). -/ variables {Ξ± Ξ² : Type} (r : Ξ² β†’ Ξ² β†’ Prop) /- This variables declaration implicitly adds the following parameters to the front of each definition below, as needed based on the variables used in the rest of a given definition. We'll see an example shortly. -/ #check r -- two place predicate: relation /- We will introduce an infix notation, β‰Ί, for the relation/predicate, r, so that instead of writing (r a b) to denote the proposition that a is related to b by r, we can write (a β‰Ί b) read as "a is related to b." -/ local infix `β‰Ί`:50 := r -- infix notation /- With these concepts and notations, we can now define many essential properties of relations in an entirely general way. -/ /- REFLEXIVITY AS A PROPERTY OF RELATIONS -/ /- Let's see an example in detail. Using the preceding definitions, implict arguments, and notations, the definition of reflexive is exceedingly clear. This is it. In English you can say "a relation, β‰Ί, is reflexive if it relates every value, x, to itself."" -/ def reflexive := βˆ€ x, x β‰Ί x -- proposition builder. predicate on binary relations. -- when applied to binary relation, gives a proposition. /- Filling in the implicit arguments and unfolding the notation gives us the full picture. See the next definition. -/ def reflexive' {Ξ² : Type} {r : Ξ² β†’ Ξ² β†’ Prop} := βˆ€ x, r x x /- These definitions are parameterized by a type, Ξ², and a (binary) relation, r, on Ξ² (r βŠ† Ξ² Γ— Ξ²), and the yield *propositions*, that every value of a type Ξ² is related to itself by r. One will often then want a proof, but now that is just a matter of ordinarly logical reasoning. We have thus now "reduced" one crucial property of mathematial relations to our underlying predicate logic. As an example, let's state and prove the proposition that the equality relation for natural numbers is is reflexive. -/ theorem eq_is_refl : reflexive (@eq nat) := -- proof below /- Let's unpack the notation, @eq Ξ±. When we write, 0 = 0, that's infix notation for the term, eq 0 0 (the application of the two-place predicate, eq, to 0 and 0. It is the meaning of the notation 0 = 0.). But what (eq 0 0) means is (@eq nat 0 0). The first argument of eq is implicit, so is usually omitted from code and inferred from the following values. But here we don't give such values. The @tells Lean to let us write the implicit argument(s) explicitly. -/ begin unfold reflexive, -- abstract concept in set theory assume x, -- has meaning in logic trivial, end /- Here's an English version. Theorem: Equality is reflexive. Proof. Unfolding the definition of reflexive, what we are to show is βˆ€ x, x = x. To prove it, assume x is an arbitrary value and show x = x. That's true by (application of) the introduction rule for equality (to x). QED. You'd usually leave out the parenthesized parts, as people with mathematical training, like you now have, will understand what is meant implicitly. Indeed, most people would just say, "and that's trivially true. QED" Of course an even simpler proof is "by the reflexivity introduction rule for equality!" We're just re-proving something we already knew, be here we're forming the proposition that equality is reflexivity using a general definition of what that means in the form of a *predicate on binary relations on any type.* -/ /- Note well: reflexive as we're defined it here is a *one-place* predicate on binary relations, which we represent as two-place predicates. So we expect the type of the reflexive predicate / property to be (Ξ² β†’ Ξ² β†’ Prop) β†’ Prop. Lean is of course plenty smart to already know that! -/ #check @reflexive /- Ξ  {Ξ² : Type}, (Ξ² β†’ Ξ² β†’ Prop) β†’ Prop Again, think of Ξ  as βˆ€. What defined reflexive to be, given any type Ξ², is a predicate on binary relations on Ξ². Some binary relations are reflexive, some aren't: i.e., some have the property of being reflexive (of reflexivity), some don't. This predicate picks out (logically) those that do. Building on our discussion of sets, it appears to be the case, and it is, that it defines the set of binary relations on Ξ² that are reflexive. We can define this set explicitly by using reflexive as a predicate on binary relations in a set builder expression. -/ def reflexive_relations := { r : Ξ² β†’ Ξ² β†’ Prop | reflexive r } -- set of all binary relations r such that r is reflexive. -- That's pretty cool. Says a lot, very precisely. /- Now we can even state and prove theorems about relations being reflexive, using the language of *set theory*. Here we say that "the equality relation on natural numbers is an element in the set of all reflexive binary relations on the natural numbers." Here it is formally. The use of @ here again turns off implicit argument inference. I do expect you to understand what it means when you see it. I will not give you any problems, except perhaps extra credit, where I expect you to know when exactly to use it. -/ example : @eq nat ∈ @reflexive_relations nat := -- proposition: equality on the natural numbers is -- in the set of all reflexive relations on the natural numbers. begin show reflexive_relations (@eq nat), unfold reflexive_relations, exact eq_is_refl, -- Already proved, use theorem! end -- You can just feel your brains getting bigger here! #check (Ξ» x, x + 1) /- IMPORTANT ASIDE ON FIRST ORDER PREDICATE LOGIC As an aside that we will return to later, you cannot express properties of relations, with= this degree of clarity, in what we call first- order predicate logic (FOPL). You are expected to understand FOPL when leaving this course. The good news is that it is simply a restricted form of higher-order predicate logic in which you are not enabled to quantify over relations. The syntax of FOPL is simply not capable of expressing the kind of idea that we developed righ there: that we can specify properties of relations by using our logic. From now on, therefore, if you are asked to write or prove propositions in first-order predicate logic, you can use everything you have learned, here except that you must not quantify over functions or relations. In the case at hand, you would not be allowed to say "for any binary relation, r, on Ξ²" or "there exists such a relation," because either of those statements involves quantification over the set of all binary relations on a type, Ξ². The broad experience of the computer science community over the last few decades has shown that there are many practical benefits to the use of higher-order logic. In particular, it is the logical foundation for automated proof assistants such as Lean, which are now rapidly coming into their own, especially for safety- / mission- critical sofware, provable system security, and in the design, analysis, and implementation of programming languages, as well as in attempts to formalize mathematics, per se. As you're instructor, I believe, based on a decade of work in this area, that the beauty with which one can express *properties of relations* (and the vital importance of this idea), the availability of automated checking of logical syntax and proof correctness (when doing formal proofs), and that fact that first-order logic is simply a restricted form of higher-order logic, make it *far* preferable to FOPL as a logic to learn in a first course on logic and proof for computer scientists. __you cannot quantify over higher order things__ __such as functions, in FOPL__ And now we return to our regularly scheduled programming! -/ /- SYMMETRY AS A PROPERTY OF RELATIONS -/ def symmetric := βˆ€ ⦃x y⦄, x β‰Ί y β†’ y β‰Ί x /- Exercise: prove that = is symmetric. And answer the question, is ≀ symmetric, and give a brief defense of your answer. -/ theorem eq_is_symm : symmetric (@eq Ξ±) := begin unfold symmetric, assume x y xy, apply eq.symm, exact xy, end /- Exercise prove that = is transitive as per the following formal and general definition of exactly what that means. Give both formal and especially informal (mathematical English) proofs. -/ def transitive := βˆ€ ⦃x y z⦄, x β‰Ί y β†’ y β‰Ί z β†’ x β‰Ί z example : transitive (@eq Ξ±) := begin unfold transitive, --__unfold translates from set theory to predicate logic__ assume x y z xy yz, exact eq.trans xy yz, end /- Here's a new concept: a relation is said to be an equivalence relation if it is reflexive, symmetric, and transitive. -/ def equivalence := reflexive r ∧ symmetric r ∧ transitive r lemma mk_equivalence (rfl : reflexive r) (symm : symmetric r) (trans : transitive r) : equivalence r := ⟨rfl, symm, trans⟩ -- Exercise theorem eq_is_equivalence : equivalence (@eq Ξ²) := begin unfold equivalence, apply and.intro, unfold reflexive, assume x, apply eq.refl, apply and.intro, unfold symmetric, assume x y xy, exact eq.symm xy, unfold transitive, assume x y z xy yz, exact eq.trans xy yz, end /- βˆ€ ( k : β„• ), βˆƒ an equivalence class of values that are congruent to each other mod 4 13 is congruent to 9, mod 4 -- multiples of 4 apart from each other β†’ congruent mod 4 11 is congruent to 3, mod 4 Congruence mod 4 is symmetric, reflexive, and transitive β†’ congruence is an equivalence relationship. -/ /- ADDITIONAL PROPERTIES OF RELATIONS. NEXT LECTURE. -/ def total := βˆ€ x y, x β‰Ί y ∨ y β‰Ί x def irreflexive := βˆ€ x, Β¬ x β‰Ί x def anti_symmetric := βˆ€ ⦃x y⦄, x β‰Ί y β†’ y β‰Ί x β†’ x = y def empty_relation := Ξ» a₁ aβ‚‚ : Ξ±, false def subrelation (q r : Ξ² β†’ Ξ² β†’ Prop) := βˆ€ ⦃x y⦄, q x y β†’ r x y def inv_image (f : Ξ± β†’ Ξ²) : Ξ± β†’ Ξ± β†’ Prop := Ξ» a₁ aβ‚‚, f a₁ β‰Ί f aβ‚‚ lemma inv_image.trans (f : Ξ± β†’ Ξ²) (h : transitive r) : transitive (inv_image r f) := Ξ» (a₁ aβ‚‚ a₃ : Ξ±) (h₁ : inv_image r f a₁ aβ‚‚) (hβ‚‚ : inv_image r f aβ‚‚ a₃), h h₁ hβ‚‚ lemma inv_image.irreflexive (f : Ξ± β†’ Ξ²) (h : irreflexive r) : irreflexive (inv_image r f) := Ξ» (a : Ξ±) (h₁ : inv_image r f a a), h (f a) h₁ inductive tc {Ξ± : Type} (r : Ξ± β†’ Ξ± β†’ Prop) : Ξ± β†’ Ξ± β†’ Prop | base : βˆ€ a b, r a b β†’ tc a b | trans : βˆ€ a b c, tc a b β†’ tc b c β†’ tc a c end relation end hidden
79fdd73f06d4a6c16e3e6546e90c139a51c72464
46125763b4dbf50619e8846a1371029346f4c3db
/src/algebra/free_monoid.lean
a362b6d5b9f9ebaab0995e9f183cd6713ac03e2c
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
3,702
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Yury Kudryashov -/ import algebra.group.hom data.equiv.algebra data.list.basic /-! # Free monoid over a given alphabet ## Main definitions * `free_monoid Ξ±`: free monoid over alphabet `Ξ±`; defined as a synonym for `list Ξ±` with multiplication given by `(++)`. * `free_monoid.of`: embedding `Ξ± β†’ free_monoid Ξ±` sending each element `x` to `[x]`; * `free_monoid.lift Ξ± M`: natural equivalence between `Ξ± β†’ M` and `free_monoid Ξ± β†’* M`; for technical reasons `Ξ±` and `M` are explicit arguments; * `free_monoid.map`: embedding of `Ξ± β†’ Ξ²` into `free_monoid Ξ± β†’* free_monoid Ξ²` given by `list.map`. -/ variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {M : Type*} [monoid M] /-- Free monoid over a given alphabet. -/ @[to_additive free_add_monoid "Free nonabelian additive monoid over a given alphabet"] def free_monoid (Ξ±) := list Ξ± namespace free_monoid @[to_additive] instance {Ξ±} : monoid (free_monoid Ξ±) := { one := [], mul := Ξ» x y, (x ++ y : list Ξ±), mul_one := by intros; apply list.append_nil, one_mul := by intros; refl, mul_assoc := by intros; apply list.append_assoc } @[to_additive] instance {Ξ±} : inhabited (free_monoid Ξ±) := ⟨1⟩ @[to_additive] lemma one_def {Ξ±} : (1 : free_monoid Ξ±) = [] := rfl @[to_additive] lemma mul_def {Ξ±} (xs ys : list Ξ±) : (xs * ys : free_monoid Ξ±) = (xs ++ ys : list Ξ±) := rfl /-- Embeds an element of `Ξ±` into `free_monoid Ξ±` as a singleton list. -/ @[to_additive "Embeds an element of `Ξ±` into `free_add_monoid Ξ±` as a singleton list." ] def of (x : Ξ±) : free_monoid Ξ± := [x] @[to_additive] lemma of_mul_eq_cons (x : Ξ±) (l : free_monoid Ξ±) : of x * l = x :: l := rfl @[to_additive] lemma hom_eq ⦃f g : free_monoid Ξ± β†’* M⦄ (h : βˆ€ x, f (of x) = g (of x)) : f = g := begin ext l, induction l with a l ihl, { exact f.map_one.trans g.map_one.symm }, { rw [← of_mul_eq_cons, f.map_mul, h, ihl, ← g.map_mul] } end section -- TODO[Lean 4] : make these arguments implicit variables (Ξ± M) /-- Equivalence between maps `Ξ± β†’ M` and monoid homomorphisms `free_monoid Ξ± β†’* M`. -/ @[to_additive "Equivalence between maps `Ξ± β†’ A` and additive monoid homomorphisms `free_add_monoid Ξ± β†’+ A`."] def lift : (Ξ± β†’ M) ≃ (free_monoid Ξ± β†’* M) := { to_fun := Ξ» f, ⟨λ l, (l.map f).prod, rfl, Ξ» l₁ lβ‚‚, by simp only [mul_def, list.map_append, list.prod_append]⟩, inv_fun := Ξ» f x, f (of x), left_inv := Ξ» f, funext $ Ξ» x, one_mul (f x), right_inv := Ξ» f, hom_eq $ Ξ» x, one_mul (f (of x)) } end lemma lift_eval_of (f : Ξ± β†’ M) (x : Ξ±) : lift Ξ± M f (of x) = f x := congr_fun ((lift Ξ± M).symm_apply_apply f) x lemma lift_restrict (f : free_monoid Ξ± β†’* M) : lift Ξ± M (f ∘ of) = f := (lift Ξ± M).apply_symm_apply f /-- The unique monoid homomorphism `free_monoid Ξ± β†’* free_monoid Ξ²` that sends each `of x` to `of (f x)`. -/ @[to_additive "The unique additive monoid homomorphism `free_add_monoid Ξ± β†’+ free_add_monoid Ξ²` that sends each `of x` to `of (f x)`."] def map (f : Ξ± β†’ Ξ²) : free_monoid Ξ± β†’* free_monoid Ξ² := { to_fun := list.map f, map_one' := rfl, map_mul' := Ξ» l₁ lβ‚‚, list.map_append _ _ _ } @[simp, to_additive] lemma map_of (f : Ξ± β†’ Ξ²) (x : Ξ±) : map f (of x) = of (f x) := rfl @[to_additive] lemma lift_of_comp_eq_map (f : Ξ± β†’ Ξ²) : lift Ξ± (free_monoid Ξ²) (Ξ» x, of (f x)) = map f := hom_eq $ Ξ» x, rfl @[to_additive] lemma map_comp (g : Ξ² β†’ Ξ³) (f : Ξ± β†’ Ξ²) : map (g ∘ f) = (map g).comp (map f) := hom_eq $ Ξ» x, rfl end free_monoid
b686b42d5ddd301dc330e4247e1542f5d0f51d43
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/algebra/order_functions.lean
44714b2555b2d227ef1c0e4472f0bbdc102bf9ef
[ "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
11,367
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.ordered_group order.lattice open lattice universes u v variables {Ξ± : Type u} {Ξ² : Type v} attribute [simp] max_eq_left max_eq_right min_eq_left min_eq_right /-- A function `f` is strictly monotone if `a < b` implies `f a < f b`. -/ def strict_mono [has_lt Ξ±] [has_lt Ξ²] (f : Ξ± β†’ Ξ²) : Prop := βˆ€ a b, a < b β†’ f a < f b namespace strict_mono open ordering function section variables [linear_order Ξ±] [preorder Ξ²] {f : Ξ± β†’ Ξ²} lemma lt_iff_lt (H : strict_mono f) {a b} : f a < f b ↔ a < b := ⟨λ h, ((lt_trichotomy b a) .resolve_left $ Ξ» h', lt_asymm h $ H _ _ h') .resolve_left $ Ξ» e, ne_of_gt h $ congr_arg _ e, H _ _⟩ lemma injective (H : strict_mono f) : injective f | a b e := ((lt_trichotomy a b) .resolve_left $ Ξ» h, ne_of_lt (H _ _ h) e) .resolve_right $ Ξ» h, ne_of_gt (H _ _ h) e theorem compares (H : strict_mono f) {a b} : βˆ€ {o}, compares o (f a) (f b) ↔ compares o a b | lt := H.lt_iff_lt | eq := ⟨λ h, H.injective h, congr_arg _⟩ | gt := H.lt_iff_lt lemma le_iff_le (H : strict_mono f) {a b} : f a ≀ f b ↔ a ≀ b := ⟨λ h, le_of_not_gt $ Ξ» h', not_le_of_lt (H b a h') h, Ξ» h, (lt_or_eq_of_le h).elim (Ξ» h', le_of_lt (H _ _ h')) (Ξ» h', h' β–Έ le_refl _)⟩ end protected lemma nat {Ξ²} [preorder Ξ²] {f : β„• β†’ Ξ²} (h : βˆ€n, f n < f (n+1)) : strict_mono f := by { intros n m hnm, induction hnm with m' hnm' ih, apply h, exact lt.trans ih (h _) } -- `preorder Ξ±` isn't strong enough: if the preorder on Ξ± is an equivalence relation, -- then `strict_mono f` is vacuously true. lemma monotone [partial_order Ξ±] [preorder Ξ²] {f : Ξ± β†’ Ξ²} (H : strict_mono f) : monotone f := Ξ» a b h, (lt_or_eq_of_le h).rec (le_of_lt ∘ (H _ _)) (by rintro rfl; refl) end strict_mono section open function variables [partial_order Ξ±] [partial_order Ξ²] {f : Ξ± β†’ Ξ²} lemma strict_mono_of_monotone_of_injective (h₁ : monotone f) (hβ‚‚ : injective f) : strict_mono f := Ξ» a b h, begin rw lt_iff_le_and_ne at ⊒ h, exact ⟨h₁ h.1, Ξ» e, h.2 (hβ‚‚ e)⟩ end end section variables [decidable_linear_order Ξ±] [decidable_linear_order Ξ²] {f : Ξ± β†’ Ξ²} {a b c d : Ξ±} -- translate from lattices to linear orders (sup β†’ max, inf β†’ min) @[simp] lemma le_min_iff : c ≀ min a b ↔ c ≀ a ∧ c ≀ b := le_inf_iff @[simp] lemma max_le_iff : max a b ≀ c ↔ a ≀ c ∧ b ≀ c := sup_le_iff lemma max_le_max : a ≀ c β†’ b ≀ d β†’ max a b ≀ max c d := sup_le_sup lemma min_le_min : a ≀ c β†’ b ≀ d β†’ min a b ≀ min c d := inf_le_inf lemma le_max_left_of_le : a ≀ b β†’ a ≀ max b c := le_sup_left_of_le lemma le_max_right_of_le : a ≀ c β†’ a ≀ max b c := le_sup_right_of_le lemma min_le_left_of_le : a ≀ c β†’ min a b ≀ c := inf_le_left_of_le lemma min_le_right_of_le : b ≀ c β†’ min a b ≀ c := inf_le_right_of_le lemma max_min_distrib_left : max a (min b c) = min (max a b) (max a c) := sup_inf_left lemma max_min_distrib_right : max (min a b) c = min (max a c) (max b c) := sup_inf_right lemma min_max_distrib_left : min a (max b c) = max (min a b) (min a c) := inf_sup_left lemma min_max_distrib_right : min (max a b) c = max (min a c) (min b c) := inf_sup_right instance max_idem : is_idempotent Ξ± max := by apply_instance instance min_idem : is_idempotent Ξ± min := by apply_instance @[simp] lemma min_le_iff : min a b ≀ c ↔ a ≀ c ∨ b ≀ c := have a ≀ b β†’ (a ≀ c ∨ b ≀ c ↔ a ≀ c), from assume h, or_iff_left_of_imp $ le_trans h, have b ≀ a β†’ (a ≀ c ∨ b ≀ c ↔ b ≀ c), from assume h, or_iff_right_of_imp $ le_trans h, by cases le_total a b; simp * @[simp] lemma le_max_iff : a ≀ max b c ↔ a ≀ b ∨ a ≀ c := have b ≀ c β†’ (a ≀ b ∨ a ≀ c ↔ a ≀ c), from assume h, or_iff_right_of_imp $ assume h', le_trans h' h, have c ≀ b β†’ (a ≀ b ∨ a ≀ c ↔ a ≀ b), from assume h, or_iff_left_of_imp $ assume h', le_trans h' h, by cases le_total b c; simp * @[simp] lemma max_lt_iff : max a b < c ↔ (a < c ∧ b < c) := by rw [lt_iff_not_ge]; simp [(β‰₯), le_max_iff, not_or_distrib] @[simp] lemma lt_min_iff : a < min b c ↔ (a < b ∧ a < c) := by rw [lt_iff_not_ge]; simp [(β‰₯), min_le_iff, not_or_distrib] @[simp] lemma lt_max_iff : a < max b c ↔ a < b ∨ a < c := by rw [lt_iff_not_ge]; simp [(β‰₯), max_le_iff, not_and_distrib] @[simp] lemma min_lt_iff : min a b < c ↔ a < c ∨ b < c := by rw [lt_iff_not_ge]; simp [(β‰₯), le_min_iff, not_and_distrib] lemma max_lt_max (h₁ : a < c) (hβ‚‚ : b < d) : max a b < max c d := by apply max_lt; simp [lt_max_iff, h₁, hβ‚‚] lemma min_lt_min (h₁ : a < c) (hβ‚‚ : b < d) : min a b < min c d := by apply lt_min; simp [min_lt_iff, h₁, hβ‚‚] theorem min_right_comm (a b c : Ξ±) : min (min a b) c = min (min a c) b := right_comm min min_comm min_assoc a b c theorem max.left_comm (a b c : Ξ±) : max a (max b c) = max b (max a c) := left_comm max max_comm max_assoc a b c theorem max.right_comm (a b c : Ξ±) : max (max a b) c = max (max a c) b := right_comm max max_comm max_assoc a b c lemma max_distrib_of_monotone (hf : monotone f) : f (max a b) = max (f a) (f b) := by cases le_total a b; simp [h, hf h] lemma min_distrib_of_monotone (hf : monotone f) : f (min a b) = min (f a) (f b) := by cases le_total a b; simp [h, hf h] theorem min_choice (a b : Ξ±) : min a b = a ∨ min a b = b := by by_cases h : a ≀ b; simp [min, h] theorem max_choice (a b : Ξ±) : max a b = a ∨ max a b = b := by by_cases h : a ≀ b; simp [max, h] lemma le_of_max_le_left {a b c : Ξ±} (h : max a b ≀ c) : a ≀ c := le_trans (le_max_left _ _) h lemma le_of_max_le_right {a b c : Ξ±} (h : max a b ≀ c) : b ≀ c := le_trans (le_max_right _ _) h end lemma min_add {Ξ± : Type u} [decidable_linear_ordered_comm_group Ξ±] (a b c : Ξ±) : min a b + c = min (a + c) (b + c) := if hle : a ≀ b then have a - c ≀ b - c, from sub_le_sub hle (le_refl _), by simp * at * else have b - c ≀ a - c, from sub_le_sub (le_of_lt (lt_of_not_ge hle)) (le_refl _), by simp * at * lemma min_sub {Ξ± : Type u} [decidable_linear_ordered_comm_group Ξ±] (a b c : Ξ±) : min a b - c = min (a - c) (b - c) := by simp [min_add, sub_eq_add_neg] /- Some lemmas about types that have an ordering and a binary operation, with no rules relating them. -/ lemma fn_min_add_fn_max [decidable_linear_order Ξ±] [add_comm_semigroup Ξ²] (f : Ξ± β†’ Ξ²) (n m : Ξ±) : f (min n m) + f (max n m) = f n + f m := by { cases le_total n m with h h; simp [h] } lemma min_add_max [decidable_linear_order Ξ±] [add_comm_semigroup Ξ±] (n m : Ξ±) : min n m + max n m = n + m := fn_min_add_fn_max id n m lemma fn_min_mul_fn_max [decidable_linear_order Ξ±] [comm_semigroup Ξ²] (f : Ξ± β†’ Ξ²) (n m : Ξ±) : f (min n m) * f (max n m) = f n * f m := by { cases le_total n m with h h; simp [h, mul_comm] } lemma min_mul_max [decidable_linear_order Ξ±] [comm_semigroup Ξ±] (n m : Ξ±) : min n m * max n m = n * m := fn_min_mul_fn_max id n m section decidable_linear_ordered_comm_group variables [decidable_linear_ordered_comm_group Ξ±] {a b c : Ξ±} attribute [simp] abs_zero abs_neg lemma abs_add (a b : Ξ±) : abs (a + b) ≀ abs a + abs b := abs_add_le_abs_add_abs a b theorem abs_le : abs a ≀ b ↔ - b ≀ a ∧ a ≀ b := ⟨assume h, ⟨neg_le_of_neg_le $ le_trans (neg_le_abs_self _) h, le_trans (le_abs_self _) h⟩, assume ⟨h₁, hβ‚‚βŸ©, abs_le_of_le_of_neg_le hβ‚‚ $ neg_le_of_neg_le hβ‚βŸ© lemma abs_lt : abs a < b ↔ - b < a ∧ a < b := ⟨assume h, ⟨neg_lt_of_neg_lt $ lt_of_le_of_lt (neg_le_abs_self _) h, lt_of_le_of_lt (le_abs_self _) h⟩, assume ⟨h₁, hβ‚‚βŸ©, abs_lt_of_lt_of_neg_lt hβ‚‚ $ neg_lt_of_neg_lt hβ‚βŸ© lemma abs_sub_le_iff : abs (a - b) ≀ c ↔ a - b ≀ c ∧ b - a ≀ c := by rw [abs_le, neg_le_sub_iff_le_add, @sub_le_iff_le_add' _ _ b, and_comm] lemma abs_sub_lt_iff : abs (a - b) < c ↔ a - b < c ∧ b - a < c := by rw [abs_lt, neg_lt_sub_iff_lt_add, @sub_lt_iff_lt_add' _ _ b, and_comm] lemma sub_abs_le_abs_sub (a b : Ξ±) : abs a - abs b ≀ abs (a - b) := abs_sub_abs_le_abs_sub a b lemma abs_abs_sub_le_abs_sub (a b : Ξ±) : abs (abs a - abs b) ≀ abs (a - b) := abs_sub_le_iff.2 ⟨sub_abs_le_abs_sub _ _, by rw abs_sub; apply sub_abs_le_abs_sub⟩ lemma abs_eq (hb : 0 ≀ b) : abs a = b ↔ a = b ∨ a = -b := iff.intro begin cases le_total a 0 with a_nonpos a_nonneg, { rw [abs_of_nonpos a_nonpos, neg_eq_iff_neg_eq, eq_comm], exact or.inr }, { rw [abs_of_nonneg a_nonneg, eq_comm], exact or.inl } end (by intro h; cases h; subst h; try { rw abs_neg }; exact abs_of_nonneg hb) @[simp] lemma abs_eq_zero : abs a = 0 ↔ a = 0 := ⟨eq_zero_of_abs_eq_zero, Ξ» e, e.symm β–Έ abs_zero⟩ lemma abs_pos_iff {a : Ξ±} : 0 < abs a ↔ a β‰  0 := ⟨λ h, mt abs_eq_zero.2 (ne_of_gt h), abs_pos_of_ne_zero⟩ @[simp] lemma abs_nonpos_iff {a : Ξ±} : abs a ≀ 0 ↔ a = 0 := by rw [← not_lt, abs_pos_iff, not_not] lemma abs_le_max_abs_abs (hab : a ≀ b) (hbc : b ≀ c) : abs b ≀ max (abs a) (abs c) := abs_le_of_le_of_neg_le (by simp [le_max_iff, le_trans hbc (le_abs_self c)]) (by simp [le_max_iff, le_trans (neg_le_neg hab) (neg_le_abs_self a)]) theorem abs_le_abs {Ξ± : Type*} [decidable_linear_ordered_comm_group Ξ±] {a b : Ξ±} (hβ‚€ : a ≀ b) (h₁ : -a ≀ b) : abs a ≀ abs b := calc abs a ≀ b : by { apply abs_le_of_le_of_neg_le; assumption } ... ≀ abs b : le_abs_self _ lemma min_le_add_of_nonneg_right {a b : Ξ±} (hb : b β‰₯ 0) : min a b ≀ a + b := calc min a b ≀ a : by apply min_le_left ... ≀ a + b : le_add_of_nonneg_right hb lemma min_le_add_of_nonneg_left {a b : Ξ±} (ha : a β‰₯ 0) : min a b ≀ a + b := calc min a b ≀ b : by apply min_le_right ... ≀ a + b : le_add_of_nonneg_left ha lemma max_le_add_of_nonneg {a b : Ξ±} (ha : a β‰₯ 0) (hb : b β‰₯ 0) : max a b ≀ a + b := max_le_iff.2 (by split; simpa) end decidable_linear_ordered_comm_group section decidable_linear_ordered_semiring variables [decidable_linear_ordered_semiring Ξ±] {a b c d : Ξ±} lemma monotone_mul_of_nonneg (ha : 0 ≀ a) : monotone (Ξ» x, a*x) := assume b c b_le_c, mul_le_mul_of_nonneg_left b_le_c ha lemma mul_max_of_nonneg (b c : Ξ±) (ha : 0 ≀ a) : a * max b c = max (a * b) (a * c) := max_distrib_of_monotone (monotone_mul_of_nonneg ha) lemma mul_min_of_nonneg (b c : Ξ±) (ha : 0 ≀ a) : a * min b c = min (a * b) (a * c) := min_distrib_of_monotone (monotone_mul_of_nonneg ha) end decidable_linear_ordered_semiring section decidable_linear_ordered_comm_ring variables [decidable_linear_ordered_comm_ring Ξ±] {a b c d : Ξ±} @[simp] lemma abs_one : abs (1 : Ξ±) = 1 := abs_of_pos zero_lt_one lemma max_mul_mul_le_max_mul_max (b c : Ξ±) (ha : 0 ≀ a) (hd: 0 ≀ d) : max (a * b) (d * c) ≀ max a c * max d b := have ba : b * a ≀ max d b * max c a, from mul_le_mul (le_max_right d b) (le_max_right c a) ha (le_trans hd (le_max_left d b)), have cd : c * d ≀ max a c * max b d, from mul_le_mul (le_max_right a c) (le_max_right b d) hd (le_trans ha (le_max_left a c)), max_le (by simpa [mul_comm, max_comm] using ba) (by simpa [mul_comm, max_comm] using cd) end decidable_linear_ordered_comm_ring
0ed7380649cde82ba5afb1356f72c02c640b4b22
c17c60327eee1622a8d7defa5af340e08619107f
/src/snippets/tactics/inegalites.lean
df98055c88a5248b6fbd6eeae3f048b0c4ecc780
[]
no_license
FredericLeRoux/dEAduction-lean2
277e3aad5102ff155fb04b188dbd01568ceea005
bf7d7d88c2511ecfda5a98ed96e4ca3bc7ae1151
refs/heads/master
1,667,931,697,036
1,593,174,880,000
1,593,174,880,000
275,150,842
0
0
null
null
null
null
UTF-8
Lean
false
false
1,002
lean
import tactic.linarith /- Tactique Γ©crite par Mario, qui aojute au contexte que les carrΓ©s sont positifs et que les produits de nb positifs sont positifs avant d'essayer linarith AFER : ajouter les inverses de positifs -/ namespace tactic meta def find_squares : expr β†’ tactic unit | e@`(%%a ^ 2) := do find_squares a, try (do p ← mk_app ``pow_two_nonneg [a], t ← infer_type p, assertv `h t p) >> skip | e := () <$ e.traverse (Ξ» e, e <$ find_squares e) meta def nra : tactic unit := do ls ← local_context, ls' ← ls.mfoldr (Ξ» h l, do t ← infer_type h, find_squares t, match t with | `(0 ≀ %%a) := return (h :: l) | _ := return l end) [], target >>= find_squares, ls'.mmap' (Ξ» a, ls'.mmap' $ Ξ» b, do p ← mk_app ``mul_nonneg [a, b], t ← infer_type p, assertv `h t p), tactic.interactive.linarith none none none example {Ξ±:Type} [linear_ordered_comm_ring Ξ±] (a b : Ξ±) : 0 ≀ a ^ 2 + b ^ 2 := by nra end tactic
220dbff69ec318c1043e3aaf41d728e5ad372c78
95dcf8dea2baf2b4b0a60d438f27c35ae3dd3990
/src/category_theory/comma.lean
86d18ad61ed958803daaa0e41ce638566699e3a4
[ "Apache-2.0" ]
permissive
uniformity1/mathlib
829341bad9dfa6d6be9adaacb8086a8a492e85a4
dd0e9bd8f2e5ec267f68e72336f6973311909105
refs/heads/master
1,588,592,015,670
1,554,219,842,000
1,554,219,842,000
179,110,702
0
0
Apache-2.0
1,554,220,076,000
1,554,220,076,000
null
UTF-8
Lean
false
false
11,561
lean
-- Copyright (c) 2018 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison, Johan Commelin import category_theory.types import category_theory.isomorphism import category_theory.whiskering import category_theory.opposites import category_theory.punit import category_theory.equivalence namespace category_theory universes v₁ vβ‚‚ v₃ u₁ uβ‚‚ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {A : Sort u₁} [π’œ : category.{v₁} A] variables {B : Sort uβ‚‚} [ℬ : category.{vβ‚‚} B] variables {T : Sort u₃} [𝒯 : category.{v₃} T] include π’œ ℬ 𝒯 structure comma (L : A β₯€ T) (R : B β₯€ T) := (left : A . obviously) (right : B . obviously) (hom : L.obj left ⟢ R.obj right) variables {L : A β₯€ T} {R : B β₯€ T} structure comma_morphism (X Y : comma L R) := (left : X.left ⟢ Y.left . obviously) (right : X.right ⟢ Y.right . obviously) (w' : L.map left ≫ Y.hom = X.hom ≫ R.map right . obviously) restate_axiom comma_morphism.w' attribute [simp] comma_morphism.w namespace comma_morphism @[extensionality] lemma ext {X Y : comma L R} {f g : comma_morphism X Y} (l : f.left = g.left) (r : f.right = g.right) : f = g := begin cases f, cases g, congr; assumption end end comma_morphism instance comma_category : category (comma L R) := { hom := comma_morphism, id := Ξ» X, { left := πŸ™ X.left, right := πŸ™ X.right }, comp := Ξ» X Y Z f g, { left := f.left ≫ g.left, right := f.right ≫ g.right, w' := begin rw [functor.map_comp, category.assoc, g.w, ←category.assoc, f.w, functor.map_comp, category.assoc], end }} namespace comma section variables {X Y Z : comma L R} {f : X ⟢ Y} {g : Y ⟢ Z} @[simp] lemma comp_left : (f ≫ g).left = f.left ≫ g.left := rfl @[simp] lemma comp_right : (f ≫ g).right = f.right ≫ g.right := rfl end variables (L) (R) def fst : comma L R β₯€ A := { obj := Ξ» X, X.left, map := Ξ» _ _ f, f.left } def snd : comma L R β₯€ B := { obj := Ξ» X, X.right, map := Ξ» _ _ f, f.right } @[simp] lemma fst_obj {X : comma L R} : (fst L R).obj X = X.left := rfl @[simp] lemma snd_obj {X : comma L R} : (snd L R).obj X = X.right := rfl @[simp] lemma fst_map {X Y : comma L R} {f : X ⟢ Y} : (fst L R).map f = f.left := rfl @[simp] lemma snd_map {X Y : comma L R} {f : X ⟢ Y} : (snd L R).map f = f.right := rfl def nat_trans : fst L R β‹™ L ⟹ snd L R β‹™ R := { app := Ξ» X, X.hom } section variables {L₁ Lβ‚‚ L₃ : A β₯€ T} {R₁ Rβ‚‚ R₃ : B β₯€ T} def map_left (l : L₁ ⟹ Lβ‚‚) : comma Lβ‚‚ R β₯€ comma L₁ R := { obj := Ξ» X, { left := X.left, right := X.right, hom := l.app X.left ≫ X.hom }, map := Ξ» X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←category.assoc, l.naturality f.left, category.assoc]; tidy } } section variables {X Y : comma Lβ‚‚ R} {f : X ⟢ Y} {l : L₁ ⟹ Lβ‚‚} @[simp] lemma map_left_obj_left : ((map_left R l).obj X).left = X.left := rfl @[simp] lemma map_left_obj_right : ((map_left R l).obj X).right = X.right := rfl @[simp] lemma map_left_obj_hom : ((map_left R l).obj X).hom = l.app X.left ≫ X.hom := rfl @[simp] lemma map_left_map_left : ((map_left R l).map f).left = f.left := rfl @[simp] lemma map_left_map_right : ((map_left R l).map f).right = f.right := rfl end def map_left_id : map_left R (πŸ™ L) β‰… functor.id _ := { hom := { app := Ξ» X, { left := πŸ™ _, right := πŸ™ _ } }, inv := { app := Ξ» X, { left := πŸ™ _, right := πŸ™ _ } } } section variables {X : comma L R} @[simp] lemma map_left_id_hom_app_left : (((map_left_id L R).hom).app X).left = πŸ™ (X.left) := rfl @[simp] lemma map_left_id_hom_app_right : (((map_left_id L R).hom).app X).right = πŸ™ (X.right) := rfl @[simp] lemma map_left_id_inv_app_left : (((map_left_id L R).inv).app X).left = πŸ™ (X.left) := rfl @[simp] lemma map_left_id_inv_app_right : (((map_left_id L R).inv).app X).right = πŸ™ (X.right) := rfl end def map_left_comp (l : L₁ ⟹ Lβ‚‚) (l' : Lβ‚‚ ⟹ L₃) : (map_left R (l ⊟ l')) β‰… (map_left R l') β‹™ (map_left R l) := { hom := { app := Ξ» X, { left := πŸ™ _, right := πŸ™ _ } }, inv := { app := Ξ» X, { left := πŸ™ _, right := πŸ™ _ } } } section variables {X : comma L₃ R} {l : L₁ ⟹ Lβ‚‚} {l' : Lβ‚‚ ⟹ L₃} @[simp] lemma map_left_comp_hom_app_left : (((map_left_comp R l l').hom).app X).left = πŸ™ (X.left) := rfl @[simp] lemma map_left_comp_hom_app_right : (((map_left_comp R l l').hom).app X).right = πŸ™ (X.right) := rfl @[simp] lemma map_left_comp_inv_app_left : (((map_left_comp R l l').inv).app X).left = πŸ™ (X.left) := rfl @[simp] lemma map_left_comp_inv_app_right : (((map_left_comp R l l').inv).app X).right = πŸ™ (X.right) := rfl end def map_right (r : R₁ ⟹ Rβ‚‚) : comma L R₁ β₯€ comma L Rβ‚‚ := { obj := Ξ» X, { left := X.left, right := X.right, hom := X.hom ≫ r.app X.right }, map := Ξ» X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←r.naturality f.right, ←category.assoc]; tidy } } section variables {X Y : comma L R₁} {f : X ⟢ Y} {r : R₁ ⟹ Rβ‚‚} @[simp] lemma map_right_obj_left : ((map_right L r).obj X).left = X.left := rfl @[simp] lemma map_right_obj_right : ((map_right L r).obj X).right = X.right := rfl @[simp] lemma map_right_obj_hom : ((map_right L r).obj X).hom = X.hom ≫ r.app X.right := rfl @[simp] lemma map_right_map_left : ((map_right L r).map f).left = f.left := rfl @[simp] lemma map_right_map_right : ((map_right L r).map f).right = f.right := rfl end def map_right_id : map_right L (πŸ™ R) β‰… functor.id _ := { hom := { app := Ξ» X, { left := πŸ™ _, right := πŸ™ _ } }, inv := { app := Ξ» X, { left := πŸ™ _, right := πŸ™ _ } } } section variables {X : comma L R} @[simp] lemma map_right_id_hom_app_left : (((map_right_id L R).hom).app X).left = πŸ™ (X.left) := rfl @[simp] lemma map_right_id_hom_app_right : (((map_right_id L R).hom).app X).right = πŸ™ (X.right) := rfl @[simp] lemma map_right_id_inv_app_left : (((map_right_id L R).inv).app X).left = πŸ™ (X.left) := rfl @[simp] lemma map_right_id_inv_app_right : (((map_right_id L R).inv).app X).right = πŸ™ (X.right) := rfl end def map_right_comp (r : R₁ ⟹ Rβ‚‚) (r' : Rβ‚‚ ⟹ R₃) : (map_right L (r ⊟ r')) β‰… (map_right L r) β‹™ (map_right L r') := { hom := { app := Ξ» X, { left := πŸ™ _, right := πŸ™ _ } }, inv := { app := Ξ» X, { left := πŸ™ _, right := πŸ™ _ } } } section variables {X : comma L R₁} {r : R₁ ⟹ Rβ‚‚} {r' : Rβ‚‚ ⟹ R₃} @[simp] lemma map_right_comp_hom_app_left : (((map_right_comp L r r').hom).app X).left = πŸ™ (X.left) := rfl @[simp] lemma map_right_comp_hom_app_right : (((map_right_comp L r r').hom).app X).right = πŸ™ (X.right) := rfl @[simp] lemma map_right_comp_inv_app_left : (((map_right_comp L r r').inv).app X).left = πŸ™ (X.left) := rfl @[simp] lemma map_right_comp_inv_app_right : (((map_right_comp L r r').inv).app X).right = πŸ™ (X.right) := rfl end end end comma omit π’œ ℬ def over (X : T) := comma.{v₃ 1 v₃} (functor.id T) (functor.of.obj X) namespace over variables {X : T} instance category : category (over X) := by delta over; apply_instance @[extensionality] lemma over_morphism.ext {X : T} {U V : over X} {f g : U ⟢ V} (h : f.left = g.left) : f = g := by tidy @[simp] lemma over_right (U : over X) : U.right = punit.star := by tidy @[simp] lemma over_morphism_right {U V : over X} (f : U ⟢ V) : f.right = πŸ™ punit.star := by tidy @[simp] lemma id_left (U : over X) : comma_morphism.left (πŸ™ U) = πŸ™ U.left := rfl @[simp] lemma comp_left (a b c : over X) (f : a ⟢ b) (g : b ⟢ c) : (f ≫ g).left = f.left ≫ g.left := rfl @[simp] lemma w {A B : over X} (f : A ⟢ B) : f.left ≫ B.hom = A.hom := by have := f.w; tidy def mk {X Y : T} (f : Y ⟢ X) : over X := { left := Y, hom := f } @[simp] lemma mk_left {X Y : T} (f : Y ⟢ X) : (mk f).left = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : Y ⟢ X) : (mk f).hom = f := rfl def hom_mk {U V : over X} (f : U.left ⟢ V.left) (w : f ≫ V.hom = U.hom . obviously) : U ⟢ V := { left := f } @[simp] lemma hom_mk_left {U V : over X} (f : U.left ⟢ V.left) (w : f ≫ V.hom = U.hom) : (hom_mk f).left = f := rfl def forget : (over X) β₯€ T := comma.fst _ _ @[simp] lemma forget_obj {U : over X} : forget.obj U = U.left := rfl @[simp] lemma forget_map {U V : over X} {f : U ⟢ V} : forget.map f = f.left := rfl def map {Y : T} (f : X ⟢ Y) : over X β₯€ over Y := comma.map_right _ $ functor.of.map f section variables {Y : T} {f : X ⟢ Y} {U V : over X} {g : U ⟢ V} @[simp] lemma map_obj_left : ((map f).obj U).left = U.left := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = U.hom ≫ f := rfl @[simp] lemma map_map_left : ((map f).map g).left = g.left := rfl end section variables {D : Sort u₃} [π’Ÿ : category.{v₃} D] include π’Ÿ def post (F : T β₯€ D) : over X β₯€ over (F.obj X) := { obj := Ξ» Y, mk $ F.map Y.hom, map := Ξ» Y₁ Yβ‚‚ f, { left := F.map f.left, w' := by tidy; erw [← F.map_comp, w] } } end end over def under (X : T) := comma.{1 v₃ v₃} (functor.of.obj X) (functor.id T) namespace under variables {X : T} instance : category (under X) := by delta under; apply_instance @[extensionality] lemma under_morphism.ext {X : T} {U V : under X} {f g : U ⟢ V} (h : f.right = g.right) : f = g := by tidy @[simp] lemma under_left (U : under X) : U.left = punit.star := by tidy @[simp] lemma under_morphism_left {U V : under X} (f : U ⟢ V) : f.left = πŸ™ punit.star := by tidy @[simp] lemma id_right (U : under X) : comma_morphism.right (πŸ™ U) = πŸ™ U.right := rfl @[simp] lemma comp_right (a b c : under X) (f : a ⟢ b) (g : b ⟢ c) : (f ≫ g).right = f.right ≫ g.right := rfl @[simp] lemma w {A B : under X} (f : A ⟢ B) : A.hom ≫ f.right = B.hom := by have := f.w; tidy def mk {X Y : T} (f : X ⟢ Y) : under X := { right := Y, hom := f } @[simp] lemma mk_right {X Y : T} (f : X ⟢ Y) : (mk f).right = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : X ⟢ Y) : (mk f).hom = f := rfl def hom_mk {U V : under X} (f : U.right ⟢ V.right) (w : U.hom ≫ f = V.hom . obviously) : U ⟢ V := { right := f } @[simp] lemma hom_mk_right {U V : under X} (f : U.right ⟢ V.right) (w : U.hom ≫ f = V.hom) : (hom_mk f).right = f := rfl def forget : (under X) β₯€ T := comma.snd _ _ @[simp] lemma forget_obj {U : under X} : forget.obj U = U.right := rfl @[simp] lemma forget_map {U V : under X} {f : U ⟢ V} : forget.map f = f.right := rfl def map {Y : T} (f : X ⟢ Y) : under Y β₯€ under X := comma.map_left _ $ functor.of.map f section variables {Y : T} {f : X ⟢ Y} {U V : under Y} {g : U ⟢ V} @[simp] lemma map_obj_right : ((map f).obj U).right = U.right := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = f ≫ U.hom := rfl @[simp] lemma map_map_right : ((map f).map g).right = g.right := rfl end section variables {D : Sort u₃} [π’Ÿ : category.{v₃} D] include π’Ÿ def post {X : T} (F : T β₯€ D) : under X β₯€ under (F.obj X) := { obj := Ξ» Y, mk $ F.map Y.hom, map := Ξ» Y₁ Yβ‚‚ f, { right := F.map f.right, w' := by tidy; erw [← F.map_comp, w] } } end end under end category_theory
324c6e25c5eda7fedd24ae1e183e840b7e079019
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/category/Group/filtered_colimits.lean
52be1744411f11d770674ad6b55353117c1b1e79
[ "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
8,557
lean
/- Copyright (c) 2021 Justus Springer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Justus Springer -/ import algebra.category.Group.basic import algebra.category.Mon.filtered_colimits /-! # The forgetful functor from (commutative) (additive) groups preserves filtered colimits. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Forgetful functors from algebraic categories usually don't preserve colimits. However, they tend to preserve _filtered_ colimits. In this file, we start with a small filtered category `J` and a functor `F : J β₯€ Group`. We show that the colimit of `F β‹™ forgetβ‚‚ Group Mon` (in `Mon`) carries the structure of a group, thereby showing that the forgetful functor `forgetβ‚‚ Group Mon` preserves filtered colimits. In particular, this implies that `forget Group` preserves filtered colimits. Similarly for `AddGroup`, `CommGroup` and `AddCommGroup`. -/ universes v u noncomputable theory open_locale classical open category_theory open category_theory.limits open category_theory.is_filtered (renaming max β†’ max') -- avoid name collision with `_root_.max`. namespace Group.filtered_colimits section open Mon.filtered_colimits (colimit_one_eq colimit_mul_mk_eq) -- We use parameters here, mainly so we can have the abbreviations `G` and `G.mk` below, without -- passing around `F` all the time. parameters {J : Type v} [small_category J] [is_filtered J] (F : J β₯€ Group.{max v u}) /-- The colimit of `F β‹™ forgetβ‚‚ Group Mon` in the category `Mon`. In the following, we will show that this has the structure of a group. -/ @[to_additive "The colimit of `F β‹™ forgetβ‚‚ AddGroup AddMon` in the category `AddMon`. In the following, we will show that this has the structure of an additive group."] abbreviation G : Mon := Mon.filtered_colimits.colimit (F β‹™ forgetβ‚‚ Group Mon.{max v u}) /-- The canonical projection into the colimit, as a quotient type. -/ @[to_additive "The canonical projection into the colimit, as a quotient type."] abbreviation G.mk : (Ξ£ j, F.obj j) β†’ G := quot.mk (types.quot.rel (F β‹™ forget Group)) @[to_additive] lemma G.mk_eq (x y : Ξ£ j, F.obj j) (h : βˆƒ (k : J) (f : x.1 ⟢ k) (g : y.1 ⟢ k), F.map f x.2 = F.map g y.2) : G.mk x = G.mk y := quot.eqv_gen_sound (types.filtered_colimit.eqv_gen_quot_rel_of_rel (F β‹™ forget Group) x y h) /-- The "unlifted" version of taking inverses in the colimit. -/ @[to_additive "The \"unlifted\" version of negation in the colimit."] def colimit_inv_aux (x : Ξ£ j, F.obj j) : G := G.mk ⟨x.1, x.2 ⁻¹⟩ @[to_additive] lemma colimit_inv_aux_eq_of_rel (x y : Ξ£ j, F.obj j) (h : types.filtered_colimit.rel (F β‹™ forget Group) x y) : colimit_inv_aux x = colimit_inv_aux y := begin apply G.mk_eq, obtain ⟨k, f, g, hfg⟩ := h, use [k, f, g], rw [monoid_hom.map_inv, monoid_hom.map_inv, inv_inj], exact hfg, end /-- Taking inverses in the colimit. See also `colimit_inv_aux`. -/ @[to_additive "Negation in the colimit. See also `colimit_neg_aux`."] instance colimit_has_inv : has_inv G := { inv := Ξ» x, begin refine quot.lift (colimit_inv_aux F) _ x, intros x y h, apply colimit_inv_aux_eq_of_rel, apply types.filtered_colimit.rel_of_quot_rel, exact h, end } @[simp, to_additive] lemma colimit_inv_mk_eq (x : Ξ£ j, F.obj j) : (G.mk x) ⁻¹ = G.mk ⟨x.1, x.2 ⁻¹⟩ := rfl @[to_additive] instance colimit_group : group G := { mul_left_inv := Ξ» x, begin apply quot.induction_on x, clear x, intro x, cases x with j x, erw [colimit_inv_mk_eq, colimit_mul_mk_eq (F β‹™ forgetβ‚‚ Group Mon.{max v u}) ⟨j, _⟩ ⟨j, _⟩ j (πŸ™ j) (πŸ™ j), colimit_one_eq (F β‹™ forgetβ‚‚ Group Mon.{max v u}) j], dsimp, simp only [category_theory.functor.map_id, id_apply, mul_left_inv], end, .. G.monoid, .. colimit_has_inv } /-- The bundled group giving the filtered colimit of a diagram. -/ @[to_additive "The bundled additive group giving the filtered colimit of a diagram."] def colimit : Group := Group.of G /-- The cocone over the proposed colimit group. -/ @[to_additive "The cocone over the proposed colimit additive group."] def colimit_cocone : cocone F := { X := colimit, ΞΉ := { ..(Mon.filtered_colimits.colimit_cocone (F β‹™ forgetβ‚‚ Group Mon.{max v u})).ΞΉ } } /-- The proposed colimit cocone is a colimit in `Group`. -/ @[to_additive "The proposed colimit cocone is a colimit in `AddGroup`."] def colimit_cocone_is_colimit : is_colimit colimit_cocone := { desc := Ξ» t, Mon.filtered_colimits.colimit_desc (F β‹™ forgetβ‚‚ Group Mon.{max v u}) ((forgetβ‚‚ Group Mon).map_cocone t), fac' := Ξ» t j, monoid_hom.coe_inj $ (types.colimit_cocone_is_colimit (F β‹™ forget Group)).fac ((forget Group).map_cocone t) j, uniq' := Ξ» t m h, monoid_hom.coe_inj $ (types.colimit_cocone_is_colimit (F β‹™ forget Group)).uniq ((forget Group).map_cocone t) m ((Ξ» j, funext $ Ξ» x, monoid_hom.congr_fun (h j) x)) } @[to_additive forgetβ‚‚_AddMon_preserves_filtered_colimits] instance forgetβ‚‚_Mon_preserves_filtered_colimits : preserves_filtered_colimits (forgetβ‚‚ Group Mon.{u}) := { preserves_filtered_colimits := Ξ» J _ _, by exactI { preserves_colimit := Ξ» F, preserves_colimit_of_preserves_colimit_cocone (colimit_cocone_is_colimit.{u u} F) (Mon.filtered_colimits.colimit_cocone_is_colimit (F β‹™ forgetβ‚‚ Group Mon.{u})) } } @[to_additive] instance forget_preserves_filtered_colimits : preserves_filtered_colimits (forget Group.{u}) := limits.comp_preserves_filtered_colimits (forgetβ‚‚ Group Mon) (forget Mon.{u}) end end Group.filtered_colimits namespace CommGroup.filtered_colimits section -- We use parameters here, mainly so we can have the abbreviation `G` below, without -- passing around `F` all the time. parameters {J : Type v} [small_category J] [is_filtered J] (F : J β₯€ CommGroup.{max v u}) /-- The colimit of `F β‹™ forgetβ‚‚ CommGroup Group` in the category `Group`. In the following, we will show that this has the structure of a _commutative_ group. -/ @[to_additive "The colimit of `F β‹™ forgetβ‚‚ AddCommGroup AddGroup` in the category `AddGroup`. In the following, we will show that this has the structure of a _commutative_ additive group."] abbreviation G : Group := Group.filtered_colimits.colimit (F β‹™ forgetβ‚‚ CommGroup Group.{max v u}) @[to_additive] instance colimit_comm_group : comm_group G := { ..G.group, ..CommMon.filtered_colimits.colimit_comm_monoid (F β‹™ forgetβ‚‚ CommGroup CommMon.{max v u}) } /-- The bundled commutative group giving the filtered colimit of a diagram. -/ @[to_additive "The bundled additive commutative group giving the filtered colimit of a diagram."] def colimit : CommGroup := CommGroup.of G /-- The cocone over the proposed colimit commutative group. -/ @[to_additive "The cocone over the proposed colimit additive commutative group."] def colimit_cocone : cocone F := { X := colimit, ΞΉ := { ..(Group.filtered_colimits.colimit_cocone (F β‹™ forgetβ‚‚ CommGroup Group.{max v u})).ΞΉ } } /-- The proposed colimit cocone is a colimit in `CommGroup`. -/ @[to_additive "The proposed colimit cocone is a colimit in `AddCommGroup`."] def colimit_cocone_is_colimit : is_colimit colimit_cocone := { desc := Ξ» t, (Group.filtered_colimits.colimit_cocone_is_colimit (F β‹™ forgetβ‚‚ CommGroup Group.{max v u})).desc ((forgetβ‚‚ CommGroup Group.{max v u}).map_cocone t), fac' := Ξ» t j, monoid_hom.coe_inj $ (types.colimit_cocone_is_colimit (F β‹™ forget CommGroup)).fac ((forget CommGroup).map_cocone t) j, uniq' := Ξ» t m h, monoid_hom.coe_inj $ (types.colimit_cocone_is_colimit (F β‹™ forget CommGroup)).uniq ((forget CommGroup).map_cocone t) m ((Ξ» j, funext $ Ξ» x, monoid_hom.congr_fun (h j) x)) } @[to_additive forgetβ‚‚_AddGroup_preserves_filtered_colimits] instance forgetβ‚‚_Group_preserves_filtered_colimits : preserves_filtered_colimits (forgetβ‚‚ CommGroup Group.{u}) := { preserves_filtered_colimits := Ξ» J _ _, by exactI { preserves_colimit := Ξ» F, preserves_colimit_of_preserves_colimit_cocone (colimit_cocone_is_colimit.{u u} F) (Group.filtered_colimits.colimit_cocone_is_colimit (F β‹™ forgetβ‚‚ CommGroup Group.{u})) } } @[to_additive] instance forget_preserves_filtered_colimits : preserves_filtered_colimits (forget CommGroup.{u}) := limits.comp_preserves_filtered_colimits (forgetβ‚‚ CommGroup Group) (forget Group.{u}) end end CommGroup.filtered_colimits
bcd45c710b7382e6f1386e835af1e9714d71817b
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/PrettyPrinter/Basic.lean
2743c66af78ae8f50069724a64a39fd449407a76
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
1,097
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.InternalExceptionId import Lean.KeyedDeclsAttribute namespace Lean namespace PrettyPrinter /- Auxiliary internal exception for backtracking the pretty printer. See `orelse.parenthesizer` for example -/ builtin_initialize backtrackExceptionId : InternalExceptionId ← registerInternalExceptionId `backtrackFormatter unsafe def runForNodeKind {Ξ±} (attr : KeyedDeclsAttribute Ξ±) (k : SyntaxNodeKind) (interp : ParserDescr β†’ CoreM Ξ±) : CoreM Ξ± := do match attr.getValues (← getEnv) k with | p::_ => pure p | _ => -- assume `k` is from a `ParserDescr`, in which case we assume it's also the declaration name let info ← getConstInfo k if info.type.isConstOf ``ParserDescr || info.type.isConstOf ``TrailingParserDescr then let d ← evalConst ParserDescr k interp d else throwError "no declaration of attribute [{attr.defn.name}] found for '{k}'" end PrettyPrinter end Lean
faef9f080ce8e7ac9378598aa246eb0ff27f4207
968e2f50b755d3048175f176376eff7139e9df70
/examples/prop_logic_theory/unnamed_2274.lean
9eb66c8bb20afc30e82e1d69bd75a758b1662869
[]
no_license
gihanmarasingha/mth1001_sphinx
190a003269ba5e54717b448302a27ca26e31d491
05126586cbf5786e521be1ea2ef5b4ba3c44e74a
refs/heads/master
1,672,913,933,677
1,604,516,583,000
1,604,516,583,000
309,245,750
1
0
null
null
null
null
UTF-8
Lean
false
false
300
lean
variables {p q : Prop} -- BEGIN theorem not_and_not_of_not_or : Β¬(p ∨ q) β†’ Β¬p ∧ Β¬q := begin intro h₁, -- Assume `h₁ : Β¬(p ∨ q)`. It suffices to prove `Β¬p ∧ Β¬q`. split, -- By and introduction, it suffices to prove 1. `Β¬p` and 2. `Β¬q`. { sorry }, { sorry }, end -- END
4680afdbf69892a5673d070b804c37ab0d29002a
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/ring_theory/integral_closure.lean
eaf02569eb938436eb22e59b5f0ccca4f35c41f1
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
26,301
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 ring_theory.algebra_tower import ring_theory.polynomial.scale_roots /-! # Integral closure of a subring. If A is an R-algebra then `a : A` is integral over R if it is a root of a monic polynomial with coefficients in R. Enough theory is developed to prove that integral elements form a sub-R-algebra of A. ## Main definitions Let `R` be a `comm_ring` and let `A` be an R-algebra. * `ring_hom.is_integral_elem (f : R β†’+* A) (x : A)` : `x` is integral with respect to the map `f`, * `is_integral (x : A)` : `x` is integral over `R`, i.e., is a root of a monic polynomial with coefficients in `R`. * `integral_closure R A` : the integral closure of `R` in `A`, regarded as a sub-`R`-algebra of `A`. -/ open_locale classical open_locale big_operators open polynomial submodule section ring variables {R S A : Type*} variables [comm_ring R] [ring A] [ring S] (f : R β†’+* S) /-- An element `x` of `A` is said to be integral over `R` with respect to `f` if it is a root of a monic polynomial `p : polynomial R` evaluated under `f` -/ def ring_hom.is_integral_elem (f : R β†’+* A) (x : A) := βˆƒ p : polynomial R, monic p ∧ evalβ‚‚ f x p = 0 /-- A ring homomorphism `f : R β†’+* A` is said to be integral if every element `A` is integral with respect to the map `f` -/ def ring_hom.is_integral (f : R β†’+* A) := βˆ€ x : A, f.is_integral_elem x variables [algebra R A] (R) /-- An element `x` of an algebra `A` over a commutative ring `R` is said to be *integral*, if it is a root of some monic polynomial `p : polynomial R`. Equivalently, the element is integral over `R` with respect to the induced `algebra_map` -/ def is_integral (x : A) : Prop := (algebra_map R A).is_integral_elem x variable (A) /-- An algebra is integral if every element of the extension is integral over the base ring -/ def algebra.is_integral : Prop := (algebra_map R A).is_integral variables {R A} lemma ring_hom.is_integral_map {x : R} : f.is_integral_elem (f x) := ⟨X - C x, monic_X_sub_C _, by simp⟩ theorem is_integral_algebra_map {x : R} : is_integral R (algebra_map R A x) := (algebra_map R A).is_integral_map theorem is_integral_of_noetherian (H : is_noetherian R A) (x : A) : is_integral R x := begin let leval : @linear_map R (polynomial R) A _ _ _ _ _ := (aeval x).to_linear_map, let D : β„• β†’ submodule R A := Ξ» n, (degree_le R n).map leval, let M := well_founded.min (is_noetherian_iff_well_founded.1 H) (set.range D) ⟨_, ⟨0, rfl⟩⟩, have HM : M ∈ set.range D := well_founded.min_mem _ _ _, cases HM with N HN, have HM : Β¬M < D (N+1) := well_founded.not_lt_min (is_noetherian_iff_well_founded.1 H) (set.range D) _ ⟨N+1, rfl⟩, rw ← HN at HM, have HN2 : D (N+1) ≀ D N := classical.by_contradiction (Ξ» H, HM (lt_of_le_not_le (map_mono (degree_le_mono (with_bot.coe_le_coe.2 (nat.le_succ N)))) H)), have HN3 : leval (X^(N+1)) ∈ D N, { exact HN2 (mem_map_of_mem (mem_degree_le.2 (degree_X_pow_le _))) }, rcases HN3 with ⟨p, hdp, hpe⟩, refine ⟨X^(N+1) - p, monic_X_pow_sub (mem_degree_le.1 hdp), _⟩, show leval (X ^ (N + 1) - p) = 0, rw [linear_map.map_sub, hpe, sub_self] end theorem is_integral_of_submodule_noetherian (S : subalgebra R A) (H : is_noetherian R (S : submodule R A)) (x : A) (hx : x ∈ S) : is_integral R x := begin letI : algebra R S := S.algebra, letI : ring S := S.ring R A, suffices : is_integral R (⟨x, hx⟩ : S), { rcases this with ⟨p, hpm, hpx⟩, replace hpx := congr_arg subtype.val hpx, refine ⟨p, hpm, eq.trans _ hpx⟩, simp only [aeval_def, evalβ‚‚, finsupp.sum], rw ← p.support.sum_hom subtype.val, { refine finset.sum_congr rfl (Ξ» n hn, _), change _ = _ * _, rw is_monoid_hom.map_pow coe, refl, split; intros; refl }, refine { map_add := _, map_zero := _ }; intros; refl }, refine is_integral_of_noetherian H ⟨x, hx⟩ end end ring section variables {R A B S : Type*} variables [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring S] variables [algebra R A] [algebra R B] (f : R β†’+* S) theorem is_integral_alg_hom (f : A →ₐ[R] B) {x : A} (hx : is_integral R x) : is_integral R (f x) := let ⟨p, hp, hpx⟩ := hx in ⟨p, hp, by rw [← aeval_def, aeval_alg_hom_apply, aeval_def, hpx, f.map_zero]⟩ theorem is_integral_of_is_scalar_tower [algebra A B] [is_scalar_tower R A B] (x : B) (hx : is_integral R x) : is_integral A x := let ⟨p, hp, hpx⟩ := hx in ⟨p.map $ algebra_map R A, monic_map _ hp, by rw [← aeval_def, ← is_scalar_tower.aeval_apply, aeval_def, hpx]⟩ section local attribute [instance] subset.comm_ring algebra.of_is_subring theorem is_integral_of_subring {x : A} (T : set R) [is_subring T] (hx : is_integral T x) : is_integral R x := is_integral_of_is_scalar_tower x hx lemma is_integral_algebra_map_iff [algebra A B] [is_scalar_tower R A B] {x : A} (hAB : function.injective (algebra_map A B)) : is_integral R (algebra_map A B x) ↔ is_integral R x := begin split; rintros ⟨f, hf, hx⟩; use [f, hf], { exact is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero R A B hAB hx }, { rw [is_scalar_tower.algebra_map_eq R A B, ← hom_evalβ‚‚, hx, ring_hom.map_zero] } end theorem is_integral_iff_is_integral_closure_finite {r : A} : is_integral R r ↔ βˆƒ s : set R, s.finite ∧ is_integral (ring.closure s) r := begin split; intro hr, { rcases hr with ⟨p, hmp, hpr⟩, refine ⟨_, set.finite_mem_finset _, p.restriction, subtype.eq hmp, _⟩, erw [← aeval_def, is_scalar_tower.aeval_apply _ R, map_restriction, aeval_def, hpr] }, rcases hr with ⟨s, hs, hsr⟩, exact is_integral_of_subring _ hsr end end theorem fg_adjoin_singleton_of_integral (x : A) (hx : is_integral R x) : (algebra.adjoin R ({x} : set A) : submodule R A).fg := begin rcases hx with ⟨f, hfm, hfx⟩, existsi finset.image ((^) x) (finset.range (nat_degree f + 1)), apply le_antisymm, { rw span_le, intros s hs, rw finset.mem_coe at hs, rcases finset.mem_image.1 hs with ⟨k, hk, rfl⟩, clear hk, exact is_submonoid.pow_mem (algebra.subset_adjoin (set.mem_singleton _)) }, intros r hr, change r ∈ algebra.adjoin R ({x} : set A) at hr, rw algebra.adjoin_singleton_eq_range at hr, rcases (aeval x).mem_range.mp hr with ⟨p, rfl⟩, rw ← mod_by_monic_add_div p hfm, rw ← aeval_def at hfx, rw [alg_hom.map_add, alg_hom.map_mul, hfx, zero_mul, add_zero], have : degree (p %β‚˜ f) ≀ degree f := degree_mod_by_monic_le p hfm, generalize_hyp : p %β‚˜ f = q at this ⊒, rw [← sum_C_mul_X_eq q, aeval_def, evalβ‚‚_sum, finsupp.sum], refine sum_mem _ (Ξ» k hkq, _), rw [evalβ‚‚_mul, evalβ‚‚_C, evalβ‚‚_pow, evalβ‚‚_X, ← algebra.smul_def], refine smul_mem _ _ (subset_span _), rw finset.mem_coe, refine finset.mem_image.2 ⟨_, _, rfl⟩, rw [finset.mem_range, nat.lt_succ_iff], refine le_of_not_lt (Ξ» hk, _), rw [degree_le_iff_coeff_zero] at this, rw [finsupp.mem_support_iff] at hkq, apply hkq, apply this, exact lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 hk) end theorem fg_adjoin_of_finite {s : set A} (hfs : s.finite) (his : βˆ€ x ∈ s, is_integral R x) : (algebra.adjoin R s : submodule R A).fg := set.finite.induction_on hfs (Ξ» _, ⟨{1}, submodule.ext $ Ξ» x, by { erw [algebra.adjoin_empty, finset.coe_singleton, ← one_eq_span, one_eq_map_top, map_top, linear_map.mem_range, algebra.mem_bot], refl }⟩) (Ξ» a s has hs ih his, by rw [← set.union_singleton, algebra.adjoin_union_coe_submodule]; exact fg_mul _ _ (ih $ Ξ» i hi, his i $ set.mem_insert_of_mem a hi) (fg_adjoin_singleton_of_integral _ $ his a $ set.mem_insert a s)) his theorem is_integral_of_mem_of_fg (S : subalgebra R A) (HS : (S : submodule R A).fg) (x : A) (hx : x ∈ S) : is_integral R x := begin cases HS with y hy, obtain ⟨lx, hlx1, hlx2⟩ : βˆƒ (l : A β†’β‚€ R) (H : l ∈ finsupp.supported R R ↑y), (finsupp.total A A R id) l = x, { rwa [←(@finsupp.mem_span_iff_total A A R _ _ _ id ↑y x), set.image_id ↑y, hy] }, have hyS : βˆ€ {p}, p ∈ y β†’ p ∈ S := Ξ» p hp, show p ∈ (S : submodule R A), by { rw ← hy, exact subset_span hp }, have : βˆ€ (jk : (↑(y.product y) : set (A Γ— A))), jk.1.1 * jk.1.2 ∈ (S : submodule R A) := Ξ» jk, S.mul_mem (hyS (finset.mem_product.1 jk.2).1) (hyS (finset.mem_product.1 jk.2).2), rw [← hy, ← set.image_id ↑y] at this, simp only [finsupp.mem_span_iff_total] at this, choose ly hly1 hly2, let Sβ‚€ : set R := ring.closure ↑(lx.frange βˆͺ finset.bUnion finset.univ (finsupp.frange ∘ ly)), refine is_integral_of_subring Sβ‚€ _, letI : comm_ring Sβ‚€ := @subtype.comm_ring _ _ _ ring.closure.is_subring, letI : algebra Sβ‚€ A := algebra.of_is_subring _, have : span Sβ‚€ (insert 1 ↑y : set A) * span Sβ‚€ (insert 1 ↑y : set A) ≀ span Sβ‚€ (insert 1 ↑y : set A), { rw span_mul_span, refine span_le.2 (Ξ» z hz, _), rcases set.mem_mul.1 hz with ⟨p, q, rfl | hp, hq, rfl⟩, { rw one_mul, exact subset_span hq }, rcases hq with rfl | hq, { rw mul_one, exact subset_span (or.inr hp) }, erw ← hly2 ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩, rw [finsupp.total_apply, finsupp.sum], refine (span Sβ‚€ (insert 1 ↑y : set A)).sum_mem (Ξ» t ht, _), have : ly ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩ t ∈ Sβ‚€ := ring.subset_closure (finset.mem_union_right _ $ finset.mem_bUnion.2 ⟨⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩, finset.mem_univ _, finsupp.mem_frange.2 ⟨finsupp.mem_support_iff.1 ht, _, rfl⟩⟩), change (⟨_, this⟩ : Sβ‚€) β€’ t ∈ _, exact smul_mem _ _ (subset_span $ or.inr $ hly1 _ ht) }, haveI : is_subring (span Sβ‚€ (insert 1 ↑y : set A) : set A) := { one_mem := subset_span $ or.inl rfl, mul_mem := Ξ» p q hp hq, this $ mul_mem_mul hp hq, zero_mem := (span Sβ‚€ (insert 1 ↑y : set A)).zero_mem, add_mem := Ξ» _ _, (span Sβ‚€ (insert 1 ↑y : set A)).add_mem, neg_mem := Ξ» _, (span Sβ‚€ (insert 1 ↑y : set A)).neg_mem }, have : span Sβ‚€ (insert 1 ↑y : set A) = algebra.adjoin Sβ‚€ (↑y : set A), { refine le_antisymm (span_le.2 $ set.insert_subset.2 ⟨(algebra.adjoin Sβ‚€ ↑y).one_mem, algebra.subset_adjoin⟩) (Ξ» z hz, _), rw [subalgebra.mem_to_submodule, algebra.mem_adjoin_iff] at hz, rw ← submodule.mem_coe, refine ring.closure_subset (set.union_subset (set.range_subset_iff.2 $ Ξ» t, _) (Ξ» t ht, subset_span $ or.inr ht)) hz, rw algebra.algebra_map_eq_smul_one, exact smul_mem (span Sβ‚€ (insert 1 ↑y : set A)) _ (subset_span $ or.inl rfl) }, haveI : is_noetherian_ring β†₯Sβ‚€ := is_noetherian_ring_closure _ (finset.finite_to_set _), refine is_integral_of_submodule_noetherian (algebra.adjoin Sβ‚€ ↑y) (is_noetherian_of_fg_of_noetherian _ ⟨insert 1 y, by rw [finset.coe_insert, this]⟩) _ _, rw [← hlx2, finsupp.total_apply, finsupp.sum], refine subalgebra.sum_mem _ (Ξ» r hr, _), have : lx r ∈ Sβ‚€ := ring.subset_closure (finset.mem_union_left _ (finset.mem_image_of_mem _ hr)), change (⟨_, this⟩ : Sβ‚€) β€’ r ∈ _, rw finsupp.mem_supported at hlx1, exact subalgebra.smul_mem _ (algebra.subset_adjoin $ hlx1 hr) _ end lemma ring_hom.is_integral_of_mem_closure {x y z : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) (hz : z ∈ ring.closure ({x, y} : set S)) : f.is_integral_elem z := begin letI : algebra R S := f.to_algebra, have := fg_mul _ _ (fg_adjoin_singleton_of_integral x hx) (fg_adjoin_singleton_of_integral y hy), rw [← algebra.adjoin_union_coe_submodule, set.singleton_union] at this, exact is_integral_of_mem_of_fg (algebra.adjoin R {x, y}) this z (algebra.mem_adjoin_iff.2 $ ring.closure_mono (set.subset_union_right _ _) hz), end theorem is_integral_of_mem_closure {x y z : A} (hx : is_integral R x) (hy : is_integral R y) (hz : z ∈ ring.closure ({x, y} : set A)) : is_integral R z := (algebra_map R A).is_integral_of_mem_closure hx hy hz lemma ring_hom.is_integral_zero : f.is_integral_elem 0 := f.map_zero β–Έ f.is_integral_map theorem is_integral_zero : is_integral R (0:A) := (algebra_map R A).is_integral_zero lemma ring_hom.is_integral_one : f.is_integral_elem 1 := f.map_one β–Έ f.is_integral_map theorem is_integral_one : is_integral R (1:A) := (algebra_map R A).is_integral_one lemma ring_hom.is_integral_add {x y : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x + y) := f.is_integral_of_mem_closure hx hy (is_add_submonoid.add_mem (ring.subset_closure (or.inl rfl)) (ring.subset_closure (or.inr rfl))) theorem is_integral_add {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x + y) := (algebra_map R A).is_integral_add hx hy lemma ring_hom.is_integral_neg {x : S} (hx : f.is_integral_elem x) : f.is_integral_elem (-x) := f.is_integral_of_mem_closure hx hx (is_add_subgroup.neg_mem (ring.subset_closure (or.inl rfl))) theorem is_integral_neg {x : A} (hx : is_integral R x) : is_integral R (-x) := (algebra_map R A).is_integral_neg hx lemma ring_hom.is_integral_sub {x y : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x - y) := by simpa only [sub_eq_add_neg] using f.is_integral_add hx (f.is_integral_neg hy) theorem is_integral_sub {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x - y) := (algebra_map R A).is_integral_sub hx hy lemma ring_hom.is_integral_mul {x y : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x * y) := f.is_integral_of_mem_closure hx hy (is_submonoid.mul_mem (ring.subset_closure (or.inl rfl)) (ring.subset_closure (or.inr rfl))) theorem is_integral_mul {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x * y) := (algebra_map R A).is_integral_mul hx hy variables (R A) /-- The integral closure of R in an R-algebra A. -/ def integral_closure : subalgebra R A := { carrier := { r | is_integral R r }, zero_mem' := is_integral_zero, one_mem' := is_integral_one, add_mem' := Ξ» _ _, is_integral_add, mul_mem' := Ξ» _ _, is_integral_mul, algebra_map_mem' := Ξ» x, is_integral_algebra_map } theorem mem_integral_closure_iff_mem_fg {r : A} : r ∈ integral_closure R A ↔ βˆƒ M : subalgebra R A, (M : submodule R A).fg ∧ r ∈ M := ⟨λ hr, ⟨algebra.adjoin R {r}, fg_adjoin_singleton_of_integral _ hr, algebra.subset_adjoin rfl⟩, Ξ» ⟨M, Hf, hrM⟩, is_integral_of_mem_of_fg M Hf _ hrM⟩ variables {R} {A} /-- Mapping an integral closure along an `alg_equiv` gives the integral closure. -/ lemma integral_closure_map_alg_equiv (f : A ≃ₐ[R] B) : (integral_closure R A).map (f : A →ₐ[R] B) = integral_closure R B := begin ext y, rw subalgebra.mem_map, split, { rintros ⟨x, hx, rfl⟩, exact is_integral_alg_hom f hx }, { intro hy, use [f.symm y, is_integral_alg_hom (f.symm : B →ₐ[R] A) hy], simp } end lemma integral_closure.is_integral (x : integral_closure R A) : is_integral R x := let ⟨p, hpm, hpx⟩ := x.2 in ⟨p, hpm, subtype.eq $ by rwa [← aeval_def, subtype.val_eq_coe, ← subalgebra.val_apply, aeval_alg_hom_apply] at hpx⟩ lemma ring_hom.is_integral_of_is_integral_mul_unit (x y : S) (r : R) (hr : f r * y = 1) (hx : f.is_integral_elem (x * y)) : f.is_integral_elem x := begin obtain ⟨p, ⟨p_monic, hp⟩⟩ := hx, refine ⟨scale_roots p r, ⟨(monic_scale_roots_iff r).2 p_monic, _⟩⟩, convert scale_roots_evalβ‚‚_eq_zero f hp, rw [mul_comm x y, ← mul_assoc, hr, one_mul], end theorem is_integral_of_is_integral_mul_unit {x y : A} {r : R} (hr : algebra_map R A r * y = 1) (hx : is_integral R (x * y)) : is_integral R x := (algebra_map R A).is_integral_of_is_integral_mul_unit x y r hr hx /-- Generalization of `is_integral_of_mem_closure` bootstrapped up from that lemma -/ lemma is_integral_of_mem_closure' (G : set A) (hG : βˆ€ x ∈ G, is_integral R x) : βˆ€ x ∈ (subring.closure G), is_integral R x := Ξ» x hx, subring.closure_induction hx hG is_integral_zero is_integral_one (Ξ» _ _, is_integral_add) (Ξ» _, is_integral_neg) (Ξ» _ _, is_integral_mul) lemma is_integral_of_mem_closure'' {S : Type*} [comm_ring S] {f : R β†’+* S} (G : set S) (hG : βˆ€ x ∈ G, f.is_integral_elem x) : βˆ€ x ∈ (subring.closure G), f.is_integral_elem x := Ξ» x hx, @is_integral_of_mem_closure' R S _ _ f.to_algebra G hG x hx end section algebra open algebra variables {R A B S T : Type*} variables [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring S] [comm_ring T] variables [algebra A B] [algebra R B] (f : R β†’+* S) (g : S β†’+* T) lemma is_integral_trans_aux (x : B) {p : polynomial A} (pmonic : monic p) (hp : aeval x p = 0) : is_integral (adjoin R (↑(p.map $ algebra_map A B).frange : set B)) x := begin generalize hS : (↑(p.map $ algebra_map A B).frange : set B) = S, have coeffs_mem : βˆ€ i, (p.map $ algebra_map A B).coeff i ∈ adjoin R S, { intro i, by_cases hi : (p.map $ algebra_map A B).coeff i = 0, { rw hi, exact subalgebra.zero_mem _ }, rw ← hS, exact subset_adjoin (finsupp.mem_frange.2 ⟨hi, i, rfl⟩) }, obtain ⟨q, hq⟩ : βˆƒ q : polynomial (adjoin R S), q.map (algebra_map (adjoin R S) B) = (p.map $ algebra_map A B), { rw ← set.mem_range, exact (polynomial.mem_map_range _).2 (Ξ» i, ⟨⟨_, coeffs_mem i⟩, rfl⟩) }, use q, split, { suffices h : (q.map (algebra_map (adjoin R S) B)).monic, { refine monic_of_injective _ h, exact subtype.val_injective }, { rw hq, exact monic_map _ pmonic } }, { convert hp using 1, replace hq := congr_arg (eval x) hq, convert hq using 1; symmetry; apply eval_map }, end variables [algebra R A] [is_scalar_tower R A B] /-- If A is an R-algebra all of whose elements are integral over R, and x is an element of an A-algebra that is integral over A, then x is integral over R.-/ lemma is_integral_trans (A_int : is_integral R A) (x : B) (hx : is_integral A x) : is_integral R x := begin rcases hx with ⟨p, pmonic, hp⟩, let S : set B := ↑(p.map $ algebra_map A B).frange, refine is_integral_of_mem_of_fg (adjoin R (S βˆͺ {x})) _ _ (subset_adjoin $ or.inr rfl), refine fg_trans (fg_adjoin_of_finite (finset.finite_to_set _) (Ξ» x hx, _)) _, { rw [finset.mem_coe, finsupp.mem_frange] at hx, rcases hx with ⟨_, i, rfl⟩, show is_integral R ((p.map $ algebra_map A B).coeff i), rw coeff_map, convert is_integral_alg_hom (is_scalar_tower.to_alg_hom R A B) (A_int _) }, { apply fg_adjoin_singleton_of_integral, exact is_integral_trans_aux _ pmonic hp } end /-- If A is an R-algebra all of whose elements are integral over R, and B is an A-algebra all of whose elements are integral over A, then all elements of B are integral over R.-/ lemma algebra.is_integral_trans (hA : is_integral R A) (hB : is_integral A B) : is_integral R B := Ξ» x, is_integral_trans hA x (hB x) lemma ring_hom.is_integral_trans (hf : f.is_integral) (hg : g.is_integral) : (g.comp f).is_integral := @algebra.is_integral_trans R S T _ _ _ g.to_algebra (g.comp f).to_algebra f.to_algebra (@is_scalar_tower.of_algebra_map_eq R S T _ _ _ f.to_algebra g.to_algebra (g.comp f).to_algebra (ring_hom.comp_apply g f)) hf hg lemma ring_hom.is_integral_of_surjective (hf : function.surjective f) : f.is_integral := Ξ» x, (hf x).rec_on (Ξ» y hy, (hy β–Έ f.is_integral_map : f.is_integral_elem x)) lemma is_integral_of_surjective (h : function.surjective (algebra_map R A)) : is_integral R A := (algebra_map R A).is_integral_of_surjective h /-- If `R β†’ A β†’ B` is an algebra tower with `A β†’ B` injective, then if the entire tower is an integral extension so is `R β†’ A` -/ lemma is_integral_tower_bot_of_is_integral (H : function.injective (algebra_map A B)) {x : A} (h : is_integral R (algebra_map A B x)) : is_integral R x := begin rcases h with ⟨p, ⟨hp, hp'⟩⟩, refine ⟨p, ⟨hp, _⟩⟩, rw [is_scalar_tower.algebra_map_eq R A B, ← evalβ‚‚_map, evalβ‚‚_hom, ← ring_hom.map_zero (algebra_map A B)] at hp', rw [evalβ‚‚_eq_eval_map], exact H hp', end lemma ring_hom.is_integral_tower_bot_of_is_integral (hg : function.injective g) (hfg : (g.comp f).is_integral) : f.is_integral := Ξ» x, @is_integral_tower_bot_of_is_integral R S T _ _ _ g.to_algebra (g.comp f).to_algebra f.to_algebra (@is_scalar_tower.of_algebra_map_eq R S T _ _ _ f.to_algebra g.to_algebra (g.comp f).to_algebra (ring_hom.comp_apply g f)) hg x (hfg (g x)) lemma is_integral_tower_bot_of_is_integral_field {R A B : Type*} [comm_ring R] [field A] [comm_ring B] [nontrivial B] [algebra R A] [algebra A B] [algebra R B] [is_scalar_tower R A B] {x : A} (h : is_integral R (algebra_map A B x)) : is_integral R x := is_integral_tower_bot_of_is_integral (algebra_map A B).injective h lemma ring_hom.is_integral_elem_of_is_integral_elem_comp {x : T} (h : (g.comp f).is_integral_elem x) : g.is_integral_elem x := let ⟨p, ⟨hp, hp'⟩⟩ := h in ⟨p.map f, monic_map f hp, by rwa ← evalβ‚‚_map at hp'⟩ lemma ring_hom.is_integral_tower_top_of_is_integral (h : (g.comp f).is_integral) : g.is_integral := Ξ» x, ring_hom.is_integral_elem_of_is_integral_elem_comp f g (h x) /-- If `R β†’ A β†’ B` is an algebra tower, then if the entire tower is an integral extension so is `A β†’ B`. -/ lemma is_integral_tower_top_of_is_integral {x : B} (h : is_integral R x) : is_integral A x := begin rcases h with ⟨p, ⟨hp, hp'⟩⟩, refine ⟨p.map (algebra_map R A), ⟨monic_map (algebra_map R A) hp, _⟩⟩, rw [is_scalar_tower.algebra_map_eq R A B, ← evalβ‚‚_map] at hp', exact hp', end lemma ring_hom.is_integral_quotient_of_is_integral {I : ideal S} (hf : f.is_integral) : (ideal.quotient_map I f le_rfl).is_integral := begin rintros ⟨x⟩, obtain ⟨p, ⟨p_monic, hpx⟩⟩ := hf x, refine ⟨p.map (ideal.quotient.mk _), ⟨monic_map _ p_monic, _⟩⟩, simpa only [hom_evalβ‚‚, evalβ‚‚_map] using congr_arg (ideal.quotient.mk I) hpx end lemma is_integral_quotient_of_is_integral {I : ideal A} (hRA : is_integral R A) : is_integral (I.comap (algebra_map R A)).quotient I.quotient := (algebra_map R A).is_integral_quotient_of_is_integral hRA lemma is_integral_quotient_map_iff {I : ideal S} : (ideal.quotient_map I f le_rfl).is_integral ↔ ((ideal.quotient.mk I).comp f : R β†’+* I.quotient).is_integral := begin let g := ideal.quotient.mk (I.comap f), have := ideal.quotient_map_comp_mk le_rfl, refine ⟨λ h, _, Ξ» h, ring_hom.is_integral_tower_top_of_is_integral g _ (this β–Έ h)⟩, refine this β–Έ ring_hom.is_integral_trans g (ideal.quotient_map I f le_rfl) _ h, exact ring_hom.is_integral_of_surjective g ideal.quotient.mk_surjective, end /-- If the integral extension `R β†’ S` is injective, and `S` is a field, then `R` is also a field. -/ lemma is_field_of_is_integral_of_is_field {R S : Type*} [integral_domain R] [integral_domain S] [algebra R S] (H : is_integral R S) (hRS : function.injective (algebra_map R S)) (hS : is_field S) : is_field R := begin refine ⟨⟨0, 1, zero_ne_one⟩, mul_comm, Ξ» a ha, _⟩, -- Let `a_inv` be the inverse of `algebra_map R S a`, -- then we need to show that `a_inv` is of the form `algebra_map R S b`. obtain ⟨a_inv, ha_inv⟩ := hS.mul_inv_cancel (Ξ» h, ha (hRS (trans h (ring_hom.map_zero _).symm))), -- Let `p : polynomial R` be monic with root `a_inv`, -- and `q` be `p` with coefficients reversed (so `q(a) = q'(a) * a + 1`). -- We claim that `q(a) = 0`, so `-q'(a)` is the inverse of `a`. obtain ⟨p, p_monic, hp⟩ := H a_inv, use -βˆ‘ (i : β„•) in finset.range p.nat_degree, (p.coeff i) * a ^ (p.nat_degree - i - 1), -- `q(a) = 0`, because multiplying everything with `a_inv^n` gives `p(a_inv) = 0`. -- TODO: this could be a lemma for `polynomial.reverse`. have hq : βˆ‘ (i : β„•) in finset.range (p.nat_degree + 1), (p.coeff i) * a ^ (p.nat_degree - i) = 0, { apply (algebra_map R S).injective_iff.mp hRS, have a_inv_ne_zero : a_inv β‰  0 := right_ne_zero_of_mul (mt ha_inv.symm.trans one_ne_zero), refine (mul_eq_zero.mp _).resolve_right (pow_ne_zero p.nat_degree a_inv_ne_zero), rw [evalβ‚‚_eq_sum_range] at hp, rw [ring_hom.map_sum, finset.sum_mul], refine (finset.sum_congr rfl (Ξ» i hi, _)).trans hp, rw [ring_hom.map_mul, mul_assoc], congr, have : a_inv ^ p.nat_degree = a_inv ^ (p.nat_degree - i) * a_inv ^ i, { rw [← pow_add a_inv, nat.sub_add_cancel (nat.le_of_lt_succ (finset.mem_range.mp hi))] }, rw [ring_hom.map_pow, this, ← mul_assoc, ← mul_pow, ha_inv, one_pow, one_mul] }, -- Since `q(a) = 0` and `q(a) = q'(a) * a + 1`, we have `a * -q'(a) = 1`. -- TODO: we could use a lemma for `polynomial.div_X` here. rw [finset.sum_range_succ, p_monic.coeff_nat_degree, one_mul, nat.sub_self, pow_zero, add_eq_zero_iff_eq_neg, eq_comm] at hq, rw [mul_comm, ← neg_mul_eq_neg_mul, finset.sum_mul], convert hq using 2, refine finset.sum_congr rfl (Ξ» i hi, _), have : 1 ≀ p.nat_degree - i := nat.le_sub_left_of_add_le (finset.mem_range.mp hi), rw [mul_assoc, ← pow_succ', nat.sub_add_cancel this] end end algebra section local attribute [instance] subset.comm_ring algebra.of_is_subring theorem integral_closure_idem {R : Type*} {A : Type*} [comm_ring R] [comm_ring A] [algebra R A] : integral_closure (integral_closure R A : set A) A = βŠ₯ := eq_bot_iff.2 $ Ξ» x hx, algebra.mem_bot.2 ⟨⟨x, @is_integral_trans _ _ _ _ _ _ _ _ (integral_closure R A).algebra _ integral_closure.is_integral x hx⟩, rfl⟩ end section integral_domain variables {R S : Type*} [comm_ring R] [integral_domain S] [algebra R S] instance : integral_domain (integral_closure R S) := { exists_pair_ne := ⟨0, 1, mt subtype.ext_iff_val.mp zero_ne_one⟩, eq_zero_or_eq_zero_of_mul_eq_zero := Ξ» ⟨a, ha⟩ ⟨b, hb⟩ h, or.imp subtype.ext_iff_val.mpr subtype.ext_iff_val.mpr (eq_zero_or_eq_zero_of_mul_eq_zero (subtype.ext_iff_val.mp h)), ..(integral_closure R S).comm_ring R S } end integral_domain
3317c1d5fc10dc7360b94f0a4c2a5306a072bf16
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/stage0/src/Lean/Expr.lean
bde632e56e8f2d22cfbfea8df6b4f9780be91fc4
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
36,669
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Data.KVMap import Lean.Level namespace Lean inductive Literal | natVal (val : Nat) | strVal (val : String) instance Literal.inhabited : Inhabited Literal := ⟨Literal.natVal 0⟩ def Literal.hash : Literal β†’ USize | Literal.natVal v => hash v | Literal.strVal v => hash v instance Literal.hashable : Hashable Literal := ⟨Literal.hash⟩ def Literal.beq : Literal β†’ Literal β†’ Bool | Literal.natVal v₁, Literal.natVal vβ‚‚ => v₁ == vβ‚‚ | Literal.strVal v₁, Literal.strVal vβ‚‚ => v₁ == vβ‚‚ | _, _ => false instance Literal.hasBeq : HasBeq Literal := ⟨Literal.beq⟩ def Literal.lt : Literal β†’ Literal β†’ Bool | Literal.natVal _, Literal.strVal _ => true | Literal.natVal v₁, Literal.natVal vβ‚‚ => v₁ < vβ‚‚ | Literal.strVal v₁, Literal.strVal vβ‚‚ => v₁ < vβ‚‚ | _, _ => false instance Literal.hasLess : HasLess Literal := ⟨fun a b => a.lt b⟩ inductive BinderInfo | default | implicit | strictImplicit | instImplicit | auxDecl def BinderInfo.hash : BinderInfo β†’ USize | BinderInfo.default => 947 | BinderInfo.implicit => 1019 | BinderInfo.strictImplicit => 1087 | BinderInfo.instImplicit => 1153 | BinderInfo.auxDecl => 1229 def BinderInfo.isExplicit : BinderInfo β†’ Bool | BinderInfo.implicit => false | BinderInfo.strictImplicit => false | BinderInfo.instImplicit => false | _ => true instance BinderInfo.hashable : Hashable BinderInfo := ⟨BinderInfo.hash⟩ instance BinderInfo.inhabited : Inhabited BinderInfo := ⟨BinderInfo.default⟩ def BinderInfo.isInstImplicit : BinderInfo β†’ Bool | BinderInfo.instImplicit => true | _ => false def BinderInfo.isAuxDecl : BinderInfo β†’ Bool | BinderInfo.auxDecl => true | _ => false protected def BinderInfo.beq : BinderInfo β†’ BinderInfo β†’ Bool | BinderInfo.default, BinderInfo.default => true | BinderInfo.implicit, BinderInfo.implicit => true | BinderInfo.strictImplicit, BinderInfo.strictImplicit => true | BinderInfo.instImplicit, BinderInfo.instImplicit => true | BinderInfo.auxDecl, BinderInfo.auxDecl => true | _, _ => false instance BinderInfo.hasBeq : HasBeq BinderInfo := ⟨BinderInfo.beq⟩ abbrev MData := KVMap abbrev MData.empty : MData := { : KVMap } instance MVData.hasEmptc : HasEmptyc MData := ⟨MData.empty⟩ /-- Cached hash code, cached results, and other data for `Expr`. hash : 32-bits hasFVar : 1-bit hasExprMVar : 1-bit hasLevelMVar : 1-bit hasLevelParam : 1-bit nonDepLet : 1-bit binderInfo : 3-bits looseBVarRange : 24-bits -/ def Expr.Data := UInt64 instance Expr.Data.inhabited : Inhabited Expr.Data := inferInstanceAs (Inhabited UInt64) def Expr.Data.hash (c : Expr.Data) : USize := c.toUInt32.toUSize instance Expr.Data.hasBeq : HasBeq Expr.Data := ⟨fun (a b : UInt64) => a == b⟩ def Expr.Data.looseBVarRange (c : Expr.Data) : UInt32 := (c.shiftRight 40).toUInt32 def Expr.Data.hasFVar (c : Expr.Data) : Bool := ((c.shiftRight 32).land 1) == 1 def Expr.Data.hasExprMVar (c : Expr.Data) : Bool := ((c.shiftRight 33).land 1) == 1 def Expr.Data.hasLevelMVar (c : Expr.Data) : Bool := ((c.shiftRight 34).land 1) == 1 def Expr.Data.hasLevelParam (c : Expr.Data) : Bool := ((c.shiftRight 35).land 1) == 1 def Expr.Data.nonDepLet (c : Expr.Data) : Bool := ((c.shiftRight 36).land 1) == 1 @[extern c inline "(uint8_t)((#1 << 24) >> 61)"] def Expr.Data.binderInfo (c : Expr.Data) : BinderInfo := let bi := (c.shiftLeft 24).shiftRight 61; if bi == 0 then BinderInfo.default else if bi == 1 then BinderInfo.implicit else if bi == 2 then BinderInfo.strictImplicit else if bi == 3 then BinderInfo.instImplicit else BinderInfo.auxDecl @[extern c inline "(uint64_t)#1"] def BinderInfo.toUInt64 : BinderInfo β†’ UInt64 | BinderInfo.default => 0 | BinderInfo.implicit => 1 | BinderInfo.strictImplicit => 2 | BinderInfo.instImplicit => 3 | BinderInfo.auxDecl => 4 @[inline] private def Expr.mkDataCore (h : USize) (looseBVarRange : Nat) (hasFVar hasExprMVar hasLevelMVar hasLevelParam nonDepLet : Bool) (bi : BinderInfo) : Expr.Data := if looseBVarRange > Nat.pow 2 24 - 1 then panic! "bound variable index is too big" else let r : UInt64 := h.toUInt32.toUInt64 + hasFVar.toUInt64.shiftLeft 32 + hasExprMVar.toUInt64.shiftLeft 33 + hasLevelMVar.toUInt64.shiftLeft 34 + hasLevelParam.toUInt64.shiftLeft 35 + nonDepLet.toUInt64.shiftLeft 36 + bi.toUInt64.shiftLeft 37 + looseBVarRange.toUInt64.shiftLeft 40; r def Expr.mkData (h : USize) (looseBVarRange : Nat := 0) (hasFVar hasExprMVar hasLevelMVar hasLevelParam : Bool := false) : Expr.Data := Expr.mkDataCore h looseBVarRange hasFVar hasExprMVar hasLevelMVar hasLevelParam false BinderInfo.default def Expr.mkDataForBinder (h : USize) (looseBVarRange : Nat) (hasFVar hasExprMVar hasLevelMVar hasLevelParam : Bool) (bi : BinderInfo) : Expr.Data := Expr.mkDataCore h looseBVarRange hasFVar hasExprMVar hasLevelMVar hasLevelParam false bi def Expr.mkDataForLet (h : USize) (looseBVarRange : Nat) (hasFVar hasExprMVar hasLevelMVar hasLevelParam nonDepLet : Bool) : Expr.Data := Expr.mkDataCore h looseBVarRange hasFVar hasExprMVar hasLevelMVar hasLevelParam nonDepLet BinderInfo.default open Expr abbrev MVarId := Name abbrev FVarId := Name /- We use the `E` suffix (short for `Expr`) to avoid collision with keywords. We considered using Β«...Β», but it is too inconvenient to use. -/ inductive Expr | bvar : Nat β†’ Data β†’ Expr -- bound variables | fvar : FVarId β†’ Data β†’ Expr -- free variables | mvar : MVarId β†’ Data β†’ Expr -- meta variables | sort : Level β†’ Data β†’ Expr -- Sort | const : Name β†’ List Level β†’ Data β†’ Expr -- constants | app : Expr β†’ Expr β†’ Data β†’ Expr -- application | lam : Name β†’ Expr β†’ Expr β†’ Data β†’ Expr -- lambda abstraction | forallE : Name β†’ Expr β†’ Expr β†’ Data β†’ Expr -- (dependent) arrow | letE : Name β†’ Expr β†’ Expr β†’ Expr β†’ Data β†’ Expr -- let expressions | lit : Literal β†’ Data β†’ Expr -- literals | mdata : MData β†’ Expr β†’ Data β†’ Expr -- metadata | proj : Name β†’ Nat β†’ Expr β†’ Data β†’ Expr -- projection -- IMPORTANT: the following constructor will be deleted | localE : Name β†’ Name β†’ Expr β†’ Data β†’ Expr -- Lean2 legacy. TODO: delete namespace Expr instance : Inhabited Expr := ⟨sort (arbitrary _) (arbitrary _)⟩ @[inline] def data : Expr β†’ Data | bvar _ d => d | fvar _ d => d | mvar _ d => d | sort _ d => d | const _ _ d => d | app _ _ d => d | lam _ _ _ d => d | forallE _ _ _ d => d | letE _ _ _ _ d => d | lit _ d => d | mdata _ _ d => d | proj _ _ _ d => d | localE _ _ _ d => d def ctorName : Expr β†’ String | bvar _ _ => "bvar" | fvar _ _ => "fvar" | mvar _ _ => "mvar" | sort _ _ => "sort" | const _ _ _ => "const" | app _ _ _ => "app" | lam _ _ _ _ => "lam" | forallE _ _ _ _ => "forallE" | letE _ _ _ _ _ => "letE" | lit _ _ => "lit" | mdata _ _ _ => "mdata" | proj _ _ _ _ => "proj" | localE _ _ _ _ => "localE" def hash (e : Expr) : USize := e.data.hash instance : Hashable Expr := ⟨Expr.hash⟩ def hasFVar (e : Expr) : Bool := e.data.hasFVar def hasExprMVar (e : Expr) : Bool := e.data.hasExprMVar def hasLevelMVar (e : Expr) : Bool := e.data.hasLevelMVar def hasMVar (e : Expr) : Bool := let d := e.data; d.hasExprMVar || d.hasLevelMVar def hasLevelParam (e : Expr) : Bool := e.data.hasLevelParam def looseBVarRange (e : Expr) : Nat := e.data.looseBVarRange.toNat def binderInfo (e : Expr) : BinderInfo := e.data.binderInfo @[export lean_expr_hash] def hashEx : Expr β†’ USize := hash @[export lean_expr_has_fvar] def hasFVarEx : Expr β†’ Bool := hasFVar @[export lean_expr_has_expr_mvar] def hasExprMVarEx : Expr β†’ Bool := hasExprMVar @[export lean_expr_has_level_mvar] def hasLevelMVarEx : Expr β†’ Bool := hasLevelMVar @[export lean_expr_has_mvar] def hasMVarEx : Expr β†’ Bool := hasMVar @[export lean_expr_has_level_param] def hasLevelParamEx : Expr β†’ Bool := hasLevelParam @[export lean_expr_loose_bvar_range] def looseBVarRangeEx (e : Expr) : UInt32 := e.data.looseBVarRange @[export lean_expr_binder_info] def binderInfoEx : Expr β†’ BinderInfo := binderInfo end Expr def mkLit (l : Literal) : Expr := Expr.lit l $ mkData (mixHash 3 (hash l)) def mkNatLit (n : Nat) : Expr := mkLit (Literal.natVal n) def mkStrLit (s : String) : Expr := mkLit (Literal.strVal s) def mkConst (n : Name) (lvls : List Level := []) : Expr := Expr.const n lvls $ mkData (mixHash 5 $ mixHash (hash n) (hash lvls)) 0 false false (lvls.any Level.hasMVar) (lvls.any Level.hasParam) def Literal.type : Literal β†’ Expr | Literal.natVal _ => mkConst `Nat | Literal.strVal _ => mkConst `String @[export lean_lit_type] def Literal.typeEx : Literal β†’ Expr := Literal.type def mkBVar (idx : Nat) : Expr := Expr.bvar idx $ mkData (mixHash 7 $ hash idx) (idx+1) def mkSort (lvl : Level) : Expr := Expr.sort lvl $ mkData (mixHash 11 $ hash lvl) 0 false false lvl.hasMVar lvl.hasParam def mkFVar (fvarId : FVarId) : Expr := Expr.fvar fvarId $ mkData (mixHash 13 $ hash fvarId) 0 true def mkMVar (fvarId : MVarId) : Expr := Expr.mvar fvarId $ mkData (mixHash 17 $ hash fvarId) 0 false true def mkMData (d : MData) (e : Expr) : Expr := Expr.mdata d e $ mkData (mixHash 19 $ hash e) e.looseBVarRange e.hasFVar e.hasExprMVar e.hasLevelMVar e.hasLevelParam def mkProj (s : Name) (i : Nat) (e : Expr) : Expr := Expr.proj s i e $ mkData (mixHash 23 $ mixHash (hash s) $ mixHash (hash i) (hash e)) e.looseBVarRange e.hasFVar e.hasExprMVar e.hasLevelMVar e.hasLevelParam def mkApp (f a : Expr) : Expr := Expr.app f a $ mkData (mixHash 29 $ mixHash (hash f) (hash a)) (Nat.max f.looseBVarRange a.looseBVarRange) (f.hasFVar || a.hasFVar) (f.hasExprMVar || a.hasExprMVar) (f.hasLevelMVar || a.hasLevelMVar) (f.hasLevelParam || a.hasLevelParam) def mkLambda (x : Name) (bi : BinderInfo) (t : Expr) (b : Expr) : Expr := -- let x := x.eraseMacroScopes; Expr.lam x t b $ mkDataForBinder (mixHash 31 $ mixHash (hash t) (hash b)) (Nat.max t.looseBVarRange (b.looseBVarRange - 1)) (t.hasFVar || b.hasFVar) (t.hasExprMVar || b.hasExprMVar) (t.hasLevelMVar || b.hasLevelMVar) (t.hasLevelParam || b.hasLevelParam) bi def mkForall (x : Name) (bi : BinderInfo) (t : Expr) (b : Expr) : Expr := -- let x := x.eraseMacroScopes; Expr.forallE x t b $ mkDataForBinder (mixHash 37 $ mixHash (hash t) (hash b)) (Nat.max t.looseBVarRange (b.looseBVarRange - 1)) (t.hasFVar || b.hasFVar) (t.hasExprMVar || b.hasExprMVar) (t.hasLevelMVar || b.hasLevelMVar) (t.hasLevelParam || b.hasLevelParam) bi /- Return `Unit -> type` -/ def mkThunkType (type : Expr) : Expr := mkForall Name.anonymous BinderInfo.default (Lean.mkConst `Unit) type /- Return `fun (_ : Unit), e` -/ def mkThunk (type : Expr) : Expr := mkLambda `_ BinderInfo.default (Lean.mkConst `Unit) type def mkLet (x : Name) (t : Expr) (v : Expr) (b : Expr) (nonDep : Bool := false) : Expr := -- let x := x.eraseMacroScopes; Expr.letE x t v b $ mkDataForLet (mixHash 41 $ mixHash (hash t) $ mixHash (hash v) (hash b)) (Nat.max (Nat.max t.looseBVarRange v.looseBVarRange) (b.looseBVarRange - 1)) (t.hasFVar || v.hasFVar || b.hasFVar) (t.hasExprMVar || v.hasExprMVar || b.hasExprMVar) (t.hasLevelMVar || v.hasLevelMVar || b.hasLevelMVar) (t.hasLevelParam || v.hasLevelParam || b.hasLevelParam) nonDep -- TODO: delete def mkLocal (x u : Name) (t : Expr) (bi : BinderInfo) : Expr := Expr.localE x u t $ mkDataForBinder (mixHash 43 $ hash t) t.looseBVarRange true t.hasExprMVar t.hasLevelMVar t.hasLevelParam bi @[export lean_expr_mk_bvar] def mkBVarEx : Nat β†’ Expr := mkBVar @[export lean_expr_mk_fvar] def mkFVarEx : FVarId β†’ Expr := mkFVar @[export lean_expr_mk_mvar] def mkMVarEx : MVarId β†’ Expr := mkMVar @[export lean_expr_mk_sort] def mkSortEx : Level β†’ Expr := mkSort @[export lean_expr_mk_const] def mkConstEx (c : Name) (lvls : List Level) : Expr := mkConst c lvls @[export lean_expr_mk_app] def mkAppEx : Expr β†’ Expr β†’ Expr := mkApp @[export lean_expr_mk_lambda] def mkLambdaEx (n : Name) (d b : Expr) (bi : BinderInfo) : Expr := mkLambda n bi d b @[export lean_expr_mk_forall] def mkForallEx (n : Name) (d b : Expr) (bi : BinderInfo) : Expr := mkForall n bi d b @[export lean_expr_mk_let] def mkLetEx (n : Name) (t v b : Expr) : Expr := mkLet n t v b @[export lean_expr_mk_lit] def mkLitEx : Literal β†’ Expr := mkLit @[export lean_expr_mk_mdata] def mkMDataEx : MData β†’ Expr β†’ Expr := mkMData @[export lean_expr_mk_proj] def mkProjEx : Name β†’ Nat β†’ Expr β†’ Expr := mkProj @[export lean_expr_mk_local] def mkLocalEx : Name β†’ Name β†’ Expr β†’ BinderInfo β†’ Expr := mkLocal def mkAppN (f : Expr) (args : Array Expr) : Expr := args.foldl mkApp f private partial def mkAppRangeAux (n : Nat) (args : Array Expr) : Nat β†’ Expr β†’ Expr | i, e => if i < n then mkAppRangeAux (i+1) (mkApp e (args.get! i)) else e /-- `mkAppRange f i j #[a_1, ..., a_i, ..., a_j, ... ]` ==> the expression `f a_i ... a_{j-1}` -/ def mkAppRange (f : Expr) (i j : Nat) (args : Array Expr) : Expr := mkAppRangeAux j args i f def mkAppRev (fn : Expr) (revArgs : Array Expr) : Expr := revArgs.foldr (fun a r => mkApp r a) fn namespace Expr -- TODO: implement it in Lean @[extern "lean_expr_dbg_to_string"] constant dbgToString (e : @& Expr) : String := arbitrary String @[extern "lean_expr_quick_lt"] constant quickLt (a : @& Expr) (b : @& Expr) : Bool := arbitrary _ @[extern "lean_expr_lt"] constant lt (a : @& Expr) (b : @& Expr) : Bool := arbitrary _ /- Return true iff `a` and `b` are alpha equivalent. Binder annotations are ignored. -/ @[extern "lean_expr_eqv"] constant eqv (a : @& Expr) (b : @& Expr) : Bool := arbitrary _ instance : HasBeq Expr := ⟨Expr.eqv⟩ /- Return true iff `a` and `b` are equal. Binder names and annotations are taking into account. -/ @[extern "lean_expr_equal"] constant equal (a : @& Expr) (b : @& Expr) : Bool := arbitrary _ def isSort : Expr β†’ Bool | sort _ _ => true | _ => false def isBVar : Expr β†’ Bool | bvar _ _ => true | _ => false def isMVar : Expr β†’ Bool | mvar _ _ => true | _ => false def isFVar : Expr β†’ Bool | fvar _ _ => true | _ => false def isApp : Expr β†’ Bool | app _ _ _ => true | _ => false def isProj : Expr β†’ Bool | proj _ _ _ _ => true | _ => false def isConst : Expr β†’ Bool | const _ _ _ => true | _ => false def isConstOf : Expr β†’ Name β†’ Bool | const n _ _, m => n == m | _, _ => false def isForall : Expr β†’ Bool | forallE _ _ _ _ => true | _ => false def isLambda : Expr β†’ Bool | lam _ _ _ _ => true | _ => false def isBinding : Expr β†’ Bool | lam _ _ _ _ => true | forallE _ _ _ _ => true | _ => false def isLet : Expr β†’ Bool | letE _ _ _ _ _ => true | _ => false def isMData : Expr β†’ Bool | mdata _ _ _ => true | _ => false def isLit : Expr β†’ Bool | lit _ _ => true | _ => false def getAppFn : Expr β†’ Expr | app f a _ => getAppFn f | e => e def getAppNumArgsAux : Expr β†’ Nat β†’ Nat | app f a _, n => getAppNumArgsAux f (n+1) | e, n => n def getAppNumArgs (e : Expr) : Nat := getAppNumArgsAux e 0 private def getAppArgsAux : Expr β†’ Array Expr β†’ Nat β†’ Array Expr | app f a _, as, i => getAppArgsAux f (as.set! i a) (i-1) | _, as, _ => as @[inline] def getAppArgs (e : Expr) : Array Expr := let dummy := mkSort levelZero; let nargs := e.getAppNumArgs; getAppArgsAux e (mkArray nargs dummy) (nargs-1) private def getAppRevArgsAux : Expr β†’ Array Expr β†’ Array Expr | app f a _, as => getAppRevArgsAux f (as.push a) | _, as => as @[inline] def getAppRevArgs (e : Expr) : Array Expr := getAppRevArgsAux e (Array.mkEmpty e.getAppNumArgs) @[specialize] def withAppAux {Ξ±} (k : Expr β†’ Array Expr β†’ Ξ±) : Expr β†’ Array Expr β†’ Nat β†’ Ξ± | app f a _, as, i => withAppAux f (as.set! i a) (i-1) | f, as, i => k f as @[inline] def withApp {Ξ±} (e : Expr) (k : Expr β†’ Array Expr β†’ Ξ±) : Ξ± := let dummy := mkSort levelZero; let nargs := e.getAppNumArgs; withAppAux k e (mkArray nargs dummy) (nargs-1) @[specialize] private def withAppRevAux {Ξ±} (k : Expr β†’ Array Expr β†’ Ξ±) : Expr β†’ Array Expr β†’ Ξ± | app f a _, as => withAppRevAux f (as.push a) | f, as => k f as @[inline] def withAppRev {Ξ±} (e : Expr) (k : Expr β†’ Array Expr β†’ Ξ±) : Ξ± := withAppRevAux k e (Array.mkEmpty e.getAppNumArgs) def getRevArgD : Expr β†’ Nat β†’ Expr β†’ Expr | app f a _, 0, _ => a | app f _ _, i+1, v => getRevArgD f i v | _, _, v => v def getRevArg! : Expr β†’ Nat β†’ Expr | app f a _, 0 => a | app f _ _, i+1 => getRevArg! f i | _, _ => panic! "invalid index" @[inline] def getArg! (e : Expr) (i : Nat) (n := e.getAppNumArgs) : Expr := getRevArg! e (n - i - 1) @[inline] def getArgD (e : Expr) (i : Nat) (vβ‚€ : Expr) (n := e.getAppNumArgs) : Expr := getRevArgD e (n - i - 1) vβ‚€ def isAppOf (e : Expr) (n : Name) : Bool := match e.getAppFn with | const c _ _ => c == n | _ => false def isAppOfArity : Expr β†’ Name β†’ Nat β†’ Bool | const c _ _, n, 0 => c == n | app f _ _, n, a+1 => isAppOfArity f n a | _, _, _ => false def appFn! : Expr β†’ Expr | app f _ _ => f | _ => panic! "application expected" def appArg! : Expr β†’ Expr | app _ a _ => a | _ => panic! "application expected" def isNatLit : Expr β†’ Bool | lit (Literal.natVal _) _ => true | _ => false def natLit? : Expr β†’ Option Nat | lit (Literal.natVal v) _ => v | _ => none def isStringLit : Expr β†’ Bool | lit (Literal.strVal _) _ => true | _ => false def isCharLit (e : Expr) : Bool := e.isAppOfArity `Char.ofNat 1 && e.appArg!.isNatLit def constName! : Expr β†’ Name | const n _ _ => n | _ => panic! "constant expected" def constName? : Expr β†’ Option Name | const n _ _ => some n | _ => none def constLevels! : Expr β†’ List Level | const _ ls _ => ls | _ => panic! "constant expected" def bvarIdx! : Expr β†’ Nat | bvar idx _ => idx | _ => panic! "bvar expected" def fvarId! : Expr β†’ FVarId | fvar n _ => n | _ => panic! "fvar expected" def mvarId! : Expr β†’ MVarId | mvar n _ => n | _ => panic! "mvar expected" def bindingName! : Expr β†’ Name | forallE n _ _ _ => n | lam n _ _ _ => n | _ => panic! "binding expected" def bindingDomain! : Expr β†’ Expr | forallE _ d _ _ => d | lam _ d _ _ => d | _ => panic! "binding expected" def bindingBody! : Expr β†’ Expr | forallE _ _ b _ => b | lam _ _ b _ => b | _ => panic! "binding expected" def bindingInfo! : Expr β†’ BinderInfo | forallE _ _ _ c => c.binderInfo | lam _ _ _ c => c.binderInfo | _ => panic! "binding expected" def letName! : Expr β†’ Name | letE n _ _ _ _ => n | _ => panic! "let expression expected" def consumeMData : Expr β†’ Expr | mdata _ e _ => consumeMData e | e => e def hasLooseBVars (e : Expr) : Bool := e.looseBVarRange > 0 @[extern "lean_expr_has_loose_bvar"] constant hasLooseBVar (e : @& Expr) (bvarIdx : @& Nat) : Bool := arbitrary _ /-- Return true if `e` contains the loose bound variable `bvarIdx` in an explicit parameter, or in the range if `tryRange == true`. -/ def hasLooseBVarInExplicitDomain : Expr β†’ Nat β†’ Bool β†’ Bool | Expr.forallE _ d b c, bvarIdx, tryRange => (c.binderInfo.isExplicit && hasLooseBVar d bvarIdx) || hasLooseBVarInExplicitDomain b (bvarIdx+1) tryRange | e, bvarIdx, tryRange => tryRange && hasLooseBVar e bvarIdx /-- Lower the loose bound variables `>= s` in `e` by `d`. That is, a loose bound variable `bvar i`. `i >= s` is mapped into `bvar (i-d)`. Remark: if `s < d`, then result is `e` -/ @[extern "lean_expr_lower_loose_bvars"] constant lowerLooseBVars (e : @& Expr) (s d : @& Nat) : Expr := arbitrary _ /-- Lift loose bound variables `>= s` in `e` by `d`. -/ @[extern "lean_expr_lift_loose_bvars"] constant liftLooseBVars (e : @& Expr) (s d : @& Nat) : Expr := arbitrary _ /-- `inferImplicit e numParams considerRange` updates the first `numParams` parameter binder annotations of the `e` forall type. It marks any parameter with an explicit binder annotation if there is another explicit arguments that depends on it or the resulting type if `considerRange == true`. Remark: we use this function to infer the bind annotations of inductive datatype constructors, and structure projections. When the `{}` annotation is used in these commands, we set `considerRange == false`. -/ def inferImplicit : Expr β†’ Nat β†’ Bool β†’ Expr | Expr.forallE n d b c, i+1, considerRange => let b := inferImplicit b i considerRange; let newInfo := if c.binderInfo.isExplicit && hasLooseBVarInExplicitDomain b 0 considerRange then BinderInfo.implicit else c.binderInfo; mkForall n newInfo d b | e, 0, _ => e | e, _, _ => e /-- Instantiate the loose bound variables in `e` using `subst`. That is, a loose `Expr.bvar i` is replaced with `subst[i]`. -/ @[extern "lean_expr_instantiate"] constant instantiate (e : @& Expr) (subst : @& Array Expr) : Expr := arbitrary _ @[extern "lean_expr_instantiate1"] constant instantiate1 (e : @& Expr) (subst : @& Expr) : Expr := arbitrary _ /-- Similar to instantiate, but `Expr.bvar i` is replaced with `subst[subst.size - i - 1]` -/ @[extern "lean_expr_instantiate_rev"] constant instantiateRev (e : @& Expr) (subst : @& Array Expr) : Expr := arbitrary _ /-- Similar to `instantiate`, but consider only the variables `xs` in the range `[beginIdx, endIdx)`. Function panics if `beginIdx <= endIdx <= xs.size` does not hold. -/ @[extern "lean_expr_instantiate_range"] constant instantiateRange (e : @& Expr) (beginIdx endIdx : @& Nat) (xs : @& Array Expr) : Expr := arbitrary _ /-- Similar to `instantiateRev`, but consider only the variables `xs` in the range `[beginIdx, endIdx)`. Function panics if `beginIdx <= endIdx <= xs.size` does not hold. -/ @[extern "lean_expr_instantiate_rev_range"] constant instantiateRevRange (e : @& Expr) (beginIdx endIdx : @& Nat) (xs : @& Array Expr) : Expr := arbitrary _ /-- Replace free variables `xs` with loose bound variables. -/ @[extern "lean_expr_abstract"] constant abstract (e : @& Expr) (xs : @& Array Expr) : Expr := arbitrary _ /-- Similar to `abstract`, but consider only the first `min n xs.size` entries in `xs`. -/ @[extern "lean_expr_abstract_range"] constant abstractRange (e : @& Expr) (n : @& Nat) (xs : @& Array Expr) : Expr := arbitrary _ /-- Replace occurrences of the free variable `fvar` in `e` with `v` -/ def replaceFVar (e : Expr) (fvar : Expr) (v : Expr) : Expr := (e.abstract #[fvar]).instantiate1 v /-- Replace occurrences of the free variable `fvarId` in `e` with `v` -/ def replaceFVarId (e : Expr) (fvarId : FVarId) (v : Expr) : Expr := replaceFVar e (mkFVar fvarId) v instance : HasToString Expr := ⟨Expr.dbgToString⟩ -- TODO: should not use dbgToString, but constructors. instance : HasRepr Expr := ⟨Expr.dbgToString⟩ def isAtomic : Expr β†’ Bool | Expr.const _ _ _ => true | Expr.sort _ _ => true | Expr.bvar _ _ => true | Expr.lit _ _ => true | Expr.mvar _ _ => true | Expr.fvar _ _ => true | _ => false end Expr def mkAppB (f a b : Expr) := mkApp (mkApp f a) b def mkApp2 (f a b : Expr) := mkAppB f a b def mkApp3 (f a b c : Expr) := mkApp (mkAppB f a b) c def mkApp4 (f a b c d : Expr) := mkAppB (mkAppB f a b) c d def mkApp5 (f a b c d e : Expr) := mkApp (mkApp4 f a b c d) e def mkApp6 (f a b c d e₁ eβ‚‚ : Expr) := mkAppB (mkApp4 f a b c d) e₁ eβ‚‚ def mkApp7 (f a b c d e₁ eβ‚‚ e₃ : Expr) := mkApp3 (mkApp4 f a b c d) e₁ eβ‚‚ e₃ def mkApp8 (f a b c d e₁ eβ‚‚ e₃ eβ‚„ : Expr) := mkApp4 (mkApp4 f a b c d) e₁ eβ‚‚ e₃ eβ‚„ def mkApp9 (f a b c d e₁ eβ‚‚ e₃ eβ‚„ eβ‚… : Expr) := mkApp5 (mkApp4 f a b c d) e₁ eβ‚‚ e₃ eβ‚„ eβ‚… def mkApp10 (f a b c d e₁ eβ‚‚ e₃ eβ‚„ eβ‚… e₆ : Expr) := mkApp6 (mkApp4 f a b c d) e₁ eβ‚‚ e₃ eβ‚„ eβ‚… e₆ def mkDecIsTrue (pred proof : Expr) := mkAppB (mkConst `Decidable.isTrue) pred proof def mkDecIsFalse (pred proof : Expr) := mkAppB (mkConst `Decidable.isFalse) pred proof open Std (HashMap HashSet PHashMap PHashSet) abbrev ExprMap (Ξ± : Type) := HashMap Expr Ξ± abbrev PersistentExprMap (Ξ± : Type) := PHashMap Expr Ξ± abbrev ExprSet := HashSet Expr abbrev PersistentExprSet := PHashSet Expr abbrev PExprSet := PersistentExprSet /- Auxiliary type for forcing `==` to be structural equality for `Expr` -/ structure ExprStructEq := (val : Expr) instance exprToExprStructEq : HasCoe Expr ExprStructEq := ⟨ExprStructEq.mk⟩ namespace ExprStructEq protected def beq : ExprStructEq β†’ ExprStructEq β†’ Bool | ⟨eβ‚βŸ©, ⟨eβ‚‚βŸ© => Expr.equal e₁ eβ‚‚ protected def hash : ExprStructEq β†’ USize | ⟨e⟩ => e.hash instance : Inhabited ExprStructEq := ⟨{ val := arbitrary _ }⟩ instance : HasBeq ExprStructEq := ⟨ExprStructEq.beq⟩ instance : Hashable ExprStructEq := ⟨ExprStructEq.hash⟩ instance : HasToString ExprStructEq := ⟨fun e => toString e.val⟩ instance : HasRepr ExprStructEq := ⟨fun e => repr e.val⟩ end ExprStructEq abbrev ExprStructMap (Ξ± : Type) := HashMap ExprStructEq Ξ± abbrev PersistentExprStructMap (Ξ± : Type) := PHashMap ExprStructEq Ξ± namespace Expr private partial def mkAppRevRangeAux (revArgs : Array Expr) (start : Nat) : Expr β†’ Nat β†’ Expr | b, i => if i == start then b else let i := i - 1; mkAppRevRangeAux (mkApp b (revArgs.get! i)) i /-- `mkAppRevRange f b e args == mkAppRev f (revArgs.extract b e)` -/ def mkAppRevRange (f : Expr) (beginIdx endIdx : Nat) (revArgs : Array Expr) : Expr := mkAppRevRangeAux revArgs beginIdx f endIdx private def betaRevAux (revArgs : Array Expr) (sz : Nat) : Expr β†’ Nat β†’ Expr | Expr.lam _ _ b _, i => if i + 1 < sz then betaRevAux b (i+1) else let n := sz - (i + 1); mkAppRevRange (b.instantiateRange n sz revArgs) 0 n revArgs | Expr.mdata _ b _, i => betaRevAux b i | b, i => let n := sz - i; mkAppRevRange (b.instantiateRange n sz revArgs) 0 n revArgs /-- If `f` is a lambda expression, than "beta-reduce" it using `revArgs`. This function is often used with `getAppRev` or `withAppRev`. Examples: - `betaRev (fun x y => t x y) #[]` ==> `fun x y => t x y` - `betaRev (fun x y => t x y) #[a]` ==> `fun y => t a y` - `betaRev (fun x y => t x y) #[a, b]` ==> t b a` - `betaRev (fun x y => t x y) #[a, b, c, d]` ==> t d c b a` Suppose `t` is `(fun x y => t x y) a b c d`, then `args := t.getAppRev` is `#[d, c, b, a]`, and `betaRev (fun x y => t x y) #[d, c, b, a]` is `t a b c d`. -/ def betaRev (f : Expr) (revArgs : Array Expr) : Expr := if revArgs.size == 0 then f else betaRevAux revArgs revArgs.size f 0 def isHeadBetaTargetFn : Expr β†’ Bool | Expr.lam _ _ _ _ => true | Expr.mdata _ b _ => isHeadBetaTargetFn b | _ => false def headBeta (e : Expr) : Expr := let f := e.getAppFn; if f.isHeadBetaTargetFn then betaRev f e.getAppRevArgs else e def isHeadBetaTarget (e : Expr) : Bool := e.getAppFn.isHeadBetaTargetFn private def etaExpandedBody : Expr β†’ Nat β†’ Nat β†’ Option Expr | app f (bvar j _) _, n+1, i => if j == i then etaExpandedBody f n (i+1) else none | _, n+1, _ => none | f, 0, _ => if f.hasLooseBVars then none else some f private def etaExpandedAux : Expr β†’ Nat β†’ Option Expr | lam _ _ b _, n => etaExpandedAux b (n+1) | e, n => etaExpandedBody e n 0 /-- If `e` is of the form `(fun x₁ ... xβ‚™ => f x₁ ... xβ‚™)` and `f` does not contain `x₁`, ..., `xβ‚™`, then return `some f`. Otherwise, return `none`. It assumes `e` does not have loose bound variables. Remark: `β‚™` may be 0 -/ def etaExpanded? (e : Expr) : Option Expr := etaExpandedAux e 0 /-- Similar to `etaExpanded?`, but only succeeds if `β‚™ β‰₯ 1`. -/ def etaExpandedStrict? : Expr β†’ Option Expr | lam _ _ b _ => etaExpandedAux b 1 | _ => none def getOptParamDefault? (e : Expr) : Option Expr := if e.isAppOfArity `optParam 2 then some e.appArg! else none def getAutoParamTactic? (e : Expr) : Option Expr := if e.isAppOfArity `autoParam 2 then some e.appArg! else none def isOptParam (e : Expr) : Bool := e.isAppOfArity `optParam 2 def isAutoParam (e : Expr) : Bool := e.isAppOfArity `autoParam 2 @[specialize] private partial def hasAnyFVarAux (p : FVarId β†’ Bool) : Expr β†’ Bool | e => if !e.hasFVar then false else match e with | Expr.forallE _ d b _ => hasAnyFVarAux d || hasAnyFVarAux b | Expr.lam _ d b _ => hasAnyFVarAux d || hasAnyFVarAux b | Expr.mdata _ e _ => hasAnyFVarAux e | Expr.letE _ t v b _ => hasAnyFVarAux t || hasAnyFVarAux v || hasAnyFVarAux b | Expr.app f a _ => hasAnyFVarAux f || hasAnyFVarAux a | Expr.proj _ _ e _ => hasAnyFVarAux e | Expr.localE _ _ _ _ => unreachable! | e@(Expr.fvar fvarId _) => p fvarId | e => false /-- Return true iff `e` contains a free variable which statisfies `p`. -/ @[inline] def hasAnyFVar (e : Expr) (p : FVarId β†’ Bool) : Bool := hasAnyFVarAux p e /- The update functions here are defined using C code. They will try to avoid allocating new values using pointer equality. The hypotheses `(h : e.is... = true)` are used to ensure Lean will not crash at runtime. The `update*!` functions are inlined and provide a convenient way of using the update proofs without providing proofs. Note that if they are used under a match-expression, the compiler will eliminate the double-match. -/ @[extern "lean_expr_update_app"] def updateApp (e : Expr) (newFn : Expr) (newArg : Expr) (h : e.isApp = true) : Expr := mkApp newFn newArg @[inline] def updateApp! (e : Expr) (newFn : Expr) (newArg : Expr) : Expr := match e with | app fn arg c => updateApp (app fn arg c) newFn newArg rfl | _ => panic! "application expected" @[extern "lean_expr_update_const"] def updateConst (e : Expr) (newLevels : List Level) (h : e.isConst = true) : Expr := mkConst e.constName! newLevels @[inline] def updateConst! (e : Expr) (newLevels : List Level) : Expr := match e with | const n ls c => updateConst (const n ls c) newLevels rfl | _ => panic! "constant expected" @[extern "lean_expr_update_sort"] def updateSort (e : Expr) (newLevel : Level) (h : e.isSort = true) : Expr := mkSort newLevel @[inline] def updateSort! (e : Expr) (newLevel : Level) : Expr := match e with | sort l c => updateSort (sort l c) newLevel rfl | _ => panic! "level expected" @[extern "lean_expr_update_proj"] def updateProj (e : Expr) (newExpr : Expr) (h : e.isProj = true) : Expr := match e with | proj s i _ _ => mkProj s i newExpr | _ => e -- unreachable because of `h` @[extern "lean_expr_update_mdata"] def updateMData (e : Expr) (newExpr : Expr) (h : e.isMData = true) : Expr := match e with | mdata d _ _ => mkMData d newExpr | _ => e -- unreachable because of `h` @[inline] def updateMData! (e : Expr) (newExpr : Expr) : Expr := match e with | mdata d e c => updateMData (mdata d e c) newExpr rfl | _ => panic! "mdata expected" @[inline] def updateProj! (e : Expr) (newExpr : Expr) : Expr := match e with | proj s i e c => updateProj (proj s i e c) newExpr rfl | _ => panic! "proj expected" @[extern "lean_expr_update_forall"] def updateForall (e : Expr) (newBinfo : BinderInfo) (newDomain : Expr) (newBody : Expr) (h : e.isForall = true) : Expr := mkForall e.bindingName! newBinfo newDomain newBody @[inline] def updateForall! (e : Expr) (newBinfo : BinderInfo) (newDomain : Expr) (newBody : Expr) : Expr := match e with | forallE n d b c => updateForall (forallE n d b c) newBinfo newDomain newBody rfl | _ => panic! "forall expected" @[inline] def updateForallE! (e : Expr) (newDomain : Expr) (newBody : Expr) : Expr := match e with | forallE n d b c => updateForall (forallE n d b c) c.binderInfo newDomain newBody rfl | _ => panic! "forall expected" @[extern "lean_expr_update_lambda"] def updateLambda (e : Expr) (newBinfo : BinderInfo) (newDomain : Expr) (newBody : Expr) (h : e.isLambda = true) : Expr := mkLambda e.bindingName! newBinfo newDomain newBody @[inline] def updateLambda! (e : Expr) (newBinfo : BinderInfo) (newDomain : Expr) (newBody : Expr) : Expr := match e with | lam n d b c => updateLambda (lam n d b c) newBinfo newDomain newBody rfl | _ => panic! "lambda expected" @[inline] def updateLambdaE! (e : Expr) (newDomain : Expr) (newBody : Expr) : Expr := match e with | lam n d b c => updateLambda (lam n d b c) c.binderInfo newDomain newBody rfl | _ => panic! "lambda expected" @[extern "lean_expr_update_let"] def updateLet (e : Expr) (newType : Expr) (newVal : Expr) (newBody : Expr) (h : e.isLet = true) : Expr := mkLet e.letName! newType newVal newBody @[inline] def updateLet! (e : Expr) (newType : Expr) (newVal : Expr) (newBody : Expr) : Expr := match e with | letE n t v b c => updateLet (letE n t v b c) newType newVal newBody rfl | _ => panic! "let expression expected" def updateFn : Expr β†’ Expr β†’ Expr | e@(app f a _), g => e.updateApp (updateFn f g) a rfl | _, g => g /- Instantiate level parameters -/ namespace InstantiateLevelParams @[inline] def visit (f : Expr β†’ Expr) (e : Expr) : Expr := if e.hasLevelParam then f e else e @[specialize] partial def instantiate (s : Name β†’ Option Level) : Expr β†’ Expr | e@(lam n d b _) => e.updateLambdaE! (visit instantiate d) (visit instantiate b) | e@(forallE n d b _) => e.updateForallE! (visit instantiate d) (visit instantiate b) | e@(letE n t v b _) => e.updateLet! (visit instantiate t) (visit instantiate v) (visit instantiate b) | e@(app f a _) => e.updateApp (visit instantiate f) (visit instantiate a) rfl | e@(proj _ _ s _) => e.updateProj (visit instantiate s) rfl | e@(mdata _ b _) => e.updateMData (visit instantiate b) rfl | e@(const _ us _) => e.updateConst (us.map (fun u => u.instantiateParams s)) rfl | e@(sort u _) => e.updateSort (u.instantiateParams s) rfl | localE _ _ _ _ => unreachable! | e => e end InstantiateLevelParams @[inline] def instantiateLevelParamsCore (s : Name β†’ Option Level) (e : Expr) : Expr := if e.hasLevelParam then InstantiateLevelParams.instantiate s e else e private def getParamSubst : List Name β†’ List Level β†’ Name β†’ Option Level | p::ps, u::us, p' => if p == p' then some u else getParamSubst ps us p' | _, _, _ => none def instantiateLevelParams (e : Expr) (paramNames : List Name) (lvls : List Level) : Expr := instantiateLevelParamsCore (getParamSubst paramNames lvls) e private partial def getParamSubstArray (ps : Array Name) (us : Array Level) (p' : Name) : Nat β†’ Option Level | i => if h : i < ps.size then let p := ps.get ⟨i, h⟩; if h : i < us.size then let u := us.get ⟨i, h⟩; if p == p' then some u else getParamSubstArray (i+1) else none else none def instantiateLevelParamsArray (e : Expr) (paramNames : Array Name) (lvls : Array Level) : Expr := instantiateLevelParamsCore (fun p => getParamSubstArray paramNames lvls p 0) e end Expr def mkAnnotation (kind : Name) (e : Expr) : Expr := mkMData (KVMap.empty.insert kind (DataValue.ofBool true)) e def annotation? (kind : Name) (e : Expr) : Option Expr := match e with | Expr.mdata d b _ => if d.size == 1 && d.getBool kind false then some b else none | _ => none def mkFreshFVarId {m : Type β†’ Type} [Monad m] [MonadNameGenerator m] : m FVarId := mkFreshId def mkFreshMVarId {m : Type β†’ Type} [Monad m] [MonadNameGenerator m] : m FVarId := mkFreshId end Lean
424941d28527c1d9296840a5f724cec06bf66e93
5749d8999a76f3a8fddceca1f6941981e33aaa96
/src/measure_theory/measurable_space.lean
b191a97979afe2486c94387546352270e6e86a37
[ "Apache-2.0" ]
permissive
jdsalchow/mathlib
13ab43ef0d0515a17e550b16d09bd14b76125276
497e692b946d93906900bb33a51fd243e7649406
refs/heads/master
1,585,819,143,348
1,580,072,892,000
1,580,072,892,000
154,287,128
0
0
Apache-2.0
1,540,281,610,000
1,540,281,609,000
null
UTF-8
Lean
false
false
42,998
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 Measurable spaces -- Οƒ-algberas -/ import data.set.disjointed order.galois_connection data.set.countable /-! # Measurable spaces and measurable functions This file defines measurable spaces and the functions and isomorphisms between them. A measurable space is a set equipped with a Οƒ-algebra, a collection of subsets closed under complementation and countable union. A function between measurable spaces is measurable if the preimage of each measurable subset is measurable. Οƒ-algebras on a fixed set Ξ± form a complete lattice. Here we order Οƒ-algebras by writing m₁ ≀ mβ‚‚ if every set which is m₁-measurable is also mβ‚‚-measurable (that is, m₁ is a subset of mβ‚‚). In particular, any collection of subsets of Ξ± generates a smallest Οƒ-algebra which contains all of them. A function f : Ξ± β†’ Ξ² induces a Galois connection between the lattices of Οƒ-algebras on Ξ± and Ξ². A measurable equivalence between measurable spaces is an equivalence which respects the Οƒ-algebras, that is, for which both directions of the equivalence are measurable functions. ## Main statements The main theorem of this file is Dynkin's Ο€-Ξ» theorem, which appears here as an induction principle `induction_on_inter`. Suppose s is a collection of subsets of Ξ± such that the intersection of two members of s belongs to s whenever it is nonempty. Let m be the Οƒ-algebra generated by s. In order to check that a predicate C holds on every member of m, it suffices to check that C holds on the members of s and that C is preserved by complementation and *disjoint* countable unions. ## Implementation notes Measurability of a function f : Ξ± β†’ Ξ² between measurable spaces is defined in terms of the Galois connection induced by f. ## References * <https://en.wikipedia.org/wiki/Measurable_space> * <https://en.wikipedia.org/wiki/Sigma-algebra> * <https://en.wikipedia.org/wiki/Dynkin_system> ## Tags measurable space, measurable function, dynkin system -/ local attribute [instance] classical.prop_decidable open set lattice encodable open_locale classical universes u v w x variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type x} {ΞΉ : Sort x} {s t u : set Ξ±} structure measurable_space (Ξ± : Type u) := (is_measurable : set Ξ± β†’ Prop) (is_measurable_empty : is_measurable βˆ…) (is_measurable_compl : βˆ€s, is_measurable s β†’ is_measurable (- s)) (is_measurable_Union : βˆ€f:β„• β†’ set Ξ±, (βˆ€i, is_measurable (f i)) β†’ is_measurable (⋃i, f i)) attribute [class] measurable_space section variable [measurable_space Ξ±] /-- `is_measurable s` means that `s` is measurable (in the ambient measure space on `Ξ±`) -/ def is_measurable : set Ξ± β†’ Prop := β€Ήmeasurable_space Ξ±β€Ί.is_measurable lemma is_measurable.empty : is_measurable (βˆ… : set Ξ±) := β€Ήmeasurable_space Ξ±β€Ί.is_measurable_empty lemma is_measurable.compl : is_measurable s β†’ is_measurable (-s) := β€Ήmeasurable_space Ξ±β€Ί.is_measurable_compl s lemma is_measurable.compl_iff : is_measurable (-s) ↔ is_measurable s := ⟨λ h, by simpa using h.compl, is_measurable.compl⟩ lemma is_measurable.univ : is_measurable (univ : set Ξ±) := by simpa using (@is_measurable.empty Ξ± _).compl lemma encodable.Union_decode2 {Ξ±} [encodable Ξ²] (f : Ξ² β†’ set Ξ±) : (⋃ b, f b) = ⋃ (i : β„•) (b ∈ decode2 Ξ² i), f b := ext $ by simp [mem_decode2, exists_swap] @[elab_as_eliminator] lemma encodable.Union_decode2_cases {Ξ±} [encodable Ξ²] {f : Ξ² β†’ set Ξ±} {C : set Ξ± β†’ Prop} (H0 : C βˆ…) (H1 : βˆ€ b, C (f b)) {n} : C (⋃ b ∈ decode2 Ξ² n, f b) := match decode2 Ξ² n with | none := by simp; apply H0 | (some b) := by convert H1 b; simp [ext_iff] end lemma is_measurable.Union [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (h : βˆ€b, is_measurable (f b)) : is_measurable (⋃b, f b) := by rw encodable.Union_decode2; exact β€Ήmeasurable_space Ξ±β€Ί.is_measurable_Union (Ξ» n, ⋃ b ∈ decode2 Ξ² n, f b) (Ξ» n, encodable.Union_decode2_cases is_measurable.empty h) lemma is_measurable.bUnion {f : Ξ² β†’ set Ξ±} {s : set Ξ²} (hs : countable s) (h : βˆ€b∈s, is_measurable (f b)) : is_measurable (⋃b∈s, f b) := begin rw bUnion_eq_Union, haveI := hs.to_encodable, exact is_measurable.Union (by simpa using h) end lemma is_measurable.sUnion {s : set (set Ξ±)} (hs : countable s) (h : βˆ€t∈s, is_measurable t) : is_measurable (⋃₀ s) := by rw sUnion_eq_bUnion; exact is_measurable.bUnion hs h lemma is_measurable.Union_Prop {p : Prop} {f : p β†’ set Ξ±} (hf : βˆ€b, is_measurable (f b)) : is_measurable (⋃b, f b) := by by_cases p; simp [h, hf, is_measurable.empty] lemma is_measurable.Inter [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (h : βˆ€b, is_measurable (f b)) : is_measurable (β‹‚b, f b) := is_measurable.compl_iff.1 $ by rw compl_Inter; exact is_measurable.Union (Ξ» b, (h b).compl) lemma is_measurable.bInter {f : Ξ² β†’ set Ξ±} {s : set Ξ²} (hs : countable s) (h : βˆ€b∈s, is_measurable (f b)) : is_measurable (β‹‚b∈s, f b) := is_measurable.compl_iff.1 $ by rw compl_bInter; exact is_measurable.bUnion hs (Ξ» b hb, (h b hb).compl) lemma is_measurable.sInter {s : set (set Ξ±)} (hs : countable s) (h : βˆ€t∈s, is_measurable t) : is_measurable (β‹‚β‚€ s) := by rw sInter_eq_bInter; exact is_measurable.bInter hs h lemma is_measurable.Inter_Prop {p : Prop} {f : p β†’ set Ξ±} (hf : βˆ€b, is_measurable (f b)) : is_measurable (β‹‚b, f b) := by by_cases p; simp [h, hf, is_measurable.univ] lemma is_measurable.union {s₁ sβ‚‚ : set Ξ±} (h₁ : is_measurable s₁) (hβ‚‚ : is_measurable sβ‚‚) : is_measurable (s₁ βˆͺ sβ‚‚) := by rw union_eq_Union; exact is_measurable.Union (bool.forall_bool.2 ⟨hβ‚‚, hβ‚βŸ©) lemma is_measurable.inter {s₁ sβ‚‚ : set Ξ±} (h₁ : is_measurable s₁) (hβ‚‚ : is_measurable sβ‚‚) : is_measurable (s₁ ∩ sβ‚‚) := by rw inter_eq_compl_compl_union_compl; exact (h₁.compl.union hβ‚‚.compl).compl lemma is_measurable.diff {s₁ sβ‚‚ : set Ξ±} (h₁ : is_measurable s₁) (hβ‚‚ : is_measurable sβ‚‚) : is_measurable (s₁ \ sβ‚‚) := h₁.inter hβ‚‚.compl lemma is_measurable.sub {s₁ sβ‚‚ : set Ξ±} : is_measurable s₁ β†’ is_measurable sβ‚‚ β†’ is_measurable (s₁ - sβ‚‚) := is_measurable.diff lemma is_measurable.disjointed {f : β„• β†’ set Ξ±} (h : βˆ€i, is_measurable (f i)) (n) : is_measurable (disjointed f n) := disjointed_induct (h n) (assume t i ht, is_measurable.diff ht $ h _) lemma is_measurable.const (p : Prop) : is_measurable {a : Ξ± | p} := by by_cases p; simp [h, is_measurable.empty]; apply is_measurable.univ end @[ext] lemma measurable_space.ext : βˆ€{m₁ mβ‚‚ : measurable_space Ξ±}, (βˆ€s:set Ξ±, m₁.is_measurable s ↔ mβ‚‚.is_measurable s) β†’ m₁ = mβ‚‚ | ⟨s₁, _, _, _⟩ ⟨sβ‚‚, _, _, _⟩ h := have s₁ = sβ‚‚, from funext $ assume x, propext $ h x, by subst this namespace measurable_space section complete_lattice instance : partial_order (measurable_space Ξ±) := { le := Ξ»m₁ mβ‚‚, m₁.is_measurable ≀ mβ‚‚.is_measurable, le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ hβ‚‚, measurable_space.ext $ assume s, ⟨h₁ s, hβ‚‚ s⟩ } /-- The smallest Οƒ-algebra containing a collection `s` of basic sets -/ inductive generate_measurable (s : set (set Ξ±)) : set Ξ± β†’ Prop | basic : βˆ€u∈s, generate_measurable u | empty : generate_measurable βˆ… | compl : βˆ€s, generate_measurable s β†’ generate_measurable (-s) | union : βˆ€f:β„• β†’ set Ξ±, (βˆ€n, generate_measurable (f n)) β†’ generate_measurable (⋃i, f i) /-- Construct the smallest measure space containing a collection of basic sets -/ def generate_from (s : set (set Ξ±)) : measurable_space Ξ± := { is_measurable := generate_measurable s, is_measurable_empty := generate_measurable.empty s, is_measurable_compl := generate_measurable.compl, is_measurable_Union := generate_measurable.union } lemma is_measurable_generate_from {s : set (set Ξ±)} {t : set Ξ±} (ht : t ∈ s) : (generate_from s).is_measurable t := generate_measurable.basic t ht lemma generate_from_le {s : set (set Ξ±)} {m : measurable_space Ξ±} (h : βˆ€t∈s, m.is_measurable t) : generate_from s ≀ m := assume t (ht : generate_measurable s t), ht.rec_on h (is_measurable_empty m) (assume s _ hs, is_measurable_compl m s hs) (assume f _ hf, is_measurable_Union m f hf) lemma generate_from_le_iff {s : set (set Ξ±)} {m : measurable_space Ξ±} : generate_from s ≀ m ↔ s βŠ† {t | m.is_measurable t} := iff.intro (assume h u hu, h _ $ is_measurable_generate_from hu) (assume h, generate_from_le h) protected def mk_of_closure (g : set (set Ξ±)) (hg : {t | (generate_from g).is_measurable t} = g) : measurable_space Ξ± := { is_measurable := Ξ»s, s ∈ g, is_measurable_empty := hg β–Έ is_measurable_empty _, is_measurable_compl := hg β–Έ is_measurable_compl _, is_measurable_Union := hg β–Έ is_measurable_Union _ } lemma mk_of_closure_sets {s : set (set Ξ±)} {hs : {t | (generate_from s).is_measurable t} = s} : measurable_space.mk_of_closure s hs = generate_from s := measurable_space.ext $ assume t, show t ∈ s ↔ _, by rw [← hs] {occs := occurrences.pos [1] }; refl def gi_generate_from : galois_insertion (@generate_from Ξ±) (Ξ»m, {t | @is_measurable Ξ± m t}) := { gc := assume s m, generate_from_le_iff, le_l_u := assume m s, is_measurable_generate_from, choice := Ξ»g hg, measurable_space.mk_of_closure g $ le_antisymm hg $ generate_from_le_iff.1 $ le_refl _, choice_eq := assume g hg, mk_of_closure_sets } instance : complete_lattice (measurable_space Ξ±) := gi_generate_from.lift_complete_lattice instance : inhabited (measurable_space Ξ±) := ⟨⊀⟩ lemma is_measurable_bot_iff {s : set Ξ±} : @is_measurable Ξ± βŠ₯ s ↔ (s = βˆ… ∨ s = univ) := let b : measurable_space Ξ± := { is_measurable := Ξ»s, s = βˆ… ∨ s = univ, is_measurable_empty := or.inl rfl, is_measurable_compl := by simp [or_imp_distrib] {contextual := tt}, is_measurable_Union := assume f hf, classical.by_cases (assume h : βˆƒi, f i = univ, let ⟨i, hi⟩ := h in or.inr $ eq_univ_of_univ_subset $ hi β–Έ le_supr f i) (assume h : Β¬ βˆƒi, f i = univ, or.inl $ eq_empty_of_subset_empty $ Union_subset $ assume i, (hf i).elim (by simp {contextual := tt}) (assume hi, false.elim $ h ⟨i, hi⟩)) } in have b = βŠ₯, from bot_unique $ assume s hs, hs.elim (assume s, s.symm β–Έ @is_measurable_empty _ βŠ₯) (assume s, s.symm β–Έ @is_measurable.univ _ βŠ₯), this β–Έ iff.rfl @[simp] theorem is_measurable_top {s : set Ξ±} : @is_measurable _ ⊀ s := trivial @[simp] theorem is_measurable_inf {m₁ mβ‚‚ : measurable_space Ξ±} {s : set Ξ±} : @is_measurable _ (m₁ βŠ“ mβ‚‚) s ↔ @is_measurable _ m₁ s ∧ @is_measurable _ mβ‚‚ s := iff.rfl @[simp] theorem is_measurable_Inf {ms : set (measurable_space Ξ±)} {s : set Ξ±} : @is_measurable _ (Inf ms) s ↔ βˆ€ m ∈ ms, @is_measurable _ m s := show s ∈ (β‹‚m∈ms, {t | @is_measurable _ m t }) ↔ _, by simp @[simp] theorem is_measurable_infi {ΞΉ} {m : ΞΉ β†’ measurable_space Ξ±} {s : set Ξ±} : @is_measurable _ (infi m) s ↔ βˆ€ i, @is_measurable _ (m i) s := show s ∈ (Ξ»m, {s | @is_measurable _ m s }) (infi m) ↔ _, by rw (@gi_generate_from Ξ±).gc.u_infi; simp; refl end complete_lattice section functors variables {m m₁ mβ‚‚ : measurable_space Ξ±} {m' : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} /-- The forward image of a measure space under a function. `map f m` contains the sets `s : set Ξ²` whose preimage under `f` is measurable. -/ protected def map (f : Ξ± β†’ Ξ²) (m : measurable_space Ξ±) : measurable_space Ξ² := { is_measurable := Ξ»s, m.is_measurable $ f ⁻¹' s, is_measurable_empty := m.is_measurable_empty, is_measurable_compl := assume s hs, m.is_measurable_compl _ hs, is_measurable_Union := assume f hf, by rw [preimage_Union]; exact m.is_measurable_Union _ hf } @[simp] lemma map_id : m.map id = m := measurable_space.ext $ assume s, iff.rfl @[simp] lemma map_comp {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ³} : (m.map f).map g = m.map (g ∘ f) := measurable_space.ext $ assume s, iff.rfl /-- The reverse image of a measure space under a function. `comap f m` contains the sets `s : set Ξ±` such that `s` is the `f`-preimage of a measurable set in `Ξ²`. -/ protected def comap (f : Ξ± β†’ Ξ²) (m : measurable_space Ξ²) : measurable_space Ξ± := { is_measurable := Ξ»s, βˆƒs', m.is_measurable s' ∧ f ⁻¹' s' = s, is_measurable_empty := βŸ¨βˆ…, m.is_measurable_empty, rfl⟩, is_measurable_compl := assume s ⟨s', h₁, hβ‚‚βŸ©, ⟨-s', m.is_measurable_compl _ h₁, hβ‚‚ β–Έ rfl⟩, is_measurable_Union := assume s hs, let ⟨s', hs'⟩ := classical.axiom_of_choice hs in βŸ¨β‹ƒi, s' i, m.is_measurable_Union _ (Ξ»i, (hs' i).left), by simp [hs'] ⟩ } @[simp] lemma comap_id : m.comap id = m := measurable_space.ext $ assume s, ⟨assume ⟨s', hs', h⟩, h β–Έ hs', assume h, ⟨s, h, rfl⟩⟩ @[simp] lemma comap_comp {f : Ξ² β†’ Ξ±} {g : Ξ³ β†’ Ξ²} : (m.comap f).comap g = m.comap (f ∘ g) := measurable_space.ext $ assume s, ⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht β–Έ hu β–Έ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩ lemma comap_le_iff_le_map {f : Ξ± β†’ Ξ²} : m'.comap f ≀ m ↔ m' ≀ m.map f := ⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq β–Έ h _ ht⟩ lemma gc_comap_map (f : Ξ± β†’ Ξ²) : galois_connection (measurable_space.comap f) (measurable_space.map f) := assume f g, comap_le_iff_le_map lemma map_mono (h : m₁ ≀ mβ‚‚) : m₁.map f ≀ mβ‚‚.map f := (gc_comap_map f).monotone_u h lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h lemma comap_mono (h : m₁ ≀ mβ‚‚) : m₁.comap g ≀ mβ‚‚.comap g := (gc_comap_map g).monotone_l h lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h @[simp] lemma comap_bot : (βŠ₯:measurable_space Ξ±).comap g = βŠ₯ := (gc_comap_map g).l_bot @[simp] lemma comap_sup : (m₁ βŠ” mβ‚‚).comap g = m₁.comap g βŠ” mβ‚‚.comap g := (gc_comap_map g).l_sup @[simp] lemma comap_supr {m : ΞΉ β†’ measurable_space Ξ±} :(⨆i, m i).comap g = (⨆i, (m i).comap g) := (gc_comap_map g).l_supr @[simp] lemma map_top : (⊀:measurable_space Ξ±).map f = ⊀ := (gc_comap_map f).u_top @[simp] lemma map_inf : (m₁ βŠ“ mβ‚‚).map f = m₁.map f βŠ“ mβ‚‚.map f := (gc_comap_map f).u_inf @[simp] lemma map_infi {m : ΞΉ β†’ measurable_space Ξ±} : (β¨…i, m i).map f = (β¨…i, (m i).map f) := (gc_comap_map f).u_infi lemma comap_map_le : (m.map f).comap f ≀ m := (gc_comap_map f).l_u_le _ lemma le_map_comap : m ≀ (m.comap g).map g := (gc_comap_map g).le_u_l _ end functors lemma generate_from_le_generate_from {s t : set (set Ξ±)} (h : s βŠ† t) : generate_from s ≀ generate_from t := gi_generate_from.gc.monotone_l h lemma generate_from_sup_generate_from {s t : set (set Ξ±)} : generate_from s βŠ” generate_from t = generate_from (s βˆͺ t) := (@gi_generate_from Ξ±).gc.l_sup.symm lemma comap_generate_from {f : Ξ± β†’ Ξ²} {s : set (set Ξ²)} : (generate_from s).comap f = generate_from (preimage f '' s) := le_antisymm (comap_le_iff_le_map.2 $ generate_from_le $ assume t hts, generate_measurable.basic _ $ mem_image_of_mem _ $ hts) (generate_from_le $ assume t ⟨u, hu, eq⟩, eq β–Έ ⟨u, generate_measurable.basic _ hu, rfl⟩) end measurable_space section measurable_functions open measurable_space /-- A function `f` between measurable spaces is measurable if the preimage of every measurable set is measurable. -/ def measurable [m₁ : measurable_space Ξ±] [mβ‚‚ : measurable_space Ξ²] (f : Ξ± β†’ Ξ²) : Prop := mβ‚‚ ≀ m₁.map f lemma measurable_id [measurable_space Ξ±] : measurable (@id Ξ±) := le_refl _ lemma measurable.preimage [measurable_space Ξ±] [measurable_space Ξ²] {f : Ξ± β†’ Ξ²} (hf : measurable f) {s : set Ξ²} : is_measurable s β†’ is_measurable (f ⁻¹' s) := hf _ lemma measurable.comp [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} (hg : measurable g) (hf : measurable f) : measurable (g ∘ f) := le_trans hg $ map_mono hf lemma measurable_generate_from [measurable_space Ξ±] {s : set (set Ξ²)} {f : Ξ± β†’ Ξ²} (h : βˆ€t∈s, is_measurable (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f := generate_from_le h lemma measurable.if [measurable_space Ξ±] [measurable_space Ξ²] {p : Ξ± β†’ Prop} {h : decidable_pred p} {f g : Ξ± β†’ Ξ²} (hp : is_measurable {a | p a}) (hf : measurable f) (hg : measurable g) : measurable (Ξ»a, if p a then f a else g a) := Ξ» s hs, show is_measurable {a | (if p a then f a else g a) ∈ s}, begin convert (hp.inter $ hf s hs).union (hp.compl.inter $ hg s hs), exact ext (Ξ» a, by by_cases p a ; { rw mem_def, simp [h] }) end lemma measurable_const {Ξ± Ξ²} [measurable_space Ξ±] [measurable_space Ξ²] {a : Ξ±} : measurable (Ξ»b:Ξ², a) := assume s hs, show is_measurable {b : Ξ² | a ∈ s}, from classical.by_cases (assume h : a ∈ s, by simp [h]; from is_measurable.univ) (assume h : a βˆ‰ s, by simp [h]; from is_measurable.empty) lemma measurable_zero {Ξ± Ξ²} [measurable_space Ξ±] [has_zero Ξ±] [measurable_space Ξ²] : measurable (Ξ»b:Ξ², (0:Ξ±)) := measurable_const end measurable_functions section constructions instance : measurable_space empty := ⊀ instance : measurable_space unit := ⊀ instance : measurable_space bool := ⊀ instance : measurable_space β„• := ⊀ instance : measurable_space β„€ := ⊀ lemma measurable_unit [measurable_space Ξ±] (f : unit β†’ Ξ±) : measurable f := have f = (Ξ»u, f ()) := funext $ assume ⟨⟩, rfl, by rw this; exact measurable_const section nat lemma measurable_from_nat [measurable_space Ξ±] {f : β„• β†’ Ξ±} : measurable f := assume s hs, show is_measurable {n : β„• | f n ∈ s}, from trivial lemma measurable_to_nat [measurable_space Ξ±] {f : Ξ± β†’ β„•} : (βˆ€ k, is_measurable {x | f x = k}) β†’ measurable f := begin assume h s hs, show is_measurable {x | f x ∈ s}, have : {x | f x ∈ s} = ⋃ (n ∈ s), {x | f x = n}, { ext, simp }, rw this, simp [is_measurable.Union, is_measurable.Union_Prop, h] end lemma measurable_find_greatest [measurable_space Ξ±] {p : β„• β†’ Ξ± β†’ Prop} : βˆ€ {N}, (βˆ€ k ≀ N, is_measurable {x | nat.find_greatest (Ξ» n, p n x) N = k}) β†’ measurable (Ξ» x, nat.find_greatest (Ξ» n, p n x) N) | 0 := assume h s hs, show is_measurable {x : Ξ± | (nat.find_greatest (Ξ» n, p n x) 0) ∈ s}, begin by_cases h : 0 ∈ s, { convert is_measurable.univ, simp only [nat.find_greatest_zero, h] }, { convert is_measurable.empty, simp only [nat.find_greatest_zero, h], refl } end | (n + 1) := assume h, begin apply measurable_to_nat, assume k, by_cases hk : k ≀ n + 1, { exact h k hk }, { have := is_measurable.empty, rw ← set_of_false at this, convert this, funext, rw eq_false, assume h, rw ← h at hk, have := nat.find_greatest_le, contradiction } end end nat section subtype instance {p : Ξ± β†’ Prop} [m : measurable_space Ξ±] : measurable_space (subtype p) := m.comap subtype.val lemma measurable.subtype_val [measurable_space Ξ±] [measurable_space Ξ²] {p : Ξ² β†’ Prop} {f : Ξ± β†’ subtype p} (hf : measurable f) : measurable (Ξ»a:Ξ±, (f a).val) := measurable.comp (measurable_space.comap_le_iff_le_map.mp (le_refl _)) hf lemma measurable.subtype_mk [measurable_space Ξ±] [measurable_space Ξ²] {p : Ξ² β†’ Prop} {f : Ξ± β†’ subtype p} (hf : measurable (Ξ»a, (f a).val)) : measurable f := measurable_space.comap_le_iff_le_map.mpr $ by rw [measurable_space.map_comp]; exact hf lemma is_measurable_subtype_image [measurable_space Ξ±] {s : set Ξ±} {t : set s} (hs : is_measurable s) : is_measurable t β†’ is_measurable ((coe : s β†’ Ξ±) '' t) | ⟨u, (hu : is_measurable u), (eq : coe ⁻¹' u = t)⟩ := begin rw [← eq, image_preimage_eq_inter_range, range_coe_subtype], exact is_measurable.inter hu hs end lemma measurable_of_measurable_union_cover [measurable_space Ξ±] [measurable_space Ξ²] {f : Ξ± β†’ Ξ²} (s t : set Ξ±) (hs : is_measurable s) (ht : is_measurable t) (h : univ βŠ† s βˆͺ t) (hc : measurable (Ξ»a:s, f a)) (hd : measurable (Ξ»a:t, f a)) : measurable f := assume u (hu : is_measurable u), show is_measurable (f ⁻¹' u), from begin rw show f ⁻¹' u = coe '' (coe ⁻¹' (f ⁻¹' u) : set s) βˆͺ coe '' (coe ⁻¹' (f ⁻¹' u) : set t), by rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, range_coe_subtype, range_coe_subtype, ← inter_distrib_left, univ_subset_iff.1 h, inter_univ], exact is_measurable.union (is_measurable_subtype_image hs (hc _ hu)) (is_measurable_subtype_image ht (hd _ hu)) end end subtype section prod instance [m₁ : measurable_space Ξ±] [mβ‚‚ : measurable_space Ξ²] : measurable_space (Ξ± Γ— Ξ²) := m₁.comap prod.fst βŠ” mβ‚‚.comap prod.snd lemma measurable.fst [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf : measurable f) : measurable (Ξ»a:Ξ±, (f a).1) := measurable.comp (measurable_space.comap_le_iff_le_map.mp le_sup_left) hf lemma measurable.snd [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf : measurable f) : measurable (Ξ»a:Ξ±, (f a).2) := measurable.comp (measurable_space.comap_le_iff_le_map.mp le_sup_right) hf lemma measurable.prod [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf₁ : measurable (Ξ»a, (f a).1)) (hfβ‚‚ : measurable (Ξ»a, (f a).2)) : measurable f := sup_le (by rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp]; exact hf₁) (by rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp]; exact hfβ‚‚) lemma measurable.prod_mk [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} (hf : measurable f) (hg : measurable g) : measurable (Ξ»a:Ξ±, (f a, g a)) := measurable.prod hf hg lemma is_measurable_set_prod [measurable_space Ξ±] [measurable_space Ξ²] {s : set Ξ±} {t : set Ξ²} (hs : is_measurable s) (ht : is_measurable t) : is_measurable (set.prod s t) := is_measurable.inter (measurable.fst measurable_id _ hs) (measurable.snd measurable_id _ ht) end prod section pi instance measurable_space.pi {Ξ± : Type u} {Ξ² : Ξ± β†’ Type v} [m : Ξ a, measurable_space (Ξ² a)] : measurable_space (Ξ a, Ξ² a) := ⨆a, (m a).comap (Ξ»b, b a) lemma measurable_pi_apply {Ξ± : Type u} {Ξ² : Ξ± β†’ Type v} [Ξ a, measurable_space (Ξ² a)] (a : Ξ±) : measurable (Ξ»f:Ξ a, Ξ² a, f a) := measurable_space.comap_le_iff_le_map.1 $ lattice.le_supr _ a lemma measurable_pi_lambda {Ξ± : Type u} {Ξ² : Ξ± β†’ Type v} {Ξ³ : Type w} [Ξ a, measurable_space (Ξ² a)] [measurable_space Ξ³] (f : Ξ³ β†’ Ξ a, Ξ² a) (hf : βˆ€a, measurable (Ξ»c, f c a)) : measurable f := lattice.supr_le $ assume a, measurable_space.comap_le_iff_le_map.2 (hf a) end pi instance [m₁ : measurable_space Ξ±] [mβ‚‚ : measurable_space Ξ²] : measurable_space (Ξ± βŠ• Ξ²) := m₁.map sum.inl βŠ“ mβ‚‚.map sum.inr section sum variables [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] lemma measurable_inl : measurable (@sum.inl Ξ± Ξ²) := inf_le_left lemma measurable_inr : measurable (@sum.inr Ξ± Ξ²) := inf_le_right lemma measurable_sum {f : Ξ± βŠ• Ξ² β†’ Ξ³} (hl : measurable (f ∘ sum.inl)) (hr : measurable (f ∘ sum.inr)) : measurable f := measurable_space.comap_le_iff_le_map.1 $ le_inf (measurable_space.comap_le_iff_le_map.2 $ hl) (measurable_space.comap_le_iff_le_map.2 $ hr) lemma measurable_sum_rec {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ³} (hf : measurable f) (hg : measurable g) : @measurable (Ξ± βŠ• Ξ²) Ξ³ _ _ (@sum.rec Ξ± Ξ² (Ξ»_, Ξ³) f g) := measurable_sum hf hg lemma is_measurable_inl_image {s : set Ξ±} (hs : is_measurable s) : is_measurable (sum.inl '' s : set (Ξ± βŠ• Ξ²)) := ⟨show is_measurable (sum.inl ⁻¹' _), by rwa [preimage_image_eq]; exact (assume a b, sum.inl.inj), have sum.inr ⁻¹' (sum.inl '' s : set (Ξ± βŠ• Ξ²)) = βˆ… := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show is_measurable (sum.inr ⁻¹' _), by rw [this]; exact is_measurable.empty⟩ lemma is_measurable_range_inl : is_measurable (range sum.inl : set (Ξ± βŠ• Ξ²)) := by rw [← image_univ]; exact is_measurable_inl_image is_measurable.univ lemma is_measurable_inr_image {s : set Ξ²} (hs : is_measurable s) : is_measurable (sum.inr '' s : set (Ξ± βŠ• Ξ²)) := ⟨ have sum.inl ⁻¹' (sum.inr '' s : set (Ξ± βŠ• Ξ²)) = βˆ… := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show is_measurable (sum.inl ⁻¹' _), by rw [this]; exact is_measurable.empty, show is_measurable (sum.inr ⁻¹' _), by rwa [preimage_image_eq]; exact (assume a b, sum.inr.inj)⟩ lemma is_measurable_range_inr : is_measurable (range sum.inr : set (Ξ± βŠ• Ξ²)) := by rw [← image_univ]; exact is_measurable_inr_image is_measurable.univ end sum instance {Ξ² : Ξ± β†’ Type v} [m : Ξ a, measurable_space (Ξ² a)] : measurable_space (sigma Ξ²) := β¨…a, (m a).map (sigma.mk a) end constructions /-- Equivalences between measurable spaces. Main application is the simplification of measurability statements along measurable equivalences. -/ structure measurable_equiv (Ξ± Ξ² : Type*) [measurable_space Ξ±] [measurable_space Ξ²] extends Ξ± ≃ Ξ² := (measurable_to_fun : measurable to_fun) (measurable_inv_fun : measurable inv_fun) namespace measurable_equiv instance (Ξ± Ξ²) [measurable_space Ξ±] [measurable_space Ξ²] : has_coe_to_fun (measurable_equiv Ξ± Ξ²) := ⟨λ_, Ξ± β†’ Ξ², Ξ»e, e.to_equiv⟩ lemma coe_eq {Ξ± Ξ²} [measurable_space Ξ±] [measurable_space Ξ²] (e : measurable_equiv Ξ± Ξ²) : (e : Ξ± β†’ Ξ²) = e.to_equiv := rfl def refl (Ξ± : Type*) [measurable_space Ξ±] : measurable_equiv Ξ± Ξ± := { to_equiv := equiv.refl Ξ±, measurable_to_fun := measurable_id, measurable_inv_fun := measurable_id } def trans [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] (ab : measurable_equiv Ξ± Ξ²) (bc : measurable_equiv Ξ² Ξ³) : measurable_equiv Ξ± Ξ³ := { to_equiv := ab.to_equiv.trans bc.to_equiv, measurable_to_fun := bc.measurable_to_fun.comp ab.measurable_to_fun, measurable_inv_fun := ab.measurable_inv_fun.comp bc.measurable_inv_fun } lemma trans_to_equiv {Ξ± Ξ²} [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] (e : measurable_equiv Ξ± Ξ²) (f : measurable_equiv Ξ² Ξ³) : (e.trans f).to_equiv = e.to_equiv.trans f.to_equiv := rfl def symm [measurable_space Ξ±] [measurable_space Ξ²] (ab : measurable_equiv Ξ± Ξ²) : measurable_equiv Ξ² Ξ± := { to_equiv := ab.to_equiv.symm, measurable_to_fun := ab.measurable_inv_fun, measurable_inv_fun := ab.measurable_to_fun } lemma symm_to_equiv {Ξ± Ξ²} [measurable_space Ξ±] [measurable_space Ξ²] (e : measurable_equiv Ξ± Ξ²) : e.symm.to_equiv = e.to_equiv.symm := rfl protected def cast {Ξ± Ξ²} [i₁ : measurable_space Ξ±] [iβ‚‚ : measurable_space Ξ²] (h : Ξ± = Ξ²) (hi : i₁ == iβ‚‚) : measurable_equiv Ξ± Ξ² := { to_equiv := equiv.cast h, measurable_to_fun := by unfreezeI; subst h; subst hi; exact measurable_id, measurable_inv_fun := by unfreezeI; subst h; subst hi; exact measurable_id } protected lemma measurable {Ξ± Ξ²} [measurable_space Ξ±] [measurable_space Ξ²] (e : measurable_equiv Ξ± Ξ²) : measurable (e : Ξ± β†’ Ξ²) := e.measurable_to_fun protected lemma measurable_coe_iff {Ξ± Ξ² Ξ³} [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] {f : Ξ² β†’ Ξ³} (e : measurable_equiv Ξ± Ξ²) : measurable (f ∘ e) ↔ measurable f := iff.intro (assume hfe, have measurable (f ∘ (e.symm.trans e).to_equiv) := hfe.comp e.symm.measurable, by rwa [trans_to_equiv, symm_to_equiv, equiv.symm_trans] at this) (Ξ»h, h.comp e.measurable) def prod_congr [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] [measurable_space Ξ΄] (ab : measurable_equiv Ξ± Ξ²) (cd : measurable_equiv Ξ³ Ξ΄) : measurable_equiv (Ξ± Γ— Ξ³) (Ξ² Γ— Ξ΄) := { to_equiv := equiv.prod_congr ab.to_equiv cd.to_equiv, measurable_to_fun := measurable.prod_mk (ab.measurable_to_fun.comp (measurable.fst measurable_id)) (cd.measurable_to_fun.comp (measurable.snd measurable_id)), measurable_inv_fun := measurable.prod_mk (ab.measurable_inv_fun.comp (measurable.fst measurable_id)) (cd.measurable_inv_fun.comp (measurable.snd measurable_id)) } def prod_comm [measurable_space Ξ±] [measurable_space Ξ²] : measurable_equiv (Ξ± Γ— Ξ²) (Ξ² Γ— Ξ±) := { to_equiv := equiv.prod_comm Ξ± Ξ², measurable_to_fun := measurable.prod_mk (measurable.snd measurable_id) (measurable.fst measurable_id), measurable_inv_fun := measurable.prod_mk (measurable.snd measurable_id) (measurable.fst measurable_id) } def sum_congr [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] [measurable_space Ξ΄] (ab : measurable_equiv Ξ± Ξ²) (cd : measurable_equiv Ξ³ Ξ΄) : measurable_equiv (Ξ± βŠ• Ξ³) (Ξ² βŠ• Ξ΄) := { to_equiv := equiv.sum_congr ab.to_equiv cd.to_equiv, measurable_to_fun := begin cases ab with ab' abm, cases ab', cases cd with cd' cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end, measurable_inv_fun := begin cases ab with ab' _ abm, cases ab', cases cd with cd' _ cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end } def set.prod [measurable_space Ξ±] [measurable_space Ξ²] (s : set Ξ±) (t : set Ξ²) : measurable_equiv (set.prod s t) (s Γ— t) := { to_equiv := equiv.set.prod s t, measurable_to_fun := measurable.prod_mk (measurable.subtype_mk $ measurable.fst $ measurable.subtype_val $ measurable_id) (measurable.subtype_mk $ measurable.snd $ measurable.subtype_val $ measurable_id), measurable_inv_fun := measurable.subtype_mk $ measurable.prod_mk (measurable.subtype_val $ measurable.fst $ measurable_id) (measurable.subtype_val $ measurable.snd $ measurable_id) } def set.univ (Ξ± : Type*) [measurable_space Ξ±] : measurable_equiv (univ : set Ξ±) Ξ± := { to_equiv := equiv.set.univ Ξ±, measurable_to_fun := measurable.subtype_val measurable_id, measurable_inv_fun := measurable.subtype_mk measurable_id } def set.singleton [measurable_space Ξ±] (a:Ξ±) : measurable_equiv ({a} : set Ξ±) unit := { to_equiv := equiv.set.singleton a, measurable_to_fun := measurable_const, measurable_inv_fun := measurable.subtype_mk $ show measurable (Ξ»u:unit, a), from measurable_const } noncomputable def set.image [measurable_space Ξ±] [measurable_space Ξ²] (f : Ξ± β†’ Ξ²) (s : set Ξ±) (hf : function.injective f) (hfm : measurable f) (hfi : βˆ€s, is_measurable s β†’ is_measurable (f '' s)) : measurable_equiv s (f '' s) := { to_equiv := equiv.set.image f s hf, measurable_to_fun := begin have : measurable (Ξ»a:s, f a) := hfm.comp (measurable.subtype_val measurable_id), refine measurable.subtype_mk _, convert this, ext ⟨a, h⟩, refl end, measurable_inv_fun := assume t ⟨u, (hu : is_measurable u), eq⟩, begin clear_, subst eq, show is_measurable {x : f '' s | ((equiv.set.image f s hf).inv_fun x).val ∈ u}, have : βˆ€(a ∈ s) (h : βˆƒa', a' ∈ s ∧ a' = a), classical.some h = a := Ξ»a ha h, (classical.some_spec h).2, rw show {x:f '' s | ((equiv.set.image f s hf).inv_fun x).val ∈ u} = subtype.val ⁻¹' (f '' u), by ext ⟨b, a, hbs, rfl⟩; simp [equiv.set.image, equiv.set.image_of_inj_on, hf, this _ hbs], exact (measurable.subtype_val measurable_id) (f '' u) (hfi u hu) end } noncomputable def set.range [measurable_space Ξ±] [measurable_space Ξ²] (f : Ξ± β†’ Ξ²) (hf : function.injective f) (hfm : measurable f) (hfi : βˆ€s, is_measurable s β†’ is_measurable (f '' s)) : measurable_equiv Ξ± (range f) := (measurable_equiv.set.univ _).symm.trans $ (measurable_equiv.set.image f univ hf hfm hfi).trans $ measurable_equiv.cast (by rw image_univ) (by rw image_univ) def set.range_inl [measurable_space Ξ±] [measurable_space Ξ²] : measurable_equiv (range sum.inl : set (Ξ± βŠ• Ξ²)) Ξ± := { to_fun := Ξ»ab, match ab with | ⟨sum.inl a, _⟩ := a | ⟨sum.inr b, p⟩ := have false, by cases p; contradiction, this.elim end, inv_fun := Ξ»a, ⟨sum.inl a, a, rfl⟩, left_inv := assume ⟨ab, a, eq⟩, by subst eq; refl, right_inv := assume a, rfl, measurable_to_fun := assume s (hs : is_measurable s), begin refine ⟨_, is_measurable_inl_image hs, set.ext _⟩, rintros ⟨ab, a, rfl⟩, simp [set.range_inl._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inl } def set.range_inr [measurable_space Ξ±] [measurable_space Ξ²] : measurable_equiv (range sum.inr : set (Ξ± βŠ• Ξ²)) Ξ² := { to_fun := Ξ»ab, match ab with | ⟨sum.inr b, _⟩ := b | ⟨sum.inl a, p⟩ := have false, by cases p; contradiction, this.elim end, inv_fun := Ξ»b, ⟨sum.inr b, b, rfl⟩, left_inv := assume ⟨ab, b, eq⟩, by subst eq; refl, right_inv := assume b, rfl, measurable_to_fun := assume s (hs : is_measurable s), begin refine ⟨_, is_measurable_inr_image hs, set.ext _⟩, rintros ⟨ab, b, rfl⟩, simp [set.range_inr._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inr } def sum_prod_distrib (Ξ± Ξ² Ξ³) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] : measurable_equiv ((Ξ± βŠ• Ξ²) Γ— Ξ³) ((Ξ± Γ— Ξ³) βŠ• (Ξ² Γ— Ξ³)) := { to_equiv := equiv.sum_prod_distrib Ξ± Ξ² Ξ³, measurable_to_fun := begin refine measurable_of_measurable_union_cover ((range sum.inl).prod univ) ((range sum.inr).prod univ) (is_measurable_set_prod is_measurable_range_inl is_measurable.univ) (is_measurable_set_prod is_measurable_range_inr is_measurable.univ) (assume ⟨ab, c⟩ s, by cases ab; simp [set.prod_eq]) _ _, { refine (set.prod (range sum.inl) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inl (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inl, ext ⟨a, c⟩, refl }, { refine (set.prod (range sum.inr) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inr (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inr, ext ⟨b, c⟩, refl } end, measurable_inv_fun := begin refine measurable_sum _ _, { convert measurable.prod_mk (measurable_inl.comp (measurable.fst measurable_id)) (measurable.snd measurable_id), ext ⟨a, c⟩; refl }, { convert measurable.prod_mk (measurable_inr.comp (measurable.fst measurable_id)) (measurable.snd measurable_id), ext ⟨b, c⟩; refl } end } def prod_sum_distrib (Ξ± Ξ² Ξ³) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] : measurable_equiv (Ξ± Γ— (Ξ² βŠ• Ξ³)) ((Ξ± Γ— Ξ²) βŠ• (Ξ± Γ— Ξ³)) := prod_comm.trans $ (sum_prod_distrib _ _ _).trans $ sum_congr prod_comm prod_comm def sum_prod_sum (Ξ± Ξ² Ξ³ Ξ΄) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] [measurable_space Ξ΄] : measurable_equiv ((Ξ± βŠ• Ξ²) Γ— (Ξ³ βŠ• Ξ΄)) (((Ξ± Γ— Ξ³) βŠ• (Ξ± Γ— Ξ΄)) βŠ• ((Ξ² Γ— Ξ³) βŠ• (Ξ² Γ— Ξ΄))) := (sum_prod_distrib _ _ _).trans $ sum_congr (prod_sum_distrib _ _ _) (prod_sum_distrib _ _ _) end measurable_equiv namespace measurable_equiv end measurable_equiv namespace measurable_space /-- Dynkin systems The main purpose of Dynkin systems is to provide a powerful induction rule for Οƒ-algebras generated by intersection stable set systems. -/ structure dynkin_system (Ξ± : Type*) := (has : set Ξ± β†’ Prop) (has_empty : has βˆ…) (has_compl : βˆ€{a}, has a β†’ has (-a)) (has_Union_nat : βˆ€{f:β„• β†’ set Ξ±}, pairwise (disjoint on f) β†’ (βˆ€i, has (f i)) β†’ has (⋃i, f i)) theorem Union_decode2_disjoint_on {Ξ²} [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hd : pairwise (disjoint on f)) : pairwise (disjoint on Ξ» i, ⋃ b ∈ decode2 Ξ² i, f b) := begin rintro i j ij x ⟨h₁, hβ‚‚βŸ©, revert h₁ hβ‚‚, simp, intros b₁ e₁ h₁ bβ‚‚ eβ‚‚ hβ‚‚, refine hd _ _ _ ⟨h₁, hβ‚‚βŸ©, cases encodable.mem_decode2.1 e₁, cases encodable.mem_decode2.1 eβ‚‚, exact mt (congr_arg _) ij end namespace dynkin_system @[ext] lemma ext : βˆ€{d₁ dβ‚‚ : dynkin_system Ξ±}, (βˆ€s:set Ξ±, d₁.has s ↔ dβ‚‚.has s) β†’ d₁ = dβ‚‚ | ⟨s₁, _, _, _⟩ ⟨sβ‚‚, _, _, _⟩ h := have s₁ = sβ‚‚, from funext $ assume x, propext $ h x, by subst this variable (d : dynkin_system Ξ±) lemma has_compl_iff {a} : d.has (-a) ↔ d.has a := ⟨λ h, by simpa using d.has_compl h, Ξ» h, d.has_compl h⟩ lemma has_univ : d.has univ := by simpa using d.has_compl d.has_empty theorem has_Union {Ξ²} [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hd : pairwise (disjoint on f)) (h : βˆ€i, d.has (f i)) : d.has (⋃i, f i) := by rw encodable.Union_decode2; exact d.has_Union_nat (Union_decode2_disjoint_on hd) (Ξ» n, encodable.Union_decode2_cases d.has_empty h) theorem has_union {s₁ sβ‚‚ : set Ξ±} (h₁ : d.has s₁) (hβ‚‚ : d.has sβ‚‚) (h : s₁ ∩ sβ‚‚ βŠ† βˆ…) : d.has (s₁ βˆͺ sβ‚‚) := by rw union_eq_Union; exact d.has_Union (pairwise_disjoint_on_bool.2 h) (bool.forall_bool.2 ⟨hβ‚‚, hβ‚βŸ©) lemma has_diff {s₁ sβ‚‚ : set Ξ±} (h₁ : d.has s₁) (hβ‚‚ : d.has sβ‚‚) (h : sβ‚‚ βŠ† s₁) : d.has (s₁ \ sβ‚‚) := d.has_compl_iff.1 begin simp [diff_eq, compl_inter], exact d.has_union (d.has_compl h₁) hβ‚‚ (Ξ» x ⟨h₁, hβ‚‚βŸ©, h₁ (h hβ‚‚)), end instance : partial_order (dynkin_system Ξ±) := { le := Ξ»m₁ mβ‚‚, m₁.has ≀ mβ‚‚.has, le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ hβ‚‚, ext $ assume s, ⟨h₁ s, hβ‚‚ s⟩ } def of_measurable_space (m : measurable_space Ξ±) : dynkin_system Ξ± := { has := m.is_measurable, has_empty := m.is_measurable_empty, has_compl := m.is_measurable_compl, has_Union_nat := assume f _ hf, m.is_measurable_Union f hf } lemma of_measurable_space_le_of_measurable_space_iff {m₁ mβ‚‚ : measurable_space Ξ±} : of_measurable_space m₁ ≀ of_measurable_space mβ‚‚ ↔ m₁ ≀ mβ‚‚ := iff.rfl /-- The least Dynkin system containing a collection of basic sets. -/ inductive generate_has (s : set (set Ξ±)) : set Ξ± β†’ Prop | basic : βˆ€t∈s, generate_has t | empty : generate_has βˆ… | compl : βˆ€{a}, generate_has a β†’ generate_has (-a) | Union : βˆ€{f:β„• β†’ set Ξ±}, pairwise (disjoint on f) β†’ (βˆ€i, generate_has (f i)) β†’ generate_has (⋃i, f i) def generate (s : set (set Ξ±)) : dynkin_system Ξ± := { has := generate_has s, has_empty := generate_has.empty s, has_compl := assume a, generate_has.compl, has_Union_nat := assume f, generate_has.Union } def to_measurable_space (h_inter : βˆ€s₁ sβ‚‚, d.has s₁ β†’ d.has sβ‚‚ β†’ d.has (s₁ ∩ sβ‚‚)) := { measurable_space . is_measurable := d.has, is_measurable_empty := d.has_empty, is_measurable_compl := assume s h, d.has_compl h, is_measurable_Union := assume f hf, have βˆ€n, d.has (disjointed f n), from assume n, disjointed_induct (hf n) (assume t i h, h_inter _ _ h $ d.has_compl $ hf i), have d.has (⋃n, disjointed f n), from d.has_Union disjoint_disjointed this, by rwa [Union_disjointed] at this } lemma of_measurable_space_to_measurable_space (h_inter : βˆ€s₁ sβ‚‚, d.has s₁ β†’ d.has sβ‚‚ β†’ d.has (s₁ ∩ sβ‚‚)) : of_measurable_space (d.to_measurable_space h_inter) = d := ext $ assume s, iff.rfl def restrict_on {s : set Ξ±} (h : d.has s) : dynkin_system Ξ± := { has := Ξ»t, d.has (t ∩ s), has_empty := by simp [d.has_empty], has_compl := assume t hts, have -t ∩ s = (- (t ∩ s)) \ -s, from set.ext $ assume x, by by_cases x ∈ s; simp [h], by rw [this]; from d.has_diff (d.has_compl hts) (d.has_compl h) (compl_subset_compl.mpr $ inter_subset_right _ _), has_Union_nat := assume f hd hf, begin rw [inter_comm, inter_Union], apply d.has_Union_nat, { exact Ξ» i j h x ⟨⟨_, hβ‚βŸ©, _, hβ‚‚βŸ©, hd i j h ⟨h₁, hβ‚‚βŸ© }, { simpa [inter_comm] using hf }, end } lemma generate_le {s : set (set Ξ±)} (h : βˆ€t∈s, d.has t) : generate s ≀ d := Ξ» t ht, ht.rec_on h d.has_empty (assume a _ h, d.has_compl h) (assume f hd _ hf, d.has_Union hd hf) lemma generate_inter {s : set (set Ξ±)} (hs : βˆ€t₁ tβ‚‚, t₁ ∈ s β†’ tβ‚‚ ∈ s β†’ t₁ ∩ tβ‚‚ β‰  βˆ… β†’ t₁ ∩ tβ‚‚ ∈ s) {t₁ tβ‚‚ : set Ξ±} (ht₁ : (generate s).has t₁) (htβ‚‚ : (generate s).has tβ‚‚) : (generate s).has (t₁ ∩ tβ‚‚) := have generate s ≀ (generate s).restrict_on htβ‚‚, from generate_le _ $ assume s₁ hs₁, have (generate s).has s₁, from generate_has.basic s₁ hs₁, have generate s ≀ (generate s).restrict_on this, from generate_le _ $ assume sβ‚‚ hsβ‚‚, show (generate s).has (sβ‚‚ ∩ s₁), from if h : sβ‚‚ ∩ s₁ = βˆ… then by rw [h]; exact generate_has.empty _ else generate_has.basic _ (hs _ _ hsβ‚‚ hs₁ h), have (generate s).has (tβ‚‚ ∩ s₁), from this _ htβ‚‚, show (generate s).has (s₁ ∩ tβ‚‚), by rwa [inter_comm], this _ ht₁ lemma generate_from_eq {s : set (set Ξ±)} (hs : βˆ€t₁ tβ‚‚, t₁ ∈ s β†’ tβ‚‚ ∈ s β†’ t₁ ∩ tβ‚‚ β‰  βˆ… β†’ t₁ ∩ tβ‚‚ ∈ s) : generate_from s = (generate s).to_measurable_space (assume t₁ tβ‚‚, generate_inter hs) := le_antisymm (generate_from_le $ assume t ht, generate_has.basic t ht) (of_measurable_space_le_of_measurable_space_iff.mp $ by rw [of_measurable_space_to_measurable_space]; from (generate_le _ $ assume t ht, is_measurable_generate_from ht)) end dynkin_system lemma induction_on_inter {C : set Ξ± β†’ Prop} {s : set (set Ξ±)} {m : measurable_space Ξ±} (h_eq : m = generate_from s) (h_inter : βˆ€t₁ tβ‚‚, t₁ ∈ s β†’ tβ‚‚ ∈ s β†’ t₁ ∩ tβ‚‚ β‰  βˆ… β†’ t₁ ∩ tβ‚‚ ∈ s) (h_empty : C βˆ…) (h_basic : βˆ€t∈s, C t) (h_compl : βˆ€t, m.is_measurable t β†’ C t β†’ C (- t)) (h_union : βˆ€f:β„• β†’ set Ξ±, (βˆ€i j, i β‰  j β†’ f i ∩ f j βŠ† βˆ…) β†’ (βˆ€i, m.is_measurable (f i)) β†’ (βˆ€i, C (f i)) β†’ C (⋃i, f i)) : βˆ€{t}, m.is_measurable t β†’ C t := have eq : m.is_measurable = dynkin_system.generate_has s, by rw [h_eq, dynkin_system.generate_from_eq h_inter]; refl, assume t ht, have dynkin_system.generate_has s t, by rwa [eq] at ht, this.rec_on h_basic h_empty (assume t ht, h_compl t $ by rw [eq]; exact ht) (assume f hf ht, h_union f hf $ assume i, by rw [eq]; exact ht _) end measurable_space
62a111804a90578f48a6982a1fef5cd9c641f0c1
abd85493667895c57a7507870867b28124b3998f
/src/topology/uniform_space/completion.lean
444681f8c08751db06ca959854300ac5a41c2ce3
[ "Apache-2.0" ]
permissive
pechersky/mathlib
d56eef16bddb0bfc8bc552b05b7270aff5944393
f1df14c2214ee114c9738e733efd5de174deb95d
refs/heads/master
1,666,714,392,571
1,591,747,567,000
1,591,747,567,000
270,557,274
0
0
Apache-2.0
1,591,597,975,000
1,591,597,974,000
null
UTF-8
Lean
false
false
23,194
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes HΓΆlzl Hausdorff completions of uniform spaces. The goal is to construct a left-adjoint to the inclusion of complete Hausdorff uniform spaces into all uniform spaces. Any uniform space `Ξ±` gets a completion `completion Ξ±` and a morphism (ie. uniformly continuous map) `completion : Ξ± β†’ completion Ξ±` which solves the universal mapping problem of factorizing morphisms from `Ξ±` to any complete Hausdorff uniform space `Ξ²`. It means any uniformly continuous `f : Ξ± β†’ Ξ²` gives rise to a unique morphism `completion.extension f : completion Ξ± β†’ Ξ²` such that `f = completion.extension f ∘ completion Ξ±`. Actually `completion.extension f` is defined for all maps from `Ξ±` to `Ξ²` but it has the desired properties only if `f` is uniformly continuous. Beware that `completion Ξ±` is not injective if `Ξ±` is not Hausdorff. But its image is always dense. The adjoint functor acting on morphisms is then constructed by the usual abstract nonsense. For every uniform spaces `Ξ±` and `Ξ²`, it turns `f : Ξ± β†’ Ξ²` into a morphism `completion.map f : completion Ξ± β†’ completion Ξ²` such that `coe ∘ f = (completion.map f) ∘ coe` provided `f` is uniformly continuous. This construction is compatible with composition. In this file we introduce the following concepts: * `Cauchy Ξ±` the uniform completion of the uniform space `Ξ±` (using Cauchy filters). These are not minimal filters. * `completion Ξ± := quotient (separation_setoid (Cauchy Ξ±))` the Hausdorff completion. This formalization is mostly based on N. Bourbaki: General Topology I. M. James: Topologies and Uniformities From a slightly different perspective in order to reuse material in topology.uniform_space.basic. -/ import topology.uniform_space.abstract_completion noncomputable theory open filter set universes u v w x open_locale uniformity classical topological_space /-- Space of Cauchy filters This is essentially the completion of a uniform space. The embeddings are the neighbourhood filters. This space is not minimal, the separated uniform space (i.e. quotiented on the intersection of all entourages) is necessary for this. -/ def Cauchy (Ξ± : Type u) [uniform_space Ξ±] : Type u := { f : filter Ξ± // cauchy f } namespace Cauchy section parameters {Ξ± : Type u} [uniform_space Ξ±] variables {Ξ² : Type v} {Ξ³ : Type w} variables [uniform_space Ξ²] [uniform_space Ξ³] def gen (s : set (Ξ± Γ— Ξ±)) : set (Cauchy Ξ± Γ— Cauchy Ξ±) := {p | s ∈ filter.prod (p.1.val) (p.2.val) } lemma monotone_gen : monotone gen := monotone_set_of $ assume p, @monotone_mem_sets (Ξ±Γ—Ξ±) (filter.prod (p.1.val) (p.2.val)) private lemma symm_gen : map prod.swap ((𝓀 Ξ±).lift' gen) ≀ (𝓀 Ξ±).lift' gen := calc map prod.swap ((𝓀 Ξ±).lift' gen) = (𝓀 Ξ±).lift' (Ξ»s:set (Ξ±Γ—Ξ±), {p | s ∈ filter.prod (p.2.val) (p.1.val) }) : begin delta gen, simp [map_lift'_eq, monotone_set_of, monotone_mem_sets, function.comp, image_swap_eq_preimage_swap] end ... ≀ (𝓀 Ξ±).lift' gen : uniformity_lift_le_swap (monotone_principal.comp (monotone_set_of $ assume p, @monotone_mem_sets (Ξ±Γ—Ξ±) ((filter.prod ((p.2).val) ((p.1).val))))) begin have h := Ξ»(p:Cauchy Ξ±Γ—Cauchy Ξ±), @filter.prod_comm _ _ (p.2.val) (p.1.val), simp [function.comp, h], exact le_refl _ end private lemma comp_rel_gen_gen_subset_gen_comp_rel {s t : set (Ξ±Γ—Ξ±)} : comp_rel (gen s) (gen t) βŠ† (gen (comp_rel s t) : set (Cauchy Ξ± Γ— Cauchy Ξ±)) := assume ⟨f, g⟩ ⟨h, h₁, hβ‚‚βŸ©, let ⟨t₁, (ht₁ : t₁ ∈ f.val), tβ‚‚, (htβ‚‚ : tβ‚‚ ∈ h.val), (h₁ : set.prod t₁ tβ‚‚ βŠ† s)⟩ := mem_prod_iff.mp h₁ in let ⟨t₃, (ht₃ : t₃ ∈ h.val), tβ‚„, (htβ‚„ : tβ‚„ ∈ g.val), (hβ‚‚ : set.prod t₃ tβ‚„ βŠ† t)⟩ := mem_prod_iff.mp hβ‚‚ in have tβ‚‚ ∩ t₃ ∈ h.val, from inter_mem_sets htβ‚‚ ht₃, let ⟨x, xtβ‚‚, xtβ‚ƒβŸ© := nonempty_of_mem_sets (h.property.left) this in (filter.prod f.val g.val).sets_of_superset (prod_mem_prod ht₁ htβ‚„) (assume ⟨a, b⟩ ⟨(ha : a ∈ t₁), (hb : b ∈ tβ‚„)⟩, ⟨x, h₁ (show (a, x) ∈ set.prod t₁ tβ‚‚, from ⟨ha, xtβ‚‚βŸ©), hβ‚‚ (show (x, b) ∈ set.prod t₃ tβ‚„, from ⟨xt₃, hb⟩)⟩) private lemma comp_gen : ((𝓀 Ξ±).lift' gen).lift' (Ξ»s, comp_rel s s) ≀ (𝓀 Ξ±).lift' gen := calc ((𝓀 Ξ±).lift' gen).lift' (Ξ»s, comp_rel s s) = (𝓀 Ξ±).lift' (Ξ»s, comp_rel (gen s) (gen s)) : begin rw [lift'_lift'_assoc], exact monotone_gen, exact (monotone_comp_rel monotone_id monotone_id) end ... ≀ (𝓀 Ξ±).lift' (Ξ»s, gen $ comp_rel s s) : lift'_mono' $ assume s hs, comp_rel_gen_gen_subset_gen_comp_rel ... = ((𝓀 Ξ±).lift' $ Ξ»s:set(Ξ±Γ—Ξ±), comp_rel s s).lift' gen : begin rw [lift'_lift'_assoc], exact (monotone_comp_rel monotone_id monotone_id), exact monotone_gen end ... ≀ (𝓀 Ξ±).lift' gen : lift'_mono comp_le_uniformity (le_refl _) instance : uniform_space (Cauchy Ξ±) := uniform_space.of_core { uniformity := (𝓀 Ξ±).lift' gen, refl := principal_le_lift' $ assume s hs ⟨a, b⟩ (a_eq_b : a = b), a_eq_b β–Έ a.property.right hs, symm := symm_gen, comp := comp_gen } theorem mem_uniformity {s : set (Cauchy Ξ± Γ— Cauchy Ξ±)} : s ∈ 𝓀 (Cauchy Ξ±) ↔ βˆƒ t ∈ 𝓀 Ξ±, gen t βŠ† s := mem_lift'_sets monotone_gen theorem mem_uniformity' {s : set (Cauchy Ξ± Γ— Cauchy Ξ±)} : s ∈ 𝓀 (Cauchy Ξ±) ↔ βˆƒ t ∈ 𝓀 Ξ±, βˆ€ f g : Cauchy Ξ±, t ∈ filter.prod f.1 g.1 β†’ (f, g) ∈ s := mem_uniformity.trans $ bex_congr $ Ξ» t h, prod.forall /-- Embedding of `Ξ±` into its completion -/ def pure_cauchy (a : Ξ±) : Cauchy Ξ± := ⟨pure a, cauchy_pure⟩ lemma uniform_inducing_pure_cauchy : uniform_inducing (pure_cauchy : Ξ± β†’ Cauchy Ξ±) := ⟨have (preimage (Ξ» (x : Ξ± Γ— Ξ±), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) = id, from funext $ assume s, set.ext $ assume ⟨a₁, aβ‚‚βŸ©, by simp [preimage, gen, pure_cauchy, prod_principal_principal], calc comap (Ξ» (x : Ξ± Γ— Ξ±), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ((𝓀 Ξ±).lift' gen) = (𝓀 Ξ±).lift' (preimage (Ξ» (x : Ξ± Γ— Ξ±), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) : comap_lift'_eq monotone_gen ... = 𝓀 Ξ± : by simp [this]⟩ lemma uniform_embedding_pure_cauchy : uniform_embedding (pure_cauchy : Ξ± β†’ Cauchy Ξ±) := { inj := assume a₁ aβ‚‚ h, pure_inj $ subtype.ext.1 h, ..uniform_inducing_pure_cauchy } lemma pure_cauchy_dense : βˆ€x, x ∈ closure (range pure_cauchy) := assume f, have h_ex : βˆ€ s ∈ 𝓀 (Cauchy Ξ±), βˆƒy:Ξ±, (f, pure_cauchy y) ∈ s, from assume s hs, let ⟨t'', ht''₁, (ht''β‚‚ : gen t'' βŠ† s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht'₁, ht'β‚‚βŸ© := comp_mem_uniformity_sets ht''₁ in have t' ∈ filter.prod (f.val) (f.val), from f.property.right ht'₁, let ⟨t, ht, (h : set.prod t t βŠ† t')⟩ := mem_prod_same_iff.mp this in let ⟨x, (hx : x ∈ t)⟩ := nonempty_of_mem_sets f.property.left ht in have t'' ∈ filter.prod f.val (pure x), from mem_prod_iff.mpr ⟨t, ht, {y:Ξ± | (x, y) ∈ t'}, h $ mk_mem_prod hx hx, assume ⟨a, b⟩ ⟨(h₁ : a ∈ t), (hβ‚‚ : (x, b) ∈ t')⟩, ht'β‚‚ $ prod_mk_mem_comp_rel (@h (a, x) ⟨h₁, hx⟩) hβ‚‚βŸ©, ⟨x, ht''β‚‚ $ by dsimp [gen]; exact this⟩, begin simp [closure_eq_nhds, nhds_eq_uniformity, lift'_inf_principal_eq, set.inter_comm], exact (lift'_ne_bot_iff $ monotone_inter monotone_const monotone_preimage).mpr (assume s hs, let ⟨y, hy⟩ := h_ex s hs in have pure_cauchy y ∈ range pure_cauchy ∩ {y : Cauchy Ξ± | (f, y) ∈ s}, from ⟨mem_range_self y, hy⟩, ⟨_, this⟩) end lemma dense_inducing_pure_cauchy : dense_inducing pure_cauchy := uniform_inducing_pure_cauchy.dense_inducing pure_cauchy_dense lemma dense_embedding_pure_cauchy : dense_embedding pure_cauchy := uniform_embedding_pure_cauchy.dense_embedding pure_cauchy_dense lemma nonempty_Cauchy_iff : nonempty (Cauchy Ξ±) ↔ nonempty Ξ± := begin split ; rintro ⟨c⟩, { have := eq_univ_iff_forall.1 dense_embedding_pure_cauchy.to_dense_inducing.closure_range c, obtain ⟨_, ⟨_, a, _⟩⟩ := mem_closure_iff.1 this _ is_open_univ trivial, exact ⟨a⟩ }, { exact ⟨pure_cauchy c⟩ } end section set_option eqn_compiler.zeta true instance : complete_space (Cauchy Ξ±) := complete_space_extension uniform_inducing_pure_cauchy pure_cauchy_dense $ assume f hf, let f' : Cauchy Ξ± := ⟨f, hf⟩ in have map pure_cauchy f ≀ (𝓀 $ Cauchy Ξ±).lift' (preimage (prod.mk f')), from le_lift' $ assume s hs, let ⟨t, ht₁, (htβ‚‚ : gen t βŠ† s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht', (h : set.prod t' t' βŠ† t)⟩ := mem_prod_same_iff.mp (hf.right ht₁) in have t' βŠ† { y : Ξ± | (f', pure_cauchy y) ∈ gen t }, from assume x hx, (filter.prod f (pure x)).sets_of_superset (prod_mem_prod ht' hx) h, f.sets_of_superset ht' $ subset.trans this (preimage_mono htβ‚‚), ⟨f', by simp [nhds_eq_uniformity]; assumption⟩ end instance [inhabited Ξ±] : inhabited (Cauchy Ξ±) := ⟨pure_cauchy $ default α⟩ instance [h : nonempty Ξ±] : nonempty (Cauchy Ξ±) := h.rec_on $ assume a, nonempty.intro $ Cauchy.pure_cauchy a section extend def extend (f : Ξ± β†’ Ξ²) : (Cauchy Ξ± β†’ Ξ²) := if uniform_continuous f then dense_inducing_pure_cauchy.extend f else Ξ» x, f (classical.inhabited_of_nonempty $ nonempty_Cauchy_iff.1 ⟨x⟩).default variables [separated Ξ²] lemma extend_pure_cauchy {f : Ξ± β†’ Ξ²} (hf : uniform_continuous f) (a : Ξ±) : extend f (pure_cauchy a) = f a := begin rw [extend, if_pos hf], exact uniformly_extend_of_ind uniform_inducing_pure_cauchy pure_cauchy_dense hf _ end variables [_root_.complete_space Ξ²] lemma uniform_continuous_extend {f : Ξ± β†’ Ξ²} : uniform_continuous (extend f) := begin by_cases hf : uniform_continuous f, { rw [extend, if_pos hf], exact uniform_continuous_uniformly_extend uniform_inducing_pure_cauchy pure_cauchy_dense hf }, { rw [extend, if_neg hf], exact uniform_continuous_of_const (assume a b, by congr) } end end extend end theorem Cauchy_eq {Ξ± : Type*} [inhabited Ξ±] [uniform_space Ξ±] [complete_space Ξ±] [separated Ξ±] {f g : Cauchy Ξ±} : Lim f.1 = Lim g.1 ↔ (f, g) ∈ separation_rel (Cauchy Ξ±) := begin split, { intros e s hs, rcases Cauchy.mem_uniformity'.1 hs with ⟨t, tu, ts⟩, apply ts, rcases comp_mem_uniformity_sets tu with ⟨d, du, dt⟩, refine mem_prod_iff.2 ⟨_, f.2.le_nhds_Lim (mem_nhds_right (Lim f.1) du), _, g.2.le_nhds_Lim (mem_nhds_left (Lim g.1) du), Ξ» x h, _⟩, cases x with a b, cases h with h₁ hβ‚‚, rw ← e at hβ‚‚, exact dt ⟨_, h₁, hβ‚‚βŸ© }, { intros H, refine separated_def.1 (by apply_instance) _ _ (Ξ» t tu, _), rcases mem_uniformity_is_closed tu with ⟨d, du, dc, dt⟩, refine H {p | (Lim p.1.1, Lim p.2.1) ∈ t} (Cauchy.mem_uniformity'.2 ⟨d, du, Ξ» f g h, _⟩), rcases mem_prod_iff.1 h with ⟨x, xf, y, yg, h⟩, have limc : βˆ€ (f : Cauchy Ξ±) (x ∈ f.1), Lim f.1 ∈ closure x, { intros f x xf, rw closure_eq_nhds, exact ne_bot_of_le_ne_bot f.2.1 (le_inf f.2.le_nhds_Lim (le_principal_iff.2 xf)) }, have := (closure_subset_iff_subset_of_is_closed dc).2 h, rw closure_prod_eq at this, refine dt (this ⟨_, _⟩); dsimp; apply limc; assumption } end section local attribute [instance] uniform_space.separation_setoid lemma injective_separated_pure_cauchy {Ξ± : Type*} [uniform_space Ξ±] [s : separated Ξ±] : function.injective (Ξ»a:Ξ±, ⟦pure_cauchy a⟧) | a b h := separated_def.1 s _ _ $ assume s hs, let ⟨t, ht, hts⟩ := by rw [← (@uniform_embedding_pure_cauchy Ξ± _).comap_uniformity, filter.mem_comap_sets] at hs; exact hs in have (pure_cauchy a, pure_cauchy b) ∈ t, from quotient.exact h t ht, @hts (a, b) this end end Cauchy local attribute [instance] uniform_space.separation_setoid open Cauchy set namespace uniform_space variables (Ξ± : Type*) [uniform_space Ξ±] variables {Ξ² : Type*} [uniform_space Ξ²] variables {Ξ³ : Type*} [uniform_space Ξ³] instance complete_space_separation [h : complete_space Ξ±] : complete_space (quotient (separation_setoid Ξ±)) := ⟨assume f, assume hf : cauchy f, have cauchy (f.comap (Ξ»x, ⟦x⟧)), from cauchy_comap comap_quotient_le_uniformity hf $ comap_ne_bot_of_surj hf.left $ assume b, quotient.exists_rep _, let ⟨x, (hx : f.comap (Ξ»x, ⟦x⟧) ≀ 𝓝 x)⟩ := complete_space.complete this in ⟨⟦x⟧, calc f = map (Ξ»x, ⟦x⟧) (f.comap (Ξ»x, ⟦x⟧)) : (map_comap $ univ_mem_sets' $ assume b, quotient.exists_rep _).symm ... ≀ map (Ξ»x, ⟦x⟧) (𝓝 x) : map_mono hx ... ≀ _ : continuous_iff_continuous_at.mp uniform_continuous_quotient_mk.continuous _⟩⟩ /-- Hausdorff completion of `Ξ±` -/ def completion := quotient (separation_setoid $ Cauchy Ξ±) namespace completion instance [inhabited Ξ±] : inhabited (completion Ξ±) := by unfold completion; apply_instance @[priority 50] instance : uniform_space (completion Ξ±) := by dunfold completion ; apply_instance instance : complete_space (completion Ξ±) := by dunfold completion ; apply_instance instance : separated (completion Ξ±) := by dunfold completion ; apply_instance instance : t2_space (completion Ξ±) := separated_t2 instance : regular_space (completion Ξ±) := separated_regular /-- Automatic coercion from `Ξ±` to its completion. Not always injective. -/ instance : has_coe_t Ξ± (completion Ξ±) := ⟨quotient.mk ∘ pure_cauchy⟩ -- note [use has_coe_t] protected lemma coe_eq : (coe : Ξ± β†’ completion Ξ±) = quotient.mk ∘ pure_cauchy := rfl lemma comap_coe_eq_uniformity : (𝓀 _).comap (Ξ»(p:Ξ±Γ—Ξ±), ((p.1 : completion Ξ±), (p.2 : completion Ξ±))) = 𝓀 Ξ± := begin have : (Ξ»x:Ξ±Γ—Ξ±, ((x.1 : completion Ξ±), (x.2 : completion Ξ±))) = (Ξ»x:(Cauchy Ξ±)Γ—(Cauchy Ξ±), (⟦x.1⟧, ⟦x.2⟧)) ∘ (Ξ»x:Ξ±Γ—Ξ±, (pure_cauchy x.1, pure_cauchy x.2)), { ext ⟨a, b⟩; simp; refl }, rw [this, ← filter.comap_comap_comp], change filter.comap _ (filter.comap _ (𝓀 $ quotient $ separation_setoid $ Cauchy Ξ±)) = 𝓀 Ξ±, rw [comap_quotient_eq_uniformity, uniform_embedding_pure_cauchy.comap_uniformity] end lemma uniform_inducing_coe : uniform_inducing (coe : Ξ± β†’ completion Ξ±) := ⟨comap_coe_eq_uniformity α⟩ variables {Ξ±} lemma dense : dense_range (coe : Ξ± β†’ completion Ξ±) := begin rw [dense_range_iff_closure_range, completion.coe_eq, range_comp], exact quotient_dense_of_dense pure_cauchy_dense end variables (Ξ±) def cpkg {Ξ± : Type*} [uniform_space Ξ±] : abstract_completion Ξ± := { space := completion Ξ±, coe := coe, uniform_struct := by apply_instance, complete := by apply_instance, separation := by apply_instance, uniform_inducing := completion.uniform_inducing_coe Ξ±, dense := completion.dense } instance abstract_completion.inhabited : inhabited (abstract_completion Ξ±) := ⟨cpkg⟩ local attribute [instance] abstract_completion.uniform_struct abstract_completion.complete abstract_completion.separation lemma nonempty_completion_iff : nonempty (completion Ξ±) ↔ nonempty Ξ± := (dense_range.nonempty (cpkg.dense)).symm lemma uniform_continuous_coe : uniform_continuous (coe : Ξ± β†’ completion Ξ±) := cpkg.uniform_continuous_coe lemma continuous_coe : continuous (coe : Ξ± β†’ completion Ξ±) := cpkg.continuous_coe lemma uniform_embedding_coe [separated Ξ±] : uniform_embedding (coe : Ξ± β†’ completion Ξ±) := { comap_uniformity := comap_coe_eq_uniformity Ξ±, inj := injective_separated_pure_cauchy } variable {Ξ±} lemma dense_inducing_coe : dense_inducing (coe : Ξ± β†’ completion Ξ±) := { dense := dense, ..(uniform_inducing_coe Ξ±).inducing } lemma dense_embedding_coe [separated Ξ±]: dense_embedding (coe : Ξ± β†’ completion Ξ±) := { inj := injective_separated_pure_cauchy, ..dense_inducing_coe } lemma denseβ‚‚ : dense_range (Ξ»x:Ξ± Γ— Ξ², ((x.1 : completion Ξ±), (x.2 : completion Ξ²))) := dense.prod dense lemma dense₃ : dense_range (Ξ»x:Ξ± Γ— (Ξ² Γ— Ξ³), ((x.1 : completion Ξ±), ((x.2.1 : completion Ξ²), (x.2.2 : completion Ξ³)))) := dense.prod denseβ‚‚ @[elab_as_eliminator] lemma induction_on {p : completion Ξ± β†’ Prop} (a : completion Ξ±) (hp : is_closed {a | p a}) (ih : βˆ€a:Ξ±, p a) : p a := is_closed_property dense hp ih a @[elab_as_eliminator] lemma induction_onβ‚‚ {p : completion Ξ± β†’ completion Ξ² β†’ Prop} (a : completion Ξ±) (b : completion Ξ²) (hp : is_closed {x : completion Ξ± Γ— completion Ξ² | p x.1 x.2}) (ih : βˆ€(a:Ξ±) (b:Ξ²), p a b) : p a b := have βˆ€x : completion Ξ± Γ— completion Ξ², p x.1 x.2, from is_closed_property denseβ‚‚ hp $ assume ⟨a, b⟩, ih a b, this (a, b) @[elab_as_eliminator] lemma induction_on₃ {p : completion Ξ± β†’ completion Ξ² β†’ completion Ξ³ β†’ Prop} (a : completion Ξ±) (b : completion Ξ²) (c : completion Ξ³) (hp : is_closed {x : completion Ξ± Γ— completion Ξ² Γ— completion Ξ³ | p x.1 x.2.1 x.2.2}) (ih : βˆ€(a:Ξ±) (b:Ξ²) (c:Ξ³), p a b c) : p a b c := have βˆ€x : completion Ξ± Γ— completion Ξ² Γ— completion Ξ³, p x.1 x.2.1 x.2.2, from is_closed_property dense₃ hp $ assume ⟨a, b, c⟩, ih a b c, this (a, b, c) lemma ext [t2_space Ξ²] {f g : completion Ξ± β†’ Ξ²} (hf : continuous f) (hg : continuous g) (h : βˆ€a:Ξ±, f a = g a) : f = g := cpkg.funext hf hg h section extension variables {f : Ξ± β†’ Ξ²} /-- "Extension" to the completion. It is defined for any map `f` but returns an arbitrary constant value if `f` is not uniformly continuous -/ protected def extension (f : Ξ± β†’ Ξ²) : completion Ξ± β†’ Ξ² := cpkg.extend f variables [separated Ξ²] @[simp] lemma extension_coe (hf : uniform_continuous f) (a : Ξ±) : (completion.extension f) a = f a := cpkg.extend_coe hf a variables [complete_space Ξ²] lemma uniform_continuous_extension : uniform_continuous (completion.extension f) := cpkg.uniform_continuous_extend lemma continuous_extension : continuous (completion.extension f) := cpkg.continuous_extend lemma extension_unique (hf : uniform_continuous f) {g : completion Ξ± β†’ Ξ²} (hg : uniform_continuous g) (h : βˆ€ a : Ξ±, f a = g (a : completion Ξ±)) : completion.extension f = g := cpkg.extend_unique hf hg h @[simp] lemma extension_comp_coe {f : completion Ξ± β†’ Ξ²} (hf : uniform_continuous f) : completion.extension (f ∘ coe) = f := cpkg.extend_comp_coe hf end extension section map variables {f : Ξ± β†’ Ξ²} /-- Completion functor acting on morphisms -/ protected def map (f : Ξ± β†’ Ξ²) : completion Ξ± β†’ completion Ξ² := cpkg.map cpkg f lemma uniform_continuous_map : uniform_continuous (completion.map f) := cpkg.uniform_continuous_map cpkg f lemma continuous_map : continuous (completion.map f) := cpkg.continuous_map cpkg f @[simp] lemma map_coe (hf : uniform_continuous f) (a : Ξ±) : (completion.map f) a = f a := cpkg.map_coe cpkg hf a lemma map_unique {f : Ξ± β†’ Ξ²} {g : completion Ξ± β†’ completion Ξ²} (hg : uniform_continuous g) (h : βˆ€a:Ξ±, ↑(f a) = g a) : completion.map f = g := cpkg.map_unique cpkg hg h @[simp] lemma map_id : completion.map (@id Ξ±) = id := cpkg.map_id lemma extension_map [complete_space Ξ³] [separated Ξ³] {f : Ξ² β†’ Ξ³} {g : Ξ± β†’ Ξ²} (hf : uniform_continuous f) (hg : uniform_continuous g) : completion.extension f ∘ completion.map g = completion.extension (f ∘ g) := completion.ext (continuous_extension.comp continuous_map) continuous_extension $ by intro a; simp only [hg, hf, hf.comp hg, (∘), map_coe, extension_coe] lemma map_comp {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} (hg : uniform_continuous g) (hf : uniform_continuous f) : completion.map g ∘ completion.map f = completion.map (g ∘ f) := extension_map ((uniform_continuous_coe _).comp hg) hf end map /- In this section we construct isomorphisms between the completion of a uniform space and the completion of its separation quotient -/ section separation_quotient_completion def completion_separation_quotient_equiv (Ξ± : Type u) [uniform_space Ξ±] : completion (separation_quotient Ξ±) ≃ completion Ξ± := begin refine ⟨completion.extension (separation_quotient.lift (coe : Ξ± β†’ completion Ξ±)), completion.map quotient.mk, _, _⟩, { assume a, refine induction_on a (is_closed_eq (continuous_map.comp continuous_extension) continuous_id) _, rintros ⟨a⟩, show completion.map quotient.mk (completion.extension (separation_quotient.lift coe) β†‘βŸ¦a⟧) = β†‘βŸ¦a⟧, rw [extension_coe (separation_quotient.uniform_continuous_lift _), separation_quotient.lift_mk (uniform_continuous_coe Ξ±), completion.map_coe uniform_continuous_quotient_mk] ; apply_instance }, { assume a, refine completion.induction_on a (is_closed_eq (continuous_extension.comp continuous_map) continuous_id) _, assume a, rw [map_coe uniform_continuous_quotient_mk, extension_coe (separation_quotient.uniform_continuous_lift _), separation_quotient.lift_mk (uniform_continuous_coe Ξ±) _] ; apply_instance } end lemma uniform_continuous_completion_separation_quotient_equiv : uniform_continuous ⇑(completion_separation_quotient_equiv Ξ±) := uniform_continuous_extension lemma uniform_continuous_completion_separation_quotient_equiv_symm : uniform_continuous ⇑(completion_separation_quotient_equiv Ξ±).symm := uniform_continuous_map end separation_quotient_completion section extensionβ‚‚ variables (f : Ξ± β†’ Ξ² β†’ Ξ³) open function protected def extensionβ‚‚ (f : Ξ± β†’ Ξ² β†’ Ξ³) : completion Ξ± β†’ completion Ξ² β†’ Ξ³ := cpkg.extendβ‚‚ cpkg f variables [separated Ξ³] {f} @[simp] lemma extensionβ‚‚_coe_coe (hf : uniform_continuousβ‚‚ f) (a : Ξ±) (b : Ξ²) : completion.extensionβ‚‚ f a b = f a b := cpkg.extensionβ‚‚_coe_coe cpkg hf a b variables [complete_space Ξ³] (f) lemma uniform_continuous_extensionβ‚‚ : uniform_continuousβ‚‚ (completion.extensionβ‚‚ f) := cpkg.uniform_continuous_extensionβ‚‚ cpkg f end extensionβ‚‚ section mapβ‚‚ open function protected def mapβ‚‚ (f : Ξ± β†’ Ξ² β†’ Ξ³) : completion Ξ± β†’ completion Ξ² β†’ completion Ξ³ := cpkg.mapβ‚‚ cpkg cpkg f lemma uniform_continuous_mapβ‚‚ (f : Ξ± β†’ Ξ² β†’ Ξ³) : uniform_continuousβ‚‚ (completion.mapβ‚‚ f) := cpkg.uniform_continuous_mapβ‚‚ cpkg cpkg f lemma continuous_mapβ‚‚ {Ξ΄} [topological_space Ξ΄] {f : Ξ± β†’ Ξ² β†’ Ξ³} {a : Ξ΄ β†’ completion Ξ±} {b : Ξ΄ β†’ completion Ξ²} (ha : continuous a) (hb : continuous b) : continuous (Ξ»d:Ξ΄, completion.mapβ‚‚ f (a d) (b d)) := cpkg.continuous_mapβ‚‚ cpkg cpkg ha hb lemma mapβ‚‚_coe_coe (a : Ξ±) (b : Ξ²) (f : Ξ± β†’ Ξ² β†’ Ξ³) (hf : uniform_continuousβ‚‚ f) : completion.mapβ‚‚ f (a : completion Ξ±) (b : completion Ξ²) = f a b := cpkg.mapβ‚‚_coe_coe cpkg cpkg a b f hf end mapβ‚‚ end completion end uniform_space
bca5c2ed73d9838a0f0dd7c6b99a49af3a067484
a0a027e4a00cdb315527e8922122f2a4411fd01c
/4.1-the-universal-quantifier.lean
83573b2f8dce889243d99d2c6a25bb9857103bd5
[]
no_license
spl/lean-tutorial
96a1ef321d06b9b28d044eeb6bf1ff9a86761a6e
35c0250004d75d8ae58f6192b649744545116022
refs/heads/master
1,610,250,012,890
1,454,259,122,000
1,454,259,122,000
49,971,362
1
0
null
null
null
null
UTF-8
Lean
false
false
2,561
lean
/------------------------------------------------------------------------------- - Section 4.1 The Universal Quantifier ------------------------------------------------------------------------------/ variables (A : Type) (p q : A β†’ Prop) example : (βˆ€ x, p x ∧ q x) ↔ (βˆ€ x, p x) ∧ (βˆ€ x, q x) := have fwd : (βˆ€ x, p x ∧ q x) β†’ (βˆ€ x, p x) ∧ (βˆ€ x, q x), from assume H : βˆ€ x, p x ∧ q x, show (βˆ€ x, p x) ∧ (βˆ€ x, q x), from and.intro (take x, and.left (H x)) (take x, and.right (H x)), have bwd : (βˆ€ x, p x) ∧ (βˆ€ x, q x) β†’ (βˆ€ x, p x ∧ q x), from assume H : (βˆ€ x, p x) ∧ (βˆ€ x, q x), show βˆ€ x, p x ∧ q x, from take x, and.intro (and.left H x) (and.right H x), iff.intro fwd bwd example : (βˆ€ x, p x β†’ q x) β†’ (βˆ€ x, p x) β†’ (βˆ€ x, q x) := assume (H₁ : βˆ€ x, p x β†’ q x) (Hβ‚‚ : βˆ€ x, p x), take x : A, show q x, from H₁ x (Hβ‚‚ x) example : (βˆ€ x, p x) ∨ (βˆ€ x, q x) β†’ βˆ€ x, p x ∨ q x := assume H : (βˆ€ x, p x) ∨ (βˆ€ x, q x), take x : A, or.elim H (assume Hp : βˆ€ x, p x, or.inl (Hp x)) (assume Hq : βˆ€ x, q x, or.inr (Hq x)) variable r : Prop example : A β†’ ((βˆ€ x : A, r) ↔ r) := assume x : A, iff.intro (assume f : A β†’ r, f x) (assume (r : r) (x : A), r) example : (βˆ€ x, p x ∨ r) ↔ (βˆ€ x, p x) ∨ r := have fwd : (βˆ€ x, p x ∨ r) β†’ (βˆ€ x, p x) ∨ r, from assume H : βˆ€ x, p x ∨ r, -- TODO. This requires classical reasoning. sorry, have bwd : (βˆ€ x, p x) ∨ r β†’ (βˆ€ x, p x ∨ r), from assume H : (βˆ€ x, p x) ∨ r, take x : A, or.elim H (assume Hp : βˆ€ x, p x, or.inl (Hp x)) or.inr, iff.intro fwd bwd example : (βˆ€ x, r β†’ p x) ↔ (r β†’ βˆ€ x, p x) := have fwd : (βˆ€ x, r β†’ p x) β†’ (r β†’ βˆ€ x, p x), from assume (H : βˆ€ x, r β†’ p x) r, take x : A, H x r, have bwd : (r β†’ βˆ€ x, p x) β†’ (βˆ€ x, r β†’ p x), from assume H : r β†’ βˆ€ x, p x, take x : A, assume r, H r x, iff.intro fwd bwd variables (men : Type) (barber : men) (shaves : men β†’ men β†’ Prop) example (H : βˆ€ x : men, shaves barber x ↔ Β¬shaves x x) : false := have H' : shaves barber barber ↔ Β¬shaves barber barber, from H barber, have barber_does_not_shave_himself : Β¬shaves barber barber, from assume barber_shaves_himself : shaves barber barber, iff.elim_left H' barber_shaves_himself barber_shaves_himself, barber_does_not_shave_himself (iff.elim_right H' barber_does_not_shave_himself)
16964dae8117736bcfeed42548154cd9e791f59f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/calculus/parametric_interval_integral.lean
0d9494850c02e0a19f5ab1dc9cb211785b286c3f
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
6,596
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.calculus.parametric_integral import measure_theory.integral.interval_integral /-! # Derivatives of interval integrals depending on parameters > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we restate theorems about derivatives of integrals depending on parameters for interval integrals. -/ open topological_space measure_theory filter metric open_locale topology filter interval variables {π•œ : Type*} [is_R_or_C π•œ] {ΞΌ : measure ℝ} {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] [normed_space π•œ E] [complete_space E] {H : Type*} [normed_add_comm_group H] [normed_space π•œ H] {a b Ξ΅ : ℝ} {bound : ℝ β†’ ℝ} namespace interval_integral /-- Differentiation under integral of `x ↦ ∫ t in a..b, F x t` at a given point `xβ‚€`, assuming `F xβ‚€` is integrable, `x ↦ F x a` is locally Lipschitz on a ball around `xβ‚€` for ae `a` (with a ball radius independent of `a`) with integrable Lipschitz bound, and `F x` is ae-measurable for `x` in a possibly smaller neighborhood of `xβ‚€`. -/ lemma has_fderiv_at_integral_of_dominated_loc_of_lip {F : H β†’ ℝ β†’ E} {F' : ℝ β†’ (H β†’L[π•œ] E)} {xβ‚€ : H} (Ξ΅_pos : 0 < Ξ΅) (hF_meas : βˆ€αΆ  x in 𝓝 xβ‚€, ae_strongly_measurable (F x) (ΞΌ.restrict (Ξ™ a b))) (hF_int : interval_integrable (F xβ‚€) ΞΌ a b) (hF'_meas : ae_strongly_measurable F' (ΞΌ.restrict (Ξ™ a b))) (h_lip : βˆ€α΅ t βˆ‚ΞΌ, t ∈ Ξ™ a b β†’ lipschitz_on_with (real.nnabs $ bound t) (Ξ» x, F x t) (ball xβ‚€ Ξ΅)) (bound_integrable : interval_integrable bound ΞΌ a b) (h_diff : βˆ€α΅ t βˆ‚ΞΌ, t ∈ Ξ™ a b β†’ has_fderiv_at (Ξ» x, F x t) (F' t) xβ‚€) : interval_integrable F' ΞΌ a b ∧ has_fderiv_at (Ξ» x, ∫ t in a..b, F x t βˆ‚ΞΌ) (∫ t in a..b, F' t βˆ‚ΞΌ) xβ‚€ := begin simp only [interval_integrable_iff, interval_integral_eq_integral_uIoc, ← ae_restrict_iff' measurable_set_uIoc] at *, have := has_fderiv_at_integral_of_dominated_loc_of_lip Ξ΅_pos hF_meas hF_int hF'_meas h_lip bound_integrable h_diff, exact ⟨this.1, this.2.const_smul _⟩ end /-- Differentiation under integral of `x ↦ ∫ F x a` at a given point `xβ‚€`, assuming `F xβ‚€` is integrable, `x ↦ F x a` is differentiable on a ball around `xβ‚€` for ae `a` with derivative norm uniformly bounded by an integrable function (the ball radius is independent of `a`), and `F x` is ae-measurable for `x` in a possibly smaller neighborhood of `xβ‚€`. -/ lemma has_fderiv_at_integral_of_dominated_of_fderiv_le {F : H β†’ ℝ β†’ E} {F' : H β†’ ℝ β†’ (H β†’L[π•œ] E)} {xβ‚€ : H} (Ξ΅_pos : 0 < Ξ΅) (hF_meas : βˆ€αΆ  x in 𝓝 xβ‚€, ae_strongly_measurable (F x) (ΞΌ.restrict (Ξ™ a b))) (hF_int : interval_integrable (F xβ‚€) ΞΌ a b) (hF'_meas : ae_strongly_measurable (F' xβ‚€) (ΞΌ.restrict (Ξ™ a b))) (h_bound : βˆ€α΅ t βˆ‚ΞΌ, t ∈ Ξ™ a b β†’ βˆ€ x ∈ ball xβ‚€ Ξ΅, β€–F' x tβ€– ≀ bound t) (bound_integrable : interval_integrable bound ΞΌ a b) (h_diff : βˆ€α΅ t βˆ‚ΞΌ, t ∈ Ξ™ a b β†’ βˆ€ x ∈ ball xβ‚€ Ξ΅, has_fderiv_at (Ξ» x, F x t) (F' x t) x) : has_fderiv_at (Ξ» x, ∫ t in a..b, F x t βˆ‚ΞΌ) (∫ t in a..b, F' xβ‚€ t βˆ‚ΞΌ) xβ‚€ := begin simp only [interval_integrable_iff, interval_integral_eq_integral_uIoc, ← ae_restrict_iff' measurable_set_uIoc] at *, exact (has_fderiv_at_integral_of_dominated_of_fderiv_le Ξ΅_pos hF_meas hF_int hF'_meas h_bound bound_integrable h_diff).const_smul _ end /-- Derivative under integral of `x ↦ ∫ F x a` at a given point `xβ‚€ : π•œ`, `π•œ = ℝ` or `π•œ = β„‚`, assuming `F xβ‚€` is integrable, `x ↦ F x a` is locally Lipschitz on a ball around `xβ‚€` for ae `a` (with ball radius independent of `a`) with integrable Lipschitz bound, and `F x` is ae-measurable for `x` in a possibly smaller neighborhood of `xβ‚€`. -/ lemma has_deriv_at_integral_of_dominated_loc_of_lip {F : π•œ β†’ ℝ β†’ E} {F' : ℝ β†’ E} {xβ‚€ : π•œ} (Ξ΅_pos : 0 < Ξ΅) (hF_meas : βˆ€αΆ  x in 𝓝 xβ‚€, ae_strongly_measurable (F x) (ΞΌ.restrict (Ξ™ a b))) (hF_int : interval_integrable (F xβ‚€) ΞΌ a b) (hF'_meas : ae_strongly_measurable F' (ΞΌ.restrict (Ξ™ a b))) (h_lipsch : βˆ€α΅ t βˆ‚ΞΌ, t ∈ Ξ™ a b β†’ lipschitz_on_with (real.nnabs $ bound t) (Ξ» x, F x t) (ball xβ‚€ Ξ΅)) (bound_integrable : interval_integrable (bound : ℝ β†’ ℝ) ΞΌ a b) (h_diff : βˆ€α΅ t βˆ‚ΞΌ, t ∈ Ξ™ a b β†’ has_deriv_at (Ξ» x, F x t) (F' t) xβ‚€) : (interval_integrable F' ΞΌ a b) ∧ has_deriv_at (Ξ» x, ∫ t in a..b, F x t βˆ‚ΞΌ) (∫ t in a..b, F' t βˆ‚ΞΌ) xβ‚€ := begin simp only [interval_integrable_iff, interval_integral_eq_integral_uIoc, ← ae_restrict_iff' measurable_set_uIoc] at *, have := has_deriv_at_integral_of_dominated_loc_of_lip Ξ΅_pos hF_meas hF_int hF'_meas h_lipsch bound_integrable h_diff, exact ⟨this.1, this.2.const_smul _⟩ end /-- Derivative under integral of `x ↦ ∫ F x a` at a given point `xβ‚€ : π•œ`, `π•œ = ℝ` or `π•œ = β„‚`, assuming `F xβ‚€` is integrable, `x ↦ F x a` is differentiable on an interval around `xβ‚€` for ae `a` (with interval radius independent of `a`) with derivative uniformly bounded by an integrable function, and `F x` is ae-measurable for `x` in a possibly smaller neighborhood of `xβ‚€`. -/ lemma has_deriv_at_integral_of_dominated_loc_of_deriv_le {F : π•œ β†’ ℝ β†’ E} {F' : π•œ β†’ ℝ β†’ E} {xβ‚€ : π•œ} (Ξ΅_pos : 0 < Ξ΅) (hF_meas : βˆ€αΆ  x in 𝓝 xβ‚€, ae_strongly_measurable (F x) (ΞΌ.restrict (Ξ™ a b))) (hF_int : interval_integrable (F xβ‚€) ΞΌ a b) (hF'_meas : ae_strongly_measurable (F' xβ‚€) (ΞΌ.restrict (Ξ™ a b))) (h_bound : βˆ€α΅ t βˆ‚ΞΌ, t ∈ Ξ™ a b β†’ βˆ€ x ∈ ball xβ‚€ Ξ΅, β€–F' x tβ€– ≀ bound t) (bound_integrable : interval_integrable bound ΞΌ a b) (h_diff : βˆ€α΅ t βˆ‚ΞΌ, t ∈ Ξ™ a b β†’ βˆ€ x ∈ ball xβ‚€ Ξ΅, has_deriv_at (Ξ» x, F x t) (F' x t) x) : (interval_integrable (F' xβ‚€) ΞΌ a b) ∧ has_deriv_at (Ξ» x, ∫ t in a..b, F x t βˆ‚ΞΌ) (∫ t in a..b, F' xβ‚€ t βˆ‚ΞΌ) xβ‚€ := begin simp only [interval_integrable_iff, interval_integral_eq_integral_uIoc, ← ae_restrict_iff' measurable_set_uIoc] at *, have := has_deriv_at_integral_of_dominated_loc_of_deriv_le Ξ΅_pos hF_meas hF_int hF'_meas h_bound bound_integrable h_diff, exact ⟨this.1, this.2.const_smul _⟩ end end interval_integral
43156c88e657823ee77e446a9cad6f2352787b76
63abd62053d479eae5abf4951554e1064a4c45b4
/src/analysis/normed_space/operator_norm.lean
cd5de39b2371a590d17127cb82f11631339816fd
[ "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
42,293
lean
/- Copyright (c) 2019 Jan-David Salchow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, SΓ©bastien GouΓ«zel, Jean Lo -/ import linear_algebra.finite_dimensional import analysis.normed_space.riesz_lemma import analysis.asymptotics /-! # Operator norm on the space of continuous linear maps Define the operator norm on the space of continuous linear maps between normed spaces, and prove its basic properties. In particular, show that this space is itself a normed space. -/ noncomputable theory open_locale classical variables {π•œ : Type*} {E : Type*} {F : Type*} {G : Type*} [normed_group E] [normed_group F] [normed_group G] open metric continuous_linear_map lemma exists_pos_bound_of_bound {f : E β†’ F} (M : ℝ) (h : βˆ€x, βˆ₯f xβˆ₯ ≀ M * βˆ₯xβˆ₯) : βˆƒ N, 0 < N ∧ βˆ€x, βˆ₯f xβˆ₯ ≀ N * βˆ₯xβˆ₯ := ⟨max M 1, lt_of_lt_of_le zero_lt_one (le_max_right _ _), Ξ»x, calc βˆ₯f xβˆ₯ ≀ M * βˆ₯xβˆ₯ : h x ... ≀ max M 1 * βˆ₯xβˆ₯ : mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg _) ⟩ section normed_field /- Most statements in this file require the field to be non-discrete, as this is necessary to deduce an inequality `βˆ₯f xβˆ₯ ≀ C βˆ₯xβˆ₯` from the continuity of f. However, the other direction always holds. In this section, we just assume that `π•œ` is a normed field. In the remainder of the file, it will be non-discrete. -/ variables [normed_field π•œ] [normed_space π•œ E] [normed_space π•œ F] (f : E β†’β‚—[π•œ] F) lemma linear_map.lipschitz_of_bound (C : ℝ) (h : βˆ€x, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) : lipschitz_with (nnreal.of_real C) f := lipschitz_with.of_dist_le' $ Ξ» x y, by simpa only [dist_eq_norm, f.map_sub] using h (x - y) theorem linear_map.antilipschitz_of_bound {K : nnreal} (h : βˆ€ x, βˆ₯xβˆ₯ ≀ K * βˆ₯f xβˆ₯) : antilipschitz_with K f := antilipschitz_with.of_le_mul_dist $ Ξ» x y, by simpa only [dist_eq_norm, f.map_sub] using h (x - y) lemma linear_map.uniform_continuous_of_bound (C : ℝ) (h : βˆ€x, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) : uniform_continuous f := (f.lipschitz_of_bound C h).uniform_continuous lemma linear_map.continuous_of_bound (C : ℝ) (h : βˆ€x, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) : continuous f := (f.lipschitz_of_bound C h).continuous /-- Construct a continuous linear map from a linear map and a bound on this linear map. The fact that the norm of the continuous linear map is then controlled is given in `linear_map.mk_continuous_norm_le`. -/ def linear_map.mk_continuous (C : ℝ) (h : βˆ€x, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) : E β†’L[π•œ] F := ⟨f, linear_map.continuous_of_bound f C h⟩ /-- Reinterpret a linear map `π•œ β†’β‚—[π•œ] E` as a continuous linear map. This construction is generalized to the case of any finite dimensional domain in `linear_map.to_continuous_linear_map`. -/ def linear_map.to_continuous_linear_map₁ (f : π•œ β†’β‚—[π•œ] E) : π•œ β†’L[π•œ] E := f.mk_continuous (βˆ₯f 1βˆ₯) $ Ξ» x, le_of_eq $ by { conv_lhs { rw ← mul_one x }, rw [← smul_eq_mul, f.map_smul, norm_smul, mul_comm] } /-- Construct a continuous linear map from a linear map and the existence of a bound on this linear map. If you have an explicit bound, use `linear_map.mk_continuous` instead, as a norm estimate will follow automatically in `linear_map.mk_continuous_norm_le`. -/ def linear_map.mk_continuous_of_exists_bound (h : βˆƒC, βˆ€x, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) : E β†’L[π•œ] F := ⟨f, let ⟨C, hC⟩ := h in linear_map.continuous_of_bound f C hC⟩ lemma continuous_of_linear_of_bound {f : E β†’ F} (h_add : βˆ€ x y, f (x + y) = f x + f y) (h_smul : βˆ€ (c : π•œ) x, f (c β€’ x) = c β€’ f x) {C : ℝ} (h_bound : βˆ€ x, βˆ₯f xβˆ₯ ≀ C*βˆ₯xβˆ₯) : continuous f := let Ο† : E β†’β‚—[π•œ] F := ⟨f, h_add, h_smul⟩ in Ο†.continuous_of_bound C h_bound @[simp, norm_cast] lemma linear_map.mk_continuous_coe (C : ℝ) (h : βˆ€x, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) : ((f.mk_continuous C h) : E β†’β‚—[π•œ] F) = f := rfl @[simp] lemma linear_map.mk_continuous_apply (C : ℝ) (h : βˆ€x, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) (x : E) : f.mk_continuous C h x = f x := rfl @[simp, norm_cast] lemma linear_map.mk_continuous_of_exists_bound_coe (h : βˆƒC, βˆ€x, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) : ((f.mk_continuous_of_exists_bound h) : E β†’β‚—[π•œ] F) = f := rfl @[simp] lemma linear_map.mk_continuous_of_exists_bound_apply (h : βˆƒC, βˆ€x, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) (x : E) : f.mk_continuous_of_exists_bound h x = f x := rfl @[simp] lemma linear_map.to_continuous_linear_map₁_coe (f : π•œ β†’β‚—[π•œ] E) : (f.to_continuous_linear_map₁ : π•œ β†’β‚—[π•œ] E) = f := rfl @[simp] lemma linear_map.to_continuous_linear_map₁_apply (f : π•œ β†’β‚—[π•œ] E) (x) : f.to_continuous_linear_map₁ x = f x := rfl lemma linear_map.continuous_iff_is_closed_ker {f : E β†’β‚—[π•œ] π•œ} : continuous f ↔ is_closed (f.ker : set E) := begin -- the continuity of f obviously implies that its kernel is closed refine ⟨λh, (continuous_iff_is_closed.1 h) {0} (t1_space.t1 0), Ξ»h, _⟩, -- for the other direction, we assume that the kernel is closed by_cases hf : βˆ€x, x ∈ f.ker, { -- if `f = 0`, its continuity is obvious have : (f : E β†’ π•œ) = (Ξ»x, 0), by { ext x, simpa using hf x }, rw this, exact continuous_const }, { /- if `f` is not zero, we use an element `xβ‚€ βˆ‰ ker f` such that `βˆ₯xβ‚€βˆ₯ ≀ 2 βˆ₯xβ‚€ - yβˆ₯` for all `y ∈ ker f`, given by Riesz's lemma, and prove that `2 βˆ₯f xβ‚€βˆ₯ / βˆ₯xβ‚€βˆ₯` gives a bound on the operator norm of `f`. For this, start from an arbitrary `x` and note that `y = xβ‚€ - (f xβ‚€ / f x) x` belongs to the kernel of `f`. Applying the above inequality to `xβ‚€` and `y` readily gives the conclusion. -/ push_neg at hf, let r : ℝ := (2 : ℝ)⁻¹, have : 0 ≀ r, by norm_num [r], have : r < 1, by norm_num [r], obtain ⟨xβ‚€, xβ‚€ker, hβ‚€βŸ© : βˆƒ (xβ‚€ : E), xβ‚€ βˆ‰ f.ker ∧ βˆ€ y ∈ linear_map.ker f, r * βˆ₯xβ‚€βˆ₯ ≀ βˆ₯xβ‚€ - yβˆ₯, from riesz_lemma h hf this, have : xβ‚€ β‰  0, { assume h, have : xβ‚€ ∈ f.ker, by { rw h, exact (linear_map.ker f).zero_mem }, exact xβ‚€ker this }, have rxβ‚€_ne_zero : r * βˆ₯xβ‚€βˆ₯ β‰  0, by { simp [norm_eq_zero, this], norm_num }, have : βˆ€x, βˆ₯f xβˆ₯ ≀ (((r * βˆ₯xβ‚€βˆ₯)⁻¹) * βˆ₯f xβ‚€βˆ₯) * βˆ₯xβˆ₯, { assume x, by_cases hx : f x = 0, { rw [hx, norm_zero], apply_rules [mul_nonneg, norm_nonneg, inv_nonneg.2] }, { let y := xβ‚€ - (f xβ‚€ * (f x)⁻¹ ) β€’ x, have fy_zero : f y = 0, by calc f y = f xβ‚€ - (f xβ‚€ * (f x)⁻¹ ) * f x : by simp [y] ... = 0 : by { rw [mul_assoc, inv_mul_cancel hx, mul_one, sub_eq_zero_of_eq], refl }, have A : r * βˆ₯xβ‚€βˆ₯ ≀ βˆ₯f xβ‚€βˆ₯ * βˆ₯f xβˆ₯⁻¹ * βˆ₯xβˆ₯, from calc r * βˆ₯xβ‚€βˆ₯ ≀ βˆ₯xβ‚€ - yβˆ₯ : hβ‚€ _ (linear_map.mem_ker.2 fy_zero) ... = βˆ₯(f xβ‚€ * (f x)⁻¹ ) β€’ xβˆ₯ : by { dsimp [y], congr, abel } ... = βˆ₯f xβ‚€βˆ₯ * βˆ₯f xβˆ₯⁻¹ * βˆ₯xβˆ₯ : by rw [norm_smul, normed_field.norm_mul, normed_field.norm_inv], calc βˆ₯f xβˆ₯ = (r * βˆ₯xβ‚€βˆ₯)⁻¹ * (r * βˆ₯xβ‚€βˆ₯) * βˆ₯f xβˆ₯ : by rwa [inv_mul_cancel, one_mul] ... ≀ (r * βˆ₯xβ‚€βˆ₯)⁻¹ * (βˆ₯f xβ‚€βˆ₯ * βˆ₯f xβˆ₯⁻¹ * βˆ₯xβˆ₯) * βˆ₯f xβˆ₯ : begin apply mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_left A _) (norm_nonneg _), exact inv_nonneg.2 (mul_nonneg (by norm_num) (norm_nonneg _)) end ... = (βˆ₯f xβˆ₯ ⁻¹ * βˆ₯f xβˆ₯) * (((r * βˆ₯xβ‚€βˆ₯)⁻¹) * βˆ₯f xβ‚€βˆ₯) * βˆ₯xβˆ₯ : by ring ... = (((r * βˆ₯xβ‚€βˆ₯)⁻¹) * βˆ₯f xβ‚€βˆ₯) * βˆ₯xβˆ₯ : by { rw [inv_mul_cancel, one_mul], simp [norm_eq_zero, hx] } } }, exact linear_map.continuous_of_bound f _ this } end end normed_field variables [nondiscrete_normed_field π•œ] [normed_space π•œ E] [normed_space π•œ F] [normed_space π•œ G] (c : π•œ) (f g : E β†’L[π•œ] F) (h : F β†’L[π•œ] G) (x y z : E) include π•œ /-- A continuous linear map between normed spaces is bounded when the field is nondiscrete. The continuity ensures boundedness on a ball of some radius `Ξ΄`. The nondiscreteness is then used to rescale any element into an element of norm in `[Ξ΄/C, Ξ΄]`, whose image has a controlled norm. The norm control for the original element follows by rescaling. -/ lemma linear_map.bound_of_continuous (f : E β†’β‚—[π•œ] F) (hf : continuous f) : βˆƒ C, 0 < C ∧ (βˆ€ x : E, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) := begin have : continuous_at f 0 := continuous_iff_continuous_at.1 hf _, rcases metric.tendsto_nhds_nhds.1 this 1 zero_lt_one with ⟨Ρ, Ξ΅_pos, hΡ⟩, let Ξ΄ := Ξ΅/2, have Ξ΄_pos : Ξ΄ > 0 := half_pos Ξ΅_pos, have H : βˆ€{a}, βˆ₯aβˆ₯ ≀ Ξ΄ β†’ βˆ₯f aβˆ₯ ≀ 1, { assume a ha, have : dist (f a) (f 0) ≀ 1, { apply le_of_lt (hΞ΅ _), rw [dist_eq_norm, sub_zero], exact lt_of_le_of_lt ha (half_lt_self Ξ΅_pos) }, simpa using this }, rcases normed_field.exists_one_lt_norm π•œ with ⟨c, hc⟩, refine ⟨δ⁻¹ * βˆ₯cβˆ₯, mul_pos (inv_pos.2 Ξ΄_pos) (lt_trans zero_lt_one hc), (Ξ»x, _)⟩, by_cases h : x = 0, { simp only [h, norm_zero, mul_zero, linear_map.map_zero] }, { rcases rescale_to_shell hc Ξ΄_pos h with ⟨d, hd, dxle, ledx, dinv⟩, calc βˆ₯f xβˆ₯ = βˆ₯f ((d⁻¹ * d) β€’ x)βˆ₯ : by rwa [inv_mul_cancel, one_smul] ... = βˆ₯dβˆ₯⁻¹ * βˆ₯f (d β€’ x)βˆ₯ : by rw [mul_smul, linear_map.map_smul, norm_smul, normed_field.norm_inv] ... ≀ βˆ₯dβˆ₯⁻¹ * 1 : mul_le_mul_of_nonneg_left (H dxle) (by { rw ← normed_field.norm_inv, exact norm_nonneg _ }) ... ≀ δ⁻¹ * βˆ₯cβˆ₯ * βˆ₯xβˆ₯ : by { rw mul_one, exact dinv } } end namespace continuous_linear_map theorem bound : βˆƒ C, 0 < C ∧ (βˆ€ x : E, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) := f.to_linear_map.bound_of_continuous f.2 section open asymptotics filter theorem is_O_id (l : filter E) : is_O f (Ξ» x, x) l := let ⟨M, hMp, hM⟩ := f.bound in is_O_of_le' l hM theorem is_O_comp {Ξ± : Type*} (g : F β†’L[π•œ] G) (f : Ξ± β†’ F) (l : filter Ξ±) : is_O (Ξ» x', g (f x')) f l := (g.is_O_id ⊀).comp_tendsto le_top theorem is_O_sub (f : E β†’L[π•œ] F) (l : filter E) (x : E) : is_O (Ξ» x', f (x' - x)) (Ξ» x', x' - x) l := f.is_O_comp _ l /-- A linear map which is a homothety is a continuous linear map. Since the field `π•œ` need not have `ℝ` as a subfield, this theorem is not directly deducible from the corresponding theorem about isometries plus a theorem about scalar multiplication. Likewise for the other theorems about homotheties in this file. -/ def of_homothety (f : E β†’β‚—[π•œ] F) (a : ℝ) (hf : βˆ€x, βˆ₯f xβˆ₯ = a * βˆ₯xβˆ₯) : E β†’L[π•œ] F := f.mk_continuous a (Ξ» x, le_of_eq (hf x)) variable (π•œ) lemma to_span_singleton_homothety (x : E) (c : π•œ) : βˆ₯linear_map.to_span_singleton π•œ E x cβˆ₯ = βˆ₯xβˆ₯ * βˆ₯cβˆ₯ := by {rw mul_comm, exact norm_smul _ _} /-- Given an element `x` of a normed space `E` over a field `π•œ`, the natural continuous linear map from `E` to the span of `x`.-/ def to_span_singleton (x : E) : π•œ β†’L[π•œ] E := of_homothety (linear_map.to_span_singleton π•œ E x) βˆ₯xβˆ₯ (to_span_singleton_homothety π•œ x) end section op_norm open set real /-- The operator norm of a continuous linear map is the inf of all its bounds. -/ def op_norm := Inf {c | 0 ≀ c ∧ βˆ€ x, βˆ₯f xβˆ₯ ≀ c * βˆ₯xβˆ₯} instance has_op_norm : has_norm (E β†’L[π•œ] F) := ⟨op_norm⟩ lemma norm_def : βˆ₯fβˆ₯ = Inf {c | 0 ≀ c ∧ βˆ€ x, βˆ₯f xβˆ₯ ≀ c * βˆ₯xβˆ₯} := rfl -- So that invocations of `real.Inf_le` make sense: we show that the set of -- bounds is nonempty and bounded below. lemma bounds_nonempty {f : E β†’L[π•œ] F} : βˆƒ c, c ∈ { c | 0 ≀ c ∧ βˆ€ x, βˆ₯f xβˆ₯ ≀ c * βˆ₯xβˆ₯ } := let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩ lemma bounds_bdd_below {f : E β†’L[π•œ] F} : bdd_below { c | 0 ≀ c ∧ βˆ€ x, βˆ₯f xβˆ₯ ≀ c * βˆ₯xβˆ₯ } := ⟨0, Ξ» _ ⟨hn, _⟩, hn⟩ lemma op_norm_nonneg : 0 ≀ βˆ₯fβˆ₯ := lb_le_Inf _ bounds_nonempty (Ξ» _ ⟨hx, _⟩, hx) /-- The fundamental property of the operator norm: `βˆ₯f xβˆ₯ ≀ βˆ₯fβˆ₯ * βˆ₯xβˆ₯`. -/ theorem le_op_norm : βˆ₯f xβˆ₯ ≀ βˆ₯fβˆ₯ * βˆ₯xβˆ₯ := classical.by_cases (Ξ» heq : x = 0, by { rw heq, simp }) (Ξ» hne, have hlt : 0 < βˆ₯xβˆ₯, from norm_pos_iff.2 hne, (div_le_iff hlt).mp ((le_Inf _ bounds_nonempty bounds_bdd_below).2 (Ξ» c ⟨_, hc⟩, (div_le_iff hlt).mpr $ by { apply hc }))) theorem le_op_norm_of_le {c : ℝ} {x} (h : βˆ₯xβˆ₯ ≀ c) : βˆ₯f xβˆ₯ ≀ βˆ₯fβˆ₯ * c := le_trans (f.le_op_norm x) (mul_le_mul_of_nonneg_left h f.op_norm_nonneg) /-- continuous linear maps are Lipschitz continuous. -/ theorem lipschitz : lipschitz_with ⟨βˆ₯fβˆ₯, op_norm_nonneg f⟩ f := lipschitz_with.of_dist_le_mul $ Ξ» x y, by { rw [dist_eq_norm, dist_eq_norm, ←map_sub], apply le_op_norm } lemma ratio_le_op_norm : βˆ₯f xβˆ₯ / βˆ₯xβˆ₯ ≀ βˆ₯fβˆ₯ := div_le_iff_of_nonneg_of_le (norm_nonneg _) f.op_norm_nonneg (le_op_norm _ _) /-- The image of the unit ball under a continuous linear map is bounded. -/ lemma unit_le_op_norm : βˆ₯xβˆ₯ ≀ 1 β†’ βˆ₯f xβˆ₯ ≀ βˆ₯fβˆ₯ := mul_one βˆ₯fβˆ₯ β–Έ f.le_op_norm_of_le /-- If one controls the norm of every `A x`, then one controls the norm of `A`. -/ lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≀ M) (hM : βˆ€ x, βˆ₯f xβˆ₯ ≀ M * βˆ₯xβˆ₯) : βˆ₯fβˆ₯ ≀ M := Inf_le _ bounds_bdd_below ⟨hMp, hM⟩ theorem op_norm_le_of_lipschitz {f : E β†’L[π•œ] F} {K : nnreal} (hf : lipschitz_with K f) : βˆ₯fβˆ₯ ≀ K := f.op_norm_le_bound K.2 $ Ξ» x, by simpa only [dist_zero_right, f.map_zero] using hf.dist_le_mul x 0 lemma op_norm_le_of_ball {f : E β†’L[π•œ] F} {Ξ΅ : ℝ} {C : ℝ} (Ξ΅_pos : 0 < Ξ΅) (hC : 0 ≀ C) (hf : βˆ€ x ∈ ball (0 : E) Ξ΅, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) : βˆ₯fβˆ₯ ≀ C := begin apply f.op_norm_le_bound hC, intros x, rcases normed_field.exists_one_lt_norm π•œ with ⟨c, hc⟩, by_cases hx : x = 0, { simp [hx] }, rcases rescale_to_shell hc (half_pos Ξ΅_pos) hx with ⟨δ, hΞ΄, Ξ΄xle, leΞ΄x, Ξ΄inv⟩, have Ξ΄x_in : Ξ΄ β€’ x ∈ ball (0 : E) Ξ΅, { rw [mem_ball, dist_eq_norm, sub_zero], linarith }, calc βˆ₯f xβˆ₯ = βˆ₯f ((1/Ξ΄) β€’ Ξ΄ β€’ x)βˆ₯ : by simp [hΞ΄, smul_smul] ... = βˆ₯1/Ξ΄βˆ₯ * βˆ₯f (Ξ΄ β€’ x)βˆ₯ : by simp [norm_smul] ... ≀ βˆ₯1/Ξ΄βˆ₯ * (C*βˆ₯Ξ΄ β€’ xβˆ₯) : mul_le_mul_of_nonneg_left _ (norm_nonneg _) ... = C * βˆ₯xβˆ₯ : by { rw norm_smul, field_simp [hΞ΄], ring }, exact hf _ Ξ΄x_in end lemma op_norm_eq_of_bounds {Ο† : E β†’L[π•œ] F} {M : ℝ} (M_nonneg : 0 ≀ M) (h_above : βˆ€ x, βˆ₯Ο† xβˆ₯ ≀ M*βˆ₯xβˆ₯) (h_below : βˆ€ N β‰₯ 0, (βˆ€ x, βˆ₯Ο† xβˆ₯ ≀ N*βˆ₯xβˆ₯) β†’ M ≀ N) : βˆ₯Ο†βˆ₯ = M := le_antisymm (Ο†.op_norm_le_bound M_nonneg h_above) ((le_cInf_iff continuous_linear_map.bounds_bdd_below ⟨M, M_nonneg, h_above⟩).mpr $ Ξ» N ⟨N_nonneg, hN⟩, h_below N N_nonneg hN) /-- The operator norm satisfies the triangle inequality. -/ theorem op_norm_add_le : βˆ₯f + gβˆ₯ ≀ βˆ₯fβˆ₯ + βˆ₯gβˆ₯ := show βˆ₯f + gβˆ₯ ≀ (coe : nnreal β†’ ℝ) (⟨_, f.op_norm_nonneg⟩ + ⟨_, g.op_norm_nonneg⟩), from op_norm_le_of_lipschitz (f.lipschitz.add g.lipschitz) /-- An operator is zero iff its norm vanishes. -/ theorem op_norm_zero_iff : βˆ₯fβˆ₯ = 0 ↔ f = 0 := iff.intro (Ξ» hn, continuous_linear_map.ext (Ξ» x, norm_le_zero_iff.1 (calc _ ≀ βˆ₯fβˆ₯ * βˆ₯xβˆ₯ : le_op_norm _ _ ... = _ : by rw [hn, zero_mul]))) (Ξ» hf, le_antisymm (Inf_le _ bounds_bdd_below ⟨ge_of_eq rfl, Ξ» _, le_of_eq (by { rw [zero_mul, hf], exact norm_zero })⟩) (op_norm_nonneg _)) /-- The norm of the identity is at most `1`. It is in fact `1`, except when the space is trivial where it is `0`. It means that one can not do better than an inequality in general. -/ lemma norm_id_le : βˆ₯id π•œ Eβˆ₯ ≀ 1 := op_norm_le_bound _ zero_le_one (Ξ»x, by simp) /-- If a space is non-trivial, then the norm of the identity equals `1`. -/ lemma norm_id [nontrivial E] : βˆ₯id π•œ Eβˆ₯ = 1 := le_antisymm norm_id_le $ let ⟨x, hx⟩ := exists_ne (0 : E) in have _ := (id π•œ E).ratio_le_op_norm x, by rwa [id_apply, div_self (ne_of_gt $ norm_pos_iff.2 hx)] at this @[simp] lemma norm_id_field : βˆ₯id π•œ π•œβˆ₯ = 1 := norm_id @[simp] lemma norm_id_field' : βˆ₯(1 : π•œ β†’L[π•œ] π•œ)βˆ₯ = 1 := norm_id_field lemma op_norm_smul_le : βˆ₯c β€’ fβˆ₯ ≀ βˆ₯cβˆ₯ * βˆ₯fβˆ₯ := ((c β€’ f).op_norm_le_bound (mul_nonneg (norm_nonneg _) (op_norm_nonneg _)) (Ξ» _, begin erw [norm_smul, mul_assoc], exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _) end)) lemma op_norm_neg : βˆ₯-fβˆ₯ = βˆ₯fβˆ₯ := by { rw norm_def, apply congr_arg, ext, simp } /-- Continuous linear maps themselves form a normed space with respect to the operator norm. -/ instance to_normed_group : normed_group (E β†’L[π•œ] F) := normed_group.of_core _ ⟨op_norm_zero_iff, op_norm_add_le, op_norm_neg⟩ instance to_normed_space : normed_space π•œ (E β†’L[π•œ] F) := ⟨op_norm_smul_le⟩ /-- The operator norm is submultiplicative. -/ lemma op_norm_comp_le (f : E β†’L[π•œ] F) : βˆ₯h.comp fβˆ₯ ≀ βˆ₯hβˆ₯ * βˆ₯fβˆ₯ := (Inf_le _ bounds_bdd_below ⟨mul_nonneg (op_norm_nonneg _) (op_norm_nonneg _), Ξ» x, by { rw mul_assoc, exact h.le_op_norm_of_le (f.le_op_norm x) } ⟩) /-- Continuous linear maps form a normed ring with respect to the operator norm. -/ instance to_normed_ring : normed_ring (E β†’L[π•œ] E) := { norm_mul := op_norm_comp_le, .. continuous_linear_map.to_normed_group } /-- For a nonzero normed space `E`, continuous linear endomorphisms form a normed algebra with respect to the operator norm. -/ instance to_normed_algebra [nontrivial E] : normed_algebra π•œ (E β†’L[π•œ] E) := { norm_algebra_map_eq := Ξ» c, show βˆ₯c β€’ id π•œ Eβˆ₯ = βˆ₯cβˆ₯, by {rw [norm_smul, norm_id], simp}, .. continuous_linear_map.algebra } /-- A continuous linear map is automatically uniformly continuous. -/ protected theorem uniform_continuous : uniform_continuous f := f.lipschitz.uniform_continuous variable {f} /-- A continuous linear map is an isometry if and only if it preserves the norm. -/ lemma isometry_iff_norm_image_eq_norm : isometry f ↔ βˆ€x, βˆ₯f xβˆ₯ = βˆ₯xβˆ₯ := begin rw isometry_emetric_iff_metric, split, { assume H x, have := H x 0, rwa [dist_eq_norm, dist_eq_norm, f.map_zero, sub_zero, sub_zero] at this }, { assume H x y, rw [dist_eq_norm, dist_eq_norm, ← f.map_sub, H] } end lemma homothety_norm [nontrivial E] (f : E β†’L[π•œ] F) {a : ℝ} (hf : βˆ€x, βˆ₯f xβˆ₯ = a * βˆ₯xβˆ₯) : βˆ₯fβˆ₯ = a := begin obtain ⟨x, hx⟩ : βˆƒ (x : E), x β‰  0 := exists_ne 0, have ha : 0 ≀ a, { apply nonneg_of_mul_nonneg_right, rw ← hf x, apply norm_nonneg, exact norm_pos_iff.mpr hx }, refine le_antisymm_iff.mpr ⟨_, _⟩, { exact continuous_linear_map.op_norm_le_bound f ha (Ξ» y, le_of_eq (hf y)) }, { rw continuous_linear_map.norm_def, apply real.lb_le_Inf _ continuous_linear_map.bounds_nonempty, intros c h, rw mem_set_of_eq at h, apply (mul_le_mul_right (norm_pos_iff.mpr hx)).mp, rw ← hf x, exact h.2 x } end lemma to_span_singleton_norm (x : E) : βˆ₯to_span_singleton π•œ xβˆ₯ = βˆ₯xβˆ₯ := homothety_norm _ (to_span_singleton_homothety π•œ x) variable (f) theorem uniform_embedding_of_bound {K : nnreal} (hf : βˆ€ x, βˆ₯xβˆ₯ ≀ K * βˆ₯f xβˆ₯) : uniform_embedding f := (f.to_linear_map.antilipschitz_of_bound hf).uniform_embedding f.uniform_continuous /-- If a continuous linear map is a uniform embedding, then it is expands the distances by a positive factor.-/ theorem antilipschitz_of_uniform_embedding (hf : uniform_embedding f) : βˆƒ K, antilipschitz_with K f := begin obtain ⟨Ρ, Ξ΅pos, hΡ⟩ : βˆƒ (Ξ΅ : ℝ) (H : Ξ΅ > 0), βˆ€ {x y : E}, dist (f x) (f y) < Ξ΅ β†’ dist x y < 1, from (uniform_embedding_iff.1 hf).2.2 1 zero_lt_one, let Ξ΄ := Ξ΅/2, have Ξ΄_pos : Ξ΄ > 0 := half_pos Ξ΅pos, have H : βˆ€{x}, βˆ₯f xβˆ₯ ≀ Ξ΄ β†’ βˆ₯xβˆ₯ ≀ 1, { assume x hx, have : dist x 0 ≀ 1, { apply le_of_lt, apply hΞ΅, simp [dist_eq_norm], exact lt_of_le_of_lt hx (half_lt_self Ξ΅pos) }, simpa using this }, rcases normed_field.exists_one_lt_norm π•œ with ⟨c, hc⟩, refine ⟨⟨δ⁻¹, _⟩ * nnnorm c, f.to_linear_map.antilipschitz_of_bound $ Ξ»x, _⟩, exact inv_nonneg.2 (le_of_lt Ξ΄_pos), by_cases hx : f x = 0, { have : f x = f 0, by { simp [hx] }, have : x = 0 := (uniform_embedding_iff.1 hf).1 this, simp [this] }, { rcases rescale_to_shell hc Ξ΄_pos hx with ⟨d, hd, dxle, ledx, dinv⟩, have : βˆ₯f (d β€’ x)βˆ₯ ≀ Ξ΄, by simpa, have : βˆ₯d β€’ xβˆ₯ ≀ 1 := H this, calc βˆ₯xβˆ₯ = βˆ₯dβˆ₯⁻¹ * βˆ₯d β€’ xβˆ₯ : by rwa [← normed_field.norm_inv, ← norm_smul, ← mul_smul, inv_mul_cancel, one_smul] ... ≀ βˆ₯dβˆ₯⁻¹ * 1 : mul_le_mul_of_nonneg_left this (inv_nonneg.2 (norm_nonneg _)) ... ≀ δ⁻¹ * βˆ₯cβˆ₯ * βˆ₯f xβˆ₯ : by rwa [mul_one] } end section completeness open_locale topological_space open filter /-- If the target space is complete, the space of continuous linear maps with its norm is also complete. -/ instance [complete_space F] : complete_space (E β†’L[π•œ] F) := begin -- We show that every Cauchy sequence converges. refine metric.complete_of_cauchy_seq_tendsto (Ξ» f hf, _), -- We now expand out the definition of a Cauchy sequence, rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩, clear hf, -- and establish that the evaluation at any point `v : E` is Cauchy. have cau : βˆ€ v, cauchy_seq (Ξ» n, f n v), { assume v, apply cauchy_seq_iff_le_tendsto_0.2 ⟨λ n, b n * βˆ₯vβˆ₯, Ξ» n, _, _, _⟩, { exact mul_nonneg (b0 n) (norm_nonneg _) }, { assume n m N hn hm, rw dist_eq_norm, apply le_trans ((f n - f m).le_op_norm v) _, exact mul_le_mul_of_nonneg_right (b_bound n m N hn hm) (norm_nonneg v) }, { simpa using b_lim.mul tendsto_const_nhds } }, -- We assemble the limits points of those Cauchy sequences -- (which exist as `F` is complete) -- into a function which we call `G`. choose G hG using Ξ»v, cauchy_seq_tendsto_of_complete (cau v), -- Next, we show that this `G` is linear, let Glin : E β†’β‚—[π•œ] F := { to_fun := G, map_add' := Ξ» v w, begin have A := hG (v + w), have B := (hG v).add (hG w), simp only [map_add] at A B, exact tendsto_nhds_unique A B, end, map_smul' := Ξ» c v, begin have A := hG (c β€’ v), have B := filter.tendsto.smul (@tendsto_const_nhds _ β„• _ c _) (hG v), simp only [map_smul] at A B, exact tendsto_nhds_unique A B end }, -- and that `G` has norm at most `(b 0 + βˆ₯f 0βˆ₯)`. have Gnorm : βˆ€ v, βˆ₯G vβˆ₯ ≀ (b 0 + βˆ₯f 0βˆ₯) * βˆ₯vβˆ₯, { assume v, have A : βˆ€ n, βˆ₯f n vβˆ₯ ≀ (b 0 + βˆ₯f 0βˆ₯) * βˆ₯vβˆ₯, { assume n, apply le_trans ((f n).le_op_norm _) _, apply mul_le_mul_of_nonneg_right _ (norm_nonneg v), calc βˆ₯f nβˆ₯ = βˆ₯(f n - f 0) + f 0βˆ₯ : by { congr' 1, abel } ... ≀ βˆ₯f n - f 0βˆ₯ + βˆ₯f 0βˆ₯ : norm_add_le _ _ ... ≀ b 0 + βˆ₯f 0βˆ₯ : begin apply add_le_add_right, simpa [dist_eq_norm] using b_bound n 0 0 (zero_le _) (zero_le _) end }, exact le_of_tendsto (hG v).norm (eventually_of_forall A) }, -- Thus `G` is continuous, and we propose that as the limit point of our original Cauchy sequence. let Gcont := Glin.mk_continuous _ Gnorm, use Gcont, -- Our last task is to establish convergence to `G` in norm. have : βˆ€ n, βˆ₯f n - Gcontβˆ₯ ≀ b n, { assume n, apply op_norm_le_bound _ (b0 n) (Ξ» v, _), have A : βˆ€αΆ  m in at_top, βˆ₯(f n - f m) vβˆ₯ ≀ b n * βˆ₯vβˆ₯, { refine eventually_at_top.2 ⟨n, Ξ» m hm, _⟩, apply le_trans ((f n - f m).le_op_norm _) _, exact mul_le_mul_of_nonneg_right (b_bound n m n (le_refl _) hm) (norm_nonneg v) }, have B : tendsto (Ξ» m, βˆ₯(f n - f m) vβˆ₯) at_top (𝓝 (βˆ₯(f n - Gcont) vβˆ₯)) := tendsto.norm (tendsto_const_nhds.sub (hG v)), exact le_of_tendsto B A }, erw tendsto_iff_norm_tendsto_zero, exact squeeze_zero (Ξ» n, norm_nonneg _) this b_lim, end end completeness section uniformly_extend variables [complete_space F] (e : E β†’L[π•œ] G) (h_dense : dense_range e) section variables (h_e : uniform_inducing e) /-- Extension of a continuous linear map `f : E β†’L[π•œ] F`, with `E` a normed space and `F` a complete normed space, along a uniform and dense embedding `e : E β†’L[π•œ] G`. -/ def extend : G β†’L[π•œ] F := /- extension of `f` is continuous -/ have cont : _ := (uniform_continuous_uniformly_extend h_e h_dense f.uniform_continuous).continuous, /- extension of `f` agrees with `f` on the domain of the embedding `e` -/ have eq : _ := uniformly_extend_of_ind h_e h_dense f.uniform_continuous, { to_fun := (h_e.dense_inducing h_dense).extend f, map_add' := begin refine h_dense.induction_onβ‚‚ _ _, { exact is_closed_eq (cont.comp continuous_add) ((cont.comp continuous_fst).add (cont.comp continuous_snd)) }, { assume x y, simp only [eq, ← e.map_add], exact f.map_add _ _ }, end, map_smul' := Ξ»k, begin refine (Ξ» b, h_dense.induction_on b _ _), { exact is_closed_eq (cont.comp (continuous_const.smul continuous_id)) ((continuous_const.smul continuous_id).comp cont) }, { assume x, rw ← map_smul, simp only [eq], exact map_smul _ _ _ }, end, cont := cont } lemma extend_unique (g : G β†’L[π•œ] F) (H : g.comp e = f) : extend f e h_dense h_e = g := continuous_linear_map.injective_coe_fn $ uniformly_extend_unique h_e h_dense (continuous_linear_map.ext_iff.1 H) g.continuous @[simp] lemma extend_zero : extend (0 : E β†’L[π•œ] F) e h_dense h_e = 0 := extend_unique _ _ _ _ _ (zero_comp _) end section variables {N : nnreal} (h_e : βˆ€x, βˆ₯xβˆ₯ ≀ N * βˆ₯e xβˆ₯) local notation `ψ` := f.extend e h_dense (uniform_embedding_of_bound _ h_e).to_uniform_inducing /-- If a dense embedding `e : E β†’L[π•œ] G` expands the norm by a constant factor `N⁻¹`, then the norm of the extension of `f` along `e` is bounded by `N * βˆ₯fβˆ₯`. -/ lemma op_norm_extend_le : βˆ₯ψβˆ₯ ≀ N * βˆ₯fβˆ₯ := begin have uni : uniform_inducing e := (uniform_embedding_of_bound _ h_e).to_uniform_inducing, have eq : βˆ€x, ψ (e x) = f x := uniformly_extend_of_ind uni h_dense f.uniform_continuous, by_cases N0 : 0 ≀ N, { refine op_norm_le_bound ψ _ (is_closed_property h_dense (is_closed_le _ _) _), { exact mul_nonneg N0 (norm_nonneg _) }, { exact continuous_norm.comp (cont ψ) }, { exact continuous_const.mul continuous_norm }, { assume x, rw eq, calc βˆ₯f xβˆ₯ ≀ βˆ₯fβˆ₯ * βˆ₯xβˆ₯ : le_op_norm _ _ ... ≀ βˆ₯fβˆ₯ * (N * βˆ₯e xβˆ₯) : mul_le_mul_of_nonneg_left (h_e x) (norm_nonneg _) ... ≀ N * βˆ₯fβˆ₯ * βˆ₯e xβˆ₯ : by rw [mul_comm ↑N βˆ₯fβˆ₯, mul_assoc] } }, { have he : βˆ€ x : E, x = 0, { assume x, have N0 : N ≀ 0 := le_of_lt (lt_of_not_ge N0), rw ← norm_le_zero_iff, exact le_trans (h_e x) (mul_nonpos_of_nonpos_of_nonneg N0 (norm_nonneg _)) }, have hf : f = 0, { ext, simp only [he x, zero_apply, map_zero] }, have hψ : ψ = 0, { rw hf, apply extend_zero }, rw [hψ, hf, norm_zero, norm_zero, mul_zero] } end end end uniformly_extend end op_norm end continuous_linear_map /-- If a continuous linear map is constructed from a linear map via the constructor `mk_continuous`, then its norm is bounded by the bound given to the constructor if it is nonnegative. -/ lemma linear_map.mk_continuous_norm_le (f : E β†’β‚—[π•œ] F) {C : ℝ} (hC : 0 ≀ C) (h : βˆ€x, βˆ₯f xβˆ₯ ≀ C * βˆ₯xβˆ₯) : βˆ₯f.mk_continuous C hβˆ₯ ≀ C := continuous_linear_map.op_norm_le_bound _ hC h namespace continuous_linear_map /-- The norm of the tensor product of a scalar linear map and of an element of a normed space is the product of the norms. -/ @[simp] lemma norm_smul_right_apply (c : E β†’L[π•œ] π•œ) (f : F) : βˆ₯smul_right c fβˆ₯ = βˆ₯cβˆ₯ * βˆ₯fβˆ₯ := begin refine le_antisymm _ _, { apply op_norm_le_bound _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) (Ξ»x, _), calc βˆ₯(c x) β€’ fβˆ₯ = βˆ₯c xβˆ₯ * βˆ₯fβˆ₯ : norm_smul _ _ ... ≀ (βˆ₯cβˆ₯ * βˆ₯xβˆ₯) * βˆ₯fβˆ₯ : mul_le_mul_of_nonneg_right (le_op_norm _ _) (norm_nonneg _) ... = βˆ₯cβˆ₯ * βˆ₯fβˆ₯ * βˆ₯xβˆ₯ : by ring }, { by_cases h : βˆ₯fβˆ₯ = 0, { rw h, simp [norm_nonneg] }, { have : 0 < βˆ₯fβˆ₯ := lt_of_le_of_ne (norm_nonneg _) (ne.symm h), rw ← le_div_iff this, apply op_norm_le_bound _ (div_nonneg (norm_nonneg _) (norm_nonneg f)) (Ξ»x, _), rw [div_mul_eq_mul_div, le_div_iff this], calc βˆ₯c xβˆ₯ * βˆ₯fβˆ₯ = βˆ₯c x β€’ fβˆ₯ : (norm_smul _ _).symm ... = βˆ₯((smul_right c f) : E β†’ F) xβˆ₯ : rfl ... ≀ βˆ₯smul_right c fβˆ₯ * βˆ₯xβˆ₯ : le_op_norm _ _ } }, end /-- Given `c : c : E β†’L[π•œ] π•œ`, `c.smul_rightL` is the continuous linear map from `F` to `E β†’L[π•œ] F` sending `f` to `Ξ» e, c e β€’ f`. -/ def smul_rightL (c : E β†’L[π•œ] π•œ) : F β†’L[π•œ] (E β†’L[π•œ] F) := (c.smul_rightβ‚— : F β†’β‚—[π•œ] (E β†’L[π•œ] F)).mk_continuous _ (Ξ» f, le_of_eq $ c.norm_smul_right_apply f) @[simp] lemma norm_smul_rightL_apply (c : E β†’L[π•œ] π•œ) (f : F) : βˆ₯c.smul_rightL fβˆ₯ = βˆ₯cβˆ₯ * βˆ₯fβˆ₯ := by simp [continuous_linear_map.smul_rightL, continuous_linear_map.smul_rightβ‚—] @[simp] lemma norm_smul_rightL (c : E β†’L[π•œ] π•œ) [nontrivial F] : βˆ₯(c.smul_rightL : F β†’L[π•œ] (E β†’L[π•œ] F))βˆ₯ = βˆ₯cβˆ₯ := continuous_linear_map.homothety_norm _ c.norm_smul_right_apply variables (π•œ F) /-- The linear map obtained by applying a continuous linear map at a given vector. -/ def applyβ‚— (v : E) : (E β†’L[π•œ] F) β†’β‚—[π•œ] F := { to_fun := Ξ» f, f v, map_add' := Ξ» f g, f.add_apply g v, map_smul' := Ξ» x f, f.smul_apply x v } lemma continuous_applyβ‚— (v : E) : continuous (continuous_linear_map.applyβ‚— π•œ F v) := begin apply (continuous_linear_map.applyβ‚— π•œ F v).continuous_of_bound, intro f, rw mul_comm, exact f.le_op_norm v, end /-- The continuous linear map obtained by applying a continuous linear map at a given vector. -/ def apply (v : E) : (E β†’L[π•œ] F) β†’L[π•œ] F := ⟨continuous_linear_map.applyβ‚— π•œ F v, continuous_linear_map.continuous_applyβ‚— _ _ _⟩ variables {π•œ F} section multiplication_linear variables (π•œ) (π•œ' : Type*) [normed_ring π•œ'] [normed_algebra π•œ π•œ'] /-- Left-multiplication in a normed algebra, considered as a continuous linear map. -/ def lmul_left : π•œ' β†’ (π•œ' β†’L[π•œ] π•œ') := Ξ» x, (algebra.lmul_left π•œ x).mk_continuous βˆ₯xβˆ₯ (Ξ» y, by {rw algebra.lmul_left_apply, exact norm_mul_le x y}) /-- Right-multiplication in a normed algebra, considered as a continuous linear map. -/ def lmul_right : π•œ' β†’ (π•œ' β†’L[π•œ] π•œ') := Ξ» x, (algebra.lmul_right π•œ x).mk_continuous βˆ₯xβˆ₯ (Ξ» y, by {rw [algebra.lmul_right_apply, mul_comm], exact norm_mul_le y x}) /-- Simultaneous left- and right-multiplication in a normed algebra, considered as a continuous linear map. -/ def lmul_left_right (vw : π•œ' Γ— π•œ') : π•œ' β†’L[π•œ] π•œ' := (lmul_right π•œ π•œ' vw.2).comp (lmul_left π•œ π•œ' vw.1) @[simp] lemma lmul_left_apply (x y : π•œ') : lmul_left π•œ π•œ' x y = x * y := rfl @[simp] lemma lmul_right_apply (x y : π•œ') : lmul_right π•œ π•œ' x y = y * x := rfl @[simp] lemma lmul_left_right_apply (vw : π•œ' Γ— π•œ') (x : π•œ') : lmul_left_right π•œ π•œ' vw x = vw.1 * x * vw.2 := rfl end multiplication_linear section restrict_scalars variable (π•œ) variables {π•œ' : Type*} [normed_field π•œ'] [normed_algebra π•œ π•œ'] variables {E' : Type*} [normed_group E'] [normed_space π•œ E'] [normed_space π•œ' E'] variables [is_scalar_tower π•œ π•œ' E'] variables {F' : Type*} [normed_group F'] [normed_space π•œ F'] [normed_space π•œ' F'] variables [is_scalar_tower π•œ π•œ' F'] /-- `π•œ`-linear continuous function induced by a `π•œ'`-linear continuous function when `π•œ'` is a normed algebra over `π•œ`. -/ def restrict_scalars (f : E' β†’L[π•œ'] F') : E' β†’L[π•œ] F' := { cont := f.cont, ..linear_map.restrict_scalars π•œ (f.to_linear_map) } @[simp, norm_cast] lemma restrict_scalars_coe_eq_coe (f : E' β†’L[π•œ'] F') : (f.restrict_scalars π•œ : E' β†’β‚—[π•œ] F') = (f : E' β†’β‚—[π•œ'] F').restrict_scalars π•œ := rfl @[simp, norm_cast squash] lemma restrict_scalars_coe_eq_coe' (f : E' β†’L[π•œ'] F') : (f.restrict_scalars π•œ : E' β†’ F') = f := rfl end restrict_scalars section extend_scalars variables {π•œ' : Type*} [normed_field π•œ'] [normed_algebra π•œ π•œ'] variables {F' : Type*} [normed_group F'] [normed_space π•œ F'] [normed_space π•œ' F'] variables [is_scalar_tower π•œ π•œ' F'] instance has_scalar_extend_scalars : has_scalar π•œ' (E β†’L[π•œ] F') := { smul := Ξ» c f, (c β€’ f.to_linear_map).mk_continuous (βˆ₯cβˆ₯ * βˆ₯fβˆ₯) begin assume x, calc βˆ₯c β€’ (f x)βˆ₯ = βˆ₯cβˆ₯ * βˆ₯f xβˆ₯ : norm_smul c _ ... ≀ βˆ₯cβˆ₯ * (βˆ₯fβˆ₯ * βˆ₯xβˆ₯) : mul_le_mul_of_nonneg_left (le_op_norm f x) (norm_nonneg _) ... = βˆ₯cβˆ₯ * βˆ₯fβˆ₯ * βˆ₯xβˆ₯ : (mul_assoc _ _ _).symm end } instance module_extend_scalars : module π•œ' (E β†’L[π•œ] F') := { smul_zero := Ξ» _, ext $ Ξ» _, smul_zero _, zero_smul := Ξ» _, ext $ Ξ» _, zero_smul _ _, one_smul := Ξ» _, ext $ Ξ» _, one_smul _ _, mul_smul := Ξ» _ _ _, ext $ Ξ» _, mul_smul _ _ _, add_smul := Ξ» _ _ _, ext $ Ξ» _, add_smul _ _ _, smul_add := Ξ» _ _ _, ext $ Ξ» _, smul_add _ _ _ } instance normed_space_extend_scalars : normed_space π•œ' (E β†’L[π•œ] F') := { norm_smul_le := Ξ» c f, linear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _ } /-- When `f` is a continuous linear map taking values in `S`, then `Ξ»b, f b β€’ x` is a continuous linear map. -/ def smul_algebra_right (f : E β†’L[π•œ] π•œ') (x : F') : E β†’L[π•œ] F' := { cont := by continuity!, .. f.to_linear_map.smul_algebra_right x } @[simp] theorem smul_algebra_right_apply (f : E β†’L[π•œ] π•œ') (x : F') (c : E) : smul_algebra_right f x c = f c β€’ x := rfl end extend_scalars end continuous_linear_map section has_sum -- Results in this section hold for continuous additive monoid homomorphisms or equivalences but we -- don't have bundled continuous additive homomorphisms. variables {ΞΉ R M Mβ‚‚ : Type*} [semiring R] [add_comm_monoid M] [semimodule R M] [add_comm_monoid Mβ‚‚] [semimodule R Mβ‚‚] [topological_space M] [topological_space Mβ‚‚] omit π•œ /-- Applying a continuous linear map commutes with taking an (infinite) sum. -/ protected lemma continuous_linear_map.has_sum {f : ΞΉ β†’ M} (Ο† : M β†’L[R] Mβ‚‚) {x : M} (hf : has_sum f x) : has_sum (Ξ» (b:ΞΉ), Ο† (f b)) (Ο† x) := by simpa only using hf.map Ο†.to_linear_map.to_add_monoid_hom Ο†.continuous alias continuous_linear_map.has_sum ← has_sum.mapL protected lemma continuous_linear_map.summable {f : ΞΉ β†’ M} (Ο† : M β†’L[R] Mβ‚‚) (hf : summable f) : summable (Ξ» b:ΞΉ, Ο† (f b)) := (hf.has_sum.mapL Ο†).summable alias continuous_linear_map.summable ← summable.mapL /-- Applying a continuous linear map commutes with taking an (infinite) sum. -/ protected lemma continuous_linear_equiv.has_sum {f : ΞΉ β†’ M} (e : M ≃L[R] Mβ‚‚) {y : Mβ‚‚} : has_sum (Ξ» (b:ΞΉ), e (f b)) y ↔ has_sum f (e.symm y) := ⟨λ h, by simpa only [e.symm.coe_coe, e.symm_apply_apply] using h.mapL (e.symm : Mβ‚‚ β†’L[R] M), Ξ» h, by simpa only [e.coe_coe, e.apply_symm_apply] using (e : M β†’L[R] Mβ‚‚).has_sum h⟩ protected lemma continuous_linear_equiv.summable {f : ΞΉ β†’ M} (e : M ≃L[R] Mβ‚‚) : summable (Ξ» b:ΞΉ, e (f b)) ↔ summable f := ⟨λ hf, (e.has_sum.1 hf.has_sum).summable, (e : M β†’L[R] Mβ‚‚).summable⟩ end has_sum namespace continuous_linear_equiv variable (e : E ≃L[π•œ] F) protected lemma lipschitz : lipschitz_with (nnnorm (e : E β†’L[π•œ] F)) e := (e : E β†’L[π•œ] F).lipschitz protected lemma antilipschitz : antilipschitz_with (nnnorm (e.symm : F β†’L[π•œ] E)) e := e.symm.lipschitz.to_right_inverse e.left_inv theorem is_O_comp {Ξ± : Type*} (f : Ξ± β†’ E) (l : filter Ξ±) : asymptotics.is_O (Ξ» x', e (f x')) f l := (e : E β†’L[π•œ] F).is_O_comp f l theorem is_O_sub (l : filter E) (x : E) : asymptotics.is_O (Ξ» x', e (x' - x)) (Ξ» x', x' - x) l := (e : E β†’L[π•œ] F).is_O_sub l x theorem is_O_comp_rev {Ξ± : Type*} (f : Ξ± β†’ E) (l : filter Ξ±) : asymptotics.is_O f (Ξ» x', e (f x')) l := (e.symm.is_O_comp _ l).congr_left $ Ξ» _, e.symm_apply_apply _ theorem is_O_sub_rev (l : filter E) (x : E) : asymptotics.is_O (Ξ» x', x' - x) (Ξ» x', e (x' - x)) l := e.is_O_comp_rev _ _ /-- A continuous linear equiv is a uniform embedding. -/ lemma uniform_embedding : uniform_embedding e := e.antilipschitz.uniform_embedding e.lipschitz.uniform_continuous lemma one_le_norm_mul_norm_symm [nontrivial E] : 1 ≀ βˆ₯(e : E β†’L[π•œ] F)βˆ₯ * βˆ₯(e.symm : F β†’L[π•œ] E)βˆ₯ := begin rw [mul_comm], convert (e.symm : F β†’L[π•œ] E).op_norm_comp_le (e : E β†’L[π•œ] F), rw [e.coe_symm_comp_coe, continuous_linear_map.norm_id] end lemma norm_pos [nontrivial E] : 0 < βˆ₯(e : E β†’L[π•œ] F)βˆ₯ := pos_of_mul_pos_right (lt_of_lt_of_le zero_lt_one e.one_le_norm_mul_norm_symm) (norm_nonneg _) lemma norm_symm_pos [nontrivial E] : 0 < βˆ₯(e.symm : F β†’L[π•œ] E)βˆ₯ := pos_of_mul_pos_left (lt_of_lt_of_le zero_lt_one e.one_le_norm_mul_norm_symm) (norm_nonneg _) lemma subsingleton_or_norm_symm_pos : subsingleton E ∨ 0 < βˆ₯(e.symm : F β†’L[π•œ] E)βˆ₯ := begin rcases subsingleton_or_nontrivial E with _i|_i; resetI, { left, apply_instance }, { right, exact e.norm_symm_pos } end lemma subsingleton_or_nnnorm_symm_pos : subsingleton E ∨ 0 < (nnnorm $ (e.symm : F β†’L[π•œ] E)) := subsingleton_or_norm_symm_pos e lemma homothety_inverse (a : ℝ) (ha : 0 < a) (f : E ≃ₗ[π•œ] F) : (βˆ€ (x : E), βˆ₯f xβˆ₯ = a * βˆ₯xβˆ₯) β†’ (βˆ€ (y : F), βˆ₯f.symm yβˆ₯ = a⁻¹ * βˆ₯yβˆ₯) := begin intros hf y, calc βˆ₯(f.symm) yβˆ₯ = a⁻¹ * (a * βˆ₯ (f.symm) yβˆ₯) : _ ... = a⁻¹ * βˆ₯f ((f.symm) y)βˆ₯ : by rw hf ... = a⁻¹ * βˆ₯yβˆ₯ : by simp, rw [← mul_assoc, inv_mul_cancel (ne_of_lt ha).symm, one_mul], end variable (π•œ) /-- A linear equivalence which is a homothety is a continuous linear equivalence. -/ def of_homothety (f : E ≃ₗ[π•œ] F) (a : ℝ) (ha : 0 < a) (hf : βˆ€x, βˆ₯f xβˆ₯ = a * βˆ₯xβˆ₯) : E ≃L[π•œ] F := { to_linear_equiv := f, continuous_to_fun := f.to_linear_map.continuous_of_bound a (Ξ» x, le_of_eq (hf x)), continuous_inv_fun := f.symm.to_linear_map.continuous_of_bound a⁻¹ (Ξ» x, le_of_eq (homothety_inverse a ha f hf x)) } lemma to_span_nonzero_singleton_homothety (x : E) (h : x β‰  0) (c : π•œ) : βˆ₯linear_equiv.to_span_nonzero_singleton π•œ E x h cβˆ₯ = βˆ₯xβˆ₯ * βˆ₯cβˆ₯ := continuous_linear_map.to_span_singleton_homothety _ _ _ /-- Given a nonzero element `x` of a normed space `E` over a field `π•œ`, the natural continuous linear equivalence from `E` to the span of `x`.-/ def to_span_nonzero_singleton (x : E) (h : x β‰  0) : π•œ ≃L[π•œ] (submodule.span π•œ ({x} : set E)) := of_homothety π•œ (linear_equiv.to_span_nonzero_singleton π•œ E x h) βˆ₯xβˆ₯ (norm_pos_iff.mpr h) (to_span_nonzero_singleton_homothety π•œ x h) /-- Given a nonzero element `x` of a normed space `E` over a field `π•œ`, the natural continuous linear map from the span of `x` to `π•œ`.-/ abbreviation coord (x : E) (h : x β‰  0) : (submodule.span π•œ ({x} : set E)) β†’L[π•œ] π•œ := (to_span_nonzero_singleton π•œ x h).symm lemma coord_norm (x : E) (h : x β‰  0) : βˆ₯coord π•œ x hβˆ₯ = βˆ₯xβˆ₯⁻¹ := begin have hx : 0 < βˆ₯xβˆ₯ := (norm_pos_iff.mpr h), haveI : nontrivial (submodule.span π•œ ({x} : set E)) := submodule.nontrivial_span_singleton h, exact continuous_linear_map.homothety_norm _ (Ξ» y, homothety_inverse _ hx _ (to_span_nonzero_singleton_homothety π•œ x h) _) end lemma coord_self (x : E) (h : x β‰  0) : (coord π•œ x h) (⟨x, submodule.mem_span_singleton_self x⟩ : submodule.span π•œ ({x} : set E)) = 1 := linear_equiv.coord_self π•œ E x h end continuous_linear_equiv lemma linear_equiv.uniform_embedding (e : E ≃ₗ[π•œ] F) (h₁ : continuous e) (hβ‚‚ : continuous e.symm) : uniform_embedding e := continuous_linear_equiv.uniform_embedding { continuous_to_fun := h₁, continuous_inv_fun := hβ‚‚, .. e } /-- Construct a continuous linear equivalence from a linear equivalence together with bounds in both directions. -/ def linear_equiv.to_continuous_linear_equiv_of_bounds (e : E ≃ₗ[π•œ] F) (C_to C_inv : ℝ) (h_to : βˆ€ x, βˆ₯e xβˆ₯ ≀ C_to * βˆ₯xβˆ₯) (h_inv : βˆ€ x : F, βˆ₯e.symm xβˆ₯ ≀ C_inv * βˆ₯xβˆ₯) : E ≃L[π•œ] F := { to_linear_equiv := e, continuous_to_fun := e.to_linear_map.continuous_of_bound C_to h_to, continuous_inv_fun := e.symm.to_linear_map.continuous_of_bound C_inv h_inv } namespace continuous_linear_map variables (π•œ) (π•œ' : Type*) [normed_ring π•œ'] [normed_algebra π•œ π•œ'] @[simp] lemma lmul_left_norm (v : π•œ') : βˆ₯lmul_left π•œ π•œ' vβˆ₯ = βˆ₯vβˆ₯ := begin refine le_antisymm _ _, { exact linear_map.mk_continuous_norm_le _ (norm_nonneg v) _ }, { simpa [@normed_algebra.norm_one π•œ _ π•œ' _ _] using le_op_norm (lmul_left π•œ π•œ' v) (1:π•œ') } end @[simp] lemma lmul_right_norm (v : π•œ') : βˆ₯lmul_right π•œ π•œ' vβˆ₯ = βˆ₯vβˆ₯ := begin refine le_antisymm _ _, { exact linear_map.mk_continuous_norm_le _ (norm_nonneg v) _ }, { simpa [@normed_algebra.norm_one π•œ _ π•œ' _ _] using le_op_norm (lmul_right π•œ π•œ' v) (1:π•œ') } end lemma lmul_left_right_norm_le (vw : π•œ' Γ— π•œ') : βˆ₯lmul_left_right π•œ π•œ' vwβˆ₯ ≀ βˆ₯vw.1βˆ₯ * βˆ₯vw.2βˆ₯ := by simpa [mul_comm] using op_norm_comp_le (lmul_right π•œ π•œ' vw.2) (lmul_left π•œ π•œ' vw.1) end continuous_linear_map
899884b8987969d3256067873f34deda0eea6584
c3f2fcd060adfa2ca29f924839d2d925e8f2c685
/hott/init/wf.hlean
79ae373f5c2d3f0bd8758033f8be39c2897012d6
[ "Apache-2.0" ]
permissive
respu/lean
6582d19a2f2838a28ecd2b3c6f81c32d07b5341d
8c76419c60b63d0d9f7bc04ebb0b99812d0ec654
refs/heads/master
1,610,882,451,231
1,427,747,084,000
1,427,747,429,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,850
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: init.wf Author: Leonardo de Moura -/ prelude import init.relation init.tactic inductive acc.{l₁ lβ‚‚} {A : Type.{l₁}} (R : A β†’ A β†’ Type.{lβ‚‚}) : A β†’ Type.{max l₁ lβ‚‚} := intro : βˆ€x, (βˆ€ y, R y x β†’ acc R y) β†’ acc R x namespace acc variables {A : Type} {R : A β†’ A β†’ Type} definition inv {x y : A} (H₁ : acc R x) (Hβ‚‚ : R y x) : acc R y := acc.rec_on H₁ (Ξ» x₁ ac₁ iH Hβ‚‚, ac₁ y Hβ‚‚) Hβ‚‚ end acc inductive well_founded [class] {A : Type} (R : A β†’ A β†’ Type) : Type := intro : (βˆ€ a, acc R a) β†’ well_founded R namespace well_founded definition apply [coercion] {A : Type} {R : A β†’ A β†’ Type} (wf : well_founded R) : βˆ€a, acc R a := take a, well_founded.rec_on wf (Ξ»p, p) a context parameters {A : Type} {R : A β†’ A β†’ Type} infix `β‰Ί`:50 := R hypothesis [Hwf : well_founded R] definition recursion {C : A β†’ Type} (a : A) (H : Ξ x, (Ξ y, y β‰Ί x β†’ C y) β†’ C x) : C a := acc.rec_on (Hwf a) (Ξ» x₁ ac₁ iH, H x₁ iH) definition induction {C : A β†’ Type} (a : A) (H : βˆ€x, (βˆ€y, y β‰Ί x β†’ C y) β†’ C x) : C a := recursion a H parameter {C : A β†’ Type} parameter F : Ξ x, (Ξ y, y β‰Ί x β†’ C y) β†’ C x definition fix_F (x : A) (a : acc R x) : C x := acc.rec_on a (Ξ» x₁ ac₁ iH, F x₁ iH) definition fix_F_eq (x : A) (r : acc R x) : fix_F x r = F x (Ξ» (y : A) (p : y β‰Ί x), fix_F y (acc.inv r p)) := acc.rec_on r (Ξ» x H ih, rfl) -- Remark: after we prove function extensionality from univalence, we can drop this hypothesis hypothesis F_ext : Ξ  (x : A) (f g : Ξ  y, y β‰Ί x β†’ C y), (Ξ  (y : A) (p : y β‰Ί x), f y p = g y p) β†’ F x f = F x g lemma fix_F_inv (x : A) (r : acc R x) : Ξ  (s : acc R x), fix_F x r = fix_F x s := acc.rec_on r (Ξ» (x₁ : A) (h₁ : Ξ  y, y β‰Ί x₁ β†’ acc R y) (ih₁ : Ξ  y (hlt : y β‰Ί x₁) (s : acc R y), fix_F y (h₁ y hlt) = fix_F y s) (s : acc R x₁), have aux₁ : Ξ  (s : acc R x₁) (h₁ : Ξ  y, y β‰Ί x₁ β†’ acc R y) (ih₁ : Ξ  y (hlt : y β‰Ί x₁) (s : acc R y), fix_F y (h₁ y hlt) = fix_F y s), fix_F x₁ (acc.intro x₁ h₁) = fix_F x₁ s, from Ξ» s, acc.rec_on s (Ξ» (xβ‚‚ : A) (hβ‚‚ : Ξ  y, y β‰Ί xβ‚‚ β†’ acc R y) (ihβ‚‚ : _) (h₁ : Ξ  y, y β‰Ί xβ‚‚ β†’ acc R y) (ih₁ : Ξ  y (hlt : y β‰Ί xβ‚‚) (s : acc R y), fix_F y (h₁ y hlt) = fix_F y s), calc fix_F xβ‚‚ (acc.intro xβ‚‚ h₁) = F xβ‚‚ (Ξ» (y : A) (p : y β‰Ί xβ‚‚), fix_F y (h₁ y p)) : rfl ... = F xβ‚‚ (Ξ» (y : A) (p : y β‰Ί xβ‚‚), fix_F y (hβ‚‚ y p)) : F_ext xβ‚‚ _ _ (Ξ» (y : A) (p : y β‰Ί xβ‚‚), ih₁ y p (hβ‚‚ y p)) ... = fix_F xβ‚‚ (acc.intro xβ‚‚ hβ‚‚) : rfl), show fix_F x₁ (acc.intro x₁ h₁) = fix_F x₁ s, from aux₁ s h₁ ih₁) -- Well-founded fixpoint definition fix (x : A) : C x := fix_F x (Hwf x) -- Well-founded fixpoint satisfies fixpoint equation definition fix_eq (x : A) : fix x = F x (Ξ»y h, fix y) := calc fix x = fix_F x (Hwf x) : rfl ... = F x (Ξ»y h, fix_F y (acc.inv (Hwf x) h)) : fix_F_eq x (Hwf x) ... = F x (Ξ»y h, fix_F y (Hwf y)) : F_ext x _ _ (Ξ» y h, fix_F_inv y _ _) ... = F x (Ξ»y h, fix y) : rfl end end well_founded open well_founded -- Empty relation is well-founded definition empty.wf {A : Type} : well_founded empty_relation := well_founded.intro (Ξ» (a : A), acc.intro a (Ξ» (b : A) (lt : empty), empty.rec _ lt)) -- Subrelation of a well-founded relation is well-founded namespace subrelation context parameters {A : Type} {R Q : A β†’ A β†’ Type} parameters (H₁ : subrelation Q R) parameters (Hβ‚‚ : well_founded R) definition accessible {a : A} (ac : acc R a) : acc Q a := acc.rec_on ac (Ξ» (x : A) (ax : _) (iH : βˆ€ (y : A), R y x β†’ acc Q y), acc.intro x (Ξ» (y : A) (lt : Q y x), iH y (H₁ lt))) definition wf : well_founded Q := well_founded.intro (Ξ» a, accessible (Hβ‚‚ a)) end end subrelation -- The inverse image of a well-founded relation is well-founded namespace inv_image context parameters {A B : Type} {R : B β†’ B β†’ Type} parameters (f : A β†’ B) parameters (H : well_founded R) definition accessible {a : A} (ac : acc R (f a)) : acc (inv_image R f) a := have gen : βˆ€x, f x = f a β†’ acc (inv_image R f) x, from acc.rec_on ac (Ξ»x acx (iH : βˆ€y, R y x β†’ (βˆ€z, f z = y β†’ acc (inv_image R f) z)) (z : A) (eq₁ : f z = x), acc.intro z (Ξ» (y : A) (lt : R (f y) (f z)), iH (f y) (eq.rec_on eq₁ lt) y rfl)), gen a rfl definition wf : well_founded (inv_image R f) := well_founded.intro (Ξ» a, accessible (H (f a))) end end inv_image -- The transitive closure of a well-founded relation is well-founded namespace tc context parameters {A : Type} {R : A β†’ A β†’ Type} notation `R⁺` := tc R definition accessible {z} (ac: acc R z) : acc R⁺ z := acc.rec_on ac (Ξ» x acx (iH : βˆ€y, R y x β†’ acc R⁺ y), acc.intro x (Ξ» (y : A) (lt : R⁺ y x), have gen : x = x β†’ acc R⁺ y, from tc.rec_on lt (Ξ»a b (H : R a b) (Heq : b = x), iH a (eq.rec_on Heq H)) (Ξ»a b c (H₁ : R⁺ a b) (Hβ‚‚ : R⁺ b c) (iH₁ : b = x β†’ acc R⁺ a) (iHβ‚‚ : c = x β†’ acc R⁺ b) (Heq : c = x), acc.inv (iHβ‚‚ Heq) H₁), gen rfl)) definition wf (H : well_founded R) : well_founded R⁺ := well_founded.intro (Ξ» a, accessible (H a)) end end tc
da961dfcd66c378aa1bf8091555d9135ad03a3f9
367134ba5a65885e863bdc4507601606690974c1
/src/measure_theory/ae_eq_fun_metric.lean
a656d28e273f513d03589e0711d83c53aa82ed13
[ "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
4,325
lean
/- Copyright (c) 2019 Johannes HΓΆlzl, Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Zhouhang Zhou -/ import measure_theory.ae_eq_fun /-! # Emetric space structure on almost everywhere equal functions Emetric on `L⁰` : If `Ξ²` is an `emetric_space`, then `L⁰` can be made into an `emetric_space`, where `edist [f] [g]` is defined to be `∫⁻ a, edist (f a) (g a)`. The integral used here is `lintegral : (Ξ± β†’ ℝβ‰₯0∞) β†’ ℝβ‰₯0∞`, which is defined in the file `integration.lean`. See `edist_mk_mk` and `edist_to_fun`. TODO: remove this file, and use instead the more general `Lp` space specialized to `p = 1`. -/ noncomputable theory open_locale classical ennreal open set filter topological_space ennreal emetric measure_theory function variables {Ξ± Ξ² Ξ³ Ξ΄ : Type*} [measurable_space Ξ±] {ΞΌ Ξ½ : measure Ξ±} namespace measure_theory namespace ae_eq_fun variables [measurable_space Ξ²] [measurable_space Ξ³] [measurable_space Ξ΄] section variables [emetric_space Ξ³] [second_countable_topology Ξ³] [opens_measurable_space Ξ³] /-- `comp_edist [f] [g] a` will return `edist (f a) (g a)` -/ protected def edist (f g : Ξ± β†’β‚˜[ΞΌ] Ξ³) : Ξ± β†’β‚˜[ΞΌ] ℝβ‰₯0∞ := compβ‚‚ edist measurable_edist f g protected lemma edist_comm (f g : Ξ± β†’β‚˜[ΞΌ] Ξ³) : f.edist g = g.edist f := induction_onβ‚‚ f g $ Ξ» f hf g hg, mk_eq_mk.2 $ eventually_of_forall $ Ξ» x, edist_comm (f x) (g x) lemma coe_fn_edist (f g : Ξ± β†’β‚˜[ΞΌ] Ξ³) : ⇑(f.edist g) =ᡐ[ΞΌ] Ξ» a, edist (f a) (g a) := coe_fn_compβ‚‚ _ _ _ _ protected lemma edist_self (f : Ξ± β†’β‚˜[ΞΌ] Ξ³) : f.edist f = 0 := induction_on f $ Ξ» f hf, mk_eq_mk.2 $ eventually_of_forall $ Ξ» x, edist_self (f x) /-- Almost everywhere equal functions form an `emetric_space`, with the emetric defined as `edist f g = ∫⁻ a, edist (f a) (g a)`. -/ instance : emetric_space (Ξ± β†’β‚˜[ΞΌ] Ξ³) := { edist := Ξ»f g, lintegral (f.edist g), edist_self := assume f, lintegral_eq_zero_iff.2 f.edist_self, edist_comm := Ξ» f g, congr_arg lintegral $ f.edist_comm g, edist_triangle := Ξ» f g h, induction_on₃ f g h $ Ξ» f hf g hg h hh, calc ∫⁻ a, edist (f a) (h a) βˆ‚ΞΌ ≀ ∫⁻ a, edist (f a) (g a) + edist (g a) (h a) βˆ‚ΞΌ : measure_theory.lintegral_mono (Ξ» a, edist_triangle (f a) (g a) (h a)) ... = ∫⁻ a, edist (f a) (g a) βˆ‚ΞΌ + ∫⁻ a, edist (g a) (h a) βˆ‚ΞΌ : lintegral_add' (hf.edist hg) (hg.edist hh), eq_of_edist_eq_zero := Ξ» f g, induction_onβ‚‚ f g $ Ξ» f hf g hg H, mk_eq_mk.2 $ ((lintegral_eq_zero_iff' (hf.edist hg)).1 H).mono $ Ξ» x, eq_of_edist_eq_zero } lemma edist_mk_mk {f g : Ξ± β†’ Ξ³} (hf hg) : edist (mk f hf : Ξ± β†’β‚˜[ΞΌ] Ξ³) (mk g hg) = ∫⁻ x, edist (f x) (g x) βˆ‚ΞΌ := rfl lemma edist_eq_coe (f g : Ξ± β†’β‚˜[ΞΌ] Ξ³) : edist f g = ∫⁻ x, edist (f x) (g x) βˆ‚ΞΌ := by rw [← edist_mk_mk, mk_coe_fn, mk_coe_fn] lemma edist_zero_eq_coe [has_zero Ξ³] (f : Ξ± β†’β‚˜[ΞΌ] Ξ³) : edist f 0 = ∫⁻ x, edist (f x) 0 βˆ‚ΞΌ := by rw [← edist_mk_mk, mk_coe_fn, zero_def] end section metric variables [metric_space Ξ³] [second_countable_topology Ξ³] [opens_measurable_space Ξ³] lemma edist_mk_mk' {f g : Ξ± β†’ Ξ³} (hf hg) : edist (mk f hf : Ξ± β†’β‚˜[ΞΌ] Ξ³) (mk g hg) = ∫⁻ x, nndist (f x) (g x) βˆ‚ΞΌ := by simp only [edist_mk_mk, edist_nndist] lemma edist_eq_coe' (f g : Ξ± β†’β‚˜[ΞΌ] Ξ³) : edist f g = ∫⁻ x, nndist (f x) (g x) βˆ‚ΞΌ := by simp only [edist_eq_coe, edist_nndist] end metric lemma edist_add_right [normed_group Ξ³] [second_countable_topology Ξ³] [borel_space Ξ³] (f g h : Ξ± β†’β‚˜[ΞΌ] Ξ³) : edist (f + h) (g + h) = edist f g := induction_on₃ f g h $ Ξ» f hf g hg h hh, by simp [edist_mk_mk, edist_dist, dist_add_right] section normed_space variables {π•œ : Type*} [normed_field π•œ] variables [normed_group Ξ³] [second_countable_topology Ξ³] [normed_space π•œ Ξ³] [borel_space Ξ³] lemma edist_smul (c : π•œ) (f : Ξ± β†’β‚˜[ΞΌ] Ξ³) : edist (c β€’ f) 0 = (ennreal.of_real βˆ₯cβˆ₯) * edist f 0 := induction_on f $ Ξ» f hf, by simp [edist_mk_mk, zero_def, smul_mk, edist_dist, norm_smul, ennreal.of_real_mul, lintegral_const_mul'] end normed_space end ae_eq_fun end measure_theory
b7c36ff43e90e023293086212ff43dfa0c02277d
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebraic_topology/dold_kan/p_infty.lean
7b1c539dca8bfc6ba0e623e9e88484f51f9ea26c
[ "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,984
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.projections import category_theory.idempotents.functor_categories import category_theory.idempotents.functor_extension /-! # Construction of the projection `P_infty` for the Dold-Kan correspondence > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. TODO (@joelriou) continue adding the various files referenced below In this file, we construct the projection `P_infty : K[X] ⟢ K[X]` by passing to the limit the projections `P q` defined in `projections.lean`. This projection is a critical tool in this formalisation of the Dold-Kan correspondence, because in the case of abelian categories, `P_infty` corresponds to the projection on the normalized Moore subcomplex, with kernel the degenerate subcomplex. (See `equivalence.lean` for the general strategy of proof of the Dold-Kan equivalence.) -/ open category_theory open category_theory.category open category_theory.preadditive open category_theory.simplicial_object open category_theory.idempotents open opposite open_locale simplicial dold_kan noncomputable theory namespace algebraic_topology namespace dold_kan variables {C : Type*} [category C] [preadditive C] {X : simplicial_object C} lemma P_is_eventually_constant {q n : β„•} (hqn : n ≀ q) : ((P (q+1)).f n : X _[n] ⟢ _ ) = (P q).f n := begin cases n, { simp only [P_f_0_eq], }, { unfold P, simp only [add_right_eq_self, comp_add, homological_complex.comp_f, homological_complex.add_f_apply, comp_id], exact (higher_faces_vanish.of_P q n).comp_HΟƒ_eq_zero (nat.succ_le_iff.mp hqn), }, end lemma Q_is_eventually_constant {q n : β„•} (hqn : n ≀ q) : ((Q (q+1)).f n : X _[n] ⟢ _ ) = (Q q).f n := by simp only [Q, homological_complex.sub_f_apply, P_is_eventually_constant hqn] /-- The endomorphism `P_infty : K[X] ⟢ K[X]` obtained from the `P q` by passing to the limit. -/ def P_infty : K[X] ⟢ K[X] := chain_complex.of_hom _ _ _ _ _ _ (Ξ» n, ((P n).f n : X _[n] ⟢ _ )) (Ξ» n, by simpa only [← P_is_eventually_constant (show n ≀ n, by refl), alternating_face_map_complex.obj_d_eq] using (P (n+1)).comm (n+1) n) /-- The endomorphism `Q_infty : K[X] ⟢ K[X]` obtained from the `Q q` by passing to the limit. -/ def Q_infty : K[X] ⟢ K[X] := πŸ™ _ - P_infty @[simp] lemma P_infty_f_0 : (P_infty.f 0 : X _[0] ⟢ X _[0]) = πŸ™ _ := rfl lemma P_infty_f (n : β„•) : (P_infty.f n : X _[n] ⟢ X _[n] ) = (P n).f n := rfl @[simp] lemma Q_infty_f_0 : (Q_infty.f 0 : X _[0] ⟢ X _[0]) = 0 := by { dsimp [Q_infty], simp only [sub_self], } lemma Q_infty_f (n : β„•) : (Q_infty.f n : X _[n] ⟢ X _[n] ) = (Q n).f n := rfl @[simp, reassoc] lemma P_infty_f_naturality (n : β„•) {X Y : simplicial_object C} (f : X ⟢ Y) : f.app (op [n]) ≫ P_infty.f n = P_infty.f n ≫ f.app (op [n]) := P_f_naturality n n f @[simp, reassoc] lemma Q_infty_f_naturality (n : β„•) {X Y : simplicial_object C} (f : X ⟢ Y) : f.app (op [n]) ≫ Q_infty.f n = Q_infty.f n ≫ f.app (op [n]) := Q_f_naturality n n f @[simp, reassoc] lemma P_infty_f_idem (n : β„•) : (P_infty.f n : X _[n] ⟢ _) ≫ (P_infty.f n) = P_infty.f n := by simp only [P_infty_f, P_f_idem] @[simp, reassoc] lemma P_infty_idem : (P_infty : K[X] ⟢ _) ≫ P_infty = P_infty := by { ext n, exact P_infty_f_idem n, } @[simp, reassoc] lemma Q_infty_f_idem (n : β„•) : (Q_infty.f n : X _[n] ⟢ _) ≫ (Q_infty.f n) = Q_infty.f n := Q_f_idem _ _ @[simp, reassoc] lemma Q_infty_idem : (Q_infty : K[X] ⟢ _) ≫ Q_infty = Q_infty := by { ext n, exact Q_infty_f_idem n, } @[simp, reassoc] lemma P_infty_f_comp_Q_infty_f (n : β„•) : (P_infty.f n : X _[n] ⟢ _) ≫ Q_infty.f n = 0 := begin dsimp only [Q_infty], simp only [homological_complex.sub_f_apply, homological_complex.id_f, comp_sub, comp_id, P_infty_f_idem, sub_self], end @[simp, reassoc] lemma P_infty_comp_Q_infty : (P_infty : K[X] ⟢ _) ≫ Q_infty = 0 := by { ext n, apply P_infty_f_comp_Q_infty_f, } @[simp, reassoc] lemma Q_infty_f_comp_P_infty_f (n : β„•) : (Q_infty.f n : X _[n] ⟢ _) ≫ P_infty.f n = 0 := begin dsimp only [Q_infty], simp only [homological_complex.sub_f_apply, homological_complex.id_f, sub_comp, id_comp, P_infty_f_idem, sub_self], end @[simp, reassoc] lemma Q_infty_comp_P_infty : (Q_infty : K[X] ⟢ _) ≫ P_infty = 0 := by { ext n, apply Q_infty_f_comp_P_infty_f, } @[simp] lemma P_infty_add_Q_infty : (P_infty : K[X] ⟢ _) + Q_infty = πŸ™ _ := by { dsimp only [Q_infty], simp only [add_sub_cancel'_right], } lemma P_infty_f_add_Q_infty_f (n : β„•) : (P_infty.f n : X _[n] ⟢ _ ) + Q_infty.f n = πŸ™ _ := homological_complex.congr_hom (P_infty_add_Q_infty) n variable (C) /-- `P_infty` induces a natural transformation, i.e. an endomorphism of the functor `alternating_face_map_complex C`. -/ @[simps] def nat_trans_P_infty : alternating_face_map_complex C ⟢ alternating_face_map_complex C := { app := Ξ» _, P_infty, naturality' := Ξ» X Y f, by { ext n, exact P_infty_f_naturality n f, }, } /-- The natural transformation in each degree that is induced by `nat_trans_P_infty`. -/ @[simps] def nat_trans_P_infty_f (n : β„•) := nat_trans_P_infty C β—« πŸ™ (homological_complex.eval _ _ n) variable {C} @[simp] lemma map_P_infty_f {D : Type*} [category D] [preadditive D] (G : C β₯€ D) [G.additive] (X : simplicial_object C) (n : β„•) : (P_infty : K[((whiskering C D).obj G).obj X] ⟢ _).f n = G.map ((P_infty : alternating_face_map_complex.obj X ⟢ _).f n) := by simp only [P_infty_f, map_P] /-- Given an object `Y : karoubi (simplicial_object C)`, this lemma computes `P_infty` for the associated object in `simplicial_object (karoubi C)` in terms of `P_infty` for `Y.X : simplicial_object C` and `Y.p`. -/ lemma karoubi_P_infty_f {Y : karoubi (simplicial_object C)} (n : β„•) : ((P_infty : K[(karoubi_functor_category_embedding _ _).obj Y] ⟢ _).f n).f = Y.p.app (op [n]) ≫ (P_infty : K[Y.X] ⟢ _).f n := begin -- We introduce P_infty endomorphisms P₁, Pβ‚‚, P₃, Pβ‚„ on various objects Y₁, Yβ‚‚, Y₃, Yβ‚„. let Y₁ := (karoubi_functor_category_embedding _ _).obj Y, let Yβ‚‚ := Y.X, let Y₃ := (((whiskering _ _).obj (to_karoubi C)).obj Y.X), let Yβ‚„ := (karoubi_functor_category_embedding _ _).obj ((to_karoubi _).obj Y.X), let P₁ : K[Y₁] ⟢ _ := P_infty, let Pβ‚‚ : K[Yβ‚‚] ⟢ _ := P_infty, let P₃ : K[Y₃] ⟢ _ := P_infty, let Pβ‚„ : K[Yβ‚„] ⟢ _ := P_infty, -- The statement of lemma relates P₁ and Pβ‚‚. change (P₁.f n).f = Y.p.app (op [n]) ≫ Pβ‚‚.f n, -- The proof proceeds by obtaining relations h₃₂, h₄₃, h₁₄. have h₃₂ : (P₃.f n).f = Pβ‚‚.f n := karoubi.hom_ext.mp (map_P_infty_f (to_karoubi C) Yβ‚‚ n), have h₄₃ : Pβ‚„.f n = P₃.f n, { have h := functor.congr_obj (to_karoubi_comp_karoubi_functor_category_embedding _ _) Yβ‚‚, simp only [← nat_trans_P_infty_f_app], congr', }, let τ₁ := πŸ™ (karoubi_functor_category_embedding (simplex_categoryα΅’α΅–) C), let Ο„β‚‚ := nat_trans_P_infty_f (karoubi C) n, let Ο„ := τ₁ β—« Ο„β‚‚, have h₁₄ := idempotents.nat_trans_eq Ο„ Y, dsimp [Ο„, τ₁, Ο„β‚‚, nat_trans_P_infty_f] at h₁₄, rw [id_comp, id_comp, comp_id, comp_id] at h₁₄, /- We use the three equalities h₃₂, h₄₃, h₁₄. -/ rw [← h₃₂, ← h₄₃, h₁₄], simp only [karoubi_functor_category_embedding.map_app_f, karoubi.decomp_id_p_f, karoubi.decomp_id_i_f, karoubi.comp_f], let Ο€ : Yβ‚„ ⟢ Yβ‚„ := (to_karoubi _ β‹™ karoubi_functor_category_embedding _ _).map Y.p, have eq := karoubi.hom_ext.mp (P_infty_f_naturality n Ο€), simp only [karoubi.comp_f] at eq, dsimp [Ο€] at eq, rw [← eq, reassoc_of (app_idem Y (op [n]))], end end dold_kan end algebraic_topology
b44f4a62279d4f09d63487c2843d1b054b365f17
64874bd1010548c7f5a6e3e8902efa63baaff785
/library/algebra/category/natural_transformation.lean
dc77986552a7edcb25cd42736278a240d987fcdb
[ "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
2,193
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: algebra.category.natural_transformation Author: Floris van Doorn -/ import .functor open category eq eq.ops functor inductive natural_transformation {C D : Category} (F G : C β‡’ D) : Type := mk : Ξ  (Ξ· : Ξ (a : C), hom (F a) (G a)), (Ξ {a b : C} (f : hom a b), G f ∘ Ξ· a = Ξ· b ∘ F f) β†’ natural_transformation F G infixl `⟹`:25 := natural_transformation -- \==> namespace natural_transformation variables {C D : Category} {F G H I : functor C D} definition natural_map [coercion] (Ξ· : F ⟹ G) : Ξ (a : C), F a ⟢ G a := rec (Ξ» x y, x) Ξ· theorem naturality (Ξ· : F ⟹ G) : Π⦃a b : C⦄ (f : a ⟢ b), G f ∘ Ξ· a = Ξ· b ∘ F f := rec (Ξ» x y, y) Ξ· protected definition compose (Ξ· : G ⟹ H) (ΞΈ : F ⟹ G) : F ⟹ H := natural_transformation.mk (Ξ» a, Ξ· a ∘ ΞΈ a) (Ξ» a b f, calc H f ∘ (Ξ· a ∘ ΞΈ a) = (H f ∘ Ξ· a) ∘ ΞΈ a : assoc ... = (Ξ· b ∘ G f) ∘ ΞΈ a : naturality Ξ· f ... = Ξ· b ∘ (G f ∘ ΞΈ a) : assoc ... = Ξ· b ∘ (ΞΈ b ∘ F f) : naturality ΞΈ f ... = (Ξ· b ∘ ΞΈ b) ∘ F f : assoc) --congr_arg (Ξ»x, Ξ· b ∘ x) (naturality ΞΈ f) -- this needed to be explicit for some reason (on Oct 24) infixr `∘n`:60 := compose protected theorem assoc (η₃ : H ⟹ I) (Ξ·β‚‚ : G ⟹ H) (η₁ : F ⟹ G) : η₃ ∘n (Ξ·β‚‚ ∘n η₁) = (η₃ ∘n Ξ·β‚‚) ∘n η₁ := dcongr_arg2 mk (funext (take x, !assoc)) !proof_irrel protected definition id {C D : Category} {F : functor C D} : natural_transformation F F := mk (Ξ»a, id) (Ξ»a b f, !id_right ⬝ symm !id_left) protected definition ID {C D : Category} (F : functor C D) : natural_transformation F F := id protected theorem id_left (Ξ· : F ⟹ G) : natural_transformation.compose id Ξ· = Ξ· := rec (Ξ»f H, dcongr_arg2 mk (funext (take x, !id_left)) !proof_irrel) Ξ· protected theorem id_right (Ξ· : F ⟹ G) : natural_transformation.compose Ξ· id = Ξ· := rec (Ξ»f H, dcongr_arg2 mk (funext (take x, !id_right)) !proof_irrel) Ξ· end natural_transformation
3db3512b79fe1bd90af0a17596b22236e6603348
2de8c1580f92bb6c28b60135f589fe9d0513faba
/src/rtc.lean
f3df72669484cb92b654a1d3300c3ca1c95da0ca
[]
no_license
FCL-lean/verification
44a52e40ab78b18654b8d61bb55c2c912a40d2f4
be02c698c0ca78b18762e3fe7749cdc72a55d197
refs/heads/master
1,585,960,207,309
1,560,259,990,000
1,560,259,990,000
155,650,137
0
0
null
1,541,039,704,000
1,541,038,972,000
Lean
UTF-8
Lean
false
false
742
lean
section relation inductive rtc {Ξ± : Sort*} (r : Ξ± β†’ Ξ± β†’ Prop) : Ξ± β†’ Ξ± β†’ Prop | refl : βˆ€ a, rtc a a | base : βˆ€ a b, r a b β†’ rtc a b | trans : βˆ€ a b c, rtc a b β†’ rtc b c β†’ rtc a c section variables {Ξ± : Sort*} {r : Ξ± β†’ Ξ± β†’ Prop} {a b c : Ξ±} lemma rtc.refl' : rtc r a a := rtc.refl _ _ lemma rtc.base' (h : r a b) : rtc r a b := rtc.base _ _ h lemma rtc.trans' (h₁ : rtc r a b) (hβ‚‚ : rtc r b c) : rtc r a c := rtc.trans _ _ _ h₁ hβ‚‚ lemma rtc.base_trans (h₁ : r a b) (hβ‚‚ : rtc r b c) : rtc r a c := rtc.trans _ _ _ (rtc.base _ _ h₁) hβ‚‚ lemma rtc.trans_base (h₁ : rtc r a b) (hβ‚‚ : r b c) : rtc r a c := rtc.trans _ _ _ h₁ (rtc.base _ _ hβ‚‚) end end relation
e752fe0752320dfe76ada24fec31a426dcf1b44d
c9b68131de1dfe4e7f0ea5749b11e67a774bc839
/src/final_correctness.lean
355671feaff3f4cd565a29c52e0a722649728af8
[]
no_license
congge666/formal-proofs
2013f158f310abcfc07c156bb2a5113fb78f7831
b5f6964d0220c8f89668357f2c08e44861128fe3
refs/heads/master
1,691,374,567,671
1,632,704,604,000
1,632,706,366,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,523
lean
/- This is the final correctenss theorem, stated in terms of the autogenerated constraints. The statements of the theorems only depend on the data and constraints specified in `constraints_autogenerated.lean` and the machine semantics in `cpu.lean`. -/ import correctness glue noncomputable theory open_locale classical open_locale big_operators open_locale disable_subsingleton_simps variables {F : Type} [field F] [fintype F] /- These are the constraints that the verifier has to check against the public data. -/ structure public_constraints (inp : input_data F) (pd : public_data F) : Prop := (h_mem_star : let z := pd.memory__multi_column_perm__perm__interaction_elm, alpha := pd.memory__multi_column_perm__hash_interaction_elm0, p := pd.memory__multi_column_perm__perm__public_memory_prod, dom_m_star := { x // option.is_some (inp.m_star x) } in p * ∏ a : dom_m_star, (z - (a.val + alpha * mem_val a)) = z^(fintype.card dom_m_star)) (h_card_dom : 8 * fintype.card { x // option.is_some (inp.m_star x) } + 2 ≀ inp.trace_length) (public_memory_prod_eq_one : pd.rc16__perm__public_memory_prod = 1) (rc_max_lt : pd.rc_max < 2^16) (rc_min_le : pd.rc_min ≀ pd.rc_max) (trace_length_le_char : inp.trace_length ≀ ring_char F) /- The main correctness theorem. -/ theorem final_correctness (char_ge : ring_char F β‰₯ 2^63) /- public data -/ (inp : input_data F) (pd : public_data F) (pc : public_constraints inp pd) (c : columns F) : /- sets to avoid -/ βˆƒ bad1 bad2 bad3 : finset F, bad1.card ≀ (inp.trace_length / 2)^2 ∧ bad2.card ≀ inp.trace_length / 2 ∧ bad3.card ≀ inp.trace_length ∧ βˆ€ ci : columns_inter F, /- autogenerated constraints-/ cpu__decode c ∧ cpu__operands c ∧ cpu__update_registers c inp ∧ cpu__opcodes c ∧ memory inp pd c ci ∧ rc16 inp pd c ci ∧ public_memory c ∧ initial_and_final inp c ∧ /- probabilistic constraints -/ pd.memory__multi_column_perm__hash_interaction_elm0 βˆ‰ bad1 ∧ pd.memory__multi_column_perm__perm__interaction_elm βˆ‰ bad2 ∧ pd.memory__multi_column_perm__perm__interaction_elm β‰  0 ∧ pd.rc16__perm__interaction_elm βˆ‰ bad3 β†’ /- the conclusion -/ let T := inp.trace_length / 16 - 1 in βˆƒ mem : F β†’ F, option.fn_extends mem inp.m_star ∧ βˆƒ exec : fin (T + 1) β†’ register_state F, (exec 0).pc = inp.initial_pc ∧ (exec 0).ap = inp.initial_ap ∧ (exec 0).fp = inp.initial_ap ∧ (exec (fin.last T)).pc = inp.final_pc ∧ (exec (fin.last T)).ap = inp.final_ap ∧ βˆ€ i : fin T, next_state mem (exec i.cast_succ) (exec i.succ) := begin use bad1 pc.h_card_dom c.column19 c.column20, use bad2 pd pc.h_card_dom c.column19 c.column20, use bad3 inp c.column0 c.column2, use bad1_bound pc.h_card_dom _ _, use bad2_bound pd pc.h_card_dom _ _, use bad3_bound pc.h_card_dom _ _, intro ci, rintros ⟨cd, ops, upd, opcodes, m, rc, pm, iandf, prob1, prob2, prob3, prob4⟩, exact execution_exists char_ge (inp.to_input_data_aux pd pc.rc_max_lt pc.rc_min_le) (to_constraints cd ops upd opcodes m rc pm iandf pc.h_mem_star pc.h_card_dom pc.public_memory_prod_eq_one pc.rc_max_lt pc.rc_min_le pc.trace_length_le_char) { hprob₁ := prob1, hprobβ‚‚ := prob2, hprob₃ := prob3, hprobβ‚„ := prob4 } end
4acc8d707a9b4b93afdcadd6ee31843438783718
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/Right0.lean
3dee9f2365a69591934ee523bff5e668f9920c78
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
6,524
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section Right0 structure Right0 (A : Type) : Type := (zero : A) (op : (A β†’ (A β†’ A))) (rightZero_op_zero : (βˆ€ {x : A} , (op x zero) = zero)) open Right0 structure Sig (AS : Type) : Type := (zeroS : AS) (opS : (AS β†’ (AS β†’ AS))) structure Product (A : Type) : Type := (zeroP : (Prod A A)) (opP : ((Prod A A) β†’ ((Prod A A) β†’ (Prod A A)))) (rightZero_op_0P : (βˆ€ {xP : (Prod A A)} , (opP xP zeroP) = zeroP)) structure Hom {A1 : Type} {A2 : Type} (Ri1 : (Right0 A1)) (Ri2 : (Right0 A2)) : Type := (hom : (A1 β†’ A2)) (pres_zero : (hom (zero Ri1)) = (zero Ri2)) (pres_op : (βˆ€ {x1 x2 : A1} , (hom ((op Ri1) x1 x2)) = ((op Ri2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Ri1 : (Right0 A1)) (Ri2 : (Right0 A2)) : Type 1 := (interp : (A1 β†’ (A2 β†’ Type))) (interp_zero : (interp (zero Ri1) (zero Ri2))) (interp_op : (βˆ€ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) β†’ ((interp x2 y2) β†’ (interp ((op Ri1) x1 x2) ((op Ri2) y1 y2)))))) inductive Right0LTerm : Type | zeroL : Right0LTerm | opL : (Right0LTerm β†’ (Right0LTerm β†’ Right0LTerm)) open Right0LTerm inductive ClRight0ClTerm (A : Type) : Type | sing : (A β†’ ClRight0ClTerm) | zeroCl : ClRight0ClTerm | opCl : (ClRight0ClTerm β†’ (ClRight0ClTerm β†’ ClRight0ClTerm)) open ClRight0ClTerm inductive OpRight0OLTerm (n : β„•) : Type | v : ((fin n) β†’ OpRight0OLTerm) | zeroOL : OpRight0OLTerm | opOL : (OpRight0OLTerm β†’ (OpRight0OLTerm β†’ OpRight0OLTerm)) open OpRight0OLTerm inductive OpRight0OL2Term2 (n : β„•) (A : Type) : Type | v2 : ((fin n) β†’ OpRight0OL2Term2) | sing2 : (A β†’ OpRight0OL2Term2) | zeroOL2 : OpRight0OL2Term2 | opOL2 : (OpRight0OL2Term2 β†’ (OpRight0OL2Term2 β†’ OpRight0OL2Term2)) open OpRight0OL2Term2 def simplifyCl {A : Type} : ((ClRight0ClTerm A) β†’ (ClRight0ClTerm A)) | zeroCl := zeroCl | (opCl x1 x2) := (opCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : β„•} : ((OpRight0OLTerm n) β†’ (OpRight0OLTerm n)) | zeroOL := zeroOL | (opOL x1 x2) := (opOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : β„•} {A : Type} : ((OpRight0OL2Term2 n A) β†’ (OpRight0OL2Term2 n A)) | zeroOL2 := zeroOL2 | (opOL2 x1 x2) := (opOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((Right0 A) β†’ (Right0LTerm β†’ A)) | Ri zeroL := (zero Ri) | Ri (opL x1 x2) := ((op Ri) (evalB Ri x1) (evalB Ri x2)) def evalCl {A : Type} : ((Right0 A) β†’ ((ClRight0ClTerm A) β†’ A)) | Ri (sing x1) := x1 | Ri zeroCl := (zero Ri) | Ri (opCl x1 x2) := ((op Ri) (evalCl Ri x1) (evalCl Ri x2)) def evalOpB {A : Type} {n : β„•} : ((Right0 A) β†’ ((vector A n) β†’ ((OpRight0OLTerm n) β†’ A))) | Ri vars (v x1) := (nth vars x1) | Ri vars zeroOL := (zero Ri) | Ri vars (opOL x1 x2) := ((op Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) def evalOp {A : Type} {n : β„•} : ((Right0 A) β†’ ((vector A n) β†’ ((OpRight0OL2Term2 n A) β†’ A))) | Ri vars (v2 x1) := (nth vars x1) | Ri vars (sing2 x1) := x1 | Ri vars zeroOL2 := (zero Ri) | Ri vars (opOL2 x1 x2) := ((op Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) def inductionB {P : (Right0LTerm β†’ Type)} : ((P zeroL) β†’ ((βˆ€ (x1 x2 : Right0LTerm) , ((P x1) β†’ ((P x2) β†’ (P (opL x1 x2))))) β†’ (βˆ€ (x : Right0LTerm) , (P x)))) | p0l popl zeroL := p0l | p0l popl (opL x1 x2) := (popl _ _ (inductionB p0l popl x1) (inductionB p0l popl x2)) def inductionCl {A : Type} {P : ((ClRight0ClTerm A) β†’ Type)} : ((βˆ€ (x1 : A) , (P (sing x1))) β†’ ((P zeroCl) β†’ ((βˆ€ (x1 x2 : (ClRight0ClTerm A)) , ((P x1) β†’ ((P x2) β†’ (P (opCl x1 x2))))) β†’ (βˆ€ (x : (ClRight0ClTerm A)) , (P x))))) | psing p0cl popcl (sing x1) := (psing x1) | psing p0cl popcl zeroCl := p0cl | psing p0cl popcl (opCl x1 x2) := (popcl _ _ (inductionCl psing p0cl popcl x1) (inductionCl psing p0cl popcl x2)) def inductionOpB {n : β„•} {P : ((OpRight0OLTerm n) β†’ Type)} : ((βˆ€ (fin : (fin n)) , (P (v fin))) β†’ ((P zeroOL) β†’ ((βˆ€ (x1 x2 : (OpRight0OLTerm n)) , ((P x1) β†’ ((P x2) β†’ (P (opOL x1 x2))))) β†’ (βˆ€ (x : (OpRight0OLTerm n)) , (P x))))) | pv p0ol popol (v x1) := (pv x1) | pv p0ol popol zeroOL := p0ol | pv p0ol popol (opOL x1 x2) := (popol _ _ (inductionOpB pv p0ol popol x1) (inductionOpB pv p0ol popol x2)) def inductionOp {n : β„•} {A : Type} {P : ((OpRight0OL2Term2 n A) β†’ Type)} : ((βˆ€ (fin : (fin n)) , (P (v2 fin))) β†’ ((βˆ€ (x1 : A) , (P (sing2 x1))) β†’ ((P zeroOL2) β†’ ((βˆ€ (x1 x2 : (OpRight0OL2Term2 n A)) , ((P x1) β†’ ((P x2) β†’ (P (opOL2 x1 x2))))) β†’ (βˆ€ (x : (OpRight0OL2Term2 n A)) , (P x)))))) | pv2 psing2 p0ol2 popol2 (v2 x1) := (pv2 x1) | pv2 psing2 p0ol2 popol2 (sing2 x1) := (psing2 x1) | pv2 psing2 p0ol2 popol2 zeroOL2 := p0ol2 | pv2 psing2 p0ol2 popol2 (opOL2 x1 x2) := (popol2 _ _ (inductionOp pv2 psing2 p0ol2 popol2 x1) (inductionOp pv2 psing2 p0ol2 popol2 x2)) def stageB : (Right0LTerm β†’ (Staged Right0LTerm)) | zeroL := (Now zeroL) | (opL x1 x2) := (stage2 opL (codeLift2 opL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClRight0ClTerm A) β†’ (Staged (ClRight0ClTerm A))) | (sing x1) := (Now (sing x1)) | zeroCl := (Now zeroCl) | (opCl x1 x2) := (stage2 opCl (codeLift2 opCl) (stageCl x1) (stageCl x2)) def stageOpB {n : β„•} : ((OpRight0OLTerm n) β†’ (Staged (OpRight0OLTerm n))) | (v x1) := (const (code (v x1))) | zeroOL := (Now zeroOL) | (opOL x1 x2) := (stage2 opOL (codeLift2 opOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : β„•} {A : Type} : ((OpRight0OL2Term2 n A) β†’ (Staged (OpRight0OL2Term2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | zeroOL2 := (Now zeroOL2) | (opOL2 x1 x2) := (stage2 opOL2 (codeLift2 opOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type β†’ Type)) : Type := (zeroT : (Repr A)) (opT : ((Repr A) β†’ ((Repr A) β†’ (Repr A)))) end Right0
0cb1c301c1e61231f21c9e5ab5ab929ac65a6b60
17d3c61bf162bf88be633867ed4cb201378a8769
/library/init/util.lean
3b9a9b713e1a527e2b0a3f04404315f0594aa579
[ "Apache-2.0" ]
permissive
u20024804/lean
11def01468fb4796fb0da76015855adceac7e311
d315e424ff17faf6fe096a0a1407b70193009726
refs/heads/master
1,611,388,567,561
1,485,836,506,000
1,485,836,625,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,071
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.string.basic universe variables u /- This function has a native implementation that tracks time. -/ def timeit {Ξ± : Type u} (s : string) (f : unit β†’ Ξ±) : Ξ± := f () /- This function has a native implementation that displays the given string in the regular output stream. -/ def trace {Ξ± : Type u} (s : string) (f : unit β†’ Ξ±) : Ξ± := f () /- This function has a native implementation that shows the VM call stack. -/ def trace_call_stack {Ξ± : Type u} (f : unit β†’ Ξ±) : Ξ± := f () /- This function has a native implementation that displays in the given position all trace messages used in f. The arguments line and col are filled by the elaborator. -/ def scope_trace {Ξ± : Type u} {line col: nat} (f : unit β†’ Ξ±) : Ξ± := f () meta constant undefined_core {Ξ± : Type u} (message : string) : Ξ± meta def undefined {Ξ± : Type u} : Ξ± := undefined_core "undefined"
5ebade349101283164de20f96464f354476864a9
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Compiler/Util.lean
8ab110b0b6189416d7b3ff205e52224891269a26
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
3,904
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.Match.MatcherInfo namespace Lean.Compiler /-- Return `true` if `mdata` should be preserved. Right now, we don't preserve any `MData`, but this may change in the future when we add support for debugging information -/ def isCompilerRelevantMData (_mdata : MData) : Bool := false /-- Return `true` if `e` is a `lcProof` application. Recall that we use `lcProof` to erase all nested proofs. -/ def isLCProof (e : Expr) : Bool := e.isAppOfArity ``lcProof 1 /-- Return `true` if `e` is a `lcUnreachable` application. -/ def isLcUnreachable (e : Expr) : Bool := e.isAppOfArity ``lcUnreachable 1 /-- Return `true` if `e` is a `lcCast` application. -/ def isLcCast? (e : Expr) : Option Expr := if e.isAppOfArity ``lcCast 3 then some e.appArg! else none /-- Create `lcProof p` -/ def mkLcProof (p : Expr) := mkApp (mkConst ``lcProof []) p /-- Store information about `matcher` and `casesOn` declarations. We treat them uniformly in the code generator. -/ structure CasesInfo where arity : Nat numParams : Nat discrsRange : Std.Range altsRange : Std.Range altNumParams : Array Nat motivePos : Nat private def getCasesOnInductiveVal? (declName : Name) : CoreM (Option InductiveVal) := do unless isCasesOnRecursor (← getEnv) declName do return none let .inductInfo val ← getConstInfo declName.getPrefix | return none return some val def getCasesInfo? (declName : Name) : CoreM (Option CasesInfo) := do let some val ← getCasesOnInductiveVal? declName | return none let numParams := val.numParams let motivePos := numParams let arity := numParams + 1 /- motive -/ + val.numIndices + 1 /- major -/ + val.numCtors let majorPos := numParams + 1 /- motive -/ + val.numIndices -- We view indices as discriminants let discrsRange := { start := numParams + 1, stop := majorPos + 1 } let altsRange := { start := majorPos + 1, stop := arity } let altNumParams ← val.ctors.toArray.mapM fun ctor => do let .ctorInfo ctorVal ← getConstInfo ctor | unreachable! return ctorVal.numFields return some { numParams, motivePos, arity, discrsRange, altsRange, altNumParams } def CasesInfo.geNumDiscrs (casesInfo : CasesInfo) : Nat := casesInfo.discrsRange.stop - casesInfo.discrsRange.start def CasesInfo.updateResultingType (casesInfo : CasesInfo) (casesArgs : Array Expr) (typeNew : Expr) : Array Expr := casesArgs.modify casesInfo.motivePos fun motive => go motive where go (e : Expr) : Expr := match e with | .lam n b d bi => .lam n b (go d) bi | _ => typeNew def isCasesApp? (e : Expr) : CoreM (Option CasesInfo) := do let .const declName _ := e.getAppFn | return none if let some info ← getCasesInfo? declName then assert! info.arity == e.getAppNumArgs return some info else return none def getCtorArity? (declName : Name) : CoreM (Option Nat) := do let .ctorInfo val ← getConstInfo declName | return none return val.numParams + val.numFields /-- Return `true` if the `value` if not a `lambda`, `let` or cases-like expression. -/ def isSimpleLCNF (value : Expr) : CoreM Bool := do if value.isLet || value.isLambda then return false else if let some _ ← isCasesApp? value then return false else return true /-- List of types that have builtin runtime support -/ def builtinRuntimeTypes : List Name := [ ``String, ``UInt8, ``UInt16, ``UInt32, ``UInt64, ``USize, ``Float, ``Thunk, ``Task, ``Array, ``ByteArray, ``FloatArray, ``Nat, ``Int ] /-- Return `true` iff `declName` is the name of a type with builtin support in the runtime. -/ def isRuntimeBultinType (declName : Name) : Bool := builtinRuntimeTypes.contains declName end Lean.Compiler
4e2f947c6307ec51773518fb3870dd7ecc26a461
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/ring_quot.lean
7d70ddb62fe7720f5670a01e48aad76e63103359
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
17,975
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.algebra.basic import ring_theory.ideal.quotient /-! # Quotients of non-commutative rings Unfortunately, ideals have only been developed in the commutative case as `ideal`, and it's not immediately clear how one should formalise ideals in the non-commutative case. In this file, we directly define the quotient of a semiring by any relation, by building a bigger relation that represents the ideal generated by that relation. We prove the universal properties of the quotient, and recommend avoiding relying on the actual definition, which is made irreducible for this purpose. Since everything runs in parallel for quotients of `R`-algebras, we do that case at the same time. -/ universes u₁ uβ‚‚ u₃ uβ‚„ variables {R : Type u₁} [semiring R] variables {S : Type uβ‚‚} [comm_semiring S] variables {A : Type u₃} [semiring A] [algebra S A] namespace ring_quot /-- Given an arbitrary relation `r` on a ring, we strengthen it to a relation `rel r`, such that the equivalence relation generated by `rel r` has `x ~ y` if and only if `x - y` is in the ideal generated by elements `a - b` such that `r a b`. -/ inductive rel (r : R β†’ R β†’ Prop) : R β†’ R β†’ Prop | of ⦃x y : R⦄ (h : r x y) : rel x y | add_left ⦃a b c⦄ : rel a b β†’ rel (a + c) (b + c) | mul_left ⦃a b c⦄ : rel a b β†’ rel (a * c) (b * c) | mul_right ⦃a b c⦄ : rel b c β†’ rel (a * b) (a * c) theorem rel.add_right {r : R β†’ R β†’ Prop} ⦃a b c : R⦄ (h : rel r b c) : rel r (a + b) (a + c) := by { rw [add_comm a b, add_comm a c], exact rel.add_left h } theorem rel.neg {R : Type u₁} [ring R] {r : R β†’ R β†’ Prop} ⦃a b : R⦄ (h : rel r a b) : rel r (-a) (-b) := by simp only [neg_eq_neg_one_mul a, neg_eq_neg_one_mul b, rel.mul_right h] theorem rel.sub_left {R : Type u₁} [ring R] {r : R β†’ R β†’ Prop} ⦃a b c : R⦄ (h : rel r a b) : rel r (a - c) (b - c) := by simp only [sub_eq_add_neg, h.add_left] theorem rel.sub_right {R : Type u₁} [ring R] {r : R β†’ R β†’ Prop} ⦃a b c : R⦄ (h : rel r b c) : rel r (a - b) (a - c) := by simp only [sub_eq_add_neg, h.neg.add_right] theorem rel.smul {r : A β†’ A β†’ Prop} (k : S) ⦃a b : A⦄ (h : rel r a b) : rel r (k β€’ a) (k β€’ b) := by simp only [algebra.smul_def, rel.mul_right h] end ring_quot /-- The quotient of a ring by an arbitrary relation. -/ structure ring_quot (r : R β†’ R β†’ Prop) := (to_quot : quot (ring_quot.rel r)) namespace ring_quot variable (r : R β†’ R β†’ Prop) @[irreducible] private def nat_cast (n : β„•) : ring_quot r := ⟨quot.mk _ n⟩ @[irreducible] private def zero : ring_quot r := ⟨quot.mk _ 0⟩ @[irreducible] private def one : ring_quot r := ⟨quot.mk _ 1⟩ @[irreducible] private def add : ring_quot r β†’ ring_quot r β†’ ring_quot r | ⟨a⟩ ⟨b⟩ := ⟨quot.mapβ‚‚ (+) rel.add_right rel.add_left a b⟩ @[irreducible] private def mul : ring_quot r β†’ ring_quot r β†’ ring_quot r | ⟨a⟩ ⟨b⟩ := ⟨quot.mapβ‚‚ (*) rel.mul_right rel.mul_left a b⟩ @[irreducible] private def neg {R : Type u₁} [ring R] (r : R β†’ R β†’ Prop) : ring_quot r β†’ ring_quot r | ⟨a⟩:= ⟨quot.map (Ξ» a, -a) rel.neg a⟩ @[irreducible] private def sub {R : Type u₁} [ring R] (r : R β†’ R β†’ Prop) : ring_quot r β†’ ring_quot r β†’ ring_quot r | ⟨a⟩ ⟨b⟩ := ⟨quot.mapβ‚‚ has_sub.sub rel.sub_right rel.sub_left a b⟩ @[irreducible] private def npow (n : β„•) : ring_quot r β†’ ring_quot r | ⟨a⟩ := ⟨quot.lift (Ξ» a, quot.mk (ring_quot.rel r) (a ^ n)) (Ξ» a b (h : rel r a b), begin -- note we can't define a `rel.pow` as `rel` isn't reflexive so `rel r 1 1` isn't true dsimp only, induction n, { rw [pow_zero, pow_zero] }, { rw [pow_succ, pow_succ], simpa only [mul] using congr_arg2 (Ξ» x y, mul r ⟨x⟩ ⟨y⟩) (quot.sound h) n_ih } end) a⟩ @[irreducible] private def smul [algebra S R] (n : S) : ring_quot r β†’ ring_quot r | ⟨a⟩ := ⟨quot.map (Ξ» a, n β€’ a) (rel.smul n) a⟩ instance : has_zero (ring_quot r) := ⟨zero r⟩ instance : has_one (ring_quot r) := ⟨one r⟩ instance : has_add (ring_quot r) := ⟨add r⟩ instance : has_mul (ring_quot r) := ⟨mul r⟩ instance : has_pow (ring_quot r) β„• := ⟨λ x n, npow r n x⟩ instance {R : Type u₁} [ring R] (r : R β†’ R β†’ Prop) : has_neg (ring_quot r) := ⟨neg r⟩ instance {R : Type u₁} [ring R] (r : R β†’ R β†’ Prop) : has_sub (ring_quot r) := ⟨sub r⟩ instance [algebra S R] : has_smul S (ring_quot r) := ⟨smul r⟩ lemma zero_quot : (⟨quot.mk _ 0⟩ : ring_quot r) = 0 := show _ = zero r, by rw zero lemma one_quot : (⟨quot.mk _ 1⟩ : ring_quot r) = 1 := show _ = one r, by rw one lemma add_quot {a b} : (⟨quot.mk _ a⟩ + ⟨quot.mk _ b⟩ : ring_quot r) = ⟨quot.mk _ (a + b)⟩ := by { show add r _ _ = _, rw add, refl } lemma mul_quot {a b} : (⟨quot.mk _ a⟩ * ⟨quot.mk _ b⟩ : ring_quot r) = ⟨quot.mk _ (a * b)⟩ := by { show mul r _ _ = _, rw mul, refl } lemma pow_quot {a} {n : β„•}: (⟨quot.mk _ a⟩ ^ n : ring_quot r) = ⟨quot.mk _ (a ^ n)⟩ := by { show npow r _ _ = _, rw npow } lemma neg_quot {R : Type u₁} [ring R] (r : R β†’ R β†’ Prop) {a} : (-⟨quot.mk _ a⟩ : ring_quot r) = ⟨quot.mk _ (-a)⟩ := by { show neg r _ = _, rw neg, refl } lemma sub_quot {R : Type u₁} [ring R] (r : R β†’ R β†’ Prop) {a b} : (⟨quot.mk _ a⟩ - ⟨ quot.mk _ b⟩ : ring_quot r) = ⟨quot.mk _ (a - b)⟩ := by { show sub r _ _ = _, rw sub, refl } lemma smul_quot [algebra S R] {n : S} {a : R} : (n β€’ ⟨quot.mk _ a⟩ : ring_quot r) = ⟨quot.mk _ (n β€’ a)⟩ := by { show smul r _ _ = _, rw smul, refl } instance (r : R β†’ R β†’ Prop) : semiring (ring_quot r) := { add := (+), mul := (*), zero := 0, one := 1, nat_cast := nat_cast r, nat_cast_zero := by simp [nat.cast, nat_cast, ← zero_quot], nat_cast_succ := by simp [nat.cast, nat_cast, ← one_quot, add_quot], add_assoc := by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨⟨⟩⟩, simp [add_quot, add_assoc] }, zero_add := by { rintros ⟨⟨⟩⟩, simp [add_quot, ← zero_quot] }, add_zero := by { rintros ⟨⟨⟩⟩, simp [add_quot, ← zero_quot], }, zero_mul := by { rintros ⟨⟨⟩⟩, simp [mul_quot, ← zero_quot], }, mul_zero := by { rintros ⟨⟨⟩⟩, simp [mul_quot, ← zero_quot], }, add_comm := by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩, simp [add_quot, add_comm], }, mul_assoc := by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨⟨⟩⟩, simp [mul_quot, mul_assoc] }, one_mul := by { rintros ⟨⟨⟩⟩, simp [mul_quot, ← one_quot] }, mul_one := by { rintros ⟨⟨⟩⟩, simp [mul_quot, ← one_quot] }, left_distrib := by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨⟨⟩⟩, simp [mul_quot, add_quot, left_distrib] }, right_distrib := by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨⟨⟩⟩, simp [mul_quot, add_quot, right_distrib] }, npow := Ξ» n x, x ^ n, npow_zero' := by { rintros ⟨⟨⟩⟩, simp [pow_quot, ← one_quot] }, npow_succ' := by { rintros n ⟨⟨⟩⟩, simp [pow_quot, mul_quot, pow_succ] }, nsmul := (β€’), nsmul_zero' := by { rintros ⟨⟨⟩⟩, simp [smul_quot, ← zero_quot] }, nsmul_succ' := by { rintros n ⟨⟨⟩⟩, simp [smul_quot, add_quot, add_mul, add_comm] } } instance {R : Type u₁} [ring R] (r : R β†’ R β†’ Prop) : ring (ring_quot r) := { neg := has_neg.neg, add_left_neg := by { rintros ⟨⟨⟩⟩, simp [neg_quot, add_quot, ← zero_quot], }, sub := has_sub.sub, sub_eq_add_neg := by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩, simp [neg_quot, sub_quot, add_quot, sub_eq_add_neg] }, zsmul := (β€’), zsmul_zero' := by { rintros ⟨⟨⟩⟩, simp [smul_quot, ← zero_quot] }, zsmul_succ' := by { rintros n ⟨⟨⟩⟩, simp [smul_quot, add_quot, add_mul, add_comm] }, zsmul_neg' := by { rintros n ⟨⟨⟩⟩, simp [smul_quot, neg_quot, add_mul] }, .. (ring_quot.semiring r) } instance {R : Type u₁} [comm_semiring R] (r : R β†’ R β†’ Prop) : comm_semiring (ring_quot r) := { mul_comm := by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩, simp [mul_quot, mul_comm], } .. (ring_quot.semiring r) } instance {R : Type u₁} [comm_ring R] (r : R β†’ R β†’ Prop) : comm_ring (ring_quot r) := { .. (ring_quot.comm_semiring r), .. (ring_quot.ring r) } instance (r : R β†’ R β†’ Prop) : inhabited (ring_quot r) := ⟨0⟩ instance [algebra S R] (r : R β†’ R β†’ Prop) : algebra S (ring_quot r) := { smul := (β€’), to_fun := Ξ» r, ⟨quot.mk _ (algebra_map S R r)⟩, map_one' := by simp [← one_quot], map_mul' := by simp [mul_quot], map_zero' := by simp [← zero_quot], map_add' := by simp [add_quot], commutes' := Ξ» r, by { rintro ⟨⟨a⟩⟩, simp [algebra.commutes, mul_quot] }, smul_def' := Ξ» r, by { rintro ⟨⟨a⟩⟩, simp [smul_quot, algebra.smul_def, mul_quot], }, } /-- The quotient map from a ring to its quotient, as a homomorphism of rings. -/ def mk_ring_hom (r : R β†’ R β†’ Prop) : R β†’+* ring_quot r := { to_fun := Ξ» x, ⟨quot.mk _ x⟩, map_one' := by simp [← one_quot], map_mul' := by simp [mul_quot], map_zero' := by simp [← zero_quot], map_add' := by simp [add_quot], } lemma mk_ring_hom_rel {r : R β†’ R β†’ Prop} {x y : R} (w : r x y) : mk_ring_hom r x = mk_ring_hom r y := by simp [mk_ring_hom, quot.sound (rel.of w)] lemma mk_ring_hom_surjective (r : R β†’ R β†’ Prop) : function.surjective (mk_ring_hom r) := by { dsimp [mk_ring_hom], rintro ⟨⟨⟩⟩, simp, } @[ext] lemma ring_quot_ext {T : Type uβ‚„} [semiring T] {r : R β†’ R β†’ Prop} (f g : ring_quot r β†’+* T) (w : f.comp (mk_ring_hom r) = g.comp (mk_ring_hom r)) : f = g := begin ext, rcases mk_ring_hom_surjective r x with ⟨x, rfl⟩, exact (ring_hom.congr_fun w x : _), end variables {T : Type uβ‚„} [semiring T] /-- Any ring homomorphism `f : R β†’+* T` which respects a relation `r : R β†’ R β†’ Prop` factors uniquely through a morphism `ring_quot r β†’+* T`. -/ def lift {r : R β†’ R β†’ Prop} : {f : R β†’+* T // βˆ€ ⦃x y⦄, r x y β†’ f x = f y} ≃ (ring_quot r β†’+* T) := { to_fun := Ξ» f', let f := (f' : R β†’+* T) in { to_fun := Ξ» x, quot.lift f begin rintros _ _ r, induction r, case of : _ _ r { exact f'.prop r, }, case add_left : _ _ _ _ r' { simp [r'], }, case mul_left : _ _ _ _ r' { simp [r'], }, case mul_right : _ _ _ _ r' { simp [r'], }, end x.to_quot, map_zero' := by simp [← zero_quot, f.map_zero], map_add' := by { rintros ⟨⟨x⟩⟩ ⟨⟨y⟩⟩, simp [add_quot, f.map_add x y], }, map_one' := by simp [← one_quot, f.map_one], map_mul' := by { rintros ⟨⟨x⟩⟩ ⟨⟨y⟩⟩, simp [mul_quot, f.map_mul x y] }, }, inv_fun := Ξ» F, ⟨F.comp (mk_ring_hom r), Ξ» x y h, by { dsimp, rw mk_ring_hom_rel h, }⟩, left_inv := Ξ» f, by { ext, simp, refl }, right_inv := Ξ» F, by { ext, simp, refl } } @[simp] lemma lift_mk_ring_hom_apply (f : R β†’+* T) {r : R β†’ R β†’ Prop} (w : βˆ€ ⦃x y⦄, r x y β†’ f x = f y) (x) : lift ⟨f, w⟩ (mk_ring_hom r x) = f x := rfl -- note this is essentially `lift.symm_apply_eq.mp h` lemma lift_unique (f : R β†’+* T) {r : R β†’ R β†’ Prop} (w : βˆ€ ⦃x y⦄, r x y β†’ f x = f y) (g : ring_quot r β†’+* T) (h : g.comp (mk_ring_hom r) = f) : g = lift ⟨f, w⟩ := by { ext, simp [h], } lemma eq_lift_comp_mk_ring_hom {r : R β†’ R β†’ Prop} (f : ring_quot r β†’+* T) : f = lift ⟨f.comp (mk_ring_hom r), Ξ» x y h, by { dsimp, rw mk_ring_hom_rel h, }⟩ := (lift.apply_symm_apply f).symm section comm_ring /-! We now verify that in the case of a commutative ring, the `ring_quot` construction agrees with the quotient by the appropriate ideal. -/ variables {B : Type u₁} [comm_ring B] /-- The universal ring homomorphism from `ring_quot r` to `B β§Έ ideal.of_rel r`. -/ def ring_quot_to_ideal_quotient (r : B β†’ B β†’ Prop) : ring_quot r β†’+* B β§Έ ideal.of_rel r := lift ⟨ideal.quotient.mk (ideal.of_rel r), Ξ» x y h, ideal.quotient.eq.2 $ submodule.mem_Inf.mpr (Ξ» p w, w ⟨x, y, h, sub_add_cancel x y⟩)⟩ @[simp] lemma ring_quot_to_ideal_quotient_apply (r : B β†’ B β†’ Prop) (x : B) : ring_quot_to_ideal_quotient r (mk_ring_hom r x) = ideal.quotient.mk _ x := rfl /-- The universal ring homomorphism from `B β§Έ ideal.of_rel r` to `ring_quot r`. -/ def ideal_quotient_to_ring_quot (r : B β†’ B β†’ Prop) : B β§Έ ideal.of_rel r β†’+* ring_quot r := ideal.quotient.lift (ideal.of_rel r) (mk_ring_hom r) begin refine Ξ» x h, submodule.span_induction h _ _ _ _, { rintro y ⟨a, b, h, su⟩, symmetry' at su, rw ←sub_eq_iff_eq_add at su, rw [ ← su, ring_hom.map_sub, mk_ring_hom_rel h, sub_self], }, { simp, }, { intros a b ha hb, simp [ha, hb], }, { intros a x hx, simp [hx], }, end @[simp] lemma ideal_quotient_to_ring_quot_apply (r : B β†’ B β†’ Prop) (x : B) : ideal_quotient_to_ring_quot r (ideal.quotient.mk _ x) = mk_ring_hom r x := rfl /-- The ring equivalence between `ring_quot r` and `(ideal.of_rel r).quotient` -/ def ring_quot_equiv_ideal_quotient (r : B β†’ B β†’ Prop) : ring_quot r ≃+* B β§Έ ideal.of_rel r := ring_equiv.of_hom_inv (ring_quot_to_ideal_quotient r) (ideal_quotient_to_ring_quot r) (by { ext, refl, }) (by { ext, refl, }) end comm_ring section star_ring variables [star_ring R] (r) (hr : βˆ€ a b, r a b β†’ r (star a) (star b)) include hr theorem rel.star ⦃a b : R⦄ (h : rel r a b) : rel r (star a) (star b) := begin induction h, { exact rel.of (hr _ _ h_h) }, { rw [star_add, star_add], exact rel.add_left h_ih, }, { rw [star_mul, star_mul], exact rel.mul_right h_ih, }, { rw [star_mul, star_mul], exact rel.mul_left h_ih, }, end @[irreducible] private def star' : ring_quot r β†’ ring_quot r | ⟨a⟩ := ⟨quot.map (star : R β†’ R) (rel.star r hr) a⟩ lemma star'_quot (hr : βˆ€ a b, r a b β†’ r (star a) (star b)) {a} : (star' r hr ⟨quot.mk _ a⟩ : ring_quot r) = ⟨quot.mk _ (star a)⟩ := by { show star' r _ _ = _, rw star', refl } /-- Transfer a star_ring instance through a quotient, if the quotient is invariant to `star` -/ def star_ring {R : Type u₁} [semiring R] [star_ring R] (r : R β†’ R β†’ Prop) (hr : βˆ€ a b, r a b β†’ r (star a) (star b)) : star_ring (ring_quot r) := { star := star' r hr, star_involutive := by { rintros ⟨⟨⟩⟩, simp [star'_quot], }, star_mul := by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩, simp [star'_quot, mul_quot, star_mul], }, star_add := by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩, simp [star'_quot, add_quot, star_add], } } end star_ring section algebra variables (S) /-- The quotient map from an `S`-algebra to its quotient, as a homomorphism of `S`-algebras. -/ def mk_alg_hom (s : A β†’ A β†’ Prop) : A →ₐ[S] ring_quot s := { commutes' := Ξ» r, rfl, ..mk_ring_hom s } @[simp] lemma mk_alg_hom_coe (s : A β†’ A β†’ Prop) : (mk_alg_hom S s : A β†’+* ring_quot s) = mk_ring_hom s := rfl lemma mk_alg_hom_rel {s : A β†’ A β†’ Prop} {x y : A} (w : s x y) : mk_alg_hom S s x = mk_alg_hom S s y := by simp [mk_alg_hom, mk_ring_hom, quot.sound (rel.of w)] lemma mk_alg_hom_surjective (s : A β†’ A β†’ Prop) : function.surjective (mk_alg_hom S s) := by { dsimp [mk_alg_hom], rintro ⟨⟨a⟩⟩, use a, refl, } variables {B : Type uβ‚„} [semiring B] [algebra S B] @[ext] lemma ring_quot_ext' {s : A β†’ A β†’ Prop} (f g : ring_quot s →ₐ[S] B) (w : f.comp (mk_alg_hom S s) = g.comp (mk_alg_hom S s)) : f = g := begin ext, rcases mk_alg_hom_surjective S s x with ⟨x, rfl⟩, exact (alg_hom.congr_fun w x : _), end /-- Any `S`-algebra homomorphism `f : A →ₐ[S] B` which respects a relation `s : A β†’ A β†’ Prop` factors uniquely through a morphism `ring_quot s →ₐ[S] B`. -/ def lift_alg_hom {s : A β†’ A β†’ Prop} : { f : A →ₐ[S] B // βˆ€ ⦃x y⦄, s x y β†’ f x = f y} ≃ (ring_quot s →ₐ[S] B) := { to_fun := Ξ» f', let f := (f' : A →ₐ[S] B) in { to_fun := Ξ» x, quot.lift f begin rintros _ _ r, induction r, case of : _ _ r { exact f'.prop r, }, case add_left : _ _ _ _ r' { simp [r'], }, case mul_left : _ _ _ _ r' { simp [r'], }, case mul_right : _ _ _ _ r' { simp [r'], }, end x.to_quot, map_zero' := by simp [← zero_quot, f.map_zero], map_add' := by { rintros ⟨⟨x⟩⟩ ⟨⟨y⟩⟩, simp [add_quot, f.map_add x y] }, map_one' := by simp [← one_quot, f.map_one], map_mul' := by { rintros ⟨⟨x⟩⟩ ⟨⟨y⟩⟩, simp [mul_quot, f.map_mul x y], }, commutes' := by { rintros x, simp [← one_quot, smul_quot, algebra.algebra_map_eq_smul_one] } }, inv_fun := Ξ» F, ⟨F.comp (mk_alg_hom S s), Ξ» _ _ h, by { dsimp, erw mk_alg_hom_rel S h }⟩, left_inv := Ξ» f, by { ext, simp, refl }, right_inv := Ξ» F, by { ext, simp, refl } } @[simp] lemma lift_alg_hom_mk_alg_hom_apply (f : A →ₐ[S] B) {s : A β†’ A β†’ Prop} (w : βˆ€ ⦃x y⦄, s x y β†’ f x = f y) (x) : (lift_alg_hom S ⟨f, w⟩) ((mk_alg_hom S s) x) = f x := rfl -- note this is essentially `(lift_alg_hom S).symm_apply_eq.mp h` lemma lift_alg_hom_unique (f : A →ₐ[S] B) {s : A β†’ A β†’ Prop} (w : βˆ€ ⦃x y⦄, s x y β†’ f x = f y) (g : ring_quot s →ₐ[S] B) (h : g.comp (mk_alg_hom S s) = f) : g = lift_alg_hom S ⟨f, w⟩ := by { ext, simp [h], } lemma eq_lift_alg_hom_comp_mk_alg_hom {s : A β†’ A β†’ Prop} (f : ring_quot s →ₐ[S] B) : f = lift_alg_hom S ⟨f.comp (mk_alg_hom S s), Ξ» x y h, by { dsimp, erw mk_alg_hom_rel S h, }⟩ := ((lift_alg_hom S).apply_symm_apply f).symm end algebra attribute [irreducible] mk_ring_hom mk_alg_hom lift lift_alg_hom end ring_quot
e908cde919fdecee86fcdad157fec2b80021019b
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/char_zero.lean
e66dd4f86d20c407a09186f25ba398e4a9155f28
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
6,384
lean
/- Copyright (c) 2014 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.nat.cast import data.fintype.basic import tactic.wlog /-! # Characteristic zero A ring `R` is called of characteristic zero if every natural number `n` is non-zero when considered as an element of `R`. Since this definition doesn't mention the multiplicative structure of `R` except for the existence of `1` in this file characteristic zero is defined for additive monoids with `1`. ## Main definition `char_zero` is the typeclass of an additive monoid with one such that the natural homomorphism from the natural numbers into it is injective. ## Main statements * A linearly ordered semiring has characteristic zero. * Characteristic zero implies that the additive monoid is infinite. ## TODO * Once order of a group is defined for infinite additive monoids redefine or at least connect to order of `1` in the additive monoid with one. * Unify with `char_p` (possibly using an out-parameter) -/ /-- 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 (R : Type*) [add_monoid R] [has_one R] : Prop := (cast_injective : function.injective (coe : β„• β†’ R)) theorem char_zero_of_inj_zero {R : Type*} [add_left_cancel_monoid R] [has_one R] (H : βˆ€ n:β„•, (n:R) = 0 β†’ n = 0) : char_zero R := ⟨λ m n, begin assume h, wlog hle : m ≀ n, rcases nat.le.dest hle with ⟨k, rfl⟩, rw [nat.cast_add, eq_comm, add_right_eq_self] at h, rw [H k h, add_zero] end⟩ /-- Note this is not an instance as `char_zero` implies `nontrivial`, and this would risk forming a loop. -/ lemma ordered_semiring.to_char_zero {R : Type*} [ordered_semiring R] [nontrivial R] : char_zero R := ⟨nat.strict_mono_cast.injective⟩ @[priority 100] -- see Note [lower instance priority] instance linear_ordered_semiring.to_char_zero {R : Type*} [linear_ordered_semiring R] : char_zero R := ordered_semiring.to_char_zero namespace nat variables {R : Type*} [add_monoid R] [has_one R] [char_zero R] theorem cast_injective : function.injective (coe : β„• β†’ R) := char_zero.cast_injective /-- `nat.cast` as an embedding into monoids of characteristic `0`. -/ @[simps] def cast_embedding : β„• β†ͺ R := ⟨coe, cast_injective⟩ @[simp, norm_cast] theorem cast_inj {m n : β„•} : (m : R) = n ↔ m = n := cast_injective.eq_iff @[simp, norm_cast] theorem cast_eq_zero {n : β„•} : (n : R) = 0 ↔ n = 0 := by rw [← cast_zero, cast_inj] @[norm_cast] theorem cast_ne_zero {n : β„•} : (n : R) β‰  0 ↔ n β‰  0 := not_congr cast_eq_zero lemma cast_add_one_ne_zero (n : β„•) : (n + 1 : R) β‰  0 := by exact_mod_cast n.succ_ne_zero @[simp, norm_cast] theorem cast_dvd_char_zero {k : Type*} [field k] [char_zero k] {m n : β„•} (n_dvd : n ∣ m) : ((m / n : β„•) : k) = m / n := begin by_cases hn : n = 0, { subst hn, simp }, { exact cast_dvd n_dvd (cast_ne_zero.mpr hn), }, end end nat section variables (M : Type*) [add_monoid M] [has_one M] [char_zero M] @[priority 100] -- see Note [lower instance priority] instance char_zero.infinite : infinite M := infinite.of_injective coe nat.cast_injective variable {M} @[field_simps] lemma two_ne_zero' : (2:M) β‰  0 := have ((2:β„•):M) β‰  0, from nat.cast_ne_zero.2 dec_trivial, by rwa [nat.cast_two] at this end section variables {R : Type*} [semiring R] [no_zero_divisors R] [char_zero R] @[simp] lemma add_self_eq_zero {a : R} : a + a = 0 ↔ a = 0 := by simp only [(two_mul a).symm, mul_eq_zero, two_ne_zero', false_or] @[simp] lemma bit0_eq_zero {a : R} : bit0 a = 0 ↔ a = 0 := add_self_eq_zero @[simp] lemma zero_eq_bit0 {a : R} : 0 = bit0 a ↔ a = 0 := by { rw [eq_comm], exact bit0_eq_zero } end section variables {R : Type*} [ring R] [no_zero_divisors R] [char_zero R] lemma neg_eq_self_iff {a : R} : -a = a ↔ a = 0 := neg_eq_iff_add_eq_zero.trans add_self_eq_zero lemma eq_neg_self_iff {a : R} : a = -a ↔ a = 0 := eq_neg_iff_add_eq_zero.trans add_self_eq_zero lemma nat_mul_inj {n : β„•} {a b : R} (h : (n : R) * a = (n : R) * b) : n = 0 ∨ a = b := begin rw [←sub_eq_zero, ←mul_sub, mul_eq_zero, sub_eq_zero] at h, exact_mod_cast h, end lemma nat_mul_inj' {n : β„•} {a b : R} (h : (n : R) * a = (n : R) * b) (w : n β‰  0) : a = b := by simpa [w] using nat_mul_inj h lemma bit0_injective : function.injective (bit0 : R β†’ R) := Ξ» a b h, begin dsimp [bit0] at h, simp only [(two_mul a).symm, (two_mul b).symm] at h, refine nat_mul_inj' _ two_ne_zero, exact_mod_cast h, end lemma bit1_injective : function.injective (bit1 : R β†’ R) := Ξ» a b h, begin simp only [bit1, add_left_inj] at h, exact bit0_injective h, end @[simp] lemma bit0_eq_bit0 {a b : R} : bit0 a = bit0 b ↔ a = b := bit0_injective.eq_iff @[simp] lemma bit1_eq_bit1 {a b : R} : bit1 a = bit1 b ↔ a = b := bit1_injective.eq_iff @[simp] lemma bit1_eq_one {a : R} : bit1 a = 1 ↔ a = 0 := by rw [show (1 : R) = bit1 0, by simp, bit1_eq_bit1] @[simp] lemma one_eq_bit1 {a : R} : 1 = bit1 a ↔ a = 0 := by { rw [eq_comm], exact bit1_eq_one } end section variables {R : Type*} [division_ring R] [char_zero R] @[simp] lemma half_add_self (a : R) : (a + a) / 2 = a := by rw [← mul_two, mul_div_cancel a two_ne_zero'] @[simp] lemma add_halves' (a : R) : a / 2 + a / 2 = a := by rw [← add_div, half_add_self] lemma sub_half (a : R) : a - a / 2 = a / 2 := by rw [sub_eq_iff_eq_add, add_halves'] lemma half_sub (a : R) : a / 2 - a = - (a / 2) := by rw [← neg_sub, sub_half] end namespace with_top instance {R : Type*} [add_monoid R] [has_one R] [char_zero R] : char_zero (with_top R) := { cast_injective := Ξ» m n h, by rwa [← coe_nat, ← coe_nat n, coe_eq_coe, nat.cast_inj] at h } end with_top section ring_hom variables {R S : Type*} [semiring R] [semiring S] lemma ring_hom.char_zero (Ο• : R β†’+* S) [hS : char_zero S] : char_zero R := ⟨λ a b h, char_zero.cast_injective (by rw [←map_nat_cast Ο•, ←map_nat_cast Ο•, h])⟩ lemma ring_hom.char_zero_iff {Ο• : R β†’+* S} (hΟ• : function.injective Ο•) : char_zero R ↔ char_zero S := ⟨λ hR, ⟨λ a b h, by rwa [←@nat.cast_inj R _ _ hR, ←hΟ•.eq_iff, map_nat_cast Ο•, map_nat_cast Ο•]⟩, Ξ» hS, by exactI Ο•.char_zero⟩ end ring_hom
260aaba216a3b5c369441df90ebca6e0acc8ebdf
87fd6b43d22688237c02b87c30d2a524f53bab24
/src/game/sets/sets_level03.lean
ce61a5ba9cf656923001720978f8705561c58d0b
[ "Apache-2.0" ]
permissive
grthomson/real-number-game
66142fedf0987db90f66daed52f9c8b42b70f909
8ddc15fdddc241c246653f7bb341df36e4e880a8
refs/heads/master
1,668,059,330,605
1,592,873,454,000
1,592,873,454,000
262,025,764
0
0
null
1,588,849,107,000
1,588,849,106,000
null
UTF-8
Lean
false
false
1,344
lean
import game.sets.sets_level02 -- hide namespace xena -- hide open_locale classical -- hide variable X : Type -- hide /- # Chapter 1 : Sets ## Level 3 : intersection (∩) -/ /- Now prove that for any two sets $A$ and $B$, $A ∩ B βŠ† A$. You will need to rewrite the following term: ``` mem_inter_iff : x ∈ A ∩ B ↔ x ∈ A ∧ x ∈ B ``` -/ /- Axiom : mem_inter_iff : x ∈ A ∩ B ↔ x ∈ A ∧ x ∈ B -/ /- Hint : Hint You need to start the same way as in the previous levels. Try and get yourself into a situation where you have a *hypothesis* `hAB : x ∈ A ∩ B` and then use `rw mem_inter_iff at hAB`. -/ /- Hint: A note on `x ∈ A ∧ x ∈ B β†’ x ∈ A` By convention, ∧ binds more tightly than β†’ (i.e. `x ∈ A ∧ x ∈ B β†’ x ∈ A` means `(x ∈ A ∧ x ∈ B) β†’ x ∈ A`) -/ /- Hint : Hint The `cases h with hP hQ` tactic turns `h : P ∧ Q` into `hP : P` and `hQ : Q` -/ /- Hint : The `tauto!` tactic The `tauto!` tactic solves logical goals -- for example it could easily solve this goal: ``` h : P ∧ Q ⊒ P ``` -/ /- Lemma If $A$ and $B$ are sets of any type $X$, then $$ A \cap B \subseteq A.$$ -/ theorem intersection_subset (A B : set X) : A ∩ B βŠ† A := begin rw subset_iff, intros x hx, rw mem_inter_iff at hx, tauto!, -- or cases, assumption end end xena -- hide
8e1bafea7cef56804723ce2515de5194fb264fc6
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/fin2.lean
8976a129fc023b96b9cba25c99f5c38d45da4522
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
2,926
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ open nat universes u /-- An alternate definition of `fin n` defined as an inductive type instead of a subtype of `nat`. This is useful for its induction principle and different definitional equalities. -/ inductive fin2 : β„• β†’ Type | fz {n} : fin2 (succ n) | fs {n} : fin2 n β†’ fin2 (succ n) namespace fin2 @[elab_as_eliminator] protected def cases' {n} {C : fin2 (succ n) β†’ Sort u} (H1 : C fz) (H2 : Ξ  n, C (fs n)) : Ξ  (i : fin2 (succ n)), C i | fz := H1 | (fs n) := H2 n def elim0 {C : fin2 0 β†’ Sort u} : Ξ  (i : fin2 0), C i. /-- convert a `fin2` into a `nat` -/ def to_nat : Ξ  {n}, fin2 n β†’ β„• | ._ (@fz n) := 0 | ._ (@fs n i) := succ (to_nat i) /-- convert a `nat` into a `fin2` if it is in range -/ def opt_of_nat : Ξ  {n} (k : β„•), option (fin2 n) | 0 _ := none | (succ n) 0 := some fz | (succ n) (succ k) := fs <$> @opt_of_nat n k /-- `i + k : fin2 (n + k)` when `i : fin2 n` and `k : β„•` -/ def add {n} (i : fin2 n) : Ξ  k, fin2 (n + k) | 0 := i | (succ k) := fs (add k) /-- `left k` is the embedding `fin2 n β†’ fin2 (k + n)` -/ def left (k) : Ξ  {n}, fin2 n β†’ fin2 (k + n) | ._ (@fz n) := fz | ._ (@fs n i) := fs (left i) /-- `insert_perm a` is a permutation of `fin2 n` with the following properties: * `insert_perm a i = i+1` if `i < a` * `insert_perm a a = 0` * `insert_perm a i = i` if `i > a` -/ def insert_perm : Ξ  {n}, fin2 n β†’ fin2 n β†’ fin2 n | ._ (@fz n) (@fz ._) := fz | ._ (@fz n) (@fs ._ j) := fs j | ._ (@fs (succ n) i) (@fz ._) := fs fz | ._ (@fs (succ n) i) (@fs ._ j) := match insert_perm i j with fz := fz | fs k := fs (fs k) end /-- `remap_left f k : fin2 (m + k) β†’ fin2 (n + k)` applies the function `f : fin2 m β†’ fin2 n` to inputs less than `m`, and leaves the right part on the right (that is, `remap_left f k (m + i) = n + i`). -/ def remap_left {m n} (f : fin2 m β†’ fin2 n) : Ξ  k, fin2 (m + k) β†’ fin2 (n + k) | 0 i := f i | (succ k) (@fz ._) := fz | (succ k) (@fs ._ i) := fs (remap_left _ i) /-- This is a simple type class inference prover for proof obligations of the form `m < n` where `m n : β„•`. -/ class is_lt (m n : β„•) := (h : m < n) instance is_lt.zero (n) : is_lt 0 (succ n) := ⟨succ_pos _⟩ instance is_lt.succ (m n) [l : is_lt m n] : is_lt (succ m) (succ n) := ⟨succ_lt_succ l.h⟩ /-- Use type class inference to infer the boundedness proof, so that we can directly convert a `nat` into a `fin2 n`. This supports notation like `&1 : fin 3`. -/ def of_nat' : Ξ  {n} m [is_lt m n], fin2 n | 0 m ⟨h⟩ := absurd h (not_lt_zero _) | (succ n) 0 ⟨h⟩ := fz | (succ n) (succ m) ⟨h⟩ := fs (@of_nat' n m ⟨lt_of_succ_lt_succ h⟩) local prefix `&`:max := of_nat' end fin2
a13787d4a9700032f01bef84e8a904ca9999cc6a
c76cc4eaee3c806716844e49b5175747d1aa170a
/src/problem_sheet_two.lean
b71b740762556566b444c4473b319a1c14c36dcf
[]
no_license
ImperialCollegeLondon/M40002
0fb55848adbb0d8bc4a65ca5d7ed6edd18764c28
a499db70323bd5ccae954c680ec9afbf15ffacca
refs/heads/master
1,674,878,059,748
1,607,624,828,000
1,607,624,828,000
309,696,750
3
0
null
null
null
null
UTF-8
Lean
false
false
5,924
lean
import tactic import data.real.basic import data.nat.choose.sum -- binomial theorem import data.real.ereal import data.pnat.basic section Q2 /-! # Q2 -/ /- 2. Fix nonempty sets S_n βŠ† R, n= 1,2,3,.... Prove that sup{sup S1,sup S2,sup S3,...} = sup(⋃_{n=1}^{infty} S_n), in the sense that if either exists then so does the other, and they are equal. -/ /- Let's first answer a better question with fewer restrictions, using extended reals. Then Sup {Sup (S_i) : i ∈ I} = Sup (⋃_{i ∈ I} S_i) is *always* true -/ -- this comes out really cleanly, we just allow infinity example (I : Type) (S : I β†’ set (ereal)) : Sup (set.range (Ξ» i, Sup (S i))) = Sup (⋃ i, S i) := begin sorry end -- useful helper lemma lemma exists_lub (S : set ℝ) : S.nonempty ∧ (upper_bounds S).nonempty β†’ (βˆƒ B, is_lub S B) := begin sorry end -- now the actual question -- "All the S_i have a sup and the set {sup S1, sup S2, ...} has a sup, if and -- only if the union of the S_i has a sup" theorem Q2a (S : β„•+ β†’ set ℝ) (hS : βˆ€ i : β„•+, (S i).nonempty) : (βˆ€ n : β„•+, βˆƒ B, is_lub (S n) B) ∧ (βˆƒ B, is_lub (set.range (Ξ» i, Sup (S i))) B) ↔ βˆƒ B, is_lub (⋃ i, S i) B := begin sorry end -- assuming both sides make sense, prove the sups are equal theorem Q2b (S : β„•+ β†’ set ℝ) (hS : βˆ€ i : β„•+, (S i).nonempty) (hLHS: βˆ€ n : β„•+, βˆƒ B, is_lub (S n) B) (hLHS' : βˆƒ B, is_lub (set.range (Ξ» i, Sup (S i))) B) (hRHS : βˆƒ B, is_lub (⋃ i, S i) B) : Sup (set.range (Ξ» i, Sup (S i))) = Sup (⋃ i, S i) := begin sorry end end Q2 /-! # Q3 Take bounded, nonempty `S, T βŠ† ℝ`. Define `S + T := { s + t : s ∈ S, t ∈ T}`. Prove `sup(S + T) = sup(S) + sup(T)` -/ -- useful for rewriting theorem is_lub_def {S : set ℝ} {a : ℝ} : is_lub S a ↔ a ∈ upper_bounds S ∧ βˆ€ x, x ∈ upper_bounds S β†’ a ≀ x := begin refl end #check mem_upper_bounds -- a ∈ upper_bounds S ↔ βˆ€ x, x ∈ S β†’ x ≀ a /- Useful tactics for this one: push_neg, specialize, have -/ theorem useful_lemma {S : set ℝ} {a : ℝ} (haS : is_lub S a) (t : ℝ) (ht : t < a) : βˆƒ s, s ∈ S ∧ t < s := begin sorry end /- Useful tactics for this one: `rcases h with ⟨s, t, hsS, htT, rfl⟩` if h : x ∈ S + T `linarith` `by_contra` `set Ξ΅ := a + b - x with hΞ΅` -/ theorem Q3 (S T : set ℝ) (a b : ℝ) : is_lub S a β†’ is_lub T b β†’ is_lub (S + T) (a + b) := begin sorry end /-! # Q4 Fix `a ∈ (0,∞)` and `n : β„•`. We will prove `βˆƒ x : ℝ, x^n = a`. -/ section Q4 noncomputable theory section one -- this first section, a and n are going to be variables variables {a : ℝ} (ha : 0 < a) {n : β„•} (hn : 0 < n) include ha hn -- Note: We do part 1 after parts 2,3,4 because on the problem sheet -- parts 2,3,4 are written for the specific x=Sup(S) but -- part 4 for x needs part 3 for 1/x so you can't use part 3 to -- do part 4 the way it's been set up on the sheet. We prove -- 2,3,4 for arbitrary 0 ≀ x (or 0 < x for 4) /- 2) For `Ξ΅ ∈ (0,1)` and arbitrary `x β‰₯ 0` show `(x+Ξ΅)ⁿ ≀ x^n + Ξ΅[(x + 1)ⁿ βˆ’ xⁿ].` (Hint: multiply out.) -/ theorem part2 {x : ℝ} (x_nonneg : 0 ≀ x) (Ξ΅ : ℝ) (hΞ΅0 : 0 < Ξ΅) (hΞ΅1 : Ξ΅ < 1) : (x + Ξ΅)^n ≀ x^n + Ξ΅ * ((x + 1)^n - x^n) := begin sorry, end /- 3) Hence show that if `x β‰₯ 0` and `xⁿ < a` then `βˆƒ Ξ΅ ∈ (0,1)` such that `(x+Ξ΅)ⁿ < a.` (*) -/ theorem part3 {x : ℝ} (x_nonneg : 0 ≀ x) (h : x ^ n < a) : βˆƒ Ξ΅ : ℝ, 0 < Ξ΅ ∧ Ξ΅ < 1 ∧ (x+Ξ΅)^n < a := begin sorry end /- 4) If `x > 0` is arbitrary and `xⁿ > a`, deduce from (βˆ—) (i.e. part 3) that `βˆƒ Ξ΅ ∈ (0,1)` such that `(1/x+Ξ΅)ⁿ < 1/a`. (βˆ—βˆ—) -/ theorem part4 {x : ℝ} (hx : 0 < x) (h : a < x^n) : βˆƒ Ξ΅ : ℝ, 0 < Ξ΅ ∧ Ξ΅ < 1 ∧ (1/x + Ξ΅)^n < 1/a := begin sorry, end end one section two -- in this section, a and n are going to be fixed parameters parameters {a : ℝ} (ha : 0 < a) {n : β„•} (hn : 0 < n) include ha hn /- 1) Set `Sₐ := {s ∈ [0,∞) : s^n < a}` and show `Sₐ` is nonempty and bounded above, so we may define `x := sup Sₐ` BUT WE WILL NOT DEFINE x TO BE THIS YET. -/ def S := {s : ℝ | 0 ≀ s ∧ s ^ n < a} theorem part1 : (βˆƒ s : ℝ, s ∈ S) ∧ (βˆƒ B : ℝ, βˆ€ s ∈ S, s ≀ B ) := begin sorry end def x := Sup S -- x is a least upper bound for X theorem is_lub_x : is_lub S x := begin sorry, end lemma x_nonneg : 0 ≀ x := begin rcases is_lub_x with ⟨h, -⟩, apply h, split, refl, convert ha, simp [hn], end lemma easy (h : a < x^n) : x β‰  0 := begin intro hx, rw hx at h, suffices : a < 0, linarith, convert h, symmetry, simp [hn], end /- 5) Deduce contradictions from (βˆ—) (part 3) and (βˆ—βˆ—) (part 4) to show that `xⁿ = a`. -/ -- lemma le_of_pow_le_pow (n : β„•) (hn : 0 < n) (x y : ℝ) (h : x^n ≀ y^n) : x ≀ y := -- begin -- by_contra hxy, -- push_neg at hxy, -- sorry, -- -- have h2 : pow_lt_pow -- end theorem part5 : x^n = a := begin sorry, end end two end Q4 /-! # Q6 -/ -- We introduce the usual mathematical notation for absolute value local notation `|` x `|` := abs x /- Useful for this one: `unfold`, `split_ifs` if you want to prove from first principles, or guessing the name of the library function if you want to use the library. -/ theorem Q6a (x y : ℝ) : | x + y | ≀ | x | + | y | := begin sorry end -- all the rest you're supposed to do using Q6a somehow: -- `simp` and `linarith` are useful. theorem Q6b (x y : ℝ) : |x + y| β‰₯ |x| - |y| := begin sorry end theorem Q6c (x y : ℝ) : |x + y| β‰₯ |y| - |x| := begin sorry end theorem Q6d (x y : ℝ) : |x - y| β‰₯ | |x| - |y| | := begin sorry, end theorem Q6e (x y : ℝ) : |x| ≀ |y| + |x - y| := begin sorry end theorem Q6f (x y : ℝ) : |x| β‰₯ |y| - |x - y| := begin sorry end theorem Q6g (x y z : ℝ) : |x - y| ≀ |x - z| + |y - z| := begin sorry end
088c61c01033e9f1ca7d696b102b551449780711
8e6cad62ec62c6c348e5faaa3c3f2079012bdd69
/src/algebra/lie/solvable.lean
c380006b32ffd9cec4934200aff6948474fc6be3
[ "Apache-2.0" ]
permissive
benjamindavidson/mathlib
8cc81c865aa8e7cf4462245f58d35ae9a56b150d
fad44b9f670670d87c8e25ff9cdf63af87ad731e
refs/heads/master
1,679,545,578,362
1,615,343,014,000
1,615,343,014,000
312,926,983
0
0
Apache-2.0
1,615,360,301,000
1,605,399,418,000
Lean
UTF-8
Lean
false
false
14,007
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.ideal_operations import algebra.lie.abelian import order.preorder_hom /-! # Solvable Lie algebras Like groups, Lie algebras admit a natural concept of solvability. We define this here via the derived series and prove some related results. We also define the radical of a Lie algebra and prove that it is solvable when the Lie algebra is Noetherian. ## Main definitions * `lie_algebra.derived_series_of_ideal` * `lie_algebra.derived_series` * `lie_algebra.is_solvable` * `lie_algebra.is_solvable_add` * `lie_algebra.radical` * `lie_algebra.radical_is_solvable` * `lie_algebra.derived_length_of_ideal` * `lie_algebra.derived_length` * `lie_algebra.derived_abelian_of_ideal` ## Tags lie algebra, derived series, derived length, solvable, radical -/ universes u v w w₁ wβ‚‚ variables (R : Type u) (L : Type v) (M : Type w) {L' : Type w₁} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] variables (I J : lie_ideal R L) {f : L' →ₗ⁅R⁆ L} 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 (k : β„•) : lie_ideal R L β†’ lie_ideal R L := (Ξ» I, ⁅I, I⁆)^[k] @[simp] lemma derived_series_of_ideal_zero : derived_series_of_ideal R L 0 I = I := rfl @[simp] lemma derived_series_of_ideal_succ (k : β„•) : derived_series_of_ideal R L (k + 1) I = ⁅derived_series_of_ideal R L k I, derived_series_of_ideal R L k I⁆ := function.iterate_succ_apply' (Ξ» I, ⁅I, I⁆) k I /-- The derived series of Lie ideals of a Lie algebra. -/ abbreviation derived_series (k : β„•) : lie_ideal R L := derived_series_of_ideal R L k ⊀ lemma derived_series_def (k : β„•) : derived_series R L k = derived_series_of_ideal R L k ⊀ := rfl variables {R L} local notation `D` := derived_series_of_ideal R L lemma derived_series_of_ideal_add (k l : β„•) : D (k + l) I = D k (D l I) := begin induction k with k ih, { rw [zero_add, derived_series_of_ideal_zero], }, { rw [nat.succ_add k l, derived_series_of_ideal_succ, derived_series_of_ideal_succ, ih], }, end lemma derived_series_of_ideal_le {I J : lie_ideal R L} {k l : β„•} (h₁ : I ≀ J) (hβ‚‚ : l ≀ k) : D k I ≀ D l J := begin revert l, induction k with k ih; intros l hβ‚‚, { rw nat.le_zero_iff at hβ‚‚, rw [hβ‚‚, derived_series_of_ideal_zero], exact h₁, }, { have h : l = k.succ ∨ l ≀ k, by rwa [le_iff_eq_or_lt, nat.lt_succ_iff] at hβ‚‚, cases h, { rw [h, derived_series_of_ideal_succ, derived_series_of_ideal_succ], exact lie_submodule.mono_lie _ _ _ _ (ih (le_refl k)) (ih (le_refl k)), }, { rw derived_series_of_ideal_succ, exact le_trans (lie_submodule.lie_le_left _ _) (ih h), }, }, end lemma derived_series_of_ideal_succ_le (k : β„•) : D (k + 1) I ≀ D k I := derived_series_of_ideal_le (le_refl I) k.le_succ lemma derived_series_of_ideal_le_self (k : β„•) : D k I ≀ I := derived_series_of_ideal_le (le_refl I) (zero_le k) lemma derived_series_of_ideal_mono {I J : lie_ideal R L} (h : I ≀ J) (k : β„•) : D k I ≀ D k J := derived_series_of_ideal_le h (le_refl k) lemma derived_series_of_ideal_antimono {k l : β„•} (h : l ≀ k) : D k I ≀ D l I := derived_series_of_ideal_le (le_refl I) h lemma derived_series_of_ideal_add_le_add (J : lie_ideal R L) (k l : β„•) : D (k + l) (I + J) ≀ (D k I) + (D l J) := begin let D₁ : lie_ideal R L β†’β‚˜ lie_ideal R L := { to_fun := Ξ» I, ⁅I, I⁆, monotone' := Ξ» I J h, lie_submodule.mono_lie I J I J h h, }, have h₁ : βˆ€ (I J : lie_ideal R L), D₁ (I βŠ” J) ≀ (D₁ I) βŠ” J, { simp [lie_submodule.lie_le_right, lie_submodule.lie_le_left, le_sup_right_of_le], }, rw ← D₁.iterate_sup_le_sup_iff at h₁, exact h₁ k l I J, end lemma derived_series_of_bot_eq_bot (k : β„•) : derived_series_of_ideal R L k βŠ₯ = βŠ₯ := by { rw eq_bot_iff, exact derived_series_of_ideal_le_self βŠ₯ k, } lemma abelian_iff_derived_one_eq_bot : is_lie_abelian I ↔ derived_series_of_ideal R L 1 I = βŠ₯ := by rw [derived_series_of_ideal_succ, derived_series_of_ideal_zero, lie_submodule.lie_abelian_iff_lie_self_eq_bot] lemma abelian_iff_derived_succ_eq_bot (I : lie_ideal R L) (k : β„•) : is_lie_abelian (derived_series_of_ideal R L k I) ↔ derived_series_of_ideal R L (k + 1) I = βŠ₯ := by rw [add_comm, derived_series_of_ideal_add I 1 k, abelian_iff_derived_one_eq_bot] end lie_algebra namespace lie_ideal open lie_algebra variables {R L} lemma derived_series_eq_derived_series_of_ideal_comap (k : β„•) : derived_series R I k = (derived_series_of_ideal R L k I).comap I.incl := begin induction k with k ih, { simp only [derived_series_def, comap_incl_self, derived_series_of_ideal_zero], }, { simp only [derived_series_def, derived_series_of_ideal_succ] at ⊒ ih, rw ih, exact comap_bracket_incl_of_le I (derived_series_of_ideal_le_self I k) (derived_series_of_ideal_le_self I k), }, end lemma derived_series_eq_derived_series_of_ideal_map (k : β„•) : (derived_series R I k).map I.incl = derived_series_of_ideal R L k I := by { rw [derived_series_eq_derived_series_of_ideal_comap, map_comap_incl, inf_eq_right], apply derived_series_of_ideal_le_self, } lemma derived_series_eq_bot_iff (k : β„•) : derived_series R I k = βŠ₯ ↔ derived_series_of_ideal R L k I = βŠ₯ := by rw [← derived_series_eq_derived_series_of_ideal_map, I.incl.map_bot_iff, ker_incl, eq_bot_iff] lemma derived_series_add_eq_bot {k l : β„•} {I J : lie_ideal R L} (hI : derived_series R I k = βŠ₯) (hJ : derived_series R J l = βŠ₯) : derived_series R β†₯(I + J) (k + l) = βŠ₯ := begin rw lie_ideal.derived_series_eq_bot_iff at hI hJ ⊒, rw ← le_bot_iff, let D := derived_series_of_ideal R L, change D k I = βŠ₯ at hI, change D l J = βŠ₯ at hJ, calc D (k + l) (I + J) ≀ (D k I) + (D l J) : derived_series_of_ideal_add_le_add I J k l ... ≀ βŠ₯ : by { rw [hI, hJ], simp, }, end lemma derived_series_map_le (k : β„•) : (derived_series R L' k).map f ≀ derived_series R L k := begin induction k with k ih, { simp only [derived_series_def, derived_series_of_ideal_zero, le_top], }, { simp only [derived_series_def, derived_series_of_ideal_succ] at ih ⊒, exact le_trans (map_bracket_le f) (lie_submodule.mono_lie _ _ _ _ ih ih), }, end lemma derived_series_map_eq (k : β„•) (h : function.surjective f) : (derived_series R L' k).map f = derived_series R L k := begin have h' : (⊀ : lie_ideal R L').map f = ⊀, { exact f.ideal_range_eq_top_of_surjective h, }, induction k with k ih, { exact h', }, { simp only [derived_series_def, map_bracket_eq f h, ih, derived_series_of_ideal_succ], }, end end lie_ideal namespace lie_algebra /-- A Lie algebra is solvable if its derived series reaches 0 (in a finite number of steps). -/ class is_solvable : Prop := (solvable : βˆƒ k, derived_series R L k = βŠ₯) instance is_solvable_bot : is_solvable R β†₯(βŠ₯ : lie_ideal R L) := ⟨⟨0, @subsingleton.elim _ lie_ideal.subsingleton_of_bot _ βŠ₯⟩⟩ instance is_solvable_add {I J : lie_ideal R L} [hI : is_solvable R I] [hJ : is_solvable R J] : is_solvable R β†₯(I + J) := begin tactic.unfreeze_local_instances, obtain ⟨k, hk⟩ := hI, obtain ⟨l, hl⟩ := hJ, exact ⟨⟨k+l, lie_ideal.derived_series_add_eq_bot hk hl⟩⟩, end end lie_algebra variables {R L} namespace function open lie_algebra lemma injective.lie_algebra_is_solvable [h₁ : is_solvable R L] (hβ‚‚ : injective f) : is_solvable R L' := begin tactic.unfreeze_local_instances, obtain ⟨k, hk⟩ := h₁, use k, apply lie_ideal.bot_of_map_eq_bot hβ‚‚, rw [eq_bot_iff, ← hk], apply lie_ideal.derived_series_map_le, end lemma surjective.lie_algebra_is_solvable [h₁ : is_solvable R L'] (hβ‚‚ : surjective f) : is_solvable R L := begin tactic.unfreeze_local_instances, obtain ⟨k, hk⟩ := h₁, use k, rw [← lie_ideal.derived_series_map_eq k hβ‚‚, hk], simp only [lie_hom.map_bot_iff, bot_le], end end function lemma lie_hom.is_solvable_range (f : L' →ₗ⁅R⁆ L) [h : lie_algebra.is_solvable R L'] : lie_algebra.is_solvable R f.range := f.surjective_range_restrict.lie_algebra_is_solvable namespace lie_algebra lemma solvable_iff_equiv_solvable (e : L' ≃ₗ⁅R⁆ L) : is_solvable R L' ↔ is_solvable R L := begin split; introsI h, { exact e.symm.injective.lie_algebra_is_solvable, }, { exact e.injective.lie_algebra_is_solvable, }, end lemma le_solvable_ideal_solvable {I J : lie_ideal R L} (h₁ : I ≀ J) (hβ‚‚ : is_solvable R J) : is_solvable R I := (lie_ideal.hom_of_le_injective h₁).lie_algebra_is_solvable variables (R L) @[priority 100] instance of_abelian_is_solvable [is_lie_abelian L] : is_solvable R L := begin use 1, rw [← abelian_iff_derived_one_eq_bot, lie_abelian_iff_equiv_lie_abelian lie_ideal.top_equiv_self], apply_instance, end /-- The (solvable) radical of Lie algebra is the `Sup` of all solvable ideals. -/ def radical := Sup { I : lie_ideal R L | is_solvable R I } /-- The radical of a Noetherian Lie algebra is solvable. -/ instance radical_is_solvable [is_noetherian R L] : is_solvable R (radical R L) := begin have hwf := lie_submodule.well_founded_of_noetherian R L L, rw ← complete_lattice.is_sup_closed_compact_iff_well_founded at hwf, refine hwf { I : lie_ideal R L | is_solvable R I } _ _, { use βŠ₯, exact lie_algebra.is_solvable_bot R L, }, { intros I J hI hJ, apply lie_algebra.is_solvable_add R L; [exact hI, exact hJ], }, end /-- The `β†’` direction of this lemma is actually true without the `is_noetherian` assumption. -/ lemma lie_ideal.solvable_iff_le_radical [is_noetherian R L] (I : lie_ideal R L) : is_solvable R I ↔ I ≀ radical R L := begin split; intros h, { exact le_Sup h, }, { apply le_solvable_ideal_solvable h, apply_instance, }, end lemma center_le_radical : center R L ≀ radical R L := have h : is_solvable R (center R L), { apply_instance, }, le_Sup h /-- Given a solvable Lie ideal `I` with derived series `I = Dβ‚€ β‰₯ D₁ β‰₯ β‹― β‰₯ Dβ‚– = βŠ₯`, this is the natural number `k` (the number of inclusions). For a non-solvable ideal, the value is 0. -/ noncomputable def derived_length_of_ideal (I : lie_ideal R L) : β„• := Inf {k | derived_series_of_ideal R L k I = βŠ₯} /-- The derived length of a Lie algebra is the derived length of its 'top' Lie ideal. See also `lie_algebra.derived_length_eq_derived_length_of_ideal`. -/ noncomputable abbreviation derived_length : β„• := derived_length_of_ideal R L ⊀ lemma derived_series_of_derived_length_succ (I : lie_ideal R L) (k : β„•) : derived_length_of_ideal R L I = k + 1 ↔ is_lie_abelian (derived_series_of_ideal R L k I) ∧ derived_series_of_ideal R L k I β‰  βŠ₯ := begin rw abelian_iff_derived_succ_eq_bot, let s := {k | derived_series_of_ideal R L k I = βŠ₯}, change Inf s = k + 1 ↔ k + 1 ∈ s ∧ k βˆ‰ s, have hs : βˆ€ (k₁ kβ‚‚ : β„•), k₁ ≀ kβ‚‚ β†’ k₁ ∈ s β†’ kβ‚‚ ∈ s, { intros k₁ kβ‚‚ h₁₂ h₁, suffices : derived_series_of_ideal R L kβ‚‚ I ≀ βŠ₯, { exact eq_bot_iff.mpr this, }, change derived_series_of_ideal R L k₁ I = βŠ₯ at h₁, rw ← h₁, exact derived_series_of_ideal_antimono I h₁₂, }, exact nat.Inf_upward_closed_eq_succ_iff hs k, end lemma derived_length_eq_derived_length_of_ideal (I : lie_ideal R L) : derived_length R I = derived_length_of_ideal R L I := begin let s₁ := {k | derived_series R I k = βŠ₯}, let sβ‚‚ := {k | derived_series_of_ideal R L k I = βŠ₯}, change Inf s₁ = Inf sβ‚‚, congr, ext k, exact I.derived_series_eq_bot_iff k, end variables {R L} /-- Given a solvable Lie ideal `I` with derived series `I = Dβ‚€ β‰₯ D₁ β‰₯ β‹― β‰₯ Dβ‚– = βŠ₯`, this is the `k-1`th term in the derived series (and is therefore an Abelian ideal contained in `I`). For a non-solvable ideal, this is the zero ideal, `βŠ₯`. -/ noncomputable def derived_abelian_of_ideal (I : lie_ideal R L) : lie_ideal R L := match derived_length_of_ideal R L I with | 0 := βŠ₯ | k + 1 := derived_series_of_ideal R L k I end lemma abelian_derived_abelian_of_ideal (I : lie_ideal R L) : is_lie_abelian (derived_abelian_of_ideal I) := begin dunfold derived_abelian_of_ideal, cases h : derived_length_of_ideal R L I with k, { exact is_lie_abelian_bot R L, }, { rw derived_series_of_derived_length_succ at h, exact h.1, }, end lemma derived_length_zero (I : lie_ideal R L) [hI : is_solvable R I] : derived_length_of_ideal R L I = 0 ↔ I = βŠ₯ := begin let s := {k | derived_series_of_ideal R L k I = βŠ₯}, change Inf s = 0 ↔ _, have hne : s β‰  βˆ…, { rw set.ne_empty_iff_nonempty, tactic.unfreeze_local_instances, obtain ⟨k, hk⟩ := hI, use k, rw [derived_series_def, lie_ideal.derived_series_eq_bot_iff] at hk, exact hk, }, simp [hne], end lemma abelian_of_solvable_ideal_eq_bot_iff (I : lie_ideal R L) [h : is_solvable R I] : derived_abelian_of_ideal I = βŠ₯ ↔ I = βŠ₯ := begin dunfold derived_abelian_of_ideal, cases h : derived_length_of_ideal R L I with k, { rw derived_length_zero at h, rw h, refl, }, { obtain ⟨h₁, hβ‚‚βŸ© := (derived_series_of_derived_length_succ R L I k).mp h, have h₃ : I β‰  βŠ₯, { intros contra, apply hβ‚‚, rw contra, apply derived_series_of_bot_eq_bot, }, change derived_series_of_ideal R L k I = βŠ₯ ↔ I = βŠ₯, split; contradiction, }, end end lie_algebra
5693b2ebfa12407acf0726463a48cb070f0be8ed
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/ac_tactics_auto.lean
7958b089f1bc93f8fadabdace4eda5b22ee02c4d
[]
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
394
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 namespace Mathlib namespace tactic /- (flat_assoc op assoc e) -/ /- (perm_ac op assoc comm e1 e2) Try to construct a proof that e1 = e2 modulo AC -/ end Mathlib
ca6080e5de570315697559cfffcd22e151fdf519
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/hom/group_action.lean
4bc204da1c7d45e341ce9dfdca1fa658f7b076c5
[ "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
14,356
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.group_ring_action.invariant import group_theory.group_action.defs import group_theory.subgroup.basic /-! # Equivariant homomorphisms ## Main definitions * `mul_action_hom M X Y`, the type of equivariant functions from `X` to `Y`, where `M` is a monoid that acts on the types `X` and `Y`. * `distrib_mul_action_hom M A B`, the type of equivariant additive monoid homomorphisms from `A` to `B`, where `M` is a monoid that acts on the additive monoids `A` and `B`. * `mul_semiring_action_hom M R S`, the type of equivariant ring homomorphisms from `R` to `S`, where `M` is a monoid that acts on the rings `R` and `S`. The above types have corresponding classes: * `smul_hom_class F M X Y` states that `F` is a type of bundled `X β†’ Y` homs preserving scalar multiplication by `M` * `distrib_mul_action_hom_class F M A B` states that `F` is a type of bundled `A β†’ B` homs preserving the additive monoid structure and scalar multiplication by `M` * `mul_semiring_action_hom_class F M R S` states that `F` is a type of bundled `R β†’ S` homs preserving the ring structure and scalar multiplication by `M` ## Notations * `X β†’[M] Y` is `mul_action_hom M X Y`. * `A β†’+[M] B` is `distrib_mul_action_hom M A B`. * `R β†’+*[M] S` is `mul_semiring_action_hom M R S`. -/ variables (M' : Type*) variables (X : Type*) [has_smul M' X] variables (Y : Type*) [has_smul M' Y] variables (Z : Type*) [has_smul M' Z] variables (M : Type*) [monoid M] variables (A : Type*) [add_monoid A] [distrib_mul_action M A] variables (A' : Type*) [add_group A'] [distrib_mul_action M A'] variables (B : Type*) [add_monoid B] [distrib_mul_action M B] variables (B' : Type*) [add_group B'] [distrib_mul_action M B'] variables (C : Type*) [add_monoid C] [distrib_mul_action M C] variables (R : Type*) [semiring R] [mul_semiring_action M R] variables (R' : Type*) [ring R'] [mul_semiring_action M R'] variables (S : Type*) [semiring S] [mul_semiring_action M S] variables (S' : Type*) [ring S'] [mul_semiring_action M S'] variables (T : Type*) [semiring T] [mul_semiring_action M T] variables (G : Type*) [group G] (H : subgroup G) set_option old_structure_cmd true /-- Equivariant functions. -/ @[nolint has_nonempty_instance] structure mul_action_hom := (to_fun : X β†’ Y) (map_smul' : βˆ€ (m : M') (x : X), to_fun (m β€’ x) = m β€’ to_fun x) notation (name := mul_action_hom) X ` β†’[`:25 M:25 `] `:0 Y:0 := mul_action_hom M X Y /-- `smul_hom_class F M X Y` states that `F` is a type of morphisms preserving scalar multiplication by `M`. You should extend this class when you extend `mul_action_hom`. -/ class smul_hom_class (F : Type*) (M X Y : out_param $ Type*) [has_smul M X] [has_smul M Y] extends fun_like F X (Ξ» _, Y) := (map_smul : βˆ€ (f : F) (c : M) (x : X), f (c β€’ x) = c β€’ f x) -- `M` becomes a metavariable but it's an `out_param` so it's not a problem. attribute [nolint dangerous_instance] smul_hom_class.to_fun_like export smul_hom_class (map_smul) attribute [simp] map_smul namespace mul_action_hom instance : has_coe_to_fun (X β†’[M'] Y) (Ξ» _, X β†’ Y) := ⟨mul_action_hom.to_fun⟩ instance : smul_hom_class (X β†’[M'] Y) M' X Y := { coe := mul_action_hom.to_fun, coe_injective' := Ξ» f g h, by cases f; cases g; congr', map_smul := mul_action_hom.map_smul' } variables {M M' X Y} protected lemma map_smul (f : X β†’[M'] Y) (m : M') (x : X) : f (m β€’ x) = m β€’ f x := map_smul _ _ _ @[ext] theorem ext : βˆ€ {f g : X β†’[M'] Y}, (βˆ€ x, f x = g x) β†’ f = g := fun_like.ext theorem ext_iff {f g : X β†’[M'] Y} : f = g ↔ βˆ€ x, f x = g x := fun_like.ext_iff protected lemma congr_fun {f g : X β†’[M'] Y} (h : f = g) (x : X) : f x = g x := fun_like.congr_fun h _ variables (M M') {X} /-- The identity map as an equivariant map. -/ protected def id : X β†’[M'] X := ⟨id, Ξ» _ _, rfl⟩ @[simp] lemma id_apply (x : X) : mul_action_hom.id M' x = x := rfl variables {M M' X Y Z} /-- Composition of two equivariant maps. -/ def comp (g : Y β†’[M'] Z) (f : X β†’[M'] Y) : X β†’[M'] Z := ⟨g ∘ f, Ξ» m x, calc g (f (m β€’ x)) = g (m β€’ f x) : by rw f.map_smul ... = m β€’ g (f x) : g.map_smul _ _⟩ @[simp] lemma comp_apply (g : Y β†’[M'] Z) (f : X β†’[M'] Y) (x : X) : g.comp f x = g (f x) := rfl @[simp] lemma id_comp (f : X β†’[M'] Y) : (mul_action_hom.id M').comp f = f := ext $ Ξ» x, by rw [comp_apply, id_apply] @[simp] lemma comp_id (f : X β†’[M'] Y) : f.comp (mul_action_hom.id M') = f := ext $ Ξ» x, by rw [comp_apply, id_apply] variables {A B} /-- The inverse of a bijective equivariant map is equivariant. -/ @[simps] def inverse (f : A β†’[M] B) (g : B β†’ A) (h₁ : function.left_inverse g f) (hβ‚‚ : function.right_inverse g f) : B β†’[M] A := { to_fun := g, map_smul' := Ξ» m x, calc g (m β€’ x) = g (m β€’ (f (g x))) : by rw hβ‚‚ ... = g (f (m β€’ (g x))) : by rw f.map_smul ... = m β€’ g x : by rw h₁, } end mul_action_hom /-- Equivariant additive monoid homomorphisms. -/ structure distrib_mul_action_hom extends A β†’[M] B, A β†’+ B. /-- Reinterpret an equivariant additive monoid homomorphism as an additive monoid homomorphism. -/ add_decl_doc distrib_mul_action_hom.to_add_monoid_hom /-- Reinterpret an equivariant additive monoid homomorphism as an equivariant function. -/ add_decl_doc distrib_mul_action_hom.to_mul_action_hom notation A ` β†’+[`:25 M:25 `] `:0 B:0 := distrib_mul_action_hom M A B /-- `distrib_mul_action_hom_class F M A B` states that `F` is a type of morphisms preserving the additive monoid structure and scalar multiplication by `M`. You should extend this class when you extend `distrib_mul_action_hom`. -/ class distrib_mul_action_hom_class (F : Type*) (M A B : out_param $ Type*) [monoid M] [add_monoid A] [add_monoid B] [distrib_mul_action M A] [distrib_mul_action M B] extends smul_hom_class F M A B, add_monoid_hom_class F A B -- `M` becomes a metavariable but it's an `out_param` so it's not a problem. attribute [nolint dangerous_instance] distrib_mul_action_hom_class.to_add_monoid_hom_class namespace distrib_mul_action_hom instance has_coe : has_coe (A β†’+[M] B) (A β†’+ B) := ⟨to_add_monoid_hom⟩ instance has_coe' : has_coe (A β†’+[M] B) (A β†’[M] B) := ⟨to_mul_action_hom⟩ instance : has_coe_to_fun (A β†’+[M] B) (Ξ» _, A β†’ B) := ⟨to_fun⟩ instance : distrib_mul_action_hom_class (A β†’+[M] B) M A B := { coe := distrib_mul_action_hom.to_fun, coe_injective' := Ξ» f g h, by cases f; cases g; congr', map_smul := distrib_mul_action_hom.map_smul', map_zero := distrib_mul_action_hom.map_zero', map_add := distrib_mul_action_hom.map_add' } variables {M A B} @[simp] lemma to_fun_eq_coe (f : A β†’+[M] B) : f.to_fun = ⇑f := rfl @[norm_cast] lemma coe_fn_coe (f : A β†’+[M] B) : ((f : A β†’+ B) : A β†’ B) = f := rfl @[norm_cast] lemma coe_fn_coe' (f : A β†’+[M] B) : ((f : A β†’[M] B) : A β†’ B) = f := rfl @[ext] theorem ext : βˆ€ {f g : A β†’+[M] B}, (βˆ€ x, f x = g x) β†’ f = g := fun_like.ext theorem ext_iff {f g : A β†’+[M] B} : f = g ↔ βˆ€ x, f x = g x := fun_like.ext_iff protected lemma congr_fun {f g : A β†’+[M] B} (h : f = g) (x : A) : f x = g x := fun_like.congr_fun h _ lemma to_mul_action_hom_injective {f g : A β†’+[M] B} (h : (f : A β†’[M] B) = (g : A β†’[M] B)) : f = g := by { ext a, exact mul_action_hom.congr_fun h a, } lemma to_add_monoid_hom_injective {f g : A β†’+[M] B} (h : (f : A β†’+ B) = (g : A β†’+ B)) : f = g := by { ext a, exact add_monoid_hom.congr_fun h a, } protected lemma map_zero (f : A β†’+[M] B) : f 0 = 0 := map_zero _ protected lemma map_add (f : A β†’+[M] B) (x y : A) : f (x + y) = f x + f y := map_add _ _ _ protected lemma map_neg (f : A' β†’+[M] B') (x : A') : f (-x) = -f x := map_neg _ _ protected lemma map_sub (f : A' β†’+[M] B') (x y : A') : f (x - y) = f x - f y := map_sub _ _ _ protected lemma map_smul (f : A β†’+[M] B) (m : M) (x : A) : f (m β€’ x) = m β€’ f x := map_smul _ _ _ variables (M) {A} /-- The identity map as an equivariant additive monoid homomorphism. -/ protected def id : A β†’+[M] A := ⟨id, Ξ» _ _, rfl, rfl, Ξ» _ _, rfl⟩ @[simp] lemma id_apply (x : A) : distrib_mul_action_hom.id M x = x := rfl variables {M A B C} instance : has_zero (A β†’+[M] B) := ⟨{ map_smul' := by simp, .. (0 : A β†’+ B) }⟩ instance : has_one (A β†’+[M] A) := ⟨distrib_mul_action_hom.id M⟩ @[simp] lemma coe_zero : ((0 : A β†’+[M] B) : A β†’ B) = 0 := rfl @[simp] lemma coe_one : ((1 : A β†’+[M] A) : A β†’ A) = id := rfl lemma zero_apply (a : A) : (0 : A β†’+[M] B) a = 0 := rfl lemma one_apply (a : A) : (1 : A β†’+[M] A) a = a := rfl instance : inhabited (A β†’+[M] B) := ⟨0⟩ /-- Composition of two equivariant additive monoid homomorphisms. -/ def comp (g : B β†’+[M] C) (f : A β†’+[M] B) : A β†’+[M] C := { .. mul_action_hom.comp (g : B β†’[M] C) (f : A β†’[M] B), .. add_monoid_hom.comp (g : B β†’+ C) (f : A β†’+ B), } @[simp] lemma comp_apply (g : B β†’+[M] C) (f : A β†’+[M] B) (x : A) : g.comp f x = g (f x) := rfl @[simp] lemma id_comp (f : A β†’+[M] B) : (distrib_mul_action_hom.id M).comp f = f := ext $ Ξ» x, by rw [comp_apply, id_apply] @[simp] lemma comp_id (f : A β†’+[M] B) : f.comp (distrib_mul_action_hom.id M) = f := ext $ Ξ» x, by rw [comp_apply, id_apply] /-- The inverse of a bijective `distrib_mul_action_hom` is a `distrib_mul_action_hom`. -/ @[simps] def inverse (f : A β†’+[M] B) (g : B β†’ A) (h₁ : function.left_inverse g f) (hβ‚‚ : function.right_inverse g f) : B β†’+[M] A := { to_fun := g, .. (f : A β†’+ B).inverse g h₁ hβ‚‚, .. (f : A β†’[M] B).inverse g h₁ hβ‚‚ } section semiring variables {R M'} [add_monoid M'] [distrib_mul_action R M'] @[ext] lemma ext_ring {f g : R β†’+[R] M'} (h : f 1 = g 1) : f = g := by { ext x, rw [← mul_one x, ← smul_eq_mul R, f.map_smul, g.map_smul, h], } lemma ext_ring_iff {f g : R β†’+[R] M'} : f = g ↔ f 1 = g 1 := ⟨λ h, h β–Έ rfl, ext_ring⟩ end semiring end distrib_mul_action_hom /-- Equivariant ring homomorphisms. -/ @[nolint has_nonempty_instance] structure mul_semiring_action_hom extends R β†’+[M] S, R β†’+* S. /-- Reinterpret an equivariant ring homomorphism as a ring homomorphism. -/ add_decl_doc mul_semiring_action_hom.to_ring_hom /-- Reinterpret an equivariant ring homomorphism as an equivariant additive monoid homomorphism. -/ add_decl_doc mul_semiring_action_hom.to_distrib_mul_action_hom notation R ` β†’+*[`:25 M:25 `] `:0 S:0 := mul_semiring_action_hom M R S /-- `mul_semiring_action_hom_class F M R S` states that `F` is a type of morphisms preserving the ring structure and scalar multiplication by `M`. You should extend this class when you extend `mul_semiring_action_hom`. -/ class mul_semiring_action_hom_class (F : Type*) (M R S : out_param $ Type*) [monoid M] [semiring R] [semiring S] [distrib_mul_action M R] [distrib_mul_action M S] extends distrib_mul_action_hom_class F M R S, ring_hom_class F R S -- `M` becomes a metavariable but it's an `out_param` so it's not a problem. attribute [nolint dangerous_instance] mul_semiring_action_hom_class.to_ring_hom_class namespace mul_semiring_action_hom instance has_coe : has_coe (R β†’+*[M] S) (R β†’+* S) := ⟨to_ring_hom⟩ instance has_coe' : has_coe (R β†’+*[M] S) (R β†’+[M] S) := ⟨to_distrib_mul_action_hom⟩ instance : has_coe_to_fun (R β†’+*[M] S) (Ξ» _, R β†’ S) := ⟨λ c, c.to_fun⟩ instance : mul_semiring_action_hom_class (R β†’+*[M] S) M R S := { coe := mul_semiring_action_hom.to_fun, coe_injective' := Ξ» f g h, by cases f; cases g; congr', map_smul := mul_semiring_action_hom.map_smul', map_zero := mul_semiring_action_hom.map_zero', map_add := mul_semiring_action_hom.map_add', map_one := mul_semiring_action_hom.map_one', map_mul := mul_semiring_action_hom.map_mul' } variables {M R S} @[norm_cast] lemma coe_fn_coe (f : R β†’+*[M] S) : ((f : R β†’+* S) : R β†’ S) = f := rfl @[norm_cast] lemma coe_fn_coe' (f : R β†’+*[M] S) : ((f : R β†’+[M] S) : R β†’ S) = f := rfl @[ext] theorem ext : βˆ€ {f g : R β†’+*[M] S}, (βˆ€ x, f x = g x) β†’ f = g := fun_like.ext theorem ext_iff {f g : R β†’+*[M] S} : f = g ↔ βˆ€ x, f x = g x := fun_like.ext_iff protected lemma map_zero (f : R β†’+*[M] S) : f 0 = 0 := map_zero _ protected lemma map_add (f : R β†’+*[M] S) (x y : R) : f (x + y) = f x + f y := map_add _ _ _ protected lemma map_neg (f : R' β†’+*[M] S') (x : R') : f (-x) = -f x := map_neg _ _ protected lemma map_sub (f : R' β†’+*[M] S') (x y : R') : f (x - y) = f x - f y := map_sub _ _ _ protected lemma map_one (f : R β†’+*[M] S) : f 1 = 1 := map_one _ protected lemma map_mul (f : R β†’+*[M] S) (x y : R) : f (x * y) = f x * f y := map_mul _ _ _ protected lemma map_smul (f : R β†’+*[M] S) (m : M) (x : R) : f (m β€’ x) = m β€’ f x := map_smul _ _ _ variables (M) {R} /-- The identity map as an equivariant ring homomorphism. -/ protected def id : R β†’+*[M] R := ⟨id, Ξ» _ _, rfl, rfl, Ξ» _ _, rfl, rfl, Ξ» _ _, rfl⟩ @[simp] lemma id_apply (x : R) : mul_semiring_action_hom.id M x = x := rfl variables {M R S T} /-- Composition of two equivariant additive monoid homomorphisms. -/ def comp (g : S β†’+*[M] T) (f : R β†’+*[M] S) : R β†’+*[M] T := { .. distrib_mul_action_hom.comp (g : S β†’+[M] T) (f : R β†’+[M] S), .. ring_hom.comp (g : S β†’+* T) (f : R β†’+* S), } @[simp] lemma comp_apply (g : S β†’+*[M] T) (f : R β†’+*[M] S) (x : R) : g.comp f x = g (f x) := rfl @[simp] lemma id_comp (f : R β†’+*[M] S) : (mul_semiring_action_hom.id M).comp f = f := ext $ Ξ» x, by rw [comp_apply, id_apply] @[simp] lemma comp_id (f : R β†’+*[M] S) : f.comp (mul_semiring_action_hom.id M) = f := ext $ Ξ» x, by rw [comp_apply, id_apply] end mul_semiring_action_hom section variables (M) {R'} (U : subring R') [is_invariant_subring M U] /-- The canonical inclusion from an invariant subring. -/ def is_invariant_subring.subtype_hom : U β†’+*[M] R' := { map_smul' := Ξ» m s, rfl, ..U.subtype } @[simp] theorem is_invariant_subring.coe_subtype_hom : (is_invariant_subring.subtype_hom M U : U β†’ R') = coe := rfl @[simp] theorem is_invariant_subring.coe_subtype_hom' : (is_invariant_subring.subtype_hom M U : U β†’+* R') = U.subtype := rfl end