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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.