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
a05746458702f97a8600c16caf04bbb6e6d61373
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/indicator_function.lean
cb13f9284b4d682fbcc88eede8f0dae5d83f0d57
[ "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
21,940
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import algebra.support /-! # Indicator function - `indicator (s : set Ξ±) (f : Ξ± β†’ Ξ²) (a : Ξ±)` is `f a` if `a ∈ s` and is `0` otherwise. - `mul_indicator (s : set Ξ±) (f : Ξ± β†’ Ξ²) (a : Ξ±)` is `f a` if `a ∈ s` and is `1` otherwise. ## Implementation note In mathematics, an indicator function or a characteristic function is a function used to indicate membership of an element in a set `s`, having the value `1` for all elements of `s` and the value `0` otherwise. But since it is usually used to restrict a function to a certain set `s`, we let the indicator function take the value `f x` for some function `f`, instead of `1`. If the usual indicator function is needed, just set `f` to be the constant function `Ξ»x, 1`. ## Tags indicator, characteristic -/ noncomputable theory open_locale classical big_operators open function variables {Ξ± Ξ² ΞΉ M N : Type*} namespace set section has_one variables [has_one M] [has_one N] {s t : set Ξ±} {f g : Ξ± β†’ M} {a : Ξ±} /-- `indicator s f a` is `f a` if `a ∈ s`, `0` otherwise. -/ def indicator {M} [has_zero M] (s : set Ξ±) (f : Ξ± β†’ M) : Ξ± β†’ M := Ξ» x, if x ∈ s then f x else 0 /-- `mul_indicator s f a` is `f a` if `a ∈ s`, `1` otherwise. -/ @[to_additive] def mul_indicator (s : set Ξ±) (f : Ξ± β†’ M) : Ξ± β†’ M := Ξ» x, if x ∈ s then f x else 1 @[simp, to_additive] lemma piecewise_eq_mul_indicator : s.piecewise f 1 = s.mul_indicator f := rfl @[to_additive] lemma mul_indicator_apply (s : set Ξ±) (f : Ξ± β†’ M) (a : Ξ±) : mul_indicator s f a = if a ∈ s then f a else 1 := rfl @[simp, to_additive] lemma mul_indicator_of_mem (h : a ∈ s) (f : Ξ± β†’ M) : mul_indicator s f a = f a := if_pos h @[simp, to_additive] lemma mul_indicator_of_not_mem (h : a βˆ‰ s) (f : Ξ± β†’ M) : mul_indicator s f a = 1 := if_neg h @[to_additive] lemma mul_indicator_eq_one_or_self (s : set Ξ±) (f : Ξ± β†’ M) (a : Ξ±) : mul_indicator s f a = 1 ∨ mul_indicator s f a = f a := if h : a ∈ s then or.inr (mul_indicator_of_mem h f) else or.inl (mul_indicator_of_not_mem h f) @[simp, to_additive] lemma mul_indicator_apply_eq_self : s.mul_indicator f a = f a ↔ (a βˆ‰ s β†’ f a = 1) := ite_eq_left_iff.trans $ by rw [@eq_comm _ (f a)] @[simp, to_additive] lemma mul_indicator_eq_self : s.mul_indicator f = f ↔ mul_support f βŠ† s := by simp only [funext_iff, subset_def, mem_mul_support, mul_indicator_apply_eq_self, not_imp_comm] @[to_additive] lemma mul_indicator_eq_self_of_superset (h1 : s.mul_indicator f = f) (h2 : s βŠ† t) : t.mul_indicator f = f := by { rw mul_indicator_eq_self at h1 ⊒, exact subset.trans h1 h2 } @[simp, to_additive] lemma mul_indicator_apply_eq_one : mul_indicator s f a = 1 ↔ (a ∈ s β†’ f a = 1) := ite_eq_right_iff @[simp, to_additive] lemma mul_indicator_eq_one : mul_indicator s f = (Ξ» x, 1) ↔ disjoint (mul_support f) s := by simp only [funext_iff, mul_indicator_apply_eq_one, set.disjoint_left, mem_mul_support, not_imp_not] @[simp, to_additive] lemma mul_indicator_eq_one' : mul_indicator s f = 1 ↔ disjoint (mul_support f) s := mul_indicator_eq_one @[to_additive] lemma mul_indicator_eq_one_iff (a : Ξ±) : s.mul_indicator f a β‰  1 ↔ a ∈ s ∩ mul_support f := begin split; intro h, { by_contra hmem, simp only [set.mem_inter_eq, not_and, not_not, function.mem_mul_support] at hmem, refine h _, by_cases a ∈ s, { simp_rw [set.mul_indicator, if_pos h], exact hmem h }, { simp_rw [set.mul_indicator, if_neg h] } }, { simp_rw [set.mul_indicator, if_pos h.1], exact h.2 } end @[simp, to_additive] lemma mul_support_mul_indicator : function.mul_support (s.mul_indicator f) = s ∩ function.mul_support f := ext $ Ξ» x, by simp [function.mem_mul_support, mul_indicator_apply_eq_one] /-- If a multiplicative indicator function is not equal to one at a point, then that point is in the set. -/ @[to_additive] lemma mem_of_mul_indicator_ne_one (h : mul_indicator s f a β‰  1) : a ∈ s := not_imp_comm.1 (Ξ» hn, mul_indicator_of_not_mem hn f) h @[to_additive] lemma eq_on_mul_indicator : eq_on (mul_indicator s f) f s := Ξ» x hx, mul_indicator_of_mem hx f @[to_additive] lemma mul_support_mul_indicator_subset : mul_support (s.mul_indicator f) βŠ† s := Ξ» x hx, hx.imp_symm (Ξ» h, mul_indicator_of_not_mem h f) @[simp, to_additive] lemma mul_indicator_mul_support : mul_indicator (mul_support f) f = f := mul_indicator_eq_self.2 subset.rfl @[simp, to_additive] lemma mul_indicator_range_comp {ΞΉ : Sort*} (f : ΞΉ β†’ Ξ±) (g : Ξ± β†’ M) : mul_indicator (range f) g ∘ f = g ∘ f := piecewise_range_comp _ _ _ @[to_additive] lemma mul_indicator_congr (h : eq_on f g s) : mul_indicator s f = mul_indicator s g := funext $ Ξ»x, by { simp only [mul_indicator], split_ifs, { exact h h_1 }, refl } @[simp, to_additive] lemma mul_indicator_univ (f : Ξ± β†’ M) : mul_indicator (univ : set Ξ±) f = f := mul_indicator_eq_self.2 $ subset_univ _ @[simp, to_additive] lemma mul_indicator_empty (f : Ξ± β†’ M) : mul_indicator (βˆ… : set Ξ±) f = Ξ»a, 1 := mul_indicator_eq_one.2 $ disjoint_empty _ @[to_additive] lemma mul_indicator_empty' (f : Ξ± β†’ M) : mul_indicator (βˆ… : set Ξ±) f = 1 := mul_indicator_empty f variable (M) @[simp, to_additive] lemma mul_indicator_one (s : set Ξ±) : mul_indicator s (Ξ»x, (1:M)) = Ξ»x, (1:M) := mul_indicator_eq_one.2 $ by simp only [mul_support_one, empty_disjoint] @[simp, to_additive] lemma mul_indicator_one' {s : set Ξ±} : s.mul_indicator (1 : Ξ± β†’ M) = 1 := mul_indicator_one M s variable {M} @[to_additive] lemma mul_indicator_mul_indicator (s t : set Ξ±) (f : Ξ± β†’ M) : mul_indicator s (mul_indicator t f) = mul_indicator (s ∩ t) f := funext $ Ξ»x, by { simp only [mul_indicator], split_ifs, repeat {simp * at * {contextual := tt}} } @[simp, to_additive] lemma mul_indicator_inter_mul_support (s : set Ξ±) (f : Ξ± β†’ M) : mul_indicator (s ∩ mul_support f) f = mul_indicator s f := by rw [← mul_indicator_mul_indicator, mul_indicator_mul_support] @[to_additive] lemma comp_mul_indicator (h : M β†’ Ξ²) (f : Ξ± β†’ M) {s : set Ξ±} {x : Ξ±} : h (s.mul_indicator f x) = s.piecewise (h ∘ f) (const Ξ± (h 1)) x := s.apply_piecewise _ _ (Ξ» _, h) @[to_additive] lemma mul_indicator_comp_right {s : set Ξ±} (f : Ξ² β†’ Ξ±) {g : Ξ± β†’ M} {x : Ξ²} : mul_indicator (f ⁻¹' s) (g ∘ f) x = mul_indicator s g (f x) := by { simp only [mul_indicator], split_ifs; refl } @[to_additive] lemma mul_indicator_comp_of_one {g : M β†’ N} (hg : g 1 = 1) : mul_indicator s (g ∘ f) = g ∘ (mul_indicator s f) := begin funext, simp only [mul_indicator], split_ifs; simp [*] end @[to_additive] lemma comp_mul_indicator_const (c : M) (f : M β†’ N) (hf : f 1 = 1) : (Ξ» x, f (s.mul_indicator (Ξ» x, c) x)) = s.mul_indicator (Ξ» x, f c) := (mul_indicator_comp_of_one hf).symm @[to_additive] lemma mul_indicator_preimage (s : set Ξ±) (f : Ξ± β†’ M) (B : set M) : (mul_indicator s f)⁻¹' B = s.ite (f ⁻¹' B) (1 ⁻¹' B) := piecewise_preimage s f 1 B @[to_additive] lemma mul_indicator_preimage_of_not_mem (s : set Ξ±) (f : Ξ± β†’ M) {t : set M} (ht : (1:M) βˆ‰ t) : (mul_indicator s f)⁻¹' t = f ⁻¹' t ∩ s := by simp [mul_indicator_preimage, pi.one_def, set.preimage_const_of_not_mem ht] @[to_additive] lemma mem_range_mul_indicator {r : M} {s : set Ξ±} {f : Ξ± β†’ M} : r ∈ range (mul_indicator s f) ↔ (r = 1 ∧ s β‰  univ) ∨ (r ∈ f '' s) := by simp [mul_indicator, ite_eq_iff, exists_or_distrib, eq_univ_iff_forall, and_comm, or_comm, @eq_comm _ r 1] @[to_additive] lemma mul_indicator_rel_mul_indicator {r : M β†’ M β†’ Prop} (h1 : r 1 1) (ha : a ∈ s β†’ r (f a) (g a)) : r (mul_indicator s f a) (mul_indicator s g a) := by { simp only [mul_indicator], split_ifs with has has, exacts [ha has, h1] } end has_one section monoid variables [mul_one_class M] {s t : set Ξ±} {f g : Ξ± β†’ M} {a : Ξ±} @[to_additive] lemma mul_indicator_union_mul_inter_apply (f : Ξ± β†’ M) (s t : set Ξ±) (a : Ξ±) : mul_indicator (s βˆͺ t) f a * mul_indicator (s ∩ t) f a = mul_indicator s f a * mul_indicator t f a := by by_cases hs : a ∈ s; by_cases ht : a ∈ t; simp * @[to_additive] lemma mul_indicator_union_mul_inter (f : Ξ± β†’ M) (s t : set Ξ±) : mul_indicator (s βˆͺ t) f * mul_indicator (s ∩ t) f = mul_indicator s f * mul_indicator t f := funext $ mul_indicator_union_mul_inter_apply f s t @[to_additive] lemma mul_indicator_union_of_not_mem_inter (h : a βˆ‰ s ∩ t) (f : Ξ± β†’ M) : mul_indicator (s βˆͺ t) f a = mul_indicator s f a * mul_indicator t f a := by rw [← mul_indicator_union_mul_inter_apply f s t, mul_indicator_of_not_mem h, mul_one] @[to_additive] lemma mul_indicator_union_of_disjoint (h : disjoint s t) (f : Ξ± β†’ M) : mul_indicator (s βˆͺ t) f = Ξ»a, mul_indicator s f a * mul_indicator t f a := funext $ Ξ»a, mul_indicator_union_of_not_mem_inter (Ξ» ha, h ha) _ @[to_additive] lemma mul_indicator_mul (s : set Ξ±) (f g : Ξ± β†’ M) : mul_indicator s (Ξ»a, f a * g a) = Ξ»a, mul_indicator s f a * mul_indicator s g a := by { funext, simp only [mul_indicator], split_ifs, { refl }, rw mul_one } @[simp, to_additive] lemma mul_indicator_compl_mul_self_apply (s : set Ξ±) (f : Ξ± β†’ M) (a : Ξ±) : mul_indicator sᢜ f a * mul_indicator s f a = f a := classical.by_cases (Ξ» ha : a ∈ s, by simp [ha]) (Ξ» ha, by simp [ha]) @[simp, to_additive] lemma mul_indicator_compl_mul_self (s : set Ξ±) (f : Ξ± β†’ M) : mul_indicator sᢜ f * mul_indicator s f = f := funext $ mul_indicator_compl_mul_self_apply s f @[simp, to_additive] lemma mul_indicator_self_mul_compl_apply (s : set Ξ±) (f : Ξ± β†’ M) (a : Ξ±) : mul_indicator s f a * mul_indicator sᢜ f a = f a := classical.by_cases (Ξ» ha : a ∈ s, by simp [ha]) (Ξ» ha, by simp [ha]) @[simp, to_additive] lemma mul_indicator_self_mul_compl (s : set Ξ±) (f : Ξ± β†’ M) : mul_indicator s f * mul_indicator sᢜ f = f := funext $ mul_indicator_self_mul_compl_apply s f @[to_additive] lemma mul_indicator_mul_eq_left {f g : Ξ± β†’ M} (h : disjoint (mul_support f) (mul_support g)) : (mul_support f).mul_indicator (f * g) = f := begin refine (mul_indicator_congr $ Ξ» x hx, _).trans mul_indicator_mul_support, have : g x = 1, from nmem_mul_support.1 (disjoint_left.1 h hx), rw [pi.mul_apply, this, mul_one] end @[to_additive] lemma mul_indicator_mul_eq_right {f g : Ξ± β†’ M} (h : disjoint (mul_support f) (mul_support g)) : (mul_support g).mul_indicator (f * g) = g := begin refine (mul_indicator_congr $ Ξ» x hx, _).trans mul_indicator_mul_support, have : f x = 1, from nmem_mul_support.1 (disjoint_right.1 h hx), rw [pi.mul_apply, this, one_mul] end /-- `set.mul_indicator` as a `monoid_hom`. -/ @[to_additive "`set.indicator` as an `add_monoid_hom`."] def mul_indicator_hom {Ξ±} (M) [mul_one_class M] (s : set Ξ±) : (Ξ± β†’ M) β†’* (Ξ± β†’ M) := { to_fun := mul_indicator s, map_one' := mul_indicator_one M s, map_mul' := mul_indicator_mul s } end monoid section distrib_mul_action variables {A : Type*} [add_monoid A] [monoid M] [distrib_mul_action M A] lemma indicator_smul_apply (s : set Ξ±) (r : M) (f : Ξ± β†’ A) (x : Ξ±) : indicator s (Ξ» x, r β€’ f x) x = r β€’ indicator s f x := by { dunfold indicator, split_ifs, exacts [rfl, (smul_zero r).symm] } lemma indicator_smul (s : set Ξ±) (r : M) (f : Ξ± β†’ A) : indicator s (Ξ» (x : Ξ±), r β€’ f x) = Ξ» (x : Ξ±), r β€’ indicator s f x := funext $ indicator_smul_apply s r f end distrib_mul_action section group variables {G : Type*} [group G] {s t : set Ξ±} {f g : Ξ± β†’ G} {a : Ξ±} @[to_additive] lemma mul_indicator_inv' (s : set Ξ±) (f : Ξ± β†’ G) : mul_indicator s (f⁻¹) = (mul_indicator s f)⁻¹ := (mul_indicator_hom G s).map_inv f @[to_additive] lemma mul_indicator_inv (s : set Ξ±) (f : Ξ± β†’ G) : mul_indicator s (Ξ»a, (f a)⁻¹) = Ξ»a, (mul_indicator s f a)⁻¹ := mul_indicator_inv' s f lemma indicator_sub {G} [add_group G] (s : set Ξ±) (f g : Ξ± β†’ G) : indicator s (Ξ»a, f a - g a) = Ξ»a, indicator s f a - indicator s g a := (indicator_hom G s).map_sub f g @[to_additive indicator_compl'] lemma mul_indicator_compl (s : set Ξ±) (f : Ξ± β†’ G) : mul_indicator sᢜ f = f * (mul_indicator s f)⁻¹ := eq_mul_inv_of_mul_eq $ s.mul_indicator_compl_mul_self f lemma indicator_compl {G} [add_group G] (s : set Ξ±) (f : Ξ± β†’ G) : indicator sᢜ f = f - indicator s f := by rw [sub_eq_add_neg, indicator_compl'] @[to_additive indicator_diff'] lemma mul_indicator_diff (h : s βŠ† t) (f : Ξ± β†’ G) : mul_indicator (t \ s) f = mul_indicator t f * (mul_indicator s f)⁻¹ := eq_mul_inv_of_mul_eq $ by rw [pi.mul_def, ← mul_indicator_union_of_disjoint disjoint_diff.symm f, diff_union_self, union_eq_self_of_subset_right h] lemma indicator_diff {G : Type*} [add_group G] {s t : set Ξ±} (h : s βŠ† t) (f : Ξ± β†’ G) : indicator (t \ s) f = indicator t f - indicator s f := by rw [indicator_diff' h, sub_eq_add_neg] end group section comm_monoid variables [comm_monoid M] /-- Consider a product of `g i (f i)` over a `finset`. Suppose `g` is a function such as `pow`, which maps a second argument of `1` to `1`. Then if `f` is replaced by the corresponding multiplicative indicator function, the `finset` may be replaced by a possibly larger `finset` without changing the value of the sum. -/ @[to_additive] lemma prod_mul_indicator_subset_of_eq_one [has_one N] (f : Ξ± β†’ N) (g : Ξ± β†’ N β†’ M) {s t : finset Ξ±} (h : s βŠ† t) (hg : βˆ€ a, g a 1 = 1) : ∏ i in s, g i (f i) = ∏ i in t, g i (mul_indicator ↑s f i) := begin rw ← finset.prod_subset h _, { apply finset.prod_congr rfl, intros i hi, congr, symmetry, exact mul_indicator_of_mem hi _ }, { refine Ξ» i hi hn, _, convert hg i, exact mul_indicator_of_not_mem hn _ } end /-- Consider a sum of `g i (f i)` over a `finset`. Suppose `g` is a function such as multiplication, which maps a second argument of 0 to 0. (A typical use case would be a weighted sum of `f i * h i` or `f i β€’ h i`, where `f` gives the weights that are multiplied by some other function `h`.) Then if `f` is replaced by the corresponding indicator function, the `finset` may be replaced by a possibly larger `finset` without changing the value of the sum. -/ add_decl_doc set.sum_indicator_subset_of_eq_zero @[to_additive] lemma prod_mul_indicator_subset (f : Ξ± β†’ M) {s t : finset Ξ±} (h : s βŠ† t) : ∏ i in s, f i = ∏ i in t, mul_indicator ↑s f i := prod_mul_indicator_subset_of_eq_one _ (Ξ» a b, b) h (Ξ» _, rfl) /-- Summing an indicator function over a possibly larger `finset` is the same as summing the original function over the original `finset`. -/ add_decl_doc sum_indicator_subset @[to_additive] lemma _root_.finset.prod_mul_indicator_eq_prod_filter (s : finset ΞΉ) (f : ΞΉ β†’ Ξ± β†’ M) (t : ΞΉ β†’ set Ξ±) (g : ΞΉ β†’ Ξ±) : ∏ i in s, mul_indicator (t i) (f i) (g i) = ∏ i in s.filter (Ξ» i, g i ∈ t i), f i (g i) := begin refine (finset.prod_filter_mul_prod_filter_not s (Ξ» i, g i ∈ t i) _).symm.trans _, refine eq.trans _ (mul_one _), exact congr_arg2 (*) (finset.prod_congr rfl $ Ξ» x hx, mul_indicator_of_mem (finset.mem_filter.1 hx).2 _) (finset.prod_eq_one $ Ξ» x hx, mul_indicator_of_not_mem (finset.mem_filter.1 hx).2 _) end @[to_additive] lemma mul_indicator_finset_prod (I : finset ΞΉ) (s : set Ξ±) (f : ΞΉ β†’ Ξ± β†’ M) : mul_indicator s (∏ i in I, f i) = ∏ i in I, mul_indicator s (f i) := (mul_indicator_hom M s).map_prod _ _ @[to_additive] lemma mul_indicator_finset_bUnion {ΞΉ} (I : finset ΞΉ) (s : ΞΉ β†’ set Ξ±) {f : Ξ± β†’ M} : (βˆ€ (i ∈ I) (j ∈ I), i β‰  j β†’ disjoint (s i) (s j)) β†’ mul_indicator (⋃ i ∈ I, s i) f = Ξ» a, ∏ i in I, mul_indicator (s i) f a := begin refine finset.induction_on I _ _, { intro h, funext, simp }, assume a I haI ih hI, funext, rw [finset.prod_insert haI, finset.set_bUnion_insert, mul_indicator_union_of_not_mem_inter, ih _], { assume i hi j hj hij, exact hI i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj) hij }, simp only [not_exists, exists_prop, mem_Union, mem_inter_eq, not_and], assume hx a' ha', refine disjoint_left.1 (hI a (finset.mem_insert_self _ _) a' (finset.mem_insert_of_mem ha') _) hx, exact (ne_of_mem_of_not_mem ha' haI).symm end end comm_monoid section mul_zero_class variables [mul_zero_class M] {s t : set Ξ±} {f g : Ξ± β†’ M} {a : Ξ±} lemma indicator_mul (s : set Ξ±) (f g : Ξ± β†’ M) : indicator s (Ξ»a, f a * g a) = Ξ»a, indicator s f a * indicator s g a := by { funext, simp only [indicator], split_ifs, { refl }, rw mul_zero } lemma indicator_mul_left (s : set Ξ±) (f g : Ξ± β†’ M) : indicator s (Ξ»a, f a * g a) a = indicator s f a * g a := by { simp only [indicator], split_ifs, { refl }, rw [zero_mul] } lemma indicator_mul_right (s : set Ξ±) (f g : Ξ± β†’ M) : indicator s (Ξ»a, f a * g a) a = f a * indicator s g a := by { simp only [indicator], split_ifs, { refl }, rw [mul_zero] } lemma inter_indicator_mul {t1 t2 : set Ξ±} (f g : Ξ± β†’ M) (x : Ξ±) : (t1 ∩ t2).indicator (Ξ» x, f x * g x) x = t1.indicator f x * t2.indicator g x := by { rw [← set.indicator_indicator], simp [indicator] } end mul_zero_class section monoid_with_zero variables [monoid_with_zero M] lemma indicator_prod_one {s : set Ξ±} {t : set Ξ²} {x : Ξ±} {y : Ξ²} : (s.prod t).indicator (1 : _ β†’ M) (x, y) = s.indicator 1 x * t.indicator 1 y := by simp [indicator, ← ite_and] end monoid_with_zero section order variables [has_one M] [preorder M] {s t : set Ξ±} {f g : Ξ± β†’ M} {a : Ξ±} {y : M} @[to_additive] lemma mul_indicator_apply_le' (hfg : a ∈ s β†’ f a ≀ y) (hg : a βˆ‰ s β†’ 1 ≀ y) : mul_indicator s f a ≀ y := if ha : a ∈ s then by simpa [ha] using hfg ha else by simpa [ha] using hg ha @[to_additive] lemma mul_indicator_le' (hfg : βˆ€ a ∈ s, f a ≀ g a) (hg : βˆ€ a βˆ‰ s, 1 ≀ g a) : mul_indicator s f ≀ g := Ξ» a, mul_indicator_apply_le' (hfg _) (hg _) @[to_additive] lemma le_mul_indicator_apply {y} (hfg : a ∈ s β†’ y ≀ g a) (hf : a βˆ‰ s β†’ y ≀ 1) : y ≀ mul_indicator s g a := @mul_indicator_apply_le' Ξ± (order_dual M) β€Ή_β€Ί _ _ _ _ _ hfg hf @[to_additive] lemma le_mul_indicator (hfg : βˆ€ a ∈ s, f a ≀ g a) (hf : βˆ€ a βˆ‰ s, f a ≀ 1) : f ≀ mul_indicator s g := Ξ» a, le_mul_indicator_apply (hfg _) (hf _) @[to_additive indicator_apply_nonneg] lemma one_le_mul_indicator_apply (h : a ∈ s β†’ 1 ≀ f a) : 1 ≀ mul_indicator s f a := le_mul_indicator_apply h (Ξ» _, le_rfl) @[to_additive indicator_nonneg] lemma one_le_mul_indicator (h : βˆ€ a ∈ s, 1 ≀ f a) (a : Ξ±) : 1 ≀ mul_indicator s f a := one_le_mul_indicator_apply (h a) @[to_additive] lemma mul_indicator_apply_le_one (h : a ∈ s β†’ f a ≀ 1) : mul_indicator s f a ≀ 1 := mul_indicator_apply_le' h (Ξ» _, le_rfl) @[to_additive] lemma mul_indicator_le_one (h : βˆ€ a ∈ s, f a ≀ 1) (a : Ξ±) : mul_indicator s f a ≀ 1 := mul_indicator_apply_le_one (h a) @[to_additive] lemma mul_indicator_le_mul_indicator (h : f a ≀ g a) : mul_indicator s f a ≀ mul_indicator s g a := mul_indicator_rel_mul_indicator (le_refl _) (Ξ» _, h) attribute [mono] mul_indicator_le_mul_indicator indicator_le_indicator @[to_additive] lemma mul_indicator_le_mul_indicator_of_subset (h : s βŠ† t) (hf : βˆ€ a, 1 ≀ f a) (a : Ξ±) : mul_indicator s f a ≀ mul_indicator t f a := mul_indicator_apply_le' (Ξ» ha, le_mul_indicator_apply (Ξ» _, le_rfl) (Ξ» hat, (hat $ h ha).elim)) (Ξ» ha, one_le_mul_indicator_apply (Ξ» _, hf _)) @[to_additive] lemma mul_indicator_le_self' (hf : βˆ€ x βˆ‰ s, 1 ≀ f x) : mul_indicator s f ≀ f := mul_indicator_le' (Ξ» _ _, le_refl _) hf @[to_additive] lemma mul_indicator_Union_apply {ΞΉ M} [complete_lattice M] [has_one M] (h1 : (βŠ₯:M) = 1) (s : ΞΉ β†’ set Ξ±) (f : Ξ± β†’ M) (x : Ξ±) : mul_indicator (⋃ i, s i) f x = ⨆ i, mul_indicator (s i) f x := begin by_cases hx : x ∈ ⋃ i, s i, { rw [mul_indicator_of_mem hx], rw [mem_Union] at hx, refine le_antisymm _ (supr_le $ Ξ» i, mul_indicator_le_self' (Ξ» x hx, h1 β–Έ bot_le) x), rcases hx with ⟨i, hi⟩, exact le_supr_of_le i (ge_of_eq $ mul_indicator_of_mem hi _) }, { rw [mul_indicator_of_not_mem hx], simp only [mem_Union, not_exists] at hx, simp [hx, ← h1] } end end order section canonically_ordered_monoid variables [canonically_ordered_monoid M] @[to_additive] lemma mul_indicator_le_self (s : set Ξ±) (f : Ξ± β†’ M) : mul_indicator s f ≀ f := mul_indicator_le_self' $ Ξ» _ _, one_le _ @[to_additive] lemma mul_indicator_apply_le {a : Ξ±} {s : set Ξ±} {f g : Ξ± β†’ M} (hfg : a ∈ s β†’ f a ≀ g a) : mul_indicator s f a ≀ g a := mul_indicator_apply_le' hfg $ Ξ» _, one_le _ @[to_additive] lemma mul_indicator_le {s : set Ξ±} {f g : Ξ± β†’ M} (hfg : βˆ€ a ∈ s, f a ≀ g a) : mul_indicator s f ≀ g := mul_indicator_le' hfg $ Ξ» _ _, one_le _ end canonically_ordered_monoid lemma indicator_le_indicator_nonneg {Ξ²} [linear_order Ξ²] [has_zero Ξ²] (s : set Ξ±) (f : Ξ± β†’ Ξ²) : s.indicator f ≀ {x | 0 ≀ f x}.indicator f := begin intro x, simp_rw indicator_apply, split_ifs, { exact le_rfl, }, { exact (not_le.mp h_1).le, }, { exact h_1, }, { exact le_rfl, }, end lemma indicator_nonpos_le_indicator {Ξ²} [linear_order Ξ²] [has_zero Ξ²] (s : set Ξ±) (f : Ξ± β†’ Ξ²) : {x | f x ≀ 0}.indicator f ≀ s.indicator f := @indicator_le_indicator_nonneg Ξ± (order_dual Ξ²) _ _ s f end set @[to_additive] lemma monoid_hom.map_mul_indicator {M N : Type*} [monoid M] [monoid N] (f : M β†’* N) (s : set Ξ±) (g : Ξ± β†’ M) (x : Ξ±) : f (s.mul_indicator g x) = s.mul_indicator (f ∘ g) x := congr_fun (set.mul_indicator_comp_of_one f.map_one).symm x
1f6e459c1ffb2afb220396f9afad11fd5d0bb53f
4727251e0cd73359b15b664c3170e5d754078599
/test/conv/apply_congr.lean
ed8b88f80122615d3b4eaffa12dd29d78631ed08
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,162
lean
/- Copyright (c) 2019 Lucas Allen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Lucas Allen, Scott Morrison -/ import algebra.big_operators.basic import data.finsupp.basic import tactic.converter.apply_congr import tactic.interactive example (f g : β„€ β†’ β„€) (S : finset β„€) (h : βˆ€ m ∈ S, f m = g m) : finset.sum S f = finset.sum S g := begin conv_lhs { -- If we just call `congr` here, in the second goal we're helpless, -- because we are only given the opportunity to rewrite `f`. -- However `apply_congr` uses the appropriate `@[congr]` lemma, -- so we get to rewrite `f x`, in the presence of the crucial `H : x ∈ S` hypothesis. apply_congr, skip, simp [h, H], } end -- Again, with some `guard` statements. example (f g : β„€ β†’ β„€) (S : finset β„€) (h : βˆ€ m ∈ S, f m = g m) : finset.sum S f = finset.sum S g := begin conv_lhs { apply_congr finset.sum_congr, -- (See the note about get_goals/set_goals inside apply_congr) (do ng ← tactic.num_goals, guard $ ng = 2), guard_target S, skip, guard_target f x, simp [h, H] } end -- Verify we can `rw` as well as `simp`. example (f g : β„€ β†’ β„€) (S : finset β„€) (h : βˆ€ m ∈ S, f m = g m) : finset.sum S f = finset.sum S g := by conv_lhs { apply_congr, skip, rw h x H, } -- Check that the appropriate `@[congr]` lemma is automatically selected. example (f g : β„€ β†’ β„€) (S : finset β„€) (h : βˆ€ m ∈ S, f m = g m) : finset.prod S f = finset.prod S g := by conv_lhs { apply_congr, skip, simp [h, H], } example (f g : β„€ β†’ β„€) (S : finset β„€) (h : βˆ€ m ∈ S, f m = g m) : finset.fold (+) 0 f S = finset.fold (+) 0 g S := begin -- This time, the automatically selected congruence lemma is "too good"! -- `finset.sum_congr` matches, and so the `conv` block actually -- rewrites the left hand side into a `finset.sum`. conv_lhs { apply_congr, skip, simp [h, H], }, -- So we need a `refl` to identify that we're done. refl, end -- This can be avoided by selecting the congruence lemma by hand. example (f g : β„€ β†’ β„€) (S : finset β„€) (h : βˆ€ m ∈ S, f m = g m) : finset.fold (+) 0 f S = finset.fold (+) 0 g S := begin conv_lhs { apply_congr finset.fold_congr, simp [h, H], }, end example (f : β„€ β†’ β„€) (S : finset β„€) (h : βˆ€ m ∈ S, f m = 0) : finset.sum S f = 0 := begin conv_lhs { apply_congr, skip, simp [h, H], }, simp, end -- An example using `finsupp.sum` open_locale classical example {k G : Type} [semiring k] [group G] (g : G β†’β‚€ k) (a₁ x : G) (b₁ : k) (t : βˆ€ (aβ‚‚ : G), a₁ * aβ‚‚ = x ↔ a₁⁻¹ * x = aβ‚‚) : g.sum (Ξ» (aβ‚‚ : G) (bβ‚‚ : k), ite (a₁ * aβ‚‚ = x) (b₁ * bβ‚‚) 0) = b₁ * g (a₁⁻¹ * x) := begin -- In fact, `congr` works fine here, because our rewrite works globally. conv_lhs { apply_congr, skip, dsimp, rw t, }, rw finset.sum_ite_eq g.support, -- it's a pity we can't just use `simp` here. split_ifs, { refl, }, { simp [finsupp.not_mem_support_iff.1 h], }, end example : true := begin success_if_fail { conv { apply_congr, }, }, trivial end
0093067cf911c5fb025b74a8062c5b42391572eb
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/944.lean
0bf6ecba1ec7743bef54c2e769914ba7f1a717d2
[ "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
805
lean
import Lean def f1 (x : Nat) : Except String Nat := if x > 0 then .ok x else .error "argument is zero" namespace Lean.Elab open Lsp def identOf : Info β†’ Option (RefIdent Γ— Bool) | .ofTermInfo ti => match ti.expr with | .const n .. => some (.const n, ti.isBinder) | .fvar id .. => some (.fvar id, ti.isBinder) | _ => none | .ofFieldInfo fi => some (.const fi.projName, false) | _ => none def isConst (e : Expr) : Bool := e matches .const .. def isImplicit (bi : BinderInfo) : Bool := bi matches .implicit end Lean.Elab def f2 (xs : List Nat) : List Nat := .map (Β· + 1) xs def f2' (xs : List Nat) : List Nat := .map .succ xs def f3 : Nat := .zero def f4 (x : Nat) : Nat := .succ x example (xs : List Ξ±) : Std.RBTree Ξ± ord := xs.foldl .insert βˆ…
5cec13c41434446638540502c5efba2f9d3e4532
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/direct_sum/internal.lean
2ceb49d35e64596a35381a075be0080a698eb55e
[ "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,198
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser, Kevin Buzzard, Jujian Zhang -/ import algebra.algebra.operations import algebra.algebra.subalgebra.basic import algebra.direct_sum.algebra /-! # Internally graded rings and algebras This module provides `gsemiring` and `gcomm_semiring` instances for a collection of subobjects `A` when a `set_like.graded_monoid` instance is available: * `set_like.gnon_unital_non_assoc_semiring` * `set_like.gsemiring` * `set_like.gcomm_semiring` With these instances in place, it provides the bundled canonical maps out of a direct sum of subobjects into their carrier type: * `direct_sum.coe_ring_hom` (a `ring_hom` version of `direct_sum.coe_add_monoid_hom`) * `direct_sum.coe_alg_hom` (an `alg_hom` version of `direct_sum.submodule_coe`) Strictly the definitions in this file are not sufficient to fully define an "internal" direct sum; to represent this case, `(h : direct_sum.is_internal A) [set_like.graded_monoid A]` is needed. In the future there will likely be a data-carrying, constructive, typeclass version of `direct_sum.is_internal` for providing an explicit decomposition function. When `complete_lattice.independent (set.range A)` (a weaker condition than `direct_sum.is_internal A`), these provide a grading of `⨆ i, A i`, and the mapping `⨁ i, A i β†’+ ⨆ i, A i` can be obtained as `direct_sum.to_monoid (Ξ» i, add_submonoid.inclusion $ le_supr A i)`. ## tags internally graded ring -/ open_locale direct_sum big_operators variables {ΞΉ : Type*} {Οƒ S R : Type*} instance add_comm_monoid.of_submonoid_on_semiring [semiring R] [set_like Οƒ R] [add_submonoid_class Οƒ R] (A : ΞΉ β†’ Οƒ) : βˆ€ i, add_comm_monoid (A i) := Ξ» i, by apply_instance instance add_comm_group.of_subgroup_on_ring [ring R] [set_like Οƒ R] [add_subgroup_class Οƒ R] (A : ΞΉ β†’ Οƒ) : βˆ€ i, add_comm_group (A i) := Ξ» i, by apply_instance lemma set_like.algebra_map_mem_graded [has_zero ΞΉ] [comm_semiring S] [semiring R] [algebra S R] (A : ΞΉ β†’ submodule S R) [set_like.has_graded_one A] (s : S) : algebra_map S R s ∈ A 0 := begin rw algebra.algebra_map_eq_smul_one, exact ((A 0).smul_mem s $ set_like.one_mem_graded _), end lemma set_like.nat_cast_mem_graded [has_zero ΞΉ] [add_monoid_with_one R] [set_like Οƒ R] [add_submonoid_class Οƒ R] (A : ΞΉ β†’ Οƒ) [set_like.has_graded_one A] (n : β„•) : (n : R) ∈ A 0 := begin induction n, { rw nat.cast_zero, exact zero_mem (A 0), }, { rw nat.cast_succ, exact add_mem n_ih (set_like.one_mem_graded _), }, end lemma set_like.int_cast_mem_graded [has_zero ΞΉ] [add_group_with_one R] [set_like Οƒ R] [add_subgroup_class Οƒ R] (A : ΞΉ β†’ Οƒ) [set_like.has_graded_one A] (z : β„€) : (z : R) ∈ A 0:= begin induction z, { rw int.cast_of_nat, exact set_like.nat_cast_mem_graded _ _, }, { rw int.cast_neg_succ_of_nat, exact neg_mem (set_like.nat_cast_mem_graded _ _), }, end section direct_sum variables [decidable_eq ΞΉ] /-! #### From `add_submonoid`s and `add_subgroup`s -/ namespace set_like /-- Build a `gnon_unital_non_assoc_semiring` instance for a collection of additive submonoids. -/ instance gnon_unital_non_assoc_semiring [has_add ΞΉ] [non_unital_non_assoc_semiring R] [set_like Οƒ R] [add_submonoid_class Οƒ R] (A : ΞΉ β†’ Οƒ) [set_like.has_graded_mul A] : direct_sum.gnon_unital_non_assoc_semiring (Ξ» i, A i) := { mul_zero := Ξ» i j _, subtype.ext (mul_zero _), zero_mul := Ξ» i j _, subtype.ext (zero_mul _), mul_add := Ξ» i j _ _ _, subtype.ext (mul_add _ _ _), add_mul := Ξ» i j _ _ _, subtype.ext (add_mul _ _ _), ..set_like.ghas_mul A } /-- Build a `gsemiring` instance for a collection of additive submonoids. -/ instance gsemiring [add_monoid ΞΉ] [semiring R] [set_like Οƒ R] [add_submonoid_class Οƒ R] (A : ΞΉ β†’ Οƒ) [set_like.graded_monoid A] : direct_sum.gsemiring (Ξ» i, A i) := { mul_zero := Ξ» i j _, subtype.ext (mul_zero _), zero_mul := Ξ» i j _, subtype.ext (zero_mul _), mul_add := Ξ» i j _ _ _, subtype.ext (mul_add _ _ _), add_mul := Ξ» i j _ _ _, subtype.ext (add_mul _ _ _), nat_cast := Ξ» n, ⟨n, set_like.nat_cast_mem_graded _ _⟩, nat_cast_zero := subtype.ext nat.cast_zero, nat_cast_succ := Ξ» n, subtype.ext (nat.cast_succ n), ..set_like.gmonoid A } /-- Build a `gcomm_semiring` instance for a collection of additive submonoids. -/ instance gcomm_semiring [add_comm_monoid ΞΉ] [comm_semiring R] [set_like Οƒ R] [add_submonoid_class Οƒ R] (A : ΞΉ β†’ Οƒ) [set_like.graded_monoid A] : direct_sum.gcomm_semiring (Ξ» i, A i) := { ..set_like.gcomm_monoid A, ..set_like.gsemiring A, } /-- Build a `gring` instance for a collection of additive subgroups. -/ instance gring [add_monoid ΞΉ] [ring R] [set_like Οƒ R] [add_subgroup_class Οƒ R] (A : ΞΉ β†’ Οƒ) [set_like.graded_monoid A] : direct_sum.gring (Ξ» i, A i) := { int_cast := Ξ» z, ⟨z, set_like.int_cast_mem_graded _ _⟩, int_cast_of_nat := Ξ» n, subtype.ext $ int.cast_of_nat _, int_cast_neg_succ_of_nat := Ξ» n, subtype.ext $ int.cast_neg_succ_of_nat n, ..set_like.gsemiring A } /-- Build a `gcomm_semiring` instance for a collection of additive submonoids. -/ instance gcomm_ring [add_comm_monoid ΞΉ] [comm_ring R] [set_like Οƒ R] [add_subgroup_class Οƒ R] (A : ΞΉ β†’ Οƒ) [set_like.graded_monoid A] : direct_sum.gcomm_ring (Ξ» i, A i) := { ..set_like.gcomm_monoid A, ..set_like.gring A, } end set_like namespace direct_sum section coe variables [semiring R] [set_like Οƒ R] [add_submonoid_class Οƒ R] (A : ΞΉ β†’ Οƒ) /-- The canonical ring isomorphism between `⨁ i, A i` and `R`-/ def coe_ring_hom [add_monoid ΞΉ] [set_like.graded_monoid A] : (⨁ i, A i) β†’+* R := direct_sum.to_semiring (Ξ» i, add_submonoid_class.subtype (A i)) rfl (Ξ» _ _ _ _, rfl) /-- The canonical ring isomorphism between `⨁ i, A i` and `R`-/ @[simp] lemma coe_ring_hom_of [add_monoid ΞΉ] [set_like.graded_monoid A] (i : ΞΉ) (x : A i) : (coe_ring_hom A : _ β†’+* R) (of (Ξ» i, A i) i x) = x := direct_sum.to_semiring_of _ _ _ _ _ lemma coe_mul_apply [add_monoid ΞΉ] [set_like.graded_monoid A] [Ξ  (i : ΞΉ) (x : A i), decidable (x β‰  0)] (r r' : ⨁ i, A i) (n : ΞΉ) : ((r * r') n : R) = βˆ‘ ij in (r.support Γ—Λ’ r'.support).filter (Ξ» ij : ΞΉ Γ— ΞΉ, ij.1 + ij.2 = n), r ij.1 * r' ij.2 := begin rw [mul_eq_sum_support_ghas_mul, dfinsupp.finset_sum_apply, add_submonoid_class.coe_finset_sum], simp_rw [coe_of_apply, ←finset.sum_filter, set_like.coe_ghas_mul], end lemma coe_mul_apply_eq_dfinsupp_sum [add_monoid ΞΉ] [set_like.graded_monoid A] [Ξ  (i : ΞΉ) (x : A i), decidable (x β‰  0)] (r r' : ⨁ i, A i) (n : ΞΉ) : ((r * r') n : R) = r.sum (Ξ» i ri, r'.sum (Ξ» j rj, if i + j = n then ri * rj else 0)) := begin simp only [mul_eq_dfinsupp_sum, dfinsupp.sum_apply], iterate 2 { rw [dfinsupp.sum, add_submonoid_class.coe_finset_sum], congr, ext }, dsimp only, split_ifs, { subst h, rw of_eq_same, refl }, { rw of_eq_of_ne _ _ _ _ h, refl }, end lemma coe_of_mul_apply_aux [add_monoid ΞΉ] [set_like.graded_monoid A] {i : ΞΉ} (r : A i) (r' : ⨁ i, A i) {j n : ΞΉ} (H : βˆ€ (x : ΞΉ), i + x = n ↔ x = j) : ((of _ i r * r') n : R) = r * r' j := begin classical, rw coe_mul_apply_eq_dfinsupp_sum, apply (dfinsupp.sum_single_index _).trans, swap, { simp_rw [zero_mem_class.coe_zero, zero_mul, if_t_t], exact dfinsupp.sum_zero }, simp_rw [dfinsupp.sum, H, finset.sum_ite_eq'], split_ifs, refl, rw [dfinsupp.not_mem_support_iff.mp h, zero_mem_class.coe_zero, mul_zero], end lemma coe_mul_of_apply_aux [add_monoid ΞΉ] [set_like.graded_monoid A] (r : ⨁ i, A i) {i : ΞΉ} (r' : A i) {j n : ΞΉ} (H : βˆ€ (x : ΞΉ), x + i = n ↔ x = j) : ((r * of _ i r') n : R) = r j * r' := begin classical, rw [coe_mul_apply_eq_dfinsupp_sum, dfinsupp.sum_comm], apply (dfinsupp.sum_single_index _).trans, swap, { simp_rw [zero_mem_class.coe_zero, mul_zero, if_t_t], exact dfinsupp.sum_zero }, simp_rw [dfinsupp.sum, H, finset.sum_ite_eq'], split_ifs, refl, rw [dfinsupp.not_mem_support_iff.mp h, zero_mem_class.coe_zero, zero_mul], end lemma coe_of_mul_apply_add [add_left_cancel_monoid ΞΉ] [set_like.graded_monoid A] {i : ΞΉ} (r : A i) (r' : ⨁ i, A i) (j : ΞΉ) : ((of _ i r * r') (i + j) : R) = r * r' j := coe_of_mul_apply_aux _ _ _ (Ξ» x, ⟨λ h, add_left_cancel h, Ξ» h, h β–Έ rfl⟩) lemma coe_mul_of_apply_add [add_right_cancel_monoid ΞΉ] [set_like.graded_monoid A] (r : ⨁ i, A i) {i : ΞΉ} (r' : A i) (j : ΞΉ) : ((r * of _ i r') (j + i) : R) = r j * r' := coe_mul_of_apply_aux _ _ _ (Ξ» x, ⟨λ h, add_right_cancel h, Ξ» h, h β–Έ rfl⟩) end coe section canonically_ordered_add_monoid variables [semiring R] [set_like Οƒ R] [add_submonoid_class Οƒ R] (A : ΞΉ β†’ Οƒ) variables [canonically_ordered_add_monoid ΞΉ] [set_like.graded_monoid A] lemma coe_of_mul_apply_of_not_le {i : ΞΉ} (r : A i) (r' : ⨁ i, A i) (n : ΞΉ) (h : Β¬ i ≀ n) : ((of _ i r * r') n : R) = 0 := begin classical, rw coe_mul_apply_eq_dfinsupp_sum, apply (dfinsupp.sum_single_index _).trans, swap, { simp_rw [zero_mem_class.coe_zero, zero_mul, if_t_t], exact dfinsupp.sum_zero }, { rw [dfinsupp.sum, finset.sum_ite_of_false _ _ (Ξ» x _ H, _), finset.sum_const_zero], exact h ((self_le_add_right i x).trans_eq H) }, end lemma coe_mul_of_apply_of_not_le (r : ⨁ i, A i) {i : ΞΉ} (r' : A i) (n : ΞΉ) (h : Β¬ i ≀ n) : ((r * of _ i r') n : R) = 0 := begin classical, rw [coe_mul_apply_eq_dfinsupp_sum, dfinsupp.sum_comm], apply (dfinsupp.sum_single_index _).trans, swap, { simp_rw [zero_mem_class.coe_zero, mul_zero, if_t_t], exact dfinsupp.sum_zero }, { rw [dfinsupp.sum, finset.sum_ite_of_false _ _ (Ξ» x _ H, _), finset.sum_const_zero], exact h ((self_le_add_left i x).trans_eq H) }, end variables [has_sub ΞΉ] [has_ordered_sub ΞΉ] [contravariant_class ΞΉ ΞΉ (+) (≀)] /- The following two lemmas only require the same hypotheses as `eq_tsub_iff_add_eq_of_le`, but we state them for `canonically_ordered_add_monoid` + the above three typeclasses for convenience. -/ lemma coe_mul_of_apply_of_le (r : ⨁ i, A i) {i : ΞΉ} (r' : A i) (n : ΞΉ) (h : i ≀ n) : ((r * of _ i r') n : R) = r (n - i) * r' := coe_mul_of_apply_aux _ _ _ (Ξ» x, (eq_tsub_iff_add_eq_of_le h).symm) lemma coe_of_mul_apply_of_le {i : ΞΉ} (r : A i) (r' : ⨁ i, A i) (n : ΞΉ) (h : i ≀ n) : ((of _ i r * r') n : R) = r * r' (n - i) := coe_of_mul_apply_aux _ _ _ (Ξ» x, by rw [eq_tsub_iff_add_eq_of_le h, add_comm]) lemma coe_mul_of_apply (r : ⨁ i, A i) {i : ΞΉ} (r' : A i) (n : ΞΉ) [decidable (i ≀ n)] : ((r * of _ i r') n : R) = if i ≀ n then r (n - i) * r' else 0 := by { split_ifs, exacts [coe_mul_of_apply_of_le _ _ _ n h, coe_mul_of_apply_of_not_le _ _ _ n h] } lemma coe_of_mul_apply {i : ΞΉ} (r : A i) (r' : ⨁ i, A i) (n : ΞΉ) [decidable (i ≀ n)] : ((of _ i r * r') n : R) = if i ≀ n then r * r' (n - i) else 0 := by { split_ifs, exacts [coe_of_mul_apply_of_le _ _ _ n h, coe_of_mul_apply_of_not_le _ _ _ n h] } end canonically_ordered_add_monoid end direct_sum /-! #### From `submodule`s -/ namespace submodule /-- Build a `galgebra` instance for a collection of `submodule`s. -/ instance galgebra [add_monoid ΞΉ] [comm_semiring S] [semiring R] [algebra S R] (A : ΞΉ β†’ submodule S R) [set_like.graded_monoid A] : direct_sum.galgebra S (Ξ» i, A i) := { to_fun := ((algebra.linear_map S R).cod_restrict (A 0) $ set_like.algebra_map_mem_graded A).to_add_monoid_hom, map_one := subtype.ext $ by exact (algebra_map S R).map_one, map_mul := Ξ» x y, sigma.subtype_ext (add_zero 0).symm $ (algebra_map S R).map_mul _ _, commutes := Ξ» r ⟨i, xi⟩, sigma.subtype_ext ((zero_add i).trans (add_zero i).symm) $ algebra.commutes _ _, smul_def := Ξ» r ⟨i, xi⟩, sigma.subtype_ext (zero_add i).symm $ algebra.smul_def _ _ } @[simp] lemma set_like.coe_galgebra_to_fun [add_monoid ΞΉ] [comm_semiring S] [semiring R] [algebra S R] (A : ΞΉ β†’ submodule S R) [set_like.graded_monoid A] (s : S) : ↑(@direct_sum.galgebra.to_fun _ S (Ξ» i, A i) _ _ _ _ _ _ _ s) = (algebra_map S R s : R) := rfl /-- A direct sum of powers of a submodule of an algebra has a multiplicative structure. -/ instance nat_power_graded_monoid [comm_semiring S] [semiring R] [algebra S R] (p : submodule S R) : set_like.graded_monoid (Ξ» i : β„•, p ^ i) := { one_mem := by { rw [←one_le, pow_zero], exact le_rfl }, mul_mem := Ξ» i j p q hp hq, by { rw pow_add, exact submodule.mul_mem_mul hp hq } } end submodule /-- The canonical algebra isomorphism between `⨁ i, A i` and `R`. -/ def direct_sum.coe_alg_hom [add_monoid ΞΉ] [comm_semiring S] [semiring R] [algebra S R] (A : ΞΉ β†’ submodule S R) [set_like.graded_monoid A] : (⨁ i, A i) →ₐ[S] R := direct_sum.to_algebra S _ (Ξ» i, (A i).subtype) rfl (Ξ» _ _ _ _, rfl) (Ξ» _, rfl) /-- The supremum of submodules that form a graded monoid is a subalgebra, and equal to the range of `direct_sum.coe_alg_hom`. -/ lemma submodule.supr_eq_to_submodule_range [add_monoid ΞΉ] [comm_semiring S] [semiring R] [algebra S R] (A : ΞΉ β†’ submodule S R) [set_like.graded_monoid A] : (⨆ i, A i) = (direct_sum.coe_alg_hom A).range.to_submodule := (submodule.supr_eq_range_dfinsupp_lsum A).trans $ set_like.coe_injective rfl @[simp] lemma direct_sum.coe_alg_hom_of [add_monoid ΞΉ] [comm_semiring S] [semiring R] [algebra S R] (A : ΞΉ β†’ submodule S R) [set_like.graded_monoid A] (i : ΞΉ) (x : A i) : direct_sum.coe_alg_hom A (direct_sum.of (Ξ» i, A i) i x) = x := direct_sum.to_semiring_of _ rfl (Ξ» _ _ _ _, rfl) _ _ end direct_sum section homogeneous_element lemma set_like.is_homogeneous_zero_submodule [has_zero ΞΉ] [semiring S] [add_comm_monoid R] [module S R] (A : ΞΉ β†’ submodule S R) : set_like.is_homogeneous A (0 : R) := ⟨0, submodule.zero_mem _⟩ lemma set_like.is_homogeneous.smul [comm_semiring S] [semiring R] [algebra S R] {A : ΞΉ β†’ submodule S R} {s : S} {r : R} (hr : set_like.is_homogeneous A r) : set_like.is_homogeneous A (s β€’ r) := let ⟨i, hi⟩ := hr in ⟨i, submodule.smul_mem _ _ hi⟩ end homogeneous_element
77f3bf8bfde9e9b070a0a8307e7a8ad23be19c02
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/normed_space/star/multiplier.lean
039c59151145bccaa3684797273163273915768f
[ "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
25,927
lean
/- Copyright (c) 2022 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux, Jon Bannon -/ import algebra.star.star_alg_hom import analysis.normed_space.star.basic import analysis.normed_space.operator_norm import analysis.special_functions.pow import analysis.normed_space.star.mul /-! # Multiplier Algebra of a C⋆-algebra Define the multiplier algebra of a C⋆-algebra as the algebra (over `π•œ`) of double centralizers, for which we provide the localized notation `π“œ(π•œ, A)`. A double centralizer is a pair of continuous linear maps `L R : A β†’L[π•œ] A` satisfying the intertwining condition `R x * y = x * L y`. There is a natural embedding `A β†’ π“œ(π•œ, A)` which sends `a : A` to the continuous linear maps `L R : A β†’L[π•œ] A` given by left and right multiplication by `a`, and we provide this map as a coercion. The multiplier algebra corresponds to a non-commutative Stoneβ€“ΔŒech compactification in the sense that when the algebra `A` is commutative, it can be identified with `Cβ‚€(X, β„‚)` for some locally compact Hausdorff space `X`, and in that case `π“œ(π•œ, A)` can be identified with `C(Ξ² X, β„‚)`. ## Implementation notes We make the hypotheses on `π•œ` as weak as possible so that, in particular, this construction works for both `π•œ = ℝ` and `π•œ = β„‚`. The reader familiar with C⋆-algebra theory may recognize that one only needs `L` and `R` to be functions instead of continuous linear maps, at least when `A` is a C⋆-algebra. Our intention is simply to eventually provide a constructor for this situation. We pull back the `normed_algebra` structure (and everything contained therein) through the ring (even algebra) homomorphism `double_centralizer.to_prod_mul_opposite_hom : π“œ(π•œ, A) β†’+* (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)ᡐᡒᡖ` which sends `a : π“œ(π•œ, A)` to `(a.fst, mul_opposite.op a.snd)`. The star structure is provided separately. ## References * https://en.wikipedia.org/wiki/Multiplier_algebra ## TODO + Define a type synonym for `π“œ(π•œ, A)` which is equipped with the strict uniform space structure and show it is complete + Show that the image of `A` in `π“œ(π•œ, A)` is an essential ideal + Prove the universal property of `π“œ(π•œ, A)` + Construct a double centralizer from a pair of maps (not necessarily linear or continuous) `L : A β†’ A`, `R : A β†’ A` satisfying the centrality condition `βˆ€ x y, R x * y = x * L y`. + Show that if `A` is unital, then `A ≃⋆ₐ[π•œ] π“œ(π•œ, A)`. -/ open_locale nnreal ennreal open nnreal continuous_linear_map mul_opposite universes u v /-- The type of *double centralizers*, also known as the *multiplier algebra* and denoted by `π“œ(π•œ, A)`, of a non-unital normed algebra. If `x : π“œ(π•œ, A)`, then `x.fst` and `x.snd` are what is usually referred to as $L$ and $R$. -/ @[ext] structure double_centralizer (π•œ : Type u) (A : Type v) [nontrivially_normed_field π•œ] [non_unital_normed_ring A] [normed_space π•œ A] [smul_comm_class π•œ A A] [is_scalar_tower π•œ A A] extends (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A) := (central : βˆ€ x y : A, snd x * y = x * fst y) localized "notation `π“œ(` π•œ `, ` A `)` := double_centralizer π•œ A" in multiplier_algebra namespace double_centralizer section nontrivially_normed variables (π•œ A : Type*) [nontrivially_normed_field π•œ] [non_unital_normed_ring A] variables [normed_space π•œ A] [smul_comm_class π•œ A A] [is_scalar_tower π•œ A A] /-! ### Algebraic structure Because the multiplier algebra is defined as the algebra of double centralizers, there is a natural injection `double_centralizer.to_prod_mul_opposite : π“œ(π•œ, A) β†’ (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)ᡐᡒᡖ` defined by `Ξ» a, (a.fst, mul_opposite.op a.snd)`. We use this map to pull back the ring, module and algebra structure from `(A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)ᡐᡒᡖ` to `π“œ(π•œ, A)`. -/ variables {π•œ A} lemma range_to_prod : set.range to_prod = {lr : (A β†’L[π•œ] A) Γ— _ | βˆ€ x y, lr.2 x * y = x * lr.1 y} := set.ext $ Ξ» x, ⟨by {rintro ⟨a, rfl⟩, exact a.central}, Ξ» hx, ⟨⟨x, hx⟩, rfl⟩⟩ instance : has_add π“œ(π•œ, A) := { add := Ξ» a b, { to_prod := a.to_prod + b.to_prod, central := Ξ» x y, show (a.snd + b.snd) x * y = x * (a.fst + b.fst) y, by simp only [continuous_linear_map.add_apply, mul_add, add_mul, central] } } instance : has_zero π“œ(π•œ, A) := { zero := { to_prod := 0, central := Ξ» x y, (zero_mul y).trans (mul_zero x).symm } } instance : has_neg π“œ(π•œ, A) := { neg := Ξ» a, { to_prod := -a.to_prod, central := Ξ» x y, show -a.snd x * y = x * -a.fst y, by simp only [continuous_linear_map.neg_apply, neg_mul, mul_neg, central] } } instance : has_sub π“œ(π•œ, A) := { sub := Ξ» a b, { to_prod := a.to_prod - b.to_prod, central := Ξ» x y, show (a.snd - b.snd) x * y = x * (a.fst - b.fst) y, by simp only [continuous_linear_map.sub_apply, sub_mul, mul_sub, central] } } section scalars variables {S : Type*} [monoid S] [distrib_mul_action S A] [smul_comm_class π•œ S A] [has_continuous_const_smul S A] [is_scalar_tower S A A] [smul_comm_class S A A] instance : has_smul S π“œ(π•œ, A) := { smul := Ξ» s a, { to_prod := s β€’ a.to_prod, central := Ξ» x y, show (s β€’ a.snd) x * y = x * (s β€’ a.fst) y, by simp only [continuous_linear_map.smul_apply, mul_smul_comm, smul_mul_assoc, central] } } @[simp] lemma smul_to_prod (s : S) (a : π“œ(π•œ, A)) : (s β€’ a).to_prod = s β€’ a.to_prod := rfl lemma smul_fst (s : S) (a : π“œ(π•œ, A)) : (s β€’ a).fst = s β€’ a.fst := rfl lemma smul_snd (s : S) (a : π“œ(π•œ, A)) : (s β€’ a).snd = s β€’ a.snd := rfl variables {T : Type*} [monoid T] [distrib_mul_action T A] [smul_comm_class π•œ T A] [has_continuous_const_smul T A] [is_scalar_tower T A A] [smul_comm_class T A A] instance [has_smul S T] [is_scalar_tower S T A] : is_scalar_tower S T π“œ(π•œ, A) := { smul_assoc := Ξ» _ _ a, ext _ _ $ smul_assoc _ _ a.to_prod } instance [smul_comm_class S T A] : smul_comm_class S T π“œ(π•œ, A) := { smul_comm := Ξ» _ _ a, ext _ _ $ smul_comm _ _ a.to_prod } instance {R : Type*} [semiring R] [module R A] [smul_comm_class π•œ R A] [has_continuous_const_smul R A] [is_scalar_tower R A A] [smul_comm_class R A A] [module Rᡐᡒᡖ A] [is_central_scalar R A] : is_central_scalar R π“œ(π•œ, A) := { op_smul_eq_smul := Ξ» _ a, ext _ _ $ op_smul_eq_smul _ a.to_prod } end scalars instance : has_one π“œ(π•œ, A) := ⟨⟨1, Ξ» x y, rfl⟩⟩ instance : has_mul π“œ(π•œ, A) := { mul := Ξ» a b, { to_prod := (a.fst.comp b.fst, b.snd.comp a.snd), central := Ξ» x y, show b.snd (a.snd x) * y = x * a.fst (b.fst y), by simp only [central] } } instance : has_nat_cast π“œ(π•œ, A) := { nat_cast := Ξ» n, ⟨n, Ξ» x y, begin rw [prod.snd_nat_cast, prod.fst_nat_cast], simp only [←nat.smul_one_eq_coe, smul_apply, one_apply, mul_smul_comm, smul_mul_assoc], end⟩ } instance : has_int_cast π“œ(π•œ, A) := { int_cast := Ξ» n, ⟨n, Ξ» x y, begin rw [prod.snd_int_cast, prod.fst_int_cast], simp only [←int.smul_one_eq_coe, smul_apply, one_apply, mul_smul_comm, smul_mul_assoc], end⟩ } instance : has_pow π“œ(π•œ, A) β„• := { pow := Ξ» a n, ⟨a.to_prod ^ n, Ξ» x y, begin induction n with k hk generalizing x y, { refl }, { rw [prod.pow_snd, prod.pow_fst] at hk ⊒, rw [pow_succ a.snd, mul_apply, a.central, hk, pow_succ' a.fst, mul_apply] }, end⟩ } instance : inhabited π“œ(π•œ, A) := ⟨0⟩ @[simp] lemma add_to_prod (a b : π“œ(π•œ, A)) : (a + b).to_prod = a.to_prod + b.to_prod := rfl @[simp] lemma zero_to_prod : (0 : π“œ(π•œ, A)).to_prod = 0 := rfl @[simp] lemma neg_to_prod (a : π“œ(π•œ, A)) : (-a).to_prod = -a.to_prod := rfl @[simp] lemma sub_to_prod (a b : π“œ(π•œ, A)) : (a - b).to_prod = a.to_prod - b.to_prod := rfl @[simp] lemma one_to_prod : (1 : π“œ(π•œ, A)).to_prod = 1 := rfl @[simp] lemma nat_cast_to_prod (n : β„•) : (n : π“œ(π•œ , A)).to_prod = n := rfl @[simp] lemma int_cast_to_prod (n : β„€) : (n : π“œ(π•œ , A)).to_prod = n := rfl @[simp] lemma pow_to_prod (n : β„•) (a : π“œ(π•œ, A)) : (a ^ n).to_prod = a.to_prod ^ n := rfl lemma add_fst (a b : π“œ(π•œ, A)) : (a + b).fst = a.fst + b.fst := rfl lemma add_snd (a b : π“œ(π•œ, A)) : (a + b).snd = a.snd + b.snd := rfl lemma zero_fst : (0 : π“œ(π•œ, A)).fst = 0 := rfl lemma zero_snd : (0 : π“œ(π•œ, A)).snd = 0 := rfl lemma neg_fst (a : π“œ(π•œ, A)) : (-a).fst = -a.fst := rfl lemma neg_snd (a : π“œ(π•œ, A)) : (-a).snd = -a.snd := rfl lemma sub_fst (a b : π“œ(π•œ, A)) : (a - b).fst = a.fst - b.fst := rfl lemma sub_snd (a b : π“œ(π•œ, A)) : (a - b).snd = a.snd - b.snd := rfl lemma one_fst : (1 : π“œ(π•œ, A)).fst = 1 := rfl lemma one_snd : (1 : π“œ(π•œ, A)).snd = 1 := rfl @[simp] lemma mul_fst (a b : π“œ(π•œ, A)) : (a * b).fst = a.fst * b.fst := rfl @[simp] lemma mul_snd (a b : π“œ(π•œ, A)) : (a * b).snd = b.snd * a.snd := rfl lemma nat_cast_fst (n : β„•) : (n : π“œ(π•œ , A)).fst = n := rfl lemma nat_cast_snd (n : β„•) : (n : π“œ(π•œ , A)).snd = n := rfl lemma int_cast_fst (n : β„€) : (n : π“œ(π•œ , A)).fst = n := rfl lemma int_cast_snd (n : β„€) : (n : π“œ(π•œ , A)).snd = n := rfl lemma pow_fst (n : β„•) (a : π“œ(π•œ, A)) : (a ^ n).fst = a.fst ^ n := rfl lemma pow_snd (n : β„•) (a : π“œ(π•œ, A)) : (a ^ n).snd = a.snd ^ n := rfl /-- The natural injection from `double_centralizer.to_prod` except the second coordinate inherits `mul_opposite.op`. The ring structure on `π“œ(π•œ, A)` is the pullback under this map. -/ def to_prod_mul_opposite : π“œ(π•œ, A) β†’ (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)ᡐᡒᡖ := Ξ» a, (a.fst, mul_opposite.op a.snd) lemma to_prod_mul_opposite_injective : function.injective (to_prod_mul_opposite : π“œ(π•œ, A) β†’ (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)ᡐᡒᡖ) := Ξ» a b h, let h' := prod.ext_iff.mp h in ext _ _ $ prod.ext h'.1 $ mul_opposite.op_injective h'.2 lemma range_to_prod_mul_opposite : set.range to_prod_mul_opposite = {lr : (A β†’L[π•œ] A) Γ— _ | βˆ€ x y, unop lr.2 x * y = x * lr.1 y} := set.ext $ Ξ» x, ⟨by {rintro ⟨a, rfl⟩, exact a.central}, Ξ» hx, ⟨⟨(x.1, unop x.2), hx⟩, prod.ext rfl rfl⟩⟩ /-- The ring structure is inherited as the pullback under the injective map `double_centralizer.to_prod_mul_opposite : π“œ(π•œ, A) β†’ (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)ᡐᡒᡖ` -/ instance : ring π“œ(π•œ, A) := to_prod_mul_opposite_injective.ring _ rfl rfl (Ξ» _ _, rfl) (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl) (Ξ» x n, prod.ext rfl $ mul_opposite.op_smul _ _) (Ξ» x n, prod.ext rfl $ mul_opposite.op_smul _ _) (Ξ» x n, prod.ext rfl $ mul_opposite.op_pow _ _) (Ξ» _, rfl) (Ξ» _, rfl) /-- The canonical map `double_centralizer.to_prod` as an additive group homomorphism. -/ @[simps] def to_prod_hom : π“œ(π•œ, A) β†’+ (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A) := { to_fun := to_prod, map_zero' := rfl, map_add' := Ξ» x y, rfl } /-- The canonical map `double_centralizer.to_prod_mul_opposite` as a ring homomorphism. -/ @[simps] def to_prod_mul_opposite_hom : π“œ(π•œ, A) β†’+* (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)ᡐᡒᡖ := { to_fun := to_prod_mul_opposite, map_zero' := rfl, map_one' := rfl, map_add' := Ξ» x y, rfl, map_mul' := Ξ» x y, rfl } /-- The module structure is inherited as the pullback under the additive group monomorphism `double_centralizer.to_prod : π“œ(π•œ, A) β†’+ (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)` -/ instance {S : Type*} [semiring S] [module S A] [smul_comm_class π•œ S A] [has_continuous_const_smul S A] [is_scalar_tower S A A] [smul_comm_class S A A] : module S π“œ(π•œ, A) := function.injective.module S to_prod_hom ext (Ξ» x y, rfl) -- TODO: generalize to `algebra S π“œ(π•œ, A)` once `continuous_linear_map.algebra` is generalized. instance : algebra π•œ π“œ(π•œ, A) := { to_fun := Ξ» k, { to_prod := algebra_map π•œ ((A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)) k, central := Ξ» x y, by simp_rw [prod.algebra_map_apply, algebra.algebra_map_eq_smul_one, smul_apply, one_apply, mul_smul_comm, smul_mul_assoc] }, map_one' := ext _ _ $ map_one $ algebra_map π•œ ((A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)), map_mul' := Ξ» k₁ kβ‚‚, ext _ _ $ prod.ext (map_mul (algebra_map π•œ (A β†’L[π•œ] A)) _ _) ((map_mul (algebra_map π•œ (A β†’L[π•œ] A)) _ _).trans (algebra.commutes _ _)), map_zero' := ext _ _ $ map_zero $ algebra_map π•œ ((A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)), map_add' := Ξ» _ _, ext _ _ $ map_add (algebra_map π•œ ((A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A))) _ _, commutes' := Ξ» _ _, ext _ _ $ prod.ext (algebra.commutes _ _) (algebra.commutes _ _).symm, smul_def' := Ξ» _ _, ext _ _ $ prod.ext (algebra.smul_def _ _) ((algebra.smul_def _ _).trans $ algebra.commutes _ _) } @[simp] lemma algebra_map_to_prod (k : π•œ) : (algebra_map π•œ π“œ(π•œ, A) k).to_prod = algebra_map π•œ _ k := rfl lemma algebra_map_fst (k : π•œ) : (algebra_map π•œ π“œ(π•œ, A) k).fst = algebra_map π•œ _ k := rfl lemma algebra_map_snd (k : π•œ) : (algebra_map π•œ π“œ(π•œ, A) k).snd = algebra_map π•œ _ k := rfl /-! ### Star structure -/ section star variables [star_ring π•œ] [star_ring A] [star_module π•œ A] [normed_star_group A] /-- The star operation on `a : π“œ(π•œ, A)` is given by `(star a).to_prod = (star ∘ a.snd ∘ star, star ∘ a.fst ∘ star)`. -/ instance : has_star π“œ(π•œ, A) := { star := Ξ» a, { fst := (((starβ‚—α΅’ π•œ : A ≃ₗᡒ⋆[π•œ] A) : A β†’L⋆[π•œ] A).comp a.snd).comp ((starβ‚—α΅’ π•œ : A ≃ₗᡒ⋆[π•œ] A) : A β†’L⋆[π•œ] A), snd := (((starβ‚—α΅’ π•œ : A ≃ₗᡒ⋆[π•œ] A) : A β†’L⋆[π•œ] A).comp a.fst).comp ((starβ‚—α΅’ π•œ : A ≃ₗᡒ⋆[π•œ] A) : A β†’L⋆[π•œ] A), central := Ξ» x y, by simpa only [star_mul, star_star] using (congr_arg star (a.central (star y) (star x))).symm } } @[simp] lemma star_fst (a : π“œ(π•œ, A)) (b : A) : (star a).fst b = star (a.snd (star b)) := rfl @[simp] lemma star_snd (a : π“œ(π•œ, A)) (b : A) : (star a).snd b = star (a.fst (star b)) := rfl instance : star_add_monoid π“œ(π•œ, A) := { star_involutive := Ξ» x, by {ext; simp only [star_fst, star_snd, star_star]}, star_add := Ξ» x y, by {ext; simp only [star_fst, star_snd, add_fst, add_snd, continuous_linear_map.add_apply, star_add]}, .. double_centralizer.has_star } instance : star_ring π“œ(π•œ, A) := { star_mul := Ξ» a b, by {ext; simp only [star_fst, star_snd, mul_fst, mul_snd, star_star, continuous_linear_map.coe_mul, function.comp_app]}, .. double_centralizer.star_add_monoid } instance : star_module π•œ π“œ(π•œ, A) := { star_smul := Ξ» k a, by {ext; exact star_smul _ _}, .. double_centralizer.star_add_monoid } end star /-! ### Coercion from an algebra into its multiplier algebra -/ /-- The natural coercion of `A` into `π“œ(π•œ, A)` given by sending `a : A` to the pair of linear maps `Lₐ Rₐ : A β†’L[π•œ] A` given by left- and right-multiplication by `a`, respectively. Warning: if `A = π•œ`, then this is a coercion which is not definitionally equal to the `algebra_map π•œ π“œ(π•œ, π•œ)` coercion, but these are propositionally equal. See `double_centralizer.coe_eq_algebra_map` below. -/ noncomputable instance : has_coe_t A π“œ(π•œ, A) := { coe := Ξ» a, { fst := continuous_linear_map.mul π•œ A a, snd := (continuous_linear_map.mul π•œ A).flip a, central := Ξ» x y, mul_assoc _ _ _ } } @[simp, norm_cast] lemma coe_fst (a : A) : (a : π“œ(π•œ, A)).fst = continuous_linear_map.mul π•œ A a := rfl @[simp, norm_cast] lemma coe_snd (a : A) : (a : π“œ(π•œ, A)).snd = (continuous_linear_map.mul π•œ A).flip a := rfl lemma coe_eq_algebra_map : (coe : π•œ β†’ π“œ(π•œ, π•œ)) = algebra_map π•œ π“œ(π•œ, π•œ) := begin ext; simp only [coe_fst, mul_apply', mul_one, algebra_map_to_prod, prod.algebra_map_apply, coe_snd, flip_apply, one_mul]; simp only [algebra.algebra_map_eq_smul_one, smul_apply, one_apply, smul_eq_mul, mul_one], end /-- The coercion of an algebra into its multiplier algebra as a non-unital star algebra homomorphism. -/ @[simps] noncomputable def coe_hom [star_ring π•œ] [star_ring A] [star_module π•œ A] [normed_star_group A] : A →⋆ₙₐ[π•œ] π“œ(π•œ, A) := { to_fun := Ξ» a, a, map_smul' := Ξ» k a, by ext; simp only [coe_fst, coe_snd, continuous_linear_map.map_smul, smul_fst, smul_snd], map_zero' := by ext; simp only [coe_fst, coe_snd, map_zero, zero_fst, zero_snd], map_add' := Ξ» a b, by ext; simp only [coe_fst, coe_snd, map_add, add_fst, add_snd], map_mul' := Ξ» a b, by ext; simp only [coe_fst, coe_snd, mul_apply', flip_apply, mul_fst, mul_snd, continuous_linear_map.coe_mul, function.comp_app, mul_assoc], map_star' := Ξ» a, by ext; simp only [coe_fst, coe_snd, mul_apply', star_fst, star_snd, flip_apply, star_mul, star_star] } /-! ### Norm structures We define the norm structure on `π“œ(π•œ, A)` as the pullback under `double_centralizer.to_prod_mul_opposite_hom : π“œ(π•œ, A) β†’+* (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)ᡐᡒᡖ`, which provides a definitional isometric embedding. Consequently, completeness of `π“œ(π•œ, A)` is obtained by proving that the range of this map is closed. In addition, we prove that `π“œ(π•œ, A)` is a normed algebra, and, when `A` is a C⋆-algebra, we show that `π“œ(π•œ, A)` is also a C⋆-algebra. Moreover, in this case, for `a : π“œ(π•œ, A)`, `β€–aβ€– = β€–a.fstβ€– = β€–a.sndβ€–`. -/ /-- The normed group structure is inherited as the pullback under the ring monomoprhism `double_centralizer.to_prod_mul_opposite_hom : π“œ(π•œ, A) β†’+* (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)ᡐᡒᡖ`. -/ noncomputable instance : normed_ring π“œ(π•œ, A) := normed_ring.induced _ _ (to_prod_mul_opposite_hom : π“œ(π•œ, A) β†’+* (A β†’L[π•œ] A) Γ— (A β†’L[π•œ] A)ᡐᡒᡖ) to_prod_mul_opposite_injective -- even though the definition is actually in terms of `double_centralizer.to_prod_mul_opposite`, we -- choose to see through that here to avoid `mul_opposite.op` appearing. lemma norm_def (a : π“œ(π•œ, A)) : β€–aβ€– = β€–a.to_prod_homβ€– := rfl lemma nnnorm_def (a : π“œ(π•œ, A)) : β€–aβ€–β‚Š = β€–a.to_prod_homβ€–β‚Š := rfl lemma norm_def' (a : π“œ(π•œ, A)) : β€–aβ€– = β€–a.to_prod_mul_opposite_homβ€– := rfl lemma nnnorm_def' (a : π“œ(π•œ, A)) : β€–aβ€–β‚Š = β€–a.to_prod_mul_opposite_homβ€–β‚Š := rfl instance : normed_space π•œ π“œ(π•œ, A) := { norm_smul_le := Ξ» k a, norm_smul_le k a.to_prod_mul_opposite, .. double_centralizer.module } instance : normed_algebra π•œ π“œ(π•œ, A) := { ..double_centralizer.algebra, ..double_centralizer.normed_space } lemma uniform_embedding_to_prod_mul_opposite : uniform_embedding (@to_prod_mul_opposite π•œ A _ _ _ _ _) := uniform_embedding_comap to_prod_mul_opposite_injective instance [complete_space A] : complete_space π“œ(π•œ, A) := begin rw complete_space_iff_is_complete_range uniform_embedding_to_prod_mul_opposite.to_uniform_inducing, apply is_closed.is_complete, simp only [range_to_prod_mul_opposite, set.set_of_forall], refine is_closed_Inter (Ξ» x, is_closed_Inter $ Ξ» y, is_closed_eq _ _), exact ((continuous_linear_map.apply π•œ A _).continuous.comp $ continuous_unop.comp continuous_snd).mul continuous_const, exact continuous_const.mul ((continuous_linear_map.apply π•œ A _).continuous.comp continuous_fst), end variables [star_ring A] [cstar_ring A] /-- For `a : π“œ(π•œ, A)`, the norms of `a.fst` and `a.snd` coincide, and hence these also coincide with `β€–aβ€–` which is `max (β€–a.fstβ€–) (β€–a.sndβ€–)`. -/ lemma norm_fst_eq_snd (a : π“œ(π•œ, A)) : β€–a.fstβ€– = β€–a.sndβ€– := begin -- a handy lemma for this proof have h0 : βˆ€ f : A β†’L[π•œ] A, βˆ€ C : ℝβ‰₯0, (βˆ€ b : A, β€–f bβ€–β‚Š ^ 2 ≀ C * β€–f bβ€–β‚Š * β€–bβ€–β‚Š) β†’ β€–fβ€–β‚Š ≀ C, { intros f C h, have h1 : βˆ€ b, C * β€–f bβ€–β‚Š * β€–bβ€–β‚Š ≀ C * β€–fβ€–β‚Š * β€–bβ€–β‚Š ^ 2, { intros b, convert mul_le_mul_right' (mul_le_mul_left' (f.le_op_nnnorm b) C) (β€–bβ€–β‚Š) using 1, ring, }, have := div_le_of_le_mul (f.op_nnnorm_le_bound _ (by simpa only [sqrt_sq, sqrt_mul] using (Ξ» b, sqrt_le_sqrt_iff.mpr ((h b).trans (h1 b))))), convert rpow_le_rpow this two_pos.le, { simp only [rpow_two, div_pow, sq_sqrt], simp only [sq, mul_self_div_self] }, { simp only [rpow_two, sq_sqrt] } }, have h1 : βˆ€ b, β€–a.fst bβ€–β‚Š ^ 2 ≀ β€–a.sndβ€–β‚Š * β€–a.fst bβ€–β‚Š * β€–bβ€–β‚Š, { intros b, calc β€–a.fst bβ€–β‚Š ^ 2 = β€–star (a.fst b) * (a.fst b)β€–β‚Š : by simpa only [←sq] using (cstar_ring.nnnorm_star_mul_self).symm ... ≀ β€–a.snd (star (a.fst b))β€–β‚Š * β€–bβ€–β‚Š : a.central (star (a.fst b)) b β–Έ nnnorm_mul_le _ _ ... ≀ β€–a.sndβ€–β‚Š * β€–a.fst bβ€–β‚Š * β€–bβ€–β‚Š : nnnorm_star (a.fst b) β–Έ mul_le_mul_right' (a.snd.le_op_nnnorm _) _}, have h2 : βˆ€ b, β€–a.snd bβ€–β‚Š ^ 2 ≀ β€–a.fstβ€–β‚Š * β€–a.snd bβ€–β‚Š * β€–bβ€–β‚Š, { intros b, calc β€–a.snd bβ€–β‚Š ^ 2 = β€–a.snd b * star (a.snd b)β€–β‚Š : by simpa only [←sq] using (cstar_ring.nnnorm_self_mul_star).symm ... ≀ β€–bβ€–β‚Š * β€–a.fst (star (a.snd b))β€–β‚Š : (a.central b (star (a.snd b))).symm β–Έ nnnorm_mul_le _ _ ... = β€–a.fst (star (a.snd b))β€–β‚Š * β€–bβ€–β‚Š : mul_comm _ _ ... ≀ β€–a.fstβ€–β‚Š * β€–a.snd bβ€–β‚Š * β€–bβ€–β‚Š : nnnorm_star (a.snd b) β–Έ mul_le_mul_right' (a.fst.le_op_nnnorm _) _ }, exact le_antisymm (h0 _ _ h1) (h0 _ _ h2), end lemma nnnorm_fst_eq_snd (a : π“œ(π•œ, A)) : β€–a.fstβ€–β‚Š = β€–a.sndβ€–β‚Š := subtype.ext $ norm_fst_eq_snd a @[simp] lemma norm_fst (a : π“œ(π•œ, A)) : β€–a.fstβ€– = β€–aβ€– := by simp only [norm_def, to_prod_hom_apply, prod.norm_def, norm_fst_eq_snd, max_eq_right, eq_self_iff_true] @[simp] lemma norm_snd (a : π“œ(π•œ, A)) : β€–a.sndβ€– = β€–aβ€– := by rw [←norm_fst, norm_fst_eq_snd] @[simp] lemma nnnorm_fst (a : π“œ(π•œ, A)) : β€–a.fstβ€–β‚Š = β€–aβ€–β‚Š := subtype.ext (norm_fst a) @[simp] lemma nnnorm_snd (a : π“œ(π•œ, A)) : β€–a.sndβ€–β‚Š = β€–aβ€–β‚Š := subtype.ext (norm_snd a) end nontrivially_normed section densely_normed variables {π•œ A : Type*} [densely_normed_field π•œ] [star_ring π•œ] variables [non_unital_normed_ring A] [star_ring A] [cstar_ring A] variables [normed_space π•œ A] [smul_comm_class π•œ A A] [is_scalar_tower π•œ A A] [star_module π•œ A] instance : cstar_ring π“œ(π•œ, A) := { norm_star_mul_self := Ξ» a, congr_arg (coe : ℝβ‰₯0 β†’ ℝ) $ show β€–star a * aβ€–β‚Š = β€–aβ€–β‚Š * β€–aβ€–β‚Š, from begin /- The essence of the argument is this: let `a = (L,R)` and recall `β€–aβ€– = β€–Lβ€–`. `star a = (star ∘ R ∘ star, star ∘ L ∘ star)`. Then for any `x y : A`, we have `β€–star a * aβ€– = β€–(star a * a).sndβ€– = β€–R (star (L (star x))) * yβ€– = β€–star (L (star x)) * L yβ€–` Now, on the one hand, `β€–star (L (star x)) * L yβ€– ≀ β€–star (L (star x))β€– * β€–L yβ€– = β€–L (star x)β€– * β€–L yβ€– ≀ β€–Lβ€– ^ 2` whenever `β€–xβ€–, β€–yβ€– ≀ 1`, so the supremum over all such `x, y` is at most `β€–Lβ€– ^ 2`. On the other hand, for any `β€–zβ€– ≀ 1`, we may choose `x := star z` and `y := z` to get: `β€–star (L (star x)) * L yβ€– = β€–star (L z) * (L z)β€– = β€–L zβ€– ^ 2`, and taking the supremum over all such `z` yields that the supremum is at least `β€–Lβ€– ^ 2`. It is the latter part of the argument where `densely_normed_field π•œ` is required (for `Sup_closed_unit_ball_eq_nnnorm`). -/ have hball : (metric.closed_ball (0 : A) 1).nonempty := metric.nonempty_closed_ball.2 (zero_le_one), have key : βˆ€ x y, β€–xβ€–β‚Š ≀ 1 β†’ β€–yβ€–β‚Š ≀ 1 β†’ β€–a.snd (star (a.fst (star x))) * yβ€–β‚Š ≀ β€–aβ€–β‚Š * β€–aβ€–β‚Š, { intros x y hx hy, rw [a.central], calc β€–star (a.fst (star x)) * a.fst yβ€–β‚Š ≀ β€–a.fst (star x)β€–β‚Š * β€–a.fst yβ€–β‚Š : nnnorm_star (a.fst (star x)) β–Έ nnnorm_mul_le _ _ ... ≀ (β€–a.fstβ€–β‚Š * 1) * (β€–a.fstβ€–β‚Š * 1) : mul_le_mul' (a.fst.le_op_norm_of_le ((nnnorm_star x).trans_le hx)) (a.fst.le_op_norm_of_le hy) ... ≀ β€–aβ€–β‚Š * β€–aβ€–β‚Š : by simp only [mul_one, nnnorm_fst] }, rw ←nnnorm_snd, simp only [mul_snd, ←Sup_closed_unit_ball_eq_nnnorm, star_snd, mul_apply], simp only [←@op_nnnorm_mul π•œ A], simp only [←Sup_closed_unit_ball_eq_nnnorm, mul_apply'], refine cSup_eq_of_forall_le_of_forall_lt_exists_gt (hball.image _) _ (Ξ» r hr, _), { rintro - ⟨x, hx, rfl⟩, refine cSup_le (hball.image _) _, rintro - ⟨y, hy, rfl⟩, exact key x y (mem_closed_ball_zero_iff.1 hx) (mem_closed_ball_zero_iff.1 hy) }, { simp only [set.mem_image, set.mem_set_of_eq, exists_prop, exists_exists_and_eq_and], have hr' : r.sqrt < β€–aβ€–β‚Š := (β€–aβ€–β‚Š).sqrt_mul_self β–Έ nnreal.sqrt_lt_sqrt_iff.2 hr, simp_rw [←nnnorm_fst, ←Sup_closed_unit_ball_eq_nnnorm] at hr', obtain ⟨_, ⟨x, hx, rfl⟩, hxr⟩ := exists_lt_of_lt_cSup (hball.image _) hr', have hx' : β€–xβ€–β‚Š ≀ 1 := mem_closed_ball_zero_iff.1 hx, refine ⟨star x, mem_closed_ball_zero_iff.2 ((nnnorm_star x).trans_le hx'), _⟩, refine lt_cSup_of_lt _ ⟨x, hx, rfl⟩ _, { refine βŸ¨β€–aβ€–β‚Š * β€–aβ€–β‚Š, _⟩, rintros - ⟨y, hy, rfl⟩, exact key (star x) y ((nnnorm_star x).trans_le hx') (mem_closed_ball_zero_iff.1 hy) }, { simpa only [a.central, star_star, cstar_ring.nnnorm_star_mul_self, nnreal.sq_sqrt, ←sq] using pow_lt_pow_of_lt_left hxr zero_le' two_pos } } end } end densely_normed end double_centralizer
208d630b19be0db0afce25b938371df12054ae57
5d166a16ae129621cb54ca9dde86c275d7d2b483
/library/init/data/int/comp_lemmas.lean
c9a339cc45758de2f3318021e0b7e5ae3f2ba8e9
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
4,832
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 Auxiliary lemmas used to compare int numerals. -/ prelude import init.data.int.order namespace int /- Auxiliary lemmas for proving that to int numerals are different -/ /- 1. Lemmas for reducing the problem to the case where the numerals are positive -/ protected lemma ne_neg_of_ne {a b : β„€} : a β‰  b β†’ -a β‰  -b := Ξ» h₁ hβ‚‚, absurd (neg_inj hβ‚‚) h₁ protected lemma neg_ne_zero_of_ne {a : β„€} : a β‰  0 β†’ -a β‰  0 := Ξ» h₁ hβ‚‚, have -a = -0, by rwa neg_zero, have a = 0, from neg_inj this, by contradiction protected lemma zero_ne_neg_of_ne {a : β„€} (h : 0 β‰  a) : 0 β‰  -a := ne.symm (int.neg_ne_zero_of_ne (ne.symm h)) protected lemma neg_ne_of_pos {a b : β„€} : a > 0 β†’ b > 0 β†’ -a β‰  b := Ξ» h₁ hβ‚‚ h, begin rw [-h] at hβ‚‚, exact absurd (le_of_lt h₁) (not_le_of_gt (neg_of_neg_pos hβ‚‚)) end protected lemma ne_neg_of_pos {a b : β„€} : a > 0 β†’ b > 0 β†’ a β‰  -b := Ξ» h₁ hβ‚‚, ne.symm (int.neg_ne_of_pos hβ‚‚ h₁) /- 2. Lemmas for proving that positive int numerals are nonneg and positive -/ protected lemma one_pos : (1:int) > 0 := zero_lt_one protected lemma bit0_pos {a : β„€} : a > 0 β†’ bit0 a > 0 := Ξ» h, add_pos h h protected lemma bit1_pos {a : β„€} : a β‰₯ 0 β†’ bit1 a > 0 := Ξ» h, lt_add_of_le_of_pos (add_nonneg h h) zero_lt_one protected lemma zero_nonneg : (0:int) β‰₯ 0 := le_refl 0 protected lemma one_nonneg : (1:int) β‰₯ 0 := le_of_lt (zero_lt_one) protected lemma bit0_nonneg {a : β„€} : a β‰₯ 0 β†’ bit0 a β‰₯ 0 := Ξ» h, add_nonneg h h protected lemma bit1_nonneg {a : β„€} : a β‰₯ 0 β†’ bit1 a β‰₯ 0 := Ξ» h, le_of_lt (int.bit1_pos h) protected lemma nonneg_of_pos {a : β„€} : a > 0 β†’ a β‰₯ 0 := le_of_lt /- 3. nat_abs auxiliary lemmas -/ lemma neg_succ_of_nat_lt_zero (n : β„•) : neg_succ_of_nat n < 0 := @lt.intro _ _ n (by simp [neg_succ_of_nat_coe, int.coe_nat_succ, int.coe_nat_add, int.coe_nat_one]) lemma of_nat_ge_zero (n : β„•) : of_nat n β‰₯ 0 := @le.intro _ _ n (by rw [zero_add, int.coe_nat_eq]) lemma of_nat_nat_abs_eq_of_nonneg : βˆ€ {a : β„€}, a β‰₯ 0 β†’ of_nat (nat_abs a) = a | (of_nat n) h := rfl | (neg_succ_of_nat n) h := absurd (neg_succ_of_nat_lt_zero n) (not_lt_of_ge h) lemma ne_of_nat_abs_ne_nat_abs_of_nonneg {a b : β„€} (ha : a β‰₯ 0) (hb : b β‰₯ 0) (h : nat_abs a β‰  nat_abs b) : a β‰  b := assume h, have of_nat (nat_abs a) = of_nat (nat_abs b), by rwa [of_nat_nat_abs_eq_of_nonneg ha, of_nat_nat_abs_eq_of_nonneg hb], begin injection this, contradiction end protected lemma ne_of_nat_ne_nonneg_case {a b : β„€} {n m : nat} (ha : a β‰₯ 0) (hb : b β‰₯ 0) (e1 : nat_abs a = n) (e2 : nat_abs b = m) (h : n β‰  m) : a β‰  b := have nat_abs a β‰  nat_abs b, by rwa [e1, e2], ne_of_nat_abs_ne_nat_abs_of_nonneg ha hb this /- 4. Aux lemmas for pushing nat_abs inside numerals nat_abs_zero and nat_abs_one are defined at init/data/int/basic.lean -/ lemma nat_abs_of_nat_core (n : β„•) : nat_abs (of_nat n) = n := rfl lemma nat_abs_of_neg_succ_of_nat (n : β„•) : nat_abs (neg_succ_of_nat n) = nat.succ n := rfl protected lemma nat_abs_add_nonneg : βˆ€ {a b : int}, a β‰₯ 0 β†’ b β‰₯ 0 β†’ nat_abs (a + b) = nat_abs a + nat_abs b | (of_nat n) (of_nat m) h₁ hβ‚‚ := have of_nat n + of_nat m = of_nat (n + m), from rfl, by simp [nat_abs_of_nat_core, this] | _ (neg_succ_of_nat m) h₁ hβ‚‚ := absurd (neg_succ_of_nat_lt_zero m) (not_lt_of_ge hβ‚‚) | (neg_succ_of_nat n) _ h₁ hβ‚‚ := absurd (neg_succ_of_nat_lt_zero n) (not_lt_of_ge h₁) protected lemma nat_abs_add_neg : βˆ€ {a b : int}, a < 0 β†’ b < 0 β†’ nat_abs (a + b) = nat_abs a + nat_abs b | (neg_succ_of_nat n) (neg_succ_of_nat m) h₁ hβ‚‚ := have -[1+ n] + -[1+ m] = -[1+ nat.succ (n + m)], from rfl, begin simp [nat_abs_of_neg_succ_of_nat, this, nat.succ_add, nat.add_succ] end protected lemma nat_abs_bit0 : βˆ€ (a : int), nat_abs (bit0 a) = bit0 (nat_abs a) | (of_nat n) := int.nat_abs_add_nonneg (of_nat_ge_zero n) (of_nat_ge_zero n) | (neg_succ_of_nat n) := int.nat_abs_add_neg (neg_succ_of_nat_lt_zero n) (neg_succ_of_nat_lt_zero n) protected lemma nat_abs_bit0_step {a : int} {n : nat} (h : nat_abs a = n) : nat_abs (bit0 a) = bit0 n := begin rw [-h], apply int.nat_abs_bit0 end protected lemma nat_abs_bit1_nonneg {a : int} (h : a β‰₯ 0) : nat_abs (bit1 a) = bit1 (nat_abs a) := show nat_abs (bit0 a + 1) = bit0 (nat_abs a) + nat_abs 1, from by rw [int.nat_abs_add_nonneg (int.bit0_nonneg h) (le_of_lt (zero_lt_one)), int.nat_abs_bit0] protected lemma nat_abs_bit1_nonneg_step {a : int} {n : nat} (h₁ : a β‰₯ 0) (hβ‚‚ : nat_abs a = n) : nat_abs (bit1 a) = bit1 n := begin rw [-hβ‚‚], apply int.nat_abs_bit1_nonneg h₁ end end int
04836b225f50729af95f1e3619553ed469f44880
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Init/Notation.lean
e8769744f3111672c428df9e0ba3151c94053bce
[ "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
20,889
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 Notation for operators defined at Prelude.lean -/ prelude import Init.Prelude -- DSL for specifying parser precedences and priorities namespace Lean.Parser.Syntax syntax:65 (name := addPrec) prec " + " prec:66 : prec syntax:65 (name := subPrec) prec " - " prec:66 : prec syntax:65 (name := addPrio) prio " + " prio:66 : prio syntax:65 (name := subPrio) prio " - " prio:66 : prio end Lean.Parser.Syntax macro "max" : prec => `(1024) -- maximum precedence used in term parsers, in particular for terms in function position (`ident`, `paren`, ...) macro "arg" : prec => `(1023) -- precedence used for application arguments (`do`, `by`, ...) macro "lead" : prec => `(1022) -- precedence used for terms not supposed to be used as arguments (`let`, `have`, ...) macro "(" p:prec ")" : prec => p macro "min" : prec => `(10) -- minimum precedence used in term parsers macro "min1" : prec => `(11) -- `(min+1) we can only `min+1` after `Meta.lean` /- `max:prec` as a term. It is equivalent to `eval_prec max` for `eval_prec` defined at `Meta.lean`. We use `max_prec` to workaround bootstrapping issues. -/ macro "max_prec" : term => `(1024) macro "default" : prio => `(1000) macro "low" : prio => `(100) macro "mid" : prio => `(1000) macro "high" : prio => `(10000) macro "(" p:prio ")" : prio => p -- Basic notation for defining parsers -- NOTE: precedence must be at least `arg` to be used in `macro` without parentheses syntax:arg stx:max "+" : stx syntax:arg stx:max "*" : stx syntax:arg stx:max "?" : stx syntax:2 stx:2 " <|> " stx:1 : stx macro_rules | `(stx| $p +) => `(stx| many1($p)) | `(stx| $p *) => `(stx| many($p)) | `(stx| $p ?) => `(stx| optional($p)) | `(stx| $p₁ <|> $pβ‚‚) => `(stx| orelse($p₁, $pβ‚‚)) /- Comma-separated sequence. -/ macro:arg x:stx:max ",*" : stx => `(stx| sepBy($x, ",", ", ")) macro:arg x:stx:max ",+" : stx => `(stx| sepBy1($x, ",", ", ")) /- Comma-separated sequence with optional trailing comma. -/ macro:arg x:stx:max ",*,?" : stx => `(stx| sepBy($x, ",", ", ", allowTrailingSep)) macro:arg x:stx:max ",+,?" : stx => `(stx| sepBy1($x, ",", ", ", allowTrailingSep)) macro:arg "!" x:stx:max : stx => `(stx| notFollowedBy($x)) syntax (name := rawNatLit) "nat_lit " num : term infixr:90 " ∘ " => Function.comp infixr:35 " Γ— " => Prod infixl:55 " ||| " => HOr.hOr infixl:58 " ^^^ " => HXor.hXor infixl:60 " &&& " => HAnd.hAnd infixl:65 " + " => HAdd.hAdd infixl:65 " - " => HSub.hSub infixl:70 " * " => HMul.hMul infixl:70 " / " => HDiv.hDiv infixl:70 " % " => HMod.hMod infixl:75 " <<< " => HShiftLeft.hShiftLeft infixl:75 " >>> " => HShiftRight.hShiftRight infixr:80 " ^ " => HPow.hPow infixl:65 " ++ " => HAppend.hAppend prefix:100 "-" => Neg.neg prefix:100 "~~~" => Complement.complement /- Remark: the infix commands above ensure a delaborator is generated for each relations. We redefine the macros below to be able to use the auxiliary `binop%` elaboration helper for binary operators. It addresses issue #382. -/ macro_rules | `($x ||| $y) => `(binop% HOr.hOr $x $y) macro_rules | `($x ^^^ $y) => `(binop% HXor.hXor $x $y) macro_rules | `($x &&& $y) => `(binop% HAnd.hAnd $x $y) macro_rules | `($x + $y) => `(binop% HAdd.hAdd $x $y) macro_rules | `($x - $y) => `(binop% HSub.hSub $x $y) macro_rules | `($x * $y) => `(binop% HMul.hMul $x $y) macro_rules | `($x / $y) => `(binop% HDiv.hDiv $x $y) macro_rules | `($x ++ $y) => `(binop% HAppend.hAppend $x $y) -- declare ASCII alternatives first so that the latter Unicode unexpander wins infix:50 " <= " => LE.le infix:50 " ≀ " => LE.le infix:50 " < " => LT.lt infix:50 " >= " => GE.ge infix:50 " β‰₯ " => GE.ge infix:50 " > " => GT.gt infix:50 " = " => Eq infix:50 " == " => BEq.beq /- Remark: the infix commands above ensure a delaborator is generated for each relations. We redefine the macros below to be able to use the auxiliary `binrel%` elaboration helper for binary relations. It has better support for applying coercions. For example, suppose we have `binrel% Eq n i` where `n : Nat` and `i : Int`. The default elaborator fails because we don't have a coercion from `Int` to `Nat`, but `binrel%` succeeds because it also tries a coercion from `Nat` to `Int` even when the nat occurs before the int. -/ macro_rules | `($x <= $y) => `(binrel% LE.le $x $y) macro_rules | `($x ≀ $y) => `(binrel% LE.le $x $y) macro_rules | `($x < $y) => `(binrel% LT.lt $x $y) macro_rules | `($x > $y) => `(binrel% GT.gt $x $y) macro_rules | `($x >= $y) => `(binrel% GE.ge $x $y) macro_rules | `($x β‰₯ $y) => `(binrel% GE.ge $x $y) macro_rules | `($x = $y) => `(binrel% Eq $x $y) macro_rules | `($x == $y) => `(binrel_no_prop% BEq.beq $x $y) infixr:35 " /\\ " => And infixr:35 " ∧ " => And infixr:30 " \\/ " => Or infixr:30 " ∨ " => Or notation:max "Β¬" p:40 => Not p infixl:35 " && " => and infixl:30 " || " => or notation:max "!" b:40 => not b infixr:67 " :: " => List.cons syntax:20 term:21 " <|> " term:20 : term syntax:60 term:61 " >> " term:60 : term infixl:55 " >>= " => Bind.bind notation:60 a:60 " <*> " b:61 => Seq.seq a fun _ : Unit => b notation:60 a:60 " <* " b:61 => SeqLeft.seqLeft a fun _ : Unit => b notation:60 a:60 " *> " b:61 => SeqRight.seqRight a fun _ : Unit => b infixr:100 " <$> " => Functor.map macro_rules | `($x <|> $y) => `(binop_lazy% HOrElse.hOrElse $x $y) macro_rules | `($x >> $y) => `(binop_lazy% HAndThen.hAndThen $x $y) syntax (name := termDepIfThenElse) ppRealGroup(ppRealFill(ppIndent("if " ident " : " term " then") ppSpace term) ppDedent(ppSpace) ppRealFill("else " term)) : term macro_rules | `(if $h:ident : $c then $t:term else $e:term) => `(let_mvar% ?m := $c; wait_if_type_mvar% ?m; dite ?m (fun $h:ident => $t) (fun $h:ident => $e)) syntax (name := termIfThenElse) ppRealGroup(ppRealFill(ppIndent("if " term " then") ppSpace term) ppDedent(ppSpace) ppRealFill("else " term)) : term macro_rules | `(if $c then $t:term else $e:term) => `(let_mvar% ?m := $c; wait_if_type_mvar% ?m; ite ?m $t $e) macro "if " "let " pat:term " := " d:term " then " t:term " else " e:term : term => `(match $d:term with | $pat:term => $t | _ => $e) syntax:min term " <| " term:min : term macro_rules | `($f $args* <| $a) => let args := args.push a; `($f $args*) | `($f <| $a) => `($f $a) syntax:min term " |> " term:min1 : term macro_rules | `($a |> $f $args*) => let args := args.push a; `($f $args*) | `($a |> $f) => `($f $a) -- Haskell-like pipe <| -- Note that we have a whitespace after `$` to avoid an ambiguity with the antiquotations. syntax:min term atomic(" $" ws) term:min : term macro_rules | `($f $args* $ $a) => let args := args.push a; `($f $args*) | `($f $ $a) => `($f $a) syntax "{ " ident (" : " term)? " // " term " }" : term macro_rules | `({ $x : $type // $p }) => ``(Subtype (fun ($x:ident : $type) => $p)) | `({ $x // $p }) => ``(Subtype (fun ($x:ident : _) => $p)) /- `without_expected_type t` instructs Lean to elaborate `t` without an expected type. Recall that terms such as `match ... with ...` and `⟨...⟩` will postpone elaboration until expected type is known. So, `without_expected_type` is not effective in this case. -/ macro "without_expected_type " x:term : term => `(let aux := $x; aux) syntax "[" term,* "]" : term syntax "%[" term,* "|" term "]" : term -- auxiliary notation for creating big list literals namespace Lean macro_rules | `([ $elems,* ]) => do let rec expandListLit (i : Nat) (skip : Bool) (result : Syntax) : MacroM Syntax := do match i, skip with | 0, _ => pure result | i+1, true => expandListLit i false result | i+1, false => expandListLit i true (← ``(List.cons $(elems.elemsAndSeps[i]) $result)) if elems.elemsAndSeps.size < 64 then expandListLit elems.elemsAndSeps.size false (← ``(List.nil)) else `(%[ $elems,* | List.nil ]) notation:50 e:51 " matches " p:51 => match e with | p => true | _ => false -- Declare `this` as a keyword that unhygienically binds to a scope-less `this` assumption (or other binding). -- The keyword prevents declaring a `this` binding except through metapgrogramming, as is done by `have`/`show`. /-- Special identifier introduced by "anonymous" `have : ...`, `suffices p ...` etc. -/ macro tk:"this" : term => Syntax.ident tk.getHeadInfo "this".toSubstring `this [] namespace Parser.Tactic /-- Introduce one or more hypotheses, optionally naming and/or pattern-matching them. For each hypothesis to be introduced, the remaining main goal's target type must be a `let` or function type. * `intro` by itself introduces one anonymous hypothesis, which can be accessed by e.g. `assumption`. * `intro x y` introduces two hypotheses and names them. Individual hypotheses can be anonymized via `_`, or matched against a pattern: ```lean -- ... ⊒ Ξ± Γ— Ξ² β†’ ... intro (a, b) -- ..., a : Ξ±, b : Ξ² ⊒ ... ``` * Alternatively, `intro` can be combined with pattern matching much like `fun`: ```lean intro | n + 1, 0 => tac | ... ``` -/ syntax (name := intro) "intro " notFollowedBy("|") (colGt term:max)* : tactic /-- `intros x...` behaves like `intro x...`, but then keeps introducing (anonymous) hypotheses until goal is not of a function type. -/ syntax (name := intros) "intros " (colGt (ident <|> "_"))* : tactic /-- `rename t => x` renames the most recent hypothesis whose type matches `t` (which may contain placeholders) to `x`, or fails if no such hypothesis could be found. -/ syntax (name := rename) "rename " term " => " ident : tactic /-- `revert x...` is the inverse of `intro x...`: it moves the given hypotheses into the main goal's target type. -/ syntax (name := revert) "revert " (colGt term:max)+ : tactic /-- `clear x...` removes the given hypotheses, or fails if there are remaining references to a hypothesis. -/ syntax (name := clear) "clear " (colGt term:max)+ : tactic /-- `subst x...` substitutes each `x` with `e` in the goal if there is a hypothesis of type `x = e` or `e = x`. If `x` is itself a hypothesis of type `y = e` or `e = y`, `y` is substituted instead. -/ syntax (name := subst) "subst " (colGt term:max)+ : tactic /-- `assumption` tries to solve the main goal using a hypothesis of compatible type, or else fails. Note also the `β€Ήtβ€Ί` term notation, which is a shorthand for `show t by assumption`. -/ syntax (name := assumption) "assumption" : tactic /-- `contradiction` closes the main goal if its hypotheses are "trivially contradictory". ```lean example (h : False) : p := by contradiction -- inductive type/family with no applicable constructors example (h : none = some true) : p := by contradiction -- injectivity of constructors example (h : 2 + 2 = 3) : p := by contradiction -- decidable false proposition example (h : p) (h' : Β¬ p) : q := by contradiction example (x : Nat) (h : x β‰  x) : p := by contradiction ``` -/ syntax (name := contradiction) "contradiction" : tactic /-- `apply e` tries to match the current goal against the conclusion of `e`'s type. If it succeeds, then the tactic returns as many subgoals as the number of premises that have not been fixed by type inference or type class resolution. Non-dependent premises are added before dependent ones. The `apply` tactic uses higher-order pattern matching, type class resolution, and first-order unification with dependent types. -/ syntax (name := apply) "apply " term : tactic /-- `exact e` closes the main goal if its target type matches that of `e`. -/ syntax (name := exact) "exact " term : tactic /-- `refine e` behaves like `exact e`, except that named (`?x`) or unnamed (`?_`) holes in `e` that are not solved by unification with the main goal's target type are converted into new goals, using the hole's name, if any, as the goal case name. -/ syntax (name := refine) "refine " term : tactic /-- `refine' e` behaves like `refine e`, except that unsolved placeholders (`_`) and implicit parameters are also converted into new goals. -/ syntax (name := refine') "refine' " term : tactic /-- If the main goal's target type is an inductive type, `constructor` solves it with the first matching constructor, or else fails. -/ syntax (name := constructor) "constructor" : tactic /-- `case tag => tac` focuses on the goal with case name `tag` and solves it using `tac`, or else fails. `case tag x₁ ... xβ‚™ => tac` additionally renames the `n` most recent hypotheses with inaccessible names to the given names. -/ syntax (name := case) "case " (ident <|> "_") (ident <|> "_")* " => " tacticSeq : tactic /-- `next => tac` focuses on the next goal solves it using `tac`, or else fails. `next x₁ ... xβ‚™ => tac` additionally renames the `n` most recent hypotheses with inaccessible names to the given names. -/ macro "next " args:(ident <|> "_")* " => " tac:tacticSeq : tactic => `(tactic| case _ $(args.getArgs)* => $tac) /-- `allGoals tac` runs `tac` on each goal, concatenating the resulting goals, if any. -/ syntax (name := allGoals) "all_goals " tacticSeq : tactic /-- `anyGoals tac` applies the tactic `tac` to every goal, and succeeds if at least one application succeeds. -/ syntax (name := anyGoals) "any_goals " tacticSeq : tactic /-- `focus tac` focuses on the main goal, suppressing all other goals, and runs `tac` on it. Usually `Β· tac`, which enforces that the goal is closed by `tac`, should be preferred. -/ syntax (name := focus) "focus " tacticSeq : tactic /-- `skip` does nothing. -/ syntax (name := skip) "skip" : tactic /-- `done` succeeds iff there are no remaining goals. -/ syntax (name := done) "done" : tactic syntax (name := traceState) "trace_state" : tactic syntax (name := failIfSuccess) "fail_if_success " tacticSeq : tactic syntax (name := paren) "(" tacticSeq ")" : tactic syntax (name := withReducible) "with_reducible " tacticSeq : tactic syntax (name := withReducibleAndInstances) "with_reducible_and_instances " tacticSeq : tactic /-- `first | tac | ...` runs each `tac` until one succeeds, or else fails. -/ syntax (name := first) "first " withPosition((group(colGe "|" tacticSeq))+) : tactic syntax (name := rotateLeft) "rotate_left" (num)? : tactic syntax (name := rotateRight) "rotate_right" (num)? : tactic /-- `try tac` runs `tac` and succeeds even if `tac` failed. -/ macro "try " t:tacticSeq : tactic => `(first | $t | skip) /-- `tac <;> tac'` runs `tac` on the main goal and `tac'` on each produced goal, concatenating all goals produced by `tac'`. -/ macro:1 x:tactic " <;> " y:tactic:0 : tactic => `(tactic| focus ($x:tactic; all_goals $y:tactic)) /-- `Β· tac` focuses on the main goal and tries to solve it using `tac`, or else fails. -/ macro dot:("Β·" <|> ".") ts:tacticSeq : tactic => `(tactic| {%$dot ($ts:tacticSeq) }) /-- `rfl` is a shorthand for `exact rfl`. -/ macro "rfl" : tactic => `(exact rfl) /-- `admit` is a shorthand for `exact sorry`. -/ macro "admit" : tactic => `(exact sorry) /-- The `sorry` tactic is a shorthand for `exact sorry`. -/ macro "sorry" : tactic => `(exact sorry) macro "infer_instance" : tactic => `(exact inferInstance) /-- Optional configuration option for tactics -/ syntax config := atomic("(" &"config") " := " term ")" syntax locationWildcard := "*" syntax locationHyp := (colGt term:max)+ ("⊒" <|> "|-")? syntax location := withPosition(" at " (locationWildcard <|> locationHyp)) syntax (name := change) "change " term (location)? : tactic syntax (name := changeWith) "change " term " with " term (location)? : tactic syntax rwRule := ("← " <|> "<- ")? term syntax rwRuleSeq := "[" rwRule,+,? "]" syntax (name := rewriteSeq) "rewrite " (config)? rwRuleSeq (location)? : tactic syntax (name := rwSeq) "rw " (config)? rwRuleSeq (location)? : tactic def rwWithRfl (kind : SyntaxNodeKind) (atom : String) (stx : Syntax) : MacroM Syntax := do -- We show the `rfl` state on `]` let seq := stx[2] let rbrak := seq[2] -- Replace `]` token with one without position information in the expanded tactic let seq := seq.setArg 2 (mkAtom "]") let tac := stx.setKind kind |>.setArg 0 (mkAtomFrom stx atom) |>.setArg 2 seq `(tactic| $tac; try (with_reducible rfl%$rbrak)) @[macro rwSeq] def expandRwSeq : Macro := rwWithRfl ``Lean.Parser.Tactic.rewriteSeq "rewrite" syntax (name := injection) "injection " term (" with " (colGt (ident <|> "_"))+)? : tactic syntax (name := injections) "injections" : tactic syntax discharger := atomic("(" (&"discharger" <|> &"disch")) " := " tacticSeq ")" syntax simpPre := "↓" syntax simpPost := "↑" syntax simpLemma := (simpPre <|> simpPost)? ("← " <|> "<- ")? term syntax simpErase := "-" term:max syntax simpStar := "*" syntax (name := simp) "simp " (config)? (discharger)? (&"only ")? ("[" (simpStar <|> simpErase <|> simpLemma),* "]")? (location)? : tactic syntax (name := simpAll) "simp_all " (config)? (discharger)? (&"only ")? ("[" (simpErase <|> simpLemma),* "]")? : tactic /-- Delta expand the given definition. This is a low-level tactic, it will expose how recursive definitions have been compiled by Lean. -/ syntax (name := delta) "delta " ident (location)? : tactic -- Auxiliary macro for lifting have/suffices/let/... -- It makes sure the "continuation" `?_` is the main goal after refining macro "refine_lift " e:term : tactic => `(focus (refine no_implicit_lambda% $e; rotate_right)) macro "have " d:haveDecl : tactic => `(refine_lift have $d:haveDecl; ?_) /- We use a priority > default, to avoid ambiguity with previous `have` notation -/ macro (priority := high) "have" x:ident " := " p:term : tactic => `(have $x:ident : _ := $p) macro "suffices " d:sufficesDecl : tactic => `(refine_lift suffices $d:sufficesDecl; ?_) macro "let " d:letDecl : tactic => `(refine_lift let $d:letDecl; ?_) macro "show " e:term : tactic => `(refine_lift show $e:term from ?_) syntax (name := letrec) withPosition(atomic(group("let " &"rec ")) letRecDecls) : tactic macro_rules | `(tactic| let rec $d:letRecDecls) => `(tactic| refine_lift let rec $d:letRecDecls; ?_) -- Similar to `refineLift`, but using `refine'` macro "refine_lift' " e:term : tactic => `(focus (refine' no_implicit_lambda% $e; rotate_right)) macro "have' " d:haveDecl : tactic => `(refine_lift' have $d:haveDecl; ?_) macro (priority := high) "have'" x:ident " := " p:term : tactic => `(have' $x:ident : _ := $p) macro "let' " d:letDecl : tactic => `(refine_lift' let $d:letDecl; ?_) syntax inductionAlt := ppDedent(ppLine) "| " (group("@"? ident) <|> "_") (ident <|> "_")* " => " (hole <|> syntheticHole <|> tacticSeq) syntax inductionAlts := "with " (tactic)? withPosition( (colGe inductionAlt)+) syntax (name := induction) "induction " term,+ (" using " ident)? ("generalizing " term:max+)? (inductionAlts)? : tactic syntax generalizeArg := atomic(ident " : ")? term:51 " = " ident /-- `generalize ([h :] e = x),+` replaces all occurrences `e`s in the main goal with a fresh hypothesis `x`s. If `h` is given, `h : e = x` is introduced as well. -/ syntax (name := generalize) "generalize " generalizeArg,+ : tactic syntax casesTarget := atomic(ident " : ")? term syntax (name := cases) "cases " casesTarget,+ (" using " ident)? (inductionAlts)? : tactic syntax (name := existsIntro) "exists " term : tactic /-- `rename_i x_1 ... x_n` renames the last `n` inaccessible names using the given names. -/ syntax (name := renameI) "rename_i " (colGt (ident <|> "_"))+ : tactic syntax "repeat " tacticSeq : tactic macro_rules | `(tactic| repeat $seq) => `(tactic| first | ($seq); repeat $seq | skip) syntax "trivial" : tactic syntax (name := split) "split " (colGt term)? (location)? : tactic /-- The tactic `specialize h a₁ ... aβ‚™` works on local hypothesis `h`. The premises of this hypothesis, either universal quantifications or non-dependent implications, are instantiated by concrete terms coming either from arguments `a₁` ... `aβ‚™`. The tactic adds a new hypothesis with the same name `h := h a₁ ... aβ‚™` and tries to clear the previous one. -/ syntax (name := specialize) "specialize " term : tactic macro_rules | `(tactic| trivial) => `(tactic| assumption) macro_rules | `(tactic| trivial) => `(tactic| rfl) macro_rules | `(tactic| trivial) => `(tactic| contradiction) macro_rules | `(tactic| trivial) => `(tactic| apply True.intro) macro_rules | `(tactic| trivial) => `(tactic| apply And.intro <;> trivial) macro "unhygienic " t:tacticSeq : tactic => `(set_option tactic.hygienic false in $t:tacticSeq) end Tactic namespace Attr -- simp attribute syntax syntax (name := simp) "simp" (Tactic.simpPre <|> Tactic.simpPost)? (prio)? : attr end Attr end Parser end Lean macro "β€Ή" type:term "β€Ί" : term => `((by assumption : $type))
b5b28626779fb2d9611cccda6c4ac73478bdc551
2102833783742d9cd20d98b382ecb3af427d8071
/library/init/meta/default.lean
0a137e8ede032f834ad195f256cf2d2649822c25
[ "Apache-2.0" ]
permissive
bryangingechen/lean
b63dd2b7d2ed46c13c6226148691b7d88282e9b2
ec76117504723667bdc0dda4b93aa1b31536147d
refs/heads/master
1,592,587,806,769
1,568,328,715,000
1,568,328,715,000
196,801,202
0
0
Apache-2.0
1,563,085,022,000
1,563,085,022,000
null
UTF-8
Lean
false
false
938
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.meta.name init.meta.options init.meta.format init.meta.rb_map import init.meta.level init.meta.expr init.meta.environment init.meta.attribute import init.meta.tactic init.meta.contradiction_tactic init.meta.constructor_tactic import init.meta.injection_tactic init.meta.relation_tactics init.meta.fun_info import init.meta.congr_lemma init.meta.match_tactic init.meta.ac_tactics import init.meta.backward init.meta.rewrite_tactic import init.meta.derive init.meta.mk_dec_eq_instance import init.meta.simp_tactic init.meta.set_get_option_tactics import init.meta.interactive init.meta.converter init.meta.vm import init.meta.comp_value_tactics init.meta.smt import init.meta.async_tactic init.meta.ref import init.meta.hole_command init.meta.congr_tactic
7432e3bfe421ef01bb6184b6ad810996d6915efa
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/data/string/default.lean
e76c416d355cb4a5608915eafaded6d8dd5b7f78
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
223
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 init.data.string.ops
7f71750806879fcb05e638aae7ecb5c0da836d66
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/elab_bug1.lean
edca3f166a7a5e4cfb507dd882bfdcb9dc3f8984
[ "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
2,794
lean
---------------------------------------------------------------------------------------------------- --- Copyright (c) 2014 Microsoft Corporation. All rights reserved. --- Released under Apache 2.0 license as described in the file LICENSE. --- Author: Jeremy Avigad ---------------------------------------------------------------------------------------------------- import logic open eq open function namespace congruence -- TODO: move this somewhere else definition reflexive {T : Type} (R : T β†’ T β†’ Prop) : Prop := βˆ€x, R x x -- Congruence classes for unary and binary functions -- ------------------------------------------------- inductive congruence [class] {T1 : Type} {T2 : Type} (R1 : T1 β†’ T1 β†’ Prop) (R2 : T2 β†’ T2 β†’ Prop) (f : T1 β†’ T2) : Prop := mk : (βˆ€x y : T1, R1 x y β†’ R2 (f x) (f y)) β†’ congruence R1 R2 f -- to trigger class inference theorem congr_app {T1 : Type} {T2 : Type} (R1 : T1 β†’ T1 β†’ Prop) (R2 : T2 β†’ T2 β†’ Prop) (f : T1 β†’ T2) {C : congruence R1 R2 f} {x y : T1} : R1 x y β†’ R2 (f x) (f y) := congruence.rec id C x y -- General tools to build instances -- -------------------------------- theorem congr_trivial [instance] {T : Type} (R : T β†’ T β†’ Prop) : congruence R R id := congruence.mk (take x y H, H) theorem congr_const {T2 : Type} (R2 : T2 β†’ T2 β†’ Prop) (H : reflexive R2) : βˆ€(T1 : Type) (R1 : T1 β†’ T1 β†’ Prop) (c : T2), congruence R1 R2 (const T1 c) := take T1 R1 c, congruence.mk (take x y H1, H c) -- congruences for logic theorem congr_const_iff [instance] (T1 : Type) (R1 : T1 β†’ T1 β†’ Prop) (c : Prop) : congruence R1 iff (const T1 c) := congr_const iff iff.refl T1 R1 c theorem congr_or [instance] (T : Type) (R : T β†’ T β†’ Prop) (f1 f2 : T β†’ Prop) [H1 : congruence R iff f1] [H2 : congruence R iff f2] : congruence R iff (Ξ»x, f1 x ∨ f2 x) := sorry theorem congr_implies [instance] (T : Type) (R : T β†’ T β†’ Prop) (f1 f2 : T β†’ Prop) [H1 : congruence R iff f1] [H2 : congruence R iff f2] : congruence R iff (Ξ»x, f1 x β†’ f2 x) := sorry theorem congr_iff [instance] (T : Type) (R : T β†’ T β†’ Prop) (f1 f2 : T β†’ Prop) [H1 : congruence R iff f1] [H2 : congruence R iff f2] : congruence R iff (Ξ»x, f1 x ↔ f2 x) := sorry theorem congr_not [instance] (T : Type) (R : T β†’ T β†’ Prop) (f : T β†’ Prop) [H : congruence R iff f] : congruence R iff (Ξ»x, Β¬ f x) := sorry theorem subst_iff {T : Type} {R : T β†’ T β†’ Prop} {P : T β†’ Prop} [C : congruence R iff P] {a b : T} (H : R a b) (H1 : P a) : P b := -- iff_mp_left (congruence.rec id C a b H) H1 iff.elim_left (@congr_app _ _ R iff P C a b H) H1 theorem test2 (a b c d e : Prop) (H1 : a ↔ b) (H2 : a ∨ c β†’ Β¬(d β†’ a)) : b ∨ c β†’ Β¬(d β†’ b) := subst_iff H1 H2 end congruence
0e181b56a5d59f419c0cb5bf7614e07a642c2ee6
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/valid/mathd-algebra-104.lean
bee73800d3607f7a368734bbf2523201938efc66
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
251
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import data.real.basic example (x : ℝ) (hβ‚€ : 125 / 8 = x / 12) : x = 375 / 2 := begin linarith, end
02ec28daa2d878059bc36afc49bd854670950977
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/lie/base_change.lean
5042d94bf98c67ebc702f1d731f0b6f5d9457969
[ "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
5,860
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.tensor_product import ring_theory.tensor_product /-! # Extension and restriction of scalars for Lie algebras Lie algebras have a well-behaved theory of extension and restriction of scalars. ## Main definitions * `lie_algebra.extend_scalars.lie_algebra` * `lie_algebra.restrict_scalars.lie_algebra` ## Tags lie ring, lie algebra, extension of scalars, restriction of scalars, base change -/ universes u v w w₁ wβ‚‚ w₃ open_locale tensor_product variables (R : Type u) (A : Type w) (L : Type v) namespace lie_algebra namespace extend_scalars variables [comm_ring R] [comm_ring A] [algebra R A] [lie_ring L] [lie_algebra R L] /-- The Lie bracket on the extension of a Lie algebra `L` over `R` by an algebra `A` over `R`. In fact this bracket is fully `A`-bilinear but without a significant upgrade to our mixed-scalar support in the tensor product library, it is far easier to bootstrap like this, starting with the definition below. -/ private def bracket' : (A βŠ—[R] L) β†’β‚—[R] (A βŠ—[R] L) β†’β‚—[R] A βŠ—[R] L := tensor_product.curry $ (tensor_product.map (algebra.lmul' R) (lie_module.to_module_hom R L L : L βŠ—[R] L β†’β‚—[R] L)).comp $ tensor_product.tensor_tensor_tensor_comm R A L A L @[simp] private lemma bracket'_tmul (s t : A) (x y : L) : bracket' R A L (s βŠ—β‚œ[R] x) (t βŠ—β‚œ[R] y) = (s*t) βŠ—β‚œ ⁅x, y⁆ := by simp [bracket'] instance : has_bracket (A βŠ—[R] L) (A βŠ—[R] L) := { bracket := Ξ» x y, bracket' R A L x y, } private lemma bracket_def (x y : A βŠ—[R] L) : ⁅x, y⁆ = bracket' R A L x y := rfl @[simp] lemma bracket_tmul (s t : A) (x y : L) : ⁅s βŠ—β‚œ[R] x, t βŠ—β‚œ[R] y⁆ = (s*t) βŠ—β‚œ ⁅x, y⁆ := by rw [bracket_def, bracket'_tmul] private lemma bracket_lie_self (x : A βŠ—[R] L) : ⁅x, x⁆ = 0 := begin simp only [bracket_def], apply x.induction_on, { simp only [linear_map.map_zero, eq_self_iff_true, linear_map.zero_apply], }, { intros a l, simp only [bracket'_tmul, tensor_product.tmul_zero, eq_self_iff_true, lie_self], }, { intros z₁ zβ‚‚ h₁ hβ‚‚, suffices : bracket' R A L z₁ zβ‚‚ + bracket' R A L zβ‚‚ z₁ = 0, { rw [linear_map.map_add, linear_map.map_add, linear_map.add_apply, linear_map.add_apply, h₁, hβ‚‚, zero_add, add_zero, add_comm, this], }, apply z₁.induction_on, { simp only [linear_map.map_zero, add_zero, linear_map.zero_apply], }, { intros a₁ l₁, apply zβ‚‚.induction_on, { simp only [linear_map.map_zero, add_zero, linear_map.zero_apply], }, { intros aβ‚‚ lβ‚‚, simp only [← lie_skew lβ‚‚ l₁, mul_comm a₁ aβ‚‚, tensor_product.tmul_neg, bracket'_tmul, add_right_neg], }, { intros y₁ yβ‚‚ hy₁ hyβ‚‚, simp only [hy₁, hyβ‚‚, add_add_add_comm, add_zero, linear_map.add_apply, linear_map.map_add], }, }, { intros y₁ yβ‚‚ hy₁ hyβ‚‚, simp only [add_add_add_comm, hy₁, hyβ‚‚, add_zero, linear_map.add_apply, linear_map.map_add], }, }, end private lemma bracket_leibniz_lie (x y z : A βŠ—[R] L) : ⁅x, ⁅y, z⁆⁆ = ⁅⁅x, y⁆, z⁆ + ⁅y, ⁅x, z⁆⁆ := begin simp only [bracket_def], apply x.induction_on, { simp only [linear_map.map_zero, add_zero, eq_self_iff_true, linear_map.zero_apply], }, { intros a₁ l₁, apply y.induction_on, { simp only [linear_map.map_zero, add_zero, eq_self_iff_true, linear_map.zero_apply], }, { intros aβ‚‚ lβ‚‚, apply z.induction_on, { simp only [linear_map.map_zero, add_zero], }, { intros a₃ l₃, simp only [bracket'_tmul], rw [mul_left_comm aβ‚‚ a₁ a₃, mul_assoc, leibniz_lie, tensor_product.tmul_add], }, { intros u₁ uβ‚‚ h₁ hβ‚‚, simp only [add_add_add_comm, h₁, hβ‚‚, linear_map.map_add], }, }, { intros u₁ uβ‚‚ h₁ hβ‚‚, simp only [add_add_add_comm, h₁, hβ‚‚, linear_map.add_apply, linear_map.map_add], }, }, { intros u₁ uβ‚‚ h₁ hβ‚‚, simp only [add_add_add_comm, h₁, hβ‚‚, linear_map.add_apply, linear_map.map_add], }, end instance : lie_ring (A βŠ—[R] L) := { add_lie := Ξ» x y z, by simp only [bracket_def, linear_map.add_apply, linear_map.map_add], lie_add := Ξ» x y z, by simp only [bracket_def, linear_map.map_add], lie_self := bracket_lie_self R A L, leibniz_lie := bracket_leibniz_lie R A L, } private lemma bracket_lie_smul (a : A) (x y : A βŠ—[R] L) : ⁅x, a β€’ y⁆ = a β€’ ⁅x, y⁆ := begin apply x.induction_on, { simp only [zero_lie, smul_zero], }, { intros a₁ l₁, apply y.induction_on, { simp only [lie_zero, smul_zero], }, { intros aβ‚‚ lβ‚‚, simp only [bracket_def, bracket', tensor_product.smul_tmul', mul_left_comm a₁ a aβ‚‚, tensor_product.curry_apply, algebra.lmul'_apply, algebra.id.smul_eq_mul, function.comp_app, linear_equiv.coe_coe, linear_map.coe_comp, tensor_product.map_tmul, tensor_product.tensor_tensor_tensor_comm_tmul], }, { intros z₁ zβ‚‚ h₁ hβ‚‚, simp only [h₁, hβ‚‚, smul_add, lie_add], }, }, { intros z₁ zβ‚‚ h₁ hβ‚‚, simp only [h₁, hβ‚‚, smul_add, add_lie], }, end instance lie_algebra : lie_algebra A (A βŠ—[R] L) := { lie_smul := bracket_lie_smul R A L, } end extend_scalars namespace restrict_scalars open restrict_scalars variables [h : lie_ring L] include h instance : lie_ring (restrict_scalars R A L) := h variables [comm_ring A] [lie_algebra A L] @[nolint unused_arguments] instance lie_algebra [comm_ring R] [algebra R A] : lie_algebra R (restrict_scalars R A L) := { lie_smul := Ξ» t x y, (lie_smul _ (show L, from x) (show L, from y) : _), .. (by apply_instance : module R (restrict_scalars R A L)), } end restrict_scalars end lie_algebra
fda5c51377c650e1fc7cd90f1e1882605bc21e83
82e44445c70db0f03e30d7be725775f122d72f3e
/src/measure_theory/pi_system.lean
262d81663811c72ff646c1f96b2f54034a50c77c
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
17,862
lean
/- Copyright (c) 2021 Martin Zinkevich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Martin Zinkevich -/ import measure_theory.measurable_space_def import data.equiv.encodable.lattice /-! # Induction principles for measurable sets, related to Ο€-systems and Ξ»-systems. ## 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. * The proof of this theorem relies on the notion of `is_pi_system`, i.e., a collection of sets which is closed under binary non-empty intersections. Note that this is a small variation around the usual notion in the literature, which often requires that a Ο€-system is non-empty, and closed also under disjoint intersections. This variation turns out to be convenient for the formalization. * The proof of Dynkin's Ο€-Ξ» theorem also requires the notion of `dynkin_system`, i.e., a collection of sets which contains the empty set, is closed under complementation and under countable union of pairwise disjoint sets. The disjointness condition is the only difference with `Οƒ`-algebras. * `generate_pi_system g` gives the minimal Ο€-system containing `g`. This can be considered a Galois insertion into both measurable spaces and sets. * `generate_from_generate_pi_system_eq` proves that if you start from a collection of sets `g`, take the generated Ο€-system, and then the generated Οƒ-algebra, you get the same result as the Οƒ-algebra generated from `g`. This is useful because there are connections between independent sets that are Ο€-systems and the generated independent spaces. * `mem_generate_pi_system_Union_elim` and `mem_generate_pi_system_Union_elim'` show that any element of the Ο€-system generated from the union of a set of Ο€-systems can be represented as the intersection of a finite number of elements from these sets. ## Implementation details * `is_pi_system` is a predicate, not a type. Thus, we don't explicitly define the galois insertion, nor do we define a complete lattice. In theory, we could define a complete lattice and galois insertion on the subtype corresponding to `is_pi_system`. -/ open measurable_space set open_locale classical /-- A Ο€-system is a collection of subsets of `Ξ±` that is closed under binary intersection of non-disjoint sets. Usually it is also required that the collection is nonempty, but we don't do that here. -/ def is_pi_system {Ξ±} (C : set (set Ξ±)) : Prop := βˆ€ s t ∈ C, (s ∩ t : set Ξ±).nonempty β†’ s ∩ t ∈ C namespace measurable_space lemma is_pi_system_measurable_set {Ξ±:Type*} [measurable_space Ξ±] : is_pi_system {s : set Ξ± | measurable_set s} := Ξ» s t hs ht _, hs.inter ht end measurable_space lemma is_pi_system.singleton {Ξ±} (S : set Ξ±) : is_pi_system ({S} : set (set Ξ±)) := begin intros s t h_s h_t h_ne, rw [set.mem_singleton_iff.1 h_s, set.mem_singleton_iff.1 h_t, set.inter_self, set.mem_singleton_iff], end /-- Given a collection `S` of subsets of `Ξ±`, then `generate_pi_system S` is the smallest Ο€-system containing `S`. -/ inductive generate_pi_system {Ξ±} (S : set (set Ξ±)) : set (set Ξ±) | base {s : set Ξ±} (h_s : s ∈ S) : generate_pi_system s | inter {s t : set Ξ±} (h_s : generate_pi_system s) (h_t : generate_pi_system t) (h_nonempty : (s ∩ t).nonempty) : generate_pi_system (s ∩ t) lemma is_pi_system_generate_pi_system {Ξ±} (S : set (set Ξ±)) : is_pi_system (generate_pi_system S) := Ξ» s t h_s h_t h_nonempty, generate_pi_system.inter h_s h_t h_nonempty lemma subset_generate_pi_system_self {Ξ±} (S : set (set Ξ±)) : S βŠ† generate_pi_system S := Ξ» s, generate_pi_system.base lemma generate_pi_system_subset_self {Ξ±} {S : set (set Ξ±)} (h_S : is_pi_system S) : generate_pi_system S βŠ† S := begin intros x h, induction h with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u, { exact h_s, }, { exact h_S _ _ h_s h_u h_nonempty, }, end lemma generate_pi_system_eq {Ξ±} {S : set (set Ξ±)} (h_pi : is_pi_system S) : generate_pi_system S = S := set.subset.antisymm (generate_pi_system_subset_self h_pi) (subset_generate_pi_system_self S) lemma generate_pi_system_mono {Ξ±} {S T : set (set Ξ±)} (hST : S βŠ† T) : generate_pi_system S βŠ† generate_pi_system T := begin intros t ht, induction ht with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u, { exact generate_pi_system.base (set.mem_of_subset_of_mem hST h_s),}, { exact is_pi_system_generate_pi_system T _ _ h_s h_u h_nonempty, }, end lemma generate_pi_system_measurable_set {Ξ±} [M : measurable_space Ξ±] {S : set (set Ξ±)} (h_meas_S : βˆ€ s ∈ S, measurable_set s) (t : set Ξ±) (h_in_pi : t ∈ generate_pi_system S) : measurable_set t := begin induction h_in_pi with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u, { apply h_meas_S _ h_s, }, { apply measurable_set.inter h_s h_u, }, end lemma generate_from_measurable_set_of_generate_pi_system {Ξ±} {g : set (set Ξ±)} (t : set Ξ±) (ht : t ∈ generate_pi_system g) : (generate_from g).measurable_set' t := @generate_pi_system_measurable_set Ξ± (generate_from g) g (Ξ» s h_s_in_g, measurable_set_generate_from h_s_in_g) t ht lemma generate_from_generate_pi_system_eq {Ξ±} {g : set (set Ξ±)} : generate_from (generate_pi_system g) = generate_from g := begin apply le_antisymm; apply generate_from_le, { exact Ξ» t h_t, generate_from_measurable_set_of_generate_pi_system t h_t, }, { exact Ξ» t h_t, measurable_set_generate_from (generate_pi_system.base h_t), }, end /- Every element of the Ο€-system generated by the union of a family of Ο€-systems is a finite intersection of elements from the Ο€-systems. For an indexed union version, see `mem_generate_pi_system_Union_elim'`. -/ lemma mem_generate_pi_system_Union_elim {Ξ± Ξ²} {g : Ξ² β†’ set (set Ξ±)} (h_pi : βˆ€ b, is_pi_system (g b)) (t : set Ξ±) (h_t : t ∈ generate_pi_system (⋃ b, g b)) : βˆƒ (T : finset Ξ²) (f : Ξ² β†’ set Ξ±), (t = β‹‚ b ∈ T, f b) ∧ (βˆ€ b ∈ T, f b ∈ g b) := begin induction h_t with s h_s s t' h_gen_s h_gen_t' h_nonempty h_s h_t', { rcases h_s with ⟨t', ⟨⟨b, rfl⟩, h_s_in_t'⟩⟩, refine ⟨{b}, (Ξ» _, s), _⟩, simpa using h_s_in_t', }, { rcases h_t' with ⟨T_t', ⟨f_t', ⟨rfl, h_t'⟩⟩⟩, rcases h_s with ⟨T_s, ⟨f_s, ⟨rfl, h_s⟩ ⟩ ⟩, use [(T_s βˆͺ T_t'), (Ξ» (b:Ξ²), if (b ∈ T_s) then (if (b ∈ T_t') then (f_s b ∩ (f_t' b)) else (f_s b)) else (if (b ∈ T_t') then (f_t' b) else (βˆ… : set Ξ±)))], split, { ext a, simp_rw [set.mem_inter_iff, set.mem_Inter, finset.mem_union, or_imp_distrib], rw ← forall_and_distrib, split; intros h1 b; by_cases hbs : b ∈ T_s; by_cases hbt : b ∈ T_t'; specialize h1 b; simp only [hbs, hbt, if_true, if_false, true_implies_iff, and_self, false_implies_iff, and_true, true_and] at h1 ⊒, all_goals { exact h1, }, }, intros b h_b, split_ifs with hbs hbt hbt, { refine h_pi b (f_s b) (f_t' b) (h_s b hbs) (h_t' b hbt) (set.nonempty.mono _ h_nonempty), exact set.inter_subset_inter (set.bInter_subset_of_mem hbs) (set.bInter_subset_of_mem hbt), }, { exact h_s b hbs, }, { exact h_t' b hbt, }, { rw finset.mem_union at h_b, apply false.elim (h_b.elim hbs hbt), }, }, end /- Every element of the Ο€-system generated by an indexed union of a family of Ο€-systems is a finite intersection of elements from the Ο€-systems. For a total union version, see `mem_generate_pi_system_Union_elim`. -/ lemma mem_generate_pi_system_Union_elim' {Ξ± Ξ²} {g : Ξ² β†’ set (set Ξ±)} {s: set Ξ²} (h_pi : βˆ€ b ∈ s, is_pi_system (g b)) (t : set Ξ±) (h_t : t ∈ generate_pi_system (⋃ b ∈ s, g b)) : βˆƒ (T : finset Ξ²) (f : Ξ² β†’ set Ξ±), (↑T βŠ† s) ∧ (t = β‹‚ b ∈ T, f b) ∧ (βˆ€ b ∈ T, f b ∈ g b) := begin have : t ∈ generate_pi_system (⋃ (b : subtype s), (g ∘ subtype.val) b), { suffices h1 : (⋃ (b : subtype s), (g ∘ subtype.val) b) = (⋃ b (H : b ∈ s), g b), by rwa h1, ext x, simp only [exists_prop, set.mem_Union, function.comp_app, subtype.exists, subtype.coe_mk], refl }, rcases @mem_generate_pi_system_Union_elim Ξ± (subtype s) (g ∘ subtype.val) (Ξ» b, h_pi b.val b.property) t this with ⟨T, ⟨f,⟨ rfl, h_t'⟩⟩⟩, refine ⟨T.image subtype.val, function.extend subtype.val f (Ξ» (b:Ξ²), (βˆ… : set Ξ±)), by simp, _, _⟩, { ext a, split; { simp only [set.mem_Inter, subtype.forall, finset.set_bInter_finset_image], intros h1 b h_b h_b_in_T, have h2 := h1 b h_b h_b_in_T, revert h2, rw function.extend_apply subtype.val_injective, apply id } }, { intros b h_b, simp_rw [finset.mem_image, exists_prop, subtype.exists, exists_and_distrib_right, exists_eq_right] at h_b, cases h_b, have h_b_alt : b = (subtype.mk b h_b_w).val := rfl, rw [h_b_alt, function.extend_apply subtype.val_injective], apply h_t', apply h_b_h }, end namespace measurable_space variable {Ξ± : Type*} /-- A Dynkin system is a collection of subsets of a type `Ξ±` that contains the empty set, is closed under complementation and under countable union of pairwise disjoint sets. The disjointness condition is the only difference with `Οƒ`-algebras. The main purpose of Dynkin systems is to provide a powerful induction rule for Οƒ-algebras generated by a collection of sets which is stable under intersection. A Dynkin system is also known as a "Ξ»-system" or a "d-system". -/ 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)) 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_decodeβ‚‚, exact d.has_Union_nat (encodable.Union_decodeβ‚‚_disjoint_on hd) (Ξ» n, encodable.Union_decodeβ‚‚_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β‚‚) := begin apply d.has_compl_iff.1, 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⟩ } /-- Every measurable space (Οƒ-algebra) forms a Dynkin system -/ def of_measurable_space (m : measurable_space Ξ±) : dynkin_system Ξ± := { has := m.measurable_set', has_empty := m.measurable_set_empty, has_compl := m.measurable_set_compl, has_Union_nat := assume f _ hf, m.measurable_set_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. This inductive type gives the underlying collection of 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) lemma generate_has_compl {C : set (set Ξ±)} {s : set Ξ±} : generate_has C sᢜ ↔ generate_has C s := by { refine ⟨_, generate_has.compl⟩, intro h, convert generate_has.compl h, simp } /-- The least Dynkin system containing a collection of basic sets. -/ def generate (s : set (set Ξ±)) : dynkin_system Ξ± := { has := generate_has s, has_empty := generate_has.empty, has_compl := assume a, generate_has.compl, has_Union_nat := assume f, generate_has.Union } lemma generate_has_def {C : set (set Ξ±)} : (generate C).has = generate_has C := rfl instance : inhabited (dynkin_system Ξ±) := ⟨generate univ⟩ /-- If a Dynkin system is closed under binary intersection, then it forms a `Οƒ`-algebra. -/ def to_measurable_space (h_inter : βˆ€ s₁ sβ‚‚, d.has s₁ β†’ d.has sβ‚‚ β†’ d.has (s₁ ∩ sβ‚‚)) := { measurable_space . measurable_set' := d.has, measurable_set_empty := d.has_empty, measurable_set_compl := assume s h, d.has_compl h, measurable_set_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 /-- If `s` is in a Dynkin system `d`, we can form the new Dynkin system `{s ∩ t | t ∈ d}`. -/ 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], exact 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_has_subset_generate_measurable {C : set (set Ξ±)} {s : set Ξ±} (hs : (generate C).has s) : (generate_from C).measurable_set' s := generate_le (of_measurable_space (generate_from C)) (Ξ» t, measurable_set_generate_from) s hs lemma generate_inter {s : set (set Ξ±)} (hs : is_pi_system 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 (sβ‚‚ ∩ s₁).eq_empty_or_nonempty.elim (Ξ» h, h.symm β–Έ generate_has.empty) (Ξ» h, 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₁ /-- **Dynkin's Ο€-Ξ» theorem**: Given a collection of sets closed under binary intersections, then the Dynkin system it generates is equal to the Οƒ-algebra it generates. This result is known as the Ο€-Ξ» theorem. A collection of sets closed under binary intersection is called a Ο€-system (often requiring additionnally that is is non-empty, but we drop this condition in the formalization). -/ lemma generate_from_eq {s : set (set Ξ±)} (hs : is_pi_system s) : generate_from s = (generate s).to_measurable_space (Ξ» 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], exact (generate_le _ $ assume t ht, measurable_set_generate_from ht) }) end dynkin_system theorem induction_on_inter {C : set Ξ± β†’ Prop} {s : set (set Ξ±)} [m : measurable_space Ξ±] (h_eq : m = generate_from s) (h_inter : is_pi_system s) (h_empty : C βˆ…) (h_basic : βˆ€ t ∈ s, C t) (h_compl : βˆ€ t, measurable_set t β†’ C t β†’ C tᢜ) (h_union : βˆ€ f : β„• β†’ set Ξ±, pairwise (disjoint on f) β†’ (βˆ€ i, measurable_set (f i)) β†’ (βˆ€ i, C (f i)) β†’ C (⋃ i, f i)) : βˆ€ ⦃t⦄, measurable_set t β†’ C t := have eq : measurable_set = 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
fb1a65ef55d8ce380253a1a6c96539ab40ea5e75
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/back_chaining1.lean
fd6cccda714012eac5fe70b8212e68e0251b2847
[ "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
599
lean
-- Backward chaining with tagged rules constants {P Q R S T U : Prop} (Hpq : P β†’ Q) (Hqr : Q β†’ R) (Hrs : R β†’ S) (Hst : S β†’ T) constants (Huq : U β†’ Q) (Hur : U β†’ R) (Hus : U β†’ S) (Hut : U β†’ T) attribute Hpq [intro] attribute Hqr [intro] attribute Hrs [intro] attribute Hst [intro] attribute Huq [intro] attribute Hur [intro] attribute Hus [intro] attribute Hut [intro] open tactic definition lemma1 (p : P) : Q := by back_chaining definition lemma2 (p : P) : R := by back_chaining definition lemma3 (p : P) : S := by back_chaining definition lemma4 (p : P) : T := by back_chaining
20398a6785d03f754fc259583cccb3db2d7873df
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/measure_theory/integration.lean
126cba98fdedc1c09bd03cc41e8084ab91b54690
[ "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
84,990
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes HΓΆlzl -/ import measure_theory.measure_space import measure_theory.borel_space import data.indicator_function import data.support /-! # Lebesgue integral for `ℝβ‰₯0∞`-valued functions We define simple functions and show that each Borel measurable function on `ℝβ‰₯0∞` can be approximated by a sequence of simple functions. To prove something for an arbitrary measurable function into `ℝβ‰₯0∞`, the theorem `measurable.ennreal_induction` shows that is it sufficient to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. ## Notation We introduce the following notation for the lower Lebesgue integral of a function `f : Ξ± β†’ ℝβ‰₯0∞`. * `∫⁻ x, f x βˆ‚ΞΌ`: integral of a function `f : Ξ± β†’ ℝβ‰₯0∞` with respect to a measure `ΞΌ`; * `∫⁻ x, f x`: integral of a function `f : Ξ± β†’ ℝβ‰₯0∞` with respect to the canonical measure `volume` on `Ξ±`; * `∫⁻ x in s, f x βˆ‚ΞΌ`: integral of a function `f : Ξ± β†’ ℝβ‰₯0∞` over a set `s` with respect to a measure `ΞΌ`, defined as `∫⁻ x, f x βˆ‚(ΞΌ.restrict s)`; * `∫⁻ x in s, f x`: integral of a function `f : Ξ± β†’ ℝβ‰₯0∞` over a set `s` with respect to the canonical measure `volume`, defined as `∫⁻ x, f x βˆ‚(volume.restrict s)`. -/ noncomputable theory open set (hiding restrict restrict_apply) filter ennreal function (support) open_locale classical topological_space big_operators nnreal ennreal namespace measure_theory variables {Ξ± Ξ² Ξ³ Ξ΄ : Type*} /-- A function `f` from a measurable space to any type is called *simple*, if every preimage `f ⁻¹' {x}` is measurable, and the range is finite. This structure bundles a function with these properties. -/ structure {u v} simple_func (Ξ± : Type u) [measurable_space Ξ±] (Ξ² : Type v) := (to_fun : Ξ± β†’ Ξ²) (measurable_set_fiber' : βˆ€ x, measurable_set (to_fun ⁻¹' {x})) (finite_range' : (set.range to_fun).finite) local infixr ` β†’β‚› `:25 := simple_func namespace simple_func section measurable variables [measurable_space Ξ±] instance has_coe_to_fun : has_coe_to_fun (Ξ± β†’β‚› Ξ²) := ⟨_, to_fun⟩ lemma coe_injective ⦃f g : Ξ± β†’β‚› β⦄ (H : ⇑f = g) : f = g := by cases f; cases g; congr; exact H @[ext] theorem ext {f g : Ξ± β†’β‚› Ξ²} (H : βˆ€ a, f a = g a) : f = g := coe_injective $ funext H lemma finite_range (f : Ξ± β†’β‚› Ξ²) : (set.range f).finite := f.finite_range' lemma measurable_set_fiber (f : Ξ± β†’β‚› Ξ²) (x : Ξ²) : measurable_set (f ⁻¹' {x}) := f.measurable_set_fiber' x /-- Range of a simple function `Ξ± β†’β‚› Ξ²` as a `finset Ξ²`. -/ protected def range (f : Ξ± β†’β‚› Ξ²) : finset Ξ² := f.finite_range.to_finset @[simp] theorem mem_range {f : Ξ± β†’β‚› Ξ²} {b} : b ∈ f.range ↔ b ∈ range f := finite.mem_to_finset _ theorem mem_range_self (f : Ξ± β†’β‚› Ξ²) (x : Ξ±) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩ @[simp] lemma coe_range (f : Ξ± β†’β‚› Ξ²) : (↑f.range : set Ξ²) = set.range f := f.finite_range.coe_to_finset theorem mem_range_of_measure_ne_zero {f : Ξ± β†’β‚› Ξ²} {x : Ξ²} {ΞΌ : measure Ξ±} (H : ΞΌ (f ⁻¹' {x}) β‰  0) : x ∈ f.range := let ⟨a, ha⟩ := nonempty_of_measure_ne_zero H in mem_range.2 ⟨a, ha⟩ lemma forall_range_iff {f : Ξ± β†’β‚› Ξ²} {p : Ξ² β†’ Prop} : (βˆ€ y ∈ f.range, p y) ↔ βˆ€ x, p (f x) := by simp only [mem_range, set.forall_range_iff] lemma exists_range_iff {f : Ξ± β†’β‚› Ξ²} {p : Ξ² β†’ Prop} : (βˆƒ y ∈ f.range, p y) ↔ βˆƒ x, p (f x) := by simpa only [mem_range, exists_prop] using set.exists_range_iff lemma preimage_eq_empty_iff (f : Ξ± β†’β‚› Ξ²) (b : Ξ²) : f ⁻¹' {b} = βˆ… ↔ b βˆ‰ f.range := preimage_singleton_eq_empty.trans $ not_congr mem_range.symm lemma exists_forall_le [nonempty Ξ²] [directed_order Ξ²] (f : Ξ± β†’β‚› Ξ²) : βˆƒ C, βˆ€ x, f x ≀ C := f.range.exists_le.imp $ Ξ» C, forall_range_iff.1 /-- Constant function as a `simple_func`. -/ def const (Ξ±) {Ξ²} [measurable_space Ξ±] (b : Ξ²) : Ξ± β†’β‚› Ξ² := ⟨λ a, b, Ξ» x, measurable_set.const _, finite_range_const⟩ instance [inhabited Ξ²] : inhabited (Ξ± β†’β‚› Ξ²) := ⟨const _ (default _)⟩ theorem const_apply (a : Ξ±) (b : Ξ²) : (const Ξ± b) a = b := rfl @[simp] theorem coe_const (b : Ξ²) : ⇑(const Ξ± b) = function.const Ξ± b := rfl @[simp] lemma range_const (Ξ±) [measurable_space Ξ±] [nonempty Ξ±] (b : Ξ²) : (const Ξ± b).range = {b} := finset.coe_injective $ by simp lemma measurable_set_cut (r : Ξ± β†’ Ξ² β†’ Prop) (f : Ξ± β†’β‚› Ξ²) (h : βˆ€b, measurable_set {a | r a b}) : measurable_set {a | r a (f a)} := begin have : {a | r a (f a)} = ⋃ b ∈ range f, {a | r a b} ∩ f ⁻¹' {b}, { ext a, suffices : r a (f a) ↔ βˆƒ i, r a (f i) ∧ f a = f i, by simpa, exact ⟨λ h, ⟨a, ⟨h, rfl⟩⟩, Ξ» ⟨a', ⟨h', e⟩⟩, e.symm β–Έ h'⟩ }, rw this, exact measurable_set.bUnion f.finite_range.countable (Ξ» b _, measurable_set.inter (h b) (f.measurable_set_fiber _)) end theorem measurable_set_preimage (f : Ξ± β†’β‚› Ξ²) (s) : measurable_set (f ⁻¹' s) := measurable_set_cut (Ξ» _ b, b ∈ s) f (Ξ» b, measurable_set.const (b ∈ s)) /-- A simple function is measurable -/ protected theorem measurable [measurable_space Ξ²] (f : Ξ± β†’β‚› Ξ²) : measurable f := Ξ» s _, measurable_set_preimage f s protected theorem ae_measurable [measurable_space Ξ²] {ΞΌ : measure Ξ±} (f : Ξ± β†’β‚› Ξ²) : ae_measurable f ΞΌ := f.measurable.ae_measurable protected lemma sum_measure_preimage_singleton (f : Ξ± β†’β‚› Ξ²) {ΞΌ : measure Ξ±} (s : finset Ξ²) : βˆ‘ y in s, ΞΌ (f ⁻¹' {y}) = ΞΌ (f ⁻¹' ↑s) := sum_measure_preimage_singleton _ (Ξ» _ _, f.measurable_set_fiber _) lemma sum_range_measure_preimage_singleton (f : Ξ± β†’β‚› Ξ²) (ΞΌ : measure Ξ±) : βˆ‘ y in f.range, ΞΌ (f ⁻¹' {y}) = ΞΌ univ := by rw [f.sum_measure_preimage_singleton, coe_range, preimage_range] /-- If-then-else as a `simple_func`. -/ def piecewise (s : set Ξ±) (hs : measurable_set s) (f g : Ξ± β†’β‚› Ξ²) : Ξ± β†’β‚› Ξ² := ⟨s.piecewise f g, Ξ» x, by letI : measurable_space Ξ² := ⊀; exact f.measurable.piecewise hs g.measurable trivial, (f.finite_range.union g.finite_range).subset range_ite_subset⟩ @[simp] theorem coe_piecewise {s : set Ξ±} (hs : measurable_set s) (f g : Ξ± β†’β‚› Ξ²) : ⇑(piecewise s hs f g) = s.piecewise f g := rfl theorem piecewise_apply {s : set Ξ±} (hs : measurable_set s) (f g : Ξ± β†’β‚› Ξ²) (a) : piecewise s hs f g a = if a ∈ s then f a else g a := rfl @[simp] lemma piecewise_compl {s : set Ξ±} (hs : measurable_set sᢜ) (f g : Ξ± β†’β‚› Ξ²) : piecewise sᢜ hs f g = piecewise s hs.of_compl g f := coe_injective $ by simp [hs] @[simp] lemma piecewise_univ (f g : Ξ± β†’β‚› Ξ²) : piecewise univ measurable_set.univ f g = f := coe_injective $ by simp @[simp] lemma piecewise_empty (f g : Ξ± β†’β‚› Ξ²) : piecewise βˆ… measurable_set.empty f g = g := coe_injective $ by simp lemma measurable_bind [measurable_space Ξ³] (f : Ξ± β†’β‚› Ξ²) (g : Ξ² β†’ Ξ± β†’ Ξ³) (hg : βˆ€ b, measurable (g b)) : measurable (Ξ» a, g (f a) a) := Ξ» s hs, f.measurable_set_cut (Ξ» a b, g b a ∈ s) $ Ξ» b, hg b hs /-- If `f : Ξ± β†’β‚› Ξ²` is a simple function and `g : Ξ² β†’ Ξ± β†’β‚› Ξ³` is a family of simple functions, then `f.bind g` binds the first argument of `g` to `f`. In other words, `f.bind g a = g (f a) a`. -/ def bind (f : Ξ± β†’β‚› Ξ²) (g : Ξ² β†’ Ξ± β†’β‚› Ξ³) : Ξ± β†’β‚› Ξ³ := ⟨λa, g (f a) a, Ξ» c, f.measurable_set_cut (Ξ» a b, g b a = c) $ Ξ» b, (g b).measurable_set_preimage {c}, (f.finite_range.bUnion (Ξ» b _, (g b).finite_range)).subset $ by rintro _ ⟨a, rfl⟩; simp; exact ⟨a, a, rfl⟩⟩ @[simp] theorem bind_apply (f : Ξ± β†’β‚› Ξ²) (g : Ξ² β†’ Ξ± β†’β‚› Ξ³) (a) : f.bind g a = g (f a) a := rfl /-- Given a function `g : Ξ² β†’ Ξ³` and a simple function `f : Ξ± β†’β‚› Ξ²`, `f.map g` return the simple function `g ∘ f : Ξ± β†’β‚› Ξ³` -/ def map (g : Ξ² β†’ Ξ³) (f : Ξ± β†’β‚› Ξ²) : Ξ± β†’β‚› Ξ³ := bind f (const Ξ± ∘ g) theorem map_apply (g : Ξ² β†’ Ξ³) (f : Ξ± β†’β‚› Ξ²) (a) : f.map g a = g (f a) := rfl theorem map_map (g : Ξ² β†’ Ξ³) (h: Ξ³ β†’ Ξ΄) (f : Ξ± β†’β‚› Ξ²) : (f.map g).map h = f.map (h ∘ g) := rfl @[simp] theorem coe_map (g : Ξ² β†’ Ξ³) (f : Ξ± β†’β‚› Ξ²) : (f.map g : Ξ± β†’ Ξ³) = g ∘ f := rfl @[simp] theorem range_map [decidable_eq Ξ³] (g : Ξ² β†’ Ξ³) (f : Ξ± β†’β‚› Ξ²) : (f.map g).range = f.range.image g := finset.coe_injective $ by simp [range_comp] @[simp] theorem map_const (g : Ξ² β†’ Ξ³) (b : Ξ²) : (const Ξ± b).map g = const Ξ± (g b) := rfl lemma map_preimage (f : Ξ± β†’β‚› Ξ²) (g : Ξ² β†’ Ξ³) (s : set Ξ³) : (f.map g) ⁻¹' s = f ⁻¹' ↑(f.range.filter (Ξ»b, g b ∈ s)) := by { simp only [coe_range, sep_mem_eq, set.mem_range, function.comp_app, coe_map, finset.coe_filter, ← mem_preimage, inter_comm, preimage_inter_range], apply preimage_comp } lemma map_preimage_singleton (f : Ξ± β†’β‚› Ξ²) (g : Ξ² β†’ Ξ³) (c : Ξ³) : (f.map g) ⁻¹' {c} = f ⁻¹' ↑(f.range.filter (Ξ» b, g b = c)) := map_preimage _ _ _ /-- Composition of a `simple_fun` and a measurable function is a `simple_func`. -/ def comp [measurable_space Ξ²] (f : Ξ² β†’β‚› Ξ³) (g : Ξ± β†’ Ξ²) (hgm : measurable g) : Ξ± β†’β‚› Ξ³ := { to_fun := f ∘ g, finite_range' := f.finite_range.subset $ set.range_comp_subset_range _ _, measurable_set_fiber' := Ξ» z, hgm (f.measurable_set_fiber z) } @[simp] lemma coe_comp [measurable_space Ξ²] (f : Ξ² β†’β‚› Ξ³) {g : Ξ± β†’ Ξ²} (hgm : measurable g) : ⇑(f.comp g hgm) = f ∘ g := rfl lemma range_comp_subset_range [measurable_space Ξ²] (f : Ξ² β†’β‚› Ξ³) {g : Ξ± β†’ Ξ²} (hgm : measurable g) : (f.comp g hgm).range βŠ† f.range := finset.coe_subset.1 $ by simp only [coe_range, coe_comp, set.range_comp_subset_range] /-- If `f` is a simple function taking values in `Ξ² β†’ Ξ³` and `g` is another simple function with the same domain and codomain `Ξ²`, then `f.seq g = f a (g a)`. -/ def seq (f : Ξ± β†’β‚› (Ξ² β†’ Ξ³)) (g : Ξ± β†’β‚› Ξ²) : Ξ± β†’β‚› Ξ³ := f.bind (Ξ»f, g.map f) @[simp] lemma seq_apply (f : Ξ± β†’β‚› (Ξ² β†’ Ξ³)) (g : Ξ± β†’β‚› Ξ²) (a : Ξ±) : f.seq g a = f a (g a) := rfl /-- Combine two simple functions `f : Ξ± β†’β‚› Ξ²` and `g : Ξ± β†’β‚› Ξ²` into `Ξ» a, (f a, g a)`. -/ def pair (f : Ξ± β†’β‚› Ξ²) (g : Ξ± β†’β‚› Ξ³) : Ξ± β†’β‚› (Ξ² Γ— Ξ³) := (f.map prod.mk).seq g @[simp] lemma pair_apply (f : Ξ± β†’β‚› Ξ²) (g : Ξ± β†’β‚› Ξ³) (a) : pair f g a = (f a, g a) := rfl lemma pair_preimage (f : Ξ± β†’β‚› Ξ²) (g : Ξ± β†’β‚› Ξ³) (s : set Ξ²) (t : set Ξ³) : (pair f g) ⁻¹' (set.prod s t) = (f ⁻¹' s) ∩ (g ⁻¹' t) := rfl /- A special form of `pair_preimage` -/ lemma pair_preimage_singleton (f : Ξ± β†’β‚› Ξ²) (g : Ξ± β†’β‚› Ξ³) (b : Ξ²) (c : Ξ³) : (pair f g) ⁻¹' {(b, c)} = (f ⁻¹' {b}) ∩ (g ⁻¹' {c}) := by { rw ← singleton_prod_singleton, exact pair_preimage _ _ _ _ } theorem bind_const (f : Ξ± β†’β‚› Ξ²) : f.bind (const Ξ±) = f := by ext; simp instance [has_zero Ξ²] : has_zero (Ξ± β†’β‚› Ξ²) := ⟨const Ξ± 0⟩ instance [has_add Ξ²] : has_add (Ξ± β†’β‚› Ξ²) := ⟨λf g, (f.map (+)).seq g⟩ instance [has_mul Ξ²] : has_mul (Ξ± β†’β‚› Ξ²) := ⟨λf g, (f.map (*)).seq g⟩ instance [has_sup Ξ²] : has_sup (Ξ± β†’β‚› Ξ²) := ⟨λf g, (f.map (βŠ”)).seq g⟩ instance [has_inf Ξ²] : has_inf (Ξ± β†’β‚› Ξ²) := ⟨λf g, (f.map (βŠ“)).seq g⟩ instance [has_le Ξ²] : has_le (Ξ± β†’β‚› Ξ²) := ⟨λf g, βˆ€a, f a ≀ g a⟩ @[simp, norm_cast] lemma coe_zero [has_zero Ξ²] : ⇑(0 : Ξ± β†’β‚› Ξ²) = 0 := rfl @[simp] lemma const_zero [has_zero Ξ²] : const Ξ± (0:Ξ²) = 0 := rfl @[simp, norm_cast] lemma coe_add [has_add Ξ²] (f g : Ξ± β†’β‚› Ξ²) : ⇑(f + g) = f + g := rfl @[simp, norm_cast] lemma coe_mul [has_mul Ξ²] (f g : Ξ± β†’β‚› Ξ²) : ⇑(f * g) = f * g := rfl @[simp, norm_cast] lemma coe_le [preorder Ξ²] {f g : Ξ± β†’β‚› Ξ²} : (f : Ξ± β†’ Ξ²) ≀ g ↔ f ≀ g := iff.rfl @[simp] lemma range_zero [nonempty Ξ±] [has_zero Ξ²] : (0 : Ξ± β†’β‚› Ξ²).range = {0} := finset.ext $ Ξ» x, by simp [eq_comm] lemma eq_zero_of_mem_range_zero [has_zero Ξ²] : βˆ€ {y : Ξ²}, y ∈ (0 : Ξ± β†’β‚› Ξ²).range β†’ y = 0 := forall_range_iff.2 $ Ξ» x, rfl lemma sup_apply [has_sup Ξ²] (f g : Ξ± β†’β‚› Ξ²) (a : Ξ±) : (f βŠ” g) a = f a βŠ” g a := rfl lemma mul_apply [has_mul Ξ²] (f g : Ξ± β†’β‚› Ξ²) (a : Ξ±) : (f * g) a = f a * g a := rfl lemma add_apply [has_add Ξ²] (f g : Ξ± β†’β‚› Ξ²) (a : Ξ±) : (f + g) a = f a + g a := rfl lemma add_eq_mapβ‚‚ [has_add Ξ²] (f g : Ξ± β†’β‚› Ξ²) : f + g = (pair f g).map (Ξ»p:Ξ²Γ—Ξ², p.1 + p.2) := rfl lemma mul_eq_mapβ‚‚ [has_mul Ξ²] (f g : Ξ± β†’β‚› Ξ²) : f * g = (pair f g).map (Ξ»p:Ξ²Γ—Ξ², p.1 * p.2) := rfl lemma sup_eq_mapβ‚‚ [has_sup Ξ²] (f g : Ξ± β†’β‚› Ξ²) : f βŠ” g = (pair f g).map (Ξ»p:Ξ²Γ—Ξ², p.1 βŠ” p.2) := rfl lemma const_mul_eq_map [has_mul Ξ²] (f : Ξ± β†’β‚› Ξ²) (b : Ξ²) : const Ξ± b * f = f.map (Ξ»a, b * a) := rfl theorem map_add [has_add Ξ²] [has_add Ξ³] {g : Ξ² β†’ Ξ³} (hg : βˆ€ x y, g (x + y) = g x + g y) (f₁ fβ‚‚ : Ξ± β†’β‚› Ξ²) : (f₁ + fβ‚‚).map g = f₁.map g + fβ‚‚.map g := ext $ Ξ» x, hg _ _ instance [add_monoid Ξ²] : add_monoid (Ξ± β†’β‚› Ξ²) := function.injective.add_monoid (Ξ» f, show Ξ± β†’ Ξ², from f) coe_injective coe_zero coe_add instance add_comm_monoid [add_comm_monoid Ξ²] : add_comm_monoid (Ξ± β†’β‚› Ξ²) := function.injective.add_comm_monoid (Ξ» f, show Ξ± β†’ Ξ², from f) coe_injective coe_zero coe_add instance [has_neg Ξ²] : has_neg (Ξ± β†’β‚› Ξ²) := ⟨λf, f.map (has_neg.neg)⟩ @[simp, norm_cast] lemma coe_neg [has_neg Ξ²] (f : Ξ± β†’β‚› Ξ²) : ⇑(-f) = -f := rfl instance [has_sub Ξ²] : has_sub (Ξ± β†’β‚› Ξ²) := ⟨λf g, (f.map (has_sub.sub)).seq g⟩ @[simp, norm_cast] lemma coe_sub [has_sub Ξ²] (f g : Ξ± β†’β‚› Ξ²) : ⇑(f - g) = f - g := rfl lemma sub_apply [has_sub Ξ²] (f g : Ξ± β†’β‚› Ξ²) (x : Ξ±) : (f - g) x = f x - g x := rfl instance [add_group Ξ²] : add_group (Ξ± β†’β‚› Ξ²) := function.injective.add_group (Ξ» f, show Ξ± β†’ Ξ², from f) coe_injective coe_zero coe_add coe_neg coe_sub instance [add_comm_group Ξ²] : add_comm_group (Ξ± β†’β‚› Ξ²) := function.injective.add_comm_group (Ξ» f, show Ξ± β†’ Ξ², from f) coe_injective coe_zero coe_add coe_neg coe_sub variables {K : Type*} instance [has_scalar K Ξ²] : has_scalar K (Ξ± β†’β‚› Ξ²) := ⟨λk f, f.map ((β€’) k)⟩ @[simp] lemma coe_smul [has_scalar K Ξ²] (c : K) (f : Ξ± β†’β‚› Ξ²) : ⇑(c β€’ f) = c β€’ f := rfl lemma smul_apply [has_scalar K Ξ²] (k : K) (f : Ξ± β†’β‚› Ξ²) (a : Ξ±) : (k β€’ f) a = k β€’ f a := rfl instance [semiring K] [add_comm_monoid Ξ²] [module K Ξ²] : module K (Ξ± β†’β‚› Ξ²) := function.injective.module K ⟨λ f, show Ξ± β†’ Ξ², from f, coe_zero, coe_add⟩ coe_injective coe_smul lemma smul_eq_map [has_scalar K Ξ²] (k : K) (f : Ξ± β†’β‚› Ξ²) : k β€’ f = f.map ((β€’) k) := rfl instance [preorder Ξ²] : preorder (Ξ± β†’β‚› Ξ²) := { le_refl := Ξ»f a, le_refl _, le_trans := Ξ»f g h hfg hgh a, le_trans (hfg _) (hgh a), .. simple_func.has_le } instance [partial_order Ξ²] : partial_order (Ξ± β†’β‚› Ξ²) := { le_antisymm := assume f g hfg hgf, ext $ assume a, le_antisymm (hfg a) (hgf a), .. simple_func.preorder } instance [order_bot Ξ²] : order_bot (Ξ± β†’β‚› Ξ²) := { bot := const Ξ± βŠ₯, bot_le := Ξ»f a, bot_le, .. simple_func.partial_order } instance [order_top Ξ²] : order_top (Ξ± β†’β‚› Ξ²) := { top := const Ξ± ⊀, le_top := Ξ»f a, le_top, .. simple_func.partial_order } instance [semilattice_inf Ξ²] : semilattice_inf (Ξ± β†’β‚› Ξ²) := { inf := (βŠ“), inf_le_left := assume f g a, inf_le_left, inf_le_right := assume f g a, inf_le_right, le_inf := assume f g h hfh hgh a, le_inf (hfh a) (hgh a), .. simple_func.partial_order } instance [semilattice_sup Ξ²] : semilattice_sup (Ξ± β†’β‚› Ξ²) := { sup := (βŠ”), le_sup_left := assume f g a, le_sup_left, le_sup_right := assume f g a, le_sup_right, sup_le := assume f g h hfh hgh a, sup_le (hfh a) (hgh a), .. simple_func.partial_order } instance [semilattice_sup_bot Ξ²] : semilattice_sup_bot (Ξ± β†’β‚› Ξ²) := { .. simple_func.semilattice_sup,.. simple_func.order_bot } instance [lattice Ξ²] : lattice (Ξ± β†’β‚› Ξ²) := { .. simple_func.semilattice_sup,.. simple_func.semilattice_inf } instance [bounded_lattice Ξ²] : bounded_lattice (Ξ± β†’β‚› Ξ²) := { .. simple_func.lattice, .. simple_func.order_bot, .. simple_func.order_top } lemma finset_sup_apply [semilattice_sup_bot Ξ²] {f : Ξ³ β†’ Ξ± β†’β‚› Ξ²} (s : finset Ξ³) (a : Ξ±) : s.sup f a = s.sup (Ξ»c, f c a) := begin refine finset.induction_on s rfl _, assume a s hs ih, rw [finset.sup_insert, finset.sup_insert, sup_apply, ih] end section restrict variables [has_zero Ξ²] /-- Restrict a simple function `f : Ξ± β†’β‚› Ξ²` to a set `s`. If `s` is measurable, then `f.restrict s a = if a ∈ s then f a else 0`, otherwise `f.restrict s = const Ξ± 0`. -/ def restrict (f : Ξ± β†’β‚› Ξ²) (s : set Ξ±) : Ξ± β†’β‚› Ξ² := if hs : measurable_set s then piecewise s hs f 0 else 0 theorem restrict_of_not_measurable {f : Ξ± β†’β‚› Ξ²} {s : set Ξ±} (hs : Β¬measurable_set s) : restrict f s = 0 := dif_neg hs @[simp] theorem coe_restrict (f : Ξ± β†’β‚› Ξ²) {s : set Ξ±} (hs : measurable_set s) : ⇑(restrict f s) = indicator s f := by { rw [restrict, dif_pos hs], refl } @[simp] theorem restrict_univ (f : Ξ± β†’β‚› Ξ²) : restrict f univ = f := by simp [restrict] @[simp] theorem restrict_empty (f : Ξ± β†’β‚› Ξ²) : restrict f βˆ… = 0 := by simp [restrict] theorem map_restrict_of_zero [has_zero Ξ³] {g : Ξ² β†’ Ξ³} (hg : g 0 = 0) (f : Ξ± β†’β‚› Ξ²) (s : set Ξ±) : (f.restrict s).map g = (f.map g).restrict s := ext $ Ξ» x, if hs : measurable_set s then by simp [hs, set.indicator_comp_of_zero hg] else by simp [restrict_of_not_measurable hs, hg] theorem map_coe_ennreal_restrict (f : Ξ± β†’β‚› ℝβ‰₯0) (s : set Ξ±) : (f.restrict s).map (coe : ℝβ‰₯0 β†’ ℝβ‰₯0∞) = (f.map coe).restrict s := map_restrict_of_zero ennreal.coe_zero _ _ theorem map_coe_nnreal_restrict (f : Ξ± β†’β‚› ℝβ‰₯0) (s : set Ξ±) : (f.restrict s).map (coe : ℝβ‰₯0 β†’ ℝ) = (f.map coe).restrict s := map_restrict_of_zero nnreal.coe_zero _ _ theorem restrict_apply (f : Ξ± β†’β‚› Ξ²) {s : set Ξ±} (hs : measurable_set s) (a) : restrict f s a = indicator s f a := by simp only [f.coe_restrict hs] theorem restrict_preimage (f : Ξ± β†’β‚› Ξ²) {s : set Ξ±} (hs : measurable_set s) {t : set Ξ²} (ht : (0:Ξ²) βˆ‰ t) : restrict f s ⁻¹' t = s ∩ f ⁻¹' t := by simp [hs, indicator_preimage_of_not_mem _ _ ht, inter_comm] theorem restrict_preimage_singleton (f : Ξ± β†’β‚› Ξ²) {s : set Ξ±} (hs : measurable_set s) {r : Ξ²} (hr : r β‰  0) : restrict f s ⁻¹' {r} = s ∩ f ⁻¹' {r} := f.restrict_preimage hs hr.symm lemma mem_restrict_range {r : Ξ²} {s : set Ξ±} {f : Ξ± β†’β‚› Ξ²} (hs : measurable_set s) : r ∈ (restrict f s).range ↔ (r = 0 ∧ s β‰  univ) ∨ (r ∈ f '' s) := by rw [← finset.mem_coe, coe_range, coe_restrict _ hs, mem_range_indicator] lemma mem_image_of_mem_range_restrict {r : Ξ²} {s : set Ξ±} {f : Ξ± β†’β‚› Ξ²} (hr : r ∈ (restrict f s).range) (h0 : r β‰  0) : r ∈ f '' s := if hs : measurable_set s then by simpa [mem_restrict_range hs, h0] using hr else by { rw [restrict_of_not_measurable hs] at hr, exact (h0 $ eq_zero_of_mem_range_zero hr).elim } @[mono] lemma restrict_mono [preorder Ξ²] (s : set Ξ±) {f g : Ξ± β†’β‚› Ξ²} (H : f ≀ g) : f.restrict s ≀ g.restrict s := if hs : measurable_set s then Ξ» x, by simp only [coe_restrict _ hs, indicator_le_indicator (H x)] else by simp only [restrict_of_not_measurable hs, le_refl] end restrict section approx section variables [semilattice_sup_bot Ξ²] [has_zero Ξ²] /-- Fix a sequence `i : β„• β†’ Ξ²`. Given a function `Ξ± β†’ Ξ²`, its `n`-th approximation by simple functions is defined so that in case `Ξ² = ℝβ‰₯0∞` it sends each `a` to the supremum of the set `{i k | k ≀ n ∧ i k ≀ f a}`, see `approx_apply` and `supr_approx_apply` for details. -/ def approx (i : β„• β†’ Ξ²) (f : Ξ± β†’ Ξ²) (n : β„•) : Ξ± β†’β‚› Ξ² := (finset.range n).sup (Ξ»k, restrict (const Ξ± (i k)) {a:Ξ± | i k ≀ f a}) lemma approx_apply [topological_space Ξ²] [order_closed_topology Ξ²] [measurable_space Ξ²] [opens_measurable_space Ξ²] {i : β„• β†’ Ξ²} {f : Ξ± β†’ Ξ²} {n : β„•} (a : Ξ±) (hf : measurable f) : (approx i f n : Ξ± β†’β‚› Ξ²) a = (finset.range n).sup (Ξ»k, if i k ≀ f a then i k else 0) := begin dsimp only [approx], rw [finset_sup_apply], congr, funext k, rw [restrict_apply], refl, exact (hf measurable_set_Ici) end lemma monotone_approx (i : β„• β†’ Ξ²) (f : Ξ± β†’ Ξ²) : monotone (approx i f) := assume n m h, finset.sup_mono $ finset.range_subset.2 h lemma approx_comp [topological_space Ξ²] [order_closed_topology Ξ²] [measurable_space Ξ²] [opens_measurable_space Ξ²] [measurable_space Ξ³] {i : β„• β†’ Ξ²} {f : Ξ³ β†’ Ξ²} {g : Ξ± β†’ Ξ³} {n : β„•} (a : Ξ±) (hf : measurable f) (hg : measurable g) : (approx i (f ∘ g) n : Ξ± β†’β‚› Ξ²) a = (approx i f n : Ξ³ β†’β‚› Ξ²) (g a) := by rw [approx_apply _ hf, approx_apply _ (hf.comp hg)] end lemma supr_approx_apply [topological_space Ξ²] [complete_lattice Ξ²] [order_closed_topology Ξ²] [has_zero Ξ²] [measurable_space Ξ²] [opens_measurable_space Ξ²] (i : β„• β†’ Ξ²) (f : Ξ± β†’ Ξ²) (a : Ξ±) (hf : measurable f) (h_zero : (0 : Ξ²) = βŠ₯) : (⨆n, (approx i f n : Ξ± β†’β‚› Ξ²) a) = (⨆k (h : i k ≀ f a), i k) := begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume k, supr_le $ assume hk, _), { rw [approx_apply a hf, h_zero], refine finset.sup_le (assume k hk, _), split_ifs, exact le_supr_of_le k (le_supr _ h), exact bot_le }, { refine le_supr_of_le (k+1) _, rw [approx_apply a hf], have : k ∈ finset.range (k+1) := finset.mem_range.2 (nat.lt_succ_self _), refine le_trans (le_of_eq _) (finset.le_sup this), rw [if_pos hk] } end end approx section eapprox /-- A sequence of `ℝβ‰₯0∞`s such that its range is the set of non-negative rational numbers. -/ def ennreal_rat_embed (n : β„•) : ℝβ‰₯0∞ := ennreal.of_real ((encodable.decode β„š n).get_or_else (0 : β„š)) lemma ennreal_rat_embed_encode (q : β„š) : ennreal_rat_embed (encodable.encode q) = nnreal.of_real q := by rw [ennreal_rat_embed, encodable.encodek]; refl /-- Approximate a function `Ξ± β†’ ℝβ‰₯0∞` by a sequence of simple functions. -/ def eapprox : (Ξ± β†’ ℝβ‰₯0∞) β†’ β„• β†’ Ξ± β†’β‚› ℝβ‰₯0∞ := approx ennreal_rat_embed @[mono] lemma monotone_eapprox (f : Ξ± β†’ ℝβ‰₯0∞) : monotone (eapprox f) := monotone_approx _ f lemma supr_eapprox_apply (f : Ξ± β†’ ℝβ‰₯0∞) (hf : measurable f) (a : Ξ±) : (⨆n, (eapprox f n : Ξ± β†’β‚› ℝβ‰₯0∞) a) = f a := begin rw [eapprox, supr_approx_apply ennreal_rat_embed f a hf rfl], refine le_antisymm (supr_le $ assume i, supr_le $ assume hi, hi) (le_of_not_gt _), assume h, rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨q, hq, lt_q, q_lt⟩, have : (nnreal.of_real q : ℝβ‰₯0∞) ≀ (⨆ (k : β„•) (h : ennreal_rat_embed k ≀ f a), ennreal_rat_embed k), { refine le_supr_of_le (encodable.encode q) _, rw [ennreal_rat_embed_encode q], refine le_supr_of_le (le_of_lt q_lt) _, exact le_refl _ }, exact lt_irrefl _ (lt_of_le_of_lt this lt_q) end lemma eapprox_comp [measurable_space Ξ³] {f : Ξ³ β†’ ℝβ‰₯0∞} {g : Ξ± β†’ Ξ³} {n : β„•} (hf : measurable f) (hg : measurable g) : (eapprox (f ∘ g) n : Ξ± β†’ ℝβ‰₯0∞) = (eapprox f n : Ξ³ β†’β‚› ℝβ‰₯0∞) ∘ g := funext $ assume a, approx_comp a hf hg end eapprox end measurable section measure variables [measurable_space Ξ±] {ΞΌ : measure Ξ±} /-- Integral of a simple function whose codomain is `ℝβ‰₯0∞`. -/ def lintegral (f : Ξ± β†’β‚› ℝβ‰₯0∞) (ΞΌ : measure Ξ±) : ℝβ‰₯0∞ := βˆ‘ x in f.range, x * ΞΌ (f ⁻¹' {x}) lemma lintegral_eq_of_subset (f : Ξ± β†’β‚› ℝβ‰₯0∞) {s : finset ℝβ‰₯0∞} (hs : βˆ€ x, f x β‰  0 β†’ ΞΌ (f ⁻¹' {f x}) β‰  0 β†’ f x ∈ s) : f.lintegral ΞΌ = βˆ‘ x in s, x * ΞΌ (f ⁻¹' {x}) := begin refine finset.sum_bij_ne_zero (Ξ»r _ _, r) _ _ _ _, { simpa only [forall_range_iff, mul_ne_zero_iff, and_imp] }, { intros, assumption }, { intros b _ hb, refine ⟨b, _, hb, rfl⟩, rw [mem_range, ← preimage_singleton_nonempty], exact nonempty_of_measure_ne_zero (mul_ne_zero_iff.1 hb).2 }, { intros, refl } end /-- Calculate the integral of `(g ∘ f)`, where `g : Ξ² β†’ ℝβ‰₯0∞` and `f : Ξ± β†’β‚› Ξ²`. -/ lemma map_lintegral (g : Ξ² β†’ ℝβ‰₯0∞) (f : Ξ± β†’β‚› Ξ²) : (f.map g).lintegral ΞΌ = βˆ‘ x in f.range, g x * ΞΌ (f ⁻¹' {x}) := begin simp only [lintegral, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, rw [map_preimage_singleton, ← f.sum_measure_preimage_singleton, finset.mul_sum], refine finset.sum_congr _ _, { congr }, { assume x, simp only [finset.mem_filter], rintro ⟨_, h⟩, rw h }, end lemma add_lintegral (f g : Ξ± β†’β‚› ℝβ‰₯0∞) : (f + g).lintegral ΞΌ = f.lintegral ΞΌ + g.lintegral ΞΌ := calc (f + g).lintegral ΞΌ = βˆ‘ x in (pair f g).range, (x.1 * ΞΌ (pair f g ⁻¹' {x}) + x.2 * ΞΌ (pair f g ⁻¹' {x})) : by rw [add_eq_mapβ‚‚, map_lintegral]; exact finset.sum_congr rfl (assume a ha, add_mul _ _ _) ... = βˆ‘ x in (pair f g).range, x.1 * ΞΌ (pair f g ⁻¹' {x}) + βˆ‘ x in (pair f g).range, x.2 * ΞΌ (pair f g ⁻¹' {x}) : by rw [finset.sum_add_distrib] ... = ((pair f g).map prod.fst).lintegral ΞΌ + ((pair f g).map prod.snd).lintegral ΞΌ : by rw [map_lintegral, map_lintegral] ... = lintegral f ΞΌ + lintegral g ΞΌ : rfl lemma const_mul_lintegral (f : Ξ± β†’β‚› ℝβ‰₯0∞) (x : ℝβ‰₯0∞) : (const Ξ± x * f).lintegral ΞΌ = x * f.lintegral ΞΌ := calc (f.map (Ξ»a, x * a)).lintegral ΞΌ = βˆ‘ r in f.range, x * r * ΞΌ (f ⁻¹' {r}) : map_lintegral _ _ ... = βˆ‘ r in f.range, x * (r * ΞΌ (f ⁻¹' {r})) : finset.sum_congr rfl (assume a ha, mul_assoc _ _ _) ... = x * f.lintegral ΞΌ : finset.mul_sum.symm /-- Integral of a simple function `Ξ± β†’β‚› ℝβ‰₯0∞` as a bilinear map. -/ def lintegralβ‚— : (Ξ± β†’β‚› ℝβ‰₯0∞) β†’β‚—[ℝβ‰₯0∞] measure Ξ± β†’β‚—[ℝβ‰₯0∞] ℝβ‰₯0∞ := { to_fun := Ξ» f, { to_fun := lintegral f, map_add' := by simp [lintegral, mul_add, finset.sum_add_distrib], map_smul' := Ξ» c ΞΌ, by simp [lintegral, mul_left_comm _ c, finset.mul_sum] }, map_add' := Ξ» f g, linear_map.ext (Ξ» ΞΌ, add_lintegral f g), map_smul' := Ξ» c f, linear_map.ext (Ξ» ΞΌ, const_mul_lintegral f c) } @[simp] lemma zero_lintegral : (0 : Ξ± β†’β‚› ℝβ‰₯0∞).lintegral ΞΌ = 0 := linear_map.ext_iff.1 lintegralβ‚—.map_zero ΞΌ lemma lintegral_add {Ξ½} (f : Ξ± β†’β‚› ℝβ‰₯0∞) : f.lintegral (ΞΌ + Ξ½) = f.lintegral ΞΌ + f.lintegral Ξ½ := (lintegralβ‚— f).map_add ΞΌ Ξ½ lemma lintegral_smul (f : Ξ± β†’β‚› ℝβ‰₯0∞) (c : ℝβ‰₯0∞) : f.lintegral (c β€’ ΞΌ) = c β€’ f.lintegral ΞΌ := (lintegralβ‚— f).map_smul c ΞΌ @[simp] lemma lintegral_zero (f : Ξ± β†’β‚› ℝβ‰₯0∞) : f.lintegral 0 = 0 := (lintegralβ‚— f).map_zero lemma lintegral_sum {ΞΉ} (f : Ξ± β†’β‚› ℝβ‰₯0∞) (ΞΌ : ΞΉ β†’ measure Ξ±) : f.lintegral (measure.sum ΞΌ) = βˆ‘' i, f.lintegral (ΞΌ i) := begin simp only [lintegral, measure.sum_apply, f.measurable_set_preimage, ← finset.tsum_subtype, ← ennreal.tsum_mul_left], apply ennreal.tsum_comm end lemma restrict_lintegral (f : Ξ± β†’β‚› ℝβ‰₯0∞) {s : set Ξ±} (hs : measurable_set s) : (restrict f s).lintegral ΞΌ = βˆ‘ r in f.range, r * ΞΌ (f ⁻¹' {r} ∩ s) := calc (restrict f s).lintegral ΞΌ = βˆ‘ r in f.range, r * ΞΌ (restrict f s ⁻¹' {r}) : lintegral_eq_of_subset _ $ Ξ» x hx, if hxs : x ∈ s then Ξ» _, by simp only [f.restrict_apply hs, indicator_of_mem hxs, mem_range_self] else false.elim $ hx $ by simp [*] ... = βˆ‘ r in f.range, r * ΞΌ (f ⁻¹' {r} ∩ s) : finset.sum_congr rfl $ forall_range_iff.2 $ Ξ» b, if hb : f b = 0 then by simp only [hb, zero_mul] else by rw [restrict_preimage_singleton _ hs hb, inter_comm] lemma lintegral_restrict (f : Ξ± β†’β‚› ℝβ‰₯0∞) (s : set Ξ±) (ΞΌ : measure Ξ±) : f.lintegral (ΞΌ.restrict s) = βˆ‘ y in f.range, y * ΞΌ (f ⁻¹' {y} ∩ s) := by simp only [lintegral, measure.restrict_apply, f.measurable_set_preimage] lemma restrict_lintegral_eq_lintegral_restrict (f : Ξ± β†’β‚› ℝβ‰₯0∞) {s : set Ξ±} (hs : measurable_set s) : (restrict f s).lintegral ΞΌ = f.lintegral (ΞΌ.restrict s) := by rw [f.restrict_lintegral hs, lintegral_restrict] lemma const_lintegral (c : ℝβ‰₯0∞) : (const Ξ± c).lintegral ΞΌ = c * ΞΌ univ := begin rw [lintegral], by_cases ha : nonempty Ξ±, { resetI, simp [preimage_const_of_mem] }, { simp [ΞΌ.eq_zero_of_not_nonempty ha] } end lemma const_lintegral_restrict (c : ℝβ‰₯0∞) (s : set Ξ±) : (const Ξ± c).lintegral (ΞΌ.restrict s) = c * ΞΌ s := by rw [const_lintegral, measure.restrict_apply measurable_set.univ, univ_inter] lemma restrict_const_lintegral (c : ℝβ‰₯0∞) {s : set Ξ±} (hs : measurable_set s) : ((const Ξ± c).restrict s).lintegral ΞΌ = c * ΞΌ s := by rw [restrict_lintegral_eq_lintegral_restrict _ hs, const_lintegral_restrict] lemma le_sup_lintegral (f g : Ξ± β†’β‚› ℝβ‰₯0∞) : f.lintegral ΞΌ βŠ” g.lintegral ΞΌ ≀ (f βŠ” g).lintegral ΞΌ := calc f.lintegral ΞΌ βŠ” g.lintegral ΞΌ = ((pair f g).map prod.fst).lintegral ΞΌ βŠ” ((pair f g).map prod.snd).lintegral ΞΌ : rfl ... ≀ βˆ‘ x in (pair f g).range, (x.1 βŠ” x.2) * ΞΌ (pair f g ⁻¹' {x}) : begin rw [map_lintegral, map_lintegral], refine sup_le _ _; refine finset.sum_le_sum (Ξ» a _, canonically_ordered_semiring.mul_le_mul _ (le_refl _)), exact le_sup_left, exact le_sup_right end ... = (f βŠ” g).lintegral ΞΌ : by rw [sup_eq_mapβ‚‚, map_lintegral] /-- `simple_func.lintegral` is monotone both in function and in measure. -/ @[mono] lemma lintegral_mono {f g : Ξ± β†’β‚› ℝβ‰₯0∞} (hfg : f ≀ g) {ΞΌ Ξ½ : measure Ξ±} (hΞΌΞ½ : ΞΌ ≀ Ξ½) : f.lintegral ΞΌ ≀ g.lintegral Ξ½ := calc f.lintegral ΞΌ ≀ f.lintegral ΞΌ βŠ” g.lintegral ΞΌ : le_sup_left ... ≀ (f βŠ” g).lintegral ΞΌ : le_sup_lintegral _ _ ... = g.lintegral ΞΌ : by rw [sup_of_le_right hfg] ... ≀ g.lintegral Ξ½ : finset.sum_le_sum $ Ξ» y hy, ennreal.mul_left_mono $ hΞΌΞ½ _ (g.measurable_set_preimage _) /-- `simple_func.lintegral` depends only on the measures of `f ⁻¹' {y}`. -/ lemma lintegral_eq_of_measure_preimage [measurable_space Ξ²] {f : Ξ± β†’β‚› ℝβ‰₯0∞} {g : Ξ² β†’β‚› ℝβ‰₯0∞} {Ξ½ : measure Ξ²} (H : βˆ€ y, ΞΌ (f ⁻¹' {y}) = Ξ½ (g ⁻¹' {y})) : f.lintegral ΞΌ = g.lintegral Ξ½ := begin simp only [lintegral, ← H], apply lintegral_eq_of_subset, simp only [H], intros, exact mem_range_of_measure_ne_zero β€Ή_β€Ί end /-- If two simple functions are equal a.e., then their `lintegral`s are equal. -/ lemma lintegral_congr {f g : Ξ± β†’β‚› ℝβ‰₯0∞} (h : f =ᡐ[ΞΌ] g) : f.lintegral ΞΌ = g.lintegral ΞΌ := lintegral_eq_of_measure_preimage $ Ξ» y, measure_congr $ eventually.set_eq $ h.mono $ Ξ» x hx, by simp [hx] lemma lintegral_map {Ξ²} [measurable_space Ξ²] {ΞΌ' : measure Ξ²} (f : Ξ± β†’β‚› ℝβ‰₯0∞) (g : Ξ² β†’β‚› ℝβ‰₯0∞) (m : Ξ± β†’ Ξ²) (eq : βˆ€a:Ξ±, f a = g (m a)) (h : βˆ€s:set Ξ², measurable_set s β†’ ΞΌ' s = ΞΌ (m ⁻¹' s)) : f.lintegral ΞΌ = g.lintegral ΞΌ' := lintegral_eq_of_measure_preimage $ Ξ» y, by { simp only [preimage, eq], exact (h (g ⁻¹' {y}) (g.measurable_set_preimage _)).symm } /-- The `lintegral` of simple functions transforms appropriately under a measurable equivalence. (Compare `lintegral_map`, which applies to a broader class of transformations of the domain, but requires measurability of the function being integrated.) -/ lemma lintegral_map_equiv {Ξ²} [measurable_space Ξ²] (g : Ξ² β†’β‚› ℝβ‰₯0∞) (m : Ξ± ≃ᡐ Ξ²) : (g.comp m m.measurable).lintegral ΞΌ = g.lintegral (measure.map m ΞΌ) := begin simp [simple_func.lintegral], have : (g.comp m m.measurable).range = g.range, { refine le_antisymm _ _, { exact g.range_comp_subset_range m.measurable }, convert (g.comp m m.measurable).range_comp_subset_range m.symm.measurable, apply simple_func.ext, intros a, exact congr_arg g (congr_fun m.self_comp_symm.symm a) }, rw this, congr' 1, funext, rw [m.map_apply (g ⁻¹' {x})], refl, end end measure section fin_meas_supp variables [measurable_space Ξ±] [has_zero Ξ²] [has_zero Ξ³] {ΞΌ : measure Ξ±} open finset function lemma support_eq (f : Ξ± β†’β‚› Ξ²) : support f = ⋃ y ∈ f.range.filter (Ξ» y, y β‰  0), f ⁻¹' {y} := set.ext $ Ξ» x, by simp only [finset.set_bUnion_preimage_singleton, mem_support, set.mem_preimage, finset.mem_coe, mem_filter, mem_range_self, true_and] /-- A `simple_func` has finite measure support if it is equal to `0` outside of a set of finite measure. -/ protected def fin_meas_supp (f : Ξ± β†’β‚› Ξ²) (ΞΌ : measure Ξ±) : Prop := f =αΆ [ΞΌ.cofinite] 0 lemma fin_meas_supp_iff_support {f : Ξ± β†’β‚› Ξ²} {ΞΌ : measure Ξ±} : f.fin_meas_supp ΞΌ ↔ ΞΌ (support f) < ∞ := iff.rfl lemma fin_meas_supp_iff {f : Ξ± β†’β‚› Ξ²} {ΞΌ : measure Ξ±} : f.fin_meas_supp ΞΌ ↔ βˆ€ y β‰  0, ΞΌ (f ⁻¹' {y}) < ∞ := begin split, { refine Ξ» h y hy, lt_of_le_of_lt (measure_mono _) h, exact Ξ» x hx (H : f x = 0), hy $ H β–Έ eq.symm hx }, { intro H, rw [fin_meas_supp_iff_support, support_eq], refine lt_of_le_of_lt (measure_bUnion_finset_le _ _) (sum_lt_top _), exact Ξ» y hy, H y (finset.mem_filter.1 hy).2 } end namespace fin_meas_supp lemma meas_preimage_singleton_ne_zero {f : Ξ± β†’β‚› Ξ²} (h : f.fin_meas_supp ΞΌ) {y : Ξ²} (hy : y β‰  0) : ΞΌ (f ⁻¹' {y}) < ∞ := fin_meas_supp_iff.1 h y hy protected lemma map {f : Ξ± β†’β‚› Ξ²} {g : Ξ² β†’ Ξ³} (hf : f.fin_meas_supp ΞΌ) (hg : g 0 = 0) : (f.map g).fin_meas_supp ΞΌ := flip lt_of_le_of_lt hf (measure_mono $ support_comp_subset hg f) lemma of_map {f : Ξ± β†’β‚› Ξ²} {g : Ξ² β†’ Ξ³} (h : (f.map g).fin_meas_supp ΞΌ) (hg : βˆ€b, g b = 0 β†’ b = 0) : f.fin_meas_supp ΞΌ := flip lt_of_le_of_lt h $ measure_mono $ support_subset_comp hg _ lemma map_iff {f : Ξ± β†’β‚› Ξ²} {g : Ξ² β†’ Ξ³} (hg : βˆ€ {b}, g b = 0 ↔ b = 0) : (f.map g).fin_meas_supp ΞΌ ↔ f.fin_meas_supp ΞΌ := ⟨λ h, h.of_map $ Ξ» b, hg.1, Ξ» h, h.map $ hg.2 rfl⟩ protected lemma pair {f : Ξ± β†’β‚› Ξ²} {g : Ξ± β†’β‚› Ξ³} (hf : f.fin_meas_supp ΞΌ) (hg : g.fin_meas_supp ΞΌ) : (pair f g).fin_meas_supp ΞΌ := calc ΞΌ (support $ pair f g) = ΞΌ (support f βˆͺ support g) : congr_arg ΞΌ $ support_prod_mk f g ... ≀ ΞΌ (support f) + ΞΌ (support g) : measure_union_le _ _ ... < _ : add_lt_top.2 ⟨hf, hg⟩ protected lemma mapβ‚‚ [has_zero Ξ΄] {ΞΌ : measure Ξ±} {f : Ξ± β†’β‚› Ξ²} (hf : f.fin_meas_supp ΞΌ) {g : Ξ± β†’β‚› Ξ³} (hg : g.fin_meas_supp ΞΌ) {op : Ξ² β†’ Ξ³ β†’ Ξ΄} (H : op 0 0 = 0) : ((pair f g).map (function.uncurry op)).fin_meas_supp ΞΌ := (hf.pair hg).map H protected lemma add {Ξ²} [add_monoid Ξ²] {f g : Ξ± β†’β‚› Ξ²} (hf : f.fin_meas_supp ΞΌ) (hg : g.fin_meas_supp ΞΌ) : (f + g).fin_meas_supp ΞΌ := by { rw [add_eq_mapβ‚‚], exact hf.mapβ‚‚ hg (zero_add 0) } protected lemma mul {Ξ²} [monoid_with_zero Ξ²] {f g : Ξ± β†’β‚› Ξ²} (hf : f.fin_meas_supp ΞΌ) (hg : g.fin_meas_supp ΞΌ) : (f * g).fin_meas_supp ΞΌ := by { rw [mul_eq_mapβ‚‚], exact hf.mapβ‚‚ hg (zero_mul 0) } lemma lintegral_lt_top {f : Ξ± β†’β‚› ℝβ‰₯0∞} (hm : f.fin_meas_supp ΞΌ) (hf : βˆ€α΅ a βˆ‚ΞΌ, f a < ∞) : f.lintegral ΞΌ < ∞ := begin refine sum_lt_top (Ξ» a ha, _), rcases eq_or_lt_of_le (le_top : a ≀ ∞) with rfl|ha, { simp only [ae_iff, lt_top_iff_ne_top, ne.def, not_not] at hf, simp [set.preimage, hf] }, { by_cases ha0 : a = 0, { subst a, rwa [zero_mul] }, { exact mul_lt_top ha (fin_meas_supp_iff.1 hm _ ha0) } } end lemma of_lintegral_lt_top {f : Ξ± β†’β‚› ℝβ‰₯0∞} (h : f.lintegral ΞΌ < ∞) : f.fin_meas_supp ΞΌ := begin refine fin_meas_supp_iff.2 (Ξ» b hb, _), rw [lintegral, sum_lt_top_iff] at h, by_cases b_mem : b ∈ f.range, { rw ennreal.lt_top_iff_ne_top, have h : Β¬ _ = ∞ := ennreal.lt_top_iff_ne_top.1 (h b b_mem), simp only [mul_eq_top, not_or_distrib, not_and_distrib] at h, rcases h with ⟨h, h'⟩, refine or.elim h (Ξ»h, by contradiction) (Ξ»h, h) }, { rw ← preimage_eq_empty_iff at b_mem, rw [b_mem, measure_empty], exact with_top.zero_lt_top } end lemma iff_lintegral_lt_top {f : Ξ± β†’β‚› ℝβ‰₯0∞} (hf : βˆ€α΅ a βˆ‚ΞΌ, f a < ∞) : f.fin_meas_supp ΞΌ ↔ f.lintegral ΞΌ < ∞ := ⟨λ h, h.lintegral_lt_top hf, Ξ» h, of_lintegral_lt_top h⟩ end fin_meas_supp end fin_meas_supp /-- To prove something for an arbitrary simple function, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition (of functions with disjoint support). It is possible to make the hypotheses in `h_add` a bit stronger, and such conditions can be added once we need them (for example it is only necessary to consider the case where `g` is a multiple of a characteristic function, and that this multiple doesn't appear in the image of `f`) -/ @[elab_as_eliminator] protected lemma induction {Ξ± Ξ³} [measurable_space Ξ±] [add_monoid Ξ³] {P : simple_func Ξ± Ξ³ β†’ Prop} (h_ind : βˆ€ c {s} (hs : measurable_set s), P (simple_func.piecewise s hs (simple_func.const _ c) (simple_func.const _ 0))) (h_add : βˆ€ ⦃f g : simple_func Ξ± γ⦄, disjoint (support f) (support g) β†’ P f β†’ P g β†’ P (f + g)) (f : simple_func Ξ± Ξ³) : P f := begin generalize' h : f.range \ {0} = s, rw [← finset.coe_inj, finset.coe_sdiff, finset.coe_singleton, simple_func.coe_range] at h, revert s f h, refine finset.induction _ _, { intros f hf, rw [finset.coe_empty, diff_eq_empty, range_subset_singleton] at hf, convert h_ind 0 measurable_set.univ, ext x, simp [hf] }, { intros x s hxs ih f hf, have mx := f.measurable_set_preimage {x}, let g := simple_func.piecewise (f ⁻¹' {x}) mx 0 f, have Pg : P g, { apply ih, simp only [g, simple_func.coe_piecewise, range_piecewise], rw [image_compl_preimage, union_diff_distrib, diff_diff_comm, hf, finset.coe_insert, insert_diff_self_of_not_mem, diff_eq_empty.mpr, set.empty_union], { rw [set.image_subset_iff], convert set.subset_univ _, exact preimage_const_of_mem (mem_singleton _) }, { rwa [finset.mem_coe] }}, convert h_add _ Pg (h_ind x mx), { ext1 y, by_cases hy : y ∈ f ⁻¹' {x}; [simpa [hy], simp [hy]] }, rintro y, by_cases hy : y ∈ f ⁻¹' {x}; simp [hy] } end end simple_func section lintegral open simple_func variables [measurable_space Ξ±] {ΞΌ : measure Ξ±} /-- The lower Lebesgue integral of a function `f` with respect to a measure `ΞΌ`. -/ def lintegral (ΞΌ : measure Ξ±) (f : Ξ± β†’ ℝβ‰₯0∞) : ℝβ‰₯0∞ := ⨆ (g : Ξ± β†’β‚› ℝβ‰₯0∞) (hf : ⇑g ≀ f), g.lintegral ΞΌ /-! In the notation for integrals, an expression like `∫⁻ x, g βˆ₯xβˆ₯ βˆ‚ΞΌ` will not be parsed correctly, and needs parentheses. We do not set the binding power of `r` to `0`, because then `∫⁻ x, f x = 0` will be parsed incorrectly. -/ notation `∫⁻` binders `, ` r:(scoped:60 f, f) ` βˆ‚` ΞΌ:70 := lintegral ΞΌ r notation `∫⁻` binders `, ` r:(scoped:60 f, lintegral volume f) := r notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, f) ` βˆ‚` ΞΌ:70 := lintegral (measure.restrict ΞΌ s) r notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, lintegral (measure.restrict volume s) f) := r theorem simple_func.lintegral_eq_lintegral (f : Ξ± β†’β‚› ℝβ‰₯0∞) (ΞΌ : measure Ξ±) : ∫⁻ a, f a βˆ‚ ΞΌ = f.lintegral ΞΌ := le_antisymm (bsupr_le $ Ξ» g hg, lintegral_mono hg $ le_refl _) (le_supr_of_le f $ le_supr_of_le (le_refl _) (le_refl _)) @[mono] lemma lintegral_mono' ⦃μ Ξ½ : measure α⦄ (hΞΌΞ½ : ΞΌ ≀ Ξ½) ⦃f g : Ξ± β†’ ℝβ‰₯0βˆžβ¦„ (hfg : f ≀ g) : ∫⁻ a, f a βˆ‚ΞΌ ≀ ∫⁻ a, g a βˆ‚Ξ½ := supr_le_supr $ Ξ» Ο†, supr_le_supr2 $ Ξ» hΟ†, ⟨le_trans hΟ† hfg, lintegral_mono (le_refl Ο†) hμν⟩ lemma lintegral_mono ⦃f g : Ξ± β†’ ℝβ‰₯0βˆžβ¦„ (hfg : f ≀ g) : ∫⁻ a, f a βˆ‚ΞΌ ≀ ∫⁻ a, g a βˆ‚ΞΌ := lintegral_mono' (le_refl ΞΌ) hfg lemma lintegral_mono_nnreal {f g : Ξ± β†’ ℝβ‰₯0} (h : f ≀ g) : ∫⁻ a, f a βˆ‚ΞΌ ≀ ∫⁻ a, g a βˆ‚ΞΌ := begin refine lintegral_mono _, intro a, rw ennreal.coe_le_coe, exact h a, end lemma monotone_lintegral (ΞΌ : measure Ξ±) : monotone (lintegral ΞΌ) := lintegral_mono @[simp] lemma lintegral_const (c : ℝβ‰₯0∞) : ∫⁻ a, c βˆ‚ΞΌ = c * ΞΌ univ := by rw [← simple_func.const_lintegral, ← simple_func.lintegral_eq_lintegral, simple_func.coe_const] @[simp] lemma lintegral_one : ∫⁻ a, (1 : ℝβ‰₯0∞) βˆ‚ΞΌ = ΞΌ univ := by rw [lintegral_const, one_mul] lemma set_lintegral_const (s : set Ξ±) (c : ℝβ‰₯0∞) : ∫⁻ a in s, c βˆ‚ΞΌ = c * ΞΌ s := by rw [lintegral_const, measure.restrict_apply_univ] lemma set_lintegral_one (s) : ∫⁻ a in s, 1 βˆ‚ΞΌ = ΞΌ s := by rw [set_lintegral_const, one_mul] /-- `∫⁻ a in s, f a βˆ‚ΞΌ` is defined as the supremum of integrals of simple functions `Ο† : Ξ± β†’β‚› ℝβ‰₯0∞` such that `Ο† ≀ f`. This lemma says that it suffices to take functions `Ο† : Ξ± β†’β‚› ℝβ‰₯0`. -/ lemma lintegral_eq_nnreal (f : Ξ± β†’ ℝβ‰₯0∞) (ΞΌ : measure Ξ±) : (∫⁻ a, f a βˆ‚ΞΌ) = (⨆ (Ο† : Ξ± β†’β‚› ℝβ‰₯0) (hf : βˆ€ x, ↑(Ο† x) ≀ f x), (Ο†.map (coe : ℝβ‰₯0 β†’ ℝβ‰₯0∞)).lintegral ΞΌ) := begin refine le_antisymm (bsupr_le $ assume Ο† hΟ†, _) (supr_le_supr2 $ Ξ» Ο†, βŸ¨Ο†.map (coe : ℝβ‰₯0 β†’ ℝβ‰₯0∞), le_refl _⟩), by_cases h : βˆ€α΅ a βˆ‚ΞΌ, Ο† a β‰  ∞, { let ψ := Ο†.map ennreal.to_nnreal, replace h : ψ.map (coe : ℝβ‰₯0 β†’ ℝβ‰₯0∞) =ᡐ[ΞΌ] Ο† := h.mono (Ξ» a, ennreal.coe_to_nnreal), have : βˆ€ x, ↑(ψ x) ≀ f x := Ξ» x, le_trans ennreal.coe_to_nnreal_le_self (hΟ† x), exact le_supr_of_le (Ο†.map ennreal.to_nnreal) (le_supr_of_le this (ge_of_eq $ lintegral_congr h)) }, { have h_meas : ΞΌ (Ο† ⁻¹' {∞}) β‰  0, from mt measure_zero_iff_ae_nmem.1 h, refine le_trans le_top (ge_of_eq $ (supr_eq_top _).2 $ Ξ» b hb, _), obtain ⟨n, hn⟩ : βˆƒ n : β„•, b < n * ΞΌ (Ο† ⁻¹' {∞}), from exists_nat_mul_gt h_meas (ne_of_lt hb), use (const Ξ± (n : ℝβ‰₯0)).restrict (Ο† ⁻¹' {∞}), simp only [lt_supr_iff, exists_prop, coe_restrict, Ο†.measurable_set_preimage, coe_const, ennreal.coe_indicator, map_coe_ennreal_restrict, map_const, ennreal.coe_nat, restrict_const_lintegral], refine ⟨indicator_le (Ξ» x hx, le_trans _ (hΟ† _)), hn⟩, simp only [mem_preimage, mem_singleton_iff] at hx, simp only [hx, le_top] } end lemma exists_simple_func_forall_lintegral_sub_lt_of_pos {f : Ξ± β†’ ℝβ‰₯0∞} (h : ∫⁻ x, f x βˆ‚ΞΌ < ∞) {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : 0 < Ξ΅) : βˆƒ Ο† : Ξ± β†’β‚› ℝβ‰₯0, (βˆ€ x, ↑(Ο† x) ≀ f x) ∧ βˆ€ ψ : Ξ± β†’β‚› ℝβ‰₯0, (βˆ€ x, ↑(ψ x) ≀ f x) β†’ (map coe (ψ - Ο†)).lintegral ΞΌ < Ξ΅ := begin rw lintegral_eq_nnreal at h, have := ennreal.lt_add_right h hΞ΅, erw ennreal.bsupr_add at this; [skip, exact ⟨0, Ξ» x, by simp⟩], simp_rw [lt_supr_iff, supr_lt_iff, supr_le_iff] at this, rcases this with βŸ¨Ο†, hle : βˆ€ x, ↑(Ο† x) ≀ f x, b, hbΟ†, hb⟩, refine βŸ¨Ο†, hle, Ξ» ψ hψ, _⟩, have : (map coe Ο†).lintegral ΞΌ < ∞, from (le_bsupr Ο† hle).trans_lt h, rw [← add_lt_add_iff_left this, ← add_lintegral, ← map_add @ennreal.coe_add], refine (hb _ (Ξ» x, le_trans _ (max_le (hle x) (hψ x)))).trans_lt hbΟ†, norm_cast, simp only [add_apply, sub_apply, nnreal.add_sub_eq_max] end theorem supr_lintegral_le {ΞΉ : Sort*} (f : ΞΉ β†’ Ξ± β†’ ℝβ‰₯0∞) : (⨆i, ∫⁻ a, f i a βˆ‚ΞΌ) ≀ (∫⁻ a, ⨆i, f i a βˆ‚ΞΌ) := begin simp only [← supr_apply], exact (monotone_lintegral ΞΌ).le_map_supr end theorem supr2_lintegral_le {ΞΉ : Sort*} {ΞΉ' : ΞΉ β†’ Sort*} (f : Ξ  i, ΞΉ' i β†’ Ξ± β†’ ℝβ‰₯0∞) : (⨆i (h : ΞΉ' i), ∫⁻ a, f i h a βˆ‚ΞΌ) ≀ (∫⁻ a, ⨆i (h : ΞΉ' i), f i h a βˆ‚ΞΌ) := by { convert (monotone_lintegral ΞΌ).le_map_supr2 f, ext1 a, simp only [supr_apply] } theorem le_infi_lintegral {ΞΉ : Sort*} (f : ΞΉ β†’ Ξ± β†’ ℝβ‰₯0∞) : (∫⁻ a, β¨…i, f i a βˆ‚ΞΌ) ≀ (β¨…i, ∫⁻ a, f i a βˆ‚ΞΌ) := by { simp only [← infi_apply], exact (monotone_lintegral ΞΌ).map_infi_le } theorem le_infi2_lintegral {ΞΉ : Sort*} {ΞΉ' : ΞΉ β†’ Sort*} (f : Ξ  i, ΞΉ' i β†’ Ξ± β†’ ℝβ‰₯0∞) : (∫⁻ a, β¨… i (h : ΞΉ' i), f i h a βˆ‚ΞΌ) ≀ (β¨… i (h : ΞΉ' i), ∫⁻ a, f i h a βˆ‚ΞΌ) := by { convert (monotone_lintegral ΞΌ).map_infi2_le f, ext1 a, simp only [infi_apply] } lemma lintegral_mono_ae {f g : Ξ± β†’ ℝβ‰₯0∞} (h : βˆ€α΅ a βˆ‚ΞΌ, f a ≀ g a) : (∫⁻ a, f a βˆ‚ΞΌ) ≀ (∫⁻ a, g a βˆ‚ΞΌ) := begin rcases exists_measurable_superset_of_null h with ⟨t, hts, ht, ht0⟩, have : βˆ€α΅ x βˆ‚ΞΌ, x βˆ‰ t := measure_zero_iff_ae_nmem.1 ht0, refine (supr_le $ assume s, supr_le $ assume hfs, le_supr_of_le (s.restrict tᢜ) $ le_supr_of_le _ _), { assume a, by_cases a ∈ t; simp [h, restrict_apply, ht.compl], exact le_trans (hfs a) (by_contradiction $ assume hnfg, h (hts hnfg)) }, { refine le_of_eq (simple_func.lintegral_congr $ this.mono $ Ξ» a hnt, _), by_cases hat : a ∈ t; simp [hat, ht.compl], exact (hnt hat).elim } end lemma lintegral_congr_ae {f g : Ξ± β†’ ℝβ‰₯0∞} (h : f =ᡐ[ΞΌ] g) : (∫⁻ a, f a βˆ‚ΞΌ) = (∫⁻ a, g a βˆ‚ΞΌ) := le_antisymm (lintegral_mono_ae $ h.le) (lintegral_mono_ae $ h.symm.le) lemma lintegral_congr {f g : Ξ± β†’ ℝβ‰₯0∞} (h : βˆ€ a, f a = g a) : (∫⁻ a, f a βˆ‚ΞΌ) = (∫⁻ a, g a βˆ‚ΞΌ) := by simp only [h] lemma set_lintegral_congr {f : Ξ± β†’ ℝβ‰₯0∞} {s t : set Ξ±} (h : s =ᡐ[ΞΌ] t) : ∫⁻ x in s, f x βˆ‚ΞΌ = ∫⁻ x in t, f x βˆ‚ΞΌ := by rw [restrict_congr_set h] /-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. See `lintegral_supr_directed` for a more general form. -/ theorem lintegral_supr {f : β„• β†’ Ξ± β†’ ℝβ‰₯0∞} (hf : βˆ€n, measurable (f n)) (h_mono : monotone f) : (∫⁻ a, ⨆n, f n a βˆ‚ΞΌ) = (⨆n, ∫⁻ a, f n a βˆ‚ΞΌ) := begin set c : ℝβ‰₯0 β†’ ℝβ‰₯0∞ := coe, set F := Ξ» a:Ξ±, ⨆n, f n a, have hF : measurable F := measurable_supr hf, refine le_antisymm _ (supr_lintegral_le _), rw [lintegral_eq_nnreal], refine supr_le (assume s, supr_le (assume hsf, _)), refine ennreal.le_of_forall_lt_one_mul_le (assume a ha, _), rcases ennreal.lt_iff_exists_coe.1 ha with ⟨r, rfl, ha⟩, have ha : r < 1 := ennreal.coe_lt_coe.1 ha, let rs := s.map (Ξ»a, r * a), have eq_rs : (const Ξ± r : Ξ± β†’β‚› ℝβ‰₯0∞) * map c s = rs.map c, { ext1 a, exact ennreal.coe_mul.symm }, have eq : βˆ€p, (rs.map c) ⁻¹' {p} = (⋃n, (rs.map c) ⁻¹' {p} ∩ {a | p ≀ f n a}), { assume p, rw [← inter_Union, ← inter_univ ((map c rs) ⁻¹' {p})] {occs := occurrences.pos [1]}, refine set.ext (assume x, and_congr_right $ assume hx, (true_iff _).2 _), by_cases p_eq : p = 0, { simp [p_eq] }, simp at hx, subst hx, have : r * s x β‰  0, { rwa [(β‰ ), ← ennreal.coe_eq_zero] }, have : s x β‰  0, { refine mt _ this, assume h, rw [h, mul_zero] }, have : (rs.map c) x < ⨆ (n : β„•), f n x, { refine lt_of_lt_of_le (ennreal.coe_lt_coe.2 (_)) (hsf x), suffices : r * s x < 1 * s x, simpa [rs], exact mul_lt_mul_of_pos_right ha (pos_iff_ne_zero.2 this) }, rcases lt_supr_iff.1 this with ⟨i, hi⟩, exact mem_Union.2 ⟨i, le_of_lt hi⟩ }, have mono : βˆ€r:ℝβ‰₯0∞, monotone (Ξ»n, (rs.map c) ⁻¹' {r} ∩ {a | r ≀ f n a}), { assume r i j h, refine inter_subset_inter (subset.refl _) _, assume x hx, exact le_trans hx (h_mono h x) }, have h_meas : βˆ€n, measurable_set {a : Ξ± | ⇑(map c rs) a ≀ f n a} := assume n, measurable_set_le (simple_func.measurable _) (hf n), calc (r:ℝβ‰₯0∞) * (s.map c).lintegral ΞΌ = βˆ‘ r in (rs.map c).range, r * ΞΌ ((rs.map c) ⁻¹' {r}) : by rw [← const_mul_lintegral, eq_rs, simple_func.lintegral] ... ≀ βˆ‘ r in (rs.map c).range, r * ΞΌ (⋃n, (rs.map c) ⁻¹' {r} ∩ {a | r ≀ f n a}) : le_of_eq (finset.sum_congr rfl $ assume x hx, by rw ← eq) ... ≀ βˆ‘ r in (rs.map c).range, (⨆n, r * ΞΌ ((rs.map c) ⁻¹' {r} ∩ {a | r ≀ f n a})) : le_of_eq (finset.sum_congr rfl $ assume x hx, begin rw [measure_Union_eq_supr _ (directed_of_sup $ mono x), ennreal.mul_supr], { assume i, refine ((rs.map c).measurable_set_preimage _).inter _, exact hf i measurable_set_Ici } end) ... ≀ ⨆n, βˆ‘ r in (rs.map c).range, r * ΞΌ ((rs.map c) ⁻¹' {r} ∩ {a | r ≀ f n a}) : begin refine le_of_eq _, rw [ennreal.finset_sum_supr_nat], assume p i j h, exact canonically_ordered_semiring.mul_le_mul (le_refl _) (measure_mono $ mono p h) end ... ≀ (⨆n:β„•, ((rs.map c).restrict {a | (rs.map c) a ≀ f n a}).lintegral ΞΌ) : begin refine supr_le_supr (assume n, _), rw [restrict_lintegral _ (h_meas n)], { refine le_of_eq (finset.sum_congr rfl $ assume r hr, _), congr' 2 with a, refine and_congr_right _, simp {contextual := tt} } end ... ≀ (⨆n, ∫⁻ a, f n a βˆ‚ΞΌ) : begin refine supr_le_supr (assume n, _), rw [← simple_func.lintegral_eq_lintegral], refine lintegral_mono (assume a, _), simp only [map_apply] at h_meas, simp only [coe_map, restrict_apply _ (h_meas _), (∘)], exact indicator_apply_le id, end end /-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. Version with ae_measurable functions. -/ theorem lintegral_supr' {f : β„• β†’ Ξ± β†’ ℝβ‰₯0∞} (hf : βˆ€n, ae_measurable (f n) ΞΌ) (h_mono : βˆ€α΅ x βˆ‚ΞΌ, monotone (Ξ» n, f n x)) : (∫⁻ a, ⨆n, f n a βˆ‚ΞΌ) = (⨆n, ∫⁻ a, f n a βˆ‚ΞΌ) := begin simp_rw ←supr_apply, let p : Ξ± β†’ (β„• β†’ ℝβ‰₯0∞) β†’ Prop := Ξ» x f', monotone f', have hp : βˆ€α΅ x βˆ‚ΞΌ, p x (Ξ» i, f i x), from h_mono, have h_ae_seq_mono : monotone (ae_seq hf p), { intros n m hnm x, by_cases hx : x ∈ ae_seq_set hf p, { exact ae_seq.prop_of_mem_ae_seq_set hf hx hnm, }, { simp only [ae_seq, hx, if_false], exact le_refl _, }, }, rw lintegral_congr_ae (ae_seq.supr hf hp).symm, simp_rw supr_apply, rw @lintegral_supr _ _ ΞΌ _ (ae_seq.measurable hf p) h_ae_seq_mono, congr, exact funext (Ξ» n, lintegral_congr_ae (ae_seq.ae_seq_n_eq_fun_n_ae hf hp n)), end lemma lintegral_eq_supr_eapprox_lintegral {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : (∫⁻ a, f a βˆ‚ΞΌ) = (⨆n, (eapprox f n).lintegral ΞΌ) := calc (∫⁻ a, f a βˆ‚ΞΌ) = (∫⁻ a, ⨆n, (eapprox f n : Ξ± β†’ ℝβ‰₯0∞) a βˆ‚ΞΌ) : by congr; ext a; rw [supr_eapprox_apply f hf] ... = (⨆n, ∫⁻ a, (eapprox f n : Ξ± β†’ ℝβ‰₯0∞) a βˆ‚ΞΌ) : begin rw [lintegral_supr], { assume n, exact (eapprox f n).measurable }, { assume i j h, exact (monotone_eapprox f h) } end ... = (⨆n, (eapprox f n).lintegral ΞΌ) : by congr; ext n; rw [(eapprox f n).lintegral_eq_lintegral] /-- If `f` has finite integral, then `∫⁻ x in s, f x βˆ‚ΞΌ` is absolutely continuous in `s`: it tends to zero as `ΞΌ s` tends to zero. This lemma states states this fact in terms of `Ξ΅` and `Ξ΄`. -/ lemma exists_pos_set_lintegral_lt_of_measure_lt {f : Ξ± β†’ ℝβ‰₯0∞} (h : ∫⁻ x, f x βˆ‚ΞΌ < ∞) {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : 0 < Ξ΅) : βˆƒ Ξ΄ > 0, βˆ€ s, ΞΌ s < Ξ΄ β†’ ∫⁻ x in s, f x βˆ‚ΞΌ < Ξ΅ := begin rcases exists_between hΞ΅ with βŸ¨Ξ΅β‚‚, hΞ΅β‚‚0, hΞ΅β‚‚Ξ΅βŸ©, rcases exists_between hΞ΅β‚‚0 with βŸ¨Ξ΅β‚, hΡ₁0, hΞ΅β‚β‚‚βŸ©, rcases exists_simple_func_forall_lintegral_sub_lt_of_pos h hΡ₁0 with βŸ¨Ο†, hle, hΟ†βŸ©, rcases Ο†.exists_forall_le with ⟨C, hC⟩, use [(Ξ΅β‚‚ - Ρ₁) / C, ennreal.div_pos_iff.2 ⟨(zero_lt_sub_iff_lt.2 hΡ₁₂).ne', ennreal.coe_ne_top⟩], intros s hs, simp only [lintegral_eq_nnreal, supr_lt_iff, supr_le_iff], refine βŸ¨Ξ΅β‚‚, hΞ΅β‚‚Ξ΅, Ξ» ψ hψ, _⟩, calc (map coe ψ).lintegral (ΞΌ.restrict s) ≀ (map coe Ο†).lintegral (ΞΌ.restrict s) + (map coe (ψ - Ο†)).lintegral (ΞΌ.restrict s) : begin rw [← simple_func.add_lintegral, ← simple_func.map_add @ennreal.coe_add], refine simple_func.lintegral_mono (Ξ» x, _) le_rfl, simp [-ennreal.coe_add, nnreal.add_sub_eq_max, le_max_right] end ... ≀ (map coe Ο†).lintegral (ΞΌ.restrict s) + Ρ₁ : begin refine add_le_add le_rfl (le_trans _ (hΟ† _ hψ).le), exact simple_func.lintegral_mono le_rfl measure.restrict_le_self end ... ≀ (simple_func.const Ξ± (C : ℝβ‰₯0∞)).lintegral (ΞΌ.restrict s) + Ρ₁ : by { mono*, exacts [Ξ» x, coe_le_coe.2 (hC x), le_rfl, le_rfl] } ... = C * ΞΌ s + Ρ₁ : by simp [← simple_func.lintegral_eq_lintegral] ... ≀ C * ((Ξ΅β‚‚ - Ρ₁) / C) + Ρ₁ : by { mono*, exacts [le_rfl, hs.le, le_rfl] } ... ≀ (Ξ΅β‚‚ - Ρ₁) + Ρ₁ : add_le_add mul_div_le le_rfl ... = Ξ΅β‚‚ : sub_add_cancel_of_le hΡ₁₂.le, end /-- If `f` has finite integral, then `∫⁻ x in s, f x βˆ‚ΞΌ` is absolutely continuous in `s`: it tends to zero as `ΞΌ s` tends to zero. -/ lemma tendsto_set_lintegral_zero {ΞΉ} {f : Ξ± β†’ ℝβ‰₯0∞} (h : ∫⁻ x, f x βˆ‚ΞΌ < ∞) {l : filter ΞΉ} {s : ΞΉ β†’ set Ξ±} (hl : tendsto (ΞΌ ∘ s) l (𝓝 0)) : tendsto (Ξ» i, ∫⁻ x in s i, f x βˆ‚ΞΌ) l (𝓝 0) := begin simp only [ennreal.nhds_zero, tendsto_infi, tendsto_principal, mem_Iio, ← pos_iff_ne_zero] at hl ⊒, intros Ξ΅ Ξ΅0, rcases exists_pos_set_lintegral_lt_of_measure_lt h Ξ΅0 with ⟨δ, Ξ΄0, hδ⟩, exact (hl Ξ΄ Ξ΄0).mono (Ξ» i, hΞ΄ _) end @[simp] lemma lintegral_add {f g : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) (hg : measurable g) : (∫⁻ a, f a + g a βˆ‚ΞΌ) = (∫⁻ a, f a βˆ‚ΞΌ) + (∫⁻ a, g a βˆ‚ΞΌ) := calc (∫⁻ a, f a + g a βˆ‚ΞΌ) = (∫⁻ a, (⨆n, (eapprox f n : Ξ± β†’ ℝβ‰₯0∞) a) + (⨆n, (eapprox g n : Ξ± β†’ ℝβ‰₯0∞) a) βˆ‚ΞΌ) : by simp only [supr_eapprox_apply, hf, hg] ... = (∫⁻ a, (⨆n, (eapprox f n + eapprox g n : Ξ± β†’ ℝβ‰₯0∞) a) βˆ‚ΞΌ) : begin congr, funext a, rw [ennreal.supr_add_supr_of_monotone], { refl }, { assume i j h, exact monotone_eapprox _ h a }, { assume i j h, exact monotone_eapprox _ h a }, end ... = (⨆n, (eapprox f n).lintegral ΞΌ + (eapprox g n).lintegral ΞΌ) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.add_lintegral, ← simple_func.lintegral_eq_lintegral], refl }, { assume n, exact measurable.add (eapprox f n).measurable (eapprox g n).measurable }, { assume i j h a, exact add_le_add (monotone_eapprox _ h _) (monotone_eapprox _ h _) } end ... = (⨆n, (eapprox f n).lintegral ΞΌ) + (⨆n, (eapprox g n).lintegral ΞΌ) : by refine (ennreal.supr_add_supr_of_monotone _ _).symm; { assume i j h, exact simple_func.lintegral_mono (monotone_eapprox _ h) (le_refl ΞΌ) } ... = (∫⁻ a, f a βˆ‚ΞΌ) + (∫⁻ a, g a βˆ‚ΞΌ) : by rw [lintegral_eq_supr_eapprox_lintegral hf, lintegral_eq_supr_eapprox_lintegral hg] lemma lintegral_add' {f g : Ξ± β†’ ℝβ‰₯0∞} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : (∫⁻ a, f a + g a βˆ‚ΞΌ) = (∫⁻ a, f a βˆ‚ΞΌ) + (∫⁻ a, g a βˆ‚ΞΌ) := calc (∫⁻ a, f a + g a βˆ‚ΞΌ) = (∫⁻ a, hf.mk f a + hg.mk g a βˆ‚ΞΌ) : lintegral_congr_ae (eventually_eq.add hf.ae_eq_mk hg.ae_eq_mk) ... = (∫⁻ a, hf.mk f a βˆ‚ΞΌ) + (∫⁻ a, hg.mk g a βˆ‚ΞΌ) : lintegral_add hf.measurable_mk hg.measurable_mk ... = (∫⁻ a, f a βˆ‚ΞΌ) + (∫⁻ a, g a βˆ‚ΞΌ) : begin congr' 1, { exact lintegral_congr_ae hf.ae_eq_mk.symm }, { exact lintegral_congr_ae hg.ae_eq_mk.symm }, end lemma lintegral_zero : (∫⁻ a:Ξ±, 0 βˆ‚ΞΌ) = 0 := by simp lemma lintegral_zero_fun : (∫⁻ a:Ξ±, (0 : Ξ± β†’ ℝβ‰₯0∞) a βˆ‚ΞΌ) = 0 := by simp @[simp] lemma lintegral_smul_measure (c : ℝβ‰₯0∞) (f : Ξ± β†’ ℝβ‰₯0∞) : ∫⁻ a, f a βˆ‚ (c β€’ ΞΌ) = c * ∫⁻ a, f a βˆ‚ΞΌ := by simp only [lintegral, supr_subtype', simple_func.lintegral_smul, ennreal.mul_supr, smul_eq_mul] @[simp] lemma lintegral_sum_measure {ΞΉ} (f : Ξ± β†’ ℝβ‰₯0∞) (ΞΌ : ΞΉ β†’ measure Ξ±) : ∫⁻ a, f a βˆ‚(measure.sum ΞΌ) = βˆ‘' i, ∫⁻ a, f a βˆ‚(ΞΌ i) := begin simp only [lintegral, supr_subtype', simple_func.lintegral_sum, ennreal.tsum_eq_supr_sum], rw [supr_comm], congr, funext s, induction s using finset.induction_on with i s hi hs, { apply bot_unique, simp }, simp only [finset.sum_insert hi, ← hs], refine (ennreal.supr_add_supr _).symm, intros Ο† ψ, exact βŸ¨βŸ¨Ο† βŠ” ψ, Ξ» x, sup_le (Ο†.2 x) (ψ.2 x)⟩, add_le_add (simple_func.lintegral_mono le_sup_left (le_refl _)) (finset.sum_le_sum $ Ξ» j hj, simple_func.lintegral_mono le_sup_right (le_refl _))⟩ end @[simp] lemma lintegral_add_measure (f : Ξ± β†’ ℝβ‰₯0∞) (ΞΌ Ξ½ : measure Ξ±) : ∫⁻ a, f a βˆ‚ (ΞΌ + Ξ½) = ∫⁻ a, f a βˆ‚ΞΌ + ∫⁻ a, f a βˆ‚Ξ½ := by simpa [tsum_fintype] using lintegral_sum_measure f (Ξ» b, cond b ΞΌ Ξ½) @[simp] lemma lintegral_zero_measure (f : Ξ± β†’ ℝβ‰₯0∞) : ∫⁻ a, f a βˆ‚0 = 0 := bot_unique $ by simp [lintegral] lemma lintegral_finset_sum (s : finset Ξ²) {f : Ξ² β†’ Ξ± β†’ ℝβ‰₯0∞} (hf : βˆ€ b ∈ s, measurable (f b)) : (∫⁻ a, βˆ‘ b in s, f b a βˆ‚ΞΌ) = βˆ‘ b in s, ∫⁻ a, f b a βˆ‚ΞΌ := begin induction s using finset.induction_on with a s has ih, { simp }, { simp only [finset.sum_insert has], rw [finset.forall_mem_insert] at hf, rw [lintegral_add hf.1 (s.measurable_sum hf.2), ih hf.2] } end @[simp] lemma lintegral_const_mul (r : ℝβ‰₯0∞) {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : (∫⁻ a, r * f a βˆ‚ΞΌ) = r * (∫⁻ a, f a βˆ‚ΞΌ) := calc (∫⁻ a, r * f a βˆ‚ΞΌ) = (∫⁻ a, (⨆n, (const Ξ± r * eapprox f n) a) βˆ‚ΞΌ) : by { congr, funext a, rw [← supr_eapprox_apply f hf, ennreal.mul_supr], refl } ... = (⨆n, r * (eapprox f n).lintegral ΞΌ) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.const_mul_lintegral, ← simple_func.lintegral_eq_lintegral] }, { assume n, exact simple_func.measurable _ }, { assume i j h a, exact canonically_ordered_semiring.mul_le_mul (le_refl _) (monotone_eapprox _ h _) } end ... = r * (∫⁻ a, f a βˆ‚ΞΌ) : by rw [← ennreal.mul_supr, lintegral_eq_supr_eapprox_lintegral hf] lemma lintegral_const_mul'' (r : ℝβ‰₯0∞) {f : Ξ± β†’ ℝβ‰₯0∞} (hf : ae_measurable f ΞΌ) : (∫⁻ a, r * f a βˆ‚ΞΌ) = r * (∫⁻ a, f a βˆ‚ΞΌ) := begin have A : ∫⁻ a, f a βˆ‚ΞΌ = ∫⁻ a, hf.mk f a βˆ‚ΞΌ := lintegral_congr_ae hf.ae_eq_mk, have B : ∫⁻ a, r * f a βˆ‚ΞΌ = ∫⁻ a, r * hf.mk f a βˆ‚ΞΌ := lintegral_congr_ae (eventually_eq.fun_comp hf.ae_eq_mk _), rw [A, B, lintegral_const_mul _ hf.measurable_mk], end lemma lintegral_const_mul_le (r : ℝβ‰₯0∞) (f : Ξ± β†’ ℝβ‰₯0∞) : r * (∫⁻ a, f a βˆ‚ΞΌ) ≀ (∫⁻ a, r * f a βˆ‚ΞΌ) := begin rw [lintegral, ennreal.mul_supr], refine supr_le (Ξ»s, _), rw [ennreal.mul_supr], simp only [supr_le_iff, ge_iff_le], assume hs, rw ← simple_func.const_mul_lintegral, refine le_supr_of_le (const Ξ± r * s) (le_supr_of_le (Ξ»x, _) (le_refl _)), exact canonically_ordered_semiring.mul_le_mul (le_refl _) (hs x) end lemma lintegral_const_mul' (r : ℝβ‰₯0∞) (f : Ξ± β†’ ℝβ‰₯0∞) (hr : r β‰  ∞) : (∫⁻ a, r * f a βˆ‚ΞΌ) = r * (∫⁻ a, f a βˆ‚ΞΌ) := begin by_cases h : r = 0, { simp [h] }, apply le_antisymm _ (lintegral_const_mul_le r f), have rinv : r * r⁻¹ = 1 := ennreal.mul_inv_cancel h hr, have rinv' : r ⁻¹ * r = 1, by { rw mul_comm, exact rinv }, have := lintegral_const_mul_le (r⁻¹) (Ξ»x, r * f x), simp [(mul_assoc _ _ _).symm, rinv'] at this, simpa [(mul_assoc _ _ _).symm, rinv] using canonically_ordered_semiring.mul_le_mul (le_refl r) this end lemma lintegral_mul_const (r : ℝβ‰₯0∞) {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : ∫⁻ a, f a * r βˆ‚ΞΌ = ∫⁻ a, f a βˆ‚ΞΌ * r := by simp_rw [mul_comm, lintegral_const_mul r hf] lemma lintegral_mul_const'' (r : ℝβ‰₯0∞) {f : Ξ± β†’ ℝβ‰₯0∞} (hf : ae_measurable f ΞΌ) : ∫⁻ a, f a * r βˆ‚ΞΌ = ∫⁻ a, f a βˆ‚ΞΌ * r := by simp_rw [mul_comm, lintegral_const_mul'' r hf] lemma lintegral_mul_const_le (r : ℝβ‰₯0∞) (f : Ξ± β†’ ℝβ‰₯0∞) : ∫⁻ a, f a βˆ‚ΞΌ * r ≀ ∫⁻ a, f a * r βˆ‚ΞΌ := by simp_rw [mul_comm, lintegral_const_mul_le r f] lemma lintegral_mul_const' (r : ℝβ‰₯0∞) (f : Ξ± β†’ ℝβ‰₯0∞) (hr : r β‰  ∞): ∫⁻ a, f a * r βˆ‚ΞΌ = ∫⁻ a, f a βˆ‚ΞΌ * r := by simp_rw [mul_comm, lintegral_const_mul' r f hr] /- A double integral of a product where each factor contains only one variable is a product of integrals -/ lemma lintegral_lintegral_mul {Ξ²} [measurable_space Ξ²] {Ξ½ : measure Ξ²} {f : Ξ± β†’ ℝβ‰₯0∞} {g : Ξ² β†’ ℝβ‰₯0∞} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g Ξ½) : ∫⁻ x, ∫⁻ y, f x * g y βˆ‚Ξ½ βˆ‚ΞΌ = ∫⁻ x, f x βˆ‚ΞΌ * ∫⁻ y, g y βˆ‚Ξ½ := by simp [lintegral_const_mul'' _ hg, lintegral_mul_const'' _ hf] -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rw₁ {f f' : Ξ± β†’ Ξ²} (h : f =ᡐ[ΞΌ] f') (g : Ξ² β†’ ℝβ‰₯0∞) : (∫⁻ a, g (f a) βˆ‚ΞΌ) = (∫⁻ a, g (f' a) βˆ‚ΞΌ) := lintegral_congr_ae $ h.mono $ Ξ» a h, by rw h -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rwβ‚‚ {f₁ f₁' : Ξ± β†’ Ξ²} {fβ‚‚ fβ‚‚' : Ξ± β†’ Ξ³} (h₁ : f₁ =ᡐ[ΞΌ] f₁') (hβ‚‚ : fβ‚‚ =ᡐ[ΞΌ] fβ‚‚') (g : Ξ² β†’ Ξ³ β†’ ℝβ‰₯0∞) : (∫⁻ a, g (f₁ a) (fβ‚‚ a) βˆ‚ΞΌ) = (∫⁻ a, g (f₁' a) (fβ‚‚' a) βˆ‚ΞΌ) := lintegral_congr_ae $ h₁.mp $ hβ‚‚.mono $ Ξ» _ hβ‚‚ h₁, by rw [h₁, hβ‚‚] @[simp] lemma lintegral_indicator (f : Ξ± β†’ ℝβ‰₯0∞) {s : set Ξ±} (hs : measurable_set s) : ∫⁻ a, s.indicator f a βˆ‚ΞΌ = ∫⁻ a in s, f a βˆ‚ΞΌ := begin simp only [lintegral, ← restrict_lintegral_eq_lintegral_restrict _ hs, supr_subtype'], apply le_antisymm; refine supr_le_supr2 (subtype.forall.2 $ Ξ» Ο† hΟ†, _), { refine βŸ¨βŸ¨Ο†, le_trans hΟ† (indicator_le_self _ _)⟩, _⟩, refine simple_func.lintegral_mono (Ξ» x, _) (le_refl _), by_cases hx : x ∈ s, { simp [hx, hs, le_refl] }, { apply le_trans (hΟ† x), simp [hx, hs, le_refl] } }, { refine βŸ¨βŸ¨Ο†.restrict s, Ξ» x, _⟩, le_refl _⟩, simp [hΟ† x, hs, indicator_le_indicator] } end /-- Chebyshev's inequality -/ lemma mul_meas_ge_le_lintegral {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) (Ξ΅ : ℝβ‰₯0∞) : Ξ΅ * ΞΌ {x | Ξ΅ ≀ f x} ≀ ∫⁻ a, f a βˆ‚ΞΌ := begin have : measurable_set {a : Ξ± | Ξ΅ ≀ f a }, from hf measurable_set_Ici, rw [← simple_func.restrict_const_lintegral _ this, ← simple_func.lintegral_eq_lintegral], refine lintegral_mono (Ξ» a, _), simp only [restrict_apply _ this], exact indicator_apply_le id end lemma meas_ge_le_lintegral_div {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : Ξ΅ β‰  0) (hΞ΅' : Ξ΅ β‰  ∞) : ΞΌ {x | Ξ΅ ≀ f x} ≀ (∫⁻ a, f a βˆ‚ΞΌ) / Ξ΅ := (ennreal.le_div_iff_mul_le (or.inl hΞ΅) (or.inl hΞ΅')).2 $ by { rw [mul_comm], exact mul_meas_ge_le_lintegral hf Ξ΅ } @[simp] lemma lintegral_eq_zero_iff {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : ∫⁻ a, f a βˆ‚ΞΌ = 0 ↔ (f =ᡐ[ΞΌ] 0) := begin refine iff.intro (assume h, _) (assume h, _), { have : βˆ€n:β„•, βˆ€α΅ a βˆ‚ΞΌ, f a < n⁻¹, { assume n, rw [ae_iff, ← nonpos_iff_eq_zero, ← @ennreal.zero_div n⁻¹, ennreal.le_div_iff_mul_le, mul_comm], simp only [not_lt], -- TODO: why `rw ← h` fails with "not an equality or an iff"? exacts [h β–Έ mul_meas_ge_le_lintegral hf n⁻¹, or.inl (ennreal.inv_ne_zero.2 ennreal.coe_nat_ne_top), or.inr ennreal.zero_ne_top] }, refine (ae_all_iff.2 this).mono (Ξ» a ha, _), by_contradiction h, rcases ennreal.exists_inv_nat_lt h with ⟨n, hn⟩, exact (lt_irrefl _ $ lt_trans hn $ ha n).elim }, { calc ∫⁻ a, f a βˆ‚ΞΌ = ∫⁻ a, 0 βˆ‚ΞΌ : lintegral_congr_ae h ... = 0 : lintegral_zero } end @[simp] lemma lintegral_eq_zero_iff' {f : Ξ± β†’ ℝβ‰₯0∞} (hf : ae_measurable f ΞΌ) : ∫⁻ a, f a βˆ‚ΞΌ = 0 ↔ (f =ᡐ[ΞΌ] 0) := begin have : ∫⁻ a, f a βˆ‚ΞΌ = ∫⁻ a, hf.mk f a βˆ‚ΞΌ := lintegral_congr_ae hf.ae_eq_mk, rw [this, lintegral_eq_zero_iff hf.measurable_mk], exact ⟨λ H, hf.ae_eq_mk.trans H, Ξ» H, hf.ae_eq_mk.symm.trans H⟩ end lemma lintegral_pos_iff_support {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : 0 < ∫⁻ a, f a βˆ‚ΞΌ ↔ 0 < ΞΌ (function.support f) := by simp [pos_iff_ne_zero, hf, filter.eventually_eq, ae_iff, function.support] /-- Weaker version of the monotone convergence theorem-/ lemma lintegral_supr_ae {f : β„• β†’ Ξ± β†’ ℝβ‰₯0∞} (hf : βˆ€n, measurable (f n)) (h_mono : βˆ€n, βˆ€α΅ a βˆ‚ΞΌ, f n a ≀ f n.succ a) : (∫⁻ a, ⨆n, f n a βˆ‚ΞΌ) = (⨆n, ∫⁻ a, f n a βˆ‚ΞΌ) := let ⟨s, hs⟩ := exists_measurable_superset_of_null (ae_iff.1 (ae_all_iff.2 h_mono)) in let g := Ξ» n a, if a ∈ s then 0 else f n a in have g_eq_f : βˆ€α΅ a βˆ‚ΞΌ, βˆ€n, g n a = f n a, from (measure_zero_iff_ae_nmem.1 hs.2.2).mono (assume a ha n, if_neg ha), calc ∫⁻ a, ⨆n, f n a βˆ‚ΞΌ = ∫⁻ a, ⨆n, g n a βˆ‚ΞΌ : lintegral_congr_ae $ g_eq_f.mono $ Ξ» a ha, by simp only [ha] ... = ⨆n, (∫⁻ a, g n a βˆ‚ΞΌ) : lintegral_supr (assume n, measurable_const.piecewise hs.2.1 (hf n)) (monotone_of_monotone_nat $ assume n a, classical.by_cases (assume h : a ∈ s, by simp [g, if_pos h]) (assume h : a βˆ‰ s, begin simp only [g, if_neg h], have := hs.1, rw subset_def at this, have := mt (this a) h, simp only [not_not, mem_set_of_eq] at this, exact this n end)) ... = ⨆n, (∫⁻ a, f n a βˆ‚ΞΌ) : by simp only [lintegral_congr_ae (g_eq_f.mono $ Ξ» a ha, ha _)] lemma lintegral_sub {f g : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) (hg : measurable g) (hg_fin : ∫⁻ a, g a βˆ‚ΞΌ < ∞) (h_le : g ≀ᡐ[ΞΌ] f) : ∫⁻ a, f a - g a βˆ‚ΞΌ = ∫⁻ a, f a βˆ‚ΞΌ - ∫⁻ a, g a βˆ‚ΞΌ := begin rw [← ennreal.add_left_inj hg_fin, ennreal.sub_add_cancel_of_le (lintegral_mono_ae h_le), ← lintegral_add (hf.sub hg) hg], refine lintegral_congr_ae (h_le.mono $ Ξ» x hx, _), exact ennreal.sub_add_cancel_of_le hx end /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi_ae {f : β„• β†’ Ξ± β†’ ℝβ‰₯0∞} (h_meas : βˆ€n, measurable (f n)) (h_mono : βˆ€n:β„•, f n.succ ≀ᡐ[ΞΌ] f n) (h_fin : ∫⁻ a, f 0 a βˆ‚ΞΌ < ∞) : ∫⁻ a, β¨…n, f n a βˆ‚ΞΌ = β¨…n, ∫⁻ a, f n a βˆ‚ΞΌ := have fn_le_f0 : ∫⁻ a, β¨…n, f n a βˆ‚ΞΌ ≀ ∫⁻ a, f 0 a βˆ‚ΞΌ, from lintegral_mono (assume a, infi_le_of_le 0 (le_refl _)), have fn_le_f0' : (β¨…n, ∫⁻ a, f n a βˆ‚ΞΌ) ≀ ∫⁻ a, f 0 a βˆ‚ΞΌ, from infi_le_of_le 0 (le_refl _), (ennreal.sub_right_inj h_fin fn_le_f0 fn_le_f0').1 $ show ∫⁻ a, f 0 a βˆ‚ΞΌ - ∫⁻ a, β¨…n, f n a βˆ‚ΞΌ = ∫⁻ a, f 0 a βˆ‚ΞΌ - (β¨…n, ∫⁻ a, f n a βˆ‚ΞΌ), from calc ∫⁻ a, f 0 a βˆ‚ΞΌ - (∫⁻ a, β¨…n, f n a βˆ‚ΞΌ) = ∫⁻ a, f 0 a - β¨…n, f n a βˆ‚ΞΌ: (lintegral_sub (h_meas 0) (measurable_infi h_meas) (calc (∫⁻ a, β¨…n, f n a βˆ‚ΞΌ) ≀ ∫⁻ a, f 0 a βˆ‚ΞΌ : lintegral_mono (assume a, infi_le _ _) ... < ∞ : h_fin ) (ae_of_all _ $ assume a, infi_le _ _)).symm ... = ∫⁻ a, ⨆n, f 0 a - f n a βˆ‚ΞΌ : congr rfl (funext (assume a, ennreal.sub_infi)) ... = ⨆n, ∫⁻ a, f 0 a - f n a βˆ‚ΞΌ : lintegral_supr_ae (assume n, (h_meas 0).sub (h_meas n)) (assume n, (h_mono n).mono $ assume a ha, ennreal.sub_le_sub (le_refl _) ha) ... = ⨆n, ∫⁻ a, f 0 a βˆ‚ΞΌ - ∫⁻ a, f n a βˆ‚ΞΌ : have h_mono : βˆ€α΅ a βˆ‚ΞΌ, βˆ€n:β„•, f n.succ a ≀ f n a := ae_all_iff.2 h_mono, have h_mono : βˆ€n, βˆ€α΅ a βˆ‚ΞΌ, f n a ≀ f 0 a := assume n, h_mono.mono $ assume a h, begin induction n with n ih, {exact le_refl _}, {exact le_trans (h n) ih} end, congr rfl (funext $ assume n, lintegral_sub (h_meas _) (h_meas _) (calc ∫⁻ a, f n a βˆ‚ΞΌ ≀ ∫⁻ a, f 0 a βˆ‚ΞΌ : lintegral_mono_ae $ h_mono n ... < ∞ : h_fin) (h_mono n)) ... = ∫⁻ a, f 0 a βˆ‚ΞΌ - β¨…n, ∫⁻ a, f n a βˆ‚ΞΌ : ennreal.sub_infi.symm /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi {f : β„• β†’ Ξ± β†’ ℝβ‰₯0∞} (h_meas : βˆ€n, measurable (f n)) (h_mono : βˆ€ ⦃m n⦄, m ≀ n β†’ f n ≀ f m) (h_fin : ∫⁻ a, f 0 a βˆ‚ΞΌ < ∞) : ∫⁻ a, β¨…n, f n a βˆ‚ΞΌ = β¨…n, ∫⁻ a, f n a βˆ‚ΞΌ := lintegral_infi_ae h_meas (Ξ» n, ae_of_all _ $ h_mono $ le_of_lt n.lt_succ_self) h_fin /-- Known as Fatou's lemma, version with `ae_measurable` functions -/ lemma lintegral_liminf_le' {f : β„• β†’ Ξ± β†’ ℝβ‰₯0∞} (h_meas : βˆ€n, ae_measurable (f n) ΞΌ) : ∫⁻ a, liminf at_top (Ξ» n, f n a) βˆ‚ΞΌ ≀ liminf at_top (Ξ» n, ∫⁻ a, f n a βˆ‚ΞΌ) := calc ∫⁻ a, liminf at_top (Ξ» n, f n a) βˆ‚ΞΌ = ∫⁻ a, ⨆n:β„•, β¨…iβ‰₯n, f i a βˆ‚ΞΌ : by simp only [liminf_eq_supr_infi_of_nat] ... = ⨆n:β„•, ∫⁻ a, β¨…iβ‰₯n, f i a βˆ‚ΞΌ : lintegral_supr' (assume n, ae_measurable_binfi _ (countable_encodable _) h_meas) (ae_of_all ΞΌ (assume a n m hnm, infi_le_infi_of_subset $ Ξ» i hi, le_trans hnm hi)) ... ≀ ⨆n:β„•, β¨…iβ‰₯n, ∫⁻ a, f i a βˆ‚ΞΌ : supr_le_supr $ Ξ» n, le_infi2_lintegral _ ... = at_top.liminf (Ξ» n, ∫⁻ a, f n a βˆ‚ΞΌ) : filter.liminf_eq_supr_infi_of_nat.symm /-- Known as Fatou's lemma -/ lemma lintegral_liminf_le {f : β„• β†’ Ξ± β†’ ℝβ‰₯0∞} (h_meas : βˆ€n, measurable (f n)) : ∫⁻ a, liminf at_top (Ξ» n, f n a) βˆ‚ΞΌ ≀ liminf at_top (Ξ» n, ∫⁻ a, f n a βˆ‚ΞΌ) := lintegral_liminf_le' (Ξ» n, (h_meas n).ae_measurable) lemma limsup_lintegral_le {f : β„• β†’ Ξ± β†’ ℝβ‰₯0∞} {g : Ξ± β†’ ℝβ‰₯0∞} (hf_meas : βˆ€ n, measurable (f n)) (h_bound : βˆ€n, f n ≀ᡐ[ΞΌ] g) (h_fin : ∫⁻ a, g a βˆ‚ΞΌ < ∞) : limsup at_top (Ξ»n, ∫⁻ a, f n a βˆ‚ΞΌ) ≀ ∫⁻ a, limsup at_top (Ξ»n, f n a) βˆ‚ΞΌ := calc limsup at_top (Ξ»n, ∫⁻ a, f n a βˆ‚ΞΌ) = β¨…n:β„•, ⨆iβ‰₯n, ∫⁻ a, f i a βˆ‚ΞΌ : limsup_eq_infi_supr_of_nat ... ≀ β¨…n:β„•, ∫⁻ a, ⨆iβ‰₯n, f i a βˆ‚ΞΌ : infi_le_infi $ assume n, supr2_lintegral_le _ ... = ∫⁻ a, β¨…n:β„•, ⨆iβ‰₯n, f i a βˆ‚ΞΌ : begin refine (lintegral_infi _ _ _).symm, { assume n, exact measurable_bsupr _ (countable_encodable _) hf_meas }, { assume n m hnm a, exact (supr_le_supr_of_subset $ Ξ» i hi, le_trans hnm hi) }, { refine lt_of_le_of_lt (lintegral_mono_ae _) h_fin, refine (ae_all_iff.2 h_bound).mono (Ξ» n hn, _), exact supr_le (Ξ» i, supr_le $ Ξ» hi, hn i) } end ... = ∫⁻ a, limsup at_top (Ξ»n, f n a) βˆ‚ΞΌ : by simp only [limsup_eq_infi_supr_of_nat] /-- Dominated convergence theorem for nonnegative functions -/ lemma tendsto_lintegral_of_dominated_convergence {F : β„• β†’ Ξ± β†’ ℝβ‰₯0∞} {f : Ξ± β†’ ℝβ‰₯0∞} (bound : Ξ± β†’ ℝβ‰₯0∞) (hF_meas : βˆ€n, measurable (F n)) (h_bound : βˆ€n, F n ≀ᡐ[ΞΌ] bound) (h_fin : ∫⁻ a, bound a βˆ‚ΞΌ < ∞) (h_lim : βˆ€α΅ a βˆ‚ΞΌ, tendsto (Ξ» n, F n a) at_top (𝓝 (f a))) : tendsto (Ξ»n, ∫⁻ a, F n a βˆ‚ΞΌ) at_top (𝓝 (∫⁻ a, f a βˆ‚ΞΌ)) := tendsto_of_le_liminf_of_limsup_le (calc ∫⁻ a, f a βˆ‚ΞΌ = ∫⁻ a, liminf at_top (Ξ» (n : β„•), F n a) βˆ‚ΞΌ : lintegral_congr_ae $ h_lim.mono $ assume a h, h.liminf_eq.symm ... ≀ liminf at_top (Ξ» n, ∫⁻ a, F n a βˆ‚ΞΌ) : lintegral_liminf_le hF_meas) (calc limsup at_top (Ξ» (n : β„•), ∫⁻ a, F n a βˆ‚ΞΌ) ≀ ∫⁻ a, limsup at_top (Ξ»n, F n a) βˆ‚ΞΌ : limsup_lintegral_le hF_meas h_bound h_fin ... = ∫⁻ a, f a βˆ‚ΞΌ : lintegral_congr_ae $ h_lim.mono $ Ξ» a h, h.limsup_eq) /-- Dominated convergence theorem for nonnegative functions which are just almost everywhere measurable. -/ lemma tendsto_lintegral_of_dominated_convergence' {F : β„• β†’ Ξ± β†’ ℝβ‰₯0∞} {f : Ξ± β†’ ℝβ‰₯0∞} (bound : Ξ± β†’ ℝβ‰₯0∞) (hF_meas : βˆ€n, ae_measurable (F n) ΞΌ) (h_bound : βˆ€n, F n ≀ᡐ[ΞΌ] bound) (h_fin : ∫⁻ a, bound a βˆ‚ΞΌ < ∞) (h_lim : βˆ€α΅ a βˆ‚ΞΌ, tendsto (Ξ» n, F n a) at_top (𝓝 (f a))) : tendsto (Ξ»n, ∫⁻ a, F n a βˆ‚ΞΌ) at_top (𝓝 (∫⁻ a, f a βˆ‚ΞΌ)) := begin have : βˆ€ n, ∫⁻ a, F n a βˆ‚ΞΌ = ∫⁻ a, (hF_meas n).mk (F n) a βˆ‚ΞΌ := Ξ» n, lintegral_congr_ae (hF_meas n).ae_eq_mk, simp_rw this, apply tendsto_lintegral_of_dominated_convergence bound (Ξ» n, (hF_meas n).measurable_mk) _ h_fin, { have : βˆ€ n, βˆ€α΅ a βˆ‚ΞΌ, (hF_meas n).mk (F n) a = F n a := Ξ» n, (hF_meas n).ae_eq_mk.symm, have : βˆ€α΅ a βˆ‚ΞΌ, βˆ€ n, (hF_meas n).mk (F n) a = F n a := ae_all_iff.mpr this, filter_upwards [this, h_lim], assume a H H', simp_rw H, exact H' }, { assume n, filter_upwards [h_bound n, (hF_meas n).ae_eq_mk], assume a H H', rwa H' at H } end /-- Dominated convergence theorem for filters with a countable basis -/ lemma tendsto_lintegral_filter_of_dominated_convergence {ΞΉ} {l : filter ΞΉ} {F : ΞΉ β†’ Ξ± β†’ ℝβ‰₯0∞} {f : Ξ± β†’ ℝβ‰₯0∞} (bound : Ξ± β†’ ℝβ‰₯0∞) (hl_cb : l.is_countably_generated) (hF_meas : βˆ€αΆ  n in l, measurable (F n)) (h_bound : βˆ€αΆ  n in l, βˆ€α΅ a βˆ‚ΞΌ, F n a ≀ bound a) (h_fin : ∫⁻ a, bound a βˆ‚ΞΌ < ∞) (h_lim : βˆ€α΅ a βˆ‚ΞΌ, tendsto (Ξ» n, F n a) l (𝓝 (f a))) : tendsto (Ξ»n, ∫⁻ a, F n a βˆ‚ΞΌ) l (𝓝 $ ∫⁻ a, f a βˆ‚ΞΌ) := begin rw hl_cb.tendsto_iff_seq_tendsto, { intros x xl, have hxl, { rw tendsto_at_top' at xl, exact xl }, have h := inter_mem_sets hF_meas h_bound, replace h := hxl _ h, rcases h with ⟨k, h⟩, rw ← tendsto_add_at_top_iff_nat k, refine tendsto_lintegral_of_dominated_convergence _ _ _ _ _, { exact bound }, { intro, refine (h _ _).1, exact nat.le_add_left _ _ }, { intro, refine (h _ _).2, exact nat.le_add_left _ _ }, { assumption }, { refine h_lim.mono (Ξ» a h_lim, _), apply @tendsto.comp _ _ _ (Ξ»n, x (n + k)) (Ξ»n, F n a), { assumption }, rw tendsto_add_at_top_iff_nat, assumption } }, end section open encodable /-- Monotone convergence for a suprema over a directed family and indexed by an encodable type -/ theorem lintegral_supr_directed [encodable Ξ²] {f : Ξ² β†’ Ξ± β†’ ℝβ‰₯0∞} (hf : βˆ€b, measurable (f b)) (h_directed : directed (≀) f) : ∫⁻ a, ⨆b, f b a βˆ‚ΞΌ = ⨆b, ∫⁻ a, f b a βˆ‚ΞΌ := begin by_cases hΞ² : nonempty Ξ², swap, { simp [supr_of_empty hΞ²] }, resetI, inhabit Ξ², have : βˆ€a, (⨆ b, f b a) = (⨆ n, f (h_directed.sequence f n) a), { assume a, refine le_antisymm (supr_le $ assume b, _) (supr_le $ assume n, le_supr (Ξ»n, f n a) _), exact le_supr_of_le (encode b + 1) (h_directed.le_sequence b a) }, calc ∫⁻ a, ⨆ b, f b a βˆ‚ΞΌ = ∫⁻ a, ⨆ n, f (h_directed.sequence f n) a βˆ‚ΞΌ : by simp only [this] ... = ⨆ n, ∫⁻ a, f (h_directed.sequence f n) a βˆ‚ΞΌ : lintegral_supr (assume n, hf _) h_directed.sequence_mono ... = ⨆ b, ∫⁻ a, f b a βˆ‚ΞΌ : begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume b, _), { exact le_supr (Ξ»b, ∫⁻ a, f b a βˆ‚ΞΌ) _ }, { exact le_supr_of_le (encode b + 1) (lintegral_mono $ h_directed.le_sequence b) } end end end lemma lintegral_tsum [encodable Ξ²] {f : Ξ² β†’ Ξ± β†’ ℝβ‰₯0∞} (hf : βˆ€i, measurable (f i)) : ∫⁻ a, βˆ‘' i, f i a βˆ‚ΞΌ = βˆ‘' i, ∫⁻ a, f i a βˆ‚ΞΌ := begin simp only [ennreal.tsum_eq_supr_sum], rw [lintegral_supr_directed], { simp [lintegral_finset_sum _ (Ξ» i _, hf i)] }, { assume b, exact finset.measurable_sum _ (Ξ» i _, hf i) }, { assume s t, use [s βˆͺ t], split, exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_left _ _), exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_right _ _) } end open measure lemma lintegral_Union [encodable Ξ²] {s : Ξ² β†’ set Ξ±} (hm : βˆ€ i, measurable_set (s i)) (hd : pairwise (disjoint on s)) (f : Ξ± β†’ ℝβ‰₯0∞) : ∫⁻ a in ⋃ i, s i, f a βˆ‚ΞΌ = βˆ‘' i, ∫⁻ a in s i, f a βˆ‚ΞΌ := by simp only [measure.restrict_Union hd hm, lintegral_sum_measure] lemma lintegral_Union_le [encodable Ξ²] (s : Ξ² β†’ set Ξ±) (f : Ξ± β†’ ℝβ‰₯0∞) : ∫⁻ a in ⋃ i, s i, f a βˆ‚ΞΌ ≀ βˆ‘' i, ∫⁻ a in s i, f a βˆ‚ΞΌ := begin rw [← lintegral_sum_measure], exact lintegral_mono' restrict_Union_le (le_refl _) end lemma lintegral_map [measurable_space Ξ²] {f : Ξ² β†’ ℝβ‰₯0∞} {g : Ξ± β†’ Ξ²} (hf : measurable f) (hg : measurable g) : ∫⁻ a, f a βˆ‚(map g ΞΌ) = ∫⁻ a, f (g a) βˆ‚ΞΌ := begin simp only [lintegral_eq_supr_eapprox_lintegral, hf, hf.comp hg], { congr, funext n, symmetry, apply simple_func.lintegral_map, { assume a, exact congr_fun (simple_func.eapprox_comp hf hg) a }, { assume s hs, exact map_apply hg hs } }, end lemma lintegral_map' [measurable_space Ξ²] {f : Ξ² β†’ ℝβ‰₯0∞} {g : Ξ± β†’ Ξ²} (hf : ae_measurable f (measure.map g ΞΌ)) (hg : measurable g) : ∫⁻ a, f a βˆ‚(measure.map g ΞΌ) = ∫⁻ a, f (g a) βˆ‚ΞΌ := calc ∫⁻ a, f a βˆ‚(measure.map g ΞΌ) = ∫⁻ a, hf.mk f a βˆ‚(measure.map g ΞΌ) : lintegral_congr_ae hf.ae_eq_mk ... = ∫⁻ a, hf.mk f (g a) βˆ‚ΞΌ : lintegral_map hf.measurable_mk hg ... = ∫⁻ a, f (g a) βˆ‚ΞΌ : lintegral_congr_ae (ae_eq_comp hg hf.ae_eq_mk.symm) lemma lintegral_comp [measurable_space Ξ²] {f : Ξ² β†’ ℝβ‰₯0∞} {g : Ξ± β†’ Ξ²} (hf : measurable f) (hg : measurable g) : lintegral ΞΌ (f ∘ g) = ∫⁻ a, f a βˆ‚(map g ΞΌ) := (lintegral_map hf hg).symm lemma set_lintegral_map [measurable_space Ξ²] {f : Ξ² β†’ ℝβ‰₯0∞} {g : Ξ± β†’ Ξ²} {s : set Ξ²} (hs : measurable_set s) (hf : measurable f) (hg : measurable g) : ∫⁻ y in s, f y βˆ‚(map g ΞΌ) = ∫⁻ x in g ⁻¹' s, f (g x) βˆ‚ΞΌ := by rw [restrict_map hg hs, lintegral_map hf hg] /-- The `lintegral` transforms appropriately under a measurable equivalence `g : Ξ± ≃ᡐ Ξ²`. (Compare `lintegral_map`, which applies to a wider class of functions `g : Ξ± β†’ Ξ²`, but requires measurability of the function being integrated.) -/ lemma lintegral_map_equiv [measurable_space Ξ²] (f : Ξ² β†’ ℝβ‰₯0∞) (g : Ξ± ≃ᡐ Ξ²) : ∫⁻ a, f a βˆ‚(map g ΞΌ) = ∫⁻ a, f (g a) βˆ‚ΞΌ := begin refine le_antisymm _ _, { refine supr_le_supr2 _, intros fβ‚€, use fβ‚€.comp g g.measurable, refine supr_le_supr2 _, intros hfβ‚€, use Ξ» x, hfβ‚€ (g x), exact (lintegral_map_equiv fβ‚€ g).symm.le }, { refine supr_le_supr2 _, intros fβ‚€, use fβ‚€.comp g.symm g.symm.measurable, refine supr_le_supr2 _, intros hfβ‚€, have : (Ξ» a, (fβ‚€.comp (g.symm) g.symm.measurable) a) ≀ Ξ» (a : Ξ²), f a, { convert Ξ» x, hfβ‚€ (g.symm x), funext, simp [congr_arg f (congr_fun g.self_comp_symm a)] }, use this, convert (lintegral_map_equiv (fβ‚€.comp g.symm g.symm.measurable) g).le, apply simple_func.ext, intros a, convert congr_arg fβ‚€ (congr_fun g.symm_comp_self a).symm using 1 } end lemma lintegral_dirac' (a : Ξ±) {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : ∫⁻ a, f a βˆ‚(dirac a) = f a := by simp [lintegral_congr_ae (ae_eq_dirac' hf)] lemma lintegral_dirac [measurable_singleton_class Ξ±] (a : Ξ±) (f : Ξ± β†’ ℝβ‰₯0∞) : ∫⁻ a, f a βˆ‚(dirac a) = f a := by simp [lintegral_congr_ae (ae_eq_dirac f)] lemma lintegral_count' {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : ∫⁻ a, f a βˆ‚count = βˆ‘' a, f a := begin rw [count, lintegral_sum_measure], congr, exact funext (Ξ» a, lintegral_dirac' a hf), end lemma lintegral_count [measurable_singleton_class Ξ±] (f : Ξ± β†’ ℝβ‰₯0∞) : ∫⁻ a, f a βˆ‚count = βˆ‘' a, f a := begin rw [count, lintegral_sum_measure], congr, exact funext (Ξ» a, lintegral_dirac a f), end lemma ae_lt_top {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) (h2f : ∫⁻ x, f x βˆ‚ΞΌ < ∞) : βˆ€α΅ x βˆ‚ΞΌ, f x < ∞ := begin simp_rw [ae_iff, ennreal.not_lt_top], by_contra h, rw [← not_le] at h2f, apply h2f, have : (f ⁻¹' {∞}).indicator ⊀ ≀ f, { intro x, by_cases hx : x ∈ f ⁻¹' {∞}; [simpa [hx], simp [hx]] }, convert lintegral_mono this, rw [lintegral_indicator _ (hf (measurable_set_singleton ∞))], simp [ennreal.top_mul, preimage, h] end lemma ae_lt_top' {f : Ξ± β†’ ℝβ‰₯0∞} (hf : ae_measurable f ΞΌ) (h2f : ∫⁻ x, f x βˆ‚ΞΌ < ∞) : βˆ€α΅ x βˆ‚ΞΌ, f x < ∞ := begin have h2f_meas : ∫⁻ x, hf.mk f x βˆ‚ΞΌ < ∞, by rwa ←lintegral_congr_ae hf.ae_eq_mk, exact (ae_lt_top hf.measurable_mk h2f_meas).mp (hf.ae_eq_mk.mono (Ξ» x hx h, by rwa hx)), end /-- Given a measure `ΞΌ : measure Ξ±` and a function `f : Ξ± β†’ ℝβ‰₯0∞`, `ΞΌ.with_density f` is the measure such that for a measurable set `s` we have `ΞΌ.with_density f s = ∫⁻ a in s, f a βˆ‚ΞΌ`. -/ def measure.with_density (ΞΌ : measure Ξ±) (f : Ξ± β†’ ℝβ‰₯0∞) : measure Ξ± := measure.of_measurable (Ξ»s hs, ∫⁻ a in s, f a βˆ‚ΞΌ) (by simp) (Ξ» s hs hd, lintegral_Union hs hd _) @[simp] lemma with_density_apply (f : Ξ± β†’ ℝβ‰₯0∞) {s : set Ξ±} (hs : measurable_set s) : ΞΌ.with_density f s = ∫⁻ a in s, f a βˆ‚ΞΌ := measure.of_measurable_apply s hs end lintegral end measure_theory open measure_theory measure_theory.simple_func /-- To prove something for an arbitrary measurable function into `ℝβ‰₯0∞`, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions can be added once we need them (for example in `h_add` it is only necessary to consider the sum of a simple function with a multiple of a characteristic function and that the intersection of their images is a subset of `{0}`. -/ @[elab_as_eliminator] theorem measurable.ennreal_induction {Ξ±} [measurable_space Ξ±] {P : (Ξ± β†’ ℝβ‰₯0∞) β†’ Prop} (h_ind : βˆ€ (c : ℝβ‰₯0∞) ⦃s⦄, measurable_set s β†’ P (indicator s (Ξ» _, c))) (h_add : βˆ€ ⦃f g : Ξ± β†’ ℝβ‰₯0βˆžβ¦„, disjoint (support f) (support g) β†’ measurable f β†’ measurable g β†’ P f β†’ P g β†’ P (f + g)) (h_supr : βˆ€ ⦃f : β„• β†’ Ξ± β†’ ℝβ‰₯0βˆžβ¦„ (hf : βˆ€n, measurable (f n)) (h_mono : monotone f) (hP : βˆ€ n, P (f n)), P (Ξ» x, ⨆ n, f n x)) ⦃f : Ξ± β†’ ℝβ‰₯0βˆžβ¦„ (hf : measurable f) : P f := begin convert h_supr (Ξ» n, (eapprox f n).measurable) (monotone_eapprox f) _, { ext1 x, rw [supr_eapprox_apply f hf] }, { exact Ξ» n, simple_func.induction (Ξ» c s hs, h_ind c hs) (Ξ» f g hfg hf hg, h_add hfg f.measurable g.measurable hf hg) (eapprox f n) } end namespace measure_theory /-- This is Exercise 1.2.1 from [tao2010]. It allows you to express integration of a measurable function with respect to `(ΞΌ.with_density f)` as an integral with respect to `ΞΌ`, called the base measure. `ΞΌ` is often the Lebesgue measure, and in this circumstance `f` is the probability density function, and `(ΞΌ.with_density f)` represents any continuous random variable as a probability measure, such as the uniform distribution between 0 and 1, the Gaussian distribution, the exponential distribution, the Beta distribution, or the Cauchy distribution (see Section 2.4 of [wasserman2004]). Thus, this method shows how to one can calculate expectations, variances, and other moments as a function of the probability density function. -/ lemma lintegral_with_density_eq_lintegral_mul {Ξ±} [measurable_space Ξ±] (ΞΌ : measure Ξ±) {f : Ξ± β†’ ℝβ‰₯0∞} (h_mf : measurable f) : βˆ€ {g : Ξ± β†’ ℝβ‰₯0∞}, measurable g β†’ ∫⁻ a, g a βˆ‚(ΞΌ.with_density f) = ∫⁻ a, (f * g) a βˆ‚ΞΌ := begin apply measurable.ennreal_induction, { intros c s h_ms, simp [*, mul_comm _ c, ← indicator_mul_right], }, { intros g h h_univ h_mea_g h_mea_h h_ind_g h_ind_h, simp [mul_add, *, measurable.mul] }, { intros g h_mea_g h_mono_g h_ind, have : monotone (Ξ» n a, f a * g n a) := Ξ» m n hmn x, ennreal.mul_le_mul le_rfl (h_mono_g hmn x), simp [lintegral_supr, ennreal.mul_supr, h_mf.mul (h_mea_g _), *] } end end measure_theory
7b6110b9fc3d396f2d9294e670b276d1fef433ed
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/partition_of_unity.lean
a826173c07c43637d122baef599c8573894a0351
[ "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
23,036
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 algebra.big_operators.finprod import set_theory.ordinal.basic import topology.continuous_function.algebra import topology.paracompact import topology.shrinking_lemma import topology.urysohns_lemma /-! # Continuous partition of unity > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define `partition_of_unity (ΞΉ X : Type*) [topological_space X] (s : set X := univ)` to be a continuous partition of unity on `s` indexed by `ΞΉ`. More precisely, `f : partition_of_unity ΞΉ X s` is a collection of continuous functions `f i : C(X, ℝ)`, `i : ΞΉ`, such that * the supports of `f i` form a locally finite family of sets; * each `f i` is nonnegative; * `βˆ‘αΆ  i, f i x = 1` for all `x ∈ s`; * `βˆ‘αΆ  i, f i x ≀ 1` for all `x : X`. In the case `s = univ` the last assumption follows from the previous one but it is convenient to have this assumption in the case `s β‰  univ`. We also define a bump function covering, `bump_covering (ΞΉ X : Type*) [topological_space X] (s : set X := univ)`, to be a collection of functions `f i : C(X, ℝ)`, `i : ΞΉ`, such that * the supports of `f i` form a locally finite family of sets; * each `f i` is nonnegative; * for each `x ∈ s` there exists `i : ΞΉ` such that `f i y = 1` in a neighborhood of `x`. The term is motivated by the smooth case. If `f` is a bump function covering indexed by a linearly ordered type, then `g i x = f i x * ∏ᢠ j < i, (1 - f j x)` is a partition of unity, see `bump_covering.to_partition_of_unity`. Note that only finitely many terms `1 - f j x` are not equal to one, so this product is well-defined. Note that `g i x = ∏ᢠ j ≀ i, (1 - f j x) - ∏ᢠ j < i, (1 - f j x)`, so most terms in the sum `βˆ‘αΆ  i, g i x` cancel, and we get `βˆ‘αΆ  i, g i x = 1 - ∏ᢠ i, (1 - f i x)`, and the latter product equals zero because one of `f i x` is equal to one. We say that a partition of unity or a bump function covering `f` is *subordinate* to a family of sets `U i`, `i : ΞΉ`, if the closure of the support of each `f i` is included in `U i`. We use Urysohn's Lemma to prove that a locally finite open covering of a normal topological space admits a subordinate bump function covering (hence, a subordinate partition of unity), see `bump_covering.exists_is_subordinate_of_locally_finite`. If `X` is a paracompact space, then any open covering admits a locally finite refinement, hence it admits a subordinate bump function covering and a subordinate partition of unity, see `bump_covering.exists_is_subordinate`. We also provide two slightly more general versions of these lemmas, `bump_covering.exists_is_subordinate_of_locally_finite_of_prop` and `bump_covering.exists_is_subordinate_of_prop`, to be used later in the construction of a smooth partition of unity. ## Implementation notes Most (if not all) books only define a partition of unity of the whole space. However, quite a few proofs only deal with `f i` such that `tsupport (f i)` meets a specific closed subset, and it is easier to formalize these proofs if we don't have other functions right away. We use `well_ordering_rel j i` instead of `j < i` in the definition of `bump_covering.to_partition_of_unity` to avoid a `[linear_order ΞΉ]` assumption. While `well_ordering_rel j i` is a well order, not only a strict linear order, we never use this property. ## Tags partition of unity, bump function, Urysohn's lemma, normal space, paracompact space -/ universes u v open function set filter open_locale big_operators topology classical noncomputable theory /-- A continuous partition of unity on a set `s : set X` is a collection of continuous functions `f i` such that * the supports of `f i` form a locally finite family of sets, i.e., for every point `x : X` there exists a neighborhood `U βˆ‹ x` such that all but finitely many functions `f i` are zero on `U`; * the functions `f i` are nonnegative; * the sum `βˆ‘αΆ  i, f i x` is equal to one for every `x ∈ s` and is less than or equal to one otherwise. If `X` is a normal paracompact space, then `partition_of_unity.exists_is_subordinate` guarantees that for every open covering `U : set (set X)` of `s` there exists a partition of unity that is subordinate to `U`. -/ structure partition_of_unity (ΞΉ X : Type*) [topological_space X] (s : set X := univ) := (to_fun : ΞΉ β†’ C(X, ℝ)) (locally_finite' : locally_finite (Ξ» i, support (to_fun i))) (nonneg' : 0 ≀ to_fun) (sum_eq_one' : βˆ€ x ∈ s, βˆ‘αΆ  i, to_fun i x = 1) (sum_le_one' : βˆ€ x, βˆ‘αΆ  i, to_fun i x ≀ 1) /-- A `bump_covering ΞΉ X s` is an indexed family of functions `f i`, `i : ΞΉ`, such that * the supports of `f i` form a locally finite family of sets, i.e., for every point `x : X` there exists a neighborhood `U βˆ‹ x` such that all but finitely many functions `f i` are zero on `U`; * for all `i`, `x` we have `0 ≀ f i x ≀ 1`; * each point `x ∈ s` belongs to the interior of `{x | f i x = 1}` for some `i`. One of the main use cases for a `bump_covering` is to define a `partition_of_unity`, see `bump_covering.to_partition_of_unity`, but some proofs can directly use a `bump_covering` instead of a `partition_of_unity`. If `X` is a normal paracompact space, then `bump_covering.exists_is_subordinate` guarantees that for every open covering `U : set (set X)` of `s` there exists a `bump_covering` of `s` that is subordinate to `U`. -/ structure bump_covering (ΞΉ X : Type*) [topological_space X] (s : set X := univ) := (to_fun : ΞΉ β†’ C(X, ℝ)) (locally_finite' : locally_finite (Ξ» i, support (to_fun i))) (nonneg' : 0 ≀ to_fun) (le_one' : to_fun ≀ 1) (eventually_eq_one' : βˆ€ x ∈ s, βˆƒ i, to_fun i =αΆ [𝓝 x] 1) variables {ΞΉ : Type u} {X : Type v} [topological_space X] namespace partition_of_unity variables {E : Type*} [add_comm_monoid E] [smul_with_zero ℝ E] [topological_space E] [has_continuous_smul ℝ E] {s : set X} (f : partition_of_unity ΞΉ X s) instance : has_coe_to_fun (partition_of_unity ΞΉ X s) (Ξ» _, ΞΉ β†’ C(X, ℝ)) := ⟨to_fun⟩ protected lemma locally_finite : locally_finite (Ξ» i, support (f i)) := f.locally_finite' lemma locally_finite_tsupport : locally_finite (Ξ» i, tsupport (f i)) := f.locally_finite.closure lemma nonneg (i : ΞΉ) (x : X) : 0 ≀ f i x := f.nonneg' i x lemma sum_eq_one {x : X} (hx : x ∈ s) : βˆ‘αΆ  i, f i x = 1 := f.sum_eq_one' x hx /-- If `f` is a partition of unity on `s`, then for every `x ∈ s` there exists an index `i` such that `0 < f i x`. -/ lemma exists_pos {x : X} (hx : x ∈ s) : βˆƒ i, 0 < f i x := begin have H := f.sum_eq_one hx, contrapose! H, simpa only [Ξ» i, (H i).antisymm (f.nonneg i x), finsum_zero] using zero_ne_one end lemma sum_le_one (x : X) : βˆ‘αΆ  i, f i x ≀ 1 := f.sum_le_one' x lemma sum_nonneg (x : X) : 0 ≀ βˆ‘αΆ  i, f i x := finsum_nonneg $ Ξ» i, f.nonneg i x lemma le_one (i : ΞΉ) (x : X) : f i x ≀ 1 := (single_le_finsum i (f.locally_finite.point_finite x) (Ξ» j, f.nonneg j x)).trans (f.sum_le_one x) /-- If `f` is a partition of unity on `s : set X` and `g : X β†’ E` is continuous at every point of the topological support of some `f i`, then `Ξ» x, f i x β€’ g x` is continuous on the whole space. -/ lemma continuous_smul {g : X β†’ E} {i : ΞΉ} (hg : βˆ€ x ∈ tsupport (f i), continuous_at g x) : continuous (Ξ» x, f i x β€’ g x) := continuous_of_tsupport $ Ξ» x hx, ((f i).continuous_at x).smul $ hg x $ tsupport_smul_subset_left _ _ hx /-- If `f` is a partition of unity on a set `s : set X` and `g : ΞΉ β†’ X β†’ E` is a family of functions such that each `g i` is continuous at every point of the topological support of `f i`, then the sum `Ξ» x, βˆ‘αΆ  i, f i x β€’ g i x` is continuous on the whole space. -/ lemma continuous_finsum_smul [has_continuous_add E] {g : ΞΉ β†’ X β†’ E} (hg : βˆ€ i (x ∈ tsupport (f i)), continuous_at (g i) x) : continuous (Ξ» x, βˆ‘αΆ  i, f i x β€’ g i x) := continuous_finsum (Ξ» i, f.continuous_smul (hg i)) $ f.locally_finite.subset $ Ξ» i, support_smul_subset_left _ _ /-- A partition of unity `f i` is subordinate to a family of sets `U i` indexed by the same type if for each `i` the closure of the support of `f i` is a subset of `U i`. -/ def is_subordinate (U : ΞΉ β†’ set X) : Prop := βˆ€ i, tsupport (f i) βŠ† U i variables {f} lemma exists_finset_nhd_support_subset {U : ΞΉ β†’ set X} (hso : f.is_subordinate U) (ho : βˆ€ i, is_open (U i)) (x : X) : βˆƒ (is : finset ΞΉ) {n : set X} (hn₁ : n ∈ 𝓝 x) (hnβ‚‚ : n βŠ† β‹‚ i ∈ is, U i), βˆ€ (z ∈ n), support (Ξ» i, f i z) βŠ† is := f.locally_finite.exists_finset_nhd_support_subset hso ho x /-- If `f` is a partition of unity that is subordinate to a family of open sets `U i` and `g : ΞΉ β†’ X β†’ E` is a family of functions such that each `g i` is continuous on `U i`, then the sum `Ξ» x, βˆ‘αΆ  i, f i x β€’ g i x` is a continuous function. -/ lemma is_subordinate.continuous_finsum_smul [has_continuous_add E] {U : ΞΉ β†’ set X} (ho : βˆ€ i, is_open (U i)) (hf : f.is_subordinate U) {g : ΞΉ β†’ X β†’ E} (hg : βˆ€ i, continuous_on (g i) (U i)) : continuous (Ξ» x, βˆ‘αΆ  i, f i x β€’ g i x) := f.continuous_finsum_smul $ Ξ» i x hx, (hg i).continuous_at $ (ho i).mem_nhds $ hf i hx end partition_of_unity namespace bump_covering variables {s : set X} (f : bump_covering ΞΉ X s) instance : has_coe_to_fun (bump_covering ΞΉ X s) (Ξ» _, ΞΉ β†’ C(X, ℝ)) := ⟨to_fun⟩ protected lemma locally_finite : locally_finite (Ξ» i, support (f i)) := f.locally_finite' lemma locally_finite_tsupport : locally_finite (Ξ» i, tsupport (f i)) := f.locally_finite.closure protected lemma point_finite (x : X) : {i | f i x β‰  0}.finite := f.locally_finite.point_finite x lemma nonneg (i : ΞΉ) (x : X) : 0 ≀ f i x := f.nonneg' i x lemma le_one (i : ΞΉ) (x : X) : f i x ≀ 1 := f.le_one' i x /-- A `bump_covering` that consists of a single function, uniformly equal to one, defined as an example for `inhabited` instance. -/ protected def single (i : ΞΉ) (s : set X) : bump_covering ΞΉ X s := { to_fun := pi.single i 1, locally_finite' := Ξ» x, begin refine ⟨univ, univ_mem, (finite_singleton i).subset _⟩, rintro j ⟨x, hx, -⟩, contrapose! hx, rw [mem_singleton_iff] at hx, simp [hx] end, nonneg' := le_update_iff.2 ⟨λ x, zero_le_one, Ξ» _ _, le_rfl⟩, le_one' := update_le_iff.2 ⟨le_rfl, Ξ» _ _ _, zero_le_one⟩, eventually_eq_one' := Ξ» x _, ⟨i, by simp⟩ } @[simp] lemma coe_single (i : ΞΉ) (s : set X) : ⇑(bump_covering.single i s) = pi.single i 1 := rfl instance [inhabited ΞΉ] : inhabited (bump_covering ΞΉ X s) := ⟨bump_covering.single default s⟩ /-- A collection of bump functions `f i` is subordinate to a family of sets `U i` indexed by the same type if for each `i` the closure of the support of `f i` is a subset of `U i`. -/ def is_subordinate (f : bump_covering ΞΉ X s) (U : ΞΉ β†’ set X) : Prop := βˆ€ i, tsupport (f i) βŠ† U i lemma is_subordinate.mono {f : bump_covering ΞΉ X s} {U V : ΞΉ β†’ set X} (hU : f.is_subordinate U) (hV : βˆ€ i, U i βŠ† V i) : f.is_subordinate V := Ξ» i, subset.trans (hU i) (hV i) /-- If `X` is a normal topological space and `U i`, `i : ΞΉ`, is a locally finite open covering of a closed set `s`, then there exists a `bump_covering ΞΉ X s` that is subordinate to `U`. If `X` is a paracompact space, then the assumption `hf : locally_finite U` can be omitted, see `bump_covering.exists_is_subordinate`. This version assumes that `p : (X β†’ ℝ) β†’ Prop` is a predicate that satisfies Urysohn's lemma, and provides a `bump_covering` such that each function of the covering satisfies `p`. -/ lemma exists_is_subordinate_of_locally_finite_of_prop [normal_space X] (p : (X β†’ ℝ) β†’ Prop) (h01 : βˆ€ s t, is_closed s β†’ is_closed t β†’ disjoint s t β†’ βˆƒ f : C(X, ℝ), p f ∧ eq_on f 0 s ∧ eq_on f 1 t ∧ βˆ€ x, f x ∈ Icc (0 : ℝ) 1) (hs : is_closed s) (U : ΞΉ β†’ set X) (ho : βˆ€ i, is_open (U i)) (hf : locally_finite U) (hU : s βŠ† ⋃ i, U i) : βˆƒ f : bump_covering ΞΉ X s, (βˆ€ i, p (f i)) ∧ f.is_subordinate U := begin rcases exists_subset_Union_closure_subset hs ho (Ξ» x _, hf.point_finite x) hU with ⟨V, hsV, hVo, hVU⟩, have hVU' : βˆ€ i, V i βŠ† U i, from Ξ» i, subset.trans subset_closure (hVU i), rcases exists_subset_Union_closure_subset hs hVo (Ξ» x _, (hf.subset hVU').point_finite x) hsV with ⟨W, hsW, hWo, hWV⟩, choose f hfp hf0 hf1 hf01 using Ξ» i, h01 _ _ (is_closed_compl_iff.2 $ hVo i) is_closed_closure (disjoint_right.2 $ Ξ» x hx, not_not.2 (hWV i hx)), have hsupp : βˆ€ i, support (f i) βŠ† V i, from Ξ» i, support_subset_iff'.2 (hf0 i), refine ⟨⟨f, hf.subset (Ξ» i, subset.trans (hsupp i) (hVU' i)), Ξ» i x, (hf01 i x).1, Ξ» i x, (hf01 i x).2, Ξ» x hx, _⟩, hfp, Ξ» i, subset.trans (closure_mono (hsupp i)) (hVU i)⟩, rcases mem_Union.1 (hsW hx) with ⟨i, hi⟩, exact ⟨i, ((hf1 i).mono subset_closure).eventually_eq_of_mem ((hWo i).mem_nhds hi)⟩ end /-- If `X` is a normal topological space and `U i`, `i : ΞΉ`, is a locally finite open covering of a closed set `s`, then there exists a `bump_covering ΞΉ X s` that is subordinate to `U`. If `X` is a paracompact space, then the assumption `hf : locally_finite U` can be omitted, see `bump_covering.exists_is_subordinate`. -/ lemma exists_is_subordinate_of_locally_finite [normal_space X] (hs : is_closed s) (U : ΞΉ β†’ set X) (ho : βˆ€ i, is_open (U i)) (hf : locally_finite U) (hU : s βŠ† ⋃ i, U i) : βˆƒ f : bump_covering ΞΉ X s, f.is_subordinate U := let ⟨f, _, hfU⟩ := exists_is_subordinate_of_locally_finite_of_prop (Ξ» _, true) (Ξ» s t hs ht hd, (exists_continuous_zero_one_of_closed hs ht hd).imp $ Ξ» f hf, ⟨trivial, hf⟩) hs U ho hf hU in ⟨f, hfU⟩ /-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set `s`, then there exists a `bump_covering ΞΉ X s` that is subordinate to `U`. This version assumes that `p : (X β†’ ℝ) β†’ Prop` is a predicate that satisfies Urysohn's lemma, and provides a `bump_covering` such that each function of the covering satisfies `p`. -/ lemma exists_is_subordinate_of_prop [normal_space X] [paracompact_space X] (p : (X β†’ ℝ) β†’ Prop) (h01 : βˆ€ s t, is_closed s β†’ is_closed t β†’ disjoint s t β†’ βˆƒ f : C(X, ℝ), p f ∧ eq_on f 0 s ∧ eq_on f 1 t ∧ βˆ€ x, f x ∈ Icc (0 : ℝ) 1) (hs : is_closed s) (U : ΞΉ β†’ set X) (ho : βˆ€ i, is_open (U i)) (hU : s βŠ† ⋃ i, U i) : βˆƒ f : bump_covering ΞΉ X s, (βˆ€ i, p (f i)) ∧ f.is_subordinate U := begin rcases precise_refinement_set hs _ ho hU with ⟨V, hVo, hsV, hVf, hVU⟩, rcases exists_is_subordinate_of_locally_finite_of_prop p h01 hs V hVo hVf hsV with ⟨f, hfp, hf⟩, exact ⟨f, hfp, hf.mono hVU⟩ end /-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set `s`, then there exists a `bump_covering ΞΉ X s` that is subordinate to `U`. -/ lemma exists_is_subordinate [normal_space X] [paracompact_space X] (hs : is_closed s) (U : ΞΉ β†’ set X) (ho : βˆ€ i, is_open (U i)) (hU : s βŠ† ⋃ i, U i) : βˆƒ f : bump_covering ΞΉ X s, f.is_subordinate U := begin rcases precise_refinement_set hs _ ho hU with ⟨V, hVo, hsV, hVf, hVU⟩, rcases exists_is_subordinate_of_locally_finite hs V hVo hVf hsV with ⟨f, hf⟩, exact ⟨f, hf.mono hVU⟩ end /-- Index of a bump function such that `fs i =αΆ [𝓝 x] 1`. -/ def ind (x : X) (hx : x ∈ s) : ΞΉ := (f.eventually_eq_one' x hx).some lemma eventually_eq_one (x : X) (hx : x ∈ s) : f (f.ind x hx) =αΆ [𝓝 x] 1 := (f.eventually_eq_one' x hx).some_spec lemma ind_apply (x : X) (hx : x ∈ s) : f (f.ind x hx) x = 1 := (f.eventually_eq_one x hx).eq_of_nhds /-- Partition of unity defined by a `bump_covering`. We use this auxiliary definition to prove some properties of the new family of functions before bundling it into a `partition_of_unity`. Do not use this definition, use `bump_function.to_partition_of_unity` instead. The partition of unity is given by the formula `g i x = f i x * ∏ᢠ j < i, (1 - f j x)`. In other words, `g i x = ∏ᢠ j < i, (1 - f j x) - ∏ᢠ j ≀ i, (1 - f j x)`, so `βˆ‘αΆ  i, g i x = 1 - ∏ᢠ j, (1 - f j x)`. If `x ∈ s`, then one of `f j x` equals one, hence the product of `1 - f j x` vanishes, and `βˆ‘αΆ  i, g i x = 1`. In order to avoid an assumption `linear_order ΞΉ`, we use `well_ordering_rel` instead of `(<)`. -/ def to_pou_fun (i : ΞΉ) (x : X) : ℝ := f i x * ∏ᢠ j (hj : well_ordering_rel j i), (1 - f j x) lemma to_pou_fun_zero_of_zero {i : ΞΉ} {x : X} (h : f i x = 0) : f.to_pou_fun i x = 0 := by rw [to_pou_fun, h, zero_mul] lemma support_to_pou_fun_subset (i : ΞΉ) : support (f.to_pou_fun i) βŠ† support (f i) := Ξ» x, mt $ f.to_pou_fun_zero_of_zero lemma to_pou_fun_eq_mul_prod (i : ΞΉ) (x : X) (t : finset ΞΉ) (ht : βˆ€ j, well_ordering_rel j i β†’ f j x β‰  0 β†’ j ∈ t) : f.to_pou_fun i x = f i x * ∏ j in t.filter (Ξ» j, well_ordering_rel j i), (1 - f j x) := begin refine congr_arg _ (finprod_cond_eq_prod_of_cond_iff _ (Ξ» j hj, _)), rw [ne.def, sub_eq_self] at hj, rw [finset.mem_filter, iff.comm, and_iff_right_iff_imp], exact flip (ht j) hj end lemma sum_to_pou_fun_eq (x : X) : βˆ‘αΆ  i, f.to_pou_fun i x = 1 - ∏ᢠ i, (1 - f i x) := begin set s := (f.point_finite x).to_finset, have hs : (s : set ΞΉ) = {i | f i x β‰  0} := finite.coe_to_finset _, have A : support (Ξ» i, to_pou_fun f i x) βŠ† s, { rw hs, exact Ξ» i hi, f.support_to_pou_fun_subset i hi }, have B : mul_support (Ξ» i, 1 - f i x) βŠ† s, { rw [hs, mul_support_one_sub], exact Ξ» i, id }, letI : linear_order ΞΉ := linear_order_of_STO well_ordering_rel, rw [finsum_eq_sum_of_support_subset _ A, finprod_eq_prod_of_mul_support_subset _ B, finset.prod_one_sub_ordered, sub_sub_cancel], refine finset.sum_congr rfl (Ξ» i hi, _), convert f.to_pou_fun_eq_mul_prod _ _ _ (Ξ» j hji hj, _), rwa finite.mem_to_finset end lemma exists_finset_to_pou_fun_eventually_eq (i : ΞΉ) (x : X) : βˆƒ t : finset ΞΉ, f.to_pou_fun i =αΆ [𝓝 x] f i * ∏ j in t.filter (Ξ» j, well_ordering_rel j i), (1 - f j) := begin rcases f.locally_finite x with ⟨U, hU, hf⟩, use hf.to_finset, filter_upwards [hU] with y hyU, simp only [pi.mul_apply, finset.prod_apply], apply to_pou_fun_eq_mul_prod, intros j hji hj, exact hf.mem_to_finset.2 ⟨y, ⟨hj, hyU⟩⟩ end lemma continuous_to_pou_fun (i : ΞΉ) : continuous (f.to_pou_fun i) := begin refine ((f i).continuous.mul $ continuous_finprod_cond (Ξ» j _, continuous_const.sub (f j).continuous) _), simp only [mul_support_one_sub], exact f.locally_finite end /-- The partition of unity defined by a `bump_covering`. The partition of unity is given by the formula `g i x = f i x * ∏ᢠ j < i, (1 - f j x)`. In other words, `g i x = ∏ᢠ j < i, (1 - f j x) - ∏ᢠ j ≀ i, (1 - f j x)`, so `βˆ‘αΆ  i, g i x = 1 - ∏ᢠ j, (1 - f j x)`. If `x ∈ s`, then one of `f j x` equals one, hence the product of `1 - f j x` vanishes, and `βˆ‘αΆ  i, g i x = 1`. In order to avoid an assumption `linear_order ΞΉ`, we use `well_ordering_rel` instead of `(<)`. -/ def to_partition_of_unity : partition_of_unity ΞΉ X s := { to_fun := Ξ» i, ⟨f.to_pou_fun i, f.continuous_to_pou_fun i⟩, locally_finite' := f.locally_finite.subset f.support_to_pou_fun_subset, nonneg' := Ξ» i x, mul_nonneg (f.nonneg i x) (finprod_cond_nonneg $ Ξ» j hj, sub_nonneg.2 $ f.le_one j x), sum_eq_one' := Ξ» x hx, begin simp only [continuous_map.coe_mk, sum_to_pou_fun_eq, sub_eq_self], apply finprod_eq_zero (Ξ» i, 1 - f i x) (f.ind x hx), { simp only [f.ind_apply x hx, sub_self] }, { rw mul_support_one_sub, exact f.point_finite x } end, sum_le_one' := Ξ» x, begin simp only [continuous_map.coe_mk, sum_to_pou_fun_eq, sub_le_self_iff], exact finprod_nonneg (Ξ» i, sub_nonneg.2 $ f.le_one i x) end } lemma to_partition_of_unity_apply (i : ΞΉ) (x : X) : f.to_partition_of_unity i x = f i x * ∏ᢠ j (hj : well_ordering_rel j i), (1 - f j x) := rfl lemma to_partition_of_unity_eq_mul_prod (i : ΞΉ) (x : X) (t : finset ΞΉ) (ht : βˆ€ j, well_ordering_rel j i β†’ f j x β‰  0 β†’ j ∈ t) : f.to_partition_of_unity i x = f i x * ∏ j in t.filter (Ξ» j, well_ordering_rel j i), (1 - f j x) := f.to_pou_fun_eq_mul_prod i x t ht lemma exists_finset_to_partition_of_unity_eventually_eq (i : ΞΉ) (x : X) : βˆƒ t : finset ΞΉ, f.to_partition_of_unity i =αΆ [𝓝 x] f i * ∏ j in t.filter (Ξ» j, well_ordering_rel j i), (1 - f j) := f.exists_finset_to_pou_fun_eventually_eq i x lemma to_partition_of_unity_zero_of_zero {i : ΞΉ} {x : X} (h : f i x = 0) : f.to_partition_of_unity i x = 0 := f.to_pou_fun_zero_of_zero h lemma support_to_partition_of_unity_subset (i : ΞΉ) : support (f.to_partition_of_unity i) βŠ† support (f i) := f.support_to_pou_fun_subset i lemma sum_to_partition_of_unity_eq (x : X) : βˆ‘αΆ  i, f.to_partition_of_unity i x = 1 - ∏ᢠ i, (1 - f i x) := f.sum_to_pou_fun_eq x lemma is_subordinate.to_partition_of_unity {f : bump_covering ΞΉ X s} {U : ΞΉ β†’ set X} (h : f.is_subordinate U) : f.to_partition_of_unity.is_subordinate U := Ξ» i, subset.trans (closure_mono $ f.support_to_partition_of_unity_subset i) (h i) end bump_covering namespace partition_of_unity variables {s : set X} instance [inhabited ΞΉ] : inhabited (partition_of_unity ΞΉ X s) := ⟨bump_covering.to_partition_of_unity default⟩ /-- If `X` is a normal topological space and `U` is a locally finite open covering of a closed set `s`, then there exists a `partition_of_unity ΞΉ X s` that is subordinate to `U`. If `X` is a paracompact space, then the assumption `hf : locally_finite U` can be omitted, see `bump_covering.exists_is_subordinate`. -/ lemma exists_is_subordinate_of_locally_finite [normal_space X] (hs : is_closed s) (U : ΞΉ β†’ set X) (ho : βˆ€ i, is_open (U i)) (hf : locally_finite U) (hU : s βŠ† ⋃ i, U i) : βˆƒ f : partition_of_unity ΞΉ X s, f.is_subordinate U := let ⟨f, hf⟩ := bump_covering.exists_is_subordinate_of_locally_finite hs U ho hf hU in ⟨f.to_partition_of_unity, hf.to_partition_of_unity⟩ /-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set `s`, then there exists a `partition_of_unity ΞΉ X s` that is subordinate to `U`. -/ lemma exists_is_subordinate [normal_space X] [paracompact_space X] (hs : is_closed s) (U : ΞΉ β†’ set X) (ho : βˆ€ i, is_open (U i)) (hU : s βŠ† ⋃ i, U i) : βˆƒ f : partition_of_unity ΞΉ X s, f.is_subordinate U := let ⟨f, hf⟩ := bump_covering.exists_is_subordinate hs U ho hU in ⟨f.to_partition_of_unity, hf.to_partition_of_unity⟩ end partition_of_unity
7e6a839fb469fa3da0c1fd115dd406eb3da4faf6
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/tests/lean/1467.lean
bc9476e0d02e4374bef9b97e3d80f3112277a592
[ "Apache-2.0" ]
permissive
moritayasuaki/lean
9f666c323cb6fa1f31ac597d777914aed41e3b7a
ae96ebf6ee953088c235ff7ae0e8c95066ba8001
refs/heads/master
1,611,135,440,814
1,493,852,869,000
1,493,852,869,000
90,269,903
0
0
null
1,493,906,291,000
1,493,906,291,000
null
UTF-8
Lean
false
false
856
lean
constants f g h : β„• β†’ β„• axiom H_f_g : βˆ€ n, f (g n) = n example (m : β„•) : h m = h m := begin definev n : β„• := g m, assertv H : f n = m := begin dsimp, rw H_f_g end, subst H, -- Error here end set_option pp.instantiate_mvars false example (m : β„•) : h m = h m := begin define n : β„•, -- add metavar exact g m, assertv H : f n = m := begin dsimp, rw H_f_g end, subst H, -- Error here end example (m : β„•) : h m = h m := begin definev n : β„• := g m, assertv H : f n = m := begin dsimp, rw H_f_g end, subst m, -- Error here end set_option pp.instantiate_mvars false example (m : β„•) : h m = h m := begin define n : β„•, -- add metavar exact g m, assertv H : f n = m := begin dsimp, rw H_f_g end, subst m, -- Error here end example (m p: β„•) : h m = h m := begin definev a : β„• := g p, definev n : β„• := g a, clear p -- Error here end
a47d2a156f5e524f5449de2a23166f08f953c94b
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/star/star_alg_hom.lean
40a10f489024005e408a964876e2927533df0255
[ "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
17,544
lean
/- Copyright (c) 2022 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import algebra.hom.non_unital_alg import algebra.star.prod /-! # Morphisms of star algebras This file defines morphisms between `R`-algebras (unital or non-unital) `A` and `B` where both `A` and `B` are equipped with a `star` operation. These morphisms, namely `star_alg_hom` and `non_unital_star_alg_hom` are direct extensions of their non-`star`red counterparts with a field `map_star` which guarantees they preserve the star operation. We keep the type classes as generic as possible, in keeping with the definition of `non_unital_alg_hom` in the non-unital case. In this file, we only assume `has_star` unless we want to talk about the zero map as a `non_unital_star_alg_hom`, in which case we need `star_add_monoid`. Note that the scalar ring `R` is not required to have a star operation, nor do we need `star_ring` or `star_module` structures on `A` and `B`. As with `non_unital_alg_hom`, in the non-unital case the multiplications are not assumed to be associative or unital, or even to be compatible with the scalar actions. In a typical application, the operations will satisfy compatibility conditions making them into algebras (albeit possibly non-associative and/or non-unital) but such conditions are not required here for the definitions. The primary impetus for defining these types is that they constitute the morphisms in the categories of unital C⋆-algebras (with `star_alg_hom`s) and of C⋆-algebras (with `non_unital_star_alg_hom`s). TODO: add `star_alg_equiv`. ## Main definitions * `non_unital_alg_hom` * `star_alg_hom` ## Tags non-unital, algebra, morphism, star -/ set_option old_structure_cmd true /-! ### Non-unital star algebra homomorphisms -/ /-- A *non-unital ⋆-algebra homomorphism* is a non-unital algebra homomorphism between non-unital `R`-algebras `A` and `B` equipped with a `star` operation, and this homomorphism is also `star`-preserving. -/ structure non_unital_star_alg_hom (R A B : Type*) [monoid R] [non_unital_non_assoc_semiring A] [distrib_mul_action R A] [has_star A] [non_unital_non_assoc_semiring B] [distrib_mul_action R B] [has_star B] extends A →ₙₐ[R] B := (map_star' : βˆ€ a : A, to_fun (star a) = star (to_fun a)) infixr ` →⋆ₙₐ `:25 := non_unital_star_alg_hom _ notation A ` →⋆ₙₐ[`:25 R `] ` B := non_unital_star_alg_hom R A B /-- Reinterpret a non-unital star algebra homomorphism as a non-unital algebra homomorphism by forgetting the interaction with the star operation. -/ add_decl_doc non_unital_star_alg_hom.to_non_unital_alg_hom /-- `non_unital_star_alg_hom_class F R A B` asserts `F` is a type of bundled non-unital ⋆-algebra homomorphisms from `A` to `B`. -/ class non_unital_star_alg_hom_class (F : Type*) (R : out_param Type*) (A : out_param Type*) (B : out_param Type*) [monoid R] [has_star A] [has_star B] [non_unital_non_assoc_semiring A] [non_unital_non_assoc_semiring B] [distrib_mul_action R A] [distrib_mul_action R B] extends non_unital_alg_hom_class F R A B, star_hom_class F A B -- `R` becomes a metavariable but that's fine because it's an `out_param` attribute [nolint dangerous_instance] non_unital_star_alg_hom_class.to_star_hom_class namespace non_unital_star_alg_hom section basic variables {R A B C D : Type*} [monoid R] variables [non_unital_non_assoc_semiring A] [distrib_mul_action R A] [has_star A] variables [non_unital_non_assoc_semiring B] [distrib_mul_action R B] [has_star B] variables [non_unital_non_assoc_semiring C] [distrib_mul_action R C] [has_star C] variables [non_unital_non_assoc_semiring D] [distrib_mul_action R D] [has_star D] instance : non_unital_star_alg_hom_class (A →⋆ₙₐ[R] B) R A B := { coe := to_fun, coe_injective' := by rintro ⟨f, _⟩ ⟨g, _⟩ ⟨h⟩; congr, map_smul := Ξ» f, f.map_smul', map_add := Ξ» f, f.map_add', map_zero := Ξ» f, f.map_zero', map_mul := Ξ» f, f.map_mul', map_star := Ξ» f, f.map_star' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (A →⋆ₙₐ[R] B) (Ξ» _, A β†’ B) := fun_like.has_coe_to_fun initialize_simps_projections non_unital_star_alg_hom (to_fun β†’ apply) @[simp] lemma coe_to_non_unital_alg_hom {f : A →⋆ₙₐ[R] B} : (f.to_non_unital_alg_hom : A β†’ B) = f := rfl @[ext] lemma ext {f g : A →⋆ₙₐ[R] B} (h : βˆ€ x, f x = g x) : f = g := fun_like.ext _ _ h /-- Copy of a `non_unital_star_alg_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : A →⋆ₙₐ[R] B) (f' : A β†’ B) (h : f' = f) : A →⋆ₙₐ[R] B := { to_fun := f', map_smul' := h.symm β–Έ map_smul f, map_zero' := h.symm β–Έ map_zero f, map_add' := h.symm β–Έ map_add f, map_mul' := h.symm β–Έ map_mul f, map_star' := h.symm β–Έ map_star f } @[simp] lemma coe_mk (f : A β†’ B) (h₁ hβ‚‚ h₃ hβ‚„ hβ‚…) : ((⟨f, h₁, hβ‚‚, h₃, hβ‚„, hβ‚…βŸ© : A →⋆ₙₐ[R] B) : A β†’ B) = f := rfl @[simp] lemma mk_coe (f : A →⋆ₙₐ[R] B) (h₁ hβ‚‚ h₃ hβ‚„ hβ‚…) : (⟨f, h₁, hβ‚‚, h₃, hβ‚„, hβ‚…βŸ© : A →⋆ₙₐ[R] B) = f := by { ext, refl, } section variables (R A) /-- The identity as a non-unital ⋆-algebra homomorphism. -/ protected def id : A →⋆ₙₐ[R] A := { map_star' := Ξ» x, rfl, .. (1 : A →ₙₐ[R] A) } @[simp] lemma coe_id : ⇑(non_unital_star_alg_hom.id R A) = id := rfl end /-- The composition of non-unital ⋆-algebra homomorphisms, as a non-unital ⋆-algebra homomorphism. -/ def comp (f : B →⋆ₙₐ[R] C) (g : A →⋆ₙₐ[R] B) : A →⋆ₙₐ[R] C := { map_star' := by simp only [map_star, non_unital_alg_hom.to_fun_eq_coe, eq_self_iff_true, non_unital_alg_hom.coe_comp, coe_to_non_unital_alg_hom, function.comp_app, forall_const], .. f.to_non_unital_alg_hom.comp g.to_non_unital_alg_hom } @[simp] lemma coe_comp (f : B →⋆ₙₐ[R] C) (g : A →⋆ₙₐ[R] B) : ⇑(comp f g) = f ∘ g := rfl @[simp] lemma comp_apply (f : B →⋆ₙₐ[R] C) (g : A →⋆ₙₐ[R] B) (a : A) : comp f g a = f (g a) := rfl @[simp] lemma comp_assoc (f : C →⋆ₙₐ[R] D) (g : B →⋆ₙₐ[R] C) (h : A →⋆ₙₐ[R] B) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma id_comp (f : A →⋆ₙₐ[R] B) : (non_unital_star_alg_hom.id _ _).comp f = f := ext $ Ξ» _, rfl @[simp] lemma comp_id (f : A →⋆ₙₐ[R] B) : f.comp (non_unital_star_alg_hom.id _ _) = f := ext $ Ξ» _, rfl instance : monoid (A →⋆ₙₐ[R] A) := { mul := comp, mul_assoc := comp_assoc, one := non_unital_star_alg_hom.id R A, one_mul := id_comp, mul_one := comp_id, } @[simp] lemma coe_one : ((1 : A →⋆ₙₐ[R] A) : A β†’ A) = id := rfl lemma one_apply (a : A) : (1 : A →⋆ₙₐ[R] A) a = a := rfl end basic section zero -- the `zero` requires extra type class assumptions because we need `star_zero` variables {R A B C D : Type*} [monoid R] variables [non_unital_non_assoc_semiring A] [distrib_mul_action R A] [star_add_monoid A] variables [non_unital_non_assoc_semiring B] [distrib_mul_action R B] [star_add_monoid B] instance : has_zero (A →⋆ₙₐ[R] B) := ⟨{ map_star' := by simp, .. (0 : non_unital_alg_hom R A B) }⟩ instance : inhabited (A →⋆ₙₐ[R] B) := ⟨0⟩ instance : monoid_with_zero (A →⋆ₙₐ[R] A) := { zero_mul := Ξ» f, ext $ Ξ» x, rfl, mul_zero := Ξ» f, ext $ Ξ» x, map_zero f, .. non_unital_star_alg_hom.monoid, .. non_unital_star_alg_hom.has_zero } @[simp] lemma coe_zero : ((0 : A →⋆ₙₐ[R] B) : A β†’ B) = 0 := rfl lemma zero_apply (a : A) : (0 : A →⋆ₙₐ[R] B) a = 0 := rfl end zero end non_unital_star_alg_hom /-! ### Unital star algebra homomorphisms -/ section unital /-- A *⋆-algebra homomorphism* is an algebra homomorphism between `R`-algebras `A` and `B` equipped with a `star` operation, and this homomorphism is also `star`-preserving. -/ structure star_alg_hom (R A B: Type*) [comm_semiring R] [semiring A] [algebra R A] [has_star A] [semiring B] [algebra R B] [has_star B] extends alg_hom R A B := (map_star' : βˆ€ x : A, to_fun (star x) = star (to_fun x)) infixr ` →⋆ₐ `:25 := star_alg_hom _ notation A ` →⋆ₐ[`:25 R `] ` B := star_alg_hom R A B /-- Reinterpret a unital star algebra homomorphism as a unital algebra homomorphism by forgetting the interaction with the star operation. -/ add_decl_doc star_alg_hom.to_alg_hom /-- `star_alg_hom_class F R A B` states that `F` is a type of ⋆-algebra homomorphisms. You should also extend this typeclass when you extend `star_alg_hom`. -/ class star_alg_hom_class (F : Type*) (R : out_param Type*) (A : out_param Type*) (B : out_param Type*) [comm_semiring R] [semiring A] [algebra R A] [has_star A] [semiring B] [algebra R B] [has_star B] extends alg_hom_class F R A B, star_hom_class F A B -- `R` becomes a metavariable but that's fine because it's an `out_param` attribute [nolint dangerous_instance] star_alg_hom_class.to_star_hom_class @[priority 100] /- See note [lower instance priority] -/ instance star_alg_hom_class.to_non_unital_star_alg_hom_class (F R A B : Type*) [comm_semiring R] [semiring A] [algebra R A] [has_star A] [semiring B] [algebra R B] [has_star B] [star_alg_hom_class F R A B] : non_unital_star_alg_hom_class F R A B := { map_smul := map_smul, .. star_alg_hom_class.to_alg_hom_class F R A B, .. star_alg_hom_class.to_star_hom_class F R A B, } namespace star_alg_hom variables {F R A B C D : Type*} [comm_semiring R] [semiring A] [algebra R A] [has_star A] [semiring B] [algebra R B] [has_star B] [semiring C] [algebra R C] [has_star C] [semiring D] [algebra R D] [has_star D] instance : star_alg_hom_class (A →⋆ₐ[R] B) R A B := { coe := Ξ» f, f.to_fun, coe_injective' := Ξ» f g h, begin obtain ⟨_, _, _, _, _, _, _⟩ := f; obtain ⟨_, _, _, _, _, _, _⟩ := g; congr' end, map_mul := map_mul', map_one := map_one', map_add := map_add', map_zero := map_zero', commutes := commutes', map_star := map_star' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (A →⋆ₐ[R] B) (Ξ» _, A β†’ B) := fun_like.has_coe_to_fun @[simp] lemma coe_to_alg_hom {f : A →⋆ₐ[R] B} : (f.to_alg_hom : A β†’ B) = f := rfl @[ext] lemma ext {f g : A →⋆ₐ[R] B} (h : βˆ€ x, f x = g x) : f = g := fun_like.ext _ _ h /-- Copy of a `star_alg_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : A →⋆ₐ[R] B) (f' : A β†’ B) (h : f' = f) : A →⋆ₐ[R] B := { to_fun := f', map_one' := h.symm β–Έ map_one f , map_mul' := h.symm β–Έ map_mul f, map_zero' := h.symm β–Έ map_zero f, map_add' := h.symm β–Έ map_add f, commutes' := h.symm β–Έ alg_hom_class.commutes f, map_star' := h.symm β–Έ map_star f } @[simp] lemma coe_mk (f : A β†’ B) (h₁ hβ‚‚ h₃ hβ‚„ hβ‚… h₆) : ((⟨f, h₁, hβ‚‚, h₃, hβ‚„, hβ‚…, hβ‚†βŸ© : A →⋆ₐ[R] B) : A β†’ B) = f := rfl @[simp] lemma mk_coe (f : A →⋆ₐ[R] B) (h₁ hβ‚‚ h₃ hβ‚„ hβ‚… h₆) : (⟨f, h₁, hβ‚‚, h₃, hβ‚„, hβ‚…, hβ‚†βŸ© : A →⋆ₐ[R] B) = f := by { ext, refl, } section variables (R A) /-- The identity as a `star_alg_hom`. -/ protected def id : A →⋆ₐ[R] A := { map_star' := Ξ» x, rfl, .. alg_hom.id _ _ } @[simp] lemma coe_id : ⇑(star_alg_hom.id R A) = id := rfl end instance : inhabited (A →⋆ₐ[R] A) := ⟨star_alg_hom.id R A⟩ /-- The composition of ⋆-algebra homomorphisms, as a ⋆-algebra homomorphism. -/ def comp (f : B →⋆ₐ[R] C) (g : A →⋆ₐ[R] B) : A →⋆ₐ[R] C := { map_star' := by simp only [map_star, alg_hom.to_fun_eq_coe, alg_hom.coe_comp, coe_to_alg_hom, function.comp_app, eq_self_iff_true, forall_const], .. f.to_alg_hom.comp g.to_alg_hom } @[simp] lemma coe_comp (f : B →⋆ₐ[R] C) (g : A →⋆ₐ[R] B) : ⇑(comp f g) = f ∘ g := rfl @[simp] lemma comp_apply (f : B →⋆ₐ[R] C) (g : A →⋆ₐ[R] B) (a : A) : comp f g a = f (g a) := rfl @[simp] lemma comp_assoc (f : C →⋆ₐ[R] D) (g : B →⋆ₐ[R] C) (h : A →⋆ₐ[R] B) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma id_comp (f : A →⋆ₐ[R] B) : (star_alg_hom.id _ _).comp f = f := ext $ Ξ» _, rfl @[simp] lemma comp_id (f : A →⋆ₐ[R] B) : f.comp (star_alg_hom.id _ _) = f := ext $ Ξ» _, rfl instance : monoid (A →⋆ₐ[R] A) := { mul := comp, mul_assoc := comp_assoc, one := star_alg_hom.id R A, one_mul := id_comp, mul_one := comp_id } /-- A unital morphism of ⋆-algebras is a `non_unital_star_alg_hom`. -/ def to_non_unital_star_alg_hom (f : A →⋆ₐ[R] B) : A →⋆ₙₐ[R] B := { map_smul' := map_smul f, .. f, } @[simp] lemma coe_to_non_unital_star_alg_hom (f : A →⋆ₐ[R] B) : (f.to_non_unital_star_alg_hom : A β†’ B) = f := rfl end star_alg_hom end unital /-! ### Operations on the product type Note that this is copied from [`algebra/hom/non_unital_alg`](non_unital_alg). -/ namespace non_unital_star_alg_hom section prod variables (R A B C : Type*) [monoid R] [non_unital_non_assoc_semiring A] [distrib_mul_action R A] [has_star A] [non_unital_non_assoc_semiring B] [distrib_mul_action R B] [has_star B] [non_unital_non_assoc_semiring C] [distrib_mul_action R C] [has_star C] /-- The first projection of a product is a non-unital ⋆-algebra homomoprhism. -/ @[simps] def fst : A Γ— B →⋆ₙₐ[R] A := { map_star' := Ξ» x, rfl, .. non_unital_alg_hom.fst R A B } /-- The second projection of a product is a non-unital ⋆-algebra homomorphism. -/ @[simps] def snd : A Γ— B →⋆ₙₐ[R] B := { map_star' := Ξ» x, rfl, .. non_unital_alg_hom.snd R A B } variables {R A B C} /-- The `pi.prod` of two morphisms is a morphism. -/ @[simps] def prod (f : A →⋆ₙₐ[R] B) (g : A →⋆ₙₐ[R] C) : (A →⋆ₙₐ[R] B Γ— C) := { map_star' := Ξ» x, by simp [map_star, prod.star_def], .. f.to_non_unital_alg_hom.prod g.to_non_unital_alg_hom } lemma coe_prod (f : A →⋆ₙₐ[R] B) (g : A →⋆ₙₐ[R] C) : ⇑(f.prod g) = pi.prod f g := rfl @[simp] theorem fst_prod (f : A →⋆ₙₐ[R] B) (g : A →⋆ₙₐ[R] C) : (fst R B C).comp (prod f g) = f := by ext; refl @[simp] theorem snd_prod (f : A →⋆ₙₐ[R] B) (g : A →⋆ₙₐ[R] C) : (snd R B C).comp (prod f g) = g := by ext; refl @[simp] theorem prod_fst_snd : prod (fst R A B) (snd R A B) = 1 := fun_like.coe_injective pi.prod_fst_snd /-- Taking the product of two maps with the same domain is equivalent to taking the product of their codomains. -/ @[simps] def prod_equiv : ((A →⋆ₙₐ[R] B) Γ— (A →⋆ₙₐ[R] C)) ≃ (A →⋆ₙₐ[R] B Γ— C) := { to_fun := Ξ» f, f.1.prod f.2, inv_fun := Ξ» f, ((fst _ _ _).comp f, (snd _ _ _).comp f), left_inv := Ξ» f, by ext; refl, right_inv := Ξ» f, by ext; refl } end prod section inl_inr variables (R A B C : Type*) [monoid R] [non_unital_non_assoc_semiring A] [distrib_mul_action R A] [star_add_monoid A] [non_unital_non_assoc_semiring B] [distrib_mul_action R B] [star_add_monoid B] [non_unital_non_assoc_semiring C] [distrib_mul_action R C] [star_add_monoid C] /-- The left injection into a product is a non-unital algebra homomorphism. -/ def inl : A →⋆ₙₐ[R] A Γ— B := prod 1 0 /-- The right injection into a product is a non-unital algebra homomorphism. -/ def inr : B →⋆ₙₐ[R] A Γ— B := prod 0 1 variables {R A B} @[simp] theorem coe_inl : (inl R A B : A β†’ A Γ— B) = Ξ» x, (x, 0) := rfl theorem inl_apply (x : A) : inl R A B x = (x, 0) := rfl @[simp] theorem coe_inr : (inr R A B : B β†’ A Γ— B) = prod.mk 0 := rfl theorem inr_apply (x : B) : inr R A B x = (0, x) := rfl end inl_inr end non_unital_star_alg_hom namespace star_alg_hom variables (R A B C : Type*) [comm_semiring R] [semiring A] [algebra R A] [has_star A] [semiring B] [algebra R B] [has_star B] [semiring C] [algebra R C] [has_star C] /-- The first projection of a product is a ⋆-algebra homomoprhism. -/ @[simps] def fst : A Γ— B →⋆ₐ[R] A := { map_star' := Ξ» x, rfl, .. alg_hom.fst R A B } /-- The second projection of a product is a ⋆-algebra homomorphism. -/ @[simps] def snd : A Γ— B →⋆ₐ[R] B := { map_star' := Ξ» x, rfl, .. alg_hom.snd R A B } variables {R A B C} /-- The `pi.prod` of two morphisms is a morphism. -/ @[simps] def prod (f : A →⋆ₐ[R] B) (g : A →⋆ₐ[R] C) : (A →⋆ₐ[R] B Γ— C) := { map_star' := Ξ» x, by simp [prod.star_def, map_star], .. f.to_alg_hom.prod g.to_alg_hom } lemma coe_prod (f : A →⋆ₐ[R] B) (g : A →⋆ₐ[R] C) : ⇑(f.prod g) = pi.prod f g := rfl @[simp] theorem fst_prod (f : A →⋆ₐ[R] B) (g : A →⋆ₐ[R] C) : (fst R B C).comp (prod f g) = f := by ext; refl @[simp] theorem snd_prod (f : A →⋆ₐ[R] B) (g : A →⋆ₐ[R] C) : (snd R B C).comp (prod f g) = g := by ext; refl @[simp] theorem prod_fst_snd : prod (fst R A B) (snd R A B) = 1 := fun_like.coe_injective pi.prod_fst_snd /-- Taking the product of two maps with the same domain is equivalent to taking the product of their codomains. -/ @[simps] def prod_equiv : ((A →⋆ₐ[R] B) Γ— (A →⋆ₐ[R] C)) ≃ (A →⋆ₐ[R] B Γ— C) := { to_fun := Ξ» f, f.1.prod f.2, inv_fun := Ξ» f, ((fst _ _ _).comp f, (snd _ _ _).comp f), left_inv := Ξ» f, by ext; refl, right_inv := Ξ» f, by ext; refl } end star_alg_hom
a1f2c6ded5581073b08720ea03678eb86b7885eb
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/order/galois_connection.lean
1cf73b9b814218da63cb17034f1d375be6a5fa8e
[ "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
24,886
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes HΓΆlzl -/ import order.complete_lattice import order.rel_iso /-! # Galois connections, insertions and coinsertions Galois connections are order theoretic adjoints, i.e. a pair of functions `u` and `l`, such that `βˆ€a b, l a ≀ b ↔ a ≀ u b`. ## Main definitions * `galois_connection`: A Galois connection is a pair of functions `l` and `u` satisfying `l a ≀ b ↔ a ≀ u b`. They are special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. * `galois_insertion`: A Galois insertion is a Galois connection where `l ∘ u = id` * `galois_coinsertion`: A Galois coinsertion is a Galois connection where `u ∘ l = id` ## Implementation details Galois insertions can be used to lift order structures from one type to another. For example if `Ξ±` is a complete lattice, and `l : Ξ± β†’ Ξ²`, and `u : Ξ² β†’ Ξ±` form a Galois insertion, then `Ξ²` is also a complete lattice. `l` is the lower adjoint and `u` is the upper adjoint. An example of this is the Galois insertion is in group thery. If `G` is a topological space, then there is a Galois insertion between the set of subsets of `G`, `set G`, and the set of subgroups of `G`, `subgroup G`. The left adjoint is `subgroup.closure`, taking the `subgroup` generated by a `set`, The right adjoint is the coercion from `subgroup G` to `set G`, taking the underlying set of an subgroup. Naively lifting a lattice structure along this Galois insertion would mean that the definition of `inf` on subgroups would be `subgroup.closure (↑S ∩ ↑T)`. This is an undesirable definition because the intersection of subgroups is already a subgroup, so there is no need to take the closure. For this reason a `choice` function is added as a field to the `galois_insertion` structure. It has type `Ξ  S : set G, ↑(closure S) ≀ S β†’ subgroup G`. When `↑(closure S) ≀ S`, then `S` is already a subgroup, so this function can be defined using `subgroup.mk` and not `closure`. This means the infimum of subgroups will be defined to be the intersection of sets, paired with a proof that intersection of subgroups is a subgroup, rather than the closure of the intersection. -/ open function set universes u v w x variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {ΞΉ : Sort x} {a a₁ aβ‚‚ : Ξ±} {b b₁ bβ‚‚ : Ξ²} /-- A Galois connection is a pair of functions `l` and `u` satisfying `l a ≀ b ↔ a ≀ u b`. They are special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. -/ def galois_connection [preorder Ξ±] [preorder Ξ²] (l : Ξ± β†’ Ξ²) (u : Ξ² β†’ Ξ±) := βˆ€a b, l a ≀ b ↔ a ≀ u b /-- Makes a Galois connection from an order-preserving bijection. -/ theorem order_iso.to_galois_connection [preorder Ξ±] [preorder Ξ²] (oi : Ξ± ≃o Ξ²) : galois_connection oi oi.symm := Ξ» b g, by rw [oi.map_rel_iff, rel_iso.apply_symm_apply] namespace galois_connection section variables [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) lemma monotone_intro (hu : monotone u) (hl : monotone l) (hul : βˆ€ a, a ≀ u (l a)) (hlu : βˆ€ a, l (u a) ≀ a) : galois_connection l u := assume a b, ⟨assume h, le_trans (hul _) (hu h), assume h, le_trans (hl h) (hlu _)⟩ include gc lemma l_le {a : Ξ±} {b : Ξ²} : a ≀ u b β†’ l a ≀ b := (gc _ _).mpr lemma le_u {a : Ξ±} {b : Ξ²} : l a ≀ b β†’ a ≀ u b := (gc _ _).mp lemma le_u_l (a) : a ≀ u (l a) := gc.le_u $ le_refl _ lemma l_u_le (a) : l (u a) ≀ a := gc.l_le $ le_refl _ lemma monotone_u : monotone u := assume a b H, gc.le_u (le_trans (gc.l_u_le a) H) lemma monotone_l : monotone l := assume a b H, gc.l_le (le_trans H (gc.le_u_l b)) lemma upper_bounds_l_image_subset {s : set Ξ±} : upper_bounds (l '' s) βŠ† u ⁻¹' upper_bounds s := assume b hb c, assume : c ∈ s, gc.le_u (hb (mem_image_of_mem _ β€Ήc ∈ sβ€Ί)) lemma lower_bounds_u_image_subset {s : set Ξ²} : lower_bounds (u '' s) βŠ† l ⁻¹' lower_bounds s := assume a ha c, assume : c ∈ s, gc.l_le (ha (mem_image_of_mem _ β€Ήc ∈ sβ€Ί)) lemma is_lub_l_image {s : set Ξ±} {a : Ξ±} (h : is_lub s a) : is_lub (l '' s) (l a) := ⟨gc.monotone_l.mem_upper_bounds_image $ and.elim_left β€Ήis_lub s aβ€Ί, assume b hb, gc.l_le $ and.elim_right β€Ήis_lub s aβ€Ί $ gc.upper_bounds_l_image_subset hb⟩ lemma is_glb_u_image {s : set Ξ²} {b : Ξ²} (h : is_glb s b) : is_glb (u '' s) (u b) := ⟨gc.monotone_u.mem_lower_bounds_image $ and.elim_left β€Ήis_glb s bβ€Ί, assume a ha, gc.le_u $ and.elim_right β€Ήis_glb s bβ€Ί $ gc.lower_bounds_u_image_subset ha⟩ lemma is_glb_l {a : Ξ±} : is_glb { b | a ≀ u b } (l a) := ⟨assume b, gc.l_le, assume b h, h $ gc.le_u_l _⟩ lemma is_lub_u {b : Ξ²} : is_lub { a | l a ≀ b } (u b) := ⟨assume b, gc.le_u, assume b h, h $ gc.l_u_le _⟩ end section partial_order variables [partial_order Ξ±] [partial_order Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) include gc lemma u_l_u_eq_u : u ∘ l ∘ u = u := funext (assume x, le_antisymm (gc.monotone_u (gc.l_u_le _)) (gc.le_u_l _)) lemma l_u_l_eq_l : l ∘ u ∘ l = l := funext (assume x, le_antisymm (gc.l_u_le _) (gc.monotone_l (gc.le_u_l _))) lemma l_unique {l' : Ξ± β†’ Ξ²} {u' : Ξ² β†’ Ξ±} (gc' : galois_connection l' u') (hu : βˆ€ b, u b = u' b) {a : Ξ±} : l a = l' a := le_antisymm (gc.l_le $ (hu (l' a)).symm β–Έ gc'.le_u_l _) (gc'.l_le $ hu (l a) β–Έ gc.le_u_l _) lemma u_unique {l' : Ξ± β†’ Ξ²} {u' : Ξ² β†’ Ξ±} (gc' : galois_connection l' u') (hl : βˆ€ a, l a = l' a) {b : Ξ²} : u b = u' b := le_antisymm (gc'.le_u $ hl (u b) β–Έ gc.l_u_le _) (gc.le_u $ (hl (u' b)).symm β–Έ gc'.l_u_le _) end partial_order section order_top variables [order_top Ξ±] [order_top Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) include gc lemma u_top : u ⊀ = ⊀ := (gc.is_glb_u_image is_glb_empty).unique $ by simp only [is_glb_empty, image_empty] end order_top section order_bot variables [order_bot Ξ±] [order_bot Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) include gc lemma l_bot : l βŠ₯ = βŠ₯ := (gc.is_lub_l_image is_lub_empty).unique $ by simp only [is_lub_empty, image_empty] end order_bot section semilattice_sup variables [semilattice_sup Ξ±] [semilattice_sup Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) include gc lemma l_sup : l (a₁ βŠ” aβ‚‚) = l a₁ βŠ” l aβ‚‚ := (gc.is_lub_l_image is_lub_pair).unique $ by simp only [image_pair, is_lub_pair] end semilattice_sup section semilattice_inf variables [semilattice_inf Ξ±] [semilattice_inf Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) include gc lemma u_inf : u (b₁ βŠ“ bβ‚‚) = u b₁ βŠ“ u bβ‚‚ := (gc.is_glb_u_image is_glb_pair).unique $ by simp only [image_pair, is_glb_pair] end semilattice_inf section complete_lattice variables [complete_lattice Ξ±] [complete_lattice Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) include gc lemma l_supr {f : ΞΉ β†’ Ξ±} : l (supr f) = (⨆i, l (f i)) := eq.symm $ is_lub.supr_eq $ show is_lub (range (l ∘ f)) (l (supr f)), by rw [range_comp, ← Sup_range]; exact gc.is_lub_l_image (is_lub_Sup _) lemma u_infi {f : ΞΉ β†’ Ξ²} : u (infi f) = (β¨…i, u (f i)) := eq.symm $ is_glb.infi_eq $ show is_glb (range (u ∘ f)) (u (infi f)), by rw [range_comp, ← Inf_range]; exact gc.is_glb_u_image (is_glb_Inf _) lemma l_Sup {s : set Ξ±} : l (Sup s) = (⨆a∈s, l a) := by simp only [Sup_eq_supr, gc.l_supr] lemma u_Inf {s : set Ξ²} : u (Inf s) = (β¨…a∈s, u a) := by simp only [Inf_eq_infi, gc.u_infi] end complete_lattice /- Constructing Galois connections -/ section constructions protected lemma id [pΞ± : preorder Ξ±] : @galois_connection Ξ± Ξ± pΞ± pΞ± id id := assume a b, iff.intro (Ξ»x, x) (Ξ»x, x) protected lemma compose [preorder Ξ±] [preorder Ξ²] [preorder Ξ³] (l1 : Ξ± β†’ Ξ²) (u1 : Ξ² β†’ Ξ±) (l2 : Ξ² β†’ Ξ³) (u2 : Ξ³ β†’ Ξ²) (gc1 : galois_connection l1 u1) (gc2 : galois_connection l2 u2) : galois_connection (l2 ∘ l1) (u1 ∘ u2) := by intros a b; rw [gc2, gc1] protected lemma dual [pΞ± : preorder Ξ±] [pΞ² : preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : @galois_connection (order_dual Ξ²) (order_dual Ξ±) _ _ u l := assume a b, (gc _ _).symm protected lemma dfun {ΞΉ : Type u} {Ξ± : ΞΉ β†’ Type v} {Ξ² : ΞΉ β†’ Type w} [βˆ€i, preorder (Ξ± i)] [βˆ€i, preorder (Ξ² i)] (l : Ξ i, Ξ± i β†’ Ξ² i) (u : Ξ i, Ξ² i β†’ Ξ± i) (gc : βˆ€i, galois_connection (l i) (u i)) : @galois_connection (Ξ  i, Ξ± i) (Ξ  i, Ξ² i) _ _ (Ξ»a i, l i (a i)) (Ξ»b i, u i (b i)) := assume a b, forall_congr $ assume i, gc i (a i) (b i) end constructions end galois_connection namespace nat lemma galois_connection_mul_div {k : β„•} (h : 0 < k) : galois_connection (Ξ»n, n * k) (Ξ»n, n / k) := assume x y, (le_div_iff_mul_le x y h).symm end nat /-- A Galois insertion is a Galois connection where `l ∘ u = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. Dual to `galois_coinsertion` -/ @[nolint has_inhabited_instance] structure galois_insertion {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] (l : Ξ± β†’ Ξ²) (u : Ξ² β†’ Ξ±) := (choice : Ξ x:Ξ±, u (l x) ≀ x β†’ Ξ²) (gc : galois_connection l u) (le_l_u : βˆ€x, x ≀ l (u x)) (choice_eq : βˆ€a h, choice a h = l a) /-- A constructor for a Galois insertion with the trivial `choice` function. -/ def galois_insertion.monotone_intro {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (hu : monotone u) (hl : monotone l) (hul : βˆ€ a, a ≀ u (l a)) (hlu : βˆ€ b, l (u b) = b) : galois_insertion l u := { choice := Ξ» x _, l x, gc := galois_connection.monotone_intro hu hl hul (Ξ» b, le_of_eq (hlu b)), le_l_u := Ξ» b, le_of_eq $ (hlu b).symm, choice_eq := Ξ» _ _, rfl } /-- Makes a Galois insertion from an order-preserving bijection. -/ protected def rel_iso.to_galois_insertion [preorder Ξ±] [preorder Ξ²] (oi : Ξ± ≃o Ξ²) : @galois_insertion Ξ± Ξ² _ _ (oi) (oi.symm) := { choice := Ξ» b h, oi b, gc := oi.to_galois_connection, le_l_u := Ξ» g, le_of_eq (oi.right_inv g).symm, choice_eq := Ξ» b h, rfl } /-- Make a `galois_insertion l u` from a `galois_connection l u` such that `βˆ€ b, b ≀ l (u b)` -/ def galois_connection.to_galois_insertion {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) (h : βˆ€ b, b ≀ l (u b)) : galois_insertion l u := { choice := Ξ» x _, l x, gc := gc, le_l_u := h, choice_eq := Ξ» _ _, rfl } /-- Lift the bottom along a Galois connection -/ def galois_connection.lift_order_bot {Ξ± Ξ² : Type*} [order_bot Ξ±] [partial_order Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : order_bot Ξ² := { bot := l βŠ₯, bot_le := assume b, gc.l_le $ bot_le, .. β€Ήpartial_order Ξ²β€Ί } namespace galois_insertion variables {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} lemma l_u_eq [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) (b : Ξ²) : l (u b) = b := le_antisymm (gi.gc.l_u_le _) (gi.le_l_u _) lemma l_surjective [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) : surjective l := assume b, ⟨u b, gi.l_u_eq b⟩ lemma u_injective [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) : injective u := assume a b h, calc a = l (u a) : (gi.l_u_eq a).symm ... = l (u b) : congr_arg l h ... = b : gi.l_u_eq b lemma l_sup_u [semilattice_sup Ξ±] [semilattice_sup Ξ²] (gi : galois_insertion l u) (a b : Ξ²) : l (u a βŠ” u b) = a βŠ” b := calc l (u a βŠ” u b) = l (u a) βŠ” l (u b) : gi.gc.l_sup ... = a βŠ” b : by simp only [gi.l_u_eq] lemma l_supr_u [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ²) : l (⨆ i, u (f i)) = ⨆ i, (f i) := calc l (⨆ (i : ΞΉ), u (f i)) = ⨆ (i : ΞΉ), l (u (f i)) : gi.gc.l_supr ... = ⨆ (i : ΞΉ), f i : congr_arg _ $ funext $ Ξ» i, gi.l_u_eq (f i) lemma l_supr_of_ul_eq_self [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ±) (hf : βˆ€ i, u (l (f i)) = f i) : l (⨆ i, (f i)) = ⨆ i, l (f i) := calc l (⨆ (i : ΞΉ), (f i)) = l ⨆ (i : ΞΉ), (u (l (f i))) : by simp [hf] ... = ⨆ (i : ΞΉ), l (f i) : gi.l_supr_u _ lemma l_inf_u [semilattice_inf Ξ±] [semilattice_inf Ξ²] (gi : galois_insertion l u) (a b : Ξ²) : l (u a βŠ“ u b) = a βŠ“ b := calc l (u a βŠ“ u b) = l (u (a βŠ“ b)) : congr_arg l gi.gc.u_inf.symm ... = a βŠ“ b : by simp only [gi.l_u_eq] lemma l_infi_u [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ²) : l (β¨… i, u (f i)) = β¨… i, (f i) := calc l (β¨… (i : ΞΉ), u (f i)) = l (u (β¨… (i : ΞΉ), (f i))) : congr_arg l gi.gc.u_infi.symm ... = β¨… (i : ΞΉ), f i : gi.l_u_eq _ lemma l_infi_of_ul_eq_self [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ±) (hf : βˆ€ i, u (l (f i)) = f i) : l (β¨… i, (f i)) = β¨… i, l (f i) := calc l (β¨… i, (f i)) = l β¨… (i : ΞΉ), (u (l (f i))) : by simp [hf] ... = β¨… i, l (f i) : gi.l_infi_u _ lemma u_le_u_iff [preorder Ξ±] [preorder Ξ²] (gi : galois_insertion l u) {a b} : u a ≀ u b ↔ a ≀ b := ⟨λ h, le_trans (gi.le_l_u _) (gi.gc.l_le h), Ξ» h, gi.gc.monotone_u h⟩ lemma strict_mono_u [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) : strict_mono u := strict_mono_of_le_iff_le $ Ξ» _ _, gi.u_le_u_iff.symm section lift variables [partial_order Ξ²] /-- Lift the suprema along a Galois insertion -/ def lift_semilattice_sup [semilattice_sup Ξ±] (gi : galois_insertion l u) : semilattice_sup Ξ² := { sup := Ξ»a b, l (u a βŠ” u b), le_sup_left := assume a b, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_sup_left, le_sup_right := assume a b, le_trans (gi.le_l_u b) $ gi.gc.monotone_l $ le_sup_right, sup_le := assume a b c hac hbc, gi.gc.l_le $ sup_le (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc), .. β€Ήpartial_order Ξ²β€Ί } /-- Lift the infima along a Galois insertion -/ def lift_semilattice_inf [semilattice_inf Ξ±] (gi : galois_insertion l u) : semilattice_inf Ξ² := { inf := Ξ»a b, gi.choice (u a βŠ“ u b) $ (le_inf (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_left) (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_right)), inf_le_left := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_left, inf_le_right := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_right, le_inf := by simp only [gi.choice_eq]; exact assume a b c hac hbc, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_inf (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc), .. β€Ήpartial_order Ξ²β€Ί } /-- Lift the suprema and infima along a Galois insertion -/ def lift_lattice [lattice Ξ±] (gi : galois_insertion l u) : lattice Ξ² := { .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf } /-- Lift the top along a Galois insertion -/ def lift_order_top [order_top Ξ±] (gi : galois_insertion l u) : order_top Ξ² := { top := gi.choice ⊀ $ le_top, le_top := by simp only [gi.choice_eq]; exact assume b, le_trans (gi.le_l_u b) (gi.gc.monotone_l le_top), .. β€Ήpartial_order Ξ²β€Ί } /-- Lift the top, bottom, suprema, and infima along a Galois insertion -/ def lift_bounded_lattice [bounded_lattice Ξ±] (gi : galois_insertion l u) : bounded_lattice Ξ² := { .. gi.lift_lattice, .. gi.lift_order_top, .. gi.gc.lift_order_bot } /-- Lift all suprema and infima along a Galois insertion -/ def lift_complete_lattice [complete_lattice Ξ±] (gi : galois_insertion l u) : complete_lattice Ξ² := { Sup := Ξ»s, l (⨆ b∈s, u b), Sup_le := assume s a hs, gi.gc.l_le $ supr_le $ assume b, supr_le $ assume hb, gi.gc.monotone_u $ hs _ hb, le_Sup := assume s a ha, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_supr_of_le a $ le_supr_of_le ha $ le_refl _, Inf := Ξ»s, gi.choice (β¨… b∈s, u b) $ le_infi $ assume b, le_infi $ assume hb, gi.gc.monotone_u $ gi.gc.l_le $ infi_le_of_le b $ infi_le_of_le hb $ le_refl _, Inf_le := by simp only [gi.choice_eq]; exact assume s a ha, gi.gc.l_le $ infi_le_of_le a $ infi_le_of_le ha $ le_refl _, le_Inf := by simp only [gi.choice_eq]; exact assume s a hs, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_infi $ assume b, show u a ≀ β¨… (H : b ∈ s), u b, from le_infi $ assume hb, gi.gc.monotone_u $ hs _ hb, .. gi.lift_bounded_lattice } end lift end galois_insertion /-- A Galois coinsertion is a Galois connection where `u ∘ l = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. Dual to `galois_insertion` -/ @[nolint has_inhabited_instance] structure galois_coinsertion {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] (l : Ξ± β†’ Ξ²) (u : Ξ² β†’ Ξ±) := (choice : Ξ x:Ξ², x ≀ l (u x) β†’ Ξ±) (gc : galois_connection l u) (u_l_le : βˆ€x, u (l x) ≀ x) (choice_eq : βˆ€a h, choice a h = u a) /-- Make a `galois_insertion u l` in the `order_dual`, from a `galois_coinsertion l u` -/ def galois_coinsertion.dual {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} : galois_coinsertion l u β†’ @galois_insertion (order_dual Ξ²) (order_dual Ξ±) _ _ u l := Ξ» x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_coinsertion u l` in the `order_dual`, from a `galois_insertion l u` -/ def galois_insertion.dual {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} : galois_insertion l u β†’ @galois_coinsertion (order_dual Ξ²) (order_dual Ξ±) _ _ u l := Ξ» x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_coinsertion l u` from a `galois_insertion l u` in the `order_dual` -/ def galois_coinsertion.of_dual {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} : @galois_insertion (order_dual Ξ²) (order_dual Ξ±) _ _ u l β†’ galois_coinsertion l u := Ξ» x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_insertion l u` from a `galois_coinsertion l u` in the `order_dual` -/ def galois_insertion.of_dual {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} : @galois_coinsertion (order_dual Ξ²) (order_dual Ξ±) _ _ u l β†’ galois_insertion l u := Ξ» x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Makes a Galois coinsertion from an order-preserving bijection. -/ protected def rel_iso.to_galois_coinsertion [preorder Ξ±] [preorder Ξ²] (oi : Ξ± ≃o Ξ²) : @galois_coinsertion Ξ± Ξ² _ _ (oi) (oi.symm) := { choice := Ξ» b h, oi.symm b, gc := oi.to_galois_connection, u_l_le := Ξ» g, le_of_eq (oi.left_inv g), choice_eq := Ξ» b h, rfl } /-- A constructor for a Galois coinsertion with the trivial `choice` function. -/ def galois_coinsertion.monotone_intro {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (hu : monotone u) (hl : monotone l) (hlu : βˆ€ b, l (u b) ≀ b) (hul : βˆ€ a, u (l a) = a) : galois_coinsertion l u := galois_coinsertion.of_dual (galois_insertion.monotone_intro hl.order_dual hu.order_dual hlu hul) /-- Make a `galois_coinsertion l u` from a `galois_connection l u` such that `βˆ€ b, b ≀ l (u b)` -/ def galois_connection.to_galois_coinsertion {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) (h : βˆ€ a, u (l a) ≀ a) : galois_coinsertion l u := { choice := Ξ» x _, u x, gc := gc, u_l_le := h, choice_eq := Ξ» _ _, rfl } /-- Lift the top along a Galois connection -/ def galois_connection.lift_order_top {Ξ± Ξ² : Type*} [partial_order Ξ±] [order_top Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : order_top Ξ± := { top := u ⊀, le_top := assume b, gc.le_u $ le_top, .. β€Ήpartial_order Ξ±β€Ί } namespace galois_coinsertion variables {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} lemma u_l_eq [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) (a : Ξ±) : u (l a) = a := gi.dual.l_u_eq a lemma u_surjective [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) : surjective u := gi.dual.l_surjective lemma l_injective [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) : injective l := gi.dual.u_injective lemma u_inf_l [semilattice_inf Ξ±] [semilattice_inf Ξ²] (gi : galois_coinsertion l u) (a b : Ξ±) : u (l a βŠ“ l b) = a βŠ“ b := gi.dual.l_sup_u a b lemma u_infi_l [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ±) : u (β¨… i, l (f i)) = β¨… i, (f i) := gi.dual.l_supr_u _ lemma u_infi_of_lu_eq_self [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ²) (hf : βˆ€ i, l (u (f i)) = f i) : u (β¨… i, (f i)) = β¨… i, u (f i) := gi.dual.l_supr_of_ul_eq_self _ hf lemma u_sup_l [semilattice_sup Ξ±] [semilattice_sup Ξ²] (gi : galois_coinsertion l u) (a b : Ξ±) : u (l a βŠ” l b) = a βŠ” b := gi.dual.l_inf_u _ _ lemma u_supr_l [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ±) : u (⨆ i, l (f i)) = ⨆ i, (f i) := gi.dual.l_infi_u _ lemma u_supr_of_lu_eq_self [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ²) (hf : βˆ€ i, l (u (f i)) = f i) : u (⨆ i, (f i)) = ⨆ i, u (f i) := gi.dual.l_infi_of_ul_eq_self _ hf lemma l_le_l_iff [preorder Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) {a b} : l a ≀ l b ↔ a ≀ b := gi.dual.u_le_u_iff lemma strict_mono_l [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) : strict_mono l := Ξ» a b h, gi.dual.strict_mono_u h section lift variables [partial_order Ξ±] /-- Lift the infima along a Galois coinsertion -/ def lift_semilattice_inf [semilattice_inf Ξ²] (gi : galois_coinsertion l u) : semilattice_inf Ξ± := { inf := Ξ»a b, u (l a βŠ“ l b), inf_le_left := assume a b, le_trans (gi.gc.monotone_u $ inf_le_left) (gi.u_l_le a), inf_le_right := assume a b, le_trans (gi.gc.monotone_u $ inf_le_right) (gi.u_l_le b), le_inf := assume a b c hac hbc, gi.gc.le_u $ le_inf (gi.gc.monotone_l hac) (gi.gc.monotone_l hbc), .. β€Ήpartial_order Ξ±β€Ί } /-- Lift the suprema along a Galois coinsertion -/ def lift_semilattice_sup [semilattice_sup Ξ²] (gi : galois_coinsertion l u) : semilattice_sup Ξ± := { sup := Ξ»a b, gi.choice (l a βŠ” l b) $ (sup_le (gi.gc.monotone_l $ gi.gc.le_u $ le_sup_left) (gi.gc.monotone_l $ gi.gc.le_u $ le_sup_right)), le_sup_left := by simp only [gi.choice_eq]; exact assume a b, gi.gc.le_u le_sup_left, le_sup_right := by simp only [gi.choice_eq]; exact assume a b, gi.gc.le_u le_sup_right, sup_le := by simp only [gi.choice_eq]; exact assume a b c hac hbc, le_trans (gi.gc.monotone_u $ sup_le (gi.gc.monotone_l hac) (gi.gc.monotone_l hbc)) (gi.u_l_le c), .. β€Ήpartial_order Ξ±β€Ί } /-- Lift the suprema and infima along a Galois coinsertion -/ def lift_lattice [lattice Ξ²] (gi : galois_coinsertion l u) : lattice Ξ± := { .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf } /-- Lift the bot along a Galois coinsertion -/ def lift_order_bot [order_bot Ξ²] (gi : galois_coinsertion l u) : order_bot Ξ± := { bot := gi.choice βŠ₯ $ bot_le, bot_le := by simp only [gi.choice_eq]; exact assume b, le_trans (gi.gc.monotone_u bot_le) (gi.u_l_le b), .. β€Ήpartial_order Ξ±β€Ί } /-- Lift the top, bottom, suprema, and infima along a Galois coinsertion -/ def lift_bounded_lattice [bounded_lattice Ξ²] (gi : galois_coinsertion l u) : bounded_lattice Ξ± := { .. gi.lift_lattice, .. gi.lift_order_bot, .. gi.gc.lift_order_top } /-- Lift all suprema and infima along a Galois coinsertion -/ def lift_complete_lattice [complete_lattice Ξ²] (gi : galois_coinsertion l u) : complete_lattice Ξ± := { Inf := Ξ»s, u (β¨… a∈s, l a), le_Inf := assume s a hs, gi.gc.le_u $ le_infi $ assume b, le_infi $ assume hb, gi.gc.monotone_l $ hs _ hb, Inf_le := assume s a ha, le_trans (gi.gc.monotone_u $ infi_le_of_le a $ infi_le_of_le ha $ le_refl _) (gi.u_l_le a), Sup := Ξ»s, gi.choice (⨆ a∈s, l a) $ supr_le $ assume b, supr_le $ assume hb, gi.gc.monotone_l $ gi.gc.le_u $ le_supr_of_le b $ le_supr_of_le hb $ le_refl _, le_Sup := by simp only [gi.choice_eq]; exact assume s a ha, gi.gc.le_u $ le_supr_of_le a $ le_supr_of_le ha $ le_refl _, Sup_le := by simp only [gi.choice_eq]; exact assume s a hs, le_trans (gi.gc.monotone_u $ supr_le $ assume b, show (⨆ (hb : b ∈ s), l b) ≀ l a, from supr_le $ assume hb, gi.gc.monotone_l $ hs b hb) (gi.u_l_le a), .. gi.lift_bounded_lattice } end lift end galois_coinsertion
1b089ff01f2d0c4bb95e43110e1215806df47b7e
63abd62053d479eae5abf4951554e1064a4c45b4
/test/simps.lean
353fe73675e008e2c10fd69ca8bc46e12bb72a25
[ "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
24,735
lean
import tactic.simps universe variables v u w -- set_option trace.simps.verbose true -- set_option trace.app_builder true open function tactic expr structure equiv (Ξ± : Sort*) (Ξ² : Sort*) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) local infix ` ≃ `:25 := equiv /- Since `prod` and `pprod` are a special case for `@[simps]`, we define a new structure to test the basic functionality.-/ structure my_prod (Ξ± Ξ² : Type*) := (fst : Ξ±) (snd : Ξ²) def myprod.map {Ξ± Ξ±' Ξ² Ξ²'} (f : Ξ± β†’ Ξ±') (g : Ξ² β†’ Ξ²') (x : my_prod Ξ± Ξ²) : my_prod Ξ±' Ξ²' := ⟨f x.1, g x.2⟩ namespace foo @[simps] protected def rfl {Ξ±} : Ξ± ≃ Ξ± := ⟨id, Ξ» x, x, Ξ» x, rfl, Ξ» x, rfl⟩ /- simps adds declarations -/ run_cmd do e ← get_env, e.get `foo.rfl_to_fun, e.get `foo.rfl_inv_fun, success_if_fail (e.get `foo.rfl_left_inv), success_if_fail (e.get `foo.rfl_right_inv) example (n : β„•) : foo.rfl.to_fun n = n := by rw [foo.rfl_to_fun, id] example (n : β„•) : foo.rfl.inv_fun n = n := by rw [foo.rfl_inv_fun] /- the declarations are simp-lemmas -/ @[simps] def foo : β„• Γ— β„€ := (1, 2) example : foo.1 = 1 := by simp example : foo.2 = 2 := by simp example : foo.1 = 1 := by { dsimp, refl } -- check that dsimp also unfolds example : foo.2 = 2 := by { dsimp, refl } example {Ξ±} (x : Ξ±) : foo.rfl.to_fun x = x := by simp example {Ξ±} (x : Ξ±) : foo.rfl.inv_fun x = x := by simp example {Ξ±} (x : Ξ±) : foo.rfl.to_fun = @id Ξ± := by { success_if_fail {simp}, refl } /- check some failures -/ def bar1 : β„• := 1 -- type is not a structure def bar2 : β„• Γ— β„€ := prod.map (Ξ» x, x + 2) (Ξ» y, y - 3) (3, 4) -- value is not a constructor noncomputable def bar3 {Ξ±} : Ξ± ≃ Ξ± := classical.choice ⟨foo.rfl⟩ run_cmd do success_if_fail_with_msg (simps_tac `foo.bar1) "Invalid `simps` attribute. Target is not a structure", success_if_fail_with_msg (simps_tac `foo.bar2) "Invalid `simps` attribute. The body is not a constructor application: prod.map (Ξ» (x : β„•), x + 2) (Ξ» (y : β„€), y - 3) (3, 4) Possible solution: add option {rhs_md := semireducible}. The option {simp_rhs := tt} might also be useful to simplify the right-hand side.", success_if_fail_with_msg (simps_tac `foo.bar3) "Invalid `simps` attribute. The body is not a constructor application: classical.choice bar3._proof_1 Possible solution: add option {rhs_md := semireducible}. The option {simp_rhs := tt} might also be useful to simplify the right-hand side.", e ← get_env, let nm := `foo.bar1, d ← e.get nm, let lhs : expr := const d.to_name (d.univ_params.map level.param), simps_add_projections e nm "" d.type lhs d.value [] d.univ_params ff {} [] /- test `rhs_md` option -/ def rfl2 {Ξ±} : Ξ± ≃ Ξ± := foo.rfl run_cmd success_if_fail (simps_tac `foo.rfl2) attribute [simps {rhs_md := semireducible}] foo.rfl2 /- test `fully_applied` option -/ @[simps {fully_applied := ff}] def rfl3 {Ξ±} : Ξ± ≃ Ξ± := ⟨id, Ξ» x, x, Ξ» x, rfl, Ξ» x, rfl⟩ end foo /- we reduce the type when applying [simps] -/ def my_equiv := equiv @[simps] def baz : my_equiv β„• β„• := ⟨id, Ξ» x, x, Ξ» x, rfl, Ξ» x, rfl⟩ /- test name clashes -/ def name_clash_fst := 1 def name_clash_snd := 1 def name_clash_snd_2 := 1 @[simps] def name_clash := (2, 3) run_cmd do e ← get_env, e.get `name_clash_fst_2, e.get `name_clash_snd_3 /- check projections for nested structures -/ namespace count_nested @[simps {attrs := [`simp, `norm]}] def nested1 : my_prod β„• $ my_prod β„€ β„• := ⟨2, -1, 1⟩ @[simps {attrs := []}] def nested2 : β„• Γ— my_prod β„• β„• := ⟨2, myprod.map nat.succ nat.pred ⟨1, 2⟩⟩ end count_nested run_cmd do e ← get_env, e.get `count_nested.nested1_fst, e.get `count_nested.nested1_snd_fst, e.get `count_nested.nested1_snd_snd, e.get `count_nested.nested2_fst, e.get `count_nested.nested2_snd, is_simp_lemma `count_nested.nested1_fst >>= Ξ» b, guard b, -- simp attribute is global is_simp_lemma `count_nested.nested2_fst >>= Ξ» b, guard $ Β¬b, --lemmas_only doesn't add simp lemma guard $ 7 = e.fold 0 -- there are no other lemmas generated (Ξ» d n, n + if d.to_name.components.init.ilast = `count_nested then 1 else 0) -- testing with arguments @[simps] def bar {Ξ± : Type*} (n m : β„•) : β„• Γ— β„€ := ⟨n - m, n + m⟩ structure equiv_plus_data (Ξ± Ξ²) extends Ξ± ≃ Ξ² := (P : (Ξ± β†’ Ξ²) β†’ Prop) (data : P to_fun) structure automorphism_plus_data Ξ± extends Ξ± βŠ• Ξ± ≃ Ξ± βŠ• Ξ± := (P : (Ξ± βŠ• Ξ± β†’ Ξ± βŠ• Ξ±) β†’ Prop) (data : P to_fun) (extra : bool β†’ my_prod β„• β„•) @[simps] def refl_with_data {Ξ±} : equiv_plus_data Ξ± Ξ± := { P := Ξ» f, f = id, data := rfl, ..foo.rfl } @[simps] def refl_with_data' {Ξ±} : equiv_plus_data Ξ± Ξ± := { P := Ξ» f, f = id, data := rfl, to_equiv := foo.rfl } /- test whether eta expansions are reduced correctly -/ @[simps] def test {Ξ±} : automorphism_plus_data Ξ± := { P := Ξ» f, f = id, data := rfl, extra := Ξ» b, ⟨(⟨3, 5⟩ : my_prod _ _).1, (⟨3, 5⟩ : my_prod _ _).2⟩, ..foo.rfl } /- test whether this is indeed rejected as a valid eta expansion -/ @[simps] def test_sneaky {Ξ±} : automorphism_plus_data Ξ± := { P := Ξ» f, f = id, data := rfl, extra := Ξ» b, ⟨(3,5).1,(3,5).2⟩, ..foo.rfl } run_cmd do e ← get_env, e.get `refl_with_data_to_equiv, e.get `refl_with_data'_to_equiv, e.get `test_extra, e.get `test_sneaky_extra_fst, success_if_fail (e.get `refl_with_data_to_equiv_to_fun), success_if_fail (e.get `refl_with_data'_to_equiv_to_fun), success_if_fail (e.get `test_extra_fst), success_if_fail (e.get `test_sneaky_extra) structure partially_applied_str := (data : β„• β†’ my_prod β„• β„•) /- if we have a partially applied constructor, we treat it as if it were eta-expanded -/ @[simps] def partially_applied_term : partially_applied_str := ⟨my_prod.mk 3⟩ run_cmd do e ← get_env, e.get `partially_applied_term_data_fst, e.get `partially_applied_term_data_snd structure very_partially_applied_str := (data : βˆ€Ξ², β„• β†’ Ξ² β†’ my_prod β„• Ξ²) /- if we have a partially applied constructor, we treat it as if it were eta-expanded -/ @[simps] -- def very_partially_applied_term : very_partially_applied_str := ⟨@my_prod.mk β„•βŸ© def very_partially_applied_term : very_partially_applied_str := ⟨λ x y z, my_prod.mk y z⟩ run_cmd do e ← get_env, e.get `very_partially_applied_term_data_fst, e.get `very_partially_applied_term_data_snd @[simps] def let1 : β„• Γ— β„€ := let n := 3 in ⟨n + 4, 5⟩ @[simps] def let2 : β„• Γ— β„€ := let n := 3, m := 4 in let k := 5 in ⟨n + m, k⟩ @[simps] def let3 : β„• β†’ β„• Γ— β„€ := Ξ» n, let m := 4, k := 5 in ⟨n + m, k⟩ @[simps] def let4 : β„• β†’ β„• Γ— β„€ := let m := 4, k := 5 in Ξ» n, ⟨n + m, k⟩ run_cmd do e ← get_env, e.get `let1_fst, e.get `let2_fst, e.get `let3_fst, e.get `let4_fst, e.get `let1_snd, e.get `let2_snd, e.get `let3_snd, e.get `let4_snd namespace specify @[simps fst] def specify1 : β„• Γ— β„• Γ— β„• := (1, 2, 3) @[simps snd] def specify2 : β„• Γ— β„• Γ— β„• := (1, 2, 3) @[simps snd_fst] def specify3 : β„• Γ— β„• Γ— β„• := (1, 2, 3) @[simps snd snd_snd snd_snd] def specify4 : β„• Γ— β„• Γ— β„• := (1, 2, 3) -- last argument is ignored @[simps] def specify5 : β„• Γ— β„• Γ— β„• := (1, prod.map (Ξ» x, x) (Ξ» y, y) (2, 3)) end specify run_cmd do e ← get_env, e.get `specify.specify1_fst, e.get `specify.specify2_snd, e.get `specify.specify3_snd_fst, e.get `specify.specify4_snd_snd, e.get `specify.specify4_snd, e.get `specify.specify5_fst, e.get `specify.specify5_snd, guard $ 12 = e.fold 0 -- there are no other lemmas generated (Ξ» d n, n + if d.to_name.components.init.ilast = `specify then 1 else 0), success_if_fail_with_msg (simps_tac `specify.specify1 {} ["fst_fst"]) "Invalid simp-lemma specify.specify1_fst_fst. Projection fst doesn't exist, because target is not a structure.", success_if_fail_with_msg (simps_tac `specify.specify1 {} ["foo_fst"]) "Invalid simp-lemma specify.specify1_foo_fst. Structure prod does not have projection foo. The known projections are: [fst, snd] You can also see this information by running `initialize_simps_projections prod`. Note: the projection names used by @[simps] might not correspond to the projection names in the structure.", success_if_fail_with_msg (simps_tac `specify.specify1 {} ["snd_bar"]) "Invalid simp-lemma specify.specify1_snd_bar. Structure prod does not have projection bar. The known projections are: [fst, snd] You can also see this information by running `initialize_simps_projections prod`. Note: the projection names used by @[simps] might not correspond to the projection names in the structure.", success_if_fail_with_msg (simps_tac `specify.specify5 {} ["snd_snd"]) "Invalid simp-lemma specify.specify5_snd_snd. The given definition is not a constructor application: prod.map (Ξ» (x : β„•), x) (Ξ» (y : β„•), y) (2, 3) Possible solution: add option {rhs_md := semireducible}. The option {simp_rhs := tt} might also be useful to simplify the right-hand side." /- We also eta-reduce if we explicitly specify the projection. -/ attribute [simps extra] test run_cmd do e ← get_env, d1 ← e.get `test_extra, d2 ← e.get `test_extra_2, guard $ d1.type =ₐ d2.type, skip /- check short_name option -/ @[simps {short_name := tt}] def short_name1 : my_prod β„• β„• Γ— my_prod β„• β„• := ⟨⟨1, 2⟩, 3, 4⟩ run_cmd do e ← get_env, e.get `short_name1_fst, e.get `short_name1_fst_2, e.get `short_name1_snd, e.get `short_name1_snd_2 /- check simp_rhs option -/ @[simps {simp_rhs := tt}] def equiv.trans {Ξ± Ξ² Ξ³} (f : Ξ± ≃ Ξ²) (g : Ξ² ≃ Ξ³) : Ξ± ≃ Ξ³ := ⟨g.to_fun ∘ f.to_fun, f.inv_fun ∘ g.inv_fun, by { intro x, simp [equiv.left_inv _ _] }, by { intro x, simp [equiv.right_inv _ _] }⟩ example {Ξ± Ξ² Ξ³ : Type} (f : Ξ± ≃ Ξ²) (g : Ξ² ≃ Ξ³) (x : Ξ±) : (f.trans g).to_fun x = (f.trans g).to_fun x := begin dsimp only [equiv.trans_to_fun], guard_target g.to_fun (f.to_fun x) = g.to_fun (f.to_fun x), refl, end local attribute [simp] nat.zero_add nat.one_mul nat.mul_one @[simps {simp_rhs := tt}] def my_nat_equiv : β„• ≃ β„• := ⟨λ n, 0 + n, Ξ» n, 1 * n * 1, by { intro n, simp }, by { intro n, simp }⟩ run_cmd success_if_fail (has_attribute `_refl_lemma `my_nat_equiv_to_fun) >> has_attribute `_refl_lemma `equiv.trans_to_fun example (n : β„•) : my_nat_equiv.to_fun (my_nat_equiv.to_fun $ my_nat_equiv.inv_fun n) = n := by { success_if_fail { refl }, simp only [my_nat_equiv_to_fun, my_nat_equiv_inv_fun] } @[simps {simp_rhs := tt}] def succeed_without_simplification_possible : β„• ≃ β„• := ⟨λ n, n, Ξ» n, n, by { intro n, refl }, by { intro n, refl }⟩ /- test that we don't recursively take projections of `prod` and `pprod` -/ @[simps] def pprod_equiv_prod : pprod β„• β„• ≃ β„• Γ— β„• := { to_fun := Ξ» x, ⟨x.1, x.2⟩, inv_fun := Ξ» x, ⟨x.1, x.2⟩, left_inv := Ξ» ⟨x, y⟩, rfl, right_inv := Ξ» ⟨x, y⟩, rfl } run_cmd do e ← get_env, e.get `pprod_equiv_prod_to_fun, e.get `pprod_equiv_prod_inv_fun attribute [simps to_fun_fst inv_fun_snd] pprod_equiv_prod run_cmd do e ← get_env, e.get `pprod_equiv_prod_to_fun_fst, e.get `pprod_equiv_prod_inv_fun_snd /- Tests with universe levels -/ class has_hom (obj : Type u) : Type (max u (v+1)) := (hom : obj β†’ obj β†’ Type v) infixr ` ⟢ `:10 := has_hom.hom -- type as \h class category_struct (obj : Type u) extends has_hom.{v} obj : Type (max u (v+1)) := (id : Ξ  X : obj, hom X X) (comp : Ξ  {X Y Z : obj}, (X ⟢ Y) β†’ (Y ⟢ Z) β†’ (X ⟢ Z)) notation `πŸ™` := category_struct.id -- type as \b1 infixr ` ≫ `:80 := category_struct.comp -- type as \gg @[simps] instance types : category_struct (Type u) := { hom := Ξ» a b, (a β†’ b), id := Ξ» a, id, comp := Ξ» _ _ _ f g, g ∘ f } example (X : Type u) : (X ⟢ X) = (X β†’ X) := by simp example (X : Type u) : πŸ™ X = (Ξ» x, x) := by { funext, simp } example (X Y Z : Type u) (f : X ⟢ Y) (g : Y ⟢ Z) : f ≫ g = g ∘ f := by { funext, simp } namespace coercing structure foo_str := (c : Type) (x : c) instance : has_coe_to_sort foo_str := ⟨_, foo_str.c⟩ @[simps] def foo : foo_str := βŸ¨β„•, 3⟩ @[simps] def foo2 : foo_str := βŸ¨β„•, 34⟩ example : β†₯foo = β„• := by simp only [foo_c] example : foo.x = (3 : β„•) := by simp only [foo_x] structure voo_str (n : β„•) := (c : Type) (x : c) instance has_coe_voo_str (n : β„•) : has_coe_to_sort (voo_str n) := ⟨_, voo_str.c⟩ @[simps] def voo : voo_str 7 := βŸ¨β„•, 3⟩ @[simps] def voo2 : voo_str 4 := βŸ¨β„•, 34⟩ example : β†₯voo = β„• := by simp only [voo_c] example : voo.x = (3 : β„•) := by simp only [voo_x] structure equiv2 (Ξ± : Sort*) (Ξ² : Sort*) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) instance {Ξ± Ξ²} : has_coe_to_fun $ equiv2 Ξ± Ξ² := ⟨_, equiv2.to_fun⟩ @[simps] protected def rfl2 {Ξ±} : equiv2 Ξ± Ξ± := ⟨λ x, x, Ξ» x, x, Ξ» x, rfl, Ξ» x, rfl⟩ example {Ξ±} (x : Ξ±) : coercing.rfl2 x = x := by rw [coercing.rfl2_to_fun] example {Ξ±} (x : Ξ±) : coercing.rfl2 x = x := by simp example {Ξ±} (x : Ξ±) : coercing.rfl2.inv_fun x = x := by simp @[simps] protected def equiv2.symm {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) : equiv2 Ξ² Ξ± := ⟨f.inv_fun, f, f.right_inv, f.left_inv⟩ @[simps] protected def equiv2.symm2 {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) : equiv2 Ξ² Ξ± := ⟨f.inv_fun, f.to_fun, f.right_inv, f.left_inv⟩ /- we can use the `md` attribute to not unfold the `has_coe_to_fun` attribute, so that `@[simps]` doesn't recognize that the type of `⇑f` is still a function type. -/ @[simps {type_md := reducible}] protected def equiv2.symm3 {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) : equiv2 Ξ² Ξ± := ⟨f.inv_fun, f, f.right_inv, f.left_inv⟩ example {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) (y : Ξ²) : f.symm y = f.inv_fun y := by simp example {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) (x : Ξ±) : f.symm.inv_fun x = f x := by simp example {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) : f.symm.inv_fun = f := by { success_if_fail {simp}, refl } example {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) : f.symm3.inv_fun = f := by simp section set_option old_structure_cmd true class semigroup (G : Type u) extends has_mul G := (mul_assoc : βˆ€ a b c : G, a * b * c = a * (b * c)) end @[simps] instance {Ξ± Ξ²} [semigroup Ξ±] [semigroup Ξ²] : semigroup (Ξ± Γ— Ξ²) := { mul := Ξ» x y, (x.1 * y.1, x.2 * y.2), mul_assoc := by { intros, simp only [semigroup.mul_assoc], refl } } example {Ξ± Ξ²} [semigroup Ξ±] [semigroup Ξ²] (x y : Ξ± Γ— Ξ²) : x * y = (x.1 * y.1, x.2 * y.2) := by simp example {Ξ± Ξ²} [semigroup Ξ±] [semigroup Ξ²] (x y : Ξ± Γ— Ξ²) : (x * y).1 = x.1 * y.1 := by simp structure Semigroup := (G : Type*) (op : G β†’ G β†’ G) (infix * := op) (op_assoc : βˆ€ (x y z : G), (x * y) * z = x * (y * z)) namespace Group instance : has_coe_to_sort Semigroup := ⟨_, Semigroup.G⟩ instance (G : Semigroup) : has_mul G := ⟨G.op⟩ @[simps] def prod_Semigroup (G H : Semigroup) : Semigroup := { G := G Γ— H, op := Ξ» x y, (x.1 * y.1, x.2 * y.2), op_assoc := by { intros, dsimp [Group.has_mul], simp [Semigroup.op_assoc] }} end Group section set_option old_structure_cmd true class extending_stuff (G : Type u) extends has_mul G, has_zero G, has_neg G, has_subset G := (new_axiom : βˆ€ x : G, x * - 0 βŠ† - x) end @[simps] def bar : extending_stuff β„• := { mul := (*), zero := 0, neg := nat.succ, subset := Ξ» x y, true, new_axiom := Ξ» x, trivial } section local attribute [instance] bar example (x : β„•) : x * - 0 βŠ† - x := by simp end class new_extending_stuff (G : Type u) extends has_mul G, has_zero G, has_neg G, has_subset G := (new_axiom : βˆ€ x : G, x * - 0 βŠ† - x) @[simps] def new_bar : new_extending_stuff β„• := { mul := (*), zero := 0, neg := nat.succ, subset := Ξ» x y, true, new_axiom := Ξ» x, trivial } section local attribute [instance] new_bar example (x : β„•) : x * - 0 βŠ† - x := by simp end end coercing namespace manual_coercion structure equiv (Ξ± : Sort*) (Ξ² : Sort*) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) local infix ` ≃ `:25 := manual_coercion.equiv variables {Ξ± Ξ² Ξ³ : Sort*} instance : has_coe_to_fun $ Ξ± ≃ Ξ² := ⟨_, equiv.to_fun⟩ def equiv.symm (e : Ξ± ≃ Ξ²) : Ξ² ≃ Ξ± := ⟨e.inv_fun, e.to_fun⟩ /-- See Note [custom simps projection] -/ def equiv.simps.inv_fun (e : Ξ± ≃ Ξ²) : Ξ² β†’ Ξ± := e.symm /-- Composition of equivalences `e₁ : Ξ± ≃ Ξ²` and `eβ‚‚ : Ξ² ≃ Ξ³`. -/ @[simps {simp_rhs := tt}] protected def equiv.trans (e₁ : Ξ± ≃ Ξ²) (eβ‚‚ : Ξ² ≃ Ξ³) : Ξ± ≃ Ξ³ := ⟨eβ‚‚ ∘ e₁, e₁.symm ∘ eβ‚‚.symm⟩ example (e₁ : Ξ± ≃ Ξ²) (eβ‚‚ : Ξ² ≃ Ξ³) (x : Ξ³) : (e₁.trans eβ‚‚).symm x = e₁.symm (eβ‚‚.symm x) := by simp only [equiv.trans_inv_fun] end manual_coercion namespace faulty_manual_coercion structure equiv (Ξ± : Sort*) (Ξ² : Sort*) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) local infix ` ≃ `:25 := faulty_manual_coercion.equiv variables {Ξ± Ξ² Ξ³ : Sort*} /-- See Note [custom simps projection] -/ noncomputable def equiv.simps.inv_fun (e : Ξ± ≃ Ξ²) : Ξ² β†’ Ξ± := classical.choice ⟨e.inv_fun⟩ run_cmd do e ← get_env, success_if_fail_with_msg (simps_get_raw_projections e `faulty_manual_coercion.equiv) "Invalid custom projection: Ξ» {Ξ± : Sort u_1} {Ξ² : Sort u_2} (e : Ξ± ≃ Ξ²), classical.choice _ Expression is not definitionally equal to equiv.inv_fun." end faulty_manual_coercion namespace manual_initialize /- defining a manual coercion. -/ variables {Ξ± Ξ² Ξ³ : Sort*} structure equiv (Ξ± : Sort*) (Ξ² : Sort*) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) local infix ` ≃ `:25 := manual_initialize.equiv instance : has_coe_to_fun $ Ξ± ≃ Ξ² := ⟨_, equiv.to_fun⟩ def equiv.symm (e : Ξ± ≃ Ξ²) : Ξ² ≃ Ξ± := ⟨e.inv_fun, e.to_fun⟩ /-- See Note [custom simps projection] -/ -- test: intentionally using different unvierse levels for equiv.symm than for equiv def equiv.simps.inv_fun (e : Ξ± ≃ Ξ²) : Ξ² β†’ Ξ± := e.symm initialize_simps_projections equiv run_cmd has_attribute `_simps_str `manual_initialize.equiv /-- Composition of equivalences `e₁ : Ξ± ≃ Ξ²` and `eβ‚‚ : Ξ² ≃ Ξ³`. -/ @[simps {simp_rhs := tt}] protected def equiv.trans (e₁ : Ξ± ≃ Ξ²) (eβ‚‚ : Ξ² ≃ Ξ³) : Ξ± ≃ Ξ³ := ⟨eβ‚‚ ∘ e₁, e₁.symm ∘ eβ‚‚.symm⟩ end manual_initialize namespace faulty_universes variables {Ξ± Ξ² Ξ³ : Sort*} structure equiv (Ξ± : Sort u) (Ξ² : Sort v) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) local infix ` ≃ `:25 := faulty_universes.equiv instance : has_coe_to_fun $ Ξ± ≃ Ξ² := ⟨_, equiv.to_fun⟩ def equiv.symm (e : Ξ± ≃ Ξ²) : Ξ² ≃ Ξ± := ⟨e.inv_fun, e.to_fun⟩ /-- See Note [custom simps projection] -/ -- test: intentionally using different names for the universe variables for equiv.symm than for -- equiv def equiv.simps.inv_fun {Ξ± : Type u} {Ξ² : Type v} (e : Ξ± ≃ Ξ²) : Ξ² β†’ Ξ± := e.symm run_cmd do e ← get_env, success_if_fail_with_msg (simps_get_raw_projections e `faulty_universes.equiv) "Invalid custom projection: Ξ» {Ξ± : Type u} {Ξ² : Type v} (e : Ξ± ≃ Ξ²), ⇑(e.symm) Expression has different type than equiv.inv_fun. Given type: Ξ  {Ξ± : Type u} {Ξ² : Type v} (e : Ξ± ≃ Ξ²), has_coe_to_fun.F e.symm Expected type: Ξ  {Ξ± : Sort u} {Ξ² : Sort v}, Ξ± ≃ Ξ² β†’ Ξ² β†’ Ξ±" end faulty_universes namespace manual_universes variables {Ξ± Ξ² Ξ³ : Sort*} structure equiv (Ξ± : Sort u) (Ξ² : Sort v) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) local infix ` ≃ `:25 := manual_universes.equiv instance : has_coe_to_fun $ Ξ± ≃ Ξ² := ⟨_, equiv.to_fun⟩ def equiv.symm (e : Ξ± ≃ Ξ²) : Ξ² ≃ Ξ± := ⟨e.inv_fun, e.to_fun⟩ /-- See Note [custom simps projection] -/ -- test: intentionally using different unvierse levels for equiv.symm than for equiv def equiv.simps.inv_fun {Ξ± : Sort w} {Ξ² : Sort u} (e : Ξ± ≃ Ξ²) : Ξ² β†’ Ξ± := e.symm -- check whether we can generate custom projections even if the universe names don't match initialize_simps_projections equiv end manual_universes namespace manual_projection_names structure equiv (Ξ± : Sort*) (Ξ² : Sort*) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) local infix ` ≃ `:25 := manual_projection_names.equiv variables {Ξ± Ξ² Ξ³ : Sort*} instance : has_coe_to_fun $ Ξ± ≃ Ξ² := ⟨_, equiv.to_fun⟩ def equiv.symm (e : Ξ± ≃ Ξ²) : Ξ² ≃ Ξ± := ⟨e.inv_fun, e.to_fun⟩ /-- See Note [custom simps projection] -/ def equiv.simps.inv_fun (e : Ξ± ≃ Ξ²) : Ξ² β†’ Ξ± := e.symm initialize_simps_projections equiv (to_fun β†’ apply, inv_fun β†’ symm_apply) run_cmd do e ← get_env, data ← simps_get_raw_projections e `manual_projection_names.equiv, guard $ data.2.map prod.fst = [`apply, `symm_apply] @[simps {simp_rhs := tt}] protected def equiv.trans (e₁ : Ξ± ≃ Ξ²) (eβ‚‚ : Ξ² ≃ Ξ³) : Ξ± ≃ Ξ³ := ⟨eβ‚‚ ∘ e₁, e₁.symm ∘ eβ‚‚.symm⟩ example (e₁ : Ξ± ≃ Ξ²) (eβ‚‚ : Ξ² ≃ Ξ³) (x : Ξ±) : (e₁.trans eβ‚‚) x = eβ‚‚ (e₁ x) := by simp only [equiv.trans_apply] example (e₁ : Ξ± ≃ Ξ²) (eβ‚‚ : Ξ² ≃ Ξ³) (x : Ξ³) : (e₁.trans eβ‚‚).symm x = e₁.symm (eβ‚‚.symm x) := by simp only [equiv.trans_symm_apply] -- the new projection names are parsed correctly (the old projection names won't work anymore) @[simps apply symm_apply] protected def equiv.trans2 (e₁ : Ξ± ≃ Ξ²) (eβ‚‚ : Ξ² ≃ Ξ³) : Ξ± ≃ Ξ³ := ⟨eβ‚‚ ∘ e₁, e₁.symm ∘ eβ‚‚.symm⟩ -- initialize_simps_projections equiv end manual_projection_names -- test transparency setting structure set_plus (Ξ± : Type) := (s : set Ξ±) (x : Ξ±) (h : x ∈ s) @[simps] def nat_set_plus : set_plus β„• := ⟨set.univ, 1, trivial⟩ example : nat_set_plus.s = set.univ := begin dsimp only [nat_set_plus_s], guard_target @set.univ β„• = set.univ, refl end @[simps {type_md := semireducible}] def nat_set_plus2 : set_plus β„• := ⟨set.univ, 1, trivial⟩ example : nat_set_plus2.s = set.univ := begin success_if_fail { dsimp only [nat_set_plus2_s] }, refl end @[simps {rhs_md := semireducible}] def nat_set_plus3 : set_plus β„• := nat_set_plus example : nat_set_plus3.s = set.univ := begin dsimp only [nat_set_plus3_s], guard_target @set.univ β„• = set.univ, refl end namespace nested_non_fully_applied structure equiv (Ξ± : Sort*) (Ξ² : Sort*) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) local infix ` ≃ `:25 := nested_non_fully_applied.equiv variables {Ξ± Ξ² Ξ³ : Sort*} @[simps] def equiv.symm (e : Ξ± ≃ Ξ²) : Ξ² ≃ Ξ± := ⟨e.inv_fun, e.to_fun⟩ @[simps {rhs_md := semireducible, fully_applied := ff}] def equiv.symm2 : (Ξ± ≃ Ξ²) ≃ (Ξ² ≃ Ξ±) := ⟨equiv.symm, equiv.symm⟩ example (e : Ξ± ≃ Ξ²) : (equiv.symm2.inv_fun e).to_fun = e.inv_fun := begin dsimp only [equiv.symm2_inv_fun_to_fun], guard_target e.inv_fun = e.inv_fun, refl end /- do not prematurely unfold `equiv.symm`, unless necessary -/ @[simps to_fun to_fun_to_fun {rhs_md := semireducible}] def equiv.symm3 : (Ξ± ≃ Ξ²) ≃ (Ξ² ≃ Ξ±) := equiv.symm2 example (e : Ξ± ≃ Ξ²) (y : Ξ²) : (equiv.symm3.to_fun e).to_fun y = e.inv_fun y ∧ (equiv.symm3.to_fun e).to_fun y = e.inv_fun y := begin split, { dsimp only [equiv.symm3_to_fun], guard_target e.symm.to_fun y = e.inv_fun y, refl }, { dsimp only [equiv.symm3_to_fun_to_fun], guard_target e.inv_fun y = e.inv_fun y, refl } end end nested_non_fully_applied /- fail if you add an attribute with a parameter. -/ run_cmd success_if_fail $ simps_tac `foo.rfl { attrs := [`higher_order] } -- test that type classes which are props work class prop_class (n : β„•) : Prop := (has_true : true) instance has_prop_class (n : β„•) : prop_class n := ⟨trivial⟩ structure needs_prop_class (n : β„•) [prop_class n] := (t : true) @[simps] def test_prop_class : needs_prop_class 1 := { t := trivial } /- check that when the coercion is given in eta-expanded form, we can also find the coercion. -/ structure alg_hom (R A B : Type*) := (to_fun : A β†’ B) instance (R A B : Type*) : has_coe_to_fun (alg_hom R A B) := ⟨_, Ξ» f, f.to_fun⟩ @[simps] def my_alg_hom : alg_hom unit bool bool := { to_fun := id } example (x : bool) : my_alg_hom x = id x := by simp only [my_alg_hom_to_fun] structure ring_hom (A B : Type*) := (to_fun : A β†’ B) instance (A B : Type*) : has_coe_to_fun (ring_hom A B) := ⟨_, Ξ» f, f.to_fun⟩ @[simps] def my_ring_hom : ring_hom bool bool := { to_fun := id } example (x : bool) : my_ring_hom x = id x := by simp only [my_ring_hom_to_fun]
7c12408200b55a6d254c1283b7a103b3eedd951a
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/hom/units.lean
71f0f7f067f89717fd28ab560a6e4f33d0ed65bf
[ "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
13,632
lean
/- Copyright (c) 2018 Johan Commelin All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Chris Hughes, Kevin Buzzard -/ import algebra.hom.group import algebra.group.units /-! # Monoid homomorphisms and units > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file allows to lift monoid homomorphisms to group homomorphisms of their units subgroups. It also contains unrelated results about `units` that depend on `monoid_hom`. ## Main declarations * `units.map`: Turn an homomorphism from `Ξ±` to `Ξ²` monoids into an homomorphism from `Ξ±Λ£` to `Ξ²Λ£`. * `monoid_hom.to_hom_units`: Turn an homomorphism from a group `Ξ±` to `Ξ²` into an homomorphism from `Ξ±` to `Ξ²Λ£`. ## TODO The results that don't mention homomorphisms should be proved (earlier?) in a different file and be used to golf the basic `group` lemmas. -/ open function universes u v w @[to_additive] lemma group.is_unit {G} [group G] (g : G) : is_unit g := ⟨⟨g, g⁻¹, mul_inv_self g, inv_mul_self g⟩, rfl⟩ section monoid_hom_class /-- If two homomorphisms from a division monoid to a monoid are equal at a unit `x`, then they are equal at `x⁻¹`. -/ @[to_additive "If two homomorphisms from a subtraction monoid to an additive monoid are equal at an additive unit `x`, then they are equal at `-x`."] lemma is_unit.eq_on_inv {F G N} [division_monoid G] [monoid N] [monoid_hom_class F G N] {x : G} (hx : is_unit x) (f g : F) (h : f x = g x) : f x⁻¹ = g x⁻¹ := left_inv_eq_right_inv (map_mul_eq_one f hx.inv_mul_cancel) $ h.symm β–Έ map_mul_eq_one g $ hx.mul_inv_cancel /-- If two homomorphism from a group to a monoid are equal at `x`, then they are equal at `x⁻¹`. -/ @[to_additive "If two homomorphism from an additive group to an additive monoid are equal at `x`, then they are equal at `-x`." ] lemma eq_on_inv {F G M} [group G] [monoid M] [monoid_hom_class F G M] (f g : F) {x : G} (h : f x = g x) : f x⁻¹ = g x⁻¹ := (group.is_unit x).eq_on_inv f g h end monoid_hom_class namespace units variables {Ξ± : Type*} {M : Type u} {N : Type v} {P : Type w} [monoid M] [monoid N] [monoid P] /-- The group homomorphism on units induced by a `monoid_hom`. -/ @[to_additive "The `add_group` homomorphism on `add_unit`s induced by an `add_monoid_hom`."] def map (f : M β†’* N) : MΛ£ β†’* NΛ£ := monoid_hom.mk' (Ξ» u, ⟨f u.val, f u.inv, by rw [← f.map_mul, u.val_inv, f.map_one], by rw [← f.map_mul, u.inv_val, f.map_one]⟩) (Ξ» x y, ext (f.map_mul x y)) @[simp, to_additive] lemma coe_map (f : M β†’* N) (x : MΛ£) : ↑(map f x) = f x := rfl @[simp, to_additive] lemma coe_map_inv (f : M β†’* N) (u : MΛ£) : ↑(map f u)⁻¹ = f ↑u⁻¹ := rfl @[simp, to_additive] lemma map_comp (f : M β†’* N) (g : N β†’* P) : map (g.comp f) = (map g).comp (map f) := rfl variables (M) @[simp, to_additive] lemma map_id : map (monoid_hom.id M) = monoid_hom.id MΛ£ := by ext; refl /-- Coercion `MΛ£ β†’ M` as a monoid homomorphism. -/ @[to_additive "Coercion `add_units M β†’ M` as an add_monoid homomorphism."] def coe_hom : MΛ£ β†’* M := ⟨coe, coe_one, coe_mul⟩ variable {M} @[simp, to_additive] lemma coe_hom_apply (x : MΛ£) : coe_hom M x = ↑x := rfl @[simp, norm_cast, to_additive] lemma coe_pow (u : MΛ£) (n : β„•) : ((u ^ n : MΛ£) : M) = u ^ n := (units.coe_hom M).map_pow u n section division_monoid variables [division_monoid Ξ±] @[simp, norm_cast, to_additive] lemma coe_div : βˆ€ u₁ uβ‚‚ : Ξ±Λ£, ↑(u₁ / uβ‚‚) = (u₁ / uβ‚‚ : Ξ±) := (units.coe_hom Ξ±).map_div @[simp, norm_cast, to_additive] lemma coe_zpow : βˆ€ (u : Ξ±Λ£) (n : β„€), ((u ^ n : Ξ±Λ£) : Ξ±) = u ^ n := (units.coe_hom Ξ±).map_zpow @[field_simps] lemma _root_.divp_eq_div (a : Ξ±) (u : Ξ±Λ£) : a /β‚š u = a / u := by rw [div_eq_mul_inv, divp, u.coe_inv] @[simp, to_additive] lemma _root_.map_units_inv {F : Type*} [monoid_hom_class F M Ξ±] (f : F) (u : units M) : f ↑u⁻¹ = (f u)⁻¹ := ((f : M β†’* Ξ±).comp (units.coe_hom M)).map_inv u end division_monoid /-- If a map `g : M β†’ NΛ£` agrees with a homomorphism `f : M β†’* N`, then this map is a monoid homomorphism too. -/ @[to_additive "If a map `g : M β†’ add_units N` agrees with a homomorphism `f : M β†’+ N`, then this map is an add_monoid homomorphism too."] def lift_right (f : M β†’* N) (g : M β†’ NΛ£) (h : βˆ€ x, ↑(g x) = f x) : M β†’* NΛ£ := { to_fun := g, map_one' := units.ext $ (h 1).symm β–Έ f.map_one, map_mul' := Ξ» x y, units.ext $ by simp only [h, coe_mul, f.map_mul] } @[simp, to_additive] lemma coe_lift_right {f : M β†’* N} {g : M β†’ NΛ£} (h : βˆ€ x, ↑(g x) = f x) (x) : (lift_right f g h x : N) = f x := h x @[simp, to_additive] lemma mul_lift_right_inv {f : M β†’* N} {g : M β†’ NΛ£} (h : βˆ€ x, ↑(g x) = f x) (x) : f x * ↑(lift_right f g h x)⁻¹ = 1 := by rw [units.mul_inv_eq_iff_eq_mul, one_mul, coe_lift_right] @[simp, to_additive] lemma lift_right_inv_mul {f : M β†’* N} {g : M β†’ NΛ£} (h : βˆ€ x, ↑(g x) = f x) (x) : ↑(lift_right f g h x)⁻¹ * f x = 1 := by rw [units.inv_mul_eq_iff_eq_mul, mul_one, coe_lift_right] end units namespace monoid_hom /-- If `f` is a homomorphism from a group `G` to a monoid `M`, then its image lies in the units of `M`, and `f.to_hom_units` is the corresponding monoid homomorphism from `G` to `MΛ£`. -/ @[to_additive "If `f` is a homomorphism from an additive group `G` to an additive monoid `M`, then its image lies in the `add_units` of `M`, and `f.to_hom_units` is the corresponding homomorphism from `G` to `add_units M`."] def to_hom_units {G M : Type*} [group G] [monoid M] (f : G β†’* M) : G β†’* MΛ£ := units.lift_right f (Ξ» g, ⟨f g, f g⁻¹, map_mul_eq_one f (mul_inv_self _), map_mul_eq_one f (inv_mul_self _)⟩) (Ξ» g, rfl) @[simp, to_additive] lemma coe_to_hom_units {G M : Type*} [group G] [monoid M] (f : G β†’* M) (g : G) : (f.to_hom_units g : M) = f g := rfl end monoid_hom namespace is_unit variables {F G Ξ± M N : Type*} section monoid variables [monoid M] [monoid N] @[to_additive] lemma map [monoid_hom_class F M N] (f : F) {x : M} (h : is_unit x) : is_unit (f x) := by rcases h with ⟨y, rfl⟩; exact (units.map (f : M β†’* N) y).is_unit @[to_additive] lemma of_left_inverse [monoid_hom_class F M N] [monoid_hom_class G N M] {f : F} {x : M} (g : G) (hfg : function.left_inverse g f) (h : is_unit (f x)) : is_unit x := by simpa only [hfg x] using h.map g @[to_additive] lemma _root_.is_unit_map_of_left_inverse [monoid_hom_class F M N] [monoid_hom_class G N M] {f : F} {x : M} (g : G) (hfg : function.left_inverse g f) : is_unit (f x) ↔ is_unit x := ⟨of_left_inverse g hfg, map _⟩ /-- If a homomorphism `f : M β†’* N` sends each element to an `is_unit`, then it can be lifted to `f : M β†’* NΛ£`. See also `units.lift_right` for a computable version. -/ @[to_additive "If a homomorphism `f : M β†’+ N` sends each element to an `is_add_unit`, then it can be lifted to `f : M β†’+ add_units N`. See also `add_units.lift_right` for a computable version."] noncomputable def lift_right (f : M β†’* N) (hf : βˆ€ x, is_unit (f x)) : M β†’* NΛ£ := units.lift_right f (Ξ» x, (hf x).unit) $ Ξ» x, rfl @[to_additive] lemma coe_lift_right (f : M β†’* N) (hf : βˆ€ x, is_unit (f x)) (x) : (is_unit.lift_right f hf x : N) = f x := rfl @[simp, to_additive] lemma mul_lift_right_inv (f : M β†’* N) (h : βˆ€ x, is_unit (f x)) (x) : f x * ↑(is_unit.lift_right f h x)⁻¹ = 1 := units.mul_lift_right_inv (Ξ» y, rfl) x @[simp, to_additive] lemma lift_right_inv_mul (f : M β†’* N) (h : βˆ€ x, is_unit (f x)) (x) : ↑(is_unit.lift_right f h x)⁻¹ * f x = 1 := units.lift_right_inv_mul (Ξ» y, rfl) x end monoid section division_monoid variables [division_monoid Ξ±] {a b c : Ξ±} /-- The element of the group of units, corresponding to an element of a monoid which is a unit. As opposed to `is_unit.unit`, the inverse is computable and comes from the inversion on `Ξ±`. This is useful to transfer properties of inversion in `units Ξ±` to `Ξ±`. See also `to_units`. -/ @[to_additive "The element of the additive group of additive units, corresponding to an element of an additive monoid which is an additive unit. As opposed to `is_add_unit.add_unit`, the negation is computable and comes from the negation on `Ξ±`. This is useful to transfer properties of negation in `add_units Ξ±` to `Ξ±`. See also `to_add_units`.", simps] def unit' (h : is_unit a) : Ξ±Λ£ := ⟨a, a⁻¹, h.mul_inv_cancel, h.inv_mul_cancel⟩ @[simp, to_additive] protected lemma mul_inv_cancel_left (h : is_unit a) : βˆ€ b, a * (a⁻¹ * b) = b := h.unit'.mul_inv_cancel_left @[simp, to_additive] protected lemma inv_mul_cancel_left (h : is_unit a) : βˆ€ b, a⁻¹ * (a * b) = b := h.unit'.inv_mul_cancel_left @[simp, to_additive] protected lemma mul_inv_cancel_right (h : is_unit b) (a : Ξ±) : a * b * b⁻¹ = a := h.unit'.mul_inv_cancel_right _ @[simp, to_additive] protected lemma inv_mul_cancel_right (h : is_unit b) (a : Ξ±) : a * b⁻¹ * b = a := h.unit'.inv_mul_cancel_right _ @[to_additive] protected lemma div_self (h : is_unit a) : a / a = 1 := by rw [div_eq_mul_inv, h.mul_inv_cancel] @[to_additive] protected lemma eq_mul_inv_iff_mul_eq (h : is_unit c) : a = b * c⁻¹ ↔ a * c = b := h.unit'.eq_mul_inv_iff_mul_eq @[to_additive] protected lemma eq_inv_mul_iff_mul_eq (h : is_unit b) : a = b⁻¹ * c ↔ b * a = c := h.unit'.eq_inv_mul_iff_mul_eq @[to_additive] protected lemma inv_mul_eq_iff_eq_mul (h : is_unit a) : a⁻¹ * b = c ↔ b = a * c := h.unit'.inv_mul_eq_iff_eq_mul @[to_additive] protected lemma mul_inv_eq_iff_eq_mul (h : is_unit b) : a * b⁻¹ = c ↔ a = c * b := h.unit'.mul_inv_eq_iff_eq_mul @[to_additive] protected lemma mul_inv_eq_one (h : is_unit b) : a * b⁻¹ = 1 ↔ a = b := @units.mul_inv_eq_one _ _ h.unit' _ @[to_additive] protected lemma inv_mul_eq_one (h : is_unit a) : a⁻¹ * b = 1 ↔ a = b := @units.inv_mul_eq_one _ _ h.unit' _ @[to_additive] protected lemma mul_eq_one_iff_eq_inv (h : is_unit b) : a * b = 1 ↔ a = b⁻¹ := @units.mul_eq_one_iff_eq_inv _ _ h.unit' _ @[to_additive] protected lemma mul_eq_one_iff_inv_eq (h : is_unit a) : a * b = 1 ↔ a⁻¹ = b := @units.mul_eq_one_iff_inv_eq _ _ h.unit' _ @[simp, to_additive] protected lemma div_mul_cancel (h : is_unit b) (a : Ξ±) : a / b * b = a := by rw [div_eq_mul_inv, h.inv_mul_cancel_right] @[simp, to_additive] protected lemma mul_div_cancel (h : is_unit b) (a : Ξ±) : a * b / b = a := by rw [div_eq_mul_inv, h.mul_inv_cancel_right] @[to_additive] protected lemma mul_one_div_cancel (h : is_unit a) : a * (1 / a) = 1 := by simp [h] @[to_additive] protected lemma one_div_mul_cancel (h : is_unit a) : (1 / a) * a = 1 := by simp [h] @[to_additive] lemma inv : is_unit a β†’ is_unit a⁻¹ := by { rintro ⟨u, rfl⟩, rw ←units.coe_inv, exact units.is_unit _ } @[to_additive] lemma div (ha : is_unit a) (hb : is_unit b) : is_unit (a / b) := by { rw div_eq_mul_inv, exact ha.mul hb.inv } @[to_additive] protected lemma div_left_inj (h : is_unit c) : a / c = b / c ↔ a = b := by { simp_rw div_eq_mul_inv, exact units.mul_left_inj h.inv.unit' } @[to_additive] protected lemma div_eq_iff (h : is_unit b) : a / b = c ↔ a = c * b := by rw [div_eq_mul_inv, h.mul_inv_eq_iff_eq_mul] @[to_additive] protected lemma eq_div_iff (h : is_unit c) : a = b / c ↔ a * c = b := by rw [div_eq_mul_inv, h.eq_mul_inv_iff_mul_eq] @[to_additive] protected lemma div_eq_of_eq_mul (h : is_unit b) : a = c * b β†’ a / b = c := h.div_eq_iff.2 @[to_additive] protected lemma eq_div_of_mul_eq (h : is_unit c) : a * c = b β†’ a = b / c := h.eq_div_iff.2 @[to_additive] protected lemma div_eq_one_iff_eq (h : is_unit b) : a / b = 1 ↔ a = b := ⟨eq_of_div_eq_one, Ξ» hab, hab.symm β–Έ h.div_self⟩ @[to_additive] protected lemma div_mul_left (h : is_unit b) : b / (a * b) = 1 / a := by rw [div_eq_mul_inv, mul_inv_rev, h.mul_inv_cancel_left, one_div] @[to_additive] protected lemma mul_div_mul_right (h : is_unit c) (a b : Ξ±) : (a * c) / (b * c) = a / b := by simp only [div_eq_mul_inv, mul_inv_rev, mul_assoc, h.mul_inv_cancel_left] @[to_additive] protected lemma mul_mul_div (a : Ξ±) (h : is_unit b) : a * b * (1 / b) = a := by simp [h] end division_monoid section division_comm_monoid variables [division_comm_monoid Ξ±] {a b c d : Ξ±} @[to_additive] protected lemma div_mul_right (h : is_unit a) (b : Ξ±) : a / (a * b) = 1 / b := by rw [mul_comm, h.div_mul_left] @[to_additive] protected lemma mul_div_cancel_left (h : is_unit a) (b : Ξ±) : a * b / a = b := by rw [mul_comm, h.mul_div_cancel] @[to_additive] protected lemma mul_div_cancel' (h : is_unit a) (b : Ξ±) : a * (b / a) = b := by rw [mul_comm, h.div_mul_cancel] @[to_additive] protected lemma mul_div_mul_left (h : is_unit c) (a b : Ξ±) : (c * a) / (c * b) = a / b := by rw [mul_comm c, mul_comm c, h.mul_div_mul_right] @[to_additive] protected lemma mul_eq_mul_of_div_eq_div (hb : is_unit b) (hd : is_unit d) (a c : Ξ±) (h : a / b = c / d) : a * d = c * b := by rw [←mul_one a, ←hb.div_self, ←mul_comm_div, h, div_mul_eq_mul_div, hd.div_mul_cancel] @[to_additive] protected lemma div_eq_div_iff (hb : is_unit b) (hd : is_unit d) : a / b = c / d ↔ a * d = c * b := by rw [←(hb.mul hd).mul_left_inj, ←mul_assoc, hb.div_mul_cancel, ←mul_assoc, mul_right_comm, hd.div_mul_cancel] @[to_additive] protected lemma div_div_cancel (h : is_unit a) : a / (a / b) = b := by rw [div_div_eq_mul_div, h.mul_div_cancel_left] end division_comm_monoid end is_unit
9c5fc063ceed812c623bb14019a9ac07ea19ec48
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/stage0/src/Lean/Parser/Level.lean
0528d9bf828754b939ca8e074baa17791e0596ff
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,003
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, Sebastian Ullrich -/ import Lean.Parser.Extra namespace Lean namespace Parser builtin_initialize registerBuiltinParserAttribute `builtinLevelParser `level @[inline] def levelParser (rbp : Nat := 0) : Parser := categoryParser `level rbp namespace Level @[builtinLevelParser] def paren := parser! "(" >> levelParser >> ")" @[builtinLevelParser] def max := parser! nonReservedSymbol "max" true >> many1 (ppSpace >> levelParser maxPrec) @[builtinLevelParser] def imax := parser! nonReservedSymbol "imax" true >> many1 (ppSpace >> levelParser maxPrec) @[builtinLevelParser] def hole := parser! "_" @[builtinLevelParser] def num := checkPrec maxPrec >> numLit @[builtinLevelParser] def ident := checkPrec maxPrec >> Parser.ident @[builtinLevelParser] def addLit := tparser!:65 " + " >> numLit end Level end Parser end Lean
c047fafc1e9f6925220345b142dceee0542c9e66
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/monoidal/limits_auto.lean
e8be4878cce567fae6e1fc3e954e41edc7470ca7
[]
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
3,349
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.category_theory.monoidal.functorial import Mathlib.category_theory.monoidal.functor_category import Mathlib.category_theory.limits.limits import Mathlib.PostPort universes u v namespace Mathlib /-! # `lim : (J β₯€ C) β₯€ C` is lax monoidal when `C` is a monoidal category. When `C` is a monoidal category, the functorial association `F ↦ limit F` is lax monoidal, i.e. there are morphisms * `lim_lax.Ξ΅ : (πŸ™_ C) β†’ limit (πŸ™_ (J β₯€ C))` * `lim_lax.ΞΌ : limit F βŠ— limit G ⟢ limit (F βŠ— G)` satisfying the laws of a lax monoidal functor. -/ namespace category_theory.limits protected instance limit_functorial {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C] : functorial fun (F : J β₯€ C) => limit F := functorial.mk (functor.map lim) @[simp] theorem limit_functorial_map {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C] {F : J β₯€ C} {G : J β₯€ C} (Ξ± : F ⟢ G) : map (fun (F : J β₯€ C) => limit F) Ξ± = functor.map lim Ξ± := rfl protected instance limit_lax_monoidal {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C] [monoidal_category C] : lax_monoidal fun (F : J β₯€ C) => limit F := lax_monoidal.mk (limit.lift (functor.obj (functor.const J) πŸ™_) (cone.mk πŸ™_ (nat_trans.mk fun (j : J) => πŸ™))) fun (F G : J β₯€ C) => limit.lift (F βŠ— G) (cone.mk (limit F βŠ— limit G) (nat_trans.mk fun (j : J) => limit.Ο€ F j βŠ— limit.Ο€ G j)) /-- The limit functor `F ↦ limit F` bundled as a lax monoidal functor. -/ def lim_lax {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C] [monoidal_category C] : lax_monoidal_functor (J β₯€ C) C := lax_monoidal_functor.of fun (F : J β₯€ C) => limit F @[simp] theorem lim_lax_obj {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C] [monoidal_category C] (F : J β₯€ C) : functor.obj (lax_monoidal_functor.to_functor lim_lax) F = limit F := rfl theorem lim_lax_obj' {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C] [monoidal_category C] (F : J β₯€ C) : functor.obj (lax_monoidal_functor.to_functor lim_lax) F = functor.obj lim F := rfl @[simp] theorem lim_lax_map {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C] [monoidal_category C] {F : J β₯€ C} {G : J β₯€ C} (Ξ± : F ⟢ G) : functor.map (lax_monoidal_functor.to_functor lim_lax) Ξ± = functor.map lim Ξ± := rfl @[simp] theorem lim_lax_Ξ΅ {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C] [monoidal_category C] : lax_monoidal_functor.Ξ΅ lim_lax = limit.lift (functor.obj (functor.const J) πŸ™_) (cone.mk πŸ™_ (nat_trans.mk fun (j : J) => πŸ™)) := rfl @[simp] theorem lim_lax_ΞΌ {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C] [monoidal_category C] (F : J β₯€ C) (G : J β₯€ C) : lax_monoidal_functor.ΞΌ lim_lax F G = limit.lift (F βŠ— G) (cone.mk (limit F βŠ— limit G) (nat_trans.mk fun (j : J) => limit.Ο€ F j βŠ— limit.Ο€ G j)) := rfl end Mathlib
e31b1770a7aa1986dfeee67655800741aabc9e99
618003631150032a5676f229d13a079ac875ff77
/src/algebra/iterate_hom.lean
d09735f139da3636d52dfcb7ac9497372403c03c
[ "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
3,479
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury Kudryashov -/ import algebra.group_power import logic.function.iterate /-! # Iterates of monoid and ring homomorphisms Iterate of a monoid/ring homomorphism is a monoid/ring homomorphism but it has a wrong type, so Lean can't apply lemmas like `monoid_hom.map_one` to `f^[n] 1`. Though it is possible to define a monoid structure on the endomorphisms, quite often we do not want to convert from `M β†’* M` to (not yet defined) `monoid.End M` and from `f^[n]` to `f^n` just to apply a simple lemma. So, we restate standard `*_hom.map_*` lemmas under names `*_hom.iterate_map_*`. -/ open function variables {M : Type*} {N : Type*} {G : Type*} {H : Type*} namespace monoid_hom variables [monoid M] [monoid N] [group G] [group H] @[simp, to_additive] theorem iterate_map_one (f : M β†’* M) (n : β„•) : f^[n] 1 = 1 := iterate_fixed f.map_one n @[simp, to_additive] theorem iterate_map_mul (f : M β†’* M) (n : β„•) (x y) : f^[n] (x * y) = (f^[n] x) * (f^[n] y) := semiconjβ‚‚.iterate f.map_mul n x y @[simp, to_additive] theorem iterate_map_inv (f : G β†’* G) (n : β„•) (x) : f^[n] (x⁻¹) = (f^[n] x)⁻¹ := commute.iterate_left f.map_inv n x theorem iterate_map_pow (f : M β†’* M) (a) (n m : β„•) : f^[n] (a^m) = (f^[n] a)^m := commute.iterate_left (Ξ» x, f.map_pow x m) n a theorem iterate_map_gpow (f : G β†’* G) (a) (n : β„•) (m : β„€) : f^[n] (a^m) = (f^[n] a)^m := commute.iterate_left (Ξ» x, f.map_gpow x m) n a end monoid_hom namespace add_monoid_hom variables [add_monoid M] [add_monoid N] [add_group G] [add_group H] @[simp] theorem iterate_map_sub (f : G β†’+ G) (n : β„•) (x y) : f^[n] (x - y) = (f^[n] x) - (f^[n] y) := semiconjβ‚‚.iterate f.map_sub n x y theorem iterate_map_smul (f : M β†’+ M) (n m : β„•) (x : M) : f^[n] (m β€’β„• x) = m β€’β„• (f^[n] x) := f.to_multiplicative.iterate_map_pow x n m theorem iterate_map_gsmul (f : G β†’+ G) (n : β„•) (m : β„€) (x : G) : f^[n] (m β€’β„€ x) = m β€’β„€ (f^[n] x) := f.to_multiplicative.iterate_map_gpow x n m end add_monoid_hom namespace ring_hom section semiring variables {R : Type*} [semiring R] (f : R β†’+* R) (n : β„•) (x y : R) lemma coe_pow : βˆ€ n : β„•, ⇑(f^n) = (f^[n]) | 0 := rfl | (n+1) := by { simp only [function.iterate_succ, pow_succ', coe_mul, coe_pow n] } theorem iterate_map_one : f^[n] 1 = 1 := f.to_monoid_hom.iterate_map_one n theorem iterate_map_zero : f^[n] 0 = 0 := f.to_add_monoid_hom.iterate_map_zero n theorem iterate_map_add : f^[n] (x + y) = (f^[n] x) + (f^[n] y) := f.to_add_monoid_hom.iterate_map_add n x y theorem iterate_map_mul : f^[n] (x * y) = (f^[n] x) * (f^[n] y) := f.to_monoid_hom.iterate_map_mul n x y theorem iterate_map_pow (a) (n m : β„•) : f^[n] (a^m) = (f^[n] a)^m := f.to_monoid_hom.iterate_map_pow a n m theorem iterate_map_smul (n m : β„•) (x : R) : f^[n] (m β€’β„• x) = m β€’β„• (f^[n] x) := f.to_add_monoid_hom.iterate_map_smul n m x end semiring variables {R : Type*} [ring R] (f : R β†’+* R) (n : β„•) (x y : R) theorem iterate_map_sub : f^[n] (x - y) = (f^[n] x) - (f^[n] y) := f.to_add_monoid_hom.iterate_map_sub n x y theorem iterate_map_neg : f^[n] (-x) = -(f^[n] x) := f.to_add_monoid_hom.iterate_map_neg n x theorem iterate_map_gsmul (n : β„•) (m : β„€) (x : R) : f^[n] (m β€’β„€ x) = m β€’β„€ (f^[n] x) := f.to_add_monoid_hom.iterate_map_gsmul n m x end ring_hom
237cd13cc7d18b12c500c0bb3576f2a6affd64a6
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/number_theory/pell.lean
eb2f2479dfea5b94d133ff91021352acd4f7edff
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
37,599
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 data.zsqrtd.basic namespace pell open nat section parameters {a : β„•} (a1 : a > 1) include a1 private def d := a*a - 1 @[simp] theorem d_pos : 0 < d := nat.sub_pos_of_lt (mul_lt_mul a1 (le_of_lt a1) dec_trivial dec_trivial : 1*1<a*a) /-- The Pell sequences, defined together in mutual recursion. -/ 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 def xz (n : β„•) : β„€ := xn n def yz (n : β„•) : β„€ := yn n def az : β„€ := a 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] 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] 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 simpa [mul_add, mul_comm, mul_left_comm]), ne_of_gt d_pos $ by rw nat.eq_zero_of_le_zero (le_trans (nat.le_add_left _ _) this) at h; exact h⟩ theorem xn_ge_a_pow : βˆ€ (n : β„•), a^n ≀ xn n | 0 := le_refl 1 | (n+1) := by simp [nat.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 [nat.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) : xn n > 0 := 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 β†’ pell_zd n β‰₯ b β†’ βˆƒ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 : b β‰₯ βŸ¨β†‘a, 1⟩ 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 $ zsqrtd.le_trans h $ 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 _) 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 nat.zero_sub 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 [nat.add_sub_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 injection (pell_zd_sub _ h) with h _; repeat {rw ← neg_mul_eq_mul_neg at h}; exact h theorem yz_sub {m n} (h : n ≀ m) : yz (m - n) = xz n * yz m - xz m * yz n := by injection (pell_zd_sub a1 h) with _ h; repeat {rw ← neg_mul_eq_mul_neg at h}; rw [add_comm, mul_comm] at h; exact h theorem xy_coprime (n) : (xn n).coprime (yn n) := nat.coprime_of_dvd' $ Ξ»k 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) (dvd_mul_of_dvd_right kx _) (dvd_mul_of_dvd_right ky _) theorem y_increasing {m} : Ξ  {n}, m < n β†’ yn m < yn n | 0 h := absurd h $ nat.not_lt_zero _ | (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 $ y_increasing 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 x_increasing {m} : Ξ  {n}, m < n β†’ xn m < xn n | 0 h := absurd h $ nat.not_lt_zero _ | (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 $ x_increasing 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) (y_increasing $ 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 _ _) (dvd_mul_of_dvd_left (y_mul_dvd k) _) 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 : m > 0, from m.eq_zero_or_pos.resolve_left $ Ξ»e, by rw [e, nat.mod_zero] at hp; rw [e] at h; exact have 0 < yn a1 n, from y_increasing _ hp, ne_of_lt (y_increasing 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 (y_increasing _ $ nat.mod_lt n m0) (nat.le_of_dvd (y_increasing _ hp) $ co.dvd_of_dvd_mul_right $ (nat.dvd_add_iff_right $ dvd_mul_of_dvd_right (y_mul_dvd _ _ _) _).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 modeq.modeq_add (modeq.modeq_mul_right _ hx) $ modeq.modeq_zero_iff.2 $ by rw nat.pow_succ; exact mul_dvd_mul_right (dvd_mul_of_dvd_right (modeq.modeq_zero_iff.1 $ (hy.modeq_of_dvd_of_modeq $ by simp [nat.pow_succ]).trans $ modeq.modeq_zero_iff.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.modeq_add (by rw nat.pow_succ; exact modeq.modeq_mul_right' _ hx) $ 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 [nat.pow_succ, mul_comm, mul_left_comm], by rw ← this; exact modeq.modeq_mul_right _ hy, by rw [nat.add_sub_cancel, nat.mul_succ, xn_add, yn_add, nat.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.modeq_zero_iff.1 $ ((xy_modeq_yn n (yn n)).right.modeq_of_dvd_of_modeq $ by simp [nat.pow_succ]).trans (modeq.modeq_zero_iff.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 rw n0; rw n0 at nt; exact nt) $ Ξ»(n0l : n > 0), let ⟨k, ke⟩ := nt in have yn n ∣ k * (xn n)^(k-1), from nat.dvd_of_mul_dvd_mul_right (y_increasing n0l) $ modeq.modeq_zero_iff.1 $ by have xm := (xy_modeq_yn a1 n k).right; rw ← ke at xm; exact (xm.modeq_of_dvd_of_modeq $ by simp [nat.pow_succ]).symm.trans (modeq.modeq_zero_iff.2 h), 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; change az a1 with a; simp [mul_add, add_mul], by simpa [mul_add, mul_comm, mul_left_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, rw [← int.cast_coe_nat, zsqrtd.smul_val] 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) := modeq.modeq_add_cancel_right (yn_modeq_a_sub_one n) $ have 2*(n+1) = n+2+n, by simp [two_mul], by rw [yn_succ_succ, ← this]; refine modeq.modeq_mul (modeq.modeq_mul_left 2 (_ : a ≑ 1 [MOD a-1])) (yn_modeq_a_sub_one (n+1)); exact (modeq.modeq_of_dvd $ by rw [int.coe_nat_sub $ le_of_lt a1]; apply dvd_refl).symm theorem yn_modeq_two : βˆ€ n, yn n ≑ n [MOD 2] | 0 := by simp | 1 := by simp | (n+2) := modeq.modeq_add_cancel_right (yn_modeq_two n) $ have 2*(n+1) = n+2+n, by simp [two_mul], by rw [yn_succ_succ, ← this]; refine modeq.modeq_mul _ (yn_modeq_two (n+1)); exact modeq.trans (modeq.modeq_zero_iff.2 $ by simp) (modeq.modeq_zero_iff.2 $ by simp).symm -- TODO(Mario): Hopefully a tactic will be able to dispense this lemma 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) := calc (a2 * yn1 - yn0) * ay + y2 - (a2 * xn1 - xn0) = a2 * yn1 * ay - yn0 * ay + y2 - (a2 * xn1 - xn0) : by rw [mul_sub_right_distrib] ... = y2 + a2 * (yn1 * ay) - a2 * xn1 - yn0 * ay + xn0 : by simp [mul_comm, mul_left_comm] ... = y2 + a2 * (yn1 * ay) - a2 * y1 + a2 * y1 - a2 * xn1 - yn0 * ay + y0 - y0 + xn0 : by rw [add_sub_cancel, sub_add_cancel] ... = y2 - a2 * y1 + y0 + a2 * (yn1 * ay + y1 - xn1) - (yn0 * ay + y0 - xn0) : by simp [mul_add] 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 [nat.pow_succ, mul_add, int.coe_nat_mul, show ((2:β„•):β„€) = 2, from rfl, mul_comm, mul_left_comm]⟩, by rw [xz_succ_succ, yz_succ_succ, x_sub_y_dvd_pow_lem a1 ↑(y^(n+2)) ↑(y^(n+1)) ↑(y^n)]; exact dvd_sub (dvd_add this $ dvd_mul_of_dvd_right (x_sub_y_dvd_pow (n+1)) _) (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] := by rw [two_mul, add_assoc, xn_add, add_assoc]; exact show _ ≑ 0+0 [MOD xn a1 n], from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_right (xn a1 n) (xn a1 (n + j))) $ by rw [yn_add, left_distrib, add_assoc]; exact show _ ≑ 0+0 [MOD xn a1 n], from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_of_dvd_right (dvd_mul_right _ _) _) $ modeq.modeq_zero_iff.2 $ xn_modeq_x2n_add_lem _ _ _ 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_of_dvd_right (dvd_mul_right _ _) _), by rw [two_mul, nat.add_sub_assoc h, xn_add, add_assoc]; exact show _ ≑ 0+0 [MOD xn a1 n], from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_right _ _) $ modeq.modeq_zero_iff.2 $ int.coe_nat_dvd.1 $ by simpa [xz, yz] using h1 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 [nat.sub_add_cancel 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 [nat.sub_sub_self h, add_comm] at t) theorem xn_modeq_x4n_add (n j) : xn (4 * n + j) ≑ xn j [MOD xn n] := modeq.modeq_add_cancel_right (modeq.refl $ 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.modeq_add_cancel_right (modeq.refl $ 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, nat.add_sub_assoc h']; apply xn_modeq_x2n_add theorem eq_of_xn_modeq_lem1 {i n} (npos : n > 0) : Ξ  {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 (x_increasing _ (nat.lt_of_succ_lt jn)), nat.mod_eq_of_lt (x_increasing _ jn)]; exact x_increasing _ (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) (y_increasing a1 np))) h, by cases this; simp at h; exact ⟨h.symm, rfl⟩ theorem eq_of_xn_modeq_lem3 {i n} (npos : n > 0) : Ξ  {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 nat.sub_add_cancel, 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 $ x_increasing 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.modeq_add_cancel_right (modeq.refl (xn a1 (2 * n - k))), rw [nat.sub_add_cancel xle], have t := xn_modeq_x2n_sub_lem a1 (le_of_lt k2nl), rw nat.sub_sub_self k2n at t, exact t.trans (modeq.modeq_zero_iff.2 $ dvd_refl _).symm }, (lt_trichotomy j n).elim (Ξ» (jn : j < n), eq_of_xn_modeq_lem1 npos 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, ← nat.sub_sub, nat.add_sub_cancel]], 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 (x_increasing _ 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 [nat.sub_add_cancel 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 := by 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 (x_increasing 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 rw[nat.sub_add_cancel npos]; exact e) in have n1 : n = 1, from le_antisymm (nat.le_of_sub_eq_zero s1) npos, by rw [ile, a2, n1]; exact ⟨rfl, rfl, rfl, rfl⟩ } }, { rw [ein, nat.mod_self, add_zero], exact x_increasing _ (nat.pred_lt $ ne_of_gt npos) } }) (Ξ» (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 $ x_increasing _ _) _, rw [nat.sub_succ], exact nat.pred_lt (ne_of_gt $ nat.sub_pos_of_lt j2n) }) theorem eq_of_xn_modeq_le {i j n} (npos : n > 0) (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 := (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 : xn a1 0 % xn a1 n > 0 := by rw [nat.mod_eq_of_lt (x_increasing a1 npos)]; exact dec_trivial, cases i with i, exact x0, rw jn at ij', exact lt_trans x0 (eq_of_xn_modeq_lem3 _ 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 npos ij' j2n jn ntriv) h theorem eq_of_xn_modeq {i j n} (npos : n > 0) (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 npos ij j2n h $ λ⟨a2, n1, i0, j2⟩, (ntriv a2 n1).left i0 j2) (Ξ»ij, (eq_of_xn_modeq_le npos ij i2n h.symm $ λ⟨a2, n1, j0, i2⟩, (ntriv a2 n1).right i2 j0).symm) theorem eq_of_xn_modeq' {i j n} (ipos : i > 0) (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, have npos : n > 0, from lt_of_lt_of_le ipos hin, (le_or_gt j (2 * n)).imp (Ξ»j2n : j ≀ 2*n, eq_of_xn_modeq npos 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 : j > 2*n, suffices i = 4*n - j, by rw [this, nat.add_sub_of_le j4n], have j42n : 4*n - j ≀ 2*n, from @nat.le_of_add_le_add_right j _ _ $ by rw [nat.sub_add_cancel 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 npos i2n j42n (h.symm.trans $ let t := xn_modeq_x4n_sub j42n in by rwa [nat.sub_sub_self 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 : i > 0) (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 : 4 * n > 0, from mul_pos dec_trivial (lt_of_lt_of_le ipos 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 modeq.trans (xn_modeq_x4n_add _ _ _) IH end, or.imp (Ξ»(ji : j' = i), by rwa ← ji) (Ξ»(ji : j' + i = 4 * n), (modeq.modeq_add jj (modeq.refl _)).trans $ by rw ji; exact modeq.modeq_zero_iff.2 (dvd_refl _)) (eq_of_xn_modeq' ipos hin (le_of_lt jl) $ (modeq.symm (by rw ← nat.mod_add_div j (4*n); exact this j' _)).trans h) end theorem xy_modeq_of_modeq {a b c} (a1 : a > 1) (b1 : b > 1) (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) := ⟨ modeq.modeq_add_cancel_right (xy_modeq_of_modeq n).left $ by rw [xn_succ_succ a1, xn_succ_succ b1]; exact modeq.modeq_mul (modeq.modeq_mul_left _ h) (xy_modeq_of_modeq (n+1)).left, modeq.modeq_add_cancel_right (xy_modeq_of_modeq n).right $ by rw [yn_succ_succ a1, yn_succ_succ b1]; exact modeq.modeq_mul (modeq.modeq_mul_left _ h) (xy_modeq_of_modeq (n+1)).right⟩ theorem matiyasevic {a k x y} : (βˆƒ a1 : a > 1, xn a1 k = x ∧ yn a1 k = y) ↔ a > 1 ∧ 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 ∧ b > 1 ∧ b ≑ 1 [MOD 4 * y] ∧ b ≑ a [MOD u] ∧ v > 0 ∧ 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_refl _, 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 dvd_trans (ysq_dvd_yy a1 k) $ (y_dvd_iff _ _ _).2 $ dvd_mul_left _ _, have uco : nat.coprime u (4 * y), from have 2 ∣ v, from modeq.modeq_zero_iff.1 $ (yn_modeq_two _ _).trans $ modeq.modeq_zero_iff.2 (dvd_mul_right _ _), 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⟩ := modeq.chinese_remainder uco a 1 in have m1 : 1 < m, from have 0 < k * y, from mul_pos kpos (y_increasing a1 kpos), nat.mul_le_mul_left 2 this, have vp : v > 0, from y_increasing a1 (lt_trans zero_lt_one m1), have b1 : b > 1, from have u > xn a1 1, from x_increasing a1 m1, have u > a, 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 modeq.dvd_of_modeq bm1.symm, modeq.modeq_of_dvd_of_modeq this $ yn_modeq_a_sub_one _ _, ⟨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 : yn a1 n > 0), (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)) (y_increasing 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 modeq.dvd_of_modeq bm1.symm, (modeq.modeq_of_dvd_of_modeq this (yn_modeq_a_sub_one b1 _)).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 (y_increasing 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.modeq_zero_iff.1 $ (modeq.modeq_add jk.symm (modeq.refl i)).trans $ modeq.modeq_of_dvd_of_modeq yd ji, by have : i % (4 * yn a1 i) = k % (4 * yn a1 i) := (modeq.modeq_of_dvd_of_modeq yd ji).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} (a1 : 1 < a) (ypos : y > 0) : k > 0 β†’ a > y^k β†’ (↑(y^k) : β„€) < 2*a*y - y*y - 1 := have y < a β†’ 2*a*y β‰₯ a + (y*y + 1), begin intro ya, induction y with y IH, exact absurd ypos (lt_irrefl _), cases nat.eq_zero_or_pos y with y0 ypos, { rw y0, simp [two_mul], apply add_le_add_left, exact a1 }, { rw [nat.mul_succ, nat.mul_succ, nat.succ_mul y], have : 2 * a β‰₯ y + nat.succ y, { change y + y < 2 * a, rw ← two_mul, exact mul_lt_mul_of_pos_left (nat.lt_of_succ_lt ya) dec_trivial }, have := add_le_add (IH ypos (nat.lt_of_succ_lt ya)) this, simp at this, simp, exact this } end, Ξ»k0 yak, lt_of_lt_of_le (int.coe_nat_lt_coe_nat_of_lt yak) $ by rw sub_sub; apply le_sub_right_of_add_le; apply int.coe_nat_le_coe_nat_of_le; have y1 := nat.pow_le_pow_of_le_right ypos k0; simp at y1; exact this (lt_of_le_of_lt y1 yak) theorem eq_pow_of_pell {m n k} : (n^k = m ↔ k = 0 ∧ m = 1 ∨ k > 0 ∧ (n = 0 ∧ m = 0 ∨ n > 0 ∧ βˆƒ (w a t z : β„•) (a1 : a > 1), 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)) := ⟨λe, by rw ← e; refine (nat.eq_zero_or_pos k).elim (Ξ»k0, by rw k0; exact or.inl ⟨rfl, rfl⟩) (Ξ»kpos, or.inr ⟨kpos, _⟩); refine (nat.eq_zero_or_pos n).elim (Ξ»n0, by rw [n0, nat.zero_pow kpos]; exact or.inl ⟨rfl, rfl⟩) (Ξ»npos, or.inr ⟨npos, _⟩); exact let w := _root_.max n k in have nw : n ≀ w, from le_max_left _ _, have kw : k ≀ w, from le_max_right _ _, have wpos : w > 0, from lt_of_lt_of_le npos nw, have w1 : w + 1 > 1, from nat.succ_lt_succ wpos, let a := xn w1 w in have a1 : a > 1, from x_increasing w1 wpos, let x := xn a1 k, y := yn a1 k in let ⟨z, ze⟩ := show w ∣ yn w1 w, from modeq.modeq_zero_iff.1 $ modeq.trans (yn_modeq_a_sub_one w1 w) (modeq.modeq_zero_iff.2 $ dvd_refl _) in have nt : (↑(n^k) : β„€) < 2 * a * n - n * n - 1, from eq_pow_of_pell_lem a1 npos kpos $ calc n^k ≀ n^w : nat.pow_le_pow_of_le_right npos 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, let ⟨t, te⟩ := int.eq_coe_of_zero_le $ le_trans (int.coe_zero_le _) $ le_of_lt nt in have na : n ≀ a, from le_trans nw $ le_of_lt $ n_lt_xn w1 w, have tm : x ≑ y * (a - n) + n^k [MOD t], begin apply modeq.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 end, have ta : 2 * a * n = t + (n * n + 1), from int.coe_nat_inj $ by rw [int.coe_nat_add, ← te, sub_sub]; repeat {rw int.coe_nat_add <|> rw int.coe_nat_mul}; rw [int.coe_nat_one, sub_add_cancel]; refl, have mt : n^k < t, from int.lt_of_coe_nat_lt_coe_nat $ by rw ← te; exact nt, have zp : a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1, by rw ← ze; exact pell_eq w1 w, ⟨w, a, t, z, a1, tm, ta, mt, nw, kw, zp⟩, Ξ»o, match o with | or.inl ⟨k0, m1⟩ := by rw [k0, m1]; refl | or.inr ⟨kpos, or.inl ⟨n0, m0⟩⟩ := by rw [n0, m0, nat.zero_pow kpos] | or.inr ⟨kpos, or.inr ⟨npos, w, a, t, z, (a1 : a > 1), (tm : xn a1 k ≑ yn a1 k * (a - n) + m [MOD t]), (ta : 2 * a * n = t + (n * n + 1)), (mt : m < t), (nw : n ≀ w), (kw : k ≀ w), (zp : a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)⟩⟩ := have wpos : w > 0, from lt_of_lt_of_le npos nw, have w1 : w + 1 > 1, from nat.succ_lt_succ wpos, let ⟨j, xj, yj⟩ := eq_pell w1 zp in by clear _match o _let_match; exact have jpos : j > 0, from (nat.eq_zero_or_pos j).resolve_left $ Ξ»j0, have a1 : a = 1, by rw j0 at xj; exact xj, have 2 * n = t + (n * n + 1), by rw a1 at ta; exact ta, have n1 : n = 1, from have n * n < n * 2, by rw [mul_comm n 2, this]; apply nat.le_add_left, have n ≀ 1, from nat.le_of_lt_succ $ lt_of_mul_lt_mul_left this (nat.zero_le _), le_antisymm this npos, by rw n1 at this; rw ← @nat.add_right_cancel 0 2 t this at mt; exact nat.not_lt_zero _ mt, have wj : w ≀ j, from nat.le_of_dvd jpos $ modeq.modeq_zero_iff.1 $ (yn_modeq_a_sub_one w1 j).symm.trans $ modeq.modeq_zero_iff.2 ⟨z, yj.symm⟩, have nt : (↑(n^k) : β„€) < 2 * a * n - n * n - 1, from eq_pow_of_pell_lem a1 npos kpos $ calc n^k ≀ n^j : nat.pow_le_pow_of_le_right npos (le_trans kw wj) ... < (w + 1)^j : nat.pow_lt_pow_of_lt_left (nat.lt_succ_of_le nw) jpos ... ≀ xn w1 j : xn_ge_a_pow w1 j ... = a : xj.symm, have na : n ≀ a, by rw xj; exact le_trans (le_trans nw wj) (le_of_lt $ n_lt_xn _ _), have te : (t : β„€) = 2 * ↑a * ↑n - ↑n * ↑n - 1, by rw sub_sub; apply eq_sub_of_add_eq; apply (int.coe_nat_eq_coe_nat_iff _ _).2; exact ta.symm, have xn a1 k ≑ yn a1 k * (a - n) + n^k [MOD t], by have := x_sub_y_dvd_pow a1 n k; rw [← te, ← int.coe_nat_sub na] at this; exact modeq.modeq_of_dvd this, have n^k % t = m % t, from modeq.modeq_add_cancel_left (modeq.refl _) (this.symm.trans tm), by rw ← te at nt; rwa [nat.mod_eq_of_lt (int.lt_of_coe_nat_lt_coe_nat nt), nat.mod_eq_of_lt mt] at this end⟩ end pell
3960bde033afcf7aa65279bd3e61cfb790dfc427
e151e9053bfd6d71740066474fc500a087837323
/src/hott/prop_trunc.lean
4c99ec1e2c7354680de9aed466316eb66f68916a
[ "Apache-2.0" ]
permissive
daniel-carranza/hott3
15bac2d90589dbb952ef15e74b2837722491963d
913811e8a1371d3a5751d7d32ff9dec8aa6815d9
refs/heads/master
1,610,091,349,670
1,596,222,336,000
1,596,222,336,000
241,957,822
0
0
Apache-2.0
1,582,222,839,000
1,582,222,838,000
null
UTF-8
Lean
false
false
1,980
lean
/- 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, Floris van Doorn Proof of the @[hott] theorem that (is_trunc n A) is a mere proposition We prove this here to avoid circular dependency of files We want to use this in .equiv; .equiv is imported by .function and .function is imported by .trunc -/ import hott.types.pi universe u namespace hott hott_theory open hott.sigma namespace is_trunc @[hott] def is_contr.sigma_char (A : Type u) : (Ξ£ (center : A), Ξ  (a : A), center = a) ≃ (is_contr A) := begin fapply equiv.MK, { intro S, exact (is_contr.mk S.1 S.2)}, { intro H, cases H with H', cases H' with ce co, exact ⟨ce, co⟩}, { intro H, cases H with H', cases H' with ce co, exact idp}, { intro S, cases S, apply idp} end @[hott] def is_trunc.pi_char (n : β„•β‚‹β‚‚) (A : Type u) : (Ξ  (x y : A), is_trunc n (x = y)) ≃ (is_trunc (n .+1) A) := begin fapply equiv.MK, { exact is_trunc_succ_intro}, { introsI H x y, apply is_trunc_eq}, { intro H, cases H, apply idp}, { introI P, apply eq_of_homotopy, intro a, apply eq_of_homotopy, intro b, change is_trunc.mk (to_internal n (a = b)) = P a b, induction (P a b), apply idp}, end @[hott] lemma is_prop_is_trunc (n : β„•β‚‹β‚‚) (A : Type u) : is_prop (is_trunc n A) := begin induction n generalizing A, { apply is_trunc_equiv_closed, { apply is_contr.sigma_char }, apply is_prop.mk, intros, fapply sigma_eq, apply x.2, apply is_prop.elimo', apply pi.is_prop_pi_eq }, { apply is_trunc_equiv_closed, apply is_trunc.pi_char, unfreezeI, apply_instance }, end local attribute [instance] is_prop_is_trunc @[hott, instance] def is_trunc_succ_is_trunc (n m : β„•β‚‹β‚‚) (A : Type u) : is_trunc (n.+1) (is_trunc m A) := is_trunc_succ_of_is_prop (is_trunc m A) n end is_trunc end hott
c1189e45399f3835bbf5cb5819b220d8a116c042
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/data/multiset/basic.lean
60f0aa4328b99b6c91e0f5d2c678e64478980571
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
105,383
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.list.perm import data.list.prod_monoid /-! # Multisets These are implemented as the quotient of a list by permutations. ## Notation We define the global infix notation `::β‚˜` for `multiset.cons`. -/ open list subtype nat variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} /-- `multiset Ξ±` is the quotient of `list Ξ±` by list permutation. The result is a type of finite sets with duplicates allowed. -/ def {u} multiset (Ξ± : Type u) : Type u := quotient (list.is_setoid Ξ±) namespace multiset instance : has_coe (list Ξ±) (multiset Ξ±) := ⟨quot.mk _⟩ @[simp] theorem quot_mk_to_coe (l : list Ξ±) : @eq (multiset Ξ±) ⟦l⟧ l := rfl @[simp] theorem quot_mk_to_coe' (l : list Ξ±) : @eq (multiset Ξ±) (quot.mk (β‰ˆ) l) l := rfl @[simp] theorem quot_mk_to_coe'' (l : list Ξ±) : @eq (multiset Ξ±) (quot.mk setoid.r l) l := rfl @[simp] theorem coe_eq_coe {l₁ lβ‚‚ : list Ξ±} : (l₁ : multiset Ξ±) = lβ‚‚ ↔ l₁ ~ lβ‚‚ := quotient.eq instance has_decidable_eq [decidable_eq Ξ±] : decidable_eq (multiset Ξ±) | s₁ sβ‚‚ := quotient.rec_on_subsingletonβ‚‚ s₁ sβ‚‚ $ Ξ» l₁ lβ‚‚, decidable_of_iff' _ quotient.eq /-- defines a size for a multiset by referring to the size of the underlying list -/ protected def sizeof [has_sizeof Ξ±] (s : multiset Ξ±) : β„• := quot.lift_on s sizeof $ Ξ» l₁ lβ‚‚, perm.sizeof_eq_sizeof instance has_sizeof [has_sizeof Ξ±] : has_sizeof (multiset Ξ±) := ⟨multiset.sizeof⟩ /-! ### Empty multiset -/ /-- `0 : multiset Ξ±` is the empty set -/ protected def zero : multiset Ξ± := @nil Ξ± instance : has_zero (multiset Ξ±) := ⟨multiset.zero⟩ instance : has_emptyc (multiset Ξ±) := ⟨0⟩ instance inhabited_multiset : inhabited (multiset Ξ±) := ⟨0⟩ @[simp] theorem coe_nil_eq_zero : (@nil Ξ± : multiset Ξ±) = 0 := rfl @[simp] theorem empty_eq_zero : (βˆ… : multiset Ξ±) = 0 := rfl theorem coe_eq_zero (l : list Ξ±) : (l : multiset Ξ±) = 0 ↔ l = [] := iff.trans coe_eq_coe perm_nil /-! ### `multiset.cons` -/ /-- `cons a s` is the multiset which contains `s` plus one more instance of `a`. -/ def cons (a : Ξ±) (s : multiset Ξ±) : multiset Ξ± := quot.lift_on s (Ξ» l, (a :: l : multiset Ξ±)) (Ξ» l₁ lβ‚‚ p, quot.sound (p.cons a)) infixr ` ::β‚˜ `:67 := multiset.cons instance : has_insert Ξ± (multiset Ξ±) := ⟨cons⟩ @[simp] theorem insert_eq_cons (a : Ξ±) (s : multiset Ξ±) : insert a s = a ::β‚˜ s := rfl @[simp] theorem cons_coe (a : Ξ±) (l : list Ξ±) : (a ::β‚˜ l : multiset Ξ±) = (a::l : list Ξ±) := rfl theorem singleton_coe (a : Ξ±) : (a ::β‚˜ 0 : multiset Ξ±) = ([a] : list Ξ±) := rfl @[simp] theorem cons_inj_left {a b : Ξ±} (s : multiset Ξ±) : a ::β‚˜ s = b ::β‚˜ s ↔ a = b := ⟨quot.induction_on s $ Ξ» l e, have [a] ++ l ~ [b] ++ l, from quotient.exact e, singleton_perm_singleton.1 $ (perm_append_right_iff _).1 this, congr_arg _⟩ @[simp] theorem cons_inj_right (a : Ξ±) : βˆ€{s t : multiset Ξ±}, a ::β‚˜ s = a ::β‚˜ t ↔ s = t := by rintros ⟨lβ‚βŸ© ⟨lβ‚‚βŸ©; simp @[recursor 5] protected theorem induction {p : multiset Ξ± β†’ Prop} (h₁ : p 0) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : multiset Ξ±}, p s β†’ p (a ::β‚˜ s)) : βˆ€s, p s := by rintros ⟨l⟩; induction l with _ _ ih; [exact h₁, exact hβ‚‚ ih] @[elab_as_eliminator] protected theorem induction_on {p : multiset Ξ± β†’ Prop} (s : multiset Ξ±) (h₁ : p 0) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : multiset Ξ±}, p s β†’ p (a ::β‚˜ s)) : p s := multiset.induction h₁ hβ‚‚ s theorem cons_swap (a b : Ξ±) (s : multiset Ξ±) : a ::β‚˜ b ::β‚˜ s = b ::β‚˜ a ::β‚˜ s := quot.induction_on s $ Ξ» l, quotient.sound $ perm.swap _ _ _ section rec variables {C : multiset Ξ± β†’ Sort*} /-- Dependent recursor on multisets. TODO: should be @[recursor 6], but then the definition of `multiset.pi` fails with a stack overflow in `whnf`. -/ protected def rec (C_0 : C 0) (C_cons : Ξ a m, C m β†’ C (a ::β‚˜ m)) (C_cons_heq : βˆ€ a a' m b, C_cons a (a' ::β‚˜ m) (C_cons a' m b) == C_cons a' (a ::β‚˜ m) (C_cons a m b)) (m : multiset Ξ±) : C m := quotient.hrec_on m (@list.rec Ξ± (Ξ»l, C ⟦l⟧) C_0 (Ξ»a l b, C_cons a ⟦l⟧ b)) $ assume l l' h, h.rec_heq (assume a l l' b b' hl, have ⟦l⟧ = ⟦l'⟧, from quot.sound hl, by cc) (assume a a' l, C_cons_heq a a' ⟦l⟧) /-- Companion to `multiset.rec` with more convenient argument order. -/ @[elab_as_eliminator] protected def rec_on (m : multiset Ξ±) (C_0 : C 0) (C_cons : Ξ a m, C m β†’ C (a ::β‚˜ m)) (C_cons_heq : βˆ€a a' m b, C_cons a (a' ::β‚˜ m) (C_cons a' m b) == C_cons a' (a ::β‚˜ m) (C_cons a m b)) : C m := multiset.rec C_0 C_cons C_cons_heq m variables {C_0 : C 0} {C_cons : Ξ a m, C m β†’ C (a ::β‚˜ m)} {C_cons_heq : βˆ€a a' m b, C_cons a (a' ::β‚˜ m) (C_cons a' m b) == C_cons a' (a ::β‚˜ m) (C_cons a m b)} @[simp] lemma rec_on_0 : @multiset.rec_on Ξ± C (0:multiset Ξ±) C_0 C_cons C_cons_heq = C_0 := rfl @[simp] lemma rec_on_cons (a : Ξ±) (m : multiset Ξ±) : (a ::β‚˜ m).rec_on C_0 C_cons C_cons_heq = C_cons a m (m.rec_on C_0 C_cons C_cons_heq) := quotient.induction_on m $ assume l, rfl end rec section mem /-- `a ∈ s` means that `a` has nonzero multiplicity in `s`. -/ def mem (a : Ξ±) (s : multiset Ξ±) : Prop := quot.lift_on s (Ξ» l, a ∈ l) (Ξ» l₁ lβ‚‚ (e : l₁ ~ lβ‚‚), propext $ e.mem_iff) instance : has_mem Ξ± (multiset Ξ±) := ⟨mem⟩ @[simp] lemma mem_coe {a : Ξ±} {l : list Ξ±} : a ∈ (l : multiset Ξ±) ↔ a ∈ l := iff.rfl instance decidable_mem [decidable_eq Ξ±] (a : Ξ±) (s : multiset Ξ±) : decidable (a ∈ s) := quot.rec_on_subsingleton s $ list.decidable_mem a @[simp] theorem mem_cons {a b : Ξ±} {s : multiset Ξ±} : a ∈ b ::β‚˜ s ↔ a = b ∨ a ∈ s := quot.induction_on s $ Ξ» l, iff.rfl lemma mem_cons_of_mem {a b : Ξ±} {s : multiset Ξ±} (h : a ∈ s) : a ∈ b ::β‚˜ s := mem_cons.2 $ or.inr h @[simp] theorem mem_cons_self (a : Ξ±) (s : multiset Ξ±) : a ∈ a ::β‚˜ s := mem_cons.2 (or.inl rfl) theorem forall_mem_cons {p : Ξ± β†’ Prop} {a : Ξ±} {s : multiset Ξ±} : (βˆ€ x ∈ (a ::β‚˜ s), p x) ↔ p a ∧ βˆ€ x ∈ s, p x := quotient.induction_on' s $ Ξ» L, list.forall_mem_cons theorem exists_cons_of_mem {s : multiset Ξ±} {a : Ξ±} : a ∈ s β†’ βˆƒ t, s = a ::β‚˜ t := quot.induction_on s $ Ξ» l (h : a ∈ l), let ⟨l₁, lβ‚‚, e⟩ := mem_split h in e.symm β–Έ ⟨(l₁++lβ‚‚ : list Ξ±), quot.sound perm_middle⟩ @[simp] theorem not_mem_zero (a : Ξ±) : a βˆ‰ (0 : multiset Ξ±) := id theorem eq_zero_of_forall_not_mem {s : multiset Ξ±} : (βˆ€x, x βˆ‰ s) β†’ s = 0 := quot.induction_on s $ Ξ» l H, by rw eq_nil_iff_forall_not_mem.mpr H; refl theorem eq_zero_iff_forall_not_mem {s : multiset Ξ±} : s = 0 ↔ βˆ€ a, a βˆ‰ s := ⟨λ h, h.symm β–Έ Ξ» _, not_false, eq_zero_of_forall_not_mem⟩ theorem exists_mem_of_ne_zero {s : multiset Ξ±} : s β‰  0 β†’ βˆƒ a : Ξ±, a ∈ s := quot.induction_on s $ assume l hl, match l, hl with | [] := assume h, false.elim $ h rfl | (a :: l) := assume _, ⟨a, by simp⟩ end @[simp] lemma zero_ne_cons {a : Ξ±} {m : multiset Ξ±} : 0 β‰  a ::β‚˜ m := assume h, have a ∈ (0:multiset Ξ±), from h.symm β–Έ mem_cons_self _ _, not_mem_zero _ this @[simp] lemma cons_ne_zero {a : Ξ±} {m : multiset Ξ±} : a ::β‚˜ m β‰  0 := zero_ne_cons.symm lemma cons_eq_cons {a b : Ξ±} {as bs : multiset Ξ±} : a ::β‚˜ as = b ::β‚˜ bs ↔ ((a = b ∧ as = bs) ∨ (a β‰  b ∧ βˆƒcs, as = b ::β‚˜ cs ∧ bs = a ::β‚˜ cs)) := begin haveI : decidable_eq Ξ± := classical.dec_eq Ξ±, split, { assume eq, by_cases a = b, { subst h, simp * at * }, { have : a ∈ b ::β‚˜ bs, from eq β–Έ mem_cons_self _ _, have : a ∈ bs, by simpa [h], rcases exists_cons_of_mem this with ⟨cs, hcs⟩, simp [h, hcs], have : a ::β‚˜ as = b ::β‚˜ a ::β‚˜ cs, by simp [eq, hcs], have : a ::β‚˜ as = a ::β‚˜ b ::β‚˜ cs, by rwa [cons_swap], simpa using this } }, { assume h, rcases h with ⟨eq₁, eqβ‚‚βŸ© | ⟨h, cs, eq₁, eqβ‚‚βŸ©, { simp * }, { simp [*, cons_swap a b] } } end end mem /-! ### `multiset.subset` -/ section subset /-- `s βŠ† t` is the lift of the list subset relation. It means that any element with nonzero multiplicity in `s` has nonzero multiplicity in `t`, but it does not imply that the multiplicity of `a` in `s` is less or equal than in `t`; see `s ≀ t` for this relation. -/ protected def subset (s t : multiset Ξ±) : Prop := βˆ€ ⦃a : α⦄, a ∈ s β†’ a ∈ t instance : has_subset (multiset Ξ±) := ⟨multiset.subset⟩ @[simp] theorem coe_subset {l₁ lβ‚‚ : list Ξ±} : (l₁ : multiset Ξ±) βŠ† lβ‚‚ ↔ l₁ βŠ† lβ‚‚ := iff.rfl @[simp] theorem subset.refl (s : multiset Ξ±) : s βŠ† s := Ξ» a h, h theorem subset.trans {s t u : multiset Ξ±} : s βŠ† t β†’ t βŠ† u β†’ s βŠ† u := Ξ» h₁ hβ‚‚ a m, hβ‚‚ (h₁ m) theorem subset_iff {s t : multiset Ξ±} : s βŠ† t ↔ (βˆ€β¦ƒx⦄, x ∈ s β†’ x ∈ t) := iff.rfl theorem mem_of_subset {s t : multiset Ξ±} {a : Ξ±} (h : s βŠ† t) : a ∈ s β†’ a ∈ t := @h _ @[simp] theorem zero_subset (s : multiset Ξ±) : 0 βŠ† s := Ξ» a, (not_mem_nil a).elim @[simp] theorem cons_subset {a : Ξ±} {s t : multiset Ξ±} : (a ::β‚˜ s) βŠ† t ↔ a ∈ t ∧ s βŠ† t := by simp [subset_iff, or_imp_distrib, forall_and_distrib] theorem eq_zero_of_subset_zero {s : multiset Ξ±} (h : s βŠ† 0) : s = 0 := eq_zero_of_forall_not_mem h theorem subset_zero {s : multiset Ξ±} : s βŠ† 0 ↔ s = 0 := ⟨eq_zero_of_subset_zero, Ξ» xeq, xeq.symm β–Έ subset.refl 0⟩ lemma induction_on' {p : multiset Ξ± β†’ Prop} (S : multiset Ξ±) (h₁ : p βˆ…) (hβ‚‚ : βˆ€ {a s}, a ∈ S β†’ s βŠ† S β†’ p s β†’ p (insert a s)) : p S := @multiset.induction_on Ξ± (Ξ» T, T βŠ† S β†’ p T) S (Ξ» _, h₁) (Ξ» a s hps hs, let ⟨hS, sS⟩ := cons_subset.1 hs in hβ‚‚ hS sS (hps sS)) (subset.refl S) end subset section to_list /-- Produces a list of the elements in the multiset using choice. -/ @[reducible] noncomputable def to_list {Ξ± : Type*} (s : multiset Ξ±) := classical.some (quotient.exists_rep s) @[simp] lemma to_list_zero {Ξ± : Type*} : (multiset.to_list 0 : list Ξ±) = [] := (multiset.coe_eq_zero _).1 (classical.some_spec (quotient.exists_rep multiset.zero)) @[simp, norm_cast] lemma coe_to_list {Ξ± : Type*} (s : multiset Ξ±) : (s.to_list : multiset Ξ±) = s := classical.some_spec (quotient.exists_rep _) @[simp] lemma mem_to_list {Ξ± : Type*} (a : Ξ±) (s : multiset Ξ±) : a ∈ s.to_list ↔ a ∈ s := by rw [←multiset.mem_coe, multiset.coe_to_list] end to_list /-! ### Partial order on `multiset`s -/ /-- `s ≀ t` means that `s` is a sublist of `t` (up to permutation). Equivalently, `s ≀ t` means that `count a s ≀ count a t` for all `a`. -/ protected def le (s t : multiset Ξ±) : Prop := quotient.lift_onβ‚‚ s t (<+~) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, propext (pβ‚‚.subperm_left.trans p₁.subperm_right) instance : partial_order (multiset Ξ±) := { le := multiset.le, le_refl := by rintros ⟨l⟩; exact subperm.refl _, le_trans := by rintros ⟨lβ‚βŸ© ⟨lβ‚‚βŸ© ⟨lβ‚ƒβŸ©; exact @subperm.trans _ _ _ _, le_antisymm := by rintros ⟨lβ‚βŸ© ⟨lβ‚‚βŸ© h₁ hβ‚‚; exact quot.sound (subperm.antisymm h₁ hβ‚‚) } theorem subset_of_le {s t : multiset Ξ±} : s ≀ t β†’ s βŠ† t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, subperm.subset theorem mem_of_le {s t : multiset Ξ±} {a : Ξ±} (h : s ≀ t) : a ∈ s β†’ a ∈ t := mem_of_subset (subset_of_le h) @[simp] theorem coe_le {l₁ lβ‚‚ : list Ξ±} : (l₁ : multiset Ξ±) ≀ lβ‚‚ ↔ l₁ <+~ lβ‚‚ := iff.rfl @[elab_as_eliminator] theorem le_induction_on {C : multiset Ξ± β†’ multiset Ξ± β†’ Prop} {s t : multiset Ξ±} (h : s ≀ t) (H : βˆ€ {l₁ lβ‚‚ : list Ξ±}, l₁ <+ lβ‚‚ β†’ C l₁ lβ‚‚) : C s t := quotient.induction_onβ‚‚ s t (Ξ» l₁ lβ‚‚ ⟨l, p, s⟩, (show ⟦l⟧ = ⟦lβ‚βŸ§, from quot.sound p) β–Έ H s) h theorem zero_le (s : multiset Ξ±) : 0 ≀ s := quot.induction_on s $ Ξ» l, (nil_sublist l).subperm theorem le_zero {s : multiset Ξ±} : s ≀ 0 ↔ s = 0 := ⟨λ h, le_antisymm h (zero_le _), le_of_eq⟩ theorem lt_cons_self (s : multiset Ξ±) (a : Ξ±) : s < a ::β‚˜ s := quot.induction_on s $ Ξ» l, suffices l <+~ a :: l ∧ (Β¬l ~ a :: l), by simpa [lt_iff_le_and_ne], ⟨(sublist_cons _ _).subperm, Ξ» p, ne_of_lt (lt_succ_self (length l)) p.length_eq⟩ theorem le_cons_self (s : multiset Ξ±) (a : Ξ±) : s ≀ a ::β‚˜ s := le_of_lt $ lt_cons_self _ _ theorem cons_le_cons_iff (a : Ξ±) {s t : multiset Ξ±} : a ::β‚˜ s ≀ a ::β‚˜ t ↔ s ≀ t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, subperm_cons a theorem cons_le_cons (a : Ξ±) {s t : multiset Ξ±} : s ≀ t β†’ a ::β‚˜ s ≀ a ::β‚˜ t := (cons_le_cons_iff a).2 theorem le_cons_of_not_mem {a : Ξ±} {s t : multiset Ξ±} (m : a βˆ‰ s) : s ≀ a ::β‚˜ t ↔ s ≀ t := begin refine ⟨_, Ξ» h, le_trans h $ le_cons_self _ _⟩, suffices : βˆ€ {t'} (_ : s ≀ t') (_ : a ∈ t'), a ::β‚˜ s ≀ t', { exact Ξ» h, (cons_le_cons_iff a).1 (this h (mem_cons_self _ _)) }, introv h, revert m, refine le_induction_on h _, introv s m₁ mβ‚‚, rcases mem_split mβ‚‚ with ⟨r₁, rβ‚‚, rfl⟩, exact perm_middle.subperm_left.2 ((subperm_cons _).2 $ ((sublist_or_mem_of_sublist s).resolve_right m₁).subperm) end /-! ### Singleton -/ instance : has_singleton Ξ± (multiset Ξ±) := ⟨λ a, a ::β‚˜ 0⟩ instance : is_lawful_singleton Ξ± (multiset Ξ±) := ⟨λ a, rfl⟩ theorem singleton_eq_cons (a : Ξ±) : singleton a = a ::β‚˜ 0 := rfl @[simp] theorem mem_singleton {a b : Ξ±} : b ∈ ({a} : multiset Ξ±) ↔ b = a := by simp only [singleton_eq_cons, mem_cons, iff_self, or_false, not_mem_zero] theorem mem_singleton_self (a : Ξ±) : a ∈ ({a} : multiset Ξ±) := by { rw singleton_eq_cons, exact mem_cons_self _ _ } theorem singleton_inj {a b : Ξ±} : ({a} : multiset Ξ±) = {b} ↔ a = b := by { simp_rw [singleton_eq_cons], exact cons_inj_left _ } @[simp] theorem singleton_ne_zero (a : Ξ±) : ({a} : multiset Ξ±) β‰  0 := ne_of_gt (lt_cons_self _ _) @[simp] theorem singleton_le {a : Ξ±} {s : multiset Ξ±} : {a} ≀ s ↔ a ∈ s := ⟨λ h, mem_of_le h (mem_singleton_self _), Ξ» h, let ⟨t, e⟩ := exists_cons_of_mem h in e.symm β–Έ cons_le_cons _ (zero_le _)⟩ /-! ### Additive monoid -/ /-- The sum of two multisets is the lift of the list append operation. This adds the multiplicities of each element, i.e. `count a (s + t) = count a s + count a t`. -/ protected def add (s₁ sβ‚‚ : multiset Ξ±) : multiset Ξ± := quotient.lift_onβ‚‚ s₁ sβ‚‚ (Ξ» l₁ lβ‚‚, ((l₁ ++ lβ‚‚ : list Ξ±) : multiset Ξ±)) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, quot.sound $ p₁.append pβ‚‚ instance : has_add (multiset Ξ±) := ⟨multiset.add⟩ @[simp] theorem coe_add (s t : list Ξ±) : (s + t : multiset Ξ±) = (s ++ t : list Ξ±) := rfl protected theorem add_comm (s t : multiset Ξ±) : s + t = t + s := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, quot.sound perm_append_comm protected theorem zero_add (s : multiset Ξ±) : 0 + s = s := quot.induction_on s $ Ξ» l, rfl theorem singleton_add (a : Ξ±) (s : multiset Ξ±) : {a} + s = a ::β‚˜ s := rfl protected theorem add_le_add_left (s) {t u : multiset Ξ±} : s + t ≀ s + u ↔ t ≀ u := quotient.induction_on₃ s t u $ Ξ» l₁ lβ‚‚ l₃, subperm_append_left _ protected theorem add_left_cancel (s) {t u : multiset Ξ±} (h : s + t = s + u) : t = u := le_antisymm ((multiset.add_le_add_left _).1 (le_of_eq h)) ((multiset.add_le_add_left _).1 (le_of_eq h.symm)) instance : ordered_cancel_add_comm_monoid (multiset Ξ±) := { zero := 0, add := (+), add_comm := multiset.add_comm, add_assoc := Ξ» s₁ sβ‚‚ s₃, quotient.induction_on₃ s₁ sβ‚‚ s₃ $ Ξ» l₁ lβ‚‚ l₃, congr_arg coe $ append_assoc l₁ lβ‚‚ l₃, zero_add := multiset.zero_add, add_zero := Ξ» s, by rw [multiset.add_comm, multiset.zero_add], add_left_cancel := multiset.add_left_cancel, add_le_add_left := Ξ» s₁ sβ‚‚ h s₃, (multiset.add_le_add_left _).2 h, le_of_add_le_add_left := Ξ» s₁ sβ‚‚ s₃, (multiset.add_le_add_left _).1, ..@multiset.partial_order Ξ± } theorem le_add_right (s t : multiset Ξ±) : s ≀ s + t := by simpa using add_le_add_left (zero_le t) s theorem le_add_left (s t : multiset Ξ±) : s ≀ t + s := by simpa using add_le_add_right (zero_le t) s theorem le_iff_exists_add {s t : multiset Ξ±} : s ≀ t ↔ βˆƒ u, t = s + u := ⟨λ h, le_induction_on h $ Ξ» l₁ lβ‚‚ s, let ⟨l, p⟩ := s.exists_perm_append in ⟨l, quot.sound p⟩, Ξ» ⟨u, e⟩, e.symm β–Έ le_add_right _ _⟩ instance : canonically_ordered_add_monoid (multiset Ξ±) := { le_iff_exists_add := @le_iff_exists_add _, bot := 0, bot_le := multiset.zero_le, ..multiset.ordered_cancel_add_comm_monoid } @[simp] theorem cons_add (a : Ξ±) (s t : multiset Ξ±) : a ::β‚˜ s + t = a ::β‚˜ (s + t) := by rw [← singleton_add, ← singleton_add, add_assoc] @[simp] theorem add_cons (a : Ξ±) (s t : multiset Ξ±) : s + a ::β‚˜ t = a ::β‚˜ (s + t) := by rw [add_comm, cons_add, add_comm] @[simp] theorem mem_add {a : Ξ±} {s t : multiset Ξ±} : a ∈ s + t ↔ a ∈ s ∨ a ∈ t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, mem_append lemma mem_of_mem_nsmul {a : Ξ±} {s : multiset Ξ±} {n : β„•} (h : a ∈ n β€’ s) : a ∈ s := begin induction n with n ih, { rw zero_nsmul at h, exact absurd h (not_mem_zero _) }, { rw [succ_nsmul, mem_add] at h, exact h.elim id ih }, end @[simp] lemma mem_nsmul {a : Ξ±} {s : multiset Ξ±} {n : β„•} (h0 : n β‰  0) : a ∈ n β€’ s ↔ a ∈ s := begin refine ⟨mem_of_mem_nsmul, Ξ» h, _⟩, obtain ⟨n, rfl⟩ := exists_eq_succ_of_ne_zero h0, rw [succ_nsmul, mem_add], exact or.inl h end lemma nsmul_cons {s : multiset Ξ±} (n : β„•) (a : Ξ±) : n β€’ (a ::β‚˜ s) = n β€’ {a} + n β€’ s := by rw [←singleton_add, nsmul_add] /-! ### Cardinality -/ /-- The cardinality of a multiset is the sum of the multiplicities of all its elements, or simply the length of the underlying list. -/ def card : multiset Ξ± β†’+ β„• := { to_fun := Ξ» s, quot.lift_on s length $ Ξ» l₁ lβ‚‚, perm.length_eq, map_zero' := rfl, map_add' := Ξ» s t, quotient.induction_onβ‚‚ s t length_append } @[simp] theorem coe_card (l : list Ξ±) : card (l : multiset Ξ±) = length l := rfl @[simp] theorem card_zero : @card Ξ± 0 = 0 := rfl theorem card_add (s t : multiset Ξ±) : card (s + t) = card s + card t := card.map_add s t lemma card_nsmul (s : multiset Ξ±) (n : β„•) : (n β€’ s).card = n * s.card := by rw [card.map_nsmul s n, nat.nsmul_eq_mul] @[simp] theorem card_cons (a : Ξ±) (s : multiset Ξ±) : card (a ::β‚˜ s) = card s + 1 := quot.induction_on s $ Ξ» l, rfl @[simp] theorem card_singleton (a : Ξ±) : card ({a} : multiset Ξ±) = 1 := by simp only [singleton_eq_cons, card_zero, eq_self_iff_true, zero_add, card_cons] theorem card_eq_one {s : multiset Ξ±} : card s = 1 ↔ βˆƒ a, s = {a} := ⟨quot.induction_on s $ Ξ» l h, (list.length_eq_one.1 h).imp $ Ξ» a, congr_arg coe, Ξ» ⟨a, e⟩, e.symm β–Έ rfl⟩ theorem card_le_of_le {s t : multiset Ξ±} (h : s ≀ t) : card s ≀ card t := le_induction_on h $ Ξ» l₁ lβ‚‚, length_le_of_sublist theorem eq_of_le_of_card_le {s t : multiset Ξ±} (h : s ≀ t) : card t ≀ card s β†’ s = t := le_induction_on h $ Ξ» l₁ lβ‚‚ s hβ‚‚, congr_arg coe $ eq_of_sublist_of_length_le s hβ‚‚ theorem card_lt_of_lt {s t : multiset Ξ±} (h : s < t) : card s < card t := lt_of_not_ge $ Ξ» hβ‚‚, ne_of_lt h $ eq_of_le_of_card_le (le_of_lt h) hβ‚‚ theorem lt_iff_cons_le {s t : multiset Ξ±} : s < t ↔ βˆƒ a, a ::β‚˜ s ≀ t := ⟨quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, subperm.exists_of_length_lt (le_of_lt h) (card_lt_of_lt h), Ξ» ⟨a, h⟩, lt_of_lt_of_le (lt_cons_self _ _) h⟩ @[simp] theorem card_eq_zero {s : multiset Ξ±} : card s = 0 ↔ s = 0 := ⟨λ h, (eq_of_le_of_card_le (zero_le _) (le_of_eq h)).symm, Ξ» e, by simp [e]⟩ theorem card_pos {s : multiset Ξ±} : 0 < card s ↔ s β‰  0 := pos_iff_ne_zero.trans $ not_congr card_eq_zero theorem card_pos_iff_exists_mem {s : multiset Ξ±} : 0 < card s ↔ βˆƒ a, a ∈ s := quot.induction_on s $ Ξ» l, length_pos_iff_exists_mem /-- A strong induction principle for multisets: If you construct a value for a particular multiset given values for all strictly smaller multisets, you can construct a value for any multiset. -/ @[elab_as_eliminator] def strong_induction_on {p : multiset Ξ± β†’ Sort*} : βˆ€ (s : multiset Ξ±), (βˆ€ s, (βˆ€t < s, p t) β†’ p s) β†’ p s | s := Ξ» ih, ih s $ Ξ» t h, have card t < card s, from card_lt_of_lt h, strong_induction_on t ih using_well_founded {rel_tac := Ξ» _ _, `[exact ⟨_, measure_wf card⟩]} theorem strong_induction_eq {p : multiset Ξ± β†’ Sort*} (s : multiset Ξ±) (H) : @strong_induction_on _ p s H = H s (Ξ» t h, @strong_induction_on _ p t H) := by rw [strong_induction_on] @[elab_as_eliminator] lemma case_strong_induction_on {p : multiset Ξ± β†’ Prop} (s : multiset Ξ±) (hβ‚€ : p 0) (h₁ : βˆ€ a s, (βˆ€t ≀ s, p t) β†’ p (a ::β‚˜ s)) : p s := multiset.strong_induction_on s $ assume s, multiset.induction_on s (Ξ» _, hβ‚€) $ Ξ» a s _ ih, h₁ _ _ $ Ξ» t h, ih _ $ lt_of_le_of_lt h $ lt_cons_self _ _ /-- Suppose that, given that `p t` can be defined on all supersets of `s` of cardinality less than `n`, one knows how to define `p s`. Then one can inductively define `p s` for all multisets `s` of cardinality less than `n`, starting from multisets of card `n` and iterating. This can be used either to define data, or to prove properties. -/ def strong_downward_induction {p : multiset Ξ± β†’ Sort*} {n : β„•} (H : βˆ€ t₁, (βˆ€ {tβ‚‚ : multiset Ξ±}, tβ‚‚.card ≀ n β†’ t₁ < tβ‚‚ β†’ p tβ‚‚) β†’ t₁.card ≀ n β†’ p t₁) : βˆ€ (s : multiset Ξ±), s.card ≀ n β†’ p s | s := H s (Ξ» t ht h, have n - card t < n - card s, from (tsub_lt_tsub_iff_left_of_le ht).2 (card_lt_of_lt h), strong_downward_induction t ht) using_well_founded {rel_tac := Ξ» _ _, `[exact ⟨_, measure_wf (Ξ» (t : multiset Ξ±), n - t.card)⟩]} lemma strong_downward_induction_eq {p : multiset Ξ± β†’ Sort*} {n : β„•} (H : βˆ€ t₁, (βˆ€ {tβ‚‚ : multiset Ξ±}, tβ‚‚.card ≀ n β†’ t₁ < tβ‚‚ β†’ p tβ‚‚) β†’ t₁.card ≀ n β†’ p t₁) (s : multiset Ξ±) : strong_downward_induction H s = H s (Ξ» t ht hst, strong_downward_induction H t ht) := by rw strong_downward_induction /-- Analogue of `strong_downward_induction` with order of arguments swapped. -/ @[elab_as_eliminator] def strong_downward_induction_on {p : multiset Ξ± β†’ Sort*} {n : β„•} : βˆ€ (s : multiset Ξ±), (βˆ€ t₁, (βˆ€ {tβ‚‚ : multiset Ξ±}, tβ‚‚.card ≀ n β†’ t₁ < tβ‚‚ β†’ p tβ‚‚) β†’ t₁.card ≀ n β†’ p t₁) β†’ s.card ≀ n β†’ p s := Ξ» s H, strong_downward_induction H s lemma strong_downward_induction_on_eq {p : multiset Ξ± β†’ Sort*} (s : multiset Ξ±) {n : β„•} (H : βˆ€ t₁, (βˆ€ {tβ‚‚ : multiset Ξ±}, tβ‚‚.card ≀ n β†’ t₁ < tβ‚‚ β†’ p tβ‚‚) β†’ t₁.card ≀ n β†’ p t₁) : s.strong_downward_induction_on H = H s (Ξ» t ht h, t.strong_downward_induction_on H ht) := by { dunfold strong_downward_induction_on, rw strong_downward_induction } /-- Another way of expressing `strong_induction_on`: the `(<)` relation is well-founded. -/ lemma well_founded_lt : well_founded ((<) : multiset Ξ± β†’ multiset Ξ± β†’ Prop) := subrelation.wf (Ξ» _ _, multiset.card_lt_of_lt) (measure_wf multiset.card) /-! ### `multiset.repeat` -/ /-- `repeat a n` is the multiset containing only `a` with multiplicity `n`. -/ def repeat (a : Ξ±) (n : β„•) : multiset Ξ± := repeat a n @[simp] lemma repeat_zero (a : Ξ±) : repeat a 0 = 0 := rfl @[simp] lemma repeat_succ (a : Ξ±) (n) : repeat a (n+1) = a ::β‚˜ repeat a n := by simp [repeat] @[simp] lemma repeat_one (a : Ξ±) : repeat a 1 = {a} := by simp only [repeat_succ, singleton_eq_cons, eq_self_iff_true, repeat_zero, cons_inj_right] @[simp] lemma card_repeat : βˆ€ (a : Ξ±) n, card (repeat a n) = n := length_repeat theorem eq_of_mem_repeat {a b : Ξ±} {n} : b ∈ repeat a n β†’ b = a := eq_of_mem_repeat theorem eq_repeat' {a : Ξ±} {s : multiset Ξ±} : s = repeat a s.card ↔ βˆ€ b ∈ s, b = a := quot.induction_on s $ Ξ» l, iff.trans ⟨λ h, (perm_repeat.1 $ (quotient.exact h)), congr_arg coe⟩ eq_repeat' theorem eq_repeat_of_mem {a : Ξ±} {s : multiset Ξ±} : (βˆ€ b ∈ s, b = a) β†’ s = repeat a s.card := eq_repeat'.2 theorem eq_repeat {a : Ξ±} {n} {s : multiset Ξ±} : s = repeat a n ↔ card s = n ∧ βˆ€ b ∈ s, b = a := ⟨λ h, h.symm β–Έ ⟨card_repeat _ _, Ξ» b, eq_of_mem_repeat⟩, Ξ» ⟨e, al⟩, e β–Έ eq_repeat_of_mem al⟩ theorem repeat_subset_singleton : βˆ€ (a : Ξ±) n, repeat a n βŠ† {a} := repeat_subset_singleton theorem repeat_le_coe {a : Ξ±} {n} {l : list Ξ±} : repeat a n ≀ l ↔ list.repeat a n <+ l := ⟨λ ⟨l', p, s⟩, (perm_repeat.1 p) β–Έ s, sublist.subperm⟩ theorem nsmul_singleton (a : Ξ±) (n) : n β€’ ({a} : multiset Ξ±) = repeat a n := begin refine eq_repeat.mpr ⟨_, Ξ» b hb, mem_singleton.mp (mem_of_mem_nsmul hb)⟩, rw [card_nsmul, card_singleton, mul_one] end lemma nsmul_repeat {a : Ξ±} (n m : β„•) : n β€’ (repeat a m) = repeat a (n * m) := begin rw eq_repeat, split, { rw [card_nsmul, card_repeat] }, { exact Ξ» b hb, eq_of_mem_repeat (mem_of_mem_nsmul hb) }, end /-! ### Erasing one copy of an element -/ section erase variables [decidable_eq Ξ±] {s t : multiset Ξ±} {a b : Ξ±} /-- `erase s a` is the multiset that subtracts 1 from the multiplicity of `a`. -/ def erase (s : multiset Ξ±) (a : Ξ±) : multiset Ξ± := quot.lift_on s (Ξ» l, (l.erase a : multiset Ξ±)) (Ξ» l₁ lβ‚‚ p, quot.sound (p.erase a)) @[simp] theorem coe_erase (l : list Ξ±) (a : Ξ±) : erase (l : multiset Ξ±) a = l.erase a := rfl @[simp] theorem erase_zero (a : Ξ±) : (0 : multiset Ξ±).erase a = 0 := rfl @[simp] theorem erase_cons_head (a : Ξ±) (s : multiset Ξ±) : (a ::β‚˜ s).erase a = s := quot.induction_on s $ Ξ» l, congr_arg coe $ erase_cons_head a l @[simp, priority 990] theorem erase_cons_tail {a b : Ξ±} (s : multiset Ξ±) (h : b β‰  a) : (b ::β‚˜ s).erase a = b ::β‚˜ s.erase a := quot.induction_on s $ Ξ» l, congr_arg coe $ erase_cons_tail l h @[simp, priority 980] theorem erase_of_not_mem {a : Ξ±} {s : multiset Ξ±} : a βˆ‰ s β†’ s.erase a = s := quot.induction_on s $ Ξ» l h, congr_arg coe $ erase_of_not_mem h @[simp, priority 980] theorem cons_erase {s : multiset Ξ±} {a : Ξ±} : a ∈ s β†’ a ::β‚˜ s.erase a = s := quot.induction_on s $ Ξ» l h, quot.sound (perm_cons_erase h).symm theorem le_cons_erase (s : multiset Ξ±) (a : Ξ±) : s ≀ a ::β‚˜ s.erase a := if h : a ∈ s then le_of_eq (cons_erase h).symm else by rw erase_of_not_mem h; apply le_cons_self theorem erase_add_left_pos {a : Ξ±} {s : multiset Ξ±} (t) : a ∈ s β†’ (s + t).erase a = s.erase a + t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ erase_append_left lβ‚‚ h theorem erase_add_right_pos {a : Ξ±} (s) {t : multiset Ξ±} (h : a ∈ t) : (s + t).erase a = s + t.erase a := by rw [add_comm, erase_add_left_pos s h, add_comm] theorem erase_add_right_neg {a : Ξ±} {s : multiset Ξ±} (t) : a βˆ‰ s β†’ (s + t).erase a = s + t.erase a := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ erase_append_right lβ‚‚ h theorem erase_add_left_neg {a : Ξ±} (s) {t : multiset Ξ±} (h : a βˆ‰ t) : (s + t).erase a = s.erase a + t := by rw [add_comm, erase_add_right_neg s h, add_comm] theorem erase_le (a : Ξ±) (s : multiset Ξ±) : s.erase a ≀ s := quot.induction_on s $ Ξ» l, (erase_sublist a l).subperm @[simp] theorem erase_lt {a : Ξ±} {s : multiset Ξ±} : s.erase a < s ↔ a ∈ s := ⟨λ h, not_imp_comm.1 erase_of_not_mem (ne_of_lt h), Ξ» h, by simpa [h] using lt_cons_self (s.erase a) a⟩ theorem erase_subset (a : Ξ±) (s : multiset Ξ±) : s.erase a βŠ† s := subset_of_le (erase_le a s) theorem mem_erase_of_ne {a b : Ξ±} {s : multiset Ξ±} (ab : a β‰  b) : a ∈ s.erase b ↔ a ∈ s := quot.induction_on s $ Ξ» l, list.mem_erase_of_ne ab theorem mem_of_mem_erase {a b : Ξ±} {s : multiset Ξ±} : a ∈ s.erase b β†’ a ∈ s := mem_of_subset (erase_subset _ _) theorem erase_comm (s : multiset Ξ±) (a b : Ξ±) : (s.erase a).erase b = (s.erase b).erase a := quot.induction_on s $ Ξ» l, congr_arg coe $ l.erase_comm a b theorem erase_le_erase {s t : multiset Ξ±} (a : Ξ±) (h : s ≀ t) : s.erase a ≀ t.erase a := le_induction_on h $ Ξ» l₁ lβ‚‚ h, (h.erase _).subperm theorem erase_le_iff_le_cons {s t : multiset Ξ±} {a : Ξ±} : s.erase a ≀ t ↔ s ≀ a ::β‚˜ t := ⟨λ h, le_trans (le_cons_erase _ _) (cons_le_cons _ h), Ξ» h, if m : a ∈ s then by rw ← cons_erase m at h; exact (cons_le_cons_iff _).1 h else le_trans (erase_le _ _) ((le_cons_of_not_mem m).1 h)⟩ @[simp] theorem card_erase_of_mem {a : Ξ±} {s : multiset Ξ±} : a ∈ s β†’ card (s.erase a) = pred (card s) := quot.induction_on s $ Ξ» l, length_erase_of_mem theorem card_erase_lt_of_mem {a : Ξ±} {s : multiset Ξ±} : a ∈ s β†’ card (s.erase a) < card s := Ξ» h, card_lt_of_lt (erase_lt.mpr h) theorem card_erase_le {a : Ξ±} {s : multiset Ξ±} : card (s.erase a) ≀ card s := card_le_of_le (erase_le a s) theorem card_erase_eq_ite {a : Ξ±} {s : multiset Ξ±} : card (s.erase a) = if a ∈ s then pred (card s) else card s := begin by_cases h : a ∈ s, { rwa [card_erase_of_mem h, if_pos] }, { rwa [erase_of_not_mem h, if_neg] } end end erase @[simp] theorem coe_reverse (l : list Ξ±) : (reverse l : multiset Ξ±) = l := quot.sound $ reverse_perm _ /-! ### `multiset.map` -/ /-- `map f s` is the lift of the list `map` operation. The multiplicity of `b` in `map f s` is the number of `a ∈ s` (counting multiplicity) such that `f a = b`. -/ def map (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) : multiset Ξ² := quot.lift_on s (Ξ» l : list Ξ±, (l.map f : multiset Ξ²)) (Ξ» l₁ lβ‚‚ p, quot.sound (p.map f)) theorem forall_mem_map_iff {f : Ξ± β†’ Ξ²} {p : Ξ² β†’ Prop} {s : multiset Ξ±} : (βˆ€ y ∈ s.map f, p y) ↔ (βˆ€ x ∈ s, p (f x)) := quotient.induction_on' s $ Ξ» L, list.forall_mem_map_iff @[simp] theorem coe_map (f : Ξ± β†’ Ξ²) (l : list Ξ±) : map f ↑l = l.map f := rfl @[simp] theorem map_zero (f : Ξ± β†’ Ξ²) : map f 0 = 0 := rfl @[simp] theorem map_cons (f : Ξ± β†’ Ξ²) (a s) : map f (a ::β‚˜ s) = f a ::β‚˜ map f s := quot.induction_on s $ Ξ» l, rfl @[simp] theorem map_singleton (f : Ξ± β†’ Ξ²) (a : Ξ±) : ({a} : multiset Ξ±).map f = {f a} := rfl theorem map_repeat (f : Ξ± β†’ Ξ²) (a : Ξ±) (k : β„•) : (repeat a k).map f = repeat (f a) k := by { induction k, simp, simpa } @[simp] theorem map_add (f : Ξ± β†’ Ξ²) (s t) : map f (s + t) = map f s + map f t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe $ map_append _ _ _ /-- If each element of `s : multiset Ξ±` can be lifted to `Ξ²`, then `s` can be lifted to `multiset Ξ²`. -/ instance [can_lift Ξ± Ξ²] : can_lift (multiset Ξ±) (multiset Ξ²) := { cond := Ξ» s, βˆ€ x ∈ s, can_lift.cond Ξ² x, coe := map can_lift.coe, prf := by { rintro ⟨l⟩ hl, lift l to list Ξ² using hl, exact ⟨l, coe_map _ _⟩ } } /-- `multiset.map` as an `add_monoid_hom`. -/ def map_add_monoid_hom (f : Ξ± β†’ Ξ²) : multiset Ξ± β†’+ multiset Ξ² := { to_fun := map f, map_zero' := map_zero _, map_add' := map_add _ } @[simp] lemma coe_map_add_monoid_hom (f : Ξ± β†’ Ξ²) : (map_add_monoid_hom f : multiset Ξ± β†’ multiset Ξ²) = map f := rfl theorem map_nsmul (f : Ξ± β†’ Ξ²) (n : β„•) (s) : map f (n β€’ s) = n β€’ (map f s) := (map_add_monoid_hom f).map_nsmul _ _ @[simp] theorem mem_map {f : Ξ± β†’ Ξ²} {b : Ξ²} {s : multiset Ξ±} : b ∈ map f s ↔ βˆƒ a, a ∈ s ∧ f a = b := quot.induction_on s $ Ξ» l, mem_map @[simp] theorem card_map (f : Ξ± β†’ Ξ²) (s) : card (map f s) = card s := quot.induction_on s $ Ξ» l, length_map _ _ @[simp] theorem map_eq_zero {s : multiset Ξ±} {f : Ξ± β†’ Ξ²} : s.map f = 0 ↔ s = 0 := by rw [← multiset.card_eq_zero, multiset.card_map, multiset.card_eq_zero] theorem mem_map_of_mem (f : Ξ± β†’ Ξ²) {a : Ξ±} {s : multiset Ξ±} (h : a ∈ s) : f a ∈ map f s := mem_map.2 ⟨_, h, rfl⟩ lemma map_eq_singleton {f : Ξ± β†’ Ξ²} {s : multiset Ξ±} {b : Ξ²} : map f s = {b} ↔ βˆƒ a : Ξ±, s = {a} ∧ f a = b := begin split, { intro h, obtain ⟨a, ha⟩ : βˆƒ a, s = {a}, { rw [←card_eq_one, ←card_map, h, card_singleton] }, refine ⟨a, ha, _⟩, rw [←mem_singleton, ←h, ha, map_singleton, mem_singleton] }, { rintro ⟨a, rfl, rfl⟩, simp } end theorem mem_map_of_injective {f : Ξ± β†’ Ξ²} (H : function.injective f) {a : Ξ±} {s : multiset Ξ±} : f a ∈ map f s ↔ a ∈ s := quot.induction_on s $ Ξ» l, mem_map_of_injective H @[simp] theorem map_map (g : Ξ² β†’ Ξ³) (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) : map g (map f s) = map (g ∘ f) s := quot.induction_on s $ Ξ» l, congr_arg coe $ list.map_map _ _ _ theorem map_id (s : multiset Ξ±) : map id s = s := quot.induction_on s $ Ξ» l, congr_arg coe $ map_id _ @[simp] lemma map_id' (s : multiset Ξ±) : map (Ξ»x, x) s = s := map_id s @[simp] theorem map_const (s : multiset Ξ±) (b : Ξ²) : map (function.const Ξ± b) s = repeat b s.card := quot.induction_on s $ Ξ» l, congr_arg coe $ map_const _ _ @[congr] theorem map_congr {f g : Ξ± β†’ Ξ²} {s : multiset Ξ±} : (βˆ€ x ∈ s, f x = g x) β†’ map f s = map g s := quot.induction_on s $ Ξ» l H, congr_arg coe $ map_congr H lemma map_hcongr {Ξ²' : Type*} {m : multiset Ξ±} {f : Ξ± β†’ Ξ²} {f' : Ξ± β†’ Ξ²'} (h : Ξ² = Ξ²') (hf : βˆ€a∈m, f a == f' a) : map f m == map f' m := begin subst h, simp at hf, simp [map_congr hf] end theorem eq_of_mem_map_const {b₁ bβ‚‚ : Ξ²} {l : list Ξ±} (h : b₁ ∈ map (function.const Ξ± bβ‚‚) l) : b₁ = bβ‚‚ := eq_of_mem_repeat $ by rwa map_const at h @[simp] theorem map_le_map {f : Ξ± β†’ Ξ²} {s t : multiset Ξ±} (h : s ≀ t) : map f s ≀ map f t := le_induction_on h $ Ξ» l₁ lβ‚‚ h, (h.map f).subperm @[simp] theorem map_subset_map {f : Ξ± β†’ Ξ²} {s t : multiset Ξ±} (H : s βŠ† t) : map f s βŠ† map f t := Ξ» b m, let ⟨a, h, e⟩ := mem_map.1 m in mem_map.2 ⟨a, H h, e⟩ lemma map_erase [decidable_eq Ξ±] [decidable_eq Ξ²] (f : Ξ± β†’ Ξ²) (hf : function.injective f) (x : Ξ±) (s : multiset Ξ±) : (s.erase x).map f = (s.map f).erase (f x) := begin induction s using multiset.induction_on with y s ih, { simp }, by_cases hxy : y = x, { cases hxy, simp }, { rw [s.erase_cons_tail hxy, map_cons, map_cons, (s.map f).erase_cons_tail (hf.ne hxy), ih] } end /-! ### `multiset.fold` -/ /-- `foldl f H b s` is the lift of the list operation `foldl f b l`, which folds `f` over the multiset. It is well defined when `f` is right-commutative, that is, `f (f b a₁) aβ‚‚ = f (f b aβ‚‚) a₁`. -/ def foldl (f : Ξ² β†’ Ξ± β†’ Ξ²) (H : right_commutative f) (b : Ξ²) (s : multiset Ξ±) : Ξ² := quot.lift_on s (Ξ» l, foldl f b l) (Ξ» l₁ lβ‚‚ p, p.foldl_eq H b) @[simp] theorem foldl_zero (f : Ξ² β†’ Ξ± β†’ Ξ²) (H b) : foldl f H b 0 = b := rfl @[simp] theorem foldl_cons (f : Ξ² β†’ Ξ± β†’ Ξ²) (H b a s) : foldl f H b (a ::β‚˜ s) = foldl f H (f b a) s := quot.induction_on s $ Ξ» l, rfl @[simp] theorem foldl_add (f : Ξ² β†’ Ξ± β†’ Ξ²) (H b s t) : foldl f H b (s + t) = foldl f H (foldl f H b s) t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, foldl_append _ _ _ _ /-- `foldr f H b s` is the lift of the list operation `foldr f b l`, which folds `f` over the multiset. It is well defined when `f` is left-commutative, that is, `f a₁ (f aβ‚‚ b) = f aβ‚‚ (f a₁ b)`. -/ def foldr (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (s : multiset Ξ±) : Ξ² := quot.lift_on s (Ξ» l, foldr f b l) (Ξ» l₁ lβ‚‚ p, p.foldr_eq H b) @[simp] theorem foldr_zero (f : Ξ± β†’ Ξ² β†’ Ξ²) (H b) : foldr f H b 0 = b := rfl @[simp] theorem foldr_cons (f : Ξ± β†’ Ξ² β†’ Ξ²) (H b a s) : foldr f H b (a ::β‚˜ s) = f a (foldr f H b s) := quot.induction_on s $ Ξ» l, rfl @[simp] theorem foldr_singleton (f : Ξ± β†’ Ξ² β†’ Ξ²) (H b a) : foldr f H b ({a} : multiset Ξ±) = f a b := rfl @[simp] theorem foldr_add (f : Ξ± β†’ Ξ² β†’ Ξ²) (H b s t) : foldr f H b (s + t) = foldr f H (foldr f H b t) s := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, foldr_append _ _ _ _ @[simp] theorem coe_foldr (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (l : list Ξ±) : foldr f H b l = l.foldr f b := rfl @[simp] theorem coe_foldl (f : Ξ² β†’ Ξ± β†’ Ξ²) (H : right_commutative f) (b : Ξ²) (l : list Ξ±) : foldl f H b l = l.foldl f b := rfl theorem coe_foldr_swap (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (l : list Ξ±) : foldr f H b l = l.foldl (Ξ» x y, f y x) b := (congr_arg (foldr f H b) (coe_reverse l)).symm.trans $ foldr_reverse _ _ _ theorem foldr_swap (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (s : multiset Ξ±) : foldr f H b s = foldl (Ξ» x y, f y x) (Ξ» x y z, (H _ _ _).symm) b s := quot.induction_on s $ Ξ» l, coe_foldr_swap _ _ _ _ theorem foldl_swap (f : Ξ² β†’ Ξ± β†’ Ξ²) (H : right_commutative f) (b : Ξ²) (s : multiset Ξ±) : foldl f H b s = foldr (Ξ» x y, f y x) (Ξ» x y z, (H _ _ _).symm) b s := (foldr_swap _ _ _ _).symm lemma foldr_induction' (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (x : Ξ²) (q : Ξ± β†’ Prop) (p : Ξ² β†’ Prop) (s : multiset Ξ±) (hpqf : βˆ€ a b, q a β†’ p b β†’ p (f a b)) (px : p x) (q_s : βˆ€ a ∈ s, q a) : p (foldr f H x s) := begin revert s, refine multiset.induction (by simp [px]) _, intros a s hs hsa, rw foldr_cons, have hps : βˆ€ (x : Ξ±), x ∈ s β†’ q x, from Ξ» x hxs, hsa x (mem_cons_of_mem hxs), exact hpqf a (foldr f H x s) (hsa a (mem_cons_self a s)) (hs hps), end lemma foldr_induction (f : Ξ± β†’ Ξ± β†’ Ξ±) (H : left_commutative f) (x : Ξ±) (p : Ξ± β†’ Prop) (s : multiset Ξ±) (p_f : βˆ€ a b, p a β†’ p b β†’ p (f a b)) (px : p x) (p_s : βˆ€ a ∈ s, p a) : p (foldr f H x s) := foldr_induction' f H x p p s p_f px p_s lemma foldl_induction' (f : Ξ² β†’ Ξ± β†’ Ξ²) (H : right_commutative f) (x : Ξ²) (q : Ξ± β†’ Prop) (p : Ξ² β†’ Prop) (s : multiset Ξ±) (hpqf : βˆ€ a b, q a β†’ p b β†’ p (f b a)) (px : p x) (q_s : βˆ€ a ∈ s, q a) : p (foldl f H x s) := begin rw foldl_swap, exact foldr_induction' (Ξ» x y, f y x) (Ξ» x y z, (H _ _ _).symm) x q p s hpqf px q_s, end lemma foldl_induction (f : Ξ± β†’ Ξ± β†’ Ξ±) (H : right_commutative f) (x : Ξ±) (p : Ξ± β†’ Prop) (s : multiset Ξ±) (p_f : βˆ€ a b, p a β†’ p b β†’ p (f b a)) (px : p x) (p_s : βˆ€ a ∈ s, p a) : p (foldl f H x s) := foldl_induction' f H x p p s p_f px p_s /-- Product of a multiset given a commutative monoid structure on `Ξ±`. `prod {a, b, c} = a * b * c` -/ @[to_additive "Sum of a multiset given a commutative additive monoid structure on `Ξ±`. `sum {a, b, c} = a + b + c`"] def prod [comm_monoid Ξ±] : multiset Ξ± β†’ Ξ± := foldr (*) (Ξ» x y z, by simp [mul_left_comm]) 1 @[to_additive] theorem prod_eq_foldr [comm_monoid Ξ±] (s : multiset Ξ±) : prod s = foldr (*) (Ξ» x y z, by simp [mul_left_comm]) 1 s := rfl @[to_additive] theorem prod_eq_foldl [comm_monoid Ξ±] (s : multiset Ξ±) : prod s = foldl (*) (Ξ» x y z, by simp [mul_right_comm]) 1 s := (foldr_swap _ _ _ _).trans (by simp [mul_comm]) @[simp, norm_cast, to_additive] theorem coe_prod [comm_monoid Ξ±] (l : list Ξ±) : prod ↑l = l.prod := prod_eq_foldl _ @[simp, to_additive] theorem prod_to_list [comm_monoid Ξ±] (s : multiset Ξ±) : s.to_list.prod = s.prod := begin conv_rhs { rw ←coe_to_list s, }, rw coe_prod, end @[simp, to_additive] theorem prod_zero [comm_monoid Ξ±] : @prod Ξ± _ 0 = 1 := rfl @[simp, to_additive] theorem prod_cons [comm_monoid Ξ±] (a : Ξ±) (s) : prod (a ::β‚˜ s) = a * prod s := foldr_cons _ _ _ _ _ @[simp, to_additive] theorem prod_singleton [comm_monoid Ξ±] (a : Ξ±) : prod {a} = a := by simp only [mul_one, prod_cons, singleton_eq_cons, eq_self_iff_true, prod_zero] @[simp, to_additive] theorem prod_add [comm_monoid Ξ±] (s t : multiset Ξ±) : prod (s + t) = prod s * prod t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, by simp /-- `multiset.sum`, the sum of the elements of a multiset, promoted to a morphism of `add_comm_monoid`s. -/ def sum_add_monoid_hom [add_comm_monoid Ξ±] : multiset Ξ± β†’+ Ξ± := { to_fun := sum, map_zero' := sum_zero, map_add' := sum_add } @[simp] lemma coe_sum_add_monoid_hom [add_comm_monoid Ξ±] : (sum_add_monoid_hom : multiset Ξ± β†’ Ξ±) = sum := rfl lemma prod_nsmul {Ξ± : Type*} [comm_monoid Ξ±] (m : multiset Ξ±) : βˆ€ (n : β„•), (n β€’ m).prod = m.prod ^ n | 0 := by { rw [zero_nsmul, pow_zero], refl } | (n + 1) := by rw [add_nsmul, one_nsmul, pow_add, pow_one, prod_add, prod_nsmul n] @[simp, to_additive] theorem prod_repeat [comm_monoid Ξ±] (a : Ξ±) (n : β„•) : prod (multiset.repeat a n) = a ^ n := by simp [repeat, list.prod_repeat] @[to_additive] lemma prod_map_one [comm_monoid Ξ³] {m : multiset Ξ±} : prod (m.map (Ξ»a, (1 : Ξ³))) = (1 : Ξ³) := by simp @[simp, to_additive] lemma prod_map_mul [comm_monoid Ξ³] {m : multiset Ξ±} {f g : Ξ± β†’ Ξ³} : prod (m.map $ Ξ»a, f a * g a) = prod (m.map f) * prod (m.map g) := multiset.induction_on m (by simp) (assume a m ih, by simp [ih]; cc) @[to_additive] lemma prod_map_prod_map [comm_monoid Ξ³] (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β†’ Ξ² β†’ Ξ³} : prod (m.map $ Ξ»a, prod $ n.map $ Ξ»b, f a b) = prod (n.map $ Ξ»b, prod $ m.map $ Ξ»a, f a b) := multiset.induction_on m (by simp) (assume a m ih, by simp [ih]) lemma sum_map_mul_left [semiring Ξ²] {b : Ξ²} {s : multiset Ξ±} {f : Ξ± β†’ Ξ²} : sum (s.map (Ξ»a, b * f a)) = b * sum (s.map f) := multiset.induction_on s (by simp) (assume a s ih, by simp [ih, mul_add]) lemma sum_map_mul_right [semiring Ξ²] {b : Ξ²} {s : multiset Ξ±} {f : Ξ± β†’ Ξ²} : sum (s.map (Ξ»a, f a * b)) = sum (s.map f) * b := multiset.induction_on s (by simp) (assume a s ih, by simp [ih, add_mul]) lemma prod_eq_zero {Mβ‚€ : Type*} [comm_monoid_with_zero Mβ‚€] {s : multiset Mβ‚€} (h : (0 : Mβ‚€) ∈ s) : multiset.prod s = 0 := begin rcases multiset.exists_cons_of_mem h with ⟨s', hs'⟩, simp [hs', multiset.prod_cons] end lemma prod_eq_zero_iff {Mβ‚€ : Type*} [comm_monoid_with_zero Mβ‚€] [no_zero_divisors Mβ‚€] [nontrivial Mβ‚€] {s : multiset Mβ‚€} : multiset.prod s = 0 ↔ (0 : Mβ‚€) ∈ s := by { rcases s with ⟨l⟩, simp } theorem prod_ne_zero {Mβ‚€ : Type*} [comm_monoid_with_zero Mβ‚€] [no_zero_divisors Mβ‚€] [nontrivial Mβ‚€] {m : multiset Mβ‚€} (h : (0 : Mβ‚€) βˆ‰ m) : m.prod β‰  0 := mt prod_eq_zero_iff.1 h @[to_additive] lemma prod_hom [comm_monoid Ξ±] [comm_monoid Ξ²] (s : multiset Ξ±) (f : Ξ± β†’* Ξ²) : (s.map f).prod = f s.prod := quotient.induction_on s $ Ξ» l, by simp only [l.prod_hom f, quot_mk_to_coe, coe_map, coe_prod] @[to_additive] theorem prod_hom_rel [comm_monoid Ξ²] [comm_monoid Ξ³] (s : multiset Ξ±) {r : Ξ² β†’ Ξ³ β†’ Prop} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} (h₁ : r 1 1) (hβ‚‚ : βˆ€β¦ƒa b c⦄, r b c β†’ r (f a * b) (g a * c)) : r (s.map f).prod (s.map g).prod := quotient.induction_on s $ Ξ» l, by simp only [l.prod_hom_rel h₁ hβ‚‚, quot_mk_to_coe, coe_map, coe_prod] @[simp] lemma coe_inv_monoid_hom {G : Type*} [comm_group G] : (comm_group.inv_monoid_hom : G β†’ G) = has_inv.inv := rfl @[simp, to_additive] lemma prod_map_inv {G : Type*} [comm_group G] (m : multiset G) : (m.map has_inv.inv).prod = m.prod⁻¹ := m.prod_hom comm_group.inv_monoid_hom lemma dvd_prod [comm_monoid Ξ±] {a : Ξ±} {s : multiset Ξ±} : a ∈ s β†’ a ∣ s.prod := quotient.induction_on s (Ξ» l a h, by simpa using list.dvd_prod h) a lemma prod_dvd_prod [comm_monoid Ξ±] {s t : multiset Ξ±} (h : s ≀ t) : s.prod ∣ t.prod := begin rcases multiset.le_iff_exists_add.1 h with ⟨z, rfl⟩, simp, end lemma prod_nonneg [ordered_comm_semiring Ξ±] {m : multiset Ξ±} (h : βˆ€ a ∈ m, (0 : Ξ±) ≀ a) : 0 ≀ m.prod := begin revert h, refine m.induction_on _ _, { rintro -, rw prod_zero, exact zero_le_one }, { intros a s hs ih, rw prod_cons, apply mul_nonneg, { exact ih _ (mem_cons_self _ _) }, { exact hs (Ξ» a ha, ih _ (mem_cons_of_mem ha)) } } end @[to_additive sum_nonneg] lemma one_le_prod_of_one_le [ordered_comm_monoid Ξ±] {m : multiset Ξ±} : (βˆ€ x ∈ m, (1 : Ξ±) ≀ x) β†’ 1 ≀ m.prod := quotient.induction_on m $ Ξ» l hl, by simpa using list.one_le_prod_of_one_le hl @[to_additive] lemma single_le_prod [ordered_comm_monoid Ξ±] {m : multiset Ξ±} : (βˆ€ x ∈ m, (1 : Ξ±) ≀ x) β†’ βˆ€ x ∈ m, x ≀ m.prod := quotient.induction_on m $ Ξ» l hl x hx, by simpa using list.single_le_prod hl x hx @[to_additive] lemma prod_le_of_forall_le [ordered_comm_monoid Ξ±] (l : multiset Ξ±) (n : Ξ±) (h : βˆ€ (x ∈ l), x ≀ n) : l.prod ≀ n ^ l.card := begin induction l using quotient.induction_on, simpa using list.prod_le_of_forall_le _ _ h end @[to_additive all_zero_of_le_zero_le_of_sum_eq_zero] lemma all_one_of_le_one_le_of_prod_eq_one [ordered_comm_monoid Ξ±] {m : multiset Ξ±} : (βˆ€ x ∈ m, (1 : Ξ±) ≀ x) β†’ m.prod = 1 β†’ (βˆ€ x ∈ m, x = (1 : Ξ±)) := begin apply quotient.induction_on m, simp only [quot_mk_to_coe, coe_prod, mem_coe], intros l hl₁ hlβ‚‚ x hx, apply all_one_of_le_one_le_of_prod_eq_one hl₁ hlβ‚‚ _ hx, end lemma sum_eq_zero_iff [canonically_ordered_add_monoid Ξ±] {m : multiset Ξ±} : m.sum = 0 ↔ βˆ€ x ∈ m, x = (0 : Ξ±) := quotient.induction_on m $ Ξ» l, by simpa using list.sum_eq_zero_iff l @[to_additive] lemma prod_induction {M : Type*} [comm_monoid M] (p : M β†’ Prop) (s : multiset M) (p_mul : βˆ€ a b, p a β†’ p b β†’ p (a * b)) (p_one : p 1) (p_s : βˆ€ a ∈ s, p a) : p s.prod := begin rw prod_eq_foldr, exact foldr_induction (*) (Ξ» x y z, by simp [mul_left_comm]) 1 p s p_mul p_one p_s, end @[to_additive le_sum_of_subadditive_on_pred] lemma le_prod_of_submultiplicative_on_pred [comm_monoid Ξ±] [ordered_comm_monoid Ξ²] (f : Ξ± β†’ Ξ²) (p : Ξ± β†’ Prop) (h_one : f 1 = 1) (hp_one : p 1) (h_mul : βˆ€ a b, p a β†’ p b β†’ f (a * b) ≀ f a * f b) (hp_mul : βˆ€ a b, p a β†’ p b β†’ p (a * b)) (s : multiset Ξ±) (hps : βˆ€ a, a ∈ s β†’ p a) : f s.prod ≀ (s.map f).prod := begin revert s, refine multiset.induction _ _, { simp [le_of_eq h_one], }, intros a s hs hpsa, have hps : βˆ€ x, x ∈ s β†’ p x, from Ξ» x hx, hpsa x (mem_cons_of_mem hx), have hp_prod : p s.prod, from prod_induction p s hp_mul hp_one hps, rw [prod_cons, map_cons, prod_cons], exact (h_mul a s.prod (hpsa a (mem_cons_self a s)) hp_prod).trans (mul_le_mul_left' (hs hps) _), end @[to_additive le_sum_of_subadditive] lemma le_prod_of_submultiplicative [comm_monoid Ξ±] [ordered_comm_monoid Ξ²] (f : Ξ± β†’ Ξ²) (h_one : f 1 = 1) (h_mul : βˆ€ a b, f (a * b) ≀ f a * f b) (s : multiset Ξ±) : f s.prod ≀ (s.map f).prod := le_prod_of_submultiplicative_on_pred f (Ξ» i, true) h_one trivial (Ξ» x y _ _ , h_mul x y) (by simp) s (by simp) @[to_additive] lemma prod_induction_nonempty {M : Type*} [comm_monoid M] (p : M β†’ Prop) (p_mul : βˆ€ a b, p a β†’ p b β†’ p (a * b)) {s : multiset M} (hs_nonempty : s β‰  βˆ…) (p_s : βˆ€ a ∈ s, p a) : p s.prod := begin revert s, refine multiset.induction _ _, { intro h, exfalso, simpa using h, }, intros a s hs hsa hpsa, rw prod_cons, by_cases hs_empty : s = βˆ…, { simp [hs_empty, hpsa a], }, have hps : βˆ€ (x : M), x ∈ s β†’ p x, from Ξ» x hxs, hpsa x (mem_cons_of_mem hxs), exact p_mul a s.prod (hpsa a (mem_cons_self a s)) (hs hs_empty hps), end @[to_additive le_sum_nonempty_of_subadditive_on_pred] lemma le_prod_nonempty_of_submultiplicative_on_pred [comm_monoid Ξ±] [ordered_comm_monoid Ξ²] (f : Ξ± β†’ Ξ²) (p : Ξ± β†’ Prop) (h_mul : βˆ€ a b, p a β†’ p b β†’ f (a * b) ≀ f a * f b) (hp_mul : βˆ€ a b, p a β†’ p b β†’ p (a * b)) (s : multiset Ξ±) (hs_nonempty : s β‰  βˆ…) (hs : βˆ€ a, a ∈ s β†’ p a) : f s.prod ≀ (s.map f).prod := begin revert s, refine multiset.induction _ _, { intro h, exfalso, exact h rfl, }, rintros a s hs hsa_nonempty hsa_prop, rw [prod_cons, map_cons, prod_cons], by_cases hs_empty : s = βˆ…, { simp [hs_empty], }, have hsa_restrict : (βˆ€ x, x ∈ s β†’ p x), from Ξ» x hx, hsa_prop x (mem_cons_of_mem hx), have hp_sup : p s.prod, from prod_induction_nonempty p hp_mul hs_empty hsa_restrict, have hp_a : p a, from hsa_prop a (mem_cons_self a s), exact (h_mul a _ hp_a hp_sup).trans (mul_le_mul_left' (hs hs_empty hsa_restrict) _), end @[to_additive le_sum_nonempty_of_subadditive] lemma le_prod_nonempty_of_submultiplicative [comm_monoid Ξ±] [ordered_comm_monoid Ξ²] (f : Ξ± β†’ Ξ²) (h_mul : βˆ€ a b, f (a * b) ≀ f a * f b) (s : multiset Ξ±) (hs_nonempty : s β‰  βˆ…) : f s.prod ≀ (s.map f).prod := le_prod_nonempty_of_submultiplicative_on_pred f (Ξ» i, true) (by simp [h_mul]) (by simp) s hs_nonempty (by simp) theorem dvd_sum [comm_semiring Ξ±] {a : Ξ±} {s : multiset Ξ±} : (βˆ€ x ∈ s, a ∣ x) β†’ a ∣ s.sum := multiset.induction_on s (Ξ» _, dvd_zero _) (Ξ» x s ih h, by rw sum_cons; exact dvd_add (h _ (mem_cons_self _ _)) (ih (Ξ» y hy, h _ (mem_cons.2 (or.inr hy))))) @[simp] theorem sum_map_singleton (s : multiset Ξ±) : (s.map (Ξ» a, ({a} : multiset Ξ±))).sum = s := multiset.induction_on s (by simp) (by simp [singleton_eq_cons]) theorem abs_sum_le_sum_abs [linear_ordered_add_comm_group Ξ±] {s : multiset Ξ±} : abs s.sum ≀ (s.map abs).sum := le_sum_of_subadditive _ abs_zero abs_add s /-! ### Join -/ /-- `join S`, where `S` is a multiset of multisets, is the lift of the list join operation, that is, the union of all the sets. join {{1, 2}, {1, 2}, {0, 1}} = {0, 1, 1, 1, 2, 2} -/ def join : multiset (multiset Ξ±) β†’ multiset Ξ± := sum theorem coe_join : βˆ€ L : list (list Ξ±), join (L.map (@coe _ (multiset Ξ±) _) : multiset (multiset Ξ±)) = L.join | [] := rfl | (l :: L) := congr_arg (Ξ» s : multiset Ξ±, ↑l + s) (coe_join L) @[simp] theorem join_zero : @join Ξ± 0 = 0 := rfl @[simp] theorem join_cons (s S) : @join Ξ± (s ::β‚˜ S) = s + join S := sum_cons _ _ @[simp] theorem join_add (S T) : @join Ξ± (S + T) = join S + join T := sum_add _ _ @[simp] theorem singleton_join (a) : join ({a} : multiset (multiset Ξ±)) = a := sum_singleton _ @[simp] theorem mem_join {a S} : a ∈ @join Ξ± S ↔ βˆƒ s ∈ S, a ∈ s := multiset.induction_on S (by simp) $ by simp [or_and_distrib_right, exists_or_distrib] {contextual := tt} @[simp] theorem card_join (S) : card (@join Ξ± S) = sum (map card S) := multiset.induction_on S (by simp) (by simp) /-! ### `multiset.bind` -/ /-- `bind s f` is the monad bind operation, defined as `join (map f s)`. It is the union of `f a` as `a` ranges over `s`. -/ def bind (s : multiset Ξ±) (f : Ξ± β†’ multiset Ξ²) : multiset Ξ² := join (map f s) @[simp] theorem coe_bind (l : list Ξ±) (f : Ξ± β†’ list Ξ²) : @bind Ξ± Ξ² l (Ξ» a, f a) = l.bind f := by rw [list.bind, ← coe_join, list.map_map]; refl @[simp] theorem zero_bind (f : Ξ± β†’ multiset Ξ²) : bind 0 f = 0 := rfl @[simp] theorem cons_bind (a s) (f : Ξ± β†’ multiset Ξ²) : bind (a ::β‚˜ s) f = f a + bind s f := by simp [bind] @[simp] theorem singleton_bind (a) (f : Ξ± β†’ multiset Ξ²) : bind {a} f = f a := by simp [bind] @[simp] theorem add_bind (s t) (f : Ξ± β†’ multiset Ξ²) : bind (s + t) f = bind s f + bind t f := by simp [bind] @[simp] theorem bind_zero (s : multiset Ξ±) : bind s (Ξ»a, 0 : Ξ± β†’ multiset Ξ²) = 0 := by simp [bind, join, nsmul_zero] @[simp] theorem bind_add (s : multiset Ξ±) (f g : Ξ± β†’ multiset Ξ²) : bind s (Ξ»a, f a + g a) = bind s f + bind s g := by simp [bind, join] @[simp] theorem bind_cons (s : multiset Ξ±) (f : Ξ± β†’ Ξ²) (g : Ξ± β†’ multiset Ξ²) : bind s (Ξ»a, f a ::β‚˜ g a) = map f s + bind s g := multiset.induction_on s (by simp) (by simp [add_comm, add_left_comm] {contextual := tt}) @[simp] theorem bind_singleton (s : multiset Ξ±) (f : Ξ± β†’ Ξ²) : bind s (Ξ» x, ({f x} : multiset Ξ²)) = map f s := multiset.induction_on s (by rw [zero_bind, map_zero]) (by simp [singleton_add]) @[simp] theorem mem_bind {b s} {f : Ξ± β†’ multiset Ξ²} : b ∈ bind s f ↔ βˆƒ a ∈ s, b ∈ f a := by simp [bind]; simp [-exists_and_distrib_right, exists_and_distrib_right.symm]; rw exists_swap; simp [and_assoc] @[simp] theorem card_bind (s) (f : Ξ± β†’ multiset Ξ²) : card (bind s f) = sum (map (card ∘ f) s) := by simp [bind] lemma bind_congr {f g : Ξ± β†’ multiset Ξ²} {m : multiset Ξ±} : (βˆ€a∈m, f a = g a) β†’ bind m f = bind m g := by simp [bind] {contextual := tt} lemma bind_hcongr {Ξ²' : Type*} {m : multiset Ξ±} {f : Ξ± β†’ multiset Ξ²} {f' : Ξ± β†’ multiset Ξ²'} (h : Ξ² = Ξ²') (hf : βˆ€a∈m, f a == f' a) : bind m f == bind m f' := begin subst h, simp at hf, simp [bind_congr hf] end lemma map_bind (m : multiset Ξ±) (n : Ξ± β†’ multiset Ξ²) (f : Ξ² β†’ Ξ³) : map f (bind m n) = bind m (Ξ»a, map f (n a)) := multiset.induction_on m (by simp) (by simp {contextual := tt}) lemma bind_map (m : multiset Ξ±) (n : Ξ² β†’ multiset Ξ³) (f : Ξ± β†’ Ξ²) : bind (map f m) n = bind m (Ξ»a, n (f a)) := multiset.induction_on m (by simp) (by simp {contextual := tt}) lemma bind_assoc {s : multiset Ξ±} {f : Ξ± β†’ multiset Ξ²} {g : Ξ² β†’ multiset Ξ³} : (s.bind f).bind g = s.bind (Ξ»a, (f a).bind g) := multiset.induction_on s (by simp) (by simp {contextual := tt}) lemma bind_bind (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β†’ Ξ² β†’ multiset Ξ³} : (bind m $ Ξ»a, bind n $ Ξ»b, f a b) = (bind n $ Ξ»b, bind m $ Ξ»a, f a b) := multiset.induction_on m (by simp) (by simp {contextual := tt}) lemma bind_map_comm (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β†’ Ξ² β†’ Ξ³} : (bind m $ Ξ»a, n.map $ Ξ»b, f a b) = (bind n $ Ξ»b, m.map $ Ξ»a, f a b) := multiset.induction_on m (by simp) (by simp {contextual := tt}) @[simp, to_additive] lemma prod_bind [comm_monoid Ξ²] (s : multiset Ξ±) (t : Ξ± β†’ multiset Ξ²) : prod (bind s t) = prod (s.map $ Ξ»a, prod (t a)) := multiset.induction_on s (by simp) (assume a s ih, by simp [ih, cons_bind]) /-! ### Product of two `multiset`s -/ /-- The multiplicity of `(a, b)` in `product s t` is the product of the multiplicity of `a` in `s` and `b` in `t`. -/ def product (s : multiset Ξ±) (t : multiset Ξ²) : multiset (Ξ± Γ— Ξ²) := s.bind $ Ξ» a, t.map $ prod.mk a @[simp] theorem coe_product (l₁ : list Ξ±) (lβ‚‚ : list Ξ²) : @product Ξ± Ξ² l₁ lβ‚‚ = l₁.product lβ‚‚ := by rw [product, list.product, ← coe_bind]; simp @[simp] theorem zero_product (t) : @product Ξ± Ξ² 0 t = 0 := rfl @[simp] theorem cons_product (a : Ξ±) (s : multiset Ξ±) (t : multiset Ξ²) : product (a ::β‚˜ s) t = map (prod.mk a) t + product s t := by simp [product] @[simp] theorem product_singleton (a : Ξ±) (b : Ξ²) : product ({a} : multiset Ξ±) ({b} : multiset Ξ²) = {(a,b)} := by simp only [product, bind_singleton, map_singleton] @[simp] theorem add_product (s t : multiset Ξ±) (u : multiset Ξ²) : product (s + t) u = product s u + product t u := by simp [product] @[simp] theorem product_add (s : multiset Ξ±) : βˆ€ t u : multiset Ξ², product s (t + u) = product s t + product s u := multiset.induction_on s (Ξ» t u, rfl) $ Ξ» a s IH t u, by rw [cons_product, IH]; simp; cc @[simp] theorem mem_product {s t} : βˆ€ {p : Ξ± Γ— Ξ²}, p ∈ @product Ξ± Ξ² s t ↔ p.1 ∈ s ∧ p.2 ∈ t | (a, b) := by simp [product, and.left_comm] @[simp] theorem card_product (s : multiset Ξ±) (t : multiset Ξ²) : card (product s t) = card s * card t := by simp [product, repeat, (∘), mul_comm] /-! ### Sigma multiset -/ section variable {Οƒ : Ξ± β†’ Type*} /-- `sigma s t` is the dependent version of `product`. It is the sum of `(a, b)` as `a` ranges over `s` and `b` ranges over `t a`. -/ protected def sigma (s : multiset Ξ±) (t : Ξ  a, multiset (Οƒ a)) : multiset (Ξ£ a, Οƒ a) := s.bind $ Ξ» a, (t a).map $ sigma.mk a @[simp] theorem coe_sigma (l₁ : list Ξ±) (lβ‚‚ : Ξ  a, list (Οƒ a)) : @multiset.sigma Ξ± Οƒ l₁ (Ξ» a, lβ‚‚ a) = l₁.sigma lβ‚‚ := by rw [multiset.sigma, list.sigma, ← coe_bind]; simp @[simp] theorem zero_sigma (t) : @multiset.sigma Ξ± Οƒ 0 t = 0 := rfl @[simp] theorem cons_sigma (a : Ξ±) (s : multiset Ξ±) (t : Ξ  a, multiset (Οƒ a)) : (a ::β‚˜ s).sigma t = map (sigma.mk a) (t a) + s.sigma t := by simp [multiset.sigma] @[simp] theorem sigma_singleton (a : Ξ±) (b : Ξ± β†’ Ξ²) : ({a} : multiset Ξ±).sigma (Ξ» a, ({b a} : multiset Ξ²)) = {⟨a, b a⟩} := rfl @[simp] theorem add_sigma (s t : multiset Ξ±) (u : Ξ  a, multiset (Οƒ a)) : (s + t).sigma u = s.sigma u + t.sigma u := by simp [multiset.sigma] @[simp] theorem sigma_add (s : multiset Ξ±) : βˆ€ t u : Ξ  a, multiset (Οƒ a), s.sigma (Ξ» a, t a + u a) = s.sigma t + s.sigma u := multiset.induction_on s (Ξ» t u, rfl) $ Ξ» a s IH t u, by rw [cons_sigma, IH]; simp; cc @[simp] theorem mem_sigma {s t} : βˆ€ {p : Ξ£ a, Οƒ a}, p ∈ @multiset.sigma Ξ± Οƒ s t ↔ p.1 ∈ s ∧ p.2 ∈ t p.1 | ⟨a, b⟩ := by simp [multiset.sigma, and_assoc, and.left_comm] @[simp] theorem card_sigma (s : multiset Ξ±) (t : Ξ  a, multiset (Οƒ a)) : card (s.sigma t) = sum (map (Ξ» a, card (t a)) s) := by simp [multiset.sigma, (∘)] end /-! ### Map for partial functions -/ /-- Lift of the list `pmap` operation. Map a partial function `f` over a multiset `s` whose elements are all in the domain of `f`. -/ def pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (s : multiset Ξ±) : (βˆ€ a ∈ s, p a) β†’ multiset Ξ² := quot.rec_on s (Ξ» l H, ↑(pmap f l H)) $ Ξ» l₁ lβ‚‚ (pp : l₁ ~ lβ‚‚), funext $ Ξ» (Hβ‚‚ : βˆ€ a ∈ lβ‚‚, p a), have H₁ : βˆ€ a ∈ l₁, p a, from Ξ» a h, Hβ‚‚ a (pp.subset h), have βˆ€ {sβ‚‚ e H}, @eq.rec (multiset Ξ±) l₁ (Ξ» s, (βˆ€ a ∈ s, p a) β†’ multiset Ξ²) (Ξ» _, ↑(pmap f l₁ H₁)) sβ‚‚ e H = ↑(pmap f l₁ H₁), by intros sβ‚‚ e _; subst e, this.trans $ quot.sound $ pp.pmap f @[simp] theorem coe_pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (l : list Ξ±) (H : βˆ€ a ∈ l, p a) : pmap f l H = l.pmap f H := rfl @[simp] lemma pmap_zero {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (h : βˆ€a∈(0:multiset Ξ±), p a) : pmap f 0 h = 0 := rfl @[simp] lemma pmap_cons {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (a : Ξ±) (m : multiset Ξ±) : βˆ€(h : βˆ€b∈a ::β‚˜ m, p b), pmap f (a ::β‚˜ m) h = f a (h a (mem_cons_self a m)) ::β‚˜ pmap f m (Ξ»a ha, h a $ mem_cons_of_mem ha) := quotient.induction_on m $ assume l h, rfl /-- "Attach" a proof that `a ∈ s` to each element `a` in `s` to produce a multiset on `{x // x ∈ s}`. -/ def attach (s : multiset Ξ±) : multiset {x // x ∈ s} := pmap subtype.mk s (Ξ» a, id) @[simp] theorem coe_attach (l : list Ξ±) : @eq (multiset {x // x ∈ l}) (@attach Ξ± l) l.attach := rfl theorem sizeof_lt_sizeof_of_mem [has_sizeof Ξ±] {x : Ξ±} {s : multiset Ξ±} (hx : x ∈ s) : sizeof x < sizeof s := by { induction s with l a b, exact list.sizeof_lt_sizeof_of_mem hx, refl } theorem pmap_eq_map (p : Ξ± β†’ Prop) (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) : βˆ€ H, @pmap _ _ p (Ξ» a _, f a) s H = map f s := quot.induction_on s $ Ξ» l H, congr_arg coe $ pmap_eq_map p f l H theorem pmap_congr {p q : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {g : Ξ  a, q a β†’ Ξ²} (s : multiset Ξ±) {H₁ Hβ‚‚} (h : βˆ€ a h₁ hβ‚‚, f a h₁ = g a hβ‚‚) : pmap f s H₁ = pmap g s Hβ‚‚ := quot.induction_on s (Ξ» l H₁ Hβ‚‚, congr_arg coe $ pmap_congr l h) H₁ Hβ‚‚ theorem map_pmap {p : Ξ± β†’ Prop} (g : Ξ² β†’ Ξ³) (f : Ξ  a, p a β†’ Ξ²) (s) : βˆ€ H, map g (pmap f s H) = pmap (Ξ» a h, g (f a h)) s H := quot.induction_on s $ Ξ» l H, congr_arg coe $ map_pmap g f l H theorem pmap_eq_map_attach {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (s) : βˆ€ H, pmap f s H = s.attach.map (Ξ» x, f x.1 (H _ x.2)) := quot.induction_on s $ Ξ» l H, congr_arg coe $ pmap_eq_map_attach f l H theorem attach_map_val (s : multiset Ξ±) : s.attach.map subtype.val = s := quot.induction_on s $ Ξ» l, congr_arg coe $ attach_map_val l @[simp] theorem mem_attach (s : multiset Ξ±) : βˆ€ x, x ∈ s.attach := quot.induction_on s $ Ξ» l, mem_attach _ @[simp] theorem mem_pmap {p : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {s H b} : b ∈ pmap f s H ↔ βˆƒ a (h : a ∈ s), f a (H a h) = b := quot.induction_on s (Ξ» l H, mem_pmap) H @[simp] theorem card_pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (s H) : card (pmap f s H) = card s := quot.induction_on s (Ξ» l H, length_pmap) H @[simp] theorem card_attach {m : multiset Ξ±} : card (attach m) = card m := card_pmap _ _ _ @[simp] lemma attach_zero : (0 : multiset Ξ±).attach = 0 := rfl lemma attach_cons (a : Ξ±) (m : multiset Ξ±) : (a ::β‚˜ m).attach = ⟨a, mem_cons_self a m⟩ ::β‚˜ (m.attach.map $ Ξ»p, ⟨p.1, mem_cons_of_mem p.2⟩) := quotient.induction_on m $ assume l, congr_arg coe $ congr_arg (list.cons _) $ by rw [list.map_pmap]; exact list.pmap_congr _ (assume a' h₁ hβ‚‚, subtype.eq rfl) section decidable_pi_exists variables {m : multiset Ξ±} /-- If `p` is a decidable predicate, so is the predicate that all elements of a multiset satisfy `p`. -/ protected def decidable_forall_multiset {p : Ξ± β†’ Prop} [hp : βˆ€a, decidable (p a)] : decidable (βˆ€a∈m, p a) := quotient.rec_on_subsingleton m (Ξ»l, decidable_of_iff (βˆ€a∈l, p a) $ by simp) instance decidable_dforall_multiset {p : Ξ a∈m, Prop} [hp : βˆ€a (h : a ∈ m), decidable (p a h)] : decidable (βˆ€a (h : a ∈ m), p a h) := decidable_of_decidable_of_iff (@multiset.decidable_forall_multiset {a // a ∈ m} m.attach (Ξ»a, p a.1 a.2) _) (iff.intro (assume h a ha, h ⟨a, ha⟩ (mem_attach _ _)) (assume h ⟨a, ha⟩ _, h _ _)) /-- decidable equality for functions whose domain is bounded by multisets -/ instance decidable_eq_pi_multiset {Ξ² : Ξ± β†’ Type*} [h : βˆ€a, decidable_eq (Ξ² a)] : decidable_eq (Ξ a∈m, Ξ² a) := assume f g, decidable_of_iff (βˆ€a (h : a ∈ m), f a h = g a h) (by simp [function.funext_iff]) /-- If `p` is a decidable predicate, so is the existence of an element in a multiset satisfying `p`. -/ def decidable_exists_multiset {p : Ξ± β†’ Prop} [decidable_pred p] : decidable (βˆƒ x ∈ m, p x) := quotient.rec_on_subsingleton m list.decidable_exists_mem instance decidable_dexists_multiset {p : Ξ a∈m, Prop} [hp : βˆ€a (h : a ∈ m), decidable (p a h)] : decidable (βˆƒa (h : a ∈ m), p a h) := decidable_of_decidable_of_iff (@multiset.decidable_exists_multiset {a // a ∈ m} m.attach (Ξ»a, p a.1 a.2) _) (iff.intro (Ξ» ⟨⟨a, haβ‚βŸ©, _, haβ‚‚βŸ©, ⟨a, ha₁, haβ‚‚βŸ©) (Ξ» ⟨a, ha₁, haβ‚‚βŸ©, ⟨⟨a, haβ‚βŸ©, mem_attach _ _, haβ‚‚βŸ©)) end decidable_pi_exists /-! ### Subtraction -/ section variables [decidable_eq Ξ±] {s t u : multiset Ξ±} {a b : Ξ±} /-- `s - t` is the multiset such that `count a (s - t) = count a s - count a t` for all `a` (note that it is truncated subtraction, so it is `0` if `count a t β‰₯ count a s`). -/ protected def sub (s t : multiset Ξ±) : multiset Ξ± := quotient.lift_onβ‚‚ s t (Ξ» l₁ lβ‚‚, (l₁.diff lβ‚‚ : multiset Ξ±)) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, quot.sound $ p₁.diff pβ‚‚ instance : has_sub (multiset Ξ±) := ⟨multiset.sub⟩ @[simp] theorem coe_sub (s t : list Ξ±) : (s - t : multiset Ξ±) = (s.diff t : list Ξ±) := rfl /-- This is a special case of `tsub_zero`, which should be used instead of this. This is needed to prove `has_ordered_sub (multiset Ξ±)`. -/ protected theorem sub_zero (s : multiset Ξ±) : s - 0 = s := quot.induction_on s $ Ξ» l, rfl @[simp] theorem sub_cons (a : Ξ±) (s t : multiset Ξ±) : s - a ::β‚˜ t = s.erase a - t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe $ diff_cons _ _ _ /-- This is a special case of `tsub_le_iff_right`, which should be used instead of this. This is needed to prove `has_ordered_sub (multiset Ξ±)`. -/ protected theorem sub_le_iff_le_add : s - t ≀ u ↔ s ≀ u + t := by revert s; exact multiset.induction_on t (by simp [multiset.sub_zero]) (Ξ» a t IH s, by simp [IH, erase_le_iff_le_cons]) instance : has_ordered_sub (multiset Ξ±) := ⟨λ n m k, multiset.sub_le_iff_le_add⟩ theorem sub_eq_fold_erase (s t : multiset Ξ±) : s - t = foldl erase erase_comm s t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, show ↑(l₁.diff lβ‚‚) = foldl erase erase_comm ↑l₁ ↑lβ‚‚, by { rw diff_eq_foldl l₁ lβ‚‚, symmetry, exact foldl_hom _ _ _ _ _ (Ξ» x y, rfl) } @[simp] theorem card_sub {s t : multiset Ξ±} (h : t ≀ s) : card (s - t) = card s - card t := (tsub_eq_of_eq_add_rev $ by rw [add_comm, ← card_add, tsub_add_cancel_of_le h]).symm /-! ### Union -/ /-- `s βˆͺ t` is the lattice join operation with respect to the multiset `≀`. The multiplicity of `a` in `s βˆͺ t` is the maximum of the multiplicities in `s` and `t`. -/ def union (s t : multiset Ξ±) : multiset Ξ± := s - t + t instance : has_union (multiset Ξ±) := ⟨union⟩ theorem union_def (s t : multiset Ξ±) : s βˆͺ t = s - t + t := rfl theorem le_union_left (s t : multiset Ξ±) : s ≀ s βˆͺ t := le_tsub_add theorem le_union_right (s t : multiset Ξ±) : t ≀ s βˆͺ t := le_add_left _ _ theorem eq_union_left : t ≀ s β†’ s βˆͺ t = s := tsub_add_cancel_of_le theorem union_le_union_right (h : s ≀ t) (u) : s βˆͺ u ≀ t βˆͺ u := add_le_add_right (tsub_le_tsub_right h _) u theorem union_le (h₁ : s ≀ u) (hβ‚‚ : t ≀ u) : s βˆͺ t ≀ u := by rw ← eq_union_left hβ‚‚; exact union_le_union_right h₁ t @[simp] theorem mem_union : a ∈ s βˆͺ t ↔ a ∈ s ∨ a ∈ t := ⟨λ h, (mem_add.1 h).imp_left (mem_of_le tsub_le_self), or.rec (mem_of_le $ le_union_left _ _) (mem_of_le $ le_union_right _ _)⟩ @[simp] theorem map_union [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (finj : function.injective f) {s t : multiset Ξ±} : map f (s βˆͺ t) = map f s βˆͺ map f t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe (by rw [list.map_append f, list.map_diff finj]) /-! ### Intersection -/ /-- `s ∩ t` is the lattice meet operation with respect to the multiset `≀`. The multiplicity of `a` in `s ∩ t` is the minimum of the multiplicities in `s` and `t`. -/ def inter (s t : multiset Ξ±) : multiset Ξ± := quotient.lift_onβ‚‚ s t (Ξ» l₁ lβ‚‚, (l₁.bag_inter lβ‚‚ : multiset Ξ±)) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, quot.sound $ p₁.bag_inter pβ‚‚ instance : has_inter (multiset Ξ±) := ⟨inter⟩ @[simp] theorem inter_zero (s : multiset Ξ±) : s ∩ 0 = 0 := quot.induction_on s $ Ξ» l, congr_arg coe l.bag_inter_nil @[simp] theorem zero_inter (s : multiset Ξ±) : 0 ∩ s = 0 := quot.induction_on s $ Ξ» l, congr_arg coe l.nil_bag_inter @[simp] theorem cons_inter_of_pos {a} (s : multiset Ξ±) {t} : a ∈ t β†’ (a ::β‚˜ s) ∩ t = a ::β‚˜ s ∩ t.erase a := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ cons_bag_inter_of_pos _ h @[simp] theorem cons_inter_of_neg {a} (s : multiset Ξ±) {t} : a βˆ‰ t β†’ (a ::β‚˜ s) ∩ t = s ∩ t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ cons_bag_inter_of_neg _ h theorem inter_le_left (s t : multiset Ξ±) : s ∩ t ≀ s := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, (bag_inter_sublist_left _ _).subperm theorem inter_le_right (s : multiset Ξ±) : βˆ€ t, s ∩ t ≀ t := multiset.induction_on s (Ξ» t, (zero_inter t).symm β–Έ zero_le _) $ Ξ» a s IH t, if h : a ∈ t then by simpa [h] using cons_le_cons a (IH (t.erase a)) else by simp [h, IH] theorem le_inter (h₁ : s ≀ t) (hβ‚‚ : s ≀ u) : s ≀ t ∩ u := begin revert s u, refine multiset.induction_on t _ (Ξ» a t IH, _); intros, { simp [h₁] }, by_cases a ∈ u, { rw [cons_inter_of_pos _ h, ← erase_le_iff_le_cons], exact IH (erase_le_iff_le_cons.2 h₁) (erase_le_erase _ hβ‚‚) }, { rw cons_inter_of_neg _ h, exact IH ((le_cons_of_not_mem $ mt (mem_of_le hβ‚‚) h).1 h₁) hβ‚‚ } end @[simp] theorem mem_inter : a ∈ s ∩ t ↔ a ∈ s ∧ a ∈ t := ⟨λ h, ⟨mem_of_le (inter_le_left _ _) h, mem_of_le (inter_le_right _ _) h⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, by rw [← cons_erase h₁, cons_inter_of_pos _ hβ‚‚]; apply mem_cons_self⟩ instance : lattice (multiset Ξ±) := { sup := (βˆͺ), sup_le := @union_le _ _, le_sup_left := le_union_left, le_sup_right := le_union_right, inf := (∩), le_inf := @le_inter _ _, inf_le_left := inter_le_left, inf_le_right := inter_le_right, ..@multiset.partial_order Ξ± } @[simp] theorem sup_eq_union (s t : multiset Ξ±) : s βŠ” t = s βˆͺ t := rfl @[simp] theorem inf_eq_inter (s t : multiset Ξ±) : s βŠ“ t = s ∩ t := rfl @[simp] theorem le_inter_iff : s ≀ t ∩ u ↔ s ≀ t ∧ s ≀ u := le_inf_iff @[simp] theorem union_le_iff : s βˆͺ t ≀ u ↔ s ≀ u ∧ t ≀ u := sup_le_iff instance : semilattice_inf_bot (multiset Ξ±) := { bot := 0, bot_le := zero_le, ..multiset.lattice } theorem union_comm (s t : multiset Ξ±) : s βˆͺ t = t βˆͺ s := sup_comm theorem inter_comm (s t : multiset Ξ±) : s ∩ t = t ∩ s := inf_comm theorem eq_union_right (h : s ≀ t) : s βˆͺ t = t := by rw [union_comm, eq_union_left h] theorem union_le_union_left (h : s ≀ t) (u) : u βˆͺ s ≀ u βˆͺ t := sup_le_sup_left h _ theorem union_le_add (s t : multiset Ξ±) : s βˆͺ t ≀ s + t := union_le (le_add_right _ _) (le_add_left _ _) theorem union_add_distrib (s t u : multiset Ξ±) : (s βˆͺ t) + u = (s + u) βˆͺ (t + u) := by simpa [(βˆͺ), union, eq_comm, add_assoc] using show s + u - (t + u) = s - t, by rw [add_comm t, tsub_add_eq_tsub_tsub, add_tsub_cancel_right] theorem add_union_distrib (s t u : multiset Ξ±) : s + (t βˆͺ u) = (s + t) βˆͺ (s + u) := by rw [add_comm, union_add_distrib, add_comm s, add_comm s] theorem cons_union_distrib (a : Ξ±) (s t : multiset Ξ±) : a ::β‚˜ (s βˆͺ t) = (a ::β‚˜ s) βˆͺ (a ::β‚˜ t) := by simpa using add_union_distrib (a ::β‚˜ 0) s t theorem inter_add_distrib (s t u : multiset Ξ±) : (s ∩ t) + u = (s + u) ∩ (t + u) := begin by_contra h, cases lt_iff_cons_le.1 (lt_of_le_of_ne (le_inter (add_le_add_right (inter_le_left s t) u) (add_le_add_right (inter_le_right s t) u)) h) with a hl, rw ← cons_add at hl, exact not_le_of_lt (lt_cons_self (s ∩ t) a) (le_inter (le_of_add_le_add_right (le_trans hl (inter_le_left _ _))) (le_of_add_le_add_right (le_trans hl (inter_le_right _ _)))) end theorem add_inter_distrib (s t u : multiset Ξ±) : s + (t ∩ u) = (s + t) ∩ (s + u) := by rw [add_comm, inter_add_distrib, add_comm s, add_comm s] theorem cons_inter_distrib (a : Ξ±) (s t : multiset Ξ±) : a ::β‚˜ (s ∩ t) = (a ::β‚˜ s) ∩ (a ::β‚˜ t) := by simp theorem union_add_inter (s t : multiset Ξ±) : s βˆͺ t + s ∩ t = s + t := begin apply le_antisymm, { rw union_add_distrib, refine union_le (add_le_add_left (inter_le_right _ _) _) _, rw add_comm, exact add_le_add_right (inter_le_left _ _) _ }, { rw [add_comm, add_inter_distrib], refine le_inter (add_le_add_right (le_union_right _ _) _) _, rw add_comm, exact add_le_add_right (le_union_left _ _) _ } end theorem sub_add_inter (s t : multiset Ξ±) : s - t + s ∩ t = s := begin rw [inter_comm], revert s, refine multiset.induction_on t (by simp) (Ξ» a t IH s, _), by_cases a ∈ s, { rw [cons_inter_of_pos _ h, sub_cons, add_cons, IH, cons_erase h] }, { rw [cons_inter_of_neg _ h, sub_cons, erase_of_not_mem h, IH] } end theorem sub_inter (s t : multiset Ξ±) : s - (s ∩ t) = s - t := add_right_cancel $ by rw [sub_add_inter s t, tsub_add_cancel_of_le (inter_le_left s t)] end /-! ### `multiset.filter` -/ section variables (p : Ξ± β†’ Prop) [decidable_pred p] /-- `filter p s` returns the elements in `s` (with the same multiplicities) which satisfy `p`, and removes the rest. -/ def filter (s : multiset Ξ±) : multiset Ξ± := quot.lift_on s (Ξ» l, (filter p l : multiset Ξ±)) (Ξ» l₁ lβ‚‚ h, quot.sound $ h.filter p) @[simp] theorem coe_filter (l : list Ξ±) : filter p (↑l) = l.filter p := rfl @[simp] theorem filter_zero : filter p 0 = 0 := rfl lemma filter_congr {p q : Ξ± β†’ Prop} [decidable_pred p] [decidable_pred q] {s : multiset Ξ±} : (βˆ€ x ∈ s, p x ↔ q x) β†’ filter p s = filter q s := quot.induction_on s $ Ξ» l h, congr_arg coe $ filter_congr h @[simp] theorem filter_add (s t : multiset Ξ±) : filter p (s + t) = filter p s + filter p t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe $ filter_append _ _ @[simp] theorem filter_le (s : multiset Ξ±) : filter p s ≀ s := quot.induction_on s $ Ξ» l, (filter_sublist _).subperm @[simp] theorem filter_subset (s : multiset Ξ±) : filter p s βŠ† s := subset_of_le $ filter_le _ _ theorem filter_le_filter {s t} (h : s ≀ t) : filter p s ≀ filter p t := le_induction_on h $ Ξ» l₁ lβ‚‚ h, (h.filter p).subperm lemma monotone_filter_left : monotone (filter p) := Ξ» s t, filter_le_filter p lemma monotone_filter_right (s : multiset Ξ±) ⦃p q : Ξ± β†’ Prop⦄ [decidable_pred p] [decidable_pred q] (h : p ≀ q) : s.filter p ≀ s.filter q := quotient.induction_on s (Ξ» l, (l.monotone_filter_right h).subperm) variable {p} @[simp] theorem filter_cons_of_pos {a : Ξ±} (s) : p a β†’ filter p (a ::β‚˜ s) = a ::β‚˜ filter p s := quot.induction_on s $ Ξ» l h, congr_arg coe $ filter_cons_of_pos l h @[simp] theorem filter_cons_of_neg {a : Ξ±} (s) : Β¬ p a β†’ filter p (a ::β‚˜ s) = filter p s := quot.induction_on s $ Ξ» l h, @congr_arg _ _ _ _ coe $ filter_cons_of_neg l h @[simp] theorem mem_filter {a : Ξ±} {s} : a ∈ filter p s ↔ a ∈ s ∧ p a := quot.induction_on s $ Ξ» l, mem_filter theorem of_mem_filter {a : Ξ±} {s} (h : a ∈ filter p s) : p a := (mem_filter.1 h).2 theorem mem_of_mem_filter {a : Ξ±} {s} (h : a ∈ filter p s) : a ∈ s := (mem_filter.1 h).1 theorem mem_filter_of_mem {a : Ξ±} {l} (m : a ∈ l) (h : p a) : a ∈ filter p l := mem_filter.2 ⟨m, h⟩ theorem filter_eq_self {s} : filter p s = s ↔ βˆ€ a ∈ s, p a := quot.induction_on s $ Ξ» l, iff.trans ⟨λ h, eq_of_sublist_of_length_eq (filter_sublist _) (@congr_arg _ _ _ _ card h), congr_arg coe⟩ filter_eq_self theorem filter_eq_nil {s} : filter p s = 0 ↔ βˆ€ a ∈ s, Β¬p a := quot.induction_on s $ Ξ» l, iff.trans ⟨λ h, eq_nil_of_length_eq_zero (@congr_arg _ _ _ _ card h), congr_arg coe⟩ filter_eq_nil theorem le_filter {s t} : s ≀ filter p t ↔ s ≀ t ∧ βˆ€ a ∈ s, p a := ⟨λ h, ⟨le_trans h (filter_le _ _), Ξ» a m, of_mem_filter (mem_of_le h m)⟩, Ξ» ⟨h, al⟩, filter_eq_self.2 al β–Έ filter_le_filter p h⟩ theorem filter_cons {a : Ξ±} (s : multiset Ξ±) : filter p (a ::β‚˜ s) = (if p a then {a} else 0) + filter p s := begin split_ifs with h, { rw [filter_cons_of_pos _ h, singleton_add] }, { rw [filter_cons_of_neg _ h, zero_add] }, end lemma filter_nsmul (s : multiset Ξ±) (n : β„•) : filter p (n β€’ s) = n β€’ filter p s := begin refine s.induction_on _ _, { simp only [filter_zero, nsmul_zero] }, { intros a ha ih, rw [nsmul_cons, filter_add, ih, filter_cons, nsmul_add], congr, split_ifs with hp; { simp only [filter_eq_self, nsmul_zero, filter_eq_nil], intros b hb, rwa (mem_singleton.mp (mem_of_mem_nsmul hb)) } } end variable (p) @[simp] theorem filter_sub [decidable_eq Ξ±] (s t : multiset Ξ±) : filter p (s - t) = filter p s - filter p t := begin revert s, refine multiset.induction_on t (by simp) (Ξ» a t IH s, _), rw [sub_cons, IH], by_cases p a, { rw [filter_cons_of_pos _ h, sub_cons], congr, by_cases m : a ∈ s, { rw [← cons_inj_right a, ← filter_cons_of_pos _ h, cons_erase (mem_filter_of_mem m h), cons_erase m] }, { rw [erase_of_not_mem m, erase_of_not_mem (mt mem_of_mem_filter m)] } }, { rw [filter_cons_of_neg _ h], by_cases m : a ∈ s, { rw [(by rw filter_cons_of_neg _ h : filter p (erase s a) = filter p (a ::β‚˜ erase s a)), cons_erase m] }, { rw [erase_of_not_mem m] } } end @[simp] theorem filter_union [decidable_eq Ξ±] (s t : multiset Ξ±) : filter p (s βˆͺ t) = filter p s βˆͺ filter p t := by simp [(βˆͺ), union] @[simp] theorem filter_inter [decidable_eq Ξ±] (s t : multiset Ξ±) : filter p (s ∩ t) = filter p s ∩ filter p t := le_antisymm (le_inter (filter_le_filter _ $ inter_le_left _ _) (filter_le_filter _ $ inter_le_right _ _)) $ le_filter.2 ⟨inf_le_inf (filter_le _ _) (filter_le _ _), Ξ» a h, of_mem_filter (mem_of_le (inter_le_left _ _) h)⟩ @[simp] theorem filter_filter (q) [decidable_pred q] (s : multiset Ξ±) : filter p (filter q s) = filter (Ξ» a, p a ∧ q a) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_filter p q l theorem filter_add_filter (q) [decidable_pred q] (s : multiset Ξ±) : filter p s + filter q s = filter (Ξ» a, p a ∨ q a) s + filter (Ξ» a, p a ∧ q a) s := multiset.induction_on s rfl $ Ξ» a s IH, by by_cases p a; by_cases q a; simp * theorem filter_add_not (s : multiset Ξ±) : filter p s + filter (Ξ» a, Β¬ p a) s = s := by rw [filter_add_filter, filter_eq_self.2, filter_eq_nil.2]; simp [decidable.em] theorem map_filter (f : Ξ² β†’ Ξ±) (s : multiset Ξ²) : filter p (map f s) = map f (filter (p ∘ f) s) := quot.induction_on s (Ξ» l, by simp [map_filter]) /-! ### Simultaneously filter and map elements of a multiset -/ /-- `filter_map f s` is a combination filter/map operation on `s`. The function `f : Ξ± β†’ option Ξ²` is applied to each element of `s`; if `f a` is `some b` then `b` is added to the result, otherwise `a` is removed from the resulting multiset. -/ def filter_map (f : Ξ± β†’ option Ξ²) (s : multiset Ξ±) : multiset Ξ² := quot.lift_on s (Ξ» l, (filter_map f l : multiset Ξ²)) (Ξ» l₁ lβ‚‚ h, quot.sound $ h.filter_map f) @[simp] theorem coe_filter_map (f : Ξ± β†’ option Ξ²) (l : list Ξ±) : filter_map f l = l.filter_map f := rfl @[simp] theorem filter_map_zero (f : Ξ± β†’ option Ξ²) : filter_map f 0 = 0 := rfl @[simp] theorem filter_map_cons_none {f : Ξ± β†’ option Ξ²} (a : Ξ±) (s : multiset Ξ±) (h : f a = none) : filter_map f (a ::β‚˜ s) = filter_map f s := quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ filter_map_cons_none a l h @[simp] theorem filter_map_cons_some (f : Ξ± β†’ option Ξ²) (a : Ξ±) (s : multiset Ξ±) {b : Ξ²} (h : f a = some b) : filter_map f (a ::β‚˜ s) = b ::β‚˜ filter_map f s := quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ filter_map_cons_some f a l h theorem filter_map_eq_map (f : Ξ± β†’ Ξ²) : filter_map (some ∘ f) = map f := funext $ Ξ» s, quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_map f) l theorem filter_map_eq_filter : filter_map (option.guard p) = filter p := funext $ Ξ» s, quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_filter p) l theorem filter_map_filter_map (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ option Ξ³) (s : multiset Ξ±) : filter_map g (filter_map f s) = filter_map (Ξ» x, (f x).bind g) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_filter_map f g l theorem map_filter_map (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ Ξ³) (s : multiset Ξ±) : map g (filter_map f s) = filter_map (Ξ» x, (f x).map g) s := quot.induction_on s $ Ξ» l, congr_arg coe $ map_filter_map f g l theorem filter_map_map (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ option Ξ³) (s : multiset Ξ±) : filter_map g (map f s) = filter_map (g ∘ f) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_map f g l theorem filter_filter_map (f : Ξ± β†’ option Ξ²) (p : Ξ² β†’ Prop) [decidable_pred p] (s : multiset Ξ±) : filter p (filter_map f s) = filter_map (Ξ» x, (f x).filter p) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_filter_map f p l theorem filter_map_filter (f : Ξ± β†’ option Ξ²) (s : multiset Ξ±) : filter_map f (filter p s) = filter_map (Ξ» x, if p x then f x else none) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_filter p f l @[simp] theorem filter_map_some (s : multiset Ξ±) : filter_map some s = s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_some l @[simp] theorem mem_filter_map (f : Ξ± β†’ option Ξ²) (s : multiset Ξ±) {b : Ξ²} : b ∈ filter_map f s ↔ βˆƒ a, a ∈ s ∧ f a = some b := quot.induction_on s $ Ξ» l, mem_filter_map f l theorem map_filter_map_of_inv (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ Ξ±) (H : βˆ€ x : Ξ±, (f x).map g = some x) (s : multiset Ξ±) : map g (filter_map f s) = s := quot.induction_on s $ Ξ» l, congr_arg coe $ map_filter_map_of_inv f g H l theorem filter_map_le_filter_map (f : Ξ± β†’ option Ξ²) {s t : multiset Ξ±} (h : s ≀ t) : filter_map f s ≀ filter_map f t := le_induction_on h $ Ξ» l₁ lβ‚‚ h, (h.filter_map _).subperm /-! ### countp -/ /-- `countp p s` counts the number of elements of `s` (with multiplicity) that satisfy `p`. -/ def countp (s : multiset Ξ±) : β„• := quot.lift_on s (countp p) (Ξ» l₁ lβ‚‚, perm.countp_eq p) @[simp] theorem coe_countp (l : list Ξ±) : countp p l = l.countp p := rfl @[simp] theorem countp_zero : countp p 0 = 0 := rfl variable {p} @[simp] theorem countp_cons_of_pos {a : Ξ±} (s) : p a β†’ countp p (a ::β‚˜ s) = countp p s + 1 := quot.induction_on s $ countp_cons_of_pos p @[simp] theorem countp_cons_of_neg {a : Ξ±} (s) : Β¬ p a β†’ countp p (a ::β‚˜ s) = countp p s := quot.induction_on s $ countp_cons_of_neg p variable (p) theorem countp_cons (b : Ξ±) (s) : countp p (b ::β‚˜ s) = countp p s + (if p b then 1 else 0) := begin split_ifs with h; simp only [h, multiset.countp_cons_of_pos, add_zero, multiset.countp_cons_of_neg, not_false_iff], end theorem countp_eq_card_filter (s) : countp p s = card (filter p s) := quot.induction_on s $ Ξ» l, countp_eq_length_filter _ _ @[simp] theorem countp_add (s t) : countp p (s + t) = countp p s + countp p t := by simp [countp_eq_card_filter] /-- `countp p`, the number of elements of a multiset satisfying `p`, promoted to an `add_monoid_hom`. -/ def countp_add_monoid_hom : multiset Ξ± β†’+ β„• := { to_fun := countp p, map_zero' := countp_zero _, map_add' := countp_add _ } @[simp] lemma coe_countp_add_monoid_hom : (countp_add_monoid_hom p : multiset Ξ± β†’ β„•) = countp p := rfl @[simp] theorem countp_sub [decidable_eq Ξ±] {s t : multiset Ξ±} (h : t ≀ s) : countp p (s - t) = countp p s - countp p t := by simp [countp_eq_card_filter, h, filter_le_filter] theorem countp_le_of_le {s t} (h : s ≀ t) : countp p s ≀ countp p t := by simpa [countp_eq_card_filter] using card_le_of_le (filter_le_filter p h) @[simp] theorem countp_filter (q) [decidable_pred q] (s : multiset Ξ±) : countp p (filter q s) = countp (Ξ» a, p a ∧ q a) s := by simp [countp_eq_card_filter] theorem countp_map (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) (p : Ξ² β†’ Prop) [decidable_pred p] : countp p (map f s) = (s.filter (Ξ» a, p (f a))).card := begin refine multiset.induction_on s _ (Ξ» a t IH, _), { rw [map_zero, countp_zero, filter_zero, card_zero] }, { rw [map_cons, countp_cons, IH, filter_cons, card_add, apply_ite card, card_zero, card_singleton, add_comm] }, end variable {p} theorem countp_pos {s} : 0 < countp p s ↔ βˆƒ a ∈ s, p a := by simp [countp_eq_card_filter, card_pos_iff_exists_mem] theorem countp_pos_of_mem {s a} (h : a ∈ s) (pa : p a) : 0 < countp p s := countp_pos.2 ⟨_, h, pa⟩ end /-! ### Multiplicity of an element -/ section variable [decidable_eq Ξ±] /-- `count a s` is the multiplicity of `a` in `s`. -/ def count (a : Ξ±) : multiset Ξ± β†’ β„• := countp (eq a) @[simp] theorem coe_count (a : Ξ±) (l : list Ξ±) : count a (↑l) = l.count a := coe_countp _ _ @[simp] theorem count_zero (a : Ξ±) : count a 0 = 0 := rfl @[simp] theorem count_cons_self (a : Ξ±) (s : multiset Ξ±) : count a (a ::β‚˜ s) = succ (count a s) := countp_cons_of_pos _ rfl @[simp, priority 990] theorem count_cons_of_ne {a b : Ξ±} (h : a β‰  b) (s : multiset Ξ±) : count a (b ::β‚˜ s) = count a s := countp_cons_of_neg _ h theorem count_le_of_le (a : Ξ±) {s t} : s ≀ t β†’ count a s ≀ count a t := countp_le_of_le _ theorem count_le_count_cons (a b : Ξ±) (s : multiset Ξ±) : count a s ≀ count a (b ::β‚˜ s) := count_le_of_le _ (le_cons_self _ _) theorem count_cons (a b : Ξ±) (s : multiset Ξ±) : count a (b ::β‚˜ s) = count a s + (if a = b then 1 else 0) := by by_cases h : a = b; simp [h] theorem count_singleton_self (a : Ξ±) : count a ({a} : multiset Ξ±) = 1 := by simp only [count_cons_self, singleton_eq_cons, eq_self_iff_true, count_zero] theorem count_singleton (a b : Ξ±) : count a ({b} : multiset Ξ±) = if a = b then 1 else 0 := by simp only [count_cons, singleton_eq_cons, count_zero, zero_add] @[simp] theorem count_add (a : Ξ±) : βˆ€ s t, count a (s + t) = count a s + count a t := countp_add _ /-- `count a`, the multiplicity of `a` in a multiset, promoted to an `add_monoid_hom`. -/ def count_add_monoid_hom (a : Ξ±) : multiset Ξ± β†’+ β„• := countp_add_monoid_hom (eq a) @[simp] lemma coe_count_add_monoid_hom {a : Ξ±} : (count_add_monoid_hom a : multiset Ξ± β†’ β„•) = count a := rfl @[simp] theorem count_nsmul (a : Ξ±) (n s) : count a (n β€’ s) = n * count a s := by induction n; simp [*, succ_nsmul', succ_mul, zero_nsmul] theorem count_pos {a : Ξ±} {s : multiset Ξ±} : 0 < count a s ↔ a ∈ s := by simp [count, countp_pos] @[simp, priority 980] theorem count_eq_zero_of_not_mem {a : Ξ±} {s : multiset Ξ±} (h : a βˆ‰ s) : count a s = 0 := by_contradiction $ Ξ» h', h $ count_pos.1 (nat.pos_of_ne_zero h') @[simp] theorem count_eq_zero {a : Ξ±} {s : multiset Ξ±} : count a s = 0 ↔ a βˆ‰ s := iff_not_comm.1 $ count_pos.symm.trans pos_iff_ne_zero theorem count_ne_zero {a : Ξ±} {s : multiset Ξ±} : count a s β‰  0 ↔ a ∈ s := by simp [ne.def, count_eq_zero] @[simp] theorem count_repeat_self (a : Ξ±) (n : β„•) : count a (repeat a n) = n := by simp [repeat] theorem count_repeat (a b : Ξ±) (n : β„•) : count a (repeat b n) = if (a = b) then n else 0 := begin split_ifs with h₁, { rw [h₁, count_repeat_self] }, { rw [count_eq_zero], apply mt eq_of_mem_repeat h₁ }, end @[simp] theorem count_erase_self (a : Ξ±) (s : multiset Ξ±) : count a (erase s a) = pred (count a s) := begin by_cases a ∈ s, { rw [(by rw cons_erase h : count a s = count a (a ::β‚˜ erase s a)), count_cons_self]; refl }, { rw [erase_of_not_mem h, count_eq_zero.2 h]; refl } end @[simp, priority 980] theorem count_erase_of_ne {a b : Ξ±} (ab : a β‰  b) (s : multiset Ξ±) : count a (erase s b) = count a s := begin by_cases b ∈ s, { rw [← count_cons_of_ne ab, cons_erase h] }, { rw [erase_of_not_mem h] } end @[simp] theorem count_sub (a : Ξ±) (s t : multiset Ξ±) : count a (s - t) = count a s - count a t := begin revert s, refine multiset.induction_on t (by simp) (Ξ» b t IH s, _), rw [sub_cons, IH], by_cases ab : a = b, { subst b, rw [count_erase_self, count_cons_self, sub_succ, pred_sub] }, { rw [count_erase_of_ne ab, count_cons_of_ne ab] } end @[simp] theorem count_union (a : Ξ±) (s t : multiset Ξ±) : count a (s βˆͺ t) = max (count a s) (count a t) := by simp [(βˆͺ), union, tsub_add_eq_max, -add_comm] @[simp] theorem count_inter (a : Ξ±) (s t : multiset Ξ±) : count a (s ∩ t) = min (count a s) (count a t) := begin apply @nat.add_left_cancel (count a (s - t)), rw [← count_add, sub_add_inter, count_sub, tsub_add_min], end lemma count_sum {m : multiset Ξ²} {f : Ξ² β†’ multiset Ξ±} {a : Ξ±} : count a (map f m).sum = sum (m.map $ Ξ»b, count a $ f b) := multiset.induction_on m (by simp) ( by simp) lemma count_bind {m : multiset Ξ²} {f : Ξ² β†’ multiset Ξ±} {a : Ξ±} : count a (bind m f) = sum (m.map $ Ξ»b, count a $ f b) := count_sum theorem le_count_iff_repeat_le {a : Ξ±} {s : multiset Ξ±} {n : β„•} : n ≀ count a s ↔ repeat a n ≀ s := quot.induction_on s $ Ξ» l, le_count_iff_repeat_sublist.trans repeat_le_coe.symm @[simp] theorem count_filter_of_pos {p} [decidable_pred p] {a} {s : multiset Ξ±} (h : p a) : count a (filter p s) = count a s := quot.induction_on s $ Ξ» l, count_filter h @[simp] theorem count_filter_of_neg {p} [decidable_pred p] {a} {s : multiset Ξ±} (h : Β¬ p a) : count a (filter p s) = 0 := multiset.count_eq_zero_of_not_mem (Ξ» t, h (of_mem_filter t)) theorem ext {s t : multiset Ξ±} : s = t ↔ βˆ€ a, count a s = count a t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, quotient.eq.trans perm_iff_count @[ext] theorem ext' {s t : multiset Ξ±} : (βˆ€ a, count a s = count a t) β†’ s = t := ext.2 @[simp] theorem coe_inter (s t : list Ξ±) : (s ∩ t : multiset Ξ±) = (s.bag_inter t : list Ξ±) := by ext; simp theorem le_iff_count {s t : multiset Ξ±} : s ≀ t ↔ βˆ€ a, count a s ≀ count a t := ⟨λ h a, count_le_of_le a h, Ξ» al, by rw ← (ext.2 (Ξ» a, by simp [max_eq_right (al a)]) : s βˆͺ t = t); apply le_union_left⟩ instance : distrib_lattice (multiset Ξ±) := { le_sup_inf := Ξ» s t u, le_of_eq $ eq.symm $ ext.2 $ Ξ» a, by simp only [max_min_distrib_left, multiset.count_inter, multiset.sup_eq_union, multiset.count_union, multiset.inf_eq_inter], ..multiset.lattice } instance : semilattice_sup_bot (multiset Ξ±) := { bot := 0, bot_le := zero_le, ..multiset.lattice } theorem repeat_inf (s : multiset Ξ±) (a : Ξ±) (n : β„•) : (repeat a n) βŠ“ s = repeat a (min (s.count a) n) := begin ext x, rw [inf_eq_inter, count_inter, count_repeat, count_repeat], by_cases x = a, simp only [min_comm, h, if_true, eq_self_iff_true], simp only [h, if_false, zero_min], end /-- `multiset.map f` preserves `count` if `f` is injective on the set of elements contained in the multiset -/ theorem count_map_eq_count [decidable_eq Ξ²] (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) (hf : set.inj_on f {x : Ξ± | x ∈ s}) (x ∈ s) : (s.map f).count (f x) = s.count x := begin suffices : (filter (Ξ» (a : Ξ±), f x = f a) s).count x = card (filter (Ξ» (a : Ξ±), f x = f a) s), { rw [count, countp_map, ← this], exact count_filter_of_pos rfl }, { rw eq_repeat.2 ⟨rfl, Ξ» b hb, eq_comm.1 ((hf H (mem_filter.1 hb).left) (mem_filter.1 hb).right)⟩, simp only [count_repeat, eq_self_iff_true, if_true, card_repeat]}, end end /-! ### Lift a relation to `multiset`s -/ section rel /-- `rel r s t` -- lift the relation `r` between two elements to a relation between `s` and `t`, s.t. there is a one-to-one mapping betweem elements in `s` and `t` following `r`. -/ @[mk_iff] inductive rel (r : Ξ± β†’ Ξ² β†’ Prop) : multiset Ξ± β†’ multiset Ξ² β†’ Prop | zero : rel 0 0 | cons {a b as bs} : r a b β†’ rel as bs β†’ rel (a ::β‚˜ as) (b ::β‚˜ bs) variables {Ξ΄ : Type*} {r : Ξ± β†’ Ξ² β†’ Prop} {p : Ξ³ β†’ Ξ΄ β†’ Prop} private lemma rel_flip_aux {s t} (h : rel r s t) : rel (flip r) t s := rel.rec_on h rel.zero (assume _ _ _ _ hβ‚€ h₁ ih, rel.cons hβ‚€ ih) lemma rel_flip {s t} : rel (flip r) s t ↔ rel r t s := ⟨rel_flip_aux, rel_flip_aux⟩ lemma rel_refl_of_refl_on {m : multiset Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} : (βˆ€ x ∈ m, r x x) β†’ rel r m m := begin apply m.induction_on, { intros, apply rel.zero }, { intros a m ih h, exact rel.cons (h _ (mem_cons_self _ _)) (ih (Ξ» _ ha, h _ (mem_cons_of_mem ha))) } end lemma rel_eq_refl {s : multiset Ξ±} : rel (=) s s := rel_refl_of_refl_on (Ξ» x hx, rfl) lemma rel_eq {s t : multiset Ξ±} : rel (=) s t ↔ s = t := begin split, { assume h, induction h; simp * }, { assume h, subst h, exact rel_eq_refl } end lemma rel.mono {r p : Ξ± β†’ Ξ² β†’ Prop} {s t} (hst : rel r s t) (h : βˆ€(a ∈ s) (b ∈ t), r a b β†’ p a b) : rel p s t := begin induction hst, case rel.zero { exact rel.zero }, case rel.cons : a b s t hab hst ih { apply rel.cons (h a (mem_cons_self _ _) b (mem_cons_self _ _) hab), exact ih (Ξ» a' ha' b' hb' h', h a' (mem_cons_of_mem ha') b' (mem_cons_of_mem hb') h') } end lemma rel.add {s t u v} (hst : rel r s t) (huv : rel r u v) : rel r (s + u) (t + v) := begin induction hst, case rel.zero { simpa using huv }, case rel.cons : a b s t hab hst ih { simpa using ih.cons hab } end lemma rel_flip_eq {s t : multiset Ξ±} : rel (Ξ»a b, b = a) s t ↔ s = t := show rel (flip (=)) s t ↔ s = t, by rw [rel_flip, rel_eq, eq_comm] @[simp] lemma rel_zero_left {b : multiset Ξ²} : rel r 0 b ↔ b = 0 := by rw [rel_iff]; simp @[simp] lemma rel_zero_right {a : multiset Ξ±} : rel r a 0 ↔ a = 0 := by rw [rel_iff]; simp lemma rel_cons_left {a as bs} : rel r (a ::β‚˜ as) bs ↔ (βˆƒb bs', r a b ∧ rel r as bs' ∧ bs = b ::β‚˜ bs') := begin split, { generalize hm : a ::β‚˜ as = m, assume h, induction h generalizing as, case rel.zero { simp at hm, contradiction }, case rel.cons : a' b as' bs ha'b h ih { rcases cons_eq_cons.1 hm with ⟨eq₁, eqβ‚‚βŸ© | ⟨h, cs, eq₁, eqβ‚‚βŸ©, { subst eq₁, subst eqβ‚‚, exact ⟨b, bs, ha'b, h, rfl⟩ }, { rcases ih eqβ‚‚.symm with ⟨b', bs', h₁, hβ‚‚, eq⟩, exact ⟨b', b ::β‚˜ bs', h₁, eq₁.symm β–Έ rel.cons ha'b hβ‚‚, eq.symm β–Έ cons_swap _ _ _⟩ } } }, { exact assume ⟨b, bs', hab, h, eq⟩, eq.symm β–Έ rel.cons hab h } end lemma rel_cons_right {as b bs} : rel r as (b ::β‚˜ bs) ↔ (βˆƒa as', r a b ∧ rel r as' bs ∧ as = a ::β‚˜ as') := begin rw [← rel_flip, rel_cons_left], apply exists_congr, assume a, apply exists_congr, assume as', rw [rel_flip, flip] end lemma rel_add_left {asβ‚€ as₁} : βˆ€{bs}, rel r (asβ‚€ + as₁) bs ↔ (βˆƒbsβ‚€ bs₁, rel r asβ‚€ bsβ‚€ ∧ rel r as₁ bs₁ ∧ bs = bsβ‚€ + bs₁) := multiset.induction_on asβ‚€ (by simp) begin assume a s ih bs, simp only [ih, cons_add, rel_cons_left], split, { assume h, rcases h with ⟨b, bs', hab, h, rfl⟩, rcases h with ⟨bsβ‚€, bs₁, hβ‚€, h₁, rfl⟩, exact ⟨b ::β‚˜ bsβ‚€, bs₁, ⟨b, bsβ‚€, hab, hβ‚€, rfl⟩, h₁, by simp⟩ }, { assume h, rcases h with ⟨bsβ‚€, bs₁, h, h₁, rfl⟩, rcases h with ⟨b, bs, hab, hβ‚€, rfl⟩, exact ⟨b, bs + bs₁, hab, ⟨bs, bs₁, hβ‚€, h₁, rfl⟩, by simp⟩ } end lemma rel_add_right {as bsβ‚€ bs₁} : rel r as (bsβ‚€ + bs₁) ↔ (βˆƒasβ‚€ as₁, rel r asβ‚€ bsβ‚€ ∧ rel r as₁ bs₁ ∧ as = asβ‚€ + as₁) := by rw [← rel_flip, rel_add_left]; simp [rel_flip] lemma rel_map_left {s : multiset Ξ³} {f : Ξ³ β†’ Ξ±} : βˆ€{t}, rel r (s.map f) t ↔ rel (Ξ»a b, r (f a) b) s t := multiset.induction_on s (by simp) (by simp [rel_cons_left] {contextual := tt}) lemma rel_map_right {s : multiset Ξ±} {t : multiset Ξ³} {f : Ξ³ β†’ Ξ²} : rel r s (t.map f) ↔ rel (Ξ»a b, r a (f b)) s t := by rw [← rel_flip, rel_map_left, ← rel_flip]; refl lemma rel_join {s t} (h : rel (rel r) s t) : rel r s.join t.join := begin induction h, case rel.zero { simp }, case rel.cons : a b s t hab hst ih { simpa using hab.add ih } end lemma rel_map {s : multiset Ξ±} {t : multiset Ξ²} {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ΄} : rel p (s.map f) (t.map g) ↔ rel (Ξ»a b, p (f a) (g b)) s t := rel_map_left.trans rel_map_right lemma rel_bind {p : Ξ³ β†’ Ξ΄ β†’ Prop} {s t} {f : Ξ± β†’ multiset Ξ³} {g : Ξ² β†’ multiset Ξ΄} (h : (r β‡’ rel p) f g) (hst : rel r s t) : rel p (s.bind f) (t.bind g) := by { apply rel_join, rw rel_map, exact hst.mono (Ξ» a ha b hb hr, h hr) } lemma card_eq_card_of_rel {r : Ξ± β†’ Ξ² β†’ Prop} {s : multiset Ξ±} {t : multiset Ξ²} (h : rel r s t) : card s = card t := by induction h; simp [*] lemma exists_mem_of_rel_of_mem {r : Ξ± β†’ Ξ² β†’ Prop} {s : multiset Ξ±} {t : multiset Ξ²} (h : rel r s t) : βˆ€ {a : Ξ±} (ha : a ∈ s), βˆƒ b ∈ t, r a b := begin induction h with x y s t hxy hst ih, { simp }, { assume a ha, cases mem_cons.1 ha with ha ha, { exact ⟨y, mem_cons_self _ _, ha.symm β–Έ hxy⟩ }, { rcases ih ha with ⟨b, hbt, hab⟩, exact ⟨b, mem_cons.2 (or.inr hbt), hab⟩ } } end lemma rel_of_forall {m1 m2 : multiset Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (h : βˆ€ a b, a ∈ m1 β†’ b ∈ m2 β†’ r a b) (hc : card m1 = card m2) : m1.rel r m2 := begin revert m1, apply m2.induction_on, { intros m h hc, rw [rel_zero_right, ← card_eq_zero, hc, card_zero] }, { intros a t ih m h hc, rw card_cons at hc, obtain ⟨b, hb⟩ := card_pos_iff_exists_mem.1 (show 0 < card m, from hc.symm β–Έ (nat.succ_pos _)), obtain ⟨m', rfl⟩ := exists_cons_of_mem hb, refine rel_cons_right.mpr ⟨b, m', h _ _ hb (mem_cons_self _ _), ih _ _, rfl⟩, { exact Ξ» _ _ ha hb, h _ _ (mem_cons_of_mem ha) (mem_cons_of_mem hb) }, { simpa using hc } } end lemma rel_repeat_left {m : multiset Ξ±} {a : Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} {n : β„•} : (repeat a n).rel r m ↔ m.card = n ∧ βˆ€ x, x ∈ m β†’ r a x := ⟨λ h, ⟨(card_eq_card_of_rel h).symm.trans (card_repeat _ _), Ξ» x hx, begin obtain ⟨b, hb1, hb2⟩ := exists_mem_of_rel_of_mem (rel_flip.2 h) hx, rwa eq_of_mem_repeat hb1 at hb2, end⟩, Ξ» h, rel_of_forall (Ξ» x y hx hy, (eq_of_mem_repeat hx).symm β–Έ (h.2 _ hy)) (eq.trans (card_repeat _ _) h.1.symm)⟩ lemma rel_repeat_right {m : multiset Ξ±} {a : Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} {n : β„•} : m.rel r (repeat a n) ↔ m.card = n ∧ βˆ€ x, x ∈ m β†’ r x a := by { rw [← rel_flip], exact rel_repeat_left } lemma sum_le_sum_of_rel_le [ordered_add_comm_monoid Ξ±] {m1 m2 : multiset Ξ±} (h : m1.rel (≀) m2) : m1.sum ≀ m2.sum := begin induction h with _ _ _ _ rh _ rt, { refl }, { rw [sum_cons, sum_cons], exact add_le_add rh rt } end end rel section sum_inequalities lemma le_sum_of_mem [canonically_ordered_add_monoid Ξ±] {m : multiset Ξ±} {a : Ξ±} (h : a ∈ m) : a ≀ m.sum := begin obtain ⟨m', rfl⟩ := exists_cons_of_mem h, rw [sum_cons], exact _root_.le_add_right (le_refl a), end variables [ordered_add_comm_monoid Ξ±] lemma sum_map_le_sum {m : multiset Ξ±} (f : Ξ± β†’ Ξ±) (h : βˆ€ x, x ∈ m β†’ f x ≀ x) : (m.map f).sum ≀ m.sum := sum_le_sum_of_rel_le (rel_map_left.2 (rel_refl_of_refl_on h)) lemma sum_le_sum_map {m : multiset Ξ±} (f : Ξ± β†’ Ξ±) (h : βˆ€ x, x ∈ m β†’ x ≀ f x) : m.sum ≀ (m.map f).sum := @sum_map_le_sum (order_dual Ξ±) _ _ f h lemma card_nsmul_le_sum {b : Ξ±} {m : multiset Ξ±} (h : βˆ€ x, x ∈ m β†’ b ≀ x) : (card m) β€’ b ≀ m.sum := begin rw [←multiset.sum_repeat, ←multiset.map_const], exact sum_map_le_sum _ h, end lemma sum_le_card_nsmul {b : Ξ±} {m : multiset Ξ±} (h : βˆ€ x, x ∈ m β†’ x ≀ b) : m.sum ≀ (card m) β€’ b := begin rw [←multiset.sum_repeat, ←multiset.map_const], exact sum_le_sum_map _ h, end end sum_inequalities section map theorem map_eq_map {f : Ξ± β†’ Ξ²} (hf : function.injective f) {s t : multiset Ξ±} : s.map f = t.map f ↔ s = t := by { rw [← rel_eq, ← rel_eq, rel_map], simp only [hf.eq_iff] } theorem map_injective {f : Ξ± β†’ Ξ²} (hf : function.injective f) : function.injective (multiset.map f) := assume x y, (map_eq_map hf).1 end map section quot theorem map_mk_eq_map_mk_of_rel {r : Ξ± β†’ Ξ± β†’ Prop} {s t : multiset Ξ±} (hst : s.rel r t) : s.map (quot.mk r) = t.map (quot.mk r) := rel.rec_on hst rfl $ assume a b s t hab hst ih, by simp [ih, quot.sound hab] theorem exists_multiset_eq_map_quot_mk {r : Ξ± β†’ Ξ± β†’ Prop} (s : multiset (quot r)) : βˆƒt:multiset Ξ±, s = t.map (quot.mk r) := multiset.induction_on s ⟨0, rfl⟩ $ assume a s ⟨t, ht⟩, quot.induction_on a $ assume a, ht.symm β–Έ ⟨a ::β‚˜ t, (map_cons _ _ _).symm⟩ theorem induction_on_multiset_quot {r : Ξ± β†’ Ξ± β†’ Prop} {p : multiset (quot r) β†’ Prop} (s : multiset (quot r)) : (βˆ€s:multiset Ξ±, p (s.map (quot.mk r))) β†’ p s := match s, exists_multiset_eq_map_quot_mk s with _, ⟨t, rfl⟩ := assume h, h _ end end quot /-! ### Disjoint multisets -/ /-- `disjoint s t` means that `s` and `t` have no elements in common. -/ def disjoint (s t : multiset Ξ±) : Prop := βˆ€ ⦃a⦄, a ∈ s β†’ a ∈ t β†’ false @[simp] theorem coe_disjoint (l₁ lβ‚‚ : list Ξ±) : @disjoint Ξ± l₁ lβ‚‚ ↔ l₁.disjoint lβ‚‚ := iff.rfl theorem disjoint.symm {s t : multiset Ξ±} (d : disjoint s t) : disjoint t s | a iβ‚‚ i₁ := d i₁ iβ‚‚ theorem disjoint_comm {s t : multiset Ξ±} : disjoint s t ↔ disjoint t s := ⟨disjoint.symm, disjoint.symm⟩ theorem disjoint_left {s t : multiset Ξ±} : disjoint s t ↔ βˆ€ {a}, a ∈ s β†’ a βˆ‰ t := iff.rfl theorem disjoint_right {s t : multiset Ξ±} : disjoint s t ↔ βˆ€ {a}, a ∈ t β†’ a βˆ‰ s := disjoint_comm theorem disjoint_iff_ne {s t : multiset Ξ±} : disjoint s t ↔ βˆ€ a ∈ s, βˆ€ b ∈ t, a β‰  b := by simp [disjoint_left, imp_not_comm] theorem disjoint_of_subset_left {s t u : multiset Ξ±} (h : s βŠ† u) (d : disjoint u t) : disjoint s t | x m₁ := d (h m₁) theorem disjoint_of_subset_right {s t u : multiset Ξ±} (h : t βŠ† u) (d : disjoint s u) : disjoint s t | x m m₁ := d m (h m₁) theorem disjoint_of_le_left {s t u : multiset Ξ±} (h : s ≀ u) : disjoint u t β†’ disjoint s t := disjoint_of_subset_left (subset_of_le h) theorem disjoint_of_le_right {s t u : multiset Ξ±} (h : t ≀ u) : disjoint s u β†’ disjoint s t := disjoint_of_subset_right (subset_of_le h) @[simp] theorem zero_disjoint (l : multiset Ξ±) : disjoint 0 l | a := (not_mem_nil a).elim @[simp, priority 1100] theorem singleton_disjoint {l : multiset Ξ±} {a : Ξ±} : disjoint {a} l ↔ a βˆ‰ l := by simp [disjoint]; refl @[simp, priority 1100] theorem disjoint_singleton {l : multiset Ξ±} {a : Ξ±} : disjoint l {a} ↔ a βˆ‰ l := by rw [disjoint_comm, singleton_disjoint] @[simp] theorem disjoint_add_left {s t u : multiset Ξ±} : disjoint (s + t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_add_right {s t u : multiset Ξ±} : disjoint s (t + u) ↔ disjoint s t ∧ disjoint s u := by rw [disjoint_comm, disjoint_add_left]; tauto @[simp] theorem disjoint_cons_left {a : Ξ±} {s t : multiset Ξ±} : disjoint (a ::β‚˜ s) t ↔ a βˆ‰ t ∧ disjoint s t := (@disjoint_add_left _ {a} s t).trans $ by rw singleton_disjoint @[simp] theorem disjoint_cons_right {a : Ξ±} {s t : multiset Ξ±} : disjoint s (a ::β‚˜ t) ↔ a βˆ‰ s ∧ disjoint s t := by rw [disjoint_comm, disjoint_cons_left]; tauto theorem inter_eq_zero_iff_disjoint [decidable_eq Ξ±] {s t : multiset Ξ±} : s ∩ t = 0 ↔ disjoint s t := by rw ← subset_zero; simp [subset_iff, disjoint] @[simp] theorem disjoint_union_left [decidable_eq Ξ±] {s t u : multiset Ξ±} : disjoint (s βˆͺ t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right [decidable_eq Ξ±] {s t u : multiset Ξ±} : disjoint s (t βˆͺ u) ↔ disjoint s t ∧ disjoint s u := by simp [disjoint, or_imp_distrib, forall_and_distrib] lemma add_eq_union_iff_disjoint [decidable_eq Ξ±] {s t : multiset Ξ±} : s + t = s βˆͺ t ↔ disjoint s t := by simp_rw [←inter_eq_zero_iff_disjoint, ext, count_add, count_union, count_inter, count_zero, nat.min_eq_zero_iff, nat.add_eq_max_iff] lemma disjoint_map_map {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ³} {s : multiset Ξ±} {t : multiset Ξ²} : disjoint (s.map f) (t.map g) ↔ (βˆ€a∈s, βˆ€b∈t, f a β‰  g b) := by { simp [disjoint, @eq_comm _ (f _) (g _)], refl } /-- `pairwise r m` states that there exists a list of the elements s.t. `r` holds pairwise on this list. -/ def pairwise (r : Ξ± β†’ Ξ± β†’ Prop) (m : multiset Ξ±) : Prop := βˆƒl:list Ξ±, m = l ∧ l.pairwise r lemma pairwise_coe_iff_pairwise {r : Ξ± β†’ Ξ± β†’ Prop} (hr : symmetric r) {l : list Ξ±} : multiset.pairwise r l ↔ l.pairwise r := iff.intro (assume ⟨l', eq, h⟩, ((quotient.exact eq).pairwise_iff hr).2 h) (assume h, ⟨l, rfl, h⟩) end multiset namespace multiset section choose variables (p : Ξ± β†’ Prop) [decidable_pred p] (l : multiset Ξ±) /-- Given a proof `hp` that there exists a unique `a ∈ l` such that `p a`, `choose_x p l hp` returns that `a` together with proofs of `a ∈ l` and `p a`. -/ def choose_x : Ξ  hp : (βˆƒ! a, a ∈ l ∧ p a), { a // a ∈ l ∧ p a } := quotient.rec_on l (Ξ» l' ex_unique, list.choose_x p l' (exists_of_exists_unique ex_unique)) begin intros, funext hp, suffices all_equal : βˆ€ x y : { t // t ∈ b ∧ p t }, x = y, { apply all_equal }, { rintros ⟨x, px⟩ ⟨y, py⟩, rcases hp with ⟨z, ⟨z_mem_l, pz⟩, z_unique⟩, congr, calc x = z : z_unique x px ... = y : (z_unique y py).symm } end /-- Given a proof `hp` that there exists a unique `a ∈ l` such that `p a`, `choose p l hp` returns that `a`. -/ def choose (hp : βˆƒ! a, a ∈ l ∧ p a) : Ξ± := choose_x p l hp lemma choose_spec (hp : βˆƒ! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : βˆƒ! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : βˆƒ! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose variable (Ξ±) /-- The equivalence between lists and multisets of a subsingleton type. -/ def subsingleton_equiv [subsingleton Ξ±] : list Ξ± ≃ multiset Ξ± := { to_fun := coe, inv_fun := quot.lift id $ Ξ» (a b : list Ξ±) (h : a ~ b), list.ext_le h.length_eq $ Ξ» n h₁ hβ‚‚, subsingleton.elim _ _, left_inv := Ξ» l, rfl, right_inv := Ξ» m, quot.induction_on m $ Ξ» l, rfl } variable {Ξ±} @[simp] lemma coe_subsingleton_equiv [subsingleton Ξ±] : (subsingleton_equiv Ξ± : list Ξ± β†’ multiset Ξ±) = coe := rfl end multiset @[to_additive] theorem monoid_hom.map_multiset_prod [comm_monoid Ξ±] [comm_monoid Ξ²] (f : Ξ± β†’* Ξ²) (s : multiset Ξ±) : f s.prod = (s.map f).prod := (s.prod_hom f).symm
43bbf775aafabff40cfbe5001fac8d7ec0ecd07b
7cef822f3b952965621309e88eadf618da0c8ae9
/src/data/pfun.lean
73e516f3831259ac8f8b3f149f40023f22985471
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
22,508
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro, Jeremy Avigad -/ import data.set.basic data.equiv.basic data.rel logic.relator /-- `roption Ξ±` is the type of "partial values" of type `Ξ±`. It is similar to `option Ξ±` except the domain condition can be an arbitrary proposition, not necessarily decidable. -/ structure {u} roption (Ξ± : Type u) : Type u := (dom : Prop) (get : dom β†’ Ξ±) namespace roption variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} /-- Convert an `roption Ξ±` with a decidable domain to an option -/ def to_option (o : roption Ξ±) [decidable o.dom] : option Ξ± := if h : dom o then some (o.get h) else none /-- `roption` extensionality -/ theorem ext' : βˆ€ {o p : roption Ξ±} (H1 : o.dom ↔ p.dom) (H2 : βˆ€h₁ hβ‚‚, o.get h₁ = p.get hβ‚‚), o = p | ⟨od, o⟩ ⟨pd, p⟩ H1 H2 := have t : od = pd, from propext H1, by cases t; rw [show o = p, from funext $ Ξ»p, H2 p p] /-- `roption` eta expansion -/ @[simp] theorem eta : Ξ  (o : roption Ξ±), (⟨o.dom, Ξ» h, o.get h⟩ : roption Ξ±) = o | ⟨h, f⟩ := rfl /-- `a ∈ o` means that `o` is defined and equal to `a` -/ protected def mem (a : Ξ±) (o : roption Ξ±) : Prop := βˆƒ h, o.get h = a instance : has_mem Ξ± (roption Ξ±) := ⟨roption.mem⟩ theorem mem_eq (a : Ξ±) (o : roption Ξ±) : (a ∈ o) = (βˆƒ h, o.get h = a) := rfl theorem dom_iff_mem : βˆ€ {o : roption Ξ±}, o.dom ↔ βˆƒy, y ∈ o | ⟨p, f⟩ := ⟨λh, ⟨f h, h, rfl⟩, λ⟨_, h, rfl⟩, h⟩ theorem get_mem {o : roption Ξ±} (h) : get o h ∈ o := ⟨_, rfl⟩ /-- `roption` extensionality -/ theorem ext {o p : roption Ξ±} (H : βˆ€ a, a ∈ o ↔ a ∈ p) : o = p := ext' ⟨λ h, ((H _).1 ⟨h, rfl⟩).fst, Ξ» h, ((H _).2 ⟨h, rfl⟩).fst⟩ $ Ξ» a b, ((H _).2 ⟨_, rfl⟩).snd /-- The `none` value in `roption` has a `false` domain and an empty function. -/ def none : roption Ξ± := ⟨false, false.rec _⟩ @[simp] theorem not_mem_none (a : Ξ±) : a βˆ‰ @none Ξ± := Ξ» h, h.fst /-- The `some a` value in `roption` has a `true` domain and the function returns `a`. -/ def some (a : Ξ±) : roption Ξ± := ⟨true, Ξ»_, a⟩ theorem mem_unique : relator.left_unique ((∈) : Ξ± β†’ roption Ξ± β†’ Prop) | _ ⟨p, f⟩ _ ⟨h₁, rfl⟩ ⟨hβ‚‚, rfl⟩ := rfl theorem get_eq_of_mem {o : roption Ξ±} {a} (h : a ∈ o) (h') : get o h' = a := mem_unique ⟨_, rfl⟩ h @[simp] theorem get_some {a : Ξ±} (ha : (some a).dom) : get (some a) ha = a := rfl theorem mem_some (a : Ξ±) : a ∈ some a := ⟨trivial, rfl⟩ @[simp] theorem mem_some_iff {a b} : b ∈ (some a : roption Ξ±) ↔ b = a := ⟨λ⟨h, e⟩, e.symm, Ξ» e, ⟨trivial, e.symm⟩⟩ theorem eq_some_iff {a : Ξ±} {o : roption Ξ±} : o = some a ↔ a ∈ o := ⟨λ e, e.symm β–Έ mem_some _, Ξ» ⟨h, e⟩, e β–Έ ext' (iff_true_intro h) (Ξ» _ _, rfl)⟩ theorem eq_none_iff {o : roption Ξ±} : o = none ↔ βˆ€ a, a βˆ‰ o := ⟨λ e, e.symm β–Έ not_mem_none, Ξ» h, ext (by simpa [not_mem_none])⟩ theorem eq_none_iff' {o : roption Ξ±} : o = none ↔ Β¬ o.dom := ⟨λ e, e.symm β–Έ id, Ξ» h, eq_none_iff.2 (Ξ» a h', h h'.fst)⟩ lemma some_ne_none (x : Ξ±) : some x β‰  none := by { intro h, change none.dom, rw [← h], trivial } lemma ne_none_iff {o : roption Ξ±} : o β‰  none ↔ βˆƒx, o = some x := begin split, { rw [ne, eq_none_iff], intro h, push_neg at h, cases h with x hx, use x, rwa [eq_some_iff] }, { rintro ⟨x, rfl⟩, apply some_ne_none } end @[simp] lemma some_inj {a b : Ξ±} : roption.some a = some b ↔ a = b := function.injective.eq_iff (Ξ» a b h, congr_fun (eq_of_heq (roption.mk.inj h).2) trivial) @[simp] lemma some_get {a : roption Ξ±} (ha : a.dom) : roption.some (roption.get a ha) = a := eq.symm (eq_some_iff.2 ⟨ha, rfl⟩) lemma get_eq_iff_eq_some {a : roption Ξ±} {ha : a.dom} {b : Ξ±} : a.get ha = b ↔ a = some b := ⟨λ h, by simp [h.symm], Ξ» h, by simp [h]⟩ instance none_decidable : decidable (@none Ξ±).dom := decidable.false instance some_decidable (a : Ξ±) : decidable (some a).dom := decidable.true def get_or_else (a : roption Ξ±) [decidable a.dom] (d : Ξ±) := if ha : a.dom then a.get ha else d @[simp] lemma get_or_else_none (d : Ξ±) : get_or_else none d = d := dif_neg id @[simp] lemma get_or_else_some (a : Ξ±) (d : Ξ±) : get_or_else (some a) d = a := dif_pos trivial @[simp] theorem mem_to_option {o : roption Ξ±} [decidable o.dom] {a : Ξ±} : a ∈ to_option o ↔ a ∈ o := begin unfold to_option, by_cases h : o.dom; simp [h], { exact ⟨λ h, ⟨_, h⟩, Ξ» ⟨_, h⟩, h⟩ }, { exact mt Exists.fst h } end /-- Convert an `option Ξ±` into an `roption Ξ±` -/ def of_option : option Ξ± β†’ roption Ξ± | option.none := none | (option.some a) := some a @[simp] theorem mem_of_option {a : Ξ±} : βˆ€ {o : option Ξ±}, a ∈ of_option o ↔ a ∈ o | option.none := ⟨λ h, h.fst.elim, Ξ» h, option.no_confusion h⟩ | (option.some b) := ⟨λ h, congr_arg option.some h.snd, Ξ» h, ⟨trivial, option.some.inj h⟩⟩ @[simp] theorem of_option_dom {Ξ±} : βˆ€ (o : option Ξ±), (of_option o).dom ↔ o.is_some | option.none := by simp [of_option, none] | (option.some a) := by simp [of_option] theorem of_option_eq_get {Ξ±} (o : option Ξ±) : of_option o = ⟨_, @option.get _ o⟩ := roption.ext' (of_option_dom o) $ Ξ» h₁ hβ‚‚, by cases o; [cases h₁, refl] instance : has_coe (option Ξ±) (roption Ξ±) := ⟨of_option⟩ @[simp] theorem mem_coe {a : Ξ±} {o : option Ξ±} : a ∈ (o : roption Ξ±) ↔ a ∈ o := mem_of_option @[simp] theorem coe_none : (@option.none Ξ± : roption Ξ±) = none := rfl @[simp] theorem coe_some (a : Ξ±) : (option.some a : roption Ξ±) = some a := rfl @[elab_as_eliminator] protected lemma induction_on {P : roption Ξ± β†’ Prop} (a : roption Ξ±) (hnone : P none) (hsome : βˆ€ a : Ξ±, P (some a)) : P a := (classical.em a.dom).elim (Ξ» h, roption.some_get h β–Έ hsome _) (Ξ» h, (eq_none_iff'.2 h).symm β–Έ hnone) instance of_option_decidable : βˆ€ o : option Ξ±, decidable (of_option o).dom | option.none := roption.none_decidable | (option.some a) := roption.some_decidable a @[simp] theorem to_of_option (o : option Ξ±) : to_option (of_option o) = o := by cases o; refl @[simp] theorem of_to_option (o : roption Ξ±) [decidable o.dom] : of_option (to_option o) = o := ext $ Ξ» a, mem_of_option.trans mem_to_option noncomputable def equiv_option : roption Ξ± ≃ option Ξ± := by haveI := classical.dec; exact ⟨λ o, to_option o, of_option, Ξ» o, of_to_option o, Ξ» o, eq.trans (by dsimp; congr) (to_of_option o)⟩ /-- `assert p f` is a bind-like operation which appends an additional condition `p` to the domain and uses `f` to produce the value. -/ def assert (p : Prop) (f : p β†’ roption Ξ±) : roption Ξ± := βŸ¨βˆƒh : p, (f h).dom, Ξ»ha, (f ha.fst).get ha.snd⟩ /-- The bind operation has value `g (f.get)`, and is defined when all the parts are defined. -/ protected def bind (f : roption Ξ±) (g : Ξ± β†’ roption Ξ²) : roption Ξ² := assert (dom f) (Ξ»b, g (f.get b)) /-- The map operation for `roption` just maps the value and maintains the same domain. -/ def map (f : Ξ± β†’ Ξ²) (o : roption Ξ±) : roption Ξ² := ⟨o.dom, f ∘ o.get⟩ theorem mem_map (f : Ξ± β†’ Ξ²) {o : roption Ξ±} : βˆ€ {a}, a ∈ o β†’ f a ∈ map f o | _ ⟨h, rfl⟩ := ⟨_, rfl⟩ @[simp] theorem mem_map_iff (f : Ξ± β†’ Ξ²) {o : roption Ξ±} {b} : b ∈ map f o ↔ βˆƒ a ∈ o, f a = b := ⟨match b with _, ⟨h, rfl⟩ := ⟨_, ⟨_, rfl⟩, rfl⟩ end, Ξ» ⟨a, h₁, hβ‚‚βŸ©, hβ‚‚ β–Έ mem_map f hβ‚βŸ© @[simp] theorem map_none (f : Ξ± β†’ Ξ²) : map f none = none := eq_none_iff.2 $ Ξ» a, by simp @[simp] theorem map_some (f : Ξ± β†’ Ξ²) (a : Ξ±) : map f (some a) = some (f a) := eq_some_iff.2 $ mem_map f $ mem_some _ theorem mem_assert {p : Prop} {f : p β†’ roption Ξ±} : βˆ€ {a} (h : p), a ∈ f h β†’ a ∈ assert p f | _ _ ⟨h, rfl⟩ := ⟨⟨_, _⟩, rfl⟩ @[simp] theorem mem_assert_iff {p : Prop} {f : p β†’ roption Ξ±} {a} : a ∈ assert p f ↔ βˆƒ h : p, a ∈ f h := ⟨match a with _, ⟨h, rfl⟩ := ⟨_, ⟨_, rfl⟩⟩ end, Ξ» ⟨a, h⟩, mem_assert _ h⟩ theorem mem_bind {f : roption Ξ±} {g : Ξ± β†’ roption Ξ²} : βˆ€ {a b}, a ∈ f β†’ b ∈ g a β†’ b ∈ f.bind g | _ _ ⟨h, rfl⟩ ⟨hβ‚‚, rfl⟩ := ⟨⟨_, _⟩, rfl⟩ @[simp] theorem mem_bind_iff {f : roption Ξ±} {g : Ξ± β†’ roption Ξ²} {b} : b ∈ f.bind g ↔ βˆƒ a ∈ f, b ∈ g a := ⟨match b with _, ⟨⟨h₁, hβ‚‚βŸ©, rfl⟩ := ⟨_, ⟨_, rfl⟩, ⟨_, rfl⟩⟩ end, Ξ» ⟨a, h₁, hβ‚‚βŸ©, mem_bind h₁ hβ‚‚βŸ© @[simp] theorem bind_none (f : Ξ± β†’ roption Ξ²) : none.bind f = none := eq_none_iff.2 $ Ξ» a, by simp @[simp] theorem bind_some (a : Ξ±) (f : Ξ± β†’ roption Ξ²) : (some a).bind f = f a := ext $ by simp theorem bind_some_eq_map (f : Ξ± β†’ Ξ²) (x : roption Ξ±) : x.bind (some ∘ f) = map f x := ext $ by simp [eq_comm] theorem bind_assoc {Ξ³} (f : roption Ξ±) (g : Ξ± β†’ roption Ξ²) (k : Ξ² β†’ roption Ξ³) : (f.bind g).bind k = f.bind (Ξ» x, (g x).bind k) := ext $ Ξ» a, by simp; exact ⟨λ ⟨_, ⟨_, h₁, hβ‚‚βŸ©, hβ‚ƒβŸ©, ⟨_, h₁, _, hβ‚‚, hβ‚ƒβŸ©, Ξ» ⟨_, h₁, _, hβ‚‚, hβ‚ƒβŸ©, ⟨_, ⟨_, h₁, hβ‚‚βŸ©, hβ‚ƒβŸ©βŸ© @[simp] theorem bind_map {Ξ³} (f : Ξ± β†’ Ξ²) (x) (g : Ξ² β†’ roption Ξ³) : (map f x).bind g = x.bind (Ξ» y, g (f y)) := by rw [← bind_some_eq_map, bind_assoc]; simp @[simp] theorem map_bind {Ξ³} (f : Ξ± β†’ roption Ξ²) (x : roption Ξ±) (g : Ξ² β†’ Ξ³) : map g (x.bind f) = x.bind (Ξ» y, map g (f y)) := by rw [← bind_some_eq_map, bind_assoc]; simp [bind_some_eq_map] theorem map_map (g : Ξ² β†’ Ξ³) (f : Ξ± β†’ Ξ²) (o : roption Ξ±) : map g (map f o) = map (g ∘ f) o := by rw [← bind_some_eq_map, bind_map, bind_some_eq_map] instance : monad roption := { pure := @some, map := @map, bind := @roption.bind } instance : is_lawful_monad roption := { bind_pure_comp_eq_map := @bind_some_eq_map, id_map := Ξ» Ξ² f, by cases f; refl, pure_bind := @bind_some, bind_assoc := @bind_assoc } theorem map_id' {f : Ξ± β†’ Ξ±} (H : βˆ€ (x : Ξ±), f x = x) (o) : map f o = o := by rw [show f = id, from funext H]; exact id_map o @[simp] theorem bind_some_right (x : roption Ξ±) : x.bind some = x := by rw [bind_some_eq_map]; simp [map_id'] @[simp] theorem ret_eq_some (a : Ξ±) : return a = some a := rfl @[simp] theorem map_eq_map {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (o : roption Ξ±) : f <$> o = map f o := rfl @[simp] theorem bind_eq_bind {Ξ± Ξ²} (f : roption Ξ±) (g : Ξ± β†’ roption Ξ²) : f >>= g = f.bind g := rfl instance : monad_fail roption := { fail := Ξ»_ _, none, ..roption.monad } /- `restrict p o h` replaces the domain of `o` with `p`, and is well defined when `p` implies `o` is defined. -/ def restrict (p : Prop) : βˆ€ (o : roption Ξ±), (p β†’ o.dom) β†’ roption Ξ± | ⟨d, f⟩ H := ⟨p, Ξ»h, f (H h)⟩ @[simp] theorem mem_restrict (p : Prop) (o : roption Ξ±) (h : p β†’ o.dom) (a : Ξ±) : a ∈ restrict p o h ↔ p ∧ a ∈ o := begin cases o, dsimp [restrict, mem_eq], split, { rintro ⟨hβ‚€, hβ‚βŸ©, exact ⟨hβ‚€, ⟨_, hβ‚βŸ©βŸ© }, rintro ⟨hβ‚€, h₁, hβ‚‚βŸ©, exact ⟨hβ‚€, hβ‚‚βŸ© end /-- `unwrap o` gets the value at `o`, ignoring the condition. (This function is unsound.) -/ meta def unwrap (o : roption Ξ±) : Ξ± := o.get undefined theorem assert_defined {p : Prop} {f : p β†’ roption Ξ±} : βˆ€ (h : p), (f h).dom β†’ (assert p f).dom := exists.intro theorem bind_defined {f : roption Ξ±} {g : Ξ± β†’ roption Ξ²} : βˆ€ (h : f.dom), (g (f.get h)).dom β†’ (f.bind g).dom := assert_defined @[simp] theorem bind_dom {f : roption Ξ±} {g : Ξ± β†’ roption Ξ²} : (f.bind g).dom ↔ βˆƒ h : f.dom, (g (f.get h)).dom := iff.rfl end roption /-- `pfun Ξ± Ξ²`, or `Ξ± β†’. Ξ²`, is the type of partial functions from `Ξ±` to `Ξ²`. It is defined as `Ξ± β†’ roption Ξ²`. -/ def pfun (Ξ± : Type*) (Ξ² : Type*) := Ξ± β†’ roption Ξ² infixr ` β†’. `:25 := pfun namespace pfun variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} /-- The domain of a partial function -/ def dom (f : Ξ± β†’. Ξ²) : set Ξ± := Ξ» a, (f a).dom theorem mem_dom (f : Ξ± β†’. Ξ²) (x : Ξ±) : x ∈ dom f ↔ βˆƒ y, y ∈ f x := by simp [dom, set.mem_def, roption.dom_iff_mem] theorem dom_eq (f : Ξ± β†’. Ξ²) : dom f = {x | βˆƒ y, y ∈ f x} := set.ext (mem_dom f) /-- Evaluate a partial function -/ def fn (f : Ξ± β†’. Ξ²) (x) (h : dom f x) : Ξ² := (f x).get h /-- Evaluate a partial function to return an `option` -/ def eval_opt (f : Ξ± β†’. Ξ²) [D : decidable_pred (dom f)] (x : Ξ±) : option Ξ² := @roption.to_option _ _ (D x) /-- Partial function extensionality -/ theorem ext' {f g : Ξ± β†’. Ξ²} (H1 : βˆ€ a, a ∈ dom f ↔ a ∈ dom g) (H2 : βˆ€ a p q, f.fn a p = g.fn a q) : f = g := funext $ Ξ» a, roption.ext' (H1 a) (H2 a) theorem ext {f g : Ξ± β†’. Ξ²} (H : βˆ€ a b, b ∈ f a ↔ b ∈ g a) : f = g := funext $ Ξ» a, roption.ext (H a) /-- Turn a partial function into a function out of a subtype -/ def as_subtype (f : Ξ± β†’. Ξ²) (s : {x // f.dom x}) : Ξ² := f.fn s.1 s.2 def equiv_subtype : (Ξ± β†’. Ξ²) ≃ (Ξ£ p : Ξ± β†’ Prop, subtype p β†’ Ξ²) := ⟨λ f, ⟨f.dom, as_subtype f⟩, Ξ» ⟨p, f⟩ x, ⟨p x, Ξ» h, f ⟨x, h⟩⟩, Ξ» f, funext $ Ξ» a, roption.eta _, Ξ» ⟨p, f⟩, by dsimp; congr; funext a; cases a; refl⟩ theorem as_subtype_eq_of_mem {f : Ξ± β†’. Ξ²} {x : Ξ±} {y : Ξ²} (fxy : y ∈ f x) (domx : x ∈ f.dom) : f.as_subtype ⟨x, domx⟩ = y := roption.mem_unique (roption.get_mem _) fxy /-- Turn a total function into a partial function -/ protected def lift (f : Ξ± β†’ Ξ²) : Ξ± β†’. Ξ² := Ξ» a, roption.some (f a) instance : has_coe (Ξ± β†’ Ξ²) (Ξ± β†’. Ξ²) := ⟨pfun.lift⟩ @[simp] theorem lift_eq_coe (f : Ξ± β†’ Ξ²) : pfun.lift f = f := rfl @[simp] theorem coe_val (f : Ξ± β†’ Ξ²) (a : Ξ±) : (f : Ξ± β†’. Ξ²) a = roption.some (f a) := rfl /-- The graph of a partial function is the set of pairs `(x, f x)` where `x` is in the domain of `f`. -/ def graph (f : Ξ± β†’. Ξ²) : set (Ξ± Γ— Ξ²) := {p | p.2 ∈ f p.1} def graph' (f : Ξ± β†’. Ξ²) : rel Ξ± Ξ² := Ξ» x y, y ∈ f x /-- The range of a partial function is the set of values `f x` where `x` is in the domain of `f`. -/ def ran (f : Ξ± β†’. Ξ²) : set Ξ² := {b | βˆƒa, b ∈ f a} /-- Restrict a partial function to a smaller domain. -/ def restrict (f : Ξ± β†’. Ξ²) {p : set Ξ±} (H : p βŠ† f.dom) : Ξ± β†’. Ξ² := Ξ» x, roption.restrict (p x) (f x) (@H x) @[simp] theorem mem_restrict {f : Ξ± β†’. Ξ²} {s : set Ξ±} (h : s βŠ† f.dom) (a : Ξ±) (b : Ξ²) : b ∈ restrict f h a ↔ a ∈ s ∧ b ∈ f a := by { simp [restrict], reflexivity } def res (f : Ξ± β†’ Ξ²) (s : set Ξ±) : Ξ± β†’. Ξ² := restrict (pfun.lift f) (set.subset_univ s) theorem mem_res (f : Ξ± β†’ Ξ²) (s : set Ξ±) (a : Ξ±) (b : Ξ²) : b ∈ res f s a ↔ (a ∈ s ∧ f a = b) := by { simp [res], split; {intro h, simp [h]} } theorem res_univ (f : Ξ± β†’ Ξ²) : pfun.res f set.univ = f := rfl theorem dom_iff_graph (f : Ξ± β†’. Ξ²) (x : Ξ±) : x ∈ f.dom ↔ βˆƒy, (x, y) ∈ f.graph := roption.dom_iff_mem theorem lift_graph {f : Ξ± β†’ Ξ²} {a b} : (a, b) ∈ (f : Ξ± β†’. Ξ²).graph ↔ f a = b := show (βˆƒ (h : true), f a = b) ↔ f a = b, by simp /-- The monad `pure` function, the total constant `x` function -/ protected def pure (x : Ξ²) : Ξ± β†’. Ξ² := Ξ»_, roption.some x /-- The monad `bind` function, pointwise `roption.bind` -/ def bind (f : Ξ± β†’. Ξ²) (g : Ξ² β†’ Ξ± β†’. Ξ³) : Ξ± β†’. Ξ³ := Ξ»a, roption.bind (f a) (Ξ»b, g b a) /-- The monad `map` function, pointwise `roption.map` -/ def map (f : Ξ² β†’ Ξ³) (g : Ξ± β†’. Ξ²) : Ξ± β†’. Ξ³ := Ξ»a, roption.map f (g a) instance : monad (pfun Ξ±) := { pure := @pfun.pure _, bind := @pfun.bind _, map := @pfun.map _ } instance : is_lawful_monad (pfun Ξ±) := { bind_pure_comp_eq_map := Ξ» Ξ² Ξ³ f x, funext $ Ξ» a, roption.bind_some_eq_map _ _, id_map := Ξ» Ξ² f, by funext a; dsimp [functor.map, pfun.map]; cases f a; refl, pure_bind := Ξ» Ξ² Ξ³ x f, funext $ Ξ» a, roption.bind_some.{u_1 u_2} _ (f x), bind_assoc := Ξ» Ξ² Ξ³ Ξ΄ f g k, funext $ Ξ» a, roption.bind_assoc (f a) (Ξ» b, g b a) (Ξ» b, k b a) } theorem pure_defined (p : set Ξ±) (x : Ξ²) : p βŠ† (@pfun.pure Ξ± _ x).dom := set.subset_univ p theorem bind_defined {Ξ± Ξ² Ξ³} (p : set Ξ±) {f : Ξ± β†’. Ξ²} {g : Ξ² β†’ Ξ± β†’. Ξ³} (H1 : p βŠ† f.dom) (H2 : βˆ€x, p βŠ† (g x).dom) : p βŠ† (f >>= g).dom := Ξ»a ha, (⟨H1 ha, H2 _ ha⟩ : (f >>= g).dom a) def fix (f : Ξ± β†’. Ξ² βŠ• Ξ±) : Ξ± β†’. Ξ² := Ξ» a, roption.assert (acc (Ξ» x y, sum.inr x ∈ f y) a) $ Ξ» h, @well_founded.fix_F _ (Ξ» x y, sum.inr x ∈ f y) _ (Ξ» a IH, roption.assert (f a).dom $ Ξ» hf, by cases e : (f a).get hf with b a'; [exact roption.some b, exact IH _ ⟨hf, e⟩]) a h theorem dom_of_mem_fix {f : Ξ± β†’. Ξ² βŠ• Ξ±} {a : Ξ±} {b : Ξ²} (h : b ∈ fix f a) : (f a).dom := let ⟨h₁, hβ‚‚βŸ© := roption.mem_assert_iff.1 h in by rw well_founded.fix_F_eq at hβ‚‚; exact hβ‚‚.fst.fst theorem mem_fix_iff {f : Ξ± β†’. Ξ² βŠ• Ξ±} {a : Ξ±} {b : Ξ²} : b ∈ fix f a ↔ sum.inl b ∈ f a ∨ βˆƒ a', sum.inr a' ∈ f a ∧ b ∈ fix f a' := ⟨λ h, let ⟨h₁, hβ‚‚βŸ© := roption.mem_assert_iff.1 h in begin rw well_founded.fix_F_eq at hβ‚‚, simp at hβ‚‚, cases hβ‚‚ with hβ‚‚ h₃, cases e : (f a).get hβ‚‚ with b' a'; simp [e] at h₃, { subst b', refine or.inl ⟨hβ‚‚, e⟩ }, { exact or.inr ⟨a', ⟨_, e⟩, roption.mem_assert _ hβ‚ƒβŸ© } end, Ξ» h, begin simp [fix], rcases h with ⟨h₁, hβ‚‚βŸ© | ⟨a', h, hβ‚ƒβŸ©, { refine ⟨⟨_, Ξ» y h', _⟩, _⟩, { injection roption.mem_unique ⟨h₁, hβ‚‚βŸ© h' }, { rw well_founded.fix_F_eq, simp [h₁, hβ‚‚] } }, { simp [fix] at h₃, cases h₃ with h₃ hβ‚„, refine ⟨⟨_, Ξ» y h', _⟩, _⟩, { injection roption.mem_unique h h' with e, exact e β–Έ h₃ }, { cases h with h₁ hβ‚‚, rw well_founded.fix_F_eq, simp [h₁, hβ‚‚, hβ‚„] } } end⟩ @[elab_as_eliminator] def fix_induction {f : Ξ± β†’. Ξ² βŠ• Ξ±} {b : Ξ²} {C : Ξ± β†’ Sort*} {a : Ξ±} (h : b ∈ fix f a) (H : βˆ€ a, b ∈ fix f a β†’ (βˆ€ a', b ∈ fix f a' β†’ sum.inr a' ∈ f a β†’ C a') β†’ C a) : C a := begin replace h := roption.mem_assert_iff.1 h, have := h.snd, revert this, induction h.fst with a ha IH, intro hβ‚‚, refine H a (roption.mem_assert_iff.2 ⟨⟨_, ha⟩, hβ‚‚βŸ©) (Ξ» a' ha' fa', _), have := (roption.mem_assert_iff.1 ha').snd, exact IH _ fa' ⟨ha _ fa', this⟩ this end end pfun namespace pfun variables {Ξ± : Type*} {Ξ² : Type*} (f : Ξ± β†’. Ξ²) def image (s : set Ξ±) : set Ξ² := rel.image f.graph' s lemma image_def (s : set Ξ±) : image f s = {y | βˆƒ x ∈ s, y ∈ f x} := rfl lemma mem_image (y : Ξ²) (s : set Ξ±) : y ∈ image f s ↔ βˆƒ x ∈ s, y ∈ f x := iff.rfl lemma image_mono {s t : set Ξ±} (h : s βŠ† t) : f.image s βŠ† f.image t := rel.image_mono _ h lemma image_inter (s t : set Ξ±) : f.image (s ∩ t) βŠ† f.image s ∩ f.image t := rel.image_inter _ s t lemma image_union (s t : set Ξ±) : f.image (s βˆͺ t) = f.image s βˆͺ f.image t := rel.image_union _ s t def preimage (s : set Ξ²) : set Ξ± := rel.preimage (Ξ» x y, y ∈ f x) s lemma preimage_def (s : set Ξ²) : preimage f s = {x | βˆƒ y ∈ s, y ∈ f x} := rfl lemma mem_preimage (s : set Ξ²) (x : Ξ±) : x ∈ preimage f s ↔ βˆƒ y ∈ s, y ∈ f x := iff.rfl lemma preimage_subset_dom (s : set Ξ²) : f.preimage s βŠ† f.dom := assume x ⟨y, ys, fxy⟩, roption.dom_iff_mem.mpr ⟨y, fxy⟩ lemma preimage_mono {s t : set Ξ²} (h : s βŠ† t) : f.preimage s βŠ† f.preimage t := rel.preimage_mono _ h lemma preimage_inter (s t : set Ξ²) : f.preimage (s ∩ t) βŠ† f.preimage s ∩ f.preimage t := rel.preimage_inter _ s t lemma preimage_union (s t : set Ξ²) : f.preimage (s βˆͺ t) = f.preimage s βˆͺ f.preimage t := rel.preimage_union _ s t lemma preimage_univ : f.preimage set.univ = f.dom := by ext; simp [mem_preimage, mem_dom] def core (s : set Ξ²) : set Ξ± := rel.core f.graph' s lemma core_def (s : set Ξ²) : core f s = {x | βˆ€ y, y ∈ f x β†’ y ∈ s} := rfl lemma mem_core (x : Ξ±) (s : set Ξ²) : x ∈ core f s ↔ (βˆ€ y, y ∈ f x β†’ y ∈ s) := iff.rfl lemma compl_dom_subset_core (s : set Ξ²) : -f.dom βŠ† f.core s := assume x hx y fxy, absurd ((mem_dom f x).mpr ⟨y, fxy⟩) hx lemma core_mono {s t : set Ξ²} (h : s βŠ† t) : f.core s βŠ† f.core t := rel.core_mono _ h lemma core_inter (s t : set Ξ²) : f.core (s ∩ t) = f.core s ∩ f.core t := rel.core_inter _ s t lemma mem_core_res (f : Ξ± β†’ Ξ²) (s : set Ξ±) (t : set Ξ²) (x : Ξ±) : x ∈ core (res f s) t ↔ (x ∈ s β†’ f x ∈ t) := begin simp [mem_core, mem_res], split, { intros h h', apply h _ h', reflexivity }, intros h y xs fxeq, rw ←fxeq, exact h xs end section open_locale classical lemma core_res (f : Ξ± β†’ Ξ²) (s : set Ξ±) (t : set Ξ²) : core (res f s) t = -s βˆͺ f ⁻¹' t := by { ext, rw mem_core_res, by_cases h : x ∈ s; simp [h] } end lemma core_restrict (f : Ξ± β†’ Ξ²) (s : set Ξ²) : core (f : Ξ± β†’. Ξ²) s = set.preimage f s := by ext x; simp [core_def] lemma preimage_subset_core (f : Ξ± β†’. Ξ²) (s : set Ξ²) : f.preimage s βŠ† f.core s := assume x ⟨y, ys, fxy⟩ y' fxy', have y = y', from roption.mem_unique fxy fxy', this β–Έ ys lemma preimage_eq (f : Ξ± β†’. Ξ²) (s : set Ξ²) : f.preimage s = f.core s ∩ f.dom := set.eq_of_subset_of_subset (set.subset_inter (preimage_subset_core f s) (preimage_subset_dom f s)) (assume x ⟨xcore, xdom⟩, let y := (f x).get xdom in have ys : y ∈ s, from xcore _ (roption.get_mem _), show x ∈ preimage f s, from ⟨(f x).get xdom, ys, roption.get_mem _⟩) lemma core_eq (f : Ξ± β†’. Ξ²) (s : set Ξ²) : f.core s = f.preimage s βˆͺ -f.dom := by rw [preimage_eq, set.union_distrib_right, set.union_comm (dom f), set.compl_union_self, set.inter_univ, set.union_eq_self_of_subset_right (compl_dom_subset_core f s)] lemma preimage_as_subtype (f : Ξ± β†’. Ξ²) (s : set Ξ²) : f.as_subtype ⁻¹' s = subtype.val ⁻¹' pfun.preimage f s := begin ext x, simp only [set.mem_preimage, set.mem_set_of_eq, pfun.as_subtype, pfun.mem_preimage], show pfun.fn f (x.val) _ ∈ s ↔ βˆƒ y ∈ s, y ∈ f (x.val), exact iff.intro (assume h, ⟨_, h, roption.get_mem _⟩) (assume ⟨y, ys, fxy⟩, have f.fn x.val x.property ∈ f x.val := roption.get_mem _, roption.mem_unique fxy this β–Έ ys) end end pfun
163d1c0294f6c6564ff5b67e5ef986fba7d71ace
d9ed0fce1c218297bcba93e046cb4e79c83c3af8
/library/tools/super/resolution.lean
a12a1dd7e1fce72e8a4058bad1ab92505cbc713a
[ "Apache-2.0" ]
permissive
leodemoura/lean_clone
005c63aa892a6492f2d4741ee3c2cb07a6be9d7f
cc077554b584d39bab55c360bc12a6fe7957afe6
refs/heads/master
1,610,506,475,484
1,482,348,354,000
1,482,348,543,000
77,091,586
0
0
null
null
null
null
UTF-8
Lean
false
false
2,298
lean
/- Copyright (c) 2016 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .clause .prover_state .utils open tactic monad namespace super variable gt : expr β†’ expr β†’ bool variables (ac1 ac2 : derived_clause) variables (c1 c2 : clause) variables (i1 i2 : nat) -- c1 : ... β†’ Β¬a β†’ ... -- c2 : ... β†’ a β†’ ... meta def try_resolve : tactic clause := do qf1 ← c1^.open_metan c1^.num_quants, qf2 ← c2^.open_metan c2^.num_quants, -- FIXME: using default transparency unifies m*n with (x*y*z)*w here ??? unify_core transparency.reducible (qf1.1^.get_lit i1)^.formula (qf2.1^.get_lit i2)^.formula, qf1i ← qf1.1^.inst_mvars, guard $ clause.is_maximal gt qf1i i1, op1 ← qf1.1^.open_constn i1, op2 ← qf2.1^.open_constn c2^.num_lits, a_in_op2 ← (op2.2^.nth i2)^.to_monad, clause.meta_closure (qf1.2 ++ qf2.2) $ (op1.1^.inst (op2.1^.close_const a_in_op2)^.proof)^.close_constn (op1.2 ++ op2.2^.remove_nth i2) meta def try_add_resolvent : prover unit := do c' ← β™― try_resolve gt ac1^.c ac2^.c i1 i2, inf_score 1 [ac1^.sc, ac2^.sc] >>= mk_derived c' >>= add_inferred meta def maybe_add_resolvent : prover unit := try_add_resolvent gt ac1 ac2 i1 i2 <|> return () meta def resolution_left_inf : inference := take given, do active ← get_active, sequence' $ do given_i ← given^.selected, guard $ clause.literal.is_neg (given^.c^.get_lit given_i), other ← rb_map.values active, guard $ Β¬given^.sc^.in_sos ∨ Β¬other^.sc^.in_sos, other_i ← other^.selected, guard $ clause.literal.is_pos (other^.c^.get_lit other_i), [maybe_add_resolvent gt other given other_i given_i] meta def resolution_right_inf : inference := take given, do active ← get_active, sequence' $ do given_i ← given^.selected, guard $ clause.literal.is_pos (given^.c^.get_lit given_i), other ← rb_map.values active, guard $ Β¬given^.sc^.in_sos ∨ Β¬other^.sc^.in_sos, other_i ← other^.selected, guard $ clause.literal.is_neg (other^.c^.get_lit other_i), [maybe_add_resolvent gt given other given_i other_i] @[super.inf] meta def resolution_inf : inf_decl := inf_decl.mk 40 $ take given, do gt ← get_term_order, resolution_left_inf gt given >> resolution_right_inf gt given end super
e97b4ac79e3713beb515653d9f0c8cde7d817e41
7cef822f3b952965621309e88eadf618da0c8ae9
/src/data/polynomial.lean
38d4b105924abda1ab4d3341dd9d0ca9a8414201
[ "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
103,286
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, Jens Wagemaker Theory of univariate polynomials, represented as `β„• β†’β‚€ Ξ±`, where Ξ± is a commutative semiring. -/ import data.finsupp algebra.gcd_domain ring_theory.euclidean_domain tactic.ring_exp ring_theory.multiplicity noncomputable theory local attribute [instance, priority 100] classical.prop_decidable /-- `polynomial Ξ±` is the type of univariate polynomials over `Ξ±`. Polynomials should be seen as (semi-)rings with the additional constructor `X`. The embedding from Ξ± is called `C`. -/ def polynomial (Ξ± : Type*) [comm_semiring Ξ±] := β„• β†’β‚€ Ξ± open finsupp finset lattice namespace polynomial universes u v variables {Ξ± : Type u} {Ξ² : Type v} {a b : Ξ±} {m n : β„•} section comm_semiring variables [comm_semiring Ξ±] {p q r : polynomial Ξ±} instance : has_zero (polynomial Ξ±) := finsupp.has_zero instance : has_one (polynomial Ξ±) := finsupp.has_one instance : has_add (polynomial Ξ±) := finsupp.has_add instance : has_mul (polynomial Ξ±) := finsupp.has_mul instance : comm_semiring (polynomial Ξ±) := finsupp.comm_semiring def coeff_coe_to_fun : has_coe_to_fun (polynomial Ξ±) := finsupp.has_coe_to_fun local attribute [instance] finsupp.comm_semiring coeff_coe_to_fun @[simp] lemma support_zero : (0 : polynomial Ξ±).support = βˆ… := rfl /-- `C a` is the constant polynomial `a`. -/ def C (a : Ξ±) : polynomial Ξ± := single 0 a /-- `X` is the polynomial variable (aka indeterminant). -/ def X : polynomial Ξ± := single 1 1 /-- coeff p n is the coefficient of X^n in p -/ def coeff (p : polynomial Ξ±) := p.to_fun @[simp] lemma coeff_mk (s) (f) (h) : coeff (finsupp.mk s f h : polynomial Ξ±) = f := rfl instance [has_repr Ξ±] : has_repr (polynomial Ξ±) := ⟨λ p, if p = 0 then "0" else (p.support.sort (≀)).foldr (Ξ» n a, a ++ (if a = "" then "" else " + ") ++ if n = 0 then "C (" ++ repr (coeff p n) ++ ")" else if n = 1 then if (coeff p n) = 1 then "X" else "C (" ++ repr (coeff p n) ++ ") * X" else if (coeff p n) = 1 then "X ^ " ++ repr n else "C (" ++ repr (coeff p n) ++ ") * X ^ " ++ repr n) ""⟩ theorem ext_iff {p q : polynomial Ξ±} : p = q ↔ βˆ€ n, coeff p n = coeff q n := ⟨λ h n, h β–Έ rfl, finsupp.ext⟩ @[ext] lemma ext {p q : polynomial Ξ±} : (βˆ€ n, coeff p n = coeff q n) β†’ p = q := (@ext_iff _ _ p q).2 /-- `degree p` is the degree of the polynomial `p`, i.e. the largest `X`-exponent in `p`. `degree p = some n` when `p β‰  0` and `n` is the highest power of `X` that appears in `p`, otherwise `degree 0 = βŠ₯`. -/ def degree (p : polynomial Ξ±) : with_bot β„• := p.support.sup some lemma degree_lt_wf : well_founded (Ξ»p q : polynomial Ξ±, degree p < degree q) := inv_image.wf degree (with_bot.well_founded_lt nat.lt_wf) instance : has_well_founded (polynomial Ξ±) := ⟨_, degree_lt_wf⟩ /-- `nat_degree p` forces `degree p` to β„•, by defining nat_degree 0 = 0. -/ def nat_degree (p : polynomial Ξ±) : β„• := (degree p).get_or_else 0 lemma single_eq_C_mul_X : βˆ€{n}, single n a = C a * X^n | 0 := (mul_one _).symm | (n+1) := calc single (n + 1) a = single n a * X : by rw [X, single_mul_single, mul_one] ... = (C a * X^n) * X : by rw [single_eq_C_mul_X] ... = C a * X^(n+1) : by simp only [pow_add, mul_assoc, pow_one] lemma sum_C_mul_X_eq (p : polynomial Ξ±) : p.sum (Ξ»n a, C a * X^n) = p := eq.trans (sum_congr rfl $ assume n hn, single_eq_C_mul_X.symm) (finsupp.sum_single _) @[elab_as_eliminator] protected lemma induction_on {M : polynomial Ξ± β†’ Prop} (p : polynomial Ξ±) (h_C : βˆ€a, M (C a)) (h_add : βˆ€p q, M p β†’ M q β†’ M (p + q)) (h_monomial : βˆ€(n : β„•) (a : Ξ±), M (C a * X^n) β†’ M (C a * X^(n+1))) : M p := have βˆ€{n:β„•} {a}, M (C a * X^n), begin assume n a, induction n with n ih, { simp only [pow_zero, mul_one, h_C] }, { exact h_monomial _ _ ih } end, finsupp.induction p (suffices M (C 0), by simpa only [C, single_zero], h_C 0) (assume n a p _ _ hp, suffices M (C a * X^n + p), by rwa [single_eq_C_mul_X], h_add _ _ this hp) @[simp] lemma C_0 : C (0 : Ξ±) = 0 := single_zero @[simp] lemma C_1 : C (1 : Ξ±) = 1 := rfl @[simp] lemma C_mul : C (a * b) = C a * C b := (@single_mul_single _ _ _ _ 0 0 a b).symm @[simp] lemma C_add : C (a + b) = C a + C b := finsupp.single_add instance C.is_semiring_hom : is_semiring_hom (C : Ξ± β†’ polynomial Ξ±) := ⟨C_0, C_1, Ξ» _ _, C_add, Ξ» _ _, C_mul⟩ @[simp] lemma C_pow : C (a ^ n) = C a ^ n := is_semiring_hom.map_pow _ _ _ lemma nat_cast_eq_C (n : β„•) : (n : polynomial Ξ±) = C n := by refine (nat.eq_cast (Ξ» n, C (n : Ξ±)) _ _ _ n).symm; simp section coeff lemma apply_eq_coeff : p n = coeff p n := rfl @[simp] lemma coeff_zero (n : β„•) : coeff (0 : polynomial Ξ±) n = 0 := rfl lemma coeff_single : coeff (single n a) m = if n = m then a else 0 := by { dsimp [single], congr } @[simp] lemma coeff_one_zero : coeff (1 : polynomial Ξ±) 0 = 1 := coeff_single @[simp] lemma coeff_add (p q : polynomial Ξ±) (n : β„•) : coeff (p + q) n = coeff p n + coeff q n := rfl instance coeff.is_add_monoid_hom {n : β„•} : is_add_monoid_hom (Ξ» p : polynomial Ξ±, p.coeff n) := { map_add := Ξ» p q, coeff_add p q n, map_zero := coeff_zero _ } lemma coeff_C : coeff (C a) n = ite (n = 0) a 0 := by simp [coeff, eq_comm, C, single]; congr @[simp] lemma coeff_C_zero : coeff (C a) 0 = a := coeff_single @[simp] lemma coeff_X_one : coeff (X : polynomial Ξ±) 1 = 1 := coeff_single @[simp] lemma coeff_X_zero : coeff (X : polynomial Ξ±) 0 = 0 := coeff_single lemma coeff_X : coeff (X : polynomial Ξ±) n = if 1 = n then 1 else 0 := coeff_single @[simp] lemma coeff_C_mul_X (x : Ξ±) (k n : β„•) : coeff (C x * X^k : polynomial Ξ±) n = if n = k then x else 0 := by rw [← single_eq_C_mul_X]; simp [single, eq_comm, coeff]; congr lemma coeff_sum [comm_semiring Ξ²] (n : β„•) (f : β„• β†’ Ξ± β†’ polynomial Ξ²) : coeff (p.sum f) n = p.sum (Ξ» a b, coeff (f a b) n) := finsupp.sum_apply @[simp] lemma coeff_C_mul (p : polynomial Ξ±) : coeff (C a * p) n = a * coeff p n := begin conv in (a * _) { rw [← @sum_single _ _ _ p, coeff_sum] }, rw [mul_def, C, sum_single_index], { simp [coeff_single, finsupp.mul_sum, coeff_sum], apply sum_congr rfl, assume i hi, by_cases i = n; simp [h] }, simp end @[simp] lemma coeff_one (n : β„•) : coeff (1 : polynomial Ξ±) n = if 0 = n then 1 else 0 := coeff_single @[simp] lemma coeff_X_pow (k n : β„•) : coeff (X^k : polynomial Ξ±) n = if n = k then 1 else 0 := by simpa only [C_1, one_mul] using coeff_C_mul_X (1:Ξ±) k n lemma coeff_mul (p q : polynomial Ξ±) (n : β„•) : coeff (p * q) n = (nat.antidiagonal n).sum (Ξ» x, coeff p x.1 * coeff q x.2) := have hite : βˆ€ a : β„• Γ— β„•, ite (a.1 + a.2 = n) (coeff p (a.fst) * coeff q (a.snd)) 0 β‰  0 β†’ a.1 + a.2 = n, from Ξ» a ha, by_contradiction (Ξ» h, absurd (eq.refl (0 : Ξ±)) (by rwa if_neg h at ha)), calc coeff (p * q) n = sum (p.support) (Ξ» a, sum (q.support) (Ξ» b, ite (a + b = n) (coeff p a * coeff q b) 0)) : by simp only [finsupp.mul_def, coeff_sum, coeff_single]; refl ... = (p.support.product q.support).sum (Ξ» v : β„• Γ— β„•, ite (v.1 + v.2 = n) (coeff p v.1 * coeff q v.2) 0) : by rw sum_product ... = (nat.antidiagonal n).sum (Ξ» x, coeff p x.1 * coeff q x.2) : begin refine sum_bij_ne_zero (Ξ» x _ _, x) (Ξ» x _ hx, nat.mem_antidiagonal.2 (hite x hx)) (Ξ» _ _ _ _ _ _ h, h) (Ξ» x h₁ hβ‚‚, ⟨x, _, _, rfl⟩) _, { rw [mem_product, mem_support_iff, mem_support_iff], exact ⟨ne_zero_of_mul_ne_zero_right hβ‚‚, ne_zero_of_mul_ne_zero_left hβ‚‚βŸ© }, { rw nat.mem_antidiagonal at h₁, rwa [if_pos h₁] }, { intros x h hx, rw [if_pos (hite x hx)] } end theorem coeff_mul_X_pow (p : polynomial Ξ±) (n d : β„•) : coeff (p * polynomial.X ^ n) (d + n) = coeff p d := begin rw [coeff_mul, sum_eq_single (d,n), coeff_X_pow, if_pos rfl, mul_one], { rintros ⟨i,j⟩ h1 h2, rw [coeff_X_pow, if_neg, mul_zero], rintro rfl, apply h2, rw [nat.mem_antidiagonal, add_right_cancel_iff] at h1, subst h1 }, { exact Ξ» h1, (h1 (nat.mem_antidiagonal.2 rfl)).elim } end theorem coeff_mul_X (p : polynomial Ξ±) (n : β„•) : coeff (p * X) (n + 1) = coeff p n := by simpa only [pow_one] using coeff_mul_X_pow p 1 n theorem mul_X_pow_eq_zero {p : polynomial Ξ±} {n : β„•} (H : p * X ^ n = 0) : p = 0 := ext $ Ξ» k, (coeff_mul_X_pow p n k).symm.trans $ ext_iff.1 H (k+n) end coeff lemma C_inj : C a = C b ↔ a = b := ⟨λ h, coeff_C_zero.symm.trans (h.symm β–Έ coeff_C_zero), congr_arg C⟩ section evalβ‚‚ variables [semiring Ξ²] variables (f : Ξ± β†’ Ξ²) (x : Ξ²) open is_semiring_hom /-- Evaluate a polynomial `p` given a ring hom `f` from the scalar ring to the target and a value `x` for the variable in the target -/ def evalβ‚‚ (p : polynomial Ξ±) : Ξ² := p.sum (Ξ» e a, f a * x ^ e) variables [is_semiring_hom f] @[simp] lemma evalβ‚‚_C : (C a).evalβ‚‚ f x = f a := (sum_single_index $ by rw [map_zero f, zero_mul]).trans $ by rw [pow_zero, mul_one] @[simp] lemma evalβ‚‚_X : X.evalβ‚‚ f x = x := (sum_single_index $ by rw [map_zero f, zero_mul]).trans $ by rw [map_one f, one_mul, pow_one] @[simp] lemma evalβ‚‚_zero : (0 : polynomial Ξ±).evalβ‚‚ f x = 0 := finsupp.sum_zero_index @[simp] lemma evalβ‚‚_add : (p + q).evalβ‚‚ f x = p.evalβ‚‚ f x + q.evalβ‚‚ f x := finsupp.sum_add_index (Ξ» _, by rw [map_zero f, zero_mul]) (Ξ» _ _ _, by rw [map_add f, add_mul]) @[simp] lemma evalβ‚‚_one : (1 : polynomial Ξ±).evalβ‚‚ f x = 1 := by rw [← C_1, evalβ‚‚_C, map_one f] instance evalβ‚‚.is_add_monoid_hom : is_add_monoid_hom (evalβ‚‚ f x) := { map_zero := evalβ‚‚_zero _ _, map_add := Ξ» _ _, evalβ‚‚_add _ _ } end evalβ‚‚ section evalβ‚‚ variables [comm_semiring Ξ²] variables (f : Ξ± β†’ Ξ²) [is_semiring_hom f] (x : Ξ²) open is_semiring_hom @[simp] lemma evalβ‚‚_mul : (p * q).evalβ‚‚ f x = p.evalβ‚‚ f x * q.evalβ‚‚ f x := begin dunfold evalβ‚‚, rw [mul_def, finsupp.sum_mul _ p], simp only [finsupp.mul_sum _ q], rw [sum_sum_index], { apply sum_congr rfl, assume i hi, dsimp only, rw [sum_sum_index], { apply sum_congr rfl, assume j hj, dsimp only, rw [sum_single_index, map_mul f, pow_add], { simp only [mul_assoc, mul_left_comm] }, { rw [map_zero f, zero_mul] } }, { intro, rw [map_zero f, zero_mul] }, { intros, rw [map_add f, add_mul] } }, { intro, rw [map_zero f, zero_mul] }, { intros, rw [map_add f, add_mul] } end instance evalβ‚‚.is_semiring_hom : is_semiring_hom (evalβ‚‚ f x) := ⟨evalβ‚‚_zero _ _, evalβ‚‚_one _ _, Ξ» _ _, evalβ‚‚_add _ _, Ξ» _ _, evalβ‚‚_mul _ _⟩ lemma evalβ‚‚_pow (n : β„•) : (p ^ n).evalβ‚‚ f x = p.evalβ‚‚ f x ^ n := map_pow _ _ _ lemma evalβ‚‚_sum (p : polynomial Ξ±) (g : β„• β†’ Ξ± β†’ polynomial Ξ±) (x : Ξ²) : (p.sum g).evalβ‚‚ f x = p.sum (Ξ» n a, (g n a).evalβ‚‚ f x) := finsupp.sum_sum_index (by simp [is_add_monoid_hom.map_zero f]) (by intros; simp [right_distrib, is_add_monoid_hom.map_add f]) end evalβ‚‚ section eval variable {x : Ξ±} /-- `eval x p` is the evaluation of the polynomial `p` at `x` -/ def eval : Ξ± β†’ polynomial Ξ± β†’ Ξ± := evalβ‚‚ id @[simp] lemma eval_C : (C a).eval x = a := evalβ‚‚_C _ _ @[simp] lemma eval_X : X.eval x = x := evalβ‚‚_X _ _ @[simp] lemma eval_zero : (0 : polynomial Ξ±).eval x = 0 := evalβ‚‚_zero _ _ @[simp] lemma eval_add : (p + q).eval x = p.eval x + q.eval x := evalβ‚‚_add _ _ @[simp] lemma eval_one : (1 : polynomial Ξ±).eval x = 1 := evalβ‚‚_one _ _ @[simp] lemma eval_mul : (p * q).eval x = p.eval x * q.eval x := evalβ‚‚_mul _ _ instance eval.is_semiring_hom : is_semiring_hom (eval x) := evalβ‚‚.is_semiring_hom _ _ @[simp] lemma eval_pow (n : β„•) : (p ^ n).eval x = p.eval x ^ n := evalβ‚‚_pow _ _ _ lemma eval_sum (p : polynomial Ξ±) (f : β„• β†’ Ξ± β†’ polynomial Ξ±) (x : Ξ±) : (p.sum f).eval x = p.sum (Ξ» n a, (f n a).eval x) := evalβ‚‚_sum _ _ _ _ lemma evalβ‚‚_hom [comm_semiring Ξ²] (f : Ξ± β†’ Ξ²) [is_semiring_hom f] (x : Ξ±) : p.evalβ‚‚ f (f x) = f (p.eval x) := polynomial.induction_on p (by simp) (by simp [is_semiring_hom.map_add f] {contextual := tt}) (by simp [is_semiring_hom.map_mul f, eval_pow, is_semiring_hom.map_pow f, pow_succ', (mul_assoc _ _ _).symm] {contextual := tt}) /-- `is_root p x` implies `x` is a root of `p`. The evaluation of `p` at `x` is zero -/ def is_root (p : polynomial Ξ±) (a : Ξ±) : Prop := p.eval a = 0 instance [decidable_eq Ξ±] : decidable (is_root p a) := by unfold is_root; apply_instance @[simp] lemma is_root.def : is_root p a ↔ p.eval a = 0 := iff.rfl lemma root_mul_left_of_is_root (p : polynomial Ξ±) {q : polynomial Ξ±} : is_root q a β†’ is_root (p * q) a := Ξ» H, by rw [is_root, eval_mul, is_root.def.1 H, mul_zero] lemma root_mul_right_of_is_root {p : polynomial Ξ±} (q : polynomial Ξ±) : is_root p a β†’ is_root (p * q) a := Ξ» H, by rw [is_root, eval_mul, is_root.def.1 H, zero_mul] lemma coeff_zero_eq_eval_zero (p : polynomial Ξ±) : coeff p 0 = p.eval 0 := calc coeff p 0 = coeff p 0 * 0 ^ 0 : by simp ... = p.eval 0 : eq.symm $ finset.sum_eq_single _ (Ξ» b _ hb, by simp [zero_pow (nat.pos_of_ne_zero hb)]) (by simp) lemma zero_is_root_of_coeff_zero_eq_zero {p : polynomial Ξ±} (hp : p.coeff 0 = 0) : is_root p 0 := by rwa coeff_zero_eq_eval_zero at hp end eval section comp def comp (p q : polynomial Ξ±) : polynomial Ξ± := p.evalβ‚‚ C q lemma evalβ‚‚_comp [comm_semiring Ξ²] (f : Ξ± β†’ Ξ²) [is_semiring_hom f] {x : Ξ²} : (p.comp q).evalβ‚‚ f x = p.evalβ‚‚ f (q.evalβ‚‚ f x) := show (p.sum (Ξ» e a, C a * q ^ e)).evalβ‚‚ f x = p.evalβ‚‚ f (evalβ‚‚ f x q), by simp only [evalβ‚‚_mul, evalβ‚‚_C, evalβ‚‚_pow, evalβ‚‚_sum]; refl lemma eval_comp : (p.comp q).eval a = p.eval (q.eval a) := evalβ‚‚_comp _ @[simp] lemma comp_X : p.comp X = p := begin refine ext (Ξ» n, _), rw [comp, evalβ‚‚], conv in (C _ * _) { rw ← single_eq_C_mul_X }, rw finsupp.sum_single end @[simp] lemma X_comp : X.comp p = p := evalβ‚‚_X _ _ @[simp] lemma comp_C : p.comp (C a) = C (p.eval a) := begin dsimp [comp, evalβ‚‚, eval, finsupp.sum], rw [← sum_hom (@C Ξ± _)], apply finset.sum_congr rfl; simp end @[simp] lemma C_comp : (C a).comp p = C a := evalβ‚‚_C _ _ @[simp] lemma comp_zero : p.comp (0 : polynomial Ξ±) = C (p.eval 0) := by rw [← C_0, comp_C] @[simp] lemma zero_comp : comp (0 : polynomial Ξ±) p = 0 := by rw [← C_0, C_comp] @[simp] lemma comp_one : p.comp 1 = C (p.eval 1) := by rw [← C_1, comp_C] @[simp] lemma one_comp : comp (1 : polynomial Ξ±) p = 1 := by rw [← C_1, C_comp] instance : is_semiring_hom (Ξ» q : polynomial Ξ±, q.comp p) := by unfold comp; apply_instance @[simp] lemma add_comp : (p + q).comp r = p.comp r + q.comp r := evalβ‚‚_add _ _ @[simp] lemma mul_comp : (p * q).comp r = p.comp r * q.comp r := evalβ‚‚_mul _ _ end comp /-- `leading_coeff p` gives the coefficient of the highest power of `X` in `p`-/ def leading_coeff (p : polynomial Ξ±) : Ξ± := coeff p (nat_degree p) /-- a polynomial is `monic` if its leading coefficient is 1 -/ def monic (p : polynomial Ξ±) := leading_coeff p = (1 : Ξ±) lemma monic.def : monic p ↔ leading_coeff p = 1 := iff.rfl instance monic.decidable [decidable_eq Ξ±] : decidable (monic p) := by unfold monic; apply_instance @[simp] lemma monic.leading_coeff {p : polynomial Ξ±} (hp : p.monic) : leading_coeff p = 1 := hp @[simp] lemma degree_zero : degree (0 : polynomial Ξ±) = βŠ₯ := rfl @[simp] lemma nat_degree_zero : nat_degree (0 : polynomial Ξ±) = 0 := rfl @[simp] lemma degree_C (ha : a β‰  0) : degree (C a) = (0 : with_bot β„•) := show sup (ite (a = 0) βˆ… {0}) some = 0, by rw if_neg ha; refl lemma degree_C_le : degree (C a) ≀ (0 : with_bot β„•) := by by_cases h : a = 0; [rw [h, C_0], rw [degree_C h]]; [exact bot_le, exact le_refl _] lemma degree_one_le : degree (1 : polynomial Ξ±) ≀ (0 : with_bot β„•) := by rw [← C_1]; exact degree_C_le lemma degree_eq_bot : degree p = βŠ₯ ↔ p = 0 := ⟨λ h, by rw [degree, ← max_eq_sup_with_bot] at h; exact support_eq_empty.1 (max_eq_none.1 h), Ξ» h, h.symm β–Έ rfl⟩ lemma degree_eq_nat_degree (hp : p β‰  0) : degree p = (nat_degree p : with_bot β„•) := let ⟨n, hn⟩ := classical.not_forall.1 (mt option.eq_none_iff_forall_not_mem.2 (mt degree_eq_bot.1 hp)) in have hn : degree p = some n := not_not.1 hn, by rw [nat_degree, hn]; refl lemma degree_eq_iff_nat_degree_eq {p : polynomial Ξ±} {n : β„•} (hp : p β‰  0) : p.degree = n ↔ p.nat_degree = n := by rw [degree_eq_nat_degree hp, with_bot.coe_eq_coe] lemma degree_eq_iff_nat_degree_eq_of_pos {p : polynomial Ξ±} {n : β„•} (hn : n > 0) : p.degree = n ↔ p.nat_degree = n := begin split, { intro H, rwa ← degree_eq_iff_nat_degree_eq, rintro rfl, rw degree_zero at H, exact option.no_confusion H }, { intro H, rwa degree_eq_iff_nat_degree_eq, rintro rfl, rw nat_degree_zero at H, rw H at hn, exact lt_irrefl _ hn } end lemma nat_degree_eq_of_degree_eq_some {p : polynomial Ξ±} {n : β„•} (h : degree p = n) : nat_degree p = n := have hp0 : p β‰  0, from Ξ» hp0, by rw hp0 at h; exact option.no_confusion h, option.some_inj.1 $ show (nat_degree p : with_bot β„•) = n, by rwa [← degree_eq_nat_degree hp0] @[simp] lemma degree_le_nat_degree : degree p ≀ nat_degree p := begin by_cases hp : p = 0, { rw hp, exact bot_le }, rw [degree_eq_nat_degree hp], exact le_refl _ end lemma nat_degree_eq_of_degree_eq [comm_semiring Ξ²] {q : polynomial Ξ²} (h : degree p = degree q) : nat_degree p = nat_degree q := by unfold nat_degree; rw h lemma le_degree_of_ne_zero (h : coeff p n β‰  0) : (n : with_bot β„•) ≀ degree p := show @has_le.le (with_bot β„•) _ (some n : with_bot β„•) (p.support.sup some : with_bot β„•), from finset.le_sup (finsupp.mem_support_iff.2 h) lemma le_nat_degree_of_ne_zero (h : coeff p n β‰  0) : n ≀ nat_degree p := begin rw [← with_bot.coe_le_coe, ← degree_eq_nat_degree], exact le_degree_of_ne_zero h, { assume h, subst h, exact h rfl } end lemma degree_le_degree (h : coeff q (nat_degree p) β‰  0) : degree p ≀ degree q := begin by_cases hp : p = 0, { rw hp, exact bot_le }, { rw degree_eq_nat_degree hp, exact le_degree_of_ne_zero h } end @[simp] lemma nat_degree_C (a : Ξ±) : nat_degree (C a) = 0 := begin by_cases ha : a = 0, { have : C a = 0, { rw [ha, C_0] }, rw [nat_degree, degree_eq_bot.2 this], refl }, { rw [nat_degree, degree_C ha], refl } end @[simp] lemma nat_degree_one : nat_degree (1 : polynomial Ξ±) = 0 := nat_degree_C 1 @[simp] lemma nat_degree_nat_cast (n : β„•) : nat_degree (n : polynomial Ξ±) = 0 := by simp [nat_cast_eq_C] @[simp] lemma degree_monomial (n : β„•) (ha : a β‰  0) : degree (C a * X ^ n) = n := by rw [← single_eq_C_mul_X, degree, support_single_ne_zero ha]; refl lemma degree_monomial_le (n : β„•) (a : Ξ±) : degree (C a * X ^ n) ≀ n := if h : a = 0 then by rw [h, C_0, zero_mul]; exact bot_le else le_of_eq (degree_monomial n h) lemma coeff_eq_zero_of_degree_lt (h : degree p < n) : coeff p n = 0 := not_not.1 (mt le_degree_of_ne_zero (not_le_of_gt h)) lemma coeff_eq_zero_of_nat_degree_lt {p : polynomial Ξ±} {n : β„•} (h : p.nat_degree < n) : p.coeff n = 0 := begin apply coeff_eq_zero_of_degree_lt, by_cases hp : p = 0, { subst hp, exact with_bot.bot_lt_coe n }, { rwa [degree_eq_nat_degree hp, with_bot.coe_lt_coe] } end -- TODO find a home (this file) @[simp] lemma finset_sum_coeff (s : finset Ξ²) (f : Ξ² β†’ polynomial Ξ±) (n : β„•) : coeff (s.sum f) n = s.sum (Ξ» b, coeff (f b) n) := (finset.sum_hom (Ξ» q : polynomial Ξ±, q.coeff n)).symm -- We need the explicit `decidable` argument here because an exotic one shows up in a moment! lemma ite_le_nat_degree_coeff (p : polynomial Ξ±) (n : β„•) (I : decidable (n < 1 + nat_degree p)) : @ite (n < 1 + nat_degree p) I _ (coeff p n) 0 = coeff p n := begin split_ifs, { refl }, { exact (coeff_eq_zero_of_nat_degree_lt (not_le.1 (Ξ» w, h (nat.lt_one_add_iff.2 w)))).symm, } end lemma as_sum (p : polynomial Ξ±) : p = (range (p.nat_degree + 1)).sum (Ξ» i, C (p.coeff i) * X^i) := begin ext n, simp only [add_comm, coeff_X_pow, coeff_C_mul, finset.mem_range, finset.sum_mul_boole, finset_sum_coeff, ite_le_nat_degree_coeff], end lemma monic.as_sum {p : polynomial Ξ±} (hp : p.monic) : p = X^(p.nat_degree) + ((finset.range p.nat_degree).sum $ Ξ» i, C (p.coeff i) * X^i) := begin conv_lhs { rw [p.as_sum, finset.sum_range_succ] }, suffices : C (p.coeff p.nat_degree) = 1, { rw [this, one_mul] }, exact congr_arg C hp end section map variables [comm_semiring Ξ²] variables (f : Ξ± β†’ Ξ²) /-- `map f p` maps a polynomial `p` across a ring hom `f` -/ def map : polynomial Ξ± β†’ polynomial Ξ² := evalβ‚‚ (C ∘ f) X variables [is_semiring_hom f] @[simp] lemma map_C : (C a).map f = C (f a) := evalβ‚‚_C _ _ @[simp] lemma map_X : X.map f = X := evalβ‚‚_X _ _ @[simp] lemma map_zero : (0 : polynomial Ξ±).map f = 0 := evalβ‚‚_zero _ _ @[simp] lemma map_add : (p + q).map f = p.map f + q.map f := evalβ‚‚_add _ _ @[simp] lemma map_one : (1 : polynomial Ξ±).map f = 1 := evalβ‚‚_one _ _ @[simp] lemma map_mul : (p * q).map f = p.map f * q.map f := evalβ‚‚_mul _ _ instance map.is_semiring_hom : is_semiring_hom (map f) := evalβ‚‚.is_semiring_hom _ _ @[simp] lemma map_pow (n : β„•) : (p ^ n).map f = p.map f ^ n := evalβ‚‚_pow _ _ _ lemma coeff_map (n : β„•) : coeff (p.map f) n = f (coeff p n) := begin rw [map, evalβ‚‚, coeff_sum], conv_rhs { rw [← sum_C_mul_X_eq p, coeff_sum, finsupp.sum, ← finset.sum_hom f], }, refine finset.sum_congr rfl (Ξ» x hx, _), simp [function.comp, coeff_C_mul_X, is_semiring_hom.map_mul f], split_ifs; simp [is_semiring_hom.map_zero f], end lemma map_map {Ξ³ : Type*} [comm_semiring Ξ³] (g : Ξ² β†’ Ξ³) [is_semiring_hom g] (p : polynomial Ξ±) : (p.map f).map g = p.map (Ξ» x, g (f x)) := ext (by simp [coeff_map]) lemma evalβ‚‚_map {Ξ³ : Type*} [comm_semiring Ξ³] (g : Ξ² β†’ Ξ³) [is_semiring_hom g] (x : Ξ³) : (p.map f).evalβ‚‚ g x = p.evalβ‚‚ (Ξ» y, g (f y)) x := polynomial.induction_on p (by simp) (by simp [is_semiring_hom.map_add f] {contextual := tt}) (by simp [is_semiring_hom.map_mul f, is_semiring_hom.map_pow f, pow_succ', (mul_assoc _ _ _).symm] {contextual := tt}) lemma eval_map (x : Ξ²) : (p.map f).eval x = p.evalβ‚‚ f x := evalβ‚‚_map _ _ _ @[simp] lemma map_id : p.map id = p := by simp [id, polynomial.ext_iff, coeff_map] lemma mem_map_range {p : polynomial Ξ²} : p ∈ set.range (map f) ↔ βˆ€ n, p.coeff n ∈ (set.range f) := begin split, { rintro ⟨p, rfl⟩ n, rw coeff_map, exact set.mem_range_self _ }, { intro h, rw p.as_sum, apply is_add_submonoid.finset_sum_mem, intros i hi, rcases h i with ⟨c, hc⟩, use [C c * X^i], rw [map_mul, map_C, hc, map_pow, map_X] } end end map section variables {Ξ³ : Type*} [comm_semiring Ξ²] [comm_semiring Ξ³] variables (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ Ξ³) [is_semiring_hom f] [is_semiring_hom g] (p) lemma hom_evalβ‚‚ (x : Ξ²) : g (p.evalβ‚‚ f x) = p.evalβ‚‚ (g ∘ f) (g x) := begin apply polynomial.induction_on p; clear p, { intros a, rw [evalβ‚‚_C, evalβ‚‚_C] }, { intros p q hp hq, simp only [hp, hq, evalβ‚‚_add, is_semiring_hom.map_add g] }, { intros n a ih, replace ih := congr_arg (Ξ» y, y * g x) ih, simpa [pow_succ', is_semiring_hom.map_mul g, (mul_assoc _ _ _).symm, evalβ‚‚_C, evalβ‚‚_mul, evalβ‚‚_X] using ih } end end lemma coeff_nat_degree_eq_zero_of_degree_lt (h : degree p < degree q) : coeff p (nat_degree q) = 0 := coeff_eq_zero_of_degree_lt (lt_of_lt_of_le h degree_le_nat_degree) lemma ne_zero_of_degree_gt {n : with_bot β„•} (h : n < degree p) : p β‰  0 := mt degree_eq_bot.2 (ne.symm (ne_of_lt (lt_of_le_of_lt bot_le h))) lemma eq_C_of_degree_le_zero (h : degree p ≀ 0) : p = C (coeff p 0) := begin refine ext (Ξ» n, _), cases n, { simp }, { have : degree p < ↑(nat.succ n) := lt_of_le_of_lt h (with_bot.some_lt_some.2 (nat.succ_pos _)), rw [coeff_C, if_neg (nat.succ_ne_zero _), coeff_eq_zero_of_degree_lt this] } end lemma eq_C_of_degree_eq_zero (h : degree p = 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero (h β–Έ le_refl _) lemma degree_le_zero_iff : degree p ≀ 0 ↔ p = C (coeff p 0) := ⟨eq_C_of_degree_le_zero, Ξ» h, h.symm β–Έ degree_C_le⟩ lemma degree_add_le (p q : polynomial Ξ±) : degree (p + q) ≀ max (degree p) (degree q) := calc degree (p + q) = ((p + q).support).sup some : rfl ... ≀ (p.support βˆͺ q.support).sup some : by convert sup_mono support_add ... = p.support.sup some βŠ” q.support.sup some : by convert sup_union ... = _ : with_bot.sup_eq_max _ _ @[simp] lemma leading_coeff_zero : leading_coeff (0 : polynomial Ξ±) = 0 := rfl @[simp] lemma leading_coeff_eq_zero : leading_coeff p = 0 ↔ p = 0 := ⟨λ h, by_contradiction $ Ξ» hp, mt mem_support_iff.1 (not_not.2 h) (mem_of_max (degree_eq_nat_degree hp)), Ξ» h, h.symm β–Έ leading_coeff_zero⟩ lemma leading_coeff_eq_zero_iff_deg_eq_bot : leading_coeff p = 0 ↔ degree p = βŠ₯ := by rw [leading_coeff_eq_zero, degree_eq_bot] lemma degree_add_eq_of_degree_lt (h : degree p < degree q) : degree (p + q) = degree q := le_antisymm (max_eq_right_of_lt h β–Έ degree_add_le _ _) $ degree_le_degree $ begin rw [coeff_add, coeff_nat_degree_eq_zero_of_degree_lt h, zero_add], exact mt leading_coeff_eq_zero.1 (ne_zero_of_degree_gt h) end lemma degree_add_C (hp : 0 < degree p) : degree (p + C a) = degree p := add_comm (C a) p β–Έ degree_add_eq_of_degree_lt $ lt_of_le_of_lt degree_C_le hp lemma degree_add_eq_of_leading_coeff_add_ne_zero (h : leading_coeff p + leading_coeff q β‰  0) : degree (p + q) = max p.degree q.degree := le_antisymm (degree_add_le _ _) $ match lt_trichotomy (degree p) (degree q) with | or.inl hlt := by rw [degree_add_eq_of_degree_lt hlt, max_eq_right_of_lt hlt]; exact le_refl _ | or.inr (or.inl heq) := le_of_not_gt $ assume hlt : max (degree p) (degree q) > degree (p + q), h $ show leading_coeff p + leading_coeff q = 0, begin rw [heq, max_self] at hlt, rw [leading_coeff, leading_coeff, nat_degree_eq_of_degree_eq heq, ← coeff_add], exact coeff_nat_degree_eq_zero_of_degree_lt hlt end | or.inr (or.inr hlt) := by rw [add_comm, degree_add_eq_of_degree_lt hlt, max_eq_left_of_lt hlt]; exact le_refl _ end lemma degree_erase_le (p : polynomial Ξ±) (n : β„•) : degree (p.erase n) ≀ degree p := by convert sup_mono (erase_subset _ _) lemma degree_erase_lt (hp : p β‰  0) : degree (p.erase (nat_degree p)) < degree p := lt_of_le_of_ne (degree_erase_le _ _) $ (degree_eq_nat_degree hp).symm β–Έ (by convert Ξ» h, not_mem_erase _ _ (mem_of_max h)) lemma degree_sum_le (s : finset Ξ²) (f : Ξ² β†’ polynomial Ξ±) : degree (s.sum f) ≀ s.sup (Ξ» b, degree (f b)) := finset.induction_on s (by simp only [sum_empty, sup_empty, degree_zero, le_refl]) $ assume a s has ih, calc degree (sum (insert a s) f) ≀ max (degree (f a)) (degree (s.sum f)) : by rw sum_insert has; exact degree_add_le _ _ ... ≀ _ : by rw [sup_insert, with_bot.sup_eq_max]; exact max_le_max (le_refl _) ih lemma degree_mul_le (p q : polynomial Ξ±) : degree (p * q) ≀ degree p + degree q := calc degree (p * q) ≀ (p.support).sup (Ξ»i, degree (sum q (Ξ»j a, C (coeff p i * a) * X ^ (i + j)))) : by simp only [single_eq_C_mul_X.symm]; exact degree_sum_le _ _ ... ≀ p.support.sup (Ξ»i, q.support.sup (Ξ»j, degree (C (coeff p i * coeff q j) * X ^ (i + j)))) : finset.sup_mono_fun (assume i hi, degree_sum_le _ _) ... ≀ degree p + degree q : begin refine finset.sup_le (Ξ» a ha, finset.sup_le (Ξ» b hb, le_trans (degree_monomial_le _ _) _)), rw [with_bot.coe_add], rw mem_support_iff at ha hb, exact add_le_add' (le_degree_of_ne_zero ha) (le_degree_of_ne_zero hb) end lemma degree_pow_le (p : polynomial Ξ±) : βˆ€ n, degree (p ^ n) ≀ add_monoid.smul n (degree p) | 0 := by rw [pow_zero, add_monoid.zero_smul]; exact degree_one_le | (n+1) := calc degree (p ^ (n + 1)) ≀ degree p + degree (p ^ n) : by rw pow_succ; exact degree_mul_le _ _ ... ≀ _ : by rw succ_smul; exact add_le_add' (le_refl _) (degree_pow_le _) @[simp] lemma leading_coeff_monomial (a : Ξ±) (n : β„•) : leading_coeff (C a * X ^ n) = a := begin by_cases ha : a = 0, { simp only [ha, C_0, zero_mul, leading_coeff_zero] }, { rw [leading_coeff, nat_degree, degree_monomial _ ha, ← single_eq_C_mul_X], exact @finsupp.single_eq_same _ _ _ n a } end @[simp] lemma leading_coeff_C (a : Ξ±) : leading_coeff (C a) = a := suffices leading_coeff (C a * X^0) = a, by rwa [pow_zero, mul_one] at this, leading_coeff_monomial a 0 @[simp] lemma leading_coeff_X : leading_coeff (X : polynomial Ξ±) = 1 := suffices leading_coeff (C (1:Ξ±) * X^1) = 1, by rwa [C_1, pow_one, one_mul] at this, leading_coeff_monomial 1 1 @[simp] lemma monic_X : monic (X : polynomial Ξ±) := leading_coeff_X @[simp] lemma leading_coeff_one : leading_coeff (1 : polynomial Ξ±) = 1 := suffices leading_coeff (C (1:Ξ±) * X^0) = 1, by rwa [C_1, pow_zero, mul_one] at this, leading_coeff_monomial 1 0 @[simp] lemma monic_one : monic (1 : polynomial Ξ±) := leading_coeff_C _ lemma monic.ne_zero_of_zero_ne_one (h : (0:Ξ±) β‰  1) {p : polynomial Ξ±} (hp : p.monic) : p β‰  0 := by { contrapose! h, rwa [h] at hp } lemma monic.ne_zero {Ξ± : Type*} [nonzero_comm_ring Ξ±] {p : polynomial Ξ±} (hp : p.monic) : p β‰  0 := hp.ne_zero_of_zero_ne_one $ zero_ne_one lemma leading_coeff_add_of_degree_lt (h : degree p < degree q) : leading_coeff (p + q) = leading_coeff q := have coeff p (nat_degree q) = 0, from coeff_nat_degree_eq_zero_of_degree_lt h, by simp only [leading_coeff, nat_degree_eq_of_degree_eq (degree_add_eq_of_degree_lt h), this, coeff_add, zero_add] lemma leading_coeff_add_of_degree_eq (h : degree p = degree q) (hlc : leading_coeff p + leading_coeff q β‰  0) : leading_coeff (p + q) = leading_coeff p + leading_coeff q := have nat_degree (p + q) = nat_degree p, by apply nat_degree_eq_of_degree_eq; rw [degree_add_eq_of_leading_coeff_add_ne_zero hlc, h, max_self], by simp only [leading_coeff, this, nat_degree_eq_of_degree_eq h, coeff_add] @[simp] lemma coeff_mul_degree_add_degree (p q : polynomial Ξ±) : coeff (p * q) (nat_degree p + nat_degree q) = leading_coeff p * leading_coeff q := calc coeff (p * q) (nat_degree p + nat_degree q) = (nat.antidiagonal (nat_degree p + nat_degree q)).sum (Ξ» x, coeff p x.1 * coeff q x.2) : coeff_mul _ _ _ ... = coeff p (nat_degree p) * coeff q (nat_degree q) : begin refine finset.sum_eq_single (nat_degree p, nat_degree q) _ _, { rintro ⟨i,j⟩ h₁ hβ‚‚, rw nat.mem_antidiagonal at h₁, by_cases H : nat_degree p < i, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 H)), zero_mul] }, { rw not_lt_iff_eq_or_lt at H, cases H, { subst H, rw add_left_cancel_iff at h₁, dsimp at h₁, subst h₁, exfalso, exact hβ‚‚ rfl }, { suffices : nat_degree q < j, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 this)), mul_zero] }, { by_contra H', rw not_lt at H', exact ne_of_lt (nat.lt_of_lt_of_le (nat.add_lt_add_right H j) (nat.add_le_add_left H' _)) h₁ } } } }, { intro H, exfalso, apply H, rw nat.mem_antidiagonal } end lemma degree_mul_eq' (h : leading_coeff p * leading_coeff q β‰  0) : degree (p * q) = degree p + degree q := have hp : p β‰  0 := by refine mt _ h; exact Ξ» hp, by rw [hp, leading_coeff_zero, zero_mul], have hq : q β‰  0 := by refine mt _ h; exact Ξ» hq, by rw [hq, leading_coeff_zero, mul_zero], le_antisymm (degree_mul_le _ _) begin rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq], refine le_degree_of_ne_zero _, rwa coeff_mul_degree_add_degree end lemma nat_degree_mul_eq' (h : leading_coeff p * leading_coeff q β‰  0) : nat_degree (p * q) = nat_degree p + nat_degree q := have hp : p β‰  0 := mt leading_coeff_eq_zero.2 (Ξ» h₁, h $ by rw [h₁, zero_mul]), have hq : q β‰  0 := mt leading_coeff_eq_zero.2 (Ξ» h₁, h $ by rw [h₁, mul_zero]), have hpq : p * q β‰  0 := Ξ» hpq, by rw [← coeff_mul_degree_add_degree, hpq, coeff_zero] at h; exact h rfl, option.some_inj.1 (show (nat_degree (p * q) : with_bot β„•) = nat_degree p + nat_degree q, by rw [← degree_eq_nat_degree hpq, degree_mul_eq' h, degree_eq_nat_degree hp, degree_eq_nat_degree hq]) lemma leading_coeff_mul' (h : leading_coeff p * leading_coeff q β‰  0) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin unfold leading_coeff, rw [nat_degree_mul_eq' h, coeff_mul_degree_add_degree], refl end lemma leading_coeff_pow' : leading_coeff p ^ n β‰  0 β†’ leading_coeff (p ^ n) = leading_coeff p ^ n := nat.rec_on n (by simp) $ Ξ» n ih h, have h₁ : leading_coeff p ^ n β‰  0 := Ξ» h₁, h $ by rw [pow_succ, h₁, mul_zero], have hβ‚‚ : leading_coeff p * leading_coeff (p ^ n) β‰  0 := by rwa [pow_succ, ← ih h₁] at h, by rw [pow_succ, pow_succ, leading_coeff_mul' hβ‚‚, ih h₁] lemma degree_pow_eq' : βˆ€ {n}, leading_coeff p ^ n β‰  0 β†’ degree (p ^ n) = add_monoid.smul n (degree p) | 0 := Ξ» h, by rw [pow_zero, ← C_1] at *; rw [degree_C h, add_monoid.zero_smul] | (n+1) := Ξ» h, have h₁ : leading_coeff p ^ n β‰  0 := Ξ» h₁, h $ by rw [pow_succ, h₁, mul_zero], have hβ‚‚ : leading_coeff p * leading_coeff (p ^ n) β‰  0 := by rwa [pow_succ, ← leading_coeff_pow' h₁] at h, by rw [pow_succ, degree_mul_eq' hβ‚‚, succ_smul, degree_pow_eq' h₁] lemma nat_degree_pow_eq' {n : β„•} (h : leading_coeff p ^ n β‰  0) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp * else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else have hpn : p ^ n β‰  0, from Ξ» hpn0, have h1 : _ := h, by rw [← leading_coeff_pow' h1, hpn0, leading_coeff_zero] at h; exact h rfl, option.some_inj.1 $ show (nat_degree (p ^ n) : with_bot β„•) = (n * nat_degree p : β„•), by rw [← degree_eq_nat_degree hpn, degree_pow_eq' h, degree_eq_nat_degree hp0, ← with_bot.coe_smul]; simp @[simp] lemma leading_coeff_X_pow : βˆ€ n : β„•, leading_coeff ((X : polynomial Ξ±) ^ n) = 1 | 0 := by simp | (n+1) := if h10 : (1 : Ξ±) = 0 then by rw [pow_succ, ← one_mul X, ← C_1, h10]; simp else have h : leading_coeff (X : polynomial Ξ±) * leading_coeff (X ^ n) β‰  0, by rw [leading_coeff_X, leading_coeff_X_pow n, one_mul]; exact h10, by rw [pow_succ, leading_coeff_mul' h, leading_coeff_X, leading_coeff_X_pow, one_mul] lemma nat_degree_comp_le : nat_degree (p.comp q) ≀ nat_degree p * nat_degree q := if h0 : p.comp q = 0 then by rw [h0, nat_degree_zero]; exact nat.zero_le _ else with_bot.coe_le_coe.1 $ calc ↑(nat_degree (p.comp q)) = degree (p.comp q) : (degree_eq_nat_degree h0).symm ... ≀ _ : degree_sum_le _ _ ... ≀ _ : sup_le (Ξ» n hn, calc degree (C (coeff p n) * q ^ n) ≀ degree (C (coeff p n)) + degree (q ^ n) : degree_mul_le _ _ ... ≀ nat_degree (C (coeff p n)) + add_monoid.smul n (degree q) : add_le_add' degree_le_nat_degree (degree_pow_le _ _) ... ≀ nat_degree (C (coeff p n)) + add_monoid.smul n (nat_degree q) : add_le_add_left' (add_monoid.smul_le_smul_of_le_right (@degree_le_nat_degree _ _ q) n) ... = (n * nat_degree q : β„•) : by rw [nat_degree_C, with_bot.coe_zero, zero_add, ← with_bot.coe_smul, add_monoid.smul_eq_mul]; simp ... ≀ (nat_degree p * nat_degree q : β„•) : with_bot.coe_le_coe.2 $ mul_le_mul_of_nonneg_right (le_nat_degree_of_ne_zero (finsupp.mem_support_iff.1 hn)) (nat.zero_le _)) lemma degree_map_le [comm_semiring Ξ²] (f : Ξ± β†’ Ξ²) [is_semiring_hom f] : degree (p.map f) ≀ degree p := if h : p.map f = 0 then by simp [h] else begin rw [degree_eq_nat_degree h], refine le_degree_of_ne_zero (mt (congr_arg f) _), rw [← coeff_map f, is_semiring_hom.map_zero f], exact mt leading_coeff_eq_zero.1 h end lemma subsingleton_of_monic_zero (h : monic (0 : polynomial Ξ±)) : (βˆ€ p q : polynomial Ξ±, p = q) ∧ (βˆ€ a b : Ξ±, a = b) := by rw [monic.def, leading_coeff_zero] at h; exact ⟨λ p q, by rw [← mul_one p, ← mul_one q, ← C_1, ← h, C_0, mul_zero, mul_zero], Ξ» a b, by rw [← mul_one a, ← mul_one b, ← h, mul_zero, mul_zero]⟩ lemma degree_map_eq_of_leading_coeff_ne_zero [comm_semiring Ξ²] (f : Ξ± β†’ Ξ²) [is_semiring_hom f] (hf : f (leading_coeff p) β‰  0) : degree (p.map f) = degree p := le_antisymm (degree_map_le f) $ have hp0 : p β‰  0, from Ξ» hp0, by simpa [hp0, is_semiring_hom.map_zero f] using hf, begin rw [degree_eq_nat_degree hp0], refine le_degree_of_ne_zero _, rw [coeff_map], exact hf end lemma monic_map [comm_semiring Ξ²] (f : Ξ± β†’ Ξ²) [is_semiring_hom f] (hp : monic p) : monic (p.map f) := if h : (0 : Ξ²) = 1 then by haveI := subsingleton_of_zero_eq_one Ξ² h; exact subsingleton.elim _ _ else have f (leading_coeff p) β‰  0, by rwa [show _ = _, from hp, is_semiring_hom.map_one f, ne.def, eq_comm], by erw [monic, leading_coeff, nat_degree_eq_of_degree_eq (degree_map_eq_of_leading_coeff_ne_zero f this), coeff_map, ← leading_coeff, show _ = _, from hp, is_semiring_hom.map_one f] lemma zero_le_degree_iff {p : polynomial Ξ±} : 0 ≀ degree p ↔ p β‰  0 := by rw [ne.def, ← degree_eq_bot]; cases degree p; exact dec_trivial @[simp] lemma coeff_mul_X_zero (p : polynomial Ξ±) : coeff (p * X) 0 = 0 := by rw [coeff_mul, nat.antidiagonal_zero]; simp only [polynomial.coeff_X_zero, finset.insert_empty_eq_singleton, finset.sum_singleton, mul_zero] end comm_semiring instance subsingleton [subsingleton Ξ±] [comm_semiring Ξ±] : subsingleton (polynomial Ξ±) := ⟨λ _ _, ext (Ξ» _, subsingleton.elim _ _)⟩ section comm_semiring variables [comm_semiring Ξ±] {p q r : polynomial Ξ±} lemma ne_zero_of_monic_of_zero_ne_one (hp : monic p) (h : (0 : Ξ±) β‰  1) : p β‰  0 := mt (congr_arg leading_coeff) $ by rw [monic.def.1 hp, leading_coeff_zero]; cc lemma eq_X_add_C_of_degree_le_one (h : degree p ≀ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := ext (Ξ» n, nat.cases_on n (by simp) (Ξ» n, nat.cases_on n (by simp [coeff_C]) (Ξ» m, have degree p < m.succ.succ, from lt_of_le_of_lt h dec_trivial, by simp [coeff_eq_zero_of_degree_lt this, coeff_C, nat.succ_ne_zero, coeff_X, nat.succ_inj', @eq_comm β„• 0]))) lemma eq_X_add_C_of_degree_eq_one (h : degree p = 1) : p = C (p.leading_coeff) * X + C (p.coeff 0) := (eq_X_add_C_of_degree_le_one (show degree p ≀ 1, from h β–Έ le_refl _)).trans (by simp [leading_coeff, nat_degree_eq_of_degree_eq_some h]) theorem degree_C_mul_X_pow_le (r : Ξ±) (n : β„•) : degree (C r * X^n) ≀ n := begin rw [← single_eq_C_mul_X], refine finset.sup_le (Ξ» b hb, _), rw list.eq_of_mem_singleton (finsupp.support_single_subset hb), exact le_refl _ end theorem degree_X_pow_le (n : β„•) : degree (X^n : polynomial Ξ±) ≀ n := by simpa only [C_1, one_mul] using degree_C_mul_X_pow_le (1:Ξ±) n theorem degree_X_le : degree (X : polynomial Ξ±) ≀ 1 := by simpa only [C_1, one_mul, pow_one] using degree_C_mul_X_pow_le (1:Ξ±) 1 section injective open function variables [comm_semiring Ξ²] {f : Ξ± β†’ Ξ²} [is_semiring_hom f] (hf : function.injective f) include hf lemma degree_map_eq_of_injective (p : polynomial Ξ±) : degree (p.map f) = degree p := if h : p = 0 then by simp [h] else degree_map_eq_of_leading_coeff_ne_zero _ (by rw [← is_semiring_hom.map_zero f]; exact mt hf.eq_iff.1 (mt leading_coeff_eq_zero.1 h)) lemma degree_map' (p : polynomial Ξ±) : degree (p.map f) = degree p := p.degree_map_eq_of_injective hf lemma nat_degree_map' (p : polynomial Ξ±) : nat_degree (p.map f) = nat_degree p := nat_degree_eq_of_degree_eq (degree_map' hf p) lemma map_injective (p : polynomial Ξ±) : injective (map f : polynomial Ξ± β†’ polynomial Ξ²) := Ξ» p q h, ext $ Ξ» m, hf $ begin rw ext_iff at h, specialize h m, rw [coeff_map f, coeff_map f] at h, exact h end lemma leading_coeff_of_injective (p : polynomial Ξ±) : leading_coeff (p.map f) = f (leading_coeff p) := begin delta leading_coeff, rw [coeff_map f, nat_degree_map' hf p] end lemma monic_of_injective {p : polynomial Ξ±} (hp : (p.map f).monic) : p.monic := begin apply hf, rw [← leading_coeff_of_injective hf, hp.leading_coeff, is_semiring_hom.map_one f] end end injective theorem monic_of_degree_le (n : β„•) (H1 : degree p ≀ n) (H2 : coeff p n = 1) : monic p := decidable.by_cases (assume H : degree p < n, @subsingleton.elim _ (subsingleton_of_zero_eq_one Ξ± $ H2 β–Έ (coeff_eq_zero_of_degree_lt H).symm) _ _) (assume H : Β¬degree p < n, by rwa [monic, leading_coeff, nat_degree, (lt_or_eq_of_le H1).resolve_left H]) theorem monic_X_pow_add {n : β„•} (H : degree p ≀ n) : monic (X ^ (n+1) + p) := have H1 : degree p < n+1, from lt_of_le_of_lt H (with_bot.coe_lt_coe.2 (nat.lt_succ_self n)), monic_of_degree_le (n+1) (le_trans (degree_add_le _ _) (max_le (degree_X_pow_le _) (le_of_lt H1))) (by rw [coeff_add, coeff_X_pow, if_pos rfl, coeff_eq_zero_of_degree_lt H1, add_zero]) theorem monic_X_add_C (x : Ξ±) : monic (X + C x) := pow_one (X : polynomial Ξ±) β–Έ monic_X_pow_add degree_C_le theorem degree_le_iff_coeff_zero (f : polynomial Ξ±) (n : with_bot β„•) : degree f ≀ n ↔ βˆ€ m : β„•, n < m β†’ coeff f m = 0 := ⟨λ (H : finset.sup (f.support) some ≀ n) m (Hm : n < (m : with_bot β„•)), decidable.of_not_not $ Ξ» H4, have H1 : m βˆ‰ f.support, from Ξ» H2, not_lt_of_ge ((finset.sup_le_iff.1 H) m H2 : ((m : with_bot β„•) ≀ n)) Hm, H1 $ (finsupp.mem_support_to_fun f m).2 H4, Ξ» H, finset.sup_le $ Ξ» b Hb, decidable.of_not_not $ Ξ» Hn, (finsupp.mem_support_to_fun f b).1 Hb $ H b $ lt_of_not_ge Hn⟩ theorem nat_degree_le_of_degree_le {p : polynomial Ξ±} {n : β„•} (H : degree p ≀ n) : nat_degree p ≀ n := show option.get_or_else (degree p) 0 ≀ n, from match degree p, H with | none, H := zero_le _ | (some d), H := with_bot.coe_le_coe.1 H end theorem leading_coeff_mul_X_pow {p : polynomial Ξ±} {n : β„•} : leading_coeff (p * X ^ n) = leading_coeff p := decidable.by_cases (assume H : leading_coeff p = 0, by rw [H, leading_coeff_eq_zero.1 H, zero_mul, leading_coeff_zero]) (assume H : leading_coeff p β‰  0, by rw [leading_coeff_mul', leading_coeff_X_pow, mul_one]; rwa [leading_coeff_X_pow, mul_one]) lemma monic_mul (hp : monic p) (hq : monic q) : monic (p * q) := if h0 : (0 : Ξ±) = 1 then by haveI := subsingleton_of_zero_eq_one _ h0; exact subsingleton.elim _ _ else have leading_coeff p * leading_coeff q β‰  0, by simp [monic.def.1 hp, monic.def.1 hq, ne.symm h0], by rw [monic.def, leading_coeff_mul' this, monic.def.1 hp, monic.def.1 hq, one_mul] lemma monic_pow (hp : monic p) : βˆ€ (n : β„•), monic (p ^ n) | 0 := monic_one | (n+1) := monic_mul hp (monic_pow n) lemma multiplicity_finite_of_degree_pos_of_monic (hp : (0 : with_bot β„•) < degree p) (hmp : monic p) (hq : q β‰  0) : multiplicity.finite p q := have zn0 : (0 : Ξ±) β‰  1, from Ξ» h, by haveI := subsingleton_of_zero_eq_one _ h; exact hq (subsingleton.elim _ _), ⟨nat_degree q, Ξ» ⟨r, hr⟩, have hp0 : p β‰  0, from Ξ» hp0, by simp [hp0] at hp; contradiction, have hr0 : r β‰  0, from Ξ» hr0, by simp * at *, have hpn1 : leading_coeff p ^ (nat_degree q + 1) = 1, by simp [show _ = _, from hmp], have hpn0' : leading_coeff p ^ (nat_degree q + 1) β‰  0, from hpn1.symm β–Έ zn0.symm, have hpnr0 : leading_coeff (p ^ (nat_degree q + 1)) * leading_coeff r β‰  0, by simp only [leading_coeff_pow' hpn0', leading_coeff_eq_zero, hpn1, one_pow, one_mul, ne.def, hr0]; simp, have hpn0 : p ^ (nat_degree q + 1) β‰  0, from mt leading_coeff_eq_zero.2 $ by rw [leading_coeff_pow' hpn0', show _ = _, from hmp, one_pow]; exact zn0.symm, have hnp : 0 < nat_degree p, by rw [← with_bot.coe_lt_coe, ← degree_eq_nat_degree hp0]; exact hp, begin have := congr_arg nat_degree hr, rw [nat_degree_mul_eq' hpnr0, nat_degree_pow_eq' hpn0', add_mul, add_assoc] at this, exact ne_of_lt (lt_add_of_le_of_pos (le_mul_of_one_le_right' (nat.zero_le _) hnp) (add_pos_of_pos_of_nonneg (by rwa one_mul) (nat.zero_le _))) this end⟩ end comm_semiring section nonzero_comm_semiring variables [nonzero_comm_semiring Ξ±] {p q : polynomial Ξ±} instance : nonzero_comm_semiring (polynomial Ξ±) := { zero_ne_one := Ξ» (h : (0 : polynomial Ξ±) = 1), @zero_ne_one Ξ± _ $ calc (0 : Ξ±) = eval 0 0 : eval_zero.symm ... = eval 0 1 : congr_arg _ h ... = 1 : eval_C, ..polynomial.comm_semiring } @[simp] lemma degree_one : degree (1 : polynomial Ξ±) = (0 : with_bot β„•) := degree_C (show (1 : Ξ±) β‰  0, from zero_ne_one.symm) @[simp] lemma degree_X : degree (X : polynomial Ξ±) = 1 := begin unfold X degree single finsupp.support, rw if_neg (zero_ne_one).symm, refl end lemma X_ne_zero : (X : polynomial Ξ±) β‰  0 := mt (congr_arg (Ξ» p, coeff p 1)) (by simp) @[simp] lemma degree_X_pow : βˆ€ (n : β„•), degree ((X : polynomial Ξ±) ^ n) = n | 0 := by simp only [pow_zero, degree_one]; refl | (n+1) := have h : leading_coeff (X : polynomial Ξ±) * leading_coeff (X ^ n) β‰  0, by rw [leading_coeff_X, leading_coeff_X_pow n, one_mul]; exact zero_ne_one.symm, by rw [pow_succ, degree_mul_eq' h, degree_X, degree_X_pow, add_comm]; refl @[simp] lemma not_monic_zero : Β¬monic (0 : polynomial Ξ±) := by simpa only [monic, leading_coeff_zero] using zero_ne_one lemma ne_zero_of_monic (h : monic p) : p β‰  0 := Ξ» h₁, @not_monic_zero Ξ± _ (h₁ β–Έ h) end nonzero_comm_semiring section comm_semiring variables [comm_semiring Ξ±] {p q : polynomial Ξ±} /-- `dix_X p` return a polynomial `q` such that `q * X + C (p.coeff 0) = p`. It can be used in a semiring where the usual division algorithm is not possible -/ def div_X (p : polynomial Ξ±) : polynomial Ξ± := { to_fun := Ξ» n, p.coeff (n + 1), support := ⟨(p.support.filter (> 0)).1.map (Ξ» n, n - 1), multiset.nodup_map_on begin simp only [finset.mem_def.symm, finset.mem_erase, finset.mem_filter], assume x hx y hy hxy, rwa [← @add_right_cancel_iff _ _ 1, nat.sub_add_cancel hx.2, nat.sub_add_cancel hy.2] at hxy end (p.support.filter (> 0)).2⟩, mem_support_to_fun := Ξ» n, suffices (βˆƒ (a : β„•), (Β¬coeff p a = 0 ∧ a > 0) ∧ a - 1 = n) ↔ Β¬coeff p (n + 1) = 0, by simpa [finset.mem_def.symm, apply_eq_coeff], ⟨λ ⟨a, ha⟩, by rw [← ha.2, nat.sub_add_cancel ha.1.2]; exact ha.1.1, Ξ» h, ⟨n + 1, ⟨h, nat.succ_pos _⟩, nat.succ_sub_one _⟩⟩ } lemma div_X_mul_X_add (p : polynomial Ξ±) : div_X p * X + C (p.coeff 0) = p := ext $ Ξ» n, nat.cases_on n (by simp) (by simp [coeff_C, nat.succ_ne_zero, coeff_mul_X, div_X]) @[simp] lemma div_X_C (a : Ξ±) : div_X (C a) = 0 := ext $ Ξ» n, by cases n; simp [div_X, coeff_C]; simp [coeff] lemma div_X_eq_zero_iff : div_X p = 0 ↔ p = C (p.coeff 0) := ⟨λ h, by simpa [eq_comm, h] using div_X_mul_X_add p, Ξ» h, by rw [h, div_X_C]⟩ lemma div_X_add : div_X (p + q) = div_X p + div_X q := ext $ by simp [div_X] def nonzero_comm_semiring.of_polynomial_ne (h : p β‰  q) : nonzero_comm_semiring Ξ± := { zero_ne_one := Ξ» h01 : 0 = 1, h $ by rw [← mul_one p, ← mul_one q, ← C_1, ← h01, C_0, mul_zero, mul_zero], ..show comm_semiring Ξ±, by apply_instance } lemma degree_lt_degree_mul_X (hp : p β‰  0) : p.degree < (p * X).degree := by letI := nonzero_comm_semiring.of_polynomial_ne hp; exact have leading_coeff p * leading_coeff X β‰  0, by simpa, by erw [degree_mul_eq' this, degree_eq_nat_degree hp, degree_X, ← with_bot.coe_one, ← with_bot.coe_add, with_bot.coe_lt_coe]; exact nat.lt_succ_self _ lemma degree_div_X_lt (hp0 : p β‰  0) : (div_X p).degree < p.degree := by letI := nonzero_comm_semiring.of_polynomial_ne hp0; exact calc (div_X p).degree < (div_X p * X + C (p.coeff 0)).degree : if h : degree p ≀ 0 then begin have h' : C (p.coeff 0) β‰  0, by rwa [← eq_C_of_degree_le_zero h], rw [eq_C_of_degree_le_zero h, div_X_C, degree_zero, zero_mul, zero_add], exact lt_of_le_of_ne lattice.bot_le (ne.symm (mt degree_eq_bot.1 $ by simp [h'])), end else have hXp0 : div_X p β‰  0, by simpa [div_X_eq_zero_iff, -not_le, degree_le_zero_iff] using h, have leading_coeff (div_X p) * leading_coeff X β‰  0, by simpa, have degree (C (p.coeff 0)) < degree (div_X p * X), from calc degree (C (p.coeff 0)) ≀ 0 : degree_C_le ... < 1 : dec_trivial ... = degree (X : polynomial Ξ±) : degree_X.symm ... ≀ degree (div_X p * X) : by rw [← zero_add (degree X), degree_mul_eq' this]; exact add_le_add' (by rw [zero_le_degree_iff, ne.def, div_X_eq_zero_iff]; exact Ξ» h0, h (h0.symm β–Έ degree_C_le)) (le_refl _), by rw [add_comm, degree_add_eq_of_degree_lt this]; exact degree_lt_degree_mul_X hXp0 ... = p.degree : by rw div_X_mul_X_add @[elab_as_eliminator] noncomputable def rec_on_horner {M : polynomial Ξ± β†’ Sort*} : Ξ  (p : polynomial Ξ±), M 0 β†’ (Ξ  p a, coeff p 0 = 0 β†’ a β‰  0 β†’ M p β†’ M (p + C a)) β†’ (Ξ  p, p β‰  0 β†’ M p β†’ M (p * X)) β†’ M p | p := Ξ» M0 MC MX, if hp : p = 0 then eq.rec_on hp.symm M0 else have wf : degree (div_X p) < degree p, from degree_div_X_lt hp, by rw [← div_X_mul_X_add p] at *; exact if hcp0 : coeff p 0 = 0 then by rw [hcp0, C_0, add_zero]; exact MX _ (Ξ» h : div_X p = 0, by simpa [h, hcp0] using hp) (rec_on_horner _ M0 MC MX) else MC _ _ (coeff_mul_X_zero _) hcp0 (if hpX0 : div_X p = 0 then show M (div_X p * X), by rw [hpX0, zero_mul]; exact M0 else MX (div_X p) hpX0 (rec_on_horner _ M0 MC MX)) using_well_founded {dec_tac := tactic.assumption} @[elab_as_eliminator] lemma degree_pos_induction_on {P : polynomial Ξ± β†’ Prop} (p : polynomial Ξ±) (h0 : 0 < degree p) (hC : βˆ€ {a}, a β‰  0 β†’ P (C a * X)) (hX : βˆ€ {p}, 0 < degree p β†’ P p β†’ P (p * X)) (hadd : βˆ€ {p} {a}, 0 < degree p β†’ P p β†’ P (p + C a)) : P p := rec_on_horner p (Ξ» h, by rw degree_zero at h; exact absurd h dec_trivial) (Ξ» p a _ _ ih h0, have 0 < degree p, from lt_of_not_ge (Ξ» h, (not_lt_of_ge degree_C_le) $ by rwa [eq_C_of_degree_le_zero h, ← C_add] at h0), hadd this (ih this)) (Ξ» p _ ih h0', if h0 : 0 < degree p then hX h0 (ih h0) else by rw [eq_C_of_degree_le_zero (le_of_not_gt h0)] at *; exact hC (Ξ» h : coeff p 0 = 0, by simpa [h, not_lt.2 (@lattice.bot_le ( β„•) _ _)] using h0')) h0 end comm_semiring section comm_ring variables [comm_ring Ξ±] {p q : polynomial Ξ±} instance : comm_ring (polynomial Ξ±) := finsupp.comm_ring instance : has_scalar Ξ± (polynomial Ξ±) := finsupp.has_scalar -- TODO if this becomes a semimodule then the below lemma could be proved for semimodules instance : module Ξ± (polynomial Ξ±) := finsupp.module β„• Ξ± -- TODO -- this is OK for semimodules @[simp] lemma coeff_smul (p : polynomial Ξ±) (r : Ξ±) (n : β„•) : coeff (r β€’ p) n = r * coeff p n := finsupp.smul_apply -- TODO -- this is OK for semimodules lemma C_mul' (a : Ξ±) (f : polynomial Ξ±) : C a * f = a β€’ f := ext $ Ξ» n, coeff_C_mul f variable (Ξ±) def lcoeff (n : β„•) : polynomial Ξ± β†’β‚— Ξ± := { to_fun := Ξ» f, coeff f n, add := Ξ» f g, coeff_add f g n, smul := Ξ» r p, coeff_smul p r n } variable {Ξ±} @[simp] lemma lcoeff_apply (n : β„•) (f : polynomial Ξ±) : lcoeff Ξ± n f = coeff f n := rfl instance C.is_ring_hom : is_ring_hom (@C Ξ± _) := by apply is_ring_hom.of_semiring lemma int_cast_eq_C (n : β„€) : (n : polynomial Ξ±) = C n := congr_fun (int.eq_cast' _).symm n @[simp] lemma C_neg : C (-a) = -C a := is_ring_hom.map_neg C @[simp] lemma C_sub : C (a - b) = C a - C b := is_ring_hom.map_sub C instance evalβ‚‚.is_ring_hom {Ξ²} [comm_ring Ξ²] (f : Ξ± β†’ Ξ²) [is_ring_hom f] {x : Ξ²} : is_ring_hom (evalβ‚‚ f x) := by apply is_ring_hom.of_semiring instance eval.is_ring_hom {x : Ξ±} : is_ring_hom (eval x) := evalβ‚‚.is_ring_hom _ instance map.is_ring_hom {Ξ²} [comm_ring Ξ²] (f : Ξ± β†’ Ξ²) [is_ring_hom f] : is_ring_hom (map f) := evalβ‚‚.is_ring_hom (C ∘ f) @[simp] lemma map_sub {Ξ²} [comm_ring Ξ²] (f : Ξ± β†’ Ξ²) [is_ring_hom f] : (p - q).map f = p.map f - q.map f := is_ring_hom.map_sub _ @[simp] lemma map_neg {Ξ²} [comm_ring Ξ²] (f : Ξ± β†’ Ξ²) [is_ring_hom f] : (-p).map f = -(p.map f) := is_ring_hom.map_neg _ @[simp] lemma degree_neg (p : polynomial Ξ±) : degree (-p) = degree p := by unfold degree; rw support_neg @[simp] lemma nat_degree_neg (p : polynomial Ξ±) : nat_degree (-p) = nat_degree p := by simp [nat_degree] @[simp] lemma nat_degree_int_cast (n : β„€) : nat_degree (n : polynomial Ξ±) = 0 := by simp [int_cast_eq_C] @[simp] lemma coeff_neg (p : polynomial Ξ±) (n : β„•) : coeff (-p) n = -coeff p n := rfl @[simp] lemma coeff_sub (p q : polynomial Ξ±) (n : β„•) : coeff (p - q) n = coeff p n - coeff q n := rfl @[simp] lemma evalβ‚‚_neg {Ξ²} [comm_ring Ξ²] (f : Ξ± β†’ Ξ²) [is_ring_hom f] {x : Ξ²} : (-p).evalβ‚‚ f x = -p.evalβ‚‚ f x := is_ring_hom.map_neg _ @[simp] lemma evalβ‚‚_sub {Ξ²} [comm_ring Ξ²] (f : Ξ± β†’ Ξ²) [is_ring_hom f] {x : Ξ²} : (p - q).evalβ‚‚ f x = p.evalβ‚‚ f x - q.evalβ‚‚ f x := is_ring_hom.map_sub _ @[simp] lemma eval_neg (p : polynomial Ξ±) (x : Ξ±) : (-p).eval x = -p.eval x := is_ring_hom.map_neg _ @[simp] lemma eval_sub (p q : polynomial Ξ±) (x : Ξ±) : (p - q).eval x = p.eval x - q.eval x := is_ring_hom.map_sub _ lemma degree_sub_lt (hd : degree p = degree q) (hp0 : p β‰  0) (hlc : leading_coeff p = leading_coeff q) : degree (p - q) < degree p := have hp : single (nat_degree p) (leading_coeff p) + p.erase (nat_degree p) = p := finsupp.single_add_erase, have hq : single (nat_degree q) (leading_coeff q) + q.erase (nat_degree q) = q := finsupp.single_add_erase, have hd' : nat_degree p = nat_degree q := by unfold nat_degree; rw hd, have hq0 : q β‰  0 := mt degree_eq_bot.2 (hd β–Έ mt degree_eq_bot.1 hp0), calc degree (p - q) = degree (erase (nat_degree q) p + -erase (nat_degree q) q) : by conv {to_lhs, rw [← hp, ← hq, hlc, hd', add_sub_add_left_eq_sub, sub_eq_add_neg]} ... ≀ max (degree (erase (nat_degree q) p)) (degree (erase (nat_degree q) q)) : degree_neg (erase (nat_degree q) q) β–Έ degree_add_le _ _ ... < degree p : max_lt_iff.2 ⟨hd' β–Έ degree_erase_lt hp0, hd.symm β–Έ degree_erase_lt hq0⟩ lemma ne_zero_of_ne_zero_of_monic (hp : p β‰  0) (hq : monic q) : q β‰  0 | h := begin rw [h, monic.def, leading_coeff_zero] at hq, rw [← mul_one p, ← C_1, ← hq, C_0, mul_zero] at hp, exact hp rfl end lemma div_wf_lemma (h : degree q ≀ degree p ∧ p β‰  0) (hq : monic q) : degree (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) < degree p := have hp : leading_coeff p β‰  0 := mt leading_coeff_eq_zero.1 h.2, have hpq : leading_coeff (C (leading_coeff p) * X ^ (nat_degree p - nat_degree q)) * leading_coeff q β‰  0, by rwa [leading_coeff_monomial, monic.def.1 hq, mul_one], if h0 : p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q = 0 then h0.symm β–Έ (lt_of_not_ge $ mt le_bot_iff.1 (mt degree_eq_bot.1 h.2)) else have hq0 : q β‰  0 := ne_zero_of_ne_zero_of_monic h.2 hq, have hlt : nat_degree q ≀ nat_degree p := with_bot.coe_le_coe.1 (by rw [← degree_eq_nat_degree h.2, ← degree_eq_nat_degree hq0]; exact h.1), degree_sub_lt (by rw [degree_mul_eq' hpq, degree_monomial _ hp, degree_eq_nat_degree h.2, degree_eq_nat_degree hq0, ← with_bot.coe_add, nat.sub_add_cancel hlt]) h.2 (by rw [leading_coeff_mul' hpq, leading_coeff_monomial, monic.def.1 hq, mul_one]) noncomputable def div_mod_by_monic_aux : Ξ  (p : polynomial Ξ±) {q : polynomial Ξ±}, monic q β†’ polynomial Ξ± Γ— polynomial Ξ± | p := Ξ» q hq, if h : degree q ≀ degree p ∧ p β‰  0 then let z := C (leading_coeff p) * X^(nat_degree p - nat_degree q) in have wf : _ := div_wf_lemma h hq, let dm := div_mod_by_monic_aux (p - z * q) hq in ⟨z + dm.1, dm.2⟩ else ⟨0, p⟩ using_well_founded {dec_tac := tactic.assumption} /-- `div_by_monic` gives the quotient of `p` by a monic polynomial `q`. -/ def div_by_monic (p q : polynomial Ξ±) : polynomial Ξ± := if hq : monic q then (div_mod_by_monic_aux p hq).1 else 0 /-- `mod_by_monic` gives the remainder of `p` by a monic polynomial `q`. -/ def mod_by_monic (p q : polynomial Ξ±) : polynomial Ξ± := if hq : monic q then (div_mod_by_monic_aux p hq).2 else p infixl ` /β‚˜ ` : 70 := div_by_monic infixl ` %β‚˜ ` : 70 := mod_by_monic lemma degree_mod_by_monic_lt : βˆ€ (p : polynomial Ξ±) {q : polynomial Ξ±} (hq : monic q) (hq0 : q β‰  0), degree (p %β‚˜ q) < degree q | p := Ξ» q hq hq0, if h : degree q ≀ degree p ∧ p β‰  0 then have wf : _ := div_wf_lemma ⟨h.1, h.2⟩ hq, have degree ((p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) %β‚˜ q) < degree q := degree_mod_by_monic_lt (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq hq0, begin unfold mod_by_monic at this ⊒, unfold div_mod_by_monic_aux, rw dif_pos hq at this ⊒, rw if_pos h, exact this end else or.cases_on (not_and_distrib.1 h) begin unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h], exact lt_of_not_ge, end begin assume hp, unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, not_not.1 hp], exact lt_of_le_of_ne bot_le (ne.symm (mt degree_eq_bot.1 hq0)), end using_well_founded {dec_tac := tactic.assumption} lemma mod_by_monic_eq_sub_mul_div : βˆ€ (p : polynomial Ξ±) {q : polynomial Ξ±} (hq : monic q), p %β‚˜ q = p - q * (p /β‚˜ q) | p := Ξ» q hq, if h : degree q ≀ degree p ∧ p β‰  0 then have wf : _ := div_wf_lemma h hq, have ih : _ := mod_by_monic_eq_sub_mul_div (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq, begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_pos h], rw [mod_by_monic, dif_pos hq] at ih, refine ih.trans _, unfold div_by_monic, rw [dif_pos hq, dif_pos hq, if_pos h, mul_add, sub_add_eq_sub_sub, mul_comm] end else begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, dif_pos hq, if_neg h, mul_zero, sub_zero] end using_well_founded {dec_tac := tactic.assumption} lemma mod_by_monic_add_div (p : polynomial Ξ±) {q : polynomial Ξ±} (hq : monic q) : p %β‚˜ q + q * (p /β‚˜ q) = p := eq_sub_iff_add_eq.1 (mod_by_monic_eq_sub_mul_div p hq) @[simp] lemma zero_mod_by_monic (p : polynomial Ξ±) : 0 %β‚˜ p = 0 := begin unfold mod_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma zero_div_by_monic (p : polynomial Ξ±) : 0 /β‚˜ p = 0 := begin unfold div_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma mod_by_monic_zero (p : polynomial Ξ±) : p %β‚˜ 0 = p := if h : monic (0 : polynomial Ξ±) then (subsingleton_of_monic_zero h).1 _ _ else by unfold mod_by_monic div_mod_by_monic_aux; rw dif_neg h @[simp] lemma div_by_monic_zero (p : polynomial Ξ±) : p /β‚˜ 0 = 0 := if h : monic (0 : polynomial Ξ±) then (subsingleton_of_monic_zero h).1 _ _ else by unfold div_by_monic div_mod_by_monic_aux; rw dif_neg h lemma div_by_monic_eq_of_not_monic (p : polynomial Ξ±) (hq : Β¬monic q) : p /β‚˜ q = 0 := dif_neg hq lemma mod_by_monic_eq_of_not_monic (p : polynomial Ξ±) (hq : Β¬monic q) : p %β‚˜ q = p := dif_neg hq lemma mod_by_monic_eq_self_iff (hq : monic q) (hq0 : q β‰  0) : p %β‚˜ q = p ↔ degree p < degree q := ⟨λ h, h β–Έ degree_mod_by_monic_lt _ hq hq0, Ξ» h, have Β¬ degree q ≀ degree p := not_le_of_gt h, by unfold mod_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ lemma div_by_monic_eq_zero_iff (hq : monic q) (hq0 : q β‰  0) : p /β‚˜ q = 0 ↔ degree p < degree q := ⟨λ h, by have := mod_by_monic_add_div p hq; rwa [h, mul_zero, add_zero, mod_by_monic_eq_self_iff hq hq0] at this, Ξ» h, have Β¬ degree q ≀ degree p := not_le_of_gt h, by unfold div_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ lemma degree_add_div_by_monic (hq : monic q) (h : degree q ≀ degree p) : degree q + degree (p /β‚˜ q) = degree p := if hq0 : q = 0 then have βˆ€ (p : polynomial Ξ±), p = 0, from Ξ» p, (@subsingleton_of_monic_zero Ξ± _ (hq0 β–Έ hq)).1 _ _, by rw [this (p /β‚˜ q), this p, this q]; refl else have hdiv0 : p /β‚˜ q β‰  0 := by rwa [(β‰ ), div_by_monic_eq_zero_iff hq hq0, not_lt], have hlc : leading_coeff q * leading_coeff (p /β‚˜ q) β‰  0 := by rwa [monic.def.1 hq, one_mul, (β‰ ), leading_coeff_eq_zero], have hmod : degree (p %β‚˜ q) < degree (q * (p /β‚˜ q)) := calc degree (p %β‚˜ q) < degree q : degree_mod_by_monic_lt _ hq hq0 ... ≀ _ : by rw [degree_mul_eq' hlc, degree_eq_nat_degree hq0, degree_eq_nat_degree hdiv0, ← with_bot.coe_add, with_bot.coe_le_coe]; exact nat.le_add_right _ _, calc degree q + degree (p /β‚˜ q) = degree (q * (p /β‚˜ q)) : eq.symm (degree_mul_eq' hlc) ... = degree (p %β‚˜ q + q * (p /β‚˜ q)) : (degree_add_eq_of_degree_lt hmod).symm ... = _ : congr_arg _ (mod_by_monic_add_div _ hq) lemma degree_div_by_monic_le (p q : polynomial Ξ±) : degree (p /β‚˜ q) ≀ degree p := if hp0 : p = 0 then by simp only [hp0, zero_div_by_monic, le_refl] else if hq : monic q then have hq0 : q β‰  0 := ne_zero_of_ne_zero_of_monic hp0 hq, if h : degree q ≀ degree p then by rw [← degree_add_div_by_monic hq h, degree_eq_nat_degree hq0, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq hq0).1 (not_lt.2 h))]; exact with_bot.coe_le_coe.2 (nat.le_add_left _ _) else by unfold div_by_monic div_mod_by_monic_aux; simp only [dif_pos hq, h, false_and, if_false, degree_zero, bot_le] else (div_by_monic_eq_of_not_monic p hq).symm β–Έ bot_le lemma degree_div_by_monic_lt (p : polynomial Ξ±) {q : polynomial Ξ±} (hq : monic q) (hp0 : p β‰  0) (h0q : 0 < degree q) : degree (p /β‚˜ q) < degree p := have hq0 : q β‰  0 := ne_zero_of_ne_zero_of_monic hp0 hq, if hpq : degree p < degree q then begin rw [(div_by_monic_eq_zero_iff hq hq0).2 hpq, degree_eq_nat_degree hp0], exact with_bot.bot_lt_some _ end else begin rw [← degree_add_div_by_monic hq (not_lt.1 hpq), degree_eq_nat_degree hq0, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq hq0).1 hpq)], exact with_bot.coe_lt_coe.2 (nat.lt_add_of_pos_left (with_bot.coe_lt_coe.1 $ (degree_eq_nat_degree hq0) β–Έ h0q)) end lemma div_mod_by_monic_unique {f g} (q r : polynomial Ξ±) (hg : monic g) (h : r + g * q = f ∧ degree r < degree g) : f /β‚˜ g = q ∧ f %β‚˜ g = r := if hg0 : g = 0 then by split; exact (subsingleton_of_monic_zero (hg0 β–Έ hg : monic (0 : polynomial Ξ±))).1 _ _ else have h₁ : r - f %β‚˜ g = -g * (q - f /β‚˜ g), from eq_of_sub_eq_zero (by rw [← sub_eq_zero_of_eq (h.1.trans (mod_by_monic_add_div f hg).symm)]; simp [mul_add, mul_comm]), have hβ‚‚ : degree (r - f %β‚˜ g) = degree (g * (q - f /β‚˜ g)), by simp [h₁], have hβ‚„ : degree (r - f %β‚˜ g) < degree g, from calc degree (r - f %β‚˜ g) ≀ max (degree r) (degree (-(f %β‚˜ g))) : degree_add_le _ _ ... < degree g : max_lt_iff.2 ⟨h.2, by rw degree_neg; exact degree_mod_by_monic_lt _ hg hg0⟩, have hβ‚… : q - (f /β‚˜ g) = 0, from by_contradiction (Ξ» hqf, not_le_of_gt hβ‚„ $ calc degree g ≀ degree g + degree (q - f /β‚˜ g) : by erw [degree_eq_nat_degree hg0, degree_eq_nat_degree hqf, with_bot.coe_le_coe]; exact nat.le_add_right _ _ ... = degree (r - f %β‚˜ g) : by rw [hβ‚‚, degree_mul_eq']; simpa [monic.def.1 hg]), ⟨eq.symm $ eq_of_sub_eq_zero hβ‚…, eq.symm $ eq_of_sub_eq_zero $ by simpa [hβ‚…] using hβ‚βŸ© lemma map_mod_div_by_monic [comm_ring Ξ²] (f : Ξ± β†’ Ξ²) [is_semiring_hom f] (hq : monic q) : (p /β‚˜ q).map f = p.map f /β‚˜ q.map f ∧ (p %β‚˜ q).map f = p.map f %β‚˜ q.map f := if h01 : (0 : Ξ²) = 1 then by haveI := subsingleton_of_zero_eq_one Ξ² h01; exact ⟨subsingleton.elim _ _, subsingleton.elim _ _⟩ else have h01Ξ± : (0 : Ξ±) β‰  1, from mt (congr_arg f) (by rwa [is_semiring_hom.map_one f, is_semiring_hom.map_zero f]), have map f p /β‚˜ map f q = map f (p /β‚˜ q) ∧ map f p %β‚˜ map f q = map f (p %β‚˜ q), from (div_mod_by_monic_unique ((p /β‚˜ q).map f) _ (monic_map f hq) ⟨eq.symm $ by rw [← map_mul, ← map_add, mod_by_monic_add_div _ hq], calc _ ≀ degree (p %β‚˜ q) : degree_map_le _ ... < degree q : degree_mod_by_monic_lt _ hq $ (ne_zero_of_monic_of_zero_ne_one hq h01Ξ±) ... = _ : eq.symm $ degree_map_eq_of_leading_coeff_ne_zero _ (by rw [monic.def.1 hq, is_semiring_hom.map_one f]; exact ne.symm h01)⟩), ⟨this.1.symm, this.2.symm⟩ lemma map_div_by_monic [comm_ring Ξ²] (f : Ξ± β†’ Ξ²) [is_semiring_hom f] (hq : monic q) : (p /β‚˜ q).map f = p.map f /β‚˜ q.map f := (map_mod_div_by_monic f hq).1 lemma map_mod_by_monic [comm_ring Ξ²] (f : Ξ± β†’ Ξ²) [is_semiring_hom f] (hq : monic q) : (p %β‚˜ q).map f = p.map f %β‚˜ q.map f := (map_mod_div_by_monic f hq).2 lemma dvd_iff_mod_by_monic_eq_zero (hq : monic q) : p %β‚˜ q = 0 ↔ q ∣ p := ⟨λ h, by rw [← mod_by_monic_add_div p hq, h, zero_add]; exact dvd_mul_right _ _, Ξ» h, if hq0 : q = 0 then by rw hq0 at hq; exact (subsingleton_of_monic_zero hq).1 _ _ else let ⟨r, hr⟩ := exists_eq_mul_right_of_dvd h in by_contradiction (Ξ» hpq0, have hmod : p %β‚˜ q = q * (r - p /β‚˜ q) := by rw [mod_by_monic_eq_sub_mul_div _ hq, mul_sub, ← hr], have degree (q * (r - p /β‚˜ q)) < degree q := hmod β–Έ degree_mod_by_monic_lt _ hq hq0, have hrpq0 : leading_coeff (r - p /β‚˜ q) β‰  0 := Ξ» h, hpq0 $ leading_coeff_eq_zero.1 (by rw [hmod, leading_coeff_eq_zero.1 h, mul_zero, leading_coeff_zero]), have hlc : leading_coeff q * leading_coeff (r - p /β‚˜ q) β‰  0 := by rwa [monic.def.1 hq, one_mul], by rw [degree_mul_eq' hlc, degree_eq_nat_degree hq0, degree_eq_nat_degree (mt leading_coeff_eq_zero.2 hrpq0)] at this; exact not_lt_of_ge (nat.le_add_right _ _) (with_bot.some_lt_some.1 this))⟩ @[simp] lemma mod_by_monic_one (p : polynomial Ξ±) : p %β‚˜ 1 = 0 := (dvd_iff_mod_by_monic_eq_zero monic_one).2 (one_dvd _) @[simp] lemma div_by_monic_one (p : polynomial Ξ±) : p /β‚˜ 1 = p := by conv_rhs { rw [← mod_by_monic_add_div p monic_one] }; simp lemma degree_pos_of_root (hp : p β‰  0) (h : is_root p a) : 0 < degree p := lt_of_not_ge $ Ξ» hlt, begin have := eq_C_of_degree_le_zero hlt, rw [is_root, this, eval_C] at h, exact hp (finsupp.ext (Ξ» n, show coeff p n = 0, from nat.cases_on n h (Ξ» _, coeff_eq_zero_of_degree_lt (lt_of_le_of_lt hlt (with_bot.coe_lt_coe.2 (nat.succ_pos _)))))), end theorem monic_X_sub_C (x : Ξ±) : monic (X - C x) := by simpa only [C_neg] using monic_X_add_C (-x) theorem monic_X_pow_sub {n : β„•} (H : degree p ≀ n) : monic (X ^ (n+1) - p) := monic_X_pow_add ((degree_neg p).symm β–Έ H) theorem degree_mod_by_monic_le (p : polynomial Ξ±) {q : polynomial Ξ±} (hq : monic q) : degree (p %β‚˜ q) ≀ degree q := decidable.by_cases (assume H : q = 0, by rw [monic, H, leading_coeff_zero] at hq; have : (0:polynomial Ξ±) = 1 := (by rw [← C_0, ← C_1, hq]); rw [eq_zero_of_zero_eq_one _ this (p %β‚˜ q), eq_zero_of_zero_eq_one _ this q]; exact le_refl _) (assume H : q β‰  0, le_of_lt $ degree_mod_by_monic_lt _ hq H) lemma root_X_sub_C : is_root (X - C a) b ↔ a = b := by rw [is_root.def, eval_sub, eval_X, eval_C, sub_eq_zero_iff_eq, eq_comm] def nonzero_comm_ring.of_polynomial_ne (h : p β‰  q) : nonzero_comm_ring Ξ± := { zero := 0, one := 1, zero_ne_one := Ξ» h01, h $ by rw [← one_mul p, ← one_mul q, ← C_1, ← h01, C_0, zero_mul, zero_mul], ..show comm_ring Ξ±, by apply_instance } end comm_ring section nonzero_comm_ring variables [nonzero_comm_ring Ξ±] {p q : polynomial Ξ±} instance : nonzero_comm_ring (polynomial Ξ±) := { ..polynomial.nonzero_comm_semiring, ..polynomial.comm_ring } @[simp] lemma degree_X_sub_C (a : Ξ±) : degree (X - C a) = 1 := begin rw [sub_eq_add_neg, add_comm, ← @degree_X Ξ±], by_cases ha : a = 0, { simp only [ha, C_0, neg_zero, zero_add] }, exact degree_add_eq_of_degree_lt (by rw [degree_X, degree_neg, degree_C ha]; exact dec_trivial) end lemma degree_X_pow_sub_C {n : β„•} (hn : 0 < n) (a : Ξ±) : degree ((X : polynomial Ξ±) ^ n - C a) = n := have degree (-C a) < degree ((X : polynomial Ξ±) ^ n), from calc degree (-C a) ≀ 0 : by rw degree_neg; exact degree_C_le ... < degree ((X : polynomial Ξ±) ^ n) : by rwa [degree_X_pow]; exact with_bot.coe_lt_coe.2 hn, by rw [sub_eq_add_neg, add_comm, degree_add_eq_of_degree_lt this, degree_X_pow] lemma X_pow_sub_C_ne_zero {n : β„•} (hn : 0 < n) (a : Ξ±) : (X : polynomial Ξ±) ^ n - C a β‰  0 := mt degree_eq_bot.2 (show degree ((X : polynomial Ξ±) ^ n - C a) β‰  βŠ₯, by rw degree_X_pow_sub_C hn; exact dec_trivial) end nonzero_comm_ring section comm_ring variables [comm_ring Ξ±] {p q : polynomial Ξ±} @[simp] lemma mod_by_monic_X_sub_C_eq_C_eval (p : polynomial Ξ±) (a : Ξ±) : p %β‚˜ (X - C a) = C (p.eval a) := if h0 : (0 : Ξ±) = 1 then by letI := subsingleton_of_zero_eq_one Ξ± h0; exact subsingleton.elim _ _ else by letI : nonzero_comm_ring Ξ± := nonzero_comm_ring.of_ne h0; exact have h : (p %β‚˜ (X - C a)).eval a = p.eval a := by rw [mod_by_monic_eq_sub_mul_div _ (monic_X_sub_C a), eval_sub, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul, sub_zero], have degree (p %β‚˜ (X - C a)) < 1 := degree_X_sub_C a β–Έ degree_mod_by_monic_lt p (monic_X_sub_C a) ((degree_X_sub_C a).symm β–Έ ne_zero_of_monic (monic_X_sub_C _)), have degree (p %β‚˜ (X - C a)) ≀ 0 := begin cases (degree (p %β‚˜ (X - C a))), { exact bot_le }, { exact with_bot.some_le_some.2 (nat.le_of_lt_succ (with_bot.some_lt_some.1 this)) } end, begin rw [eq_C_of_degree_le_zero this, eval_C] at h, rw [eq_C_of_degree_le_zero this, h] end lemma mul_div_by_monic_eq_iff_is_root : (X - C a) * (p /β‚˜ (X - C a)) = p ↔ is_root p a := ⟨λ h, by rw [← h, is_root.def, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul], Ξ» h : p.eval a = 0, by conv {to_rhs, rw ← mod_by_monic_add_div p (monic_X_sub_C a)}; rw [mod_by_monic_X_sub_C_eq_C_eval, h, C_0, zero_add]⟩ lemma dvd_iff_is_root : (X - C a) ∣ p ↔ is_root p a := ⟨λ h, by rwa [← dvd_iff_mod_by_monic_eq_zero (monic_X_sub_C _), mod_by_monic_X_sub_C_eq_C_eval, ← C_0, C_inj] at h, Ξ» h, ⟨(p /β‚˜ (X - C a)), by rw mul_div_by_monic_eq_iff_is_root.2 h⟩⟩ lemma mod_by_monic_X (p : polynomial Ξ±) : p %β‚˜ X = C (p.eval 0) := by rw [← mod_by_monic_X_sub_C_eq_C_eval, C_0, sub_zero] section multiplicity def decidable_dvd_monic (p : polynomial Ξ±) (hq : monic q) : decidable (q ∣ p) := decidable_of_iff (p %β‚˜ q = 0) (dvd_iff_mod_by_monic_eq_zero hq) open_locale classical lemma multiplicity_X_sub_C_finite (a : Ξ±) (h0 : p β‰  0) : multiplicity.finite (X - C a) p := multiplicity_finite_of_degree_pos_of_monic (have (0 : Ξ±) β‰  1, from (Ξ» h, by haveI := subsingleton_of_zero_eq_one _ h; exact h0 (subsingleton.elim _ _)), by letI : nonzero_comm_ring Ξ± := { zero_ne_one := this, ..show comm_ring Ξ±, by apply_instance }; rw degree_X_sub_C; exact dec_trivial) (monic_X_sub_C _) h0 def root_multiplicity (a : Ξ±) (p : polynomial Ξ±) : β„• := if h0 : p = 0 then 0 else let I : decidable_pred (Ξ» n : β„•, Β¬(X - C a) ^ (n + 1) ∣ p) := Ξ» n, @not.decidable _ (decidable_dvd_monic p (monic_pow (monic_X_sub_C a) (n + 1))) in by exactI nat.find (multiplicity_X_sub_C_finite a h0) lemma root_multiplicity_eq_multiplicity (p : polynomial Ξ±) (a : Ξ±) : root_multiplicity a p = if h0 : p = 0 then 0 else (multiplicity (X - C a) p).get (multiplicity_X_sub_C_finite a h0) := by simp [multiplicity, root_multiplicity, roption.dom]; congr; funext; congr lemma pow_root_multiplicity_dvd (p : polynomial Ξ±) (a : Ξ±) : (X - C a) ^ root_multiplicity a p ∣ p := if h : p = 0 then by simp [h] else by rw [root_multiplicity_eq_multiplicity, dif_neg h]; exact multiplicity.pow_multiplicity_dvd _ lemma div_by_monic_mul_pow_root_multiplicity_eq (p : polynomial Ξ±) (a : Ξ±) : p /β‚˜ ((X - C a) ^ root_multiplicity a p) * (X - C a) ^ root_multiplicity a p = p := have monic ((X - C a) ^ root_multiplicity a p), from monic_pow (monic_X_sub_C _) _, by conv_rhs { rw [← mod_by_monic_add_div p this, (dvd_iff_mod_by_monic_eq_zero this).2 (pow_root_multiplicity_dvd _ _)] }; simp [mul_comm] lemma eval_div_by_monic_pow_root_multiplicity_ne_zero {p : polynomial Ξ±} (a : Ξ±) (hp : p β‰  0) : (p /β‚˜ ((X - C a) ^ root_multiplicity a p)).eval a β‰  0 := begin letI : nonzero_comm_ring Ξ± := nonzero_comm_ring.of_polynomial_ne hp, rw [ne.def, ← is_root.def, ← dvd_iff_is_root], rintros ⟨q, hq⟩, have := div_by_monic_mul_pow_root_multiplicity_eq p a, rw [mul_comm, hq, ← mul_assoc, ← pow_succ', root_multiplicity_eq_multiplicity, dif_neg hp] at this, exact multiplicity.is_greatest' (multiplicity_finite_of_degree_pos_of_monic (show (0 : with_bot β„•) < degree (X - C a), by rw degree_X_sub_C; exact dec_trivial) _ hp) (nat.lt_succ_self _) (dvd_of_mul_right_eq _ this) end end multiplicity end comm_ring section integral_domain variables [integral_domain Ξ±] {p q : polynomial Ξ±} @[simp] lemma degree_mul_eq : degree (p * q) = degree p + degree q := if hp0 : p = 0 then by simp only [hp0, degree_zero, zero_mul, with_bot.bot_add] else if hq0 : q = 0 then by simp only [hq0, degree_zero, mul_zero, with_bot.add_bot] else degree_mul_eq' $ mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (mt leading_coeff_eq_zero.1 hq0) @[simp] lemma degree_pow_eq (p : polynomial Ξ±) (n : β„•) : degree (p ^ n) = add_monoid.smul n (degree p) := by induction n; [simp only [pow_zero, degree_one, add_monoid.zero_smul], simp only [*, pow_succ, succ_smul, degree_mul_eq]] @[simp] lemma leading_coeff_mul (p q : polynomial Ξ±) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin by_cases hp : p = 0, { simp only [hp, zero_mul, leading_coeff_zero] }, { by_cases hq : q = 0, { simp only [hq, mul_zero, leading_coeff_zero] }, { rw [leading_coeff_mul'], exact mul_ne_zero (mt leading_coeff_eq_zero.1 hp) (mt leading_coeff_eq_zero.1 hq) } } end @[simp] lemma leading_coeff_pow (p : polynomial Ξ±) (n : β„•) : leading_coeff (p ^ n) = leading_coeff p ^ n := by induction n; [simp only [pow_zero, leading_coeff_one], simp only [*, pow_succ, leading_coeff_mul]] instance : integral_domain (polynomial Ξ±) := { eq_zero_or_eq_zero_of_mul_eq_zero := Ξ» a b h, begin have : leading_coeff 0 = leading_coeff a * leading_coeff b := h β–Έ leading_coeff_mul a b, rw [leading_coeff_zero, eq_comm] at this, erw [← leading_coeff_eq_zero, ← leading_coeff_eq_zero], exact eq_zero_or_eq_zero_of_mul_eq_zero this end, ..polynomial.nonzero_comm_ring } lemma nat_degree_mul_eq (hp : p β‰  0) (hq : q β‰  0) : nat_degree (p * q) = nat_degree p + nat_degree q := by rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree (mul_ne_zero hp hq), with_bot.coe_add, ← degree_eq_nat_degree hp, ← degree_eq_nat_degree hq, degree_mul_eq] @[simp] lemma nat_degree_pow_eq (p : polynomial Ξ±) (n : β„•) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp [hp0, hn0] else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else nat_degree_pow_eq' (by rw [← leading_coeff_pow, ne.def, leading_coeff_eq_zero]; exact pow_ne_zero _ hp0) lemma root_or_root_of_root_mul (h : is_root (p * q) a) : is_root p a ∨ is_root q a := by rw [is_root, eval_mul] at h; exact eq_zero_or_eq_zero_of_mul_eq_zero h lemma degree_le_mul_left (p : polynomial Ξ±) (hq : q β‰  0) : degree p ≀ degree (p * q) := if hp : p = 0 then by simp only [hp, zero_mul, le_refl] else by rw [degree_mul_eq, degree_eq_nat_degree hp, degree_eq_nat_degree hq]; exact with_bot.coe_le_coe.2 (nat.le_add_right _ _) lemma exists_finset_roots : βˆ€ {p : polynomial Ξ±} (hp : p β‰  0), βˆƒ s : finset Ξ±, (s.card : with_bot β„•) ≀ degree p ∧ βˆ€ x, x ∈ s ↔ is_root p x | p := Ξ» hp, by haveI := classical.prop_decidable (βˆƒ x, is_root p x); exact if h : βˆƒ x, is_root p x then let ⟨x, hx⟩ := h in have hpd : 0 < degree p := degree_pos_of_root hp hx, have hd0 : p /β‚˜ (X - C x) β‰  0 := Ξ» h, by rw [← mul_div_by_monic_eq_iff_is_root.2 hx, h, mul_zero] at hp; exact hp rfl, have wf : degree (p /β‚˜ _) < degree p := degree_div_by_monic_lt _ (monic_X_sub_C x) hp ((degree_X_sub_C x).symm β–Έ dec_trivial), let ⟨t, htd, htr⟩ := @exists_finset_roots (p /β‚˜ (X - C x)) hd0 in have hdeg : degree (X - C x) ≀ degree p := begin rw [degree_X_sub_C, degree_eq_nat_degree hp], rw degree_eq_nat_degree hp at hpd, exact with_bot.coe_le_coe.2 (with_bot.coe_lt_coe.1 hpd) end, have hdiv0 : p /β‚˜ (X - C x) β‰  0 := mt (div_by_monic_eq_zero_iff (monic_X_sub_C x) (ne_zero_of_monic (monic_X_sub_C x))).1 $ not_lt.2 hdeg, ⟨insert x t, calc (card (insert x t) : with_bot β„•) ≀ card t + 1 : with_bot.coe_le_coe.2 $ finset.card_insert_le _ _ ... ≀ degree p : by rw [← degree_add_div_by_monic (monic_X_sub_C x) hdeg, degree_X_sub_C, add_comm]; exact add_le_add' (le_refl (1 : with_bot β„•)) htd, begin assume y, rw [mem_insert, htr, eq_comm, ← root_X_sub_C], conv {to_rhs, rw ← mul_div_by_monic_eq_iff_is_root.2 hx}, exact ⟨λ h, or.cases_on h (root_mul_right_of_is_root _) (root_mul_left_of_is_root _), root_or_root_of_root_mul⟩ end⟩ else βŸ¨βˆ…, (degree_eq_nat_degree hp).symm β–Έ with_bot.coe_le_coe.2 (nat.zero_le _), by simpa only [not_mem_empty, false_iff, not_exists] using h⟩ using_well_founded {dec_tac := tactic.assumption} /-- `roots p` noncomputably gives a finset containing all the roots of `p` -/ noncomputable def roots (p : polynomial Ξ±) : finset Ξ± := if h : p = 0 then βˆ… else classical.some (exists_finset_roots h) lemma card_roots (hp0 : p β‰  0) : ((roots p).card : with_bot β„•) ≀ degree p := begin unfold roots, rw dif_neg hp0, exact (classical.some_spec (exists_finset_roots hp0)).1 end lemma card_roots' {p : polynomial Ξ±} (hp0 : p β‰  0) : p.roots.card ≀ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots hp0) (le_of_eq $ degree_eq_nat_degree hp0)) lemma card_roots_sub_C {p : polynomial Ξ±} {a : Ξ±} (hp0 : 0 < degree p) : ((p - C a).roots.card : with_bot β„•) ≀ degree p := calc ((p - C a).roots.card : with_bot β„•) ≀ degree (p - C a) : card_roots $ mt sub_eq_zero.1 $ Ξ» h, not_le_of_gt hp0 $ h.symm β–Έ degree_C_le ... = degree p : by rw [sub_eq_add_neg, ← C_neg]; exact degree_add_C hp0 lemma card_roots_sub_C' {p : polynomial Ξ±} {a : Ξ±} (hp0 : 0 < degree p) : (p - C a).roots.card ≀ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots_sub_C hp0) (le_of_eq $ degree_eq_nat_degree (Ξ» h, by simp [*, lt_irrefl] at *))) @[simp] lemma mem_roots (hp : p β‰  0) : a ∈ p.roots ↔ is_root p a := by unfold roots; rw dif_neg hp; exact (classical.some_spec (exists_finset_roots hp)).2 _ @[simp] lemma mem_roots_sub_C {p : polynomial Ξ±} {a x : Ξ±} (hp0 : 0 < degree p) : x ∈ (p - C a).roots ↔ p.eval x = a := (mem_roots (show p - C a β‰  0, from mt sub_eq_zero.1 $ Ξ» h, not_le_of_gt hp0 $ h.symm β–Έ degree_C_le)).trans (by rw [is_root.def, eval_sub, eval_C, sub_eq_zero]) lemma card_roots_X_pow_sub_C {n : β„•} (hn : 0 < n) (a : Ξ±) : (roots ((X : polynomial Ξ±) ^ n - C a)).card ≀ n := with_bot.coe_le_coe.1 $ calc ((roots ((X : polynomial Ξ±) ^ n - C a)).card : with_bot β„•) ≀ degree ((X : polynomial Ξ±) ^ n - C a) : card_roots (X_pow_sub_C_ne_zero hn a) ... = n : degree_X_pow_sub_C hn a /-- `nth_roots n a` noncomputably returns the solutions to `x ^ n = a`-/ def nth_roots {Ξ± : Type*} [integral_domain Ξ±] (n : β„•) (a : Ξ±) : finset Ξ± := roots ((X : polynomial Ξ±) ^ n - C a) @[simp] lemma mem_nth_roots {Ξ± : Type*} [integral_domain Ξ±] {n : β„•} (hn : 0 < n) {a x : Ξ±} : x ∈ nth_roots n a ↔ x ^ n = a := by rw [nth_roots, mem_roots (X_pow_sub_C_ne_zero hn a), is_root.def, eval_sub, eval_C, eval_pow, eval_X, sub_eq_zero_iff_eq] lemma card_nth_roots {Ξ± : Type*} [integral_domain Ξ±] (n : β„•) (a : Ξ±) : (nth_roots n a).card ≀ n := if hn : n = 0 then if h : (X : polynomial Ξ±) ^ n - C a = 0 then by simp only [nat.zero_le, nth_roots, roots, h, dif_pos rfl, card_empty] else with_bot.coe_le_coe.1 (le_trans (card_roots h) (by rw [hn, pow_zero, ← C_1, ← @is_ring_hom.map_sub _ _ _ _ (@C Ξ± _)]; exact degree_C_le)) else by rw [← with_bot.coe_le_coe, ← degree_X_pow_sub_C (nat.pos_of_ne_zero hn) a]; exact card_roots (X_pow_sub_C_ne_zero (nat.pos_of_ne_zero hn) a) lemma coeff_comp_degree_mul_degree (hqd0 : nat_degree q β‰  0) : coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p := if hp0 : p = 0 then by simp [hp0] else calc coeff (p.comp q) (nat_degree p * nat_degree q) = p.sum (Ξ» n a, coeff (C a * q ^ n) (nat_degree p * nat_degree q)) : by rw [comp, evalβ‚‚, coeff_sum] ... = coeff (C (leading_coeff p) * q ^ nat_degree p) (nat_degree p * nat_degree q) : finset.sum_eq_single _ begin assume b hbs hbp, have hq0 : q β‰  0, from Ξ» hq0, hqd0 (by rw [hq0, nat_degree_zero]), have : coeff p b β‰  0, rwa [← apply_eq_coeff, ← finsupp.mem_support_iff], dsimp [apply_eq_coeff], refine coeff_eq_zero_of_degree_lt _, rw [degree_mul_eq, degree_C this, degree_pow_eq, zero_add, degree_eq_nat_degree hq0, ← with_bot.coe_smul, add_monoid.smul_eq_mul, with_bot.coe_lt_coe, nat.cast_id], exact (mul_lt_mul_right (nat.pos_of_ne_zero hqd0)).2 (lt_of_le_of_ne (with_bot.coe_le_coe.1 (by rw ← degree_eq_nat_degree hp0; exact le_sup hbs)) hbp) end (by rw [finsupp.mem_support_iff, apply_eq_coeff, ← leading_coeff, ne.def, leading_coeff_eq_zero, classical.not_not]; simp {contextual := tt}) ... = _ : have coeff (q ^ nat_degree p) (nat_degree p * nat_degree q) = leading_coeff (q ^ nat_degree p), by rw [leading_coeff, nat_degree_pow_eq], by rw [coeff_C_mul, this, leading_coeff_pow] lemma nat_degree_comp : nat_degree (p.comp q) = nat_degree p * nat_degree q := le_antisymm nat_degree_comp_le (if hp0 : p = 0 then by rw [hp0, zero_comp, nat_degree_zero, zero_mul] else if hqd0 : nat_degree q = 0 then have degree q ≀ 0, by rw [← with_bot.coe_zero, ← hqd0]; exact degree_le_nat_degree, by rw [eq_C_of_degree_le_zero this]; simp else le_nat_degree_of_ne_zero $ have hq0 : q β‰  0, from Ξ» hq0, hqd0 $ by rw [hq0, nat_degree_zero], calc coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p : coeff_comp_degree_mul_degree hqd0 ... β‰  0 : mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (pow_ne_zero _ (mt leading_coeff_eq_zero.1 hq0))) lemma leading_coeff_comp (hq : nat_degree q β‰  0) : leading_coeff (p.comp q) = leading_coeff p * leading_coeff q ^ nat_degree p := by rw [← coeff_comp_degree_mul_degree hq, ← nat_degree_comp]; refl lemma degree_eq_zero_of_is_unit (h : is_unit p) : degree p = 0 := let ⟨q, hq⟩ := is_unit_iff_dvd_one.1 h in have hp0 : p β‰  0, from Ξ» hp0, by simpa [hp0] using hq, have hq0 : q β‰  0, from Ξ» hp0, by simpa [hp0] using hq, have nat_degree (1 : polynomial Ξ±) = nat_degree (p * q), from congr_arg _ hq, by rw [nat_degree_one, nat_degree_mul_eq hp0 hq0, eq_comm, add_eq_zero_iff, ← with_bot.coe_eq_coe, ← degree_eq_nat_degree hp0] at this; exact this.1 @[simp] lemma degree_coe_units (u : units (polynomial Ξ±)) : degree (u : polynomial Ξ±) = 0 := degree_eq_zero_of_is_unit ⟨u, rfl⟩ @[simp] lemma nat_degree_coe_units (u : units (polynomial Ξ±)) : nat_degree (u : polynomial Ξ±) = 0 := nat_degree_eq_of_degree_eq_some (degree_coe_units u) lemma coeff_coe_units_zero_ne_zero (u : units (polynomial Ξ±)) : coeff (u : polynomial Ξ±) 0 β‰  0 := begin conv in (0) {rw [← nat_degree_coe_units u]}, rw [← leading_coeff, ne.def, leading_coeff_eq_zero], exact units.coe_ne_zero _ end lemma degree_eq_degree_of_associated (h : associated p q) : degree p = degree q := let ⟨u, hu⟩ := h in by simp [hu.symm] lemma degree_eq_one_of_irreducible_of_root (hi : irreducible p) {x : Ξ±} (hx : is_root p x) : degree p = 1 := let ⟨g, hg⟩ := dvd_iff_is_root.2 hx in have is_unit (X - C x) ∨ is_unit g, from hi.2 _ _ hg, this.elim (Ξ» h, have h₁ : degree (X - C x) = 1, from degree_X_sub_C x, have hβ‚‚ : degree (X - C x) = 0, from degree_eq_zero_of_is_unit h, by rw h₁ at hβ‚‚; exact absurd hβ‚‚ dec_trivial) (Ξ» hgu, by rw [hg, degree_mul_eq, degree_X_sub_C, degree_eq_zero_of_is_unit hgu, add_zero]) end integral_domain section field variables [discrete_field Ξ±] {p q : polynomial Ξ±} instance : vector_space Ξ± (polynomial Ξ±) := finsupp.vector_space _ _ lemma is_unit_iff_degree_eq_zero : is_unit p ↔ degree p = 0 := ⟨degree_eq_zero_of_is_unit, Ξ» h, have degree p ≀ 0, by simp [*, le_refl], have hc : coeff p 0 β‰  0, from Ξ» hc, by rw [eq_C_of_degree_le_zero this, hc] at h; simpa using h, is_unit_iff_dvd_one.2 ⟨C (coeff p 0)⁻¹, begin conv in p { rw eq_C_of_degree_le_zero this }, rw [← C_mul, _root_.mul_inv_cancel hc, C_1] end⟩⟩ lemma degree_pos_of_ne_zero_of_nonunit (hp0 : p β‰  0) (hp : Β¬is_unit p) : 0 < degree p := lt_of_not_ge (Ξ» h, by rw [eq_C_of_degree_le_zero h] at hp0 hp; exact (hp $ is_unit.map' C $ is_unit.mk0 (coeff p 0) (mt C_inj.2 (by simpa using hp0)))) lemma irreducible_of_degree_eq_one (hp1 : degree p = 1) : irreducible p := ⟨mt is_unit_iff_dvd_one.1 (Ξ» ⟨q, hq⟩, absurd (congr_arg degree hq) (Ξ» h, have degree q = 0, by rw [degree_one, degree_mul_eq, hp1, eq_comm, nat.with_bot.add_eq_zero_iff] at h; exact h.2, by simp [degree_mul_eq, this, degree_one, hp1] at h; exact absurd h dec_trivial)), Ξ» q r hpqr, begin have := congr_arg degree hpqr, rw [hp1, degree_mul_eq, eq_comm, nat.with_bot.add_eq_one_iff] at this, rw [is_unit_iff_degree_eq_zero, is_unit_iff_degree_eq_zero]; tautology end⟩ lemma monic_mul_leading_coeff_inv (h : p β‰  0) : monic (p * C (leading_coeff p)⁻¹) := by rw [monic, leading_coeff_mul, leading_coeff_C, mul_inv_cancel (show leading_coeff p β‰  0, from mt leading_coeff_eq_zero.1 h)] lemma degree_mul_leading_coeff_inv (p : polynomial Ξ±) (h : q β‰  0) : degree (p * C (leading_coeff q)⁻¹) = degree p := have h₁ : (leading_coeff q)⁻¹ β‰  0 := inv_ne_zero (mt leading_coeff_eq_zero.1 h), by rw [degree_mul_eq, degree_C h₁, add_zero] def div (p q : polynomial Ξ±) := C (leading_coeff q)⁻¹ * (p /β‚˜ (q * C (leading_coeff q)⁻¹)) def mod (p q : polynomial Ξ±) := p %β‚˜ (q * C (leading_coeff q)⁻¹) private lemma quotient_mul_add_remainder_eq_aux (p q : polynomial Ξ±) : q * div p q + mod p q = p := if h : q = 0 then by simp only [h, zero_mul, mod, mod_by_monic_zero, zero_add] else begin conv {to_rhs, rw ← mod_by_monic_add_div p (monic_mul_leading_coeff_inv h)}, rw [div, mod, add_comm, mul_assoc] end private lemma remainder_lt_aux (p : polynomial Ξ±) (hq : q β‰  0) : degree (mod p q) < degree q := by rw ← degree_mul_leading_coeff_inv q hq; exact degree_mod_by_monic_lt p (monic_mul_leading_coeff_inv hq) (mul_ne_zero hq (mt leading_coeff_eq_zero.2 (by rw leading_coeff_C; exact inv_ne_zero (mt leading_coeff_eq_zero.1 hq)))) instance : has_div (polynomial Ξ±) := ⟨div⟩ instance : has_mod (polynomial Ξ±) := ⟨mod⟩ lemma div_def : p / q = C (leading_coeff q)⁻¹ * (p /β‚˜ (q * C (leading_coeff q)⁻¹)) := rfl lemma mod_def : p % q = p %β‚˜ (q * C (leading_coeff q)⁻¹) := rfl lemma mod_by_monic_eq_mod (p : polynomial Ξ±) (hq : monic q) : p %β‚˜ q = p % q := show p %β‚˜ q = p %β‚˜ (q * C (leading_coeff q)⁻¹), by simp only [monic.def.1 hq, inv_one, mul_one, C_1] lemma div_by_monic_eq_div (p : polynomial Ξ±) (hq : monic q) : p /β‚˜ q = p / q := show p /β‚˜ q = C (leading_coeff q)⁻¹ * (p /β‚˜ (q * C (leading_coeff q)⁻¹)), by simp only [monic.def.1 hq, inv_one, C_1, one_mul, mul_one] lemma mod_X_sub_C_eq_C_eval (p : polynomial Ξ±) (a : Ξ±) : p % (X - C a) = C (p.eval a) := mod_by_monic_eq_mod p (monic_X_sub_C a) β–Έ mod_by_monic_X_sub_C_eq_C_eval _ _ lemma mul_div_eq_iff_is_root : (X - C a) * (p / (X - C a)) = p ↔ is_root p a := div_by_monic_eq_div p (monic_X_sub_C a) β–Έ mul_div_by_monic_eq_iff_is_root instance : euclidean_domain (polynomial Ξ±) := { quotient := (/), quotient_zero := by simp [div_def], remainder := (%), r := _, r_well_founded := degree_lt_wf, quotient_mul_add_remainder_eq := quotient_mul_add_remainder_eq_aux, remainder_lt := Ξ» p q hq, remainder_lt_aux _ hq, mul_left_not_lt := Ξ» p q hq, not_lt_of_ge (degree_le_mul_left _ hq) } lemma mod_eq_self_iff (hq0 : q β‰  0) : p % q = p ↔ degree p < degree q := ⟨λ h, h β–Έ euclidean_domain.mod_lt _ hq0, Ξ» h, have Β¬degree (q * C (leading_coeff q)⁻¹) ≀ degree p := not_le_of_gt $ by rwa degree_mul_leading_coeff_inv q hq0, begin rw [mod_def, mod_by_monic, dif_pos (monic_mul_leading_coeff_inv hq0)], unfold div_mod_by_monic_aux, simp only [this, false_and, if_false] end⟩ lemma div_eq_zero_iff (hq0 : q β‰  0) : p / q = 0 ↔ degree p < degree q := ⟨λ h, by have := euclidean_domain.div_add_mod p q; rwa [h, mul_zero, zero_add, mod_eq_self_iff hq0] at this, Ξ» h, have hlt : degree p < degree (q * C (leading_coeff q)⁻¹), by rwa degree_mul_leading_coeff_inv q hq0, have hm : monic (q * C (leading_coeff q)⁻¹) := monic_mul_leading_coeff_inv hq0, by rw [div_def, (div_by_monic_eq_zero_iff hm (ne_zero_of_monic hm)).2 hlt, mul_zero]⟩ lemma degree_add_div (hq0 : q β‰  0) (hpq : degree q ≀ degree p) : degree q + degree (p / q) = degree p := have degree (p % q) < degree (q * (p / q)) := calc degree (p % q) < degree q : euclidean_domain.mod_lt _ hq0 ... ≀ _ : degree_le_mul_left _ (mt (div_eq_zero_iff hq0).1 (not_lt_of_ge hpq)), by conv {to_rhs, rw [← euclidean_domain.div_add_mod p q, add_comm, degree_add_eq_of_degree_lt this, degree_mul_eq]} lemma degree_div_le (p q : polynomial Ξ±) : degree (p / q) ≀ degree p := if hq : q = 0 then by simp [hq] else by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq]; exact degree_div_by_monic_le _ _ lemma degree_div_lt (hp : p β‰  0) (hq : 0 < degree q) : degree (p / q) < degree p := have hq0 : q β‰  0, from Ξ» hq0, by simpa [hq0] using hq, by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq0]; exact degree_div_by_monic_lt _ (monic_mul_leading_coeff_inv hq0) hp (by rw degree_mul_leading_coeff_inv _ hq0; exact hq) @[simp] lemma degree_map [discrete_field Ξ²] (p : polynomial Ξ±) (f : Ξ± β†’ Ξ²) [is_ring_hom f] : degree (p.map f) = degree p := p.degree_map_eq_of_injective (is_ring_hom.injective f) @[simp] lemma nat_degree_map [discrete_field Ξ²] (f : Ξ± β†’ Ξ²) [is_ring_hom f] : nat_degree (p.map f) = nat_degree p := nat_degree_eq_of_degree_eq (degree_map _ f) @[simp] lemma leading_coeff_map [discrete_field Ξ²] (f : Ξ± β†’ Ξ²) [is_ring_hom f] : leading_coeff (p.map f) = f (leading_coeff p) := by simp [leading_coeff, coeff_map f] lemma map_div [discrete_field Ξ²] (f : Ξ± β†’ Ξ²) [is_ring_hom f] : (p / q).map f = p.map f / q.map f := if hq0 : q = 0 then by simp [hq0] else by rw [div_def, div_def, map_mul, map_div_by_monic f (monic_mul_leading_coeff_inv hq0)]; simp [is_ring_hom.map_inv f, leading_coeff, coeff_map f] lemma map_mod [discrete_field Ξ²] (f : Ξ± β†’ Ξ²) [is_ring_hom f] : (p % q).map f = p.map f % q.map f := if hq0 : q = 0 then by simp [hq0] else by rw [mod_def, mod_def, leading_coeff_map f, ← is_ring_hom.map_inv f, ← map_C f, ← map_mul f, map_mod_by_monic f (monic_mul_leading_coeff_inv hq0)] @[simp] lemma map_eq_zero [discrete_field Ξ²] (f : Ξ± β†’ Ξ²) [is_ring_hom f] : p.map f = 0 ↔ p = 0 := by simp [polynomial.ext_iff, is_ring_hom.map_eq_zero f, coeff_map] lemma exists_root_of_degree_eq_one (h : degree p = 1) : βˆƒ x, is_root p x := ⟨-(p.coeff 0 / p.coeff 1), have p.coeff 1 β‰  0, by rw ← nat_degree_eq_of_degree_eq_some h; exact mt leading_coeff_eq_zero.1 (Ξ» h0, by simpa [h0] using h), by conv in p { rw [eq_X_add_C_of_degree_le_one (show degree p ≀ 1, by rw h; exact le_refl _)] }; simp [is_root, mul_div_cancel' _ this]⟩ lemma coeff_inv_units (u : units (polynomial Ξ±)) (n : β„•) : ((↑u : polynomial Ξ±).coeff n)⁻¹ = ((↑u⁻¹ : polynomial Ξ±).coeff n) := begin rw [eq_C_of_degree_eq_zero (degree_coe_units u), eq_C_of_degree_eq_zero (degree_coe_units u⁻¹), coeff_C, coeff_C, inv_eq_one_div], split_ifs, { rw [div_eq_iff_mul_eq (coeff_coe_units_zero_ne_zero u), coeff_zero_eq_eval_zero, coeff_zero_eq_eval_zero, ← eval_mul, ← units.coe_mul, inv_mul_self]; simp }, { simp } end instance : normalization_domain (polynomial Ξ±) := { norm_unit := Ξ» p, if hp0 : p = 0 then 1 else ⟨C p.leading_coeff⁻¹, C p.leading_coeff, by rw [← C_mul, inv_mul_cancel, C_1]; exact mt leading_coeff_eq_zero.1 hp0, by rw [← C_mul, mul_inv_cancel, C_1]; exact mt leading_coeff_eq_zero.1 hp0,⟩, norm_unit_zero := dif_pos rfl, norm_unit_mul := Ξ» p q hp0 hq0, begin rw [dif_neg hp0, dif_neg hq0, dif_neg (mul_ne_zero hp0 hq0)], apply units.ext, show C (leading_coeff (p * q))⁻¹ = C (leading_coeff p)⁻¹ * C (leading_coeff q)⁻¹, rw [leading_coeff_mul, mul_inv', C_mul, mul_comm] end, norm_unit_coe_units := Ξ» u, have hu : degree ↑u⁻¹ = 0, from degree_eq_zero_of_is_unit ⟨u⁻¹, rfl⟩, begin apply units.ext, rw [dif_neg (units.coe_ne_zero u)], conv_rhs {rw eq_C_of_degree_eq_zero hu}, refine C_inj.2 _, rw [← nat_degree_eq_of_degree_eq_some hu, leading_coeff, coeff_inv_units], simp end, ..polynomial.integral_domain } lemma monic_normalize (hp0 : p β‰  0) : monic (normalize p) := show leading_coeff (p * ↑(dite _ _ _)) = 1, by rw dif_neg hp0; exact monic_mul_leading_coeff_inv hp0 lemma coe_norm_unit (hp : p β‰  0) : (norm_unit p : polynomial Ξ±) = C p.leading_coeff⁻¹ := show ↑(dite _ _ _) = C p.leading_coeff⁻¹, by rw dif_neg hp; refl end field section derivative variables [comm_semiring Ξ±] /-- `derivative p` formal derivative of the polynomial `p` -/ def derivative (p : polynomial Ξ±) : polynomial Ξ± := p.sum (Ξ»n a, C (a * n) * X^(n - 1)) lemma coeff_derivative (p : polynomial Ξ±) (n : β„•) : coeff (derivative p) n = coeff p (n + 1) * (n + 1) := begin rw [derivative], simp only [coeff_X_pow, coeff_sum, coeff_C_mul], rw [finsupp.sum, finset.sum_eq_single (n + 1), apply_eq_coeff], { rw [if_pos (nat.add_sub_cancel _ _).symm, mul_one, nat.cast_add, nat.cast_one] }, { 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] } }, { intro H, rw [not_mem_support_iff.1 H, zero_mul, zero_mul] } end @[simp] lemma derivative_zero : derivative (0 : polynomial Ξ±) = 0 := finsupp.sum_zero_index lemma derivative_monomial (a : Ξ±) (n : β„•) : derivative (C a * X ^ n) = C (a * n) * X^(n - 1) := by rw [← single_eq_C_mul_X, ← single_eq_C_mul_X, derivative, sum_single_index, single_eq_C_mul_X]; simp only [zero_mul, C_0]; refl @[simp] lemma derivative_C {a : Ξ±} : derivative (C a) = 0 := suffices derivative (C a * X^0) = C (a * 0:Ξ±) * X ^ 0, by simpa only [mul_one, zero_mul, C_0, mul_zero, pow_zero], derivative_monomial a 0 @[simp] lemma derivative_X : derivative (X : polynomial Ξ±) = 1 := suffices derivative (C (1:Ξ±) * X^1) = C (1 * (1:β„•)) * X ^ 0, by simpa only [mul_one, one_mul, C_1, pow_one, nat.cast_one, pow_zero], derivative_monomial 1 1 @[simp] lemma derivative_one : derivative (1 : polynomial Ξ±) = 0 := derivative_C @[simp] lemma derivative_add {f g : polynomial Ξ±} : derivative (f + g) = derivative f + derivative g := by refine finsupp.sum_add_index _ _; intros; simp only [add_mul, zero_mul, C_0, C_add, C_mul] instance : is_add_monoid_hom (derivative : polynomial Ξ± β†’ polynomial Ξ±) := { map_add := Ξ» _ _, derivative_add, map_zero := derivative_zero } @[simp] lemma derivative_sum {s : finset Ξ²} {f : Ξ² β†’ polynomial Ξ±} : derivative (s.sum f) = s.sum (Ξ»b, derivative (f b)) := (finset.sum_hom derivative).symm @[simp] lemma derivative_mul {f g : polynomial Ξ±} : 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 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 single_eq_C_mul_X }, exact derivative_monomial _ _ 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.sub_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] } }, unfold derivative finsupp.sum, simp only [sum_add_distrib, finset.mul_sum, finset.sum_mul] end lemma derivative_eval (p : polynomial Ξ±) (x : Ξ±) : p.derivative.eval x = p.sum (Ξ» n a, (a * n)*x^(n-1)) := by simp [derivative, eval_sum, eval_pow] end derivative section domain variables [integral_domain Ξ±] lemma mem_support_derivative [char_zero Ξ±] (p : polynomial Ξ±) (n : β„•) : n ∈ (derivative p).support ↔ n + 1 ∈ p.support := suffices (Β¬(coeff p (n + 1) = 0 ∨ ((n + 1:β„•) : Ξ±) = 0)) ↔ coeff p (n + 1) β‰  0, by simpa only [coeff_derivative, apply_eq_coeff, mem_support_iff, 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 Ξ±] (p : polynomial Ξ±) (hp : 0 < nat_degree p) : degree (derivative p) = (nat_degree p - 1 : β„•) := le_antisymm (le_trans (degree_sum_le _ _) $ sup_le $ assume n hn, have n ≀ nat_degree p, begin rw [← with_bot.coe_le_coe, ← degree_eq_nat_degree], { refine le_degree_of_ne_zero _, simpa only [mem_support_iff] using hn }, { assume h, simpa only [h, support_zero] using hn } end, le_trans (degree_monomial_le _ _) $ with_bot.coe_le_coe.2 $ nat.sub_le_sub_right this _) begin refine le_sup _, rw [mem_support_derivative, nat.sub_add_cancel, 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 end domain section identities /- @TODO: pow_add_expansion and pow_sub_pow_factor are not specific to polynomials. These belong somewhere else. But not in group_power because they depend on tactic.ring Maybe use data.nat.choose to prove it. -/ def pow_add_expansion {Ξ± : Type*} [comm_semiring Ξ±] (x y : Ξ±) : βˆ€ (n : β„•), {k // (x + y)^n = x^n + n*x^(n-1)*y + k * y^2} | 0 := ⟨0, by simp⟩ | 1 := ⟨0, by simp⟩ | (n+2) := begin cases pow_add_expansion (n+1) with z hz, existsi x*z + (n+1)*x^n+z*y, calc (x + y) ^ (n + 2) = (x + y) * (x + y) ^ (n + 1) : by ring_exp ... = (x + y) * (x ^ (n + 1) + ↑(n + 1) * x ^ (n + 1 - 1) * y + z * y ^ 2) : by rw hz ... = x ^ (n + 2) + ↑(n + 2) * x ^ (n + 1) * y + (x*z + (n+1)*x^n+z*y) * y ^ 2 : by { push_cast, ring_exp! } end variables [comm_ring Ξ±] private def poly_binom_aux1 (x y : Ξ±) (e : β„•) (a : Ξ±) : {k : Ξ± // a * (x + y)^e = a * (x^e + e*x^(e-1)*y + k*y^2)} := begin existsi (pow_add_expansion x y e).val, congr, apply (pow_add_expansion _ _ _).property end private lemma poly_binom_aux2 (f : polynomial Ξ±) (x y : Ξ±) : f.eval (x + y) = f.sum (Ξ» e a, a * (x^e + e*x^(e-1)*y + (poly_binom_aux1 x y e a).val*y^2)) := begin unfold eval evalβ‚‚, congr, ext, apply (poly_binom_aux1 x y _ _).property end private lemma poly_binom_aux3 (f : polynomial Ξ±) (x y : Ξ±) : f.eval (x + y) = f.sum (Ξ» e a, a * x^e) + f.sum (Ξ» e a, (a * e * x^(e-1)) * y) + f.sum (Ξ» e a, (a *(poly_binom_aux1 x y e a).val)*y^2) := by rw poly_binom_aux2; simp [left_distrib, finsupp.sum_add, mul_assoc] def binom_expansion (f : polynomial Ξ±) (x y : Ξ±) : {k : Ξ± // f.eval (x + y) = f.eval x + (f.derivative.eval x) * y + k * y^2} := begin existsi f.sum (Ξ» e a, a *((poly_binom_aux1 x y e a).val)), rw poly_binom_aux3, congr, { rw derivative_eval, symmetry, apply finsupp.sum_mul }, { symmetry, apply finsupp.sum_mul } end def pow_sub_pow_factor (x y : Ξ±) : Ξ  {i : β„•},{z : Ξ± // x^i - y^i = z*(x - y)} | 0 := ⟨0, by simp⟩ | 1 := ⟨1, by simp⟩ | (k+2) := begin cases @pow_sub_pow_factor (k+1) with z hz, existsi z*x + y^(k+1), calc x ^ (k + 2) - y ^ (k + 2) = x * (x ^ (k + 1) - y ^ (k + 1)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by ring_exp ... = x * (z * (x - y)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by rw hz ... = (z * x + y ^ (k + 1)) * (x - y) : by ring_exp end def eval_sub_factor (f : polynomial Ξ±) (x y : Ξ±) : {z : Ξ± // f.eval x - f.eval y = z*(x - y)} := begin existsi f.sum (Ξ» a b, b * (pow_sub_pow_factor x y).val), unfold eval evalβ‚‚, rw [←finsupp.sum_sub], have : finsupp.sum f (Ξ» (a : β„•) (b : Ξ±), b * (pow_sub_pow_factor x y).val) * (x - y) = finsupp.sum f (Ξ» (a : β„•) (b : Ξ±), b * (pow_sub_pow_factor x y).val * (x - y)), { apply finsupp.sum_mul }, rw this, congr, ext e a, rw [mul_assoc, ←(pow_sub_pow_factor x y).property], simp [left_distrib] end end identities end polynomial
979c75c196557384bc815a826685e7bee9e416f9
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_trace.lean
0ffbbe06e179766ecf1abd31c3dae3e195f27d3d
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
120
lean
set_option trace.blast true set_option trace.blast.action false example (a b : Prop) : a ∧ b β†’ b ∧ a := by blast
ad84745238268fe803477b525b4005f0a83db8a3
d796eac6dc113f68ec6fc0579c13e8eae2bdef6c
/Resources/Code/Category+Theory-Github-Topic/monoidal-categories-reboot/src/monoidal_categories_reboot/tensor_product.lean
326cb974171057311bdae95c430e496593c3462e
[ "Apache-2.0" ]
permissive
paddlelaw/functional-discipline-content-cats
5a7e13e8adedd96b94f66b0b3cbf1847bc86d7f6
d93b7aa849b343c384cec40c73ee84a9300004e8
refs/heads/master
1,675,541,859,508
1,607,251,766,000
1,607,251,766,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,322
lean
-- Copyright (c) 2018 Michael Jendrusch. All rights reserved. import category_theory.category import category_theory.functor import category_theory.products import category_theory.natural_isomorphism open category_theory universes v u open category_theory.category open category_theory.functor open category_theory.prod open category_theory.functor.category.nat_trans namespace category_theory.monoidal @[reducible] def tensor_obj_type (C : Sort u) [category.{v} C] := C β†’ C β†’ C @[reducible] def tensor_hom_type {C : Sort u} [category.{v} C] (tensor_obj : tensor_obj_type C) : Sort (imax u u u u v) := Ξ  {X₁ Y₁ Xβ‚‚ Yβ‚‚ : C}, has_hom.hom X₁ Y₁ β†’ has_hom.hom Xβ‚‚ Yβ‚‚ β†’ has_hom.hom (tensor_obj X₁ Xβ‚‚) (tensor_obj Y₁ Yβ‚‚) local attribute [tidy] tactic.assumption def assoc_obj {C : Sort u} [category.{v} C] (tensor_obj : tensor_obj_type C) : Sort (max u v 1) := Ξ  X Y Z : C, (tensor_obj (tensor_obj X Y) Z) β‰… (tensor_obj X (tensor_obj Y Z)) def assoc_natural {C : Sort u} [category.{v} C] (tensor_obj : tensor_obj_type C) (tensor_hom : tensor_hom_type tensor_obj) (assoc : assoc_obj tensor_obj) : Prop := βˆ€ {X₁ Xβ‚‚ X₃ Y₁ Yβ‚‚ Y₃ : C} (f₁ : X₁ ⟢ Y₁) (fβ‚‚ : Xβ‚‚ ⟢ Yβ‚‚) (f₃ : X₃ ⟢ Y₃), (tensor_hom (tensor_hom f₁ fβ‚‚) f₃) ≫ (assoc Y₁ Yβ‚‚ Y₃).hom = (assoc X₁ Xβ‚‚ X₃).hom ≫ (tensor_hom f₁ (tensor_hom fβ‚‚ f₃)) def left_unitor_obj {C : Sort u} [category.{v} C] (tensor_obj : tensor_obj_type C) (tensor_unit : C) : Sort (max v 1 u) := Ξ  X : C, (tensor_obj tensor_unit X) β‰… X def left_unitor_natural {C : Sort u} [category.{v} C] (tensor_obj : tensor_obj_type C) (tensor_hom : tensor_hom_type tensor_obj) (tensor_unit : C) (left_unitor : left_unitor_obj tensor_obj tensor_unit) : Prop := βˆ€ {X Y : C} (f : X ⟢ Y), (tensor_hom (πŸ™ tensor_unit) f) ≫ (left_unitor Y).hom = (left_unitor X).hom ≫ f def right_unitor_obj {C : Sort u} [category.{v} C] (tensor_obj : tensor_obj_type C) (tensor_unit : C) : Sort (max v 1 u) := Ξ  (X : C), (tensor_obj X tensor_unit) β‰… X def right_unitor_natural {C : Sort u} [category.{v} C] (tensor_obj : tensor_obj_type C) (tensor_hom : tensor_hom_type tensor_obj) (tensor_unit : C) (right_unitor : right_unitor_obj tensor_obj tensor_unit) : Prop := βˆ€ {X Y : C} (f : X ⟢ Y), (tensor_hom f (πŸ™ tensor_unit)) ≫ (right_unitor Y).hom = (right_unitor X).hom ≫ f @[reducible] def pentagon {C : Sort u} [category.{v} C] {tensor_obj : tensor_obj_type C} (tensor_hom : tensor_hom_type tensor_obj) (assoc : assoc_obj tensor_obj) : Prop := βˆ€ W X Y Z : C, (tensor_hom (assoc W X Y).hom (πŸ™ Z)) ≫ (assoc W (tensor_obj X Y) Z).hom ≫ (tensor_hom (πŸ™ W) (assoc X Y Z).hom) = (assoc (tensor_obj W X) Y Z).hom ≫ (assoc W X (tensor_obj Y Z)).hom @[reducible] def triangle {C : Sort u} [category.{v} C] {tensor_obj : tensor_obj_type C} {tensor_unit : C} (tensor_hom : tensor_hom_type tensor_obj) (left_unitor : left_unitor_obj tensor_obj tensor_unit) (right_unitor : right_unitor_obj tensor_obj tensor_unit) (assoc : assoc_obj tensor_obj) : Prop := βˆ€ X Y : C, (assoc X tensor_unit Y).hom ≫ (tensor_hom (πŸ™ X) (left_unitor Y).hom) = tensor_hom (right_unitor X).hom (πŸ™ Y) end category_theory.monoidal
725b197bb14652dc138423eb7154f960e1d8d295
d29d82a0af640c937e499f6be79fc552eae0aa13
/src/group_theory/subgroup.lean
7f8b05fec3b83bdc265ccb46fbfd46711b5402cc
[ "Apache-2.0" ]
permissive
AbdulMajeedkhurasani/mathlib
835f8a5c5cf3075b250b3737172043ab4fa1edf6
79bc7323b164aebd000524ebafd198eb0e17f956
refs/heads/master
1,688,003,895,660
1,627,788,521,000
1,627,788,521,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
84,747
lean
/- Copyright (c) 2020 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import group_theory.submonoid import algebra.group.conj import algebra.pointwise import order.atoms /-! # Subgroups This file defines multiplicative and additive subgroups as an extension of submonoids, in a bundled form (unbundled subgroups are in `deprecated/subgroups.lean`). We prove subgroups of a group form a complete lattice, and results about images and preimages of subgroups under group homomorphisms. The bundled subgroups use bundled monoid homomorphisms. There are also theorems about the subgroups generated by an element or a subset of a group, defined both inductively and as the infimum of the set of subgroups containing a given element/subset. Special thanks goes to Amelia Livingston and Yury Kudryashov for their help and inspiration. ## Main definitions Notation used here: - `G N` are `group`s - `A` is an `add_group` - `H K` are `subgroup`s of `G` or `add_subgroup`s of `A` - `x` is an element of type `G` or type `A` - `f g : N β†’* G` are group homomorphisms - `s k` are sets of elements of type `G` Definitions in the file: * `subgroup G` : the type of subgroups of a group `G` * `add_subgroup A` : the type of subgroups of an additive group `A` * `complete_lattice (subgroup G)` : the subgroups of `G` form a complete lattice * `subgroup.closure k` : the minimal subgroup that includes the set `k` * `subgroup.subtype` : the natural group homomorphism from a subgroup of group `G` to `G` * `subgroup.gi` : `closure` forms a Galois insertion with the coercion to set * `subgroup.comap H f` : the preimage of a subgroup `H` along the group homomorphism `f` is also a subgroup * `subgroup.map f H` : the image of a subgroup `H` along the group homomorphism `f` is also a subgroup * `subgroup.prod H K` : the product of subgroups `H`, `K` of groups `G`, `N` respectively, `H Γ— K` is a subgroup of `G Γ— N` * `monoid_hom.range f` : the range of the group homomorphism `f` is a subgroup * `monoid_hom.ker f` : the kernel of a group homomorphism `f` is the subgroup of elements `x : G` such that `f x = 1` * `monoid_hom.eq_locus f g` : given group homomorphisms `f`, `g`, the elements of `G` such that `f x = g x` form a subgroup of `G` * `is_simple_group G` : a class indicating that a group has exactly two normal subgroups ## Implementation notes Subgroup inclusion is denoted `≀` rather than `βŠ†`, although `∈` is defined as membership of a subgroup's underlying set. ## Tags subgroup, subgroups -/ open_locale big_operators variables {G : Type*} [group G] variables {A : Type*} [add_group A] set_option old_structure_cmd true /-- A subgroup of a group `G` is a subset containing 1, closed under multiplication and closed under multiplicative inverse. -/ structure subgroup (G : Type*) [group G] extends submonoid G := (inv_mem' {x} : x ∈ carrier β†’ x⁻¹ ∈ carrier) /-- An additive subgroup of an additive group `G` is a subset containing 0, closed under addition and additive inverse. -/ structure add_subgroup (G : Type*) [add_group G] extends add_submonoid G:= (neg_mem' {x} : x ∈ carrier β†’ -x ∈ carrier) attribute [to_additive] subgroup attribute [to_additive add_subgroup.to_add_submonoid] subgroup.to_submonoid /-- Reinterpret a `subgroup` as a `submonoid`. -/ add_decl_doc subgroup.to_submonoid /-- Reinterpret an `add_subgroup` as an `add_submonoid`. -/ add_decl_doc add_subgroup.to_add_submonoid namespace subgroup @[to_additive] instance : set_like (subgroup G) G := ⟨subgroup.carrier, Ξ» p q h, by cases p; cases q; congr'⟩ @[simp, to_additive] lemma mem_carrier {s : subgroup G} {x : G} : x ∈ s.carrier ↔ x ∈ s := iff.rfl /-- See Note [custom simps projection] -/ @[to_additive "See Note [custom simps projection]"] def simps.coe (S : subgroup G) : set G := S initialize_simps_projections subgroup (carrier β†’ coe) initialize_simps_projections add_subgroup (carrier β†’ coe) @[simp, to_additive] lemma coe_to_submonoid (K : subgroup G) : (K.to_submonoid : set G) = K := rfl @[simp, to_additive] lemma mem_to_submonoid (K : subgroup G) (x : G) : x ∈ K.to_submonoid ↔ x ∈ K := iff.rfl @[to_additive] instance (K : subgroup G) [d : decidable_pred (∈ K)] [fintype G] : fintype K := show fintype {g : G // g ∈ K}, from infer_instance @[to_additive] theorem to_submonoid_injective : function.injective (to_submonoid : subgroup G β†’ submonoid G) := Ξ» p q h, set_like.ext'_iff.2 (show _, from set_like.ext'_iff.1 h) @[simp, to_additive] theorem to_submonoid_eq {p q : subgroup G} : p.to_submonoid = q.to_submonoid ↔ p = q := to_submonoid_injective.eq_iff @[mono, to_additive] lemma to_submonoid_strict_mono : strict_mono (to_submonoid : subgroup G β†’ submonoid G) := Ξ» _ _, id @[mono, to_additive] lemma to_submonoid_mono : monotone (to_submonoid : subgroup G β†’ submonoid G) := to_submonoid_strict_mono.monotone @[simp, to_additive] lemma to_submonoid_le {p q : subgroup G} : p.to_submonoid ≀ q.to_submonoid ↔ p ≀ q := iff.rfl end subgroup /-! ### Conversion to/from `additive`/`multiplicative` -/ section mul_add /-- Supgroups of a group `G` are isomorphic to additive subgroups of `additive G`. -/ @[simps] def subgroup.to_add_subgroup : subgroup G ≃o add_subgroup (additive G) := { to_fun := Ξ» S, { neg_mem' := S.inv_mem', ..S.to_submonoid.to_add_submonoid }, inv_fun := Ξ» S, { inv_mem' := S.neg_mem', ..S.to_add_submonoid.to_submonoid' }, left_inv := Ξ» x, by cases x; refl, right_inv := Ξ» x, by cases x; refl, map_rel_iff' := Ξ» a b, iff.rfl, } /-- Additive subgroup of an additive group `additive G` are isomorphic to subgroup of `G`. -/ abbreviation add_subgroup.to_subgroup' : add_subgroup (additive G) ≃o subgroup G := subgroup.to_add_subgroup.symm /-- Additive supgroups of an additive group `A` are isomorphic to subgroups of `multiplicative A`. -/ @[simps] def add_subgroup.to_subgroup : add_subgroup A ≃o subgroup (multiplicative A) := { to_fun := Ξ» S, { inv_mem' := S.neg_mem', ..S.to_add_submonoid.to_submonoid }, inv_fun := Ξ» S, { neg_mem' := S.inv_mem', ..S.to_submonoid.to_add_submonoid' }, left_inv := Ξ» x, by cases x; refl, right_inv := Ξ» x, by cases x; refl, map_rel_iff' := Ξ» a b, iff.rfl, } /-- Subgroups of an additive group `multiplicative A` are isomorphic to additive subgroups of `A`. -/ abbreviation subgroup.to_add_subgroup' : subgroup (multiplicative A) ≃o add_subgroup A := add_subgroup.to_subgroup.symm end mul_add namespace subgroup variables (H K : subgroup G) /-- Copy of a subgroup with a new `carrier` equal to the old one. Useful to fix definitional equalities.-/ @[to_additive "Copy of an additive subgroup with a new `carrier` equal to the old one. Useful to fix definitional equalities"] protected def copy (K : subgroup G) (s : set G) (hs : s = K) : subgroup G := { carrier := s, one_mem' := hs.symm β–Έ K.one_mem', mul_mem' := hs.symm β–Έ K.mul_mem', inv_mem' := hs.symm β–Έ K.inv_mem' } /-- Two subgroups are equal if they have the same elements. -/ @[ext, to_additive "Two `add_subgroup`s are equal if they have the same elements."] theorem ext {H K : subgroup G} (h : βˆ€ x, x ∈ H ↔ x ∈ K) : H = K := set_like.ext h attribute [ext] add_subgroup.ext /-- A subgroup contains the group's 1. -/ @[to_additive "An `add_subgroup` contains the group's 0."] theorem one_mem : (1 : G) ∈ H := H.one_mem' /-- A subgroup is closed under multiplication. -/ @[to_additive "An `add_subgroup` is closed under addition."] theorem mul_mem {x y : G} : x ∈ H β†’ y ∈ H β†’ x * y ∈ H := Ξ» hx hy, H.mul_mem' hx hy /-- A subgroup is closed under inverse. -/ @[to_additive "An `add_subgroup` is closed under inverse."] theorem inv_mem {x : G} : x ∈ H β†’ x⁻¹ ∈ H := Ξ» hx, H.inv_mem' hx /-- A subgroup is closed under division. -/ @[to_additive "An `add_subgroup` is closed under subtraction."] theorem div_mem {x y : G} (hx : x ∈ H) (hy : y ∈ H) : x / y ∈ H := by simpa only [div_eq_mul_inv] using H.mul_mem' hx (H.inv_mem' hy) @[simp, to_additive] theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H := ⟨λ h, inv_inv x β–Έ H.inv_mem h, H.inv_mem⟩ @[to_additive] lemma div_mem_comm_iff {a b : G} : a / b ∈ H ↔ b / a ∈ H := by rw [← H.inv_mem_iff, div_eq_mul_inv, div_eq_mul_inv, mul_inv_rev, inv_inv] @[simp, to_additive] theorem inv_coe_set : (H : set G)⁻¹ = H := by { ext, simp, } @[simp, to_additive] lemma exists_inv_mem_iff_exists_mem (K : subgroup G) {P : G β†’ Prop} : (βˆƒ (x : G), x ∈ K ∧ P x⁻¹) ↔ βˆƒ x ∈ K, P x := by split; { rintros ⟨x, x_in, hx⟩, exact ⟨x⁻¹, inv_mem K x_in, by simp [hx]⟩ } @[to_additive] lemma mul_mem_cancel_right {x y : G} (h : x ∈ H) : y * x ∈ H ↔ y ∈ H := ⟨λ hba, by simpa using H.mul_mem hba (H.inv_mem h), Ξ» hb, H.mul_mem hb h⟩ @[to_additive] lemma mul_mem_cancel_left {x y : G} (h : x ∈ H) : x * y ∈ H ↔ y ∈ H := ⟨λ hab, by simpa using H.mul_mem (H.inv_mem h) hab, H.mul_mem h⟩ /-- Product of a list of elements in a subgroup is in the subgroup. -/ @[to_additive "Sum of a list of elements in an `add_subgroup` is in the `add_subgroup`."] lemma list_prod_mem {l : list G} : (βˆ€ x ∈ l, x ∈ K) β†’ l.prod ∈ K := K.to_submonoid.list_prod_mem /-- Product of a multiset of elements in a subgroup of a `comm_group` is in the subgroup. -/ @[to_additive "Sum of a multiset of elements in an `add_subgroup` of an `add_comm_group` is in the `add_subgroup`."] lemma multiset_prod_mem {G} [comm_group G] (K : subgroup G) (g : multiset G) : (βˆ€ a ∈ g, a ∈ K) β†’ g.prod ∈ K := K.to_submonoid.multiset_prod_mem g /-- Product of elements of a subgroup of a `comm_group` indexed by a `finset` is in the subgroup. -/ @[to_additive "Sum of elements in an `add_subgroup` of an `add_comm_group` indexed by a `finset` is in the `add_subgroup`."] lemma prod_mem {G : Type*} [comm_group G] (K : subgroup G) {ΞΉ : Type*} {t : finset ΞΉ} {f : ΞΉ β†’ G} (h : βˆ€ c ∈ t, f c ∈ K) : ∏ c in t, f c ∈ K := K.to_submonoid.prod_mem h lemma pow_mem {x : G} (hx : x ∈ K) : βˆ€ n : β„•, x ^ n ∈ K := K.to_submonoid.pow_mem hx lemma gpow_mem {x : G} (hx : x ∈ K) : βˆ€ n : β„€, x ^ n ∈ K | (n : β„•) := by { rw [gpow_coe_nat], exact pow_mem _ hx n } | -[1+ n] := by { rw [gpow_neg_succ_of_nat], exact K.inv_mem (K.pow_mem hx n.succ) } /-- Construct a subgroup from a nonempty set that is closed under division. -/ @[to_additive "Construct a subgroup from a nonempty set that is closed under subtraction"] def of_div (s : set G) (hsn : s.nonempty) (hs : βˆ€ x y ∈ s, x * y⁻¹ ∈ s) : subgroup G := have one_mem : (1 : G) ∈ s, from let ⟨x, hx⟩ := hsn in by simpa using hs x x hx hx, have inv_mem : βˆ€ x, x ∈ s β†’ x⁻¹ ∈ s, from Ξ» x hx, by simpa using hs 1 x one_mem hx, { carrier := s, one_mem' := one_mem, inv_mem' := inv_mem, mul_mem' := Ξ» x y hx hy, by simpa using hs x y⁻¹ hx (inv_mem y hy) } /-- A subgroup of a group inherits a multiplication. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits an addition."] instance has_mul : has_mul H := H.to_submonoid.has_mul /-- A subgroup of a group inherits a 1. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits a zero."] instance has_one : has_one H := H.to_submonoid.has_one /-- A subgroup of a group inherits an inverse. -/ @[to_additive "A `add_subgroup` of a `add_group` inherits an inverse."] instance has_inv : has_inv H := ⟨λ a, ⟨a⁻¹, H.inv_mem a.2⟩⟩ /-- A subgroup of a group inherits a division -/ @[to_additive "An `add_subgroup` of an `add_group` inherits a subtraction."] instance has_div : has_div H := ⟨λ a b, ⟨a / b, H.div_mem a.2 b.2⟩⟩ @[simp, norm_cast, to_additive] lemma coe_mul (x y : H) : (↑(x * y) : G) = ↑x * ↑y := rfl @[simp, norm_cast, to_additive] lemma coe_one : ((1 : H) : G) = 1 := rfl @[simp, norm_cast, to_additive] lemma coe_inv (x : H) : ↑(x⁻¹ : H) = (x⁻¹ : G) := rfl @[simp, norm_cast, to_additive] lemma coe_div (x y : H) : (↑(x / y) : G) = ↑x / ↑y := rfl @[simp, norm_cast, to_additive] lemma coe_mk (x : G) (hx : x ∈ H) : ((⟨x, hx⟩ : H) : G) = x := rfl attribute [norm_cast] add_subgroup.coe_add add_subgroup.coe_zero add_subgroup.coe_neg add_subgroup.coe_mk /-- A subgroup of a group inherits a group structure. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits an `add_group` structure."] instance to_group {G : Type*} [group G] (H : subgroup G) : group H := subtype.coe_injective.group _ rfl (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl) /-- A subgroup of a `comm_group` is a `comm_group`. -/ @[to_additive "An `add_subgroup` of an `add_comm_group` is an `add_comm_group`."] instance to_comm_group {G : Type*} [comm_group G] (H : subgroup G) : comm_group H := subtype.coe_injective.comm_group _ rfl (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl) /-- A subgroup of an `ordered_comm_group` is an `ordered_comm_group`. -/ @[to_additive "An `add_subgroup` of an `add_ordered_comm_group` is an `add_ordered_comm_group`."] instance to_ordered_comm_group {G : Type*} [ordered_comm_group G] (H : subgroup G) : ordered_comm_group H := subtype.coe_injective.ordered_comm_group _ rfl (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl) /-- A subgroup of a `linear_ordered_comm_group` is a `linear_ordered_comm_group`. -/ @[to_additive "An `add_subgroup` of a `linear_ordered_add_comm_group` is a `linear_ordered_add_comm_group`."] instance to_linear_ordered_comm_group {G : Type*} [linear_ordered_comm_group G] (H : subgroup G) : linear_ordered_comm_group H := subtype.coe_injective.linear_ordered_comm_group _ rfl (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl) /-- The natural group hom from a subgroup of group `G` to `G`. -/ @[to_additive "The natural group hom from an `add_subgroup` of `add_group` `G` to `G`."] def subtype : H β†’* G := ⟨coe, rfl, Ξ» _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑H.subtype = coe := rfl @[simp, norm_cast] lemma coe_pow (x : H) (n : β„•) : ((x ^ n : H) : G) = x ^ n := coe_subtype H β–Έ monoid_hom.map_pow _ _ _ @[simp, norm_cast] lemma coe_gpow (x : H) (n : β„€) : ((x ^ n : H) : G) = x ^ n := coe_subtype H β–Έ monoid_hom.map_gpow _ _ _ /-- The inclusion homomorphism from a subgroup `H` contained in `K` to `K`. -/ @[to_additive "The inclusion homomorphism from a additive subgroup `H` contained in `K` to `K`."] def inclusion {H K : subgroup G} (h : H ≀ K) : H β†’* K := monoid_hom.mk' (Ξ» x, ⟨x, h x.prop⟩) (Ξ» ⟨a, ha⟩ ⟨b, hb⟩, rfl) @[simp, to_additive] lemma coe_inclusion {H K : subgroup G} {h : H ≀ K} (a : H) : (inclusion h a : G) = a := by { cases a, simp only [inclusion, coe_mk, monoid_hom.mk'_apply] } @[simp, to_additive] lemma subtype_comp_inclusion {H K : subgroup G} (hH : H ≀ K) : K.subtype.comp (inclusion hH) = H.subtype := by { ext, simp } /-- The subgroup `G` of the group `G`. -/ @[to_additive "The `add_subgroup G` of the `add_group G`."] instance : has_top (subgroup G) := ⟨{ inv_mem' := Ξ» _ _, set.mem_univ _ , .. (⊀ : submonoid G) }⟩ /-- The trivial subgroup `{1}` of an group `G`. -/ @[to_additive "The trivial `add_subgroup` `{0}` of an `add_group` `G`."] instance : has_bot (subgroup G) := ⟨{ inv_mem' := Ξ» _, by simp *, .. (βŠ₯ : submonoid G) }⟩ @[to_additive] instance : inhabited (subgroup G) := ⟨βŠ₯⟩ @[simp, to_additive] lemma mem_bot {x : G} : x ∈ (βŠ₯ : subgroup G) ↔ x = 1 := iff.rfl @[simp, to_additive] lemma mem_top (x : G) : x ∈ (⊀ : subgroup G) := set.mem_univ x @[simp, to_additive] lemma coe_top : ((⊀ : subgroup G) : set G) = set.univ := rfl @[simp, to_additive] lemma coe_bot : ((βŠ₯ : subgroup G) : set G) = {1} := rfl @[to_additive] lemma eq_bot_iff_forall : H = βŠ₯ ↔ βˆ€ x ∈ H, x = (1 : G) := begin rw set_like.ext'_iff, simp only [coe_bot, set.eq_singleton_iff_unique_mem, set_like.mem_coe, H.one_mem, true_and], end @[to_additive] lemma eq_bot_of_subsingleton [subsingleton H] : H = βŠ₯ := begin rw subgroup.eq_bot_iff_forall, intros y hy, rw [← subgroup.coe_mk H y hy, subsingleton.elim (⟨y, hy⟩ : H) 1, subgroup.coe_one], end @[to_additive] instance fintype_bot : fintype (βŠ₯ : subgroup G) := ⟨{1}, by {rintro ⟨x, ⟨hx⟩⟩, exact finset.mem_singleton_self _}⟩ @[simp] lemma _root_.add_subgroup.card_bot : fintype.card (βŠ₯ : add_subgroup A) = 1 := fintype.card_eq_one_iff.2 ⟨⟨(0 : A), set.mem_singleton 0⟩, Ξ» ⟨y, hy⟩, subtype.eq $ add_subgroup.mem_bot.1 hy⟩ -- `@[to_additive]` doesn't work, because it converts the `1 : β„•` to `0`. @[simp] lemma card_bot : fintype.card (βŠ₯ : subgroup G) = 1 := fintype.card_eq_one_iff.2 ⟨⟨(1 : G), set.mem_singleton 1⟩, Ξ» ⟨y, hy⟩, subtype.eq $ subgroup.mem_bot.1 hy⟩ @[to_additive] lemma eq_top_of_card_eq [fintype H] [fintype G] (h : fintype.card H = fintype.card G) : H = ⊀ := begin haveI : fintype (H : set G) := β€Ήfintype Hβ€Ί, rw [set_like.ext'_iff, coe_top, ← finset.coe_univ, ← (H : set G).coe_to_finset, finset.coe_inj, ← finset.card_eq_iff_eq_univ, ← h, set.to_finset_card], congr end @[to_additive] lemma eq_top_of_le_card [fintype H] [fintype G] (h : fintype.card G ≀ fintype.card H) : H = ⊀ := eq_top_of_card_eq H (le_antisymm (fintype.card_le_of_injective coe subtype.coe_injective) h) @[to_additive] lemma eq_bot_of_card_le [fintype H] (h : fintype.card H ≀ 1) : H = βŠ₯ := let _ := fintype.card_le_one_iff_subsingleton.mp h in by exactI eq_bot_of_subsingleton H @[to_additive] lemma eq_bot_of_card_eq [fintype H] (h : fintype.card H = 1) : H = βŠ₯ := H.eq_bot_of_card_le (le_of_eq h) @[to_additive] lemma nontrivial_iff_exists_ne_one (H : subgroup G) : nontrivial H ↔ βˆƒ x ∈ H, x β‰  (1:G) := subtype.nontrivial_iff_exists_ne (Ξ» x, x ∈ H) (1 : H) /-- A subgroup is either the trivial subgroup or nontrivial. -/ @[to_additive] lemma bot_or_nontrivial (H : subgroup G) : H = βŠ₯ ∨ nontrivial H := begin classical, by_cases h : βˆ€ x ∈ H, x = (1 : G), { left, exact H.eq_bot_iff_forall.mpr h }, { right, push_neg at h, simpa [nontrivial_iff_exists_ne_one] using h }, end /-- A subgroup is either the trivial subgroup or contains a nonzero element. -/ @[to_additive] lemma bot_or_exists_ne_one (H : subgroup G) : H = βŠ₯ ∨ βˆƒ x ∈ H, x β‰  (1:G) := begin convert H.bot_or_nontrivial, rw nontrivial_iff_exists_ne_one end /-- The inf of two subgroups is their intersection. -/ @[to_additive "The inf of two `add_subgroups`s is their intersection."] instance : has_inf (subgroup G) := ⟨λ H₁ Hβ‚‚, { inv_mem' := Ξ» _ ⟨hx, hx'⟩, ⟨H₁.inv_mem hx, Hβ‚‚.inv_mem hx'⟩, .. H₁.to_submonoid βŠ“ Hβ‚‚.to_submonoid }⟩ @[simp, to_additive] lemma coe_inf (p p' : subgroup G) : ((p βŠ“ p' : subgroup G) : set G) = p ∩ p' := rfl @[simp, to_additive] lemma mem_inf {p p' : subgroup G} {x : G} : x ∈ p βŠ“ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[to_additive] instance : has_Inf (subgroup G) := ⟨λ s, { inv_mem' := Ξ» x hx, set.mem_bInter $ Ξ» i h, i.inv_mem (by apply set.mem_bInter_iff.1 hx i h), .. (β¨… S ∈ s, subgroup.to_submonoid S).copy (β‹‚ S ∈ s, ↑S) (by simp) }⟩ @[simp, to_additive] lemma coe_Inf (H : set (subgroup G)) : ((Inf H : subgroup G) : set G) = β‹‚ s ∈ H, ↑s := rfl attribute [norm_cast] coe_Inf add_subgroup.coe_Inf @[simp, to_additive] lemma mem_Inf {S : set (subgroup G)} {x : G} : x ∈ Inf S ↔ βˆ€ p ∈ S, x ∈ p := set.mem_bInter_iff @[to_additive] lemma mem_infi {ΞΉ : Sort*} {S : ΞΉ β†’ subgroup G} {x : G} : (x ∈ β¨… i, S i) ↔ βˆ€ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp, to_additive] lemma coe_infi {ΞΉ : Sort*} {S : ΞΉ β†’ subgroup G} : (↑(β¨… i, S i) : set G) = β‹‚ i, S i := by simp only [infi, coe_Inf, set.bInter_range] attribute [norm_cast] coe_infi add_subgroup.coe_infi /-- Subgroups of a group form a complete lattice. -/ @[to_additive "The `add_subgroup`s of an `add_group` form a complete lattice."] instance : complete_lattice (subgroup G) := { bot := (βŠ₯), bot_le := Ξ» S x hx, (mem_bot.1 hx).symm β–Έ S.one_mem, top := (⊀), le_top := Ξ» S x hx, mem_top x, inf := (βŠ“), le_inf := Ξ» a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := Ξ» a b x, and.left, inf_le_right := Ξ» a b x, and.right, .. complete_lattice_of_Inf (subgroup G) $ Ξ» s, is_glb.of_image (Ξ» H K, show (H : set G) ≀ K ↔ H ≀ K, from set_like.coe_subset_coe) is_glb_binfi } @[to_additive] lemma mem_sup_left {S T : subgroup G} : βˆ€ {x : G}, x ∈ S β†’ x ∈ S βŠ” T := show S ≀ S βŠ” T, from le_sup_left @[to_additive] lemma mem_sup_right {S T : subgroup G} : βˆ€ {x : G}, x ∈ T β†’ x ∈ S βŠ” T := show T ≀ S βŠ” T, from le_sup_right @[to_additive] lemma mem_supr_of_mem {ΞΉ : Type*} {S : ΞΉ β†’ subgroup G} (i : ΞΉ) : βˆ€ {x : G}, x ∈ S i β†’ x ∈ supr S := show S i ≀ supr S, from le_supr _ _ @[to_additive] lemma mem_Sup_of_mem {S : set (subgroup G)} {s : subgroup G} (hs : s ∈ S) : βˆ€ {x : G}, x ∈ s β†’ x ∈ Sup S := show s ≀ Sup S, from le_Sup hs @[to_additive] lemma subsingleton_iff : subsingleton G ↔ subsingleton (subgroup G) := ⟨ Ξ» h, by exactI ⟨λ x y, subgroup.ext $ Ξ» i, subsingleton.elim 1 i β–Έ by simp [subgroup.one_mem]⟩, Ξ» h, by exactI ⟨λ x y, have βˆ€ i : G, i = 1 := Ξ» i, mem_bot.mp $ subsingleton.elim (⊀ : subgroup G) βŠ₯ β–Έ mem_top i, (this x).trans (this y).symm⟩⟩ @[to_additive] lemma nontrivial_iff : nontrivial G ↔ nontrivial (subgroup G) := not_iff_not.mp ( (not_nontrivial_iff_subsingleton.trans subsingleton_iff).trans not_nontrivial_iff_subsingleton.symm) @[to_additive] instance [subsingleton G] : unique (subgroup G) := ⟨⟨βŠ₯⟩, Ξ» a, @subsingleton.elim _ (subsingleton_iff.mp β€Ή_β€Ί) a _⟩ @[to_additive] instance [nontrivial G] : nontrivial (subgroup G) := nontrivial_iff.mp β€Ή_β€Ί @[to_additive] lemma eq_top_iff' : H = ⊀ ↔ βˆ€ x : G, x ∈ H := eq_top_iff.trans ⟨λ h m, h $ mem_top m, Ξ» h m _, h m⟩ /-- The `subgroup` generated by a set. -/ @[to_additive "The `add_subgroup` generated by a set"] def closure (k : set G) : subgroup G := Inf {K | k βŠ† K} variable {k : set G} @[to_additive] lemma mem_closure {x : G} : x ∈ closure k ↔ βˆ€ K : subgroup G, k βŠ† K β†’ x ∈ K := mem_Inf /-- The subgroup generated by a set includes the set. -/ @[simp, to_additive "The `add_subgroup` generated by a set includes the set."] lemma subset_closure : k βŠ† closure k := Ξ» x hx, mem_closure.2 $ Ξ» K hK, hK hx open set /-- A subgroup `K` includes `closure k` if and only if it includes `k`. -/ @[simp, to_additive "An additive subgroup `K` includes `closure k` if and only if it includes `k`"] lemma closure_le : closure k ≀ K ↔ k βŠ† K := ⟨subset.trans subset_closure, Ξ» h, Inf_le h⟩ @[to_additive] lemma closure_eq_of_le (h₁ : k βŠ† K) (hβ‚‚ : K ≀ closure k) : closure k = K := le_antisymm ((closure_le $ K).2 h₁) hβ‚‚ /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k`, and is preserved under multiplication and inverse, then `p` holds for all elements of the closure of `k`. -/ @[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `k`, and is preserved under addition and inverses, then `p` holds for all elements of the additive closure of `k`."] lemma closure_induction {p : G β†’ Prop} {x} (h : x ∈ closure k) (Hk : βˆ€ x ∈ k, p x) (H1 : p 1) (Hmul : βˆ€ x y, p x β†’ p y β†’ p (x * y)) (Hinv : βˆ€ x, p x β†’ p x⁻¹) : p x := (@closure_le _ _ ⟨p, H1, Hmul, Hinv⟩ _).2 Hk h attribute [elab_as_eliminator] subgroup.closure_induction add_subgroup.closure_induction /-- An induction principle on elements of the subtype `subgroup.closure`. If `p` holds for `1` and all elements of `k`, and is preserved under multiplication and inverse, then `p` holds for all elements `x : closure k`. The difference with `subgroup.closure_induction` is that this acts on the subtype. -/ @[to_additive "An induction principle on elements of the subtype `add_subgroup.closure`. If `p` holds for `0` and all elements of `k`, and is preserved under addition and negation, then `p` holds for all elements `x : closure k`. The difference with `add_subgroup.closure_induction` is that this acts on the subtype."] lemma closure_induction' (k : set G) {p : closure k β†’ Prop} (Hk : βˆ€ x (h : x ∈ k), p ⟨x, subset_closure h⟩) (H1 : p 1) (Hmul : βˆ€ x y, p x β†’ p y β†’ p (x * y)) (Hinv : βˆ€ x, p x β†’ p x⁻¹) (x : closure k) : p x := subtype.rec_on x $ Ξ» x hx, begin refine exists.elim _ (Ξ» (hx : x ∈ closure k) (hc : p ⟨x, hx⟩), hc), exact closure_induction hx (Ξ» x hx, ⟨subset_closure hx, Hk x hx⟩) ⟨one_mem _, H1⟩ (Ξ» x y hx hy, exists.elim hx $ Ξ» hx' hx, exists.elim hy $ Ξ» hy' hy, ⟨mul_mem _ hx' hy', Hmul _ _ hx hy⟩) (Ξ» x hx, exists.elim hx $ Ξ» hx' hx, ⟨inv_mem _ hx', Hinv _ hx⟩), end attribute [elab_as_eliminator] subgroup.closure_induction' add_subgroup.closure_induction' variable (G) /-- `closure` forms a Galois insertion with the coercion to set. -/ @[to_additive "`closure` forms a Galois insertion with the coercion to set."] protected def gi : galois_insertion (@closure G _) coe := { choice := Ξ» s _, closure s, gc := Ξ» s t, @closure_le _ _ t s, le_l_u := Ξ» s, subset_closure, choice_eq := Ξ» s h, rfl } variable {G} /-- Subgroup closure of a set is monotone in its argument: if `h βŠ† k`, then `closure h ≀ closure k`. -/ @[to_additive "Additive subgroup closure of a set is monotone in its argument: if `h βŠ† k`, then `closure h ≀ closure k`"] lemma closure_mono ⦃h k : set G⦄ (h' : h βŠ† k) : closure h ≀ closure k := (subgroup.gi G).gc.monotone_l h' /-- Closure of a subgroup `K` equals `K`. -/ @[simp, to_additive "Additive closure of an additive subgroup `K` equals `K`"] lemma closure_eq : closure (K : set G) = K := (subgroup.gi G).l_u_eq K @[simp, to_additive] lemma closure_empty : closure (βˆ… : set G) = βŠ₯ := (subgroup.gi G).gc.l_bot @[simp, to_additive] lemma closure_univ : closure (univ : set G) = ⊀ := @coe_top G _ β–Έ closure_eq ⊀ @[to_additive] lemma closure_union (s t : set G) : closure (s βˆͺ t) = closure s βŠ” closure t := (subgroup.gi G).gc.l_sup @[to_additive] lemma closure_Union {ΞΉ} (s : ΞΉ β†’ set G) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subgroup.gi G).gc.l_supr @[to_additive] lemma closure_eq_bot_iff (G : Type*) [group G] (S : set G) : closure S = βŠ₯ ↔ S βŠ† {1} := by { rw [← le_bot_iff], exact closure_le _} /-- The subgroup generated by an element of a group equals the set of integer number powers of the element. -/ lemma mem_closure_singleton {x y : G} : y ∈ closure ({x} : set G) ↔ βˆƒ n : β„€, x ^ n = y := begin refine ⟨λ hy, closure_induction hy _ _ _ _, Ξ» ⟨n, hn⟩, hn β–Έ gpow_mem _ (subset_closure $ mem_singleton x) n⟩, { intros y hy, rw [eq_of_mem_singleton hy], exact ⟨1, gpow_one x⟩ }, { exact ⟨0, gpow_zero x⟩ }, { rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, exact ⟨n + m, gpow_add x n m⟩ }, rintros _ ⟨n, rfl⟩, exact ⟨-n, gpow_neg x n⟩ end lemma closure_singleton_one : closure ({1} : set G) = βŠ₯ := by simp [eq_bot_iff_forall, mem_closure_singleton] @[simp, to_additive] lemma inv_subset_closure (S : set G) : S⁻¹ βŠ† closure S := begin intros s hs, rw [set_like.mem_coe, ←subgroup.inv_mem_iff], exact subset_closure (mem_inv.mp hs), end @[simp, to_additive] lemma closure_inv (S : set G) : closure S⁻¹ = closure S := begin refine le_antisymm ((subgroup.closure_le _).2 _) ((subgroup.closure_le _).2 _), { exact inv_subset_closure S }, { simpa only [set.inv_inv] using inv_subset_closure S⁻¹ }, end @[to_additive] lemma closure_to_submonoid (S : set G) : (closure S).to_submonoid = submonoid.closure (S βˆͺ S⁻¹) := begin refine le_antisymm _ (submonoid.closure_le.2 _), { intros x hx, refine closure_induction hx (Ξ» x hx, submonoid.closure_mono (subset_union_left S S⁻¹) (submonoid.subset_closure hx)) (submonoid.one_mem _) (Ξ» x y hx hy, submonoid.mul_mem _ hx hy) (Ξ» x hx, _), rwa [←submonoid.mem_closure_inv, set.union_inv, set.inv_inv, set.union_comm] }, { simp only [true_and, coe_to_submonoid, union_subset_iff, subset_closure, inv_subset_closure] } end /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k` and their inverse, and is preserved under multiplication, then `p` holds for all elements of the closure of `k`. -/ @[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `k` and their negation, and is preserved under addition, then `p` holds for all elements of the additive closure of `k`."] lemma closure_induction'' {p : G β†’ Prop} {x} (h : x ∈ closure k) (Hk : βˆ€ x ∈ k, p x) (Hk_inv : βˆ€ x ∈ k, p x⁻¹) (H1 : p 1) (Hmul : βˆ€ x y, p x β†’ p y β†’ p (x * y)) : p x := begin rw [← mem_to_submonoid, closure_to_submonoid k] at h, refine submonoid.closure_induction h (Ξ» x hx, _) H1 (Ξ» x y hx hy, Hmul x y hx hy), { rw [mem_union, mem_inv] at hx, cases hx with mem invmem, { exact Hk x mem }, { rw [← inv_inv x], exact Hk_inv _ invmem } }, end @[to_additive] lemma mem_supr_of_directed {ΞΉ} [hΞΉ : nonempty ΞΉ] {K : ΞΉ β†’ subgroup G} (hK : directed (≀) K) {x : G} : x ∈ (supr K : subgroup G) ↔ βˆƒ i, x ∈ K i := begin refine ⟨_, Ξ» ⟨i, hi⟩, (set_like.le_def.1 $ le_supr K i) hi⟩, suffices : x ∈ closure (⋃ i, (K i : set G)) β†’ βˆƒ i, x ∈ K i, by simpa only [closure_Union, closure_eq (K _)] using this, refine (Ξ» hx, closure_induction hx (Ξ» _, mem_Union.1) _ _ _), { exact hΞΉ.elim (Ξ» i, ⟨i, (K i).one_mem⟩) }, { rintros x y ⟨i, hi⟩ ⟨j, hj⟩, rcases hK i j with ⟨k, hki, hkj⟩, exact ⟨k, (K k).mul_mem (hki hi) (hkj hj)⟩ }, rintros _ ⟨i, hi⟩, exact ⟨i, inv_mem (K i) hi⟩ end @[to_additive] lemma coe_supr_of_directed {ΞΉ} [nonempty ΞΉ] {S : ΞΉ β†’ subgroup G} (hS : directed (≀) S) : ((⨆ i, S i : subgroup G) : set G) = ⋃ i, ↑(S i) := set.ext $ Ξ» x, by simp [mem_supr_of_directed hS] @[to_additive] lemma mem_Sup_of_directed_on {K : set (subgroup G)} (Kne : K.nonempty) (hK : directed_on (≀) K) {x : G} : x ∈ Sup K ↔ βˆƒ s ∈ K, x ∈ s := begin haveI : nonempty K := Kne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hK.directed_coe, set_coe.exists, subtype.coe_mk] end variables {N : Type*} [group N] {P : Type*} [group P] /-- The preimage of a subgroup along a monoid homomorphism is a subgroup. -/ @[to_additive "The preimage of an `add_subgroup` along an `add_monoid` homomorphism is an `add_subgroup`."] def comap {N : Type*} [group N] (f : G β†’* N) (H : subgroup N) : subgroup G := { carrier := (f ⁻¹' H), inv_mem' := Ξ» a ha, show f a⁻¹ ∈ H, by rw f.map_inv; exact H.inv_mem ha, .. H.to_submonoid.comap f } @[simp, to_additive] lemma coe_comap (K : subgroup N) (f : G β†’* N) : (K.comap f : set G) = f ⁻¹' K := rfl @[simp, to_additive] lemma mem_comap {K : subgroup N} {f : G β†’* N} {x : G} : x ∈ K.comap f ↔ f x ∈ K := iff.rfl @[to_additive] lemma comap_mono {f : G β†’* N} {K K' : subgroup N} : K ≀ K' β†’ comap f K ≀ comap f K' := preimage_mono @[to_additive] lemma comap_comap (K : subgroup P) (g : N β†’* P) (f : G β†’* N) : (K.comap g).comap f = K.comap (g.comp f) := rfl /-- The image of a subgroup along a monoid homomorphism is a subgroup. -/ @[to_additive "The image of an `add_subgroup` along an `add_monoid` homomorphism is an `add_subgroup`."] def map (f : G β†’* N) (H : subgroup G) : subgroup N := { carrier := (f '' H), inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, H.inv_mem hx, f.map_inv x⟩ }, .. H.to_submonoid.map f } @[simp, to_additive] lemma coe_map (f : G β†’* N) (K : subgroup G) : (K.map f : set N) = f '' K := rfl @[simp, to_additive] lemma mem_map {f : G β†’* N} {K : subgroup G} {y : N} : y ∈ K.map f ↔ βˆƒ x ∈ K, f x = y := mem_image_iff_bex @[to_additive] lemma mem_map_of_mem (f : G β†’* N) {K : subgroup G} {x : G} (hx : x ∈ K) : f x ∈ K.map f := mem_image_of_mem f hx @[to_additive] lemma apply_coe_mem_map (f : G β†’* N) (K : subgroup G) (x : K) : f x ∈ K.map f := mem_map_of_mem f x.prop @[to_additive] lemma map_mono {f : G β†’* N} {K K' : subgroup G} : K ≀ K' β†’ map f K ≀ map f K' := image_subset _ @[to_additive] lemma map_map (g : N β†’* P) (f : G β†’* N) : (K.map f).map g = K.map (g.comp f) := set_like.coe_injective $ image_image _ _ _ @[to_additive] lemma mem_map_equiv {f : G ≃* N} {K : subgroup G} {x : N} : x ∈ K.map f.to_monoid_hom ↔ f.symm x ∈ K := @set.mem_image_equiv _ _ ↑K f.to_equiv x @[to_additive] lemma map_equiv_eq_comap_symm (f : G ≃* N) (K : subgroup G) : K.map f.to_monoid_hom = K.comap f.symm.to_monoid_hom := set_like.coe_injective (f.to_equiv.image_eq_preimage K) @[to_additive] lemma comap_equiv_eq_map_symm (f : N ≃* G) (K : subgroup G) : K.comap f.to_monoid_hom = K.map f.symm.to_monoid_hom := (map_equiv_eq_comap_symm f.symm K).symm @[to_additive] lemma map_le_iff_le_comap {f : G β†’* N} {K : subgroup G} {H : subgroup N} : K.map f ≀ H ↔ K ≀ H.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : G β†’* N) : galois_connection (map f) (comap f) := Ξ» _ _, map_le_iff_le_comap @[to_additive] lemma map_sup (H K : subgroup G) (f : G β†’* N) : (H βŠ” K).map f = H.map f βŠ” K.map f := (gc_map_comap f).l_sup @[to_additive] lemma map_supr {ΞΉ : Sort*} (f : G β†’* N) (s : ΞΉ β†’ subgroup G) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr @[to_additive] lemma comap_sup_comap_le (H K : subgroup N) (f : G β†’* N) : comap f H βŠ” comap f K ≀ comap f (H βŠ” K) := monotone.le_map_sup (Ξ» _ _, comap_mono) H K @[to_additive] lemma supr_comap_le {ΞΉ : Sort*} (f : G β†’* N) (s : ΞΉ β†’ subgroup N) : (⨆ i, (s i).comap f) ≀ (supr s).comap f := monotone.le_map_supr (Ξ» _ _, comap_mono) @[to_additive] lemma comap_inf (H K : subgroup N) (f : G β†’* N) : (H βŠ“ K).comap f = H.comap f βŠ“ K.comap f := (gc_map_comap f).u_inf @[to_additive] lemma comap_infi {ΞΉ : Sort*} (f : G β†’* N) (s : ΞΉ β†’ subgroup N) : (infi s).comap f = β¨… i, (s i).comap f := (gc_map_comap f).u_infi @[to_additive] lemma map_inf_le (H K : subgroup G) (f : G β†’* N) : map f (H βŠ“ K) ≀ map f H βŠ“ map f K := le_inf (map_mono inf_le_left) (map_mono inf_le_right) @[to_additive] lemma map_inf_eq (H K : subgroup G) (f : G β†’* N) (hf : function.injective f) : map f (H βŠ“ K) = map f H βŠ“ map f K := begin rw ← set_like.coe_set_eq, simp [set.image_inter hf], end @[simp, to_additive] lemma map_bot (f : G β†’* N) : (βŠ₯ : subgroup G).map f = βŠ₯ := (gc_map_comap f).l_bot @[simp, to_additive] lemma comap_top (f : G β†’* N) : (⊀ : subgroup N).comap f = ⊀ := (gc_map_comap f).u_top @[simp, to_additive] lemma comap_subtype_inf_left {H K : subgroup G} : comap H.subtype (H βŠ“ K) = comap H.subtype K := ext $ Ξ» x, and_iff_right_of_imp (Ξ» _, x.prop) @[simp, to_additive] lemma comap_subtype_inf_right {H K : subgroup G} : comap K.subtype (H βŠ“ K) = comap K.subtype H := ext $ Ξ» x, and_iff_left_of_imp (Ξ» _, x.prop) /-- For any subgroups `H` and `K`, view `H βŠ“ K` as a subgroup of `K`. -/ @[to_additive "For any subgroups `H` and `K`, view `H βŠ“ K` as a subgroup of `K`."] def subgroup_of (H K : subgroup G) : subgroup K := H.comap K.subtype @[to_additive] lemma coe_subgroup_of (H K : subgroup G) : (H.subgroup_of K : set K) = K.subtype ⁻¹' H := rfl @[to_additive] lemma mem_subgroup_of {H K : subgroup G} {h : K} : h ∈ H.subgroup_of K ↔ (h : G) ∈ H := iff.rfl @[to_additive] lemma subgroup_of_map_subtype (H K : subgroup G) : (H.subgroup_of K).map K.subtype = H βŠ“ K := set_like.ext' begin convert set.image_preimage_eq_inter_range, simp only [subtype.range_coe_subtype, coe_subtype, coe_inf], refl, end /-- Given `subgroup`s `H`, `K` of groups `G`, `N` respectively, `H Γ— K` as a subgroup of `G Γ— N`. -/ @[to_additive prod "Given `add_subgroup`s `H`, `K` of `add_group`s `A`, `B` respectively, `H Γ— K` as an `add_subgroup` of `A Γ— B`."] def prod (H : subgroup G) (K : subgroup N) : subgroup (G Γ— N) := { inv_mem' := Ξ» _ hx, ⟨H.inv_mem' hx.1, K.inv_mem' hx.2⟩, .. submonoid.prod H.to_submonoid K.to_submonoid} @[to_additive coe_prod] lemma coe_prod (H : subgroup G) (K : subgroup N) : (H.prod K : set (G Γ— N)) = (H : set G).prod (K : set N) := rfl @[to_additive mem_prod] lemma mem_prod {H : subgroup G} {K : subgroup N} {p : G Γ— N} : p ∈ H.prod K ↔ p.1 ∈ H ∧ p.2 ∈ K := iff.rfl @[to_additive prod_mono] lemma prod_mono : ((≀) β‡’ (≀) β‡’ (≀)) (@prod G _ N _) (@prod G _ N _) := Ξ» s s' hs t t' ht, set.prod_mono hs ht @[to_additive prod_mono_right] lemma prod_mono_right (K : subgroup G) : monotone (Ξ» t : subgroup N, K.prod t) := prod_mono (le_refl K) @[to_additive prod_mono_left] lemma prod_mono_left (H : subgroup N) : monotone (Ξ» K : subgroup G, K.prod H) := Ξ» s₁ sβ‚‚ hs, prod_mono hs (le_refl H) @[to_additive prod_top] lemma prod_top (K : subgroup G) : K.prod (⊀ : subgroup N) = K.comap (monoid_hom.fst G N) := ext $ Ξ» x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (H : subgroup N) : (⊀ : subgroup G).prod H = H.comap (monoid_hom.snd G N) := ext $ Ξ» x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊀ : subgroup G).prod (⊀ : subgroup N) = ⊀ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (βŠ₯ : subgroup G).prod (βŠ₯ : subgroup N) = βŠ₯ := set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk] /-- Product of subgroups is isomorphic to their product as groups. -/ @[to_additive prod_equiv "Product of additive subgroups is isomorphic to their product as additive groups"] def prod_equiv (H : subgroup G) (K : subgroup N) : H.prod K ≃* H Γ— K := { map_mul' := Ξ» x y, rfl, .. equiv.set.prod ↑H ↑K } /-- A subgroup is normal if whenever `n ∈ H`, then `g * n * g⁻¹ ∈ H` for every `g : G` -/ structure normal : Prop := (conj_mem : βˆ€ n, n ∈ H β†’ βˆ€ g : G, g * n * g⁻¹ ∈ H) attribute [class] normal end subgroup namespace add_subgroup /-- An add_subgroup is normal if whenever `n ∈ H`, then `g + n - g ∈ H` for every `g : G` -/ structure normal (H : add_subgroup A) : Prop := (conj_mem [] : βˆ€ n, n ∈ H β†’ βˆ€ g : A, g + n + -g ∈ H) attribute [to_additive add_subgroup.normal] subgroup.normal attribute [class] normal end add_subgroup namespace subgroup variables {H K : subgroup G} @[priority 100, to_additive] instance normal_of_comm {G : Type*} [comm_group G] (H : subgroup G) : H.normal := ⟨by simp [mul_comm, mul_left_comm]⟩ namespace normal variable (nH : H.normal) @[to_additive] lemma mem_comm {a b : G} (h : a * b ∈ H) : b * a ∈ H := have a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ H, from nH.conj_mem (a * b) h a⁻¹, by simpa @[to_additive] lemma mem_comm_iff {a b : G} : a * b ∈ H ↔ b * a ∈ H := ⟨nH.mem_comm, nH.mem_comm⟩ end normal @[priority 100, to_additive] instance bot_normal : normal (βŠ₯ : subgroup G) := ⟨by simp⟩ @[priority 100, to_additive] instance top_normal : normal (⊀ : subgroup G) := ⟨λ _ _, mem_top⟩ variable (G) /-- The center of a group `G` is the set of elements that commute with everything in `G` -/ @[to_additive "The center of a group `G` is the set of elements that commute with everything in `G`"] def center : subgroup G := { carrier := {z | βˆ€ g, g * z = z * g}, one_mem' := by simp, mul_mem' := Ξ» a b (ha : βˆ€ g, g * a = a * g) (hb : βˆ€ g, g * b = b * g) g, by assoc_rw [ha, hb g], inv_mem' := Ξ» a (ha : βˆ€ g, g * a = a * g) g, by rw [← inv_inj, mul_inv_rev, inv_inv, ← ha, mul_inv_rev, inv_inv] } variable {G} @[to_additive] lemma mem_center_iff {z : G} : z ∈ center G ↔ βˆ€ g, g * z = z * g := iff.rfl @[priority 100, to_additive] instance center_normal : (center G).normal := ⟨begin assume n hn g h, assoc_rw [hn (h * g), hn g], simp end⟩ variables {G} (H) /-- The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal. -/ @[to_additive "The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal."] def normalizer : subgroup G := { carrier := {g : G | βˆ€ n, n ∈ H ↔ g * n * g⁻¹ ∈ H}, one_mem' := by simp, mul_mem' := Ξ» a b (ha : βˆ€ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) (hb : βˆ€ n, n ∈ H ↔ b * n * b⁻¹ ∈ H) n, by { rw [hb, ha], simp [mul_assoc] }, inv_mem' := Ξ» a (ha : βˆ€ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) n, by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } } -- variant for sets. -- TODO should this replace `normalizer`? /-- The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g*S*g⁻¹=S` -/ @[to_additive "The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g+S-g=S`."] def set_normalizer (S : set G) : subgroup G := { carrier := {g : G | βˆ€ n, n ∈ S ↔ g * n * g⁻¹ ∈ S}, one_mem' := by simp, mul_mem' := Ξ» a b (ha : βˆ€ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) (hb : βˆ€ n, n ∈ S ↔ b * n * b⁻¹ ∈ S) n, by { rw [hb, ha], simp [mul_assoc] }, inv_mem' := Ξ» a (ha : βˆ€ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) n, by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } } lemma mem_normalizer_fintype {S : set G} [fintype S] {x : G} (h : βˆ€ n, n ∈ S β†’ x * n * x⁻¹ ∈ S) : x ∈ subgroup.set_normalizer S := by haveI := classical.prop_decidable; haveI := set.fintype_image S (Ξ» n, x * n * x⁻¹); exact Ξ» n, ⟨h n, Ξ» h₁, have heq : (Ξ» n, x * n * x⁻¹) '' S = S := set.eq_of_subset_of_card_le (Ξ» n ⟨y, hy⟩, hy.2 β–Έ h y hy.1) (by rw set.card_image_of_injective S conj_injective), have x * n * x⁻¹ ∈ (Ξ» n, x * n * x⁻¹) '' S := heq.symm β–Έ h₁, let ⟨y, hy⟩ := this in conj_injective hy.2 β–Έ hy.1⟩ variable {H} @[to_additive] lemma mem_normalizer_iff {g : G} : g ∈ normalizer H ↔ βˆ€ n, n ∈ H ↔ g * n * g⁻¹ ∈ H := iff.rfl @[to_additive] lemma le_normalizer : H ≀ normalizer H := Ξ» x xH n, by rw [H.mul_mem_cancel_right (H.inv_mem xH), H.mul_mem_cancel_left xH] @[priority 100, to_additive] instance normal_in_normalizer : (H.comap H.normalizer.subtype).normal := ⟨λ x xH g, by simpa using (g.2 x).1 xH⟩ open_locale classical @[to_additive] lemma le_normalizer_of_normal [hK : (H.comap K.subtype).normal] (HK : H ≀ K) : K ≀ H.normalizer := Ξ» x hx y, ⟨λ yH, hK.conj_mem ⟨y, HK yH⟩ yH ⟨x, hx⟩, Ξ» yH, by simpa [mem_comap, mul_assoc] using hK.conj_mem ⟨x * y * x⁻¹, HK yH⟩ yH ⟨x⁻¹, K.inv_mem hx⟩⟩ variable (H) /-- Commutivity of a subgroup -/ structure is_commutative : Prop := (is_comm : _root_.is_commutative H (*)) attribute [class] is_commutative /-- Commutivity of an additive subgroup -/ structure _root_.add_subgroup.is_commutative (H : add_subgroup A) : Prop := (is_comm : _root_.is_commutative H (+)) attribute [to_additive add_subgroup.is_commutative] subgroup.is_commutative attribute [class] add_subgroup.is_commutative /-- A commutative subgroup is commutative -/ @[to_additive] instance is_commutative.comm_group [h : H.is_commutative] : comm_group H := { mul_comm := h.is_comm.comm, .. H.to_group } instance center.is_commutative : (center G).is_commutative := ⟨⟨λ a b, subtype.ext (b.2 a)⟩⟩ end subgroup namespace group variables {s : set G} /-- Given a set `s`, `conjugates_of_set s` is the set of all conjugates of the elements of `s`. -/ def conjugates_of_set (s : set G) : set G := ⋃ a ∈ s, conjugates_of a lemma mem_conjugates_of_set_iff {x : G} : x ∈ conjugates_of_set s ↔ βˆƒ a ∈ s, is_conj a x := set.mem_bUnion_iff theorem subset_conjugates_of_set : s βŠ† conjugates_of_set s := Ξ» (x : G) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj_refl _⟩ theorem conjugates_of_set_mono {s t : set G} (h : s βŠ† t) : conjugates_of_set s βŠ† conjugates_of_set t := set.bUnion_subset_bUnion_left h lemma conjugates_subset_normal {N : subgroup G} [tn : N.normal] {a : G} (h : a ∈ N) : conjugates_of a βŠ† N := by { rintros a hc, obtain ⟨c, rfl⟩ := is_conj_iff.1 hc, exact tn.conj_mem a h c } theorem conjugates_of_set_subset {s : set G} {N : subgroup G} [N.normal] (h : s βŠ† N) : conjugates_of_set s βŠ† N := set.bUnion_subset (Ξ» x H, conjugates_subset_normal (h H)) /-- The set of conjugates of `s` is closed under conjugation. -/ lemma conj_mem_conjugates_of_set {x c : G} : x ∈ conjugates_of_set s β†’ (c * x * c⁻¹ ∈ conjugates_of_set s) := Ξ» H, begin rcases (mem_conjugates_of_set_iff.1 H) with ⟨a,h₁,hβ‚‚βŸ©, exact mem_conjugates_of_set_iff.2 ⟨a, h₁, is_conj_trans hβ‚‚ (is_conj_iff.2 ⟨c,rfl⟩)⟩, end end group namespace subgroup open group variable {s : set G} /-- The normal closure of a set `s` is the subgroup closure of all the conjugates of elements of `s`. It is the smallest normal subgroup containing `s`. -/ def normal_closure (s : set G) : subgroup G := closure (conjugates_of_set s) theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s βŠ† normal_closure s := subset_closure theorem subset_normal_closure : s βŠ† normal_closure s := set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure theorem le_normal_closure {H : subgroup G} : H ≀ normal_closure ↑H := Ξ» _ h, subset_normal_closure h /-- The normal closure of `s` is a normal subgroup. -/ instance normal_closure_normal : (normal_closure s).normal := ⟨λ n h g, begin refine subgroup.closure_induction h (Ξ» x hx, _) _ (Ξ» x y ihx ihy, _) (Ξ» x ihx, _), { exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx)) }, { simpa using (normal_closure s).one_mem }, { rw ← conj_mul, exact mul_mem _ ihx ihy }, { rw ← conj_inv, exact inv_mem _ ihx } end⟩ /-- The normal closure of `s` is the smallest normal subgroup containing `s`. -/ theorem normal_closure_le_normal {N : subgroup G} [N.normal] (h : s βŠ† N) : normal_closure s ≀ N := begin assume a w, refine closure_induction w (Ξ» x hx, _) _ (Ξ» x y ihx ihy, _) (Ξ» x ihx, _), { exact (conjugates_of_set_subset h hx) }, { exact subgroup.one_mem _ }, { exact subgroup.mul_mem _ ihx ihy }, { exact subgroup.inv_mem _ ihx } end lemma normal_closure_subset_iff {N : subgroup G} [N.normal] : s βŠ† N ↔ normal_closure s ≀ N := ⟨normal_closure_le_normal, set.subset.trans (subset_normal_closure)⟩ theorem normal_closure_mono {s t : set G} (h : s βŠ† t) : normal_closure s ≀ normal_closure t := normal_closure_le_normal (set.subset.trans h subset_normal_closure) theorem normal_closure_eq_infi : normal_closure s = β¨… (N : subgroup G) [normal N] (hs : s βŠ† N), N := le_antisymm (le_infi (Ξ» N, le_infi (Ξ» hN, by exactI le_infi (normal_closure_le_normal)))) (infi_le_of_le (normal_closure s) (infi_le_of_le (by apply_instance) (infi_le_of_le subset_normal_closure (le_refl _)))) @[simp] theorem normal_closure_eq_self (H : subgroup G) [H.normal] : normal_closure ↑H = H := le_antisymm (normal_closure_le_normal rfl.subset) (le_normal_closure) @[simp] theorem normal_closure_idempotent : normal_closure ↑(normal_closure s) = normal_closure s := normal_closure_eq_self _ theorem closure_le_normal_closure {s : set G} : closure s ≀ normal_closure s := by simp only [subset_normal_closure, closure_le] @[simp] theorem normal_closure_closure_eq_normal_closure {s : set G} : normal_closure ↑(closure s) = normal_closure s := le_antisymm (normal_closure_le_normal closure_le_normal_closure) (normal_closure_mono subset_closure) end subgroup namespace add_subgroup open set lemma gsmul_mem (H : add_subgroup A) {x : A} (hx : x ∈ H) : βˆ€ n : β„€, n β€’ x ∈ H | (n : β„•) := by { rw [gsmul_coe_nat], exact add_submonoid.nsmul_mem H.to_add_submonoid hx n } | -[1+ n] := begin rw gsmul_neg_succ_of_nat, apply H.neg_mem', exact add_submonoid.nsmul_mem H.to_add_submonoid hx n.succ end /-- The `add_subgroup` generated by an element of an `add_group` equals the set of natural number multiples of the element. -/ lemma mem_closure_singleton {x y : A} : y ∈ closure ({x} : set A) ↔ βˆƒ n : β„€, n β€’ x = y := begin refine ⟨λ hy, closure_induction hy _ _ _ _, Ξ» ⟨n, hn⟩, hn β–Έ gsmul_mem _ (subset_closure $ mem_singleton x) n⟩, { intros y hy, rw [eq_of_mem_singleton hy], exact ⟨1, one_gsmul x⟩ }, { exact ⟨0, zero_gsmul x⟩ }, { rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, exact ⟨n + m, add_gsmul x n m⟩ }, { rintros _ ⟨n, rfl⟩, refine ⟨-n, neg_gsmul x n⟩ } end lemma closure_singleton_zero : closure ({0} : set A) = βŠ₯ := by simp [eq_bot_iff_forall, mem_closure_singleton] variable (H : add_subgroup A) @[simp] lemma coe_smul (x : H) (n : β„•) : ((n β€’ x : H) : A) = n β€’ x := coe_subtype H β–Έ add_monoid_hom.map_nsmul _ _ _ @[simp] lemma coe_gsmul (x : H) (n : β„€) : ((n β€’ x : H) : A) = n β€’ x := coe_subtype H β–Έ add_monoid_hom.map_gsmul _ _ _ attribute [to_additive add_subgroup.coe_smul] subgroup.coe_pow attribute [to_additive add_subgroup.coe_gsmul] subgroup.coe_gpow end add_subgroup namespace monoid_hom variables {N : Type*} {P : Type*} [group N] [group P] (K : subgroup G) open subgroup /-- The range of a monoid homomorphism from a group is a subgroup. -/ @[to_additive "The range of an `add_monoid_hom` from an `add_group` is an `add_subgroup`."] def range (f : G β†’* N) : subgroup N := subgroup.copy ((⊀ : subgroup G).map f) (set.range f) (by simp [set.ext_iff]) @[to_additive] instance decidable_mem_range (f : G β†’* N) [fintype G] [decidable_eq N] : decidable_pred (∈ f.range) := Ξ» x, fintype.decidable_exists_fintype @[simp, to_additive] lemma coe_range (f : G β†’* N) : (f.range : set N) = set.range f := rfl @[simp, to_additive] lemma mem_range {f : G β†’* N} {y : N} : y ∈ f.range ↔ βˆƒ x, f x = y := iff.rfl @[to_additive] lemma range_eq_map (f : G β†’* N) : f.range = (⊀ : subgroup G).map f := by ext; simp /-- The canonical surjective group homomorphism `G β†’* f(G)` induced by a group homomorphism `G β†’* N`. -/ @[to_additive "The canonical surjective `add_group` homomorphism `G β†’+ f(G)` induced by a group homomorphism `G β†’+ N`."] def range_restrict (f : G β†’* N) : G β†’* f.range := monoid_hom.mk' (Ξ» g, ⟨f g, ⟨g, rfl⟩⟩) $ Ξ» a b, by {ext, exact f.map_mul' _ _} @[simp, to_additive] lemma coe_range_restrict (f : G β†’* N) (g : G) : (f.range_restrict g : N) = f g := rfl @[to_additive] lemma map_range (g : N β†’* P) (f : G β†’* N) : f.range.map g = (g.comp f).range := by rw [range_eq_map, range_eq_map]; exact (⊀ : subgroup G).map_map g f @[to_additive] lemma range_top_iff_surjective {N} [group N] {f : G β†’* N} : f.range = (⊀ : subgroup N) ↔ function.surjective f := set_like.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid homomorphism is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` homomorphism is the whole of the codomain."] lemma range_top_of_surjective {N} [group N] (f : G β†’* N) (hf : function.surjective f) : f.range = (⊀ : subgroup N) := range_top_iff_surjective.2 hf @[simp, to_additive] lemma _root_.subgroup.subtype_range (H : subgroup G) : H.subtype.range = H := by { rw [range_eq_map, ← set_like.coe_set_eq, coe_map, subgroup.coe_subtype], ext, simp } /-- Restriction of a group hom to a subgroup of the domain. -/ @[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the domain."] def restrict (f : G β†’* N) (H : subgroup G) : H β†’* N := f.comp H.subtype @[simp, to_additive] lemma restrict_apply {H : subgroup G} (f : G β†’* N) (x : H) : f.restrict H x = f (x : G) := rfl /-- Restriction of a group hom to a subgroup of the codomain. -/ @[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the codomain."] def cod_restrict (f : G β†’* N) (S : subgroup N) (h : βˆ€ x, f x ∈ S) : G β†’* S := { to_fun := Ξ» n, ⟨f n, h n⟩, map_one' := subtype.eq f.map_one, map_mul' := Ξ» x y, subtype.eq (f.map_mul x y) } @[simp, to_additive] lemma cod_restrict_apply {G : Type*} [group G] {N : Type*} [group N] (f : G β†’* N) (S : subgroup N) (h : βˆ€ (x : G), f x ∈ S) {x : G} : f.cod_restrict S h x = ⟨f x, h x⟩ := rfl @[to_additive] lemma subgroup_of_range_eq_of_le {G₁ Gβ‚‚ : Type*} [group G₁] [group Gβ‚‚] {K : subgroup Gβ‚‚} (f : G₁ β†’* Gβ‚‚) (h : f.range ≀ K) : f.range.subgroup_of K = (f.cod_restrict K (Ξ» x, h ⟨x, rfl⟩)).range := begin ext k, refine exists_congr _, simp [subtype.ext_iff], end /-- Computable alternative to `monoid_hom.of_injective`. -/ def of_left_inverse {f : G β†’* N} {g : N β†’* G} (h : function.left_inverse g f) : G ≃* f.range := { to_fun := f.range_restrict, inv_fun := g ∘ f.range.subtype, left_inv := h, right_inv := by { rintros ⟨x, y, rfl⟩, apply subtype.ext, rw [coe_range_restrict, function.comp_apply, subgroup.coe_subtype, subtype.coe_mk, h] }, .. f.range_restrict } @[simp] lemma of_left_inverse_apply {f : G β†’* N} {g : N β†’* G} (h : function.left_inverse g f) (x : G) : ↑(of_left_inverse h x) = f x := rfl @[simp] lemma of_left_inverse_symm_apply {f : G β†’* N} {g : N β†’* G} (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl /-- The range of an injective group homomorphism is isomorphic to its domain. -/ noncomputable def of_injective {f : G β†’* N} (hf : function.injective f) : G ≃* f.range := (mul_equiv.of_bijective (f.cod_restrict f.range (Ξ» x, ⟨x, rfl⟩)) ⟨λ x y h, hf (subtype.ext_iff.mp h), by { rintros ⟨x, y, rfl⟩, exact ⟨y, rfl⟩ }⟩) lemma of_injective_apply {f : G β†’* N} (hf : function.injective f) {x : G} : ↑(of_injective hf x) = f x := rfl /-- The multiplicative kernel of a monoid homomorphism is the subgroup of elements `x : G` such that `f x = 1` -/ @[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_subgroup` of elements such that `f x = 0`"] def ker (f : G β†’* N) := (βŠ₯ : subgroup N).comap f @[to_additive] lemma mem_ker (f : G β†’* N) {x : G} : x ∈ f.ker ↔ f x = 1 := iff.rfl @[to_additive] lemma coe_ker (f : G β†’* N) : (f.ker : set G) = (f : G β†’ N) ⁻¹' {1} := rfl @[to_additive] lemma eq_iff (f : G β†’* N) {x y : G} : f x = f y ↔ y⁻¹ * x ∈ f.ker := by rw [f.mem_ker, f.map_mul, f.map_inv, inv_mul_eq_one, eq_comm] @[to_additive] instance decidable_mem_ker [decidable_eq N] (f : G β†’* N) : decidable_pred (∈ f.ker) := Ξ» x, decidable_of_iff (f x = 1) f.mem_ker @[to_additive] lemma comap_ker (g : N β†’* P) (f : G β†’* N) : g.ker.comap f = (g.comp f).ker := rfl @[simp, to_additive] lemma comap_bot (f : G β†’* N) : (βŠ₯ : subgroup N).comap f = f.ker := rfl @[to_additive] lemma range_restrict_ker (f : G β†’* N) : ker (range_restrict f) = ker f := begin ext, change (⟨f x, _⟩ : range f) = ⟨1, _⟩ ↔ f x = 1, simp only [], end @[simp, to_additive] lemma ker_one : (1 : G β†’* N).ker = ⊀ := by { ext, simp [mem_ker] } @[to_additive] lemma ker_eq_bot_iff (f : G β†’* N) : f.ker = βŠ₯ ↔ function.injective f := begin split, { intros h x y hxy, rwa [←mul_inv_eq_one, ←map_inv, ←map_mul, ←mem_ker, h, mem_bot, mul_inv_eq_one] at hxy }, { exact Ξ» h, le_bot_iff.mp (Ξ» x hx, h (hx.trans f.map_one.symm)) }, end @[to_additive] lemma prod_map_comap_prod {G' : Type*} {N' : Type*} [group G'] [group N'] (f : G β†’* N) (g : G' β†’* N') (S : subgroup N) (S' : subgroup N') : (S.prod S').comap (prod_map f g) = (S.comap f).prod (S'.comap g) := set_like.coe_injective $ set.preimage_prod_map_prod f g _ _ @[to_additive] lemma ker_prod_map {G' : Type*} {N' : Type*} [group G'] [group N'] (f : G β†’* N) (g : G' β†’* N') : (prod_map f g).ker = f.ker.prod g.ker := begin dsimp only [ker], rw [←prod_map_comap_prod, bot_prod_bot], end /-- The subgroup of elements `x : G` such that `f x = g x` -/ @[to_additive "The additive subgroup of elements `x : G` such that `f x = g x`"] def eq_locus (f g : G β†’* N) : subgroup G := { inv_mem' := Ξ» x (hx : f x = g x), show f x⁻¹ = g x⁻¹, by rw [f.map_inv, g.map_inv, hx], .. eq_mlocus f g} /-- If two monoid homomorphisms are equal on a set, then they are equal on its subgroup closure. -/ @[to_additive] lemma eq_on_closure {f g : G β†’* N} {s : set G} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≀ f.eq_locus g, from (closure_le _).2 h @[to_additive] lemma eq_of_eq_on_top {f g : G β†’* N} (h : set.eq_on f g (⊀ : subgroup G)) : f = g := ext $ Ξ» x, h trivial @[to_additive] lemma eq_of_eq_on_dense {s : set G} (hs : closure s = ⊀) {f g : G β†’* N} (h : s.eq_on f g) : f = g := eq_of_eq_on_top $ hs β–Έ eq_on_closure h @[to_additive] lemma gclosure_preimage_le (f : G β†’* N) (s : set N) : closure (f ⁻¹' s) ≀ (closure s).comap f := (closure_le _).2 $ Ξ» x hx, by rw [set_like.mem_coe, mem_comap]; exact subset_closure hx /-- The image under a monoid homomorphism of the subgroup generated by a set equals the subgroup generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_subgroup` generated by a set equals the `add_subgroup` generated by the image of the set."] lemma map_closure (f : G β†’* N) (s : set G) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image f s) (gclosure_preimage_le _ _)) ((closure_le _).2 $ set.image_subset _ subset_closure) -- this instance can't go just after the definition of `mrange` because `fintype` is -- not imported at that stage /-- The range of a finite monoid under a monoid homomorphism is finite. Note: this instance can form a diamond with `subtype.fintype` in the presence of `fintype N`. -/ @[to_additive "The range of a finite additive monoid under an additive monoid homomorphism is finite. Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the presence of `fintype N`."] instance fintype_mrange {M N : Type*} [monoid M] [monoid N] [fintype M] [decidable_eq N] (f : M β†’* N) : fintype (mrange f) := set.fintype_range f /-- The range of a finite group under a group homomorphism is finite. Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the presence of `fintype N`. -/ @[to_additive "The range of a finite additive group under an additive group homomorphism is finite. Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the presence of `fintype N`."] instance fintype_range [fintype G] [decidable_eq N] (f : G β†’* N) : fintype (range f) := set.fintype_range f end monoid_hom namespace subgroup variables {N : Type*} [group N] (H : subgroup G) @[to_additive] lemma map_eq_bot_iff {f : G β†’* N} : H.map f = βŠ₯ ↔ H ≀ f.ker := begin rw eq_bot_iff, split, { exact Ξ» h x hx, h ⟨x, hx, rfl⟩ }, { intros h x hx, obtain ⟨y, hy, rfl⟩ := hx, exact h hy }, end @[to_additive] lemma map_eq_bot_iff_of_injective {f : G β†’* N} (hf : function.injective f) : H.map f = βŠ₯ ↔ H = βŠ₯ := by rw [map_eq_bot_iff, f.ker_eq_bot_iff.mpr hf, le_bot_iff] end subgroup namespace subgroup open monoid_hom variables {N : Type*} [group N] (f : G β†’* N) @[to_additive] lemma map_le_range (H : subgroup G) : map f H ≀ f.range := (range_eq_map f).symm β–Έ map_mono le_top @[to_additive] lemma ker_le_comap (H : subgroup N) : f.ker ≀ comap f H := comap_mono bot_le @[to_additive] lemma map_comap_le (H : subgroup N) : map f (comap f H) ≀ H := (gc_map_comap f).l_u_le _ @[to_additive] lemma le_comap_map (H : subgroup G) : H ≀ comap f (map f H) := (gc_map_comap f).le_u_l _ @[to_additive] lemma map_comap_eq (H : subgroup N) : map f (comap f H) = f.range βŠ“ H := set_like.ext' begin convert set.image_preimage_eq_inter_range, simp [set.inter_comm], end @[to_additive] lemma comap_map_eq (H : subgroup G) : comap f (map f H) = H βŠ” f.ker := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (ker_le_comap _ _)), intros x hx, simp only [exists_prop, mem_map, mem_comap] at hx, rcases hx with ⟨y, hy, hy'⟩, have : y⁻¹ * x ∈ f.ker, { rw mem_ker, simp [hy'] }, convert mul_mem _ (mem_sup_left hy) (mem_sup_right this), simp, end @[to_additive] lemma map_comap_eq_self {f : G β†’* N} {H : subgroup N} (h : H ≀ f.range) : map f (comap f H) = H := by rwa [map_comap_eq, inf_eq_right] @[to_additive] lemma map_comap_eq_self_of_surjective {f : G β†’* N} (h : function.surjective f) (H : subgroup N) : map f (comap f H) = H := map_comap_eq_self ((range_top_of_surjective _ h).symm β–Έ le_top) @[to_additive] lemma comap_injective {f : G β†’* N} (h : function.surjective f) : function.injective (comap f) := Ξ» K L hKL, by { apply_fun map f at hKL, simpa [map_comap_eq_self_of_surjective h] using hKL } @[to_additive] lemma comap_map_eq_self {f : G β†’* N} {H : subgroup G} (h : f.ker ≀ H) : comap f (map f H) = H := by rwa [comap_map_eq, sup_eq_left] @[to_additive] lemma comap_map_eq_self_of_injective {f : G β†’* N} (h : function.injective f) (H : subgroup G) : comap f (map f H) = H := comap_map_eq_self (((ker_eq_bot_iff _).mpr h).symm β–Έ bot_le) @[to_additive] lemma map_injective {f : G β†’* N} (h : function.injective f) : function.injective (map f) := Ξ» K L hKL, by { apply_fun comap f at hKL, simpa [comap_map_eq_self_of_injective h] using hKL } @[to_additive] lemma map_eq_comap_of_inverse {f : G β†’* N} {g : N β†’* G} (hl : function.left_inverse g f) (hr : function.right_inverse g f) (H : subgroup G) : map f H = comap g H := set_like.ext' $ by rw [coe_map, coe_comap, set.image_eq_preimage_of_inverse hl hr] /-- Given `f(A) = f(B)`, `ker f ≀ A`, and `ker f ≀ B`, deduce that `A = B` -/ @[to_additive] lemma map_injective_of_ker_le {H K : subgroup G} (hH : f.ker ≀ H) (hK : f.ker ≀ K) (hf : map f H = map f K) : H = K := begin apply_fun comap f at hf, rwa [comap_map_eq, comap_map_eq, sup_of_le_left hH, sup_of_le_left hK] at hf, end @[to_additive] lemma comap_sup_eq (H K : subgroup N) (hf : function.surjective f): comap f H βŠ” comap f K = comap f (H βŠ” K) := begin have : map f (comap f H βŠ” comap f K) = map f (comap f (H βŠ” K)), { simp [subgroup.map_comap_eq, map_sup, f.range_top_of_surjective hf], }, refine map_injective_of_ker_le f _ _ this, { calc f.ker ≀ comap f H : ker_le_comap f _ ... ≀ comap f H βŠ” comap f K : le_sup_left, }, exact ker_le_comap _ _, end /-- A subgroup is isomorphic to its image under an injective function -/ @[to_additive "An additive subgroup is isomorphic to its image under an injective function"] noncomputable def equiv_map_of_injective (H : subgroup G) (f : G β†’* N) (hf : function.injective f) : H ≃* H.map f := { map_mul' := Ξ» _ _, subtype.ext (f.map_mul _ _), ..equiv.set.image f H hf } @[simp, to_additive] lemma coe_equiv_map_of_injective_apply (H : subgroup G) (f : G β†’* N) (hf : function.injective f) (h : H) : (equiv_map_of_injective H f hf h : N) = f h := rfl end subgroup namespace monoid_hom variables {G₁ Gβ‚‚ G₃ : Type*} [group G₁] [group Gβ‚‚] [group G₃] variables (f : G₁ β†’* Gβ‚‚) (f_inv : Gβ‚‚ β†’ G₁) /-- Auxiliary definition used to define `lift_of_right_inverse` -/ @[to_additive "Auxiliary definition used to define `lift_of_right_inverse`"] def lift_of_right_inverse_aux (hf : function.right_inverse f_inv f) (g : G₁ β†’* G₃) (hg : f.ker ≀ g.ker) : Gβ‚‚ β†’* G₃ := { to_fun := Ξ» b, g (f_inv b), map_one' := hg (hf 1), map_mul' := begin intros x y, rw [← g.map_mul, ← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker], apply hg, rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one, f.map_mul], simp only [hf _], end } @[simp, to_additive] lemma lift_of_right_inverse_aux_comp_apply (hf : function.right_inverse f_inv f) (g : G₁ β†’* G₃) (hg : f.ker ≀ g.ker) (x : G₁) : (f.lift_of_right_inverse_aux f_inv hf g hg) (f x) = g x := begin dsimp [lift_of_right_inverse_aux], rw [← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker], apply hg, rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one], simp only [hf _], end /-- `lift_of_right_inverse f hf g hg` is the unique group homomorphism `Ο†` * such that `Ο†.comp f = g` (`monoid_hom.lift_of_right_inverse_comp`), * where `f : G₁ β†’+* Gβ‚‚` has a right_inverse `f_inv` (`hf`), * and `g : Gβ‚‚ β†’+* G₃` satisfies `hg : f.ker ≀ g.ker`. See `monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma. ``` G₁. | \ f | \ g | \ v \⌟ Gβ‚‚----> G₃ βˆƒ!Ο† ``` -/ @[to_additive "`lift_of_right_inverse f f_inv hf g hg` is the unique additive group homomorphism `Ο†` * such that `Ο†.comp f = g` (`add_monoid_hom.lift_of_right_inverse_comp`), * where `f : G₁ β†’+ Gβ‚‚` has a right_inverse `f_inv` (`hf`), * and `g : Gβ‚‚ β†’+ G₃` satisfies `hg : f.ker ≀ g.ker`. See `add_monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma. ``` G₁. | \\ f | \\ g | \\ v \\⌟ Gβ‚‚----> G₃ βˆƒ!Ο† ```"] def lift_of_right_inverse (hf : function.right_inverse f_inv f) : {g : G₁ β†’* G₃ // f.ker ≀ g.ker} ≃ (Gβ‚‚ β†’* G₃) := { to_fun := Ξ» g, f.lift_of_right_inverse_aux f_inv hf g.1 g.2, inv_fun := Ξ» Ο†, βŸ¨Ο†.comp f, Ξ» x hx, (mem_ker _).mpr $ by simp [(mem_ker _).mp hx]⟩, left_inv := Ξ» g, by { ext, simp only [comp_apply, lift_of_right_inverse_aux_comp_apply, subtype.coe_mk, subtype.val_eq_coe], }, right_inv := Ξ» Ο†, by { ext b, simp [lift_of_right_inverse_aux, hf b], } } /-- A non-computable version of `monoid_hom.lift_of_right_inverse` for when no computable right inverse is available, that uses `function.surj_inv`. -/ @[simp, to_additive "A non-computable version of `add_monoid_hom.lift_of_right_inverse` for when no computable right inverse is available."] noncomputable abbreviation lift_of_surjective (hf : function.surjective f) : {g : G₁ β†’* G₃ // f.ker ≀ g.ker} ≃ (Gβ‚‚ β†’* G₃) := f.lift_of_right_inverse (function.surj_inv hf) (function.right_inverse_surj_inv hf) @[simp, to_additive] lemma lift_of_right_inverse_comp_apply (hf : function.right_inverse f_inv f) (g : {g : G₁ β†’* G₃ // f.ker ≀ g.ker}) (x : G₁) : (f.lift_of_right_inverse f_inv hf g) (f x) = g x := f.lift_of_right_inverse_aux_comp_apply f_inv hf g.1 g.2 x @[simp, to_additive] lemma lift_of_right_inverse_comp (hf : function.right_inverse f_inv f) (g : {g : G₁ β†’* G₃ // f.ker ≀ g.ker}) : (f.lift_of_right_inverse f_inv hf g).comp f = g := monoid_hom.ext $ f.lift_of_right_inverse_comp_apply f_inv hf g @[to_additive] lemma eq_lift_of_right_inverse (hf : function.right_inverse f_inv f) (g : G₁ β†’* G₃) (hg : f.ker ≀ g.ker) (h : Gβ‚‚ β†’* G₃) (hh : h.comp f = g) : h = (f.lift_of_right_inverse f_inv hf ⟨g, hg⟩) := begin simp_rw ←hh, exact ((f.lift_of_right_inverse f_inv hf).apply_symm_apply _).symm, end end monoid_hom variables {N : Type*} [group N] -- Here `H.normal` is an explicit argument so we can use dot notation with `comap`. @[to_additive] lemma subgroup.normal.comap {H : subgroup N} (hH : H.normal) (f : G β†’* N) : (H.comap f).normal := ⟨λ _, by simp [subgroup.mem_comap, hH.conj_mem] {contextual := tt}⟩ @[priority 100, to_additive] instance subgroup.normal_comap {H : subgroup N} [nH : H.normal] (f : G β†’* N) : (H.comap f).normal := nH.comap _ @[priority 100, to_additive] instance monoid_hom.normal_ker (f : G β†’* N) : f.ker.normal := by rw [monoid_hom.ker]; apply_instance @[priority 100, to_additive] instance subgroup.normal_inf (H N : subgroup G) [hN : N.normal] : ((H βŠ“ N).comap H.subtype).normal := ⟨λ x hx g, begin simp only [subgroup.mem_inf, coe_subtype, subgroup.mem_comap] at hx, simp only [subgroup.coe_mul, subgroup.mem_inf, coe_subtype, subgroup.coe_inv, subgroup.mem_comap], exact ⟨H.mul_mem (H.mul_mem g.2 hx.1) (H.inv_mem g.2), hN.1 x hx.2 g⟩, end⟩ namespace subgroup /-- The subgroup generated by an element. -/ def gpowers (g : G) : subgroup G := subgroup.copy (gpowers_hom G g).range (set.range ((^) g : β„€ β†’ G)) rfl @[simp] lemma mem_gpowers (g : G) : g ∈ gpowers g := ⟨1, gpow_one _⟩ lemma gpowers_eq_closure (g : G) : gpowers g = closure {g} := by { ext, exact mem_closure_singleton.symm } @[simp] lemma range_gpowers_hom (g : G) : (gpowers_hom G g).range = gpowers g := rfl lemma gpowers_subset {a : G} {K : subgroup G} (h : a ∈ K) : gpowers a ≀ K := Ξ» x hx, match x, hx with _, ⟨i, rfl⟩ := K.gpow_mem h i end lemma mem_gpowers_iff {g h : G} : h ∈ gpowers g ↔ βˆƒ (k : β„€), g ^ k = h := iff.rfl end subgroup namespace add_subgroup /-- The subgroup generated by an element. -/ def gmultiples (a : A) : add_subgroup A := add_subgroup.copy (gmultiples_hom A a).range (set.range ((β€’ a) : β„€ β†’ A)) rfl @[simp] lemma mem_gmultiples (a : A) : a ∈ gmultiples a := ⟨1, one_gsmul _⟩ lemma gmultiples_eq_closure (a : A) : gmultiples a = closure {a} := by { ext, exact mem_closure_singleton.symm } @[simp] lemma range_gmultiples_hom (a : A) : (gmultiples_hom A a).range = gmultiples a := rfl lemma gmultiples_subset {a : A} {B : add_subgroup A} (h : a ∈ B) : gmultiples a ≀ B := @subgroup.gpowers_subset (multiplicative A) _ _ (B.to_subgroup) h lemma mem_gmultiples_iff {a b : A} : b ∈ add_subgroup.gmultiples a ↔ βˆƒ (k : β„€), k β€’ a = b := iff.rfl attribute [to_additive add_subgroup.gmultiples] subgroup.gpowers attribute [to_additive add_subgroup.mem_gmultiples] subgroup.mem_gpowers attribute [to_additive add_subgroup.gmultiples_eq_closure] subgroup.gpowers_eq_closure attribute [to_additive add_subgroup.range_gmultiples_hom] subgroup.range_gpowers_hom attribute [to_additive add_subgroup.gmultiples_subset] subgroup.gpowers_subset attribute [to_additive add_subgroup.mem_gmultiples_iff] subgroup.mem_gpowers_iff end add_subgroup lemma int.mem_gmultiples_iff {a b : β„€} : b ∈ add_subgroup.gmultiples a ↔ a ∣ b := exists_congr (Ξ» k, by rw [mul_comm, eq_comm, ← smul_eq_mul]) lemma of_mul_image_gpowers_eq_gmultiples_of_mul { x : G } : additive.of_mul '' ((subgroup.gpowers x) : set G) = add_subgroup.gmultiples (additive.of_mul x) := begin ext y, split, { rintro ⟨z, ⟨m, hm⟩, hz2⟩, use m, simp only, rwa [← of_mul_gpow, hm] }, { rintros ⟨n, hn⟩, refine ⟨x ^ n, ⟨n, rfl⟩, _⟩, rwa of_mul_gpow } end lemma of_add_image_gmultiples_eq_gpowers_of_add {x : A} : multiplicative.of_add '' ((add_subgroup.gmultiples x) : set A) = subgroup.gpowers (multiplicative.of_add x) := begin symmetry, rw equiv.eq_image_iff_symm_image_eq, exact of_mul_image_gpowers_eq_gmultiples_of_mul, end namespace mul_equiv variables {H K : subgroup G} /-- Makes the identity isomorphism from a proof two subgroups of a multiplicative group are equal. -/ @[to_additive "Makes the identity additive isomorphism from a proof two subgroups of an additive group are equal."] def subgroup_congr (h : H = K) : H ≃* K := { map_mul' := Ξ» _ _, rfl, ..equiv.set_congr $ congr_arg _ h } end mul_equiv -- TODO : β†₯(⊀ : subgroup H) ≃* H ? namespace subgroup variables {C : Type*} [comm_group C] {s t : subgroup C} {x : C} @[to_additive] lemma mem_sup : x ∈ s βŠ” t ↔ βˆƒ (y ∈ s) (z ∈ t), y * z = x := ⟨λ h, begin rw [← closure_eq s, ← closure_eq t, ← closure_union] at h, apply closure_induction h, { rintro y (h | h), { exact ⟨y, h, 1, t.one_mem, by simp⟩ }, { exact ⟨1, s.one_mem, y, h, by simp⟩ } }, { exact ⟨1, s.one_mem, 1, ⟨t.one_mem, mul_one 1⟩⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨yβ‚‚, hyβ‚‚, zβ‚‚, hzβ‚‚, rfl⟩, exact ⟨_, mul_mem _ hy₁ hyβ‚‚, _, mul_mem _ hz₁ hzβ‚‚, by simp [mul_assoc]; cc⟩ }, { rintro _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, inv_mem _ hy, _, inv_mem _ hz, mul_comm z y β–Έ (mul_inv_rev z y).symm⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact mul_mem _ ((le_sup_left : s ≀ s βŠ” t) hy) ((le_sup_right : t ≀ s βŠ” t) hz)⟩ @[to_additive] lemma mem_sup' : x ∈ s βŠ” t ↔ βˆƒ (y : s) (z : t), (y:C) * z = x := mem_sup.trans $ by simp only [set_like.exists, coe_mk] @[to_additive] instance : is_modular_lattice (subgroup C) := ⟨λ x y z xz a ha, begin rw [mem_inf, mem_sup] at ha, rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩, rw mem_sup, refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩, rw ← inv_mul_cancel_left b c, apply z.mul_mem (z.inv_mem (xz hb)) haz, end⟩ end subgroup section variables (G) (A) /-- A `group` is simple when it has exactly two normal `subgroup`s. -/ class is_simple_group extends nontrivial G : Prop := (eq_bot_or_eq_top_of_normal : βˆ€ H : subgroup G, H.normal β†’ H = βŠ₯ ∨ H = ⊀) /-- An `add_group` is simple when it has exactly two normal `add_subgroup`s. -/ class is_simple_add_group extends nontrivial A : Prop := (eq_bot_or_eq_top_of_normal : βˆ€ H : add_subgroup A, H.normal β†’ H = βŠ₯ ∨ H = ⊀) attribute [to_additive] is_simple_group variables {G} {A} @[to_additive] lemma subgroup.normal.eq_bot_or_eq_top [is_simple_group G] {H : subgroup G} (Hn : H.normal) : H = βŠ₯ ∨ H = ⊀ := is_simple_group.eq_bot_or_eq_top_of_normal H Hn namespace is_simple_group @[to_additive] instance {C : Type*} [comm_group C] [is_simple_group C] : is_simple_lattice (subgroup C) := ⟨λ H, H.normal_of_comm.eq_bot_or_eq_top⟩ open subgroup @[to_additive] lemma is_simple_group_of_surjective {H : Type*} [group H] [is_simple_group G] [nontrivial H] (f : G β†’* H) (hf : function.surjective f) : is_simple_group H := ⟨nontrivial.exists_pair_ne, Ξ» H iH, begin refine ((iH.comap f).eq_bot_or_eq_top).imp (Ξ» h, _) (Ξ» h, _), { rw [←map_bot f, ←h, map_comap_eq_self_of_surjective hf] }, { rw [←comap_top f] at h, exact comap_injective hf h } end⟩ end is_simple_group end namespace subgroup section pointwise @[to_additive] lemma closure_mul_le (S T : set G) : closure (S * T) ≀ closure S βŠ” closure T := Inf_le $ Ξ» x ⟨s, t, hs, ht, hx⟩, hx β–Έ (closure S βŠ” closure T).mul_mem (set_like.le_def.mp le_sup_left $ subset_closure hs) (set_like.le_def.mp le_sup_right $ subset_closure ht) @[to_additive] lemma sup_eq_closure (H K : subgroup G) : H βŠ” K = closure (H * K) := le_antisymm (sup_le (Ξ» h hh, subset_closure ⟨h, 1, hh, K.one_mem, mul_one h⟩) (Ξ» k hk, subset_closure ⟨1, k, H.one_mem, hk, one_mul k⟩)) (by conv_rhs { rw [← closure_eq H, ← closure_eq K] }; apply closure_mul_le) @[to_additive] private def mul_normal_aux (H N : subgroup G) [hN : N.normal] : subgroup G := { carrier := (H : set G) * N, one_mem' := ⟨1, 1, H.one_mem, N.one_mem, by rw mul_one⟩, mul_mem' := Ξ» a b ⟨h, n, hh, hn, ha⟩ ⟨h', n', hh', hn', hb⟩, ⟨h * h', h'⁻¹ * n * h' * n', H.mul_mem hh hh', N.mul_mem (by simpa using hN.conj_mem _ hn h'⁻¹) hn', by simp [← ha, ← hb, mul_assoc]⟩, inv_mem' := Ξ» x ⟨h, n, hh, hn, hx⟩, ⟨h⁻¹, h * n⁻¹ * h⁻¹, H.inv_mem hh, hN.conj_mem _ (N.inv_mem hn) h, by rw [mul_assoc h, inv_mul_cancel_left, ← hx, mul_inv_rev]⟩ } /-- The carrier of `H βŠ” N` is just `↑H * ↑N` (pointwise set product) when `N` is normal. -/ @[to_additive "The carrier of `H βŠ” N` is just `↑H + ↑N` (pointwise set addition) when `N` is normal."] lemma mul_normal (H N : subgroup G) [N.normal] : (↑(H βŠ” N) : set G) = H * N := set.subset.antisymm (show H βŠ” N ≀ mul_normal_aux H N, by { rw sup_eq_closure, apply Inf_le _, dsimp, refl }) ((sup_eq_closure H N).symm β–Έ subset_closure) @[to_additive] private def normal_mul_aux (N H : subgroup G) [hN : N.normal] : subgroup G := { carrier := (N : set G) * H, one_mem' := ⟨1, 1, N.one_mem, H.one_mem, by rw mul_one⟩, mul_mem' := Ξ» a b ⟨n, h, hn, hh, ha⟩ ⟨n', h', hn', hh', hb⟩, ⟨n * (h * n' * h⁻¹), h * h', N.mul_mem hn (hN.conj_mem _ hn' _), H.mul_mem hh hh', by simp [← ha, ← hb, mul_assoc]⟩, inv_mem' := Ξ» x ⟨n, h, hn, hh, hx⟩, ⟨h⁻¹ * n⁻¹ * h, h⁻¹, by simpa using hN.conj_mem _ (N.inv_mem hn) h⁻¹, H.inv_mem hh, by rw [mul_inv_cancel_right, ← mul_inv_rev, hx]⟩ } /-- The carrier of `N βŠ” H` is just `↑N * ↑H` (pointwise set product) when `N` is normal. -/ @[to_additive "The carrier of `N βŠ” H` is just `↑N + ↑H` (pointwise set addition) when `N` is normal."] lemma normal_mul (N H : subgroup G) [N.normal] : (↑(N βŠ” H) : set G) = N * H := set.subset.antisymm (show N βŠ” H ≀ normal_mul_aux N H, by { rw sup_eq_closure, apply Inf_le _, dsimp, refl }) ((sup_eq_closure N H).symm β–Έ subset_closure) @[to_additive] lemma mul_inf_assoc (A B C : subgroup G) (h : A ≀ C) : (A : set G) * ↑(B βŠ“ C) = (A * B) βŠ“ C := begin ext, simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff], split, { rintros ⟨y, z, hy, ⟨hzB, hzC⟩, rfl⟩, refine ⟨_, mul_mem C (h hy) hzC⟩, exact ⟨y, z, hy, hzB, rfl⟩ }, rintros ⟨⟨y, z, hy, hz, rfl⟩, hyz⟩, refine ⟨y, z, hy, ⟨hz, _⟩, rfl⟩, suffices : y⁻¹ * (y * z) ∈ C, { simpa }, exact mul_mem C (inv_mem C (h hy)) hyz end @[to_additive] lemma inf_mul_assoc (A B C : subgroup G) (h : C ≀ A) : ((A βŠ“ B : subgroup G) : set G) * C = A βŠ“ (B * C) := begin ext, simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff], split, { rintros ⟨y, z, ⟨hyA, hyB⟩, hz, rfl⟩, refine ⟨mul_mem A hyA (h hz), _⟩, exact ⟨y, z, hyB, hz, rfl⟩ }, rintros ⟨hyz, y, z, hy, hz, rfl⟩, refine ⟨y, z, ⟨_, hy⟩, hz, rfl⟩, suffices : (y * z) * z⁻¹ ∈ A, { simpa }, exact mul_mem A hyz (inv_mem A (h hz)) end end pointwise section subgroup_normal @[to_additive] lemma normal_subgroup_of_iff {H K : subgroup G} (hHK : H ≀ K) : (H.subgroup_of K).normal ↔ βˆ€ h k, h ∈ H β†’ k ∈ K β†’ k * h * k⁻¹ ∈ H := ⟨λ hN h k hH hK, hN.conj_mem ⟨h, hHK hH⟩ hH ⟨k, hK⟩, Ξ» hN, { conj_mem := Ξ» h hm k, (hN h.1 k.1 hm k.2) }⟩ @[to_additive] instance prod_subgroup_of_prod_normal {H₁ K₁ : subgroup G} {Hβ‚‚ Kβ‚‚ : subgroup N} [h₁ : (H₁.subgroup_of K₁).normal] [hβ‚‚ : (Hβ‚‚.subgroup_of Kβ‚‚).normal] : ((H₁.prod Hβ‚‚).subgroup_of (K₁.prod Kβ‚‚)).normal := { conj_mem := Ξ» n hgHK g, ⟨h₁.conj_mem ⟨(n : G Γ— N).fst, (mem_prod.mp n.2).1⟩ hgHK.1 ⟨(g : G Γ— N).fst, (mem_prod.mp g.2).1⟩, hβ‚‚.conj_mem ⟨(n : G Γ— N).snd, (mem_prod.mp n.2).2⟩ hgHK.2 ⟨(g : G Γ— N).snd, (mem_prod.mp g.2).2⟩⟩ } @[to_additive] instance prod_normal (H : subgroup G) (K : subgroup N) [hH : H.normal] [hK : K.normal] : (H.prod K).normal := { conj_mem := Ξ» n hg g, ⟨hH.conj_mem n.fst (subgroup.mem_prod.mp hg).1 g.fst, hK.conj_mem n.snd (subgroup.mem_prod.mp hg).2 g.snd⟩ } @[to_additive] lemma inf_subgroup_of_inf_normal_of_right (A B' B : subgroup G) (hB : B' ≀ B) [hN : (B'.subgroup_of B).normal] : ((A βŠ“ B').subgroup_of (A βŠ“ B)).normal := { conj_mem := Ξ» n hn g, ⟨mul_mem A (mul_mem A (mem_inf.1 g.2).1 (mem_inf.1 n.2).1) (inv_mem A (mem_inf.1 g.2).1), (normal_subgroup_of_iff hB).mp hN n g hn.2 (mem_inf.mp g.2).2⟩ } @[to_additive] lemma inf_subgroup_of_inf_normal_of_left {A' A : subgroup G} (B : subgroup G) (hA : A' ≀ A) [hN : (A'.subgroup_of A).normal] : ((A' βŠ“ B).subgroup_of (A βŠ“ B)).normal := { conj_mem := Ξ» n hn g, ⟨(normal_subgroup_of_iff hA).mp hN n g hn.1 (mem_inf.mp g.2).1, mul_mem B (mul_mem B (mem_inf.1 g.2).2 (mem_inf.1 n.2).2) (inv_mem B (mem_inf.1 g.2).2)⟩ } instance sup_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] : (H βŠ” K).normal := { conj_mem := Ξ» n hmem g, begin change n ∈ ↑(H βŠ” K) at hmem, change g * n * g⁻¹ ∈ ↑(H βŠ” K), rw [normal_mul, set.mem_mul] at *, rcases hmem with ⟨h, k, hh, hk, rfl⟩, refine ⟨g * h * g⁻¹, g * k * g⁻¹, hH.conj_mem h hh g, hK.conj_mem k hk g, _⟩, simp end } @[to_additive] instance normal_inf_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] : (H βŠ“ K).normal := { conj_mem := Ξ» n hmem g, by { rw mem_inf at *, exact ⟨hH.conj_mem n hmem.1 g, hK.conj_mem n hmem.2 g⟩ } } @[to_additive] lemma subgroup_of_sup (A A' B : subgroup G) (hA : A ≀ B) (hA' : A' ≀ B) : (A βŠ” A').subgroup_of B = A.subgroup_of B βŠ” A'.subgroup_of B := begin refine map_injective_of_ker_le B.subtype (ker_le_comap _ _) (le_trans (ker_le_comap B.subtype _) le_sup_left) _, { simp only [subgroup_of, map_comap_eq, map_sup, subtype_range], rw [inf_of_le_right (sup_le hA hA'), inf_of_le_right hA', inf_of_le_right hA] }, end @[to_additive] lemma subgroup_normal.mem_comm {H K : subgroup G} (hK : H ≀ K) [hN : (H.subgroup_of K).normal] {a b : G} (hb : b ∈ K) (h : a * b ∈ H) : b * a ∈ H := begin have := (normal_subgroup_of_iff hK).mp hN (a * b) b h hb, rwa [mul_assoc, mul_assoc, mul_right_inv, mul_one] at this, end end subgroup_normal end subgroup namespace is_conj open subgroup lemma normal_closure_eq_top_of {N : subgroup G} [hn : N.normal] {g g' : G} {hg : g ∈ N} {hg' : g' ∈ N} (hc : is_conj g g') (ht : normal_closure ({⟨g, hg⟩} : set N) = ⊀) : normal_closure ({⟨g', hg'⟩} : set N) = ⊀ := begin obtain ⟨c, rfl⟩ := is_conj_iff.1 hc, have h : βˆ€ x : N, (mul_aut.conj c) x ∈ N, { rintro ⟨x, hx⟩, exact hn.conj_mem _ hx c }, have hs : function.surjective (((mul_aut.conj c).to_monoid_hom.restrict N).cod_restrict _ h), { rintro ⟨x, hx⟩, refine ⟨⟨c⁻¹ * x * c, _⟩, _⟩, { have h := hn.conj_mem _ hx c⁻¹, rwa [inv_inv] at h }, simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply, coe_mk, monoid_hom.restrict_apply, subtype.mk_eq_mk, ← mul_assoc, mul_inv_self, one_mul], rw [mul_assoc, mul_inv_self, mul_one] }, have ht' := map_mono (eq_top_iff.1 ht), rw [← monoid_hom.range_eq_map, monoid_hom.range_top_of_surjective _ hs] at ht', refine eq_top_iff.2 (le_trans ht' (map_le_iff_le_comap.2 (normal_closure_le_normal _))), rw [set.singleton_subset_iff, set_like.mem_coe], simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply, coe_mk, monoid_hom.restrict_apply, mem_comap], exact subset_normal_closure (set.mem_singleton _), end end is_conj /-! ### Actions by `subgroup`s These are just copies of the definitions about `submonoid` starting from `submonoid.mul_action`. -/ section actions namespace subgroup variables {Ξ± Ξ² : Type*} /-- The action by a subgroup is the action by the underlying group. -/ @[to_additive /-"The additive action by an add_subgroup is the action by the underlying add_group. "-/] instance [mul_action G Ξ±] (S : subgroup G) : mul_action S Ξ± := S.to_submonoid.mul_action @[to_additive] lemma smul_def [mul_action G Ξ±] {S : subgroup G} (g : S) (m : Ξ±) : g β€’ m = (g : G) β€’ m := rfl @[to_additive] instance smul_comm_class_left [mul_action G Ξ²] [has_scalar Ξ± Ξ²] [smul_comm_class G Ξ± Ξ²] (S : subgroup G) : smul_comm_class S Ξ± Ξ² := S.to_submonoid.smul_comm_class_left @[to_additive] instance smul_comm_class_right [has_scalar Ξ± Ξ²] [mul_action G Ξ²] [smul_comm_class Ξ± G Ξ²] (S : subgroup G) : smul_comm_class Ξ± S Ξ² := S.to_submonoid.smul_comm_class_right /-- Note that this provides `is_scalar_tower S G G` which is needed by `smul_mul_assoc`. -/ instance [has_scalar Ξ± Ξ²] [mul_action G Ξ±] [mul_action G Ξ²] [is_scalar_tower G Ξ± Ξ²] (S : subgroup G) : is_scalar_tower S Ξ± Ξ² := S.to_submonoid.is_scalar_tower /-- The action by a subgroup is the action by the underlying group. -/ instance [add_monoid Ξ±] [distrib_mul_action G Ξ±] (S : subgroup G) : distrib_mul_action S Ξ± := S.to_submonoid.distrib_mul_action end subgroup end actions
40d3757445e0521998125183d64acd809aadcab3
12dabd587ce2621d9a4eff9f16e354d02e206c8e
/world03/level07.lean
6c50d1e68f9055cb7d11ed97917fc4109421417e
[]
no_license
abdelq/natural-number-game
a1b5b8f1d52625a7addcefc97c966d3f06a48263
bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2
refs/heads/master
1,668,606,478,691
1,594,175,058,000
1,594,175,058,000
278,673,209
0
1
null
null
null
null
UTF-8
Lean
false
false
241
lean
lemma add_mul (a b t : mynat) : (a + b) * t = a * t + b * t := begin induction t with h hd, repeat {rw mul_zero}, refl, repeat {rw mul_succ}, rw hd, rw add_right_comm, rw add_assoc, rw add_assoc, rw add_comm (b * h), rw add_assoc, refl, end
f7fe836e8ecb255fcc7a467c07d1331bcfbae22f
471bedbd023d35c9d078c2f936dd577ace7f5813
/library/init/data/nat/basic.lean
0ebd580bebfedac7b1ab86e2971b52f9c9b7460c
[ "Apache-2.0" ]
permissive
lambdaxymox/lean
e06f0fa503666df827edd9867d7f49ca017aae64
fc13c8c72a15dab71a2c2b31410c2cadc3526bd7
refs/heads/master
1,666,785,407,985
1,666,153,673,000
1,666,153,673,000
310,165,986
0
0
Apache-2.0
1,604,542,096,000
1,604,542,095,000
null
UTF-8
Lean
false
false
5,985
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import init.logic notation `β„•` := nat namespace nat inductive less_than_or_equal (a : β„•) : β„• β†’ Prop | refl : less_than_or_equal a | step : Ξ  {b}, less_than_or_equal b β†’ less_than_or_equal (succ b) instance : has_le β„• := ⟨nat.less_than_or_equal⟩ @[reducible] protected def le (n m : β„•) := nat.less_than_or_equal n m @[reducible] protected def lt (n m : β„•) := nat.less_than_or_equal (succ n) m instance : has_lt β„• := ⟨nat.lt⟩ def pred : β„• β†’ β„• | 0 := 0 | (a+1) := a protected def sub : β„• β†’ β„• β†’ β„• | a 0 := a | a (b+1) := pred (sub a b) protected def mul : nat β†’ nat β†’ nat | a 0 := 0 | a (b+1) := (mul a b) + a instance : has_sub β„• := ⟨nat.sub⟩ instance : has_mul β„• := ⟨nat.mul⟩ -- defeq to the instance provided by comm_semiring instance : has_dvd β„• := has_dvd.mk (Ξ» a b, βˆƒ c, b = a * c) instance : decidable_eq β„• | zero zero := is_true rfl | (succ x) zero := is_false (Ξ» h, nat.no_confusion h) | zero (succ y) := is_false (Ξ» h, nat.no_confusion h) | (succ x) (succ y) := match decidable_eq x y with | is_true xeqy := is_true (xeqy β–Έ eq.refl (succ x)) | is_false xney := is_false (Ξ» h, nat.no_confusion h (Ξ» xeqy, absurd xeqy xney)) end def {u} repeat {Ξ± : Type u} (f : β„• β†’ Ξ± β†’ Ξ±) : β„• β†’ Ξ± β†’ Ξ± | 0 a := a | (succ n) a := f n (repeat n a) instance : inhabited β„• := ⟨nat.zero⟩ @[simp] lemma nat_zero_eq_zero : nat.zero = 0 := rfl /- properties of inequality -/ @[refl] protected lemma le_refl (a : β„•) : a ≀ a := less_than_or_equal.refl lemma le_succ (n : β„•) : n ≀ succ n := less_than_or_equal.step (nat.le_refl n) lemma succ_le_succ {n m : β„•} : n ≀ m β†’ succ n ≀ succ m := Ξ» h, less_than_or_equal.rec (nat.le_refl (succ n)) (Ξ» a b, less_than_or_equal.step) h protected lemma zero_le : βˆ€ (n : β„•), 0 ≀ n | 0 := nat.le_refl 0 | (n+1) := less_than_or_equal.step (zero_le n) lemma zero_lt_succ (n : β„•) : 0 < succ n := succ_le_succ n.zero_le lemma succ_pos (n : β„•) : 0 < succ n := zero_lt_succ n lemma not_succ_le_zero : βˆ€ (n : β„•), succ n ≀ 0 β†’ false . protected lemma not_lt_zero (a : β„•) : Β¬ a < 0 := not_succ_le_zero a lemma pred_le_pred {n m : β„•} : n ≀ m β†’ pred n ≀ pred m := Ξ» h, less_than_or_equal.rec_on h (nat.le_refl (pred n)) (Ξ» n, nat.rec (Ξ» a b, b) (Ξ» a b c, less_than_or_equal.step) n) lemma le_of_succ_le_succ {n m : β„•} : succ n ≀ succ m β†’ n ≀ m := pred_le_pred instance decidable_le : βˆ€ a b : β„•, decidable (a ≀ b) | 0 b := is_true b.zero_le | (a+1) 0 := is_false (not_succ_le_zero a) | (a+1) (b+1) := match decidable_le a b with | is_true h := is_true (succ_le_succ h) | is_false h := is_false (Ξ» a, h (le_of_succ_le_succ a)) end instance decidable_lt : βˆ€ a b : β„•, decidable (a < b) := Ξ» a b, nat.decidable_le (succ a) b protected lemma eq_or_lt_of_le {a b : β„•} (h : a ≀ b) : a = b ∨ a < b := less_than_or_equal.cases_on h (or.inl rfl) (Ξ» n h, or.inr (succ_le_succ h)) lemma lt_succ_of_le {a b : β„•} : a ≀ b β†’ a < succ b := succ_le_succ @[simp] lemma succ_sub_succ_eq_sub (a b : β„•) : succ a - succ b = a - b := nat.rec_on b (show succ a - succ zero = a - zero, from (eq.refl (succ a - succ zero))) (Ξ» b, congr_arg pred) lemma not_succ_le_self : βˆ€ n : β„•, Β¬succ n ≀ n := Ξ» n, nat.rec (not_succ_le_zero 0) (Ξ» a b c, b (le_of_succ_le_succ c)) n protected lemma lt_irrefl (n : β„•) : Β¬n < n := not_succ_le_self n protected lemma le_trans {n m k : β„•} (h1 : n ≀ m) : m ≀ k β†’ n ≀ k := less_than_or_equal.rec h1 (Ξ» p h2, less_than_or_equal.step) lemma pred_le : βˆ€ (n : β„•), pred n ≀ n | 0 := less_than_or_equal.refl | (succ a) := less_than_or_equal.step less_than_or_equal.refl lemma pred_lt : βˆ€ {n : β„•}, n β‰  0 β†’ pred n < n | 0 h := absurd rfl h | (succ a) h := lt_succ_of_le less_than_or_equal.refl protected lemma sub_le (a b : β„•) : a - b ≀ a := nat.rec_on b (nat.le_refl (a - 0)) (Ξ» b₁, nat.le_trans (pred_le (a - b₁))) protected lemma sub_lt : βˆ€ {a b : β„•}, 0 < a β†’ 0 < b β†’ a - b < a | 0 b h1 h2 := absurd h1 (nat.lt_irrefl 0) | (a+1) 0 h1 h2 := absurd h2 (nat.lt_irrefl 0) | (a+1) (b+1) h1 h2 := eq.symm (succ_sub_succ_eq_sub a b) β–Έ show a - b < succ a, from lt_succ_of_le (a.sub_le b) protected lemma lt_of_lt_of_le {n m k : β„•} : n < m β†’ m ≀ k β†’ n < k := nat.le_trans /- Basic nat.add lemmas -/ protected lemma zero_add : βˆ€ n : β„•, 0 + n = n | 0 := rfl | (n+1) := congr_arg succ (zero_add n) lemma succ_add : βˆ€ n m : β„•, (succ n) + m = succ (n + m) | n 0 := rfl | n (m+1) := congr_arg succ (succ_add n m) lemma add_succ (n m : β„•) : n + succ m = succ (n + m) := rfl protected lemma add_zero (n : β„•) : n + 0 = n := rfl lemma add_one (n : β„•) : n + 1 = succ n := rfl lemma succ_eq_add_one (n : β„•) : succ n = n + 1 := rfl /- Basic lemmas for comparing numerals -/ protected lemma bit0_succ_eq (n : β„•) : bit0 (succ n) = succ (succ (bit0 n)) := show succ (succ n + n) = succ (succ (n + n)), from congr_arg succ (succ_add n n) protected lemma zero_lt_bit0 : βˆ€ {n : nat}, n β‰  0 β†’ 0 < bit0 n | 0 h := absurd rfl h | (succ n) h := calc 0 < succ (succ (bit0 n)) : zero_lt_succ _ ... = bit0 (succ n) : (nat.bit0_succ_eq n).symm protected lemma zero_lt_bit1 (n : nat) : 0 < bit1 n := zero_lt_succ _ protected lemma bit0_ne_zero : βˆ€ {n : β„•}, n β‰  0 β†’ bit0 n β‰  0 | 0 h := absurd rfl h | (n+1) h := suffices (n+1) + (n+1) β‰  0, from this, suffices succ ((n+1) + n) β‰  0, from this, Ξ» h, nat.no_confusion h protected lemma bit1_ne_zero (n : β„•) : bit1 n β‰  0 := show succ (n + n) β‰  0, from Ξ» h, nat.no_confusion h end nat
328d7df4ae72bb4a448aad1b1b03db52a5aa4440
43390109ab88557e6090f3245c47479c123ee500
/src/finite_dimensional_vector_spaces/eigen.lean
f107fc8abaa14795e422a2b6889dd6bfa6eb0c42
[ "Apache-2.0" ]
permissive
Ja1941/xena-UROP-2018
41f0956519f94d56b8bf6834a8d39473f4923200
b111fb87f343cf79eca3b886f99ee15c1dd9884b
refs/heads/master
1,662,355,955,139
1,590,577,325,000
1,590,577,325,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,545
lean
/- Copyright (c) 2018 Keji Neri, Blair Shi. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Blair Shi -- This file aims to prove some theorems about eigenvalue and eigenvectors * `is_eigenvalue` `is_eigenvector`: constructed eigenvalues and eigenvectors in terms of linear map way * `is_eigenvalue_M` `is_eigenvector_M`: constructed eigenvalues and eigenvectors in terms of matrix way -- Proved two ways are equivalent * `is_diagonal_mat` : checked whether the given matrix is diagonal -- currently working on Cayley-Hamilton Theorem - Proved e β€’ V = (e β€’ I) * V, with e : F, V : vector F -/ import .linear_map .ring_n_is_module_of_vector import xenalib.Ellen_Arlt_matrix_rings import xenalib.Keji_further_matrix_things namespace M_module instance mo {R : Type} {n : β„•} [ring R] : module R (matrix R n 1) := { smul_add := @smul_add' _ _ 1 _, add_smul := @add_smul' _ _ 1 _, mul_smul := @mul_smul' _ _ 1 _, one_smul := @one_smul' _ _ 1 _, } end M_module def mat_to_finite_free_module {F : Type} {n : β„•} [ring F] (M : matrix F n 1) : finite_free_module F n := Ξ» I, M I 0 def finite_free_module_to_vec {F : Type} {n : β„•} (fn : finite_free_module F n) : vector F n := vector.of_fn fn instance add_hommm {F : Type} [ring F] {n : nat} : is_add_group_hom (@mat_to_finite_free_module F n _) := { add := begin intros a b, unfold mat_to_finite_free_module,refl, end } def module_hom_mat_to_finite_free_module {F : Type} [ring F] {n : nat} : is_linear_map (@mat_to_finite_free_module F n _) := { smul := begin intro c, intro x, unfold mat_to_finite_free_module, funext, refl, end, .. add_hommm } def mat_to_finite_free_module_is_linear {F : Type} [ring F] {n : nat}: linear_map (matrix F n 1) (finite_free_module F n) := ⟨mat_to_finite_free_module, module_hom_mat_to_finite_free_module⟩ def smul' {F : Type} {n : β„•} [ring F] (M : matrix F n n) (fn : finite_free_module F n) : finite_free_module F n := @mat_to_finite_free_module_is_linear F _ n (map_matrix.mat_mul_vec M (finite_free_module_to_vec fn)) theorem vector.nth_map {Ξ± Ξ² : Type*} {n : β„•} (v : vector Ξ± n) (f : Ξ± β†’ Ξ²) (m : fin n) : (v.map f).nth m = f (v.nth m) := begin cases v with l hl, unfold vector.nth vector.map, rw list.nth_le_map, end open M_module open map_matrix lemma smul_eq_smul_two {F : Type} {n : β„•} [ring F] (fn : finite_free_module F n) : βˆ€ a : F, (smul a fn) = mat_to_finite_free_module_is_linear.val (@smul_M F _ _ _ a (vec_to_mat((finite_free_module_to_vec fn)))) := begin intro a, unfold mat_to_finite_free_module_is_linear, simp, unfold mat_to_finite_free_module, unfold smul, funext, unfold smul_M, unfold vec_to_mat, unfold finite_free_module_to_vec, simp, end def is_eigenvalue_M {F : Type*} {n : β„•} [ring F] (M : matrix F n n ) (a : F) := βˆƒ v : (finite_free_module F n), (v β‰  (0 : finite_free_module F n)) ∧ (smul' M v = smul a v) def is_eigenvector_M {F : Type*} {n : β„•} [ring F] (M : matrix F n n) (fn : finite_free_module F n) := βˆƒ a : F, smul' M fn = smul a fn def is_eigenvalue {F : Type*} {n : β„•} [field F] [vector_space F (finite_free_module F n)] (T : linear_map (finite_free_module F n) (finite_free_module F n)) (a : F) := βˆƒ v : (finite_free_module F n) , (v β‰  (0 : (finite_free_module F n))) ∧ (T v = smul a v) def is_eigenvector {F : Type*} {n : β„•} [field F] [vector_space F (finite_free_module F n)] (T : linear_map (finite_free_module F n) (finite_free_module F n)) (v : finite_free_module F n) (h : v β‰  (0 : finite_free_module F n)) := βˆƒ a : F, T v = smul a v namespace eigen open function -- def smul {R : Type} {n : nat} [ring R] (s : R) (rn : finite_free_module R n) : -- finite_free_module R n := Ξ» I, s * (rn I) theorem finite_free_module_eq_vec {F : Type} [ring F] {n : nat} (fn : finite_free_module F n) : βˆ€ K, fn K = vector.nth (@finite_free_module_to_vec _ _ fn) K := begin intro, unfold finite_free_module_to_vec, simp, end def finite_free_module_to_row_mat {F : Type} {n : β„•} [ring F] (fn : finite_free_module F n) : matrix F 1 n := Ξ» I, Ξ» J, fn J def finite_free_module_to_col_mat {F : Type} {n : β„•} [ring F] (fn : finite_free_module F n) : matrix F n 1 := Ξ» I, Ξ» J, fn I theorem finite_free_module_eq_col_mat {F : Type} [ring F] {n : nat} (fn : finite_free_module F n) : βˆ€ K, fn K = (finite_free_module_to_col_mat fn) K 0 := begin intro, unfold finite_free_module_to_col_mat, end theorem finite_free_module_eq_row_mat {F : Type} [ring F] {n : nat} (fn : finite_free_module F n) : βˆ€ K, fn K = (finite_free_module_to_row_mat fn) 0 K := begin intro, unfold finite_free_module_to_row_mat, end -- A : matrix F n n -- The eigenvalues of TA are just those of A. (TA : Fn β†’ Fn,TA(v) = Av) open vector instance {F : Type} {n : β„•} [field F]: vector_space F (finite_free_module F n) := {} open map_matrix #check transpose_of_product theorem eigen_map_equiv_one {F : Type} {n : β„•} [field F] (M : matrix F n n) : βˆ€ eva : F, is_eigenvalue_M M eva ↔ @is_eigenvalue F n _ _ (@matrix_to_linear_map _ _ _ _ (Mα΅€)) eva := begin intro, split, intro hβ‚€, unfold is_eigenvalue, cases hβ‚€ with v, have h₁: v β‰  0 ∧ (matrix_to_linear_map (Mα΅€)) v = smul eva v, cases hβ‚€_h with hβ‚€_hl hβ‚€_hr, split, exact hβ‚€_hl, unfold smul' at hβ‚€_hr, unfold_coes at hβ‚€_hr, unfold mat_to_finite_free_module_is_linear at hβ‚€_hr, simp at hβ‚€_hr, unfold mat_to_finite_free_module at hβ‚€_hr, unfold map_matrix.mat_mul_vec at hβ‚€_hr, unfold matrix.mul at hβ‚€_hr, unfold map_matrix.vec_to_mat at hβ‚€_hr, show (matrix_to_linear_map (Mα΅€)).1 v = smul eva v, unfold matrix_to_linear_map, simp, unfold matrix_to_map, rw ←hβ‚€_hr, funext, simp only [(finite_free_module_eq_vec _ _).symm], conv in (v _ * (Mα΅€) _ i) begin rw [finite_free_module_eq_row_mat v _], end, conv in (M i _ * v _ ) begin rw [finite_free_module_eq_col_mat v _], end, show (matrix.mul F (finite_free_module_to_row_mat v) (Mα΅€)) 0 i = _, show _ = matrix.mul F M (finite_free_module_to_col_mat v) i 0, show _ =(matrix.mul F M (finite_free_module_to_col_mat v))α΅€ 0 i, rw [transpose_of_product], have hβ‚‚ : (finite_free_module_to_row_mat v) = finite_free_module_to_col_mat vα΅€, unfold finite_free_module_to_row_mat, unfold finite_free_module_to_col_mat, funext, rw [transpose], rw [hβ‚‚], existsi v, exact h₁, intro hβ‚€, unfold is_eigenvalue at hβ‚€, unfold is_eigenvalue_M, cases hβ‚€ with v, have h₁ : v β‰  0 ∧ smul' M v = smul eva v, split, cases hβ‚€_h with hβ‚€_hl hβ‚€_hr, exact hβ‚€_hl, unfold smul', cases hβ‚€_h with hβ‚€_hl hβ‚€_hr, unfold_coes at hβ‚€_hr, unfold matrix_to_linear_map at hβ‚€_hr, simp at hβ‚€_hr, unfold matrix_to_map at hβ‚€_hr, unfold_coes, unfold mat_to_finite_free_module_is_linear, simp, unfold mat_to_finite_free_module, unfold mat_mul_vec, unfold vec_to_mat, unfold matrix.mul, unfold finite_free_module_to_vec, rw [← hβ‚€_hr], funext, simp, conv in (v _ * (Mα΅€) _ I) begin rw [finite_free_module_eq_row_mat v _], end, conv in (M I _ * v _ ) begin rw [finite_free_module_eq_col_mat v _], end, rw [eq_comm], show (matrix.mul F (finite_free_module_to_row_mat v) (Mα΅€)) 0 I = _, show _ = matrix.mul F M (finite_free_module_to_col_mat v) I 0, show _ =(matrix.mul F M (finite_free_module_to_col_mat v))α΅€ 0 I, rw [transpose_of_product], have hβ‚‚ : (finite_free_module_to_row_mat v) = finite_free_module_to_col_mat vα΅€, unfold finite_free_module_to_row_mat, unfold finite_free_module_to_col_mat, funext, rw [transpose], rw [hβ‚‚], existsi v, exact h₁, end end eigen namespace Cayley_Hamilton open eigen theorem L0 {F : Type} [comm_ring F] {n : β„•} (v : finite_free_module F n): βˆ€ eva : F, smul_M eva (vec_to_mat (finite_free_module_to_vec v)) = matrix.mul F (smul_M eva (matrix.identity_matrix F)) (vec_to_mat (finite_free_module_to_vec v)) := begin intro eva, unfold smul_M, unfold matrix.mul, funext, unfold matrix.identity_matrix, unfold vec_to_mat, unfold finite_free_module_to_vec, simp, conv in ( _ * _ * _) begin rw [mul_assoc], end, conv begin to_rhs, rw [← finset.mul_sum], end, congr, rw [← @finset.sum_single _ _ _ _ (Ξ» (x : fin n), ite (I = x) 1 0 * v x) I _], simp, intro J, intro h, funext, simp, split_ifs, simp, apply false.elim, contradiction, simp, end lemma sub_eq_sub {R : Type} [ring R] {a b : β„•} (A B : matrix R a b) : A - B = matrix.sub R A B := begin refl, end @[simp] lemma val_eq_coe {Ξ± Ξ² Ξ³ : Type*} [ring Ξ±] [module Ξ± Ξ²] [module Ξ± Ξ³] (f : linear_map Ξ² Ξ³) : f.val = f := rfl theorem L1 {R : Type} [comm_ring R] {n : β„•} (M : matrix R n n) (v : finite_free_module R n): βˆ€ eva : R, (smul' M v = smul eva v) ↔ smul' (matrix.sub R M (smul_M eva (@matrix.identity_matrix R _ n))) v = (0 : finite_free_module R n) := begin intro eva, split, intro hβ‚€, rw [← sub_eq_zero] at hβ‚€, unfold smul' at hβ‚€, unfold smul', simp only [smul_eq_smul_two] at hβ‚€, unfold mat_mul_vec, unfold mat_mul_vec at hβ‚€, rw val_eq_coe at hβ‚€, conv at hβ‚€ begin to_lhs, rw ← @linear_map.map_sub _ _ _ _ _ _ (mat_to_finite_free_module_is_linear) _ _, end, have h₁ : matrix.sub R (matrix.mul R M (vec_to_mat (finite_free_module_to_vec v))) (smul_M eva (vec_to_mat (finite_free_module_to_vec v))) = matrix.mul R (matrix.sub R M (smul_M eva (matrix.identity_matrix R))) (vec_to_mat (finite_free_module_to_vec v)), rw [← matrix.mul_sub_mul], congr, rw [L0], conv at hβ‚€ in (matrix.mul R M _ - smul_M eva _) begin rw [sub_eq_sub], rw [h₁], end, exact hβ‚€, intro hβ‚€, rw [← sub_eq_zero], unfold smul' at hβ‚€, unfold_coes at hβ‚€, unfold mat_mul_vec at hβ‚€, conv at hβ‚€ in ((matrix.mul R (matrix.sub R _ _) _)) begin rw [← matrix.mul_sub_mul], end, unfold mat_to_finite_free_module_is_linear at hβ‚€, simp at hβ‚€, have h₁ : mat_to_finite_free_module (matrix.sub R (matrix.mul R M (vec_to_mat (finite_free_module_to_vec v))) (matrix.mul R (smul_M eva (matrix.identity_matrix R)) (vec_to_mat (finite_free_module_to_vec v)))) = smul' M v - smul eva v, unfold smul', unfold smul, unfold mat_to_finite_free_module, unfold_coes, unfold mat_to_finite_free_module_is_linear, simp, unfold mat_to_finite_free_module, unfold matrix.sub, unfold mat_mul_vec, simp, funext, congr, unfold matrix.mul, unfold vec_to_mat, unfold finite_free_module_to_vec, simp, unfold smul_M, conv in (_ * _ * _) begin rw [mul_assoc], end, rw [← finset.mul_sum], congr, rw [eq_comm], unfold matrix.identity_matrix, rw [← @finset.sum_single _ _ _ _ (Ξ» (x : fin n), ite (I = x) 1 0 * v x) I _], simp, intro J, intro h, funext, simp, split_ifs, simp, apply false.elim, contradiction, simp, rw [h₁] at hβ‚€, exact hβ‚€, end -- noncomputable def det_grl {R : Type} [comm_ring R] : Ξ  {n : β„•} (M : matrix R (n + 1) (n + 1)) -- (a : fin (n + 1)), R -- | 0 M a := M 0 0 -- | (n + 1) M a := -- finset.sum finset.univ (Ξ» (K : fin (n + 1)), M a K * -- (((-1) ^ (a.val + K.val)) * (Ξ» b : fin n, @det_grl n (minor M a.val K.val) b))) -- theorem det_grl_eq_det {R : Type} [comm_ring R] : -- βˆ€ n : β„•, βˆ€ M : matrix R (n + 1) (n + 1), βˆ€ a : fin (n + 1), -- det_grl M a = det M := -- begin -- intros n M a, -- unfold det, -- unfold det_grl, -- end theorem L3 {R : Type} [comm_ring R] {n : β„•} : βˆ€ a : R, smul_M a (@matrix.identity_matrix _ _ n) = Ξ» I J, if I = J then a else 0 := begin intro a, unfold smul_M, funext, unfold matrix.identity_matrix, split_ifs, rw [mul_one], rw [mul_zero], end #check equiv.perm theorem L2 {R : Type} [comm_ring R] {n : β„•} (M : matrix R (n + 1) (n + 1)) : smul_M (det M) (@matrix.identity_matrix _ _ (n + 1)) = matrix.mul R M (adj M) := begin rw [L3], funext, unfold matrix.mul, split_ifs, unfold adj, unfold transpose, unfold cofactor, unfold_coes, unfold det, unfold minor, funext, sorry end theorem Cayley_Hamilton {R : Type} [comm_ring R] {n : β„•} (M : matrix R n n) : βˆ€ eva : R, @det n R _ (matrix.sub R M (smul_M eva (@matrix.identity_matrix R _ n))) = 0 ↔ is_eigenvalue_M M eva := begin intro, split, intro hβ‚€, show is_eigenvalue_M M eva, sorry, intro hβ‚€, unfold is_eigenvalue_M at hβ‚€, cases hβ‚€ with v, cases hβ‚€_h with hβ‚€_hl hβ‚€_hl, rw [L1] at hβ‚€_hl, sorry end end Cayley_Hamilton namespace diagonal def is_diagonal_mat {R : Type} [ring R] {n: β„•} (M : matrix R n n) : Prop := βˆ€ I J, (I β‰  J) β†’ (M I J = 0) -- Proposition 7.5. Suppose V is a f.d.v.s, and B = {v1, ..., vn} is a basis. -- Suppose T : V β†’ V is a linear map. Then, -- [T]B is diagonal⇔v1,...,vn are eigenvectors of T -- theorem diagonal_eigen_basis {R : Type} [ring R] {a b : β„•} (M : ) -- Definition 7.6. If V is a f.d.v.s, and T : V β†’ V is a linear map, say that T is diagonalisable if -- there a basis of V consisting of eigenvectors of V . -- Corollary 7.7. Suppose V is a f.d.v.s. with a basis B = {v1, ..., vn}. Suppose T : V β†’ V is a -- linear map. Then the following are equivalent: i T is diagonalisable -- ii There is a basis C of V with [T]C is diagonal -- iii There is an invertible nΓ—n matrix P with Pβˆ’1[T]BP a diagonal matrix end diagonal
f5c62f252734eac79a36068e4645b07afcaa9fbd
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/omega/nat/preterm_auto.lean
d946a9c83be81cd72023cbad6debf44fb4c5f170
[]
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
2,640
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.omega.term import Mathlib.PostPort universes l namespace Mathlib /- Linear natural number arithmetic terms in pre-normalized form. -/ namespace omega namespace nat /-- The shadow syntax for arithmetic terms. All constants are reified to `cst` (e.g., `5` is reified to `cst 5`) and all other atomic terms are reified to `exp` (e.g., `5 * (list.length l)` is reified to `exp 5 \`(list.length l)`). `exp` accepts a coefficient of type `nat` as its first argument because multiplication by constant is allowed by the omega test. -/ /-- Similar to `exprterm`, except that all exprs are now replaced with de Brujin indices of type `nat`. This is akin to generalizing over the terms represented by the said exprs. -/ inductive preterm where | cst : β„• β†’ preterm | var : β„• β†’ β„• β†’ preterm | add : preterm β†’ preterm β†’ preterm | sub : preterm β†’ preterm β†’ preterm namespace preterm /-- Helper tactic for proof by induction over preterms -/ /-- Preterm evaluation -/ def val (v : β„• β†’ β„•) : preterm β†’ β„• := sorry @[simp] theorem val_const {v : β„• β†’ β„•} {m : β„•} : val v (cst m) = m := rfl @[simp] theorem val_var {v : β„• β†’ β„•} {m : β„•} {n : β„•} : val v (var m n) = m * v n := sorry @[simp] theorem val_add {v : β„• β†’ β„•} {t : preterm} {s : preterm} : val v (add t s) = val v t + val v s := rfl @[simp] theorem val_sub {v : β„• β†’ β„•} {t : preterm} {s : preterm} : val v (sub t s) = val v t - val v s := rfl /-- Fresh de Brujin index not used by any variable in argument -/ def fresh_index : preterm β†’ β„• := sorry /-- If variable assignments `v` and `w` agree on all variables that occur in term `t`, the value of `t` under `v` and `w` are identical. -/ theorem val_constant (v : β„• β†’ β„•) (w : β„• β†’ β„•) (t : preterm) : (βˆ€ (x : β„•), x < fresh_index t β†’ v x = w x) β†’ val v t = val w t := sorry def repr : preterm β†’ string := sorry @[simp] def add_one (t : preterm) : preterm := add t (cst 1) /-- Preterm is free of subtractions -/ def sub_free : preterm β†’ Prop := sorry end preterm /-- Return a term (which is in canonical form by definition) that is equivalent to the input preterm -/ @[simp] def canonize : preterm β†’ term := sorry @[simp] theorem val_canonize {v : β„• β†’ β„•} {t : preterm} : preterm.sub_free t β†’ term.val (fun (x : β„•) => ↑(v x)) (canonize t) = ↑(preterm.val v t) := sorry end Mathlib
069dc5dd4471ebd4a20f0702e8eae08e8232c99e
12dabd587ce2621d9a4eff9f16e354d02e206c8e
/world08/level07.lean
7ce6c43e9023460d4c83cadb3d06efba83e5b69a
[]
no_license
abdelq/natural-number-game
a1b5b8f1d52625a7addcefc97c966d3f06a48263
bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2
refs/heads/master
1,668,606,478,691
1,594,175,058,000
1,594,175,058,000
278,673,209
0
1
null
null
null
null
UTF-8
Lean
false
false
138
lean
theorem add_right_cancel_iff (t a b : mynat) : a + t = b + t ↔ a = b := begin split, exact add_right_cancel _ _ _, intro h, rwa h, end
5c8c32e58fb15521e9fecd27e339015764376583
fe84e287c662151bb313504482b218a503b972f3
/src/data/list_extra.lean
fa5a8e2363853c4113cff5d96bbd2b10e9f757a9
[]
no_license
NeilStrickland/lean_lib
91e163f514b829c42fe75636407138b5c75cba83
6a9563de93748ace509d9db4302db6cd77d8f92c
refs/heads/master
1,653,408,198,261
1,652,996,419,000
1,652,996,419,000
181,006,067
4
1
null
null
null
null
UTF-8
Lean
false
false
3,655
lean
import data.list.basic data.list.big_operators logic.embedding import tactic.squeeze namespace list universes u v w x variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type x} open list def all_prop {Ξ± : Type*} (p : Ξ± β†’ Prop) : βˆ€ (l : list Ξ±), Prop | nil := true | (a :: l) := (p a) ∧ (all_prop l) lemma all_prop_iff {Ξ± : Type*} {p : Ξ± β†’ Prop} : βˆ€ {l : list Ξ±}, all_prop p l ↔ βˆ€ a, a ∈ l β†’ p a | nil := by {rw[all_prop,true_iff],intros a ha,cases ha,} | (cons m l) := by {rw[all_prop],split, {rintro ⟨hm,hl⟩ a ha,rcases ha,exact ha.symm β–Έ hm, exact (all_prop_iff.mp hl) a ha}, {intro h,split, exact h m (mem_cons_self m l), apply all_prop_iff.mpr,intros a ha, exact h a (mem_cons_of_mem m ha), } } def cons_embedding (a : Ξ±) : list Ξ± β†ͺ list Ξ± := ⟨cons a,cons_injective⟩ def concat_embedding (a : Ξ±) : list Ξ± β†ͺ list Ξ± := ⟨λ l, l ++ [a],Ξ» l₁ lβ‚‚ e, append_right_cancel e⟩ def rtake (n : β„•) (l : list Ξ±) := l.drop (l.length - n) def rdrop (n : β„•) (l : list Ξ±) := l.take (l.length - n) lemma eq_singleton : βˆ€ (l : list Ξ±) (h : l.length = 1), l = [l.nth_le 0 (by {rw[h],exact nat.lt_succ_self 0})] | [] h := by {cases h} | [a] h := rfl | (_ :: _ :: _) h := by {cases h} lemma nth_le_congr (l : list Ξ±) {n₁ nβ‚‚ : β„•} (h₁ : n₁ < l.length) (e : n₁ = nβ‚‚) : l.nth_le n₁ h₁ = l.nth_le nβ‚‚ (e β–Έ h₁) := by {cases e,refl} lemma nth_le_append' : βˆ€ (l₁ : list Ξ±) {lβ‚‚ : list Ξ±} {n : β„•} (hn : n < lβ‚‚.length), (l₁ ++ lβ‚‚).nth_le (l₁.length + n) ((list.length_append l₁ lβ‚‚).symm β–Έ (nat.add_lt_add_left hn l₁.length)) = lβ‚‚.nth_le n hn | list.nil lβ‚‚ n hn := by {congr,dsimp[length],rw[zero_add]} | (a :: l₁) lβ‚‚ n hn := begin let h := nth_le_append' l₁ hn, dsimp[append],rw[h.symm], have : l₁.length + 1 + n = (l₁.length + n) + 1 := by {rw[add_assoc,add_comm 1,← add_assoc],}, rw[nth_le_congr _ _ this,nth_le],refl, end #check list.nth_le_take lemma nth_le_take_old : βˆ€ {n m : β„•} {l : list Ξ±} (hn : n < m) (hm : m ≀ l.length), (l.take m).nth_le n (by {rw[length_take,min_eq_left hm], exact hn}) = l.nth_le n (lt_of_lt_of_le hn hm) | n 0 l hn hm := by {cases hn} | n (m + 1) [] hn hm := by {cases hm} | 0 (m + 1) (a :: l) hn hm := rfl | (n + 1) (m + 1) (a :: l) hn hm := nth_le_take_old (nat.lt_of_succ_lt_succ hn) (nat.le_of_succ_le_succ hm) #check list.nth_le_drop lemma nth_le_drop_old : βˆ€ {n m : β„•} {l : list Ξ±} (h : m + n < l.length), (l.drop m).nth_le n (by {rw[length_drop],rw[add_comm] at h,exact lt_tsub_iff_right.mpr h}) = l.nth_le (m + n) h | n 0 l h := (nth_le_congr l h (zero_add n)).symm | n (m + 1) [] h := by {cases h} | n (m + 1) (a :: l) h := begin dsimp[drop], have : m + 1 + n = (m + n) + 1 := by {rw[add_assoc,add_comm 1,← add_assoc],}, rw[this] at h, rw[nth_le_congr _ _ this],dsimp[nth_le],apply nth_le_drop_old, end lemma drop_eq_last {n : β„•} {l : list Ξ±} (h : l.length = n + 1) : (l.drop n) = [l.nth_le n (h.symm β–Έ n.lt_succ_self)] := begin let t := l.drop n, let a := l.nth_le n (h.symm β–Έ n.lt_succ_self), change t = [a], let a' := t.nth_le 0 _, have : a = a' := begin dsimp[a,a',last], let h := @list.nth_le_drop_old Ξ± 0 n l (by {rw[h,add_zero],exact n.lt_succ_self}), rw[list.nth_le_congr _ _ (add_zero n)] at h, exact h.symm, end, rw[this], have t_len : t.length = 1 := by { rw[list.length_drop,h,nat.add_sub_cancel_left]}, exact eq_singleton t t_len, end #check list.sum_singleton lemma sum_singleton_old [add_monoid Ξ±] (a : Ξ±) : [a].sum = a := by {rw[list.sum_cons,sum_nil,add_zero]} end list
443f265b100fdfb6ba105ff5a66d036fdb3c8bc4
e38d5e91d30731bef617cc9b6de7f79c34cdce9a
/src/core/connection/join.lean
b6ee75d7ffaf921169a8ea9e1abe6bac9180c63c
[ "Apache-2.0" ]
permissive
bbentzen/cubicalean
55e979c303fbf55a81ac46b1000c944b2498be7a
3b94cd2aefdfc2163c263bd3fc6f2086fef814b5
refs/heads/master
1,588,314,875,258
1,554,412,699,000
1,554,412,699,000
177,333,390
0
0
null
null
null
null
UTF-8
Lean
false
false
6,851
lean
/- Copyright (c) 2019 Bruno Bentzen. All rights reserved. Released under the Apache License 2.0 (see "License"); Author: Bruno Bentzen -/ import .meet open interval -- the join connection def join.horn {A : Type} (kan : has_hcom2 A) (p : I β†’ A) : horn2 A := let u := horn1.mk (Ξ» _, p i0) (Ξ» _, p i0) p rfl rfl in horn2.mk (Ξ» _ _, p i0) (meet kan p) (Ξ» i _, p i) (meet kan p) (Ξ» i _, p i) ((kan.eq0j (meet.horn kan p)).symm) rfl ((kan.eq0j (meet.horn kan p)).symm) rfl rfl begin transitivity, apply kan.eq1i (meet.horn kan p), apply (kan.has1.t1 u) end begin symmetry, transitivity, apply kan.eq1i (meet.horn kan p), apply (kan.has1.t1 u) end rfl def join.filler {A : Type} (kan : has_hcom2 A) (p : I β†’ A) : I β†’ I β†’ I β†’ A := kan.hcom2 (join.horn kan p) def join {A : Type} (kan : has_hcom2 A) (p : I β†’ A) : I β†’ I β†’ A := (join.filler kan p) i1 --notation p `[` j `∨` i `]` kan := join kan p j i namespace join lemma face0i {A : Type} (kan : has_hcom2 A) (p : I β†’ A) : join kan p i0 = p := begin transitivity, apply kan.eq0j (join.horn kan p), transitivity, apply kan.eq1j (meet.horn kan p), apply (kan.has1.t1 (horn1.mk (Ξ» _, p i0) (Ξ» _, p i0) p rfl rfl)) end lemma face1i {A : Type} (kan : has_hcom2 A) (p : I β†’ A) : join kan p i1 = Ξ» _, p i1 := kan.eq1j (join.horn kan p) lemma face1j {A : Type} (kan : has_hcom2 A) (p : I β†’ A) : (Ξ» i, join kan p i i1) = Ξ» _, p i1 := kan.eq1i (join.horn kan p) lemma face0j {A : Type} (kan : has_hcom2 A) (p : I β†’ A) : (Ξ» i, join kan p i i0) = p := begin transitivity, apply kan.eq0i, -- (join.horn kan p) transitivity, apply kan.eq1j, -- (meet.horn kan p) apply (kan.has1.t1) -- (horn1.mk (Ξ» _, p i0) (Ξ» _, p i0) p rfl rfl) end end join /- p --> i p i0 -----------------> p i1 j | | || v | || | || p | join p j i || Ξ» _, p i1 | || | || v || p i1 ================== p i1 Ξ» _, p i1 -/ /-def test2 {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) : join kan (Ξ» (i : I), p @@ i) i0 i0 = eq.mp rfl (p@@i0) := begin rw join.face0i, refl --apply path.app0 end def test3 {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) : join kan (Ξ» (i : I), p @@ i) i0 i1 = eq.mp rfl (p@@i1) := begin rw join.face0i, refl end def test31 {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) : (Ξ» i, p @@ i) i1 = eq.mp rfl (p @@ i1) := begin refl end variables {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) #check join kan (Ξ» i, p@@i) i0 #check (Ξ» i, p@@i) #check @pathdp.abs #check pathdp.abs (Ξ» i, p@@i) rfl rfl #check pathdp.abs (join kan (Ξ» (i : I), p @@ i) i0) rfl rfl def joinsquare22 {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) : pathdp.abs (join kan (Ξ» i, p@@i) i0) rfl rfl (test2 kan p) (test3 kan p) = eq.mp (path.tyeq p) p := begin transitivity, apply pathdp.abseq, apply join.face0i, --rw pathdp.eta, --have h : pathdp.abs (Ξ» (i : I), p @@ i) rfl rfl _ _ = -- pathdp.abs (Ξ» (i : I), p @@ i) rfl rfl (pathdp.app0 p) (pathdp.app1 p) := -- pathdp.abs_irrel, apply pathdp.abs_irrel', --apply eq.mp, apply pathdp.abs_irrel, transitivity, exact pathdp.eta p rfl rfl (pathdp.app0 p) (pathdp.app1 p), end def joinsquare {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) : pathdp.abs (join kan (Ξ» i, p@@i) i0) rfl rfl (test2 kan p) (test3 kan p) = eq.mp (path.tyeq p) p := begin --induction ((join.face0i kan (Ξ» i, p@@i)).symm), transitivity, apply pathdp.eta.eq, end def tyeq_refl {A : I β†’ Type} {A0 A1 : Type} {a : A i0} {b : A i1} (p : Ξ  i, A i) {ha : A i0 = A i0} {hb : A i1 = A i1} (p0 : p i0 = eq.mp ha a) (p1 : p i1 = eq.mp hb b) : (pathd.tyeq (pathdp.abs p ha hb p0 p1)) = refl := sorry def joinsquare'' {A : Type} {a b : A} (p : path A a b) : pathdp.abs (pathdp.app p) rfl rfl rfl rfl = eq.mp (pathd.tyeq p) p := begin induction p, --induction p_ha, --induction (path.tyeq p),--induction p, --induction ((join.face0i kan (Ξ» i, p@@i)).symm), end def joinsquare' {A : Type} {a b : A} (p : path A a b) (q : I β†’ A) (h0 : q i0 = eq.mp rfl (p @@ i0)) (h1 : q i1 = eq.mp rfl (p @@ i1)) : pathdp.abs q rfl rfl h0 h1 = eq.mp (path.tyeq p) p := begin --induction ((join.face0i kan (Ξ» i, p@@i)).symm), end --calc -- pathdp.abs (join kan (Ξ» i, p@@i) i0) rfl rfl (test2 kan p) (test3 kan p) = -- pathdp.abs (Ξ» i, p @@ i) rfl rfl rfl rfl : eq.rec _ (join.face0i kan (Ξ» i, p@@i)).symm -- ... = eq.mp (path.tyeq p) p : sorry --eq.rec _ (join.face0i kan (Ξ» i, p@@i)).symm -- eq.rec (eq.rec _ (pathdp.eta p (pathdp.app0 p) (pathdp.app1 p))) (join.face0i kan (Ξ» i, p@@i)).symm -- (eq.rec _ (pathdp.eta (app p) (test2 kan p) (test3 kan p))) --(eq.rec rfl (pathd.app0 p).symm) (eq.rec rfl (pathd.app1 p).symm) def join0 {A : Type} {a b : A} (p : path A a b) : Type := pathdp (Ξ» j, pathd (Ξ» _, A) (p @@ j) b) p (path.refl b) --set_option pp.implicit true lemma pathjoin {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) : pathdp (Ξ» j, pathd (Ξ» _, A) (p @@ j) b) p (path.refl b) := begin fapply pathdp.abs, intro j, fapply pathdp.abs, intro i, apply join kan, apply (Ξ» i, p @@ i), apply j, apply i, repeat {refl}, apply (calc join kan (Ξ» i, p@@i) j i0 = (Ξ» i, join kan (Ξ» i, p@@i) i i0) j : rfl ... = p@@j : by rw join.face0j kan ), apply (calc join kan (Ξ» i, p@@i) j i1 = (Ξ» i, join kan (Ξ» i, p@@i) i i1) j : rfl ... = p@@i1 : by rw join.face1j kan ... = b : by apply path.app1 ), --rw pathd.app0, refl, --rw pathd.app1, refl, exact (eq.rec rfl (pathd.app0 p).symm), exact (eq.rec rfl (pathd.app1 p).symm), --simp, simp, transitivity, --rw join.face0i, -- (join.face0i kan (app p)), simp, simp, --rw join.face01 kan, --exact (eq.rec (Ξ» x, pathdp.abs x _ _ _ _ = eq.rec p (eq.rec rfl (pathd.app0 p).symm)) (join.face0i kan (app p)) ) --rw join.face01 kan (@app (Ξ» (_x : I), A) ((Ξ» (_x : I), A) i0) ((Ξ» (_x : I), A) i1) a b _ _ p), -- (@join A kan (@app (Ξ» (_x : I), A) ((Ξ» (_x : I), A) i0) ((Ξ» (_x : I), A) i1) a b _ _ p) --apply pathdp.eta, -- join kan (Ξ» (i : I), p@@i) j i0 = p@@j end -/
3ba4f47ddbadc760547428c3792263cb39d3b54f
90edd5cdcf93124fe15627f7304069fdce3442dd
/stage0/src/Lean/Aesop/Tree.lean
393f578e35a53421951fe88ea5bf97ffad5e5ed1
[ "Apache-2.0" ]
permissive
JLimperg/lean4-aesop
8a9d9cd3ee484a8e67fda2dd9822d76708098712
5c4b9a3e05c32f69a4357c3047c274f4b94f9c71
refs/heads/master
1,689,415,944,104
1,627,383,284,000
1,627,383,284,000
377,536,770
0
0
null
null
null
null
UTF-8
Lean
false
false
15,781
lean
/- Copyright (c) 2021 Jannis Limperg. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jannis Limperg, Asta HalkjΓ¦r From -/ import Lean.Aesop.MutAltTree import Lean.Aesop.Rule import Lean.Aesop.Util import Lean.Aesop.Tracing open Lean open Lean.Meta @[inlineIfReduce] private def Bool.toYesNo : Bool β†’ Format | true => "yes" | false => "no " namespace Lean.Aesop /-! ## Node IDs -/ structure GoalId where toNat : Nat deriving Inhabited, DecidableEq namespace GoalId protected def zero : GoalId := ⟨0⟩ protected def one : GoalId := ⟨1⟩ protected def succ : GoalId β†’ GoalId | ⟨n⟩ => ⟨n + 1⟩ def dummy : GoalId := ⟨1000000000000000⟩ instance : LT GoalId where lt n m := n.toNat < m.toNat instance : DecidableRel (Ξ± := GoalId) (Β· < Β·) := Ξ» n m => inferInstanceAs (Decidable (n.toNat < m.toNat)) instance : ToFormat GoalId where format n := format n.toNat end GoalId /-! ## Rule Application IDs -/ structure RappId where toNat : Nat deriving Inhabited, DecidableEq namespace RappId protected def zero : RappId := ⟨0⟩ protected def succ : RappId β†’ RappId | ⟨n⟩ => ⟨n + 1⟩ protected def one : RappId := ⟨1⟩ def dummy : RappId := ⟨1000000000000000⟩ instance : LT RappId where lt n m := n.toNat < m.toNat instance : DecidableRel (Ξ± := RappId) (Β· < Β·) := Ξ» n m => inferInstanceAs $ Decidable (n.toNat < m.toNat) instance : ToFormat RappId where format n := format n.toNat end RappId /-! ## Goal Nodes and Rule Applications -/ structure GoalData : Type where id : GoalId goal : MVarId successProbability : Percent normalizationProof : Option Expr failedRapps : List RegularRule unsafeQueue : Option (List UnsafeRule) proven? : Bool unprovable? : Bool irrelevant? : Bool deriving Inhabited namespace GoalData def normal? (g : GoalData) : Bool := g.normalizationProof.isSome protected structure MessageInfo where showGoal : Bool showUnsafeQueue : Bool showFailedRapps : Bool deriving Inhabited open GoalData (MessageInfo) variable [Monad m] [MonadOptions m] protected def getMessageInfo (traceCtx : TraceContext) : m MessageInfo := do return { showGoal := (← TraceOption.showGoals.get traceCtx) showUnsafeQueue := (← TraceOption.showUnsafeQueues.get traceCtx) showFailedRapps := (← TraceOption.showFailedRapps.get traceCtx) } open MessageData in protected def toMessageData' (minfo : MessageInfo) (g : GoalData) : MessageData := let unsafeQueueLength := match g.unsafeQueue with | none => f!"<not selected>" | some q => format q.length m!"Goal {g.id} [{g.successProbability.toHumanString}]" ++ nodeFiltering #[ m!"Unsafe rules in queue: {unsafeQueueLength}, failed: {g.failedRapps.length}", join [ m!"normal: {g.normal?.toYesNo} | ", m!"proven: {g.proven?.toYesNo} | ", m!"unprovable: {g.unprovable?.toYesNo} | ", m!"irrelevant: {g.irrelevant?.toYesNo}" ], if Β¬ minfo.showGoal then none else m!"Goal:{indentD $ ofGoal g.goal}", if Β¬ minfo.showUnsafeQueue && g.unsafeQueue.isSome then none else m!"Unsafe queue:{indentDUnlines $ g.unsafeQueue.get!.map toMessageData}", if Β¬ minfo.showFailedRapps then none else m!"Failed rule applications:{indentDUnlines $ g.failedRapps.map toMessageData}" ] protected def toMessageData (traceCtx : TraceContext) (g : GoalData) : m MessageData := return g.toMessageData' (← GoalData.getMessageInfo traceCtx) protected def mkInitial (id : GoalId) (goal : MVarId) (successProbability : Percent) : GoalData where id := id goal := goal successProbability := successProbability normalizationProof := none failedRapps := [] unsafeQueue := none proven? := false unprovable? := false irrelevant? := false end GoalData structure RappData : Type where id : RappId appliedRule : RegularRule successProbability : Percent proof : Expr proven? : Bool unprovable? : Bool irrelevant? : Bool deriving Inhabited namespace RappData protected structure MessageInfo where showProof : Bool open RappData (MessageInfo) variable [Monad m] [MonadOptions m] protected def getMessageInfo (traceCtx : TraceContext) : m MessageInfo := return { showProof := (← TraceOption.showProofs.get traceCtx) } open MessageData in protected def toMessageData' (minfo : MessageInfo) (r : RappData) : MessageData := m!"Rule application {r.id} [{r.successProbability}]" ++ nodeFiltering #[ toMessageData r.appliedRule, join [ m!"proven: {r.proven?.toYesNo} | ", m!"unprovable: {r.unprovable?.toYesNo} | ", m!"irrelevant: {r.irrelevant?.toYesNo}" ], if Β¬ minfo.showProof then none else m!"Proof:{indentD r.proof}" ] protected def toMessageData (traceCtx : TraceContext) (r : RappData) : m MessageData := return r.toMessageData' (← RappData.getMessageInfo traceCtx) protected def mkInitial (id : RappId) (appliedRule : RegularRule) (successProbability : Percent) (proof : Expr) : RappData where id := id appliedRule := appliedRule successProbability := successProbability proof := proof proven? := false unprovable? := false irrelevant? := false end RappData abbrev Goal := MutAltTree IO.RealWorld GoalData RappData abbrev GoalRef := IO.Ref Goal abbrev Rapp := MutAltTree IO.RealWorld RappData GoalData abbrev RappRef := IO.Ref Rapp variable [Monad m] [MonadLiftT (ST IO.RealWorld) m] /-! ## Functions on Goals -/ namespace Goal /-! ### Constructors -/ @[inline] protected def mk (parent : Option RappRef) (rapps : Array RappRef) (data : GoalData) : Goal := MutAltTree.mk data parent rapps /-! ### Getters -/ @[inline] def rapps (g : Goal) : Array RappRef := g.children @[inline] def id (g : Goal) : GoalId := g.payload.id @[inline] def goal (g : Goal) : MVarId := g.payload.goal @[inline] def successProbability (g : Goal) : Percent := g.payload.successProbability @[inline] def normalizationProof (g : Goal) : Option Expr := g.payload.normalizationProof @[inline] def failedRapps (g : Goal) : List RegularRule := g.payload.failedRapps @[inline] def unsafeQueue (g : Goal) : Option (List UnsafeRule) := g.payload.unsafeQueue @[inline] def proven? (g : Goal) : Bool := g.payload.proven? @[inline] def unprovable? (g : Goal) : Bool := g.payload.unprovable? @[inline] def irrelevant? (g : Goal) : Bool := g.payload.irrelevant? /-! ### Setters -/ @[inline] def setId (id : GoalId) (g : Goal) : Goal := g.modifyPayload Ξ» d => { d with id := id } @[inline] def setGoal (goal : MVarId) (g : Goal) : Goal := g.modifyPayload Ξ» d => { d with goal := goal } @[inline] def setSuccessProbability (successProbability : Percent) (g : Goal) : Goal := g.modifyPayload Ξ» d => { d with successProbability := successProbability } @[inline] def setNormalizationProof (normalizationProof : Expr) (g : Goal) : Goal := g.modifyPayload Ξ» d => { d with normalizationProof := normalizationProof } @[inline] def setFailedRapps (failedRapps : List RegularRule) (g : Goal) : Goal := g.modifyPayload Ξ» d => { d with failedRapps := failedRapps } @[inline] def setUnsafeQueue (unsafeQueue : Option (List UnsafeRule)) (g : Goal) : Goal := g.modifyPayload Ξ» d => { d with unsafeQueue := unsafeQueue } @[inline] def setProven? (proven? : Bool) (g : Goal) : Goal := g.modifyPayload Ξ» d => { d with proven? := proven? } @[inline] def setUnprovable? (unprovable? : Bool) (g : Goal) : Goal := g.modifyPayload Ξ» d => { d with unprovable? := unprovable? } @[inline] def setIrrelevant? (irrelevant? : Bool) (g : Goal) : Goal := g.modifyPayload Ξ» d => { d with irrelevant? := irrelevant? } /-! ### Miscellaneous -/ @[inline] def normal? (g : Goal) : Bool := g.payload.normal? def hasNoUnexpandedUnsafeRule (g : Goal) : Bool := match g.unsafeQueue with | none => false | some q => q.isEmpty end Goal /-! ## Functions on Rule Applications -/ namespace Rapp /-! ### Constructors -/ @[inline] protected def mk (parent : Option GoalRef) (subgoals : Array GoalRef) (data : RappData) : Rapp := MutAltTree.mk data parent subgoals /-! ### Getters -/ @[inline] def subgoals (r : Rapp) : Array GoalRef := r.children @[inline] def id (r : Rapp) : RappId := r.payload.id @[inline] def appliedRule (r : Rapp) : RegularRule := r.payload.appliedRule @[inline] def successProbability (r : Rapp) : Percent := r.payload.successProbability @[inline] def proof (r : Rapp) : Expr := r.payload.proof @[inline] def proven? (r : Rapp) : Bool := r.payload.proven? @[inline] def unprovable? (r : Rapp) : Bool := r.payload.unprovable? @[inline] def irrelevant? (r : Rapp) : Bool := r.payload.irrelevant? -- Setters @[inline] def setId (id : RappId) (r : Rapp) : Rapp := r.modifyPayload Ξ» r => { r with id := id } @[inline] def setAppliedRule (appliedRule : RegularRule) (r : Rapp) : Rapp := r.modifyPayload Ξ» r => { r with appliedRule := appliedRule } @[inline] def setSuccessProbability (successProbability : Percent) (r : Rapp) : Rapp := r.modifyPayload Ξ» r => { r with successProbability := successProbability } @[inline] def setProof (proof : Expr) (r : Rapp) : Rapp := r.modifyPayload Ξ» r => { r with proof := proof } @[inline] def setProven? (proven? : Bool) (r : Rapp) : Rapp := r.modifyPayload Ξ» r => { r with proven? := proven? } @[inline] def setUnprovable? (unprovable? : Bool) (r : Rapp) : Rapp := r.modifyPayload Ξ» r => { r with unprovable? := unprovable? } @[inline] def setIrrelevant? (irrelevant? : Bool) (r : Rapp) : Rapp := r.modifyPayload Ξ» r => { r with irrelevant? := irrelevant? } /-! ### Miscellaneous -/ def subgoalsProven? (r : Rapp) : m Bool := r.subgoals.allM Ξ» subgoal => return (← subgoal.get).proven? end Rapp /-! ## Formatting -/ mutual private partial def formatTreeGoal (goalMInfo : GoalData.MessageInfo) (rappMInfo : RappData.MessageInfo) (goal : Goal) : m MessageData := do let goalMsg := goal.payload.toMessageData' goalMInfo let childrenMsgs ← goal.rapps.mapM Ξ» c => do formatTreeRapp goalMInfo rappMInfo (← c.get) return goalMsg ++ indentD (MessageData.node childrenMsgs) private partial def formatTreeRapp (goalMInfo : GoalData.MessageInfo) (rappMInfo : RappData.MessageInfo) (rapp : Rapp) : m MessageData := do let rappMsg := rapp.payload.toMessageData' rappMInfo let childrenMsgs ← rapp.subgoals.mapM Ξ» c => do formatTreeGoal goalMInfo rappMInfo (← c.get) return rappMsg ++ indentD (MessageData.node childrenMsgs) end namespace Goal def toMessageData {m} [Monad m] [MonadOptions m] (traceCtx : TraceContext) (g : Goal) : m MessageData := do g.payload.toMessageData traceCtx @[inline] def treeToMessageData' : GoalData.MessageInfo β†’ RappData.MessageInfo β†’ Goal β†’ m MessageData := formatTreeGoal def treeToMessageData [MonadOptions m] (traceCtx : TraceContext) (g : Goal) : m MessageData := do treeToMessageData' (← GoalData.getMessageInfo traceCtx) (← RappData.getMessageInfo traceCtx) g end Goal namespace Rapp def toMessageData {m} [Monad m] [MonadOptions m] (traceCtx : TraceContext) (r : Rapp) : m MessageData := do r.payload.toMessageData traceCtx @[inline] def treeToMessageData' : GoalData.MessageInfo β†’ RappData.MessageInfo β†’ Rapp β†’ m MessageData := formatTreeRapp def treeToMessageData [MonadOptions m] (traceCtx : TraceContext) (r : Rapp) : m MessageData := do treeToMessageData' (← GoalData.getMessageInfo traceCtx) (← RappData.getMessageInfo traceCtx) r end Rapp /-! ## Miscellaneous Functions on Goals -/ namespace Goal def mayHaveUnexpandedRapp (g : Goal) : m Bool := do pure $ Β¬ g.hasNoUnexpandedUnsafeRule ∧ Β¬ (← g.rapps.anyM Ξ» r => return (← r.get : Rapp).appliedRule.isSafe) def hasProvableRapp (g : Goal) : m Bool := g.rapps.anyM Ξ» r => return Β¬ (← r.get).unprovable? end Goal /-! ## Proof Extraction -/ namespace GoalRef /- May only be called *once*. The given goal must be proven. -/ partial def linkProofs (gref : GoalRef) : MetaM Unit := do let g ← gref.get let (some r) ← g.rapps.findSomeM? Ξ» r => do let r ← r.get return if r.proven? then some r else none | throwError "aesop/linkProofs: internal error: node {g.id} not proven" r.subgoals.forM linkProofs let goalMVar := g.goal checkNotAssigned `aesop goalMVar -- TODO check for type-correct assignment? -- let goalType ← getMVarType goalMVar -- let (true) ← isDefEq goalType r.proof | throwError -- "aesop/linkProofs: internal error: proof of rule application {r.id} did not unify with the goal of its parent node {g.id}" assignExprMVar g.goal r.proof /- Only call this after `linkProofs` has been run. -/ def extractProof (gref : GoalRef) : MetaM Expr := do let g ← gref.get match g.normalizationProof with | none => instantiateMVars $ mkMVar g.goal | some prf => instantiateMVars prf end GoalRef /-! ## Propagating Provability/Unprovability/Irrelevance -/ @[inline] def Internal.setIrrelevant : Sum GoalRef RappRef β†’ m Unit := MutAltTree.visitDown' (Ξ» gref => do let g : Goal ← gref.get if g.irrelevant? then return false -- Subtree should already be marked as irrelevant. else do gref.set $ g.setIrrelevant? true return true) (Ξ» rref => do let r : Rapp ← rref.get if r.irrelevant? then return false else do rref.set $ r.setIrrelevant? true return true) def GoalRef.setIrrelevant : GoalRef β†’ m Unit := Internal.setIrrelevant ∘ Sum.inl def RappRef.setIrrelevant : RappRef β†’ m Unit := Internal.setIrrelevant ∘ Sum.inr @[inline] def Internal.setProven : Sum GoalRef RappRef β†’ m Unit := MutAltTree.visitUp' -- Goals are unconditionally marked as proven. (Ξ» gref => do gref.modify Ξ» (g : Goal) => g.setProven? true return true) -- Rapps are marked as proven only if they are in fact proven, i.e. if all -- their subgoals are (marked as) proven. In this case, we also need to -- mark siblings of the rapp (and their descendants) as irrelevant. (Ξ» rref => do let r : Rapp ← rref.get if Β¬ (← r.subgoalsProven?) then return false else do rref.set $ r.setProven? true let siblings ← MutAltTree.siblings rref siblings.forM RappRef.setIrrelevant return true) def GoalRef.setProven : GoalRef β†’ m Unit := Internal.setProven ∘ Sum.inl def RappRef.setProven : RappRef β†’ m Unit := Internal.setProven ∘ Sum.inr @[inline] def Internal.setUnprovable : Sum GoalRef RappRef β†’ m Unit := MutAltTree.visitUp' -- Goals are marked as unprovable only if they are in fact unprovable, i.e. -- if all their rule applications are unprovable and they do not have -- unexpanded rule applications. In this case, we also need to mark -- siblings of the goal (and their descendants) as irrelevant. (Ξ» gref => do let g : Goal ← gref.get if (← g.mayHaveUnexpandedRapp <||> g.hasProvableRapp) then return false else do gref.set $ g.setUnprovable? true let siblings ← MutAltTree.siblings gref siblings.forM GoalRef.setIrrelevant return true) -- Rapps are unconditionally marked as unprovable. (Ξ» rref => do rref.modify Ξ» (r : Rapp) => r.setUnprovable? true return true) def GoalRef.setUnprovable : GoalRef β†’ m Unit := Internal.setUnprovable ∘ Sum.inl def RappRef.setUnprovable : RappRef β†’ m Unit := Internal.setUnprovable ∘ Sum.inr end Lean.Aesop
2692df9103eb831f4cb0d4c679003428c7492cbf
4727251e0cd73359b15b664c3170e5d754078599
/src/number_theory/zsqrtd/basic.lean
23fc0249b1bfd54d73e81119bcab6e9c7f419e16
[ "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
33,185
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.associated import ring_theory.int.basic import tactic.ring /-! # β„€[√d] The ring of integers adjoined with a square root of `d : β„€`. After defining the norm, we show that it is a linearly ordered commutative ring, as well as an integral domain. We provide the universal property, that ring homomorphisms `β„€βˆšd β†’+* R` correspond to choices of square roots of `d` in `R`. -/ /-- The ring of integers adjoined with a square root of `d`. These have the form `a + b √d` where `a b : β„€`. The components are called `re` and `im` by analogy to the negative `d` case. -/ structure zsqrtd (d : β„€) := (re : β„€) (im : β„€) prefix `β„€βˆš`:100 := zsqrtd namespace zsqrtd section parameters {d : β„€} instance : decidable_eq β„€βˆšd := by tactic.mk_dec_eq_instance theorem ext : βˆ€ {z w : β„€βˆšd}, z = w ↔ z.re = w.re ∧ z.im = w.im | ⟨x, y⟩ ⟨x', y'⟩ := ⟨λ h, by injection h; split; assumption, Ξ» ⟨h₁, hβ‚‚βŸ©, by congr; assumption⟩ /-- Convert an integer to a `β„€βˆšd` -/ def of_int (n : β„€) : β„€βˆšd := ⟨n, 0⟩ theorem of_int_re (n : β„€) : (of_int n).re = n := rfl theorem of_int_im (n : β„€) : (of_int n).im = 0 := rfl /-- The zero of the ring -/ instance : has_zero β„€βˆšd := ⟨of_int 0⟩ @[simp] theorem zero_re : (0 : β„€βˆšd).re = 0 := rfl @[simp] theorem zero_im : (0 : β„€βˆšd).im = 0 := rfl instance : inhabited β„€βˆšd := ⟨0⟩ /-- The one of the ring -/ instance : has_one β„€βˆšd := ⟨of_int 1⟩ @[simp] theorem one_re : (1 : β„€βˆšd).re = 1 := rfl @[simp] theorem one_im : (1 : β„€βˆšd).im = 0 := rfl /-- The representative of `√d` in the ring -/ def sqrtd : β„€βˆšd := ⟨0, 1⟩ @[simp] theorem sqrtd_re : (sqrtd : β„€βˆšd).re = 0 := rfl @[simp] theorem sqrtd_im : (sqrtd : β„€βˆšd).im = 1 := rfl /-- Addition of elements of `β„€βˆšd` -/ instance : has_add β„€βˆšd := ⟨λ z w, ⟨z.1 + w.1, z.2 + w.2⟩⟩ @[simp] lemma add_def (x y x' y' : β„€) : (⟨x, y⟩ + ⟨x', y'⟩ : β„€βˆšd) = ⟨x + x', y + y'⟩ := rfl @[simp] lemma add_re (z w : β„€βˆšd) : (z + w).re = z.re + w.re := rfl @[simp] lemma add_im (z w : β„€βˆšd) : (z + w).im = z.im + w.im := rfl @[simp] lemma bit0_re (z) : (bit0 z : β„€βˆšd).re = bit0 z.re := rfl @[simp] lemma bit0_im (z) : (bit0 z : β„€βˆšd).im = bit0 z.im := rfl @[simp] theorem bit1_re (z) : (bit1 z : β„€βˆšd).re = bit1 z.re := rfl @[simp] theorem bit1_im (z) : (bit1 z : β„€βˆšd).im = bit0 z.im := by simp [bit1] /-- Negation in `β„€βˆšd` -/ instance : has_neg β„€βˆšd := ⟨λ z, ⟨-z.1, -z.2⟩⟩ @[simp] lemma neg_re (z : β„€βˆšd) : (-z).re = -z.re := rfl @[simp] lemma neg_im (z : β„€βˆšd) : (-z).im = -z.im := rfl /-- Multiplication in `β„€βˆšd` -/ instance : has_mul β„€βˆšd := ⟨λ z w, ⟨z.1 * w.1 + d * z.2 * w.2, z.1 * w.2 + z.2 * w.1⟩⟩ @[simp] lemma mul_re (z w : β„€βˆšd) : (z * w).re = z.re * w.re + d * z.im * w.im := rfl @[simp] lemma mul_im (z w : β„€βˆšd) : (z * w).im = z.re * w.im + z.im * w.re := rfl instance : comm_ring β„€βˆšd := by refine_struct { add := (+), zero := (0 : β„€βˆšd), neg := has_neg.neg, mul := (*), sub := Ξ» a b, a + -b, one := 1, npow := @npow_rec (β„€βˆšd) ⟨1⟩ ⟨(*)⟩, nsmul := @nsmul_rec (β„€βˆšd) ⟨0⟩ ⟨(+)⟩, zsmul := @zsmul_rec (β„€βˆšd) ⟨0⟩ ⟨(+)⟩ ⟨has_neg.neg⟩ }; intros; try { refl }; simp [ext, add_mul, mul_add, add_comm, add_left_comm, mul_comm, mul_left_comm] instance : add_comm_monoid β„€βˆšd := by apply_instance instance : add_monoid β„€βˆšd := by apply_instance instance : monoid β„€βˆšd := by apply_instance instance : comm_monoid β„€βˆšd := by apply_instance instance : comm_semigroup β„€βˆšd := by apply_instance instance : semigroup β„€βˆšd := by apply_instance instance : add_comm_semigroup β„€βˆšd := by apply_instance instance : add_semigroup β„€βˆšd := by apply_instance instance : comm_semiring β„€βˆšd := by apply_instance instance : semiring β„€βˆšd := by apply_instance instance : ring β„€βˆšd := by apply_instance instance : distrib β„€βˆšd := by apply_instance /-- Conjugation in `β„€βˆšd`. The conjugate of `a + b √d` is `a - b √d`. -/ def conj (z : β„€βˆšd) : β„€βˆšd := ⟨z.1, -z.2⟩ @[simp] lemma conj_re (z : β„€βˆšd) : (conj z).re = z.re := rfl @[simp] lemma conj_im (z : β„€βˆšd) : (conj z).im = -z.im := rfl /-- `conj` as an `add_monoid_hom`. -/ def conj_hom : β„€βˆšd β†’+ β„€βˆšd := { to_fun := conj, map_add' := Ξ» ⟨a, ai⟩ ⟨b, bi⟩, ext.mpr ⟨rfl, neg_add _ _⟩, map_zero' := ext.mpr ⟨rfl, neg_zero⟩ } @[simp] lemma conj_zero : conj (0 : β„€βˆšd) = 0 := conj_hom.map_zero @[simp] lemma conj_one : conj (1 : β„€βˆšd) = 1 := by simp only [zsqrtd.ext, zsqrtd.conj_re, zsqrtd.conj_im, zsqrtd.one_im, neg_zero, eq_self_iff_true, and_self] @[simp] lemma conj_neg (x : β„€βˆšd) : (-x).conj = -x.conj := rfl @[simp] lemma conj_add (x y : β„€βˆšd) : (x + y).conj = x.conj + y.conj := conj_hom.map_add x y @[simp] lemma conj_sub (x y : β„€βˆšd) : (x - y).conj = x.conj - y.conj := conj_hom.map_sub x y @[simp] lemma conj_conj {d : β„€} (x : β„€βˆšd) : x.conj.conj = x := by simp only [ext, true_and, conj_re, eq_self_iff_true, neg_neg, conj_im] instance : nontrivial β„€βˆšd := ⟨⟨0, 1, dec_trivial⟩⟩ @[simp] theorem coe_nat_re (n : β„•) : (n : β„€βˆšd).re = n := by induction n; simp * @[simp] theorem coe_nat_im (n : β„•) : (n : β„€βˆšd).im = 0 := by induction n; simp * theorem coe_nat_val (n : β„•) : (n : β„€βˆšd) = ⟨n, 0⟩ := by simp [ext] @[simp] theorem coe_int_re (n : β„€) : (n : β„€βˆšd).re = n := by cases n; simp [*, int.of_nat_eq_coe, int.neg_succ_of_nat_eq] @[simp] theorem coe_int_im (n : β„€) : (n : β„€βˆšd).im = 0 := by cases n; simp * theorem coe_int_val (n : β„€) : (n : β„€βˆšd) = ⟨n, 0⟩ := by simp [ext] instance : char_zero β„€βˆšd := { cast_injective := Ξ» m n, by simp [ext] } @[simp] theorem of_int_eq_coe (n : β„€) : (of_int n : β„€βˆšd) = n := by simp [ext, of_int_re, of_int_im] @[simp] theorem smul_val (n x y : β„€) : (n : β„€βˆšd) * ⟨x, y⟩ = ⟨n * x, n * y⟩ := by simp [ext] theorem smul_re (a : β„€) (b : β„€βˆšd) : (↑a * b).re = a * b.re := by simp theorem smul_im (a : β„€) (b : β„€βˆšd) : (↑a * b).im = a * b.im := by simp @[simp] theorem muld_val (x y : β„€) : sqrtd * ⟨x, y⟩ = ⟨d * y, x⟩ := by simp [ext] @[simp] theorem dmuld : sqrtd * sqrtd = d := by simp [ext] @[simp] theorem smuld_val (n x y : β„€) : sqrtd * (n : β„€βˆšd) * ⟨x, y⟩ = ⟨d * n * y, n * x⟩ := by simp [ext] theorem decompose {x y : β„€} : (⟨x, y⟩ : β„€βˆšd) = x + sqrtd * y := by simp [ext] theorem mul_conj {x y : β„€} : (⟨x, y⟩ * conj ⟨x, y⟩ : β„€βˆšd) = x * x - d * y * y := by simp [ext, sub_eq_add_neg, mul_comm] theorem conj_mul {a b : β„€βˆšd} : conj (a * b) = conj a * conj b := by { simp [ext], ring } protected lemma coe_int_add (m n : β„€) : (↑(m + n) : β„€βˆšd) = ↑m + ↑n := (int.cast_ring_hom _).map_add _ _ protected lemma coe_int_sub (m n : β„€) : (↑(m - n) : β„€βˆšd) = ↑m - ↑n := (int.cast_ring_hom _).map_sub _ _ protected lemma coe_int_mul (m n : β„€) : (↑(m * n) : β„€βˆšd) = ↑m * ↑n := (int.cast_ring_hom _).map_mul _ _ protected lemma coe_int_inj {m n : β„€} (h : (↑m : β„€βˆšd) = ↑n) : m = n := by simpa using congr_arg re h lemma coe_int_dvd_iff (z : β„€) (a : β„€βˆšd) : ↑z ∣ a ↔ z ∣ a.re ∧ z ∣ a.im := begin split, { rintro ⟨x, rfl⟩, simp only [add_zero, coe_int_re, zero_mul, mul_im, dvd_mul_right, and_self, mul_re, mul_zero, coe_int_im] }, { rintro ⟨⟨r, hr⟩, ⟨i, hi⟩⟩, use ⟨r, i⟩, rw [smul_val, ext], exact ⟨hr, hi⟩ }, end @[simp, norm_cast] lemma coe_int_dvd_coe_int (a b : β„€) : (a : β„€βˆšd) ∣ b ↔ a ∣ b := begin rw coe_int_dvd_iff, split, { rintro ⟨hre, -⟩, rwa [coe_int_re] at hre }, { rw [coe_int_re, coe_int_im], exact Ξ» hc, ⟨hc, dvd_zero a⟩ }, end protected lemma eq_of_smul_eq_smul_left {a : β„€} {b c : β„€βˆšd} (ha : a β‰  0) (h : ↑a * b = a * c) : b = c := begin rw ext at h ⊒, apply and.imp _ _ h; { simp only [smul_re, smul_im], exact int.eq_of_mul_eq_mul_left ha }, end section gcd lemma gcd_eq_zero_iff (a : β„€βˆšd) : int.gcd a.re a.im = 0 ↔ a = 0 := by simp only [int.gcd_eq_zero_iff, ext, eq_self_iff_true, zero_im, zero_re] lemma gcd_pos_iff (a : β„€βˆšd) : 0 < int.gcd a.re a.im ↔ a β‰  0 := pos_iff_ne_zero.trans $ not_congr a.gcd_eq_zero_iff lemma coprime_of_dvd_coprime {a b : β„€βˆšd} (hcoprime : is_coprime a.re a.im) (hdvd : b ∣ a) : is_coprime b.re b.im := begin apply is_coprime_of_dvd, { rintro ⟨hre, him⟩, obtain rfl : b = 0, { simp only [ext, hre, eq_self_iff_true, zero_im, him, and_self, zero_re] }, rw zero_dvd_iff at hdvd, simpa only [hdvd, zero_im, zero_re, not_coprime_zero_zero] using hcoprime }, { intros z hz hznezero hzdvdu hzdvdv, apply hz, obtain ⟨ha, hb⟩ : z ∣ a.re ∧ z ∣ a.im, { rw ←coe_int_dvd_iff, apply dvd_trans _ hdvd, rw coe_int_dvd_iff, exact ⟨hzdvdu, hzdvdv⟩ }, exact hcoprime.is_unit_of_dvd' ha hb }, end lemma exists_coprime_of_gcd_pos {a : β„€βˆšd} (hgcd : 0 < int.gcd a.re a.im) : βˆƒ b : β„€βˆšd, a = ((int.gcd a.re a.im : β„€) : β„€βˆšd) * b ∧ is_coprime b.re b.im := begin obtain ⟨re, im, H1, Hre, Him⟩ := int.exists_gcd_one hgcd, rw [mul_comm] at Hre Him, refine ⟨⟨re, im⟩, _, _⟩, { rw [smul_val, ext, ←Hre, ←Him], split; refl }, { rw [←int.gcd_eq_one_iff_coprime, H1] } end end gcd /-- Read `sq_le a c b d` as `a √c ≀ b √d` -/ def sq_le (a c b d : β„•) : Prop := c*a*a ≀ d*b*b theorem sq_le_of_le {c d x y z w : β„•} (xz : z ≀ x) (yw : y ≀ w) (xy : sq_le x c y d) : sq_le z c w d := le_trans (mul_le_mul (nat.mul_le_mul_left _ xz) xz (nat.zero_le _) (nat.zero_le _)) $ le_trans xy (mul_le_mul (nat.mul_le_mul_left _ yw) yw (nat.zero_le _) (nat.zero_le _)) theorem sq_le_add_mixed {c d x y z w : β„•} (xy : sq_le x c y d) (zw : sq_le z c w d) : c * (x * z) ≀ d * (y * w) := nat.mul_self_le_mul_self_iff.2 $ by simpa [mul_comm, mul_left_comm] using mul_le_mul xy zw (nat.zero_le _) (nat.zero_le _) theorem sq_le_add {c d x y z w : β„•} (xy : sq_le x c y d) (zw : sq_le z c w d) : sq_le (x + z) c (y + w) d := begin have xz := sq_le_add_mixed xy zw, simp [sq_le, mul_assoc] at xy zw, simp [sq_le, mul_add, mul_comm, mul_left_comm, add_le_add, *] end theorem sq_le_cancel {c d x y z w : β„•} (zw : sq_le y d x c) (h : sq_le (x + z) c (y + w) d) : sq_le z c w d := begin apply le_of_not_gt, intro l, refine not_le_of_gt _ h, simp [sq_le, mul_add, mul_comm, mul_left_comm, add_assoc], have hm := sq_le_add_mixed zw (le_of_lt l), simp [sq_le, mul_assoc] at l zw, exact lt_of_le_of_lt (add_le_add_right zw _) (add_lt_add_left (add_lt_add_of_le_of_lt hm (add_lt_add_of_le_of_lt hm l)) _) end theorem sq_le_smul {c d x y : β„•} (n : β„•) (xy : sq_le x c y d) : sq_le (n * x) c (n * y) d := by simpa [sq_le, mul_left_comm, mul_assoc] using nat.mul_le_mul_left (n * n) xy theorem sq_le_mul {d x y z w : β„•} : (sq_le x 1 y d β†’ sq_le z 1 w d β†’ sq_le (x * w + y * z) d (x * z + d * y * w) 1) ∧ (sq_le x 1 y d β†’ sq_le w d z 1 β†’ sq_le (x * z + d * y * w) 1 (x * w + y * z) d) ∧ (sq_le y d x 1 β†’ sq_le z 1 w d β†’ sq_le (x * z + d * y * w) 1 (x * w + y * z) d) ∧ (sq_le y d x 1 β†’ sq_le w d z 1 β†’ sq_le (x * w + y * z) d (x * z + d * y * w) 1) := by refine ⟨_, _, _, _⟩; { intros xy zw, have := int.mul_nonneg (sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le xy)) (sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le zw)), refine int.le_of_coe_nat_le_coe_nat (le_of_sub_nonneg _), convert this, simp only [one_mul, int.coe_nat_add, int.coe_nat_mul], ring } /-- "Generalized" `nonneg`. `nonnegg c d x y` means `a √c + b √d β‰₯ 0`; we are interested in the case `c = 1` but this is more symmetric -/ def nonnegg (c d : β„•) : β„€ β†’ β„€ β†’ Prop | (a : β„•) (b : β„•) := true | (a : β„•) -[1+ b] := sq_le (b+1) c a d | -[1+ a] (b : β„•) := sq_le (a+1) d b c | -[1+ a] -[1+ b] := false theorem nonnegg_comm {c d : β„•} {x y : β„€} : nonnegg c d x y = nonnegg d c y x := by induction x; induction y; refl theorem nonnegg_neg_pos {c d} : Ξ  {a b : β„•}, nonnegg c d (-a) b ↔ sq_le a d b c | 0 b := ⟨by simp [sq_le, nat.zero_le], Ξ»a, trivial⟩ | (a+1) b := by rw ← int.neg_succ_of_nat_coe; refl theorem nonnegg_pos_neg {c d} {a b : β„•} : nonnegg c d a (-b) ↔ sq_le b c a d := by rw nonnegg_comm; exact nonnegg_neg_pos theorem nonnegg_cases_right {c d} {a : β„•} : Ξ  {b : β„€}, (Ξ  x : β„•, b = -x β†’ sq_le x c a d) β†’ nonnegg c d a b | (b:nat) h := trivial | -[1+ b] h := h (b+1) rfl theorem nonnegg_cases_left {c d} {b : β„•} {a : β„€} (h : Ξ  x : β„•, a = -x β†’ sq_le x d b c) : nonnegg c d a b := cast nonnegg_comm (nonnegg_cases_right h) section norm /-- The norm of an element of `β„€[√d]`. -/ def norm (n : β„€βˆšd) : β„€ := n.re * n.re - d * n.im * n.im lemma norm_def (n : β„€βˆšd) : n.norm = n.re * n.re - d * n.im * n.im := rfl @[simp] lemma norm_zero : norm 0 = 0 := by simp [norm] @[simp] lemma norm_one : norm 1 = 1 := by simp [norm] @[simp] lemma norm_int_cast (n : β„€) : norm n = n * n := by simp [norm] @[simp] lemma norm_nat_cast (n : β„•) : norm n = n * n := norm_int_cast n @[simp] lemma norm_mul (n m : β„€βˆšd) : norm (n * m) = norm n * norm m := by { simp only [norm, mul_im, mul_re], ring } /-- `norm` as a `monoid_hom`. -/ def norm_monoid_hom : β„€βˆšd β†’* β„€ := { to_fun := norm, map_mul' := norm_mul, map_one' := norm_one } lemma norm_eq_mul_conj (n : β„€βˆšd) : (norm n : β„€βˆšd) = n * n.conj := by cases n; simp [norm, conj, zsqrtd.ext, mul_comm, sub_eq_add_neg] @[simp] lemma norm_neg (x : β„€βˆšd) : (-x).norm = x.norm := coe_int_inj $ by simp only [norm_eq_mul_conj, conj_neg, neg_mul, mul_neg, neg_neg] @[simp] lemma norm_conj (x : β„€βˆšd) : x.conj.norm = x.norm := coe_int_inj $ by simp only [norm_eq_mul_conj, conj_conj, mul_comm] lemma norm_nonneg (hd : d ≀ 0) (n : β„€βˆšd) : 0 ≀ n.norm := add_nonneg (mul_self_nonneg _) (by rw [mul_assoc, neg_mul_eq_neg_mul]; exact (mul_nonneg (neg_nonneg.2 hd) (mul_self_nonneg _))) lemma norm_eq_one_iff {x : β„€βˆšd} : x.norm.nat_abs = 1 ↔ is_unit x := ⟨λ h, is_unit_iff_dvd_one.2 $ (le_total 0 (norm x)).cases_on (Ξ» hx, show x ∣ 1, from ⟨x.conj, by rwa [← int.coe_nat_inj', int.nat_abs_of_nonneg hx, ← @int.cast_inj (β„€βˆšd) _ _, norm_eq_mul_conj, eq_comm] at h⟩) (Ξ» hx, show x ∣ 1, from ⟨- x.conj, by rwa [← int.coe_nat_inj', int.of_nat_nat_abs_of_nonpos hx, ← @int.cast_inj (β„€βˆšd) _ _, int.cast_neg, norm_eq_mul_conj, neg_mul_eq_mul_neg, eq_comm] at h⟩), Ξ» h, let ⟨y, hy⟩ := is_unit_iff_dvd_one.1 h in begin have := congr_arg (int.nat_abs ∘ norm) hy, rw [function.comp_app, function.comp_app, norm_mul, int.nat_abs_mul, norm_one, int.nat_abs_one, eq_comm, nat.mul_eq_one_iff] at this, exact this.1 end⟩ lemma is_unit_iff_norm_is_unit {d : β„€} (z : β„€βˆšd) : is_unit z ↔ is_unit z.norm := by rw [int.is_unit_iff_nat_abs_eq, norm_eq_one_iff] lemma norm_eq_one_iff' {d : β„€} (hd : d ≀ 0) (z : β„€βˆšd) : z.norm = 1 ↔ is_unit z := by rw [←norm_eq_one_iff, ←int.coe_nat_inj', int.nat_abs_of_nonneg (norm_nonneg hd z), int.coe_nat_one] lemma norm_eq_zero_iff {d : β„€} (hd : d < 0) (z : β„€βˆšd) : z.norm = 0 ↔ z = 0 := begin split, { intro h, rw [ext, zero_re, zero_im], rw [norm_def, sub_eq_add_neg, mul_assoc] at h, have left := mul_self_nonneg z.re, have right := neg_nonneg.mpr (mul_nonpos_of_nonpos_of_nonneg hd.le (mul_self_nonneg z.im)), obtain ⟨ha, hb⟩ := (add_eq_zero_iff' left right).mp h, split; apply eq_zero_of_mul_self_eq_zero, { exact ha }, { rw [neg_eq_zero, mul_eq_zero] at hb, exact hb.resolve_left hd.ne } }, { rintro rfl, exact norm_zero } end lemma norm_eq_of_associated {d : β„€} (hd : d ≀ 0) {x y : β„€βˆšd} (h : associated x y) : x.norm = y.norm := begin obtain ⟨u, rfl⟩ := h, rw [norm_mul, (norm_eq_one_iff' hd _).mpr u.is_unit, mul_one], end end norm end section parameter {d : β„•} /-- Nonnegativity of an element of `β„€βˆšd`. -/ def nonneg : β„€βˆšd β†’ Prop | ⟨a, b⟩ := nonnegg d 1 a b instance : has_le β„€βˆšd := ⟨λ a b, nonneg (b - a)⟩ instance : has_lt β„€βˆšd := ⟨λ a b, Β¬ b ≀ a⟩ instance decidable_nonnegg (c d a b) : decidable (nonnegg c d a b) := by cases a; cases b; repeat {rw int.of_nat_eq_coe}; unfold nonnegg sq_le; apply_instance instance decidable_nonneg : Ξ  (a : β„€βˆšd), decidable (nonneg a) | ⟨a, b⟩ := zsqrtd.decidable_nonnegg _ _ _ _ instance decidable_le : @decidable_rel (β„€βˆšd) (≀) := Ξ» _ _, decidable_nonneg _ theorem nonneg_cases : Ξ  {a : β„€βˆšd}, nonneg a β†’ βˆƒ x y : β„•, a = ⟨x, y⟩ ∨ a = ⟨x, -y⟩ ∨ a = ⟨-x, y⟩ | ⟨(x : β„•), (y : β„•)⟩ h := ⟨x, y, or.inl rfl⟩ | ⟨(x : β„•), -[1+ y]⟩ h := ⟨x, y+1, or.inr $ or.inl rfl⟩ | ⟨-[1+ x], (y : β„•)⟩ h := ⟨x+1, y, or.inr $ or.inr rfl⟩ | ⟨-[1+ x], -[1+ y]⟩ h := false.elim h lemma nonneg_add_lem {x y z w : β„•} (xy : nonneg ⟨x, -y⟩) (zw : nonneg ⟨-z, w⟩) : nonneg (⟨x, -y⟩ + ⟨-z, w⟩) := have nonneg ⟨int.sub_nat_nat x z, int.sub_nat_nat w y⟩, from int.sub_nat_nat_elim x z (Ξ»m n i, sq_le y d m 1 β†’ sq_le n 1 w d β†’ nonneg ⟨i, int.sub_nat_nat w y⟩) (Ξ»j k, int.sub_nat_nat_elim w y (Ξ»m n i, sq_le n d (k + j) 1 β†’ sq_le k 1 m d β†’ nonneg ⟨int.of_nat j, i⟩) (Ξ»m n xy zw, trivial) (Ξ»m n xy zw, sq_le_cancel zw xy)) (Ξ»j k, int.sub_nat_nat_elim w y (Ξ»m n i, sq_le n d k 1 β†’ sq_le (k + j + 1) 1 m d β†’ nonneg ⟨-[1+ j], i⟩) (Ξ»m n xy zw, sq_le_cancel xy zw) (Ξ»m n xy zw, let t := nat.le_trans zw (sq_le_of_le (nat.le_add_right n (m+1)) le_rfl xy) in have k + j + 1 ≀ k, from nat.mul_self_le_mul_self_iff.2 (by repeat{rw one_mul at t}; exact t), absurd this (not_le_of_gt $ nat.succ_le_succ $ nat.le_add_right _ _))) (nonnegg_pos_neg.1 xy) (nonnegg_neg_pos.1 zw), show nonneg ⟨_, _⟩, by rw [neg_add_eq_sub]; rwa [int.sub_nat_nat_eq_coe,int.sub_nat_nat_eq_coe] at this lemma nonneg.add {a b : β„€βˆšd} (ha : nonneg a) (hb : nonneg b) : nonneg (a + b) := begin rcases nonneg_cases ha with ⟨x, y, rfl|rfl|rfl⟩; rcases nonneg_cases hb with ⟨z, w, rfl|rfl|rfl⟩, { trivial }, { refine nonnegg_cases_right (Ξ»i h, sq_le_of_le _ _ (nonnegg_pos_neg.1 hb)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ y (by simp [add_comm, *]))) }, { apply nat.le_add_left } }, { refine nonnegg_cases_left (Ξ»i h, sq_le_of_le _ _ (nonnegg_neg_pos.1 hb)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ x (by simp [add_comm, *]))) }, { apply nat.le_add_left } }, { refine nonnegg_cases_right (Ξ»i h, sq_le_of_le _ _ (nonnegg_pos_neg.1 ha)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ w (by simp *))) }, { apply nat.le_add_right } }, { simpa [add_comm] using nonnegg_pos_neg.2 (sq_le_add (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb)) }, { exact nonneg_add_lem ha hb }, { refine nonnegg_cases_left (Ξ»i h, sq_le_of_le _ _ (nonnegg_neg_pos.1 ha)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (int.le.intro h)) }, { apply nat.le_add_right } }, { dsimp, rw [add_comm, add_comm ↑y], exact nonneg_add_lem hb ha }, { simpa [add_comm] using nonnegg_neg_pos.2 (sq_le_add (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb)) }, end theorem nonneg_iff_zero_le {a : β„€βˆšd} : nonneg a ↔ 0 ≀ a := show _ ↔ nonneg _, by simp theorem le_of_le_le {x y z w : β„€} (xz : x ≀ z) (yw : y ≀ w) : (⟨x, y⟩ : β„€βˆšd) ≀ ⟨z, w⟩ := show nonneg ⟨z - x, w - y⟩, from match z - x, w - y, int.le.dest_sub xz, int.le.dest_sub yw with ._, ._, ⟨a, rfl⟩, ⟨b, rfl⟩ := trivial end protected theorem nonneg_total : Ξ  (a : β„€βˆšd), nonneg a ∨ nonneg (-a) | ⟨(x : β„•), (y : β„•)⟩ := or.inl trivial | ⟨-[1+ x], -[1+ y]⟩ := or.inr trivial | ⟨0, -[1+ y]⟩ := or.inr trivial | ⟨-[1+ x], 0⟩ := or.inr trivial | ⟨(x+1:β„•), -[1+ y]⟩ := nat.le_total | ⟨-[1+ x], (y+1:β„•)⟩ := nat.le_total protected theorem le_total (a b : β„€βˆšd) : a ≀ b ∨ b ≀ a := begin have t := (b - a).nonneg_total, rwa neg_sub at t, end instance : preorder β„€βˆšd := { le := (≀), le_refl := Ξ» a, show nonneg (a - a), by simp only [sub_self], le_trans := Ξ» a b c hab hbc, by simpa [sub_add_sub_cancel'] using hab.add hbc, lt := (<), lt_iff_le_not_le := Ξ» a b, (and_iff_right_of_imp (zsqrtd.le_total _ _).resolve_left).symm } theorem le_arch (a : β„€βˆšd) : βˆƒn : β„•, a ≀ n := let ⟨x, y, (h : a ≀ ⟨x, y⟩)⟩ := show βˆƒx y : β„•, nonneg (⟨x, y⟩ + -a), from match -a with | ⟨int.of_nat x, int.of_nat y⟩ := ⟨0, 0, trivial⟩ | ⟨int.of_nat x, -[1+ y]⟩ := ⟨0, y+1, by simp [int.neg_succ_of_nat_coe, add_assoc]⟩ | ⟨-[1+ x], int.of_nat y⟩ := ⟨x+1, 0, by simp [int.neg_succ_of_nat_coe, add_assoc]⟩ | ⟨-[1+ x], -[1+ y]⟩ := ⟨x+1, y+1, by simp [int.neg_succ_of_nat_coe, add_assoc]⟩ end in begin refine ⟨x + d*y, h.trans _⟩, rw [← int.cast_coe_nat, ← of_int_eq_coe], change nonneg ⟨(↑x + d*y) - ↑x, 0-↑y⟩, cases y with y, { simp }, have h : βˆ€y, sq_le y d (d * y) 1 := Ξ» y, by simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_right (y * y) (nat.le_mul_self d), rw [show (x:β„€) + d * nat.succ y - x = d * nat.succ y, by simp], exact h (y+1) end protected theorem add_le_add_left (a b : β„€βˆšd) (ab : a ≀ b) (c : β„€βˆšd) : c + a ≀ c + b := show nonneg _, by rw add_sub_add_left_eq_sub; exact ab protected theorem le_of_add_le_add_left (a b c : β„€βˆšd) (h : c + a ≀ c + b) : a ≀ b := by simpa using zsqrtd.add_le_add_left _ _ h (-c) protected theorem add_lt_add_left (a b : β„€βˆšd) (h : a < b) (c) : c + a < c + b := Ξ» h', h (zsqrtd.le_of_add_le_add_left _ _ _ h') theorem nonneg_smul {a : β„€βˆšd} {n : β„•} (ha : nonneg a) : nonneg (n * a) := by rw ← int.cast_coe_nat; exact match a, nonneg_cases ha, ha with | ._, ⟨x, y, or.inl rfl⟩, ha := by rw smul_val; trivial | ._, ⟨x, y, or.inr $ or.inl rfl⟩, ha := by rw smul_val; simpa using nonnegg_pos_neg.2 (sq_le_smul n $ nonnegg_pos_neg.1 ha) | ._, ⟨x, y, or.inr $ or.inr rfl⟩, ha := by rw smul_val; simpa using nonnegg_neg_pos.2 (sq_le_smul n $ nonnegg_neg_pos.1 ha) end theorem nonneg_muld {a : β„€βˆšd} (ha : nonneg a) : nonneg (sqrtd * a) := by refine match a, nonneg_cases ha, ha with | ._, ⟨x, y, or.inl rfl⟩, ha := trivial | ._, ⟨x, y, or.inr $ or.inl rfl⟩, ha := by simp; apply nonnegg_neg_pos.2; simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_left d (nonnegg_pos_neg.1 ha) | ._, ⟨x, y, or.inr $ or.inr rfl⟩, ha := by simp; apply nonnegg_pos_neg.2; simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_left d (nonnegg_neg_pos.1 ha) end theorem nonneg_mul_lem {x y : β„•} {a : β„€βˆšd} (ha : nonneg a) : nonneg (⟨x, y⟩ * a) := have (⟨x, y⟩ * a : β„€βˆšd) = x * a + sqrtd * (y * a), by rw [decompose, right_distrib, mul_assoc]; refl, by rw this; exact (nonneg_smul ha).add (nonneg_muld $ nonneg_smul ha) theorem nonneg_mul {a b : β„€βˆšd} (ha : nonneg a) (hb : nonneg b) : nonneg (a * b) := match a, b, nonneg_cases ha, nonneg_cases hb, ha, hb with | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := trivial | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := nonneg_mul_lem hb | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := nonneg_mul_lem hb | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := by rw mul_comm; exact nonneg_mul_lem ha | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := by rw mul_comm; exact nonneg_mul_lem ha | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := by rw [calc (⟨-x, y⟩ * ⟨-z, w⟩ : β„€βˆšd) = ⟨_, _⟩ : rfl ... = ⟨x * z + d * y * w, -(x * w + y * z)⟩ : by simp [add_comm]]; exact nonnegg_pos_neg.2 (sq_le_mul.left (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := by rw [calc (⟨-x, y⟩ * ⟨z, -w⟩ : β„€βˆšd) = ⟨_, _⟩ : rfl ... = ⟨-(x * z + d * y * w), x * w + y * z⟩ : by simp [add_comm]]; exact nonnegg_neg_pos.2 (sq_le_mul.right.left (nonnegg_neg_pos.1 ha) (nonnegg_pos_neg.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := by rw [calc (⟨x, -y⟩ * ⟨-z, w⟩ : β„€βˆšd) = ⟨_, _⟩ : rfl ... = ⟨-(x * z + d * y * w), x * w + y * z⟩ : by simp [add_comm]]; exact nonnegg_neg_pos.2 (sq_le_mul.right.right.left (nonnegg_pos_neg.1 ha) (nonnegg_neg_pos.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := by rw [calc (⟨x, -y⟩ * ⟨z, -w⟩ : β„€βˆšd) = ⟨_, _⟩ : rfl ... = ⟨x * z + d * y * w, -(x * w + y * z)⟩ : by simp [add_comm]]; exact nonnegg_pos_neg.2 (sq_le_mul.right.right.right (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb)) end protected theorem mul_nonneg (a b : β„€βˆšd) : 0 ≀ a β†’ 0 ≀ b β†’ 0 ≀ a * b := by repeat {rw ← nonneg_iff_zero_le}; exact nonneg_mul theorem not_sq_le_succ (c d y) (h : 0 < c) : Β¬sq_le (y + 1) c 0 d := not_le_of_gt $ mul_pos (mul_pos h $ nat.succ_pos _) $ nat.succ_pos _ /-- A nonsquare is a natural number that is not equal to the square of an integer. This is implemented as a typeclass because it's a necessary condition for much of the Pell equation theory. -/ class nonsquare (x : β„•) : Prop := (ns [] : βˆ€n : β„•, x β‰  n*n) parameter [dnsq : nonsquare d] include dnsq theorem d_pos : 0 < d := lt_of_le_of_ne (nat.zero_le _) $ ne.symm $ (nonsquare.ns d 0) theorem divides_sq_eq_zero {x y} (h : x * x = d * y * y) : x = 0 ∧ y = 0 := let g := x.gcd y in or.elim g.eq_zero_or_pos (Ξ»H, ⟨nat.eq_zero_of_gcd_eq_zero_left H, nat.eq_zero_of_gcd_eq_zero_right H⟩) (Ξ»gpos, false.elim $ let ⟨m, n, co, (hx : x = m * g), (hy : y = n * g)⟩ := nat.exists_coprime gpos in begin rw [hx, hy] at h, have : m * m = d * (n * n) := nat.eq_of_mul_eq_mul_left (mul_pos gpos gpos) (by simpa [mul_comm, mul_left_comm] using h), have co2 := let co1 := co.mul_right co in co1.mul co1, exact nonsquare.ns d m (nat.dvd_antisymm (by rw this; apply dvd_mul_right) $ co2.dvd_of_dvd_mul_right $ by simp [this]) end) theorem divides_sq_eq_zero_z {x y : β„€} (h : x * x = d * y * y) : x = 0 ∧ y = 0 := by rw [mul_assoc, ← int.nat_abs_mul_self, ← int.nat_abs_mul_self, ← int.coe_nat_mul, ← mul_assoc] at h; exact let ⟨h1, h2⟩ := divides_sq_eq_zero (int.coe_nat_inj h) in ⟨int.eq_zero_of_nat_abs_eq_zero h1, int.eq_zero_of_nat_abs_eq_zero h2⟩ theorem not_divides_sq (x y) : (x + 1) * (x + 1) β‰  d * (y + 1) * (y + 1) := Ξ»e, by have t := (divides_sq_eq_zero e).left; contradiction theorem nonneg_antisymm : Ξ  {a : β„€βˆšd}, nonneg a β†’ nonneg (-a) β†’ a = 0 | ⟨0, 0⟩ xy yx := rfl | ⟨-[1+ x], -[1+ y]⟩ xy yx := false.elim xy | ⟨(x+1:nat), (y+1:nat)⟩ xy yx := false.elim yx | ⟨-[1+ x], 0⟩ xy yx := absurd xy (not_sq_le_succ _ _ _ dec_trivial) | ⟨(x+1:nat), 0⟩ xy yx := absurd yx (not_sq_le_succ _ _ _ dec_trivial) | ⟨0, -[1+ y]⟩ xy yx := absurd xy (not_sq_le_succ _ _ _ d_pos) | ⟨0, (y+1:nat)⟩ _ yx := absurd yx (not_sq_le_succ _ _ _ d_pos) | ⟨(x+1:nat), -[1+ y]⟩ (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) := let t := le_antisymm yx xy in by rw[one_mul] at t; exact absurd t (not_divides_sq _ _) | ⟨-[1+ x], (y+1:nat)⟩ (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) := let t := le_antisymm xy yx in by rw[one_mul] at t; exact absurd t (not_divides_sq _ _) theorem le_antisymm {a b : β„€βˆšd} (ab : a ≀ b) (ba : b ≀ a) : a = b := eq_of_sub_eq_zero $ nonneg_antisymm ba (by rw neg_sub; exact ab) instance : linear_order β„€βˆšd := { le_antisymm := @zsqrtd.le_antisymm, le_total := zsqrtd.le_total, decidable_le := zsqrtd.decidable_le, ..zsqrtd.preorder } protected theorem eq_zero_or_eq_zero_of_mul_eq_zero : Ξ  {a b : β„€βˆšd}, a * b = 0 β†’ a = 0 ∨ b = 0 | ⟨x, y⟩ ⟨z, w⟩ h := by injection h with h1 h2; exact have h1 : x*z = -(d*y*w), from eq_neg_of_add_eq_zero_left h1, have h2 : x*w = -(y*z), from eq_neg_of_add_eq_zero_left h2, have fin : x*x = d*y*y β†’ (⟨x, y⟩:β„€βˆšd) = 0, from Ξ»e, match x, y, divides_sq_eq_zero_z e with ._, ._, ⟨rfl, rfl⟩ := rfl end, if z0 : z = 0 then if w0 : w = 0 then or.inr (match z, w, z0, w0 with ._, ._, rfl, rfl := rfl end) else or.inl $ fin $ mul_right_cancelβ‚€ w0 $ calc x * x * w = -y * (x * z) : by simp [h2, mul_assoc, mul_left_comm] ... = d * y * y * w : by simp [h1, mul_assoc, mul_left_comm] else or.inl $ fin $ mul_right_cancelβ‚€ z0 $ calc x * x * z = d * -y * (x * w) : by simp [h1, mul_assoc, mul_left_comm] ... = d * y * y * z : by simp [h2, mul_assoc, mul_left_comm] instance : is_domain β„€βˆšd := { eq_zero_or_eq_zero_of_mul_eq_zero := @zsqrtd.eq_zero_or_eq_zero_of_mul_eq_zero, .. zsqrtd.comm_ring, .. zsqrtd.nontrivial } protected theorem mul_pos (a b : β„€βˆšd) (a0 : 0 < a) (b0 : 0 < b) : 0 < a * b := Ξ»ab, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero (le_antisymm ab (mul_nonneg _ _ (le_of_lt a0) (le_of_lt b0)))) (Ξ»e, ne_of_gt a0 e) (Ξ»e, ne_of_gt b0 e) instance : linear_ordered_comm_ring β„€βˆšd := { add_le_add_left := @zsqrtd.add_le_add_left, mul_pos := @zsqrtd.mul_pos, zero_le_one := dec_trivial, .. zsqrtd.comm_ring, .. zsqrtd.linear_order, .. zsqrtd.nontrivial } instance : linear_ordered_ring β„€βˆšd := by apply_instance instance : ordered_ring β„€βˆšd := by apply_instance end lemma norm_eq_zero {d : β„€} (h_nonsquare : βˆ€ n : β„€, d β‰  n*n) (a : β„€βˆšd) : norm a = 0 ↔ a = 0 := begin refine ⟨λ ha, ext.mpr _, Ξ» h, by rw [h, norm_zero]⟩, delta norm at ha, rw sub_eq_zero at ha, by_cases h : 0 ≀ d, { obtain ⟨d', rfl⟩ := int.eq_coe_of_zero_le h, haveI : nonsquare d' := ⟨λ n h, h_nonsquare n $ by exact_mod_cast h⟩, exact divides_sq_eq_zero_z ha, }, { push_neg at h, suffices : a.re * a.re = 0, { rw eq_zero_of_mul_self_eq_zero this at ha ⊒, simpa only [true_and, or_self_right, zero_re, zero_im, eq_self_iff_true, zero_eq_mul, mul_zero, mul_eq_zero, h.ne, false_or, or_self] using ha }, apply _root_.le_antisymm _ (mul_self_nonneg _), rw [ha, mul_assoc], exact mul_nonpos_of_nonpos_of_nonneg h.le (mul_self_nonneg _) } end variables {R : Type} @[ext] lemma hom_ext [ring R] {d : β„€} (f g : β„€βˆšd β†’+* R) (h : f sqrtd = g sqrtd) : f = g := begin ext ⟨x_re, x_im⟩, simp [decompose, h], end variables [comm_ring R] /-- The unique `ring_hom` from `β„€βˆšd` to a ring `R`, constructed by replacing `√d` with the provided root. Conversely, this associates to every mapping `β„€βˆšd β†’+* R` a value of `√d` in `R`. -/ @[simps] def lift {d : β„€} : {r : R // r * r = ↑d} ≃ (β„€βˆšd β†’+* R) := { to_fun := Ξ» r, { to_fun := Ξ» a, a.1 + a.2*(r : R), map_zero' := by simp, map_add' := Ξ» a b, by { simp, ring, }, map_one' := by simp, map_mul' := Ξ» a b, by { have : (a.re + a.im * r : R) * (b.re + b.im * r) = a.re * b.re + (a.re * b.im + a.im * b.re) * r + a.im * b.im * (r * r) := by ring, simp [this, r.prop], ring, } }, inv_fun := Ξ» f, ⟨f sqrtd, by rw [←f.map_mul, dmuld, ring_hom.map_int_cast]⟩, left_inv := Ξ» r, by { ext, simp }, right_inv := Ξ» f, by { ext, simp } } /-- `lift r` is injective if `d` is non-square, and R has characteristic zero (that is, the map from `β„€` into `R` is injective). -/ lemma lift_injective [char_zero R] {d : β„€} (r : {r : R // r * r = ↑d}) (hd : βˆ€ n : β„€, d β‰  n*n) : function.injective (lift r) := (injective_iff_map_eq_zero (lift r)).mpr $ Ξ» a ha, begin have h_inj : function.injective (coe : β„€ β†’ R) := int.cast_injective, suffices : lift r a.norm = 0, { simp only [coe_int_re, add_zero, lift_apply_apply, coe_int_im, int.cast_zero, zero_mul] at this, rwa [← int.cast_zero, h_inj.eq_iff, norm_eq_zero hd] at this }, rw [norm_eq_mul_conj, ring_hom.map_mul, ha, zero_mul] end end zsqrtd
ec6fc8fa1f5b93fae2a796e2d3c386dca1463431
bb31430994044506fa42fd667e2d556327e18dfe
/src/ring_theory/is_adjoin_root.lean
a9d74b7173734c8e96776d0e5fdfd3fb5ba0851b
[ "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
26,087
lean
/- Copyright (c) 2022 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import data.polynomial.algebra_map import field_theory.minpoly.gcd_monoid import ring_theory.power_basis /-! # A predicate on adjoining roots of polynomial This file defines a predicate `is_adjoin_root S f`, which states that the ring `S` can be constructed by adjoining a specified root of the polynomial `f : R[X]` to `R`. This predicate is useful when the same ring can be generated by adjoining the root of different polynomials, and you want to vary which polynomial you're considering. The results in this file are intended to mirror those in `ring_theory.adjoin_root`, in order to provide an easier way to translate results from one to the other. ## Motivation `adjoin_root` presents one construction of a ring `R[Ξ±]`. However, it is possible to obtain rings of this form in many ways, such as `number_field.ring_of_integers β„š(√-5)`, or `algebra.adjoin R {Ξ±, Ξ±^2}`, or `intermediate_field.adjoin R {Ξ±, 2 - Ξ±}`, or even if we want to view `β„‚` as adjoining a root of `X^2 + 1` to `ℝ`. ## Main definitions The two main predicates in this file are: * `is_adjoin_root S f`: `S` is generated by adjoining a specified root of `f : R[X]` to `R` * `is_adjoin_root_monic S f`: `S` is generated by adjoining a root of the monic polynomial `f : R[X]` to `R` Using `is_adjoin_root` to map into `S`: * `is_adjoin_root.map`: inclusion from `R[X]` to `S` * `is_adjoin_root.root`: the specific root adjoined to `R` to give `S` Using `is_adjoin_root` to map out of `S`: * `is_adjoin_root.repr`: choose a non-unique representative in `R[X]` * `is_adjoin_root.lift`, `is_adjoin_root.lift_hom`: lift a morphism `R β†’+* T` to `S β†’+* T` * `is_adjoin_root_monic.mod_by_monic_hom`: a unique representative in `R[X]` if `f` is monic ## Main results * `adjoin_root.is_adjoin_root` and `adjoin_root.is_adjoin_root_monic`: `adjoin_root` satisfies the conditions on `is_adjoin_root`(`_monic`) * `is_adjoin_root_monic.power_basis`: the `root` generates a power basis on `S` over `R` * `is_adjoin_root.aequiv`: algebra isomorphism showing adjoining a root gives a unique ring up to isomorphism * `is_adjoin_root.of_equiv`: transfer `is_adjoin_root` across an algebra isomorphism * `is_adjoin_root_eq.minpoly_eq`: the minimal polynomial of the adjoined root of `f` is equal to `f`, if `f` is irreducible and monic, and `R` is a GCD domain -/ open_locale polynomial open polynomial noncomputable theory universes u v section move_me end move_me /-- `is_adjoin_root S f` states that the ring `S` can be constructed by adjoining a specified root of the polynomial `f : R[X]` to `R`. Compare `power_basis R S`, which does not explicitly specify which polynomial we adjoin a root of (in particular `f` does not need to be the minimal polynomial of the root we adjoin), and `adjoin_root` which constructs a new type. This is not a typeclass because the choice of root given `S` and `f` is not unique. -/ @[nolint has_nonempty_instance] -- This class doesn't really make sense on a predicate structure is_adjoin_root {R : Type u} (S : Type v) [comm_semiring R] [semiring S] [algebra R S] (f : R[X]) : Type (max u v) := (map : R[X] β†’+* S) (map_surjective : function.surjective map) (ker_map : ring_hom.ker map = ideal.span {f}) (algebra_map_eq : algebra_map R S = map.comp polynomial.C) /-- `is_adjoin_root_monic S f` states that the ring `S` can be constructed by adjoining a specified root of the monic polynomial `f : R[X]` to `R`. As long as `f` is monic, there is a well-defined representation of elements of `S` as polynomials in `R[X]` of degree lower than `deg f` (see `mod_by_monic_hom` and `coeff`). In particular, we have `is_adjoin_root_monic.power_basis`. Bundling `monic` into this structure is very useful when working with explicit `f`s such as `X^2 - C a * X - C b` since it saves you carrying around the proofs of monicity. -/ @[nolint has_nonempty_instance] -- This class doesn't really make sense on a predicate structure is_adjoin_root_monic {R : Type u} (S : Type v) [comm_semiring R] [semiring S] [algebra R S] (f : R[X]) extends is_adjoin_root S f := (monic : monic f) section ring variables {R : Type u} {S : Type v} [comm_ring R] [ring S] {f : R[X]} [algebra R S] namespace is_adjoin_root /-- `(h : is_adjoin_root S f).root` is the root of `f` that can be adjoined to generate `S`. -/ def root (h : is_adjoin_root S f) : S := h.map X lemma subsingleton (h : is_adjoin_root S f) [subsingleton R] : subsingleton S := h.map_surjective.subsingleton lemma algebra_map_apply (h : is_adjoin_root S f) (x : R) : algebra_map R S x = h.map (polynomial.C x) := by rw [h.algebra_map_eq, ring_hom.comp_apply] @[simp] lemma mem_ker_map (h : is_adjoin_root S f) {p} : p ∈ ring_hom.ker h.map ↔ f ∣ p := by rw [h.ker_map, ideal.mem_span_singleton] lemma map_eq_zero_iff (h : is_adjoin_root S f) {p} : h.map p = 0 ↔ f ∣ p := by rw [← h.mem_ker_map, ring_hom.mem_ker] @[simp] lemma map_X (h : is_adjoin_root S f) : h.map X = h.root := rfl @[simp] lemma map_self (h : is_adjoin_root S f) : h.map f = 0 := h.map_eq_zero_iff.mpr dvd_rfl @[simp] lemma aeval_eq (h : is_adjoin_root S f) (p : R[X]) : aeval h.root p = h.map p := polynomial.induction_on p (Ξ» x, by { rw [aeval_C, h.algebra_map_apply] }) (Ξ» p q ihp ihq, by rw [alg_hom.map_add, ring_hom.map_add, ihp, ihq]) (Ξ» n x ih, by { rw [alg_hom.map_mul, aeval_C, alg_hom.map_pow, aeval_X, ring_hom.map_mul, ← h.algebra_map_apply, ring_hom.map_pow, map_X] }) @[simp] lemma aeval_root (h : is_adjoin_root S f) : aeval h.root f = 0 := by rw [aeval_eq, map_self] /-- Choose an arbitrary representative so that `h.map (h.repr x) = x`. If `f` is monic, use `is_adjoin_root_monic.mod_by_monic_hom` for a unique choice of representative. -/ def repr (h : is_adjoin_root S f) (x : S) : R[X] := (h.map_surjective x).some lemma map_repr (h : is_adjoin_root S f) (x : S) : h.map (h.repr x) = x := (h.map_surjective x).some_spec /-- `repr` preserves zero, up to multiples of `f` -/ lemma repr_zero_mem_span (h : is_adjoin_root S f) : h.repr 0 ∈ ideal.span ({f} : set R[X]) := by rw [← h.ker_map, ring_hom.mem_ker, h.map_repr] /-- `repr` preserves addition, up to multiples of `f` -/ lemma repr_add_sub_repr_add_repr_mem_span (h : is_adjoin_root S f) (x y : S) : h.repr (x + y) - (h.repr x + h.repr y) ∈ ideal.span ({f} : set R[X]) := by rw [← h.ker_map, ring_hom.mem_ker, map_sub, h.map_repr, map_add, h.map_repr, h.map_repr, sub_self] /-- Extensionality of the `is_adjoin_root` structure itself. See `is_adjoin_root_monic.ext_elem` for extensionality of the ring elements. -/ lemma ext_map (h h' : is_adjoin_root S f) (eq : βˆ€ x, h.map x = h'.map x) : h = h' := begin cases h, cases h', congr, exact ring_hom.ext eq end /-- Extensionality of the `is_adjoin_root` structure itself. See `is_adjoin_root_monic.ext_elem` for extensionality of the ring elements. -/ @[ext] lemma ext (h h' : is_adjoin_root S f) (eq : h.root = h'.root) : h = h' := h.ext_map h' (Ξ» x, by rw [← h.aeval_eq, ← h'.aeval_eq, eq]) section lift variables {T : Type*} [comm_ring T] {i : R β†’+* T} {x : T} (hx : f.evalβ‚‚ i x = 0) include hx /-- Auxiliary lemma for `is_adjoin_root.lift` -/ lemma evalβ‚‚_repr_eq_evalβ‚‚_of_map_eq (h : is_adjoin_root S f) (z : S) (w : R[X]) (hzw : h.map w = z) : (h.repr z).evalβ‚‚ i x = w.evalβ‚‚ i x := begin rw [eq_comm, ← sub_eq_zero, ← h.map_repr z, ← map_sub, h.map_eq_zero_iff] at hzw, obtain ⟨y, hy⟩ := hzw, rw [← sub_eq_zero, ← evalβ‚‚_sub, hy, evalβ‚‚_mul, hx, zero_mul] end variables (i x) -- To match `adjoin_root.lift` /-- Lift a ring homomorphism `R β†’+* T` to `S β†’+* T` by specifying a root `x` of `f` in `T`, where `S` is given by adjoining a root of `f` to `R`. -/ def lift (h : is_adjoin_root S f) : S β†’+* T := { to_fun := Ξ» z, (h.repr z).evalβ‚‚ i x, map_zero' := by rw [h.evalβ‚‚_repr_eq_evalβ‚‚_of_map_eq hx _ _ (map_zero _), evalβ‚‚_zero], map_add' := Ξ» z w, begin rw [h.evalβ‚‚_repr_eq_evalβ‚‚_of_map_eq hx _ (h.repr z + h.repr w), evalβ‚‚_add], { rw [map_add, map_repr, map_repr] } end, map_one' := by rw [h.evalβ‚‚_repr_eq_evalβ‚‚_of_map_eq hx _ _ (map_one _), evalβ‚‚_one], map_mul' := Ξ» z w, begin rw [h.evalβ‚‚_repr_eq_evalβ‚‚_of_map_eq hx _ (h.repr z * h.repr w), evalβ‚‚_mul], { rw [map_mul, map_repr, map_repr] } end } variables {i x} @[simp] lemma lift_map (h : is_adjoin_root S f) (z : R[X]) : h.lift i x hx (h.map z) = z.evalβ‚‚ i x := by rw [lift, ring_hom.coe_mk, h.evalβ‚‚_repr_eq_evalβ‚‚_of_map_eq hx _ _ rfl] @[simp] lemma lift_root (h : is_adjoin_root S f) : h.lift i x hx h.root = x := by rw [← h.map_X, lift_map, evalβ‚‚_X] @[simp] lemma lift_algebra_map (h : is_adjoin_root S f) (a : R) : h.lift i x hx (algebra_map R S a) = i a := by rw [h.algebra_map_apply, lift_map, evalβ‚‚_C] /-- Auxiliary lemma for `apply_eq_lift` -/ lemma apply_eq_lift (h : is_adjoin_root S f) (g : S β†’+* T) (hmap : βˆ€ a, g (algebra_map R S a) = i a) (hroot : g h.root = x) (a : S): g a = h.lift i x hx a := begin rw [← h.map_repr a, polynomial.as_sum_range_C_mul_X_pow (h.repr a)], simp only [map_sum, map_mul, map_pow, h.map_X, hroot, ← h.algebra_map_apply, hmap, lift_root, lift_algebra_map] end /-- Unicity of `lift`: a map that agrees on `R` and `h.root` agrees with `lift` everywhere. -/ lemma eq_lift (h : is_adjoin_root S f) (g : S β†’+* T) (hmap : βˆ€ a, g (algebra_map R S a) = i a) (hroot : g h.root = x) : g = h.lift i x hx := ring_hom.ext (h.apply_eq_lift hx g hmap hroot) variables [algebra R T] (hx' : aeval x f = 0) omit hx variables (x) -- To match `adjoin_root.lift_hom` /-- Lift the algebra map `R β†’ T` to `S →ₐ[R] T` by specifying a root `x` of `f` in `T`, where `S` is given by adjoining a root of `f` to `R`. -/ def lift_hom (h : is_adjoin_root S f) : S →ₐ[R] T := { commutes' := Ξ» a, h.lift_algebra_map hx' a, .. h.lift (algebra_map R T) x hx' } variables {x} @[simp] lemma coe_lift_hom (h : is_adjoin_root S f) : (h.lift_hom x hx' : S β†’+* T) = h.lift (algebra_map R T) x hx' := rfl lemma lift_algebra_map_apply (h : is_adjoin_root S f) (z : S) : h.lift (algebra_map R T) x hx' z = h.lift_hom x hx' z := rfl @[simp] lemma lift_hom_map (h : is_adjoin_root S f) (z : R[X]) : h.lift_hom x hx' (h.map z) = aeval x z := by rw [← lift_algebra_map_apply, lift_map, aeval_def] @[simp] lemma lift_hom_root (h : is_adjoin_root S f) : h.lift_hom x hx' h.root = x := by rw [← lift_algebra_map_apply, lift_root] /-- Unicity of `lift_hom`: a map that agrees on `h.root` agrees with `lift_hom` everywhere. -/ lemma eq_lift_hom (h : is_adjoin_root S f) (g : S →ₐ[R] T) (hroot : g h.root = x) : g = h.lift_hom x hx' := alg_hom.ext (h.apply_eq_lift hx' g g.commutes hroot) end lift end is_adjoin_root namespace adjoin_root variables (f) /-- `adjoin_root f` is indeed given by adjoining a root of `f`. -/ protected def is_adjoin_root : is_adjoin_root (adjoin_root f) f := { map := adjoin_root.mk f, map_surjective := ideal.quotient.mk_surjective, ker_map := begin ext, rw [ring_hom.mem_ker, ← @adjoin_root.mk_self _ _ f, adjoin_root.mk_eq_mk, ideal.mem_span_singleton, ← dvd_add_left (dvd_refl f), sub_add_cancel] end, algebra_map_eq := adjoin_root.algebra_map_eq f } /-- `adjoin_root f` is indeed given by adjoining a root of `f`. If `f` is monic this is more powerful than `adjoin_root.is_adjoin_root`. -/ protected def is_adjoin_root_monic (hf : monic f) : is_adjoin_root_monic (adjoin_root f) f := { monic := hf, .. adjoin_root.is_adjoin_root f } end adjoin_root namespace is_adjoin_root_monic open is_adjoin_root lemma map_mod_by_monic (h : is_adjoin_root_monic S f) (g : R[X]) : h.map (g %β‚˜ f) = h.map g := begin rw [← ring_hom.sub_mem_ker_iff, mem_ker_map, mod_by_monic_eq_sub_mul_div _ h.monic, sub_right_comm, sub_self, zero_sub, dvd_neg], exact ⟨_, rfl⟩ end lemma mod_by_monic_repr_map (h : is_adjoin_root_monic S f) (g : R[X]) : h.repr (h.map g) %β‚˜ f = g %β‚˜ f := mod_by_monic_eq_of_dvd_sub h.monic $ by rw [← h.mem_ker_map, ring_hom.sub_mem_ker_iff, map_repr] /-- `is_adjoin_root.mod_by_monic_hom` sends the equivalence class of `f` mod `g` to `f %β‚˜ g`. -/ def mod_by_monic_hom (h : is_adjoin_root_monic S f) : S β†’β‚—[R] R[X] := { to_fun := Ξ» x, h.repr x %β‚˜ f, map_add' := Ξ» x y, by conv_lhs { rw [← h.map_repr x, ← h.map_repr y, ← map_add, h.mod_by_monic_repr_map, add_mod_by_monic] }, map_smul' := Ξ» c x, by rw [ring_hom.id_apply, ← h.map_repr x, algebra.smul_def, h.algebra_map_apply, ← map_mul, h.mod_by_monic_repr_map, ← smul_eq_C_mul, smul_mod_by_monic, h.map_repr] } @[simp] lemma mod_by_monic_hom_map (h : is_adjoin_root_monic S f) (g : R[X]) : h.mod_by_monic_hom (h.map g) = g %β‚˜ f := h.mod_by_monic_repr_map g @[simp] lemma map_mod_by_monic_hom (h : is_adjoin_root_monic S f) (x : S) : h.map (h.mod_by_monic_hom x) = x := by rw [mod_by_monic_hom, linear_map.coe_mk, map_mod_by_monic, map_repr] @[simp] lemma mod_by_monic_hom_root_pow (h : is_adjoin_root_monic S f) {n : β„•} (hdeg : n < nat_degree f) : h.mod_by_monic_hom (h.root ^ n) = X ^ n := begin nontriviality R, rwa [← h.map_X, ← map_pow, mod_by_monic_hom_map, mod_by_monic_eq_self_iff h.monic, degree_X_pow], contrapose! hdeg, simpa [nat_degree_le_iff_degree_le] using hdeg end @[simp] lemma mod_by_monic_hom_root (h : is_adjoin_root_monic S f) (hdeg : 1 < nat_degree f) : h.mod_by_monic_hom h.root = X := by simpa using mod_by_monic_hom_root_pow h hdeg /-- The basis on `S` generated by powers of `h.root`. Auxiliary definition for `is_adjoin_root_monic.power_basis`. -/ def basis (h : is_adjoin_root_monic S f) : basis (fin (nat_degree f)) R S := basis.of_repr { to_fun := Ξ» x, (h.mod_by_monic_hom x).to_finsupp.comap_domain coe (fin.coe_injective.inj_on _), inv_fun := Ξ» g, h.map (of_finsupp (g.map_domain coe)), left_inv := Ξ» x, begin casesI subsingleton_or_nontrivial R, { haveI := h.subsingleton, exact subsingleton.elim _ _ }, simp only, rw [finsupp.map_domain_comap_domain, polynomial.eta, h.map_mod_by_monic_hom x], intros i hi, refine set.mem_range.mpr ⟨⟨i, _⟩, rfl⟩, contrapose! hi, simp only [polynomial.to_finsupp_apply, not_not, finsupp.mem_support_iff, ne.def, mod_by_monic_hom, linear_map.coe_mk, finset.mem_coe], by_cases hx : h.to_is_adjoin_root.repr x %β‚˜ f = 0, { simp [hx] }, refine coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le _ hi), rw nat_degree_lt_nat_degree_iff hx, { exact degree_mod_by_monic_lt _ h.monic }, end, right_inv := Ξ» g, begin nontriviality R, ext i, simp only [h.mod_by_monic_hom_map, finsupp.comap_domain_apply, polynomial.to_finsupp_apply], rw [(polynomial.mod_by_monic_eq_self_iff h.monic).mpr, polynomial.coeff, finsupp.map_domain_apply fin.coe_injective], rw [degree_eq_nat_degree h.monic.ne_zero, degree_lt_iff_coeff_zero], intros m hm, rw [polynomial.coeff, finsupp.map_domain_notin_range], rw [set.mem_range, not_exists], rintro i rfl, exact i.prop.not_le hm end, map_add' := Ξ» x y, by simp only [map_add, finsupp.comap_domain_add_of_injective fin.coe_injective, to_finsupp_add], map_smul' := Ξ» c x, by simp only [map_smul, finsupp.comap_domain_smul_of_injective fin.coe_injective, ring_hom.id_apply, to_finsupp_smul] } @[simp] lemma basis_apply (h : is_adjoin_root_monic S f) (i) : h.basis i = h.root ^ (i : β„•) := basis.apply_eq_iff.mpr $ show (h.mod_by_monic_hom (h.to_is_adjoin_root.root ^ (i : β„•))).to_finsupp .comap_domain coe (fin.coe_injective.inj_on _) = finsupp.single _ _, begin ext j, rw [finsupp.comap_domain_apply, mod_by_monic_hom_root_pow], { rw [X_pow_eq_monomial, to_finsupp_monomial, finsupp.single_apply_left fin.coe_injective] }, { exact i.is_lt }, end lemma deg_pos [nontrivial S] (h : is_adjoin_root_monic S f) : 0 < nat_degree f := begin rcases h.basis.index_nonempty with ⟨⟨i, hi⟩⟩, exact (nat.zero_le _).trans_lt hi end lemma deg_ne_zero [nontrivial S] (h : is_adjoin_root_monic S f) : nat_degree f β‰  0 := h.deg_pos.ne' /-- If `f` is monic, the powers of `h.root` form a basis. -/ @[simps gen dim basis] def power_basis (h : is_adjoin_root_monic S f) : power_basis R S := { gen := h.root, dim := nat_degree f, basis := h.basis, basis_eq_pow := h.basis_apply } @[simp] lemma basis_repr (h : is_adjoin_root_monic S f) (x : S) (i : fin (nat_degree f)) : h.basis.repr x i = (h.mod_by_monic_hom x).coeff (i : β„•) := begin change (h.mod_by_monic_hom x).to_finsupp.comap_domain coe (fin.coe_injective.inj_on _) i = _, rw [finsupp.comap_domain_apply, polynomial.to_finsupp_apply] end lemma basis_one (h : is_adjoin_root_monic S f) (hdeg : 1 < nat_degree f) : h.basis ⟨1, hdeg⟩ = h.root := by rw [h.basis_apply, fin.coe_mk, pow_one] /-- `is_adjoin_root_monic.lift_polyβ‚—` lifts a linear map on polynomials to a linear map on `S`. -/ @[simps] def lift_polyβ‚— {T : Type*} [add_comm_group T] [module R T] (h : is_adjoin_root_monic S f) (g : R[X] β†’β‚—[R] T) : S β†’β‚—[R] T := g.comp h.mod_by_monic_hom /-- `is_adjoin_root_monic.coeff h x i` is the `i`th coefficient of the representative of `x : S`. -/ def coeff (h : is_adjoin_root_monic S f) : S β†’β‚—[R] (β„• β†’ R) := h.lift_polyβ‚— { to_fun := polynomial.coeff, map_add' := Ξ» p q, funext (polynomial.coeff_add p q), map_smul' := Ξ» c p, funext (polynomial.coeff_smul c p) } lemma coeff_apply_lt (h : is_adjoin_root_monic S f) (z : S) (i : β„•) (hi : i < nat_degree f) : h.coeff z i = h.basis.repr z ⟨i, hi⟩ := begin simp only [coeff, linear_map.comp_apply, finsupp.lcoe_fun_apply, finsupp.lmap_domain_apply, linear_equiv.coe_coe, lift_polyβ‚—_apply, linear_map.coe_mk, h.basis_repr], refl end lemma coeff_apply_coe (h : is_adjoin_root_monic S f) (z : S) (i : fin (nat_degree f)) : h.coeff z i = h.basis.repr z i := h.coeff_apply_lt z i i.prop lemma coeff_apply_le (h : is_adjoin_root_monic S f) (z : S) (i : β„•) (hi : nat_degree f ≀ i) : h.coeff z i = 0 := begin simp only [coeff, linear_map.comp_apply, finsupp.lcoe_fun_apply, finsupp.lmap_domain_apply, linear_equiv.coe_coe, lift_polyβ‚—_apply, linear_map.coe_mk, h.basis_repr], nontriviality R, exact polynomial.coeff_eq_zero_of_degree_lt ((degree_mod_by_monic_lt _ h.monic).trans_le (polynomial.degree_le_of_nat_degree_le hi)), end lemma coeff_apply (h : is_adjoin_root_monic S f) (z : S) (i : β„•) : h.coeff z i = if hi : i < nat_degree f then h.basis.repr z ⟨i, hi⟩ else 0 := begin split_ifs with hi, { exact h.coeff_apply_lt z i hi }, { exact h.coeff_apply_le z i (le_of_not_lt hi) }, end lemma coeff_root_pow (h : is_adjoin_root_monic S f) {n} (hn : n < nat_degree f) : h.coeff (h.root ^ n) = pi.single n 1 := begin ext i, rw coeff_apply, split_ifs with hi, { calc h.basis.repr (h.root ^ n) ⟨i, _⟩ = h.basis.repr (h.basis ⟨n, hn⟩) ⟨i, hi⟩ : by rw [h.basis_apply, fin.coe_mk] ... = pi.single ((⟨n, hn⟩ : fin _) : β„•) (1 : (Ξ» _, R) _) ↑(⟨i, _⟩ : fin _) : by rw [h.basis.repr_self, ← finsupp.single_eq_pi_single, finsupp.single_apply_left fin.coe_injective] ... = pi.single n 1 i : by rw [fin.coe_mk, fin.coe_mk] }, { refine (pi.single_eq_of_ne _ (1 : (Ξ» _, R) _)).symm, rintro rfl, simpa [hi] using hn }, end lemma coeff_one [nontrivial S] (h : is_adjoin_root_monic S f) : h.coeff 1 = pi.single 0 1 := by rw [← h.coeff_root_pow h.deg_pos, pow_zero] lemma coeff_root (h : is_adjoin_root_monic S f) (hdeg : 1 < (nat_degree f)) : h.coeff h.root = pi.single 1 1 := by rw [← h.coeff_root_pow hdeg, pow_one] lemma coeff_algebra_map [nontrivial S] (h : is_adjoin_root_monic S f) (x : R) : h.coeff (algebra_map R S x) = pi.single 0 x := begin ext i, rw [algebra.algebra_map_eq_smul_one, map_smul, coeff_one, pi.smul_apply, smul_eq_mul], refine (pi.apply_single (Ξ» _ y, x * y) _ 0 1 i).trans (by simp), intros, simp end lemma ext_elem (h : is_adjoin_root_monic S f) ⦃x y : S⦄ (hxy : βˆ€ i < (nat_degree f), h.coeff x i = h.coeff y i) : x = y := equiv_like.injective h.basis.equiv_fun $ funext $ Ξ» i, show h.basis.equiv_fun x i = h.basis.equiv_fun y i, by rw [basis.equiv_fun_apply, ← h.coeff_apply_coe, basis.equiv_fun_apply, ← h.coeff_apply_coe, hxy i i.prop] lemma ext_elem_iff (h : is_adjoin_root_monic S f) {x y : S} : x = y ↔ βˆ€ i < (nat_degree f), h.coeff x i = h.coeff y i := ⟨λ hxy i hi, hxy β–Έ rfl, Ξ» hxy, h.ext_elem hxy⟩ lemma coeff_injective (h : is_adjoin_root_monic S f) : function.injective h.coeff := Ξ» x y hxy, h.ext_elem (Ξ» i hi, hxy β–Έ rfl) lemma is_integral_root (h : is_adjoin_root_monic S f) : is_integral R h.root := ⟨f, h.monic, h.aeval_root⟩ end is_adjoin_root_monic end ring section comm_ring variables {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] [algebra R S] {f : R[X]} namespace is_adjoin_root section lift @[simp] lemma lift_self_apply (h : is_adjoin_root S f) (x : S) : h.lift (algebra_map R S) h.root h.aeval_root x = x := by rw [← h.map_repr x, lift_map, ← aeval_def, h.aeval_eq] lemma lift_self (h : is_adjoin_root S f) : h.lift (algebra_map R S) h.root h.aeval_root = ring_hom.id S := ring_hom.ext (h.lift_self_apply) end lift section equiv variables {T : Type*} [comm_ring T] [algebra R T] /-- Adjoining a root gives a unique ring up to algebra isomorphism. This is the converse of `is_adjoin_root.of_equiv`: this turns an `is_adjoin_root` into an `alg_equiv`, and `is_adjoin_root.of_equiv` turns an `alg_equiv` into an `is_adjoin_root`. -/ def aequiv (h : is_adjoin_root S f) (h' : is_adjoin_root T f) : S ≃ₐ[R] T := { to_fun := h.lift_hom h'.root h'.aeval_root, inv_fun := h'.lift_hom h.root h.aeval_root, left_inv := Ξ» x, by rw [← h.map_repr x, lift_hom_map, aeval_eq, lift_hom_map, aeval_eq], right_inv := Ξ» x, by rw [← h'.map_repr x, lift_hom_map, aeval_eq, lift_hom_map, aeval_eq], .. h.lift_hom h'.root h'.aeval_root } @[simp] lemma aequiv_map (h : is_adjoin_root S f) (h' : is_adjoin_root T f) (z : R[X]) : h.aequiv h' (h.map z) = h'.map z := by rw [aequiv, alg_equiv.coe_mk, lift_hom_map, aeval_eq] @[simp] lemma aequiv_root (h : is_adjoin_root S f) (h' : is_adjoin_root T f) : h.aequiv h' h.root = h'.root := by rw [aequiv, alg_equiv.coe_mk, lift_hom_root] @[simp] lemma aequiv_self (h : is_adjoin_root S f) : h.aequiv h = alg_equiv.refl := by { ext a, exact h.lift_self_apply a } @[simp] lemma aequiv_symm (h : is_adjoin_root S f) (h' : is_adjoin_root T f) : (h.aequiv h').symm = h'.aequiv h := by { ext, refl } @[simp] lemma lift_aequiv {U : Type*} [comm_ring U] (h : is_adjoin_root S f) (h' : is_adjoin_root T f) (i : R β†’+* U) (x hx z) : (h'.lift i x hx (h.aequiv h' z)) = h.lift i x hx z := by rw [← h.map_repr z, aequiv_map, lift_map, lift_map] @[simp] lemma lift_hom_aequiv {U : Type*} [comm_ring U] [algebra R U] (h : is_adjoin_root S f) (h' : is_adjoin_root T f) (x : U) (hx z) : (h'.lift_hom x hx (h.aequiv h' z)) = h.lift_hom x hx z := h.lift_aequiv h' _ _ hx _ @[simp] lemma aequiv_aequiv {U : Type*} [comm_ring U] [algebra R U] (h : is_adjoin_root S f) (h' : is_adjoin_root T f) (h'' : is_adjoin_root U f) (x) : (h'.aequiv h'') (h.aequiv h' x) = h.aequiv h'' x := h.lift_hom_aequiv _ _ h''.aeval_root _ @[simp] lemma aequiv_trans {U : Type*} [comm_ring U] [algebra R U] (h : is_adjoin_root S f) (h' : is_adjoin_root T f) (h'' : is_adjoin_root U f) : (h.aequiv h').trans (h'.aequiv h'') = h.aequiv h'' := by { ext z, exact h.aequiv_aequiv h' h'' z } /-- Transfer `is_adjoin_root` across an algebra isomorphism. This is the converse of `is_adjoin_root.aequiv`: this turns an `alg_equiv` into an `is_adjoin_root`, and `is_adjoin_root.aequiv` turns an `is_adjoin_root` into an `alg_equiv`. -/ @[simps map_apply] def of_equiv (h : is_adjoin_root S f) (e : S ≃ₐ[R] T) : is_adjoin_root T f := { map := ((e : S ≃+* T) : S β†’+* T).comp h.map, map_surjective := e.surjective.comp h.map_surjective, ker_map := by rw [← ring_hom.comap_ker, ring_hom.ker_coe_equiv, ← ring_hom.ker_eq_comap_bot, h.ker_map], algebra_map_eq := by ext; simp only [alg_equiv.commutes, ring_hom.comp_apply, alg_equiv.coe_ring_equiv, ring_equiv.coe_to_ring_hom, ← h.algebra_map_apply] } @[simp] lemma of_equiv_root (h : is_adjoin_root S f) (e : S ≃ₐ[R] T) : (h.of_equiv e).root = e h.root := rfl @[simp] lemma aequiv_of_equiv {U : Type*} [comm_ring U] [algebra R U] (h : is_adjoin_root S f) (h' : is_adjoin_root T f) (e : T ≃ₐ[R] U) : h.aequiv (h'.of_equiv e) = (h.aequiv h').trans e := by ext a; rw [← h.map_repr a, aequiv_map, alg_equiv.trans_apply, aequiv_map, of_equiv_map_apply] @[simp] lemma of_equiv_aequiv {U : Type*} [comm_ring U] [algebra R U] (h : is_adjoin_root S f) (h' : is_adjoin_root U f) (e : S ≃ₐ[R] T) : (h.of_equiv e).aequiv h' = e.symm.trans (h.aequiv h') := by ext a; rw [← (h.of_equiv e).map_repr a, aequiv_map, alg_equiv.trans_apply, of_equiv_map_apply, e.symm_apply_apply, aequiv_map] end equiv end is_adjoin_root namespace is_adjoin_root_monic lemma minpoly_eq [is_domain R] [is_domain S] [no_zero_smul_divisors R S] [normalized_gcd_monoid R] (h : is_adjoin_root_monic S f) (hirr : irreducible f) : minpoly R h.root = f := let ⟨q, hq⟩ := minpoly.gcd_domain_dvd h.is_integral_root h.monic.ne_zero h.aeval_root in symm $ eq_of_monic_of_associated h.monic (minpoly.monic h.is_integral_root) $ by convert (associated.mul_left (minpoly R h.root) $ associated_one_iff_is_unit.2 $ (hirr.is_unit_or_is_unit hq).resolve_left $ minpoly.not_is_unit R h.root); rw mul_one end is_adjoin_root_monic end comm_ring
81e02554cc4a390a755ad01b51da3b53182071cc
0d7f5899c0475f9e105a439896d9377f80c0d7c3
/src/examples/monoid.lean
f8df1505848b1045ae86b7c2fdc6fe078cb7c00f
[]
no_license
adamtopaz/UnivAlg
127038f320e68cdf3efcd0c084c9af02fdb8da3d
2458d47a6e4fd0525e3a25b07cb7dd518ac173ef
refs/heads/master
1,670,320,985,286
1,597,350,882,000
1,597,350,882,000
280,585,500
4
0
null
1,597,350,883,000
1,595,048,527,000
Lean
UTF-8
Lean
false
false
926
lean
import ..lang import data.nat.basic namespace langs namespace monoid inductive preL : β„• β†’ Type | one : preL 0 | mul : preL 2 --| const : preL 1 def L : lang := ⟨preL⟩ open lang.term inductive preR : Ξ  n, L.gen n β†’ L.gen n β†’ Prop | one_mul : preR 1 get $ proj (fin.cast $ show 0 + 1 = 1, by simp) (compl (of preL.one) (proj (fin.cast $ show 2 = 1 + 1, by simp) (of preL.mul))) | mul_one : preR 1 get $ proj (fin.cast $ show 1 + 0 = 1, by simp) (compr (of preL.one) (proj (fin.cast $ show 2 = 1 + 1, by simp) (of preL.mul))) | mul_assoc : preR 3 (proj (fin.cast $ show 2 + 1 = 3, by simp) $ compl (of preL.mul) (proj (fin.cast $ show 2 = 1 + 1, by simp) $ of preL.mul)) (proj (fin.cast $ show 1 + 2 = 3, by simp) $ compr (of preL.mul) (proj (fin.cast $ show 2 = 1 + 1, by simp) $ of preL.mul)) def R : rules L := ⟨preR⟩ end monoid end langs
8ce24bf64d50c9220a743464f3d2043b5b221d2c
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/measure_theory/constructions/borel_space.lean
283316dff24095e2bb9f1c77bc5c0b67e6bc0b6a
[ "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
85,920
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Yury Kudryashov -/ import measure_theory.function.ae_measurable_sequence import analysis.complex.basic import analysis.normed_space.finite_dimension import measure_theory.group.arithmetic import measure_theory.lattice import measure_theory.measure.open_pos import topology.algebra.ordered.liminf_limsup import topology.continuous_function.basic import topology.instances.ereal import topology.G_delta import topology.semicontinuous import topology.order.lattice /-! # Borel (measurable) space ## Main definitions * `borel Ξ±` : the least `Οƒ`-algebra that contains all open sets; * `class borel_space` : a space with `topological_space` and `measurable_space` structures such that `β€Ήmeasurable_space Ξ±β€Ί = borel Ξ±`; * `class opens_measurable_space` : a space with `topological_space` and `measurable_space` structures such that all open sets are measurable; equivalently, `borel Ξ± ≀ β€Ήmeasurable_space Ξ±β€Ί`. * `borel_space` instances on `empty`, `unit`, `bool`, `nat`, `int`, `rat`; * `measurable` and `borel_space` instances on `ℝ`, `ℝβ‰₯0`, `ℝβ‰₯0∞`. ## Main statements * `is_open.measurable_set`, `is_closed.measurable_set`: open and closed sets are measurable; * `continuous.measurable` : a continuous function is measurable; * `continuous.measurable2` : if `f : Ξ± β†’ Ξ²` and `g : Ξ± β†’ Ξ³` are measurable and `op : Ξ² Γ— Ξ³ β†’ Ξ΄` is continuous, then `Ξ» x, op (f x, g y)` is measurable; * `measurable.add` etc : dot notation for arithmetic operations on `measurable` predicates, and similarly for `dist` and `edist`; * `ae_measurable.add` : similar dot notation for almost everywhere measurable functions; * `measurable.ennreal*` : special cases for arithmetic operations on `ℝβ‰₯0∞`. -/ noncomputable theory open classical set filter measure_theory open_locale classical big_operators topological_space nnreal ennreal interval universes u v w x y variables {Ξ± Ξ² Ξ³ Ξ³β‚‚ Ξ΄ : Type*} {ΞΉ : Sort y} {s t u : set Ξ±} open measurable_space topological_space /-- `measurable_space` structure generated by `topological_space`. -/ def borel (Ξ± : Type u) [topological_space Ξ±] : measurable_space Ξ± := generate_from {s : set Ξ± | is_open s} lemma borel_eq_top_of_discrete [topological_space Ξ±] [discrete_topology Ξ±] : borel Ξ± = ⊀ := top_le_iff.1 $ Ξ» s hs, generate_measurable.basic s (is_open_discrete s) lemma borel_eq_top_of_encodable [topological_space Ξ±] [t1_space Ξ±] [encodable Ξ±] : borel Ξ± = ⊀ := begin refine (top_le_iff.1 $ Ξ» s hs, bUnion_of_singleton s β–Έ _), apply measurable_set.bUnion s.countable_encodable, intros x hx, apply measurable_set.of_compl, apply generate_measurable.basic, exact is_closed_singleton.is_open_compl end lemma borel_eq_generate_from_of_subbasis {s : set (set Ξ±)} [t : topological_space Ξ±] [second_countable_topology Ξ±] (hs : t = generate_from s) : borel Ξ± = generate_from s := le_antisymm (generate_from_le $ assume u (hu : t.is_open u), begin rw [hs] at hu, induction hu, case generate_open.basic : u hu { exact generate_measurable.basic u hu }, case generate_open.univ { exact @measurable_set.univ Ξ± (generate_from s) }, case generate_open.inter : s₁ sβ‚‚ _ _ hs₁ hsβ‚‚ { exact @measurable_set.inter Ξ± (generate_from s) _ _ hs₁ hsβ‚‚ }, case generate_open.sUnion : f hf ih { rcases is_open_sUnion_countable f (by rwa hs) with ⟨v, hv, vf, vu⟩, rw ← vu, exact @measurable_set.sUnion Ξ± (generate_from s) _ hv (Ξ» x xv, ih _ (vf xv)) } end) (generate_from_le $ assume u hu, generate_measurable.basic _ $ show t.is_open u, by rw [hs]; exact generate_open.basic _ hu) lemma topological_space.is_topological_basis.borel_eq_generate_from [topological_space Ξ±] [second_countable_topology Ξ±] {s : set (set Ξ±)} (hs : is_topological_basis s) : borel Ξ± = generate_from s := borel_eq_generate_from_of_subbasis hs.eq_generate_from lemma is_pi_system_is_open [topological_space Ξ±] : is_pi_system (is_open : set Ξ± β†’ Prop) := Ξ» s hs t ht hst, is_open.inter hs ht lemma borel_eq_generate_from_is_closed [topological_space Ξ±] : borel Ξ± = generate_from {s | is_closed s} := le_antisymm (generate_from_le $ Ξ» t ht, @measurable_set.of_compl Ξ± _ (generate_from {s | is_closed s}) (generate_measurable.basic _ $ is_closed_compl_iff.2 ht)) (generate_from_le $ Ξ» t ht, @measurable_set.of_compl Ξ± _ (borel Ξ±) (generate_measurable.basic _ $ is_open_compl_iff.2 ht)) section order_topology variable (Ξ±) variables [topological_space Ξ±] [second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] lemma borel_eq_generate_from_Iio : borel Ξ± = generate_from (range Iio) := begin refine le_antisymm _ (generate_from_le _), { rw borel_eq_generate_from_of_subbasis (@order_topology.topology_eq_generate_intervals Ξ± _ _ _), letI : measurable_space Ξ± := measurable_space.generate_from (range Iio), have H : βˆ€ a : Ξ±, measurable_set (Iio a) := Ξ» a, generate_measurable.basic _ ⟨_, rfl⟩, refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩; [skip, apply H], by_cases h : βˆƒ a', βˆ€ b, a < b ↔ a' ≀ b, { rcases h with ⟨a', ha'⟩, rw (_ : Ioi a = (Iio a')ᢜ), { exact (H _).compl }, simp [set.ext_iff, ha'] }, { rcases is_open_Union_countable (Ξ» a' : {a' : Ξ± // a < a'}, {b | a'.1 < b}) (Ξ» a', is_open_lt' _) with ⟨v, ⟨hv⟩, vu⟩, simp [set.ext_iff] at vu, have : Ioi a = ⋃ x : v, (Iio x.1.1)ᢜ, { simp [set.ext_iff], refine Ξ» x, ⟨λ ax, _, Ξ» ⟨a', ⟨h, av⟩, ax⟩, lt_of_lt_of_le h ax⟩, rcases (vu x).2 _ with ⟨a', h₁, hβ‚‚βŸ©, { exact ⟨a', h₁, le_of_lt hβ‚‚βŸ© }, refine not_imp_comm.1 (Ξ» h, _) h, exact ⟨x, Ξ» b, ⟨λ ab, le_of_not_lt (Ξ» h', h ⟨b, ab, h'⟩), lt_of_lt_of_le ax⟩⟩ }, rw this, resetI, apply measurable_set.Union, exact Ξ» _, (H _).compl } }, { rw forall_range_iff, intro a, exact generate_measurable.basic _ is_open_Iio } end lemma borel_eq_generate_from_Ioi : borel Ξ± = generate_from (range Ioi) := @borel_eq_generate_from_Iio (order_dual Ξ±) _ (by apply_instance : second_countable_topology Ξ±) _ _ end order_topology lemma borel_comap {f : Ξ± β†’ Ξ²} {t : topological_space Ξ²} : @borel Ξ± (t.induced f) = (@borel Ξ² t).comap f := comap_generate_from.symm lemma continuous.borel_measurable [topological_space Ξ±] [topological_space Ξ²] {f : Ξ± β†’ Ξ²} (hf : continuous f) : @measurable Ξ± Ξ² (borel Ξ±) (borel Ξ²) f := measurable.of_le_map $ generate_from_le $ Ξ» s hs, generate_measurable.basic (f ⁻¹' s) (hs.preimage hf) /-- A space with `measurable_space` and `topological_space` structures such that all open sets are measurable. -/ class opens_measurable_space (Ξ± : Type*) [topological_space Ξ±] [h : measurable_space Ξ±] : Prop := (borel_le : borel Ξ± ≀ h) /-- A space with `measurable_space` and `topological_space` structures such that the `Οƒ`-algebra of measurable sets is exactly the `Οƒ`-algebra generated by open sets. -/ class borel_space (Ξ± : Type*) [topological_space Ξ±] [measurable_space Ξ±] : Prop := (measurable_eq : β€Ήmeasurable_space Ξ±β€Ί = borel Ξ±) @[priority 100] instance order_dual.opens_measurable_space {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±] [h : opens_measurable_space Ξ±] : opens_measurable_space (order_dual Ξ±) := { borel_le := h.borel_le } @[priority 100] instance order_dual.borel_space {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±] [h : borel_space Ξ±] : borel_space (order_dual Ξ±) := { measurable_eq := h.measurable_eq } /-- In a `borel_space` all open sets are measurable. -/ @[priority 100] instance borel_space.opens_measurable {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±] [borel_space Ξ±] : opens_measurable_space Ξ± := ⟨ge_of_eq $ borel_space.measurable_eq⟩ instance subtype.borel_space {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±] [hΞ± : borel_space Ξ±] (s : set Ξ±) : borel_space s := ⟨by { rw [hΞ±.1, subtype.measurable_space, ← borel_comap], refl }⟩ instance subtype.opens_measurable_space {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±] [h : opens_measurable_space Ξ±] (s : set Ξ±) : opens_measurable_space s := ⟨by { rw [borel_comap], exact comap_mono h.1 }⟩ theorem _root_.measurable_set.induction_on_open [topological_space Ξ±] [measurable_space Ξ±] [borel_space Ξ±] {C : set Ξ± β†’ Prop} (h_open : βˆ€ U, is_open U β†’ C U) (h_compl : βˆ€ t, measurable_set t β†’ C t β†’ C tᢜ) (h_union : βˆ€ f : β„• β†’ set Ξ±, pairwise (disjoint on f) β†’ (βˆ€ i, measurable_set (f i)) β†’ (βˆ€ i, C (f i)) β†’ C (⋃ i, f i)) : βˆ€ ⦃t⦄, measurable_set t β†’ C t := measurable_space.induction_on_inter borel_space.measurable_eq is_pi_system_is_open (h_open _ is_open_empty) h_open h_compl h_union section variables [topological_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±] [topological_space Ξ²] [measurable_space Ξ²] [opens_measurable_space Ξ²] [topological_space Ξ³] [measurable_space Ξ³] [borel_space Ξ³] [topological_space Ξ³β‚‚] [measurable_space Ξ³β‚‚] [borel_space Ξ³β‚‚] [measurable_space Ξ΄] lemma is_open.measurable_set (h : is_open s) : measurable_set s := opens_measurable_space.borel_le _ $ generate_measurable.basic _ h @[measurability] lemma measurable_set_interior : measurable_set (interior s) := is_open_interior.measurable_set lemma is_GΞ΄.measurable_set (h : is_GΞ΄ s) : measurable_set s := begin rcases h with ⟨S, hSo, hSc, rfl⟩, exact measurable_set.sInter hSc (Ξ» t ht, (hSo t ht).measurable_set) end lemma measurable_set_of_continuous_at {Ξ²} [emetric_space Ξ²] (f : Ξ± β†’ Ξ²) : measurable_set {x | continuous_at f x} := (is_GΞ΄_set_of_continuous_at f).measurable_set lemma is_closed.measurable_set (h : is_closed s) : measurable_set s := h.is_open_compl.measurable_set.of_compl lemma is_compact.measurable_set [t2_space Ξ±] (h : is_compact s) : measurable_set s := h.is_closed.measurable_set @[measurability] lemma measurable_set_closure : measurable_set (closure s) := is_closed_closure.measurable_set lemma measurable_of_is_open {f : Ξ΄ β†’ Ξ³} (hf : βˆ€ s, is_open s β†’ measurable_set (f ⁻¹' s)) : measurable f := by { rw [β€Ήborel_space Ξ³β€Ί.measurable_eq], exact measurable_generate_from hf } lemma measurable_of_is_closed {f : Ξ΄ β†’ Ξ³} (hf : βˆ€ s, is_closed s β†’ measurable_set (f ⁻¹' s)) : measurable f := begin apply measurable_of_is_open, intros s hs, rw [← measurable_set.compl_iff, ← preimage_compl], apply hf, rw [is_closed_compl_iff], exact hs end lemma measurable_of_is_closed' {f : Ξ΄ β†’ Ξ³} (hf : βˆ€ s, is_closed s β†’ s.nonempty β†’ s β‰  univ β†’ measurable_set (f ⁻¹' s)) : measurable f := begin apply measurable_of_is_closed, intros s hs, cases eq_empty_or_nonempty s with h1 h1, { simp [h1] }, by_cases h2 : s = univ, { simp [h2] }, exact hf s hs h1 h2 end instance nhds_is_measurably_generated (a : Ξ±) : (𝓝 a).is_measurably_generated := begin rw [nhds, infi_subtype'], refine @filter.infi_is_measurably_generated _ _ _ _ (Ξ» i, _), exact i.2.2.measurable_set.principal_is_measurably_generated end /-- If `s` is a measurable set, then `𝓝[s] a` is a measurably generated filter for each `a`. This cannot be an `instance` because it depends on a non-instance `hs : measurable_set s`. -/ lemma measurable_set.nhds_within_is_measurably_generated {s : set Ξ±} (hs : measurable_set s) (a : Ξ±) : (𝓝[s] a).is_measurably_generated := by haveI := hs.principal_is_measurably_generated; exact filter.inf_is_measurably_generated _ _ @[priority 100] -- see Note [lower instance priority] instance opens_measurable_space.to_measurable_singleton_class [t1_space Ξ±] : measurable_singleton_class Ξ± := ⟨λ x, is_closed_singleton.measurable_set⟩ instance pi.opens_measurable_space_encodable {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} [encodable ΞΉ] [t' : Ξ  i, topological_space (Ο€ i)] [Ξ  i, measurable_space (Ο€ i)] [βˆ€ i, second_countable_topology (Ο€ i)] [βˆ€ i, opens_measurable_space (Ο€ i)] : opens_measurable_space (Ξ  i, Ο€ i) := begin constructor, have : Pi.topological_space = generate_from {t | βˆƒ(s:Ξ a, set (Ο€ a)) (i : finset ΞΉ), (βˆ€a∈i, s a ∈ countable_basis (Ο€ a)) ∧ t = pi ↑i s}, { rw [funext (Ξ» a, @eq_generate_from_countable_basis (Ο€ a) _ _), pi_generate_from_eq] }, rw [borel_eq_generate_from_of_subbasis this], apply generate_from_le, rintros _ ⟨s, i, hi, rfl⟩, refine measurable_set.pi i.countable_to_set (Ξ» a ha, is_open.measurable_set _), rw [eq_generate_from_countable_basis (Ο€ a)], exact generate_open.basic _ (hi a ha) end instance pi.opens_measurable_space_fintype {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} [fintype ΞΉ] [t' : Ξ  i, topological_space (Ο€ i)] [Ξ  i, measurable_space (Ο€ i)] [βˆ€ i, second_countable_topology (Ο€ i)] [βˆ€ i, opens_measurable_space (Ο€ i)] : opens_measurable_space (Ξ  i, Ο€ i) := by { letI := fintype.encodable ΞΉ, apply_instance } instance prod.opens_measurable_space [second_countable_topology Ξ±] [second_countable_topology Ξ²] : opens_measurable_space (Ξ± Γ— Ξ²) := begin constructor, rw [((is_basis_countable_basis Ξ±).prod (is_basis_countable_basis Ξ²)).borel_eq_generate_from], apply generate_from_le, rintros _ ⟨u, v, hu, hv, rfl⟩, exact (is_open_of_mem_countable_basis hu).measurable_set.prod (is_open_of_mem_countable_basis hv).measurable_set end variables {Ξ±' : Type*} [topological_space Ξ±'] [measurable_space Ξ±'] lemma measure_interior_of_null_bdry {ΞΌ : measure Ξ±'} {s : set Ξ±'} (h_nullbdry : ΞΌ (frontier s) = 0) : ΞΌ (interior s) = ΞΌ s := measure_eq_measure_smaller_of_between_null_diff interior_subset subset_closure h_nullbdry lemma measure_closure_of_null_bdry {ΞΌ : measure Ξ±'} {s : set Ξ±'} (h_nullbdry : ΞΌ (frontier s) = 0) : ΞΌ (closure s) = ΞΌ s := (measure_eq_measure_larger_of_between_null_diff interior_subset subset_closure h_nullbdry).symm section preorder variables [preorder Ξ±] [order_closed_topology Ξ±] {a b x : Ξ±} @[simp, measurability] lemma measurable_set_Ici : measurable_set (Ici a) := is_closed_Ici.measurable_set @[simp, measurability] lemma measurable_set_Iic : measurable_set (Iic a) := is_closed_Iic.measurable_set @[simp, measurability] lemma measurable_set_Icc : measurable_set (Icc a b) := is_closed_Icc.measurable_set instance nhds_within_Ici_is_measurably_generated : (𝓝[Ici b] a).is_measurably_generated := measurable_set_Ici.nhds_within_is_measurably_generated _ instance nhds_within_Iic_is_measurably_generated : (𝓝[Iic b] a).is_measurably_generated := measurable_set_Iic.nhds_within_is_measurably_generated _ instance nhds_within_Icc_is_measurably_generated : is_measurably_generated (𝓝[Icc a b] x) := by { rw [← Ici_inter_Iic, nhds_within_inter], apply_instance } instance at_top_is_measurably_generated : (filter.at_top : filter Ξ±).is_measurably_generated := @filter.infi_is_measurably_generated _ _ _ _ $ Ξ» a, (measurable_set_Ici : measurable_set (Ici a)).principal_is_measurably_generated instance at_bot_is_measurably_generated : (filter.at_bot : filter Ξ±).is_measurably_generated := @filter.infi_is_measurably_generated _ _ _ _ $ Ξ» a, (measurable_set_Iic : measurable_set (Iic a)).principal_is_measurably_generated end preorder section partial_order variables [partial_order Ξ±] [order_closed_topology Ξ±] [second_countable_topology Ξ±] {a b : Ξ±} @[measurability] lemma measurable_set_le' : measurable_set {p : Ξ± Γ— Ξ± | p.1 ≀ p.2} := order_closed_topology.is_closed_le'.measurable_set @[measurability] lemma measurable_set_le {f g : Ξ΄ β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable_set {a | f a ≀ g a} := hf.prod_mk hg measurable_set_le' end partial_order section linear_order variables [linear_order Ξ±] [order_closed_topology Ξ±] {a b x : Ξ±} @[simp, measurability] lemma measurable_set_Iio : measurable_set (Iio a) := is_open_Iio.measurable_set @[simp, measurability] lemma measurable_set_Ioi : measurable_set (Ioi a) := is_open_Ioi.measurable_set @[simp, measurability] lemma measurable_set_Ioo : measurable_set (Ioo a b) := is_open_Ioo.measurable_set @[simp, measurability] lemma measurable_set_Ioc : measurable_set (Ioc a b) := measurable_set_Ioi.inter measurable_set_Iic @[simp, measurability] lemma measurable_set_Ico : measurable_set (Ico a b) := measurable_set_Ici.inter measurable_set_Iio instance nhds_within_Ioi_is_measurably_generated : (𝓝[Ioi b] a).is_measurably_generated := measurable_set_Ioi.nhds_within_is_measurably_generated _ instance nhds_within_Iio_is_measurably_generated : (𝓝[Iio b] a).is_measurably_generated := measurable_set_Iio.nhds_within_is_measurably_generated _ instance nhds_within_interval_is_measurably_generated : is_measurably_generated (𝓝[[a, b]] x) := nhds_within_Icc_is_measurably_generated @[measurability] lemma measurable_set_lt' [second_countable_topology Ξ±] : measurable_set {p : Ξ± Γ— Ξ± | p.1 < p.2} := (is_open_lt continuous_fst continuous_snd).measurable_set @[measurability] lemma measurable_set_lt [second_countable_topology Ξ±] {f g : Ξ΄ β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable_set {a | f a < g a} := hf.prod_mk hg measurable_set_lt' lemma set.ord_connected.measurable_set (h : ord_connected s) : measurable_set s := begin let u := ⋃ (x ∈ s) (y ∈ s), Ioo x y, have huopen : is_open u := is_open_bUnion (Ξ» x hx, is_open_bUnion (Ξ» y hy, is_open_Ioo)), have humeas : measurable_set u := huopen.measurable_set, have hfinite : (s \ u).finite, { refine set.finite_of_forall_between_eq_endpoints (s \ u) (Ξ» x hx y hy z hz hxy hyz, _), by_contra' h, exact hy.2 (mem_Unionβ‚‚.mpr ⟨x, hx.1, mem_Unionβ‚‚.mpr ⟨z, hz.1, lt_of_le_of_ne hxy h.1, lt_of_le_of_ne hyz h.2⟩⟩) }, have : u βŠ† s := Unionβ‚‚_subset (Ξ» x hx, Unionβ‚‚_subset (Ξ» y hy, Ioo_subset_Icc_self.trans (h.out hx hy))), rw ← union_diff_cancel this, exact humeas.union hfinite.measurable_set end lemma is_preconnected.measurable_set (h : is_preconnected s) : measurable_set s := h.ord_connected.measurable_set lemma generate_from_Ico_mem_le_borel {Ξ± : Type*} [topological_space Ξ±] [linear_order Ξ±] [order_closed_topology Ξ±] (s t : set Ξ±) : measurable_space.generate_from {S | βˆƒ (l ∈ s) (u ∈ t) (h : l < u), Ico l u = S} ≀ borel Ξ± := begin apply generate_from_le, letI : measurable_space Ξ± := borel Ξ±, haveI : borel_space Ξ± := ⟨rfl⟩, rintro _ ⟨a, -, b, -, -, rfl⟩, exact measurable_set_Ico end lemma dense.borel_eq_generate_from_Ico_mem_aux {Ξ± : Type*} [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±] {s : set Ξ±} (hd : dense s) (hbot : βˆ€ x, is_bot x β†’ x ∈ s) (hIoo : βˆ€ x y : Ξ±, x < y β†’ Ioo x y = βˆ… β†’ y ∈ s) : borel Ξ± = generate_from {S : set Ξ± | βˆƒ (l ∈ s) (u ∈ s) (h : l < u), Ico l u = S} := begin set S : set (set Ξ±) := {S | βˆƒ (l ∈ s) (u ∈ s) (h : l < u), Ico l u = S}, refine le_antisymm _ (generate_from_Ico_mem_le_borel _ _), letI : measurable_space Ξ± := generate_from S, rw borel_eq_generate_from_Iio, refine generate_from_le (forall_range_iff.2 $ Ξ» a, _), rcases hd.exists_countable_dense_subset_bot_top with ⟨t, hts, hc, htd, htb, htt⟩, by_cases ha : βˆ€ b < a, (Ioo b a).nonempty, { convert_to measurable_set (⋃ (l ∈ t) (u ∈ t) (hlu : l < u) (hu : u ≀ a), Ico l u), { ext y, simp only [mem_Union, mem_Iio, mem_Ico], split, { intro hy, rcases htd.exists_le' (Ξ» b hb, htb _ hb (hbot b hb)) y with ⟨l, hlt, hly⟩, rcases htd.exists_mem_open is_open_Ioo (ha y hy) with ⟨u, hut, hyu, hua⟩, exact ⟨l, hlt, u, hut, hly.trans_lt hyu, hua.le, hly, hyu⟩ }, { rintro ⟨l, -, u, -, -, hua, -, hyu⟩, exact hyu.trans_le hua } }, { refine measurable_set.bUnion hc (Ξ» a ha, measurable_set.bUnion hc $ Ξ» b hb, _), refine measurable_set.Union_Prop (Ξ» hab, measurable_set.Union_Prop $ Ξ» hb', _), exact generate_measurable.basic _ ⟨a, hts ha, b, hts hb, hab, mem_singleton _⟩ } }, { simp only [not_forall, not_nonempty_iff_eq_empty] at ha, replace ha : a ∈ s := hIoo ha.some a ha.some_spec.fst ha.some_spec.snd, convert_to measurable_set (⋃ (l ∈ t) (hl : l < a), Ico l a), { symmetry, simp only [← Ici_inter_Iio, ← Union_inter, inter_eq_right_iff_subset, subset_def, mem_Union, mem_Ici, mem_Iio], intros x hx, rcases htd.exists_le' (Ξ» b hb, htb _ hb (hbot b hb)) x with ⟨z, hzt, hzx⟩, exact ⟨z, hzt, hzx.trans_lt hx, hzx⟩ }, { refine measurable_set.bUnion hc (Ξ» x hx, measurable_set.Union_Prop $ Ξ» hlt, _), exact generate_measurable.basic _ ⟨x, hts hx, a, ha, hlt, mem_singleton _⟩ } } end lemma dense.borel_eq_generate_from_Ico_mem {Ξ± : Type*} [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±] [densely_ordered Ξ±] [no_min_order Ξ±] {s : set Ξ±} (hd : dense s) : borel Ξ± = generate_from {S : set Ξ± | βˆƒ (l ∈ s) (u ∈ s) (h : l < u), Ico l u = S} := hd.borel_eq_generate_from_Ico_mem_aux (by simp) $ Ξ» x y hxy H, ((nonempty_Ioo.2 hxy).ne_empty H).elim lemma borel_eq_generate_from_Ico (Ξ± : Type*) [topological_space Ξ±] [second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] : borel Ξ± = generate_from {S : set Ξ± | βˆƒ l u (h : l < u), Ico l u = S} := by simpa only [exists_prop, mem_univ, true_and] using (@dense_univ Ξ± _).borel_eq_generate_from_Ico_mem_aux (Ξ» _ _, mem_univ _) (Ξ» _ _ _ _, mem_univ _) lemma dense.borel_eq_generate_from_Ioc_mem_aux {Ξ± : Type*} [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±] {s : set Ξ±} (hd : dense s) (hbot : βˆ€ x, is_top x β†’ x ∈ s) (hIoo : βˆ€ x y : Ξ±, x < y β†’ Ioo x y = βˆ… β†’ x ∈ s) : borel Ξ± = generate_from {S : set Ξ± | βˆƒ (l ∈ s) (u ∈ s) (h : l < u), Ioc l u = S} := begin convert hd.order_dual.borel_eq_generate_from_Ico_mem_aux hbot (Ξ» x y hlt he, hIoo y x hlt _), { ext s, split; rintro ⟨l, hl, u, hu, hlt, rfl⟩, exacts [⟨u, hu, l, hl, hlt, dual_Ico⟩, ⟨u, hu, l, hl, hlt, dual_Ioc⟩] }, { erw dual_Ioo, exact he } end lemma dense.borel_eq_generate_from_Ioc_mem {Ξ± : Type*} [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±] [densely_ordered Ξ±] [no_max_order Ξ±] {s : set Ξ±} (hd : dense s) : borel Ξ± = generate_from {S : set Ξ± | βˆƒ (l ∈ s) (u ∈ s) (h : l < u), Ioc l u = S} := hd.borel_eq_generate_from_Ioc_mem_aux (by simp) $ Ξ» x y hxy H, ((nonempty_Ioo.2 hxy).ne_empty H).elim lemma borel_eq_generate_from_Ioc (Ξ± : Type*) [topological_space Ξ±] [second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] : borel Ξ± = generate_from {S : set Ξ± | βˆƒ l u (h : l < u), Ioc l u = S} := by simpa only [exists_prop, mem_univ, true_and] using (@dense_univ Ξ± _).borel_eq_generate_from_Ioc_mem_aux (Ξ» _ _, mem_univ _) (Ξ» _ _ _ _, mem_univ _) namespace measure_theory.measure /-- Two finite measures on a Borel space are equal if they agree on all closed-open intervals. If `Ξ±` is a conditionally complete linear order with no top element, `measure_theory.measure..ext_of_Ico` is an extensionality lemma with weaker assumptions on `ΞΌ` and `Ξ½`. -/ lemma ext_of_Ico_finite {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±} [second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±] (ΞΌ Ξ½ : measure Ξ±) [is_finite_measure ΞΌ] (hΞΌΞ½ : ΞΌ univ = Ξ½ univ) (h : βˆ€ ⦃a b⦄, a < b β†’ ΞΌ (Ico a b) = Ξ½ (Ico a b)) : ΞΌ = Ξ½ := begin refine ext_of_generate_finite _ (borel_space.measurable_eq.trans (borel_eq_generate_from_Ico Ξ±)) (is_pi_system_Ico (id : Ξ± β†’ Ξ±) id) _ hΞΌΞ½, { rintro - ⟨a, b, hlt, rfl⟩, exact h hlt } end /-- Two finite measures on a Borel space are equal if they agree on all open-closed intervals. If `Ξ±` is a conditionally complete linear order with no top element, `measure_theory.measure..ext_of_Ioc` is an extensionality lemma with weaker assumptions on `ΞΌ` and `Ξ½`. -/ lemma ext_of_Ioc_finite {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±} [second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±] (ΞΌ Ξ½ : measure Ξ±) [is_finite_measure ΞΌ] (hΞΌΞ½ : ΞΌ univ = Ξ½ univ) (h : βˆ€ ⦃a b⦄, a < b β†’ ΞΌ (Ioc a b) = Ξ½ (Ioc a b)) : ΞΌ = Ξ½ := begin refine @ext_of_Ico_finite (order_dual Ξ±) _ _ _ _ _ β€Ή_β€Ί ΞΌ Ξ½ _ hΞΌΞ½ (Ξ» a b hab, _), erw dual_Ico, exact h hab end /-- Two measures which are finite on closed-open intervals are equal if the agree on all closed-open intervals. -/ lemma ext_of_Ico' {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±} [second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±] [no_max_order Ξ±] (ΞΌ Ξ½ : measure Ξ±) (hΞΌ : βˆ€ ⦃a b⦄, a < b β†’ ΞΌ (Ico a b) β‰  ∞) (h : βˆ€ ⦃a b⦄, a < b β†’ ΞΌ (Ico a b) = Ξ½ (Ico a b)) : ΞΌ = Ξ½ := begin rcases exists_countable_dense_bot_top Ξ± with ⟨s, hsc, hsd, hsb, hst⟩, have : countable (⋃ (l ∈ s) (u ∈ s) (h : l < u), {Ico l u} : set (set Ξ±)), from hsc.bUnion (Ξ» l hl, hsc.bUnion (Ξ» u hu, countable_Union_Prop $ Ξ» _, countable_singleton _)), simp only [← set_of_eq_eq_singleton, ← set_of_exists] at this, refine measure.ext_of_generate_from_of_cover_subset (borel_space.measurable_eq.trans (borel_eq_generate_from_Ico Ξ±)) (is_pi_system_Ico id id) _ this _ _ _, { rintro _ ⟨l, -, u, -, h, rfl⟩, exact ⟨l, u, h, rfl⟩ }, { refine sUnion_eq_univ_iff.2 (Ξ» x, _), rcases hsd.exists_le' hsb x with ⟨l, hls, hlx⟩, rcases hsd.exists_gt x with ⟨u, hus, hxu⟩, exact ⟨_, ⟨l, hls, u, hus, hlx.trans_lt hxu, rfl⟩, hlx, hxu⟩ }, { rintro _ ⟨l, -, u, -, hlt, rfl⟩, exact hΞΌ hlt }, { rintro _ ⟨l, u, hlt, rfl⟩, exact h hlt } end /-- Two measures which are finite on closed-open intervals are equal if the agree on all open-closed intervals. -/ lemma ext_of_Ioc' {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±} [second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±] [no_min_order Ξ±] (ΞΌ Ξ½ : measure Ξ±) (hΞΌ : βˆ€ ⦃a b⦄, a < b β†’ ΞΌ (Ioc a b) β‰  ∞) (h : βˆ€ ⦃a b⦄, a < b β†’ ΞΌ (Ioc a b) = Ξ½ (Ioc a b)) : ΞΌ = Ξ½ := begin refine @ext_of_Ico' (order_dual Ξ±) _ _ _ _ _ β€Ή_β€Ί _ ΞΌ Ξ½ _ _; intros a b hab; erw dual_Ico, exacts [hΞΌ hab, h hab] end /-- Two measures which are finite on closed-open intervals are equal if the agree on all closed-open intervals. -/ lemma ext_of_Ico {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±} [second_countable_topology Ξ±] [conditionally_complete_linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±] [no_max_order Ξ±] (ΞΌ Ξ½ : measure Ξ±) [is_locally_finite_measure ΞΌ] (h : βˆ€ ⦃a b⦄, a < b β†’ ΞΌ (Ico a b) = Ξ½ (Ico a b)) : ΞΌ = Ξ½ := ΞΌ.ext_of_Ico' Ξ½ (Ξ» a b hab, measure_Ico_lt_top.ne) h /-- Two measures which are finite on closed-open intervals are equal if the agree on all open-closed intervals. -/ lemma ext_of_Ioc {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±} [second_countable_topology Ξ±] [conditionally_complete_linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±] [no_min_order Ξ±] (ΞΌ Ξ½ : measure Ξ±) [is_locally_finite_measure ΞΌ] (h : βˆ€ ⦃a b⦄, a < b β†’ ΞΌ (Ioc a b) = Ξ½ (Ioc a b)) : ΞΌ = Ξ½ := ΞΌ.ext_of_Ioc' Ξ½ (Ξ» a b hab, measure_Ioc_lt_top.ne) h /-- Two finite measures on a Borel space are equal if they agree on all left-infinite right-closed intervals. -/ lemma ext_of_Iic {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±} [second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±] (ΞΌ Ξ½ : measure Ξ±) [is_finite_measure ΞΌ] (h : βˆ€ a, ΞΌ (Iic a) = Ξ½ (Iic a)) : ΞΌ = Ξ½ := begin refine ext_of_Ioc_finite ΞΌ Ξ½ _ (Ξ» a b hlt, _), { rcases exists_countable_dense_bot_top Ξ± with ⟨s, hsc, hsd, -, hst⟩, have : directed_on (≀) s, from directed_on_iff_directed.2 (directed_of_sup $ Ξ» _ _, id), simp only [← bsupr_measure_Iic hsc (hsd.exists_ge' hst) this, h] }, rw [← Iic_diff_Iic, measure_diff (Iic_subset_Iic.2 hlt.le) measurable_set_Iic, measure_diff (Iic_subset_Iic.2 hlt.le) measurable_set_Iic, h a, h b], { rw ← h a, exact (measure_lt_top ΞΌ _).ne }, { exact (measure_lt_top ΞΌ _).ne } end /-- Two finite measures on a Borel space are equal if they agree on all left-closed right-infinite intervals. -/ lemma ext_of_Ici {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±} [second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±] (ΞΌ Ξ½ : measure Ξ±) [is_finite_measure ΞΌ] (h : βˆ€ a, ΞΌ (Ici a) = Ξ½ (Ici a)) : ΞΌ = Ξ½ := @ext_of_Iic (order_dual Ξ±) _ _ _ _ _ β€Ή_β€Ί _ _ _ h end measure_theory.measure end linear_order section linear_order variables [linear_order Ξ±] [order_closed_topology Ξ±] @[measurability] lemma measurable_set_interval {a b : Ξ±} : measurable_set (interval a b) := measurable_set_Icc @[measurability] lemma measurable_set_interval_oc {a b : Ξ±} : measurable_set (interval_oc a b) := measurable_set_Ioc variables [second_countable_topology Ξ±] @[measurability] lemma measurable.max {f g : Ξ΄ β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» a, max (f a) (g a)) := by simpa only [max_def] using hf.piecewise (measurable_set_le hg hf) hg @[measurability] lemma ae_measurable.max {f g : Ξ΄ β†’ Ξ±} {ΞΌ : measure Ξ΄} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, max (f a) (g a)) ΞΌ := ⟨λ a, max (hf.mk f a) (hg.mk g a), hf.measurable_mk.max hg.measurable_mk, eventually_eq.compβ‚‚ hf.ae_eq_mk _ hg.ae_eq_mk⟩ @[measurability] lemma measurable.min {f g : Ξ΄ β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» a, min (f a) (g a)) := by simpa only [min_def] using hf.piecewise (measurable_set_le hf hg) hg @[measurability] lemma ae_measurable.min {f g : Ξ΄ β†’ Ξ±} {ΞΌ : measure Ξ΄} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, min (f a) (g a)) ΞΌ := ⟨λ a, min (hf.mk f a) (hg.mk g a), hf.measurable_mk.min hg.measurable_mk, eventually_eq.compβ‚‚ hf.ae_eq_mk _ hg.ae_eq_mk⟩ end linear_order /-- A continuous function from an `opens_measurable_space` to a `borel_space` is measurable. -/ lemma continuous.measurable {f : Ξ± β†’ Ξ³} (hf : continuous f) : measurable f := hf.borel_measurable.mono opens_measurable_space.borel_le (le_of_eq $ borel_space.measurable_eq) /-- A continuous function from an `opens_measurable_space` to a `borel_space` is ae-measurable. -/ lemma continuous.ae_measurable {f : Ξ± β†’ Ξ³} (h : continuous f) (ΞΌ : measure Ξ±) : ae_measurable f ΞΌ := h.measurable.ae_measurable lemma closed_embedding.measurable {f : Ξ± β†’ Ξ³} (hf : closed_embedding f) : measurable f := hf.continuous.measurable lemma continuous.is_open_pos_measure_map {f : Ξ² β†’ Ξ³} (hf : continuous f) (hf_surj : function.surjective f) {ΞΌ : measure Ξ²} [ΞΌ.is_open_pos_measure] : (measure.map f ΞΌ).is_open_pos_measure := begin refine ⟨λ U hUo hUne, _⟩, rw [measure.map_apply hf.measurable hUo.measurable_set], exact (hUo.preimage hf).measure_ne_zero ΞΌ (hf_surj.nonempty_preimage.mpr hUne) end @[priority 100, to_additive] instance has_continuous_mul.has_measurable_mul [has_mul Ξ³] [has_continuous_mul Ξ³] : has_measurable_mul Ξ³ := { measurable_const_mul := Ξ» c, (continuous_const.mul continuous_id).measurable, measurable_mul_const := Ξ» c, (continuous_id.mul continuous_const).measurable } @[priority 100] instance has_continuous_sub.has_measurable_sub [has_sub Ξ³] [has_continuous_sub Ξ³] : has_measurable_sub Ξ³ := { measurable_const_sub := Ξ» c, (continuous_const.sub continuous_id).measurable, measurable_sub_const := Ξ» c, (continuous_id.sub continuous_const).measurable } @[priority 100, to_additive] instance topological_group.has_measurable_inv [group Ξ³] [topological_group Ξ³] : has_measurable_inv Ξ³ := ⟨continuous_inv.measurable⟩ @[priority 100] instance has_continuous_smul.has_measurable_smul {M Ξ±} [topological_space M] [topological_space Ξ±] [measurable_space M] [measurable_space Ξ±] [opens_measurable_space M] [borel_space Ξ±] [has_scalar M Ξ±] [has_continuous_smul M Ξ±] : has_measurable_smul M Ξ± := ⟨λ c, (continuous_const.smul continuous_id).measurable, Ξ» y, (continuous_id.smul continuous_const).measurable⟩ section lattice @[priority 100] instance has_continuous_sup.has_measurable_sup [has_sup Ξ³] [has_continuous_sup Ξ³] : has_measurable_sup Ξ³ := { measurable_const_sup := Ξ» c, (continuous_const.sup continuous_id).measurable, measurable_sup_const := Ξ» c, (continuous_id.sup continuous_const).measurable } @[priority 100] instance has_continuous_sup.has_measurable_supβ‚‚ [second_countable_topology Ξ³] [has_sup Ξ³] [has_continuous_sup Ξ³] : has_measurable_supβ‚‚ Ξ³ := ⟨continuous_sup.measurable⟩ @[priority 100] instance has_continuous_inf.has_measurable_inf [has_inf Ξ³] [has_continuous_inf Ξ³] : has_measurable_inf Ξ³ := { measurable_const_inf := Ξ» c, (continuous_const.inf continuous_id).measurable, measurable_inf_const := Ξ» c, (continuous_id.inf continuous_const).measurable } @[priority 100] instance has_continuous_inf.has_measurable_infβ‚‚ [second_countable_topology Ξ³] [has_inf Ξ³] [has_continuous_inf Ξ³] : has_measurable_infβ‚‚ Ξ³ := ⟨continuous_inf.measurable⟩ end lattice section homeomorph @[measurability] protected lemma homeomorph.measurable (h : Ξ± β‰ƒβ‚œ Ξ³) : measurable h := h.continuous.measurable /-- A homeomorphism between two Borel spaces is a measurable equivalence.-/ def homeomorph.to_measurable_equiv (h : Ξ³ β‰ƒβ‚œ Ξ³β‚‚) : Ξ³ ≃ᡐ Ξ³β‚‚ := { measurable_to_fun := h.measurable, measurable_inv_fun := h.symm.measurable, to_equiv := h.to_equiv } @[simp] lemma homeomorph.to_measurable_equiv_coe (h : Ξ³ β‰ƒβ‚œ Ξ³β‚‚) : (h.to_measurable_equiv : Ξ³ β†’ Ξ³β‚‚) = h := rfl @[simp] lemma homeomorph.to_measurable_equiv_symm_coe (h : Ξ³ β‰ƒβ‚œ Ξ³β‚‚) : (h.to_measurable_equiv.symm : Ξ³β‚‚ β†’ Ξ³) = h.symm := rfl end homeomorph @[measurability] lemma continuous_map.measurable (f : C(Ξ±, Ξ³)) : measurable f := f.continuous.measurable lemma measurable_of_continuous_on_compl_singleton [t1_space Ξ±] {f : Ξ± β†’ Ξ³} (a : Ξ±) (hf : continuous_on f {a}ᢜ) : measurable f := measurable_of_measurable_on_compl_singleton a (continuous_on_iff_continuous_restrict.1 hf).measurable lemma continuous.measurable2 [second_countable_topology Ξ±] [second_countable_topology Ξ²] {f : Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ²} {c : Ξ± β†’ Ξ² β†’ Ξ³} (h : continuous (Ξ» p : Ξ± Γ— Ξ², c p.1 p.2)) (hf : measurable f) (hg : measurable g) : measurable (Ξ» a, c (f a) (g a)) := h.measurable.comp (hf.prod_mk hg) lemma continuous.ae_measurable2 [second_countable_topology Ξ±] [second_countable_topology Ξ²] {f : Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ²} {c : Ξ± β†’ Ξ² β†’ Ξ³} {ΞΌ : measure Ξ΄} (h : continuous (Ξ» p : Ξ± Γ— Ξ², c p.1 p.2)) (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, c (f a) (g a)) ΞΌ := h.measurable.comp_ae_measurable (hf.prod_mk hg) @[priority 100] instance has_continuous_invβ‚€.has_measurable_inv [group_with_zero Ξ³] [t1_space Ξ³] [has_continuous_invβ‚€ Ξ³] : has_measurable_inv Ξ³ := ⟨measurable_of_continuous_on_compl_singleton 0 continuous_on_invβ‚€βŸ© @[priority 100, to_additive] instance has_continuous_mul.has_measurable_mulβ‚‚ [second_countable_topology Ξ³] [has_mul Ξ³] [has_continuous_mul Ξ³] : has_measurable_mulβ‚‚ Ξ³ := ⟨continuous_mul.measurable⟩ @[priority 100] instance has_continuous_sub.has_measurable_subβ‚‚ [second_countable_topology Ξ³] [has_sub Ξ³] [has_continuous_sub Ξ³] : has_measurable_subβ‚‚ Ξ³ := ⟨continuous_sub.measurable⟩ @[priority 100] instance has_continuous_smul.has_measurable_smulβ‚‚ {M Ξ±} [topological_space M] [second_countable_topology M] [measurable_space M] [opens_measurable_space M] [topological_space Ξ±] [second_countable_topology Ξ±] [measurable_space Ξ±] [borel_space Ξ±] [has_scalar M Ξ±] [has_continuous_smul M Ξ±] : has_measurable_smulβ‚‚ M Ξ± := ⟨continuous_smul.measurable⟩ end section borel_space variables [topological_space Ξ±] [measurable_space Ξ±] [borel_space Ξ±] [topological_space Ξ²] [measurable_space Ξ²] [borel_space Ξ²] [topological_space Ξ³] [measurable_space Ξ³] [borel_space Ξ³] [measurable_space Ξ΄] lemma pi_le_borel_pi {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} [Ξ  i, topological_space (Ο€ i)] [Ξ  i, measurable_space (Ο€ i)] [βˆ€ i, borel_space (Ο€ i)] : measurable_space.pi ≀ borel (Ξ  i, Ο€ i) := begin have : β€ΉΞ  i, measurable_space (Ο€ i)β€Ί = Ξ» i, borel (Ο€ i) := funext (Ξ» i, borel_space.measurable_eq), rw [this], exact supr_le (Ξ» i, comap_le_iff_le_map.2 $ (continuous_apply i).borel_measurable) end lemma prod_le_borel_prod : prod.measurable_space ≀ borel (Ξ± Γ— Ξ²) := begin rw [β€Ήborel_space Ξ±β€Ί.measurable_eq, β€Ήborel_space Ξ²β€Ί.measurable_eq], refine sup_le _ _, { exact comap_le_iff_le_map.mpr continuous_fst.borel_measurable }, { exact comap_le_iff_le_map.mpr continuous_snd.borel_measurable } end instance pi.borel_space_fintype_encodable {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} [encodable ΞΉ] [t' : Ξ  i, topological_space (Ο€ i)] [Ξ  i, measurable_space (Ο€ i)] [βˆ€ i, second_countable_topology (Ο€ i)] [βˆ€ i, borel_space (Ο€ i)] : borel_space (Ξ  i, Ο€ i) := ⟨le_antisymm pi_le_borel_pi opens_measurable_space.borel_le⟩ instance pi.borel_space_fintype {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} [fintype ΞΉ] [t' : Ξ  i, topological_space (Ο€ i)] [Ξ  i, measurable_space (Ο€ i)] [βˆ€ i, second_countable_topology (Ο€ i)] [βˆ€ i, borel_space (Ο€ i)] : borel_space (Ξ  i, Ο€ i) := ⟨le_antisymm pi_le_borel_pi opens_measurable_space.borel_le⟩ instance prod.borel_space [second_countable_topology Ξ±] [second_countable_topology Ξ²] : borel_space (Ξ± Γ— Ξ²) := ⟨le_antisymm prod_le_borel_prod opens_measurable_space.borel_le⟩ protected lemma embedding.measurable_embedding {f : Ξ± β†’ Ξ²} (h₁ : embedding f) (hβ‚‚ : measurable_set (range f)) : measurable_embedding f := show measurable_embedding (coe ∘ (homeomorph.of_embedding f h₁).to_measurable_equiv), from (measurable_embedding.subtype_coe hβ‚‚).comp (measurable_equiv.measurable_embedding _) protected lemma closed_embedding.measurable_embedding {f : Ξ± β†’ Ξ²} (h : closed_embedding f) : measurable_embedding f := h.to_embedding.measurable_embedding h.closed_range.measurable_set protected lemma open_embedding.measurable_embedding {f : Ξ± β†’ Ξ²} (h : open_embedding f) : measurable_embedding f := h.to_embedding.measurable_embedding h.open_range.measurable_set section linear_order variables [linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±] lemma measurable_of_Iio {f : Ξ΄ β†’ Ξ±} (hf : βˆ€ x, measurable_set (f ⁻¹' Iio x)) : measurable f := begin convert measurable_generate_from _, exact borel_space.measurable_eq.trans (borel_eq_generate_from_Iio _), rintro _ ⟨x, rfl⟩, exact hf x end lemma upper_semicontinuous.measurable [topological_space Ξ΄] [opens_measurable_space Ξ΄] {f : Ξ΄ β†’ Ξ±} (hf : upper_semicontinuous f) : measurable f := measurable_of_Iio (Ξ» y, (hf.is_open_preimage y).measurable_set) lemma measurable_of_Ioi {f : Ξ΄ β†’ Ξ±} (hf : βˆ€ x, measurable_set (f ⁻¹' Ioi x)) : measurable f := begin convert measurable_generate_from _, exact borel_space.measurable_eq.trans (borel_eq_generate_from_Ioi _), rintro _ ⟨x, rfl⟩, exact hf x end lemma lower_semicontinuous.measurable [topological_space Ξ΄] [opens_measurable_space Ξ΄] {f : Ξ΄ β†’ Ξ±} (hf : lower_semicontinuous f) : measurable f := measurable_of_Ioi (Ξ» y, (hf.is_open_preimage y).measurable_set) lemma measurable_of_Iic {f : Ξ΄ β†’ Ξ±} (hf : βˆ€ x, measurable_set (f ⁻¹' Iic x)) : measurable f := begin apply measurable_of_Ioi, simp_rw [← compl_Iic, preimage_compl, measurable_set.compl_iff], assumption end lemma measurable_of_Ici {f : Ξ΄ β†’ Ξ±} (hf : βˆ€ x, measurable_set (f ⁻¹' Ici x)) : measurable f := begin apply measurable_of_Iio, simp_rw [← compl_Ici, preimage_compl, measurable_set.compl_iff], assumption end lemma measurable.is_lub {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) (hg : βˆ€ b, is_lub {a | βˆƒ i, f i b = a} (g b)) : measurable g := begin change βˆ€ b, is_lub (range $ Ξ» i, f i b) (g b) at hg, rw [β€Ήborel_space Ξ±β€Ί.measurable_eq, borel_eq_generate_from_Ioi Ξ±], apply measurable_generate_from, rintro _ ⟨a, rfl⟩, simp_rw [set.preimage, mem_Ioi, lt_is_lub_iff (hg _), exists_range_iff, set_of_exists], exact measurable_set.Union (Ξ» i, hf i (is_open_lt' _).measurable_set) end private lemma ae_measurable.is_lub_of_nonempty {ΞΉ} (hΞΉ : nonempty ΞΉ) {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) (hg : βˆ€α΅ b βˆ‚ΞΌ, is_lub {a | βˆƒ i, f i b = a} (g b)) : ae_measurable g ΞΌ := begin let p : Ξ΄ β†’ (ΞΉ β†’ Ξ±) β†’ Prop := Ξ» x f', is_lub {a | βˆƒ i, f' i = a} (g x), let g_seq := Ξ» x, ite (x ∈ ae_seq_set hf p) (g x) (⟨g x⟩ : nonempty Ξ±).some, have hg_seq : βˆ€ b, is_lub {a | βˆƒ i, ae_seq hf p i b = a} (g_seq b), { intro b, haveI hΞ± : nonempty Ξ± := nonempty.map g ⟨b⟩, simp only [ae_seq, g_seq], split_ifs, { have h_set_eq : {a : Ξ± | βˆƒ (i : ΞΉ), (hf i).mk (f i) b = a} = {a : Ξ± | βˆƒ (i : ΞΉ), f i b = a}, { ext x, simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], }, rw h_set_eq, exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, }, { have h_singleton : {a : Ξ± | βˆƒ (i : ΞΉ), hΞ±.some = a} = {hΞ±.some}, { ext1 x, exact ⟨λ hx, hx.some_spec.symm, Ξ» hx, ⟨hΞΉ.some, hx.symm⟩⟩, }, rw h_singleton, exact is_lub_singleton, }, }, refine ⟨g_seq, measurable.is_lub (ae_seq.measurable hf p) hg_seq, _⟩, exact (ite_ae_eq_of_measure_compl_zero g (Ξ» x, (⟨g x⟩ : nonempty Ξ±).some) (ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm, end lemma ae_measurable.is_lub {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) (hg : βˆ€α΅ b βˆ‚ΞΌ, is_lub {a | βˆƒ i, f i b = a} (g b)) : ae_measurable g ΞΌ := begin by_cases hΞΌ : ΞΌ = 0, { rw hΞΌ, exact ae_measurable_zero_measure }, haveI : ΞΌ.ae.ne_bot, { simpa [ne_bot_iff] }, by_cases hΞΉ : nonempty ΞΉ, { exact ae_measurable.is_lub_of_nonempty hΞΉ hf hg, }, suffices : βˆƒ x, g =ᡐ[ΞΌ] Ξ» y, g x, by { exact ⟨(Ξ» y, g this.some), measurable_const, this.some_spec⟩, }, have h_empty : βˆ€ x, {a : Ξ± | βˆƒ (i : ΞΉ), f i x = a} = βˆ…, { intro x, ext1 y, rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false], exact Ξ» hi, hΞΉ (nonempty_of_exists hi), }, simp_rw h_empty at hg, exact ⟨hg.exists.some, hg.mono (Ξ» y hy, is_lub.unique hy hg.exists.some_spec)⟩, end lemma measurable.is_glb {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) (hg : βˆ€ b, is_glb {a | βˆƒ i, f i b = a} (g b)) : measurable g := begin change βˆ€ b, is_glb (range $ Ξ» i, f i b) (g b) at hg, rw [β€Ήborel_space Ξ±β€Ί.measurable_eq, borel_eq_generate_from_Iio Ξ±], apply measurable_generate_from, rintro _ ⟨a, rfl⟩, simp_rw [set.preimage, mem_Iio, is_glb_lt_iff (hg _), exists_range_iff, set_of_exists], exact measurable_set.Union (Ξ» i, hf i (is_open_gt' _).measurable_set) end private lemma ae_measurable.is_glb_of_nonempty {ΞΉ} (hΞΉ : nonempty ΞΉ) {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) (hg : βˆ€α΅ b βˆ‚ΞΌ, is_glb {a | βˆƒ i, f i b = a} (g b)) : ae_measurable g ΞΌ := begin let p : Ξ΄ β†’ (ΞΉ β†’ Ξ±) β†’ Prop := Ξ» x f', is_glb {a | βˆƒ i, f' i = a} (g x), let g_seq := Ξ» x, ite (x ∈ ae_seq_set hf p) (g x) (⟨g x⟩ : nonempty Ξ±).some, have hg_seq : βˆ€ b, is_glb {a | βˆƒ i, ae_seq hf p i b = a} (g_seq b), { intro b, haveI hΞ± : nonempty Ξ± := nonempty.map g ⟨b⟩, simp only [ae_seq, g_seq], split_ifs, { have h_set_eq : {a : Ξ± | βˆƒ (i : ΞΉ), (hf i).mk (f i) b = a} = {a : Ξ± | βˆƒ (i : ΞΉ), f i b = a}, { ext x, simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], }, rw h_set_eq, exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, }, { have h_singleton : {a : Ξ± | βˆƒ (i : ΞΉ), hΞ±.some = a} = {hΞ±.some}, { ext1 x, exact ⟨λ hx, hx.some_spec.symm, Ξ» hx, ⟨hΞΉ.some, hx.symm⟩⟩, }, rw h_singleton, exact is_glb_singleton, }, }, refine ⟨g_seq, measurable.is_glb (ae_seq.measurable hf p) hg_seq, _⟩, exact (ite_ae_eq_of_measure_compl_zero g (Ξ» x, (⟨g x⟩ : nonempty Ξ±).some) (ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm, end lemma ae_measurable.is_glb {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) (hg : βˆ€α΅ b βˆ‚ΞΌ, is_glb {a | βˆƒ i, f i b = a} (g b)) : ae_measurable g ΞΌ := begin by_cases hΞΌ : ΞΌ = 0, { rw hΞΌ, exact ae_measurable_zero_measure }, haveI : ΞΌ.ae.ne_bot, { simpa [ne_bot_iff] }, by_cases hΞΉ : nonempty ΞΉ, { exact ae_measurable.is_glb_of_nonempty hΞΉ hf hg, }, suffices : βˆƒ x, g =ᡐ[ΞΌ] Ξ» y, g x, by { exact ⟨(Ξ» y, g this.some), measurable_const, this.some_spec⟩, }, have h_empty : βˆ€ x, {a : Ξ± | βˆƒ (i : ΞΉ), f i x = a} = βˆ…, { intro x, ext1 y, rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false], exact Ξ» hi, hΞΉ (nonempty_of_exists hi), }, simp_rw h_empty at hg, exact ⟨hg.exists.some, hg.mono (Ξ» y hy, is_glb.unique hy hg.exists.some_spec)⟩, end protected lemma monotone.measurable [linear_order Ξ²] [order_closed_topology Ξ²] {f : Ξ² β†’ Ξ±} (hf : monotone f) : measurable f := suffices h : βˆ€ x, ord_connected (f ⁻¹' Ioi x), from measurable_of_Ioi (Ξ» x, (h x).measurable_set), Ξ» x, ord_connected_def.mpr (Ξ» a ha b hb c hc, lt_of_lt_of_le ha (hf hc.1)) lemma ae_measurable_restrict_of_monotone_on [linear_order Ξ²] [order_closed_topology Ξ²] {ΞΌ : measure Ξ²} {s : set Ξ²} (hs : measurable_set s) {f : Ξ² β†’ Ξ±} (hf : monotone_on f s) : ae_measurable f (ΞΌ.restrict s) := have this : monotone (f ∘ coe : s β†’ Ξ±), from Ξ» ⟨x, hx⟩ ⟨y, hy⟩ (hxy : x ≀ y), hf hx hy hxy, ae_measurable_restrict_of_measurable_subtype hs this.measurable protected lemma antitone.measurable [linear_order Ξ²] [order_closed_topology Ξ²] {f : Ξ² β†’ Ξ±} (hf : antitone f) : measurable f := @monotone.measurable (order_dual Ξ±) Ξ² _ _ β€Ή_β€Ί _ _ _ _ _ β€Ή_β€Ί _ _ _ hf lemma ae_measurable_restrict_of_antitone_on [linear_order Ξ²] [order_closed_topology Ξ²] {ΞΌ : measure Ξ²} {s : set Ξ²} (hs : measurable_set s) {f : Ξ² β†’ Ξ±} (hf : antitone_on f s) : ae_measurable f (ΞΌ.restrict s) := @ae_measurable_restrict_of_monotone_on (order_dual Ξ±) Ξ² _ _ β€Ή_β€Ί _ _ _ _ _ β€Ή_β€Ί _ _ _ _ hs _ hf end linear_order @[measurability] lemma measurable.supr_Prop {Ξ±} [measurable_space Ξ±] [complete_lattice Ξ±] (p : Prop) {f : Ξ΄ β†’ Ξ±} (hf : measurable f) : measurable (Ξ» b, ⨆ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact supr_pos h end) (assume h : Β¬p, begin convert measurable_const, funext, exact supr_neg h end) @[measurability] lemma measurable.infi_Prop {Ξ±} [measurable_space Ξ±] [complete_lattice Ξ±] (p : Prop) {f : Ξ΄ β†’ Ξ±} (hf : measurable f) : measurable (Ξ» b, β¨… h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact infi_pos h end ) (assume h : Β¬p, begin convert measurable_const, funext, exact infi_neg h end) section complete_linear_order variables [complete_linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±] @[measurability] lemma measurable_supr {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» b, ⨆ i, f i b) := measurable.is_lub hf $ Ξ» b, is_lub_supr @[measurability] lemma ae_measurable_supr {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» b, ⨆ i, f i b) ΞΌ := ae_measurable.is_lub hf $ (ae_of_all ΞΌ (Ξ» b, is_lub_supr)) @[measurability] lemma measurable_infi {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» b, β¨… i, f i b) := measurable.is_glb hf $ Ξ» b, is_glb_infi @[measurability] lemma ae_measurable_infi {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» b, β¨… i, f i b) ΞΌ := ae_measurable.is_glb hf $ (ae_of_all ΞΌ (Ξ» b, is_glb_infi)) lemma measurable_bsupr {ΞΉ} (s : set ΞΉ) {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hs : countable s) (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» b, ⨆ i ∈ s, f i b) := by { haveI : encodable s := hs.to_encodable, simp only [supr_subtype'], exact measurable_supr (Ξ» i, hf i) } lemma ae_measurable_bsupr {ΞΉ} {ΞΌ : measure Ξ΄} (s : set ΞΉ) {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hs : countable s) (hf : βˆ€ i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» b, ⨆ i ∈ s, f i b) ΞΌ := begin haveI : encodable s := hs.to_encodable, simp only [supr_subtype'], exact ae_measurable_supr (Ξ» i, hf i), end lemma measurable_binfi {ΞΉ} (s : set ΞΉ) {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hs : countable s) (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» b, β¨… i ∈ s, f i b) := by { haveI : encodable s := hs.to_encodable, simp only [infi_subtype'], exact measurable_infi (Ξ» i, hf i) } lemma ae_measurable_binfi {ΞΉ} {ΞΌ : measure Ξ΄} (s : set ΞΉ) {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hs : countable s) (hf : βˆ€ i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» b, β¨… i ∈ s, f i b) ΞΌ := begin haveI : encodable s := hs.to_encodable, simp only [infi_subtype'], exact ae_measurable_infi (Ξ» i, hf i), end /-- `liminf` over a general filter is measurable. See `measurable_liminf` for the version over `β„•`. -/ lemma measurable_liminf' {ΞΉ ΞΉ'} {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {u : filter ΞΉ} (hf : βˆ€ i, measurable (f i)) {p : ΞΉ' β†’ Prop} {s : ΞΉ' β†’ set ΞΉ} (hu : u.has_countable_basis p s) (hs : βˆ€ i, (s i).countable) : measurable (Ξ» x, liminf u (Ξ» i, f i x)) := begin simp_rw [hu.to_has_basis.liminf_eq_supr_infi], refine measurable_bsupr _ hu.countable _, exact Ξ» i, measurable_binfi _ (hs i) hf end /-- `limsup` over a general filter is measurable. See `measurable_limsup` for the version over `β„•`. -/ lemma measurable_limsup' {ΞΉ ΞΉ'} {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {u : filter ΞΉ} (hf : βˆ€ i, measurable (f i)) {p : ΞΉ' β†’ Prop} {s : ΞΉ' β†’ set ΞΉ} (hu : u.has_countable_basis p s) (hs : βˆ€ i, (s i).countable) : measurable (Ξ» x, limsup u (Ξ» i, f i x)) := begin simp_rw [hu.to_has_basis.limsup_eq_infi_supr], refine measurable_binfi _ hu.countable _, exact Ξ» i, measurable_bsupr _ (hs i) hf end /-- `liminf` over `β„•` is measurable. See `measurable_liminf'` for a version with a general filter. -/ @[measurability] lemma measurable_liminf {f : β„• β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» x, liminf at_top (Ξ» i, f i x)) := measurable_liminf' hf at_top_countable_basis (Ξ» i, countable_encodable _) /-- `limsup` over `β„•` is measurable. See `measurable_limsup'` for a version with a general filter. -/ @[measurability] lemma measurable_limsup {f : β„• β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» x, limsup at_top (Ξ» i, f i x)) := measurable_limsup' hf at_top_countable_basis (Ξ» i, countable_encodable _) end complete_linear_order section conditionally_complete_linear_order variables [conditionally_complete_linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±] lemma measurable_cSup {ΞΉ} {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {s : set ΞΉ} (hs : s.countable) (hf : βˆ€ i, measurable (f i)) (bdd : βˆ€ x, bdd_above ((Ξ» i, f i x) '' s)) : measurable (Ξ» x, Sup ((Ξ» i, f i x) '' s)) := begin cases eq_empty_or_nonempty s with h2s h2s, { simp [h2s, measurable_const] }, { apply measurable_of_Iic, intro y, simp_rw [preimage, mem_Iic, cSup_le_iff (bdd _) (h2s.image _), ball_image_iff, set_of_forall], exact measurable_set.bInter hs (Ξ» i hi, measurable_set_le (hf i) measurable_const) } end end conditionally_complete_linear_order /-- Convert a `homeomorph` to a `measurable_equiv`. -/ def homemorph.to_measurable_equiv (h : Ξ± β‰ƒβ‚œ Ξ²) : Ξ± ≃ᡐ Ξ² := { to_equiv := h.to_equiv, measurable_to_fun := h.continuous_to_fun.measurable, measurable_inv_fun := h.continuous_inv_fun.measurable } protected lemma is_finite_measure_on_compacts.map {Ξ± : Type*} {m0 : measurable_space Ξ±} [topological_space Ξ±] [opens_measurable_space Ξ±] {Ξ² : Type*} [measurable_space Ξ²] [topological_space Ξ²] [borel_space Ξ²] [t2_space Ξ²] (ΞΌ : measure Ξ±) [is_finite_measure_on_compacts ΞΌ] (f : Ξ± β‰ƒβ‚œ Ξ²) : is_finite_measure_on_compacts (measure.map f ΞΌ) := ⟨begin assume K hK, rw [measure.map_apply f.measurable hK.measurable_set], apply is_compact.measure_lt_top, rwa f.compact_preimage end⟩ end borel_space instance empty.borel_space : borel_space empty := ⟨borel_eq_top_of_discrete.symm⟩ instance unit.borel_space : borel_space unit := ⟨borel_eq_top_of_discrete.symm⟩ instance bool.borel_space : borel_space bool := ⟨borel_eq_top_of_discrete.symm⟩ instance nat.borel_space : borel_space β„• := ⟨borel_eq_top_of_discrete.symm⟩ instance int.borel_space : borel_space β„€ := ⟨borel_eq_top_of_discrete.symm⟩ instance rat.borel_space : borel_space β„š := ⟨borel_eq_top_of_encodable.symm⟩ @[priority 900] instance is_R_or_C.measurable_space {π•œ : Type*} [is_R_or_C π•œ] : measurable_space π•œ := borel π•œ @[priority 900] instance is_R_or_C.borel_space {π•œ : Type*} [is_R_or_C π•œ] : borel_space π•œ := ⟨rfl⟩ /- Instances on `real` and `complex` are special cases of `is_R_or_C` but without these instances, Lean fails to prove `borel_space (ΞΉ β†’ ℝ)`, so we leave them here. -/ instance real.measurable_space : measurable_space ℝ := borel ℝ instance real.borel_space : borel_space ℝ := ⟨rfl⟩ instance nnreal.measurable_space : measurable_space ℝβ‰₯0 := subtype.measurable_space instance nnreal.borel_space : borel_space ℝβ‰₯0 := subtype.borel_space _ instance ennreal.measurable_space : measurable_space ℝβ‰₯0∞ := borel ℝβ‰₯0∞ instance ennreal.borel_space : borel_space ℝβ‰₯0∞ := ⟨rfl⟩ instance ereal.measurable_space : measurable_space ereal := borel ereal instance ereal.borel_space : borel_space ereal := ⟨rfl⟩ instance complex.measurable_space : measurable_space β„‚ := borel β„‚ instance complex.borel_space : borel_space β„‚ := ⟨rfl⟩ /-- One can cut out `ℝβ‰₯0∞` into the sets `{0}`, `Ico (t^n) (t^(n+1))` for `n : β„€` and `{∞}`. This gives a way to compute the measure of a set in terms of sets on which a given function `f` does not fluctuate by more than `t`. -/ lemma measure_eq_measure_preimage_add_measure_tsum_Ico_zpow [measurable_space Ξ±] (ΞΌ : measure Ξ±) {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) {s : set Ξ±} (hs : measurable_set s) {t : ℝβ‰₯0} (ht : 1 < t) : ΞΌ s = ΞΌ (s ∩ f⁻¹' {0}) + ΞΌ (s ∩ f⁻¹' {∞}) + βˆ‘' (n : β„€), ΞΌ (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) := begin have A : ΞΌ s = ΞΌ (s ∩ f⁻¹' {0}) + ΞΌ (s ∩ f⁻¹' (Ioi 0)), { rw ← measure_union, { congr' 1, ext x, have : 0 = f x ∨ 0 < f x := eq_or_lt_of_le bot_le, rw eq_comm at this, simp only [←and_or_distrib_left, this, mem_singleton_iff, mem_inter_eq, and_true, mem_union_eq, mem_Ioi, mem_preimage], }, { apply disjoint_left.2 (Ξ» x hx h'x, _), have : 0 < f x := h'x.2, exact lt_irrefl 0 (this.trans_le hx.2.le) }, { exact hs.inter (hf measurable_set_Ioi) } }, have B : ΞΌ (s ∩ f⁻¹' (Ioi 0)) = ΞΌ (s ∩ f⁻¹' {∞}) + ΞΌ (s ∩ f⁻¹' (Ioo 0 ∞)), { rw ← measure_union, { rw ← inter_union_distrib_left, congr, ext x, simp only [mem_singleton_iff, mem_union_eq, mem_Ioo, mem_Ioi, mem_preimage], have H : f x = ∞ ∨ f x < ∞ := eq_or_lt_of_le le_top, cases H, { simp only [H, eq_self_iff_true, or_false, with_top.zero_lt_top, not_top_lt, and_false] }, { simp only [H, H.ne, and_true, false_or] } }, { apply disjoint_left.2 (Ξ» x hx h'x, _), have : f x < ∞ := h'x.2.2, exact lt_irrefl _ (this.trans_le (le_of_eq hx.2.symm)) }, { exact hs.inter (hf measurable_set_Ioo) } }, have C : ΞΌ (s ∩ f⁻¹' (Ioo 0 ∞)) = βˆ‘' (n : β„€), ΞΌ (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))), { rw [← measure_Union, ennreal.Ioo_zero_top_eq_Union_Ico_zpow (ennreal.one_lt_coe_iff.2 ht) ennreal.coe_ne_top, preimage_Union, inter_Union], { assume i j, simp only [function.on_fun], wlog h : i ≀ j := le_total i j using [i j, j i] tactic.skip, { assume hij, replace hij : i + 1 ≀ j := lt_of_le_of_ne h hij, apply disjoint_left.2 (Ξ» x hx h'x, lt_irrefl (f x) _), calc f x < t ^ (i + 1) : hx.2.2 ... ≀ t ^ j : ennreal.zpow_le_of_le (ennreal.one_le_coe_iff.2 ht.le) hij ... ≀ f x : h'x.2.1 }, { assume hij, rw disjoint.comm, exact this hij.symm } }, { assume n, exact hs.inter (hf measurable_set_Ico) } }, rw [A, B, C, add_assoc], end section metric_space variables [metric_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±] variables [measurable_space Ξ²] {x : Ξ±} {Ξ΅ : ℝ} open metric @[measurability] lemma measurable_set_ball : measurable_set (metric.ball x Ξ΅) := metric.is_open_ball.measurable_set @[measurability] lemma measurable_set_closed_ball : measurable_set (metric.closed_ball x Ξ΅) := metric.is_closed_ball.measurable_set @[measurability] lemma measurable_inf_dist {s : set Ξ±} : measurable (Ξ» x, inf_dist x s) := (continuous_inf_dist_pt s).measurable @[measurability] lemma measurable.inf_dist {f : Ξ² β†’ Ξ±} (hf : measurable f) {s : set Ξ±} : measurable (Ξ» x, inf_dist (f x) s) := measurable_inf_dist.comp hf @[measurability] lemma measurable_inf_nndist {s : set Ξ±} : measurable (Ξ» x, inf_nndist x s) := (continuous_inf_nndist_pt s).measurable @[measurability] lemma measurable.inf_nndist {f : Ξ² β†’ Ξ±} (hf : measurable f) {s : set Ξ±} : measurable (Ξ» x, inf_nndist (f x) s) := measurable_inf_nndist.comp hf section variables [second_countable_topology Ξ±] @[measurability] lemma measurable_dist : measurable (Ξ» p : Ξ± Γ— Ξ±, dist p.1 p.2) := continuous_dist.measurable @[measurability] lemma measurable.dist {f g : Ξ² β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» b, dist (f b) (g b)) := (@continuous_dist Ξ± _).measurable2 hf hg @[measurability] lemma measurable_nndist : measurable (Ξ» p : Ξ± Γ— Ξ±, nndist p.1 p.2) := continuous_nndist.measurable @[measurability] lemma measurable.nndist {f g : Ξ² β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» b, nndist (f b) (g b)) := (@continuous_nndist Ξ± _).measurable2 hf hg end /-- If a set has a closed thickening with finite measure, then the measure of its `r`-closed thickenings converges to the measure of its closure as `r` tends to `0`. -/ lemma tendsto_measure_cthickening {ΞΌ : measure Ξ±} {s : set Ξ±} (hs : βˆƒ R > 0, ΞΌ (cthickening R s) β‰  ∞) : tendsto (Ξ» r, ΞΌ (cthickening r s)) (𝓝 0) (𝓝 (ΞΌ (closure s))) := begin have A : tendsto (Ξ» r, ΞΌ (cthickening r s)) (𝓝[Ioi 0] 0) (𝓝 (ΞΌ (closure s))), { rw closure_eq_Inter_cthickening, exact tendsto_measure_bInter_gt (Ξ» r hr, is_closed_cthickening.measurable_set) (Ξ» i j ipos ij, cthickening_mono ij _) hs }, have B : tendsto (Ξ» r, ΞΌ (cthickening r s)) (𝓝[Iic 0] 0) (𝓝 (ΞΌ (closure s))), { apply tendsto.congr' _ tendsto_const_nhds, filter_upwards [self_mem_nhds_within] with _ hr, rw cthickening_of_nonpos hr, }, convert B.sup A, exact (nhds_left_sup_nhds_right' 0).symm, end /-- If a closed set has a closed thickening with finite measure, then the measure of its `r`-closed thickenings converges to its measure as `r` tends to `0`. -/ lemma tendsto_measure_cthickening_of_is_closed {ΞΌ : measure Ξ±} {s : set Ξ±} (hs : βˆƒ R > 0, ΞΌ (cthickening R s) β‰  ∞) (h's : is_closed s) : tendsto (Ξ» r, ΞΌ (cthickening r s)) (𝓝 0) (𝓝 (ΞΌ s)) := begin convert tendsto_measure_cthickening hs, exact h's.closure_eq.symm end /-- Given a compact set in a proper space, the measure of its `r`-closed thickenings converges to its measure as `r` tends to `0`. -/ lemma tendsto_measure_cthickening_of_is_compact [proper_space Ξ±] {ΞΌ : measure Ξ±} [is_finite_measure_on_compacts ΞΌ] {s : set Ξ±} (hs : is_compact s) : tendsto (Ξ» r, ΞΌ (cthickening r s)) (𝓝 0) (𝓝 (ΞΌ s)) := tendsto_measure_cthickening_of_is_closed ⟨1, zero_lt_one, (bounded.measure_lt_top hs.bounded.cthickening).ne⟩ hs.is_closed end metric_space section emetric_space variables [emetric_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±] variables [measurable_space Ξ²] {x : Ξ±} {Ξ΅ : ℝβ‰₯0∞} open emetric @[measurability] lemma measurable_set_eball : measurable_set (emetric.ball x Ξ΅) := emetric.is_open_ball.measurable_set @[measurability] lemma measurable_edist_right : measurable (edist x) := (continuous_const.edist continuous_id).measurable @[measurability] lemma measurable_edist_left : measurable (Ξ» y, edist y x) := (continuous_id.edist continuous_const).measurable @[measurability] lemma measurable_inf_edist {s : set Ξ±} : measurable (Ξ» x, inf_edist x s) := continuous_inf_edist.measurable @[measurability] lemma measurable.inf_edist {f : Ξ² β†’ Ξ±} (hf : measurable f) {s : set Ξ±} : measurable (Ξ» x, inf_edist (f x) s) := measurable_inf_edist.comp hf variables [second_countable_topology Ξ±] @[measurability] lemma measurable_edist : measurable (Ξ» p : Ξ± Γ— Ξ±, edist p.1 p.2) := continuous_edist.measurable @[measurability] lemma measurable.edist {f g : Ξ² β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» b, edist (f b) (g b)) := (@continuous_edist Ξ± _).measurable2 hf hg @[measurability] lemma ae_measurable.edist {f g : Ξ² β†’ Ξ±} {ΞΌ : measure Ξ²} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, edist (f a) (g a)) ΞΌ := (@continuous_edist Ξ± _).ae_measurable2 hf hg end emetric_space namespace real open measurable_space measure_theory lemma borel_eq_generate_from_Ioo_rat : borel ℝ = generate_from (⋃(a b : β„š) (h : a < b), {Ioo a b}) := is_topological_basis_Ioo_rat.borel_eq_generate_from lemma is_pi_system_Ioo_rat : @is_pi_system ℝ (⋃ (a b : β„š) (h : a < b), {Ioo a b}) := begin convert is_pi_system_Ioo (coe : β„š β†’ ℝ) (coe : β„š β†’ ℝ), ext x, simp [eq_comm] end /-- The intervals `(-(n + 1), (n + 1))` form a finite spanning sets in the set of open intervals with rational endpoints for a locally finite measure `ΞΌ` on `ℝ`. -/ def finite_spanning_sets_in_Ioo_rat (ΞΌ : measure ℝ) [is_locally_finite_measure ΞΌ] : ΞΌ.finite_spanning_sets_in (⋃ (a b : β„š) (h : a < b), {Ioo a b}) := { set := Ξ» n, Ioo (-(n + 1)) (n + 1), set_mem := Ξ» n, begin simp only [mem_Union, mem_singleton_iff], refine ⟨-(n + 1), n + 1, _, by norm_cast⟩, exact (neg_nonpos.2 (@nat.cast_nonneg β„š _ (n + 1))).trans_lt n.cast_add_one_pos end, finite := Ξ» n, measure_Ioo_lt_top, spanning := Union_eq_univ_iff.2 $ Ξ» x, ⟨⌊|x|βŒ‹β‚Š, neg_lt.1 ((neg_le_abs_self x).trans_lt (nat.lt_floor_add_one _)), (le_abs_self x).trans_lt (nat.lt_floor_add_one _)⟩ } lemma measure_ext_Ioo_rat {ΞΌ Ξ½ : measure ℝ} [is_locally_finite_measure ΞΌ] (h : βˆ€ a b : β„š, ΞΌ (Ioo a b) = Ξ½ (Ioo a b)) : ΞΌ = Ξ½ := (finite_spanning_sets_in_Ioo_rat ΞΌ).ext borel_eq_generate_from_Ioo_rat is_pi_system_Ioo_rat $ by { simp only [mem_Union, mem_singleton_iff], rintro _ ⟨a, b, -, rfl⟩, apply h } lemma borel_eq_generate_from_Iio_rat : borel ℝ = generate_from (⋃ a : β„š, {Iio a}) := begin let g : measurable_space ℝ := generate_from (⋃ a : β„š, {Iio a}), refine le_antisymm _ _, { rw borel_eq_generate_from_Ioo_rat, refine generate_from_le (Ξ» t, _), simp only [mem_Union, mem_singleton_iff], rintro ⟨a, b, h, rfl⟩, rw (set.ext (Ξ» x, _) : Ioo (a : ℝ) b = (⋃c>a, (Iio c)ᢜ) ∩ Iio b), { have hg : βˆ€ q : β„š, g.measurable_set' (Iio q) := Ξ» q, generate_measurable.basic (Iio q) (by simp), refine @measurable_set.inter _ g _ _ _ (hg _), refine @measurable_set.bUnion _ _ g _ _ (countable_encodable _) (Ξ» c h, _), exact @measurable_set.compl _ _ g (hg _) }, { suffices : x < ↑b β†’ (↑a < x ↔ βˆƒ (i : β„š), a < i ∧ ↑i ≀ x), by simpa, refine Ξ» _, ⟨λ h, _, Ξ» ⟨i, hai, hix⟩, (rat.cast_lt.2 hai).trans_le hix⟩, rcases exists_rat_btwn h with ⟨c, ac, cx⟩, exact ⟨c, rat.cast_lt.1 ac, cx.le⟩ } }, { refine measurable_space.generate_from_le (Ξ» _, _), simp only [mem_Union, mem_singleton_iff], rintro ⟨r, rfl⟩, exact measurable_set_Iio } end end real variable [measurable_space Ξ±] @[measurability] lemma measurable_real_to_nnreal : measurable (real.to_nnreal) := nnreal.continuous_of_real.measurable @[measurability] lemma measurable.real_to_nnreal {f : Ξ± β†’ ℝ} (hf : measurable f) : measurable (Ξ» x, real.to_nnreal (f x)) := measurable_real_to_nnreal.comp hf @[measurability] lemma ae_measurable.real_to_nnreal {f : Ξ± β†’ ℝ} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» x, real.to_nnreal (f x)) ΞΌ := measurable_real_to_nnreal.comp_ae_measurable hf @[measurability] lemma measurable_coe_nnreal_real : measurable (coe : ℝβ‰₯0 β†’ ℝ) := nnreal.continuous_coe.measurable @[measurability] lemma measurable.coe_nnreal_real {f : Ξ± β†’ ℝβ‰₯0} (hf : measurable f) : measurable (Ξ» x, (f x : ℝ)) := measurable_coe_nnreal_real.comp hf @[measurability] lemma ae_measurable.coe_nnreal_real {f : Ξ± β†’ ℝβ‰₯0} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» x, (f x : ℝ)) ΞΌ := measurable_coe_nnreal_real.comp_ae_measurable hf @[measurability] lemma measurable_coe_nnreal_ennreal : measurable (coe : ℝβ‰₯0 β†’ ℝβ‰₯0∞) := ennreal.continuous_coe.measurable @[measurability] lemma measurable.coe_nnreal_ennreal {f : Ξ± β†’ ℝβ‰₯0} (hf : measurable f) : measurable (Ξ» x, (f x : ℝβ‰₯0∞)) := ennreal.continuous_coe.measurable.comp hf @[measurability] lemma ae_measurable.coe_nnreal_ennreal {f : Ξ± β†’ ℝβ‰₯0} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» x, (f x : ℝβ‰₯0∞)) ΞΌ := ennreal.continuous_coe.measurable.comp_ae_measurable hf @[measurability] lemma measurable.ennreal_of_real {f : Ξ± β†’ ℝ} (hf : measurable f) : measurable (Ξ» x, ennreal.of_real (f x)) := ennreal.continuous_of_real.measurable.comp hf /-- The set of finite `ℝβ‰₯0∞` numbers is `measurable_equiv` to `ℝβ‰₯0`. -/ def measurable_equiv.ennreal_equiv_nnreal : {r : ℝβ‰₯0∞ | r β‰  ∞} ≃ᡐ ℝβ‰₯0 := ennreal.ne_top_homeomorph_nnreal.to_measurable_equiv namespace ennreal lemma measurable_of_measurable_nnreal {f : ℝβ‰₯0∞ β†’ Ξ±} (h : measurable (Ξ» p : ℝβ‰₯0, f p)) : measurable f := measurable_of_measurable_on_compl_singleton ∞ (measurable_equiv.ennreal_equiv_nnreal.symm.measurable_comp_iff.1 h) /-- `ℝβ‰₯0∞` is `measurable_equiv` to `ℝβ‰₯0 βŠ• unit`. -/ def ennreal_equiv_sum : ℝβ‰₯0∞ ≃ᡐ ℝβ‰₯0 βŠ• unit := { measurable_to_fun := measurable_of_measurable_nnreal measurable_inl, measurable_inv_fun := measurable_sum measurable_coe_nnreal_ennreal (@measurable_const ℝβ‰₯0∞ unit _ _ ∞), .. equiv.option_equiv_sum_punit ℝβ‰₯0 } open function (uncurry) lemma measurable_of_measurable_nnreal_prod [measurable_space Ξ²] [measurable_space Ξ³] {f : ℝβ‰₯0∞ Γ— Ξ² β†’ Ξ³} (H₁ : measurable (Ξ» p : ℝβ‰₯0 Γ— Ξ², f (p.1, p.2))) (Hβ‚‚ : measurable (Ξ» x, f (∞, x))) : measurable f := let e : ℝβ‰₯0∞ Γ— Ξ² ≃ᡐ ℝβ‰₯0 Γ— Ξ² βŠ• unit Γ— Ξ² := (ennreal_equiv_sum.prod_congr (measurable_equiv.refl Ξ²)).trans (measurable_equiv.sum_prod_distrib _ _ _) in e.symm.measurable_comp_iff.1 $ measurable_sum H₁ (Hβ‚‚.comp measurable_id.snd) lemma measurable_of_measurable_nnreal_nnreal [measurable_space Ξ²] {f : ℝβ‰₯0∞ Γ— ℝβ‰₯0∞ β†’ Ξ²} (h₁ : measurable (Ξ» p : ℝβ‰₯0 Γ— ℝβ‰₯0, f (p.1, p.2))) (hβ‚‚ : measurable (Ξ» r : ℝβ‰₯0, f (∞, r))) (h₃ : measurable (Ξ» r : ℝβ‰₯0, f (r, ∞))) : measurable f := measurable_of_measurable_nnreal_prod (measurable_swap_iff.1 $ measurable_of_measurable_nnreal_prod (h₁.comp measurable_swap) h₃) (measurable_of_measurable_nnreal hβ‚‚) @[measurability] lemma measurable_of_real : measurable ennreal.of_real := ennreal.continuous_of_real.measurable @[measurability] lemma measurable_to_real : measurable ennreal.to_real := ennreal.measurable_of_measurable_nnreal measurable_coe_nnreal_real @[measurability] lemma measurable_to_nnreal : measurable ennreal.to_nnreal := ennreal.measurable_of_measurable_nnreal measurable_id instance : has_measurable_mulβ‚‚ ℝβ‰₯0∞ := begin refine ⟨measurable_of_measurable_nnreal_nnreal _ _ _⟩, { simp only [← ennreal.coe_mul, measurable_mul.coe_nnreal_ennreal] }, { simp only [ennreal.top_mul, ennreal.coe_eq_zero], exact measurable_const.piecewise (measurable_set_singleton _) measurable_const }, { simp only [ennreal.mul_top, ennreal.coe_eq_zero], exact measurable_const.piecewise (measurable_set_singleton _) measurable_const } end instance : has_measurable_subβ‚‚ ℝβ‰₯0∞ := ⟨by apply measurable_of_measurable_nnreal_nnreal; simp [← with_top.coe_sub, continuous_sub.measurable.coe_nnreal_ennreal]⟩ instance : has_measurable_inv ℝβ‰₯0∞ := ⟨ennreal.continuous_inv.measurable⟩ end ennreal @[measurability] lemma measurable.ennreal_to_nnreal {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : measurable (Ξ» x, (f x).to_nnreal) := ennreal.measurable_to_nnreal.comp hf @[measurability] lemma ae_measurable.ennreal_to_nnreal {f : Ξ± β†’ ℝβ‰₯0∞} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» x, (f x).to_nnreal) ΞΌ := ennreal.measurable_to_nnreal.comp_ae_measurable hf lemma measurable_coe_nnreal_ennreal_iff {f : Ξ± β†’ ℝβ‰₯0} : measurable (Ξ» x, (f x : ℝβ‰₯0∞)) ↔ measurable f := ⟨λ h, h.ennreal_to_nnreal, Ξ» h, h.coe_nnreal_ennreal⟩ @[measurability] lemma measurable.ennreal_to_real {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : measurable (Ξ» x, ennreal.to_real (f x)) := ennreal.measurable_to_real.comp hf @[measurability] lemma ae_measurable.ennreal_to_real {f : Ξ± β†’ ℝβ‰₯0∞} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» x, ennreal.to_real (f x)) ΞΌ := ennreal.measurable_to_real.comp_ae_measurable hf /-- note: `ℝβ‰₯0∞` can probably be generalized in a future version of this lemma. -/ @[measurability] lemma measurable.ennreal_tsum {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ± β†’ ℝβ‰₯0∞} (h : βˆ€ i, measurable (f i)) : measurable (Ξ» x, βˆ‘' i, f i x) := by { simp_rw [ennreal.tsum_eq_supr_sum], apply measurable_supr, exact Ξ» s, s.measurable_sum (Ξ» i _, h i) } @[measurability] lemma measurable.ennreal_tsum' {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ± β†’ ℝβ‰₯0∞} (h : βˆ€ i, measurable (f i)) : measurable (βˆ‘' i, f i) := begin convert measurable.ennreal_tsum h, ext1 x, exact tsum_apply (pi.summable.2 (Ξ» _, ennreal.summable)), end @[measurability] lemma measurable.nnreal_tsum {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ± β†’ ℝβ‰₯0} (h : βˆ€ i, measurable (f i)) : measurable (Ξ» x, βˆ‘' i, f i x) := begin simp_rw [nnreal.tsum_eq_to_nnreal_tsum], exact (measurable.ennreal_tsum (Ξ» i, (h i).coe_nnreal_ennreal)).ennreal_to_nnreal, end @[measurability] lemma ae_measurable.ennreal_tsum {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ± β†’ ℝβ‰₯0∞} {ΞΌ : measure Ξ±} (h : βˆ€ i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» x, βˆ‘' i, f i x) ΞΌ := by { simp_rw [ennreal.tsum_eq_supr_sum], apply ae_measurable_supr, exact Ξ» s, finset.ae_measurable_sum s (Ξ» i _, h i) } @[measurability] lemma measurable_coe_real_ereal : measurable (coe : ℝ β†’ ereal) := continuous_coe_real_ereal.measurable @[measurability] lemma measurable.coe_real_ereal {f : Ξ± β†’ ℝ} (hf : measurable f) : measurable (Ξ» x, (f x : ereal)) := measurable_coe_real_ereal.comp hf @[measurability] lemma ae_measurable.coe_real_ereal {f : Ξ± β†’ ℝ} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» x, (f x : ereal)) ΞΌ := measurable_coe_real_ereal.comp_ae_measurable hf /-- The set of finite `ereal` numbers is `measurable_equiv` to `ℝ`. -/ def measurable_equiv.ereal_equiv_real : ({βŠ₯, ⊀} : set ereal).compl ≃ᡐ ℝ := ereal.ne_bot_top_homeomorph_real.to_measurable_equiv lemma ereal.measurable_of_measurable_real {f : ereal β†’ Ξ±} (h : measurable (Ξ» p : ℝ, f p)) : measurable f := measurable_of_measurable_on_compl_finite {βŠ₯, ⊀} (by simp) (measurable_equiv.ereal_equiv_real.symm.measurable_comp_iff.1 h) @[measurability] lemma measurable_ereal_to_real : measurable ereal.to_real := ereal.measurable_of_measurable_real (by simpa using measurable_id) @[measurability] lemma measurable.ereal_to_real {f : Ξ± β†’ ereal} (hf : measurable f) : measurable (Ξ» x, (f x).to_real) := measurable_ereal_to_real.comp hf @[measurability] lemma ae_measurable.ereal_to_real {f : Ξ± β†’ ereal} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» x, (f x).to_real) ΞΌ := measurable_ereal_to_real.comp_ae_measurable hf @[measurability] lemma measurable_coe_ennreal_ereal : measurable (coe : ℝβ‰₯0∞ β†’ ereal) := continuous_coe_ennreal_ereal.measurable @[measurability] lemma measurable.coe_ereal_ennreal {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : measurable (Ξ» x, (f x : ereal)) := measurable_coe_ennreal_ereal.comp hf @[measurability] lemma ae_measurable.coe_ereal_ennreal {f : Ξ± β†’ ℝβ‰₯0∞} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» x, (f x : ereal)) ΞΌ := measurable_coe_ennreal_ereal.comp_ae_measurable hf section normed_group variables [normed_group Ξ±] [opens_measurable_space Ξ±] [measurable_space Ξ²] @[measurability] lemma measurable_norm : measurable (norm : Ξ± β†’ ℝ) := continuous_norm.measurable @[measurability] lemma measurable.norm {f : Ξ² β†’ Ξ±} (hf : measurable f) : measurable (Ξ» a, norm (f a)) := measurable_norm.comp hf @[measurability] lemma ae_measurable.norm {f : Ξ² β†’ Ξ±} {ΞΌ : measure Ξ²} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» a, norm (f a)) ΞΌ := measurable_norm.comp_ae_measurable hf @[measurability] lemma measurable_nnnorm : measurable (nnnorm : Ξ± β†’ ℝβ‰₯0) := continuous_nnnorm.measurable @[measurability] lemma measurable.nnnorm {f : Ξ² β†’ Ξ±} (hf : measurable f) : measurable (Ξ» a, nnnorm (f a)) := measurable_nnnorm.comp hf @[measurability] lemma ae_measurable.nnnorm {f : Ξ² β†’ Ξ±} {ΞΌ : measure Ξ²} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» a, nnnorm (f a)) ΞΌ := measurable_nnnorm.comp_ae_measurable hf @[measurability] lemma measurable_ennnorm : measurable (Ξ» x : Ξ±, (nnnorm x : ℝβ‰₯0∞)) := measurable_nnnorm.coe_nnreal_ennreal @[measurability] lemma measurable.ennnorm {f : Ξ² β†’ Ξ±} (hf : measurable f) : measurable (Ξ» a, (nnnorm (f a) : ℝβ‰₯0∞)) := hf.nnnorm.coe_nnreal_ennreal @[measurability] lemma ae_measurable.ennnorm {f : Ξ² β†’ Ξ±} {ΞΌ : measure Ξ²} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» a, (nnnorm (f a) : ℝβ‰₯0∞)) ΞΌ := measurable_ennnorm.comp_ae_measurable hf end normed_group section limits variables [measurable_space Ξ²] [metric_space Ξ²] [borel_space Ξ²] open metric /-- A limit (over a general filter) of measurable `ℝβ‰₯0` valued functions is measurable. -/ lemma measurable_of_tendsto_nnreal' {ΞΉ} {f : ΞΉ β†’ Ξ± β†’ ℝβ‰₯0} {g : Ξ± β†’ ℝβ‰₯0} (u : filter ΞΉ) [ne_bot u] [is_countably_generated u] (hf : βˆ€ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) : measurable g := begin rcases u.exists_seq_tendsto with ⟨x, hx⟩, rw [tendsto_pi_nhds] at lim, rw [← measurable_coe_nnreal_ennreal_iff], have : βˆ€ y, liminf at_top (Ξ» n, (f (x n) y : ℝβ‰₯0∞)) = (g y : ℝβ‰₯0∞) := Ξ» y, ((ennreal.continuous_coe.tendsto (g y)).comp $ (lim y).comp hx).liminf_eq, simp only [← this], show measurable (Ξ» y, liminf at_top (Ξ» n, (f (x n) y : ℝβ‰₯0∞))), exact measurable_liminf (Ξ» n, (hf (x n)).coe_nnreal_ennreal), end /-- A sequential limit of measurable `ℝβ‰₯0` valued functions is measurable. -/ lemma measurable_of_tendsto_nnreal {f : β„• β†’ Ξ± β†’ ℝβ‰₯0} {g : Ξ± β†’ ℝβ‰₯0} (hf : βˆ€ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) : measurable g := measurable_of_tendsto_nnreal' at_top hf lim /-- A limit (over a general filter) of measurable functions valued in a metric space is measurable. The assumption `hs` can be dropped using `filter.is_countably_generated.has_antitone_basis`, but we don't need that case yet. -/ lemma measurable_of_tendsto_metric' {ΞΉ} {f : ΞΉ β†’ Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} (u : filter ΞΉ) [ne_bot u] [is_countably_generated u] (hf : βˆ€ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) : measurable g := begin apply measurable_of_is_closed', intros s h1s h2s h3s, have : measurable (Ξ» x, inf_nndist (g x) s), { suffices : tendsto (Ξ» i x, inf_nndist (f i x) s) u (𝓝 (Ξ» x, inf_nndist (g x) s)), from measurable_of_tendsto_nnreal' u (Ξ» i, (hf i).inf_nndist) this, rw [tendsto_pi_nhds] at lim ⊒, intro x, exact ((continuous_inf_nndist_pt s).tendsto (g x)).comp (lim x) }, have h4s : g ⁻¹' s = (Ξ» x, inf_nndist (g x) s) ⁻¹' {0}, { ext x, simp [h1s, ← h1s.mem_iff_inf_dist_zero h2s, ← nnreal.coe_eq_zero] }, rw [h4s], exact this (measurable_set_singleton 0), end /-- A sequential limit of measurable functions valued in a metric space is measurable. -/ lemma measurable_of_tendsto_metric {f : β„• β†’ Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} (hf : βˆ€ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) : measurable g := measurable_of_tendsto_metric' at_top hf lim lemma ae_measurable_of_tendsto_metric_ae {ΞΌ : measure Ξ±} {f : β„• β†’ Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} (hf : βˆ€ n, ae_measurable (f n) ΞΌ) (h_ae_tendsto : βˆ€α΅ x βˆ‚ΞΌ, tendsto (Ξ» n, f n x) at_top (𝓝 (g x))) : ae_measurable g ΞΌ := begin let p : Ξ± β†’ (β„• β†’ Ξ²) β†’ Prop := Ξ» x f', filter.at_top.tendsto (Ξ» n, f' n) (𝓝 (g x)), let hp : βˆ€α΅ x βˆ‚ΞΌ, p x (Ξ» n, f n x), from h_ae_tendsto, let ae_seq_lim := Ξ» x, ite (x ∈ ae_seq_set hf p) (g x) (⟨f 0 x⟩ : nonempty Ξ²).some, refine ⟨ae_seq_lim, _, (ite_ae_eq_of_measure_compl_zero g (Ξ» x, (⟨f 0 x⟩ : nonempty Ξ²).some) (ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hp)).symm⟩, refine measurable_of_tendsto_metric (@ae_seq.measurable Ξ± Ξ² _ _ _ f ΞΌ hf p) _, refine tendsto_pi_nhds.mpr (Ξ» x, _), simp_rw [ae_seq, ae_seq_lim], split_ifs with hx, { simp_rw ae_seq.mk_eq_fun_of_mem_ae_seq_set hf hx, exact @ae_seq.fun_prop_of_mem_ae_seq_set Ξ± Ξ² _ _ _ _ _ _ hf x hx, }, { exact tendsto_const_nhds, }, end lemma ae_measurable_of_unif_approx {ΞΌ : measure Ξ±} {g : Ξ± β†’ Ξ²} (hf : βˆ€ Ξ΅ > (0 : ℝ), βˆƒ (f : Ξ± β†’ Ξ²), ae_measurable f ΞΌ ∧ βˆ€α΅ x βˆ‚ΞΌ, dist (f x) (g x) ≀ Ξ΅) : ae_measurable g ΞΌ := begin obtain ⟨u, u_anti, u_pos, u_lim⟩ : βˆƒ (u : β„• β†’ ℝ), strict_anti u ∧ (βˆ€ (n : β„•), 0 < u n) ∧ tendsto u at_top (𝓝 0) := exists_seq_strict_anti_tendsto (0 : ℝ), choose f Hf using Ξ» (n : β„•), hf (u n) (u_pos n), have : βˆ€α΅ x βˆ‚ΞΌ, tendsto (Ξ» n, f n x) at_top (𝓝 (g x)), { have : βˆ€α΅ x βˆ‚ ΞΌ, βˆ€ n, dist (f n x) (g x) ≀ u n := ae_all_iff.2 (Ξ» n, (Hf n).2), filter_upwards [this], assume x hx, rw tendsto_iff_dist_tendsto_zero, exact squeeze_zero (Ξ» n, dist_nonneg) hx u_lim }, exact ae_measurable_of_tendsto_metric_ae (Ξ» n, (Hf n).1) this, end lemma measurable_of_tendsto_metric_ae {ΞΌ : measure Ξ±} [ΞΌ.is_complete] {f : β„• β†’ Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} (hf : βˆ€ n, measurable (f n)) (h_ae_tendsto : βˆ€α΅ x βˆ‚ΞΌ, filter.at_top.tendsto (Ξ» n, f n x) (𝓝 (g x))) : measurable g := ae_measurable_iff_measurable.mp (ae_measurable_of_tendsto_metric_ae (Ξ» i, (hf i).ae_measurable) h_ae_tendsto) lemma measurable_limit_of_tendsto_metric_ae {ΞΌ : measure Ξ±} {f : β„• β†’ Ξ± β†’ Ξ²} (hf : βˆ€ n, ae_measurable (f n) ΞΌ) (h_ae_tendsto : βˆ€α΅ x βˆ‚ΞΌ, βˆƒ l : Ξ², filter.at_top.tendsto (Ξ» n, f n x) (𝓝 l)) : βˆƒ (f_lim : Ξ± β†’ Ξ²) (hf_lim_meas : measurable f_lim), βˆ€α΅ x βˆ‚ΞΌ, filter.at_top.tendsto (Ξ» n, f n x) (𝓝 (f_lim x)) := begin let p : Ξ± β†’ (β„• β†’ Ξ²) β†’ Prop := Ξ» x f', βˆƒ l : Ξ², filter.at_top.tendsto (Ξ» n, f' n) (𝓝 l), have hp_mem : βˆ€ x, x ∈ ae_seq_set hf p β†’ p x (Ξ» n, f n x), from Ξ» x hx, ae_seq.fun_prop_of_mem_ae_seq_set hf hx, have hΞΌ_compl : ΞΌ (ae_seq_set hf p)ᢜ = 0, from ae_seq.measure_compl_ae_seq_set_eq_zero hf h_ae_tendsto, let f_lim : Ξ± β†’ Ξ² := Ξ» x, dite (x ∈ ae_seq_set hf p) (Ξ» h, (hp_mem x h).some) (Ξ» h, (⟨f 0 x⟩ : nonempty Ξ²).some), have hf_lim_conv : βˆ€ x, x ∈ ae_seq_set hf p β†’ filter.at_top.tendsto (Ξ» n, f n x) (𝓝 (f_lim x)), { intros x hx_conv, simp only [f_lim, hx_conv, dif_pos], exact (hp_mem x hx_conv).some_spec, }, have hf_lim : βˆ€ x, filter.at_top.tendsto (Ξ» n, ae_seq hf p n x) (𝓝 (f_lim x)), { intros x, simp only [f_lim, ae_seq], split_ifs, { rw funext (Ξ» n, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h n), exact (hp_mem x h).some_spec, }, { exact tendsto_const_nhds, }, }, have h_ae_tendsto_f_lim : βˆ€α΅ x βˆ‚ΞΌ, filter.at_top.tendsto (Ξ» n, f n x) (𝓝 (f_lim x)), { refine le_antisymm (le_of_eq (measure_mono_null _ hΞΌ_compl)) (zero_le _), exact set.compl_subset_compl.mpr (Ξ» x hx, hf_lim_conv x hx), }, have h_f_lim_meas : measurable f_lim, from measurable_of_tendsto_metric (ae_seq.measurable hf p) (tendsto_pi_nhds.mpr (Ξ» x, hf_lim x)), exact ⟨f_lim, h_f_lim_meas, h_ae_tendsto_f_lim⟩, end end limits namespace continuous_linear_map variables {π•œ : Type*} [normed_field π•œ] variables {E : Type*} [normed_group E] [normed_space π•œ E] [measurable_space E] variables [opens_measurable_space E] variables {F : Type*} [normed_group F] [normed_space π•œ F] [measurable_space F] [borel_space F] @[measurability] protected lemma measurable (L : E β†’L[π•œ] F) : measurable L := L.continuous.measurable lemma measurable_comp (L : E β†’L[π•œ] F) {Ο† : Ξ± β†’ E} (Ο†_meas : measurable Ο†) : measurable (Ξ» (a : Ξ±), L (Ο† a)) := L.measurable.comp Ο†_meas end continuous_linear_map namespace continuous_linear_map variables {π•œ : Type*} [nondiscrete_normed_field π•œ] variables {E : Type*} [normed_group E] [normed_space π•œ E] {F : Type*} [normed_group F] [normed_space π•œ F] instance : measurable_space (E β†’L[π•œ] F) := borel _ instance : borel_space (E β†’L[π•œ] F) := ⟨rfl⟩ @[measurability] lemma measurable_apply [measurable_space F] [borel_space F] (x : E) : measurable (Ξ» f : E β†’L[π•œ] F, f x) := (apply π•œ F x).continuous.measurable @[measurability] lemma measurable_apply' [measurable_space E] [opens_measurable_space E] [measurable_space F] [borel_space F] : measurable (Ξ» (x : E) (f : E β†’L[π•œ] F), f x) := measurable_pi_lambda _ $ Ξ» f, f.measurable @[measurability] lemma measurable_coe [measurable_space F] [borel_space F] : measurable (Ξ» (f : E β†’L[π•œ] F) (x : E), f x) := measurable_pi_lambda _ measurable_apply end continuous_linear_map section continuous_linear_map_nondiscrete_normed_field variables {π•œ : Type*} [nondiscrete_normed_field π•œ] variables {E : Type*} [normed_group E] [normed_space π•œ E] [measurable_space E] [borel_space E] variables {F : Type*} [normed_group F] [normed_space π•œ F] @[measurability] lemma measurable.apply_continuous_linear_map {Ο† : Ξ± β†’ F β†’L[π•œ] E} (hΟ† : measurable Ο†) (v : F) : measurable (Ξ» a, Ο† a v) := (continuous_linear_map.apply π•œ E v).measurable.comp hΟ† @[measurability] lemma ae_measurable.apply_continuous_linear_map {Ο† : Ξ± β†’ F β†’L[π•œ] E} {ΞΌ : measure Ξ±} (hΟ† : ae_measurable Ο† ΞΌ) (v : F) : ae_measurable (Ξ» a, Ο† a v) ΞΌ := (continuous_linear_map.apply π•œ E v).measurable.comp_ae_measurable hΟ† end continuous_linear_map_nondiscrete_normed_field section normed_space variables {π•œ : Type*} [nondiscrete_normed_field π•œ] [complete_space π•œ] [measurable_space π•œ] variables [borel_space π•œ] variables {E : Type*} [normed_group E] [normed_space π•œ E] [measurable_space E] [borel_space E] lemma measurable_smul_const {f : Ξ± β†’ π•œ} {c : E} (hc : c β‰  0) : measurable (Ξ» x, f x β€’ c) ↔ measurable f := (closed_embedding_smul_left hc).measurable_embedding.measurable_comp_iff lemma ae_measurable_smul_const {f : Ξ± β†’ π•œ} {ΞΌ : measure Ξ±} {c : E} (hc : c β‰  0) : ae_measurable (Ξ» x, f x β€’ c) ΞΌ ↔ ae_measurable f ΞΌ := (closed_embedding_smul_left hc).measurable_embedding.ae_measurable_comp_iff end normed_space
4c1eb191a82b3758fe9d06d2a480a7ad078e5a76
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/data/nat/sqrt.lean
18c5a3d057dfc81a64c73ca62609b2a686f68b49
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
6,939
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura Very simple (sqrt n) function that returns s s.t. s*s ≀ n ≀ s*s + s + s -/ import data.nat.order data.nat.sub namespace nat open decidable -- This is the simplest possible function that just performs a linear search definition sqrt_aux : nat β†’ nat β†’ nat | 0 n := 0 | (succ s) n := if (succ s)*(succ s) ≀ n then succ s else sqrt_aux s n theorem sqrt_aux_succ_of_pos {s n} : (succ s)*(succ s) ≀ n β†’ sqrt_aux (succ s) n = (succ s) := assume h, if_pos h theorem sqrt_aux_succ_of_neg {s n} : Β¬ (succ s)*(succ s) ≀ n β†’ sqrt_aux (succ s) n = sqrt_aux s n := assume h, if_neg h theorem sqrt_aux_of_le : βˆ€ {s n : nat}, s * s ≀ n β†’ sqrt_aux s n = s | 0 n h := rfl | (succ s) n h := by rewrite [sqrt_aux_succ_of_pos h] theorem sqrt_aux_le : βˆ€ (s n), sqrt_aux s n ≀ s | 0 n := !zero_le | (succ s) n := or.elim (em ((succ s)*(succ s) ≀ n)) (Ξ» h, begin unfold sqrt_aux, rewrite [if_pos h] end) (Ξ» h, assert sqrt_aux s n ≀ succ s, from le.step (sqrt_aux_le s n), begin unfold sqrt_aux, rewrite [if_neg h], assumption end) definition sqrt (n : nat) : nat := sqrt_aux n n theorem sqrt_aux_lower : βˆ€ {s n : nat}, s ≀ n β†’ sqrt_aux s n * sqrt_aux s n ≀ n | 0 n h := h | (succ s) n h := by_cases (Ξ» h₁ : (succ s)*(succ s) ≀ n, by rewrite [sqrt_aux_succ_of_pos h₁]; exact h₁) (Ξ» hβ‚‚ : Β¬ (succ s)*(succ s) ≀ n, assert aux : s ≀ n, from le_of_succ_le h, by rewrite [sqrt_aux_succ_of_neg hβ‚‚]; exact (sqrt_aux_lower aux)) theorem sqrt_lower (n : nat) : sqrt n * sqrt n ≀ n := sqrt_aux_lower (le.refl n) theorem sqrt_aux_upper : βˆ€ {s n : nat}, n ≀ s*s + s + s β†’ n ≀ sqrt_aux s n * sqrt_aux s n + sqrt_aux s n + sqrt_aux s n | 0 n h := h | (succ s) n h := by_cases (Ξ» h₁ : (succ s)*(succ s) ≀ n, by rewrite [sqrt_aux_succ_of_pos h₁]; exact h) (Ξ» hβ‚‚ : Β¬ (succ s)*(succ s) ≀ n, assert h₃ : n < (succ s) * (succ s), from lt_of_not_ge hβ‚‚, assert hβ‚„ : n ≀ s * s + s + s, by rewrite [succ_mul_succ_eq at h₃]; exact le_of_lt_succ h₃, by rewrite [sqrt_aux_succ_of_neg hβ‚‚]; exact (sqrt_aux_upper hβ‚„)) theorem sqrt_upper (n : nat) : n ≀ sqrt n * sqrt n + sqrt n + sqrt n := have aux : n ≀ n*n + n + n, from le_add_of_le_right (le_add_of_le_left (le.refl n)), sqrt_aux_upper aux private theorem le_squared : βˆ€ (n : nat), n ≀ n*n | 0 := !le.refl | (succ n) := have aux₁ : 1 ≀ succ n, from succ_le_succ !zero_le, assert auxβ‚‚ : 1 * succ n ≀ succ n * succ n, from nat.mul_le_mul aux₁ !le.refl, by rewrite [one_mul at auxβ‚‚]; exact auxβ‚‚ private theorem lt_squared : βˆ€ {n : nat}, n > 1 β†’ n < n * n | 0 h := absurd h dec_trivial | 1 h := absurd h dec_trivial | (succ (succ n)) h := have 1 < succ (succ n), from dec_trivial, assert succ (succ n) * 1 < succ (succ n) * succ (succ n), from mul_lt_mul_of_pos_left this dec_trivial, by rewrite [mul_one at this]; exact this theorem sqrt_le (n : nat) : sqrt n ≀ n := calc sqrt n ≀ sqrt n * sqrt n : le_squared ... ≀ n : sqrt_lower theorem eq_zero_of_sqrt_eq_zero {n : nat} : sqrt n = 0 β†’ n = 0 := suppose sqrt n = 0, assert n ≀ sqrt n * sqrt n + sqrt n + sqrt n, from !sqrt_upper, have n ≀ 0, by rewrite [*`sqrt n = 0` at this]; exact this, eq_zero_of_le_zero this theorem le_three_of_sqrt_eq_one {n : nat} : sqrt n = 1 β†’ n ≀ 3 := suppose sqrt n = 1, assert n ≀ sqrt n * sqrt n + sqrt n + sqrt n, from !sqrt_upper, show n ≀ 3, by rewrite [*`sqrt n = 1` at this]; exact this theorem sqrt_lt : βˆ€ {n : nat}, n > 1 β†’ sqrt n < n | 0 h := absurd h dec_trivial | 1 h := absurd h dec_trivial | 2 h := dec_trivial | 3 h := dec_trivial | (n+4) h := have sqrt (n+4) > 1, from by_contradiction (suppose Β¬ sqrt (n+4) > 1, have sqrt (n+4) ≀ 1, from le_of_not_gt this, or.elim (eq_or_lt_of_le this) (suppose sqrt (n+4) = 1, have n+4 ≀ 3, from le_three_of_sqrt_eq_one this, absurd this dec_trivial) (suppose sqrt (n+4) < 1, have sqrt (n+4) = 0, from eq_zero_of_le_zero (le_of_lt_succ this), have n + 4 = 0, from eq_zero_of_sqrt_eq_zero this, absurd this dec_trivial)), calc sqrt (n+4) < sqrt (n+4) * sqrt (n+4) : lt_squared this ... ≀ n+4 : sqrt_lower theorem sqrt_pos_of_pos {n : nat} : n > 0 β†’ sqrt n > 0 := suppose n > 0, have sqrt n β‰  0, from suppose sqrt n = 0, assert n = 0, from eq_zero_of_sqrt_eq_zero this, by subst n; exact absurd `0 > 0` !lt.irrefl, pos_of_ne_zero this theorem sqrt_aux_offset_eq {n k : nat} (h₁ : k ≀ n + n) : βˆ€ {s}, s β‰₯ n β†’ sqrt_aux s (n*n + k) = n | 0 hβ‚‚ := assert neqz : n = 0, from eq_zero_of_le_zero hβ‚‚, by rewrite neqz | (succ s) hβ‚‚ := by_cases (Ξ» hl : (succ s)*(succ s) ≀ n*n + k, have l₁ : n*n + k ≀ n*n + n + n, from by rewrite [add.assoc]; exact (add_le_add_left h₁ (n*n)), assert lβ‚‚ : n*n + k < n*n + n + n + 1, from lt_succ_of_le l₁, have l₃ : n*n + k < (succ n)*(succ n), by rewrite [-succ_mul_succ_eq at lβ‚‚]; exact lβ‚‚, assert lβ‚„ : (succ s)*(succ s) < (succ n)*(succ n), from lt_of_le_of_lt hl l₃, have ng : Β¬ succ s > (succ n), from assume g : succ s > succ n, have g₁ : (succ s)*(succ s) > (succ n)*(succ n), from mul_lt_mul_of_le_of_le g g, absurd (lt.trans g₁ lβ‚„) !lt.irrefl, have sslesn : succ s ≀ succ n, from le_of_not_gt ng, have ssnesn : succ s β‰  succ n, from assume sseqsn : succ s = succ n, by rewrite [sseqsn at lβ‚„]; exact (absurd lβ‚„ !lt.irrefl), have sslen : s < n, from lt_of_succ_lt_succ (lt_of_le_of_ne sslesn ssnesn), assert sseqn : succ s = n, from le.antisymm sslen hβ‚‚, by rewrite [sqrt_aux_succ_of_pos hl]; exact sseqn) (Ξ» hg : Β¬ (succ s)*(succ s) ≀ n*n + k, or.elim (eq_or_lt_of_le hβ‚‚) (Ξ» neqss : n = succ s, have p : n*n ≀ n*n + k, from !le_add_right, have n : Β¬ n*n ≀ n*n + k, by rewrite [-neqss at hg]; exact hg, absurd p n) (Ξ» sgen : succ s > n, by rewrite [sqrt_aux_succ_of_neg hg]; exact (sqrt_aux_offset_eq (le_of_lt_succ sgen)))) theorem sqrt_offset_eq {n k : nat} : k ≀ n + n β†’ sqrt (n*n + k) = n := assume h, have h₁ : n ≀ n*n + k, from le.trans !le_squared !le_add_right, sqrt_aux_offset_eq h h₁ theorem sqrt_eq (n : nat) : sqrt (n*n) = n := sqrt_offset_eq !zero_le theorem mul_square_cancel {a b : nat} : a*a = b*b β†’ a = b := assume h, assert aux : sqrt (a*a) = sqrt (b*b), by rewrite h, by rewrite [*sqrt_eq at aux]; exact aux end nat
fb276fb4ebfdbf6ccc641d5d8b79930802a1fd74
29cc89d6158dd3b90acbdbcab4d2c7eb9a7dbf0f
/Project/mucrl.lean
2fe131155940967e3023c89a86792c46cf82a60d
[]
no_license
KjellZijlemaker/Logical_Verification_VU
ced0ba95316a30e3c94ba8eebd58ea004fa6f53b
4578b93bf1615466996157bb333c84122b201d99
refs/heads/master
1,585,966,086,108
1,549,187,704,000
1,549,187,704,000
155,690,284
0
0
null
null
null
null
UTF-8
Lean
false
false
855
lean
namespace test inductive Set | true : Set | false : Set inductive types | onetype : types | booltype : types | nattype : types | Dtype : types | Frametype : types | bool_Errtype : types | Frame_Errtype : types def type := types β†’ Set def one (t: type) := t types.onetype def bool (t: type) := t types.booltype def nat (t: type) := t types.nattype def D (t: type) := t types.Dtype def Frame (t: type) := t types.Frametype def bool_Err (t: type) := t types.bool_Errtype def Frame_Err (t: type) := t types.Frame_Errtype notation `i` := one notation `true` := bool notation `false` := bool notation `o` := nat notation `S` := nat β†’ nat axiom t1 one (j: one) : j = i axiom t2 bool : true = false axiom t3 bool (b:bool) : b=true ∨ b=false axiom nat_ind (P: nat β†’ Prop) (n:nat) end test
332e49ec293ebce8f9159a0d60f7ec9179138977
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/category/Cat_auto.lean
e458d1e826659fb78d6fe15e9bbe2237f7aab80c
[]
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
2,423
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.concrete_category.default import Mathlib.category_theory.discrete_category import Mathlib.category_theory.eq_to_hom import Mathlib.PostPort universes v u u_1 u_2 u_3 u_4 namespace Mathlib /-! # Category of categories This file contains the definition of the category `Cat` of all categories. In this category objects are categories and morphisms are functors between these categories. ## Implementation notes Though `Cat` is not a concrete category, we use `bundled` to define its carrier type. -/ namespace category_theory /-- Category of categories. -/ def Cat := bundled category namespace Cat protected instance inhabited : Inhabited Cat := { default := bundled.mk (Type u) } protected instance has_coe_to_sort : has_coe_to_sort Cat := has_coe_to_sort.mk (Type u) bundled.Ξ± protected instance str (C : Cat) : category β†₯C := bundled.str C /-- Construct a bundled `Cat` from the underlying type and the typeclass. -/ def of (C : Type u) [category C] : Cat := bundled.of C /-- Category structure on `Cat` -/ protected instance category : large_category Cat := category.mk /-- Functor that gets the set of objects of a category. It is not called `forget`, because it is not a faithful functor. -/ def objects : Cat β₯€ Type u := functor.mk (fun (C : Cat) => β†₯C) fun (C D : Cat) (F : C ⟢ D) => functor.obj F /-- Any isomorphism in `Cat` induces an equivalence of the underlying categories. -/ def equiv_of_iso {C : Cat} {D : Cat} (Ξ³ : C β‰… D) : β†₯C β‰Œ β†₯D := equivalence.mk' (iso.hom Ξ³) (iso.inv Ξ³) (eq_to_iso sorry) (eq_to_iso (iso.inv_hom_id Ξ³)) end Cat /-- Embedding `Type` into `Cat` as discrete categories. This ought to be modelled as a 2-functor! -/ @[simp] theorem Type_to_Cat_obj (X : Type u) : functor.obj Type_to_Cat X = Cat.of (discrete X) := Eq.refl (functor.obj Type_to_Cat X) protected instance Type_to_Cat.faithful : faithful Type_to_Cat := faithful.mk protected instance Type_to_Cat.full : full Type_to_Cat := full.mk fun (X Y : Type (max (max (max u_1 u_2 u_3 u_4) u_1 u_2 u_3) (max u_1 u_2 u_3 u_4) u_1 u_2)) (F : functor.obj Type_to_Cat X ⟢ functor.obj Type_to_Cat Y) => functor.obj F end Mathlib
34d604aa12f24bbda6b78b9fed0a9b68ec315b84
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/constructions/pi.lean
cf65cef2c62d15780420d42077a1155ea7db2543
[ "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
31,404
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import measure_theory.constructions.prod import measure_theory.group.measure /-! # Product measures In this file we define and prove properties about finite products of measures (and at some point, countable products of measures). ## Main definition * `measure_theory.measure.pi`: The product of finitely many Οƒ-finite measures. Given `ΞΌ : Ξ  i : ΞΉ, measure (Ξ± i)` for `[fintype ΞΉ]` it has type `measure (Ξ  i : ΞΉ, Ξ± i)`. To apply Fubini along some subset of the variables, use `measure_theory.measure_preserving_pi_equiv_pi_subtype_prod` to reduce to the situation of a product of two measures: this lemma states that the bijection `measurable_equiv.pi_equiv_pi_subtype_prod Ξ± p` between `(Ξ  i : ΞΉ, Ξ± i)` and `(Ξ  i : {i // p i}, Ξ± i) Γ— (Ξ  i : {i // Β¬ p i}, Ξ± i)` maps a product measure to a direct product of product measures, to which one can apply the usual Fubini for direct product of measures. ## Implementation Notes We define `measure_theory.outer_measure.pi`, the product of finitely many outer measures, as the maximal outer measure `n` with the property that `n (pi univ s) ≀ ∏ i, m i (s i)`, where `pi univ s` is the product of the sets `{s i | i : ΞΉ}`. We then show that this induces a product of measures, called `measure_theory.measure.pi`. For a collection of Οƒ-finite measures `ΞΌ` and a collection of measurable sets `s` we show that `measure.pi ΞΌ (pi univ s) = ∏ i, m i (s i)`. To do this, we follow the following steps: * We know that there is some ordering on `ΞΉ`, given by an element of `[encodable ΞΉ]`. * Using this, we have an equivalence `measurable_equiv.pi_measurable_equiv_tprod` between `Ξ  ΞΉ, Ξ± i` and an iterated product of `Ξ± i`, called `list.tprod Ξ± l` for some list `l`. * On this iterated product we can easily define a product measure `measure_theory.measure.tprod` by iterating `measure_theory.measure.prod` * Using the previous two steps we construct `measure_theory.measure.pi'` on `Ξ  ΞΉ, Ξ± i` for encodable `ΞΉ`. * We know that `measure_theory.measure.pi'` sends products of sets to products of measures, and since `measure_theory.measure.pi` is the maximal such measure (or at least, it comes from an outer measure which is the maximal such outer measure), we get the same rule for `measure_theory.measure.pi`. ## Tags finitary product measure -/ noncomputable theory open function set measure_theory.outer_measure filter measurable_space encodable open_locale classical big_operators topological_space ennreal universes u v variables {ΞΉ ΞΉ' : Type*} {Ξ± : ΞΉ β†’ Type*} /-! We start with some measurability properties -/ /-- Boxes formed by Ο€-systems form a Ο€-system. -/ lemma is_pi_system.pi {C : Ξ  i, set (set (Ξ± i))} (hC : βˆ€ i, is_pi_system (C i)) : is_pi_system (pi univ '' pi univ C) := begin rintro _ ⟨s₁, hs₁, rfl⟩ _ ⟨sβ‚‚, hsβ‚‚, rfl⟩ hst, rw [← pi_inter_distrib] at hst ⊒, rw [univ_pi_nonempty_iff] at hst, exact mem_image_of_mem _ (Ξ» i _, hC i _ (hs₁ i (mem_univ i)) _ (hsβ‚‚ i (mem_univ i)) (hst i)) end /-- Boxes form a Ο€-system. -/ lemma is_pi_system_pi [Ξ  i, measurable_space (Ξ± i)] : is_pi_system (pi univ '' pi univ (Ξ» i, {s : set (Ξ± i) | measurable_set s})) := is_pi_system.pi (Ξ» i, is_pi_system_measurable_set) variables [fintype ΞΉ] [fintype ΞΉ'] /-- Boxes of countably spanning sets are countably spanning. -/ lemma is_countably_spanning.pi {C : Ξ  i, set (set (Ξ± i))} (hC : βˆ€ i, is_countably_spanning (C i)) : is_countably_spanning (pi univ '' pi univ C) := begin choose s h1s h2s using hC, haveI := fintype.to_encodable ΞΉ, let e : β„• β†’ (ΞΉ β†’ β„•) := Ξ» n, (decode (ΞΉ β†’ β„•) n).iget, refine ⟨λ n, pi univ (Ξ» i, s i (e n i)), Ξ» n, mem_image_of_mem _ (Ξ» i _, h1s i _), _⟩, simp_rw [(surjective_decode_iget (ΞΉ β†’ β„•)).Union_comp (Ξ» x, pi univ (Ξ» i, s i (x i))), Union_univ_pi s, h2s, pi_univ] end /-- The product of generated Οƒ-algebras is the one generated by boxes, if both generating sets are countably spanning. -/ lemma generate_from_pi_eq {C : Ξ  i, set (set (Ξ± i))} (hC : βˆ€ i, is_countably_spanning (C i)) : @measurable_space.pi _ _ (Ξ» i, generate_from (C i)) = generate_from (pi univ '' pi univ C) := begin haveI := fintype.to_encodable ΞΉ, apply le_antisymm, { refine supr_le _, intro i, rw [comap_generate_from], apply generate_from_le, rintro _ ⟨s, hs, rfl⟩, dsimp, choose t h1t h2t using hC, simp_rw [eval_preimage, ← h2t], rw [← @Union_const _ β„• _ s], have : (pi univ (update (Ξ» (i' : ΞΉ), Union (t i')) i (⋃ (i' : β„•), s))) = (pi univ (Ξ» k, ⋃ j : β„•, @update ΞΉ (Ξ» i', set (Ξ± i')) _ (Ξ» i', t i' j) i s k)), { ext, simp_rw [mem_univ_pi], apply forall_congr, intro i', by_cases (i' = i), { subst h, simp }, { rw [← ne.def] at h, simp [h] }}, rw [this, ← Union_univ_pi], apply measurable_set.Union, intro n, apply measurable_set_generate_from, apply mem_image_of_mem, intros j _, dsimp only, by_cases h: j = i, subst h, rwa [update_same], rw [update_noteq h], apply h1t }, { apply generate_from_le, rintro _ ⟨s, hs, rfl⟩, rw [univ_pi_eq_Inter], apply measurable_set.Inter, intro i, apply measurable_pi_apply, exact measurable_set_generate_from (hs i (mem_univ i)) } end /-- If `C` and `D` generate the Οƒ-algebras on `Ξ±` resp. `Ξ²`, then rectangles formed by `C` and `D` generate the Οƒ-algebra on `Ξ± Γ— Ξ²`. -/ lemma generate_from_eq_pi [h : Ξ  i, measurable_space (Ξ± i)] {C : Ξ  i, set (set (Ξ± i))} (hC : βˆ€ i, generate_from (C i) = h i) (h2C : βˆ€ i, is_countably_spanning (C i)) : generate_from (pi univ '' pi univ C) = measurable_space.pi := by rw [← funext hC, generate_from_pi_eq h2C] /-- The product Οƒ-algebra is generated from boxes, i.e. `s Γ—Λ’ t` for sets `s : set Ξ±` and `t : set Ξ²`. -/ lemma generate_from_pi [Ξ  i, measurable_space (Ξ± i)] : generate_from (pi univ '' pi univ (Ξ» i, { s : set (Ξ± i) | measurable_set s})) = measurable_space.pi := generate_from_eq_pi (Ξ» i, generate_from_measurable_set) (Ξ» i, is_countably_spanning_measurable_set) namespace measure_theory variables {m : Ξ  i, outer_measure (Ξ± i)} /-- An upper bound for the measure in a finite product space. It is defined to by taking the image of the set under all projections, and taking the product of the measures of these images. For measurable boxes it is equal to the correct measure. -/ @[simp] def pi_premeasure (m : Ξ  i, outer_measure (Ξ± i)) (s : set (Ξ  i, Ξ± i)) : ℝβ‰₯0∞ := ∏ i, m i (eval i '' s) lemma pi_premeasure_pi {s : Ξ  i, set (Ξ± i)} (hs : (pi univ s).nonempty) : pi_premeasure m (pi univ s) = ∏ i, m i (s i) := by simp [hs] lemma pi_premeasure_pi' {s : Ξ  i, set (Ξ± i)} : pi_premeasure m (pi univ s) = ∏ i, m i (s i) := begin casesI is_empty_or_nonempty ΞΉ, { simp, }, cases (pi univ s).eq_empty_or_nonempty with h h, { rcases univ_pi_eq_empty_iff.mp h with ⟨i, hi⟩, have : βˆƒ i, m i (s i) = 0 := ⟨i, by simp [hi]⟩, simpa [h, finset.card_univ, zero_pow (fintype.card_pos_iff.mpr β€Ή_β€Ί), @eq_comm _ (0 : ℝβ‰₯0∞), finset.prod_eq_zero_iff] }, { simp [h] } end lemma pi_premeasure_pi_mono {s t : set (Ξ  i, Ξ± i)} (h : s βŠ† t) : pi_premeasure m s ≀ pi_premeasure m t := finset.prod_le_prod' (Ξ» i _, (m i).mono' (image_subset _ h)) lemma pi_premeasure_pi_eval {s : set (Ξ  i, Ξ± i)} : pi_premeasure m (pi univ (Ξ» i, eval i '' s)) = pi_premeasure m s := by simp [pi_premeasure_pi'] namespace outer_measure /-- `outer_measure.pi m` is the finite product of the outer measures `{m i | i : ΞΉ}`. It is defined to be the maximal outer measure `n` with the property that `n (pi univ s) ≀ ∏ i, m i (s i)`, where `pi univ s` is the product of the sets `{s i | i : ΞΉ}`. -/ protected def pi (m : Ξ  i, outer_measure (Ξ± i)) : outer_measure (Ξ  i, Ξ± i) := bounded_by (pi_premeasure m) lemma pi_pi_le (m : Ξ  i, outer_measure (Ξ± i)) (s : Ξ  i, set (Ξ± i)) : outer_measure.pi m (pi univ s) ≀ ∏ i, m i (s i) := by { cases (pi univ s).eq_empty_or_nonempty with h h, simp [h], exact (bounded_by_le _).trans_eq (pi_premeasure_pi h) } lemma le_pi {m : Ξ  i, outer_measure (Ξ± i)} {n : outer_measure (Ξ  i, Ξ± i)} : n ≀ outer_measure.pi m ↔ βˆ€ (s : Ξ  i, set (Ξ± i)), (pi univ s).nonempty β†’ n (pi univ s) ≀ ∏ i, m i (s i) := begin rw [outer_measure.pi, le_bounded_by'], split, { intros h s hs, refine (h _ hs).trans_eq (pi_premeasure_pi hs) }, { intros h s hs, refine le_trans (n.mono $ subset_pi_eval_image univ s) (h _ _), simp [univ_pi_nonempty_iff, hs] } end end outer_measure namespace measure variables [Ξ  i, measurable_space (Ξ± i)] (ΞΌ : Ξ  i, measure (Ξ± i)) section tprod open list variables {Ξ΄ : Type*} {Ο€ : Ξ΄ β†’ Type*} [βˆ€ x, measurable_space (Ο€ x)] /-- A product of measures in `tprod Ξ± l`. -/ -- for some reason the equation compiler doesn't like this definition protected def tprod (l : list Ξ΄) (ΞΌ : Ξ  i, measure (Ο€ i)) : measure (tprod Ο€ l) := by { induction l with i l ih, exact dirac punit.star, exact (ΞΌ i).prod ih } @[simp] lemma tprod_nil (ΞΌ : Ξ  i, measure (Ο€ i)) : measure.tprod [] ΞΌ = dirac punit.star := rfl @[simp] lemma tprod_cons (i : Ξ΄) (l : list Ξ΄) (ΞΌ : Ξ  i, measure (Ο€ i)) : measure.tprod (i :: l) ΞΌ = (ΞΌ i).prod (measure.tprod l ΞΌ) := rfl instance sigma_finite_tprod (l : list Ξ΄) (ΞΌ : Ξ  i, measure (Ο€ i)) [βˆ€ i, sigma_finite (ΞΌ i)] : sigma_finite (measure.tprod l ΞΌ) := begin induction l with i l ih, { rw [tprod_nil], apply_instance }, { rw [tprod_cons], resetI, apply_instance } end lemma tprod_tprod (l : list Ξ΄) (ΞΌ : Ξ  i, measure (Ο€ i)) [βˆ€ i, sigma_finite (ΞΌ i)] (s : Ξ  i, set (Ο€ i)) : measure.tprod l ΞΌ (set.tprod l s) = (l.map (Ξ» i, (ΞΌ i) (s i))).prod := begin induction l with i l ih, { simp }, rw [tprod_cons, set.tprod, prod_prod, map_cons, prod_cons, ih] end end tprod section encodable open list measurable_equiv variables [encodable ΞΉ] /-- The product measure on an encodable finite type, defined by mapping `measure.tprod` along the equivalence `measurable_equiv.pi_measurable_equiv_tprod`. The definition `measure_theory.measure.pi` should be used instead of this one. -/ def pi' : measure (Ξ  i, Ξ± i) := measure.map (tprod.elim' mem_sorted_univ) (measure.tprod (sorted_univ ΞΉ) ΞΌ) lemma pi'_pi [βˆ€ i, sigma_finite (ΞΌ i)] (s : Ξ  i, set (Ξ± i)) : pi' ΞΌ (pi univ s) = ∏ i, ΞΌ i (s i) := by rw [pi', ← measurable_equiv.pi_measurable_equiv_tprod_symm_apply, measurable_equiv.map_apply, measurable_equiv.pi_measurable_equiv_tprod_symm_apply, elim_preimage_pi, tprod_tprod _ ΞΌ, ← list.prod_to_finset, sorted_univ_to_finset]; exact sorted_univ_nodup ΞΉ end encodable lemma pi_caratheodory : measurable_space.pi ≀ (outer_measure.pi (Ξ» i, (ΞΌ i).to_outer_measure)).caratheodory := begin refine supr_le _, intros i s hs, rw [measurable_space.comap] at hs, rcases hs with ⟨s, hs, rfl⟩, apply bounded_by_caratheodory, intro t, simp_rw [pi_premeasure], refine finset.prod_add_prod_le' (finset.mem_univ i) _ _ _, { simp [image_inter_preimage, image_diff_preimage, measure_inter_add_diff _ hs, le_refl] }, { rintro j - hj, apply mono', apply image_subset, apply inter_subset_left }, { rintro j - hj, apply mono', apply image_subset, apply diff_subset } end /-- `measure.pi ΞΌ` is the finite product of the measures `{ΞΌ i | i : ΞΉ}`. It is defined to be measure corresponding to `measure_theory.outer_measure.pi`. -/ @[irreducible] protected def pi : measure (Ξ  i, Ξ± i) := to_measure (outer_measure.pi (Ξ» i, (ΞΌ i).to_outer_measure)) (pi_caratheodory ΞΌ) lemma pi_pi_aux [βˆ€ i, sigma_finite (ΞΌ i)] (s : Ξ  i, set (Ξ± i)) (hs : βˆ€ i, measurable_set (s i)) : measure.pi ΞΌ (pi univ s) = ∏ i, ΞΌ i (s i) := begin refine le_antisymm _ _, { rw [measure.pi, to_measure_apply _ _ (measurable_set.pi_fintype (Ξ» i _, hs i))], apply outer_measure.pi_pi_le }, { haveI : encodable ΞΉ := fintype.to_encodable ΞΉ, rw [← pi'_pi ΞΌ s], simp_rw [← pi'_pi ΞΌ s, measure.pi, to_measure_apply _ _ (measurable_set.pi_fintype (Ξ» i _, hs i)), ← to_outer_measure_apply], suffices : (pi' ΞΌ).to_outer_measure ≀ outer_measure.pi (Ξ» i, (ΞΌ i).to_outer_measure), { exact this _ }, clear hs s, rw [outer_measure.le_pi], intros s hs, simp_rw [to_outer_measure_apply], exact (pi'_pi ΞΌ s).le } end variable {ΞΌ} /-- `measure.pi ΞΌ` has finite spanning sets in rectangles of finite spanning sets. -/ def finite_spanning_sets_in.pi {C : Ξ  i, set (set (Ξ± i))} (hΞΌ : βˆ€ i, (ΞΌ i).finite_spanning_sets_in (C i)) : (measure.pi ΞΌ).finite_spanning_sets_in (pi univ '' pi univ C) := begin haveI := Ξ» i, (hΞΌ i).sigma_finite, haveI := fintype.to_encodable ΞΉ, refine ⟨λ n, pi univ (Ξ» i, (hΞΌ i).set ((decode (ΞΉ β†’ β„•) n).iget i)), Ξ» n, _, Ξ» n, _, _⟩; -- TODO (kmill) If this let comes before the refine, while the noncomputability checker -- correctly sees this definition is computable, the Lean VM fails to see the binding is -- computationally irrelevant. The `noncomputable theory` doesn't help because all it does -- is insert `noncomputable` for you when necessary. let e : β„• β†’ (ΞΉ β†’ β„•) := Ξ» n, (decode (ΞΉ β†’ β„•) n).iget, { refine mem_image_of_mem _ (Ξ» i _, (hΞΌ i).set_mem _) }, { calc measure.pi ΞΌ (pi univ (Ξ» i, (hΞΌ i).set (e n i))) ≀ measure.pi ΞΌ (pi univ (Ξ» i, to_measurable (ΞΌ i) ((hΞΌ i).set (e n i)))) : measure_mono (pi_mono $ Ξ» i hi, subset_to_measurable _ _) ... = ∏ i, ΞΌ i (to_measurable (ΞΌ i) ((hΞΌ i).set (e n i))) : pi_pi_aux ΞΌ _ (Ξ» i, measurable_set_to_measurable _ _) ... = ∏ i, ΞΌ i ((hΞΌ i).set (e n i)) : by simp only [measure_to_measurable] ... < ∞ : ennreal.prod_lt_top (Ξ» i hi, ((hΞΌ i).finite _).ne) }, { simp_rw [(surjective_decode_iget (ΞΉ β†’ β„•)).Union_comp (Ξ» x, pi univ (Ξ» i, (hΞΌ i).set (x i))), Union_univ_pi (Ξ» i, (hΞΌ i).set), (hΞΌ _).spanning, set.pi_univ] } end /-- A measure on a finite product space equals the product measure if they are equal on rectangles with as sides sets that generate the corresponding Οƒ-algebras. -/ lemma pi_eq_generate_from {C : Ξ  i, set (set (Ξ± i))} (hC : βˆ€ i, generate_from (C i) = _inst_3 i) (h2C : βˆ€ i, is_pi_system (C i)) (h3C : βˆ€ i, (ΞΌ i).finite_spanning_sets_in (C i)) {ΞΌΞ½ : measure (Ξ  i, Ξ± i)} (h₁ : βˆ€ s : Ξ  i, set (Ξ± i), (βˆ€ i, s i ∈ C i) β†’ ΞΌΞ½ (pi univ s) = ∏ i, ΞΌ i (s i)) : measure.pi ΞΌ = ΞΌΞ½ := begin have h4C : βˆ€ i (s : set (Ξ± i)), s ∈ C i β†’ measurable_set s, { intros i s hs, rw [← hC], exact measurable_set_generate_from hs }, refine (finite_spanning_sets_in.pi h3C).ext (generate_from_eq_pi hC (Ξ» i, (h3C i).is_countably_spanning)).symm (is_pi_system.pi h2C) _, rintro _ ⟨s, hs, rfl⟩, rw [mem_univ_pi] at hs, haveI := Ξ» i, (h3C i).sigma_finite, simp_rw [h₁ s hs, pi_pi_aux ΞΌ s (Ξ» i, h4C i _ (hs i))] end variables [βˆ€ i, sigma_finite (ΞΌ i)] /-- A measure on a finite product space equals the product measure if they are equal on rectangles. -/ lemma pi_eq {ΞΌ' : measure (Ξ  i, Ξ± i)} (h : βˆ€ s : Ξ  i, set (Ξ± i), (βˆ€ i, measurable_set (s i)) β†’ ΞΌ' (pi univ s) = ∏ i, ΞΌ i (s i)) : measure.pi ΞΌ = ΞΌ' := pi_eq_generate_from (Ξ» i, generate_from_measurable_set) (Ξ» i, is_pi_system_measurable_set) (Ξ» i, (ΞΌ i).to_finite_spanning_sets_in) h variables (ΞΌ) lemma pi'_eq_pi [encodable ΞΉ] : pi' ΞΌ = measure.pi ΞΌ := eq.symm $ pi_eq $ Ξ» s hs, pi'_pi ΞΌ s @[simp] lemma pi_pi (s : Ξ  i, set (Ξ± i)) : measure.pi ΞΌ (pi univ s) = ∏ i, ΞΌ i (s i) := begin haveI : encodable ΞΉ := fintype.to_encodable ΞΉ, rw [← pi'_eq_pi, pi'_pi] end lemma pi_univ : measure.pi ΞΌ univ = ∏ i, ΞΌ i univ := by rw [← pi_univ, pi_pi ΞΌ] lemma pi_ball [βˆ€ i, metric_space (Ξ± i)] (x : Ξ  i, Ξ± i) {r : ℝ} (hr : 0 < r) : measure.pi ΞΌ (metric.ball x r) = ∏ i, ΞΌ i (metric.ball (x i) r) := by rw [ball_pi _ hr, pi_pi] lemma pi_closed_ball [βˆ€ i, metric_space (Ξ± i)] (x : Ξ  i, Ξ± i) {r : ℝ} (hr : 0 ≀ r) : measure.pi ΞΌ (metric.closed_ball x r) = ∏ i, ΞΌ i (metric.closed_ball (x i) r) := by rw [closed_ball_pi _ hr, pi_pi] instance pi.sigma_finite : sigma_finite (measure.pi ΞΌ) := (finite_spanning_sets_in.pi (Ξ» i, (ΞΌ i).to_finite_spanning_sets_in)).sigma_finite lemma pi_of_empty {Ξ± : Type*} [is_empty Ξ±] {Ξ² : Ξ± β†’ Type*} {m : Ξ  a, measurable_space (Ξ² a)} (ΞΌ : Ξ  a : Ξ±, measure (Ξ² a)) (x : Ξ  a, Ξ² a := is_empty_elim) : measure.pi ΞΌ = dirac x := begin haveI : βˆ€ a, sigma_finite (ΞΌ a) := is_empty_elim, refine pi_eq (Ξ» s hs, _), rw [fintype.prod_empty, dirac_apply_of_mem], exact is_empty_elim end lemma pi_eval_preimage_null {i : ΞΉ} {s : set (Ξ± i)} (hs : ΞΌ i s = 0) : measure.pi ΞΌ (eval i ⁻¹' s) = 0 := begin /- WLOG, `s` is measurable -/ rcases exists_measurable_superset_of_null hs with ⟨t, hst, htm, hΞΌt⟩, suffices : measure.pi ΞΌ (eval i ⁻¹' t) = 0, from measure_mono_null (preimage_mono hst) this, clear_dependent s, /- Now rewrite it as `set.pi`, and apply `pi_pi` -/ rw [← univ_pi_update_univ, pi_pi], apply finset.prod_eq_zero (finset.mem_univ i), simp [hΞΌt] end lemma pi_hyperplane (i : ΞΉ) [has_no_atoms (ΞΌ i)] (x : Ξ± i) : measure.pi ΞΌ {f : Ξ  i, Ξ± i | f i = x} = 0 := show measure.pi ΞΌ (eval i ⁻¹' {x}) = 0, from pi_eval_preimage_null _ (measure_singleton x) lemma ae_eval_ne (i : ΞΉ) [has_no_atoms (ΞΌ i)] (x : Ξ± i) : βˆ€α΅ y : Ξ  i, Ξ± i βˆ‚measure.pi ΞΌ, y i β‰  x := compl_mem_ae_iff.2 (pi_hyperplane ΞΌ i x) variable {ΞΌ} lemma tendsto_eval_ae_ae {i : ΞΉ} : tendsto (eval i) (measure.pi ΞΌ).ae (ΞΌ i).ae := Ξ» s hs, pi_eval_preimage_null ΞΌ hs lemma ae_pi_le_pi : (measure.pi ΞΌ).ae ≀ filter.pi (Ξ» i, (ΞΌ i).ae) := le_infi $ Ξ» i, tendsto_eval_ae_ae.le_comap lemma ae_eq_pi {Ξ² : ΞΉ β†’ Type*} {f f' : Ξ  i, Ξ± i β†’ Ξ² i} (h : βˆ€ i, f i =ᡐ[ΞΌ i] f' i) : (Ξ» (x : Ξ  i, Ξ± i) i, f i (x i)) =ᡐ[measure.pi ΞΌ] (Ξ» x i, f' i (x i)) := (eventually_all.2 (Ξ» i, tendsto_eval_ae_ae.eventually (h i))).mono $ Ξ» x hx, funext hx lemma ae_le_pi {Ξ² : ΞΉ β†’ Type*} [Ξ  i, preorder (Ξ² i)] {f f' : Ξ  i, Ξ± i β†’ Ξ² i} (h : βˆ€ i, f i ≀ᡐ[ΞΌ i] f' i) : (Ξ» (x : Ξ  i, Ξ± i) i, f i (x i)) ≀ᡐ[measure.pi ΞΌ] (Ξ» x i, f' i (x i)) := (eventually_all.2 (Ξ» i, tendsto_eval_ae_ae.eventually (h i))).mono $ Ξ» x hx, hx lemma ae_le_set_pi {I : set ΞΉ} {s t : Ξ  i, set (Ξ± i)} (h : βˆ€ i ∈ I, s i ≀ᡐ[ΞΌ i] t i) : (set.pi I s) ≀ᡐ[measure.pi ΞΌ] (set.pi I t) := ((eventually_all_finite (finite.of_fintype I)).2 (Ξ» i hi, tendsto_eval_ae_ae.eventually (h i hi))).mono $ Ξ» x hst hx i hi, hst i hi $ hx i hi lemma ae_eq_set_pi {I : set ΞΉ} {s t : Ξ  i, set (Ξ± i)} (h : βˆ€ i ∈ I, s i =ᡐ[ΞΌ i] t i) : (set.pi I s) =ᡐ[measure.pi ΞΌ] (set.pi I t) := (ae_le_set_pi (Ξ» i hi, (h i hi).le)).antisymm (ae_le_set_pi (Ξ» i hi, (h i hi).symm.le)) section intervals variables {ΞΌ} [Ξ  i, partial_order (Ξ± i)] [βˆ€ i, has_no_atoms (ΞΌ i)] lemma pi_Iio_ae_eq_pi_Iic {s : set ΞΉ} {f : Ξ  i, Ξ± i} : pi s (Ξ» i, Iio (f i)) =ᡐ[measure.pi ΞΌ] pi s (Ξ» i, Iic (f i)) := ae_eq_set_pi $ Ξ» i hi, Iio_ae_eq_Iic lemma pi_Ioi_ae_eq_pi_Ici {s : set ΞΉ} {f : Ξ  i, Ξ± i} : pi s (Ξ» i, Ioi (f i)) =ᡐ[measure.pi ΞΌ] pi s (Ξ» i, Ici (f i)) := ae_eq_set_pi $ Ξ» i hi, Ioi_ae_eq_Ici lemma univ_pi_Iio_ae_eq_Iic {f : Ξ  i, Ξ± i} : pi univ (Ξ» i, Iio (f i)) =ᡐ[measure.pi ΞΌ] Iic f := by { rw ← pi_univ_Iic, exact pi_Iio_ae_eq_pi_Iic } lemma univ_pi_Ioi_ae_eq_Ici {f : Ξ  i, Ξ± i} : pi univ (Ξ» i, Ioi (f i)) =ᡐ[measure.pi ΞΌ] Ici f := by { rw ← pi_univ_Ici, exact pi_Ioi_ae_eq_pi_Ici } lemma pi_Ioo_ae_eq_pi_Icc {s : set ΞΉ} {f g : Ξ  i, Ξ± i} : pi s (Ξ» i, Ioo (f i) (g i)) =ᡐ[measure.pi ΞΌ] pi s (Ξ» i, Icc (f i) (g i)) := ae_eq_set_pi $ Ξ» i hi, Ioo_ae_eq_Icc lemma pi_Ioo_ae_eq_pi_Ioc {s : set ΞΉ} {f g : Ξ  i, Ξ± i} : pi s (Ξ» i, Ioo (f i) (g i)) =ᡐ[measure.pi ΞΌ] pi s (Ξ» i, Ioc (f i) (g i)) := ae_eq_set_pi $ Ξ» i hi, Ioo_ae_eq_Ioc lemma univ_pi_Ioo_ae_eq_Icc {f g : Ξ  i, Ξ± i} : pi univ (Ξ» i, Ioo (f i) (g i)) =ᡐ[measure.pi ΞΌ] Icc f g := by { rw ← pi_univ_Icc, exact pi_Ioo_ae_eq_pi_Icc } lemma pi_Ioc_ae_eq_pi_Icc {s : set ΞΉ} {f g : Ξ  i, Ξ± i} : pi s (Ξ» i, Ioc (f i) (g i)) =ᡐ[measure.pi ΞΌ] pi s (Ξ» i, Icc (f i) (g i)) := ae_eq_set_pi $ Ξ» i hi, Ioc_ae_eq_Icc lemma univ_pi_Ioc_ae_eq_Icc {f g : Ξ  i, Ξ± i} : pi univ (Ξ» i, Ioc (f i) (g i)) =ᡐ[measure.pi ΞΌ] Icc f g := by { rw ← pi_univ_Icc, exact pi_Ioc_ae_eq_pi_Icc } lemma pi_Ico_ae_eq_pi_Icc {s : set ΞΉ} {f g : Ξ  i, Ξ± i} : pi s (Ξ» i, Ico (f i) (g i)) =ᡐ[measure.pi ΞΌ] pi s (Ξ» i, Icc (f i) (g i)) := ae_eq_set_pi $ Ξ» i hi, Ico_ae_eq_Icc lemma univ_pi_Ico_ae_eq_Icc {f g : Ξ  i, Ξ± i} : pi univ (Ξ» i, Ico (f i) (g i)) =ᡐ[measure.pi ΞΌ] Icc f g := by { rw ← pi_univ_Icc, exact pi_Ico_ae_eq_pi_Icc } end intervals /-- If one of the measures `ΞΌ i` has no atoms, them `measure.pi Β΅` has no atoms. The instance below assumes that all `ΞΌ i` have no atoms. -/ lemma pi_has_no_atoms (i : ΞΉ) [has_no_atoms (ΞΌ i)] : has_no_atoms (measure.pi ΞΌ) := ⟨λ x, flip measure_mono_null (pi_hyperplane ΞΌ i (x i)) (singleton_subset_iff.2 rfl)⟩ instance [h : nonempty ΞΉ] [βˆ€ i, has_no_atoms (ΞΌ i)] : has_no_atoms (measure.pi ΞΌ) := h.elim $ Ξ» i, pi_has_no_atoms i instance [Ξ  i, topological_space (Ξ± i)] [βˆ€ i, is_locally_finite_measure (ΞΌ i)] : is_locally_finite_measure (measure.pi ΞΌ) := begin refine ⟨λ x, _⟩, choose s hxs ho hΞΌ using Ξ» i, (ΞΌ i).exists_is_open_measure_lt_top (x i), refine ⟨pi univ s, set_pi_mem_nhds finite_univ (Ξ» i hi, is_open.mem_nhds (ho i) (hxs i)), _⟩, rw [pi_pi], exact ennreal.prod_lt_top (Ξ» i _, (hΞΌ i).ne) end variable (ΞΌ) @[to_additive] instance pi.is_mul_left_invariant [βˆ€ i, group (Ξ± i)] [βˆ€ i, has_measurable_mul (Ξ± i)] [βˆ€ i, is_mul_left_invariant (ΞΌ i)] : is_mul_left_invariant (measure.pi ΞΌ) := begin refine ⟨λ x, (measure.pi_eq (Ξ» s hs, _)).symm⟩, have h : has_mul.mul x ⁻¹' (pi univ s) = set.pi univ (Ξ» i, (Ξ» y, x i * y) ⁻¹' s i), { ext, simp }, simp_rw [measure.map_apply (measurable_const_mul x) (measurable_set.univ_pi_fintype hs), h, pi_pi, measure_preimage_mul] end @[to_additive] instance pi.is_inv_invariant [βˆ€ i, group (Ξ± i)] [βˆ€ i, has_measurable_inv (Ξ± i)] [βˆ€ i, is_inv_invariant (ΞΌ i)] : is_inv_invariant (measure.pi ΞΌ) := begin refine ⟨(measure.pi_eq (Ξ» s hs, _)).symm⟩, have A : has_inv.inv ⁻¹' (pi univ s) = set.pi univ (Ξ» i, has_inv.inv ⁻¹' s i), { ext, simp }, simp_rw [measure.inv, measure.map_apply measurable_inv (measurable_set.univ_pi_fintype hs), A, pi_pi, measure_preimage_inv] end end measure instance measure_space.pi [Ξ  i, measure_space (Ξ± i)] : measure_space (Ξ  i, Ξ± i) := ⟨measure.pi (Ξ» i, volume)⟩ lemma volume_pi [Ξ  i, measure_space (Ξ± i)] : (volume : measure (Ξ  i, Ξ± i)) = measure.pi (Ξ» i, volume) := rfl lemma volume_pi_pi [Ξ  i, measure_space (Ξ± i)] [βˆ€ i, sigma_finite (volume : measure (Ξ± i))] (s : Ξ  i, set (Ξ± i)) : volume (pi univ s) = ∏ i, volume (s i) := measure.pi_pi (Ξ» i, volume) s lemma volume_pi_ball [Ξ  i, measure_space (Ξ± i)] [βˆ€ i, sigma_finite (volume : measure (Ξ± i))] [βˆ€ i, metric_space (Ξ± i)] (x : Ξ  i, Ξ± i) {r : ℝ} (hr : 0 < r) : volume (metric.ball x r) = ∏ i, volume (metric.ball (x i) r) := measure.pi_ball _ _ hr lemma volume_pi_closed_ball [Ξ  i, measure_space (Ξ± i)] [βˆ€ i, sigma_finite (volume : measure (Ξ± i))] [βˆ€ i, metric_space (Ξ± i)] (x : Ξ  i, Ξ± i) {r : ℝ} (hr : 0 ≀ r) : volume (metric.closed_ball x r) = ∏ i, volume (metric.closed_ball (x i) r) := measure.pi_closed_ball _ _ hr open measure /-- We intentionally restrict this only to the nondependent function space, since type-class inference cannot find an instance for `ΞΉ β†’ ℝ` when this is stated for dependent function spaces. -/ @[to_additive "We intentionally restrict this only to the nondependent function space, since type-class inference cannot find an instance for `ΞΉ β†’ ℝ` when this is stated for dependent function spaces."] instance pi.is_mul_left_invariant_volume {Ξ±} [group Ξ±] [measure_space Ξ±] [sigma_finite (volume : measure Ξ±)] [has_measurable_mul Ξ±] [is_mul_left_invariant (volume : measure Ξ±)] : is_mul_left_invariant (volume : measure (ΞΉ β†’ Ξ±)) := pi.is_mul_left_invariant _ /-- We intentionally restrict this only to the nondependent function space, since type-class inference cannot find an instance for `ΞΉ β†’ ℝ` when this is stated for dependent function spaces. -/ @[to_additive "We intentionally restrict this only to the nondependent function space, since type-class inference cannot find an instance for `ΞΉ β†’ ℝ` when this is stated for dependent function spaces."] instance pi.is_inv_invariant_volume {Ξ±} [group Ξ±] [measure_space Ξ±] [sigma_finite (volume : measure Ξ±)] [has_measurable_inv Ξ±] [is_inv_invariant (volume : measure Ξ±)] : is_inv_invariant (volume : measure (ΞΉ β†’ Ξ±)) := pi.is_inv_invariant _ /-! ### Measure preserving equivalences In this section we prove that some measurable equivalences (e.g., between `fin 1 β†’ Ξ±` and `Ξ±` or between `fin 2 β†’ Ξ±` and `Ξ± Γ— Ξ±`) preserve measure or volume. These lemmas can be used to prove that measures of corresponding sets (images or preimages) have equal measures and functions `f ∘ e` and `f` have equal integrals, see lemmas in the `measure_theory.measure_preserving` prefix. -/ section measure_preserving lemma measure_preserving_pi_equiv_pi_subtype_prod {ΞΉ : Type u} {Ξ± : ΞΉ β†’ Type v} [fintype ΞΉ] {m : Ξ  i, measurable_space (Ξ± i)} (ΞΌ : Ξ  i, measure (Ξ± i)) [βˆ€ i, sigma_finite (ΞΌ i)] (p : ΞΉ β†’ Prop) [decidable_pred p] : measure_preserving (measurable_equiv.pi_equiv_pi_subtype_prod Ξ± p) (measure.pi ΞΌ) ((measure.pi $ Ξ» i : subtype p, ΞΌ i).prod (measure.pi $ Ξ» i, ΞΌ i)) := begin set e := (measurable_equiv.pi_equiv_pi_subtype_prod Ξ± p).symm, refine measure_preserving.symm e _, refine ⟨e.measurable, (pi_eq $ Ξ» s hs, _).symm⟩, have : e ⁻¹' (pi univ s) = (pi univ (Ξ» i : {i // p i}, s i)) Γ—Λ’ (pi univ (Ξ» i : {i // Β¬p i}, s i)), from equiv.preimage_pi_equiv_pi_subtype_prod_symm_pi p s, rw [e.map_apply, this, prod_prod, pi_pi, pi_pi], exact fintype.prod_subtype_mul_prod_subtype p (Ξ» i, ΞΌ i (s i)) end lemma volume_preserving_pi_equiv_pi_subtype_prod {ΞΉ : Type*} (Ξ± : ΞΉ β†’ Type*) [fintype ΞΉ] [Ξ  i, measure_space (Ξ± i)] [βˆ€ i, sigma_finite (volume : measure (Ξ± i))] (p : ΞΉ β†’ Prop) [decidable_pred p] : measure_preserving (measurable_equiv.pi_equiv_pi_subtype_prod Ξ± p) := measure_preserving_pi_equiv_pi_subtype_prod (Ξ» i, volume) p lemma measure_preserving_pi_fin_succ_above_equiv {n : β„•} {Ξ± : fin (n + 1) β†’ Type u} {m : Ξ  i, measurable_space (Ξ± i)} (ΞΌ : Ξ  i, measure (Ξ± i)) [βˆ€ i, sigma_finite (ΞΌ i)] (i : fin (n + 1)) : measure_preserving (measurable_equiv.pi_fin_succ_above_equiv Ξ± i) (measure.pi ΞΌ) ((ΞΌ i).prod $ measure.pi $ Ξ» j, ΞΌ (i.succ_above j)) := begin set e := (measurable_equiv.pi_fin_succ_above_equiv Ξ± i).symm, refine measure_preserving.symm e _, refine ⟨e.measurable, (pi_eq $ Ξ» s hs, _).symm⟩, rw [e.map_apply, i.prod_univ_succ_above _, ← pi_pi, ← prod_prod], congr' 1 with ⟨x, f⟩, simp [i.forall_iff_succ_above] end lemma volume_preserving_pi_fin_succ_above_equiv {n : β„•} (Ξ± : fin (n + 1) β†’ Type u) [Ξ  i, measure_space (Ξ± i)] [βˆ€ i, sigma_finite (volume : measure (Ξ± i))] (i : fin (n + 1)) : measure_preserving (measurable_equiv.pi_fin_succ_above_equiv Ξ± i) := measure_preserving_pi_fin_succ_above_equiv (Ξ» _, volume) i lemma measure_preserving_fun_unique {Ξ² : Type u} {m : measurable_space Ξ²} (ΞΌ : measure Ξ²) (Ξ± : Type v) [unique Ξ±] : measure_preserving (measurable_equiv.fun_unique Ξ± Ξ²) (measure.pi (Ξ» a : Ξ±, ΞΌ)) ΞΌ := begin set e := measurable_equiv.fun_unique Ξ± Ξ², have : pi_premeasure (Ξ» _ : Ξ±, ΞΌ.to_outer_measure) = measure.map e.symm ΞΌ, { ext1 s, rw [pi_premeasure, fintype.prod_unique, to_outer_measure_apply, e.symm.map_apply], congr' 1, exact e.to_equiv.image_eq_preimage s }, simp only [measure.pi, outer_measure.pi, this, bounded_by_measure, to_outer_measure_to_measure], exact (e.symm.measurable.measure_preserving _).symm e.symm end lemma volume_preserving_fun_unique (Ξ± : Type u) (Ξ² : Type v) [unique Ξ±] [measure_space Ξ²] : measure_preserving (measurable_equiv.fun_unique Ξ± Ξ²) volume volume := measure_preserving_fun_unique volume Ξ± lemma measure_preserving_pi_fin_two {Ξ± : fin 2 β†’ Type u} {m : Ξ  i, measurable_space (Ξ± i)} (ΞΌ : Ξ  i, measure (Ξ± i)) [βˆ€ i, sigma_finite (ΞΌ i)] : measure_preserving (measurable_equiv.pi_fin_two Ξ±) (measure.pi ΞΌ) ((ΞΌ 0).prod (ΞΌ 1)) := begin refine ⟨measurable_equiv.measurable _, (measure.prod_eq $ Ξ» s t hs ht, _).symm⟩, rw [measurable_equiv.map_apply, measurable_equiv.pi_fin_two_apply, fin.preimage_apply_01_prod, measure.pi_pi, fin.prod_univ_two], refl end lemma volume_preserving_pi_fin_two (Ξ± : fin 2 β†’ Type u) [Ξ  i, measure_space (Ξ± i)] [βˆ€ i, sigma_finite (volume : measure (Ξ± i))] : measure_preserving (measurable_equiv.pi_fin_two Ξ±) volume volume := measure_preserving_pi_fin_two _ lemma measure_preserving_fin_two_arrow_vec {Ξ± : Type u} {m : measurable_space Ξ±} (ΞΌ Ξ½ : measure Ξ±) [sigma_finite ΞΌ] [sigma_finite Ξ½] : measure_preserving measurable_equiv.fin_two_arrow (measure.pi ![ΞΌ, Ξ½]) (ΞΌ.prod Ξ½) := begin haveI : βˆ€ i, sigma_finite (![ΞΌ, Ξ½] i) := fin.forall_fin_two.2 βŸ¨β€Ή_β€Ί, β€Ή_β€ΊβŸ©, exact measure_preserving_pi_fin_two _ end lemma measure_preserving_fin_two_arrow {Ξ± : Type u} {m : measurable_space Ξ±} (ΞΌ : measure Ξ±) [sigma_finite ΞΌ] : measure_preserving measurable_equiv.fin_two_arrow (measure.pi (Ξ» _, ΞΌ)) (ΞΌ.prod ΞΌ) := by simpa only [matrix.vec_single_eq_const, matrix.vec_cons_const] using measure_preserving_fin_two_arrow_vec ΞΌ ΞΌ lemma volume_preserving_fin_two_arrow (Ξ± : Type u) [measure_space Ξ±] [sigma_finite (volume : measure Ξ±)] : measure_preserving (@measurable_equiv.fin_two_arrow Ξ± _) volume volume := measure_preserving_fin_two_arrow volume lemma measure_preserving_pi_empty {ΞΉ : Type u} {Ξ± : ΞΉ β†’ Type v} [is_empty ΞΉ] {m : Ξ  i, measurable_space (Ξ± i)} (ΞΌ : Ξ  i, measure (Ξ± i)) : measure_preserving (measurable_equiv.of_unique_of_unique (Ξ  i, Ξ± i) unit) (measure.pi ΞΌ) (measure.dirac ()) := begin set e := (measurable_equiv.of_unique_of_unique (Ξ  i, Ξ± i) unit), refine ⟨e.measurable, _⟩, rw [measure.pi_of_empty, measure.map_dirac e.measurable], refl end lemma volume_preserving_pi_empty {ΞΉ : Type u} (Ξ± : ΞΉ β†’ Type v) [is_empty ΞΉ] [Ξ  i, measure_space (Ξ± i)] : measure_preserving (measurable_equiv.of_unique_of_unique (Ξ  i, Ξ± i) unit) volume volume := measure_preserving_pi_empty (Ξ» _, volume) end measure_preserving end measure_theory
16d17805395ccbdcca6ff16b35187248c73cbbd3
c3f2fcd060adfa2ca29f924839d2d925e8f2c685
/tests/lean/run/univ1.lean
66f4d45923f6f1c5796ce8fcdba1a9653b727dcd
[ "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
644
lean
import logic namespace S1 axiom I : Type definition F (X : Type) : Type := (X β†’ Prop) β†’ Prop axiom unfold.{l} : I.{l} β†’ F I.{l} axiom foldd.{l} : F I.{l} β†’ I.{l} axiom iso1 : βˆ€x, foldd (unfold x) = x end S1 namespace S2 universe u axiom I : Type.{u} definition F (X : Type) : Type := (X β†’ Prop) β†’ Prop axiom unfold : I β†’ F I axiom foldd : F I β†’ I axiom iso1 : βˆ€x, foldd (unfold x) = x end S2 namespace S3 context hypothesis I : Type definition F (X : Type) : Type := (X β†’ Prop) β†’ Prop hypothesis unfold : I β†’ F I hypothesis foldd : F I β†’ I hypothesis iso1 : βˆ€x, foldd (unfold x) = x end end S3
f0a930507e31e221ffa8c7fc3ead9c8ea8bf001d
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/measure_theory/measure/giry_monad.lean
be269394c1034e749a87fbb7c269b7bf529981c9
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,548
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 -/ import measure_theory.integral.lebesgue /-! # The Giry monad Let X be a measurable space. The collection of all measures on X again forms a measurable space. This construction forms a monad on measurable spaces and measurable functions, called the Giry monad. Note that most sources use the term "Giry monad" for the restriction to *probability* measures. Here we include all measures on X. See also `measure_theory/category/Meas.lean`, containing an upgrade of the type-level monad to an honest monad of the functor `Measure : Meas β₯€ Meas`. ## References * <https://ncatlab.org/nlab/show/Giry+monad> ## Tags giry monad -/ noncomputable theory open_locale classical big_operators ennreal open classical set filter variables {Ξ± Ξ² Ξ³ Ξ΄ Ξ΅ : Type*} namespace measure_theory namespace measure variables [measurable_space Ξ±] [measurable_space Ξ²] /-- Measurability structure on `measure`: Measures are measurable w.r.t. all projections -/ instance : measurable_space (measure Ξ±) := ⨆ (s : set Ξ±) (hs : measurable_set s), (borel ℝβ‰₯0∞).comap (λμ, ΞΌ s) lemma measurable_coe {s : set Ξ±} (hs : measurable_set s) : measurable (λμ : measure Ξ±, ΞΌ s) := measurable.of_comap_le $ le_supr_of_le s $ le_supr_of_le hs $ le_refl _ lemma measurable_of_measurable_coe (f : Ξ² β†’ measure Ξ±) (h : βˆ€(s : set Ξ±) (hs : measurable_set s), measurable (Ξ»b, f b s)) : measurable f := measurable.of_le_map $ bsupr_le $ assume s hs, measurable_space.comap_le_iff_le_map.2 $ by rw [measurable_space.map_comp]; exact h s hs lemma measurable_measure {ΞΌ : Ξ± β†’ measure Ξ²} : measurable ΞΌ ↔ βˆ€(s : set Ξ²) (hs : measurable_set s), measurable (Ξ»b, ΞΌ b s) := ⟨λ hΞΌ s hs, (measurable_coe hs).comp hΞΌ, measurable_of_measurable_coe μ⟩ lemma measurable_map (f : Ξ± β†’ Ξ²) (hf : measurable f) : measurable (λμ : measure Ξ±, map f ΞΌ) := measurable_of_measurable_coe _ $ assume s hs, suffices measurable (Ξ» (ΞΌ : measure Ξ±), ΞΌ (f ⁻¹' s)), by simpa [map_apply, hs, hf], measurable_coe (hf hs) lemma measurable_dirac : measurable (measure.dirac : Ξ± β†’ measure Ξ±) := measurable_of_measurable_coe _ $ assume s hs, begin simp only [dirac_apply', hs], exact measurable_one.indicator hs end lemma measurable_lintegral {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : measurable (λμ : measure Ξ±, ∫⁻ x, f x βˆ‚ΞΌ) := begin simp only [lintegral_eq_supr_eapprox_lintegral, hf, simple_func.lintegral], refine measurable_supr (Ξ» n, finset.measurable_sum _ (Ξ» i _, _)), refine measurable.const_mul _ _, exact measurable_coe ((simple_func.eapprox f n).measurable_set_preimage _) end /-- Monadic join on `measure` in the category of measurable spaces and measurable functions. -/ def join (m : measure (measure Ξ±)) : measure Ξ± := measure.of_measurable (Ξ»s hs, ∫⁻ ΞΌ, ΞΌ s βˆ‚m) (by simp) begin assume f hf h, simp [measure_Union h hf], apply lintegral_tsum, assume i, exact measurable_coe (hf i) end @[simp] lemma join_apply {m : measure (measure Ξ±)} : βˆ€{s : set Ξ±}, measurable_set s β†’ join m s = ∫⁻ ΞΌ, ΞΌ s βˆ‚m := measure.of_measurable_apply @[simp] lemma join_zero : (0 : measure (measure Ξ±)).join = 0 := by { ext1 s hs, simp [hs] } lemma measurable_join : measurable (join : measure (measure Ξ±) β†’ measure Ξ±) := measurable_of_measurable_coe _ $ assume s hs, by simp only [join_apply hs]; exact measurable_lintegral (measurable_coe hs) lemma lintegral_join {m : measure (measure Ξ±)} {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : ∫⁻ x, f x βˆ‚(join m) = ∫⁻ ΞΌ, ∫⁻ x, f x βˆ‚ΞΌ βˆ‚m := begin rw [lintegral_eq_supr_eapprox_lintegral hf], have : βˆ€n x, join m (⇑(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) ⁻¹' {x}) = ∫⁻ ΞΌ, ΞΌ ((⇑(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) ⁻¹' {x})) βˆ‚m := assume n x, join_apply (simple_func.measurable_set_preimage _ _), simp only [simple_func.lintegral, this], transitivity, have : βˆ€(s : β„• β†’ finset ℝβ‰₯0∞) (f : β„• β†’ ℝβ‰₯0∞ β†’ measure Ξ± β†’ ℝβ‰₯0∞) (hf : βˆ€n r, measurable (f n r)) (hm : monotone (Ξ»n ΞΌ, βˆ‘ r in s n, r * f n r ΞΌ)), (⨆n:β„•, βˆ‘ r in s n, r * ∫⁻ ΞΌ, f n r ΞΌ βˆ‚m) = ∫⁻ ΞΌ, ⨆n:β„•, βˆ‘ r in s n, r * f n r ΞΌ βˆ‚m, { assume s f hf hm, symmetry, transitivity, apply lintegral_supr, { assume n, exact finset.measurable_sum _ (assume r _, (hf _ _).const_mul _) }, { exact hm }, congr, funext n, transitivity, apply lintegral_finset_sum, { assume r _, exact (hf _ _).const_mul _ }, congr, funext r, apply lintegral_const_mul, exact hf _ _ }, specialize this (Ξ»n, simple_func.range (simple_func.eapprox f n)), specialize this (Ξ»n r ΞΌ, ΞΌ (⇑(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) ⁻¹' {r})), refine this _ _; clear this, { assume n r, apply measurable_coe, exact simple_func.measurable_set_preimage _ _ }, { change monotone (Ξ»n ΞΌ, (simple_func.eapprox f n).lintegral ΞΌ), assume n m h ΞΌ, refine simple_func.lintegral_mono _ (le_refl _), apply simple_func.monotone_eapprox, assumption }, congr, funext ΞΌ, symmetry, apply lintegral_eq_supr_eapprox_lintegral, exact hf end /-- Monadic bind on `measure`, only works in the category of measurable spaces and measurable functions. When the function `f` is not measurable the result is not well defined. -/ def bind (m : measure Ξ±) (f : Ξ± β†’ measure Ξ²) : measure Ξ² := join (map f m) @[simp] lemma bind_zero_left (f : Ξ± β†’ measure Ξ²) : bind 0 f = 0 := by simp [bind] @[simp] lemma bind_zero_right (m : measure Ξ±) : bind m (0 : Ξ± β†’ measure Ξ²) = 0 := begin ext1 s hs, simp only [bind, hs, join_apply, coe_zero, pi.zero_apply], rw [lintegral_map (measurable_coe hs) measurable_zero], simp end @[simp] lemma bind_zero_right' (m : measure Ξ±) : bind m (Ξ» _, 0 : Ξ± β†’ measure Ξ²) = 0 := bind_zero_right m @[simp] lemma bind_apply {m : measure Ξ±} {f : Ξ± β†’ measure Ξ²} {s : set Ξ²} (hs : measurable_set s) (hf : measurable f) : bind m f s = ∫⁻ a, f a s βˆ‚m := by rw [bind, join_apply hs, lintegral_map (measurable_coe hs) hf] lemma measurable_bind' {g : Ξ± β†’ measure Ξ²} (hg : measurable g) : measurable (Ξ»m, bind m g) := measurable_join.comp (measurable_map _ hg) lemma lintegral_bind {m : measure Ξ±} {ΞΌ : Ξ± β†’ measure Ξ²} {f : Ξ² β†’ ℝβ‰₯0∞} (hΞΌ : measurable ΞΌ) (hf : measurable f) : ∫⁻ x, f x βˆ‚ (bind m ΞΌ) = ∫⁻ a, ∫⁻ x, f x βˆ‚(ΞΌ a) βˆ‚m:= (lintegral_join hf).trans (lintegral_map (measurable_lintegral hf) hΞΌ) lemma bind_bind {Ξ³} [measurable_space Ξ³] {m : measure Ξ±} {f : Ξ± β†’ measure Ξ²} {g : Ξ² β†’ measure Ξ³} (hf : measurable f) (hg : measurable g) : bind (bind m f) g = bind m (Ξ»a, bind (f a) g) := measure.ext $ assume s hs, begin rw [bind_apply hs hg, bind_apply hs ((measurable_bind' hg).comp hf), lintegral_bind hf], { congr, funext a, exact (bind_apply hs hg).symm }, exact (measurable_coe hs).comp hg end lemma bind_dirac {f : Ξ± β†’ measure Ξ²} (hf : measurable f) (a : Ξ±) : bind (dirac a) f = f a := measure.ext $ Ξ» s hs, by rw [bind_apply hs hf, lintegral_dirac' a ((measurable_coe hs).comp hf)] lemma dirac_bind {m : measure Ξ±} : bind m dirac = m := measure.ext $ assume s hs, by simp [bind_apply hs measurable_dirac, dirac_apply' _ hs, lintegral_indicator 1 hs] lemma join_eq_bind (ΞΌ : measure (measure Ξ±)) : join ΞΌ = bind ΞΌ id := by rw [bind, map_id] lemma join_map_map {f : Ξ± β†’ Ξ²} (hf : measurable f) (ΞΌ : measure (measure Ξ±)) : join (map (map f) ΞΌ) = map f (join ΞΌ) := measure.ext $ assume s hs, begin rw [join_apply hs, map_apply hf hs, join_apply, lintegral_map (measurable_coe hs) (measurable_map f hf)], { congr, funext Ξ½, exact map_apply hf hs }, exact hf hs end lemma join_map_join (ΞΌ : measure (measure (measure Ξ±))) : join (map join ΞΌ) = join (join ΞΌ) := begin show bind ΞΌ join = join (join ΞΌ), rw [join_eq_bind, join_eq_bind, bind_bind measurable_id measurable_id], apply congr_arg (bind ΞΌ), funext Ξ½, exact join_eq_bind Ξ½ end lemma join_map_dirac (ΞΌ : measure Ξ±) : join (map dirac ΞΌ) = ΞΌ := dirac_bind lemma join_dirac (ΞΌ : measure Ξ±) : join (dirac ΞΌ) = ΞΌ := eq.trans (join_eq_bind (dirac ΞΌ)) (bind_dirac measurable_id _) end measure end measure_theory
097d13d9717d99dacc1eddb4eedc89d3ad15b5b2
00d2363f9655e2a7618f6b94dda7e2c4e5cf8d19
/src/parsing_example.lean
14e20939730d02f16d307f7e1f7b6e6f460a180d
[ "Apache-2.0" ]
permissive
devjuice1/lean_proof_recording
927e276e2ab8fb1288f51d9146dcfbf0d6444a87
bf7c527315deccd35363fa7ca89d97d7b9cb6ac1
refs/heads/master
1,692,914,925,585
1,633,018,872,000
1,633,018,872,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,518
lean
import init.meta.lean.parser --set_option pp.colors false namespace parser open lean meta def get_options : parser options := Ξ» s, interaction_monad.result.success s.options s meta def myparser : parser unit := do p <- parser.cur_pos, o <- get_options, tactic.trace "Inside parser", tactic.trace o, tactic.trace p, return () @[reducible] protected meta def my_itactic : parser (tactic unit) := parser.val (do pos <- parser.cur_pos, tactic.trace pos, a <- parser.itactic_reflected, pos <- parser.cur_pos, tactic.trace pos, return a) meta def pr_parser {Ξ± : Type} (p : parser Ξ±) [lean.parser.reflectable p] : parser Ξ± := do pos <- parser.cur_pos, tactic.trace pos, a <- p, pos <- parser.cur_pos, tactic.trace pos, return a end parser meta def tactic.interactive.my_try (t : interactive.parse parser.my_itactic) : tactic unit := tactic.try t meta def tactic.interactive.my_apply (q : interactive.parse $ parser.pr_parser $ interactive.types.texpr) : tactic unit := tactic.interactive.concat_tags (do h ← tactic.i_to_expr_for_apply q, tactic.apply h) set_option pp.all true #check interactive.parse $ interactive.types.texpr #check interactive.parse $ parser.pr_parser $ interactive.types.texpr #check interactive.parse $ parser.my_itactic #check interactive.parse $ lean.parser.itactic -- #check interactive.parse $ parser.pr_parser $ lean.parser.itactic #check @lean.parser.reflectable.cast (tactic.{0} unit) lean.parser.itactic_reflected example : true := begin my_try { my_apply true.intro }, end
b26a778349cb8d08a95981e95bb0f6a03abdf7e7
7cef822f3b952965621309e88eadf618da0c8ae9
/src/ring_theory/polynomial.lean
b486c6ed84ccb54713c67b95f69d1e6a196bf477
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
14,166
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Ring-theoretic supplement of data.polynomial. Main result: Hilbert basis theorem, that if a ring is noetherian then so is its polynomial ring. -/ import data.polynomial data.mv_polynomial import ring_theory.subring import ring_theory.ideals ring_theory.noetherian noncomputable theory local attribute [instance, priority 100] classical.prop_decidable universes u v w namespace polynomial variables (R : Type u) [comm_ring R] [decidable_eq R] /-- The `R`-submodule of `R[X]` consisting of polynomials of degree ≀ `n`. -/ def degree_le (n : with_bot β„•) : submodule R (polynomial R) := β¨… k : β„•, β¨… h : ↑k > n, (lcoeff R k).ker variable {R} theorem mem_degree_le {n : with_bot β„•} {f : polynomial R} : f ∈ degree_le R n ↔ degree f ≀ n := by simp only [degree_le, submodule.mem_infi, degree_le_iff_coeff_zero, linear_map.mem_ker]; refl theorem degree_le_mono {m n : with_bot β„•} (H : m ≀ n) : degree_le R m ≀ degree_le R n := Ξ» f hf, mem_degree_le.2 (le_trans (mem_degree_le.1 hf) H) theorem degree_le_eq_span_X_pow {n : β„•} : degree_le R n = submodule.span R ↑((finset.range (n+1)).image (Ξ» n, X^n) : finset (polynomial R)) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_le.1 hp, rw [← finsupp.sum_single p, finsupp.sum, submodule.mem_coe], refine submodule.sum_mem _ (Ξ» k hk, _), have := with_bot.coe_le_coe.1 (finset.sup_le_iff.1 hp k hk), rw [single_eq_C_mul_X, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 (nat.lt_succ_of_le this), rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_le.2, apply le_trans (degree_X_pow_le _) (with_bot.coe_le_coe.2 $ nat.le_of_lt_succ $ finset.mem_range.1 hk) end /-- Given a polynomial, return the polynomial whose coefficients are in the ring closure of the original coefficients. -/ def restriction (p : polynomial R) : polynomial (ring.closure (↑p.frange : set R)) := ⟨p.support, Ξ» i, ⟨p.to_fun i, if H : p.to_fun i = 0 then H.symm β–Έ is_add_submonoid.zero_mem _ else ring.subset_closure $ finsupp.mem_frange.2 ⟨H, i, rfl⟩⟩, Ξ» i, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ H, subtype.eq H, subtype.mk.inj⟩)⟩ @[simp] theorem coeff_restriction {p : polynomial R} {n : β„•} : ↑(coeff (restriction p) n) = coeff p n := rfl @[simp] theorem coeff_restriction' {p : polynomial R} {n : β„•} : (coeff (restriction p) n).1 = coeff p n := rfl @[simp] theorem degree_restriction {p : polynomial R} : (restriction p).degree = p.degree := rfl @[simp] theorem nat_degree_restriction {p : polynomial R} : (restriction p).nat_degree = p.nat_degree := rfl @[simp] theorem monic_restriction {p : polynomial R} : monic (restriction p) ↔ monic p := ⟨λ H, congr_arg subtype.val H, Ξ» H, subtype.eq H⟩ @[simp] theorem restriction_zero : restriction (0 : polynomial R) = 0 := rfl @[simp] theorem restriction_one : restriction (1 : polynomial R) = 1 := ext $ Ξ» i, subtype.eq $ by rw [coeff_restriction', coeff_one, coeff_one]; split_ifs; refl variables {S : Type v} [comm_ring S] {f : R β†’ S} {x : S} theorem evalβ‚‚_restriction {p : polynomial R} : evalβ‚‚ f x p = evalβ‚‚ (f ∘ subtype.val) x p.restriction := rfl section to_subring variables (p : polynomial R) (T : set R) [is_subring T] /-- Given a polynomial `p` and a subring `T` that contains the coefficients of `p`, return the corresponding polynomial whose coefficients are in `T. -/ def to_subring (hp : ↑p.frange βŠ† T) : polynomial T := ⟨p.support, Ξ» i, ⟨p.to_fun i, if H : p.to_fun i = 0 then H.symm β–Έ is_add_submonoid.zero_mem _ else hp $ finsupp.mem_frange.2 ⟨H, i, rfl⟩⟩, Ξ» i, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ H, subtype.eq H, subtype.mk.inj⟩)⟩ variables (hp : ↑p.frange βŠ† T) include hp @[simp] theorem coeff_to_subring {n : β„•} : ↑(coeff (to_subring p T hp) n) = coeff p n := rfl @[simp] theorem coeff_to_subring' {n : β„•} : (coeff (to_subring p T hp) n).1 = coeff p n := rfl @[simp] theorem degree_to_subring : (to_subring p T hp).degree = p.degree := rfl @[simp] theorem nat_degree_to_subring : (to_subring p T hp).nat_degree = p.nat_degree := rfl @[simp] theorem monic_to_subring : monic (to_subring p T hp) ↔ monic p := ⟨λ H, congr_arg subtype.val H, Ξ» H, subtype.eq H⟩ omit hp @[simp] theorem to_subring_zero : to_subring (0 : polynomial R) T (set.empty_subset _) = 0 := rfl @[simp] theorem to_subring_one : to_subring (1 : polynomial R) T (set.subset.trans (finset.coe_subset.2 finsupp.frange_single) (set.singleton_subset_iff.2 (is_submonoid.one_mem _))) = 1 := ext $ Ξ» i, subtype.eq $ by rw [coeff_to_subring', coeff_one, coeff_one]; split_ifs; refl end to_subring variables (T : set R) [is_subring T] /-- Given a polynomial whose coefficients are in some subring, return the corresponding polynomial whose coefificents are in the ambient ring. -/ def of_subring (p : polynomial T) : polynomial R := ⟨p.support, subtype.val ∘ p.to_fun, Ξ» n, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ h, congr_arg subtype.val h, Ξ» h, subtype.eq h⟩)⟩ @[simp] theorem frange_of_subring {p : polynomial T} : ↑(p.of_subring T).frange βŠ† T := Ξ» y H, let ⟨hy, x, hx⟩ := finsupp.mem_frange.1 H in hx β–Έ (p.to_fun x).2 end polynomial variables {R : Type u} [comm_ring R] [decidable_eq R] namespace ideal open polynomial /-- Transport an ideal of `R[X]` to an `R`-submodule of `R[X]`. -/ def of_polynomial (I : ideal (polynomial R)) : submodule R (polynomial R) := { carrier := I.carrier, zero := I.zero_mem, add := Ξ» _ _, I.add_mem, smul := Ξ» c x H, by rw [← C_mul']; exact submodule.smul_mem _ _ H } variables {I : ideal (polynomial R)} theorem mem_of_polynomial (x) : x ∈ I.of_polynomial ↔ x ∈ I := iff.rfl variables (I) /-- Given an ideal `I` of `R[X]`, make the `R`-submodule of `I` consisting of polynomials of degree ≀ `n`. -/ def degree_le (n : with_bot β„•) : submodule R (polynomial R) := degree_le R n βŠ“ I.of_polynomial /-- Given an ideal `I` of `R[X]`, make the ideal in `R` of leading coefficients of polynomials in `I` with degree ≀ `n`. -/ def leading_coeff_nth (n : β„•) : ideal R := (I.degree_le n).map $ lcoeff R n theorem mem_leading_coeff_nth (n : β„•) (x) : x ∈ I.leading_coeff_nth n ↔ βˆƒ p ∈ I, degree p ≀ n ∧ leading_coeff p = x := begin simp only [leading_coeff_nth, degree_le, submodule.mem_map, lcoeff_apply, submodule.mem_inf, mem_degree_le], split, { rintro ⟨p, ⟨hpdeg, hpI⟩, rfl⟩, cases lt_or_eq_of_le hpdeg with hpdeg hpdeg, { refine ⟨0, I.zero_mem, lattice.bot_le, _⟩, rw [leading_coeff_zero, eq_comm], exact coeff_eq_zero_of_degree_lt hpdeg }, { refine ⟨p, hpI, le_of_eq hpdeg, _⟩, rw [leading_coeff, nat_degree, hpdeg], refl } }, { rintro ⟨p, hpI, hpdeg, rfl⟩, have : nat_degree p + (n - nat_degree p) = n, { exact nat.add_sub_cancel' (nat_degree_le_of_degree_le hpdeg) }, refine ⟨p * X ^ (n - nat_degree p), ⟨_, I.mul_mem_right hpI⟩, _⟩, { apply le_trans (degree_mul_le _ _) _, apply le_trans (add_le_add' (degree_le_nat_degree) (degree_X_pow_le _)) _, rw [← with_bot.coe_add, this], exact le_refl _ }, { rw [leading_coeff, ← coeff_mul_X_pow p (n - nat_degree p), this] } } end theorem mem_leading_coeff_nth_zero (x) : x ∈ I.leading_coeff_nth 0 ↔ C x ∈ I := (mem_leading_coeff_nth _ _ _).trans ⟨λ ⟨p, hpI, hpdeg, hpx⟩, by rwa [← hpx, leading_coeff, nat.eq_zero_of_le_zero (nat_degree_le_of_degree_le hpdeg), ← eq_C_of_degree_le_zero hpdeg], Ξ» hx, ⟨C x, hx, degree_C_le, leading_coeff_C x⟩⟩ theorem leading_coeff_nth_mono {m n : β„•} (H : m ≀ n) : I.leading_coeff_nth m ≀ I.leading_coeff_nth n := begin intros r hr, simp only [submodule.mem_coe, mem_leading_coeff_nth] at hr ⊒, rcases hr with ⟨p, hpI, hpdeg, rfl⟩, refine ⟨p * X ^ (n - m), I.mul_mem_right hpI, _, leading_coeff_mul_X_pow⟩, refine le_trans (degree_mul_le _ _) _, refine le_trans (add_le_add' hpdeg (degree_X_pow_le _)) _, rw [← with_bot.coe_add, nat.add_sub_cancel' H], exact le_refl _ end /-- Given an ideal `I` in `R[X]`, make the ideal in `R` of the leading coefficients in `I`. -/ def leading_coeff : ideal R := ⨆ n : β„•, I.leading_coeff_nth n theorem mem_leading_coeff (x) : x ∈ I.leading_coeff ↔ βˆƒ p ∈ I, polynomial.leading_coeff p = x := begin rw [leading_coeff, submodule.mem_supr_of_directed], simp only [mem_leading_coeff_nth], { split, { rintro ⟨i, p, hpI, hpdeg, rfl⟩, exact ⟨p, hpI, rfl⟩ }, rintro ⟨p, hpI, rfl⟩, exact ⟨nat_degree p, p, hpI, degree_le_nat_degree, rfl⟩ }, { exact ⟨0⟩ }, intros i j, exact ⟨i + j, I.leading_coeff_nth_mono (nat.le_add_right _ _), I.leading_coeff_nth_mono (nat.le_add_left _ _)⟩ end theorem is_fg_degree_le [is_noetherian_ring R] (n : β„•) : submodule.fg (I.degree_le n) := is_noetherian_submodule_left.1 (is_noetherian_of_fg_of_noetherian _ ⟨_, degree_le_eq_span_X_pow.symm⟩) _ end ideal /-- Hilbert basis theorem. -/ theorem is_noetherian_ring_polynomial [is_noetherian_ring R] : is_noetherian_ring (polynomial R) := ⟨assume I : ideal (polynomial R), let L := I.leading_coeff in let M := well_founded.min (is_noetherian_iff_well_founded.1 (by apply_instance)) (set.range I.leading_coeff_nth) ⟨_, ⟨0, rfl⟩⟩ in have hm : M ∈ set.range I.leading_coeff_nth := well_founded.min_mem _ _ _, let ⟨N, HN⟩ := hm, ⟨s, hs⟩ := I.is_fg_degree_le N in have hm2 : βˆ€ k, I.leading_coeff_nth k ≀ M := Ξ» k, or.cases_on (le_or_lt k N) (Ξ» h, HN β–Έ I.leading_coeff_nth_mono h) (Ξ» h x hx, classical.by_contradiction $ Ξ» hxm, have Β¬M < I.leading_coeff_nth k, by refine well_founded.not_lt_min (well_founded_submodule_gt _ _) _ _ _; exact ⟨k, rfl⟩, this ⟨HN β–Έ I.leading_coeff_nth_mono (le_of_lt h), Ξ» H, hxm (H hx)⟩), have hs2 : βˆ€ {x}, x ∈ I.degree_le N β†’ x ∈ ideal.span (↑s : set (polynomial R)), from hs β–Έ Ξ» x hx, submodule.span_induction hx (Ξ» _ hx, ideal.subset_span hx) (ideal.zero_mem _) (Ξ» _ _, ideal.add_mem _) (Ξ» c f hf, f.C_mul' c β–Έ ideal.mul_mem_left _ hf), ⟨s, le_antisymm (ideal.span_le.2 $ Ξ» x hx, have x ∈ I.degree_le N, from hs β–Έ submodule.subset_span hx, this.2) $ begin change I ≀ ideal.span ↑s, intros p hp, generalize hn : p.nat_degree = k, induction k using nat.strong_induction_on with k ih generalizing p, cases le_or_lt k N, { subst k, refine hs2 ⟨polynomial.mem_degree_le.2 (le_trans polynomial.degree_le_nat_degree $ with_bot.coe_le_coe.2 h), hp⟩ }, { have hp0 : p β‰  0, { rintro rfl, cases hn, exact nat.not_lt_zero _ h }, have : (0 : R) β‰  1, { intro h, apply hp0, ext i, refine (mul_one _).symm.trans _, rw [← h, mul_zero], refl }, letI : nonzero_comm_ring R := { zero_ne_one := this, ..(infer_instance : comm_ring R) }, have : p.leading_coeff ∈ I.leading_coeff_nth N, { rw HN, exact hm2 k ((I.mem_leading_coeff_nth _ _).2 ⟨_, hp, hn β–Έ polynomial.degree_le_nat_degree, rfl⟩) }, rw I.mem_leading_coeff_nth at this, rcases this with ⟨q, hq, hdq, hlqp⟩, have hq0 : q β‰  0, { intro H, rw [← polynomial.leading_coeff_eq_zero] at H, rw [hlqp, polynomial.leading_coeff_eq_zero] at H, exact hp0 H }, have h1 : p.degree = (q * polynomial.X ^ (k - q.nat_degree)).degree, { rw [polynomial.degree_mul_eq', polynomial.degree_X_pow], rw [polynomial.degree_eq_nat_degree hp0, polynomial.degree_eq_nat_degree hq0], rw [← with_bot.coe_add, nat.add_sub_cancel', hn], { refine le_trans (polynomial.nat_degree_le_of_degree_le hdq) (le_of_lt h) }, rw [polynomial.leading_coeff_X_pow, mul_one], exact mt polynomial.leading_coeff_eq_zero.1 hq0 }, have h2 : p.leading_coeff = (q * polynomial.X ^ (k - q.nat_degree)).leading_coeff, { rw [← hlqp, polynomial.leading_coeff_mul_X_pow] }, have := polynomial.degree_sub_lt h1 hp0 h2, rw [polynomial.degree_eq_nat_degree hp0] at this, rw ← sub_add_cancel p (q * polynomial.X ^ (k - q.nat_degree)), refine (ideal.span ↑s).add_mem _ ((ideal.span ↑s).mul_mem_right _), { by_cases hpq : p - q * polynomial.X ^ (k - q.nat_degree) = 0, { rw hpq, exact ideal.zero_mem _ }, refine ih _ _ (I.sub_mem hp (I.mul_mem_right hq)) rfl, rwa [polynomial.degree_eq_nat_degree hpq, with_bot.coe_lt_coe, hn] at this }, exact hs2 ⟨polynomial.mem_degree_le.2 hdq, hq⟩ } end⟩⟩ theorem is_noetherian_ring_mv_polynomial_fin {n : β„•} [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial (fin n) R) := begin induction n with n ih, { exact is_noetherian_ring_of_ring_equiv R ((mv_polynomial.pempty_ring_equiv R).symm.trans $ mv_polynomial.ring_equiv_of_equiv _ ⟨pempty.elim, fin.elim0, Ξ» x, pempty.elim x, Ξ» x, fin.elim0 x⟩) }, exact @is_noetherian_ring_of_ring_equiv (polynomial (mv_polynomial (fin n) R)) _ (mv_polynomial (fin (n+1)) R) _ ((mv_polynomial.option_equiv_left _ _).symm.trans (mv_polynomial.ring_equiv_of_equiv _ ⟨λ x, option.rec_on x 0 fin.succ, Ξ» x, fin.cases none some x, by rintro ⟨none | x⟩; [refl, exact fin.cases_succ _], Ξ» x, fin.cases rfl (Ξ» i, show (option.rec_on (fin.cases none some (fin.succ i) : option (fin n)) 0 fin.succ : fin n.succ) = _, by rw fin.cases_succ) x⟩)) (@@is_noetherian_ring_polynomial _ _ ih) end theorem is_noetherian_ring_mv_polynomial_of_fintype {Οƒ : Type v} [fintype Οƒ] [decidable_eq Οƒ] [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial Οƒ R) := trunc.induction_on (fintype.equiv_fin Οƒ) $ Ξ» e, @is_noetherian_ring_of_ring_equiv (mv_polynomial (fin (fintype.card Οƒ)) R) _ _ _ (mv_polynomial.ring_equiv_of_equiv _ e.symm) is_noetherian_ring_mv_polynomial_fin
295974f8d636d6dffb57e9575521552a114b5eb8
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/data/equiv/functor.lean
adbf42f84919396ce8e6cc2d00e52cab4940b589
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
2,256
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Simon Hudon, Scott Morrison -/ import data.equiv.basic import category.bifunctor /-! # Functor and bifunctors can be applied to `equiv`s. We define ```lean def functor.map_equiv (f : Type u β†’ Type v) [functor f] [is_lawful_functor f] : Ξ± ≃ Ξ² β†’ f Ξ± ≃ f Ξ² ``` and ```lean def bifunctor.map_equiv (F : Type u β†’ Type v β†’ Type w) [bifunctor F] [is_lawful_bifunctor F] : Ξ± ≃ Ξ² β†’ Ξ±' ≃ Ξ²' β†’ F Ξ± Ξ±' ≃ F Ξ² Ξ²' ``` -/ universes u v w variables {Ξ± Ξ² : Type u} open equiv namespace functor variables (f : Type u β†’ Type v) [functor f] [is_lawful_functor f] /-- Apply a functor to an `equiv`. -/ def map_equiv (h : Ξ± ≃ Ξ²) : f Ξ± ≃ f Ξ² := { to_fun := map h, inv_fun := map h.symm, left_inv := Ξ» x, by { rw map_map, convert is_lawful_functor.id_map x, ext a, apply symm_apply_apply }, right_inv := Ξ» x, by { rw map_map, convert is_lawful_functor.id_map x, ext a, apply apply_symm_apply } } @[simp] lemma map_equiv_apply (h : Ξ± ≃ Ξ²) (x : f Ξ±) : (map_equiv f h : f Ξ± ≃ f Ξ²) x = map h x := rfl @[simp] lemma map_equiv_symm_apply (h : Ξ± ≃ Ξ²) (y : f Ξ²) : (map_equiv f h : f Ξ± ≃ f Ξ²).symm y = map h.symm y := rfl end functor namespace bifunctor variables {Ξ±' Ξ²' : Type v} (F : Type u β†’ Type v β†’ Type w) [bifunctor F] [is_lawful_bifunctor F] /-- Apply a bifunctor to a pair of `equiv`s. -/ def map_equiv (h : Ξ± ≃ Ξ²) (h' : Ξ±' ≃ Ξ²') : F Ξ± Ξ±' ≃ F Ξ² Ξ²' := { to_fun := bimap h h', inv_fun := bimap h.symm h'.symm, left_inv := Ξ» x, by { rw bimap_bimap, convert is_lawful_bifunctor.id_bimap x; { ext a, apply symm_apply_apply } }, right_inv := Ξ» x, by { rw bimap_bimap, convert is_lawful_bifunctor.id_bimap x; { ext a, apply apply_symm_apply } } } @[simp] lemma map_equiv_apply (h : Ξ± ≃ Ξ²) (h' : Ξ±' ≃ Ξ²') (x : F Ξ± Ξ±') : (map_equiv F h h' : F Ξ± Ξ±' ≃ F Ξ² Ξ²') x = bimap h h' x := rfl @[simp] lemma map_equiv_symm_apply (h : Ξ± ≃ Ξ²) (h' : Ξ±' ≃ Ξ²') (y : F Ξ² Ξ²') : (map_equiv F h h' : F Ξ± Ξ±' ≃ F Ξ² Ξ²').symm y = bimap h.symm h'.symm y := rfl end bifunctor
77d9ef8800dfad59a33baa65c4772d2552137360
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/analysis/convex/basic.lean
c68fac1eead759b10854210e579617f6b74d7c7f
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
39,557
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov, YaΓ«l Dillies -/ import algebra.order.module import linear_algebra.affine_space.affine_subspace /-! # Convex sets and functions in vector spaces In a π•œ-vector space, we define the following objects and properties. * `segment π•œ x y`: Closed segment joining `x` and `y`. * `open_segment π•œ x y`: Open segment joining `x` and `y`. * `convex π•œ s`: A set `s` is convex if for any two points `x y ∈ s` it includes `segment π•œ x y`. * `std_simplex π•œ ΞΉ`: The standard simplex in `ΞΉ β†’ π•œ` (currently requires `fintype ΞΉ`). It is the intersection of the positive quadrant with the hyperplane `s.sum = 1`. We also provide various equivalent versions of the definitions above, prove that some specific sets are convex. ## Notations We provide the following notation: * `[x -[π•œ] y] = segment π•œ x y` in locale `convex` ## TODO Generalize all this file to affine spaces. Should we rename `segment` and `open_segment` to `convex.Icc` and `convex.Ioo`? Should we also define `clopen_segment`/`convex.Ico`/`convex.Ioc`? -/ variables {π•œ E F Ξ² : Type*} open linear_map set open_locale big_operators classical pointwise /-! ### Segment -/ section ordered_semiring variables [ordered_semiring π•œ] [add_comm_monoid E] section has_scalar variables (π•œ) [has_scalar π•œ E] /-- Segments in a vector space. -/ def segment (x y : E) : set E := {z : E | βˆƒ (a b : π•œ) (ha : 0 ≀ a) (hb : 0 ≀ b) (hab : a + b = 1), a β€’ x + b β€’ y = z} /-- Open segment in a vector space. Note that `open_segment π•œ x x = {x}` instead of being `βˆ…` when the base semiring has some element between `0` and `1`. -/ def open_segment (x y : E) : set E := {z : E | βˆƒ (a b : π•œ) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1), a β€’ x + b β€’ y = z} localized "notation `[` x ` -[` π•œ `] ` y `]` := segment π•œ x y" in convex lemma segment_symm (x y : E) : [x -[π•œ] y] = [y -[π•œ] x] := set.ext $ Ξ» z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, Ξ» ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma open_segment_symm (x y : E) : open_segment π•œ x y = open_segment π•œ y x := set.ext $ Ξ» z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, Ξ» ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma open_segment_subset_segment (x y : E) : open_segment π•œ x y βŠ† [x -[π•œ] y] := Ξ» z ⟨a, b, ha, hb, hab, hz⟩, ⟨a, b, ha.le, hb.le, hab, hz⟩ end has_scalar open_locale convex section mul_action_with_zero variables (π•œ) [mul_action_with_zero π•œ E] lemma left_mem_segment (x y : E) : x ∈ [x -[π•œ] y] := ⟨1, 0, zero_le_one, le_refl 0, add_zero 1, by rw [zero_smul, one_smul, add_zero]⟩ lemma right_mem_segment (x y : E) : y ∈ [x -[π•œ] y] := segment_symm π•œ y x β–Έ left_mem_segment π•œ y x end mul_action_with_zero section module variables (π•œ) [module π•œ E] lemma segment_same (x : E) : [x -[π•œ] x] = {x} := set.ext $ Ξ» z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [(add_smul _ _ _).symm, mem_singleton_iff, hab, one_smul, eq_comm] using hz, Ξ» h, mem_singleton_iff.1 h β–Έ left_mem_segment π•œ z z⟩ lemma mem_open_segment_of_ne_left_right {x y z : E} (hx : x β‰  z) (hy : y β‰  z) (hz : z ∈ [x -[π•œ] y]) : z ∈ open_segment π•œ x y := begin obtain ⟨a, b, ha, hb, hab, hz⟩ := hz, by_cases ha' : a = 0, { rw [ha', zero_add] at hab, rw [ha', hab, zero_smul, one_smul, zero_add] at hz, exact (hy hz).elim }, by_cases hb' : b = 0, { rw [hb', add_zero] at hab, rw [hb', hab, zero_smul, one_smul, add_zero] at hz, exact (hx hz).elim }, exact ⟨a, b, ha.lt_of_ne (ne.symm ha'), hb.lt_of_ne (ne.symm hb'), hab, hz⟩, end variables {π•œ} lemma open_segment_subset_iff_segment_subset {x y : E} {s : set E} (hx : x ∈ s) (hy : y ∈ s) : open_segment π•œ x y βŠ† s ↔ [x -[π•œ] y] βŠ† s := begin refine ⟨λ h z hz, _, (open_segment_subset_segment π•œ x y).trans⟩, obtain rfl | hxz := eq_or_ne x z, { exact hx }, obtain rfl | hyz := eq_or_ne y z, { exact hy }, exact h (mem_open_segment_of_ne_left_right π•œ hxz hyz hz), end lemma convex.combo_self {a b : π•œ} (h : a + b = 1) (x : E) : a β€’ x + b β€’ x = x := by rw [←add_smul, h, one_smul] end module end ordered_semiring open_locale convex section ordered_ring variables [ordered_ring π•œ] section add_comm_group variables (π•œ) [add_comm_group E] [add_comm_group F] [module π•œ E] [module π•œ F] section densely_ordered variables [nontrivial π•œ] [densely_ordered π•œ] @[simp] lemma open_segment_same (x : E) : open_segment π•œ x x = {x} := set.ext $ Ξ» z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [← add_smul, mem_singleton_iff, hab, one_smul, eq_comm] using hz, Ξ» (h : z = x), begin obtain ⟨a, haβ‚€, haβ‚βŸ© := densely_ordered.dense (0 : π•œ) 1 zero_lt_one, refine ⟨a, 1 - a, haβ‚€, sub_pos_of_lt ha₁, add_sub_cancel'_right _ _, _⟩, rw [←add_smul, add_sub_cancel'_right, one_smul, h], end⟩ end densely_ordered lemma segment_eq_image (x y : E) : [x -[π•œ] y] = (Ξ» ΞΈ : π•œ, (1 - ΞΈ) β€’ x + ΞΈ β€’ y) '' Icc (0 : π•œ) 1 := set.ext $ Ξ» z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab β–Έ le_add_of_nonneg_left ha⟩, hab β–Έ hz β–Έ by simp only [add_sub_cancel]⟩, Ξ» ⟨θ, ⟨hΞΈβ‚€, hΞΈβ‚βŸ©, hz⟩, ⟨1-ΞΈ, ΞΈ, sub_nonneg.2 hθ₁, hΞΈβ‚€, sub_add_cancel _ _, hz⟩⟩ lemma open_segment_eq_image (x y : E) : open_segment π•œ x y = (Ξ» (ΞΈ : π•œ), (1 - ΞΈ) β€’ x + ΞΈ β€’ y) '' Ioo (0 : π•œ) 1 := set.ext $ Ξ» z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab β–Έ lt_add_of_pos_left _ ha⟩, hab β–Έ hz β–Έ by simp only [add_sub_cancel]⟩, Ξ» ⟨θ, ⟨hΞΈβ‚€, hΞΈβ‚βŸ©, hz⟩, ⟨1 - ΞΈ, ΞΈ, sub_pos.2 hθ₁, hΞΈβ‚€, sub_add_cancel _ _, hz⟩⟩ lemma segment_eq_imageβ‚‚ (x y : E) : [x -[π•œ] y] = (Ξ» p : π•œ Γ— π•œ, p.1 β€’ x + p.2 β€’ y) '' {p | 0 ≀ p.1 ∧ 0 ≀ p.2 ∧ p.1 + p.2 = 1} := by simp only [segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma open_segment_eq_imageβ‚‚ (x y : E) : open_segment π•œ x y = (Ξ» p : π•œ Γ— π•œ, p.1 β€’ x + p.2 β€’ y) '' {p | 0 < p.1 ∧ 0 < p.2 ∧ p.1 + p.2 = 1} := by simp only [open_segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma segment_eq_image' (x y : E) : [x -[π•œ] y] = (Ξ» (ΞΈ : π•œ), x + ΞΈ β€’ (y - x)) '' Icc (0 : π•œ) 1 := by { convert segment_eq_image π•œ x y, ext ΞΈ, simp only [smul_sub, sub_smul, one_smul], abel } lemma open_segment_eq_image' (x y : E) : open_segment π•œ x y = (Ξ» (ΞΈ : π•œ), x + ΞΈ β€’ (y - x)) '' Ioo (0 : π•œ) 1 := by { convert open_segment_eq_image π•œ x y, ext ΞΈ, simp only [smul_sub, sub_smul, one_smul], abel } lemma segment_image (f : E β†’β‚—[π•œ] F) (a b : E) : f '' [a -[π•œ] b] = [f a -[π•œ] f b] := set.ext (Ξ» x, by simp_rw [segment_eq_image, mem_image, exists_exists_and_eq_and, map_add, map_smul]) @[simp] lemma open_segment_image (f : E β†’β‚—[π•œ] F) (a b : E) : f '' open_segment π•œ a b = open_segment π•œ (f a) (f b) := set.ext (Ξ» x, by simp_rw [open_segment_eq_image, mem_image, exists_exists_and_eq_and, map_add, map_smul]) lemma mem_segment_translate (a : E) {x b c} : a + x ∈ [a + b -[π•œ] a + c] ↔ x ∈ [b -[π•œ] c] := begin rw [segment_eq_image', segment_eq_image'], refine exists_congr (Ξ» ΞΈ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj], end @[simp] lemma mem_open_segment_translate (a : E) {x b c : E} : a + x ∈ open_segment π•œ (a + b) (a + c) ↔ x ∈ open_segment π•œ b c := begin rw [open_segment_eq_image', open_segment_eq_image'], refine exists_congr (Ξ» ΞΈ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj], end lemma segment_translate_preimage (a b c : E) : (Ξ» x, a + x) ⁻¹' [a + b -[π•œ] a + c] = [b -[π•œ] c] := set.ext $ Ξ» x, mem_segment_translate π•œ a lemma open_segment_translate_preimage (a b c : E) : (Ξ» x, a + x) ⁻¹' open_segment π•œ (a + b) (a + c) = open_segment π•œ b c := set.ext $ Ξ» x, mem_open_segment_translate π•œ a lemma segment_translate_image (a b c : E) : (Ξ» x, a + x) '' [b -[π•œ] c] = [a + b -[π•œ] a + c] := segment_translate_preimage π•œ a b c β–Έ image_preimage_eq _ $ add_left_surjective a lemma open_segment_translate_image (a b c : E) : (Ξ» x, a + x) '' open_segment π•œ b c = open_segment π•œ (a + b) (a + c) := open_segment_translate_preimage π•œ a b c β–Έ image_preimage_eq _ $ add_left_surjective a end add_comm_group end ordered_ring section linear_ordered_field variables [linear_ordered_field π•œ] section add_comm_group variables [add_comm_group E] [add_comm_group F] [module π•œ E] [module π•œ F] @[simp] lemma left_mem_open_segment_iff [no_zero_smul_divisors π•œ E] {x y : E} : x ∈ open_segment π•œ x y ↔ x = y := begin split, { rintro ⟨a, b, ha, hb, hab, hx⟩, refine smul_right_injective _ hb.ne' ((add_right_inj (a β€’ x)).1 _), rw [hx, ←add_smul, hab, one_smul] }, { rintro rfl, rw open_segment_same, exact mem_singleton _ } end @[simp] lemma right_mem_open_segment_iff {x y : E} : y ∈ open_segment π•œ x y ↔ x = y := by rw [open_segment_symm, left_mem_open_segment_iff, eq_comm] end add_comm_group end linear_ordered_field /-! #### Segments in an ordered space Relates `segment`, `open_segment` and `set.Icc`, `set.Ico`, `set.Ioc`, `set.Ioo` -/ section ordered_semiring variables [ordered_semiring π•œ] section ordered_add_comm_monoid variables [ordered_add_comm_monoid E] [module π•œ E] [ordered_smul π•œ E] lemma segment_subset_Icc {x y : E} (h : x ≀ y) : [x -[π•œ] y] βŠ† Icc x y := begin rintro z ⟨a, b, ha, hb, hab, rfl⟩, split, calc x = a β€’ x + b β€’ x :(convex.combo_self hab _).symm ... ≀ a β€’ x + b β€’ y : add_le_add_left (smul_le_smul_of_nonneg h hb) _, calc a β€’ x + b β€’ y ≀ a β€’ y + b β€’ y : add_le_add_right (smul_le_smul_of_nonneg h ha) _ ... = y : convex.combo_self hab _, end end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid E] [module π•œ E] [ordered_smul π•œ E] lemma open_segment_subset_Ioo {x y : E} (h : x < y) : open_segment π•œ x y βŠ† Ioo x y := begin rintro z ⟨a, b, ha, hb, hab, rfl⟩, split, calc x = a β€’ x + b β€’ x : (convex.combo_self hab _).symm ... < a β€’ x + b β€’ y : add_lt_add_left (smul_lt_smul_of_pos h hb) _, calc a β€’ x + b β€’ y < a β€’ y + b β€’ y : add_lt_add_right (smul_lt_smul_of_pos h ha) _ ... = y : convex.combo_self hab _, end end ordered_cancel_add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid E] [module π•œ E] [ordered_smul π•œ E] {π•œ} lemma segment_subset_interval (x y : E) : [x -[π•œ] y] βŠ† interval x y := begin cases le_total x y, { rw interval_of_le h, exact segment_subset_Icc h }, { rw [interval_of_ge h, segment_symm], exact segment_subset_Icc h } end lemma convex.min_le_combo (x y : E) {a b : π•œ} (ha : 0 ≀ a) (hb : 0 ≀ b) (hab : a + b = 1) : min x y ≀ a β€’ x + b β€’ y := (segment_subset_interval x y ⟨_, _, ha, hb, hab, rfl⟩).1 lemma convex.combo_le_max (x y : E) {a b : π•œ} (ha : 0 ≀ a) (hb : 0 ≀ b) (hab : a + b = 1) : a β€’ x + b β€’ y ≀ max x y := (segment_subset_interval x y ⟨_, _, ha, hb, hab, rfl⟩).2 end linear_ordered_add_comm_monoid end ordered_semiring section linear_ordered_field variables [linear_ordered_field π•œ] lemma Icc_subset_segment {x y : π•œ} : Icc x y βŠ† [x -[π•œ] y] := begin rintro z ⟨hxz, hyz⟩, obtain rfl | h := (hxz.trans hyz).eq_or_lt, { rw segment_same, exact hyz.antisymm hxz }, rw ←sub_nonneg at hxz hyz, rw ←sub_pos at h, refine ⟨(y - z) / (y - x), (z - x) / (y - x), div_nonneg hyz h.le, div_nonneg hxz h.le, _, _⟩, { rw [←add_div, sub_add_sub_cancel, div_self h.ne'] }, { rw [smul_eq_mul, smul_eq_mul, ←mul_div_right_comm, ←mul_div_right_comm, ←add_div, div_eq_iff h.ne', add_comm, sub_mul, sub_mul, mul_comm x, sub_add_sub_cancel, mul_sub] } end @[simp] lemma segment_eq_Icc {x y : π•œ} (h : x ≀ y) : [x -[π•œ] y] = Icc x y := (segment_subset_Icc h).antisymm Icc_subset_segment lemma Ioo_subset_open_segment {x y : π•œ} : Ioo x y βŠ† open_segment π•œ x y := Ξ» z hz, mem_open_segment_of_ne_left_right _ hz.1.ne hz.2.ne' (Icc_subset_segment $ Ioo_subset_Icc_self hz) @[simp] lemma open_segment_eq_Ioo {x y : π•œ} (h : x < y) : open_segment π•œ x y = Ioo x y := (open_segment_subset_Ioo h).antisymm Ioo_subset_open_segment lemma segment_eq_Icc' (x y : π•œ) : [x -[π•œ] y] = Icc (min x y) (max x y) := begin cases le_total x y, { rw [segment_eq_Icc h, max_eq_right h, min_eq_left h] }, { rw [segment_symm, segment_eq_Icc h, max_eq_left h, min_eq_right h] } end lemma open_segment_eq_Ioo' {x y : π•œ} (hxy : x β‰  y) : open_segment π•œ x y = Ioo (min x y) (max x y) := begin cases hxy.lt_or_lt, { rw [open_segment_eq_Ioo h, max_eq_right h.le, min_eq_left h.le] }, { rw [open_segment_symm, open_segment_eq_Ioo h, max_eq_left h.le, min_eq_right h.le] } end lemma segment_eq_interval (x y : π•œ) : [x -[π•œ] y] = interval x y := segment_eq_Icc' _ _ /-- A point is in an `Icc` iff it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Icc {x y : π•œ} (h : x ≀ y) {z : π•œ} : z ∈ Icc x y ↔ βˆƒ (a b : π•œ), 0 ≀ a ∧ 0 ≀ b ∧ a + b = 1 ∧ a * x + b * y = z := begin rw ←segment_eq_Icc h, simp_rw [←exists_prop], refl, end /-- A point is in an `Ioo` iff it can be expressed as a strict convex combination of the endpoints. -/ lemma convex.mem_Ioo {x y : π•œ} (h : x < y) {z : π•œ} : z ∈ Ioo x y ↔ βˆƒ (a b : π•œ), 0 < a ∧ 0 < b ∧ a + b = 1 ∧ a * x + b * y = z := begin rw ←open_segment_eq_Ioo h, simp_rw [←exists_prop], refl, end /-- A point is in an `Ioc` iff it can be expressed as a semistrict convex combination of the endpoints. -/ lemma convex.mem_Ioc {x y : π•œ} (h : x < y) {z : π•œ} : z ∈ Ioc x y ↔ βˆƒ (a b : π•œ), 0 ≀ a ∧ 0 < b ∧ a + b = 1 ∧ a * x + b * y = z := begin split, { rintro hz, obtain ⟨a, b, ha, hb, hab, rfl⟩ := (convex.mem_Icc h.le).1 (Ioc_subset_Icc_self hz), obtain rfl | hb' := hb.eq_or_lt, { rw add_zero at hab, rw [hab, one_mul, zero_mul, add_zero] at hz, exact (hz.1.ne rfl).elim }, { exact ⟨a, b, ha, hb', hab, rfl⟩ } }, { rintro ⟨a, b, ha, hb, hab, rfl⟩, obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rwa [hab, one_mul, zero_mul, zero_add, right_mem_Ioc] }, { exact Ioo_subset_Ioc_self ((convex.mem_Ioo h).2 ⟨a, b, ha', hb, hab, rfl⟩) } } end /-- A point is in an `Ico` iff it can be expressed as a semistrict convex combination of the endpoints. -/ lemma convex.mem_Ico {x y : π•œ} (h : x < y) {z : π•œ} : z ∈ Ico x y ↔ βˆƒ (a b : π•œ), 0 < a ∧ 0 ≀ b ∧ a + b = 1 ∧ a * x + b * y = z := begin split, { rintro hz, obtain ⟨a, b, ha, hb, hab, rfl⟩ := (convex.mem_Icc h.le).1 (Ico_subset_Icc_self hz), obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rw [hab, one_mul, zero_mul, zero_add] at hz, exact (hz.2.ne rfl).elim }, { exact ⟨a, b, ha', hb, hab, rfl⟩ } }, { rintro ⟨a, b, ha, hb, hab, rfl⟩, obtain rfl | hb' := hb.eq_or_lt, { rw add_zero at hab, rwa [hab, one_mul, zero_mul, add_zero, left_mem_Ico] }, { exact Ioo_subset_Ico_self ((convex.mem_Ioo h).2 ⟨a, b, ha, hb', hab, rfl⟩) } } end end linear_ordered_field /-! ### Convexity of sets -/ section ordered_semiring variables [ordered_semiring π•œ] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] section has_scalar variables (π•œ) [has_scalar π•œ E] [has_scalar π•œ F] (s : set E) /-- Convexity of sets. -/ def convex : Prop := βˆ€ ⦃x y : E⦄, x ∈ s β†’ y ∈ s β†’ βˆ€ ⦃a b : π•œβ¦„, 0 ≀ a β†’ 0 ≀ b β†’ a + b = 1 β†’ a β€’ x + b β€’ y ∈ s variables {π•œ s} lemma convex_iff_segment_subset : convex π•œ s ↔ βˆ€ ⦃x y⦄, x ∈ s β†’ y ∈ s β†’ [x -[π•œ] y] βŠ† s := begin split, { rintro h x y hx hy z ⟨a, b, ha, hb, hab, rfl⟩, exact h hx hy ha hb hab }, { rintro h x y hx hy a b ha hb hab, exact h hx hy ⟨a, b, ha, hb, hab, rfl⟩ } end lemma convex.segment_subset (h : convex π•œ s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) : [x -[π•œ] y] βŠ† s := convex_iff_segment_subset.1 h hx hy lemma convex.open_segment_subset (h : convex π•œ s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) : open_segment π•œ x y βŠ† s := (open_segment_subset_segment π•œ x y).trans (h.segment_subset hx hy) /-- Alternative definition of set convexity, in terms of pointwise set operations. -/ lemma convex_iff_pointwise_add_subset : convex π•œ s ↔ βˆ€ ⦃a b : π•œβ¦„, 0 ≀ a β†’ 0 ≀ b β†’ a + b = 1 β†’ a β€’ s + b β€’ s βŠ† s := iff.intro begin rintro hA a b ha hb hab w ⟨au, bv, ⟨u, hu, rfl⟩, ⟨v, hv, rfl⟩, rfl⟩, exact hA hu hv ha hb hab end (Ξ» h x y hx hy a b ha hb hab, (h ha hb hab) (set.add_mem_add ⟨_, hx, rfl⟩ ⟨_, hy, rfl⟩)) lemma convex_empty : convex π•œ (βˆ… : set E) := by finish lemma convex_univ : convex π•œ (set.univ : set E) := Ξ» _ _ _ _ _ _ _ _ _, trivial lemma convex.inter {t : set E} (hs : convex π•œ s) (ht : convex π•œ t) : convex π•œ (s ∩ t) := Ξ» x y (hx : x ∈ s ∩ t) (hy : y ∈ s ∩ t) a b (ha : 0 ≀ a) (hb : 0 ≀ b) (hab : a + b = 1), ⟨hs hx.left hy.left ha hb hab, ht hx.right hy.right ha hb hab⟩ lemma convex_sInter {S : set (set E)} (h : βˆ€ s ∈ S, convex π•œ s) : convex π•œ (β‹‚β‚€ S) := assume x y hx hy a b ha hb hab s hs, h s hs (hx s hs) (hy s hs) ha hb hab lemma convex_Inter {ΞΉ : Sort*} {s : ΞΉ β†’ set E} (h : βˆ€ i : ΞΉ, convex π•œ (s i)) : convex π•œ (β‹‚ i, s i) := (sInter_range s) β–Έ convex_sInter $ forall_range_iff.2 h lemma convex.prod {s : set E} {t : set F} (hs : convex π•œ s) (ht : convex π•œ t) : convex π•œ (s.prod t) := begin intros x y hx hy a b ha hb hab, apply mem_prod.2, exact ⟨hs (mem_prod.1 hx).1 (mem_prod.1 hy).1 ha hb hab, ht (mem_prod.1 hx).2 (mem_prod.1 hy).2 ha hb hab⟩ end lemma directed.convex_Union {ΞΉ : Sort*} {s : ΞΉ β†’ set E} (hdir : directed (βŠ†) s) (hc : βˆ€ ⦃i : ι⦄, convex π•œ (s i)) : convex π•œ (⋃ i, s i) := begin rintro x y hx hy a b ha hb hab, rw mem_Union at ⊒ hx hy, obtain ⟨i, hx⟩ := hx, obtain ⟨j, hy⟩ := hy, obtain ⟨k, hik, hjk⟩ := hdir i j, exact ⟨k, hc (hik hx) (hjk hy) ha hb hab⟩, end lemma directed_on.convex_sUnion {c : set (set E)} (hdir : directed_on (βŠ†) c) (hc : βˆ€ ⦃A : set E⦄, A ∈ c β†’ convex π•œ A) : convex π•œ (⋃₀c) := begin rw sUnion_eq_Union, exact (directed_on_iff_directed.1 hdir).convex_Union (Ξ» A, hc A.2), end end has_scalar section module variables [module π•œ E] [module π•œ F] {s : set E} lemma convex_iff_forall_pos : convex π•œ s ↔ βˆ€ ⦃x y⦄, x ∈ s β†’ y ∈ s β†’ βˆ€ ⦃a b : π•œβ¦„, 0 < a β†’ 0 < b β†’ a + b = 1 β†’ a β€’ x + b β€’ y ∈ s := begin refine ⟨λ h x y hx hy a b ha hb hab, h hx hy ha.le hb.le hab, _⟩, intros h x y hx hy a b ha hb hab, cases ha.eq_or_lt with ha ha, { subst a, rw [zero_add] at hab, simp [hab, hy] }, cases hb.eq_or_lt with hb hb, { subst b, rw [add_zero] at hab, simp [hab, hx] }, exact h hx hy ha hb hab end lemma convex_iff_pairwise_on_pos : convex π•œ s ↔ s.pairwise_on (Ξ» x y, βˆ€ ⦃a b : π•œβ¦„, 0 < a β†’ 0 < b β†’ a + b = 1 β†’ a β€’ x + b β€’ y ∈ s) := begin refine ⟨λ h x hx y hy _ a b ha hb hab, h hx hy ha.le hb.le hab, _⟩, intros h x y hx hy a b ha hb hab, obtain rfl | ha' := ha.eq_or_lt, { rw [zero_add] at hab, rwa [hab, zero_smul, one_smul, zero_add] }, obtain rfl | hb' := hb.eq_or_lt, { rw [add_zero] at hab, rwa [hab, zero_smul, one_smul, add_zero] }, obtain rfl | hxy := eq_or_ne x y, { rwa convex.combo_self hab }, exact h _ hx _ hy hxy ha' hb' hab, end lemma convex_iff_open_segment_subset : convex π•œ s ↔ βˆ€ ⦃x y⦄, x ∈ s β†’ y ∈ s β†’ open_segment π•œ x y βŠ† s := begin rw convex_iff_segment_subset, exact forallβ‚‚_congr (Ξ» x y, forallβ‚‚_congr $ Ξ» hx hy, (open_segment_subset_iff_segment_subset hx hy).symm), end lemma convex_singleton (c : E) : convex π•œ ({c} : set E) := begin intros x y hx hy a b ha hb hab, rw [set.eq_of_mem_singleton hx, set.eq_of_mem_singleton hy, ←add_smul, hab, one_smul], exact mem_singleton c end lemma convex.linear_image (hs : convex π•œ s) (f : E β†’β‚—[π•œ] F) : convex π•œ (s.image f) := begin intros x y hx hy a b ha hb hab, obtain ⟨x', hx', rfl⟩ := mem_image_iff_bex.1 hx, obtain ⟨y', hy', rfl⟩ := mem_image_iff_bex.1 hy, exact ⟨a β€’ x' + b β€’ y', hs hx' hy' ha hb hab, by rw [f.map_add, f.map_smul, f.map_smul]⟩, end lemma convex.is_linear_image (hs : convex π•œ s) {f : E β†’ F} (hf : is_linear_map π•œ f) : convex π•œ (f '' s) := hs.linear_image $ hf.mk' f lemma convex.linear_preimage {s : set F} (hs : convex π•œ s) (f : E β†’β‚—[π•œ] F) : convex π•œ (s.preimage f) := begin intros x y hx hy a b ha hb hab, rw [mem_preimage, f.map_add, f.map_smul, f.map_smul], exact hs hx hy ha hb hab, end lemma convex.is_linear_preimage {s : set F} (hs : convex π•œ s) {f : E β†’ F} (hf : is_linear_map π•œ f) : convex π•œ (preimage f s) := hs.linear_preimage $ hf.mk' f lemma convex.add {t : set E} (hs : convex π•œ s) (ht : convex π•œ t) : convex π•œ (s + t) := by { rw ← add_image_prod, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add } lemma convex.translate (hs : convex π•œ s) (z : E) : convex π•œ ((Ξ» x, z + x) '' s) := begin intros x y hx hy a b ha hb hab, obtain ⟨x', hx', rfl⟩ := mem_image_iff_bex.1 hx, obtain ⟨y', hy', rfl⟩ := mem_image_iff_bex.1 hy, refine ⟨a β€’ x' + b β€’ y', hs hx' hy' ha hb hab, _⟩, rw [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul], end /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_right (hs : convex π•œ s) (z : E) : convex π•œ ((Ξ» x, z + x) ⁻¹' s) := begin intros x y hx hy a b ha hb hab, have h := hs hx hy ha hb hab, rwa [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul] at h, end /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_left (hs : convex π•œ s) (z : E) : convex π•œ ((Ξ» x, x + z) ⁻¹' s) := by simpa only [add_comm] using hs.translate_preimage_right z section ordered_add_comm_monoid variables [ordered_add_comm_monoid Ξ²] [module π•œ Ξ²] [ordered_smul π•œ Ξ²] lemma convex_Iic (r : Ξ²) : convex π•œ (Iic r) := Ξ» x y hx hy a b ha hb hab, calc a β€’ x + b β€’ y ≀ a β€’ r + b β€’ r : add_le_add (smul_le_smul_of_nonneg hx ha) (smul_le_smul_of_nonneg hy hb) ... = r : convex.combo_self hab _ lemma convex_Ici (r : Ξ²) : convex π•œ (Ici r) := @convex_Iic π•œ (order_dual Ξ²) _ _ _ _ r lemma convex_Icc (r s : Ξ²) : convex π•œ (Icc r s) := Ici_inter_Iic.subst ((convex_Ici r).inter $ convex_Iic s) lemma convex_halfspace_le {f : E β†’ Ξ²} (h : is_linear_map π•œ f) (r : Ξ²) : convex π•œ {w | f w ≀ r} := (convex_Iic r).is_linear_preimage h lemma convex_halfspace_ge {f : E β†’ Ξ²} (h : is_linear_map π•œ f) (r : Ξ²) : convex π•œ {w | r ≀ f w} := (convex_Ici r).is_linear_preimage h lemma convex_hyperplane {f : E β†’ Ξ²} (h : is_linear_map π•œ f) (r : Ξ²) : convex π•œ {w | f w = r} := begin simp_rw le_antisymm_iff, exact (convex_halfspace_le h r).inter (convex_halfspace_ge h r), end end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid Ξ²] [module π•œ Ξ²] [ordered_smul π•œ Ξ²] lemma convex_Iio (r : Ξ²) : convex π•œ (Iio r) := begin intros x y hx hy a b ha hb hab, obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rwa [zero_smul, zero_add, hab, one_smul] }, rw mem_Iio at hx hy, calc a β€’ x + b β€’ y < a β€’ r + b β€’ r : add_lt_add_of_lt_of_le (smul_lt_smul_of_pos hx ha') (smul_le_smul_of_nonneg hy.le hb) ... = r : convex.combo_self hab _ end lemma convex_Ioi (r : Ξ²) : convex π•œ (Ioi r) := @convex_Iio π•œ (order_dual Ξ²) _ _ _ _ r lemma convex_Ioo (r s : Ξ²) : convex π•œ (Ioo r s) := Ioi_inter_Iio.subst ((convex_Ioi r).inter $ convex_Iio s) lemma convex_Ico (r s : Ξ²) : convex π•œ (Ico r s) := Ici_inter_Iio.subst ((convex_Ici r).inter $ convex_Iio s) lemma convex_Ioc (r s : Ξ²) : convex π•œ (Ioc r s) := Ioi_inter_Iic.subst ((convex_Ioi r).inter $ convex_Iic s) lemma convex_halfspace_lt {f : E β†’ Ξ²} (h : is_linear_map π•œ f) (r : Ξ²) : convex π•œ {w | f w < r} := (convex_Iio r).is_linear_preimage h lemma convex_halfspace_gt {f : E β†’ Ξ²} (h : is_linear_map π•œ f) (r : Ξ²) : convex π•œ {w | r < f w} := (convex_Ioi r).is_linear_preimage h end ordered_cancel_add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid Ξ²] [module π•œ Ξ²] [ordered_smul π•œ Ξ²] lemma convex_interval (r s : Ξ²) : convex π•œ (interval r s) := convex_Icc _ _ end linear_ordered_add_comm_monoid end module end add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid E] [ordered_add_comm_monoid Ξ²] [module π•œ E] [ordered_smul π•œ E] {s : set E} {f : E β†’ Ξ²} lemma monotone_on.convex_le (hf : monotone_on f s) (hs : convex π•œ s) (r : Ξ²) : convex π•œ {x ∈ s | f x ≀ r} := Ξ» x y hx hy a b ha hb hab, ⟨hs hx.1 hy.1 ha hb hab, (hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans (max_rec' _ hx.2 hy.2)⟩ lemma monotone_on.convex_lt (hf : monotone_on f s) (hs : convex π•œ s) (r : Ξ²) : convex π•œ {x ∈ s | f x < r} := Ξ» x y hx hy a b ha hb hab, ⟨hs hx.1 hy.1 ha hb hab, (hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans_lt (max_rec' _ hx.2 hy.2)⟩ lemma monotone_on.convex_ge (hf : monotone_on f s) (hs : convex π•œ s) (r : Ξ²) : convex π•œ {x ∈ s | r ≀ f x} := @monotone_on.convex_le π•œ (order_dual E) (order_dual Ξ²) _ _ _ _ _ _ _ hf.dual hs r lemma monotone_on.convex_gt (hf : monotone_on f s) (hs : convex π•œ s) (r : Ξ²) : convex π•œ {x ∈ s | r < f x} := @monotone_on.convex_lt π•œ (order_dual E) (order_dual Ξ²) _ _ _ _ _ _ _ hf.dual hs r lemma antitone_on.convex_le (hf : antitone_on f s) (hs : convex π•œ s) (r : Ξ²) : convex π•œ {x ∈ s | f x ≀ r} := @monotone_on.convex_ge π•œ E (order_dual Ξ²) _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_lt (hf : antitone_on f s) (hs : convex π•œ s) (r : Ξ²) : convex π•œ {x ∈ s | f x < r} := @monotone_on.convex_gt π•œ E (order_dual Ξ²) _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_ge (hf : antitone_on f s) (hs : convex π•œ s) (r : Ξ²) : convex π•œ {x ∈ s | r ≀ f x} := @monotone_on.convex_le π•œ E (order_dual Ξ²) _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_gt (hf : antitone_on f s) (hs : convex π•œ s) (r : Ξ²) : convex π•œ {x ∈ s | r < f x} := @monotone_on.convex_lt π•œ E (order_dual Ξ²) _ _ _ _ _ _ _ hf hs r lemma monotone.convex_le (hf : monotone f) (r : Ξ²) : convex π•œ {x | f x ≀ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma monotone.convex_lt (hf : monotone f) (r : Ξ²) : convex π•œ {x | f x ≀ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma monotone.convex_ge (hf : monotone f ) (r : Ξ²) : convex π•œ {x | r ≀ f x} := set.sep_univ.subst ((hf.monotone_on univ).convex_ge convex_univ r) lemma monotone.convex_gt (hf : monotone f) (r : Ξ²) : convex π•œ {x | f x ≀ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma antitone.convex_le (hf : antitone f) (r : Ξ²) : convex π•œ {x | f x ≀ r} := set.sep_univ.subst ((hf.antitone_on univ).convex_le convex_univ r) lemma antitone.convex_lt (hf : antitone f) (r : Ξ²) : convex π•œ {x | f x < r} := set.sep_univ.subst ((hf.antitone_on univ).convex_lt convex_univ r) lemma antitone.convex_ge (hf : antitone f) (r : Ξ²) : convex π•œ {x | r ≀ f x} := set.sep_univ.subst ((hf.antitone_on univ).convex_ge convex_univ r) lemma antitone.convex_gt (hf : antitone f) (r : Ξ²) : convex π•œ {x | r < f x} := set.sep_univ.subst ((hf.antitone_on univ).convex_gt convex_univ r) end linear_ordered_add_comm_monoid section add_comm_group variables [add_comm_group E] [module π•œ E] {s t : set E} lemma convex.combo_eq_vadd {a b : π•œ} {x y : E} (h : a + b = 1) : a β€’ x + b β€’ y = b β€’ (y - x) + x := calc a β€’ x + b β€’ y = (b β€’ y - b β€’ x) + (a β€’ x + b β€’ x) : by abel ... = b β€’ (y - x) + x : by rw [smul_sub, convex.combo_self h] lemma convex.sub (hs : convex π•œ s) (ht : convex π•œ t) : convex π•œ ((Ξ» x : E Γ— E, x.1 - x.2) '' (s.prod t)) := (hs.prod ht).is_linear_image is_linear_map.is_linear_map_sub lemma convex_segment (x y : E) : convex π•œ [x -[π•œ] y] := begin rintro p q ⟨ap, bp, hap, hbp, habp, rfl⟩ ⟨aq, bq, haq, hbq, habq, rfl⟩ a b ha hb hab, refine ⟨a * ap + b * aq, a * bp + b * bq, add_nonneg (mul_nonneg ha hap) (mul_nonneg hb haq), add_nonneg (mul_nonneg ha hbp) (mul_nonneg hb hbq), _, _⟩, { rw [add_add_add_comm, ←mul_add, ←mul_add, habp, habq, mul_one, mul_one, hab] }, { simp_rw [add_smul, mul_smul, smul_add], exact add_add_add_comm _ _ _ _ } end lemma convex_open_segment (a b : E) : convex π•œ (open_segment π•œ a b) := begin rw convex_iff_open_segment_subset, rintro p q ⟨ap, bp, hap, hbp, habp, rfl⟩ ⟨aq, bq, haq, hbq, habq, rfl⟩ z ⟨a, b, ha, hb, hab, rfl⟩, refine ⟨a * ap + b * aq, a * bp + b * bq, add_pos (mul_pos ha hap) (mul_pos hb haq), add_pos (mul_pos ha hbp) (mul_pos hb hbq), _, _⟩, { rw [add_add_add_comm, ←mul_add, ←mul_add, habp, habq, mul_one, mul_one, hab] }, { simp_rw [add_smul, mul_smul, smul_add], exact add_add_add_comm _ _ _ _ } end end add_comm_group end ordered_semiring section ordered_comm_semiring variables [ordered_comm_semiring π•œ] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] [module π•œ E] [module π•œ F] {s : set E} lemma convex.smul (hs : convex π•œ s) (c : π•œ) : convex π•œ (c β€’ s) := hs.linear_image (linear_map.lsmul _ _ c) lemma convex.smul_preimage (hs : convex π•œ s) (c : π•œ) : convex π•œ ((Ξ» z, c β€’ z) ⁻¹' s) := hs.linear_preimage (linear_map.lsmul _ _ c) lemma convex.affinity (hs : convex π•œ s) (z : E) (c : π•œ) : convex π•œ ((Ξ» x, z + c β€’ x) '' s) := begin have h := (hs.smul c).translate z, rwa [←image_smul, image_image] at h, end end add_comm_monoid end ordered_comm_semiring section ordered_ring variables [ordered_ring π•œ] section add_comm_group variables [add_comm_group E] [add_comm_group F] [module π•œ E] [module π•œ F] {s : set E} lemma convex.add_smul_mem (hs : convex π•œ s) {x y : E} (hx : x ∈ s) (hy : x + y ∈ s) {t : π•œ} (ht : t ∈ Icc (0 : π•œ) 1) : x + t β€’ y ∈ s := begin have h : x + t β€’ y = (1 - t) β€’ x + t β€’ (x + y), { rw [smul_add, ←add_assoc, ←add_smul, sub_add_cancel, one_smul] }, rw h, exact hs hx hy (sub_nonneg_of_le ht.2) ht.1 (sub_add_cancel _ _), end lemma convex.smul_mem_of_zero_mem (hs : convex π•œ s) {x : E} (zero_mem : (0 : E) ∈ s) (hx : x ∈ s) {t : π•œ} (ht : t ∈ Icc (0 : π•œ) 1) : t β€’ x ∈ s := by simpa using hs.add_smul_mem zero_mem (by simpa using hx) ht lemma convex.add_smul_sub_mem (h : convex π•œ s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {t : π•œ} (ht : t ∈ Icc (0 : π•œ) 1) : x + t β€’ (y - x) ∈ s := begin apply h.segment_subset hx hy, rw segment_eq_image', exact mem_image_of_mem _ ht, end /-- Affine subspaces are convex. -/ lemma affine_subspace.convex (Q : affine_subspace π•œ E) : convex π•œ (Q : set E) := begin intros x y hx hy a b ha hb hab, rw [eq_sub_of_add_eq hab, ← affine_map.line_map_apply_module], exact affine_map.line_map_mem b hx hy, end /-- Applying an affine map to an affine combination of two points yields an affine combination of the images. -/ lemma convex.combo_affine_apply {a b : π•œ} {x y : E} {f : E →ᡃ[π•œ] F} (h : a + b = 1) : f (a β€’ x + b β€’ y) = a β€’ f x + b β€’ f y := begin simp only [convex.combo_eq_vadd h, ← vsub_eq_sub], exact f.apply_line_map _ _ _, end /-- The preimage of a convex set under an affine map is convex. -/ lemma convex.affine_preimage (f : E →ᡃ[π•œ] F) {s : set F} (hs : convex π•œ s) : convex π•œ (f ⁻¹' s) := begin intros x y xs ys a b ha hb hab, rw [mem_preimage, convex.combo_affine_apply hab], exact hs xs ys ha hb hab, end /-- The image of a convex set under an affine map is convex. -/ lemma convex.affine_image (f : E →ᡃ[π•œ] F) {s : set E} (hs : convex π•œ s) : convex π•œ (f '' s) := begin rintro x y ⟨x', ⟨hx', hx'f⟩⟩ ⟨y', ⟨hy', hy'f⟩⟩ a b ha hb hab, refine ⟨a β€’ x' + b β€’ y', ⟨hs hx' hy' ha hb hab, _⟩⟩, rw [convex.combo_affine_apply hab, hx'f, hy'f] end lemma convex.neg (hs : convex π•œ s) : convex π•œ ((Ξ» z, -z) '' s) := hs.is_linear_image is_linear_map.is_linear_map_neg lemma convex.neg_preimage (hs : convex π•œ s) : convex π•œ ((Ξ» z, -z) ⁻¹' s) := hs.is_linear_preimage is_linear_map.is_linear_map_neg end add_comm_group end ordered_ring section linear_ordered_field variables [linear_ordered_field π•œ] section add_comm_group variables [add_comm_group E] [add_comm_group F] [module π•œ E] [module π•œ F] {s : set E} /-- Alternative definition of set convexity, using division. -/ lemma convex_iff_div : convex π•œ s ↔ βˆ€ ⦃x y : E⦄, x ∈ s β†’ y ∈ s β†’ βˆ€ ⦃a b : π•œβ¦„, 0 ≀ a β†’ 0 ≀ b β†’ 0 < a + b β†’ (a/(a+b)) β€’ x + (b/(a+b)) β€’ y ∈ s := ⟨λ h x y hx hy a b ha hb hab, begin apply h hx hy, { have ha', from mul_le_mul_of_nonneg_left ha (inv_pos.2 hab).le, rwa [mul_zero, ←div_eq_inv_mul] at ha' }, { have hb', from mul_le_mul_of_nonneg_left hb (inv_pos.2 hab).le, rwa [mul_zero, ←div_eq_inv_mul] at hb' }, { rw ←add_div, exact div_self hab.ne' } end, Ξ» h x y hx hy a b ha hb hab, begin have h', from h hx hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ lemma convex.mem_smul_of_zero_mem (h : convex π•œ s) {x : E} (zero_mem : (0 : E) ∈ s) (hx : x ∈ s) {t : π•œ} (ht : 1 ≀ t) : x ∈ t β€’ s := begin rw mem_smul_set_iff_inv_smul_memβ‚€ (zero_lt_one.trans_le ht).ne', exact h.smul_mem_of_zero_mem zero_mem hx ⟨inv_nonneg.2 (zero_le_one.trans ht), inv_le_one ht⟩, end lemma convex.add_smul (h_conv : convex π•œ s) {p q : π•œ} (hp : 0 ≀ p) (hq : 0 ≀ q) : (p + q) β€’ s = p β€’ s + q β€’ s := begin obtain rfl | hs := s.eq_empty_or_nonempty, { simp_rw [smul_set_empty, add_empty] }, obtain rfl | hp' := hp.eq_or_lt, { rw [zero_add, zero_smul_set hs, zero_add] }, obtain rfl | hq' := hq.eq_or_lt, { rw [add_zero, zero_smul_set hs, add_zero] }, ext, split, { rintro ⟨v, hv, rfl⟩, exact ⟨p β€’ v, q β€’ v, smul_mem_smul_set hv, smul_mem_smul_set hv, (add_smul _ _ _).symm⟩ }, { rintro ⟨v₁, vβ‚‚, ⟨v₁₁, h₁₂, rfl⟩, ⟨v₂₁, hβ‚‚β‚‚, rfl⟩, rfl⟩, have hpq := add_pos hp' hq', exact mem_smul_set.2 ⟨_, h_conv h₁₂ hβ‚‚β‚‚ (div_pos hp' hpq).le (div_pos hq' hpq).le (by rw [←div_self hpq.ne', add_div] : p / (p + q) + q / (p + q) = 1), by simp only [← mul_smul, smul_add, mul_div_cancel' _ hpq.ne']⟩ } end end add_comm_group end linear_ordered_field /-! #### Convex sets in an ordered space Relates `convex` and `ord_connected`. -/ section lemma set.ord_connected.convex_of_chain [ordered_semiring π•œ] [ordered_add_comm_monoid E] [module π•œ E] [ordered_smul π•œ E] {s : set E} (hs : s.ord_connected) (h : zorn.chain (≀) s) : convex π•œ s := begin intros x y hx hy a b ha hb hab, obtain hxy | hyx := h.total_of_refl hx hy, { refine hs.out hx hy (mem_Icc.2 ⟨_, _⟩), calc x = a β€’ x + b β€’ x : (convex.combo_self hab _).symm ... ≀ a β€’ x + b β€’ y : add_le_add_left (smul_le_smul_of_nonneg hxy hb) _, calc a β€’ x + b β€’ y ≀ a β€’ y + b β€’ y : add_le_add_right (smul_le_smul_of_nonneg hxy ha) _ ... = y : convex.combo_self hab _ }, { refine hs.out hy hx (mem_Icc.2 ⟨_, _⟩), calc y = a β€’ y + b β€’ y : (convex.combo_self hab _).symm ... ≀ a β€’ x + b β€’ y : add_le_add_right (smul_le_smul_of_nonneg hyx ha) _, calc a β€’ x + b β€’ y ≀ a β€’ x + b β€’ x : add_le_add_left (smul_le_smul_of_nonneg hyx hb) _ ... = x : convex.combo_self hab _ } end lemma set.ord_connected.convex [ordered_semiring π•œ] [linear_ordered_add_comm_monoid E] [module π•œ E] [ordered_smul π•œ E] {s : set E} (hs : s.ord_connected) : convex π•œ s := hs.convex_of_chain (zorn.chain_of_trichotomous s) lemma convex_iff_ord_connected [linear_ordered_field π•œ] {s : set π•œ} : convex π•œ s ↔ s.ord_connected := begin simp_rw [convex_iff_segment_subset, segment_eq_interval, ord_connected_iff_interval_subset], exact forall_congr (Ξ» x, forall_swap) end alias convex_iff_ord_connected ↔ convex.ord_connected _ end /-! #### Convexity of submodules/subspaces -/ section submodule open submodule lemma submodule.convex [ordered_semiring π•œ] [add_comm_monoid E] [module π•œ E] (K : submodule π•œ E) : convex π•œ (↑K : set E) := by { repeat {intro}, refine add_mem _ (smul_mem _ _ _) (smul_mem _ _ _); assumption } lemma subspace.convex [linear_ordered_field π•œ] [add_comm_group E] [module π•œ E] (K : subspace π•œ E) : convex π•œ (↑K : set E) := K.convex end submodule /-! ### Simplex -/ section simplex variables (π•œ) (ΞΉ : Type*) [ordered_semiring π•œ] [fintype ΞΉ] /-- The standard simplex in the space of functions `ΞΉ β†’ π•œ` is the set of vectors with non-negative coordinates with total sum `1`. This is the free object in the category of convex spaces. -/ def std_simplex : set (ΞΉ β†’ π•œ) := {f | (βˆ€ x, 0 ≀ f x) ∧ βˆ‘ x, f x = 1} lemma std_simplex_eq_inter : std_simplex π•œ ΞΉ = (β‹‚ x, {f | 0 ≀ f x}) ∩ {f | βˆ‘ x, f x = 1} := by { ext f, simp only [std_simplex, set.mem_inter_eq, set.mem_Inter, set.mem_set_of_eq] } lemma convex_std_simplex : convex π•œ (std_simplex π•œ ΞΉ) := begin refine Ξ» f g hf hg a b ha hb hab, ⟨λ x, _, _⟩, { apply_rules [add_nonneg, mul_nonneg, hf.1, hg.1] }, { erw [finset.sum_add_distrib, ← finset.smul_sum, ← finset.smul_sum, hf.2, hg.2, smul_eq_mul, smul_eq_mul, mul_one, mul_one], exact hab } end variable {ΞΉ} lemma ite_eq_mem_std_simplex (i : ΞΉ) : (Ξ» j, ite (i = j) (1:π•œ) 0) ∈ std_simplex π•œ ΞΉ := ⟨λ j, by simp only; split_ifs; norm_num, by rw [finset.sum_ite_eq, if_pos (finset.mem_univ _)]⟩ end simplex
d1bc322e6e0ae6ee483816d820377481fad03511
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/analysis/asymptotics/asymptotics.lean
152dba0a3ee156dc89f94b994118a3c6c2cdd3e6
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
61,421
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Yury Kudryashov -/ import analysis.normed_space.basic import topology.local_homeomorph /-! # Asymptotics We introduce these relations: * `is_O_with c f g l` : "f is big O of g along l with constant c"; * `is_O f g l` : "f is big O of g along l"; * `is_o f g l` : "f is little o of g along l". Here `l` is any filter on the domain of `f` and `g`, which are assumed to be the same. The codomains of `f` and `g` do not need to be the same; all that is needed that there is a norm associated with these types, and it is the norm that is compared asymptotically. The relation `is_O_with c` is introduced to factor out common algebraic arguments in the proofs of similar properties of `is_O` and `is_o`. Usually proofs outside of this file should use `is_O` instead. Often the ranges of `f` and `g` will be the real numbers, in which case the norm is the absolute value. In general, we have `is_O f g l ↔ is_O (Ξ» x, βˆ₯f xβˆ₯) (Ξ» x, βˆ₯g xβˆ₯) l`, and similarly for `is_o`. But our setup allows us to use the notions e.g. with functions to the integers, rationals, complex numbers, or any normed vector space without mentioning the norm explicitly. If `f` and `g` are functions to a normed field like the reals or complex numbers and `g` is always nonzero, we have `is_o f g l ↔ tendsto (Ξ» x, f x / (g x)) l (𝓝 0)`. In fact, the right-to-left direction holds without the hypothesis on `g`, and in the other direction it suffices to assume that `f` is zero wherever `g` is. (This generalization is useful in defining the FrΓ©chet derivative.) -/ open filter set open_locale topological_space big_operators classical filter nnreal namespace asymptotics variables {Ξ± : Type*} {Ξ² : Type*} {E : Type*} {F : Type*} {G : Type*} {E' : Type*} {F' : Type*} {G' : Type*} {R : Type*} {R' : Type*} {π•œ : Type*} {π•œ' : Type*} variables [has_norm E] [has_norm F] [has_norm G] [normed_group E'] [normed_group F'] [normed_group G'] [normed_ring R] [normed_ring R'] [normed_field π•œ] [normed_field π•œ'] {c c' : ℝ} {f : Ξ± β†’ E} {g : Ξ± β†’ F} {k : Ξ± β†’ G} {f' : Ξ± β†’ E'} {g' : Ξ± β†’ F'} {k' : Ξ± β†’ G'} {l l' : filter Ξ±} section defs /-! ### Definitions -/ /-- This version of the Landau notation `is_O_with C f g l` where `f` and `g` are two functions on a type `Ξ±` and `l` is a filter on `Ξ±`, means that eventually for `l`, `βˆ₯fβˆ₯` is bounded by `C * βˆ₯gβˆ₯`. In other words, `βˆ₯fβˆ₯ / βˆ₯gβˆ₯` is eventually bounded by `C`, modulo division by zero issues that are avoided by this definition. Probably you want to use `is_O` instead of this relation. -/ @[irreducible] def is_O_with (c : ℝ) (f : Ξ± β†’ E) (g : Ξ± β†’ F) (l : filter Ξ±) : Prop := βˆ€αΆ  x in l, βˆ₯ f x βˆ₯ ≀ c * βˆ₯ g x βˆ₯ /-- Definition of `is_O_with`. We record it in a lemma as we will set `is_O_with` to be irreducible at the end of this file. -/ lemma is_O_with_iff {c : ℝ} {f : Ξ± β†’ E} {g : Ξ± β†’ F} {l : filter Ξ±} : is_O_with c f g l ↔ βˆ€αΆ  x in l, βˆ₯ f x βˆ₯ ≀ c * βˆ₯ g x βˆ₯ := by rw is_O_with alias is_O_with_iff ↔ asymptotics.is_O_with.bound asymptotics.is_O_with.of_bound /-- The Landau notation `is_O f g l` where `f` and `g` are two functions on a type `Ξ±` and `l` is a filter on `Ξ±`, means that eventually for `l`, `βˆ₯fβˆ₯` is bounded by a constant multiple of `βˆ₯gβˆ₯`. In other words, `βˆ₯fβˆ₯ / βˆ₯gβˆ₯` is eventually bounded, modulo division by zero issues that are avoided by this definition. -/ @[irreducible] def is_O (f : Ξ± β†’ E) (g : Ξ± β†’ F) (l : filter Ξ±) : Prop := βˆƒ c : ℝ, is_O_with c f g l /-- Definition of `is_O` in terms of `is_O_with`. We record it in a lemma as we will set `is_O` to be irreducible at the end of this file. -/ lemma is_O_iff_is_O_with {f : Ξ± β†’ E} {g : Ξ± β†’ F} {l : filter Ξ±} : is_O f g l ↔ βˆƒ c : ℝ, is_O_with c f g l := by rw is_O /-- Definition of `is_O` in terms of filters. We record it in a lemma as we will set `is_O` to be irreducible at the end of this file. -/ lemma is_O_iff {f : Ξ± β†’ E} {g : Ξ± β†’ F} {l : filter Ξ±} : is_O f g l ↔ βˆƒ c : ℝ, βˆ€αΆ  x in l, βˆ₯ f x βˆ₯ ≀ c * βˆ₯ g x βˆ₯ := by simp [is_O, is_O_with] lemma is_O.of_bound (c : ℝ) {f : Ξ± β†’ E} {g : Ξ± β†’ F} {l : filter Ξ±} (h : βˆ€αΆ  x in l, βˆ₯ f x βˆ₯ ≀ c * βˆ₯ g x βˆ₯) : is_O f g l := is_O_iff.2 ⟨c, h⟩ lemma is_O.bound {f : Ξ± β†’ E} {g : Ξ± β†’ F} {l : filter Ξ±} : is_O f g l β†’ βˆƒ c : ℝ, βˆ€αΆ  x in l, βˆ₯ f x βˆ₯ ≀ c * βˆ₯ g x βˆ₯ := is_O_iff.1 /-- The Landau notation `is_o f g l` where `f` and `g` are two functions on a type `Ξ±` and `l` is a filter on `Ξ±`, means that eventually for `l`, `βˆ₯fβˆ₯` is bounded by an arbitrarily small constant multiple of `βˆ₯gβˆ₯`. In other words, `βˆ₯fβˆ₯ / βˆ₯gβˆ₯` tends to `0` along `l`, modulo division by zero issues that are avoided by this definition. -/ @[irreducible] def is_o (f : Ξ± β†’ E) (g : Ξ± β†’ F) (l : filter Ξ±) : Prop := βˆ€ ⦃c : ℝ⦄, 0 < c β†’ is_O_with c f g l /-- Definition of `is_o` in terms of `is_O_with`. We record it in a lemma as we will set `is_o` to be irreducible at the end of this file. -/ lemma is_o_iff_forall_is_O_with {f : Ξ± β†’ E} {g : Ξ± β†’ F} {l : filter Ξ±} : is_o f g l ↔ βˆ€ ⦃c : ℝ⦄, 0 < c β†’ is_O_with c f g l := by rw is_o alias is_o_iff_forall_is_O_with ↔ asymptotics.is_o.forall_is_O_with asymptotics.is_o.of_is_O_with /-- Definition of `is_o` in terms of filters. We record it in a lemma as we will set `is_o` to be irreducible at the end of this file. -/ lemma is_o_iff {f : Ξ± β†’ E} {g : Ξ± β†’ F} {l : filter Ξ±} : is_o f g l ↔ βˆ€ ⦃c : ℝ⦄, 0 < c β†’ βˆ€αΆ  x in l, βˆ₯ f x βˆ₯ ≀ c * βˆ₯ g x βˆ₯ := by simp only [is_o, is_O_with] alias is_o_iff ↔ asymptotics.is_o.bound asymptotics.is_o.of_bound lemma is_o.def {f : Ξ± β†’ E} {g : Ξ± β†’ F} {l : filter Ξ±} (h : is_o f g l) {c : ℝ} (hc : 0 < c) : βˆ€αΆ  x in l, βˆ₯ f x βˆ₯ ≀ c * βˆ₯ g x βˆ₯ := is_o_iff.1 h hc lemma is_o.def' {f : Ξ± β†’ E} {g : Ξ± β†’ F} {l : filter Ξ±} (h : is_o f g l) {c : ℝ} (hc : 0 < c) : is_O_with c f g l := is_O_with_iff.2 $ is_o_iff.1 h hc end defs /-! ### Conversions -/ theorem is_O_with.is_O (h : is_O_with c f g l) : is_O f g l := by rw is_O; exact ⟨c, h⟩ theorem is_o.is_O_with (hgf : is_o f g l) : is_O_with 1 f g l := hgf.def' zero_lt_one theorem is_o.is_O (hgf : is_o f g l) : is_O f g l := hgf.is_O_with.is_O lemma is_O.is_O_with {f : Ξ± β†’ E} {g : Ξ± β†’ F} {l : filter Ξ±} : is_O f g l β†’ βˆƒ c : ℝ, is_O_with c f g l := is_O_iff_is_O_with.1 theorem is_O_with.weaken (h : is_O_with c f g' l) (hc : c ≀ c') : is_O_with c' f g' l := is_O_with.of_bound $ mem_of_superset h.bound $ Ξ» x hx, calc βˆ₯f xβˆ₯ ≀ c * βˆ₯g' xβˆ₯ : hx ... ≀ _ : mul_le_mul_of_nonneg_right hc (norm_nonneg _) theorem is_O_with.exists_pos (h : is_O_with c f g' l) : βˆƒ c' (H : 0 < c'), is_O_with c' f g' l := ⟨max c 1, lt_of_lt_of_le zero_lt_one (le_max_right c 1), h.weaken $ le_max_left c 1⟩ theorem is_O.exists_pos (h : is_O f g' l) : βˆƒ c (H : 0 < c), is_O_with c f g' l := let ⟨c, hc⟩ := h.is_O_with in hc.exists_pos theorem is_O_with.exists_nonneg (h : is_O_with c f g' l) : βˆƒ c' (H : 0 ≀ c'), is_O_with c' f g' l := let ⟨c, cpos, hc⟩ := h.exists_pos in ⟨c, le_of_lt cpos, hc⟩ theorem is_O.exists_nonneg (h : is_O f g' l) : βˆƒ c (H : 0 ≀ c), is_O_with c f g' l := let ⟨c, hc⟩ := h.is_O_with in hc.exists_nonneg /-- `f = O(g)` if and only if `is_O_with c f g` for all sufficiently large `c`. -/ lemma is_O_iff_eventually_is_O_with : is_O f g' l ↔ βˆ€αΆ  c in at_top, is_O_with c f g' l := is_O_iff_is_O_with.trans ⟨λ ⟨c, hc⟩, mem_at_top_sets.2 ⟨c, Ξ» c' hc', hc.weaken hc'⟩, Ξ» h, h.exists⟩ /-- `f = O(g)` if and only if `βˆ€αΆ  x in l, βˆ₯f xβˆ₯ ≀ c * βˆ₯g xβˆ₯` for all sufficiently large `c`. -/ lemma is_O_iff_eventually : is_O f g' l ↔ βˆ€αΆ  c in at_top, βˆ€αΆ  x in l, βˆ₯f xβˆ₯ ≀ c * βˆ₯g' xβˆ₯ := is_O_iff_eventually_is_O_with.trans $ by simp only [is_O_with] /-! ### Subsingleton -/ @[nontriviality] lemma is_o_of_subsingleton [subsingleton E'] : is_o f' g' l := is_o.of_bound $ Ξ» c hc, by simp [subsingleton.elim (f' _) 0, mul_nonneg hc.le] @[nontriviality] lemma is_O_of_subsingleton [subsingleton E'] : is_O f' g' l := is_o_of_subsingleton.is_O /-! ### Congruence -/ theorem is_O_with_congr {c₁ cβ‚‚} {f₁ fβ‚‚ : Ξ± β†’ E} {g₁ gβ‚‚ : Ξ± β†’ F} {l : filter Ξ±} (hc : c₁ = cβ‚‚) (hf : f₁ =αΆ [l] fβ‚‚) (hg : g₁ =αΆ [l] gβ‚‚) : is_O_with c₁ f₁ g₁ l ↔ is_O_with cβ‚‚ fβ‚‚ gβ‚‚ l := begin unfold is_O_with, subst cβ‚‚, apply filter.eventually_congr, filter_upwards [hf, hg], assume x e₁ eβ‚‚, rw [e₁, eβ‚‚] end theorem is_O_with.congr' {c₁ cβ‚‚} {f₁ fβ‚‚ : Ξ± β†’ E} {g₁ gβ‚‚ : Ξ± β†’ F} {l : filter Ξ±} (hc : c₁ = cβ‚‚) (hf : f₁ =αΆ [l] fβ‚‚) (hg : g₁ =αΆ [l] gβ‚‚) : is_O_with c₁ f₁ g₁ l β†’ is_O_with cβ‚‚ fβ‚‚ gβ‚‚ l := (is_O_with_congr hc hf hg).mp theorem is_O_with.congr {c₁ cβ‚‚} {f₁ fβ‚‚ : Ξ± β†’ E} {g₁ gβ‚‚ : Ξ± β†’ F} {l : filter Ξ±} (hc : c₁ = cβ‚‚) (hf : βˆ€ x, f₁ x = fβ‚‚ x) (hg : βˆ€ x, g₁ x = gβ‚‚ x) : is_O_with c₁ f₁ g₁ l β†’ is_O_with cβ‚‚ fβ‚‚ gβ‚‚ l := Ξ» h, h.congr' hc (univ_mem' hf) (univ_mem' hg) theorem is_O_with.congr_left {f₁ fβ‚‚ : Ξ± β†’ E} {l : filter Ξ±} (hf : βˆ€ x, f₁ x = fβ‚‚ x) : is_O_with c f₁ g l β†’ is_O_with c fβ‚‚ g l := is_O_with.congr rfl hf (Ξ» _, rfl) theorem is_O_with.congr_right {g₁ gβ‚‚ : Ξ± β†’ F} {l : filter Ξ±} (hg : βˆ€ x, g₁ x = gβ‚‚ x) : is_O_with c f g₁ l β†’ is_O_with c f gβ‚‚ l := is_O_with.congr rfl (Ξ» _, rfl) hg theorem is_O_with.congr_const {c₁ cβ‚‚} {l : filter Ξ±} (hc : c₁ = cβ‚‚) : is_O_with c₁ f g l β†’ is_O_with cβ‚‚ f g l := is_O_with.congr hc (Ξ» _, rfl) (Ξ» _, rfl) theorem is_O_congr {f₁ fβ‚‚ : Ξ± β†’ E} {g₁ gβ‚‚ : Ξ± β†’ F} {l : filter Ξ±} (hf : f₁ =αΆ [l] fβ‚‚) (hg : g₁ =αΆ [l] gβ‚‚) : is_O f₁ g₁ l ↔ is_O fβ‚‚ gβ‚‚ l := by { unfold is_O, exact exists_congr (Ξ» c, is_O_with_congr rfl hf hg) } theorem is_O.congr' {f₁ fβ‚‚ : Ξ± β†’ E} {g₁ gβ‚‚ : Ξ± β†’ F} {l : filter Ξ±} (hf : f₁ =αΆ [l] fβ‚‚) (hg : g₁ =αΆ [l] gβ‚‚) : is_O f₁ g₁ l β†’ is_O fβ‚‚ gβ‚‚ l := (is_O_congr hf hg).mp theorem is_O.congr {f₁ fβ‚‚ : Ξ± β†’ E} {g₁ gβ‚‚ : Ξ± β†’ F} {l : filter Ξ±} (hf : βˆ€ x, f₁ x = fβ‚‚ x) (hg : βˆ€ x, g₁ x = gβ‚‚ x) : is_O f₁ g₁ l β†’ is_O fβ‚‚ gβ‚‚ l := Ξ» h, h.congr' (univ_mem' hf) (univ_mem' hg) theorem is_O.congr_left {f₁ fβ‚‚ : Ξ± β†’ E} {l : filter Ξ±} (hf : βˆ€ x, f₁ x = fβ‚‚ x) : is_O f₁ g l β†’ is_O fβ‚‚ g l := is_O.congr hf (Ξ» _, rfl) theorem is_O.congr_right {g₁ gβ‚‚ : Ξ± β†’ E} {l : filter Ξ±} (hg : βˆ€ x, g₁ x = gβ‚‚ x) : is_O f g₁ l β†’ is_O f gβ‚‚ l := is_O.congr (Ξ» _, rfl) hg theorem is_o_congr {f₁ fβ‚‚ : Ξ± β†’ E} {g₁ gβ‚‚ : Ξ± β†’ F} {l : filter Ξ±} (hf : f₁ =αΆ [l] fβ‚‚) (hg : g₁ =αΆ [l] gβ‚‚) : is_o f₁ g₁ l ↔ is_o fβ‚‚ gβ‚‚ l := by { unfold is_o, exact ball_congr (Ξ» c hc, is_O_with_congr (eq.refl c) hf hg) } theorem is_o.congr' {f₁ fβ‚‚ : Ξ± β†’ E} {g₁ gβ‚‚ : Ξ± β†’ F} {l : filter Ξ±} (hf : f₁ =αΆ [l] fβ‚‚) (hg : g₁ =αΆ [l] gβ‚‚) : is_o f₁ g₁ l β†’ is_o fβ‚‚ gβ‚‚ l := (is_o_congr hf hg).mp theorem is_o.congr {f₁ fβ‚‚ : Ξ± β†’ E} {g₁ gβ‚‚ : Ξ± β†’ F} {l : filter Ξ±} (hf : βˆ€ x, f₁ x = fβ‚‚ x) (hg : βˆ€ x, g₁ x = gβ‚‚ x) : is_o f₁ g₁ l β†’ is_o fβ‚‚ gβ‚‚ l := Ξ» h, h.congr' (univ_mem' hf) (univ_mem' hg) theorem is_o.congr_left {f₁ fβ‚‚ : Ξ± β†’ E} {l : filter Ξ±} (hf : βˆ€ x, f₁ x = fβ‚‚ x) : is_o f₁ g l β†’ is_o fβ‚‚ g l := is_o.congr hf (Ξ» _, rfl) theorem is_o.congr_right {g₁ gβ‚‚ : Ξ± β†’ E} {l : filter Ξ±} (hg : βˆ€ x, g₁ x = gβ‚‚ x) : is_o f g₁ l β†’ is_o f gβ‚‚ l := is_o.congr (Ξ» _, rfl) hg /-! ### Filter operations and transitivity -/ theorem is_O_with.comp_tendsto (hcfg : is_O_with c f g l) {k : Ξ² β†’ Ξ±} {l' : filter Ξ²} (hk : tendsto k l' l): is_O_with c (f ∘ k) (g ∘ k) l' := is_O_with.of_bound $ hk hcfg.bound theorem is_O.comp_tendsto (hfg : is_O f g l) {k : Ξ² β†’ Ξ±} {l' : filter Ξ²} (hk : tendsto k l' l) : is_O (f ∘ k) (g ∘ k) l' := is_O_iff_is_O_with.2 $ hfg.is_O_with.imp (Ξ» c h, h.comp_tendsto hk) theorem is_o.comp_tendsto (hfg : is_o f g l) {k : Ξ² β†’ Ξ±} {l' : filter Ξ²} (hk : tendsto k l' l) : is_o (f ∘ k) (g ∘ k) l' := is_o.of_is_O_with $ Ξ» c cpos, (hfg.forall_is_O_with cpos).comp_tendsto hk @[simp] theorem is_O_with_map {k : Ξ² β†’ Ξ±} {l : filter Ξ²} : is_O_with c f g (map k l) ↔ is_O_with c (f ∘ k) (g ∘ k) l := by { unfold is_O_with, exact mem_map } @[simp] theorem is_O_map {k : Ξ² β†’ Ξ±} {l : filter Ξ²} : is_O f g (map k l) ↔ is_O (f ∘ k) (g ∘ k) l := by simp only [is_O, is_O_with_map] @[simp] theorem is_o_map {k : Ξ² β†’ Ξ±} {l : filter Ξ²} : is_o f g (map k l) ↔ is_o (f ∘ k) (g ∘ k) l := by simp only [is_o, is_O_with_map] theorem is_O_with.mono (h : is_O_with c f g l') (hl : l ≀ l') : is_O_with c f g l := is_O_with.of_bound $ hl h.bound theorem is_O.mono (h : is_O f g l') (hl : l ≀ l') : is_O f g l := is_O_iff_is_O_with.2 $ h.is_O_with.imp (Ξ» c h, h.mono hl) theorem is_o.mono (h : is_o f g l') (hl : l ≀ l') : is_o f g l := is_o.of_is_O_with $ Ξ» c cpos, (h.forall_is_O_with cpos).mono hl theorem is_O_with.trans (hfg : is_O_with c f g l) (hgk : is_O_with c' g k l) (hc : 0 ≀ c) : is_O_with (c * c') f k l := begin unfold is_O_with at *, filter_upwards [hfg, hgk], assume x hx hx', calc βˆ₯f xβˆ₯ ≀ c * βˆ₯g xβˆ₯ : hx ... ≀ c * (c' * βˆ₯k xβˆ₯) : mul_le_mul_of_nonneg_left hx' hc ... = c * c' * βˆ₯k xβˆ₯ : (mul_assoc _ _ _).symm end theorem is_O.trans (hfg : is_O f g' l) (hgk : is_O g' k l) : is_O f k l := let ⟨c, cnonneg, hc⟩ := hfg.exists_nonneg, ⟨c', hc'⟩ := hgk.is_O_with in (hc.trans hc' cnonneg).is_O theorem is_o.trans_is_O_with (hfg : is_o f g l) (hgk : is_O_with c g k l) (hc : 0 < c) : is_o f k l := begin unfold is_o at *, intros c' c'pos, have : 0 < c' / c, from div_pos c'pos hc, exact ((hfg this).trans hgk (le_of_lt this)).congr_const (div_mul_cancel _ (ne_of_gt hc)) end theorem is_o.trans_is_O (hfg : is_o f g l) (hgk : is_O g k' l) : is_o f k' l := let ⟨c, cpos, hc⟩ := hgk.exists_pos in hfg.trans_is_O_with hc cpos theorem is_O_with.trans_is_o (hfg : is_O_with c f g l) (hgk : is_o g k l) (hc : 0 < c) : is_o f k l := begin unfold is_o at *, intros c' c'pos, have : 0 < c' / c, from div_pos c'pos hc, exact (hfg.trans (hgk this) (le_of_lt hc)).congr_const (mul_div_cancel' _ (ne_of_gt hc)) end theorem is_O.trans_is_o (hfg : is_O f g' l) (hgk : is_o g' k l) : is_o f k l := let ⟨c, cpos, hc⟩ := hfg.exists_pos in hc.trans_is_o hgk cpos theorem is_o.trans (hfg : is_o f g l) (hgk : is_o g k' l) : is_o f k' l := hfg.trans_is_O hgk.is_O theorem is_o.trans' (hfg : is_o f g' l) (hgk : is_o g' k l) : is_o f k l := hfg.is_O.trans_is_o hgk section variable (l) theorem is_O_with_of_le' (hfg : βˆ€ x, βˆ₯f xβˆ₯ ≀ c * βˆ₯g xβˆ₯) : is_O_with c f g l := is_O_with.of_bound $ univ_mem' hfg theorem is_O_with_of_le (hfg : βˆ€ x, βˆ₯f xβˆ₯ ≀ βˆ₯g xβˆ₯) : is_O_with 1 f g l := is_O_with_of_le' l $ Ξ» x, by { rw one_mul, exact hfg x } theorem is_O_of_le' (hfg : βˆ€ x, βˆ₯f xβˆ₯ ≀ c * βˆ₯g xβˆ₯) : is_O f g l := (is_O_with_of_le' l hfg).is_O theorem is_O_of_le (hfg : βˆ€ x, βˆ₯f xβˆ₯ ≀ βˆ₯g xβˆ₯) : is_O f g l := (is_O_with_of_le l hfg).is_O end theorem is_O_with_refl (f : Ξ± β†’ E) (l : filter Ξ±) : is_O_with 1 f f l := is_O_with_of_le l $ Ξ» _, le_refl _ theorem is_O_refl (f : Ξ± β†’ E) (l : filter Ξ±) : is_O f f l := (is_O_with_refl f l).is_O theorem is_O_with.trans_le (hfg : is_O_with c f g l) (hgk : βˆ€ x, βˆ₯g xβˆ₯ ≀ βˆ₯k xβˆ₯) (hc : 0 ≀ c) : is_O_with c f k l := (hfg.trans (is_O_with_of_le l hgk) hc).congr_const $ mul_one c theorem is_O.trans_le (hfg : is_O f g' l) (hgk : βˆ€ x, βˆ₯g' xβˆ₯ ≀ βˆ₯k xβˆ₯) : is_O f k l := hfg.trans (is_O_of_le l hgk) theorem is_o.trans_le (hfg : is_o f g l) (hgk : βˆ€ x, βˆ₯g xβˆ₯ ≀ βˆ₯k xβˆ₯) : is_o f k l := hfg.trans_is_O_with (is_O_with_of_le _ hgk) zero_lt_one section bot variables (c f g) @[simp] theorem is_O_with_bot : is_O_with c f g βŠ₯ := is_O_with.of_bound $ trivial @[simp] theorem is_O_bot : is_O f g βŠ₯ := (is_O_with_bot 1 f g).is_O @[simp] theorem is_o_bot : is_o f g βŠ₯ := is_o.of_is_O_with $ Ξ» c _, is_O_with_bot c f g end bot theorem is_O_with.join (h : is_O_with c f g l) (h' : is_O_with c f g l') : is_O_with c f g (l βŠ” l') := is_O_with.of_bound $ mem_sup.2 ⟨h.bound, h'.bound⟩ theorem is_O_with.join' (h : is_O_with c f g' l) (h' : is_O_with c' f g' l') : is_O_with (max c c') f g' (l βŠ” l') := is_O_with.of_bound $ mem_sup.2 ⟨(h.weaken $ le_max_left c c').bound, (h'.weaken $ le_max_right c c').bound⟩ theorem is_O.join (h : is_O f g' l) (h' : is_O f g' l') : is_O f g' (l βŠ” l') := let ⟨c, hc⟩ := h.is_O_with, ⟨c', hc'⟩ := h'.is_O_with in (hc.join' hc').is_O theorem is_o.join (h : is_o f g l) (h' : is_o f g l') : is_o f g (l βŠ” l') := is_o.of_is_O_with $ Ξ» c cpos, (h.forall_is_O_with cpos).join (h'.forall_is_O_with cpos) /-! ### Simplification : norm -/ @[simp] theorem is_O_with_norm_right : is_O_with c f (Ξ» x, βˆ₯g' xβˆ₯) l ↔ is_O_with c f g' l := by simp only [is_O_with, norm_norm] alias is_O_with_norm_right ↔ asymptotics.is_O_with.of_norm_right asymptotics.is_O_with.norm_right @[simp] theorem is_O_norm_right : is_O f (Ξ» x, βˆ₯g' xβˆ₯) l ↔ is_O f g' l := by { unfold is_O, exact exists_congr (Ξ» _, is_O_with_norm_right) } alias is_O_norm_right ↔ asymptotics.is_O.of_norm_right asymptotics.is_O.norm_right @[simp] theorem is_o_norm_right : is_o f (Ξ» x, βˆ₯g' xβˆ₯) l ↔ is_o f g' l := by { unfold is_o, exact forall_congr (Ξ» _, forall_congr $ Ξ» _, is_O_with_norm_right) } alias is_o_norm_right ↔ asymptotics.is_o.of_norm_right asymptotics.is_o.norm_right @[simp] theorem is_O_with_norm_left : is_O_with c (Ξ» x, βˆ₯f' xβˆ₯) g l ↔ is_O_with c f' g l := by simp only [is_O_with, norm_norm] alias is_O_with_norm_left ↔ asymptotics.is_O_with.of_norm_left asymptotics.is_O_with.norm_left @[simp] theorem is_O_norm_left : is_O (Ξ» x, βˆ₯f' xβˆ₯) g l ↔ is_O f' g l := by { unfold is_O, exact exists_congr (Ξ» _, is_O_with_norm_left) } alias is_O_norm_left ↔ asymptotics.is_O.of_norm_left asymptotics.is_O.norm_left @[simp] theorem is_o_norm_left : is_o (Ξ» x, βˆ₯f' xβˆ₯) g l ↔ is_o f' g l := by { unfold is_o, exact forall_congr (Ξ» _, forall_congr $ Ξ» _, is_O_with_norm_left) } alias is_o_norm_left ↔ asymptotics.is_o.of_norm_left asymptotics.is_o.norm_left theorem is_O_with_norm_norm : is_O_with c (Ξ» x, βˆ₯f' xβˆ₯) (Ξ» x, βˆ₯g' xβˆ₯) l ↔ is_O_with c f' g' l := is_O_with_norm_left.trans is_O_with_norm_right alias is_O_with_norm_norm ↔ asymptotics.is_O_with.of_norm_norm asymptotics.is_O_with.norm_norm theorem is_O_norm_norm : is_O (Ξ» x, βˆ₯f' xβˆ₯) (Ξ» x, βˆ₯g' xβˆ₯) l ↔ is_O f' g' l := is_O_norm_left.trans is_O_norm_right alias is_O_norm_norm ↔ asymptotics.is_O.of_norm_norm asymptotics.is_O.norm_norm theorem is_o_norm_norm : is_o (Ξ» x, βˆ₯f' xβˆ₯) (Ξ» x, βˆ₯g' xβˆ₯) l ↔ is_o f' g' l := is_o_norm_left.trans is_o_norm_right alias is_o_norm_norm ↔ asymptotics.is_o.of_norm_norm asymptotics.is_o.norm_norm /-! ### Simplification: negate -/ @[simp] theorem is_O_with_neg_right : is_O_with c f (Ξ» x, -(g' x)) l ↔ is_O_with c f g' l := by simp only [is_O_with, norm_neg] alias is_O_with_neg_right ↔ asymptotics.is_O_with.of_neg_right asymptotics.is_O_with.neg_right @[simp] theorem is_O_neg_right : is_O f (Ξ» x, -(g' x)) l ↔ is_O f g' l := by { unfold is_O, exact exists_congr (Ξ» _, is_O_with_neg_right) } alias is_O_neg_right ↔ asymptotics.is_O.of_neg_right asymptotics.is_O.neg_right @[simp] theorem is_o_neg_right : is_o f (Ξ» x, -(g' x)) l ↔ is_o f g' l := by { unfold is_o, exact forall_congr (Ξ» _, (forall_congr (Ξ» _, is_O_with_neg_right))) } alias is_o_neg_right ↔ asymptotics.is_o.of_neg_right asymptotics.is_o.neg_right @[simp] theorem is_O_with_neg_left : is_O_with c (Ξ» x, -(f' x)) g l ↔ is_O_with c f' g l := by simp only [is_O_with, norm_neg] alias is_O_with_neg_left ↔ asymptotics.is_O_with.of_neg_left asymptotics.is_O_with.neg_left @[simp] theorem is_O_neg_left : is_O (Ξ» x, -(f' x)) g l ↔ is_O f' g l := by { unfold is_O, exact exists_congr (Ξ» _, is_O_with_neg_left) } alias is_O_neg_left ↔ asymptotics.is_O.of_neg_left asymptotics.is_O.neg_left @[simp] theorem is_o_neg_left : is_o (Ξ» x, -(f' x)) g l ↔ is_o f' g l := by { unfold is_o, exact forall_congr (Ξ» _, (forall_congr (Ξ» _, is_O_with_neg_left))) } alias is_o_neg_left ↔ asymptotics.is_o.of_neg_right asymptotics.is_o.neg_left /-! ### Product of functions (right) -/ lemma is_O_with_fst_prod : is_O_with 1 f' (Ξ» x, (f' x, g' x)) l := is_O_with_of_le l $ Ξ» x, le_max_left _ _ lemma is_O_with_snd_prod : is_O_with 1 g' (Ξ» x, (f' x, g' x)) l := is_O_with_of_le l $ Ξ» x, le_max_right _ _ lemma is_O_fst_prod : is_O f' (Ξ» x, (f' x, g' x)) l := is_O_with_fst_prod.is_O lemma is_O_snd_prod : is_O g' (Ξ» x, (f' x, g' x)) l := is_O_with_snd_prod.is_O lemma is_O_fst_prod' {f' : Ξ± β†’ E' Γ— F'} : is_O (Ξ» x, (f' x).1) f' l := by simpa [is_O, is_O_with] using is_O_fst_prod lemma is_O_snd_prod' {f' : Ξ± β†’ E' Γ— F'} : is_O (Ξ» x, (f' x).2) f' l := by simpa [is_O, is_O_with] using is_O_snd_prod section variables (f' k') lemma is_O_with.prod_rightl (h : is_O_with c f g' l) (hc : 0 ≀ c) : is_O_with c f (Ξ» x, (g' x, k' x)) l := (h.trans is_O_with_fst_prod hc).congr_const (mul_one c) lemma is_O.prod_rightl (h : is_O f g' l) : is_O f (Ξ»x, (g' x, k' x)) l := let ⟨c, cnonneg, hc⟩ := h.exists_nonneg in (hc.prod_rightl k' cnonneg).is_O lemma is_o.prod_rightl (h : is_o f g' l) : is_o f (Ξ» x, (g' x, k' x)) l := is_o.of_is_O_with $ Ξ» c cpos, (h.forall_is_O_with cpos).prod_rightl k' (le_of_lt cpos) lemma is_O_with.prod_rightr (h : is_O_with c f g' l) (hc : 0 ≀ c) : is_O_with c f (Ξ» x, (f' x, g' x)) l := (h.trans is_O_with_snd_prod hc).congr_const (mul_one c) lemma is_O.prod_rightr (h : is_O f g' l) : is_O f (Ξ»x, (f' x, g' x)) l := let ⟨c, cnonneg, hc⟩ := h.exists_nonneg in (hc.prod_rightr f' cnonneg).is_O lemma is_o.prod_rightr (h : is_o f g' l) : is_o f (Ξ»x, (f' x, g' x)) l := is_o.of_is_O_with $ Ξ» c cpos, (h.forall_is_O_with cpos).prod_rightr f' (le_of_lt cpos) end lemma is_O_with.prod_left_same (hf : is_O_with c f' k' l) (hg : is_O_with c g' k' l) : is_O_with c (Ξ» x, (f' x, g' x)) k' l := by rw is_O_with_iff at *; filter_upwards [hf, hg] Ξ» x, max_le lemma is_O_with.prod_left (hf : is_O_with c f' k' l) (hg : is_O_with c' g' k' l) : is_O_with (max c c') (Ξ» x, (f' x, g' x)) k' l := (hf.weaken $ le_max_left c c').prod_left_same (hg.weaken $ le_max_right c c') lemma is_O_with.prod_left_fst (h : is_O_with c (Ξ» x, (f' x, g' x)) k' l) : is_O_with c f' k' l := (is_O_with_fst_prod.trans h zero_le_one).congr_const $ one_mul c lemma is_O_with.prod_left_snd (h : is_O_with c (Ξ» x, (f' x, g' x)) k' l) : is_O_with c g' k' l := (is_O_with_snd_prod.trans h zero_le_one).congr_const $ one_mul c lemma is_O_with_prod_left : is_O_with c (Ξ» x, (f' x, g' x)) k' l ↔ is_O_with c f' k' l ∧ is_O_with c g' k' l := ⟨λ h, ⟨h.prod_left_fst, h.prod_left_snd⟩, Ξ» h, h.1.prod_left_same h.2⟩ lemma is_O.prod_left (hf : is_O f' k' l) (hg : is_O g' k' l) : is_O (Ξ» x, (f' x, g' x)) k' l := let ⟨c, hf⟩ := hf.is_O_with, ⟨c', hg⟩ := hg.is_O_with in (hf.prod_left hg).is_O lemma is_O.prod_left_fst (h : is_O (Ξ» x, (f' x, g' x)) k' l) : is_O f' k' l := is_O_fst_prod.trans h lemma is_O.prod_left_snd (h : is_O (Ξ» x, (f' x, g' x)) k' l) : is_O g' k' l := is_O_snd_prod.trans h @[simp] lemma is_O_prod_left : is_O (Ξ» x, (f' x, g' x)) k' l ↔ is_O f' k' l ∧ is_O g' k' l := ⟨λ h, ⟨h.prod_left_fst, h.prod_left_snd⟩, Ξ» h, h.1.prod_left h.2⟩ lemma is_o.prod_left (hf : is_o f' k' l) (hg : is_o g' k' l) : is_o (Ξ» x, (f' x, g' x)) k' l := is_o.of_is_O_with $ Ξ» c hc, (hf.forall_is_O_with hc).prod_left_same (hg.forall_is_O_with hc) lemma is_o.prod_left_fst (h : is_o (Ξ» x, (f' x, g' x)) k' l) : is_o f' k' l := is_O_fst_prod.trans_is_o h lemma is_o.prod_left_snd (h : is_o (Ξ» x, (f' x, g' x)) k' l) : is_o g' k' l := is_O_snd_prod.trans_is_o h @[simp] lemma is_o_prod_left : is_o (Ξ» x, (f' x, g' x)) k' l ↔ is_o f' k' l ∧ is_o g' k' l := ⟨λ h, ⟨h.prod_left_fst, h.prod_left_snd⟩, Ξ» h, h.1.prod_left h.2⟩ lemma is_O_with.eq_zero_imp (h : is_O_with c f' g' l) : βˆ€αΆ  x in l, g' x = 0 β†’ f' x = 0 := eventually.mono h.bound $ Ξ» x hx hg, norm_le_zero_iff.1 $ by simpa [hg] using hx lemma is_O.eq_zero_imp (h : is_O f' g' l) : βˆ€αΆ  x in l, g' x = 0 β†’ f' x = 0 := let ⟨C, hC⟩ := h.is_O_with in hC.eq_zero_imp /-! ### Addition and subtraction -/ section add_sub variables {c₁ cβ‚‚ : ℝ} {f₁ fβ‚‚ : Ξ± β†’ E'} theorem is_O_with.add (h₁ : is_O_with c₁ f₁ g l) (hβ‚‚ : is_O_with cβ‚‚ fβ‚‚ g l) : is_O_with (c₁ + cβ‚‚) (Ξ» x, f₁ x + fβ‚‚ x) g l := by rw is_O_with at *; filter_upwards [h₁, hβ‚‚] Ξ» x hx₁ hxβ‚‚, calc βˆ₯f₁ x + fβ‚‚ xβˆ₯ ≀ c₁ * βˆ₯g xβˆ₯ + cβ‚‚ * βˆ₯g xβˆ₯ : norm_add_le_of_le hx₁ hxβ‚‚ ... = (c₁ + cβ‚‚) * βˆ₯g xβˆ₯ : (add_mul _ _ _).symm theorem is_O.add (h₁ : is_O f₁ g l) (hβ‚‚ : is_O fβ‚‚ g l) : is_O (Ξ» x, f₁ x + fβ‚‚ x) g l := let ⟨c₁, hcβ‚βŸ© := h₁.is_O_with, ⟨cβ‚‚, hcβ‚‚βŸ© := hβ‚‚.is_O_with in (hc₁.add hcβ‚‚).is_O theorem is_o.add (h₁ : is_o f₁ g l) (hβ‚‚ : is_o fβ‚‚ g l) : is_o (Ξ» x, f₁ x + fβ‚‚ x) g l := is_o.of_is_O_with $ Ξ» c cpos, ((h₁.forall_is_O_with $ half_pos cpos).add (hβ‚‚.forall_is_O_with $ half_pos cpos)).congr_const (add_halves c) theorem is_o.add_add {g₁ gβ‚‚ : Ξ± β†’ F'} (h₁ : is_o f₁ g₁ l) (hβ‚‚ : is_o fβ‚‚ gβ‚‚ l) : is_o (Ξ» x, f₁ x + fβ‚‚ x) (Ξ» x, βˆ₯g₁ xβˆ₯ + βˆ₯gβ‚‚ xβˆ₯) l := by refine (h₁.trans_le $ Ξ» x, _).add (hβ‚‚.trans_le _); simp [real.norm_eq_abs, abs_of_nonneg, add_nonneg] theorem is_O.add_is_o (h₁ : is_O f₁ g l) (hβ‚‚ : is_o fβ‚‚ g l) : is_O (Ξ» x, f₁ x + fβ‚‚ x) g l := h₁.add hβ‚‚.is_O theorem is_o.add_is_O (h₁ : is_o f₁ g l) (hβ‚‚ : is_O fβ‚‚ g l) : is_O (Ξ» x, f₁ x + fβ‚‚ x) g l := h₁.is_O.add hβ‚‚ theorem is_O_with.add_is_o (h₁ : is_O_with c₁ f₁ g l) (hβ‚‚ : is_o fβ‚‚ g l) (hc : c₁ < cβ‚‚) : is_O_with cβ‚‚ (Ξ»x, f₁ x + fβ‚‚ x) g l := (h₁.add (hβ‚‚.forall_is_O_with (sub_pos.2 hc))).congr_const (add_sub_cancel'_right _ _) theorem is_o.add_is_O_with (h₁ : is_o f₁ g l) (hβ‚‚ : is_O_with c₁ fβ‚‚ g l) (hc : c₁ < cβ‚‚) : is_O_with cβ‚‚ (Ξ»x, f₁ x + fβ‚‚ x) g l := (hβ‚‚.add_is_o h₁ hc).congr_left $ Ξ» _, add_comm _ _ theorem is_O_with.sub (h₁ : is_O_with c₁ f₁ g l) (hβ‚‚ : is_O_with cβ‚‚ fβ‚‚ g l) : is_O_with (c₁ + cβ‚‚) (Ξ» x, f₁ x - fβ‚‚ x) g l := by simpa only [sub_eq_add_neg] using h₁.add hβ‚‚.neg_left theorem is_O_with.sub_is_o (h₁ : is_O_with c₁ f₁ g l) (hβ‚‚ : is_o fβ‚‚ g l) (hc : c₁ < cβ‚‚) : is_O_with cβ‚‚ (Ξ» x, f₁ x - fβ‚‚ x) g l := by simpa only [sub_eq_add_neg] using h₁.add_is_o hβ‚‚.neg_left hc theorem is_O.sub (h₁ : is_O f₁ g l) (hβ‚‚ : is_O fβ‚‚ g l) : is_O (Ξ» x, f₁ x - fβ‚‚ x) g l := by simpa only [sub_eq_add_neg] using h₁.add hβ‚‚.neg_left theorem is_o.sub (h₁ : is_o f₁ g l) (hβ‚‚ : is_o fβ‚‚ g l) : is_o (Ξ» x, f₁ x - fβ‚‚ x) g l := by simpa only [sub_eq_add_neg] using h₁.add hβ‚‚.neg_left end add_sub /-! ### Lemmas about `is_O (f₁ - fβ‚‚) g l` / `is_o (f₁ - fβ‚‚) g l` treated as a binary relation -/ section is_oO_as_rel variables {f₁ fβ‚‚ f₃ : Ξ± β†’ E'} theorem is_O_with.symm (h : is_O_with c (Ξ» x, f₁ x - fβ‚‚ x) g l) : is_O_with c (Ξ» x, fβ‚‚ x - f₁ x) g l := h.neg_left.congr_left $ Ξ» x, neg_sub _ _ theorem is_O_with_comm : is_O_with c (Ξ» x, f₁ x - fβ‚‚ x) g l ↔ is_O_with c (Ξ» x, fβ‚‚ x - f₁ x) g l := ⟨is_O_with.symm, is_O_with.symm⟩ theorem is_O.symm (h : is_O (Ξ» x, f₁ x - fβ‚‚ x) g l) : is_O (Ξ» x, fβ‚‚ x - f₁ x) g l := h.neg_left.congr_left $ Ξ» x, neg_sub _ _ theorem is_O_comm : is_O (Ξ» x, f₁ x - fβ‚‚ x) g l ↔ is_O (Ξ» x, fβ‚‚ x - f₁ x) g l := ⟨is_O.symm, is_O.symm⟩ theorem is_o.symm (h : is_o (Ξ» x, f₁ x - fβ‚‚ x) g l) : is_o (Ξ» x, fβ‚‚ x - f₁ x) g l := by simpa only [neg_sub] using h.neg_left theorem is_o_comm : is_o (Ξ» x, f₁ x - fβ‚‚ x) g l ↔ is_o (Ξ» x, fβ‚‚ x - f₁ x) g l := ⟨is_o.symm, is_o.symm⟩ theorem is_O_with.triangle (h₁ : is_O_with c (Ξ» x, f₁ x - fβ‚‚ x) g l) (hβ‚‚ : is_O_with c' (Ξ» x, fβ‚‚ x - f₃ x) g l) : is_O_with (c + c') (Ξ» x, f₁ x - f₃ x) g l := (h₁.add hβ‚‚).congr_left $ Ξ» x, sub_add_sub_cancel _ _ _ theorem is_O.triangle (h₁ : is_O (Ξ» x, f₁ x - fβ‚‚ x) g l) (hβ‚‚ : is_O (Ξ» x, fβ‚‚ x - f₃ x) g l) : is_O (Ξ» x, f₁ x - f₃ x) g l := (h₁.add hβ‚‚).congr_left $ Ξ» x, sub_add_sub_cancel _ _ _ theorem is_o.triangle (h₁ : is_o (Ξ» x, f₁ x - fβ‚‚ x) g l) (hβ‚‚ : is_o (Ξ» x, fβ‚‚ x - f₃ x) g l) : is_o (Ξ» x, f₁ x - f₃ x) g l := (h₁.add hβ‚‚).congr_left $ Ξ» x, sub_add_sub_cancel _ _ _ theorem is_O.congr_of_sub (h : is_O (Ξ» x, f₁ x - fβ‚‚ x) g l) : is_O f₁ g l ↔ is_O fβ‚‚ g l := ⟨λ h', (h'.sub h).congr_left (Ξ» x, sub_sub_cancel _ _), Ξ» h', (h.add h').congr_left (Ξ» x, sub_add_cancel _ _)⟩ theorem is_o.congr_of_sub (h : is_o (Ξ» x, f₁ x - fβ‚‚ x) g l) : is_o f₁ g l ↔ is_o fβ‚‚ g l := ⟨λ h', (h'.sub h).congr_left (Ξ» x, sub_sub_cancel _ _), Ξ» h', (h.add h').congr_left (Ξ» x, sub_add_cancel _ _)⟩ end is_oO_as_rel /-! ### Zero, one, and other constants -/ section zero_const variables (g g' l) theorem is_o_zero : is_o (Ξ» x, (0 : E')) g' l := is_o.of_bound $ Ξ» c hc, univ_mem' $ Ξ» x, by simpa using mul_nonneg (le_of_lt hc) (norm_nonneg $ g' x) theorem is_O_with_zero (hc : 0 ≀ c) : is_O_with c (Ξ» x, (0 : E')) g' l := is_O_with.of_bound $ univ_mem' $ Ξ» x, by simpa using mul_nonneg hc (norm_nonneg $ g' x) theorem is_O_with_zero' : is_O_with 0 (Ξ» x, (0 : E')) g l := is_O_with.of_bound $ univ_mem' $ Ξ» x, by simp theorem is_O_zero : is_O (Ξ» x, (0 : E')) g l := is_O_iff_is_O_with.2 ⟨0, is_O_with_zero' _ _⟩ theorem is_O_refl_left : is_O (Ξ» x, f' x - f' x) g' l := (is_O_zero g' l).congr_left $ Ξ» x, (sub_self _).symm theorem is_o_refl_left : is_o (Ξ» x, f' x - f' x) g' l := (is_o_zero g' l).congr_left $ Ξ» x, (sub_self _).symm variables {g g' l} @[simp] theorem is_O_with_zero_right_iff : is_O_with c f' (Ξ» x, (0 : F')) l ↔ βˆ€αΆ  x in l, f' x = 0 := by simp only [is_O_with, exists_prop, true_and, norm_zero, mul_zero, norm_le_zero_iff] @[simp] theorem is_O_zero_right_iff : is_O f' (Ξ» x, (0 : F')) l ↔ βˆ€αΆ  x in l, f' x = 0 := ⟨λ h, let ⟨c, hc⟩ := h.is_O_with in is_O_with_zero_right_iff.1 hc, Ξ» h, (is_O_with_zero_right_iff.2 h : is_O_with 1 _ _ _).is_O⟩ @[simp] theorem is_o_zero_right_iff : is_o f' (Ξ» x, (0 : F')) l ↔ βˆ€αΆ  x in l, f' x = 0 := ⟨λ h, is_O_zero_right_iff.1 h.is_O, Ξ» h, is_o.of_is_O_with $ Ξ» c hc, is_O_with_zero_right_iff.2 h⟩ theorem is_O_with_const_const (c : E) {c' : F'} (hc' : c' β‰  0) (l : filter Ξ±) : is_O_with (βˆ₯cβˆ₯ / βˆ₯c'βˆ₯) (Ξ» x : Ξ±, c) (Ξ» x, c') l := begin unfold is_O_with, apply univ_mem', intro x, rw [mem_set_of_eq, div_mul_cancel], rwa [ne.def, norm_eq_zero] end theorem is_O_const_const (c : E) {c' : F'} (hc' : c' β‰  0) (l : filter Ξ±) : is_O (Ξ» x : Ξ±, c) (Ξ» x, c') l := (is_O_with_const_const c hc' l).is_O end zero_const @[simp] lemma is_O_with_top : is_O_with c f g ⊀ ↔ βˆ€ x, βˆ₯f xβˆ₯ ≀ c * βˆ₯g xβˆ₯ := by rw is_O_with; refl @[simp] lemma is_O_top : is_O f g ⊀ ↔ βˆƒ C, βˆ€ x, βˆ₯f xβˆ₯ ≀ C * βˆ₯g xβˆ₯ := by rw is_O_iff; refl @[simp] lemma is_o_top : is_o f' g' ⊀ ↔ βˆ€ x, f' x = 0 := begin refine ⟨_, Ξ» h, (is_o_zero g' ⊀).congr (Ξ» x, (h x).symm) (Ξ» x, rfl)⟩, simp only [is_o_iff, eventually_top], refine Ξ» h x, norm_le_zero_iff.1 _, have : tendsto (Ξ» c : ℝ, c * βˆ₯g' xβˆ₯) (𝓝[Ioi 0] 0) (𝓝 0) := ((continuous_id.mul continuous_const).tendsto' _ _ (zero_mul _)).mono_left inf_le_left, exact le_of_tendsto_of_tendsto tendsto_const_nhds this (eventually_nhds_within_iff.2 $ eventually_of_forall $ Ξ» c hc, h hc x) end @[simp] lemma is_O_with_principal {s : set Ξ±} : is_O_with c f g (π“Ÿ s) ↔ βˆ€ x ∈ s, βˆ₯f xβˆ₯ ≀ c * βˆ₯g xβˆ₯ := by rw is_O_with; refl lemma is_O_principal {s : set Ξ±} : is_O f g (π“Ÿ s) ↔ βˆƒ c, βˆ€ x ∈ s, βˆ₯f xβˆ₯ ≀ c * βˆ₯g xβˆ₯ := by rw is_O_iff; refl theorem is_O_with_const_one (c : E) (l : filter Ξ±) : is_O_with βˆ₯cβˆ₯ (Ξ» x : Ξ±, c) (Ξ» x, (1 : π•œ)) l := begin refine (is_O_with_const_const c _ l).congr_const _, { rw [norm_one, div_one] }, { exact one_ne_zero } end theorem is_O_const_one (c : E) (l : filter Ξ±) : is_O (Ξ» x : Ξ±, c) (Ξ» x, (1 : π•œ)) l := (is_O_with_const_one c l).is_O section variable (π•œ) theorem is_o_const_iff_is_o_one {c : F'} (hc : c β‰  0) : is_o f (Ξ» x, c) l ↔ is_o f (Ξ» x, (1:π•œ)) l := ⟨λ h, h.trans_is_O $ is_O_const_one c l, Ξ» h, h.trans_is_O $ is_O_const_const _ hc _⟩ end theorem is_o_const_iff {c : F'} (hc : c β‰  0) : is_o f' (Ξ» x, c) l ↔ tendsto f' l (𝓝 0) := (is_o_const_iff_is_o_one ℝ hc).trans begin clear hc c, simp only [is_o, is_O_with, norm_one, mul_one, metric.nhds_basis_closed_ball.tendsto_right_iff, metric.mem_closed_ball, dist_zero_right] end theorem is_o_const_const_iff [ne_bot l] {d : E'} {c : F'} (hc : c β‰  0) : is_o (Ξ» x, d) (Ξ» x, c) l ↔ d = 0 := begin rw is_o_const_iff hc, refine ⟨λ h, tendsto_nhds_unique tendsto_const_nhds h, _⟩, rintros rfl, exact tendsto_const_nhds, end lemma is_o_id_const {c : F'} (hc : c β‰  0) : is_o (Ξ» (x : E'), x) (Ξ» x, c) (𝓝 0) := (is_o_const_iff hc).mpr (continuous_id.tendsto 0) theorem is_O_const_of_tendsto {y : E'} (h : tendsto f' l (𝓝 y)) {c : F'} (hc : c β‰  0) : is_O f' (Ξ» x, c) l := begin refine is_O.trans _ (is_O_const_const (βˆ₯yβˆ₯ + 1) hc l), refine is_O.of_bound 1 _, simp only [is_O_with, one_mul], have : tendsto (Ξ»x, βˆ₯f' xβˆ₯) l (𝓝 βˆ₯yβˆ₯), from (continuous_norm.tendsto _).comp h, have Iy : βˆ₯yβˆ₯ < βˆ₯βˆ₯yβˆ₯ + 1βˆ₯, from lt_of_lt_of_le (lt_add_one _) (le_abs_self _), exact this (ge_mem_nhds Iy) end section variable (π•œ) theorem is_o_one_iff : is_o f' (Ξ» x, (1 : π•œ)) l ↔ tendsto f' l (𝓝 0) := is_o_const_iff one_ne_zero theorem is_O_one_of_tendsto {y : E'} (h : tendsto f' l (𝓝 y)) : is_O f' (Ξ» x, (1:π•œ)) l := is_O_const_of_tendsto h one_ne_zero theorem is_O.trans_tendsto_nhds (hfg : is_O f g' l) {y : F'} (hg : tendsto g' l (𝓝 y)) : is_O f (Ξ» x, (1:π•œ)) l := hfg.trans $ is_O_one_of_tendsto π•œ hg end theorem is_O.trans_tendsto (hfg : is_O f' g' l) (hg : tendsto g' l (𝓝 0)) : tendsto f' l (𝓝 0) := (is_o_one_iff ℝ).1 $ hfg.trans_is_o $ (is_o_one_iff ℝ).2 hg theorem is_o.trans_tendsto (hfg : is_o f' g' l) (hg : tendsto g' l (𝓝 0)) : tendsto f' l (𝓝 0) := hfg.is_O.trans_tendsto hg /-! ### Multiplication by a constant -/ theorem is_O_with_const_mul_self (c : R) (f : Ξ± β†’ R) (l : filter Ξ±) : is_O_with βˆ₯cβˆ₯ (Ξ» x, c * f x) f l := is_O_with_of_le' _ $ Ξ» x, norm_mul_le _ _ theorem is_O_const_mul_self (c : R) (f : Ξ± β†’ R) (l : filter Ξ±) : is_O (Ξ» x, c * f x) f l := (is_O_with_const_mul_self c f l).is_O theorem is_O_with.const_mul_left {f : Ξ± β†’ R} (h : is_O_with c f g l) (c' : R) : is_O_with (βˆ₯c'βˆ₯ * c) (Ξ» x, c' * f x) g l := (is_O_with_const_mul_self c' f l).trans h (norm_nonneg c') theorem is_O.const_mul_left {f : Ξ± β†’ R} (h : is_O f g l) (c' : R) : is_O (Ξ» x, c' * f x) g l := let ⟨c, hc⟩ := h.is_O_with in (hc.const_mul_left c').is_O theorem is_O_with_self_const_mul' (u : units R) (f : Ξ± β†’ R) (l : filter Ξ±) : is_O_with βˆ₯(↑u⁻¹:R)βˆ₯ f (Ξ» x, ↑u * f x) l := (is_O_with_const_mul_self ↑u⁻¹ _ l).congr_left $ Ξ» x, u.inv_mul_cancel_left (f x) theorem is_O_with_self_const_mul (c : π•œ) (hc : c β‰  0) (f : Ξ± β†’ π•œ) (l : filter Ξ±) : is_O_with βˆ₯cβˆ₯⁻¹ f (Ξ» x, c * f x) l := (is_O_with_self_const_mul' (units.mk0 c hc) f l).congr_const $ normed_field.norm_inv c theorem is_O_self_const_mul' {c : R} (hc : is_unit c) (f : Ξ± β†’ R) (l : filter Ξ±) : is_O f (Ξ» x, c * f x) l := let ⟨u, hu⟩ := hc in hu β–Έ (is_O_with_self_const_mul' u f l).is_O theorem is_O_self_const_mul (c : π•œ) (hc : c β‰  0) (f : Ξ± β†’ π•œ) (l : filter Ξ±) : is_O f (Ξ» x, c * f x) l := is_O_self_const_mul' (is_unit.mk0 c hc) f l theorem is_O_const_mul_left_iff' {f : Ξ± β†’ R} {c : R} (hc : is_unit c) : is_O (Ξ» x, c * f x) g l ↔ is_O f g l := ⟨(is_O_self_const_mul' hc f l).trans, Ξ» h, h.const_mul_left c⟩ theorem is_O_const_mul_left_iff {f : Ξ± β†’ π•œ} {c : π•œ} (hc : c β‰  0) : is_O (Ξ» x, c * f x) g l ↔ is_O f g l := is_O_const_mul_left_iff' $ is_unit.mk0 c hc theorem is_o.const_mul_left {f : Ξ± β†’ R} (h : is_o f g l) (c : R) : is_o (Ξ» x, c * f x) g l := (is_O_const_mul_self c f l).trans_is_o h theorem is_o_const_mul_left_iff' {f : Ξ± β†’ R} {c : R} (hc : is_unit c) : is_o (Ξ» x, c * f x) g l ↔ is_o f g l := ⟨(is_O_self_const_mul' hc f l).trans_is_o, Ξ» h, h.const_mul_left c⟩ theorem is_o_const_mul_left_iff {f : Ξ± β†’ π•œ} {c : π•œ} (hc : c β‰  0) : is_o (Ξ» x, c * f x) g l ↔ is_o f g l := is_o_const_mul_left_iff' $ is_unit.mk0 c hc theorem is_O_with.of_const_mul_right {g : Ξ± β†’ R} {c : R} (hc' : 0 ≀ c') (h : is_O_with c' f (Ξ» x, c * g x) l) : is_O_with (c' * βˆ₯cβˆ₯) f g l := h.trans (is_O_with_const_mul_self c g l) hc' theorem is_O.of_const_mul_right {g : Ξ± β†’ R} {c : R} (h : is_O f (Ξ» x, c * g x) l) : is_O f g l := let ⟨c, cnonneg, hc⟩ := h.exists_nonneg in (hc.of_const_mul_right cnonneg).is_O theorem is_O_with.const_mul_right' {g : Ξ± β†’ R} {u : units R} {c' : ℝ} (hc' : 0 ≀ c') (h : is_O_with c' f g l) : is_O_with (c' * βˆ₯(↑u⁻¹:R)βˆ₯) f (Ξ» x, ↑u * g x) l := h.trans (is_O_with_self_const_mul' _ _ _) hc' theorem is_O_with.const_mul_right {g : Ξ± β†’ π•œ} {c : π•œ} (hc : c β‰  0) {c' : ℝ} (hc' : 0 ≀ c') (h : is_O_with c' f g l) : is_O_with (c' * βˆ₯cβˆ₯⁻¹) f (Ξ» x, c * g x) l := h.trans (is_O_with_self_const_mul c hc g l) hc' theorem is_O.const_mul_right' {g : Ξ± β†’ R} {c : R} (hc : is_unit c) (h : is_O f g l) : is_O f (Ξ» x, c * g x) l := h.trans (is_O_self_const_mul' hc g l) theorem is_O.const_mul_right {g : Ξ± β†’ π•œ} {c : π•œ} (hc : c β‰  0) (h : is_O f g l) : is_O f (Ξ» x, c * g x) l := h.const_mul_right' $ is_unit.mk0 c hc theorem is_O_const_mul_right_iff' {g : Ξ± β†’ R} {c : R} (hc : is_unit c) : is_O f (Ξ» x, c * g x) l ↔ is_O f g l := ⟨λ h, h.of_const_mul_right, Ξ» h, h.const_mul_right' hc⟩ theorem is_O_const_mul_right_iff {g : Ξ± β†’ π•œ} {c : π•œ} (hc : c β‰  0) : is_O f (Ξ» x, c * g x) l ↔ is_O f g l := is_O_const_mul_right_iff' $ is_unit.mk0 c hc theorem is_o.of_const_mul_right {g : Ξ± β†’ R} {c : R} (h : is_o f (Ξ» x, c * g x) l) : is_o f g l := h.trans_is_O (is_O_const_mul_self c g l) theorem is_o.const_mul_right' {g : Ξ± β†’ R} {c : R} (hc : is_unit c) (h : is_o f g l) : is_o f (Ξ» x, c * g x) l := h.trans_is_O (is_O_self_const_mul' hc g l) theorem is_o.const_mul_right {g : Ξ± β†’ π•œ} {c : π•œ} (hc : c β‰  0) (h : is_o f g l) : is_o f (Ξ» x, c * g x) l := h.const_mul_right' $ is_unit.mk0 c hc theorem is_o_const_mul_right_iff' {g : Ξ± β†’ R} {c : R} (hc : is_unit c) : is_o f (Ξ» x, c * g x) l ↔ is_o f g l := ⟨λ h, h.of_const_mul_right, Ξ» h, h.const_mul_right' hc⟩ theorem is_o_const_mul_right_iff {g : Ξ± β†’ π•œ} {c : π•œ} (hc : c β‰  0) : is_o f (Ξ» x, c * g x) l ↔ is_o f g l := is_o_const_mul_right_iff' $ is_unit.mk0 c hc /-! ### Multiplication -/ theorem is_O_with.mul {f₁ fβ‚‚ : Ξ± β†’ R} {g₁ gβ‚‚ : Ξ± β†’ π•œ} {c₁ cβ‚‚ : ℝ} (h₁ : is_O_with c₁ f₁ g₁ l) (hβ‚‚ : is_O_with cβ‚‚ fβ‚‚ gβ‚‚ l) : is_O_with (c₁ * cβ‚‚) (Ξ» x, f₁ x * fβ‚‚ x) (Ξ» x, g₁ x * gβ‚‚ x) l := begin unfold is_O_with at *, filter_upwards [h₁, hβ‚‚], intros x hx₁ hxβ‚‚, apply le_trans (norm_mul_le _ _), convert mul_le_mul hx₁ hxβ‚‚ (norm_nonneg _) (le_trans (norm_nonneg _) hx₁) using 1, rw normed_field.norm_mul, ac_refl end theorem is_O.mul {f₁ fβ‚‚ : Ξ± β†’ R} {g₁ gβ‚‚ : Ξ± β†’ π•œ} (h₁ : is_O f₁ g₁ l) (hβ‚‚ : is_O fβ‚‚ gβ‚‚ l) : is_O (Ξ» x, f₁ x * fβ‚‚ x) (Ξ» x, g₁ x * gβ‚‚ x) l := let ⟨c, hc⟩ := h₁.is_O_with, ⟨c', hc'⟩ := hβ‚‚.is_O_with in (hc.mul hc').is_O theorem is_O.mul_is_o {f₁ fβ‚‚ : Ξ± β†’ R} {g₁ gβ‚‚ : Ξ± β†’ π•œ} (h₁ : is_O f₁ g₁ l) (hβ‚‚ : is_o fβ‚‚ gβ‚‚ l) : is_o (Ξ» x, f₁ x * fβ‚‚ x) (Ξ» x, g₁ x * gβ‚‚ x) l := begin unfold is_o at *, intros c cpos, rcases h₁.exists_pos with ⟨c', c'pos, hc'⟩, exact (hc'.mul (hβ‚‚ (div_pos cpos c'pos))).congr_const (mul_div_cancel' _ (ne_of_gt c'pos)) end theorem is_o.mul_is_O {f₁ fβ‚‚ : Ξ± β†’ R} {g₁ gβ‚‚ : Ξ± β†’ π•œ} (h₁ : is_o f₁ g₁ l) (hβ‚‚ : is_O fβ‚‚ gβ‚‚ l) : is_o (Ξ» x, f₁ x * fβ‚‚ x) (Ξ» x, g₁ x * gβ‚‚ x) l := begin unfold is_o at *, intros c cpos, rcases hβ‚‚.exists_pos with ⟨c', c'pos, hc'⟩, exact ((h₁ (div_pos cpos c'pos)).mul hc').congr_const (div_mul_cancel _ (ne_of_gt c'pos)) end theorem is_o.mul {f₁ fβ‚‚ : Ξ± β†’ R} {g₁ gβ‚‚ : Ξ± β†’ π•œ} (h₁ : is_o f₁ g₁ l) (hβ‚‚ : is_o fβ‚‚ gβ‚‚ l) : is_o (Ξ» x, f₁ x * fβ‚‚ x) (Ξ» x, g₁ x * gβ‚‚ x) l := h₁.mul_is_O hβ‚‚.is_O theorem is_O_with.pow' {f : Ξ± β†’ R} {g : Ξ± β†’ π•œ} (h : is_O_with c f g l) : βˆ€ n : β„•, is_O_with (nat.cases_on n βˆ₯(1 : R)βˆ₯ (Ξ» n, c ^ (n + 1))) (Ξ» x, f x ^ n) (Ξ» x, g x ^ n) l | 0 := by simpa using is_O_with_const_const (1 : R) (@one_ne_zero π•œ _ _) l | 1 := by simpa | (n + 2) := by simpa [pow_succ] using h.mul (is_O_with.pow' (n + 1)) theorem is_O_with.pow [norm_one_class R] {f : Ξ± β†’ R} {g : Ξ± β†’ π•œ} (h : is_O_with c f g l) : βˆ€ n : β„•, is_O_with (c ^ n) (Ξ» x, f x ^ n) (Ξ» x, g x ^ n) l | 0 := by simpa using h.pow' 0 | (n + 1) := h.pow' (n + 1) theorem is_O.pow {f : Ξ± β†’ R} {g : Ξ± β†’ π•œ} (h : is_O f g l) (n : β„•) : is_O (Ξ» x, f x ^ n) (Ξ» x, g x ^ n) l := let ⟨C, hC⟩ := h.is_O_with in is_O_iff_is_O_with.2 ⟨_, hC.pow' n⟩ theorem is_o.pow {f : Ξ± β†’ R} {g : Ξ± β†’ π•œ} (h : is_o f g l) {n : β„•} (hn : 0 < n) : is_o (Ξ» x, f x ^ n) (Ξ» x, g x ^ n) l := begin cases n, exact hn.false.elim, clear hn, induction n with n ihn, { simpa only [pow_one] }, convert h.mul ihn; simp [pow_succ] end /-! ### Scalar multiplication -/ section smul_const variables [normed_space π•œ E'] theorem is_O_with.const_smul_left (h : is_O_with c f' g l) (c' : π•œ) : is_O_with (βˆ₯c'βˆ₯ * c) (Ξ» x, c' β€’ f' x) g l := by refine ((h.norm_left.const_mul_left (βˆ₯c'βˆ₯)).congr _ _ (Ξ» _, rfl)).of_norm_left; intros; simp only [norm_norm, norm_smul] theorem is_O_const_smul_left_iff {c : π•œ} (hc : c β‰  0) : is_O (Ξ» x, c β€’ f' x) g l ↔ is_O f' g l := begin have cne0 : βˆ₯cβˆ₯ β‰  0, from mt norm_eq_zero.mp hc, rw [←is_O_norm_left], simp only [norm_smul], rw [is_O_const_mul_left_iff cne0, is_O_norm_left], end theorem is_o_const_smul_left (h : is_o f' g l) (c : π•œ) : is_o (Ξ» x, c β€’ f' x) g l := begin refine ((h.norm_left.const_mul_left (βˆ₯cβˆ₯)).congr_left _).of_norm_left, exact Ξ» x, (norm_smul _ _).symm end theorem is_o_const_smul_left_iff {c : π•œ} (hc : c β‰  0) : is_o (Ξ» x, c β€’ f' x) g l ↔ is_o f' g l := begin have cne0 : βˆ₯cβˆ₯ β‰  0, from mt norm_eq_zero.mp hc, rw [←is_o_norm_left], simp only [norm_smul], rw [is_o_const_mul_left_iff cne0, is_o_norm_left] end theorem is_O_const_smul_right {c : π•œ} (hc : c β‰  0) : is_O f (Ξ» x, c β€’ f' x) l ↔ is_O f f' l := begin have cne0 : βˆ₯cβˆ₯ β‰  0, from mt norm_eq_zero.mp hc, rw [←is_O_norm_right], simp only [norm_smul], rw [is_O_const_mul_right_iff cne0, is_O_norm_right] end theorem is_o_const_smul_right {c : π•œ} (hc : c β‰  0) : is_o f (Ξ» x, c β€’ f' x) l ↔ is_o f f' l := begin have cne0 : βˆ₯cβˆ₯ β‰  0, from mt norm_eq_zero.mp hc, rw [←is_o_norm_right], simp only [norm_smul], rw [is_o_const_mul_right_iff cne0, is_o_norm_right] end end smul_const section smul variables [normed_space π•œ E'] [normed_space π•œ F'] theorem is_O_with.smul {k₁ kβ‚‚ : Ξ± β†’ π•œ} (h₁ : is_O_with c k₁ kβ‚‚ l) (hβ‚‚ : is_O_with c' f' g' l) : is_O_with (c * c') (Ξ» x, k₁ x β€’ f' x) (Ξ» x, kβ‚‚ x β€’ g' x) l := by refine ((h₁.norm_norm.mul hβ‚‚.norm_norm).congr rfl _ _).of_norm_norm; by intros; simp only [norm_smul] theorem is_O.smul {k₁ kβ‚‚ : Ξ± β†’ π•œ} (h₁ : is_O k₁ kβ‚‚ l) (hβ‚‚ : is_O f' g' l) : is_O (Ξ» x, k₁ x β€’ f' x) (Ξ» x, kβ‚‚ x β€’ g' x) l := by refine ((h₁.norm_norm.mul hβ‚‚.norm_norm).congr _ _).of_norm_norm; by intros; simp only [norm_smul] theorem is_O.smul_is_o {k₁ kβ‚‚ : Ξ± β†’ π•œ} (h₁ : is_O k₁ kβ‚‚ l) (hβ‚‚ : is_o f' g' l) : is_o (Ξ» x, k₁ x β€’ f' x) (Ξ» x, kβ‚‚ x β€’ g' x) l := by refine ((h₁.norm_norm.mul_is_o hβ‚‚.norm_norm).congr _ _).of_norm_norm; by intros; simp only [norm_smul] theorem is_o.smul_is_O {k₁ kβ‚‚ : Ξ± β†’ π•œ} (h₁ : is_o k₁ kβ‚‚ l) (hβ‚‚ : is_O f' g' l) : is_o (Ξ» x, k₁ x β€’ f' x) (Ξ» x, kβ‚‚ x β€’ g' x) l := by refine ((h₁.norm_norm.mul_is_O hβ‚‚.norm_norm).congr _ _).of_norm_norm; by intros; simp only [norm_smul] theorem is_o.smul {k₁ kβ‚‚ : Ξ± β†’ π•œ} (h₁ : is_o k₁ kβ‚‚ l) (hβ‚‚ : is_o f' g' l) : is_o (Ξ» x, k₁ x β€’ f' x) (Ξ» x, kβ‚‚ x β€’ g' x) l := by refine ((h₁.norm_norm.mul hβ‚‚.norm_norm).congr _ _).of_norm_norm; by intros; simp only [norm_smul] end smul /-! ### Sum -/ section sum variables {ΞΉ : Type*} {A : ΞΉ β†’ Ξ± β†’ E'} {C : ΞΉ β†’ ℝ} {s : finset ΞΉ} theorem is_O_with.sum (h : βˆ€ i ∈ s, is_O_with (C i) (A i) g l) : is_O_with (βˆ‘ i in s, C i) (Ξ» x, βˆ‘ i in s, A i x) g l := begin induction s using finset.induction_on with i s is IH, { simp only [is_O_with_zero', finset.sum_empty, forall_true_iff] }, { simp only [is, finset.sum_insert, not_false_iff], exact (h _ (finset.mem_insert_self i s)).add (IH (Ξ» j hj, h _ (finset.mem_insert_of_mem hj))) } end theorem is_O.sum (h : βˆ€ i ∈ s, is_O (A i) g l) : is_O (Ξ» x, βˆ‘ i in s, A i x) g l := begin induction s using finset.induction_on with i s is IH, { simp only [is_O_zero, finset.sum_empty, forall_true_iff] }, { simp only [is, finset.sum_insert, not_false_iff], exact (h _ (finset.mem_insert_self i s)).add (IH (Ξ» j hj, h _ (finset.mem_insert_of_mem hj))) } end theorem is_o.sum (h : βˆ€ i ∈ s, is_o (A i) g' l) : is_o (Ξ» x, βˆ‘ i in s, A i x) g' l := begin induction s using finset.induction_on with i s is IH, { simp only [is_o_zero, finset.sum_empty, forall_true_iff] }, { simp only [is, finset.sum_insert, not_false_iff], exact (h _ (finset.mem_insert_self i s)).add (IH (Ξ» j hj, h _ (finset.mem_insert_of_mem hj))) } end end sum /-! ### Relation between `f = o(g)` and `f / g β†’ 0` -/ theorem is_o.tendsto_0 {f g : Ξ± β†’ π•œ} {l : filter Ξ±} (h : is_o f g l) : tendsto (Ξ» x, f x / (g x)) l (𝓝 0) := have eq₁ : is_o (Ξ» x, f x / g x) (Ξ» x, g x / g x) l, by simpa only [div_eq_mul_inv] using h.mul_is_O (is_O_refl _ _), have eqβ‚‚ : is_O (Ξ» x, g x / g x) (Ξ» x, (1 : π•œ)) l, from is_O_of_le _ (Ξ» x, by by_cases h : βˆ₯g xβˆ₯ = 0; simp [h, zero_le_one]), (is_o_one_iff π•œ).mp (eq₁.trans_is_O eqβ‚‚) theorem is_o_iff_tendsto' {f g : Ξ± β†’ π•œ} {l : filter Ξ±} (hgf : βˆ€αΆ  x in l, g x = 0 β†’ f x = 0) : is_o f g l ↔ tendsto (Ξ» x, f x / (g x)) l (𝓝 0) := iff.intro is_o.tendsto_0 $ Ξ» h, (((is_o_one_iff _).mpr h).mul_is_O (is_O_refl g l)).congr' (hgf.mono $ Ξ» x, div_mul_cancel_of_imp) (eventually_of_forall $ Ξ» x, one_mul _) theorem is_o_iff_tendsto {f g : Ξ± β†’ π•œ} {l : filter Ξ±} (hgf : βˆ€ x, g x = 0 β†’ f x = 0) : is_o f g l ↔ tendsto (Ξ» x, f x / (g x)) l (𝓝 0) := ⟨λ h, h.tendsto_0, (is_o_iff_tendsto' (eventually_of_forall hgf)).2⟩ alias is_o_iff_tendsto' ↔ _ asymptotics.is_o_of_tendsto' alias is_o_iff_tendsto ↔ _ asymptotics.is_o_of_tendsto /-! ### Eventually (u / v) * v = u If `u` and `v` are linked by an `is_O_with` relation, then we eventually have `(u / v) * v = u`, even if `v` vanishes. -/ section eventually_mul_div_cancel variables {u v : Ξ± β†’ π•œ} lemma is_O_with.eventually_mul_div_cancel (h : is_O_with c u v l) : (u / v) * v =αΆ [l] u := eventually.mono h.bound (Ξ» y hy, div_mul_cancel_of_imp $ Ξ» hv, by simpa [hv] using hy) /-- If `u = O(v)` along `l`, then `(u / v) * v = u` eventually at `l`. -/ lemma is_O.eventually_mul_div_cancel (h : is_O u v l) : (u / v) * v =αΆ [l] u := let ⟨c, hc⟩ := h.is_O_with in hc.eventually_mul_div_cancel /-- If `u = o(v)` along `l`, then `(u / v) * v = u` eventually at `l`. -/ lemma is_o.eventually_mul_div_cancel (h : is_o u v l) : (u / v) * v =αΆ [l] u := (h.forall_is_O_with zero_lt_one).eventually_mul_div_cancel end eventually_mul_div_cancel /-! ### Equivalent definitions of the form `βˆƒ Ο†, u =αΆ [l] Ο† * v` in a `normed_field`. -/ section exists_mul_eq variables {u v : Ξ± β†’ π•œ} /-- If `βˆ₯Ο†βˆ₯` is eventually bounded by `c`, and `u =αΆ [l] Ο† * v`, then we have `is_O_with c u v l`. This does not require any assumptions on `c`, which is why we keep this version along with `is_O_with_iff_exists_eq_mul`. -/ lemma is_O_with_of_eq_mul (Ο† : Ξ± β†’ π•œ) (hΟ† : βˆ€αΆ  x in l, βˆ₯Ο† xβˆ₯ ≀ c) (h : u =αΆ [l] Ο† * v) : is_O_with c u v l := begin unfold is_O_with, refine h.symm.rw (Ξ» x a, βˆ₯aβˆ₯ ≀ c * βˆ₯v xβˆ₯) (hΟ†.mono $ Ξ» x hx, _), simp only [normed_field.norm_mul, pi.mul_apply], exact mul_le_mul_of_nonneg_right hx (norm_nonneg _) end lemma is_O_with_iff_exists_eq_mul (hc : 0 ≀ c) : is_O_with c u v l ↔ βˆƒ (Ο† : Ξ± β†’ π•œ) (hΟ† : βˆ€αΆ  x in l, βˆ₯Ο† xβˆ₯ ≀ c), u =αΆ [l] Ο† * v := begin split, { intro h, use (Ξ» x, u x / v x), refine ⟨eventually.mono h.bound (Ξ» y hy, _), h.eventually_mul_div_cancel.symm⟩, simpa using div_le_of_nonneg_of_le_mul (norm_nonneg _) hc hy }, { rintros βŸ¨Ο†, hΟ†, h⟩, exact is_O_with_of_eq_mul Ο† hΟ† h } end lemma is_O_with.exists_eq_mul (h : is_O_with c u v l) (hc : 0 ≀ c) : βˆƒ (Ο† : Ξ± β†’ π•œ) (hΟ† : βˆ€αΆ  x in l, βˆ₯Ο† xβˆ₯ ≀ c), u =αΆ [l] Ο† * v := (is_O_with_iff_exists_eq_mul hc).mp h lemma is_O_iff_exists_eq_mul : is_O u v l ↔ βˆƒ (Ο† : Ξ± β†’ π•œ) (hΟ† : l.is_bounded_under (≀) (norm ∘ Ο†)), u =αΆ [l] Ο† * v := begin split, { rintros h, rcases h.exists_nonneg with ⟨c, hnnc, hc⟩, rcases hc.exists_eq_mul hnnc with βŸ¨Ο†, hΟ†, huvΟ†βŸ©, exact βŸ¨Ο†, ⟨c, hΟ†βŸ©, huvΟ†βŸ© }, { rintros βŸ¨Ο†, ⟨c, hΟ†βŸ©, huvΟ†βŸ©, exact is_O_iff_is_O_with.2 ⟨c, is_O_with_of_eq_mul Ο† hΟ† huvΟ†βŸ© } end alias is_O_iff_exists_eq_mul ↔ asymptotics.is_O.exists_eq_mul _ lemma is_o_iff_exists_eq_mul : is_o u v l ↔ βˆƒ (Ο† : Ξ± β†’ π•œ) (hΟ† : tendsto Ο† l (𝓝 0)), u =αΆ [l] Ο† * v := begin split, { exact Ξ» h, ⟨λ x, u x / v x, h.tendsto_0, h.eventually_mul_div_cancel.symm⟩ }, { unfold is_o, rintros βŸ¨Ο†, hΟ†, huvΟ†βŸ© c hpos, rw normed_group.tendsto_nhds_zero at hΟ†, exact is_O_with_of_eq_mul _ ((hΟ† c hpos).mono $ Ξ» x, le_of_lt) huvΟ† } end alias is_o_iff_exists_eq_mul ↔ asymptotics.is_o.exists_eq_mul _ end exists_mul_eq /-! ### Miscellanous lemmas -/ theorem div_is_bounded_under_of_is_O {Ξ± : Type*} {l : filter Ξ±} {f g : Ξ± β†’ π•œ} (h : is_O f g l) : is_bounded_under (≀) l (Ξ» x, βˆ₯f x / g xβˆ₯) := begin obtain ⟨c, hc⟩ := is_O_iff.mp h, refine ⟨max c 0, eventually_map.2 (filter.mem_of_superset hc (Ξ» x hx, _))⟩, simp only [mem_set_of_eq, normed_field.norm_div] at ⊒ hx, by_cases hgx : g x = 0, { rw [hgx, norm_zero, div_zero, le_max_iff], exact or.inr le_rfl }, { exact le_max_iff.2 (or.inl ((div_le_iff (norm_pos_iff.2 hgx)).2 hx)) } end theorem is_O_iff_div_is_bounded_under {Ξ± : Type*} {l : filter Ξ±} {f g : Ξ± β†’ π•œ} (hgf : βˆ€αΆ  x in l, g x = 0 β†’ f x = 0) : is_O f g l ↔ is_bounded_under (≀) l (Ξ» x, βˆ₯f x / g xβˆ₯) := begin refine ⟨div_is_bounded_under_of_is_O, Ξ» h, _⟩, obtain ⟨c, hc⟩ := h, rw filter.eventually_iff at hgf hc, simp only [mem_set_of_eq, mem_map, normed_field.norm_div] at hc, refine is_O_iff.2 ⟨c, filter.eventually_of_mem (inter_mem hgf hc) (Ξ» x hx, _)⟩, by_cases hgx : g x = 0, { simp [hx.1 hgx, hgx] }, { refine (div_le_iff (norm_pos_iff.2 hgx)).mp hx.2 }, end theorem is_O_of_div_tendsto_nhds {Ξ± : Type*} {l : filter Ξ±} {f g : Ξ± β†’ π•œ} (hgf : βˆ€αΆ  x in l, g x = 0 β†’ f x = 0) (c : π•œ) (H : filter.tendsto (f / g) l (𝓝 c)) : is_O f g l := (is_O_iff_div_is_bounded_under hgf).2 $ is_bounded_under_of_tendsto H lemma is_o.tendsto_zero_of_tendsto {Ξ± E π•œ : Type*} [normed_group E] [normed_field π•œ] {u : Ξ± β†’ E} {v : Ξ± β†’ π•œ} {l : filter Ξ±} {y : π•œ} (huv : is_o u v l) (hv : tendsto v l (𝓝 y)) : tendsto u l (𝓝 0) := begin suffices h : is_o u (Ξ» x, (1 : π•œ)) l, { rwa is_o_one_iff at h }, exact huv.trans_is_O (is_O_one_of_tendsto π•œ hv), end theorem is_o_pow_pow {m n : β„•} (h : m < n) : is_o (Ξ»(x : π•œ), x^n) (Ξ»x, x^m) (𝓝 0) := begin let p := n - m, have nmp : n = m + p := (nat.add_sub_cancel' (le_of_lt h)).symm, have : (Ξ»(x : π•œ), x^m) = (Ξ»x, x^m * 1), by simp only [mul_one], simp only [this, pow_add, nmp], refine is_O.mul_is_o (is_O_refl _ _) ((is_o_one_iff _).2 _), convert (continuous_pow p).tendsto (0 : π•œ), exact (zero_pow (nat.sub_pos_of_lt h)).symm end theorem is_o_norm_pow_norm_pow {m n : β„•} (h : m < n) : is_o (Ξ»(x : E'), βˆ₯xβˆ₯^n) (Ξ»x, βˆ₯xβˆ₯^m) (𝓝 (0 : E')) := (is_o_pow_pow h).comp_tendsto tendsto_norm_zero theorem is_o_pow_id {n : β„•} (h : 1 < n) : is_o (Ξ»(x : π•œ), x^n) (Ξ»x, x) (𝓝 0) := by { convert is_o_pow_pow h, simp only [pow_one] } theorem is_o_norm_pow_id {n : β„•} (h : 1 < n) : is_o (Ξ»(x : E'), βˆ₯xβˆ₯^n) (Ξ»x, x) (𝓝 0) := by simpa only [pow_one, is_o_norm_right] using @is_o_norm_pow_norm_pow E' _ _ _ h theorem is_O_with.right_le_sub_of_lt_1 {f₁ fβ‚‚ : Ξ± β†’ E'} (h : is_O_with c f₁ fβ‚‚ l) (hc : c < 1) : is_O_with (1 / (1 - c)) fβ‚‚ (Ξ»x, fβ‚‚ x - f₁ x) l := is_O_with.of_bound $ mem_of_superset h.bound $ Ξ» x hx, begin simp only [mem_set_of_eq] at hx ⊒, rw [mul_comm, one_div, ← div_eq_mul_inv, le_div_iff, mul_sub, mul_one, mul_comm], { exact le_trans (sub_le_sub_left hx _) (norm_sub_norm_le _ _) }, { exact sub_pos.2 hc } end theorem is_O_with.right_le_add_of_lt_1 {f₁ fβ‚‚ : Ξ± β†’ E'} (h : is_O_with c f₁ fβ‚‚ l) (hc : c < 1) : is_O_with (1 / (1 - c)) fβ‚‚ (Ξ»x, f₁ x + fβ‚‚ x) l := (h.neg_right.right_le_sub_of_lt_1 hc).neg_right.of_neg_left.congr rfl (Ξ» x, rfl) (Ξ» x, by rw [neg_sub, sub_neg_eq_add]) theorem is_o.right_is_O_sub {f₁ fβ‚‚ : Ξ± β†’ E'} (h : is_o f₁ fβ‚‚ l) : is_O fβ‚‚ (Ξ»x, fβ‚‚ x - f₁ x) l := ((h.def' one_half_pos).right_le_sub_of_lt_1 one_half_lt_one).is_O theorem is_o.right_is_O_add {f₁ fβ‚‚ : Ξ± β†’ E'} (h : is_o f₁ fβ‚‚ l) : is_O fβ‚‚ (Ξ»x, f₁ x + fβ‚‚ x) l := ((h.def' one_half_pos).right_le_add_of_lt_1 one_half_lt_one).is_O /-- If `f x = O(g x)` along `cofinite`, then there exists a positive constant `C` such that `βˆ₯f xβˆ₯ ≀ C * βˆ₯g xβˆ₯` whenever `g x β‰  0`. -/ theorem bound_of_is_O_cofinite (h : is_O f g' cofinite) : βˆƒ C > 0, βˆ€ ⦃x⦄, g' x β‰  0 β†’ βˆ₯f xβˆ₯ ≀ C * βˆ₯g' xβˆ₯ := begin rcases h.exists_pos with ⟨C, Cβ‚€, hC⟩, rw [is_O_with, eventually_cofinite] at hC, rcases (hC.to_finset.image (Ξ» x, βˆ₯f xβˆ₯ / βˆ₯g' xβˆ₯)).exists_le with ⟨C', hC'⟩, have : βˆ€ x, C * βˆ₯g' xβˆ₯ < βˆ₯f xβˆ₯ β†’ βˆ₯f xβˆ₯ / βˆ₯g' xβˆ₯ ≀ C', by simpa using hC', refine ⟨max C C', lt_max_iff.2 (or.inl Cβ‚€), Ξ» x hβ‚€, _⟩, rw [max_mul_of_nonneg _ _ (norm_nonneg _), le_max_iff, or_iff_not_imp_left, not_le], exact Ξ» hx, (div_le_iff (norm_pos_iff.2 hβ‚€)).1 (this _ hx) end theorem is_O_cofinite_iff (h : βˆ€ x, g' x = 0 β†’ f' x = 0) : is_O f' g' cofinite ↔ βˆƒ C, βˆ€ x, βˆ₯f' xβˆ₯ ≀ C * βˆ₯g' xβˆ₯ := ⟨λ h', let ⟨C, Cβ‚€, hC⟩ := bound_of_is_O_cofinite h' in ⟨C, Ξ» x, if hx : g' x = 0 then by simp [h _ hx, hx] else hC hx⟩, Ξ» h, (is_O_top.2 h).mono le_top⟩ theorem bound_of_is_O_nat_at_top {f : β„• β†’ E} {g' : β„• β†’ E'} (h : is_O f g' at_top) : βˆƒ C > 0, βˆ€ ⦃x⦄, g' x β‰  0 β†’ βˆ₯f xβˆ₯ ≀ C * βˆ₯g' xβˆ₯ := bound_of_is_O_cofinite $ by rwa nat.cofinite_eq_at_top theorem is_O_nat_at_top_iff {f : β„• β†’ E'} {g : β„• β†’ F'} (h : βˆ€ x, g x = 0 β†’ f x = 0) : is_O f g at_top ↔ βˆƒ C, βˆ€ x, βˆ₯f xβˆ₯ ≀ C * βˆ₯g xβˆ₯ := by rw [← nat.cofinite_eq_at_top, is_O_cofinite_iff h] theorem is_O_one_nat_at_top_iff {f : β„• β†’ E'} : is_O f (Ξ» n, 1 : β„• β†’ ℝ) at_top ↔ βˆƒ C, βˆ€ n, βˆ₯f nβˆ₯ ≀ C := iff.trans (is_O_nat_at_top_iff (Ξ» n h, (one_ne_zero h).elim)) $ by simp only [norm_one, mul_one] theorem is_O_with_pi {ΞΉ : Type*} [fintype ΞΉ] {E' : ΞΉ β†’ Type*} [Ξ  i, normed_group (E' i)] {f : Ξ± β†’ Ξ  i, E' i} {C : ℝ} (hC : 0 ≀ C) : is_O_with C f g' l ↔ βˆ€ i, is_O_with C (Ξ» x, f x i) g' l := have βˆ€ x, 0 ≀ C * βˆ₯g' xβˆ₯, from Ξ» x, mul_nonneg hC (norm_nonneg _), by simp only [is_O_with_iff, pi_norm_le_iff (this _), eventually_all] @[simp] theorem is_O_pi {ΞΉ : Type*} [fintype ΞΉ] {E' : ΞΉ β†’ Type*} [Ξ  i, normed_group (E' i)] {f : Ξ± β†’ Ξ  i, E' i} : is_O f g' l ↔ βˆ€ i, is_O (Ξ» x, f x i) g' l := begin simp only [is_O_iff_eventually_is_O_with, ← eventually_all], exact eventually_congr (eventually_at_top.2 ⟨0, Ξ» c, is_O_with_pi⟩) end @[simp] theorem is_o_pi {ΞΉ : Type*} [fintype ΞΉ] {E' : ΞΉ β†’ Type*} [Ξ  i, normed_group (E' i)] {f : Ξ± β†’ Ξ  i, E' i} : is_o f g' l ↔ βˆ€ i, is_o (Ξ» x, f x i) g' l := begin simp only [is_o, is_O_with_pi, le_of_lt] { contextual := tt }, exact ⟨λ h i c hc, h hc i, Ξ» h c hc i, h i hc⟩ end end asymptotics open asymptotics lemma summable_of_is_O {ΞΉ E} [normed_group E] [complete_space E] {f : ΞΉ β†’ E} {g : ΞΉ β†’ ℝ} (hg : summable g) (h : is_O f g cofinite) : summable f := let ⟨C, hC⟩ := h.is_O_with in summable_of_norm_bounded_eventually (Ξ» x, C * βˆ₯g xβˆ₯) (hg.abs.mul_left _) hC.bound lemma summable_of_is_O_nat {E} [normed_group E] [complete_space E] {f : β„• β†’ E} {g : β„• β†’ ℝ} (hg : summable g) (h : is_O f g at_top) : summable f := summable_of_is_O hg $ nat.cofinite_eq_at_top.symm β–Έ h namespace local_homeomorph variables {Ξ± : Type*} {Ξ² : Type*} [topological_space Ξ±] [topological_space Ξ²] variables {E : Type*} [has_norm E] {F : Type*} [has_norm F] /-- Transfer `is_O_with` over a `local_homeomorph`. -/ lemma is_O_with_congr (e : local_homeomorph Ξ± Ξ²) {b : Ξ²} (hb : b ∈ e.target) {f : Ξ² β†’ E} {g : Ξ² β†’ F} {C : ℝ} : is_O_with C f g (𝓝 b) ↔ is_O_with C (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := ⟨λ h, h.comp_tendsto $ by { convert e.continuous_at (e.map_target hb), exact (e.right_inv hb).symm }, Ξ» h, (h.comp_tendsto (e.continuous_at_symm hb)).congr' rfl ((e.eventually_right_inverse hb).mono $ Ξ» x hx, congr_arg f hx) ((e.eventually_right_inverse hb).mono $ Ξ» x hx, congr_arg g hx)⟩ /-- Transfer `is_O` over a `local_homeomorph`. -/ lemma is_O_congr (e : local_homeomorph Ξ± Ξ²) {b : Ξ²} (hb : b ∈ e.target) {f : Ξ² β†’ E} {g : Ξ² β†’ F} : is_O f g (𝓝 b) ↔ is_O (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := by { unfold is_O, exact exists_congr (Ξ» C, e.is_O_with_congr hb) } /-- Transfer `is_o` over a `local_homeomorph`. -/ lemma is_o_congr (e : local_homeomorph Ξ± Ξ²) {b : Ξ²} (hb : b ∈ e.target) {f : Ξ² β†’ E} {g : Ξ² β†’ F} : is_o f g (𝓝 b) ↔ is_o (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := by { unfold is_o, exact (forall_congr $ Ξ» c, forall_congr $ Ξ» hc, e.is_O_with_congr hb) } end local_homeomorph namespace homeomorph variables {Ξ± : Type*} {Ξ² : Type*} [topological_space Ξ±] [topological_space Ξ²] variables {E : Type*} [has_norm E] {F : Type*} [has_norm F] open asymptotics /-- Transfer `is_O_with` over a `homeomorph`. -/ lemma is_O_with_congr (e : Ξ± β‰ƒβ‚œ Ξ²) {b : Ξ²} {f : Ξ² β†’ E} {g : Ξ² β†’ F} {C : ℝ} : is_O_with C f g (𝓝 b) ↔ is_O_with C (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := e.to_local_homeomorph.is_O_with_congr trivial /-- Transfer `is_O` over a `homeomorph`. -/ lemma is_O_congr (e : Ξ± β‰ƒβ‚œ Ξ²) {b : Ξ²} {f : Ξ² β†’ E} {g : Ξ² β†’ F} : is_O f g (𝓝 b) ↔ is_O (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := by { unfold is_O, exact exists_congr (Ξ» C, e.is_O_with_congr) } /-- Transfer `is_o` over a `homeomorph`. -/ lemma is_o_congr (e : Ξ± β‰ƒβ‚œ Ξ²) {b : Ξ²} {f : Ξ² β†’ E} {g : Ξ² β†’ F} : is_o f g (𝓝 b) ↔ is_o (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := by { unfold is_o, exact forall_congr (Ξ» c, forall_congr (Ξ» hc, e.is_O_with_congr)) } end homeomorph
28704b2bb4d652e4bf9b491eb018cbc9f6b30b8d
d29d82a0af640c937e499f6be79fc552eae0aa13
/src/data/int/basic.lean
09f5ad8f75c838bed2e6034f1a4ddf65ba6cb5c1
[ "Apache-2.0" ]
permissive
AbdulMajeedkhurasani/mathlib
835f8a5c5cf3075b250b3737172043ab4fa1edf6
79bc7323b164aebd000524ebafd198eb0e17f956
refs/heads/master
1,688,003,895,660
1,627,788,521,000
1,627,788,521,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
57,658
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The integers, with addition, multiplication, and subtraction. -/ import data.nat.pow import algebra.order_functions open nat namespace int instance : inhabited β„€ := ⟨int.zero⟩ instance : nontrivial β„€ := ⟨⟨0, 1, int.zero_ne_one⟩⟩ instance : comm_ring int := { add := int.add, add_assoc := int.add_assoc, zero := int.zero, zero_add := int.zero_add, add_zero := int.add_zero, neg := int.neg, add_left_neg := int.add_left_neg, add_comm := int.add_comm, mul := int.mul, mul_assoc := int.mul_assoc, one := int.one, one_mul := int.one_mul, mul_one := int.mul_one, sub := int.sub, left_distrib := int.distrib_left, right_distrib := int.distrib_right, mul_comm := int.mul_comm, gsmul := (*), gsmul_zero' := int.zero_mul, gsmul_succ' := Ξ» n x, by rw [succ_eq_one_add, of_nat_add, int.distrib_right, of_nat_one, int.one_mul], gsmul_neg' := Ξ» n x, neg_mul_eq_neg_mul_symm (n.succ : β„€) x } /-! ### Extra instances to short-circuit type class resolution -/ -- instance : has_sub int := by apply_instance -- This is in core instance : add_comm_monoid int := by apply_instance instance : add_monoid int := by apply_instance instance : monoid int := by apply_instance instance : comm_monoid int := by apply_instance instance : comm_semigroup int := by apply_instance instance : semigroup int := by apply_instance instance : add_comm_semigroup int := by apply_instance instance : add_semigroup int := by apply_instance instance : comm_semiring int := by apply_instance instance : semiring int := by apply_instance instance : ring int := by apply_instance instance : distrib int := by apply_instance instance : linear_ordered_comm_ring int := { add_le_add_left := @int.add_le_add_left, mul_pos := @int.mul_pos, zero_le_one := le_of_lt int.zero_lt_one, .. int.comm_ring, .. int.linear_order, .. int.nontrivial } instance : linear_ordered_add_comm_group int := by apply_instance @[simp] lemma add_neg_one (i : β„€) : i + -1 = i - 1 := rfl theorem abs_eq_nat_abs : βˆ€ a : β„€, abs a = nat_abs a | (n : β„•) := abs_of_nonneg $ coe_zero_le _ | -[1+ n] := abs_of_nonpos $ le_of_lt $ neg_succ_lt_zero _ theorem nat_abs_abs (a : β„€) : nat_abs (abs a) = nat_abs a := by rw [abs_eq_nat_abs]; refl theorem sign_mul_abs (a : β„€) : sign a * abs a = a := by rw [abs_eq_nat_abs, sign_mul_nat_abs] @[simp] lemma default_eq_zero : default β„€ = 0 := rfl meta instance : has_to_format β„€ := ⟨λ z, to_string z⟩ meta instance : has_reflect β„€ := by tactic.mk_has_reflect_instance attribute [simp] int.coe_nat_add int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_succ attribute [simp] int.of_nat_eq_coe int.bodd @[simp] theorem add_def {a b : β„€} : int.add a b = a + b := rfl @[simp] theorem mul_def {a b : β„€} : int.mul a b = a * b := rfl @[simp] lemma neg_succ_not_nonneg (n : β„•) : 0 ≀ -[1+ n] ↔ false := by { simp only [not_le, iff_false], exact int.neg_succ_lt_zero n, } @[simp] lemma neg_succ_not_pos (n : β„•) : 0 < -[1+ n] ↔ false := by simp only [not_lt, iff_false] @[simp] lemma neg_succ_sub_one (n : β„•) : -[1+ n] - 1 = -[1+ (n+1)] := rfl @[simp] theorem coe_nat_mul_neg_succ (m n : β„•) : (m : β„€) * -[1+ n] = -(m * succ n) := rfl @[simp] theorem neg_succ_mul_coe_nat (m n : β„•) : -[1+ m] * n = -(succ m * n) := rfl @[simp] theorem neg_succ_mul_neg_succ (m n : β„•) : -[1+ m] * -[1+ n] = succ m * succ n := rfl @[simp, norm_cast] theorem coe_nat_le {m n : β„•} : (↑m : β„€) ≀ ↑n ↔ m ≀ n := coe_nat_le_coe_nat_iff m n @[simp, norm_cast] theorem coe_nat_lt {m n : β„•} : (↑m : β„€) < ↑n ↔ m < n := coe_nat_lt_coe_nat_iff m n @[simp, norm_cast] theorem coe_nat_inj' {m n : β„•} : (↑m : β„€) = ↑n ↔ m = n := int.coe_nat_eq_coe_nat_iff m n @[simp] theorem coe_nat_pos {n : β„•} : (0 : β„€) < n ↔ 0 < n := by rw [← int.coe_nat_zero, coe_nat_lt] @[simp] theorem coe_nat_eq_zero {n : β„•} : (n : β„€) = 0 ↔ n = 0 := by rw [← int.coe_nat_zero, coe_nat_inj'] theorem coe_nat_ne_zero {n : β„•} : (n : β„€) β‰  0 ↔ n β‰  0 := not_congr coe_nat_eq_zero @[simp] lemma coe_nat_nonneg (n : β„•) : 0 ≀ (n : β„€) := coe_nat_le.2 (nat.zero_le _) lemma coe_nat_ne_zero_iff_pos {n : β„•} : (n : β„€) β‰  0 ↔ 0 < n := ⟨λ h, nat.pos_of_ne_zero (coe_nat_ne_zero.1 h), Ξ» h, (ne_of_lt (coe_nat_lt.2 h)).symm⟩ lemma coe_nat_succ_pos (n : β„•) : 0 < (n.succ : β„€) := int.coe_nat_pos.2 (succ_pos n) @[simp, norm_cast] theorem coe_nat_abs (n : β„•) : abs (n : β„€) = n := abs_of_nonneg (coe_nat_nonneg n) /-! ### succ and pred -/ /-- Immediate successor of an integer: `succ n = n + 1` -/ def succ (a : β„€) := a + 1 /-- Immediate predecessor of an integer: `pred n = n - 1` -/ def pred (a : β„€) := a - 1 theorem nat_succ_eq_int_succ (n : β„•) : (nat.succ n : β„€) = int.succ n := rfl theorem pred_succ (a : β„€) : pred (succ a) = a := add_sub_cancel _ _ theorem succ_pred (a : β„€) : succ (pred a) = a := sub_add_cancel _ _ theorem neg_succ (a : β„€) : -succ a = pred (-a) := neg_add _ _ theorem succ_neg_succ (a : β„€) : succ (-succ a) = -a := by rw [neg_succ, succ_pred] theorem neg_pred (a : β„€) : -pred a = succ (-a) := by rw [eq_neg_of_eq_neg (neg_succ (-a)).symm, neg_neg] theorem pred_neg_pred (a : β„€) : pred (-pred a) = -a := by rw [neg_pred, pred_succ] theorem pred_nat_succ (n : β„•) : pred (nat.succ n) = n := pred_succ n theorem neg_nat_succ (n : β„•) : -(nat.succ n : β„€) = pred (-n) := neg_succ n theorem succ_neg_nat_succ (n : β„•) : succ (-nat.succ n) = -n := succ_neg_succ n theorem lt_succ_self (a : β„€) : a < succ a := lt_add_of_pos_right _ zero_lt_one theorem pred_self_lt (a : β„€) : pred a < a := sub_lt_self _ zero_lt_one theorem add_one_le_iff {a b : β„€} : a + 1 ≀ b ↔ a < b := iff.rfl theorem lt_add_one_iff {a b : β„€} : a < b + 1 ↔ a ≀ b := add_le_add_iff_right _ @[simp] lemma succ_coe_nat_pos (n : β„•) : 0 < (n : β„€) + 1 := lt_add_one_iff.mpr (by simp) @[norm_cast] lemma coe_pred_of_pos {n : β„•} (h : 0 < n) : ((n - 1 : β„•) : β„€) = (n : β„€) - 1 := by { cases n, cases h, simp, } lemma le_add_one {a b : β„€} (h : a ≀ b) : a ≀ b + 1 := le_of_lt (int.lt_add_one_iff.mpr h) theorem sub_one_lt_iff {a b : β„€} : a - 1 < b ↔ a ≀ b := sub_lt_iff_lt_add.trans lt_add_one_iff theorem le_sub_one_iff {a b : β„€} : a ≀ b - 1 ↔ a < b := le_sub_iff_add_le @[simp] lemma eq_zero_iff_abs_lt_one {a : β„€} : abs a < 1 ↔ a = 0 := ⟨λ a0, let ⟨hn, hp⟩ := abs_lt.mp a0 in (le_of_lt_add_one (by exact hp)).antisymm hn, Ξ» a0, (abs_eq_zero.mpr a0).le.trans_lt zero_lt_one⟩ @[elab_as_eliminator] protected lemma induction_on {p : β„€ β†’ Prop} (i : β„€) (hz : p 0) (hp : βˆ€ i : β„•, p i β†’ p (i + 1)) (hn : βˆ€ i : β„•, p (-i) β†’ p (-i - 1)) : p i := begin induction i, { induction i, { exact hz }, { exact hp _ i_ih } }, { have : βˆ€ n:β„•, p (- n), { intro n, induction n, { simp [hz] }, { convert hn _ n_ih using 1, simp [sub_eq_neg_add] } }, exact this (i + 1) } end /-- Inductively define a function on `β„€` by defining it at `b`, for the `succ` of a number greater than `b`, and the `pred` of a number less than `b`. -/ protected def induction_on' {C : β„€ β†’ Sort*} (z : β„€) (b : β„€) : C b β†’ (βˆ€ k, b ≀ k β†’ C k β†’ C (k + 1)) β†’ (βˆ€ k ≀ b, C k β†’ C (k - 1)) β†’ C z := Ξ» H0 Hs Hp, begin rw ←sub_add_cancel z b, induction (z - b) with n n, { induction n with n ih, { rwa [of_nat_zero, zero_add] }, rw [of_nat_succ, add_assoc, add_comm 1 b, ←add_assoc], exact Hs _ (le_add_of_nonneg_left (of_nat_nonneg _)) ih }, { induction n with n ih, { rw [neg_succ_of_nat_eq, ←of_nat_eq_coe, of_nat_zero, zero_add, neg_add_eq_sub], exact Hp _ (le_refl _) H0 }, { rw [neg_succ_of_nat_coe', nat.succ_eq_add_one, ←neg_succ_of_nat_coe, sub_add_eq_add_sub], exact Hp _ (le_of_lt (add_lt_of_neg_of_le (neg_succ_lt_zero _) (le_refl _))) ih } } end /-! ### nat abs -/ attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one theorem nat_abs_add_le (a b : β„€) : nat_abs (a + b) ≀ nat_abs a + nat_abs b := begin have : βˆ€ (a b : β„•), nat_abs (sub_nat_nat a (nat.succ b)) ≀ nat.succ (a + b), { refine (Ξ» a b : β„•, sub_nat_nat_elim a b.succ (Ξ» m n i, n = b.succ β†’ nat_abs i ≀ (m + b).succ) _ _ rfl); intros i n e, { subst e, rw [add_comm _ i, add_assoc], exact nat.le_add_right i (b.succ + b).succ }, { apply succ_le_succ, rw [← succ.inj e, ← add_assoc, add_comm], apply nat.le_add_right } }, cases a; cases b with b b; simp [nat_abs, nat.succ_add]; try {refl}; [skip, rw add_comm a b]; apply this end lemma nat_abs_sub_le (a b : β„€) : nat_abs (a - b) ≀ nat_abs a + nat_abs b := by { rw [sub_eq_add_neg, ← int.nat_abs_neg b], apply nat_abs_add_le } theorem nat_abs_neg_of_nat (n : β„•) : nat_abs (neg_of_nat n) = n := by cases n; refl theorem nat_abs_mul (a b : β„€) : nat_abs (a * b) = (nat_abs a) * (nat_abs b) := by cases a; cases b; simp only [← int.mul_def, int.mul, nat_abs_neg_of_nat, eq_self_iff_true, int.nat_abs] lemma nat_abs_mul_nat_abs_eq {a b : β„€} {c : β„•} (h : a * b = (c : β„€)) : a.nat_abs * b.nat_abs = c := by rw [← nat_abs_mul, h, nat_abs_of_nat] @[simp] lemma nat_abs_mul_self' (a : β„€) : (nat_abs a * nat_abs a : β„€) = a * a := by rw [← int.coe_nat_mul, nat_abs_mul_self] theorem neg_succ_of_nat_eq' (m : β„•) : -[1+ m] = -m - 1 := by simp [neg_succ_of_nat_eq, sub_eq_neg_add] lemma nat_abs_ne_zero_of_ne_zero {z : β„€} (hz : z β‰  0) : z.nat_abs β‰  0 := Ξ» h, hz $ int.eq_zero_of_nat_abs_eq_zero h @[simp] lemma nat_abs_eq_zero {a : β„€} : a.nat_abs = 0 ↔ a = 0 := ⟨int.eq_zero_of_nat_abs_eq_zero, Ξ» h, h.symm β–Έ rfl⟩ lemma nat_abs_ne_zero {a : β„€} : a.nat_abs β‰  0 ↔ a β‰  0 := not_congr int.nat_abs_eq_zero lemma nat_abs_lt_nat_abs_of_nonneg_of_lt {a b : β„€} (w₁ : 0 ≀ a) (wβ‚‚ : a < b) : a.nat_abs < b.nat_abs := begin lift b to β„• using le_trans w₁ (le_of_lt wβ‚‚), lift a to β„• using w₁, simpa using wβ‚‚, end lemma nat_abs_eq_nat_abs_iff {a b : β„€} : a.nat_abs = b.nat_abs ↔ a = b ∨ a = -b := begin split; intro h, { cases int.nat_abs_eq a with h₁ h₁; cases int.nat_abs_eq b with hβ‚‚ hβ‚‚; rw [h₁, hβ‚‚]; simp [h], }, { cases h; rw h, rw int.nat_abs_neg, }, end lemma nat_abs_eq_iff {a : β„€} {n : β„•} : a.nat_abs = n ↔ a = n ∨ a = -n := by rw [←int.nat_abs_eq_nat_abs_iff, int.nat_abs_of_nat] lemma nat_abs_eq_iff_mul_self_eq {a b : β„€} : a.nat_abs = b.nat_abs ↔ a * a = b * b := begin rw [← abs_eq_iff_mul_self_eq, abs_eq_nat_abs, abs_eq_nat_abs], exact int.coe_nat_inj'.symm end lemma nat_abs_lt_iff_mul_self_lt {a b : β„€} : a.nat_abs < b.nat_abs ↔ a * a < b * b := begin rw [← abs_lt_iff_mul_self_lt, abs_eq_nat_abs, abs_eq_nat_abs], exact int.coe_nat_lt.symm end lemma nat_abs_le_iff_mul_self_le {a b : β„€} : a.nat_abs ≀ b.nat_abs ↔ a * a ≀ b * b := begin rw [← abs_le_iff_mul_self_le, abs_eq_nat_abs, abs_eq_nat_abs], exact int.coe_nat_le.symm end lemma nat_abs_eq_iff_sq_eq {a b : β„€} : a.nat_abs = b.nat_abs ↔ a ^ 2 = b ^ 2 := by { rw [sq, sq], exact nat_abs_eq_iff_mul_self_eq } lemma nat_abs_lt_iff_sq_lt {a b : β„€} : a.nat_abs < b.nat_abs ↔ a ^ 2 < b ^ 2 := by { rw [sq, sq], exact nat_abs_lt_iff_mul_self_lt } lemma nat_abs_le_iff_sq_le {a b : β„€} : a.nat_abs ≀ b.nat_abs ↔ a ^ 2 ≀ b ^ 2 := by { rw [sq, sq], exact nat_abs_le_iff_mul_self_le } /-! ### `/` -/ @[simp] theorem of_nat_div (m n : β„•) : of_nat (m / n) = (of_nat m) / (of_nat n) := rfl @[simp, norm_cast] theorem coe_nat_div (m n : β„•) : ((m / n : β„•) : β„€) = m / n := rfl theorem neg_succ_of_nat_div (m : β„•) {b : β„€} (H : 0 < b) : -[1+m] / b = -(m / b + 1) := match b, eq_succ_of_zero_lt H with ._, ⟨n, rfl⟩ := rfl end -- Will be generalized to Euclidean domains. local attribute [simp] protected theorem zero_div : βˆ€ (b : β„€), 0 / b = 0 | 0 := show of_nat _ = _, by simp | (n+1:β„•) := show of_nat _ = _, by simp | -[1+ n] := show -of_nat _ = _, by simp local attribute [simp] -- Will be generalized to Euclidean domains. protected theorem div_zero : βˆ€ (a : β„€), a / 0 = 0 | 0 := show of_nat _ = _, by simp | (n+1:β„•) := show of_nat _ = _, by simp | -[1+ n] := rfl @[simp] protected theorem div_neg : βˆ€ (a b : β„€), a / -b = -(a / b) | (m : β„•) 0 := show of_nat (m / 0) = -(m / 0 : β„•), by rw nat.div_zero; refl | (m : β„•) (n+1:β„•) := rfl | 0 -[1+ n] := by simp | (m+1:β„•) -[1+ n] := (neg_neg _).symm | -[1+ m] 0 := rfl | -[1+ m] (n+1:β„•) := rfl | -[1+ m] -[1+ n] := rfl theorem div_of_neg_of_pos {a b : β„€} (Ha : a < 0) (Hb : 0 < b) : a / b = -((-a - 1) / b + 1) := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := by change (- -[1+ m] : β„€) with (m+1 : β„€); rw add_sub_cancel; refl end protected theorem div_nonneg {a b : β„€} (Ha : 0 ≀ a) (Hb : 0 ≀ b) : 0 ≀ a / b := match a, b, eq_coe_of_zero_le Ha, eq_coe_of_zero_le Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := coe_zero_le _ end protected theorem div_nonpos {a b : β„€} (Ha : 0 ≀ a) (Hb : b ≀ 0) : a / b ≀ 0 := nonpos_of_neg_nonneg $ by rw [← int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb) theorem div_neg' {a b : β„€} (Ha : a < 0) (Hb : 0 < b) : a / b < 0 := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := neg_succ_lt_zero _ end @[simp] protected theorem div_one : βˆ€ (a : β„€), a / 1 = a | 0 := show of_nat _ = _, by simp | (n+1:β„•) := congr_arg of_nat (nat.div_one _) | -[1+ n] := congr_arg neg_succ_of_nat (nat.div_one _) theorem div_eq_zero_of_lt {a b : β„€} (H1 : 0 ≀ a) (H2 : a < b) : a / b = 0 := match a, b, eq_coe_of_zero_le H1, eq_succ_of_zero_lt (lt_of_le_of_lt H1 H2), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.div_eq_of_lt $ lt_of_coe_nat_lt_coe_nat H2 end theorem div_eq_zero_of_lt_abs {a b : β„€} (H1 : 0 ≀ a) (H2 : a < abs b) : a / b = 0 := match b, abs b, abs_eq_nat_abs b, H2 with | (n : β„•), ._, rfl, H2 := div_eq_zero_of_lt H1 H2 | -[1+ n], ._, rfl, H2 := neg_injective $ by rw [← int.div_neg]; exact div_eq_zero_of_lt H1 H2 end protected theorem add_mul_div_right (a b : β„€) {c : β„€} (H : c β‰  0) : (a + b * c) / c = a / c + b := have βˆ€ {k n : β„•} {a : β„€}, (a + n * k.succ) / k.succ = a / k.succ + n, from Ξ» k n a, match a with | (m : β„•) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos | -[1+ m] := show ((n * k.succ:β„•) - m.succ : β„€) / k.succ = n - (m / k.succ + 1 : β„•), begin cases lt_or_ge m (n*k.succ) with h h, { rw [← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.div_lt_iff_lt_mul _ _ k.succ_pos).2 h)], apply congr_arg of_nat, rw [mul_comm, nat.mul_sub_div], rwa mul_comm }, { change (↑(n * nat.succ k) - (m + 1) : β„€) / ↑(nat.succ k) = ↑n - ((m / nat.succ k : β„•) + 1), rw [← sub_sub, ← sub_sub, ← neg_sub (m:β„€), ← neg_sub _ (n:β„€), ← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.le_div_iff_mul_le _ _ k.succ_pos).2 h), ← neg_succ_of_nat_coe', ← neg_succ_of_nat_coe'], { apply congr_arg neg_succ_of_nat, rw [mul_comm, nat.sub_mul_div], rwa mul_comm } } end end, have βˆ€ {a b c : β„€}, 0 < c β†’ (a + b * c) / c = a / c + b, from Ξ» a b c H, match c, eq_succ_of_zero_lt H, b with | ._, ⟨k, rfl⟩, (n : β„•) := this | ._, ⟨k, rfl⟩, -[1+ n] := show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from eq_sub_of_add_eq $ by rw [← this, sub_add_cancel] end, match lt_trichotomy c 0 with | or.inl hlt := neg_inj.1 $ by rw [← int.div_neg, neg_add, ← int.div_neg, ← neg_mul_neg]; apply this (neg_pos_of_neg hlt) | or.inr (or.inl heq) := absurd heq H | or.inr (or.inr hgt) := this hgt end protected theorem add_mul_div_left (a : β„€) {b : β„€} (c : β„€) (H : b β‰  0) : (a + b * c) / b = a / b + c := by rw [mul_comm, int.add_mul_div_right _ _ H] protected theorem add_div_of_dvd_right {a b c : β„€} (H : c ∣ b) : (a + b) / c = a / c + b / c := begin by_cases h1 : c = 0, { simp [h1] }, cases H with k hk, rw hk, change c β‰  0 at h1, rw [mul_comm c k, int.add_mul_div_right _ _ h1, ←zero_add (k * c), int.add_mul_div_right _ _ h1, int.zero_div, zero_add] end protected theorem add_div_of_dvd_left {a b c : β„€} (H : c ∣ a) : (a + b) / c = a / c + b / c := by rw [add_comm, int.add_div_of_dvd_right H, add_comm] @[simp] protected theorem mul_div_cancel (a : β„€) {b : β„€} (H : b β‰  0) : a * b / b = a := by have := int.add_mul_div_right 0 a H; rwa [zero_add, int.zero_div, zero_add] at this @[simp] protected theorem mul_div_cancel_left {a : β„€} (b : β„€) (H : a β‰  0) : a * b / a = b := by rw [mul_comm, int.mul_div_cancel _ H] @[simp] protected theorem div_self {a : β„€} (H : a β‰  0) : a / a = 1 := by have := int.mul_div_cancel 1 H; rwa one_mul at this /-! ### mod -/ theorem of_nat_mod (m n : nat) : (m % n : β„€) = of_nat (m % n) := rfl @[simp, norm_cast] theorem coe_nat_mod (m n : β„•) : (↑(m % n) : β„€) = ↑m % ↑n := rfl theorem neg_succ_of_nat_mod (m : β„•) {b : β„€} (bpos : 0 < b) : -[1+m] % b = b - 1 - m % b := by rw [sub_sub, add_comm]; exact match b, eq_succ_of_zero_lt bpos with ._, ⟨n, rfl⟩ := rfl end @[simp] theorem mod_neg : βˆ€ (a b : β„€), a % -b = a % b | (m : β„•) n := @congr_arg β„• β„€ _ _ (Ξ» i, ↑(m % i)) (nat_abs_neg _) | -[1+ m] n := @congr_arg β„• β„€ _ _ (Ξ» i, sub_nat_nat i (nat.succ (m % i))) (nat_abs_neg _) @[simp] theorem mod_abs (a b : β„€) : a % (abs b) = a % b := abs_by_cases (Ξ» i, a % i = a % b) rfl (mod_neg _ _) local attribute [simp] -- Will be generalized to Euclidean domains. theorem zero_mod (b : β„€) : 0 % b = 0 := rfl local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_zero : βˆ€ (a : β„€), a % 0 = a | (m : β„•) := congr_arg of_nat $ nat.mod_zero _ | -[1+ m] := congr_arg neg_succ_of_nat $ nat.mod_zero _ local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_one : βˆ€ (a : β„€), a % 1 = 0 | (m : β„•) := congr_arg of_nat $ nat.mod_one _ | -[1+ m] := show (1 - (m % 1).succ : β„€) = 0, by rw nat.mod_one; refl theorem mod_eq_of_lt {a b : β„€} (H1 : 0 ≀ a) (H2 : a < b) : a % b = a := match a, b, eq_coe_of_zero_le H1, eq_coe_of_zero_le (le_trans H1 (le_of_lt H2)), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.mod_eq_of_lt (lt_of_coe_nat_lt_coe_nat H2) end theorem mod_nonneg : βˆ€ (a : β„€) {b : β„€}, b β‰  0 β†’ 0 ≀ a % b | (m : β„•) n H := coe_zero_le _ | -[1+ m] n H := sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H) theorem mod_lt_of_pos (a : β„€) {b : β„€} (H : 0 < b) : a % b < b := match a, b, eq_succ_of_zero_lt H with | (m : β„•), ._, ⟨n, rfl⟩ := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _)) | -[1+ m], ._, ⟨n, rfl⟩ := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _) end theorem mod_lt (a : β„€) {b : β„€} (H : b β‰  0) : a % b < abs b := by rw [← mod_abs]; exact mod_lt_of_pos _ (abs_pos.2 H) theorem mod_add_div_aux (m n : β„•) : (n - (m % n + 1) - (n * (m / n) + n) : β„€) = -[1+ m] := begin rw [← sub_sub, neg_succ_of_nat_coe, sub_sub (n:β„€)], apply eq_neg_of_eq_neg, rw [neg_sub, sub_sub_self, add_right_comm], exact @congr_arg β„• β„€ _ _ (Ξ»i, (i + 1 : β„€)) (nat.mod_add_div _ _).symm end theorem mod_add_div : βˆ€ (a b : β„€), a % b + b * (a / b) = a | (m : β„•) 0 := congr_arg of_nat (nat.mod_add_div _ _) | (m : β„•) (n+1:β„•) := congr_arg of_nat (nat.mod_add_div _ _) | 0 -[1+ n] := by simp | (m+1:β„•) -[1+ n] := show (_ + -(n+1) * -((m + 1) / (n + 1) : β„•) : β„€) = _, by rw [neg_mul_neg]; exact congr_arg of_nat (nat.mod_add_div _ _) | -[1+ m] 0 := by rw [mod_zero, int.div_zero]; refl | -[1+ m] (n+1:β„•) := mod_add_div_aux m n.succ | -[1+ m] -[1+ n] := mod_add_div_aux m n.succ theorem div_add_mod (a b : β„€) : b * (a / b) + a % b = a := (add_comm _ _).trans (mod_add_div _ _) lemma mod_add_div' (m k : β„€) : m % k + (m / k) * k = m := by { rw mul_comm, exact mod_add_div _ _ } lemma div_add_mod' (m k : β„€) : (m / k) * k + m % k = m := by { rw mul_comm, exact div_add_mod _ _ } theorem mod_def (a b : β„€) : a % b = a - b * (a / b) := eq_sub_of_add_eq (mod_add_div _ _) @[simp] theorem add_mul_mod_self {a b c : β„€} : (a + b * c) % c = a % c := if cz : c = 0 then by rw [cz, mul_zero, add_zero] else by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz, mul_add, mul_comm, add_sub_add_right_eq_sub] @[simp] theorem add_mul_mod_self_left (a b c : β„€) : (a + b * c) % b = a % b := by rw [mul_comm, add_mul_mod_self] @[simp] theorem add_mod_self {a b : β„€} : (a + b) % b = a % b := by have := add_mul_mod_self_left a b 1; rwa mul_one at this @[simp] theorem add_mod_self_left {a b : β„€} : (a + b) % a = b % a := by rw [add_comm, add_mod_self] @[simp] theorem mod_add_mod (m n k : β„€) : (m % n + k) % n = (m + k) % n := by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm; rwa [add_right_comm, mod_add_div] at this @[simp] theorem add_mod_mod (m n k : β„€) : (m + n % k) % k = (m + n) % k := by rw [add_comm, mod_add_mod, add_comm] lemma add_mod (a b n : β„€) : (a + b) % n = ((a % n) + (b % n)) % n := by rw [add_mod_mod, mod_add_mod] theorem add_mod_eq_add_mod_right {m n k : β„€} (i : β„€) (H : m % n = k % n) : (m + i) % n = (k + i) % n := by rw [← mod_add_mod, ← mod_add_mod k, H] theorem add_mod_eq_add_mod_left {m n k : β„€} (i : β„€) (H : m % n = k % n) : (i + m) % n = (i + k) % n := by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm] theorem mod_add_cancel_right {m n k : β„€} (i) : (m + i) % n = (k + i) % n ↔ m % n = k % n := ⟨λ H, by have := add_mod_eq_add_mod_right (-i) H; rwa [add_neg_cancel_right, add_neg_cancel_right] at this, add_mod_eq_add_mod_right _⟩ theorem mod_add_cancel_left {m n k i : β„€} : (i + m) % n = (i + k) % n ↔ m % n = k % n := by rw [add_comm, add_comm i, mod_add_cancel_right] theorem mod_sub_cancel_right {m n k : β„€} (i) : (m - i) % n = (k - i) % n ↔ m % n = k % n := mod_add_cancel_right _ theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : β„€} : m % n = k % n ↔ (m - k) % n = 0 := (mod_sub_cancel_right k).symm.trans $ by simp @[simp] theorem mul_mod_left (a b : β„€) : (a * b) % b = 0 := by rw [← zero_add (a * b), add_mul_mod_self, zero_mod] @[simp] theorem mul_mod_right (a b : β„€) : (a * b) % a = 0 := by rw [mul_comm, mul_mod_left] lemma mul_mod (a b n : β„€) : (a * b) % n = ((a % n) * (b % n)) % n := begin conv_lhs { rw [←mod_add_div a n, ←mod_add_div' b n, right_distrib, left_distrib, left_distrib, mul_assoc, mul_assoc, ←left_distrib n _ _, add_mul_mod_self_left, ← mul_assoc, add_mul_mod_self] } end @[simp] lemma neg_mod_two (i : β„€) : (-i) % 2 = i % 2 := begin apply int.mod_eq_mod_iff_mod_sub_eq_zero.mpr, convert int.mul_mod_right 2 (-i), simp only [two_mul, sub_eq_add_neg] end local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_self {a : β„€} : a % a = 0 := by have := mul_mod_left 1 a; rwa one_mul at this @[simp] theorem mod_mod_of_dvd (n : β„€) {m k : β„€} (h : m ∣ k) : n % k % m = n % m := begin conv { to_rhs, rw ←mod_add_div n k }, rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left] end @[simp] theorem mod_mod (a b : β„€) : a % b % b = a % b := by conv {to_rhs, rw [← mod_add_div a b, add_mul_mod_self_left]} lemma sub_mod (a b n : β„€) : (a - b) % n = ((a % n) - (b % n)) % n := begin apply (mod_add_cancel_right b).mp, rw [sub_add_cancel, ← add_mod_mod, sub_add_cancel, mod_mod] end /-! ### properties of `/` and `%` -/ @[simp] theorem mul_div_mul_of_pos {a : β„€} (b c : β„€) (H : 0 < a) : a * b / (a * c) = b / c := suffices βˆ€ (m k : β„•) (b : β„€), (m.succ * b / (m.succ * k) : β„€) = b / k, from match a, eq_succ_of_zero_lt H, c, eq_coe_or_neg c with | ._, ⟨m, rfl⟩, ._, ⟨k, or.inl rfl⟩ := this _ _ _ | ._, ⟨m, rfl⟩, ._, ⟨k, or.inr rfl⟩ := by rw [← neg_mul_eq_mul_neg, int.div_neg, int.div_neg]; apply congr_arg has_neg.neg; apply this end, Ξ» m k b, match b, k with | (n : β„•), k := congr_arg of_nat (nat.mul_div_mul _ _ m.succ_pos) | -[1+ n], 0 := by rw [int.coe_nat_zero, mul_zero, int.div_zero, int.div_zero] | -[1+ n], k+1 := congr_arg neg_succ_of_nat $ show (m.succ * n + m) / (m.succ * k.succ) = n / k.succ, begin apply nat.div_eq_of_lt_le, { refine le_trans _ (nat.le_add_right _ _), rw [← nat.mul_div_mul _ _ m.succ_pos], apply nat.div_mul_le_self }, { change m.succ * n.succ ≀ _, rw [mul_left_comm], apply nat.mul_le_mul_left, apply (nat.div_lt_iff_lt_mul _ _ k.succ_pos).1, apply nat.lt_succ_self } end end @[simp] theorem mul_div_mul_of_pos_left (a : β„€) {b : β„€} (H : 0 < b) (c : β„€) : a * b / (c * b) = a / c := by rw [mul_comm, mul_comm c, mul_div_mul_of_pos _ _ H] @[simp] theorem mul_mod_mul_of_pos {a : β„€} (H : 0 < a) (b c : β„€) : a * b % (a * c) = a * (b % c) := by rw [mod_def, mod_def, mul_div_mul_of_pos _ _ H, mul_sub_left_distrib, mul_assoc] theorem lt_div_add_one_mul_self (a : β„€) {b : β„€} (H : 0 < b) : a < (a / b + 1) * b := by { rw [add_mul, one_mul, mul_comm, ← sub_lt_iff_lt_add', ← mod_def], exact mod_lt_of_pos _ H } theorem abs_div_le_abs : βˆ€ (a b : β„€), abs (a / b) ≀ abs a := suffices βˆ€ (a : β„€) (n : β„•), abs (a / n) ≀ abs a, from Ξ» a b, match b, eq_coe_or_neg b with | ._, ⟨n, or.inl rfl⟩ := this _ _ | ._, ⟨n, or.inr rfl⟩ := by rw [int.div_neg, abs_neg]; apply this end, Ξ» a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact coe_nat_le_coe_nat_of_le (match a, n with | (m : β„•), n := nat.div_le_self _ _ | -[1+ m], 0 := nat.zero_le _ | -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _) end) theorem div_le_self {a : β„€} (b : β„€) (Ha : 0 ≀ a) : a / b ≀ a := by have := le_trans (le_abs_self _) (abs_div_le_abs a b); rwa [abs_of_nonneg Ha] at this theorem mul_div_cancel_of_mod_eq_zero {a b : β„€} (H : a % b = 0) : b * (a / b) = a := by have := mod_add_div a b; rwa [H, zero_add] at this theorem div_mul_cancel_of_mod_eq_zero {a b : β„€} (H : a % b = 0) : a / b * b = a := by rw [mul_comm, mul_div_cancel_of_mod_eq_zero H] lemma mod_two_eq_zero_or_one (n : β„€) : n % 2 = 0 ∨ n % 2 = 1 := have h : n % 2 < 2 := abs_of_nonneg (show 0 ≀ (2 : β„€), from dec_trivial) β–Έ int.mod_lt _ dec_trivial, have h₁ : 0 ≀ n % 2 := int.mod_nonneg _ dec_trivial, match (n % 2), h, h₁ with | (0 : β„•) := Ξ» _ _, or.inl rfl | (1 : β„•) := Ξ» _ _, or.inr rfl | (k + 2 : β„•) := Ξ» h _, absurd h dec_trivial | -[1+ a] := Ξ» _ h₁, absurd h₁ dec_trivial end /-! ### dvd -/ @[norm_cast] theorem coe_nat_dvd {m n : β„•} : (↑m : β„€) ∣ ↑n ↔ m ∣ n := ⟨λ ⟨a, ae⟩, m.eq_zero_or_pos.elim (Ξ»m0, by simp [m0] at ae; simp [ae, m0]) (Ξ»m0l, by { cases eq_coe_of_zero_le (@nonneg_of_mul_nonneg_left β„€ _ m a (by simp [ae.symm]) (by simpa using m0l)) with k e, subst a, exact ⟨k, int.coe_nat_inj ae⟩ }), Ξ» ⟨k, e⟩, dvd.intro k $ by rw [e, int.coe_nat_mul]⟩ theorem coe_nat_dvd_left {n : β„•} {z : β„€} : (↑n : β„€) ∣ z ↔ n ∣ z.nat_abs := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem coe_nat_dvd_right {n : β„•} {z : β„€} : z ∣ (↑n : β„€) ↔ z.nat_abs ∣ n := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem dvd_antisymm {a b : β„€} (H1 : 0 ≀ a) (H2 : 0 ≀ b) : a ∣ b β†’ b ∣ a β†’ a = b := begin rw [← abs_of_nonneg H1, ← abs_of_nonneg H2, abs_eq_nat_abs, abs_eq_nat_abs], rw [coe_nat_dvd, coe_nat_dvd, coe_nat_inj'], apply nat.dvd_antisymm end theorem dvd_of_mod_eq_zero {a b : β„€} (H : b % a = 0) : a ∣ b := ⟨b / a, (mul_div_cancel_of_mod_eq_zero H).symm⟩ theorem mod_eq_zero_of_dvd : βˆ€ {a b : β„€}, a ∣ b β†’ b % a = 0 | a ._ ⟨c, rfl⟩ := mul_mod_right _ _ theorem dvd_iff_mod_eq_zero (a b : β„€) : a ∣ b ↔ b % a = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ /-- If `a % b = c` then `b` divides `a - c`. -/ lemma dvd_sub_of_mod_eq {a b c : β„€} (h : a % b = c) : b ∣ a - c := begin have hx : a % b % b = c % b, { rw h }, rw [mod_mod, ←mod_sub_cancel_right c, sub_self, zero_mod] at hx, exact dvd_of_mod_eq_zero hx end theorem nat_abs_dvd {a b : β„€} : (a.nat_abs : β„€) ∣ b ↔ a ∣ b := (nat_abs_eq a).elim (Ξ» e, by rw ← e) (Ξ» e, by rw [← neg_dvd, ← e]) theorem dvd_nat_abs {a b : β„€} : a ∣ b.nat_abs ↔ a ∣ b := (nat_abs_eq b).elim (Ξ» e, by rw ← e) (Ξ» e, by rw [← dvd_neg, ← e]) instance decidable_dvd : @decidable_rel β„€ (∣) := assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm protected theorem div_mul_cancel {a b : β„€} (H : b ∣ a) : a / b * b = a := div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H) protected theorem mul_div_cancel' {a b : β„€} (H : a ∣ b) : a * (b / a) = b := by rw [mul_comm, int.div_mul_cancel H] protected theorem mul_div_assoc (a : β„€) : βˆ€ {b c : β„€}, c ∣ b β†’ (a * b) / c = a * (b / c) | ._ c ⟨d, rfl⟩ := if cz : c = 0 then by simp [cz] else by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz] protected theorem mul_div_assoc' (b : β„€) {a c : β„€} (h : c ∣ a) : a * b / c = a / c * b := by rw [mul_comm, int.mul_div_assoc _ h, mul_comm] theorem div_dvd_div : βˆ€ {a b c : β„€} (H1 : a ∣ b) (H2 : b ∣ c), b / a ∣ c / a | a ._ ._ ⟨b, rfl⟩ ⟨c, rfl⟩ := if az : a = 0 then by simp [az] else by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az]; apply dvd_mul_right protected theorem eq_mul_of_div_eq_right {a b c : β„€} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, int.mul_div_cancel' H1] protected theorem div_eq_of_eq_mul_right {a b c : β„€} (H1 : b β‰  0) (H2 : a = b * c) : a / b = c := by rw [H2, int.mul_div_cancel_left _ H1] protected theorem eq_div_of_mul_eq_right {a b c : β„€} (H1 : a β‰  0) (H2 : a * b = c) : b = c / a := eq.symm $ int.div_eq_of_eq_mul_right H1 H2.symm protected theorem div_eq_iff_eq_mul_right {a b c : β„€} (H : b β‰  0) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : β„€} (H : b β‰  0) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : β„€} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2] protected theorem div_eq_of_eq_mul_left {a b c : β„€} (H1 : b β‰  0) (H2 : a = c * b) : a / b = c := int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2]) protected lemma eq_zero_of_div_eq_zero {d n : β„€} (h : d ∣ n) (H : n / d = 0) : n = 0 := by rw [← int.mul_div_cancel' h, H, mul_zero] theorem neg_div_of_dvd : βˆ€ {a b : β„€} (H : b ∣ a), -a / b = -(a / b) | ._ b ⟨c, rfl⟩ := if bz : b = 0 then by simp [bz] else by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz] lemma sub_div_of_dvd (a : β„€) {b c : β„€} (hcb : c ∣ b) : (a - b) / c = a / c - b / c := begin rw [sub_eq_add_neg, sub_eq_add_neg, int.add_div_of_dvd_right ((dvd_neg c b).mpr hcb)], congr, exact neg_div_of_dvd hcb, end lemma sub_div_of_dvd_sub {a b c : β„€} (hcab : c ∣ (a - b)) : (a - b) / c = a / c - b / c := by rw [eq_sub_iff_add_eq, ← int.add_div_of_dvd_left hcab, sub_add_cancel] theorem div_sign : βˆ€ a b, a / sign b = a * sign b | a (n+1:β„•) := by unfold sign; simp | a 0 := by simp [sign] | a -[1+ n] := by simp [sign] @[simp] theorem sign_mul : βˆ€ a b, sign (a * b) = sign a * sign b | a 0 := by simp | 0 b := by simp | (m+1:β„•) (n+1:β„•) := rfl | (m+1:β„•) -[1+ n] := rfl | -[1+ m] (n+1:β„•) := rfl | -[1+ m] -[1+ n] := rfl protected theorem sign_eq_div_abs (a : β„€) : sign a = a / (abs a) := if az : a = 0 then by simp [az] else (int.div_eq_of_eq_mul_left (mt abs_eq_zero.1 az) (sign_mul_abs _).symm).symm theorem mul_sign : βˆ€ (i : β„€), i * sign i = nat_abs i | (n+1:β„•) := mul_one _ | 0 := mul_zero _ | -[1+ n] := mul_neg_one _ @[simp] theorem sign_pow_bit1 (k : β„•) : βˆ€ n : β„€, n.sign ^ (bit1 k) = n.sign | (n+1:β„•) := one_pow (bit1 k) | 0 := zero_pow (nat.zero_lt_bit1 k) | -[1+ n] := (neg_pow_bit1 1 k).trans (congr_arg (Ξ» x, -x) (one_pow (bit1 k))) theorem le_of_dvd {a b : β„€} (bpos : 0 < b) (H : a ∣ b) : a ≀ b := match a, b, eq_succ_of_zero_lt bpos, H with | (m : β„•), ._, ⟨n, rfl⟩, H := coe_nat_le_coe_nat_of_le $ nat.le_of_dvd n.succ_pos $ coe_nat_dvd.1 H | -[1+ m], ._, ⟨n, rfl⟩, _ := le_trans (le_of_lt $ neg_succ_lt_zero _) (coe_zero_le _) end theorem eq_one_of_dvd_one {a : β„€} (H : 0 ≀ a) (H' : a ∣ 1) : a = 1 := match a, eq_coe_of_zero_le H, H' with | ._, ⟨n, rfl⟩, H' := congr_arg coe $ nat.eq_one_of_dvd_one $ coe_nat_dvd.1 H' end theorem eq_one_of_mul_eq_one_right {a b : β„€} (H : 0 ≀ a) (H' : a * b = 1) : a = 1 := eq_one_of_dvd_one H ⟨b, H'.symm⟩ theorem eq_one_of_mul_eq_one_left {a b : β„€} (H : 0 ≀ b) (H' : a * b = 1) : b = 1 := eq_one_of_mul_eq_one_right H (by rw [mul_comm, H']) lemma of_nat_dvd_of_dvd_nat_abs {a : β„•} : βˆ€ {z : β„€} (haz : a ∣ z.nat_abs), ↑a ∣ z | (int.of_nat _) haz := int.coe_nat_dvd.2 haz | -[1+k] haz := begin change ↑a ∣ -(k+1 : β„€), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, exact haz end lemma dvd_nat_abs_of_of_nat_dvd {a : β„•} : βˆ€ {z : β„€} (haz : ↑a ∣ z), a ∣ z.nat_abs | (int.of_nat _) haz := int.coe_nat_dvd.1 (int.dvd_nat_abs.2 haz) | -[1+k] haz := have haz' : (↑a:β„€) ∣ (↑(k+1):β„€), from dvd_of_dvd_neg haz, int.coe_nat_dvd.1 haz' lemma pow_dvd_of_le_of_pow_dvd {p m n : β„•} {k : β„€} (hmn : m ≀ n) (hdiv : ↑(p ^ n) ∣ k) : ↑(p ^ m) ∣ k := begin induction k, { apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1 hdiv }, change -[1+k] with -(↑(k+1) : β„€), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1, apply dvd_of_dvd_neg, exact hdiv, end lemma dvd_of_pow_dvd {p k : β„•} {m : β„€} (hk : 1 ≀ k) (hpk : ↑(p^k) ∣ m) : ↑p ∣ m := by rw ←pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk /-- If `n > 0` then `m` is not divisible by `n` iff it is between `n * k` and `n * (k + 1)` for some `k`. -/ lemma exists_lt_and_lt_iff_not_dvd (m : β„€) {n : β„€} (hn : 0 < n) : (βˆƒ k, n * k < m ∧ m < n * (k + 1)) ↔ Β¬ n ∣ m := begin split, { rintro ⟨k, h1k, h2k⟩ ⟨l, rfl⟩, rw [mul_lt_mul_left hn] at h1k h2k, rw [lt_add_one_iff, ← not_lt] at h2k, exact h2k h1k }, { intro h, rw [dvd_iff_mod_eq_zero, ← ne.def] at h, have := (mod_nonneg m hn.ne.symm).lt_of_ne h.symm, simp only [← mod_add_div m n] {single_pass := tt}, refine ⟨m / n, lt_add_of_pos_left _ this, _⟩, rw [add_comm _ (1 : β„€), left_distrib, mul_one], exact add_lt_add_right (mod_lt_of_pos _ hn) _ } end /-! ### `/` and ordering -/ protected theorem div_mul_le (a : β„€) {b : β„€} (H : b β‰  0) : a / b * b ≀ a := le_of_sub_nonneg $ by rw [mul_comm, ← mod_def]; apply mod_nonneg _ H protected theorem div_le_of_le_mul {a b c : β„€} (H : 0 < c) (H' : a ≀ b * c) : a / c ≀ b := le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H protected theorem mul_lt_of_lt_div {a b c : β„€} (H : 0 < c) (H3 : a < b / c) : a * c < b := lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3) protected theorem mul_le_of_le_div {a b c : β„€} (H1 : 0 < c) (H2 : a ≀ b / c) : a * c ≀ b := le_trans (decidable.mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1)) protected theorem le_div_of_mul_le {a b c : β„€} (H1 : 0 < c) (H2 : a * c ≀ b) : a ≀ b / c := le_of_lt_add_one $ lt_of_mul_lt_mul_right (lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1) protected theorem le_div_iff_mul_le {a b c : β„€} (H : 0 < c) : a ≀ b / c ↔ a * c ≀ b := ⟨int.mul_le_of_le_div H, int.le_div_of_mul_le H⟩ protected theorem div_le_div {a b c : β„€} (H : 0 < c) (H' : a ≀ b) : a / c ≀ b / c := int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H') protected theorem div_lt_of_lt_mul {a b c : β„€} (H : 0 < c) (H' : a < b * c) : a / c < b := lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H') protected theorem lt_mul_of_div_lt {a b c : β„€} (H1 : 0 < c) (H2 : a / c < b) : a < b * c := lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2) protected theorem div_lt_iff_lt_mul {a b c : β„€} (H : 0 < c) : a / c < b ↔ a < b * c := ⟨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul H⟩ protected theorem le_mul_of_div_le {a b c : β„€} (H1 : 0 ≀ b) (H2 : b ∣ a) (H3 : a / b ≀ c) : a ≀ c * b := by rw [← int.div_mul_cancel H2]; exact decidable.mul_le_mul_of_nonneg_right H3 H1 protected theorem lt_div_of_mul_lt {a b c : β„€} (H1 : 0 ≀ b) (H2 : b ∣ c) (H3 : a * b < c) : a < c / b := lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3) protected theorem lt_div_iff_mul_lt {a b : β„€} (c : β„€) (H : 0 < c) (H' : c ∣ b) : a < b / c ↔ a * c < b := ⟨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'⟩ theorem div_pos_of_pos_of_dvd {a b : β„€} (H1 : 0 < a) (H2 : 0 ≀ b) (H3 : b ∣ a) : 0 < a / b := int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul) theorem div_eq_div_of_mul_eq_mul {a b c d : β„€} (H2 : d ∣ c) (H3 : b β‰  0) (H4 : d β‰  0) (H5 : a * d = b * c) : a / b = c / d := int.div_eq_of_eq_mul_right H3 $ by rw [← int.mul_div_assoc _ H2]; exact (int.div_eq_of_eq_mul_left H4 H5.symm).symm theorem eq_mul_div_of_mul_eq_mul_of_dvd_left {a b c d : β„€} (hb : b β‰  0) (hbc : b ∣ c) (h : b * a = c * d) : a = c / b * d := begin cases hbc with k hk, subst hk, rw [int.mul_div_cancel_left _ hb], rw mul_assoc at h, apply mul_left_cancel' hb h end /-- If an integer with larger absolute value divides an integer, it is zero. -/ lemma eq_zero_of_dvd_of_nat_abs_lt_nat_abs {a b : β„€} (w : a ∣ b) (h : nat_abs b < nat_abs a) : b = 0 := begin rw [←nat_abs_dvd, ←dvd_nat_abs, coe_nat_dvd] at w, rw ←nat_abs_eq_zero, exact eq_zero_of_dvd_of_lt w h end lemma eq_zero_of_dvd_of_nonneg_of_lt {a b : β„€} (w₁ : 0 ≀ a) (wβ‚‚ : a < b) (h : b ∣ a) : a = 0 := eq_zero_of_dvd_of_nat_abs_lt_nat_abs h (nat_abs_lt_nat_abs_of_nonneg_of_lt w₁ wβ‚‚) /-- If two integers are congruent to a sufficiently large modulus, they are equal. -/ lemma eq_of_mod_eq_of_nat_abs_sub_lt_nat_abs {a b c : β„€} (h1 : a % b = c) (h2 : nat_abs (a - c) < nat_abs b) : a = c := eq_of_sub_eq_zero (eq_zero_of_dvd_of_nat_abs_lt_nat_abs (dvd_sub_of_mod_eq h1) h2) theorem of_nat_add_neg_succ_of_nat_of_lt {m n : β„•} (h : m < n.succ) : of_nat m + -[1+n] = -[1+ n - m] := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = (n - m).succ, apply succ_sub, apply le_of_lt_succ h, simp [*, sub_nat_nat] end theorem of_nat_add_neg_succ_of_nat_of_ge {m n : β„•} (h : n.succ ≀ m) : of_nat m + -[1+n] = of_nat (m - n.succ) := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = 0, apply sub_eq_zero_of_le h, simp [*, sub_nat_nat] end @[simp] theorem neg_add_neg (m n : β„•) : -[1+m] + -[1+n] = -[1+nat.succ(m+n)] := rfl /-! ### to_nat -/ theorem to_nat_eq_max : βˆ€ (a : β„€), (to_nat a : β„€) = max a 0 | (n : β„•) := (max_eq_left (coe_zero_le n)).symm | -[1+ n] := (max_eq_right (le_of_lt (neg_succ_lt_zero n))).symm @[simp] lemma to_nat_zero : (0 : β„€).to_nat = 0 := rfl @[simp] lemma to_nat_one : (1 : β„€).to_nat = 1 := rfl @[simp] theorem to_nat_of_nonneg {a : β„€} (h : 0 ≀ a) : (to_nat a : β„€) = a := by rw [to_nat_eq_max, max_eq_left h] @[simp] lemma to_nat_sub_of_le {a b : β„€} (h : b ≀ a) : (to_nat (a - b) : β„€) = a - b := int.to_nat_of_nonneg (sub_nonneg_of_le h) @[simp] theorem to_nat_coe_nat (n : β„•) : to_nat ↑n = n := rfl @[simp] lemma to_nat_coe_nat_add_one {n : β„•} : ((n : β„€) + 1).to_nat = n + 1 := rfl theorem le_to_nat (a : β„€) : a ≀ to_nat a := by rw [to_nat_eq_max]; apply le_max_left @[simp] theorem to_nat_le {a : β„€} {n : β„•} : to_nat a ≀ n ↔ a ≀ n := by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff]; exact and_iff_left (coe_zero_le _) @[simp] theorem lt_to_nat {n : β„•} {a : β„€} : n < to_nat a ↔ (n : β„€) < a := le_iff_le_iff_lt_iff_lt.1 to_nat_le theorem to_nat_le_to_nat {a b : β„€} (h : a ≀ b) : to_nat a ≀ to_nat b := by rw to_nat_le; exact le_trans h (le_to_nat b) theorem to_nat_lt_to_nat {a b : β„€} (hb : 0 < b) : to_nat a < to_nat b ↔ a < b := ⟨λ h, begin cases a, exact lt_to_nat.1 h, exact lt_trans (neg_succ_of_nat_lt_zero a) hb, end, Ξ» h, begin rw lt_to_nat, cases a, exact h, exact hb end⟩ theorem lt_of_to_nat_lt {a b : β„€} (h : to_nat a < to_nat b) : a < b := (to_nat_lt_to_nat $ lt_to_nat.1 $ lt_of_le_of_lt (nat.zero_le _) h).1 h lemma to_nat_add {a b : β„€} (ha : 0 ≀ a) (hb : 0 ≀ b) : (a + b).to_nat = a.to_nat + b.to_nat := begin lift a to β„• using ha, lift b to β„• using hb, norm_cast, end lemma to_nat_add_nat {a : β„€} (ha : 0 ≀ a) (n : β„•) : (a + n).to_nat = a.to_nat + n := begin lift a to β„• using ha, norm_cast, end @[simp] lemma pred_to_nat : βˆ€ (i : β„€), (i - 1).to_nat = i.to_nat - 1 | (0:β„•) := rfl | (n+1:β„•) := by simp | -[1+ n] := rfl @[simp] lemma to_nat_pred_coe_of_pos {i : β„€} (h : 0 < i) : ((i.to_nat - 1 : β„•) : β„€) = i - 1 := by simp [h, le_of_lt h] with push_cast @[simp] lemma to_nat_sub_to_nat_neg : βˆ€ (n : β„€), ↑n.to_nat - ↑((-n).to_nat) = n | (0 : β„•) := rfl | (n+1 : β„•) := show ↑(n+1) - (0:β„€) = n+1, from sub_zero _ | -[1+ n] := show 0 - (n+1 : β„€) = _, from zero_sub _ @[simp] lemma to_nat_add_to_nat_neg_eq_nat_abs : βˆ€ (n : β„€), (n.to_nat) + ((-n).to_nat) = n.nat_abs | (0 : β„•) := rfl | (n+1 : β„•) := show (n+1) + 0 = n+1, from add_zero _ | -[1+ n] := show 0 + (n+1) = n+1, from zero_add _ /-- If `n : β„•`, then `int.to_nat' n = some n`, if `n : β„€` is negative, then `int.to_nat' n = none`. -/ def to_nat' : β„€ β†’ option β„• | (n : β„•) := some n | -[1+ n] := none theorem mem_to_nat' : βˆ€ (a : β„€) (n : β„•), n ∈ to_nat' a ↔ a = n | (m : β„•) n := option.some_inj.trans coe_nat_inj'.symm | -[1+ m] n := by split; intro h; cases h lemma to_nat_of_nonpos : βˆ€ {z : β„€}, z ≀ 0 β†’ z.to_nat = 0 | (0 : β„•) := Ξ» _, rfl | (n + 1 : β„•) := Ξ» h, (h.not_lt (by { exact_mod_cast nat.succ_pos n })).elim | (-[1+ n]) := Ξ» _, rfl /-! ### units -/ @[simp] theorem units_nat_abs (u : units β„€) : nat_abs u = 1 := units.ext_iff.1 $ nat.units_eq_one ⟨nat_abs u, nat_abs ↑u⁻¹, by rw [← nat_abs_mul, units.mul_inv]; refl, by rw [← nat_abs_mul, units.inv_mul]; refl⟩ theorem units_eq_one_or (u : units β„€) : u = 1 ∨ u = -1 := by simpa only [units.ext_iff, units_nat_abs] using nat_abs_eq u lemma is_unit_eq_one_or {a : β„€} : is_unit a β†’ a = 1 ∨ a = -1 | ⟨x, hx⟩ := hx β–Έ (units_eq_one_or _).imp (congr_arg coe) (congr_arg coe) lemma is_unit_iff {a : β„€} : is_unit a ↔ a = 1 ∨ a = -1 := begin refine ⟨λ h, is_unit_eq_one_or h, Ξ» h, _⟩, rcases h with rfl | rfl, { exact is_unit_one }, { exact is_unit_one.neg } end theorem is_unit_iff_nat_abs_eq {n : β„€} : is_unit n ↔ n.nat_abs = 1 := by simp [nat_abs_eq_iff, is_unit_iff] lemma units_inv_eq_self (u : units β„€) : u⁻¹ = u := (units_eq_one_or u).elim (Ξ» h, h.symm β–Έ rfl) (Ξ» h, h.symm β–Έ rfl) @[simp] lemma units_mul_self (u : units β„€) : u * u = 1 := (units_eq_one_or u).elim (Ξ» h, h.symm β–Έ rfl) (Ξ» h, h.symm β–Έ rfl) -- `units.coe_mul` is a "wrong turn" for the simplifier, this undoes it and simplifies further @[simp] lemma units_coe_mul_self (u : units β„€) : (u * u : β„€) = 1 := by rw [←units.coe_mul, units_mul_self, units.coe_one] @[simp] lemma neg_one_pow_ne_zero {n : β„•} : (-1 : β„€)^n β‰  0 := pow_ne_zero _ (abs_pos.mp trivial) /-! ### bitwise ops -/ @[simp] lemma bodd_zero : bodd 0 = ff := rfl @[simp] lemma bodd_one : bodd 1 = tt := rfl lemma bodd_two : bodd 2 = ff := rfl @[simp, norm_cast] lemma bodd_coe (n : β„•) : int.bodd n = nat.bodd n := rfl @[simp] lemma bodd_sub_nat_nat (m n : β„•) : bodd (sub_nat_nat m n) = bxor m.bodd n.bodd := by apply sub_nat_nat_elim m n (Ξ» m n i, bodd i = bxor m.bodd n.bodd); intros; simp; cases i.bodd; simp @[simp] lemma bodd_neg_of_nat (n : β„•) : bodd (neg_of_nat n) = n.bodd := by cases n; simp; refl @[simp] lemma bodd_neg (n : β„€) : bodd (-n) = bodd n := by cases n; simp [has_neg.neg, int.coe_nat_eq, int.neg, bodd, -of_nat_eq_coe] @[simp] lemma bodd_add (m n : β„€) : bodd (m + n) = bxor (bodd m) (bodd n) := by cases m with m m; cases n with n n; unfold has_add.add; simp [int.add, -of_nat_eq_coe, bool.bxor_comm] @[simp] lemma bodd_mul (m n : β„€) : bodd (m * n) = bodd m && bodd n := by cases m with m m; cases n with n n; simp [← int.mul_def, int.mul, -of_nat_eq_coe, bool.bxor_comm] theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | (n : β„•) := by rw [show (cond (bodd n) 1 0 : β„€) = (cond (bodd n) 1 0 : β„•), by cases bodd n; refl]; exact congr_arg of_nat n.bodd_add_div2 | -[1+ n] := begin refine eq.trans _ (congr_arg neg_succ_of_nat n.bodd_add_div2), dsimp [bodd], cases nat.bodd n; dsimp [cond, bnot, div2, int.mul], { change -[1+ 2 * nat.div2 n] = _, rw zero_add }, { rw [zero_add, add_comm], refl } end theorem div2_val : βˆ€ n, div2 n = n / 2 | (n : β„•) := congr_arg of_nat n.div2_val | -[1+ n] := congr_arg neg_succ_of_nat n.div2_val lemma bit0_val (n : β„€) : bit0 n = 2 * n := (two_mul _).symm lemma bit1_val (n : β„€) : bit1 n = 2 * n + 1 := congr_arg (+(1:β„€)) (bit0_val _) lemma bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by { cases b, apply (bit0_val n).trans (add_zero _).symm, apply bit1_val } lemma bit_decomp (n : β„€) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans $ (add_comm _ _).trans $ bodd_add_div2 _ /-- Defines a function from `β„€` conditionally, if it is defined for odd and even integers separately using `bit`. -/ def {u} bit_cases_on {C : β„€ β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := by rw [← bit_decomp n]; apply h @[simp] lemma bit_zero : bit ff 0 = 0 := rfl @[simp] lemma bit_coe_nat (b) (n : β„•) : bit b n = nat.bit b n := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bit_neg_succ (b) (n : β„•) : bit b -[1+ n] = -[1+ nat.bit (bnot b) n] := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bodd_bit (b n) : bodd (bit b n) = b := by rw bit_val; simp; cases b; cases bodd n; refl @[simp] lemma bodd_bit0 (n : β„€) : bodd (bit0 n) = ff := bodd_bit ff n @[simp] lemma bodd_bit1 (n : β„€) : bodd (bit1 n) = tt := bodd_bit tt n @[simp] lemma div2_bit (b n) : div2 (bit b n) = n := begin rw [bit_val, div2_val, add_comm, int.add_mul_div_left, (_ : (_/2:β„€) = 0), zero_add], cases b, { simp }, { show of_nat _ = _, rw nat.div_eq_zero; simp }, { cc } end lemma bit0_ne_bit1 (m n : β„€) : bit0 m β‰  bit1 n := mt (congr_arg bodd) $ by simp lemma bit1_ne_bit0 (m n : β„€) : bit1 m β‰  bit0 n := (bit0_ne_bit1 _ _).symm lemma bit1_ne_zero (m : β„€) : bit1 m β‰  0 := by simpa only [bit0_zero] using bit1_ne_bit0 m 0 @[simp] lemma test_bit_zero (b) : βˆ€ n, test_bit (bit b n) 0 = b | (n : β„•) := by rw [bit_coe_nat]; apply nat.test_bit_zero | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_zero]; clear test_bit_zero; cases b; refl @[simp] lemma test_bit_succ (m b) : βˆ€ n, test_bit (bit b n) (nat.succ m) = test_bit n m | (n : β„•) := by rw [bit_coe_nat]; apply nat.test_bit_succ | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_succ] private meta def bitwise_tac : tactic unit := `[ funext m, funext n, cases m with m m; cases n with n n; try {refl}, all_goals { apply congr_arg of_nat <|> apply congr_arg neg_succ_of_nat, try {dsimp [nat.land, nat.ldiff, nat.lor]}, try {rw [ show nat.bitwise (Ξ» a b, a && bnot b) n m = nat.bitwise (Ξ» a b, b && bnot a) m n, from congr_fun (congr_fun (@nat.bitwise_swap (Ξ» a b, b && bnot a) rfl) n) m]}, apply congr_arg (Ξ» f, nat.bitwise f m n), funext a, funext b, cases a; cases b; refl }, all_goals {unfold nat.land nat.ldiff nat.lor} ] theorem bitwise_or : bitwise bor = lor := by bitwise_tac theorem bitwise_and : bitwise band = land := by bitwise_tac theorem bitwise_diff : bitwise (Ξ» a b, a && bnot b) = ldiff := by bitwise_tac theorem bitwise_xor : bitwise bxor = lxor := by bitwise_tac @[simp] lemma bitwise_bit (f : bool β†’ bool β†’ bool) (a m b n) : bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n) := begin cases m with m m; cases n with n n; repeat { rw [← int.coe_nat_eq] <|> rw bit_coe_nat <|> rw bit_neg_succ }; unfold bitwise nat_bitwise bnot; [ induction h : f ff ff, induction h : f ff tt, induction h : f tt ff, induction h : f tt tt ], all_goals { unfold cond, rw nat.bitwise_bit, repeat { rw bit_coe_nat <|> rw bit_neg_succ <|> rw bnot_bnot } }, all_goals { unfold bnot {fail_if_unchanged := ff}; rw h; refl } end @[simp] lemma lor_bit (a m b n) : lor (bit a m) (bit b n) = bit (a || b) (lor m n) := by rw [← bitwise_or, bitwise_bit] @[simp] lemma land_bit (a m b n) : land (bit a m) (bit b n) = bit (a && b) (land m n) := by rw [← bitwise_and, bitwise_bit] @[simp] lemma ldiff_bit (a m b n) : ldiff (bit a m) (bit b n) = bit (a && bnot b) (ldiff m n) := by rw [← bitwise_diff, bitwise_bit] @[simp] lemma lxor_bit (a m b n) : lxor (bit a m) (bit b n) = bit (bxor a b) (lxor m n) := by rw [← bitwise_xor, bitwise_bit] @[simp] lemma lnot_bit (b) : βˆ€ n, lnot (bit b n) = bit (bnot b) (lnot n) | (n : β„•) := by simp [lnot] | -[1+ n] := by simp [lnot] @[simp] lemma test_bit_bitwise (f : bool β†’ bool β†’ bool) (m n k) : test_bit (bitwise f m n) k = f (test_bit m k) (test_bit n k) := begin induction k with k IH generalizing m n; apply bit_cases_on m; intros a m'; apply bit_cases_on n; intros b n'; rw bitwise_bit, { simp [test_bit_zero] }, { simp [test_bit_succ, IH] } end @[simp] lemma test_bit_lor (m n k) : test_bit (lor m n) k = test_bit m k || test_bit n k := by rw [← bitwise_or, test_bit_bitwise] @[simp] lemma test_bit_land (m n k) : test_bit (land m n) k = test_bit m k && test_bit n k := by rw [← bitwise_and, test_bit_bitwise] @[simp] lemma test_bit_ldiff (m n k) : test_bit (ldiff m n) k = test_bit m k && bnot (test_bit n k) := by rw [← bitwise_diff, test_bit_bitwise] @[simp] lemma test_bit_lxor (m n k) : test_bit (lxor m n) k = bxor (test_bit m k) (test_bit n k) := by rw [← bitwise_xor, test_bit_bitwise] @[simp] lemma test_bit_lnot : βˆ€ n k, test_bit (lnot n) k = bnot (test_bit n k) | (n : β„•) k := by simp [lnot, test_bit] | -[1+ n] k := by simp [lnot, test_bit] lemma shiftl_add : βˆ€ (m : β„€) (n : β„•) (k : β„€), shiftl m (n + k) = shiftl (shiftl m n) k | (m : β„•) n (k:β„•) := congr_arg of_nat (nat.shiftl_add _ _ _) | -[1+ m] n (k:β„•) := congr_arg neg_succ_of_nat (nat.shiftl'_add _ _ _ _) | (m : β„•) n -[1+k] := sub_nat_nat_elim n k.succ (Ξ» n k i, shiftl ↑m i = nat.shiftr (nat.shiftl m n) k) (Ξ» i n, congr_arg coe $ by rw [← nat.shiftl_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (Ξ» i n, congr_arg coe $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl_sub, nat.sub_self]; refl) | -[1+ m] n -[1+k] := sub_nat_nat_elim n k.succ (Ξ» n k i, shiftl -[1+ m] i = -[1+ nat.shiftr (nat.shiftl' tt m n) k]) (Ξ» i n, congr_arg neg_succ_of_nat $ by rw [← nat.shiftl'_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (Ξ» i n, congr_arg neg_succ_of_nat $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl'_sub, nat.sub_self]; refl) lemma shiftl_sub (m : β„€) (n : β„•) (k : β„€) : shiftl m (n - k) = shiftr (shiftl m n) k := shiftl_add _ _ _ @[simp] lemma shiftl_neg (m n : β„€) : shiftl m (-n) = shiftr m n := rfl @[simp] lemma shiftr_neg (m n : β„€) : shiftr m (-n) = shiftl m n := by rw [← shiftl_neg, neg_neg] @[simp] lemma shiftl_coe_nat (m n : β„•) : shiftl m n = nat.shiftl m n := rfl @[simp] lemma shiftr_coe_nat (m n : β„•) : shiftr m n = nat.shiftr m n := by cases n; refl @[simp] lemma shiftl_neg_succ (m n : β„•) : shiftl -[1+ m] n = -[1+ nat.shiftl' tt m n] := rfl @[simp] lemma shiftr_neg_succ (m n : β„•) : shiftr -[1+ m] n = -[1+ nat.shiftr m n] := by cases n; refl lemma shiftr_add : βˆ€ (m : β„€) (n k : β„•), shiftr m (n + k) = shiftr (shiftr m n) k | (m : β„•) n k := by rw [shiftr_coe_nat, shiftr_coe_nat, ← int.coe_nat_add, shiftr_coe_nat, nat.shiftr_add] | -[1+ m] n k := by rw [shiftr_neg_succ, shiftr_neg_succ, ← int.coe_nat_add, shiftr_neg_succ, nat.shiftr_add] lemma shiftl_eq_mul_pow : βˆ€ (m : β„€) (n : β„•), shiftl m n = m * ↑(2 ^ n) | (m : β„•) n := congr_arg coe (nat.shiftl_eq_mul_pow _ _) | -[1+ m] n := @congr_arg β„• β„€ _ _ (Ξ»i, -i) (nat.shiftl'_tt_eq_mul_pow _ _) lemma shiftr_eq_div_pow : βˆ€ (m : β„€) (n : β„•), shiftr m n = m / ↑(2 ^ n) | (m : β„•) n := by rw shiftr_coe_nat; exact congr_arg coe (nat.shiftr_eq_div_pow _ _) | -[1+ m] n := begin rw [shiftr_neg_succ, neg_succ_of_nat_div, nat.shiftr_eq_div_pow], refl, exact coe_nat_lt_coe_nat_of_lt (pow_pos dec_trivial _) end lemma one_shiftl (n : β„•) : shiftl 1 n = (2 ^ n : β„•) := congr_arg coe (nat.one_shiftl _) @[simp] lemma zero_shiftl : βˆ€ n : β„€, shiftl 0 n = 0 | (n : β„•) := congr_arg coe (nat.zero_shiftl _) | -[1+ n] := congr_arg coe (nat.zero_shiftr _) @[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := zero_shiftl _ /-! ### Least upper bound property for integers -/ /-- A computable version of `exists_least_of_bdd`: given a decidable predicate on the integers, with an explicit lower bound and a proof that it is somewhere true, return the least value for which the predicate is true. -/ def least_of_bdd {P : β„€ β†’ Prop} [decidable_pred P] (b : β„€) (Hb : βˆ€ z : β„€, P z β†’ b ≀ z) (Hinh : βˆƒ z : β„€, P z) : {lb : β„€ // P lb ∧ (βˆ€ z : β„€, P z β†’ lb ≀ z)} := have EX : βˆƒ n : β„•, P (b + n), from let ⟨elt, Helt⟩ := Hinh in match elt, le.dest (Hb _ Helt), Helt with | ._, ⟨n, rfl⟩, Hn := ⟨n, Hn⟩ end, ⟨b + (nat.find EX : β„€), nat.find_spec EX, Ξ» z h, match z, le.dest (Hb _ h), h with | ._, ⟨n, rfl⟩, h := add_le_add_left (int.coe_nat_le.2 $ nat.find_min' _ h) _ end⟩ theorem exists_least_of_bdd {P : β„€ β†’ Prop} (Hbdd : βˆƒ b : β„€, βˆ€ z : β„€, P z β†’ b ≀ z) (Hinh : βˆƒ z : β„€, P z) : βˆƒ lb : β„€, P lb ∧ (βˆ€ z : β„€, P z β†’ lb ≀ z) := by classical; exact let ⟨b, Hb⟩ := Hbdd, ⟨lb, H⟩ := least_of_bdd b Hb Hinh in ⟨lb, H⟩ lemma coe_least_of_bdd_eq {P : β„€ β†’ Prop} [decidable_pred P] {b b' : β„€} (Hb : βˆ€ z : β„€, P z β†’ b ≀ z) (Hb' : βˆ€ z : β„€, P z β†’ b' ≀ z) (Hinh : βˆƒ z : β„€, P z) : (least_of_bdd b Hb Hinh : β„€) = least_of_bdd b' Hb' Hinh := begin rcases least_of_bdd b Hb Hinh with ⟨n, hn, h2n⟩, rcases least_of_bdd b' Hb' Hinh with ⟨n', hn', h2n'⟩, exact le_antisymm (h2n _ hn') (h2n' _ hn), end /-- A computable version of `exists_greatest_of_bdd`: given a decidable predicate on the integers, with an explicit upper bound and a proof that it is somewhere true, return the greatest value for which the predicate is true. -/ def greatest_of_bdd {P : β„€ β†’ Prop} [decidable_pred P] (b : β„€) (Hb : βˆ€ z : β„€, P z β†’ z ≀ b) (Hinh : βˆƒ z : β„€, P z) : {ub : β„€ // P ub ∧ (βˆ€ z : β„€, P z β†’ z ≀ ub)} := have Hbdd' : βˆ€ (z : β„€), P (-z) β†’ -b ≀ z, from Ξ» z h, neg_le.1 (Hb _ h), have Hinh' : βˆƒ z : β„€, P (-z), from let ⟨elt, Helt⟩ := Hinh in ⟨-elt, by rw [neg_neg]; exact Helt⟩, let ⟨lb, Plb, al⟩ := least_of_bdd (-b) Hbdd' Hinh' in ⟨-lb, Plb, Ξ» z h, le_neg.1 $ al _ $ by rwa neg_neg⟩ theorem exists_greatest_of_bdd {P : β„€ β†’ Prop} (Hbdd : βˆƒ b : β„€, βˆ€ z : β„€, P z β†’ z ≀ b) (Hinh : βˆƒ z : β„€, P z) : βˆƒ ub : β„€, P ub ∧ (βˆ€ z : β„€, P z β†’ z ≀ ub) := by classical; exact let ⟨b, Hb⟩ := Hbdd, ⟨lb, H⟩ := greatest_of_bdd b Hb Hinh in ⟨lb, H⟩ lemma coe_greatest_of_bdd_eq {P : β„€ β†’ Prop} [decidable_pred P] {b b' : β„€} (Hb : βˆ€ z : β„€, P z β†’ z ≀ b) (Hb' : βˆ€ z : β„€, P z β†’ z ≀ b') (Hinh : βˆƒ z : β„€, P z) : (greatest_of_bdd b Hb Hinh : β„€) = greatest_of_bdd b' Hb' Hinh := begin rcases greatest_of_bdd b Hb Hinh with ⟨n, hn, h2n⟩, rcases greatest_of_bdd b' Hb' Hinh with ⟨n', hn', h2n'⟩, exact le_antisymm (h2n' _ hn) (h2n _ hn'), end end int attribute [irreducible] int.nonneg
16c11e5b939905de29c3beafced59dfc48f5f3bb
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/theories/finite_group_theory/cyclic.lean
5adf832a7860248a3b9cb00403c71667bda4504d
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
15,826
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Haitao Zhang -/ import data algebra.group algebra.group_power .finsubg .hom .perm open function finset open eq.ops namespace group_theory section cyclic open nat fin list local attribute madd [reducible] variable {A : Type} variable [ambG : group A] include ambG lemma pow_mod {a : A} {n m : nat} : a ^ m = 1 β†’ a ^ n = a ^ (n % m) := assume Pid, have a ^ (n / m * m) = 1, from calc a ^ (n / m * m) = a ^ (m * (n / m)) : by rewrite (mul.comm (n / m) m) ... = (a ^ m) ^ (n / m) : by rewrite pow_mul ... = 1 ^ (n / m) : by rewrite Pid ... = 1 : one_pow (n / m), calc a ^ n = a ^ (n / m * m + n % m) : by rewrite -(eq_div_mul_add_mod n m) ... = a ^ (n / m * m) * a ^ (n % m) : by rewrite pow_add ... = 1 * a ^ (n % m) : by rewrite this ... = a ^ (n % m) : by rewrite one_mul lemma pow_sub_eq_one_of_pow_eq {a : A} {i j : nat} : a^i = a^j β†’ a^(i - j) = 1 := assume Pe, or.elim (lt_or_ge i j) (assume Piltj, begin rewrite [sub_eq_zero_of_le (nat.le_of_lt Piltj)] end) (assume Pigej, begin rewrite [pow_sub a Pigej, Pe, mul.right_inv] end) lemma pow_dist_eq_one_of_pow_eq {a : A} {i j : nat} : a^i = a^j β†’ a^(dist i j) = 1 := assume Pe, or.elim (lt_or_ge i j) (suppose i < j, by rewrite [dist_eq_sub_of_lt this]; exact pow_sub_eq_one_of_pow_eq (eq.symm Pe)) (suppose i β‰₯ j, by rewrite [dist_eq_sub_of_ge this]; exact pow_sub_eq_one_of_pow_eq Pe) lemma pow_madd {a : A} {n : nat} {i j : fin (succ n)} : a^(succ n) = 1 β†’ a^(val (i + j)) = a^i * a^j := assume Pe, calc a^(val (i + j)) = a^((i + j) % (succ n)) : rfl ... = a^(val i + val j) : by rewrite [-pow_mod Pe] ... = a^i * a^j : by rewrite pow_add lemma mk_pow_mod {a : A} {n m : nat} : a ^ (succ m) = 1 β†’ a ^ n = a ^ (mk_mod m n) := assume Pe, pow_mod Pe variable [finA : fintype A] include finA open fintype variable [deceqA : decidable_eq A] include deceqA lemma exists_pow_eq_one (a : A) : βˆƒ n, n < card A ∧ a ^ (succ n) = 1 := let f := (Ξ» i : fin (succ (card A)), a ^ i) in have Pninj : Β¬(injective f), from assume Pinj, absurd (card_le_of_inj _ _ (exists.intro f Pinj)) (begin rewrite [card_fin], apply not_succ_le_self end), obtain i₁ P₁, from exists_not_of_not_forall Pninj, obtain iβ‚‚ Pβ‚‚, from exists_not_of_not_forall P₁, obtain Pfe Pne, from and_not_of_not_implies Pβ‚‚, have Pvne : val i₁ β‰  val iβ‚‚, from assume Pveq, absurd (eq_of_veq Pveq) Pne, exists.intro (pred (dist i₁ iβ‚‚)) (begin rewrite [succ_pred_of_pos (dist_pos_of_ne Pvne)], apply and.intro, apply lt_of_succ_lt_succ, rewrite [succ_pred_of_pos (dist_pos_of_ne Pvne)], apply nat.lt_of_le_of_lt dist_le_max (max_lt i₁ iβ‚‚), apply pow_dist_eq_one_of_pow_eq Pfe end) -- Another possibility is to generate a list of powers and use find to get the first -- unity. -- The bound on bex is arbitrary as long as it is large enough (at least card A). Making -- it larger simplifies some proofs, such as a ∈ cyc a. definition cyc (a : A) : finset A := {x ∈ univ | bex (succ (card A)) (Ξ» n, a ^ n = x)} definition order (a : A) := card (cyc a) definition pow_fin (a : A) (n : nat) (i : fin (order a)) := a ^ (i + n) definition cyc_pow_fin (a : A) (n : nat) : finset A := image (pow_fin a n) univ lemma order_le_group_order {a : A} : order a ≀ card A := card_le_card_of_subset !subset_univ lemma cyc_has_one (a : A) : 1 ∈ cyc a := begin apply mem_sep_of_mem !mem_univ, existsi 0, apply and.intro, apply zero_lt_succ, apply pow_zero end lemma order_pos (a : A) : 0 < order a := length_pos_of_mem (cyc_has_one a) lemma cyc_mul_closed (a : A) : finset_mul_closed_on (cyc a) := take g h, assume Pgin Phin, obtain n Plt Pe, from exists_pow_eq_one a, obtain i Pilt Pig, from of_mem_sep Pgin, obtain j Pjlt Pjh, from of_mem_sep Phin, begin rewrite [-Pig, -Pjh, -pow_add, pow_mod Pe], apply mem_sep_of_mem !mem_univ, existsi ((i + j) % (succ n)), apply and.intro, apply nat.lt_trans (mod_lt (i+j) !zero_lt_succ) (succ_lt_succ Plt), apply rfl end lemma cyc_has_inv (a : A) : finset_has_inv (cyc a) := take g, assume Pgin, obtain n Plt Pe, from exists_pow_eq_one a, obtain i Pilt Pig, from of_mem_sep Pgin, let ni := -(mk_mod n i) in have Pinv : g*a^ni = 1, by rewrite [-Pig, mk_pow_mod Pe, -(pow_madd Pe), add.right_inv], begin rewrite [inv_eq_of_mul_eq_one Pinv], apply mem_sep_of_mem !mem_univ, existsi ni, apply and.intro, apply nat.lt_trans (is_lt ni) (succ_lt_succ Plt), apply rfl end lemma self_mem_cyc (a : A) : a ∈ cyc a := mem_sep_of_mem !mem_univ (exists.intro (1 : nat) (and.intro (succ_lt_succ card_pos) !pow_one)) lemma mem_cyc (a : A) : βˆ€ {n : nat}, a^n ∈ cyc a | 0 := cyc_has_one a | (succ n) := begin rewrite pow_succ', apply cyc_mul_closed a, exact mem_cyc, apply self_mem_cyc end lemma order_le {a : A} {n : nat} : a^(succ n) = 1 β†’ order a ≀ succ n := assume Pe, let s := image (pow_nat a) (upto (succ n)) in have Psub: cyc a βŠ† s, from subset_of_forall (take g, assume Pgin, obtain i Pilt Pig, from of_mem_sep Pgin, begin rewrite [-Pig, pow_mod Pe], apply mem_image, apply mem_upto_of_lt (mod_lt i !zero_lt_succ), exact rfl end), #nat calc order a ≀ card s : card_le_card_of_subset Psub ... ≀ card (upto (succ n)) : !card_image_le ... = succ n : card_upto (succ n) lemma pow_ne_of_lt_order {a : A} {n : nat} : succ n < order a β†’ a^(succ n) β‰  1 := assume Plt, not_imp_not_of_imp order_le (not_le_of_gt Plt) lemma eq_zero_of_pow_eq_one {a : A} : βˆ€ {n : nat}, a^n = 1 β†’ n < order a β†’ n = 0 | 0 := assume Pe Plt, rfl | (succ n) := assume Pe Plt, absurd Pe (pow_ne_of_lt_order Plt) lemma pow_fin_inj (a : A) (n : nat) : injective (pow_fin a n) := take i j : fin (order a), suppose a^(i + n) = a^(j + n), have a^(dist i j) = 1, begin apply !dist_add_add_right β–Έ (pow_dist_eq_one_of_pow_eq this) end, have dist i j = 0, from eq_zero_of_pow_eq_one this (nat.lt_of_le_of_lt dist_le_max (max_lt i j)), eq_of_veq (eq_of_dist_eq_zero this) lemma cyc_eq_cyc (a : A) (n : nat) : cyc_pow_fin a n = cyc a := have Psub : cyc_pow_fin a n βŠ† cyc a, from subset_of_forall (take g, assume Pgin, obtain i Pin Pig, from exists_of_mem_image Pgin, by rewrite [-Pig]; apply mem_cyc), eq_of_card_eq_of_subset (begin apply eq.trans, apply card_image_eq_of_inj_on, rewrite [to_set_univ, -set.injective_iff_inj_on_univ], exact pow_fin_inj a n, rewrite [card_fin] end) Psub lemma pow_order (a : A) : a^(order a) = 1 := obtain i Pin Pone, from exists_of_mem_image (eq.symm (cyc_eq_cyc a 1) β–Έ cyc_has_one a), or.elim (eq_or_lt_of_le (succ_le_of_lt (is_lt i))) (assume P, P β–Έ Pone) (assume P, absurd Pone (pow_ne_of_lt_order P)) lemma eq_one_of_order_eq_one {a : A} : order a = 1 β†’ a = 1 := assume Porder, calc a = a^1 : by rewrite (pow_one a) ... = a^(order a) : by rewrite Porder ... = 1 : by rewrite pow_order lemma order_of_min_pow {a : A} {n : nat} (Pone : a^(succ n) = 1) (Pmin : βˆ€ i, i < n β†’ a^(succ i) β‰  1) : order a = succ n := or.elim (eq_or_lt_of_le (order_le Pone)) (Ξ» P, P) (Ξ» P : order a < succ n, begin have Pn : a^(order a) β‰  1, begin rewrite [-(succ_pred_of_pos (order_pos a))], apply Pmin, apply nat.lt_of_succ_lt_succ, rewrite [succ_pred_of_pos !order_pos], assumption end, exact absurd (pow_order a) Pn end) lemma order_dvd_of_pow_eq_one {a : A} {n : nat} (Pone : a^n = 1) : order a ∣ n := have Pe : a^(n % order a) = 1, from begin revert Pone, rewrite [eq_div_mul_add_mod n (order a) at {1}, pow_add, mul.comm _ (order a), pow_mul, pow_order, one_pow, one_mul], intros, assumption end, dvd_of_mod_eq_zero (eq_zero_of_pow_eq_one Pe (mod_lt n !order_pos)) definition cyc_is_finsubg [instance] (a : A) : is_finsubg (cyc a) := is_finsubg.mk (cyc_has_one a) (cyc_mul_closed a) (cyc_has_inv a) lemma order_dvd_group_order (a : A) : order a ∣ card A := dvd.intro (eq.symm (!mul.comm β–Έ lagrange_theorem (subset_univ (cyc a)))) definition pow_fin' (a : A) (i : fin (succ (pred (order a)))) := pow_nat a i local attribute group_of_add_group [instance] lemma pow_fin_hom (a : A) : homomorphic (pow_fin' a) := take i j : fin (succ (pred (order a))), begin rewrite [↑pow_fin'], apply pow_madd, rewrite [succ_pred_of_pos !order_pos], exact pow_order a end definition pow_fin_is_iso (a : A) : is_iso_class (pow_fin' a) := is_iso_class.mk (pow_fin_hom a) (have H : injective (Ξ» (i : fin (order a)), a ^ (val i + 0)), from pow_fin_inj a 0, begin rewrite [↑pow_fin', succ_pred_of_pos !order_pos]; exact H end) end cyclic section rot open nat list open fin fintype list section local attribute group_of_add_group [instance] lemma pow_eq_mul {n : nat} {i : fin (succ n)} : βˆ€ {k : nat}, i^k = mk_mod n (i*k) | 0 := by rewrite [pow_zero] | (succ k) := begin have Psucc : i^(succ k) = madd (i^k) i, by apply pow_succ', rewrite [Psucc, pow_eq_mul], apply eq_of_veq, rewrite [mul_succ, val_madd, ↑mk_mod, mod_add_mod] end end definition rotl : βˆ€ {n : nat} m : nat, fin n β†’ fin n | 0 := take m i, elim0 i | (succ n) := take m, madd (mk_mod n (n*m)) definition rotr : βˆ€ {n : nat} m : nat, fin n β†’ fin n | 0 := take m i, elim0 i | (succ n) := take m, madd (-(mk_mod n (n*m))) lemma rotl_succ' {n m : nat} : rotl m = madd (mk_mod n (n*m)) := rfl lemma rotl_zero : βˆ€ {n : nat}, @rotl n 0 = id | 0 := funext take i, elim0 i | (nat.succ n) := funext take i, begin rewrite [↑rotl, mul_zero, mk_mod_zero_eq, zero_madd] end lemma rotl_id : βˆ€ {n : nat}, @rotl n n = id | 0 := funext take i, elim0 i | (nat.succ n) := have P : mk_mod n (n * succ n) = mk_mod n 0, from eq_of_veq (by rewrite [↑mk_mod, mul_mod_left]), begin rewrite [rotl_succ', P], apply rotl_zero end lemma rotl_to_zero {n i : nat} : rotl i (mk_mod n i) = 0 := eq_of_veq begin rewrite [↑rotl, val_madd], esimp [mk_mod], rewrite [ mod_add_mod, add_mod_mod, -succ_mul, mul_mod_right] end lemma rotl_compose : βˆ€ {n : nat} {j k : nat}, (@rotl n j) ∘ (rotl k) = rotl (j + k) | 0 := take j k, funext take i, elim0 i | (succ n) := take j k, funext take i, eq.symm begin rewrite [*rotl_succ', left_distrib, -(@madd_mk_mod n (n*j)), madd_assoc], end lemma rotr_rotl : βˆ€ {n : nat} (m : nat) {i : fin n}, rotr m (rotl m i) = i | 0 := take m i, elim0 i | (nat.succ n) := take m i, calc (-(mk_mod n (n*m))) + ((mk_mod n (n*m)) + i) = i : by rewrite neg_add_cancel_left lemma rotl_rotr : βˆ€ {n : nat} (m : nat), (@rotl n m) ∘ (rotr m) = id | 0 := take m, funext take i, elim0 i | (nat.succ n) := take m, funext take i, calc (mk_mod n (n*m)) + (-(mk_mod n (n*m)) + i) = i : add_neg_cancel_left lemma rotl_succ {n : nat} : (rotl 1) ∘ (@succ n) = lift_succ := funext (take i, eq_of_veq (begin rewrite [↑comp, ↑rotl, ↑madd, mul_one n, ↑mk_mod, mod_add_mod, ↑lift_succ, val_succ, -succ_add_eq_succ_add, add_mod_self_left, mod_eq_of_lt (lt.trans (is_lt i) !lt_succ_self), -val_lift] end)) definition list.rotl {A : Type} : βˆ€ l : list A, list A | [] := [] | (a::l) := l++[a] lemma rotl_cons {A : Type} {a : A} {l} : list.rotl (a::l) = l++[a] := rfl lemma rotl_map {A B : Type} {f : A β†’ B} : βˆ€ {l : list A}, list.rotl (map f l) = map f (list.rotl l) | [] := rfl | (a::l) := begin rewrite [map_cons, *rotl_cons, map_append] end lemma rotl_eq_rotl : βˆ€ {n : nat}, map (rotl 1) (upto n) = list.rotl (upto n) | 0 := rfl | (succ n) := begin rewrite [upto_step at {1}, fin.upto_succ, rotl_cons, map_append], congruence, rewrite [map_map], congruence, exact rotl_succ, rewrite [map_singleton], congruence, rewrite [↑rotl, mul_one n, ↑mk_mod, ↑maxi, ↑madd], congruence, rewrite [ mod_add_mod, val_zero, add_zero, mod_eq_of_lt !lt_succ_self ] end definition seq [reducible] (A : Type) (n : nat) := fin n β†’ A variable {A : Type} definition rotl_fun {n : nat} (m : nat) (f : seq A n) : seq A n := f ∘ (rotl m) definition rotr_fun {n : nat} (m : nat) (f : seq A n) : seq A n := f ∘ (rotr m) lemma rotl_seq_zero {n : nat} : rotl_fun 0 = @id (seq A n) := funext take f, begin rewrite [↑rotl_fun, rotl_zero] end lemma rotl_seq_ne_id : βˆ€ {n : nat}, (βˆƒ a b : A, a β‰  b) β†’ βˆ€ i, i < n β†’ rotl_fun (succ i) β‰  (@id (seq A (succ n))) | 0 := assume Pex, take i, assume Piltn, absurd Piltn !not_lt_zero | (nat.succ n) := assume Pex, obtain a b Pne, from Pex, take i, assume Pilt, let f := (Ξ» j : fin (succ (succ n)), if j = 0 then a else b), fi := mk_mod (succ n) (succ i) in have Pfne : rotl_fun (succ i) f fi β‰  f fi, from begin rewrite [↑rotl_fun, rotl_to_zero, mk_mod_of_lt (succ_lt_succ Pilt), if_pos rfl, if_neg mk_succ_ne_zero], assumption end, have P : rotl_fun (succ i) f β‰  f, from assume Peq, absurd (congr_fun Peq fi) Pfne, assume Peq, absurd (congr_fun Peq f) P lemma rotr_rotl_fun {n : nat} (m : nat) (f : seq A n) : rotr_fun m (rotl_fun m f) = f := calc f ∘ (rotl m) ∘ (rotr m) = f ∘ ((rotl m) ∘ (rotr m)) : by rewrite -comp.assoc ... = f ∘ id : by rewrite (rotl_rotr m) lemma rotl_fun_inj {n : nat} {m : nat} : @injective (seq A n) (seq A n) (rotl_fun m) := injective_of_has_left_inverse (exists.intro (rotr_fun m) (rotr_rotl_fun m)) lemma seq_rotl_eq_list_rotl {n : nat} (f : seq A n) : fun_to_list (rotl_fun 1 f) = list.rotl (fun_to_list f) := begin rewrite [↑fun_to_list, ↑rotl_fun, -map_map, rotl_map], congruence, exact rotl_eq_rotl end end rot section rotg open nat fin fintype definition rotl_perm [reducible] (A : Type) [finA : fintype A] [deceqA : decidable_eq A] (n : nat) (m : nat) : perm (seq A n) := perm.mk (rotl_fun m) rotl_fun_inj variable {A : Type} variable [finA : fintype A] variable [deceqA : decidable_eq A] variable {n : nat} include finA deceqA lemma rotl_perm_mul {i j : nat} : (rotl_perm A n i) * (rotl_perm A n j) = rotl_perm A n (j+i) := eq_of_feq (funext take f, calc f ∘ (rotl j) ∘ (rotl i) = f ∘ ((rotl j) ∘ (rotl i)) : by rewrite -comp.assoc ... = f ∘ (rotl (j+i)) : by rewrite rotl_compose) lemma rotl_perm_pow_eq : βˆ€ {i : nat}, (rotl_perm A n 1) ^ i = rotl_perm A n i | 0 := begin rewrite [pow_zero, ↑rotl_perm, perm_one, -eq_iff_feq], esimp, rewrite rotl_seq_zero end | (succ i) := begin rewrite [pow_succ', rotl_perm_pow_eq, rotl_perm_mul, one_add] end lemma rotl_perm_pow_eq_one : (rotl_perm A n 1) ^ n = 1 := eq.trans rotl_perm_pow_eq (eq_of_feq begin esimp [rotl_perm], rewrite [↑rotl_fun, rotl_id] end) lemma rotl_perm_mod {i : nat} : rotl_perm A n i = rotl_perm A n (i % n) := calc rotl_perm A n i = (rotl_perm A n 1) ^ i : by rewrite rotl_perm_pow_eq ... = (rotl_perm A n 1) ^ (i % n) : by rewrite (pow_mod rotl_perm_pow_eq_one) ... = rotl_perm A n (i % n) : by rewrite rotl_perm_pow_eq -- needs A to have at least two elements! lemma rotl_perm_pow_ne_one (Pex : βˆƒ a b : A, a β‰  b) : βˆ€ i, i < n β†’ (rotl_perm A (succ n) 1)^(succ i) β‰  1 := take i, assume Piltn, begin intro P, revert P, rewrite [rotl_perm_pow_eq, -eq_iff_feq, perm_one, *perm.f_mk], intro P, exact absurd P (rotl_seq_ne_id Pex i Piltn) end lemma rotl_perm_order (Pex : βˆƒ a b : A, a β‰  b) : order (rotl_perm A (succ n) 1) = (succ n) := order_of_min_pow rotl_perm_pow_eq_one (rotl_perm_pow_ne_one Pex) end rotg end group_theory
64ff51ba611ba148cac43093d4eb7ed824af677b
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/algebraic_card.lean
bac85cd699ce034b56a1d78820838c89d34a594f
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
3,716
lean
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import data.polynomial.cardinal import ring_theory.algebraic /-! ### Cardinality of algebraic numbers In this file, we prove variants of the following result: the cardinality of algebraic numbers under an R-algebra is at most `# polynomial R * β„΅β‚€`. Although this can be used to prove that real or complex transcendental numbers exist, a more direct proof is given by `liouville.is_transcendental`. -/ universes u v open cardinal polynomial open_locale cardinal namespace algebraic theorem aleph_0_le_cardinal_mk_of_char_zero (R A : Type*) [comm_ring R] [is_domain R] [ring A] [algebra R A] [char_zero A] : β„΅β‚€ ≀ #{x : A // is_algebraic R x} := @mk_le_of_injective (ulift β„•) {x : A | is_algebraic R x} (Ξ» n, ⟨_, is_algebraic_nat n.down⟩) (Ξ» m n hmn, by simpa using hmn) section lift variables (R : Type u) (A : Type v) [comm_ring R] [comm_ring A] [is_domain A] [algebra R A] [no_zero_smul_divisors R A] theorem cardinal_mk_lift_le_mul : cardinal.lift.{u v} (#{x : A // is_algebraic R x}) ≀ cardinal.lift.{v u} (#(polynomial R)) * β„΅β‚€ := begin rw [←mk_ulift, ←mk_ulift], let g : ulift.{u} {x : A | is_algebraic R x} β†’ ulift.{v} (polynomial R) := Ξ» x, ulift.up (classical.some x.1.2), apply cardinal.mk_le_mk_mul_of_mk_preimage_le g (Ξ» f, _), suffices : fintype (g ⁻¹' {f}), { exact @mk_le_aleph_0 _ (@fintype.to_encodable _ this) }, by_cases hf : f.1 = 0, { convert set.fintype_empty, apply set.eq_empty_iff_forall_not_mem.2 (Ξ» x hx, _), simp only [set.mem_preimage, set.mem_singleton_iff] at hx, apply_fun ulift.down at hx, rw hf at hx, exact (classical.some_spec x.1.2).1 hx }, let h : g ⁻¹' {f} β†’ f.down.root_set A := Ξ» x, ⟨x.1.1.1, (mem_root_set_iff hf x.1.1.1).2 begin have key' : g x = f := x.2, simp_rw ← key', exact (classical.some_spec x.1.1.2).2 end⟩, apply fintype.of_injective h (Ξ» _ _ H, _), simp only [subtype.val_eq_coe, subtype.mk_eq_mk] at H, exact subtype.ext (ulift.down_injective (subtype.ext H)) end theorem cardinal_mk_lift_le_max : cardinal.lift.{u v} (#{x : A // is_algebraic R x}) ≀ max (cardinal.lift.{v u} (#R)) β„΅β‚€ := (cardinal_mk_lift_le_mul R A).trans $ (mul_le_mul_right' (lift_le.2 cardinal_mk_le_max) _).trans $ by simp [le_total] theorem cardinal_mk_lift_le_of_infinite [infinite R] : cardinal.lift.{u v} (#{x : A // is_algebraic R x}) ≀ cardinal.lift.{v u} (#R) := (cardinal_mk_lift_le_max R A).trans $ by simp variable [encodable R] @[simp] theorem countable_of_encodable : set.countable {x : A | is_algebraic R x} := begin rw [←mk_set_le_aleph_0, ←lift_le], apply (cardinal_mk_lift_le_max R A).trans, simp end @[simp] theorem cardinal_mk_of_encodable_of_char_zero [char_zero A] [is_domain R] : #{x : A // is_algebraic R x} = β„΅β‚€ := le_antisymm (by simp) (aleph_0_le_cardinal_mk_of_char_zero R A) end lift section non_lift variables (R A : Type u) [comm_ring R] [comm_ring A] [is_domain A] [algebra R A] [no_zero_smul_divisors R A] theorem cardinal_mk_le_mul : #{x : A // is_algebraic R x} ≀ #(polynomial R) * β„΅β‚€ := by { rw [←lift_id (#_), ←lift_id (#(polynomial R))], exact cardinal_mk_lift_le_mul R A } theorem cardinal_mk_le_max : #{x : A // is_algebraic R x} ≀ max (#R) β„΅β‚€ := by { rw [←lift_id (#_), ←lift_id (#R)], exact cardinal_mk_lift_le_max R A } theorem cardinal_mk_le_of_infinite [infinite R] : #{x : A // is_algebraic R x} ≀ #R := (cardinal_mk_le_max R A).trans $ by simp end non_lift end algebraic
37f1077227581e3857744c248b279afb0b1136e6
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/calculus/fderiv_measurable.lean
5ea4d5a2c2a2fef67a38d87ac555b4917630ea0f
[]
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
11,517
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, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.calculus.deriv import Mathlib.measure_theory.borel_space import Mathlib.PostPort universes u_1 u_2 u_3 namespace Mathlib /-! # Derivative is measurable In this file we prove that the derivative of any function with complete codomain is a measurable function. Namely, we prove: * `is_measurable_set_of_differentiable_at`: the set `{x | differentiable_at π•œ f x}` is measurable; * `measurable_fderiv`: the function `fderiv π•œ f` is measurable; * `measurable_fderiv_apply_const`: for a fixed vector `y`, the function `Ξ» x, fderiv π•œ f x y` is measurable; * `measurable_deriv`: the function `deriv f` is measurable (for `f : π•œ β†’ F`). ## Implementation We give a proof that avoids second-countability issues, by expressing the differentiability set as a function of open sets in the following way. Define `A (L, r, Ξ΅)` to be the set of points where, on a ball of radius roughly `r` around `x`, the function is uniformly approximated by the linear map `L`, up to `Ξ΅ r`. It is an open set. Let also `B (L, r, s, Ξ΅) = A (L, r, Ξ΅) ∩ A (L, s, Ξ΅)`: we require that at two possibly different scales `r` and `s`, the function is well approximated by the linear map `L`. It is also open. We claim that the differentiability set of `f` is exactly `D = β‹‚ Ξ΅ > 0, ⋃ Ξ΄ > 0, β‹‚ r, s < Ξ΄, ⋃ L, B (L, r, s, Ξ΅)`. In other words, for any `Ξ΅ > 0`, we require that there is a size `Ξ΄` such that, for any two scales below this size, the function is well approximated by a linear map, common to the two scales. The set `⋃ L, B (L, r, s, Ξ΅)` is open, as a union of open sets. Converting the intersections and unions to countable ones (using real numbers of the form `2 ^ (-n)`), it follows that the differentiability set is measurable. To prove the claim, there are two inclusions. One is trivial: if the function is differentiable at `x`, then `x` belongs to `D` (just take `L` to be the derivative, and use that the differentiability exactly says that the map is well approximated by `L`). This is proved in `mem_A_of_differentiable` and `differentiable_set_subset_D`. For the other direction, the difficulty is that `L` in the union may depend on `Ξ΅, r, s`. The key point is that, in fact, it doesn't depend too much on them. First, if `x` belongs both to `A (L, r, Ξ΅)` and `A (L', r, Ξ΅)`, then `L` and `L'` have to be close on a shell, and thus `βˆ₯L - L'βˆ₯` is bounded by `Ξ΅` (see `norm_sub_le_of_mem_A`). Assume now `x ∈ D`. If one has two maps `L` and `L'` such that `x` belongs to `A (L, r, Ξ΅)` and to `A (L', r', Ξ΅')`, one deduces that `L` is close to `L'` by arguing as follows. Consider another scale `s` smaller than `r` and `r'`. Take a linear map `L₁` that approximates `f` around `x` both at scales `r` and `s` w.r.t. `Ξ΅` (it exists as `x` belongs to `D`). Take also `Lβ‚‚` that approximates `f` around `x` both at scales `r'` and `s` w.r.t. `Ξ΅'`. Then `L₁` is close to `L` (as they are close on a shell of radius `r`), and `Lβ‚‚` is close to `L₁` (as they are close on a shell of radius `s`), and `L'` is close to `Lβ‚‚` (as they are close on a shell of radius `r'`). It follows that `L` is close to `L'`, as we claimed. It follows that the different approximating linear maps that show up form a Cauchy sequence when `Ξ΅` tends to `0`. When the target space is complete, this sequence converges, to a limit `f'`. With the same kind of arguments, one checks that `f` is differentiable with derivative `f'`. To show that the derivative itself is measurable, add in the definition of `B` and `D` a set `K` of continuous linear maps to which `L` should belong. Then, when `K` is complete, the set `D K` is exactly the set of points where `f` is differentiable with a derivative in `K`. ## Tags derivative, measurable function, Borel Οƒ-algebra -/ namespace continuous_linear_map protected instance measurable_space {π•œ : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π•œ] [normed_group E] [normed_space π•œ E] [normed_group F] [normed_space π•œ F] : measurable_space (continuous_linear_map π•œ E F) := borel (continuous_linear_map π•œ E F) protected instance borel_space {π•œ : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π•œ] [normed_group E] [normed_space π•œ E] [normed_group F] [normed_space π•œ F] : borel_space (continuous_linear_map π•œ E F) := borel_space.mk rfl theorem measurable_apply {π•œ : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π•œ] [normed_group E] [normed_space π•œ E] [normed_group F] [normed_space π•œ F] [measurable_space F] [borel_space F] (x : E) : measurable fun (f : continuous_linear_map π•œ E F) => coe_fn f x := continuous.measurable (continuous_linear_map.continuous (apply π•œ F x)) theorem measurable_apply' {π•œ : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π•œ] [normed_group E] [normed_space π•œ E] [normed_group F] [normed_space π•œ F] [measurable_space E] [opens_measurable_space E] [measurable_space F] [borel_space F] : measurable fun (x : E) (f : continuous_linear_map π•œ E F) => coe_fn f x := measurable_pi_lambda (fun (x : E) (f : continuous_linear_map π•œ E F) => coe_fn f x) fun (f : continuous_linear_map π•œ E F) => continuous_linear_map.measurable f theorem measurable_applyβ‚‚ {π•œ : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π•œ] [normed_group E] [normed_space π•œ E] [normed_group F] [normed_space π•œ F] [measurable_space E] [opens_measurable_space E] [topological_space.second_countable_topology E] [topological_space.second_countable_topology (continuous_linear_map π•œ E F)] [measurable_space F] [borel_space F] : measurable fun (p : continuous_linear_map π•œ E F Γ— E) => coe_fn (prod.fst p) (prod.snd p) := continuous.measurable (is_bounded_bilinear_map.continuous is_bounded_bilinear_map_apply) theorem measurable_coe {π•œ : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π•œ] [normed_group E] [normed_space π•œ E] [normed_group F] [normed_space π•œ F] [measurable_space F] [borel_space F] : measurable fun (f : continuous_linear_map π•œ E F) (x : E) => coe_fn f x := measurable_pi_lambda (fun (f : continuous_linear_map π•œ E F) (x : E) => coe_fn f x) measurable_apply end continuous_linear_map namespace fderiv_measurable_aux /-- The set `A f L r Ξ΅` is the set of points `x` around which the function `f` is well approximated at scale `r` by the linear map `L`, up to an error `Ξ΅`. We tweak the definition to make sure that this is an open set.-/ def A {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {E : Type u_2} [normed_group E] [normed_space π•œ E] {F : Type u_3} [normed_group F] [normed_space π•œ F] (f : E β†’ F) (L : continuous_linear_map π•œ E F) (r : ℝ) (Ξ΅ : ℝ) : set E := set_of fun (x : E) => βˆƒ (r' : ℝ), βˆƒ (H : r' ∈ set.Ioc (r / bit0 1) r), βˆ€ (y z : E), y ∈ metric.ball x r' β†’ z ∈ metric.ball x r' β†’ norm (f z - f y - coe_fn L (z - y)) ≀ Ξ΅ * r /-- The set `B f K r s Ξ΅` is the set of points `x` around which there exists a continuous linear map `L` belonging to `K` (a given set of continuous linear maps) that approximates well the function `f` (up to an error `Ξ΅`), simultaneously at scales `r` and `s`. -/ def B {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {E : Type u_2} [normed_group E] [normed_space π•œ E] {F : Type u_3} [normed_group F] [normed_space π•œ F] (f : E β†’ F) (K : set (continuous_linear_map π•œ E F)) (r : ℝ) (s : ℝ) (Ξ΅ : ℝ) : set E := set.Union fun (L : continuous_linear_map π•œ E F) => set.Union fun (H : L ∈ K) => A f L r Ξ΅ ∩ A f L s Ξ΅ /-- The set `D f K` is a complicated set constructed using countable intersections and unions. Its main use is that, when `K` is complete, it is exactly the set of points where `f` is differentiable, with a derivative in `K`. -/ def D {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {E : Type u_2} [normed_group E] [normed_space π•œ E] {F : Type u_3} [normed_group F] [normed_space π•œ F] (f : E β†’ F) (K : set (continuous_linear_map π•œ E F)) : set E := set.Inter fun (e : β„•) => set.Union fun (n : β„•) => set.Inter fun (p : β„•) => set.Inter fun (H : p β‰₯ n) => set.Inter fun (q : β„•) => set.Inter fun (H : q β‰₯ n) => B f K ((1 / bit0 1) ^ p) ((1 / bit0 1) ^ q) ((1 / bit0 1) ^ e) theorem is_open_A {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {E : Type u_2} [normed_group E] [normed_space π•œ E] {F : Type u_3} [normed_group F] [normed_space π•œ F] {f : E β†’ F} (L : continuous_linear_map π•œ E F) (r : ℝ) (Ξ΅ : ℝ) : is_open (A f L r Ξ΅) := sorry theorem is_open_B {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {E : Type u_2} [normed_group E] [normed_space π•œ E] {F : Type u_3} [normed_group F] [normed_space π•œ F] {f : E β†’ F} {K : set (continuous_linear_map π•œ E F)} {r : ℝ} {s : ℝ} {Ξ΅ : ℝ} : is_open (B f K r s Ξ΅) := sorry theorem A_mono {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {E : Type u_2} [normed_group E] [normed_space π•œ E] {F : Type u_3} [normed_group F] [normed_space π•œ F] {f : E β†’ F} (L : continuous_linear_map π•œ E F) (r : ℝ) {Ξ΅ : ℝ} {Ξ΄ : ℝ} (h : Ξ΅ ≀ Ξ΄) : A f L r Ξ΅ βŠ† A f L r Ξ΄ := sorry theorem le_of_mem_A {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {E : Type u_2} [normed_group E] [normed_space π•œ E] {F : Type u_3} [normed_group F] [normed_space π•œ F] {f : E β†’ F} {r : ℝ} {Ξ΅ : ℝ} {L : continuous_linear_map π•œ E F} {x : E} (hx : x ∈ A f L r Ξ΅) {y : E} {z : E} (hy : y ∈ metric.closed_ball x (r / bit0 1)) (hz : z ∈ metric.closed_ball x (r / bit0 1)) : norm (f z - f y - coe_fn L (z - y)) ≀ Ξ΅ * r := sorry theorem mem_A_of_differentiable {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {E : Type u_2} [normed_group E] [normed_space π•œ E] {F : Type u_3} [normed_group F] [normed_space π•œ F] {f : E β†’ F} {Ξ΅ : ℝ} (hΞ΅ : 0 < Ξ΅) {x : E} (hx : differentiable_at π•œ f x) : βˆƒ (R : ℝ), βˆƒ (H : R > 0), βˆ€ (r : ℝ), r ∈ set.Ioo 0 R β†’ x ∈ A f (fderiv π•œ f x) r Ξ΅ := sorry theorem norm_sub_le_of_mem_A {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {E : Type u_2} [normed_group E] [normed_space π•œ E] {F : Type u_3} [normed_group F] [normed_space π•œ F] {f : E β†’ F} {c : π•œ} (hc : 1 < norm c) {r : ℝ} {Ξ΅ : ℝ} (hΞ΅ : 0 < Ξ΅) (hr : 0 < r) {x : E} {L₁ : continuous_linear_map π•œ E F} {Lβ‚‚ : continuous_linear_map π•œ E F} (h₁ : x ∈ A f L₁ r Ξ΅) (hβ‚‚ : x ∈ A f Lβ‚‚ r Ξ΅) : norm (L₁ - Lβ‚‚) ≀ bit0 (bit0 1) * norm c * Ξ΅ := sorry /-- Easy inclusion: a differentiability point with derivative in `K` belongs to `D f K`. -/ theorem differentiable_set_subset_D {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {E : Type u_2} [normed_group E] [normed_space π•œ E] {F : Type u_3} [normed_group F] [normed_space π•œ F] {f : E β†’ F} (K : set (continuous_linear_map π•œ E F)) : (set_of fun (x : E) => differentiable_at π•œ f x ∧ fderiv π•œ f x ∈ K) βŠ† D f K := sorry /-- Harder inclusion: at a point in `D f K`, the function `f` has a derivative, in `K`. -/
b9cc6d8939cff2f936199d1d2f3895fc6aa7f485
92c6b42948d74fe325c2d88530f1d36da388b2f7
/src/cvc4/sig/sat.lean
dbeba9db72000e3ca416651fc862f8f1c431bf88
[ "MIT" ]
permissive
riaqn/smtlean
8ad65055b6c1600cd03b9e345059a3b24419b6d5
c11768cfb43cd634340b552f5039cba094701a87
refs/heads/master
1,584,569,627,940
1,535,314,713,000
1,535,314,713,000
135,333,334
0
1
null
null
null
null
UTF-8
Lean
false
false
213
lean
namespace sig def bool := bool def tt := bool.tt def ff := bool.ff def satlem (Ξ± Ξ² : Prop) (x : Ξ±) (y : Ξ± β†’ Ξ²) : Ξ² := y x def satlem_simplify (Ξ± Ξ² : Prop) (x : Ξ±) (y : Ξ± β†’ Ξ²) : Ξ² := y x end sig
cff3ae880f5fb4c5cd68b59693d99fef705ba431
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/hierarchy_design.lean
6f4e04c9b95ea3cd426ec414331d5f1083cd100d
[ "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
9,132
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Eric Weiser -/ import tactic.doc_commands /-! # Documentation of the algebraic hierarchy A library note giving advice on modifying the algebraic hierarchy. (It is not intended as a "tour".) TODO: Add sections about interactions with topological typeclasses, and order typeclasses. -/ /-- # The algebraic hierarchy In any theorem proving environment, there are difficult decisions surrounding the design of the "algebraic hierarchy". There is a danger of exponential explosion in the number of gadgets, especially once interactions between algebraic and order/topological/etc structures are considered. In mathlib, we try to avoid this by only introducing new algebraic typeclasses either 1. when there is "real mathematics" to be done with them, or 2. when there is a meaninful gain in simplicity by factoring out a common substructure. (As examples, at this point we don't have `loop`, or `unital_magma`, but we do have `lie_submodule` and `topological_field`! We also have `group_with_zero`, as an exemplar of point 2.) Generally in mathlib we use the extension mechanism (so `comm_ring` extends `ring`) rather than mixins (e.g. with separate `ring` and `comm_mul` classes), in part because of the potential blow-up in term sizes described at https://www.ralfj.de/blog/2019/05/15/typeclasses-exponential-blowup.html However there is tension here, as it results in considerable duplication in the API, particularly in the interaction with order structures. This library note is not intended as a design document justifying and explaining the history of mathlib's algebraic hierarchy! Instead it is intended as a developer's guide, for contributors wanting to extend (either new leaves, or new intermediate classes) the algebraic hierarchy as it exists. (Ideally we would have both a tour guide to the existing hierarchy, and an account of the design choices. See https://arxiv.org/abs/1910.09336 for an overview of mathlib as a whole, with some attention to the algebraic hierarchy and https://leanprover-community.github.io/mathlib-overview.html for a summary of what is in mathlib today.) ## Instances When adding a new typeclass `Z` to the algebraic hierarchy one should attempt to add the following constructions and results, when applicable: * Instances transferred elementwise to products, like `prod.monoid`. See `algebra.group.prod` for more examples. ``` instance prod.Z [Z M] [Z N] : Z (M Γ— N) := ... ``` * Instances transferred elementwise to pi types, like `pi.monoid`. See `algebra.group.pi` for more examples. ``` instance pi.Z [βˆ€ i, Z $ f i] : Z (Ξ  i : I, f i) := ... ``` * Instances transferred to `mul_opposite M`, like `mul_opposite.monoid`. See `algebra.opposites` for more examples. ``` instance mul_opposite.Z [Z M] : Z (mul_opposite M) := ... ``` * Instances transferred to `ulift M`, like `ulift.monoid`. See `algebra.group.ulift` for more examples. ``` instance ulift.Z [Z M] : Z (ulift M) := ... ``` * Definitions for transferring the proof fields of instances along injective or surjective functions that agree on the data fields, like `function.injective.monoid` and `function.surjective.monoid`. We make these definitions `@[reducible]`, see note [reducible non-instances]. See `algebra.group.inj_surj` for more examples. ``` @[reducible] def function.injective.Z [Z Mβ‚‚] (f : M₁ β†’ Mβ‚‚) (hf : injective f) (one : f 1 = 1) (mul : βˆ€ x y, f (x * y) = f x * f y) : Z M₁ := ... @[reducible] def function.surjective.Z [Z M₁] (f : M₁ β†’ Mβ‚‚) (hf : surjective f) (one : f 1 = 1) (mul : βˆ€ x y, f (x * y) = f x * f y) : Z Mβ‚‚ := ... ``` * Instances transferred elementwise to `finsupp`s, like `finsupp.semigroup`. See `data.finsupp.pointwise` for more examples. ``` instance finsupp.Z [Z Ξ²] : Z (Ξ± β†’β‚€ Ξ²) := ... ``` * Instances transferred elementwise to `set`s, like `set.monoid`. See `algebra.pointwise` for more examples. ``` instance set.Z [Z Ξ±] : Z (set Ξ±) := ... ``` * Definitions for transferring the entire structure across an equivalence, like `equiv.monoid`. See `data.equiv.transfer_instance` for more examples. See also the `transport` tactic. ``` def equiv.Z (e : Ξ± ≃ Ξ²) [Z Ξ²] : Z Ξ± := ... /- When there is a new notion of `Z`-equiv: -/ def equiv.Z_equiv (e : Ξ± ≃ Ξ²) [Z Ξ²] : by { letI := equiv.Z e, exact Ξ± ≃Z Ξ² } := ... ``` ## Subobjects When a new typeclass `Z` adds new data fields, you should also create a new `sub_Z` `structure` with a `carrier` field. This can be a lot of work; for now try to closely follow the existing examples (e.g. `submonoid`, `subring`, `subalgebra`). We would very much like to provide some automation here, but a prerequisite will be making all the existing APIs more uniform. If `Z` extends `Y`, then `sub_Z` should usually extend `sub_Y`. When `Z` adds only new proof fields to an existing structure `Y`, you should provide instances transferring `Z Ξ±` to `Z (sub_Y Ξ±)`, like `submonoid.to_comm_monoid`. Typically this is done using the `function.injective.Z` definition mentioned above. ``` instance sub_Y.to_Z [Z Ξ±] : Z (sub_Y Ξ±) := coe_injective.Z coe ... ``` ## Morphisms and equivalences ## Category theory For many algebraic structures, particularly ones used in representation theory, algebraic geometry, etc., we also define "bundled" versions, which carry `category` instances. These bundled versions are usually named in camel case, so for example we have `AddCommGroup` as a bundled `add_comm_group`, and `TopCommRing` (which bundles together `comm_ring`, `topological_space`, and `topological_ring`). These bundled versions have many appealing features: * a uniform notation for morphisms `X ⟢ Y` * a uniform notation (and definition) for isomorphisms `X β‰… Y` * a uniform API for subobjects, via the partial order `subobject X` * interoperability with unbundled structures, via coercions to `Type` (so if `G : AddCommGroup`, you can treat `G` as a type, and it automatically has an `add_comm_group` instance) and lifting maps `AddCommGroup.of G`, when `G` is a type with an `add_comm_group` instance. If, for example you do the work of proving that a typeclass `Z` has a good notion of tensor product, you are strongly encouraged to provide the corresponding `monoidal_category` instance on a bundled version. This ensures that the API for tensor products is complete, and enables use of general machinery. Similarly if you prove universal properties, or adjunctions, you are encouraged to state these using categorical language! One disadvantage of the bundled approach is that we can only speak of morphisms between objects living in the same type-theoretic universe. In practice this is rarely a problem. # Making a pull request With so many moving parts, how do you actually go about changing the algebraic hierarchy? We're still evolving how to handle this, but the current suggestion is: * If you're adding a new "leaf" class, the requirements are lower, and an initial PR can just add whatever is immediately needed. * A new "intermediate" class, especially low down in the hierarchy, needs to be careful about leaving gaps. In a perfect world, there would be a group of simultaneous PRs that basically cover everything! (Or at least an expectation that PRs may not be merged immediately while waiting on other PRs that fill out the API.) However "perfect is the enemy of good", and it would also be completely reasonable to add a TODO list in the main module doc-string for the new class, briefly listing the parts of the API which still need to be provided. Hopefully this document makes it easy to assemble this list. Another alternative to a TODO list in the doc-strings is adding github issues. -/ library_note "the algebraic hierarchy" /-- Some definitions that define objects of a class cannot be instances, because they have an explicit argument that does not occur in the conclusion. An example is `preorder.lift` that has a function `f : Ξ± β†’ Ξ²` as an explicit argument to lift a preorder on `Ξ²` to a preorder on `Ξ±`. If these definitions are used to define instances of this class *and* this class is an argument to some other type-class so that type-class inference will have to unfold these instances to check for definitional equality, then these definitions should be marked `@[reducible]`. For example, `preorder.lift` is used to define `units.preorder` and `partial_order.lift` is used to define `units.partial_order`. In some cases it is important that type-class inference can recognize that `units.preorder` and `units.partial_order` give rise to the same `has_le` instance. For example, you might have another class that takes `[has_le Ξ±]` as an argument, and this argument sometimes comes from `units.preorder` and sometimes from `units.partial_order`. Therefore, `preorder.lift` and `partial_order.lift` are marked `@[reducible]`. -/ library_note "reducible non-instances"
b55503e1e30a717b1c4821b0278a5da888d52cb3
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/equiv/transfer_instance.lean
0c857f0699152295ac8d9d6258080d59fe039c80
[ "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
13,113
lean
/- Copyright (c) 2018 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl -/ import data.equiv.basic import algebra.field import algebra.module import algebra.algebra.basic import algebra.group.type_tags import ring_theory.ideal.local_ring /-! # Transfer algebraic structures across `equiv`s In this file we prove theorems of the following form: if `Ξ²` has a group structure and `Ξ± ≃ Ξ²` then `Ξ±` has a group structure, and similarly for monoids, semigroups, rings, integral domains, fields and so on. Note that most of these constructions can also be obtained using the `transport` tactic. ## Tags equiv, group, ring, field, module, algebra -/ universes u v variables {Ξ± : Type u} {Ξ² : Type v} namespace equiv section instances variables (e : Ξ± ≃ Ξ²) /-- Transfer `has_one` across an `equiv` -/ @[to_additive "Transfer `has_zero` across an `equiv`"] protected def has_one [has_one Ξ²] : has_one Ξ± := ⟨e.symm 1⟩ @[to_additive] lemma one_def [has_one Ξ²] : @has_one.one _ (equiv.has_one e) = e.symm 1 := rfl /-- Transfer `has_mul` across an `equiv` -/ @[to_additive "Transfer `has_add` across an `equiv`"] protected def has_mul [has_mul Ξ²] : has_mul Ξ± := ⟨λ x y, e.symm (e x * e y)⟩ @[to_additive] lemma mul_def [has_mul Ξ²] (x y : Ξ±) : @has_mul.mul _ (equiv.has_mul e) x y = e.symm (e x * e y) := rfl /-- Transfer `has_div` across an `equiv` -/ @[to_additive "Transfer `has_sub` across an `equiv`"] protected def has_div [has_div Ξ²] : has_div Ξ± := ⟨λ x y, e.symm (e x / e y)⟩ @[to_additive] lemma div_def [has_div Ξ²] (x y : Ξ±) : @has_div.div _ (equiv.has_div e) x y = e.symm (e x / e y) := rfl /-- Transfer `has_inv` across an `equiv` -/ @[to_additive "Transfer `has_neg` across an `equiv`"] protected def has_inv [has_inv Ξ²] : has_inv Ξ± := ⟨λ x, e.symm (e x)⁻¹⟩ @[to_additive] lemma inv_def [has_inv Ξ²] (x : Ξ±) : @has_inv.inv _ (equiv.has_inv e) x = e.symm (e x)⁻¹ := rfl /-- Transfer `has_scalar` across an `equiv` -/ protected def has_scalar {R : Type*} [has_scalar R Ξ²] : has_scalar R Ξ± := ⟨λ r x, e.symm (r β€’ (e x))⟩ lemma smul_def {R : Type*} [has_scalar R Ξ²] (r : R) (x : Ξ±) : @has_scalar.smul _ _ (equiv.has_scalar e) r x = e.symm (r β€’ (e x)) := rfl /-- An equivalence `e : Ξ± ≃ Ξ²` gives a multiplicative equivalence `Ξ± ≃* Ξ²` where the multiplicative structure on `Ξ±` is the one obtained by transporting a multiplicative structure on `Ξ²` back along `e`. -/ @[to_additive "An equivalence `e : Ξ± ≃ Ξ²` gives a additive equivalence `Ξ± ≃+ Ξ²` where the additive structure on `Ξ±` is the one obtained by transporting an additive structure on `Ξ²` back along `e`."] def mul_equiv (e : Ξ± ≃ Ξ²) [has_mul Ξ²] : by { letI := equiv.has_mul e, exact Ξ± ≃* Ξ² } := begin introsI, exact { map_mul' := Ξ» x y, by { apply e.symm.injective, simp, refl, }, ..e } end @[simp, to_additive] lemma mul_equiv_apply (e : Ξ± ≃ Ξ²) [has_mul Ξ²] (a : Ξ±) : (mul_equiv e) a = e a := rfl @[to_additive] lemma mul_equiv_symm_apply (e : Ξ± ≃ Ξ²) [has_mul Ξ²] (b : Ξ²) : by { letI := equiv.has_mul e, exact (mul_equiv e).symm b = e.symm b } := begin intros, refl, end /-- An equivalence `e : Ξ± ≃ Ξ²` gives a ring equivalence `Ξ± ≃+* Ξ²` where the ring structure on `Ξ±` is the one obtained by transporting a ring structure on `Ξ²` back along `e`. -/ def ring_equiv (e : Ξ± ≃ Ξ²) [has_add Ξ²] [has_mul Ξ²] : by { letI := equiv.has_add e, letI := equiv.has_mul e, exact Ξ± ≃+* Ξ² } := begin introsI, exact { map_add' := Ξ» x y, by { apply e.symm.injective, simp, refl, }, map_mul' := Ξ» x y, by { apply e.symm.injective, simp, refl, }, ..e } end @[simp] lemma ring_equiv_apply (e : Ξ± ≃ Ξ²) [has_add Ξ²] [has_mul Ξ²] (a : Ξ±) : (ring_equiv e) a = e a := rfl lemma ring_equiv_symm_apply (e : Ξ± ≃ Ξ²) [has_add Ξ²] [has_mul Ξ²] (b : Ξ²) : by { letI := equiv.has_add e, letI := equiv.has_mul e, exact (ring_equiv e).symm b = e.symm b } := begin intros, refl, end /-- Transfer `semigroup` across an `equiv` -/ @[to_additive "Transfer `add_semigroup` across an `equiv`"] protected def semigroup [semigroup Ξ²] : semigroup Ξ± := let mul := e.has_mul in by resetI; apply e.injective.semigroup _; intros; exact e.apply_symm_apply _ /-- Transfer `semigroup_with_zero` across an `equiv` -/ protected def semigroup_with_zero [semigroup_with_zero Ξ²] : semigroup_with_zero Ξ± := let mul := e.has_mul, zero := e.has_zero in by resetI; apply e.injective.semigroup_with_zero _; intros; exact e.apply_symm_apply _ /-- Transfer `comm_semigroup` across an `equiv` -/ @[to_additive "Transfer `add_comm_semigroup` across an `equiv`"] protected def comm_semigroup [comm_semigroup Ξ²] : comm_semigroup Ξ± := let mul := e.has_mul in by resetI; apply e.injective.comm_semigroup _; intros; exact e.apply_symm_apply _ /-- Transfer `mul_zero_class` across an `equiv` -/ protected def mul_zero_class [mul_zero_class Ξ²] : mul_zero_class Ξ± := let zero := e.has_zero, mul := e.has_mul in by resetI; apply e.injective.mul_zero_class _; intros; exact e.apply_symm_apply _ /-- Transfer `mul_one_class` across an `equiv` -/ @[to_additive "Transfer `add_zero_class` across an `equiv`"] protected def mul_one_class [mul_one_class Ξ²] : mul_one_class Ξ± := let one := e.has_one, mul := e.has_mul in by resetI; apply e.injective.mul_one_class _; intros; exact e.apply_symm_apply _ /-- Transfer `mul_zero_one_class` across an `equiv` -/ protected def mul_zero_one_class [mul_zero_one_class Ξ²] : mul_zero_one_class Ξ± := let zero := e.has_zero, one := e.has_one,mul := e.has_mul in by resetI; apply e.injective.mul_zero_one_class _; intros; exact e.apply_symm_apply _ /-- Transfer `monoid` across an `equiv` -/ @[to_additive "Transfer `add_monoid` across an `equiv`"] protected def monoid [monoid Ξ²] : monoid Ξ± := let one := e.has_one, mul := e.has_mul in by resetI; apply e.injective.monoid _; intros; exact e.apply_symm_apply _ /-- Transfer `comm_monoid` across an `equiv` -/ @[to_additive "Transfer `add_comm_monoid` across an `equiv`"] protected def comm_monoid [comm_monoid Ξ²] : comm_monoid Ξ± := let one := e.has_one, mul := e.has_mul in by resetI; apply e.injective.comm_monoid _; intros; exact e.apply_symm_apply _ /-- Transfer `group` across an `equiv` -/ @[to_additive "Transfer `add_group` across an `equiv`"] protected def group [group Ξ²] : group Ξ± := let one := e.has_one, mul := e.has_mul, inv := e.has_inv, div := e.has_div in by resetI; apply e.injective.group _; intros; exact e.apply_symm_apply _ /-- Transfer `comm_group` across an `equiv` -/ @[to_additive "Transfer `add_comm_group` across an `equiv`"] protected def comm_group [comm_group Ξ²] : comm_group Ξ± := let one := e.has_one, mul := e.has_mul, inv := e.has_inv, div := e.has_div in by resetI; apply e.injective.comm_group _; intros; exact e.apply_symm_apply _ /-- Transfer `non_unital_non_assoc_semiring` across an `equiv` -/ protected def non_unital_non_assoc_semiring [non_unital_non_assoc_semiring Ξ²] : non_unital_non_assoc_semiring Ξ± := let zero := e.has_zero, add := e.has_add, mul := e.has_mul in by resetI; apply e.injective.non_unital_non_assoc_semiring _; intros; exact e.apply_symm_apply _ /-- Transfer `non_unital_semiring` across an `equiv` -/ protected def non_unital_semiring [non_unital_semiring Ξ²] : non_unital_semiring Ξ± := let zero := e.has_zero, add := e.has_add, mul := e.has_mul in by resetI; apply e.injective.non_unital_semiring _; intros; exact e.apply_symm_apply _ /-- Transfer `non_assoc_semiring` across an `equiv` -/ protected def non_assoc_semiring [non_assoc_semiring Ξ²] : non_assoc_semiring Ξ± := let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul in by resetI; apply e.injective.non_assoc_semiring _; intros; exact e.apply_symm_apply _ /-- Transfer `semiring` across an `equiv` -/ protected def semiring [semiring Ξ²] : semiring Ξ± := let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul in by resetI; apply e.injective.semiring _; intros; exact e.apply_symm_apply _ /-- Transfer `comm_semiring` across an `equiv` -/ protected def comm_semiring [comm_semiring Ξ²] : comm_semiring Ξ± := let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul in by resetI; apply e.injective.comm_semiring _; intros; exact e.apply_symm_apply _ /-- Transfer `ring` across an `equiv` -/ protected def ring [ring Ξ²] : ring Ξ± := let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg, sub := e.has_sub in by resetI; apply e.injective.ring _; intros; exact e.apply_symm_apply _ /-- Transfer `comm_ring` across an `equiv` -/ protected def comm_ring [comm_ring Ξ²] : comm_ring Ξ± := let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg, sub := e.has_sub in by resetI; apply e.injective.comm_ring _; intros; exact e.apply_symm_apply _ /-- Transfer `nonzero` across an `equiv` -/ protected theorem nontrivial [nontrivial Ξ²] : nontrivial Ξ± := e.surjective.nontrivial /-- Transfer `is_domain` across an `equiv` -/ protected theorem is_domain [ring Ξ±] [ring Ξ²] [is_domain Ξ²] (e : Ξ± ≃+* Ξ²) : is_domain Ξ± := function.injective.is_domain e.to_ring_hom e.injective /-- Transfer `division_ring` across an `equiv` -/ protected def division_ring [division_ring Ξ²] : division_ring Ξ± := let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg, sub := e.has_sub, inv := e.has_inv, div := e.has_div in by resetI; apply e.injective.division_ring _; intros; exact e.apply_symm_apply _ /-- Transfer `field` across an `equiv` -/ protected def field [field Ξ²] : field Ξ± := let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg, sub := e.has_sub, inv := e.has_inv, div := e.has_div in by resetI; apply e.injective.field _; intros; exact e.apply_symm_apply _ section R variables (R : Type*) include R section variables [monoid R] /-- Transfer `mul_action` across an `equiv` -/ protected def mul_action (e : Ξ± ≃ Ξ²) [mul_action R Ξ²] : mul_action R Ξ± := { one_smul := by simp [smul_def], mul_smul := by simp [smul_def, mul_smul], ..equiv.has_scalar e } /-- Transfer `distrib_mul_action` across an `equiv` -/ protected def distrib_mul_action (e : Ξ± ≃ Ξ²) [add_comm_monoid Ξ²] : begin letI := equiv.add_comm_monoid e, exact Ξ  [distrib_mul_action R Ξ²], distrib_mul_action R Ξ± end := begin intros, letI := equiv.add_comm_monoid e, exact ( { smul_zero := by simp [zero_def, smul_def], smul_add := by simp [add_def, smul_def, smul_add], ..equiv.mul_action R e } : distrib_mul_action R Ξ±) end end section variables [semiring R] /-- Transfer `module` across an `equiv` -/ protected def module (e : Ξ± ≃ Ξ²) [add_comm_monoid Ξ²] : begin letI := equiv.add_comm_monoid e, exact Ξ  [module R Ξ²], module R Ξ± end := begin introsI, exact ( { zero_smul := by simp [zero_def, smul_def], add_smul := by simp [add_def, smul_def, add_smul], ..equiv.distrib_mul_action R e } : module R Ξ±) end /-- An equivalence `e : Ξ± ≃ Ξ²` gives a linear equivalence `Ξ± ≃ₗ[R] Ξ²` where the `R`-module structure on `Ξ±` is the one obtained by transporting an `R`-module structure on `Ξ²` back along `e`. -/ def linear_equiv (e : Ξ± ≃ Ξ²) [add_comm_monoid Ξ²] [module R Ξ²] : begin letI := equiv.add_comm_monoid e, letI := equiv.module R e, exact Ξ± ≃ₗ[R] Ξ² end := begin introsI, exact { map_smul' := Ξ» r x, by { apply e.symm.injective, simp, refl, }, ..equiv.add_equiv e } end end section variables [comm_semiring R] /-- Transfer `algebra` across an `equiv` -/ protected def algebra (e : Ξ± ≃ Ξ²) [semiring Ξ²] : begin letI := equiv.semiring e, exact Ξ  [algebra R Ξ²], algebra R Ξ± end := begin introsI, fapply ring_hom.to_algebra', { exact ((ring_equiv e).symm : Ξ² β†’+* Ξ±).comp (algebra_map R Ξ²), }, { intros r x, simp only [function.comp_app, ring_hom.coe_comp], have p := ring_equiv_symm_apply e, dsimp at p, erw p, clear p, apply (ring_equiv e).injective, simp only [(ring_equiv e).map_mul], simp [algebra.commutes], } end /-- An equivalence `e : Ξ± ≃ Ξ²` gives an algebra equivalence `Ξ± ≃ₐ[R] Ξ²` where the `R`-algebra structure on `Ξ±` is the one obtained by transporting an `R`-algebra structure on `Ξ²` back along `e`. -/ def alg_equiv (e : Ξ± ≃ Ξ²) [semiring Ξ²] [algebra R Ξ²] : begin letI := equiv.semiring e, letI := equiv.algebra R e, exact Ξ± ≃ₐ[R] Ξ² end := begin introsI, exact { commutes' := Ξ» r, by { apply e.symm.injective, simp, refl, }, ..equiv.ring_equiv e } end end end R end instances end equiv namespace ring_equiv protected lemma local_ring {A B : Type*} [comm_ring A] [local_ring A] [comm_ring B] (e : A ≃+* B) : local_ring B := begin haveI := e.symm.to_equiv.nontrivial, refine @local_of_surjective A B _ _ _ _ e e.to_equiv.surjective, end end ring_equiv
07e526b6760794714d5baf3ba6f63a66cab23418
9dc8cecdf3c4634764a18254e94d43da07142918
/src/combinatorics/set_family/lym.lean
7734086928244cce66a49e6aae0a903a69fcc983
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
9,305
lean
/- Copyright (c) 2022 Bhavik Mehta, YaΓ«l Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Alena Gusakov, YaΓ«l Dillies -/ import algebra.big_operators.ring import combinatorics.double_counting import combinatorics.set_family.shadow import data.rat.order import tactic.linarith /-! # Lubell-Yamamoto-Meshalkin inequality and Sperner's theorem This file proves the local LYM and LYM inequalities as well as Sperner's theorem. ## Main declarations * `finset.card_div_choose_le_card_shadow_div_choose`: Local Lubell-Yamamoto-Meshalkin inequality. The shadow of a set `π’œ` in a layer takes a greater proportion of its layer than `π’œ` does. * `finset.sum_card_slice_div_choose_le_one`: Lubell-Yamamoto-Meshalkin inequality. The sum of densities of `π’œ` in each layer is at most `1` for any antichain `π’œ`. * `is_antichain.sperner`: Sperner's theorem. The size of any antichain in `finset Ξ±` is at most the size of the maximal layer of `finset Ξ±`. It is a corollary of `sum_card_slice_div_choose_le_one`. ## TODO Prove upward local LYM. Provide equality cases. Local LYM gives that the equality case of LYM and Sperner is precisely when `π’œ` is a middle layer. `falling` could be useful more generally in grade orders. ## References * http://b-mehta.github.io/maths-notes/iii/mich/combinatorics.pdf * http://discretemath.imp.fu-berlin.de/DMII-2015-16/kruskal.pdf ## Tags shadow, lym, slice, sperner, antichain -/ open finset nat open_locale big_operators finset_family variables {π•œ Ξ± : Type*} [linear_ordered_field π•œ] namespace finset /-! ### Local LYM inequality -/ section local_lym variables [decidable_eq Ξ±] [fintype Ξ±] {π’œ : finset (finset Ξ±)} {r : β„•} /-- The downward **local LYM inequality**, with cancelled denominators. `π’œ` takes up less of `Ξ±^(r)` (the finsets of card `r`) than `βˆ‚π’œ` takes up of `Ξ±^(r - 1)`. -/ lemma card_mul_le_card_shadow_mul (hπ’œ : (π’œ : set (finset Ξ±)).sized r) : π’œ.card * r ≀ (βˆ‚π’œ).card * (fintype.card Ξ± - r + 1) := begin refine card_mul_le_card_mul' (βŠ†) (Ξ» s hs, _) (Ξ» s hs, _), { rw [←hπ’œ hs, ←card_image_of_inj_on s.erase_inj_on], refine card_le_of_subset _, simp_rw [image_subset_iff, mem_bipartite_below], exact Ξ» a ha, ⟨erase_mem_shadow hs ha, erase_subset _ _⟩ }, refine le_trans _ tsub_tsub_le_tsub_add, rw [←hπ’œ.shadow hs, ←card_compl, ←card_image_of_inj_on (insert_inj_on' _)], refine card_le_of_subset (Ξ» t ht, _), apply_instance, rw mem_bipartite_above at ht, have : βˆ… βˆ‰ π’œ, { rw [←mem_coe, hπ’œ.empty_mem_iff, coe_eq_singleton], rintro rfl, rwa shadow_singleton_empty at hs }, obtain ⟨a, ha, rfl⟩ := exists_eq_insert_iff.2 ⟨ht.2, by rw [(sized_shadow_iff this).1 hπ’œ.shadow ht.1, hπ’œ.shadow hs]⟩, exact mem_image_of_mem _ (mem_compl.2 ha), end /-- The downward **local LYM inequality**. `π’œ` takes up less of `Ξ±^(r)` (the finsets of card `r`) than `βˆ‚π’œ` takes up of `Ξ±^(r - 1)`. -/ lemma card_div_choose_le_card_shadow_div_choose (hr : r β‰  0) (hπ’œ : (π’œ : set (finset Ξ±)).sized r) : (π’œ.card : π•œ) / (fintype.card Ξ±).choose r ≀ (βˆ‚π’œ).card / (fintype.card Ξ±).choose (r - 1) := begin obtain hr' | hr' := lt_or_le (fintype.card Ξ±) r, { rw [choose_eq_zero_of_lt hr', cast_zero, div_zero], exact div_nonneg (cast_nonneg _) (cast_nonneg _) }, replace hπ’œ := card_mul_le_card_shadow_mul hπ’œ, rw div_le_div_iff; norm_cast, { cases r, { exact (hr rfl).elim }, rw nat.succ_eq_add_one at *, rw [tsub_add_eq_add_tsub hr', add_tsub_add_eq_tsub_right] at hπ’œ, apply le_of_mul_le_mul_right _ (pos_iff_ne_zero.2 hr), convert nat.mul_le_mul_right ((fintype.card Ξ±).choose r) hπ’œ using 1, { simp [mul_assoc, nat.choose_succ_right_eq], exact or.inl (mul_comm _ _) }, { simp only [mul_assoc, choose_succ_right_eq, mul_eq_mul_left_iff], exact or.inl (mul_comm _ _) } }, { exact nat.choose_pos hr' }, { exact nat.choose_pos (r.pred_le.trans hr') } end end local_lym /-! ### LYM inequality -/ section lym section falling variables [decidable_eq Ξ±] (k : β„•) (π’œ : finset (finset Ξ±)) /-- `falling k π’œ` is all the finsets of cardinality `k` which are a subset of something in `π’œ`. -/ def falling : finset (finset Ξ±) := π’œ.sup $ powerset_len k variables {π’œ k} {s : finset Ξ±} lemma mem_falling : s ∈ falling k π’œ ↔ (βˆƒ t ∈ π’œ, s βŠ† t) ∧ s.card = k := by simp_rw [falling, mem_sup, mem_powerset_len, exists_and_distrib_right] variables (π’œ k) lemma sized_falling : (falling k π’œ : set (finset Ξ±)).sized k := Ξ» s hs, (mem_falling.1 hs).2 lemma slice_subset_falling : π’œ # k βŠ† falling k π’œ := Ξ» s hs, mem_falling.2 $ (mem_slice.1 hs).imp_left $ Ξ» h, ⟨s, h, subset.refl _⟩ lemma falling_zero_subset : falling 0 π’œ βŠ† {βˆ…} := subset_singleton_iff'.2 $ Ξ» t ht, card_eq_zero.1 $ sized_falling _ _ ht lemma slice_union_shadow_falling_succ : π’œ # k βˆͺ βˆ‚ (falling (k + 1) π’œ) = falling k π’œ := begin ext s, simp_rw [mem_union, mem_slice, mem_shadow_iff, exists_prop, mem_falling], split, { rintro (h | ⟨s, ⟨⟨t, ht, hst⟩, hs⟩, a, ha, rfl⟩), { exact ⟨⟨s, h.1, subset.refl _⟩, h.2⟩ }, refine ⟨⟨t, ht, (erase_subset _ _).trans hst⟩, _⟩, rw [card_erase_of_mem ha, hs], refl }, { rintro ⟨⟨t, ht, hst⟩, hs⟩, by_cases s ∈ π’œ, { exact or.inl ⟨h, hs⟩ }, obtain ⟨a, ha, hst⟩ := ssubset_iff.1 (ssubset_of_subset_of_ne hst (ht.ne_of_not_mem h).symm), refine or.inr ⟨insert a s, ⟨⟨t, ht, hst⟩, _⟩, a, mem_insert_self _ _, erase_insert ha⟩, rw [card_insert_of_not_mem ha, hs] } end variables {π’œ k} /-- The shadow of `falling m π’œ` is disjoint from the `n`-sized elements of `π’œ`, thanks to the antichain property. -/ lemma _root_.is_antichain.disjoint_slice_shadow_falling {m n : β„•} (hπ’œ : is_antichain (βŠ†) (π’œ : set (finset Ξ±))) : disjoint (π’œ # m) (βˆ‚ (falling n π’œ)) := disjoint_right.2 $ Ξ» s h₁ hβ‚‚, begin simp_rw [mem_shadow_iff, exists_prop, mem_falling] at h₁, obtain ⟨s, ⟨⟨t, ht, hst⟩, hs⟩, a, ha, rfl⟩ := h₁, refine hπ’œ (slice_subset hβ‚‚) ht _ ((erase_subset _ _).trans hst), rintro rfl, exact not_mem_erase _ _ (hst ha), end /-- A bound on any top part of the sum in LYM in terms of the size of `falling k π’œ`. -/ lemma le_card_falling_div_choose [fintype Ξ±] (hk : k ≀ fintype.card Ξ±) (hπ’œ : is_antichain (βŠ†) (π’œ : set (finset Ξ±))) : βˆ‘ r in range (k + 1), ((π’œ # (fintype.card Ξ± - r)).card : π•œ) / (fintype.card Ξ±).choose (fintype.card Ξ± - r) ≀ (falling (fintype.card Ξ± - k) π’œ).card / (fintype.card Ξ±).choose (fintype.card Ξ± - k) := begin induction k with k ih, { simp only [tsub_zero, cast_one, cast_le, sum_singleton, div_one, choose_self, range_one], exact card_le_of_subset (slice_subset_falling _ _) }, rw succ_eq_add_one at *, rw [sum_range_succ, ←slice_union_shadow_falling_succ, card_disjoint_union hπ’œ.disjoint_slice_shadow_falling, cast_add, _root_.add_div, add_comm], rw [←tsub_tsub, tsub_add_cancel_of_le (le_tsub_of_add_le_left hk)], exact add_le_add_left ((ih $ le_of_succ_le hk).trans $ card_div_choose_le_card_shadow_div_choose (tsub_pos_iff_lt.2 $ nat.succ_le_iff.1 hk).ne' $ sized_falling _ _) _, end end falling variables {π’œ : finset (finset Ξ±)} {s : finset Ξ±} {k : β„•} /-- The **Lubell-Yamamoto-Meshalkin inequality**. If `π’œ` is an antichain, then the sum of the proportion of elements it takes from each layer is less than `1`. -/ lemma sum_card_slice_div_choose_le_one [fintype Ξ±] (hπ’œ : is_antichain (βŠ†) (π’œ : set (finset Ξ±))) : βˆ‘ r in range (fintype.card Ξ± + 1), ((π’œ # r).card : π•œ) / (fintype.card Ξ±).choose r ≀ 1 := begin classical, rw ←sum_flip, refine (le_card_falling_div_choose le_rfl hπ’œ).trans _, rw div_le_iff; norm_cast, { simpa only [nat.sub_self, one_mul, nat.choose_zero_right, falling] using (sized_falling 0 π’œ).card_le }, { rw [tsub_self, choose_zero_right], exact zero_lt_one } end end lym /-! ### Sperner's theorem -/ /-- **Sperner's theorem**. The size of an antichain in `finset Ξ±` is bounded by the size of the maximal layer in `finset Ξ±`. This precisely means that `finset Ξ±` is a Sperner order. -/ lemma _root_.is_antichain.sperner [fintype Ξ±] {π’œ : finset (finset Ξ±)} (hπ’œ : is_antichain (βŠ†) (π’œ : set (finset Ξ±))) : π’œ.card ≀ (fintype.card Ξ±).choose (fintype.card Ξ± / 2) := begin classical, suffices : βˆ‘ r in Iic (fintype.card Ξ±), ((π’œ # r).card : β„š) / (fintype.card Ξ±).choose (fintype.card Ξ± / 2) ≀ 1, { rwa [←sum_div, ←nat.cast_sum, div_le_one, cast_le, sum_card_slice] at this, norm_cast, exact choose_pos (nat.div_le_self _ _) }, rw [Iic_eq_Icc, ←Ico_succ_right, bot_eq_zero, Ico_zero_eq_range], refine (sum_le_sum $ Ξ» r hr, _).trans (sum_card_slice_div_choose_le_one hπ’œ), rw mem_range at hr, refine div_le_div_of_le_left _ _ _; norm_cast, { exact nat.zero_le _ }, { exact choose_pos (lt_succ_iff.1 hr) }, { exact choose_le_middle _ _ } end end finset
e76855b40319fc7b434a862561cf8aa954669e05
94637389e03c919023691dcd05bd4411b1034aa5
/src/inClassNotes/propositions/propositions_as_types_the_idea.lean
4260f391a7a6affde213b3ade617c115eada45f7
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
2,867
lean
/- Propositions as types: the very idea. -/ /- Idea #1. Use types to represent logical propositions and values of these types to represent "evidence" or "proofs" of truth. A type that is inhabited we then judge to be logically true, and one that is uninhabited we judge to be false. -/ /- Idea #2. When we use values of a type only to represent evidence of logical truth, then it doesn't really matter *which* value of a type we pick as evidence. To the first order, we don't care which proof of a theorem a mathematician produces, only that there is at least one. And indeed, any proof will do to justify a judgment that a proposition is true. So when we represent propositions as types and proofs as values, in some sense we want every proof of a given type to be equivalent to any other. We want every value of a "logical" type to be equivalent to any other. Making this equivalence of proof terms is one of the main purposes of the last of Lean's type universes, which we now introduce: Prop. -/ inductive its_raining : Prop -- not quite right | i_see_rain_falling : its_raining | i_hear_rain_on_roof : its_raining inductive streets_wet : Prop | i_see_wet_streets : streets_wet | i_feel_wet_streets : streets_wet open its_raining streets_wet def proof_1'' : its_raining := i_see_rain_falling def proof_2'' : its_raining := i_hear_rain_on_roof lemma proof_1' : its_raining := i_see_rain_falling lemma proof_2' : its_raining := i_hear_rain_on_roof theorem proof_1 : its_raining := i_see_rain_falling theorem proof_2 : its_raining := i_hear_rain_on_roof /- We just don't care which value of a propositional type is used. Values build by different constructors in Type n, are always NOT equal. This is the idea that "constructors are injective and disjoint." -/ /- Prop Sort 0 Type 0 Sort 1 Type 1 Sort 2 Type 2 Sort 3 ... ... -/ -- Change Type to Prop above! /- Just as we can represent propositions as types, we can represent *predicates* as type families indexed by arguments, i.e., as "propositions with parameters." -/ inductive day : Type | su | mo | tu | we | th | fr | sa open day /- A type family indexed by day representing the predicate ⟨ is_always_rainy d ⟩, where d is a parameter of type day. Providing a value for d yields a specific proposition in the family of propositions we've defined, namely one claims that a specific day, d, is always rainy. -/ inductive is_always_rainy : day β†’ Sort 0 | mo_rainy : βˆ€ (d :day), (d = mo) β†’ is_always_rainy d open is_always_rainy #check is_always_rainy #check is_always_rainy su #check is_always_rainy mo #check is_always_rainy tu lemma bad_tuesdays : is_always_rainy tu := mo_rainy _ _ -- stuck lemma bad_fridays : is_always_rainy fr := mo_rainy _ _ -- stuck lemma bad_mondays : is_always_rainy mo := mo_rainy mo (eq.refl mo)
4c6eab007431f36850f9d753823ef9685329421e
de4548698671d50981659ecc9f4910de15969d3d
/Metamath/Verify.lean
ba124670a9e2d55a18a7c64175435e9410a688b5
[]
no_license
digama0/mm-lean4
7ad17c81853816c6cd4bb97b8abe4bea0fd35ff6
6a427edecb851cec04818848a755c0145a5f2e98
refs/heads/master
1,688,934,520,262
1,687,937,043,000
1,687,937,043,000
365,257,017
15
1
null
null
null
null
UTF-8
Lean
false
false
26,335
lean
import Lean.Data.HashMap import Lean.Data.HashSet import Std.Data.Nat.Lemmas theorem Fin.val_eq_of_lt : a < n.succ β†’ (@Fin.ofNat n a).val = a := Nat.mod_eq_of_lt theorem UInt32.val_eq_of_lt : a < UInt32.size β†’ (UInt32.ofNat a).val = a := Fin.val_eq_of_lt theorem toChar_aux (n : Nat) (h : n < UInt8.size) : Nat.isValidChar (UInt32.ofNat n).1 := by rw [UInt32.val_eq_of_lt] exact Or.inl (Nat.lt_trans h (by decide)) exact Nat.lt_trans h (by decide) def UInt8.toChar (n : UInt8) : Char := ⟨n.toUInt32, toChar_aux n.1 n.1.2⟩ def Char.toUInt8 (n : Char) : UInt8 := n.1.toUInt8 theorem Char.utf8Size_pos (c : Char) : 0 < c.utf8Size := let foo {c} {t e : UInt32} : [Decidable c] β†’ 0 < t β†’ 0 < e β†’ 0 < ite c t e | Decidable.isTrue _, h, _ => h | Decidable.isFalse _, _, h => h foo (by decide) $ foo (by decide) $ foo (by decide) (by decide) theorem String.csize_pos : (c : Char) β†’ 0 < String.csize c := Char.utf8Size_pos namespace UInt8 def isUpper (c : UInt8) : Bool := c β‰₯ 65 && c ≀ 90 def isLower (c : UInt8) : Bool := c β‰₯ 97 && c ≀ 122 def isAlpha (c : UInt8) : Bool := c.isUpper || c.isLower def isDigit (c : UInt8) : Bool := c β‰₯ 48 && c ≀ 57 def isAlphanum (c : UInt8) : Bool := c.isAlpha || c.isDigit end UInt8 structure ByteSliceT where arr : ByteArray off : Nat namespace ByteSliceT @[inline] def size (self : ByteSliceT) : Nat := self.arr.size - self.off instance : GetElem ByteSliceT Nat UInt8 fun _ _ => True where getElem self idx _ := self.arr.get! (self.off + idx) end ByteSliceT def ByteArray.toSliceT (arr : ByteArray) : ByteSliceT := ⟨arr, 0⟩ structure ByteSlice where arr : ByteArray off : Nat len : Nat namespace ByteSlice def toArray : ByteSlice β†’ ByteArray | ⟨arr, off, len⟩ => arr.extract off len instance : GetElem ByteSlice Nat UInt8 fun _ _ => True where getElem self idx _ := self.arr.get! (self.off + idx) def forIn.loop [Monad m] (f : UInt8 β†’ Ξ² β†’ m (ForInStep Ξ²)) (arr : ByteArray) (off stop : Nat) (i : Nat) (b : Ξ²) : m Ξ² := do if i < stop then match ← f (arr.get! i) b with | ForInStep.done b => pure b | ForInStep.yield b => loop f arr off stop (i+1) b else pure b termination_by _ => stop - i instance : ForIn m ByteSlice UInt8 := ⟨fun ⟨arr, off, len⟩ b f => forIn.loop f arr off (off + len) off b⟩ end ByteSlice def ByteSliceT.toSlice : ByteSliceT β†’ ByteSlice | ⟨arr, off⟩ => ⟨arr, off, arr.size - off⟩ def ByteArray.toSlice (arr : ByteArray) : ByteSlice := ⟨arr, 0, arr.size⟩ def ByteSlice.eqArray (bs : ByteSlice) (arr : ByteArray) : Bool := let rec loop (arr₁ : ByteArray) (i j : Nat) : Bool := if j < arr.size then arr₁.get! i == arr.get! j && loop arr₁ (i+1) (j+1) else true bs.len == arr.size && loop bs.arr bs.off 0 termination_by loop => arr.size - j def String.toAscii (s : String) : ByteArray := let rec loop (out : ByteArray) (p : Pos) : ByteArray := if h : s.atEnd p then out else let c := s.get p have := Nat.sub_lt_sub_left (Nat.gt_of_not_le (mt decide_eq_true h)) (lt_next s _) loop (out.push c.toUInt8) (s.next p) loop ByteArray.empty 0 termination_by loop => s.endPos.1 - p.1 def ByteSlice.toString (bs : ByteSlice) : String := Id.run do let mut s := "" for c in bs do s := s.push c.toChar s instance : ToString ByteSlice where toString bs := Id.run do let mut s := "" for c in bs do s := s.push c.toChar s namespace Metamath namespace Verify open IO.FS (Handle) open Lean (HashMap HashSet) def isLabelChar (c : UInt8) : Bool := c.isAlphanum || c == '-'.toUInt8 || c == '_'.toUInt8 || c == '.'.toUInt8 def isWhitespace (c : UInt8) : Bool := c == ' '.toUInt8 || c == '\n'.toUInt8 || c == '\r'.toUInt8 || c == '\t'.toUInt8 def isPrintable (c : UInt8) : Bool := c >= 32 && c <= 126 def isMathChar (c : UInt8) : Bool := c β‰  '$'.toUInt8 && isPrintable c def toLabel (bs : ByteSlice) : Bool Γ— String := Id.run do let mut ok := true let mut s := "" for c in bs do s := s.push c.toChar unless isLabelChar c do ok := false (ok, s) def toMath (bs : ByteSlice) : Bool Γ— String := Id.run do let mut ok := true let mut s := "" for c in bs do s := s.push c.toChar unless isPrintable c do ok := false (ok, s) structure Pos := (line col : Nat) instance : ToString Pos := ⟨fun ⟨l, c⟩ => s!"{l}:{c}"⟩ def DJ := String Γ— String instance : BEq DJ := instBEqProd structure Frame where dj : Array DJ hyps : Array String deriving Inhabited def Frame.size : Frame β†’ Nat Γ— Nat | ⟨dj, hyps⟩ => (dj.size, hyps.size) def Frame.shrink : Frame β†’ Nat Γ— Nat β†’ Frame | ⟨dj, hyps⟩, (x, y) => ⟨dj.shrink x, hyps.shrink y⟩ instance : ToString Frame := ⟨fun fr => toString fr.hyps⟩ inductive Sym | const (c : String) | var (v : String) deriving Inhabited def Sym.isVar : Sym β†’ Bool | const _ => false | var _ => true def Sym.value : Sym β†’ String | const c => c | var v => v instance : BEq Sym := ⟨fun a b => a.value == b.value⟩ abbrev Formula := Array Sym instance : ToString Formula where toString f := Id.run do let s := f[0]!.value f.foldl (init := s) (start := 1) fun (s:String) v => s ++ " " ++ v.value def Formula.subst (Οƒ : HashMap String Formula) (f : Formula) : Except String Formula := do let mut f' := #[] for c in f do match c with | Sym.const _ => f' := f'.push c | Sym.var v => match Οƒ.find? v with | none => throw s!"variable {v} not found" | some e => f' := e.foldl Array.push f' 1 pure f' def Formula.foldlVars (self : Formula) (init : Ξ±) (f : Ξ± β†’ String β†’ Ξ±) : Ξ± := self.foldl (init := init) (start := 1) fun a v => match v with | Sym.var v => f a v | _ => a inductive Object | const : String β†’ Object | var : String β†’ Object | hyp : Bool β†’ Formula β†’ String β†’ Object | assert : Formula β†’ Frame β†’ String β†’ Object inductive ProofTokenParser | start | preload | normal | compressed (chr : Nat) inductive HeapEl | fmla (f : Formula) | assert (f : Formula) (fr : Frame) instance : ToString HeapEl where toString | HeapEl.fmla f => toString f | HeapEl.assert f fr => s!"{fr} |- {f}" structure ProofState where pos : Pos label : String fmla : Formula frame : Frame heap : Array HeapEl stack : Array Formula ptp : ProofTokenParser instance : ToString ProofState where toString p := Id.run do let mut s := s!"at {p.pos}: {p.label}\n" let mut i := 0 for el in p.heap do s := s ++ s!"heap {i} := {el}\n" i := i + 1 s := s ++ "\n" for el in p.stack do s := s ++ s!"{el}\n" s namespace ProofState def push (pr : ProofState) (f : Formula) : ProofState := { pr with stack := pr.stack.push f } def pushHeap (pr : ProofState) (el : HeapEl) : ProofState := { pr with heap := pr.heap.push el } def save (pr : ProofState) : Except String ProofState := if pr.stack.isEmpty then throw "can't save empty stack" else let f := pr.stack.back pure <| pr.pushHeap (HeapEl.fmla f) end ProofState inductive Error | error (pos : Pos) (msg : String) | ax (pos : Pos) (l : String) (f : Formula) (fr : Frame) | thm (pos : Pos) (l : String) (f : Formula) (fr : Frame) structure Interrupt := e : Error idx : Nat structure DB where frame : Frame scopes : Array (Nat Γ— Nat) objects : HashMap String Object interrupt : Bool error? : Option Interrupt deriving Inhabited namespace DB @[inline] def error (s : DB) : Bool := s.error?.isSome def mkError (s : DB) (pos : Pos) (msg : String) : DB := { s with error? := some ⟨Error.error pos msg, default⟩ } def pushScope (s : DB) : DB := { s with scopes := s.scopes.push s.frame.size } def popScope (pos : Pos) (db : DB) : DB := if db.scopes.isEmpty then db.mkError pos "can't pop global scope" else { db with frame := db.frame.shrink db.scopes.back, scopes := db.scopes.pop } def find? (db : DB) (l : String) : Option Object := db.objects.find? l def isConst (db : DB) (tk : String) : Bool := if let some (Object.const _) := db.find? tk then true else false def isVar (db : DB) (tk : String) : Bool := if let some (Object.var _) := db.find? tk then true else false def isSym (db : DB) (tk : String) : Bool := match db.find? tk with | some (Object.const _) => true | some (Object.var _) => true | _ => false @[inline] def withFrame (f : Frame β†’ Frame) (db : DB) : DB := { db with frame := f db.frame } @[inline] def withDJ (f : Array DJ β†’ Array DJ) (db : DB) : DB := db.withFrame fun ⟨dj, hyps⟩ => ⟨f dj, hyps⟩ @[inline] def withHyps (f : Array String β†’ Array String) (db : DB) : DB := db.withFrame fun ⟨dj, hyps⟩ => ⟨dj, f hyps⟩ def insert (db : DB) (pos : Pos) (l : String) (obj : String β†’ Object) : DB := if let some o := db.find? l then let ok : Bool := match o with | Object.var _ => if let Object.var _ := obj l then true else false | _ => false if ok then db else db.mkError pos s!"duplicate symbol/assert {l}" else { db with objects := db.objects.insert l (obj l) } def insertHyp (db : DB) (pos : Pos) (l : String) (ess : Bool) (f : Formula) : DB := let db := db.insert pos l (Object.hyp ess f) db.withHyps fun hyps => hyps.push l def trimFrame (db : DB) (fmla : Formula) (fr := db.frame) : Bool Γ— Frame := Id.run do let collectVars (fmla : Formula) vars := fmla.foldlVars vars HashSet.insert let mut vars : HashSet String := collectVars fmla HashSet.empty for l in fr.hyps do if let some (Object.hyp true f _) := db.find? l then vars := collectVars f vars let mut dj := #[] for v in fr.dj do if vars.contains v.1 && vars.contains v.2 then dj := dj.push v let mut hyps := #[] let mut inHyps := false let mut ok := true for l in fr.hyps do let ess ← if let some (Object.hyp false f _) := db.find? l then if inHyps then ok := false vars.contains f[1]!.value else inHyps := true true if ess then hyps := hyps.push l (ok, ⟨dj, hyps⟩) def trimFrame' (db : DB) (fmla : Formula) : Except String Frame := let (ok, fr) := db.trimFrame fmla if ok then pure fr else throw s!"out of order hypotheses in frame" def insertAxiom (db : DB) (pos : Pos) (l : String) (fmla : Formula) : DB := match db.trimFrame' fmla with | Except.ok fr => if db.interrupt then { db with error? := some ⟨Error.ax pos l fmla fr, default⟩ } else db.insert pos l (Object.assert fmla fr) | Except.error msg => db.mkError pos msg def mkProofState (db : DB) (pos : Pos) (l : String) (fmla : Formula) (fr : Frame) : ProofState := Id.run do let mut heap := #[] for l in fr.hyps do if let some (Object.hyp _ f _) := db.find? l then heap := heap.push (HeapEl.fmla f) ⟨pos, l, fmla, fr, heap, #[], ProofTokenParser.start⟩ def preload (db : DB) (pr : ProofState) (l : String) : Except String ProofState := match db.find? l with | some (Object.hyp true _ _) => throw "$e found in paren list" | some (Object.hyp _ f _) => return pr.pushHeap (HeapEl.fmla f) | some (Object.assert f fr _) => return pr.pushHeap (HeapEl.assert f fr) | _ => throw s!"statement {l} not found" @[inline] def checkHypF (db : DB) (hyps : Array String) (stack : Array Formula) (off : {off // off + hyps.size = stack.size}) (IH : HashMap String Formula β†’ Except String (HashMap String Formula)) (i : Nat) (h : i < hyps.size) (subst : HashMap String Formula) : Except String (HashMap String Formula) := do let val := stack.get ⟨off.1 + i, let thm {a b n} : i < a β†’ n + a = b β†’ n + i < b | h, rfl => Nat.add_lt_add_left h _ thm h off.2⟩ if let some (Object.hyp ess f _) := db.find? (hyps.get ⟨i, h⟩) then if f[0]! == val[0]! then if ess then if (← f.subst subst) == val then IH subst else throw "type error in substitution" else IH (subst.insert f[1]!.value val) else throw s!"bad typecode in substitution {hyps[i]}: {f} / {val}" else unreachable! variable (db : DB) (hyps : Array String) (stack : Array Formula) (off : {off // off + hyps.size = stack.size}) in def checkHyp (i : Nat) (subst : HashMap String Formula) : Except String (HashMap String Formula) := do if h : i < hyps.size then let val := stack.get ⟨off.1 + i, let thm {a b n} : i < a β†’ n + a = b β†’ n + i < b | h, rfl => Nat.add_lt_add_left h _ thm h off.2⟩ if let some (Object.hyp ess f _) := db.find? (hyps.get ⟨i, h⟩) then if f[0]! == val[0]! then if ess then if (← f.subst subst) == val then checkHyp (i+1) subst else throw "type error in substitution" else checkHyp (i+1) (subst.insert f[1]!.value val) else throw s!"bad typecode in substitution {hyps[i]}: {f} / {val}" else unreachable! else pure subst termination_by _ => hyps.size - i def stepAssert (db : DB) (pr : ProofState) (f : Formula) : Frame β†’ Except String ProofState | ⟨dj, hyps⟩ => do if h : hyps.size ≀ pr.stack.size then let off : {off // off + hyps.size = pr.stack.size} := ⟨pr.stack.size - hyps.size, Nat.sub_add_cancel h⟩ let subst ← checkHyp db hyps pr.stack off 0 HashMap.empty let disj s1 s2 := s1 != s2 && db.frame.dj.contains (if s1 < s2 then (s1, s2) else (s2, s1)) for (v1, v2) in dj do let e1 := subst.find! v1 let e2 := subst.find! v2 let disjoint := e1.foldlVars (init := true) fun b s1 => e2.foldlVars b fun b s2 => b && disj s1 s2 if !disjoint then throw "disjoint variable violation" let concl ← f.subst subst pure { pr with stack := (pr.stack.shrink off).push concl } else throw "stack underflow" def stepNormal (db : DB) (pr : ProofState) (l : String) : Except String ProofState := match db.find? l with | some (Object.hyp _ f _) => return pr.push f | some (Object.assert f fr _) => db.stepAssert pr f fr | _ => throw s!"statement {l} not found" def stepProof (db : DB) (pr : ProofState) (i : Nat) : Except String ProofState := match pr.heap.get? i with | none => throw "proof backref index out of range" | some (HeapEl.fmla f) => return pr.push f | some (HeapEl.assert f fr) => db.stepAssert pr f fr end DB inductive CharParser | ws : CharParser | token : Nat β†’ ByteSliceT β†’ CharParser deriving Inhabited inductive TokensKind | float | ess | ax | thm open TokensKind in instance : ToString TokensKind where toString | float => "float" | ess => "ess" | ax => "ax" | thm => "thm" def TokensKind.delim | thm => "$=".toAscii | _ => "$.".toAscii structure TokensParser where k : TokensKind pos : Pos label : String instance : ToString TokensParser where toString | ⟨k, pos, label⟩ => s!"at {pos}: {k} {label}" inductive TokenParser | start : TokenParser | comment : TokenParser β†’ TokenParser | const : TokenParser | var : TokenParser | djvars : Array String β†’ TokenParser | math : Array Sym β†’ TokensParser β†’ TokenParser | label : Pos β†’ String β†’ TokenParser | proof : ProofState β†’ TokenParser deriving Inhabited def TokenParser.toString : TokenParser β†’ String | start => "start" | comment p => "comment " ++ toString p | const => "const" | var => "var" | djvars s => s!"djvars {s}" | math s p => s!"math {s} {p}" | label pos l => s!"at {pos}: ? {l}" | proof p => ToString.toString p instance : ToString TokenParser := ⟨TokenParser.toString⟩ structure ParserState where db : DB tokp : TokenParser charp : CharParser line : Nat linepos : Nat deriving Inhabited namespace ParserState @[inline] def withDB (f : DB β†’ DB) (s : ParserState) : ParserState := { s with db := f s.db } def mkPos (s : ParserState) (pos : Nat) : Pos := ⟨s.line, pos - s.linepos⟩ def mkError (s : ParserState) (pos : Pos) (msg : String) : ParserState := s.withDB fun db => db.mkError pos msg def mkErrorAt (s : ParserState) (pos : Pos) (l msg : String) : ParserState := s.mkError pos s!"at {l}: {msg}" def withAt (l : String) (f : Unit β†’ ParserState) : ParserState := let s := f () if let some ⟨Error.error pos msg, i⟩ := s.db.error? then s.withDB fun db => { db with error? := some ⟨Error.error pos s!"at {l}: {msg}", i⟩ } else s def label (s : ParserState) (pos : Pos) (tk : ByteSlice) : ParserState := let (ok, tk) := toLabel tk if ok then { s with tokp := TokenParser.label pos tk } else s.mkError pos s!"invalid label '{tk}'" def withMath (s : ParserState) (pos : Pos) (tk : ByteSlice) (f : ParserState β†’ String β†’ ParserState) : ParserState := let (ok, tk) := toMath tk if !ok then s.mkError pos s!"invalid math string '{tk}'" else f s tk def sym (s : ParserState) (pos : Pos) (tk : ByteSlice) (f : String β†’ Object) : ParserState := if tk.eqArray "$.".toAscii then { s with tokp := TokenParser.start } else s.withMath pos tk fun s tk => s.withDB fun db => db.insert pos tk f def resumeAxiom (s : ParserState) (pos : Pos) (l : String) (fmla : Formula) (fr : Frame) : ParserState := s.withDB fun db => db.insert pos l (Object.assert fmla fr) def resumeThm (s : ParserState) (pos : Pos) (l : String) (fmla : Formula) (fr : Frame) : ParserState := let pr := s.db.mkProofState pos l fmla fr { s with tokp := TokenParser.proof pr } def feedTokens (s : ParserState) (arr : Array Sym) : TokensParser β†’ ParserState | ⟨k, pos, l⟩ => withAt l fun _ => Id.run do unless arr.size > 0 && !arr[0]!.isVar do return s.mkError pos "first symbol is not a constant" match k with | TokensKind.float => unless arr.size == 2 && arr[1]!.isVar do return s.mkError pos "expected a constant and a variable" let s := s.withDB fun db => db.insertHyp pos l false arr pure { s with tokp := TokenParser.start } | TokensKind.ess => let s := s.withDB fun db => db.insertHyp pos l true arr pure { s with tokp := TokenParser.start } | TokensKind.ax => let s := s.withDB fun db => db.insertAxiom pos l arr pure { s with tokp := TokenParser.start } | TokensKind.thm => match s.db.trimFrame' arr with | Except.ok fr => if s.db.interrupt then s.withDB fun db => { db with error? := some ⟨Error.thm pos l arr fr, default⟩ } else s.resumeThm pos l arr fr | Except.error msg => s.mkError pos msg def feedProof (s : ParserState) (tk : ByteSlice) (pr : ProofState) : ParserState := withAt pr.label fun _ => match go pr with | Except.ok pr => { s with tokp := TokenParser.proof pr } | Except.error msg => s.mkError pr.pos msg where goNormal (pr : ProofState) := let (ok, tk) := toLabel tk if ok then s.db.stepNormal pr tk else throw s!"invalid label '{tk}'" go (pr : ProofState) : Except String ProofState := do match pr.ptp with | ProofTokenParser.start => if tk.eqArray "(".toAscii then pure { pr with ptp := ProofTokenParser.preload } else goNormal { pr with ptp := ProofTokenParser.normal } | ProofTokenParser.preload => if tk.eqArray ")".toAscii then pure { pr with ptp := ProofTokenParser.compressed 0 } else let (ok, tk) := toLabel tk if ok then s.db.preload pr tk else throw s!"invalid label '{tk}'" | ProofTokenParser.normal => goNormal pr | ProofTokenParser.compressed chr => let mut pr := pr let mut chr := chr for c in tk do if 'A'.toUInt8 ≀ c && c ≀ 'Z'.toUInt8 then if c ≀ 'T'.toUInt8 then let n := 20 * chr + (c - 'A'.toUInt8).1 pr ← s.db.stepProof pr n chr := 0 else if c < 'Z'.toUInt8 then chr := 5 * chr + (c - 'T'.toUInt8).1 else pr ← pr.save chr := 0 else if c = '?'.toUInt8 then throw "proof contains '?'" else throw "proof parse error" pure { pr with ptp := ProofTokenParser.compressed chr } def finishProof (s : ParserState) : ProofState β†’ ParserState | ⟨pos, l, fmla, fr, _, stack, ptp⟩ => withAt l fun _ => Id.run do let s := { s with tokp := TokenParser.start } match ptp with | ProofTokenParser.compressed 0 => () | ProofTokenParser.normal => () | _ => return s.mkError pos "proof parse error" unless stack.size == 1 do return s.mkError pos "more than one element on stack" unless stack[0]! == fmla do return s.mkError pos "theorem does not prove what it claims" s.withDB fun db => db.insert pos l (Object.assert fmla fr) def feedToken (s : ParserState) (pos : Nat) (tk : ByteSlice) : ParserState := let pos := s.mkPos pos match s.tokp with | TokenParser.comment p => if tk.eqArray "$)".toAscii then { s with tokp := p } else s | p => if tk.eqArray "$(".toAscii then { s with tokp := p.comment } else match p with | TokenParser.comment _ => unreachable! | TokenParser.start => if tk.len == 2 && tk[0] == '$'.toUInt8 then match tk[1].toChar with | '{' => s.withDB DB.pushScope | '}' => s.withDB (DB.popScope pos) | 'c' => { s with tokp := TokenParser.const } | 'v' => { s with tokp := TokenParser.var } | 'd' => { s with tokp := TokenParser.djvars #[] } | _ => s.label pos tk else s.label pos tk | TokenParser.const => s.sym pos tk Object.const | TokenParser.var => s.sym pos tk Object.var | TokenParser.djvars arr => if tk.eqArray "$.".toAscii then { s with tokp := TokenParser.start } else s.withMath pos tk fun s tk => Id.run do unless s.db.isVar tk do return s.mkError pos s!"{tk} is not a variable" let mut s := s for tk1 in arr do if tk1 == tk then return s.mkError pos s!"duplicate disjoint variable {tk}" let p := if tk1 < tk then (tk1, tk) else (tk, tk1) s := s.withDB fun db => db.withDJ fun dj => dj.push p { s with tokp := TokenParser.djvars (arr.push tk) } | TokenParser.math arr p => if tk.eqArray p.k.delim then s.feedTokens arr p else s.withMath pos tk fun s tk => Id.run do let tk ← match s.db.find? tk with | some (Object.const _) => Sym.const tk | some (Object.var _) => Sym.var tk | _ => return s.mkError pos s!"{tk} is not a constant or variable" { s with tokp := TokenParser.math (arr.push tk) p } | TokenParser.label pos lab => if tk.len == 2 && tk[0] == '$'.toUInt8 then let go (s : ParserState) (k : TokensKind) := { s with tokp := TokenParser.math #[] ⟨k, pos, lab⟩ } match tk[1].toChar with | 'f' => go s TokensKind.float | 'e' => go s TokensKind.ess | 'a' => go s TokensKind.ax | 'p' => go s TokensKind.thm | _ => s.mkError pos s!"unknown statement type {(toLabel tk).2}" else s.mkError pos s!"unknown statement type {(toLabel tk).2}" | TokenParser.proof pr => let s := { s with tokp := default } if tk.eqArray "$.".toAscii then s.finishProof pr else s.feedProof tk pr inductive OldToken | this (off : Nat) | old (base off : Nat) (arr : ByteArray) inductive FeedState | ws : FeedState | token : OldToken β†’ FeedState def updateLine (s : ParserState) (i : Nat) (c : UInt8) : ParserState := if c == '\n'.toUInt8 then { s with line := s.line + 1, linepos := i + 1 } else s def feed (base : Nat) (arr : ByteArray) (i : Nat) (rs : FeedState) (s : ParserState) : ParserState := if h : i < arr.size then let c := arr.get ⟨i, h⟩ if isWhitespace c then match rs with | FeedState.ws => let s := s.updateLine (base + i) c feed base arr (i+1) FeedState.ws s | FeedState.token ot => let s := match ot with | OldToken.this off => s.feedToken (base + off) ⟨arr, off, i - off⟩ | OldToken.old base off arr' => s.feedToken (base + off) ⟨arr.copySlice 0 arr' arr'.size i false, off, arr'.size - off + i⟩ let s : ParserState := s.updateLine (base + i) c if let some ⟨e, _⟩ := s.db.error? then { s with db := { s.db with error? := some ⟨e, i+1⟩ } } else feed base arr (i+1) FeedState.ws s else let rs := if let FeedState.ws := rs then FeedState.token (OldToken.this i) else rs feed base arr (i+1) rs s else { s with charp := match rs with | FeedState.ws => CharParser.ws | FeedState.token ot => match ot with | OldToken.this off => CharParser.token base ⟨arr, off⟩ | OldToken.old base off arr' => CharParser.token base ⟨arr' ++ arr, off⟩ } termination_by _ => arr.size - i def feedAll (s : ParserState) (base : Nat) (arr : ByteArray) : ParserState := match s.charp with | CharParser.ws => s.feed base arr 0 FeedState.ws | CharParser.token base' ⟨arr', off⟩ => let s := { s with charp := default } s.feed base arr 0 (FeedState.token (OldToken.old base' off arr')) def done (s : ParserState) (base : Nat) : DB := Id.run do let mut s := s if let CharParser.token pos tk := s.charp then s := s.feedToken pos tk.toSlice let base := s.mkPos base let { db := db, tokp := tokp, ..} := s match tokp with | TokenParser.start => db | TokenParser.comment _ => db.mkError base "unclosed comment" | TokenParser.const => db.mkError base "unclosed $c" | TokenParser.var => db.mkError base "unclosed $v" | TokenParser.djvars _ => db.mkError base "unclosed $d" | TokenParser.math _ p => match p.k with | TokensKind.float => db.mkError base "unclosed $f" | TokensKind.ess => db.mkError base "unclosed $e" | TokensKind.ax => db.mkError base "unclosed $a" | TokensKind.thm => db.mkError base "unclosed $p" | TokenParser.label pos _ => db.mkError pos "not a command" | TokenParser.proof _ => db.mkError base "unclosed $p proof" end ParserState partial def check (fname : String) : IO DB := do let h ← Handle.mk fname IO.FS.Mode.read let rec loop (s : ParserState) (base : Nat) : IO DB := do let buf ← h.read 1024 if buf.isEmpty then return s.done base else let s := s.feedAll base buf if s.db.error?.isSome then return s.db else loop s (base + buf.size) loop default 0
1af2ef631c97d32af4dcee11cd88f361dcf881d5
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/1657.lean
7da8dddd02d52ea994fa8ba909bd25bd435239d8
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
556
lean
namespace X1 inductive wrap (A : Type) : Type | mk : A β†’ wrap inductive type : Type | mk : list type β†’ type | refinement : type β†’ (bool β†’ type) β†’ type end X1 namespace X2 inductive type : Type | mk : (bool β†’ type) β†’ list type β†’ β„• β†’ type | refinement : list type β†’ type β†’ β„• β†’ (βˆ€ (b : bool), b = tt β†’ type) β†’ (bool β†’ type) β†’ type end X2 namespace X3 mutual inductive type, term with type : Type | fn : list type β†’ type | refinement : type β†’ (string β†’ type) β†’ type with term : Type | apply : term end X3
12f3fe95c0d1af78016947a406acd5a7d3a819e9
07c6143268cfb72beccd1cc35735d424ebcb187b
/src/set_theory/cardinal.lean
6cce49fdea09adc801a8ad3ebd38313bb81d3bec
[ "Apache-2.0" ]
permissive
khoek/mathlib
bc49a842910af13a3c372748310e86467d1dc766
aa55f8b50354b3e11ba64792dcb06cccb2d8ee28
refs/heads/master
1,588,232,063,837
1,587,304,803,000
1,587,304,803,000
176,688,517
0
0
Apache-2.0
1,553,070,585,000
1,553,070,585,000
null
UTF-8
Lean
false
false
45,216
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes HΓΆlzl, Mario Carneiro -/ import data.set.countable data.quot logic.function set_theory.schroeder_bernstein import data.fintype.card /-! # Cardinal Numbers We define cardinal numbers as a quotient of types under the equivalence relation of equinumerity. We define the order on cardinal numbers, define omega, and do basic cardinal arithmetic: addition, multiplication, power, cardinal successor, minimum, supremum, infinitary sums and products ## Implementation notes * There is a type of cardinal numbers in every universe level: `cardinal.{u} : Type (u + 1)` is the quotient of types in `Type u`. There is a lift operation lifting cardinal numbers to a higher level. * Cardinal arithmetic specifically for infinite cardinals (like `ΞΊ * ΞΊ = ΞΊ`) is in the file `set_theory/ordinal.lean`, because concepts from that file are used in the proof. ## References * <https://en.wikipedia.org/wiki/Cardinal_number> ## Tags cardinal number, cardinal arithmetic, cardinal exponentiation, omega -/ open function set open_locale classical universes u v w x variables {Ξ± Ξ² : Type u} /-- The equivalence relation on types given by equivalence (bijective correspondence) of types. Quotienting by this equivalence relation gives the cardinal numbers. -/ instance cardinal.is_equivalent : setoid (Type u) := { r := λα Ξ², nonempty (Ξ± ≃ Ξ²), iseqv := ⟨λα, ⟨equiv.refl α⟩, λα Ξ² ⟨e⟩, ⟨e.symm⟩, λα Ξ² Ξ³ ⟨eβ‚βŸ© ⟨eβ‚‚βŸ©, ⟨e₁.trans eβ‚‚βŸ©βŸ© } /-- `cardinal.{u}` is the type of cardinal numbers in `Type u`, defined as the quotient of `Type u` by existence of an equivalence (a bijection with explicit inverse). -/ def cardinal : Type (u + 1) := quotient cardinal.is_equivalent namespace cardinal /-- The cardinal number of a type -/ def mk : Type u β†’ cardinal := quotient.mk localized "notation `#` := cardinal.mk" in cardinal protected lemma eq : mk Ξ± = mk Ξ² ↔ nonempty (Ξ± ≃ Ξ²) := quotient.eq @[simp] theorem mk_def (Ξ± : Type u) : @eq cardinal ⟦α⟧ (mk Ξ±) := rfl @[simp] theorem mk_out (c : cardinal) : mk (c.out) = c := quotient.out_eq _ /-- We define the order on cardinal numbers by `mk Ξ± ≀ mk Ξ²` if and only if there exists an embedding (injective function) from Ξ± to Ξ². -/ instance : has_le cardinal.{u} := ⟨λq₁ qβ‚‚, quotient.lift_onβ‚‚ q₁ qβ‚‚ (λα Ξ², nonempty $ Ξ± β†ͺ Ξ²) $ assume Ξ± Ξ² Ξ³ Ξ΄ ⟨eβ‚βŸ© ⟨eβ‚‚βŸ©, propext ⟨assume ⟨e⟩, ⟨e.congr e₁ eβ‚‚βŸ©, assume ⟨e⟩, ⟨e.congr e₁.symm eβ‚‚.symm⟩⟩⟩ theorem mk_le_of_injective {Ξ± Ξ² : Type u} {f : Ξ± β†’ Ξ²} (hf : injective f) : mk Ξ± ≀ mk Ξ² := ⟨⟨f, hf⟩⟩ theorem mk_le_of_surjective {Ξ± Ξ² : Type u} {f : Ξ± β†’ Ξ²} (hf : surjective f) : mk Ξ² ≀ mk Ξ± := ⟨embedding.of_surjective hf⟩ theorem le_mk_iff_exists_set {c : cardinal} {Ξ± : Type u} : c ≀ mk Ξ± ↔ βˆƒ p : set Ξ±, mk p = c := ⟨quotient.induction_on c $ Ξ» Ξ² ⟨⟨f, hf⟩⟩, ⟨set.range f, eq.symm $ quot.sound ⟨equiv.set.range f hf⟩⟩, Ξ» ⟨p, e⟩, e β–Έ ⟨⟨subtype.val, Ξ» a b, subtype.eq⟩⟩⟩ theorem out_embedding {c c' : cardinal} : c ≀ c' ↔ nonempty (c.out β†ͺ c'.out) := by { transitivity _, rw [←quotient.out_eq c, ←quotient.out_eq c'], refl } instance : linear_order cardinal.{u} := { le := (≀), le_refl := by rintros ⟨α⟩; exact ⟨embedding.refl _⟩, le_trans := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨eβ‚βŸ© ⟨eβ‚‚βŸ©; exact ⟨e₁.trans eβ‚‚βŸ©, le_antisymm := by rintros ⟨α⟩ ⟨β⟩ ⟨eβ‚βŸ© ⟨eβ‚‚βŸ©; exact quotient.sound (e₁.antisymm eβ‚‚), le_total := by rintros ⟨α⟩ ⟨β⟩; exact embedding.total } noncomputable instance : decidable_linear_order cardinal.{u} := classical.DLO _ noncomputable instance : distrib_lattice cardinal.{u} := by apply_instance -- short-circuit type class inference instance : has_zero cardinal.{u} := ⟨⟦pempty⟧⟩ instance : inhabited cardinal.{u} := ⟨0⟩ theorem ne_zero_iff_nonempty {Ξ± : Type u} : mk Ξ± β‰  0 ↔ nonempty Ξ± := not_iff_comm.1 ⟨λ h, quotient.sound ⟨(equiv.empty_of_not_nonempty h).trans equiv.empty_equiv_pempty⟩, Ξ» e, let ⟨h⟩ := quotient.exact e in Ξ» ⟨a⟩, (h a).elim⟩ instance : has_one cardinal.{u} := ⟨⟦punit⟧⟩ instance : zero_ne_one_class cardinal.{u} := { zero := 0, one := 1, zero_ne_one := ne.symm $ ne_zero_iff_nonempty.2 ⟨punit.star⟩ } theorem le_one_iff_subsingleton {Ξ± : Type u} : mk Ξ± ≀ 1 ↔ subsingleton Ξ± := ⟨λ ⟨f⟩, ⟨λ a b, f.inj (subsingleton.elim _ _)⟩, Ξ» ⟨h⟩, ⟨⟨λ a, punit.star, Ξ» a b _, h _ _⟩⟩⟩ instance : has_add cardinal.{u} := ⟨λq₁ qβ‚‚, quotient.lift_onβ‚‚ q₁ qβ‚‚ (λα Ξ², mk (Ξ± βŠ• Ξ²)) $ assume Ξ± Ξ² Ξ³ Ξ΄ ⟨eβ‚βŸ© ⟨eβ‚‚βŸ©, quotient.sound ⟨equiv.sum_congr e₁ eβ‚‚βŸ©βŸ© @[simp] theorem add_def (Ξ± Ξ²) : mk Ξ± + mk Ξ² = mk (Ξ± βŠ• Ξ²) := rfl instance : has_mul cardinal.{u} := ⟨λq₁ qβ‚‚, quotient.lift_onβ‚‚ q₁ qβ‚‚ (λα Ξ², mk (Ξ± Γ— Ξ²)) $ assume Ξ± Ξ² Ξ³ Ξ΄ ⟨eβ‚βŸ© ⟨eβ‚‚βŸ©, quotient.sound ⟨equiv.prod_congr e₁ eβ‚‚βŸ©βŸ© @[simp] theorem mul_def (Ξ± Ξ² : Type u) : mk Ξ± * mk Ξ² = mk (Ξ± Γ— Ξ²) := rfl private theorem add_comm (a b : cardinal.{u}) : a + b = b + a := quotient.induction_onβ‚‚ a b $ assume Ξ± Ξ², quotient.sound ⟨equiv.sum_comm Ξ± β⟩ private theorem mul_comm (a b : cardinal.{u}) : a * b = b * a := quotient.induction_onβ‚‚ a b $ assume Ξ± Ξ², quotient.sound ⟨equiv.prod_comm Ξ± β⟩ private theorem zero_add (a : cardinal.{u}) : 0 + a = a := quotient.induction_on a $ assume Ξ±, quotient.sound ⟨equiv.pempty_sum α⟩ private theorem zero_mul (a : cardinal.{u}) : 0 * a = 0 := quotient.induction_on a $ assume Ξ±, quotient.sound ⟨equiv.pempty_prod α⟩ private theorem one_mul (a : cardinal.{u}) : 1 * a = a := quotient.induction_on a $ assume Ξ±, quotient.sound ⟨equiv.punit_prod α⟩ private theorem left_distrib (a b c : cardinal.{u}) : a * (b + c) = a * b + a * c := quotient.induction_on₃ a b c $ assume Ξ± Ξ² Ξ³, quotient.sound ⟨equiv.prod_sum_distrib Ξ± Ξ² γ⟩ instance : comm_semiring cardinal.{u} := { zero := 0, one := 1, add := (+), mul := (*), zero_add := zero_add, add_zero := assume a, by rw [add_comm a 0, zero_add a], add_assoc := Ξ»a b c, quotient.induction_on₃ a b c $ assume Ξ± Ξ² Ξ³, quotient.sound ⟨equiv.sum_assoc Ξ± Ξ² γ⟩, add_comm := add_comm, zero_mul := zero_mul, mul_zero := assume a, by rw [mul_comm a 0, zero_mul a], one_mul := one_mul, mul_one := assume a, by rw [mul_comm a 1, one_mul a], mul_assoc := Ξ»a b c, quotient.induction_on₃ a b c $ assume Ξ± Ξ² Ξ³, quotient.sound ⟨equiv.prod_assoc Ξ± Ξ² γ⟩, mul_comm := mul_comm, left_distrib := left_distrib, right_distrib := assume a b c, by rw [mul_comm (a + b) c, left_distrib c a b, mul_comm c a, mul_comm c b] } /-- The cardinal exponential. `mk Ξ± ^ mk Ξ²` is the cardinal of `Ξ² β†’ Ξ±`. -/ protected def power (a b : cardinal.{u}) : cardinal.{u} := quotient.lift_onβ‚‚ a b (λα Ξ², mk (Ξ² β†’ Ξ±)) $ assume α₁ Ξ±β‚‚ β₁ Ξ²β‚‚ ⟨eβ‚βŸ© ⟨eβ‚‚βŸ©, quotient.sound ⟨equiv.arrow_congr eβ‚‚ eβ‚βŸ© instance : has_pow cardinal cardinal := ⟨cardinal.power⟩ local infixr ^ := @has_pow.pow cardinal cardinal cardinal.has_pow @[simp] theorem power_def (Ξ± Ξ²) : mk Ξ± ^ mk Ξ² = mk (Ξ² β†’ Ξ±) := rfl @[simp] theorem power_zero {a : cardinal} : a ^ 0 = 1 := quotient.induction_on a $ assume Ξ±, quotient.sound ⟨equiv.pempty_arrow_equiv_punit α⟩ @[simp] theorem power_one {a : cardinal} : a ^ 1 = a := quotient.induction_on a $ assume Ξ±, quotient.sound ⟨equiv.punit_arrow_equiv α⟩ @[simp] theorem one_power {a : cardinal} : 1 ^ a = 1 := quotient.induction_on a $ assume Ξ±, quotient.sound ⟨equiv.arrow_punit_equiv_punit α⟩ @[simp] theorem prop_eq_two : mk (ulift Prop) = 2 := quot.sound ⟨equiv.ulift.trans $ equiv.Prop_equiv_bool.trans equiv.bool_equiv_punit_sum_punit⟩ @[simp] theorem zero_power {a : cardinal} : a β‰  0 β†’ 0 ^ a = 0 := quotient.induction_on a $ assume Ξ± heq, nonempty.rec_on (ne_zero_iff_nonempty.1 heq) $ assume a, quotient.sound ⟨equiv.equiv_pempty $ assume f, pempty.rec (Ξ» _, false) (f a)⟩ theorem power_ne_zero {a : cardinal} (b) : a β‰  0 β†’ a ^ b β‰  0 := quotient.induction_onβ‚‚ a b $ Ξ» Ξ± Ξ² h, let ⟨a⟩ := ne_zero_iff_nonempty.1 h in ne_zero_iff_nonempty.2 ⟨λ _, a⟩ theorem mul_power {a b c : cardinal} : (a * b) ^ c = a ^ c * b ^ c := quotient.induction_on₃ a b c $ assume Ξ± Ξ² Ξ³, quotient.sound ⟨equiv.arrow_prod_equiv_prod_arrow Ξ± Ξ² γ⟩ theorem power_add {a b c : cardinal} : a ^ (b + c) = a ^ b * a ^ c := quotient.induction_on₃ a b c $ assume Ξ± Ξ² Ξ³, quotient.sound ⟨equiv.sum_arrow_equiv_prod_arrow Ξ² Ξ³ α⟩ theorem power_mul {a b c : cardinal} : (a ^ b) ^ c = a ^ (b * c) := by rw [_root_.mul_comm b c]; from (quotient.induction_on₃ a b c $ assume Ξ± Ξ² Ξ³, quotient.sound ⟨equiv.arrow_arrow_equiv_prod_arrow Ξ³ Ξ² α⟩) @[simp] lemma pow_cast_right (ΞΊ : cardinal.{u}) : βˆ€ n : β„•, (ΞΊ ^ (↑n : cardinal.{u})) = @has_pow.pow _ _ monoid.has_pow ΞΊ n | 0 := by simp | (_+1) := by rw [nat.cast_succ, power_add, power_one, _root_.mul_comm, pow_succ, pow_cast_right] section order_properties open sum theorem zero_le : βˆ€(a : cardinal), 0 ≀ a := by rintro ⟨α⟩; exact ⟨embedding.of_not_nonempty $ Ξ» ⟨a⟩, a.elim⟩ theorem le_zero (a : cardinal) : a ≀ 0 ↔ a = 0 := by simp [le_antisymm_iff, zero_le] theorem pos_iff_ne_zero {o : cardinal} : 0 < o ↔ o β‰  0 := by simp [lt_iff_le_and_ne, eq_comm, zero_le] theorem zero_lt_one : (0 : cardinal) < 1 := lt_of_le_of_ne (zero_le _) zero_ne_one lemma zero_power_le (c : cardinal.{u}) : (0 : cardinal.{u}) ^ c ≀ 1 := by { by_cases h : c = 0, rw [h, power_zero], rw [zero_power h], apply zero_le } theorem add_le_add : βˆ€{a b c d : cardinal}, a ≀ b β†’ c ≀ d β†’ a + c ≀ b + d := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨eβ‚βŸ© ⟨eβ‚‚βŸ©; exact ⟨embedding.sum_congr e₁ eβ‚‚βŸ© theorem add_le_add_left (a) {b c : cardinal} : b ≀ c β†’ a + b ≀ a + c := add_le_add (le_refl _) theorem add_le_add_right {a b : cardinal} (c) (h : a ≀ b) : a + c ≀ b + c := add_le_add h (le_refl _) theorem le_add_right (a b : cardinal) : a ≀ a + b := by simpa using add_le_add_left a (zero_le b) theorem le_add_left (a b : cardinal) : a ≀ b + a := by simpa using add_le_add_right a (zero_le b) theorem mul_le_mul : βˆ€{a b c d : cardinal}, a ≀ b β†’ c ≀ d β†’ a * c ≀ b * d := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨eβ‚βŸ© ⟨eβ‚‚βŸ©; exact ⟨embedding.prod_congr e₁ eβ‚‚βŸ© theorem mul_le_mul_left (a) {b c : cardinal} : b ≀ c β†’ a * b ≀ a * c := mul_le_mul (le_refl _) theorem mul_le_mul_right {a b : cardinal} (c) (h : a ≀ b) : a * c ≀ b * c := mul_le_mul h (le_refl _) theorem power_le_power_left : βˆ€{a b c : cardinal}, a β‰  0 β†’ b ≀ c β†’ a ^ b ≀ a ^ c := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ hΞ± ⟨e⟩; exact let ⟨a⟩ := ne_zero_iff_nonempty.1 hΞ± in ⟨@embedding.arrow_congr_right _ _ _ ⟨a⟩ e⟩ theorem power_le_max_power_one {a b c : cardinal} (h : b ≀ c) : a ^ b ≀ max (a ^ c) 1 := begin by_cases ha : a = 0, simp [ha, zero_power_le], exact le_trans (power_le_power_left ha h) (le_max_left _ _) end theorem power_le_power_right {a b c : cardinal} : a ≀ b β†’ a ^ c ≀ b ^ c := quotient.induction_on₃ a b c $ assume Ξ± Ξ² Ξ³ ⟨e⟩, ⟨embedding.arrow_congr_left e⟩ theorem le_iff_exists_add {a b : cardinal} : a ≀ b ↔ βˆƒ c, b = a + c := ⟨quotient.induction_onβ‚‚ a b $ Ξ» Ξ± Ξ² ⟨⟨f, hf⟩⟩, have (Ξ± βŠ• β†₯-range f) ≃ Ξ², from (equiv.sum_congr (equiv.set.range f hf) (equiv.refl _)).trans $ (equiv.set.sum_compl (range f)), ⟨⟦(-range f : set Ξ²)⟧, quotient.sound ⟨this.symm⟩⟩, Ξ» ⟨c, e⟩, add_zero a β–Έ e.symm β–Έ add_le_add_left _ (zero_le _)⟩ end order_properties instance : order_bot cardinal.{u} := { bot := 0, bot_le := zero_le, ..cardinal.linear_order } instance : canonically_ordered_add_monoid cardinal.{u} := { add_le_add_left := Ξ» a b h c, add_le_add_left _ h, lt_of_add_lt_add_left := Ξ» a b c, lt_imp_lt_of_le_imp_le (add_le_add_left _), le_iff_exists_add := @le_iff_exists_add, ..cardinal.order_bot, ..cardinal.comm_semiring, ..cardinal.linear_order } theorem cantor : βˆ€(a : cardinal.{u}), a < 2 ^ a := by rw ← prop_eq_two; rintros ⟨a⟩; exact ⟨ ⟨⟨λ a b, ⟨a = b⟩, Ξ» a b h, cast (ulift.up.inj (@congr_fun _ _ _ _ h b)).symm rfl⟩⟩, Ξ» ⟨⟨f, hf⟩⟩, cantor_injective (Ξ» s, f (Ξ» a, ⟨s a⟩)) $ Ξ» s t h, by funext a; injection congr_fun (hf h) a⟩ instance : no_top_order cardinal.{u} := { no_top := Ξ» a, ⟨_, cantor a⟩, ..cardinal.linear_order } /-- The minimum cardinal in a family of cardinals (the existence of which is provided by `injective_min`). -/ noncomputable def min {ΞΉ} (I : nonempty ΞΉ) (f : ΞΉ β†’ cardinal) : cardinal := f $ classical.some $ @embedding.injective_min _ (Ξ» i, (f i).out) I theorem min_eq {ΞΉ} (I) (f : ΞΉ β†’ cardinal) : βˆƒ i, min I f = f i := ⟨_, rfl⟩ theorem min_le {ΞΉ I} (f : ΞΉ β†’ cardinal) (i) : min I f ≀ f i := by rw [← mk_out (min I f), ← mk_out (f i)]; exact let ⟨g⟩ := classical.some_spec (@embedding.injective_min _ (Ξ» i, (f i).out) I) in ⟨g i⟩ theorem le_min {ΞΉ I} {f : ΞΉ β†’ cardinal} {a} : a ≀ min I f ↔ βˆ€ i, a ≀ f i := ⟨λ h i, le_trans h (min_le _ _), Ξ» h, let ⟨i, e⟩ := min_eq I f in e.symm β–Έ h i⟩ protected theorem wf : @well_founded cardinal.{u} (<) := ⟨λ a, classical.by_contradiction $ Ξ» h, let ΞΉ := {c :cardinal // Β¬ acc (<) c}, f : ΞΉ β†’ cardinal := subtype.val, ⟨⟨c, hc⟩, hi⟩ := @min_eq ΞΉ ⟨⟨_, h⟩⟩ f in hc (acc.intro _ (Ξ» j ⟨_, h'⟩, classical.by_contradiction $ Ξ» hj, h' $ by have := min_le f ⟨j, hj⟩; rwa hi at this))⟩ instance has_wf : @has_well_founded cardinal.{u} := ⟨(<), cardinal.wf⟩ instance wo : @is_well_order cardinal.{u} (<) := ⟨cardinal.wf⟩ /-- The successor cardinal - the smallest cardinal greater than `c`. This is not the same as `c + 1` except in the case of finite `c`. -/ noncomputable def succ (c : cardinal) : cardinal := @min {c' // c < c'} ⟨⟨_, cantor _⟩⟩ subtype.val theorem lt_succ_self (c : cardinal) : c < succ c := by cases min_eq _ _ with s e; rw [succ, e]; exact s.2 theorem succ_le {a b : cardinal} : succ a ≀ b ↔ a < b := ⟨lt_of_lt_of_le (lt_succ_self _), Ξ» h, by exact min_le _ (subtype.mk b h)⟩ theorem lt_succ {a b : cardinal} : a < succ b ↔ a ≀ b := by rw [← not_le, succ_le, not_lt] theorem add_one_le_succ (c : cardinal) : c + 1 ≀ succ c := begin refine quot.induction_on c (Ξ» Ξ±, _) (lt_succ_self c), refine quot.induction_on (succ (quot.mk setoid.r Ξ±)) (Ξ» Ξ² h, _), cases h.left with f, have : Β¬ surjective f := Ξ» hn, ne_of_lt h (quotient.sound ⟨equiv.of_bijective ⟨f.inj, hn⟩⟩), cases classical.not_forall.1 this with b nex, refine ⟨⟨sum.rec (by exact f) _, _⟩⟩, { exact Ξ» _, b }, { intros a b h, rcases a with a|⟨⟨⟨⟩⟩⟩; rcases b with b|⟨⟨⟨⟩⟩⟩, { rw f.inj h }, { exact nex.elim ⟨_, h⟩ }, { exact nex.elim ⟨_, h.symm⟩ }, { refl } } end lemma succ_ne_zero (c : cardinal) : succ c β‰  0 := by { rw [←pos_iff_ne_zero, lt_succ], apply zero_le } /-- The indexed sum of cardinals is the cardinality of the indexed disjoint union, i.e. sigma type. -/ def sum {ΞΉ} (f : ΞΉ β†’ cardinal) : cardinal := mk Ξ£ i, (f i).out theorem le_sum {ΞΉ} (f : ΞΉ β†’ cardinal) (i) : f i ≀ sum f := by rw ← quotient.out_eq (f i); exact ⟨⟨λ a, ⟨i, a⟩, Ξ» a b h, eq_of_heq $ by injection h⟩⟩ @[simp] theorem sum_mk {ΞΉ} (f : ΞΉ β†’ Type*) : sum (Ξ» i, mk (f i)) = mk (Ξ£ i, f i) := quot.sound ⟨equiv.sigma_congr_right $ Ξ» i, classical.choice $ quotient.exact $ quot.out_eq $ mk (f i)⟩ theorem sum_const (ΞΉ : Type u) (a : cardinal.{u}) : sum (Ξ» _:ΞΉ, a) = mk ΞΉ * a := quotient.induction_on a $ Ξ» Ξ±, by simp; exact quotient.sound ⟨equiv.sigma_equiv_prod _ _⟩ theorem sum_le_sum {ΞΉ} (f g : ΞΉ β†’ cardinal) (H : βˆ€ i, f i ≀ g i) : sum f ≀ sum g := ⟨embedding.sigma_congr_right $ Ξ» i, classical.choice $ by have := H i; rwa [← quot.out_eq (f i), ← quot.out_eq (g i)] at this⟩ /-- The indexed supremum of cardinals is the smallest cardinal above everything in the family. -/ noncomputable def sup {ΞΉ} (f : ΞΉ β†’ cardinal) : cardinal := @min {c // βˆ€ i, f i ≀ c} ⟨⟨sum f, le_sum f⟩⟩ (Ξ» a, a.1) theorem le_sup {ΞΉ} (f : ΞΉ β†’ cardinal) (i) : f i ≀ sup f := by dsimp [sup]; cases min_eq _ _ with c hc; rw hc; exact c.2 i theorem sup_le {ΞΉ} {f : ΞΉ β†’ cardinal} {a} : sup f ≀ a ↔ βˆ€ i, f i ≀ a := ⟨λ h i, le_trans (le_sup _ _) h, Ξ» h, by dsimp [sup]; change a with (⟨a, h⟩:subtype _).1; apply min_le⟩ theorem sup_le_sup {ΞΉ} (f g : ΞΉ β†’ cardinal) (H : βˆ€ i, f i ≀ g i) : sup f ≀ sup g := sup_le.2 $ Ξ» i, le_trans (H i) (le_sup _ _) theorem sup_le_sum {ΞΉ} (f : ΞΉ β†’ cardinal) : sup f ≀ sum f := sup_le.2 $ le_sum _ theorem sum_le_sup {ΞΉ : Type u} (f : ΞΉ β†’ cardinal.{u}) : sum f ≀ mk ΞΉ * sup.{u u} f := by rw ← sum_const; exact sum_le_sum _ _ (le_sup _) theorem sup_eq_zero {ΞΉ} {f : ΞΉ β†’ cardinal} (h : ΞΉ β†’ false) : sup f = 0 := by { rw [←le_zero, sup_le], intro x, exfalso, exact h x } /-- The indexed product of cardinals is the cardinality of the Pi type (dependent product). -/ def prod {ΞΉ : Type u} (f : ΞΉ β†’ cardinal) : cardinal := mk (Ξ  i, (f i).out) @[simp] theorem prod_mk {ΞΉ} (f : ΞΉ β†’ Type*) : prod (Ξ» i, mk (f i)) = mk (Ξ  i, f i) := quot.sound ⟨equiv.Pi_congr_right $ Ξ» i, classical.choice $ quotient.exact $ mk_out $ mk (f i)⟩ theorem prod_const (ΞΉ : Type u) (a : cardinal.{u}) : prod (Ξ» _:ΞΉ, a) = a ^ mk ΞΉ := quotient.induction_on a $ by simp theorem prod_le_prod {ΞΉ} (f g : ΞΉ β†’ cardinal) (H : βˆ€ i, f i ≀ g i) : prod f ≀ prod g := ⟨embedding.Pi_congr_right $ Ξ» i, classical.choice $ by have := H i; rwa [← mk_out (f i), ← mk_out (g i)] at this⟩ theorem prod_ne_zero {ΞΉ} (f : ΞΉ β†’ cardinal) : prod f β‰  0 ↔ βˆ€ i, f i β‰  0 := begin conv in (f _) {rw ← mk_out (f i)}, simp [prod, ne_zero_iff_nonempty, -mk_out, -ne.def], exact ⟨λ ⟨F⟩ i, ⟨F i⟩, Ξ» h, ⟨λ i, classical.choice (h i)⟩⟩, end theorem prod_eq_zero {ΞΉ} (f : ΞΉ β†’ cardinal) : prod f = 0 ↔ βˆƒ i, f i = 0 := not_iff_not.1 $ by simpa using prod_ne_zero f /-- The universe lift operation on cardinals. You can specify the universes explicitly with `lift.{u v} : cardinal.{u} β†’ cardinal.{max u v}` -/ def lift (c : cardinal.{u}) : cardinal.{max u v} := quotient.lift_on c (Ξ» Ξ±, ⟦ulift α⟧) $ Ξ» Ξ± Ξ² ⟨e⟩, quotient.sound ⟨equiv.ulift.trans $ e.trans equiv.ulift.symm⟩ theorem lift_mk (Ξ±) : lift.{u v} (mk Ξ±) = mk (ulift.{v u} Ξ±) := rfl theorem lift_umax : lift.{u (max u v)} = lift.{u v} := funext $ Ξ» a, quot.induction_on a $ Ξ» Ξ±, quotient.sound ⟨equiv.ulift.trans equiv.ulift.symm⟩ theorem lift_id' (a : cardinal) : lift a = a := quot.induction_on a $ Ξ» Ξ±, quot.sound ⟨equiv.ulift⟩ @[simp] theorem lift_id : βˆ€ a, lift.{u u} a = a := lift_id'.{u u} @[simp] theorem lift_lift (a : cardinal) : lift.{(max u v) w} (lift.{u v} a) = lift.{u (max v w)} a := quot.induction_on a $ Ξ» Ξ±, quotient.sound ⟨equiv.ulift.trans $ equiv.ulift.trans equiv.ulift.symm⟩ theorem lift_mk_le {Ξ± : Type u} {Ξ² : Type v} : lift.{u (max v w)} (mk Ξ±) ≀ lift.{v (max u w)} (mk Ξ²) ↔ nonempty (Ξ± β†ͺ Ξ²) := ⟨λ ⟨f⟩, ⟨embedding.congr equiv.ulift equiv.ulift f⟩, Ξ» ⟨f⟩, ⟨embedding.congr equiv.ulift.symm equiv.ulift.symm f⟩⟩ theorem lift_mk_eq {Ξ± : Type u} {Ξ² : Type v} : lift.{u (max v w)} (mk Ξ±) = lift.{v (max u w)} (mk Ξ²) ↔ nonempty (Ξ± ≃ Ξ²) := quotient.eq.trans ⟨λ ⟨f⟩, ⟨equiv.ulift.symm.trans $ f.trans equiv.ulift⟩, Ξ» ⟨f⟩, ⟨equiv.ulift.trans $ f.trans equiv.ulift.symm⟩⟩ @[simp] theorem lift_le {a b : cardinal} : lift a ≀ lift b ↔ a ≀ b := quotient.induction_onβ‚‚ a b $ Ξ» Ξ± Ξ², by rw ← lift_umax; exact lift_mk_le @[simp] theorem lift_inj {a b : cardinal} : lift a = lift b ↔ a = b := by simp [le_antisymm_iff] @[simp] theorem lift_lt {a b : cardinal} : lift a < lift b ↔ a < b := by simp [lt_iff_le_not_le, -not_le] @[simp] theorem lift_zero : lift 0 = 0 := quotient.sound ⟨equiv.ulift.trans equiv.pempty_equiv_pempty⟩ @[simp] theorem lift_one : lift 1 = 1 := quotient.sound ⟨equiv.ulift.trans equiv.punit_equiv_punit⟩ @[simp] theorem lift_add (a b) : lift (a + b) = lift a + lift b := quotient.induction_onβ‚‚ a b $ Ξ» Ξ± Ξ², quotient.sound ⟨equiv.ulift.trans (equiv.sum_congr equiv.ulift equiv.ulift).symm⟩ @[simp] theorem lift_mul (a b) : lift (a * b) = lift a * lift b := quotient.induction_onβ‚‚ a b $ Ξ» Ξ± Ξ², quotient.sound ⟨equiv.ulift.trans (equiv.prod_congr equiv.ulift equiv.ulift).symm⟩ @[simp] theorem lift_power (a b) : lift (a ^ b) = lift a ^ lift b := quotient.induction_onβ‚‚ a b $ Ξ» Ξ± Ξ², quotient.sound ⟨equiv.ulift.trans (equiv.arrow_congr equiv.ulift equiv.ulift).symm⟩ @[simp] theorem lift_two_power (a) : lift (2 ^ a) = 2 ^ lift a := by simp [bit0] @[simp] theorem lift_min {ΞΉ I} (f : ΞΉ β†’ cardinal) : lift (min I f) = min I (lift ∘ f) := le_antisymm (le_min.2 $ Ξ» a, lift_le.2 $ min_le _ a) $ let ⟨i, e⟩ := min_eq I (lift ∘ f) in by rw e; exact lift_le.2 (le_min.2 $ Ξ» j, lift_le.1 $ by have := min_le (lift ∘ f) j; rwa e at this) theorem lift_down {a : cardinal.{u}} {b : cardinal.{max u v}} : b ≀ lift a β†’ βˆƒ a', lift a' = b := quotient.induction_onβ‚‚ a b $ Ξ» Ξ± Ξ², by dsimp; rw [← lift_id (mk Ξ²), ← lift_umax, ← lift_umax.{u v}, lift_mk_le]; exact Ξ» ⟨f⟩, ⟨mk (set.range f), eq.symm $ lift_mk_eq.2 ⟨embedding.equiv_of_surjective (embedding.cod_restrict _ f set.mem_range_self) $ Ξ» ⟨a, ⟨b, e⟩⟩, ⟨b, subtype.eq e⟩⟩⟩ theorem le_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} : b ≀ lift a ↔ βˆƒ a', lift a' = b ∧ a' ≀ a := ⟨λ h, let ⟨a', e⟩ := lift_down h in ⟨a', e, lift_le.1 $ e.symm β–Έ h⟩, Ξ» ⟨a', e, h⟩, e β–Έ lift_le.2 h⟩ theorem lt_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} : b < lift a ↔ βˆƒ a', lift a' = b ∧ a' < a := ⟨λ h, let ⟨a', e⟩ := lift_down (le_of_lt h) in ⟨a', e, lift_lt.1 $ e.symm β–Έ h⟩, Ξ» ⟨a', e, h⟩, e β–Έ lift_lt.2 h⟩ @[simp] theorem lift_succ (a) : lift (succ a) = succ (lift a) := le_antisymm (le_of_not_gt $ Ξ» h, begin rcases lt_lift_iff.1 h with ⟨b, e, h⟩, rw [lt_succ, ← lift_le, e] at h, exact not_lt_of_le h (lt_succ_self _) end) (succ_le.2 $ lift_lt.2 $ lt_succ_self _) @[simp] theorem lift_max {a : cardinal.{u}} {b : cardinal.{v}} : lift.{u (max v w)} a = lift.{v (max u w)} b ↔ lift.{u v} a = lift.{v u} b := calc lift.{u (max v w)} a = lift.{v (max u w)} b ↔ lift.{(max u v) w} (lift.{u v} a) = lift.{(max u v) w} (lift.{v u} b) : by simp ... ↔ lift.{u v} a = lift.{v u} b : lift_inj theorem mk_prod {Ξ± : Type u} {Ξ² : Type v} : mk (Ξ± Γ— Ξ²) = lift.{u v} (mk Ξ±) * lift.{v u} (mk Ξ²) := quotient.sound ⟨equiv.prod_congr (equiv.ulift).symm (equiv.ulift).symm⟩ theorem sum_const_eq_lift_mul (ΞΉ : Type u) (a : cardinal.{v}) : sum (Ξ» _:ΞΉ, a) = lift.{u v} (mk ΞΉ) * lift.{v u} a := begin apply quotient.induction_on a, intro Ξ±, simp only [cardinal.mk_def, cardinal.sum_mk, cardinal.lift_id], convert mk_prod using 1, exact quotient.sound ⟨equiv.sigma_equiv_prod ΞΉ α⟩, end /-- `Ο‰` is the smallest infinite cardinal, also known as β„΅β‚€. -/ def omega : cardinal.{u} := lift (mk β„•) lemma mk_nat : mk nat = omega := (lift_id _).symm theorem omega_ne_zero : omega β‰  0 := ne_zero_iff_nonempty.2 ⟨⟨0⟩⟩ theorem omega_pos : 0 < omega := pos_iff_ne_zero.2 omega_ne_zero @[simp] theorem lift_omega : lift omega = omega := lift_lift _ /- properties about the cast from nat -/ @[simp] theorem mk_fin : βˆ€ (n : β„•), mk (fin n) = n | 0 := quotient.sound ⟨(equiv.pempty_of_not_nonempty $ Ξ» ⟨h⟩, h.elim0)⟩ | (n+1) := by rw [nat.cast_succ, ← mk_fin]; exact quotient.sound (fintype.card_eq.1 $ by simp) @[simp] theorem lift_nat_cast (n : β„•) : lift n = n := by induction n; simp * lemma lift_eq_nat_iff {a : cardinal.{u}} {n : β„•} : lift.{u v} a = n ↔ a = n := by rw [← lift_nat_cast.{u v} n, lift_inj] lemma nat_eq_lift_eq_iff {n : β„•} {a : cardinal.{u}} : (n : cardinal) = lift.{u v} a ↔ (n : cardinal) = a := by rw [← lift_nat_cast.{u v} n, lift_inj] theorem lift_mk_fin (n : β„•) : lift (mk (fin n)) = n := by simp theorem fintype_card (Ξ± : Type u) [fintype Ξ±] : mk Ξ± = fintype.card Ξ± := by rw [← lift_mk_fin.{u}, ← lift_id (mk Ξ±), lift_mk_eq.{u 0 u}]; exact fintype.card_eq.1 (by simp) theorem card_le_of_finset {Ξ±} (s : finset Ξ±) : (s.card : cardinal) ≀ cardinal.mk Ξ± := begin rw (_ : (s.card : cardinal) = cardinal.mk (↑s : set Ξ±)), { exact ⟨function.embedding.subtype _⟩ }, rw [cardinal.fintype_card, fintype.card_coe] end @[simp, norm_cast] theorem nat_cast_pow {m n : β„•} : (↑(pow m n) : cardinal) = m ^ n := by induction n; simp [nat.pow_succ, -_root_.add_comm, power_add, *] @[simp, norm_cast] theorem nat_cast_le {m n : β„•} : (m : cardinal) ≀ n ↔ m ≀ n := by rw [← lift_mk_fin, ← lift_mk_fin, lift_le]; exact ⟨λ ⟨⟨f, hf⟩⟩, begin have : _ = fintype.card _ := finset.card_image_of_injective finset.univ hf, simp at this, rw [← fintype.card_fin n, ← this], exact finset.card_le_of_subset (finset.subset_univ _) end, Ξ» h, ⟨⟨λ i, ⟨i.1, lt_of_lt_of_le i.2 h⟩, Ξ» a b h, have _, from fin.veq_of_eq h, fin.eq_of_veq this⟩⟩⟩ @[simp, norm_cast] theorem nat_cast_lt {m n : β„•} : (m : cardinal) < n ↔ m < n := by simp [lt_iff_le_not_le, -not_le] @[simp, norm_cast] theorem nat_cast_inj {m n : β„•} : (m : cardinal) = n ↔ m = n := by simp [le_antisymm_iff] @[simp, norm_cast, priority 900] theorem nat_succ (n : β„•) : (n.succ : cardinal) = succ n := le_antisymm (add_one_le_succ _) (succ_le.2 $ nat_cast_lt.2 $ nat.lt_succ_self _) @[simp] theorem succ_zero : succ 0 = 1 := by norm_cast theorem cantor' (a) {b : cardinal} (hb : 1 < b) : a < b ^ a := by rw [← succ_le, (by norm_cast : succ 1 = 2)] at hb; exact lt_of_lt_of_le (cantor _) (power_le_power_right hb) theorem one_le_iff_pos {c : cardinal} : 1 ≀ c ↔ 0 < c := by rw [← succ_zero, succ_le] theorem one_le_iff_ne_zero {c : cardinal} : 1 ≀ c ↔ c β‰  0 := by rw [one_le_iff_pos, pos_iff_ne_zero] theorem nat_lt_omega (n : β„•) : (n : cardinal.{u}) < omega := succ_le.1 $ by rw [← nat_succ, ← lift_mk_fin, omega, lift_mk_le.{0 0 u}]; exact ⟨⟨fin.val, Ξ» a b, fin.eq_of_veq⟩⟩ theorem one_lt_omega : 1 < omega := by simpa using nat_lt_omega 1 theorem lt_omega {c : cardinal.{u}} : c < omega ↔ βˆƒ n : β„•, c = n := ⟨λ h, begin rcases lt_lift_iff.1 h with ⟨c, rfl, h'⟩, rcases le_mk_iff_exists_set.1 h'.1 with ⟨S, rfl⟩, suffices : finite S, { cases this, resetI, existsi fintype.card S, rw [← lift_nat_cast.{0 u}, lift_inj, fintype_card S] }, by_contra nf, have P : βˆ€ (n : β„•) (IH : βˆ€ i<n, S), βˆƒ a : S, Β¬ βˆƒ y h, IH y h = a := Ξ» n IH, let g : {i | i < n} β†’ S := Ξ» ⟨i, h⟩, IH i h in classical.not_forall.1 (Ξ» h, nf ⟨fintype.of_surjective g (Ξ» a, subtype.exists.2 (h a))⟩), let F : β„• β†’ S := nat.lt_wf.fix (Ξ» n IH, classical.some (P n IH)), refine not_le_of_lt h' ⟨⟨F, _⟩⟩, suffices : βˆ€ (n : β„•) (m < n), F m β‰  F n, { refine Ξ» m n, not_imp_not.1 (Ξ» ne, _), rcases lt_trichotomy m n with h|h|h, { exact this n m h }, { contradiction }, { exact (this m n h).symm } }, intros n m h, have := classical.some_spec (P n (Ξ» y _, F y)), rw [← show F n = classical.some (P n (Ξ» y _, F y)), from nat.lt_wf.fix_eq (Ξ» n IH, classical.some (P n IH)) n] at this, exact Ξ» e, this ⟨m, h, e⟩, end, Ξ» ⟨n, e⟩, e.symm β–Έ nat_lt_omega _⟩ theorem omega_le {c : cardinal.{u}} : omega ≀ c ↔ βˆ€ n : β„•, (n:cardinal) ≀ c := ⟨λ h n, le_trans (le_of_lt (nat_lt_omega _)) h, Ξ» h, le_of_not_lt $ Ξ» hn, begin rcases lt_omega.1 hn with ⟨n, rfl⟩, exact not_le_of_lt (nat.lt_succ_self _) (nat_cast_le.1 (h (n+1))) end⟩ theorem lt_omega_iff_fintype {Ξ± : Type u} : mk Ξ± < omega ↔ nonempty (fintype Ξ±) := lt_omega.trans ⟨λ ⟨n, e⟩, begin rw [← lift_mk_fin n] at e, cases quotient.exact e with f, exact ⟨fintype.of_equiv _ f.symm⟩ end, Ξ» ⟨_⟩, by exactI ⟨_, fintype_card _⟩⟩ theorem lt_omega_iff_finite {Ξ±} {S : set Ξ±} : mk S < omega ↔ finite S := lt_omega_iff_fintype instance can_lift_cardinal_nat : can_lift cardinal β„• := ⟨ coe, Ξ» x, x < omega, Ξ» x hx, let ⟨n, hn⟩ := lt_omega.mp hx in ⟨n, hn.symm⟩⟩ theorem add_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a + b < omega := match a, b, lt_omega.1 ha, lt_omega.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_add]; apply nat_lt_omega end lemma add_lt_omega_iff {a b : cardinal} : a + b < omega ↔ a < omega ∧ b < omega := ⟨λ h, ⟨lt_of_le_of_lt (le_add_right _ _) h, lt_of_le_of_lt (le_add_left _ _) h⟩, λ⟨h1, h2⟩, add_lt_omega h1 h2⟩ theorem mul_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a * b < omega := match a, b, lt_omega.1 ha, lt_omega.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_mul]; apply nat_lt_omega end lemma mul_lt_omega_iff {a b : cardinal} : a * b < omega ↔ a = 0 ∨ b = 0 ∨ a < omega ∧ b < omega := begin split, { intro h, by_cases ha : a = 0, { left, exact ha }, right, by_cases hb : b = 0, { left, exact hb }, right, rw [← ne, ← one_le_iff_ne_zero] at ha hb, split, { rw [← mul_one a], refine lt_of_le_of_lt (mul_le_mul (le_refl a) hb) h }, { rw [← _root_.one_mul b], refine lt_of_le_of_lt (mul_le_mul ha (le_refl b)) h }}, rintro (rfl|rfl|⟨ha,hb⟩); simp only [*, mul_lt_omega, omega_pos, _root_.zero_mul, mul_zero] end lemma mul_lt_omega_iff_of_ne_zero {a b : cardinal} (ha : a β‰  0) (hb : b β‰  0) : a * b < omega ↔ a < omega ∧ b < omega := by simp [mul_lt_omega_iff, ha, hb] theorem power_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a ^ b < omega := match a, b, lt_omega.1 ha, lt_omega.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat_cast_pow]; apply nat_lt_omega end lemma eq_one_iff_subsingleton_and_nonempty {Ξ± : Type*} : mk Ξ± = 1 ↔ (subsingleton Ξ± ∧ nonempty Ξ±) := calc mk Ξ± = 1 ↔ mk Ξ± ≀ 1 ∧ Β¬mk Ξ± < 1 : eq_iff_le_not_lt ... ↔ subsingleton Ξ± ∧ nonempty Ξ± : begin apply and_congr le_one_iff_subsingleton, push_neg, rw [one_le_iff_ne_zero, ne_zero_iff_nonempty] end theorem infinite_iff {Ξ± : Type u} : infinite Ξ± ↔ omega ≀ mk Ξ± := by rw [←not_lt, lt_omega_iff_fintype, not_nonempty_fintype] lemma countable_iff (s : set Ξ±) : countable s ↔ mk s ≀ omega := begin rw [countable_iff_exists_injective], split, rintro ⟨f, hf⟩, exact ⟨embedding.trans ⟨f, hf⟩ equiv.ulift.symm.to_embedding⟩, rintro ⟨f'⟩, cases embedding.trans f' equiv.ulift.to_embedding with f hf, exact ⟨f, hf⟩ end lemma denumerable_iff {Ξ± : Type u} : nonempty (denumerable Ξ±) ↔ mk Ξ± = omega := ⟨λ⟨h⟩, quotient.sound $ by exactI ⟨ (denumerable.eqv Ξ±).trans equiv.ulift.symm ⟩, Ξ» h, by { cases quotient.exact h with f, exact ⟨denumerable.mk' $ f.trans equiv.ulift⟩ }⟩ lemma mk_int : mk β„€ = omega := denumerable_iff.mp ⟨by apply_instance⟩ lemma mk_pnat : mk β„•+ = omega := denumerable_iff.mp ⟨by apply_instance⟩ lemma two_le_iff : (2 : cardinal) ≀ mk Ξ± ↔ βˆƒx y : Ξ±, x β‰  y := begin split, { rintro ⟨f⟩, refine ⟨f $ sum.inl ⟨⟩, f $ sum.inr ⟨⟩, _⟩, intro h, cases f.2 h }, { rintro ⟨x, y, h⟩, by_contra h', rw [not_le, ←nat.cast_two, nat_succ, lt_succ, nat.cast_one, le_one_iff_subsingleton] at h', apply h, exactI subsingleton.elim _ _ } end lemma two_le_iff' (x : Ξ±) : (2 : cardinal) ≀ mk Ξ± ↔ βˆƒy : Ξ±, x β‰  y := begin rw [two_le_iff], split, { rintro ⟨y, z, h⟩, refine classical.by_cases (Ξ»(h' : x = y), _) (Ξ» h', ⟨y, h'⟩), rw [←h'] at h, exact ⟨z, h⟩ }, { rintro ⟨y, h⟩, exact ⟨x, y, h⟩ } end /-- KΓΆnig's theorem -/ theorem sum_lt_prod {ΞΉ} (f g : ΞΉ β†’ cardinal) (H : βˆ€ i, f i < g i) : sum f < prod g := lt_of_not_ge $ Ξ» ⟨F⟩, begin have : inhabited (Ξ  (i : ΞΉ), (g i).out), { refine ⟨λ i, classical.choice $ ne_zero_iff_nonempty.1 _⟩, rw mk_out, exact ne_of_gt (lt_of_le_of_lt (zero_le _) (H i)) }, resetI, let G := inv_fun F, have sG : surjective G := inv_fun_surjective F.2, choose C hc using show βˆ€ i, βˆƒ b, βˆ€ a, G ⟨i, a⟩ i β‰  b, { assume i, simp only [- not_exists, not_exists.symm, classical.not_forall.symm], refine Ξ» h, not_le_of_lt (H i) _, rw [← mk_out (f i), ← mk_out (g i)], exact ⟨embedding.of_surjective h⟩ }, exact (let ⟨⟨i, a⟩, h⟩ := sG C in hc i a (congr_fun h _)) end @[simp] theorem mk_empty : mk empty = 0 := fintype_card empty @[simp] theorem mk_pempty : mk pempty = 0 := fintype_card pempty @[simp] theorem mk_plift_of_false {p : Prop} (h : Β¬ p) : mk (plift p) = 0 := quotient.sound ⟨equiv.plift.trans $ equiv.equiv_pempty h⟩ theorem mk_unit : mk unit = 1 := (fintype_card unit).trans nat.cast_one @[simp] theorem mk_punit : mk punit = 1 := (fintype_card punit).trans nat.cast_one @[simp] theorem mk_singleton {Ξ± : Type u} (x : Ξ±) : mk ({x} : set Ξ±) = 1 := quotient.sound ⟨equiv.set.singleton x⟩ @[simp] theorem mk_plift_of_true {p : Prop} (h : p) : mk (plift p) = 1 := quotient.sound ⟨equiv.plift.trans $ equiv.prop_equiv_punit h⟩ @[simp] theorem mk_bool : mk bool = 2 := quotient.sound ⟨equiv.bool_equiv_punit_sum_punit⟩ @[simp] theorem mk_Prop : mk Prop = 2 := (quotient.sound ⟨equiv.Prop_equiv_bool⟩ : mk Prop = mk bool).trans mk_bool @[simp] theorem mk_option {Ξ± : Type u} : mk (option Ξ±) = mk Ξ± + 1 := quotient.sound ⟨equiv.option_equiv_sum_punit α⟩ theorem mk_list_eq_sum_pow (Ξ± : Type u) : mk (list Ξ±) = sum (Ξ» n : β„•, (mk Ξ±)^(n:cardinal.{u})) := calc mk (list Ξ±) = mk (Ξ£ n, vector Ξ± n) : quotient.sound ⟨(equiv.sigma_preimage_equiv list.length).symm⟩ ... = mk (Ξ£ n, fin n β†’ Ξ±) : quotient.sound ⟨equiv.sigma_congr_right $ Ξ» n, ⟨vector.nth, vector.of_fn, vector.of_fn_nth, Ξ» f, funext $ vector.nth_of_fn f⟩⟩ ... = mk (Ξ£ n : β„•, ulift.{u} (fin n) β†’ Ξ±) : quotient.sound ⟨equiv.sigma_congr_right $ Ξ» n, equiv.arrow_congr equiv.ulift.symm (equiv.refl Ξ±)⟩ ... = sum (Ξ» n : β„•, (mk Ξ±)^(n:cardinal.{u})) : by simp only [(lift_mk_fin _).symm, lift_mk, power_def, sum_mk] theorem mk_quot_le {Ξ± : Type u} {r : Ξ± β†’ Ξ± β†’ Prop} : mk (quot r) ≀ mk Ξ± := mk_le_of_surjective quot.exists_rep theorem mk_quotient_le {Ξ± : Type u} {s : setoid Ξ±} : mk (quotient s) ≀ mk Ξ± := mk_quot_le theorem mk_subtype_le {Ξ± : Type u} (p : Ξ± β†’ Prop) : mk (subtype p) ≀ mk Ξ± := ⟨embedding.subtype p⟩ theorem mk_subtype_le_of_subset {Ξ± : Type u} {p q : Ξ± β†’ Prop} (h : βˆ€ ⦃x⦄, p x β†’ q x) : mk (subtype p) ≀ mk (subtype q) := ⟨embedding.subtype_map (embedding.refl Ξ±) h⟩ @[simp] theorem mk_emptyc (Ξ± : Type u) : mk (βˆ… : set Ξ±) = 0 := quotient.sound ⟨equiv.set.pempty α⟩ theorem mk_univ {Ξ± : Type u} : mk (@univ Ξ±) = mk Ξ± := quotient.sound ⟨equiv.set.univ α⟩ theorem mk_image_le {Ξ± Ξ² : Type u} {f : Ξ± β†’ Ξ²} {s : set Ξ±} : mk (f '' s) ≀ mk s := mk_le_of_surjective surjective_onto_image theorem mk_image_le_lift {Ξ± : Type u} {Ξ² : Type v} {f : Ξ± β†’ Ξ²} {s : set Ξ±} : lift.{v u} (mk (f '' s)) ≀ lift.{u v} (mk s) := lift_mk_le.{v u 0}.mpr ⟨embedding.of_surjective surjective_onto_image⟩ theorem mk_range_le {Ξ± Ξ² : Type u} {f : Ξ± β†’ Ξ²} : mk (range f) ≀ mk Ξ± := mk_le_of_surjective surjective_onto_range lemma mk_range_eq (f : Ξ± β†’ Ξ²) (h : injective f) : mk (range f) = mk Ξ± := quotient.sound ⟨(equiv.set.range f h).symm⟩ lemma mk_range_eq_of_inj {Ξ± : Type u} {Ξ² : Type v} {f : Ξ± β†’ Ξ²} (hf : injective f) : lift.{v u} (mk (range f)) = lift.{u v} (mk Ξ±) := begin have := (@lift_mk_eq.{v u max u v} (range f) Ξ±).2 ⟨(equiv.set.range f hf).symm⟩, simp only [lift_umax.{u v}, lift_umax.{v u}] at this, exact this end lemma mk_range_eq_lift {Ξ± : Type u} {Ξ² : Type v} {f : Ξ± β†’ Ξ²} (hf : injective f) : lift.{v (max u w)} (# (range f)) = lift.{u (max v w)} (# Ξ±) := lift_mk_eq.mpr ⟨(equiv.set.range f hf).symm⟩ theorem mk_image_eq {Ξ± Ξ² : Type u} {f : Ξ± β†’ Ξ²} {s : set Ξ±} (hf : injective f) : mk (f '' s) = mk s := quotient.sound ⟨(equiv.set.image f s hf).symm⟩ theorem mk_Union_le_sum_mk {Ξ± ΞΉ : Type u} {f : ΞΉ β†’ set Ξ±} : mk (⋃ i, f i) ≀ sum (Ξ» i, mk (f i)) := calc mk (⋃ i, f i) ≀ mk (Ξ£ i, f i) : mk_le_of_surjective (set.surjective_sigma_to_Union f) ... = sum (Ξ» i, mk (f i)) : (sum_mk _).symm theorem mk_Union_eq_sum_mk {Ξ± ΞΉ : Type u} {f : ΞΉ β†’ set Ξ±} (h : βˆ€i j, i β‰  j β†’ disjoint (f i) (f j)) : mk (⋃ i, f i) = sum (Ξ» i, mk (f i)) := calc mk (⋃ i, f i) = mk (Ξ£i, f i) : quot.sound ⟨set.Union_eq_sigma_of_disjoint h⟩ ... = sum (Ξ»i, mk (f i)) : (sum_mk _).symm lemma mk_Union_le {Ξ± ΞΉ : Type u} (f : ΞΉ β†’ set Ξ±) : mk (⋃ i, f i) ≀ mk ΞΉ * cardinal.sup.{u u} (Ξ» i, mk (f i)) := le_trans mk_Union_le_sum_mk (sum_le_sup _) lemma mk_sUnion_le {Ξ± : Type u} (A : set (set Ξ±)) : mk (⋃₀ A) ≀ mk A * cardinal.sup.{u u} (Ξ» s : A, mk s) := by { rw [sUnion_eq_Union], apply mk_Union_le } lemma mk_bUnion_le {ΞΉ Ξ± : Type u} (A : ΞΉ β†’ set Ξ±) (s : set ΞΉ) : mk (⋃(x ∈ s), A x) ≀ mk s * cardinal.sup.{u u} (Ξ» x : s, mk (A x.1)) := by { rw [bUnion_eq_Union], apply mk_Union_le } @[simp] lemma finset_card {Ξ± : Type u} {s : finset Ξ±} : ↑(finset.card s) = mk (↑s : set Ξ±) := by rw [fintype_card, nat_cast_inj, fintype.card_coe] lemma finset_card_lt_omega (s : finset Ξ±) : mk (↑s : set Ξ±) < omega := by { rw [lt_omega_iff_fintype], exact ⟨finset.subtype.fintype s⟩ } theorem mk_union_add_mk_inter {Ξ± : Type u} {S T : set Ξ±} : mk (S βˆͺ T : set Ξ±) + mk (S ∩ T : set Ξ±) = mk S + mk T := quot.sound ⟨equiv.set.union_sum_inter S T⟩ theorem mk_union_of_disjoint {Ξ± : Type u} {S T : set Ξ±} (H : disjoint S T) : mk (S βˆͺ T : set Ξ±) = mk S + mk T := quot.sound ⟨equiv.set.union (disjoint_iff.1 H)⟩ lemma mk_sum_compl {Ξ±} (s : set Ξ±) : #s + #(-s : set Ξ±) = #Ξ± := quotient.sound ⟨equiv.set.sum_compl s⟩ lemma mk_le_mk_of_subset {Ξ±} {s t : set Ξ±} (h : s βŠ† t) : mk s ≀ mk t := ⟨ set.embedding_of_subset h ⟩ lemma mk_subtype_mono {p q : Ξ± β†’ Prop} (h : βˆ€x, p x β†’ q x) : mk {x // p x} ≀ mk {x // q x} := ⟨embedding_of_subset h⟩ lemma mk_set_le (s : set Ξ±) : mk s ≀ mk Ξ± := mk_subtype_le s lemma mk_image_eq_lift {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) (s : set Ξ±) (h : injective f) : lift.{v u} (mk (f '' s)) = lift.{u v} (mk s) := lift_mk_eq.{v u 0}.mpr ⟨(equiv.set.image f s h).symm⟩ lemma mk_image_eq_of_inj_on_lift {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) (s : set Ξ±) (h : inj_on f s) : lift.{v u} (mk (f '' s)) = lift.{u v} (mk s) := lift_mk_eq.{v u 0}.mpr ⟨(equiv.set.image_of_inj_on f s h).symm⟩ lemma mk_image_eq_of_inj_on {Ξ± Ξ² : Type u} (f : Ξ± β†’ Ξ²) (s : set Ξ±) (h : inj_on f s) : mk (f '' s) = mk s := quotient.sound ⟨(equiv.set.image_of_inj_on f s h).symm⟩ lemma mk_subtype_of_equiv {Ξ± Ξ² : Type u} (p : Ξ± β†’ Prop) (e : Ξ± ≃ Ξ²) : mk {a : Ξ± // p a} = mk {b : Ξ² // p (e.symm b)} := quotient.sound ⟨equiv.subtype_equiv_of_subtype' e⟩ lemma mk_sep (s : set Ξ±) (t : Ξ± β†’ Prop) : mk ({ x ∈ s | t x } : set Ξ±) = mk { x : s | t x.1 } := quotient.sound ⟨equiv.set.sep s t⟩ lemma mk_preimage_of_injective_lift {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) (s : set Ξ²) (h : injective f) : lift.{u v} (mk (f ⁻¹' s)) ≀ lift.{v u} (mk s) := begin rw lift_mk_le.{u v 0}, use subtype.coind (Ξ» x, f x.1) (Ξ» x, x.2), apply subtype.coind_injective, exact injective_comp h subtype.val_injective end lemma mk_preimage_of_subset_range_lift {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) (s : set Ξ²) (h : s βŠ† range f) : lift.{v u} (mk s) ≀ lift.{u v} (mk (f ⁻¹' s)) := begin rw lift_mk_le.{v u 0}, refine ⟨⟨_, _⟩⟩, { rintro ⟨y, hy⟩, rcases classical.subtype_of_exists (h hy) with ⟨x, rfl⟩, exact ⟨x, hy⟩ }, rintro ⟨y, hy⟩ ⟨y', hy'⟩, dsimp, rcases classical.subtype_of_exists (h hy) with ⟨x, rfl⟩, rcases classical.subtype_of_exists (h hy') with ⟨x', rfl⟩, simp, intro hxx', rw hxx' end lemma mk_preimage_of_injective_of_subset_range_lift {Ξ² : Type v} (f : Ξ± β†’ Ξ²) (s : set Ξ²) (h : injective f) (h2 : s βŠ† range f) : lift.{u v} (mk (f ⁻¹' s)) = lift.{v u} (mk s) := le_antisymm (mk_preimage_of_injective_lift f s h) (mk_preimage_of_subset_range_lift f s h2) lemma mk_preimage_of_injective (f : Ξ± β†’ Ξ²) (s : set Ξ²) (h : injective f) : mk (f ⁻¹' s) ≀ mk s := by { convert mk_preimage_of_injective_lift.{u u} f s h using 1; rw [lift_id] } lemma mk_preimage_of_subset_range (f : Ξ± β†’ Ξ²) (s : set Ξ²) (h : s βŠ† range f) : mk s ≀ mk (f ⁻¹' s) := by { convert mk_preimage_of_subset_range_lift.{u u} f s h using 1; rw [lift_id] } lemma mk_preimage_of_injective_of_subset_range (f : Ξ± β†’ Ξ²) (s : set Ξ²) (h : injective f) (h2 : s βŠ† range f) : mk (f ⁻¹' s) = mk s := by { convert mk_preimage_of_injective_of_subset_range_lift.{u u} f s h h2 using 1; rw [lift_id] } lemma mk_subset_ge_of_subset_image_lift {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) {s : set Ξ±} {t : set Ξ²} (h : t βŠ† f '' s) : lift.{v u} (mk t) ≀ lift.{u v} (mk ({ x ∈ s | f x ∈ t } : set Ξ±)) := by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range_lift _ _ h using 1, rw [mk_sep], refl } lemma mk_subset_ge_of_subset_image (f : Ξ± β†’ Ξ²) {s : set Ξ±} {t : set Ξ²} (h : t βŠ† f '' s) : mk t ≀ mk ({ x ∈ s | f x ∈ t } : set Ξ±) := by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range _ _ h using 1, rw [mk_sep], refl } theorem le_mk_iff_exists_subset {c : cardinal} {Ξ± : Type u} {s : set Ξ±} : c ≀ mk s ↔ βˆƒ p : set Ξ±, p βŠ† s ∧ mk p = c := begin rw [le_mk_iff_exists_set, ←subtype.exists_set_subtype], apply exists_congr, intro t, rw [mk_image_eq], apply subtype.val_injective end /-- The function Ξ±^{<Ξ²}, defined to be sup_{Ξ³ < Ξ²} Ξ±^Ξ³. We index over {s : set Ξ².out // mk s < Ξ² } instead of {Ξ³ // Ξ³ < Ξ²}, because the latter lives in a higher universe -/ noncomputable def powerlt (Ξ± Ξ² : cardinal.{u}) : cardinal.{u} := sup.{u u} (Ξ»(s : {s : set Ξ².out // mk s < Ξ²}), Ξ± ^ mk.{u} s) infix ` ^< `:80 := powerlt theorem powerlt_aux {c c' : cardinal} (h : c < c') : βˆƒ(s : {s : set c'.out // mk s < c'}), mk s = c := begin cases out_embedding.mp (le_of_lt h) with f, have : mk β†₯(range ⇑f) = c, { rwa [mk_range_eq, mk, quotient.out_eq c], exact f.2 }, exact ⟨⟨range f, by convert h⟩, this⟩ end lemma le_powerlt {c₁ cβ‚‚ c₃ : cardinal} (h : cβ‚‚ < c₃) : c₁ ^ cβ‚‚ ≀ c₁ ^< c₃ := by { rcases powerlt_aux h with ⟨s, rfl⟩, apply le_sup _ s } lemma powerlt_le {c₁ cβ‚‚ c₃ : cardinal} : c₁ ^< cβ‚‚ ≀ c₃ ↔ βˆ€(cβ‚„ < cβ‚‚), c₁ ^ cβ‚„ ≀ c₃ := begin rw [powerlt, sup_le], split, { intros h cβ‚„ hcβ‚„, rcases powerlt_aux hcβ‚„ with ⟨s, rfl⟩, exact h s }, intros h s, exact h _ s.2 end lemma powerlt_le_powerlt_left {a b c : cardinal} (h : b ≀ c) : a ^< b ≀ a ^< c := by { rw [powerlt, sup_le], rintro ⟨s, hs⟩, apply le_powerlt, exact lt_of_lt_of_le hs h } lemma powerlt_succ {c₁ cβ‚‚ : cardinal} (h : c₁ β‰  0) : c₁ ^< cβ‚‚.succ = c₁ ^ cβ‚‚ := begin apply le_antisymm, { rw powerlt_le, intros c₃ h2, apply power_le_power_left h, rwa [←lt_succ] }, { apply le_powerlt, apply lt_succ_self } end lemma powerlt_max {c₁ cβ‚‚ c₃ : cardinal} : c₁ ^< max cβ‚‚ c₃ = max (c₁ ^< cβ‚‚) (c₁ ^< c₃) := by { cases le_total cβ‚‚ c₃; simp only [max_eq_left, max_eq_right, h, powerlt_le_powerlt_left] } lemma zero_powerlt {a : cardinal} (h : a β‰  0) : 0 ^< a = 1 := begin apply le_antisymm, { rw [powerlt_le], intros c hc, apply zero_power_le }, convert le_powerlt (pos_iff_ne_zero.2 h), rw [power_zero] end lemma powerlt_zero {a : cardinal} : a ^< 0 = 0 := by { apply sup_eq_zero, rintro ⟨x, hx⟩, rw [←not_le] at hx, apply hx, apply zero_le } end cardinal
83031c73cb07040778bd1340b24dcd42226fd050
bb31430994044506fa42fd667e2d556327e18dfe
/src/ring_theory/polynomial/cyclotomic/basic.lean
7ec13bdc53eedfbccd2f67e4cdfff5b18caab9f7
[ "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
47,237
lean
/- Copyright (c) 2020 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import algebra.ne_zero import algebra.polynomial.big_operators import analysis.complex.roots_of_unity import data.polynomial.lifts import data.polynomial.splits import data.zmod.algebra import field_theory.ratfunc import field_theory.separable import number_theory.arithmetic_function import ring_theory.roots_of_unity /-! # Cyclotomic polynomials. For `n : β„•` and an integral domain `R`, we define a modified version of the `n`-th cyclotomic polynomial with coefficients in `R`, denoted `cyclotomic' n R`, as `∏ (X - ΞΌ)`, where `ΞΌ` varies over the primitive `n`th roots of unity. If there is a primitive `n`th root of unity in `R` then this the standard definition. We then define the standard cyclotomic polynomial `cyclotomic n R` with coefficients in any ring `R`. ## Main definition * `cyclotomic n R` : the `n`-th cyclotomic polynomial with coefficients in `R`. ## Main results * `int_coeff_of_cycl` : If there is a primitive `n`-th root of unity in `K`, then `cyclotomic' n K` comes from a polynomial with integer coefficients. * `deg_of_cyclotomic` : The degree of `cyclotomic n` is `totient n`. * `prod_cyclotomic_eq_X_pow_sub_one` : `X ^ n - 1 = ∏ (cyclotomic i)`, where `i` divides `n`. * `cyclotomic_eq_prod_X_pow_sub_one_pow_moebius` : The MΓΆbius inversion formula for `cyclotomic n R` over an abstract fraction field for `R[X]`. * `cyclotomic.irreducible` : `cyclotomic n β„€` is irreducible. ## Implementation details Our definition of `cyclotomic' n R` makes sense in any integral domain `R`, but the interesting results hold if there is a primitive `n`-th root of unity in `R`. In particular, our definition is not the standard one unless there is a primitive `n`th root of unity in `R`. For example, `cyclotomic' 3 β„€ = 1`, since there are no primitive cube roots of unity in `β„€`. The main example is `R = β„‚`, we decided to work in general since the difficulties are essentially the same. To get the standard cyclotomic polynomials, we use `int_coeff_of_cycl`, with `R = β„‚`, to get a polynomial with integer coefficients and then we map it to `R[X]`, for any ring `R`. To prove `cyclotomic.irreducible`, the irreducibility of `cyclotomic n β„€`, we show in `cyclotomic_eq_minpoly` that `cyclotomic n β„€` is the minimal polynomial of any `n`-th primitive root of unity `ΞΌ : K`, where `K` is a field of characteristic `0`. -/ open_locale classical big_operators polynomial noncomputable theory universe u namespace polynomial section cyclotomic' section is_domain variables {R : Type*} [comm_ring R] [is_domain R] /-- The modified `n`-th cyclotomic polynomial with coefficients in `R`, it is the usual cyclotomic polynomial if there is a primitive `n`-th root of unity in `R`. -/ def cyclotomic' (n : β„•) (R : Type*) [comm_ring R] [is_domain R] : R[X] := ∏ ΞΌ in primitive_roots n R, (X - C ΞΌ) /-- The zeroth modified cyclotomic polyomial is `1`. -/ @[simp] lemma cyclotomic'_zero (R : Type*) [comm_ring R] [is_domain R] : cyclotomic' 0 R = 1 := by simp only [cyclotomic', finset.prod_empty, primitive_roots_zero] /-- The first modified cyclotomic polyomial is `X - 1`. -/ @[simp] lemma cyclotomic'_one (R : Type*) [comm_ring R] [is_domain R] : cyclotomic' 1 R = X - 1 := begin simp only [cyclotomic', finset.prod_singleton, ring_hom.map_one, is_primitive_root.primitive_roots_one] end /-- The second modified cyclotomic polyomial is `X + 1` if the characteristic of `R` is not `2`. -/ @[simp] lemma cyclotomic'_two (R : Type*) [comm_ring R] [is_domain R] (p : β„•) [char_p R p] (hp : p β‰  2) : cyclotomic' 2 R = X + 1 := begin rw [cyclotomic'], have prim_root_two : primitive_roots 2 R = {(-1 : R)}, { simp only [finset.eq_singleton_iff_unique_mem, mem_primitive_roots two_pos], exact ⟨is_primitive_root.neg_one p hp, Ξ» x, is_primitive_root.eq_neg_one_of_two_right⟩ }, simp only [prim_root_two, finset.prod_singleton, ring_hom.map_neg, ring_hom.map_one, sub_neg_eq_add] end /-- `cyclotomic' n R` is monic. -/ lemma cyclotomic'.monic (n : β„•) (R : Type*) [comm_ring R] [is_domain R] : (cyclotomic' n R).monic := monic_prod_of_monic _ _ $ Ξ» z hz, monic_X_sub_C _ /-- `cyclotomic' n R` is different from `0`. -/ lemma cyclotomic'_ne_zero (n : β„•) (R : Type*) [comm_ring R] [is_domain R] : cyclotomic' n R β‰  0 := (cyclotomic'.monic n R).ne_zero /-- The natural degree of `cyclotomic' n R` is `totient n` if there is a primitive root of unity in `R`. -/ lemma nat_degree_cyclotomic' {ΞΆ : R} {n : β„•} (h : is_primitive_root ΞΆ n) : (cyclotomic' n R).nat_degree = nat.totient n := begin rw [cyclotomic'], rw nat_degree_prod (primitive_roots n R) (Ξ» (z : R), (X - C z)), simp only [is_primitive_root.card_primitive_roots h, mul_one, nat_degree_X_sub_C, nat.cast_id, finset.sum_const, nsmul_eq_mul], intros z hz, exact X_sub_C_ne_zero z end /-- The degree of `cyclotomic' n R` is `totient n` if there is a primitive root of unity in `R`. -/ lemma degree_cyclotomic' {ΞΆ : R} {n : β„•} (h : is_primitive_root ΞΆ n) : (cyclotomic' n R).degree = nat.totient n := by simp only [degree_eq_nat_degree (cyclotomic'_ne_zero n R), nat_degree_cyclotomic' h] /-- The roots of `cyclotomic' n R` are the primitive `n`-th roots of unity. -/ lemma roots_of_cyclotomic (n : β„•) (R : Type*) [comm_ring R] [is_domain R] : (cyclotomic' n R).roots = (primitive_roots n R).val := by { rw cyclotomic', exact roots_prod_X_sub_C (primitive_roots n R) } /-- If there is a primitive `n`th root of unity in `K`, then `X ^ n - 1 = ∏ (X - ΞΌ)`, where `ΞΌ` varies over the `n`-th roots of unity. -/ lemma X_pow_sub_one_eq_prod {ΞΆ : R} {n : β„•} (hpos : 0 < n) (h : is_primitive_root ΞΆ n) : X ^ n - 1 = ∏ ΞΆ in nth_roots_finset n R, (X - C ΞΆ) := begin rw [nth_roots_finset, ← multiset.to_finset_eq (is_primitive_root.nth_roots_nodup h)], simp only [finset.prod_mk, ring_hom.map_one], rw [nth_roots], have hmonic : (X ^ n - C (1 : R)).monic := monic_X_pow_sub_C (1 : R) (ne_of_lt hpos).symm, symmetry, apply prod_multiset_X_sub_C_of_monic_of_roots_card_eq hmonic, rw [@nat_degree_X_pow_sub_C R _ _ n 1, ← nth_roots], exact is_primitive_root.card_nth_roots h end end is_domain section field variables {K : Type*} [field K] /-- `cyclotomic' n K` splits. -/ lemma cyclotomic'_splits (n : β„•) : splits (ring_hom.id K) (cyclotomic' n K) := begin apply splits_prod (ring_hom.id K), intros z hz, simp only [splits_X_sub_C (ring_hom.id K)] end /-- If there is a primitive `n`-th root of unity in `K`, then `X ^ n - 1`splits. -/ lemma X_pow_sub_one_splits {ΞΆ : K} {n : β„•} (h : is_primitive_root ΞΆ n) : splits (ring_hom.id K) (X ^ n - C (1 : K)) := by rw [splits_iff_card_roots, ← nth_roots, is_primitive_root.card_nth_roots h, nat_degree_X_pow_sub_C] /-- If there is a primitive `n`-th root of unity in `K`, then `∏ i in nat.divisors n, cyclotomic' i K = X ^ n - 1`. -/ lemma prod_cyclotomic'_eq_X_pow_sub_one {K : Type*} [comm_ring K] [is_domain K] {ΞΆ : K} {n : β„•} (hpos : 0 < n) (h : is_primitive_root ΞΆ n) : ∏ i in nat.divisors n, cyclotomic' i K = X ^ n - 1 := have hd : (n.divisors : set β„•).pairwise_disjoint (Ξ» k, primitive_roots k K), from Ξ» x hx y hy hne, is_primitive_root.disjoint hne, by simp only [X_pow_sub_one_eq_prod hpos h, cyclotomic', ← finset.prod_bUnion hd, h.nth_roots_one_eq_bUnion_primitive_roots] /-- If there is a primitive `n`-th root of unity in `K`, then `cyclotomic' n K = (X ^ k - 1) /β‚˜ (∏ i in nat.proper_divisors k, cyclotomic' i K)`. -/ lemma cyclotomic'_eq_X_pow_sub_one_div {K : Type*} [comm_ring K] [is_domain K] {ΞΆ : K} {n : β„•} (hpos : 0 < n) (h : is_primitive_root ΞΆ n) : cyclotomic' n K = (X ^ n - 1) /β‚˜ (∏ i in nat.proper_divisors n, cyclotomic' i K) := begin rw [←prod_cyclotomic'_eq_X_pow_sub_one hpos h, nat.divisors_eq_proper_divisors_insert_self_of_pos hpos, finset.prod_insert nat.proper_divisors.not_self_mem], have prod_monic : (∏ i in nat.proper_divisors n, cyclotomic' i K).monic, { apply monic_prod_of_monic, intros i hi, exact cyclotomic'.monic i K }, rw (div_mod_by_monic_unique (cyclotomic' n K) 0 prod_monic _).1, simp only [degree_zero, zero_add], refine ⟨by rw mul_comm, _⟩, rw [bot_lt_iff_ne_bot], intro h, exact monic.ne_zero prod_monic (degree_eq_bot.1 h) end /-- If there is a primitive `n`-th root of unity in `K`, then `cyclotomic' n K` comes from a monic polynomial with integer coefficients. -/ lemma int_coeff_of_cyclotomic' {K : Type*} [comm_ring K] [is_domain K] {ΞΆ : K} {n : β„•} (h : is_primitive_root ΞΆ n) : (βˆƒ (P : β„€[X]), map (int.cast_ring_hom K) P = cyclotomic' n K ∧ P.degree = (cyclotomic' n K).degree ∧ P.monic) := begin refine lifts_and_degree_eq_and_monic _ (cyclotomic'.monic n K), induction n using nat.strong_induction_on with k ihk generalizing ΞΆ h, rcases k.eq_zero_or_pos with rfl|hpos, { use 1, simp only [cyclotomic'_zero, coe_map_ring_hom, polynomial.map_one] }, let B : K[X] := ∏ i in nat.proper_divisors k, cyclotomic' i K, have Bmo : B.monic, { apply monic_prod_of_monic, intros i hi, exact (cyclotomic'.monic i K) }, have Bint : B ∈ lifts (int.cast_ring_hom K), { refine subsemiring.prod_mem (lifts (int.cast_ring_hom K)) _, intros x hx, have xsmall := (nat.mem_proper_divisors.1 hx).2, obtain ⟨d, hd⟩ := (nat.mem_proper_divisors.1 hx).1, rw [mul_comm] at hd, exact ihk x xsmall (h.pow hpos hd) }, replace Bint := lifts_and_degree_eq_and_monic Bint Bmo, obtain ⟨B₁, hB₁, hB₁deg, hB₁mo⟩ := Bint, let Q₁ : β„€[X] := (X ^ k - 1) /β‚˜ B₁, have huniq : 0 + B * cyclotomic' k K = X ^ k - 1 ∧ (0 : K[X]).degree < B.degree, { split, { rw [zero_add, mul_comm, ←(prod_cyclotomic'_eq_X_pow_sub_one hpos h), nat.divisors_eq_proper_divisors_insert_self_of_pos hpos], simp only [true_and, finset.prod_insert, not_lt, nat.mem_proper_divisors, dvd_refl] }, rw [degree_zero, bot_lt_iff_ne_bot], intro habs, exact (monic.ne_zero Bmo) (degree_eq_bot.1 habs) }, replace huniq := div_mod_by_monic_unique (cyclotomic' k K) (0 : K[X]) Bmo huniq, simp only [lifts, ring_hom.mem_srange], use Q₁, rw [coe_map_ring_hom, (map_div_by_monic (int.cast_ring_hom K) hB₁mo), hB₁, ← huniq.1], simp end /-- If `K` is of characteristic `0` and there is a primitive `n`-th root of unity in `K`, then `cyclotomic n K` comes from a unique polynomial with integer coefficients. -/ lemma unique_int_coeff_of_cycl {K : Type*} [comm_ring K] [is_domain K] [char_zero K] {ΞΆ : K} {n : β„•+} (h : is_primitive_root ΞΆ n) : (βˆƒ! (P : β„€[X]), map (int.cast_ring_hom K) P = cyclotomic' n K) := begin obtain ⟨P, hP⟩ := int_coeff_of_cyclotomic' h, refine ⟨P, hP.1, Ξ» Q hQ, _⟩, apply (map_injective (int.cast_ring_hom K) int.cast_injective), rw [hP.1, hQ] end end field end cyclotomic' section cyclotomic /-- The `n`-th cyclotomic polynomial with coefficients in `R`. -/ def cyclotomic (n : β„•) (R : Type*) [ring R] : R[X] := if h : n = 0 then 1 else map (int.cast_ring_hom R) ((int_coeff_of_cyclotomic' (complex.is_primitive_root_exp n h)).some) lemma int_cyclotomic_rw {n : β„•} (h : n β‰  0) : cyclotomic n β„€ = (int_coeff_of_cyclotomic' (complex.is_primitive_root_exp n h)).some := begin simp only [cyclotomic, h, dif_neg, not_false_iff], ext i, simp only [coeff_map, int.cast_id, eq_int_cast] end /-- `cyclotomic n R` comes from `cyclotomic n β„€`. -/ lemma map_cyclotomic_int (n : β„•) (R : Type*) [ring R] : map (int.cast_ring_hom R) (cyclotomic n β„€) = cyclotomic n R := begin by_cases hzero : n = 0, { simp only [hzero, cyclotomic, dif_pos, polynomial.map_one] }, simp only [cyclotomic, int_cyclotomic_rw, hzero, ne.def, dif_neg, not_false_iff] end lemma int_cyclotomic_spec (n : β„•) : map (int.cast_ring_hom β„‚) (cyclotomic n β„€) = cyclotomic' n β„‚ ∧ (cyclotomic n β„€).degree = (cyclotomic' n β„‚).degree ∧ (cyclotomic n β„€).monic := begin by_cases hzero : n = 0, { simp only [hzero, cyclotomic, degree_one, monic_one, cyclotomic'_zero, dif_pos, eq_self_iff_true, polynomial.map_one, and_self] }, rw int_cyclotomic_rw hzero, exact (int_coeff_of_cyclotomic' (complex.is_primitive_root_exp n hzero)).some_spec end lemma int_cyclotomic_unique {n : β„•} {P : β„€[X]} (h : map (int.cast_ring_hom β„‚) P = cyclotomic' n β„‚) : P = cyclotomic n β„€ := begin apply map_injective (int.cast_ring_hom β„‚) int.cast_injective, rw [h, (int_cyclotomic_spec n).1] end /-- The definition of `cyclotomic n R` commutes with any ring homomorphism. -/ @[simp] lemma map_cyclotomic (n : β„•) {R S : Type*} [ring R] [ring S] (f : R β†’+* S) : map f (cyclotomic n R) = cyclotomic n S := begin rw [←map_cyclotomic_int n R, ←map_cyclotomic_int n S, map_map], congr end lemma cyclotomic.eval_apply {R S : Type*} (q : R) (n : β„•) [ring R] [ring S] (f : R β†’+* S) : eval (f q) (cyclotomic n S) = f (eval q (cyclotomic n R)) := by rw [← map_cyclotomic n f, eval_map, evalβ‚‚_at_apply] /-- The zeroth cyclotomic polyomial is `1`. -/ @[simp] lemma cyclotomic_zero (R : Type*) [ring R] : cyclotomic 0 R = 1 := by simp only [cyclotomic, dif_pos] /-- The first cyclotomic polyomial is `X - 1`. -/ @[simp] lemma cyclotomic_one (R : Type*) [ring R] : cyclotomic 1 R = X - 1 := begin have hspec : map (int.cast_ring_hom β„‚) (X - 1) = cyclotomic' 1 β„‚, { simp only [cyclotomic'_one, pnat.one_coe, map_X, polynomial.map_one, polynomial.map_sub] }, symmetry, rw [←map_cyclotomic_int, ←(int_cyclotomic_unique hspec)], simp only [map_X, polynomial.map_one, polynomial.map_sub] end /-- `cyclotomic n` is monic. -/ lemma cyclotomic.monic (n : β„•) (R : Type*) [ring R] : (cyclotomic n R).monic := begin rw ←map_cyclotomic_int, exact (int_cyclotomic_spec n).2.2.map _, end /-- `cyclotomic n` is primitive. -/ lemma cyclotomic.is_primitive (n : β„•) (R : Type*) [comm_ring R] : (cyclotomic n R).is_primitive := (cyclotomic.monic n R).is_primitive /-- `cyclotomic n R` is different from `0`. -/ lemma cyclotomic_ne_zero (n : β„•) (R : Type*) [ring R] [nontrivial R] : cyclotomic n R β‰  0 := (cyclotomic.monic n R).ne_zero /-- The degree of `cyclotomic n` is `totient n`. -/ lemma degree_cyclotomic (n : β„•) (R : Type*) [ring R] [nontrivial R] : (cyclotomic n R).degree = nat.totient n := begin rw ←map_cyclotomic_int, rw degree_map_eq_of_leading_coeff_ne_zero (int.cast_ring_hom R) _, { cases n with k, { simp only [cyclotomic, degree_one, dif_pos, nat.totient_zero, with_top.coe_zero]}, rw [←degree_cyclotomic' (complex.is_primitive_root_exp k.succ (nat.succ_ne_zero k))], exact (int_cyclotomic_spec k.succ).2.1 }, simp only [(int_cyclotomic_spec n).right.right, eq_int_cast, monic.leading_coeff, int.cast_one, ne.def, not_false_iff, one_ne_zero] end /-- The natural degree of `cyclotomic n` is `totient n`. -/ lemma nat_degree_cyclotomic (n : β„•) (R : Type*) [ring R] [nontrivial R] : (cyclotomic n R).nat_degree = nat.totient n := by rw [nat_degree, degree_cyclotomic, with_bot.unbot'_coe] /-- The degree of `cyclotomic n R` is positive. -/ lemma degree_cyclotomic_pos (n : β„•) (R : Type*) (hpos : 0 < n) [ring R] [nontrivial R] : 0 < (cyclotomic n R).degree := by { rw degree_cyclotomic n R, exact_mod_cast (nat.totient_pos hpos) } open finset /-- `∏ i in nat.divisors n, cyclotomic i R = X ^ n - 1`. -/ lemma prod_cyclotomic_eq_X_pow_sub_one {n : β„•} (hpos : 0 < n) (R : Type*) [comm_ring R] : ∏ i in nat.divisors n, cyclotomic i R = X ^ n - 1 := begin have integer : ∏ i in nat.divisors n, cyclotomic i β„€ = X ^ n - 1, { apply map_injective (int.cast_ring_hom β„‚) int.cast_injective, simp only [polynomial.map_prod, int_cyclotomic_spec, polynomial.map_pow, map_X, polynomial.map_one, polynomial.map_sub], exact prod_cyclotomic'_eq_X_pow_sub_one hpos (complex.is_primitive_root_exp n hpos.ne') }, simpa only [polynomial.map_prod, map_cyclotomic_int, polynomial.map_sub, polynomial.map_one, polynomial.map_pow, polynomial.map_X] using congr_arg (map (int.cast_ring_hom R)) integer end lemma cyclotomic.dvd_X_pow_sub_one (n : β„•) (R : Type*) [ring R] : (cyclotomic n R) ∣ X ^ n - 1 := begin suffices : cyclotomic n β„€ ∣ X ^ n - 1, { simpa only [map_cyclotomic_int, polynomial.map_sub, polynomial.map_one, polynomial.map_pow, polynomial.map_X] using map_dvd (int.cast_ring_hom R) this }, rcases n.eq_zero_or_pos with rfl | hn, { simp }, rw [← prod_cyclotomic_eq_X_pow_sub_one hn], exact finset.dvd_prod_of_mem _ (n.mem_divisors_self hn.ne') end lemma prod_cyclotomic_eq_geom_sum {n : β„•} (h : 0 < n) (R) [comm_ring R] : ∏ i in n.divisors.erase 1, cyclotomic i R = βˆ‘ i in finset.range n, X ^ i := suffices ∏ i in n.divisors.erase 1, cyclotomic i β„€ = βˆ‘ i in finset.range n, X ^ i, by simpa only [polynomial.map_prod, map_cyclotomic_int, polynomial.map_sum, polynomial.map_pow, polynomial.map_X] using congr_arg (map (int.cast_ring_hom R)) this, by rw [← mul_left_inj' (cyclotomic_ne_zero 1 β„€), prod_erase_mul _ _ (nat.one_mem_divisors.2 h.ne'), cyclotomic_one, geom_sum_mul, prod_cyclotomic_eq_X_pow_sub_one h] /-- If `p` is prime, then `cyclotomic p R = βˆ‘ i in range p, X ^ i`. -/ lemma cyclotomic_prime (R : Type*) [ring R] (p : β„•) [hp : fact p.prime] : cyclotomic p R = βˆ‘ i in finset.range p, X ^ i := begin suffices : cyclotomic p β„€ = βˆ‘ i in range p, X ^ i, { simpa only [map_cyclotomic_int, polynomial.map_sum, polynomial.map_pow, polynomial.map_X] using congr_arg (map (int.cast_ring_hom R)) this }, rw [← prod_cyclotomic_eq_geom_sum hp.out.pos, hp.out.divisors, erase_insert (mem_singleton.not.2 hp.out.ne_one.symm), prod_singleton] end lemma cyclotomic_prime_mul_X_sub_one (R : Type*) [ring R] (p : β„•) [hn : fact (nat.prime p)] : (cyclotomic p R) * (X - 1) = X ^ p - 1 := by rw [cyclotomic_prime, geom_sum_mul] @[simp] lemma cyclotomic_two (R : Type*) [ring R] : cyclotomic 2 R = X + 1 := by simp [cyclotomic_prime] @[simp] lemma cyclotomic_three (R : Type*) [ring R] : cyclotomic 3 R = X ^ 2 + X + 1 := by simp [cyclotomic_prime, sum_range_succ'] lemma cyclotomic_dvd_geom_sum_of_dvd (R) [ring R] {d n : β„•} (hdn : d ∣ n) (hd : d β‰  1) : cyclotomic d R ∣ βˆ‘ i in finset.range n, X ^ i := begin suffices : cyclotomic d β„€ ∣ βˆ‘ i in finset.range n, X ^ i, { simpa only [map_cyclotomic_int, polynomial.map_sum, polynomial.map_pow, polynomial.map_X] using map_dvd (int.cast_ring_hom R) this }, rcases n.eq_zero_or_pos with rfl | hn, { simp }, rw ←prod_cyclotomic_eq_geom_sum hn, apply finset.dvd_prod_of_mem, simp [hd, hdn, hn.ne'] end lemma X_pow_sub_one_mul_prod_cyclotomic_eq_X_pow_sub_one_of_dvd (R) [comm_ring R] {d n : β„•} (h : d ∈ n.proper_divisors) : (X ^ d - 1) * ∏ x in n.divisors \ d.divisors, cyclotomic x R = X ^ n - 1 := begin obtain ⟨hd, hdn⟩ := nat.mem_proper_divisors.mp h, have h0n : 0 < n := pos_of_gt hdn, have h0d : 0 < d := nat.pos_of_dvd_of_pos hd h0n, rw [←prod_cyclotomic_eq_X_pow_sub_one h0d, ←prod_cyclotomic_eq_X_pow_sub_one h0n, mul_comm, finset.prod_sdiff (nat.divisors_subset_of_dvd h0n.ne' hd)] end lemma X_pow_sub_one_mul_cyclotomic_dvd_X_pow_sub_one_of_dvd (R) [comm_ring R] {d n : β„•} (h : d ∈ n.proper_divisors) : (X ^ d - 1) * cyclotomic n R ∣ X ^ n - 1 := begin have hdn := (nat.mem_proper_divisors.mp h).2, use ∏ x in n.proper_divisors \ d.divisors, cyclotomic x R, symmetry, convert X_pow_sub_one_mul_prod_cyclotomic_eq_X_pow_sub_one_of_dvd R h using 1, rw mul_assoc, congr' 1, rw [nat.divisors_eq_proper_divisors_insert_self_of_pos $ pos_of_gt hdn, finset.insert_sdiff_of_not_mem, finset.prod_insert], { exact finset.not_mem_sdiff_of_not_mem_left nat.proper_divisors.not_self_mem }, { exact Ξ» hk, hdn.not_le $ nat.divisor_le hk } end lemma _root_.is_root_of_unity_iff {n : β„•} (h : 0 < n) (R : Type*) [comm_ring R] [is_domain R] {ΞΆ : R} : ΞΆ ^ n = 1 ↔ βˆƒ i ∈ n.divisors, (cyclotomic i R).is_root ΞΆ := by rw [←mem_nth_roots h, nth_roots, mem_roots $ X_pow_sub_C_ne_zero h _, C_1, ←prod_cyclotomic_eq_X_pow_sub_one h, is_root_prod]; apply_instance lemma is_root_of_unity_of_root_cyclotomic {n : β„•} {R} [comm_ring R] {ΞΆ : R} {i : β„•} (hi : i ∈ n.divisors) (h : (cyclotomic i R).is_root ΞΆ) : ΞΆ ^ n = 1 := begin rcases n.eq_zero_or_pos with rfl | hn, { exact pow_zero _ }, have := congr_arg (eval ΞΆ) (prod_cyclotomic_eq_X_pow_sub_one hn R).symm, rw [eval_sub, eval_pow, eval_X, eval_one] at this, convert eq_add_of_sub_eq' this, convert (add_zero _).symm, apply eval_eq_zero_of_dvd_of_eval_eq_zero _ h, exact finset.dvd_prod_of_mem _ hi end section arithmetic_function open nat.arithmetic_function open_locale arithmetic_function /-- `cyclotomic n R` can be expressed as a product in a fraction field of `R[X]` using MΓΆbius inversion. -/ lemma cyclotomic_eq_prod_X_pow_sub_one_pow_moebius {n : β„•} (R : Type*) [comm_ring R] [is_domain R] : algebra_map _ (ratfunc R) (cyclotomic n R) = ∏ i in n.divisors_antidiagonal, (algebra_map R[X] _ (X ^ i.snd - 1)) ^ ΞΌ i.fst := begin rcases n.eq_zero_or_pos with rfl | hpos, { simp }, have h : βˆ€ (n : β„•), 0 < n β†’ ∏ i in nat.divisors n, algebra_map _ (ratfunc R) (cyclotomic i R) = algebra_map _ _ (X ^ n - 1), { intros n hn, rw [← prod_cyclotomic_eq_X_pow_sub_one hn R, ring_hom.map_prod] }, rw (prod_eq_iff_prod_pow_moebius_eq_of_nonzero (Ξ» n hn, _) (Ξ» n hn, _)).1 h n hpos; rw [ne.def, is_fraction_ring.to_map_eq_zero_iff], { apply cyclotomic_ne_zero }, { apply monic.ne_zero, apply monic_X_pow_sub_C _ (ne_of_gt hn) } end end arithmetic_function /-- We have `cyclotomic n R = (X ^ k - 1) /β‚˜ (∏ i in nat.proper_divisors k, cyclotomic i K)`. -/ lemma cyclotomic_eq_X_pow_sub_one_div {R : Type*} [comm_ring R] {n : β„•} (hpos: 0 < n) : cyclotomic n R = (X ^ n - 1) /β‚˜ (∏ i in nat.proper_divisors n, cyclotomic i R) := begin nontriviality R, rw [←prod_cyclotomic_eq_X_pow_sub_one hpos, nat.divisors_eq_proper_divisors_insert_self_of_pos hpos, finset.prod_insert nat.proper_divisors.not_self_mem], have prod_monic : (∏ i in nat.proper_divisors n, cyclotomic i R).monic, { apply monic_prod_of_monic, intros i hi, exact cyclotomic.monic i R }, rw (div_mod_by_monic_unique (cyclotomic n R) 0 prod_monic _).1, simp only [degree_zero, zero_add], split, { rw mul_comm }, rw [bot_lt_iff_ne_bot], intro h, exact monic.ne_zero prod_monic (degree_eq_bot.1 h) end /-- If `m` is a proper divisor of `n`, then `X ^ m - 1` divides `∏ i in nat.proper_divisors n, cyclotomic i R`. -/ lemma X_pow_sub_one_dvd_prod_cyclotomic (R : Type*) [comm_ring R] {n m : β„•} (hpos : 0 < n) (hm : m ∣ n) (hdiff : m β‰  n) : X ^ m - 1 ∣ ∏ i in nat.proper_divisors n, cyclotomic i R := begin replace hm := nat.mem_proper_divisors.2 ⟨hm, lt_of_le_of_ne (nat.divisor_le (nat.mem_divisors.2 ⟨hm, hpos.ne'⟩)) hdiff⟩, rw [← finset.sdiff_union_of_subset (nat.divisors_subset_proper_divisors (ne_of_lt hpos).symm (nat.mem_proper_divisors.1 hm).1 (ne_of_lt (nat.mem_proper_divisors.1 hm).2)), finset.prod_union finset.sdiff_disjoint, prod_cyclotomic_eq_X_pow_sub_one (nat.pos_of_mem_proper_divisors hm)], exact ⟨(∏ (x : β„•) in n.proper_divisors \ m.divisors, cyclotomic x R), by rw mul_comm⟩ end /-- If there is a primitive `n`-th root of unity in `K`, then `cyclotomic n K = ∏ ΞΌ in primitive_roots n R, (X - C ΞΌ)`. In particular, `cyclotomic n K = cyclotomic' n K` -/ lemma cyclotomic_eq_prod_X_sub_primitive_roots {K : Type*} [comm_ring K] [is_domain K] {ΞΆ : K} {n : β„•} (hz : is_primitive_root ΞΆ n) : cyclotomic n K = ∏ ΞΌ in primitive_roots n K, (X - C ΞΌ) := begin rw ←cyclotomic', induction n using nat.strong_induction_on with k hk generalizing ΞΆ hz, obtain hzero | hpos := k.eq_zero_or_pos, { simp only [hzero, cyclotomic'_zero, cyclotomic_zero] }, have h : βˆ€ i ∈ k.proper_divisors, cyclotomic i K = cyclotomic' i K, { intros i hi, obtain ⟨d, hd⟩ := (nat.mem_proper_divisors.1 hi).1, rw mul_comm at hd, exact hk i (nat.mem_proper_divisors.1 hi).2 (is_primitive_root.pow hpos hz hd) }, rw [@cyclotomic_eq_X_pow_sub_one_div _ _ _ hpos, cyclotomic'_eq_X_pow_sub_one_div hpos hz, finset.prod_congr (refl k.proper_divisors) h] end section roots variables {R : Type*} {n : β„•} [comm_ring R] [is_domain R] /-- Any `n`-th primitive root of unity is a root of `cyclotomic n K`.-/ lemma _root_.is_primitive_root.is_root_cyclotomic (hpos : 0 < n) {ΞΌ : R} (h : is_primitive_root ΞΌ n) : is_root (cyclotomic n R) ΞΌ := begin rw [← mem_roots (cyclotomic_ne_zero n R), cyclotomic_eq_prod_X_sub_primitive_roots h, roots_prod_X_sub_C, ← finset.mem_def], rwa [← mem_primitive_roots hpos] at h, end private lemma is_root_cyclotomic_iff' {n : β„•} {K : Type*} [field K] {ΞΌ : K} [ne_zero (n : K)] : is_root (cyclotomic n K) ΞΌ ↔ is_primitive_root ΞΌ n := begin -- in this proof, `o` stands for `order_of ΞΌ` have hnpos : 0 < n := (ne_zero.of_ne_zero_coe K).out.bot_lt, refine ⟨λ hΞΌ, _, is_primitive_root.is_root_cyclotomic hnpos⟩, have hΞΌn : ΞΌ ^ n = 1, { rw is_root_of_unity_iff hnpos, exact ⟨n, n.mem_divisors_self hnpos.ne', hμ⟩ }, by_contra hnΞΌ, have ho : 0 < order_of ΞΌ, { apply order_of_pos', rw is_of_fin_order_iff_pow_eq_one, exact ⟨n, hnpos, hΞΌn⟩ }, have := pow_order_of_eq_one ΞΌ, rw is_root_of_unity_iff ho at this, obtain ⟨i, hio, hiμ⟩ := this, replace hio := nat.dvd_of_mem_divisors hio, rw is_primitive_root.not_iff at hnΞΌ, rw ←order_of_dvd_iff_pow_eq_one at hΞΌn, have key : i < n := (nat.le_of_dvd ho hio).trans_lt ((nat.le_of_dvd hnpos hΞΌn).lt_of_ne hnΞΌ), have key' : i ∣ n := hio.trans hΞΌn, rw ←polynomial.dvd_iff_is_root at hΞΌ hiΞΌ, have hni : {i, n} βŠ† n.divisors, { simpa [finset.insert_subset, key'] using hnpos.ne' }, obtain ⟨k, hk⟩ := hiΞΌ, obtain ⟨j, hj⟩ := hΞΌ, have := prod_cyclotomic_eq_X_pow_sub_one hnpos K, rw [←finset.prod_sdiff hni, finset.prod_pair key.ne, hk, hj] at this, have hn := (X_pow_sub_one_separable_iff.mpr $ ne_zero.nat_cast_ne n K).squarefree, rw [←this, squarefree] at hn, contrapose! hn, refine ⟨X - C ΞΌ, ⟨(∏ x in n.divisors \ {i, n}, cyclotomic x K) * k * j, by ring⟩, _⟩, simp [polynomial.is_unit_iff_degree_eq_zero] end lemma is_root_cyclotomic_iff [ne_zero (n : R)] {ΞΌ : R} : is_root (cyclotomic n R) ΞΌ ↔ is_primitive_root ΞΌ n := begin have hf : function.injective _ := is_fraction_ring.injective R (fraction_ring R), haveI : ne_zero (n : fraction_ring R) := ne_zero.nat_of_injective hf, rw [←is_root_map_iff hf, ←is_primitive_root.map_iff_of_injective hf, map_cyclotomic, ←is_root_cyclotomic_iff'] end lemma roots_cyclotomic_nodup [ne_zero (n : R)] : (cyclotomic n R).roots.nodup := begin obtain h | ⟨΢, h΢⟩ := (cyclotomic n R).roots.empty_or_exists_mem, { exact h.symm β–Έ multiset.nodup_zero }, rw [mem_roots $ cyclotomic_ne_zero n R, is_root_cyclotomic_iff] at hΞΆ, refine multiset.nodup_of_le (roots.le_of_dvd (X_pow_sub_C_ne_zero (ne_zero.pos_of_ne_zero_coe R) 1) $ cyclotomic.dvd_X_pow_sub_one n R) hΞΆ.nth_roots_nodup, end lemma cyclotomic.roots_to_finset_eq_primitive_roots [ne_zero (n : R)] : (⟨(cyclotomic n R).roots, roots_cyclotomic_nodup⟩ : finset _) = primitive_roots n R := by { ext, simp [cyclotomic_ne_zero n R, is_root_cyclotomic_iff, mem_primitive_roots, ne_zero.pos_of_ne_zero_coe R] } lemma cyclotomic.roots_eq_primitive_roots_val [ne_zero (n : R)] : (cyclotomic n R).roots = (primitive_roots n R).val := by rw ←cyclotomic.roots_to_finset_eq_primitive_roots end roots /-- If `R` is of characteristic zero, then `ΞΆ` is a root of `cyclotomic n R` if and only if it is a primitive `n`-th root of unity. -/ lemma is_root_cyclotomic_iff_char_zero {n : β„•} {R : Type*} [comm_ring R] [is_domain R] [char_zero R] {ΞΌ : R} (hn : 0 < n) : (polynomial.cyclotomic n R).is_root ΞΌ ↔ is_primitive_root ΞΌ n := by { letI := ne_zero.of_gt hn, exact is_root_cyclotomic_iff } /-- Over a ring `R` of characteristic zero, `Ξ» n, cyclotomic n R` is injective. -/ lemma cyclotomic_injective {R : Type*} [comm_ring R] [char_zero R] : function.injective (Ξ» n, cyclotomic n R) := begin intros n m hnm, simp only at hnm, rcases eq_or_ne n 0 with rfl | hzero, { rw [cyclotomic_zero] at hnm, replace hnm := congr_arg nat_degree hnm, rw [nat_degree_one, nat_degree_cyclotomic] at hnm, by_contra, exact (nat.totient_pos (zero_lt_iff.2 (ne.symm h))).ne hnm }, { haveI := ne_zero.mk hzero, rw [← map_cyclotomic_int _ R, ← map_cyclotomic_int _ R] at hnm, replace hnm := map_injective (int.cast_ring_hom R) int.cast_injective hnm, replace hnm := congr_arg (map (int.cast_ring_hom β„‚)) hnm, rw [map_cyclotomic_int, map_cyclotomic_int] at hnm, have hprim := complex.is_primitive_root_exp _ hzero, have hroot := is_root_cyclotomic_iff.2 hprim, rw hnm at hroot, haveI hmzero : ne_zero m := ⟨λ h, by simpa [h] using hroot⟩, rw is_root_cyclotomic_iff at hroot, replace hprim := hprim.eq_order_of, rwa [← is_primitive_root.eq_order_of hroot] at hprim} end lemma eq_cyclotomic_iff {R : Type*} [comm_ring R] {n : β„•} (hpos: 0 < n) (P : R[X]) : P = cyclotomic n R ↔ P * (∏ i in nat.proper_divisors n, polynomial.cyclotomic i R) = X ^ n - 1 := begin nontriviality R, refine ⟨λ hcycl, _, Ξ» hP, _⟩, { rw [hcycl, ← finset.prod_insert (@nat.proper_divisors.not_self_mem n), ← nat.divisors_eq_proper_divisors_insert_self_of_pos hpos], exact prod_cyclotomic_eq_X_pow_sub_one hpos R }, { have prod_monic : (∏ i in nat.proper_divisors n, cyclotomic i R).monic, { apply monic_prod_of_monic, intros i hi, exact cyclotomic.monic i R }, rw [@cyclotomic_eq_X_pow_sub_one_div R _ _ hpos, (div_mod_by_monic_unique P 0 prod_monic _).1], refine ⟨by rwa [zero_add, mul_comm], _⟩, rw [degree_zero, bot_lt_iff_ne_bot], intro h, exact monic.ne_zero prod_monic (degree_eq_bot.1 h) }, end /-- If `p ^ k` is a prime power, then `cyclotomic (p ^ (n + 1)) R = βˆ‘ i in range p, (X ^ (p ^ n)) ^ i`. -/ lemma cyclotomic_prime_pow_eq_geom_sum {R : Type*} [comm_ring R] {p n : β„•} (hp : p.prime) : cyclotomic (p ^ (n + 1)) R = βˆ‘ i in finset.range p, (X ^ (p ^ n)) ^ i := begin have : βˆ€ m, cyclotomic (p ^ (m + 1)) R = βˆ‘ i in finset.range p, (X ^ (p ^ m)) ^ i ↔ (βˆ‘ i in finset.range p, (X ^ (p ^ m)) ^ i) * ∏ (x : β„•) in finset.range (m + 1), cyclotomic (p ^ x) R = X ^ p ^ (m + 1) - 1, { intro m, have := eq_cyclotomic_iff (pow_pos hp.pos (m + 1)) _, rw eq_comm at this, rw [this, nat.prod_proper_divisors_prime_pow hp], }, induction n with n_n n_ih, { haveI := fact.mk hp, simp [cyclotomic_prime], }, rw ((eq_cyclotomic_iff (pow_pos hp.pos (n_n.succ + 1)) _).mpr _).symm, rw [nat.prod_proper_divisors_prime_pow hp, finset.prod_range_succ, n_ih], rw this at n_ih, rw [mul_comm _ (βˆ‘ i in _, _), n_ih, geom_sum_mul, sub_left_inj, ← pow_mul, pow_add, pow_one], end lemma cyclotomic_prime_pow_mul_X_pow_sub_one (R : Type*) [comm_ring R] (p k : β„•) [hn : fact (nat.prime p)] : (cyclotomic (p ^ (k + 1)) R) * (X ^ (p ^ k) - 1) = X ^ (p ^ (k + 1)) - 1 := by rw [cyclotomic_prime_pow_eq_geom_sum hn.out, geom_sum_mul, ← pow_mul, pow_succ, mul_comm] /-- The constant term of `cyclotomic n R` is `1` if `2 ≀ n`. -/ lemma cyclotomic_coeff_zero (R : Type*) [comm_ring R] {n : β„•} (hn : 2 ≀ n) : (cyclotomic n R).coeff 0 = 1 := begin induction n using nat.strong_induction_on with n hi, have hprod : (∏ i in nat.proper_divisors n, (polynomial.cyclotomic i R).coeff 0) = -1, { rw [←finset.insert_erase (nat.one_mem_proper_divisors_iff_one_lt.2 (lt_of_lt_of_le one_lt_two hn)), finset.prod_insert (finset.not_mem_erase 1 _), cyclotomic_one R], have hleq : βˆ€ j ∈ n.proper_divisors.erase 1, 2 ≀ j, { intros j hj, apply nat.succ_le_of_lt, exact (ne.le_iff_lt ((finset.mem_erase.1 hj).1).symm).mp (nat.succ_le_of_lt (nat.pos_of_mem_proper_divisors (finset.mem_erase.1 hj).2)) }, have hcongr : βˆ€ j ∈ n.proper_divisors.erase 1, (cyclotomic j R).coeff 0 = 1, { intros j hj, exact hi j (nat.mem_proper_divisors.1 (finset.mem_erase.1 hj).2).2 (hleq j hj) }, have hrw : ∏ (x : β„•) in n.proper_divisors.erase 1, (cyclotomic x R).coeff 0 = 1, { rw finset.prod_congr (refl (n.proper_divisors.erase 1)) hcongr, simp only [finset.prod_const_one] }, simp only [hrw, mul_one, zero_sub, coeff_one_zero, coeff_X_zero, coeff_sub] }, have heq : (X ^ n - 1).coeff 0 = -(cyclotomic n R).coeff 0, { rw [←prod_cyclotomic_eq_X_pow_sub_one (lt_of_lt_of_le zero_lt_two hn), nat.divisors_eq_proper_divisors_insert_self_of_pos (lt_of_lt_of_le zero_lt_two hn), finset.prod_insert nat.proper_divisors.not_self_mem, mul_coeff_zero, coeff_zero_prod, hprod, mul_neg, mul_one] }, have hzero : (X ^ n - 1).coeff 0 = (-1 : R), { rw coeff_zero_eq_eval_zero _, simp only [zero_pow (lt_of_lt_of_le zero_lt_two hn), eval_X, eval_one, zero_sub, eval_pow, eval_sub] }, rw hzero at heq, exact neg_inj.mp (eq.symm heq) end /-- If `(a : β„•)` is a root of `cyclotomic n (zmod p)`, where `p` is a prime, then `a` and `p` are coprime. -/ lemma coprime_of_root_cyclotomic {n : β„•} (hpos : 0 < n) {p : β„•} [hprime : fact p.prime] {a : β„•} (hroot : is_root (cyclotomic n (zmod p)) (nat.cast_ring_hom (zmod p) a)) : a.coprime p := begin apply nat.coprime.symm, rw [hprime.1.coprime_iff_not_dvd], intro h, replace h := (zmod.nat_coe_zmod_eq_zero_iff_dvd a p).2 h, rw [is_root.def, eq_nat_cast, h, ← coeff_zero_eq_eval_zero] at hroot, by_cases hone : n = 1, { simp only [hone, cyclotomic_one, zero_sub, coeff_one_zero, coeff_X_zero, neg_eq_zero, one_ne_zero, coeff_sub] at hroot, exact hroot }, rw [cyclotomic_coeff_zero (zmod p) (nat.succ_le_of_lt (lt_of_le_of_ne (nat.succ_le_of_lt hpos) (ne.symm hone)))] at hroot, exact one_ne_zero hroot end end cyclotomic section order /-- If `(a : β„•)` is a root of `cyclotomic n (zmod p)`, then the multiplicative order of `a` modulo `p` divides `n`. -/ lemma order_of_root_cyclotomic_dvd {n : β„•} (hpos : 0 < n) {p : β„•} [fact p.prime] {a : β„•} (hroot : is_root (cyclotomic n (zmod p)) (nat.cast_ring_hom (zmod p) a)) : order_of (zmod.unit_of_coprime a (coprime_of_root_cyclotomic hpos hroot)) ∣ n := begin apply order_of_dvd_of_pow_eq_one, suffices hpow : eval (nat.cast_ring_hom (zmod p) a) (X ^ n - 1 : (zmod p)[X]) = 0, { simp only [eval_X, eval_one, eval_pow, eval_sub, eq_nat_cast] at hpow, apply units.coe_eq_one.1, simp only [sub_eq_zero.mp hpow, zmod.coe_unit_of_coprime, units.coe_pow] }, rw [is_root.def] at hroot, rw [← prod_cyclotomic_eq_X_pow_sub_one hpos (zmod p), nat.divisors_eq_proper_divisors_insert_self_of_pos hpos, finset.prod_insert nat.proper_divisors.not_self_mem, eval_mul, hroot, zero_mul] end end order section minpoly open is_primitive_root complex /-- The minimal polynomial of a primitive `n`-th root of unity `ΞΌ` divides `cyclotomic n β„€`. -/ lemma _root_.is_primitive_root.minpoly_dvd_cyclotomic {n : β„•} {K : Type*} [field K] {ΞΌ : K} (h : is_primitive_root ΞΌ n) (hpos : 0 < n) [char_zero K] : minpoly β„€ ΞΌ ∣ cyclotomic n β„€ := begin apply minpoly.gcd_domain_dvd (is_integral h hpos) (cyclotomic_ne_zero n β„€), simpa [aeval_def, evalβ‚‚_eq_eval_map, is_root.def] using is_root_cyclotomic hpos h end lemma _root_.is_primitive_root.minpoly_eq_cyclotomic_of_irreducible {K : Type*} [field K] {R : Type*} [comm_ring R] [is_domain R] {ΞΌ : R} {n : β„•} [algebra K R] (hΞΌ : is_primitive_root ΞΌ n) (h : irreducible $ cyclotomic n K) [ne_zero (n : K)] : cyclotomic n K = minpoly K ΞΌ := begin haveI := ne_zero.of_no_zero_smul_divisors K R n, refine minpoly.eq_of_irreducible_of_monic h _ (cyclotomic.monic n K), rwa [aeval_def, evalβ‚‚_eq_eval_map, map_cyclotomic, ←is_root.def, is_root_cyclotomic_iff] end /-- `cyclotomic n β„€` is the minimal polynomial of a primitive `n`-th root of unity `ΞΌ`. -/ lemma cyclotomic_eq_minpoly {n : β„•} {K : Type*} [field K] {ΞΌ : K} (h : is_primitive_root ΞΌ n) (hpos : 0 < n) [char_zero K] : cyclotomic n β„€ = minpoly β„€ ΞΌ := begin refine eq_of_monic_of_dvd_of_nat_degree_le (minpoly.monic (is_integral h hpos)) (cyclotomic.monic n β„€) (h.minpoly_dvd_cyclotomic hpos) _, simpa [nat_degree_cyclotomic n β„€] using totient_le_degree_minpoly h end /-- `cyclotomic n β„š` is the minimal polynomial of a primitive `n`-th root of unity `ΞΌ`. -/ lemma cyclotomic_eq_minpoly_rat {n : β„•} {K : Type*} [field K] {ΞΌ : K} (h : is_primitive_root ΞΌ n) (hpos : 0 < n) [char_zero K] : cyclotomic n β„š = minpoly β„š ΞΌ := begin rw [← map_cyclotomic_int, cyclotomic_eq_minpoly h hpos], exact (minpoly.gcd_domain_eq_field_fractions' _ (is_integral h hpos)).symm end /-- `cyclotomic n β„€` is irreducible. -/ lemma cyclotomic.irreducible {n : β„•} (hpos : 0 < n) : irreducible (cyclotomic n β„€) := begin rw [cyclotomic_eq_minpoly (is_primitive_root_exp n hpos.ne') hpos], apply minpoly.irreducible, exact (is_primitive_root_exp n hpos.ne').is_integral hpos, end /-- `cyclotomic n β„š` is irreducible. -/ lemma cyclotomic.irreducible_rat {n : β„•} (hpos : 0 < n) : irreducible (cyclotomic n β„š) := begin rw [← map_cyclotomic_int], exact (is_primitive.int.irreducible_iff_irreducible_map_cast (cyclotomic.is_primitive n β„€)).1 (cyclotomic.irreducible hpos), end /-- If `n β‰  m`, then `(cyclotomic n β„š)` and `(cyclotomic m β„š)` are coprime. -/ lemma cyclotomic.is_coprime_rat {n m : β„•} (h : n β‰  m) : is_coprime (cyclotomic n β„š) (cyclotomic m β„š) := begin rcases n.eq_zero_or_pos with rfl | hnzero, { exact is_coprime_one_left }, rcases m.eq_zero_or_pos with rfl | hmzero, { exact is_coprime_one_right }, rw (irreducible.coprime_iff_not_dvd $ cyclotomic.irreducible_rat $ hnzero), exact (Ξ» hdiv, h $ cyclotomic_injective $ eq_of_monic_of_associated (cyclotomic.monic n β„š) (cyclotomic.monic m β„š) $ irreducible.associated_of_dvd (cyclotomic.irreducible_rat hnzero) (cyclotomic.irreducible_rat hmzero) hdiv), end end minpoly section expand /-- If `p` is a prime such that `Β¬ p ∣ n`, then `expand R p (cyclotomic n R) = (cyclotomic (n * p) R) * (cyclotomic n R)`. -/ @[simp] lemma cyclotomic_expand_eq_cyclotomic_mul {p n : β„•} (hp : nat.prime p) (hdiv : Β¬p ∣ n) (R : Type*) [comm_ring R] : expand R p (cyclotomic n R) = (cyclotomic (n * p) R) * (cyclotomic n R) := begin rcases nat.eq_zero_or_pos n with rfl | hnpos, { simp }, haveI := ne_zero.of_pos hnpos, suffices : expand β„€ p (cyclotomic n β„€) = (cyclotomic (n * p) β„€) * (cyclotomic n β„€), { rw [← map_cyclotomic_int, ← map_expand, this, polynomial.map_mul, map_cyclotomic_int] }, refine eq_of_monic_of_dvd_of_nat_degree_le ((cyclotomic.monic _ _).mul (cyclotomic.monic _ _)) ((cyclotomic.monic n β„€).expand hp.pos) _ _, { refine (is_primitive.int.dvd_iff_map_cast_dvd_map_cast _ _ (is_primitive.mul (cyclotomic.is_primitive (n * p) β„€) (cyclotomic.is_primitive n β„€)) ((cyclotomic.monic n β„€).expand hp.pos).is_primitive).2 _, rw [polynomial.map_mul, map_cyclotomic_int, map_cyclotomic_int, map_expand, map_cyclotomic_int], refine is_coprime.mul_dvd (cyclotomic.is_coprime_rat (Ξ» h, _)) _ _, { replace h : n * p = n * 1 := by simp [h], exact nat.prime.ne_one hp (mul_left_cancelβ‚€ hnpos.ne' h) }, { have hpos : 0 < n * p := mul_pos hnpos hp.pos, have hprim := complex.is_primitive_root_exp _ hpos.ne', rw [cyclotomic_eq_minpoly_rat hprim hpos], refine @minpoly.dvd β„š β„‚ _ _ algebra_rat _ _ _, rw [aeval_def, ← eval_map, map_expand, map_cyclotomic, expand_eval, ← is_root.def, is_root_cyclotomic_iff], convert is_primitive_root.pow_of_dvd hprim hp.ne_zero (dvd_mul_left p n), rw [nat.mul_div_cancel _ (nat.prime.pos hp)] }, { have hprim := complex.is_primitive_root_exp _ hnpos.ne.symm, rw [cyclotomic_eq_minpoly_rat hprim hnpos], refine @minpoly.dvd β„š β„‚ _ _ algebra_rat _ _ _, rw [aeval_def, ← eval_map, map_expand, expand_eval, ← is_root.def, ← cyclotomic_eq_minpoly_rat hprim hnpos, map_cyclotomic, is_root_cyclotomic_iff], exact is_primitive_root.pow_of_prime hprim hp hdiv,} }, { rw [nat_degree_expand, nat_degree_cyclotomic, nat_degree_mul (cyclotomic_ne_zero _ β„€) (cyclotomic_ne_zero _ β„€), nat_degree_cyclotomic, nat_degree_cyclotomic, mul_comm n, nat.totient_mul ((nat.prime.coprime_iff_not_dvd hp).2 hdiv), nat.totient_prime hp, mul_comm (p - 1), ← nat.mul_succ, nat.sub_one, nat.succ_pred_eq_of_pos hp.pos] } end /-- If `p` is a prime such that `p ∣ n`, then `expand R p (cyclotomic n R) = cyclotomic (p * n) R`. -/ @[simp] lemma cyclotomic_expand_eq_cyclotomic {p n : β„•} (hp : nat.prime p) (hdiv : p ∣ n) (R : Type*) [comm_ring R] : expand R p (cyclotomic n R) = cyclotomic (n * p) R := begin rcases n.eq_zero_or_pos with rfl | hzero, { simp }, haveI := ne_zero.of_pos hzero, suffices : expand β„€ p (cyclotomic n β„€) = cyclotomic (n * p) β„€, { rw [← map_cyclotomic_int, ← map_expand, this, map_cyclotomic_int] }, refine eq_of_monic_of_dvd_of_nat_degree_le (cyclotomic.monic _ _) ((cyclotomic.monic n β„€).expand hp.pos) _ _, { have hpos := nat.mul_pos hzero hp.pos, have hprim := complex.is_primitive_root_exp _ hpos.ne.symm, rw [cyclotomic_eq_minpoly hprim hpos], refine minpoly.gcd_domain_dvd (hprim.is_integral hpos) ((cyclotomic.monic n β„€).expand hp.pos).ne_zero _, rw [aeval_def, ← eval_map, map_expand, map_cyclotomic, expand_eval, ← is_root.def, is_root_cyclotomic_iff], { convert is_primitive_root.pow_of_dvd hprim hp.ne_zero (dvd_mul_left p n), rw [nat.mul_div_cancel _ hp.pos] } }, { rw [nat_degree_expand, nat_degree_cyclotomic, nat_degree_cyclotomic, mul_comm n, nat.totient_mul_of_prime_of_dvd hp hdiv, mul_comm] } end /-- If the `p ^ n`th cyclotomic polynomial is irreducible, so is the `p ^ m`th, for `m ≀ n`. -/ lemma cyclotomic_irreducible_pow_of_irreducible_pow {p : β„•} (hp : nat.prime p) {R} [comm_ring R] [is_domain R] {n m : β„•} (hmn : m ≀ n) (h : irreducible (cyclotomic (p ^ n) R)) : irreducible (cyclotomic (p ^ m) R) := begin unfreezingI { rcases m.eq_zero_or_pos with rfl | hm, { simpa using irreducible_X_sub_C (1 : R) }, obtain ⟨k, rfl⟩ := nat.exists_eq_add_of_le hmn, induction k with k hk }, { simpa using h }, have : m + k β‰  0 := (add_pos_of_pos_of_nonneg hm k.zero_le).ne', rw [nat.add_succ, pow_succ', ←cyclotomic_expand_eq_cyclotomic hp $ dvd_pow_self p this] at h, exact hk (by linarith) (of_irreducible_expand hp.ne_zero h) end /-- If `irreducible (cyclotomic (p ^ n) R)` then `irreducible (cyclotomic p R).` -/ lemma cyclotomic_irreducible_of_irreducible_pow {p : β„•} (hp : nat.prime p) {R} [comm_ring R] [is_domain R] {n : β„•} (hn : n β‰  0) (h : irreducible (cyclotomic (p ^ n) R)) : irreducible (cyclotomic p R) := pow_one p β–Έ cyclotomic_irreducible_pow_of_irreducible_pow hp hn.bot_lt h end expand section char_p /-- If `R` is of characteristic `p` and `Β¬p ∣ n`, then `cyclotomic (n * p) R = (cyclotomic n R) ^ (p - 1)`. -/ lemma cyclotomic_mul_prime_eq_pow_of_not_dvd (R : Type*) {p n : β„•} [hp : fact (nat.prime p)] [ring R] [char_p R p] (hn : Β¬p ∣ n) : cyclotomic (n * p) R = (cyclotomic n R) ^ (p - 1) := begin suffices : cyclotomic (n * p) (zmod p) = (cyclotomic n (zmod p)) ^ (p - 1), { rw [← map_cyclotomic _ (algebra_map (zmod p) R), ← map_cyclotomic _ (algebra_map (zmod p) R), this, polynomial.map_pow] }, apply mul_right_injectiveβ‚€ (cyclotomic_ne_zero n $ zmod p), rw [←pow_succ, tsub_add_cancel_of_le hp.out.one_lt.le, mul_comm, ← zmod.expand_card], nth_rewrite 2 [← map_cyclotomic_int], rw [← map_expand, cyclotomic_expand_eq_cyclotomic_mul hp.out hn, polynomial.map_mul, map_cyclotomic, map_cyclotomic] end /-- If `R` is of characteristic `p` and `p ∣ n`, then `cyclotomic (n * p) R = (cyclotomic n R) ^ p`. -/ lemma cyclotomic_mul_prime_dvd_eq_pow (R : Type*) {p n : β„•} [hp : fact (nat.prime p)] [ring R] [char_p R p] (hn : p ∣ n) : cyclotomic (n * p) R = (cyclotomic n R) ^ p := begin suffices : cyclotomic (n * p) (zmod p) = (cyclotomic n (zmod p)) ^ p, { rw [← map_cyclotomic _ (algebra_map (zmod p) R), ← map_cyclotomic _ (algebra_map (zmod p) R), this, polynomial.map_pow] }, rw [← zmod.expand_card, ← map_cyclotomic_int n, ← map_expand, cyclotomic_expand_eq_cyclotomic hp.out hn, map_cyclotomic, mul_comm] end /-- If `R` is of characteristic `p` and `Β¬p ∣ m`, then `cyclotomic (p ^ k * m) R = (cyclotomic m R) ^ (p ^ k - p ^ (k - 1))`. -/ lemma cyclotomic_mul_prime_pow_eq (R : Type*) {p m : β„•} [fact (nat.prime p)] [ring R] [char_p R p] (hm : Β¬p ∣ m) : βˆ€ {k}, 0 < k β†’ cyclotomic (p ^ k * m) R = (cyclotomic m R) ^ (p ^ k - p ^ (k - 1)) | 1 _ := by rw [pow_one, nat.sub_self, pow_zero, mul_comm, cyclotomic_mul_prime_eq_pow_of_not_dvd R hm] | (a + 2) _ := begin have hdiv : p ∣ p ^ a.succ * m := ⟨p ^ a * m, by rw [← mul_assoc, pow_succ]⟩, rw [pow_succ, mul_assoc, mul_comm, cyclotomic_mul_prime_dvd_eq_pow R hdiv, cyclotomic_mul_prime_pow_eq a.succ_pos, ← pow_mul], congr' 1, simp only [tsub_zero, nat.succ_sub_succ_eq_sub], rw [nat.mul_sub_right_distrib, mul_comm, pow_succ'] end /-- If `R` is of characteristic `p` and `Β¬p ∣ m`, then `ΞΆ` is a root of `cyclotomic (p ^ k * m) R` if and only if it is a primitive `m`-th root of unity. -/ lemma is_root_cyclotomic_prime_pow_mul_iff_of_char_p {m k p : β„•} {R : Type*} [comm_ring R] [is_domain R] [hp : fact (nat.prime p)] [hchar : char_p R p] {ΞΌ : R} [ne_zero (m : R)] : (polynomial.cyclotomic (p ^ k * m) R).is_root ΞΌ ↔ is_primitive_root ΞΌ m := begin rcases k.eq_zero_or_pos with rfl | hk, { rw [pow_zero, one_mul, is_root_cyclotomic_iff] }, refine ⟨λ h, _, Ξ» h, _⟩, { rw [is_root.def, cyclotomic_mul_prime_pow_eq R (ne_zero.not_char_dvd R p m) hk, eval_pow] at h, replace h := pow_eq_zero h, rwa [← is_root.def, is_root_cyclotomic_iff] at h }, { rw [← is_root_cyclotomic_iff, is_root.def] at h, rw [cyclotomic_mul_prime_pow_eq R (ne_zero.not_char_dvd R p m) hk, is_root.def, eval_pow, h, zero_pow], simp only [tsub_pos_iff_lt], apply pow_strict_mono_right hp.out.one_lt (nat.pred_lt hk.ne') } end end char_p end polynomial
00319f8e4aeba4b6208a69255eb56130c3f2dab8
cfef816283af58a9ea4678b69595e86562fd4c6a
/src/scone/t2.lean
aecfc2a1037a6cb150a78688b639187080d2d67c
[]
no_license
rwbarton/scone
6fc5bee6766c170d2edeefaf608ee32d665c7c77
6f3d35f7a3bed772475ff7954875b9ee7554aae3
refs/heads/master
1,672,462,177,915
1,603,245,162,000
1,603,245,162,000
305,762,158
0
0
null
null
null
null
UTF-8
Lean
false
false
3,394
lean
import scone.edge -- Demo file. open scone /- The type of "Hausdorffness", or any other property of a topological space. Here, to stick close to the primitives, we just represent a topology as a collection of (open) sets, and omit the axioms. -/ def A : Type 1 := Ξ  (X : Type), ((X β†’ Prop) β†’ Prop) β†’ Prop def edge_A : edge_ty A := begin unfold A, -- optional -- β€’ ⊒ Ξ  (X : Type), ((X β†’ Prop) β†’ Prop) β†’ Prop) type refine pi_edge_ctx_ty _ type_edge_ty _, -- X : Type ⊒ ((X β†’ Prop) β†’ Prop) β†’ Prop) type refine pi_edge_ctx_ty _ _ (gen_edge_ty prop_edge_ty), refine pi_edge_ctx_ty _ _ (gen_edge_ty prop_edge_ty), refine pi_edge_ctx_ty _ _ (gen_edge_ty prop_edge_ty), -- X : Type ⊒ X type -- For this we need Russell-style universes plus the variable rule. refine univ_ctx_ty _ _, -- X : Type ⊒ X : Type refine last_var_ctx_tm _ _, done end /- The property of being a Hausdorff space: any two distinct points are separated by disjoint open sets. -/ def Tβ‚‚ : A := Ξ» X Ο„, βˆ€ (x y : X), (x = y β†’ false) β†’ βˆƒ (U V : X β†’ Prop), Ο„ U ∧ Ο„ V ∧ U x ∧ V y ∧ βˆ€ (z : X), U z β†’ V z β†’ false -- TODO: By a similar process, prove -- def edge_Tβ‚‚ : edge_ctx_tm unit_edge_ctx edge_A (Ξ» _, Tβ‚‚) := sorry variables {X Y : Type} (Ο„X : (X β†’ Prop) β†’ Prop) (Ο„Y : (Y β†’ Prop) β†’ Prop) variables (e : X ≃ Y) (he : βˆ€ (s : set X), Ο„X s ↔ Ο„Y (e.symm ⁻¹' s)) variables {F : A} (hF : edge_ctx_tm unit_edge_ctx edge_A.{0 0} (Ξ» _, F)) include he hF lemma invariant : F X Ο„X ↔ F Y Ο„Y := begin have := hF.e ⟨rfl⟩, swap, exact (), swap, exact (), swap, exact (), specialize this X Y (discrete_edge_ctx_ty _) (discrete_edge_ctx_ty _), specialize this ⟨e, Ξ» x, equiv.refl _, _⟩, swap, { rintros xβ‚€ x₁ ⟨⟩ ⟨⟩, -- need to check that the function `e` "preserves edges", -- which is automatic because `X` is discrete change plift (_ =[rfl] _) ≃ plift (_ =[rfl] _), rw [deq_iff_eq, deq_iff_eq], refine equiv.plift.trans ((equiv.of_iff _).trans equiv.plift.symm), symmetry, apply equiv.apply_eq_iff_eq }, specialize this Ο„X Ο„Y, -- next we need a proof that Ο„X "preserves edges", -- which is automatic because `X β†’ Prop` is discrete, -- and the same for Ο„Y specialize this _, swap, { fsplit, { rintro (s : set X) hs, exact ⟨⟩ }, { rintro (sβ‚€ : set X) (s₁ : set X) hsβ‚€ hs₁ h, suffices : sβ‚€ = s₁, { constructor, rw this }, ext x, specialize h x x () () ⟨deq.refl _⟩, exact h.down } }, specialize this _, swap, { fsplit, { rintro (s : set Y) hs, exact ⟨⟩ }, { rintro (sβ‚€ : set Y) (s₁ : set Y) hsβ‚€ hs₁ h, suffices : sβ‚€ = s₁, { constructor, rw this }, ext y, specialize h y y () () ⟨deq.refl _⟩, exact h.down } }, -- now we need to use the compatibility of `e` and `Ο„X`, `Ο„Y` specialize this _, swap, { rintros (s : set X) (t : set Y) _ _ h, suffices : t = e.symm ⁻¹' s, { change plift (Ο„X s ↔ Ο„Y t), rw this, exact ⟨he s⟩ }, ext y, specialize h (e.symm y) y () (), specialize h ⟨_⟩, swap, { conv { whnf }, conv_lhs { change e (e.symm y) }, rw e.apply_symm_apply }, exact h.down.symm }, exact this.down end
7f633d96c3d2d92a142efd24aaa3b8b991125d7b
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/category_theory/limits/shapes/equalizers.lean
49c80aea41d771ffa21a7baf5bdd1182f97d57e1
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
28,307
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import category_theory.epi_mono import category_theory.limits.limits /-! # Equalizers and coequalizers This file defines (co)equalizers as special cases of (co)limits. An equalizer is the categorical generalization of the subobject {a ∈ A | f(a) = g(a)} known from abelian groups or modules. It is a limit cone over the diagram formed by `f` and `g`. A coequalizer is the dual concept. ## Main definitions * `walking_parallel_pair` is the indexing category used for (co)equalizer_diagrams * `parallel_pair` is a functor from `walking_parallel_pair` to our category `C`. * a `fork` is a cone over a parallel pair. * there is really only one interesting morphism in a fork: the arrow from the vertex of the fork to the domain of f and g. It is called `fork.ΞΉ`. * an `equalizer` is now just a `limit (parallel_pair f g)` Each of these has a dual. ## Main statements * `equalizer.ΞΉ_mono` states that every equalizer map is a monomorphism * `is_iso_limit_cone_parallel_pair_of_self` states that the identity on the domain of `f` is an equalizer of `f` and `f`. ## Implementation notes As with the other special shapes in the limits library, all the definitions here are given as `abbreviation`s of the general statements for limits, so all the `simp` lemmas and theorems about general limits can be used. ## References * [F. Borceux, *Handbook of Categorical Algebra 1*][borceux-vol1] -/ open category_theory namespace category_theory.limits local attribute [tidy] tactic.case_bash universes v u /-- The type of objects for the diagram indexing a (co)equalizer. -/ @[derive decidable_eq, derive inhabited] inductive walking_parallel_pair : Type v | zero | one open walking_parallel_pair /-- The type family of morphisms for the diagram indexing a (co)equalizer. -/ @[derive decidable_eq] inductive walking_parallel_pair_hom : walking_parallel_pair β†’ walking_parallel_pair β†’ Type v | left : walking_parallel_pair_hom zero one | right : walking_parallel_pair_hom zero one | id : Ξ  X : walking_parallel_pair.{v}, walking_parallel_pair_hom X X /-- Satisfying the inhabited linter -/ instance : inhabited (walking_parallel_pair_hom zero one) := { default := walking_parallel_pair_hom.left } open walking_parallel_pair_hom /-- Composition of morphisms in the indexing diagram for (co)equalizers. -/ def walking_parallel_pair_hom.comp : Ξ  (X Y Z : walking_parallel_pair) (f : walking_parallel_pair_hom X Y) (g : walking_parallel_pair_hom Y Z), walking_parallel_pair_hom X Z | _ _ _ (id _) h := h | _ _ _ left (id one) := left | _ _ _ right (id one) := right . instance walking_parallel_pair_hom_category : small_category walking_parallel_pair := { hom := walking_parallel_pair_hom, id := walking_parallel_pair_hom.id, comp := walking_parallel_pair_hom.comp } @[simp] lemma walking_parallel_pair_hom_id (X : walking_parallel_pair) : walking_parallel_pair_hom.id X = πŸ™ X := rfl variables {C : Type u} [category.{v} C] variables {X Y : C} /-- `parallel_pair f g` is the diagram in `C` consisting of the two morphisms `f` and `g` with common domain and codomain. -/ def parallel_pair (f g : X ⟢ Y) : walking_parallel_pair β₯€ C := { obj := Ξ» x, match x with | zero := X | one := Y end, map := Ξ» x y h, match x, y, h with | _, _, (id _) := πŸ™ _ | _, _, left := f | _, _, right := g end, -- `tidy` can cope with this, but it's too slow: map_comp' := begin rintros (⟨⟩|⟨⟩) (⟨⟩|⟨⟩) (⟨⟩|⟨⟩) ⟨⟩⟨⟩; { unfold_aux, simp; refl }, end, }. @[simp] lemma parallel_pair_obj_zero (f g : X ⟢ Y) : (parallel_pair f g).obj zero = X := rfl @[simp] lemma parallel_pair_obj_one (f g : X ⟢ Y) : (parallel_pair f g).obj one = Y := rfl @[simp] lemma parallel_pair_map_left (f g : X ⟢ Y) : (parallel_pair f g).map left = f := rfl @[simp] lemma parallel_pair_map_right (f g : X ⟢ Y) : (parallel_pair f g).map right = g := rfl @[simp] lemma parallel_pair_functor_obj {F : walking_parallel_pair β₯€ C} (j : walking_parallel_pair) : (parallel_pair (F.map left) (F.map right)).obj j = F.obj j := begin cases j; refl end /-- Every functor indexing a (co)equalizer is naturally isomorphic (actually, equal) to a `parallel_pair` -/ def diagram_iso_parallel_pair (F : walking_parallel_pair β₯€ C) : F β‰… parallel_pair (F.map left) (F.map right) := nat_iso.of_components (Ξ» j, eq_to_iso $ by cases j; tidy) $ by tidy /-- A fork on `f` and `g` is just a `cone (parallel_pair f g)`. -/ abbreviation fork (f g : X ⟢ Y) := cone (parallel_pair f g) /-- A cofork on `f` and `g` is just a `cocone (parallel_pair f g)`. -/ abbreviation cofork (f g : X ⟢ Y) := cocone (parallel_pair f g) variables {f g : X ⟢ Y} @[simp, reassoc] lemma fork.app_zero_left (s : fork f g) : (s.Ο€).app zero ≫ f = (s.Ο€).app one := by rw [←s.w left, parallel_pair_map_left] @[simp, reassoc] lemma fork.app_zero_right (s : fork f g) : (s.Ο€).app zero ≫ g = (s.Ο€).app one := by rw [←s.w right, parallel_pair_map_right] @[simp, reassoc] lemma cofork.left_app_one (s : cofork f g) : f ≫ (s.ΞΉ).app one = (s.ΞΉ).app zero := by rw [←s.w left, parallel_pair_map_left] @[simp, reassoc] lemma cofork.right_app_one (s : cofork f g) : g ≫ (s.ΞΉ).app one = (s.ΞΉ).app zero := by rw [←s.w right, parallel_pair_map_right] /-- A fork on `f g : X ⟢ Y` is determined by the morphism `ΞΉ : P ⟢ X` satisfying `ΞΉ ≫ f = ΞΉ ≫ g`. -/ def fork.of_ΞΉ {P : C} (ΞΉ : P ⟢ X) (w : ΞΉ ≫ f = ΞΉ ≫ g) : fork f g := { X := P, Ο€ := { app := Ξ» X, begin cases X, exact ΞΉ, exact ΞΉ ≫ f, end, naturality' := Ξ» X Y f, begin cases X; cases Y; cases f; dsimp; simp, { dsimp, simp, }, -- TODO If someone could decipher why these aren't done on the previous line, that would be great { exact w }, { dsimp, simp, }, -- TODO idem end } } /-- A cofork on `f g : X ⟢ Y` is determined by the morphism `Ο€ : Y ⟢ P` satisfying `f ≫ Ο€ = g ≫ Ο€`. -/ def cofork.of_Ο€ {P : C} (Ο€ : Y ⟢ P) (w : f ≫ Ο€ = g ≫ Ο€) : cofork f g := { X := P, ΞΉ := { app := Ξ» X, begin cases X, exact f ≫ Ο€, exact Ο€, end, naturality' := Ξ» X Y f, begin cases X; cases Y; cases f; dsimp; simp, { dsimp, simp, }, -- TODO idem { exact w.symm }, { dsimp, simp, }, -- TODO idem end } } @[simp] lemma fork.of_ΞΉ_app_zero {P : C} (ΞΉ : P ⟢ X) (w : ΞΉ ≫ f = ΞΉ ≫ g) : (fork.of_ΞΉ ΞΉ w).Ο€.app zero = ΞΉ := rfl @[simp] lemma fork.of_ΞΉ_app_one {P : C} (ΞΉ : P ⟢ X) (w : ΞΉ ≫ f = ΞΉ ≫ g) : (fork.of_ΞΉ ΞΉ w).Ο€.app one = ΞΉ ≫ f := rfl @[simp] lemma cofork.of_Ο€_app_zero {P : C} (Ο€ : Y ⟢ P) (w : f ≫ Ο€ = g ≫ Ο€) : (cofork.of_Ο€ Ο€ w).ΞΉ.app zero = f ≫ Ο€ := rfl @[simp] lemma cofork.of_Ο€_app_one {P : C} (Ο€ : Y ⟢ P) (w : f ≫ Ο€ = g ≫ Ο€) : (cofork.of_Ο€ Ο€ w).ΞΉ.app one = Ο€ := rfl /-- A fork `t` on the parallel pair `f g : X ⟢ Y` consists of two morphisms `t.Ο€.app zero : t.X ⟢ X` and `t.Ο€.app one : t.X ⟢ Y`. Of these, only the first one is interesting, and we give it the shorter name `fork.ΞΉ t`. -/ abbreviation fork.ΞΉ (t : fork f g) := t.Ο€.app zero /-- A cofork `t` on the parallel_pair `f g : X ⟢ Y` consists of two morphisms `t.ΞΉ.app zero : X ⟢ t.X` and `t.ΞΉ.app one : Y ⟢ t.X`. Of these, only the second one is interesting, and we give it the shorter name `cofork.Ο€ t`. -/ abbreviation cofork.Ο€ (t : cofork f g) := t.ΞΉ.app one @[simp] lemma fork.ΞΉ_of_ΞΉ {P : C} (ΞΉ : P ⟢ X) (w : ΞΉ ≫ f = ΞΉ ≫ g) : fork.ΞΉ (fork.of_ΞΉ ΞΉ w) = ΞΉ := rfl @[simp] lemma cofork.Ο€_of_Ο€ {P : C} (Ο€ : Y ⟢ P) (w : f ≫ Ο€ = g ≫ Ο€) : cofork.Ο€ (cofork.of_Ο€ Ο€ w) = Ο€ := rfl lemma fork.ΞΉ_eq_app_zero (t : fork f g) : fork.ΞΉ t = t.Ο€.app zero := rfl lemma cofork.Ο€_eq_app_one (t : cofork f g) : cofork.Ο€ t = t.ΞΉ.app one := rfl lemma fork.condition (t : fork f g) : (fork.ΞΉ t) ≫ f = (fork.ΞΉ t) ≫ g := begin erw [t.w left, ← t.w right], refl end lemma cofork.condition (t : cofork f g) : f ≫ (cofork.Ο€ t) = g ≫ (cofork.Ο€ t) := begin erw [t.w left, ← t.w right], refl end /-- To check whether two maps are equalized by both maps of a fork, it suffices to check it for the first map -/ lemma fork.equalizer_ext (s : fork f g) {W : C} {k l : W ⟢ s.X} (h : k ≫ fork.ΞΉ s = l ≫ fork.ΞΉ s) : βˆ€ (j : walking_parallel_pair), k ≫ s.Ο€.app j = l ≫ s.Ο€.app j | zero := h | one := by rw [←fork.app_zero_left, reassoc_of h] /-- To check whether two maps are coequalized by both maps of a cofork, it suffices to check it for the second map -/ lemma cofork.coequalizer_ext (s : cofork f g) {W : C} {k l : s.X ⟢ W} (h : cofork.Ο€ s ≫ k = cofork.Ο€ s ≫ l) : βˆ€ (j : walking_parallel_pair), s.ΞΉ.app j ≫ k = s.ΞΉ.app j ≫ l | zero := by simp only [←cofork.left_app_one, category.assoc, h] | one := h lemma fork.is_limit.hom_ext {s : fork f g} (hs : is_limit s) {W : C} {k l : W ⟢ s.X} (h : k ≫ fork.ΞΉ s = l ≫ fork.ΞΉ s) : k = l := hs.hom_ext $ fork.equalizer_ext _ h lemma cofork.is_colimit.hom_ext {s : cofork f g} (hs : is_colimit s) {W : C} {k l : s.X ⟢ W} (h : cofork.Ο€ s ≫ k = cofork.Ο€ s ≫ l) : k = l := hs.hom_ext $ cofork.coequalizer_ext _ h /-- If `s` is a limit fork over `f` and `g`, then a morphism `k : W ⟢ X` satisfying `k ≫ f = k ≫ g` induces a morphism `l : W ⟢ s.X` such that `l ≫ fork.ΞΉ s = k`. -/ def fork.is_limit.lift' {s : fork f g} (hs : is_limit s) {W : C} (k : W ⟢ X) (h : k ≫ f = k ≫ g) : {l : W ⟢ s.X // l ≫ fork.ΞΉ s = k} := ⟨hs.lift $ fork.of_ΞΉ _ h, hs.fac _ _⟩ /-- If `s` is a colimit cofork over `f` and `g`, then a morphism `k : Y ⟢ W` satisfying `f ≫ k = g ≫ k` induces a morphism `l : s.X ⟢ W` such that `cofork.Ο€ s ≫ l = k`. -/ def cofork.is_colimit.desc' {s : cofork f g} (hs : is_colimit s) {W : C} (k : Y ⟢ W) (h : f ≫ k = g ≫ k) : {l : s.X ⟢ W // cofork.Ο€ s ≫ l = k} := ⟨hs.desc $ cofork.of_Ο€ _ h, hs.fac _ _⟩ /-- This is a slightly more convenient method to verify that a fork is a limit cone. It only asks for a proof of facts that carry any mathematical content -/ def fork.is_limit.mk (t : fork f g) (lift : Ξ  (s : fork f g), s.X ⟢ t.X) (fac : βˆ€ (s : fork f g), lift s ≫ fork.ΞΉ t = fork.ΞΉ s) (uniq : βˆ€ (s : fork f g) (m : s.X ⟢ t.X) (w : βˆ€ j : walking_parallel_pair, m ≫ t.Ο€.app j = s.Ο€.app j), m = lift s) : is_limit t := { lift := lift, fac' := Ξ» s j, walking_parallel_pair.cases_on j (fac s) $ by erw [←s.w left, ←t.w left, ←category.assoc, fac]; refl, uniq' := uniq } /-- This is another convenient method to verify that a fork is a limit cone. It only asks for a proof of facts that carry any mathematical content, and allows access to the same `s` for all parts. -/ def fork.is_limit.mk' {X Y : C} {f g : X ⟢ Y} (t : fork f g) (create : Ξ  (s : fork f g), {l // l ≫ t.ΞΉ = s.ΞΉ ∧ βˆ€ {m}, m ≫ t.ΞΉ = s.ΞΉ β†’ m = l}) : is_limit t := fork.is_limit.mk t (Ξ» s, (create s).1) (Ξ» s, (create s).2.1) (Ξ» s m w, (create s).2.2 (w zero)) /-- This is a slightly more convenient method to verify that a cofork is a colimit cocone. It only asks for a proof of facts that carry any mathematical content -/ def cofork.is_colimit.mk (t : cofork f g) (desc : Ξ  (s : cofork f g), t.X ⟢ s.X) (fac : βˆ€ (s : cofork f g), cofork.Ο€ t ≫ desc s = cofork.Ο€ s) (uniq : βˆ€ (s : cofork f g) (m : t.X ⟢ s.X) (w : βˆ€ j : walking_parallel_pair, t.ΞΉ.app j ≫ m = s.ΞΉ.app j), m = desc s) : is_colimit t := { desc := desc, fac' := Ξ» s j, walking_parallel_pair.cases_on j (by erw [←s.w left, ←t.w left, category.assoc, fac]; refl) (fac s), uniq' := uniq } /-- This is another convenient method to verify that a fork is a limit cone. It only asks for a proof of facts that carry any mathematical content, and allows access to the same `s` for all parts. -/ def cofork.is_colimit.mk' {X Y : C} {f g : X ⟢ Y} (t : cofork f g) (create : Ξ  (s : cofork f g), {l : t.X ⟢ s.X // t.Ο€ ≫ l = s.Ο€ ∧ βˆ€ {m}, t.Ο€ ≫ m = s.Ο€ β†’ m = l}) : is_colimit t := cofork.is_colimit.mk t (Ξ» s, (create s).1) (Ξ» s, (create s).2.1) (Ξ» s m w, (create s).2.2 (w one)) /-- This is a helper construction that can be useful when verifying that a category has all equalizers. Given `F : walking_parallel_pair β₯€ C`, which is really the same as `parallel_pair (F.map left) (F.map right)`, and a fork on `F.map left` and `F.map right`, we get a cone on `F`. If you're thinking about using this, have a look at `has_equalizers_of_has_limit_parallel_pair`, which you may find to be an easier way of achieving your goal. -/ def cone.of_fork {F : walking_parallel_pair β₯€ C} (t : fork (F.map left) (F.map right)) : cone F := { X := t.X, Ο€ := { app := Ξ» X, t.Ο€.app X ≫ eq_to_hom (by tidy), naturality' := Ξ» j j' g, by { cases j; cases j'; cases g; dsimp; simp } } } /-- This is a helper construction that can be useful when verifying that a category has all coequalizers. Given `F : walking_parallel_pair β₯€ C`, which is really the same as `parallel_pair (F.map left) (F.map right)`, and a cofork on `F.map left` and `F.map right`, we get a cocone on `F`. If you're thinking about using this, have a look at `has_coequalizers_of_has_colimit_parallel_pair`, which you may find to be an easier way of achieving your goal. -/ def cocone.of_cofork {F : walking_parallel_pair β₯€ C} (t : cofork (F.map left) (F.map right)) : cocone F := { X := t.X, ΞΉ := { app := Ξ» X, eq_to_hom (by tidy) ≫ t.ΞΉ.app X, naturality' := Ξ» j j' g, by { cases j; cases j'; cases g; dsimp; simp } } } @[simp] lemma cone.of_fork_Ο€ {F : walking_parallel_pair β₯€ C} (t : fork (F.map left) (F.map right)) (j) : (cone.of_fork t).Ο€.app j = t.Ο€.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cocone.of_cofork_ΞΉ {F : walking_parallel_pair β₯€ C} (t : cofork (F.map left) (F.map right)) (j) : (cocone.of_cofork t).ΞΉ.app j = eq_to_hom (by tidy) ≫ t.ΞΉ.app j := rfl /-- Given `F : walking_parallel_pair β₯€ C`, which is really the same as `parallel_pair (F.map left) (F.map right)` and a cone on `F`, we get a fork on `F.map left` and `F.map right`. -/ def fork.of_cone {F : walking_parallel_pair β₯€ C} (t : cone F) : fork (F.map left) (F.map right) := { X := t.X, Ο€ := { app := Ξ» X, t.Ο€.app X ≫ eq_to_hom (by tidy) } } /-- Given `F : walking_parallel_pair β₯€ C`, which is really the same as `parallel_pair (F.map left) (F.map right)` and a cocone on `F`, we get a cofork on `F.map left` and `F.map right`. -/ def cofork.of_cocone {F : walking_parallel_pair β₯€ C} (t : cocone F) : cofork (F.map left) (F.map right) := { X := t.X, ΞΉ := { app := Ξ» X, eq_to_hom (by tidy) ≫ t.ΞΉ.app X } } @[simp] lemma fork.of_cone_Ο€ {F : walking_parallel_pair β₯€ C} (t : cone F) (j) : (fork.of_cone t).Ο€.app j = t.Ο€.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cofork.of_cocone_ΞΉ {F : walking_parallel_pair β₯€ C} (t : cocone F) (j) : (cofork.of_cocone t).ΞΉ.app j = eq_to_hom (by tidy) ≫ t.ΞΉ.app j := rfl variables (f g) section variables [has_limit (parallel_pair f g)] /-- If we have chosen an equalizer of `f` and `g`, we can access the corresponding object by saying `equalizer f g`. -/ abbreviation equalizer := limit (parallel_pair f g) /-- If we have chosen an equalizer of `f` and `g`, we can access the inclusion `equalizer f g ⟢ X` by saying `equalizer.ΞΉ f g`. -/ abbreviation equalizer.ΞΉ : equalizer f g ⟢ X := limit.Ο€ (parallel_pair f g) zero @[simp] lemma equalizer.ΞΉ.fork : fork.ΞΉ (limit.cone (parallel_pair f g)) = equalizer.ΞΉ f g := rfl @[simp] lemma equalizer.ΞΉ.eq_app_zero : (limit.cone (parallel_pair f g)).Ο€.app zero = equalizer.ΞΉ f g := rfl @[reassoc] lemma equalizer.condition : equalizer.ΞΉ f g ≫ f = equalizer.ΞΉ f g ≫ g := fork.condition $ limit.cone $ parallel_pair f g variables {f g} /-- A morphism `k : W ⟢ X` satisfying `k ≫ f = k ≫ g` factors through the equalizer of `f` and `g` via `equalizer.lift : W ⟢ equalizer f g`. -/ abbreviation equalizer.lift {W : C} (k : W ⟢ X) (h : k ≫ f = k ≫ g) : W ⟢ equalizer f g := limit.lift (parallel_pair f g) (fork.of_ΞΉ k h) @[simp, reassoc] lemma equalizer.lift_ΞΉ {W : C} (k : W ⟢ X) (h : k ≫ f = k ≫ g) : equalizer.lift k h ≫ equalizer.ΞΉ f g = k := limit.lift_Ο€ _ _ /-- A morphism `k : W ⟢ X` satisfying `k ≫ f = k ≫ g` induces a morphism `l : W ⟢ equalizer f g` satisfying `l ≫ equalizer.ΞΉ f g = k`. -/ def equalizer.lift' {W : C} (k : W ⟢ X) (h : k ≫ f = k ≫ g) : {l : W ⟢ equalizer f g // l ≫ equalizer.ΞΉ f g = k} := ⟨equalizer.lift k h, equalizer.lift_ΞΉ _ _⟩ /-- Two maps into an equalizer are equal if they are are equal when composed with the equalizer map. -/ @[ext] lemma equalizer.hom_ext {W : C} {k l : W ⟢ equalizer f g} (h : k ≫ equalizer.ΞΉ f g = l ≫ equalizer.ΞΉ f g) : k = l := fork.is_limit.hom_ext (limit.is_limit _) h /-- An equalizer morphism is a monomorphism -/ instance equalizer.ΞΉ_mono : mono (equalizer.ΞΉ f g) := { right_cancellation := Ξ» Z h k w, equalizer.hom_ext w } end section variables {f g} /-- The equalizer morphism in any limit cone is a monomorphism. -/ lemma mono_of_is_limit_parallel_pair {c : cone (parallel_pair f g)} (i : is_limit c) : mono (fork.ΞΉ c) := { right_cancellation := Ξ» Z h k w, fork.is_limit.hom_ext i w } end section variables {f g} /-- The identity determines a cone on the equalizer diagram of `f` and `g` if `f = g`. -/ def id_fork (h : f = g) : fork f g := fork.of_ΞΉ (πŸ™ X) $ h β–Έ rfl /-- The identity on `X` is an equalizer of `(f, g)`, if `f = g`. -/ def is_limit_id_fork (h : f = g) : is_limit (id_fork h) := fork.is_limit.mk _ (Ξ» s, fork.ΞΉ s) (Ξ» s, category.comp_id _) (Ξ» s m h, by { convert h zero, exact (category.comp_id _).symm }) /-- Every equalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def is_iso_limit_cone_parallel_pair_of_eq (hβ‚€ : f = g) {c : cone (parallel_pair f g)} (h : is_limit c) : is_iso (c.Ο€.app zero) := is_iso.of_iso $ is_limit.cone_point_unique_up_to_iso h $ is_limit_id_fork hβ‚€ /-- The equalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def equalizer.ΞΉ_of_eq [has_limit (parallel_pair f g)] (h : f = g) : is_iso (equalizer.ΞΉ f g) := is_iso_limit_cone_parallel_pair_of_eq h $ limit.is_limit _ /-- Every equalizer of `(f, f)` is an isomorphism. -/ def is_iso_limit_cone_parallel_pair_of_self {c : cone (parallel_pair f f)} (h : is_limit c) : is_iso (c.Ο€.app zero) := is_iso_limit_cone_parallel_pair_of_eq rfl h /-- An equalizer that is an epimorphism is an isomorphism. -/ def is_iso_limit_cone_parallel_pair_of_epi {c : cone (parallel_pair f g)} (h : is_limit c) [epi (c.Ο€.app zero)] : is_iso (c.Ο€.app zero) := is_iso_limit_cone_parallel_pair_of_eq ((cancel_epi _).1 (fork.condition c)) h end /-- The equalizer inclusion for `(f, f)` is an isomorphism. -/ instance equalizer.ΞΉ_of_self [has_limit (parallel_pair f f)] : is_iso (equalizer.ΞΉ f f) := equalizer.ΞΉ_of_eq rfl /-- The equalizer of a morphism with itself is isomorphic to the source. -/ def equalizer.iso_source_of_self [has_limit (parallel_pair f f)] : equalizer f f β‰… X := as_iso (equalizer.ΞΉ f f) @[simp] lemma equalizer.iso_source_of_self_hom [has_limit (parallel_pair f f)] : (equalizer.iso_source_of_self f).hom = equalizer.ΞΉ f f := rfl @[simp] lemma equalizer.iso_source_of_self_inv [has_limit (parallel_pair f f)] : (equalizer.iso_source_of_self f).inv = equalizer.lift (πŸ™ X) (by simp) := rfl section variables [has_colimit (parallel_pair f g)] /-- If we have chosen a coequalizer of `f` and `g`, we can access the corresponding object by saying `coequalizer f g`. -/ abbreviation coequalizer := colimit (parallel_pair f g) /-- If we have chosen a coequalizer of `f` and `g`, we can access the corresponding projection by saying `coequalizer.Ο€ f g`. -/ abbreviation coequalizer.Ο€ : Y ⟢ coequalizer f g := colimit.ΞΉ (parallel_pair f g) one @[simp] lemma coequalizer.Ο€.cofork : cofork.Ο€ (colimit.cocone (parallel_pair f g)) = coequalizer.Ο€ f g := rfl @[simp] lemma coequalizer.Ο€.eq_app_one : (colimit.cocone (parallel_pair f g)).ΞΉ.app one = coequalizer.Ο€ f g := rfl @[reassoc] lemma coequalizer.condition : f ≫ coequalizer.Ο€ f g = g ≫ coequalizer.Ο€ f g := cofork.condition $ colimit.cocone $ parallel_pair f g variables {f g} /-- Any morphism `k : Y ⟢ W` satisfying `f ≫ k = g ≫ k` factors through the coequalizer of `f` and `g` via `coequalizer.desc : coequalizer f g ⟢ W`. -/ abbreviation coequalizer.desc {W : C} (k : Y ⟢ W) (h : f ≫ k = g ≫ k) : coequalizer f g ⟢ W := colimit.desc (parallel_pair f g) (cofork.of_Ο€ k h) @[simp, reassoc] lemma coequalizer.Ο€_desc {W : C} (k : Y ⟢ W) (h : f ≫ k = g ≫ k) : coequalizer.Ο€ f g ≫ coequalizer.desc k h = k := colimit.ΞΉ_desc _ _ /-- Any morphism `k : Y ⟢ W` satisfying `f ≫ k = g ≫ k` induces a morphism `l : coequalizer f g ⟢ W` satisfying `coequalizer.Ο€ ≫ g = l`. -/ def coequalizer.desc' {W : C} (k : Y ⟢ W) (h : f ≫ k = g ≫ k) : {l : coequalizer f g ⟢ W // coequalizer.Ο€ f g ≫ l = k} := ⟨coequalizer.desc k h, coequalizer.Ο€_desc _ _⟩ /-- Two maps from a coequalizer are equal if they are equal when composed with the coequalizer map -/ @[ext] lemma coequalizer.hom_ext {W : C} {k l : coequalizer f g ⟢ W} (h : coequalizer.Ο€ f g ≫ k = coequalizer.Ο€ f g ≫ l) : k = l := cofork.is_colimit.hom_ext (colimit.is_colimit _) h /-- A coequalizer morphism is an epimorphism -/ instance coequalizer.Ο€_epi : epi (coequalizer.Ο€ f g) := { left_cancellation := Ξ» Z h k w, coequalizer.hom_ext w } end section variables {f g} /-- The coequalizer morphism in any colimit cocone is an epimorphism. -/ lemma epi_of_is_colimit_parallel_pair {c : cocone (parallel_pair f g)} (i : is_colimit c) : epi (c.ΞΉ.app one) := { left_cancellation := Ξ» Z h k w, cofork.is_colimit.hom_ext i w } end section variables {f g} /-- The identity determines a cocone on the coequalizer diagram of `f` and `g`, if `f = g`. -/ def id_cofork (h : f = g) : cofork f g := cofork.of_Ο€ (πŸ™ Y) $ h β–Έ rfl /-- The identity on `Y` is a coequalizer of `(f, g)`, where `f = g`. -/ def is_colimit_id_cofork (h : f = g) : is_colimit (id_cofork h) := cofork.is_colimit.mk _ (Ξ» s, cofork.Ο€ s) (Ξ» s, category.id_comp _) (Ξ» s m h, by { convert h one, exact (category.id_comp _).symm }) /-- Every coequalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def is_iso_colimit_cocone_parallel_pair_of_eq (hβ‚€ : f = g) {c : cocone (parallel_pair f g)} (h : is_colimit c) : is_iso (c.ΞΉ.app one) := is_iso.of_iso $ is_colimit.cocone_point_unique_up_to_iso (is_colimit_id_cofork hβ‚€) h /-- The coequalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def coequalizer.Ο€_of_eq [has_colimit (parallel_pair f g)] (h : f = g) : is_iso (coequalizer.Ο€ f g) := is_iso_colimit_cocone_parallel_pair_of_eq h $ colimit.is_colimit _ /-- Every coequalizer of `(f, f)` is an isomorphism. -/ def is_iso_colimit_cocone_parallel_pair_of_self {c : cocone (parallel_pair f f)} (h : is_colimit c) : is_iso (c.ΞΉ.app one) := is_iso_colimit_cocone_parallel_pair_of_eq rfl h /-- A coequalizer that is a monomorphism is an isomorphism. -/ def is_iso_limit_cocone_parallel_pair_of_epi {c : cocone (parallel_pair f g)} (h : is_colimit c) [mono (c.ΞΉ.app one)] : is_iso (c.ΞΉ.app one) := is_iso_colimit_cocone_parallel_pair_of_eq ((cancel_mono _).1 (cofork.condition c)) h end /-- The coequalizer projection for `(f, f)` is an isomorphism. -/ instance coequalizer.Ο€_of_self [has_colimit (parallel_pair f f)] : is_iso (coequalizer.Ο€ f f) := coequalizer.Ο€_of_eq rfl /-- The coequalizer of a morphism with itself is isomorphic to the target. -/ def coequalizer.iso_target_of_self [has_colimit (parallel_pair f f)] : coequalizer f f β‰… Y := (as_iso (coequalizer.Ο€ f f)).symm @[simp] lemma coequalizer.iso_target_of_self_hom [has_colimit (parallel_pair f f)] : (coequalizer.iso_target_of_self f).hom = coequalizer.desc (πŸ™ Y) (by simp) := rfl @[simp] lemma coequalizer.iso_target_of_self_inv [has_colimit (parallel_pair f f)] : (coequalizer.iso_target_of_self f).inv = coequalizer.Ο€ f f := rfl variables (C) /-- `has_equalizers` represents a choice of equalizer for every pair of morphisms -/ class has_equalizers := (has_limits_of_shape : has_limits_of_shape walking_parallel_pair C) /-- `has_coequalizers` represents a choice of coequalizer for every pair of morphisms -/ class has_coequalizers := (has_colimits_of_shape : has_colimits_of_shape walking_parallel_pair C) attribute [instance] has_equalizers.has_limits_of_shape has_coequalizers.has_colimits_of_shape /-- If `C` has all limits of diagrams `parallel_pair f g`, then it has all equalizers -/ def has_equalizers_of_has_limit_parallel_pair [Ξ  {X Y : C} {f g : X ⟢ Y}, has_limit (parallel_pair f g)] : has_equalizers C := { has_limits_of_shape := { has_limit := Ξ» F, has_limit_of_iso (diagram_iso_parallel_pair F).symm } } /-- If `C` has all colimits of diagrams `parallel_pair f g`, then it has all coequalizers -/ def has_coequalizers_of_has_colimit_parallel_pair [Ξ  {X Y : C} {f g : X ⟢ Y}, has_colimit (parallel_pair f g)] : has_coequalizers C := { has_colimits_of_shape := { has_colimit := Ξ» F, has_colimit_of_iso (diagram_iso_parallel_pair F) } } section -- In this section we show that a split mono `f` equalizes `(retraction f ≫ f)` and `(πŸ™ Y)`. variables {C} [split_mono f] /-- A split mono `f` equalizes `(retraction f ≫ f)` and `(πŸ™ Y)`. Here we build the cone, and show in `split_mono_equalizes` that it is a limit cone. -/ def cone_of_split_mono : cone (parallel_pair (πŸ™ Y) (retraction f ≫ f)) := fork.of_ΞΉ f (by tidy) @[simp] lemma cone_of_split_mono_Ο€_app_zero : (cone_of_split_mono f).Ο€.app zero = f := rfl @[simp] lemma cone_of_split_mono_Ο€_app_one : (cone_of_split_mono f).Ο€.app one = f ≫ πŸ™ Y := rfl /-- A split mono `f` equalizes `(retraction f ≫ f)` and `(πŸ™ Y)`. -/ def split_mono_equalizes {X Y : C} (f : X ⟢ Y) [split_mono f] : is_limit (cone_of_split_mono f) := { lift := Ξ» s, s.Ο€.app zero ≫ retraction f, fac' := Ξ» s, begin rintros (⟨⟩|⟨⟩), { rw [cone_of_split_mono_Ο€_app_zero], erw [category.assoc, ← s.Ο€.naturality right, s.Ο€.naturality left, category.comp_id], }, { erw [cone_of_split_mono_Ο€_app_one, category.comp_id, category.assoc, ← s.Ο€.naturality right, category.id_comp], } end, uniq' := Ξ» s m w, begin rw ←(w zero), simp, end, } end section -- In this section we show that a split epi `f` coequalizes `(f ≫ section_ f)` and `(πŸ™ X)`. variables {C} [split_epi f] /-- A split epi `f` coequalizes `(f ≫ section_ f)` and `(πŸ™ X)`. Here we build the cocone, and show in `split_epi_coequalizes` that it is a colimit cocone. -/ def cocone_of_split_epi : cocone (parallel_pair (πŸ™ X) (f ≫ section_ f)) := cofork.of_Ο€ f (by tidy) @[simp] lemma cocone_of_split_epi_ΞΉ_app_one : (cocone_of_split_epi f).ΞΉ.app one = f := rfl @[simp] lemma cocone_of_split_epi_ΞΉ_app_zero : (cocone_of_split_epi f).ΞΉ.app zero = πŸ™ X ≫ f := rfl /-- A split epi `f` coequalizes `(f ≫ section_ f)` and `(πŸ™ X)`. -/ def split_epi_coequalizes {X Y : C} (f : X ⟢ Y) [split_epi f] : is_colimit (cocone_of_split_epi f) := { desc := Ξ» s, section_ f ≫ s.ΞΉ.app one, fac' := Ξ» s, begin rintros (⟨⟩|⟨⟩), { erw [cocone_of_split_epi_ΞΉ_app_zero, category.assoc, category.id_comp, ←category.assoc, s.ΞΉ.naturality right, functor.const.obj_map, category.comp_id], }, { erw [cocone_of_split_epi_ΞΉ_app_one, ←category.assoc, s.ΞΉ.naturality right, ←s.ΞΉ.naturality left, category.id_comp] } end, uniq' := Ξ» s m w, begin rw ←(w one), simp, end, } end end category_theory.limits
89f9e49362c1adb91cf940f559de62751de7f51b
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/list/basic.lean
bbbb9ac72c1bdfe023a0f81fa6efd7ff058295d4
[ "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
161,587
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro -/ import data.nat.order.basic /-! # Basic properties of lists -/ open function nat (hiding one_pos) assert_not_exists set.range namespace list universes u v w x variables {ΞΉ : Type*} {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type x} {l₁ lβ‚‚ : list Ξ±} attribute [inline] list.head /-- There is only one list of an empty type -/ instance unique_of_is_empty [is_empty Ξ±] : unique (list Ξ±) := { uniq := Ξ» l, match l with | [] := rfl | (a :: l) := is_empty_elim a end, ..list.inhabited Ξ± } instance : is_left_id (list Ξ±) has_append.append [] := ⟨ nil_append ⟩ instance : is_right_id (list Ξ±) has_append.append [] := ⟨ append_nil ⟩ instance : is_associative (list Ξ±) has_append.append := ⟨ append_assoc ⟩ theorem cons_ne_nil (a : Ξ±) (l : list Ξ±) : a::l β‰  []. theorem cons_ne_self (a : Ξ±) (l : list Ξ±) : a::l β‰  l := mt (congr_arg length) (nat.succ_ne_self _) theorem head_eq_of_cons_eq {h₁ hβ‚‚ : Ξ±} {t₁ tβ‚‚ : list Ξ±} : (h₁::t₁) = (hβ‚‚::tβ‚‚) β†’ h₁ = hβ‚‚ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq) theorem tail_eq_of_cons_eq {h₁ hβ‚‚ : Ξ±} {t₁ tβ‚‚ : list Ξ±} : (h₁::t₁) = (hβ‚‚::tβ‚‚) β†’ t₁ = tβ‚‚ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq) @[simp] theorem cons_injective {a : Ξ±} : injective (cons a) := assume l₁ lβ‚‚, assume Pe, tail_eq_of_cons_eq Pe theorem cons_inj (a : Ξ±) {l l' : list Ξ±} : a::l = a::l' ↔ l = l' := cons_injective.eq_iff theorem cons_eq_cons {a b : Ξ±} {l l' : list Ξ±} : a::l = b::l' ↔ a = b ∧ l = l' := ⟨list.cons.inj, Ξ» h, h.1 β–Έ h.2 β–Έ rfl⟩ lemma singleton_injective : injective (Ξ» a : Ξ±, [a]) := Ξ» a b h, (cons_eq_cons.1 h).1 lemma singleton_inj {a b : Ξ±} : [a] = [b] ↔ a = b := singleton_injective.eq_iff theorem exists_cons_of_ne_nil {l : list Ξ±} (h : l β‰  nil) : βˆƒ b L, l = b :: L := by { induction l with c l', contradiction, use [c,l'], } lemma set_of_mem_cons (l : list Ξ±) (a : Ξ±) : {x | x ∈ a :: l} = insert a {x | x ∈ l} := rfl /-! ### mem -/ theorem mem_singleton_self (a : Ξ±) : a ∈ [a] := mem_cons_self _ _ theorem eq_of_mem_singleton {a b : Ξ±} : a ∈ [b] β†’ a = b := assume : a ∈ [b], or.elim (eq_or_mem_of_mem_cons this) (assume : a = b, this) (assume : a ∈ [], absurd this (not_mem_nil a)) @[simp] theorem mem_singleton {a b : Ξ±} : a ∈ [b] ↔ a = b := ⟨eq_of_mem_singleton, or.inl⟩ theorem mem_of_mem_cons_of_mem {a b : Ξ±} {l : list Ξ±} : a ∈ b::l β†’ b ∈ l β†’ a ∈ l := assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl) (assume : a = b, begin subst a, exact binl end) (assume : a ∈ l, this) theorem _root_.decidable.list.eq_or_ne_mem_of_mem [decidable_eq Ξ±] {a b : Ξ±} {l : list Ξ±} (h : a ∈ b :: l) : a = b ∨ (a β‰  b ∧ a ∈ l) := decidable.by_cases or.inl $ assume : a β‰  b, h.elim or.inl $ assume h, or.inr ⟨this, h⟩ theorem eq_or_ne_mem_of_mem {a b : Ξ±} {l : list Ξ±} : a ∈ b :: l β†’ a = b ∨ (a β‰  b ∧ a ∈ l) := by classical; exact decidable.list.eq_or_ne_mem_of_mem theorem not_mem_append {a : Ξ±} {s t : list Ξ±} (h₁ : a βˆ‰ s) (hβ‚‚ : a βˆ‰ t) : a βˆ‰ s ++ t := mt mem_append.1 $ not_or_distrib.2 ⟨h₁, hβ‚‚βŸ© theorem ne_nil_of_mem {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : l β‰  [] := by intro e; rw e at h; cases h theorem mem_split {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : βˆƒ s t : list Ξ±, l = s ++ a :: t := begin induction l with b l ih, {cases h}, rcases h with rfl | h, { exact ⟨[], l, rfl⟩ }, { rcases ih h with ⟨s, t, rfl⟩, exact ⟨b::s, t, rfl⟩ } end theorem mem_of_ne_of_mem {a y : Ξ±} {l : list Ξ±} (h₁ : a β‰  y) (hβ‚‚ : a ∈ y :: l) : a ∈ l := or.elim (eq_or_mem_of_mem_cons hβ‚‚) (Ξ»e, absurd e h₁) (Ξ»r, r) theorem ne_of_not_mem_cons {a b : Ξ±} {l : list Ξ±} : a βˆ‰ b::l β†’ a β‰  b := assume nin aeqb, absurd (or.inl aeqb) nin theorem not_mem_of_not_mem_cons {a b : Ξ±} {l : list Ξ±} : a βˆ‰ b::l β†’ a βˆ‰ l := assume nin nainl, absurd (or.inr nainl) nin theorem not_mem_cons_of_ne_of_not_mem {a y : Ξ±} {l : list Ξ±} : a β‰  y β†’ a βˆ‰ l β†’ a βˆ‰ y::l := assume p1 p2, not.intro (assume Pain, absurd (eq_or_mem_of_mem_cons Pain) (not_or p1 p2)) theorem ne_and_not_mem_of_not_mem_cons {a y : Ξ±} {l : list Ξ±} : a βˆ‰ y::l β†’ a β‰  y ∧ a βˆ‰ l := assume p, and.intro (ne_of_not_mem_cons p) (not_mem_of_not_mem_cons p) @[simp] theorem mem_map {f : Ξ± β†’ Ξ²} {b : Ξ²} {l : list Ξ±} : b ∈ map f l ↔ βˆƒ a, a ∈ l ∧ f a = b := begin -- This proof uses no axioms, that's why it's longer that `induction`; simp [...] induction l with a l ihl, { split, { rintro ⟨_⟩ }, { rintro ⟨a, ⟨_⟩, _⟩ } }, { refine (or_congr eq_comm ihl).trans _, split, { rintro (h|⟨c, hcl, h⟩), exacts [⟨a, or.inl rfl, h⟩, ⟨c, or.inr hcl, h⟩] }, { rintro ⟨c, (hc|hc), h⟩, exacts [or.inl $ (congr_arg f hc.symm).trans h, or.inr ⟨c, hc, h⟩] } } end alias mem_map ↔ exists_of_mem_map _ theorem mem_map_of_mem (f : Ξ± β†’ Ξ²) {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : f a ∈ map f l := mem_map.2 ⟨a, h, rfl⟩ theorem mem_map_of_injective {f : Ξ± β†’ Ξ²} (H : injective f) {a : Ξ±} {l : list Ξ±} : f a ∈ map f l ↔ a ∈ l := ⟨λ m, let ⟨a', m', e⟩ := exists_of_mem_map m in H e β–Έ m', mem_map_of_mem _⟩ @[simp] lemma _root_.function.involutive.exists_mem_and_apply_eq_iff {f : Ξ± β†’ Ξ±} (hf : function.involutive f) (x : Ξ±) (l : list Ξ±) : (βˆƒ (y : Ξ±), y ∈ l ∧ f y = x) ↔ f x ∈ l := ⟨by { rintro ⟨y, h, rfl⟩, rwa hf y }, Ξ» h, ⟨f x, h, hf _⟩⟩ theorem mem_map_of_involutive {f : Ξ± β†’ Ξ±} (hf : involutive f) {a : Ξ±} {l : list Ξ±} : a ∈ map f l ↔ f a ∈ l := by rw [mem_map, hf.exists_mem_and_apply_eq_iff] lemma forall_mem_map_iff {f : Ξ± β†’ Ξ²} {l : list Ξ±} {P : Ξ² β†’ Prop} : (βˆ€ i ∈ l.map f, P i) ↔ βˆ€ j ∈ l, P (f j) := begin split, { assume H j hj, exact H (f j) (mem_map_of_mem f hj) }, { assume H i hi, rcases mem_map.1 hi with ⟨j, hj, ji⟩, rw ← ji, exact H j hj } end @[simp] lemma map_eq_nil {f : Ξ± β†’ Ξ²} {l : list Ξ±} : list.map f l = [] ↔ l = [] := ⟨by cases l; simp only [forall_prop_of_true, map, forall_prop_of_false, not_false_iff], Ξ» h, h.symm β–Έ rfl⟩ @[simp] theorem mem_join {a : Ξ±} : βˆ€ {L : list (list Ξ±)}, a ∈ join L ↔ βˆƒ l, l ∈ L ∧ a ∈ l | [] := ⟨false.elim, λ⟨_, h, _⟩, false.elim h⟩ | (c :: L) := by simp only [join, mem_append, @mem_join L, mem_cons_iff, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem exists_of_mem_join {a : Ξ±} {L : list (list Ξ±)} : a ∈ join L β†’ βˆƒ l, l ∈ L ∧ a ∈ l := mem_join.1 theorem mem_join_of_mem {a : Ξ±} {L : list (list Ξ±)} {l} (lL : l ∈ L) (al : a ∈ l) : a ∈ join L := mem_join.2 ⟨l, lL, al⟩ @[simp] theorem mem_bind {b : Ξ²} {l : list Ξ±} {f : Ξ± β†’ list Ξ²} : b ∈ list.bind l f ↔ βˆƒ a ∈ l, b ∈ f a := iff.trans mem_join ⟨λ ⟨l', h1, h2⟩, let ⟨a, al, fa⟩ := exists_of_mem_map h1 in ⟨a, al, fa.symm β–Έ h2⟩, Ξ» ⟨a, al, bfa⟩, ⟨f a, mem_map_of_mem _ al, bfa⟩⟩ theorem exists_of_mem_bind {b : Ξ²} {l : list Ξ±} {f : Ξ± β†’ list Ξ²} : b ∈ list.bind l f β†’ βˆƒ a ∈ l, b ∈ f a := mem_bind.1 theorem mem_bind_of_mem {b : Ξ²} {l : list Ξ±} {f : Ξ± β†’ list Ξ²} {a} (al : a ∈ l) (h : b ∈ f a) : b ∈ list.bind l f := mem_bind.2 ⟨a, al, h⟩ lemma bind_map {g : Ξ± β†’ list Ξ²} {f : Ξ² β†’ Ξ³} : βˆ€(l : list Ξ±), list.map f (l.bind g) = l.bind (Ξ»a, (g a).map f) | [] := rfl | (a::l) := by simp only [cons_bind, map_append, bind_map l] lemma map_bind (g : Ξ² β†’ list Ξ³) (f : Ξ± β†’ Ξ²) : βˆ€ l : list Ξ±, (list.map f l).bind g = l.bind (Ξ» a, g (f a)) | [] := rfl | (a::l) := by simp only [cons_bind, map_cons, map_bind l] /-! ### length -/ theorem length_eq_zero {l : list Ξ±} : length l = 0 ↔ l = [] := ⟨eq_nil_of_length_eq_zero, Ξ» h, h.symm β–Έ rfl⟩ @[simp] lemma length_singleton (a : Ξ±) : length [a] = 1 := rfl theorem length_pos_of_mem {a : Ξ±} : βˆ€ {l : list Ξ±}, a ∈ l β†’ 0 < length l | (b::l) _ := zero_lt_succ _ theorem exists_mem_of_length_pos : βˆ€ {l : list Ξ±}, 0 < length l β†’ βˆƒ a, a ∈ l | (b::l) _ := ⟨b, mem_cons_self _ _⟩ theorem length_pos_iff_exists_mem {l : list Ξ±} : 0 < length l ↔ βˆƒ a, a ∈ l := ⟨exists_mem_of_length_pos, Ξ» ⟨a, h⟩, length_pos_of_mem h⟩ theorem ne_nil_of_length_pos {l : list Ξ±} : 0 < length l β†’ l β‰  [] := Ξ» h1 h2, lt_irrefl 0 ((length_eq_zero.2 h2).subst h1) theorem length_pos_of_ne_nil {l : list Ξ±} : l β‰  [] β†’ 0 < length l := Ξ» h, pos_iff_ne_zero.2 $ Ξ» h0, h $ length_eq_zero.1 h0 theorem length_pos_iff_ne_nil {l : list Ξ±} : 0 < length l ↔ l β‰  [] := ⟨ne_nil_of_length_pos, length_pos_of_ne_nil⟩ lemma exists_mem_of_ne_nil (l : list Ξ±) (h : l β‰  []) : βˆƒ x, x ∈ l := exists_mem_of_length_pos (length_pos_of_ne_nil h) theorem length_eq_one {l : list Ξ±} : length l = 1 ↔ βˆƒ a, l = [a] := ⟨match l with [a], _ := ⟨a, rfl⟩ end, Ξ» ⟨a, e⟩, e.symm β–Έ rfl⟩ lemma exists_of_length_succ {n} : βˆ€ l : list Ξ±, l.length = n + 1 β†’ βˆƒ h t, l = h :: t | [] H := absurd H.symm $ succ_ne_zero n | (h :: t) H := ⟨h, t, rfl⟩ @[simp] lemma length_injective_iff : injective (list.length : list Ξ± β†’ β„•) ↔ subsingleton Ξ± := begin split, { intro h, refine ⟨λ x y, _⟩, suffices : [x] = [y], { simpa using this }, apply h, refl }, { intros hΞ± l1 l2 hl, induction l1 generalizing l2; cases l2, { refl }, { cases hl }, { cases hl }, congr, exactI subsingleton.elim _ _, apply l1_ih, simpa using hl } end @[simp] lemma length_injective [subsingleton Ξ±] : injective (length : list Ξ± β†’ β„•) := length_injective_iff.mpr $ by apply_instance lemma length_eq_two {l : list Ξ±} : l.length = 2 ↔ βˆƒ a b, l = [a, b] := ⟨match l with [a, b], _ := ⟨a, b, rfl⟩ end, Ξ» ⟨a, b, e⟩, e.symm β–Έ rfl⟩ lemma length_eq_three {l : list Ξ±} : l.length = 3 ↔ βˆƒ a b c, l = [a, b, c] := ⟨match l with [a, b, c], _ := ⟨a, b, c, rfl⟩ end, Ξ» ⟨a, b, c, e⟩, e.symm β–Έ rfl⟩ alias length_le_of_sublist ← sublist.length_le /-! ### set-theoretic notation of lists -/ lemma empty_eq : (βˆ… : list Ξ±) = [] := by refl lemma singleton_eq (x : Ξ±) : ({x} : list Ξ±) = [x] := rfl lemma insert_neg [decidable_eq Ξ±] {x : Ξ±} {l : list Ξ±} (h : x βˆ‰ l) : has_insert.insert x l = x :: l := if_neg h lemma insert_pos [decidable_eq Ξ±] {x : Ξ±} {l : list Ξ±} (h : x ∈ l) : has_insert.insert x l = l := if_pos h lemma doubleton_eq [decidable_eq Ξ±] {x y : Ξ±} (h : x β‰  y) : ({x, y} : list Ξ±) = [x, y] := by { rw [insert_neg, singleton_eq], rwa [singleton_eq, mem_singleton] } /-! ### bounded quantifiers over lists -/ theorem forall_mem_nil (p : Ξ± β†’ Prop) : βˆ€ x ∈ @nil Ξ±, p x. theorem forall_mem_cons : βˆ€ {p : Ξ± β†’ Prop} {a : Ξ±} {l : list Ξ±}, (βˆ€ x ∈ a :: l, p x) ↔ p a ∧ βˆ€ x ∈ l, p x := ball_cons theorem forall_mem_of_forall_mem_cons {p : Ξ± β†’ Prop} {a : Ξ±} {l : list Ξ±} (h : βˆ€ x ∈ a :: l, p x) : βˆ€ x ∈ l, p x := (forall_mem_cons.1 h).2 theorem forall_mem_singleton {p : Ξ± β†’ Prop} {a : Ξ±} : (βˆ€ x ∈ [a], p x) ↔ p a := by simp only [mem_singleton, forall_eq] theorem forall_mem_append {p : Ξ± β†’ Prop} {l₁ lβ‚‚ : list Ξ±} : (βˆ€ x ∈ l₁ ++ lβ‚‚, p x) ↔ (βˆ€ x ∈ l₁, p x) ∧ (βˆ€ x ∈ lβ‚‚, p x) := by simp only [mem_append, or_imp_distrib, forall_and_distrib] theorem not_exists_mem_nil (p : Ξ± β†’ Prop) : Β¬ βˆƒ x ∈ @nil Ξ±, p x. theorem exists_mem_cons_of {p : Ξ± β†’ Prop} {a : Ξ±} (l : list Ξ±) (h : p a) : βˆƒ x ∈ a :: l, p x := bex.intro a (mem_cons_self _ _) h theorem exists_mem_cons_of_exists {p : Ξ± β†’ Prop} {a : Ξ±} {l : list Ξ±} (h : βˆƒ x ∈ l, p x) : βˆƒ x ∈ a :: l, p x := bex.elim h (Ξ» x xl px, bex.intro x (mem_cons_of_mem _ xl) px) theorem or_exists_of_exists_mem_cons {p : Ξ± β†’ Prop} {a : Ξ±} {l : list Ξ±} (h : βˆƒ x ∈ a :: l, p x) : p a ∨ βˆƒ x ∈ l, p x := bex.elim h (Ξ» x xal px, or.elim (eq_or_mem_of_mem_cons xal) (assume : x = a, begin rw ←this, left, exact px end) (assume : x ∈ l, or.inr (bex.intro x this px))) theorem exists_mem_cons_iff (p : Ξ± β†’ Prop) (a : Ξ±) (l : list Ξ±) : (βˆƒ x ∈ a :: l, p x) ↔ p a ∨ βˆƒ x ∈ l, p x := iff.intro or_exists_of_exists_mem_cons (assume h, or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists) /-! ### list subset -/ theorem subset_def {l₁ lβ‚‚ : list Ξ±} : l₁ βŠ† lβ‚‚ ↔ βˆ€ ⦃a : α⦄, a ∈ l₁ β†’ a ∈ lβ‚‚ := iff.rfl theorem subset_append_of_subset_left (l l₁ lβ‚‚ : list Ξ±) : l βŠ† l₁ β†’ l βŠ† l₁++lβ‚‚ := Ξ» s, subset.trans s $ subset_append_left _ _ theorem subset_append_of_subset_right (l l₁ lβ‚‚ : list Ξ±) : l βŠ† lβ‚‚ β†’ l βŠ† l₁++lβ‚‚ := Ξ» s, subset.trans s $ subset_append_right _ _ @[simp] theorem cons_subset {a : Ξ±} {l m : list Ξ±} : a::l βŠ† m ↔ a ∈ m ∧ l βŠ† m := by simp only [subset_def, mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] theorem cons_subset_of_subset_of_mem {a : Ξ±} {l m : list Ξ±} (ainm : a ∈ m) (lsubm : l βŠ† m) : a::l βŠ† m := cons_subset.2 ⟨ainm, lsubm⟩ theorem append_subset_of_subset_of_subset {l₁ lβ‚‚ l : list Ξ±} (l₁subl : l₁ βŠ† l) (lβ‚‚subl : lβ‚‚ βŠ† l) : l₁ ++ lβ‚‚ βŠ† l := Ξ» a h, (mem_append.1 h).elim (@l₁subl _) (@lβ‚‚subl _) @[simp] theorem append_subset_iff {l₁ lβ‚‚ l : list Ξ±} : l₁ ++ lβ‚‚ βŠ† l ↔ l₁ βŠ† l ∧ lβ‚‚ βŠ† l := begin split, { intro h, simp only [subset_def] at *, split; intros; simp* }, { rintro ⟨h1, h2⟩, apply append_subset_of_subset_of_subset h1 h2 } end theorem eq_nil_of_subset_nil : βˆ€ {l : list Ξ±}, l βŠ† [] β†’ l = [] | [] s := rfl | (a::l) s := false.elim $ s $ mem_cons_self a l theorem eq_nil_iff_forall_not_mem {l : list Ξ±} : l = [] ↔ βˆ€ a, a βˆ‰ l := show l = [] ↔ l βŠ† [], from ⟨λ e, e β–Έ subset.refl _, eq_nil_of_subset_nil⟩ theorem map_subset {l₁ lβ‚‚ : list Ξ±} (f : Ξ± β†’ Ξ²) (H : l₁ βŠ† lβ‚‚) : map f l₁ βŠ† map f lβ‚‚ := Ξ» x, by simp only [mem_map, not_and, exists_imp_distrib, and_imp]; exact Ξ» a h e, ⟨a, H h, e⟩ theorem map_subset_iff {l₁ lβ‚‚ : list Ξ±} (f : Ξ± β†’ Ξ²) (h : injective f) : map f l₁ βŠ† map f lβ‚‚ ↔ l₁ βŠ† lβ‚‚ := begin refine ⟨_, map_subset f⟩, intros h2 x hx, rcases mem_map.1 (h2 (mem_map_of_mem f hx)) with ⟨x', hx', hxx'⟩, cases h hxx', exact hx' end /-! ### append -/ lemma append_eq_has_append {L₁ Lβ‚‚ : list Ξ±} : list.append L₁ Lβ‚‚ = L₁ ++ Lβ‚‚ := rfl @[simp] lemma singleton_append {x : Ξ±} {l : list Ξ±} : [x] ++ l = x :: l := rfl theorem append_ne_nil_of_ne_nil_left (s t : list Ξ±) : s β‰  [] β†’ s ++ t β‰  [] := by induction s; intros; contradiction theorem append_ne_nil_of_ne_nil_right (s t : list Ξ±) : t β‰  [] β†’ s ++ t β‰  [] := by induction s; intros; contradiction @[simp] lemma append_eq_nil {p q : list Ξ±} : (p ++ q) = [] ↔ p = [] ∧ q = [] := by cases p; simp only [nil_append, cons_append, eq_self_iff_true, true_and, false_and] @[simp] lemma nil_eq_append_iff {a b : list Ξ±} : [] = a ++ b ↔ a = [] ∧ b = [] := by rw [eq_comm, append_eq_nil] lemma append_eq_cons_iff {a b c : list Ξ±} {x : Ξ±} : a ++ b = x :: c ↔ (a = [] ∧ b = x :: c) ∨ (βˆƒa', a = x :: a' ∧ c = a' ++ b) := by cases a; simp only [and_assoc, @eq_comm _ c, nil_append, cons_append, eq_self_iff_true, true_and, false_and, exists_false, false_or, or_false, exists_and_distrib_left, exists_eq_left'] lemma cons_eq_append_iff {a b c : list Ξ±} {x : Ξ±} : (x :: c : list Ξ±) = a ++ b ↔ (a = [] ∧ b = x :: c) ∨ (βˆƒa', a = x :: a' ∧ c = a' ++ b) := by rw [eq_comm, append_eq_cons_iff] lemma append_eq_append_iff {a b c d : list Ξ±} : a ++ b = c ++ d ↔ (βˆƒa', c = a ++ a' ∧ b = a' ++ d) ∨ (βˆƒc', a = c ++ c' ∧ d = c' ++ b) := begin induction a generalizing c, case nil { rw nil_append, split, { rintro rfl, left, exact ⟨_, rfl, rfl⟩ }, { rintro (⟨a', rfl, rfl⟩ | ⟨a', H, rfl⟩), {refl}, {rw [← append_assoc, ← H], refl} } }, case cons : a as ih { cases c, { simp only [cons_append, nil_append, false_and, exists_false, false_or, exists_eq_left'], exact eq_comm }, { simp only [cons_append, @eq_comm _ a, ih, and_assoc, and_or_distrib_left, exists_and_distrib_left] } } end @[simp] theorem take_append_drop : βˆ€ (n : β„•) (l : list Ξ±), take n l ++ drop n l = l | 0 a := rfl | (succ n) [] := rfl | (succ n) (x :: xs) := congr_arg (cons x) $ take_append_drop n xs -- TODO(Leo): cleanup proof after arith dec proc theorem append_inj : βˆ€ {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±}, s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚ β†’ length s₁ = length sβ‚‚ β†’ s₁ = sβ‚‚ ∧ t₁ = tβ‚‚ | [] [] t₁ tβ‚‚ h hl := ⟨rfl, h⟩ | (a::s₁) [] t₁ tβ‚‚ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl | [] (b::sβ‚‚) t₁ tβ‚‚ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl.symm | (a::s₁) (b::sβ‚‚) t₁ tβ‚‚ h hl := list.no_confusion h $ Ξ»ab hap, let ⟨e1, e2⟩ := @append_inj s₁ sβ‚‚ t₁ tβ‚‚ hap (succ.inj hl) in by rw [ab, e1, e2]; exact ⟨rfl, rfl⟩ theorem append_inj_right {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±} (h : s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚) (hl : length s₁ = length sβ‚‚) : t₁ = tβ‚‚ := (append_inj h hl).right theorem append_inj_left {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±} (h : s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚) (hl : length s₁ = length sβ‚‚) : s₁ = sβ‚‚ := (append_inj h hl).left theorem append_inj' {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±} (h : s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚) (hl : length t₁ = length tβ‚‚) : s₁ = sβ‚‚ ∧ t₁ = tβ‚‚ := append_inj h $ @nat.add_right_cancel _ (length t₁) _ $ let hap := congr_arg length h in by simp only [length_append] at hap; rwa [← hl] at hap theorem append_inj_right' {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±} (h : s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚) (hl : length t₁ = length tβ‚‚) : t₁ = tβ‚‚ := (append_inj' h hl).right theorem append_inj_left' {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±} (h : s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚) (hl : length t₁ = length tβ‚‚) : s₁ = sβ‚‚ := (append_inj' h hl).left theorem append_left_cancel {s t₁ tβ‚‚ : list Ξ±} (h : s ++ t₁ = s ++ tβ‚‚) : t₁ = tβ‚‚ := append_inj_right h rfl theorem append_right_cancel {s₁ sβ‚‚ t : list Ξ±} (h : s₁ ++ t = sβ‚‚ ++ t) : s₁ = sβ‚‚ := append_inj_left' h rfl theorem append_right_injective (s : list Ξ±) : function.injective (Ξ» t, s ++ t) := Ξ» t₁ tβ‚‚, append_left_cancel theorem append_right_inj {t₁ tβ‚‚ : list Ξ±} (s) : s ++ t₁ = s ++ tβ‚‚ ↔ t₁ = tβ‚‚ := (append_right_injective s).eq_iff theorem append_left_injective (t : list Ξ±) : function.injective (Ξ» s, s ++ t) := Ξ» s₁ sβ‚‚, append_right_cancel theorem append_left_inj {s₁ sβ‚‚ : list Ξ±} (t) : s₁ ++ t = sβ‚‚ ++ t ↔ s₁ = sβ‚‚ := (append_left_injective t).eq_iff theorem map_eq_append_split {f : Ξ± β†’ Ξ²} {l : list Ξ±} {s₁ sβ‚‚ : list Ξ²} (h : map f l = s₁ ++ sβ‚‚) : βˆƒ l₁ lβ‚‚, l = l₁ ++ lβ‚‚ ∧ map f l₁ = s₁ ∧ map f lβ‚‚ = sβ‚‚ := begin have := h, rw [← take_append_drop (length s₁) l] at this ⊒, rw map_append at this, refine ⟨_, _, rfl, append_inj this _⟩, rw [length_map, length_take, min_eq_left], rw [← length_map f l, h, length_append], apply nat.le_add_right end /-! ### repeat -/ @[simp] theorem repeat_succ (a : Ξ±) (n) : repeat a (n + 1) = a :: repeat a n := rfl theorem mem_repeat {a b : Ξ±} : βˆ€ {n}, b ∈ repeat a n ↔ n β‰  0 ∧ b = a | 0 := by simp | (n + 1) := by simp [mem_repeat] theorem eq_of_mem_repeat {a b : Ξ±} {n} (h : b ∈ repeat a n) : b = a := (mem_repeat.1 h).2 theorem eq_repeat_of_mem {a : Ξ±} : βˆ€ {l : list Ξ±}, (βˆ€ b ∈ l, b = a) β†’ l = repeat a l.length | [] H := rfl | (b::l) H := by cases forall_mem_cons.1 H with H₁ Hβ‚‚; unfold length repeat; congr; [exact H₁, exact eq_repeat_of_mem Hβ‚‚] theorem eq_repeat' {a : Ξ±} {l : list Ξ±} : l = repeat a l.length ↔ βˆ€ b ∈ l, b = a := ⟨λ h, h.symm β–Έ Ξ» b, eq_of_mem_repeat, eq_repeat_of_mem⟩ theorem eq_repeat {a : Ξ±} {n} {l : list Ξ±} : l = repeat a n ↔ length l = n ∧ βˆ€ b ∈ l, b = a := ⟨λ h, h.symm β–Έ ⟨length_repeat _ _, Ξ» b, eq_of_mem_repeat⟩, Ξ» ⟨e, al⟩, e β–Έ eq_repeat_of_mem al⟩ theorem repeat_add (a : Ξ±) (m n) : repeat a (m + n) = repeat a m ++ repeat a n := by induction m; simp only [*, zero_add, succ_add, repeat]; split; refl theorem repeat_subset_singleton (a : Ξ±) (n) : repeat a n βŠ† [a] := Ξ» b h, mem_singleton.2 (eq_of_mem_repeat h) lemma subset_singleton_iff {a : Ξ±} : βˆ€ L : list Ξ±, L βŠ† [a] ↔ βˆƒ n, L = repeat a n | [] := ⟨λ h, ⟨0, by simp⟩, by simp⟩ | (h :: L) := begin refine ⟨λ h, _, Ξ» ⟨k, hk⟩, by simp [hk, repeat_subset_singleton]⟩, rw [cons_subset] at h, obtain ⟨n, rfl⟩ := (subset_singleton_iff L).mp h.2, exact ⟨n.succ, by simp [mem_singleton.mp h.1]⟩ end @[simp] theorem map_const (l : list Ξ±) (b : Ξ²) : map (function.const Ξ± b) l = repeat b l.length := by induction l; [refl, simp only [*, map]]; split; refl theorem eq_of_mem_map_const {b₁ bβ‚‚ : Ξ²} {l : list Ξ±} (h : b₁ ∈ map (function.const Ξ± bβ‚‚) l) : b₁ = bβ‚‚ := by rw map_const at h; exact eq_of_mem_repeat h @[simp] theorem map_repeat (f : Ξ± β†’ Ξ²) (a : Ξ±) (n) : map f (repeat a n) = repeat (f a) n := by induction n; [refl, simp only [*, repeat, map]]; split; refl @[simp] theorem tail_repeat (a : Ξ±) (n) : tail (repeat a n) = repeat a n.pred := by cases n; refl @[simp] theorem join_repeat_nil (n : β„•) : join (repeat [] n) = @nil Ξ± := by induction n; [refl, simp only [*, repeat, join, append_nil]] lemma repeat_left_injective {n : β„•} (hn : n β‰  0) : function.injective (Ξ» a : Ξ±, repeat a n) := Ξ» a b h, (eq_repeat.1 h).2 _ $ mem_repeat.2 ⟨hn, rfl⟩ lemma repeat_left_inj {a b : Ξ±} {n : β„•} (hn : n β‰  0) : repeat a n = repeat b n ↔ a = b := (repeat_left_injective hn).eq_iff @[simp] lemma repeat_left_inj' {a b : Ξ±} : βˆ€ {n}, repeat a n = repeat b n ↔ n = 0 ∨ a = b | 0 := by simp | (n + 1) := (repeat_left_inj n.succ_ne_zero).trans $ by simp only [n.succ_ne_zero, false_or] lemma repeat_right_injective (a : Ξ±) : function.injective (repeat a) := function.left_inverse.injective (length_repeat a) @[simp] lemma repeat_right_inj {a : Ξ±} {n m : β„•} : repeat a n = repeat a m ↔ n = m := (repeat_right_injective a).eq_iff /-! ### pure -/ @[simp] theorem mem_pure {Ξ±} (x y : Ξ±) : x ∈ (pure y : list Ξ±) ↔ x = y := by simp! [pure,list.ret] /-! ### bind -/ @[simp] theorem bind_eq_bind {Ξ± Ξ²} (f : Ξ± β†’ list Ξ²) (l : list Ξ±) : l >>= f = l.bind f := rfl -- TODO: duplicate of a lemma in core theorem bind_append (f : Ξ± β†’ list Ξ²) (l₁ lβ‚‚ : list Ξ±) : (l₁ ++ lβ‚‚).bind f = l₁.bind f ++ lβ‚‚.bind f := append_bind _ _ _ @[simp] theorem bind_singleton (f : Ξ± β†’ list Ξ²) (x : Ξ±) : [x].bind f = f x := append_nil (f x) @[simp] theorem bind_singleton' (l : list Ξ±) : l.bind (Ξ» x, [x]) = l := bind_pure l theorem map_eq_bind {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (l : list Ξ±) : map f l = l.bind (Ξ» x, [f x]) := by { transitivity, rw [← bind_singleton' l, bind_map], refl } theorem bind_assoc {Ξ± Ξ²} (l : list Ξ±) (f : Ξ± β†’ list Ξ²) (g : Ξ² β†’ list Ξ³) : (l.bind f).bind g = l.bind (Ξ» x, (f x).bind g) := by induction l; simp * /-! ### concat -/ theorem concat_nil (a : Ξ±) : concat [] a = [a] := rfl theorem concat_cons (a b : Ξ±) (l : list Ξ±) : concat (a :: l) b = a :: concat l b := rfl @[simp] theorem concat_eq_append (a : Ξ±) (l : list Ξ±) : concat l a = l ++ [a] := by induction l; simp only [*, concat]; split; refl theorem init_eq_of_concat_eq {a : Ξ±} {l₁ lβ‚‚ : list Ξ±} : concat l₁ a = concat lβ‚‚ a β†’ l₁ = lβ‚‚ := begin intro h, rw [concat_eq_append, concat_eq_append] at h, exact append_right_cancel h end theorem last_eq_of_concat_eq {a b : Ξ±} {l : list Ξ±} : concat l a = concat l b β†’ a = b := begin intro h, rw [concat_eq_append, concat_eq_append] at h, exact head_eq_of_cons_eq (append_left_cancel h) end theorem concat_ne_nil (a : Ξ±) (l : list Ξ±) : concat l a β‰  [] := by simp theorem concat_append (a : Ξ±) (l₁ lβ‚‚ : list Ξ±) : concat l₁ a ++ lβ‚‚ = l₁ ++ a :: lβ‚‚ := by simp theorem length_concat (a : Ξ±) (l : list Ξ±) : length (concat l a) = succ (length l) := by simp only [concat_eq_append, length_append, length] theorem append_concat (a : Ξ±) (l₁ lβ‚‚ : list Ξ±) : l₁ ++ concat lβ‚‚ a = concat (l₁ ++ lβ‚‚) a := by simp /-! ### reverse -/ @[simp] theorem reverse_nil : reverse (@nil Ξ±) = [] := rfl local attribute [simp] reverse_core @[simp] theorem reverse_cons (a : Ξ±) (l : list Ξ±) : reverse (a::l) = reverse l ++ [a] := have aux : βˆ€ l₁ lβ‚‚, reverse_core l₁ lβ‚‚ ++ [a] = reverse_core l₁ (lβ‚‚ ++ [a]), by intro l₁; induction l₁; intros; [refl, simp only [*, reverse_core, cons_append]], (aux l nil).symm theorem reverse_core_eq (l₁ lβ‚‚ : list Ξ±) : reverse_core l₁ lβ‚‚ = reverse l₁ ++ lβ‚‚ := by induction l₁ generalizing lβ‚‚; [refl, simp only [*, reverse_core, reverse_cons, append_assoc]]; refl theorem reverse_cons' (a : Ξ±) (l : list Ξ±) : reverse (a::l) = concat (reverse l) a := by simp only [reverse_cons, concat_eq_append] @[simp] theorem reverse_singleton (a : Ξ±) : reverse [a] = [a] := rfl @[simp] theorem reverse_append (s t : list Ξ±) : reverse (s ++ t) = (reverse t) ++ (reverse s) := by induction s; [rw [nil_append, reverse_nil, append_nil], simp only [*, cons_append, reverse_cons, append_assoc]] theorem reverse_concat (l : list Ξ±) (a : Ξ±) : reverse (concat l a) = a :: reverse l := by rw [concat_eq_append, reverse_append, reverse_singleton, singleton_append] @[simp] theorem reverse_reverse (l : list Ξ±) : reverse (reverse l) = l := by induction l; [refl, simp only [*, reverse_cons, reverse_append]]; refl @[simp] theorem reverse_involutive : involutive (@reverse Ξ±) := reverse_reverse @[simp] theorem reverse_injective : injective (@reverse Ξ±) := reverse_involutive.injective theorem reverse_surjective : surjective (@reverse Ξ±) := reverse_involutive.surjective theorem reverse_bijective : bijective (@reverse Ξ±) := reverse_involutive.bijective @[simp] theorem reverse_inj {l₁ lβ‚‚ : list Ξ±} : reverse l₁ = reverse lβ‚‚ ↔ l₁ = lβ‚‚ := reverse_injective.eq_iff lemma reverse_eq_iff {l l' : list Ξ±} : l.reverse = l' ↔ l = l'.reverse := reverse_involutive.eq_iff @[simp] theorem reverse_eq_nil {l : list Ξ±} : reverse l = [] ↔ l = [] := @reverse_inj _ l [] theorem concat_eq_reverse_cons (a : Ξ±) (l : list Ξ±) : concat l a = reverse (a :: reverse l) := by simp only [concat_eq_append, reverse_cons, reverse_reverse] @[simp] theorem length_reverse (l : list Ξ±) : length (reverse l) = length l := by induction l; [refl, simp only [*, reverse_cons, length_append, length]] @[simp] theorem map_reverse (f : Ξ± β†’ Ξ²) (l : list Ξ±) : map f (reverse l) = reverse (map f l) := by induction l; [refl, simp only [*, map, reverse_cons, map_append]] theorem map_reverse_core (f : Ξ± β†’ Ξ²) (l₁ lβ‚‚ : list Ξ±) : map f (reverse_core l₁ lβ‚‚) = reverse_core (map f l₁) (map f lβ‚‚) := by simp only [reverse_core_eq, map_append, map_reverse] @[simp] theorem mem_reverse {a : Ξ±} {l : list Ξ±} : a ∈ reverse l ↔ a ∈ l := by induction l; [refl, simp only [*, reverse_cons, mem_append, mem_singleton, mem_cons_iff, not_mem_nil, false_or, or_false, or_comm]] @[simp] theorem reverse_repeat (a : Ξ±) (n) : reverse (repeat a n) = repeat a n := eq_repeat.2 ⟨by simp only [length_reverse, length_repeat], Ξ» b h, eq_of_mem_repeat (mem_reverse.1 h)⟩ /-! ### empty -/ attribute [simp] list.empty lemma empty_iff_eq_nil {l : list Ξ±} : l.empty ↔ l = [] := list.cases_on l (by simp) (by simp) /-! ### init -/ @[simp] theorem length_init : βˆ€ (l : list Ξ±), length (init l) = length l - 1 | [] := rfl | [a] := rfl | (a :: b :: l) := begin rw init, simp only [add_left_inj, length, succ_add_sub_one], exact length_init (b :: l) end /-! ### last -/ @[simp] theorem last_cons {a : Ξ±} {l : list Ξ±} : βˆ€ (h : l β‰  nil), last (a :: l) (cons_ne_nil a l) = last l h := by {induction l; intros, contradiction, reflexivity} @[simp] theorem last_append_singleton {a : Ξ±} (l : list Ξ±) : last (l ++ [a]) (append_ne_nil_of_ne_nil_right l _ (cons_ne_nil a _)) = a := by induction l; [refl, simp only [cons_append, last_cons (Ξ» H, cons_ne_nil _ _ (append_eq_nil.1 H).2), *]] theorem last_append (l₁ lβ‚‚ : list Ξ±) (h : lβ‚‚ β‰  []) : last (l₁ ++ lβ‚‚) (append_ne_nil_of_ne_nil_right l₁ lβ‚‚ h) = last lβ‚‚ h := begin induction l₁ with _ _ ih, { simp }, { simp only [cons_append], rw list.last_cons, exact ih }, end theorem last_concat {a : Ξ±} (l : list Ξ±) : last (concat l a) (concat_ne_nil a l) = a := by simp only [concat_eq_append, last_append_singleton] @[simp] theorem last_singleton (a : Ξ±) : last [a] (cons_ne_nil a []) = a := rfl @[simp] theorem last_cons_cons (a₁ aβ‚‚ : Ξ±) (l : list Ξ±) : last (a₁::aβ‚‚::l) (cons_ne_nil _ _) = last (aβ‚‚::l) (cons_ne_nil aβ‚‚ l) := rfl theorem init_append_last : βˆ€ {l : list Ξ±} (h : l β‰  []), init l ++ [last l h] = l | [] h := absurd rfl h | [a] h := rfl | (a::b::l) h := begin rw [init, cons_append, last_cons (cons_ne_nil _ _)], congr, exact init_append_last (cons_ne_nil b l) end theorem last_congr {l₁ lβ‚‚ : list Ξ±} (h₁ : l₁ β‰  []) (hβ‚‚ : lβ‚‚ β‰  []) (h₃ : l₁ = lβ‚‚) : last l₁ h₁ = last lβ‚‚ hβ‚‚ := by subst l₁ theorem last_mem : βˆ€ {l : list Ξ±} (h : l β‰  []), last l h ∈ l | [] h := absurd rfl h | [a] h := or.inl rfl | (a::b::l) h := or.inr $ by { rw [last_cons_cons], exact last_mem (cons_ne_nil b l) } lemma last_repeat_succ (a m : β„•) : (repeat a m.succ).last (ne_nil_of_length_eq_succ (show (repeat a m.succ).length = m.succ, by rw length_repeat)) = a := begin induction m with k IH, { simp }, { simpa only [repeat_succ, last] } end /-! ### last' -/ @[simp] theorem last'_is_none : βˆ€ {l : list Ξ±}, (last' l).is_none ↔ l = [] | [] := by simp | [a] := by simp | (a::b::l) := by simp [@last'_is_none (b::l)] @[simp] theorem last'_is_some : βˆ€ {l : list Ξ±}, l.last'.is_some ↔ l β‰  [] | [] := by simp | [a] := by simp | (a::b::l) := by simp [@last'_is_some (b::l)] theorem mem_last'_eq_last : βˆ€ {l : list Ξ±} {x : Ξ±}, x ∈ l.last' β†’ βˆƒ h, x = last l h | [] x hx := false.elim $ by simpa using hx | [a] x hx := have a = x, by simpa using hx, this β–Έ ⟨cons_ne_nil a [], rfl⟩ | (a::b::l) x hx := begin rw last' at hx, rcases mem_last'_eq_last hx with ⟨h₁, hβ‚‚βŸ©, use cons_ne_nil _ _, rwa [last_cons] end theorem last'_eq_last_of_ne_nil : βˆ€ {l : list Ξ±} (h : l β‰  []), l.last' = some (l.last h) | [] h := (h rfl).elim | [a] _ := by {unfold last, unfold last'} | (a::b::l) _ := @last'_eq_last_of_ne_nil (b::l) (cons_ne_nil _ _) theorem mem_last'_cons {x y : Ξ±} : βˆ€ {l : list Ξ±} (h : x ∈ l.last'), x ∈ (y :: l).last' | [] _ := by contradiction | (a::l) h := h theorem mem_of_mem_last' {l : list Ξ±} {a : Ξ±} (ha : a ∈ l.last') : a ∈ l := let ⟨h₁, hβ‚‚βŸ© := mem_last'_eq_last ha in hβ‚‚.symm β–Έ last_mem _ theorem init_append_last' : βˆ€ {l : list Ξ±} (a ∈ l.last'), init l ++ [a] = l | [] a ha := (option.not_mem_none a ha).elim | [a] _ rfl := rfl | (a :: b :: l) c hc := by { rw [last'] at hc, rw [init, cons_append, init_append_last' _ hc] } theorem ilast_eq_last' [inhabited Ξ±] : βˆ€ l : list Ξ±, l.ilast = l.last'.iget | [] := by simp [ilast, arbitrary] | [a] := rfl | [a, b] := rfl | [a, b, c] := rfl | (a :: b :: c :: l) := by simp [ilast, ilast_eq_last' (c :: l)] @[simp] theorem last'_append_cons : βˆ€ (l₁ : list Ξ±) (a : Ξ±) (lβ‚‚ : list Ξ±), last' (l₁ ++ a :: lβ‚‚) = last' (a :: lβ‚‚) | [] a lβ‚‚ := rfl | [b] a lβ‚‚ := rfl | (b::c::l₁) a lβ‚‚ := by rw [cons_append, cons_append, last', ← cons_append, last'_append_cons] @[simp] theorem last'_cons_cons (x y : Ξ±) (l : list Ξ±) : last' (x :: y :: l) = last' (y :: l) := rfl theorem last'_append_of_ne_nil (l₁ : list Ξ±) : βˆ€ {lβ‚‚ : list Ξ±} (hlβ‚‚ : lβ‚‚ β‰  []), last' (l₁ ++ lβ‚‚) = last' lβ‚‚ | [] hlβ‚‚ := by contradiction | (b::lβ‚‚) _ := last'_append_cons l₁ b lβ‚‚ theorem last'_append {l₁ lβ‚‚ : list Ξ±} {x : Ξ±} (h : x ∈ lβ‚‚.last') : x ∈ (l₁ ++ lβ‚‚).last' := by { cases lβ‚‚, { contradiction, }, { rw list.last'_append_cons, exact h } } /-! ### head(') and tail -/ theorem head_eq_head' [inhabited Ξ±] (l : list Ξ±) : head l = (head' l).iget := by cases l; refl theorem surjective_head [inhabited Ξ±] : surjective (@head Ξ± _) := Ξ» x, ⟨[x], rfl⟩ theorem surjective_head' : surjective (@head' Ξ±) := option.forall.2 ⟨⟨[], rfl⟩, Ξ» x, ⟨[x], rfl⟩⟩ theorem surjective_tail : surjective (@tail Ξ±) | [] := ⟨[], rfl⟩ | (a :: l) := ⟨a :: a :: l, rfl⟩ lemma eq_cons_of_mem_head' {x : Ξ±} : βˆ€ {l : list Ξ±}, x ∈ l.head' β†’ l = x::tail l | [] h := (option.not_mem_none _ h).elim | (a::l) h := by { simp only [head', option.mem_def] at h, exact h β–Έ rfl } theorem mem_of_mem_head' {x : Ξ±} {l : list Ξ±} (h : x ∈ l.head') : x ∈ l := (eq_cons_of_mem_head' h).symm β–Έ mem_cons_self _ _ @[simp] theorem head_cons [inhabited Ξ±] (a : Ξ±) (l : list Ξ±) : head (a::l) = a := rfl @[simp] theorem tail_nil : tail (@nil Ξ±) = [] := rfl @[simp] theorem tail_cons (a : Ξ±) (l : list Ξ±) : tail (a::l) = l := rfl @[simp] theorem head_append [inhabited Ξ±] (t : list Ξ±) {s : list Ξ±} (h : s β‰  []) : head (s ++ t) = head s := by {induction s, contradiction, refl} theorem head'_append {s t : list Ξ±} {x : Ξ±} (h : x ∈ s.head') : x ∈ (s ++ t).head' := by { cases s, contradiction, exact h } theorem head'_append_of_ne_nil : βˆ€ (l₁ : list Ξ±) {lβ‚‚ : list Ξ±} (hl₁ : l₁ β‰  []), head' (l₁ ++ lβ‚‚) = head' l₁ | [] _ hl₁ := by contradiction | (x::l₁) _ _ := rfl theorem tail_append_singleton_of_ne_nil {a : Ξ±} {l : list Ξ±} (h : l β‰  nil) : tail (l ++ [a]) = tail l ++ [a] := by { induction l, contradiction, rw [tail,cons_append,tail], } theorem cons_head'_tail : βˆ€ {l : list Ξ±} {a : Ξ±} (h : a ∈ head' l), a :: tail l = l | [] a h := by contradiction | (b::l) a h := by { simp at h, simp [h] } theorem head_mem_head' [inhabited Ξ±] : βˆ€ {l : list Ξ±} (h : l β‰  []), head l ∈ head' l | [] h := by contradiction | (a::l) h := rfl theorem cons_head_tail [inhabited Ξ±] {l : list Ξ±} (h : l β‰  []) : (head l)::(tail l) = l := cons_head'_tail (head_mem_head' h) lemma head_mem_self [inhabited Ξ±] {l : list Ξ±} (h : l β‰  nil) : l.head ∈ l := begin have h' := mem_cons_self l.head l.tail, rwa cons_head_tail h at h', end @[simp] theorem head'_map (f : Ξ± β†’ Ξ²) (l) : head' (map f l) = (head' l).map f := by cases l; refl lemma tail_append_of_ne_nil (l l' : list Ξ±) (h : l β‰  []) : (l ++ l').tail = l.tail ++ l' := begin cases l, { contradiction }, { simp } end @[simp] lemma nth_le_tail (l : list Ξ±) (i) (h : i < l.tail.length) (h' : i + 1 < l.length := by simpa [←lt_tsub_iff_right] using h) : l.tail.nth_le i h = l.nth_le (i + 1) h' := begin cases l, { cases h, }, { simpa } end lemma nth_le_cons_aux {l : list Ξ±} {a : Ξ±} {n} (hn : n β‰  0) (h : n < (a :: l).length) : n - 1 < l.length := begin contrapose! h, rw length_cons, convert succ_le_succ h, exact (nat.succ_pred_eq_of_pos hn.bot_lt).symm end lemma nth_le_cons {l : list Ξ±} {a : Ξ±} {n} (hl) : (a :: l).nth_le n hl = if hn : n = 0 then a else l.nth_le (n - 1) (nth_le_cons_aux hn hl) := begin split_ifs, { simp [nth_le, h] }, cases l, { rw [length_singleton, lt_succ_iff, nonpos_iff_eq_zero] at hl, contradiction }, cases n, { contradiction }, refl end @[simp] lemma modify_head_modify_head (l : list Ξ±) (f g : Ξ± β†’ Ξ±) : (l.modify_head f).modify_head g = l.modify_head (g ∘ f) := by cases l; simp /-! ### Induction from the right -/ /-- Induction principle from the right for lists: if a property holds for the empty list, and for `l ++ [a]` if it holds for `l`, then it holds for all lists. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ @[elab_as_eliminator] def reverse_rec_on {C : list Ξ± β†’ Sort*} (l : list Ξ±) (H0 : C []) (H1 : βˆ€ (l : list Ξ±) (a : Ξ±), C l β†’ C (l ++ [a])) : C l := begin rw ← reverse_reverse l, induction reverse l, { exact H0 }, { rw reverse_cons, exact H1 _ _ ih } end /-- Bidirectional induction principle for lists: if a property holds for the empty list, the singleton list, and `a :: (l ++ [b])` from `l`, then it holds for all lists. This can be used to prove statements about palindromes. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ def bidirectional_rec {C : list Ξ± β†’ Sort*} (H0 : C []) (H1 : βˆ€ (a : Ξ±), C [a]) (Hn : βˆ€ (a : Ξ±) (l : list Ξ±) (b : Ξ±), C l β†’ C (a :: (l ++ [b]))) : βˆ€ l, C l | [] := H0 | [a] := H1 a | (a :: b :: l) := let l' := init (b :: l), b' := last (b :: l) (cons_ne_nil _ _) in have length l' < length (a :: b :: l), by { change _ < length l + 2, simp }, begin rw ←init_append_last (cons_ne_nil b l), have : C l', from bidirectional_rec l', exact Hn a l' b' β€ΉC l'β€Ί end using_well_founded { rel_tac := Ξ» _ _, `[exact ⟨_, measure_wf list.length⟩] } /-- Like `bidirectional_rec`, but with the list parameter placed first. -/ @[elab_as_eliminator] def bidirectional_rec_on {C : list Ξ± β†’ Sort*} (l : list Ξ±) (H0 : C []) (H1 : βˆ€ (a : Ξ±), C [a]) (Hn : βˆ€ (a : Ξ±) (l : list Ξ±) (b : Ξ±), C l β†’ C (a :: (l ++ [b]))) : C l := bidirectional_rec H0 H1 Hn l /-! ### sublists -/ @[simp] theorem nil_sublist : Ξ  (l : list Ξ±), [] <+ l | [] := sublist.slnil | (a :: l) := sublist.cons _ _ a (nil_sublist l) @[refl, simp] theorem sublist.refl : Ξ  (l : list Ξ±), l <+ l | [] := sublist.slnil | (a :: l) := sublist.cons2 _ _ a (sublist.refl l) @[trans] theorem sublist.trans {l₁ lβ‚‚ l₃ : list Ξ±} (h₁ : l₁ <+ lβ‚‚) (hβ‚‚ : lβ‚‚ <+ l₃) : l₁ <+ l₃ := sublist.rec_on hβ‚‚ (Ξ»_ s, s) (Ξ»lβ‚‚ l₃ a hβ‚‚ IH l₁ h₁, sublist.cons _ _ _ (IH l₁ h₁)) (Ξ»lβ‚‚ l₃ a hβ‚‚ IH l₁ h₁, @sublist.cases_on _ (Ξ»l₁ lβ‚‚', lβ‚‚' = a :: lβ‚‚ β†’ l₁ <+ a :: l₃) _ _ h₁ (Ξ»_, nil_sublist _) (Ξ»l₁ lβ‚‚' a' h₁' e, match a', lβ‚‚', e, h₁' with ._, ._, rfl, h₁ := sublist.cons _ _ _ (IH _ h₁) end) (Ξ»l₁ lβ‚‚' a' h₁' e, match a', lβ‚‚', e, h₁' with ._, ._, rfl, h₁ := sublist.cons2 _ _ _ (IH _ h₁) end) rfl) l₁ h₁ @[simp] theorem sublist_cons (a : Ξ±) (l : list Ξ±) : l <+ a::l := sublist.cons _ _ _ (sublist.refl l) theorem sublist_of_cons_sublist {a : Ξ±} {l₁ lβ‚‚ : list Ξ±} : a::l₁ <+ lβ‚‚ β†’ l₁ <+ lβ‚‚ := sublist.trans (sublist_cons a l₁) theorem sublist.cons_cons {l₁ lβ‚‚ : list Ξ±} (a : Ξ±) (s : l₁ <+ lβ‚‚) : a::l₁ <+ a::lβ‚‚ := sublist.cons2 _ _ _ s @[simp] theorem sublist_append_left : Ξ  (l₁ lβ‚‚ : list Ξ±), l₁ <+ l₁++lβ‚‚ | [] lβ‚‚ := nil_sublist _ | (a::l₁) lβ‚‚ := (sublist_append_left l₁ lβ‚‚).cons_cons _ @[simp] theorem sublist_append_right : Ξ  (l₁ lβ‚‚ : list Ξ±), lβ‚‚ <+ l₁++lβ‚‚ | [] lβ‚‚ := sublist.refl _ | (a::l₁) lβ‚‚ := sublist.cons _ _ _ (sublist_append_right l₁ lβ‚‚) theorem sublist_cons_of_sublist (a : Ξ±) {l₁ lβ‚‚ : list Ξ±} : l₁ <+ lβ‚‚ β†’ l₁ <+ a::lβ‚‚ := sublist.cons _ _ _ theorem sublist_append_of_sublist_left {l l₁ lβ‚‚ : list Ξ±} (s : l <+ l₁) : l <+ l₁++lβ‚‚ := s.trans $ sublist_append_left _ _ theorem sublist_append_of_sublist_right {l l₁ lβ‚‚ : list Ξ±} (s : l <+ lβ‚‚) : l <+ l₁++lβ‚‚ := s.trans $ sublist_append_right _ _ theorem sublist_of_cons_sublist_cons {l₁ lβ‚‚ : list Ξ±} : βˆ€ {a : Ξ±}, a::l₁ <+ a::lβ‚‚ β†’ l₁ <+ lβ‚‚ | ._ (sublist.cons ._ ._ a s) := sublist_of_cons_sublist s | ._ (sublist.cons2 ._ ._ a s) := s theorem cons_sublist_cons_iff {l₁ lβ‚‚ : list Ξ±} {a : Ξ±} : a::l₁ <+ a::lβ‚‚ ↔ l₁ <+ lβ‚‚ := ⟨sublist_of_cons_sublist_cons, sublist.cons_cons _⟩ @[simp] theorem append_sublist_append_left {l₁ lβ‚‚ : list Ξ±} : βˆ€ l, l++l₁ <+ l++lβ‚‚ ↔ l₁ <+ lβ‚‚ | [] := iff.rfl | (a::l) := cons_sublist_cons_iff.trans (append_sublist_append_left l) theorem sublist.append_right {l₁ lβ‚‚ : list Ξ±} (h : l₁ <+ lβ‚‚) (l) : l₁++l <+ lβ‚‚++l := begin induction h with _ _ a _ ih _ _ a _ ih, { refl }, { apply sublist_cons_of_sublist a ih }, { apply ih.cons_cons a } end theorem sublist_or_mem_of_sublist {l l₁ lβ‚‚ : list Ξ±} {a : Ξ±} (h : l <+ l₁ ++ a::lβ‚‚) : l <+ l₁ ++ lβ‚‚ ∨ a ∈ l := begin induction l₁ with b l₁ IH generalizing l, { cases h, { left, exact β€Ήl <+ lβ‚‚β€Ί }, { right, apply mem_cons_self } }, { cases h with _ _ _ h _ _ _ h, { exact or.imp_left (sublist_cons_of_sublist _) (IH h) }, { exact (IH h).imp (sublist.cons_cons _) (mem_cons_of_mem _) } } end theorem sublist.reverse {l₁ lβ‚‚ : list Ξ±} (h : l₁ <+ lβ‚‚) : l₁.reverse <+ lβ‚‚.reverse := begin induction h with _ _ _ _ ih _ _ a _ ih, {refl}, { rw reverse_cons, exact sublist_append_of_sublist_left ih }, { rw [reverse_cons, reverse_cons], exact ih.append_right [a] } end @[simp] theorem reverse_sublist_iff {l₁ lβ‚‚ : list Ξ±} : l₁.reverse <+ lβ‚‚.reverse ↔ l₁ <+ lβ‚‚ := ⟨λ h, l₁.reverse_reverse β–Έ lβ‚‚.reverse_reverse β–Έ h.reverse, sublist.reverse⟩ @[simp] theorem append_sublist_append_right {l₁ lβ‚‚ : list Ξ±} (l) : l₁++l <+ lβ‚‚++l ↔ l₁ <+ lβ‚‚ := ⟨λ h, by simpa only [reverse_append, append_sublist_append_left, reverse_sublist_iff] using h.reverse, Ξ» h, h.append_right l⟩ theorem sublist.append {l₁ lβ‚‚ r₁ rβ‚‚ : list Ξ±} (hl : l₁ <+ lβ‚‚) (hr : r₁ <+ rβ‚‚) : l₁ ++ r₁ <+ lβ‚‚ ++ rβ‚‚ := (hl.append_right _).trans ((append_sublist_append_left _).2 hr) theorem sublist.subset : Ξ  {l₁ lβ‚‚ : list Ξ±}, l₁ <+ lβ‚‚ β†’ l₁ βŠ† lβ‚‚ | ._ ._ sublist.slnil b h := h | ._ ._ (sublist.cons l₁ lβ‚‚ a s) b h := mem_cons_of_mem _ (sublist.subset s h) | ._ ._ (sublist.cons2 l₁ lβ‚‚ a s) b h := match eq_or_mem_of_mem_cons h with | or.inl h := h β–Έ mem_cons_self _ _ | or.inr h := mem_cons_of_mem _ (sublist.subset s h) end @[simp] theorem singleton_sublist {a : Ξ±} {l} : [a] <+ l ↔ a ∈ l := ⟨λ h, h.subset (mem_singleton_self _), Ξ» h, let ⟨s, t, e⟩ := mem_split h in e.symm β–Έ ((nil_sublist _).cons_cons _ ).trans (sublist_append_right _ _)⟩ theorem eq_nil_of_sublist_nil {l : list Ξ±} (s : l <+ []) : l = [] := eq_nil_of_subset_nil $ s.subset @[simp] theorem sublist_nil_iff_eq_nil {l : list Ξ±} : l <+ [] ↔ l = [] := ⟨eq_nil_of_sublist_nil, Ξ» H, H β–Έ sublist.refl _⟩ @[simp] theorem repeat_sublist_repeat (a : Ξ±) {m n} : repeat a m <+ repeat a n ↔ m ≀ n := ⟨λ h, by simpa only [length_repeat] using h.length_le, Ξ» h, by induction h; [refl, simp only [*, repeat_succ, sublist.cons]] ⟩ lemma sublist_repeat_iff {l : list Ξ±} {a : Ξ±} {n : β„•} : l <+ repeat a n ↔ βˆƒ (k ≀ n), l = repeat a k := ⟨λ h, ⟨l.length, h.length_le.trans (length_repeat _ _).le, eq_repeat.mpr ⟨rfl, Ξ» b hb, list.eq_of_mem_repeat (h.subset hb)⟩⟩, by { rintro ⟨k, h, rfl⟩, exact (repeat_sublist_repeat _).mpr h }⟩ theorem sublist.eq_of_length : βˆ€ {l₁ lβ‚‚ : list Ξ±}, l₁ <+ lβ‚‚ β†’ length l₁ = length lβ‚‚ β†’ l₁ = lβ‚‚ | ._ ._ sublist.slnil h := rfl | ._ ._ (sublist.cons l₁ lβ‚‚ a s) h := by cases s.length_le.not_lt (by rw h; apply lt_succ_self) | ._ ._ (sublist.cons2 l₁ lβ‚‚ a s) h := by rw [length, length] at h; injection h with h; rw s.eq_of_length h theorem sublist.eq_of_length_le (s : l₁ <+ lβ‚‚) (h : length lβ‚‚ ≀ length l₁) : l₁ = lβ‚‚ := s.eq_of_length $ s.length_le.antisymm h lemma sublist.antisymm (s₁ : l₁ <+ lβ‚‚) (sβ‚‚ : lβ‚‚ <+ l₁) : l₁ = lβ‚‚ := s₁.eq_of_length_le sβ‚‚.length_le instance decidable_sublist [decidable_eq Ξ±] : βˆ€ (l₁ lβ‚‚ : list Ξ±), decidable (l₁ <+ lβ‚‚) | [] lβ‚‚ := is_true $ nil_sublist _ | (a::l₁) [] := is_false $ Ξ»h, list.no_confusion $ eq_nil_of_sublist_nil h | (a::l₁) (b::lβ‚‚) := if h : a = b then decidable_of_decidable_of_iff (decidable_sublist l₁ lβ‚‚) $ by rw [← h]; exact ⟨sublist.cons_cons _, sublist_of_cons_sublist_cons⟩ else decidable_of_decidable_of_iff (decidable_sublist (a::l₁) lβ‚‚) ⟨sublist_cons_of_sublist _, Ξ»s, match a, l₁, s, h with | a, l₁, sublist.cons ._ ._ ._ s', h := s' | ._, ._, sublist.cons2 t ._ ._ s', h := absurd rfl h end⟩ /-! ### index_of -/ section index_of variable [decidable_eq Ξ±] @[simp] theorem index_of_nil (a : Ξ±) : index_of a [] = 0 := rfl theorem index_of_cons (a b : Ξ±) (l : list Ξ±) : index_of a (b::l) = if a = b then 0 else succ (index_of a l) := rfl theorem index_of_cons_eq {a b : Ξ±} (l : list Ξ±) : a = b β†’ index_of a (b::l) = 0 := assume e, if_pos e @[simp] theorem index_of_cons_self (a : Ξ±) (l : list Ξ±) : index_of a (a::l) = 0 := index_of_cons_eq _ rfl @[simp, priority 990] theorem index_of_cons_ne {a b : Ξ±} (l : list Ξ±) : a β‰  b β†’ index_of a (b::l) = succ (index_of a l) := assume n, if_neg n theorem index_of_eq_length {a : Ξ±} {l : list Ξ±} : index_of a l = length l ↔ a βˆ‰ l := begin induction l with b l ih, { exact iff_of_true rfl (not_mem_nil _) }, simp only [length, mem_cons_iff, index_of_cons], split_ifs, { exact iff_of_false (by rintro ⟨⟩) (Ξ» H, H $ or.inl h) }, { simp only [h, false_or], rw ← ih, exact succ_inj' } end @[simp, priority 980] theorem index_of_of_not_mem {l : list Ξ±} {a : Ξ±} : a βˆ‰ l β†’ index_of a l = length l := index_of_eq_length.2 theorem index_of_le_length {a : Ξ±} {l : list Ξ±} : index_of a l ≀ length l := begin induction l with b l ih, {refl}, simp only [length, index_of_cons], by_cases h : a = b, {rw if_pos h, exact nat.zero_le _}, rw if_neg h, exact succ_le_succ ih end theorem index_of_lt_length {a} {l : list Ξ±} : index_of a l < length l ↔ a ∈ l := ⟨λh, decidable.by_contradiction $ Ξ» al, ne_of_lt h $ index_of_eq_length.2 al, Ξ»al, lt_of_le_of_ne index_of_le_length $ Ξ» h, index_of_eq_length.1 h al⟩ theorem index_of_append_of_mem {a : Ξ±} (h : a ∈ l₁) : index_of a (l₁ ++ lβ‚‚) = index_of a l₁ := begin induction l₁ with d₁ t₁ ih, { exfalso, exact not_mem_nil a h }, rw list.cons_append, by_cases hh : a = d₁, { iterate 2 { rw index_of_cons_eq _ hh } }, rw [index_of_cons_ne _ hh, index_of_cons_ne _ hh, ih (mem_of_ne_of_mem hh h)], end theorem index_of_append_of_not_mem {a : Ξ±} (h : a βˆ‰ l₁) : index_of a (l₁ ++ lβ‚‚) = l₁.length + index_of a lβ‚‚ := begin induction l₁ with d₁ t₁ ih, { rw [list.nil_append, list.length, zero_add] }, rw [list.cons_append, index_of_cons_ne _ (ne_of_not_mem_cons h), list.length, ih (not_mem_of_not_mem_cons h), nat.succ_add], end end index_of /-! ### nth element -/ theorem nth_le_of_mem : βˆ€ {a} {l : list Ξ±}, a ∈ l β†’ βˆƒ n h, nth_le l n h = a | a (_ :: l) (or.inl rfl) := ⟨0, succ_pos _, rfl⟩ | a (b :: l) (or.inr m) := let ⟨n, h, e⟩ := nth_le_of_mem m in ⟨n+1, succ_lt_succ h, e⟩ theorem nth_le_nth : βˆ€ {l : list Ξ±} {n} h, nth l n = some (nth_le l n h) | (a :: l) 0 h := rfl | (a :: l) (n+1) h := @nth_le_nth l n _ theorem nth_len_le : βˆ€ {l : list Ξ±} {n}, length l ≀ n β†’ nth l n = none | [] n h := rfl | (a :: l) (n+1) h := nth_len_le (le_of_succ_le_succ h) @[simp] theorem nth_length (l : list Ξ±) : l.nth l.length = none := nth_len_le le_rfl theorem nth_eq_some {l : list Ξ±} {n a} : nth l n = some a ↔ βˆƒ h, nth_le l n h = a := ⟨λ e, have h : n < length l, from lt_of_not_ge $ Ξ» hn, by rw nth_len_le hn at e; contradiction, ⟨h, by rw nth_le_nth h at e; injection e with e; apply nth_le_mem⟩, Ξ» ⟨h, e⟩, e β–Έ nth_le_nth _⟩ @[simp] theorem nth_eq_none_iff : βˆ€ {l : list Ξ±} {n}, nth l n = none ↔ length l ≀ n := begin intros, split, { intro h, by_contradiction h', have hβ‚‚ : βˆƒ h, l.nth_le n h = l.nth_le n (lt_of_not_ge h') := ⟨lt_of_not_ge h', rfl⟩, rw [← nth_eq_some, h] at hβ‚‚, cases hβ‚‚ }, { solve_by_elim [nth_len_le] }, end theorem nth_of_mem {a} {l : list Ξ±} (h : a ∈ l) : βˆƒ n, nth l n = some a := let ⟨n, h, e⟩ := nth_le_of_mem h in ⟨n, by rw [nth_le_nth, e]⟩ theorem nth_le_mem : βˆ€ (l : list Ξ±) n h, nth_le l n h ∈ l | (a :: l) 0 h := mem_cons_self _ _ | (a :: l) (n+1) h := mem_cons_of_mem _ (nth_le_mem l _ _) theorem nth_mem {l : list Ξ±} {n a} (e : nth l n = some a) : a ∈ l := let ⟨h, e⟩ := nth_eq_some.1 e in e β–Έ nth_le_mem _ _ _ theorem mem_iff_nth_le {a} {l : list Ξ±} : a ∈ l ↔ βˆƒ n h, nth_le l n h = a := ⟨nth_le_of_mem, Ξ» ⟨n, h, e⟩, e β–Έ nth_le_mem _ _ _⟩ theorem mem_iff_nth {a} {l : list Ξ±} : a ∈ l ↔ βˆƒ n, nth l n = some a := mem_iff_nth_le.trans $ exists_congr $ Ξ» n, nth_eq_some.symm lemma nth_zero (l : list Ξ±) : l.nth 0 = l.head' := by cases l; refl lemma nth_injective {Ξ± : Type u} {xs : list Ξ±} {i j : β„•} (hβ‚€ : i < xs.length) (h₁ : nodup xs) (hβ‚‚ : xs.nth i = xs.nth j) : i = j := begin induction xs with x xs generalizing i j, { cases hβ‚€ }, { cases i; cases j, case nat.zero nat.zero { refl }, case nat.succ nat.succ { congr, cases h₁, apply xs_ih; solve_by_elim [lt_of_succ_lt_succ] }, iterate 2 { dsimp at hβ‚‚, cases h₁ with _ _ h h', cases h x _ rfl, rw mem_iff_nth, exact ⟨_, hβ‚‚.symm⟩ <|> exact ⟨_, hβ‚‚βŸ© } }, end @[simp] theorem nth_map (f : Ξ± β†’ Ξ²) : βˆ€ l n, nth (map f l) n = (nth l n).map f | [] n := rfl | (a :: l) 0 := rfl | (a :: l) (n+1) := nth_map l n theorem nth_le_map (f : Ξ± β†’ Ξ²) {l n} (H1 H2) : nth_le (map f l) n H1 = f (nth_le l n H2) := option.some.inj $ by rw [← nth_le_nth, nth_map, nth_le_nth]; refl /-- A version of `nth_le_map` that can be used for rewriting. -/ theorem nth_le_map_rev (f : Ξ± β†’ Ξ²) {l n} (H) : f (nth_le l n H) = nth_le (map f l) n ((length_map f l).symm β–Έ H) := (nth_le_map f _ _).symm @[simp] theorem nth_le_map' (f : Ξ± β†’ Ξ²) {l n} (H) : nth_le (map f l) n H = f (nth_le l n (length_map f l β–Έ H)) := nth_le_map f _ _ /-- If one has `nth_le L i hi` in a formula and `h : L = L'`, one can not `rw h` in the formula as `hi` gives `i < L.length` and not `i < L'.length`. The lemma `nth_le_of_eq` can be used to make such a rewrite, with `rw (nth_le_of_eq h)`. -/ lemma nth_le_of_eq {L L' : list Ξ±} (h : L = L') {i : β„•} (hi : i < L.length) : nth_le L i hi = nth_le L' i (h β–Έ hi) := by { congr, exact h} @[simp] lemma nth_le_singleton (a : Ξ±) {n : β„•} (hn : n < 1) : nth_le [a] n hn = a := have hn0 : n = 0 := nat.eq_zero_of_le_zero (le_of_lt_succ hn), by subst hn0; refl lemma nth_le_zero [inhabited Ξ±] {L : list Ξ±} (h : 0 < L.length) : L.nth_le 0 h = L.head := by { cases L, cases h, simp, } lemma nth_le_append : βˆ€ {l₁ lβ‚‚ : list Ξ±} {n : β„•} (hn₁) (hnβ‚‚), (l₁ ++ lβ‚‚).nth_le n hn₁ = l₁.nth_le n hnβ‚‚ | [] _ n hn₁ hnβ‚‚ := (nat.not_lt_zero _ hnβ‚‚).elim | (a::l) _ 0 hn₁ hnβ‚‚ := rfl | (a::l) _ (n+1) hn₁ hnβ‚‚ := by simp only [nth_le, cons_append]; exact nth_le_append _ _ lemma nth_le_append_right_aux {l₁ lβ‚‚ : list Ξ±} {n : β„•} (h₁ : l₁.length ≀ n) (hβ‚‚ : n < (l₁ ++ lβ‚‚).length) : n - l₁.length < lβ‚‚.length := begin rw list.length_append at hβ‚‚, apply lt_of_add_lt_add_right, rwa [nat.sub_add_cancel h₁, nat.add_comm], end lemma nth_le_append_right : βˆ€ {l₁ lβ‚‚ : list Ξ±} {n : β„•} (h₁ : l₁.length ≀ n) (hβ‚‚), (l₁ ++ lβ‚‚).nth_le n hβ‚‚ = lβ‚‚.nth_le (n - l₁.length) (nth_le_append_right_aux h₁ hβ‚‚) | [] _ n h₁ hβ‚‚ := rfl | (a :: l) _ (n+1) h₁ hβ‚‚ := begin dsimp, conv { to_rhs, congr, skip, rw [nat.add_sub_add_right], }, rw nth_le_append_right (nat.lt_succ_iff.mp h₁), end @[simp] lemma nth_le_repeat (a : Ξ±) {n m : β„•} (h : m < (list.repeat a n).length) : (list.repeat a n).nth_le m h = a := eq_of_mem_repeat (nth_le_mem _ _ _) lemma nth_append {l₁ lβ‚‚ : list Ξ±} {n : β„•} (hn : n < l₁.length) : (l₁ ++ lβ‚‚).nth n = l₁.nth n := have hn' : n < (l₁ ++ lβ‚‚).length := lt_of_lt_of_le hn (by rw length_append; exact nat.le_add_right _ _), by rw [nth_le_nth hn, nth_le_nth hn', nth_le_append] lemma nth_append_right {l₁ lβ‚‚ : list Ξ±} {n : β„•} (hn : l₁.length ≀ n) : (l₁ ++ lβ‚‚).nth n = lβ‚‚.nth (n - l₁.length) := begin by_cases hl : n < (l₁ ++ lβ‚‚).length, { rw [nth_le_nth hl, nth_le_nth, nth_le_append_right hn] }, { rw [nth_len_le (le_of_not_lt hl), nth_len_le], rw [not_lt, length_append] at hl, exact le_tsub_of_add_le_left hl } end lemma last_eq_nth_le : βˆ€ (l : list Ξ±) (h : l β‰  []), last l h = l.nth_le (l.length - 1) (nat.sub_lt (length_pos_of_ne_nil h) one_pos) | [] h := rfl | [a] h := by rw [last_singleton, nth_le_singleton] | (a :: b :: l) h := by { rw [last_cons, last_eq_nth_le (b :: l)], refl, exact cons_ne_nil b l } lemma nth_le_length_sub_one {l : list Ξ±} (h : l.length - 1 < l.length) : l.nth_le (l.length - 1) h = l.last (by { rintro rfl, exact nat.lt_irrefl 0 h }) := (last_eq_nth_le l _).symm @[simp] lemma nth_concat_length : βˆ€ (l : list Ξ±) (a : Ξ±), (l ++ [a]).nth l.length = some a | [] a := rfl | (b::l) a := by rw [cons_append, length_cons, nth, nth_concat_length] lemma nth_le_cons_length (x : Ξ±) (xs : list Ξ±) (n : β„•) (h : n = xs.length) : (x :: xs).nth_le n (by simp [h]) = (x :: xs).last (cons_ne_nil x xs) := begin rw last_eq_nth_le, congr, simp [h] end lemma take_one_drop_eq_of_lt_length {l : list Ξ±} {n : β„•} (h : n < l.length) : (l.drop n).take 1 = [l.nth_le n h] := begin induction l with x l ih generalizing n, { cases h }, { by_cases h₁ : l = [], { subst h₁, rw nth_le_singleton, simp [lt_succ_iff] at h, subst h, simp }, have hβ‚‚ := h, rw [length_cons, nat.lt_succ_iff, le_iff_eq_or_lt] at hβ‚‚, cases n, { simp }, rw [drop, nth_le], apply ih }, end @[ext] theorem ext : βˆ€ {l₁ lβ‚‚ : list Ξ±}, (βˆ€n, nth l₁ n = nth lβ‚‚ n) β†’ l₁ = lβ‚‚ | [] [] h := rfl | (a::l₁) [] h := by have h0 := h 0; contradiction | [] (a'::lβ‚‚) h := by have h0 := h 0; contradiction | (a::l₁) (a'::lβ‚‚) h := by have h0 : some a = some a' := h 0; injection h0 with aa; simp only [aa, ext (Ξ»n, h (n+1))]; split; refl theorem ext_le {l₁ lβ‚‚ : list Ξ±} (hl : length l₁ = length lβ‚‚) (h : βˆ€n h₁ hβ‚‚, nth_le l₁ n h₁ = nth_le lβ‚‚ n hβ‚‚) : l₁ = lβ‚‚ := ext $ Ξ»n, if h₁ : n < length l₁ then by rw [nth_le_nth, nth_le_nth, h n h₁ (by rwa [← hl])] else let h₁ := le_of_not_gt h₁ in by { rw [nth_len_le h₁, nth_len_le], rwa [←hl], } @[simp] theorem index_of_nth_le [decidable_eq Ξ±] {a : Ξ±} : βˆ€ {l : list Ξ±} h, nth_le l (index_of a l) h = a | (b::l) h := by by_cases h' : a = b; simp only [h', if_pos, if_false, index_of_cons, nth_le, @index_of_nth_le l] @[simp] theorem index_of_nth [decidable_eq Ξ±] {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : nth l (index_of a l) = some a := by rw [nth_le_nth, index_of_nth_le (index_of_lt_length.2 h)] theorem nth_le_reverse_aux1 : βˆ€ (l r : list Ξ±) (i h1 h2), nth_le (reverse_core l r) (i + length l) h1 = nth_le r i h2 | [] r i := Ξ»h1 h2, rfl | (a :: l) r i := by rw (show i + length (a :: l) = i + 1 + length l, from add_right_comm i (length l) 1); exact Ξ»h1 h2, nth_le_reverse_aux1 l (a :: r) (i+1) h1 (succ_lt_succ h2) lemma index_of_inj [decidable_eq Ξ±] {l : list Ξ±} {x y : Ξ±} (hx : x ∈ l) (hy : y ∈ l) : index_of x l = index_of y l ↔ x = y := ⟨λ h, have nth_le l (index_of x l) (index_of_lt_length.2 hx) = nth_le l (index_of y l) (index_of_lt_length.2 hy), by simp only [h], by simpa only [index_of_nth_le], Ξ» h, by subst h⟩ theorem nth_le_reverse_aux2 : βˆ€ (l r : list Ξ±) (i : nat) (h1) (h2), nth_le (reverse_core l r) (length l - 1 - i) h1 = nth_le l i h2 | [] r i h1 h2 := absurd h2 (nat.not_lt_zero _) | (a :: l) r 0 h1 h2 := begin have aux := nth_le_reverse_aux1 l (a :: r) 0, rw zero_add at aux, exact aux _ (zero_lt_succ _) end | (a :: l) r (i+1) h1 h2 := begin have aux := nth_le_reverse_aux2 l (a :: r) i, have heq := calc length (a :: l) - 1 - (i + 1) = length l - (1 + i) : by rw add_comm; refl ... = length l - 1 - i : by rw ← tsub_add_eq_tsub_tsub, rw [← heq] at aux, apply aux end @[simp] theorem nth_le_reverse (l : list Ξ±) (i : nat) (h1 h2) : nth_le (reverse l) (length l - 1 - i) h1 = nth_le l i h2 := nth_le_reverse_aux2 _ _ _ _ _ lemma nth_le_reverse' (l : list Ξ±) (n : β„•) (hn : n < l.reverse.length) (hn') : l.reverse.nth_le n hn = l.nth_le (l.length - 1 - n) hn' := begin rw eq_comm, convert nth_le_reverse l.reverse _ _ _ using 1, { simp }, { simpa } end lemma eq_cons_of_length_one {l : list Ξ±} (h : l.length = 1) : l = [l.nth_le 0 (h.symm β–Έ zero_lt_one)] := begin refine ext_le (by convert h) (Ξ» n h₁ hβ‚‚, _), simp only [nth_le_singleton], congr, exact eq_bot_iff.mpr (nat.lt_succ_iff.mp hβ‚‚) end lemma nth_le_eq_iff {l : list Ξ±} {n : β„•} {x : Ξ±} {h} : l.nth_le n h = x ↔ l.nth n = some x := by { rw nth_eq_some, tauto } lemma some_nth_le_eq {l : list Ξ±} {n : β„•} {h} : some (l.nth_le n h) = l.nth n := by { symmetry, rw nth_eq_some, tauto } lemma modify_nth_tail_modify_nth_tail {f g : list Ξ± β†’ list Ξ±} (m : β„•) : βˆ€n (l:list Ξ±), (l.modify_nth_tail f n).modify_nth_tail g (m + n) = l.modify_nth_tail (Ξ»l, (f l).modify_nth_tail g m) n | 0 l := rfl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_modify_nth_tail n l) lemma modify_nth_tail_modify_nth_tail_le {f g : list Ξ± β†’ list Ξ±} (m n : β„•) (l : list Ξ±) (h : n ≀ m) : (l.modify_nth_tail f n).modify_nth_tail g m = l.modify_nth_tail (Ξ»l, (f l).modify_nth_tail g (m - n)) n := begin rcases exists_add_of_le h with ⟨m, rfl⟩, rw [add_tsub_cancel_left, add_comm, modify_nth_tail_modify_nth_tail] end lemma modify_nth_tail_modify_nth_tail_same {f g : list Ξ± β†’ list Ξ±} (n : β„•) (l:list Ξ±) : (l.modify_nth_tail f n).modify_nth_tail g n = l.modify_nth_tail (g ∘ f) n := by rw [modify_nth_tail_modify_nth_tail_le n n l (le_refl n), tsub_self]; refl lemma modify_nth_tail_id : βˆ€n (l:list Ξ±), l.modify_nth_tail id n = l | 0 l := rfl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_id n l) theorem remove_nth_eq_nth_tail : βˆ€ n (l : list Ξ±), remove_nth l n = modify_nth_tail tail n l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (cons _) (remove_nth_eq_nth_tail _ _) theorem update_nth_eq_modify_nth (a : Ξ±) : βˆ€ n (l : list Ξ±), update_nth l n a = modify_nth (Ξ» _, a) n l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (b::l) := congr_arg (cons _) (update_nth_eq_modify_nth _ _) theorem modify_nth_eq_update_nth (f : Ξ± β†’ Ξ±) : βˆ€ n (l : list Ξ±), modify_nth f n l = ((Ξ» a, update_nth l n (f a)) <$> nth l n).get_or_else l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (b::l) := (congr_arg (cons b) (modify_nth_eq_update_nth n l)).trans $ by cases nth l n; refl theorem nth_modify_nth (f : Ξ± β†’ Ξ±) : βˆ€ n (l : list Ξ±) m, nth (modify_nth f n l) m = (Ξ» a, if n = m then f a else a) <$> nth l m | n l 0 := by cases l; cases n; refl | n [] (m+1) := by cases n; refl | 0 (a::l) (m+1) := by cases nth l m; refl | (n+1) (a::l) (m+1) := (nth_modify_nth n l m).trans $ by cases nth l m with b; by_cases n = m; simp only [h, if_pos, if_true, if_false, option.map_none, option.map_some, mt succ.inj, not_false_iff] theorem modify_nth_tail_length (f : list Ξ± β†’ list Ξ±) (H : βˆ€ l, length (f l) = length l) : βˆ€ n l, length (modify_nth_tail f n l) = length l | 0 l := H _ | (n+1) [] := rfl | (n+1) (a::l) := @congr_arg _ _ _ _ (+1) (modify_nth_tail_length _ _) @[simp] theorem modify_nth_length (f : Ξ± β†’ Ξ±) : βˆ€ n l, length (modify_nth f n l) = length l := modify_nth_tail_length _ (Ξ» l, by cases l; refl) @[simp] theorem update_nth_length (l : list Ξ±) (n) (a : Ξ±) : length (update_nth l n a) = length l := by simp only [update_nth_eq_modify_nth, modify_nth_length] @[simp] theorem nth_modify_nth_eq (f : Ξ± β†’ Ξ±) (n) (l : list Ξ±) : nth (modify_nth f n l) n = f <$> nth l n := by simp only [nth_modify_nth, if_pos] @[simp] theorem nth_modify_nth_ne (f : Ξ± β†’ Ξ±) {m n} (l : list Ξ±) (h : m β‰  n) : nth (modify_nth f m l) n = nth l n := by simp only [nth_modify_nth, if_neg h, id_map'] theorem nth_update_nth_eq (a : Ξ±) (n) (l : list Ξ±) : nth (update_nth l n a) n = (Ξ» _, a) <$> nth l n := by simp only [update_nth_eq_modify_nth, nth_modify_nth_eq] theorem nth_update_nth_of_lt (a : Ξ±) {n} {l : list Ξ±} (h : n < length l) : nth (update_nth l n a) n = some a := by rw [nth_update_nth_eq, nth_le_nth h]; refl theorem nth_update_nth_ne (a : Ξ±) {m n} (l : list Ξ±) (h : m β‰  n) : nth (update_nth l m a) n = nth l n := by simp only [update_nth_eq_modify_nth, nth_modify_nth_ne _ _ h] @[simp] lemma update_nth_nil (n : β„•) (a : Ξ±) : [].update_nth n a = [] := rfl @[simp] lemma update_nth_succ (x : Ξ±) (xs : list Ξ±) (n : β„•) (a : Ξ±) : (x :: xs).update_nth n.succ a = x :: xs.update_nth n a := rfl lemma update_nth_comm (a b : Ξ±) : Ξ  {n m : β„•} (l : list Ξ±) (h : n β‰  m), (l.update_nth n a).update_nth m b = (l.update_nth m b).update_nth n a | _ _ [] _ := by simp | 0 0 (x :: t) h := absurd rfl h | (n + 1) 0 (x :: t) h := by simp [list.update_nth] | 0 (m + 1) (x :: t) h := by simp [list.update_nth] | (n + 1) (m + 1) (x :: t) h := by { simp only [update_nth, true_and, eq_self_iff_true], exact update_nth_comm t (Ξ» h', h $ nat.succ_inj'.mpr h'), } @[simp] lemma nth_le_update_nth_eq (l : list Ξ±) (i : β„•) (a : Ξ±) (h : i < (l.update_nth i a).length) : (l.update_nth i a).nth_le i h = a := by rw [← option.some_inj, ← nth_le_nth, nth_update_nth_eq, nth_le_nth]; simp * at * @[simp] lemma nth_le_update_nth_of_ne {l : list Ξ±} {i j : β„•} (h : i β‰  j) (a : Ξ±) (hj : j < (l.update_nth i a).length) : (l.update_nth i a).nth_le j hj = l.nth_le j (by simpa using hj) := by rw [← option.some_inj, ← list.nth_le_nth, list.nth_update_nth_ne _ _ h, list.nth_le_nth] lemma mem_or_eq_of_mem_update_nth : βˆ€ {l : list Ξ±} {n : β„•} {a b : Ξ±} (h : a ∈ l.update_nth n b), a ∈ l ∨ a = b | [] n a b h := false.elim h | (c::l) 0 a b h := ((mem_cons_iff _ _ _).1 h).elim or.inr (or.inl ∘ mem_cons_of_mem _) | (c::l) (n+1) a b h := ((mem_cons_iff _ _ _).1 h).elim (Ξ» h, h β–Έ or.inl (mem_cons_self _ _)) (Ξ» h, (mem_or_eq_of_mem_update_nth h).elim (or.inl ∘ mem_cons_of_mem _) or.inr) section insert_nth variable {a : Ξ±} @[simp] lemma insert_nth_zero (s : list Ξ±) (x : Ξ±) : insert_nth 0 x s = x :: s := rfl @[simp] lemma insert_nth_succ_nil (n : β„•) (a : Ξ±) : insert_nth (n + 1) a [] = [] := rfl @[simp] lemma insert_nth_succ_cons (s : list Ξ±) (hd x : Ξ±) (n : β„•) : insert_nth (n + 1) x (hd :: s) = hd :: (insert_nth n x s) := rfl lemma length_insert_nth : βˆ€n as, n ≀ length as β†’ length (insert_nth n a as) = length as + 1 | 0 as h := rfl | (n+1) [] h := (nat.not_succ_le_zero _ h).elim | (n+1) (a'::as) h := congr_arg nat.succ $ length_insert_nth n as (nat.le_of_succ_le_succ h) lemma remove_nth_insert_nth (n:β„•) (l : list Ξ±) : (l.insert_nth n a).remove_nth n = l := by rw [remove_nth_eq_nth_tail, insert_nth, modify_nth_tail_modify_nth_tail_same]; from modify_nth_tail_id _ _ lemma insert_nth_remove_nth_of_ge : βˆ€n m as, n < length as β†’ n ≀ m β†’ insert_nth m a (as.remove_nth n) = (as.insert_nth (m + 1) a).remove_nth n | 0 0 [] has _ := (lt_irrefl _ has).elim | 0 0 (a::as) has hmn := by simp [remove_nth, insert_nth] | 0 (m+1) (a::as) has hmn := rfl | (n+1) (m+1) (a::as) has hmn := congr_arg (cons a) $ insert_nth_remove_nth_of_ge n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn) lemma insert_nth_remove_nth_of_le : βˆ€n m as, n < length as β†’ m ≀ n β†’ insert_nth m a (as.remove_nth n) = (as.insert_nth m a).remove_nth (n + 1) | n 0 (a :: as) has hmn := rfl | (n + 1) (m + 1) (a :: as) has hmn := congr_arg (cons a) $ insert_nth_remove_nth_of_le n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn) lemma insert_nth_comm (a b : Ξ±) : βˆ€(i j : β„•) (l : list Ξ±) (h : i ≀ j) (hj : j ≀ length l), (l.insert_nth i a).insert_nth (j + 1) b = (l.insert_nth j b).insert_nth i a | 0 j l := by simp [insert_nth] | (i + 1) 0 l := assume h, (nat.not_lt_zero _ h).elim | (i + 1) (j+1) [] := by simp | (i + 1) (j+1) (c::l) := assume hβ‚€ h₁, by simp [insert_nth]; exact insert_nth_comm i j l (nat.le_of_succ_le_succ hβ‚€) (nat.le_of_succ_le_succ h₁) lemma mem_insert_nth {a b : Ξ±} : βˆ€ {n : β„•} {l : list Ξ±} (hi : n ≀ l.length), a ∈ l.insert_nth n b ↔ a = b ∨ a ∈ l | 0 as h := iff.rfl | (n+1) [] h := (nat.not_succ_le_zero _ h).elim | (n+1) (a'::as) h := begin dsimp [list.insert_nth], erw [mem_insert_nth (nat.le_of_succ_le_succ h), ← or.assoc, or_comm (a = a'), or.assoc] end lemma insert_nth_of_length_lt (l : list Ξ±) (x : Ξ±) (n : β„•) (h : l.length < n) : insert_nth n x l = l := begin induction l with hd tl IH generalizing n, { cases n, { simpa using h }, { simp } }, { cases n, { simpa using h }, { simp only [nat.succ_lt_succ_iff, length] at h, simpa using IH _ h } } end @[simp] lemma insert_nth_length_self (l : list Ξ±) (x : Ξ±) : insert_nth l.length x l = l ++ [x] := begin induction l with hd tl IH, { simp }, { simpa using IH } end lemma length_le_length_insert_nth (l : list Ξ±) (x : Ξ±) (n : β„•) : l.length ≀ (insert_nth n x l).length := begin cases le_or_lt n l.length with hn hn, { rw length_insert_nth _ _ hn, exact (nat.lt_succ_self _).le }, { rw insert_nth_of_length_lt _ _ _ hn } end lemma length_insert_nth_le_succ (l : list Ξ±) (x : Ξ±) (n : β„•) : (insert_nth n x l).length ≀ l.length + 1 := begin cases le_or_lt n l.length with hn hn, { rw length_insert_nth _ _ hn }, { rw insert_nth_of_length_lt _ _ _ hn, exact (nat.lt_succ_self _).le } end lemma nth_le_insert_nth_of_lt (l : list Ξ±) (x : Ξ±) (n k : β„•) (hn : k < n) (hk : k < l.length) (hk' : k < (insert_nth n x l).length := hk.trans_le (length_le_length_insert_nth _ _ _)): (insert_nth n x l).nth_le k hk' = l.nth_le k hk := begin induction n with n IH generalizing k l, { simpa using hn }, { cases l with hd tl, { simp }, { cases k, { simp }, { rw nat.succ_lt_succ_iff at hn, simpa using IH _ _ hn _ } } } end @[simp] lemma nth_le_insert_nth_self (l : list Ξ±) (x : Ξ±) (n : β„•) (hn : n ≀ l.length) (hn' : n < (insert_nth n x l).length := by rwa [length_insert_nth _ _ hn, nat.lt_succ_iff]) : (insert_nth n x l).nth_le n hn' = x := begin induction l with hd tl IH generalizing n, { simp only [length, nonpos_iff_eq_zero] at hn, simp [hn] }, { cases n, { simp }, { simp only [nat.succ_le_succ_iff, length] at hn, simpa using IH _ hn } } end lemma nth_le_insert_nth_add_succ (l : list Ξ±) (x : Ξ±) (n k : β„•) (hk' : n + k < l.length) (hk : n + k + 1 < (insert_nth n x l).length := by rwa [length_insert_nth _ _ (le_self_add.trans hk'.le), nat.succ_lt_succ_iff]) : (insert_nth n x l).nth_le (n + k + 1) hk = nth_le l (n + k) hk' := begin induction l with hd tl IH generalizing n k, { simpa using hk' }, { cases n, { simpa }, { simpa [succ_add] using IH _ _ _ } } end lemma insert_nth_injective (n : β„•) (x : Ξ±) : function.injective (insert_nth n x) := begin induction n with n IH, { have : insert_nth 0 x = cons x := funext (Ξ» _, rfl), simp [this] }, { rintros (_|⟨a, as⟩) (_|⟨b, bs⟩) h; simpa [IH.eq_iff] using h <|> refl } end end insert_nth /-! ### map -/ @[simp] lemma map_nil (f : Ξ± β†’ Ξ²) : map f [] = [] := rfl theorem map_eq_foldr (f : Ξ± β†’ Ξ²) (l : list Ξ±) : map f l = foldr (Ξ» a bs, f a :: bs) [] l := by induction l; simp * lemma map_congr {f g : Ξ± β†’ Ξ²} : βˆ€ {l : list Ξ±}, (βˆ€ x ∈ l, f x = g x) β†’ map f l = map g l | [] _ := rfl | (a::l) h := let ⟨h₁, hβ‚‚βŸ© := forall_mem_cons.1 h in by rw [map, map, h₁, map_congr hβ‚‚] lemma map_eq_map_iff {f g : Ξ± β†’ Ξ²} {l : list Ξ±} : map f l = map g l ↔ (βˆ€ x ∈ l, f x = g x) := begin refine ⟨_, map_congr⟩, intros h x hx, rw [mem_iff_nth_le] at hx, rcases hx with ⟨n, hn, rfl⟩, rw [nth_le_map_rev f, nth_le_map_rev g], congr, exact h end theorem map_concat (f : Ξ± β†’ Ξ²) (a : Ξ±) (l : list Ξ±) : map f (concat l a) = concat (map f l) (f a) := by induction l; [refl, simp only [*, concat_eq_append, cons_append, map, map_append]]; split; refl @[simp] theorem map_id'' (l : list Ξ±) : map (Ξ» x, x) l = l := map_id _ theorem map_id' {f : Ξ± β†’ Ξ±} (h : βˆ€ x, f x = x) (l : list Ξ±) : map f l = l := by simp [show f = id, from funext h] theorem eq_nil_of_map_eq_nil {f : Ξ± β†’ Ξ²} {l : list Ξ±} (h : map f l = nil) : l = nil := eq_nil_of_length_eq_zero $ by rw [← length_map f l, h]; refl @[simp] theorem map_join (f : Ξ± β†’ Ξ²) (L : list (list Ξ±)) : map f (join L) = join (map (map f) L) := by induction L; [refl, simp only [*, join, map, map_append]] theorem bind_ret_eq_map (f : Ξ± β†’ Ξ²) (l : list Ξ±) : l.bind (list.ret ∘ f) = map f l := by unfold list.bind; induction l; simp only [map, join, list.ret, cons_append, nil_append, *]; split; refl lemma bind_congr {l : list Ξ±} {f g : Ξ± β†’ list Ξ²} (h : βˆ€ x ∈ l, f x = g x) : list.bind l f = list.bind l g := (congr_arg list.join $ map_congr h : _) @[simp] theorem map_eq_map {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (l : list Ξ±) : f <$> l = map f l := rfl @[simp] theorem map_tail (f : Ξ± β†’ Ξ²) (l) : map f (tail l) = tail (map f l) := by cases l; refl @[simp] theorem map_injective_iff {f : Ξ± β†’ Ξ²} : injective (map f) ↔ injective f := begin split; intros h x y hxy, { suffices : [x] = [y], { simpa using this }, apply h, simp [hxy] }, { induction y generalizing x, simpa using hxy, cases x, simpa using hxy, simp at hxy, simp [y_ih hxy.2, h hxy.1] } end /-- A single `list.map` of a composition of functions is equal to composing a `list.map` with another `list.map`, fully applied. This is the reverse direction of `list.map_map`. -/ lemma comp_map (h : Ξ² β†’ Ξ³) (g : Ξ± β†’ Ξ²) (l : list Ξ±) : map (h ∘ g) l = map h (map g l) := (map_map _ _ _).symm /-- Composing a `list.map` with another `list.map` is equal to a single `list.map` of composed functions. -/ @[simp] lemma map_comp_map (g : Ξ² β†’ Ξ³) (f : Ξ± β†’ Ξ²) : map g ∘ map f = map (g ∘ f) := by { ext l, rw comp_map } theorem map_filter_eq_foldr (f : Ξ± β†’ Ξ²) (p : Ξ± β†’ Prop) [decidable_pred p] (as : list Ξ±) : map f (filter p as) = foldr (Ξ» a bs, if p a then f a :: bs else bs) [] as := by { induction as, { refl }, { simp! [*, apply_ite (map f)] } } lemma last_map (f : Ξ± β†’ Ξ²) {l : list Ξ±} (hl : l β‰  []) : (l.map f).last (mt eq_nil_of_map_eq_nil hl) = f (l.last hl) := begin induction l with l_ih l_tl l_ih, { apply (hl rfl).elim }, { cases l_tl, { simp }, { simpa using l_ih } } end /-! ### mapβ‚‚ -/ theorem nil_mapβ‚‚ (f : Ξ± β†’ Ξ² β†’ Ξ³) (l : list Ξ²) : mapβ‚‚ f [] l = [] := by cases l; refl theorem mapβ‚‚_nil (f : Ξ± β†’ Ξ² β†’ Ξ³) (l : list Ξ±) : mapβ‚‚ f l [] = [] := by cases l; refl @[simp] theorem mapβ‚‚_flip (f : Ξ± β†’ Ξ² β†’ Ξ³) : βˆ€ as bs, mapβ‚‚ (flip f) bs as = mapβ‚‚ f as bs | [] [] := rfl | [] (b :: bs) := rfl | (a :: as) [] := rfl | (a :: as) (b :: bs) := by { simp! [mapβ‚‚_flip], refl } /-! ### take, drop -/ @[simp] theorem take_zero (l : list Ξ±) : take 0 l = [] := rfl @[simp] theorem take_nil : βˆ€ n, take n [] = ([] : list Ξ±) | 0 := rfl | (n+1) := rfl theorem take_cons (n) (a : Ξ±) (l : list Ξ±) : take (succ n) (a::l) = a :: take n l := rfl @[simp] theorem take_length : βˆ€ (l : list Ξ±), take (length l) l = l | [] := rfl | (a::l) := begin change a :: (take (length l) l) = a :: l, rw take_length end theorem take_all_of_le : βˆ€ {n} {l : list Ξ±}, length l ≀ n β†’ take n l = l | 0 [] h := rfl | 0 (a::l) h := absurd h (not_le_of_gt (zero_lt_succ _)) | (n+1) [] h := rfl | (n+1) (a::l) h := begin change a :: take n l = a :: l, rw [take_all_of_le (le_of_succ_le_succ h)] end @[simp] theorem take_left : βˆ€ l₁ lβ‚‚ : list Ξ±, take (length l₁) (l₁ ++ lβ‚‚) = l₁ | [] lβ‚‚ := rfl | (a::l₁) lβ‚‚ := congr_arg (cons a) (take_left l₁ lβ‚‚) theorem take_left' {l₁ lβ‚‚ : list Ξ±} {n} (h : length l₁ = n) : take n (l₁ ++ lβ‚‚) = l₁ := by rw ← h; apply take_left theorem take_take : βˆ€ (n m) (l : list Ξ±), take n (take m l) = take (min n m) l | n 0 l := by rw [min_zero, take_zero, take_nil] | 0 m l := by rw [zero_min, take_zero, take_zero] | (succ n) (succ m) nil := by simp only [take_nil] | (succ n) (succ m) (a::l) := by simp only [take, min_succ_succ, take_take n m l]; split; refl theorem take_repeat (a : Ξ±) : βˆ€ (n m : β„•), take n (repeat a m) = repeat a (min n m) | n 0 := by simp | 0 m := by simp | (succ n) (succ m) := by simp [min_succ_succ, take_repeat] lemma map_take {Ξ± Ξ² : Type*} (f : Ξ± β†’ Ξ²) : βˆ€ (L : list Ξ±) (i : β„•), (L.take i).map f = (L.map f).take i | [] i := by simp | L 0 := by simp | (h :: t) (n+1) := by { dsimp, rw [map_take], } /-- Taking the first `n` elements in `l₁ ++ lβ‚‚` is the same as appending the first `n` elements of `l₁` to the first `n - l₁.length` elements of `lβ‚‚`. -/ lemma take_append_eq_append_take {l₁ lβ‚‚ : list Ξ±} {n : β„•} : take n (l₁ ++ lβ‚‚) = take n l₁ ++ take (n - l₁.length) lβ‚‚ := begin induction l₁ generalizing n, { simp }, cases n, { simp }, simp * end lemma take_append_of_le_length {l₁ lβ‚‚ : list Ξ±} {n : β„•} (h : n ≀ l₁.length) : (l₁ ++ lβ‚‚).take n = l₁.take n := by simp [take_append_eq_append_take, tsub_eq_zero_iff_le.mpr h] /-- Taking the first `l₁.length + i` elements in `l₁ ++ lβ‚‚` is the same as appending the first `i` elements of `lβ‚‚` to `l₁`. -/ lemma take_append {l₁ lβ‚‚ : list Ξ±} (i : β„•) : take (l₁.length + i) (l₁ ++ lβ‚‚) = l₁ ++ (take i lβ‚‚) := by simp [take_append_eq_append_take, take_all_of_le le_self_add] /-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of length `> i`. Version designed to rewrite from the big list to the small list. -/ lemma nth_le_take (L : list Ξ±) {i j : β„•} (hi : i < L.length) (hj : i < j) : nth_le L i hi = nth_le (L.take j) i (by { rw length_take, exact lt_min hj hi }) := by { rw nth_le_of_eq (take_append_drop j L).symm hi, exact nth_le_append _ _ } /-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of length `> i`. Version designed to rewrite from the small list to the big list. -/ lemma nth_le_take' (L : list Ξ±) {i j : β„•} (hi : i < (L.take j).length) : nth_le (L.take j) i hi = nth_le L i (lt_of_lt_of_le hi (by simp [le_refl])) := by { simp at hi, rw nth_le_take L _ hi.1 } lemma nth_take {l : list Ξ±} {n m : β„•} (h : m < n) : (l.take n).nth m = l.nth m := begin induction n with n hn generalizing l m, { simp only [nat.nat_zero_eq_zero] at h, exact absurd h (not_lt_of_le m.zero_le) }, { cases l with hd tl, { simp only [take_nil] }, { cases m, { simp only [nth, take] }, { simpa only using hn (nat.lt_of_succ_lt_succ h) } } }, end @[simp] lemma nth_take_of_succ {l : list Ξ±} {n : β„•} : (l.take (n + 1)).nth n = l.nth n := nth_take (nat.lt_succ_self n) lemma take_succ {l : list Ξ±} {n : β„•} : l.take (n + 1) = l.take n ++ (l.nth n).to_list := begin induction l with hd tl hl generalizing n, { simp only [option.to_list, nth, take_nil, append_nil]}, { cases n, { simp only [option.to_list, nth, eq_self_iff_true, and_self, take, nil_append] }, { simp only [hl, cons_append, nth, eq_self_iff_true, and_self, take] } } end @[simp] lemma take_eq_nil_iff {l : list Ξ±} {k : β„•} : l.take k = [] ↔ l = [] ∨ k = 0 := by { cases l; cases k; simp [nat.succ_ne_zero] } lemma take_eq_take : βˆ€ {l : list Ξ±} {m n : β„•}, l.take m = l.take n ↔ min m l.length = min n l.length | [] m n := by simp | (x :: xs) 0 0 := by simp | (x :: xs) (m + 1) 0 := by simp | (x :: xs) 0 (n + 1) := by simp [@eq_comm β„• 0] | (x :: xs) (m + 1) (n + 1) := by simp [nat.min_succ_succ, take_eq_take] lemma take_add (l : list Ξ±) (m n : β„•) : l.take (m + n) = l.take m ++ (l.drop m).take n := begin convert_to take (m + n) (take m l ++ drop m l) = take m l ++ take n (drop m l), { rw take_append_drop }, rw [take_append_eq_append_take, take_all_of_le, append_right_inj], swap, { transitivity m, { apply length_take_le }, { simp }}, simp only [take_eq_take, length_take, length_drop], generalize : l.length = k, by_cases h : m ≀ k, { simp [min_eq_left_iff.mpr h] }, { push_neg at h, simp [nat.sub_eq_zero_of_le (le_of_lt h)] }, end lemma init_eq_take (l : list Ξ±) : l.init = l.take l.length.pred := begin cases l with x l, { simp [init] }, { induction l with hd tl hl generalizing x, { simp [init], }, { simp [init, hl] } } end lemma init_take {n : β„•} {l : list Ξ±} (h : n < l.length) : (l.take n).init = l.take n.pred := by simp [init_eq_take, min_eq_left_of_lt h, take_take, pred_le] @[simp] lemma init_cons_of_ne_nil {Ξ± : Type*} {x : Ξ±} : βˆ€ {l : list Ξ±} (h : l β‰  []), (x :: l).init = x :: l.init | [] h := false.elim (h rfl) | (a :: l) _ := by simp [init] @[simp] lemma init_append_of_ne_nil {Ξ± : Type*} {l : list Ξ±} : βˆ€ (l' : list Ξ±) (h : l β‰  []), (l' ++ l).init = l' ++ l.init | [] _ := by simp only [nil_append] | (a :: l') h := by simp [append_ne_nil_of_ne_nil_right l' l h, init_append_of_ne_nil l' h] @[simp] lemma drop_eq_nil_of_le {l : list Ξ±} {k : β„•} (h : l.length ≀ k) : l.drop k = [] := by simpa [←length_eq_zero] using tsub_eq_zero_iff_le.mpr h lemma drop_eq_nil_iff_le {l : list Ξ±} {k : β„•} : l.drop k = [] ↔ l.length ≀ k := begin refine ⟨λ h, _, drop_eq_nil_of_le⟩, induction k with k hk generalizing l, { simp only [drop] at h, simp [h] }, { cases l, { simp }, { simp only [drop] at h, simpa [nat.succ_le_succ_iff] using hk h } } end lemma tail_drop (l : list Ξ±) (n : β„•) : (l.drop n).tail = l.drop (n + 1) := begin induction l with hd tl hl generalizing n, { simp }, { cases n, { simp }, { simp [hl] } } end lemma cons_nth_le_drop_succ {l : list Ξ±} {n : β„•} (hn : n < l.length) : l.nth_le n hn :: l.drop (n + 1) = l.drop n := begin induction l with hd tl hl generalizing n, { exact absurd n.zero_le (not_le_of_lt (by simpa using hn)) }, { cases n, { simp }, { simp only [nat.succ_lt_succ_iff, list.length] at hn, simpa [list.nth_le, list.drop] using hl hn } } end theorem drop_nil : βˆ€ n, drop n [] = ([] : list Ξ±) := Ξ» _, drop_eq_nil_of_le (nat.zero_le _) @[simp] theorem drop_one : βˆ€ l : list Ξ±, drop 1 l = tail l | [] := rfl | (a :: l) := rfl theorem drop_add : βˆ€ m n (l : list Ξ±), drop (m + n) l = drop m (drop n l) | m 0 l := rfl | m (n+1) [] := (drop_nil _).symm | m (n+1) (a::l) := drop_add m n _ @[simp] theorem drop_left : βˆ€ l₁ lβ‚‚ : list Ξ±, drop (length l₁) (l₁ ++ lβ‚‚) = lβ‚‚ | [] lβ‚‚ := rfl | (a::l₁) lβ‚‚ := drop_left l₁ lβ‚‚ theorem drop_left' {l₁ lβ‚‚ : list Ξ±} {n} (h : length l₁ = n) : drop n (l₁ ++ lβ‚‚) = lβ‚‚ := by rw ← h; apply drop_left theorem drop_eq_nth_le_cons : βˆ€ {n} {l : list Ξ±} h, drop n l = nth_le l n h :: drop (n+1) l | 0 (a::l) h := rfl | (n+1) (a::l) h := @drop_eq_nth_le_cons n _ _ @[simp] lemma drop_length (l : list Ξ±) : l.drop l.length = [] := calc l.drop l.length = (l ++ []).drop l.length : by simp ... = [] : drop_left _ _ lemma drop_length_cons {l : list Ξ±} (h : l β‰  []) (a : Ξ±) : (a :: l).drop l.length = [l.last h] := begin induction l with y l ih generalizing a, { cases h rfl }, { simp only [drop, length], by_cases h₁ : l = [], { simp [h₁] }, rw last_cons h₁, exact ih h₁ y }, end /-- Dropping the elements up to `n` in `l₁ ++ lβ‚‚` is the same as dropping the elements up to `n` in `l₁`, dropping the elements up to `n - l₁.length` in `lβ‚‚`, and appending them. -/ lemma drop_append_eq_append_drop {l₁ lβ‚‚ : list Ξ±} {n : β„•} : drop n (l₁ ++ lβ‚‚) = drop n l₁ ++ drop (n - l₁.length) lβ‚‚ := begin induction l₁ generalizing n, { simp }, cases n, { simp }, simp * end lemma drop_append_of_le_length {l₁ lβ‚‚ : list Ξ±} {n : β„•} (h : n ≀ l₁.length) : (l₁ ++ lβ‚‚).drop n = l₁.drop n ++ lβ‚‚ := by simp [drop_append_eq_append_drop, tsub_eq_zero_iff_le.mpr h] /-- Dropping the elements up to `l₁.length + i` in `l₁ + lβ‚‚` is the same as dropping the elements up to `i` in `lβ‚‚`. -/ lemma drop_append {l₁ lβ‚‚ : list Ξ±} (i : β„•) : drop (l₁.length + i) (l₁ ++ lβ‚‚) = drop i lβ‚‚ := by simp [drop_append_eq_append_drop, take_all_of_le le_self_add] lemma drop_sizeof_le [has_sizeof Ξ±] (l : list Ξ±) : βˆ€ (n : β„•), (l.drop n).sizeof ≀ l.sizeof := begin induction l with _ _ lih; intro n, { rw [drop_nil] }, { induction n with n nih, { refl, }, { exact trans (lih _) le_add_self } } end /-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by dropping the first `i` elements. Version designed to rewrite from the big list to the small list. -/ lemma nth_le_drop (L : list Ξ±) {i j : β„•} (h : i + j < L.length) : nth_le L (i + j) h = nth_le (L.drop i) j begin have A : i < L.length := lt_of_le_of_lt (nat.le.intro rfl) h, rw (take_append_drop i L).symm at h, simpa only [le_of_lt A, min_eq_left, add_lt_add_iff_left, length_take, length_append] using h end := begin have A : length (take i L) = i, by simp [le_of_lt (lt_of_le_of_lt (nat.le.intro rfl) h)], rw [nth_le_of_eq (take_append_drop i L).symm h, nth_le_append_right]; simp [A] end /-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by dropping the first `i` elements. Version designed to rewrite from the small list to the big list. -/ lemma nth_le_drop' (L : list Ξ±) {i j : β„•} (h : j < (L.drop i).length) : nth_le (L.drop i) j h = nth_le L (i + j) (lt_tsub_iff_left.mp ((length_drop i L) β–Έ h)) := by rw nth_le_drop lemma nth_drop (L : list Ξ±) (i j : β„•) : nth (L.drop i) j = nth L (i + j) := begin ext, simp only [nth_eq_some, nth_le_drop', option.mem_def], split; exact Ξ» ⟨h, ha⟩, ⟨by simpa [lt_tsub_iff_left] using h, ha⟩ end @[simp] theorem drop_drop (n : β„•) : βˆ€ (m) (l : list Ξ±), drop n (drop m l) = drop (n + m) l | m [] := by simp | 0 l := by simp | (m+1) (a::l) := calc drop n (drop (m + 1) (a :: l)) = drop n (drop m l) : rfl ... = drop (n + m) l : drop_drop m l ... = drop (n + (m + 1)) (a :: l) : rfl theorem drop_take : βˆ€ (m : β„•) (n : β„•) (l : list Ξ±), drop m (take (m + n) l) = take n (drop m l) | 0 n _ := by simp | (m+1) n nil := by simp | (m+1) n (_::l) := have h: m + 1 + n = (m+n) + 1, by ac_refl, by simpa [take_cons, h] using drop_take m n l lemma map_drop {Ξ± Ξ² : Type*} (f : Ξ± β†’ Ξ²) : βˆ€ (L : list Ξ±) (i : β„•), (L.drop i).map f = (L.map f).drop i | [] i := by simp | L 0 := by simp | (h :: t) (n+1) := by { dsimp, rw [map_drop], } theorem modify_nth_tail_eq_take_drop (f : list Ξ± β†’ list Ξ±) (H : f [] = []) : βˆ€ n l, modify_nth_tail f n l = take n l ++ f (drop n l) | 0 l := rfl | (n+1) [] := H.symm | (n+1) (b::l) := congr_arg (cons b) (modify_nth_tail_eq_take_drop n l) theorem modify_nth_eq_take_drop (f : Ξ± β†’ Ξ±) : βˆ€ n l, modify_nth f n l = take n l ++ modify_head f (drop n l) := modify_nth_tail_eq_take_drop _ rfl theorem modify_nth_eq_take_cons_drop (f : Ξ± β†’ Ξ±) {n l} (h) : modify_nth f n l = take n l ++ f (nth_le l n h) :: drop (n+1) l := by rw [modify_nth_eq_take_drop, drop_eq_nth_le_cons h]; refl theorem update_nth_eq_take_cons_drop (a : Ξ±) {n l} (h : n < length l) : update_nth l n a = take n l ++ a :: drop (n+1) l := by rw [update_nth_eq_modify_nth, modify_nth_eq_take_cons_drop _ h] lemma reverse_take {Ξ±} {xs : list Ξ±} (n : β„•) (h : n ≀ xs.length) : xs.reverse.take n = (xs.drop (xs.length - n)).reverse := begin induction xs generalizing n; simp only [reverse_cons, drop, reverse_nil, zero_tsub, length, take_nil], cases h.lt_or_eq_dec with h' h', { replace h' := le_of_succ_le_succ h', rwa [take_append_of_le_length, xs_ih _ h'], rw [show xs_tl.length + 1 - n = succ (xs_tl.length - n), from _, drop], { rwa [succ_eq_add_one, ← tsub_add_eq_add_tsub] }, { rwa length_reverse } }, { subst h', rw [length, tsub_self, drop], suffices : xs_tl.length + 1 = (xs_tl.reverse ++ [xs_hd]).length, by rw [this, take_length, reverse_cons], rw [length_append, length_reverse], refl } end @[simp] lemma update_nth_eq_nil (l : list Ξ±) (n : β„•) (a : Ξ±) : l.update_nth n a = [] ↔ l = [] := by cases l; cases n; simp only [update_nth] section take' variable [inhabited Ξ±] @[simp] theorem take'_length : βˆ€ n l, length (@take' Ξ± _ n l) = n | 0 l := rfl | (n+1) l := congr_arg succ (take'_length _ _) @[simp] theorem take'_nil : βˆ€ n, take' n (@nil Ξ±) = repeat default n | 0 := rfl | (n+1) := congr_arg (cons _) (take'_nil _) theorem take'_eq_take : βˆ€ {n} {l : list Ξ±}, n ≀ length l β†’ take' n l = take n l | 0 l h := rfl | (n+1) (a::l) h := congr_arg (cons _) $ take'_eq_take $ le_of_succ_le_succ h @[simp] theorem take'_left (l₁ lβ‚‚ : list Ξ±) : take' (length l₁) (l₁ ++ lβ‚‚) = l₁ := (take'_eq_take (by simp only [length_append, nat.le_add_right])).trans (take_left _ _) theorem take'_left' {l₁ lβ‚‚ : list Ξ±} {n} (h : length l₁ = n) : take' n (l₁ ++ lβ‚‚) = l₁ := by rw ← h; apply take'_left end take' /-! ### foldl, foldr -/ lemma foldl_ext (f g : Ξ± β†’ Ξ² β†’ Ξ±) (a : Ξ±) {l : list Ξ²} (H : βˆ€ a : Ξ±, βˆ€ b ∈ l, f a b = g a b) : foldl f a l = foldl g a l := begin induction l with hd tl ih generalizing a, {refl}, unfold foldl, rw [ih (Ξ» a b bin, H a b $ mem_cons_of_mem _ bin), H a hd (mem_cons_self _ _)] end lemma foldr_ext (f g : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) {l : list Ξ±} (H : βˆ€ a ∈ l, βˆ€ b : Ξ², f a b = g a b) : foldr f b l = foldr g b l := begin induction l with hd tl ih, {refl}, simp only [mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] at H, simp only [foldr, ih H.2, H.1] end @[simp] theorem foldl_nil (f : Ξ± β†’ Ξ² β†’ Ξ±) (a : Ξ±) : foldl f a [] = a := rfl @[simp] theorem foldl_cons (f : Ξ± β†’ Ξ² β†’ Ξ±) (a : Ξ±) (b : Ξ²) (l : list Ξ²) : foldl f a (b::l) = foldl f (f a b) l := rfl @[simp] theorem foldr_nil (f : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) : foldr f b [] = b := rfl @[simp] theorem foldr_cons (f : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) (a : Ξ±) (l : list Ξ±) : foldr f b (a::l) = f a (foldr f b l) := rfl @[simp] theorem foldl_append (f : Ξ± β†’ Ξ² β†’ Ξ±) : βˆ€ (a : Ξ±) (l₁ lβ‚‚ : list Ξ²), foldl f a (l₁++lβ‚‚) = foldl f (foldl f a l₁) lβ‚‚ | a [] lβ‚‚ := rfl | a (b::l₁) lβ‚‚ := by simp only [cons_append, foldl_cons, foldl_append (f a b) l₁ lβ‚‚] @[simp] theorem foldr_append (f : Ξ± β†’ Ξ² β†’ Ξ²) : βˆ€ (b : Ξ²) (l₁ lβ‚‚ : list Ξ±), foldr f b (l₁++lβ‚‚) = foldr f (foldr f b lβ‚‚) l₁ | b [] lβ‚‚ := rfl | b (a::l₁) lβ‚‚ := by simp only [cons_append, foldr_cons, foldr_append b l₁ lβ‚‚] theorem foldl_fixed' {f : Ξ± β†’ Ξ² β†’ Ξ±} {a : Ξ±} (hf : βˆ€ b, f a b = a) : Ξ  l : list Ξ², foldl f a l = a | [] := rfl | (b::l) := by rw [foldl_cons, hf b, foldl_fixed' l] theorem foldr_fixed' {f : Ξ± β†’ Ξ² β†’ Ξ²} {b : Ξ²} (hf : βˆ€ a, f a b = b) : Ξ  l : list Ξ±, foldr f b l = b | [] := rfl | (a::l) := by rw [foldr_cons, foldr_fixed' l, hf a] @[simp] theorem foldl_fixed {a : Ξ±} : Ξ  l : list Ξ², foldl (Ξ» a b, a) a l = a := foldl_fixed' (Ξ» _, rfl) @[simp] theorem foldr_fixed {b : Ξ²} : Ξ  l : list Ξ±, foldr (Ξ» a b, b) b l = b := foldr_fixed' (Ξ» _, rfl) @[simp] theorem foldl_join (f : Ξ± β†’ Ξ² β†’ Ξ±) : βˆ€ (a : Ξ±) (L : list (list Ξ²)), foldl f a (join L) = foldl (foldl f) a L | a [] := rfl | a (l::L) := by simp only [join, foldl_append, foldl_cons, foldl_join (foldl f a l) L] @[simp] theorem foldr_join (f : Ξ± β†’ Ξ² β†’ Ξ²) : βˆ€ (b : Ξ²) (L : list (list Ξ±)), foldr f b (join L) = foldr (Ξ» l b, foldr f b l) b L | a [] := rfl | a (l::L) := by simp only [join, foldr_append, foldr_join a L, foldr_cons] theorem foldl_reverse (f : Ξ± β†’ Ξ² β†’ Ξ±) (a : Ξ±) (l : list Ξ²) : foldl f a (reverse l) = foldr (Ξ»x y, f y x) a l := by induction l; [refl, simp only [*, reverse_cons, foldl_append, foldl_cons, foldl_nil, foldr]] theorem foldr_reverse (f : Ξ± β†’ Ξ² β†’ Ξ²) (a : Ξ²) (l : list Ξ±) : foldr f a (reverse l) = foldl (Ξ»x y, f y x) a l := let t := foldl_reverse (Ξ»x y, f y x) a (reverse l) in by rw reverse_reverse l at t; rwa t @[simp] theorem foldr_eta : βˆ€ (l : list Ξ±), foldr cons [] l = l | [] := rfl | (x::l) := by simp only [foldr_cons, foldr_eta l]; split; refl @[simp] theorem reverse_foldl {l : list Ξ±} : reverse (foldl (Ξ» t h, h :: t) [] l) = l := by rw ←foldr_reverse; simp @[simp] theorem foldl_map (g : Ξ² β†’ Ξ³) (f : Ξ± β†’ Ξ³ β†’ Ξ±) (a : Ξ±) (l : list Ξ²) : foldl f a (map g l) = foldl (Ξ»x y, f x (g y)) a l := by revert a; induction l; intros; [refl, simp only [*, map, foldl]] @[simp] theorem foldr_map (g : Ξ² β†’ Ξ³) (f : Ξ³ β†’ Ξ± β†’ Ξ±) (a : Ξ±) (l : list Ξ²) : foldr f a (map g l) = foldr (f ∘ g) a l := by revert a; induction l; intros; [refl, simp only [*, map, foldr]] theorem foldl_map' {Ξ± Ξ²: Type u} (g : Ξ± β†’ Ξ²) (f : Ξ± β†’ Ξ± β†’ Ξ±) (f' : Ξ² β†’ Ξ² β†’ Ξ²) (a : Ξ±) (l : list Ξ±) (h : βˆ€ x y, f' (g x) (g y) = g (f x y)) : list.foldl f' (g a) (l.map g) = g (list.foldl f a l) := begin induction l generalizing a, { simp }, { simp [l_ih, h] } end theorem foldr_map' {Ξ± Ξ²: Type u} (g : Ξ± β†’ Ξ²) (f : Ξ± β†’ Ξ± β†’ Ξ±) (f' : Ξ² β†’ Ξ² β†’ Ξ²) (a : Ξ±) (l : list Ξ±) (h : βˆ€ x y, f' (g x) (g y) = g (f x y)) : list.foldr f' (g a) (l.map g) = g (list.foldr f a l) := begin induction l generalizing a, { simp }, { simp [l_ih, h] } end theorem foldl_hom (l : list Ξ³) (f : Ξ± β†’ Ξ²) (op : Ξ± β†’ Ξ³ β†’ Ξ±) (op' : Ξ² β†’ Ξ³ β†’ Ξ²) (a : Ξ±) (h : βˆ€a x, f (op a x) = op' (f a) x) : foldl op' (f a) l = f (foldl op a l) := eq.symm $ by { revert a, induction l; intros; [refl, simp only [*, foldl]] } theorem foldr_hom (l : list Ξ³) (f : Ξ± β†’ Ξ²) (op : Ξ³ β†’ Ξ± β†’ Ξ±) (op' : Ξ³ β†’ Ξ² β†’ Ξ²) (a : Ξ±) (h : βˆ€x a, f (op x a) = op' x (f a)) : foldr op' (f a) l = f (foldr op a l) := by { revert a, induction l; intros; [refl, simp only [*, foldr]] } lemma foldl_homβ‚‚ (l : list ΞΉ) (f : Ξ± β†’ Ξ² β†’ Ξ³) (op₁ : Ξ± β†’ ΞΉ β†’ Ξ±) (opβ‚‚ : Ξ² β†’ ΞΉ β†’ Ξ²) (op₃ : Ξ³ β†’ ΞΉ β†’ Ξ³) (a : Ξ±) (b : Ξ²) (h : βˆ€ a b i, f (op₁ a i) (opβ‚‚ b i) = op₃ (f a b) i) : foldl op₃ (f a b) l = f (foldl op₁ a l) (foldl opβ‚‚ b l) := eq.symm $ by { revert a b, induction l; intros; [refl, simp only [*, foldl]] } lemma foldr_homβ‚‚ (l : list ΞΉ) (f : Ξ± β†’ Ξ² β†’ Ξ³) (op₁ : ΞΉ β†’ Ξ± β†’ Ξ±) (opβ‚‚ : ΞΉ β†’ Ξ² β†’ Ξ²) (op₃ : ΞΉ β†’ Ξ³ β†’ Ξ³) (a : Ξ±) (b : Ξ²) (h : βˆ€ a b i, f (op₁ i a) (opβ‚‚ i b) = op₃ i (f a b)) : foldr op₃ (f a b) l = f (foldr op₁ a l) (foldr opβ‚‚ b l) := by { revert a, induction l; intros; [refl, simp only [*, foldr]] } lemma injective_foldl_comp {Ξ± : Type*} {l : list (Ξ± β†’ Ξ±)} {f : Ξ± β†’ Ξ±} (hl : βˆ€ f ∈ l, function.injective f) (hf : function.injective f): function.injective (@list.foldl (Ξ± β†’ Ξ±) (Ξ± β†’ Ξ±) function.comp f l) := begin induction l generalizing f, { exact hf }, { apply l_ih (Ξ» _ h, hl _ (list.mem_cons_of_mem _ h)), apply function.injective.comp hf, apply hl _ (list.mem_cons_self _ _) } end /-- Induction principle for values produced by a `foldr`: if a property holds for the seed element `b : Ξ²` and for all incremental `op : Ξ± β†’ Ξ² β†’ Ξ²` performed on the elements `(a : Ξ±) ∈ l`. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ def foldr_rec_on {C : Ξ² β†’ Sort*} (l : list Ξ±) (op : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) (hb : C b) (hl : βˆ€ (b : Ξ²) (hb : C b) (a : Ξ±) (ha : a ∈ l), C (op a b)) : C (foldr op b l) := begin induction l with hd tl IH, { exact hb }, { refine hl _ _ hd (mem_cons_self hd tl), refine IH _, intros y hy x hx, exact hl y hy x (mem_cons_of_mem hd hx) } end /-- Induction principle for values produced by a `foldl`: if a property holds for the seed element `b : Ξ²` and for all incremental `op : Ξ² β†’ Ξ± β†’ Ξ²` performed on the elements `(a : Ξ±) ∈ l`. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ def foldl_rec_on {C : Ξ² β†’ Sort*} (l : list Ξ±) (op : Ξ² β†’ Ξ± β†’ Ξ²) (b : Ξ²) (hb : C b) (hl : βˆ€ (b : Ξ²) (hb : C b) (a : Ξ±) (ha : a ∈ l), C (op b a)) : C (foldl op b l) := begin induction l with hd tl IH generalizing b, { exact hb }, { refine IH _ _ _, { intros y hy x hx, exact hl y hy x (mem_cons_of_mem hd hx) }, { exact hl b hb hd (mem_cons_self hd tl) } } end @[simp] lemma foldr_rec_on_nil {C : Ξ² β†’ Sort*} (op : Ξ± β†’ Ξ² β†’ Ξ²) (b) (hb : C b) (hl) : foldr_rec_on [] op b hb hl = hb := rfl @[simp] lemma foldr_rec_on_cons {C : Ξ² β†’ Sort*} (x : Ξ±) (l : list Ξ±) (op : Ξ± β†’ Ξ² β†’ Ξ²) (b) (hb : C b) (hl : βˆ€ (b : Ξ²) (hb : C b) (a : Ξ±) (ha : a ∈ (x :: l)), C (op a b)) : foldr_rec_on (x :: l) op b hb hl = hl _ (foldr_rec_on l op b hb (Ξ» b hb a ha, hl b hb a (mem_cons_of_mem _ ha))) x (mem_cons_self _ _) := rfl @[simp] lemma foldl_rec_on_nil {C : Ξ² β†’ Sort*} (op : Ξ² β†’ Ξ± β†’ Ξ²) (b) (hb : C b) (hl) : foldl_rec_on [] op b hb hl = hb := rfl /- scanl -/ section scanl variables {f : Ξ² β†’ Ξ± β†’ Ξ²} {b : Ξ²} {a : Ξ±} {l : list Ξ±} lemma length_scanl : βˆ€ a l, length (scanl f a l) = l.length + 1 | a [] := rfl | a (x :: l) := by erw [length_cons, length_cons, length_scanl] @[simp] lemma scanl_nil (b : Ξ²) : scanl f b nil = [b] := rfl @[simp] lemma scanl_cons : scanl f b (a :: l) = [b] ++ scanl f (f b a) l := by simp only [scanl, eq_self_iff_true, singleton_append, and_self] @[simp] lemma nth_zero_scanl : (scanl f b l).nth 0 = some b := begin cases l, { simp only [nth, scanl_nil] }, { simp only [nth, scanl_cons, singleton_append] } end @[simp] lemma nth_le_zero_scanl {h : 0 < (scanl f b l).length} : (scanl f b l).nth_le 0 h = b := begin cases l, { simp only [nth_le, scanl_nil] }, { simp only [nth_le, scanl_cons, singleton_append] } end lemma nth_succ_scanl {i : β„•} : (scanl f b l).nth (i + 1) = ((scanl f b l).nth i).bind (Ξ» x, (l.nth i).map (Ξ» y, f x y)) := begin induction l with hd tl hl generalizing b i, { symmetry, simp only [option.bind_eq_none', nth, forall_2_true_iff, not_false_iff, option.map_none', scanl_nil, option.not_mem_none, forall_true_iff] }, { simp only [nth, scanl_cons, singleton_append], cases i, { simp only [option.map_some', nth_zero_scanl, nth, option.some_bind'] }, { simp only [hl, nth] } } end lemma nth_le_succ_scanl {i : β„•} {h : i + 1 < (scanl f b l).length} : (scanl f b l).nth_le (i + 1) h = f ((scanl f b l).nth_le i (nat.lt_of_succ_lt h)) (l.nth_le i (nat.lt_of_succ_lt_succ (lt_of_lt_of_le h (le_of_eq (length_scanl b l))))) := begin induction i with i hi generalizing b l, { cases l, { simp only [length, zero_add, scanl_nil] at h, exact absurd h (lt_irrefl 1) }, { simp only [scanl_cons, singleton_append, nth_le_zero_scanl, nth_le] } }, { cases l, { simp only [length, add_lt_iff_neg_right, scanl_nil] at h, exact absurd h (not_lt_of_lt nat.succ_pos') }, { simp_rw scanl_cons, rw nth_le_append_right _, { simpa only [hi, length, succ_add_sub_one] }, { simp only [length, nat.zero_le, le_add_iff_nonneg_left] } } } end end scanl /- scanr -/ @[simp] theorem scanr_nil (f : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) : scanr f b [] = [b] := rfl @[simp] theorem scanr_aux_cons (f : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) : βˆ€ (a : Ξ±) (l : list Ξ±), scanr_aux f b (a::l) = (foldr f b (a::l), scanr f b l) | a [] := rfl | a (x::l) := let t := scanr_aux_cons x l in by simp only [scanr, scanr_aux, t, foldr_cons] @[simp] theorem scanr_cons (f : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) (a : Ξ±) (l : list Ξ±) : scanr f b (a::l) = foldr f b (a::l) :: scanr f b l := by simp only [scanr, scanr_aux_cons, foldr_cons]; split; refl section foldl_eq_foldr -- foldl and foldr coincide when f is commutative and associative variables {f : Ξ± β†’ Ξ± β†’ Ξ±} (hcomm : commutative f) (hassoc : associative f) include hassoc theorem foldl1_eq_foldr1 : βˆ€ a b l, foldl f a (l++[b]) = foldr f b (a::l) | a b nil := rfl | a b (c :: l) := by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l]; rw hassoc include hcomm theorem foldl_eq_of_comm_of_assoc : βˆ€ a b l, foldl f a (b::l) = f b (foldl f a l) | a b nil := hcomm a b | a b (c::l) := by simp only [foldl_cons]; rw [← foldl_eq_of_comm_of_assoc, right_comm _ hcomm hassoc]; refl theorem foldl_eq_foldr : βˆ€ a l, foldl f a l = foldr f a l | a nil := rfl | a (b :: l) := by simp only [foldr_cons, foldl_eq_of_comm_of_assoc hcomm hassoc]; rw (foldl_eq_foldr a l) end foldl_eq_foldr section foldl_eq_foldlr' variables {f : Ξ± β†’ Ξ² β†’ Ξ±} variables hf : βˆ€ a b c, f (f a b) c = f (f a c) b include hf theorem foldl_eq_of_comm' : βˆ€ a b l, foldl f a (b::l) = f (foldl f a l) b | a b [] := rfl | a b (c :: l) := by rw [foldl,foldl,foldl,← foldl_eq_of_comm',foldl,hf] theorem foldl_eq_foldr' : βˆ€ a l, foldl f a l = foldr (flip f) a l | a [] := rfl | a (b :: l) := by rw [foldl_eq_of_comm' hf,foldr,foldl_eq_foldr']; refl end foldl_eq_foldlr' section foldl_eq_foldlr' variables {f : Ξ± β†’ Ξ² β†’ Ξ²} variables hf : βˆ€ a b c, f a (f b c) = f b (f a c) include hf theorem foldr_eq_of_comm' : βˆ€ a b l, foldr f a (b::l) = foldr f (f b a) l | a b [] := rfl | a b (c :: l) := by rw [foldr,foldr,foldr,hf,← foldr_eq_of_comm']; refl end foldl_eq_foldlr' section variables {op : Ξ± β†’ Ξ± β†’ Ξ±} [ha : is_associative Ξ± op] [hc : is_commutative Ξ± op] local notation (name := op) a ` * ` b := op a b local notation (name := foldl) l ` <*> ` a := foldl op a l include ha lemma foldl_assoc : βˆ€ {l : list Ξ±} {a₁ aβ‚‚}, l <*> (a₁ * aβ‚‚) = a₁ * (l <*> aβ‚‚) | [] a₁ aβ‚‚ := rfl | (a :: l) a₁ aβ‚‚ := calc a::l <*> (a₁ * aβ‚‚) = l <*> (a₁ * (aβ‚‚ * a)) : by simp only [foldl_cons, ha.assoc] ... = a₁ * (a::l <*> aβ‚‚) : by rw [foldl_assoc, foldl_cons] lemma foldl_op_eq_op_foldr_assoc : βˆ€{l : list Ξ±} {a₁ aβ‚‚}, (l <*> a₁) * aβ‚‚ = a₁ * l.foldr (*) aβ‚‚ | [] a₁ aβ‚‚ := rfl | (a :: l) a₁ aβ‚‚ := by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc]; rw [foldl_op_eq_op_foldr_assoc] include hc lemma foldl_assoc_comm_cons {l : list Ξ±} {a₁ aβ‚‚} : (a₁ :: l) <*> aβ‚‚ = a₁ * (l <*> aβ‚‚) := by rw [foldl_cons, hc.comm, foldl_assoc] end /-! ### mfoldl, mfoldr, mmap -/ section mfoldl_mfoldr variables {m : Type v β†’ Type w} [monad m] @[simp] theorem mfoldl_nil (f : Ξ² β†’ Ξ± β†’ m Ξ²) {b} : mfoldl f b [] = pure b := rfl @[simp] theorem mfoldr_nil (f : Ξ± β†’ Ξ² β†’ m Ξ²) {b} : mfoldr f b [] = pure b := rfl @[simp] theorem mfoldl_cons {f : Ξ² β†’ Ξ± β†’ m Ξ²} {b a l} : mfoldl f b (a :: l) = f b a >>= Ξ» b', mfoldl f b' l := rfl @[simp] theorem mfoldr_cons {f : Ξ± β†’ Ξ² β†’ m Ξ²} {b a l} : mfoldr f b (a :: l) = mfoldr f b l >>= f a := rfl theorem mfoldr_eq_foldr (f : Ξ± β†’ Ξ² β†’ m Ξ²) (b l) : mfoldr f b l = foldr (Ξ» a mb, mb >>= f a) (pure b) l := by induction l; simp * attribute [simp] mmap mmap' variables [is_lawful_monad m] theorem mfoldl_eq_foldl (f : Ξ² β†’ Ξ± β†’ m Ξ²) (b l) : mfoldl f b l = foldl (Ξ» mb a, mb >>= Ξ» b, f b a) (pure b) l := begin suffices h : βˆ€ (mb : m Ξ²), (mb >>= Ξ» b, mfoldl f b l) = foldl (Ξ» mb a, mb >>= Ξ» b, f b a) mb l, by simp [←h (pure b)], induction l; intro, { simp }, { simp only [mfoldl, foldl, ←l_ih] with functor_norm } end @[simp] theorem mfoldl_append {f : Ξ² β†’ Ξ± β†’ m Ξ²} : βˆ€ {b l₁ lβ‚‚}, mfoldl f b (l₁ ++ lβ‚‚) = mfoldl f b l₁ >>= Ξ» x, mfoldl f x lβ‚‚ | _ [] _ := by simp only [nil_append, mfoldl_nil, pure_bind] | _ (_::_) _ := by simp only [cons_append, mfoldl_cons, mfoldl_append, is_lawful_monad.bind_assoc] @[simp] theorem mfoldr_append {f : Ξ± β†’ Ξ² β†’ m Ξ²} : βˆ€ {b l₁ lβ‚‚}, mfoldr f b (l₁ ++ lβ‚‚) = mfoldr f b lβ‚‚ >>= Ξ» x, mfoldr f x l₁ | _ [] _ := by simp only [nil_append, mfoldr_nil, bind_pure] | _ (_::_) _ := by simp only [mfoldr_cons, cons_append, mfoldr_append, is_lawful_monad.bind_assoc] end mfoldl_mfoldr /-! ### intersperse -/ @[simp] lemma intersperse_nil {Ξ± : Type u} (a : Ξ±) : intersperse a [] = [] := rfl @[simp] lemma intersperse_singleton {Ξ± : Type u} (a b : Ξ±) : intersperse a [b] = [b] := rfl @[simp] lemma intersperse_cons_cons {Ξ± : Type u} (a b c : Ξ±) (tl : list Ξ±) : intersperse a (b :: c :: tl) = b :: a :: intersperse a (c :: tl) := rfl /-! ### split_at and split_on -/ section split_at_on variables (p : Ξ± β†’ Prop) [decidable_pred p] (xs ys : list Ξ±) (ls : list (list Ξ±)) (f : list Ξ± β†’ list Ξ±) @[simp] theorem split_at_eq_take_drop : βˆ€ (n : β„•) (l : list Ξ±), split_at n l = (take n l, drop n l) | 0 a := rfl | (succ n) [] := rfl | (succ n) (x :: xs) := by simp only [split_at, split_at_eq_take_drop n xs, take, drop] @[simp] lemma split_on_nil {Ξ± : Type u} [decidable_eq Ξ±] (a : Ξ±) : [].split_on a = [[]] := rfl @[simp] lemma split_on_p_nil : [].split_on_p p = [[]] := rfl /-- An auxiliary definition for proving a specification lemma for `split_on_p`. `split_on_p_aux' P xs ys` splits the list `ys ++ xs` at every element satisfying `P`, where `ys` is an accumulating parameter for the initial segment of elements not satisfying `P`. -/ def split_on_p_aux' {Ξ± : Type u} (P : Ξ± β†’ Prop) [decidable_pred P] : list Ξ± β†’ list Ξ± β†’ list (list Ξ±) | [] xs := [xs] | (h :: t) xs := if P h then xs :: split_on_p_aux' t [] else split_on_p_aux' t (xs ++ [h]) lemma split_on_p_aux_eq : split_on_p_aux' p xs ys = split_on_p_aux p xs ((++) ys) := begin induction xs with a t ih generalizing ys; simp! only [append_nil, eq_self_iff_true, and_self], split_ifs; rw ih, { refine ⟨rfl, rfl⟩ }, { congr, ext, simp } end lemma split_on_p_aux_nil : split_on_p_aux p xs id = split_on_p_aux' p xs [] := by { rw split_on_p_aux_eq, refl } /-- The original list `L` can be recovered by joining the lists produced by `split_on_p p L`, interspersed with the elements `L.filter p`. -/ lemma split_on_p_spec (as : list Ξ±) : join (zip_with (++) (split_on_p p as) ((as.filter p).map (Ξ» x, [x]) ++ [[]])) = as := begin rw [split_on_p, split_on_p_aux_nil], suffices : βˆ€ xs, join (zip_with (++) (split_on_p_aux' p as xs) ((as.filter p).map(Ξ» x, [x]) ++ [[]])) = xs ++ as, { rw this, refl }, induction as; intro; simp! only [split_on_p_aux', append_nil], split_ifs; simp [zip_with, join, *], end lemma split_on_p_aux_ne_nil : split_on_p_aux p xs f β‰  [] := begin induction xs with _ _ ih generalizing f, { trivial, }, simp only [split_on_p_aux], split_ifs, { trivial, }, exact ih _, end lemma split_on_p_aux_spec : split_on_p_aux p xs f = (xs.split_on_p p).modify_head f := begin simp only [split_on_p], induction xs with hd tl ih generalizing f, { simp [split_on_p_aux], }, simp only [split_on_p_aux], split_ifs, { simp, }, rw [ih (Ξ» l, f (hd :: l)), ih (Ξ» l, id (hd :: l))], simp, end lemma split_on_p_ne_nil : xs.split_on_p p β‰  [] := split_on_p_aux_ne_nil _ _ id @[simp] lemma split_on_p_cons (x : Ξ±) (xs : list Ξ±) : (x :: xs).split_on_p p = if p x then [] :: xs.split_on_p p else (xs.split_on_p p).modify_head (cons x) := by { simp only [split_on_p, split_on_p_aux], split_ifs, { simp }, rw split_on_p_aux_spec, refl, } /-- If no element satisfies `p` in the list `xs`, then `xs.split_on_p p = [xs]` -/ lemma split_on_p_eq_single (h : βˆ€ x ∈ xs, Β¬p x) : xs.split_on_p p = [xs] := by { induction xs with hd tl ih, { refl, }, simp [h hd _, ih (Ξ» t ht, h t (or.inr ht))], } /-- When a list of the form `[...xs, sep, ...as]` is split on `p`, the first element is `xs`, assuming no element in `xs` satisfies `p` but `sep` does satisfy `p` -/ lemma split_on_p_first (h : βˆ€ x ∈ xs, Β¬p x) (sep : Ξ±) (hsep : p sep) (as : list Ξ±) : (xs ++ sep :: as).split_on_p p = xs :: as.split_on_p p := by { induction xs with hd tl ih, { simp [hsep], }, simp [h hd _, ih (Ξ» t ht, h t (or.inr ht))], } /-- `intercalate [x]` is the left inverse of `split_on x` -/ lemma intercalate_split_on (x : Ξ±) [decidable_eq Ξ±] : [x].intercalate (xs.split_on x) = xs := begin simp only [intercalate, split_on], induction xs with hd tl ih, { simp [join], }, simp only [split_on_p_cons], cases h' : split_on_p (=x) tl with hd' tl', { exact (split_on_p_ne_nil _ tl h').elim, }, rw h' at ih, split_ifs, { subst h, simp [ih, join], }, cases tl'; simpa [join] using ih, end /-- `split_on x` is the left inverse of `intercalate [x]`, on the domain consisting of each nonempty list of lists `ls` whose elements do not contain `x` -/ lemma split_on_intercalate [decidable_eq Ξ±] (x : Ξ±) (hx : βˆ€ l ∈ ls, x βˆ‰ l) (hls : ls β‰  []) : ([x].intercalate ls).split_on x = ls := begin simp only [intercalate], induction ls with hd tl ih, { contradiction, }, cases tl, { suffices : hd.split_on x = [hd], { simpa [join], }, refine split_on_p_eq_single _ _ _, intros y hy H, rw H at hy, refine hx hd _ hy, simp, }, { simp only [intersperse_cons_cons, singleton_append, join], specialize ih _ _, { intros l hl, apply hx l, simp at hl ⊒, tauto, }, { trivial, }, have := split_on_p_first (=x) hd _ x rfl _, { simp only [split_on] at ⊒ ih, rw this, rw ih, }, intros y hy H, rw H at hy, exact hx hd (or.inl rfl) hy, } end end split_at_on /-! ### map for partial functions -/ /-- Partial map. If `f : Ξ  a, p a β†’ Ξ²` is a partial function defined on `a : Ξ±` satisfying `p`, then `pmap f l h` is essentially the same as `map f l` but is defined only when all members of `l` satisfy `p`, using the proof to apply `f`. -/ @[simp] def pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) : Ξ  l : list Ξ±, (βˆ€ a ∈ l, p a) β†’ list Ξ² | [] H := [] | (a::l) H := f a (forall_mem_cons.1 H).1 :: pmap l (forall_mem_cons.1 H).2 /-- "Attach" the proof that the elements of `l` are in `l` to produce a new list with the same elements but in the type `{x // x ∈ l}`. -/ def attach (l : list Ξ±) : list {x // x ∈ l} := pmap subtype.mk l (Ξ» a, id) theorem sizeof_lt_sizeof_of_mem [has_sizeof Ξ±] {x : Ξ±} {l : list Ξ±} (hx : x ∈ l) : sizeof x < sizeof l := begin induction l with h t ih; cases hx, { rw hx, exact lt_add_of_lt_of_nonneg (lt_one_add _) (nat.zero_le _) }, { exact lt_add_of_pos_of_le (zero_lt_one_add _) (le_of_lt (ih hx)) } end @[simp] theorem pmap_eq_map (p : Ξ± β†’ Prop) (f : Ξ± β†’ Ξ²) (l : list Ξ±) (H) : @pmap _ _ p (Ξ» a _, f a) l H = map f l := by induction l; [refl, simp only [*, pmap, map]]; split; refl theorem pmap_congr {p q : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {g : Ξ  a, q a β†’ Ξ²} (l : list Ξ±) {H₁ Hβ‚‚} (h : βˆ€ (a ∈ l) h₁ hβ‚‚, f a h₁ = g a hβ‚‚) : pmap f l H₁ = pmap g l Hβ‚‚ := begin induction l with _ _ ih, { refl, }, { rw [pmap, pmap, h _ (mem_cons_self _ _), ih (Ξ» a ha, h a (mem_cons_of_mem _ ha))], }, end theorem map_pmap {p : Ξ± β†’ Prop} (g : Ξ² β†’ Ξ³) (f : Ξ  a, p a β†’ Ξ²) (l H) : map g (pmap f l H) = pmap (Ξ» a h, g (f a h)) l H := by induction l; [refl, simp only [*, pmap, map]]; split; refl theorem pmap_map {p : Ξ² β†’ Prop} (g : βˆ€ b, p b β†’ Ξ³) (f : Ξ± β†’ Ξ²) (l H) : pmap g (map f l) H = pmap (Ξ» a h, g (f a) h) l (Ξ» a h, H _ (mem_map_of_mem _ h)) := by induction l; [refl, simp only [*, pmap, map]]; split; refl theorem pmap_eq_map_attach {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (l H) : pmap f l H = l.attach.map (Ξ» x, f x.1 (H _ x.2)) := by rw [attach, map_pmap]; exact pmap_congr l (Ξ» _ _ _ _, rfl) @[simp] lemma attach_map_coe' (l : list Ξ±) (f : Ξ± β†’ Ξ²) : l.attach.map (Ξ» i, f i) = l.map f := by rw [attach, map_pmap]; exact (pmap_eq_map _ _ _ _) lemma attach_map_val' (l : list Ξ±) (f : Ξ± β†’ Ξ²) : l.attach.map (Ξ» i, f i.val) = l.map f := attach_map_coe' _ _ @[simp] lemma attach_map_coe (l : list Ξ±) : l.attach.map (coe : _ β†’ Ξ±) = l := (attach_map_coe' _ _).trans l.map_id lemma attach_map_val (l : list Ξ±) : l.attach.map subtype.val = l := attach_map_coe _ @[simp] theorem mem_attach (l : list Ξ±) : βˆ€ x, x ∈ l.attach | ⟨a, h⟩ := by have := mem_map.1 (by rw [attach_map_val]; exact h); { rcases this with ⟨⟨_, _⟩, m, rfl⟩, exact m } @[simp] theorem mem_pmap {p : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {l H b} : b ∈ pmap f l H ↔ βˆƒ a (h : a ∈ l), f a (H a h) = b := by simp only [pmap_eq_map_attach, mem_map, mem_attach, true_and, subtype.exists] @[simp] theorem length_pmap {p : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {l H} : length (pmap f l H) = length l := by induction l; [refl, simp only [*, pmap, length]] @[simp] lemma length_attach (L : list Ξ±) : L.attach.length = L.length := length_pmap @[simp] lemma pmap_eq_nil {p : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {l H} : pmap f l H = [] ↔ l = [] := by rw [← length_eq_zero, length_pmap, length_eq_zero] @[simp] lemma attach_eq_nil (l : list Ξ±) : l.attach = [] ↔ l = [] := pmap_eq_nil lemma last_pmap {Ξ± Ξ² : Type*} (p : Ξ± β†’ Prop) (f : Ξ  a, p a β†’ Ξ²) (l : list Ξ±) (hl₁ : βˆ€ a ∈ l, p a) (hlβ‚‚ : l β‰  []) : (l.pmap f hl₁).last (mt list.pmap_eq_nil.1 hlβ‚‚) = f (l.last hlβ‚‚) (hl₁ _ (list.last_mem hlβ‚‚)) := begin induction l with l_hd l_tl l_ih, { apply (hlβ‚‚ rfl).elim }, { cases l_tl, { simp }, { apply l_ih } } end lemma nth_pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) {l : list Ξ±} (h : βˆ€ a ∈ l, p a) (n : β„•) : nth (pmap f l h) n = option.pmap f (nth l n) (Ξ» x H, h x (nth_mem H)) := begin induction l with hd tl hl generalizing n, { simp }, { cases n; simp [hl] } end lemma nth_le_pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) {l : list Ξ±} (h : βˆ€ a ∈ l, p a) {n : β„•} (hn : n < (pmap f l h).length) : nth_le (pmap f l h) n hn = f (nth_le l n (@length_pmap _ _ p f l h β–Έ hn)) (h _ (nth_le_mem l n (@length_pmap _ _ p f l h β–Έ hn))) := begin induction l with hd tl hl generalizing n, { simp only [length, pmap] at hn, exact absurd hn (not_lt_of_le n.zero_le) }, { cases n, { simp }, { simpa [hl] } } end lemma pmap_append {p : ΞΉ β†’ Prop} (f : Ξ  (a : ΞΉ), p a β†’ Ξ±) (l₁ lβ‚‚ : list ΞΉ) (h : βˆ€ a ∈ l₁ ++ lβ‚‚, p a) : (l₁ ++ lβ‚‚).pmap f h = l₁.pmap f (Ξ» a ha, h a (mem_append_left lβ‚‚ ha)) ++ lβ‚‚.pmap f (Ξ» a ha, h a (mem_append_right l₁ ha)) := begin induction l₁ with _ _ ih, { refl, }, { dsimp only [pmap, cons_append], rw ih, } end lemma pmap_append' {Ξ± Ξ² : Type*} {p : Ξ± β†’ Prop} (f : Ξ  (a : Ξ±), p a β†’ Ξ²) (l₁ lβ‚‚ : list Ξ±) (h₁ : βˆ€ a ∈ l₁, p a) (hβ‚‚ : βˆ€ a ∈ lβ‚‚, p a) : (l₁ ++ lβ‚‚).pmap f (Ξ» a ha, (list.mem_append.1 ha).elim (h₁ a) (hβ‚‚ a)) = l₁.pmap f h₁ ++ lβ‚‚.pmap f hβ‚‚ := pmap_append f l₁ lβ‚‚ _ /-! ### find -/ section find variables {p : Ξ± β†’ Prop} [decidable_pred p] {l : list Ξ±} {a : Ξ±} @[simp] theorem find_nil (p : Ξ± β†’ Prop) [decidable_pred p] : find p [] = none := rfl @[simp] theorem find_cons_of_pos (l) (h : p a) : find p (a::l) = some a := if_pos h @[simp] theorem find_cons_of_neg (l) (h : Β¬ p a) : find p (a::l) = find p l := if_neg h @[simp] theorem find_eq_none : find p l = none ↔ βˆ€ x ∈ l, Β¬ p x := begin induction l with a l IH, { exact iff_of_true rfl (forall_mem_nil _) }, rw forall_mem_cons, by_cases h : p a, { simp only [find_cons_of_pos _ h, h, not_true, false_and] }, { rwa [find_cons_of_neg _ h, iff_true_intro h, true_and] } end theorem find_some (H : find p l = some a) : p a := begin induction l with b l IH, {contradiction}, by_cases h : p b, { rw find_cons_of_pos _ h at H, cases H, exact h }, { rw find_cons_of_neg _ h at H, exact IH H } end @[simp] theorem find_mem (H : find p l = some a) : a ∈ l := begin induction l with b l IH, {contradiction}, by_cases h : p b, { rw find_cons_of_pos _ h at H, cases H, apply mem_cons_self }, { rw find_cons_of_neg _ h at H, exact mem_cons_of_mem _ (IH H) } end end find /-! ### lookmap -/ section lookmap variables (f : Ξ± β†’ option Ξ±) @[simp] theorem lookmap_nil : [].lookmap f = [] := rfl @[simp] theorem lookmap_cons_none {a : Ξ±} (l : list Ξ±) (h : f a = none) : (a :: l).lookmap f = a :: l.lookmap f := by simp [lookmap, h] @[simp] theorem lookmap_cons_some {a b : Ξ±} (l : list Ξ±) (h : f a = some b) : (a :: l).lookmap f = b :: l := by simp [lookmap, h] theorem lookmap_some : βˆ€ l : list Ξ±, l.lookmap some = l | [] := rfl | (a::l) := rfl theorem lookmap_none : βˆ€ l : list Ξ±, l.lookmap (Ξ» _, none) = l | [] := rfl | (a::l) := congr_arg (cons a) (lookmap_none l) theorem lookmap_congr {f g : Ξ± β†’ option Ξ±} : βˆ€ {l : list Ξ±}, (βˆ€ a ∈ l, f a = g a) β†’ l.lookmap f = l.lookmap g | [] H := rfl | (a::l) H := begin cases forall_mem_cons.1 H with H₁ Hβ‚‚, cases h : g a with b, { simp [h, H₁.trans h, lookmap_congr Hβ‚‚] }, { simp [lookmap_cons_some _ _ h, lookmap_cons_some _ _ (H₁.trans h)] } end theorem lookmap_of_forall_not {l : list Ξ±} (H : βˆ€ a ∈ l, f a = none) : l.lookmap f = l := (lookmap_congr H).trans (lookmap_none l) theorem lookmap_map_eq (g : Ξ± β†’ Ξ²) (h : βˆ€ a (b ∈ f a), g a = g b) : βˆ€ l : list Ξ±, map g (l.lookmap f) = map g l | [] := rfl | (a::l) := begin cases h' : f a with b, { simp [h', lookmap_map_eq] }, { simp [lookmap_cons_some _ _ h', h _ _ h'] } end theorem lookmap_id' (h : βˆ€ a (b ∈ f a), a = b) (l : list Ξ±) : l.lookmap f = l := by rw [← map_id (l.lookmap f), lookmap_map_eq, map_id]; exact h theorem length_lookmap (l : list Ξ±) : length (l.lookmap f) = length l := by rw [← length_map, lookmap_map_eq _ (Ξ» _, ()), length_map]; simp end lookmap /-! ### filter_map -/ @[simp] theorem filter_map_nil (f : Ξ± β†’ option Ξ²) : filter_map f [] = [] := rfl @[simp] theorem filter_map_cons_none {f : Ξ± β†’ option Ξ²} (a : Ξ±) (l : list Ξ±) (h : f a = none) : filter_map f (a :: l) = filter_map f l := by simp only [filter_map, h] @[simp] theorem filter_map_cons_some (f : Ξ± β†’ option Ξ²) (a : Ξ±) (l : list Ξ±) {b : Ξ²} (h : f a = some b) : filter_map f (a :: l) = b :: filter_map f l := by simp only [filter_map, h]; split; refl theorem filter_map_cons (f : Ξ± β†’ option Ξ²) (a : Ξ±) (l : list Ξ±) : filter_map f (a :: l) = option.cases_on (f a) (filter_map f l) (Ξ»b, b :: filter_map f l) := begin generalize eq : f a = b, cases b, { rw filter_map_cons_none _ _ eq }, { rw filter_map_cons_some _ _ _ eq }, end lemma filter_map_append {Ξ± Ξ² : Type*} (l l' : list Ξ±) (f : Ξ± β†’ option Ξ²) : filter_map f (l ++ l') = filter_map f l ++ filter_map f l' := begin induction l with hd tl hl generalizing l', { simp }, { rw [cons_append, filter_map, filter_map], cases f hd; simp only [filter_map, hl, cons_append, eq_self_iff_true, and_self] } end theorem filter_map_eq_map (f : Ξ± β†’ Ξ²) : filter_map (some ∘ f) = map f := begin funext l, induction l with a l IH, {refl}, simp only [filter_map_cons_some (some ∘ f) _ _ rfl, IH, map_cons], split; refl end theorem filter_map_eq_filter (p : Ξ± β†’ Prop) [decidable_pred p] : filter_map (option.guard p) = filter p := begin funext l, induction l with a l IH, {refl}, by_cases pa : p a, { simp only [filter_map, option.guard, IH, if_pos pa, filter_cons_of_pos _ pa], split; refl }, { simp only [filter_map, option.guard, IH, if_neg pa, filter_cons_of_neg _ pa] } end theorem filter_map_filter_map (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ option Ξ³) (l : list Ξ±) : filter_map g (filter_map f l) = filter_map (Ξ» x, (f x).bind g) l := begin induction l with a l IH, {refl}, cases h : f a with b, { rw [filter_map_cons_none _ _ h, filter_map_cons_none, IH], simp only [h, option.none_bind'] }, rw filter_map_cons_some _ _ _ h, cases h' : g b with c; [ rw [filter_map_cons_none _ _ h', filter_map_cons_none, IH], rw [filter_map_cons_some _ _ _ h', filter_map_cons_some, IH] ]; simp only [h, h', option.some_bind'] end theorem map_filter_map (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ Ξ³) (l : list Ξ±) : map g (filter_map f l) = filter_map (Ξ» x, (f x).map g) l := by rw [← filter_map_eq_map, filter_map_filter_map]; refl theorem filter_map_map (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ option Ξ³) (l : list Ξ±) : filter_map g (map f l) = filter_map (g ∘ f) l := by rw [← filter_map_eq_map, filter_map_filter_map]; refl theorem filter_filter_map (f : Ξ± β†’ option Ξ²) (p : Ξ² β†’ Prop) [decidable_pred p] (l : list Ξ±) : filter p (filter_map f l) = filter_map (Ξ» x, (f x).filter p) l := by rw [← filter_map_eq_filter, filter_map_filter_map]; refl theorem filter_map_filter (p : Ξ± β†’ Prop) [decidable_pred p] (f : Ξ± β†’ option Ξ²) (l : list Ξ±) : filter_map f (filter p l) = filter_map (Ξ» x, if p x then f x else none) l := begin rw [← filter_map_eq_filter, filter_map_filter_map], congr, funext x, show (option.guard p x).bind f = ite (p x) (f x) none, by_cases h : p x, { simp only [option.guard, if_pos h, option.some_bind'] }, { simp only [option.guard, if_neg h, option.none_bind'] } end @[simp] theorem filter_map_some (l : list Ξ±) : filter_map some l = l := by rw filter_map_eq_map; apply map_id theorem map_filter_map_some_eq_filter_map_is_some (f : Ξ± β†’ option Ξ²) (l : list Ξ±) : (l.filter_map f).map some = (l.map f).filter (Ξ» b, b.is_some) := begin induction l with x xs ih, { simp }, { cases h : f x; rw [list.filter_map_cons, h]; simp [h, ih] }, end @[simp] theorem mem_filter_map (f : Ξ± β†’ option Ξ²) (l : list Ξ±) {b : Ξ²} : b ∈ filter_map f l ↔ βˆƒ a, a ∈ l ∧ f a = some b := begin induction l with a l IH, { split, { intro H, cases H }, { rintro ⟨_, H, _⟩, cases H } }, cases h : f a with b', { have : f a β‰  some b, {rw h, intro, contradiction}, simp only [filter_map_cons_none _ _ h, IH, mem_cons_iff, or_and_distrib_right, exists_or_distrib, exists_eq_left, this, false_or] }, { have : f a = some b ↔ b = b', { split; intro t, {rw t at h; injection h}, {exact t.symm β–Έ h} }, simp only [filter_map_cons_some _ _ _ h, IH, mem_cons_iff, or_and_distrib_right, exists_or_distrib, this, exists_eq_left] } end @[simp] theorem filter_map_join (f : Ξ± β†’ option Ξ²) (L : list (list Ξ±)) : filter_map f (join L) = join (map (filter_map f) L) := begin induction L with hd tl ih, { refl }, { rw [map, join, join, filter_map_append, ih] }, end theorem map_filter_map_of_inv (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ Ξ±) (H : βˆ€ x : Ξ±, (f x).map g = some x) (l : list Ξ±) : map g (filter_map f l) = l := by simp only [map_filter_map, H, filter_map_some] theorem length_filter_le (p : Ξ± β†’ Prop) [decidable_pred p] (l : list Ξ±) : (l.filter p).length ≀ l.length := (list.filter_sublist _).length_le theorem length_filter_map_le (f : Ξ± β†’ option Ξ²) (l : list Ξ±) : (list.filter_map f l).length ≀ l.length := begin rw [← list.length_map some, list.map_filter_map_some_eq_filter_map_is_some, ← list.length_map f], apply list.length_filter_le, end theorem sublist.filter_map (f : Ξ± β†’ option Ξ²) {l₁ lβ‚‚ : list Ξ±} (s : l₁ <+ lβ‚‚) : filter_map f l₁ <+ filter_map f lβ‚‚ := by induction s with l₁ lβ‚‚ a s IH l₁ lβ‚‚ a s IH; simp only [filter_map]; cases f a with b; simp only [filter_map, IH, sublist.cons, sublist.cons2] theorem sublist.map (f : Ξ± β†’ Ξ²) {l₁ lβ‚‚ : list Ξ±} (s : l₁ <+ lβ‚‚) : map f l₁ <+ map f lβ‚‚ := filter_map_eq_map f β–Έ s.filter_map _ /-! ### reduce_option -/ @[simp] lemma reduce_option_cons_of_some (x : Ξ±) (l : list (option Ξ±)) : reduce_option (some x :: l) = x :: l.reduce_option := by simp only [reduce_option, filter_map, id.def, eq_self_iff_true, and_self] @[simp] lemma reduce_option_cons_of_none (l : list (option Ξ±)) : reduce_option (none :: l) = l.reduce_option := by simp only [reduce_option, filter_map, id.def] @[simp] lemma reduce_option_nil : @reduce_option Ξ± [] = [] := rfl @[simp] lemma reduce_option_map {l : list (option Ξ±)} {f : Ξ± β†’ Ξ²} : reduce_option (map (option.map f) l) = map f (reduce_option l) := begin induction l with hd tl hl, { simp only [reduce_option_nil, map_nil] }, { cases hd; simpa only [true_and, option.map_some', map, eq_self_iff_true, reduce_option_cons_of_some] using hl }, end lemma reduce_option_append (l l' : list (option Ξ±)) : (l ++ l').reduce_option = l.reduce_option ++ l'.reduce_option := filter_map_append l l' id lemma reduce_option_length_le (l : list (option Ξ±)) : l.reduce_option.length ≀ l.length := begin induction l with hd tl hl, { simp only [reduce_option_nil, length] }, { cases hd, { exact nat.le_succ_of_le hl }, { simpa only [length, add_le_add_iff_right, reduce_option_cons_of_some] using hl} } end lemma reduce_option_length_eq_iff {l : list (option Ξ±)} : l.reduce_option.length = l.length ↔ βˆ€ x ∈ l, option.is_some x := begin induction l with hd tl hl, { simp only [forall_const, reduce_option_nil, not_mem_nil, forall_prop_of_false, eq_self_iff_true, length, not_false_iff] }, { cases hd, { simp only [mem_cons_iff, forall_eq_or_imp, bool.coe_sort_ff, false_and, reduce_option_cons_of_none, length, option.is_some_none, iff_false], intro H, have := reduce_option_length_le tl, rw H at this, exact absurd (nat.lt_succ_self _) (not_lt_of_le this) }, { simp only [hl, true_and, mem_cons_iff, forall_eq_or_imp, add_left_inj, bool.coe_sort_tt, length, option.is_some_some, reduce_option_cons_of_some] } } end lemma reduce_option_length_lt_iff {l : list (option Ξ±)} : l.reduce_option.length < l.length ↔ none ∈ l := begin rw [(reduce_option_length_le l).lt_iff_ne, ne, reduce_option_length_eq_iff], induction l; simp *, rw [eq_comm, ← option.not_is_some_iff_eq_none, decidable.imp_iff_not_or] end lemma reduce_option_singleton (x : option Ξ±) : [x].reduce_option = x.to_list := by cases x; refl lemma reduce_option_concat (l : list (option Ξ±)) (x : option Ξ±) : (l.concat x).reduce_option = l.reduce_option ++ x.to_list := begin induction l with hd tl hl generalizing x, { cases x; simp [option.to_list] }, { simp only [concat_eq_append, reduce_option_append] at hl, cases hd; simp [hl, reduce_option_append] } end lemma reduce_option_concat_of_some (l : list (option Ξ±)) (x : Ξ±) : (l.concat (some x)).reduce_option = l.reduce_option.concat x := by simp only [reduce_option_nil, concat_eq_append, reduce_option_append, reduce_option_cons_of_some] lemma reduce_option_mem_iff {l : list (option Ξ±)} {x : Ξ±} : x ∈ l.reduce_option ↔ (some x) ∈ l := by simp only [reduce_option, id.def, mem_filter_map, exists_eq_right] lemma reduce_option_nth_iff {l : list (option Ξ±)} {x : Ξ±} : (βˆƒ i, l.nth i = some (some x)) ↔ βˆƒ i, l.reduce_option.nth i = some x := by rw [←mem_iff_nth, ←mem_iff_nth, reduce_option_mem_iff] /-! ### filter -/ section filter variables {p : Ξ± β†’ Prop} [decidable_pred p] lemma filter_singleton {a : Ξ±} : [a].filter p = if p a then [a] else [] := rfl theorem filter_eq_foldr (p : Ξ± β†’ Prop) [decidable_pred p] (l : list Ξ±) : filter p l = foldr (Ξ» a out, if p a then a :: out else out) [] l := by induction l; simp [*, filter] lemma filter_congr' {p q : Ξ± β†’ Prop} [decidable_pred p] [decidable_pred q] : βˆ€ {l : list Ξ±}, (βˆ€ x ∈ l, p x ↔ q x) β†’ filter p l = filter q l | [] _ := rfl | (a::l) h := by rw forall_mem_cons at h; by_cases pa : p a; [simp only [filter_cons_of_pos _ pa, filter_cons_of_pos _ (h.1.1 pa), filter_congr' h.2], simp only [filter_cons_of_neg _ pa, filter_cons_of_neg _ (mt h.1.2 pa), filter_congr' h.2]]; split; refl @[simp] theorem filter_subset (l : list Ξ±) : filter p l βŠ† l := (filter_sublist l).subset theorem of_mem_filter {a : Ξ±} : βˆ€ {l}, a ∈ filter p l β†’ p a | (b::l) ain := if pb : p b then have a ∈ b :: filter p l, by simpa only [filter_cons_of_pos _ pb] using ain, or.elim (eq_or_mem_of_mem_cons this) (assume : a = b, begin rw [← this] at pb, exact pb end) (assume : a ∈ filter p l, of_mem_filter this) else begin simp only [filter_cons_of_neg _ pb] at ain, exact (of_mem_filter ain) end theorem mem_of_mem_filter {a : Ξ±} {l} (h : a ∈ filter p l) : a ∈ l := filter_subset l h theorem mem_filter_of_mem {a : Ξ±} : βˆ€ {l}, a ∈ l β†’ p a β†’ a ∈ filter p l | (_::l) (or.inl rfl) pa := by rw filter_cons_of_pos _ pa; apply mem_cons_self | (b::l) (or.inr ain) pa := if pb : p b then by rw [filter_cons_of_pos _ pb]; apply mem_cons_of_mem; apply mem_filter_of_mem ain pa else by rw [filter_cons_of_neg _ pb]; apply mem_filter_of_mem ain pa @[simp] theorem mem_filter {a : Ξ±} {l} : a ∈ filter p l ↔ a ∈ l ∧ p a := ⟨λ h, ⟨mem_of_mem_filter h, of_mem_filter h⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, mem_filter_of_mem h₁ hβ‚‚βŸ© lemma monotone_filter_left (p : Ξ± β†’ Prop) [decidable_pred p] ⦃l l' : list α⦄ (h : l βŠ† l') : filter p l βŠ† filter p l' := begin intros x hx, rw [mem_filter] at hx ⊒, exact ⟨h hx.left, hx.right⟩ end theorem filter_eq_self {l} : filter p l = l ↔ βˆ€ a ∈ l, p a := begin induction l with a l ih, { exact iff_of_true rfl (forall_mem_nil _) }, rw forall_mem_cons, by_cases p a, { rw [filter_cons_of_pos _ h, cons_inj, ih, and_iff_right h] }, { refine iff_of_false (Ξ» hl, h $ of_mem_filter (_ : a ∈ filter p (a :: l))) (mt and.left h), rw hl, exact mem_cons_self _ _ } end theorem filter_length_eq_length {l} : (filter p l).length = l.length ↔ βˆ€ a ∈ l, p a := iff.trans ⟨l.filter_sublist.eq_of_length, congr_arg list.length⟩ filter_eq_self theorem filter_eq_nil {l} : filter p l = [] ↔ βˆ€ a ∈ l, Β¬p a := by simp only [eq_nil_iff_forall_not_mem, mem_filter, not_and] variable (p) theorem sublist.filter {l₁ lβ‚‚} (s : l₁ <+ lβ‚‚) : filter p l₁ <+ filter p lβ‚‚ := filter_map_eq_filter p β–Έ s.filter_map _ lemma monotone_filter_right (l : list Ξ±) ⦃p q : Ξ± β†’ Prop⦄ [decidable_pred p] [decidable_pred q] (h : p ≀ q) : l.filter p <+ l.filter q := begin induction l with hd tl IH, { refl }, { by_cases hp : p hd, { rw [filter_cons_of_pos _ hp, filter_cons_of_pos _ (h _ hp)], exact IH.cons_cons hd }, { rw filter_cons_of_neg _ hp, by_cases hq : q hd, { rw filter_cons_of_pos _ hq, exact sublist_cons_of_sublist hd IH }, { rw filter_cons_of_neg _ hq, exact IH } } } end theorem map_filter (f : Ξ² β†’ Ξ±) (l : list Ξ²) : filter p (map f l) = map f (filter (p ∘ f) l) := by rw [← filter_map_eq_map, filter_filter_map, filter_map_filter]; refl @[simp] theorem filter_filter (q) [decidable_pred q] : βˆ€ l, filter p (filter q l) = filter (Ξ» a, p a ∧ q a) l | [] := rfl | (a :: l) := by by_cases hp : p a; by_cases hq : q a; simp only [hp, hq, filter, if_true, if_false, true_and, false_and, filter_filter l, eq_self_iff_true] @[simp] lemma filter_true {h : decidable_pred (Ξ» a : Ξ±, true)} (l : list Ξ±) : @filter Ξ± (Ξ» _, true) h l = l := by convert filter_eq_self.2 (Ξ» _ _, trivial) @[simp] lemma filter_false {h : decidable_pred (Ξ» a : Ξ±, false)} (l : list Ξ±) : @filter Ξ± (Ξ» _, false) h l = [] := by convert filter_eq_nil.2 (Ξ» _ _, id) @[simp] theorem span_eq_take_drop : βˆ€ (l : list Ξ±), span p l = (take_while p l, drop_while p l) | [] := rfl | (a::l) := if pa : p a then by simp only [span, if_pos pa, span_eq_take_drop l, take_while, drop_while] else by simp only [span, take_while, drop_while, if_neg pa] @[simp] theorem take_while_append_drop : βˆ€ (l : list Ξ±), take_while p l ++ drop_while p l = l | [] := rfl | (a::l) := if pa : p a then by rw [take_while, drop_while, if_pos pa, if_pos pa, cons_append, take_while_append_drop l] else by rw [take_while, drop_while, if_neg pa, if_neg pa, nil_append] lemma drop_while_nth_le_zero_not (l : list Ξ±) (hl : 0 < (l.drop_while p).length) : Β¬ p ((l.drop_while p).nth_le 0 hl) := begin induction l with hd tl IH, { cases hl }, { simp only [drop_while], split_ifs with hp, { exact IH _ }, { simpa using hp } } end variables {p} {l : list Ξ±} @[simp] lemma drop_while_eq_nil_iff : drop_while p l = [] ↔ βˆ€ x ∈ l, p x := begin induction l with x xs IH, { simp [drop_while] }, { by_cases hp : p x; simp [hp, drop_while, IH] } end @[simp] lemma take_while_eq_self_iff : take_while p l = l ↔ βˆ€ x ∈ l, p x := begin induction l with x xs IH, { simp [take_while] }, { by_cases hp : p x; simp [hp, take_while, IH] } end @[simp] lemma take_while_eq_nil_iff : take_while p l = [] ↔ βˆ€ (hl : 0 < l.length), Β¬ p (l.nth_le 0 hl) := begin induction l with x xs IH, { simp }, { by_cases hp : p x; simp [hp, take_while, IH] } end lemma mem_take_while_imp {x : Ξ±} (hx : x ∈ take_while p l) : p x := begin induction l with hd tl IH, { simpa [take_while] using hx }, { simp only [take_while] at hx, split_ifs at hx, { rw mem_cons_iff at hx, rcases hx with rfl|hx, { exact h }, { exact IH hx } }, { simpa using hx } } end lemma take_while_take_while (p q : Ξ± β†’ Prop) [decidable_pred p] [decidable_pred q] (l : list Ξ±) : take_while p (take_while q l) = take_while (Ξ» a, p a ∧ q a) l := begin induction l with hd tl IH, { simp [take_while] }, { by_cases hp : p hd; by_cases hq : q hd; simp [take_while, hp, hq, IH] } end lemma take_while_idem : take_while p (take_while p l) = take_while p l := by simp_rw [take_while_take_while, and_self] end filter /-! ### erasep -/ section erasep variables {p : Ξ± β†’ Prop} [decidable_pred p] @[simp] theorem erasep_nil : [].erasep p = [] := rfl theorem erasep_cons (a : Ξ±) (l : list Ξ±) : (a :: l).erasep p = if p a then l else a :: l.erasep p := rfl @[simp] theorem erasep_cons_of_pos {a : Ξ±} {l : list Ξ±} (h : p a) : (a :: l).erasep p = l := by simp [erasep_cons, h] @[simp] theorem erasep_cons_of_neg {a : Ξ±} {l : list Ξ±} (h : Β¬ p a) : (a::l).erasep p = a :: l.erasep p := by simp [erasep_cons, h] theorem erasep_of_forall_not {l : list Ξ±} (h : βˆ€ a ∈ l, Β¬ p a) : l.erasep p = l := by induction l with _ _ ih; [refl, simp [h _ (or.inl rfl), ih (forall_mem_of_forall_mem_cons h)]] theorem exists_of_erasep {l : list Ξ±} {a} (al : a ∈ l) (pa : p a) : βˆƒ a l₁ lβ‚‚, (βˆ€ b ∈ l₁, Β¬ p b) ∧ p a ∧ l = l₁ ++ a :: lβ‚‚ ∧ l.erasep p = l₁ ++ lβ‚‚ := begin induction l with b l IH, {cases al}, by_cases pb : p b, { exact ⟨b, [], l, forall_mem_nil _, pb, by simp [pb]⟩ }, { rcases al with rfl | al, {exact pb.elim pa}, rcases IH al with ⟨c, l₁, lβ‚‚, h₁, hβ‚‚, h₃, hβ‚„βŸ©, exact ⟨c, b::l₁, lβ‚‚, forall_mem_cons.2 ⟨pb, hβ‚βŸ©, hβ‚‚, by rw h₃; refl, by simp [pb, hβ‚„]⟩ } end theorem exists_or_eq_self_of_erasep (p : Ξ± β†’ Prop) [decidable_pred p] (l : list Ξ±) : l.erasep p = l ∨ βˆƒ a l₁ lβ‚‚, (βˆ€ b ∈ l₁, Β¬ p b) ∧ p a ∧ l = l₁ ++ a :: lβ‚‚ ∧ l.erasep p = l₁ ++ lβ‚‚ := begin by_cases h : βˆƒ a ∈ l, p a, { rcases h with ⟨a, ha, pa⟩, exact or.inr (exists_of_erasep ha pa) }, { simp at h, exact or.inl (erasep_of_forall_not h) } end @[simp] theorem length_erasep_of_mem {l : list Ξ±} {a} (al : a ∈ l) (pa : p a) : length (l.erasep p) = pred (length l) := by rcases exists_of_erasep al pa with ⟨_, l₁, lβ‚‚, _, _, e₁, eβ‚‚βŸ©; rw eβ‚‚; simp [-add_comm, e₁]; refl @[simp] lemma length_erasep_add_one {l : list Ξ±} {a} (al : a ∈ l) (pa : p a) : (l.erasep p).length + 1 = l.length := let ⟨_, l₁, lβ‚‚, _, _, h₁, hβ‚‚βŸ© := exists_of_erasep al pa in by { rw [hβ‚‚, h₁, length_append, length_append], refl } theorem erasep_append_left {a : Ξ±} (pa : p a) : βˆ€ {l₁ : list Ξ±} (lβ‚‚), a ∈ l₁ β†’ (l₁++lβ‚‚).erasep p = l₁.erasep p ++ lβ‚‚ | (x::xs) lβ‚‚ h := begin by_cases h' : p x; simp [h'], rw erasep_append_left lβ‚‚ (mem_of_ne_of_mem (mt _ h') h), rintro rfl, exact pa end theorem erasep_append_right : βˆ€ {l₁ : list Ξ±} (lβ‚‚), (βˆ€ b ∈ l₁, Β¬ p b) β†’ (l₁++lβ‚‚).erasep p = l₁ ++ lβ‚‚.erasep p | [] lβ‚‚ h := rfl | (x::xs) lβ‚‚ h := by simp [(forall_mem_cons.1 h).1, erasep_append_right _ (forall_mem_cons.1 h).2] theorem erasep_sublist (l : list Ξ±) : l.erasep p <+ l := by rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, lβ‚‚, h₁, hβ‚‚, h₃, hβ‚„βŸ©; [rw h, {rw [hβ‚„, h₃], simp}] theorem erasep_subset (l : list Ξ±) : l.erasep p βŠ† l := (erasep_sublist l).subset theorem sublist.erasep {l₁ lβ‚‚ : list Ξ±} (s : l₁ <+ lβ‚‚) : l₁.erasep p <+ lβ‚‚.erasep p := begin induction s, case list.sublist.slnil { refl }, case list.sublist.cons : l₁ lβ‚‚ a s IH { by_cases h : p a; simp [h], exacts [IH.trans (erasep_sublist _), IH.cons _ _ _] }, case list.sublist.cons2 : l₁ lβ‚‚ a s IH { by_cases h : p a; simp [h], exacts [s, IH.cons2 _ _ _] } end theorem mem_of_mem_erasep {a : Ξ±} {l : list Ξ±} : a ∈ l.erasep p β†’ a ∈ l := @erasep_subset _ _ _ _ _ @[simp] theorem mem_erasep_of_neg {a : Ξ±} {l : list Ξ±} (pa : Β¬ p a) : a ∈ l.erasep p ↔ a ∈ l := ⟨mem_of_mem_erasep, Ξ» al, begin rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, lβ‚‚, h₁, hβ‚‚, h₃, hβ‚„βŸ©, { rwa h }, { rw hβ‚„, rw h₃ at al, have : a β‰  c, {rintro rfl, exact pa.elim hβ‚‚}, simpa [this] using al } end⟩ theorem erasep_map (f : Ξ² β†’ Ξ±) : βˆ€ (l : list Ξ²), (map f l).erasep p = map f (l.erasep (p ∘ f)) | [] := rfl | (b::l) := by by_cases p (f b); simp [h, erasep_map l] @[simp] theorem extractp_eq_find_erasep : βˆ€ l : list Ξ±, extractp p l = (find p l, erasep p l) | [] := rfl | (a::l) := by by_cases pa : p a; simp [extractp, pa, extractp_eq_find_erasep l] end erasep /-! ### erase -/ section erase variable [decidable_eq Ξ±] @[simp] theorem erase_nil (a : Ξ±) : [].erase a = [] := rfl theorem erase_cons (a b : Ξ±) (l : list Ξ±) : (b :: l).erase a = if b = a then l else b :: l.erase a := rfl @[simp] theorem erase_cons_head (a : Ξ±) (l : list Ξ±) : (a :: l).erase a = l := by simp only [erase_cons, if_pos rfl] @[simp] theorem erase_cons_tail {a b : Ξ±} (l : list Ξ±) (h : b β‰  a) : (b::l).erase a = b :: l.erase a := by simp only [erase_cons, if_neg h]; split; refl theorem erase_eq_erasep (a : Ξ±) (l : list Ξ±) : l.erase a = l.erasep (eq a) := by { induction l with b l, {refl}, by_cases a = b; [simp [h], simp [h, ne.symm h, *]] } @[simp, priority 980] theorem erase_of_not_mem {a : Ξ±} {l : list Ξ±} (h : a βˆ‰ l) : l.erase a = l := by rw [erase_eq_erasep, erasep_of_forall_not]; rintro b h' rfl; exact h h' theorem exists_erase_eq {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : βˆƒ l₁ lβ‚‚, a βˆ‰ l₁ ∧ l = l₁ ++ a :: lβ‚‚ ∧ l.erase a = l₁ ++ lβ‚‚ := by rcases exists_of_erasep h rfl with ⟨_, l₁, lβ‚‚, h₁, rfl, hβ‚‚, hβ‚ƒβŸ©; rw erase_eq_erasep; exact ⟨l₁, lβ‚‚, Ξ» h, h₁ _ h rfl, hβ‚‚, hβ‚ƒβŸ© @[simp] theorem length_erase_of_mem {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : length (l.erase a) = pred (length l) := by rw erase_eq_erasep; exact length_erasep_of_mem h rfl @[simp] lemma length_erase_add_one {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : (l.erase a).length + 1 = l.length := by rw [erase_eq_erasep, length_erasep_add_one h rfl] theorem erase_append_left {a : Ξ±} {l₁ : list Ξ±} (lβ‚‚) (h : a ∈ l₁) : (l₁++lβ‚‚).erase a = l₁.erase a ++ lβ‚‚ := by simp [erase_eq_erasep]; exact erasep_append_left (by refl) lβ‚‚ h theorem erase_append_right {a : Ξ±} {l₁ : list Ξ±} (lβ‚‚) (h : a βˆ‰ l₁) : (l₁++lβ‚‚).erase a = l₁ ++ lβ‚‚.erase a := by rw [erase_eq_erasep, erase_eq_erasep, erasep_append_right]; rintro b h' rfl; exact h h' theorem erase_sublist (a : Ξ±) (l : list Ξ±) : l.erase a <+ l := by rw erase_eq_erasep; apply erasep_sublist theorem erase_subset (a : Ξ±) (l : list Ξ±) : l.erase a βŠ† l := (erase_sublist a l).subset theorem sublist.erase (a : Ξ±) {l₁ lβ‚‚ : list Ξ±} (h : l₁ <+ lβ‚‚) : l₁.erase a <+ lβ‚‚.erase a := by simp [erase_eq_erasep]; exact sublist.erasep h theorem mem_of_mem_erase {a b : Ξ±} {l : list Ξ±} : a ∈ l.erase b β†’ a ∈ l := @erase_subset _ _ _ _ _ @[simp] theorem mem_erase_of_ne {a b : Ξ±} {l : list Ξ±} (ab : a β‰  b) : a ∈ l.erase b ↔ a ∈ l := by rw erase_eq_erasep; exact mem_erasep_of_neg ab.symm theorem erase_comm (a b : Ξ±) (l : list Ξ±) : (l.erase a).erase b = (l.erase b).erase a := if ab : a = b then by rw ab else if ha : a ∈ l then if hb : b ∈ l then match l, l.erase a, exists_erase_eq ha, hb with | ._, ._, ⟨l₁, lβ‚‚, ha', rfl, rfl⟩, hb := if h₁ : b ∈ l₁ then by rw [erase_append_left _ h₁, erase_append_left _ h₁, erase_append_right _ (mt mem_of_mem_erase ha'), erase_cons_head] else by rw [erase_append_right _ h₁, erase_append_right _ h₁, erase_append_right _ ha', erase_cons_tail _ ab, erase_cons_head] end else by simp only [erase_of_not_mem hb, erase_of_not_mem (mt mem_of_mem_erase hb)] else by simp only [erase_of_not_mem ha, erase_of_not_mem (mt mem_of_mem_erase ha)] theorem map_erase [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (finj : injective f) {a : Ξ±} (l : list Ξ±) : map f (l.erase a) = (map f l).erase (f a) := have this : eq a = eq (f a) ∘ f, { ext b, simp [finj.eq_iff] }, by simp [erase_eq_erasep, erase_eq_erasep, erasep_map, this] theorem map_foldl_erase [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (finj : injective f) {l₁ lβ‚‚ : list Ξ±} : map f (foldl list.erase l₁ lβ‚‚) = foldl (Ξ» l a, l.erase (f a)) (map f l₁) lβ‚‚ := by induction lβ‚‚ generalizing l₁; [refl, simp only [foldl_cons, map_erase finj, *]] end erase /-! ### diff -/ section diff variable [decidable_eq Ξ±] @[simp] theorem diff_nil (l : list Ξ±) : l.diff [] = l := rfl @[simp] theorem diff_cons (l₁ lβ‚‚ : list Ξ±) (a : Ξ±) : l₁.diff (a::lβ‚‚) = (l₁.erase a).diff lβ‚‚ := if h : a ∈ l₁ then by simp only [list.diff, if_pos h] else by simp only [list.diff, if_neg h, erase_of_not_mem h] lemma diff_cons_right (l₁ lβ‚‚ : list Ξ±) (a : Ξ±) : l₁.diff (a::lβ‚‚) = (l₁.diff lβ‚‚).erase a := begin induction lβ‚‚ with b lβ‚‚ ih generalizing l₁ a, { simp_rw [diff_cons, diff_nil] }, { rw [diff_cons, diff_cons, erase_comm, ← diff_cons, ih, ← diff_cons] } end lemma diff_erase (l₁ lβ‚‚ : list Ξ±) (a : Ξ±) : (l₁.diff lβ‚‚).erase a = (l₁.erase a).diff lβ‚‚ := by rw [← diff_cons_right, diff_cons] @[simp] theorem nil_diff (l : list Ξ±) : [].diff l = [] := by induction l; [refl, simp only [*, diff_cons, erase_of_not_mem (not_mem_nil _)]] lemma cons_diff (a : Ξ±) (l₁ lβ‚‚ : list Ξ±) : (a :: l₁).diff lβ‚‚ = if a ∈ lβ‚‚ then l₁.diff (lβ‚‚.erase a) else a :: l₁.diff lβ‚‚ := begin induction lβ‚‚ with b lβ‚‚ ih, { refl }, rcases eq_or_ne a b with rfl|hne, { simp }, { simp only [mem_cons_iff, *, false_or, diff_cons_right], split_ifs with hβ‚‚; simp [diff_erase, list.erase, hne, hne.symm] } end lemma cons_diff_of_mem {a : Ξ±} {lβ‚‚ : list Ξ±} (h : a ∈ lβ‚‚) (l₁ : list Ξ±) : (a :: l₁).diff lβ‚‚ = l₁.diff (lβ‚‚.erase a) := by rw [cons_diff, if_pos h] lemma cons_diff_of_not_mem {a : Ξ±} {lβ‚‚ : list Ξ±} (h : a βˆ‰ lβ‚‚) (l₁ : list Ξ±) : (a :: l₁).diff lβ‚‚ = a :: l₁.diff lβ‚‚ := by rw [cons_diff, if_neg h] theorem diff_eq_foldl : βˆ€ (l₁ lβ‚‚ : list Ξ±), l₁.diff lβ‚‚ = foldl list.erase l₁ lβ‚‚ | l₁ [] := rfl | l₁ (a::lβ‚‚) := (diff_cons l₁ lβ‚‚ a).trans (diff_eq_foldl _ _) @[simp] theorem diff_append (l₁ lβ‚‚ l₃ : list Ξ±) : l₁.diff (lβ‚‚ ++ l₃) = (l₁.diff lβ‚‚).diff l₃ := by simp only [diff_eq_foldl, foldl_append] @[simp] theorem map_diff [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (finj : injective f) {l₁ lβ‚‚ : list Ξ±} : map f (l₁.diff lβ‚‚) = (map f l₁).diff (map f lβ‚‚) := by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj] theorem diff_sublist : βˆ€ l₁ lβ‚‚ : list Ξ±, l₁.diff lβ‚‚ <+ l₁ | l₁ [] := sublist.refl _ | l₁ (a::lβ‚‚) := calc l₁.diff (a :: lβ‚‚) = (l₁.erase a).diff lβ‚‚ : diff_cons _ _ _ ... <+ l₁.erase a : diff_sublist _ _ ... <+ l₁ : list.erase_sublist _ _ theorem diff_subset (l₁ lβ‚‚ : list Ξ±) : l₁.diff lβ‚‚ βŠ† l₁ := (diff_sublist _ _).subset theorem mem_diff_of_mem {a : Ξ±} : βˆ€ {l₁ lβ‚‚ : list Ξ±}, a ∈ l₁ β†’ a βˆ‰ lβ‚‚ β†’ a ∈ l₁.diff lβ‚‚ | l₁ [] h₁ hβ‚‚ := h₁ | l₁ (b::lβ‚‚) h₁ hβ‚‚ := by rw diff_cons; exact mem_diff_of_mem ((mem_erase_of_ne (ne_of_not_mem_cons hβ‚‚)).2 h₁) (not_mem_of_not_mem_cons hβ‚‚) theorem sublist.diff_right : βˆ€ {l₁ lβ‚‚ l₃: list Ξ±}, l₁ <+ lβ‚‚ β†’ l₁.diff l₃ <+ lβ‚‚.diff l₃ | l₁ lβ‚‚ [] h := h | l₁ lβ‚‚ (a::l₃) h := by simp only [diff_cons, (h.erase _).diff_right] theorem erase_diff_erase_sublist_of_sublist {a : Ξ±} : βˆ€ {l₁ lβ‚‚ : list Ξ±}, l₁ <+ lβ‚‚ β†’ (lβ‚‚.erase a).diff (l₁.erase a) <+ lβ‚‚.diff l₁ | [] lβ‚‚ h := erase_sublist _ _ | (b::l₁) lβ‚‚ h := if heq : b = a then by simp only [heq, erase_cons_head, diff_cons] else by simpa only [erase_cons_head, erase_cons_tail _ heq, diff_cons, erase_comm a b lβ‚‚] using erase_diff_erase_sublist_of_sublist (h.erase b) end diff /-! ### enum -/ theorem length_enum_from : βˆ€ n (l : list Ξ±), length (enum_from n l) = length l | n [] := rfl | n (a::l) := congr_arg nat.succ (length_enum_from _ _) theorem length_enum : βˆ€ (l : list Ξ±), length (enum l) = length l := length_enum_from _ @[simp] theorem enum_from_nth : βˆ€ n (l : list Ξ±) m, nth (enum_from n l) m = (Ξ» a, (n + m, a)) <$> nth l m | n [] m := rfl | n (a :: l) 0 := rfl | n (a :: l) (m+1) := (enum_from_nth (n+1) l m).trans $ by rw [add_right_comm]; refl @[simp] theorem enum_nth : βˆ€ (l : list Ξ±) n, nth (enum l) n = (Ξ» a, (n, a)) <$> nth l n := by simp only [enum, enum_from_nth, zero_add]; intros; refl @[simp] theorem enum_from_map_snd : βˆ€ n (l : list Ξ±), map prod.snd (enum_from n l) = l | n [] := rfl | n (a :: l) := congr_arg (cons _) (enum_from_map_snd _ _) @[simp] theorem enum_map_snd : βˆ€ (l : list Ξ±), map prod.snd (enum l) = l := enum_from_map_snd _ theorem mem_enum_from {x : Ξ±} {i : β„•} : βˆ€ {j : β„•} (xs : list Ξ±), (i, x) ∈ xs.enum_from j β†’ j ≀ i ∧ i < j + xs.length ∧ x ∈ xs | j [] := by simp [enum_from] | j (y :: ys) := suffices i = j ∧ x = y ∨ (i, x) ∈ enum_from (j + 1) ys β†’ j ≀ i ∧ i < j + (length ys + 1) ∧ (x = y ∨ x ∈ ys), by simpa [enum_from, mem_enum_from ys], begin rintro (h|h), { refine ⟨le_of_eq h.1.symm,h.1 β–Έ _,or.inl h.2⟩, apply nat.lt_add_of_pos_right; simp }, { obtain ⟨hji, hijlen, hmem⟩ := mem_enum_from _ h, refine ⟨_, _, _⟩, { exact le_trans (nat.le_succ _) hji }, { convert hijlen using 1, ac_refl }, { simp [hmem] } } end @[simp] lemma enum_nil : enum ([] : list Ξ±) = [] := rfl @[simp] lemma enum_from_nil (n : β„•) : enum_from n ([] : list Ξ±) = [] := rfl @[simp] lemma enum_from_cons (x : Ξ±) (xs : list Ξ±) (n : β„•) : enum_from n (x :: xs) = (n, x) :: enum_from (n + 1) xs := rfl @[simp] lemma enum_cons (x : Ξ±) (xs : list Ξ±) : enum (x :: xs) = (0, x) :: enum_from 1 xs := rfl @[simp] lemma enum_from_singleton (x : Ξ±) (n : β„•) : enum_from n [x] = [(n, x)] := rfl @[simp] lemma enum_singleton (x : Ξ±) : enum [x] = [(0, x)] := rfl lemma enum_from_append (xs ys : list Ξ±) (n : β„•) : enum_from n (xs ++ ys) = enum_from n xs ++ enum_from (n + xs.length) ys := begin induction xs with x xs IH generalizing ys n, { simp }, { rw [cons_append, enum_from_cons, IH, ←cons_append, ←enum_from_cons, length, add_right_comm, add_assoc] } end lemma enum_append (xs ys : list Ξ±) : enum (xs ++ ys) = enum xs ++ enum_from xs.length ys := by simp [enum, enum_from_append] lemma map_fst_add_enum_from_eq_enum_from (l : list Ξ±) (n k : β„•) : map (prod.map (+ n) id) (enum_from k l) = enum_from (n + k) l := begin induction l with hd tl IH generalizing n k, { simp [enum_from] }, { simp only [enum_from, map, zero_add, prod.map_mk, id.def, eq_self_iff_true, true_and], simp [IH, add_comm n k, add_assoc, add_left_comm] } end lemma map_fst_add_enum_eq_enum_from (l : list Ξ±) (n : β„•) : map (prod.map (+ n) id) (enum l) = enum_from n l := map_fst_add_enum_from_eq_enum_from l _ _ lemma nth_le_enum_from (l : list Ξ±) (n i : β„•) (hi' : i < (l.enum_from n).length) (hi : i < l.length := by simpa [length_enum_from] using hi') : (l.enum_from n).nth_le i hi' = (n + i, l.nth_le i hi) := begin rw [←option.some_inj, ←nth_le_nth], simp [enum_from_nth, nth_le_nth hi] end lemma nth_le_enum (l : list Ξ±) (i : β„•) (hi' : i < l.enum.length) (hi : i < l.length := by simpa [length_enum] using hi') : l.enum.nth_le i hi' = (i, l.nth_le i hi) := by { convert nth_le_enum_from _ _ _ hi', exact (zero_add _).symm } section choose variables (p : Ξ± β†’ Prop) [decidable_pred p] (l : list Ξ±) lemma choose_spec (hp : βˆƒ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : βˆƒ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : βˆƒ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose /-! ### mapβ‚‚_left' -/ section mapβ‚‚_left' -- The definitional equalities for `mapβ‚‚_left'` can already be used by the -- simplifie because `mapβ‚‚_left'` is marked `@[simp]`. @[simp] theorem mapβ‚‚_left'_nil_right (f : Ξ± β†’ option Ξ² β†’ Ξ³) (as) : mapβ‚‚_left' f as [] = (as.map (Ξ» a, f a none), []) := by cases as; refl end mapβ‚‚_left' /-! ### mapβ‚‚_right' -/ section mapβ‚‚_right' variables (f : option Ξ± β†’ Ξ² β†’ Ξ³) (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem mapβ‚‚_right'_nil_left : mapβ‚‚_right' f [] bs = (bs.map (f none), []) := by cases bs; refl @[simp] theorem mapβ‚‚_right'_nil_right : mapβ‚‚_right' f as [] = ([], as) := rfl @[simp] theorem mapβ‚‚_right'_nil_cons : mapβ‚‚_right' f [] (b :: bs) = (f none b :: bs.map (f none), []) := rfl @[simp] theorem mapβ‚‚_right'_cons_cons : mapβ‚‚_right' f (a :: as) (b :: bs) = let rec := mapβ‚‚_right' f as bs in (f (some a) b :: rec.fst, rec.snd) := rfl end mapβ‚‚_right' /-! ### zip_left' -/ section zip_left' variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem zip_left'_nil_right : zip_left' as ([] : list Ξ²) = (as.map (Ξ» a, (a, none)), []) := by cases as; refl @[simp] theorem zip_left'_nil_left : zip_left' ([] : list Ξ±) bs = ([], bs) := rfl @[simp] theorem zip_left'_cons_nil : zip_left' (a :: as) ([] : list Ξ²) = ((a, none) :: as.map (Ξ» a, (a, none)), []) := rfl @[simp] theorem zip_left'_cons_cons : zip_left' (a :: as) (b :: bs) = let rec := zip_left' as bs in ((a, some b) :: rec.fst, rec.snd) := rfl end zip_left' /-! ### zip_right' -/ section zip_right' variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem zip_right'_nil_left : zip_right' ([] : list Ξ±) bs = (bs.map (Ξ» b, (none, b)), []) := by cases bs; refl @[simp] theorem zip_right'_nil_right : zip_right' as ([] : list Ξ²) = ([], as) := rfl @[simp] theorem zip_right'_nil_cons : zip_right' ([] : list Ξ±) (b :: bs) = ((none, b) :: bs.map (Ξ» b, (none, b)), []) := rfl @[simp] theorem zip_right'_cons_cons : zip_right' (a :: as) (b :: bs) = let rec := zip_right' as bs in ((some a, b) :: rec.fst, rec.snd) := rfl end zip_right' /-! ### mapβ‚‚_left -/ section mapβ‚‚_left variables (f : Ξ± β†’ option Ξ² β†’ Ξ³) (as : list Ξ±) -- The definitional equalities for `mapβ‚‚_left` can already be used by the -- simplifier because `mapβ‚‚_left` is marked `@[simp]`. @[simp] theorem mapβ‚‚_left_nil_right : mapβ‚‚_left f as [] = as.map (Ξ» a, f a none) := by cases as; refl theorem mapβ‚‚_left_eq_mapβ‚‚_left' : βˆ€ as bs, mapβ‚‚_left f as bs = (mapβ‚‚_left' f as bs).fst | [] bs := by simp! | (a :: as) [] := by simp! | (a :: as) (b :: bs) := by simp! [*] theorem mapβ‚‚_left_eq_mapβ‚‚ : βˆ€ as bs, length as ≀ length bs β†’ mapβ‚‚_left f as bs = mapβ‚‚ (Ξ» a b, f a (some b)) as bs | [] [] h := by simp! | [] (b :: bs) h := by simp! | (a :: as) [] h := by { simp at h, contradiction } | (a :: as) (b :: bs) h := by { simp at h, simp! [*] } end mapβ‚‚_left /-! ### mapβ‚‚_right -/ section mapβ‚‚_right variables (f : option Ξ± β†’ Ξ² β†’ Ξ³) (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem mapβ‚‚_right_nil_left : mapβ‚‚_right f [] bs = bs.map (f none) := by cases bs; refl @[simp] theorem mapβ‚‚_right_nil_right : mapβ‚‚_right f as [] = [] := rfl @[simp] theorem mapβ‚‚_right_nil_cons : mapβ‚‚_right f [] (b :: bs) = f none b :: bs.map (f none) := rfl @[simp] theorem mapβ‚‚_right_cons_cons : mapβ‚‚_right f (a :: as) (b :: bs) = f (some a) b :: mapβ‚‚_right f as bs := rfl theorem mapβ‚‚_right_eq_mapβ‚‚_right' : mapβ‚‚_right f as bs = (mapβ‚‚_right' f as bs).fst := by simp only [mapβ‚‚_right, mapβ‚‚_right', mapβ‚‚_left_eq_mapβ‚‚_left'] theorem mapβ‚‚_right_eq_mapβ‚‚ (h : length bs ≀ length as) : mapβ‚‚_right f as bs = mapβ‚‚ (Ξ» a b, f (some a) b) as bs := begin have : (Ξ» a b, flip f a (some b)) = (flip (Ξ» a b, f (some a) b)) := rfl, simp only [mapβ‚‚_right, mapβ‚‚_left_eq_mapβ‚‚, mapβ‚‚_flip, *] end end mapβ‚‚_right /-! ### zip_left -/ section zip_left variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem zip_left_nil_right : zip_left as ([] : list Ξ²) = as.map (Ξ» a, (a, none)) := by cases as; refl @[simp] theorem zip_left_nil_left : zip_left ([] : list Ξ±) bs = [] := rfl @[simp] theorem zip_left_cons_nil : zip_left (a :: as) ([] : list Ξ²) = (a, none) :: as.map (Ξ» a, (a, none)) := rfl @[simp] theorem zip_left_cons_cons : zip_left (a :: as) (b :: bs) = (a, some b) :: zip_left as bs := rfl theorem zip_left_eq_zip_left' : zip_left as bs = (zip_left' as bs).fst := by simp only [zip_left, zip_left', mapβ‚‚_left_eq_mapβ‚‚_left'] end zip_left /-! ### zip_right -/ section zip_right variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem zip_right_nil_left : zip_right ([] : list Ξ±) bs = bs.map (Ξ» b, (none, b)) := by cases bs; refl @[simp] theorem zip_right_nil_right : zip_right as ([] : list Ξ²) = [] := rfl @[simp] theorem zip_right_nil_cons : zip_right ([] : list Ξ±) (b :: bs) = (none, b) :: bs.map (Ξ» b, (none, b)) := rfl @[simp] theorem zip_right_cons_cons : zip_right (a :: as) (b :: bs) = (some a, b) :: zip_right as bs := rfl theorem zip_right_eq_zip_right' : zip_right as bs = (zip_right' as bs).fst := by simp only [zip_right, zip_right', mapβ‚‚_right_eq_mapβ‚‚_right'] end zip_right /-! ### to_chunks -/ section to_chunks @[simp] theorem to_chunks_nil (n) : @to_chunks Ξ± n [] = [] := by cases n; refl theorem to_chunks_aux_eq (n) : βˆ€ xs i, @to_chunks_aux Ξ± n xs i = (xs.take i, (xs.drop i).to_chunks (n+1)) | [] i := by cases i; refl | (x::xs) 0 := by rw [to_chunks_aux, drop, to_chunks]; cases to_chunks_aux n xs n; refl | (x::xs) (i+1) := by rw [to_chunks_aux, to_chunks_aux_eq]; refl theorem to_chunks_eq_cons' (n) : βˆ€ {xs : list Ξ±} (h : xs β‰  []), xs.to_chunks (n+1) = xs.take (n+1) :: (xs.drop (n+1)).to_chunks (n+1) | [] e := (e rfl).elim | (x::xs) _ := by rw [to_chunks, to_chunks_aux_eq]; refl theorem to_chunks_eq_cons : βˆ€ {n} {xs : list Ξ±} (n0 : n β‰  0) (x0 : xs β‰  []), xs.to_chunks n = xs.take n :: (xs.drop n).to_chunks n | 0 _ e := (e rfl).elim | (n+1) xs _ := to_chunks_eq_cons' _ theorem to_chunks_aux_join {n} : βˆ€ {xs i l L}, @to_chunks_aux Ξ± n xs i = (l, L) β†’ l ++ L.join = xs | [] _ _ _ rfl := rfl | (x::xs) i l L e := begin cases i; [ cases e' : to_chunks_aux n xs n with l L, cases e' : to_chunks_aux n xs i with l L]; { rw [to_chunks_aux, e', to_chunks_aux] at e, cases e, exact (congr_arg (cons x) (to_chunks_aux_join e') : _) } end @[simp] theorem to_chunks_join : βˆ€ n xs, (@to_chunks Ξ± n xs).join = xs | n [] := by cases n; refl | 0 (x::xs) := by simp only [to_chunks, join]; rw append_nil | (n+1) (x::xs) := begin rw to_chunks, cases e : to_chunks_aux n xs n with l L, exact (congr_arg (cons x) (to_chunks_aux_join e) : _), end theorem to_chunks_length_le : βˆ€ n xs, n β‰  0 β†’ βˆ€ l : list Ξ±, l ∈ @to_chunks Ξ± n xs β†’ l.length ≀ n | 0 _ e _ := (e rfl).elim | (n+1) xs _ l := begin refine (measure_wf length).induction xs _, intros xs IH h, by_cases x0 : xs = [], {subst xs, cases h}, rw to_chunks_eq_cons' _ x0 at h, rcases h with rfl|h, { apply length_take_le }, { refine IH _ _ h, simp only [measure, inv_image, length_drop], exact tsub_lt_self (length_pos_iff_ne_nil.2 x0) (succ_pos _) }, end end to_chunks /-! ### allβ‚‚ -/ section allβ‚‚ variables {p q : Ξ± β†’ Prop} {l : list Ξ±} @[simp] lemma allβ‚‚_cons (p : Ξ± β†’ Prop) (x : Ξ±) : βˆ€ (l : list Ξ±), allβ‚‚ p (x :: l) ↔ p x ∧ allβ‚‚ p l | [] := (and_true _).symm | (x :: l) := iff.rfl lemma allβ‚‚_iff_forall : βˆ€ {l : list Ξ±}, allβ‚‚ p l ↔ βˆ€ x ∈ l, p x | [] := (iff_true_intro $ ball_nil _).symm | (x :: l) := by rw [ball_cons, allβ‚‚_cons, allβ‚‚_iff_forall] lemma allβ‚‚.imp (h : βˆ€ x, p x β†’ q x) : βˆ€ {l : list Ξ±}, allβ‚‚ p l β†’ allβ‚‚ q l | [] := id | (x :: l) := by simpa using and.imp (h x) allβ‚‚.imp @[simp] lemma allβ‚‚_map_iff {p : Ξ² β†’ Prop} (f : Ξ± β†’ Ξ²) : allβ‚‚ p (l.map f) ↔ allβ‚‚ (p ∘ f) l := by induction l; simp * instance (p : Ξ± β†’ Prop) [decidable_pred p] : decidable_pred (allβ‚‚ p) := Ξ» l, decidable_of_iff' _ allβ‚‚_iff_forall end allβ‚‚ /-! ### Retroattributes The list definitions happen earlier than `to_additive`, so here we tag the few multiplicative definitions that couldn't be tagged earlier. -/ attribute [to_additive] list.prod -- `list.sum` attribute [to_additive] alternating_prod -- `list.alternating_sum` /-! ### Miscellaneous lemmas -/ lemma last_reverse {l : list Ξ±} (hl : l.reverse β‰  []) (hl' : 0 < l.length := by { contrapose! hl, simpa [length_eq_zero] using hl }) : l.reverse.last hl = l.nth_le 0 hl' := begin rw [last_eq_nth_le, nth_le_reverse'], { simp, }, { simpa using hl' } end theorem ilast'_mem : βˆ€ a l, @ilast' Ξ± a l ∈ a :: l | a [] := or.inl rfl | a (b::l) := or.inr (ilast'_mem b l) @[simp] lemma nth_le_attach (L : list Ξ±) (i) (H : i < L.attach.length) : (L.attach.nth_le i H).1 = L.nth_le i (length_attach L β–Έ H) := calc (L.attach.nth_le i H).1 = (L.attach.map subtype.val).nth_le i (by simpa using H) : by rw nth_le_map' ... = L.nth_le i _ : by congr; apply attach_map_val @[simp] theorem mem_map_swap (x : Ξ±) (y : Ξ²) (xs : list (Ξ± Γ— Ξ²)) : (y, x) ∈ map prod.swap xs ↔ (x, y) ∈ xs := begin induction xs with x xs, { simp only [not_mem_nil, map_nil] }, { cases x with a b, simp only [mem_cons_iff, prod.mk.inj_iff, map, prod.swap_prod_mk, prod.exists, xs_ih, and_comm] }, end lemma slice_eq (xs : list Ξ±) (n m : β„•) : slice n m xs = xs.take n ++ xs.drop (n+m) := begin induction n generalizing xs, { simp [slice] }, { cases xs; simp [slice, *, nat.succ_add], } end lemma sizeof_slice_lt [has_sizeof Ξ±] (i j : β„•) (hj : 0 < j) (xs : list Ξ±) (hi : i < xs.length) : sizeof (list.slice i j xs) < sizeof xs := begin induction xs generalizing i j, case list.nil : i j h { cases hi }, case list.cons : x xs xs_ih i j h { cases i; simp only [-slice_eq, list.slice], { cases j, cases h, dsimp only [drop], unfold_wf, apply @lt_of_le_of_lt _ _ _ xs.sizeof, { clear_except, induction xs generalizing j; unfold_wf, case list.nil : j { refl }, case list.cons : xs_hd xs_tl xs_ih j { cases j; unfold_wf, refl, transitivity, apply xs_ih, simp }, }, unfold_wf, }, { unfold_wf, apply xs_ih _ _ h, apply lt_of_succ_lt_succ hi, } }, end /-! ### nthd and inth -/ section nthd variables (l : list Ξ±) (x : Ξ±) (xs : list Ξ±) (d : Ξ±) (n : β„•) @[simp] lemma nthd_nil : nthd d [] n = d := rfl @[simp] lemma nthd_cons_zero : nthd d (x::xs) 0 = x := rfl @[simp] lemma nthd_cons_succ : nthd d (x::xs) (n + 1) = nthd d xs n := rfl lemma nthd_eq_nth_le {n : β„•} (hn : n < l.length) : l.nthd d n = l.nth_le n hn := begin induction l with hd tl IH generalizing n, { exact absurd hn (not_lt_of_ge (nat.zero_le _)) }, { cases n, { exact nthd_cons_zero _ _ _ }, { exact IH _ } } end lemma nthd_eq_default {n : β„•} (hn : l.length ≀ n) : l.nthd d n = d := begin induction l with hd tl IH generalizing n, { exact nthd_nil _ _ }, { cases n, { refine absurd (nat.zero_lt_succ _) (not_lt_of_ge hn) }, { exact IH (nat.le_of_succ_le_succ hn) } } end /-- An empty list can always be decidably checked for the presence of an element. Not an instance because it would clash with `decidable_eq Ξ±`. -/ def decidable_nthd_nil_ne {Ξ±} (a : Ξ±) : decidable_pred (Ξ» (i : β„•), nthd a ([] : list Ξ±) i β‰  a) := Ξ» i, is_false $ Ξ» H, H (nthd_nil _ _) @[simp] lemma nthd_singleton_default_eq (n : β„•) : [d].nthd d n = d := by { cases n; simp } @[simp] lemma nthd_repeat_default_eq (r n : β„•) : (repeat d r).nthd d n = d := begin induction r with r IH generalizing n, { simp }, { cases n; simp [IH] } end lemma nthd_append (l l' : list Ξ±) (d : Ξ±) (n : β„•) (h : n < l.length) (h' : n < (l ++ l').length := h.trans_le ((length_append l l').symm β–Έ le_self_add)) : (l ++ l').nthd d n = l.nthd d n := by rw [nthd_eq_nth_le _ _ h', nth_le_append h' h, nthd_eq_nth_le] lemma nthd_append_right (l l' : list Ξ±) (d : Ξ±) (n : β„•) (h : l.length ≀ n) : (l ++ l').nthd d n = l'.nthd d (n - l.length) := begin cases lt_or_le _ _ with h' h', { rw [nthd_eq_nth_le _ _ h', nth_le_append_right h h', nthd_eq_nth_le] }, { rw [nthd_eq_default _ _ h', nthd_eq_default], rwa [le_tsub_iff_left h, ←length_append] } end lemma nthd_eq_get_or_else_nth (n : β„•) : l.nthd d n = (l.nth n).get_or_else d := begin cases lt_or_le _ _ with h h, { rw [nthd_eq_nth_le _ _ h, nth_le_nth h, option.get_or_else_some] }, { rw [nthd_eq_default _ _ h, nth_eq_none_iff.mpr h, option.get_or_else_none] } end end nthd section inth variables [inhabited Ξ±] (l : list Ξ±) (x : Ξ±) (xs : list Ξ±) (n : β„•) @[simp] lemma inth_nil : inth ([] : list Ξ±) n = default := rfl @[simp] lemma inth_cons_zero : inth (x::xs) 0 = x := rfl @[simp] lemma inth_cons_succ : inth (x::xs) (n + 1) = inth xs n := rfl lemma inth_eq_nth_le {n : β„•} (hn : n < l.length) : l.inth n = l.nth_le n hn := nthd_eq_nth_le _ _ _ lemma inth_eq_default {n : β„•} (hn : l.length ≀ n) : l.inth n = default := nthd_eq_default _ _ hn lemma nthd_default_eq_inth : l.nthd default = l.inth := rfl lemma inth_append (l l' : list Ξ±) (n : β„•) (h : n < l.length) (h' : n < (l ++ l').length := h.trans_le ((length_append l l').symm β–Έ le_self_add)) : (l ++ l').inth n = l.inth n := nthd_append _ _ _ _ h h' lemma inth_append_right (l l' : list Ξ±) (n : β„•) (h : l.length ≀ n) : (l ++ l').inth n = l'.inth (n - l.length) := nthd_append_right _ _ _ _ h lemma inth_eq_iget_nth (n : β„•) : l.inth n = (l.nth n).iget := by rw [←nthd_default_eq_inth, nthd_eq_get_or_else_nth, option.get_or_else_default_eq_iget] lemma inth_zero_eq_head : l.inth 0 = l.head := by { cases l; refl, } end inth end list
203f1f4424155f242fb12b6428dc1d338a20d886
217bb195841a8be2d1b4edd2084d6b69ccd62f50
/library/init/data/ordering/basic.lean
10771741482ff7dc5e3c239f0fc335d2e6f50308
[ "Apache-2.0" ]
permissive
frank-lesser/lean4
717f56c9bacd5bf3a67542d2f5cea721d4743a30
79e2abe33f73162f773ea731265e456dbfe822f9
refs/heads/master
1,589,741,267,933
1,556,424,200,000
1,556,424,281,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,624
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.repr universes u v inductive Ordering | lt | Eq | gt instance : HasRepr Ordering := ⟨(Ξ» s, match s with | Ordering.lt := "lt" | Ordering.Eq := "Eq" | Ordering.gt := "gt")⟩ namespace Ordering def swap : Ordering β†’ Ordering | lt := gt | Eq := Eq | gt := lt @[inline] def orElse : Ordering β†’ Ordering β†’ Ordering | lt _ := lt | Eq o := o | gt _ := gt theorem swapSwap : βˆ€ (o : Ordering), o.swap.swap = o | lt := rfl | Eq := rfl | gt := rfl end Ordering @[inline] def cmpUsing {Ξ± : Type u} (lt : Ξ± β†’ Ξ± β†’ Prop) [DecidableRel lt] (a b : Ξ±) : Ordering := if lt a b then Ordering.lt else if lt b a then Ordering.gt else Ordering.Eq def cmp {Ξ± : Type u} [HasLess Ξ±] [DecidableRel ((<) : Ξ± β†’ Ξ± β†’ Prop)] (a b : Ξ±) : Ordering := cmpUsing (<) a b instance : DecidableEq Ordering := {decEq := Ξ» a b, match a with | Ordering.lt := (match b with | Ordering.lt := isTrue rfl | Ordering.Eq := isFalse (Ξ» h, Ordering.noConfusion h) | Ordering.gt := isFalse (Ξ» h, Ordering.noConfusion h)) | Ordering.Eq := (match b with | Ordering.lt := isFalse (Ξ» h, Ordering.noConfusion h) | Ordering.Eq := isTrue rfl | Ordering.gt := isFalse (Ξ» h, Ordering.noConfusion h)) | Ordering.gt := match b with | Ordering.lt := isFalse (Ξ» h, Ordering.noConfusion h) | Ordering.Eq := isFalse (Ξ» h, Ordering.noConfusion h) | Ordering.gt := isTrue rfl}
35b0afd671869c87956e7f59008d135448d531f9
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/topology/algebra/ordered/basic.lean
adfa05d7d0dac9b7afcbaff3041c24fd878f8574
[ "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
189,662
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 algebra.group_with_zero.power import data.set.intervals.pi import order.filter.interval import topology.algebra.group import tactic.linarith import tactic.tfae /-! # Theory of topology on ordered spaces ## Main definitions The order topology on an ordered space is the topology generated by all open intervals (or equivalently by those of the form `(-∞, a)` and `(b, +∞)`). We define it as `preorder.topology Ξ±`. However, we do *not* register it as an instance (as many existing ordered types already have topologies, which would be equal but not definitionally equal to `preorder.topology Ξ±`). Instead, we introduce a class `order_topology Ξ±` (which is a `Prop`, also known as a mixin) saying that on the type `Ξ±` having already a topological space structure and a preorder structure, the topological structure is equal to the order topology. We also introduce another (mixin) class `order_closed_topology Ξ±` saying that the set of points `(x, y)` with `x ≀ y` is closed in the product space. This is automatically satisfied on a linear order with the order topology. We prove many basic properties of such topologies. ## Main statements This file contains the proofs of the following facts. For exact requirements (`order_closed_topology` vs `order_topology`, `preorder` vs `partial_order` vs `linear_order` etc) see their statements. ### Open / closed sets * `is_open_lt` : if `f` and `g` are continuous functions, then `{x | f x < g x}` is open; * `is_open_Iio`, `is_open_Ioi`, `is_open_Ioo` : open intervals are open; * `is_closed_le` : if `f` and `g` are continuous functions, then `{x | f x ≀ g x}` is closed; * `is_closed_Iic`, `is_closed_Ici`, `is_closed_Icc` : closed intervals are closed; * `frontier_le_subset_eq`, `frontier_lt_subset_eq` : frontiers of both `{x | f x ≀ g x}` and `{x | f x < g x}` are included by `{x | f x = g x}`; * `exists_Ioc_subset_of_mem_nhds`, `exists_Ico_subset_of_mem_nhds` : if `x < y`, then any neighborhood of `x` includes an interval `[x, z)` for some `z ∈ (x, y]`, and any neighborhood of `y` includes an interval `(z, y]` for some `z ∈ [x, y)`. ### Convergence and inequalities * `le_of_tendsto_of_tendsto` : if `f` converges to `a`, `g` converges to `b`, and eventually `f x ≀ g x`, then `a ≀ b` * `le_of_tendsto`, `ge_of_tendsto` : if `f` converges to `a` and eventually `f x ≀ b` (resp., `b ≀ f x`), then `a ≀ b` (resp., `b ≀ a); we also provide primed versions that assume the inequalities to hold for all `x`. ### Min, max, `Sup` and `Inf` * `continuous.min`, `continuous.max`: pointwise `min`/`max` of two continuous functions is continuous. * `tendsto.min`, `tendsto.max` : if `f` tends to `a` and `g` tends to `b`, then their pointwise `min`/`max` tend to `min a b` and `max a b`, respectively. * `tendsto_of_tendsto_of_tendsto_of_le_of_le` : theorem known as squeeze theorem, sandwich theorem, theorem of Carabinieri, and two policemen (and a drunk) theorem; if `g` and `h` both converge to `a`, and eventually `g x ≀ f x ≀ h x`, then `f` converges to `a`. ### Connected sets and Intermediate Value Theorem * `is_preconnected_I??` : all intervals `I??` are preconnected, * `is_preconnected.intermediate_value`, `intermediate_value_univ` : Intermediate Value Theorem for connected sets and connected spaces, respectively; * `intermediate_value_Icc`, `intermediate_value_Icc'`: Intermediate Value Theorem for functions on closed intervals. ### Miscellaneous facts * `is_compact.exists_forall_le`, `is_compact.exists_forall_ge` : extreme value theorem, a continuous function on a compact set takes its minimum and maximum values. * `is_closed.Icc_subset_of_forall_mem_nhds_within` : β€œContinuous induction” principle; if `s ∩ [a, b]` is closed, `a ∈ s`, and for each `x ∈ [a, b) ∩ s` some of its right neighborhoods is included `s`, then `[a, b] βŠ† s`. * `is_closed.Icc_subset_of_forall_exists_gt`, `is_closed.mem_of_ge_of_forall_exists_gt` : two other versions of the β€œcontinuous induction” principle. ## Implementation We do _not_ register the order topology as an instance on a preorder (or even on a linear order). Indeed, on many such spaces, a topology has already been constructed in a different way (think of the discrete spaces `β„•` or `β„€`, or `ℝ` that could inherit a topology as the completion of `β„š`), and is in general not defeq to the one generated by the intervals. We make it available as a definition `preorder.topology Ξ±` though, that can be registered as an instance when necessary, or for specific types. -/ open classical set filter topological_space open function open_locale topological_space classical filter universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} /-- A topology on a set which is both a topological space and a preorder is _order-closed_ if the set of points `(x, y)` with `x ≀ y` is closed in the product space. We introduce this as a mixin. This property is satisfied for the order topology on a linear order, but it can be satisfied more generally, and suffices to derive many interesting properties relating order and topology. -/ class order_closed_topology (Ξ± : Type*) [topological_space Ξ±] [preorder Ξ±] : Prop := (is_closed_le' : is_closed {p:Ξ±Γ—Ξ± | p.1 ≀ p.2}) instance : Ξ  [topological_space Ξ±], topological_space (order_dual Ξ±) := id instance [topological_space Ξ±] [h : first_countable_topology Ξ±] : first_countable_topology (order_dual Ξ±) := h @[to_additive] instance [topological_space Ξ±] [has_mul Ξ±] [h : has_continuous_mul Ξ±] : has_continuous_mul (order_dual Ξ±) := h section order_closed_topology section preorder variables [topological_space Ξ±] [preorder Ξ±] [t : order_closed_topology Ξ±] include t namespace subtype instance {p : Ξ± β†’ Prop} : order_closed_topology (subtype p) := have this : continuous (Ξ» (p : (subtype p) Γ— (subtype p)), ((p.fst : Ξ±), (p.snd : Ξ±))) := (continuous_subtype_coe.comp continuous_fst).prod_mk (continuous_subtype_coe.comp continuous_snd), order_closed_topology.mk (t.is_closed_le'.preimage this) end subtype lemma is_closed_le_prod : is_closed {p : Ξ± Γ— Ξ± | p.1 ≀ p.2} := t.is_closed_le' lemma is_closed_le [topological_space Ξ²] {f g : Ξ² β†’ Ξ±} (hf : continuous f) (hg : continuous g) : is_closed {b | f b ≀ g b} := continuous_iff_is_closed.mp (hf.prod_mk hg) _ is_closed_le_prod lemma is_closed_le' (a : Ξ±) : is_closed {b | b ≀ a} := is_closed_le continuous_id continuous_const lemma is_closed_Iic {a : Ξ±} : is_closed (Iic a) := is_closed_le' a lemma is_closed_ge' (a : Ξ±) : is_closed {b | a ≀ b} := is_closed_le continuous_const continuous_id lemma is_closed_Ici {a : Ξ±} : is_closed (Ici a) := is_closed_ge' a instance : order_closed_topology (order_dual Ξ±) := ⟨(@order_closed_topology.is_closed_le' Ξ± _ _ _).preimage continuous_swap⟩ lemma is_closed_Icc {a b : Ξ±} : is_closed (Icc a b) := is_closed.inter is_closed_Ici is_closed_Iic @[simp] lemma closure_Icc (a b : Ξ±) : closure (Icc a b) = Icc a b := is_closed_Icc.closure_eq @[simp] lemma closure_Iic (a : Ξ±) : closure (Iic a) = Iic a := is_closed_Iic.closure_eq @[simp] lemma closure_Ici (a : Ξ±) : closure (Ici a) = Ici a := is_closed_Ici.closure_eq lemma le_of_tendsto_of_tendsto {f g : Ξ² β†’ Ξ±} {b : filter Ξ²} {a₁ aβ‚‚ : Ξ±} [ne_bot b] (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 aβ‚‚)) (h : f ≀ᢠ[b] g) : a₁ ≀ aβ‚‚ := have tendsto (Ξ»b, (f b, g b)) b (𝓝 (a₁, aβ‚‚)), by rw [nhds_prod_eq]; exact hf.prod_mk hg, show (a₁, aβ‚‚) ∈ {p:Ξ±Γ—Ξ± | p.1 ≀ p.2}, from t.is_closed_le'.mem_of_tendsto this h lemma le_of_tendsto_of_tendsto' {f g : Ξ² β†’ Ξ±} {b : filter Ξ²} {a₁ aβ‚‚ : Ξ±} [ne_bot b] (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 aβ‚‚)) (h : βˆ€ x, f x ≀ g x) : a₁ ≀ aβ‚‚ := le_of_tendsto_of_tendsto hf hg (eventually_of_forall h) lemma le_of_tendsto {f : Ξ² β†’ Ξ±} {a b : Ξ±} {x : filter Ξ²} [ne_bot x] (lim : tendsto f x (𝓝 a)) (h : βˆ€αΆ  c in x, f c ≀ b) : a ≀ b := le_of_tendsto_of_tendsto lim tendsto_const_nhds h lemma le_of_tendsto' {f : Ξ² β†’ Ξ±} {a b : Ξ±} {x : filter Ξ²} [ne_bot x] (lim : tendsto f x (𝓝 a)) (h : βˆ€ c, f c ≀ b) : a ≀ b := le_of_tendsto lim (eventually_of_forall h) lemma ge_of_tendsto {f : Ξ² β†’ Ξ±} {a b : Ξ±} {x : filter Ξ²} [ne_bot x] (lim : tendsto f x (𝓝 a)) (h : βˆ€αΆ  c in x, b ≀ f c) : b ≀ a := le_of_tendsto_of_tendsto tendsto_const_nhds lim h lemma ge_of_tendsto' {f : Ξ² β†’ Ξ±} {a b : Ξ±} {x : filter Ξ²} [ne_bot x] (lim : tendsto f x (𝓝 a)) (h : βˆ€ c, b ≀ f c) : b ≀ a := ge_of_tendsto lim (eventually_of_forall h) @[simp] lemma closure_le_eq [topological_space Ξ²] {f g : Ξ² β†’ Ξ±} (hf : continuous f) (hg : continuous g) : closure {b | f b ≀ g b} = {b | f b ≀ g b} := (is_closed_le hf hg).closure_eq lemma closure_lt_subset_le [topological_space Ξ²] {f g : Ξ² β†’ Ξ±} (hf : continuous f) (hg : continuous g) : closure {b | f b < g b} βŠ† {b | f b ≀ g b} := by { rw [←closure_le_eq hf hg], exact closure_mono (Ξ» b, le_of_lt) } lemma continuous_within_at.closure_le [topological_space Ξ²] {f g : Ξ² β†’ Ξ±} {s : set Ξ²} {x : Ξ²} (hx : x ∈ closure s) (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) (h : βˆ€ y ∈ s, f y ≀ g y) : f x ≀ g x := show (f x, g x) ∈ {p : Ξ± Γ— Ξ± | p.1 ≀ p.2}, from order_closed_topology.is_closed_le'.closure_subset ((hf.prod hg).mem_closure hx h) /-- If `s` is a closed set and two functions `f` and `g` are continuous on `s`, then the set `{x ∈ s | f x ≀ g x}` is a closed set. -/ lemma is_closed.is_closed_le [topological_space Ξ²] {f g : Ξ² β†’ Ξ±} {s : set Ξ²} (hs : is_closed s) (hf : continuous_on f s) (hg : continuous_on g s) : is_closed {x ∈ s | f x ≀ g x} := (hf.prod hg).preimage_closed_of_closed hs order_closed_topology.is_closed_le' omit t lemma nhds_within_Ici_ne_bot {a b : Ξ±} (Hβ‚‚ : a ≀ b) : ne_bot (𝓝[Ici a] b) := nhds_within_ne_bot_of_mem Hβ‚‚ @[instance] lemma nhds_within_Ici_self_ne_bot (a : Ξ±) : ne_bot (𝓝[Ici a] a) := nhds_within_Ici_ne_bot (le_refl a) lemma nhds_within_Iic_ne_bot {a b : Ξ±} (H : a ≀ b) : ne_bot (𝓝[Iic b] a) := nhds_within_ne_bot_of_mem H @[instance] lemma nhds_within_Iic_self_ne_bot (a : Ξ±) : ne_bot (𝓝[Iic a] a) := nhds_within_Iic_ne_bot (le_refl a) end preorder section partial_order variables [topological_space Ξ±] [partial_order Ξ±] [t : order_closed_topology Ξ±] include t private lemma is_closed_eq_aux : is_closed {p : Ξ± Γ— Ξ± | p.1 = p.2} := by simp only [le_antisymm_iff]; exact is_closed.inter t.is_closed_le' (is_closed_le continuous_snd continuous_fst) @[priority 90] -- see Note [lower instance priority] instance order_closed_topology.to_t2_space : t2_space Ξ± := { t2 := have is_open {p : Ξ± Γ— Ξ± | p.1 β‰  p.2} := is_closed_eq_aux.is_open_compl, assume a b h, let ⟨u, v, hu, hv, ha, hb, h⟩ := is_open_prod_iff.mp this a b h in ⟨u, v, hu, hv, ha, hb, set.eq_empty_iff_forall_not_mem.2 $ assume a ⟨h₁, hβ‚‚βŸ©, have a β‰  a, from @h (a, a) ⟨h₁, hβ‚‚βŸ©, this rfl⟩ } end partial_order section linear_order variables [topological_space Ξ±] [linear_order Ξ±] [order_closed_topology Ξ±] lemma is_open_lt_prod : is_open {p : Ξ± Γ— Ξ± | p.1 < p.2} := by { simp_rw [← is_closed_compl_iff, compl_set_of, not_lt], exact is_closed_le continuous_snd continuous_fst } lemma is_open_lt [topological_space Ξ²] {f g : Ξ² β†’ Ξ±} (hf : continuous f) (hg : continuous g) : is_open {b | f b < g b} := by simp [lt_iff_not_ge, -not_le]; exact (is_closed_le hg hf).is_open_compl variables {a b : Ξ±} lemma is_open_Iio : is_open (Iio a) := is_open_lt continuous_id continuous_const lemma is_open_Ioi : is_open (Ioi a) := is_open_lt continuous_const continuous_id lemma is_open_Ioo : is_open (Ioo a b) := is_open.inter is_open_Ioi is_open_Iio @[simp] lemma interior_Ioi : interior (Ioi a) = Ioi a := is_open_Ioi.interior_eq @[simp] lemma interior_Iio : interior (Iio a) = Iio a := is_open_Iio.interior_eq @[simp] lemma interior_Ioo : interior (Ioo a b) = Ioo a b := is_open_Ioo.interior_eq lemma eventually_le_of_tendsto_lt {l : filter Ξ³} {f : Ξ³ β†’ Ξ±} {u v : Ξ±} (hv : v < u) (h : tendsto f l (𝓝 v)) : βˆ€αΆ  a in l, f a ≀ u := eventually.mono (tendsto_nhds.1 h (< u) is_open_Iio hv) (Ξ» v, le_of_lt) lemma eventually_ge_of_tendsto_gt {l : filter Ξ³} {f : Ξ³ β†’ Ξ±} {u v : Ξ±} (hv : u < v) (h : tendsto f l (𝓝 v)) : βˆ€αΆ  a in l, u ≀ f a := eventually.mono (tendsto_nhds.1 h (> u) is_open_Ioi hv) (Ξ» v, le_of_lt) variables [topological_space Ξ³] /-- Intermediate value theorem for two functions: if `f` and `g` are two continuous functions on a preconnected space and `f a ≀ g a` and `g b ≀ f b`, then for some `x` we have `f x = g x`. -/ lemma intermediate_value_univβ‚‚ [preconnected_space Ξ³] {a b : Ξ³} {f g : Ξ³ β†’ Ξ±} (hf : continuous f) (hg : continuous g) (ha : f a ≀ g a) (hb : g b ≀ f b) : βˆƒ x, f x = g x := begin obtain ⟨x, h, hfg, hgf⟩ : (univ ∩ {x | f x ≀ g x ∧ g x ≀ f x}).nonempty, from is_preconnected_closed_iff.1 preconnected_space.is_preconnected_univ _ _ (is_closed_le hf hg) (is_closed_le hg hf) (Ξ» x hx, le_total _ _) ⟨a, trivial, ha⟩ ⟨b, trivial, hb⟩, exact ⟨x, le_antisymm hfg hgf⟩ end lemma intermediate_value_univβ‚‚_eventually₁ [preconnected_space Ξ³] {a : Ξ³} {l : filter Ξ³} [ne_bot l] {f g : Ξ³ β†’ Ξ±} (hf : continuous f) (hg : continuous g) (ha : f a ≀ g a) (he : g ≀ᢠ[l] f) : βˆƒ x, f x = g x := let ⟨c, hc⟩ := he.frequently.exists in intermediate_value_univβ‚‚ hf hg ha hc lemma intermediate_value_univβ‚‚_eventuallyβ‚‚ [preconnected_space Ξ³] {l₁ lβ‚‚ : filter Ξ³} [ne_bot l₁] [ne_bot lβ‚‚] {f g : Ξ³ β†’ Ξ±} (hf : continuous f) (hg : continuous g) (he₁ : f ≀ᢠ[l₁] g ) (heβ‚‚ : g ≀ᢠ[lβ‚‚] f) : βˆƒ x, f x = g x := let ⟨c₁, hcβ‚βŸ© := he₁.frequently.exists, ⟨cβ‚‚, hcβ‚‚βŸ© := heβ‚‚.frequently.exists in intermediate_value_univβ‚‚ hf hg hc₁ hcβ‚‚ /-- Intermediate value theorem for two functions: if `f` and `g` are two functions continuous on a preconnected set `s` and for some `a b ∈ s` we have `f a ≀ g a` and `g b ≀ f b`, then for some `x ∈ s` we have `f x = g x`. -/ lemma is_preconnected.intermediate_valueβ‚‚ {s : set Ξ³} (hs : is_preconnected s) {a b : Ξ³} (ha : a ∈ s) (hb : b ∈ s) {f g : Ξ³ β†’ Ξ±} (hf : continuous_on f s) (hg : continuous_on g s) (ha' : f a ≀ g a) (hb' : g b ≀ f b) : βˆƒ x ∈ s, f x = g x := let ⟨x, hx⟩ := @intermediate_value_univβ‚‚ Ξ± s _ _ _ _ (subtype.preconnected_space hs) ⟨a, ha⟩ ⟨b, hb⟩ _ _ (continuous_on_iff_continuous_restrict.1 hf) (continuous_on_iff_continuous_restrict.1 hg) ha' hb' in ⟨x, x.2, hx⟩ lemma is_preconnected.intermediate_valueβ‚‚_eventually₁ {s : set Ξ³} (hs : is_preconnected s) {a : Ξ³} {l : filter Ξ³} (ha : a ∈ s) [ne_bot l] (hl : l ≀ π“Ÿ s) {f g : Ξ³ β†’ Ξ±} (hf : continuous_on f s) (hg : continuous_on g s) (ha' : f a ≀ g a) (he : g ≀ᢠ[l] f) : βˆƒ x ∈ s, f x = g x := begin rw continuous_on_iff_continuous_restrict at hf hg, obtain ⟨b, h⟩ := @intermediate_value_univβ‚‚_eventually₁ _ _ _ _ _ _ (subtype.preconnected_space hs) ⟨a, ha⟩ _ (comap_coe_ne_bot_of_le_principal hl) _ _ hf hg ha' (eventually_comap' he), exact ⟨b, b.prop, h⟩, end lemma is_preconnected.intermediate_valueβ‚‚_eventuallyβ‚‚ {s : set Ξ³} (hs : is_preconnected s) {l₁ lβ‚‚ : filter Ξ³} [ne_bot l₁] [ne_bot lβ‚‚] (hl₁ : l₁ ≀ π“Ÿ s) (hlβ‚‚ : lβ‚‚ ≀ π“Ÿ s) {f g : Ξ³ β†’ Ξ±} (hf : continuous_on f s) (hg : continuous_on g s) (he₁ : f ≀ᢠ[l₁] g) (heβ‚‚ : g ≀ᢠ[lβ‚‚] f) : βˆƒ x ∈ s, f x = g x := begin rw continuous_on_iff_continuous_restrict at hf hg, obtain ⟨b, h⟩ := @intermediate_value_univβ‚‚_eventuallyβ‚‚ _ _ _ _ _ _ (subtype.preconnected_space hs) _ _ (comap_coe_ne_bot_of_le_principal hl₁) (comap_coe_ne_bot_of_le_principal hlβ‚‚) _ _ hf hg (eventually_comap' he₁) (eventually_comap' heβ‚‚), exact ⟨b, b.prop, h⟩, end /-- **Intermediate Value Theorem** for continuous functions on connected sets. -/ lemma is_preconnected.intermediate_value {s : set Ξ³} (hs : is_preconnected s) {a b : Ξ³} (ha : a ∈ s) (hb : b ∈ s) {f : Ξ³ β†’ Ξ±} (hf : continuous_on f s) : Icc (f a) (f b) βŠ† f '' s := Ξ» x hx, mem_image_iff_bex.2 $ hs.intermediate_valueβ‚‚ ha hb hf continuous_on_const hx.1 hx.2 lemma is_preconnected.intermediate_value_Ico {s : set Ξ³} (hs : is_preconnected s) {a : Ξ³} {l : filter Ξ³} (ha : a ∈ s) [ne_bot l] (hl : l ≀ π“Ÿ s) {f : Ξ³ β†’ Ξ±} (hf : continuous_on f s) {v : Ξ±} (ht : tendsto f l (𝓝 v)) : Ico (f a) v βŠ† f '' s := Ξ» y h, bex_def.1 $ hs.intermediate_valueβ‚‚_eventually₁ ha hl hf continuous_on_const h.1 (eventually_ge_of_tendsto_gt h.2 ht) lemma is_preconnected.intermediate_value_Ioc {s : set Ξ³} (hs : is_preconnected s) {a : Ξ³} {l : filter Ξ³} (ha : a ∈ s) [ne_bot l] (hl : l ≀ π“Ÿ s) {f : Ξ³ β†’ Ξ±} (hf : continuous_on f s) {v : Ξ±} (ht : tendsto f l (𝓝 v)) : Ioc v (f a) βŠ† f '' s := Ξ» y h, bex_def.1 $ bex.imp_right (Ξ» x _, eq.symm) $ hs.intermediate_valueβ‚‚_eventually₁ ha hl continuous_on_const hf h.2 (eventually_le_of_tendsto_lt h.1 ht) lemma is_preconnected.intermediate_value_Ioo {s : set Ξ³} (hs : is_preconnected s) {l₁ lβ‚‚ : filter Ξ³} [ne_bot l₁] [ne_bot lβ‚‚] (hl₁ : l₁ ≀ π“Ÿ s) (hlβ‚‚ : lβ‚‚ ≀ π“Ÿ s) {f : Ξ³ β†’ Ξ±} (hf : continuous_on f s) {v₁ vβ‚‚ : Ξ±} (ht₁ : tendsto f l₁ (𝓝 v₁)) (htβ‚‚ : tendsto f lβ‚‚ (𝓝 vβ‚‚)) : Ioo v₁ vβ‚‚ βŠ† f '' s := Ξ» y h, bex_def.1 $ hs.intermediate_valueβ‚‚_eventuallyβ‚‚ hl₁ hlβ‚‚ hf continuous_on_const (eventually_le_of_tendsto_lt h.1 ht₁) (eventually_ge_of_tendsto_gt h.2 htβ‚‚) lemma is_preconnected.intermediate_value_Ici {s : set Ξ³} (hs : is_preconnected s) {a : Ξ³} {l : filter Ξ³} (ha : a ∈ s) [ne_bot l] (hl : l ≀ π“Ÿ s) {f : Ξ³ β†’ Ξ±} (hf : continuous_on f s) (ht : tendsto f l at_top) : Ici (f a) βŠ† f '' s := Ξ» y h, bex_def.1 $ hs.intermediate_valueβ‚‚_eventually₁ ha hl hf continuous_on_const h (tendsto_at_top.1 ht y) lemma is_preconnected.intermediate_value_Iic {s : set Ξ³} (hs : is_preconnected s) {a : Ξ³} {l : filter Ξ³} (ha : a ∈ s) [ne_bot l] (hl : l ≀ π“Ÿ s) {f : Ξ³ β†’ Ξ±} (hf : continuous_on f s) (ht : tendsto f l at_bot) : Iic (f a) βŠ† f '' s := Ξ» y h, bex_def.1 $ bex.imp_right (Ξ» x _, eq.symm) $ hs.intermediate_valueβ‚‚_eventually₁ ha hl continuous_on_const hf h (tendsto_at_bot.1 ht y) lemma is_preconnected.intermediate_value_Ioi {s : set Ξ³} (hs : is_preconnected s) {l₁ lβ‚‚ : filter Ξ³} [ne_bot l₁] [ne_bot lβ‚‚] (hl₁ : l₁ ≀ π“Ÿ s) (hlβ‚‚ : lβ‚‚ ≀ π“Ÿ s) {f : Ξ³ β†’ Ξ±} (hf : continuous_on f s) {v : Ξ±} (ht₁ : tendsto f l₁ (𝓝 v)) (htβ‚‚ : tendsto f lβ‚‚ at_top) : Ioi v βŠ† f '' s := Ξ» y h, bex_def.1 $ hs.intermediate_valueβ‚‚_eventuallyβ‚‚ hl₁ hlβ‚‚ hf continuous_on_const (eventually_le_of_tendsto_lt h ht₁) (tendsto_at_top.1 htβ‚‚ y) lemma is_preconnected.intermediate_value_Iio {s : set Ξ³} (hs : is_preconnected s) {l₁ lβ‚‚ : filter Ξ³} [ne_bot l₁] [ne_bot lβ‚‚] (hl₁ : l₁ ≀ π“Ÿ s) (hlβ‚‚ : lβ‚‚ ≀ π“Ÿ s) {f : Ξ³ β†’ Ξ±} (hf : continuous_on f s) {v : Ξ±} (ht₁ : tendsto f l₁ at_bot) (htβ‚‚ : tendsto f lβ‚‚ (𝓝 v)) : Iio v βŠ† f '' s := Ξ» y h, bex_def.1 $ hs.intermediate_valueβ‚‚_eventuallyβ‚‚ hl₁ hlβ‚‚ hf continuous_on_const (tendsto_at_bot.1 ht₁ y) (eventually_ge_of_tendsto_gt h htβ‚‚) lemma is_preconnected.intermediate_value_Iii {s : set Ξ³} (hs : is_preconnected s) {l₁ lβ‚‚ : filter Ξ³} [ne_bot l₁] [ne_bot lβ‚‚] (hl₁ : l₁ ≀ π“Ÿ s) (hlβ‚‚ : lβ‚‚ ≀ π“Ÿ s) {f : Ξ³ β†’ Ξ±} (hf : continuous_on f s) (ht₁ : tendsto f l₁ at_bot) (htβ‚‚ : tendsto f lβ‚‚ at_top) : univ βŠ† f '' s := Ξ» y h, bex_def.1 $ hs.intermediate_valueβ‚‚_eventuallyβ‚‚ hl₁ hlβ‚‚ hf continuous_on_const (tendsto_at_bot.1 ht₁ y) (tendsto_at_top.1 htβ‚‚ y) /-- **Intermediate Value Theorem** for continuous functions on connected spaces. -/ lemma intermediate_value_univ [preconnected_space Ξ³] (a b : Ξ³) {f : Ξ³ β†’ Ξ±} (hf : continuous f) : Icc (f a) (f b) βŠ† range f := Ξ» x hx, intermediate_value_univβ‚‚ hf continuous_const hx.1 hx.2 /-- **Intermediate Value Theorem** for continuous functions on connected spaces. -/ lemma mem_range_of_exists_le_of_exists_ge [preconnected_space Ξ³] {c : Ξ±} {f : Ξ³ β†’ Ξ±} (hf : continuous f) (h₁ : βˆƒ a, f a ≀ c) (hβ‚‚ : βˆƒ b, c ≀ f b) : c ∈ range f := let ⟨a, ha⟩ := h₁, ⟨b, hb⟩ := hβ‚‚ in intermediate_value_univ a b hf ⟨ha, hb⟩ /-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/ lemma is_preconnected.Icc_subset {s : set Ξ±} (hs : is_preconnected s) {a b : Ξ±} (ha : a ∈ s) (hb : b ∈ s) : Icc a b βŠ† s := by simpa only [image_id] using hs.intermediate_value ha hb continuous_on_id /-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/ lemma is_connected.Icc_subset {s : set Ξ±} (hs : is_connected s) {a b : Ξ±} (ha : a ∈ s) (hb : b ∈ s) : Icc a b βŠ† s := hs.2.Icc_subset ha hb /-- If preconnected set in a linear order space is unbounded below and above, then it is the whole space. -/ lemma is_preconnected.eq_univ_of_unbounded {s : set Ξ±} (hs : is_preconnected s) (hb : Β¬bdd_below s) (ha : Β¬bdd_above s) : s = univ := begin refine eq_univ_of_forall (Ξ» x, _), obtain ⟨y, ys, hy⟩ : βˆƒ y ∈ s, y < x := not_bdd_below_iff.1 hb x, obtain ⟨z, zs, hz⟩ : βˆƒ z ∈ s, x < z := not_bdd_above_iff.1 ha x, exact hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩ end /-! ### Neighborhoods to the left and to the right on an `order_closed_topology` Limits to the left and to the right of real functions are defined in terms of neighborhoods to the left and to the right, either open or closed, i.e., members of `𝓝[Ioi a] a` and `𝓝[Ici a] a` on the right, and similarly on the left. Here we simply prove that all right-neighborhoods of a point are equal, and we'll prove later other useful characterizations which require the stronger hypothesis `order_topology Ξ±` -/ /-! #### Right neighborhoods, point excluded -/ lemma Ioo_mem_nhds_within_Ioi {a b c : Ξ±} (H : b ∈ Ico a c) : Ioo a c ∈ 𝓝[Ioi b] b := mem_nhds_within.2 ⟨Iio c, is_open_Iio, H.2, by rw [inter_comm, Ioi_inter_Iio]; exact Ioo_subset_Ioo_left H.1⟩ lemma Ioc_mem_nhds_within_Ioi {a b c : Ξ±} (H : b ∈ Ico a c) : Ioc a c ∈ 𝓝[Ioi b] b := mem_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Ioc_self lemma Ico_mem_nhds_within_Ioi {a b c : Ξ±} (H : b ∈ Ico a c) : Ico a c ∈ 𝓝[Ioi b] b := mem_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Ico_self lemma Icc_mem_nhds_within_Ioi {a b c : Ξ±} (H : b ∈ Ico a c) : Icc a c ∈ 𝓝[Ioi b] b := mem_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Icc_self @[simp] lemma nhds_within_Ioc_eq_nhds_within_Ioi {a b : Ξ±} (h : a < b) : 𝓝[Ioc a b] a = 𝓝[Ioi a] a := le_antisymm (nhds_within_mono _ Ioc_subset_Ioi_self) $ nhds_within_le_of_mem $ Ioc_mem_nhds_within_Ioi $ left_mem_Ico.2 h @[simp] lemma nhds_within_Ioo_eq_nhds_within_Ioi {a b : Ξ±} (h : a < b) : 𝓝[Ioo a b] a = 𝓝[Ioi a] a := le_antisymm (nhds_within_mono _ Ioo_subset_Ioi_self) $ nhds_within_le_of_mem $ Ioo_mem_nhds_within_Ioi $ left_mem_Ico.2 h @[simp] lemma continuous_within_at_Ioc_iff_Ioi [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β†’ Ξ²} (h : a < b) : continuous_within_at f (Ioc a b) a ↔ continuous_within_at f (Ioi a) a := by simp only [continuous_within_at, nhds_within_Ioc_eq_nhds_within_Ioi h] @[simp] lemma continuous_within_at_Ioo_iff_Ioi [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β†’ Ξ²} (h : a < b) : continuous_within_at f (Ioo a b) a ↔ continuous_within_at f (Ioi a) a := by simp only [continuous_within_at, nhds_within_Ioo_eq_nhds_within_Ioi h] /-! #### Left neighborhoods, point excluded -/ lemma Ioo_mem_nhds_within_Iio {a b c : Ξ±} (H : b ∈ Ioc a c) : Ioo a c ∈ 𝓝[Iio b] b := by simpa only [dual_Ioo] using @Ioo_mem_nhds_within_Ioi (order_dual Ξ±) _ _ _ _ _ _ ⟨H.2, H.1⟩ lemma Ico_mem_nhds_within_Iio {a b c : Ξ±} (H : b ∈ Ioc a c) : Ico a c ∈ 𝓝[Iio b] b := mem_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Ico_self lemma Ioc_mem_nhds_within_Iio {a b c : Ξ±} (H : b ∈ Ioc a c) : Ioc a c ∈ 𝓝[Iio b] b := mem_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Ioc_self lemma Icc_mem_nhds_within_Iio {a b c : Ξ±} (H : b ∈ Ioc a c) : Icc a c ∈ 𝓝[Iio b] b := mem_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Icc_self @[simp] lemma nhds_within_Ico_eq_nhds_within_Iio {a b : Ξ±} (h : a < b) : 𝓝[Ico a b] b = 𝓝[Iio b] b := by simpa only [dual_Ioc] using @nhds_within_Ioc_eq_nhds_within_Ioi (order_dual Ξ±) _ _ _ _ _ h @[simp] lemma nhds_within_Ioo_eq_nhds_within_Iio {a b : Ξ±} (h : a < b) : 𝓝[Ioo a b] b = 𝓝[Iio b] b := by simpa only [dual_Ioo] using @nhds_within_Ioo_eq_nhds_within_Ioi (order_dual Ξ±) _ _ _ _ _ h @[simp] lemma continuous_within_at_Ico_iff_Iio {a b : Ξ±} {f : Ξ± β†’ Ξ³} (h : a < b) : continuous_within_at f (Ico a b) b ↔ continuous_within_at f (Iio b) b := by simp only [continuous_within_at, nhds_within_Ico_eq_nhds_within_Iio h] @[simp] lemma continuous_within_at_Ioo_iff_Iio {a b : Ξ±} {f : Ξ± β†’ Ξ³} (h : a < b) : continuous_within_at f (Ioo a b) b ↔ continuous_within_at f (Iio b) b := by simp only [continuous_within_at, nhds_within_Ioo_eq_nhds_within_Iio h] /-! #### Right neighborhoods, point included -/ lemma Ioo_mem_nhds_within_Ici {a b c : Ξ±} (H : b ∈ Ioo a c) : Ioo a c ∈ 𝓝[Ici b] b := mem_nhds_within_of_mem_nhds $ is_open.mem_nhds is_open_Ioo H lemma Ioc_mem_nhds_within_Ici {a b c : Ξ±} (H : b ∈ Ioo a c) : Ioc a c ∈ 𝓝[Ici b] b := mem_of_superset (Ioo_mem_nhds_within_Ici H) Ioo_subset_Ioc_self lemma Ico_mem_nhds_within_Ici {a b c : Ξ±} (H : b ∈ Ico a c) : Ico a c ∈ 𝓝[Ici b] b := mem_nhds_within.2 ⟨Iio c, is_open_Iio, H.2, by simp only [inter_comm, Ici_inter_Iio, Ico_subset_Ico_left H.1]⟩ lemma Icc_mem_nhds_within_Ici {a b c : Ξ±} (H : b ∈ Ico a c) : Icc a c ∈ 𝓝[Ici b] b := mem_of_superset (Ico_mem_nhds_within_Ici H) Ico_subset_Icc_self @[simp] lemma nhds_within_Icc_eq_nhds_within_Ici {a b : Ξ±} (h : a < b) : 𝓝[Icc a b] a = 𝓝[Ici a] a := le_antisymm (nhds_within_mono _ Icc_subset_Ici_self) $ nhds_within_le_of_mem $ Icc_mem_nhds_within_Ici $ left_mem_Ico.2 h @[simp] lemma nhds_within_Ico_eq_nhds_within_Ici {a b : Ξ±} (h : a < b) : 𝓝[Ico a b] a = 𝓝[Ici a] a := le_antisymm (nhds_within_mono _ (Ξ» x, and.left)) $ nhds_within_le_of_mem $ Ico_mem_nhds_within_Ici $ left_mem_Ico.2 h @[simp] lemma continuous_within_at_Icc_iff_Ici [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β†’ Ξ²} (h : a < b) : continuous_within_at f (Icc a b) a ↔ continuous_within_at f (Ici a) a := by simp only [continuous_within_at, nhds_within_Icc_eq_nhds_within_Ici h] @[simp] lemma continuous_within_at_Ico_iff_Ici [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β†’ Ξ²} (h : a < b) : continuous_within_at f (Ico a b) a ↔ continuous_within_at f (Ici a) a := by simp only [continuous_within_at, nhds_within_Ico_eq_nhds_within_Ici h] /-! #### Left neighborhoods, point included -/ lemma Ioo_mem_nhds_within_Iic {a b c : Ξ±} (H : b ∈ Ioo a c) : Ioo a c ∈ 𝓝[Iic b] b := mem_nhds_within_of_mem_nhds $ is_open.mem_nhds is_open_Ioo H lemma Ico_mem_nhds_within_Iic {a b c : Ξ±} (H : b ∈ Ioo a c) : Ico a c ∈ 𝓝[Iic b] b := mem_of_superset (Ioo_mem_nhds_within_Iic H) Ioo_subset_Ico_self lemma Ioc_mem_nhds_within_Iic {a b c : Ξ±} (H : b ∈ Ioc a c) : Ioc a c ∈ 𝓝[Iic b] b := by simpa only [dual_Ico] using @Ico_mem_nhds_within_Ici (order_dual Ξ±) _ _ _ _ _ _ ⟨H.2, H.1⟩ lemma Icc_mem_nhds_within_Iic {a b c : Ξ±} (H : b ∈ Ioc a c) : Icc a c ∈ 𝓝[Iic b] b := mem_of_superset (Ioc_mem_nhds_within_Iic H) Ioc_subset_Icc_self @[simp] lemma nhds_within_Icc_eq_nhds_within_Iic {a b : Ξ±} (h : a < b) : 𝓝[Icc a b] b = 𝓝[Iic b] b := by simpa only [dual_Icc] using @nhds_within_Icc_eq_nhds_within_Ici (order_dual Ξ±) _ _ _ _ _ h @[simp] lemma nhds_within_Ioc_eq_nhds_within_Iic {a b : Ξ±} (h : a < b) : 𝓝[Ioc a b] b = 𝓝[Iic b] b := by simpa only [dual_Ico] using @nhds_within_Ico_eq_nhds_within_Ici (order_dual Ξ±) _ _ _ _ _ h @[simp] lemma continuous_within_at_Icc_iff_Iic [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β†’ Ξ²} (h : a < b) : continuous_within_at f (Icc a b) b ↔ continuous_within_at f (Iic b) b := by simp only [continuous_within_at, nhds_within_Icc_eq_nhds_within_Iic h] @[simp] lemma continuous_within_at_Ioc_iff_Iic [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β†’ Ξ²} (h : a < b) : continuous_within_at f (Ioc a b) b ↔ continuous_within_at f (Iic b) b := by simp only [continuous_within_at, nhds_within_Ioc_eq_nhds_within_Iic h] end linear_order section linear_order variables [topological_space Ξ±] [linear_order Ξ±] [order_closed_topology Ξ±] {f g : Ξ² β†’ Ξ±} section variables [topological_space Ξ²] lemma frontier_le_subset_eq (hf : continuous f) (hg : continuous g) : frontier {b | f b ≀ g b} βŠ† {b | f b = g b} := begin rw [frontier_eq_closure_inter_closure, closure_le_eq hf hg], rintros b ⟨hb₁, hbβ‚‚βŸ©, refine le_antisymm hb₁ (closure_lt_subset_le hg hf _), convert hbβ‚‚ using 2, simp only [not_le.symm], refl end lemma frontier_Iic_subset (a : Ξ±) : frontier (Iic a) βŠ† {a} := frontier_le_subset_eq (@continuous_id Ξ± _) continuous_const lemma frontier_Ici_subset (a : Ξ±) : frontier (Ici a) βŠ† {a} := @frontier_Iic_subset (order_dual Ξ±) _ _ _ _ lemma frontier_lt_subset_eq (hf : continuous f) (hg : continuous g) : frontier {b | f b < g b} βŠ† {b | f b = g b} := by rw ← frontier_compl; convert frontier_le_subset_eq hg hf; simp [ext_iff, eq_comm] lemma continuous_if_le [topological_space Ξ³] [Ξ  x, decidable (f x ≀ g x)] {f' g' : Ξ² β†’ Ξ³} (hf : continuous f) (hg : continuous g) (hf' : continuous_on f' {x | f x ≀ g x}) (hg' : continuous_on g' {x | g x ≀ f x}) (hfg : βˆ€ x, f x = g x β†’ f' x = g' x) : continuous (Ξ» x, if f x ≀ g x then f' x else g' x) := begin refine continuous_if (Ξ» a ha, hfg _ (frontier_le_subset_eq hf hg ha)) _ (hg'.mono _), { rwa [(is_closed_le hf hg).closure_eq] }, { simp only [not_le], exact closure_lt_subset_le hg hf } end lemma continuous.if_le [topological_space Ξ³] [Ξ  x, decidable (f x ≀ g x)] {f' g' : Ξ² β†’ Ξ³} (hf' : continuous f') (hg' : continuous g') (hf : continuous f) (hg : continuous g) (hfg : βˆ€ x, f x = g x β†’ f' x = g' x) : continuous (Ξ» x, if f x ≀ g x then f' x else g' x) := continuous_if_le hf hg hf'.continuous_on hg'.continuous_on hfg @[continuity] lemma continuous.min (hf : continuous f) (hg : continuous g) : continuous (Ξ»b, min (f b) (g b)) := hf.if_le hg hf hg (Ξ» x, id) @[continuity] lemma continuous.max (hf : continuous f) (hg : continuous g) : continuous (Ξ»b, max (f b) (g b)) := @continuous.min (order_dual Ξ±) _ _ _ _ _ _ _ hf hg end lemma continuous_min : continuous (Ξ» p : Ξ± Γ— Ξ±, min p.1 p.2) := continuous_fst.min continuous_snd lemma continuous_max : continuous (Ξ» p : Ξ± Γ— Ξ±, max p.1 p.2) := continuous_fst.max continuous_snd lemma filter.tendsto.max {b : filter Ξ²} {a₁ aβ‚‚ : Ξ±} (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 aβ‚‚)) : tendsto (Ξ»b, max (f b) (g b)) b (𝓝 (max a₁ aβ‚‚)) := (continuous_max.tendsto (a₁, aβ‚‚)).comp (hf.prod_mk_nhds hg) lemma filter.tendsto.min {b : filter Ξ²} {a₁ aβ‚‚ : Ξ±} (hf : tendsto f b (𝓝 a₁)) (hg : tendsto g b (𝓝 aβ‚‚)) : tendsto (Ξ»b, min (f b) (g b)) b (𝓝 (min a₁ aβ‚‚)) := (continuous_min.tendsto (a₁, aβ‚‚)).comp (hf.prod_mk_nhds hg) lemma is_preconnected.ord_connected {s : set Ξ±} (h : is_preconnected s) : ord_connected s := ⟨λ x hx y hy, h.Icc_subset hx hy⟩ end linear_order end order_closed_topology instance [preorder Ξ±] [topological_space Ξ±] [order_closed_topology Ξ±] [preorder Ξ²] [topological_space Ξ²] [order_closed_topology Ξ²] : order_closed_topology (Ξ± Γ— Ξ²) := ⟨(is_closed_le (continuous_fst.comp continuous_fst) (continuous_fst.comp continuous_snd)).inter (is_closed_le (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd))⟩ instance {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, preorder (Ξ± i)] [Ξ  i, topological_space (Ξ± i)] [Ξ  i, order_closed_topology (Ξ± i)] : order_closed_topology (Ξ  i, Ξ± i) := begin constructor, simp only [pi.le_def, set_of_forall], exact is_closed_Inter (Ξ» i, is_closed_le ((continuous_apply i).comp continuous_fst) ((continuous_apply i).comp continuous_snd)) end instance pi.order_closed_topology' [preorder Ξ²] [topological_space Ξ²] [order_closed_topology Ξ²] : order_closed_topology (Ξ± β†’ Ξ²) := pi.order_closed_topology /-- The order topology on an ordered type is the topology generated by open intervals. We register it on a preorder, but it is mostly interesting in linear orders, where it is also order-closed. We define it as a mixin. If you want to introduce the order topology on a preorder, use `preorder.topology`. -/ class order_topology (Ξ± : Type*) [t : topological_space Ξ±] [preorder Ξ±] : Prop := (topology_eq_generate_intervals : t = generate_from {s | βˆƒa, s = Ioi a ∨ s = Iio a}) /-- (Order) topology on a partial order `Ξ±` generated by the subbase of open intervals `(a, ∞) = { x ∣ a < x }, (-∞ , b) = {x ∣ x < b}` for all `a, b` in `Ξ±`. We do not register it as an instance as many ordered sets are already endowed with the same topology, most often in a non-defeq way though. Register as a local instance when necessary. -/ def preorder.topology (Ξ± : Type*) [preorder Ξ±] : topological_space Ξ± := generate_from {s : set Ξ± | βˆƒ (a : Ξ±), s = {b : Ξ± | a < b} ∨ s = {b : Ξ± | b < a}} section order_topology instance {Ξ± : Type*} [topological_space Ξ±] [partial_order Ξ±] [order_topology Ξ±] : order_topology (order_dual Ξ±) := ⟨by convert @order_topology.topology_eq_generate_intervals Ξ± _ _ _; conv in (_ ∨ _) { rw or.comm }; refl⟩ section partial_order variables [topological_space Ξ±] [partial_order Ξ±] [t : order_topology Ξ±] include t lemma is_open_iff_generate_intervals {s : set Ξ±} : is_open s ↔ generate_open {s | βˆƒa, s = Ioi a ∨ s = Iio a} s := by rw [t.topology_eq_generate_intervals]; refl lemma is_open_lt' (a : Ξ±) : is_open {b:Ξ± | a < b} := by rw [@is_open_iff_generate_intervals Ξ± _ _ t]; exact generate_open.basic _ ⟨a, or.inl rfl⟩ lemma is_open_gt' (a : Ξ±) : is_open {b:Ξ± | b < a} := by rw [@is_open_iff_generate_intervals Ξ± _ _ t]; exact generate_open.basic _ ⟨a, or.inr rfl⟩ lemma lt_mem_nhds {a b : Ξ±} (h : a < b) : βˆ€αΆ  x in 𝓝 b, a < x := is_open.mem_nhds (is_open_lt' _) h lemma le_mem_nhds {a b : Ξ±} (h : a < b) : βˆ€αΆ  x in 𝓝 b, a ≀ x := (𝓝 b).sets_of_superset (lt_mem_nhds h) $ assume b hb, le_of_lt hb lemma gt_mem_nhds {a b : Ξ±} (h : a < b) : βˆ€αΆ  x in 𝓝 a, x < b := is_open.mem_nhds (is_open_gt' _) h lemma ge_mem_nhds {a b : Ξ±} (h : a < b) : βˆ€αΆ  x in 𝓝 a, x ≀ b := (𝓝 a).sets_of_superset (gt_mem_nhds h) $ assume b hb, le_of_lt hb lemma nhds_eq_order (a : Ξ±) : 𝓝 a = (β¨…b ∈ Iio a, π“Ÿ (Ioi b)) βŠ“ (β¨…b ∈ Ioi a, π“Ÿ (Iio b)) := by rw [t.topology_eq_generate_intervals, nhds_generate_from]; from le_antisymm (le_inf (le_binfi $ assume b hb, infi_le_of_le {c : Ξ± | b < c} $ infi_le _ ⟨hb, b, or.inl rfl⟩) (le_binfi $ assume b hb, infi_le_of_le {c : Ξ± | c < b} $ infi_le _ ⟨hb, b, or.inr rfl⟩)) (le_infi $ assume s, le_infi $ assume ⟨ha, b, hs⟩, match s, ha, hs with | _, h, (or.inl rfl) := inf_le_of_left_le $ infi_le_of_le b $ infi_le _ h | _, h, (or.inr rfl) := inf_le_of_right_le $ infi_le_of_le b $ infi_le _ h end) lemma tendsto_order {f : Ξ² β†’ Ξ±} {a : Ξ±} {x : filter Ξ²} : tendsto f x (𝓝 a) ↔ (βˆ€ a' < a, βˆ€αΆ  b in x, a' < f b) ∧ (βˆ€ a' > a, βˆ€αΆ  b in x, f b < a') := by simp [nhds_eq_order a, tendsto_inf, tendsto_infi, tendsto_principal] instance tendsto_Icc_class_nhds (a : Ξ±) : tendsto_Ixx_class Icc (𝓝 a) (𝓝 a) := begin simp only [nhds_eq_order, infi_subtype'], refine ((has_basis_infi_principal_finite _).inf (has_basis_infi_principal_finite _)).tendsto_Ixx_class (Ξ» s hs, _), refine ((ord_connected_bInter _).inter (ord_connected_bInter _)).out; intros _ _, exacts [ord_connected_Ioi, ord_connected_Iio] end instance tendsto_Ico_class_nhds (a : Ξ±) : tendsto_Ixx_class Ico (𝓝 a) (𝓝 a) := tendsto_Ixx_class_of_subset (Ξ» _ _, Ico_subset_Icc_self) instance tendsto_Ioc_class_nhds (a : Ξ±) : tendsto_Ixx_class Ioc (𝓝 a) (𝓝 a) := tendsto_Ixx_class_of_subset (Ξ» _ _, Ioc_subset_Icc_self) instance tendsto_Ioo_class_nhds (a : Ξ±) : tendsto_Ixx_class Ioo (𝓝 a) (𝓝 a) := tendsto_Ixx_class_of_subset (Ξ» _ _, Ioo_subset_Icc_self) /-- Also known as squeeze or sandwich theorem. This version assumes that inequalities hold eventually for the filter. -/ lemma tendsto_of_tendsto_of_tendsto_of_le_of_le' {f g h : Ξ² β†’ Ξ±} {b : filter Ξ²} {a : Ξ±} (hg : tendsto g b (𝓝 a)) (hh : tendsto h b (𝓝 a)) (hgf : βˆ€αΆ  b in b, g b ≀ f b) (hfh : βˆ€αΆ  b in b, f b ≀ h b) : tendsto f b (𝓝 a) := tendsto_order.2 ⟨assume a' h', have βˆ€αΆ  b in b, a' < g b, from (tendsto_order.1 hg).left a' h', by filter_upwards [this, hgf] assume a, lt_of_lt_of_le, assume a' h', have βˆ€αΆ  b in b, h b < a', from (tendsto_order.1 hh).right a' h', by filter_upwards [this, hfh] assume a h₁ hβ‚‚, lt_of_le_of_lt hβ‚‚ hβ‚βŸ© /-- Also known as squeeze or sandwich theorem. This version assumes that inequalities hold everywhere. -/ lemma tendsto_of_tendsto_of_tendsto_of_le_of_le {f g h : Ξ² β†’ Ξ±} {b : filter Ξ²} {a : Ξ±} (hg : tendsto g b (𝓝 a)) (hh : tendsto h b (𝓝 a)) (hgf : g ≀ f) (hfh : f ≀ h) : tendsto f b (𝓝 a) := tendsto_of_tendsto_of_tendsto_of_le_of_le' hg hh (eventually_of_forall hgf) (eventually_of_forall hfh) lemma nhds_order_unbounded {a : Ξ±} (hu : βˆƒu, a < u) (hl : βˆƒl, l < a) : 𝓝 a = (β¨…l (hβ‚‚ : l < a) u (hβ‚‚ : a < u), π“Ÿ (Ioo l u)) := have βˆƒ u, u ∈ Ioi a, from hu, have βˆƒ l, l ∈ Iio a, from hl, by { simp only [nhds_eq_order, inf_binfi, binfi_inf, *, inf_principal, Ioi_inter_Iio], refl } lemma tendsto_order_unbounded {f : Ξ² β†’ Ξ±} {a : Ξ±} {x : filter Ξ²} (hu : βˆƒu, a < u) (hl : βˆƒl, l < a) (h : βˆ€l u, l < a β†’ a < u β†’ βˆ€αΆ  b in x, l < f b ∧ f b < u) : tendsto f x (𝓝 a) := by rw [nhds_order_unbounded hu hl]; from (tendsto_infi.2 $ assume l, tendsto_infi.2 $ assume hl, tendsto_infi.2 $ assume u, tendsto_infi.2 $ assume hu, tendsto_principal.2 $ h l u hl hu) end partial_order instance tendsto_Ixx_nhds_within {Ξ± : Type*} [preorder Ξ±] [topological_space Ξ±] (a : Ξ±) {s t : set Ξ±} {Ixx} [tendsto_Ixx_class Ixx (𝓝 a) (𝓝 a)] [tendsto_Ixx_class Ixx (π“Ÿ s) (π“Ÿ t)]: tendsto_Ixx_class Ixx (𝓝[s] a) (𝓝[t] a) := filter.tendsto_Ixx_class_inf instance tendsto_Icc_class_nhds_pi {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, partial_order (Ξ± i)] [Ξ  i, topological_space (Ξ± i)] [βˆ€ i, order_topology (Ξ± i)] (f : Ξ  i, Ξ± i) : tendsto_Ixx_class Icc (𝓝 f) (𝓝 f) := begin constructor, conv in ((𝓝 f).lift' powerset) { rw [nhds_pi] }, simp only [lift'_infi_powerset, comap_lift'_eq2 monotone_powerset, tendsto_infi, tendsto_lift', mem_powerset_iff, subset_def, mem_preimage], intros i s hs, have : tendsto (Ξ» g : Ξ  i, Ξ± i, g i) (𝓝 f) (𝓝 (f i)) := ((continuous_apply i).tendsto f), refine (tendsto_lift'.1 ((this.comp tendsto_fst).Icc (this.comp tendsto_snd)) s hs).mono _, exact Ξ» p hp g hg, hp ⟨hg.1 _, hg.2 _⟩ end theorem induced_order_topology' {Ξ± : Type u} {Ξ² : Type v} [partial_order Ξ±] [ta : topological_space Ξ²] [partial_order Ξ²] [order_topology Ξ²] (f : Ξ± β†’ Ξ²) (hf : βˆ€ {x y}, f x < f y ↔ x < y) (H₁ : βˆ€ {a x}, x < f a β†’ βˆƒ b < a, x ≀ f b) (Hβ‚‚ : βˆ€ {a x}, f a < x β†’ βˆƒ b > a, f b ≀ x) : @order_topology _ (induced f ta) _ := begin letI := induced f ta, refine ⟨eq_of_nhds_eq_nhds (Ξ» a, _)⟩, rw [nhds_induced, nhds_generate_from, nhds_eq_order (f a)], apply le_antisymm, { refine le_infi (Ξ» s, le_infi $ Ξ» hs, le_principal_iff.2 _), rcases hs with ⟨ab, b, rfl|rfl⟩, { exact mem_comap.2 ⟨{x | f b < x}, mem_inf_of_left $ mem_infi_of_mem _ $ mem_infi_of_mem (hf.2 ab) $ mem_principal_self _, Ξ» x, hf.1⟩ }, { exact mem_comap.2 ⟨{x | x < f b}, mem_inf_of_right $ mem_infi_of_mem _ $ mem_infi_of_mem (hf.2 ab) $ mem_principal_self _, Ξ» x, hf.1⟩ } }, { rw [← map_le_iff_le_comap], refine le_inf _ _; refine le_infi (Ξ» x, le_infi $ Ξ» h, le_principal_iff.2 _); simp, { rcases H₁ h with ⟨b, ab, xb⟩, refine mem_infi_of_mem _ (mem_infi_of_mem ⟨ab, b, or.inl rfl⟩ (mem_principal.2 _)), exact Ξ» c hc, lt_of_le_of_lt xb (hf.2 hc) }, { rcases Hβ‚‚ h with ⟨b, ab, xb⟩, refine mem_infi_of_mem _ (mem_infi_of_mem ⟨ab, b, or.inr rfl⟩ (mem_principal.2 _)), exact Ξ» c hc, lt_of_lt_of_le (hf.2 hc) xb } }, end theorem induced_order_topology {Ξ± : Type u} {Ξ² : Type v} [partial_order Ξ±] [ta : topological_space Ξ²] [partial_order Ξ²] [order_topology Ξ²] (f : Ξ± β†’ Ξ²) (hf : βˆ€ {x y}, f x < f y ↔ x < y) (H : βˆ€ {x y}, x < y β†’ βˆƒ a, x < f a ∧ f a < y) : @order_topology _ (induced f ta) _ := induced_order_topology' f @hf (Ξ» a x xa, let ⟨b, xb, ba⟩ := H xa in ⟨b, hf.1 ba, le_of_lt xb⟩) (Ξ» a x ax, let ⟨b, ab, bx⟩ := H ax in ⟨b, hf.1 ab, le_of_lt bx⟩) /-- On an `ord_connected` subset of a linear order, the order topology for the restriction of the order is the same as the restriction to the subset of the order topology. -/ instance order_topology_of_ord_connected {Ξ± : Type u} [ta : topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] {t : set Ξ±} [ht : ord_connected t] : order_topology t := begin letI := induced (coe : t β†’ Ξ±) ta, refine ⟨eq_of_nhds_eq_nhds (Ξ» a, _)⟩, rw [nhds_induced, nhds_generate_from, nhds_eq_order (a : Ξ±)], apply le_antisymm, { refine le_infi (Ξ» s, le_infi $ Ξ» hs, le_principal_iff.2 _), rcases hs with ⟨ab, b, rfl|rfl⟩, { refine ⟨Ioi b, _, Ξ» _, id⟩, refine mem_inf_of_left (mem_infi_of_mem b _), exact mem_infi_of_mem ab (mem_principal_self (Ioi ↑b)) }, { refine ⟨Iio b, _, Ξ» _, id⟩, refine mem_inf_of_right (mem_infi_of_mem b _), exact mem_infi_of_mem ab (mem_principal_self (Iio b)) } }, { rw [← map_le_iff_le_comap], refine le_inf _ _, { refine le_infi (Ξ» x, le_infi $ Ξ» h, le_principal_iff.2 _), by_cases hx : x ∈ t, { refine mem_infi_of_mem (Ioi ⟨x, hx⟩) (mem_infi_of_mem ⟨h, ⟨⟨x, hx⟩, or.inl rfl⟩⟩ _), exact Ξ» _, id }, simp only [set_coe.exists, mem_set_of_eq, mem_map'], convert univ_sets _, suffices hx' : βˆ€ (y : t), ↑y ∈ Ioi x, { simp [hx'] }, intros y, revert hx, contrapose!, -- here we use the `ord_connected` hypothesis exact Ξ» hx, ht.out y.2 a.2 ⟨le_of_not_gt hx, le_of_lt h⟩ }, { refine le_infi (Ξ» x, le_infi $ Ξ» h, le_principal_iff.2 _), by_cases hx : x ∈ t, { refine mem_infi_of_mem (Iio ⟨x, hx⟩) (mem_infi_of_mem ⟨h, ⟨⟨x, hx⟩, or.inr rfl⟩⟩ _), exact Ξ» _, id }, simp only [set_coe.exists, mem_set_of_eq, mem_map'], convert univ_sets _, suffices hx' : βˆ€ (y : t), ↑y ∈ Iio x, { simp [hx'] }, intros y, revert hx, contrapose!, -- here we use the `ord_connected` hypothesis exact Ξ» hx, ht.out a.2 y.2 ⟨le_of_lt h, le_of_not_gt hx⟩ } } end lemma nhds_top_order [topological_space Ξ±] [order_top Ξ±] [order_topology Ξ±] : 𝓝 (⊀:Ξ±) = (β¨…l (hβ‚‚ : l < ⊀), π“Ÿ (Ioi l)) := by simp [nhds_eq_order (⊀:Ξ±)] lemma nhds_bot_order [topological_space Ξ±] [order_bot Ξ±] [order_topology Ξ±] : 𝓝 (βŠ₯:Ξ±) = (β¨…l (hβ‚‚ : βŠ₯ < l), π“Ÿ (Iio l)) := by simp [nhds_eq_order (βŠ₯:Ξ±)] lemma nhds_top_basis [topological_space Ξ±] [semilattice_sup_top Ξ±] [is_total Ξ± has_le.le] [order_topology Ξ±] [nontrivial Ξ±] : (𝓝 ⊀).has_basis (Ξ» a : Ξ±, a < ⊀) (Ξ» a : Ξ±, Ioi a) := ⟨ begin simp only [nhds_top_order], refine @filter.mem_binfi_of_directed Ξ± Ξ± (Ξ» a, π“Ÿ (Ioi a)) (Ξ» a, a < ⊀) _ _, { rintros a (ha : a < ⊀) b (hb : b < ⊀), use a βŠ” b, simp only [filter.le_principal_iff, ge_iff_le, order.preimage], exact ⟨sup_lt_iff.mpr ⟨ha, hb⟩, Ioi_subset_Ioi le_sup_left, Ioi_subset_Ioi le_sup_right⟩ }, { obtain ⟨a, ha⟩ : βˆƒ a : Ξ±, a β‰  ⊀ := exists_ne ⊀, exact ⟨a, lt_top_iff_ne_top.mpr ha⟩ } end ⟩ lemma nhds_bot_basis [topological_space Ξ±] [semilattice_inf_bot Ξ±] [is_total Ξ± has_le.le] [order_topology Ξ±] [nontrivial Ξ±] : (𝓝 βŠ₯).has_basis (Ξ» a : Ξ±, βŠ₯ < a) (Ξ» a : Ξ±, Iio a) := @nhds_top_basis (order_dual Ξ±) _ _ _ _ _ lemma nhds_top_basis_Ici [topological_space Ξ±] [semilattice_sup_top Ξ±] [is_total Ξ± has_le.le] [order_topology Ξ±] [nontrivial Ξ±] [densely_ordered Ξ±] : (𝓝 ⊀).has_basis (Ξ» a : Ξ±, a < ⊀) Ici := nhds_top_basis.to_has_basis (Ξ» a ha, let ⟨b, hab, hb⟩ := exists_between ha in ⟨b, hb, Ici_subset_Ioi.mpr hab⟩) (Ξ» a ha, ⟨a, ha, Ioi_subset_Ici_self⟩) lemma nhds_bot_basis_Iic [topological_space Ξ±] [semilattice_inf_bot Ξ±] [is_total Ξ± has_le.le] [order_topology Ξ±] [nontrivial Ξ±] [densely_ordered Ξ±] : (𝓝 βŠ₯).has_basis (Ξ» a : Ξ±, βŠ₯ < a) Iic := @nhds_top_basis_Ici (order_dual Ξ±) _ _ _ _ _ _ lemma tendsto_nhds_top_mono [topological_space Ξ²] [order_top Ξ²] [order_topology Ξ²] {l : filter Ξ±} {f g : Ξ± β†’ Ξ²} (hf : tendsto f l (𝓝 ⊀)) (hg : f ≀ᢠ[l] g) : tendsto g l (𝓝 ⊀) := begin simp only [nhds_top_order, tendsto_infi, tendsto_principal] at hf ⊒, intros x hx, filter_upwards [hf x hx, hg], exact Ξ» x, lt_of_lt_of_le end lemma tendsto_nhds_bot_mono [topological_space Ξ²] [order_bot Ξ²] [order_topology Ξ²] {l : filter Ξ±} {f g : Ξ± β†’ Ξ²} (hf : tendsto f l (𝓝 βŠ₯)) (hg : g ≀ᢠ[l] f) : tendsto g l (𝓝 βŠ₯) := @tendsto_nhds_top_mono Ξ± (order_dual Ξ²) _ _ _ _ _ _ hf hg lemma tendsto_nhds_top_mono' [topological_space Ξ²] [order_top Ξ²] [order_topology Ξ²] {l : filter Ξ±} {f g : Ξ± β†’ Ξ²} (hf : tendsto f l (𝓝 ⊀)) (hg : f ≀ g) : tendsto g l (𝓝 ⊀) := tendsto_nhds_top_mono hf (eventually_of_forall hg) lemma tendsto_nhds_bot_mono' [topological_space Ξ²] [order_bot Ξ²] [order_topology Ξ²] {l : filter Ξ±} {f g : Ξ± β†’ Ξ²} (hf : tendsto f l (𝓝 βŠ₯)) (hg : g ≀ f) : tendsto g l (𝓝 βŠ₯) := tendsto_nhds_bot_mono hf (eventually_of_forall hg) section linear_order variables [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] lemma exists_Ioc_subset_of_mem_nhds' {a : Ξ±} {s : set Ξ±} (hs : s ∈ 𝓝 a) {l : Ξ±} (hl : l < a) : βˆƒ l' ∈ Ico l a, Ioc l' a βŠ† s := begin rw [nhds_eq_order a] at hs, rcases hs with ⟨t₁, ht₁, tβ‚‚, htβ‚‚, rfl⟩, -- First we show that `tβ‚‚` includes `(-∞, a]`, so it suffices to show `(l', ∞) βŠ† t₁` suffices : βˆƒ l' ∈ Ico l a, Ioi l' βŠ† t₁, { have A : π“Ÿ (Iic a) ≀ β¨… b ∈ Ioi a, π“Ÿ (Iio b), from (le_infi $ Ξ» b, le_infi $ Ξ» hb, principal_mono.2 $ Iic_subset_Iio.2 hb), have B : t₁ ∩ Iic a βŠ† t₁ ∩ tβ‚‚, from inter_subset_inter_right _ (A htβ‚‚), from this.imp (Ξ» l', Exists.imp $ Ξ» hl' hl x hx, B ⟨hl hx.1, hx.2⟩) }, clear htβ‚‚ tβ‚‚, -- Now we find `l` such that `(l', ∞) βŠ† t₁` rw [mem_binfi_of_directed] at ht₁, { rcases ht₁ with ⟨b, hb, hb'⟩, exact ⟨max b l, ⟨le_max_right _ _, max_lt hb hl⟩, Ξ» x hx, hb' $ Ioi_subset_Ioi (le_max_left _ _) hx⟩ }, { intros b hb b' hb', simp only [mem_Iio] at hb hb', use [max b b', max_lt hb hb'], simp [le_refl] }, exact ⟨l, hl⟩ end lemma exists_Ico_subset_of_mem_nhds' {a : Ξ±} {s : set Ξ±} (hs : s ∈ 𝓝 a) {u : Ξ±} (hu : a < u) : βˆƒ u' ∈ Ioc a u, Ico a u' βŠ† s := begin convert @exists_Ioc_subset_of_mem_nhds' (order_dual Ξ±) _ _ _ _ _ hs _ hu, ext, rw [dual_Ico, dual_Ioc] end lemma exists_Ioc_subset_of_mem_nhds {a : Ξ±} {s : set Ξ±} (hs : s ∈ 𝓝 a) (h : βˆƒ l, l < a) : βˆƒ l < a, Ioc l a βŠ† s := let ⟨l', hl'⟩ := h in let ⟨l, hl⟩ := exists_Ioc_subset_of_mem_nhds' hs hl' in ⟨l, hl.fst.2, hl.snd⟩ lemma exists_Ico_subset_of_mem_nhds {a : Ξ±} {s : set Ξ±} (hs : s ∈ 𝓝 a) (h : βˆƒ u, a < u) : βˆƒ u (_ : a < u), Ico a u βŠ† s := let ⟨l', hl'⟩ := h in let ⟨l, hl⟩ := exists_Ico_subset_of_mem_nhds' hs hl' in ⟨l, hl.fst.1, hl.snd⟩ lemma order_separated {a₁ aβ‚‚ : Ξ±} (h : a₁ < aβ‚‚) : βˆƒu v : set Ξ±, is_open u ∧ is_open v ∧ a₁ ∈ u ∧ aβ‚‚ ∈ v ∧ (βˆ€bβ‚βˆˆu, βˆ€bβ‚‚βˆˆv, b₁ < bβ‚‚) := match dense_or_discrete a₁ aβ‚‚ with | or.inl ⟨a, ha₁, haβ‚‚βŸ© := ⟨{a' | a' < a}, {a' | a < a'}, is_open_gt' a, is_open_lt' a, ha₁, haβ‚‚, assume b₁ h₁ bβ‚‚ hβ‚‚, lt_trans h₁ hβ‚‚βŸ© | or.inr ⟨h₁, hβ‚‚βŸ© := ⟨{a | a < aβ‚‚}, {a | a₁ < a}, is_open_gt' aβ‚‚, is_open_lt' a₁, h, h, assume b₁ hb₁ bβ‚‚ hbβ‚‚, calc b₁ ≀ a₁ : hβ‚‚ _ hb₁ ... < aβ‚‚ : h ... ≀ bβ‚‚ : h₁ _ hbβ‚‚βŸ© end @[priority 100] -- see Note [lower instance priority] instance order_topology.to_order_closed_topology : order_closed_topology Ξ± := { is_closed_le' := is_open_compl_iff.1 $ is_open_prod_iff.mpr $ assume a₁ aβ‚‚ (h : Β¬ a₁ ≀ aβ‚‚), have h : aβ‚‚ < a₁, from lt_of_not_ge h, let ⟨u, v, hu, hv, ha₁, haβ‚‚, h⟩ := order_separated h in ⟨v, u, hv, hu, haβ‚‚, ha₁, assume ⟨b₁, bβ‚‚βŸ© ⟨h₁, hβ‚‚βŸ©, not_le_of_gt $ h bβ‚‚ hβ‚‚ b₁ hβ‚βŸ© } lemma order_topology.t2_space : t2_space Ξ± := by apply_instance @[priority 100] -- see Note [lower instance priority] instance order_topology.regular_space : regular_space Ξ± := { regular := assume s a hs ha, have hs' : sᢜ ∈ 𝓝 a, from is_open.mem_nhds hs.is_open_compl ha, have βˆƒt:set Ξ±, is_open t ∧ (βˆ€l∈ s, l < a β†’ l ∈ t) ∧ 𝓝[t] a = βŠ₯, from by_cases (assume h : βˆƒl, l < a, let ⟨l, hl, h⟩ := exists_Ioc_subset_of_mem_nhds hs' h in match dense_or_discrete l a with | or.inl ⟨b, hb₁, hbβ‚‚βŸ© := ⟨{a | a < b}, is_open_gt' _, assume c hcs hca, show c < b, from lt_of_not_ge $ assume hbc, h ⟨lt_of_lt_of_le hb₁ hbc, le_of_lt hca⟩ hcs, inf_principal_eq_bot.2 $ (𝓝 a).sets_of_superset ((is_open_lt' _).mem_nhds hbβ‚‚) $ assume x (hx : b < x), show Β¬ x < b, from not_lt.2 $ le_of_lt hx⟩ | or.inr ⟨h₁, hβ‚‚βŸ© := ⟨{a' | a' < a}, is_open_gt' _, assume b hbs hba, hba, inf_principal_eq_bot.2 $ (𝓝 a).sets_of_superset ((is_open_lt' _).mem_nhds hl) $ assume x (hx : l < x), show Β¬ x < a, from not_lt.2 $ h₁ _ hx⟩ end) (assume : Β¬ βˆƒl, l < a, βŸ¨βˆ…, is_open_empty, assume l _ hl, (this ⟨l, hl⟩).elim, nhds_within_empty _⟩), let ⟨t₁, ht₁o, ht₁s, ht₁a⟩ := this in have βˆƒt:set Ξ±, is_open t ∧ (βˆ€u∈ s, u>a β†’ u ∈ t) ∧ 𝓝[t] a = βŠ₯, from by_cases (assume h : βˆƒu, u > a, let ⟨u, hu, h⟩ := exists_Ico_subset_of_mem_nhds hs' h in match dense_or_discrete a u with | or.inl ⟨b, hb₁, hbβ‚‚βŸ© := ⟨{a | b < a}, is_open_lt' _, assume c hcs hca, show c > b, from lt_of_not_ge $ assume hbc, h ⟨le_of_lt hca, lt_of_le_of_lt hbc hbβ‚‚βŸ© hcs, inf_principal_eq_bot.2 $ (𝓝 a).sets_of_superset ((is_open_gt' _).mem_nhds hb₁) $ assume x (hx : b > x), show Β¬ x > b, from not_lt.2 $ le_of_lt hx⟩ | or.inr ⟨h₁, hβ‚‚βŸ© := ⟨{a' | a' > a}, is_open_lt' _, assume b hbs hba, hba, inf_principal_eq_bot.2 $ (𝓝 a).sets_of_superset ((is_open_gt' _).mem_nhds hu) $ assume x (hx : u > x), show Β¬ x > a, from not_lt.2 $ hβ‚‚ _ hx⟩ end) (assume : Β¬ βˆƒu, u > a, βŸ¨βˆ…, is_open_empty, assume l _ hl, (this ⟨l, hl⟩).elim, nhds_within_empty _⟩), let ⟨tβ‚‚, htβ‚‚o, htβ‚‚s, htβ‚‚a⟩ := this in ⟨t₁ βˆͺ tβ‚‚, is_open.union ht₁o htβ‚‚o, assume x hx, have x β‰  a, from assume eq, ha $ eq β–Έ hx, (ne_iff_lt_or_gt.mp this).imp (ht₁s _ hx) (htβ‚‚s _ hx), by rw [nhds_within_union, ht₁a, htβ‚‚a, bot_sup_eq]⟩, ..order_topology.t2_space } /-- A set is a neighborhood of `a` if and only if it contains an interval `(l, u)` containing `a`, provided `a` is neither a bottom element nor a top element. -/ lemma mem_nhds_iff_exists_Ioo_subset' {a : Ξ±} {s : set Ξ±} (hl : βˆƒ l, l < a) (hu : βˆƒ u, a < u) : s ∈ 𝓝 a ↔ βˆƒl u, a ∈ Ioo l u ∧ Ioo l u βŠ† s := begin split, { assume h, rcases exists_Ico_subset_of_mem_nhds h hu with ⟨u, au, hu⟩, rcases exists_Ioc_subset_of_mem_nhds h hl with ⟨l, la, hl⟩, refine ⟨l, u, ⟨la, au⟩, Ξ»x hx, _⟩, cases le_total a x with hax hax, { exact hu ⟨hax, hx.2⟩ }, { exact hl ⟨hx.1, hax⟩ } }, { rintros ⟨l, u, ha, h⟩, apply mem_of_superset (is_open.mem_nhds is_open_Ioo ha) h } end /-- A set is a neighborhood of `a` if and only if it contains an interval `(l, u)` containing `a`. -/ lemma mem_nhds_iff_exists_Ioo_subset [no_top_order Ξ±] [no_bot_order Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝 a ↔ βˆƒl u, a ∈ Ioo l u ∧ Ioo l u βŠ† s := mem_nhds_iff_exists_Ioo_subset' (no_bot a) (no_top a) lemma nhds_basis_Ioo' {a : Ξ±} (hl : βˆƒ l, l < a) (hu : βˆƒ u, a < u) : (𝓝 a).has_basis (Ξ» b : Ξ± Γ— Ξ±, b.1 < a ∧ a < b.2) (Ξ» b, Ioo b.1 b.2) := ⟨λ s, (mem_nhds_iff_exists_Ioo_subset' hl hu).trans $ by simp⟩ lemma nhds_basis_Ioo [no_top_order Ξ±] [no_bot_order Ξ±] (a : Ξ±) : (𝓝 a).has_basis (Ξ» b : Ξ± Γ— Ξ±, b.1 < a ∧ a < b.2) (Ξ» b, Ioo b.1 b.2) := nhds_basis_Ioo' (no_bot a) (no_top a) lemma filter.eventually.exists_Ioo_subset [no_top_order Ξ±] [no_bot_order Ξ±] {a : Ξ±} {p : Ξ± β†’ Prop} (hp : βˆ€αΆ  x in 𝓝 a, p x) : βˆƒ l u, a ∈ Ioo l u ∧ Ioo l u βŠ† {x | p x} := mem_nhds_iff_exists_Ioo_subset.1 hp lemma Iio_mem_nhds {a b : Ξ±} (h : a < b) : Iio b ∈ 𝓝 a := is_open.mem_nhds is_open_Iio h lemma Ioi_mem_nhds {a b : Ξ±} (h : a < b) : Ioi a ∈ 𝓝 b := is_open.mem_nhds is_open_Ioi h lemma Iic_mem_nhds {a b : Ξ±} (h : a < b) : Iic b ∈ 𝓝 a := mem_of_superset (Iio_mem_nhds h) Iio_subset_Iic_self lemma Ici_mem_nhds {a b : Ξ±} (h : a < b) : Ici a ∈ 𝓝 b := mem_of_superset (Ioi_mem_nhds h) Ioi_subset_Ici_self lemma Ioo_mem_nhds {a b x : Ξ±} (ha : a < x) (hb : x < b) : Ioo a b ∈ 𝓝 x := is_open.mem_nhds is_open_Ioo ⟨ha, hb⟩ lemma Ioc_mem_nhds {a b x : Ξ±} (ha : a < x) (hb : x < b) : Ioc a b ∈ 𝓝 x := mem_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Ioc_self lemma Ico_mem_nhds {a b x : Ξ±} (ha : a < x) (hb : x < b) : Ico a b ∈ 𝓝 x := mem_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Ico_self lemma Icc_mem_nhds {a b x : Ξ±} (ha : a < x) (hb : x < b) : Icc a b ∈ 𝓝 x := mem_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Icc_self section pi /-! ### Intervals in `Ξ  i, Ο€ i` belong to `𝓝 x` For each lemma `pi_Ixx_mem_nhds` we add a non-dependent version `pi_Ixx_mem_nhds'` because sometimes Lean fails to unify different instances while trying to apply the dependent version to, e.g., `ΞΉ β†’ ℝ`. -/ variables {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} [fintype ΞΉ] [Ξ  i, linear_order (Ο€ i)] [Ξ  i, topological_space (Ο€ i)] [βˆ€ i, order_topology (Ο€ i)] {a b x : Ξ  i, Ο€ i} {a' b' x' : ΞΉ β†’ Ξ±} lemma pi_Iic_mem_nhds (ha : βˆ€ i, x i < a i) : Iic a ∈ 𝓝 x := pi_univ_Iic a β–Έ set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, Iic_mem_nhds (ha _)) lemma pi_Iic_mem_nhds' (ha : βˆ€ i, x' i < a' i) : Iic a' ∈ 𝓝 x' := pi_Iic_mem_nhds ha lemma pi_Ici_mem_nhds (ha : βˆ€ i, a i < x i) : Ici a ∈ 𝓝 x := pi_univ_Ici a β–Έ set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, Ici_mem_nhds (ha _)) lemma pi_Ici_mem_nhds' (ha : βˆ€ i, a' i < x' i) : Ici a' ∈ 𝓝 x' := pi_Ici_mem_nhds ha lemma pi_Icc_mem_nhds (ha : βˆ€ i, a i < x i) (hb : βˆ€ i, x i < b i) : Icc a b ∈ 𝓝 x := pi_univ_Icc a b β–Έ set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, Icc_mem_nhds (ha _) (hb _)) lemma pi_Icc_mem_nhds' (ha : βˆ€ i, a' i < x' i) (hb : βˆ€ i, x' i < b' i) : Icc a' b' ∈ 𝓝 x' := pi_Icc_mem_nhds ha hb variables [nonempty ΞΉ] lemma pi_Iio_mem_nhds (ha : βˆ€ i, x i < a i) : Iio a ∈ 𝓝 x := begin refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, _)) (pi_univ_Iio_subset a), exact Iio_mem_nhds (ha i) end lemma pi_Iio_mem_nhds' (ha : βˆ€ i, x' i < a' i) : Iio a' ∈ 𝓝 x' := pi_Iio_mem_nhds ha lemma pi_Ioi_mem_nhds (ha : βˆ€ i, a i < x i) : Ioi a ∈ 𝓝 x := @pi_Iio_mem_nhds ΞΉ (Ξ» i, order_dual (Ο€ i)) _ _ _ _ _ _ _ ha lemma pi_Ioi_mem_nhds' (ha : βˆ€ i, a' i < x' i) : Ioi a' ∈ 𝓝 x' := pi_Ioi_mem_nhds ha lemma pi_Ioc_mem_nhds (ha : βˆ€ i, a i < x i) (hb : βˆ€ i, x i < b i) : Ioc a b ∈ 𝓝 x := begin refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, _)) (pi_univ_Ioc_subset a b), exact Ioc_mem_nhds (ha i) (hb i) end lemma pi_Ioc_mem_nhds' (ha : βˆ€ i, a' i < x' i) (hb : βˆ€ i, x' i < b' i) : Ioc a' b' ∈ 𝓝 x' := pi_Ioc_mem_nhds ha hb lemma pi_Ico_mem_nhds (ha : βˆ€ i, a i < x i) (hb : βˆ€ i, x i < b i) : Ico a b ∈ 𝓝 x := begin refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, _)) (pi_univ_Ico_subset a b), exact Ico_mem_nhds (ha i) (hb i) end lemma pi_Ico_mem_nhds' (ha : βˆ€ i, a' i < x' i) (hb : βˆ€ i, x' i < b' i) : Ico a' b' ∈ 𝓝 x' := pi_Ico_mem_nhds ha hb lemma pi_Ioo_mem_nhds (ha : βˆ€ i, a i < x i) (hb : βˆ€ i, x i < b i) : Ioo a b ∈ 𝓝 x := begin refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, _)) (pi_univ_Ioo_subset a b), exact Ioo_mem_nhds (ha i) (hb i) end lemma pi_Ioo_mem_nhds' (ha : βˆ€ i, a' i < x' i) (hb : βˆ€ i, x' i < b' i) : Ioo a' b' ∈ 𝓝 x' := pi_Ioo_mem_nhds ha hb end pi lemma disjoint_nhds_at_top [no_top_order Ξ±] (x : Ξ±) : disjoint (𝓝 x) at_top := begin rw filter.disjoint_iff, cases no_top x with a ha, use [Iio a, Iio_mem_nhds ha, Ici a, mem_at_top a], rw [inter_comm, Ici_inter_Iio, Ico_self] end @[simp] lemma inf_nhds_at_top [no_top_order Ξ±] (x : Ξ±) : 𝓝 x βŠ“ at_top = βŠ₯ := disjoint_iff.1 (disjoint_nhds_at_top x) lemma disjoint_nhds_at_bot [no_bot_order Ξ±] (x : Ξ±) : disjoint (𝓝 x) at_bot := @disjoint_nhds_at_top (order_dual Ξ±) _ _ _ _ x @[simp] lemma inf_nhds_at_bot [no_bot_order Ξ±] (x : Ξ±) : 𝓝 x βŠ“ at_bot = βŠ₯ := @inf_nhds_at_top (order_dual Ξ±) _ _ _ _ x lemma not_tendsto_nhds_of_tendsto_at_top [no_top_order Ξ±] {F : filter Ξ²} [ne_bot F] {f : Ξ² β†’ Ξ±} (hf : tendsto f F at_top) (x : Ξ±) : Β¬ tendsto f F (𝓝 x) := hf.not_tendsto (disjoint_nhds_at_top x).symm lemma not_tendsto_at_top_of_tendsto_nhds [no_top_order Ξ±] {F : filter Ξ²} [ne_bot F] {f : Ξ² β†’ Ξ±} {x : Ξ±} (hf : tendsto f F (𝓝 x)) : Β¬ tendsto f F at_top := hf.not_tendsto (disjoint_nhds_at_top x) lemma not_tendsto_nhds_of_tendsto_at_bot [no_bot_order Ξ±] {F : filter Ξ²} [ne_bot F] {f : Ξ² β†’ Ξ±} (hf : tendsto f F at_bot) (x : Ξ±) : Β¬ tendsto f F (𝓝 x) := hf.not_tendsto (disjoint_nhds_at_bot x).symm lemma not_tendsto_at_bot_of_tendsto_nhds [no_bot_order Ξ±] {F : filter Ξ²} [ne_bot F] {f : Ξ² β†’ Ξ±} {x : Ξ±} (hf : tendsto f F (𝓝 x)) : Β¬ tendsto f F at_bot := hf.not_tendsto (disjoint_nhds_at_bot x) /-! ### Neighborhoods to the left and to the right on an `order_topology` We've seen some properties of left and right neighborhood of a point in an `order_closed_topology`. In an `order_topology`, such neighborhoods can be characterized as the sets containing suitable intervals to the right or to the left of `a`. We give now these characterizations. -/ -- NB: If you extend the list, append to the end please to avoid breaking the API /-- The following statements are equivalent: 0. `s` is a neighborhood of `a` within `(a, +∞)` 1. `s` is a neighborhood of `a` within `(a, b]` 2. `s` is a neighborhood of `a` within `(a, b)` 3. `s` includes `(a, u)` for some `u ∈ (a, b]` 4. `s` includes `(a, u)` for some `u > a` -/ lemma tfae_mem_nhds_within_Ioi {a b : Ξ±} (hab : a < b) (s : set Ξ±) : tfae [s ∈ 𝓝[Ioi a] a, -- 0 : `s` is a neighborhood of `a` within `(a, +∞)` s ∈ 𝓝[Ioc a b] a, -- 1 : `s` is a neighborhood of `a` within `(a, b]` s ∈ 𝓝[Ioo a b] a, -- 2 : `s` is a neighborhood of `a` within `(a, b)` βˆƒ u ∈ Ioc a b, Ioo a u βŠ† s, -- 3 : `s` includes `(a, u)` for some `u ∈ (a, b]` βˆƒ u ∈ Ioi a, Ioo a u βŠ† s] := -- 4 : `s` includes `(a, u)` for some `u > a` begin tfae_have : 1 ↔ 2, by rw [nhds_within_Ioc_eq_nhds_within_Ioi hab], tfae_have : 1 ↔ 3, by rw [nhds_within_Ioo_eq_nhds_within_Ioi hab], tfae_have : 4 β†’ 5, from Ξ» ⟨u, umem, hu⟩, ⟨u, umem.1, hu⟩, tfae_have : 5 β†’ 1, { rintros ⟨u, hau, hu⟩, exact mem_of_superset (Ioo_mem_nhds_within_Ioi ⟨le_refl a, hau⟩) hu }, tfae_have : 1 β†’ 4, { assume h, rcases mem_nhds_within_iff_exists_mem_nhds_inter.1 h with ⟨v, va, hv⟩, rcases exists_Ico_subset_of_mem_nhds' va hab with ⟨u, au, hu⟩, refine ⟨u, au, Ξ»x hx, _⟩, refine hv ⟨hu ⟨le_of_lt hx.1, hx.2⟩, _⟩, exact hx.1 }, tfae_finish end lemma mem_nhds_within_Ioi_iff_exists_mem_Ioc_Ioo_subset {a u' : Ξ±} {s : set Ξ±} (hu' : a < u') : s ∈ 𝓝[Ioi a] a ↔ βˆƒu ∈ Ioc a u', Ioo a u βŠ† s := (tfae_mem_nhds_within_Ioi hu' s).out 0 3 /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u)` with `a < u < u'`, provided `a` is not a top element. -/ lemma mem_nhds_within_Ioi_iff_exists_Ioo_subset' {a u' : Ξ±} {s : set Ξ±} (hu' : a < u') : s ∈ 𝓝[Ioi a] a ↔ βˆƒu ∈ Ioi a, Ioo a u βŠ† s := (tfae_mem_nhds_within_Ioi hu' s).out 0 4 /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u)` with `a < u`. -/ lemma mem_nhds_within_Ioi_iff_exists_Ioo_subset [no_top_order Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝[Ioi a] a ↔ βˆƒu ∈ Ioi a, Ioo a u βŠ† s := let ⟨u', hu'⟩ := no_top a in mem_nhds_within_Ioi_iff_exists_Ioo_subset' hu' /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u]` with `a < u`. -/ lemma mem_nhds_within_Ioi_iff_exists_Ioc_subset [no_top_order Ξ±] [densely_ordered Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝[Ioi a] a ↔ βˆƒu ∈ Ioi a, Ioc a u βŠ† s := begin rw mem_nhds_within_Ioi_iff_exists_Ioo_subset, split, { rintros ⟨u, au, as⟩, rcases exists_between au with ⟨v, hv⟩, exact ⟨v, hv.1, Ξ»x hx, as ⟨hx.1, lt_of_le_of_lt hx.2 hv.2⟩⟩ }, { rintros ⟨u, au, as⟩, exact ⟨u, au, subset.trans Ioo_subset_Ioc_self as⟩ } end /-- The following statements are equivalent: 0. `s` is a neighborhood of `b` within `(-∞, b)` 1. `s` is a neighborhood of `b` within `[a, b)` 2. `s` is a neighborhood of `b` within `(a, b)` 3. `s` includes `(l, b)` for some `l ∈ [a, b)` 4. `s` includes `(l, b)` for some `l < b` -/ lemma tfae_mem_nhds_within_Iio {a b : Ξ±} (h : a < b) (s : set Ξ±) : tfae [s ∈ 𝓝[Iio b] b, -- 0 : `s` is a neighborhood of `b` within `(-∞, b)` s ∈ 𝓝[Ico a b] b, -- 1 : `s` is a neighborhood of `b` within `[a, b)` s ∈ 𝓝[Ioo a b] b, -- 2 : `s` is a neighborhood of `b` within `(a, b)` βˆƒ l ∈ Ico a b, Ioo l b βŠ† s, -- 3 : `s` includes `(l, b)` for some `l ∈ [a, b)` βˆƒ l ∈ Iio b, Ioo l b βŠ† s] := -- 4 : `s` includes `(l, b)` for some `l < b` begin have := @tfae_mem_nhds_within_Ioi (order_dual Ξ±) _ _ _ _ _ h s, -- If we call `convert` here, it generates wrong equations, so we need to simplify first simp only [exists_prop] at this ⊒, rw [dual_Ioi, dual_Ioc, dual_Ioo] at this, convert this; ext l; rw [dual_Ioo] end lemma mem_nhds_within_Iio_iff_exists_mem_Ico_Ioo_subset {a l' : Ξ±} {s : set Ξ±} (hl' : l' < a) : s ∈ 𝓝[Iio a] a ↔ βˆƒl ∈ Ico l' a, Ioo l a βŠ† s := (tfae_mem_nhds_within_Iio hl' s).out 0 3 /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `(l, a)` with `l < a`, provided `a` is not a bottom element. -/ lemma mem_nhds_within_Iio_iff_exists_Ioo_subset' {a l' : Ξ±} {s : set Ξ±} (hl' : l' < a) : s ∈ 𝓝[Iio a] a ↔ βˆƒl ∈ Iio a, Ioo l a βŠ† s := (tfae_mem_nhds_within_Iio hl' s).out 0 4 /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `(l, a)` with `l < a`. -/ lemma mem_nhds_within_Iio_iff_exists_Ioo_subset [no_bot_order Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝[Iio a] a ↔ βˆƒl ∈ Iio a, Ioo l a βŠ† s := let ⟨l', hl'⟩ := no_bot a in mem_nhds_within_Iio_iff_exists_Ioo_subset' hl' /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `[l, a)` with `l < a`. -/ lemma mem_nhds_within_Iio_iff_exists_Ico_subset [no_bot_order Ξ±] [densely_ordered Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝[Iio a] a ↔ βˆƒl ∈ Iio a, Ico l a βŠ† s := begin convert @mem_nhds_within_Ioi_iff_exists_Ioc_subset (order_dual Ξ±) _ _ _ _ _ _ _, simp only [dual_Ioc], refl end /-- The following statements are equivalent: 0. `s` is a neighborhood of `a` within `[a, +∞)` 1. `s` is a neighborhood of `a` within `[a, b]` 2. `s` is a neighborhood of `a` within `[a, b)` 3. `s` includes `[a, u)` for some `u ∈ (a, b]` 4. `s` includes `[a, u)` for some `u > a` -/ lemma tfae_mem_nhds_within_Ici {a b : Ξ±} (hab : a < b) (s : set Ξ±) : tfae [s ∈ 𝓝[Ici a] a, -- 0 : `s` is a neighborhood of `a` within `[a, +∞)` s ∈ 𝓝[Icc a b] a, -- 1 : `s` is a neighborhood of `a` within `[a, b]` s ∈ 𝓝[Ico a b] a, -- 2 : `s` is a neighborhood of `a` within `[a, b)` βˆƒ u ∈ Ioc a b, Ico a u βŠ† s, -- 3 : `s` includes `[a, u)` for some `u ∈ (a, b]` βˆƒ u ∈ Ioi a, Ico a u βŠ† s] := -- 4 : `s` includes `[a, u)` for some `u > a` begin tfae_have : 1 ↔ 2, by rw [nhds_within_Icc_eq_nhds_within_Ici hab], tfae_have : 1 ↔ 3, by rw [nhds_within_Ico_eq_nhds_within_Ici hab], tfae_have : 4 β†’ 5, from Ξ» ⟨u, umem, hu⟩, ⟨u, umem.1, hu⟩, tfae_have : 5 β†’ 1, { rintros ⟨u, hau, hu⟩, exact mem_of_superset (Ico_mem_nhds_within_Ici ⟨le_refl a, hau⟩) hu }, tfae_have : 1 β†’ 4, { assume h, rcases mem_nhds_within_iff_exists_mem_nhds_inter.1 h with ⟨v, va, hv⟩, rcases exists_Ico_subset_of_mem_nhds' va hab with ⟨u, au, hu⟩, refine ⟨u, au, Ξ»x hx, _⟩, refine hv ⟨hu ⟨hx.1, hx.2⟩, _⟩, exact hx.1 }, tfae_finish end lemma mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset {a u' : Ξ±} {s : set Ξ±} (hu' : a < u') : s ∈ 𝓝[Ici a] a ↔ βˆƒu ∈ Ioc a u', Ico a u βŠ† s := (tfae_mem_nhds_within_Ici hu' s).out 0 3 (by norm_num) (by norm_num) /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u)` with `a < u < u'`, provided `a` is not a top element. -/ lemma mem_nhds_within_Ici_iff_exists_Ico_subset' {a u' : Ξ±} {s : set Ξ±} (hu' : a < u') : s ∈ 𝓝[Ici a] a ↔ βˆƒu ∈ Ioi a, Ico a u βŠ† s := (tfae_mem_nhds_within_Ici hu' s).out 0 4 (by norm_num) (by norm_num) /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u)` with `a < u`. -/ lemma mem_nhds_within_Ici_iff_exists_Ico_subset [no_top_order Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝[Ici a] a ↔ βˆƒu ∈ Ioi a, Ico a u βŠ† s := let ⟨u', hu'⟩ := no_top a in mem_nhds_within_Ici_iff_exists_Ico_subset' hu' /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u]` with `a < u`. -/ lemma mem_nhds_within_Ici_iff_exists_Icc_subset' [no_top_order Ξ±] [densely_ordered Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝[Ici a] a ↔ βˆƒu ∈ Ioi a, Icc a u βŠ† s := begin rw mem_nhds_within_Ici_iff_exists_Ico_subset, split, { rintros ⟨u, au, as⟩, rcases exists_between au with ⟨v, hv⟩, exact ⟨v, hv.1, Ξ»x hx, as ⟨hx.1, lt_of_le_of_lt hx.2 hv.2⟩⟩ }, { rintros ⟨u, au, as⟩, exact ⟨u, au, subset.trans Ico_subset_Icc_self as⟩ } end /-- The following statements are equivalent: 0. `s` is a neighborhood of `b` within `(-∞, b]` 1. `s` is a neighborhood of `b` within `[a, b]` 2. `s` is a neighborhood of `b` within `(a, b]` 3. `s` includes `(l, b]` for some `l ∈ [a, b)` 4. `s` includes `(l, b]` for some `l < b` -/ lemma tfae_mem_nhds_within_Iic {a b : Ξ±} (h : a < b) (s : set Ξ±) : tfae [s ∈ 𝓝[Iic b] b, -- 0 : `s` is a neighborhood of `b` within `(-∞, b]` s ∈ 𝓝[Icc a b] b, -- 1 : `s` is a neighborhood of `b` within `[a, b]` s ∈ 𝓝[Ioc a b] b, -- 2 : `s` is a neighborhood of `b` within `(a, b]` βˆƒ l ∈ Ico a b, Ioc l b βŠ† s, -- 3 : `s` includes `(l, b]` for some `l ∈ [a, b)` βˆƒ l ∈ Iio b, Ioc l b βŠ† s] := -- 4 : `s` includes `(l, b]` for some `l < b` begin have := @tfae_mem_nhds_within_Ici (order_dual Ξ±) _ _ _ _ _ h s, -- If we call `convert` here, it generates wrong equations, so we need to simplify first simp only [exists_prop] at this ⊒, rw [dual_Icc, dual_Ioc, dual_Ioi] at this, convert this; ext l; rw [dual_Ico] end lemma mem_nhds_within_Iic_iff_exists_mem_Ico_Ioc_subset {a l' : Ξ±} {s : set Ξ±} (hl' : l' < a) : s ∈ 𝓝[Iic a] a ↔ βˆƒl ∈ Ico l' a, Ioc l a βŠ† s := (tfae_mem_nhds_within_Iic hl' s).out 0 3 (by norm_num) (by norm_num) /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `(l, a]` with `l < a`, provided `a` is not a bottom element. -/ lemma mem_nhds_within_Iic_iff_exists_Ioc_subset' {a l' : Ξ±} {s : set Ξ±} (hl' : l' < a) : s ∈ 𝓝[Iic a] a ↔ βˆƒl ∈ Iio a, Ioc l a βŠ† s := (tfae_mem_nhds_within_Iic hl' s).out 0 4 (by norm_num) (by norm_num) /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `(l, a]` with `l < a`. -/ lemma mem_nhds_within_Iic_iff_exists_Ioc_subset [no_bot_order Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝[Iic a] a ↔ βˆƒl ∈ Iio a, Ioc l a βŠ† s := let ⟨l', hl'⟩ := no_bot a in mem_nhds_within_Iic_iff_exists_Ioc_subset' hl' /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `[l, a]` with `l < a`. -/ lemma mem_nhds_within_Iic_iff_exists_Icc_subset' [no_bot_order Ξ±] [densely_ordered Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝[Iic a] a ↔ βˆƒl ∈ Iio a, Icc l a βŠ† s := begin convert @mem_nhds_within_Ici_iff_exists_Icc_subset' (order_dual Ξ±) _ _ _ _ _ _ _, simp_rw (show βˆ€ u : order_dual Ξ±, @Icc (order_dual Ξ±) _ a u = @Icc Ξ± _ u a, from Ξ» u, dual_Icc), refl, end /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u]` with `a < u`. -/ lemma mem_nhds_within_Ici_iff_exists_Icc_subset [no_top_order Ξ±] [densely_ordered Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝[Ici a] a ↔ βˆƒu, a < u ∧ Icc a u βŠ† s := begin rw mem_nhds_within_Ici_iff_exists_Ico_subset, split, { rintros ⟨u, au, as⟩, rcases exists_between au with ⟨v, hv⟩, exact ⟨v, hv.1, Ξ»x hx, as ⟨hx.1, lt_of_le_of_lt hx.2 hv.2⟩⟩ }, { rintros ⟨u, au, as⟩, exact ⟨u, au, subset.trans Ico_subset_Icc_self as⟩ } end /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `[l, a]` with `l < a`. -/ lemma mem_nhds_within_Iic_iff_exists_Icc_subset [no_bot_order Ξ±] [densely_ordered Ξ±] {a : Ξ±} {s : set Ξ±} : s ∈ 𝓝[Iic a] a ↔ βˆƒl, l < a ∧ Icc l a βŠ† s := begin rw mem_nhds_within_Iic_iff_exists_Ioc_subset, split, { rintros ⟨l, la, as⟩, rcases exists_between la with ⟨v, hv⟩, refine ⟨v, hv.2, Ξ»x hx, as ⟨lt_of_lt_of_le hv.1 hx.1, hx.2⟩⟩, }, { rintros ⟨l, la, as⟩, exact ⟨l, la, subset.trans Ioc_subset_Icc_self as⟩ } end end linear_order section linear_ordered_add_comm_group variables [topological_space Ξ±] [linear_ordered_add_comm_group Ξ±] [order_topology Ξ±] variables {l : filter Ξ²} {f g : Ξ² β†’ Ξ±} local notation `|` x `|` := abs x lemma nhds_eq_infi_abs_sub (a : Ξ±) : 𝓝 a = (β¨…r>0, π“Ÿ {b | |a - b| < r}) := begin simp only [le_antisymm_iff, nhds_eq_order, le_inf_iff, le_infi_iff, le_principal_iff, mem_Ioi, mem_Iio, abs_sub_lt_iff, @sub_lt_iff_lt_add _ _ _ _ _ _ a, @sub_lt _ _ _ _ a, set_of_and], refine ⟨_, _, _⟩, { intros Ξ΅ Ξ΅0, exact inter_mem_inf (mem_infi_of_mem (a - Ξ΅) $ mem_infi_of_mem (sub_lt_self a Ξ΅0) (mem_principal_self _)) (mem_infi_of_mem (Ξ΅ + a) $ mem_infi_of_mem (by simpa) (mem_principal_self _)) }, { intros b hb, exact mem_infi_of_mem (a - b) (mem_infi_of_mem (sub_pos.2 hb) (by simp [Ioi])) }, { intros b hb, exact mem_infi_of_mem (b - a) (mem_infi_of_mem (sub_pos.2 hb) (by simp [Iio])) } end lemma order_topology_of_nhds_abs {Ξ± : Type*} [topological_space Ξ±] [linear_ordered_add_comm_group Ξ±] (h_nhds : βˆ€a:Ξ±, 𝓝 a = (β¨…r>0, π“Ÿ {b | |a - b| < r})) : order_topology Ξ± := begin refine ⟨eq_of_nhds_eq_nhds $ Ξ» a, _⟩, rw [h_nhds], letI := preorder.topology Ξ±, letI : order_topology Ξ± := ⟨rfl⟩, exact (nhds_eq_infi_abs_sub a).symm end lemma linear_ordered_add_comm_group.tendsto_nhds {x : filter Ξ²} {a : Ξ±} : tendsto f x (𝓝 a) ↔ βˆ€ Ξ΅ > (0 : Ξ±), βˆ€αΆ  b in x, |f b - a| < Ξ΅ := by simp [nhds_eq_infi_abs_sub, abs_sub_comm a] lemma eventually_abs_sub_lt (a : Ξ±) {Ξ΅ : Ξ±} (hΞ΅ : 0 < Ξ΅) : βˆ€αΆ  x in 𝓝 a, |x - a| < Ξ΅ := (nhds_eq_infi_abs_sub a).symm β–Έ mem_infi_of_mem Ξ΅ (mem_infi_of_mem hΞ΅ $ by simp only [abs_sub_comm, mem_principal_self]) @[priority 100] -- see Note [lower instance priority] instance linear_ordered_add_comm_group.topological_add_group : topological_add_group Ξ± := { continuous_add := begin refine continuous_iff_continuous_at.2 _, rintro ⟨a, b⟩, refine linear_ordered_add_comm_group.tendsto_nhds.2 (Ξ» Ξ΅ Ξ΅0, _), rcases dense_or_discrete 0 Ξ΅ with (⟨δ, Ξ΄0, δΡ⟩|⟨h₁, hβ‚‚βŸ©), { -- If there exists `Ξ΄ ∈ (0, Ξ΅)`, then we choose `Ξ΄`-nhd of `a` and `(Ξ΅-Ξ΄)`-nhd of `b` filter_upwards [prod_is_open.mem_nhds (eventually_abs_sub_lt a Ξ΄0) (eventually_abs_sub_lt b (sub_pos.2 δΡ))], rintros ⟨x, y⟩ ⟨hx : |x - a| < Ξ΄, hy : |y - b| < Ξ΅ - δ⟩, rw [add_sub_comm], calc |x - a + (y - b)| ≀ |x - a| + |y - b| : abs_add _ _ ... < Ξ΄ + (Ξ΅ - Ξ΄) : add_lt_add hx hy ... = Ξ΅ : add_sub_cancel'_right _ _ }, { -- Otherewise `Ξ΅`-nhd of each point `a` is `{a}` have hΞ΅ : βˆ€ {x y}, abs (x - y) < Ξ΅ β†’ x = y, { intros x y h, simpa [sub_eq_zero] using hβ‚‚ _ h }, filter_upwards [prod_is_open.mem_nhds (eventually_abs_sub_lt a Ξ΅0) (eventually_abs_sub_lt b Ξ΅0)], rintros ⟨x, y⟩ ⟨hx : |x - a| < Ξ΅, hy : |y - b| < Ρ⟩, simpa [hΞ΅ hx, hΞ΅ hy] } end, continuous_neg := continuous_iff_continuous_at.2 $ Ξ» a, linear_ordered_add_comm_group.tendsto_nhds.2 $ Ξ» Ξ΅ Ξ΅0, (eventually_abs_sub_lt a Ξ΅0).mono $ Ξ» x hx, by rwa [neg_sub_neg, abs_sub_comm] } @[continuity] lemma continuous_abs : continuous (abs : Ξ± β†’ Ξ±) := continuous_id.max continuous_neg lemma filter.tendsto.abs {f : Ξ² β†’ Ξ±} {a : Ξ±} {l : filter Ξ²} (h : tendsto f l (𝓝 a)) : tendsto (Ξ» x, |f x|) l (𝓝 (|a|)) := (continuous_abs.tendsto _).comp h lemma nhds_basis_Ioo_pos [no_bot_order Ξ±] [no_top_order Ξ±] (a : Ξ±) : (𝓝 a).has_basis (Ξ» Ξ΅ : Ξ±, (0 : Ξ±) < Ξ΅) (Ξ» Ξ΅, Ioo (a-Ξ΅) (a+Ξ΅)) := ⟨begin refine Ξ» t, (nhds_basis_Ioo a).mem_iff.trans ⟨_, _⟩, { rintros ⟨⟨l, u⟩, ⟨hl : l < a, hu : a < u⟩, h' : Ioo l u βŠ† t⟩, refine ⟨min (a-l) (u-a), by apply lt_min; rwa sub_pos, _⟩, rintros x ⟨hx, hx'⟩, apply h', rw [sub_lt, lt_min_iff, sub_lt_sub_iff_left] at hx, rw [← sub_lt_iff_lt_add', lt_min_iff, sub_lt_sub_iff_right] at hx', exact ⟨hx.1, hx'.2⟩ }, { rintros ⟨Ρ, Ξ΅_pos, h⟩, exact ⟨(a-Ξ΅, a+Ξ΅), by simp [Ξ΅_pos], h⟩ }, end⟩ lemma nhds_basis_abs_sub_lt [no_bot_order Ξ±] [no_top_order Ξ±] (a : Ξ±) : (𝓝 a).has_basis (Ξ» Ξ΅ : Ξ±, (0 : Ξ±) < Ξ΅) (Ξ» Ξ΅, {b | abs (b - a) < Ξ΅}) := begin convert nhds_basis_Ioo_pos a, { ext Ξ΅, change abs (x - a) < Ξ΅ ↔ a - Ξ΅ < x ∧ x < a + Ξ΅, simp [abs_lt, sub_lt_iff_lt_add, add_comm Ξ΅ a, add_comm x Ξ΅] } end variable (Ξ±) lemma nhds_basis_zero_abs_sub_lt [no_bot_order Ξ±] [no_top_order Ξ±] : (𝓝 (0 : Ξ±)).has_basis (Ξ» Ξ΅ : Ξ±, (0 : Ξ±) < Ξ΅) (Ξ» Ξ΅, {b | abs b < Ξ΅}) := by simpa using nhds_basis_abs_sub_lt (0 : Ξ±) variable {Ξ±} /-- If `a` is positive we can form a basis from only nonnegative `Ioo` intervals -/ lemma nhds_basis_Ioo_pos_of_pos [no_bot_order Ξ±] [no_top_order Ξ±] {a : Ξ±} (ha : 0 < a) : (𝓝 a).has_basis (Ξ» Ξ΅ : Ξ±, (0 : Ξ±) < Ξ΅ ∧ Ξ΅ ≀ a) (Ξ» Ξ΅, Ioo (a-Ξ΅) (a+Ξ΅)) := ⟨ Ξ» t, (nhds_basis_Ioo_pos a).mem_iff.trans ⟨λ h, let ⟨i, hi, hit⟩ := h in ⟨min i a, ⟨lt_min hi ha, min_le_right i a⟩, trans (Ioo_subset_Ioo (sub_le_sub_left (min_le_left i a) a) (add_le_add_left (min_le_left i a) a)) hit⟩, Ξ» h, let ⟨i, hi, hit⟩ := h in ⟨i, hi.1, hit⟩ ⟩ ⟩ section variables [topological_space Ξ²] {b : Ξ²} {a : Ξ±} {s : set Ξ²} lemma continuous.abs (h : continuous f) : continuous (Ξ» x, |f x|) := continuous_abs.comp h lemma continuous_at.abs (h : continuous_at f b) : continuous_at (Ξ» x, |f x|) b := h.abs lemma continuous_within_at.abs (h : continuous_within_at f s b) : continuous_within_at (Ξ» x, |f x|) s b := h.abs lemma continuous_on.abs (h : continuous_on f s) : continuous_on (Ξ» x, |f x|) s := Ξ» x hx, (h x hx).abs lemma tendsto_abs_nhds_within_zero : tendsto (abs : Ξ± β†’ Ξ±) (𝓝[{0}ᢜ] 0) (𝓝[Ioi 0] 0) := (continuous_abs.tendsto' (0 : Ξ±) 0 abs_zero).inf $ tendsto_principal_principal.2 $ Ξ» x, abs_pos.2 end /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `C` and `g` tends to `at_top` then `f + g` tends to `at_top`. -/ lemma filter.tendsto.add_at_top {C : Ξ±} (hf : tendsto f l (𝓝 C)) (hg : tendsto g l at_top) : tendsto (Ξ» x, f x + g x) l at_top := begin nontriviality Ξ±, obtain ⟨C', hC'⟩ : βˆƒ C', C' < C := no_bot C, refine tendsto_at_top_add_left_of_le' _ C' _ hg, exact (hf.eventually (lt_mem_nhds hC')).mono (Ξ» x, le_of_lt) end /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `C` and `g` tends to `at_bot` then `f + g` tends to `at_bot`. -/ lemma filter.tendsto.add_at_bot {C : Ξ±} (hf : tendsto f l (𝓝 C)) (hg : tendsto g l at_bot) : tendsto (Ξ» x, f x + g x) l at_bot := @filter.tendsto.add_at_top (order_dual Ξ±) _ _ _ _ _ _ _ _ hf hg /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `at_top` and `g` tends to `C` then `f + g` tends to `at_top`. -/ lemma filter.tendsto.at_top_add {C : Ξ±} (hf : tendsto f l at_top) (hg : tendsto g l (𝓝 C)) : tendsto (Ξ» x, f x + g x) l at_top := by { conv in (_ + _) { rw add_comm }, exact hg.add_at_top hf } /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `at_bot` and `g` tends to `C` then `f + g` tends to `at_bot`. -/ lemma filter.tendsto.at_bot_add {C : Ξ±} (hf : tendsto f l at_bot) (hg : tendsto g l (𝓝 C)) : tendsto (Ξ» x, f x + g x) l at_bot := by { conv in (_ + _) { rw add_comm }, exact hg.add_at_bot hf } end linear_ordered_add_comm_group section linear_ordered_field variables [linear_ordered_field Ξ±] [topological_space Ξ±] [order_topology Ξ±] variables {l : filter Ξ²} {f g : Ξ² β†’ Ξ±} section continuous_mul lemma mul_tendsto_nhds_zero_right (x : Ξ±) : tendsto (uncurry ((*) : Ξ± β†’ Ξ± β†’ Ξ±)) (𝓝 0 Γ—αΆ  𝓝 x) $ 𝓝 0 := begin have hx : 0 < 2 * (1 + abs x) := (mul_pos (zero_lt_two) $ lt_of_lt_of_le zero_lt_one $ le_add_of_le_of_nonneg le_rfl (abs_nonneg x)), rw ((nhds_basis_zero_abs_sub_lt Ξ±).prod $ nhds_basis_abs_sub_lt x).tendsto_iff (nhds_basis_zero_abs_sub_lt Ξ±), refine Ξ» Ξ΅ Ξ΅_pos, ⟨(Ξ΅/(2 * (1 + abs x)), 1), ⟨div_pos Ξ΅_pos hx, zero_lt_one⟩, _⟩, suffices : βˆ€ (a b : Ξ±), abs a < Ξ΅ / (2 * (1 + abs x)) β†’ abs (b - x) < 1 β†’ (abs a) * (abs b) < Ξ΅, by simpa only [and_imp, prod.forall, mem_prod, ← abs_mul], intros a b h h', refine lt_of_le_of_lt (mul_le_mul_of_nonneg_left _ (abs_nonneg a)) ((lt_div_iff hx).1 h), calc abs b = abs ((b - x) + x) : by rw sub_add_cancel b x ... ≀ abs (b - x) + abs x : abs_add (b - x) x ... ≀ 1 + abs x : add_le_add_right (le_of_lt h') (abs x) ... ≀ 2 * (1 + abs x) : by linarith, end lemma mul_tendsto_nhds_zero_left (x : Ξ±) : tendsto (uncurry ((*) : Ξ± β†’ Ξ± β†’ Ξ±)) (𝓝 x Γ—αΆ  𝓝 0) $ 𝓝 0 := begin intros s hs, have := mul_tendsto_nhds_zero_right x hs, rw [filter.mem_map, mem_prod_iff] at this ⊒, obtain ⟨U, hU, V, hV, h⟩ := this, exact ⟨V, hV, U, hU, Ξ» y hy, ((mul_comm y.2 y.1) β–Έ h (⟨hy.2, hy.1⟩ : (prod.mk y.2 y.1) ∈ (U.prod V)) : y.1 * y.2 ∈ s)⟩, end lemma nhds_eq_map_mul_left_nhds_one {xβ‚€ : Ξ±} (hxβ‚€ : xβ‚€ β‰  0) : 𝓝 xβ‚€ = map (Ξ» x, xβ‚€*x) (𝓝 1) := begin have hxβ‚€' : 0 < abs xβ‚€ := abs_pos.2 hxβ‚€, refine filter.ext (Ξ» t, _), simp only [exists_prop, set_of_subset_set_of, (nhds_basis_abs_sub_lt xβ‚€).mem_iff, (nhds_basis_abs_sub_lt (1 : Ξ±)).mem_iff, filter.mem_map'], refine ⟨λ h, _, Ξ» h, _⟩, { obtain ⟨i, hi, hit⟩ := h, refine ⟨i / (abs xβ‚€), div_pos hi (abs_pos.2 hxβ‚€), Ξ» x hx, hit _⟩, calc abs (xβ‚€ * x - xβ‚€) = abs (xβ‚€ * (x - 1)) : congr_arg abs (by ring_nf) ... = abs xβ‚€ * abs (x - 1) : abs_mul xβ‚€ (x - 1) ... < abs xβ‚€ * (i / abs xβ‚€) : mul_lt_mul' le_rfl hx (abs_nonneg (x - 1)) (abs_pos.2 hxβ‚€) ... = abs xβ‚€ * i / abs xβ‚€ : by ring ... = i : mul_div_cancel_left i (Ξ» h, hxβ‚€ (abs_eq_zero.1 h)) }, { obtain ⟨i, hi, hit⟩ := h, refine ⟨i * (abs xβ‚€), mul_pos hi (abs_pos.2 hxβ‚€), Ξ» x hx, _⟩, have : abs (x / xβ‚€ - 1) < i, calc abs (x / xβ‚€ - 1) = abs (x / xβ‚€ - xβ‚€ / xβ‚€) : (by rw div_self hxβ‚€) ... = abs ((x - xβ‚€) / xβ‚€) : congr_arg abs (sub_div x xβ‚€ xβ‚€).symm ... = abs (x - xβ‚€) / abs xβ‚€ : abs_div (x - xβ‚€) xβ‚€ ... < i * abs xβ‚€ / abs xβ‚€ : div_lt_div hx le_rfl (mul_nonneg (le_of_lt hi) (abs_nonneg xβ‚€)) (abs_pos.2 hxβ‚€) ... = i : by rw [← mul_div_assoc', div_self (ne_of_lt $ abs_pos.2 hxβ‚€).symm, mul_one], specialize hit (x / xβ‚€) this, rwa [mul_div_assoc', mul_div_cancel_left x hxβ‚€] at hit } end lemma nhds_eq_map_mul_right_nhds_one {xβ‚€ : Ξ±} (hxβ‚€ : xβ‚€ β‰  0) : 𝓝 xβ‚€ = map (Ξ» x, x*xβ‚€) (𝓝 1) := by simp_rw [mul_comm _ xβ‚€, nhds_eq_map_mul_left_nhds_one hxβ‚€] lemma mul_tendsto_nhds_one_nhds_one : tendsto (uncurry ((*) : Ξ± β†’ Ξ± β†’ Ξ±)) (𝓝 1 Γ—αΆ  𝓝 1) $ 𝓝 1 := begin rw ((nhds_basis_Ioo_pos (1 : Ξ±)).prod $ nhds_basis_Ioo_pos (1 : Ξ±)).tendsto_iff (nhds_basis_Ioo_pos_of_pos (zero_lt_one : (0 : Ξ±) < 1)), intros Ξ΅ hΞ΅, have hΞ΅' : 0 ≀ 1 - Ξ΅ / 4 := by linarith, have Ξ΅_pos : 0 < Ξ΅ / 4 := by linarith, have Ξ΅_pos' : 0 < Ξ΅ / 2 := by linarith, simp only [and_imp, prod.forall, mem_Ioo, function.uncurry_apply_pair, mem_prod, prod.exists], refine ⟨Ρ/4, Ξ΅/4, ⟨Ρ_pos, Ξ΅_pos⟩, Ξ» a b ha ha' hb hb', _⟩, have ha0 : 0 ≀ a := le_trans hΞ΅' (le_of_lt ha), have hb0 : 0 ≀ b := le_trans hΞ΅' (le_of_lt hb), refine ⟨lt_of_le_of_lt _ (mul_lt_mul'' ha hb hΞ΅' hΞ΅'), lt_of_lt_of_le (mul_lt_mul'' ha' hb' ha0 hb0) _⟩, { calc 1 - Ξ΅ = 1 - Ξ΅ / 2 - Ξ΅/2 : by ring_nf ... ≀ 1 - Ξ΅/2 - Ξ΅/2 + (Ξ΅/2)*(Ξ΅/2) : le_add_of_nonneg_right (le_of_lt (mul_pos Ξ΅_pos' Ξ΅_pos')) ... = (1 - Ξ΅/2) * (1 - Ξ΅/2) : by ring_nf ... ≀ (1 - Ξ΅/4) * (1 - Ξ΅/4) : mul_le_mul (by linarith) (by linarith) (by linarith) hΞ΅' }, { calc (1 + Ξ΅/4) * (1 + Ξ΅/4) = 1 + Ξ΅/2 + (Ξ΅/4)*(Ξ΅/4) : by ring_nf ... = 1 + Ξ΅/2 + (Ξ΅ * Ξ΅) / 16 : by ring_nf ... ≀ 1 + Ξ΅/2 + Ξ΅/2 : add_le_add_left (div_le_div (le_of_lt hΞ΅.1) (le_trans ((mul_le_mul_left hΞ΅.1).2 hΞ΅.2) (le_of_eq $ mul_one Ξ΅)) zero_lt_two (by linarith)) (1 + Ξ΅/2) ... ≀ 1 + Ξ΅ : by ring_nf } end @[priority 100] instance linear_ordered_field.has_continuous_mul : has_continuous_mul Ξ± := ⟨begin rw continuous_iff_continuous_at, rintro ⟨xβ‚€, yβ‚€βŸ©, by_cases hxβ‚€ : xβ‚€ = 0, { rw [hxβ‚€, continuous_at, zero_mul, nhds_prod_eq], exact mul_tendsto_nhds_zero_right yβ‚€ }, by_cases hyβ‚€ : yβ‚€ = 0, { rw [hyβ‚€, continuous_at, mul_zero, nhds_prod_eq], exact mul_tendsto_nhds_zero_left xβ‚€ }, have hxy : xβ‚€ * yβ‚€ β‰  0 := mul_ne_zero hxβ‚€ hyβ‚€, have key : (Ξ» p : Ξ± Γ— Ξ±, xβ‚€ * p.1 * (p.2 * yβ‚€)) = ((Ξ» x, xβ‚€*x) ∘ (Ξ» x, x*yβ‚€)) ∘ (uncurry (*)), { ext p, simp [uncurry, mul_assoc] }, have keyβ‚‚ : (Ξ» x, xβ‚€*x) ∘ (Ξ» x, yβ‚€*x) = Ξ» x, (xβ‚€ *yβ‚€)*x, { ext x, simp }, calc map (uncurry (*)) (𝓝 (xβ‚€, yβ‚€)) = map (uncurry (*)) (𝓝 xβ‚€ Γ—αΆ  𝓝 yβ‚€) : by rw nhds_prod_eq ... = map (Ξ» (p : Ξ± Γ— Ξ±), xβ‚€ * p.1 * (p.2 * yβ‚€)) ((𝓝 1) Γ—αΆ  (𝓝 1)) : by rw [uncurry, nhds_eq_map_mul_left_nhds_one hxβ‚€, nhds_eq_map_mul_right_nhds_one hyβ‚€, prod_map_map_eq, filter.map_map] ... = map ((Ξ» x, xβ‚€ * x) ∘ Ξ» x, x * yβ‚€) (map (uncurry (*)) (𝓝 1 Γ—αΆ  𝓝 1)) : by rw [key, ← filter.map_map] ... ≀ map ((Ξ» (x : Ξ±), xβ‚€ * x) ∘ Ξ» x, x * yβ‚€) (𝓝 1) : map_mono (mul_tendsto_nhds_one_nhds_one) ... = 𝓝 (xβ‚€*yβ‚€) : by rw [← filter.map_map, ← nhds_eq_map_mul_right_nhds_one hyβ‚€, nhds_eq_map_mul_left_nhds_one hyβ‚€, filter.map_map, keyβ‚‚, ← nhds_eq_map_mul_left_nhds_one hxy], end⟩ end continuous_mul /-- In a linearly ordered field with the order topology, if `f` tends to `at_top` and `g` tends to a positive constant `C` then `f * g` tends to `at_top`. -/ lemma filter.tendsto.at_top_mul {C : Ξ±} (hC : 0 < C) (hf : tendsto f l at_top) (hg : tendsto g l (𝓝 C)) : tendsto (Ξ» x, (f x * g x)) l at_top := begin refine tendsto_at_top_mono' _ _ (hf.at_top_mul_const (half_pos hC)), filter_upwards [hg.eventually (lt_mem_nhds (half_lt_self hC)), hf.eventually (eventually_ge_at_top 0)], exact Ξ» x hg hf, mul_le_mul_of_nonneg_left hg.le hf end /-- In a linearly ordered field with the order topology, if `f` tends to a positive constant `C` and `g` tends to `at_top` then `f * g` tends to `at_top`. -/ lemma filter.tendsto.mul_at_top {C : Ξ±} (hC : 0 < C) (hf : tendsto f l (𝓝 C)) (hg : tendsto g l at_top) : tendsto (Ξ» x, (f x * g x)) l at_top := by simpa only [mul_comm] using hg.at_top_mul hC hf /-- In a linearly ordered field with the order topology, if `f` tends to `at_top` and `g` tends to a negative constant `C` then `f * g` tends to `at_bot`. -/ lemma filter.tendsto.at_top_mul_neg {C : Ξ±} (hC : C < 0) (hf : tendsto f l at_top) (hg : tendsto g l (𝓝 C)) : tendsto (Ξ» x, (f x * g x)) l at_bot := by simpa only [(∘), neg_mul_eq_mul_neg, neg_neg] using tendsto_neg_at_top_at_bot.comp (hf.at_top_mul (neg_pos.2 hC) hg.neg) /-- In a linearly ordered field with the order topology, if `f` tends to a negative constant `C` and `g` tends to `at_top` then `f * g` tends to `at_bot`. -/ lemma filter.tendsto.neg_mul_at_top {C : Ξ±} (hC : C < 0) (hf : tendsto f l (𝓝 C)) (hg : tendsto g l at_top) : tendsto (Ξ» x, (f x * g x)) l at_bot := by simpa only [mul_comm] using hg.at_top_mul_neg hC hf /-- In a linearly ordered field with the order topology, if `f` tends to `at_bot` and `g` tends to a positive constant `C` then `f * g` tends to `at_bot`. -/ lemma filter.tendsto.at_bot_mul {C : Ξ±} (hC : 0 < C) (hf : tendsto f l at_bot) (hg : tendsto g l (𝓝 C)) : tendsto (Ξ» x, (f x * g x)) l at_bot := by simpa [(∘)] using tendsto_neg_at_top_at_bot.comp ((tendsto_neg_at_bot_at_top.comp hf).at_top_mul hC hg) /-- In a linearly ordered field with the order topology, if `f` tends to `at_bot` and `g` tends to a negative constant `C` then `f * g` tends to `at_top`. -/ lemma filter.tendsto.at_bot_mul_neg {C : Ξ±} (hC : C < 0) (hf : tendsto f l at_bot) (hg : tendsto g l (𝓝 C)) : tendsto (Ξ» x, (f x * g x)) l at_top := by simpa [(∘)] using tendsto_neg_at_bot_at_top.comp ((tendsto_neg_at_bot_at_top.comp hf).at_top_mul_neg hC hg) /-- In a linearly ordered field with the order topology, if `f` tends to a positive constant `C` and `g` tends to `at_bot` then `f * g` tends to `at_bot`. -/ lemma filter.tendsto.mul_at_bot {C : Ξ±} (hC : 0 < C) (hf : tendsto f l (𝓝 C)) (hg : tendsto g l at_bot) : tendsto (Ξ» x, (f x * g x)) l at_bot := by simpa only [mul_comm] using hg.at_bot_mul hC hf /-- In a linearly ordered field with the order topology, if `f` tends to a negative constant `C` and `g` tends to `at_bot` then `f * g` tends to `at_top`. -/ lemma filter.tendsto.neg_mul_at_bot {C : Ξ±} (hC : C < 0) (hf : tendsto f l (𝓝 C)) (hg : tendsto g l at_bot) : tendsto (Ξ» x, (f x * g x)) l at_top := by simpa only [mul_comm] using hg.at_bot_mul_neg hC hf /-- The function `x ↦ x⁻¹` tends to `+∞` on the right of `0`. -/ lemma tendsto_inv_zero_at_top : tendsto (Ξ»x:Ξ±, x⁻¹) (𝓝[set.Ioi (0:Ξ±)] 0) at_top := begin refine (at_top_basis' 1).tendsto_right_iff.2 (Ξ» b hb, _), have hb' : 0 < b := zero_lt_one.trans_le hb, filter_upwards [Ioc_mem_nhds_within_Ioi ⟨le_rfl, inv_pos.2 hb'⟩], exact Ξ» x hx, (le_inv hx.1 hb').1 hx.2 end /-- The function `r ↦ r⁻¹` tends to `0` on the right as `r β†’ +∞`. -/ lemma tendsto_inv_at_top_zero' : tendsto (Ξ»r:Ξ±, r⁻¹) at_top (𝓝[set.Ioi (0:Ξ±)] 0) := begin refine (has_basis.tendsto_iff at_top_basis ⟨λ s, mem_nhds_within_Ioi_iff_exists_Ioc_subset⟩).2 _, refine Ξ» b hb, ⟨b⁻¹, trivial, Ξ» x hx, _⟩, have : 0 < x := lt_of_lt_of_le (inv_pos.2 hb) hx, exact ⟨inv_pos.2 this, (inv_le this hb).2 hx⟩ end lemma tendsto_inv_at_top_zero : tendsto (Ξ»r:Ξ±, r⁻¹) at_top (𝓝 0) := tendsto_inv_at_top_zero'.mono_right inf_le_left lemma filter.tendsto.div_at_top [has_continuous_mul Ξ±] {f g : Ξ² β†’ Ξ±} {l : filter Ξ²} {a : Ξ±} (h : tendsto f l (𝓝 a)) (hg : tendsto g l at_top) : tendsto (Ξ» x, f x / g x) l (𝓝 0) := by { simp only [div_eq_mul_inv], exact mul_zero a β–Έ h.mul (tendsto_inv_at_top_zero.comp hg) } lemma filter.tendsto.inv_tendsto_at_top (h : tendsto f l at_top) : tendsto (f⁻¹) l (𝓝 0) := tendsto_inv_at_top_zero.comp h lemma filter.tendsto.inv_tendsto_zero (h : tendsto f l (𝓝[set.Ioi 0] 0)) : tendsto (f⁻¹) l at_top := tendsto_inv_zero_at_top.comp h /-- The function `x^(-n)` tends to `0` at `+∞` for any positive natural `n`. A version for positive real powers exists as `tendsto_rpow_neg_at_top`. -/ lemma tendsto_pow_neg_at_top {n : β„•} (hn : 1 ≀ n) : tendsto (Ξ» x : Ξ±, x ^ (-(n:β„€))) at_top (𝓝 0) := tendsto.congr (Ξ» x, (fpow_neg x n).symm) (filter.tendsto.inv_tendsto_at_top (by simpa [gpow_coe_nat] using tendsto_pow_at_top hn)) lemma tendsto_fpow_at_top_zero {n : β„€} (hn : n < 0) : tendsto (Ξ» x : Ξ±, x^n) at_top (𝓝 0) := begin have : 1 ≀ -n := le_neg.mp (int.le_of_lt_add_one (hn.trans_le (neg_add_self 1).symm.le)), apply tendsto.congr (show βˆ€ x : Ξ±, x^-(-n) = x^n, by simp), lift -n to β„• using le_of_lt (neg_pos.mpr hn) with N, exact tendsto_pow_neg_at_top (by exact_mod_cast this) end lemma tendsto_const_mul_fpow_at_top_zero {n : β„€} {c : Ξ±} (hn : n < 0) : tendsto (Ξ» x, c * x ^ n) at_top (𝓝 0) := (mul_zero c) β–Έ (filter.tendsto.const_mul c (tendsto_fpow_at_top_zero hn)) lemma tendsto_const_mul_pow_nhds_iff {n : β„•} {c d : Ξ±} (hc : c β‰  0) : tendsto (Ξ» x : Ξ±, c * x ^ n) at_top (𝓝 d) ↔ n = 0 ∧ c = d := begin refine ⟨λ h, _, Ξ» h, _⟩, { have hn : n = 0, { by_contradiction hn, have hn : 1 ≀ n := nat.succ_le_iff.2 (lt_of_le_of_ne (zero_le _) (ne.symm hn)), by_cases hc' : 0 < c, { have := (tendsto_const_mul_pow_at_top_iff c n).2 ⟨hn, hc'⟩, exact not_tendsto_nhds_of_tendsto_at_top this d h }, { have := (tendsto_neg_const_mul_pow_at_top_iff c n).2 ⟨hn, lt_of_le_of_ne (not_lt.1 hc') hc⟩, exact not_tendsto_nhds_of_tendsto_at_bot this d h } }, have : (Ξ» x : Ξ±, c * x ^ n) = (Ξ» x : Ξ±, c), by simp [hn], rw [this, tendsto_const_nhds_iff] at h, exact ⟨hn, h⟩ }, { obtain ⟨hn, hcd⟩ := h, simpa [hn, hcd] using tendsto_const_nhds } end lemma tendsto_const_mul_fpow_at_top_zero_iff {n : β„€} {c d : Ξ±} (hc : c β‰  0) : tendsto (Ξ» x : Ξ±, c * x ^ n) at_top (𝓝 d) ↔ (n = 0 ∧ c = d) ∨ (n < 0 ∧ d = 0) := begin refine ⟨λ h, _, Ξ» h, _⟩, { by_cases hn : 0 ≀ n, { lift n to β„• using hn, simp only [gpow_coe_nat] at h, rw [tendsto_const_mul_pow_nhds_iff hc, ← int.coe_nat_eq_zero] at h, exact or.inl h }, { rw not_le at hn, refine or.inr ⟨hn, tendsto_nhds_unique h (tendsto_const_mul_fpow_at_top_zero hn)⟩ } }, { cases h, { simp only [h.left, h.right, gpow_zero, mul_one], exact tendsto_const_nhds }, { exact h.2.symm β–Έ tendsto_const_mul_fpow_at_top_zero h.1} } end end linear_ordered_field lemma preimage_neg [add_group Ξ±] : preimage (has_neg.neg : Ξ± β†’ Ξ±) = image (has_neg.neg : Ξ± β†’ Ξ±) := (image_eq_preimage_of_inverse neg_neg neg_neg).symm lemma filter.map_neg [add_group Ξ±] : map (has_neg.neg : Ξ± β†’ Ξ±) = comap (has_neg.neg : Ξ± β†’ Ξ±) := funext $ assume f, map_eq_comap_of_inverse (funext neg_neg) (funext neg_neg) section order_topology variables [topological_space Ξ±] [topological_space Ξ²] [linear_order Ξ±] [linear_order Ξ²] [order_topology Ξ±] [order_topology Ξ²] lemma is_lub.frequently_mem {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) : βˆƒαΆ  x in 𝓝[Iic a] a, x ∈ s := begin rcases hs with ⟨a', ha'⟩, intro h, rcases (ha.1 ha').eq_or_lt with (rfl|ha'a), { exact h.self_of_nhds_within le_rfl ha' }, { rcases (mem_nhds_within_Iic_iff_exists_Ioc_subset' ha'a).1 h with ⟨b, hba, hb⟩, rcases ha.exists_between hba with ⟨b', hb's, hb'⟩, exact hb hb' hb's }, end lemma is_lub.frequently_nhds_mem {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) : βˆƒαΆ  x in 𝓝 a, x ∈ s := (ha.frequently_mem hs).filter_mono inf_le_left lemma is_glb.frequently_mem {a : Ξ±} {s : set Ξ±} (ha : is_glb s a) (hs : s.nonempty) : βˆƒαΆ  x in 𝓝[Ici a] a, x ∈ s := @is_lub.frequently_mem (order_dual Ξ±) _ _ _ _ _ ha hs lemma is_glb.frequently_nhds_mem {a : Ξ±} {s : set Ξ±} (ha : is_glb s a) (hs : s.nonempty) : βˆƒαΆ  x in 𝓝 a, x ∈ s := (ha.frequently_mem hs).filter_mono inf_le_left lemma is_lub.mem_closure {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) : a ∈ closure s := (ha.frequently_nhds_mem hs).mem_closure lemma is_glb.mem_closure {a : Ξ±} {s : set Ξ±} (ha : is_glb s a) (hs : s.nonempty) : a ∈ closure s := (ha.frequently_nhds_mem hs).mem_closure lemma is_lub.nhds_within_ne_bot {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) : ne_bot (𝓝[s] a) := mem_closure_iff_nhds_within_ne_bot.1 (ha.mem_closure hs) lemma is_glb.nhds_within_ne_bot : βˆ€ {a : Ξ±} {s : set Ξ±}, is_glb s a β†’ s.nonempty β†’ ne_bot (𝓝[s] a) := @is_lub.nhds_within_ne_bot (order_dual Ξ±) _ _ _ lemma is_lub_of_mem_nhds {s : set Ξ±} {a : Ξ±} {f : filter Ξ±} (hsa : a ∈ upper_bounds s) (hsf : s ∈ f) [ne_bot (f βŠ“ 𝓝 a)] : is_lub s a := ⟨hsa, assume b hb, not_lt.1 $ assume hba, have s ∩ {a | b < a} ∈ f βŠ“ 𝓝 a, from inter_mem_inf hsf (is_open.mem_nhds (is_open_lt' _) hba), let ⟨x, ⟨hxs, hxb⟩⟩ := filter.nonempty_of_mem this in have b < b, from lt_of_lt_of_le hxb $ hb hxs, lt_irrefl b this⟩ lemma is_lub_of_mem_closure {s : set Ξ±} {a : Ξ±} (hsa : a ∈ upper_bounds s) (hsf : a ∈ closure s) : is_lub s a := begin rw [mem_closure_iff_cluster_pt, cluster_pt, inf_comm] at hsf, haveI : (π“Ÿ s βŠ“ 𝓝 a).ne_bot := hsf, exact is_lub_of_mem_nhds hsa (mem_principal_self s), end lemma is_glb_of_mem_nhds : βˆ€ {s : set Ξ±} {a : Ξ±} {f : filter Ξ±}, a ∈ lower_bounds s β†’ s ∈ f β†’ ne_bot (f βŠ“ 𝓝 a) β†’ is_glb s a := @is_lub_of_mem_nhds (order_dual Ξ±) _ _ _ lemma is_glb_of_mem_closure {s : set Ξ±} {a : Ξ±} (hsa : a ∈ lower_bounds s) (hsf : a ∈ closure s) : is_glb s a := @is_lub_of_mem_closure (order_dual Ξ±) _ _ _ s a hsa hsf lemma is_lub.mem_upper_bounds_of_tendsto [preorder Ξ³] [topological_space Ξ³] [order_closed_topology Ξ³] {f : Ξ± β†’ Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³} (hf : βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f x ≀ f y) (ha : is_lub s a) (hb : tendsto f (𝓝[s] a) (𝓝 b)) : b ∈ upper_bounds (f '' s) := begin rintro _ ⟨x, hx, rfl⟩, replace ha := ha.inter_Ici_of_mem hx, haveI := ha.nhds_within_ne_bot ⟨x, hx, le_rfl⟩, refine ge_of_tendsto (hb.mono_left (nhds_within_mono _ (inter_subset_left s (Ici x)))) _, exact mem_of_superset self_mem_nhds_within (Ξ» y hy, hf _ hx _ hy.1 hy.2) end -- For a version of this theorem in which the convergence considered on the domain `Ξ±` is as -- `x : Ξ±` tends to infinity, rather than tending to a point `x` in `Ξ±`, see `is_lub_of_tendsto`, -- below lemma is_lub.is_lub_of_tendsto [preorder Ξ³] [topological_space Ξ³] [order_closed_topology Ξ³] {f : Ξ± β†’ Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³} (hf : βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f x ≀ f y) (ha : is_lub s a) (hs : s.nonempty) (hb : tendsto f (𝓝[s] a) (𝓝 b)) : is_lub (f '' s) b := begin haveI := ha.nhds_within_ne_bot hs, exact ⟨ha.mem_upper_bounds_of_tendsto hf hb, Ξ» b' hb', le_of_tendsto hb (mem_of_superset self_mem_nhds_within $ Ξ» x hx, hb' $ mem_image_of_mem _ hx)⟩ end lemma is_glb.mem_lower_bounds_of_tendsto [preorder Ξ³] [topological_space Ξ³] [order_closed_topology Ξ³] {f : Ξ± β†’ Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³} (hf : βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f x ≀ f y) (ha : is_glb s a) (hb : tendsto f (𝓝[s] a) (𝓝 b)) : b ∈ lower_bounds (f '' s) := @is_lub.mem_upper_bounds_of_tendsto (order_dual Ξ±) (order_dual Ξ³) _ _ _ _ _ _ _ _ _ _ (Ξ» x hx y hy, hf y hy x hx) ha hb -- For a version of this theorem in which the convergence considered on the domain `Ξ±` is as -- `x : Ξ±` tends to negative infinity, rather than tending to a point `x` in `Ξ±`, see -- `is_glb_of_tendsto`, below lemma is_glb.is_glb_of_tendsto [preorder Ξ³] [topological_space Ξ³] [order_closed_topology Ξ³] {f : Ξ± β†’ Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³} (hf : βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f x ≀ f y) : is_glb s a β†’ s.nonempty β†’ tendsto f (𝓝[s] a) (𝓝 b) β†’ is_glb (f '' s) b := @is_lub.is_lub_of_tendsto (order_dual Ξ±) (order_dual Ξ³) _ _ _ _ _ _ f s a b (Ξ» x hx y hy, hf y hy x hx) lemma is_lub.mem_lower_bounds_of_tendsto [preorder Ξ³] [topological_space Ξ³] [order_closed_topology Ξ³] {f : Ξ± β†’ Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³} (hf : βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f y ≀ f x) (ha : is_lub s a) (hb : tendsto f (𝓝[s] a) (𝓝 b)) : b ∈ lower_bounds (f '' s) := @is_lub.mem_upper_bounds_of_tendsto Ξ± (order_dual Ξ³) _ _ _ _ _ _ _ _ _ _ hf ha hb lemma is_lub.is_glb_of_tendsto [preorder Ξ³] [topological_space Ξ³] [order_closed_topology Ξ³] : βˆ€ {f : Ξ± β†’ Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³}, (βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f y ≀ f x) β†’ is_lub s a β†’ s.nonempty β†’ tendsto f (𝓝[s] a) (𝓝 b) β†’ is_glb (f '' s) b := @is_lub.is_lub_of_tendsto Ξ± (order_dual Ξ³) _ _ _ _ _ _ lemma is_glb.mem_upper_bounds_of_tendsto [preorder Ξ³] [topological_space Ξ³] [order_closed_topology Ξ³] {f : Ξ± β†’ Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³} (hf : βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f y ≀ f x) (ha : is_glb s a) (hb : tendsto f (𝓝[s] a) (𝓝 b)) : b ∈ upper_bounds (f '' s) := @is_glb.mem_lower_bounds_of_tendsto Ξ± (order_dual Ξ³) _ _ _ _ _ _ _ _ _ _ hf ha hb lemma is_glb.is_lub_of_tendsto [preorder Ξ³] [topological_space Ξ³] [order_closed_topology Ξ³] : βˆ€ {f : Ξ± β†’ Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³}, (βˆ€x∈s, βˆ€y∈s, x ≀ y β†’ f y ≀ f x) β†’ is_glb s a β†’ s.nonempty β†’ tendsto f (𝓝[s] a) (𝓝 b) β†’ is_lub (f '' s) b := @is_glb.is_glb_of_tendsto Ξ± (order_dual Ξ³) _ _ _ _ _ _ lemma is_lub.mem_of_is_closed {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) (sc : is_closed s) : a ∈ s := sc.closure_subset $ ha.mem_closure hs alias is_lub.mem_of_is_closed ← is_closed.is_lub_mem lemma is_glb.mem_of_is_closed {a : Ξ±} {s : set Ξ±} (ha : is_glb s a) (hs : s.nonempty) (sc : is_closed s) : a ∈ s := sc.closure_subset $ ha.mem_closure hs alias is_glb.mem_of_is_closed ← is_closed.is_glb_mem /-! ### Existence of sequences tending to Inf or Sup of a given set -/ lemma is_lub.exists_seq_strict_mono_tendsto_of_not_mem' {t : set Ξ±} {x : Ξ±} (htx : is_lub t x) (not_mem : x βˆ‰ t) (ht : t.nonempty) (hx : is_countably_generated (𝓝 x)) : βˆƒ u : β„• β†’ Ξ±, strict_mono u ∧ (βˆ€ n, u n < x) ∧ tendsto u at_top (𝓝 x) ∧ (βˆ€ n, u n ∈ t) := begin rcases ht with ⟨l, hl⟩, have hl : l < x, { rcases lt_or_eq_of_le (htx.1 hl) with h|rfl, { exact h }, { exact (not_mem hl).elim } }, obtain ⟨s, hs⟩ : βˆƒ s : β„• β†’ set Ξ±, (𝓝 x).has_basis (Ξ» (_x : β„•), true) s := let ⟨s, hs⟩ := hx.exists_antimono_basis in ⟨s, hs.to_has_basis⟩, have : βˆ€ n k, k < x β†’ βˆƒ y, Icc y x βŠ† s n ∧ k < y ∧ y < x ∧ y ∈ t, { assume n k hk, obtain ⟨L, hL, h⟩ : βˆƒ (L : Ξ±) (hL : L ∈ Ico k x), Ioc L x βŠ† s n := exists_Ioc_subset_of_mem_nhds' (hs.mem_of_mem trivial) hk, obtain ⟨y, hy⟩ : βˆƒ (y : Ξ±), L < y ∧ y < x ∧ y ∈ t, { rcases htx.exists_between' not_mem hL.2 with ⟨y, yt, hy⟩, refine ⟨y, hy.1, hy.2, yt⟩ }, exact ⟨y, Ξ» z hz, h ⟨hy.1.trans_le hz.1, hz.2⟩, hL.1.trans_lt hy.1, hy.2⟩ }, choose! f hf using this, let u : β„• β†’ Ξ± := Ξ» n, nat.rec_on n (f 0 l) (Ξ» n h, f n.succ h), have I : βˆ€ n, u n < x, { assume n, induction n with n IH, { exact (hf 0 l hl).2.2.1 }, { exact (hf n.succ _ IH).2.2.1 } }, have S : strict_mono u := strict_mono_nat_of_lt_succ (Ξ» n, (hf n.succ _ (I n)).2.1), refine ⟨u, S, I, hs.tendsto_right_iff.2 (Ξ» n _, _), (Ξ» n, _)⟩, { simp only [ge_iff_le, eventually_at_top], refine ⟨n, Ξ» p hp, _⟩, have up : u p ∈ Icc (u n) x := ⟨S.monotone hp, (I p).le⟩, have : Icc (u n) x βŠ† s n, by { cases n, { exact (hf 0 l hl).1 }, { exact (hf n.succ (u n) (I n)).1 } }, exact this up }, { cases n, { exact (hf 0 l hl).2.2.2 }, { exact (hf n.succ _ (I n)).2.2.2 } } end lemma is_lub.exists_seq_monotone_tendsto' {t : set Ξ±} {x : Ξ±} (htx : is_lub t x) (ht : t.nonempty) (hx : is_countably_generated (𝓝 x)) : βˆƒ u : β„• β†’ Ξ±, monotone u ∧ (βˆ€ n, u n ≀ x) ∧ tendsto u at_top (𝓝 x) ∧ (βˆ€ n, u n ∈ t) := begin by_cases h : x ∈ t, { exact ⟨λ n, x, monotone_const, Ξ» n, le_rfl, tendsto_const_nhds, Ξ» n, h⟩ }, { rcases htx.exists_seq_strict_mono_tendsto_of_not_mem' h ht hx with ⟨u, hu⟩, exact ⟨u, hu.1.monotone, Ξ» n, (hu.2.1 n).le, hu.2.2⟩ } end lemma is_lub.exists_seq_strict_mono_tendsto_of_not_mem [first_countable_topology Ξ±] {t : set Ξ±} {x : Ξ±} (htx : is_lub t x) (ht : t.nonempty) (not_mem : x βˆ‰ t) : βˆƒ u : β„• β†’ Ξ±, strict_mono u ∧ (βˆ€ n, u n < x) ∧ tendsto u at_top (𝓝 x) ∧ (βˆ€ n, u n ∈ t) := htx.exists_seq_strict_mono_tendsto_of_not_mem' not_mem ht (is_countably_generated_nhds x) lemma is_lub.exists_seq_monotone_tendsto [first_countable_topology Ξ±] {t : set Ξ±} {x : Ξ±} (htx : is_lub t x) (ht : t.nonempty) : βˆƒ u : β„• β†’ Ξ±, monotone u ∧ (βˆ€ n, u n ≀ x) ∧ tendsto u at_top (𝓝 x) ∧ (βˆ€ n, u n ∈ t) := htx.exists_seq_monotone_tendsto' ht (is_countably_generated_nhds x) lemma exists_seq_strict_mono_tendsto' {Ξ± : Type*} [linear_order Ξ±] [topological_space Ξ±] [densely_ordered Ξ±] [order_topology Ξ±] [first_countable_topology Ξ±] {x y : Ξ±} (hy : y < x) : βˆƒ u : β„• β†’ Ξ±, strict_mono u ∧ (βˆ€ n, u n < x) ∧ tendsto u at_top (𝓝 x) := begin have hx : x βˆ‰ Iio x := Ξ» h, (lt_irrefl x h).elim, have ht : set.nonempty (Iio x) := ⟨y, hy⟩, rcases is_lub_Iio.exists_seq_strict_mono_tendsto_of_not_mem ht hx with ⟨u, hu⟩, exact ⟨u, hu.1, hu.2.1, hu.2.2.1⟩, end lemma exists_seq_strict_mono_tendsto [densely_ordered Ξ±] [no_bot_order Ξ±] [first_countable_topology Ξ±] (x : Ξ±) : βˆƒ u : β„• β†’ Ξ±, strict_mono u ∧ (βˆ€ n, u n < x) ∧ tendsto u at_top (𝓝 x) := begin obtain ⟨y, hy⟩ : βˆƒ y, y < x := no_bot _, exact exists_seq_strict_mono_tendsto' hy end lemma exists_seq_tendsto_Sup {Ξ± : Type*} [conditionally_complete_linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±] [first_countable_topology Ξ±] {S : set Ξ±} (hS : S.nonempty) (hS' : bdd_above S) : βˆƒ (u : β„• β†’ Ξ±), monotone u ∧ tendsto u at_top (𝓝 (Sup S)) ∧ (βˆ€ n, u n ∈ S) := begin rcases (is_lub_cSup hS hS').exists_seq_monotone_tendsto hS with ⟨u, hu⟩, exact ⟨u, hu.1, hu.2.2⟩, end lemma is_glb.exists_seq_strict_mono_tendsto_of_not_mem' {t : set Ξ±} {x : Ξ±} (htx : is_glb t x) (not_mem : x βˆ‰ t) (ht : t.nonempty) (hx : is_countably_generated (𝓝 x)) : βˆƒ u : β„• β†’ Ξ±, (βˆ€ m n, m < n β†’ u n < u m) ∧ (βˆ€ n, x < u n) ∧ tendsto u at_top (𝓝 x) ∧ (βˆ€ n, u n ∈ t) := @is_lub.exists_seq_strict_mono_tendsto_of_not_mem' (order_dual Ξ±) _ _ _ t x htx not_mem ht hx lemma is_glb.exists_seq_monotone_tendsto' {t : set Ξ±} {x : Ξ±} (htx : is_glb t x) (ht : t.nonempty) (hx : is_countably_generated (𝓝 x)) : βˆƒ u : β„• β†’ Ξ±, (βˆ€ m n, m ≀ n β†’ u n ≀ u m) ∧ (βˆ€ n, x ≀ u n) ∧ tendsto u at_top (𝓝 x) ∧ (βˆ€ n, u n ∈ t) := @is_lub.exists_seq_monotone_tendsto' (order_dual Ξ±) _ _ _ t x htx ht hx lemma is_glb.exists_seq_strict_mono_tendsto_of_not_mem [first_countable_topology Ξ±] {t : set Ξ±} {x : Ξ±} (htx : is_glb t x) (ht : t.nonempty) (not_mem : x βˆ‰ t) : βˆƒ u : β„• β†’ Ξ±, (βˆ€ m n, m < n β†’ u n < u m) ∧ (βˆ€ n, x < u n) ∧ tendsto u at_top (𝓝 x) ∧ (βˆ€ n, u n ∈ t) := htx.exists_seq_strict_mono_tendsto_of_not_mem' not_mem ht (is_countably_generated_nhds x) lemma is_glb.exists_seq_monotone_tendsto [first_countable_topology Ξ±] {t : set Ξ±} {x : Ξ±} (htx : is_glb t x) (ht : t.nonempty) : βˆƒ u : β„• β†’ Ξ±, (βˆ€ m n, m ≀ n β†’ u n ≀ u m) ∧ (βˆ€ n, x ≀ u n) ∧ tendsto u at_top (𝓝 x) ∧ (βˆ€ n, u n ∈ t) := htx.exists_seq_monotone_tendsto' ht (is_countably_generated_nhds x) lemma exists_seq_strict_antimono_tendsto' [densely_ordered Ξ±] [first_countable_topology Ξ±] {x y : Ξ±} (hy : x < y) : βˆƒ u : β„• β†’ Ξ±, (βˆ€ m n, m < n β†’ u n < u m) ∧ (βˆ€ n, x < u n) ∧ tendsto u at_top (𝓝 x) := @exists_seq_strict_mono_tendsto' (order_dual Ξ±) _ _ _ _ _ x y hy lemma exists_seq_strict_antimono_tendsto [densely_ordered Ξ±] [no_top_order Ξ±] [first_countable_topology Ξ±] (x : Ξ±) : βˆƒ u : β„• β†’ Ξ±, (βˆ€ m n, m < n β†’ u n < u m) ∧ (βˆ€ n, x < u n) ∧ tendsto u at_top (𝓝 x) := @exists_seq_strict_mono_tendsto (order_dual Ξ±) _ _ _ _ _ _ x lemma exists_seq_tendsto_Inf {Ξ± : Type*} [conditionally_complete_linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±] [first_countable_topology Ξ±] {S : set Ξ±} (hS : S.nonempty) (hS' : bdd_below S) : βˆƒ (u : β„• β†’ Ξ±), (βˆ€ m n, m ≀ n β†’ u n ≀ u m) ∧ tendsto u at_top (𝓝 (Inf S)) ∧ (βˆ€ n, u n ∈ S) := @exists_seq_tendsto_Sup (order_dual Ξ±) _ _ _ _ S hS hS' /-- A compact set is bounded below -/ lemma is_compact.bdd_below {Ξ± : Type u} [topological_space Ξ±] [linear_order Ξ±] [order_closed_topology Ξ±] [nonempty Ξ±] {s : set Ξ±} (hs : is_compact s) : bdd_below s := begin by_contra H, rcases hs.elim_finite_subcover_image (Ξ» x (_ : x ∈ s), @is_open_Ioi _ _ _ _ x) _ with ⟨t, st, ft, ht⟩, { refine H (ft.bdd_below.imp $ Ξ» C hC y hy, _), rcases mem_bUnion_iff.1 (ht hy) with ⟨x, hx, xy⟩, exact le_trans (hC hx) (le_of_lt xy) }, { refine Ξ» x hx, mem_bUnion_iff.2 (not_imp_comm.1 _ H), exact Ξ» h, ⟨x, Ξ» y hy, le_of_not_lt (h.imp $ Ξ» ys, ⟨_, hy, ys⟩)⟩ } end /-- A compact set is bounded above -/ lemma is_compact.bdd_above {Ξ± : Type u} [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] : Ξ  [nonempty Ξ±] {s : set Ξ±}, is_compact s β†’ bdd_above s := @is_compact.bdd_below (order_dual Ξ±) _ _ _ end order_topology section densely_ordered variables [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] [densely_ordered Ξ±] {a b : Ξ±} {s : set Ξ±} /-- The closure of the interval `(a, +∞)` is the closed interval `[a, +∞)`, unless `a` is a top element. -/ lemma closure_Ioi' {a b : Ξ±} (hab : a < b) : closure (Ioi a) = Ici a := begin apply subset.antisymm, { exact closure_minimal Ioi_subset_Ici_self is_closed_Ici }, { rw [← diff_subset_closure_iff, Ici_diff_Ioi_same, singleton_subset_iff], exact is_glb_Ioi.mem_closure ⟨_, hab⟩ } end /-- The closure of the interval `(a, +∞)` is the closed interval `[a, +∞)`. -/ @[simp] lemma closure_Ioi (a : Ξ±) [no_top_order Ξ±] : closure (Ioi a) = Ici a := let ⟨b, hb⟩ := no_top a in closure_Ioi' hb /-- The closure of the interval `(-∞, a)` is the closed interval `(-∞, a]`, unless `a` is a bottom element. -/ lemma closure_Iio' {a b : Ξ±} (hab : b < a) : closure (Iio a) = Iic a := @closure_Ioi' (order_dual Ξ±) _ _ _ _ _ _ hab /-- The closure of the interval `(-∞, a)` is the interval `(-∞, a]`. -/ @[simp] lemma closure_Iio (a : Ξ±) [no_bot_order Ξ±] : closure (Iio a) = Iic a := let ⟨b, hb⟩ := no_bot a in closure_Iio' hb /-- The closure of the open interval `(a, b)` is the closed interval `[a, b]`. -/ @[simp] lemma closure_Ioo {a b : Ξ±} (hab : a < b) : closure (Ioo a b) = Icc a b := begin apply subset.antisymm, { exact closure_minimal Ioo_subset_Icc_self is_closed_Icc }, { rw [← diff_subset_closure_iff, Icc_diff_Ioo_same hab.le], have hab' : (Ioo a b).nonempty, from nonempty_Ioo.2 hab, simp only [insert_subset, singleton_subset_iff], exact ⟨(is_glb_Ioo hab).mem_closure hab', (is_lub_Ioo hab).mem_closure hab'⟩ } end /-- The closure of the interval `(a, b]` is the closed interval `[a, b]`. -/ @[simp] lemma closure_Ioc {a b : Ξ±} (hab : a < b) : closure (Ioc a b) = Icc a b := begin apply subset.antisymm, { exact closure_minimal Ioc_subset_Icc_self is_closed_Icc }, { apply subset.trans _ (closure_mono Ioo_subset_Ioc_self), rw closure_Ioo hab } end /-- The closure of the interval `[a, b)` is the closed interval `[a, b]`. -/ @[simp] lemma closure_Ico {a b : Ξ±} (hab : a < b) : closure (Ico a b) = Icc a b := begin apply subset.antisymm, { exact closure_minimal Ico_subset_Icc_self is_closed_Icc }, { apply subset.trans _ (closure_mono Ioo_subset_Ico_self), rw closure_Ioo hab } end @[simp] lemma interior_Ici [no_bot_order Ξ±] {a : Ξ±} : interior (Ici a) = Ioi a := by rw [← compl_Iio, interior_compl, closure_Iio, compl_Iic] @[simp] lemma interior_Iic [no_top_order Ξ±] {a : Ξ±} : interior (Iic a) = Iio a := by rw [← compl_Ioi, interior_compl, closure_Ioi, compl_Ici] @[simp] lemma interior_Icc [no_bot_order Ξ±] [no_top_order Ξ±] {a b : Ξ±}: interior (Icc a b) = Ioo a b := by rw [← Ici_inter_Iic, interior_inter, interior_Ici, interior_Iic, Ioi_inter_Iio] @[simp] lemma interior_Ico [no_bot_order Ξ±] {a b : Ξ±} : interior (Ico a b) = Ioo a b := by rw [← Ici_inter_Iio, interior_inter, interior_Ici, interior_Iio, Ioi_inter_Iio] @[simp] lemma interior_Ioc [no_top_order Ξ±] {a b : Ξ±} : interior (Ioc a b) = Ioo a b := by rw [← Ioi_inter_Iic, interior_inter, interior_Ioi, interior_Iic, Ioi_inter_Iio] @[simp] lemma frontier_Ici [no_bot_order Ξ±] {a : Ξ±} : frontier (Ici a) = {a} := by simp [frontier] @[simp] lemma frontier_Iic [no_top_order Ξ±] {a : Ξ±} : frontier (Iic a) = {a} := by simp [frontier] @[simp] lemma frontier_Ioi [no_top_order Ξ±] {a : Ξ±} : frontier (Ioi a) = {a} := by simp [frontier] @[simp] lemma frontier_Iio [no_bot_order Ξ±] {a : Ξ±} : frontier (Iio a) = {a} := by simp [frontier] @[simp] lemma frontier_Icc [no_bot_order Ξ±] [no_top_order Ξ±] {a b : Ξ±} (h : a < b) : frontier (Icc a b) = {a, b} := by simp [frontier, le_of_lt h, Icc_diff_Ioo_same] @[simp] lemma frontier_Ioo {a b : Ξ±} (h : a < b) : frontier (Ioo a b) = {a, b} := by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same] @[simp] lemma frontier_Ico [no_bot_order Ξ±] {a b : Ξ±} (h : a < b) : frontier (Ico a b) = {a, b} := by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same] @[simp] lemma frontier_Ioc [no_top_order Ξ±] {a b : Ξ±} (h : a < b) : frontier (Ioc a b) = {a, b} := by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same] lemma nhds_within_Ioi_ne_bot' {a b c : Ξ±} (H₁ : a < c) (Hβ‚‚ : a ≀ b) : ne_bot (𝓝[Ioi a] b) := mem_closure_iff_nhds_within_ne_bot.1 $ by { rw [closure_Ioi' H₁], exact Hβ‚‚ } lemma nhds_within_Ioi_ne_bot [no_top_order Ξ±] {a b : Ξ±} (H : a ≀ b) : ne_bot (𝓝[Ioi a] b) := let ⟨c, hc⟩ := no_top a in nhds_within_Ioi_ne_bot' hc H lemma nhds_within_Ioi_self_ne_bot' {a b : Ξ±} (H : a < b) : ne_bot (𝓝[Ioi a] a) := nhds_within_Ioi_ne_bot' H (le_refl a) @[instance] lemma nhds_within_Ioi_self_ne_bot [no_top_order Ξ±] (a : Ξ±) : ne_bot (𝓝[Ioi a] a) := nhds_within_Ioi_ne_bot (le_refl a) lemma nhds_within_Iio_ne_bot' {a b c : Ξ±} (H₁ : a < c) (Hβ‚‚ : b ≀ c) : ne_bot (𝓝[Iio c] b) := mem_closure_iff_nhds_within_ne_bot.1 $ by { rw [closure_Iio' H₁], exact Hβ‚‚ } lemma nhds_within_Iio_ne_bot [no_bot_order Ξ±] {a b : Ξ±} (H : a ≀ b) : ne_bot (𝓝[Iio b] a) := let ⟨c, hc⟩ := no_bot b in nhds_within_Iio_ne_bot' hc H lemma nhds_within_Iio_self_ne_bot' {a b : Ξ±} (H : a < b) : ne_bot (𝓝[Iio b] b) := nhds_within_Iio_ne_bot' H (le_refl b) @[instance] lemma nhds_within_Iio_self_ne_bot [no_bot_order Ξ±] (a : Ξ±) : ne_bot (𝓝[Iio a] a) := nhds_within_Iio_ne_bot (le_refl a) lemma right_nhds_within_Ico_ne_bot {a b : Ξ±} (H : a < b) : ne_bot (𝓝[Ico a b] b) := (is_lub_Ico H).nhds_within_ne_bot (nonempty_Ico.2 H) lemma left_nhds_within_Ioc_ne_bot {a b : Ξ±} (H : a < b) : ne_bot (𝓝[Ioc a b] a) := (is_glb_Ioc H).nhds_within_ne_bot (nonempty_Ioc.2 H) lemma left_nhds_within_Ioo_ne_bot {a b : Ξ±} (H : a < b) : ne_bot (𝓝[Ioo a b] a) := (is_glb_Ioo H).nhds_within_ne_bot (nonempty_Ioo.2 H) lemma right_nhds_within_Ioo_ne_bot {a b : Ξ±} (H : a < b) : ne_bot (𝓝[Ioo a b] b) := (is_lub_Ioo H).nhds_within_ne_bot (nonempty_Ioo.2 H) lemma comap_coe_nhds_within_Iio_of_Ioo_subset (hb : s βŠ† Iio b) (hs : s.nonempty β†’ βˆƒ a < b, Ioo a b βŠ† s) : comap (coe : s β†’ Ξ±) (𝓝[Iio b] b) = at_top := begin nontriviality, haveI : nonempty s := nontrivial_iff_nonempty.1 β€Ή_β€Ί, rcases hs (nonempty_subtype.1 β€Ή_β€Ί) with ⟨a, h, hs⟩, ext u, split, { rintros ⟨t, ht, hts⟩, obtain ⟨x, ⟨hxa : a ≀ x, hxb : x < b⟩, hxt : Ioo x b βŠ† t⟩ := (mem_nhds_within_Iio_iff_exists_mem_Ico_Ioo_subset h).mp ht, obtain ⟨y, hxy, hyb⟩ := exists_between hxb, refine mem_of_superset (mem_at_top ⟨y, hs ⟨hxa.trans_lt hxy, hyb⟩⟩) _, rintros ⟨z, hzs⟩ (hyz : y ≀ z), refine hts (hxt ⟨hxy.trans_le _, hb _⟩); assumption }, { intros hu, obtain ⟨x : s, hx : βˆ€ z, x ≀ z β†’ z ∈ u⟩ := mem_at_top_sets.1 hu, exact ⟨Ioo x b, Ioo_mem_nhds_within_Iio (right_mem_Ioc.2 $ hb x.2), Ξ» z hz, hx _ hz.1.le⟩ } end lemma comap_coe_nhds_within_Ioi_of_Ioo_subset (ha : s βŠ† Ioi a) (hs : s.nonempty β†’ βˆƒ b > a, Ioo a b βŠ† s) : comap (coe : s β†’ Ξ±) (𝓝[Ioi a] a) = at_bot := begin refine @comap_coe_nhds_within_Iio_of_Ioo_subset (order_dual Ξ±) _ _ _ _ _ _ ha (Ξ» h, _), rcases hs h with ⟨b, hab, h⟩, use [b, hab], rwa dual_Ioo end lemma map_coe_at_top_of_Ioo_subset (hb : s βŠ† Iio b) (hs : βˆ€ a' < b, βˆƒ a < b, Ioo a b βŠ† s) : map (coe : s β†’ Ξ±) at_top = 𝓝[Iio b] b := begin rcases eq_empty_or_nonempty (Iio b) with (hb'|⟨a, ha⟩), { rw [filter_eq_bot_of_is_empty at_top, map_bot, hb', nhds_within_empty], exact ⟨λ x, hb'.subset (hb x.2)⟩ }, { rw [← comap_coe_nhds_within_Iio_of_Ioo_subset hb (Ξ» _, hs a ha), map_comap_of_mem], rw subtype.range_coe, exact (mem_nhds_within_Iio_iff_exists_Ioo_subset' ha).2 (hs a ha) }, end lemma map_coe_at_bot_of_Ioo_subset (ha : s βŠ† Ioi a) (hs : βˆ€ b' > a, βˆƒ b > a, Ioo a b βŠ† s) : map (coe : s β†’ Ξ±) at_bot = (𝓝[Ioi a] a) := begin refine @map_coe_at_top_of_Ioo_subset (order_dual Ξ±) _ _ _ _ a s ha (Ξ» b' hb', _), rcases hs b' hb' with ⟨b, hab, hbs⟩, use [b, hab], rwa dual_Ioo end /-- The `at_top` filter for an open interval `Ioo a b` comes from the left-neighbourhoods filter at the right endpoint in the ambient order. -/ lemma comap_coe_Ioo_nhds_within_Iio (a b : Ξ±) : comap (coe : Ioo a b β†’ Ξ±) (𝓝[Iio b] b) = at_top := comap_coe_nhds_within_Iio_of_Ioo_subset Ioo_subset_Iio_self $ Ξ» h, ⟨a, nonempty_Ioo.1 h, subset.refl _⟩ /-- The `at_bot` filter for an open interval `Ioo a b` comes from the right-neighbourhoods filter at the left endpoint in the ambient order. -/ lemma comap_coe_Ioo_nhds_within_Ioi (a b : Ξ±) : comap (coe : Ioo a b β†’ Ξ±) (𝓝[Ioi a] a) = at_bot := comap_coe_nhds_within_Ioi_of_Ioo_subset Ioo_subset_Ioi_self $ Ξ» h, ⟨b, nonempty_Ioo.1 h, subset.refl _⟩ lemma comap_coe_Ioi_nhds_within_Ioi (a : Ξ±) : comap (coe : Ioi a β†’ Ξ±) (𝓝[Ioi a] a) = at_bot := comap_coe_nhds_within_Ioi_of_Ioo_subset (subset.refl _) $ Ξ» ⟨x, hx⟩, ⟨x, hx, Ioo_subset_Ioi_self⟩ lemma comap_coe_Iio_nhds_within_Iio (a : Ξ±) : comap (coe : Iio a β†’ Ξ±) (𝓝[Iio a] a) = at_top := @comap_coe_Ioi_nhds_within_Ioi (order_dual Ξ±) _ _ _ _ a @[simp] lemma map_coe_Ioo_at_top {a b : Ξ±} (h : a < b) : map (coe : Ioo a b β†’ Ξ±) at_top = 𝓝[Iio b] b := map_coe_at_top_of_Ioo_subset Ioo_subset_Iio_self $ Ξ» _ _, ⟨_, h, subset.refl _⟩ @[simp] lemma map_coe_Ioo_at_bot {a b : Ξ±} (h : a < b) : map (coe : Ioo a b β†’ Ξ±) at_bot = 𝓝[Ioi a] a := map_coe_at_bot_of_Ioo_subset Ioo_subset_Ioi_self $ Ξ» _ _, ⟨_, h, subset.refl _⟩ @[simp] lemma map_coe_Ioi_at_bot (a : Ξ±) : map (coe : Ioi a β†’ Ξ±) at_bot = 𝓝[Ioi a] a := map_coe_at_bot_of_Ioo_subset (subset.refl _) $ Ξ» b hb, ⟨b, hb, Ioo_subset_Ioi_self⟩ @[simp] lemma map_coe_Iio_at_top (a : Ξ±) : map (coe : Iio a β†’ Ξ±) at_top = 𝓝[Iio a] a := @map_coe_Ioi_at_bot (order_dual Ξ±) _ _ _ _ _ variables {l : filter Ξ²} {f : Ξ± β†’ Ξ²} @[simp] lemma tendsto_comp_coe_Ioo_at_top (h : a < b) : tendsto (Ξ» x : Ioo a b, f x) at_top l ↔ tendsto f (𝓝[Iio b] b) l := by rw [← map_coe_Ioo_at_top h, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Ioo_at_bot (h : a < b) : tendsto (Ξ» x : Ioo a b, f x) at_bot l ↔ tendsto f (𝓝[Ioi a] a) l := by rw [← map_coe_Ioo_at_bot h, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Ioi_at_bot : tendsto (Ξ» x : Ioi a, f x) at_bot l ↔ tendsto f (𝓝[Ioi a] a) l := by rw [← map_coe_Ioi_at_bot, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Iio_at_top : tendsto (Ξ» x : Iio a, f x) at_top l ↔ tendsto f (𝓝[Iio a] a) l := by rw [← map_coe_Iio_at_top, tendsto_map'_iff] @[simp] lemma tendsto_Ioo_at_top {f : Ξ² β†’ Ioo a b} : tendsto f l at_top ↔ tendsto (Ξ» x, (f x : Ξ±)) l (𝓝[Iio b] b) := by rw [← comap_coe_Ioo_nhds_within_Iio, tendsto_comap_iff] @[simp] lemma tendsto_Ioo_at_bot {f : Ξ² β†’ Ioo a b} : tendsto f l at_bot ↔ tendsto (Ξ» x, (f x : Ξ±)) l (𝓝[Ioi a] a) := by rw [← comap_coe_Ioo_nhds_within_Ioi, tendsto_comap_iff] @[simp] lemma tendsto_Ioi_at_bot {f : Ξ² β†’ Ioi a} : tendsto f l at_bot ↔ tendsto (Ξ» x, (f x : Ξ±)) l (𝓝[Ioi a] a) := by rw [← comap_coe_Ioi_nhds_within_Ioi, tendsto_comap_iff] @[simp] lemma tendsto_Iio_at_top {f : Ξ² β†’ Iio a} : tendsto f l at_top ↔ tendsto (Ξ» x, (f x : Ξ±)) l (𝓝[Iio a] a) := by rw [← comap_coe_Iio_nhds_within_Iio, tendsto_comap_iff] end densely_ordered section complete_linear_order variables [complete_linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±] [complete_linear_order Ξ²] [topological_space Ξ²] [order_topology Ξ²] [nonempty Ξ³] lemma Sup_mem_closure {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) : Sup s ∈ closure s := (is_lub_Sup s).mem_closure hs lemma Inf_mem_closure {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) : Inf s ∈ closure s := (is_glb_Inf s).mem_closure hs lemma is_closed.Sup_mem {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) (hc : is_closed s) : Sup s ∈ s := (is_lub_Sup s).mem_of_is_closed hs hc lemma is_closed.Inf_mem {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) (hc : is_closed s) : Inf s ∈ s := (is_glb_Inf s).mem_of_is_closed hs hc /-- A monotone function continuous at the supremum of a nonempty set sends this supremum to the supremum of the image of this set. -/ lemma map_Sup_of_continuous_at_of_monotone' {f : Ξ± β†’ Ξ²} {s : set Ξ±} (Cf : continuous_at f (Sup s)) (Mf : monotone f) (hs : s.nonempty) : f (Sup s) = Sup (f '' s) := --This is a particular case of the more general is_lub.is_lub_of_tendsto ((is_lub_Sup _).is_lub_of_tendsto (Ξ» x hx y hy xy, Mf xy) hs $ Cf.mono_left inf_le_left).Sup_eq.symm /-- A monotone function `s` sending `bot` to `bot` and continuous at the supremum of a set sends this supremum to the supremum of the image of this set. -/ lemma map_Sup_of_continuous_at_of_monotone {f : Ξ± β†’ Ξ²} {s : set Ξ±} (Cf : continuous_at f (Sup s)) (Mf : monotone f) (fbot : f βŠ₯ = βŠ₯) : f (Sup s) = Sup (f '' s) := begin cases s.eq_empty_or_nonempty with h h, { simp [h, fbot] }, { exact map_Sup_of_continuous_at_of_monotone' Cf Mf h } end /-- A monotone function continuous at the indexed supremum over a nonempty `Sort` sends this indexed supremum to the indexed supremum of the composition. -/ lemma map_supr_of_continuous_at_of_monotone' {ΞΉ : Sort*} [nonempty ΞΉ] {f : Ξ± β†’ Ξ²} {g : ΞΉ β†’ Ξ±} (Cf : continuous_at f (supr g)) (Mf : monotone f) : f (⨆ i, g i) = ⨆ i, f (g i) := by rw [supr, map_Sup_of_continuous_at_of_monotone' Cf Mf (range_nonempty g), ← range_comp, supr] /-- If a monotone function sending `bot` to `bot` is continuous at the indexed supremum over a `Sort`, then it sends this indexed supremum to the indexed supremum of the composition. -/ lemma map_supr_of_continuous_at_of_monotone {ΞΉ : Sort*} {f : Ξ± β†’ Ξ²} {g : ΞΉ β†’ Ξ±} (Cf : continuous_at f (supr g)) (Mf : monotone f) (fbot : f βŠ₯ = βŠ₯) : f (⨆ i, g i) = ⨆ i, f (g i) := by rw [supr, map_Sup_of_continuous_at_of_monotone Cf Mf fbot, ← range_comp, supr] /-- A monotone function continuous at the infimum of a nonempty set sends this infimum to the infimum of the image of this set. -/ lemma map_Inf_of_continuous_at_of_monotone' {f : Ξ± β†’ Ξ²} {s : set Ξ±} (Cf : continuous_at f (Inf s)) (Mf : monotone f) (hs : s.nonempty) : f (Inf s) = Inf (f '' s) := @map_Sup_of_continuous_at_of_monotone' (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ f s Cf Mf.order_dual hs /-- A monotone function `s` sending `top` to `top` and continuous at the infimum of a set sends this infimum to the infimum of the image of this set. -/ lemma map_Inf_of_continuous_at_of_monotone {f : Ξ± β†’ Ξ²} {s : set Ξ±} (Cf : continuous_at f (Inf s)) (Mf : monotone f) (ftop : f ⊀ = ⊀) : f (Inf s) = Inf (f '' s) := @map_Sup_of_continuous_at_of_monotone (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ f s Cf Mf.order_dual ftop /-- A monotone function continuous at the indexed infimum over a nonempty `Sort` sends this indexed infimum to the indexed infimum of the composition. -/ lemma map_infi_of_continuous_at_of_monotone' {ΞΉ : Sort*} [nonempty ΞΉ] {f : Ξ± β†’ Ξ²} {g : ΞΉ β†’ Ξ±} (Cf : continuous_at f (infi g)) (Mf : monotone f) : f (β¨… i, g i) = β¨… i, f (g i) := @map_supr_of_continuous_at_of_monotone' (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ ΞΉ _ f g Cf Mf.order_dual /-- If a monotone function sending `top` to `top` is continuous at the indexed infimum over a `Sort`, then it sends this indexed infimum to the indexed infimum of the composition. -/ lemma map_infi_of_continuous_at_of_monotone {ΞΉ : Sort*} {f : Ξ± β†’ Ξ²} {g : ΞΉ β†’ Ξ±} (Cf : continuous_at f (infi g)) (Mf : monotone f) (ftop : f ⊀ = ⊀) : f (infi g) = infi (f ∘ g) := @map_supr_of_continuous_at_of_monotone (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ ΞΉ f g Cf Mf.order_dual ftop end complete_linear_order section conditionally_complete_linear_order variables [conditionally_complete_linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±] [conditionally_complete_linear_order Ξ²] [topological_space Ξ²] [order_topology Ξ²] [nonempty Ξ³] lemma cSup_mem_closure {s : set Ξ±} (hs : s.nonempty) (B : bdd_above s) : Sup s ∈ closure s := (is_lub_cSup hs B).mem_closure hs lemma cInf_mem_closure {s : set Ξ±} (hs : s.nonempty) (B : bdd_below s) : Inf s ∈ closure s := (is_glb_cInf hs B).mem_closure hs lemma is_closed.cSup_mem {s : set Ξ±} (hc : is_closed s) (hs : s.nonempty) (B : bdd_above s) : Sup s ∈ s := (is_lub_cSup hs B).mem_of_is_closed hs hc lemma is_closed.cInf_mem {s : set Ξ±} (hc : is_closed s) (hs : s.nonempty) (B : bdd_below s) : Inf s ∈ s := (is_glb_cInf hs B).mem_of_is_closed hs hc /-- If a monotone function is continuous at the supremum of a nonempty bounded above set `s`, then it sends this supremum to the supremum of the image of `s`. -/ lemma map_cSup_of_continuous_at_of_monotone {f : Ξ± β†’ Ξ²} {s : set Ξ±} (Cf : continuous_at f (Sup s)) (Mf : monotone f) (ne : s.nonempty) (H : bdd_above s) : f (Sup s) = Sup (f '' s) := begin refine ((is_lub_cSup (ne.image f) (Mf.map_bdd_above H)).unique _).symm, refine (is_lub_cSup ne H).is_lub_of_tendsto (Ξ»x hx y hy xy, Mf xy) ne _, exact Cf.mono_left inf_le_left end /-- If a monotone function is continuous at the indexed supremum of a bounded function on a nonempty `Sort`, then it sends this supremum to the supremum of the composition. -/ lemma map_csupr_of_continuous_at_of_monotone {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ±} (Cf : continuous_at f (⨆ i, g i)) (Mf : monotone f) (H : bdd_above (range g)) : f (⨆ i, g i) = ⨆ i, f (g i) := by rw [supr, map_cSup_of_continuous_at_of_monotone Cf Mf (range_nonempty _) H, ← range_comp, supr] /-- If a monotone function is continuous at the infimum of a nonempty bounded below set `s`, then it sends this infimum to the infimum of the image of `s`. -/ lemma map_cInf_of_continuous_at_of_monotone {f : Ξ± β†’ Ξ²} {s : set Ξ±} (Cf : continuous_at f (Inf s)) (Mf : monotone f) (ne : s.nonempty) (H : bdd_below s) : f (Inf s) = Inf (f '' s) := @map_cSup_of_continuous_at_of_monotone (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ f s Cf Mf.order_dual ne H /-- A continuous monotone function sends indexed infimum to indexed infimum in conditionally complete linear order, under a boundedness assumption. -/ lemma map_cinfi_of_continuous_at_of_monotone {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ±} (Cf : continuous_at f (β¨… i, g i)) (Mf : monotone f) (H : bdd_below (range g)) : f (β¨… i, g i) = β¨… i, f (g i) := @map_csupr_of_continuous_at_of_monotone (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ _ _ _ _ Cf Mf.order_dual H /-- A monotone map has a limit to the left of any point `x`, equal to `Sup (f '' (Iio x))`. -/ lemma monotone.tendsto_nhds_within_Iio {Ξ± : Type*} [linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±] {f : Ξ± β†’ Ξ²} (Mf : monotone f) (x : Ξ±) : tendsto f (𝓝[Iio x] x) (𝓝 (Sup (f '' (Iio x)))) := begin rcases eq_empty_or_nonempty (Iio x) with h|h, { simp [h] }, refine tendsto_order.2 ⟨λ l hl, _, Ξ» m hm, _⟩, { obtain ⟨z, zx, lz⟩ : βˆƒ (a : Ξ±), a < x ∧ l < f a, by simpa only [mem_image, exists_prop, exists_exists_and_eq_and] using exists_lt_of_lt_cSup (nonempty_image_iff.2 h) hl, exact (mem_nhds_within_Iio_iff_exists_Ioo_subset' zx).2 ⟨z, zx, Ξ» y hy, lz.trans_le (Mf (hy.1.le))⟩ }, { filter_upwards [self_mem_nhds_within], assume y hy, apply lt_of_le_of_lt _ hm, exact le_cSup (Mf.map_bdd_above bdd_above_Iio) (mem_image_of_mem _ hy) } end /-- A monotone map has a limit to the right of any point `x`, equal to `Inf (f '' (Ioi x))`. -/ lemma monotone.tendsto_nhds_within_Ioi {Ξ± : Type*} [linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±] {f : Ξ± β†’ Ξ²} (Mf : monotone f) (x : Ξ±) : tendsto f (𝓝[Ioi x] x) (𝓝 (Inf (f '' (Ioi x)))) := @monotone.tendsto_nhds_within_Iio (order_dual Ξ²) _ _ _ (order_dual Ξ±) _ _ _ f Mf.order_dual x /-- A bounded connected subset of a conditionally complete linear order includes the open interval `(Inf s, Sup s)`. -/ lemma is_connected.Ioo_cInf_cSup_subset {s : set Ξ±} (hs : is_connected s) (hb : bdd_below s) (ha : bdd_above s) : Ioo (Inf s) (Sup s) βŠ† s := Ξ» x hx, let ⟨y, ys, hy⟩ := (is_glb_lt_iff (is_glb_cInf hs.nonempty hb)).1 hx.1 in let ⟨z, zs, hz⟩ := (lt_is_lub_iff (is_lub_cSup hs.nonempty ha)).1 hx.2 in hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩ lemma eq_Icc_cInf_cSup_of_connected_bdd_closed {s : set Ξ±} (hc : is_connected s) (hb : bdd_below s) (ha : bdd_above s) (hcl : is_closed s) : s = Icc (Inf s) (Sup s) := subset.antisymm (subset_Icc_cInf_cSup hb ha) $ hc.Icc_subset (hcl.cInf_mem hc.nonempty hb) (hcl.cSup_mem hc.nonempty ha) lemma is_preconnected.Ioi_cInf_subset {s : set Ξ±} (hs : is_preconnected s) (hb : bdd_below s) (ha : Β¬bdd_above s) : Ioi (Inf s) βŠ† s := begin have sne : s.nonempty := @nonempty_of_not_bdd_above Ξ± _ s ⟨Inf βˆ…βŸ© ha, intros x hx, obtain ⟨y, ys, hy⟩ : βˆƒ y ∈ s, y < x := (is_glb_lt_iff (is_glb_cInf sne hb)).1 hx, obtain ⟨z, zs, hz⟩ : βˆƒ z ∈ s, x < z := not_bdd_above_iff.1 ha x, exact hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩ end lemma is_preconnected.Iio_cSup_subset {s : set Ξ±} (hs : is_preconnected s) (hb : Β¬bdd_below s) (ha : bdd_above s) : Iio (Sup s) βŠ† s := @is_preconnected.Ioi_cInf_subset (order_dual Ξ±) _ _ _ s hs ha hb /-- A preconnected set in a conditionally complete linear order is either one of the intervals `[Inf s, Sup s]`, `[Inf s, Sup s)`, `(Inf s, Sup s]`, `(Inf s, Sup s)`, `[Inf s, +∞)`, `(Inf s, +∞)`, `(-∞, Sup s]`, `(-∞, Sup s)`, `(-∞, +∞)`, or `βˆ…`. The converse statement requires `Ξ±` to be densely ordererd. -/ lemma is_preconnected.mem_intervals {s : set Ξ±} (hs : is_preconnected s) : s ∈ ({Icc (Inf s) (Sup s), Ico (Inf s) (Sup s), Ioc (Inf s) (Sup s), Ioo (Inf s) (Sup s), Ici (Inf s), Ioi (Inf s), Iic (Sup s), Iio (Sup s), univ, βˆ…} : set (set Ξ±)) := begin rcases s.eq_empty_or_nonempty with rfl|hne, { apply_rules [or.inr, mem_singleton] }, have hs' : is_connected s := ⟨hne, hs⟩, by_cases hb : bdd_below s; by_cases ha : bdd_above s, { rcases mem_Icc_Ico_Ioc_Ioo_of_subset_of_subset (hs'.Ioo_cInf_cSup_subset hb ha) (subset_Icc_cInf_cSup hb ha) with hs|hs|hs|hs, { exact (or.inl hs) }, { exact (or.inr $ or.inl hs) }, { exact (or.inr $ or.inr $ or.inl hs) }, { exact (or.inr $ or.inr $ or.inr $ or.inl hs) } }, { refine (or.inr $ or.inr $ or.inr $ or.inr _), cases mem_Ici_Ioi_of_subset_of_subset (hs.Ioi_cInf_subset hb ha) (Ξ» x hx, cInf_le hb hx) with hs hs, { exact or.inl hs }, { exact or.inr (or.inl hs) } }, { iterate 6 { apply or.inr }, cases mem_Iic_Iio_of_subset_of_subset (hs.Iio_cSup_subset hb ha) (Ξ» x hx, le_cSup ha hx) with hs hs, { exact or.inl hs }, { exact or.inr (or.inl hs) } }, { iterate 8 { apply or.inr }, exact or.inl (hs.eq_univ_of_unbounded hb ha) } end /-- A preconnected set is either one of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`, `Iic`, `Iio`, or `univ`, or `βˆ…`. The converse statement requires `Ξ±` to be densely ordered. Though one can represent `βˆ…` as `(Inf s, Inf s)`, we include it into the list of possible cases to improve readability. -/ lemma set_of_is_preconnected_subset_of_ordered : {s : set Ξ± | is_preconnected s} βŠ† -- bounded intervals (range (uncurry Icc) βˆͺ range (uncurry Ico) βˆͺ range (uncurry Ioc) βˆͺ range (uncurry Ioo)) βˆͺ -- unbounded intervals and `univ` (range Ici βˆͺ range Ioi βˆͺ range Iic βˆͺ range Iio βˆͺ {univ, βˆ…}) := begin intros s hs, rcases hs.mem_intervals with hs|hs|hs|hs|hs|hs|hs|hs|hs|hs, { exact (or.inl $ or.inl $ or.inl $ or.inl ⟨(Inf s, Sup s), hs.symm⟩) }, { exact (or.inl $ or.inl $ or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) }, { exact (or.inl $ or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) }, { exact (or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) }, { exact (or.inr $ or.inl $ or.inl $ or.inl $ or.inl ⟨Inf s, hs.symm⟩) }, { exact (or.inr $ or.inl $ or.inl $ or.inl $ or.inr ⟨Inf s, hs.symm⟩) }, { exact (or.inr $ or.inl $ or.inl $ or.inr ⟨Sup s, hs.symm⟩) }, { exact (or.inr $ or.inl $ or.inr ⟨Sup s, hs.symm⟩) }, { exact (or.inr $ or.inr $ or.inl hs) }, { exact (or.inr $ or.inr $ or.inr hs) } end /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and the set `s ∩ [a, b)` has no maximal point, then `b ∈ s`. -/ lemma is_closed.mem_of_ge_of_forall_exists_gt {a b : Ξ±} {s : set Ξ±} (hs : is_closed (s ∩ Icc a b)) (ha : a ∈ s) (hab : a ≀ b) (hgt : βˆ€ x ∈ s ∩ Ico a b, (s ∩ Ioc x b).nonempty) : b ∈ s := begin let S := s ∩ Icc a b, replace ha : a ∈ S, from ⟨ha, left_mem_Icc.2 hab⟩, have Sbd : bdd_above S, from ⟨b, Ξ» z hz, hz.2.2⟩, let c := Sup (s ∩ Icc a b), have c_mem : c ∈ S, from hs.cSup_mem ⟨_, ha⟩ Sbd, have c_le : c ≀ b, from cSup_le ⟨_, ha⟩ (Ξ» x hx, hx.2.2), cases eq_or_lt_of_le c_le with hc hc, from hc β–Έ c_mem.1, exfalso, rcases hgt c ⟨c_mem.1, c_mem.2.1, hc⟩ with ⟨x, xs, cx, xb⟩, exact not_lt_of_le (le_cSup Sbd ⟨xs, le_trans (le_cSup Sbd ha) (le_of_lt cx), xb⟩) cx end /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and for any `a ≀ x < y ≀ b`, `x ∈ s`, the set `s ∩ (x, y]` is not empty, then `[a, b] βŠ† s`. -/ lemma is_closed.Icc_subset_of_forall_exists_gt {a b : Ξ±} {s : set Ξ±} (hs : is_closed (s ∩ Icc a b)) (ha : a ∈ s) (hgt : βˆ€ x ∈ s ∩ Ico a b, βˆ€ y ∈ Ioi x, (s ∩ Ioc x y).nonempty) : Icc a b βŠ† s := begin assume y hy, have : is_closed (s ∩ Icc a y), { suffices : s ∩ Icc a y = s ∩ Icc a b ∩ Icc a y, { rw this, exact is_closed.inter hs is_closed_Icc }, rw [inter_assoc], congr, exact (inter_eq_self_of_subset_right $ Icc_subset_Icc_right hy.2).symm }, exact is_closed.mem_of_ge_of_forall_exists_gt this ha hy.1 (Ξ» x hx, hgt x ⟨hx.1, Ico_subset_Ico_right hy.2 hx.2⟩ y hx.2.2) end section densely_ordered variables [densely_ordered Ξ±] {a b : Ξ±} /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and for any `x ∈ s ∩ [a, b)` the set `s` includes some open neighborhood of `x` within `(x, +∞)`, then `[a, b] βŠ† s`. -/ lemma is_closed.Icc_subset_of_forall_mem_nhds_within {a b : Ξ±} {s : set Ξ±} (hs : is_closed (s ∩ Icc a b)) (ha : a ∈ s) (hgt : βˆ€ x ∈ s ∩ Ico a b, s ∈ 𝓝[Ioi x] x) : Icc a b βŠ† s := begin apply hs.Icc_subset_of_forall_exists_gt ha, rintros x ⟨hxs, hxab⟩ y hyxb, have : s ∩ Ioc x y ∈ 𝓝[Ioi x] x, from inter_mem (hgt x ⟨hxs, hxab⟩) (Ioc_mem_nhds_within_Ioi ⟨le_refl _, hyxb⟩), exact (nhds_within_Ioi_self_ne_bot' hxab.2).nonempty_of_mem this end /-- A closed interval in a densely ordered conditionally complete linear order is preconnected. -/ lemma is_preconnected_Icc : is_preconnected (Icc a b) := is_preconnected_closed_iff.2 begin rintros s t hs ht hab ⟨x, hx⟩ ⟨y, hy⟩, wlog hxy : x ≀ y := le_total x y using [x y s t, y x t s], have xyab : Icc x y βŠ† Icc a b := Icc_subset_Icc hx.1.1 hy.1.2, by_contradiction hst, suffices : Icc x y βŠ† s, from hst ⟨y, xyab $ right_mem_Icc.2 hxy, this $ right_mem_Icc.2 hxy, hy.2⟩, apply (is_closed.inter hs is_closed_Icc).Icc_subset_of_forall_mem_nhds_within hx.2, rintros z ⟨zs, hz⟩, have zt : z ∈ tᢜ, from Ξ» zt, hst ⟨z, xyab $ Ico_subset_Icc_self hz, zs, zt⟩, have : tᢜ ∩ Ioc z y ∈ 𝓝[Ioi z] z, { rw [← nhds_within_Ioc_eq_nhds_within_Ioi hz.2], exact mem_nhds_within.2 ⟨tᢜ, ht.is_open_compl, zt, subset.refl _⟩}, apply mem_of_superset this, have : Ioc z y βŠ† s βˆͺ t, from Ξ» w hw, hab (xyab ⟨le_trans hz.1 (le_of_lt hw.1), hw.2⟩), exact Ξ» w ⟨wt, wzy⟩, (this wzy).elim id (Ξ» h, (wt h).elim) end lemma is_preconnected_interval : is_preconnected (interval a b) := is_preconnected_Icc lemma set.ord_connected.is_preconnected {s : set Ξ±} (h : s.ord_connected) : is_preconnected s := is_preconnected_of_forall_pair $ Ξ» x y hx hy, ⟨interval x y, h.interval_subset hx hy, left_mem_interval, right_mem_interval, is_preconnected_interval⟩ lemma is_preconnected_iff_ord_connected {s : set Ξ±} : is_preconnected s ↔ ord_connected s := ⟨is_preconnected.ord_connected, set.ord_connected.is_preconnected⟩ lemma is_preconnected_Ici : is_preconnected (Ici a) := ord_connected_Ici.is_preconnected lemma is_preconnected_Iic : is_preconnected (Iic a) := ord_connected_Iic.is_preconnected lemma is_preconnected_Iio : is_preconnected (Iio a) := ord_connected_Iio.is_preconnected lemma is_preconnected_Ioi : is_preconnected (Ioi a) := ord_connected_Ioi.is_preconnected lemma is_preconnected_Ioo : is_preconnected (Ioo a b) := ord_connected_Ioo.is_preconnected lemma is_preconnected_Ioc : is_preconnected (Ioc a b) := ord_connected_Ioc.is_preconnected lemma is_preconnected_Ico : is_preconnected (Ico a b) := ord_connected_Ico.is_preconnected @[priority 100] instance ordered_connected_space : preconnected_space Ξ± := ⟨ord_connected_univ.is_preconnected⟩ /-- In a dense conditionally complete linear order, the set of preconnected sets is exactly the set of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`, `Iic`, `Iio`, `(-∞, +∞)`, or `βˆ…`. Though one can represent `βˆ…` as `(Inf s, Inf s)`, we include it into the list of possible cases to improve readability. -/ lemma set_of_is_preconnected_eq_of_ordered : {s : set Ξ± | is_preconnected s} = -- bounded intervals (range (uncurry Icc) βˆͺ range (uncurry Ico) βˆͺ range (uncurry Ioc) βˆͺ range (uncurry Ioo)) βˆͺ -- unbounded intervals and `univ` (range Ici βˆͺ range Ioi βˆͺ range Iic βˆͺ range Iio βˆͺ {univ, βˆ…}) := begin refine subset.antisymm set_of_is_preconnected_subset_of_ordered _, simp only [subset_def, -mem_range, forall_range_iff, uncurry, or_imp_distrib, forall_and_distrib, mem_union, mem_set_of_eq, insert_eq, mem_singleton_iff, forall_eq, forall_true_iff, and_true, is_preconnected_Icc, is_preconnected_Ico, is_preconnected_Ioc, is_preconnected_Ioo, is_preconnected_Ioi, is_preconnected_Iio, is_preconnected_Ici, is_preconnected_Iic, is_preconnected_univ, is_preconnected_empty], end variables {Ξ΄ : Type*} [linear_order Ξ΄] [topological_space Ξ΄] [order_closed_topology Ξ΄] /-- **Intermediate Value Theorem** for continuous functions on closed intervals, case `f a ≀ t ≀ f b`.-/ lemma intermediate_value_Icc {a b : Ξ±} (hab : a ≀ b) {f : Ξ± β†’ Ξ΄} (hf : continuous_on f (Icc a b)) : Icc (f a) (f b) βŠ† f '' (Icc a b) := is_preconnected_Icc.intermediate_value (left_mem_Icc.2 hab) (right_mem_Icc.2 hab) hf /-- **Intermediate Value Theorem** for continuous functions on closed intervals, case `f a β‰₯ t β‰₯ f b`.-/ lemma intermediate_value_Icc' {a b : Ξ±} (hab : a ≀ b) {f : Ξ± β†’ Ξ΄} (hf : continuous_on f (Icc a b)) : Icc (f b) (f a) βŠ† f '' (Icc a b) := is_preconnected_Icc.intermediate_value (right_mem_Icc.2 hab) (left_mem_Icc.2 hab) hf /-- **Intermediate Value Theorem** for continuous functions on closed intervals, unordered case. -/ lemma intermediate_value_interval {a b : Ξ±} {f : Ξ± β†’ Ξ΄} (hf : continuous_on f (interval a b)) : interval (f a) (f b) βŠ† f '' interval a b := by cases le_total (f a) (f b); simp [*, is_preconnected_interval.intermediate_value] lemma intermediate_value_Ico {a b : Ξ±} (hab : a ≀ b) {f : Ξ± β†’ Ξ΄} (hf : continuous_on f (Icc a b)) : Ico (f a) (f b) βŠ† f '' (Ico a b) := or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.2 (not_lt_of_le (he β–Έ h.1))) (Ξ» hlt, @is_preconnected.intermediate_value_Ico _ _ _ _ _ _ _ (is_preconnected_Ico) _ _ ⟨refl a, hlt⟩ (right_nhds_within_Ico_ne_bot hlt) inf_le_right _ (hf.mono Ico_subset_Icc_self) _ ((hf.continuous_within_at ⟨hab, refl b⟩).mono Ico_subset_Icc_self)) lemma intermediate_value_Ico' {a b : Ξ±} (hab : a ≀ b) {f : Ξ± β†’ Ξ΄} (hf : continuous_on f (Icc a b)) : Ioc (f b) (f a) βŠ† f '' (Ico a b) := or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.1 (not_lt_of_le (he β–Έ h.2))) (Ξ» hlt, @is_preconnected.intermediate_value_Ioc _ _ _ _ _ _ _ (is_preconnected_Ico) _ _ ⟨refl a, hlt⟩ (right_nhds_within_Ico_ne_bot hlt) inf_le_right _ (hf.mono Ico_subset_Icc_self) _ ((hf.continuous_within_at ⟨hab, refl b⟩).mono Ico_subset_Icc_self)) lemma intermediate_value_Ioc {a b : Ξ±} (hab : a ≀ b) {f : Ξ± β†’ Ξ΄} (hf : continuous_on f (Icc a b)) : Ioc (f a) (f b) βŠ† f '' (Ioc a b) := or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.2 (not_le_of_lt (he β–Έ h.1))) (Ξ» hlt, @is_preconnected.intermediate_value_Ioc _ _ _ _ _ _ _ (is_preconnected_Ioc) _ _ ⟨hlt, refl b⟩ (left_nhds_within_Ioc_ne_bot hlt) inf_le_right _ (hf.mono Ioc_subset_Icc_self) _ ((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioc_subset_Icc_self)) lemma intermediate_value_Ioc' {a b : Ξ±} (hab : a ≀ b) {f : Ξ± β†’ Ξ΄} (hf : continuous_on f (Icc a b)) : Ico (f b) (f a) βŠ† f '' (Ioc a b) := or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.1 (not_le_of_lt (he β–Έ h.2))) (Ξ» hlt, @is_preconnected.intermediate_value_Ico _ _ _ _ _ _ _ (is_preconnected_Ioc) _ _ ⟨hlt, refl b⟩ (left_nhds_within_Ioc_ne_bot hlt) inf_le_right _ (hf.mono Ioc_subset_Icc_self) _ ((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioc_subset_Icc_self)) lemma intermediate_value_Ioo {a b : Ξ±} (hab : a ≀ b) {f : Ξ± β†’ Ξ΄} (hf : continuous_on f (Icc a b)) : Ioo (f a) (f b) βŠ† f '' (Ioo a b) := or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.2 (not_lt_of_lt (he β–Έ h.1))) (Ξ» hlt, @is_preconnected.intermediate_value_Ioo _ _ _ _ _ _ _ (is_preconnected_Ioo) _ _ (left_nhds_within_Ioo_ne_bot hlt) (right_nhds_within_Ioo_ne_bot hlt) inf_le_right inf_le_right _ (hf.mono Ioo_subset_Icc_self) _ _ ((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioo_subset_Icc_self) ((hf.continuous_within_at ⟨hab, refl b⟩).mono Ioo_subset_Icc_self)) lemma intermediate_value_Ioo' {a b : Ξ±} (hab : a ≀ b) {f : Ξ± β†’ Ξ΄} (hf : continuous_on f (Icc a b)) : Ioo (f b) (f a) βŠ† f '' (Ioo a b) := or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.1 (not_lt_of_lt (he β–Έ h.2))) (Ξ» hlt, @is_preconnected.intermediate_value_Ioo _ _ _ _ _ _ _ (is_preconnected_Ioo) _ _ (right_nhds_within_Ioo_ne_bot hlt) (left_nhds_within_Ioo_ne_bot hlt) inf_le_right inf_le_right _ (hf.mono Ioo_subset_Icc_self) _ _ ((hf.continuous_within_at ⟨hab, refl b⟩).mono Ioo_subset_Icc_self) ((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioo_subset_Icc_self)) /-- A continuous function which tendsto `at_top` `at_top` and to `at_bot` `at_bot` is surjective. -/ lemma continuous.surjective {f : Ξ± β†’ Ξ΄} (hf : continuous f) (h_top : tendsto f at_top at_top) (h_bot : tendsto f at_bot at_bot) : function.surjective f := Ξ» p, mem_range_of_exists_le_of_exists_ge hf (h_bot.eventually (eventually_le_at_bot p)).exists (h_top.eventually (eventually_ge_at_top p)).exists /-- A continuous function which tendsto `at_bot` `at_top` and to `at_top` `at_bot` is surjective. -/ lemma continuous.surjective' {f : Ξ± β†’ Ξ΄} (hf : continuous f) (h_top : tendsto f at_bot at_top) (h_bot : tendsto f at_top at_bot) : function.surjective f := @continuous.surjective (order_dual Ξ±) _ _ _ _ _ _ _ _ _ hf h_top h_bot /-- If a function `f : Ξ± β†’ Ξ²` is continuous on a nonempty interval `s`, its restriction to `s` tends to `at_bot : filter Ξ²` along `at_bot : filter β†₯s` and tends to `at_top : filter Ξ²` along `at_top : filter β†₯s`, then the restriction of `f` to `s` is surjective. We formulate the conclusion as `surj_on f s univ`. -/ lemma continuous_on.surj_on_of_tendsto {f : Ξ± β†’ Ξ²} {s : set Ξ±} [ord_connected s] (hs : s.nonempty) (hf : continuous_on f s) (hbot : tendsto (Ξ» x : s, f x) at_bot at_bot) (htop : tendsto (Ξ» x : s, f x) at_top at_top) : surj_on f s univ := by haveI := inhabited_of_nonempty hs.to_subtype; exact (surj_on_iff_surjective.2 $ (continuous_on_iff_continuous_restrict.1 hf).surjective htop hbot) /-- If a function `f : Ξ± β†’ Ξ²` is continuous on a nonempty interval `s`, its restriction to `s` tends to `at_top : filter Ξ²` along `at_bot : filter β†₯s` and tends to `at_bot : filter Ξ²` along `at_top : filter β†₯s`, then the restriction of `f` to `s` is surjective. We formulate the conclusion as `surj_on f s univ`. -/ lemma continuous_on.surj_on_of_tendsto' {f : Ξ± β†’ Ξ²} {s : set Ξ±} [ord_connected s] (hs : s.nonempty) (hf : continuous_on f s) (hbot : tendsto (Ξ» x : s, f x) at_bot at_top) (htop : tendsto (Ξ» x : s, f x) at_top at_bot) : surj_on f s univ := @continuous_on.surj_on_of_tendsto Ξ± (order_dual Ξ²) _ _ _ _ _ _ _ _ _ _ hs hf hbot htop end densely_ordered /-- A closed interval in a conditionally complete linear order is compact. -/ lemma is_compact_Icc {a b : Ξ±} : is_compact (Icc a b) := begin cases le_or_lt a b with hab hab, swap, { simp [hab] }, refine is_compact_iff_ultrafilter_le_nhds.2 (Ξ» f hf, _), contrapose! hf, rw [le_principal_iff], have hpt : βˆ€ x ∈ Icc a b, {x} βˆ‰ f, from Ξ» x hx hxf, hf x hx ((le_pure_iff.2 hxf).trans (pure_le_nhds x)), set s := {x ∈ Icc a b | Icc a x βˆ‰ f}, have hsb : b ∈ upper_bounds s, from Ξ» x hx, hx.1.2, have sbd : bdd_above s, from ⟨b, hsb⟩, have ha : a ∈ s, by simp [hpt, hab], rcases hab.eq_or_lt with rfl|hlt, { exact ha.2 }, set c := Sup s, have hsc : is_lub s c, from is_lub_cSup ⟨a, ha⟩ sbd, have hc : c ∈ Icc a b, from ⟨hsc.1 ha, hsc.2 hsb⟩, specialize hf c hc, have hcs : c ∈ s, { cases hc.1.eq_or_lt with heq hlt, { rwa ← heq }, refine ⟨hc, Ξ» hcf, hf (Ξ» U hU, _)⟩, rcases (mem_nhds_within_Iic_iff_exists_Ioc_subset' hlt).1 (mem_nhds_within_of_mem_nhds hU) with ⟨x, hxc, hxU⟩, rcases ((hsc.frequently_mem ⟨a, ha⟩).and_eventually (Ioc_mem_nhds_within_Iic ⟨hxc, le_rfl⟩)).exists with ⟨y, ⟨hyab, hyf⟩, hy⟩, refine mem_of_superset(f.diff_mem_iff.2 ⟨hcf, hyf⟩) (subset.trans _ hxU), rw diff_subset_iff, exact subset.trans Icc_subset_Icc_union_Ioc (union_subset_union subset.rfl $ Ioc_subset_Ioc_left hy.1.le) }, cases hc.2.eq_or_lt with heq hlt, { rw ← heq, exact hcs.2 }, contrapose! hf, intros U hU, rcases (mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset hlt).1 (mem_nhds_within_of_mem_nhds hU) with ⟨y, hxy, hyU⟩, refine mem_of_superset _ hyU, clear_dependent U, have hy : y ∈ Icc a b, from ⟨hc.1.trans hxy.1.le, hxy.2⟩, by_cases hay : Icc a y ∈ f, { refine mem_of_superset (f.diff_mem_iff.2 ⟨f.diff_mem_iff.2 ⟨hay, hcs.2⟩, hpt y hy⟩) _, rw [diff_subset_iff, union_comm, Ico_union_right hxy.1.le, diff_subset_iff], exact Icc_subset_Icc_union_Icc }, { exact ((hsc.1 ⟨hy, hay⟩).not_lt hxy.1).elim }, end /-- An unordered closed interval in a conditionally complete linear order is compact. -/ lemma is_compact_interval {a b : Ξ±} : is_compact (interval a b) := is_compact_Icc lemma is_compact_pi_Icc {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, conditionally_complete_linear_order (Ξ± i)] [Ξ  i, topological_space (Ξ± i)] [Ξ  i, order_topology (Ξ± i)] (a b : Ξ  i, Ξ± i) : is_compact (Icc a b) := pi_univ_Icc a b β–Έ is_compact_univ_pi $ Ξ» i, is_compact_Icc instance compact_space_Icc (a b : Ξ±) : compact_space (Icc a b) := is_compact_iff_compact_space.mp is_compact_Icc instance compact_space_pi_Icc {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, conditionally_complete_linear_order (Ξ± i)] [Ξ  i, topological_space (Ξ± i)] [Ξ  i, order_topology (Ξ± i)] (a b : Ξ  i, Ξ± i) : compact_space (Icc a b) := is_compact_iff_compact_space.mp (is_compact_pi_Icc a b) @[priority 100] -- See note [lower instance priority] instance compact_space_of_complete_linear_order {Ξ± : Type*} [complete_linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±] : compact_space Ξ± := ⟨by simp only [← Icc_bot_top, is_compact_Icc]⟩ lemma is_compact.Inf_mem {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) : Inf s ∈ s := hs.is_closed.cInf_mem ne_s hs.bdd_below lemma is_compact.Sup_mem {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) : Sup s ∈ s := @is_compact.Inf_mem (order_dual Ξ±) _ _ _ _ hs ne_s lemma is_compact.is_glb_Inf {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) : is_glb s (Inf s) := is_glb_cInf ne_s hs.bdd_below lemma is_compact.is_lub_Sup {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) : is_lub s (Sup s) := @is_compact.is_glb_Inf (order_dual Ξ±) _ _ _ _ hs ne_s lemma is_compact.is_least_Inf {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) : is_least s (Inf s) := ⟨hs.Inf_mem ne_s, (hs.is_glb_Inf ne_s).1⟩ lemma is_compact.is_greatest_Sup {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) : is_greatest s (Sup s) := @is_compact.is_least_Inf (order_dual Ξ±) _ _ _ _ hs ne_s lemma is_compact.exists_is_least {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) : βˆƒ x, is_least s x := ⟨_, hs.is_least_Inf ne_s⟩ lemma is_compact.exists_is_greatest {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) : βˆƒ x, is_greatest s x := ⟨_, hs.is_greatest_Sup ne_s⟩ lemma is_compact.exists_is_glb {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) : βˆƒ x ∈ s, is_glb s x := ⟨_, hs.Inf_mem ne_s, hs.is_glb_Inf ne_s⟩ lemma is_compact.exists_is_lub {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) : βˆƒ x ∈ s, is_lub s x := ⟨_, hs.Sup_mem ne_s, hs.is_lub_Sup ne_s⟩ lemma is_compact.exists_Inf_image_eq {Ξ± : Type u} [topological_space Ξ±] {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) {f : Ξ± β†’ Ξ²} (hf : continuous_on f s) : βˆƒ x ∈ s, Inf (f '' s) = f x := let ⟨x, hxs, hx⟩ := (hs.image_of_continuous_on hf).Inf_mem (ne_s.image f) in ⟨x, hxs, hx.symm⟩ lemma is_compact.exists_Sup_image_eq {Ξ± : Type u} [topological_space Ξ±]: βˆ€ {s : set Ξ±}, is_compact s β†’ s.nonempty β†’ βˆ€ {f : Ξ± β†’ Ξ²}, continuous_on f s β†’ βˆƒ x ∈ s, Sup (f '' s) = f x := @is_compact.exists_Inf_image_eq (order_dual Ξ²) _ _ _ _ _ lemma eq_Icc_of_connected_compact {s : set Ξ±} (h₁ : is_connected s) (hβ‚‚ : is_compact s) : s = Icc (Inf s) (Sup s) := eq_Icc_cInf_cSup_of_connected_bdd_closed h₁ hβ‚‚.bdd_below hβ‚‚.bdd_above hβ‚‚.is_closed /-- The extreme value theorem: a continuous function realizes its minimum on a compact set -/ lemma is_compact.exists_forall_le {Ξ± : Type u} [topological_space Ξ±] {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) {f : Ξ± β†’ Ξ²} (hf : continuous_on f s) : βˆƒx∈s, βˆ€y∈s, f x ≀ f y := begin rcases hs.exists_Inf_image_eq ne_s hf with ⟨x, hxs, hx⟩, refine ⟨x, hxs, Ξ» y hy, _⟩, rw ← hx, exact ((hs.image_of_continuous_on hf).is_glb_Inf (ne_s.image f)).1 (mem_image_of_mem _ hy) end /-- The extreme value theorem: a continuous function realizes its maximum on a compact set -/ lemma is_compact.exists_forall_ge {Ξ± : Type u} [topological_space Ξ±]: βˆ€ {s : set Ξ±}, is_compact s β†’ s.nonempty β†’ βˆ€ {f : Ξ± β†’ Ξ²}, continuous_on f s β†’ βˆƒx∈s, βˆ€y∈s, f y ≀ f x := @is_compact.exists_forall_le (order_dual Ξ²) _ _ _ _ _ /-- The extreme value theorem: if a continuous function `f` tends to infinity away from compact sets, then it has a global minimum. -/ lemma continuous.exists_forall_le {Ξ± : Type*} [topological_space Ξ±] [nonempty Ξ±] {f : Ξ± β†’ Ξ²} (hf : continuous f) (hlim : tendsto f (cocompact Ξ±) at_top) : βˆƒ x, βˆ€ y, f x ≀ f y := begin inhabit Ξ±, obtain ⟨s : set Ξ±, hsc : is_compact s, hsf : βˆ€ x βˆ‰ s, f (default Ξ±) ≀ f x⟩ := (has_basis_cocompact.tendsto_iff at_top_basis).1 hlim (f $ default Ξ±) trivial, obtain ⟨x, -, hx⟩ := (hsc.insert (default Ξ±)).exists_forall_le (nonempty_insert _ _) hf.continuous_on, refine ⟨x, Ξ» y, _⟩, by_cases hy : y ∈ s, exacts [hx y (or.inr hy), (hx _ (or.inl rfl)).trans (hsf y hy)] end /-- The extreme value theorem: if a continuous function `f` tends to negative infinity away from compactx sets, then it has a global maximum. -/ lemma continuous.exists_forall_ge {Ξ± : Type*} [topological_space Ξ±] [nonempty Ξ±] {f : Ξ± β†’ Ξ²} (hf : continuous f) (hlim : tendsto f (cocompact Ξ±) at_bot) : βˆƒ x, βˆ€ y, f y ≀ f x := @continuous.exists_forall_le (order_dual Ξ²) _ _ _ _ _ _ _ hf hlim end conditionally_complete_linear_order end order_topology /-! ### Bounded monotone sequences converge In this section we prove a few theorems of the form β€œif the range of a monotone function `f : ΞΉ β†’ Ξ±` admits a least upper bound `a`, then `f x` tends to `a` as `x β†’ ∞`”, as well as version of this statement for (conditionally) complete lattices that use `⨆ x, f x` instead of `is_lub`. These theorems work for linear orders with order topologies as well as their products (both in terms of `prod` and in terms of function types). In order to reduce code duplication, we introduce two typeclasses (one for the property formulated above and one for the dual property), prove theorems assuming one of these typeclasses, and provide instances for linear orders and their products. -/ /-- We say that `Ξ±` is a `Sup_convergence_class` if the following holds. Let `f : ΞΉ β†’ Ξ±` be a monotone function, let `a : Ξ±` be a least upper bound of `set.range f`. Then `f x` tends to `𝓝 a` as `x β†’ ∞` (formally, at the filter `filter.at_top`). We require this for `ΞΉ = (s : set Ξ±)`, `f = coe` in the definition, then prove it for any `f` in `tendsto_at_top_is_lub`. This property holds for linear orders with order topology as well as their products. -/ class Sup_convergence_class (Ξ± : Type*) [preorder Ξ±] [topological_space Ξ±] : Prop := (tendsto_coe_at_top_is_lub : βˆ€ (a : Ξ±) (s : set Ξ±), is_lub s a β†’ tendsto (coe : s β†’ Ξ±) at_top (𝓝 a)) /-- We say that `Ξ±` is an `Inf_convergence_class` if the following holds. Let `f : ΞΉ β†’ Ξ±` be a monotone function, let `a : Ξ±` be a greatest lower bound of `set.range f`. Then `f x` tends to `𝓝 a` as `x β†’ -∞` (formally, at the filter `filter.at_bot`). We require this for `ΞΉ = (s : set Ξ±)`, `f = coe` in the definition, then prove it for any `f` in `tendsto_at_bot_is_glb`. This property holds for linear orders with order topology as well as their products. -/ class Inf_convergence_class (Ξ± : Type*) [preorder Ξ±] [topological_space Ξ±] : Prop := (tendsto_coe_at_bot_is_glb : βˆ€ (a : Ξ±) (s : set Ξ±), is_glb s a β†’ tendsto (coe : s β†’ Ξ±) at_bot (𝓝 a)) instance order_dual.Sup_convergence_class [preorder Ξ±] [topological_space Ξ±] [Inf_convergence_class Ξ±] : Sup_convergence_class (order_dual Ξ±) := βŸ¨β€ΉInf_convergence_class Ξ±β€Ί.1⟩ instance order_dual.Inf_convergence_class [preorder Ξ±] [topological_space Ξ±] [Sup_convergence_class Ξ±] : Inf_convergence_class (order_dual Ξ±) := βŸ¨β€ΉSup_convergence_class Ξ±β€Ί.1⟩ @[priority 100] -- see Note [lower instance priority] instance linear_order.Sup_convergence_class [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] : Sup_convergence_class Ξ± := begin refine ⟨λ a s ha, tendsto_order.2 ⟨λ b hb, _, Ξ» b hb, _⟩⟩, { rcases ha.exists_between hb with ⟨c, hcs, bc, bca⟩, lift c to s using hcs, refine (eventually_ge_at_top c).mono (Ξ» x hx, bc.trans_le hx) }, { exact eventually_of_forall (Ξ» x, (ha.1 x.2).trans_lt hb) } end @[priority 100] -- see Note [lower instance priority] instance linear_order.Inf_convergence_class [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] : Inf_convergence_class Ξ± := show Inf_convergence_class (order_dual $ order_dual Ξ±), from order_dual.Inf_convergence_class section variables {ΞΉ : Type*} [preorder ΞΉ] [topological_space Ξ±] section is_lub variables [preorder Ξ±] [Sup_convergence_class Ξ±] {f : ΞΉ β†’ Ξ±} {a : Ξ±} lemma tendsto_at_top_is_lub (h_mono : monotone f) (ha : is_lub (set.range f) a) : tendsto f at_top (𝓝 a) := begin suffices : tendsto (range_factorization f) at_top at_top, from (Sup_convergence_class.tendsto_coe_at_top_is_lub _ _ ha).comp this, exact h_mono.range_factorization.tendsto_at_top_at_top (Ξ» b, b.2.imp $ Ξ» a ha, ha.ge) end lemma tendsto_at_bot_is_lub (h_mono : monotone (order_dual.to_dual ∘ f)) (ha : is_lub (set.range f) a) : tendsto f at_bot (𝓝 a) := @tendsto_at_top_is_lub Ξ± (order_dual ΞΉ) _ _ _ _ f a h_mono.order_dual ha end is_lub section is_glb variables [preorder Ξ±] [Inf_convergence_class Ξ±] {f : ΞΉ β†’ Ξ±} {a : Ξ±} lemma tendsto_at_bot_is_glb (h_mono : monotone f) (ha : is_glb (set.range f) a) : tendsto f at_bot (𝓝 a) := @tendsto_at_top_is_lub (order_dual Ξ±) (order_dual ΞΉ) _ _ _ _ f a h_mono.order_dual ha lemma tendsto_at_top_is_glb (h_mono : monotone (order_dual.to_dual ∘ f)) (ha : is_glb (set.range f) a) : tendsto f at_top (𝓝 a) := @tendsto_at_top_is_lub (order_dual Ξ±) ΞΉ _ _ _ _ f a h_mono ha end is_glb section csupr variables [conditionally_complete_lattice Ξ±] [Sup_convergence_class Ξ±] {f : ΞΉ β†’ Ξ±} {a : Ξ±} lemma tendsto_at_top_csupr (h_mono : monotone f) (hbdd : bdd_above $ range f) : tendsto f at_top (𝓝 (⨆i, f i)) := begin casesI is_empty_or_nonempty ΞΉ, exacts [tendsto_of_is_empty, tendsto_at_top_is_lub h_mono (is_lub_csupr hbdd)] end lemma tendsto_at_bot_csupr (h_mono : monotone (order_dual.to_dual ∘ f)) (hbdd : bdd_above $ range f) : tendsto f at_bot (𝓝 (⨆i, f i)) := @tendsto_at_top_csupr Ξ± (order_dual ΞΉ) _ _ _ _ _ h_mono.order_dual hbdd end csupr section cinfi variables [conditionally_complete_lattice Ξ±] [Inf_convergence_class Ξ±] {f : ΞΉ β†’ Ξ±} {a : Ξ±} lemma tendsto_at_bot_cinfi (h_mono : monotone f) (hbdd : bdd_below $ range f) : tendsto f at_bot (𝓝 (β¨…i, f i)) := @tendsto_at_top_csupr (order_dual Ξ±) (order_dual ΞΉ) _ _ _ _ _ h_mono.order_dual hbdd lemma tendsto_at_top_cinfi (h_mono : monotone (order_dual.to_dual ∘ f)) (hbdd : bdd_below $ range f) : tendsto f at_top (𝓝 (β¨…i, f i)) := @tendsto_at_top_csupr (order_dual Ξ±) ΞΉ _ _ _ _ _ h_mono hbdd end cinfi section supr variables [complete_lattice Ξ±] [Sup_convergence_class Ξ±] {f : ΞΉ β†’ Ξ±} {a : Ξ±} lemma tendsto_at_top_supr (h_mono : monotone f) : tendsto f at_top (𝓝 (⨆i, f i)) := tendsto_at_top_csupr h_mono (order_top.bdd_above _) lemma tendsto_at_bot_supr (h_mono : monotone (order_dual.to_dual ∘ f)) : tendsto f at_bot (𝓝 (⨆i, f i)) := tendsto_at_bot_csupr h_mono (order_top.bdd_above _) end supr section infi variables [complete_lattice Ξ±] [Inf_convergence_class Ξ±] {f : ΞΉ β†’ Ξ±} {a : Ξ±} lemma tendsto_at_bot_infi (h_mono : monotone f) : tendsto f at_bot (𝓝 (β¨…i, f i)) := tendsto_at_bot_cinfi h_mono (order_bot.bdd_below _) lemma tendsto_at_top_infi (h_mono : monotone (order_dual.to_dual ∘ f)) : tendsto f at_top (𝓝 (β¨…i, f i)) := tendsto_at_top_cinfi h_mono (order_bot.bdd_below _) end infi end instance [preorder Ξ±] [preorder Ξ²] [topological_space Ξ±] [topological_space Ξ²] [Sup_convergence_class Ξ±] [Sup_convergence_class Ξ²] : Sup_convergence_class (Ξ± Γ— Ξ²) := begin constructor, rintro ⟨a, b⟩ s h, rw [is_lub_prod, ← range_restrict, ← range_restrict] at h, have A : tendsto (Ξ» x : s, (x : Ξ± Γ— Ξ²).1) at_top (𝓝 a), from tendsto_at_top_is_lub (monotone_fst.restrict s) h.1, have B : tendsto (Ξ» x : s, (x : Ξ± Γ— Ξ²).2) at_top (𝓝 b), from tendsto_at_top_is_lub (monotone_snd.restrict s) h.2, convert A.prod_mk_nhds B, ext1 ⟨⟨x, y⟩, h⟩, refl end instance [preorder Ξ±] [preorder Ξ²] [topological_space Ξ±] [topological_space Ξ²] [Inf_convergence_class Ξ±] [Inf_convergence_class Ξ²] : Inf_convergence_class (Ξ± Γ— Ξ²) := show Inf_convergence_class (order_dual $ (order_dual Ξ± Γ— order_dual Ξ²)), from order_dual.Inf_convergence_class instance {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, preorder (Ξ± i)] [Ξ  i, topological_space (Ξ± i)] [Ξ  i, Sup_convergence_class (Ξ± i)] : Sup_convergence_class (Ξ  i, Ξ± i) := begin refine ⟨λ f s h, _⟩, simp only [is_lub_pi, ← range_restrict] at h, exact tendsto_pi.2 (Ξ» i, tendsto_at_top_is_lub ((monotone_eval _).restrict _) (h i)) end instance {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} [Ξ  i, preorder (Ξ± i)] [Ξ  i, topological_space (Ξ± i)] [Ξ  i, Inf_convergence_class (Ξ± i)] : Inf_convergence_class (Ξ  i, Ξ± i) := show Inf_convergence_class (order_dual $ Ξ  i, order_dual (Ξ± i)), from order_dual.Inf_convergence_class instance pi.Sup_convergence_class' {ΞΉ : Type*} [preorder Ξ±] [topological_space Ξ±] [Sup_convergence_class Ξ±] : Sup_convergence_class (ΞΉ β†’ Ξ±) := pi.Sup_convergence_class instance pi.Inf_convergence_class' {ΞΉ : Type*} [preorder Ξ±] [topological_space Ξ±] [Inf_convergence_class Ξ±] : Inf_convergence_class (ΞΉ β†’ Ξ±) := pi.Inf_convergence_class lemma tendsto_of_monotone {ΞΉ Ξ± : Type*} [preorder ΞΉ] [topological_space Ξ±] [conditionally_complete_linear_order Ξ±] [order_topology Ξ±] {f : ΞΉ β†’ Ξ±} (h_mono : monotone f) : tendsto f at_top at_top ∨ (βˆƒ l, tendsto f at_top (𝓝 l)) := if H : bdd_above (range f) then or.inr ⟨_, tendsto_at_top_csupr h_mono H⟩ else or.inl $ tendsto_at_top_at_top_of_monotone' h_mono H lemma tendsto_iff_tendsto_subseq_of_monotone {ι₁ ΞΉβ‚‚ Ξ± : Type*} [semilattice_sup ι₁] [preorder ΞΉβ‚‚] [nonempty ι₁] [topological_space Ξ±] [conditionally_complete_linear_order Ξ±] [order_topology Ξ±] [no_top_order Ξ±] {f : ΞΉβ‚‚ β†’ Ξ±} {Ο† : ι₁ β†’ ΞΉβ‚‚} {l : Ξ±} (hf : monotone f) (hg : tendsto Ο† at_top at_top) : tendsto f at_top (𝓝 l) ↔ tendsto (f ∘ Ο†) at_top (𝓝 l) := begin split; intro h, { exact h.comp hg }, { rcases tendsto_of_monotone hf with h' | ⟨l', hl'⟩, { exact (not_tendsto_at_top_of_tendsto_nhds h (h'.comp hg)).elim }, { rwa tendsto_nhds_unique h (hl'.comp hg) } } end /-! The next family of results, such as `is_lub_of_tendsto` and `supr_eq_of_tendsto`, are converses to the standard fact that bounded monotone functions converge. They state, that if a monotone function `f` tends to `a` along `at_top`, then that value `a` is a least upper bound for the range of `f`. Related theorems above (`is_lub.is_lub_of_tendsto`, `is_glb.is_glb_of_tendsto` etc) cover the case when `f x` tends to `a` as `x` tends to some point `b` in the domain. -/ lemma monotone.ge_of_tendsto {Ξ± Ξ² : Type*} [topological_space Ξ±] [preorder Ξ±] [order_closed_topology Ξ±] [semilattice_sup Ξ²] {f : Ξ² β†’ Ξ±} {a : Ξ±} (hf : monotone f) (ha : tendsto f at_top (𝓝 a)) (b : Ξ²) : f b ≀ a := begin haveI : nonempty Ξ² := nonempty.intro b, exact ge_of_tendsto ha ((eventually_ge_at_top b).mono (Ξ» _ hxy, hf hxy)) end lemma monotone.le_of_tendsto {Ξ± Ξ² : Type*} [topological_space Ξ±] [preorder Ξ±] [order_closed_topology Ξ±] [semilattice_inf Ξ²] {f : Ξ² β†’ Ξ±} {a : Ξ±} (hf : monotone f) (ha : tendsto f at_bot (𝓝 a)) (b : Ξ²) : a ≀ f b := @monotone.ge_of_tendsto (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ f _ hf.order_dual ha b lemma is_lub_of_tendsto {Ξ± Ξ² : Type*} [topological_space Ξ±] [preorder Ξ±] [order_closed_topology Ξ±] [nonempty Ξ²] [semilattice_sup Ξ²] {f : Ξ² β†’ Ξ±} {a : Ξ±} (hf : monotone f) (ha : tendsto f at_top (𝓝 a)) : is_lub (set.range f) a := begin split, { rintros _ ⟨b, rfl⟩, exact hf.ge_of_tendsto ha b }, { exact Ξ» _ hb, le_of_tendsto' ha (Ξ» x, hb (set.mem_range_self x)) } end lemma is_glb_of_tendsto {Ξ± Ξ² : Type*} [topological_space Ξ±] [preorder Ξ±] [order_closed_topology Ξ±] [nonempty Ξ²] [semilattice_inf Ξ²] {f : Ξ² β†’ Ξ±} {a : Ξ±} (hf : monotone f) (ha : tendsto f at_bot (𝓝 a)) : is_glb (set.range f) a := @is_lub_of_tendsto (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ _ hf.order_dual ha lemma supr_eq_of_tendsto {Ξ± Ξ²} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] [nonempty Ξ²] [semilattice_sup Ξ²] {f : Ξ² β†’ Ξ±} {a : Ξ±} (hf : monotone f) : tendsto f at_top (𝓝 a) β†’ supr f = a := tendsto_nhds_unique (tendsto_at_top_supr hf) lemma infi_eq_of_tendsto {Ξ±} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±] [nonempty Ξ²] [semilattice_sup Ξ²] {f : Ξ² β†’ Ξ±} {a : Ξ±} (hf : βˆ€n m, n ≀ m β†’ f m ≀ f n) : tendsto f at_top (𝓝 a) β†’ infi f = a := tendsto_nhds_unique (tendsto_at_top_infi hf) lemma supr_eq_supr_subseq_of_monotone {ι₁ ΞΉβ‚‚ Ξ± : Type*} [preorder ΞΉβ‚‚] [complete_lattice Ξ±] {l : filter ι₁} [l.ne_bot] {f : ΞΉβ‚‚ β†’ Ξ±} {Ο† : ι₁ β†’ ΞΉβ‚‚} (hf : monotone f) (hΟ† : tendsto Ο† l at_top) : (⨆ i, f i) = (⨆ i, f (Ο† i)) := le_antisymm (supr_le_supr2 $ Ξ» i, exists_imp_exists (Ξ» j (hj : i ≀ Ο† j), hf hj) (hΟ†.eventually $ eventually_ge_at_top i).exists) (supr_le_supr2 $ Ξ» i, βŸ¨Ο† i, le_refl _⟩) lemma infi_eq_infi_subseq_of_monotone {ι₁ ΞΉβ‚‚ Ξ± : Type*} [preorder ΞΉβ‚‚] [complete_lattice Ξ±] {l : filter ι₁} [l.ne_bot] {f : ΞΉβ‚‚ β†’ Ξ±} {Ο† : ι₁ β†’ ΞΉβ‚‚} (hf : monotone f) (hΟ† : tendsto Ο† l at_bot) : (β¨… i, f i) = (β¨… i, f (Ο† i)) := supr_eq_supr_subseq_of_monotone hf.order_dual hΟ† @[to_additive] lemma tendsto_inv_nhds_within_Ioi [ordered_comm_group Ξ±] [topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} : tendsto has_inv.inv (𝓝[Ioi a] a) (𝓝[Iio (a⁻¹)] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Iio [ordered_comm_group Ξ±] [topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} : tendsto has_inv.inv (𝓝[Iio a] a) (𝓝[Ioi (a⁻¹)] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Ioi_inv [ordered_comm_group Ξ±] [topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} : tendsto has_inv.inv (𝓝[Ioi (a⁻¹)] (a⁻¹)) (𝓝[Iio a] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Ioi _ _ _ _ (a⁻¹) @[to_additive] lemma tendsto_inv_nhds_within_Iio_inv [ordered_comm_group Ξ±] [topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} : tendsto has_inv.inv (𝓝[Iio (a⁻¹)] (a⁻¹)) (𝓝[Ioi a] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Iio _ _ _ _ (a⁻¹) @[to_additive] lemma tendsto_inv_nhds_within_Ici [ordered_comm_group Ξ±] [topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} : tendsto has_inv.inv (𝓝[Ici a] a) (𝓝[Iic (a⁻¹)] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Iic [ordered_comm_group Ξ±] [topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} : tendsto has_inv.inv (𝓝[Iic a] a) (𝓝[Ici (a⁻¹)] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Ici_inv [ordered_comm_group Ξ±] [topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} : tendsto has_inv.inv (𝓝[Ici (a⁻¹)] (a⁻¹)) (𝓝[Iic a] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Ici _ _ _ _ (a⁻¹) @[to_additive] lemma tendsto_inv_nhds_within_Iic_inv [ordered_comm_group Ξ±] [topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} : tendsto has_inv.inv (𝓝[Iic (a⁻¹)] (a⁻¹)) (𝓝[Ici a] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Iic _ _ _ _ (a⁻¹) lemma nhds_left_sup_nhds_right (a : Ξ±) [topological_space Ξ±] [linear_order Ξ±] : 𝓝[Iic a] a βŠ” 𝓝[Ici a] a = 𝓝 a := by rw [← nhds_within_union, Iic_union_Ici, nhds_within_univ] lemma nhds_left'_sup_nhds_right (a : Ξ±) [topological_space Ξ±] [linear_order Ξ±] : 𝓝[Iio a] a βŠ” 𝓝[Ici a] a = 𝓝 a := by rw [← nhds_within_union, Iio_union_Ici, nhds_within_univ] lemma nhds_left_sup_nhds_right' (a : Ξ±) [topological_space Ξ±] [linear_order Ξ±] : 𝓝[Iic a] a βŠ” 𝓝[Ioi a] a = 𝓝 a := by rw [← nhds_within_union, Iic_union_Ioi, nhds_within_univ] lemma continuous_at_iff_continuous_left_right [topological_space Ξ±] [linear_order Ξ±] [topological_space Ξ²] {a : Ξ±} {f : Ξ± β†’ Ξ²} : continuous_at f a ↔ continuous_within_at f (Iic a) a ∧ continuous_within_at f (Ici a) a := by simp only [continuous_within_at, continuous_at, ← tendsto_sup, nhds_left_sup_nhds_right] lemma continuous_within_at_Ioi_iff_Ici {Ξ± Ξ² : Type*} [topological_space Ξ±] [partial_order Ξ±] [topological_space Ξ²] {a : Ξ±} {f : Ξ± β†’ Ξ²} : continuous_within_at f (Ioi a) a ↔ continuous_within_at f (Ici a) a := by simp only [← Ici_diff_left, continuous_within_at_diff_self] lemma continuous_within_at_Iio_iff_Iic {Ξ± Ξ² : Type*} [topological_space Ξ±] [linear_order Ξ±] [topological_space Ξ²] {a : Ξ±} {f : Ξ± β†’ Ξ²} : continuous_within_at f (Iio a) a ↔ continuous_within_at f (Iic a) a := begin have := @continuous_within_at_Ioi_iff_Ici (order_dual Ξ±) _ _ _ _ _ f, erw [dual_Ici, dual_Ioi] at this, exact this, end lemma continuous_at_iff_continuous_left'_right' [topological_space Ξ±] [linear_order Ξ±] [topological_space Ξ²] {a : Ξ±} {f : Ξ± β†’ Ξ²} : continuous_at f a ↔ continuous_within_at f (Iio a) a ∧ continuous_within_at f (Ioi a) a := by rw [continuous_within_at_Ioi_iff_Ici, continuous_within_at_Iio_iff_Iic, continuous_at_iff_continuous_left_right] /-! ### Continuity of monotone functions In this section we prove the following fact: if `f` is a monotone function on a neighborhood of `a` and the image of this neighborhood is a neighborhood of `f a`, then `f` is continuous at `a`, see `continuous_at_of_mono_incr_on_of_image_mem_nhds`, as well as several similar facts. -/ section linear_order variables [linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±] variables [linear_order Ξ²] [topological_space Ξ²] [order_topology Ξ²] /-- If `f` is a function strictly monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : βˆ€ b > f a, βˆƒ c ∈ s, f c ∈ Ioc (f a) b` is required because otherwise the function `f : ℝ β†’ ℝ` given by `f x = if x ≀ 0 then x else x + 1` would be a counter-example at `a = 0`. -/ lemma strict_mono_incr_on.continuous_at_right_of_exists_between {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : βˆ€ b > f a, βˆƒ c ∈ s, f c ∈ Ioc (f a) b) : continuous_within_at f (Ici a) a := begin have ha : a ∈ Ici a := left_mem_Ici, have has : a ∈ s := mem_of_mem_nhds_within ha hs, refine tendsto_order.2 ⟨λ b hb, _, Ξ» b hb, _⟩, { filter_upwards [hs, self_mem_nhds_within], intros x hxs hxa, exact hb.trans_le ((h_mono.le_iff_le has hxs).2 hxa) }, { rcases hfs b hb with ⟨c, hcs, hac, hcb⟩, rw [h_mono.lt_iff_lt has hcs] at hac, filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 hac)], rintros x hx ⟨hax, hxc⟩, exact ((h_mono.lt_iff_lt hx hcs).2 hxc).trans_le hcb } end /-- If `f` is a function monotonically increasing function on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : βˆ€ b > f a, βˆƒ c ∈ s, f c ∈ Ioo (f a) b` cannot be replaced by the weaker assumption `hfs : βˆ€ b > f a, βˆƒ c ∈ s, f c ∈ Ioc (f a) b` we use for strictly monotone functions because otherwise the function `ceil : ℝ β†’ β„€` would be a counter-example at `a = 0`. -/ lemma continuous_at_right_of_mono_incr_on_of_exists_between {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : βˆ€ (x ∈ s) (y ∈ s), x ≀ y β†’ f x ≀ f y) (hs : s ∈ 𝓝[Ici a] a) (hfs : βˆ€ b > f a, βˆƒ c ∈ s, f c ∈ Ioo (f a) b) : continuous_within_at f (Ici a) a := begin have ha : a ∈ Ici a := left_mem_Ici, have has : a ∈ s := mem_of_mem_nhds_within ha hs, refine tendsto_order.2 ⟨λ b hb, _, Ξ» b hb, _⟩, { filter_upwards [hs, self_mem_nhds_within], intros x hxs hxa, exact hb.trans_le (h_mono _ has _ hxs hxa) }, { rcases hfs b hb with ⟨c, hcs, hac, hcb⟩, have : a < c, from not_le.1 (Ξ» h, hac.not_le $ h_mono _ hcs _ has h), filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 this)], rintros x hx ⟨hax, hxc⟩, exact (h_mono _ hx _ hcs hxc.le).trans_lt hcb } end /-- If a function `f` with a densely ordered codomain is monotonically increasing on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : βˆ€ (x ∈ s) (y ∈ s), x ≀ y β†’ f x ≀ f y) (hs : s ∈ 𝓝[Ici a] a) (hfs : closure (f '' s) ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := begin refine continuous_at_right_of_mono_incr_on_of_exists_between h_mono hs (Ξ» b hb, _), rcases (mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset hb).1 hfs with ⟨b', ⟨hab', hbb'⟩, hb'⟩, rcases exists_between hab' with ⟨c', hc'⟩, rcases mem_closure_iff.1 (hb' ⟨hc'.1.le, hc'.2⟩) (Ioo (f a) b') is_open_Ioo hc' with ⟨_, hc, ⟨c, hcs, rfl⟩⟩, exact ⟨c, hcs, hc.1, hc.2.trans_le hbb'⟩ end /-- If a function `f` with a densely ordered codomain is monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma continuous_at_right_of_mono_incr_on_of_image_mem_nhds_within [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : βˆ€ (x ∈ s) (y ∈ s), x ≀ y β†’ f x ≀ f y) (hs : s ∈ 𝓝[Ici a] a) (hfs : f '' s ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono hs $ mem_of_superset hfs subset_closure /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_incr_on.continuous_at_right_of_closure_image_mem_nhds_within [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : closure (f '' s) ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within (Ξ» x hx y hy, (h_mono.le_iff_le hx hy).2) hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_incr_on.continuous_at_right_of_image_mem_nhds_within [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : f '' s ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := h_mono.continuous_at_right_of_closure_image_mem_nhds_within hs (mem_of_superset hfs subset_closure) /-- If a function `f` is strictly monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` includes `Ioi (f a)`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_incr_on.continuous_at_right_of_surj_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : surj_on f s (Ioi (f a))) : continuous_within_at f (Ici a) a := h_mono.continuous_at_right_of_exists_between hs $ Ξ» b hb, let ⟨c, hcs, hcb⟩ := hfs hb in ⟨c, hcs, hcb.symm β–Έ hb, hcb.le⟩ /-- If `f` is a function strictly monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : βˆ€ b < f a, βˆƒ c ∈ s, f c ∈ Ico b (f a)` is required because otherwise the function `f : ℝ β†’ ℝ` given by `f x = if x < 0 then x else x + 1` would be a counter-example at `a = 0`. -/ lemma strict_mono_incr_on.continuous_at_left_of_exists_between {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : βˆ€ b < f a, βˆƒ c ∈ s, f c ∈ Ico b (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_exists_between hs $ Ξ» b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩ /-- If `f` is a function monotonically increasing function on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : βˆ€ b < f a, βˆƒ c ∈ s, f c ∈ Ioo b (f a)` cannot be replaced by the weaker assumption `hfs : βˆ€ b < f a, βˆƒ c ∈ s, f c ∈ Ico b (f a)` we use for strictly monotone functions because otherwise the function `floor : ℝ β†’ β„€` would be a counter-example at `a = 0`. -/ lemma continuous_at_left_of_mono_incr_on_of_exists_between {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : βˆ€ (x ∈ s) (y ∈ s), x ≀ y β†’ f x ≀ f y) (hs : s ∈ 𝓝[Iic a] a) (hfs : βˆ€ b < f a, βˆƒ c ∈ s, f c ∈ Ioo b (f a)) : continuous_within_at f (Iic a) a := @continuous_at_right_of_mono_incr_on_of_exists_between (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ f s a (Ξ» x hx y hy, h_mono y hy x hx) hs $ Ξ» b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩ /-- If a function `f` with a densely ordered codomain is monotonically increasing on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left -/ lemma continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : βˆ€ (x ∈ s) (y ∈ s), x ≀ y β†’ f x ≀ f y) (hs : s ∈ 𝓝[Iic a] a) (hfs : closure (f '' s) ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := @continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ _ f s a (Ξ» x hx y hy, h_mono y hy x hx) hs hfs /-- If a function `f` with a densely ordered codomain is monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma continuous_at_left_of_mono_incr_on_of_image_mem_nhds_within [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : βˆ€ (x ∈ s) (y ∈ s), x ≀ y β†’ f x ≀ f y) (hs : s ∈ 𝓝[Iic a] a) (hfs : f '' s ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono hs (mem_of_superset hfs subset_closure) /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_incr_on.continuous_at_left_of_closure_image_mem_nhds_within [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : closure (f '' s) ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_closure_image_mem_nhds_within hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_incr_on.continuous_at_left_of_image_mem_nhds_within [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : f '' s ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_image_mem_nhds_within hs hfs /-- If a function `f` is strictly monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` includes `Iio (f a)`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_incr_on.continuous_at_left_of_surj_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : surj_on f s (Iio (f a))) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_surj_on hs hfs /-- If a function `f` is strictly monotonically increasing on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, and every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a`. -/ lemma strict_mono_incr_on.continuous_at_of_exists_between {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝 a) (hfs_l : βˆ€ b < f a, βˆƒ c ∈ s, f c ∈ Ico b (f a)) (hfs_r : βˆ€ b > f a, βˆƒ c ∈ s, f c ∈ Ioc (f a) b) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨h_mono.continuous_at_left_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_l, h_mono.continuous_at_right_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_r⟩ /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma strict_mono_incr_on.continuous_at_of_closure_image_mem_nhds [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝 a) (hfs : closure (f '' s) ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨h_mono.continuous_at_left_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs), h_mono.continuous_at_right_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)⟩ /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a neighborhood of `a` and the image of this set under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma strict_mono_incr_on.continuous_at_of_image_mem_nhds [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s ∈ 𝓝 a) (hfs : f '' s ∈ 𝓝 (f a)) : continuous_at f a := h_mono.continuous_at_of_closure_image_mem_nhds hs (mem_of_superset hfs subset_closure) /-- If `f` is a function monotonically increasing function on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, and every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_mono_incr_on_of_exists_between {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : βˆ€ (x ∈ s) (y ∈ s), x ≀ y β†’ f x ≀ f y) (hs : s ∈ 𝓝 a) (hfs_l : βˆ€ b < f a, βˆƒ c ∈ s, f c ∈ Ioo b (f a)) (hfs_r : βˆ€ b > f a, βˆƒ c ∈ s, f c ∈ Ioo (f a) b) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨continuous_at_left_of_mono_incr_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_l, continuous_at_right_of_mono_incr_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_r⟩ /-- If a function `f` with a densely ordered codomain is monotonically increasing on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_mono_incr_on_of_closure_image_mem_nhds [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : βˆ€ (x ∈ s) (y ∈ s), x ≀ y β†’ f x ≀ f y) (hs : s ∈ 𝓝 a) (hfs : closure (f '' s) ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs), continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)⟩ /-- If a function `f` with a densely ordered codomain is monotonically increasing on a neighborhood of `a` and the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_mono_incr_on_of_image_mem_nhds [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : βˆ€ (x ∈ s) (y ∈ s), x ≀ y β†’ f x ≀ f y) (hs : s ∈ 𝓝 a) (hfs : f '' s ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_of_mono_incr_on_of_closure_image_mem_nhds h_mono hs (mem_of_superset hfs subset_closure) /-- A monotone function with densely ordered codomain and a dense range is continuous. -/ lemma monotone.continuous_of_dense_range [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} (h_mono : monotone f) (h_dense : dense_range f) : continuous f := continuous_iff_continuous_at.mpr $ Ξ» a, continuous_at_of_mono_incr_on_of_closure_image_mem_nhds (Ξ» x hx y hy hxy, h_mono hxy) univ_mem $ by simp only [image_univ, h_dense.closure_eq, univ_mem] /-- A monotone surjective function with a densely ordered codomain is surjective. -/ lemma monotone.continuous_of_surjective [densely_ordered Ξ²] {f : Ξ± β†’ Ξ²} (h_mono : monotone f) (h_surj : function.surjective f) : continuous f := h_mono.continuous_of_dense_range h_surj.dense_range end linear_order /-! ### Continuity of order isomorphisms In this section we prove that an `order_iso` is continuous, hence it is a `homeomorph`. We prove this for an `order_iso` between to partial orders with order topology. -/ namespace order_iso variables [partial_order Ξ±] [partial_order Ξ²] [topological_space Ξ±] [topological_space Ξ²] [order_topology Ξ±] [order_topology Ξ²] protected lemma continuous (e : Ξ± ≃o Ξ²) : continuous e := begin rw [β€Ήorder_topology Ξ²β€Ί.topology_eq_generate_intervals], refine continuous_generated_from (Ξ» s hs, _), rcases hs with ⟨a, rfl|rfl⟩, { rw e.preimage_Ioi, apply is_open_lt' }, { rw e.preimage_Iio, apply is_open_gt' } end /-- An order isomorphism between two linear order `order_topology` spaces is a homeomorphism. -/ def to_homeomorph (e : Ξ± ≃o Ξ²) : Ξ± β‰ƒβ‚œ Ξ² := { continuous_to_fun := e.continuous, continuous_inv_fun := e.symm.continuous, .. e } @[simp] lemma coe_to_homeomorph (e : Ξ± ≃o Ξ²) : ⇑e.to_homeomorph = e := rfl @[simp] lemma coe_to_homeomorph_symm (e : Ξ± ≃o Ξ²) : ⇑e.to_homeomorph.symm = e.symm := rfl end order_iso
808a35663e1143afea67d3477a0ea77e6cb0872c
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/eq6.lean
fbd73ecaef3ba52cc8d1e24ca719cb185731c90e
[ "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
421
lean
import data.list open list definition append {A : Type} : list A β†’ list A β†’ list A | append nil l := l | append (h :: t) l := h :: (append t l) theorem append_nil {A : Type} (l : list A) : append nil l = l := rfl theorem append_cons {A : Type} (h : A) (t l : list A) : append (h :: t) l = h :: (append t l) := rfl example : append (1 :: 2 :: nil) (3 :: 4 :: 5 :: nil) = (1 :: 2 :: 3 :: 4 :: 5 :: nil) := rfl
eaf887e60049c7980c152e8f20e4f94b3f107e21
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/pfunctor/multivariate/W.lean
3923df2b0c67f6d4852bda6b8f56e8206b6298a1
[ "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,502
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Simon Hudon -/ import data.pfunctor.multivariate.basic /-! # The W construction as a multivariate polynomial functor. W types are well-founded tree-like structures. They are defined as the least fixpoint of a polynomial functor. ## Main definitions * `W_mk` - constructor * `W_dest - destructor * `W_rec` - recursor: basis for defining functions by structural recursion on `P.W Ξ±` * `W_rec_eq` - defining equation for `W_rec` * `W_ind` - induction principle for `P.W Ξ±` ## Implementation notes Three views of M-types: * `Wp`: polynomial functor * `W`: data type inductively defined by a triple: shape of the root, data in the root and children of the root * `W`: least fixed point of a polynomial functor Specifically, we define the polynomial functor `Wp` as: * A := a tree-like structure without information in the nodes * B := given the tree-like structure `t`, `B t` is a valid path (specified inductively by `W_path`) from the root of `t` to any given node. As a result `Wp.obj Ξ±` is made of a dataless tree and a function from its valid paths to values of `Ξ±` ## Reference * Jeremy Avigad, Mario M. Carneiro and Simon Hudon. [*Data Types as Quotients of Polynomial Functors*][avigad-carneiro-hudon2019] -/ universes u v namespace mvpfunctor open typevec open_locale mvfunctor variables {n : β„•} (P : mvpfunctor.{u} (n+1)) /-- A path from the root of a tree to one of its node -/ inductive W_path : P.last.W β†’ fin2 n β†’ Type u | root (a : P.A) (f : P.last.B a β†’ P.last.W) (i : fin2 n) (c : P.drop.B a i) : W_path ⟨a, f⟩ i | child (a : P.A) (f : P.last.B a β†’ P.last.W) (i : fin2 n) (j : P.last.B a) (c : W_path (f j) i) : W_path ⟨a, f⟩ i instance W_path.inhabited (x : P.last.W) {i} [I : inhabited (P.drop.B x.head i)] : inhabited (W_path P x i) := ⟨ match x, I with | ⟨a, f ⟩, I := W_path.root a f i (@default _ I) end ⟩ /-- Specialized destructor on `W_path` -/ def W_path_cases_on {Ξ± : typevec n} {a : P.A} {f : P.last.B a β†’ P.last.W} (g' : P.drop.B a ⟹ Ξ±) (g : Ξ  j : P.last.B a, P.W_path (f j) ⟹ Ξ±) : P.W_path ⟨a, f⟩ ⟹ Ξ± := begin intros i x, cases x, case W_path.root : _ _ i c { exact g' i c }, case W_path.child : _ _ i j c { exact g j i c} end /-- Specialized destructor on `W_path` -/ def W_path_dest_left {Ξ± : typevec n} {a : P.A} {f : P.last.B a β†’ P.last.W} (h : P.W_path ⟨a, f⟩ ⟹ Ξ±) : P.drop.B a ⟹ Ξ± := Ξ» i c, h i (W_path.root a f i c) /-- Specialized destructor on `W_path` -/ def W_path_dest_right {Ξ± : typevec n} {a : P.A} {f : P.last.B a β†’ P.last.W} (h : P.W_path ⟨a, f⟩ ⟹ Ξ±) : Ξ  j : P.last.B a, P.W_path (f j) ⟹ Ξ± := Ξ» j i c, h i (W_path.child a f i j c) theorem W_path_dest_left_W_path_cases_on {Ξ± : typevec n} {a : P.A} {f : P.last.B a β†’ P.last.W} (g' : P.drop.B a ⟹ Ξ±) (g : Ξ  j : P.last.B a, P.W_path (f j) ⟹ Ξ±) : P.W_path_dest_left (P.W_path_cases_on g' g) = g' := rfl theorem W_path_dest_right_W_path_cases_on {Ξ± : typevec n} {a : P.A} {f : P.last.B a β†’ P.last.W} (g' : P.drop.B a ⟹ Ξ±) (g : Ξ  j : P.last.B a, P.W_path (f j) ⟹ Ξ±) : P.W_path_dest_right (P.W_path_cases_on g' g) = g := rfl theorem W_path_cases_on_eta {Ξ± : typevec n} {a : P.A} {f : P.last.B a β†’ P.last.W} (h : P.W_path ⟨a, f⟩ ⟹ Ξ±) : P.W_path_cases_on (P.W_path_dest_left h) (P.W_path_dest_right h) = h := by ext i x; cases x; reflexivity theorem comp_W_path_cases_on {Ξ± Ξ² : typevec n} (h : Ξ± ⟹ Ξ²) {a : P.A} {f : P.last.B a β†’ P.last.W} (g' : P.drop.B a ⟹ Ξ±) (g : Ξ  j : P.last.B a, P.W_path (f j) ⟹ Ξ±) : h ⊚ P.W_path_cases_on g' g = P.W_path_cases_on (h ⊚ g') (Ξ» i, h ⊚ g i) := by ext i x; cases x; reflexivity /-- Polynomial functor for the W-type of `P`. `A` is a data-less well-founded tree whereas, for a given `a : A`, `B a` is a valid path in tree `a` so that `Wp.obj Ξ±` is made of a tree and a function from its valid paths to the values it contains -/ def Wp : mvpfunctor n := { A := P.last.W, B := P.W_path } /-- W-type of `P` -/ @[nolint has_nonempty_instance] def W (Ξ± : typevec n) : Type* := P.Wp.obj Ξ± instance mvfunctor_W : mvfunctor P.W := by delta mvpfunctor.W; apply_instance /-! First, describe operations on `W` as a polynomial functor. -/ /-- Constructor for `Wp` -/ def Wp_mk {Ξ± : typevec n} (a : P.A) (f : P.last.B a β†’ P.last.W) (f' : P.W_path ⟨a, f⟩ ⟹ Ξ±) : P.W Ξ± := ⟨⟨a, f⟩, f'⟩ /-- Recursor for `Wp` -/ def Wp_rec {Ξ± : typevec n} {C : Type*} (g : Ξ  (a : P.A) (f : P.last.B a β†’ P.last.W), (P.W_path ⟨a, f⟩ ⟹ Ξ±) β†’ (P.last.B a β†’ C) β†’ C) : Ξ  (x : P.last.W) (f' : P.W_path x ⟹ Ξ±), C | ⟨a, f⟩ f' := g a f f' (Ξ» i, Wp_rec (f i) (P.W_path_dest_right f' i)) theorem Wp_rec_eq {Ξ± : typevec n} {C : Type*} (g : Ξ  (a : P.A) (f : P.last.B a β†’ P.last.W), (P.W_path ⟨a, f⟩ ⟹ Ξ±) β†’ (P.last.B a β†’ C) β†’ C) (a : P.A) (f : P.last.B a β†’ P.last.W) (f' : P.W_path ⟨a, f⟩ ⟹ Ξ±) : P.Wp_rec g ⟨a, f⟩ f' = g a f f' (Ξ» i, P.Wp_rec g (f i) (P.W_path_dest_right f' i)) := rfl -- Note: we could replace Prop by Type* and obtain a dependent recursor theorem Wp_ind {Ξ± : typevec n} {C : Ξ  x : P.last.W, P.W_path x ⟹ Ξ± β†’ Prop} (ih : βˆ€ (a : P.A) (f : P.last.B a β†’ P.last.W) (f' : P.W_path ⟨a, f⟩ ⟹ Ξ±), (βˆ€ i : P.last.B a, C (f i) (P.W_path_dest_right f' i)) β†’ C ⟨a, f⟩ f') : Ξ  (x : P.last.W) (f' : P.W_path x ⟹ Ξ±), C x f' | ⟨a, f⟩ f' := ih a f f' (Ξ» i, Wp_ind _ _) /-! Now think of W as defined inductively by the data ⟨a, f', f⟩ where - `a : P.A` is the shape of the top node - `f' : P.drop.B a ⟹ Ξ±` is the contents of the top node - `f : P.last.B a β†’ P.last.W` are the subtrees -/ /-- Constructor for `W` -/ def W_mk {Ξ± : typevec n} (a : P.A) (f' : P.drop.B a ⟹ Ξ±) (f : P.last.B a β†’ P.W Ξ±) : P.W Ξ± := let g : P.last.B a β†’ P.last.W := Ξ» i, (f i).fst, g' : P.W_path ⟨a, g⟩ ⟹ Ξ± := P.W_path_cases_on f' (Ξ» i, (f i).snd) in ⟨⟨a, g⟩, g'⟩ /-- Recursor for `W` -/ def W_rec {Ξ± : typevec n} {C : Type*} (g : Ξ  a : P.A, ((P.drop).B a ⟹ Ξ±) β†’ ((P.last).B a β†’ P.W Ξ±) β†’ ((P.last).B a β†’ C) β†’ C) : P.W Ξ± β†’ C | ⟨a, f'⟩ := let g' (a : P.A) (f : P.last.B a β†’ P.last.W) (h : P.W_path ⟨a, f⟩ ⟹ Ξ±) (h' : P.last.B a β†’ C) : C := g a (P.W_path_dest_left h) (Ξ» i, ⟨f i, P.W_path_dest_right h i⟩) h' in P.Wp_rec g' a f' /-- Defining equation for the recursor of `W` -/ theorem W_rec_eq {Ξ± : typevec n} {C : Type*} (g : Ξ  a : P.A, ((P.drop).B a ⟹ Ξ±) β†’ ((P.last).B a β†’ P.W Ξ±) β†’ ((P.last).B a β†’ C) β†’ C) (a : P.A) (f' : P.drop.B a ⟹ Ξ±) (f : P.last.B a β†’ P.W Ξ±) : P.W_rec g (P.W_mk a f' f) = g a f' f (Ξ» i, P.W_rec g (f i)) := begin rw [W_mk, W_rec], dsimp, rw [Wp_rec_eq], dsimp only [W_path_dest_left_W_path_cases_on, W_path_dest_right_W_path_cases_on], congr; ext1 i; cases (f i); refl end /-- Induction principle for `W` -/ theorem W_ind {Ξ± : typevec n} {C : P.W Ξ± β†’ Prop} (ih : βˆ€ (a : P.A) (f' : P.drop.B a ⟹ Ξ±) (f : P.last.B a β†’ P.W Ξ±), (βˆ€ i, C (f i)) β†’ C (P.W_mk a f' f)) : βˆ€ x, C x := begin intro x, cases x with a f, apply @Wp_ind n P Ξ± (Ξ» a f, C ⟨a, f⟩), dsimp, intros a f f' ih', dsimp [W_mk] at ih, let ih'' := ih a (P.W_path_dest_left f') (Ξ» i, ⟨f i, P.W_path_dest_right f' i⟩), dsimp at ih'', rw W_path_cases_on_eta at ih'', apply ih'', apply ih' end theorem W_cases {Ξ± : typevec n} {C : P.W Ξ± β†’ Prop} (ih : βˆ€ (a : P.A) (f' : P.drop.B a ⟹ Ξ±) (f : P.last.B a β†’ P.W Ξ±), C (P.W_mk a f' f)) : βˆ€ x, C x := P.W_ind (Ξ» a f' f ih', ih a f' f) /-- W-types are functorial -/ def W_map {Ξ± Ξ² : typevec n} (g : Ξ± ⟹ Ξ²) : P.W Ξ± β†’ P.W Ξ² := Ξ» x, g <$$> x theorem W_mk_eq {Ξ± : typevec n} (a : P.A) (f : P.last.B a β†’ P.last.W) (g' : P.drop.B a ⟹ Ξ±) (g : Ξ  j : P.last.B a, P.W_path (f j) ⟹ Ξ±) : P.W_mk a g' (Ξ» i, ⟨f i, g i⟩) = ⟨⟨a, f⟩, P.W_path_cases_on g' g⟩ := rfl theorem W_map_W_mk {Ξ± Ξ² : typevec n} (g : Ξ± ⟹ Ξ²) (a : P.A) (f' : P.drop.B a ⟹ Ξ±) (f : P.last.B a β†’ P.W Ξ±) : g <$$> P.W_mk a f' f = P.W_mk a (g ⊚ f') (Ξ» i, g <$$> f i) := begin show _ = P.W_mk a (g ⊚ f') (mvfunctor.map g ∘ f), have : mvfunctor.map g ∘ f = Ξ» i, ⟨(f i).fst, g ⊚ ((f i).snd)⟩, { ext i : 1, dsimp [function.comp], cases (f i), refl }, rw this, have : f = Ξ» i, ⟨(f i).fst, (f i).snd⟩, { ext1, cases (f x), refl }, rw this, dsimp, rw [W_mk_eq, W_mk_eq], have h := mvpfunctor.map_eq P.Wp g, rw [h, comp_W_path_cases_on] end -- TODO: this technical theorem is used in one place in constructing the initial algebra. -- Can it be avoided? /-- Constructor of a value of `P.obj (Ξ± ::: Ξ²)` from components. Useful to avoid complicated type annotation -/ @[reducible] def obj_append1 {Ξ± : typevec n} {Ξ² : Type*} (a : P.A) (f' : P.drop.B a ⟹ Ξ±) (f : P.last.B a β†’ Ξ²) : P.obj (Ξ± ::: Ξ²) := ⟨a, split_fun f' f⟩ theorem map_obj_append1 {Ξ± Ξ³ : typevec n} (g : Ξ± ⟹ Ξ³) (a : P.A) (f' : P.drop.B a ⟹ Ξ±) (f : P.last.B a β†’ P.W Ξ±) : append_fun g (P.W_map g) <$$> P.obj_append1 a f' f = P.obj_append1 a (g ⊚ f') (Ξ» x, P.W_map g (f x)) := by rw [obj_append1, obj_append1, map_eq, append_fun, ← split_fun_comp]; refl /-! Yet another view of the W type: as a fixed point for a multivariate polynomial functor. These are needed to use the W-construction to construct a fixed point of a qpf, since the qpf axioms are expressed in terms of `map` on `P`. -/ /-- Constructor for the W-type of `P` -/ def W_mk' {Ξ± : typevec n} : P.obj (Ξ± ::: P.W Ξ±) β†’ P.W Ξ± | ⟨a, f⟩ := P.W_mk a (drop_fun f) (last_fun f) /-- Destructor for the W-type of `P` -/ def W_dest' {Ξ± : typevec.{u} n} : P.W Ξ± β†’ P.obj (Ξ±.append1 (P.W Ξ±)) := P.W_rec (Ξ» a f' f _, ⟨a, split_fun f' f⟩) theorem W_dest'_W_mk {Ξ± : typevec n} (a : P.A) (f' : P.drop.B a ⟹ Ξ±) (f : P.last.B a β†’ P.W Ξ±) : P.W_dest' (P.W_mk a f' f) = ⟨a, split_fun f' f⟩ := by rw [W_dest', W_rec_eq] theorem W_dest'_W_mk' {Ξ± : typevec n} (x : P.obj (Ξ±.append1 (P.W Ξ±))) : P.W_dest' (P.W_mk' x) = x := by cases x with a f; rw [W_mk', W_dest'_W_mk, split_drop_fun_last_fun] end mvpfunctor
a459a3bb2b2011395bca310ccfcf1a44fc5f7d73
abd85493667895c57a7507870867b28124b3998f
/src/algebra/group_power.lean
13472a3314e52ed23baa6c8bd3d18e13dfd39883
[ "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
35,371
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis -/ import data.int.basic import data.equiv.basic import deprecated.ring /-! # Power operations on monoids and groups The power operation on monoids and groups. We separate this from group, because it depends on `β„•`, which in turn depends on other parts of algebra. ## Notation The class `has_pow Ξ± Ξ²` provides the notation `a^b` for powers. We define instances of `has_pow M β„•`, for monoids `M`, and `has_pow G β„€` for groups `G`. We also define infix operators `β€’β„•` and `β€’β„€` for scalar multiplication by a natural and an integer numbers, respectively. ## Implementation details We adopt the convention that `0^0 = 1`. -/ universes u v w x y z u₁ uβ‚‚ variables {M : Type u} {N : Type v} {G : Type w} {H : Type x} {A : Type y} {B : Type z} {R : Type u₁} {S : Type uβ‚‚} /-- The power operation in a monoid. `a^n = a*a*...*a` n times. -/ def monoid.pow [has_mul M] [has_one M] (a : M) : β„• β†’ M | 0 := 1 | (n+1) := a * monoid.pow n /-- The scalar multiplication in an additive monoid. `n β€’β„• a = a+a+...+a` n times. -/ def nsmul [has_add A] [has_zero A] (n : β„•) (a : A) : A := @monoid.pow (multiplicative A) _ { one := (0 : A) } a n infix ` β€’β„• `:70 := nsmul @[priority 5] instance monoid.has_pow [monoid M] : has_pow M β„• := ⟨monoid.pow⟩ /-! ### Commutativity First we prove some facts about `semiconj_by` and `commute`. They do not require any theory about `pow` and/or `nsmul` and will be useful later in this file. -/ namespace semiconj_by variables [monoid M] @[simp] lemma pow_right {a x y : M} (h : semiconj_by a x y) (n : β„•) : semiconj_by a (x^n) (y^n) := nat.rec_on n (one_right a) $ Ξ» n ihn, h.mul_right ihn end semiconj_by namespace commute variables [monoid M] {a b : M} @[simp] theorem pow_right (h : commute a b) (n : β„•) : commute a (b ^ n) := h.pow_right n @[simp] theorem pow_left (h : commute a b) (n : β„•) : commute (a ^ n) b := (h.symm.pow_right n).symm @[simp] theorem pow_pow (h : commute a b) (m n : β„•) : commute (a ^ m) (b ^ n) := (h.pow_left m).pow_right n @[simp] theorem self_pow (a : M) (n : β„•) : commute a (a ^ n) := (commute.refl a).pow_right n @[simp] theorem pow_self (a : M) (n : β„•) : commute (a ^ n) a := (commute.refl a).pow_left n @[simp] theorem pow_pow_self (a : M) (m n : β„•) : commute (a ^ m) (a ^ n) := (commute.refl a).pow_pow m n end commute /-! ### (Additive) monoid -/ section monoid variables [monoid M] [monoid N] [add_monoid A] [add_monoid B] @[simp] theorem pow_zero (a : M) : a^0 = 1 := rfl @[simp] theorem zero_nsmul (a : A) : 0 β€’β„• a = 0 := rfl theorem pow_succ (a : M) (n : β„•) : a^(n+1) = a * a^n := rfl theorem succ_nsmul (a : A) (n : β„•) : (n+1) β€’β„• a = a + n β€’β„• a := rfl @[simp] theorem pow_one (a : M) : a^1 = a := mul_one _ @[simp] theorem one_nsmul (a : A) : 1 β€’β„• a = a := add_zero _ @[simp] lemma pow_ite (P : Prop) [decidable P] (a : M) (b c : β„•) : a ^ (if P then b else c) = if P then a ^ b else a ^ c := by split_ifs; refl @[simp] lemma ite_pow (P : Prop) [decidable P] (a b : M) (c : β„•) : (if P then a else b) ^ c = if P then a ^ c else b ^ c := by split_ifs; refl @[simp] lemma pow_boole (P : Prop) [decidable P] (a : M) : a ^ (if P then 1 else 0) = if P then a else 1 := by simp theorem pow_mul_comm' (a : M) (n : β„•) : a^n * a = a * a^n := commute.pow_self a n theorem nsmul_add_comm' : βˆ€ (a : A) (n : β„•), n β€’β„• a + a = a + n β€’β„• a := @pow_mul_comm' (multiplicative A) _ theorem pow_succ' (a : M) (n : β„•) : a^(n+1) = a^n * a := by rw [pow_succ, pow_mul_comm'] theorem succ_nsmul' (a : A) (n : β„•) : (n+1) β€’β„• a = n β€’β„• a + a := @pow_succ' (multiplicative A) _ _ _ theorem pow_two (a : M) : a^2 = a * a := show a*(a*1)=a*a, by rw mul_one theorem two_nsmul (a : A) : 2 β€’β„• a = a + a := @pow_two (multiplicative A) _ a theorem pow_add (a : M) (m n : β„•) : a^(m + n) = a^m * a^n := by induction n with n ih; [rw [add_zero, pow_zero, mul_one], rw [pow_succ', ← mul_assoc, ← ih, ← pow_succ', add_assoc]] theorem add_nsmul : βˆ€ (a : A) (m n : β„•), (m + n) β€’β„• a = m β€’β„• a + n β€’β„• a := @pow_add (multiplicative A) _ @[simp] theorem one_pow (n : β„•) : (1 : M)^n = 1 := by induction n with n ih; [refl, rw [pow_succ, ih, one_mul]] @[simp] theorem nsmul_zero (n : β„•) : n β€’β„• (0 : A) = 0 := by induction n with n ih; [refl, rw [succ_nsmul, ih, zero_add]] theorem pow_mul (a : M) (m n : β„•) : a^(m * n) = (a^m)^n := by induction n with n ih; [rw mul_zero, rw [nat.mul_succ, pow_add, pow_succ', ih]]; refl theorem mul_nsmul' : βˆ€ (a : A) (m n : β„•), m * n β€’β„• a = n β€’β„• (m β€’β„• a) := @pow_mul (multiplicative A) _ theorem pow_mul' (a : M) (m n : β„•) : a^(m * n) = (a^n)^m := by rw [mul_comm, pow_mul] theorem mul_nsmul (a : A) (m n : β„•) : m * n β€’β„• a = m β€’β„• (n β€’β„• a) := @pow_mul' (multiplicative A) _ a m n @[simp] theorem nsmul_one [has_one A] : βˆ€ n : β„•, n β€’β„• (1 : A) = n := add_monoid_hom.eq_nat_cast ⟨λ n, n β€’β„• (1 : A), zero_nsmul _, Ξ» _ _, add_nsmul _ _ _⟩ (one_nsmul _) theorem pow_bit0 (a : M) (n : β„•) : a ^ bit0 n = a^n * a^n := pow_add _ _ _ theorem bit0_nsmul (a : A) (n : β„•) : bit0 n β€’β„• a = n β€’β„• a + n β€’β„• a := add_nsmul _ _ _ theorem pow_bit1 (a : M) (n : β„•) : a ^ bit1 n = a^n * a^n * a := by rw [bit1, pow_succ', pow_bit0] theorem bit1_nsmul : βˆ€ (a : A) (n : β„•), bit1 n β€’β„• a = n β€’β„• a + n β€’β„• a + a := @pow_bit1 (multiplicative A) _ theorem pow_mul_comm (a : M) (m n : β„•) : a^m * a^n = a^n * a^m := commute.pow_pow_self a m n theorem nsmul_add_comm : βˆ€ (a : A) (m n : β„•), m β€’β„• a + n β€’β„• a = n β€’β„• a + m β€’β„• a := @pow_mul_comm (multiplicative A) _ @[simp, priority 500] theorem list.prod_repeat (a : M) (n : β„•) : (list.repeat a n).prod = a ^ n := by induction n with n ih; [refl, rw [list.repeat_succ, list.prod_cons, ih]]; refl @[simp, priority 500] theorem list.sum_repeat : βˆ€ (a : A) (n : β„•), (list.repeat a n).sum = n β€’β„• a := @list.prod_repeat (multiplicative A) _ theorem monoid_hom.map_pow (f : M β†’* N) (a : M) : βˆ€(n : β„•), f (a ^ n) = (f a) ^ n | 0 := f.map_one | (n+1) := by rw [pow_succ, pow_succ, f.map_mul, monoid_hom.map_pow] theorem add_monoid_hom.map_nsmul (f : A β†’+ B) (a : A) (n : β„•) : f (n β€’β„• a) = n β€’β„• f a := f.to_multiplicative.map_pow a n theorem is_monoid_hom.map_pow (f : M β†’ N) [is_monoid_hom f] (a : M) : βˆ€(n : β„•), f (a ^ n) = (f a) ^ n := (monoid_hom.of f).map_pow a theorem is_add_monoid_hom.map_nsmul (f : A β†’ B) [is_add_monoid_hom f] (a : A) (n : β„•) : f (n β€’β„• a) = n β€’β„• f a := (add_monoid_hom.of f).map_nsmul a n @[simp, norm_cast] lemma units.coe_pow (u : units M) (n : β„•) : ((u ^ n : units M) : M) = u ^ n := (units.coe_hom M).map_pow u n lemma commute.mul_pow {a b : M} (h : commute a b) (n : β„•) : (a * b) ^ n = a ^ n * b ^ n := nat.rec_on n (by simp) $ Ξ» n ihn, by simp only [pow_succ, ihn, ← mul_assoc, (h.pow_left n).right_comm] theorem neg_pow [ring R] (a : R) (n : β„•) : (- a) ^ n = (-1) ^ n * a ^ n := (neg_one_mul a) β–Έ (commute.neg_one_left a).mul_pow n end monoid @[simp] theorem nat.pow_eq_pow (p q : β„•) : @has_pow.pow _ _ monoid.has_pow p q = p ^ q := by induction q with q ih; [refl, rw [nat.pow_succ, pow_succ', ih]] theorem nat.nsmul_eq_mul (m n : β„•) : m β€’β„• n = m * n := by induction m with m ih; [rw [zero_nsmul, zero_mul], rw [succ_nsmul', ih, nat.succ_mul]] /-! ### Commutative (additive) monoid -/ section comm_monoid variables [comm_monoid M] [add_comm_monoid A] theorem mul_pow (a b : M) (n : β„•) : (a * b)^n = a^n * b^n := (commute.all a b).mul_pow n theorem nsmul_add : βˆ€ (a b : A) (n : β„•), n β€’β„• (a + b) = n β€’β„• a + n β€’β„• b := @mul_pow (multiplicative A) _ instance pow.is_monoid_hom (n : β„•) : is_monoid_hom ((^ n) : M β†’ M) := { map_mul := Ξ» _ _, mul_pow _ _ _, map_one := one_pow _ } instance nsmul.is_add_monoid_hom (n : β„•) : is_add_monoid_hom (nsmul n : A β†’ A) := { map_add := Ξ» _ _, nsmul_add _ _ _, map_zero := nsmul_zero _ } end comm_monoid section group variables [group G] [group H] [add_group A] [add_group B] section nat @[simp] theorem inv_pow (a : G) (n : β„•) : (a⁻¹)^n = (a^n)⁻¹ := by induction n with n ih; [exact one_inv.symm, rw [pow_succ', pow_succ, ih, mul_inv_rev]] @[simp] theorem neg_nsmul : βˆ€ (a : A) (n : β„•), n β€’β„• (-a) = -(n β€’β„• a) := @inv_pow (multiplicative A) _ theorem pow_sub (a : G) {m n : β„•} (h : n ≀ m) : a^(m - n) = a^m * (a^n)⁻¹ := have h1 : m - n + n = m, from nat.sub_add_cancel h, have h2 : a^(m - n) * a^n = a^m, by rw [←pow_add, h1], eq_mul_inv_of_mul_eq h2 theorem nsmul_sub : βˆ€ (a : A) {m n : β„•}, n ≀ m β†’ (m - n) β€’β„• a = m β€’β„• a - n β€’β„• a := @pow_sub (multiplicative A) _ theorem pow_inv_comm (a : G) (m n : β„•) : (a⁻¹)^m * a^n = a^n * (a⁻¹)^m := (commute.refl a).inv_left.pow_pow m n theorem nsmul_neg_comm : βˆ€ (a : A) (m n : β„•), m β€’β„• (-a) + n β€’β„• a = n β€’β„• a + m β€’β„• (-a) := @pow_inv_comm (multiplicative A) _ end nat open int /-- The power operation in a group. This extends `monoid.pow` to negative integers with the definition `a^(-n) = (a^n)⁻¹`. -/ def gpow (a : G) : β„€ β†’ G | (of_nat n) := a^n | -[1+n] := (a^(nat.succ n))⁻¹ /-- The scalar multiplication by integers on an additive group. This extends `nsmul` to negative integers with the definition `(-n) β€’β„€ a = -(n β€’β„• a)`. -/ def gsmul (n : β„€) (a : A) : A := @gpow (multiplicative A) _ a n @[priority 10] instance group.has_pow : has_pow G β„€ := ⟨gpow⟩ infix ` β€’β„€ `:70 := gsmul @[simp] theorem gpow_coe_nat (a : G) (n : β„•) : a ^ (n:β„€) = a ^ n := rfl @[simp] theorem gsmul_coe_nat (a : A) (n : β„•) : n β€’β„€ a = n β€’β„• a := rfl theorem gpow_of_nat (a : G) (n : β„•) : a ^ of_nat n = a ^ n := rfl theorem gsmul_of_nat (a : A) (n : β„•) : of_nat n β€’β„€ a = n β€’β„• a := rfl @[simp] theorem gpow_neg_succ_of_nat (a : G) (n : β„•) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl @[simp] theorem gsmul_neg_succ_of_nat (a : A) (n : β„•) : -[1+n] β€’β„€ a = - (n.succ β€’β„• a) := rfl local attribute [ematch] le_of_lt open nat @[simp] theorem gpow_zero (a : G) : a ^ (0:β„€) = 1 := rfl @[simp] theorem zero_gsmul (a : A) : (0:β„€) β€’β„€ a = 0 := rfl @[simp] theorem gpow_one (a : G) : a ^ (1:β„€) = a := pow_one a @[simp] theorem one_gsmul (a : A) : (1:β„€) β€’β„€ a = a := add_zero _ @[simp] theorem one_gpow : βˆ€ (n : β„€), (1 : G) ^ n = 1 | (n : β„•) := one_pow _ | -[1+ n] := show _⁻¹=(1:G), by rw [_root_.one_pow, one_inv] @[simp] theorem gsmul_zero : βˆ€ (n : β„€), n β€’β„€ (0 : A) = 0 := @one_gpow (multiplicative A) _ @[simp] theorem gpow_neg (a : G) : βˆ€ (n : β„€), a ^ -n = (a ^ n)⁻¹ | (n+1:β„•) := rfl | 0 := one_inv.symm | -[1+ n] := (inv_inv _).symm @[simp] theorem neg_gsmul : βˆ€ (a : A) (n : β„€), -n β€’β„€ a = -(n β€’β„€ a) := @gpow_neg (multiplicative A) _ theorem gpow_neg_one (x : G) : x ^ (-1:β„€) = x⁻¹ := congr_arg has_inv.inv $ pow_one x theorem neg_one_gsmul (x : A) : (-1:β„€) β€’β„€ x = -x := congr_arg has_neg.neg $ one_nsmul x theorem gsmul_one [has_one A] (n : β„€) : n β€’β„€ (1 : A) = n := by cases n; simp theorem inv_gpow (a : G) : βˆ€n:β„€, a⁻¹ ^ n = (a ^ n)⁻¹ | (n : β„•) := inv_pow a n | -[1+ n] := congr_arg has_inv.inv $ inv_pow a (n+1) theorem gsmul_neg (a : A) (n : β„€) : gsmul n (- a) = - gsmul n a := @inv_gpow (multiplicative A) _ a n lemma gpow_add_one (a : G) : βˆ€ n : β„€, a ^ (n + 1) = a ^ n * a | (of_nat n) := by simp [← int.coe_nat_succ, pow_succ'] | -[1+0] := by simp [int.neg_succ_of_nat_eq] | -[1+(n+1)] := by rw [int.neg_succ_of_nat_eq, gpow_neg, neg_add, neg_add_cancel_right, gpow_neg, ← int.coe_nat_succ, gpow_coe_nat, gpow_coe_nat, _root_.pow_succ _ (n + 1), mul_inv_rev, inv_mul_cancel_right] theorem add_one_gsmul : βˆ€ (a : A) (i : β„€), (i + 1) β€’β„€ a = i β€’β„€ a + a := @gpow_add_one (multiplicative A) _ lemma gpow_sub_one (a : G) (n : β„€) : a ^ (n - 1) = a ^ n * a⁻¹ := calc a ^ (n - 1) = a ^ (n - 1) * a * a⁻¹ : (mul_inv_cancel_right _ _).symm ... = a^n * a⁻¹ : by rw [← gpow_add_one, sub_add_cancel] lemma gpow_add (a : G) (m n : β„€) : a ^ (m + n) = a ^ m * a ^ n := begin induction n using int.induction_on with n ihn n ihn, case hz : { simp }, { simp only [← add_assoc, gpow_add_one, ihn, mul_assoc] }, { rw [gpow_sub_one, ← mul_assoc, ← ihn, ← gpow_sub_one, add_sub_assoc] } end theorem add_gsmul : βˆ€ (a : A) (i j : β„€), (i + j) β€’β„€ a = i β€’β„€ a + j β€’β„€ a := @gpow_add (multiplicative A) _ lemma gpow_sub (a : G) (m n : β„€) : a ^ (m - n) = a ^ m * (a ^ n)⁻¹ := by rw [sub_eq_add_neg, gpow_add, gpow_neg] lemma sub_gsmul (m n : β„€) (a : A) : (m - n) β€’β„€ a = m β€’β„€ a - n β€’β„€ a := @gpow_sub (multiplicative A) _ _ _ _ theorem gpow_one_add (a : G) (i : β„€) : a ^ (1 + i) = a * a ^ i := by rw [gpow_add, gpow_one] theorem one_add_gsmul : βˆ€ (a : A) (i : β„€), (1 + i) β€’β„€ a = a + i β€’β„€ a := @gpow_one_add (multiplicative A) _ theorem gpow_mul_comm (a : G) (i j : β„€) : a ^ i * a ^ j = a ^ j * a ^ i := by rw [← gpow_add, ← gpow_add, add_comm] theorem gsmul_add_comm : βˆ€ (a : A) (i j), i β€’β„€ a + j β€’β„€ a = j β€’β„€ a + i β€’β„€ a := @gpow_mul_comm (multiplicative A) _ theorem gpow_mul (a : G) (m n : β„€) : a ^ (m * n) = (a ^ m) ^ n := int.induction_on n (by simp) (Ξ» n ihn, by simp [mul_add, gpow_add, ihn]) (Ξ» n ihn, by simp only [mul_sub, gpow_sub, ihn, mul_one, gpow_one]) theorem gsmul_mul' : βˆ€ (a : A) (m n : β„€), m * n β€’β„€ a = n β€’β„€ (m β€’β„€ a) := @gpow_mul (multiplicative A) _ theorem gpow_mul' (a : G) (m n : β„€) : a ^ (m * n) = (a ^ n) ^ m := by rw [mul_comm, gpow_mul] theorem gsmul_mul (a : A) (m n : β„€) : m * n β€’β„€ a = m β€’β„€ (n β€’β„€ a) := by rw [mul_comm, gsmul_mul'] theorem gpow_bit0 (a : G) (n : β„€) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _ theorem bit0_gsmul (a : A) (n : β„€) : bit0 n β€’β„€ a = n β€’β„€ a + n β€’β„€ a := gpow_add _ _ _ theorem gpow_bit1 (a : G) (n : β„€) : a ^ bit1 n = a ^ n * a ^ n * a := by rw [bit1, gpow_add]; simp [gpow_bit0] theorem bit1_gsmul : βˆ€ (a : A) (n : β„€), bit1 n β€’β„€ a = n β€’β„€ a + n β€’β„€ a + a := @gpow_bit1 (multiplicative A) _ theorem monoid_hom.map_gpow (f : G β†’* H) (a : G) (n : β„€) : f (a ^ n) = f a ^ n := by cases n; [exact f.map_pow _ _, exact (f.map_inv _).trans (congr_arg _ $ f.map_pow _ _)] theorem add_monoid_hom.map_gsmul (f : A β†’+ B) (a : A) (n : β„€) : f (n β€’β„€ a) = n β€’β„€ f a := f.to_multiplicative.map_gpow a n @[simp, norm_cast] lemma units.coe_gpow (u : units G) (n : β„€) : ((u ^ n : units G) : G) = u ^ n := (units.coe_hom G).map_gpow u n theorem commute.mul_gpow {a b : G} (h : commute a b) : βˆ€ n : β„€, (a * b) ^ n = a ^ n * b ^ n | (n : β„•) := h.mul_pow n | -[1+n] := by simp [h.mul_pow, (h.pow_pow n.succ n.succ).inv_inv.symm.eq] end group section comm_group variables [comm_group G] [add_comm_group A] theorem mul_gpow (a b : G) (n : β„€) : (a * b)^n = a^n * b^n := (commute.all a b).mul_gpow n theorem gsmul_add : βˆ€ (a b : A) (n : β„€), n β€’β„€ (a + b) = n β€’β„€ a + n β€’β„€ b := @mul_gpow (multiplicative A) _ theorem gsmul_sub (a b : A) (n : β„€) : gsmul n (a - b) = gsmul n a - gsmul n b := by simp only [gsmul_add, gsmul_neg, sub_eq_add_neg] instance gpow.is_group_hom (n : β„€) : is_group_hom ((^ n) : G β†’ G) := { map_mul := Ξ» _ _, mul_gpow _ _ n } instance gsmul.is_add_group_hom (n : β„€) : is_add_group_hom (gsmul n : A β†’ A) := { map_add := Ξ» _ _, gsmul_add _ _ n } end comm_group @[simp] lemma with_bot.coe_nsmul [add_monoid A] (a : A) (n : β„•) : ((nsmul n a : A) : with_bot A) = nsmul n a := add_monoid_hom.map_nsmul ⟨_, with_bot.coe_zero, with_bot.coe_add⟩ a n theorem nsmul_eq_mul' [semiring R] (a : R) (n : β„•) : n β€’β„• a = a * n := by induction n with n ih; [rw [zero_nsmul, nat.cast_zero, mul_zero], rw [succ_nsmul', ih, nat.cast_succ, mul_add, mul_one]] @[simp] theorem nsmul_eq_mul [semiring R] (n : β„•) (a : R) : n β€’β„• a = n * a := by rw [nsmul_eq_mul', (n.cast_commute a).eq] theorem mul_nsmul_left [semiring R] (a b : R) (n : β„•) : n β€’β„• (a * b) = a * (n β€’β„• b) := by rw [nsmul_eq_mul', nsmul_eq_mul', mul_assoc] theorem mul_nsmul_assoc [semiring R] (a b : R) (n : β„•) : n β€’β„• (a * b) = n β€’β„• a * b := by rw [nsmul_eq_mul, nsmul_eq_mul, mul_assoc] lemma zero_pow [semiring R] : βˆ€ {n : β„•}, 0 < n β†’ (0 : R) ^ n = 0 | (n+1) _ := zero_mul _ @[simp, norm_cast] theorem nat.cast_pow [semiring R] (n m : β„•) : (↑(n ^ m) : R) = ↑n ^ m := by induction m with m ih; [exact nat.cast_one, rw [nat.pow_succ, pow_succ', nat.cast_mul, ih]] @[simp, norm_cast] theorem int.coe_nat_pow (n m : β„•) : ((n ^ m : β„•) : β„€) = n ^ m := by induction m with m ih; [exact int.coe_nat_one, rw [nat.pow_succ, pow_succ', int.coe_nat_mul, ih]] theorem int.nat_abs_pow (n : β„€) (k : β„•) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k := by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, nat.pow_succ, ih]] namespace ring_hom variables [semiring R] [semiring S] @[simp] lemma map_pow (f : R β†’+* S) (a) : βˆ€ n : β„•, f (a ^ n) = (f a) ^ n := f.to_monoid_hom.map_pow a end ring_hom lemma is_semiring_hom.map_pow [semiring R] [semiring S] (f : R β†’ S) [is_semiring_hom f] (a) : βˆ€ n : β„•, f (a ^ n) = (f a) ^ n := is_monoid_hom.map_pow f a theorem neg_one_pow_eq_or [ring R] : βˆ€ n : β„•, (-1 : R)^n = 1 ∨ (-1 : R)^n = -1 | 0 := or.inl rfl | (n+1) := (neg_one_pow_eq_or n).swap.imp (Ξ» h, by rw [pow_succ, h, neg_one_mul, neg_neg]) (Ξ» h, by rw [pow_succ, h, mul_one]) lemma pow_dvd_pow [comm_semiring R] (a : R) {m n : β„•} (h : m ≀ n) : a ^ m ∣ a ^ n := ⟨a ^ (n - m), by rw [← pow_add, nat.add_sub_cancel' h]⟩ -- The next four lemmas allow us to replace multiplication by a numeral with a `gsmul` expression. -- They are used by the `noncomm_ring` tactic, to normalise expressions before passing to `abel`. lemma bit0_mul [ring R] {n r : R} : bit0 n * r = gsmul 2 (n * r) := by { dsimp [bit0], rw [add_mul, add_gsmul, one_gsmul], } lemma mul_bit0 [ring R] {n r : R} : r * bit0 n = gsmul 2 (r * n) := by { dsimp [bit0], rw [mul_add, add_gsmul, one_gsmul], } lemma bit1_mul [ring R] {n r : R} : bit1 n * r = gsmul 2 (n * r) + r := by { dsimp [bit1], rw [add_mul, bit0_mul, one_mul], } lemma mul_bit1 [ring R] {n r : R} : r * bit1 n = gsmul 2 (r * n) + r := by { dsimp [bit1], rw [mul_add, mul_bit0, mul_one], } @[simp] theorem gsmul_eq_mul [ring R] (a : R) : βˆ€ n, n β€’β„€ a = n * a | (n : β„•) := nsmul_eq_mul _ _ | -[1+ n] := show -(_ β€’β„• _)=-_*_, by rw [neg_mul_eq_neg_mul_symm, nsmul_eq_mul, nat.cast_succ] theorem gsmul_eq_mul' [ring R] (a : R) (n : β„€) : n β€’β„€ a = a * n := by rw [gsmul_eq_mul, (n.cast_commute a).eq] theorem mul_gsmul_left [ring R] (a b : R) (n : β„€) : n β€’β„€ (a * b) = a * (n β€’β„€ b) := by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc] theorem mul_gsmul_assoc [ring R] (a b : R) (n : β„€) : n β€’β„€ (a * b) = n β€’β„€ a * b := by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc] @[simp] lemma gsmul_int_int (a b : β„€) : a β€’β„€ b = a * b := by simp [gsmul_eq_mul] lemma gsmul_int_one (n : β„€) : n β€’β„€ 1 = n := by simp @[simp, norm_cast] theorem int.cast_pow [ring R] (n : β„€) (m : β„•) : (↑(n ^ m) : R) = ↑n ^ m := by induction m with m ih; [exact int.cast_one, rw [pow_succ, pow_succ, int.cast_mul, ih]] lemma neg_one_pow_eq_pow_mod_two [ring R] {n : β„•} : (-1 : R) ^ n = -1 ^ (n % 2) := by rw [← nat.mod_add_div n 2, pow_add, pow_mul]; simp [pow_two] theorem sq_sub_sq [comm_ring R] (a b : R) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by rw [pow_two, pow_two, mul_self_sub_mul_self] theorem pow_eq_zero [domain R] {x : R} {n : β„•} (H : x^n = 0) : x = 0 := begin induction n with n ih, { rw pow_zero at H, rw [← mul_one x, H, mul_zero] }, exact or.cases_on (mul_eq_zero.1 H) id ih end @[field_simps] theorem pow_ne_zero [domain R] {a : R} (n : β„•) (h : a β‰  0) : a ^ n β‰  0 := mt pow_eq_zero h theorem nsmul_nonneg [ordered_add_comm_monoid R] {a : R} (H : 0 ≀ a) : βˆ€ n : β„•, 0 ≀ n β€’β„• a | 0 := le_refl _ | (n+1) := add_nonneg' H (nsmul_nonneg n) lemma pow_abs [decidable_linear_ordered_comm_ring R] (a : R) (n : β„•) : (abs a)^n = abs (a^n) := by induction n with n ih; [exact (abs_one).symm, rw [pow_succ, pow_succ, ih, abs_mul]] lemma abs_neg_one_pow [decidable_linear_ordered_comm_ring R] (n : β„•) : abs ((-1 : R)^n) = 1 := by rw [←pow_abs, abs_neg, abs_one, one_pow] section add_monoid variable [ordered_add_comm_monoid A] theorem nsmul_le_nsmul {a : A} {n m : β„•} (ha : 0 ≀ a) (h : n ≀ m) : n β€’β„• a ≀ m β€’β„• a := let ⟨k, hk⟩ := nat.le.dest h in calc n β€’β„• a = n β€’β„• a + 0 : (add_zero _).symm ... ≀ n β€’β„• a + k β€’β„• a : add_le_add_left' (nsmul_nonneg ha _) ... = m β€’β„• a : by rw [← hk, add_nsmul] lemma nsmul_le_nsmul_of_le_right {a b : A} (hab : a ≀ b) : βˆ€ i : β„•, i β€’β„• a ≀ i β€’β„• b | 0 := by simp | (k+1) := add_le_add' hab (nsmul_le_nsmul_of_le_right _) end add_monoid namespace canonically_ordered_semiring variable [canonically_ordered_comm_semiring R] theorem pow_pos {a : R} (H : 0 < a) : βˆ€ n : β„•, 0 < a ^ n | 0 := canonically_ordered_semiring.zero_lt_one | (n+1) := canonically_ordered_semiring.mul_pos.2 ⟨H, pow_pos n⟩ lemma pow_le_pow_of_le_left {a b : R} (hab : a ≀ b) : βˆ€ i : β„•, a^i ≀ b^i | 0 := by simp | (k+1) := canonically_ordered_semiring.mul_le_mul hab (pow_le_pow_of_le_left k) theorem one_le_pow_of_one_le {a : R} (H : 1 ≀ a) (n : β„•) : 1 ≀ a ^ n := by simpa only [one_pow] using pow_le_pow_of_le_left H n theorem pow_le_one {a : R} (H : a ≀ 1) (n : β„•) : a ^ n ≀ 1:= by simpa only [one_pow] using pow_le_pow_of_le_left H n end canonically_ordered_semiring section linear_ordered_semiring variable [linear_ordered_semiring R] theorem pow_pos {a : R} (H : 0 < a) : βˆ€ (n : β„•), 0 < a ^ n | 0 := zero_lt_one | (n+1) := mul_pos H (pow_pos _) theorem pow_nonneg {a : R} (H : 0 ≀ a) : βˆ€ (n : β„•), 0 ≀ a ^ n | 0 := zero_le_one | (n+1) := mul_nonneg H (pow_nonneg _) theorem pow_lt_pow_of_lt_left {x y : R} {n : β„•} (Hxy : x < y) (Hxpos : 0 ≀ x) (Hnpos : 0 < n) : x ^ n < y ^ n := begin cases lt_or_eq_of_le Hxpos, { rw ←nat.sub_add_cancel Hnpos, induction (n - 1), { simpa only [pow_one] }, rw [pow_add, pow_add, nat.succ_eq_add_one, pow_one, pow_one], apply mul_lt_mul ih (le_of_lt Hxy) h (le_of_lt (pow_pos (lt_trans h Hxy) _)) }, { rw [←h, zero_pow Hnpos], apply pow_pos (by rwa ←h at Hxy : 0 < y),} end theorem pow_left_inj {x y : R} {n : β„•} (Hxpos : 0 ≀ x) (Hypos : 0 ≀ y) (Hnpos : 0 < n) (Hxyn : x ^ n = y ^ n) : x = y := begin rcases lt_trichotomy x y with hxy | rfl | hyx, { exact absurd Hxyn (ne_of_lt (pow_lt_pow_of_lt_left hxy Hxpos Hnpos)) }, { refl }, { exact absurd Hxyn (ne_of_gt (pow_lt_pow_of_lt_left hyx Hypos Hnpos)) }, end theorem one_le_pow_of_one_le {a : R} (H : 1 ≀ a) : βˆ€ (n : β„•), 1 ≀ a ^ n | 0 := le_refl _ | (n+1) := by simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le n) zero_le_one (le_trans zero_le_one H) /-- Bernoulli's inequality. This version works for semirings but requires an additional hypothesis `0 ≀ a * a`. -/ theorem one_add_mul_le_pow' {a : R} (Hsqr : 0 ≀ a * a) (H : 0 ≀ 1 + a) : βˆ€ (n : β„•), 1 + n β€’β„• a ≀ (1 + a) ^ n | 0 := le_of_eq $ add_zero _ | (n+1) := calc 1 + (n + 1) β€’β„• a ≀ (1 + a) * (1 + n β€’β„• a) : by simpa [succ_nsmul, mul_add, add_mul, mul_nsmul_left, add_comm, add_left_comm] using nsmul_nonneg Hsqr n ... ≀ (1 + a)^(n+1) : mul_le_mul_of_nonneg_left (one_add_mul_le_pow' n) H theorem pow_le_pow {a : R} {n m : β„•} (ha : 1 ≀ a) (h : n ≀ m) : a ^ n ≀ a ^ m := let ⟨k, hk⟩ := nat.le.dest h in calc a ^ n = a ^ n * 1 : (mul_one _).symm ... ≀ a ^ n * a ^ k : mul_le_mul_of_nonneg_left (one_le_pow_of_one_le ha _) (pow_nonneg (le_trans zero_le_one ha) _) ... = a ^ m : by rw [←hk, pow_add] lemma pow_lt_pow {a : R} {n m : β„•} (h : 1 < a) (h2 : n < m) : a ^ n < a ^ m := begin have h' : 1 ≀ a := le_of_lt h, have h'' : 0 < a := lt_trans zero_lt_one h, cases m, cases h2, rw [pow_succ, ←one_mul (a ^ n)], exact mul_lt_mul h (pow_le_pow h' (nat.le_of_lt_succ h2)) (pow_pos h'' _) (le_of_lt h'') end lemma pow_le_pow_of_le_left {a b : R} (ha : 0 ≀ a) (hab : a ≀ b) : βˆ€ i : β„•, a^i ≀ b^i | 0 := by simp | (k+1) := mul_le_mul hab (pow_le_pow_of_le_left _) (pow_nonneg ha _) (le_trans ha hab) lemma lt_of_pow_lt_pow {a b : R} (n : β„•) (hb : 0 ≀ b) (h : a ^ n < b ^ n) : a < b := lt_of_not_ge $ Ξ» hn, not_lt_of_ge (pow_le_pow_of_le_left hb hn _) h private lemma pow_lt_pow_of_lt_one_aux {a : R} (h : 0 < a) (ha : a < 1) (i : β„•) : βˆ€ k : β„•, a ^ (i + k + 1) < a ^ i | 0 := begin simp only [add_zero], rw ←one_mul (a^i), exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one end | (k+1) := begin rw ←one_mul (a^i), apply mul_lt_mul ha _ _ zero_le_one, { apply le_of_lt, apply pow_lt_pow_of_lt_one_aux }, { show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h } end private lemma pow_le_pow_of_le_one_aux {a : R} (h : 0 ≀ a) (ha : a ≀ 1) (i : β„•) : βˆ€ k : β„•, a ^ (i + k) ≀ a ^ i | 0 := by simp | (k+1) := by rw [←add_assoc, ←one_mul (a^i)]; exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one lemma pow_lt_pow_of_lt_one {a : R} (h : 0 < a) (ha : a < 1) {i j : β„•} (hij : i < j) : a ^ j < a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_lt hij in by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _ lemma pow_le_pow_of_le_one {a : R} (h : 0 ≀ a) (ha : a ≀ 1) {i j : β„•} (hij : i ≀ j) : a ^ j ≀ a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_le hij in by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _ lemma pow_le_one {x : R} : βˆ€ (n : β„•) (h0 : 0 ≀ x) (h1 : x ≀ 1), x ^ n ≀ 1 | 0 h0 h1 := le_refl (1 : R) | (n+1) h0 h1 := mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1) end linear_ordered_semiring theorem pow_two_nonneg [linear_ordered_ring R] (a : R) : 0 ≀ a ^ 2 := by { rw pow_two, exact mul_self_nonneg _ } /-- Bernoulli's inequality for `n : β„•`, `-2 ≀ a`. -/ theorem one_add_mul_le_pow [linear_ordered_ring R] {a : R} (H : -2 ≀ a) : βˆ€ (n : β„•), 1 + n β€’β„• a ≀ (1 + a) ^ n | 0 := le_of_eq $ add_zero _ | 1 := by simp | (n+2) := have H' : 0 ≀ 2 + a, from neg_le_iff_add_nonneg.1 H, have 0 ≀ n β€’β„• (a * a * (2 + a)) + a * a, from add_nonneg (nsmul_nonneg (mul_nonneg (mul_self_nonneg a) H') n) (mul_self_nonneg a), calc 1 + (n + 2) β€’β„• a ≀ 1 + (n + 2) β€’β„• a + (n β€’β„• (a * a * (2 + a)) + a * a) : (le_add_iff_nonneg_right _).2 this ... = (1 + a) * (1 + a) * (1 + n β€’β„• a) : by { simp only [add_mul, mul_add, mul_two, mul_one, one_mul, succ_nsmul, nsmul_add, mul_nsmul_assoc, (mul_nsmul_left _ _ _).symm], ac_refl } ... ≀ (1 + a) * (1 + a) * (1 + a)^n : mul_le_mul_of_nonneg_left (one_add_mul_le_pow n) (mul_self_nonneg (1 + a)) ... = (1 + a)^(n + 2) : by simp only [pow_succ, mul_assoc] /-- Bernoulli's inequality reformulated to estimate `a^n`. -/ theorem one_add_sub_mul_le_pow [linear_ordered_ring R] {a : R} (H : -1 ≀ a) (n : β„•) : 1 + n β€’β„• (a - 1) ≀ a ^ n := have -2 ≀ a - 1, by { rw [bit0, neg_add], exact sub_le_sub_right H 1 }, by simpa only [add_sub_cancel'_right] using one_add_mul_le_pow this n namespace int lemma units_pow_two (u : units β„€) : u ^ 2 = 1 := (units_eq_one_or u).elim (Ξ» h, h.symm β–Έ rfl) (Ξ» h, h.symm β–Έ rfl) lemma units_pow_eq_pow_mod_two (u : units β„€) (n : β„•) : u ^ n = u ^ (n % 2) := by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_pow_two, one_pow, mul_one] @[simp] lemma nat_abs_pow_two (x : β„€) : (x.nat_abs ^ 2 : β„€) = x ^ 2 := by rw [pow_two, int.nat_abs_mul_self', pow_two] end int @[simp] lemma neg_square {Ξ±} [ring Ξ±] (z : Ξ±) : (-z)^2 = z^2 := by simp [pow, monoid.pow] lemma of_add_nsmul [add_monoid A] (x : A) (n : β„•) : multiplicative.of_add (n β€’β„• x) = (multiplicative.of_add x)^n := rfl lemma of_add_gsmul [add_group A] (x : A) (n : β„€) : multiplicative.of_add (n β€’β„€ x) = (multiplicative.of_add x)^n := rfl variables (M G A) /-- Monoid homomorphisms from `multiplicative β„•` are defined by the image of `multiplicative.of_add 1`. -/ def powers_hom [monoid M] : M ≃ (multiplicative β„• β†’* M) := { to_fun := Ξ» x, ⟨λ n, x ^ n.to_add, pow_zero x, Ξ» m n, pow_add x m n⟩, inv_fun := Ξ» f, f (multiplicative.of_add 1), left_inv := pow_one, right_inv := Ξ» f, monoid_hom.ext $ Ξ» n, by { simp [← f.map_pow, ← of_add_nsmul] } } /-- Monoid homomorphisms from `multiplicative β„€` are defined by the image of `multiplicative.of_add 1`. -/ def gpowers_hom [group G] : G ≃ (multiplicative β„€ β†’* G) := { to_fun := Ξ» x, ⟨λ n, x ^ n.to_add, gpow_zero x, Ξ» m n, gpow_add x m n⟩, inv_fun := Ξ» f, f (multiplicative.of_add 1), left_inv := gpow_one, right_inv := Ξ» f, monoid_hom.ext $ Ξ» n, by { simp [← f.map_gpow, ← of_add_gsmul ] } } /-- Additive homomorphisms from `β„•` are defined by the image of `1`. -/ def multiples_hom [add_monoid A] : A ≃ (β„• β†’+ A) := { to_fun := Ξ» x, ⟨λ n, n β€’β„• x, zero_nsmul x, Ξ» m n, add_nsmul _ _ _⟩, inv_fun := Ξ» f, f 1, left_inv := one_nsmul, right_inv := Ξ» f, add_monoid_hom.ext_nat $ one_nsmul (f 1) } /-- Additive homomorphisms from `β„€` are defined by the image of `1`. -/ def gmultiples_hom [add_group A] : A ≃ (β„€ β†’+ A) := { to_fun := Ξ» x, ⟨λ n, n β€’β„€ x, zero_gsmul x, Ξ» m n, add_gsmul _ _ _⟩, inv_fun := Ξ» f, f 1, left_inv := one_gsmul, right_inv := Ξ» f, add_monoid_hom.ext_int $ one_gsmul (f 1) } variables {M G A} @[simp] lemma powers_hom_apply [monoid M] (x : M) (n : multiplicative β„•) : powers_hom M x n = x ^ n.to_add := rfl @[simp] lemma powers_hom_symm_apply [monoid M] (f : multiplicative β„• β†’* M) : (powers_hom M).symm f = f (multiplicative.of_add 1) := rfl lemma mnat_monoid_hom_eq [monoid M] (f : multiplicative β„• β†’* M) (n : multiplicative β„•) : f n = (f (multiplicative.of_add 1)) ^ n.to_add := by rw [← powers_hom_symm_apply, ← powers_hom_apply, equiv.apply_symm_apply] lemma mnat_monoid_hom_ext [monoid M] ⦃f g : multiplicative β„• β†’* M⦄ (h : f (multiplicative.of_add 1) = g (multiplicative.of_add 1)) : f = g := monoid_hom.ext $ Ξ» n, by rw [mnat_monoid_hom_eq f, mnat_monoid_hom_eq g, h] /-! ### Commutativity (again) Facts about `semiconj_by` and `commute` that require `gpow` or `gsmul`, or the fact that integer multiplication equals semiring multiplication. -/ namespace semiconj_by section variables [semiring R] {a x y : R} @[simp] lemma cast_nat_mul_right (h : semiconj_by a x y) (n : β„•) : semiconj_by a ((n : R) * x) (n * y) := semiconj_by.mul_right (nat.commute_cast _ _) h @[simp] lemma cast_nat_mul_left (h : semiconj_by a x y) (n : β„•) : semiconj_by ((n : R) * a) x y := semiconj_by.mul_left (nat.cast_commute _ _) h @[simp] lemma cast_nat_mul_cast_nat_mul (h : semiconj_by a x y) (m n : β„•) : semiconj_by ((m : R) * a) (n * x) (n * y) := (h.cast_nat_mul_left m).cast_nat_mul_right n end variables [monoid M] [group G] [ring R] @[simp] lemma units_gpow_right {a : M} {x y : units M} (h : semiconj_by a x y) : βˆ€ m : β„€, semiconj_by a (↑(x^m)) (↑(y^m)) | (n : β„•) := by simp only [gpow_coe_nat, units.coe_pow, h, pow_right] | -[1+n] := by simp only [gpow_neg_succ_of_nat, units.coe_pow, units_inv_right, h, pow_right] @[simp] lemma gpow_right {a x y : G} (h : semiconj_by a x y) : βˆ€ m : β„€, semiconj_by a (x^m) (y^m) | (n : β„•) := h.pow_right n | -[1+n] := (h.pow_right n.succ).inv_right variables {a b x y x' y' : R} @[simp] lemma cast_int_mul_right (h : semiconj_by a x y) (m : β„€) : semiconj_by a ((m : β„€) * x) (m * y) := semiconj_by.mul_right (int.commute_cast _ _) h @[simp] lemma cast_int_mul_left (h : semiconj_by a x y) (m : β„€) : semiconj_by ((m : R) * a) x y := semiconj_by.mul_left (int.cast_commute _ _) h @[simp] lemma cast_int_mul_cast_int_mul (h : semiconj_by a x y) (m n : β„€) : semiconj_by ((m : R) * a) (n * x) (n * y) := (h.cast_int_mul_left m).cast_int_mul_right n end semiconj_by namespace commute section variables [semiring R] {a b : R} @[simp] theorem cast_nat_mul_right (h : commute a b) (n : β„•) : commute a ((n : R) * b) := h.cast_nat_mul_right n @[simp] theorem cast_nat_mul_left (h : commute a b) (n : β„•) : commute ((n : R) * a) b := h.cast_nat_mul_left n @[simp] theorem cast_nat_mul_cast_nat_mul (h : commute a b) (m n : β„•) : commute ((m : R) * a) (n * b) := h.cast_nat_mul_cast_nat_mul m n @[simp] theorem self_cast_nat_mul (n : β„•) : commute a (n * a) := (commute.refl a).cast_nat_mul_right n @[simp] theorem cast_nat_mul_self (n : β„•) : commute ((n : R) * a) a := (commute.refl a).cast_nat_mul_left n @[simp] theorem self_cast_nat_mul_cast_nat_mul (m n : β„•) : commute ((m : R) * a) (n * a) := (commute.refl a).cast_nat_mul_cast_nat_mul m n end variables [monoid M] [group G] [ring R] @[simp] lemma units_gpow_right {a : M} {u : units M} (h : commute a u) (m : β„€) : commute a (↑(u^m)) := h.units_gpow_right m @[simp] lemma units_gpow_left {u : units M} {a : M} (h : commute ↑u a) (m : β„€) : commute (↑(u^m)) a := (h.symm.units_gpow_right m).symm section variables {a b : G} @[simp] lemma gpow_right (h : commute a b) (m : β„€) : commute a (b^m) := h.gpow_right m @[simp] lemma gpow_left (h : commute a b) (m : β„€) : commute (a^m) b := (h.symm.gpow_right m).symm lemma gpow_gpow (h : commute a b) (m n : β„€) : commute (a^m) (b^n) := (h.gpow_left m).gpow_right n variables (a) (m n : β„•) @[simp] theorem self_gpow : commute a (a ^ n) := (commute.refl a).gpow_right n @[simp] theorem gpow_self : commute (a ^ n) a := (commute.refl a).gpow_left n @[simp] theorem gpow_gpow_self : commute (a ^ m) (a ^ n) := (commute.refl a).gpow_gpow m n end variables {a b : R} @[simp] lemma cast_int_mul_right (h : commute a b) (m : β„€) : commute a (m * b) := h.cast_int_mul_right m @[simp] lemma cast_int_mul_left (h : commute a b) (m : β„€) : commute ((m : R) * a) b := h.cast_int_mul_left m lemma cast_int_mul_cast_int_mul (h : commute a b) (m n : β„€) : commute ((m : R) * a) (n * b) := h.cast_int_mul_cast_int_mul m n variables (a) (m n : β„€) @[simp] theorem self_cast_int_mul : commute a (n * a) := (commute.refl a).cast_int_mul_right n @[simp] theorem cast_int_mul_self : commute ((n : R) * a) a := (commute.refl a).cast_int_mul_left n theorem self_cast_int_mul_cast_int_mul : commute ((m : R) * a) (n * a) := (commute.refl a).cast_int_mul_cast_int_mul m n end commute namespace units variables [monoid M] lemma conj_pow (u : units M) (x : M) (n : β„•) : (↑u * x * ↑(u⁻¹))^n = u * x^n * ↑(u⁻¹) := (divp_eq_iff_mul_eq.2 ((u.mk_semiconj_by x).pow_right n).eq.symm).symm lemma conj_pow' (u : units M) (x : M) (n : β„•) : (↑(u⁻¹) * x * u)^n = ↑(u⁻¹) * x^n * u:= (u⁻¹).conj_pow x n end units
d272b6e31ace33b561f0ef2019ccfc450954d17e
367134ba5a65885e863bdc4507601606690974c1
/src/data/polynomial/degree/definitions.lean
b42d334b435c352981191642fb5dd1e240c2c770
[ "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
38,277
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes HΓΆlzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.coeff import data.nat.with_bot /-! # Theory of univariate polynomials The definitions include `degree`, `monic`, `leading_coeff` Results include - `degree_mul` : The degree of the product is the sum of degrees - `leading_coeff_add_of_degree_eq` and `leading_coeff_add_of_degree_lt` : The leading_coefficient of a sum is determined by the leading coefficients and degrees -/ noncomputable theory local attribute [instance, priority 100] classical.prop_decidable open finsupp finset open_locale big_operators namespace polynomial universes u v variables {R : Type u} {S : Type v} {a b : R} {n m : β„•} section semiring variables [semiring R] {p q r : polynomial R} /-- `degree p` is the degree of the polynomial `p`, i.e. the largest `X`-exponent in `p`. `degree p = some n` when `p β‰  0` and `n` is the highest power of `X` that appears in `p`, otherwise `degree 0 = βŠ₯`. -/ def degree (p : polynomial R) : with_bot β„• := p.support.sup some lemma degree_lt_wf : well_founded (Ξ»p q : polynomial R, degree p < degree q) := inv_image.wf degree (with_bot.well_founded_lt nat.lt_wf) instance : has_well_founded (polynomial R) := ⟨_, degree_lt_wf⟩ /-- `nat_degree p` forces `degree p` to β„•, by defining nat_degree 0 = 0. -/ def nat_degree (p : polynomial R) : β„• := (degree p).get_or_else 0 /-- `leading_coeff p` gives the coefficient of the highest power of `X` in `p`-/ def leading_coeff (p : polynomial R) : R := coeff p (nat_degree p) /-- a polynomial is `monic` if its leading coefficient is 1 -/ def monic (p : polynomial R) := leading_coeff p = (1 : R) @[nontriviality] lemma monic_of_subsingleton [subsingleton R] (p : polynomial R) : monic p := subsingleton.elim _ _ lemma monic.def : monic p ↔ leading_coeff p = 1 := iff.rfl instance monic.decidable [decidable_eq R] : decidable (monic p) := by unfold monic; apply_instance @[simp] lemma monic.leading_coeff {p : polynomial R} (hp : p.monic) : leading_coeff p = 1 := hp lemma monic.coeff_nat_degree {p : polynomial R} (hp : p.monic) : p.coeff p.nat_degree = 1 := hp @[simp] lemma degree_zero : degree (0 : polynomial R) = βŠ₯ := rfl @[simp] lemma nat_degree_zero : nat_degree (0 : polynomial R) = 0 := rfl @[simp] lemma coeff_nat_degree : coeff p (nat_degree p) = leading_coeff p := rfl lemma degree_eq_bot : degree p = βŠ₯ ↔ p = 0 := ⟨λ h, by rw [degree, ← max_eq_sup_with_bot] at h; exact support_eq_empty.1 (max_eq_none.1 h), Ξ» h, h.symm β–Έ rfl⟩ @[nontriviality] lemma degree_of_subsingleton [subsingleton R] : degree p = βŠ₯ := by rw [subsingleton.elim p 0, degree_zero] @[nontriviality] lemma nat_degree_of_subsingleton [subsingleton R] : nat_degree p = 0 := by rw [subsingleton.elim p 0, nat_degree_zero] lemma degree_eq_nat_degree (hp : p β‰  0) : degree p = (nat_degree p : with_bot β„•) := let ⟨n, hn⟩ := not_forall.1 (mt option.eq_none_iff_forall_not_mem.2 (mt degree_eq_bot.1 hp)) in have hn : degree p = some n := not_not.1 hn, by rw [nat_degree, hn]; refl lemma degree_eq_iff_nat_degree_eq {p : polynomial R} {n : β„•} (hp : p β‰  0) : p.degree = n ↔ p.nat_degree = n := by rw [degree_eq_nat_degree hp, with_bot.coe_eq_coe] lemma degree_eq_iff_nat_degree_eq_of_pos {p : polynomial R} {n : β„•} (hn : 0 < n) : p.degree = n ↔ p.nat_degree = n := begin split, { intro H, rwa ← degree_eq_iff_nat_degree_eq, rintro rfl, rw degree_zero at H, exact option.no_confusion H }, { intro H, rwa degree_eq_iff_nat_degree_eq, rintro rfl, rw nat_degree_zero at H, rw H at hn, exact lt_irrefl _ hn } end lemma nat_degree_eq_of_degree_eq_some {p : polynomial R} {n : β„•} (h : degree p = n) : nat_degree p = n := have hp0 : p β‰  0, from Ξ» hp0, by rw hp0 at h; exact option.no_confusion h, option.some_inj.1 $ show (nat_degree p : with_bot β„•) = n, by rwa [← degree_eq_nat_degree hp0] @[simp] lemma degree_le_nat_degree : degree p ≀ nat_degree p := with_bot.gi_get_or_else_bot.gc.le_u_l _ lemma nat_degree_eq_of_degree_eq [semiring S] {q : polynomial S} (h : degree p = degree q) : nat_degree p = nat_degree q := by unfold nat_degree; rw h lemma le_degree_of_ne_zero (h : coeff p n β‰  0) : (n : with_bot β„•) ≀ degree p := show @has_le.le (with_bot β„•) _ (some n : with_bot β„•) (p.support.sup some : with_bot β„•), from finset.le_sup (finsupp.mem_support_iff.2 h) lemma le_nat_degree_of_ne_zero (h : coeff p n β‰  0) : n ≀ nat_degree p := begin rw [← with_bot.coe_le_coe, ← degree_eq_nat_degree], exact le_degree_of_ne_zero h, { assume h, subst h, exact h rfl } end lemma le_nat_degree_of_mem_supp (a : β„•) : a ∈ p.support β†’ a ≀ nat_degree p:= le_nat_degree_of_ne_zero ∘ mem_support_iff.mp lemma supp_subset_range (h : nat_degree p < m) : p.support βŠ† finset.range m := Ξ» n hn, mem_range.2 $ (le_nat_degree_of_mem_supp _ hn).trans_lt h lemma supp_subset_range_nat_degree_succ : p.support βŠ† finset.range (nat_degree p + 1) := supp_subset_range (nat.lt_succ_self _) lemma degree_le_degree (h : coeff q (nat_degree p) β‰  0) : degree p ≀ degree q := begin by_cases hp : p = 0, { rw hp, exact bot_le }, { rw degree_eq_nat_degree hp, exact le_degree_of_ne_zero h } end lemma degree_ne_of_nat_degree_ne {n : β„•} : p.nat_degree β‰  n β†’ degree p β‰  n := mt $ Ξ» h, by rw [nat_degree, h, option.get_or_else_coe] theorem nat_degree_le_iff_degree_le {n : β„•} : nat_degree p ≀ n ↔ degree p ≀ n := with_bot.get_or_else_bot_le_iff alias polynomial.nat_degree_le_iff_degree_le ↔ . . lemma nat_degree_le_nat_degree (hpq : p.degree ≀ q.degree) : p.nat_degree ≀ q.nat_degree := with_bot.gi_get_or_else_bot.gc.monotone_l hpq @[simp] lemma degree_C (ha : a β‰  0) : degree (C a) = (0 : with_bot β„•) := show sup (ite (a = 0) βˆ… {0}) some = 0, by rw if_neg ha; refl lemma degree_C_le : degree (C a) ≀ (0 : with_bot β„•) := by by_cases h : a = 0; [rw [h, C_0], rw [degree_C h]]; [exact bot_le, exact le_refl _] lemma degree_one_le : degree (1 : polynomial R) ≀ (0 : with_bot β„•) := by rw [← C_1]; exact degree_C_le @[simp] lemma nat_degree_C (a : R) : nat_degree (C a) = 0 := begin by_cases ha : a = 0, { have : C a = 0, { rw [ha, C_0] }, rw [nat_degree, degree_eq_bot.2 this], refl }, { rw [nat_degree, degree_C ha], refl } end @[simp] lemma nat_degree_one : nat_degree (1 : polynomial R) = 0 := nat_degree_C 1 @[simp] lemma nat_degree_nat_cast (n : β„•) : nat_degree (n : polynomial R) = 0 := by simp only [←C_eq_nat_cast, nat_degree_C] @[simp] lemma degree_monomial (n : β„•) (ha : a β‰  0) : degree (monomial n a) = n := by rw [degree, support_monomial _ _ ha]; refl @[simp] lemma degree_C_mul_X_pow (n : β„•) (ha : a β‰  0) : degree (C a * X ^ n) = n := by rw [← single_eq_C_mul_X, degree_monomial n ha] lemma degree_monomial_le (n : β„•) (a : R) : degree (monomial n a) ≀ n := if h : a = 0 then by rw [h, (monomial n).map_zero]; exact bot_le else le_of_eq (degree_monomial n h) lemma degree_C_mul_X_pow_le (n : β„•) (a : R) : degree (C a * X ^ n) ≀ n := by { rw C_mul_X_pow_eq_monomial, apply degree_monomial_le } @[simp] lemma nat_degree_C_mul_X_pow (n : β„•) (a : R) (ha : a β‰  0) : nat_degree (C a * X ^ n) = n := nat_degree_eq_of_degree_eq_some (degree_C_mul_X_pow n ha) @[simp] lemma nat_degree_C_mul_X (a : R) (ha : a β‰  0) : nat_degree (C a * X) = 1 := by simpa only [pow_one] using nat_degree_C_mul_X_pow 1 a ha @[simp] lemma nat_degree_monomial (i : β„•) (r : R) (hr : r β‰  0) : nat_degree (monomial i r) = i := by rw [← C_mul_X_pow_eq_monomial, nat_degree_C_mul_X_pow i r hr] lemma coeff_eq_zero_of_degree_lt (h : degree p < n) : coeff p n = 0 := not_not.1 (mt le_degree_of_ne_zero (not_le_of_gt h)) lemma coeff_eq_zero_of_nat_degree_lt {p : polynomial R} {n : β„•} (h : p.nat_degree < n) : p.coeff n = 0 := begin apply coeff_eq_zero_of_degree_lt, by_cases hp : p = 0, { subst hp, exact with_bot.bot_lt_coe n }, { rwa [degree_eq_nat_degree hp, with_bot.coe_lt_coe] } end @[simp] lemma coeff_nat_degree_succ_eq_zero {p : polynomial R} : p.coeff (p.nat_degree + 1) = 0 := coeff_eq_zero_of_nat_degree_lt (lt_add_one _) -- We need the explicit `decidable` argument here because an exotic one shows up in a moment! lemma ite_le_nat_degree_coeff (p : polynomial R) (n : β„•) (I : decidable (n < 1 + nat_degree p)) : @ite _ (n < 1 + nat_degree p) I (coeff p n) 0 = coeff p n := begin split_ifs, { refl }, { exact (coeff_eq_zero_of_nat_degree_lt (not_le.1 (Ξ» w, h (nat.lt_one_add_iff.2 w)))).symm, } end lemma as_sum_support (p : polynomial R) : p = βˆ‘ i in p.support, monomial i (p.coeff i) := p.sum_single.symm lemma as_sum_support_C_mul_X_pow (p : polynomial R) : p = βˆ‘ i in p.support, C (p.coeff i) * X^i := trans p.as_sum_support $ by simp only [C_mul_X_pow_eq_monomial] /-- We can reexpress a sum over `p.support` as a sum over `range n`, for any `n` satisfying `p.nat_degree < n`. -/ lemma sum_over_range' [add_comm_monoid S] (p : polynomial R) {f : β„• β†’ R β†’ S} (h : βˆ€ n, f n 0 = 0) (n : β„•) (w : p.nat_degree < n) : p.sum f = βˆ‘ (a : β„•) in range n, f a (coeff p a) := finsupp.sum_of_support_subset _ (supp_subset_range w) _ $ Ξ» n hn, h n /-- We can reexpress a sum over `p.support` as a sum over `range (p.nat_degree + 1)`. -/ lemma sum_over_range [add_comm_monoid S] (p : polynomial R) {f : β„• β†’ R β†’ S} (h : βˆ€ n, f n 0 = 0) : p.sum f = βˆ‘ (a : β„•) in range (p.nat_degree + 1), f a (coeff p a) := sum_over_range' p h (p.nat_degree + 1) (lt_add_one _) lemma as_sum_range' (p : polynomial R) (n : β„•) (w : p.nat_degree < n) : p = βˆ‘ i in range n, monomial i (coeff p i) := p.sum_single.symm.trans $ p.sum_over_range' (Ξ» n, single_zero) _ w lemma as_sum_range (p : polynomial R) : p = βˆ‘ i in range (p.nat_degree + 1), monomial i (coeff p i) := p.sum_single.symm.trans $ p.sum_over_range $ Ξ» n, single_zero lemma as_sum_range_C_mul_X_pow (p : polynomial R) : p = βˆ‘ i in range (p.nat_degree + 1), C (coeff p i) * X ^ i := p.as_sum_range.trans $ by simp only [C_mul_X_pow_eq_monomial] lemma coeff_ne_zero_of_eq_degree (hn : degree p = n) : coeff p n β‰  0 := Ξ» h, mem_support_iff.mp (mem_of_max hn) h lemma eq_X_add_C_of_degree_le_one (h : degree p ≀ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := ext (Ξ» n, nat.cases_on n (by simp) (Ξ» n, nat.cases_on n (by simp [coeff_C]) (Ξ» m, have degree p < m.succ.succ, from lt_of_le_of_lt h dec_trivial, by simp [coeff_eq_zero_of_degree_lt this, coeff_C, nat.succ_ne_zero, coeff_X, nat.succ_inj', @eq_comm β„• 0]))) lemma eq_X_add_C_of_degree_eq_one (h : degree p = 1) : p = C (p.leading_coeff) * X + C (p.coeff 0) := (eq_X_add_C_of_degree_le_one (show degree p ≀ 1, from h β–Έ le_refl _)).trans (by simp [leading_coeff, nat_degree_eq_of_degree_eq_some h]) lemma eq_X_add_C_of_nat_degree_le_one (h : nat_degree p ≀ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := eq_X_add_C_of_degree_le_one $ degree_le_of_nat_degree_le h lemma exists_eq_X_add_C_of_nat_degree_le_one (h : nat_degree p ≀ 1) : βˆƒ a b, p = C a * X + C b := ⟨p.coeff 1, p.coeff 0, eq_X_add_C_of_nat_degree_le_one h⟩ theorem degree_X_pow_le (n : β„•) : degree (X^n : polynomial R) ≀ n := by simpa only [C_1, one_mul] using degree_C_mul_X_pow_le n (1:R) theorem degree_X_le : degree (X : polynomial R) ≀ 1 := degree_monomial_le _ _ lemma nat_degree_X_le : (X : polynomial R).nat_degree ≀ 1 := nat_degree_le_of_degree_le degree_X_le lemma support_C_mul_X_pow (c : R) (n : β„•) : (C c * X ^ n).support βŠ† singleton n := begin rw [C_mul_X_pow_eq_monomial], exact support_single_subset end lemma mem_support_C_mul_X_pow {n a : β„•} {c : R} (h : a ∈ (C c * X ^ n).support) : a = n := mem_singleton.1 $ support_C_mul_X_pow _ _ h lemma card_support_C_mul_X_pow_le_one {c : R} {n : β„•} : (C c * X ^ n).support.card ≀ 1 := begin rw ← card_singleton n, apply card_le_of_subset (support_C_mul_X_pow c n), end lemma card_supp_le_succ_nat_degree (p : polynomial R) : p.support.card ≀ p.nat_degree + 1 := begin rw ← finset.card_range (p.nat_degree + 1), exact finset.card_le_of_subset supp_subset_range_nat_degree_succ, end lemma le_degree_of_mem_supp (a : β„•) : a ∈ p.support β†’ ↑a ≀ degree p := le_degree_of_ne_zero ∘ mem_support_iff.mp lemma nonempty_support_iff : p.support.nonempty ↔ p β‰  0 := by rw [ne.def, nonempty_iff_ne_empty, ne.def, ← support_eq_empty] lemma support_C_mul_X_pow_nonzero {c : R} {n : β„•} (h : c β‰  0) : (C c * X ^ n).support = singleton n := begin rw [C_mul_X_pow_eq_monomial], exact support_single_ne_zero h end end semiring section nonzero_semiring variables [semiring R] [nontrivial R] {p q : polynomial R} @[simp] lemma degree_one : degree (1 : polynomial R) = (0 : with_bot β„•) := degree_C (show (1 : R) β‰  0, from zero_ne_one.symm) @[simp] lemma degree_X : degree (X : polynomial R) = 1 := degree_monomial _ one_ne_zero @[simp] lemma nat_degree_X : (X : polynomial R).nat_degree = 1 := nat_degree_eq_of_degree_eq_some degree_X end nonzero_semiring section ring variables [ring R] lemma coeff_mul_X_sub_C {p : polynomial R} {r : R} {a : β„•} : coeff (p * (X - C r)) (a + 1) = coeff p a - coeff p (a + 1) * r := by simp [mul_sub] lemma C_eq_int_cast (n : β„€) : C (n : R) = n := (C : R β†’+* _).map_int_cast n @[simp] lemma degree_neg (p : polynomial R) : degree (-p) = degree p := by unfold degree; rw support_neg @[simp] lemma nat_degree_neg (p : polynomial R) : nat_degree (-p) = nat_degree p := by simp [nat_degree] @[simp] lemma nat_degree_int_cast (n : β„€) : nat_degree (n : polynomial R) = 0 := by simp only [←C_eq_int_cast, nat_degree_C] end ring section semiring variables [semiring R] /-- The second-highest coefficient, or 0 for constants -/ def next_coeff (p : polynomial R) : R := if p.nat_degree = 0 then 0 else p.coeff (p.nat_degree - 1) @[simp] lemma next_coeff_C_eq_zero (c : R) : next_coeff (C c) = 0 := by { rw next_coeff, simp } lemma next_coeff_of_pos_nat_degree (p : polynomial R) (hp : 0 < p.nat_degree) : next_coeff p = p.coeff (p.nat_degree - 1) := by { rw [next_coeff, if_neg], contrapose! hp, simpa } end semiring section semiring variables [semiring R] {p q : polynomial R} {ΞΉ : Type*} lemma coeff_nat_degree_eq_zero_of_degree_lt (h : degree p < degree q) : coeff p (nat_degree q) = 0 := coeff_eq_zero_of_degree_lt (lt_of_lt_of_le h degree_le_nat_degree) lemma ne_zero_of_degree_gt {n : with_bot β„•} (h : n < degree p) : p β‰  0 := mt degree_eq_bot.2 (ne.symm (ne_of_lt (lt_of_le_of_lt bot_le h))) lemma ne_zero_of_nat_degree_gt {n : β„•} (h : n < nat_degree p) : p β‰  0 := Ξ» H, by simpa [H, nat.not_lt_zero] using h lemma degree_lt_degree (h : nat_degree p < nat_degree q) : degree p < degree q := begin by_cases hp : p = 0, { simp [hp], rw bot_lt_iff_ne_bot, intro hq, simpa [hp, degree_eq_bot.mp hq, lt_irrefl] using h }, { rw [degree_eq_nat_degree hp, degree_eq_nat_degree $ ne_zero_of_nat_degree_gt h], exact_mod_cast h } end lemma nat_degree_lt_nat_degree_iff (hp : p β‰  0) : nat_degree p < nat_degree q ↔ degree p < degree q := ⟨degree_lt_degree, begin intro h, have hq : q β‰  0 := ne_zero_of_degree_gt h, rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq] at h, exact_mod_cast h end⟩ lemma eq_C_of_degree_le_zero (h : degree p ≀ 0) : p = C (coeff p 0) := begin ext (_|n), { simp }, rw [coeff_C, if_neg (nat.succ_ne_zero _), coeff_eq_zero_of_degree_lt], exact h.trans_lt (with_bot.some_lt_some.2 n.succ_pos), end lemma eq_C_of_degree_eq_zero (h : degree p = 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero (h β–Έ le_refl _) lemma degree_le_zero_iff : degree p ≀ 0 ↔ p = C (coeff p 0) := ⟨eq_C_of_degree_le_zero, Ξ» h, h.symm β–Έ degree_C_le⟩ lemma degree_add_le (p q : polynomial R) : degree (p + q) ≀ max (degree p) (degree q) := calc degree (p + q) = ((p + q).support).sup some : rfl ... ≀ (p.support βˆͺ q.support).sup some : by convert sup_mono support_add ... = p.support.sup some βŠ” q.support.sup some : by convert sup_union ... = _ : with_bot.sup_eq_max _ _ @[simp] lemma leading_coeff_zero : leading_coeff (0 : polynomial R) = 0 := rfl @[simp] lemma leading_coeff_eq_zero : leading_coeff p = 0 ↔ p = 0 := ⟨λ h, by_contradiction $ Ξ» hp, mt mem_support_iff.1 (not_not.2 h) (mem_of_max (degree_eq_nat_degree hp)), Ξ» h, h.symm β–Έ leading_coeff_zero⟩ lemma leading_coeff_ne_zero : leading_coeff p β‰  0 ↔ p β‰  0 := by rw [ne.def, leading_coeff_eq_zero] lemma leading_coeff_eq_zero_iff_deg_eq_bot : leading_coeff p = 0 ↔ degree p = βŠ₯ := by rw [leading_coeff_eq_zero, degree_eq_bot] lemma nat_degree_mem_support_of_nonzero (H : p β‰  0) : p.nat_degree ∈ p.support := (p.mem_support_to_fun p.nat_degree).mpr ((not_congr leading_coeff_eq_zero).mpr H) lemma nat_degree_eq_support_max' (h : p β‰  0) : p.nat_degree = p.support.max' (nonempty_support_iff.mpr h) := (le_max' _ _ $ nat_degree_mem_support_of_nonzero h).antisymm $ max'_le _ _ _ le_nat_degree_of_mem_supp lemma nat_degree_C_mul_X_pow_le (a : R) (n : β„•) : nat_degree (C a * X ^ n) ≀ n := nat_degree_le_iff_degree_le.2 $ degree_C_mul_X_pow_le _ _ lemma degree_add_eq_left_of_degree_lt (h : degree q < degree p) : degree (p + q) = degree p := le_antisymm (max_eq_left_of_lt h β–Έ degree_add_le _ _) $ degree_le_degree $ begin rw [coeff_add, coeff_nat_degree_eq_zero_of_degree_lt h, add_zero], exact mt leading_coeff_eq_zero.1 (ne_zero_of_degree_gt h) end lemma degree_add_eq_right_of_degree_lt (h : degree p < degree q) : degree (p + q) = degree q := by rw [add_comm, degree_add_eq_left_of_degree_lt h] lemma degree_add_C (hp : 0 < degree p) : degree (p + C a) = degree p := add_comm (C a) p β–Έ degree_add_eq_right_of_degree_lt $ lt_of_le_of_lt degree_C_le hp lemma degree_add_eq_of_leading_coeff_add_ne_zero (h : leading_coeff p + leading_coeff q β‰  0) : degree (p + q) = max p.degree q.degree := le_antisymm (degree_add_le _ _) $ match lt_trichotomy (degree p) (degree q) with | or.inl hlt := by rw [degree_add_eq_right_of_degree_lt hlt, max_eq_right_of_lt hlt]; exact le_refl _ | or.inr (or.inl heq) := le_of_not_gt $ assume hlt : max (degree p) (degree q) > degree (p + q), h $ show leading_coeff p + leading_coeff q = 0, begin rw [heq, max_self] at hlt, rw [leading_coeff, leading_coeff, nat_degree_eq_of_degree_eq heq, ← coeff_add], exact coeff_nat_degree_eq_zero_of_degree_lt hlt end | or.inr (or.inr hlt) := by rw [degree_add_eq_left_of_degree_lt hlt, max_eq_left_of_lt hlt]; exact le_refl _ end lemma degree_erase_le (p : polynomial R) (n : β„•) : degree (p.erase n) ≀ degree p := by convert sup_mono (erase_subset _ _) lemma degree_erase_lt (hp : p β‰  0) : degree (p.erase (nat_degree p)) < degree p := lt_of_le_of_ne (degree_erase_le _ _) $ (degree_eq_nat_degree hp).symm β–Έ (by convert Ξ» h, not_mem_erase _ _ (mem_of_max h)) lemma degree_sum_le (s : finset ΞΉ) (f : ΞΉ β†’ polynomial R) : degree (βˆ‘ i in s, f i) ≀ s.sup (Ξ» b, degree (f b)) := finset.induction_on s (by simp only [sum_empty, sup_empty, degree_zero, le_refl]) $ assume a s has ih, calc degree (βˆ‘ i in insert a s, f i) ≀ max (degree (f a)) (degree (βˆ‘ i in s, f i)) : by rw sum_insert has; exact degree_add_le _ _ ... ≀ _ : by rw [sup_insert, with_bot.sup_eq_max]; exact max_le_max (le_refl _) ih lemma degree_mul_le (p q : polynomial R) : degree (p * q) ≀ degree p + degree q := calc degree (p * q) ≀ (p.support).sup (Ξ»i, degree (sum q (Ξ»j a, C (coeff p i * a) * X ^ (i + j)))) : by simp only [single_eq_C_mul_X.symm]; exact degree_sum_le _ _ ... ≀ p.support.sup (Ξ»i, q.support.sup (Ξ»j, degree (C (coeff p i * coeff q j) * X ^ (i + j)))) : finset.sup_mono_fun (assume i hi, degree_sum_le _ _) ... ≀ degree p + degree q : begin refine finset.sup_le (Ξ» a ha, finset.sup_le (Ξ» b hb, le_trans (degree_C_mul_X_pow_le _ _) _)), rw [with_bot.coe_add], rw mem_support_iff at ha hb, exact add_le_add (le_degree_of_ne_zero ha) (le_degree_of_ne_zero hb) end lemma degree_pow_le (p : polynomial R) : βˆ€ n, degree (p ^ n) ≀ n β€’β„• (degree p) | 0 := by rw [pow_zero, zero_nsmul]; exact degree_one_le | (n+1) := calc degree (p ^ (n + 1)) ≀ degree p + degree (p ^ n) : by rw pow_succ; exact degree_mul_le _ _ ... ≀ _ : by rw succ_nsmul; exact add_le_add (le_refl _) (degree_pow_le _) @[simp] lemma leading_coeff_monomial (a : R) (n : β„•) : leading_coeff (monomial n a) = a := begin by_cases ha : a = 0, { simp only [ha, (monomial n).map_zero, leading_coeff_zero] }, { rw [leading_coeff, nat_degree_monomial _ _ ha], exact @finsupp.single_eq_same _ _ _ n a } end lemma leading_coeff_C_mul_X_pow (a : R) (n : β„•) : leading_coeff (C a * X ^ n) = a := by rw [C_mul_X_pow_eq_monomial, leading_coeff_monomial] @[simp] lemma leading_coeff_C (a : R) : leading_coeff (C a) = a := leading_coeff_monomial a 0 @[simp] lemma leading_coeff_X_pow (n : β„•) : leading_coeff ((X : polynomial R) ^ n) = 1 := by simpa only [C_1, one_mul] using leading_coeff_C_mul_X_pow (1 : R) n @[simp] lemma leading_coeff_X : leading_coeff (X : polynomial R) = 1 := by simpa only [pow_one] using @leading_coeff_X_pow R _ 1 @[simp] lemma monic_X_pow (n : β„•) : monic (X ^ n : polynomial R) := leading_coeff_X_pow n @[simp] lemma monic_X : monic (X : polynomial R) := leading_coeff_X @[simp] lemma leading_coeff_one : leading_coeff (1 : polynomial R) = 1 := leading_coeff_C 1 @[simp] lemma monic_one : monic (1 : polynomial R) := leading_coeff_C _ lemma monic.ne_zero {R : Type*} [semiring R] [nontrivial R] {p : polynomial R} (hp : p.monic) : p β‰  0 := by { rintro rfl, simpa [monic] using hp } lemma monic.ne_zero_of_ne (h : (0:R) β‰  1) {p : polynomial R} (hp : p.monic) : p β‰  0 := by { nontriviality R, exact hp.ne_zero } lemma monic.ne_zero_of_polynomial_ne {r} (hp : monic p) (hne : q β‰  r) : p β‰  0 := by { haveI := nontrivial.of_polynomial_ne hne, exact hp.ne_zero } lemma leading_coeff_add_of_degree_lt (h : degree p < degree q) : leading_coeff (p + q) = leading_coeff q := have coeff p (nat_degree q) = 0, from coeff_nat_degree_eq_zero_of_degree_lt h, by simp only [leading_coeff, nat_degree_eq_of_degree_eq (degree_add_eq_right_of_degree_lt h), this, coeff_add, zero_add] lemma leading_coeff_add_of_degree_eq (h : degree p = degree q) (hlc : leading_coeff p + leading_coeff q β‰  0) : leading_coeff (p + q) = leading_coeff p + leading_coeff q := have nat_degree (p + q) = nat_degree p, by apply nat_degree_eq_of_degree_eq; rw [degree_add_eq_of_leading_coeff_add_ne_zero hlc, h, max_self], by simp only [leading_coeff, this, nat_degree_eq_of_degree_eq h, coeff_add] @[simp] lemma coeff_mul_degree_add_degree (p q : polynomial R) : coeff (p * q) (nat_degree p + nat_degree q) = leading_coeff p * leading_coeff q := calc coeff (p * q) (nat_degree p + nat_degree q) = βˆ‘ x in nat.antidiagonal (nat_degree p + nat_degree q), coeff p x.1 * coeff q x.2 : coeff_mul _ _ _ ... = coeff p (nat_degree p) * coeff q (nat_degree q) : begin refine finset.sum_eq_single (nat_degree p, nat_degree q) _ _, { rintro ⟨i,j⟩ h₁ hβ‚‚, rw nat.mem_antidiagonal at h₁, by_cases H : nat_degree p < i, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 H)), zero_mul] }, { rw not_lt_iff_eq_or_lt at H, cases H, { subst H, rw add_left_cancel_iff at h₁, dsimp at h₁, subst h₁, exfalso, exact hβ‚‚ rfl }, { suffices : nat_degree q < j, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 this)), mul_zero] }, { by_contra H', rw not_lt at H', exact ne_of_lt (nat.lt_of_lt_of_le (nat.add_lt_add_right H j) (nat.add_le_add_left H' _)) h₁ } } } }, { intro H, exfalso, apply H, rw nat.mem_antidiagonal } end lemma degree_mul' (h : leading_coeff p * leading_coeff q β‰  0) : degree (p * q) = degree p + degree q := have hp : p β‰  0 := by refine mt _ h; exact Ξ» hp, by rw [hp, leading_coeff_zero, zero_mul], have hq : q β‰  0 := by refine mt _ h; exact Ξ» hq, by rw [hq, leading_coeff_zero, mul_zero], le_antisymm (degree_mul_le _ _) begin rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq], refine le_degree_of_ne_zero _, rwa coeff_mul_degree_add_degree end lemma degree_mul_monic (hq : monic q) : degree (p * q) = degree p + degree q := if hp : p = 0 then by simp [hp] else degree_mul' $ by rwa [hq.leading_coeff, mul_one, ne.def, leading_coeff_eq_zero] lemma nat_degree_mul' (h : leading_coeff p * leading_coeff q β‰  0) : nat_degree (p * q) = nat_degree p + nat_degree q := have hp : p β‰  0 := mt leading_coeff_eq_zero.2 (Ξ» h₁, h $ by rw [h₁, zero_mul]), have hq : q β‰  0 := mt leading_coeff_eq_zero.2 (Ξ» h₁, h $ by rw [h₁, mul_zero]), nat_degree_eq_of_degree_eq_some $ by rw [degree_mul' h, with_bot.coe_add, degree_eq_nat_degree hp, degree_eq_nat_degree hq] lemma leading_coeff_mul' (h : leading_coeff p * leading_coeff q β‰  0) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin unfold leading_coeff, rw [nat_degree_mul' h, coeff_mul_degree_add_degree], refl end lemma leading_coeff_pow' : leading_coeff p ^ n β‰  0 β†’ leading_coeff (p ^ n) = leading_coeff p ^ n := nat.rec_on n (by simp) $ Ξ» n ih h, have h₁ : leading_coeff p ^ n β‰  0 := Ξ» h₁, h $ by rw [pow_succ, h₁, mul_zero], have hβ‚‚ : leading_coeff p * leading_coeff (p ^ n) β‰  0 := by rwa [pow_succ, ← ih h₁] at h, by rw [pow_succ, pow_succ, leading_coeff_mul' hβ‚‚, ih h₁] lemma degree_pow' : βˆ€ {n}, leading_coeff p ^ n β‰  0 β†’ degree (p ^ n) = n β€’β„• (degree p) | 0 := Ξ» h, by rw [pow_zero, ← C_1] at *; rw [degree_C h, zero_nsmul] | (n+1) := Ξ» h, have h₁ : leading_coeff p ^ n β‰  0 := Ξ» h₁, h $ by rw [pow_succ, h₁, mul_zero], have hβ‚‚ : leading_coeff p * leading_coeff (p ^ n) β‰  0 := by rwa [pow_succ, ← leading_coeff_pow' h₁] at h, by rw [pow_succ, degree_mul' hβ‚‚, succ_nsmul, degree_pow' h₁] lemma nat_degree_pow' {n : β„•} (h : leading_coeff p ^ n β‰  0) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp * else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else have hpn : p ^ n β‰  0, from Ξ» hpn0, have h1 : _ := h, by rw [← leading_coeff_pow' h1, hpn0, leading_coeff_zero] at h; exact h rfl, option.some_inj.1 $ show (nat_degree (p ^ n) : with_bot β„•) = (n * nat_degree p : β„•), by rw [← degree_eq_nat_degree hpn, degree_pow' h, degree_eq_nat_degree hp0, ← with_bot.coe_nsmul]; simp theorem leading_coeff_mul_monic {p q : polynomial R} (hq : monic q) : leading_coeff (p * q) = leading_coeff p := decidable.by_cases (Ξ» H : leading_coeff p = 0, by rw [H, leading_coeff_eq_zero.1 H, zero_mul, leading_coeff_zero]) (Ξ» H : leading_coeff p β‰  0, by rw [leading_coeff_mul', hq.leading_coeff, mul_one]; rwa [hq.leading_coeff, mul_one]) @[simp] theorem leading_coeff_mul_X_pow {p : polynomial R} {n : β„•} : leading_coeff (p * X ^ n) = leading_coeff p := leading_coeff_mul_monic (monic_X_pow n) @[simp] theorem leading_coeff_mul_X {p : polynomial R} : leading_coeff (p * X) = leading_coeff p := leading_coeff_mul_monic monic_X lemma nat_degree_mul_le {p q : polynomial R} : nat_degree (p * q) ≀ nat_degree p + nat_degree q := begin apply nat_degree_le_of_degree_le, apply le_trans (degree_mul_le p q), rw with_bot.coe_add, refine add_le_add _ _; apply degree_le_nat_degree, end lemma subsingleton_of_monic_zero (h : monic (0 : polynomial R)) : (βˆ€ p q : polynomial R, p = q) ∧ (βˆ€ a b : R, a = b) := by rw [monic.def, leading_coeff_zero] at h; exact ⟨λ p q, by rw [← mul_one p, ← mul_one q, ← C_1, ← h, C_0, mul_zero, mul_zero], Ξ» a b, by rw [← mul_one a, ← mul_one b, ← h, mul_zero, mul_zero]⟩ lemma zero_le_degree_iff {p : polynomial R} : 0 ≀ degree p ↔ p β‰  0 := by rw [ne.def, ← degree_eq_bot]; cases degree p; exact dec_trivial lemma degree_nonneg_iff_ne_zero : 0 ≀ degree p ↔ p β‰  0 := ⟨λ h0p hp0, absurd h0p (by rw [hp0, degree_zero]; exact dec_trivial), Ξ» hp0, le_of_not_gt (Ξ» h, by simp [gt, degree_eq_bot, *] at *)⟩ lemma nat_degree_eq_zero_iff_degree_le_zero : p.nat_degree = 0 ↔ p.degree ≀ 0 := by rw [← nonpos_iff_eq_zero, nat_degree_le_iff_degree_le, with_bot.coe_zero] theorem degree_le_iff_coeff_zero (f : polynomial R) (n : with_bot β„•) : degree f ≀ n ↔ βˆ€ m : β„•, n < m β†’ coeff f m = 0 := ⟨λ (H : finset.sup (f.support) some ≀ n) m (Hm : n < (m : with_bot β„•)), decidable.of_not_not $ Ξ» H4, have H1 : m βˆ‰ f.support, from Ξ» H2, not_lt_of_ge ((finset.sup_le_iff.1 H) m H2 : ((m : with_bot β„•) ≀ n)) Hm, H1 $ (finsupp.mem_support_to_fun f m).2 H4, Ξ» H, finset.sup_le $ Ξ» b Hb, decidable.of_not_not $ Ξ» Hn, (finsupp.mem_support_to_fun f b).1 Hb $ H b $ lt_of_not_ge Hn⟩ theorem degree_lt_iff_coeff_zero (f : polynomial R) (n : β„•) : degree f < n ↔ βˆ€ m : β„•, n ≀ m β†’ coeff f m = 0 := begin refine ⟨λ hf m hm, coeff_eq_zero_of_degree_lt (lt_of_lt_of_le hf (with_bot.coe_le_coe.2 hm)), _⟩, simp only [degree, finset.sup_lt_iff (with_bot.bot_lt_coe n), mem_support_iff, with_bot.some_eq_coe, with_bot.coe_lt_coe, ← @not_le β„•], exact Ξ» h m, mt (h m), end lemma degree_lt_degree_mul_X (hp : p β‰  0) : p.degree < (p * X).degree := by haveI := nontrivial.of_polynomial_ne hp; exact have leading_coeff p * leading_coeff X β‰  0, by simpa, by erw [degree_mul' this, degree_eq_nat_degree hp, degree_X, ← with_bot.coe_one, ← with_bot.coe_add, with_bot.coe_lt_coe]; exact nat.lt_succ_self _ lemma nat_degree_pos_iff_degree_pos : 0 < nat_degree p ↔ 0 < degree p := lt_iff_lt_of_le_iff_le nat_degree_le_iff_degree_le lemma eq_C_of_nat_degree_le_zero (h : nat_degree p ≀ 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero $ degree_le_of_nat_degree_le h lemma eq_C_of_nat_degree_eq_zero (h : nat_degree p = 0) : p = C (coeff p 0) := eq_C_of_nat_degree_le_zero h.le lemma ne_zero_of_coe_le_degree (hdeg : ↑n ≀ p.degree) : p β‰  0 := by rw ← degree_nonneg_iff_ne_zero; exact trans (by exact_mod_cast n.zero_le) hdeg lemma le_nat_degree_of_coe_le_degree (hdeg : ↑n ≀ p.degree) : n ≀ p.nat_degree := with_bot.coe_le_coe.mp ((degree_eq_nat_degree $ ne_zero_of_coe_le_degree hdeg) β–Έ hdeg) end semiring section nonzero_semiring variables [semiring R] [nontrivial R] {p q : polynomial R} @[simp] lemma degree_X_pow (n : β„•) : degree ((X : polynomial R) ^ n) = n := by rw [X_pow_eq_monomial, degree_monomial _ (@one_ne_zero R _ _)] @[simp] lemma nat_degree_X_pow (n : β„•) : nat_degree ((X : polynomial R) ^ n) = n := nat_degree_eq_of_degree_eq_some (degree_X_pow n) theorem not_is_unit_X : Β¬ is_unit (X : polynomial R) := Ξ» ⟨⟨_, g, hfg, hgf⟩, rfl⟩, @zero_ne_one R _ _ $ by { rw [← coeff_one_zero, ← hgf], simp } @[simp] lemma degree_mul_X : degree (p * X) = degree p + 1 := by simp [degree_mul_monic monic_X] @[simp] lemma degree_mul_X_pow : degree (p * X ^ n) = degree p + n := by simp [degree_mul_monic (monic_X_pow n)] end nonzero_semiring section ring variables [ring R] {p q : polynomial R} lemma degree_sub_le (p q : polynomial R) : degree (p - q) ≀ max (degree p) (degree q) := by simpa only [sub_eq_add_neg, degree_neg q] using degree_add_le p (-q) lemma degree_sub_lt (hd : degree p = degree q) (hp0 : p β‰  0) (hlc : leading_coeff p = leading_coeff q) : degree (p - q) < degree p := have hp : single (nat_degree p) (leading_coeff p) + p.erase (nat_degree p) = p := finsupp.single_add_erase _ _, have hq : single (nat_degree q) (leading_coeff q) + q.erase (nat_degree q) = q := finsupp.single_add_erase _ _, have hd' : nat_degree p = nat_degree q := by unfold nat_degree; rw hd, have hq0 : q β‰  0 := mt degree_eq_bot.2 (hd β–Έ mt degree_eq_bot.1 hp0), calc degree (p - q) = degree (erase (nat_degree q) p + -erase (nat_degree q) q) : by conv {to_lhs, rw [← hp, ← hq, hlc, hd', add_sub_add_left_eq_sub, sub_eq_add_neg]} ... ≀ max (degree (erase (nat_degree q) p)) (degree (erase (nat_degree q) q)) : degree_neg (erase (nat_degree q) q) β–Έ degree_add_le _ _ ... < degree p : max_lt_iff.2 ⟨hd' β–Έ degree_erase_lt hp0, hd.symm β–Έ degree_erase_lt hq0⟩ lemma nat_degree_X_sub_C_le {r : R} : (X - C r).nat_degree ≀ 1 := nat_degree_le_iff_degree_le.2 $ le_trans (degree_sub_le _ _) $ max_le degree_X_le $ le_trans degree_C_le $ with_bot.coe_le_coe.2 zero_le_one lemma degree_sum_fin_lt {n : β„•} (f : fin n β†’ R) : degree (βˆ‘ i : fin n, C (f i) * X ^ (i : β„•)) < n := begin haveI : is_commutative (with_bot β„•) max := ⟨max_comm⟩, haveI : is_associative (with_bot β„•) max := ⟨max_assoc⟩, calc (βˆ‘ i, C (f i) * X ^ (i : β„•)).degree ≀ finset.univ.fold (βŠ”) βŠ₯ (Ξ» i, (C (f i) * X ^ (i : β„•)).degree) : degree_sum_le _ _ ... = finset.univ.fold max βŠ₯ (Ξ» i, (C (f i) * X ^ (i : β„•)).degree) : (@finset.fold_hom _ _ _ (βŠ”) _ _ _ βŠ₯ finset.univ _ _ _ id (with_bot.sup_eq_max)).symm ... < n : (finset.fold_max_lt (n : with_bot β„•)).mpr ⟨with_bot.bot_lt_some _, _⟩, rintros ⟨i, hi⟩ -, calc (C (f ⟨i, hi⟩) * X ^ i).degree ≀ (C _).degree + (X ^ i).degree : degree_mul_le _ _ ... ≀ 0 + i : add_le_add degree_C_le (degree_X_pow_le i) ... = i : zero_add _ ... < n : with_bot.some_lt_some.mpr hi, end lemma degree_sub_eq_left_of_degree_lt (h : degree q < degree p) : degree (p - q) = degree p := by { rw ← degree_neg q at h, rw [sub_eq_add_neg, degree_add_eq_left_of_degree_lt h] } lemma degree_sub_eq_right_of_degree_lt (h : degree p < degree q) : degree (p - q) = degree q := by { rw ← degree_neg q at h, rw [sub_eq_add_neg, degree_add_eq_right_of_degree_lt h, degree_neg] } end ring section nonzero_ring variables [nontrivial R] [ring R] @[simp] lemma degree_X_sub_C (a : R) : degree (X - C a) = 1 := have degree (C a) < degree (X : polynomial R), from calc degree (C a) ≀ 0 : degree_C_le ... < 1 : with_bot.some_lt_some.mpr zero_lt_one ... = degree X : degree_X.symm, by rw [degree_sub_eq_left_of_degree_lt this, degree_X] @[simp] lemma degree_X_add_C (a : R) : degree (X + C a) = 1 := have degree (C a) < degree (X : polynomial R), from calc degree (C a) ≀ 0 : degree_C_le ... < 1 : with_bot.some_lt_some.mpr zero_lt_one ... = degree X : degree_X.symm, by rw [degree_add_eq_left_of_degree_lt this, degree_X] @[simp] lemma nat_degree_X_sub_C (x : R) : (X - C x).nat_degree = 1 := nat_degree_eq_of_degree_eq_some $ degree_X_sub_C x @[simp] lemma next_coeff_X_sub_C (c : R) : next_coeff (X - C c) = - c := by simp [next_coeff_of_pos_nat_degree] lemma degree_X_pow_sub_C {n : β„•} (hn : 0 < n) (a : R) : degree ((X : polynomial R) ^ n - C a) = n := have degree (C a) < degree ((X : polynomial R) ^ n), from calc degree (C a) ≀ 0 : degree_C_le ... < degree ((X : polynomial R) ^ n) : by rwa [degree_X_pow]; exact with_bot.coe_lt_coe.2 hn, by rw [degree_sub_eq_left_of_degree_lt this, degree_X_pow] lemma X_pow_sub_C_ne_zero {n : β„•} (hn : 0 < n) (a : R) : (X : polynomial R) ^ n - C a β‰  0 := mt degree_eq_bot.2 (show degree ((X : polynomial R) ^ n - C a) β‰  βŠ₯, by rw degree_X_pow_sub_C hn a; exact dec_trivial) theorem X_sub_C_ne_zero (r : R) : X - C r β‰  0 := pow_one (X : polynomial R) β–Έ X_pow_sub_C_ne_zero zero_lt_one r theorem zero_nmem_multiset_map_X_sub_C {Ξ± : Type*} (m : multiset Ξ±) (f : Ξ± β†’ R) : (0 : polynomial R) βˆ‰ m.map (Ξ» a, X - C (f a)) := Ξ» mem, let ⟨a, _, ha⟩ := multiset.mem_map.mp mem in X_sub_C_ne_zero _ ha lemma nat_degree_X_pow_sub_C {n : β„•} {r : R} : (X ^ n - C r).nat_degree = n := begin by_cases hn : n = 0, { rw [hn, pow_zero, ←C_1, ←ring_hom.map_sub, nat_degree_C] }, { exact nat_degree_eq_of_degree_eq_some (degree_X_pow_sub_C (pos_iff_ne_zero.mpr hn) r) }, end end nonzero_ring section no_zero_divisors variables [semiring R] [no_zero_divisors R] {p q : polynomial R} @[simp] lemma degree_mul : degree (p * q) = degree p + degree q := if hp0 : p = 0 then by simp only [hp0, degree_zero, zero_mul, with_bot.bot_add] else if hq0 : q = 0 then by simp only [hq0, degree_zero, mul_zero, with_bot.add_bot] else degree_mul' $ mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (mt leading_coeff_eq_zero.1 hq0) @[simp] lemma degree_pow [nontrivial R] (p : polynomial R) (n : β„•) : degree (p ^ n) = n β€’β„• (degree p) := by induction n; [simp only [pow_zero, degree_one, zero_nsmul], simp only [*, pow_succ, succ_nsmul, degree_mul]] @[simp] lemma leading_coeff_mul (p q : polynomial R) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin by_cases hp : p = 0, { simp only [hp, zero_mul, leading_coeff_zero] }, { by_cases hq : q = 0, { simp only [hq, mul_zero, leading_coeff_zero] }, { rw [leading_coeff_mul'], exact mul_ne_zero (mt leading_coeff_eq_zero.1 hp) (mt leading_coeff_eq_zero.1 hq) } } end @[simp] lemma leading_coeff_X_add_C [nontrivial R] (a b : R) (ha : a β‰  0): leading_coeff (C a * X + C b) = a := begin rw [add_comm, leading_coeff_add_of_degree_lt], { simp }, { simpa [degree_C ha] using lt_of_le_of_lt degree_C_le (with_bot.coe_lt_coe.2 zero_lt_one)} end /-- `polynomial.leading_coeff` bundled as a `monoid_hom` when `R` has `no_zero_divisors`, and thus `leading_coeff` is multiplicative -/ def leading_coeff_hom : polynomial R β†’* R := { to_fun := leading_coeff, map_one' := by simp, map_mul' := leading_coeff_mul } @[simp] lemma leading_coeff_hom_apply (p : polynomial R) : leading_coeff_hom p = leading_coeff p := rfl @[simp] lemma leading_coeff_pow (p : polynomial R) (n : β„•) : leading_coeff (p ^ n) = leading_coeff p ^ n := leading_coeff_hom.map_pow p n end no_zero_divisors end polynomial
33627aee752faf525ab119cb957bc77499d49f60
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/protected_consts.lean
b5e15624cedbda0882da7eb874f426dbaea05f66
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
596
lean
namespace foo protected axiom A : Prop axiom B : Prop protected constant a : A constant b : B protected axioms (A₁ Aβ‚‚ : Prop) protected constants (a₁ aβ‚‚ : A) axioms (B₁ Bβ‚‚ : Prop) constants (b₁ bβ‚‚ : B) end foo open foo check foo.A check A -- error check foo.a check a -- error check foo.A₁ check foo.Aβ‚‚ check A₁ -- error check Aβ‚‚ -- error check foo.a₁ check foo.aβ‚‚ check a₁ -- error check aβ‚‚ -- error check foo.B check B check foo.b check b check foo.b₁ check foo.bβ‚‚ check b₁ check bβ‚‚ check foo.B₁ check foo.Bβ‚‚ check B₁ check Bβ‚‚
8204ad7317dbc64a3829fae5a1225690f75b0644
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/n5.lean
ce8dc27bd8c6fb1a8fa18419f1d4efc09690377a
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
422
lean
variable N : Type.{1} variable f : N β†’ N β†’ N β†’ N variable g : N β†’ N β†’ N variable h : N β†’ N β†’ N β†’ N variable s : N β†’ N β†’ N β†’ N β†’ N precedence `*`:75 precedence `|`:75 notation a * b:prev | c:prev := f a b c notation a * b := g a b notation a * b * c:prev := h a b c notation a * b | c * d:prev := s a b c d variables a b c d e : N check a * b check a * b | d check a * b * c check a * b | d * e
f65d68456b508ed145d7fdc64a6330637cec1b1d
4727251e0cd73359b15b664c3170e5d754078599
/src/tactic/induction.lean
0c3e6d5b1296115811b195f88d2cdea12b2709cf
[ "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
55,028
lean
/- Copyright (c) 2020 Jannis Limperg. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jannis Limperg -/ import tactic.clear import tactic.dependencies import tactic.fresh_names import tactic.generalizes import tactic.has_variable_names import tactic.unify_equations /-! # A better tactic for induction and case analysis This module defines the tactics `tactic.interactive.induction'` and `tactic.interactive.cases'`, which are variations on Lean's builtin `induction` and `cases`. The primed variants feature various improvements over the builtin tactics; in particular, they generate more human-friendly names and `induction'` deals much better with indexed inductive types. See the tactics' documentation for more details. We also provide corresponding non-interactive induction tactics `tactic.eliminate_hyp` and `tactic.eliminate_expr`. The design and implementation of these tactics is described in a [draft paper](https://limperg.de/paper/cpp2021-induction/). -/ open expr native open tactic.interactive (case_tag.from_tag_hyps) namespace tactic namespace eliminate /-! ## Tracing We set up two tracing functions to be used by `eliminate_hyp` and its supporting tactics. Their output is enabled by setting `trace.eliminate_hyp` to `true`. -/ declare_trace eliminate_hyp /-- `trace_eliminate_hyp msg` traces `msg` if the option `trace.eliminate_hyp` is `true`. -/ meta def trace_eliminate_hyp {Ξ±} [has_to_format Ξ±] (msg : thunk Ξ±) : tactic unit := when_tracing `eliminate_hyp $ trace $ to_fmt "eliminate_hyp: " ++ to_fmt (msg ()) /-- `trace_state_eliminate_hyp msg` traces `msg` followed by the tactic state if the option `trace.eliminate_hyp` is `true`. -/ meta def trace_state_eliminate_hyp {Ξ±} [has_to_format Ξ±] (msg : thunk Ξ±) : tactic unit := do state ← read, trace_eliminate_hyp $ format.join [to_fmt (msg ()), "\n-----\n", to_fmt state, "\n-----"] /-! ## Information Gathering We define data structures for information relevant to the induction, and functions to collect this information for a specific goal. -/ /-- Information about a constructor argument. E.g. given the declaration ``` induction β„• : Type | zero : β„• | suc (n : β„•) : β„• ``` the `zero` constructor has no arguments and the `suc` constructor has one argument, `n`. We record the following information: - `aname`: the argument's name. If the argument was not explicitly named in the declaration, the elaborator generates a name for it. - `type` : the argument's type. - `dependent`: whether the argument is dependent, i.e. whether it occurs in the remainder of the constructor type. - `index_occurrences`: the index arguments of the constructor's return type in which this argument occurs. If the constructor return type is `I iβ‚€ ... iβ‚™` and the argument under consideration is `a`, and `a` occurs in `i₁` and `iβ‚‚`, then the `index_occurrences` are `1, 2`. As an additional requirement, for `iβ±Ό` to be considered an index occurrences, the type of `iβ±Ό` must match that of `a` according to `index_occurrence_type_match`. - `recursive_leading_pis`: `none` if this constructor is not recursive. Otherwise, the argument has type `Ξ  (x₁ : T₁) ... (xβ‚™ : Tβ‚™), I ...` where `I` is the inductive type to which this constructor belongs. In this case, `recursive_leading_pis` is `some n` with `n` the number of leading Ξ  binders in the argument's type. -/ @[derive has_reflect] meta structure constructor_argument_info := (aname : name) (type : expr) (dependent : bool) (index_occurrences : list β„•) (recursive_leading_pis : option β„•) namespace constructor_argument_info /-- `is_recursive c` is true iff the constructor argument described by `c` is recursive. -/ meta def is_recursive (c : constructor_argument_info) := c.recursive_leading_pis.is_some end constructor_argument_info /-- Information about a constructor. Contains: - `cname`: the constructor's name. - `non_param_args`: information about the arguments of the constructor, excluding the arguments induced by the parameters of the inductive type. - `num_non_param_args`: the length of `non_param_args`. - `rec_args`: the subset of `non_param_args` which are recursive constructor arguments. - `num_rec_args`: the length of `rec_args`. For example, take the constructor ``` list.cons : βˆ€ {Ξ±} (x : Ξ±) (xs : list Ξ±), list Ξ± ``` `Ξ±` is a parameter of `list`, so `non_param_args` contains information about `x` and `xs`. `rec_args` contains information about `xs`. -/ @[derive has_reflect] meta structure constructor_info := (cname : name) (non_param_args : list constructor_argument_info) (num_non_param_args : β„•) (rec_args : list constructor_argument_info) (num_rec_args : β„•) /-- When we construct the goal for the minor premise of a given constructor, this is the number of hypotheses we must name. -/ meta def constructor_info.num_nameable_hypotheses (c : constructor_info) : β„• := c.num_non_param_args + c.num_rec_args /-- Information about an inductive type. Contains: - `iname`: the type's name. - `constructors`: information about the type's constructors. - `num_constructors`: the length of `constructors`. - `type`: the type's type. - `num_param`: the type's number of parameters. - `num_indices`: the type's number of indices. -/ @[derive has_reflect] meta structure inductive_info := (iname : name) (constructors : list constructor_info) (num_constructors : β„•) (type : expr) (num_params : β„•) (num_indices : β„•) /-- Information about a major premise (i.e. the hypothesis on which we are performing induction). Contains: - `mpname`: the major premise's name. - `mpexpr`: the major premise itself. - `type`: the type of `mpexpr`. - `args`: the arguments of the major premise. The major premise has type `I xβ‚€ ... xβ‚™`, where `I` is an inductive type. `args` is the map `[0 β†’ xβ‚€, ..., n β†’ xβ‚™]`. -/ meta structure major_premise_info := (mpname : name) (mpexpr : expr) (type : expr) (args : rb_map β„• expr) /-- `index_occurrence_type_match t s` is true iff `t` and `s` are definitionally equal. -/ -- We could extend this check to be more permissive. E.g. if a constructor -- argument has type `list Ξ±` and the index has type `list Ξ²`, we may want to -- consider these types sufficiently similar to inherit the name. Same (but even -- more obvious) with `vec Ξ± n` and `vec Ξ± (n + 1)`. meta def index_occurrence_type_match (t s : expr) : tactic bool := succeeds $ is_def_eq t s /-- From the return type of a constructor `C` of an inductive type `I`, determine the index occurrences of the constructor arguments of `C`. Input: - `num_params:` the number of parameters of `I`. - `ret_type`: the return type of `C`. `e` must be of the form `I x₁ ... xβ‚™`. Output: A map associating each local constant `c` that appears in any of the `xα΅’` with the set of indexes `j` such that `c` appears in `xβ±Ό` and `xβ±Ό`'s type matches that of `c` according to `tactic.index_occurrence_type_match`. -/ meta def get_index_occurrences (num_params : β„•) (ret_type : expr) : tactic (rb_lmap expr β„•) := do ret_args ← get_app_args_whnf ret_type, ret_args.mfoldl_with_index (Ξ» i occ_map ret_arg, do if i < num_params then pure occ_map else do let ret_arg_consts := ret_arg.list_local_consts', ret_arg_consts.mfold occ_map $ Ξ» c occ_map, do ret_arg_type ← infer_type ret_arg, eq ← index_occurrence_type_match c.local_type ret_arg_type, pure $ if eq then occ_map.insert c i else occ_map) mk_rb_map /-- `match_recursive_constructor_arg I T`, given `I` the name of an inductive type and `T` the type of an argument of a constructor of `I`, returns `none` if the argument is non-recursive (i.e. `I` does not appear in `T`). If the argument is recursive, `T` is of the form `Ξ  (x₁ : T₁) ... (xβ‚™ : Tβ‚™), I ...`, in which case `match_recursive_constructor_arg` returns `some n`. Matching is performed up to WHNF with semireducible transparency. -/ meta def match_recursive_constructor_arg (I : name) (T : expr) : tactic (option β„•) := do (pis, base) ← open_pis_whnf T, base ← get_app_fn_whnf base, pure $ match base with | (const c _) := if c = I then some pis.length else none | _ := none end /-- Get information about the arguments of a constructor `C` of an inductive type `I`. Input: - `inductive_name`: the name of `I`. - `num_params`: the number of parameters of `I`. - `T`: the type of `C`. Output: a `constructor_argument_info` structure for each argument of `C`. -/ meta def get_constructor_argument_info (inductive_name : name) (num_params : β„•) (T : expr) : tactic (list constructor_argument_info) := do ⟨args, ret⟩ ← open_pis_whnf_dep T, index_occs ← get_index_occurrences num_params ret, args.mmap $ Ξ» ⟨c, dep⟩, do let occs := rb_set.of_list $ index_occs.find c, let type := c.local_type, recursive_leading_pis ← match_recursive_constructor_arg inductive_name type, pure ⟨c.local_pp_name, type, dep, occs.to_list, recursive_leading_pis⟩ /-- Get information about a constructor `C` of an inductive type `I`. Input: - `iname`: the name of `I`. - `num_params`: the number of parameters of `I`. - `c` : the name of `C`. Output: A `constructor_info` structure for `C`. -/ meta def get_constructor_info (iname : name) (num_params : β„•) (c : name) : tactic constructor_info := do env ← get_env, when (Β¬ env.is_constructor c) $ fail! "Expected {c} to be a constructor.", decl ← env.get c, args ← get_constructor_argument_info iname num_params decl.type, let non_param_args := args.drop num_params, let rec_args := non_param_args.filter $ Ξ» ainfo, ainfo.is_recursive, pure { cname := decl.to_name, non_param_args := non_param_args, num_non_param_args := non_param_args.length, rec_args := rec_args, num_rec_args := rec_args.length } /-- Get information about an inductive type `I`, given `I`'s name. -/ meta def get_inductive_info (I : name) : tactic inductive_info := do env ← get_env, when (Β¬ env.is_inductive I) $ fail! "Expected {I} to be an inductive type.", decl ← env.get I, let type := decl.type, let num_params := env.inductive_num_params I, let num_indices := env.inductive_num_indices I, let constructor_names := env.constructors_of I, constructors ← constructor_names.mmap (get_constructor_info I num_params), pure { iname := I, constructors := constructors, num_constructors := constructors.length, type := type, num_params := num_params, num_indices := num_indices } /-- Get information about a major premise. The given `expr` must be a local hypothesis. -/ meta def get_major_premise_info (major_premise : expr) : tactic major_premise_info := do type ← infer_type major_premise, ⟨f, args⟩ ← get_app_fn_args_whnf type, pure { mpname := major_premise.local_pp_name, mpexpr := major_premise, type := type, args := args.to_rb_map } /-! ## Constructor Argument Naming We define the algorithm for naming constructor arguments (which is a remarkably big part of the tactic). -/ /-- Information used when naming a constructor argument. -/ meta structure constructor_argument_naming_info := (mpinfo : major_premise_info) (iinfo : inductive_info) (cinfo : constructor_info) (ainfo : constructor_argument_info) /-- A constructor argument naming rule takes a `constructor_argument_naming_info` structure and returns a list of suitable names for the argument. If the rule is not applicable to the given constructor argument, the returned list is empty. -/ @[reducible] meta def constructor_argument_naming_rule : Type := constructor_argument_naming_info β†’ tactic (list name) /-- Naming rule for recursive constructor arguments. -/ meta def constructor_argument_naming_rule_rec : constructor_argument_naming_rule := Ξ» i, pure $ if i.ainfo.is_recursive then [i.mpinfo.mpname] else [] /-- Naming rule for constructor arguments associated with an index. -/ meta def constructor_argument_naming_rule_index : constructor_argument_naming_rule := Ξ» i, let index_occs := i.ainfo.index_occurrences in let major_premise_args := i.mpinfo.args in let get_major_premise_arg_local_names : β„• β†’ option (name Γ— name) := Ξ» i, do { arg ← major_premise_args.find i, (uname, ppname, _) ← arg.match_local_const, pure (uname, ppname) } in let local_index_instantiations := (index_occs.map get_major_premise_arg_local_names).all_some in /- Right now, this rule only triggers if the major premise arg is exactly a local const. We could consider a more permissive rule where the major premise arg can be an arbitrary term as long as that term *contains* only a single local const. -/ pure $ match local_index_instantiations with | none := [] | some [] := [] | some ((uname, ppname) :: is) := if is.all (Ξ» ⟨uname', _⟩, uname' = uname) then [ppname] else [] end /-- Naming rule for constructor arguments which are named in the constructor declaration. -/ meta def constructor_argument_naming_rule_named : constructor_argument_naming_rule := Ξ» i, let arg_name := i.ainfo.aname in let arg_dep := i.ainfo.dependent in pure $ if ! arg_dep && arg_name.is_likely_generated_binder_name then [] else [arg_name] /-- Naming rule for constructor arguments whose type is associated with a list of typical variable names. See `tactic.typical_variable_names`. -/ meta def constructor_argument_naming_rule_type : constructor_argument_naming_rule := Ξ» i, typical_variable_names i.ainfo.type <|> pure [] /-- Naming rule for constructor arguments whose type is in `Prop`. -/ meta def constructor_argument_naming_rule_prop : constructor_argument_naming_rule := Ξ» i, do (sort level.zero) ← infer_type i.ainfo.type | pure [], pure [`h] /-- Fallback constructor argument naming rule. This rule never fails. -/ meta def constructor_argument_naming_rule_fallback : constructor_argument_naming_rule := Ξ» _, pure [`x] /-- `apply_constructor_argument_naming_rules info rules` applies the constructor argument naming rules in `rules` to the constructor argument given by `info`. Returns the result of the first applicable rule. Fails if no rule is applicable. -/ meta def apply_constructor_argument_naming_rules (info : constructor_argument_naming_info) (rules : list constructor_argument_naming_rule) : tactic (list name) := do names ← try_core $ rules.mfirst (Ξ» r, do names ← r info, match names with | [] := failed | _ := pure names end), match names with | none := fail "apply_constructor_argument_naming_rules: no applicable naming rule" | (some names) := pure names end /-- Get possible names for a constructor argument. This tactic applies all the previously defined rules in order. It cannot fail and always returns a nonempty list. -/ meta def constructor_argument_names (info : constructor_argument_naming_info) : tactic (list name) := apply_constructor_argument_naming_rules info [ constructor_argument_naming_rule_rec , constructor_argument_naming_rule_index , constructor_argument_naming_rule_named , constructor_argument_naming_rule_type , constructor_argument_naming_rule_prop , constructor_argument_naming_rule_fallback ] /-- `intron_fresh n` introduces `n` hypotheses with names generated by `tactic.mk_fresh_name`. -/ meta def intron_fresh (n : β„•) : tactic (list expr) := iterate_exactly n (mk_fresh_name >>= intro) /-- Introduce the new hypotheses generated by the minor premise for a given constructor. The new hypotheses are given fresh (unique, non-human-friendly) names. They are later renamed by `constructor_renames`. We delay the generation of the human-friendly names because when `constructor_renames` is called, more names may have become unused. Input: - `generate_induction_hyps`: whether we generate induction hypotheses (i.e. whether `eliminate_hyp` is in `induction` or `cases` mode). - `cinfo`: information about the constructor. Output: - For each constructor argument: (1) the pretty name of the newly introduced hypothesis corresponding to the argument; (2) the argument's `constructor_argument_info`. - For each newly introduced induction hypothesis: (1) its pretty name; (2) the pretty name of the hypothesis corresponding to the constructor argument from which this induction hypothesis was derived; (3) that constructor argument's `constructor_argument_info`. -/ meta def constructor_intros (generate_induction_hyps : bool) (cinfo : constructor_info) : tactic (list (name Γ— constructor_argument_info) Γ— list (name Γ— name Γ— constructor_argument_info)) := do let args := cinfo.non_param_args, arg_hyps ← intron_fresh cinfo.num_non_param_args, let args := (arg_hyps.map expr.local_pp_name).zip args, tt ← pure generate_induction_hyps | pure (args, []), let rec_args := args.filter $ Ξ» x, x.2.is_recursive, ih_hyps ← intron_fresh cinfo.num_rec_args, let ihs := (ih_hyps.map expr.local_pp_name).zip rec_args, pure (args, ihs) /-- `ih_name arg_name` is the name `ih_<arg_name>`. -/ meta def ih_name (arg_name : name) : name := mk_simple_name ("ih_" ++ arg_name.to_string) /-- Representation of a pattern in the `with n ...` syntax supported by `induction'` and `cases'`. A `with_pattern` can be: - `with_pattern.auto` (`with _` or no `with` clause): use the name generated by the tactic. - `with_pattern.clear` (`with -`): clear this hypothesis and any hypotheses depending on it. - `with_pattern.exact n` (`with n`): use the name `n` for this hypothesis. -/ @[derive has_reflect] meta inductive with_pattern | auto | clear | exact (n : name) namespace with_pattern open lean (parser) open lean.parser /-- Parser for a `with_pattern`. -/ protected meta def parser : lean.parser with_pattern := (tk "-" *> pure with_pattern.clear) <|> (tk "_" *> pure with_pattern.auto) <|> (with_pattern.exact <$> ident) /-- Parser for a `with` clause. -/ meta def clause_parser : lean.parser (list with_pattern) := (tk "with" *> many with_pattern.parser) <|> pure [] /-- `to_name_spec auto_candidates p` returns a description of how the hypothesis to which the `with_pattern` `p` applies should be named. If this function returns `none`, the hypothesis should be cleared. If it returns `some (inl n)`, it should receive exactly the name `n`, even if this shadows other hypotheses. If it returns `some (inr ns)`, it should receive the first unused name from `ns`. If `p = auto`, the `auto_candidates` tactic is run to determine candidate names for the hypothesis (from which the first fresh one is later chosen). `auto_candidates` must return a nonempty list. -/ meta def to_name_spec (auto_candidates : tactic (list name)) : with_pattern β†’ tactic (option (name βŠ• list name)) | auto := (some ∘ sum.inr) <$> auto_candidates | clear := pure none | (exact n) := pure $ some $ sum.inl n end with_pattern /-- If `h` refers to a hypothesis, `clear_dependent_if_exists h` clears `h` and any hypotheses which depend on it. Otherwise, the tactic does nothing. -/ meta def clear_dependent_if_exists (h : name) : tactic unit := do (some h) ← try_core $ get_local h | pure (), clear' tt [h] /-- Rename the new hypotheses in the goal for a minor premise. Input: - `generate_induction_hyps`: whether we generate induction hypotheses (i.e. whether `eliminate_hyp` is in `induction` or `cases` mode). - `mpinfo`: information about the major premise. - `iinfo`: information about the inductive type. - `cinfo`: information about the constructor whose minor premise we are processing. - `with_patterns`: a list of `with` patterns given by the user. These are used to name constructor arguments and induction hypotheses. If the list does not contain enough patterns for all introduced hypotheses, the remaining ones are treated as if the user had given `with_pattern.auto` (`_`). - `args` and `ihs`: the output of `constructor_intros`. Output: - The newly introduced hypotheses corresponding to constructor arguments. - The newly introduced induction hypotheses. -/ meta def constructor_renames (generate_induction_hyps : bool) (mpinfo : major_premise_info) (iinfo : inductive_info) (cinfo : constructor_info) (with_patterns : list with_pattern) (args : list (name Γ— constructor_argument_info)) (ihs : list (name Γ— name Γ— constructor_argument_info)) : tactic (list expr Γ— list expr) := do -- Rename constructor arguments let arg_pp_name_set := name_set.of_list $ args.map prod.fst, let iname := iinfo.iname, let ⟨args, with_patterns⟩ := args.mapβ‚‚_left' (Ξ» arg p, (arg, p.get_or_else with_pattern.auto)) with_patterns, arg_renames ← args.mmap_filter $ Ξ» ⟨⟨old_ppname, ainfo⟩, with_pat⟩, do { (some new) ← with_pat.to_name_spec (constructor_argument_names ⟨mpinfo, iinfo, cinfo, ainfo⟩) | clear_dependent_if_exists old_ppname >> pure none, -- Some of the arg hyps may have been cleared by earlier simplification -- steps, so get_local may fail. (some old) ← try_core $ get_local old_ppname | pure none, pure $ some (old.local_uniq_name, new) }, let arg_renames := rb_map.of_list arg_renames, arg_hyp_map ← rename_fresh arg_renames mk_name_set, let new_arg_hyps := arg_hyp_map.filter_map $ Ξ» ⟨old, new⟩, if arg_pp_name_set.contains old.local_pp_name then some new else none, let arg_hyp_map : name_map expr := rb_map.of_list $ arg_hyp_map.map $ Ξ» ⟨old, new⟩, (old.local_pp_name, new), -- Rename induction hypotheses (if we generated them) tt ← pure generate_induction_hyps | pure (new_arg_hyps, []), let ih_pp_name_set := name_set.of_list $ ihs.map prod.fst, let ihs := ihs.mapβ‚‚_left (Ξ» ih p, (ih, p.get_or_else with_pattern.auto)) with_patterns, let single_ih := ihs.length = 1, ih_renames ← ihs.mmap_filter $ Ξ» ⟨⟨ih_hyp_ppname, arg_hyp_ppname, _⟩, with_pat⟩, do { some arg_hyp ← pure $ arg_hyp_map.find arg_hyp_ppname | fail! "internal error in constructor_renames: {arg_hyp_ppname} not found in arg_hyp_map", (some new) ← with_pat.to_name_spec (pure $ if single_ih then [`ih, ih_name arg_hyp.local_pp_name] -- If we have only a single IH which hasn't been named explicitly in a -- `with` clause, the preferred name is "ih". If that is taken, we fall -- back to the name the IH would ordinarily receive. else [ih_name arg_hyp.local_pp_name]) | clear_dependent_if_exists ih_hyp_ppname >> pure none, (some ih_hyp) ← try_core $ get_local ih_hyp_ppname | pure none, pure $ some (ih_hyp.local_uniq_name, new) }, ih_hyp_map ← rename_fresh (rb_map.of_list ih_renames) mk_name_set, let new_ih_hyps := ih_hyp_map.filter_map $ Ξ» ⟨old, new⟩, if ih_pp_name_set.contains old.local_pp_name then some new else none, pure (new_arg_hyps, new_ih_hyps) /-! ## Generalisation `induction'` can generalise the goal before performing an induction, which gives us a more general induction hypothesis. We call this 'auto-generalisation'. -/ /-- A value of `generalization_mode` describes the behaviour of the auto-generalisation functionality: - `generalize_all_except hs` means that the `hs` remain fixed and all other hypotheses are generalised. However, there are three exceptions: * Hypotheses depending on any `h` in `hs` also remain fixed. If we were to generalise them, we would have to generalise `h` as well. * Hypotheses which do not occur in the target and which do not mention the major premise or its dependencies are never generalised. Generalising them would not lead to a more general induction hypothesis. * Local definitions (hypotheses of the form `h : T := t`) and their dependencies are not generalised. This is due to limitations of the implementation; local definitions could in principle be generalised. - `generalize_only hs` means that only the `hs` are generalised. Exception: hypotheses which depend on the major premise are generalised even if they do not appear in `hs`. -/ @[derive has_reflect] inductive generalization_mode | generalize_all_except (hs : list name) : generalization_mode | generalize_only (hs : list name) : generalization_mode instance : inhabited generalization_mode := ⟨ generalization_mode.generalize_all_except []⟩ namespace generalization_mode /-- Given the major premise and a generalization_mode, this function returns the unique names of the hypotheses that should be generalized. See `generalization_mode` for what these are. -/ meta def to_generalize (major_premise : expr) : generalization_mode β†’ tactic name_set | (generalize_only ns) := do major_premise_rev_deps ← reverse_dependencies_of_hyps [major_premise], let major_premise_rev_deps := name_set.of_list $ major_premise_rev_deps.map local_uniq_name, ns ← ns.mmap (functor.map local_uniq_name ∘ get_local), pure $ major_premise_rev_deps.insert_list ns | (generalize_all_except fixed) := do fixed ← fixed.mmap get_local, tgt ← target, let tgt_dependencies := tgt.list_local_const_unique_names, major_premise_type ← infer_type major_premise, major_premise_dependencies ← dependency_name_set_of_hyp_inclusive major_premise, defs ← local_defs, fixed_dependencies ← (major_premise :: defs ++ fixed).mmap dependency_name_set_of_hyp_inclusive, let fixed_dependencies := fixed_dependencies.foldl name_set.union mk_name_set, ctx ← local_context, to_revert ← ctx.mmap_filter $ Ξ» h, do { h_depends_on_major_premise_deps ← -- TODO `hyp_depends_on_local_name_set` is somewhat expensive hyp_depends_on_local_name_set h major_premise_dependencies, let h_name := h.local_uniq_name, let rev := Β¬ fixed_dependencies.contains h_name ∧ (h_depends_on_major_premise_deps ∨ tgt_dependencies.contains h_name), /- I think `h_depends_on_major_premise_deps` is an overapproximation. What we actually want is any hyp that depends either on the major_premise or on one of the major_premise's index args. (But the overapproximation seems to work okay in practice as well.) -/ pure $ if rev then some h_name else none }, pure $ name_set.of_list to_revert end generalization_mode /-- Generalize hypotheses for the given major premise and generalization mode. See `generalization_mode` and `to_generalize`. -/ meta def generalize_hyps (major_premise : expr) (gm : generalization_mode) : tactic β„• := do to_revert ← gm.to_generalize major_premise, ⟨n, _⟩ ← unfreezing (revert_name_set to_revert), pure n /-! ## Complex Index Generalisation A *complex* expression is any expression that is not merely a local constant. When such a complex expression appears as an argument of the major premise, and when that argument is an index of the inductive type, we must generalise the complex expression. E.g. when we operate on the major premise `fin (2 + n)` (assuming that `fin` is encoded as an inductive type), the `2 + n` is a complex index argument. To generalise it, we replace it with a new hypothesis `index : β„•` and add an equation `induction_eq : index = 2 + n`. -/ /-- Generalise the complex index arguments. Input: - `major premise`: the major premise. - `num_params`: the number of parameters of the inductive type. - `generate_induction_hyps`: whether we generate induction hypotheses (i.e. whether `eliminate_hyp` is in `induction` or `cases` mode). Output: - The new major premise. This procedure may change the major premise's type signature, so the old major premise hypothesis is invalidated. - The number of index placeholder hypotheses we introduced. - The index placeholder hypotheses we introduced. - The number of hypotheses which were reverted because they contain complex indices. -/ /- TODO The following function currently replaces complex index arguments everywhere in the goal, not only in the major premise. Such replacements are sometimes necessary to make sure that the goal remains type-correct. However, the replacements can also have the opposite effect, yielding unprovable subgoals. The test suite contains one such case. There is probably a middle ground between 'replace everywhere' and 'replace only in the major premise', but I don't know what exactly this middle ground is. See also the discussion at https://github.com/leanprover-community/mathlib/pull/5027#discussion_r538902424 -/ meta def generalize_complex_index_args (major_premise : expr) (num_params : β„•) (generate_induction_hyps : bool) : tactic (expr Γ— β„• Γ— list name Γ— β„•) := focus1 $ do major_premise_type ← infer_type major_premise, (major_premise_head, major_premise_args) ← get_app_fn_args_whnf major_premise_type, let ⟨major_premise_param_args, major_premise_index_args⟩ := major_premise_args.split_at num_params, -- TODO Add equations only for complex index args (not all index args). -- This shouldn't matter semantically, but we'd get simpler terms. let js := major_premise_index_args, ctx ← local_context, tgt ← target, major_premise_deps ← dependency_name_set_of_hyp_inclusive major_premise, -- Revert the hypotheses which depend on the index args or the major_premise. -- We exclude dependencies of the major premise because we can't replace their -- index occurrences anyway when we apply the recursor. relevant_ctx ← ctx.mfilter $ Ξ» h, do { let dep_of_major_premise := major_premise_deps.contains h.local_uniq_name, dep_on_major_premise ← hyp_depends_on_locals h [major_premise], H ← infer_type h, dep_of_index ← js.many $ Ξ» j, kdepends_on H j, -- TODO We need a variant of `kdepends_on` that takes local defs into account. pure $ (dep_on_major_premise ∧ h β‰  major_premise) ∨ (dep_of_index ∧ Β¬ dep_of_major_premise) }, ⟨relevant_ctx_size, relevant_ctx⟩ ← unfreezing $ do { r ← revert_lst' relevant_ctx, revert major_premise, pure r }, -- Create the local constants that will replace the index args. We have to be -- careful to get the right types. let go : expr β†’ list expr β†’ tactic (list expr) := Ξ» j ks, do { J ← infer_type j, k ← mk_local' `index binder_info.default J, ks ← ks.mmap $ Ξ» k', kreplace k' j k, pure $ k :: ks }, ks ← js.mfoldr go [], let js_ks := js.zip ks, -- Replace the index args in the relevant context. new_ctx ← relevant_ctx.mmap $ Ξ» h, js_ks.mfoldr (Ξ» ⟨j, k⟩ h, kreplace h j k) h, -- Replace the index args in the major premise. let new_major_premise_type := major_premise_head.mk_app (major_premise_param_args ++ ks), let new_major_premise := local_const major_premise.local_uniq_name major_premise.local_pp_name major_premise.binding_info new_major_premise_type, -- Replace the index args in the target. new_tgt ← js_ks.mfoldr (Ξ» ⟨j, k⟩ tgt, kreplace tgt j k) tgt, let new_tgt := new_tgt.pis (new_major_premise :: new_ctx), -- Generate the index equations and their proofs. let eq_name := if generate_induction_hyps then `induction_eq else `cases_eq, let step2_input := js_ks.map $ Ξ» ⟨j, k⟩, (eq_name, j, k), eqs_and_proofs ← generalizes.step2 reducible step2_input, let eqs := eqs_and_proofs.map prod.fst, let eq_proofs := eqs_and_proofs.map prod.snd, -- Assert the generalized goal and derive the current goal from it. generalizes.step3 new_tgt js ks eqs eq_proofs, -- Introduce the index variables and major premise. The index equations -- and the relevant context remain reverted. let num_index_vars := js.length, index_vars ← intron' num_index_vars, index_equations ← intron' num_index_vars, major_premise ← intro1, revert_lst index_equations, let index_vars := index_vars.map local_pp_name, pure (major_premise, index_vars.length, index_vars, relevant_ctx_size) /-! ## Simplification of Induction Hypotheses Auto-generalisation and complex index generalisation may produce unnecessarily complex induction hypotheses. We define a simplification algorithm that recovers understandable induction hypotheses in many practical cases. -/ /-- Process one index equation for `simplify_ih`. Input: a local constant `h : x = y` or `h : x == y`. Output: A proof of `x = y` or `x == y` and possibly a local constant of type `x = y` or `x == y` used in the proof. More specifically: - For `h : x = y` and `x` defeq `y`, we return the proof of `x = y` by reflexivity and `none`. - For `h : x = y` and `x` not defeq `y`, we return `h` and `h`. - For `h : x == y` where `x` and `y` have defeq types: - If `x` defeq `y`, we return the proof of `x == y` by reflexivity and `none`. - If `x` not defeq `y`, we return `heq_of_eq h'` and a fresh local constant `h' : x = y`. - For `h : x == y` where `x` and `y` do not have defeq types, we return `h` and `h`. Checking for definitional equality of the left- and right-hand sides may assign metavariables. -/ meta def process_index_equation : expr β†’ tactic (expr Γ— option expr) | h@(local_const _ ppname binfo T@(app (app (app (const `eq [u]) type) lhs) rhs)) := do rhs_eq_lhs ← succeeds $ unify rhs lhs, -- Note: It is important that we `unify rhs lhs` rather than `unify lhs rhs`. -- This is because `lhs` and `rhs` may be metavariables which represent -- Ξ -bound variables, so if they unify, we want to assign `rhs := lhs`. -- If we assign `lhs := rhs` instead, it can happen that `lhs` is used before -- `rhs` is bound, so the generated term becomes ill-typed. if rhs_eq_lhs then pure ((const `eq.refl [u]) type lhs, none) else do pure (h, some h) | h@(local_const uname ppname binfo T@(app (app (app (app (const `heq [u]) lhs_type) lhs) rhs_type) rhs)) := do lhs_type_eq_rhs_type ← succeeds $ is_def_eq lhs_type rhs_type, if Β¬ lhs_type_eq_rhs_type then do pure (h, some h) else do lhs_eq_rhs ← succeeds $ unify rhs lhs, -- See note above about `unify rhs lhs`. if lhs_eq_rhs then pure ((const `heq.refl [u]) lhs_type lhs, none) else do c ← mk_local' ppname binfo $ (const `eq [u]) lhs_type lhs rhs, let arg := (const `heq_of_eq [u]) lhs_type lhs rhs c, pure (arg, some c) | (local_const _ _ _ T) := fail! "process_index_equation: expected a homogeneous or heterogeneous equation, but got:\n{T}" | e := fail! "process_index_equation: expected a local constant, but got:\n{e}" /-- `assign_local_to_unassigned_mvar mv pp_name binfo`, where `mv` is a metavariable, acts as follows: - If `mv` is assigned, it is not changed and the tactic returns `none`. - If `mv` is not assigned, it is assigned a fresh local constant with the type of `mv`, pretty name `pp_name` and binder info `binfo`. This local constant is returned. -/ meta def assign_local_to_unassigned_mvar (mv : expr) (pp_name : name) (binfo : binder_info) : tactic (option expr) := do ff ← is_assigned mv | pure none, type ← infer_type mv, c ← mk_local' pp_name binfo type, unify mv c, pure c /-- Apply `assign_local_to_unassigned_mvar` to a list of metavariables. Returns the newly created local constants. -/ meta def assign_locals_to_unassigned_mvars (mvars : list (expr Γ— name Γ— binder_info)) : tactic (list expr) := mvars.mmap_filter $ Ξ» ⟨mv, pp_name, binfo⟩, assign_local_to_unassigned_mvar mv pp_name binfo /-- Simplify an induction hypothesis. Input: a local constant ``` ih : βˆ€ (a₁ : A₁) ... (aβ‚™ : Aβ‚™) (b₁ : B₁) ... (bβ‚˜ : Bβ‚˜) (eq₁ : y₁ = z₁) ... (eqβ‚– : yβ‚’ = zβ‚’), P ``` where `n = num_leading_pis`, `m = num_generalized` and `o = num_index_vars`. The `aα΅’` are arguments of the type of the constructor argument to which this induction hypothesis belongs (usually zero). The `xα΅’` are hypotheses that we generalised over before performing induction. The `eqα΅’` are index equations. Output: a new local constant ``` ih' : βˆ€ (a'₁ : A'₁) ... (b'β‚– : B'β‚–) (eq'₁ : y'₁ = z'₁) ... (eq'β‚— : y'β‚— = z'β‚—), P' ``` This new induction hypothesis is derived from `ih` by removing those `eqα΅’` whose left- and right-hand sides can be unified. This unification may also determine some of the `aα΅’` and `bα΅’`. The `a'α΅’`, `b'α΅’` and `eq'α΅’` are those `aα΅’`, `bα΅’` and `eqα΅’` that were not removed by this process. Some of the `eqα΅’` may be heterogeneous: `eqα΅’ : yα΅’ == zα΅’`. In this case, we proceed as follows: - If `yα΅’` and `zα΅’` are defeq, then `eqα΅’` is removed. - If `yα΅’` and `zα΅’` are not defeq but their types are, then `eqα΅’` is replaced by `eq'α΅’ : x = y`. - Otherwise `eqα΅’` remains unchanged. -/ /- TODO `simplify_ih` currently uses Lean's builtin unification procedure to process the index equations. This procedure has some limitations. For example, we would like to clear an IH that assumes `0 = 1` since this IH can never be applied, but Lean's unification doesn't allow us to conclude this. It would therefore be preferable to use the algorithm from `tactic.unify_equations` instead. There is no problem with this in principle, but it requires a complete refactoring of `unify_equations` so that it works not only on hypotheses but on arbitrary terms. -/ meta def simplify_ih (num_leading_pis : β„•) (num_generalized : β„•) (num_index_vars : β„•) (ih : expr) : tactic expr := do T ← infer_type ih, -- Replace the `xα΅’` with fresh metavariables. (generalized_arg_mvars, body) ← open_n_pis_metas' T (num_leading_pis + num_generalized), -- Replace the `eqα΅’` with fresh local constants. (index_eq_lcs, body) ← open_n_pis body num_index_vars, -- Process the `eqα΅’` local constants, yielding -- - `new_args`: proofs of `yα΅’ = zα΅’`. -- - `new_index_eq_lcs`: local constants of type `yα΅’ = zα΅’` or `yα΅’ == zα΅’` used -- in `new_args`. new_index_eq_lcs_new_args ← index_eq_lcs.mmap process_index_equation, let (new_args, new_index_eq_lcs) := new_index_eq_lcs_new_args.unzip, let new_index_eq_lcs := new_index_eq_lcs.reduce_option, -- Assign fresh local constants to those `xα΅’` metavariables that were not -- assigned by the previous step. new_generalized_arg_lcs ← assign_locals_to_unassigned_mvars generalized_arg_mvars, -- Instantiate the metavariables assigned in the previous steps. new_generalized_arg_lcs ← new_generalized_arg_lcs.mmap instantiate_mvars, new_index_eq_lcs ← new_index_eq_lcs.mmap instantiate_mvars, -- Construct a proof of the new induction hypothesis by applying `ih` to the -- `xα΅’` metavariables and the `new_args`, then abstracting over the -- `new_index_eq_lcs` and the `new_generalized_arg_lcs`. b ← instantiate_mvars $ ih.mk_app (generalized_arg_mvars.map prod.fst ++ new_args), new_ih ← lambdas (new_generalized_arg_lcs ++ new_index_eq_lcs) b, -- Type-check the new induction hypothesis as a sanity check. type_check new_ih <|> fail! "internal error in simplify_ih: constructed term does not type check:\n{new_ih}", -- Replace the old induction hypothesis with the new one. ih' ← note ih.local_pp_name none new_ih, clear ih, pure ih' /-! ## Temporary utilities The utility functions in this section should be removed pending certain changes to Lean's standard library. -/ /-- Updates the tags of new subgoals produced by `cases` or `induction`. `in_tag` is the initial tag, i.e. the tag of the goal on which `cases`/`induction` was applied. `rs` should contain, for each subgoal, the constructor name associated with that goal and the hypotheses that were introduced. -/ -- TODO Copied from init.meta.interactive. Make that function non-private. meta def set_cases_tags (in_tag : tag) (rs : list (name Γ— list expr)) : tactic unit := do gs ← get_goals, match gs with -- if only one goal was produced, we should not make the tag longer | [g] := set_tag g in_tag | _ := let tgs : list (name Γ— list expr Γ— expr) := rs.mapβ‚‚ (Ξ» ⟨n, new_hyps⟩ g, ⟨n, new_hyps, g⟩) gs in tgs.mmap' $ Ξ» ⟨n, new_hyps, g⟩, with_enable_tags $ set_tag g $ (case_tag.from_tag_hyps (n :: in_tag) (new_hyps.map expr.local_uniq_name)).render end end eliminate /-! ## The Elimination Tactics Finally, we define the tactics `induction'` and `cases'` tactics as well as the non-interactive variant `eliminate_hyp.` -/ open eliminate /-- `eliminate_hyp generate_ihs h gm with_patterns` performs induction or case analysis on the hypothesis `h`. If `generate_ihs` is true, the tactic performs induction, otherwise case analysis. In case analysis mode, `eliminate_hyp` is very similar to `tactic.cases`. The only differences (assuming no bugs in `eliminate_hyp`) are that `eliminate_hyp` can do case analysis on a slightly larger class of hypotheses and that it generates more human-friendly names. In induction mode, `eliminate_hyp` is similar to `tactic.induction`, but with more significant differences: - If `h` (the hypothesis we are performing induction on) has complex indices, `eliminate_hyp` 'remembers' them. A complex expression is any expression that is not merely a local hypothesis. A hypothesis `h : I p₁ ... pβ‚™ j₁ ... jβ‚˜`, where `I` is an inductive type with `n` parameters and `m` indices, has a complex index if any of the `jα΅’` are complex. In this situation, standard `induction` effectively forgets the exact values of the complex indices, which often leads to unprovable goals. `eliminate_hyp` 'remembers' them by adding propositional equalities. As a result, you may find equalities named `induction_eq` in your goal, and the induction hypotheses may also quantify over additional equalities. - `eliminate_hyp` generalises induction hypotheses as much as possible by default. This means that if you eliminate `n` in the goal ``` n m : β„• ⊒ P n m ``` the induction hypothesis is `βˆ€ m, P n m` instead of `P n m`. You can modify this behaviour by giving a different generalisation mode `gm`; see `tactic.eliminate.generalization_mode`. - `eliminate_hyp` generates much more human-friendly names than `induction`. It also clears more redundant hypotheses. - `eliminate_hyp` currently does not support custom induction principles a la `induction using`. The `with_patterns` can be used to give names for the hypotheses introduced by `eliminate_hyp`. See `tactic.eliminate.with_pattern` for details. To debug this tactic, use ``` set_option trace.eliminate_hyp true ``` -/ meta def eliminate_hyp (generate_ihs : bool) (major_premise : expr) (gm := generalization_mode.generalize_all_except []) (with_patterns : list with_pattern := []) : tactic unit := focus1 $ do mpinfo ← get_major_premise_info major_premise, let major_premise_type := mpinfo.type, let major_premise_args := mpinfo.args.values.reverse, env ← get_env, -- Get info about the inductive type iname ← get_app_fn_const_whnf major_premise_type <|> fail! "The type of {major_premise} should be an inductive type, but it is\n{major_premise_type}", iinfo ← get_inductive_info iname, -- We would like to disallow mutual/nested inductive types, since these have -- complicated recursors which we probably don't support. However, there seems -- to be no way to find out whether an inductive type is mutual/nested. -- (`environment.is_ginductive` doesn't seem to work.) trace_state_eliminate_hyp "State before complex index generalisation:", -- Generalise complex indices (major_premise, num_index_vars, index_var_names, num_index_generalized) ← generalize_complex_index_args major_premise iinfo.num_params generate_ihs, trace_state_eliminate_hyp "State after complex index generalisation and before auto-generalisation:", -- Generalise hypotheses according to the given generalization_mode. num_auto_generalized ← generalize_hyps major_premise gm, let num_generalized := num_index_generalized + num_auto_generalized, -- NOTE: The previous step may have changed the unique names of all hyps in -- the context. -- Record the current case tag. in_tag ← get_main_tag, trace_state_eliminate_hyp "State after auto-generalisation and before recursor application:", -- Apply the recursor. We first try the nondependent recursor, then the -- dependent recursor (if available). -- Construct a pexpr `@rec _ ... _ major_premise`. Why not -- ```(%%rec %%major_premise)?` Because for whatever reason, `false.rec_on` -- takes the motive not as an implicit argument, like any other recursor, but -- as an explicit one. Why not something based on `mk_app` or `mk_mapp`? -- Because we need the special elaborator support for `elab_as_eliminator` -- definitions. let rec_app : name β†’ pexpr := Ξ» rec_suffix, (unchecked_cast expr.mk_app : pexpr β†’ list pexpr β†’ pexpr) (pexpr.mk_explicit (const (iname ++ rec_suffix) [])) (list.repeat pexpr.mk_placeholder (major_premise_args.length + 1) ++ [to_pexpr major_premise]), let rec_suffix := if generate_ihs then "rec_on" else "cases_on", let drec_suffix := if generate_ihs then "drec_on" else "dcases_on", interactive.apply (rec_app rec_suffix) <|> interactive.apply (rec_app drec_suffix) <|> fail! "Failed to apply the (dependent) recursor for {iname} on {major_premise}.", -- Prepare the "with" names for each constructor case. let with_patterns := prod.fst $ with_patterns.take_list (iinfo.constructors.map constructor_info.num_nameable_hypotheses), let constrs := iinfo.constructors.zip with_patterns, -- For each case (constructor): cases : list (option (name Γ— list expr)) ← focus $ constrs.map $ Ξ» ⟨cinfo, with_patterns⟩, do { trace_eliminate_hyp "============", trace_eliminate_hyp $ format! "Case {cinfo.cname}", trace_state_eliminate_hyp "Initial state:", -- Get the major premise's arguments. (Some of these may have changed due -- to the generalising step above.) major_premise_type ← infer_type major_premise, major_premise_args ← get_app_args_whnf major_premise_type, -- Clear the eliminated hypothesis (if possible) try $ clear major_premise, -- Clear the index args (unless other stuff in the goal depends on them) major_premise_args.mmap' (try ∘ clear), trace_state_eliminate_hyp "State after clearing the major premise (and its arguments) and before introductions:", -- Introduce the constructor arguments (constructor_args, ihs) ← constructor_intros generate_ihs cinfo, -- Introduce the auto-generalised hypotheses. intron num_auto_generalized, -- Introduce the index equations index_equations ← intron' num_index_vars, let index_equations := index_equations.map local_pp_name, -- Introduce the hypotheses that were generalised during index -- generalisation. intron num_index_generalized, trace_state_eliminate_hyp "State after introductions and before simplifying index equations:", -- Simplify the index equations. Stop after this step if the goal has been -- solved by the simplification. ff ← unify_equations index_equations | trace_eliminate_hyp "Case solved while simplifying index equations." >> pure none, trace_state_eliminate_hyp "State after simplifying index equations and before simplifying IHs:", -- Simplify the induction hypotheses -- NOTE: The previous step may have changed the unique names of the -- induction hypotheses, so we have to locate them again. Their pretty -- names should be unique in the context, so we can use these. ihs.mmap' $ Ξ» ⟨ih, _, arg_info⟩, do { ih ← get_local ih, (some num_leading_pis) ← pure arg_info.recursive_leading_pis | fail! "eliminate_hyp: internal error: unexpected non-recursive argument info", simplify_ih num_leading_pis num_auto_generalized num_index_vars ih }, trace_state_eliminate_hyp "State after simplifying IHs and before clearing index variables:", -- Try to clear the index variables. These often become unused during -- the index equation simplification step. index_var_names.mmap $ Ξ» h, try (get_local h >>= clear), trace_state_eliminate_hyp "State after clearing index variables and before renaming:", -- Rename the constructor names and IHs. We do this here (rather than -- earlier, when we introduced them) because there may now be less -- hypotheses in the context, and therefore more of the desired -- names may be free. (constructor_arg_hyps, ih_hyps) ← constructor_renames generate_ihs mpinfo iinfo cinfo with_patterns constructor_args ihs, trace_state_eliminate_hyp "Final state:", -- Return the constructor name and the renamable new hypotheses. These are -- the hypotheses that can later be renamed by the `case` tactic. Note -- that index variables and index equations are not renamable. This may be -- counterintuitive in some cases, but it's surprisingly difficult to -- catch exactly the relevant hyps here. pure $ some (cinfo.cname, constructor_arg_hyps ++ ih_hyps) }, set_cases_tags in_tag cases.reduce_option, pure () /-- A variant of `tactic.eliminate_hyp` which performs induction or case analysis on an arbitrary expression. `eliminate_hyp` requires that the major premise is a hypothesis. `eliminate_expr` lifts this restriction by generalising the goal over the major premise before calling `eliminate_hyp`. The generalisation replaces the major premise with a new hypothesis `x` everywhere in the goal. If `eq_name` is `some h`, an equation `h : major_premise = x` is added to remember the value of the major premise. -/ meta def eliminate_expr (generate_induction_hyps : bool) (major_premise : expr) (eq_name : option name := none) (gm := generalization_mode.generalize_all_except []) (with_patterns : list with_pattern := []) : tactic unit := do major_premise_revdeps ← reverse_dependencies_of_hyps [major_premise], num_reverted ← unfreezing (revert_lst major_premise_revdeps), hyp ← match eq_name with | some h := do x ← get_unused_name `x, interactive.generalize h () (to_pexpr major_premise, x), get_local x | none := do if major_premise.is_local_constant then pure major_premise else do x ← get_unused_name `x, generalize' major_premise x end, intron num_reverted, eliminate_hyp generate_induction_hyps hyp gm with_patterns end tactic namespace tactic.interactive open tactic tactic.eliminate interactive interactive.types lean.parser /-- Parse a `fixing` or `generalizing` clause for `induction'` or `cases'`. -/ meta def generalisation_mode_parser : lean.parser generalization_mode := (tk "fixing" *> ((tk "*" *> pure (generalization_mode.generalize_only [])) <|> generalization_mode.generalize_all_except <$> many ident)) <|> (tk "generalizing" *> generalization_mode.generalize_only <$> many ident) <|> pure (generalization_mode.generalize_all_except []) /-- A variant of `tactic.interactive.induction`, with the following differences: - If the major premise (the hypothesis we are performing induction on) has complex indices, `induction'` 'remembers' them. A complex expression is any expression that is not merely a local hypothesis. A major premise `h : I p₁ ... pβ‚™ j₁ ... jβ‚˜`, where `I` is an inductive type with `n` parameters and `m` indices, has a complex index if any of the `jα΅’` are complex. In this situation, standard `induction` effectively forgets the exact values of the complex indices, which often leads to unprovable goals. `induction'` 'remembers' them by adding propositional equalities. As a result, you may find equalities named `induction_eq` in your goal, and the induction hypotheses may also quantify over additional equalities. - `induction'` generalises induction hypotheses as much as possible by default. This means that if you eliminate `n` in the goal ``` n m : β„• ⊒ P n m ``` the induction hypothesis is `βˆ€ m, P n m` instead of `P n m`. - `induction'` generates much more human-friendly names than `induction`. It also clears redundant hypotheses more aggressively. - `induction'` currently does not support custom induction principles a la `induction using`. Like `induction`, `induction'` supports some modifiers: `induction' e with n₁ ... nβ‚˜` uses the names `nα΅’` for the new hypotheses. Instead of a name, you can also give an underscore (`_`) to have `induction'` generate a name for you, or a hyphen (`-`) to clear the hypothesis and any hypotheses that depend on it. `induction' e fixing h₁ ... hβ‚™` fixes the hypotheses `hα΅’`, so the induction hypothesis is not generalised over these hypotheses. `induction' e fixing *` fixes all hypotheses. This disables the generalisation functionality, so this mode behaves like standard `induction`. `induction' e generalizing h₁ ... hβ‚™` generalises only the hypotheses `hα΅’`. This mode behaves like `induction e generalizing h₁ ... hβ‚™`. `induction' t`, where `t` is an arbitrary term (rather than a hypothesis), generalises the goal over `t`, then performs induction on the generalised goal. `induction' h : t = x` is similar, but also adds an equation `h : t = x` to remember the value of `t`. To debug this tactic, use ``` set_option trace.eliminate_hyp true ``` -/ meta def induction' (major_premise : parse cases_arg_p) (gm : parse generalisation_mode_parser) (with_patterns : parse with_pattern.clause_parser) : tactic unit := do let ⟨eq_name, e⟩ := major_premise, e ← to_expr e, eliminate_expr tt e eq_name gm with_patterns /-- A variant of `tactic.interactive.cases`, with minor changes: - `cases'` can perform case analysis on some (rare) goals that `cases` does not support. - `cases'` generates much more human-friendly names for the new hypotheses it introduces. This tactic supports the same modifiers as `cases`, e.g. ``` cases' H : e = x with n _ o ``` This is almost exactly the same as `tactic.interactive.induction'`, only that no induction hypotheses are generated. To debug this tactic, use ``` set_option trace.eliminate_hyp true ``` -/ meta def cases' (major_premise : parse cases_arg_p) (with_patterns : parse with_pattern.clause_parser) : tactic unit := do let ⟨eq_name, e⟩ := major_premise, e ← to_expr e, eliminate_expr ff e eq_name (generalization_mode.generalize_only []) with_patterns end tactic.interactive
4feab82e3f813c6de2c730de95455b93a9314aa8
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/field_theory/cardinality.lean
049050e06e1350430e00a7f7d6359594378519e3
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
3,349
lean
/- Copyright (c) 2022 Eric Rodriguez. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Rodriguez -/ import algebra.ring.ulift import data.mv_polynomial.cardinal import data.rat.denumerable import field_theory.finite.galois_field import logic.equiv.transfer_instance import ring_theory.localization.cardinality import set_theory.cardinal.divisibility import data.nat.factorization.prime_pow /-! # Cardinality of Fields In this file we show all the possible cardinalities of fields. All infinite cardinals can harbour a field structure, and so can all types with prime power cardinalities, and this is sharp. ## Main statements * `fintype.nonempty_field_iff`: A `fintype` can be given a field structure iff its cardinality is a prime power. * `infinite.nonempty_field` : Any infinite type can be endowed a field structure. * `field.nonempty_iff` : There is a field structure on type iff its cardinality is a prime power. -/ local notation `β€–` x `β€–` := fintype.card x open_locale cardinal non_zero_divisors universe u /-- A finite field has prime power cardinality. -/ lemma fintype.is_prime_pow_card_of_field {Ξ±} [fintype Ξ±] [field Ξ±] : is_prime_pow (β€–Ξ±β€–) := begin casesI char_p.exists Ξ± with p _, haveI hp := fact.mk (char_p.char_is_prime Ξ± p), let b := is_noetherian.finset_basis (zmod p) Ξ±, rw [module.card_fintype b, zmod.card, is_prime_pow_pow_iff], { exact hp.1.is_prime_pow }, rw ←finite_dimensional.finrank_eq_card_basis b, exact finite_dimensional.finrank_pos.ne' end /-- A `fintype` can be given a field structure iff its cardinality is a prime power. -/ lemma fintype.nonempty_field_iff {Ξ±} [fintype Ξ±] : nonempty (field Ξ±) ↔ is_prime_pow (β€–Ξ±β€–) := begin refine ⟨λ ⟨h⟩, by exactI fintype.is_prime_pow_card_of_field, _⟩, rintros ⟨p, n, hp, hn, hα⟩, haveI := fact.mk hp.nat_prime, exact ⟨(fintype.equiv_of_card_eq ((galois_field.card p n hn.ne').trans hΞ±)).symm.field⟩, end lemma fintype.not_is_field_of_card_not_prime_pow {Ξ±} [fintype Ξ±] [ring Ξ±] : Β¬ is_prime_pow (β€–Ξ±β€–) β†’ Β¬ is_field Ξ± := mt $ Ξ» h, fintype.nonempty_field_iff.mp ⟨h.to_field⟩ /-- Any infinite type can be endowed a field structure. -/ lemma infinite.nonempty_field {Ξ± : Type u} [infinite Ξ±] : nonempty (field Ξ±) := begin letI K := fraction_ring (mv_polynomial Ξ± $ ulift.{u} β„š), suffices : #Ξ± = #K, { obtain ⟨e⟩ := cardinal.eq.1 this, exact ⟨e.field⟩ }, rw ←is_localization.card (mv_polynomial Ξ± $ ulift.{u} β„š)⁰ K le_rfl, apply le_antisymm, { refine ⟨⟨λ a, mv_polynomial.monomial (finsupp.single a 1) (1 : ulift.{u} β„š), Ξ» x y h, _⟩⟩, simpa [mv_polynomial.monomial_eq_monomial_iff, finsupp.single_eq_single_iff] using h }, { simp } end /-- There is a field structure on type if and only if its cardinality is a prime power. -/ lemma field.nonempty_iff {Ξ± : Type u} : nonempty (field Ξ±) ↔ is_prime_pow (#Ξ±) := begin rw cardinal.is_prime_pow_iff, casesI fintype_or_infinite Ξ± with h h, { simpa only [cardinal.mk_fintype, nat.cast_inj, exists_eq_left', (cardinal.nat_lt_aleph_0 _).not_le, false_or] using fintype.nonempty_field_iff }, { simpa only [← cardinal.infinite_iff, h, true_or, iff_true] using infinite.nonempty_field }, end
542733fb217bd318ee669923b2fc65b595e9b8c4
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/algebra/tropical/basic.lean
26fe607601e0bc60222b6698e77ca198389ddfbd
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,091
lean
/- Copyright (c) 2021 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import algebra.group_power.order import algebra.smul_with_zero /-! # Tropical algebraic structures This file defines algebraic structures of the (min-)tropical numbers, up to the tropical semiring. Some basic lemmas about conversion from the base type `R` to `tropical R` are provided, as well as the expected implementations of tropical addition and tropical multiplication. ## Main declarations * `tropical R`: The type synonym of the tropical interpretation of `R`. If `[linear_order R]`, then addition on `R` is via `min`. * `semiring (tropical R)`: A `linear_ordered_add_comm_monoid_with_top R` induces a `semiring (tropical R)`. If one solely has `[linear_ordered_add_comm_monoid R]`, then the "tropicalization of `R`" would be `tropical (with_top R)`. ## Implementation notes The tropical structure relies on `has_top` and `min`. For the max-tropical numbers, use `order_dual R`. Inspiration was drawn from the implementation of `additive`/`multiplicative`/`opposite`, where a type synonym is created with some barebones API, and quickly made irreducible. Algebraic structures are provided with as few typeclass assumptions as possible, even though most references rely on `semiring (tropical R)` for building up the whole theory. ## References followed * https://arxiv.org/pdf/math/0408099.pdf * https://www.mathenjeans.fr/sites/default/files/sujets/tropical_geometry_-_casagrande.pdf -/ universes u v variables (R : Type u) /-- The tropicalization of a type `R`. -/ def tropical : Type u := R variables {R} namespace tropical /-- Reinterpret `x : R` as an element of `tropical R`. See `tropical.trop_equiv` for the equivalence. -/ @[pp_nodot] def trop : R β†’ tropical R := id /-- Reinterpret `x : tropical R` as an element of `R`. See `tropical.trop_equiv` for the equivalence. -/ @[pp_nodot] def untrop : tropical R β†’ R := id lemma trop_injective : function.injective (trop : R β†’ tropical R) := Ξ» _ _, id lemma untrop_injective : function.injective (untrop : tropical R β†’ R) := Ξ» _ _, id @[simp] lemma trop_inj_iff (x y : R) : trop x = trop y ↔ x = y := iff.rfl @[simp] lemma untrop_inj_iff (x y : tropical R) : untrop x = untrop y ↔ x = y := iff.rfl @[simp] lemma trop_untrop (x : tropical R) : trop (untrop x) = x := rfl @[simp] lemma untrop_trop (x : R) : untrop (trop x) = x := rfl lemma left_inverse_trop : function.left_inverse (trop : R β†’ tropical R) untrop := trop_untrop lemma right_inverse_trop : function.right_inverse (trop : R β†’ tropical R) untrop := trop_untrop attribute [irreducible] tropical /-- Reinterpret `x : R` as an element of `tropical R`. See `tropical.trop_order_iso` for the order-preserving equivalence. -/ def trop_equiv : R ≃ tropical R := { to_fun := trop, inv_fun := untrop, left_inv := untrop_trop, right_inv := trop_untrop } @[simp] lemma trop_equiv_coe_fn : (trop_equiv : R β†’ tropical R) = trop := rfl @[simp] lemma trop_equiv_symm_coe_fn : (trop_equiv.symm : tropical R β†’ R) = untrop := rfl lemma trop_eq_iff_eq_untrop {x : R} {y} : trop x = y ↔ x = untrop y := trop_equiv.apply_eq_iff_eq_symm_apply lemma untrop_eq_iff_eq_trop {x} {y : R} : untrop x = y ↔ x = trop y := trop_equiv.symm.apply_eq_iff_eq_symm_apply lemma injective_trop : function.injective (trop : R β†’ tropical R) := trop_equiv.injective lemma injective_untrop : function.injective (untrop : tropical R β†’ R) := trop_equiv.symm.injective lemma surjective_trop : function.surjective (trop : R β†’ tropical R) := trop_equiv.surjective lemma surjective_untrop : function.surjective (untrop : tropical R β†’ R) := trop_equiv.symm.surjective instance [inhabited R] : inhabited (tropical R) := ⟨trop (default _)⟩ /-- Recursing on a `x' : tropical R` is the same as recursing on an `x : R` reinterpreted as a term of `tropical R` via `trop x`. -/ @[simp] def trop_rec {F : Ξ  (X : tropical R), Sort v} (h : Ξ  X, F (trop X)) : Ξ  X, F X := Ξ» X, h (untrop X) section order instance [preorder R] : preorder (tropical R) := { le := Ξ» x y, untrop x ≀ untrop y, le_refl := Ξ» _, le_refl _, le_trans := Ξ» _ _ _ h h', le_trans h h', } @[simp] lemma untrop_le_iff [preorder R] {x y : tropical R} : untrop x ≀ untrop y ↔ x ≀ y := iff.rfl /-- Reinterpret `x : R` as an element of `tropical R`, preserving the order. -/ def trop_order_iso [preorder R] : R ≃o tropical R := { map_rel_iff' := Ξ» _ _, untrop_le_iff, ..trop_equiv } @[simp] lemma trop_order_iso_coe_fn [preorder R] : (trop_order_iso : R β†’ tropical R) = trop := rfl @[simp] lemma trop_order_iso_symm_coe_fn [preorder R] : (trop_order_iso.symm : tropical R β†’ R) = untrop := rfl instance [partial_order R] : partial_order (tropical R) := { le_antisymm := Ξ» _ _ h h', untrop_injective (le_antisymm h h'), ..tropical.preorder } instance [has_top R] : has_zero (tropical R) := ⟨trop ⊀⟩ instance [has_top R] : has_top (tropical R) := ⟨0⟩ @[simp] lemma untrop_zero [has_top R] : untrop (0 : tropical R) = ⊀ := rfl @[simp] lemma trop_top [has_top R] : trop (⊀ : R) = 0 := rfl @[simp] lemma trop_coe_ne_zero (x : R) : trop (x : with_top R) β‰  0 . @[simp] lemma zero_ne_trop_coe (x : R) : (0 : tropical (with_top R)) β‰  trop x . @[simp] lemma le_zero [order_top R] (x : tropical R) : x ≀ 0 := le_top instance [partial_order R] : order_top (tropical (with_top R)) := { le_top := Ξ» a a' h, option.no_confusion h, ..tropical.partial_order, ..tropical.has_top } variable [linear_order R] /-- Tropical addition is the minimum of two underlying elements of `R`. -/ protected def add (x y : tropical R) : tropical R := trop (min (untrop x) (untrop y)) instance : add_comm_semigroup (tropical R) := { add := tropical.add, add_assoc := Ξ» _ _ _, untrop_injective (min_assoc _ _ _), add_comm := Ξ» _ _, untrop_injective (min_comm _ _) } instance : linear_order (tropical R) := { le_total := Ξ» a b, le_total (untrop a) (untrop b), decidable_le := Ξ» x y, if h : (untrop x) ≀ (untrop y) then is_true h else is_false h, ..tropical.partial_order } @[simp] lemma untrop_add (x y : tropical R) : untrop (x + y) = min (untrop x) (untrop y) := rfl lemma trop_add_def (x y : tropical R) : x + y = trop (min (untrop x) (untrop y)) := rfl @[simp] lemma add_eq_left ⦃x y : tropical R⦄ (h : x ≀ y) : x + y = x := untrop_injective (by simpa using h) @[simp] lemma add_eq_right ⦃x y : tropical R⦄ (h : y ≀ x) : x + y = y := untrop_injective (by simpa using h) @[simp] lemma add_self (x : tropical R) : x + x = x := untrop_injective (min_eq_right le_rfl) @[simp] lemma bit0 (x : tropical R) : bit0 x = x := add_self x lemma add_eq_iff {x y z : tropical R} : x + y = z ↔ x = z ∧ x ≀ y ∨ y = z ∧ y ≀ x := by simp [trop_add_def, trop_eq_iff_eq_untrop, min_eq_iff] @[simp] lemma add_eq_zero_iff {a b : tropical (with_top R)} : a + b = 0 ↔ a = 0 ∧ b = 0 := begin rw add_eq_iff, split, { rintro (⟨rfl, h⟩|⟨rfl, h⟩), { exact ⟨rfl, le_antisymm (le_zero _) h⟩ }, { exact ⟨le_antisymm (le_zero _) h, rfl⟩ } }, { rintro ⟨rfl, rfl⟩, simp } end -- We cannot define `add_comm_monoid` here because there is no class that is solely -- `[linear_order R] [order_top R]` end order section monoid /-- Tropical multiplication is the addition in the underlying `R`. -/ protected def mul [has_add R] (x y : tropical R) : tropical R := trop (untrop x + untrop y) instance [has_add R] : has_mul (tropical R) := ⟨tropical.mul⟩ @[simp] lemma untrop_mul [has_add R] (x y : tropical R) : untrop (x * y) = untrop x + untrop y := rfl lemma trop_mul_def [has_add R] (x y : tropical R) : x * y = trop (untrop x + untrop y) := rfl instance [has_zero R] : has_one (tropical R) := ⟨trop 0⟩ instance [has_zero R] : nontrivial (tropical (with_top R)) := ⟨⟨0, 1, trop_injective.ne with_top.top_ne_coe⟩⟩ instance [has_neg R] : has_inv (tropical R) := ⟨λ x, trop (- untrop x)⟩ @[simp] lemma untrop_inv [has_neg R] (x : tropical R) : untrop x⁻¹ = - untrop x := rfl instance [has_sub R] : has_div (tropical R) := ⟨λ x y, trop (untrop x - untrop y)⟩ @[simp] lemma untrop_div [has_sub R] (x y : tropical R) : untrop (x / y) = untrop x - untrop y := rfl instance [add_semigroup R] : semigroup (tropical R) := { mul := tropical.mul, mul_assoc := Ξ» _ _ _, untrop_injective (add_assoc _ _ _) } instance [add_comm_semigroup R] : comm_semigroup (tropical R) := { mul_comm := Ξ» _ _, untrop_injective (add_comm _ _), ..tropical.semigroup } instance [add_monoid R] : monoid (tropical R) := { one := trop 0, one_mul := Ξ» _, untrop_injective (zero_add _), mul_one := Ξ» _, untrop_injective (add_zero _), ..tropical.semigroup } @[simp] lemma untrop_one [add_monoid R] : untrop (1 : tropical R) = 0 := rfl @[simp] lemma untrop_pow [add_monoid R] (x : tropical R) (n : β„•) : untrop (x ^ n) = n β€’ untrop x := begin induction n with n IH, { simp, }, { rw [pow_succ, untrop_mul, IH, succ_nsmul] } end @[simp] lemma trop_nsmul [add_monoid R] (x : R) (n : β„•) : trop (n β€’ x) = trop x ^ n := by simp [trop_eq_iff_eq_untrop] instance [add_comm_monoid R] : comm_monoid (tropical R) := { ..tropical.monoid, ..tropical.comm_semigroup } instance [add_group R] : group (tropical R) := { inv := Ξ» x, trop (- untrop x), mul_left_inv := Ξ» _, untrop_injective (add_left_neg _), ..tropical.monoid } instance [add_comm_group R] : comm_group (tropical R) := { mul_comm := Ξ» _ _, untrop_injective (add_comm _ _), ..tropical.group } end monoid section distrib instance covariant_mul [preorder R] [has_add R] [covariant_class R R (+) (≀)] : covariant_class (tropical R) (tropical R) (*) (≀) := ⟨λ x y z h, add_le_add_left h _⟩ instance covariant_swap_mul [preorder R] [has_add R] [covariant_class R R (function.swap (+)) (≀)] : covariant_class (tropical R) (tropical R) (function.swap (*)) (≀) := ⟨λ x y z h, add_le_add_right h _⟩ instance [linear_order R] [has_add R] [covariant_class R R (+) (≀)] [covariant_class R R (function.swap (+)) (≀)] : distrib (tropical R) := { mul := tropical.mul, add := tropical.add, left_distrib := Ξ» _ _ _, untrop_injective (min_add_add_left _ _ _).symm, right_distrib := Ξ» _ _ _, untrop_injective (min_add_add_right _ _ _).symm } @[simp] lemma add_pow [linear_order R] [add_monoid R] [covariant_class R R (+) (≀)] [covariant_class R R (function.swap (+)) (≀)] (x y : tropical R) (n : β„•) : (x + y) ^ n = x ^ n + y ^ n := begin cases le_total x y with h h, { rw [add_eq_left h, add_eq_left (pow_le_pow_of_le_left' h _)] }, { rw [add_eq_right h, add_eq_right (pow_le_pow_of_le_left' h _)] } end end distrib section semiring variable [linear_ordered_add_comm_monoid_with_top R] instance : comm_semiring (tropical R) := { zero_add := Ξ» _, untrop_injective (min_top_left _), add_zero := Ξ» _, untrop_injective (min_top_right _), zero_mul := Ξ» _, untrop_injective (top_add _), mul_zero := Ξ» _, untrop_injective (add_top _), ..tropical.has_zero, ..tropical.distrib, ..tropical.add_comm_semigroup, ..tropical.comm_monoid } -- This could be stated on something like `linear_order_with_top Ξ±` if that existed @[simp] lemma succ_nsmul (x : tropical R) (n : β„•) : (n + 1) β€’ x = x := begin induction n with n IH, { simp }, { rw [add_nsmul, IH, one_nsmul, add_self] } end -- TODO: find/create the right classes to make this hold (for enat, ennreal, etc) -- Requires `zero_eq_bot` to be true -- lemma add_eq_zero_iff {a b : tropical R} : -- a + b = 1 ↔ a = 1 ∨ b = 1 := sorry @[simp] lemma mul_eq_zero_iff {R : Type*} [linear_ordered_add_comm_monoid R] {a b : tropical (with_top R)} : a * b = 0 ↔ a = 0 ∨ b = 0 := by simp [←untrop_inj_iff, with_top.add_eq_top] instance {R : Type*} [linear_ordered_add_comm_monoid R] : no_zero_divisors (tropical (with_top R)) := ⟨λ _ _, mul_eq_zero_iff.mp⟩ end semiring end tropical
d4f83ac81654a96f130b77e288e1b8eac5934bf6
46125763b4dbf50619e8846a1371029346f4c3db
/src/topology/metric_space/closeds.lean
6bde51e333c1c8556068b633648bf2e7ab264c89
[ "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
21,582
lean
/- Copyright (c) 2019 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: SΓ©bastien GouΓ«zel -/ import topology.metric_space.hausdorff_distance topology.opens analysis.specific_limits /-! # Closed subsets This file defines the metric and emetric space structure on the types of closed subsets and nonempty compact subsets of a metric or emetric space. The Hausdorff distance induces an emetric space structure on the type of closed subsets of an emetric space, called `closeds`. Its completeness, resp. compactness, resp. second-countability, follow from the corresponding properties of the original space. In a metric space, the type of nonempty compact subsets (called `nonempty_compacts`) also inherits a metric space structure from the Hausdorff distance, as the Hausdorff edistance is always finite in this context. -/ noncomputable theory open_locale classical open_locale topological_space universe u open classical lattice set function topological_space filter namespace emetric section variables {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} /-- In emetric spaces, the Hausdorff edistance defines an emetric space structure on the type of closed subsets -/ instance closeds.emetric_space : emetric_space (closeds Ξ±) := { edist := Ξ»s t, Hausdorff_edist s.val t.val, edist_self := Ξ»s, Hausdorff_edist_self, edist_comm := Ξ»s t, Hausdorff_edist_comm, edist_triangle := Ξ»s t u, Hausdorff_edist_triangle, eq_of_edist_eq_zero := Ξ»s t h, subtype.eq ((Hausdorff_edist_zero_iff_eq_of_closed s.property t.property).1 h) } /-- The edistance to a closed set depends continuously on the point and the set -/ lemma continuous_inf_edist_Hausdorff_edist : continuous (Ξ»p : Ξ± Γ— (closeds Ξ±), inf_edist p.1 (p.2).val) := begin refine continuous_of_le_add_edist 2 (by simp) _, rintros ⟨x, s⟩ ⟨y, t⟩, calc inf_edist x (s.val) ≀ inf_edist x (t.val) + Hausdorff_edist (t.val) (s.val) : inf_edist_le_inf_edist_add_Hausdorff_edist ... ≀ (inf_edist y (t.val) + edist x y) + Hausdorff_edist (t.val) (s.val) : add_le_add_right' inf_edist_le_inf_edist_add_edist ... = inf_edist y (t.val) + (edist x y + Hausdorff_edist (s.val) (t.val)) : by simp [add_comm, add_left_comm, Hausdorff_edist_comm] ... ≀ inf_edist y (t.val) + (edist (x, s) (y, t) + edist (x, s) (y, t)) : add_le_add_left' (add_le_add' (by simp [edist, le_refl]) (by simp [edist, le_refl])) ... = inf_edist y (t.val) + 2 * edist (x, s) (y, t) : by rw [← mul_two, mul_comm] end /-- Subsets of a given closed subset form a closed set -/ lemma is_closed_subsets_of_is_closed (hs : is_closed s) : is_closed {t : closeds Ξ± | t.val βŠ† s} := begin refine is_closed_of_closure_subset (Ξ»t ht x hx, _), -- t : closeds Ξ±, ht : t ∈ closure {t : closeds Ξ± | t.val βŠ† s}, -- x : Ξ±, hx : x ∈ t.val -- goal : x ∈ s have : x ∈ closure s, { refine mem_closure_iff.2 (λΡ Ξ΅pos, _), rcases mem_closure_iff.1 ht Ξ΅ Ξ΅pos with ⟨u, hu, Dtu⟩, -- u : closeds Ξ±, hu : u ∈ {t : closeds Ξ± | t.val βŠ† s}, hu' : edist t u < Ξ΅ rcases exists_edist_lt_of_Hausdorff_edist_lt hx Dtu with ⟨y, hy, Dxy⟩, -- y : Ξ±, hy : y ∈ u.val, Dxy : edist x y < Ξ΅ exact ⟨y, hu hy, Dxy⟩ }, rwa closure_eq_of_is_closed hs at this, end /-- By definition, the edistance on `closeds Ξ±` is given by the Hausdorff edistance -/ lemma closeds.edist_eq {s t : closeds Ξ±} : edist s t = Hausdorff_edist s.val t.val := rfl /-- In a complete space, the type of closed subsets is complete for the Hausdorff edistance. -/ instance closeds.complete_space [complete_space Ξ±] : complete_space (closeds Ξ±) := begin /- We will show that, if a sequence of sets `s n` satisfies `edist (s n) (s (n+1)) < 2^{-n}`, then it converges. This is enough to guarantee completeness, by a standard completeness criterion. We use the shorthand `B n = 2^{-n}` in ennreal. -/ let B : β„• β†’ ennreal := Ξ» n, (2⁻¹)^n, have B_pos : βˆ€ n, (0:ennreal) < B n, by simp [B, ennreal.pow_pos], have B_ne_top : βˆ€ n, B n β‰  ⊀, by simp [B, ennreal.div_def, ennreal.pow_ne_top], /- Consider a sequence of closed sets `s n` with `edist (s n) (s (n+1)) < B n`. We will show that it converges. The limit set is t0 = β‹‚n, closure (⋃mβ‰₯n, s m). We will have to show that a point in `s n` is close to a point in `t0`, and a point in `t0` is close to a point in `s n`. The completeness then follows from a standard criterion. -/ refine complete_of_convergent_controlled_sequences B B_pos (Ξ»s hs, _), let t0 := β‹‚n, closure (⋃mβ‰₯n, (s m).val), let t : closeds Ξ± := ⟨t0, is_closed_Inter (Ξ»_, is_closed_closure)⟩, use t, -- The inequality is written this way to agree with `edist_le_of_edist_le_geometric_of_tendstoβ‚€` have I1 : βˆ€n:β„•, βˆ€x ∈ (s n).val, βˆƒy ∈ t0, edist x y ≀ 2 * B n, { /- This is the main difficulty of the proof. Starting from `x ∈ s n`, we want to find a point in `t0` which is close to `x`. Define inductively a sequence of points `z m` with `z n = x` and `z m ∈ s m` and `edist (z m) (z (m+1)) ≀ B m`. This is possible since the Hausdorff distance between `s m` and `s (m+1)` is at most `B m`. This sequence is a Cauchy sequence, therefore converging as the space is complete, to a limit which satisfies the required properties. -/ assume n x hx, obtain ⟨z, hzβ‚€, hz⟩ : βˆƒ z : Ξ  l, (s (n+l)).val, (z 0:Ξ±) = x ∧ βˆ€ k, edist (z k:Ξ±) (z (k+1):Ξ±) ≀ B n / 2^k, { -- We prove existence of the sequence by induction. have : βˆ€ (l : β„•) (z : (s (n+l)).val), βˆƒ z' : (s (n+l+1)).val, edist (z:Ξ±) z' ≀ B n / 2^l, { assume l z, obtain ⟨z', z'_mem, hz'⟩ : βˆƒ z' ∈ (s (n+l+1)).val, edist (z:Ξ±) z' < B n / 2^l, { apply exists_edist_lt_of_Hausdorff_edist_lt z.2, simp only [B, ennreal.div_def, ennreal.inv_pow'], rw [← pow_add], apply hs; simp }, exact ⟨⟨z', z'_mem⟩, le_of_lt hz'⟩ }, use [Ξ» k, nat.rec_on k ⟨x, hx⟩ (Ξ»l z, some (this l z)), rfl], exact Ξ» k, some_spec (this k _) }, -- it follows from the previous bound that `z` is a Cauchy sequence have : cauchy_seq (Ξ» k, ((z k):Ξ±)), from cauchy_seq_of_edist_le_geometric_two (B n) (B_ne_top n) hz, -- therefore, it converges rcases cauchy_seq_tendsto_of_complete this with ⟨y, y_lim⟩, use y, -- the limit point `y` will be the desired point, in `t0` and close to our initial point `x`. -- First, we check it belongs to `t0`. have : y ∈ t0 := mem_Inter.2 (Ξ»k, mem_closure_of_tendsto (by simp) y_lim begin simp only [exists_prop, set.mem_Union, filter.mem_at_top_sets, set.mem_preimage, set.preimage_Union], exact ⟨k, Ξ» m hm, ⟨n+m, zero_add k β–Έ add_le_add (zero_le n) hm, (z m).2⟩⟩ end), use this, -- Then, we check that `y` is close to `x = z n`. This follows from the fact that `y` -- is the limit of `z k`, and the distance between `z n` and `z k` has already been estimated. rw [← hzβ‚€], exact edist_le_of_edist_le_geometric_two_of_tendstoβ‚€ (B n) hz y_lim }, have I2 : βˆ€n:β„•, βˆ€x ∈ t0, βˆƒy ∈ (s n).val, edist x y ≀ 2 * B n, { /- For the (much easier) reverse inequality, we start from a point `x ∈ t0` and we want to find a point `y ∈ s n` which is close to `x`. `x` belongs to `t0`, the intersection of the closures. In particular, it is well approximated by a point `z` in `⋃mβ‰₯n, s m`, say in `s m`. Since `s m` and `s n` are close, this point is itself well approximated by a point `y` in `s n`, as required. -/ assume n x xt0, have : x ∈ closure (⋃mβ‰₯n, (s m).val), by apply mem_Inter.1 xt0 n, rcases mem_closure_iff.1 this (B n) (B_pos n) with ⟨z, hz, Dxz⟩, -- z : Ξ±, Dxz : edist x z < B n, simp only [exists_prop, set.mem_Union] at hz, rcases hz with ⟨m, ⟨m_ge_n, hm⟩⟩, -- m : β„•, m_ge_n : m β‰₯ n, hm : z ∈ (s m).val have : Hausdorff_edist (s m).val (s n).val < B n := hs n m n m_ge_n (le_refl n), rcases exists_edist_lt_of_Hausdorff_edist_lt hm this with ⟨y, hy, Dzy⟩, -- y : Ξ±, hy : y ∈ (s n).val, Dzy : edist z y < B n exact ⟨y, hy, calc edist x y ≀ edist x z + edist z y : edist_triangle _ _ _ ... ≀ B n + B n : add_le_add' (le_of_lt Dxz) (le_of_lt Dzy) ... = 2 * B n : (two_mul _).symm ⟩ }, -- Deduce from the above inequalities that the distance between `s n` and `t0` is at most `2 B n`. have main : βˆ€n:β„•, edist (s n) t ≀ 2 * B n := Ξ»n, Hausdorff_edist_le_of_mem_edist (I1 n) (I2 n), -- from this, the convergence of `s n` to `t0` follows. refine (tendsto_at_top _).2 (λΡ Ξ΅pos, _), have : tendsto (Ξ»n, 2 * B n) at_top (𝓝 (2 * 0)), from ennreal.tendsto.const_mul (ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 $ by simp [ennreal.one_lt_two]) (or.inr $ by simp), rw mul_zero at this, obtain ⟨N, hN⟩ : βˆƒ N, βˆ€ b β‰₯ N, Ξ΅ > 2 * B b, from ((tendsto_order.1 this).2 Ξ΅ Ξ΅pos).exists_forall_of_at_top, exact ⟨N, Ξ»n hn, lt_of_le_of_lt (main n) (hN n hn)⟩ end /-- In a compact space, the type of closed subsets is compact. -/ instance closeds.compact_space [compact_space Ξ±] : compact_space (closeds Ξ±) := ⟨begin /- by completeness, it suffices to show that it is totally bounded, i.e., for all Ξ΅>0, there is a finite set which is Ξ΅-dense. start from a set `s` which is Ξ΅-dense in Ξ±. Then the subsets of `s` are finitely many, and Ξ΅-dense for the Hausdorff distance. -/ refine compact_of_totally_bounded_is_closed (emetric.totally_bounded_iff.2 (λΡ Ξ΅pos, _)) is_closed_univ, rcases dense Ξ΅pos with ⟨δ, Ξ΄pos, Ξ΄lt⟩, rcases emetric.totally_bounded_iff.1 (compact_iff_totally_bounded_complete.1 (@compact_univ Ξ± _ _)).1 Ξ΄ Ξ΄pos with ⟨s, fs, hs⟩, -- s : set Ξ±, fs : finite s, hs : univ βŠ† ⋃ (y : Ξ±) (H : y ∈ s), eball y Ξ΄ -- we first show that any set is well approximated by a subset of `s`. have main : βˆ€ u : set Ξ±, βˆƒv βŠ† s, Hausdorff_edist u v ≀ Ξ΄, { assume u, let v := {x : Ξ± | x ∈ s ∧ βˆƒy∈u, edist x y < Ξ΄}, existsi [v, ((Ξ»x hx, hx.1) : v βŠ† s)], refine Hausdorff_edist_le_of_mem_edist _ _, { assume x hx, have : x ∈ ⋃y ∈ s, ball y Ξ΄ := hs (by simp), rcases mem_bUnion_iff.1 this with ⟨y, ys, dy⟩, have : edist y x < Ξ΄ := by simp at dy; rwa [edist_comm] at dy, exact ⟨y, ⟨ys, ⟨x, hx, this⟩⟩, le_of_lt dy⟩ }, { rintros x ⟨hx1, ⟨y, yu, hy⟩⟩, exact ⟨y, yu, le_of_lt hy⟩ }}, -- introduce the set F of all subsets of `s` (seen as members of `closeds Ξ±`). let F := {f : closeds Ξ± | f.val βŠ† s}, use F, split, -- `F` is finite { apply @finite_of_finite_image _ _ F (Ξ»f, f.val), { exact subtype.val_injective.inj_on F }, { refine finite_subset (finite_subsets_of_finite fs) (Ξ»b, _), simp only [and_imp, set.mem_image, set.mem_set_of_eq, exists_imp_distrib], assume x hx hx', rwa hx' at hx }}, -- `F` is Ξ΅-dense { assume u _, rcases main u.val with ⟨t0, t0s, Dut0⟩, have : is_closed t0 := closed_of_compact _ (finite_subset fs t0s).compact, let t : closeds Ξ± := ⟨t0, this⟩, have : t ∈ F := t0s, have : edist u t < Ξ΅ := lt_of_le_of_lt Dut0 Ξ΄lt, apply mem_bUnion_iff.2, exact ⟨t, β€Ήt ∈ Fβ€Ί, this⟩ } end⟩ /-- In an emetric space, the type of non-empty compact subsets is an emetric space, where the edistance is the Hausdorff edistance -/ instance nonempty_compacts.emetric_space : emetric_space (nonempty_compacts Ξ±) := { edist := Ξ»s t, Hausdorff_edist s.val t.val, edist_self := Ξ»s, Hausdorff_edist_self, edist_comm := Ξ»s t, Hausdorff_edist_comm, edist_triangle := Ξ»s t u, Hausdorff_edist_triangle, eq_of_edist_eq_zero := Ξ»s t h, subtype.eq $ begin have : closure (s.val) = closure (t.val) := Hausdorff_edist_zero_iff_closure_eq_closure.1 h, rwa [closure_eq_iff_is_closed.2 (closed_of_compact _ s.property.2), closure_eq_iff_is_closed.2 (closed_of_compact _ t.property.2)] at this, end } /-- `nonempty_compacts.to_closeds` is a uniform embedding (as it is an isometry) -/ lemma nonempty_compacts.to_closeds.uniform_embedding : uniform_embedding (@nonempty_compacts.to_closeds Ξ± _ _) := isometry.uniform_embedding $ Ξ»x y, rfl /-- The range of `nonempty_compacts.to_closeds` is closed in a complete space -/ lemma nonempty_compacts.is_closed_in_closeds [complete_space Ξ±] : is_closed (range $ @nonempty_compacts.to_closeds Ξ± _ _) := begin have : range nonempty_compacts.to_closeds = {s : closeds Ξ± | s.val.nonempty ∧ compact s.val}, from range_inclusion _, rw this, refine is_closed_of_closure_subset (Ξ»s hs, ⟨_, _⟩), { -- take a set set t which is nonempty and at a finite distance of s rcases mem_closure_iff.1 hs ⊀ ennreal.coe_lt_top with ⟨t, ht, Dst⟩, rw edist_comm at Dst, -- since `t` is nonempty, so is `s` exact nonempty_of_Hausdorff_edist_ne_top ht.1 (ne_of_lt Dst) }, { refine compact_iff_totally_bounded_complete.2 ⟨_, is_complete_of_is_closed s.property⟩, refine totally_bounded_iff.2 (λΡ Ξ΅pos, _), -- we have to show that s is covered by finitely many eballs of radius Ξ΅ -- pick a nonempty compact set t at distance at most Ξ΅/2 of s rcases mem_closure_iff.1 hs (Ξ΅/2) (ennreal.half_pos Ξ΅pos) with ⟨t, ht, Dst⟩, -- cover this space with finitely many balls of radius Ξ΅/2 rcases totally_bounded_iff.1 (compact_iff_totally_bounded_complete.1 ht.2).1 (Ξ΅/2) (ennreal.half_pos Ξ΅pos) with ⟨u, fu, ut⟩, refine ⟨u, ⟨fu, Ξ»x hx, _⟩⟩, -- u : set Ξ±, fu : finite u, ut : t.val βŠ† ⋃ (y : Ξ±) (H : y ∈ u), eball y (Ξ΅ / 2) -- then s is covered by the union of the balls centered at u of radius Ξ΅ rcases exists_edist_lt_of_Hausdorff_edist_lt hx Dst with ⟨z, hz, Dxz⟩, rcases mem_bUnion_iff.1 (ut hz) with ⟨y, hy, Dzy⟩, have : edist x y < Ξ΅ := calc edist x y ≀ edist x z + edist z y : edist_triangle _ _ _ ... < Ξ΅/2 + Ξ΅/2 : ennreal.add_lt_add Dxz Dzy ... = Ξ΅ : ennreal.add_halves _, exact mem_bUnion hy this }, end /-- In a complete space, the type of nonempty compact subsets is complete. This follows from the same statement for closed subsets -/ instance nonempty_compacts.complete_space [complete_space Ξ±] : complete_space (nonempty_compacts Ξ±) := (complete_space_iff_is_complete_range nonempty_compacts.to_closeds.uniform_embedding).2 $ is_complete_of_is_closed nonempty_compacts.is_closed_in_closeds /-- In a compact space, the type of nonempty compact subsets is compact. This follows from the same statement for closed subsets -/ instance nonempty_compacts.compact_space [compact_space Ξ±] : compact_space (nonempty_compacts Ξ±) := ⟨begin rw embedding.compact_iff_compact_image nonempty_compacts.to_closeds.uniform_embedding.embedding, rw [image_univ], exact nonempty_compacts.is_closed_in_closeds.compact end⟩ /-- In a second countable space, the type of nonempty compact subsets is second countable -/ instance nonempty_compacts.second_countable_topology [second_countable_topology Ξ±] : second_countable_topology (nonempty_compacts Ξ±) := begin haveI : separable_space (nonempty_compacts Ξ±) := begin /- To obtain a countable dense subset of `nonempty_compacts Ξ±`, start from a countable dense subset `s` of Ξ±, and then consider all its finite nonempty subsets. This set is countable and made of nonempty compact sets. It turns out to be dense: by total boundedness, any compact set `t` can be covered by finitely many small balls, and approximations in `s` of the centers of these balls give the required finite approximation of `t`. -/ have : separable_space Ξ± := by apply_instance, rcases this.exists_countable_closure_eq_univ with ⟨s, cs, s_dense⟩, let v0 := {t : set Ξ± | finite t ∧ t βŠ† s}, let v : set (nonempty_compacts Ξ±) := {t : nonempty_compacts Ξ± | t.val ∈ v0}, refine ⟨⟨v, ⟨_, _⟩⟩⟩, { have : countable (subtype.val '' v), { refine countable_subset (Ξ»x hx, _) (countable_set_of_finite_subset cs), rcases (mem_image _ _ _).1 hx with ⟨y, ⟨hy, yx⟩⟩, rw ← yx, exact hy }, apply countable_of_injective_of_countable_image _ this, apply subtype.val_injective.inj_on }, { refine subset.antisymm (subset_univ _) (Ξ»t ht, mem_closure_iff.2 (λΡ Ξ΅pos, _)), -- t is a compact nonempty set, that we have to approximate uniformly by a a set in `v`. rcases dense Ξ΅pos with ⟨δ, Ξ΄pos, Ξ΄lt⟩, -- construct a map F associating to a point in Ξ± an approximating point in s, up to Ξ΄/2. have Exy : βˆ€x, βˆƒy, y ∈ s ∧ edist x y < Ξ΄/2, { assume x, have : x ∈ closure s := by rw s_dense; exact mem_univ _, rcases mem_closure_iff.1 this (Ξ΄/2) (ennreal.half_pos Ξ΄pos) with ⟨y, ys, hy⟩, exact ⟨y, ⟨ys, hy⟩⟩ }, let F := Ξ»x, some (Exy x), have Fspec : βˆ€x, F x ∈ s ∧ edist x (F x) < Ξ΄/2 := Ξ»x, some_spec (Exy x), -- cover `t` with finitely many balls. Their centers form a set `a` have : totally_bounded t.val := (compact_iff_totally_bounded_complete.1 t.property.2).1, rcases totally_bounded_iff.1 this (Ξ΄/2) (ennreal.half_pos Ξ΄pos) with ⟨a, af, ta⟩, -- a : set Ξ±, af : finite a, ta : t.val βŠ† ⋃ (y : Ξ±) (H : y ∈ a), eball y (Ξ΄ / 2) -- replace each center by a nearby approximation in `s`, giving a new set `b` let b := F '' a, have : finite b := finite_image _ af, have tb : βˆ€x ∈ t.val, βˆƒy ∈ b, edist x y < Ξ΄, { assume x hx, rcases mem_bUnion_iff.1 (ta hx) with ⟨z, za, Dxz⟩, existsi [F z, mem_image_of_mem _ za], calc edist x (F z) ≀ edist x z + edist z (F z) : edist_triangle _ _ _ ... < Ξ΄/2 + Ξ΄/2 : ennreal.add_lt_add Dxz (Fspec z).2 ... = Ξ΄ : ennreal.add_halves _ }, -- keep only the points in `b` that are close to point in `t`, yielding a new set `c` let c := {y ∈ b | βˆƒx∈t.val, edist x y < Ξ΄}, have : finite c := finite_subset β€Ήfinite bβ€Ί (Ξ»x hx, hx.1), -- points in `t` are well approximated by points in `c` have tc : βˆ€x ∈ t.val, βˆƒy ∈ c, edist x y ≀ Ξ΄, { assume x hx, rcases tb x hx with ⟨y, yv, Dxy⟩, have : y ∈ c := by simp [c, -mem_image]; exact ⟨yv, ⟨x, hx, Dxy⟩⟩, exact ⟨y, this, le_of_lt Dxy⟩ }, -- points in `c` are well approximated by points in `t` have ct : βˆ€y ∈ c, βˆƒx ∈ t.val, edist y x ≀ Ξ΄, { rintros y ⟨hy1, ⟨x, xt, Dyx⟩⟩, have : edist y x ≀ Ξ΄ := calc edist y x = edist x y : edist_comm _ _ ... ≀ Ξ΄ : le_of_lt Dyx, exact ⟨x, xt, this⟩ }, -- it follows that their Hausdorff distance is small have : Hausdorff_edist t.val c ≀ Ξ΄ := Hausdorff_edist_le_of_mem_edist tc ct, have Dtc : Hausdorff_edist t.val c < Ξ΅ := lt_of_le_of_lt this Ξ΄lt, -- the set `c` is not empty, as it is well approximated by a nonempty set have hc : c.nonempty, from nonempty_of_Hausdorff_edist_ne_top t.property.1 (lattice.ne_top_of_lt Dtc), -- let `d` be the version of `c` in the type `nonempty_compacts Ξ±` let d : nonempty_compacts Ξ± := ⟨c, ⟨hc, β€Ήfinite cβ€Ί.compact⟩⟩, have : c βŠ† s, { assume x hx, rcases (mem_image _ _ _).1 hx.1 with ⟨y, ⟨ya, yx⟩⟩, rw ← yx, exact (Fspec y).1 }, have : d ∈ v := βŸ¨β€Ήfinite cβ€Ί, this⟩, -- we have proved that `d` is a good approximation of `t` as requested exact ⟨d, β€Ήd ∈ vβ€Ί, Dtc⟩ }, end, apply second_countable_of_separable, end end --section end emetric --namespace namespace metric section variables {Ξ± : Type u} [metric_space Ξ±] /-- `nonempty_compacts Ξ±` inherits a metric space structure, as the Hausdorff edistance between two such sets is finite. -/ instance nonempty_compacts.metric_space : metric_space (nonempty_compacts Ξ±) := emetric_space.to_metric_space $ Ξ»x y, Hausdorff_edist_ne_top_of_nonempty_of_bounded x.2.1 y.2.1 (bounded_of_compact x.2.2) (bounded_of_compact y.2.2) /-- The distance on `nonempty_compacts Ξ±` is the Hausdorff distance, by construction -/ lemma nonempty_compacts.dist_eq {x y : nonempty_compacts Ξ±} : dist x y = Hausdorff_dist x.val y.val := rfl lemma lipschitz_inf_dist_set (x : Ξ±) : lipschitz_with 1 (Ξ» s : nonempty_compacts Ξ±, inf_dist x s.val) := lipschitz_with.of_le_add $ assume s t, by { rw dist_comm, exact inf_dist_le_inf_dist_add_Hausdorff_dist (edist_ne_top t s) } lemma lipschitz_inf_dist : lipschitz_with 2 (Ξ» p : Ξ± Γ— (nonempty_compacts Ξ±), inf_dist p.1 p.2.val) := @lipschitz_with.uncurry' _ _ _ _ _ _ (Ξ» (x : Ξ±) (s : nonempty_compacts Ξ±), inf_dist x s.val) 1 1 (Ξ» s, lipschitz_inf_dist_pt s.val) lipschitz_inf_dist_set lemma uniform_continuous_inf_dist_Hausdorff_dist : uniform_continuous (Ξ»p : Ξ± Γ— (nonempty_compacts Ξ±), inf_dist p.1 (p.2).val) := lipschitz_inf_dist.uniform_continuous end --section end metric --namespace
56cac4f3afd7558ab1d3141145e2287adfe88da9
ae1c30bdeac9e0b1ceea786a6c72c058f01b2eed
/tools/auto/auto.lean
76ae0abddb051e02b268880424c29b08f272cdb6
[]
no_license
Kha/library_dev
7e88c7592b43b939f053dabd708944f22c09627a
812d543fbf732df80c31d0fdbfae700e6f8535d2
refs/heads/master
1,610,378,810,349
1,487,673,201,000
1,487,673,201,000
80,344,419
0
0
null
1,485,692,341,000
1,485,692,341,000
null
UTF-8
Lean
false
false
45,989
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad Automated tableaux reasoners, inspired by the ones in Isabelle. clarify : applies safe rules only, doesn't split goals safe : applies safe rules only, though it may split goals auto : applies safe rules, and then does a backtracking search with unsafe rules. All can be called in classical or intuitionistic mode, and all can use the simplifier as well to simplify the goal and hypotheses. To do: - use attribute manager to keep track of rules - use tactics to build introduction and elimination rules automatically - need version of apply that doesn't instantiate metavariables - bound split depth in safe, backtracking depth in force, and iterations in clarify - rules should match goal w/ or w/out reduction to whnf (reducible) - improve tracing output by keeping track of backtracking depth - provide better error messages when the name of a theorem doesn't exist; for example, replace mk_const with mk_const_with_warning (when installing the rule?) - write a splitter, to help simp - use a backtracking version of reflexivity - do more instantiations of quantifiers - with bintro and belim rules, "num_subgoals" is really "num_branches" - for intuitionistic logic, add a safe elimination rule for A β†’ B, A, and also for A ∨ B β†’ C, and similarly for (βˆƒ x, A x) β†’ C - in fact, for intuitionistic logic, add the rules for Dyckhoff's LJT - add safe rules for quantifiers, e.g. βˆ€ x, P x |- P a Questions: - In backtracking search, use better selection of rules, e.g. to chose those with fewest subgoals? - Should calls to simplifier use clarify as the prover? - Use more sophisticated handling of quantifiers? - Should we ever call cases, or induction? Maybe with user hints? - Better handling of equality? E.g. use symmetry as an elim rule? - When backtracking, can we intelligently detect that some subgoals can be thrown away? For example, in the intuitionistic elim rule for A β†’ B, we may end up not using B. The tactic that handles that can detect this. - Check and remove duplicate hypotheses? Note: the rules are not complete for intuitionistic propositional logic, which may require using an elim rule on A β†’ B multiple times. -/ open expr tactic list nat universe uβ‚— declare_trace auto set_option trace.auto false /- logic rules for the tableau prover -/ theorem not_or_of_imp {A B : Prop} (H : A β†’ B) : Β¬ A ∨ B := or.elim (classical.em A) (Ξ» H', or.inr (H H')) (Ξ» H', or.inl H') lemma classical_swap {A : Prop} (B : Prop) (H₁ : Β¬ A) (Hβ‚‚ : Β¬ B β†’ A) : B := classical.by_contradiction (Ξ» H, H₁ (Hβ‚‚ H)) theorem imp_classical_elim {A B C : Prop} (H : A β†’ B) (H₁ : Β¬ A β†’ C) (Hβ‚‚ : B β†’ C) : C := or.elim (not_or_of_imp H) (Ξ» H', H₁ H') (Ξ» H', Hβ‚‚ H') theorem imp_intuit_elim {A B C : Prop} (H : A β†’ B) (H₁ : A) (Hβ‚‚ : B β†’ C) : C := Hβ‚‚ (H H₁) theorem or_classical_intro {A B : Prop} (H : Β¬ A β†’ B) : A ∨ B := or.elim (classical.em A) (Ξ» H', or.inl H') (Ξ» H', or.inr (H H')) theorem iff_elim {A B C : Prop} (H : A ↔ B) (H' : (A β†’ B) β†’ (B β†’ A) β†’ C) : C := iff.elim H' H theorem exists.intro2 {A : Type} {P : A β†’ Prop} (a₁ aβ‚‚ : A) (H : P a₁ ∨ P aβ‚‚) : βˆƒ x, P x := or.elim H (Ξ» H', exists.intro _ H') (Ξ» H', exists.intro _ H') theorem forall_elim {A : Type} {P : A β†’ Prop} {C : Prop} (H : βˆ€ x, P x) {y : A} (H' : P y β†’ C) : C := H' (H y) theorem forall_elim2 {A : Type} {P : A β†’ Prop} {C : Prop} (H : βˆ€ x, P x) {y₁ yβ‚‚ : A} (H' : P y₁ ∧ P yβ‚‚ β†’ C) : C := H' (and.intro (H y₁) (H yβ‚‚)) theorem not_true_elim {C : Prop} (H : Β¬ true) : C := false.elim (H trivial) theorem not_of_not_or_left {A B : Prop} (H : Β¬ (A ∨ B)) : Β¬ A := Ξ» H', H (or.inl H') theorem not_of_not_or_right {A B : Prop} (H : Β¬ (A ∨ B)) : Β¬ B := Ξ» H', H (or.inr H') theorem exists_not_of_not_forall {A : Type} {P : A β†’ Prop} (H : Β¬ βˆ€ x, P x) : βˆƒ x, Β¬ P x := classical.by_contradiction (assume H' : Β¬ βˆƒ x, Β¬ P x, H (take x, show P x, from classical.by_contradiction (Ξ» H'', H' (exists.intro x H'')))) theorem not_not_dest {A : Prop} (H : Β¬ Β¬ A) : A := classical.by_contradiction (Ξ» H', H H') theorem not_not_not_dest {A : Prop} (H : Β¬ Β¬ Β¬ A) : Β¬ A := Ξ» H', H (Ξ» H'', H'' H') theorem not_not_of_not_imp {A B : Prop} (H : Β¬ (A β†’ B)) : Β¬ Β¬ A := Ξ» H', H (Ξ» H'', absurd H'' H') theorem of_not_imp {A B : Prop} (H : Β¬ (A β†’ B)) : A := not_not_dest (not_not_of_not_imp H) theorem not_of_not_imp {A B : Prop} (H : Β¬ (A β†’ B)) : Β¬ B := Ξ» H', H (Ξ» H'', H') theorem not_or_not_of_not_and {A B : Prop} (H : Β¬ (A ∧ B)) : Β¬ A ∨ Β¬ B := or.elim (classical.em A) (Ξ» HA, or.inr (Ξ» HB, H (and.intro HA HB))) (Ξ» HnA, or.inl HnA) theorem contrapos {A B : Prop} (H : A β†’ B) : Β¬ B β†’ Β¬ A := Ξ» H₁ Hβ‚‚, H₁ (H Hβ‚‚) theorem not_iff {A B : Prop} (H : Β¬ (A ↔ B)) : Β¬ ((A β†’ B) ∧ (B β†’ A)) := Ξ» H', H ((iff_iff_implies_and_implies A B)^.mpr H') theorem not_of_imp_false {A : Prop} (H : A β†’ false) : Β¬ A := H theorem imp_of_or_imp_left {A B C : Prop} (H : A ∨ B β†’ C) : A β†’ C := Ξ» H', H (or.inl H') theorem imp_of_or_imp_right {A B C : Prop} (H : A ∨ B β†’ C) : B β†’ C := Ξ» H', H (or.inr H') namespace tactic /- utils -/ meta def collect_props : list expr β†’ tactic (list expr) | [] := return [] | (h :: hs) := do props ← collect_props hs, ht ← infer_type h, htt ← infer_type ht, (unify htt prop >> return (h :: props)) <|> return props meta def unfold_all (ns : list name) : tactic unit := do dunfold ns, local_context >>= collect_props >>= monad.mapm' (dunfold_at ns) meta def head_symbol : expr β†’ name | (const n a) := n | (app e a) := match (get_app_fn e) with | (const n l) := n | a := `none end | (pi a₁ aβ‚‚ a₃ aβ‚„) := `pi | a := `none private meta def whnf_red (e : expr) : tactic expr := whnf e reducible meta def is_forall (e : expr) : tactic bool := if head_symbol e β‰  `pi then return ff else do et ← infer_type e, if et β‰  prop then return ff else do dt ← infer_type (binding_domain e), if dt β‰  prop then return tt else return ff meta def is_negation (e : expr) : tactic bool := do e' ← whnf_red e, if head_symbol e' = `not then return tt else if is_pi e' = tt then (do b' ← whnf_red (binding_body e'), cond (is_false b') (return tt) (return ff)) else return ff meta def at_least_once (t : tactic unit) : tactic unit := t >> repeat t -- assert_fresh P infers the type T of P, creates a fresh name H, and -- asserts H : T meta def assert_fresh (P : expr) : tactic unit := do n ← mk_fresh_name, t ← infer_type P, assertv n t P meta def expr_with_type_to_string (h : expr) : tactic string := do ht ← infer_type h, pph ← pp h, ppht ← pp ht, return (to_string pph ++ " : " ++ to_string ppht) /- versions of the simplifier that call themselves recursively -/ -- FIXME(gabriel): figure out how to provide custom tactic to simplifier meta def simplify_goal' (t : command) (lemmas : list expr) : command := simp_using lemmas meta def simp_core_at' (t : command) (lemmas : list expr) (h : expr) : command := simp_at h lemmas -- simp_add_prove_max_depth l d uses the simplifier as its own prover, recursing up to depth d meta def simp_add_prove_max_depth (lemmas : list expr) : β„• β†’ tactic unit | 0 := failed | (succ d) := do l ← local_context >>= collect_props, simplify_goal' (simp_add_prove_max_depth d) (l ++ lemmas), triv meta def strong_simp_add (lemmas : list expr) : tactic unit := do l ← local_context >>= collect_props, simplify_goal' (simp_add_prove_max_depth lemmas 10) (l ++ lemmas), try triv meta def strong_simp : tactic unit := strong_simp_add [] meta def strong_simp_at_add (h : expr) (lemmas : list expr) : tactic unit := do simp_core_at' (simp_add_prove_max_depth lemmas 10) lemmas h meta def strong_simp_at (h : expr) : tactic unit := do strong_simp_at_add h [] -- TODO: how to inline this? private meta def strong_simp_hyps_add_aux (lemmas : list expr) : list expr β†’ tactic unit | [] := skip | (h :: hs) := try (strong_simp_at_add h lemmas) >> strong_simp_hyps_add_aux hs meta def strong_simp_hyps_add (lemmas : list expr) : tactic unit := do l ← local_context, strong_simp_hyps_add_aux lemmas l meta def strong_simp_hyps : tactic unit := strong_simp_hyps_add [] /- These are for tracing. We use a thunk to avoid computing a string when it is not needed. -/ -- show a trace message meta def auto_trace (s : unit β†’ string) : tactic unit := if is_trace_enabled_for `auto = tt then trace (s ()) else skip -- a version where the string is in the tactic monad meta def auto_traceM (s : unit β†’ tactic string) : tactic unit := if is_trace_enabled_for `auto = tt then s () >>= trace else skip -- trace a step, e.g. an application of a rule, and show the result meta def auto_trace_step (tac : tactic unit) (s : unit β†’ string) : tactic unit := if is_trace_enabled_for `auto = tt then do trace (s ()), (tac >> trace ("result:") >> trace_state >> trace "-----") <|> (trace ("failed:") >> trace_state >> trace "-----" >> failed) else tac -- a version where the string is in the tactic monad meta def auto_trace_stepM (tac : tactic unit) (s : unit β†’ tactic string) : tactic unit := if is_trace_enabled_for `auto = tt then do s () >>= trace, (tac >> trace ("result:") >> trace_state >> trace "-----") <|> (trace ("failed:") >> trace_state >> trace "-----" >> failed) else tac -- this can be used to print a message after a tactic if it fails, e.g. a continuation. meta def auto_trace_with_fail_message (tac : tactic unit) (s : unit β†’ string) : tactic unit := if is_trace_enabled_for `auto = tt then do tac <|> (trace (s ()) >> failed) else tac meta def auto_trace_with_fail_messageM (tac : tactic unit) (s : unit β†’ tactic string) : tactic unit := if is_trace_enabled_for `auto = tt then do tac <|> (s () >>= trace >> failed) else tac /- Safe versions of some tactics, i.e. tactics that do not instantiate metavariables and hence can be applied in safe mode. -/ check unify -- we really want: e₁ and eβ‚‚ can be unified without instantiating metavariables meta def unify_safe_core (t : transparency) (e₁ eβ‚‚ : expr) : tactic unit := cond (has_meta_var e₁ || has_meta_var eβ‚‚) failed (unify e₁ eβ‚‚ t) meta def unify_safe (e₁ eβ‚‚ : expr) : tactic unit := unify_safe_core semireducible e₁ eβ‚‚ check apply -- we really want: try to apply e, without instantiation any metavariables in the goal -- maybe we also want the same for fapply? meta def apply_safe_core (t : transparency) (all : bool) (insts : bool) (e : expr) : tactic (list expr) := apply_core e {md := t, approx := tt, all := all, use_instances := insts } meta def apply_safe (e : expr) : tactic (list expr) := apply_core e {md := semireducible, approx := ff, all := tt} /- a safe version of assumption -/ meta def find_same_type_safe (e : expr) (l : list expr) : tactic expr := first $ list.for l (Ξ» h, do ht ← infer_type h, unify_safe e ht >> return h) meta def find_hyp_with_type (e : expr) : tactic expr := local_context >>= find_same_type_safe e meta def assumption_safe : tactic unit := do goal ← target, h ← find_hyp_with_type goal, auto_trace_stepM (exact h) (Ξ» u, do s ← expr_with_type_to_string h, return ("applying assumption " ++ s)) /- a safe version of contradiction -/ private meta def contra_A_not_A_safe : list expr β†’ list expr β†’ tactic unit | [] Hs := failed | (H1 :: Rs) Hs := do t_0 ← infer_type H1, t ← whnf t_0, (do a ← match_not t, H2 ← find_same_type_safe a Hs, tgt ← target, pr ← mk_app `absurd [tgt, H2, H1], auto_trace_stepM (exact pr) (Ξ» u, do s2 ← expr_with_type_to_string H2, s1 ← expr_with_type_to_string H1, return ("using contradiction, " ++ s2 ++ ", " ++ s1))) <|> contra_A_not_A_safe Rs Hs meta def contradiction_safe : tactic unit := do ctx ← local_context, contra_A_not_A_safe ctx ctx /- The structure storing a rule has the following data: key : name := the head symbol that triggers the rule num_subgoals : nat := number of subgoals introduced classical : bool := whether to use in classical mode intuit : bool := whether to use in intuitionistic mode tac : ... := the tactic used to execute the rule Notice that all the work is done by tac, which is arbitrary. Helper functions build suitable tactics in common situations, but users can write more complicated ones. All the other data is used to find the rules quickly and decide when to apply them. Currently, the only thing that varies is the type of the tactic, so this is given as a parameter: intro_rule == tactic unit elim_rule == expr β†’ tactic unit bintro_rule == tactic unit β†’ tactic unit belim_rule == tactic unit β†’ expr β†’ tactic unit Intro rules are keyed on the head symbol of the goal. Elim rules are keyed on the head symbol of a hypothesis, and take that hypothesis as an argument. We actually have a separate rule database for rules where they head symbol is a negation, keyed to the next head symbol. The intro and elim rules should be safe, which is to say, they can be applied without backtracking. In the other rules, the letter "b" is for "backtracking." Those rules take continuations that carry out the rest of the search, so that they can backtrack on failure. Note that many some elimination rules that would otherwise be safe become unsafe when there are metavariables involved. For example, applying (or.elim H) is unsafe if H has metavariables; if those metavariables are not instantiated by the end of the search, then the attempt was unsuccessful, and needs to be retracted. So there are both safe and unsafe versions of the rule for or. -/ structure rule_data (A : Type) := (key : name) (num_subgoals : β„•) (classical : bool) (intuit : bool) (tac : A) meta def rule_key {A : Type} : rule_data A β†’ name := rule_data.key meta def intro_rule : Type := rule_data (tactic unit) meta def elim_rule : Type := rule_data (expr β†’ tactic unit) meta def bintro_rule : Type := rule_data (tactic unit β†’ tactic unit) meta def belim_rule : Type := rule_data (expr β†’ tactic unit β†’ tactic unit) meta def rule_database (A : Type) : Type := rb_lmap name (rule_data A) meta def intro_rule_database : Type := rb_lmap name intro_rule meta def elim_rule_database : Type := rb_lmap name elim_rule meta def nelim_rule_database : Type := rb_lmap name elim_rule meta def bintro_rule_database : Type := rb_lmap name bintro_rule meta def belim_rule_database : Type := rb_lmap name belim_rule meta def bnelim_rule_database : Type := rb_lmap name belim_rule meta def mk_rule_database (A : Type) : rule_database A := rb_lmap.mk _ _ meta def insert_rule {A : Type} (db : rule_database A) (r : rule_data A) : rule_database A := rb_lmap.insert db (rule_key r) r meta def insert_rule_list {A : Type} (db : rule_database A) : list (rule_data A) β†’ rule_database A | [] := db | (r :: rs) := insert_rule (insert_rule_list rs) r meta def initialize_rule_database {A : Type} (l : list (rule_data A)) : rule_database A := insert_rule_list (mk_rule_database A) l meta def find_rules {A : Type} (db : rule_database A) (key : name) : list (rule_data A) := rb_lmap.find db key /- set up attributes -/ meta def intro_rule_database_of_list_name (ns : list name) : tactic (intro_rule_database) := do env ← get_env, rule_list ← monad.for ns (Ξ» n, do e ← mk_const n, eval_expr intro_rule e), return (initialize_rule_database rule_list) meta def intro_rule_attr : caching_user_attribute (intro_rule_database) := { name := `auto.intro_rule, descr := "intro rule for tableau provers", mk_cache := intro_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``intro_rule_attr meta def elim_rule_database_of_list_name (ns : list name) : tactic (elim_rule_database) := do env ← get_env, rule_list ← monad.for ns (Ξ» n, do e ← mk_const n, eval_expr elim_rule e), return (initialize_rule_database rule_list) meta def elim_rule_attr : caching_user_attribute (elim_rule_database) := { name := `auto.elim_rule, descr := "elim rule for tableau provers", mk_cache := elim_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``elim_rule_attr meta def nelim_rule_attr : caching_user_attribute (elim_rule_database) := { name := `auto.nelim_rule, descr := "negated elim rule for tableau provers", mk_cache := elim_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``nelim_rule_attr meta def bintro_rule_database_of_list_name (ns : list name) : tactic (bintro_rule_database) := do env ← get_env, rule_list ← monad.for ns (Ξ» n, do e ← mk_const n, eval_expr bintro_rule e), return (initialize_rule_database rule_list) meta def bintro_rule_attr : caching_user_attribute (bintro_rule_database) := { name := `auto.bintro_rule, descr := "backtracking intro rule for tableau provers", mk_cache := bintro_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``bintro_rule_attr meta def belim_rule_database_of_list_name (ns : list name) : tactic (belim_rule_database) := do env ← get_env, rule_list ← monad.for ns (Ξ» n, do e ← mk_const n, eval_expr belim_rule e), return (initialize_rule_database rule_list) meta def belim_rule_attr : caching_user_attribute (belim_rule_database) := { name := `auto.belim_rule, descr := "backtracking elim rule for tableau provers", mk_cache := belim_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``belim_rule_attr meta def bnelim_rule_attr : caching_user_attribute (belim_rule_database) := { name := `auto.bnelim_rule, descr := "backtracking negated elim rule for tableau provers", mk_cache := belim_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``bnelim_rule_attr /- intro rules -/ meta def apply_intro_rule (db : intro_rule_database) (max_subgoals : β„•) (classical : bool) : tactic unit := do goal ← target >>= whnf_red, first $ list.for (find_rules db (head_symbol goal)) (Ξ» r, if rule_data.num_subgoals r ≀ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r else failed) /- procedures for building particular intro rules -/ meta def deploy_intro (op : name) : tactic unit := auto_trace_step (mk_const op >>= apply) (Ξ» u, "applying introduction " ++ to_string op) meta def deploy_intro_then_intros (op : name) : tactic unit := auto_trace_step (mk_const op >>= apply >> intros >> return ()) (Ξ» u, "applying introduction " ++ to_string op) /- elim rules -/ meta def apply_elim_rule_at (edb : elim_rule_database) (nedb : nelim_rule_database) (h : expr) (max_subgoals : β„•) (classical : bool) : tactic unit := do ht ← infer_type h >>= whnf_red, (first $ list.for (find_rules edb (head_symbol ht)) (Ξ» r, if rule_data.num_subgoals r ≀ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h else failed)) <|> if head_symbol ht = `not then do unneg ← return (app_arg ht) >>= whnf_red, first $ list.for (find_rules nedb (head_symbol (unneg))) (Ξ» r, if rule_data.num_subgoals r ≀ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h else failed) else failed meta def apply_elim_rule (edb : elim_rule_database) (nedb : nelim_rule_database) (max_subgoals : β„•) (classical : bool) : tactic unit := do hs ← local_context >>= collect_props, first $ list.for hs (Ξ» h, apply_elim_rule_at edb nedb h max_subgoals classical) /- procedures for building particular elim rules general elimination rules: All the arguments are assumed to be inferrable from the motive and major premise. The rule is not applied if the hypothesis has metavariables -- backtracking is needed for that. destruct rules: This eliminates a hypothesis by applying a single theorem or a list of theorems in the forward direction. The arguments are assume to be inferrable from the premise. It is safe even if the hypothesis has variables. -/ private meta def elim_instance_mapp_args (motive major : β„•) (emotive emajor : expr) : list (option expr) := let diff := major - motive in nat.rec_on major [] (Ξ» n l, if n = diff then some emotive :: l else if n = 0 then some emajor :: l else none :: l) meta def deploy_elim_at (op : name) (motive : β„•) (major : β„•) : expr β†’ tactic unit := Ξ» h : expr, do auto_trace_stepM (do goal ← target, el ← mk_mapp op (elim_instance_mapp_args motive major goal h), clear h, apply el ; (intros >> skip), return ()) (Ξ» u, do s ← expr_with_type_to_string h, return ("applying elimination " ++ to_string op ++ " at " ++ s)) -- only apply the elim rule if there are no metavars meta def deploy_elim_at_safe (op : name) (motive : β„•) (major : β„•) : expr β†’ tactic unit := Ξ» h : expr, do ht ← infer_type h, when (has_meta_var ht = tt) failed, deploy_elim_at op motive major h private meta def dest_instance_mapp_args (prem : β„•) (hyp : expr) : list (option expr) := nat.rec_on (prem - 1) [some hyp] (Ξ» n l, none :: l) meta def deploy_dest_at (op : name) (prem : β„•) : expr β†’ tactic unit := Ξ» h : expr, auto_trace_stepM (mk_mapp op (dest_instance_mapp_args prem h) >>= assert_fresh >> clear h) (Ξ» u, do s ← expr_with_type_to_string h, return ("applying destructor " ++ to_string op ++ " at " ++ s)) meta def deploy_dests_at (ops : list (name Γ— β„•)) : expr β†’ tactic unit := Ξ» h : expr, auto_trace_stepM (monad.for' ops (Ξ» p, mk_mapp (p.1) (dest_instance_mapp_args (p.2) h) >>= assert_fresh) >> clear h) (Ξ» u, do s ← expr_with_type_to_string h, return ("applying destructors " ++ (map prod.fst ops)^.to_string ++ " at " ++ s)) meta def deploy_clear_at : expr β†’ tactic unit := Ξ» h : expr, auto_trace_stepM (clear h) (Ξ» u, do s ← expr_with_type_to_string h, return ("clearing " ++ s)) -- convert (... h : Β¬ A ... ==> B) to (... hn : Β¬ B ... ==> A), where h' has a fresh name meta def do_classical_swap (h : expr) : tactic expr := do goal ← target, mk_mapp `classical_swap [none, some goal, some h] >>= apply, clear h, mk_fresh_name >>= intro meta def classical_apply_intro_rule_at (db : intro_rule_database) (h : expr) (max_subgoals : β„•) (classical : bool) : tactic unit := do n ← mk_fresh_name, negated_concl ← do_classical_swap h, apply_intro_rule db max_subgoals classical ; (intros >> do_classical_swap negated_concl >> skip) /- backtracking intro rules -/ meta def apply_bintro_rule (db : bintro_rule_database) (max_subgoals : β„•) (classical : bool) (cont : tactic unit) : tactic unit := do goal ← target >>= whnf_red, first $ list.for (find_rules db (head_symbol goal)) (Ξ» r, if rule_data.num_subgoals r ≀ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r cont else failed) /- procedure for building particular bintro rules -/ meta def deploy_bintro_choices (l : list (tactic unit)) : tactic unit β†’ tactic unit := take cont, first $ list.for l (Ξ» t, do auto_trace (Ξ» u, "setting backtracking point for intro rule"), t, auto_trace_with_fail_message cont (Ξ» u, "backtracking intro rule")) /- backtracking elim rules -/ meta def classical_apply_bintro_rule_at (db : bintro_rule_database) (h : expr) (max_subgoals : β„•) (classical : bool) (cont : tactic unit) : tactic unit := do n ← mk_fresh_name, negated_concl ← do_classical_swap h, apply_bintro_rule db max_subgoals classical (intros >> do_classical_swap negated_concl >> cont) meta def apply_belim_rule_at (bedb : belim_rule_database) (bnedb : belim_rule_database) (h : expr) (max_subgoals : β„•) (classical : bool) (cont : tactic unit) : tactic unit := do ht ← infer_type h >>= whnf_red, (first $ list.for (find_rules bedb (head_symbol ht)) (Ξ» r, if rule_data.num_subgoals r ≀ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h cont else failed)) <|> (monad.cond (is_negation ht) (do dt ← infer_type (binding_domain h), first $ list.for (find_rules bnedb (head_symbol dt)) (Ξ» r, if rule_data.num_subgoals r ≀ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h cont else failed)) failed) meta def apply_belim_rule (bedb : belim_rule_database) (bnedb : belim_rule_database) (max_subgoals : β„•) (classical : bool) (cont : tactic unit) : tactic unit := do hs ← local_context >>= collect_props, first (list.for hs (Ξ» h, apply_belim_rule_at bedb bnedb h max_subgoals classical cont)) /- procedure for building particular belim rules -/ meta def deploy_belim_choices (l : list (expr β†’ tactic unit)) : expr β†’ tactic unit β†’ tactic unit := take h cont, (first $ list.for l (Ξ» t, do auto_traceM (Ξ» u, do s ← expr_with_type_to_string h, return ("setting backtracking point for elim rule at " ++ s)), t h, auto_trace_with_fail_messageM cont (Ξ» u, do s ← expr_with_type_to_string h, return ("backtracking elim rule at " ++ s)))) /- try to do a subst or injection on a hypothesis -/ meta def has_eq_type (h : expr) : tactic bool := do htype ← infer_type h >>= whnf_red, return (match (expr.is_eq htype) with (some _) := tt | none := ff end) meta def try_subst_and_injection_on_hyps : tactic unit := do ctx ← local_context, first $ list.for ctx (Ξ» h, do b ← has_eq_type h, when (b = ff) failed, (do subst h, auto_trace_stepM skip (Ξ» u, do s ← expr_with_type_to_string h, return ("performing subst with " ++ s)), clear h) <|> (do injection h, auto_trace_stepM skip (Ξ» u, do s ← expr_with_type_to_string h, return ("performing injection with " ++ s)), clear h)) /- Standard rule sets -/ /- standard introduction rules -/ @[auto.intro_rule] meta def true_intro_rule : intro_rule := { key := ``true, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_intro ``true.intro } @[auto.intro_rule] meta def and_intro_rule : intro_rule := { key := ``and, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_intro ``and.intro } @[auto.intro_rule] meta def or_classical_intro_rule : intro_rule := { key := ``or, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_intro_then_intros ``or_classical_intro } -- TODO: eliminate trick to get the recursive call private meta def auto_intros_aux : unit β†’ tactic unit | unit.star := do goal ← target >>= whnf_red, when (head_symbol goal = `pi ∨ head_symbol goal = `not) (do n ← mk_fresh_name, intro n, auto_intros_aux unit.star) meta def auto_intros : tactic unit := auto_intros_aux unit.star meta def deploy_intros : tactic unit := auto_trace_step auto_intros (Ξ» u, "applying intros") @[auto.intro_rule] meta def Pi_intro_rule : intro_rule := { key := `pi, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_intros } @[auto.intro_rule] meta def not_intro_rule : intro_rule := { key := ``not, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_intros } @[auto.intro_rule] meta def iff_intro_rule : intro_rule := { key := ``iff, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_intro ``iff.intro } /- standard backtracking intro rules -/ @[auto.bintro_rule] meta def or_intuit_bintro_rule : bintro_rule := { key := ``or, num_subgoals := 2, classical := ff, intuit := tt, tac := deploy_bintro_choices [deploy_intro ``or.inl, deploy_intro ``or.inr] } @[auto.bintro_rule] meta def exists_bintro_rule : bintro_rule := { key := ``Exists, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_bintro_choices [deploy_intro ``exists.intro, deploy_intro ``false.elim] } /- standard elimination rules -/ @[auto.elim_rule] meta def and_elim_rule : elim_rule := { key := ``and, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dests_at [(``and.left, 3), (``and.right, 3)] } @[auto.elim_rule] meta def iff_elim_rule : elim_rule := { key := ``iff, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dests_at [(``iff.mp, 3), (``iff.mpr, 3)] } @[auto.elim_rule] meta def or_elim_rule : elim_rule := { key := ``or, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_elim_at_safe ``or.elim 3 4 } @[auto.elim_rule] meta def false_elim_rule : elim_rule := { key := ``false, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_elim_at ``false.elim 1 2 } @[auto.elim_rule] meta def exists_elim_rule : elim_rule := { key := ``Exists, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_elim_at_safe ``exists.elim 3 4 } -- given a hypothesis h with type ht, an implication, try to find something -- in the context to apply it to meta def try_modus_ponens_at (h : expr) (ht : expr) : tactic unit := do h' ← find_hyp_with_type (binding_domain ht), auto_trace_stepM (assert_fresh (expr.app h h') >> clear h) (Ξ» u, do s₁ ← expr_with_type_to_string h, sβ‚‚ ← expr_with_type_to_string h', return ("applying " ++ s₁ ++ " to " ++ sβ‚‚)) -- if h is of the form A β†’ B: -- if B = false, replace by h' : Β¬ A -- if h' : A is in the context, apply h to h' -- if A if of the form C ∨ D, replace with C β†’ B, D β†’ B meta def deploy_imp_elim_at (h : expr) : tactic unit := do ht ← infer_type h >>= whnf_red, dt ← infer_type (binding_domain ht), if dt β‰  prop then failed else do conc ← return (binding_body ht) >>= whnf_red, if head_symbol conc = ``false then deploy_dest_at ``not_of_imp_false 2 h else try_modus_ponens_at h ht <|> (do hyp ← return (binding_domain ht) >>= whnf_red, if head_symbol hyp = `or then deploy_dests_at [(``imp_of_or_imp_left, 4), (``imp_of_or_imp_right, 4)] h else failed) @[auto.elim_rule] meta def imp_elim_rule : elim_rule := { key := `pi, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_imp_elim_at } meta def deploy_imp_classical_elim_at (h : expr) : tactic unit := do ht ← infer_type h >>= whnf_red, dt ← infer_type (binding_domain ht), if dt β‰  prop then failed else monad.cond (is_negation ht) failed (deploy_elim_at ``imp_classical_elim 3 4 h) @[auto.elim_rule] meta def imp_classical_elim_rule : elim_rule := { key := `pi, num_subgoals := 2, classical := tt, intuit := ff, tac := deploy_imp_classical_elim_at } -- try to find a contradiction meta def deploy_not_elim_at (h : expr) : tactic unit := do ht ← infer_type h, h' ← find_hyp_with_type (app_arg ht), goal ← target, t ← mk_app `absurd [goal, h', h], auto_trace_stepM (exact t) (Ξ» u, do s₁ ← expr_with_type_to_string h', sβ‚‚ ← expr_with_type_to_string h, return ("using contradiction, " ++ s₁ ++ " and " ++ sβ‚‚)) @[auto.elim_rule] meta def not_elim_rule : elim_rule := { key := `not, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_not_elim_at } /- elimination rules for negated formulas -/ @[auto.nelim_rule] meta def not_true_elim_rule : elim_rule := { key := ``true, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_elim_at ``not_true_elim 1 2 } @[auto.nelim_rule] meta def not_or_elim_rule : elim_rule := { key := ``or, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dests_at [(``not_of_not_or_left, 3), (``not_of_not_or_right, 3)] } @[auto.nelim_rule] meta def not_and_elim_rule : elim_rule := { key := ``and, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_dest_at ``not_or_not_of_not_and 3 } @[auto.nelim_rule] meta def not_imp_elim_rule : elim_rule := { key := `pi, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_dests_at [(``of_not_imp, 3), (``not_of_not_imp, 3)] } @[auto.nelim_rule] meta def not_not_elim_rule : elim_rule := { key := ``not, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_dest_at ``not_not_dest 2 } @[auto.nelim_rule] meta def not_not_not_elim_rule : elim_rule := { key := ``not, num_subgoals := 1, classical := ff, intuit := tt, tac := deploy_dest_at ``not_not_not_dest 2 } @[auto.nelim_rule] meta def not_iff_elim_rule : elim_rule := { key := ``iff, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dest_at ``not_iff 3 } @[auto.nelim_rule] meta def not_exists_elim_rule : elim_rule := { key := ``Exists, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dest_at ``forall_not_of_not_exists 3 } /- standard backtracking elim rules -/ meta def deploy_imp_intuit_belim_at (h : expr) (cont : tactic unit) : tactic unit := do ht ← infer_type h >>= whnf_red, dt ← infer_type (binding_domain ht), if dt β‰  prop then failed else deploy_belim_choices [deploy_elim_at ``imp_intuit_elim 3 4, deploy_clear_at] h cont @[auto.belim_rule] meta def imp_intuit_belim_rule : belim_rule := { key := `pi, num_subgoals := 2, classical := ff, intuit := tt, tac := deploy_imp_intuit_belim_at } --meta def imp_classical_belim_rule := --{ key := `pi, num_subgoals := 2, classical := tt, intuit := ff, -- tac := deploy_belim_choices [deploy_clear_at, deploy_imp_classical_elim_at] } @[auto.belim_rule] meta def or_belim_rule : belim_rule := { key := `or, num_subgoals := 2, classical := ff, intuit := tt, tac := deploy_belim_choices [deploy_clear_at, deploy_elim_at ``or.elim 3 4] } /- standard backtracking negated elim rules -/ -- none yet /- backtracking assumption tactic -/ meta def try_assumptions (cont : tactic unit) := do ctx ← local_context, goal ← target, first $ list.for ctx (Ξ» h, do ht ← infer_type h, unify ht goal, auto_trace_stepM (exact h) (Ξ» u, do s ← expr_with_type_to_string h, return ("try applying assumption " ++ s)), auto_trace_with_fail_messageM cont (Ξ» u, do s ← expr_with_type_to_string h, return ("backtracking assumption " ++ s))) meta def try_contradictions (cont : tactic unit) := do ctx ← local_context, goal ← target, first $ list.for ctx (Ξ» h, do ht ← infer_type h, unneg_ht ← match_not ht, first $ list.for ctx (Ξ» h', do ht' ← infer_type h', unify ht' unneg_ht, t ← mk_app ``absurd [goal, h', h], auto_trace_stepM (exact t) (Ξ» u, do s₁ ← expr_with_type_to_string h', sβ‚‚ ← expr_with_type_to_string h, return ("try using contradiction " ++ s₁ ++ ", " ++ sβ‚‚)), auto_trace_with_fail_messageM cont (Ξ» u, do s₁ ← expr_with_type_to_string h', sβ‚‚ ← expr_with_type_to_string h, return ("backtracking contradiction " ++ s₁ ++ ", " ++ sβ‚‚)))) /- instantiating quantifiers in the backtracking search -/ meta def has_forall_type (h : expr) : tactic bool := do ht ← infer_type h, is_forall ht -- TODO: eliminate meta def apply_to_metavars_while_universal_aux : unit β†’ expr β†’ tactic expr | unit.star h := do ht ← infer_type h, if head_symbol ht β‰  `pi then return h else do htt ← infer_type ht, if htt β‰  prop then return h else do dt ← infer_type (binding_domain ht), if dt = prop then return h else do v ← mk_meta_var (binding_domain ht), apply_to_metavars_while_universal_aux unit.star (expr.app h v) meta def apply_to_metavars_while_universal (h : expr) : tactic expr := apply_to_metavars_while_universal_aux unit.star h meta def try_instantiate_quantifiers (cont : tactic unit) : tactic unit := do hs ← (local_context >>= monad.filter has_forall_type), gt ← target, when (hs = []/- ∧ head_symbol gt β‰  `Exists-/) failed, monad.for' hs (Ξ» h, do h' ← apply_to_metavars_while_universal h, assert_fresh h'), -- when (head_symbol gt = `Exists) split, monad.for' hs clear, monad.whenb (is_trace_enabled_for `auto) (trace "instantiating quantifiers" >> trace_state >> trace "-----"), cont /- Safe automation. These operate on the first goal, and apply only safe rules (the new state is logically equivalent to the original one). They make whatever progress they can, and leave the user with zero or more goals. They take the following arguments: classical : classical or intuitionistic use_simp : whether to use the simplifier simp_lemmas : in the latter case, extra lemmas to use -/ -- perform safe rules that do not split the goal meta def clarify_core (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : nelim_rule_database) (simp_lemmas : list expr) : tactic unit := do repeat (apply_intro_rule idb 1 classical), repeat (apply_elim_rule edb nedb 1 classical), repeat try_subst_and_injection_on_hyps, (now <|> assumption_safe <|> -- contradiction_safe <|> when (use_simp = tt) (do when_tracing `auto (trace "calling simplifier"), try (strong_simp_hyps_add simp_lemmas), try (strong_simp_add simp_lemmas), try (now <|> assumption_safe))) -- perform safe rules -- TODO: fix recursion meta def safe_core (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : nelim_rule_database) (simp_lemmas : list expr) : unit β†’ tactic unit | unit.star := do clarify_core classical use_simp idb edb nedb simp_lemmas, now <|> try ((apply_intro_rule idb 10 classical <|> apply_elim_rule edb nedb 10 classical) ; (safe_core /- classical use_simp idb edb nedb simp_lemmas -/ unit.star)) /- The backtracking tableau prover. The function force_all_core_aux is the heart of the tableau prover. It takes a list of goals, which are assumed to have been processed with the safe rules already and are not visible on the goal stack. It applies safe rules to the goals in the current goal list (if any), and then starts calling backtracking rules. This function is meant to be passed as a continuation to the backtracking tactics, which are called with a single goal. The tactics should operate on the goal, resulting in a new goal list. They should then call the continuation to finish the backtracking search. The function succeeds if all the goals are ultimately proved, and it fails otherwise. -/ -- TODO: fix recursion meta def force_all_core_aux (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : elim_rule_database) (bidb : bintro_rule_database) (bedb : belim_rule_database) (bnedb : belim_rule_database) (simp_lemmas : list expr) (final_check : tactic unit) /- (preprocessed_goals : list expr) -/ : unit β†’ list expr β†’ tactic unit | unit.star := let force_core_rec := force_all_core_aux /- classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas final_check -/ unit.star in let process_goals_with_backtracking : list expr β†’ tactic unit := Ξ» l, match l with | [] := final_check -- if it passes, we have success! | (g :: gs) := do {set_goals [g], try_assumptions (force_core_rec gs) <|> try_contradictions (force_core_rec gs) <|> try_instantiate_quantifiers (force_core_rec gs) <|> apply_bintro_rule bidb 10 classical (force_core_rec gs) <|> apply_belim_rule bedb bnedb 10 classical (force_core_rec gs)} end in Ξ» preprocessed_goals, do n ← num_goals, if n β‰  0 then do all_goals (safe_core classical use_simp idb edb nedb simp_lemmas unit.star), gs ← get_goals, set_goals [], force_all_core_aux /-classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas final_check-/ unit.star (gs ++ preprocessed_goals) else do process_goals_with_backtracking preprocessed_goals meta def final_check_for_metavariables (g : expr) : tactic unit := do result ← instantiate_mvars g, monad.whenb (has_meta_var result) (when_tracing `auto (trace_state >> trace "result has metavariables:" >> trace result) >> failed) -- the main tableaux prover: acts on one goal, makes sure there are no metavariables at the end meta def force_core (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : elim_rule_database) (bidb : bintro_rule_database) (bedb : belim_rule_database) (bnedb : belim_rule_database) (simp_lemmas : list expr) : tactic unit := do auto_trace_step skip (Ξ» u, ">>> entering force"), gs ← get_goals, match gs with | [] := fail "force failed, there are no goals" | (g :: gs') := do set_goals [g], force_all_core_aux classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas (final_check_for_metavariables g) unit.star [], set_goals gs end /- front ends -/ /- -- TODO: a temporary hack: using trace_option to declare a boolean option declare_trace auto.classical set_option trace.auto.classical true declare_trace auto.use_simp set_option trace.auto.use_simp false -/ -- applies to first goal, never splits it, applies only safe rules, always succeeds meta def clarify (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (simp_lemmas : list expr) : tactic unit := do idb ← caching_user_attribute.get_cache intro_rule_attr, edb ← caching_user_attribute.get_cache elim_rule_attr, nedb ← caching_user_attribute.get_cache nelim_rule_attr, clarify_core classical use_simp idb edb nedb simp_lemmas -- applies to first goal, applies only safe rules, always succeeds meta def safe (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (simp_lemmas : list expr) : tactic unit := do idb ← caching_user_attribute.get_cache intro_rule_attr, edb ← caching_user_attribute.get_cache elim_rule_attr, nedb ← caching_user_attribute.get_cache nelim_rule_attr, safe_core classical use_simp idb edb nedb simp_lemmas unit.star -- applies safe to all goals meta def safe_all (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (simp_lemmas : list expr) : tactic unit := all_goals (safe classical use_simp irules erules nerules simp_lemmas) -- applies to first goal, fails if it does not solve it meta def force (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (birules : list bintro_rule) (berules : list belim_rule) (bnerules : list belim_rule) (simp_lemmas : list expr) : tactic unit := do idb ← caching_user_attribute.get_cache intro_rule_attr, edb ← caching_user_attribute.get_cache elim_rule_attr, nedb ← caching_user_attribute.get_cache nelim_rule_attr, bidb ← caching_user_attribute.get_cache bintro_rule_attr, bedb ← caching_user_attribute.get_cache belim_rule_attr, bnedb ← caching_user_attribute.get_cache bnelim_rule_attr, force_core classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas -- applies to all goals, always succeeds meta def auto (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (birules : list bintro_rule) (berules : list belim_rule) (bnerules : list belim_rule) (simp_lemmas : list expr) : tactic unit := safe_all classical use_simp irules erules nerules simp_lemmas >> all_goals (try (force classical use_simp irules erules nerules birules berules bnerules simp_lemmas)) /- for testing -/ meta def clarify' : tactic unit := clarify tt ff [] [] [] [] meta def safe' : tactic unit := safe tt ff [] [] [] [] meta def ssafe' : tactic unit := safe tt tt [] [] [] [] -- with simplification meta def force' : tactic unit := force tt ff [] [] [] [] [] [] [] meta def sforce' : tactic unit := force tt tt [] [] [] [] [] [] [] meta def auto' : tactic unit := auto tt ff [] [] [] [] [] [] [] meta def sauto' : tactic unit := auto tt tt [] [] [] [] [] [] [] meta def iauto' : tactic unit := auto ff ff [] [] [] [] [] [] [] meta def isauto' : tactic unit := auto ff tt [] [] [] [] [] [] [] end tactic
1811240e784f78bd5bdebf897ca6175797e0b88e
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/classAbbrev.lean
6e209b8222159de3195c27c8c284d4df9e781e34
[ "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
332
lean
class Refl.{U} {Ξ± : Type U} (R : Ξ± β†’ Ξ± β†’ Prop) : Prop := refl (a : Ξ±) : R a a class Symm.{U} {Ξ± : Type U} (R : Ξ± β†’ Ξ± β†’ Prop) : Prop := symm {a b : Ξ±} : R a b β†’ R b a /-- An example decl modifier (a doc comment). -/ class abbrev PEquiv.{U} {Ξ± : Type U} (R : Ξ± β†’ Ξ± β†’ Prop) : Prop := Refl R, Symm R
dc250761133206b8478d24108edb54365c088066
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/big_operators/order.lean
68bb20bb7cc68509de5dc48e4cdf64f8fff471a3
[ "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
27,603
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl -/ import algebra.order.absolute_value import algebra.big_operators.basic /-! # Results about big operators with values in an ordered algebraic structure. Mostly monotonicity results for the `∏` and `βˆ‘` operations. -/ open function open_locale big_operators variables {ΞΉ Ξ± Ξ² M N G k R : Type*} namespace finset section ordered_comm_monoid variables [comm_monoid M] [ordered_comm_monoid N] /-- Let `{x | p x}` be a subsemigroup of a commutative monoid `M`. Let `f : M β†’ N` be a map submultiplicative on `{x | p x}`, i.e., `p x β†’ p y β†’ f (x * y) ≀ f x * f y`. Let `g i`, `i ∈ s`, be a nonempty finite family of elements of `M` such that `βˆ€ i ∈ s, p (g i)`. Then `f (∏ x in s, g x) ≀ ∏ x in s, f (g x)`. -/ @[to_additive le_sum_nonempty_of_subadditive_on_pred] lemma le_prod_nonempty_of_submultiplicative_on_pred (f : M β†’ N) (p : M β†’ Prop) (h_mul : βˆ€ x y, p x β†’ p y β†’ f (x * y) ≀ f x * f y) (hp_mul : βˆ€ x y, p x β†’ p y β†’ p (x * y)) (g : ΞΉ β†’ M) (s : finset ΞΉ) (hs_nonempty : s.nonempty) (hs : βˆ€ i ∈ s, p (g i)) : f (∏ i in s, g i) ≀ ∏ i in s, f (g i) := begin refine le_trans (multiset.le_prod_nonempty_of_submultiplicative_on_pred f p h_mul hp_mul _ _ _) _, { simp [hs_nonempty.ne_empty], }, { exact multiset.forall_mem_map_iff.mpr hs, }, rw multiset.map_map, refl, end /-- Let `{x | p x}` be an additive subsemigroup of an additive commutative monoid `M`. Let `f : M β†’ N` be a map subadditive on `{x | p x}`, i.e., `p x β†’ p y β†’ f (x + y) ≀ f x + f y`. Let `g i`, `i ∈ s`, be a nonempty finite family of elements of `M` such that `βˆ€ i ∈ s, p (g i)`. Then `f (βˆ‘ i in s, g i) ≀ βˆ‘ i in s, f (g i)`. -/ add_decl_doc le_sum_nonempty_of_subadditive_on_pred /-- If `f : M β†’ N` is a submultiplicative function, `f (x * y) ≀ f x * f y` and `g i`, `i ∈ s`, is a nonempty finite family of elements of `M`, then `f (∏ i in s, g i) ≀ ∏ i in s, f (g i)`. -/ @[to_additive le_sum_nonempty_of_subadditive] lemma le_prod_nonempty_of_submultiplicative (f : M β†’ N) (h_mul : βˆ€ x y, f (x * y) ≀ f x * f y) {s : finset ΞΉ} (hs : s.nonempty) (g : ΞΉ β†’ M) : f (∏ i in s, g i) ≀ ∏ i in s, f (g i) := le_prod_nonempty_of_submultiplicative_on_pred f (Ξ» i, true) (Ξ» x y _ _, h_mul x y) (Ξ» _ _ _ _, trivial) g s hs (Ξ» _ _, trivial) /-- If `f : M β†’ N` is a subadditive function, `f (x + y) ≀ f x + f y` and `g i`, `i ∈ s`, is a nonempty finite family of elements of `M`, then `f (βˆ‘ i in s, g i) ≀ βˆ‘ i in s, f (g i)`. -/ add_decl_doc le_sum_nonempty_of_subadditive /-- Let `{x | p x}` be a subsemigroup of a commutative monoid `M`. Let `f : M β†’ N` be a map such that `f 1 = 1` and `f` is submultiplicative on `{x | p x}`, i.e., `p x β†’ p y β†’ f (x * y) ≀ f x * f y`. Let `g i`, `i ∈ s`, be a finite family of elements of `M` such that `βˆ€ i ∈ s, p (g i)`. Then `f (∏ i in s, g i) ≀ ∏ i in s, f (g i)`. -/ @[to_additive le_sum_of_subadditive_on_pred] lemma le_prod_of_submultiplicative_on_pred (f : M β†’ N) (p : M β†’ Prop) (h_one : f 1 = 1) (h_mul : βˆ€ x y, p x β†’ p y β†’ f (x * y) ≀ f x * f y) (hp_mul : βˆ€ x y, p x β†’ p y β†’ p (x * y)) (g : ΞΉ β†’ M) {s : finset ΞΉ} (hs : βˆ€ i ∈ s, p (g i)) : f (∏ i in s, g i) ≀ ∏ i in s, f (g i) := begin rcases eq_empty_or_nonempty s with rfl|hs_nonempty, { simp [h_one] }, { exact le_prod_nonempty_of_submultiplicative_on_pred f p h_mul hp_mul g s hs_nonempty hs, }, end /-- Let `{x | p x}` be a subsemigroup of a commutative additive monoid `M`. Let `f : M β†’ N` be a map such that `f 0 = 0` and `f` is subadditive on `{x | p x}`, i.e. `p x β†’ p y β†’ f (x + y) ≀ f x + f y`. Let `g i`, `i ∈ s`, be a finite family of elements of `M` such that `βˆ€ i ∈ s, p (g i)`. Then `f (βˆ‘ x in s, g x) ≀ βˆ‘ x in s, f (g x)`. -/ add_decl_doc le_sum_of_subadditive_on_pred /-- If `f : M β†’ N` is a submultiplicative function, `f (x * y) ≀ f x * f y`, `f 1 = 1`, and `g i`, `i ∈ s`, is a finite family of elements of `M`, then `f (∏ i in s, g i) ≀ ∏ i in s, f (g i)`. -/ @[to_additive le_sum_of_subadditive] lemma le_prod_of_submultiplicative (f : M β†’ N) (h_one : f 1 = 1) (h_mul : βˆ€ x y, f (x * y) ≀ f x * f y) (s : finset ΞΉ) (g : ΞΉ β†’ M) : f (∏ i in s, g i) ≀ ∏ i in s, f (g i) := begin refine le_trans (multiset.le_prod_of_submultiplicative f h_one h_mul _) _, rw multiset.map_map, refl, end /-- If `f : M β†’ N` is a subadditive function, `f (x + y) ≀ f x + f y`, `f 0 = 0`, and `g i`, `i ∈ s`, is a finite family of elements of `M`, then `f (βˆ‘ i in s, g i) ≀ βˆ‘ i in s, f (g i)`. -/ add_decl_doc le_sum_of_subadditive variables {f g : ΞΉ β†’ N} {s t : finset ΞΉ} /-- In an ordered commutative monoid, if each factor `f i` of one finite product is less than or equal to the corresponding factor `g i` of another finite product, then `∏ i in s, f i ≀ ∏ i in s, g i`. -/ @[to_additive sum_le_sum] lemma prod_le_prod'' (h : βˆ€ i ∈ s, f i ≀ g i) : ∏ i in s, f i ≀ ∏ i in s, g i := multiset.prod_map_le_prod_map f g h /-- In an ordered additive commutative monoid, if each summand `f i` of one finite sum is less than or equal to the corresponding summand `g i` of another finite sum, then `βˆ‘ i in s, f i ≀ βˆ‘ i in s, g i`. -/ add_decl_doc sum_le_sum @[to_additive sum_nonneg] lemma one_le_prod' (h : βˆ€i ∈ s, 1 ≀ f i) : 1 ≀ (∏ i in s, f i) := le_trans (by rw prod_const_one) (prod_le_prod'' h) @[to_additive finset.sum_nonneg'] lemma one_le_prod'' (h : βˆ€ (i : ΞΉ), 1 ≀ f i) : 1 ≀ ∏ (i : ΞΉ) in s, f i := finset.one_le_prod' (Ξ» i hi, h i) @[to_additive sum_nonpos] lemma prod_le_one' (h : βˆ€i ∈ s, f i ≀ 1) : (∏ i in s, f i) ≀ 1 := (prod_le_prod'' h).trans_eq (by rw prod_const_one) @[to_additive sum_le_sum_of_subset_of_nonneg] lemma prod_le_prod_of_subset_of_one_le' (h : s βŠ† t) (hf : βˆ€ i ∈ t, i βˆ‰ s β†’ 1 ≀ f i) : ∏ i in s, f i ≀ ∏ i in t, f i := by classical; calc (∏ i in s, f i) ≀ (∏ i in t \ s, f i) * (∏ i in s, f i) : le_mul_of_one_le_left' $ one_le_prod' $ by simpa only [mem_sdiff, and_imp] ... = ∏ i in t \ s βˆͺ s, f i : (prod_union sdiff_disjoint).symm ... = ∏ i in t, f i : by rw [sdiff_union_of_subset h] @[to_additive sum_mono_set_of_nonneg] lemma prod_mono_set_of_one_le' (hf : βˆ€ x, 1 ≀ f x) : monotone (Ξ» s, ∏ x in s, f x) := Ξ» s t hst, prod_le_prod_of_subset_of_one_le' hst $ Ξ» x _ _, hf x @[to_additive sum_le_univ_sum_of_nonneg] lemma prod_le_univ_prod_of_one_le' [fintype ΞΉ] {s : finset ΞΉ} (w : βˆ€ x, 1 ≀ f x) : ∏ x in s, f x ≀ ∏ x, f x := prod_le_prod_of_subset_of_one_le' (subset_univ s) (Ξ» a _ _, w a) @[to_additive sum_eq_zero_iff_of_nonneg] lemma prod_eq_one_iff_of_one_le' : (βˆ€ i ∈ s, 1 ≀ f i) β†’ (∏ i in s, f i = 1 ↔ βˆ€ i ∈ s, f i = 1) := begin classical, apply finset.induction_on s, exact Ξ» _, ⟨λ _ _, false.elim, Ξ» _, rfl⟩, assume a s ha ih H, have : βˆ€ i ∈ s, 1 ≀ f i, from Ξ» _, H _ ∘ mem_insert_of_mem, rw [prod_insert ha, mul_eq_one_iff' (H _ $ mem_insert_self _ _) (one_le_prod' this), forall_mem_insert, ih this] end @[to_additive sum_eq_zero_iff_of_nonneg] lemma prod_eq_one_iff_of_le_one' : (βˆ€ i ∈ s, f i ≀ 1) β†’ (∏ i in s, f i = 1 ↔ βˆ€ i ∈ s, f i = 1) := @prod_eq_one_iff_of_one_le' _ Nα΅’α΅ˆ _ _ _ @[to_additive single_le_sum] lemma single_le_prod' (hf : βˆ€ i ∈ s, 1 ≀ f i) {a} (h : a ∈ s) : f a ≀ (∏ x in s, f x) := calc f a = ∏ i in {a}, f i : prod_singleton.symm ... ≀ ∏ i in s, f i : prod_le_prod_of_subset_of_one_le' (singleton_subset_iff.2 h) $ Ξ» i hi _, hf i hi @[to_additive sum_le_card_nsmul] lemma prod_le_pow_card (s : finset ΞΉ) (f : ΞΉ β†’ N) (n : N) (h : βˆ€ x ∈ s, f x ≀ n) : s.prod f ≀ n ^ s.card := begin refine (multiset.prod_le_pow_card (s.val.map f) n _).trans _, { simpa using h }, { simpa } end @[to_additive card_nsmul_le_sum] lemma pow_card_le_prod (s : finset ΞΉ) (f : ΞΉ β†’ N) (n : N) (h : βˆ€ x ∈ s, n ≀ f x) : n ^ s.card ≀ s.prod f := @finset.prod_le_pow_card _ Nα΅’α΅ˆ _ _ _ _ h lemma card_bUnion_le_card_mul [decidable_eq Ξ²] (s : finset ΞΉ) (f : ΞΉ β†’ finset Ξ²) (n : β„•) (h : βˆ€ a ∈ s, (f a).card ≀ n) : (s.bUnion f).card ≀ s.card * n := card_bUnion_le.trans $ sum_le_card_nsmul _ _ _ h variables {ΞΉ' : Type*} [decidable_eq ΞΉ'] @[to_additive sum_fiberwise_le_sum_of_sum_fiber_nonneg] lemma prod_fiberwise_le_prod_of_one_le_prod_fiber' {t : finset ΞΉ'} {g : ΞΉ β†’ ΞΉ'} {f : ΞΉ β†’ N} (h : βˆ€ y βˆ‰ t, (1 : N) ≀ ∏ x in s.filter (Ξ» x, g x = y), f x) : ∏ y in t, ∏ x in s.filter (Ξ» x, g x = y), f x ≀ ∏ x in s, f x := calc (∏ y in t, ∏ x in s.filter (Ξ» x, g x = y), f x) ≀ (∏ y in t βˆͺ s.image g, ∏ x in s.filter (Ξ» x, g x = y), f x) : prod_le_prod_of_subset_of_one_le' (subset_union_left _ _) $ Ξ» y hyts, h y ... = ∏ x in s, f x : prod_fiberwise_of_maps_to (Ξ» x hx, mem_union.2 $ or.inr $ mem_image_of_mem _ hx) _ @[to_additive sum_le_sum_fiberwise_of_sum_fiber_nonpos] lemma prod_le_prod_fiberwise_of_prod_fiber_le_one' {t : finset ΞΉ'} {g : ΞΉ β†’ ΞΉ'} {f : ΞΉ β†’ N} (h : βˆ€ y βˆ‰ t, (∏ x in s.filter (Ξ» x, g x = y), f x) ≀ 1) : (∏ x in s, f x) ≀ ∏ y in t, ∏ x in s.filter (Ξ» x, g x = y), f x := @prod_fiberwise_le_prod_of_one_le_prod_fiber' _ Nα΅’α΅ˆ _ _ _ _ _ _ _ h end ordered_comm_monoid lemma abs_sum_le_sum_abs {G : Type*} [linear_ordered_add_comm_group G] (f : ΞΉ β†’ G) (s : finset ΞΉ) : |βˆ‘ i in s, f i| ≀ βˆ‘ i in s, |f i| := le_sum_of_subadditive _ abs_zero abs_add s f lemma abs_sum_of_nonneg {G : Type*} [linear_ordered_add_comm_group G] {f : ΞΉ β†’ G} {s : finset ΞΉ} (hf : βˆ€ i ∈ s, 0 ≀ f i) : |βˆ‘ (i : ΞΉ) in s, f i| = βˆ‘ (i : ΞΉ) in s, f i := by rw abs_of_nonneg (finset.sum_nonneg hf) lemma abs_sum_of_nonneg' {G : Type*} [linear_ordered_add_comm_group G] {f : ΞΉ β†’ G} {s : finset ΞΉ} (hf : βˆ€ i, 0 ≀ f i) : |βˆ‘ (i : ΞΉ) in s, f i| = βˆ‘ (i : ΞΉ) in s, f i := by rw abs_of_nonneg (finset.sum_nonneg' hf) lemma abs_prod {R : Type*} [linear_ordered_comm_ring R] {f : ΞΉ β†’ R} {s : finset ΞΉ} : |∏ x in s, f x| = ∏ x in s, |f x| := (abs_hom.to_monoid_hom : R β†’* R).map_prod _ _ section pigeonhole variable [decidable_eq Ξ²] theorem card_le_mul_card_image_of_maps_to {f : Ξ± β†’ Ξ²} {s : finset Ξ±} {t : finset Ξ²} (Hf : βˆ€ a ∈ s, f a ∈ t) (n : β„•) (hn : βˆ€ a ∈ t, (s.filter (Ξ» x, f x = a)).card ≀ n) : s.card ≀ n * t.card := calc s.card = (βˆ‘ a in t, (s.filter (Ξ» x, f x = a)).card) : card_eq_sum_card_fiberwise Hf ... ≀ (βˆ‘ _ in t, n) : sum_le_sum hn ... = _ : by simp [mul_comm] theorem card_le_mul_card_image {f : Ξ± β†’ Ξ²} (s : finset Ξ±) (n : β„•) (hn : βˆ€ a ∈ s.image f, (s.filter (Ξ» x, f x = a)).card ≀ n) : s.card ≀ n * (s.image f).card := card_le_mul_card_image_of_maps_to (Ξ» x, mem_image_of_mem _) n hn theorem mul_card_image_le_card_of_maps_to {f : Ξ± β†’ Ξ²} {s : finset Ξ±} {t : finset Ξ²} (Hf : βˆ€ a ∈ s, f a ∈ t) (n : β„•) (hn : βˆ€ a ∈ t, n ≀ (s.filter (Ξ» x, f x = a)).card) : n * t.card ≀ s.card := calc n * t.card = (βˆ‘ _ in t, n) : by simp [mul_comm] ... ≀ (βˆ‘ a in t, (s.filter (Ξ» x, f x = a)).card) : sum_le_sum hn ... = s.card : by rw ← card_eq_sum_card_fiberwise Hf theorem mul_card_image_le_card {f : Ξ± β†’ Ξ²} (s : finset Ξ±) (n : β„•) (hn : βˆ€ a ∈ s.image f, n ≀ (s.filter (Ξ» x, f x = a)).card) : n * (s.image f).card ≀ s.card := mul_card_image_le_card_of_maps_to (Ξ» x, mem_image_of_mem _) n hn end pigeonhole section double_counting variables [decidable_eq Ξ±] {s : finset Ξ±} {B : finset (finset Ξ±)} {n : β„•} /-- If every element belongs to at most `n` finsets, then the sum of their sizes is at most `n` times how many they are. -/ lemma sum_card_inter_le (h : βˆ€ a ∈ s, (B.filter $ (∈) a).card ≀ n) : βˆ‘ t in B, (s ∩ t).card ≀ s.card * n := begin refine le_trans _ (s.sum_le_card_nsmul _ _ h), simp_rw [←filter_mem_eq_inter, card_eq_sum_ones, sum_filter], exact sum_comm.le, end /-- If every element belongs to at most `n` finsets, then the sum of their sizes is at most `n` times how many they are. -/ lemma sum_card_le [fintype Ξ±] (h : βˆ€ a, (B.filter $ (∈) a).card ≀ n) : βˆ‘ s in B, s.card ≀ fintype.card Ξ± * n := calc βˆ‘ s in B, s.card = βˆ‘ s in B, (univ ∩ s).card : by simp_rw univ_inter ... ≀ fintype.card Ξ± * n : sum_card_inter_le (Ξ» a _, h a) /-- If every element belongs to at least `n` finsets, then the sum of their sizes is at least `n` times how many they are. -/ lemma le_sum_card_inter (h : βˆ€ a ∈ s, n ≀ (B.filter $ (∈) a).card) : s.card * n ≀ βˆ‘ t in B, (s ∩ t).card := begin apply (s.card_nsmul_le_sum _ _ h).trans, simp_rw [←filter_mem_eq_inter, card_eq_sum_ones, sum_filter], exact sum_comm.le, end /-- If every element belongs to at least `n` finsets, then the sum of their sizes is at least `n` times how many they are. -/ lemma le_sum_card [fintype Ξ±] (h : βˆ€ a, n ≀ (B.filter $ (∈) a).card) : fintype.card Ξ± * n ≀ βˆ‘ s in B, s.card := calc fintype.card Ξ± * n ≀ βˆ‘ s in B, (univ ∩ s).card : le_sum_card_inter (Ξ» a _, h a) ... = βˆ‘ s in B, s.card : by simp_rw univ_inter /-- If every element belongs to exactly `n` finsets, then the sum of their sizes is `n` times how many they are. -/ lemma sum_card_inter (h : βˆ€ a ∈ s, (B.filter $ (∈) a).card = n) : βˆ‘ t in B, (s ∩ t).card = s.card * n := (sum_card_inter_le $ Ξ» a ha, (h a ha).le).antisymm (le_sum_card_inter $ Ξ» a ha, (h a ha).ge) /-- If every element belongs to exactly `n` finsets, then the sum of their sizes is `n` times how many they are. -/ lemma sum_card [fintype Ξ±] (h : βˆ€ a, (B.filter $ (∈) a).card = n) : βˆ‘ s in B, s.card = fintype.card Ξ± * n := by simp_rw [fintype.card, ←sum_card_inter (Ξ» a _, h a), univ_inter] lemma card_le_card_bUnion {s : finset ΞΉ} {f : ΞΉ β†’ finset Ξ±} (hs : (s : set ΞΉ).pairwise_disjoint f) (hf : βˆ€ i ∈ s, (f i).nonempty) : s.card ≀ (s.bUnion f).card := by { rw [card_bUnion hs, card_eq_sum_ones], exact sum_le_sum (Ξ» i hi, (hf i hi).card_pos) } lemma card_le_card_bUnion_add_card_fiber {s : finset ΞΉ} {f : ΞΉ β†’ finset Ξ±} (hs : (s : set ΞΉ).pairwise_disjoint f) : s.card ≀ (s.bUnion f).card + (s.filter $ Ξ» i, f i = βˆ…).card := begin rw [←finset.filter_card_add_filter_neg_card_eq_card (Ξ» i, f i = βˆ…), add_comm], exact add_le_add_right ((card_le_card_bUnion (hs.subset $ filter_subset _ _) $ Ξ» i hi, nonempty_of_ne_empty $ (mem_filter.1 hi).2).trans $ card_le_of_subset $ bUnion_subset_bUnion_of_subset_left _ $ filter_subset _ _) _, end lemma card_le_card_bUnion_add_one {s : finset ΞΉ} {f : ΞΉ β†’ finset Ξ±} (hf : injective f) (hs : (s : set ΞΉ).pairwise_disjoint f) : s.card ≀ (s.bUnion f).card + 1 := (card_le_card_bUnion_add_card_fiber hs).trans $ add_le_add_left (card_le_one.2 $ Ξ» i hi j hj, hf $ (mem_filter.1 hi).2.trans (mem_filter.1 hj).2.symm) _ end double_counting section canonically_ordered_monoid variables [canonically_ordered_monoid M] {f : ΞΉ β†’ M} {s t : finset ΞΉ} @[simp, to_additive sum_eq_zero_iff] lemma prod_eq_one_iff' : ∏ x in s, f x = 1 ↔ βˆ€ x ∈ s, f x = 1 := prod_eq_one_iff_of_one_le' $ Ξ» x hx, one_le (f x) @[to_additive sum_le_sum_of_subset] lemma prod_le_prod_of_subset' (h : s βŠ† t) : ∏ x in s, f x ≀ ∏ x in t, f x := prod_le_prod_of_subset_of_one_le' h $ assume x h₁ hβ‚‚, one_le _ @[to_additive sum_mono_set] lemma prod_mono_set' (f : ΞΉ β†’ M) : monotone (Ξ» s, ∏ x in s, f x) := Ξ» s₁ sβ‚‚ hs, prod_le_prod_of_subset' hs @[to_additive sum_le_sum_of_ne_zero] lemma prod_le_prod_of_ne_one' (h : βˆ€ x ∈ s, f x β‰  1 β†’ x ∈ t) : ∏ x in s, f x ≀ ∏ x in t, f x := by classical; calc ∏ x in s, f x = (∏ x in s.filter (Ξ» x, f x = 1), f x) * ∏ x in s.filter (Ξ» x, f x β‰  1), f x : by rw [← prod_union, filter_union_filter_neg_eq]; exact disjoint_filter.2 (assume _ _ h n_h, n_h h) ... ≀ (∏ x in t, f x) : mul_le_of_le_one_of_le (prod_le_one' $ by simp only [mem_filter, and_imp]; exact Ξ» _ _, le_of_eq) (prod_le_prod_of_subset' $ by simpa only [subset_iff, mem_filter, and_imp]) end canonically_ordered_monoid section ordered_cancel_comm_monoid variables [ordered_cancel_comm_monoid M] {f g : ΞΉ β†’ M} {s t : finset ΞΉ} @[to_additive sum_lt_sum] theorem prod_lt_prod' (Hle : βˆ€ i ∈ s, f i ≀ g i) (Hlt : βˆƒ i ∈ s, f i < g i) : ∏ i in s, f i < ∏ i in s, g i := begin classical, rcases Hlt with ⟨i, hi, hlt⟩, rw [← insert_erase hi, prod_insert (not_mem_erase _ _), prod_insert (not_mem_erase _ _)], exact mul_lt_mul_of_lt_of_le hlt (prod_le_prod'' $ Ξ» j hj, Hle j $ mem_of_mem_erase hj) end @[to_additive sum_lt_sum_of_nonempty] lemma prod_lt_prod_of_nonempty' (hs : s.nonempty) (Hlt : βˆ€ i ∈ s, f i < g i) : ∏ i in s, f i < ∏ i in s, g i := begin apply prod_lt_prod', { intros i hi, apply le_of_lt (Hlt i hi) }, cases hs with i hi, exact ⟨i, hi, Hlt i hi⟩, end @[to_additive sum_lt_sum_of_subset] lemma prod_lt_prod_of_subset' (h : s βŠ† t) {i : ΞΉ} (ht : i ∈ t) (hs : i βˆ‰ s) (hlt : 1 < f i) (hle : βˆ€ j ∈ t, j βˆ‰ s β†’ 1 ≀ f j) : ∏ j in s, f j < ∏ j in t, f j := by classical; calc ∏ j in s, f j < ∏ j in insert i s, f j : begin rw prod_insert hs, exact lt_mul_of_one_lt_left' (∏ j in s, f j) hlt, end ... ≀ ∏ j in t, f j : begin apply prod_le_prod_of_subset_of_one_le', { simp [finset.insert_subset, h, ht] }, { assume x hx h'x, simp only [mem_insert, not_or_distrib] at h'x, exact hle x hx h'x.2 } end @[to_additive single_lt_sum] lemma single_lt_prod' {i j : ΞΉ} (hij : j β‰  i) (hi : i ∈ s) (hj : j ∈ s) (hlt : 1 < f j) (hle : βˆ€ k ∈ s, k β‰  i β†’ 1 ≀ f k) : f i < ∏ k in s, f k := calc f i = ∏ k in {i}, f k : prod_singleton.symm ... < ∏ k in s, f k : prod_lt_prod_of_subset' (singleton_subset_iff.2 hi) hj (mt mem_singleton.1 hij) hlt $ Ξ» k hks hki, hle k hks (mt mem_singleton.2 hki) @[to_additive sum_pos] lemma one_lt_prod (h : βˆ€i ∈ s, 1 < f i) (hs : s.nonempty) : 1 < (∏ i in s, f i) := lt_of_le_of_lt (by rw prod_const_one) $ prod_lt_prod_of_nonempty' hs h @[to_additive] lemma prod_lt_one (h : βˆ€i ∈ s, f i < 1) (hs : s.nonempty) : (∏ i in s, f i) < 1 := (prod_lt_prod_of_nonempty' hs h).trans_le (by rw prod_const_one) @[to_additive] lemma prod_eq_prod_iff_of_le {f g : ΞΉ β†’ M} (h : βˆ€ i ∈ s, f i ≀ g i) : ∏ i in s, f i = ∏ i in s, g i ↔ βˆ€ i ∈ s, f i = g i := begin classical, revert h, refine finset.induction_on s (Ξ» _, ⟨λ _ _, false.elim, Ξ» _, rfl⟩) (Ξ» a s ha ih H, _), specialize ih (Ξ» i, H i ∘ finset.mem_insert_of_mem), rw [finset.prod_insert ha, finset.prod_insert ha, finset.forall_mem_insert, ←ih], exact mul_eq_mul_iff_eq_and_eq (H a (s.mem_insert_self a)) (finset.prod_le_prod'' (Ξ» i, H i ∘ finset.mem_insert_of_mem)), end end ordered_cancel_comm_monoid section linear_ordered_cancel_comm_monoid variables [linear_ordered_cancel_comm_monoid M] {f g : ΞΉ β†’ M} {s t : finset ΞΉ} @[to_additive exists_lt_of_sum_lt] theorem exists_lt_of_prod_lt' (Hlt : ∏ i in s, f i < ∏ i in s, g i) : βˆƒ i ∈ s, f i < g i := begin contrapose! Hlt with Hle, exact prod_le_prod'' Hle end @[to_additive exists_le_of_sum_le] theorem exists_le_of_prod_le' (hs : s.nonempty) (Hle : ∏ i in s, f i ≀ ∏ i in s, g i) : βˆƒ i ∈ s, f i ≀ g i := begin contrapose! Hle with Hlt, exact prod_lt_prod_of_nonempty' hs Hlt end @[to_additive exists_pos_of_sum_zero_of_exists_nonzero] lemma exists_one_lt_of_prod_one_of_exists_ne_one' (f : ΞΉ β†’ M) (h₁ : ∏ i in s, f i = 1) (hβ‚‚ : βˆƒ i ∈ s, f i β‰  1) : βˆƒ i ∈ s, 1 < f i := begin contrapose! h₁, obtain ⟨i, m, i_ne⟩ : βˆƒ i ∈ s, f i β‰  1 := hβ‚‚, apply ne_of_lt, calc ∏ j in s, f j < ∏ j in s, 1 : prod_lt_prod' h₁ ⟨i, m, (h₁ i m).lt_of_ne i_ne⟩ ... = 1 : prod_const_one end end linear_ordered_cancel_comm_monoid section ordered_comm_semiring variables [ordered_comm_semiring R] {f g : ΞΉ β†’ R} {s t : finset ΞΉ} open_locale classical /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_nonneg (h0 : βˆ€ i ∈ s, 0 ≀ f i) : 0 ≀ ∏ i in s, f i := prod_induction f (Ξ» i, 0 ≀ i) (Ξ» _ _ ha hb, mul_nonneg ha hb) zero_le_one h0 /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_pos [nontrivial R] (h0 : βˆ€ i ∈ s, 0 < f i) : 0 < ∏ i in s, f i := prod_induction f (Ξ» x, 0 < x) (Ξ» _ _ ha hb, mul_pos ha hb) zero_lt_one h0 /-- If all `f i`, `i ∈ s`, are nonnegative and each `f i` is less than or equal to `g i`, then the product of `f i` is less than or equal to the product of `g i`. See also `finset.prod_le_prod''` for the case of an ordered commutative multiplicative monoid. -/ lemma prod_le_prod (h0 : βˆ€ i ∈ s, 0 ≀ f i) (h1 : βˆ€ i ∈ s, f i ≀ g i) : ∏ i in s, f i ≀ ∏ i in s, g i := begin induction s using finset.induction with a s has ih h, { simp }, { simp only [prod_insert has], apply mul_le_mul, { exact h1 a (mem_insert_self a s) }, { apply ih (Ξ» x H, h0 _ _) (Ξ» x H, h1 _ _); exact (mem_insert_of_mem H) }, { apply prod_nonneg (Ξ» x H, h0 x (mem_insert_of_mem H)) }, { apply le_trans (h0 a (mem_insert_self a s)) (h1 a (mem_insert_self a s)) } } end /-- If each `f i`, `i ∈ s` belongs to `[0, 1]`, then their product is less than or equal to one. See also `finset.prod_le_one'` for the case of an ordered commutative multiplicative monoid. -/ lemma prod_le_one (h0 : βˆ€ i ∈ s, 0 ≀ f i) (h1 : βˆ€ i ∈ s, f i ≀ 1) : ∏ i in s, f i ≀ 1 := begin convert ← prod_le_prod h0 h1, exact finset.prod_const_one end /-- If `g, h ≀ f` and `g i + h i ≀ f i`, then the product of `f` over `s` is at least the sum of the products of `g` and `h`. This is the version for `ordered_comm_semiring`. -/ lemma prod_add_prod_le {i : ΞΉ} {f g h : ΞΉ β†’ R} (hi : i ∈ s) (h2i : g i + h i ≀ f i) (hgf : βˆ€ j ∈ s, j β‰  i β†’ g j ≀ f j) (hhf : βˆ€ j ∈ s, j β‰  i β†’ h j ≀ f j) (hg : βˆ€ i ∈ s, 0 ≀ g i) (hh : βˆ€ i ∈ s, 0 ≀ h i) : ∏ i in s, g i + ∏ i in s, h i ≀ ∏ i in s, f i := begin simp_rw [prod_eq_mul_prod_diff_singleton hi], refine le_trans _ (mul_le_mul_of_nonneg_right h2i _), { rw [right_distrib], apply add_le_add; apply mul_le_mul_of_nonneg_left; try { apply_assumption; assumption }; apply prod_le_prod; simp * { contextual := tt } }, { apply prod_nonneg, simp only [and_imp, mem_sdiff, mem_singleton], intros j h1j h2j, exact le_trans (hg j h1j) (hgf j h1j h2j) } end end ordered_comm_semiring section canonically_ordered_comm_semiring variables [canonically_ordered_comm_semiring R] {f g h : ΞΉ β†’ R} {s : finset ΞΉ} {i : ΞΉ} lemma prod_le_prod' (h : βˆ€ i ∈ s, f i ≀ g i) : ∏ i in s, f i ≀ ∏ i in s, g i := begin classical, induction s using finset.induction with a s has ih h, { simp }, { rw [finset.prod_insert has, finset.prod_insert has], apply mul_le_mul', { exact h _ (finset.mem_insert_self a s) }, { exact ih (Ξ» i hi, h _ (finset.mem_insert_of_mem hi)) } } end /-- If `g, h ≀ f` and `g i + h i ≀ f i`, then the product of `f` over `s` is at least the sum of the products of `g` and `h`. This is the version for `canonically_ordered_comm_semiring`. -/ lemma prod_add_prod_le' (hi : i ∈ s) (h2i : g i + h i ≀ f i) (hgf : βˆ€ j ∈ s, j β‰  i β†’ g j ≀ f j) (hhf : βˆ€ j ∈ s, j β‰  i β†’ h j ≀ f j) : ∏ i in s, g i + ∏ i in s, h i ≀ ∏ i in s, f i := begin classical, simp_rw [prod_eq_mul_prod_diff_singleton hi], refine le_trans _ (mul_le_mul_right' h2i _), rw [right_distrib], apply add_le_add; apply mul_le_mul_left'; apply prod_le_prod'; simp only [and_imp, mem_sdiff, mem_singleton]; intros; apply_assumption; assumption end end canonically_ordered_comm_semiring end finset namespace fintype variables [fintype ΞΉ] @[to_additive sum_mono, mono] lemma prod_mono' [ordered_comm_monoid M] : monotone (Ξ» f : ΞΉ β†’ M, ∏ i, f i) := Ξ» f g hfg, finset.prod_le_prod'' $ Ξ» x _, hfg x attribute [mono] sum_mono @[to_additive sum_strict_mono] lemma prod_strict_mono' [ordered_cancel_comm_monoid M] : strict_mono (Ξ» f : ΞΉ β†’ M, ∏ x, f x) := Ξ» f g hfg, let ⟨hle, i, hlt⟩ := pi.lt_def.mp hfg in finset.prod_lt_prod' (Ξ» i _, hle i) ⟨i, finset.mem_univ i, hlt⟩ end fintype namespace with_top open finset /-- A product of finite numbers is still finite -/ lemma prod_lt_top [canonically_ordered_comm_semiring R] [nontrivial R] [decidable_eq R] {s : finset ΞΉ} {f : ΞΉ β†’ with_top R} (h : βˆ€ i ∈ s, f i β‰  ⊀) : ∏ i in s, f i < ⊀ := prod_induction f (Ξ» a, a < ⊀) (Ξ» a b h₁ hβ‚‚, mul_lt_top h₁.ne hβ‚‚.ne) (coe_lt_top 1) $ Ξ» a ha, lt_top_iff_ne_top.2 (h a ha) /-- A sum of finite numbers is still finite -/ lemma sum_lt_top [ordered_add_comm_monoid M] {s : finset ΞΉ} {f : ΞΉ β†’ with_top M} (h : βˆ€ i ∈ s, f i β‰  ⊀) : (βˆ‘ i in s, f i) < ⊀ := sum_induction f (Ξ» a, a < ⊀) (Ξ» a b h₁ hβ‚‚, add_lt_top.2 ⟨h₁, hβ‚‚βŸ©) zero_lt_top $ Ξ» i hi, lt_top_iff_ne_top.2 (h i hi) /-- A sum of numbers is infinite iff one of them is infinite -/ lemma sum_eq_top_iff [ordered_add_comm_monoid M] {s : finset ΞΉ} {f : ΞΉ β†’ with_top M} : βˆ‘ i in s, f i = ⊀ ↔ βˆƒ i ∈ s, f i = ⊀ := begin classical, split, { contrapose!, exact Ξ» h, (sum_lt_top $ Ξ» i hi, (h i hi)).ne }, { rintro ⟨i, his, hi⟩, rw [sum_eq_add_sum_diff_singleton his, hi, top_add] } end /-- A sum of finite numbers is still finite -/ lemma sum_lt_top_iff [ordered_add_comm_monoid M] {s : finset ΞΉ} {f : ΞΉ β†’ with_top M} : βˆ‘ i in s, f i < ⊀ ↔ βˆ€ i ∈ s, f i < ⊀ := by simp only [lt_top_iff_ne_top, ne.def, sum_eq_top_iff, not_exists] end with_top section absolute_value variables {S : Type*} lemma absolute_value.sum_le [semiring R] [ordered_semiring S] (abv : absolute_value R S) (s : finset ΞΉ) (f : ΞΉ β†’ R) : abv (βˆ‘ i in s, f i) ≀ βˆ‘ i in s, abv (f i) := begin letI := classical.dec_eq ΞΉ, refine finset.induction_on s _ (Ξ» i s hi ih, _), { simp }, { simp only [finset.sum_insert hi], exact (abv.add_le _ _).trans (add_le_add le_rfl ih) }, end lemma is_absolute_value.abv_sum [semiring R] [ordered_semiring S] (abv : R β†’ S) [is_absolute_value abv] (f : ΞΉ β†’ R) (s : finset ΞΉ) : abv (βˆ‘ i in s, f i) ≀ βˆ‘ i in s, abv (f i) := (is_absolute_value.to_absolute_value abv).sum_le _ _ lemma absolute_value.map_prod [comm_semiring R] [nontrivial R] [linear_ordered_comm_ring S] (abv : absolute_value R S) (f : ΞΉ β†’ R) (s : finset ΞΉ) : abv (∏ i in s, f i) = ∏ i in s, abv (f i) := abv.to_monoid_hom.map_prod f s lemma is_absolute_value.map_prod [comm_semiring R] [nontrivial R] [linear_ordered_comm_ring S] (abv : R β†’ S) [is_absolute_value abv] (f : ΞΉ β†’ R) (s : finset ΞΉ) : abv (∏ i in s, f i) = ∏ i in s, abv (f i) := (is_absolute_value.to_absolute_value abv).map_prod _ _ end absolute_value
cf27e04bdd86dfb64b63c422f8a4ec7d85e200b3
4d3f29a7b2eff44af8fd0d3176232e039acb9ee3
/LAMR/Util/Propositional/Transformations.lean
ea64d7e3feff8e0cced4c7ecd3c1f227f50ed5f4
[]
no_license
marijnheule/lamr
5fc5d69d326ff92e321242cfd7f72e78d7f99d7e
28cc4114c7361059bb54f407fa312bf38b48728b
refs/heads/main
1,689,338,013,620
1,630,359,632,000
1,630,359,632,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,332
lean
import LAMR.Util.Propositional.Syntax namespace NnfForm def neg : NnfForm β†’ NnfForm | tr => fls | fls => tr | var n => negVar n | negVar n => var n | conj p q => disj (neg p) (neg q) | disj p q => conj (neg p) (neg q) end NnfForm namespace PropForm def toNnfForm : PropForm β†’ NnfForm | tr => NnfForm.tr | fls => NnfForm.fls | var n => NnfForm.var n | neg p => p.toNnfForm.neg | conj p q => NnfForm.conj p.toNnfForm q.toNnfForm | disj p q => NnfForm.disj p.toNnfForm q.toNnfForm | impl p q => NnfForm.disj p.toNnfForm.neg q.toNnfForm | biImpl p q => NnfForm.conj (NnfForm.disj p.toNnfForm.neg q.toNnfForm) (NnfForm.disj q.toNnfForm.neg p.toNnfForm) end PropForm /- Translation to CNF. -/ def List.Union [DecidableEq Ξ±]: List (List Ξ±) β†’ List Ξ± | [] => [] | (l ::ls) => l.union (ls.Union) def CnfForm.disj (cnf1 cnf2 : CnfForm) : CnfForm := (cnf1.map (fun cls => cnf2.map cls.union)).Union def NnfForm.toCnfForm : NnfForm β†’ CnfForm | NnfForm.var s => [ [Lit.pos s] ] | NnfForm.negVar s => [ [Lit.neg s] ] | NnfForm.tr => [] | NnfForm.fls => [ [] ] | NnfForm.conj A B => A.toCnfForm.union B.toCnfForm | NnfForm.disj A B => A.toCnfForm.disj B.toCnfForm def PropForm.toCnfForm (A : PropForm) : CnfForm := A.toNnfForm.toCnfForm
1eb6f4680254266390ad44d37039d75c5f6f8197
c3f2fcd060adfa2ca29f924839d2d925e8f2c685
/library/init/prod.lean
a515e9bcb01db17498c091d68d58aad4b6763975
[ "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
3,445
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: init.prod Author: Leonardo de Moura, Jeremy Avigad -/ prelude import init.num init.wf definition pair := @prod.mk notation A Γ— B := prod A B -- notation for n-ary tuples notation `(` h `,` t:(foldl `,` (e r, prod.mk r e) h) `)` := t namespace prod notation A * B := prod A B notation A Γ— B := prod A B -- repeat, so this takes precedence namespace low_precedence_times reserve infixr `*`:30 -- conflicts with notation for multiplication infixr `*` := prod end low_precedence_times notation `pr₁` := pr1 notation `prβ‚‚` := pr2 namespace ops postfix `.1`:(max+1) := pr1 postfix `.2`:(max+1) := pr2 end ops open well_founded section variables {A B : Type} variable (Ra : A β†’ A β†’ Prop) variable (Rb : B β†’ B β†’ Prop) -- Lexicographical order based on Ra and Rb inductive lex : A Γ— B β†’ A Γ— B β†’ Prop := | left : βˆ€{a₁ b₁} aβ‚‚ bβ‚‚, Ra a₁ aβ‚‚ β†’ lex (a₁, b₁) (aβ‚‚, bβ‚‚) | right : βˆ€a {b₁ bβ‚‚}, Rb b₁ bβ‚‚ β†’ lex (a, b₁) (a, bβ‚‚) -- Relational product based on Ra and Rb inductive rprod : A Γ— B β†’ A Γ— B β†’ Prop := intro : βˆ€{a₁ b₁ aβ‚‚ bβ‚‚}, Ra a₁ aβ‚‚ β†’ Rb b₁ bβ‚‚ β†’ rprod (a₁, b₁) (aβ‚‚, bβ‚‚) end context parameters {A B : Type} parameters {Ra : A β†’ A β†’ Prop} {Rb : B β†’ B β†’ Prop} infix `β‰Ί`:50 := lex Ra Rb definition lex.accessible {a} (aca : acc Ra a) (acb : βˆ€b, acc Rb b): βˆ€b, acc (lex Ra Rb) (a, b) := acc.rec_on aca (Ξ»xa aca (iHa : βˆ€y, Ra y xa β†’ βˆ€b, acc (lex Ra Rb) (y, b)), Ξ»b, acc.rec_on (acb b) (Ξ»xb acb (iHb : βˆ€y, Rb y xb β†’ acc (lex Ra Rb) (xa, y)), acc.intro (xa, xb) (Ξ»p (lt : p β‰Ί (xa, xb)), have aux : xa = xa β†’ xb = xb β†’ acc (lex Ra Rb) p, from @prod.lex.rec_on A B Ra Rb (Ξ»p₁ pβ‚‚, pr₁ pβ‚‚ = xa β†’ prβ‚‚ pβ‚‚ = xb β†’ acc (lex Ra Rb) p₁) p (xa, xb) lt (Ξ»a₁ b₁ aβ‚‚ bβ‚‚ (H : Ra a₁ aβ‚‚) (eqβ‚‚ : aβ‚‚ = xa) (eq₃ : bβ‚‚ = xb), show acc (lex Ra Rb) (a₁, b₁), from have Ra₁ : Ra a₁ xa, from eq.rec_on eqβ‚‚ H, iHa a₁ Ra₁ b₁) (Ξ»a b₁ bβ‚‚ (H : Rb b₁ bβ‚‚) (eqβ‚‚ : a = xa) (eq₃ : bβ‚‚ = xb), show acc (lex Ra Rb) (a, b₁), from have Rb₁ : Rb b₁ xb, from eq.rec_on eq₃ H, have eqβ‚‚' : xa = a, from eq.rec_on eqβ‚‚ rfl, eq.rec_on eqβ‚‚' (iHb b₁ Rb₁)), aux rfl rfl))) -- The lexicographical order of well founded relations is well-founded definition lex.wf (Ha : well_founded Ra) (Hb : well_founded Rb) : well_founded (lex Ra Rb) := well_founded.intro (Ξ»p, destruct p (Ξ»a b, lex.accessible (Ha a) (well_founded.apply Hb) b)) -- Relational product is a subrelation of the lex definition rprod.sub_lex : βˆ€ a b, rprod Ra Rb a b β†’ lex Ra Rb a b := Ξ»a b H, prod.rprod.rec_on H (Ξ» a₁ b₁ aβ‚‚ bβ‚‚ H₁ Hβ‚‚, lex.left Rb aβ‚‚ bβ‚‚ H₁) -- The relational product of well founded relations is well-founded definition rprod.wf (Ha : well_founded Ra) (Hb : well_founded Rb) : well_founded (rprod Ra Rb) := subrelation.wf (rprod.sub_lex) (lex.wf Ha Hb) end end prod
c4dc4e3c8322a21a17fc2ff42cbdcc4278e897d1
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/tactic/cache.lean
b34d3af036d5d67cf1303a45cdf56d5d27485fb5
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,234
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import tactic.doc_commands /-! # Instance cache tactics For performance reasons, Lean does not automatically update its database of class instances during a proof. The group of tactics in this file helps to force such updates. -/ open lean.parser open interactive interactive.types local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic /-- Reset the instance cache for the main goal. -/ meta def reset_instance_cache : tactic unit := do unfreeze_local_instances, freeze_local_instances /-- Unfreeze the local instances while executing `tac` on the main goal. -/ meta def unfreezing {Ξ±} (tac : tactic Ξ±) : tactic Ξ± := focus1 $ unfreeze_local_instances *> tac <* all_goals freeze_local_instances /-- Unfreeze local instances while executing `tac`, if the passed expression is amongst the frozen instances. -/ meta def unfreezing_hyp (h : expr) (tac : tactic unit) : tactic unit := do frozen ← frozen_local_instances, if h ∈ frozen.get_or_else [] then unfreezing tac else tac namespace interactive /-- `unfreezingI { tac }` executes tac while temporarily unfreezing the instance cache. -/ meta def unfreezingI (tac : itactic) := unfreezing tac /-- Reset the instance cache. This allows any new instances added to the context to be used in typeclass inference. -/ meta def resetI := reset_instance_cache /-- Like `subst`, but can also substitute in instance arguments. -/ meta def substI (q : parse texpr) : tactic unit := unfreezingI (subst q) /-- Like `cases`, but can also be used with instance arguments. -/ meta def casesI (p : parse cases_arg_p) (q : parse with_ident_list) : tactic unit := unfreezingI (cases p q) /-- Like `intro`, but uses the introduced variable in typeclass inference. -/ meta def introI (p : parse ident_?) : tactic unit := intro p >> reset_instance_cache /-- Like `intros`, but uses the introduced variable(s) in typeclass inference. -/ meta def introsI (p : parse ident_*) : tactic unit := intros p >> reset_instance_cache /-- Used to add typeclasses to the context so that they can be used in typeclass inference. The syntax is the same as `have`. -/ meta def haveI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (qβ‚‚ : parse (tk ":=" *> texpr)?) : tactic unit := do h ← match h with | none := get_unused_name "_inst" | some a := return a end, Β«haveΒ» (some h) q₁ qβ‚‚, match qβ‚‚ with | none := swap >> reset_instance_cache >> swap | some pβ‚‚ := reset_instance_cache end /-- Used to add typeclasses to the context so that they can be used in typeclass inference. The syntax is the same as `let`. -/ meta def letI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (qβ‚‚ : parse $ (tk ":=" *> texpr)?) : tactic unit := do h ← match h with | none := get_unused_name "_inst" | some a := return a end, Β«letΒ» (some h) q₁ qβ‚‚, match qβ‚‚ with | none := swap >> reset_instance_cache >> swap | some pβ‚‚ := reset_instance_cache end /-- Like `exact`, but uses all variables in the context for typeclass inference. -/ meta def exactI (q : parse texpr) : tactic unit := reset_instance_cache >> exact q /-- For performance reasons, Lean does not automatically update its database of class instances during a proof. The group of tactics described below helps to force such updates. For a simple (but very artificial) example, consider the function `default` from the core library. It has type `Ξ  (Ξ± : Sort u) [inhabited Ξ±], Ξ±`, so one can use `default Ξ±` only if Lean can find a registered instance of `inhabited Ξ±`. Because the database of such instance is not automatically updated during a proof, the following attempt won't work (Lean will not pick up the instance from the local context): ```lean def my_id (Ξ± : Type) : Ξ± β†’ Ξ± := begin intro x, have : inhabited Ξ± := ⟨x⟩, exact default Ξ±, -- Won't work! end ``` However, it will work, producing the identity function, if one replaces `have` by its variant `haveI` described below. * `resetI`: Reset the instance cache. This allows any instances currently in the context to be used in typeclass inference. * `unfreezingI { tac }`: Unfreeze local instances while executing the tactic `tac`. * `introI`/`introsI`: `intro`/`intros` followed by `resetI`. Like `intro`/`intros`, but uses the introduced variable in typeclass inference. * `casesI`: like `cases`, but can also be used with instance arguments. * `substI`: like `subst`, but can also substitute in type-class arguments * `haveI`/`letI`: `have`/`let` followed by `resetI`. Used to add typeclasses to the context so that they can be used in typeclass inference. * `exactI`: `resetI` followed by `exact`. Like `exact`, but uses all variables in the context for typeclass inference. -/ add_tactic_doc { name := "Instance cache tactics", category := doc_category.tactic, decl_names := [``resetI, ``unfreezingI, ``casesI, ``substI, ``introI, ``introsI, ``haveI, ``letI, ``exactI], tags := ["type class", "context management"] } end interactive end tactic
fd38f3146755ce14d1dae9c36672bda2a0642f00
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Lean/Elab/InfoTree.lean
8bd57827ba0be236f6a13a7e473b3a617b91a665
[ "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
15,236
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki, Leonardo de Moura, Sebastian Ullrich -/ import Lean.Data.Position import Lean.Expr import Lean.Message import Lean.Data.Json import Lean.Meta.Basic import Lean.Meta.PPGoal namespace Lean.Elab open Std (PersistentArray PersistentArray.empty PersistentHashMap) /- Context after executing `liftTermElabM`. Note that the term information collected during elaboration may contain metavariables, and their assignments are stored at `mctx`. -/ structure ContextInfo where env : Environment fileMap : FileMap mctx : MetavarContext := {} options : Options := {} currNamespace : Name := Name.anonymous openDecls : List OpenDecl := [] deriving Inhabited /-- An elaboration step -/ structure ElabInfo where elaborator : Name stx : Syntax deriving Inhabited structure TermInfo extends ElabInfo where lctx : LocalContext -- The local context when the term was elaborated. expectedType? : Option Expr expr : Expr deriving Inhabited structure CommandInfo extends ElabInfo where deriving Inhabited inductive CompletionInfo where | dot (termInfo : TermInfo) (field? : Option Syntax) (expectedType? : Option Expr) | id (stx : Syntax) (id : Name) (danglingDot : Bool) (lctx : LocalContext) (expectedType? : Option Expr) | namespaceId (stx : Syntax) | option (stx : Syntax) | endSection (stx : Syntax) (scopeNames : List String) | tactic (stx : Syntax) (goals : List MVarId) -- TODO `import` def CompletionInfo.stx : CompletionInfo β†’ Syntax | dot i .. => i.stx | id stx .. => stx | namespaceId stx => stx | option stx => stx | endSection stx .. => stx | tactic stx .. => stx structure FieldInfo where /-- Name of the projection. -/ projName : Name /-- Name of the field as written. -/ fieldName : Name lctx : LocalContext val : Expr stx : Syntax deriving Inhabited /- We store the list of goals before and after the execution of a tactic. We also store the metavariable context at each time since, we want to unassigned metavariables at tactic execution time to be displayed as `?m...`. -/ structure TacticInfo extends ElabInfo where mctxBefore : MetavarContext goalsBefore : List MVarId mctxAfter : MetavarContext goalsAfter : List MVarId deriving Inhabited structure MacroExpansionInfo where lctx : LocalContext -- The local context when the macro was expanded. stx : Syntax output : Syntax deriving Inhabited inductive Info where | ofTacticInfo (i : TacticInfo) | ofTermInfo (i : TermInfo) | ofCommandInfo (i : CommandInfo) | ofMacroExpansionInfo (i : MacroExpansionInfo) | ofFieldInfo (i : FieldInfo) | ofCompletionInfo (i : CompletionInfo) deriving Inhabited inductive InfoTree where | context (i : ContextInfo) (t : InfoTree) -- The context object is created by `liftTermElabM` at `Command.lean` | node (i : Info) (children : PersistentArray InfoTree) -- The children contains information for nested term elaboration and tactic evaluation | ofJson (j : Json) -- For user data | hole (mvarId : MVarId) -- The elaborator creates holes (aka metavariables) for tactics and postponed terms deriving Inhabited partial def InfoTree.findInfo? (p : Info β†’ Bool) (t : InfoTree) : Option InfoTree := match t with | context _ t => findInfo? p t | node i ts => if p i then some t else ts.findSome? (findInfo? p) | _ => none structure InfoState where enabled : Bool := false assignment : PersistentHashMap MVarId InfoTree := {} -- map from holeId to InfoTree trees : PersistentArray InfoTree := {} deriving Inhabited class MonadInfoTree (m : Type β†’ Type) where getInfoState : m InfoState modifyInfoState : (InfoState β†’ InfoState) β†’ m Unit export MonadInfoTree (getInfoState modifyInfoState) instance [MonadLift m n] [MonadInfoTree m] : MonadInfoTree n where getInfoState := liftM (getInfoState : m _) modifyInfoState f := liftM (modifyInfoState f : m _) partial def InfoTree.substitute (tree : InfoTree) (assignment : PersistentHashMap MVarId InfoTree) : InfoTree := match tree with | node i c => node i <| c.map (substitute Β· assignment) | context i t => context i (substitute t assignment) | ofJson j => ofJson j | hole id => match assignment.find? id with | none => hole id | some tree => substitute tree assignment def ContextInfo.runMetaM (info : ContextInfo) (lctx : LocalContext) (x : MetaM Ξ±) : IO Ξ± := do let x := x.run { lctx := lctx } { mctx := info.mctx } let ((a, _), _) ← x.toIO { options := info.options, currNamespace := info.currNamespace, openDecls := info.openDecls } { env := info.env } return a def ContextInfo.toPPContext (info : ContextInfo) (lctx : LocalContext) : PPContext := { env := info.env, mctx := info.mctx, lctx := lctx, opts := info.options, currNamespace := info.currNamespace, openDecls := info.openDecls } def ContextInfo.ppSyntax (info : ContextInfo) (lctx : LocalContext) (stx : Syntax) : IO Format := do ppTerm (info.toPPContext lctx) stx private def formatStxRange (ctx : ContextInfo) (stx : Syntax) : Format := do let pos := stx.getPos?.getD 0 let endPos := stx.getTailPos?.getD pos return f!"{fmtPos pos stx.getHeadInfo}-{fmtPos endPos stx.getTailInfo}" where fmtPos pos info := let pos := format <| ctx.fileMap.toPosition pos match info with | SourceInfo.original .. => pos | _ => f!"{pos}†" private def formatElabInfo (ctx : ContextInfo) (info : ElabInfo) : Format := if info.elaborator.isAnonymous then formatStxRange ctx info.stx else f!"{formatStxRange ctx info.stx} @ {info.elaborator}" def TermInfo.runMetaM (info : TermInfo) (ctx : ContextInfo) (x : MetaM Ξ±) : IO Ξ± := ctx.runMetaM info.lctx x def TermInfo.format (ctx : ContextInfo) (info : TermInfo) : IO Format := do info.runMetaM ctx do try return f!"{← Meta.ppExpr info.expr} : {← Meta.ppExpr (← Meta.inferType info.expr)} @ {formatElabInfo ctx info.toElabInfo}" catch _ => return f!"{← Meta.ppExpr info.expr} : <failed-to-infer-type> @ {formatElabInfo ctx info.toElabInfo}" def CompletionInfo.format (ctx : ContextInfo) (info : CompletionInfo) : IO Format := match info with | CompletionInfo.dot i (expectedType? := expectedType?) .. => return f!"[.] {← i.format ctx} : {expectedType?}" | CompletionInfo.id stx _ _ lctx expectedType? => ctx.runMetaM lctx do return f!"[.] {stx} : {expectedType?} @ {formatStxRange ctx info.stx}" | _ => return f!"[.] {info.stx} @ {formatStxRange ctx info.stx}" def CommandInfo.format (ctx : ContextInfo) (info : CommandInfo) : IO Format := do return f!"command @ {formatElabInfo ctx info.toElabInfo}" def FieldInfo.format (ctx : ContextInfo) (info : FieldInfo) : IO Format := do ctx.runMetaM info.lctx do return f!"{info.fieldName} : {← Meta.ppExpr (← Meta.inferType info.val)} := {← Meta.ppExpr info.val} @ {formatStxRange ctx info.stx}" def ContextInfo.ppGoals (ctx : ContextInfo) (goals : List MVarId) : IO Format := if goals.isEmpty then return "no goals" else ctx.runMetaM {} (return Std.Format.prefixJoin "\n" (← goals.mapM Meta.ppGoal)) def TacticInfo.format (ctx : ContextInfo) (info : TacticInfo) : IO Format := do let ctxB := { ctx with mctx := info.mctxBefore } let ctxA := { ctx with mctx := info.mctxAfter } let goalsBefore ← ctxB.ppGoals info.goalsBefore let goalsAfter ← ctxA.ppGoals info.goalsAfter return f!"Tactic @ {formatElabInfo ctx info.toElabInfo}\n{info.stx}\nbefore {goalsBefore}\nafter {goalsAfter}" def MacroExpansionInfo.format (ctx : ContextInfo) (info : MacroExpansionInfo) : IO Format := do let stx ← ctx.ppSyntax info.lctx info.stx let output ← ctx.ppSyntax info.lctx info.output return f!"Macro expansion\n{stx}\n===>\n{output}" def Info.format (ctx : ContextInfo) : Info β†’ IO Format | ofTacticInfo i => i.format ctx | ofTermInfo i => i.format ctx | ofCommandInfo i => i.format ctx | ofMacroExpansionInfo i => i.format ctx | ofFieldInfo i => i.format ctx | ofCompletionInfo i => i.format ctx def Info.toElabInfo? : Info β†’ Option ElabInfo | ofTacticInfo i => some i.toElabInfo | ofTermInfo i => some i.toElabInfo | ofCommandInfo i => some i.toElabInfo | ofMacroExpansionInfo i => none | ofFieldInfo i => none | ofCompletionInfo i => none /-- Helper function for propagating the tactic metavariable context to its children nodes. We need this function because we preserve `TacticInfo` nodes during backtracking *and* their children. Moreover, we backtrack the metavariable context to undo metavariable assignments. `TacticInfo` nodes save the metavariable context before/after the tactic application, and can be pretty printed without any extra information. This is not the case for `TermInfo` nodes. Without this function, the formatting method would often fail when processing `TermInfo` nodes that are children of `TacticInfo` nodes that have been preserved during backtracking. Saving the metavariable context at `TermInfo` nodes is also not a good option because at `TermInfo` creation time, the metavariable context often miss information, e.g., a TC problem has not been resolved, a postponed subterm has not been elaborated, etc. See `Term.SavedState.restore`. -/ def Info.updateContext? : Option ContextInfo β†’ Info β†’ Option ContextInfo | some ctx, ofTacticInfo i => some { ctx with mctx := i.mctxAfter } | ctx?, _ => ctx? partial def InfoTree.format (tree : InfoTree) (ctx? : Option ContextInfo := none) : IO Format := do match tree with | ofJson j => return toString j | hole id => return toString id | context i t => format t i | node i cs => match ctx? with | none => return "<context-not-available>" | some ctx => let fmt ← i.format ctx if cs.size == 0 then return fmt else let ctx? := i.updateContext? ctx? return f!"{fmt}{Std.Format.nestD <| Std.Format.prefixJoin "\n" (← cs.toList.mapM fun c => format c ctx?)}" section variable [Monad m] [MonadInfoTree m] @[inline] private def modifyInfoTrees (f : PersistentArray InfoTree β†’ PersistentArray InfoTree) : m Unit := modifyInfoState fun s => { s with trees := f s.trees } def getResetInfoTrees : m (PersistentArray InfoTree) := do let trees := (← getInfoState).trees modifyInfoTrees fun _ => {} return trees def pushInfoTree (t : InfoTree) : m Unit := do if (← getInfoState).enabled then modifyInfoTrees fun ts => ts.push t def pushInfoLeaf (t : Info) : m Unit := do if (← getInfoState).enabled then pushInfoTree <| InfoTree.node (children := {}) t def addCompletionInfo (info : CompletionInfo) : m Unit := do pushInfoLeaf <| Info.ofCompletionInfo info def resolveGlobalConstNoOverloadWithInfo [MonadResolveName m] [MonadEnv m] [MonadError m] (stx : Syntax) (id := stx.getId) (expectedType? : Option Expr := none) : m Name := do let n ← resolveGlobalConstNoOverload id if (← getInfoState).enabled then -- we do not store a specific elaborator since identifiers are special-cased by the server anyway pushInfoLeaf <| Info.ofTermInfo { elaborator := Name.anonymous, lctx := LocalContext.empty, expr := (← mkConstWithLevelParams n), stx, expectedType? } return n def resolveGlobalConstWithInfos [MonadResolveName m] [MonadEnv m] [MonadError m] (stx : Syntax) (id := stx.getId) (expectedType? : Option Expr := none) : m (List Name) := do let ns ← resolveGlobalConst id if (← getInfoState).enabled then for n in ns do pushInfoLeaf <| Info.ofTermInfo { elaborator := Name.anonymous, lctx := LocalContext.empty, expr := (← mkConstWithLevelParams n), stx, expectedType? } return ns def withInfoContext' [MonadFinally m] (x : m Ξ±) (mkInfo : Ξ± β†’ m (Sum Info MVarId)) : m Ξ± := do if (← getInfoState).enabled then let treesSaved ← getResetInfoTrees Prod.fst <$> MonadFinally.tryFinally' x fun a? => do match a? with | none => modifyInfoTrees fun _ => treesSaved | some a => let info ← mkInfo a modifyInfoTrees fun trees => match info with | Sum.inl info => treesSaved.push <| InfoTree.node info trees | Sum.inr mvaId => treesSaved.push <| InfoTree.hole mvaId else x def withInfoTreeContext [MonadFinally m] (x : m Ξ±) (mkInfoTree : PersistentArray InfoTree β†’ m InfoTree) : m Ξ± := do if (← getInfoState).enabled then let treesSaved ← getResetInfoTrees Prod.fst <$> MonadFinally.tryFinally' x fun _ => do let st ← getInfoState let tree ← mkInfoTree st.trees modifyInfoTrees fun _ => treesSaved.push tree else x @[inline] def withInfoContext [MonadFinally m] (x : m Ξ±) (mkInfo : m Info) : m Ξ± := do withInfoTreeContext x (fun trees => do return InfoTree.node (← mkInfo) trees) def withSaveInfoContext [MonadFinally m] [MonadEnv m] [MonadOptions m] [MonadMCtx m] [MonadResolveName m] [MonadFileMap m] (x : m Ξ±) : m Ξ± := do if (← getInfoState).enabled then let treesSaved ← getResetInfoTrees Prod.fst <$> MonadFinally.tryFinally' x fun _ => do let st ← getInfoState let trees ← st.trees.mapM fun tree => do let tree := tree.substitute st.assignment InfoTree.context { env := (← getEnv), fileMap := (← getFileMap), mctx := (← getMCtx), currNamespace := (← getCurrNamespace), openDecls := (← getOpenDecls), options := (← getOptions) } tree modifyInfoTrees fun _ => treesSaved ++ trees else x def getInfoHoleIdAssignment? (mvarId : MVarId) : m (Option InfoTree) := return (← getInfoState).assignment[mvarId] def assignInfoHoleId (mvarId : MVarId) (infoTree : InfoTree) : m Unit := do assert! (← getInfoHoleIdAssignment? mvarId).isNone modifyInfoState fun s => { s with assignment := s.assignment.insert mvarId infoTree } end def withMacroExpansionInfo [MonadFinally m] [Monad m] [MonadInfoTree m] [MonadLCtx m] (stx output : Syntax) (x : m Ξ±) : m Ξ± := let mkInfo : m Info := do return Info.ofMacroExpansionInfo { lctx := (← getLCtx) stx, output } withInfoContext x mkInfo @[inline] def withInfoHole [MonadFinally m] [Monad m] [MonadInfoTree m] (mvarId : MVarId) (x : m Ξ±) : m Ξ± := do if (← getInfoState).enabled then let treesSaved ← getResetInfoTrees Prod.fst <$> MonadFinally.tryFinally' x fun a? => modifyInfoState fun s => if s.trees.size > 0 then { s with trees := treesSaved, assignment := s.assignment.insert mvarId s.trees[s.trees.size - 1] } else { s with trees := treesSaved } else x def enableInfoTree [MonadInfoTree m] (flag := true) : m Unit := modifyInfoState fun s => { s with enabled := flag } def getInfoTrees [MonadInfoTree m] [Monad m] : m (PersistentArray InfoTree) := return (← getInfoState).trees end Lean.Elab
90ce523f97a4d262749ad6618726054168a83f55
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/data/equiv/basic.lean
5349e7d32cb58d790ed1bc1ba88416b37ebf3bc6
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
103,837
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.set.function import data.sigma.basic /-! # Equivalence between types In this file we define two types: * `equiv Ξ± Ξ²` a.k.a. `Ξ± ≃ Ξ²`: a bijective map `Ξ± β†’ Ξ²` bundled with its inverse map; we use this (and not equality!) to express that various `Type`s or `Sort`s are equivalent. * `equiv.perm Ξ±`: the group of permutations `Ξ± ≃ Ξ±`. More lemmas about `equiv.perm` can be found in `group_theory/perm`. Then we define * canonical isomorphisms between various types: e.g., - `equiv.refl Ξ±` is the identity map interpreted as `Ξ± ≃ Ξ±`; - `equiv.sum_equiv_sigma_bool` is the canonical equivalence between the sum of two types `Ξ± βŠ• Ξ²` and the sigma-type `Ξ£ b : bool, cond b Ξ± Ξ²`; - `equiv.prod_sum_distrib : Ξ± Γ— (Ξ² βŠ• Ξ³) ≃ (Ξ± Γ— Ξ²) βŠ• (Ξ± Γ— Ξ³)` shows that type product and type sum satisfy the distributive law up to a canonical equivalence; * operations on equivalences: e.g., - `equiv.symm e : Ξ² ≃ Ξ±` is the inverse of `e : Ξ± ≃ Ξ²`; - `equiv.trans e₁ eβ‚‚ : Ξ± ≃ Ξ³` is the composition of `e₁ : Ξ± ≃ Ξ²` and `eβ‚‚ : Ξ² ≃ Ξ³` (note the order of the arguments!); - `equiv.prod_congr ea eb : α₁ Γ— β₁ ≃ Ξ±β‚‚ Γ— Ξ²β‚‚`: combine two equivalences `ea : α₁ ≃ Ξ±β‚‚` and `eb : β₁ ≃ Ξ²β‚‚` using `prod.map`. * definitions that transfer some instances along an equivalence. By convention, we transfer instances from right to left. - `equiv.inhabited` takes `e : Ξ± ≃ Ξ²` and `[inhabited Ξ²]` and returns `inhabited Ξ±`; - `equiv.unique` takes `e : Ξ± ≃ Ξ²` and `[unique Ξ²]` and returns `unique Ξ±`; - `equiv.decidable_eq` takes `e : Ξ± ≃ Ξ²` and `[decidable_eq Ξ²]` and returns `decidable_eq Ξ±`. More definitions of this kind can be found in other files. E.g., `data/equiv/transfer_instance` does it for many algebraic type classes like `group`, `module`, etc. ## Tags equivalence, congruence, bijective map -/ open function universes u v w z variables {Ξ± : Sort u} {Ξ² : Sort v} {Ξ³ : Sort w} /-- `Ξ± ≃ Ξ²` is the type of functions from `Ξ± β†’ Ξ²` with a two-sided inverse. -/ @[nolint has_inhabited_instance] structure equiv (Ξ± : Sort*) (Ξ² : Sort*) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) infix ` ≃ `:25 := equiv /-- Convert an involutive function `f` to an equivalence with `to_fun = inv_fun = f`. -/ def function.involutive.to_equiv (f : Ξ± β†’ Ξ±) (h : involutive f) : Ξ± ≃ Ξ± := ⟨f, f, h.left_inverse, h.right_inverse⟩ namespace equiv /-- `perm Ξ±` is the type of bijections from `Ξ±` to itself. -/ @[reducible] def perm (Ξ± : Sort*) := equiv Ξ± Ξ± instance : has_coe_to_fun (Ξ± ≃ Ξ²) := ⟨_, to_fun⟩ @[simp] theorem coe_fn_mk (f : Ξ± β†’ Ξ²) (g l r) : (equiv.mk f g l r : Ξ± β†’ Ξ²) = f := rfl /-- The map `coe_fn : (r ≃ s) β†’ (r β†’ s)` is injective. -/ theorem coe_fn_injective : @function.injective (Ξ± ≃ Ξ²) (Ξ± β†’ Ξ²) coe_fn | ⟨f₁, g₁, l₁, rβ‚βŸ© ⟨fβ‚‚, gβ‚‚, lβ‚‚, rβ‚‚βŸ© h := have f₁ = fβ‚‚, from h, have g₁ = gβ‚‚, from l₁.eq_right_inverse (this.symm β–Έ rβ‚‚), by simp * @[simp, norm_cast] protected lemma coe_inj {e₁ eβ‚‚ : Ξ± ≃ Ξ²} : ⇑e₁ = eβ‚‚ ↔ e₁ = eβ‚‚ := coe_fn_injective.eq_iff @[ext] lemma ext {f g : equiv Ξ± Ξ²} (H : βˆ€ x, f x = g x) : f = g := coe_fn_injective (funext H) protected lemma congr_arg {f : equiv Ξ± Ξ²} : Ξ  {x x' : Ξ±}, x = x' β†’ f x = f x' | _ _ rfl := rfl protected lemma congr_fun {f g : equiv Ξ± Ξ²} (h : f = g) (x : Ξ±) : f x = g x := h β–Έ rfl lemma ext_iff {f g : equiv Ξ± Ξ²} : f = g ↔ βˆ€ x, f x = g x := ⟨λ h x, h β–Έ rfl, ext⟩ @[ext] lemma perm.ext {Οƒ Ο„ : equiv.perm Ξ±} (H : βˆ€ x, Οƒ x = Ο„ x) : Οƒ = Ο„ := equiv.ext H protected lemma perm.congr_arg {f : equiv.perm Ξ±} {x x' : Ξ±} : x = x' β†’ f x = f x' := equiv.congr_arg protected lemma perm.congr_fun {f g : equiv.perm Ξ±} (h : f = g) (x : Ξ±) : f x = g x := equiv.congr_fun h x lemma perm.ext_iff {Οƒ Ο„ : equiv.perm Ξ±} : Οƒ = Ο„ ↔ βˆ€ x, Οƒ x = Ο„ x := ext_iff /-- Any type is equivalent to itself. -/ @[refl] protected def refl (Ξ± : Sort*) : Ξ± ≃ Ξ± := ⟨id, id, Ξ» x, rfl, Ξ» x, rfl⟩ instance inhabited' : inhabited (Ξ± ≃ Ξ±) := ⟨equiv.refl α⟩ /-- Inverse of an equivalence `e : Ξ± ≃ Ξ²`. -/ @[symm] protected def symm (e : Ξ± ≃ Ξ²) : Ξ² ≃ Ξ± := ⟨e.inv_fun, e.to_fun, e.right_inv, e.left_inv⟩ /-- See Note [custom simps projection] -/ def simps.symm_apply (e : Ξ± ≃ Ξ²) : Ξ² β†’ Ξ± := e.symm initialize_simps_projections equiv (to_fun β†’ apply, inv_fun β†’ symm_apply) -- Generate the `simps` projections for previously defined equivs. attribute [simps] function.involutive.to_equiv /-- Composition of equivalences `e₁ : Ξ± ≃ Ξ²` and `eβ‚‚ : Ξ² ≃ Ξ³`. -/ @[trans] protected def trans (e₁ : Ξ± ≃ Ξ²) (eβ‚‚ : Ξ² ≃ Ξ³) : Ξ± ≃ Ξ³ := ⟨eβ‚‚ ∘ e₁, e₁.symm ∘ eβ‚‚.symm, eβ‚‚.left_inv.comp e₁.left_inv, eβ‚‚.right_inv.comp e₁.right_inv⟩ @[simp] lemma to_fun_as_coe (e : Ξ± ≃ Ξ²) : e.to_fun = e := rfl @[simp] lemma inv_fun_as_coe (e : Ξ± ≃ Ξ²) : e.inv_fun = e.symm := rfl protected theorem injective (e : Ξ± ≃ Ξ²) : injective e := e.left_inv.injective protected theorem surjective (e : Ξ± ≃ Ξ²) : surjective e := e.right_inv.surjective protected theorem bijective (f : Ξ± ≃ Ξ²) : bijective f := ⟨f.injective, f.surjective⟩ @[simp] lemma range_eq_univ {Ξ± : Type*} {Ξ² : Type*} (e : Ξ± ≃ Ξ²) : set.range e = set.univ := set.eq_univ_of_forall e.surjective protected theorem subsingleton (e : Ξ± ≃ Ξ²) [subsingleton Ξ²] : subsingleton Ξ± := e.injective.subsingleton protected theorem subsingleton.symm (e : Ξ± ≃ Ξ²) [subsingleton Ξ±] : subsingleton Ξ² := e.symm.injective.subsingleton lemma subsingleton_congr (e : Ξ± ≃ Ξ²) : subsingleton Ξ± ↔ subsingleton Ξ² := ⟨λ h, by exactI e.symm.subsingleton, Ξ» h, by exactI e.subsingleton⟩ instance equiv_subsingleton_cod [subsingleton Ξ²] : subsingleton (Ξ± ≃ Ξ²) := ⟨λ f g, equiv.ext $ Ξ» x, subsingleton.elim _ _⟩ instance equiv_subsingleton_dom [subsingleton Ξ±] : subsingleton (Ξ± ≃ Ξ²) := ⟨λ f g, equiv.ext $ Ξ» x, @subsingleton.elim _ (equiv.subsingleton.symm f) _ _⟩ instance perm_unique [subsingleton Ξ±] : unique (perm Ξ±) := unique_of_subsingleton (equiv.refl Ξ±) lemma perm.subsingleton_eq_refl [subsingleton Ξ±] (e : perm Ξ±) : e = equiv.refl Ξ± := subsingleton.elim _ _ /-- Transfer `decidable_eq` across an equivalence. -/ protected def decidable_eq (e : Ξ± ≃ Ξ²) [decidable_eq Ξ²] : decidable_eq Ξ± := e.injective.decidable_eq lemma nonempty_iff_nonempty (e : Ξ± ≃ Ξ²) : nonempty Ξ± ↔ nonempty Ξ² := nonempty.congr e e.symm /-- If `Ξ± ≃ Ξ²` and `Ξ²` is inhabited, then so is `Ξ±`. -/ protected def inhabited [inhabited Ξ²] (e : Ξ± ≃ Ξ²) : inhabited Ξ± := ⟨e.symm (default _)⟩ /-- If `Ξ± ≃ Ξ²` and `Ξ²` is a singleton type, then so is `Ξ±`. -/ protected def unique [unique Ξ²] (e : Ξ± ≃ Ξ²) : unique Ξ± := e.symm.surjective.unique /-- Equivalence between equal types. -/ protected def cast {Ξ± Ξ² : Sort*} (h : Ξ± = Ξ²) : Ξ± ≃ Ξ² := ⟨cast h, cast h.symm, Ξ» x, by { cases h, refl }, Ξ» x, by { cases h, refl }⟩ @[simp] theorem coe_fn_symm_mk (f : Ξ± β†’ Ξ²) (g l r) : ((equiv.mk f g l r).symm : Ξ² β†’ Ξ±) = g := rfl @[simp] theorem coe_refl : ⇑(equiv.refl Ξ±) = id := rfl @[simp] theorem perm.coe_subsingleton {Ξ± : Type*} [subsingleton Ξ±] (e : perm Ξ±) : ⇑(e) = id := by rw [perm.subsingleton_eq_refl e, coe_refl] theorem refl_apply (x : Ξ±) : equiv.refl Ξ± x = x := rfl @[simp] theorem coe_trans (f : Ξ± ≃ Ξ²) (g : Ξ² ≃ Ξ³) : ⇑(f.trans g) = g ∘ f := rfl theorem trans_apply (f : Ξ± ≃ Ξ²) (g : Ξ² ≃ Ξ³) (a : Ξ±) : (f.trans g) a = g (f a) := rfl @[simp] theorem apply_symm_apply (e : Ξ± ≃ Ξ²) (x : Ξ²) : e (e.symm x) = x := e.right_inv x @[simp] theorem symm_apply_apply (e : Ξ± ≃ Ξ²) (x : Ξ±) : e.symm (e x) = x := e.left_inv x @[simp] theorem symm_comp_self (e : Ξ± ≃ Ξ²) : e.symm ∘ e = id := funext e.symm_apply_apply @[simp] theorem self_comp_symm (e : Ξ± ≃ Ξ²) : e ∘ e.symm = id := funext e.apply_symm_apply @[simp] lemma symm_trans_apply (f : Ξ± ≃ Ξ²) (g : Ξ² ≃ Ξ³) (a : Ξ³) : (f.trans g).symm a = f.symm (g.symm a) := rfl -- The `simp` attribute is needed to make this a `dsimp` lemma. -- `simp` will always rewrite with `equiv.symm_symm` before this has a chance to fire. @[simp, nolint simp_nf] theorem symm_symm_apply (f : Ξ± ≃ Ξ²) (b : Ξ±) : f.symm.symm b = f b := rfl @[simp] theorem apply_eq_iff_eq (f : Ξ± ≃ Ξ²) {x y : Ξ±} : f x = f y ↔ x = y := f.injective.eq_iff theorem apply_eq_iff_eq_symm_apply {Ξ± Ξ² : Sort*} (f : Ξ± ≃ Ξ²) {x : Ξ±} {y : Ξ²} : f x = y ↔ x = f.symm y := begin conv_lhs { rw ←apply_symm_apply f y, }, rw apply_eq_iff_eq, end @[simp] theorem cast_apply {Ξ± Ξ²} (h : Ξ± = Ξ²) (x : Ξ±) : equiv.cast h x = cast h x := rfl @[simp] theorem cast_symm {Ξ± Ξ²} (h : Ξ± = Ξ²) : (equiv.cast h).symm = equiv.cast h.symm := rfl @[simp] theorem cast_refl {Ξ±} (h : Ξ± = Ξ± := rfl) : equiv.cast h = equiv.refl Ξ± := rfl @[simp] theorem cast_trans {Ξ± Ξ² Ξ³} (h : Ξ± = Ξ²) (h2 : Ξ² = Ξ³) : (equiv.cast h).trans (equiv.cast h2) = equiv.cast (h.trans h2) := ext $ Ξ» x, by { substs h h2, refl } lemma cast_eq_iff_heq {Ξ± Ξ²} (h : Ξ± = Ξ²) {a : Ξ±} {b : Ξ²} : equiv.cast h a = b ↔ a == b := by { subst h, simp } lemma symm_apply_eq {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) {x y} : e.symm x = y ↔ x = e y := ⟨λ H, by simp [H.symm], Ξ» H, by simp [H]⟩ lemma eq_symm_apply {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) {x y} : y = e.symm x ↔ e y = x := (eq_comm.trans e.symm_apply_eq).trans eq_comm @[simp] theorem symm_symm (e : Ξ± ≃ Ξ²) : e.symm.symm = e := by { cases e, refl } @[simp] theorem trans_refl (e : Ξ± ≃ Ξ²) : e.trans (equiv.refl Ξ²) = e := by { cases e, refl } @[simp] theorem refl_symm : (equiv.refl Ξ±).symm = equiv.refl Ξ± := rfl @[simp] theorem refl_trans (e : Ξ± ≃ Ξ²) : (equiv.refl Ξ±).trans e = e := by { cases e, refl } @[simp] theorem symm_trans (e : Ξ± ≃ Ξ²) : e.symm.trans e = equiv.refl Ξ² := ext (by simp) @[simp] theorem trans_symm (e : Ξ± ≃ Ξ²) : e.trans e.symm = equiv.refl Ξ± := ext (by simp) lemma trans_assoc {Ξ΄} (ab : Ξ± ≃ Ξ²) (bc : Ξ² ≃ Ξ³) (cd : Ξ³ ≃ Ξ΄) : (ab.trans bc).trans cd = ab.trans (bc.trans cd) := equiv.ext $ assume a, rfl theorem left_inverse_symm (f : equiv Ξ± Ξ²) : left_inverse f.symm f := f.left_inv theorem right_inverse_symm (f : equiv Ξ± Ξ²) : function.right_inverse f.symm f := f.right_inv @[simp] lemma injective_comp (e : Ξ± ≃ Ξ²) (f : Ξ² β†’ Ξ³) : injective (f ∘ e) ↔ injective f := injective.of_comp_iff' f e.bijective @[simp] lemma comp_injective (f : Ξ± β†’ Ξ²) (e : Ξ² ≃ Ξ³) : injective (e ∘ f) ↔ injective f := e.injective.of_comp_iff f @[simp] lemma surjective_comp (e : Ξ± ≃ Ξ²) (f : Ξ² β†’ Ξ³) : surjective (f ∘ e) ↔ surjective f := e.surjective.of_comp_iff f @[simp] lemma comp_surjective (f : Ξ± β†’ Ξ²) (e : Ξ² ≃ Ξ³) : surjective (e ∘ f) ↔ surjective f := surjective.of_comp_iff' e.bijective f @[simp] lemma bijective_comp (e : Ξ± ≃ Ξ²) (f : Ξ² β†’ Ξ³) : bijective (f ∘ e) ↔ bijective f := e.bijective.of_comp_iff f @[simp] lemma comp_bijective (f : Ξ± β†’ Ξ²) (e : Ξ² ≃ Ξ³) : bijective (e ∘ f) ↔ bijective f := bijective.of_comp_iff' e.bijective f /-- If `Ξ±` is equivalent to `Ξ²` and `Ξ³` is equivalent to `Ξ΄`, then the type of equivalences `Ξ± ≃ Ξ³` is equivalent to the type of equivalences `Ξ² ≃ Ξ΄`. -/ def equiv_congr {Ξ΄} (ab : Ξ± ≃ Ξ²) (cd : Ξ³ ≃ Ξ΄) : (Ξ± ≃ Ξ³) ≃ (Ξ² ≃ Ξ΄) := ⟨ Ξ»ac, (ab.symm.trans ac).trans cd, Ξ»bd, ab.trans $ bd.trans $ cd.symm, assume ac, by { ext x, simp }, assume ac, by { ext x, simp } ⟩ @[simp] lemma equiv_congr_refl {Ξ± Ξ²} : (equiv.refl Ξ±).equiv_congr (equiv.refl Ξ²) = equiv.refl (Ξ± ≃ Ξ²) := by { ext, refl } @[simp] lemma equiv_congr_symm {Ξ΄} (ab : Ξ± ≃ Ξ²) (cd : Ξ³ ≃ Ξ΄) : (ab.equiv_congr cd).symm = ab.symm.equiv_congr cd.symm := by { ext, refl } @[simp] lemma equiv_congr_trans {Ξ΄ Ξ΅ ΞΆ} (ab : Ξ± ≃ Ξ²) (de : Ξ΄ ≃ Ξ΅) (bc : Ξ² ≃ Ξ³) (ef : Ξ΅ ≃ ΞΆ) : (ab.equiv_congr de).trans (bc.equiv_congr ef) = (ab.trans bc).equiv_congr (de.trans ef) := by { ext, refl } @[simp] lemma equiv_congr_refl_left {Ξ± Ξ² Ξ³} (bg : Ξ² ≃ Ξ³) (e : Ξ± ≃ Ξ²) : (equiv.refl Ξ±).equiv_congr bg e = e.trans bg := rfl @[simp] lemma equiv_congr_refl_right {Ξ± Ξ²} (ab e : Ξ± ≃ Ξ²) : ab.equiv_congr (equiv.refl Ξ²) e = ab.symm.trans e := rfl @[simp] lemma equiv_congr_apply_apply {Ξ΄} (ab : Ξ± ≃ Ξ²) (cd : Ξ³ ≃ Ξ΄) (e : Ξ± ≃ Ξ³) (x) : ab.equiv_congr cd e x = cd (e (ab.symm x)) := rfl section perm_congr variables {Ξ±' Ξ²' : Type*} (e : Ξ±' ≃ Ξ²') /-- If `Ξ±` is equivalent to `Ξ²`, then `perm Ξ±` is equivalent to `perm Ξ²`. -/ def perm_congr : perm Ξ±' ≃ perm Ξ²' := equiv_congr e e lemma perm_congr_def (p : equiv.perm Ξ±') : e.perm_congr p = (e.symm.trans p).trans e := rfl @[simp] lemma perm_congr_refl : e.perm_congr (equiv.refl _) = equiv.refl _ := by simp [perm_congr_def] @[simp] lemma perm_congr_symm : e.perm_congr.symm = e.symm.perm_congr := rfl @[simp] lemma perm_congr_apply (p : equiv.perm Ξ±') (x) : e.perm_congr p x = e (p (e.symm x)) := rfl lemma perm_congr_symm_apply (p : equiv.perm Ξ²') (x) : e.perm_congr.symm p x = e.symm (p (e x)) := rfl lemma perm_congr_trans (p p' : equiv.perm Ξ±') : (e.perm_congr p).trans (e.perm_congr p') = e.perm_congr (p.trans p') := by { ext, simp } end perm_congr protected lemma image_eq_preimage {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s : set Ξ±) : e '' s = e.symm ⁻¹' s := set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv lemma _root_.set.mem_image_equiv {Ξ± Ξ²} {S : set Ξ±} {f : Ξ± ≃ Ξ²} {x : Ξ²} : x ∈ f '' S ↔ f.symm x ∈ S := set.ext_iff.mp (f.image_eq_preimage S) x /-- Alias for `equiv.image_eq_preimage` -/ lemma _root_.set.image_equiv_eq_preimage_symm {Ξ± Ξ²} (S : set Ξ±) (f : Ξ± ≃ Ξ²) : f '' S = f.symm ⁻¹' S := f.image_eq_preimage S /-- Alias for `equiv.image_eq_preimage` -/ lemma _root_.set.preimage_equiv_eq_image_symm {Ξ± Ξ²} (S : set Ξ±) (f : Ξ² ≃ Ξ±) : f ⁻¹' S = f.symm '' S := (f.symm.image_eq_preimage S).symm protected lemma subset_image {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s : set Ξ±) (t : set Ξ²) : t βŠ† e '' s ↔ e.symm '' t βŠ† s := by rw [set.image_subset_iff, e.image_eq_preimage] @[simp] lemma symm_image_image {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s : set Ξ±) : e.symm '' (e '' s) = s := e.left_inverse_symm.image_image s lemma eq_image_iff_symm_image_eq {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s : set Ξ±) (t : set Ξ²) : t = e '' s ↔ e.symm '' t = s := (e.symm.injective.image_injective.eq_iff' (e.symm_image_image s)).symm @[simp] lemma image_symm_image {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s : set Ξ²) : e '' (e.symm '' s) = s := e.symm.symm_image_image s @[simp] lemma image_preimage {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s : set Ξ²) : e '' (e ⁻¹' s) = s := e.surjective.image_preimage s @[simp] lemma preimage_image {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s : set Ξ±) : e ⁻¹' (e '' s) = s := e.injective.preimage_image s protected lemma image_compl {Ξ± Ξ²} (f : equiv Ξ± Ξ²) (s : set Ξ±) : f '' sᢜ = (f '' s)ᢜ := set.image_compl_eq f.bijective @[simp] lemma symm_preimage_preimage {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s : set Ξ²) : e.symm ⁻¹' (e ⁻¹' s) = s := e.right_inverse_symm.preimage_preimage s @[simp] lemma preimage_symm_preimage {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s : set Ξ±) : e ⁻¹' (e.symm ⁻¹' s) = s := e.left_inverse_symm.preimage_preimage s @[simp] lemma preimage_subset {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s t : set Ξ²) : e ⁻¹' s βŠ† e ⁻¹' t ↔ s βŠ† t := e.surjective.preimage_subset_preimage_iff @[simp] lemma image_subset {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s t : set Ξ±) : e '' s βŠ† e '' t ↔ s βŠ† t := set.image_subset_image_iff e.injective @[simp] lemma image_eq_iff_eq {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s t : set Ξ±) : e '' s = e '' t ↔ s = t := set.image_eq_image e.injective lemma preimage_eq_iff_eq_image {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s t) : e ⁻¹' s = t ↔ s = e '' t := set.preimage_eq_iff_eq_image e.bijective lemma eq_preimage_iff_image_eq {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) (s t) : s = e ⁻¹' t ↔ e '' s = t := set.eq_preimage_iff_image_eq e.bijective /-- If `Ξ±` is an empty type, then it is equivalent to the `empty` type. -/ def equiv_empty (Ξ± : Sort u) [is_empty Ξ±] : Ξ± ≃ empty := ⟨is_empty_elim, Ξ» e, e.rec _, is_empty_elim, Ξ» e, e.rec _⟩ /-- `Ξ±` is equivalent to an empty type iff `Ξ±` is empty. -/ def equiv_empty_equiv (Ξ± : Sort u) : (Ξ± ≃ empty) ≃ is_empty Ξ± := ⟨λ e, function.is_empty e, @equiv_empty Ξ±, Ξ» e, ext $ Ξ» x, (e x).elim, Ξ» p, rfl⟩ /-- `false` is equivalent to `empty`. -/ def false_equiv_empty : false ≃ empty := equiv_empty _ /-- If `Ξ±` is an empty type, then it is equivalent to the `pempty` type in any universe. -/ def {u' v'} equiv_pempty (Ξ± : Sort v') [is_empty Ξ±] : Ξ± ≃ pempty.{u'} := ⟨is_empty_elim, Ξ» e, e.rec _, is_empty_elim, Ξ» e, e.rec _⟩ /-- `false` is equivalent to `pempty`. -/ def false_equiv_pempty : false ≃ pempty := equiv_pempty _ /-- `empty` is equivalent to `pempty`. -/ def empty_equiv_pempty : empty ≃ pempty := equiv_pempty _ /-- `pempty` types from any two universes are equivalent. -/ def pempty_equiv_pempty : pempty.{v} ≃ pempty.{w} := equiv_pempty _ /-- The `Sort` of proofs of a true proposition is equivalent to `punit`. -/ def prop_equiv_punit {p : Prop} (h : p) : p ≃ punit := ⟨λ x, (), Ξ» x, h, Ξ» _, rfl, Ξ» ⟨⟩, rfl⟩ /-- `true` is equivalent to `punit`. -/ def true_equiv_punit : true ≃ punit := prop_equiv_punit trivial /-- `ulift Ξ±` is equivalent to `Ξ±`. -/ @[simps apply symm_apply {fully_applied := ff}] protected def ulift {Ξ± : Type v} : ulift.{u} Ξ± ≃ Ξ± := ⟨ulift.down, ulift.up, ulift.up_down, Ξ» a, rfl⟩ /-- `plift Ξ±` is equivalent to `Ξ±`. -/ @[simps apply symm_apply {fully_applied := ff}] protected def plift : plift Ξ± ≃ Ξ± := ⟨plift.down, plift.up, plift.up_down, plift.down_up⟩ /-- equivalence of propositions is the same as iff -/ def of_iff {P Q : Prop} (h : P ↔ Q) : P ≃ Q := { to_fun := h.mp, inv_fun := h.mpr, left_inv := Ξ» x, rfl, right_inv := Ξ» y, rfl } /-- If `α₁` is equivalent to `Ξ±β‚‚` and `β₁` is equivalent to `Ξ²β‚‚`, then the type of maps `α₁ β†’ β₁` is equivalent to the type of maps `Ξ±β‚‚ β†’ Ξ²β‚‚`. -/ @[congr, simps apply] def arrow_congr {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Sort*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : β₁ ≃ Ξ²β‚‚) : (α₁ β†’ β₁) ≃ (Ξ±β‚‚ β†’ Ξ²β‚‚) := { to_fun := Ξ» f, eβ‚‚ ∘ f ∘ e₁.symm, inv_fun := Ξ» f, eβ‚‚.symm ∘ f ∘ e₁, left_inv := Ξ» f, funext $ Ξ» x, by simp, right_inv := Ξ» f, funext $ Ξ» x, by simp } lemma arrow_congr_comp {α₁ β₁ γ₁ Ξ±β‚‚ Ξ²β‚‚ Ξ³β‚‚ : Sort*} (ea : α₁ ≃ Ξ±β‚‚) (eb : β₁ ≃ Ξ²β‚‚) (ec : γ₁ ≃ Ξ³β‚‚) (f : α₁ β†’ β₁) (g : β₁ β†’ γ₁) : arrow_congr ea ec (g ∘ f) = (arrow_congr eb ec g) ∘ (arrow_congr ea eb f) := by { ext, simp only [comp, arrow_congr_apply, eb.symm_apply_apply] } @[simp] lemma arrow_congr_refl {Ξ± Ξ² : Sort*} : arrow_congr (equiv.refl Ξ±) (equiv.refl Ξ²) = equiv.refl (Ξ± β†’ Ξ²) := rfl @[simp] lemma arrow_congr_trans {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ α₃ β₃ : Sort*} (e₁ : α₁ ≃ Ξ±β‚‚) (e₁' : β₁ ≃ Ξ²β‚‚) (eβ‚‚ : Ξ±β‚‚ ≃ α₃) (eβ‚‚' : Ξ²β‚‚ ≃ β₃) : arrow_congr (e₁.trans eβ‚‚) (e₁'.trans eβ‚‚') = (arrow_congr e₁ e₁').trans (arrow_congr eβ‚‚ eβ‚‚') := rfl @[simp] lemma arrow_congr_symm {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Sort*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : β₁ ≃ Ξ²β‚‚) : (arrow_congr e₁ eβ‚‚).symm = arrow_congr e₁.symm eβ‚‚.symm := rfl /-- A version of `equiv.arrow_congr` in `Type`, rather than `Sort`. The `equiv_rw` tactic is not able to use the default `Sort` level `equiv.arrow_congr`, because Lean's universe rules will not unify `?l_1` with `imax (1 ?m_1)`. -/ @[congr, simps apply] def arrow_congr' {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Type*} (hΞ± : α₁ ≃ Ξ±β‚‚) (hΞ² : β₁ ≃ Ξ²β‚‚) : (α₁ β†’ β₁) ≃ (Ξ±β‚‚ β†’ Ξ²β‚‚) := equiv.arrow_congr hΞ± hΞ² @[simp] lemma arrow_congr'_refl {Ξ± Ξ² : Type*} : arrow_congr' (equiv.refl Ξ±) (equiv.refl Ξ²) = equiv.refl (Ξ± β†’ Ξ²) := rfl @[simp] lemma arrow_congr'_trans {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ α₃ β₃ : Type*} (e₁ : α₁ ≃ Ξ±β‚‚) (e₁' : β₁ ≃ Ξ²β‚‚) (eβ‚‚ : Ξ±β‚‚ ≃ α₃) (eβ‚‚' : Ξ²β‚‚ ≃ β₃) : arrow_congr' (e₁.trans eβ‚‚) (e₁'.trans eβ‚‚') = (arrow_congr' e₁ e₁').trans (arrow_congr' eβ‚‚ eβ‚‚') := rfl @[simp] lemma arrow_congr'_symm {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Type*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : β₁ ≃ Ξ²β‚‚) : (arrow_congr' e₁ eβ‚‚).symm = arrow_congr' e₁.symm eβ‚‚.symm := rfl /-- Conjugate a map `f : Ξ± β†’ Ξ±` by an equivalence `Ξ± ≃ Ξ²`. -/ @[simps apply] def conj (e : Ξ± ≃ Ξ²) : (Ξ± β†’ Ξ±) ≃ (Ξ² β†’ Ξ²) := arrow_congr e e @[simp] lemma conj_refl : conj (equiv.refl Ξ±) = equiv.refl (Ξ± β†’ Ξ±) := rfl @[simp] lemma conj_symm (e : Ξ± ≃ Ξ²) : e.conj.symm = e.symm.conj := rfl @[simp] lemma conj_trans (e₁ : Ξ± ≃ Ξ²) (eβ‚‚ : Ξ² ≃ Ξ³) : (e₁.trans eβ‚‚).conj = e₁.conj.trans eβ‚‚.conj := rfl -- This should not be a simp lemma as long as `(∘)` is reducible: -- when `(∘)` is reducible, Lean can unify `f₁ ∘ fβ‚‚` with any `g` using -- `f₁ := g` and `fβ‚‚ := Ξ» x, x`. This causes nontermination. lemma conj_comp (e : Ξ± ≃ Ξ²) (f₁ fβ‚‚ : Ξ± β†’ Ξ±) : e.conj (f₁ ∘ fβ‚‚) = (e.conj f₁) ∘ (e.conj fβ‚‚) := by apply arrow_congr_comp section binary_op variables {α₁ β₁ : Type*} (e : α₁ ≃ β₁) (f : α₁ β†’ α₁ β†’ α₁) lemma semiconj_conj (f : α₁ β†’ α₁) : semiconj e f (e.conj f) := Ξ» x, by simp lemma semiconjβ‚‚_conj : semiconjβ‚‚ e f (e.arrow_congr e.conj f) := Ξ» x y, by simp instance [is_associative α₁ f] : is_associative β₁ (e.arrow_congr (e.arrow_congr e) f) := (e.semiconjβ‚‚_conj f).is_associative_right e.surjective instance [is_idempotent α₁ f] : is_idempotent β₁ (e.arrow_congr (e.arrow_congr e) f) := (e.semiconjβ‚‚_conj f).is_idempotent_right e.surjective instance [is_left_cancel α₁ f] : is_left_cancel β₁ (e.arrow_congr (e.arrow_congr e) f) := ⟨e.surjective.forall₃.2 $ Ξ» x y z, by simpa using @is_left_cancel.left_cancel _ f _ x y z⟩ instance [is_right_cancel α₁ f] : is_right_cancel β₁ (e.arrow_congr (e.arrow_congr e) f) := ⟨e.surjective.forall₃.2 $ Ξ» x y z, by simpa using @is_right_cancel.right_cancel _ f _ x y z⟩ end binary_op /-- `punit` sorts in any two universes are equivalent. -/ def punit_equiv_punit : punit.{v} ≃ punit.{w} := ⟨λ _, punit.star, Ξ» _, punit.star, Ξ» u, by { cases u, refl }, Ξ» u, by { cases u, reflexivity }⟩ section /-- The sort of maps to `punit.{v}` is equivalent to `punit.{w}`. -/ def arrow_punit_equiv_punit (Ξ± : Sort*) : (Ξ± β†’ punit.{v}) ≃ punit.{w} := ⟨λ f, punit.star, Ξ» u f, punit.star, Ξ» f, by { funext x, cases f x, refl }, Ξ» u, by { cases u, reflexivity }⟩ /-- If `Ξ±` has a unique term, then the type of function `Ξ± β†’ Ξ²` is equivalent to `Ξ²`. -/ @[simps] def fun_unique (Ξ± Ξ²) [unique Ξ±] : (Ξ± β†’ Ξ²) ≃ Ξ² := { to_fun := Ξ» f, f (default Ξ±), inv_fun := Ξ» b a, b, left_inv := Ξ» f, funext $ Ξ» a, congr_arg f $ subsingleton.elim _ _, right_inv := Ξ» b, rfl } /-- The sort of maps from `punit` is equivalent to the codomain. -/ def punit_arrow_equiv (Ξ± : Sort*) : (punit.{u} β†’ Ξ±) ≃ Ξ± := fun_unique _ _ /-- The sort of maps from `true` is equivalent to the codomain. -/ def true_arrow_equiv (Ξ± : Sort*) : (true β†’ Ξ±) ≃ Ξ± := fun_unique _ _ /-- The sort of maps from a type that `is_empty` is equivalent to `punit`. -/ def arrow_punit_of_is_empty (Ξ± Ξ² : Sort*) [is_empty Ξ±] : (Ξ± β†’ Ξ²) ≃ punit.{u} := ⟨λ f, punit.star, Ξ» u, is_empty_elim, Ξ» f, funext is_empty_elim, Ξ» u, by { cases u, refl }⟩ /-- The sort of maps from `empty` is equivalent to `punit`. -/ def empty_arrow_equiv_punit (Ξ± : Sort*) : (empty β†’ Ξ±) ≃ punit.{u} := arrow_punit_of_is_empty _ _ /-- The sort of maps from `pempty` is equivalent to `punit`. -/ def pempty_arrow_equiv_punit (Ξ± : Sort*) : (pempty β†’ Ξ±) ≃ punit.{u} := arrow_punit_of_is_empty _ _ /-- The sort of maps from `false` is equivalent to `punit`. -/ def false_arrow_equiv_punit (Ξ± : Sort*) : (false β†’ Ξ±) ≃ punit.{u} := arrow_punit_of_is_empty _ _ end /-- Product of two equivalences. If `α₁ ≃ Ξ±β‚‚` and `β₁ ≃ Ξ²β‚‚`, then `α₁ Γ— β₁ ≃ Ξ±β‚‚ Γ— Ξ²β‚‚`. -/ @[congr, simps apply] def prod_congr {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Type*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : β₁ ≃ Ξ²β‚‚) : α₁ Γ— β₁ ≃ Ξ±β‚‚ Γ— Ξ²β‚‚ := ⟨prod.map e₁ eβ‚‚, prod.map e₁.symm eβ‚‚.symm, Ξ» ⟨a, b⟩, by simp, Ξ» ⟨a, b⟩, by simp⟩ @[simp] theorem prod_congr_symm {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Type*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : β₁ ≃ Ξ²β‚‚) : (prod_congr e₁ eβ‚‚).symm = prod_congr e₁.symm eβ‚‚.symm := rfl /-- Type product is commutative up to an equivalence: `Ξ± Γ— Ξ² ≃ Ξ² Γ— Ξ±`. -/ @[simps apply] def prod_comm (Ξ± Ξ² : Type*) : Ξ± Γ— Ξ² ≃ Ξ² Γ— Ξ± := ⟨prod.swap, prod.swap, λ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩ @[simp] lemma prod_comm_symm (Ξ± Ξ²) : (prod_comm Ξ± Ξ²).symm = prod_comm Ξ² Ξ± := rfl /-- Type product is associative up to an equivalence. -/ @[simps] def prod_assoc (Ξ± Ξ² Ξ³ : Sort*) : (Ξ± Γ— Ξ²) Γ— Ξ³ ≃ Ξ± Γ— (Ξ² Γ— Ξ³) := ⟨λ p, (p.1.1, p.1.2, p.2), Ξ»p, ((p.1, p.2.1), p.2.2), Ξ» ⟨⟨a, b⟩, c⟩, rfl, Ξ» ⟨a, ⟨b, c⟩⟩, rfl⟩ lemma prod_assoc_preimage {Ξ± Ξ² Ξ³} {s : set Ξ±} {t : set Ξ²} {u : set Ξ³} : equiv.prod_assoc Ξ± Ξ² Ξ³ ⁻¹' s.prod (t.prod u) = (s.prod t).prod u := by { ext, simp [and_assoc] } /-- Functions on `Ξ± Γ— Ξ²` are equivalent to functions `Ξ± β†’ Ξ² β†’ Ξ³`. -/ @[simps {fully_applied := ff}] def curry (Ξ± Ξ² Ξ³ : Type*) : (Ξ± Γ— Ξ² β†’ Ξ³) ≃ (Ξ± β†’ Ξ² β†’ Ξ³) := { to_fun := curry, inv_fun := uncurry, left_inv := uncurry_curry, right_inv := curry_uncurry } section /-- `punit` is a right identity for type product up to an equivalence. -/ @[simps] def prod_punit (Ξ± : Type*) : Ξ± Γ— punit.{u+1} ≃ Ξ± := ⟨λ p, p.1, Ξ» a, (a, punit.star), Ξ» ⟨_, punit.star⟩, rfl, Ξ» a, rfl⟩ /-- `punit` is a left identity for type product up to an equivalence. -/ @[simps] def punit_prod (Ξ± : Type*) : punit.{u+1} Γ— Ξ± ≃ Ξ± := calc punit Γ— Ξ± ≃ Ξ± Γ— punit : prod_comm _ _ ... ≃ Ξ± : prod_punit _ /-- `empty` type is a right absorbing element for type product up to an equivalence. -/ def prod_empty (Ξ± : Type*) : Ξ± Γ— empty ≃ empty := equiv_empty _ /-- `empty` type is a left absorbing element for type product up to an equivalence. -/ def empty_prod (Ξ± : Type*) : empty Γ— Ξ± ≃ empty := equiv_empty _ /-- `pempty` type is a right absorbing element for type product up to an equivalence. -/ def prod_pempty (Ξ± : Type*) : Ξ± Γ— pempty ≃ pempty := equiv_pempty _ /-- `pempty` type is a left absorbing element for type product up to an equivalence. -/ def pempty_prod (Ξ± : Type*) : pempty Γ— Ξ± ≃ pempty := equiv_pempty _ end section open sum /-- `psum` is equivalent to `sum`. -/ def psum_equiv_sum (Ξ± Ξ² : Type*) : psum Ξ± Ξ² ≃ Ξ± βŠ• Ξ² := ⟨λ s, psum.cases_on s inl inr, Ξ» s, sum.cases_on s psum.inl psum.inr, Ξ» s, by cases s; refl, Ξ» s, by cases s; refl⟩ /-- If `Ξ± ≃ Ξ±'` and `Ξ² ≃ Ξ²'`, then `Ξ± βŠ• Ξ² ≃ Ξ±' βŠ• Ξ²'`. -/ @[simps apply] def sum_congr {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Type*} (ea : α₁ ≃ Ξ±β‚‚) (eb : β₁ ≃ Ξ²β‚‚) : α₁ βŠ• β₁ ≃ Ξ±β‚‚ βŠ• Ξ²β‚‚ := ⟨sum.map ea eb, sum.map ea.symm eb.symm, Ξ» x, by simp, Ξ» x, by simp⟩ @[simp] lemma sum_congr_trans {α₁ Ξ±β‚‚ β₁ Ξ²β‚‚ γ₁ Ξ³β‚‚ : Sort*} (e : α₁ ≃ β₁) (f : Ξ±β‚‚ ≃ Ξ²β‚‚) (g : β₁ ≃ γ₁) (h : Ξ²β‚‚ ≃ Ξ³β‚‚) : (equiv.sum_congr e f).trans (equiv.sum_congr g h) = (equiv.sum_congr (e.trans g) (f.trans h)) := by { ext i, cases i; refl } @[simp] lemma sum_congr_symm {Ξ± Ξ² Ξ³ Ξ΄ : Sort*} (e : Ξ± ≃ Ξ²) (f : Ξ³ ≃ Ξ΄) : (equiv.sum_congr e f).symm = (equiv.sum_congr (e.symm) (f.symm)) := rfl @[simp] lemma sum_congr_refl {Ξ± Ξ² : Sort*} : equiv.sum_congr (equiv.refl Ξ±) (equiv.refl Ξ²) = equiv.refl (Ξ± βŠ• Ξ²) := by { ext i, cases i; refl } namespace perm /-- Combine a permutation of `Ξ±` and of `Ξ²` into a permutation of `Ξ± βŠ• Ξ²`. -/ @[reducible] def sum_congr {Ξ± Ξ² : Type*} (ea : equiv.perm Ξ±) (eb : equiv.perm Ξ²) : equiv.perm (Ξ± βŠ• Ξ²) := equiv.sum_congr ea eb @[simp] lemma sum_congr_apply {Ξ± Ξ² : Type*} (ea : equiv.perm Ξ±) (eb : equiv.perm Ξ²) (x : Ξ± βŠ• Ξ²) : sum_congr ea eb x = sum.map ⇑ea ⇑eb x := equiv.sum_congr_apply ea eb x @[simp] lemma sum_congr_trans {Ξ± Ξ² : Sort*} (e : equiv.perm Ξ±) (f : equiv.perm Ξ²) (g : equiv.perm Ξ±) (h : equiv.perm Ξ²) : (sum_congr e f).trans (sum_congr g h) = sum_congr (e.trans g) (f.trans h) := equiv.sum_congr_trans e f g h @[simp] lemma sum_congr_symm {Ξ± Ξ² : Sort*} (e : equiv.perm Ξ±) (f : equiv.perm Ξ²) : (sum_congr e f).symm = sum_congr (e.symm) (f.symm) := equiv.sum_congr_symm e f @[simp] lemma sum_congr_refl {Ξ± Ξ² : Sort*} : sum_congr (equiv.refl Ξ±) (equiv.refl Ξ²) = equiv.refl (Ξ± βŠ• Ξ²) := equiv.sum_congr_refl end perm /-- `bool` is equivalent the sum of two `punit`s. -/ def bool_equiv_punit_sum_punit : bool ≃ punit.{u+1} βŠ• punit.{v+1} := ⟨λ b, cond b (inr punit.star) (inl punit.star), Ξ» s, sum.rec_on s (Ξ»_, ff) (Ξ»_, tt), Ξ» b, by cases b; refl, Ξ» s, by rcases s with ⟨⟨⟩⟩ | ⟨⟨⟩⟩; refl⟩ /-- `Prop` is noncomputably equivalent to `bool`. -/ noncomputable def Prop_equiv_bool : Prop ≃ bool := ⟨λ p, @to_bool p (classical.prop_decidable _), Ξ» b, b, Ξ» p, by simp, Ξ» b, by simp⟩ /-- Sum of types is commutative up to an equivalence. -/ @[simps apply] def sum_comm (Ξ± Ξ² : Sort*) : Ξ± βŠ• Ξ² ≃ Ξ² βŠ• Ξ± := ⟨sum.swap, sum.swap, sum.swap_swap, sum.swap_swap⟩ @[simp] lemma sum_comm_symm (Ξ± Ξ²) : (sum_comm Ξ± Ξ²).symm = sum_comm Ξ² Ξ± := rfl /-- Sum of types is associative up to an equivalence. -/ def sum_assoc (Ξ± Ξ² Ξ³ : Sort*) : (Ξ± βŠ• Ξ²) βŠ• Ξ³ ≃ Ξ± βŠ• (Ξ² βŠ• Ξ³) := ⟨sum.elim (sum.elim sum.inl (sum.inr ∘ sum.inl)) (sum.inr ∘ sum.inr), sum.elim (sum.inl ∘ sum.inl) $ sum.elim (sum.inl ∘ sum.inr) sum.inr, by rintros (⟨_ | _⟩ | _); refl, by rintros (_ | ⟨_ | _⟩); refl⟩ @[simp] theorem sum_assoc_apply_in1 {Ξ± Ξ² Ξ³} (a) : sum_assoc Ξ± Ξ² Ξ³ (inl (inl a)) = inl a := rfl @[simp] theorem sum_assoc_apply_in2 {Ξ± Ξ² Ξ³} (b) : sum_assoc Ξ± Ξ² Ξ³ (inl (inr b)) = inr (inl b) := rfl @[simp] theorem sum_assoc_apply_in3 {Ξ± Ξ² Ξ³} (c) : sum_assoc Ξ± Ξ² Ξ³ (inr c) = inr (inr c) := rfl /-- Sum with `empty` is equivalent to the original type. -/ @[simps symm_apply] def sum_empty (Ξ± Ξ² : Type*) [is_empty Ξ²] : Ξ± βŠ• Ξ² ≃ Ξ± := ⟨sum.elim id is_empty_elim, inl, Ξ» s, by { rcases s with _ | x, refl, exact is_empty_elim x }, Ξ» a, rfl⟩ @[simp] lemma sum_empty_apply_inl {Ξ± Ξ² : Type*} [is_empty Ξ²] (a : Ξ±) : sum_empty Ξ± Ξ² (sum.inl a) = a := rfl /-- The sum of `empty` with any `Sort*` is equivalent to the right summand. -/ @[simps symm_apply] def empty_sum (Ξ± Ξ² : Type*) [is_empty Ξ±] : Ξ± βŠ• Ξ² ≃ Ξ² := (sum_comm _ _).trans $ sum_empty _ _ @[simp] lemma empty_sum_apply_inr {Ξ± Ξ² : Type*} [is_empty Ξ±] (b : Ξ²) : empty_sum Ξ± Ξ² (sum.inr b) = b := rfl /-- `option Ξ±` is equivalent to `Ξ± βŠ• punit` -/ def option_equiv_sum_punit (Ξ± : Type*) : option Ξ± ≃ Ξ± βŠ• punit.{u+1} := ⟨λ o, match o with none := inr punit.star | some a := inl a end, Ξ» s, match s with inr _ := none | inl a := some a end, Ξ» o, by cases o; refl, Ξ» s, by rcases s with _ | ⟨⟨⟩⟩; refl⟩ @[simp] lemma option_equiv_sum_punit_none {Ξ±} : option_equiv_sum_punit Ξ± none = sum.inr punit.star := rfl @[simp] lemma option_equiv_sum_punit_some {Ξ±} (a) : option_equiv_sum_punit Ξ± (some a) = sum.inl a := rfl @[simp] lemma option_equiv_sum_punit_coe {Ξ±} (a : Ξ±) : option_equiv_sum_punit Ξ± a = sum.inl a := rfl @[simp] lemma option_equiv_sum_punit_symm_inl {Ξ±} (a) : (option_equiv_sum_punit Ξ±).symm (sum.inl a) = a := rfl @[simp] lemma option_equiv_sum_punit_symm_inr {Ξ±} (a) : (option_equiv_sum_punit Ξ±).symm (sum.inr a) = none := rfl /-- The set of `x : option Ξ±` such that `is_some x` is equivalent to `Ξ±`. -/ def option_is_some_equiv (Ξ± : Type*) : {x : option Ξ± // x.is_some} ≃ Ξ± := { to_fun := Ξ» o, option.get o.2, inv_fun := Ξ» x, ⟨some x, dec_trivial⟩, left_inv := Ξ» o, subtype.eq $ option.some_get _, right_inv := Ξ» x, option.get_some _ _ } /-- `Ξ± βŠ• Ξ²` is equivalent to a `sigma`-type over `bool`. Note that this definition assumes `Ξ±` and `Ξ²` to be types from the same universe, so it cannot by used directly to transfer theorems about sigma types to theorems about sum types. In many cases one can use `ulift` to work around this difficulty. -/ def sum_equiv_sigma_bool (Ξ± Ξ² : Type u) : Ξ± βŠ• Ξ² ≃ (Ξ£ b: bool, cond b Ξ± Ξ²) := ⟨λ s, s.elim (Ξ» x, ⟨tt, x⟩) (Ξ» x, ⟨ff, x⟩), Ξ» s, match s with ⟨tt, a⟩ := inl a | ⟨ff, b⟩ := inr b end, Ξ» s, by cases s; refl, Ξ» s, by rcases s with ⟨_|_, _⟩; refl⟩ /-- `sigma_preimage_equiv f` for `f : Ξ± β†’ Ξ²` is the natural equivalence between the type of all fibres of `f` and the total space `Ξ±`. -/ @[simps] def sigma_preimage_equiv {Ξ± Ξ² : Type*} (f : Ξ± β†’ Ξ²) : (Ξ£ y : Ξ², {x // f x = y}) ≃ Ξ± := ⟨λ x, ↑x.2, Ξ» x, ⟨f x, x, rfl⟩, Ξ» ⟨y, x, rfl⟩, rfl, Ξ» x, rfl⟩ /-- A set `s` in `Ξ± Γ— Ξ²` is equivalent to the sigma-type `Ξ£ x, {y | (x, y) ∈ s}`. -/ def set_prod_equiv_sigma {Ξ± Ξ² : Type*} (s : set (Ξ± Γ— Ξ²)) : s ≃ Ξ£ x : Ξ±, {y | (x, y) ∈ s} := { to_fun := Ξ» x, ⟨x.1.1, x.1.2, by simp⟩, inv_fun := Ξ» x, ⟨(x.1, x.2.1), x.2.2⟩, left_inv := Ξ» ⟨⟨x, y⟩, h⟩, rfl, right_inv := Ξ» ⟨x, y, h⟩, rfl } end section sum_compl /-- For any predicate `p` on `Ξ±`, the sum of the two subtypes `{a // p a}` and its complement `{a // Β¬ p a}` is naturally equivalent to `Ξ±`. See `subtype_or_equiv` for sum types over subtypes `{x // p x}` and `{x // q x}` that are not necessarily `is_compl p q`. -/ def sum_compl {Ξ± : Type*} (p : Ξ± β†’ Prop) [decidable_pred p] : {a // p a} βŠ• {a // Β¬ p a} ≃ Ξ± := { to_fun := sum.elim coe coe, inv_fun := Ξ» a, if h : p a then sum.inl ⟨a, h⟩ else sum.inr ⟨a, h⟩, left_inv := by { rintros (⟨x,hx⟩|⟨x,hx⟩); dsimp; [rw dif_pos, rw dif_neg], }, right_inv := Ξ» a, by { dsimp, split_ifs; refl } } @[simp] lemma sum_compl_apply_inl {Ξ± : Type*} (p : Ξ± β†’ Prop) [decidable_pred p] (x : {a // p a}) : sum_compl p (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {Ξ± : Type*} (p : Ξ± β†’ Prop) [decidable_pred p] (x : {a // Β¬ p a}) : sum_compl p (sum.inr x) = x := rfl @[simp] lemma sum_compl_apply_symm_of_pos {Ξ± : Type*} (p : Ξ± β†’ Prop) [decidable_pred p] (a : Ξ±) (h : p a) : (sum_compl p).symm a = sum.inl ⟨a, h⟩ := dif_pos h @[simp] lemma sum_compl_apply_symm_of_neg {Ξ± : Type*} (p : Ξ± β†’ Prop) [decidable_pred p] (a : Ξ±) (h : Β¬ p a) : (sum_compl p).symm a = sum.inr ⟨a, h⟩ := dif_neg h /-- Combines an `equiv` between two subtypes with an `equiv` between their complements to form a permutation. -/ def subtype_congr {Ξ± : Type*} {p q : Ξ± β†’ Prop} [decidable_pred p] [decidable_pred q] (e : {x // p x} ≃ {x // q x}) (f : {x // Β¬p x} ≃ {x // Β¬q x}) : perm Ξ± := (sum_compl p).symm.trans ((sum_congr e f).trans (sum_compl q)) open equiv variables {Ξ΅ : Type*} {p : Ξ΅ β†’ Prop} [decidable_pred p] variables (ep ep' : perm {a // p a}) (en en' : perm {a // Β¬ p a}) /-- Combining permutations on `Ξ΅` that permute only inside or outside the subtype split induced by `p : Ξ΅ β†’ Prop` constructs a permutation on `Ξ΅`. -/ def perm.subtype_congr : equiv.perm Ξ΅ := perm_congr (sum_compl p) (sum_congr ep en) lemma perm.subtype_congr.apply (a : Ξ΅) : ep.subtype_congr en a = if h : p a then ep ⟨a, h⟩ else en ⟨a, h⟩ := by { by_cases h : p a; simp [perm.subtype_congr, h] } @[simp] lemma perm.subtype_congr.left_apply {a : Ξ΅} (h : p a) : ep.subtype_congr en a = ep ⟨a, h⟩ := by simp [perm.subtype_congr.apply, h] @[simp] lemma perm.subtype_congr.left_apply_subtype (a : {a // p a}) : ep.subtype_congr en a = ep a := by { convert perm.subtype_congr.left_apply _ _ a.property, simp } @[simp] lemma perm.subtype_congr.right_apply {a : Ξ΅} (h : Β¬ p a) : ep.subtype_congr en a = en ⟨a, h⟩ := by simp [perm.subtype_congr.apply, h] @[simp] lemma perm.subtype_congr.right_apply_subtype (a : {a // Β¬ p a}) : ep.subtype_congr en a = en a := by { convert perm.subtype_congr.right_apply _ _ a.property, simp } @[simp] lemma perm.subtype_congr.refl : perm.subtype_congr (equiv.refl {a // p a}) (equiv.refl {a // Β¬ p a}) = equiv.refl Ξ΅ := by { ext x, by_cases h : p x; simp [h] } @[simp] lemma perm.subtype_congr.symm : (ep.subtype_congr en).symm = perm.subtype_congr ep.symm en.symm := begin ext x, by_cases h : p x, { have : p (ep.symm ⟨x, h⟩) := subtype.property _, simp [perm.subtype_congr.apply, h, symm_apply_eq, this] }, { have : Β¬ p (en.symm ⟨x, h⟩) := subtype.property (en.symm _), simp [perm.subtype_congr.apply, h, symm_apply_eq, this] } end @[simp] lemma perm.subtype_congr.trans : (ep.subtype_congr en).trans (ep'.subtype_congr en') = perm.subtype_congr (ep.trans ep') (en.trans en') := begin ext x, by_cases h : p x, { have : p (ep ⟨x, h⟩) := subtype.property _, simp [perm.subtype_congr.apply, h, this] }, { have : Β¬ p (en ⟨x, h⟩) := subtype.property (en _), simp [perm.subtype_congr.apply, h, symm_apply_eq, this] } end end sum_compl section subtype_preimage variables (p : Ξ± β†’ Prop) [decidable_pred p] (xβ‚€ : {a // p a} β†’ Ξ²) /-- For a fixed function `xβ‚€ : {a // p a} β†’ Ξ²` defined on a subtype of `Ξ±`, the subtype of functions `x : Ξ± β†’ Ξ²` that agree with `xβ‚€` on the subtype `{a // p a}` is naturally equivalent to the type of functions `{a // Β¬ p a} β†’ Ξ²`. -/ @[simps] def subtype_preimage : {x : Ξ± β†’ Ξ² // x ∘ coe = xβ‚€} ≃ ({a // Β¬ p a} β†’ Ξ²) := { to_fun := Ξ» (x : {x : Ξ± β†’ Ξ² // x ∘ coe = xβ‚€}) a, (x : Ξ± β†’ Ξ²) a, inv_fun := Ξ» x, ⟨λ a, if h : p a then xβ‚€ ⟨a, h⟩ else x ⟨a, h⟩, funext $ Ξ» ⟨a, h⟩, dif_pos h⟩, left_inv := Ξ» ⟨x, hx⟩, subtype.val_injective $ funext $ Ξ» a, (by { dsimp, split_ifs; [ rw ← hx, skip ]; refl }), right_inv := Ξ» x, funext $ Ξ» ⟨a, h⟩, show dite (p a) _ _ = _, by { dsimp, rw [dif_neg h] } } lemma subtype_preimage_symm_apply_coe_pos (x : {a // Β¬ p a} β†’ Ξ²) (a : Ξ±) (h : p a) : ((subtype_preimage p xβ‚€).symm x : Ξ± β†’ Ξ²) a = xβ‚€ ⟨a, h⟩ := dif_pos h lemma subtype_preimage_symm_apply_coe_neg (x : {a // Β¬ p a} β†’ Ξ²) (a : Ξ±) (h : Β¬ p a) : ((subtype_preimage p xβ‚€).symm x : Ξ± β†’ Ξ²) a = x ⟨a, h⟩ := dif_neg h end subtype_preimage section /-- A family of equivalences `Ξ  a, β₁ a ≃ Ξ²β‚‚ a` generates an equivalence between `Ξ  a, β₁ a` and `Ξ  a, Ξ²β‚‚ a`. -/ def Pi_congr_right {Ξ±} {β₁ Ξ²β‚‚ : Ξ± β†’ Sort*} (F : Ξ  a, β₁ a ≃ Ξ²β‚‚ a) : (Ξ  a, β₁ a) ≃ (Ξ  a, Ξ²β‚‚ a) := ⟨λ H a, F a (H a), Ξ» H a, (F a).symm (H a), Ξ» H, funext $ by simp, Ξ» H, funext $ by simp⟩ /-- Dependent `curry` equivalence: the type of dependent functions on `Ξ£ i, Ξ² i` is equivalent to the type of dependent functions of two arguments (i.e., functions to the space of functions). This is `sigma.curry` and `sigma.uncurry` together as an equiv. -/ def Pi_curry {Ξ±} {Ξ² : Ξ± β†’ Sort*} (Ξ³ : Ξ  a, Ξ² a β†’ Sort*) : (Ξ  x : Ξ£ i, Ξ² i, Ξ³ x.1 x.2) ≃ (Ξ  a b, Ξ³ a b) := { to_fun := sigma.curry, inv_fun := sigma.uncurry, left_inv := sigma.uncurry_curry, right_inv := sigma.curry_uncurry } end section /-- A `psigma`-type is equivalent to the corresponding `sigma`-type. -/ @[simps apply symm_apply] def psigma_equiv_sigma {Ξ±} (Ξ² : Ξ± β†’ Sort*) : (Ξ£' i, Ξ² i) ≃ Ξ£ i, Ξ² i := ⟨λ a, ⟨a.1, a.2⟩, Ξ» a, ⟨a.1, a.2⟩, Ξ» ⟨a, b⟩, rfl, Ξ» ⟨a, b⟩, rfl⟩ /-- A family of equivalences `Ξ  a, β₁ a ≃ Ξ²β‚‚ a` generates an equivalence between `Ξ£ a, β₁ a` and `Ξ£ a, Ξ²β‚‚ a`. -/ @[simps apply] def sigma_congr_right {Ξ±} {β₁ Ξ²β‚‚ : Ξ± β†’ Sort*} (F : Ξ  a, β₁ a ≃ Ξ²β‚‚ a) : (Ξ£ a, β₁ a) ≃ Ξ£ a, Ξ²β‚‚ a := ⟨λ a, ⟨a.1, F a.1 a.2⟩, Ξ» a, ⟨a.1, (F a.1).symm a.2⟩, Ξ» ⟨a, b⟩, congr_arg (sigma.mk a) $ symm_apply_apply (F a) b, Ξ» ⟨a, b⟩, congr_arg (sigma.mk a) $ apply_symm_apply (F a) b⟩ @[simp] lemma sigma_congr_right_trans {Ξ±} {β₁ Ξ²β‚‚ β₃ : Ξ± β†’ Sort*} (F : Ξ  a, β₁ a ≃ Ξ²β‚‚ a) (G : Ξ  a, Ξ²β‚‚ a ≃ β₃ a) : (sigma_congr_right F).trans (sigma_congr_right G) = sigma_congr_right (Ξ» a, (F a).trans (G a)) := by { ext1 x, cases x, refl } @[simp] lemma sigma_congr_right_symm {Ξ±} {β₁ Ξ²β‚‚ : Ξ± β†’ Sort*} (F : Ξ  a, β₁ a ≃ Ξ²β‚‚ a) : (sigma_congr_right F).symm = sigma_congr_right (Ξ» a, (F a).symm) := by { ext1 x, cases x, refl } @[simp] lemma sigma_congr_right_refl {Ξ±} {Ξ² : Ξ± β†’ Sort*} : (sigma_congr_right (Ξ» a, equiv.refl (Ξ² a))) = equiv.refl (Ξ£ a, Ξ² a) := by { ext1 x, cases x, refl } namespace perm /-- A family of permutations `Ξ  a, perm (Ξ² a)` generates a permuation `perm (Ξ£ a, β₁ a)`. -/ @[reducible] def sigma_congr_right {Ξ±} {Ξ² : Ξ± β†’ Sort*} (F : Ξ  a, perm (Ξ² a)) : perm (Ξ£ a, Ξ² a) := equiv.sigma_congr_right F @[simp] lemma sigma_congr_right_trans {Ξ±} {Ξ² : Ξ± β†’ Sort*} (F : Ξ  a, perm (Ξ² a)) (G : Ξ  a, perm (Ξ² a)) : (sigma_congr_right F).trans (sigma_congr_right G) = sigma_congr_right (Ξ» a, (F a).trans (G a)) := equiv.sigma_congr_right_trans F G @[simp] lemma sigma_congr_right_symm {Ξ±} {Ξ² : Ξ± β†’ Sort*} (F : Ξ  a, perm (Ξ² a)) : (sigma_congr_right F).symm = sigma_congr_right (Ξ» a, (F a).symm) := equiv.sigma_congr_right_symm F @[simp] lemma sigma_congr_right_refl {Ξ±} {Ξ² : Ξ± β†’ Sort*} : (sigma_congr_right (Ξ» a, equiv.refl (Ξ² a))) = equiv.refl (Ξ£ a, Ξ² a) := equiv.sigma_congr_right_refl end perm /-- An equivalence `f : α₁ ≃ Ξ±β‚‚` generates an equivalence between `Ξ£ a, Ξ² (f a)` and `Ξ£ a, Ξ² a`. -/ @[simps apply] def sigma_congr_left {α₁ Ξ±β‚‚} {Ξ² : Ξ±β‚‚ β†’ Sort*} (e : α₁ ≃ Ξ±β‚‚) : (Ξ£ a:α₁, Ξ² (e a)) ≃ (Ξ£ a:Ξ±β‚‚, Ξ² a) := ⟨λ a, ⟨e a.1, a.2⟩, Ξ» a, ⟨e.symm a.1, @@eq.rec Ξ² a.2 (e.right_inv a.1).symm⟩, Ξ» ⟨a, b⟩, match e.symm (e a), e.left_inv a : βˆ€ a' (h : a' = a), @sigma.mk _ (Ξ² ∘ e) _ (@@eq.rec Ξ² b (congr_arg e h.symm)) = ⟨a, b⟩ with | _, rfl := rfl end, Ξ» ⟨a, b⟩, match e (e.symm a), _ : βˆ€ a' (h : a' = a), sigma.mk a' (@@eq.rec Ξ² b h.symm) = ⟨a, b⟩ with | _, rfl := rfl end⟩ /-- Transporting a sigma type through an equivalence of the base -/ def sigma_congr_left' {α₁ Ξ±β‚‚} {Ξ² : α₁ β†’ Sort*} (f : α₁ ≃ Ξ±β‚‚) : (Ξ£ a:α₁, Ξ² a) ≃ (Ξ£ a:Ξ±β‚‚, Ξ² (f.symm a)) := (sigma_congr_left f.symm).symm /-- Transporting a sigma type through an equivalence of the base and a family of equivalences of matching fibers -/ def sigma_congr {α₁ Ξ±β‚‚} {β₁ : α₁ β†’ Sort*} {Ξ²β‚‚ : Ξ±β‚‚ β†’ Sort*} (f : α₁ ≃ Ξ±β‚‚) (F : βˆ€ a, β₁ a ≃ Ξ²β‚‚ (f a)) : sigma β₁ ≃ sigma Ξ²β‚‚ := (sigma_congr_right F).trans (sigma_congr_left f) /-- `sigma` type with a constant fiber is equivalent to the product. -/ @[simps apply symm_apply] def sigma_equiv_prod (Ξ± Ξ² : Type*) : (Ξ£_:Ξ±, Ξ²) ≃ Ξ± Γ— Ξ² := ⟨λ a, ⟨a.1, a.2⟩, Ξ» a, ⟨a.1, a.2⟩, Ξ» ⟨a, b⟩, rfl, Ξ» ⟨a, b⟩, rfl⟩ /-- If each fiber of a `sigma` type is equivalent to a fixed type, then the sigma type is equivalent to the product. -/ def sigma_equiv_prod_of_equiv {Ξ± Ξ²} {β₁ : Ξ± β†’ Sort*} (F : Ξ  a, β₁ a ≃ Ξ²) : sigma β₁ ≃ Ξ± Γ— Ξ² := (sigma_congr_right F).trans (sigma_equiv_prod Ξ± Ξ²) end section prod_congr variables {α₁ β₁ Ξ²β‚‚ : Type*} (e : α₁ β†’ β₁ ≃ Ξ²β‚‚) /-- A family of equivalences `Ξ  (a : α₁), β₁ ≃ Ξ²β‚‚` generates an equivalence between `β₁ Γ— α₁` and `Ξ²β‚‚ Γ— α₁`. -/ def prod_congr_left : β₁ Γ— α₁ ≃ Ξ²β‚‚ Γ— α₁ := { to_fun := Ξ» ab, ⟨e ab.2 ab.1, ab.2⟩, inv_fun := Ξ» ab, ⟨(e ab.2).symm ab.1, ab.2⟩, left_inv := by { rintros ⟨a, b⟩, simp }, right_inv := by { rintros ⟨a, b⟩, simp } } @[simp] lemma prod_congr_left_apply (b : β₁) (a : α₁) : prod_congr_left e (b, a) = (e a b, a) := rfl lemma prod_congr_refl_right (e : β₁ ≃ Ξ²β‚‚) : prod_congr e (equiv.refl α₁) = prod_congr_left (Ξ» _, e) := by { ext ⟨a, b⟩ : 1, simp } /-- A family of equivalences `Ξ  (a : α₁), β₁ ≃ Ξ²β‚‚` generates an equivalence between `α₁ Γ— β₁` and `α₁ Γ— Ξ²β‚‚`. -/ def prod_congr_right : α₁ Γ— β₁ ≃ α₁ Γ— Ξ²β‚‚ := { to_fun := Ξ» ab, ⟨ab.1, e ab.1 ab.2⟩, inv_fun := Ξ» ab, ⟨ab.1, (e ab.1).symm ab.2⟩, left_inv := by { rintros ⟨a, b⟩, simp }, right_inv := by { rintros ⟨a, b⟩, simp } } @[simp] lemma prod_congr_right_apply (a : α₁) (b : β₁) : prod_congr_right e (a, b) = (a, e a b) := rfl lemma prod_congr_refl_left (e : β₁ ≃ Ξ²β‚‚) : prod_congr (equiv.refl α₁) e = prod_congr_right (Ξ» _, e) := by { ext ⟨a, b⟩ : 1, simp } @[simp] lemma prod_congr_left_trans_prod_comm : (prod_congr_left e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_right e) := by { ext ⟨a, b⟩ : 1, simp } @[simp] lemma prod_congr_right_trans_prod_comm : (prod_congr_right e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_left e) := by { ext ⟨a, b⟩ : 1, simp } lemma sigma_congr_right_sigma_equiv_prod : (sigma_congr_right e).trans (sigma_equiv_prod α₁ Ξ²β‚‚) = (sigma_equiv_prod α₁ β₁).trans (prod_congr_right e) := by { ext ⟨a, b⟩ : 1, simp } lemma sigma_equiv_prod_sigma_congr_right : (sigma_equiv_prod α₁ β₁).symm.trans (sigma_congr_right e) = (prod_congr_right e).trans (sigma_equiv_prod α₁ Ξ²β‚‚).symm := by { ext ⟨a, b⟩ : 1, simp } /-- A variation on `equiv.prod_congr` where the equivalence in the second component can depend on the first component. A typical example is a shear mapping, explaining the name of this declaration. -/ @[simps {fully_applied := ff}] def prod_shear {α₁ β₁ Ξ±β‚‚ Ξ²β‚‚ : Type*} (e₁ : α₁ ≃ Ξ±β‚‚) (eβ‚‚ : α₁ β†’ β₁ ≃ Ξ²β‚‚) : α₁ Γ— β₁ ≃ Ξ±β‚‚ Γ— Ξ²β‚‚ := { to_fun := Ξ» x : α₁ Γ— β₁, (e₁ x.1, eβ‚‚ x.1 x.2), inv_fun := Ξ» y : Ξ±β‚‚ Γ— Ξ²β‚‚, (e₁.symm y.1, (eβ‚‚ $ e₁.symm y.1).symm y.2), left_inv := by { rintro ⟨x₁, yβ‚βŸ©, simp only [symm_apply_apply] }, right_inv := by { rintro ⟨x₁, yβ‚βŸ©, simp only [apply_symm_apply] } } end prod_congr namespace perm variables {α₁ β₁ Ξ²β‚‚ : Type*} [decidable_eq α₁] (a : α₁) (e : perm β₁) /-- `prod_extend_right a e` extends `e : perm Ξ²` to `perm (Ξ± Γ— Ξ²)` by sending `(a, b)` to `(a, e b)` and keeping the other `(a', b)` fixed. -/ def prod_extend_right : perm (α₁ Γ— β₁) := { to_fun := Ξ» ab, if ab.fst = a then (a, e ab.snd) else ab, inv_fun := Ξ» ab, if ab.fst = a then (a, e.symm ab.snd) else ab, left_inv := by { rintros ⟨k', x⟩, simp only, split_ifs with h; simp [h] }, right_inv := by { rintros ⟨k', x⟩, simp only, split_ifs with h; simp [h] } } @[simp] lemma prod_extend_right_apply_eq (b : β₁) : prod_extend_right a e (a, b) = (a, e b) := if_pos rfl lemma prod_extend_right_apply_ne {a a' : α₁} (h : a' β‰  a) (b : β₁) : prod_extend_right a e (a', b) = (a', b) := if_neg h lemma eq_of_prod_extend_right_ne {e : perm β₁} {a a' : α₁} {b : β₁} (h : prod_extend_right a e (a', b) β‰  (a', b)) : a' = a := by { contrapose! h, exact prod_extend_right_apply_ne _ h _ } @[simp] lemma fst_prod_extend_right (ab : α₁ Γ— β₁) : (prod_extend_right a e ab).fst = ab.fst := begin rw [prod_extend_right, coe_fn_mk], split_ifs with h, { rw h }, { refl } end end perm section /-- The type of functions to a product `Ξ± Γ— Ξ²` is equivalent to the type of pairs of functions `Ξ³ β†’ Ξ±` and `Ξ³ β†’ Ξ²`. -/ def arrow_prod_equiv_prod_arrow (Ξ± Ξ² Ξ³ : Type*) : (Ξ³ β†’ Ξ± Γ— Ξ²) ≃ (Ξ³ β†’ Ξ±) Γ— (Ξ³ β†’ Ξ²) := ⟨λ f, (Ξ» c, (f c).1, Ξ» c, (f c).2), Ξ» p c, (p.1 c, p.2 c), Ξ» f, funext $ Ξ» c, prod.mk.eta, Ξ» p, by { cases p, refl }⟩ open sum /-- The type of functions on a sum type `Ξ± βŠ• Ξ²` is equivalent to the type of pairs of functions on `Ξ±` and on `Ξ²`. -/ def sum_arrow_equiv_prod_arrow (Ξ± Ξ² Ξ³ : Type*) : ((Ξ± βŠ• Ξ²) β†’ Ξ³) ≃ (Ξ± β†’ Ξ³) Γ— (Ξ² β†’ Ξ³) := ⟨λ f, (f ∘ inl, f ∘ inr), Ξ» p, sum.elim p.1 p.2, Ξ» f, by { ext ⟨⟩; refl }, Ξ» p, by { cases p, refl }⟩ @[simp] lemma sum_arrow_equiv_prod_arrow_apply_fst {Ξ± Ξ² Ξ³} (f : (Ξ± βŠ• Ξ²) β†’ Ξ³) (a : Ξ±) : (sum_arrow_equiv_prod_arrow Ξ± Ξ² Ξ³ f).1 a = f (inl a) := rfl @[simp] lemma sum_arrow_equiv_prod_arrow_apply_snd {Ξ± Ξ² Ξ³} (f : (Ξ± βŠ• Ξ²) β†’ Ξ³) (b : Ξ²) : (sum_arrow_equiv_prod_arrow Ξ± Ξ² Ξ³ f).2 b = f (inr b) := rfl @[simp] lemma sum_arrow_equiv_prod_arrow_symm_apply_inl {Ξ± Ξ² Ξ³} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ³) (a : Ξ±) : ((sum_arrow_equiv_prod_arrow Ξ± Ξ² Ξ³).symm (f, g)) (inl a) = f a := rfl @[simp] lemma sum_arrow_equiv_prod_arrow_symm_apply_inr {Ξ± Ξ² Ξ³} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ³) (b : Ξ²) : ((sum_arrow_equiv_prod_arrow Ξ± Ξ² Ξ³).symm (f, g)) (inr b) = g b := rfl /-- Type product is right distributive with respect to type sum up to an equivalence. -/ def sum_prod_distrib (Ξ± Ξ² Ξ³ : Sort*) : (Ξ± βŠ• Ξ²) Γ— Ξ³ ≃ (Ξ± Γ— Ξ³) βŠ• (Ξ² Γ— Ξ³) := ⟨λ p, match p with (inl a, c) := inl (a, c) | (inr b, c) := inr (b, c) end, Ξ» s, match s with inl q := (inl q.1, q.2) | inr q := (inr q.1, q.2) end, Ξ» p, by rcases p with ⟨_ | _, _⟩; refl, Ξ» s, by rcases s with ⟨_, _⟩ | ⟨_, _⟩; refl⟩ @[simp] theorem sum_prod_distrib_apply_left {Ξ± Ξ² Ξ³} (a : Ξ±) (c : Ξ³) : sum_prod_distrib Ξ± Ξ² Ξ³ (sum.inl a, c) = sum.inl (a, c) := rfl @[simp] theorem sum_prod_distrib_apply_right {Ξ± Ξ² Ξ³} (b : Ξ²) (c : Ξ³) : sum_prod_distrib Ξ± Ξ² Ξ³ (sum.inr b, c) = sum.inr (b, c) := rfl /-- Type product is left distributive with respect to type sum up to an equivalence. -/ def prod_sum_distrib (Ξ± Ξ² Ξ³ : Sort*) : Ξ± Γ— (Ξ² βŠ• Ξ³) ≃ (Ξ± Γ— Ξ²) βŠ• (Ξ± Γ— Ξ³) := calc Ξ± Γ— (Ξ² βŠ• Ξ³) ≃ (Ξ² βŠ• Ξ³) Γ— Ξ± : prod_comm _ _ ... ≃ (Ξ² Γ— Ξ±) βŠ• (Ξ³ Γ— Ξ±) : sum_prod_distrib _ _ _ ... ≃ (Ξ± Γ— Ξ²) βŠ• (Ξ± Γ— Ξ³) : sum_congr (prod_comm _ _) (prod_comm _ _) @[simp] theorem prod_sum_distrib_apply_left {Ξ± Ξ² Ξ³} (a : Ξ±) (b : Ξ²) : prod_sum_distrib Ξ± Ξ² Ξ³ (a, sum.inl b) = sum.inl (a, b) := rfl @[simp] theorem prod_sum_distrib_apply_right {Ξ± Ξ² Ξ³} (a : Ξ±) (c : Ξ³) : prod_sum_distrib Ξ± Ξ² Ξ³ (a, sum.inr c) = sum.inr (a, c) := rfl /-- The product of an indexed sum of types (formally, a `sigma`-type `Ξ£ i, Ξ± i`) by a type `Ξ²` is equivalent to the sum of products `Ξ£ i, (Ξ± i Γ— Ξ²)`. -/ def sigma_prod_distrib {ΞΉ : Type*} (Ξ± : ΞΉ β†’ Type*) (Ξ² : Type*) : ((Ξ£ i, Ξ± i) Γ— Ξ²) ≃ (Ξ£ i, (Ξ± i Γ— Ξ²)) := ⟨λ p, ⟨p.1.1, (p.1.2, p.2)⟩, Ξ» p, (⟨p.1, p.2.1⟩, p.2.2), Ξ» p, by { rcases p with ⟨⟨_, _⟩, _⟩, refl }, Ξ» p, by { rcases p with ⟨_, ⟨_, _⟩⟩, refl }⟩ /-- The product `bool Γ— Ξ±` is equivalent to `Ξ± βŠ• Ξ±`. -/ def bool_prod_equiv_sum (Ξ± : Type u) : bool Γ— Ξ± ≃ Ξ± βŠ• Ξ± := calc bool Γ— Ξ± ≃ (unit βŠ• unit) Γ— Ξ± : prod_congr bool_equiv_punit_sum_punit (equiv.refl _) ... ≃ (unit Γ— Ξ±) βŠ• (unit Γ— Ξ±) : sum_prod_distrib _ _ _ ... ≃ Ξ± βŠ• Ξ± : sum_congr (punit_prod _) (punit_prod _) /-- The function type `bool β†’ Ξ±` is equivalent to `Ξ± Γ— Ξ±`. -/ def bool_to_equiv_prod (Ξ± : Type u) : (bool β†’ Ξ±) ≃ Ξ± Γ— Ξ± := calc (bool β†’ Ξ±) ≃ ((unit βŠ• unit) β†’ Ξ±) : (arrow_congr bool_equiv_punit_sum_punit (equiv.refl Ξ±)) ... ≃ (unit β†’ Ξ±) Γ— (unit β†’ Ξ±) : sum_arrow_equiv_prod_arrow _ _ _ ... ≃ Ξ± Γ— Ξ± : prod_congr (punit_arrow_equiv _) (punit_arrow_equiv _) @[simp] lemma bool_to_equiv_prod_apply {Ξ± : Type u} (f : bool β†’ Ξ±) : bool_to_equiv_prod Ξ± f = (f ff, f tt) := rfl @[simp] lemma bool_to_equiv_prod_symm_apply_ff {Ξ± : Type u} (p : Ξ± Γ— Ξ±) : (bool_to_equiv_prod Ξ±).symm p ff = p.1 := rfl @[simp] lemma bool_to_equiv_prod_symm_apply_tt {Ξ± : Type u} (p : Ξ± Γ— Ξ±) : (bool_to_equiv_prod Ξ±).symm p tt = p.2 := rfl end section open sum nat /-- The set of natural numbers is equivalent to `β„• βŠ• punit`. -/ def nat_equiv_nat_sum_punit : β„• ≃ β„• βŠ• punit.{u+1} := ⟨λ n, match n with zero := inr punit.star | succ a := inl a end, Ξ» s, match s with inl n := succ n | inr punit.star := zero end, Ξ» n, begin cases n, repeat { refl } end, Ξ» s, begin cases s with a u, { refl }, {cases u, { refl }} end⟩ /-- `β„• βŠ• punit` is equivalent to `β„•`. -/ def nat_sum_punit_equiv_nat : β„• βŠ• punit.{u+1} ≃ β„• := nat_equiv_nat_sum_punit.symm /-- The type of integer numbers is equivalent to `β„• βŠ• β„•`. -/ def int_equiv_nat_sum_nat : β„€ ≃ β„• βŠ• β„• := by refine ⟨_, _, _, _⟩; intro z; {cases z; [left, right]; assumption} <|> {cases z; refl} end /-- An equivalence between `Ξ±` and `Ξ²` generates an equivalence between `list Ξ±` and `list Ξ²`. -/ def list_equiv_of_equiv {Ξ± Ξ² : Type*} (e : Ξ± ≃ Ξ²) : list Ξ± ≃ list Ξ² := { to_fun := list.map e, inv_fun := list.map e.symm, left_inv := Ξ» l, by rw [list.map_map, e.symm_comp_self, list.map_id], right_inv := Ξ» l, by rw [list.map_map, e.self_comp_symm, list.map_id] } /-- `fin n` is equivalent to `{m // m < n}`. -/ def fin_equiv_subtype (n : β„•) : fin n ≃ {m // m < n} := ⟨λ x, ⟨x.1, x.2⟩, Ξ» x, ⟨x.1, x.2⟩, Ξ» ⟨a, b⟩, rfl,Ξ» ⟨a, b⟩, rfl⟩ /-- If `Ξ±` is equivalent to `Ξ²`, then `unique Ξ±` is equivalent to `unique Ξ²`. -/ def unique_congr (e : Ξ± ≃ Ξ²) : unique Ξ± ≃ unique Ξ² := { to_fun := Ξ» h, @equiv.unique _ _ h e.symm, inv_fun := Ξ» h, @equiv.unique _ _ h e, left_inv := Ξ» _, subsingleton.elim _ _, right_inv := Ξ» _, subsingleton.elim _ _ } /-- If `Ξ±` is equivalent to `Ξ²`, then `is_empty Ξ±` is equivalent to `is_empty Ξ²`. -/ lemma is_empty_congr (e : Ξ± ≃ Ξ²) : is_empty Ξ± ↔ is_empty Ξ² := ⟨λ h, @function.is_empty _ _ h e.symm, Ξ» h, @function.is_empty _ _ h e⟩ section open subtype /-- If `Ξ±` is equivalent to `Ξ²` and the predicates `p : Ξ± β†’ Prop` and `q : Ξ² β†’ Prop` are equivalent at corresponding points, then `{a // p a}` is equivalent to `{b // q b}`. For the statement where `Ξ± = Ξ²`, that is, `e : perm Ξ±`, see `perm.subtype_perm`. -/ def subtype_equiv {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} (e : Ξ± ≃ Ξ²) (h : βˆ€ a, p a ↔ q (e a)) : {a : Ξ± // p a} ≃ {b : Ξ² // q b} := ⟨λ x, ⟨e x, (h _).1 x.2⟩, Ξ» y, ⟨e.symm y, (h _).2 (by { simp, exact y.2 })⟩, Ξ» ⟨x, h⟩, subtype.ext_val $ by simp, Ξ» ⟨y, h⟩, subtype.ext_val $ by simp⟩ @[simp] lemma subtype_equiv_refl {p : Ξ± β†’ Prop} (h : βˆ€ a, p a ↔ p (equiv.refl _ a) := Ξ» a, iff.rfl) : (equiv.refl Ξ±).subtype_equiv h = equiv.refl {a : Ξ± // p a} := by { ext, refl } @[simp] lemma subtype_equiv_symm {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} (e : Ξ± ≃ Ξ²) (h : βˆ€ (a : Ξ±), p a ↔ q (e a)) : (e.subtype_equiv h).symm = e.symm.subtype_equiv (Ξ» a, by { convert (h $ e.symm a).symm, exact (e.apply_symm_apply a).symm, }) := rfl @[simp] lemma subtype_equiv_trans {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} {r : Ξ³ β†’ Prop} (e : Ξ± ≃ Ξ²) (f : Ξ² ≃ Ξ³) (h : βˆ€ (a : Ξ±), p a ↔ q (e a)) (h' : βˆ€ (b : Ξ²), q b ↔ r (f b)): (e.subtype_equiv h).trans (f.subtype_equiv h') = (e.trans f).subtype_equiv (Ξ» a, (h a).trans (h' $ e a)) := rfl @[simp] lemma subtype_equiv_apply {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} (e : Ξ± ≃ Ξ²) (h : βˆ€ (a : Ξ±), p a ↔ q (e a)) (x : {x // p x}) : e.subtype_equiv h x = ⟨e x, (h _).1 x.2⟩ := rfl /-- If two predicates `p` and `q` are pointwise equivalent, then `{x // p x}` is equivalent to `{x // q x}`. -/ @[simps] def subtype_equiv_right {p q : Ξ± β†’ Prop} (e : βˆ€x, p x ↔ q x) : {x // p x} ≃ {x // q x} := subtype_equiv (equiv.refl _) e /-- If `Ξ± ≃ Ξ²`, then for any predicate `p : Ξ² β†’ Prop` the subtype `{a // p (e a)}` is equivalent to the subtype `{b // p b}`. -/ def subtype_equiv_of_subtype {p : Ξ² β†’ Prop} (e : Ξ± ≃ Ξ²) : {a : Ξ± // p (e a)} ≃ {b : Ξ² // p b} := subtype_equiv e $ by simp /-- If `Ξ± ≃ Ξ²`, then for any predicate `p : Ξ± β†’ Prop` the subtype `{a // p a}` is equivalent to the subtype `{b // p (e.symm b)}`. This version is used by `equiv_rw`. -/ def subtype_equiv_of_subtype' {p : Ξ± β†’ Prop} (e : Ξ± ≃ Ξ²) : {a : Ξ± // p a} ≃ {b : Ξ² // p (e.symm b)} := e.symm.subtype_equiv_of_subtype.symm /-- If two predicates are equal, then the corresponding subtypes are equivalent. -/ def subtype_equiv_prop {Ξ± : Type*} {p q : Ξ± β†’ Prop} (h : p = q) : subtype p ≃ subtype q := subtype_equiv (equiv.refl Ξ±) (assume a, h β–Έ iff.rfl) /-- The subtypes corresponding to equal sets are equivalent. -/ @[simps apply] def set_congr {Ξ± : Type*} {s t : set Ξ±} (h : s = t) : s ≃ t := subtype_equiv_prop h /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. This version allows the β€œinner” predicate to depend on `h : p a`. -/ def subtype_subtype_equiv_subtype_exists {Ξ± : Type u} (p : Ξ± β†’ Prop) (q : subtype p β†’ Prop) : subtype q ≃ {a : Ξ± // βˆƒh:p a, q ⟨a, h⟩ } := ⟨λ⟨⟨a, ha⟩, ha'⟩, ⟨a, ha, ha'⟩, λ⟨a, ha⟩, ⟨⟨a, ha.cases_on $ assume h _, h⟩, by { cases ha, exact ha_h }⟩, assume ⟨⟨a, ha⟩, h⟩, rfl, assume ⟨a, h₁, hβ‚‚βŸ©, rfl⟩ @[simp] lemma subtype_subtype_equiv_subtype_exists_apply {Ξ± : Type u} (p : Ξ± β†’ Prop) (q : subtype p β†’ Prop) (a) : (subtype_subtype_equiv_subtype_exists p q a : Ξ±) = a := by { cases a, cases a_val, refl } /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. -/ def subtype_subtype_equiv_subtype_inter {Ξ± : Type u} (p q : Ξ± β†’ Prop) : {x : subtype p // q x.1} ≃ subtype (Ξ» x, p x ∧ q x) := (subtype_subtype_equiv_subtype_exists p _).trans $ subtype_equiv_right $ Ξ» x, exists_prop @[simp] lemma subtype_subtype_equiv_subtype_inter_apply {Ξ± : Type u} (p q : Ξ± β†’ Prop) (a) : (subtype_subtype_equiv_subtype_inter p q a : Ξ±) = a := by { cases a, cases a_val, refl } /-- If the outer subtype has more restrictive predicate than the inner one, then we can drop the latter. -/ def subtype_subtype_equiv_subtype {Ξ± : Type u} {p q : Ξ± β†’ Prop} (h : βˆ€ {x}, q x β†’ p x) : {x : subtype p // q x.1} ≃ subtype q := (subtype_subtype_equiv_subtype_inter p _).trans $ subtype_equiv_right $ assume x, ⟨and.right, Ξ» h₁, ⟨h h₁, hβ‚βŸ©βŸ© @[simp] lemma subtype_subtype_equiv_subtype_apply {Ξ± : Type u} {p q : Ξ± β†’ Prop} (h : βˆ€ x, q x β†’ p x) (a : {x : subtype p // q x.1}) : (subtype_subtype_equiv_subtype h a : Ξ±) = a := by { cases a, cases a_val, refl } /-- If a proposition holds for all elements, then the subtype is equivalent to the original type. -/ @[simps apply symm_apply] def subtype_univ_equiv {Ξ± : Type u} {p : Ξ± β†’ Prop} (h : βˆ€ x, p x) : subtype p ≃ Ξ± := ⟨λ x, x, Ξ» x, ⟨x, h x⟩, Ξ» x, subtype.eq rfl, Ξ» x, rfl⟩ /-- A subtype of a sigma-type is a sigma-type over a subtype. -/ def subtype_sigma_equiv {Ξ± : Type u} (p : Ξ± β†’ Type v) (q : Ξ± β†’ Prop) : { y : sigma p // q y.1 } ≃ Ξ£(x : subtype q), p x.1 := ⟨λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, Ξ» x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, Ξ» ⟨⟨x, h⟩, y⟩, rfl, Ξ» ⟨⟨x, y⟩, h⟩, rfl⟩ /-- A sigma type over a subtype is equivalent to the sigma set over the original type, if the fiber is empty outside of the subset -/ def sigma_subtype_equiv_of_subset {Ξ± : Type u} (p : Ξ± β†’ Type v) (q : Ξ± β†’ Prop) (h : βˆ€ x, p x β†’ q x) : (Ξ£ x : subtype q, p x) ≃ Ξ£ x : Ξ±, p x := (subtype_sigma_equiv p q).symm.trans $ subtype_univ_equiv $ Ξ» x, h x.1 x.2 /-- If a predicate `p : Ξ² β†’ Prop` is true on the range of a map `f : Ξ± β†’ Ξ²`, then `Ξ£ y : {y // p y}, {x // f x = y}` is equivalent to `Ξ±`. -/ def sigma_subtype_preimage_equiv {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) (p : Ξ² β†’ Prop) (h : βˆ€ x, p (f x)) : (Ξ£ y : subtype p, {x : Ξ± // f x = y}) ≃ Ξ± := calc _ ≃ Ξ£ y : Ξ², {x : Ξ± // f x = y} : sigma_subtype_equiv_of_subset _ p (Ξ» y ⟨x, h'⟩, h' β–Έ h x) ... ≃ Ξ± : sigma_preimage_equiv f /-- If for each `x` we have `p x ↔ q (f x)`, then `Ξ£ y : {y // q y}, f ⁻¹' {y}` is equivalent to `{x // p x}`. -/ def sigma_subtype_preimage_equiv_subtype {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} (h : βˆ€ x, p x ↔ q (f x)) : (Ξ£ y : subtype q, {x : Ξ± // f x = y}) ≃ subtype p := calc (Ξ£ y : subtype q, {x : Ξ± // f x = y}) ≃ Ξ£ y : subtype q, {x : subtype p // subtype.mk (f x) ((h x).1 x.2) = y} : begin apply sigma_congr_right, assume y, symmetry, refine (subtype_subtype_equiv_subtype_exists _ _).trans (subtype_equiv_right _), assume x, exact ⟨λ ⟨hp, h'⟩, congr_arg subtype.val h', Ξ» h', ⟨(h x).2 (h'.symm β–Έ y.2), subtype.eq h'⟩⟩ end ... ≃ subtype p : sigma_preimage_equiv (Ξ» x : subtype p, (⟨f x, (h x).1 x.property⟩ : subtype q)) /-- The `pi`-type `Ξ  i, Ο€ i` is equivalent to the type of sections `f : ΞΉ β†’ Ξ£ i, Ο€ i` of the `sigma` type such that for all `i` we have `(f i).fst = i`. -/ def pi_equiv_subtype_sigma (ΞΉ : Type*) (Ο€ : ΞΉ β†’ Type*) : (Ξ i, Ο€ i) ≃ {f : ΞΉ β†’ Ξ£i, Ο€ i | βˆ€i, (f i).1 = i } := ⟨ Ξ»f, ⟨λi, ⟨i, f i⟩, assume i, rfl⟩, Ξ»f i, begin rw ← f.2 i, exact (f.1 i).2 end, assume f, funext $ assume i, rfl, assume ⟨f, hf⟩, subtype.eq $ funext $ assume i, sigma.eq (hf i).symm $ eq_of_heq $ rec_heq_of_heq _ $ rec_heq_of_heq _ $ heq.refl _⟩ /-- The set of functions `f : Ξ  a, Ξ² a` such that for all `a` we have `p a (f a)` is equivalent to the set of functions `Ξ  a, {b : Ξ² a // p a b}`. -/ def subtype_pi_equiv_pi {Ξ± : Sort u} {Ξ² : Ξ± β†’ Sort v} {p : Ξ a, Ξ² a β†’ Prop} : {f : Ξ a, Ξ² a // βˆ€a, p a (f a) } ≃ Ξ a, { b : Ξ² a // p a b } := ⟨λf a, ⟨f.1 a, f.2 a⟩, Ξ»f, ⟨λa, (f a).1, Ξ»a, (f a).2⟩, by { rintro ⟨f, h⟩, refl }, by { rintro f, funext a, exact subtype.ext_val rfl }⟩ /-- A subtype of a product defined by componentwise conditions is equivalent to a product of subtypes. -/ def subtype_prod_equiv_prod {Ξ± : Type u} {Ξ² : Type v} {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} : {c : Ξ± Γ— Ξ² // p c.1 ∧ q c.2} ≃ ({a // p a} Γ— {b // q b}) := ⟨λ x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, Ξ» x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, Ξ» ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl, Ξ» ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl⟩ /-- A subtype of a `prod` is equivalent to a sigma type whose fibers are subtypes. -/ def subtype_prod_equiv_sigma_subtype {Ξ± Ξ² : Type*} (p : Ξ± β†’ Ξ² β†’ Prop) : {x : Ξ± Γ— Ξ² // p x.1 x.2} ≃ Ξ£ a, {b : Ξ² // p a b} := { to_fun := Ξ» x, ⟨x.1.1, x.1.2, x.prop⟩, inv_fun := Ξ» x, ⟨⟨x.1, x.2⟩, x.2.prop⟩, left_inv := Ξ» x, by ext; refl, right_inv := Ξ» ⟨a, b, pab⟩, rfl } end section subtype_equiv_codomain variables {X : Type*} {Y : Type*} [decidable_eq X] {x : X} /-- The type of all functions `X β†’ Y` with prescribed values for all `x' β‰  x` is equivalent to the codomain `Y`. -/ def subtype_equiv_codomain (f : {x' // x' β‰  x} β†’ Y) : {g : X β†’ Y // g ∘ coe = f} ≃ Y := (subtype_preimage _ f).trans $ @fun_unique {x' // Β¬ x' β‰  x} _ $ show unique {x' // Β¬ x' β‰  x}, from @equiv.unique _ _ (show unique {x' // x' = x}, from { default := ⟨x, rfl⟩, uniq := Ξ» ⟨x', h⟩, subtype.val_injective h }) (subtype_equiv_right $ Ξ» a, not_not) @[simp] lemma coe_subtype_equiv_codomain (f : {x' // x' β‰  x} β†’ Y) : (subtype_equiv_codomain f : {g : X β†’ Y // g ∘ coe = f} β†’ Y) = Ξ» g, (g : X β†’ Y) x := rfl @[simp] lemma subtype_equiv_codomain_apply (f : {x' // x' β‰  x} β†’ Y) (g : {g : X β†’ Y // g ∘ coe = f}) : subtype_equiv_codomain f g = (g : X β†’ Y) x := rfl lemma coe_subtype_equiv_codomain_symm (f : {x' // x' β‰  x} β†’ Y) : ((subtype_equiv_codomain f).symm : Y β†’ {g : X β†’ Y // g ∘ coe = f}) = Ξ» y, ⟨λ x', if h : x' β‰  x then f ⟨x', h⟩ else y, by { funext x', dsimp, erw [dif_pos x'.2, subtype.coe_eta] }⟩ := rfl @[simp] lemma subtype_equiv_codomain_symm_apply (f : {x' // x' β‰  x} β†’ Y) (y : Y) (x' : X) : ((subtype_equiv_codomain f).symm y : X β†’ Y) x' = if h : x' β‰  x then f ⟨x', h⟩ else y := rfl @[simp] lemma subtype_equiv_codomain_symm_apply_eq (f : {x' // x' β‰  x} β†’ Y) (y : Y) : ((subtype_equiv_codomain f).symm y : X β†’ Y) x = y := dif_neg (not_not.mpr rfl) lemma subtype_equiv_codomain_symm_apply_ne (f : {x' // x' β‰  x} β†’ Y) (y : Y) (x' : X) (h : x' β‰  x) : ((subtype_equiv_codomain f).symm y : X β†’ Y) x' = f ⟨x', h⟩ := dif_pos h end subtype_equiv_codomain /-- A set is equivalent to its image under an equivalence. -/ -- We could construct this using `equiv.set.image e s e.injective`, -- but this definition provides an explicit inverse. @[simps] def image {Ξ± Ξ² : Type*} (e : Ξ± ≃ Ξ²) (s : set Ξ±) : s ≃ e '' s := { to_fun := Ξ» x, ⟨e x.1, by simp⟩, inv_fun := Ξ» y, ⟨e.symm y.1, by { rcases y with ⟨-, ⟨a, ⟨m, rfl⟩⟩⟩, simpa using m, }⟩, left_inv := Ξ» x, by simp, right_inv := Ξ» y, by simp, }. namespace set open set /-- `univ Ξ±` is equivalent to `Ξ±`. -/ @[simps apply symm_apply] protected def univ (Ξ±) : @univ Ξ± ≃ Ξ± := ⟨coe, Ξ» a, ⟨a, trivial⟩, Ξ» ⟨a, _⟩, rfl, Ξ» a, rfl⟩ /-- An empty set is equivalent to the `empty` type. -/ protected def empty (Ξ±) : (βˆ… : set Ξ±) ≃ empty := equiv_empty _ /-- An empty set is equivalent to a `pempty` type. -/ protected def pempty (Ξ±) : (βˆ… : set Ξ±) ≃ pempty := equiv_pempty _ /-- If sets `s` and `t` are separated by a decidable predicate, then `s βˆͺ t` is equivalent to `s βŠ• t`. -/ protected def union' {Ξ±} {s t : set Ξ±} (p : Ξ± β†’ Prop) [decidable_pred p] (hs : βˆ€ x ∈ s, p x) (ht : βˆ€ x ∈ t, Β¬ p x) : (s βˆͺ t : set Ξ±) ≃ s βŠ• t := { to_fun := Ξ» x, if hp : p x then sum.inl ⟨_, x.2.resolve_right (Ξ» xt, ht _ xt hp)⟩ else sum.inr ⟨_, x.2.resolve_left (Ξ» xs, hp (hs _ xs))⟩, inv_fun := Ξ» o, match o with | (sum.inl x) := ⟨x, or.inl x.2⟩ | (sum.inr x) := ⟨x, or.inr x.2⟩ end, left_inv := Ξ» ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr, right_inv := Ξ» o, begin rcases o with ⟨x, h⟩ | ⟨x, h⟩; dsimp [union'._match_1]; [simp [hs _ h], simp [ht _ h]] end } /-- If sets `s` and `t` are disjoint, then `s βˆͺ t` is equivalent to `s βŠ• t`. -/ protected def union {Ξ±} {s t : set Ξ±} [decidable_pred (Ξ» x, x ∈ s)] (H : s ∩ t βŠ† βˆ…) : (s βˆͺ t : set Ξ±) ≃ s βŠ• t := set.union' (Ξ» x, x ∈ s) (Ξ» _, id) (Ξ» x xt xs, H ⟨xs, xt⟩) lemma union_apply_left {Ξ±} {s t : set Ξ±} [decidable_pred (Ξ» x, x ∈ s)] (H : s ∩ t βŠ† βˆ…) {a : (s βˆͺ t : set Ξ±)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ := dif_pos ha lemma union_apply_right {Ξ±} {s t : set Ξ±} [decidable_pred (Ξ» x, x ∈ s)] (H : s ∩ t βŠ† βˆ…) {a : (s βˆͺ t : set Ξ±)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ := dif_neg $ Ξ» h, H ⟨h, ha⟩ @[simp] lemma union_symm_apply_left {Ξ±} {s t : set Ξ±} [decidable_pred (Ξ» x, x ∈ s)] (H : s ∩ t βŠ† βˆ…) (a : s) : (equiv.set.union H).symm (sum.inl a) = ⟨a, subset_union_left _ _ a.2⟩ := rfl @[simp] lemma union_symm_apply_right {Ξ±} {s t : set Ξ±} [decidable_pred (Ξ» x, x ∈ s)] (H : s ∩ t βŠ† βˆ…) (a : t) : (equiv.set.union H).symm (sum.inr a) = ⟨a, subset_union_right _ _ a.2⟩ := rfl /-- A singleton set is equivalent to a `punit` type. -/ protected def singleton {Ξ±} (a : Ξ±) : ({a} : set Ξ±) ≃ punit.{u} := ⟨λ _, punit.star, Ξ» _, ⟨a, mem_singleton _⟩, Ξ» ⟨x, h⟩, by { simp at h, subst x }, Ξ» ⟨⟩, rfl⟩ /-- Equal sets are equivalent. -/ @[simps apply symm_apply] protected def of_eq {Ξ± : Type u} {s t : set Ξ±} (h : s = t) : s ≃ t := { to_fun := Ξ» x, ⟨x, h β–Έ x.2⟩, inv_fun := Ξ» x, ⟨x, h.symm β–Έ x.2⟩, left_inv := Ξ» _, subtype.eq rfl, right_inv := Ξ» _, subtype.eq rfl } /-- If `a βˆ‰ s`, then `insert a s` is equivalent to `s βŠ• punit`. -/ protected def insert {Ξ±} {s : set.{u} Ξ±} [decidable_pred (∈ s)] {a : Ξ±} (H : a βˆ‰ s) : (insert a s : set Ξ±) ≃ s βŠ• punit.{u+1} := calc (insert a s : set Ξ±) ≃ β†₯(s βˆͺ {a}) : equiv.set.of_eq (by simp) ... ≃ s βŠ• ({a} : set Ξ±) : equiv.set.union (by finish [set.subset_def]) ... ≃ s βŠ• punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _) @[simp] lemma insert_symm_apply_inl {Ξ±} {s : set.{u} Ξ±} [decidable_pred (∈ s)] {a : Ξ±} (H : a βˆ‰ s) (b : s) : (equiv.set.insert H).symm (sum.inl b) = ⟨b, or.inr b.2⟩ := rfl @[simp] lemma insert_symm_apply_inr {Ξ±} {s : set.{u} Ξ±} [decidable_pred (∈ s)] {a : Ξ±} (H : a βˆ‰ s) (b : punit.{u+1}) : (equiv.set.insert H).symm (sum.inr b) = ⟨a, or.inl rfl⟩ := rfl @[simp] lemma insert_apply_left {Ξ±} {s : set.{u} Ξ±} [decidable_pred (∈ s)] {a : Ξ±} (H : a βˆ‰ s) : equiv.set.insert H ⟨a, or.inl rfl⟩ = sum.inr punit.star := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma insert_apply_right {Ξ±} {s : set.{u} Ξ±} [decidable_pred (∈ s)] {a : Ξ±} (H : a βˆ‰ s) (b : s) : equiv.set.insert H ⟨b, or.inr b.2⟩ = sum.inl b := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl /-- If `s : set Ξ±` is a set with decidable membership, then `s βŠ• sᢜ` is equivalent to `Ξ±`. -/ protected def sum_compl {Ξ±} (s : set Ξ±) [decidable_pred (∈ s)] : s βŠ• (sᢜ : set Ξ±) ≃ Ξ± := calc s βŠ• (sᢜ : set Ξ±) ≃ β†₯(s βˆͺ sᢜ) : (equiv.set.union (by simp [set.ext_iff])).symm ... ≃ @univ Ξ± : equiv.set.of_eq (by simp) ... ≃ Ξ± : equiv.set.univ _ @[simp] lemma sum_compl_apply_inl {Ξ± : Type u} (s : set Ξ±) [decidable_pred (∈ s)] (x : s) : equiv.set.sum_compl s (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {Ξ± : Type u} (s : set Ξ±) [decidable_pred (∈ s)] (x : sᢜ) : equiv.set.sum_compl s (sum.inr x) = x := rfl lemma sum_compl_symm_apply_of_mem {Ξ± : Type u} {s : set Ξ±} [decidable_pred (∈ s)] {x : Ξ±} (hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ := have ↑(⟨x, or.inl hx⟩ : (s βˆͺ sᢜ : set Ξ±)) ∈ s, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this } lemma sum_compl_symm_apply_of_not_mem {Ξ± : Type u} {s : set Ξ±} [decidable_pred (∈ s)] {x : Ξ±} (hx : x βˆ‰ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ := have ↑(⟨x, or.inr hx⟩ : (s βˆͺ sᢜ : set Ξ±)) ∈ sᢜ, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this } @[simp] lemma sum_compl_symm_apply {Ξ± : Type*} {s : set Ξ±} [decidable_pred (∈ s)] {x : s} : (equiv.set.sum_compl s).symm x = sum.inl x := by cases x with x hx; exact set.sum_compl_symm_apply_of_mem hx @[simp] lemma sum_compl_symm_apply_compl {Ξ± : Type*} {s : set Ξ±} [decidable_pred (∈ s)] {x : sᢜ} : (equiv.set.sum_compl s).symm x = sum.inr x := by cases x with x hx; exact set.sum_compl_symm_apply_of_not_mem hx /-- `sum_diff_subset s t` is the natural equivalence between `s βŠ• (t \ s)` and `t`, where `s` and `t` are two sets. -/ protected def sum_diff_subset {Ξ±} {s t : set Ξ±} (h : s βŠ† t) [decidable_pred (∈ s)] : s βŠ• (t \ s : set Ξ±) ≃ t := calc s βŠ• (t \ s : set Ξ±) ≃ (s βˆͺ (t \ s) : set Ξ±) : (equiv.set.union (by simp [inter_diff_self])).symm ... ≃ t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] }) @[simp] lemma sum_diff_subset_apply_inl {Ξ±} {s t : set Ξ±} (h : s βŠ† t) [decidable_pred (∈ s)] (x : s) : equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl @[simp] lemma sum_diff_subset_apply_inr {Ξ±} {s t : set Ξ±} (h : s βŠ† t) [decidable_pred (∈ s)] (x : t \ s) : equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl lemma sum_diff_subset_symm_apply_of_mem {Ξ±} {s t : set Ξ±} (h : s βŠ† t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∈ s) : (equiv.set.sum_diff_subset h).symm x = sum.inl ⟨x, hx⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inl], exact subtype.eq rfl, end lemma sum_diff_subset_symm_apply_of_not_mem {Ξ±} {s t : set Ξ±} (h : s βŠ† t) [decidable_pred (∈ s)] {x : t} (hx : x.1 βˆ‰ s) : (equiv.set.sum_diff_subset h).symm x = sum.inr ⟨x, ⟨x.2, hx⟩⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inr], exact subtype.eq rfl, end /-- If `s` is a set with decidable membership, then the sum of `s βˆͺ t` and `s ∩ t` is equivalent to `s βŠ• t`. -/ protected def union_sum_inter {Ξ± : Type u} (s t : set Ξ±) [decidable_pred (∈ s)] : (s βˆͺ t : set Ξ±) βŠ• (s ∩ t : set Ξ±) ≃ s βŠ• t := calc (s βˆͺ t : set Ξ±) βŠ• (s ∩ t : set Ξ±) ≃ (s βˆͺ t \ s : set Ξ±) βŠ• (s ∩ t : set Ξ±) : by rw [union_diff_self] ... ≃ (s βŠ• (t \ s : set Ξ±)) βŠ• (s ∩ t : set Ξ±) : sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _) ... ≃ s βŠ• (t \ s : set Ξ±) βŠ• (s ∩ t : set Ξ±) : sum_assoc _ _ _ ... ≃ s βŠ• (t \ s βˆͺ s ∩ t : set Ξ±) : sum_congr (equiv.refl _) begin refine (set.union' (βˆ‰ s) _ _).symm, exacts [Ξ» x hx, hx.2, Ξ» x hx, not_not_intro hx.1] end ... ≃ s βŠ• t : by { rw (_ : t \ s βˆͺ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] } /-- Given an equivalence `eβ‚€` between sets `s : set Ξ±` and `t : set Ξ²`, the set of equivalences `e : Ξ± ≃ Ξ²` such that `e ↑x = ↑(eβ‚€ x)` for each `x : s` is equivalent to the set of equivalences between `sᢜ` and `tᢜ`. -/ protected def compl {Ξ± : Type u} {Ξ² : Type v} {s : set Ξ±} {t : set Ξ²} [decidable_pred (∈ s)] [decidable_pred (∈ t)] (eβ‚€ : s ≃ t) : {e : Ξ± ≃ Ξ² // βˆ€ x : s, e x = eβ‚€ x} ≃ ((sᢜ : set Ξ±) ≃ (tᢜ : set Ξ²)) := { to_fun := Ξ» e, subtype_equiv e (Ξ» a, not_congr $ iff.symm $ maps_to.mem_iff (maps_to_iff_exists_map_subtype.2 ⟨eβ‚€, e.2⟩) (surj_on.maps_to_compl (surj_on_iff_exists_map_subtype.2 ⟨t, eβ‚€, subset.refl t, eβ‚€.surjective, e.2⟩) e.1.injective)), inv_fun := Ξ» e₁, subtype.mk (calc Ξ± ≃ s βŠ• (sᢜ : set Ξ±) : (set.sum_compl s).symm ... ≃ t βŠ• (tᢜ : set Ξ²) : eβ‚€.sum_congr e₁ ... ≃ Ξ² : set.sum_compl t) (Ξ» x, by simp only [sum.map_inl, trans_apply, sum_congr_apply, set.sum_compl_apply_inl, set.sum_compl_symm_apply]), left_inv := Ξ» e, begin ext x, by_cases hx : x ∈ s, { simp only [set.sum_compl_symm_apply_of_mem hx, ←e.prop ⟨x, hx⟩, sum.map_inl, sum_congr_apply, trans_apply, subtype.coe_mk, set.sum_compl_apply_inl] }, { simp only [set.sum_compl_symm_apply_of_not_mem hx, sum.map_inr, subtype_equiv_apply, set.sum_compl_apply_inr, trans_apply, sum_congr_apply, subtype.coe_mk] }, end, right_inv := Ξ» e, equiv.ext $ Ξ» x, by simp only [sum.map_inr, subtype_equiv_apply, set.sum_compl_apply_inr, function.comp_app, sum_congr_apply, equiv.coe_trans, subtype.coe_eta, subtype.coe_mk, set.sum_compl_symm_apply_compl] } /-- The set product of two sets is equivalent to the type product of their coercions to types. -/ protected def prod {Ξ± Ξ²} (s : set Ξ±) (t : set Ξ²) : s.prod t ≃ s Γ— t := @subtype_prod_equiv_prod Ξ± Ξ² s t /-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/ protected noncomputable def image_of_inj_on {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (s : set Ξ±) (H : inj_on f s) : s ≃ (f '' s) := ⟨λ p, ⟨f p, mem_image_of_mem f p.2⟩, Ξ» p, ⟨classical.some p.2, (classical.some_spec p.2).1⟩, Ξ» ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h (classical.some_spec (mem_image_of_mem f h)).2), Ξ» ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩ /-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/ @[simps apply] protected noncomputable def image {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (s : set Ξ±) (H : injective f) : s ≃ (f '' s) := equiv.set.image_of_inj_on f s (H.inj_on s) @[simp] protected lemma image_symm_apply {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (s : set Ξ±) (H : injective f) (x : Ξ±) (h : x ∈ s) : (set.image f s H).symm ⟨f x, ⟨x, ⟨h, rfl⟩⟩⟩ = ⟨x, h⟩ := begin apply (set.image f s H).injective, simp [(set.image f s H).apply_symm_apply], end lemma image_symm_preimage {Ξ± Ξ²} {f : Ξ± β†’ Ξ²} (hf : injective f) (u s : set Ξ±) : (Ξ» x, (set.image f s hf).symm x : f '' s β†’ Ξ±) ⁻¹' u = coe ⁻¹' (f '' u) := begin ext ⟨b, a, has, rfl⟩, have : βˆ€(h : βˆƒa', a' ∈ s ∧ a' = a), classical.some h = a := Ξ» h, (classical.some_spec h).2, simp [equiv.set.image, equiv.set.image_of_inj_on, hf.eq_iff, this], end /-- If `Ξ±` is equivalent to `Ξ²`, then `set Ξ±` is equivalent to `set Ξ²`. -/ @[simps] protected def congr {Ξ± Ξ² : Type*} (e : Ξ± ≃ Ξ²) : set Ξ± ≃ set Ξ² := ⟨λ s, e '' s, Ξ» t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩ /-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/ protected def sep {Ξ± : Type u} (s : set Ξ±) (t : Ξ± β†’ Prop) : ({ x ∈ s | t x } : set Ξ±) ≃ { x : s | t x } := (equiv.subtype_subtype_equiv_subtype_inter s t).symm /-- The set `𝒫 S := {x | x βŠ† S}` is equivalent to the type `set S`. -/ protected def powerset {Ξ±} (S : set Ξ±) : 𝒫 S ≃ set S := { to_fun := Ξ» x : 𝒫 S, coe ⁻¹' (x : set Ξ±), inv_fun := Ξ» x : set S, ⟨coe '' x, by rintro _ ⟨a : S, _, rfl⟩; exact a.2⟩, left_inv := Ξ» x, by ext y; exact ⟨λ ⟨⟨_, _⟩, h, rfl⟩, h, Ξ» h, ⟨⟨_, x.2 h⟩, h, rfl⟩⟩, right_inv := Ξ» x, by ext; simp } /-- If `s` is a set in `range f`, then its image under `range_splitting f` is in bijection (via `f`) with `s`. -/ @[simps] noncomputable def range_splitting_image_equiv {Ξ± Ξ² : Type*} (f : Ξ± β†’ Ξ²) (s : set (range f)) : range_splitting f '' s ≃ s := { to_fun := Ξ» x, ⟨⟨f x, by simp⟩, (by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simpa [apply_range_splitting f] using m, })⟩, inv_fun := Ξ» x, ⟨range_splitting f x, ⟨x, ⟨x.2, rfl⟩⟩⟩, left_inv := Ξ» x, by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simp [apply_range_splitting f] }, right_inv := Ξ» x, by simp [apply_range_splitting f], } end set /-- If `f : Ξ± β†’ Ξ²` has a left-inverse when `Ξ±` is nonempty, then `Ξ±` is computably equivalent to the range of `f`. While awkward, the `nonempty Ξ±` hypothesis on `f_inv` and `hf` allows this to be used when `Ξ±` is empty too. This hypothesis is absent on analogous definitions on stronger `equiv`s like `linear_equiv.of_left_inverse` and `ring_equiv.of_left_inverse` as their typeclass assumptions are already sufficient to ensure non-emptiness. -/ @[simps] def of_left_inverse {Ξ± Ξ² : Sort*} (f : Ξ± β†’ Ξ²) (f_inv : nonempty Ξ± β†’ Ξ² β†’ Ξ±) (hf : Ξ  h : nonempty Ξ±, left_inverse (f_inv h) f) : Ξ± ≃ set.range f := { to_fun := Ξ» a, ⟨f a, a, rfl⟩, inv_fun := Ξ» b, f_inv (nonempty_of_exists b.2) b, left_inv := Ξ» a, hf ⟨a⟩ a, right_inv := Ξ» ⟨b, a, ha⟩, subtype.eq $ show f (f_inv ⟨a⟩ b) = b, from eq.trans (congr_arg f $ by exact ha β–Έ (hf _ a)) ha } /-- If `f : Ξ± β†’ Ξ²` has a left-inverse, then `Ξ±` is computably equivalent to the range of `f`. Note that if `Ξ±` is empty, no such `f_inv` exists and so this definition can't be used, unlike the stronger but less convenient `of_left_inverse`. -/ abbreviation of_left_inverse' {Ξ± Ξ² : Sort*} (f : Ξ± β†’ Ξ²) (f_inv : Ξ² β†’ Ξ±) (hf : left_inverse f_inv f) : Ξ± ≃ set.range f := of_left_inverse f (Ξ» _, f_inv) (Ξ» _, hf) /-- If `f : Ξ± β†’ Ξ²` is an injective function, then domain `Ξ±` is equivalent to the range of `f`. -/ @[simps apply] noncomputable def of_injective {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (hf : injective f) : Ξ± ≃ set.range f := equiv.of_left_inverse f (Ξ» h, by exactI function.inv_fun f) (Ξ» h, by exactI function.left_inverse_inv_fun hf) theorem apply_of_injective_symm {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (hf : injective f) (b : set.range f) : f ((of_injective f hf).symm b) = b := subtype.ext_iff.1 $ (of_injective f hf).apply_symm_apply b @[simp] theorem of_injective_symm_apply {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (hf : injective f) (a : Ξ±) : (of_injective f hf).symm ⟨f a, ⟨a, rfl⟩⟩ = a := begin apply (of_injective f hf).injective, simp [apply_of_injective_symm f hf], end @[simp] lemma self_comp_of_injective_symm {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (hf : injective f) : f ∘ ((of_injective f hf).symm) = coe := funext (Ξ» x, apply_of_injective_symm f hf x) lemma of_left_inverse_eq_of_injective {Ξ± Ξ² : Type*} (f : Ξ± β†’ Ξ²) (f_inv : nonempty Ξ± β†’ Ξ² β†’ Ξ±) (hf : Ξ  h : nonempty Ξ±, left_inverse (f_inv h) f) : of_left_inverse f f_inv hf = of_injective f ((em (nonempty Ξ±)).elim (Ξ» h, (hf h).injective) (Ξ» h _ _ _, by { haveI : subsingleton Ξ± := subsingleton_of_not_nonempty h, simp })) := by { ext, simp } lemma of_left_inverse'_eq_of_injective {Ξ± Ξ² : Type*} (f : Ξ± β†’ Ξ²) (f_inv : Ξ² β†’ Ξ±) (hf : left_inverse f_inv f) : of_left_inverse' f f_inv hf = of_injective f hf.injective := by { ext, simp } /-- If `f` is a bijective function, then its domain is equivalent to its codomain. -/ @[simps apply] noncomputable def of_bijective {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (hf : bijective f) : Ξ± ≃ Ξ² := (of_injective f hf.1).trans $ (set_congr hf.2.range_eq).trans $ equiv.set.univ Ξ² lemma of_bijective_apply_symm_apply {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (hf : bijective f) (x : Ξ²) : f ((of_bijective f hf).symm x) = x := (of_bijective f hf).apply_symm_apply x @[simp] lemma of_bijective_symm_apply_apply {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (hf : bijective f) (x : Ξ±) : (of_bijective f hf).symm (f x) = x := (of_bijective f hf).symm_apply_apply x section variables {Ξ±' Ξ²' : Type*} (e : perm Ξ±') {p : Ξ²' β†’ Prop} [decidable_pred p] (f : Ξ±' ≃ subtype p) /-- Extend the domain of `e : equiv.perm Ξ±` to one that is over `Ξ²` via `f : Ξ± β†’ subtype p`, where `p : Ξ² β†’ Prop`, permuting only the `b : Ξ²` that satisfy `p b`. This can be used to extend the domain across a function `f : Ξ± β†’ Ξ²`, keeping everything outside of `set.range f` fixed. For this use-case `equiv` given by `f` can be constructed by `equiv.of_left_inverse'` or `equiv.of_left_inverse` when there is a known inverse, or `equiv.of_injective` in the general case.`. -/ def perm.extend_domain : perm Ξ²' := (perm_congr f e).subtype_congr (equiv.refl _) @[simp] lemma perm.extend_domain_apply_image (a : Ξ±') : e.extend_domain f (f a) = f (e a) := by simp [perm.extend_domain] lemma perm.extend_domain_apply_subtype {b : Ξ²'} (h : p b) : e.extend_domain f b = f (e (f.symm ⟨b, h⟩)) := by simp [perm.extend_domain, h] lemma perm.extend_domain_apply_not_subtype {b : Ξ²'} (h : Β¬ p b) : e.extend_domain f b = b := by simp [perm.extend_domain, h] @[simp] lemma perm.extend_domain_refl : perm.extend_domain (equiv.refl _) f = equiv.refl _ := by simp [perm.extend_domain] @[simp] lemma perm.extend_domain_symm : (e.extend_domain f).symm = perm.extend_domain e.symm f := rfl lemma perm.extend_domain_trans (e e' : perm Ξ±') : (e.extend_domain f).trans (e'.extend_domain f) = perm.extend_domain (e.trans e') f := by simp [perm.extend_domain, perm_congr_trans] end /-- Subtype of the quotient is equivalent to the quotient of the subtype. Let `Ξ±` be a setoid with equivalence relation `~`. Let `pβ‚‚` be a predicate on the quotient type `Ξ±/~`, and `p₁` be the lift of this predicate to `Ξ±`: `p₁ a ↔ pβ‚‚ ⟦a⟧`. Let `~β‚‚` be the restriction of `~` to `{x // p₁ x}`. Then `{x // pβ‚‚ x}` is equivalent to the quotient of `{x // p₁ x}` by `~β‚‚`. -/ def subtype_quotient_equiv_quotient_subtype (p₁ : Ξ± β†’ Prop) [s₁ : setoid Ξ±] [sβ‚‚ : setoid (subtype p₁)] (pβ‚‚ : quotient s₁ β†’ Prop) (hpβ‚‚ : βˆ€ a, p₁ a ↔ pβ‚‚ ⟦a⟧) (h : βˆ€ x y : subtype p₁, @setoid.r _ sβ‚‚ x y ↔ (x : Ξ±) β‰ˆ y) : {x // pβ‚‚ x} ≃ quotient sβ‚‚ := { to_fun := Ξ» a, quotient.hrec_on a.1 (Ξ» a h, ⟦⟨a, (hpβ‚‚ _).2 h⟩⟧) (Ξ» a b hab, hfunext (by rw quotient.sound hab) (Ξ» h₁ hβ‚‚ _, heq_of_eq (quotient.sound ((h _ _).2 hab)))) a.2, inv_fun := Ξ» a, quotient.lift_on a (Ξ» a, (⟨⟦a.1⟧, (hpβ‚‚ _).1 a.2⟩ : {x // pβ‚‚ x})) (Ξ» a b hab, subtype.ext_val (quotient.sound ((h _ _).1 hab))), left_inv := Ξ» ⟨a, ha⟩, quotient.induction_on a (Ξ» a ha, rfl) ha, right_inv := Ξ» a, quotient.induction_on a (Ξ» ⟨a, ha⟩, rfl) } section swap variable [decidable_eq Ξ±] /-- A helper function for `equiv.swap`. -/ def swap_core (a b r : Ξ±) : Ξ± := if r = a then b else if r = b then a else r theorem swap_core_self (r a : Ξ±) : swap_core a a r = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_swap_core (r a b : Ξ±) : swap_core a b (swap_core a b r) = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_comm (r a b : Ξ±) : swap_core a b r = swap_core b a r := by { unfold swap_core, split_ifs; cc } /-- `swap a b` is the permutation that swaps `a` and `b` and leaves other values as is. -/ def swap (a b : Ξ±) : perm Ξ± := ⟨swap_core a b, swap_core a b, Ξ»r, swap_core_swap_core r a b, Ξ»r, swap_core_swap_core r a b⟩ @[simp] theorem swap_self (a : Ξ±) : swap a a = equiv.refl _ := ext $ Ξ» r, swap_core_self r a theorem swap_comm (a b : Ξ±) : swap a b = swap b a := ext $ Ξ» r, swap_core_comm r _ _ theorem swap_apply_def (a b x : Ξ±) : swap a b x = if x = a then b else if x = b then a else x := rfl @[simp] theorem swap_apply_left (a b : Ξ±) : swap a b a = b := if_pos rfl @[simp] theorem swap_apply_right (a b : Ξ±) : swap a b b = a := by { by_cases h : b = a; simp [swap_apply_def, h], } theorem swap_apply_of_ne_of_ne {a b x : Ξ±} : x β‰  a β†’ x β‰  b β†’ swap a b x = x := by simp [swap_apply_def] {contextual := tt} @[simp] theorem swap_swap (a b : Ξ±) : (swap a b).trans (swap a b) = equiv.refl _ := ext $ Ξ» x, swap_core_swap_core _ _ _ @[simp] lemma symm_swap (a b : Ξ±) : (swap a b).symm = swap a b := rfl @[simp] lemma swap_eq_refl_iff {x y : Ξ±} : swap x y = equiv.refl _ ↔ x = y := begin refine ⟨λ h, (equiv.refl _).injective _, Ξ» h, h β–Έ (swap_self _)⟩, rw [←h, swap_apply_left, h, refl_apply] end theorem swap_comp_apply {a b x : Ξ±} (Ο€ : perm Ξ±) : Ο€.trans (swap a b) x = if Ο€ x = a then b else if Ο€ x = b then a else Ο€ x := by { cases Ο€, refl } lemma swap_eq_update (i j : Ξ±) : ⇑(equiv.swap i j) = update (update id j i) i j := funext $ Ξ» x, by rw [update_apply _ i j, update_apply _ j i, equiv.swap_apply_def, id.def] lemma comp_swap_eq_update (i j : Ξ±) (f : Ξ± β†’ Ξ²) : f ∘ equiv.swap i j = update (update f j (f i)) i (f j) := by rw [swap_eq_update, comp_update, comp_update, comp.right_id] @[simp] lemma symm_trans_swap_trans [decidable_eq Ξ²] (a b : Ξ±) (e : Ξ± ≃ Ξ²) : (e.symm.trans (swap a b)).trans e = swap (e a) (e b) := equiv.ext (Ξ» x, begin have : βˆ€ a, e.symm x = a ↔ x = e a := Ξ» a, by { rw @eq_comm _ (e.symm x), split; intros; simp * at * }, simp [swap_apply_def, this], split_ifs; simp end) @[simp] lemma trans_swap_trans_symm [decidable_eq Ξ²] (a b : Ξ²) (e : Ξ± ≃ Ξ²) : (e.trans (swap a b)).trans e.symm = swap (e.symm a) (e.symm b) := symm_trans_swap_trans a b e.symm @[simp] lemma swap_apply_self (i j a : Ξ±) : swap i j (swap i j a) = a := by rw [← equiv.trans_apply, equiv.swap_swap, equiv.refl_apply] /-- A function is invariant to a swap if it is equal at both elements -/ lemma apply_swap_eq_self {v : Ξ± β†’ Ξ²} {i j : Ξ±} (hv : v i = v j) (k : Ξ±) : v (swap i j k) = v k := begin by_cases hi : k = i, { rw [hi, swap_apply_left, hv] }, by_cases hj : k = j, { rw [hj, swap_apply_right, hv] }, rw swap_apply_of_ne_of_ne hi hj, end lemma swap_apply_eq_iff {x y z w : Ξ±} : swap x y z = w ↔ z = swap x y w := by rw [apply_eq_iff_eq_symm_apply, symm_swap] lemma swap_apply_ne_self_iff {a b x : Ξ±} : swap a b x β‰  x ↔ a β‰  b ∧ (x = a ∨ x = b) := begin by_cases hab : a = b, { simp [hab] }, by_cases hax : x = a, { simp [hax, eq_comm] }, by_cases hbx : x = b, { simp [hbx] }, simp [hab, hax, hbx, swap_apply_of_ne_of_ne] end namespace perm @[simp] lemma sum_congr_swap_refl {Ξ± Ξ² : Sort*} [decidable_eq Ξ±] [decidable_eq Ξ²] (i j : Ξ±) : equiv.perm.sum_congr (equiv.swap i j) (equiv.refl Ξ²) = equiv.swap (sum.inl i) (sum.inl j) := begin ext x, cases x, { simp [sum.map, swap_apply_def], split_ifs; refl}, { simp [sum.map, swap_apply_of_ne_of_ne] }, end @[simp] lemma sum_congr_refl_swap {Ξ± Ξ² : Sort*} [decidable_eq Ξ±] [decidable_eq Ξ²] (i j : Ξ²) : equiv.perm.sum_congr (equiv.refl Ξ±) (equiv.swap i j) = equiv.swap (sum.inr i) (sum.inr j) := begin ext x, cases x, { simp [sum.map, swap_apply_of_ne_of_ne] }, { simp [sum.map, swap_apply_def], split_ifs; refl}, end end perm /-- Augment an equivalence with a prescribed mapping `f a = b` -/ def set_value (f : Ξ± ≃ Ξ²) (a : Ξ±) (b : Ξ²) : Ξ± ≃ Ξ² := (swap a (f.symm b)).trans f @[simp] theorem set_value_eq (f : Ξ± ≃ Ξ²) (a : Ξ±) (b : Ξ²) : set_value f a b a = b := by { dsimp [set_value], simp [swap_apply_left] } end swap end equiv lemma plift.eq_up_iff_down_eq {x : plift Ξ±} {y : Ξ±} : x = plift.up y ↔ x.down = y := equiv.plift.eq_symm_apply lemma function.injective.map_swap {Ξ± Ξ² : Type*} [decidable_eq Ξ±] [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (hf : function.injective f) (x y z : Ξ±) : f (equiv.swap x y z) = equiv.swap (f x) (f y) (f z) := begin conv_rhs { rw equiv.swap_apply_def }, split_ifs with h₁ hβ‚‚, { rw [hf h₁, equiv.swap_apply_left] }, { rw [hf hβ‚‚, equiv.swap_apply_right] }, { rw [equiv.swap_apply_of_ne_of_ne (mt (congr_arg f) h₁) (mt (congr_arg f) hβ‚‚)] } end namespace equiv protected lemma exists_unique_congr {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} (f : Ξ± ≃ Ξ²) (h : βˆ€{x}, p x ↔ q (f x)) : (βˆƒ! x, p x) ↔ βˆƒ! y, q y := begin split, { rintro ⟨a, ha₁, haβ‚‚βŸ©, exact ⟨f a, h.1 ha₁, Ξ» b hb, f.symm_apply_eq.1 (haβ‚‚ (f.symm b) (h.2 (by simpa using hb)))⟩ }, { rintro ⟨b, hb₁, hbβ‚‚βŸ©, exact ⟨f.symm b, h.2 (by simpa using hb₁), Ξ» y hy, (eq_symm_apply f).2 (hbβ‚‚ _ (h.1 hy))⟩ } end protected lemma exists_unique_congr_left' {p : Ξ± β†’ Prop} (f : Ξ± ≃ Ξ²) : (βˆƒ! x, p x) ↔ (βˆƒ! y, p (f.symm y)) := equiv.exists_unique_congr f (Ξ»x, by simp) protected lemma exists_unique_congr_left {p : Ξ² β†’ Prop} (f : Ξ± ≃ Ξ²) : (βˆƒ! x, p (f x)) ↔ (βˆƒ! y, p y) := (equiv.exists_unique_congr_left' f.symm).symm protected lemma forall_congr {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} (f : Ξ± ≃ Ξ²) (h : βˆ€{x}, p x ↔ q (f x)) : (βˆ€x, p x) ↔ (βˆ€y, q y) := begin split; intros hβ‚‚ x, { rw [←f.right_inv x], apply h.mp, apply hβ‚‚ }, apply h.mpr, apply hβ‚‚ end protected lemma forall_congr' {p : Ξ± β†’ Prop} {q : Ξ² β†’ Prop} (f : Ξ± ≃ Ξ²) (h : βˆ€{x}, p (f.symm x) ↔ q x) : (βˆ€x, p x) ↔ (βˆ€y, q y) := (equiv.forall_congr f.symm (Ξ» x, h.symm)).symm -- We next build some higher arity versions of `equiv.forall_congr`. -- Although they appear to just be repeated applications of `equiv.forall_congr`, -- unification of metavariables works better with these versions. -- In particular, they are necessary in `equiv_rw`. -- (Stopping at ternary functions seems reasonable: at least in 1-categorical mathematics, -- it's rare to have axioms involving more than 3 elements at once.) universes ua1 ua2 ub1 ub2 ug1 ug2 variables {α₁ : Sort ua1} {Ξ±β‚‚ : Sort ua2} {β₁ : Sort ub1} {Ξ²β‚‚ : Sort ub2} {γ₁ : Sort ug1} {Ξ³β‚‚ : Sort ug2} protected lemma forallβ‚‚_congr {p : α₁ β†’ β₁ β†’ Prop} {q : Ξ±β‚‚ β†’ Ξ²β‚‚ β†’ Prop} (eΞ± : α₁ ≃ Ξ±β‚‚) (eΞ² : β₁ ≃ Ξ²β‚‚) (h : βˆ€{x y}, p x y ↔ q (eΞ± x) (eΞ² y)) : (βˆ€x y, p x y) ↔ (βˆ€x y, q x y) := begin apply equiv.forall_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forallβ‚‚_congr' {p : α₁ β†’ β₁ β†’ Prop} {q : Ξ±β‚‚ β†’ Ξ²β‚‚ β†’ Prop} (eΞ± : α₁ ≃ Ξ±β‚‚) (eΞ² : β₁ ≃ Ξ²β‚‚) (h : βˆ€{x y}, p (eΞ±.symm x) (eΞ².symm y) ↔ q x y) : (βˆ€x y, p x y) ↔ (βˆ€x y, q x y) := (equiv.forallβ‚‚_congr eΞ±.symm eΞ².symm (Ξ» x y, h.symm)).symm protected lemma forall₃_congr {p : α₁ β†’ β₁ β†’ γ₁ β†’ Prop} {q : Ξ±β‚‚ β†’ Ξ²β‚‚ β†’ Ξ³β‚‚ β†’ Prop} (eΞ± : α₁ ≃ Ξ±β‚‚) (eΞ² : β₁ ≃ Ξ²β‚‚) (eΞ³ : γ₁ ≃ Ξ³β‚‚) (h : βˆ€{x y z}, p x y z ↔ q (eΞ± x) (eΞ² y) (eΞ³ z)) : (βˆ€x y z, p x y z) ↔ (βˆ€x y z, q x y z) := begin apply equiv.forallβ‚‚_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forall₃_congr' {p : α₁ β†’ β₁ β†’ γ₁ β†’ Prop} {q : Ξ±β‚‚ β†’ Ξ²β‚‚ β†’ Ξ³β‚‚ β†’ Prop} (eΞ± : α₁ ≃ Ξ±β‚‚) (eΞ² : β₁ ≃ Ξ²β‚‚) (eΞ³ : γ₁ ≃ Ξ³β‚‚) (h : βˆ€{x y z}, p (eΞ±.symm x) (eΞ².symm y) (eΞ³.symm z) ↔ q x y z) : (βˆ€x y z, p x y z) ↔ (βˆ€x y z, q x y z) := (equiv.forall₃_congr eΞ±.symm eΞ².symm eΞ³.symm (Ξ» x y z, h.symm)).symm protected lemma forall_congr_left' {p : Ξ± β†’ Prop} (f : Ξ± ≃ Ξ²) : (βˆ€x, p x) ↔ (βˆ€y, p (f.symm y)) := equiv.forall_congr f (Ξ»x, by simp) protected lemma forall_congr_left {p : Ξ² β†’ Prop} (f : Ξ± ≃ Ξ²) : (βˆ€x, p (f x)) ↔ (βˆ€y, p y) := (equiv.forall_congr_left' f.symm).symm protected lemma exists_congr_left {Ξ± Ξ²} (f : Ξ± ≃ Ξ²) {p : Ξ± β†’ Prop} : (βˆƒ a, p a) ↔ (βˆƒ b, p (f.symm b)) := ⟨λ ⟨a, h⟩, ⟨f a, by simpa using h⟩, Ξ» ⟨b, h⟩, ⟨_, h⟩⟩ protected lemma set_forall_iff {Ξ± Ξ²} (e : Ξ± ≃ Ξ²) {p : set Ξ± β†’ Prop} : (βˆ€ a, p a) ↔ (βˆ€ a, p (e ⁻¹' a)) := by simpa [equiv.image_eq_preimage] using (equiv.set.congr e).forall_congr_left' protected lemma preimage_sUnion {Ξ± Ξ²} (f : Ξ± ≃ Ξ²) {s : set (set Ξ²)} : f ⁻¹' (⋃₀ s) = ⋃₀ (_root_.set.image f ⁻¹' s) := by { ext x, simp [(equiv.set.congr f).symm.exists_congr_left] } section variables (P : Ξ± β†’ Sort w) (e : Ξ± ≃ Ξ²) /-- Transport dependent functions through an equivalence of the base space. -/ @[simps] def Pi_congr_left' : (Ξ  a, P a) ≃ (Ξ  b, P (e.symm b)) := { to_fun := Ξ» f x, f (e.symm x), inv_fun := Ξ» f x, begin rw [← e.symm_apply_apply x], exact f (e x) end, left_inv := Ξ» f, funext $ Ξ» x, eq_of_heq ((eq_rec_heq _ _).trans (by { dsimp, rw e.symm_apply_apply })), right_inv := Ξ» f, funext $ Ξ» x, eq_of_heq ((eq_rec_heq _ _).trans (by { rw e.apply_symm_apply })) } end section variables (P : Ξ² β†’ Sort w) (e : Ξ± ≃ Ξ²) /-- Transporting dependent functions through an equivalence of the base, expressed as a "simplification". -/ def Pi_congr_left : (Ξ  a, P (e a)) ≃ (Ξ  b, P b) := (Pi_congr_left' P e.symm).symm end section variables {W : Ξ± β†’ Sort w} {Z : Ξ² β†’ Sort z} (h₁ : Ξ± ≃ Ξ²) (hβ‚‚ : Ξ  a : Ξ±, (W a ≃ Z (h₁ a))) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibers. -/ def Pi_congr : (Ξ  a, W a) ≃ (Ξ  b, Z b) := (equiv.Pi_congr_right hβ‚‚).trans (equiv.Pi_congr_left _ h₁) end section variables {W : Ξ± β†’ Sort w} {Z : Ξ² β†’ Sort z} (h₁ : Ξ± ≃ Ξ²) (hβ‚‚ : Ξ  b : Ξ², (W (h₁.symm b) ≃ Z b)) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibres. -/ def Pi_congr' : (Ξ  a, W a) ≃ (Ξ  b, Z b) := (Pi_congr h₁.symm (Ξ» b, (hβ‚‚ b).symm)).symm end end equiv lemma function.injective.swap_apply [decidable_eq Ξ±] [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (hf : function.injective f) (x y z : Ξ±) : equiv.swap (f x) (f y) (f z) = f (equiv.swap x y z) := begin by_cases hx : z = x, by simp [hx], by_cases hy : z = y, by simp [hy], rw [equiv.swap_apply_of_ne_of_ne hx hy, equiv.swap_apply_of_ne_of_ne (hf.ne hx) (hf.ne hy)] end lemma function.injective.swap_comp [decidable_eq Ξ±] [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (hf : function.injective f) (x y : Ξ±) : equiv.swap (f x) (f y) ∘ f = f ∘ equiv.swap x y := funext $ Ξ» z, hf.swap_apply _ _ _ instance {Ξ±} [subsingleton Ξ±] : subsingleton (ulift Ξ±) := equiv.ulift.subsingleton instance {Ξ±} [subsingleton Ξ±] : subsingleton (plift Ξ±) := equiv.plift.subsingleton instance {Ξ±} [unique Ξ±] : unique (ulift Ξ±) := equiv.ulift.unique instance {Ξ±} [unique Ξ±] : unique (plift Ξ±) := equiv.plift.unique instance {Ξ±} [decidable_eq Ξ±] : decidable_eq (ulift Ξ±) := equiv.ulift.decidable_eq instance {Ξ±} [decidable_eq Ξ±] : decidable_eq (plift Ξ±) := equiv.plift.decidable_eq /-- If both `Ξ±` and `Ξ²` are singletons, then `Ξ± ≃ Ξ²`. -/ def equiv_of_unique_of_unique [unique Ξ±] [unique Ξ²] : Ξ± ≃ Ξ² := { to_fun := Ξ» _, default Ξ², inv_fun := Ξ» _, default Ξ±, left_inv := Ξ» _, subsingleton.elim _ _, right_inv := Ξ» _, subsingleton.elim _ _ } /-- If `Ξ±` is a singleton, then it is equivalent to any `punit`. -/ def equiv_punit_of_unique [unique Ξ±] : Ξ± ≃ punit.{v} := equiv_of_unique_of_unique /-- If `Ξ±` is a subsingleton, then it is equivalent to `Ξ± Γ— Ξ±`. -/ def subsingleton_prod_self_equiv {Ξ± : Type*} [subsingleton Ξ±] : Ξ± Γ— Ξ± ≃ Ξ± := { to_fun := Ξ» p, p.1, inv_fun := Ξ» a, (a, a), left_inv := Ξ» p, subsingleton.elim _ _, right_inv := Ξ» p, subsingleton.elim _ _, } /-- To give an equivalence between two subsingleton types, it is sufficient to give any two functions between them. -/ def equiv_of_subsingleton_of_subsingleton [subsingleton Ξ±] [subsingleton Ξ²] (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ Ξ±) : Ξ± ≃ Ξ² := { to_fun := f, inv_fun := g, left_inv := Ξ» _, subsingleton.elim _ _, right_inv := Ξ» _, subsingleton.elim _ _ } /-- A nonempty subsingleton type is (noncomputably) equivalent to `punit`. -/ noncomputable def equiv.punit_of_nonempty_of_subsingleton {Ξ± : Sort*} [h : nonempty Ξ±] [subsingleton Ξ±] : Ξ± ≃ punit.{v} := equiv_of_subsingleton_of_subsingleton (Ξ» _, punit.star) (Ξ» _, h.some) /-- `unique (unique Ξ±)` is equivalent to `unique Ξ±`. -/ def unique_unique_equiv : unique (unique Ξ±) ≃ unique Ξ± := equiv_of_subsingleton_of_subsingleton (Ξ» h, h.default) (Ξ» h, { default := h, uniq := Ξ» _, subsingleton.elim _ _ }) namespace quot /-- An equivalence `e : Ξ± ≃ Ξ²` generates an equivalence between quotient spaces, if `ra a₁ aβ‚‚ ↔ rb (e a₁) (e aβ‚‚). -/ protected def congr {ra : Ξ± β†’ Ξ± β†’ Prop} {rb : Ξ² β†’ Ξ² β†’ Prop} (e : Ξ± ≃ Ξ²) (eq : βˆ€a₁ aβ‚‚, ra a₁ aβ‚‚ ↔ rb (e a₁) (e aβ‚‚)) : quot ra ≃ quot rb := { to_fun := quot.map e (assume a₁ aβ‚‚, (eq a₁ aβ‚‚).1), inv_fun := quot.map e.symm (assume b₁ bβ‚‚ h, (eq (e.symm b₁) (e.symm bβ‚‚)).2 ((e.apply_symm_apply b₁).symm β–Έ (e.apply_symm_apply bβ‚‚).symm β–Έ h)), left_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.symm_apply_apply] }, right_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.apply_symm_apply] } } @[simp] lemma congr_mk {ra : Ξ± β†’ Ξ± β†’ Prop} {rb : Ξ² β†’ Ξ² β†’ Prop} (e : Ξ± ≃ Ξ²) (eq : βˆ€ (a₁ aβ‚‚ : Ξ±), ra a₁ aβ‚‚ ↔ rb (e a₁) (e aβ‚‚)) (a : Ξ±) : quot.congr e eq (quot.mk ra a) = quot.mk rb (e a) := rfl /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : Ξ± β†’ Ξ± β†’ Prop} (eq : βˆ€a₁ aβ‚‚, r a₁ aβ‚‚ ↔ r' a₁ aβ‚‚) : quot r ≃ quot r' := quot.congr (equiv.refl Ξ±) eq /-- An equivalence `e : Ξ± ≃ Ξ²` generates an equivalence between the quotient space of `Ξ±` by a relation `ra` and the quotient space of `Ξ²` by the image of this relation under `e`. -/ protected def congr_left {r : Ξ± β†’ Ξ± β†’ Prop} (e : Ξ± ≃ Ξ²) : quot r ≃ quot (Ξ» b b', r (e.symm b) (e.symm b')) := @quot.congr Ξ± Ξ² r (Ξ» b b', r (e.symm b) (e.symm b')) e (Ξ» a₁ aβ‚‚, by simp only [e.symm_apply_apply]) end quot namespace quotient /-- An equivalence `e : Ξ± ≃ Ξ²` generates an equivalence between quotient spaces, if `ra a₁ aβ‚‚ ↔ rb (e a₁) (e aβ‚‚). -/ protected def congr {ra : setoid Ξ±} {rb : setoid Ξ²} (e : Ξ± ≃ Ξ²) (eq : βˆ€a₁ aβ‚‚, @setoid.r Ξ± ra a₁ aβ‚‚ ↔ @setoid.r Ξ² rb (e a₁) (e aβ‚‚)) : quotient ra ≃ quotient rb := quot.congr e eq @[simp] lemma congr_mk {ra : setoid Ξ±} {rb : setoid Ξ²} (e : Ξ± ≃ Ξ²) (eq : βˆ€ (a₁ aβ‚‚ : Ξ±), setoid.r a₁ aβ‚‚ ↔ setoid.r (e a₁) (e aβ‚‚)) (a : Ξ±): quotient.congr e eq (quotient.mk a) = quotient.mk (e a) := rfl /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : setoid Ξ±} (eq : βˆ€a₁ aβ‚‚, @setoid.r Ξ± r a₁ aβ‚‚ ↔ @setoid.r Ξ± r' a₁ aβ‚‚) : quotient r ≃ quotient r' := quot.congr_right eq end quotient /-- If a function is a bijection between two sets `s` and `t`, then it induces an equivalence between the the types `β†₯s` and ``β†₯t`. -/ noncomputable def set.bij_on.equiv {Ξ± : Type*} {Ξ² : Type*} {s : set Ξ±} {t : set Ξ²} (f : Ξ± β†’ Ξ²) (h : set.bij_on f s t) : s ≃ t := equiv.of_bijective _ h.bijective namespace function lemma update_comp_equiv {Ξ± Ξ² Ξ±' : Sort*} [decidable_eq Ξ±'] [decidable_eq Ξ±] (f : Ξ± β†’ Ξ²) (g : Ξ±' ≃ Ξ±) (a : Ξ±) (v : Ξ²) : update f a v ∘ g = update (f ∘ g) (g.symm a) v := by rw [← update_comp_eq_of_injective _ g.injective, g.apply_symm_apply] lemma update_apply_equiv_apply {Ξ± Ξ² Ξ±' : Sort*} [decidable_eq Ξ±'] [decidable_eq Ξ±] (f : Ξ± β†’ Ξ²) (g : Ξ±' ≃ Ξ±) (a : Ξ±) (v : Ξ²) (a' : Ξ±') : update f a v (g a') = update (f ∘ g) (g.symm a) v a' := congr_fun (update_comp_equiv f g a v) a' end function /-- The composition of an updated function with an equiv on a subset can be expressed as an updated function. -/ lemma dite_comp_equiv_update {Ξ± : Type*} {Ξ² : Sort*} {Ξ³ : Sort*} {s : set Ξ±} (e : Ξ² ≃ s) (v : Ξ² β†’ Ξ³) (w : Ξ± β†’ Ξ³) (j : Ξ²) (x : Ξ³) [decidable_eq Ξ²] [decidable_eq Ξ±] [βˆ€ j, decidable (j ∈ s)] : (Ξ» (i : Ξ±), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) = function.update (Ξ» (i : Ξ±), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x := begin ext i, by_cases h : i ∈ s, { rw [dif_pos h, function.update_apply_equiv_apply, equiv.symm_symm, function.comp, function.update_apply, function.update_apply, dif_pos h], have h_coe : (⟨i, h⟩ : s) = e j ↔ i = e j := subtype.ext_iff.trans (by rw subtype.coe_mk), simp_rw h_coe, congr, }, { have : i β‰  e j, by { contrapose! h, have : (e j : Ξ±) ∈ s := (e j).2, rwa ← h at this }, simp [h, this] } end
46d67c2f28a7a74de9004bda03c10d4f07013b0a
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/field_theory/chevalley_warning.lean
b4e592a7e799c754b57e29658025c228836f8084
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
8,463
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.mv_polynomial import field_theory.finite /-! # The Chevalley–Warning theorem This file contains a proof of the Chevalley–Warning theorem. Throughout most of this file, `K` denotes a finite field and `q` is notation for the cardinality of `K`. ## Main results 1. Let `f` be a multivariate polynomial in finitely many variables (`X s`, `s : Οƒ`) such that the total degree of `f` is less than `(q-1)` times the cardinality of `Οƒ`. Then the evaluation of `f` on all points of `Οƒ β†’ K` (aka `K^Οƒ`) sums to `0`. (`sum_mv_polynomial_eq_zero`) 2. The Chevalley–Warning theorem (`char_dvd_card_solutions`). Let `f i` be a finite family of multivariate polynomials in finitely many variables (`X s`, `s : Οƒ`) such that the sum of the total degrees of the `f i` is less than the cardinality of `Οƒ`. Then the number of common solutions of the `f i` is divisible by the characteristic of `K`. ## Notation - `K` is a finite field - `q` is notation for the cardinality of `K` - `Οƒ` is the indexing type for the variables of a multivariate polynomial ring over `K` -/ universes u v open_locale big_operators section finite_field open mv_polynomial function finset finite_field variables {K : Type*} {Οƒ : Type*} [fintype K] [field K] [fintype Οƒ] local notation `q` := fintype.card K lemma mv_polynomial.sum_mv_polynomial_eq_zero [decidable_eq Οƒ] (f : mv_polynomial Οƒ K) (h : f.total_degree < (q - 1) * fintype.card Οƒ) : (βˆ‘ x, f.eval x) = 0 := begin haveI : decidable_eq K := classical.dec_eq K, calc (βˆ‘ x, f.eval x) = βˆ‘ x : Οƒ β†’ K, βˆ‘ d in f.support, f.coeff d * ∏ i, x i ^ d i : by simp only [eval_eq'] ... = βˆ‘ d in f.support, βˆ‘ x : Οƒ β†’ K, f.coeff d * ∏ i, x i ^ d i : sum_comm ... = 0 : sum_eq_zero _, intros d hd, obtain ⟨i, hi⟩ : βˆƒ i, d i < q - 1, from f.exists_degree_lt (q - 1) h hd, calc (βˆ‘ x : Οƒ β†’ K, f.coeff d * ∏ i, x i ^ d i) = f.coeff d * (βˆ‘ x : Οƒ β†’ K, ∏ i, x i ^ d i) : mul_sum.symm ... = 0 : (mul_eq_zero.mpr ∘ or.inr) _, calc (βˆ‘ x : Οƒ β†’ K, ∏ i, x i ^ d i) = βˆ‘ (xβ‚€ : {j // j β‰  i} β†’ K) (x : {x : Οƒ β†’ K // x ∘ coe = xβ‚€}), ∏ j, (x : Οƒ β†’ K) j ^ d j : (fintype.sum_fiberwise _ _).symm ... = 0 : fintype.sum_eq_zero _ _, intros xβ‚€, let e : K ≃ {x // x ∘ coe = xβ‚€} := (equiv.subtype_equiv_codomain _).symm, calc (βˆ‘ x : {x : Οƒ β†’ K // x ∘ coe = xβ‚€}, ∏ j, (x : Οƒ β†’ K) j ^ d j) = βˆ‘ a : K, ∏ j : Οƒ, (e a : Οƒ β†’ K) j ^ d j : (finset.sum_equiv e _).symm ... = βˆ‘ a : K, (∏ j, xβ‚€ j ^ d j) * a ^ d i : fintype.sum_congr _ _ _ ... = (∏ j, xβ‚€ j ^ d j) * βˆ‘ a : K, a ^ d i : by rw mul_sum ... = 0 : by rw [sum_pow_lt_card_sub_one _ hi, mul_zero], intros a, let e' : {j // j = i} βŠ• {j // j β‰  i} ≃ Οƒ := equiv.sum_compl _, letI : unique {j // j = i} := { default := ⟨i, rfl⟩, uniq := Ξ» ⟨j, h⟩, subtype.val_injective h }, calc (∏ j : Οƒ, (e a : Οƒ β†’ K) j ^ d j) = (e a : Οƒ β†’ K) i ^ d i * (∏ (j : {j // j β‰  i}), (e a : Οƒ β†’ K) j ^ d j) : by { rw [← finset.prod_equiv e', fintype.prod_sum_type, univ_unique, prod_singleton], refl } ... = a ^ d i * (∏ (j : {j // j β‰  i}), (e a : Οƒ β†’ K) j ^ d j) : by rw equiv.subtype_equiv_codomain_symm_apply_eq ... = a ^ d i * (∏ j, xβ‚€ j ^ d j) : congr_arg _ (fintype.prod_congr _ _ _) -- see below ... = (∏ j, xβ‚€ j ^ d j) * a ^ d i : mul_comm _ _, { -- the remaining step of the calculation above rintros ⟨j, hj⟩, show (e a : Οƒ β†’ K) j ^ d j = xβ‚€ ⟨j, hj⟩ ^ d j, rw equiv.subtype_equiv_codomain_symm_apply_ne, } end variables [decidable_eq K] [decidable_eq Οƒ] /-- The Chevalley–Warning theorem. Let `(f i)` be a finite family of multivariate polynomials in finitely many variables (`X s`, `s : Οƒ`) over a finite field of characteristic `p`. Assume that the sum of the total degrees of the `f i` is less than the cardinality of `Οƒ`. Then the number of common solutions of the `f i` is divisible by `p`. -/ theorem char_dvd_card_solutions_family (p : β„•) [char_p K p] {ΞΉ : Type*} {s : finset ΞΉ} {f : ΞΉ β†’ mv_polynomial Οƒ K} (h : (βˆ‘ i in s, (f i).total_degree) < fintype.card Οƒ) : p ∣ fintype.card {x : Οƒ β†’ K // βˆ€ i ∈ s, (f i).eval x = 0} := begin have hq : 0 < q - 1, { rw [← card_units, fintype.card_pos_iff], exact ⟨1⟩ }, let S : finset (Οƒ β†’ K) := { x ∈ univ | βˆ€ i ∈ s, (f i).eval x = 0 }, have hS : βˆ€ (x : Οƒ β†’ K), x ∈ S ↔ βˆ€ (i : ΞΉ), i ∈ s β†’ eval x (f i) = 0, { intros x, simp only [S, true_and, sep_def, mem_filter, mem_univ], }, /- The polynomial `F = ∏ i in s, (1 - (f i)^(q - 1))` has the nice property that it takes the value `1` on elements of `{x : Οƒ β†’ K // βˆ€ i ∈ s, (f i).eval x = 0}` while it is `0` outside that locus. Hence the sum of its values is equal to the cardinality of `{x : Οƒ β†’ K // βˆ€ i ∈ s, (f i).eval x = 0}` modulo `p`. -/ let F : mv_polynomial Οƒ K := ∏ i in s, (1 - (f i)^(q - 1)), have hF : βˆ€ x, F.eval x = if x ∈ S then 1 else 0, { intro x, calc F.eval x = ∏ i in s, (1 - f i ^ (q - 1)).eval x : eval_prod s _ x ... = if x ∈ S then 1 else 0 : _, simp only [eval_sub, eval_pow, eval_one], split_ifs with hx hx, { apply finset.prod_eq_one, intros i hi, rw hS at hx, rw [hx i hi, zero_pow hq, sub_zero], }, { obtain ⟨i, hi, hx⟩ : βˆƒ (i : ΞΉ), i ∈ s ∧ (f i).eval x β‰  0, { simpa only [hS, classical.not_forall, classical.not_imp] using hx }, apply finset.prod_eq_zero hi, rw [pow_card_sub_one_eq_one ((f i).eval x) hx, sub_self], } }, -- In particular, we can now show: have key : βˆ‘ x, F.eval x = fintype.card {x : Οƒ β†’ K // βˆ€ i ∈ s, (f i).eval x = 0}, rw [fintype.card_of_subtype S hS, card_eq_sum_ones, sum_nat_cast, nat.cast_one, ← fintype.sum_extend_by_zero S, sum_congr rfl (Ξ» x hx, hF x)], -- With these preparations under our belt, we will approach the main goal. show p ∣ fintype.card {x // βˆ€ (i : ΞΉ), i ∈ s β†’ (f i).eval x = 0}, rw [← char_p.cast_eq_zero_iff K, ← key], show βˆ‘ x, F.eval x = 0, -- We are now ready to apply the main machine, proven before. apply F.sum_mv_polynomial_eq_zero, -- It remains to verify the crucial assumption of this machine show F.total_degree < (q - 1) * fintype.card Οƒ, calc F.total_degree ≀ βˆ‘ i in s, (1 - (f i)^(q - 1)).total_degree : total_degree_finset_prod s _ ... ≀ βˆ‘ i in s, (q - 1) * (f i).total_degree : sum_le_sum $ Ξ» i hi, _ -- see ↓ ... = (q - 1) * (βˆ‘ i in s, (f i).total_degree) : mul_sum.symm ... < (q - 1) * (fintype.card Οƒ) : by rwa mul_lt_mul_left hq, -- Now we prove the remaining step from the preceding calculation show (1 - f i ^ (q - 1)).total_degree ≀ (q - 1) * (f i).total_degree, calc (1 - f i ^ (q - 1)).total_degree ≀ max (1 : mv_polynomial Οƒ K).total_degree (f i ^ (q - 1)).total_degree : total_degree_sub _ _ ... ≀ (f i ^ (q - 1)).total_degree : by simp only [max_eq_right, nat.zero_le, total_degree_one] ... ≀ (q - 1) * (f i).total_degree : total_degree_pow _ _ end /-- The Chevalley–Warning theorem. Let `f` be a multivariate polynomial in finitely many variables (`X s`, `s : Οƒ`) over a finite field of characteristic `p`. Assume that the total degree of `f` is less than the cardinality of `Οƒ`. Then the number of solutions of `f` is divisible by `p`. See `char_dvd_card_solutions_family` for a version that takes a family of polynomials `f i`. -/ theorem char_dvd_card_solutions (p : β„•) [char_p K p] {f : mv_polynomial Οƒ K} (h : f.total_degree < fintype.card Οƒ) : p ∣ fintype.card {x : Οƒ β†’ K // f.eval x = 0} := begin let F : unit β†’ mv_polynomial Οƒ K := Ξ» _, f, have : βˆ‘ i : unit, (F i).total_degree < fintype.card Οƒ, { simpa only [fintype.univ_punit, sum_singleton] using h, }, have key := char_dvd_card_solutions_family p this, simp only [F, fintype.univ_punit, forall_eq, mem_singleton] at key, convert key, end end finite_field
bfe563ec758ee0ca1fe8f6c6b64030044d731f41
4fa161becb8ce7378a709f5992a594764699e268
/src/algebra/ordered_group.lean
7e83220e4a2283ed4a6fd1aa5c310f3fff44b05f
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
64,035
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes HΓΆlzl -/ import algebra.group.units import algebra.group.with_one import algebra.group.type_tags import order.bounded_lattice set_option old_structure_cmd true set_option default_priority 100 -- see Note [default priority] /-! # Ordered monoids and groups This file develops the basics of ordered monoids and groups. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. -/ universe u variable {Ξ± : Type u} /-- An ordered commutative monoid is a commutative monoid with a partial order such that * `a ≀ b β†’ c * a ≀ c * b` (multiplication is monotone) * `a * b < a * c β†’ b < c`. -/ @[protect_proj, ancestor comm_monoid partial_order] class ordered_comm_monoid (Ξ± : Type*) extends comm_monoid Ξ±, partial_order Ξ± := (mul_le_mul_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c * a ≀ c * b) (lt_of_mul_lt_mul_left : βˆ€ a b c : Ξ±, a * b < a * c β†’ b < c) /-- An ordered (additive) commutative monoid is a commutative monoid with a partial order such that * `a ≀ b β†’ c + a ≀ c + b` (addition is monotone) * `a + b < a + c β†’ b < c`. -/ @[protect_proj, ancestor add_comm_monoid partial_order] class ordered_add_comm_monoid (Ξ± : Type*) extends add_comm_monoid Ξ±, partial_order Ξ± := (add_le_add_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c + a ≀ c + b) (lt_of_add_lt_add_left : βˆ€ a b c : Ξ±, a + b < a + c β†’ b < c) attribute [to_additive ordered_add_comm_monoid] ordered_comm_monoid section ordered_comm_monoid variables [ordered_comm_monoid Ξ±] {a b c d : Ξ±} @[to_additive] lemma mul_le_mul_left' (h : a ≀ b) : c * a ≀ c * b := ordered_comm_monoid.mul_le_mul_left a b h c @[to_additive] lemma mul_le_mul_right' (h : a ≀ b) : a * c ≀ b * c := mul_comm c a β–Έ mul_comm c b β–Έ mul_le_mul_left' h @[to_additive] lemma lt_of_mul_lt_mul_left' : a * b < a * c β†’ b < c := ordered_comm_monoid.lt_of_mul_lt_mul_left a b c @[to_additive] lemma mul_le_mul' (h₁ : a ≀ b) (hβ‚‚ : c ≀ d) : a * c ≀ b * d := le_trans (mul_le_mul_right' h₁) (mul_le_mul_left' hβ‚‚) @[to_additive le_add_of_nonneg_right'] lemma le_mul_of_one_le_right'' (h : 1 ≀ b) : a ≀ a * b := have a * 1 ≀ a * b, from mul_le_mul_left' h, by rwa mul_one at this @[to_additive le_add_of_nonneg_left'] lemma le_mul_of_one_le_left'' (h : 1 ≀ b) : a ≀ b * a := have 1 * a ≀ b * a, from mul_le_mul_right' h, by rwa one_mul at this @[to_additive] lemma lt_of_mul_lt_mul_right' (h : a * b < c * b) : a < c := lt_of_mul_lt_mul_left' (show b * a < b * c, begin rw [mul_comm b a, mul_comm b c], assumption end) -- here we start using properties of one. @[to_additive] lemma le_mul_of_one_le_of_le' (ha : 1 ≀ a) (hbc : b ≀ c) : b ≀ a * c := one_mul b β–Έ mul_le_mul' ha hbc @[to_additive] lemma le_mul_of_le_of_one_le' (hbc : b ≀ c) (ha : 1 ≀ a) : b ≀ c * a := mul_one b β–Έ mul_le_mul' hbc ha @[to_additive add_nonneg'] lemma one_le_mul' (ha : 1 ≀ a) (hb : 1 ≀ b) : 1 ≀ a * b := le_mul_of_one_le_of_le' ha hb @[to_additive] lemma mul_one_lt_of_one_lt_of_one_le' (ha : 1 < a) (hb : 1 ≀ b) : 1 < a * b := lt_of_lt_of_le ha $ le_mul_of_one_le_right'' hb @[to_additive] lemma mul_one_lt' (ha : 1 < a) (hb : 1 < b) : 1 < a * b := mul_one_lt_of_one_lt_of_one_le' ha $ le_of_lt hb @[to_additive] lemma mul_one_lt_of_one_le_of_one_lt' (ha : 1 ≀ a) (hb : 1 < b) : 1 < a * b := lt_of_lt_of_le hb $ le_mul_of_one_le_left'' ha @[to_additive] lemma mul_le_one' (ha : a ≀ 1) (hb : b ≀ 1) : a * b ≀ 1 := one_mul (1:Ξ±) β–Έ (mul_le_mul' ha hb) @[to_additive] lemma mul_le_of_le_one_of_le' (ha : a ≀ 1) (hbc : b ≀ c) : a * b ≀ c := one_mul c β–Έ mul_le_mul' ha hbc @[to_additive] lemma mul_le_of_le_of_le_one' (hbc : b ≀ c) (ha : a ≀ 1) : b * a ≀ c := mul_one c β–Έ mul_le_mul' hbc ha @[to_additive] lemma mul_lt_one_of_lt_one_of_le_one' (ha : a < 1) (hb : b ≀ 1) : a * b < 1 := lt_of_le_of_lt (mul_le_of_le_of_le_one' (le_refl _) hb) ha @[to_additive] lemma mul_lt_one_of_le_one_of_lt_one' (ha : a ≀ 1) (hb : b < 1) : a * b < 1 := lt_of_le_of_lt (mul_le_of_le_one_of_le' ha (le_refl _)) hb @[to_additive] lemma mul_lt_one' (ha : a < 1) (hb : b < 1) : a * b < 1 := mul_lt_one_of_le_one_of_lt_one' (le_of_lt ha) hb @[to_additive] lemma lt_mul_of_one_le_of_lt' (ha : 1 ≀ a) (hbc : b < c) : b < a * c := lt_of_lt_of_le hbc $ le_mul_of_one_le_left'' ha @[to_additive] lemma lt_mul_of_lt_of_one_le' (hbc : b < c) (ha : 1 ≀ a) : b < c * a := lt_of_lt_of_le hbc $ le_mul_of_one_le_right'' ha @[to_additive] lemma lt_mul_of_one_lt_of_lt' (ha : 1 < a) (hbc : b < c) : b < a * c := lt_mul_of_one_le_of_lt' (le_of_lt ha) hbc @[to_additive] lemma lt_mul_of_lt_of_one_lt' (hbc : b < c) (ha : 1 < a) : b < c * a := lt_mul_of_lt_of_one_le' hbc (le_of_lt ha) @[to_additive] lemma mul_lt_of_le_one_of_lt' (ha : a ≀ 1) (hbc : b < c) : a * b < c := lt_of_le_of_lt (mul_le_of_le_one_of_le' ha (le_refl _)) hbc @[to_additive] lemma mul_lt_of_lt_of_le_one' (hbc : b < c) (ha : a ≀ 1) : b * a < c := lt_of_le_of_lt (mul_le_of_le_of_le_one' (le_refl _) ha) hbc @[to_additive] lemma mul_lt_of_lt_one_of_lt' (ha : a < 1) (hbc : b < c) : a * b < c := mul_lt_of_le_one_of_lt' (le_of_lt ha) hbc @[to_additive] lemma mul_lt_of_lt_of_lt_one' (hbc : b < c) (ha : a < 1) : b * a < c := mul_lt_of_lt_of_le_one' hbc (le_of_lt ha) @[to_additive] lemma mul_eq_one_iff' (ha : 1 ≀ a) (hb : 1 ≀ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := iff.intro (assume hab : a * b = 1, have a ≀ 1, from hab β–Έ le_mul_of_le_of_one_le' (le_refl _) hb, have a = 1, from le_antisymm this ha, have b ≀ 1, from hab β–Έ le_mul_of_one_le_of_le' ha (le_refl _), have b = 1, from le_antisymm this hb, and.intro β€Ήa = 1β€Ί β€Ήb = 1β€Ί) (assume ⟨ha', hb'⟩, by rw [ha', hb', mul_one]) section mono variables {Ξ² : Type*} [preorder Ξ²] {f g : Ξ² β†’ Ξ±} @[to_additive monotone.add] lemma monotone.mul' (hf : monotone f) (hg : monotone g) : monotone (Ξ» x, f x * g x) := Ξ» x y h, mul_le_mul' (hf h) (hg h) @[to_additive monotone.add_const] lemma monotone.mul_const' (hf : monotone f) (a : Ξ±) : monotone (Ξ» x, f x * a) := hf.mul' monotone_const @[to_additive monotone.const_add] lemma monotone.const_mul' (hf : monotone f) (a : Ξ±) : monotone (Ξ» x, a * f x) := monotone_const.mul' hf end mono end ordered_comm_monoid lemma bit0_pos [ordered_add_comm_monoid Ξ±] {a : Ξ±} (h : 0 < a) : 0 < bit0 a := add_pos' h h namespace units @[to_additive] instance [monoid Ξ±] [preorder Ξ±] : preorder (units Ξ±) := preorder.lift (coe : units Ξ± β†’ Ξ±) @[simp, to_additive] theorem coe_le_coe [monoid Ξ±] [preorder Ξ±] {a b : units Ξ±} : (a : Ξ±) ≀ b ↔ a ≀ b := iff.rfl @[simp, to_additive] theorem coe_lt_coe [monoid Ξ±] [preorder Ξ±] {a b : units Ξ±} : (a : Ξ±) < b ↔ a < b := iff.rfl @[to_additive] instance [monoid Ξ±] [partial_order Ξ±] : partial_order (units Ξ±) := partial_order.lift coe units.ext @[to_additive] instance [monoid Ξ±] [linear_order Ξ±] : linear_order (units Ξ±) := linear_order.lift coe units.ext @[to_additive] instance [monoid Ξ±] [decidable_linear_order Ξ±] : decidable_linear_order (units Ξ±) := decidable_linear_order.lift coe units.ext @[simp, to_additive] theorem max_coe [monoid Ξ±] [decidable_linear_order Ξ±] {a b : units Ξ±} : (↑(max a b) : Ξ±) = max a b := by by_cases a ≀ b; simp [max, h] @[simp, to_additive] theorem min_coe [monoid Ξ±] [decidable_linear_order Ξ±] {a b : units Ξ±} : (↑(min a b) : Ξ±) = min a b := by by_cases a ≀ b; simp [min, h] end units namespace with_zero instance [preorder Ξ±] : preorder (with_zero Ξ±) := with_bot.preorder instance [partial_order Ξ±] : partial_order (with_zero Ξ±) := with_bot.partial_order instance [partial_order Ξ±] : order_bot (with_zero Ξ±) := with_bot.order_bot instance [lattice Ξ±] : lattice (with_zero Ξ±) := with_bot.lattice instance [linear_order Ξ±] : linear_order (with_zero Ξ±) := with_bot.linear_order instance [decidable_linear_order Ξ±] : decidable_linear_order (with_zero Ξ±) := with_bot.decidable_linear_order /-- If `0` is the least element in `Ξ±`, then `with_zero Ξ±` is an `ordered_add_comm_monoid`. -/ def ordered_add_comm_monoid [ordered_add_comm_monoid Ξ±] (zero_le : βˆ€ a : Ξ±, 0 ≀ a) : ordered_add_comm_monoid (with_zero Ξ±) := begin suffices, refine { add_le_add_left := this, ..with_zero.partial_order, ..with_zero.add_comm_monoid, .. }, { intros a b c h, have h' := lt_iff_le_not_le.1 h, rw lt_iff_le_not_le at ⊒, refine ⟨λ b hβ‚‚, _, Ξ» hβ‚‚, h'.2 $ this _ _ hβ‚‚ _⟩, cases hβ‚‚, cases c with c, { cases h'.2 (this _ _ bot_le a) }, { refine ⟨_, rfl, _⟩, cases a with a, { exact with_bot.some_le_some.1 h'.1 }, { exact le_of_lt (lt_of_add_lt_add_left' $ with_bot.some_lt_some.1 h), } } }, { intros a b h c ca hβ‚‚, cases b with b, { rw le_antisymm h bot_le at hβ‚‚, exact ⟨_, hβ‚‚, le_refl _⟩ }, cases a with a, { change c + 0 = some ca at hβ‚‚, simp at hβ‚‚, simp [hβ‚‚], exact ⟨_, rfl, by simpa using add_le_add_left' (zero_le b)⟩ }, { simp at h, cases c with c; change some _ = _ at hβ‚‚; simp [-add_comm] at hβ‚‚; subst ca; refine ⟨_, rfl, _⟩, { exact h }, { exact add_le_add_left' h } } } end end with_zero namespace with_top instance [add_semigroup Ξ±] : add_semigroup (with_top Ξ±) := { add := Ξ» o₁ oβ‚‚, o₁.bind (Ξ» a, oβ‚‚.map (Ξ» b, a + b)), ..@additive.add_semigroup _ $ @with_zero.semigroup (multiplicative Ξ±) _ } lemma coe_add [add_semigroup Ξ±] {a b : Ξ±} : ((a + b : Ξ±) : with_top Ξ±) = a + b := rfl instance [add_comm_semigroup Ξ±] : add_comm_semigroup (with_top Ξ±) := { ..@additive.add_comm_semigroup _ $ @with_zero.comm_semigroup (multiplicative Ξ±) _ } instance [add_monoid Ξ±] : add_monoid (with_top Ξ±) := { zero := some 0, add := (+), ..@additive.add_monoid _ $ @with_zero.monoid (multiplicative Ξ±) _ } instance [add_comm_monoid Ξ±] : add_comm_monoid (with_top Ξ±) := { zero := 0, add := (+), ..@additive.add_comm_monoid _ $ @with_zero.comm_monoid (multiplicative Ξ±) _ } instance [ordered_add_comm_monoid Ξ±] : ordered_add_comm_monoid (with_top Ξ±) := begin suffices, refine { add_le_add_left := this, ..with_top.partial_order, ..with_top.add_comm_monoid, ..}, { intros a b c h, have h' := h, rw lt_iff_le_not_le at h' ⊒, refine ⟨λ c hβ‚‚, _, Ξ» hβ‚‚, h'.2 $ this _ _ hβ‚‚ _⟩, cases hβ‚‚, cases a with a, { exact (not_le_of_lt h).elim le_top }, cases b with b, { exact (not_le_of_lt h).elim le_top }, { exact ⟨_, rfl, le_of_lt (lt_of_add_lt_add_left' $ with_top.some_lt_some.1 h)⟩ } }, { intros a b h c ca hβ‚‚, cases c with c, {cases hβ‚‚}, cases b with b; cases hβ‚‚, cases a with a, {cases le_antisymm h le_top }, simp at h, exact ⟨_, rfl, add_le_add_left' h⟩, } end @[simp] lemma zero_lt_top [ordered_add_comm_monoid Ξ±] : (0 : with_top Ξ±) < ⊀ := coe_lt_top 0 @[simp] lemma zero_lt_coe [ordered_add_comm_monoid Ξ±] (a : Ξ±) : (0 : with_top Ξ±) < a ↔ 0 < a := coe_lt_coe @[simp] lemma add_top [ordered_add_comm_monoid Ξ±] : βˆ€{a : with_top Ξ±}, a + ⊀ = ⊀ | none := rfl | (some a) := rfl @[simp] lemma top_add [ordered_add_comm_monoid Ξ±] {a : with_top Ξ±} : ⊀ + a = ⊀ := rfl lemma add_eq_top [ordered_add_comm_monoid Ξ±] (a b : with_top Ξ±) : a + b = ⊀ ↔ a = ⊀ ∨ b = ⊀ := by cases a; cases b; simp [none_eq_top, some_eq_coe, coe_add.symm] lemma add_lt_top [ordered_add_comm_monoid Ξ±] (a b : with_top Ξ±) : a + b < ⊀ ↔ a < ⊀ ∧ b < ⊀ := begin apply not_iff_not.1, simp [lt_top_iff_ne_top, add_eq_top], finish, apply classical.dec _, apply classical.dec _, end end with_top namespace with_bot instance [add_semigroup Ξ±] : add_semigroup (with_bot Ξ±) := with_top.add_semigroup instance [add_comm_semigroup Ξ±] : add_comm_semigroup (with_bot Ξ±) := with_top.add_comm_semigroup instance [add_monoid Ξ±] : add_monoid (with_bot Ξ±) := with_top.add_monoid instance [add_comm_monoid Ξ±] : add_comm_monoid (with_bot Ξ±) := with_top.add_comm_monoid instance [ordered_add_comm_monoid Ξ±] : ordered_add_comm_monoid (with_bot Ξ±) := begin suffices, refine { add_le_add_left := this, ..with_bot.partial_order, ..with_bot.add_comm_monoid, ..}, { intros a b c h, have h' := h, rw lt_iff_le_not_le at h' ⊒, refine ⟨λ b hβ‚‚, _, Ξ» hβ‚‚, h'.2 $ this _ _ hβ‚‚ _⟩, cases hβ‚‚, cases a with a, { exact (not_le_of_lt h).elim bot_le }, cases c with c, { exact (not_le_of_lt h).elim bot_le }, { exact ⟨_, rfl, le_of_lt (lt_of_add_lt_add_left' $ with_bot.some_lt_some.1 h)⟩ } }, { intros a b h c ca hβ‚‚, cases c with c, {cases hβ‚‚}, cases a with a; cases hβ‚‚, cases b with b, {cases le_antisymm h bot_le}, simp at h, exact ⟨_, rfl, add_le_add_left' h⟩, } end @[simp] lemma coe_zero [add_monoid Ξ±] : ((0 : Ξ±) : with_bot Ξ±) = 0 := rfl @[simp] lemma coe_add [add_semigroup Ξ±] (a b : Ξ±) : ((a + b : Ξ±) : with_bot Ξ±) = a + b := rfl @[simp] lemma bot_add [ordered_add_comm_monoid Ξ±] (a : with_bot Ξ±) : βŠ₯ + a = βŠ₯ := rfl @[simp] lemma add_bot [ordered_add_comm_monoid Ξ±] (a : with_bot Ξ±) : a + βŠ₯ = βŠ₯ := by cases a; refl instance has_one [has_one Ξ±] : has_one (with_bot Ξ±) := ⟨(1 : Ξ±)⟩ @[simp] lemma coe_one [has_one Ξ±] : ((1 : Ξ±) : with_bot Ξ±) = 1 := rfl end with_bot /-- A canonically ordered monoid is an ordered commutative monoid in which the ordering coincides with the divisibility relation, which is to say, `a ≀ b` iff there exists `c` with `b = a + c`. This is satisfied by the natural numbers, for example, but not the integers or other ordered groups. -/ @[protect_proj] class canonically_ordered_add_monoid (Ξ± : Type*) extends ordered_add_comm_monoid Ξ±, order_bot Ξ± := (le_iff_exists_add : βˆ€a b:Ξ±, a ≀ b ↔ βˆƒc, b = a + c) section canonically_ordered_add_monoid variables [canonically_ordered_add_monoid Ξ±] {a b c d : Ξ±} lemma le_iff_exists_add : a ≀ b ↔ βˆƒc, b = a + c := canonically_ordered_add_monoid.le_iff_exists_add a b @[simp] lemma zero_le (a : Ξ±) : 0 ≀ a := le_iff_exists_add.mpr ⟨a, by simp⟩ @[simp] lemma bot_eq_zero : (βŠ₯ : Ξ±) = 0 := le_antisymm bot_le (zero_le βŠ₯) @[simp] lemma add_eq_zero_iff : a + b = 0 ↔ a = 0 ∧ b = 0 := add_eq_zero_iff' (zero_le _) (zero_le _) @[simp] lemma le_zero_iff_eq : a ≀ 0 ↔ a = 0 := iff.intro (assume h, le_antisymm h (zero_le a)) (assume h, h β–Έ le_refl a) lemma zero_lt_iff_ne_zero : 0 < a ↔ a β‰  0 := iff.intro ne_of_gt $ assume hne, lt_of_le_of_ne (zero_le _) hne.symm @[nolint ge_or_gt] lemma exists_pos_add_of_lt (h : a < b) : βˆƒ c > 0, a + c = b := begin obtain ⟨c, hc⟩ := le_iff_exists_add.1 (le_of_lt h), refine ⟨c, zero_lt_iff_ne_zero.2 _, hc.symm⟩, rintro rfl, simpa [hc, lt_irrefl] using h end lemma le_add_left (h : a ≀ c) : a ≀ b + c := calc a = 0 + a : by simp ... ≀ b + c : add_le_add' (zero_le _) h lemma le_add_right (h : a ≀ b) : a ≀ b + c := calc a = a + 0 : by simp ... ≀ b + c : add_le_add' h (zero_le _) instance with_zero.canonically_ordered_add_monoid : canonically_ordered_add_monoid (with_zero Ξ±) := { le_iff_exists_add := Ξ» a b, begin cases a with a, { exact iff_of_true bot_le ⟨b, (zero_add b).symm⟩ }, cases b with b, { exact iff_of_false (mt (le_antisymm bot_le) (by simp)) (Ξ» ⟨c, h⟩, by cases c; cases h) }, { simp [le_iff_exists_add, -add_comm], split; intro h; rcases h with ⟨c, h⟩, { exact ⟨some c, congr_arg some h⟩ }, { cases c; cases h, { exact ⟨_, (add_zero _).symm⟩ }, { exact ⟨_, rfl⟩ } } } end, bot := 0, bot_le := assume a a' h, option.no_confusion h, .. with_zero.ordered_add_comm_monoid zero_le } instance with_top.canonically_ordered_add_monoid : canonically_ordered_add_monoid (with_top Ξ±) := { le_iff_exists_add := assume a b, match a, b with | a, none := show a ≀ ⊀ ↔ βˆƒc, ⊀ = a + c, by simp; refine ⟨⊀, _⟩; cases a; refl | (some a), (some b) := show (a:with_top Ξ±) ≀ ↑b ↔ βˆƒc:with_top Ξ±, ↑b = ↑a + c, begin simp [canonically_ordered_add_monoid.le_iff_exists_add, -add_comm], split, { rintro ⟨c, rfl⟩, refine ⟨c, _⟩, simp [with_top.coe_add] }, { exact assume h, match b, h with _, ⟨some c, rfl⟩ := ⟨_, rfl⟩ end } end | none, some b := show (⊀ : with_top Ξ±) ≀ b ↔ βˆƒc:with_top Ξ±, ↑b = ⊀ + c, by simp end, .. with_top.order_bot, .. with_top.ordered_add_comm_monoid } end canonically_ordered_add_monoid /-- An ordered cancellative additive commutative monoid is an additive commutative monoid with a partial order, in which addition is cancellative and strictly monotone. -/ @[protect_proj, ancestor add_comm_monoid add_left_cancel_semigroup add_right_cancel_semigroup partial_order] class ordered_cancel_add_comm_monoid (Ξ± : Type u) extends add_comm_monoid Ξ±, add_left_cancel_semigroup Ξ±, add_right_cancel_semigroup Ξ±, partial_order Ξ± := (add_le_add_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c + a ≀ c + b) (le_of_add_le_add_left : βˆ€ a b c : Ξ±, a + b ≀ a + c β†’ b ≀ c) /-- An ordered cancellative commutative monoid is a commutative monoid with a partial order, in which multiplication is cancellative and strictly monotone. -/ @[protect_proj, ancestor comm_monoid left_cancel_semigroup right_cancel_semigroup partial_order] class ordered_cancel_comm_monoid (Ξ± : Type u) extends comm_monoid Ξ±, left_cancel_semigroup Ξ±, right_cancel_semigroup Ξ±, partial_order Ξ± := (mul_le_mul_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c * a ≀ c * b) (le_of_mul_le_mul_left : βˆ€ a b c : Ξ±, a * b ≀ a * c β†’ b ≀ c) attribute [to_additive ordered_cancel_add_comm_monoid] ordered_cancel_comm_monoid section ordered_cancel_comm_monoid variables [ordered_cancel_comm_monoid Ξ±] {a b c d : Ξ±} @[to_additive] instance ordered_cancel_comm_monoid.to_left_cancel_monoid : left_cancel_monoid Ξ± := { ..β€Ήordered_cancel_comm_monoid Ξ±β€Ί } @[to_additive add_le_add_left] lemma mul_le_mul_left'' : βˆ€ {a b : Ξ±} (h : a ≀ b) (c : Ξ±), c * a ≀ c * b := ordered_cancel_comm_monoid.mul_le_mul_left @[to_additive le_of_add_le_add_left] lemma le_of_mul_le_mul_left' : βˆ€ {a b c : Ξ±}, a * b ≀ a * c β†’ b ≀ c := ordered_cancel_comm_monoid.le_of_mul_le_mul_left @[to_additive add_lt_add_left] lemma mul_lt_mul_left' (h : a < b) (c : Ξ±) : c * a < c * b := lt_of_le_not_le (mul_le_mul_left'' (le_of_lt h) _) $ mt le_of_mul_le_mul_left' (not_le_of_gt h) @[to_additive lt_of_add_lt_add_left] lemma lt_of_mul_lt_mul_left'' (h : a * b < a * c) : b < c := lt_of_le_not_le (le_of_mul_le_mul_left' (le_of_lt h)) $ mt (Ξ» h, mul_le_mul_left'' h _) (not_le_of_gt h) @[to_additive] instance ordered_cancel_comm_monoid.to_ordered_comm_monoid : ordered_comm_monoid Ξ± := { lt_of_mul_lt_mul_left := @lt_of_mul_lt_mul_left'' _ _, ..β€Ήordered_cancel_comm_monoid Ξ±β€Ί } @[to_additive add_le_add_right] lemma mul_le_mul_right'' (h : a ≀ b) (c : Ξ±) : a * c ≀ b * c := mul_comm c a β–Έ mul_comm c b β–Έ mul_le_mul_left' h @[to_additive add_lt_add_right] lemma mul_lt_mul_right' (h : a < b) (c : Ξ±) : a * c < b * c := begin rw [mul_comm a c, mul_comm b c], exact (mul_lt_mul_left' h c) end @[to_additive add_le_add] lemma mul_le_mul'' {a b c d : Ξ±} (h₁ : a ≀ b) (hβ‚‚ : c ≀ d) : a * c ≀ b * d := le_trans (mul_le_mul_right' h₁) (mul_le_mul_left' hβ‚‚) @[to_additive] lemma le_mul_of_one_le_right (h : 1 ≀ b) : a ≀ a * b := have a * 1 ≀ a * b, from mul_le_mul_left' h, by rwa mul_one at this @[to_additive] lemma le_mul_of_one_le_left (h : 1 ≀ b) : a ≀ b * a := have 1 * a ≀ b * a, from mul_le_mul_right' h, by rwa one_mul at this @[to_additive add_lt_add] lemma mul_lt_mul''' (h₁ : a < b) (hβ‚‚ : c < d) : a * c < b * d := lt_trans (mul_lt_mul_right' h₁ c) (mul_lt_mul_left' hβ‚‚ b) @[to_additive] lemma mul_lt_mul_of_le_of_lt (h₁ : a ≀ b) (hβ‚‚ : c < d) : a * c < b * d := lt_of_le_of_lt (mul_le_mul_right' h₁) (mul_lt_mul_left' hβ‚‚ b) @[to_additive] lemma mul_lt_mul_of_lt_of_le (h₁ : a < b) (hβ‚‚ : c ≀ d) : a * c < b * d := lt_of_lt_of_le (mul_lt_mul_right' h₁ c) (mul_le_mul_left' hβ‚‚) @[to_additive] lemma lt_mul_of_one_lt_right (a : Ξ±) {b : Ξ±} (h : 1 < b) : a < a * b := have a * 1 < a * b, from mul_lt_mul_left' h a, by rwa [mul_one] at this @[to_additive] lemma lt_mul_of_one_lt_left (a : Ξ±) {b : Ξ±} (h : 1 < b) : a < b * a := have 1 * a < b * a, from mul_lt_mul_right' h a, by rwa [one_mul] at this @[to_additive le_of_add_le_add_right] lemma le_of_mul_le_mul_right' (h : a * b ≀ c * b) : a ≀ c := le_of_mul_le_mul_left' (show b * a ≀ b * c, begin rw [mul_comm b a, mul_comm b c], assumption end) @[to_additive lt_of_add_lt_add_right] lemma lt_of_mul_lt_mul_right'' (h : a * b < c * b) : a < c := lt_of_mul_lt_mul_left'' (show b * a < b * c, begin rw [mul_comm b a, mul_comm b c], assumption end) -- here we start using properties of one. @[to_additive add_nonneg] lemma one_le_mul (ha : 1 ≀ a) (hb : 1 ≀ b) : 1 ≀ a * b := one_mul (1:Ξ±) β–Έ (mul_le_mul'' ha hb) @[to_additive] lemma mul_one_lt (ha : 1 < a) (hb : 1 < b) : 1 < a * b := one_mul (1:Ξ±) β–Έ (mul_lt_mul''' ha hb) @[to_additive] lemma mul_one_lt_of_one_lt_of_one_le (ha : 1 < a) (hb : 1 ≀ b) : 1 < a * b := one_mul (1:Ξ±) β–Έ (mul_lt_mul_of_lt_of_le ha hb) @[to_additive] lemma mul_one_lt_of_one_le_of_one_lt (ha : 1 ≀ a) (hb : 1 < b) : 1 < a * b := one_mul (1:Ξ±) β–Έ (mul_lt_mul_of_le_of_lt ha hb) @[to_additive add_nonpos] lemma mul_le_one'' (ha : a ≀ 1) (hb : b ≀ 1) : a * b ≀ 1 := one_mul (1:Ξ±) β–Έ (mul_le_mul'' ha hb) @[to_additive] lemma mul_lt_one (ha : a < 1) (hb : b < 1) : a * b < 1 := one_mul (1:Ξ±) β–Έ (mul_lt_mul''' ha hb) @[to_additive] lemma mul_lt_one_of_lt_one_of_le_one (ha : a < 1) (hb : b ≀ 1) : a * b < 1 := one_mul (1:Ξ±) β–Έ (mul_lt_mul_of_lt_of_le ha hb) @[to_additive] lemma mul_lt_one_of_le_one_of_lt_one (ha : a ≀ 1) (hb : b < 1) : a * b < 1 := one_mul (1:Ξ±) β–Έ (mul_lt_mul_of_le_of_lt ha hb) @[to_additive] lemma mul_eq_one_iff_eq_one_and_eq_one_of_one_le_of_one_le (ha : 1 ≀ a) (hb : 1 ≀ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := iff.intro (assume hab : a * b = 1, have ha' : a ≀ 1, from calc a = a * 1 : by rw mul_one ... ≀ a * b : mul_le_mul_left' hb ... = 1 : hab, have haz : a = 1, from le_antisymm ha' ha, have hb' : b ≀ 1, from calc b = 1 * b : by rw one_mul ... ≀ a * b : by exact mul_le_mul_right' ha ... = 1 : hab, have hbz : b = 1, from le_antisymm hb' hb, and.intro haz hbz) (assume ⟨ha', hb'⟩, by rw [ha', hb', mul_one]) @[to_additive] lemma le_mul_of_one_le_of_le (ha : 1 ≀ a) (hbc : b ≀ c) : b ≀ a * c := one_mul b β–Έ mul_le_mul'' ha hbc @[to_additive] lemma le_mul_of_le_of_one_le (hbc : b ≀ c) (ha : 1 ≀ a) : b ≀ c * a := mul_one b β–Έ mul_le_mul'' hbc ha @[to_additive] lemma lt_mul_of_one_lt_of_le (ha : 1 < a) (hbc : b ≀ c) : b < a * c := one_mul b β–Έ mul_lt_mul_of_lt_of_le ha hbc @[to_additive] lemma lt_mul_of_le_of_one_lt (hbc : b ≀ c) (ha : 1 < a) : b < c * a := mul_one b β–Έ mul_lt_mul_of_le_of_lt hbc ha @[to_additive] lemma mul_le_of_le_one_of_le (ha : a ≀ 1) (hbc : b ≀ c) : a * b ≀ c := one_mul c β–Έ mul_le_mul'' ha hbc @[to_additive] lemma mul_le_of_le_of_le_one (hbc : b ≀ c) (ha : a ≀ 1) : b * a ≀ c := mul_one c β–Έ mul_le_mul'' hbc ha @[to_additive] lemma mul_lt_of_lt_one_of_le (ha : a < 1) (hbc : b ≀ c) : a * b < c := one_mul c β–Έ mul_lt_mul_of_lt_of_le ha hbc @[to_additive] lemma mul_lt_of_le_of_lt_one (hbc : b ≀ c) (ha : a < 1) : b * a < c := mul_one c β–Έ mul_lt_mul_of_le_of_lt hbc ha @[to_additive] lemma lt_mul_of_one_le_of_lt (ha : 1 ≀ a) (hbc : b < c) : b < a * c := one_mul b β–Έ mul_lt_mul_of_le_of_lt ha hbc @[to_additive] lemma lt_mul_of_lt_of_one_le (hbc : b < c) (ha : 1 ≀ a) : b < c * a := mul_one b β–Έ mul_lt_mul_of_lt_of_le hbc ha @[to_additive] lemma lt_mul_of_one_lt_of_lt (ha : 1 < a) (hbc : b < c) : b < a * c := one_mul b β–Έ mul_lt_mul''' ha hbc @[to_additive] lemma lt_mul_of_lt_of_one_lt (hbc : b < c) (ha : 1 < a) : b < c * a := mul_one b β–Έ mul_lt_mul''' hbc ha @[to_additive] lemma mul_lt_of_le_one_of_lt (ha : a ≀ 1) (hbc : b < c) : a * b < c := one_mul c β–Έ mul_lt_mul_of_le_of_lt ha hbc @[to_additive] lemma mul_lt_of_lt_of_le_one (hbc : b < c) (ha : a ≀ 1) : b * a < c := mul_one c β–Έ mul_lt_mul_of_lt_of_le hbc ha @[to_additive] lemma mul_lt_of_lt_one_of_lt (ha : a < 1) (hbc : b < c) : a * b < c := one_mul c β–Έ mul_lt_mul''' ha hbc @[to_additive] lemma mul_lt_of_lt_of_lt_one (hbc : b < c) (ha : a < 1) : b * a < c := mul_one c β–Έ mul_lt_mul''' hbc ha @[simp, to_additive] lemma mul_le_mul_iff_left (a : Ξ±) {b c : Ξ±} : a * b ≀ a * c ↔ b ≀ c := ⟨le_of_mul_le_mul_left', Ξ» h, mul_le_mul_left' h⟩ @[simp, to_additive] lemma mul_le_mul_iff_right (c : Ξ±) : a * c ≀ b * c ↔ a ≀ b := mul_comm c a β–Έ mul_comm c b β–Έ mul_le_mul_iff_left c @[simp, to_additive] lemma mul_lt_mul_iff_left (a : Ξ±) {b c : Ξ±} : a * b < a * c ↔ b < c := ⟨lt_of_mul_lt_mul_left'', Ξ» h, mul_lt_mul_left' h _⟩ @[simp, to_additive] lemma mul_lt_mul_iff_right (c : Ξ±) : a * c < b * c ↔ a < b := mul_comm c a β–Έ mul_comm c b β–Έ mul_lt_mul_iff_left c @[simp, to_additive le_add_iff_nonneg_right] lemma le_mul_iff_one_le_right' (a : Ξ±) {b : Ξ±} : a ≀ a * b ↔ 1 ≀ b := have a * 1 ≀ a * b ↔ 1 ≀ b, from mul_le_mul_iff_left a, by rwa mul_one at this @[simp, to_additive le_add_iff_nonneg_left] lemma le_mul_iff_one_le_left' (a : Ξ±) {b : Ξ±} : a ≀ b * a ↔ 1 ≀ b := by rw [mul_comm, le_mul_iff_one_le_right'] @[simp, to_additive lt_add_iff_pos_right] lemma lt_mul_iff_one_lt_right' (a : Ξ±) {b : Ξ±} : a < a * b ↔ 1 < b := have a * 1 < a * b ↔ 1 < b, from mul_lt_mul_iff_left a, by rwa mul_one at this @[simp, to_additive lt_add_iff_pos_left] lemma lt_mul_iff_one_lt_left' (a : Ξ±) {b : Ξ±} : a < b * a ↔ 1 < b := by rw [mul_comm, lt_mul_iff_one_lt_right'] @[simp, to_additive add_le_iff_nonpos_left] lemma mul_le_iff_le_one_left' : a * b ≀ b ↔ a ≀ 1 := by { convert mul_le_mul_iff_right b, rw [one_mul] } @[simp, to_additive add_le_iff_nonpos_right] lemma mul_le_iff_le_one_right' : a * b ≀ a ↔ b ≀ 1 := by { convert mul_le_mul_iff_left a, rw [mul_one] } @[simp, to_additive add_lt_iff_neg_right] lemma mul_lt_iff_lt_one_right' : a * b < b ↔ a < 1 := by { convert mul_lt_mul_iff_right b, rw [one_mul] } @[simp, to_additive add_lt_iff_neg_left] lemma mul_lt_iff_lt_one_left' : a * b < a ↔ b < 1 := by { convert mul_lt_mul_iff_left a, rw [mul_one] } @[to_additive] lemma mul_eq_one_iff_eq_one_of_one_le (ha : 1 ≀ a) (hb : 1 ≀ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := ⟨λ hab : a * b = 1, by split; apply le_antisymm; try {assumption}; rw ← hab; simp [ha, hb], Ξ» ⟨ha', hb'⟩, by rw [ha', hb', mul_one]⟩ section mono variables {Ξ² : Type*} [preorder Ξ²] {f g : Ξ² β†’ Ξ±} @[to_additive monotone.add_strict_mono] lemma monotone.mul_strict_mono' (hf : monotone f) (hg : strict_mono g) : strict_mono (Ξ» x, f x * g x) := Ξ» x y h, mul_lt_mul_of_le_of_lt (hf $ le_of_lt h) (hg h) @[to_additive strict_mono.add_monotone] lemma strict_mono.mul_monotone' (hf : strict_mono f) (hg : monotone g) : strict_mono (Ξ» x, f x * g x) := Ξ» x y h, mul_lt_mul_of_lt_of_le (hf h) (hg $ le_of_lt h) @[to_additive strict_mono.add_const] lemma strict_mono.mul_const' (hf : strict_mono f) (c : Ξ±) : strict_mono (Ξ» x, f x * c) := hf.mul_monotone' monotone_const @[to_additive strict_mono.const_add] lemma strict_mono.const_mul' (hf : strict_mono f) (c : Ξ±) : strict_mono (Ξ» x, c * f x) := monotone_const.mul_strict_mono' hf end mono end ordered_cancel_comm_monoid section ordered_cancel_add_comm_monoid variable [ordered_cancel_add_comm_monoid Ξ±] lemma with_top.add_lt_add_iff_left : βˆ€{a b c : with_top Ξ±}, a < ⊀ β†’ (a + c < a + b ↔ c < b) | none := assume b c h, (lt_irrefl ⊀ h).elim | (some a) := begin assume b c h, cases b; cases c; simp [with_top.none_eq_top, with_top.some_eq_coe, with_top.coe_lt_top, with_top.coe_lt_coe], { rw [← with_top.coe_add], exact with_top.coe_lt_top _ }, { rw [← with_top.coe_add, ← with_top.coe_add, with_top.coe_lt_coe], exact add_lt_add_iff_left _ } end lemma with_top.add_lt_add_iff_right {a b c : with_top Ξ±} : a < ⊀ β†’ (c + a < b + a ↔ c < b) := by simpa [add_comm] using @with_top.add_lt_add_iff_left _ _ a b c end ordered_cancel_add_comm_monoid /-- An ordered additive commutative group is an additive commutative group with a partial order in which addition is strictly monotone. -/ @[protect_proj, ancestor add_comm_group partial_order] class ordered_add_comm_group (Ξ± : Type u) extends add_comm_group Ξ±, partial_order Ξ± := (add_le_add_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c + a ≀ c + b) /-- An ordered commutative group is an commutative group with a partial order in which multiplication is strictly monotone. -/ @[protect_proj, ancestor comm_group partial_order] class ordered_comm_group (Ξ± : Type u) extends comm_group Ξ±, partial_order Ξ± := (mul_le_mul_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c * a ≀ c * b) attribute [to_additive ordered_add_comm_group] ordered_comm_group /--The units of an ordered commutative monoid form an ordered commutative group. -/ @[to_additive] instance units.ordered_comm_group [ordered_comm_monoid Ξ±] : ordered_comm_group (units Ξ±) := { mul_le_mul_left := Ξ» a b h c, mul_le_mul_left' h, .. units.partial_order, .. (infer_instance : comm_group (units Ξ±)) } section ordered_comm_group variables [ordered_comm_group Ξ±] {a b c d : Ξ±} @[to_additive ordered_add_comm_group.add_lt_add_left] lemma ordered_comm_group.mul_lt_mul_left' (a b : Ξ±) (h : a < b) (c : Ξ±) : c * a < c * b := begin rw lt_iff_le_not_le at h ⊒, split, { apply ordered_comm_group.mul_le_mul_left _ _ h.1 }, { intro w, replace w : c⁻¹ * (c * b) ≀ c⁻¹ * (c * a) := ordered_comm_group.mul_le_mul_left _ _ w _, simp only [mul_one, mul_comm, mul_left_inv, mul_left_comm] at w, exact h.2 w }, end @[to_additive ordered_add_comm_group.le_of_add_le_add_left] lemma ordered_comm_group.le_of_mul_le_mul_left (h : a * b ≀ a * c) : b ≀ c := have a⁻¹ * (a * b) ≀ a⁻¹ * (a * c), from ordered_comm_group.mul_le_mul_left _ _ h _, begin simp [inv_mul_cancel_left] at this, assumption end @[to_additive] lemma ordered_comm_group.lt_of_mul_lt_mul_left (h : a * b < a * c) : b < c := have a⁻¹ * (a * b) < a⁻¹ * (a * c), from ordered_comm_group.mul_lt_mul_left' _ _ h _, begin simp [inv_mul_cancel_left] at this, assumption end @[to_additive] instance ordered_comm_group.to_ordered_cancel_comm_monoid (Ξ± : Type u) [s : ordered_comm_group Ξ±] : ordered_cancel_comm_monoid Ξ± := { mul_left_cancel := @mul_left_cancel Ξ± _, mul_right_cancel := @mul_right_cancel Ξ± _, le_of_mul_le_mul_left := @ordered_comm_group.le_of_mul_le_mul_left Ξ± _, ..s } @[to_additive neg_le_neg] lemma inv_le_inv' (h : a ≀ b) : b⁻¹ ≀ a⁻¹ := have 1 ≀ a⁻¹ * b, from mul_left_inv a β–Έ mul_le_mul_left' h, have 1 * b⁻¹ ≀ a⁻¹ * b * b⁻¹, from mul_le_mul_right' this, by rwa [mul_inv_cancel_right, one_mul] at this @[to_additive] lemma le_of_inv_le_inv (h : b⁻¹ ≀ a⁻¹) : a ≀ b := suffices (a⁻¹)⁻¹ ≀ (b⁻¹)⁻¹, from begin simp [inv_inv] at this, assumption end, inv_le_inv' h @[to_additive] lemma one_le_of_inv_le_one (h : a⁻¹ ≀ 1) : 1 ≀ a := have a⁻¹ ≀ 1⁻¹, by rwa one_inv, le_of_inv_le_inv this @[to_additive] lemma inv_le_one_of_one_le (h : 1 ≀ a) : a⁻¹ ≀ 1 := have a⁻¹ ≀ 1⁻¹, from inv_le_inv' h, by rwa one_inv at this @[to_additive nonpos_of_neg_nonneg] lemma le_one_of_one_le_inv (h : 1 ≀ a⁻¹) : a ≀ 1 := have 1⁻¹ ≀ a⁻¹, by rwa one_inv, le_of_inv_le_inv this @[to_additive neg_nonneg_of_nonpos] lemma one_le_inv_of_le_one (h : a ≀ 1) : 1 ≀ a⁻¹ := have 1⁻¹ ≀ a⁻¹, from inv_le_inv' h, by rwa one_inv at this @[to_additive neg_lt_neg] lemma inv_lt_inv' (h : a < b) : b⁻¹ < a⁻¹ := have 1 < a⁻¹ * b, from mul_left_inv a β–Έ mul_lt_mul_left' h (a⁻¹), have 1 * b⁻¹ < a⁻¹ * b * b⁻¹, from mul_lt_mul_right' this (b⁻¹), by rwa [mul_inv_cancel_right, one_mul] at this @[to_additive] lemma lt_of_inv_lt_inv (h : b⁻¹ < a⁻¹) : a < b := inv_inv a β–Έ inv_inv b β–Έ inv_lt_inv' h @[to_additive] lemma one_lt_of_inv_inv (h : a⁻¹ < 1) : 1 < a := have a⁻¹ < 1⁻¹, by rwa one_inv, lt_of_inv_lt_inv this @[to_additive] lemma inv_inv_of_one_lt (h : 1 < a) : a⁻¹ < 1 := have a⁻¹ < 1⁻¹, from inv_lt_inv' h, by rwa one_inv at this @[to_additive neg_of_neg_pos] lemma inv_of_one_lt_inv (h : 1 < a⁻¹) : a < 1 := have 1⁻¹ < a⁻¹, by rwa one_inv, lt_of_inv_lt_inv this @[to_additive neg_pos_of_neg] lemma one_lt_inv_of_inv (h : a < 1) : 1 < a⁻¹ := have 1⁻¹ < a⁻¹, from inv_lt_inv' h, by rwa one_inv at this @[to_additive] lemma le_inv_of_le_inv (h : a ≀ b⁻¹) : b ≀ a⁻¹ := begin have h := inv_le_inv' h, rwa inv_inv at h end @[to_additive] lemma inv_le_of_inv_le (h : a⁻¹ ≀ b) : b⁻¹ ≀ a := begin have h := inv_le_inv' h, rwa inv_inv at h end @[to_additive] lemma lt_inv_of_lt_inv (h : a < b⁻¹) : b < a⁻¹ := begin have h := inv_lt_inv' h, rwa inv_inv at h end @[to_additive] lemma inv_lt_of_inv_lt (h : a⁻¹ < b) : b⁻¹ < a := begin have h := inv_lt_inv' h, rwa inv_inv at h end @[to_additive] lemma mul_le_of_le_inv_mul (h : b ≀ a⁻¹ * c) : a * b ≀ c := begin have h := mul_le_mul_left' h, rwa mul_inv_cancel_left at h end @[to_additive] lemma le_inv_mul_of_mul_le (h : a * b ≀ c) : b ≀ a⁻¹ * c := begin have h := mul_le_mul_left' h, rwa inv_mul_cancel_left at h end @[to_additive] lemma le_mul_of_inv_mul_le (h : b⁻¹ * a ≀ c) : a ≀ b * c := begin have h := mul_le_mul_left' h, rwa mul_inv_cancel_left at h end @[to_additive] lemma inv_mul_le_of_le_mul (h : a ≀ b * c) : b⁻¹ * a ≀ c := begin have h := mul_le_mul_left' h, rwa inv_mul_cancel_left at h end @[to_additive] lemma le_mul_of_inv_mul_le_left (h : b⁻¹ * a ≀ c) : a ≀ b * c := le_mul_of_inv_mul_le h @[to_additive] lemma inv_mul_le_left_of_le_mul (h : a ≀ b * c) : b⁻¹ * a ≀ c := inv_mul_le_of_le_mul h @[to_additive] lemma le_mul_of_inv_mul_le_right (h : c⁻¹ * a ≀ b) : a ≀ b * c := by { rw mul_comm, exact le_mul_of_inv_mul_le h } @[to_additive] lemma inv_mul_le_right_of_le_mul (h : a ≀ b * c) : c⁻¹ * a ≀ b := by { rw mul_comm at h, apply inv_mul_le_left_of_le_mul h } @[to_additive] lemma mul_lt_of_lt_inv_mul (h : b < a⁻¹ * c) : a * b < c := begin have h := mul_lt_mul_left' h a, rwa mul_inv_cancel_left at h end @[to_additive] lemma lt_inv_mul_of_mul_lt (h : a * b < c) : b < a⁻¹ * c := begin have h := mul_lt_mul_left' h (a⁻¹), rwa inv_mul_cancel_left at h end @[to_additive] lemma lt_mul_of_inv_mul_lt (h : b⁻¹ * a < c) : a < b * c := begin have h := mul_lt_mul_left' h b, rwa mul_inv_cancel_left at h end @[to_additive] lemma inv_mul_lt_of_lt_mul (h : a < b * c) : b⁻¹ * a < c := begin have h := mul_lt_mul_left' h (b⁻¹), rwa inv_mul_cancel_left at h end @[to_additive] lemma lt_mul_of_inv_mul_lt_left (h : b⁻¹ * a < c) : a < b * c := lt_mul_of_inv_mul_lt h @[to_additive] lemma inv_mul_lt_left_of_lt_mul (h : a < b * c) : b⁻¹ * a < c := inv_mul_lt_of_lt_mul h @[to_additive] lemma lt_mul_of_inv_mul_lt_right (h : c⁻¹ * a < b) : a < b * c := by { rw mul_comm, exact lt_mul_of_inv_mul_lt h } @[to_additive] lemma inv_mul_lt_right_of_lt_mul (h : a < b * c) : c⁻¹ * a < b := by { rw mul_comm at h, exact inv_mul_lt_of_lt_mul h } @[to_additive] lemma mul_le_mul_three {a b c d e f : Ξ±} (h₁ : a ≀ d) (hβ‚‚ : b ≀ e) (h₃ : c ≀ f) : a * b * c ≀ d * e * f := begin apply le_trans, apply mul_le_mul'', apply mul_le_mul'', assumption', apply le_refl end @[simp, to_additive] lemma inv_lt_one_iff_one_lt : a⁻¹ < 1 ↔ 1 < a := ⟨ one_lt_of_inv_inv, inv_inv_of_one_lt ⟩ @[simp, to_additive] lemma inv_le_inv_iff : a⁻¹ ≀ b⁻¹ ↔ b ≀ a := have a * b * a⁻¹ ≀ a * b * b⁻¹ ↔ a⁻¹ ≀ b⁻¹, from mul_le_mul_iff_left _, by { rw [mul_inv_cancel_right, mul_comm a, mul_inv_cancel_right] at this, rw [this] } @[to_additive neg_le] lemma inv_le' : a⁻¹ ≀ b ↔ b⁻¹ ≀ a := have a⁻¹ ≀ (b⁻¹)⁻¹ ↔ b⁻¹ ≀ a, from inv_le_inv_iff, by rwa inv_inv at this @[to_additive le_neg] lemma le_inv' : a ≀ b⁻¹ ↔ b ≀ a⁻¹ := have (a⁻¹)⁻¹ ≀ b⁻¹ ↔ b ≀ a⁻¹, from inv_le_inv_iff, by rwa inv_inv at this @[to_additive neg_le_iff_add_nonneg] lemma inv_le_iff_one_le_mul : a⁻¹ ≀ b ↔ 1 ≀ a * b := (mul_le_mul_iff_left a).symm.trans $ by rw mul_inv_self @[to_additive] lemma le_inv_iff_mul_le_one : a ≀ b⁻¹ ↔ a * b ≀ 1 := (mul_le_mul_iff_right b).symm.trans $ by rw inv_mul_self @[simp, to_additive neg_nonpos] lemma inv_le_one' : a⁻¹ ≀ 1 ↔ 1 ≀ a := have a⁻¹ ≀ 1⁻¹ ↔ 1 ≀ a, from inv_le_inv_iff, by rwa one_inv at this @[simp, to_additive neg_nonneg] lemma one_le_inv' : 1 ≀ a⁻¹ ↔ a ≀ 1 := have 1⁻¹ ≀ a⁻¹ ↔ a ≀ 1, from inv_le_inv_iff, by rwa one_inv at this @[to_additive] lemma inv_le_self (h : 1 ≀ a) : a⁻¹ ≀ a := le_trans (inv_le_one'.2 h) h @[to_additive] lemma self_le_inv (h : a ≀ 1) : a ≀ a⁻¹ := le_trans h (one_le_inv'.2 h) @[simp, to_additive] lemma inv_lt_inv_iff : a⁻¹ < b⁻¹ ↔ b < a := have a * b * a⁻¹ < a * b * b⁻¹ ↔ a⁻¹ < b⁻¹, from mul_lt_mul_iff_left _, by { rw [mul_inv_cancel_right, mul_comm a, mul_inv_cancel_right] at this, rw [this] } @[to_additive neg_lt_zero] lemma inv_lt_one' : a⁻¹ < 1 ↔ 1 < a := have a⁻¹ < 1⁻¹ ↔ 1 < a, from inv_lt_inv_iff, by rwa one_inv at this @[to_additive neg_pos] lemma one_lt_inv' : 1 < a⁻¹ ↔ a < 1 := have 1⁻¹ < a⁻¹ ↔ a < 1, from inv_lt_inv_iff, by rwa one_inv at this @[to_additive neg_lt] lemma inv_lt' : a⁻¹ < b ↔ b⁻¹ < a := have a⁻¹ < (b⁻¹)⁻¹ ↔ b⁻¹ < a, from inv_lt_inv_iff, by rwa inv_inv at this @[to_additive lt_neg] lemma lt_inv' : a < b⁻¹ ↔ b < a⁻¹ := have (a⁻¹)⁻¹ < b⁻¹ ↔ b < a⁻¹, from inv_lt_inv_iff, by rwa inv_inv at this @[to_additive] lemma le_inv_mul_iff_mul_le : b ≀ a⁻¹ * c ↔ a * b ≀ c := have a⁻¹ * (a * b) ≀ a⁻¹ * c ↔ a * b ≀ c, from mul_le_mul_iff_left _, by rwa inv_mul_cancel_left at this @[simp, to_additive] lemma inv_mul_le_iff_le_mul : b⁻¹ * a ≀ c ↔ a ≀ b * c := have b⁻¹ * a ≀ b⁻¹ * (b * c) ↔ a ≀ b * c, from mul_le_mul_iff_left _, by rwa inv_mul_cancel_left at this @[to_additive] lemma mul_inv_le_iff_le_mul : a * c⁻¹ ≀ b ↔ a ≀ b * c := by rw [mul_comm a, mul_comm b, inv_mul_le_iff_le_mul] @[simp, to_additive] lemma mul_inv_le_iff_le_mul' : a * b⁻¹ ≀ c ↔ a ≀ b * c := by rw [← inv_mul_le_iff_le_mul, mul_comm] @[to_additive] lemma inv_mul_le_iff_le_mul' : c⁻¹ * a ≀ b ↔ a ≀ b * c := by rw [inv_mul_le_iff_le_mul, mul_comm] @[simp, to_additive] lemma lt_inv_mul_iff_mul_lt : b < a⁻¹ * c ↔ a * b < c := have a⁻¹ * (a * b) < a⁻¹ * c ↔ a * b < c, from mul_lt_mul_iff_left _, by rwa inv_mul_cancel_left at this @[simp, to_additive] lemma inv_mul_lt_iff_lt_mul : b⁻¹ * a < c ↔ a < b * c := have b⁻¹ * a < b⁻¹ * (b * c) ↔ a < b * c, from mul_lt_mul_iff_left _, by rwa inv_mul_cancel_left at this @[to_additive] lemma inv_mul_lt_iff_lt_mul_right : c⁻¹ * a < b ↔ a < b * c := by rw [inv_mul_lt_iff_lt_mul, mul_comm] @[to_additive sub_le_sub_iff] lemma div_le_div_iff' (a b c d : Ξ±) : a * b⁻¹ ≀ c * d⁻¹ ↔ a * d ≀ c * b := begin split ; intro h, have := mul_le_mul_right'' (mul_le_mul_right'' h b) d, rwa [inv_mul_cancel_right, mul_assoc _ _ b, mul_comm _ b, ← mul_assoc, inv_mul_cancel_right] at this, have := mul_le_mul_right'' (mul_le_mul_right'' h d⁻¹) b⁻¹, rwa [mul_inv_cancel_right, _root_.mul_assoc, _root_.mul_comm d⁻¹ b⁻¹, ← mul_assoc, mul_inv_cancel_right] at this, end end ordered_comm_group section ordered_add_comm_group variables [ordered_add_comm_group Ξ±] {a b c d : Ξ±} lemma sub_nonneg_of_le (h : b ≀ a) : 0 ≀ a - b := begin have h := add_le_add_right h (-b), rwa add_right_neg at h end lemma le_of_sub_nonneg (h : 0 ≀ a - b) : b ≀ a := begin have h := add_le_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_nonpos_of_le (h : a ≀ b) : a - b ≀ 0 := begin have h := add_le_add_right h (-b), rwa add_right_neg at h end lemma le_of_sub_nonpos (h : a - b ≀ 0) : a ≀ b := begin have h := add_le_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_pos_of_lt (h : b < a) : 0 < a - b := begin have h := add_lt_add_right h (-b), rwa add_right_neg at h end lemma lt_of_sub_pos (h : 0 < a - b) : b < a := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_neg_of_lt (h : a < b) : a - b < 0 := begin have h := add_lt_add_right h (-b), rwa add_right_neg at h end lemma lt_of_sub_neg (h : a - b < 0) : a < b := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma add_le_of_le_sub_left (h : b ≀ c - a) : a + b ≀ c := begin have h := add_le_add_left h a, rwa [← add_sub_assoc, add_comm a c, add_sub_cancel] at h end lemma le_sub_left_of_add_le (h : a + b ≀ c) : b ≀ c - a := begin have h := add_le_add_right h (-a), rwa [add_comm a b, add_neg_cancel_right] at h end lemma add_le_of_le_sub_right (h : a ≀ c - b) : a + b ≀ c := begin have h := add_le_add_right h b, rwa sub_add_cancel at h end lemma le_sub_right_of_add_le (h : a + b ≀ c) : a ≀ c - b := begin have h := add_le_add_right h (-b), rwa add_neg_cancel_right at h end lemma le_add_of_sub_left_le (h : a - b ≀ c) : a ≀ b + c := begin have h := add_le_add_right h b, rwa [sub_add_cancel, add_comm] at h end lemma sub_left_le_of_le_add (h : a ≀ b + c) : a - b ≀ c := begin have h := add_le_add_right h (-b), rwa [add_comm b c, add_neg_cancel_right] at h end lemma le_add_of_sub_right_le (h : a - c ≀ b) : a ≀ b + c := begin have h := add_le_add_right h c, rwa sub_add_cancel at h end lemma sub_right_le_of_le_add (h : a ≀ b + c) : a - c ≀ b := begin have h := add_le_add_right h (-c), rwa add_neg_cancel_right at h end lemma le_add_of_neg_le_sub_left (h : -a ≀ b - c) : c ≀ a + b := le_add_of_neg_add_le_left (add_le_of_le_sub_right h) lemma neg_le_sub_left_of_le_add (h : c ≀ a + b) : -a ≀ b - c := begin have h := le_neg_add_of_add_le (sub_left_le_of_le_add h), rwa add_comm at h end lemma le_add_of_neg_le_sub_right (h : -b ≀ a - c) : c ≀ a + b := le_add_of_sub_right_le (add_le_of_le_sub_left h) lemma neg_le_sub_right_of_le_add (h : c ≀ a + b) : -b ≀ a - c := le_sub_left_of_add_le (sub_right_le_of_le_add h) lemma sub_le_of_sub_le (h : a - b ≀ c) : a - c ≀ b := sub_left_le_of_le_add (le_add_of_sub_right_le h) lemma sub_le_sub_left (h : a ≀ b) (c : Ξ±) : c - b ≀ c - a := add_le_add_left (neg_le_neg h) c lemma sub_le_sub_right (h : a ≀ b) (c : Ξ±) : a - c ≀ b - c := add_le_add_right h (-c) lemma sub_le_sub (hab : a ≀ b) (hcd : c ≀ d) : a - d ≀ b - c := add_le_add hab (neg_le_neg hcd) lemma add_lt_of_lt_sub_left (h : b < c - a) : a + b < c := begin have h := add_lt_add_left h a, rwa [← add_sub_assoc, add_comm a c, add_sub_cancel] at h end lemma lt_sub_left_of_add_lt (h : a + b < c) : b < c - a := begin have h := add_lt_add_right h (-a), rwa [add_comm a b, add_neg_cancel_right] at h end lemma add_lt_of_lt_sub_right (h : a < c - b) : a + b < c := begin have h := add_lt_add_right h b, rwa sub_add_cancel at h end lemma lt_sub_right_of_add_lt (h : a + b < c) : a < c - b := begin have h := add_lt_add_right h (-b), rwa add_neg_cancel_right at h end lemma lt_add_of_sub_left_lt (h : a - b < c) : a < b + c := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, add_comm] at h end lemma sub_left_lt_of_lt_add (h : a < b + c) : a - b < c := begin have h := add_lt_add_right h (-b), rwa [add_comm b c, add_neg_cancel_right] at h end lemma lt_add_of_sub_right_lt (h : a - c < b) : a < b + c := begin have h := add_lt_add_right h c, rwa sub_add_cancel at h end lemma sub_right_lt_of_lt_add (h : a < b + c) : a - c < b := begin have h := add_lt_add_right h (-c), rwa add_neg_cancel_right at h end lemma lt_add_of_neg_lt_sub_left (h : -a < b - c) : c < a + b := lt_add_of_neg_add_lt_left (add_lt_of_lt_sub_right h) lemma neg_lt_sub_left_of_lt_add (h : c < a + b) : -a < b - c := begin have h := lt_neg_add_of_add_lt (sub_left_lt_of_lt_add h), rwa add_comm at h end lemma lt_add_of_neg_lt_sub_right (h : -b < a - c) : c < a + b := lt_add_of_sub_right_lt (add_lt_of_lt_sub_left h) lemma neg_lt_sub_right_of_lt_add (h : c < a + b) : -b < a - c := lt_sub_left_of_add_lt (sub_right_lt_of_lt_add h) lemma sub_lt_of_sub_lt (h : a - b < c) : a - c < b := sub_left_lt_of_lt_add (lt_add_of_sub_right_lt h) lemma sub_lt_sub_left (h : a < b) (c : Ξ±) : c - b < c - a := add_lt_add_left (neg_lt_neg h) c lemma sub_lt_sub_right (h : a < b) (c : Ξ±) : a - c < b - c := add_lt_add_right h (-c) lemma sub_lt_sub (hab : a < b) (hcd : c < d) : a - d < b - c := add_lt_add hab (neg_lt_neg hcd) lemma sub_lt_sub_of_le_of_lt (hab : a ≀ b) (hcd : c < d) : a - d < b - c := add_lt_add_of_le_of_lt hab (neg_lt_neg hcd) lemma sub_lt_sub_of_lt_of_le (hab : a < b) (hcd : c ≀ d) : a - d < b - c := add_lt_add_of_lt_of_le hab (neg_le_neg hcd) lemma sub_le_self (a : Ξ±) {b : Ξ±} (h : 0 ≀ b) : a - b ≀ a := calc a - b = a + -b : rfl ... ≀ a + 0 : add_le_add_left (neg_nonpos_of_nonneg h) _ ... = a : by rw add_zero lemma sub_lt_self (a : Ξ±) {b : Ξ±} (h : 0 < b) : a - b < a := calc a - b = a + -b : rfl ... < a + 0 : add_lt_add_left (neg_neg_of_pos h) _ ... = a : by rw add_zero @[simp] lemma sub_le_sub_iff_left (a : Ξ±) {b c : Ξ±} : a - b ≀ a - c ↔ c ≀ b := (add_le_add_iff_left _).trans neg_le_neg_iff @[simp] lemma sub_le_sub_iff_right (c : Ξ±) : a - c ≀ b - c ↔ a ≀ b := add_le_add_iff_right _ @[simp] lemma sub_lt_sub_iff_left (a : Ξ±) {b c : Ξ±} : a - b < a - c ↔ c < b := (add_lt_add_iff_left _).trans neg_lt_neg_iff @[simp] lemma sub_lt_sub_iff_right (c : Ξ±) : a - c < b - c ↔ a < b := add_lt_add_iff_right _ @[simp] lemma sub_nonneg : 0 ≀ a - b ↔ b ≀ a := have a - a ≀ a - b ↔ b ≀ a, from sub_le_sub_iff_left a, by rwa sub_self at this @[simp] lemma sub_nonpos : a - b ≀ 0 ↔ a ≀ b := have a - b ≀ b - b ↔ a ≀ b, from sub_le_sub_iff_right b, by rwa sub_self at this @[simp] lemma sub_pos : 0 < a - b ↔ b < a := have a - a < a - b ↔ b < a, from sub_lt_sub_iff_left a, by rwa sub_self at this @[simp] lemma sub_lt_zero : a - b < 0 ↔ a < b := have a - b < b - b ↔ a < b, from sub_lt_sub_iff_right b, by rwa sub_self at this lemma le_sub_iff_add_le' : b ≀ c - a ↔ a + b ≀ c := by rw [sub_eq_add_neg, add_comm, le_neg_add_iff_add_le] lemma le_sub_iff_add_le : a ≀ c - b ↔ a + b ≀ c := by rw [le_sub_iff_add_le', add_comm] lemma sub_le_iff_le_add' : a - b ≀ c ↔ a ≀ b + c := by rw [sub_eq_add_neg, add_comm, neg_add_le_iff_le_add] lemma sub_le_iff_le_add : a - c ≀ b ↔ a ≀ b + c := by rw [sub_le_iff_le_add', add_comm] @[simp] lemma neg_le_sub_iff_le_add : -b ≀ a - c ↔ c ≀ a + b := le_sub_iff_add_le.trans neg_add_le_iff_le_add' lemma neg_le_sub_iff_le_add' : -a ≀ b - c ↔ c ≀ a + b := by rw [neg_le_sub_iff_le_add, add_comm] lemma sub_le : a - b ≀ c ↔ a - c ≀ b := sub_le_iff_le_add'.trans sub_le_iff_le_add.symm theorem le_sub : a ≀ b - c ↔ c ≀ b - a := le_sub_iff_add_le'.trans le_sub_iff_add_le.symm lemma lt_sub_iff_add_lt' : b < c - a ↔ a + b < c := by rw [sub_eq_add_neg, add_comm, lt_neg_add_iff_add_lt] lemma lt_sub_iff_add_lt : a < c - b ↔ a + b < c := by rw [lt_sub_iff_add_lt', add_comm] lemma sub_lt_iff_lt_add' : a - b < c ↔ a < b + c := by rw [sub_eq_add_neg, add_comm, neg_add_lt_iff_lt_add] lemma sub_lt_iff_lt_add : a - c < b ↔ a < b + c := by rw [sub_lt_iff_lt_add', add_comm] @[simp] lemma neg_lt_sub_iff_lt_add : -b < a - c ↔ c < a + b := lt_sub_iff_add_lt.trans neg_add_lt_iff_lt_add_right lemma neg_lt_sub_iff_lt_add' : -a < b - c ↔ c < a + b := by rw [neg_lt_sub_iff_lt_add, add_comm] lemma sub_lt : a - b < c ↔ a - c < b := sub_lt_iff_lt_add'.trans sub_lt_iff_lt_add.symm theorem lt_sub : a < b - c ↔ c < b - a := lt_sub_iff_add_lt'.trans lt_sub_iff_add_lt.symm lemma sub_le_self_iff (a : Ξ±) {b : Ξ±} : a - b ≀ a ↔ 0 ≀ b := sub_le_iff_le_add'.trans (le_add_iff_nonneg_left _) lemma sub_lt_self_iff (a : Ξ±) {b : Ξ±} : a - b < a ↔ 0 < b := sub_lt_iff_lt_add'.trans (lt_add_iff_pos_left _) end ordered_add_comm_group /- TODO: The `add_lt_add_left` field of `ordered_add_comm_group` is redundant, and it is no longer in core so we can remove it now. This alternative constructor is a workaround until someone fixes this. -/ /-- Alternative constructor for ordered commutative groups, that avoids the field `mul_lt_mul_left`. -/ @[to_additive "Alternative constructor for ordered commutative groups, that avoids the field `mul_lt_mul_left`."] def ordered_comm_group.mk' {Ξ± : Type u} [comm_group Ξ±] [partial_order Ξ±] (mul_le_mul_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c * a ≀ c * b) : ordered_comm_group Ξ± := { mul_le_mul_left := mul_le_mul_left, ..(by apply_instance : comm_group Ξ±), ..(by apply_instance : partial_order Ξ±) } /-- A decidable linearly ordered cancellative additive commutative monoid is an additive commutative monoid with a decidable linear order in which addition is cancellative and strictly monotone. -/ @[protect_proj] class decidable_linear_ordered_cancel_add_comm_monoid (Ξ± : Type u) extends ordered_cancel_add_comm_monoid Ξ±, decidable_linear_order Ξ± section decidable_linear_ordered_cancel_add_comm_monoid variables [decidable_linear_ordered_cancel_add_comm_monoid Ξ±] lemma min_add_add_left (a b c : Ξ±) : min (a + b) (a + c) = a + min b c := eq.symm (eq_min (show a + min b c ≀ a + b, from add_le_add_left (min_le_left _ _) _) (show a + min b c ≀ a + c, from add_le_add_left (min_le_right _ _) _) (assume d, assume : d ≀ a + b, assume : d ≀ a + c, decidable.by_cases (assume : b ≀ c, by rwa [min_eq_left this]) (assume : Β¬ b ≀ c, by rwa [min_eq_right (le_of_lt (lt_of_not_ge this))]))) lemma min_add_add_right (a b c : Ξ±) : min (a + c) (b + c) = min a b + c := begin rw [add_comm a c, add_comm b c, add_comm _ c], apply min_add_add_left end lemma max_add_add_left (a b c : Ξ±) : max (a + b) (a + c) = a + max b c := eq.symm (eq_max (add_le_add_left (le_max_left _ _) _) (add_le_add_left (le_max_right _ _) _) (assume d, assume : a + b ≀ d, assume : a + c ≀ d, decidable.by_cases (assume : b ≀ c, by rwa [max_eq_right this]) (assume : Β¬ b ≀ c, by rwa [max_eq_left (le_of_lt (lt_of_not_ge this))]))) lemma max_add_add_right (a b c : Ξ±) : max (a + c) (b + c) = max a b + c := begin rw [add_comm a c, add_comm b c, add_comm _ c], apply max_add_add_left end end decidable_linear_ordered_cancel_add_comm_monoid /-- A decidable linearly ordered additive commutative group is an additive commutative group with a decidable linear order in which addition is strictly monotone. -/ @[protect_proj] class decidable_linear_ordered_add_comm_group (Ξ± : Type u) extends add_comm_group Ξ±, decidable_linear_order Ξ± := (add_le_add_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c + a ≀ c + b) instance decidable_linear_ordered_comm_group.to_ordered_add_comm_group (Ξ± : Type u) [s : decidable_linear_ordered_add_comm_group Ξ±] : ordered_add_comm_group Ξ± := { add := s.add, ..s } section decidable_linear_ordered_add_comm_group variables [decidable_linear_ordered_add_comm_group Ξ±] @[priority 100] -- see Note [lower instance priority] instance decidable_linear_ordered_add_comm_group.to_decidable_linear_ordered_cancel_add_comm_monoid : decidable_linear_ordered_cancel_add_comm_monoid Ξ± := { le_of_add_le_add_left := Ξ» x y z, le_of_add_le_add_left, add_left_cancel := Ξ» x y z, add_left_cancel, add_right_cancel := Ξ» x y z, add_right_cancel, ..β€Ήdecidable_linear_ordered_add_comm_group Ξ±β€Ί } lemma decidable_linear_ordered_add_comm_group.add_lt_add_left (a b : Ξ±) (h : a < b) (c : Ξ±) : c + a < c + b := ordered_add_comm_group.add_lt_add_left a b h c lemma max_neg_neg (a b : Ξ±) : max (-a) (-b) = - min a b := eq.symm (eq_max (show -a ≀ -(min a b), from neg_le_neg $ min_le_left a b) (show -b ≀ -(min a b), from neg_le_neg $ min_le_right a b) (assume d, assume H₁ : -a ≀ d, assume Hβ‚‚ : -b ≀ d, have H : -d ≀ min a b, from le_min (neg_le_of_neg_le H₁) (neg_le_of_neg_le Hβ‚‚), show -(min a b) ≀ d, from neg_le_of_neg_le H)) lemma min_eq_neg_max_neg_neg (a b : Ξ±) : min a b = - max (-a) (-b) := by rw [max_neg_neg, neg_neg] lemma min_neg_neg (a b : Ξ±) : min (-a) (-b) = - max a b := by rw [min_eq_neg_max_neg_neg, neg_neg, neg_neg] lemma max_eq_neg_min_neg_neg (a b : Ξ±) : max a b = - min (-a) (-b) := by rw [min_neg_neg, neg_neg] /-- `abs a` is the absolute value of `a`. -/ def abs (a : Ξ±) : Ξ± := max a (-a) lemma abs_of_nonneg {a : Ξ±} (h : 0 ≀ a) : abs a = a := have h' : -a ≀ a, from le_trans (neg_nonpos_of_nonneg h) h, max_eq_left h' lemma abs_of_pos {a : Ξ±} (h : 0 < a) : abs a = a := abs_of_nonneg (le_of_lt h) lemma abs_of_nonpos {a : Ξ±} (h : a ≀ 0) : abs a = -a := have h' : a ≀ -a, from le_trans h (neg_nonneg_of_nonpos h), max_eq_right h' lemma abs_of_neg {a : Ξ±} (h : a < 0) : abs a = -a := abs_of_nonpos (le_of_lt h) lemma abs_zero : abs 0 = (0:Ξ±) := abs_of_nonneg (le_refl _) lemma abs_neg (a : Ξ±) : abs (-a) = abs a := begin unfold abs, rw [max_comm, neg_neg] end lemma abs_pos_of_pos {a : Ξ±} (h : 0 < a) : 0 < abs a := by rwa (abs_of_pos h) lemma abs_pos_of_neg {a : Ξ±} (h : a < 0) : 0 < abs a := abs_neg a β–Έ abs_pos_of_pos (neg_pos_of_neg h) lemma abs_sub (a b : Ξ±) : abs (a - b) = abs (b - a) := by rw [← neg_sub, abs_neg] lemma ne_zero_of_abs_ne_zero {a : Ξ±} (h : abs a β‰  0) : a β‰  0 := assume ha, h (eq.symm ha β–Έ abs_zero) /- these assume a linear order -/ lemma eq_zero_of_neg_eq {a : Ξ±} (h : -a = a) : a = 0 := match lt_trichotomy a 0 with | or.inl h₁ := have 0 < a, from h β–Έ neg_pos_of_neg h₁, absurd h₁ (lt_asymm this) | or.inr (or.inl h₁) := h₁ | or.inr (or.inr h₁) := have a < 0, from h β–Έ neg_neg_of_pos h₁, absurd h₁ (lt_asymm this) end lemma abs_nonneg (a : Ξ±) : 0 ≀ abs a := or.elim (le_total 0 a) (assume h : 0 ≀ a, by rwa (abs_of_nonneg h)) (assume h : a ≀ 0, calc 0 ≀ -a : neg_nonneg_of_nonpos h ... = abs a : eq.symm (abs_of_nonpos h)) lemma abs_abs (a : Ξ±) : abs (abs a) = abs a := abs_of_nonneg $ abs_nonneg a lemma le_abs_self (a : Ξ±) : a ≀ abs a := or.elim (le_total 0 a) (assume h : 0 ≀ a, begin rw [abs_of_nonneg h] end) (assume h : a ≀ 0, le_trans h $ abs_nonneg a) lemma neg_le_abs_self (a : Ξ±) : -a ≀ abs a := abs_neg a β–Έ le_abs_self (-a) lemma eq_zero_of_abs_eq_zero {a : Ξ±} (h : abs a = 0) : a = 0 := have h₁ : a ≀ 0, from h β–Έ le_abs_self a, have hβ‚‚ : -a ≀ 0, from h β–Έ abs_neg a β–Έ le_abs_self (-a), le_antisymm h₁ (nonneg_of_neg_nonpos hβ‚‚) lemma eq_of_abs_sub_eq_zero {a b : Ξ±} (h : abs (a - b) = 0) : a = b := have a - b = 0, from eq_zero_of_abs_eq_zero h, show a = b, from eq_of_sub_eq_zero this lemma abs_pos_of_ne_zero {a : Ξ±} (h : a β‰  0) : 0 < abs a := or.elim (lt_or_gt_of_ne h) abs_pos_of_neg abs_pos_of_pos lemma abs_by_cases (P : Ξ± β†’ Prop) {a : Ξ±} (h1 : P a) (h2 : P (-a)) : P (abs a) := or.elim (le_total 0 a) (assume h : 0 ≀ a, eq.symm (abs_of_nonneg h) β–Έ h1) (assume h : a ≀ 0, eq.symm (abs_of_nonpos h) β–Έ h2) lemma abs_le_of_le_of_neg_le {a b : Ξ±} (h1 : a ≀ b) (h2 : -a ≀ b) : abs a ≀ b := abs_by_cases (Ξ» x : Ξ±, x ≀ b) h1 h2 lemma abs_lt_of_lt_of_neg_lt {a b : Ξ±} (h1 : a < b) (h2 : -a < b) : abs a < b := abs_by_cases (Ξ» x : Ξ±, x < b) h1 h2 private lemma aux1 {a b : Ξ±} (h1 : 0 ≀ a + b) (h2 : 0 ≀ a) : abs (a + b) ≀ abs a + abs b := decidable.by_cases (assume h3 : 0 ≀ b, calc abs (a + b) ≀ abs (a + b) : by apply le_refl ... = a + b : by rw (abs_of_nonneg h1) ... = abs a + b : by rw (abs_of_nonneg h2) ... = abs a + abs b : by rw (abs_of_nonneg h3)) (assume h3 : Β¬ 0 ≀ b, have h4 : b ≀ 0, from le_of_lt (lt_of_not_ge h3), calc abs (a + b) = a + b : by rw (abs_of_nonneg h1) ... = abs a + b : by rw (abs_of_nonneg h2) ... ≀ abs a + 0 : add_le_add_left h4 _ ... ≀ abs a + -b : add_le_add_left (neg_nonneg_of_nonpos h4) _ ... = abs a + abs b : by rw (abs_of_nonpos h4)) private lemma aux2 {a b : Ξ±} (h1 : 0 ≀ a + b) : abs (a + b) ≀ abs a + abs b := or.elim (le_total b 0) (assume h2 : b ≀ 0, have h3 : Β¬ a < 0, from assume h4 : a < 0, have h5 : a + b < 0, begin have aux := add_lt_add_of_lt_of_le h4 h2, rwa [add_zero] at aux end, not_lt_of_ge h1 h5, aux1 h1 (le_of_not_gt h3)) (assume h2 : 0 ≀ b, begin have h3 : abs (b + a) ≀ abs b + abs a, begin rw add_comm at h1, exact aux1 h1 h2 end, rw [add_comm, add_comm (abs a)], exact h3 end) lemma abs_add_le_abs_add_abs (a b : Ξ±) : abs (a + b) ≀ abs a + abs b := or.elim (le_total 0 (a + b)) (assume h2 : 0 ≀ a + b, aux2 h2) (assume h2 : a + b ≀ 0, have h3 : -a + -b = -(a + b), by rw neg_add, have h4 : 0 ≀ -(a + b), from neg_nonneg_of_nonpos h2, have h5 : 0 ≀ -a + -b, begin rw [← h3] at h4, exact h4 end, calc abs (a + b) = abs (-a + -b) : by rw [← abs_neg, neg_add] ... ≀ abs (-a) + abs (-b) : aux2 h5 ... = abs a + abs b : by rw [abs_neg, abs_neg]) lemma abs_sub_abs_le_abs_sub (a b : Ξ±) : abs a - abs b ≀ abs (a - b) := have h1 : abs a - abs b + abs b ≀ abs (a - b) + abs b, from calc abs a - abs b + abs b = abs a : by rw sub_add_cancel ... = abs (a - b + b) : by rw sub_add_cancel ... ≀ abs (a - b) + abs b : by apply abs_add_le_abs_add_abs, le_of_add_le_add_right h1 lemma abs_sub_le (a b c : Ξ±) : abs (a - c) ≀ abs (a - b) + abs (b - c) := calc abs (a - c) = abs (a - b + (b - c)) : by rw [sub_eq_add_neg, sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left] ... ≀ abs (a - b) + abs (b - c) : by apply abs_add_le_abs_add_abs lemma abs_add_three (a b c : Ξ±) : abs (a + b + c) ≀ abs a + abs b + abs c := begin apply le_trans, apply abs_add_le_abs_add_abs, apply le_trans, apply add_le_add_right, apply abs_add_le_abs_add_abs, apply le_refl end lemma dist_bdd_within_interval {a b lb ub : Ξ±} (hal : lb ≀ a) (hau : a ≀ ub) (hbl : lb ≀ b) (hbu : b ≀ ub) : abs (a - b) ≀ ub - lb := begin cases (decidable.em (b ≀ a)) with hba hba, rw (abs_of_nonneg (sub_nonneg_of_le hba)), apply sub_le_sub, apply hau, apply hbl, rw [abs_of_neg (sub_neg_of_lt (lt_of_not_ge hba)), neg_sub], apply sub_le_sub, apply hbu, apply hal end lemma decidable_linear_ordered_add_comm_group.eq_of_abs_sub_nonpos {a b : Ξ±} (h : abs (a - b) ≀ 0) : a = b := eq_of_abs_sub_eq_zero (le_antisymm h (abs_nonneg (a - b))) end decidable_linear_ordered_add_comm_group set_option old_structure_cmd true section prio set_option default_priority 100 -- see Note [default priority] /-- This is not so much a new structure as a construction mechanism for ordered groups, by specifying only the "positive cone" of the group. -/ class nonneg_add_comm_group (Ξ± : Type*) extends add_comm_group Ξ± := (nonneg : Ξ± β†’ Prop) (pos : Ξ± β†’ Prop := Ξ» a, nonneg a ∧ Β¬ nonneg (neg a)) (pos_iff : βˆ€ a, pos a ↔ nonneg a ∧ Β¬ nonneg (-a) . order_laws_tac) (zero_nonneg : nonneg 0) (add_nonneg : βˆ€ {a b}, nonneg a β†’ nonneg b β†’ nonneg (a + b)) (nonneg_antisymm : βˆ€ {a}, nonneg a β†’ nonneg (-a) β†’ a = 0) end prio namespace nonneg_add_comm_group variable [s : nonneg_add_comm_group Ξ±] include s @[reducible, priority 100] -- see Note [lower instance priority] instance to_ordered_add_comm_group : ordered_add_comm_group Ξ± := { le := Ξ» a b, nonneg (b - a), lt := Ξ» a b, pos (b - a), lt_iff_le_not_le := Ξ» a b, by simp; rw [pos_iff]; simp, le_refl := Ξ» a, by simp [zero_nonneg], le_trans := Ξ» a b c nab nbc, by simp [-sub_eq_add_neg]; rw ← sub_add_sub_cancel; exact add_nonneg nbc nab, le_antisymm := Ξ» a b nab nba, eq_of_sub_eq_zero $ nonneg_antisymm nba (by rw neg_sub; exact nab), add_le_add_left := Ξ» a b nab c, by simpa [(≀), preorder.le] using nab, ..s } theorem nonneg_def {a : Ξ±} : nonneg a ↔ 0 ≀ a := show _ ↔ nonneg _, by simp theorem pos_def {a : Ξ±} : pos a ↔ 0 < a := show _ ↔ pos _, by simp theorem not_zero_pos : Β¬ pos (0 : Ξ±) := mt pos_def.1 (lt_irrefl _) theorem zero_lt_iff_nonneg_nonneg {a : Ξ±} : 0 < a ↔ nonneg a ∧ Β¬ nonneg (-a) := pos_def.symm.trans (pos_iff _) theorem nonneg_total_iff : (βˆ€ a : Ξ±, nonneg a ∨ nonneg (-a)) ↔ (βˆ€ a b : Ξ±, a ≀ b ∨ b ≀ a) := ⟨λ h a b, by have := h (b - a); rwa [neg_sub] at this, Ξ» h a, by rw [nonneg_def, nonneg_def, neg_nonneg]; apply h⟩ /-- A `nonneg_add_comm_group` is a `decidable_linear_ordered_add_comm_group` if `nonneg` is total and decidable. -/ def to_decidable_linear_ordered_add_comm_group [decidable_pred (@nonneg Ξ± _)] (nonneg_total : βˆ€ a : Ξ±, nonneg a ∨ nonneg (-a)) : decidable_linear_ordered_add_comm_group Ξ± := { le := (≀), lt := (<), lt_iff_le_not_le := @lt_iff_le_not_le _ _, le_refl := @le_refl _ _, le_trans := @le_trans _ _, le_antisymm := @le_antisymm _ _, le_total := nonneg_total_iff.1 nonneg_total, decidable_le := by apply_instance, decidable_lt := by apply_instance, ..@nonneg_add_comm_group.to_ordered_add_comm_group _ s } end nonneg_add_comm_group namespace order_dual instance [ordered_add_comm_monoid Ξ±] : ordered_add_comm_monoid (order_dual Ξ±) := { add_le_add_left := Ξ» a b h c, @add_le_add_left' Ξ± _ b a c h, lt_of_add_lt_add_left := Ξ» a b c h, @lt_of_add_lt_add_left' Ξ± _ a c b h, ..order_dual.partial_order Ξ±, ..show add_comm_monoid Ξ±, by apply_instance } instance [ordered_cancel_add_comm_monoid Ξ±] : ordered_cancel_add_comm_monoid (order_dual Ξ±) := { le_of_add_le_add_left := Ξ» a b c : Ξ±, le_of_add_le_add_left, add_left_cancel := @add_left_cancel Ξ± _, add_right_cancel := @add_right_cancel Ξ± _, ..order_dual.ordered_add_comm_monoid } instance [ordered_add_comm_group Ξ±] : ordered_add_comm_group (order_dual Ξ±) := { add_left_neg := Ξ» a : Ξ±, add_left_neg a, ..order_dual.ordered_add_comm_monoid, ..show add_comm_group Ξ±, by apply_instance } end order_dual
c109f95ac5b4fb9429413610ce4e047fc2e423b2
4727251e0cd73359b15b664c3170e5d754078599
/src/data/polynomial/cardinal.lean
25880dfa7c6ed54ea641568740ec14b507a6c560
[ "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
789
lean
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.mv_polynomial.cardinal import data.mv_polynomial.equiv /-! # Cardinality of Polynomial Ring The reuslt in this file is that the cardinality of `polynomial R` is at most the maximum of `#R` and `Ο‰`. -/ universe u open_locale cardinal polynomial open cardinal namespace polynomial lemma cardinal_mk_le_max {R : Type u} [comm_semiring R] : #R[X] ≀ max (#R) Ο‰ := calc #R[X] = #(mv_polynomial punit.{u + 1} R) : cardinal.eq.2 ⟨(mv_polynomial.punit_alg_equiv.{u u} R).to_equiv.symm⟩ ... ≀ _ : mv_polynomial.cardinal_mk_le_max ... ≀ _ : by rw [max_assoc, max_eq_right (lt_omega_of_fintype punit).le] end polynomial
d88086eaae21944c2698ece102832d7c1f4f40c0
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/category_theory/hom_functor.lean
26ea273aa570a0e078a2377dd6b8a3a061605f0b
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
929
lean
/- Copyright (c) 2018 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Scott Morrison -/ import category_theory.opposites import category_theory.products.basic /-! The hom functor, sending `(X, Y)` to the type `X ⟢ Y`. -/ universes v u open opposite open category_theory namespace category_theory.functor variables (C : Type u) [π’ž : category.{v} C] include π’ž /-- `functor.hom` is the hom-pairing, sending (X,Y) to X β†’ Y, contravariant in X and covariant in Y. -/ definition hom : Cα΅’α΅– Γ— C β₯€ Type v := { obj := Ξ» p, unop p.1 ⟢ p.2, map := Ξ» X Y f, Ξ» h, f.1.unop ≫ h ≫ f.2 } @[simp] lemma hom_obj (X : Cα΅’α΅– Γ— C) : (hom C).obj X = (unop X.1 ⟢ X.2) := rfl @[simp] lemma hom_pairing_map {X Y : Cα΅’α΅– Γ— C} (f : X ⟢ Y) : (hom C).map f = Ξ» h, f.1.unop ≫ h ≫ f.2 := rfl end category_theory.functor
bab4ebb5de63bb6e2d2f674739383137bdcda0db
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Meta/Match/Match.lean
ce358a32cda6bf2e5c8d689f4af37c5469343432
[ "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
42,657
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.Util.CollectLevelParams import Lean.Util.Recognizers import Lean.Meta.Check import Lean.Meta.Closure import Lean.Meta.Tactic.Cases import Lean.Meta.GeneralizeTelescope import Lean.Meta.Match.MVarRenaming import Lean.Meta.Match.CaseValues import Lean.Meta.Match.CaseArraySizes namespace Lean namespace Meta namespace Match inductive Pattern : Type | inaccessible (e : Expr) : Pattern | var (fvarId : FVarId) : Pattern | ctor (ctorName : Name) (us : List Level) (params : List Expr) (fields : List Pattern) : Pattern | val (e : Expr) : Pattern | arrayLit (type : Expr) (xs : List Pattern) : Pattern | as (varId : FVarId) (p : Pattern) : Pattern namespace Pattern instance : Inhabited Pattern := ⟨Pattern.inaccessible (arbitrary _)⟩ partial def toMessageData : Pattern β†’ MessageData | inaccessible e => ".(" ++ e ++ ")" | var varId => mkFVar varId | ctor ctorName _ _ [] => ctorName | ctor ctorName _ _ pats => "(" ++ ctorName ++ pats.foldl (fun (msg : MessageData) pat => msg ++ " " ++ toMessageData pat) Format.nil ++ ")" | val e => e | arrayLit _ pats => "#[" ++ MessageData.joinSep (pats.map toMessageData) ", " ++ "]" | as varId p => mkFVar varId ++ "@" ++toMessageData p partial def toExpr : Pattern β†’ MetaM Expr | inaccessible e => pure e | var fvarId => pure $ mkFVar fvarId | val e => pure e | as _ p => toExpr p | arrayLit type xs => do xs ← xs.mapM toExpr; mkArrayLit type xs | ctor ctorName us params fields => do fields ← fields.mapM toExpr; pure $ mkAppN (mkConst ctorName us) (params ++ fields).toArray /- Apply the free variable substitution `s` to the given pattern -/ partial def applyFVarSubst (s : FVarSubst) : Pattern β†’ Pattern | inaccessible e => inaccessible $ s.apply e | ctor n us ps fs => ctor n us (ps.map s.apply) $ fs.map applyFVarSubst | val e => val $ s.apply e | arrayLit t xs => arrayLit (s.apply t) $ xs.map applyFVarSubst | var fvarId => match s.find? fvarId with | some e => inaccessible e | none => var fvarId | as fvarId p => match s.find? fvarId with | none => as fvarId $ applyFVarSubst p | some _ => applyFVarSubst p def replaceFVarId (fvarId : FVarId) (v : Expr) (p : Pattern) : Pattern := let s : FVarSubst := {}; p.applyFVarSubst (s.insert fvarId v) end Pattern structure AltLHS := (ref : Syntax) (fvarDecls : List LocalDecl) -- Free variables used in the patterns. (patterns : List Pattern) -- We use `List Pattern` since we have nary match-expressions. structure Alt := (ref : Syntax) (idx : Nat) -- for generating error messages (rhs : Expr) (fvarDecls : List LocalDecl) (patterns : List Pattern) namespace Alt instance : Inhabited Alt := ⟨⟨arbitrary _, 0, arbitrary _, [], []⟩⟩ partial def toMessageData (alt : Alt) : MetaM MessageData := do withExistingLocalDecls alt.fvarDecls do let msg : List MessageData := alt.fvarDecls.map fun d => d.toExpr ++ ":(" ++ d.type ++ ")"; let msg : MessageData := msg ++ " |- " ++ (alt.patterns.map Pattern.toMessageData) ++ " => " ++ alt.rhs; addMessageContext msg def applyFVarSubst (s : FVarSubst) (alt : Alt) : Alt := { alt with patterns := alt.patterns.map fun p => p.applyFVarSubst s, fvarDecls := alt.fvarDecls.map fun d => d.applyFVarSubst s, rhs := alt.rhs.applyFVarSubst s } def replaceFVarId (fvarId : FVarId) (v : Expr) (alt : Alt) : Alt := { alt with patterns := alt.patterns.map fun p => p.replaceFVarId fvarId v, fvarDecls := let decls := alt.fvarDecls.filter fun d => d.fvarId != fvarId; decls.map $ replaceFVarIdAtLocalDecl fvarId v, rhs := alt.rhs.replaceFVarId fvarId v } /- Similar to `checkAndReplaceFVarId`, but ensures type of `v` is definitionally equal to type of `fvarId`. This extra check is necessary when performing dependent elimination and inaccessible terms have been used. For example, consider the following code fragment: ``` inductive Vec (Ξ± : Type u) : Nat β†’ Type u | nil : Vec Ξ± 0 | cons {n} (head : Ξ±) (tail : Vec Ξ± n) : Vec Ξ± (n+1) inductive VecPred {Ξ± : Type u} (P : Ξ± β†’ Prop) : {n : Nat} β†’ Vec Ξ± n β†’ Prop | nil : VecPred P Vec.nil | cons {n : Nat} {head : Ξ±} {tail : Vec Ξ± n} : P head β†’ VecPred P tail β†’ VecPred P (Vec.cons head tail) theorem ex {Ξ± : Type u} (P : Ξ± β†’ Prop) : {n : Nat} β†’ (v : Vec Ξ± (n+1)) β†’ VecPred P v β†’ Exists P | _, Vec.cons head _, VecPred.cons h (w : VecPred P Vec.nil) => ⟨head, h⟩ ``` Recall that `_` in a pattern can be elaborated into pattern variable or an inaccessible term. The elaborator uses an inaccessible term when typing constraints restrict its value. Thus, in the example above, the `_` at `Vec.cons head _` becomes the inaccessible pattern `.(Vec.nil)` because the type ascription `(w : VecPred P Vec.nil)` propagates typing constraints that restrict its value to be `Vec.nil`. After elaboration the alternative becomes: ``` | .(0), @Vec.cons .(Ξ±) .(0) head .(Vec.nil), @VecPred.cons .(Ξ±) .(P) .(0) .(head) .(Vec.nil) h w => ⟨head, h⟩ ``` where ``` (head : Ξ±), (h: P head), (w : VecPred P Vec.nil) ``` Then, when we process this alternative in this module, the following check will detect that `w` has type `VecPred P Vec.nil`, when it is supposed to have type `VecPred P tail`. Note that if we had written ``` theorem ex {Ξ± : Type u} (P : Ξ± β†’ Prop) : {n : Nat} β†’ (v : Vec Ξ± (n+1)) β†’ VecPred P v β†’ Exists P | _, Vec.cons head Vec.nil, VecPred.cons h (w : VecPred P Vec.nil) => ⟨head, h⟩ ``` we would get the easier to digest error message ``` missing cases: _, (Vec.cons _ _ (Vec.cons _ _ _)), _ ``` -/ def checkAndReplaceFVarId (fvarId : FVarId) (v : Expr) (alt : Alt) : MetaM Alt := do match alt.fvarDecls.find? fun (fvarDecl : LocalDecl) => fvarDecl.fvarId == fvarId with | none => throwErrorAt alt.ref "unknown free pattern variable" | some fvarDecl => do vType ← inferType v; unlessM (isDefEqGuarded fvarDecl.type vType) $ withExistingLocalDecls alt.fvarDecls $ throwErrorAt alt.ref $ "type mismatch during dependent match-elimination at pattern variable '" ++ mkFVar fvarDecl.fvarId ++ "' with type" ++ indentExpr fvarDecl.type ++ Format.line ++ "expected type" ++ indentExpr vType; pure $ replaceFVarId fvarId v alt end Alt inductive Example | var : FVarId β†’ Example | underscore : Example | ctor : Name β†’ List Example β†’ Example | val : Expr β†’ Example | arrayLit : List Example β†’ Example namespace Example partial def replaceFVarId (fvarId : FVarId) (ex : Example) : Example β†’ Example | var x => if x == fvarId then ex else var x | ctor n exs => ctor n $ exs.map replaceFVarId | arrayLit exs => arrayLit $ exs.map replaceFVarId | ex => ex partial def applyFVarSubst (s : FVarSubst) : Example β†’ Example | var fvarId => match s.get fvarId with | Expr.fvar fvarId' _ => var fvarId' | _ => underscore | ctor n exs => ctor n $ exs.map applyFVarSubst | arrayLit exs => arrayLit $ exs.map applyFVarSubst | ex => ex partial def varsToUnderscore : Example β†’ Example | var x => underscore | ctor n exs => ctor n $ exs.map varsToUnderscore | arrayLit exs => arrayLit $ exs.map varsToUnderscore | ex => ex partial def toMessageData : Example β†’ MessageData | var fvarId => mkFVar fvarId | ctor ctorName [] => mkConst ctorName | ctor ctorName exs => "(" ++ mkConst ctorName ++ exs.foldl (fun (msg : MessageData) pat => msg ++ " " ++ toMessageData pat) Format.nil ++ ")" | arrayLit exs => "#" ++ MessageData.ofList (exs.map toMessageData) | val e => e | underscore => "_" end Example def examplesToMessageData (cex : List Example) : MessageData := MessageData.joinSep (cex.map (Example.toMessageData ∘ Example.varsToUnderscore)) ", " structure Problem := (mvarId : MVarId) (vars : List Expr) (alts : List Alt) (examples : List Example) def withGoalOf {Ξ±} (p : Problem) (x : MetaM Ξ±) : MetaM Ξ± := withMVarContext p.mvarId x namespace Problem instance : Inhabited Problem := ⟨{ mvarId := arbitrary _, vars := [], alts := [], examples := []}⟩ def toMessageData (p : Problem) : MetaM MessageData := withGoalOf p do alts ← p.alts.mapM Alt.toMessageData; vars : List MessageData ← p.vars.mapM fun x => do { xType ← inferType x; pure (x ++ ":(" ++ xType ++ ")" : MessageData) }; pure $ "vars " ++ vars -- ++ Format.line ++ "var ids " ++ toString (p.vars.map (fun x => match x with | Expr.fvar id _ => toString id | _ => "[nonvar]")) ++ Format.line ++ MessageData.joinSep alts Format.line ++ Format.line ++ "examples: " ++ examplesToMessageData p.examples ++ Format.line end Problem abbrev CounterExample := List Example def counterExampleToMessageData (cex : CounterExample) : MessageData := examplesToMessageData cex def counterExamplesToMessageData (cexs : List CounterExample) : MessageData := MessageData.joinSep (cexs.map counterExampleToMessageData) Format.line structure MatcherResult := (matcher : Expr) -- The matcher. It is not just `Expr.const matcherName` because the type of the major premises may contain free variables. (counterExamples : List CounterExample) (unusedAltIdxs : List Nat) /- The number of patterns in each AltLHS must be equal to majors.length -/ private def checkNumPatterns (majors : Array Expr) (lhss : List AltLHS) : MetaM Unit := let num := majors.size; when (lhss.any (fun lhs => lhs.patterns.length != num)) $ throwError "incorrect number of patterns" private partial def withAltsAux {Ξ±} (motive : Expr) : List AltLHS β†’ List Alt β†’ Array (Expr Γ— Nat) β†’ (List Alt β†’ Array (Expr Γ— Nat) β†’ MetaM Ξ±) β†’ MetaM Ξ± | [], alts, minors, k => k alts.reverse minors | lhs::lhss, alts, minors, k => do let xs := lhs.fvarDecls.toArray.map LocalDecl.toExpr; minorType ← withExistingLocalDecls lhs.fvarDecls do { args ← lhs.patterns.toArray.mapM Pattern.toExpr; let minorType := mkAppN motive args; mkForallFVars xs minorType }; let (minorType, minorNumParams) := if !xs.isEmpty then (minorType, xs.size) else (mkThunkType minorType, 1); let idx := alts.length; let minorName := (`h).appendIndexAfter (idx+1); trace! `Meta.Match.debug ("minor premise " ++ minorName ++ " : " ++ minorType); withLocalDeclD minorName minorType fun minor => do let rhs := if xs.isEmpty then mkApp minor (mkConst `Unit.unit) else mkAppN minor xs; let minors := minors.push (minor, minorNumParams); fvarDecls ← lhs.fvarDecls.mapM instantiateLocalDeclMVars; let alts := { ref := lhs.ref, idx := idx, rhs := rhs, fvarDecls := fvarDecls, patterns := lhs.patterns : Alt } :: alts; withAltsAux lhss alts minors k /- Given a list of `AltLHS`, create a minor premise for each one, convert them into `Alt`, and then execute `k` -/ private partial def withAlts {Ξ±} (motive : Expr) (lhss : List AltLHS) (k : List Alt β†’ Array (Expr Γ— Nat) β†’ MetaM Ξ±) : MetaM Ξ± := withAltsAux motive lhss [] #[] k def assignGoalOf (p : Problem) (e : Expr) : MetaM Unit := withGoalOf p (assignExprMVar p.mvarId e) structure State := (used : Std.HashSet Nat := {}) -- used alternatives (counterExamples : List (List Example) := []) /-- Return true if the given (sub-)problem has been solved. -/ private def isDone (p : Problem) : Bool := p.vars.isEmpty /-- Return true if the next element on the `p.vars` list is a variable. -/ private def isNextVar (p : Problem) : Bool := match p.vars with | Expr.fvar _ _ :: _ => true | _ => false private def hasAsPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.as _ _ :: _ => true | _ => false private def hasCtorPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.ctor _ _ _ _ :: _ => true | _ => false private def hasValPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.val _ :: _ => true | _ => false private def hasNatValPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.val v :: _ => v.isNatLit | _ => false private def hasVarPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.var _ :: _ => true | _ => false private def hasArrayLitPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.arrayLit _ _ :: _ => true | _ => false private def isVariableTransition (p : Problem) : Bool := p.alts.all fun alt => match alt.patterns with | Pattern.inaccessible _ :: _ => true | Pattern.var _ :: _ => true | _ => false private def isConstructorTransition (p : Problem) : Bool := (hasCtorPattern p || p.alts.isEmpty) && p.alts.all fun alt => match alt.patterns with | Pattern.ctor _ _ _ _ :: _ => true | Pattern.var _ :: _ => true | Pattern.inaccessible _ :: _ => true | _ => false private def isValueTransition (p : Problem) : Bool := hasVarPattern p && hasValPattern p && p.alts.all fun alt => match alt.patterns with | Pattern.val _ :: _ => true | Pattern.var _ :: _ => true | _ => false private def isArrayLitTransition (p : Problem) : Bool := hasArrayLitPattern p && hasVarPattern p && p.alts.all fun alt => match alt.patterns with | Pattern.arrayLit _ _ :: _ => true | Pattern.var _ :: _ => true | _ => false private def isNatValueTransition (p : Problem) : Bool := hasNatValPattern p && (!isNextVar p || p.alts.any fun alt => match alt.patterns with | Pattern.ctor _ _ _ _ :: _ => true | Pattern.inaccessible _ :: _ => true | _ => false) private def processSkipInaccessible (p : Problem) : Problem := match p.vars with | [] => unreachable! | x :: xs => do let alts := p.alts.map fun alt => match alt.patterns with | Pattern.inaccessible _ :: ps => { alt with patterns := ps } | _ => unreachable!; { p with alts := alts, vars := xs } private def processLeaf (p : Problem) : StateRefT State MetaM Unit := match p.alts with | [] => do liftM $ admit p.mvarId; modify fun s => { s with counterExamples := p.examples :: s.counterExamples } | alt :: _ => do -- TODO: check whether we have unassigned metavars in rhs liftM $ assignGoalOf p alt.rhs; modify fun s => { s with used := s.used.insert alt.idx } private def processAsPattern (p : Problem) : MetaM Problem := match p.vars with | [] => unreachable! | x :: xs => withGoalOf p do alts ← p.alts.mapM fun alt => match alt.patterns with | Pattern.as fvarId p :: ps => { alt with patterns := p :: ps }.checkAndReplaceFVarId fvarId x | _ => pure alt; pure { p with alts := alts } private def processVariable (p : Problem) : MetaM Problem := match p.vars with | [] => unreachable! | x :: xs => withGoalOf p do alts ← p.alts.mapM fun alt => match alt.patterns with | Pattern.inaccessible _ :: ps => pure { alt with patterns := ps } | Pattern.var fvarId :: ps => { alt with patterns := ps }.checkAndReplaceFVarId fvarId x | _ => unreachable!; pure { p with alts := alts, vars := xs } private def throwInductiveTypeExpected {Ξ±} (e : Expr) : MetaM Ξ± := do t ← inferType e; throwError ("failed to compile pattern matching, inductive type expected" ++ indentExpr e ++ Format.line ++ "has type" ++ indentExpr t) private def inLocalDecls (localDecls : List LocalDecl) (fvarId : FVarId) : Bool := localDecls.any fun d => d.fvarId == fvarId namespace Unify structure Context := (altFVarDecls : List LocalDecl) structure State := (fvarSubst : FVarSubst := {}) abbrev M := ReaderT Context $ StateRefT State MetaM def isAltVar (fvarId : FVarId) : M Bool := do ctx ← read; pure $ inLocalDecls ctx.altFVarDecls fvarId def expandIfVar (e : Expr) : M Expr := do match e with | Expr.fvar _ _ => do s ← get; pure $ s.fvarSubst.apply e | _ => pure e def occurs (fvarId : FVarId) (v : Expr) : Bool := (v.find? fun e => match e with | Expr.fvar fvarId' _ => fvarId == fvarId' | _=> false).isSome def assign (fvarId : FVarId) (v : Expr) : M Bool := if occurs fvarId v then do trace! `Meta.Match.unify ("assign occurs check failed, " ++ mkFVar fvarId ++ " := " ++ v); pure false else do ctx ← read; condM (isAltVar fvarId) (do trace! `Meta.Match.unify (mkFVar fvarId ++ " := " ++ v); modify fun s => { s with fvarSubst := s.fvarSubst.insert fvarId v }; pure true) (do trace! `Meta.Match.unify ("assign failed variable is not local, " ++ mkFVar fvarId ++ " := " ++ v); pure false) partial def unify : Expr β†’ Expr β†’ M Bool | a, b => do trace! `Meta.Match.unify (a ++ " =?= " ++ b); condM (isDefEq a b) (pure true) do a' ← expandIfVar a; b' ← expandIfVar b; if a != a' || b != b' then unify a' b' else match a, b with | Expr.mdata _ a _, b => unify a b | a, Expr.mdata _ b _ => unify a b | Expr.fvar aFvarId _, Expr.fvar bFVarId _ => assign aFvarId b <||> assign bFVarId a | Expr.fvar aFvarId _, b => assign aFvarId b | a, Expr.fvar bFVarId _ => assign bFVarId a | Expr.app aFn aArg _, Expr.app bFn bArg _ => unify aFn bFn <&&> unify aArg bArg | _, _ => do trace! `Meta.Match.unify ("unify failed @" ++ a ++ " =?= " ++ b); pure false end Unify private def unify? (altFVarDecls : List LocalDecl) (a b : Expr) : MetaM (Option FVarSubst) := do a ← instantiateMVars a; b ← instantiateMVars b; (b, s) ← (Unify.unify a b { altFVarDecls := altFVarDecls}).run {}; if b then pure s.fvarSubst else pure none private def expandVarIntoCtor? (alt : Alt) (fvarId : FVarId) (ctorName : Name) : MetaM (Option Alt) := withExistingLocalDecls alt.fvarDecls do env ← getEnv; ldecl ← getLocalDecl fvarId; expectedType ← inferType (mkFVar fvarId); expectedType ← whnfD expectedType; (ctorLevels, ctorParams) ← getInductiveUniverseAndParams expectedType; let ctor := mkAppN (mkConst ctorName ctorLevels) ctorParams; ctorType ← inferType ctor; forallTelescopeReducing ctorType fun ctorFields resultType => do let ctor := mkAppN ctor ctorFields; let alt := alt.replaceFVarId fvarId ctor; ctorFieldDecls ← ctorFields.mapM fun ctorField => getLocalDecl ctorField.fvarId!; let newAltDecls := ctorFieldDecls.toList ++ alt.fvarDecls; subst? ← unify? newAltDecls resultType expectedType; match subst? with | none => pure none | some subst => do let newAltDecls := newAltDecls.filter fun d => !subst.contains d.fvarId; -- remove declarations that were assigned let newAltDecls := newAltDecls.map fun d => d.applyFVarSubst subst; -- apply substitution to remaining declaration types let patterns := alt.patterns.map fun p => p.applyFVarSubst subst; let rhs := subst.apply alt.rhs; let ctorFieldPatterns := ctorFields.toList.map fun ctorField => match subst.get ctorField.fvarId! with | e@(Expr.fvar fvarId _) => if inLocalDecls newAltDecls fvarId then Pattern.var fvarId else Pattern.inaccessible e | e => Pattern.inaccessible e; pure $ some { alt with fvarDecls := newAltDecls, rhs := rhs, patterns := ctorFieldPatterns ++ patterns } private def getInductiveVal? (x : Expr) : MetaM (Option InductiveVal) := do xType ← inferType x; xType ← whnfD xType; match xType.getAppFn with | Expr.const constName _ _ => do cinfo ← getConstInfo constName; match cinfo with | ConstantInfo.inductInfo val => pure (some val) | _ => pure none | _ => pure none private def hasRecursiveType (x : Expr) : MetaM Bool := do val? ← getInductiveVal? x; match val? with | some val => pure val.isRec | _ => pure false /- Given `alt` s.t. the next pattern is an inaccessible pattern `e`, try to normalize `e` into a constructor application. If it is not a constructor, throw an error. Otherwise, if it is a constructor application of `ctorName`, update the next patterns with the fields of the constructor. Otherwise, return none. -/ def processInaccessibleAsCtor (alt : Alt) (ctorName : Name) : MetaM (Option Alt) := do env ← getEnv; match alt.patterns with | p@(Pattern.inaccessible e) :: ps => do trace! `Meta.Match.match ("inaccessible in ctor step " ++ e); withExistingLocalDecls alt.fvarDecls do -- Try to push inaccessible annotations. e ← whnfD e; match e.constructorApp? env with | some (ctorVal, ctorArgs) => do if ctorVal.name == ctorName then let fields := ctorArgs.extract ctorVal.nparams ctorArgs.size; let fields := fields.toList.map Pattern.inaccessible; pure $ some { alt with patterns := fields ++ ps } else pure none | _ => throwErrorAt alt.ref $ "dependent match elimination failed, inaccessible pattern found " ++ indentD p.toMessageData ++ Format.line ++ "constructor expected" | _ => unreachable! private def processConstructor (p : Problem) : MetaM (Array Problem) := do trace! `Meta.Match.match ("constructor step"); env ← getEnv; match p.vars with | [] => unreachable! | x :: xs => do subgoals? ← commitWhenSome? do { subgoals ← cases p.mvarId x.fvarId!; if subgoals.isEmpty then /- Easy case: we have solved problem `p` since there are no subgoals -/ pure (some #[]) else if !p.alts.isEmpty then pure (some subgoals) else do isRec ← withGoalOf p $ hasRecursiveType x; /- If there are no alternatives and the type of the current variable is recursive, we do NOT consider a constructor-transition to avoid nontermination. TODO: implement a more general approach if this is not sufficient in practice -/ if isRec then pure none else pure (some subgoals) }; match subgoals? with | none => pure #[{ p with vars := xs }] | some subgoals => do subgoals.mapM fun subgoal => withMVarContext subgoal.mvarId do let subst := subgoal.subst; let fields := subgoal.fields.toList; let newVars := fields ++ xs; let newVars := newVars.map fun x => x.applyFVarSubst subst; let subex := Example.ctor subgoal.ctorName $ fields.map fun field => match field with | Expr.fvar fvarId _ => Example.var fvarId | _ => Example.underscore; -- This case can happen due to dependent elimination let examples := p.examples.map $ Example.replaceFVarId x.fvarId! subex; let examples := examples.map $ Example.applyFVarSubst subst; let newAlts := p.alts.filter fun alt => match alt.patterns with | Pattern.ctor n _ _ _ :: _ => n == subgoal.ctorName | Pattern.var _ :: _ => true | Pattern.inaccessible _ :: _ => true | _ => false; let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst; newAlts ← newAlts.filterMapM fun alt => match alt.patterns with | Pattern.ctor _ _ _ fields :: ps => pure $ some { alt with patterns := fields ++ ps } | Pattern.var fvarId :: ps => expandVarIntoCtor? { alt with patterns := ps } fvarId subgoal.ctorName | Pattern.inaccessible _ :: _ => processInaccessibleAsCtor alt subgoal.ctorName | _ => unreachable!; pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples } private def processNonVariable (p : Problem) : MetaM Problem := match p.vars with | [] => unreachable! | x :: xs => withGoalOf p do x ← whnfD x; env ← getEnv; match x.constructorApp? env with | some (ctorVal, xArgs) => do alts ← p.alts.filterMapM fun alt => match alt.patterns with | Pattern.ctor n _ _ fields :: ps => if n != ctorVal.name then pure none else pure $ some { alt with patterns := fields ++ ps } | Pattern.inaccessible _ :: _ => processInaccessibleAsCtor alt ctorVal.name | p :: _ => throwError ("failed to compile pattern matching, inaccessible pattern or constructor expected" ++ indentD p.toMessageData) | _ => unreachable!; let xFields := xArgs.extract ctorVal.nparams xArgs.size; pure { p with alts := alts, vars := xFields.toList ++ xs } | none => throwError ("failed to compile pattern matching, constructor expected" ++ indentExpr x) private def collectValues (p : Problem) : Array Expr := p.alts.foldl (fun (values : Array Expr) alt => match alt.patterns with | Pattern.val v :: _ => if values.contains v then values else values.push v | _ => values) #[] private def isFirstPatternVar (alt : Alt) : Bool := match alt.patterns with | Pattern.var _ :: _ => true | _ => false private def processValue (p : Problem) : MetaM (Array Problem) := do trace! `Meta.Match.match ("value step"); match p.vars with | [] => unreachable! | x :: xs => do let values := collectValues p; subgoals ← caseValues p.mvarId x.fvarId! values; subgoals.mapIdxM fun i subgoal => if h : i < values.size then do let value := values.get ⟨i, h⟩; -- (x = value) branch let subst := subgoal.subst; let examples := p.examples.map $ Example.replaceFVarId x.fvarId! (Example.val value); let examples := examples.map $ Example.applyFVarSubst subst; let newAlts := p.alts.filter fun alt => match alt.patterns with | Pattern.val v :: _ => v == value | Pattern.var _ :: _ => true | _ => false; let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst; let newAlts := newAlts.map fun alt => match alt.patterns with | Pattern.val _ :: ps => { alt with patterns := ps } | Pattern.var fvarId :: ps => do let alt := { alt with patterns := ps }; alt.replaceFVarId fvarId value | _ => unreachable!; let newVars := xs.map fun x => x.applyFVarSubst subst; pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples } else do -- else branch let newAlts := p.alts.filter isFirstPatternVar; pure { p with mvarId := subgoal.mvarId, alts := newAlts, vars := x::xs } private def collectArraySizes (p : Problem) : Array Nat := p.alts.foldl (fun (sizes : Array Nat) alt => match alt.patterns with | Pattern.arrayLit _ ps :: _ => let sz := ps.length; if sizes.contains sz then sizes else sizes.push sz | _ => sizes) #[] private def expandVarIntoArrayLitAux (alt : Alt) (fvarId : FVarId) (arrayElemType : Expr) (varNamePrefix : Name) : Nat β†’ Array Expr β†’ MetaM Alt | n+1, newVars => withLocalDeclD (varNamePrefix.appendIndexAfter (n+1)) arrayElemType fun x => expandVarIntoArrayLitAux n (newVars.push x) | 0, newVars => do arrayLit ← mkArrayLit arrayElemType newVars.toList; let alt := alt.replaceFVarId fvarId arrayLit; newDecls ← newVars.toList.mapM fun newVar => getLocalDecl newVar.fvarId!; let newPatterns := newVars.toList.map fun newVar => Pattern.var newVar.fvarId!; pure { alt with fvarDecls := newDecls ++ alt.fvarDecls, patterns := newPatterns ++ alt.patterns } private def expandVarIntoArrayLit (alt : Alt) (fvarId : FVarId) (arrayElemType : Expr) (arraySize : Nat) : MetaM Alt := withExistingLocalDecls alt.fvarDecls do fvarDecl ← getLocalDecl fvarId; expandVarIntoArrayLitAux alt fvarId arrayElemType fvarDecl.userName arraySize #[] private def processArrayLit (p : Problem) : MetaM (Array Problem) := do trace! `Meta.Match.match ("array literal step"); match p.vars with | [] => unreachable! | x :: xs => do let sizes := collectArraySizes p; subgoals ← caseArraySizes p.mvarId x.fvarId! sizes; subgoals.mapIdxM fun i subgoal => if h : i < sizes.size then do let size := sizes.get! i; let subst := subgoal.subst; let elems := subgoal.elems.toList; let newVars := elems.map mkFVar ++ xs; let newVars := newVars.map fun x => x.applyFVarSubst subst; let subex := Example.arrayLit $ elems.map Example.var; let examples := p.examples.map $ Example.replaceFVarId x.fvarId! subex; let examples := examples.map $ Example.applyFVarSubst subst; let newAlts := p.alts.filter fun alt => match alt.patterns with | Pattern.arrayLit _ ps :: _ => ps.length == size | Pattern.var _ :: _ => true | _ => false; let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst; newAlts ← newAlts.mapM fun alt => match alt.patterns with | Pattern.arrayLit _ pats :: ps => pure { alt with patterns := pats ++ ps } | Pattern.var fvarId :: ps => do Ξ± ← getArrayArgType x; expandVarIntoArrayLit { alt with patterns := ps } fvarId Ξ± size | _ => unreachable!; pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples } else do -- else branch let newAlts := p.alts.filter isFirstPatternVar; pure { p with mvarId := subgoal.mvarId, alts := newAlts, vars := x::xs } private def expandNatValuePattern (p : Problem) : Problem := do let alts := p.alts.map fun alt => match alt.patterns with | Pattern.val (Expr.lit (Literal.natVal 0) _) :: ps => { alt with patterns := Pattern.ctor `Nat.zero [] [] [] :: ps } | Pattern.val (Expr.lit (Literal.natVal (n+1)) _) :: ps => { alt with patterns := Pattern.ctor `Nat.succ [] [] [Pattern.val (mkNatLit n)] :: ps } | _ => alt; { p with alts := alts } private def traceStep (msg : String) : StateRefT State MetaM Unit := liftM (trace! `Meta.Match.match (msg ++ " step") : MetaM Unit) private def traceState (p : Problem) : MetaM Unit := withGoalOf p (traceM `Meta.Match.match p.toMessageData) private def throwNonSupported (p : Problem) : MetaM Unit := do msg ← p.toMessageData; throwError ("not implement yet " ++ msg) def isCurrVarInductive (p : Problem) : MetaM Bool := do match p.vars with | [] => pure false | x::_ => withGoalOf p do val? ← getInductiveVal? x; pure val?.isSome private partial def process : Problem β†’ StateRefT State MetaM Unit | p => withIncRecDepth do liftM $ traceState p; isInductive ← liftM $ isCurrVarInductive p; if isDone p then processLeaf p else if hasAsPattern p then do traceStep ("as-pattern"); p ← liftM $ processAsPattern p; process p else if isNatValueTransition p then do traceStep ("nat value to constructor"); process (expandNatValuePattern p) else if !isNextVar p then do traceStep ("non variable"); p ← liftM $ processNonVariable p; process p else if isInductive && isConstructorTransition p then do ps ← liftM $ processConstructor p; ps.forM process else if isVariableTransition p then do traceStep ("variable"); p ← liftM $ processVariable p; process p else if isValueTransition p then do ps ← liftM $ processValue p; ps.forM process else if isArrayLitTransition p then do ps ← liftM $ processArrayLit p; ps.forM process else liftM $ throwNonSupported p /-- A "matcher" auxiliary declaration has the following structure: - `numParams` parameters - motive - `numDiscrs` discriminators (aka major premises) - `altNumParams.size` alternatives (aka minor premises) where alternative `i` has `altNumParams[i]` alternatives - `uElimPos?` is `some pos` when the matcher can eliminate in different universe levels, and `pos` is the position of the universe level parameter that specifies the elimination universe. It is `none` if the matcher only eliminates into `Prop`. -/ structure MatcherInfo := (numParams : Nat) (numDiscrs : Nat) (altNumParams : Array Nat) (uElimPos? : Option Nat) def MatcherInfo.numAlts (matcherInfo : MatcherInfo) : Nat := matcherInfo.altNumParams.size namespace Extension structure Entry := (name : Name) (info : MatcherInfo) structure State := (map : SMap Name MatcherInfo := {}) instance State.inhabited : Inhabited State := ⟨{}⟩ def State.addEntry (s : State) (e : Entry) : State := { s with map := s.map.insert e.name e.info } def State.switch (s : State) : State := { s with map := s.map.switch } def mkExtension : IO (SimplePersistentEnvExtension Entry State) := registerSimplePersistentEnvExtension { name := `matcher, addEntryFn := State.addEntry, addImportedFn := fun es => (mkStateFromImportedEntries State.addEntry {} es).switch } @[init mkExtension] constant extension : SimplePersistentEnvExtension Entry State := arbitrary _ def addMatcherInfo (env : Environment) (matcherName : Name) (info : MatcherInfo) : Environment := extension.addEntry env { name := matcherName, info := info } def getMatcherInfo? (env : Environment) (declName : Name) : Option MatcherInfo := (extension.getState env).map.find? declName end Extension def addMatcherInfo (matcherName : Name) (info : MatcherInfo) : MetaM Unit := modifyEnv fun env => Extension.addMatcherInfo env matcherName info private def getUElimPos? (matcherLevels : List Level) (uElim : Level) : MetaM (Option Nat) := if uElim == levelZero then pure none else match matcherLevels.toArray.indexOf? uElim with | none => throwError "dependent match elimination failed, universe level not found" | some pos => pure $ some pos.val /- Create a dependent matcher for `matchType` where `matchType` is of the form `(a_1 : A_1) -> (a_2 : A_2[a_1]) -> ... -> (a_n : A_n[a_1, a_2, ... a_{n-1}]) -> B[a_1, ..., a_n]` where `n = numDiscrs`, and the `lhss` are the left-hand-sides of the `match`-expression alternatives. Each `AltLHS` has a list of local declarations and a list of patterns. The number of patterns must be the same in each `AltLHS`. The generated matcher has the structure described at `MatcherInfo`. The motive argument is of the form `(motive : (a_1 : A_1) -> (a_2 : A_2[a_1]) -> ... -> (a_n : A_n[a_1, a_2, ... a_{n-1}]) -> Sort v)` where `v` is a universe parameter or 0 if `B[a_1, ..., a_n]` is a proposition. -/ def mkMatcher (matcherName : Name) (matchType : Expr) (numDiscrs : Nat) (lhss : List AltLHS) : MetaM MatcherResult := forallBoundedTelescope matchType numDiscrs fun majors matchTypeBody => do checkNumPatterns majors lhss; /- We generate an matcher that can eliminate using different motives with different universe levels. `uElim` is the universe level the caller wants to eliminate to. If it is not levelZero, we create a matcher that can eliminate in any universe level. This is useful for implementing `MatcherApp.addArg` because it may have to change the universe level. -/ uElim ← getLevel matchTypeBody; uElimGen ← if uElim == levelZero then pure levelZero else mkFreshLevelMVar; motiveType ← mkForallFVars majors (mkSort uElimGen); withLocalDeclD `motive motiveType fun motive => do trace! `Meta.Match.debug ("motiveType: " ++ motiveType); let mvarType := mkAppN motive majors; trace! `Meta.Match.debug ("target: " ++ mvarType); withAlts motive lhss fun alts minors => do mvar ← mkFreshExprMVar mvarType; let examples := majors.toList.map fun major => Example.var major.fvarId!; (_, s) ← (process { mvarId := mvar.mvarId!, vars := majors.toList, alts := alts, examples := examples }).run {}; let args := #[motive] ++ majors ++ minors.map Prod.fst; type ← mkForallFVars args mvarType; val ← mkLambdaFVars args mvar; trace! `Meta.Match.debug ("matcher value: " ++ val ++ "\ntype: " ++ type); matcher ← mkAuxDefinition matcherName type val; trace! `Meta.Match.debug ("matcher levels: " ++ toString matcher.getAppFn.constLevels! ++ ", uElim: " ++ toString uElimGen); uElimPos? ← getUElimPos? matcher.getAppFn.constLevels! uElimGen; isLevelDefEq uElimGen uElim; addMatcherInfo matcherName { numParams := matcher.getAppNumArgs, numDiscrs := numDiscrs, altNumParams := minors.map Prod.snd, uElimPos? := uElimPos? }; setInlineAttribute matcherName; trace! `Meta.Match.debug ("matcher: " ++ matcher); let unusedAltIdxs : List Nat := lhss.length.fold (fun i r => if s.used.contains i then r else i::r) []; pure { matcher := matcher, counterExamples := s.counterExamples, unusedAltIdxs := unusedAltIdxs.reverse } end Match export Match (MatcherInfo) def getMatcherInfo? (declName : Name) : MetaM (Option MatcherInfo) := do env ← getEnv; pure $ Match.Extension.getMatcherInfo? env declName def isMatcher (declName : Name) : MetaM Bool := do info? ← getMatcherInfo? declName; pure info?.isSome structure MatcherApp := (matcherName : Name) (matcherLevels : Array Level) (uElimPos? : Option Nat) (params : Array Expr) (motive : Expr) (discrs : Array Expr) (altNumParams : Array Nat) (alts : Array Expr) (remaining : Array Expr) def matchMatcherApp? (e : Expr) : MetaM (Option MatcherApp) := match e.getAppFn with | Expr.const declName declLevels _ => do some info ← getMatcherInfo? declName | pure none; let args := e.getAppArgs; if args.size < info.numParams + 1 + info.numDiscrs + info.numAlts then pure none else pure $ some { matcherName := declName, matcherLevels := declLevels.toArray, uElimPos? := info.uElimPos?, params := args.extract 0 info.numParams, motive := args.get! info.numParams, discrs := args.extract (info.numParams + 1) (info.numParams + 1 + info.numDiscrs), altNumParams := info.altNumParams, alts := args.extract (info.numParams + 1 + info.numDiscrs) (info.numParams + 1 + info.numDiscrs + info.numAlts), remaining := args.extract (info.numParams + 1 + info.numDiscrs + info.numAlts) args.size } | _ => pure none def MatcherApp.toExpr (matcherApp : MatcherApp) : Expr := let result := mkAppN (mkConst matcherApp.matcherName matcherApp.matcherLevels.toList) matcherApp.params; let result := mkApp result matcherApp.motive; let result := mkAppN result matcherApp.discrs; let result := mkAppN result matcherApp.alts; mkAppN result matcherApp.remaining /- Auxiliary function for MatcherApp.addArg -/ private partial def updateAlts : Expr β†’ Array Nat β†’ Array Expr β†’ Nat β†’ MetaM (Array Nat Γ— Array Expr) | typeNew, altNumParams, alts, i => if h : i < alts.size then do let alt := alts.get ⟨i, h⟩; let numParams := altNumParams.get! i; typeNew ← whnfD typeNew; match typeNew with | Expr.forallE n d b _ => do alt ← forallBoundedTelescope d (some numParams) fun xs d => do { alt ← catch (instantiateLambda alt xs) (fun _ => throwError "unexpected matcher application, insufficient number of parameters in alternative"); forallBoundedTelescope d (some 1) fun x d => do alt ← mkLambdaFVars x alt; -- x is the new argument we are adding to the alternative alt ← mkLambdaFVars xs alt; pure alt }; updateAlts (b.instantiate1 alt) (altNumParams.set! i (numParams+1)) (alts.set ⟨i, h⟩ alt) (i+1) | _ => throwError "unexpected type at MatcherApp.addArg" else pure (altNumParams, alts) /- Given - matcherApp `match_i As (fun xs => motive[xs]) discrs (fun ys_1 => (alt_1 : motive (C_1[ys_1])) ... (fun ys_n => (alt_n : motive (C_n[ys_n]) remaining`, and - expression `e : B[discrs]`, Construct the term `match_i As (fun xs => B[xs] -> motive[xs]) discrs (fun ys_1 (y : B[C_1[ys_1]]) => alt_1) ... (fun ys_n (y : B[C_n[ys_n]]) => alt_n) e remaining`, and We use `kabstract` to abstract the discriminants from `B[discrs]`. This method assumes - the `matcherApp.motive` is a lambda abstraction where `xs.size == discrs.size` - each alternative is a lambda abstraction where `ys_i.size == matcherApp.altNumParams[i]` -/ def MatcherApp.addArg (matcherApp : MatcherApp) (e : Expr) : MetaM MatcherApp := lambdaTelescope matcherApp.motive fun motiveArgs motiveBody => do unless (motiveArgs.size == matcherApp.discrs.size) $ -- This error can only happen if someone implemented a transformation that rewrites the motive created by `mkMatcher`. throwError ("unexpected matcher application, motive must be lambda expression with #" ++ toString matcherApp.discrs.size ++ " arguments"); eType ← inferType e; eTypeAbst ← matcherApp.discrs.size.foldRevM (fun i eTypeAbst => do let motiveArg := motiveArgs.get! i; let discr := matcherApp.discrs.get! i; eTypeAbst ← kabstract eTypeAbst discr; pure $ eTypeAbst.instantiate1 motiveArg) eType; motiveBody ← mkArrow eTypeAbst motiveBody; matcherLevels ← match matcherApp.uElimPos? with | none => pure matcherApp.matcherLevels | some pos => do { uElim ← getLevel motiveBody; pure $ matcherApp.matcherLevels.set! pos uElim }; motive ← mkLambdaFVars motiveArgs motiveBody; -- Construct `aux` `match_i As (fun xs => B[xs] β†’ motive[xs]) discrs`, and infer its type `auxType`. -- We use `auxType` to infer the type `B[C_i[ys_i]]` of the new argument in each alternative. let aux := mkAppN (mkConst matcherApp.matcherName matcherLevels.toList) matcherApp.params; let aux := mkApp aux motive; let aux := mkAppN aux matcherApp.discrs; trace! `Meta.debug aux; check aux; unlessM (isTypeCorrect aux) $ throwError "failed to add argument to matcher application, type error when constructing the new motive"; auxType ← inferType aux; (altNumParams, alts) ← updateAlts auxType matcherApp.altNumParams matcherApp.alts 0; pure { matcherApp with matcherLevels := matcherLevels, motive := motive, alts := alts, altNumParams := altNumParams, remaining := #[e] ++ matcherApp.remaining } @[init] private def regTraceClasses : IO Unit := do registerTraceClass `Meta.Match.match; registerTraceClass `Meta.Match.debug; registerTraceClass `Meta.Match.unify; pure () end Meta end Lean
b3b58d06de40b894a531adefd2fc4ad102c039a3
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/category_theory/monoidal/category.lean
4a7560333ddfafbc46049ecc6d2b557fe9cc70ce
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
16,316
lean
/- Copyright (c) 2018 Michael Jendrusch. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Jendrusch, Scott Morrison -/ import category_theory.products import category_theory.natural_isomorphism import tactic.basic import tactic.slice open category_theory universes v u open category_theory open category_theory.category open category_theory.iso namespace category_theory /-- In a monoidal category, we can take the tensor product of objects, `X βŠ— Y` and of morphisms `f βŠ— g`. Tensor product does not need to be strictly associative on objects, but there is a specified associator, `Ξ±_ X Y Z : (X βŠ— Y) βŠ— Z β‰… X βŠ— (Y βŠ— Z)`. There is a tensor unit `πŸ™_ C`, with specified left and right unitor isomorphisms `Ξ»_ X : πŸ™_ C βŠ— X β‰… X` and `ρ_ X : X βŠ— πŸ™_ C β‰… X`. These associators and unitors satisfy the pentagon and triangle equations. -/ class monoidal_category (C : Type u) [π’ž : category.{v} C] := -- curried tensor product of objects: (tensor_obj : C β†’ C β†’ C) (infixr ` βŠ— `:70 := tensor_obj) -- This notation is only temporary -- curried tensor product of morphisms: (tensor_hom : Ξ  {X₁ Y₁ Xβ‚‚ Yβ‚‚ : C}, (X₁ ⟢ Y₁) β†’ (Xβ‚‚ ⟢ Yβ‚‚) β†’ ((X₁ βŠ— Xβ‚‚) ⟢ (Y₁ βŠ— Yβ‚‚))) (infixr ` βŠ—' `:69 := tensor_hom) -- This notation is only temporary -- tensor product laws: (tensor_id' : βˆ€ (X₁ Xβ‚‚ : C), (πŸ™ X₁) βŠ—' (πŸ™ Xβ‚‚) = πŸ™ (X₁ βŠ— Xβ‚‚) . obviously) (tensor_comp' : βˆ€ {X₁ Y₁ Z₁ Xβ‚‚ Yβ‚‚ Zβ‚‚ : C} (f₁ : X₁ ⟢ Y₁) (fβ‚‚ : Xβ‚‚ ⟢ Yβ‚‚) (g₁ : Y₁ ⟢ Z₁) (gβ‚‚ : Yβ‚‚ ⟢ Zβ‚‚), (f₁ ≫ g₁) βŠ—' (fβ‚‚ ≫ gβ‚‚) = (f₁ βŠ—' fβ‚‚) ≫ (g₁ βŠ—' gβ‚‚) . obviously) -- tensor unit: (tensor_unit : C) (notation `πŸ™_` := tensor_unit) -- associator: (associator : Ξ  X Y Z : C, (X βŠ— Y) βŠ— Z β‰… X βŠ— (Y βŠ— Z)) (notation `Ξ±_` := associator) (associator_naturality' : βˆ€ {X₁ Xβ‚‚ X₃ Y₁ Yβ‚‚ Y₃ : C} (f₁ : X₁ ⟢ Y₁) (fβ‚‚ : Xβ‚‚ ⟢ Yβ‚‚) (f₃ : X₃ ⟢ Y₃), ((f₁ βŠ—' fβ‚‚) βŠ—' f₃) ≫ (Ξ±_ Y₁ Yβ‚‚ Y₃).hom = (Ξ±_ X₁ Xβ‚‚ X₃).hom ≫ (f₁ βŠ—' (fβ‚‚ βŠ—' f₃)) . obviously) -- left unitor: (left_unitor : Ξ  X : C, πŸ™_ βŠ— X β‰… X) (notation `Ξ»_` := left_unitor) (left_unitor_naturality' : βˆ€ {X Y : C} (f : X ⟢ Y), ((πŸ™ πŸ™_) βŠ—' f) ≫ (Ξ»_ Y).hom = (Ξ»_ X).hom ≫ f . obviously) -- right unitor: (right_unitor : Ξ  X : C, X βŠ— πŸ™_ β‰… X) (notation `ρ_` := right_unitor) (right_unitor_naturality' : βˆ€ {X Y : C} (f : X ⟢ Y), (f βŠ—' (πŸ™ πŸ™_)) ≫ (ρ_ Y).hom = (ρ_ X).hom ≫ f . obviously) -- pentagon identity: (pentagon' : βˆ€ W X Y Z : C, ((Ξ±_ W X Y).hom βŠ—' (πŸ™ Z)) ≫ (Ξ±_ W (X βŠ— Y) Z).hom ≫ ((πŸ™ W) βŠ—' (Ξ±_ X Y Z).hom) = (Ξ±_ (W βŠ— X) Y Z).hom ≫ (Ξ±_ W X (Y βŠ— Z)).hom . obviously) -- triangle identity: (triangle' : βˆ€ X Y : C, (Ξ±_ X πŸ™_ Y).hom ≫ ((πŸ™ X) βŠ—' (Ξ»_ Y).hom) = (ρ_ X).hom βŠ—' (πŸ™ Y) . obviously) restate_axiom monoidal_category.tensor_id' attribute [simp] monoidal_category.tensor_id restate_axiom monoidal_category.tensor_comp' attribute [simp] monoidal_category.tensor_comp restate_axiom monoidal_category.associator_naturality' restate_axiom monoidal_category.left_unitor_naturality' restate_axiom monoidal_category.right_unitor_naturality' restate_axiom monoidal_category.pentagon' restate_axiom monoidal_category.triangle' attribute [simp] monoidal_category.triangle open monoidal_category infixr ` βŠ— `:70 := tensor_obj infixr ` βŠ— `:70 := tensor_hom notation `πŸ™_` := tensor_unit notation `Ξ±_` := associator notation `Ξ»_` := left_unitor notation `ρ_` := right_unitor /-- The tensor product of two isomorphisms is an isomorphism. -/ def tensor_iso {C : Type u} {X Y X' Y' : C} [category.{v} C] [monoidal_category.{v} C] (f : X β‰… Y) (g : X' β‰… Y') : X βŠ— X' β‰… Y βŠ— Y' := { hom := f.hom βŠ— g.hom, inv := f.inv βŠ— g.inv, hom_inv_id' := by rw [←tensor_comp, iso.hom_inv_id, iso.hom_inv_id, ←tensor_id], inv_hom_id' := by rw [←tensor_comp, iso.inv_hom_id, iso.inv_hom_id, ←tensor_id] } infixr ` βŠ— `:70 := tensor_iso namespace monoidal_category section variables {C : Type u} [category.{v} C] [π’ž : monoidal_category.{v} C] include π’ž instance tensor_is_iso {W X Y Z : C} (f : W ⟢ X) [is_iso f] (g : Y ⟢ Z) [is_iso g] : is_iso (f βŠ— g) := { ..(as_iso f βŠ— as_iso g) } @[simp] lemma inv_tensor {W X Y Z : C} (f : W ⟢ X) [is_iso f] (g : Y ⟢ Z) [is_iso g] : inv (f βŠ— g) = inv f βŠ— inv g := rfl variables {U V W X Y Z : C} -- When `rewrite_search` lands, add @[search] attributes to -- monoidal_category.tensor_id monoidal_category.tensor_comp monoidal_category.associator_naturality -- monoidal_category.left_unitor_naturality monoidal_category.right_unitor_naturality -- monoidal_category.pentagon monoidal_category.triangle -- tensor_comp_id tensor_id_comp comp_id_tensor_tensor_id -- triangle_assoc_comp_left triangle_assoc_comp_right triangle_assoc_comp_left_inv triangle_assoc_comp_right_inv -- left_unitor_tensor left_unitor_tensor_inv -- right_unitor_tensor right_unitor_tensor_inv -- pentagon_inv -- associator_inv_naturality -- left_unitor_inv_naturality -- right_unitor_inv_naturality @[simp] lemma comp_tensor_id (f : W ⟢ X) (g : X ⟢ Y) : (f ≫ g) βŠ— (πŸ™ Z) = (f βŠ— (πŸ™ Z)) ≫ (g βŠ— (πŸ™ Z)) := by { rw ←tensor_comp, simp } @[simp] lemma id_tensor_comp (f : W ⟢ X) (g : X ⟢ Y) : (πŸ™ Z) βŠ— (f ≫ g) = (πŸ™ Z βŠ— f) ≫ (πŸ™ Z βŠ— g) := by { rw ←tensor_comp, simp } @[simp] lemma id_tensor_comp_tensor_id (f : W ⟢ X) (g : Y ⟢ Z) : ((πŸ™ Y) βŠ— f) ≫ (g βŠ— (πŸ™ X)) = g βŠ— f := by { rw [←tensor_comp], simp } @[simp] lemma tensor_id_comp_id_tensor (f : W ⟢ X) (g : Y ⟢ Z) : (g βŠ— (πŸ™ W)) ≫ ((πŸ™ Z) βŠ— f) = g βŠ— f := by { rw [←tensor_comp], simp } lemma left_unitor_inv_naturality {X X' : C} (f : X ⟢ X') : f ≫ (Ξ»_ X').inv = (Ξ»_ X).inv ≫ (πŸ™ _ βŠ— f) := begin apply (cancel_mono (Ξ»_ X').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [left_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end lemma right_unitor_inv_naturality {X X' : C} (f : X ⟢ X') : f ≫ (ρ_ X').inv = (ρ_ X).inv ≫ (f βŠ— πŸ™ _) := begin apply (cancel_mono (ρ_ X').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [right_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end @[simp] lemma tensor_left_iff {X Y : C} (f g : X ⟢ Y) : ((πŸ™ (πŸ™_ C)) βŠ— f = (πŸ™ (πŸ™_ C)) βŠ— g) ↔ (f = g) := begin split, { intro h, have h' := congr_arg (Ξ» k, (Ξ»_ _).inv ≫ k) h, dsimp at h', rw [←left_unitor_inv_naturality, ←left_unitor_inv_naturality] at h', exact (cancel_mono _).1 h', }, { intro h, subst h, } end @[simp] lemma tensor_right_iff {X Y : C} (f g : X ⟢ Y) : (f βŠ— (πŸ™ (πŸ™_ C)) = g βŠ— (πŸ™ (πŸ™_ C))) ↔ (f = g) := begin split, { intro h, have h' := congr_arg (Ξ» k, (ρ_ _).inv ≫ k) h, dsimp at h', rw [←right_unitor_inv_naturality, ←right_unitor_inv_naturality] at h', exact (cancel_mono _).1 h' }, { intro h, subst h, } end -- We now prove: -- ((Ξ±_ (πŸ™_ C) X Y).hom) ≫ -- ((Ξ»_ (X βŠ— Y)).hom) -- = ((Ξ»_ X).hom βŠ— (πŸ™ Y)) -- (and the corresponding fact for right unitors) -- following the proof on nLab: -- Lemma 2.2 at https://ncatlab.org/nlab/revision/monoidal+category/115 lemma left_unitor_product_aux_perimeter (X Y : C) : ((Ξ±_ (πŸ™_ C) (πŸ™_ C) X).hom βŠ— (πŸ™ Y)) ≫ (Ξ±_ (πŸ™_ C) ((πŸ™_ C) βŠ— X) Y).hom ≫ ((πŸ™ (πŸ™_ C)) βŠ— (Ξ±_ (πŸ™_ C) X Y).hom) ≫ ((πŸ™ (πŸ™_ C)) βŠ— (Ξ»_ (X βŠ— Y)).hom) = (((ρ_ (πŸ™_ C)).hom βŠ— (πŸ™ X)) βŠ— (πŸ™ Y)) ≫ (Ξ±_ (πŸ™_ C) X Y).hom := begin conv_lhs { congr, skip, rw [←category.assoc] }, rw [←category.assoc, monoidal_category.pentagon, associator_naturality, tensor_id, ←monoidal_category.triangle, ←category.assoc] end lemma left_unitor_product_aux_triangle (X Y : C) : ((Ξ±_ (πŸ™_ C) (πŸ™_ C) X).hom βŠ— (πŸ™ Y)) ≫ (((πŸ™ (πŸ™_ C)) βŠ— (Ξ»_ X).hom) βŠ— (πŸ™ Y)) = ((ρ_ (πŸ™_ C)).hom βŠ— (πŸ™ X)) βŠ— (πŸ™ Y) := by rw [←comp_tensor_id, ←monoidal_category.triangle] lemma left_unitor_product_aux_square (X Y : C) : (Ξ±_ (πŸ™_ C) ((πŸ™_ C) βŠ— X) Y).hom ≫ ((πŸ™ (πŸ™_ C)) βŠ— (Ξ»_ X).hom βŠ— (πŸ™ Y)) = (((πŸ™ (πŸ™_ C)) βŠ— (Ξ»_ X).hom) βŠ— (πŸ™ Y)) ≫ (Ξ±_ (πŸ™_ C) X Y).hom := by rw associator_naturality lemma left_unitor_product_aux (X Y : C) : ((πŸ™ (πŸ™_ C)) βŠ— (Ξ±_ (πŸ™_ C) X Y).hom) ≫ ((πŸ™ (πŸ™_ C)) βŠ— (Ξ»_ (X βŠ— Y)).hom) = (πŸ™ (πŸ™_ C)) βŠ— ((Ξ»_ X).hom βŠ— (πŸ™ Y)) := begin rw ←(cancel_epi (Ξ±_ (πŸ™_ C) ((πŸ™_ C) βŠ— X) Y).hom), rw left_unitor_product_aux_square, rw ←(cancel_epi ((Ξ±_ (πŸ™_ C) (πŸ™_ C) X).hom βŠ— (πŸ™ Y))), slice_rhs 1 2 { rw left_unitor_product_aux_triangle }, conv_lhs { rw [left_unitor_product_aux_perimeter] } end lemma right_unitor_product_aux_perimeter (X Y : C) : ((Ξ±_ X Y (πŸ™_ C)).hom βŠ— (πŸ™ (πŸ™_ C))) ≫ (Ξ±_ X (Y βŠ— (πŸ™_ C)) (πŸ™_ C)).hom ≫ ((πŸ™ X) βŠ— (Ξ±_ Y (πŸ™_ C) (πŸ™_ C)).hom) ≫ ((πŸ™ X) βŠ— (πŸ™ Y) βŠ— (Ξ»_ (πŸ™_ C)).hom) = ((ρ_ (X βŠ— Y)).hom βŠ— (πŸ™ (πŸ™_ C))) ≫ (Ξ±_ X Y (πŸ™_ C)).hom := begin transitivity (((Ξ±_ X Y _).hom βŠ— πŸ™ _) ≫ (Ξ±_ X _ _).hom ≫ (πŸ™ X βŠ— (Ξ±_ Y _ _).hom)) ≫ (πŸ™ X βŠ— πŸ™ Y βŠ— (Ξ»_ _).hom), { conv_lhs { congr, skip, rw [←category.assoc] }, conv_rhs { rw [category.assoc] } }, { conv_lhs { congr, rw [monoidal_category.pentagon] }, conv_rhs { congr, rw [←monoidal_category.triangle] }, conv_rhs { rw [category.assoc] }, conv_rhs { congr, skip, congr, congr, rw [←tensor_id] }, conv_rhs { congr, skip, rw [associator_naturality] }, conv_rhs { rw [←category.assoc] } } end lemma right_unitor_product_aux_triangle (X Y : C) : ((πŸ™ X) βŠ— (Ξ±_ Y (πŸ™_ C) (πŸ™_ C)).hom) ≫ ((πŸ™ X) βŠ— (πŸ™ Y) βŠ— (Ξ»_ (πŸ™_ C)).hom) = (πŸ™ X) βŠ— (ρ_ Y).hom βŠ— (πŸ™ (πŸ™_ C)) := by rw [←id_tensor_comp, ←monoidal_category.triangle] lemma right_unitor_product_aux_square (X Y : C) : (Ξ±_ X (Y βŠ— (πŸ™_ C)) (πŸ™_ C)).hom ≫ ((πŸ™ X) βŠ— (ρ_ Y).hom βŠ— (πŸ™ (πŸ™_ C))) = (((πŸ™ X) βŠ— (ρ_ Y).hom) βŠ— (πŸ™ (πŸ™_ C))) ≫ (Ξ±_ X Y (πŸ™_ C)).hom := by rw [associator_naturality] lemma right_unitor_product_aux (X Y : C) : ((Ξ±_ X Y (πŸ™_ C)).hom βŠ— (πŸ™ (πŸ™_ C))) ≫ (((πŸ™ X) βŠ— (ρ_ Y).hom) βŠ— (πŸ™ (πŸ™_ C))) = ((ρ_ (X βŠ— Y)).hom βŠ— (πŸ™ (πŸ™_ C))) := begin rw ←(cancel_mono (Ξ±_ X Y (πŸ™_ C)).hom), slice_lhs 2 3 { rw ←right_unitor_product_aux_square }, rw [←right_unitor_product_aux_triangle, ←right_unitor_product_aux_perimeter], end -- See Proposition 2.2.4 of http://www-math.mit.edu/~etingof/egnobookfinal.pdf @[simp] lemma left_unitor_tensor (X Y : C) : ((Ξ±_ (πŸ™_ C) X Y).hom) ≫ ((Ξ»_ (X βŠ— Y)).hom) = ((Ξ»_ X).hom βŠ— (πŸ™ Y)) := by rw [←tensor_left_iff, id_tensor_comp, left_unitor_product_aux] @[simp] lemma left_unitor_tensor_inv (X Y : C) : ((Ξ»_ (X βŠ— Y)).inv) ≫ ((Ξ±_ (πŸ™_ C) X Y).inv) = ((Ξ»_ X).inv βŠ— (πŸ™ Y)) := eq_of_inv_eq_inv (by simp) @[simp] lemma right_unitor_tensor (X Y : C) : ((Ξ±_ X Y (πŸ™_ C)).hom) ≫ ((πŸ™ X) βŠ— (ρ_ Y).hom) = ((ρ_ (X βŠ— Y)).hom) := by rw [←tensor_right_iff, comp_tensor_id, right_unitor_product_aux] @[simp] lemma right_unitor_tensor_inv (X Y : C) : ((πŸ™ X) βŠ— (ρ_ Y).inv) ≫ ((Ξ±_ X Y (πŸ™_ C)).inv) = ((ρ_ (X βŠ— Y)).inv) := eq_of_inv_eq_inv (by simp) lemma associator_inv_naturality {X Y Z X' Y' Z' : C} (f : X ⟢ X') (g : Y ⟢ Y') (h : Z ⟢ Z') : (f βŠ— (g βŠ— h)) ≫ (Ξ±_ X' Y' Z').inv = (Ξ±_ X Y Z).inv ≫ ((f βŠ— g) βŠ— h) := begin apply (cancel_mono (Ξ±_ X' Y' Z').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [associator_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end lemma pentagon_inv (W X Y Z : C) : ((πŸ™ W) βŠ— (Ξ±_ X Y Z).inv) ≫ (Ξ±_ W (X βŠ— Y) Z).inv ≫ ((Ξ±_ W X Y).inv βŠ— (πŸ™ Z)) = (Ξ±_ W X (Y βŠ— Z)).inv ≫ (Ξ±_ (W βŠ— X) Y Z).inv := begin apply category_theory.eq_of_inv_eq_inv, dsimp, rw [category.assoc, monoidal_category.pentagon] end @[simp] lemma triangle_assoc_comp_left (X Y : C) : (Ξ±_ X (πŸ™_ C) Y).hom ≫ ((πŸ™ X) βŠ— (Ξ»_ Y).hom) = (ρ_ X).hom βŠ— πŸ™ Y := monoidal_category.triangle C X Y @[simp] lemma triangle_assoc_comp_right (X Y : C) : (Ξ±_ X (πŸ™_ C) Y).inv ≫ ((ρ_ X).hom βŠ— πŸ™ Y) = ((πŸ™ X) βŠ— (Ξ»_ Y).hom) := by rw [←triangle_assoc_comp_left, ←category.assoc, iso.inv_hom_id, category.id_comp] @[simp] lemma triangle_assoc_comp_right_inv (X Y : C) : ((ρ_ X).inv βŠ— πŸ™ Y) ≫ (Ξ±_ X (πŸ™_ C) Y).hom = ((πŸ™ X) βŠ— (Ξ»_ Y).inv) := begin apply (cancel_mono (πŸ™ X βŠ— (Ξ»_ Y).hom)).1, simp only [assoc, triangle_assoc_comp_left], rw [←comp_tensor_id, iso.inv_hom_id, ←id_tensor_comp, iso.inv_hom_id] end @[simp] lemma triangle_assoc_comp_left_inv (X Y : C) : ((πŸ™ X) βŠ— (Ξ»_ Y).inv) ≫ (Ξ±_ X (πŸ™_ C) Y).inv = ((ρ_ X).inv βŠ— πŸ™ Y) := begin apply (cancel_mono ((ρ_ X).hom βŠ— πŸ™ Y)).1, simp only [triangle_assoc_comp_right, assoc], rw [←id_tensor_comp, iso.inv_hom_id, ←comp_tensor_id, iso.inv_hom_id] end end section -- In order to be able to describe the tensor product as a functor, we -- need to be up in at least `Type 0` for both objects and morphisms, -- so that we can construct products. variables (C : Type u) [category.{v+1} C] [π’ž : monoidal_category.{v+1} C] include π’ž /-- The tensor product expressed as a functor. -/ def tensor : (C Γ— C) β₯€ C := { obj := Ξ» X, X.1 βŠ— X.2, map := Ξ» {X Y : C Γ— C} (f : X ⟢ Y), f.1 βŠ— f.2 } /-- The left-associated triple tensor product as a functor. -/ def left_assoc_tensor : (C Γ— C Γ— C) β₯€ C := { obj := Ξ» X, (X.1 βŠ— X.2.1) βŠ— X.2.2, map := Ξ» {X Y : C Γ— C Γ— C} (f : X ⟢ Y), (f.1 βŠ— f.2.1) βŠ— f.2.2 } @[simp] lemma left_assoc_tensor_obj (X) : (left_assoc_tensor C).obj X = (X.1 βŠ— X.2.1) βŠ— X.2.2 := rfl @[simp] lemma left_assoc_tensor_map {X Y} (f : X ⟢ Y) : (left_assoc_tensor C).map f = (f.1 βŠ— f.2.1) βŠ— f.2.2 := rfl /-- The right-associated triple tensor product as a functor. -/ def right_assoc_tensor : (C Γ— C Γ— C) β₯€ C := { obj := Ξ» X, X.1 βŠ— (X.2.1 βŠ— X.2.2), map := Ξ» {X Y : C Γ— C Γ— C} (f : X ⟢ Y), f.1 βŠ— (f.2.1 βŠ— f.2.2) } @[simp] lemma right_assoc_tensor_obj (X) : (right_assoc_tensor C).obj X = X.1 βŠ— (X.2.1 βŠ— X.2.2) := rfl @[simp] lemma right_assoc_tensor_map {X Y} (f : X ⟢ Y) : (right_assoc_tensor C).map f = f.1 βŠ— (f.2.1 βŠ— f.2.2) := rfl /-- The functor `Ξ» X, πŸ™_ C βŠ— X`. -/ def tensor_unit_left : C β₯€ C := { obj := Ξ» X, πŸ™_ C βŠ— X, map := Ξ» {X Y : C} (f : X ⟢ Y), (πŸ™ (πŸ™_ C)) βŠ— f } /-- The functor `Ξ» X, X βŠ— πŸ™_ C`. -/ def tensor_unit_right : C β₯€ C := { obj := Ξ» X, X βŠ— πŸ™_ C, map := Ξ» {X Y : C} (f : X ⟢ Y), f βŠ— (πŸ™ (πŸ™_ C)) } -- We can express the associator and the unitors, given componentwise above, -- as natural isomorphisms. /-- The associator as a natural isomorphism. -/ def associator_nat_iso : left_assoc_tensor C β‰… right_assoc_tensor C := nat_iso.of_components (by { intros, apply monoidal_category.associator }) (by { intros, apply monoidal_category.associator_naturality }) /-- The left unitor as a natural isomorphism. -/ def left_unitor_nat_iso : tensor_unit_left C β‰… 𝟭 C := nat_iso.of_components (by { intros, apply monoidal_category.left_unitor }) (by { intros, apply monoidal_category.left_unitor_naturality }) /-- The right unitor as a natural isomorphism. -/ def right_unitor_nat_iso : tensor_unit_right C β‰… 𝟭 C := nat_iso.of_components (by { intros, apply monoidal_category.right_unitor }) (by { intros, apply monoidal_category.right_unitor_naturality }) end end monoidal_category end category_theory
e35999855d01ebf3d77b5262fe7f8b41fc0c6779
e514e8b939af519a1d5e9b30a850769d058df4e9
/src/tactic/rewrite_search/tracer/graph.lean
5f31f10085494bd8187f85921af3ea3061b00ec0
[]
no_license
semorrison/lean-rewrite-search
dca317c5a52e170fb6ffc87c5ab767afb5e3e51a
e804b8f2753366b8957be839908230ee73f9e89f
refs/heads/master
1,624,051,754,485
1,614,160,817,000
1,614,160,817,000
162,660,605
0
1
null
null
null
null
UTF-8
Lean
false
false
5,289
lean
import tactic.iconfig import tactic.rewrite_search.core import tactic.rewrite_search.module import system.io open tactic.rewrite_search namespace tactic.rewrite_search.tracer.graph open tactic open io.process.stdio def SUCCESS_CHAR : string := "S" def ERROR_CHAR : string := "E" def SEARCH_PATHS : list string := [ "_target/deps/lean-rewrite-search/res/graph_tracer", "res/graph_tracer" ] def get_app_path (dir : string) (app : string) : string := dir ++ "/" ++ app ++ ".py" def args (dir : string) (app : string) : io.process.spawn_args := { cmd := "python3", args := [get_app_path dir app], stdin := piped, stdout := piped, stderr := inherit, env := [ ("PYTHONPATH", some (dir ++ "/pygraphvis.zip/pygraphvis")), ("PYTHONIOENCODING", "utf-8") ], } structure visualiser := (proc : io.proc.child) meta def visualiser.publish (v : visualiser) (f : format) : tactic unit := tactic.unsafe_run_io $ do io.fs.write v.proc.stdin (f.to_string.to_char_buffer.push_back '\n'), io.fs.flush v.proc.stdin meta def visualiser.pause (v : visualiser) : tactic unit := tactic.unsafe_run_io (do io.fs.read v.proc.stdout 1, return ()) def file_exists (path : string) : io bool := do c ← io.proc.spawn { cmd := "test", args := ["-f", path] }, retval ← io.proc.wait c, return (retval = 0) inductive spawn_result | success : io.proc.child β†’ spawn_result -- Client launched and the client reported success status | abort : string β†’ spawn_result -- Client launched and we got a bad response code | failure -- Could not launch client | missing -- The script we tried to launch does't exist meta def read_until_nl (h : io.handle) : io string := do c ← io.fs.read h 1, match c.to_list with | ['\n'] := return "" | [c] := do r ← read_until_nl, return (c.to_string ++ r) | _ := return "" end meta def try_launch_with_path (path : string) : io spawn_result := do ex ← file_exists (get_app_path path "client"), if ex then do c ← io.proc.spawn (args path "client"), buff ← io.fs.read c.stdout 1, str ← pure buff.to_string, if str = SUCCESS_CHAR then return (spawn_result.success c) else if str = ERROR_CHAR then do reason ← read_until_nl c.stdout, return (spawn_result.abort reason) else if str = "" then return spawn_result.failure else return $ spawn_result.abort (format!"bug: unknown client status character \"{str}\"").to_string else return spawn_result.missing meta def try_launch_with_paths : list string β†’ io spawn_result | [] := return spawn_result.failure | (p :: rest) := do sr ← try_launch_with_path p, match sr with | spawn_result.missing := try_launch_with_paths rest | _ := return sr end meta def diagnose_launch_failure : io string := do c ← io.proc.spawn { cmd := "python3", args := ["--version"], stdin := piped, stdout := piped, stderr := piped }, r ← io.proc.wait c, match r with | 255 := return "python3 is missing, and the graph visualiser requires it. Please install python3." | 0 := return "bug: python3 present but could not launch client!" | ret := return (format!"bug: unexpected return code {ret} during launch failure diagnosis").to_string end meta def init : tactic (init_result visualiser) := do c ← tactic.unsafe_run_io (try_launch_with_paths SEARCH_PATHS), match c with | spawn_result.success c := let vs : visualiser := ⟨c⟩ in do vs.publish "S", init_result.pure vs | spawn_result.abort reason := init_result.fail ("Abort! " ++ reason) | spawn_result.failure := do reason ← tactic.unsafe_run_io diagnose_launch_failure, init_result.fail ("Failure! " ++ reason) | spawn_result.missing := init_result.fail "Error! bug: could not determine client location" end meta def publish_vertex (vs : visualiser) (v : vertex) : tactic unit := vs.publish format!"V|{v.id.to_string}|{v.s.to_string}|{v.id}" meta def publish_edge (vs : visualiser) (e : edge) : tactic unit := vs.publish format!"E|{e.f.to_string}|{e.t.to_string}" meta def publish_visited (vs : visualiser) (v : vertex) : tactic unit := vs.publish format!"B|{v.id.to_string}" meta def publish_finished (vs : visualiser) (es : list edge) : tactic unit := do es.mmap' (Ξ» e : edge, vs.publish format!"F|{e.f.to_string}|{e.t.to_string}"), vs.publish format!"D" meta def dump (vs : visualiser) (str : string) : tactic unit := vs.publish (str ++ "\n") meta def pause (vs : visualiser) : tactic unit := vs.pause end tactic.rewrite_search.tracer.graph namespace tactic.rewrite_search.tracer open tactic.rewrite_search.tracer.graph meta def graph_cnst := Ξ» Ξ± Ξ² Ξ³, tracer.mk Ξ± Ξ² Ξ³ graph.init graph.publish_vertex graph.publish_edge graph.publish_visited graph.publish_finished graph.dump graph.pause meta def graph : tactic expr := generic `tactic.rewrite_search.tracer.graph_cnst meta def visualiser_cfg (_ : name) : cfgtactic unit := iconfig.publish `tracer $ cfgopt.value.pexpr $ expr.const `graph [] iconfig_add rewrite_search [ tracer.graph : custom visualiser_cfg visualiser : custom visualiser_cfg ] end tactic.rewrite_search.tracer
ddeb7b40737a182f3fb0c54d78340ad2c2bde8e2
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Elab/Util.lean
c78e9d2cc62b6a3b510a1160f73a9dc5515a91c0
[ "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
10,277
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.Util.Trace import Lean.Parser.Syntax import Lean.Parser.Extension import Lean.KeyedDeclsAttribute import Lean.Elab.Exception import Lean.Elab.InfoTree import Lean.DocString import Lean.DeclarationRange import Lean.Compiler.InitAttr import Lean.Log namespace Lean def Syntax.prettyPrint (stx : Syntax) : Format := match stx.unsetTrailing.reprint with -- TODO use syntax pretty printer | some str => format str.toFormat | none => format stx def MacroScopesView.format (view : MacroScopesView) (mainModule : Name) : Format := Std.format <| if view.scopes.isEmpty then view.name else if view.mainModule == mainModule then view.scopes.foldl Name.mkNum (view.name ++ view.imported) else view.scopes.foldl Name.mkNum (view.name ++ view.imported ++ view.mainModule) namespace Elab def expandOptNamedPrio (stx : Syntax) : MacroM Nat := if stx.isNone then return eval_prio default else match stx[0] with | `(Parser.Command.namedPrio| (priority := $prio)) => evalPrio prio | _ => Macro.throwUnsupported structure MacroStackElem where before : Syntax after : Syntax abbrev MacroStack := List MacroStackElem /-- If `ref` does not have position information, then try to use macroStack -/ def getBetterRef (ref : Syntax) (macroStack : MacroStack) : Syntax := match ref.getPos? with | some _ => ref | none => match macroStack.find? (Β·.before.getPos? != none) with | some elem => elem.before | none => ref register_builtin_option pp.macroStack : Bool := { defValue := false group := "pp" descr := "dispaly macro expansion stack" } def addMacroStack {m} [Monad m] [MonadOptions m] (msgData : MessageData) (macroStack : MacroStack) : m MessageData := do if !pp.macroStack.get (← getOptions) then pure msgData else match macroStack with | [] => pure msgData | stack@(top::_) => let msgData := msgData ++ Format.line ++ "with resulting expansion" ++ indentD top.after pure $ stack.foldl (fun (msgData : MessageData) (elem : MacroStackElem) => msgData ++ Format.line ++ "while expanding" ++ indentD elem.before) msgData def checkSyntaxNodeKind [Monad m] [MonadEnv m] [MonadError m] (k : Name) : m Name := do if Parser.isValidSyntaxNodeKind (← getEnv) k then pure k else throwError "failed" def checkSyntaxNodeKindAtNamespaces [Monad m] [MonadEnv m] [MonadError m] (k : Name) : Name β†’ m Name | n@(.str p _) => checkSyntaxNodeKind (n ++ k) <|> checkSyntaxNodeKindAtNamespaces k p | .anonymous => checkSyntaxNodeKind k | _ => throwError "failed" def checkSyntaxNodeKindAtCurrentNamespaces (k : Name) : AttrM Name := do let ctx ← read checkSyntaxNodeKindAtNamespaces k ctx.currNamespace def syntaxNodeKindOfAttrParam (defaultParserNamespace : Name) (stx : Syntax) : AttrM SyntaxNodeKind := do let k ← Attribute.Builtin.getId stx checkSyntaxNodeKindAtCurrentNamespaces k <|> checkSyntaxNodeKind (defaultParserNamespace ++ k) <|> throwError "invalid syntax node kind '{k}'" private unsafe def evalSyntaxConstantUnsafe (env : Environment) (opts : Options) (constName : Name) : ExceptT String Id Syntax := env.evalConstCheck Syntax opts `Lean.Syntax constName @[implementedBy evalSyntaxConstantUnsafe] opaque evalSyntaxConstant (env : Environment) (opts : Options) (constName : Name) : ExceptT String Id Syntax := throw "" unsafe def mkElabAttribute (Ξ³) (attrDeclName attrBuiltinName attrName : Name) (parserNamespace : Name) (typeName : Name) (kind : String) : IO (KeyedDeclsAttribute Ξ³) := KeyedDeclsAttribute.init { builtinName := attrBuiltinName name := attrName descr := kind ++ " elaborator" valueTypeName := typeName evalKey := fun _ stx => do let kind ← syntaxNodeKindOfAttrParam parserNamespace stx /- Recall that a `SyntaxNodeKind` is often the name of the parser, but this is not always true, and we must check it. -/ if (← getEnv).contains kind && (← getInfoState).enabled then pushInfoLeaf <| Info.ofTermInfo { elaborator := .anonymous lctx := {} expr := mkConst kind stx := stx[1] expectedType? := none } return kind onAdded := fun builtin declName => do if builtin then if let some doc ← findDocString? (← getEnv) declName then declareBuiltin (declName ++ `docString) (mkAppN (mkConst ``addBuiltinDocString) #[toExpr declName, toExpr doc]) if let some declRanges ← findDeclarationRanges? declName then declareBuiltin (declName ++ `declRange) (mkAppN (mkConst ``addBuiltinDeclarationRanges) #[toExpr declName, toExpr declRanges]) } attrDeclName unsafe def mkMacroAttributeUnsafe : IO (KeyedDeclsAttribute Macro) := mkElabAttribute Macro `Lean.Elab.macroAttribute `builtinMacro `macro Name.anonymous `Lean.Macro "macro" @[implementedBy mkMacroAttributeUnsafe] opaque mkMacroAttribute : IO (KeyedDeclsAttribute Macro) builtin_initialize macroAttribute : KeyedDeclsAttribute Macro ← mkMacroAttribute /-- Try to expand macro at syntax tree root and return macro declaration name and new syntax if successful. Return none if all macros threw `Macro.Exception.unsupportedSyntax`. -/ def expandMacroImpl? (env : Environment) : Syntax β†’ MacroM (Option (Name Γ— Except Macro.Exception Syntax)) := fun stx => do for e in macroAttribute.getEntries env stx.getKind do try let stx' ← withFreshMacroScope (e.value stx) return (e.declName, Except.ok stx') catch | Macro.Exception.unsupportedSyntax => pure () | ex => return (e.declName, Except.error ex) return none class MonadMacroAdapter (m : Type β†’ Type) where getCurrMacroScope : m MacroScope getNextMacroScope : m MacroScope setNextMacroScope : MacroScope β†’ m Unit instance (m n) [MonadLift m n] [MonadMacroAdapter m] : MonadMacroAdapter n := { getCurrMacroScope := liftM (MonadMacroAdapter.getCurrMacroScope : m _) getNextMacroScope := liftM (MonadMacroAdapter.getNextMacroScope : m _) setNextMacroScope := fun s => liftM (MonadMacroAdapter.setNextMacroScope s : m _) } def liftMacroM {Ξ±} {m : Type β†’ Type} [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] [MonadLiftT IO m] (x : MacroM Ξ±) : m Ξ± := do let env ← getEnv let currNamespace ← getCurrNamespace let openDecls ← getOpenDecls let methods := Macro.mkMethods { -- TODO: record recursive expansions in info tree? expandMacro? := fun stx => do match (← expandMacroImpl? env stx) with | some (_, stx?) => liftExcept stx? | none => return none hasDecl := fun declName => return env.contains declName getCurrNamespace := return currNamespace resolveNamespace := fun n => return ResolveName.resolveNamespace env currNamespace openDecls n resolveGlobalName := fun n => return ResolveName.resolveGlobalName env currNamespace openDecls n } match x { methods := methods ref := ← getRef currMacroScope := ← MonadMacroAdapter.getCurrMacroScope mainModule := env.mainModule currRecDepth := ← MonadRecDepth.getRecDepth maxRecDepth := ← MonadRecDepth.getMaxRecDepth } { macroScope := (← MonadMacroAdapter.getNextMacroScope) } with | EStateM.Result.error Macro.Exception.unsupportedSyntax _ => throwUnsupportedSyntax | EStateM.Result.error (Macro.Exception.error ref msg) _ => if msg == maxRecDepthErrorMessage then -- Make sure we can detect exception using `Exception.isMaxRecDepth` throwMaxRecDepthAt ref else throwErrorAt ref msg | EStateM.Result.ok a s => MonadMacroAdapter.setNextMacroScope s.macroScope s.traceMsgs.reverse.forM fun (clsName, msg) => trace clsName fun _ => msg return a @[inline] def adaptMacro {m : Type β†’ Type} [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] [MonadLiftT IO m] (x : Macro) (stx : Syntax) : m Syntax := liftMacroM (x stx) partial def mkUnusedBaseName (baseName : Name) : MacroM Name := do let currNamespace ← Macro.getCurrNamespace if ← Macro.hasDecl (currNamespace ++ baseName) then let rec loop (idx : Nat) := do let name := baseName.appendIndexAfter idx if ← Macro.hasDecl (currNamespace ++ name) then loop (idx+1) else return name loop 1 else return baseName def logException [Monad m] [MonadLog m] [AddMessageContext m] [MonadOptions m] [MonadLiftT IO m] (ex : Exception) : m Unit := do match ex with | Exception.error ref msg => logErrorAt ref msg | Exception.internal id _ => unless isAbortExceptionId id do let name ← id.getName logError m!"internal exception: {name}" def withLogging [Monad m] [MonadLog m] [MonadExcept Exception m] [AddMessageContext m] [MonadOptions m] [MonadLiftT IO m] (x : m Unit) : m Unit := do try x catch ex => logException ex def nestedExceptionToMessageData [Monad m] [MonadLog m] (ex : Exception) : m MessageData := do let pos ← getRefPos match ex.getRef.getPos? with | none => return ex.toMessageData | some exPos => if pos == exPos then return ex.toMessageData else let exPosition := (← getFileMap).toPosition exPos return m!"{exPosition.line}:{exPosition.column} {ex.toMessageData}" def throwErrorWithNestedErrors [MonadError m] [Monad m] [MonadLog m] (msg : MessageData) (exs : Array Exception) : m Ξ± := do throwError "{msg}, errors {toMessageList (← exs.mapM fun | ex => nestedExceptionToMessageData ex)}" builtin_initialize registerTraceClass `Elab registerTraceClass `Elab.step registerTraceClass `Elab.step.result (inherited := true) end Lean.Elab