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
c3623304de04f937b6a98b219e26edab84fe2c79
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch5/ex0205.lean
bcca5e1a8e17842b50bc47d8a7e5e4e1bdd8ffdb
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
176
lean
variables x y z w : ℕ example (h₁ : x = y) (h₂ : y = z) (h₃ : z = w) : x = w := begin apply eq.trans, assumption, apply eq.trans, assumption, assumption end
d0084cd88d03a421049ade7001d45453b3310919
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/box_integral/divergence_theorem.lean
ea65ee8a4ed601061c210e881f489381343ad41d
[ "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
16,443
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.box_integral.basic import analysis.box_integral.partition.additive import analysis.calculus.fderiv.basic /-! # Divergence integral for Henstock-Kurzweil integral In this file we prove the Divergence Theorem for a Henstock-Kurzweil style integral. The theorem says the following. Let `f : ℝⁿ → Eⁿ` be a function differentiable on a closed rectangular box `I` with derivative `f' x : ℝⁿ →L[ℝ] Eⁿ` at `x ∈ I`. Then the divergence `λ x, ∑ k, f' x eₖ k`, where `eₖ = pi.single k 1` is the `k`-th basis vector, is integrable on `I`, and its integral is equal to the sum of integrals of `f` over the faces of `I` taken with appropriate signs. To make the proof work, we had to ban tagged partitions with “long and thin” boxes. More precisely, we use the following generalization of one-dimensional Henstock-Kurzweil integral to functions defined on a box in `ℝⁿ` (it corresponds to the value `box_integral.integration_params.GP = ⊥` of `box_integral.integration_params` in the definition of `box_integral.has_integral`). We say that `f : ℝⁿ → E` has integral `y : E` over a box `I ⊆ ℝⁿ` if for an arbitrarily small positive `ε` and an arbitrarily large `c`, there exists a function `r : ℝⁿ → (0, ∞)` such that for any tagged partition `π` of `I` such that * `π` is a Henstock partition, i.e., each tag belongs to its box; * `π` is subordinate to `r`; * for every box of `π`, the maximum of the ratios of its sides is less than or equal to `c`, the integral sum of `f` over `π` is `ε`-close to `y`. In case of dimension one, the last condition trivially holds for any `c ≥ 1`, so this definition is equivalent to the standard definition of Henstock-Kurzweil integral. ## Tags Henstock-Kurzweil integral, integral, Stokes theorem, divergence theorem -/ open_locale classical big_operators nnreal ennreal topology box_integral open continuous_linear_map (lsmul) filter set finset metric box_integral.integration_params (GP GP_le) noncomputable theory universes u variables {E : Type u} [normed_add_comm_group E] [normed_space ℝ E] {n : ℕ} namespace box_integral local notation `ℝⁿ` := fin n → ℝ local notation `ℝⁿ⁺¹` := fin (n + 1) → ℝ local notation `Eⁿ⁺¹` := fin (n + 1) → E variables [complete_space E] (I : box (fin (n + 1))) {i : fin (n + 1)} open measure_theory /-- Auxiliary lemma for the divergence theorem. -/ lemma norm_volume_sub_integral_face_upper_sub_lower_smul_le {f : ℝⁿ⁺¹ → E} {f' : ℝⁿ⁺¹ →L[ℝ] E} (hfc : continuous_on f I.Icc) {x : ℝⁿ⁺¹} (hxI : x ∈ I.Icc) {a : E} {ε : ℝ} (h0 : 0 < ε) (hε : ∀ y ∈ I.Icc, ‖f y - a - f' (y - x)‖ ≤ ε * ‖y - x‖) {c : ℝ≥0} (hc : I.distortion ≤ c) : ‖(∏ j, (I.upper j - I.lower j)) • f' (pi.single i 1) - (integral (I.face i) ⊥ (f ∘ i.insert_nth (I.upper i)) box_additive_map.volume - integral (I.face i) ⊥ (f ∘ i.insert_nth (I.lower i)) box_additive_map.volume)‖ ≤ 2 * ε * c * ∏ j, (I.upper j - I.lower j) := begin /- **Plan of the proof**. The difference of the integrals of the affine function `λ y, a + f' (y - x)` over the faces `x i = I.upper i` and `x i = I.lower i` is equal to the volume of `I` multiplied by `f' (pi.single i 1)`, so it suffices to show that the integral of `f y - a - f' (y - x)` over each of these faces is less than or equal to `ε * c * vol I`. We integrate a function of the norm `≤ ε * diam I.Icc` over a box of volume `∏ j ≠ i, (I.upper j - I.lower j)`. Since `diam I.Icc ≤ c * (I.upper i - I.lower i)`, we get the required estimate. -/ have Hl : I.lower i ∈ Icc (I.lower i) (I.upper i) := set.left_mem_Icc.2 (I.lower_le_upper i), have Hu : I.upper i ∈ Icc (I.lower i) (I.upper i) := set.right_mem_Icc.2 (I.lower_le_upper i), have Hi : ∀ x ∈ Icc (I.lower i) (I.upper i), integrable.{0 u u} (I.face i) ⊥ (f ∘ i.insert_nth x) box_additive_map.volume, from λ x hx, integrable_of_continuous_on _ (box.continuous_on_face_Icc hfc hx) volume, /- We start with an estimate: the difference of the values of `f` at the corresponding points of the faces `x i = I.lower i` and `x i = I.upper i` is `(2 * ε * diam I.Icc)`-close to the value of `f'` on `pi.single i (I.upper i - I.lower i) = lᵢ • eᵢ`, where `lᵢ = I.upper i - I.lower i` is the length of `i`-th edge of `I` and `eᵢ = pi.single i 1` is the `i`-th unit vector. -/ have : ∀ y ∈ (I.face i).Icc, ‖f' (pi.single i (I.upper i - I.lower i)) - (f (i.insert_nth (I.upper i) y) - f (i.insert_nth (I.lower i) y))‖ ≤ 2 * ε * diam I.Icc, { intros y hy, set g := λ y, f y - a - f' (y - x) with hg, change ∀ y ∈ I.Icc, ‖g y‖ ≤ ε * ‖y - x‖ at hε, clear_value g, obtain rfl : f = λ y, a + f' (y - x) + g y, by simp [hg], convert_to ‖g (i.insert_nth (I.lower i) y) - g (i.insert_nth (I.upper i) y)‖ ≤ _, { congr' 1, have := fin.insert_nth_sub_same i (I.upper i) (I.lower i) y, simp only [← this, f'.map_sub], abel }, { have : ∀ z ∈ Icc (I.lower i) (I.upper i), i.insert_nth z y ∈ I.Icc, from λ z hz, I.maps_to_insert_nth_face_Icc hz hy, replace hε : ∀ y ∈ I.Icc, ‖g y‖ ≤ ε * diam I.Icc, { intros y hy, refine (hε y hy).trans (mul_le_mul_of_nonneg_left _ h0.le), rw ← dist_eq_norm, exact dist_le_diam_of_mem I.is_compact_Icc.bounded hy hxI }, rw [two_mul, add_mul], exact norm_sub_le_of_le (hε _ (this _ Hl)) (hε _ (this _ Hu)) } }, calc ‖(∏ j, (I.upper j - I.lower j)) • f' (pi.single i 1) - (integral (I.face i) ⊥ (f ∘ i.insert_nth (I.upper i)) box_additive_map.volume - integral (I.face i) ⊥ (f ∘ i.insert_nth (I.lower i)) box_additive_map.volume)‖ = ‖integral.{0 u u} (I.face i) ⊥ (λ (x : fin n → ℝ), f' (pi.single i (I.upper i - I.lower i)) - (f (i.insert_nth (I.upper i) x) - f (i.insert_nth (I.lower i) x))) box_additive_map.volume‖ : begin rw [← integral_sub (Hi _ Hu) (Hi _ Hl), ← box.volume_face_mul i, mul_smul, ← box.volume_apply, ← box_additive_map.to_smul_apply, ← integral_const, ← box_additive_map.volume, ← integral_sub (integrable_const _) ((Hi _ Hu).sub (Hi _ Hl))], simp only [(∘), pi.sub_def, ← f'.map_smul, ← pi.single_smul', smul_eq_mul, mul_one] end ... ≤ (volume (I.face i : set ℝⁿ)).to_real * (2 * ε * c * (I.upper i - I.lower i)) : begin -- The hard part of the estimate was done above, here we just replace `diam I.Icc` -- with `c * (I.upper i - I.lower i)` refine norm_integral_le_of_le_const (λ y hy, (this y hy).trans _) volume, rw mul_assoc (2 * ε), exact mul_le_mul_of_nonneg_left (I.diam_Icc_le_of_distortion_le i hc) (mul_nonneg zero_le_two h0.le) end ... = 2 * ε * c * ∏ j, (I.upper j - I.lower j) : begin rw [← measure.to_box_additive_apply, box.volume_apply, ← I.volume_face_mul i], ac_refl end end /-- If `f : ℝⁿ⁺¹ → E` is differentiable on a closed rectangular box `I` with derivative `f'`, then the partial derivative `λ x, f' x (pi.single i 1)` is Henstock-Kurzweil integrable with integral equal to the difference of integrals of `f` over the faces `x i = I.upper i` and `x i = I.lower i`. More precisely, we use a non-standard generalization of the Henstock-Kurzweil integral and we allow `f` to be non-differentiable (but still continuous) at a countable set of points. TODO: If `n > 0`, then the condition at `x ∈ s` can be replaced by a much weaker estimate but this requires either better integrability theorems, or usage of a filter depending on the countable set `s` (we need to ensure that none of the faces of a partition contain a point from `s`). -/ lemma has_integral_GP_pderiv (f : ℝⁿ⁺¹ → E) (f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] E) (s : set ℝⁿ⁺¹) (hs : s.countable) (Hs : ∀ x ∈ s, continuous_within_at f I.Icc x) (Hd : ∀ x ∈ I.Icc \ s, has_fderiv_within_at f (f' x) I.Icc x) (i : fin (n + 1)) : has_integral.{0 u u} I GP (λ x, f' x (pi.single i 1)) box_additive_map.volume (integral.{0 u u} (I.face i) GP (λ x, f (i.insert_nth (I.upper i) x)) box_additive_map.volume - integral.{0 u u} (I.face i) GP (λ x, f (i.insert_nth (I.lower i) x)) box_additive_map.volume) := begin /- Note that `f` is continuous on `I.Icc`, hence it is integrable on the faces of all boxes `J ≤ I`, thus the difference of integrals over `x i = J.upper i` and `x i = J.lower i` is a box-additive function of `J ≤ I`. -/ have Hc : continuous_on f I.Icc, { intros x hx, by_cases hxs : x ∈ s, exacts [Hs x hxs, (Hd x ⟨hx, hxs⟩).continuous_within_at] }, set fI : ℝ → box (fin n) → E := λ y J, integral.{0 u u} J GP (λ x, f (i.insert_nth y x)) box_additive_map.volume, set fb : Icc (I.lower i) (I.upper i) → fin n →ᵇᵃ[↑(I.face i)] E := λ x, (integrable_of_continuous_on GP (box.continuous_on_face_Icc Hc x.2) volume).to_box_additive, set F : fin (n + 1) →ᵇᵃ[I] E := box_additive_map.upper_sub_lower I i fI fb (λ x hx J, rfl), /- Thus our statement follows from some local estimates. -/ change has_integral I GP (λ x, f' x (pi.single i 1)) _ (F I), refine has_integral_of_le_Henstock_of_forall_is_o GP_le _ _ _ s hs _ _, { /- We use the volume as an upper estimate. -/ exact (volume : measure ℝⁿ⁺¹).to_box_additive.restrict _ le_top }, { exact λ J, ennreal.to_real_nonneg }, { intros c x hx ε ε0, /- Near `x ∈ s` we choose `δ` so that both vectors are small. `volume J • eᵢ` is small because `volume J ≤ (2 * δ) ^ (n + 1)` is small, and the difference of the integrals is small because each of the integrals is close to `volume (J.face i) • f x`. TODO: there should be a shorter and more readable way to formalize this simple proof. -/ have : ∀ᶠ δ in 𝓝[>] (0 : ℝ), δ ∈ Ioc (0 : ℝ) (1 / 2) ∧ (∀ y₁ y₂ ∈ closed_ball x δ ∩ I.Icc, ‖f y₁ - f y₂‖ ≤ ε / 2) ∧ ((2 * δ) ^ (n + 1) * ‖f' x (pi.single i 1)‖ ≤ ε / 2), { refine eventually.and _ (eventually.and _ _), { exact Ioc_mem_nhds_within_Ioi ⟨le_rfl, one_half_pos⟩ }, { rcases ((nhds_within_has_basis nhds_basis_closed_ball _).tendsto_iff nhds_basis_closed_ball).1 (Hs x hx.2) _ (half_pos $ half_pos ε0) with ⟨δ₁, δ₁0, hδ₁⟩, filter_upwards [Ioc_mem_nhds_within_Ioi ⟨le_rfl, δ₁0⟩] with δ hδ y₁ hy₁ y₂ hy₂, have : closed_ball x δ ∩ I.Icc ⊆ closed_ball x δ₁ ∩ I.Icc, from inter_subset_inter_left _ (closed_ball_subset_closed_ball hδ.2), rw ← dist_eq_norm, calc dist (f y₁) (f y₂) ≤ dist (f y₁) (f x) + dist (f y₂) (f x) : dist_triangle_right _ _ _ ... ≤ ε / 2 / 2 + ε / 2 / 2 : add_le_add (hδ₁ _ $ this hy₁) (hδ₁ _ $ this hy₂) ... = ε / 2 : add_halves _ }, { have : continuous_within_at (λ δ, (2 * δ) ^ (n + 1) * ‖f' x (pi.single i 1)‖) (Ioi (0 : ℝ)) 0 := ((continuous_within_at_id.const_mul _).pow _).mul_const _, refine this.eventually (ge_mem_nhds _), simpa using half_pos ε0 } }, rcases this.exists with ⟨δ, ⟨hδ0, hδ12⟩, hdfδ, hδ⟩, refine ⟨δ, hδ0, λ J hJI hJδ hxJ hJc, add_halves ε ▸ _⟩, have Hl : J.lower i ∈ Icc (J.lower i) (J.upper i) := set.left_mem_Icc.2 (J.lower_le_upper i), have Hu : J.upper i ∈ Icc (J.lower i) (J.upper i) := set.right_mem_Icc.2 (J.lower_le_upper i), have Hi : ∀ x ∈ Icc (J.lower i) (J.upper i), integrable.{0 u u} (J.face i) GP (λ y, f (i.insert_nth x y)) box_additive_map.volume, from λ x hx, integrable_of_continuous_on _ (box.continuous_on_face_Icc (Hc.mono $ box.le_iff_Icc.1 hJI) hx) volume, have hJδ' : J.Icc ⊆ closed_ball x δ ∩ I.Icc, from subset_inter hJδ (box.le_iff_Icc.1 hJI), have Hmaps : ∀ z ∈ Icc (J.lower i) (J.upper i), maps_to (i.insert_nth z) (J.face i).Icc (closed_ball x δ ∩ I.Icc), from λ z hz, (J.maps_to_insert_nth_face_Icc hz).mono subset.rfl hJδ', simp only [dist_eq_norm, F, fI], dsimp, rw [← integral_sub (Hi _ Hu) (Hi _ Hl)], refine (norm_sub_le _ _).trans (add_le_add _ _), { simp_rw [box_additive_map.volume_apply, norm_smul, real.norm_eq_abs, abs_prod], refine (mul_le_mul_of_nonneg_right _ $ norm_nonneg _).trans hδ, have : ∀ j, |J.upper j - J.lower j| ≤ 2 * δ, { intro j, calc dist (J.upper j) (J.lower j) ≤ dist J.upper J.lower : dist_le_pi_dist _ _ _ ... ≤ dist J.upper x + dist J.lower x : dist_triangle_right _ _ _ ... ≤ δ + δ : add_le_add (hJδ J.upper_mem_Icc) (hJδ J.lower_mem_Icc) ... = 2 * δ : (two_mul δ).symm }, calc (∏ j, |J.upper j - J.lower j|) ≤ ∏ j : fin (n + 1), (2 * δ) : prod_le_prod (λ _ _ , abs_nonneg _) (λ j hj, this j) ... = (2 * δ) ^ (n + 1) : by simp }, { refine (norm_integral_le_of_le_const (λ y hy, hdfδ _ (Hmaps _ Hu hy) _ (Hmaps _ Hl hy)) _).trans _, refine (mul_le_mul_of_nonneg_right _ (half_pos ε0).le).trans_eq (one_mul _), rw [box.coe_eq_pi, real.volume_pi_Ioc_to_real (box.lower_le_upper _)], refine prod_le_one (λ _ _, sub_nonneg.2 $ box.lower_le_upper _ _) (λ j hj, _), calc J.upper (i.succ_above j) - J.lower (i.succ_above j) ≤ dist (J.upper (i.succ_above j)) (J.lower (i.succ_above j)) : le_abs_self _ ... ≤ dist J.upper J.lower : dist_le_pi_dist J.upper J.lower (i.succ_above j) ... ≤ dist J.upper x + dist J.lower x : dist_triangle_right _ _ _ ... ≤ δ + δ : add_le_add (hJδ J.upper_mem_Icc) (hJδ J.lower_mem_Icc) ... ≤ 1 / 2 + 1 / 2 : add_le_add hδ12 hδ12 ... = 1 : add_halves 1 } }, { intros c x hx ε ε0, /- At a point `x ∉ s`, we unfold the definition of Fréchet differentiability, then use an estimate we proved earlier in this file. -/ rcases exists_pos_mul_lt ε0 (2 * c) with ⟨ε', ε'0, hlt⟩, rcases (nhds_within_has_basis nhds_basis_closed_ball _).mem_iff.1 ((Hd x hx).def ε'0) with ⟨δ, δ0, Hδ⟩, refine ⟨δ, δ0, λ J hle hJδ hxJ hJc, _⟩, simp only [box_additive_map.volume_apply, box.volume_apply, dist_eq_norm], refine (norm_volume_sub_integral_face_upper_sub_lower_smul_le _ (Hc.mono $ box.le_iff_Icc.1 hle) hxJ ε'0 (λ y hy, Hδ _) (hJc rfl)).trans _, { exact ⟨hJδ hy, box.le_iff_Icc.1 hle hy⟩ }, { rw [mul_right_comm (2 : ℝ), ← box.volume_apply], exact mul_le_mul_of_nonneg_right hlt.le ennreal.to_real_nonneg } } end /-- Divergence theorem for a Henstock-Kurzweil style integral. If `f : ℝⁿ⁺¹ → Eⁿ⁺¹` is differentiable on a closed rectangular box `I` with derivative `f'`, then the divergence `∑ i, f' x (pi.single i 1) i` is Henstock-Kurzweil integrable with integral equal to the sum of integrals of `f` over the faces of `I` taken with appropriate signs. More precisely, we use a non-standard generalization of the Henstock-Kurzweil integral and we allow `f` to be non-differentiable (but still continuous) at a countable set of points. -/ lemma has_integral_GP_divergence_of_forall_has_deriv_within_at (f : ℝⁿ⁺¹ → Eⁿ⁺¹) (f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹) (s : set ℝⁿ⁺¹) (hs : s.countable) (Hs : ∀ x ∈ s, continuous_within_at f I.Icc x) (Hd : ∀ x ∈ I.Icc \ s, has_fderiv_within_at f (f' x) I.Icc x) : has_integral.{0 u u} I GP (λ x, ∑ i, f' x (pi.single i 1) i) box_additive_map.volume (∑ i, (integral.{0 u u} (I.face i) GP (λ x, f (i.insert_nth (I.upper i) x) i) box_additive_map.volume - integral.{0 u u} (I.face i) GP (λ x, f (i.insert_nth (I.lower i) x) i) box_additive_map.volume)) := begin refine has_integral_sum (λ i hi, _), clear hi, simp only [has_fderiv_within_at_pi', continuous_within_at_pi] at Hd Hs, convert has_integral_GP_pderiv I _ _ s hs (λ x hx, Hs x hx i) (λ x hx, Hd x hx i) i end end box_integral
d32c9ccc8a18d09701a408741919c9052958cb4c
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/extend_from.lean
5fa21fb42a4d0b0a14095692b5a59a95874835b3
[ "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
4,128
lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Anatole Dedecker -/ import topology.separation /-! # Extending a function from a subset > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The main definition of this file is `extend_from A f` where `f : X → Y` and `A : set X`. This defines a new function `g : X → Y` which maps any `x₀ : X` to the limit of `f` as `x` tends to `x₀`, if such a limit exists. This is analoguous to the way `dense_inducing.extend` "extends" a function `f : X → Z` to a function `g : Y → Z` along a dense inducing `i : X → Y`. The main theorem we prove about this definition is `continuous_on_extend_from` which states that, for `extend_from A f` to be continuous on a set `B ⊆ closure A`, it suffices that `f` converges within `A` at any point of `B`, provided that `f` is a function to a T₃ space. -/ noncomputable theory open_locale topology open filter set variables {X Y : Type*} [topological_space X] [topological_space Y] /-- Extend a function from a set `A`. The resulting function `g` is such that at any `x₀`, if `f` converges to some `y` as `x` tends to `x₀` within `A`, then `g x₀` is defined to be one of these `y`. Else, `g x₀` could be anything. -/ def extend_from (A : set X) (f : X → Y) : X → Y := λ x, @@lim _ ⟨f x⟩ (𝓝[A] x) f /-- If `f` converges to some `y` as `x` tends to `x₀` within `A`, then `f` tends to `extend_from A f x` as `x` tends to `x₀`. -/ lemma tendsto_extend_from {A : set X} {f : X → Y} {x : X} (h : ∃ y, tendsto f (𝓝[A] x) (𝓝 y)) : tendsto f (𝓝[A] x) (𝓝 $ extend_from A f x) := tendsto_nhds_lim h lemma extend_from_eq [t2_space Y] {A : set X} {f : X → Y} {x : X} {y : Y} (hx : x ∈ closure A) (hf : tendsto f (𝓝[A] x) (𝓝 y)) : extend_from A f x = y := begin haveI := mem_closure_iff_nhds_within_ne_bot.mp hx, exact tendsto_nhds_unique (tendsto_nhds_lim ⟨y, hf⟩) hf, end lemma extend_from_extends [t2_space Y] {f : X → Y} {A : set X} (hf : continuous_on f A) : ∀ x ∈ A, extend_from A f x = f x := λ x x_in, extend_from_eq (subset_closure x_in) (hf x x_in) /-- If `f` is a function to a T₃ space `Y` which has a limit within `A` at any point of a set `B ⊆ closure A`, then `extend_from A f` is continuous on `B`. -/ lemma continuous_on_extend_from [regular_space Y] {f : X → Y} {A B : set X} (hB : B ⊆ closure A) (hf : ∀ x ∈ B, ∃ y, tendsto f (𝓝[A] x) (𝓝 y)) : continuous_on (extend_from A f) B := begin set φ := extend_from A f, intros x x_in, suffices : ∀ V' ∈ 𝓝 (φ x), is_closed V' → φ ⁻¹' V' ∈ 𝓝[B] x, by simpa [continuous_within_at, (closed_nhds_basis _).tendsto_right_iff], intros V' V'_in V'_closed, obtain ⟨V, V_in, V_op, hV⟩ : ∃ V ∈ 𝓝 x, is_open V ∧ V ∩ A ⊆ f ⁻¹' V', { have := tendsto_extend_from (hf x x_in), rcases (nhds_within_basis_open x A).tendsto_left_iff.mp this V' V'_in with ⟨V, ⟨hxV, V_op⟩, hV⟩, use [V, is_open.mem_nhds V_op hxV, V_op, hV] }, suffices : ∀ y ∈ V ∩ B, φ y ∈ V', from mem_of_superset (inter_mem_inf V_in $ mem_principal_self B) this, rintros y ⟨hyV, hyB⟩, haveI := mem_closure_iff_nhds_within_ne_bot.mp (hB hyB), have limy : tendsto f (𝓝[A] y) (𝓝 $ φ y) := tendsto_extend_from (hf y hyB), have hVy : V ∈ 𝓝 y := is_open.mem_nhds V_op hyV, have : V ∩ A ∈ (𝓝[A] y), by simpa [inter_comm] using inter_mem_nhds_within _ hVy, exact V'_closed.mem_of_tendsto limy (mem_of_superset this hV) end /-- If a function `f` to a T₃ space `Y` has a limit within a dense set `A` for any `x`, then `extend_from A f` is continuous. -/ lemma continuous_extend_from [regular_space Y] {f : X → Y} {A : set X} (hA : dense A) (hf : ∀ x, ∃ y, tendsto f (𝓝[A] x) (𝓝 y)) : continuous (extend_from A f) := begin rw continuous_iff_continuous_on_univ, exact continuous_on_extend_from (λ x _, hA x) (by simpa using hf) end
f9825aac3b728bb0384de2ec8140a37dd182f65f
1a61aba1b67cddccce19532a9596efe44be4285f
/library/data/rat/order.lean
5dedf223a7e333e02088f7bbce79004539829079
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
14,179
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Adds the ordering, and instantiates the rationals as an ordered field. -/ import data.int algebra.ordered_field .basic open quot eq.ops /- the ordering on representations -/ namespace prerat section int_notation open int variables {a b : prerat} definition pos (a : prerat) : Prop := num a > 0 definition nonneg (a : prerat) : Prop := num a ≥ 0 theorem pos_of_int (a : ℤ) : pos (of_int a) ↔ (#int a > 0) := !iff.rfl theorem nonneg_of_int (a : ℤ) : nonneg (of_int a) ↔ (#int a ≥ 0) := !iff.rfl theorem pos_eq_pos_of_equiv {a b : prerat} (H1 : a ≡ b) : pos a = pos b := propext (iff.intro (num_pos_of_equiv H1) (num_pos_of_equiv H1⁻¹)) theorem nonneg_eq_nonneg_of_equiv (H : a ≡ b) : nonneg a = nonneg b := have H1 : (0 = num a) = (0 = num b), from propext (iff.intro (assume H2, eq.symm (num_eq_zero_of_equiv H H2⁻¹)) (assume H2, eq.symm (num_eq_zero_of_equiv H⁻¹ H2⁻¹))), calc nonneg a = (pos a ∨ 0 = num a) : propext !le_iff_lt_or_eq ... = (pos b ∨ 0 = num a) : pos_eq_pos_of_equiv H ... = (pos b ∨ 0 = num b) : H1 ... = nonneg b : propext !le_iff_lt_or_eq theorem nonneg_zero : nonneg zero := le.refl 0 theorem nonneg_add (H1 : nonneg a) (H2 : nonneg b) : nonneg (add a b) := show num a * denom b + num b * denom a ≥ 0, from add_nonneg (mul_nonneg H1 (le_of_lt (denom_pos b))) (mul_nonneg H2 (le_of_lt (denom_pos a))) theorem nonneg_antisymm (H1 : nonneg a) (H2 : nonneg (neg a)) : a ≡ zero := have H3 : num a = 0, from le.antisymm (nonpos_of_neg_nonneg H2) H1, equiv_zero_of_num_eq_zero H3 theorem nonneg_total (a : prerat) : nonneg a ∨ nonneg (neg a) := or.elim (le.total 0 (num a)) (suppose 0 ≤ num a, or.inl this) (suppose 0 ≥ num a, or.inr (neg_nonneg_of_nonpos this)) theorem nonneg_of_pos (H : pos a) : nonneg a := le_of_lt H theorem ne_zero_of_pos (H : pos a) : ¬ a ≡ zero := assume H', ne_of_gt H (num_eq_zero_of_equiv_zero H') theorem pos_of_nonneg_of_ne_zero (H1 : nonneg a) (H2 : ¬ a ≡ zero) : pos a := have num a ≠ 0, from suppose num a = 0, H2 (equiv_zero_of_num_eq_zero this), lt_of_le_of_ne H1 (ne.symm this) theorem nonneg_mul (H1 : nonneg a) (H2 : nonneg b) : nonneg (mul a b) := mul_nonneg H1 H2 theorem pos_mul (H1 : pos a) (H2 : pos b) : pos (mul a b) := mul_pos H1 H2 end int_notation end prerat local attribute prerat.setoid [instance] /- The ordering on the rationals. The definitions of pos and nonneg are kept private, because they are only meant for internal use. Users should use a > 0 and a ≥ 0 instead of pos and nonneg. -/ namespace rat variables {a b c : ℚ} /- transfer properties of pos and nonneg -/ private definition pos (a : ℚ) : Prop := quot.lift prerat.pos @prerat.pos_eq_pos_of_equiv a private definition nonneg (a : ℚ) : Prop := quot.lift prerat.nonneg @prerat.nonneg_eq_nonneg_of_equiv a private theorem pos_of_int (a : ℤ) : (#int a > 0) ↔ pos (of_int a) := prerat.pos_of_int a private theorem nonneg_of_int (a : ℤ) : (#int a ≥ 0) ↔ nonneg (of_int a) := prerat.nonneg_of_int a private theorem nonneg_zero : nonneg 0 := prerat.nonneg_zero private theorem nonneg_add : nonneg a → nonneg b → nonneg (a + b) := quot.induction_on₂ a b @prerat.nonneg_add private theorem nonneg_antisymm : nonneg a → nonneg (-a) → a = 0 := quot.induction_on a (take u, assume H1 H2, quot.sound (prerat.nonneg_antisymm H1 H2)) private theorem nonneg_total (a : ℚ) : nonneg a ∨ nonneg (-a) := quot.induction_on a @prerat.nonneg_total private theorem nonneg_of_pos : pos a → nonneg a := quot.induction_on a @prerat.nonneg_of_pos private theorem ne_zero_of_pos : pos a → a ≠ 0 := quot.induction_on a (take u, assume H1 H2, prerat.ne_zero_of_pos H1 (quot.exact H2)) private theorem pos_of_nonneg_of_ne_zero : nonneg a → ¬ a = 0 → pos a := quot.induction_on a (take u, assume h : nonneg ⟦u⟧, suppose ⟦u⟧ ≠ (rat.of_num 0), have ¬ (prerat.equiv u prerat.zero), from assume H, this (quot.sound H), prerat.pos_of_nonneg_of_ne_zero h this) private theorem nonneg_mul : nonneg a → nonneg b → nonneg (a * b) := quot.induction_on₂ a b @prerat.nonneg_mul private theorem pos_mul : pos a → pos b → pos (a * b) := quot.induction_on₂ a b @prerat.pos_mul private definition decidable_pos (a : ℚ) : decidable (pos a) := quot.rec_on_subsingleton a (take u, int.decidable_lt 0 (prerat.num u)) /- define order in terms of pos and nonneg -/ definition lt (a b : ℚ) : Prop := pos (b - a) definition le (a b : ℚ) : Prop := nonneg (b - a) definition gt [reducible] (a b : ℚ) := lt b a definition ge [reducible] (a b : ℚ) := le b a infix [priority rat.prio] < := rat.lt infix [priority rat.prio] <= := rat.le infix [priority rat.prio] ≤ := rat.le infix [priority rat.prio] >= := rat.ge infix [priority rat.prio] ≥ := rat.ge infix [priority rat.prio] > := rat.gt theorem of_int_lt_of_int (a b : ℤ) : of_int a < of_int b ↔ (#int a < b) := iff.symm (calc (#int a < b) ↔ (#int b - a > 0) : iff.symm !int.sub_pos_iff_lt ... ↔ pos (of_int (#int b - a)) : iff.symm !pos_of_int ... ↔ pos (of_int b - of_int a) : !of_int_sub ▸ iff.rfl ... ↔ of_int a < of_int b : iff.rfl) theorem of_int_le_of_int (a b : ℤ) : of_int a ≤ of_int b ↔ (#int a ≤ b) := iff.symm (calc (#int a ≤ b) ↔ (#int b - a ≥ 0) : iff.symm !int.sub_nonneg_iff_le ... ↔ nonneg (of_int (#int b - a)) : iff.symm !nonneg_of_int ... ↔ nonneg (of_int b - of_int a) : !of_int_sub ▸ iff.rfl ... ↔ of_int a ≤ of_int b : iff.rfl) theorem of_int_pos (a : ℤ) : (of_int a > 0) ↔ (#int a > 0) := !of_int_lt_of_int theorem of_int_nonneg (a : ℤ) : (of_int a ≥ 0) ↔ (#int a ≥ 0) := !of_int_le_of_int theorem of_nat_lt_of_nat (a b : ℕ) : of_nat a < of_nat b ↔ (#nat a < b) := by rewrite [*of_nat_eq, propext !of_int_lt_of_int]; apply int.of_nat_lt_of_nat theorem of_nat_le_of_nat (a b : ℕ) : of_nat a ≤ of_nat b ↔ (#nat a ≤ b) := by rewrite [*of_nat_eq, propext !of_int_le_of_int]; apply int.of_nat_le_of_nat theorem of_nat_pos (a : ℕ) : (of_nat a > 0) ↔ (#nat a > nat.zero) := !of_nat_lt_of_nat theorem of_nat_nonneg (a : ℕ) : (of_nat a ≥ 0) := iff.mpr !of_nat_le_of_nat !nat.zero_le theorem le.refl (a : ℚ) : a ≤ a := by rewrite [↑rat.le, sub_self]; apply nonneg_zero theorem le.trans (H1 : a ≤ b) (H2 : b ≤ c) : a ≤ c := assert H3 : nonneg (c - b + (b - a)), from nonneg_add H2 H1, begin revert H3, rewrite [↑rat.sub, add.assoc, neg_add_cancel_left], intro H3, apply H3 end theorem le.antisymm (H1 : a ≤ b) (H2 : b ≤ a) : a = b := have H3 : nonneg (-(a - b)), from !neg_sub⁻¹ ▸ H1, have H4 : a - b = 0, from nonneg_antisymm H2 H3, eq_of_sub_eq_zero H4 theorem le.total (a b : ℚ) : a ≤ b ∨ b ≤ a := or.elim (nonneg_total (b - a)) (assume H, or.inl H) (assume H, or.inr (!neg_sub ▸ H)) theorem le.by_cases {P : Prop} (a b : ℚ) (H : a ≤ b → P) (H2 : b ≤ a → P) : P := or.elim (!rat.le.total) H H2 theorem lt_iff_le_and_ne (a b : ℚ) : a < b ↔ a ≤ b ∧ a ≠ b := iff.intro (assume H : a < b, have b - a ≠ 0, from ne_zero_of_pos H, have a ≠ b, from ne.symm (assume H', this (H' ▸ !sub_self)), and.intro (nonneg_of_pos H) this) (assume H : a ≤ b ∧ a ≠ b, obtain aleb aneb, from H, have b - a ≠ 0, from (assume H', aneb (eq_of_sub_eq_zero H')⁻¹), pos_of_nonneg_of_ne_zero aleb this) theorem le_iff_lt_or_eq (a b : ℚ) : a ≤ b ↔ a < b ∨ a = b := iff.intro (assume h : a ≤ b, decidable.by_cases (suppose a = b, or.inr this) (suppose a ≠ b, or.inl (iff.mpr !lt_iff_le_and_ne (and.intro h this)))) (suppose a < b ∨ a = b, or.elim this (suppose a < b, and.left (iff.mp !lt_iff_le_and_ne this)) (suppose a = b, this ▸ !le.refl)) private theorem to_nonneg : a ≥ 0 → nonneg a := by intros; rewrite -sub_zero; eassumption theorem add_le_add_left (H : a ≤ b) (c : ℚ) : c + a ≤ c + b := have c + b - (c + a) = b - a, by rewrite [↑sub, neg_add, -add.assoc, add.comm c, add_neg_cancel_right], show nonneg (c + b - (c + a)), from this⁻¹ ▸ H theorem mul_nonneg (H1 : a ≥ (0 : ℚ)) (H2 : b ≥ (0 : ℚ)) : a * b ≥ (0 : ℚ) := have nonneg (a * b), from nonneg_mul (to_nonneg H1) (to_nonneg H2), !sub_zero⁻¹ ▸ this private theorem to_pos : a > 0 → pos a := by intros; rewrite -sub_zero; eassumption theorem mul_pos (H1 : a > (0 : ℚ)) (H2 : b > (0 : ℚ)) : a * b > (0 : ℚ) := have pos (a * b), from pos_mul (to_pos H1) (to_pos H2), !sub_zero⁻¹ ▸ this definition decidable_lt [instance] : decidable_rel rat.lt := take a b, decidable_pos (b - a) theorem le_of_lt (H : a < b) : a ≤ b := iff.mpr !le_iff_lt_or_eq (or.inl H) theorem lt_irrefl (a : ℚ) : ¬ a < a := take Ha, let Hand := (iff.mp !lt_iff_le_and_ne) Ha in (and.right Hand) rfl theorem not_le_of_gt (H : a < b) : ¬ b ≤ a := assume Hba, let Heq := le.antisymm (le_of_lt H) Hba in !lt_irrefl (Heq ▸ H) theorem lt_of_lt_of_le (Hab : a < b) (Hbc : b ≤ c) : a < c := let Hab' := le_of_lt Hab in let Hac := le.trans Hab' Hbc in (iff.mpr !lt_iff_le_and_ne) (and.intro Hac (assume Heq, not_le_of_gt (Heq ▸ Hab) Hbc)) theorem lt_of_le_of_lt (Hab : a ≤ b) (Hbc : b < c) : a < c := let Hbc' := le_of_lt Hbc in let Hac := le.trans Hab Hbc' in (iff.mpr !lt_iff_le_and_ne) (and.intro Hac (assume Heq, not_le_of_gt (Heq⁻¹ ▸ Hbc) Hab)) theorem zero_lt_one : (0 : ℚ) < 1 := trivial theorem add_lt_add_left (H : a < b) (c : ℚ) : c + a < c + b := let H' := le_of_lt H in (iff.mpr (lt_iff_le_and_ne _ _)) (and.intro (add_le_add_left H' _) (take Heq, let Heq' := add_left_cancel Heq in !lt_irrefl (Heq' ▸ H))) section migrate_algebra open [classes] algebra protected definition discrete_linear_ordered_field [reducible] : algebra.discrete_linear_ordered_field rat := ⦃algebra.discrete_linear_ordered_field, rat.discrete_field, le_refl := le.refl, le_trans := @le.trans, le_antisymm := @le.antisymm, le_total := @le.total, le_of_lt := @le_of_lt, lt_irrefl := lt_irrefl, lt_of_lt_of_le := @lt_of_lt_of_le, lt_of_le_of_lt := @lt_of_le_of_lt, le_iff_lt_or_eq := @le_iff_lt_or_eq, add_le_add_left := @add_le_add_left, mul_nonneg := @mul_nonneg, mul_pos := @mul_pos, decidable_lt := @decidable_lt, zero_lt_one := zero_lt_one, add_lt_add_left := @add_lt_add_left⦄ local attribute rat.discrete_linear_ordered_field [trans-instance] local attribute rat.discrete_field [instance] definition min : ℚ → ℚ → ℚ := algebra.min definition max : ℚ → ℚ → ℚ := algebra.max definition abs : ℚ → ℚ := algebra.abs definition sign : ℚ → ℚ := algebra.sign migrate from algebra with rat replacing sub → sub, dvd → dvd, has_le.ge → ge, has_lt.gt → gt, divide → divide, max → max, min → min, abs → abs, sign → sign, nmul → nmul, imul → imul attribute le.trans lt.trans lt_of_lt_of_le lt_of_le_of_lt ge.trans gt.trans gt_of_gt_of_ge gt_of_ge_of_gt [trans] attribute decidable_le [instance] end migrate_algebra theorem rat_of_nat_abs (a : ℤ) : abs (of_int a) = of_nat (int.nat_abs a) := assert ∀ n : ℕ, of_int (int.neg_succ_of_nat n) = - of_nat (nat.succ n), from λ n, rfl, int.induction_on a (take b, abs_of_nonneg (!of_nat_nonneg)) (take b, by rewrite [this, abs_neg, abs_of_nonneg (!of_nat_nonneg)]) section open int set_option pp.coercions true theorem num_nonneg_of_nonneg {q : ℚ} (H : q ≥ 0) : num q ≥ 0 := have of_int (num q) ≥ of_int 0, begin rewrite [-mul_denom], apply mul_nonneg H, rewrite [of_int_le_of_int], exact int.le_of_lt !denom_pos end, show num q ≥ 0, from iff.mp !of_int_le_of_int this theorem num_pos_of_pos {q : ℚ} (H : q > 0) : num q > 0 := have of_int (num q) > of_int 0, begin rewrite [-mul_denom], apply mul_pos H, rewrite [of_int_lt_of_int], exact !denom_pos end, show num q > 0, from iff.mp !of_int_lt_of_int this theorem num_neg_of_neg {q : ℚ} (H : q < 0) : num q < 0 := have of_int (num q) < of_int 0, begin rewrite [-mul_denom], apply mul_neg_of_neg_of_pos H, rewrite [of_int_lt_of_int], exact !denom_pos end, show num q < 0, from iff.mp !of_int_lt_of_int this theorem num_nonpos_of_nonpos {q : ℚ} (H : q ≤ 0) : num q ≤ 0 := have of_int (num q) ≤ of_int 0, begin rewrite [-mul_denom], apply mul_nonpos_of_nonpos_of_nonneg H, rewrite [of_int_le_of_int], exact int.le_of_lt !denom_pos end, show num q ≤ 0, from iff.mp !of_int_le_of_int this end definition ubound : ℚ → ℕ := λ a : ℚ, nat.succ (int.nat_abs (num a)) theorem ubound_ge (a : ℚ) : of_nat (ubound a) ≥ a := have h : abs a * abs (of_int (denom a)) = abs (of_int (num a)), from !abs_mul ▸ !mul_denom ▸ rfl, assert of_int (denom a) > 0, from (iff.mpr !of_int_pos) !denom_pos, have 1 ≤ abs (of_int (denom a)), begin rewrite (abs_of_pos this), apply iff.mpr !of_int_le_of_int, apply denom_pos end, have abs a ≤ abs (of_int (num a)), from le_of_mul_le_of_ge_one (h ▸ !le.refl) !abs_nonneg this, calc a ≤ abs a : le_abs_self ... ≤ abs (of_int (num a)) : this ... ≤ abs (of_int (num a)) + 1 : rat.le_add_of_nonneg_right trivial ... = of_nat (int.nat_abs (num a)) + 1 : rat_of_nat_abs ... = of_nat (nat.succ (int.nat_abs (num a))) : of_nat_add theorem ubound_pos (a : ℚ) : nat.gt (ubound a) nat.zero := !nat.succ_pos end rat
3de0499ebf77b15f79c495cb578063bae97e2078
c777c32c8e484e195053731103c5e52af26a25d1
/src/topology/algebra/valued_field.lean
9bf06e431ca653c28093a407e1eeb0b0ee250dbc
[ "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
14,617
lean
/- Copyright (c) 2021 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import topology.algebra.valuation import topology.algebra.with_zero_topology import topology.algebra.uniform_field /-! # Valued fields and their completions In this file we study the topology of a field `K` endowed with a valuation (in our application to adic spaces, `K` will be the valuation field associated to some valuation on a ring, defined in valuation.basic). We already know from valuation.topology that one can build a topology on `K` which makes it a topological ring. The first goal is to show `K` is a topological *field*, ie inversion is continuous at every non-zero element. The next goal is to prove `K` is a *completable* topological field. This gives us a completion `hat K` which is a topological field. We also prove that `K` is automatically separated, so the map from `K` to `hat K` is injective. Then we extend the valuation given on `K` to a valuation on `hat K`. -/ open filter set open_locale topology section division_ring variables {K : Type*} [division_ring K] {Γ₀ : Type*} [linear_ordered_comm_group_with_zero Γ₀] section valuation_topological_division_ring section inversion_estimate variables (v : valuation K Γ₀) -- The following is the main technical lemma ensuring that inversion is continuous -- in the topology induced by a valuation on a division ring (ie the next instance) -- and the fact that a valued field is completable -- [BouAC, VI.5.1 Lemme 1] lemma valuation.inversion_estimate {x y : K} {γ : Γ₀ˣ} (y_ne : y ≠ 0) (h : v (x - y) < min (γ * ((v y) * (v y))) (v y)) : v (x⁻¹ - y⁻¹) < γ := begin have hyp1 : v (x - y) < γ * ((v y) * (v y)), from lt_of_lt_of_le h (min_le_left _ _), have hyp1' : v (x - y) * ((v y) * (v y))⁻¹ < γ, from mul_inv_lt_of_lt_mul₀ hyp1, have hyp2 : v (x - y) < v y, from lt_of_lt_of_le h (min_le_right _ _), have key : v x = v y, from valuation.map_eq_of_sub_lt v hyp2, have x_ne : x ≠ 0, { intro h, apply y_ne, rw [h, v.map_zero] at key, exact v.zero_iff.1 key.symm }, have decomp : x⁻¹ - y⁻¹ = x⁻¹ * (y - x) * y⁻¹, by rw [mul_sub_left_distrib, sub_mul, mul_assoc, show y * y⁻¹ = 1, from mul_inv_cancel y_ne, show x⁻¹ * x = 1, from inv_mul_cancel x_ne, mul_one, one_mul], calc v (x⁻¹ - y⁻¹) = v (x⁻¹ * (y - x) * y⁻¹) : by rw decomp ... = (v x⁻¹) * (v $ y - x) * (v y⁻¹) : by repeat { rw valuation.map_mul } ... = (v x)⁻¹ * (v $ y - x) * (v y)⁻¹ : by rw [map_inv₀, map_inv₀] ... = (v $ y - x) * ((v y) * (v y))⁻¹ : by { rw [mul_assoc, mul_comm, key, mul_assoc, mul_inv_rev] } ... = (v $ y - x) * ((v y) * (v y))⁻¹ : rfl ... = (v $ x - y) * ((v y) * (v y))⁻¹ : by rw valuation.map_sub_swap ... < γ : hyp1', end end inversion_estimate open valued /-- The topology coming from a valuation on a division ring makes it a topological division ring [BouAC, VI.5.1 middle of Proposition 1] -/ @[priority 100] instance valued.topological_division_ring [valued K Γ₀] : topological_division_ring K := { continuous_at_inv₀ := begin intros x x_ne s s_in, cases valued.mem_nhds.mp s_in with γ hs, clear s_in, rw [mem_map, valued.mem_nhds], change ∃ (γ : Γ₀ˣ), {y : K | (v (y - x) : Γ₀) < γ} ⊆ {x : K | x⁻¹ ∈ s}, have vx_ne := (valuation.ne_zero_iff $ v).mpr x_ne, let γ' := units.mk0 _ vx_ne, use min (γ * (γ'*γ')) γ', intros y y_in, apply hs, simp only [mem_set_of_eq] at y_in, rw [units.min_coe, units.coe_mul, units.coe_mul] at y_in, exact valuation.inversion_estimate _ x_ne y_in end, ..(by apply_instance : topological_ring K) } /-- A valued division ring is separated. -/ @[priority 100] instance valued_ring.separated [valued K Γ₀] : separated_space K := begin rw separated_iff_t2, apply topological_add_group.t2_space_of_zero_sep, intros x x_ne, refine ⟨{k | v k < v x}, _, λ h, lt_irrefl _ h⟩, rw valued.mem_nhds, have vx_ne := (valuation.ne_zero_iff $ v).mpr x_ne, let γ' := units.mk0 _ vx_ne, exact ⟨γ', λ y hy, by simpa using hy⟩, end section open_locale with_zero_topology open valued lemma valued.continuous_valuation [valued K Γ₀] : continuous (v : K → Γ₀) := begin rw continuous_iff_continuous_at, intro x, rcases eq_or_ne x 0 with rfl|h, { rw [continuous_at, map_zero, with_zero_topology.tendsto_zero], intros γ hγ, rw [filter.eventually, valued.mem_nhds_zero], use [units.mk0 γ hγ, subset.rfl] }, { have v_ne : (v x : Γ₀) ≠ 0, from (valuation.ne_zero_iff _).mpr h, rw [continuous_at, with_zero_topology.tendsto_of_ne_zero v_ne], apply valued.loc_const v_ne }, end end end valuation_topological_division_ring end division_ring namespace valued open uniform_space variables {K : Type*} [field K] {Γ₀ : Type*} [linear_ordered_comm_group_with_zero Γ₀] [hv: valued K Γ₀] include hv local notation `hat ` := completion /-- A valued field is completable. -/ @[priority 100] instance completable : completable_top_field K := { nice := begin rintros F hF h0, have : ∃ (γ₀ : Γ₀ˣ) (M ∈ F), ∀ x ∈ M, (γ₀ : Γ₀) ≤ v x, { rcases filter.inf_eq_bot_iff.mp h0 with ⟨U, U_in, M, M_in, H⟩, rcases valued.mem_nhds_zero.mp U_in with ⟨γ₀, hU⟩, existsi [γ₀, M, M_in], intros x xM, apply le_of_not_lt _, intro hyp, have : x ∈ U ∩ M := ⟨hU hyp, xM⟩, rwa H at this }, rcases this with ⟨γ₀, M₀, M₀_in, H₀⟩, rw valued.cauchy_iff at hF ⊢, refine ⟨hF.1.map _, _⟩, replace hF := hF.2, intros γ, rcases hF (min (γ * γ₀ * γ₀) γ₀) with ⟨M₁, M₁_in, H₁⟩, clear hF, use (λ x : K, x⁻¹) '' (M₀ ∩ M₁), split, { rw mem_map, apply mem_of_superset (filter.inter_mem M₀_in M₁_in), exact subset_preimage_image _ _ }, { rintros _ ⟨x, ⟨x_in₀, x_in₁⟩, rfl⟩ _ ⟨y, ⟨y_in₀, y_in₁⟩, rfl⟩, simp only [mem_set_of_eq], specialize H₁ x x_in₁ y y_in₁, replace x_in₀ := H₀ x x_in₀, replace y_in₀ := H₀ y y_in₀, clear H₀, apply valuation.inversion_estimate, { have : (v x : Γ₀) ≠ 0, { intro h, rw h at x_in₀, simpa using x_in₀, }, exact (valuation.ne_zero_iff _).mp this }, { refine lt_of_lt_of_le H₁ _, rw units.min_coe, apply min_le_min _ x_in₀, rw mul_assoc, have : ((γ₀ * γ₀ : Γ₀ˣ) : Γ₀) ≤ v x * v x, from calc ↑γ₀ * ↑γ₀ ≤ ↑γ₀ * v x : mul_le_mul_left' x_in₀ ↑γ₀ ... ≤ _ : mul_le_mul_right' x_in₀ (v x), rw units.coe_mul, exact mul_le_mul_left' this γ } } end, ..valued_ring.separated } open_locale with_zero_topology /-- The extension of the valuation of a valued field to the completion of the field. -/ noncomputable def extension : hat K → Γ₀ := completion.dense_inducing_coe.extend (v : K → Γ₀) lemma continuous_extension : continuous (valued.extension : hat K → Γ₀) := begin refine completion.dense_inducing_coe.continuous_extend _, intro x₀, rcases eq_or_ne x₀ 0 with rfl|h, { refine ⟨0, _⟩, erw [← completion.dense_inducing_coe.to_inducing.nhds_eq_comap], exact valued.continuous_valuation.tendsto' 0 0 (map_zero v) }, { have preimage_one : v ⁻¹' {(1 : Γ₀)} ∈ 𝓝 (1 : K), { have : (v (1 : K) : Γ₀) ≠ 0, { rw valuation.map_one, exact zero_ne_one.symm }, convert valued.loc_const this, ext x, rw [valuation.map_one, mem_preimage, mem_singleton_iff, mem_set_of_eq] }, obtain ⟨V, V_in, hV⟩ : ∃ V ∈ 𝓝 (1 : hat K), ∀ x : K, (x : hat K) ∈ V → (v x : Γ₀) = 1, { rwa [completion.dense_inducing_coe.nhds_eq_comap, mem_comap] at preimage_one }, have : ∃ V' ∈ (𝓝 (1 : hat K)), (0 : hat K) ∉ V' ∧ ∀ x y ∈ V', x*y⁻¹ ∈ V, { have : tendsto (λ p : hat K × hat K, p.1*p.2⁻¹) ((𝓝 1).prod (𝓝 1)) (𝓝 1), { rw ← nhds_prod_eq, conv {congr, skip, skip, rw ← (one_mul (1 : hat K))}, refine tendsto.mul continuous_fst.continuous_at (tendsto.comp _ continuous_snd.continuous_at), convert continuous_at_inv₀ (zero_ne_one.symm : 1 ≠ (0 : hat K)), exact inv_one.symm }, rcases tendsto_prod_self_iff.mp this V V_in with ⟨U, U_in, hU⟩, let hatKstar := ({0}ᶜ : set $ hat K), have : hatKstar ∈ 𝓝 (1 : hat K), from compl_singleton_mem_nhds zero_ne_one.symm, use [U ∩ hatKstar, filter.inter_mem U_in this], split, { rintro ⟨h, h'⟩, rw mem_compl_singleton_iff at h', exact h' rfl }, { rintros x ⟨hx, _⟩ y ⟨hy, _⟩, apply hU ; assumption } }, rcases this with ⟨V', V'_in, zeroV', hV'⟩, have nhds_right : (λ x, x*x₀) '' V' ∈ 𝓝 x₀, { have l : function.left_inverse (λ x : hat K, x * x₀⁻¹) (λ x : hat K, x * x₀), { intro x, simp only [mul_assoc, mul_inv_cancel h, mul_one] }, have r: function.right_inverse (λ x : hat K, x * x₀⁻¹) (λ x : hat K, x * x₀), { intro x, simp only [mul_assoc, inv_mul_cancel h, mul_one] }, have c : continuous (λ x : hat K, x * x₀⁻¹), from continuous_id.mul continuous_const, rw image_eq_preimage_of_inverse l r, rw ← mul_inv_cancel h at V'_in, exact c.continuous_at V'_in }, have : ∃ (z₀ : K) (y₀ ∈ V'), coe z₀ = y₀*x₀ ∧ z₀ ≠ 0, { rcases completion.dense_range_coe.mem_nhds nhds_right with ⟨z₀, y₀, y₀_in, H : y₀ * x₀ = z₀⟩, refine ⟨z₀, y₀, y₀_in, ⟨H.symm, _⟩⟩, rintro rfl, exact mul_ne_zero (ne_of_mem_of_not_mem y₀_in zeroV') h H }, rcases this with ⟨z₀, y₀, y₀_in, hz₀, z₀_ne⟩, have vz₀_ne: (v z₀ : Γ₀) ≠ 0 := by rwa valuation.ne_zero_iff, refine ⟨v z₀, _⟩, rw [with_zero_topology.tendsto_of_ne_zero vz₀_ne, eventually_comap], filter_upwards [nhds_right] with x x_in a ha, rcases x_in with ⟨y, y_in, rfl⟩, have : (v (a * z₀⁻¹) : Γ₀) = 1, { apply hV, have : ((z₀⁻¹ : K) : hat K) = z₀⁻¹, from map_inv₀ (completion.coe_ring_hom : K →+* hat K) z₀, rw [completion.coe_mul, this, ha, hz₀, mul_inv, mul_comm y₀⁻¹, ← mul_assoc, mul_assoc y, mul_inv_cancel h, mul_one], solve_by_elim }, calc v a = v (a * z₀⁻¹ * z₀) : by rw [mul_assoc, inv_mul_cancel z₀_ne, mul_one] ... = v (a * z₀⁻¹) * v z₀ : valuation.map_mul _ _ _ ... = v z₀ : by rw [this, one_mul] }, end @[simp, norm_cast] lemma extension_extends (x : K) : extension (x : hat K) = v x := begin refine completion.dense_inducing_coe.extend_eq_of_tendsto _, rw ← completion.dense_inducing_coe.nhds_eq_comap, exact valued.continuous_valuation.continuous_at, end /-- the extension of a valuation on a division ring to its completion. -/ noncomputable def extension_valuation : valuation (hat K) Γ₀ := { to_fun := valued.extension, map_zero' := by { rw [← v.map_zero, ← valued.extension_extends (0 : K)], refl, }, map_one' := by { rw [← completion.coe_one, valued.extension_extends (1 : K)], exact valuation.map_one _ }, map_mul' := λ x y, begin apply completion.induction_on₂ x y, { have c1 : continuous (λ (x : hat K × hat K), valued.extension (x.1 * x.2)), from valued.continuous_extension.comp (continuous_fst.mul continuous_snd), have c2 : continuous (λ (x : hat K × hat K), valued.extension x.1 * valued.extension x.2), from (valued.continuous_extension.comp continuous_fst).mul (valued.continuous_extension.comp continuous_snd), exact is_closed_eq c1 c2 }, { intros x y, norm_cast, exact valuation.map_mul _ _ _ }, end, map_add_le_max' := λ x y, begin rw le_max_iff, apply completion.induction_on₂ x y, { have cont : continuous (valued.extension : hat K → Γ₀) := valued.continuous_extension, exact (is_closed_le (cont.comp continuous_add) $ cont.comp continuous_fst).union (is_closed_le (cont.comp continuous_add) $ cont.comp continuous_snd) }, { intros x y, dsimp, norm_cast, rw ← le_max_iff, exact v.map_add x y, }, end } -- Bourbaki CA VI §5 no.3 Proposition 5 (d) lemma closure_coe_completion_v_lt {γ : Γ₀ˣ} : closure (coe '' { x : K | v x < (γ : Γ₀) }) = { x : hat K | extension_valuation x < (γ : Γ₀) } := begin ext x, let γ₀ := extension_valuation x, suffices : γ₀ ≠ 0 → (x ∈ closure (coe '' { x : K | v x < (γ : Γ₀) }) ↔ γ₀ < (γ : Γ₀)), { cases eq_or_ne γ₀ 0, { simp only [h, (valuation.zero_iff _).mp h, mem_set_of_eq, valuation.map_zero, units.zero_lt, iff_true], apply subset_closure, exact ⟨0, by simpa only [mem_set_of_eq, valuation.map_zero, units.zero_lt, true_and]⟩, }, { exact this h, }, }, intros h, have hγ₀ : extension ⁻¹' {γ₀} ∈ 𝓝 x := continuous_extension.continuous_at.preimage_mem_nhds (with_zero_topology.singleton_mem_nhds_of_ne_zero h), rw mem_closure_iff_nhds', refine ⟨λ hx, _, λ hx s hs, _⟩, { obtain ⟨⟨-, y, hy₁ : v y < (γ : Γ₀), rfl⟩, hy₂⟩ := hx _ hγ₀, replace hy₂ : v y = γ₀, { simpa using hy₂, }, rwa ← hy₂, }, { obtain ⟨y, hy₁, hy₂ : ↑y ∈ s⟩ := completion.dense_range_coe.mem_nhds (inter_mem hγ₀ hs), replace hy₁ : v y = γ₀, { simpa using hy₁, }, rw ← hy₁ at hx, exact ⟨⟨y, ⟨y, hx, rfl⟩⟩, hy₂⟩, }, end noncomputable instance valued_completion : valued (hat K) Γ₀ := { v := extension_valuation, is_topological_valuation := λ s, begin suffices : has_basis (𝓝 (0 : hat K)) (λ _, true) (λ γ : Γ₀ˣ, { x | extension_valuation x < γ }), { rw this.mem_iff, exact exists_congr (λ γ, by simp), }, simp_rw ← closure_coe_completion_v_lt, exact (has_basis_nhds_zero K Γ₀).has_basis_of_dense_inducing completion.dense_inducing_coe, end } @[simp, norm_cast] lemma valued_completion_apply (x : K) : valued.v (x : hat K) = v x := extension_extends x end valued
64b561cf7cc09edb64042d50e00d7adac83fd8f7
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/number_theory/sum_four_squares.lean
4afaf3b0b2606dce1c58d8a189fb30e4abefd117
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,925
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.group_power.identities import data.zmod.basic import field_theory.finite.basic import data.int.parity import data.fintype.card /-! # Lagrange's four square theorem The main result in this file is `sum_four_squares`, a proof that every natural number is the sum of four square numbers. ## Implementation Notes The proof used is close to Lagrange's original proof. -/ open finset polynomial finite_field equiv open_locale big_operators namespace int lemma sum_two_squares_of_two_mul_sum_two_squares {m x y : ℤ} (h : 2 * m = x^2 + y^2) : m = ((x - y) / 2) ^ 2 + ((x + y) / 2) ^ 2 := have even (x^2 + y^2), by simp [h.symm, even_mul], have hxaddy : even (x + y), by simpa [pow_two] with parity_simps, have hxsuby : even (x - y), by simpa [pow_two] with parity_simps, (mul_right_inj' (show (2*2 : ℤ) ≠ 0, from dec_trivial)).1 $ calc 2 * 2 * m = (x - y)^2 + (x + y)^2 : by rw [mul_assoc, h]; ring ... = (2 * ((x - y) / 2))^2 + (2 * ((x + y) / 2))^2 : by rw [int.mul_div_cancel' hxsuby, int.mul_div_cancel' hxaddy] ... = 2 * 2 * (((x - y) / 2) ^ 2 + ((x + y) / 2) ^ 2) : by simp [mul_add, pow_succ, mul_comm, mul_assoc, mul_left_comm] lemma exists_sum_two_squares_add_one_eq_k (p : ℕ) [hp : fact p.prime] : ∃ (a b : ℤ) (k : ℕ), a^2 + b^2 + 1 = k * p ∧ k < p := hp.1.eq_two_or_odd.elim (λ hp2, hp2.symm ▸ ⟨1, 0, 1, rfl, dec_trivial⟩) $ λ hp1, let ⟨a, b, hab⟩ := zmod.sum_two_squares p (-1) in have hab' : (p : ℤ) ∣ a.val_min_abs ^ 2 + b.val_min_abs ^ 2 + 1, from (char_p.int_cast_eq_zero_iff (zmod p) p _).1 $ by simpa [eq_neg_iff_add_eq_zero] using hab, let ⟨k, hk⟩ := hab' in have hk0 : 0 ≤ k, from nonneg_of_mul_nonneg_left (by rw ← hk; exact (add_nonneg (add_nonneg (pow_two_nonneg _) (pow_two_nonneg _)) zero_le_one)) (int.coe_nat_pos.2 hp.1.pos), ⟨a.val_min_abs, b.val_min_abs, k.nat_abs, by rw [hk, int.nat_abs_of_nonneg hk0, mul_comm], lt_of_mul_lt_mul_left (calc p * k.nat_abs = a.val_min_abs.nat_abs ^ 2 + b.val_min_abs.nat_abs ^ 2 + 1 : by rw [← int.coe_nat_inj', int.coe_nat_add, int.coe_nat_add, int.coe_nat_pow, int.coe_nat_pow, int.nat_abs_pow_two, int.nat_abs_pow_two, int.coe_nat_one, hk, int.coe_nat_mul, int.nat_abs_of_nonneg hk0] ... ≤ (p / 2) ^ 2 + (p / 2)^2 + 1 : add_le_add (add_le_add (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) (le_refl _) ... < (p / 2) ^ 2 + (p / 2)^ 2 + (p % 2)^2 + ((2 * (p / 2)^2 + (4 * (p / 2) * (p % 2)))) : by rw [hp1, one_pow, mul_one]; exact (lt_add_iff_pos_right _).2 (add_pos_of_nonneg_of_pos (nat.zero_le _) (mul_pos dec_trivial (nat.div_pos hp.1.two_le dec_trivial))) ... = p * p : by { conv_rhs { rw [← nat.mod_add_div p 2] }, ring }) (show 0 ≤ p, from nat.zero_le _)⟩ end int namespace nat open int open_locale classical private lemma sum_four_squares_of_two_mul_sum_four_squares {m a b c d : ℤ} (h : a^2 + b^2 + c^2 + d^2 = 2 * m) : ∃ w x y z : ℤ, w^2 + x^2 + y^2 + z^2 = m := have ∀ f : fin 4 → zmod 2, (f 0)^2 + (f 1)^2 + (f 2)^2 + (f 3)^2 = 0 → ∃ i : (fin 4), (f i)^2 + f (swap i 0 1)^2 = 0 ∧ f (swap i 0 2)^2 + f (swap i 0 3)^2 = 0, from dec_trivial, let f : fin 4 → ℤ := vector.nth (a ::ᵥ b ::ᵥ c ::ᵥ d ::ᵥ vector.nil) in let ⟨i, hσ⟩ := this (coe ∘ f) (by rw [← @zero_mul (zmod 2) _ m, ← show ((2 : ℤ) : zmod 2) = 0, from rfl, ← int.cast_mul, ← h]; simp only [int.cast_add, int.cast_pow]; refl) in let σ := swap i 0 in have h01 : 2 ∣ f (σ 0) ^ 2 + f (σ 1) ^ 2, from (char_p.int_cast_eq_zero_iff (zmod 2) 2 _).1 $ by simpa [σ] using hσ.1, have h23 : 2 ∣ f (σ 2) ^ 2 + f (σ 3) ^ 2, from (char_p.int_cast_eq_zero_iff (zmod 2) 2 _).1 $ by simpa using hσ.2, let ⟨x, hx⟩ := h01 in let ⟨y, hy⟩ := h23 in ⟨(f (σ 0) - f (σ 1)) / 2, (f (σ 0) + f (σ 1)) / 2, (f (σ 2) - f (σ 3)) / 2, (f (σ 2) + f (σ 3)) / 2, begin rw [← int.sum_two_squares_of_two_mul_sum_two_squares hx.symm, add_assoc, ← int.sum_two_squares_of_two_mul_sum_two_squares hy.symm, ← mul_right_inj' (show (2 : ℤ) ≠ 0, from dec_trivial), ← h, mul_add, ← hx, ← hy], have : ∑ x, f (σ x)^2 = ∑ x, f x^2, { conv_rhs { rw ← σ.sum_comp } }, have fin4univ : (univ : finset (fin 4)).1 = 0 ::ₘ 1 ::ₘ 2 ::ₘ 3 ::ₘ 0, from dec_trivial, simpa [finset.sum_eq_multiset_sum, fin4univ, multiset.sum_cons, f, add_assoc] end⟩ private lemma prime_sum_four_squares (p : ℕ) [hp : _root_.fact p.prime] : ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = p := have hm : ∃ m < p, 0 < m ∧ ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = m * p, from let ⟨a, b, k, hk⟩ := exists_sum_two_squares_add_one_eq_k p in ⟨k, hk.2, nat.pos_of_ne_zero $ (λ hk0, by { rw [hk0, int.coe_nat_zero, zero_mul] at hk, exact ne_of_gt (show a^2 + b^2 + 1 > 0, from add_pos_of_nonneg_of_pos (add_nonneg (pow_two_nonneg _) (pow_two_nonneg _)) zero_lt_one) hk.1 }), a, b, 1, 0, by simpa [pow_two] using hk.1⟩, let m := nat.find hm in let ⟨a, b, c, d, (habcd : a^2 + b^2 + c^2 + d^2 = m * p)⟩ := (nat.find_spec hm).snd.2 in by haveI hm0 : _root_.fact (0 < m) := ⟨(nat.find_spec hm).snd.1⟩; exact have hmp : m < p, from (nat.find_spec hm).fst, m.mod_two_eq_zero_or_one.elim (λ hm2 : m % 2 = 0, let ⟨k, hk⟩ := (nat.dvd_iff_mod_eq_zero _ _).2 hm2 in have hk0 : 0 < k, from nat.pos_of_ne_zero $ λ _, by { simp [*, lt_irrefl] at * }, have hkm : k < m, { rw [hk, two_mul], exact (lt_add_iff_pos_left _).2 hk0 }, false.elim $ nat.find_min hm hkm ⟨lt_trans hkm hmp, hk0, sum_four_squares_of_two_mul_sum_four_squares (show a^2 + b^2 + c^2 + d^2 = 2 * (k * p), by { rw [habcd, hk, int.coe_nat_mul, mul_assoc], simp })⟩) (λ hm2 : m % 2 = 1, if hm1 : m = 1 then ⟨a, b, c, d, by simp only [hm1, habcd, int.coe_nat_one, one_mul]⟩ else let w := (a : zmod m).val_min_abs, x := (b : zmod m).val_min_abs, y := (c : zmod m).val_min_abs, z := (d : zmod m).val_min_abs in have hnat_abs : w^2 + x^2 + y^2 + z^2 = (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs ^2 + z.nat_abs ^ 2 : ℕ), by simp [pow_two], have hwxyzlt : w^2 + x^2 + y^2 + z^2 < m^2, from calc w^2 + x^2 + y^2 + z^2 = (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs ^2 + z.nat_abs ^ 2 : ℕ) : hnat_abs ... ≤ ((m / 2) ^ 2 + (m / 2) ^ 2 + (m / 2) ^ 2 + (m / 2) ^ 2 : ℕ) : int.coe_nat_le.2 $ add_le_add (add_le_add (add_le_add (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) ... = 4 * (m / 2 : ℕ) ^ 2 : by simp [pow_two, bit0, bit1, mul_add, add_mul, add_assoc] ... < 4 * (m / 2 : ℕ) ^ 2 + ((4 * (m / 2) : ℕ) * (m % 2 : ℕ) + (m % 2 : ℕ)^2) : (lt_add_iff_pos_right _).2 (by { rw [hm2, int.coe_nat_one, one_pow, mul_one], exact add_pos_of_nonneg_of_pos (int.coe_nat_nonneg _) zero_lt_one }) ... = m ^ 2 : by { conv_rhs {rw [← nat.mod_add_div m 2]}, simp [-nat.mod_add_div, mul_add, add_mul, bit0, bit1, mul_comm, mul_assoc, mul_left_comm, pow_add, add_comm, add_left_comm] }, have hwxyzabcd : ((w^2 + x^2 + y^2 + z^2 : ℤ) : zmod m) = ((a^2 + b^2 + c^2 + d^2 : ℤ) : zmod m), by simp [w, x, y, z, pow_two], have hwxyz0 : ((w^2 + x^2 + y^2 + z^2 : ℤ) : zmod m) = 0, by rw [hwxyzabcd, habcd, int.cast_mul, cast_coe_nat, zmod.nat_cast_self, zero_mul], let ⟨n, hn⟩ := ((char_p.int_cast_eq_zero_iff _ m _).1 hwxyz0) in have hn0 : 0 < n.nat_abs, from int.nat_abs_pos_of_ne_zero (λ hn0, have hwxyz0 : (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs^2 + z.nat_abs^2 : ℕ) = 0, by { rw [← int.coe_nat_eq_zero, ← hnat_abs], rwa [hn0, mul_zero] at hn }, have habcd0 : (m : ℤ) ∣ a ∧ (m : ℤ) ∣ b ∧ (m : ℤ) ∣ c ∧ (m : ℤ) ∣ d, by simpa [@add_eq_zero_iff_eq_zero_of_nonneg ℤ _ _ _ (pow_two_nonneg _) (pow_two_nonneg _), pow_two, w, x, y, z, (char_p.int_cast_eq_zero_iff _ m _), and.assoc] using hwxyz0, let ⟨ma, hma⟩ := habcd0.1, ⟨mb, hmb⟩ := habcd0.2.1, ⟨mc, hmc⟩ := habcd0.2.2.1, ⟨md, hmd⟩ := habcd0.2.2.2 in have hmdvdp : m ∣ p, from int.coe_nat_dvd.1 ⟨ma^2 + mb^2 + mc^2 + md^2, (mul_right_inj' (show (m : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 hm0.1)).1 $ by { rw [← habcd, hma, hmb, hmc, hmd], ring }⟩, (hp.1.2 _ hmdvdp).elim hm1 (λ hmeqp, by simpa [lt_irrefl, hmeqp] using hmp)), have hawbxcydz : ((m : ℕ) : ℤ) ∣ a * w + b * x + c * y + d * z, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { rw [← hwxyz0], simp, ring }, have haxbwczdy : ((m : ℕ) : ℤ) ∣ a * x - b * w - c * z + d * y, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { simp [sub_eq_add_neg], ring }, have haybzcwdx : ((m : ℕ) : ℤ) ∣ a * y + b * z - c * w - d * x, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { simp [sub_eq_add_neg], ring }, have hazbycxdw : ((m : ℕ) : ℤ) ∣ a * z - b * y + c * x - d * w, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { simp [sub_eq_add_neg], ring }, let ⟨s, hs⟩ := hawbxcydz, ⟨t, ht⟩ := haxbwczdy, ⟨u, hu⟩ := haybzcwdx, ⟨v, hv⟩ := hazbycxdw in have hn_nonneg : 0 ≤ n, from nonneg_of_mul_nonneg_left (by { erw [← hn], repeat {try {refine add_nonneg _ _}, try {exact pow_two_nonneg _}} }) (int.coe_nat_pos.2 hm0.1), have hnm : n.nat_abs < m, from int.coe_nat_lt.1 (lt_of_mul_lt_mul_left (by { rw [int.nat_abs_of_nonneg hn_nonneg, ← hn, ← pow_two], exact hwxyzlt }) (int.coe_nat_nonneg m)), have hstuv : s^2 + t^2 + u^2 + v^2 = n.nat_abs * p, from (mul_right_inj' (show (m^2 : ℤ) ≠ 0, from pow_ne_zero 2 (int.coe_nat_ne_zero_iff_pos.2 hm0.1))).1 $ calc (m : ℤ)^2 * (s^2 + t^2 + u^2 + v^2) = ((m : ℕ) * s)^2 + ((m : ℕ) * t)^2 + ((m : ℕ) * u)^2 + ((m : ℕ) * v)^2 : by { simp [mul_pow], ring } ... = (w^2 + x^2 + y^2 + z^2) * (a^2 + b^2 + c^2 + d^2) : by { simp only [hs.symm, ht.symm, hu.symm, hv.symm], ring } ... = _ : by { rw [hn, habcd, int.nat_abs_of_nonneg hn_nonneg], dsimp [m], ring }, false.elim $ nat.find_min hm hnm ⟨lt_trans hnm hmp, hn0, s, t, u, v, hstuv⟩) lemma sum_four_squares : ∀ n : ℕ, ∃ a b c d : ℕ, a^2 + b^2 + c^2 + d^2 = n | 0 := ⟨0, 0, 0, 0, rfl⟩ | 1 := ⟨1, 0, 0, 0, rfl⟩ | n@(k+2) := have hm : _root_.fact (min_fac (k+2)).prime := ⟨min_fac_prime dec_trivial⟩, have n / min_fac n < n := factors_lemma, let ⟨a, b, c, d, h₁⟩ := show ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = min_fac n, by exactI prime_sum_four_squares (min_fac (k+2)) in let ⟨w, x, y, z, h₂⟩ := sum_four_squares (n / min_fac n) in ⟨(a * w - b * x - c * y - d * z).nat_abs, (a * x + b * w + c * z - d * y).nat_abs, (a * y - b * z + c * w + d * x).nat_abs, (a * z + b * y - c * x + d * w).nat_abs, begin rw [← int.coe_nat_inj', ← nat.mul_div_cancel' (min_fac_dvd (k+2)), int.coe_nat_mul, ← h₁, ← h₂], simp [sum_four_sq_mul_sum_four_sq], end⟩ end nat
a5af56844e273ee3ca2460bacf97577dd3db0e22
f2fbd9ce3f46053c664b74a5294d7d2f584e72d3
/src/for_mathlib/complete_groups.lean
9aeedd5fab858d81d79da0f1c52e303ebb23dd29
[ "Apache-2.0" ]
permissive
jcommelin/lean-perfectoid-spaces
c656ae26a2338ee7a0072dab63baf577f079ca12
d5ed816bcc116fd4cde5ce9aaf03905d00ee391c
refs/heads/master
1,584,610,432,107
1,538,491,594,000
1,538,491,594,000
136,299,168
0
0
null
1,528,274,452,000
1,528,274,452,000
null
UTF-8
Lean
false
false
9,285
lean
import for_mathlib.group_completion namespace add_comm_group variables {α : Type*} {β : Type*} {γ : Type*} [add_comm_group α] [add_comm_group β] [add_comm_group γ] class is_Z_bilin (f : α × β → γ) : Prop := (add_left : ∀ a a' b, f (a + a', b) = f (a, b) + f (a', b)) (add_right : ∀ a b b', f (a, b + b') = f (a, b) + f (a, b')) variables (f : α × β → γ) [is_Z_bilin f] lemma is_Z_bilin.zero_left : ∀ b, f (0, b) = 0 := begin intro b, apply add_self_iff_eq_zero.1, rw ←is_Z_bilin.add_left f, simp end lemma is_Z_bilin.zero_right : ∀ a, f (a, 0) = 0 := begin intro b, apply add_self_iff_eq_zero.1, rw ←is_Z_bilin.add_right f, simp end lemma is_Z_bilin.zero : f (0, 0) = 0 := is_Z_bilin.zero_left f 0 lemma is_Z_bilin.neg_left : ∀ a b, f (-a, b) = -f (a, b) := begin intros a b, apply eq_of_sub_eq_zero, rw [sub_eq_add_neg, neg_neg, ←is_Z_bilin.add_left f, neg_add_self, is_Z_bilin.zero_left f] end lemma is_Z_bilin.neg_right : ∀ a b, f (a, -b) = -f (a, b) := begin intros a b, apply eq_of_sub_eq_zero, rw [sub_eq_add_neg, neg_neg, ←is_Z_bilin.add_right f, neg_add_self, is_Z_bilin.zero_right f] end lemma is_Z_bilin.sub_left : ∀ a a' b, f (a - a', b) = f (a, b) - f (a', b) := begin intros, dsimp [algebra.sub], rw [is_Z_bilin.add_left f, is_Z_bilin.neg_left f] end lemma is_Z_bilin.sub_right : ∀ a b b', f (a, b - b') = f (a, b) - f (a,b') := begin intros, dsimp [algebra.sub], rw [is_Z_bilin.add_right f, is_Z_bilin.neg_right f] end end add_comm_group open add_comm_group filter set function -- E, F and G are abelian topological groups, G is complete Hausdorff variables {E : Type*} [topological_space E] [add_comm_group E] [topological_add_group E] variables {F : Type*} [topological_space F] [add_comm_group F] [topological_add_group F] variables {G : Type*} [topological_space G] [add_comm_group G] [topological_add_group G] [complete_space G] [separated G] -- A is a dense subgroup of E, inclusion is denoted by e variables {A : Type*} [topological_space A] [add_comm_group A] [topological_add_group A] variables {e : A → E} [is_add_group_hom e] (de : dense_embedding e) include de -- B is a dense subgroup of F, inclusion is denoted by f variables {B : Type*} [topological_space B] [add_comm_group B] [topological_add_group B] variables {f : B → F} [is_add_group_hom f] (df : dense_embedding f) include df namespace dense_embedding /-- Bourbaki GT III.6.5 Theorem I: ℤ-bilinear continuous maps from dense sub-groups into a complete Hausdorff group extend by continuity. Note that Bourbaki assumes that E and F are also complete Hausdorff, but this is useless -/ theorem extend_Z_bilin {φ : A × B → G} (hφ : continuous φ) [is_Z_bilin φ] : continuous (extend (dense_embedding.prod de df) φ) := begin sorry /- let dp := dense_embedding.prod de df, let ee := λ u : A × A, (e u.1, e u.2), let ff := λ u : B × B, (f u.1, f u.2), refine dense_embedding.continuous_extend_of_cauchy dp _, rintro ⟨x₀, y₀⟩, split, { apply map_ne_bot, apply vmap_neq_bot, intros U h, rcases exists_mem_of_ne_empty (mem_closure_iff_nhds.1 (dp.dense (x₀, y₀)) U h) with ⟨x, x_in, ⟨z, z_x⟩⟩, existsi z, cc }, { suffices : map (λ (p : (A × B) × (A × B)), φ p.2 - φ p.1) (vmap (λ (p : (A × B) × A × B), ((e p.1.1, f p.1.2), (e p.2.1, f p.2.2))) (filter.prod (nhds (x₀, y₀)) (nhds (x₀, y₀)))) ≤ nhds 0, by rwa [uniformity_eq_vmap_nhds_zero, prod_map_map_eq, ←map_le_iff_le_vmap, filter.map_map, prod_vmap_vmap_eq], intros W' W'_nhd, have key : ∃ U ∈ (vmap e (nhds x₀)).sets, ∃ V ∈ (vmap f (nhds y₀)).sets, ∀ x x' ∈ U, ∀ y y' ∈ V, φ (x', y') - φ (x, y) ∈ W', { let Nx := nhds x₀, let Ny := nhds y₀, have lim_x : tendsto (λ (t : A × A), t.2 - t.1) (vmap ee $ nhds (x₀, x₀)) (nhds 0) := tendsto_sub_vmap_self de x₀, have lim_φ : filter.tendsto φ (nhds (0, 0)) (nhds 0), { have := continuous.tendsto hφ (0, 0), rwa [is_Z_bilin.zero φ] at this }, have lim_y : tendsto (λ (t : B × B), t.2 - t.1) (vmap ff $ nhds (y₀, y₀)) (nhds 0) := tendsto_sub_vmap_self df y₀, have lim_φ_sub_sub : tendsto (λ (p : (A × A) × (B × B)), φ (p.1.2 - p.1.1, p.2.2 - p.2.1)) (filter.prod (vmap ee $ nhds (x₀, x₀)) (vmap ff $ nhds (y₀, y₀))) (nhds 0), { have lim_sub_sub : tendsto (λ (p : (A × A) × B × B), (p.1.2 - p.1.1, p.2.2 - p.2.1)) (filter.prod (vmap ee (nhds (x₀, x₀))) (vmap ff (nhds (y₀, y₀)))) (filter.prod (nhds 0) (nhds 0)), { have := filter.prod_mono lim_x lim_y, rwa prod_map_map_eq at this }, rw ← nhds_prod_eq at lim_sub_sub, exact tendsto.comp lim_sub_sub lim_φ }, rcases quarter_nhd W' W'_nhd with ⟨W, W_nhd, W4⟩, have : ∃ U₁ ∈ (vmap e (nhds x₀)).sets, ∃ V₁ ∈ (vmap f (nhds y₀)).sets, ∀ x x' ∈ U₁, ∀ y y' ∈ V₁, φ (x'-x, y'-y) ∈ W, { have := tendsto_prod_iff.1 lim_φ_sub_sub W W_nhd, repeat { rw [nhds_prod_eq, ←prod_vmap_vmap_eq] at this }, rcases this with ⟨U, U_in, V, V_in, H⟩, rw [mem_prod_same_iff] at U_in V_in, rcases U_in with ⟨U₁, U₁_in, HU₁⟩, rcases V_in with ⟨V₁, V₁_in, HV₁⟩, existsi [U₁, U₁_in, V₁, V₁_in], intros x x' x_in x'_in y y' y_in y'_in, exact H _ _ (HU₁ (mk_mem_prod x_in x'_in)) (HV₁ (mk_mem_prod y_in y'_in)) }, rcases this with ⟨U₁, U₁_nhd, V₁, V₁_nhd, H⟩, have : ∃ x₁, x₁ ∈ U₁ := exists_mem_of_ne_empty (forall_sets_neq_empty_iff_neq_bot.2 de.vmap_nhds_neq_bot U₁ U₁_nhd), rcases this with ⟨x₁, x₁_in⟩, have : ∃ y₁, y₁ ∈ V₁ := exists_mem_of_ne_empty (forall_sets_neq_empty_iff_neq_bot.2 df.vmap_nhds_neq_bot V₁ V₁_nhd), rcases this with ⟨y₁, y₁_in⟩, have : ∃ U₂ ∈ (vmap e (nhds x₀)).sets, ∀ x x' ∈ U₂, φ(x' -x, y₁) ∈ W, { have lim1 : tendsto (λ a : A × A, (a.2 - a.1, y₁)) (filter.prod (vmap e Nx) (vmap e Nx)) (nhds (0, y₁)), { have := tendsto.prod_mk lim_x (tendsto_const_nhds : tendsto (λ (p : A × A), y₁) (vmap ee $ nhds (x₀, x₀)) (nhds y₁)), rw [nhds_prod_eq, prod_vmap_vmap_eq, ←nhds_prod_eq], simpa [-sub_eq_add_neg, sub_self] using this }, have lim_φ : tendsto φ (nhds (0, y₁)) (nhds 0), { have := continuous.tendsto hφ (0, y₁), rwa [is_Z_bilin.zero_left φ] at this }, have lim := tendsto.comp lim1 lim_φ, rw tendsto_prod_self_iff at lim, exact lim W W_nhd }, rcases this with ⟨U₂, U₂_nhd, HU⟩, have : ∃ V₂ ∈ (vmap f (nhds y₀)).sets, ∀ y y' ∈ V₂, φ(x₁, y' - y) ∈ W, { have lim1 : tendsto (λ p : B × B, (x₁, p.2 - p.1)) (filter.prod (vmap f Ny) (vmap f Ny)) (nhds (x₁, 0)), { have := tendsto.prod_mk (tendsto_const_nhds : tendsto (λ (p : B × B), x₁) (vmap ff $ nhds (y₀, y₀)) (nhds x₁)) lim_y, rw [nhds_prod_eq, prod_vmap_vmap_eq, ←nhds_prod_eq], simpa [-sub_eq_add_neg, sub_self] using this }, have lim_φ : tendsto φ (nhds (x₁, 0)) (nhds 0), { have := continuous.tendsto hφ (x₁, 0), rwa [is_Z_bilin.zero_right φ] at this }, have lim := tendsto.comp lim1 lim_φ, rw tendsto_prod_self_iff at lim, exact lim W W_nhd }, rcases this with ⟨V₂, V₂_nhd, HV⟩, existsi [U₁ ∩ U₂, inter_mem_sets U₁_nhd U₂_nhd, V₁ ∩ V₂, inter_mem_sets V₁_nhd V₂_nhd], rintros x x' ⟨xU₁, xU₂⟩ ⟨x'U₁, x'U₂⟩ y y' ⟨yV₁, yV₂⟩ ⟨y'V₁, y'V₂⟩, have key_formula : φ(x', y') - φ(x, y) = φ(x' - x, y₁) + φ(x' - x, y' - y₁) + φ(x₁, y' - y) + φ(x - x₁, y' - y), { repeat { rw is_Z_bilin.sub_left φ }, repeat { rw is_Z_bilin.sub_right φ }, apply eq_of_sub_eq_zero, simp }, rw key_formula, have h₁ := HU x x' xU₂ x'U₂, have h₂ := H x x' xU₁ x'U₁ y₁ y' y₁_in y'V₁, have h₃ := HV y y' yV₂ y'V₂, have h₄ := H x₁ x x₁_in xU₁ y y' yV₁ y'V₁, exact W4 h₁ h₂ h₃ h₄ }, -- end of key part -- Now we only need to massage the key fact rcases key with ⟨U, U_nhd, V, V_nhd, h⟩, rw mem_vmap_sets at U_nhd, rcases U_nhd with ⟨U', U'_nhd, U'_sub⟩, rw mem_vmap_sets at V_nhd, rcases V_nhd with ⟨V', V'_nhd, V'_sub⟩, rw [mem_map, mem_vmap_sets, nhds_prod_eq], existsi set.prod (set.prod U' V') (set.prod U' V'), rw mem_prod_same_iff, simp only [exists_prop], split, { have := prod_mem_prod U'_nhd V'_nhd, tauto }, { intros p h', simp only [set.mem_preimage_eq, set.prod_mk_mem_set_prod_eq] at h', rcases p with ⟨⟨x, y⟩, ⟨x', y'⟩⟩, apply h ; tauto }} -/ end end dense_embedding
7306a8cd01cd696b2bdd039c9c82ae0c22993bcd
680b0d1592ce164979dab866b232f6fa743f2cc8
/library/data/fintype/card.lean
a4a9555a71da59fb6852248b3129aad9bf35db47
[ "Apache-2.0" ]
permissive
syohex/lean
657428ab520f8277fc18cf04bea2ad200dbae782
081ad1212b686780f3ff8a6d0e5f8a1d29a7d8bc
refs/heads/master
1,611,274,838,635
1,452,668,188,000
1,452,668,188,000
49,562,028
0
0
null
1,452,675,604,000
1,452,675,602,000
null
UTF-8
Lean
false
false
2,018
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Haitao Zhang Cardinality for finite types. -/ import .basic data.list data.finset.card open eq.ops nat function list finset namespace fintype definition card [reducible] (A : Type) [finA : fintype A] := finset.card (@finset.univ A _) lemma card_eq_card_image_of_inj {A : Type} [finA : fintype A] [deceqA : decidable_eq A] {B : Type} [finB : fintype B] [deceqB : decidable_eq B] {f : A → B} : injective f → finset.card (image f univ) = card A := assume Pinj, card_image_eq_of_inj_on (to_set_univ⁻¹ ▸ (iff.mp !set.injective_iff_inj_on_univ Pinj)) -- General version of the pigeonhole principle. See also data.less_than. lemma card_le_of_inj (A : Type) [finA : fintype A] [deceqA : decidable_eq A] (B : Type) [finB : fintype B] [deceqB : decidable_eq B] : (∃ f : A → B, injective f) → card A ≤ card B := assume Pex, obtain f Pinj, from Pex, assert Pinj_on_univ : _, from iff.mp !set.injective_iff_inj_on_univ Pinj, assert Pinj_ts : _, from to_set_univ⁻¹ ▸ Pinj_on_univ, assert Psub : (image f univ) ⊆ univ, from !subset_univ, finset.card_le_of_inj_on univ univ (exists.intro f (and.intro Pinj_ts Psub)) -- used to prove that inj ∧ eq card => surj lemma univ_of_card_eq_univ {A : Type} [finA : fintype A] [deceqA : decidable_eq A] {s : finset A} : finset.card s = card A → s = univ := assume Pcardeq, ext (take a, assert D : decidable (a ∈ s), from decidable_mem a s, begin apply iff.intro, intro ain, apply mem_univ, intro ain, cases D with Pin Pnin, exact Pin, assert Pplus1 : finset.card (insert a s) = finset.card s + 1, exact card_insert_of_not_mem Pnin, rewrite Pcardeq at Pplus1, assert Ple : finset.card (insert a s) ≤ card A, apply card_le_card_of_subset, apply subset_univ, rewrite Pplus1 at Ple, exact false.elim (not_succ_le_self Ple) end) end fintype
1df64597b14ec7275981cc0bdcef5fe24c94699b
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/stage0/src/Lean/Util.lean
24177c3f0f338d2cbba6fd48c4d4688ee4e83c16
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
674
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.CollectFVars import Lean.Util.CollectLevelParams import Lean.Util.CollectMVars import Lean.Util.FindMVar import Lean.Util.MonadCache import Lean.Util.PPExt import Lean.Util.PPGoal import Lean.Util.Path import Lean.Util.Profile import Lean.Util.RecDepth import Lean.Util.Sorry import Lean.Util.Trace import Lean.Util.FindExpr import Lean.Util.ReplaceExpr import Lean.Util.ForEachExpr import Lean.Util.ReplaceLevel import Lean.Util.FoldConsts import Lean.Util.Constructions import Lean.Util.SCC
0a125f40ad9f335e8cd4d9e953feddf1c7aa1966
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/interactive/hover.lean
cc42cc1db88669c7af26c40bc1e366ad71c029a4
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
3,969
lean
import Lean example : True := by apply True.intro --^ textDocument/hover example : True := by simp [True.intro] --^ textDocument/hover example (n : Nat) : True := by match n with | Nat.zero => _ --^ textDocument/hover | n + 1 => _ /-- My tactic -/ macro "mytac" o:"only"? e:term : tactic => `(exact $e) example : True := by mytac only True.intro --^ textDocument/hover --^ textDocument/hover --^ textDocument/hover /-- My way better tactic -/ macro_rules | `(tactic| mytac $[only]? $e:term) => --^ textDocument/hover --^ textDocument/hover `(tactic| apply $e:term) --^ textDocument/hover --^ textDocument/hover example : True := by mytac only True.intro --^ textDocument/hover /-- My ultimate tactic -/ elab_rules : tactic | `(tactic| mytac $[only]? $e) => do Lean.Elab.Tactic.evalTactic (← `(tactic| refine $e)) example : True := by mytac only True.intro --^ textDocument/hover /-- My notation -/ macro "mynota" e:term : term => pure e #check mynota 1 --^ textDocument/hover /-- My way better notation -/ macro_rules | `(mynota $e) => `(2 * $e) #check mynota 1 --^ textDocument/hover -- macro_rules take precedence over elab_rules for term/command, so use new syntax syntax "mynota'" term : term /-- My ultimate notation -/ elab_rules : term | `(mynota' $e) => `($e * $e) >>= (Lean.Elab.Term.elabTerm · none) #check mynota' 1 --^ textDocument/hover infix:65 " >+< " => Nat.add --^ textDocument/hover #check 1 >+< 2 --^ textDocument/hover /-- My command -/ macro "mycmd" e:term : command => do let seq ← `(Lean.Parser.Term.doSeq| $e:term) --^ textDocument/hover `(def hi := Id.run do $seq:doSeq) --^ textDocument/hover mycmd 1 --^ textDocument/hover /-- My way better command -/ macro_rules | `(mycmd $e) => `(@[inline] def hi := $e) mycmd 1 --^ textDocument/hover syntax "mycmd'" ppSpace sepBy1(term, " + ") : command --^ textDocument/hover --^ textDocument/hover --^ textDocument/hover /-- My ultimate command -/ elab_rules : command | `(mycmd' $e) => do Lean.Elab.Command.elabCommand (← `(/-- hi -/ @[inline] def hi := $e)) mycmd' 1 --^ textDocument/hover #check ({ a := }) -- should not show `sorry` --^ textDocument/hover example : True := by simp [id True.intro] --^ textDocument/hover --^ textDocument/hover example : Id Nat := do let mut n := 1 n := 2 --^ textDocument/hover n opaque foo : Nat #check _root_.foo --^ textDocument/hover namespace Bar opaque foo : Nat --^ textDocument/hover #check _root_.foo --^ textDocument/hover def bar := 1 --^ textDocument/hover structure Foo := mk :: --^ textDocument/hover --^ textDocument/hover hi : Nat --^ textDocument/hover inductive Bar --^ textDocument/hover | mk : Bar --^ textDocument/hover instance : ToString Nat := ⟨toString⟩ --^ textDocument/hover instance f : ToString Nat := ⟨toString⟩ --^ textDocument/hover example : Type 0 := Nat --^ textDocument/hover def foo.bar : Nat := 1 --^ textDocument/hover --^ textDocument/hover example : Nat → Nat → Nat := fun x y => --^ textDocument/hover --v textDocument/definition x --^ textDocument/hover example : Nat → Nat → Nat := by intro x y --^ textDocument/hover --v textDocument/definition exact x --^ textDocument/hover def g (n : Nat) : Nat := g 0 termination_by g n => n decreasing_by have n' := n; admit --^ textDocument/hover @[inline] --^ textDocument/hover def one := 1 example : True ∧ False := by constructor · constructor --^ textDocument/hover example : Nat := Id.run do (← 1) --^ textDocument/hover
733861165d1c95deef55f8f04348ef99c8ce21a1
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/zorn.lean
ee9041cf52c619c982ff79b7297074c9f8f85887
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
7,161
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Zorn's lemmas. Ported from Isabelle/HOL (written by Jacques D. Fleuriot, Tobias Nipkow, and Christian Sternagel). -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.set.lattice import Mathlib.PostPort universes u u_1 u_2 namespace Mathlib namespace zorn /-- A chain is a subset `c` satisfying `x ≺ y ∨ x = y ∨ y ≺ x` for all `x y ∈ c`. -/ def chain {α : Type u} (r : α → α → Prop) (c : set α) := set.pairwise_on c fun (x y : α) => r x y ∨ r y x theorem chain.total_of_refl {α : Type u} {r : α → α → Prop} [is_refl α r] {c : set α} (H : chain r c) {x : α} {y : α} (hx : x ∈ c) (hy : y ∈ c) : r x y ∨ r y x := dite (x = y) (fun (e : x = y) => Or.inl (e ▸ refl x)) fun (e : ¬x = y) => H x hx y hy e theorem chain.mono {α : Type u} {r : α → α → Prop} {c : set α} {c' : set α} : c' ⊆ c → chain r c → chain r c' := set.pairwise_on.mono theorem chain.directed_on {α : Type u} {r : α → α → Prop} [is_refl α r] {c : set α} (H : chain r c) : directed_on r c := sorry theorem chain_insert {α : Type u} {r : α → α → Prop} {c : set α} {a : α} (hc : chain r c) (ha : ∀ (b : α), b ∈ c → b ≠ a → r a b ∨ r b a) : chain r (insert a c) := sorry /-- `super_chain c₁ c₂` means that `c₂ is a chain that strictly includes `c₁`. -/ def super_chain {α : Type u} {r : α → α → Prop} (c₁ : set α) (c₂ : set α) := chain r c₂ ∧ c₁ ⊂ c₂ /-- A chain `c` is a maximal chain if there does not exists a chain strictly including `c`. -/ def is_max_chain {α : Type u} {r : α → α → Prop} (c : set α) := chain r c ∧ ¬∃ (c' : set α), super_chain c c' /-- Given a set `c`, if there exists a chain `c'` strictly including `c`, then `succ_chain c` is one of these chains. Otherwise it is `c`. -/ def succ_chain {α : Type u} {r : α → α → Prop} (c : set α) : set α := dite (∃ (c' : set α), chain r c ∧ super_chain c c') (fun (h : ∃ (c' : set α), chain r c ∧ super_chain c c') => classical.some h) fun (h : ¬∃ (c' : set α), chain r c ∧ super_chain c c') => c theorem succ_spec {α : Type u} {r : α → α → Prop} {c : set α} (h : ∃ (c' : set α), chain r c ∧ super_chain c c') : super_chain c (succ_chain c) := sorry theorem chain_succ {α : Type u} {r : α → α → Prop} {c : set α} (hc : chain r c) : chain r (succ_chain c) := sorry theorem super_of_not_max {α : Type u} {r : α → α → Prop} {c : set α} (hc₁ : chain r c) (hc₂ : ¬is_max_chain c) : super_chain c (succ_chain c) := sorry theorem succ_increasing {α : Type u} {r : α → α → Prop} {c : set α} : c ⊆ succ_chain c := sorry /-- Set of sets reachable from `∅` using `succ_chain` and `⋃₀`. -/ inductive chain_closure {α : Type u} {r : α → α → Prop} : set α → Prop where | succ : ∀ {s : set α}, chain_closure s → chain_closure (succ_chain s) | union : ∀ {s : set (set α)}, (∀ (a : set α), a ∈ s → chain_closure a) → chain_closure (⋃₀s) theorem chain_closure_empty {α : Type u} {r : α → α → Prop} : chain_closure ∅ := sorry theorem chain_closure_closure {α : Type u} {r : α → α → Prop} : chain_closure (⋃₀chain_closure) := chain_closure.union fun (s : set α) (hs : s ∈ chain_closure) => hs theorem chain_closure_total {α : Type u} {r : α → α → Prop} {c₁ : set α} {c₂ : set α} (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) : c₁ ⊆ c₂ ∨ c₂ ⊆ c₁ := (fun (this : c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁) => or.imp_right (fun (this : succ_chain c₂ ⊆ c₁) => set.subset.trans succ_increasing this) this) (chain_closure_succ_total_aux hc₁ hc₂ fun (c₃ : set α) (hc₃ : chain_closure c₃) => chain_closure_succ_total hc₃ hc₂) theorem chain_closure_succ_fixpoint {α : Type u} {r : α → α → Prop} {c₁ : set α} {c₂ : set α} (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h_eq : succ_chain c₂ = c₂) : c₁ ⊆ c₂ := sorry theorem chain_closure_succ_fixpoint_iff {α : Type u} {r : α → α → Prop} {c : set α} (hc : chain_closure c) : succ_chain c = c ↔ c = ⋃₀chain_closure := sorry theorem chain_chain_closure {α : Type u} {r : α → α → Prop} {c : set α} (hc : chain_closure c) : chain r c := sorry /-- `max_chain` is the union of all sets in the chain closure. -/ def max_chain {α : Type u} {r : α → α → Prop} : set α := ⋃₀chain_closure /-- Hausdorff's maximality principle There exists a maximal totally ordered subset of `α`. Note that we do not require `α` to be partially ordered by `r`. -/ theorem max_chain_spec {α : Type u} {r : α → α → Prop} : is_max_chain max_chain := sorry /-- Zorn's lemma If every chain has an upper bound, then there is a maximal element -/ theorem exists_maximal_of_chains_bounded {α : Type u} {r : α → α → Prop} (h : ∀ (c : set α), chain r c → ∃ (ub : α), ∀ (a : α), a ∈ c → r a ub) (trans : ∀ {a b c : α}, r a b → r b c → r a c) : ∃ (m : α), ∀ (a : α), r m a → r a m := sorry theorem zorn_partial_order {α : Type u} [partial_order α] (h : ∀ (c : set α), chain LessEq c → ∃ (ub : α), ∀ (a : α), a ∈ c → a ≤ ub) : ∃ (m : α), ∀ (a : α), m ≤ a → a = m := sorry theorem zorn_partial_order₀ {α : Type u} [partial_order α] (s : set α) (ih : ∀ (c : set α) (H : c ⊆ s), chain LessEq c → ∀ (y : α) (H : y ∈ c), ∃ (ub : α), ∃ (H : ub ∈ s), ∀ (z : α), z ∈ c → z ≤ ub) (x : α) (hxs : x ∈ s) : ∃ (m : α), ∃ (H : m ∈ s), x ≤ m ∧ ∀ (z : α), z ∈ s → m ≤ z → z = m := sorry theorem zorn_subset {α : Type u} (S : set (set α)) (h : ∀ (c : set (set α)) (H : c ⊆ S), chain has_subset.subset c → ∃ (ub : set α), ∃ (H : ub ∈ S), ∀ (s : set α), s ∈ c → s ⊆ ub) : ∃ (m : set α), ∃ (H : m ∈ S), ∀ (a : set α), a ∈ S → m ⊆ a → a = m := sorry theorem zorn_subset₀ {α : Type u} (S : set (set α)) (H : ∀ (c : set (set α)) (H : c ⊆ S), chain has_subset.subset c → set.nonempty c → ∃ (ub : set α), ∃ (H : ub ∈ S), ∀ (s : set α), s ∈ c → s ⊆ ub) (x : set α) (hx : x ∈ S) : ∃ (m : set α), ∃ (H : m ∈ S), x ⊆ m ∧ ∀ (a : set α), a ∈ S → m ⊆ a → a = m := sorry theorem chain.total {α : Type u} [preorder α] {c : set α} (H : chain LessEq c) {x : α} {y : α} : x ∈ c → y ∈ c → x ≤ y ∨ y ≤ x := chain.total_of_refl H theorem chain.image {α : Type u_1} {β : Type u_2} (r : α → α → Prop) (s : β → β → Prop) (f : α → β) (h : ∀ (x y : α), r x y → s (f x) (f y)) {c : set α} (hrc : chain r c) : chain s (f '' c) := sorry end zorn theorem directed_of_chain {α : Type u_1} {β : Type u_2} {r : β → β → Prop} [is_refl β r] {f : α → β} {c : set α} (h : zorn.chain (f ⁻¹'o r) c) : directed r fun (x : Subtype fun (a : α) => a ∈ c) => f ↑x := sorry
d13483b28b21166a4f83352cb0ef24e2644ce2a6
47c6e122a73eb61594f0426d74d9c1897ac5668e
/src/test.lean
7fcd09635184a5f823be2b0c81bfda651b149c10
[]
no_license
ineswright/Lean-Sylow
19ec5d964be8a66e25e227621fbac79c88a83ddf
74b99544ab1ca96dc28fbe152125f565763a893b
refs/heads/main
1,688,108,643,799
1,627,664,383,000
1,627,664,383,000
382,315,172
1
0
null
null
null
null
UTF-8
Lean
false
false
1,235
lean
import data.nat.prime import number_theory.padics.padic_norm import data.pnat.factors -- ERIC'S CODE @[simp] lemma prime_multiset.coe_add (m n : prime_multiset) : ↑(m + n) = (m + n : multiset ℕ) := is_add_monoid_hom.map_add _ _ _ attribute [simp] pnat.coe_nat_factor_multiset lemma factors_mul {p q : ℕ} (hp : 0 < p) (hq : 0 < q) : (p * q).factors ~ p.factors ++ q.factors := begin rw ←multiset.coe_eq_coe, have := pnat.factor_multiset_mul ⟨p, hp⟩ ⟨q, hq⟩, rw ←prime_multiset.coe_nat_injective.eq_iff at this, simpa using this, end --MY CODE lemma factors_prime_pow_eq_repeat_prime_pow (p y : ℕ) (hprime : p.prime) : list.repeat p y = (p ^ y).factors := begin haveI : fact p.prime := ⟨hprime⟩, induction y, { simp [pow_zero, nat.factors_one] }, { apply list.repeat_perm.1 (list.perm.trans _ (factors_mul (nat.prime.pos hprime) (pow_pos (nat.prime.pos hprime) y_n)).symm), simp [nat.factors_prime hprime, y_ih] } end lemma padic_val_nat.prime_pow_eq_pow {p y : ℕ} (hp : p.prime) : y = padic_val_nat p (p ^ y) := begin haveI : fact p.prime := ⟨hp⟩, rw [padic_val_nat_eq_factors_count p, ← @factors_prime_pow_eq_repeat_prime_pow p y hp, ← (list.count_repeat p y).symm], end
699fafffdc387a4020108bb2c59f28ce77e97527
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/polynomial/big_operators.lean
9f48a03f1f5af5d7e6b9692542263f5870543549
[ "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
13,231
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jalex Stark -/ import algebra.order.with_zero import data.polynomial.monic /-! # Lemmas for the interaction between polynomials and `∑` and `∏`. Recall that `∑` and `∏` are notation for `finset.sum` and `finset.prod` respectively. ## Main results - `polynomial.nat_degree_prod_of_monic` : the degree of a product of monic polynomials is the product of degrees. We prove this only for `[comm_semiring R]`, but it ought to be true for `[semiring R]` and `list.prod`. - `polynomial.nat_degree_prod` : for polynomials over an integral domain, the degree of the product is the sum of degrees. - `polynomial.leading_coeff_prod` : for polynomials over an integral domain, the leading coefficient is the product of leading coefficients. - `polynomial.prod_X_sub_C_coeff_card_pred` carries most of the content for computing the second coefficient of the characteristic polynomial. -/ open finset open multiset open_locale big_operators polynomial universes u w variables {R : Type u} {ι : Type w} namespace polynomial variables (s : finset ι) section semiring variables {S : Type*} [semiring S] lemma nat_degree_list_sum_le (l : list S[X]) : nat_degree l.sum ≤ (l.map nat_degree).foldr max 0 := list.sum_le_foldr_max nat_degree (by simp) nat_degree_add_le _ lemma nat_degree_multiset_sum_le (l : multiset S[X]) : nat_degree l.sum ≤ (l.map nat_degree).foldr max max_left_comm 0 := quotient.induction_on l (by simpa using nat_degree_list_sum_le) lemma nat_degree_sum_le (f : ι → S[X]) : nat_degree (∑ i in s, f i) ≤ s.fold max 0 (nat_degree ∘ f) := by simpa using nat_degree_multiset_sum_le (s.val.map f) lemma degree_list_sum_le (l : list S[X]) : degree l.sum ≤ (l.map nat_degree).maximum := begin by_cases h : l.sum = 0, { simp [h] }, { rw degree_eq_nat_degree h, suffices : (l.map nat_degree).maximum = ((l.map nat_degree).foldr max 0 : ℕ), { rw this, simpa [this] using nat_degree_list_sum_le l }, rw list.maximum_eq_coe_foldr_max_of_ne_nil, { congr }, contrapose! h, rw [list.map_eq_nil] at h, simp [h] } end lemma nat_degree_list_prod_le (l : list S[X]) : nat_degree l.prod ≤ (l.map nat_degree).sum := begin induction l with hd tl IH, { simp }, { simpa using nat_degree_mul_le.trans (add_le_add_left IH _) } end lemma degree_list_prod_le (l : list S[X]) : degree l.prod ≤ (l.map degree).sum := begin induction l with hd tl IH, { simp }, { simpa using (degree_mul_le _ _).trans (add_le_add_left IH _) } end lemma coeff_list_prod_of_nat_degree_le (l : list S[X]) (n : ℕ) (hl : ∀ p ∈ l, nat_degree p ≤ n) : coeff (list.prod l) (l.length * n) = (l.map (λ p, coeff p n)).prod := begin induction l with hd tl IH, { simp }, { have hl' : ∀ (p ∈ tl), nat_degree p ≤ n := λ p hp, hl p (list.mem_cons_of_mem _ hp), simp only [list.prod_cons, list.map, list.length], rw [add_mul, one_mul, add_comm, ←IH hl', mul_comm tl.length], have h : nat_degree tl.prod ≤ n * tl.length, { refine (nat_degree_list_prod_le _).trans _, rw [←tl.length_map nat_degree, mul_comm], refine list.sum_le_card_nsmul _ _ _, simpa using hl' }, have hdn : nat_degree hd ≤ n := hl _ (list.mem_cons_self _ _), rcases hdn.eq_or_lt with rfl|hdn', { cases h.eq_or_lt with h' h', { rw [←h', coeff_mul_degree_add_degree, leading_coeff, leading_coeff] }, { rw [coeff_eq_zero_of_nat_degree_lt, coeff_eq_zero_of_nat_degree_lt h', mul_zero], exact nat_degree_mul_le.trans_lt (add_lt_add_left h' _) } }, { rw [coeff_eq_zero_of_nat_degree_lt hdn', coeff_eq_zero_of_nat_degree_lt, zero_mul], exact nat_degree_mul_le.trans_lt (add_lt_add_of_lt_of_le hdn' h) } } end end semiring section comm_semiring variables [comm_semiring R] (f : ι → R[X]) (t : multiset R[X]) lemma nat_degree_multiset_prod_le : t.prod.nat_degree ≤ (t.map nat_degree).sum := quotient.induction_on t (by simpa using nat_degree_list_prod_le) lemma nat_degree_prod_le : (∏ i in s, f i).nat_degree ≤ ∑ i in s, (f i).nat_degree := by simpa using nat_degree_multiset_prod_le (s.1.map f) /-- The degree of a product of polynomials is at most the sum of the degrees, where the degree of the zero polynomial is ⊥. -/ lemma degree_multiset_prod_le : t.prod.degree ≤ (t.map polynomial.degree).sum := quotient.induction_on t (by simpa using degree_list_prod_le) lemma degree_prod_le : (∏ i in s, f i).degree ≤ ∑ i in s, (f i).degree := by simpa only [multiset.map_map] using degree_multiset_prod_le (s.1.map f) /-- The leading coefficient of a product of polynomials is equal to the product of the leading coefficients, provided that this product is nonzero. See `polynomial.leading_coeff_multiset_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ lemma leading_coeff_multiset_prod' (h : (t.map leading_coeff).prod ≠ 0) : t.prod.leading_coeff = (t.map leading_coeff).prod := begin induction t using multiset.induction_on with a t ih, { simp }, simp only [map_cons, multiset.prod_cons] at h ⊢, rw polynomial.leading_coeff_mul'; { rwa ih, apply right_ne_zero_of_mul h } end /-- The leading coefficient of a product of polynomials is equal to the product of the leading coefficients, provided that this product is nonzero. See `polynomial.leading_coeff_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ lemma leading_coeff_prod' (h : ∏ i in s, (f i).leading_coeff ≠ 0) : (∏ i in s, f i).leading_coeff = ∏ i in s, (f i).leading_coeff := by simpa using leading_coeff_multiset_prod' (s.1.map f) (by simpa using h) /-- The degree of a product of polynomials is equal to the sum of the degrees, provided that the product of leading coefficients is nonzero. See `polynomial.nat_degree_multiset_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ lemma nat_degree_multiset_prod' (h : (t.map (λ f, leading_coeff f)).prod ≠ 0) : t.prod.nat_degree = (t.map (λ f, nat_degree f)).sum := begin revert h, refine multiset.induction_on t _ (λ a t ih ht, _), { simp }, rw [map_cons, multiset.prod_cons] at ht ⊢, rw [multiset.sum_cons, polynomial.nat_degree_mul', ih], { apply right_ne_zero_of_mul ht }, { rwa polynomial.leading_coeff_multiset_prod', apply right_ne_zero_of_mul ht }, end /-- The degree of a product of polynomials is equal to the sum of the degrees, provided that the product of leading coefficients is nonzero. See `polynomial.nat_degree_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ lemma nat_degree_prod' (h : ∏ i in s, (f i).leading_coeff ≠ 0) : (∏ i in s, f i).nat_degree = ∑ i in s, (f i).nat_degree := by simpa using nat_degree_multiset_prod' (s.1.map f) (by simpa using h) lemma nat_degree_multiset_prod_of_monic (h : ∀ f ∈ t, monic f) : t.prod.nat_degree = (t.map nat_degree).sum := begin nontriviality R, apply nat_degree_multiset_prod', suffices : (t.map (λ f, leading_coeff f)).prod = 1, { rw this, simp }, convert prod_repeat (1 : R) t.card, { simp only [eq_repeat, multiset.card_map, eq_self_iff_true, true_and], rintros i hi, obtain ⟨i, hi, rfl⟩ := multiset.mem_map.mp hi, apply h, assumption }, { simp } end lemma nat_degree_prod_of_monic (h : ∀ i ∈ s, (f i).monic) : (∏ i in s, f i).nat_degree = ∑ i in s, (f i).nat_degree := by simpa using nat_degree_multiset_prod_of_monic (s.1.map f) (by simpa using h) lemma coeff_multiset_prod_of_nat_degree_le (n : ℕ) (hl : ∀ p ∈ t, nat_degree p ≤ n) : coeff t.prod (t.card * n) = (t.map (λ p, coeff p n)).prod := begin induction t using quotient.induction_on, simpa using coeff_list_prod_of_nat_degree_le _ _ hl end lemma coeff_prod_of_nat_degree_le (f : ι → R[X]) (n : ℕ) (h : ∀ p ∈ s, nat_degree (f p) ≤ n) : coeff (∏ i in s, f i) (s.card * n) = ∏ i in s, coeff (f i) n := begin cases s with l hl, convert coeff_multiset_prod_of_nat_degree_le (l.map f) _ _, { simp }, { simp }, { simpa using h } end lemma coeff_zero_multiset_prod : t.prod.coeff 0 = (t.map (λ f, coeff f 0)).prod := begin refine multiset.induction_on t _ (λ a t ht, _), { simp }, rw [multiset.prod_cons, map_cons, multiset.prod_cons, polynomial.mul_coeff_zero, ht] end lemma coeff_zero_prod : (∏ i in s, f i).coeff 0 = ∏ i in s, (f i).coeff 0 := by simpa using coeff_zero_multiset_prod (s.1.map f) end comm_semiring section comm_ring variables [comm_ring R] open monic -- Eventually this can be generalized with Vieta's formulas -- plus the connection between roots and factorization. lemma multiset_prod_X_sub_C_next_coeff (t : multiset R) : next_coeff (t.map (λ x, X - C x)).prod = -t.sum := begin rw next_coeff_multiset_prod, { simp only [next_coeff_X_sub_C], exact t.sum_hom (-add_monoid_hom.id R) }, { intros, apply monic_X_sub_C } end lemma prod_X_sub_C_next_coeff {s : finset ι} (f : ι → R) : next_coeff ∏ i in s, (X - C (f i)) = -∑ i in s, f i := by simpa using multiset_prod_X_sub_C_next_coeff (s.1.map f) lemma multiset_prod_X_sub_C_coeff_card_pred (t : multiset R) (ht : 0 < t.card) : (t.map (λ x, (X - C x))).prod.coeff (t.card - 1) = -t.sum := begin nontriviality R, convert multiset_prod_X_sub_C_next_coeff (by assumption), rw next_coeff, split_ifs, { rw nat_degree_multiset_prod_of_monic at h; simp only [multiset.mem_map] at *, swap, { rintros _ ⟨_, _, rfl⟩, apply monic_X_sub_C }, simp_rw [multiset.sum_eq_zero_iff, multiset.mem_map] at h, contrapose! h, obtain ⟨x, hx⟩ := card_pos_iff_exists_mem.mp ht, exact ⟨_, ⟨_, ⟨x, hx, rfl⟩, nat_degree_X_sub_C _⟩, one_ne_zero⟩ }, congr, rw nat_degree_multiset_prod_of_monic; { simp [nat_degree_X_sub_C, monic_X_sub_C] }, end lemma prod_X_sub_C_coeff_card_pred (s : finset ι) (f : ι → R) (hs : 0 < s.card) : (∏ i in s, (X - C (f i))).coeff (s.card - 1) = - ∑ i in s, f i := by simpa using multiset_prod_X_sub_C_coeff_card_pred (s.1.map f) (by simpa using hs) end comm_ring section no_zero_divisors section semiring variables [semiring R] [no_zero_divisors R] /-- The degree of a product of polynomials is equal to the sum of the degrees, where the degree of the zero polynomial is ⊥. `[nontrivial R]` is needed, otherwise for `l = []` we have `⊥` in the LHS and `0` in the RHS. -/ lemma degree_list_prod [nontrivial R] (l : list R[X]) : l.prod.degree = (l.map degree).sum := map_list_prod (@degree_monoid_hom R _ _ _) l end semiring section comm_semiring variables [comm_semiring R] [no_zero_divisors R] (f : ι → R[X]) (t : multiset R[X]) /-- The degree of a product of polynomials is equal to the sum of the degrees. See `polynomial.nat_degree_prod'` (with a `'`) for a version for commutative semirings, where additionally, the product of the leading coefficients must be nonzero. -/ lemma nat_degree_prod (h : ∀ i ∈ s, f i ≠ 0) : (∏ i in s, f i).nat_degree = ∑ i in s, (f i).nat_degree := begin nontriviality R, apply nat_degree_prod', rw prod_ne_zero_iff, intros x hx, simp [h x hx] end lemma nat_degree_multiset_prod (h : (0 : R[X]) ∉ t) : nat_degree t.prod = (t.map nat_degree).sum := begin nontriviality R, rw nat_degree_multiset_prod', simp_rw [ne.def, multiset.prod_eq_zero_iff, multiset.mem_map, leading_coeff_eq_zero], rintro ⟨_, h, rfl⟩, contradiction end /-- The degree of a product of polynomials is equal to the sum of the degrees, where the degree of the zero polynomial is ⊥. -/ lemma degree_multiset_prod [nontrivial R] : t.prod.degree = (t.map (λ f, degree f)).sum := map_multiset_prod (@degree_monoid_hom R _ _ _) _ /-- The degree of a product of polynomials is equal to the sum of the degrees, where the degree of the zero polynomial is ⊥. -/ lemma degree_prod [nontrivial R] : (∏ i in s, f i).degree = ∑ i in s, (f i).degree := map_prod (@degree_monoid_hom R _ _ _) _ _ /-- The leading coefficient of a product of polynomials is equal to the product of the leading coefficients. See `polynomial.leading_coeff_multiset_prod'` (with a `'`) for a version for commutative semirings, where additionally, the product of the leading coefficients must be nonzero. -/ lemma leading_coeff_multiset_prod : t.prod.leading_coeff = (t.map (λ f, leading_coeff f)).prod := by { rw [← leading_coeff_hom_apply, monoid_hom.map_multiset_prod], refl } /-- The leading coefficient of a product of polynomials is equal to the product of the leading coefficients. See `polynomial.leading_coeff_prod'` (with a `'`) for a version for commutative semirings, where additionally, the product of the leading coefficients must be nonzero. -/ lemma leading_coeff_prod : (∏ i in s, f i).leading_coeff = ∏ i in s, (f i).leading_coeff := by simpa using leading_coeff_multiset_prod (s.1.map f) end comm_semiring end no_zero_divisors end polynomial
fb9ac8a3612dc5af5fee06d5bd24dad1579bb0fb
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/calculus/fderiv_analytic.lean
bddec64dbb2bc7eda5cbdd39605d791a5e15d378
[ "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
7,198
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.analytic.basic import analysis.calculus.deriv import analysis.calculus.cont_diff_def /-! # Frechet derivatives of analytic functions. A function expressible as a power series at a point has a Frechet derivative there. Also the special case in terms of `deriv` when the domain is 1-dimensional. -/ open filter asymptotics open_locale ennreal variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] variables {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] section fderiv variables {p : formal_multilinear_series 𝕜 E F} {r : ℝ≥0∞} variables {f : E → F} {x : E} {s : set E} lemma has_fpower_series_at.has_strict_fderiv_at (h : has_fpower_series_at f p x) : has_strict_fderiv_at f (continuous_multilinear_curry_fin1 𝕜 E F (p 1)) x := begin refine h.is_O_image_sub_norm_mul_norm_sub.trans_is_o (is_o.of_norm_right _), refine is_o_iff_exists_eq_mul.2 ⟨λ y, ‖y - (x, x)‖, _, eventually_eq.rfl⟩, refine (continuous_id.sub continuous_const).norm.tendsto' _ _ _, rw [_root_.id, sub_self, norm_zero] end lemma has_fpower_series_at.has_fderiv_at (h : has_fpower_series_at f p x) : has_fderiv_at f (continuous_multilinear_curry_fin1 𝕜 E F (p 1)) x := h.has_strict_fderiv_at.has_fderiv_at lemma has_fpower_series_at.differentiable_at (h : has_fpower_series_at f p x) : differentiable_at 𝕜 f x := h.has_fderiv_at.differentiable_at lemma analytic_at.differentiable_at : analytic_at 𝕜 f x → differentiable_at 𝕜 f x | ⟨p, hp⟩ := hp.differentiable_at lemma analytic_at.differentiable_within_at (h : analytic_at 𝕜 f x) : differentiable_within_at 𝕜 f s x := h.differentiable_at.differentiable_within_at lemma has_fpower_series_at.fderiv_eq (h : has_fpower_series_at f p x) : fderiv 𝕜 f x = continuous_multilinear_curry_fin1 𝕜 E F (p 1) := h.has_fderiv_at.fderiv lemma has_fpower_series_on_ball.differentiable_on [complete_space F] (h : has_fpower_series_on_ball f p x r) : differentiable_on 𝕜 f (emetric.ball x r) := λ y hy, (h.analytic_at_of_mem hy).differentiable_within_at lemma analytic_on.differentiable_on (h : analytic_on 𝕜 f s) : differentiable_on 𝕜 f s := λ y hy, (h y hy).differentiable_within_at lemma has_fpower_series_on_ball.has_fderiv_at [complete_space F] (h : has_fpower_series_on_ball f p x r) {y : E} (hy : (‖y‖₊ : ℝ≥0∞) < r) : has_fderiv_at f (continuous_multilinear_curry_fin1 𝕜 E F (p.change_origin y 1)) (x + y) := (h.change_origin hy).has_fpower_series_at.has_fderiv_at lemma has_fpower_series_on_ball.fderiv_eq [complete_space F] (h : has_fpower_series_on_ball f p x r) {y : E} (hy : (‖y‖₊ : ℝ≥0∞) < r) : fderiv 𝕜 f (x + y) = continuous_multilinear_curry_fin1 𝕜 E F (p.change_origin y 1) := (h.has_fderiv_at hy).fderiv /-- If a function has a power series on a ball, then so does its derivative. -/ lemma has_fpower_series_on_ball.fderiv [complete_space F] (h : has_fpower_series_on_ball f p x r) : has_fpower_series_on_ball (fderiv 𝕜 f) ((continuous_multilinear_curry_fin1 𝕜 E F : (E [×1]→L[𝕜] F) →L[𝕜] (E →L[𝕜] F)) .comp_formal_multilinear_series (p.change_origin_series 1)) x r := begin suffices A : has_fpower_series_on_ball (λ z, continuous_multilinear_curry_fin1 𝕜 E F (p.change_origin (z - x) 1)) ((continuous_multilinear_curry_fin1 𝕜 E F : (E [×1]→L[𝕜] F) →L[𝕜] (E →L[𝕜] F)) .comp_formal_multilinear_series (p.change_origin_series 1)) x r, { apply A.congr, assume z hz, dsimp, rw [← h.fderiv_eq, add_sub_cancel'_right], simpa only [edist_eq_coe_nnnorm_sub, emetric.mem_ball] using hz}, suffices B : has_fpower_series_on_ball (λ z, p.change_origin (z - x) 1) (p.change_origin_series 1) x r, from (continuous_multilinear_curry_fin1 𝕜 E F).to_continuous_linear_equiv .to_continuous_linear_map.comp_has_fpower_series_on_ball B, simpa using ((p.has_fpower_series_on_ball_change_origin 1 (h.r_pos.trans_le h.r_le)).mono h.r_pos h.r_le).comp_sub x, end /-- If a function is analytic on a set `s`, so is its Fréchet derivative. -/ lemma analytic_on.fderiv [complete_space F] (h : analytic_on 𝕜 f s) : analytic_on 𝕜 (fderiv 𝕜 f) s := begin assume y hy, rcases h y hy with ⟨p, r, hp⟩, exact hp.fderiv.analytic_at, end /-- If a function is analytic on a set `s`, so are its successive Fréchet derivative. -/ lemma analytic_on.iterated_fderiv [complete_space F] (h : analytic_on 𝕜 f s) (n : ℕ) : analytic_on 𝕜 (iterated_fderiv 𝕜 n f) s := begin induction n with n IH, { rw iterated_fderiv_zero_eq_comp, exact ((continuous_multilinear_curry_fin0 𝕜 E F).symm : F →L[𝕜] (E [×0]→L[𝕜] F)) .comp_analytic_on h }, { rw iterated_fderiv_succ_eq_comp_left, apply (continuous_multilinear_curry_left_equiv 𝕜 (λ (i : fin (n + 1)), E) F) .to_continuous_linear_equiv.to_continuous_linear_map.comp_analytic_on, exact IH.fderiv } end /-- An analytic function is infinitely differentiable. -/ lemma analytic_on.cont_diff_on [complete_space F] (h : analytic_on 𝕜 f s) {n : ℕ∞} : cont_diff_on 𝕜 n f s := begin let t := {x | analytic_at 𝕜 f x}, suffices : cont_diff_on 𝕜 n f t, from this.mono h, have H : analytic_on 𝕜 f t := λ x hx, hx, have t_open : is_open t := is_open_analytic_at 𝕜 f, apply cont_diff_on_of_continuous_on_differentiable_on, { assume m hm, apply (H.iterated_fderiv m).continuous_on.congr, assume x hx, exact iterated_fderiv_within_of_is_open _ t_open hx }, { assume m hm, apply (H.iterated_fderiv m).differentiable_on.congr, assume x hx, exact iterated_fderiv_within_of_is_open _ t_open hx } end end fderiv section deriv variables {p : formal_multilinear_series 𝕜 𝕜 F} {r : ℝ≥0∞} variables {f : 𝕜 → F} {x : 𝕜} {s : set 𝕜} protected lemma has_fpower_series_at.has_strict_deriv_at (h : has_fpower_series_at f p x) : has_strict_deriv_at f (p 1 (λ _, 1)) x := h.has_strict_fderiv_at.has_strict_deriv_at protected lemma has_fpower_series_at.has_deriv_at (h : has_fpower_series_at f p x) : has_deriv_at f (p 1 (λ _, 1)) x := h.has_strict_deriv_at.has_deriv_at protected lemma has_fpower_series_at.deriv (h : has_fpower_series_at f p x) : deriv f x = p 1 (λ _, 1) := h.has_deriv_at.deriv /-- If a function is analytic on a set `s`, so is its derivative. -/ lemma analytic_on.deriv [complete_space F] (h : analytic_on 𝕜 f s) : analytic_on 𝕜 (deriv f) s := (continuous_linear_map.apply 𝕜 F (1 : 𝕜)).comp_analytic_on h.fderiv /-- If a function is analytic on a set `s`, so are its successive derivatives. -/ lemma analytic_on.iterated_deriv [complete_space F] (h : analytic_on 𝕜 f s) (n : ℕ) : analytic_on 𝕜 (deriv^[n] f) s := begin induction n with n IH, { exact h }, { simpa only [function.iterate_succ', function.comp_app] using IH.deriv } end end deriv
2ee4b6c897ad86a01e5c34cf5408cbfc55cbc862
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/topology/metric_space/completion.lean
1c3af9bda6bd8ef57c1120bec912c45a14c8e615
[ "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,683
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.uniform_space.completion import topology.metric_space.isometry /-! # The completion of a metric space Completion of uniform spaces are already defined in `topology.uniform_space.completion`. We show here that the uniform space completion of a metric space inherits a metric space structure, by extending the distance to the completion and checking that it is indeed a distance, and that it defines the same uniformity as the already defined uniform structure on the completion -/ open set filter uniform_space uniform_space.completion open_locale filter noncomputable theory universes u variables {α : Type u} [metric_space α] namespace metric /-- The distance on the completion is obtained by extending the distance on the original space, by uniform continuity. -/ instance : has_dist (completion α) := ⟨completion.extension₂ dist⟩ /-- The new distance is uniformly continuous. -/ protected lemma completion.uniform_continuous_dist : uniform_continuous (λp:completion α × completion α, dist p.1 p.2) := uniform_continuous_extension₂ dist /-- The new distance is an extension of the original distance. -/ protected lemma completion.dist_eq (x y : α) : dist (x : completion α) y = dist x y := completion.extension₂_coe_coe uniform_continuous_dist _ _ /- Let us check that the new distance satisfies the axioms of a distance, by starting from the properties on α and extending them to `completion α` by continuity. -/ protected lemma completion.dist_self (x : completion α) : dist x x = 0 := begin apply induction_on x, { refine is_closed_eq _ continuous_const, exact (completion.uniform_continuous_dist.continuous.comp (continuous.prod_mk continuous_id continuous_id) : _) }, { assume a, rw [completion.dist_eq, dist_self] } end protected lemma completion.dist_comm (x y : completion α) : dist x y = dist y x := begin apply induction_on₂ x y, { refine is_closed_eq completion.uniform_continuous_dist.continuous _, exact (completion.uniform_continuous_dist.continuous.comp continuous_swap : _) }, { assume a b, rw [completion.dist_eq, completion.dist_eq, dist_comm] } end protected lemma completion.dist_triangle (x y z : completion α) : dist x z ≤ dist x y + dist y z := begin apply induction_on₃ x y z, { refine is_closed_le _ (continuous.add _ _), { have : continuous (λp : completion α × completion α × completion α, (p.1, p.2.2)) := continuous.prod_mk continuous_fst (continuous.comp continuous_snd continuous_snd), exact (completion.uniform_continuous_dist.continuous.comp this : _) }, { have : continuous (λp : completion α × completion α × completion α, (p.1, p.2.1)) := continuous.prod_mk continuous_fst (continuous_fst.comp continuous_snd), exact (completion.uniform_continuous_dist.continuous.comp this : _) }, { have : continuous (λp : completion α × completion α × completion α, (p.2.1, p.2.2)) := continuous.prod_mk (continuous_fst.comp continuous_snd) (continuous.comp continuous_snd continuous_snd), exact (continuous.comp completion.uniform_continuous_dist.continuous this : _) } }, { assume a b c, rw [completion.dist_eq, completion.dist_eq, completion.dist_eq], exact dist_triangle a b c } end /-- Elements of the uniformity (defined generally for completions) can be characterized in terms of the distance. -/ @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma completion.mem_uniformity_dist (s : set (completion α × completion α)) : s ∈ uniformity (completion α) ↔ (∃ε>0, ∀{a b}, dist a b < ε → (a, b) ∈ s) := begin split, { /- Start from an entourage `s`. It contains a closed entourage `t`. Its pullback in α is an entourage, so it contains an ε-neighborhood of the diagonal by definition of the entourages in metric spaces. Then `t` contains an ε-neighborhood of the diagonal in `completion α`, as closed properties pass to the completion. -/ assume hs, rcases mem_uniformity_is_closed hs with ⟨t, ht, ⟨tclosed, ts⟩⟩, have A : {x : α × α | (coe (x.1), coe (x.2)) ∈ t} ∈ uniformity α := uniform_continuous_def.1 (uniform_continuous_coe α) t ht, rcases mem_uniformity_dist.1 A with ⟨ε, εpos, hε⟩, refine ⟨ε, εpos, λx y hxy, _⟩, have : ε ≤ dist x y ∨ (x, y) ∈ t, { apply induction_on₂ x y, { have : {x : completion α × completion α | ε ≤ dist (x.fst) (x.snd) ∨ (x.fst, x.snd) ∈ t} = {p : completion α × completion α | ε ≤ dist p.1 p.2} ∪ t, by ext; simp, rw this, apply is_closed_union _ tclosed, exact is_closed_le continuous_const completion.uniform_continuous_dist.continuous }, { assume x y, rw completion.dist_eq, by_cases h : ε ≤ dist x y, { exact or.inl h }, { have Z := hε (not_le.1 h), simp only [set.mem_set_of_eq] at Z, exact or.inr Z }}}, simp only [not_le.mpr hxy, false_or, not_le] at this, exact ts this }, { /- Start from a set `s` containing an ε-neighborhood of the diagonal in `completion α`. To show that it is an entourage, we use the fact that `dist` is uniformly continuous on `completion α × completion α` (this is a general property of the extension of uniformly continuous functions). Therefore, the preimage of the ε-neighborhood of the diagonal in ℝ is an entourage in `completion α × completion α`. Massaging this property, it follows that the ε-neighborhood of the diagonal is an entourage in `completion α`, and therefore this is also the case of `s`. -/ rintros ⟨ε, εpos, hε⟩, let r : set (ℝ × ℝ) := {p | dist p.1 p.2 < ε}, have : r ∈ uniformity ℝ := metric.dist_mem_uniformity εpos, have T := uniform_continuous_def.1 (@completion.uniform_continuous_dist α _) r this, simp only [uniformity_prod_eq_prod, mem_prod_iff, exists_prop, filter.mem_map, set.mem_set_of_eq] at T, rcases T with ⟨t1, ht1, t2, ht2, ht⟩, refine mem_sets_of_superset ht1 _, have A : ∀a b : completion α, (a, b) ∈ t1 → dist a b < ε, { assume a b hab, have : ((a, b), (a, a)) ∈ set.prod t1 t2 := ⟨hab, refl_mem_uniformity ht2⟩, have I := ht this, simp [completion.dist_self, real.dist_eq, completion.dist_comm] at I, exact lt_of_le_of_lt (le_abs_self _) I }, show t1 ⊆ s, { rintros ⟨a, b⟩ hp, have : dist a b < ε := A a b hp, exact hε this }} end /-- If two points are at distance 0, then they coincide. -/ protected lemma completion.eq_of_dist_eq_zero (x y : completion α) (h : dist x y = 0) : x = y := begin /- This follows from the separation of `completion α` and from the description of entourages in terms of the distance. -/ have : separated_space (completion α) := by apply_instance, refine separated_def.1 this x y (λs hs, _), rcases (completion.mem_uniformity_dist s).1 hs with ⟨ε, εpos, hε⟩, rw ← h at εpos, exact hε εpos end /- Reformulate `completion.mem_uniformity_dist` in terms that are suitable for the definition of the metric space structure. -/ @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma completion.uniformity_dist' : uniformity (completion α) = (⨅ε:{ε:ℝ // ε>0}, 𝓟 {p | dist p.1 p.2 < ε.val}) := begin ext s, rw mem_infi, { simp [completion.mem_uniformity_dist, subset_def] }, { rintro ⟨r, hr⟩ ⟨p, hp⟩, use ⟨min r p, lt_min hr hp⟩, simp [lt_min_iff, (≥)] {contextual := tt} } end @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma completion.uniformity_dist : uniformity (completion α) = (⨅ ε>0, 𝓟 {p | dist p.1 p.2 < ε}) := by simpa [infi_subtype] using @completion.uniformity_dist' α _ /-- Metric space structure on the completion of a metric space. -/ instance completion.metric_space : metric_space (completion α) := { dist_self := completion.dist_self, eq_of_dist_eq_zero := completion.eq_of_dist_eq_zero, dist_comm := completion.dist_comm, dist_triangle := completion.dist_triangle, to_uniform_space := by apply_instance, uniformity_dist := completion.uniformity_dist } /-- The embedding of a metric space in its completion is an isometry. -/ lemma completion.coe_isometry : isometry (coe : α → completion α) := isometry_emetric_iff_metric.2 completion.dist_eq end metric
85effe81833aad160988bcf839f99dc91231e09a
9631e35ca5dd719ccc7d3a57ec322fd93d6a0776
/src/gcd_maze/definition.lean
430333451f336c4138f4997a3d68ad0811292b06
[]
no_license
kbuzzard/maze-game
123fa47e0262edb6d147178d239a345d847ff147
362ab394ae5218c43b24232aaeef08efc0067eab
refs/heads/master
1,670,131,466,823
1,598,546,702,000
1,598,546,702,000
290,744,081
5
1
null
1,598,546,703,000
1,598,523,538,000
Lean
UTF-8
Lean
false
false
1,564
lean
import tactic -- start in room 0 -- need to find our way to room gcd(a,b) /-- Solver docs : Terms of type `maze` are rooms in the game. -/ @[derive has_reflect] inductive direction : Type | north | east | south | west namespace maze /- ABC DEF GHI -/ section parameter_a_b parameters (a b : ℕ) @[reducible] definition maze := ℤ instance : has_coe ℤ (maze) := ⟨id⟩ /-- north = +a, south = -a, east = +b, west = -b-/ def go (d : direction) (m : maze) : maze := direction.rec_on d -- go north = add 8 (m + a) -- go east = add 3 (m + b) -- go south = subtract 8 (m - a) -- go west = subtract 3 (m - b) /-- The predicate of being able to escape from the maze -/ inductive can_escape : maze → Prop | exit : can_escape (nat.gcd a b) | go : ∀ d x, can_escape (go d x) → can_escape x end parameter_a_b end maze open maze direction open tactic meta def go (d : direction) : tactic unit := `[apply can_escape.go %%d, unfold maze.go, norm_num, fail_if_success { guard_target can_escape W } <|> fail "You hit a wall!" ] meta def tactic.interactive.n := `[go north] meta def tactic.interactive.s := `[go south] meta def tactic.interactive.e := `[go east] meta def tactic.interactive.w := `[go west] open tactic meta def out : tactic unit := `[apply can_escape.exit <|> fail "you are not at the exit level J"] -- theorem solve : can_escape A := -- begin -- s, -- sorry, -- end -- I have no solver -- meta def xyzzy : tactic unit := sorry -- --`[ {e,n,e,e,out} <|> {n,e,e,out} <|> {e,e,out} <|> {e,out} <|> {out} ]
0cf2b1b515b873eec4dcb27159a34bfe299acb0b
cbcb0199842f03e7606d4e43666573fc15dd07a5
/src/topology/metric_space/completion.lean
a6cfd04fae82925991b829df21fd15d6e90a6226
[ "Apache-2.0" ]
permissive
truonghoangle/mathlib
a6a7c14b3767ec71156239d8ea97f6921fe79627
673bae584febcd830c2c9256eb7e7a81e27ed303
refs/heads/master
1,590,347,998,944
1,559,728,860,000
1,559,728,860,000
187,431,971
0
0
null
1,558,238,525,000
1,558,238,525,000
null
UTF-8
Lean
false
false
8,740
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 The completion of a metric space. Completion of uniform spaces are already defined in `topology.uniform_space.completion`. We show here that the uniform space completion of a metric space inherits a metric space structure, by extending the distance to the completion and checking that it is indeed a distance, and that it defines the same uniformity as the already defined uniform structure on the completion -/ import topology.uniform_space.completion topology.metric_space.isometry open lattice set filter uniform_space uniform_space.completion noncomputable theory universes u variables {α : Type u} [metric_space α] namespace metric /-- The distance on the completion is obtained by extending the distance on the original space, by uniform continuity. -/ instance : has_dist (completion α) := ⟨λx y, completion.extension (λp:α×α, dist p.1 p.2) (completion.prod (x, y))⟩ /-- The new distance is uniformly continuous. -/ protected lemma completion.uniform_continuous_dist : uniform_continuous (λp:completion α × completion α, dist p.1 p.2) := uniform_continuous.comp uniform_continuous_prod uniform_continuous_extension /-- The new distance is an extension of the original distance. -/ protected lemma completion.dist_eq (x y : α) : dist (x : completion α) y = dist x y := begin unfold dist, rw [completion.prod_coe_coe, completion.extension_coe], exact uniform_continuous_dist', end /- Let us check that the new distance satisfies the axioms of a distance, by starting from the properties on α and extending them to `completion α` by continuity. -/ protected lemma completion.dist_self (x : completion α) : dist x x = 0 := begin apply induction_on x, { refine is_closed_eq _ continuous_const, have : continuous (λx : completion α, (x, x)) := continuous.prod_mk continuous_id continuous_id, exact completion.uniform_continuous_dist.continuous.comp this }, { assume a, rw [completion.dist_eq, dist_self] } end protected lemma completion.dist_comm (x y : completion α) : dist x y = dist y x := begin apply induction_on₂ x y, { refine is_closed_eq completion.uniform_continuous_dist.continuous _, exact completion.uniform_continuous_dist.continuous.comp continuous_swap }, { assume a b, rw [completion.dist_eq, completion.dist_eq, dist_comm] } end protected lemma completion.dist_triangle (x y z : completion α) : dist x z ≤ dist x y + dist y z := begin apply induction_on₃ x y z, { refine is_closed_le _ (continuous_add _ _), { have : continuous (λp : completion α × completion α × completion α, (p.1, p.2.2)) := continuous.prod_mk continuous_fst (continuous.comp continuous_snd continuous_snd), exact completion.uniform_continuous_dist.continuous.comp this }, { have : continuous (λp : completion α × completion α × completion α, (p.1, p.2.1)) := continuous.prod_mk continuous_fst (continuous_fst.comp continuous_snd), exact completion.uniform_continuous_dist.continuous.comp this }, { have : continuous (λp : completion α × completion α × completion α, (p.2.1, p.2.2)) := continuous.prod_mk (continuous_fst.comp continuous_snd) (continuous.comp continuous_snd continuous_snd), exact continuous.comp completion.uniform_continuous_dist.continuous this }}, { assume a b c, rw [completion.dist_eq, completion.dist_eq, completion.dist_eq], exact dist_triangle a b c } end /-- Elements of the uniformity (defined generally for completions) can be characterized in terms of the distance. -/ protected lemma completion.mem_uniformity_dist (s : set (completion α × completion α)) : s ∈ uniformity (completion α) ↔ (∃ε>0, ∀{a b}, dist a b < ε → (a, b) ∈ s) := begin split, { /- Start from an entourage `s`. It contains a closed entourage `t`. Its pullback in α is an entourage, so it contains an ε-neighborhood of the diagonal by definition of the entourages in metric spaces. Then `t` contains an ε-neighborhood of the diagonal in `completion α`, as closed properties pass to the completion. -/ assume hs, rcases mem_uniformity_is_closed hs with ⟨t, ht, ⟨tclosed, ts⟩⟩, have A : {x : α × α | (coe (x.1), coe (x.2)) ∈ t} ∈ uniformity α := uniform_continuous_def.1 (uniform_continuous_coe α) t ht, rcases mem_uniformity_dist.1 A with ⟨ε, εpos, hε⟩, refine ⟨ε, εpos, λx y hxy, _⟩, have : ε ≤ dist x y ∨ (x, y) ∈ t, { apply induction_on₂ x y, { have : {x : completion α × completion α | ε ≤ dist (x.fst) (x.snd) ∨ (x.fst, x.snd) ∈ t} = {p : completion α × completion α | ε ≤ dist p.1 p.2} ∪ t, by ext; simp, rw this, apply is_closed_union _ tclosed, exact is_closed_le continuous_const completion.uniform_continuous_dist.continuous }, { assume x y, rw completion.dist_eq, by_cases h : ε ≤ dist x y, { exact or.inl h }, { have Z := hε (not_le.1 h), simp only [set.mem_set_of_eq] at Z, exact or.inr Z }}}, simp only [not_le.mpr hxy, false_or, not_le] at this, exact ts this }, { /- Start from a set `s` containing an ε-neighborhood of the diagonal in `completion α`. To show that it is an entourage, we use the fact that `dist` is uniformly continuous on `completion α × completion α` (this is a general property of the extension of uniformly continuous functions). Therefore, the preimage of the ε-neighborhood of the diagonal in ℝ is an entourage in `completion α × completion α`. Massaging this property, it follows that the ε-neighborhood of the diagonal is an entourage in `completion α`, and therefore this is also the case of `s`. -/ rintros ⟨ε, εpos, hε⟩, let r : set (ℝ × ℝ) := {p | dist p.1 p.2 < ε}, have : r ∈ uniformity ℝ := metric.dist_mem_uniformity εpos, have T := uniform_continuous_def.1 (@completion.uniform_continuous_dist α _) r this, simp only [uniformity_prod_eq_prod, mem_prod_iff, exists_prop, filter.mem_map, set.mem_set_of_eq] at T, rcases T with ⟨t1, ht1, t2, ht2, ht⟩, refine mem_sets_of_superset ht1 _, have A : ∀a b : completion α, (a, b) ∈ t1 → dist a b < ε, { assume a b hab, have : ((a, b), (a, a)) ∈ set.prod t1 t2 := ⟨hab, refl_mem_uniformity ht2⟩, have I := ht this, simp [completion.dist_self, real.dist_eq, completion.dist_comm] at I, exact lt_of_le_of_lt (le_abs_self _) I }, show t1 ⊆ s, { rintros ⟨a, b⟩ hp, have : dist a b < ε := A a b hp, exact hε this }} end /-- If two points are at distance 0, then they coincide. -/ protected lemma completion.eq_of_dist_eq_zero (x y : completion α) (h : dist x y = 0) : x = y := begin /- This follows from the separation of `completion α` and from the description of entourages in terms of the distance. -/ have : separated (completion α) := by apply_instance, refine separated_def.1 this x y (λs hs, _), rcases (completion.mem_uniformity_dist s).1 hs with ⟨ε, εpos, hε⟩, rw ← h at εpos, exact hε εpos end /- Reformulate `completion.mem_uniformity_dist` in terms that are suitable for the definition of the metric space structure. -/ protected lemma completion.uniformity_dist' : uniformity (completion α) = (⨅ε:{ε:ℝ // ε>0}, principal {p | dist p.1 p.2 < ε.val}) := begin ext s, rw mem_infi, { simp [completion.mem_uniformity_dist, subset_def] }, { rintro ⟨r, hr⟩ ⟨p, hp⟩, use ⟨min r p, lt_min hr hp⟩, simp [lt_min_iff, (≥)] {contextual := tt} }, { exact ⟨⟨1, zero_lt_one⟩⟩ } end protected lemma completion.uniformity_dist : uniformity (completion α) = (⨅ ε>0, principal {p | dist p.1 p.2 < ε}) := by simpa [infi_subtype] using @completion.uniformity_dist' α _ /-- Metric space structure on the completion of a metric space. -/ instance completion.metric_space : metric_space (completion α) := { dist_self := completion.dist_self, eq_of_dist_eq_zero := completion.eq_of_dist_eq_zero, dist_comm := completion.dist_comm, dist_triangle := completion.dist_triangle, to_uniform_space := by apply_instance, uniformity_dist := completion.uniformity_dist } /-- The embedding of a metric space in its completion is an isometry. -/ lemma completion.coe_isometry : isometry (coe : α → completion α) := isometry_emetric_iff_metric.2 completion.dist_eq end metric
fe1a0dc298e4dc50028c429c3ebc3b674470af71
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/abelian/basic.lean
053e568b728595408a0917ce00a8571f9691818a
[ "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
30,039
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel, Johan Commelin, Scott Morrison -/ import category_theory.limits.constructions.pullbacks import category_theory.limits.shapes.biproducts import category_theory.limits.shapes.images import category_theory.limits.constructions.limits_of_products_and_equalizers import category_theory.abelian.non_preadditive /-! # Abelian categories This file contains the definition and basic properties of abelian categories. There are many definitions of abelian category. Our definition is as follows: A category is called abelian if it is preadditive, has a finite products, kernels and cokernels, and if every monomorphism and epimorphism is normal. It should be noted that if we also assume coproducts, then preadditivity is actually a consequence of the other properties, as we show in `non_preadditive_abelian.lean`. However, this fact is of little practical relevance, since essentially all interesting abelian categories come with a preadditive structure. In this way, by requiring preadditivity, we allow the user to pass in the "native" preadditive structure for the specific category they are working with. ## Main definitions * `abelian` is the type class indicating that a category is abelian. It extends `preadditive`. * `abelian.image f` is `kernel (cokernel.π f)`, and * `abelian.coimage f` is `cokernel (kernel.ι f)`. ## Main results * In an abelian category, mono + epi = iso. * If `f : X ⟶ Y`, then the map `factor_thru_image f : X ⟶ image f` is an epimorphism, and the map `factor_thru_coimage f : coimage f ⟶ Y` is a monomorphism. * Factoring through the image and coimage is a strong epi-mono factorisation. This means that * every abelian category has images. We provide the isomorphism `image_iso_image : abelian.image f ≅ limits.image f`. * the canonical morphism `coimage_image_comparison : coimage f ⟶ image f` is an isomorphism. * We provide the alternate characterisation of an abelian category as a category with (co)kernels and finite products, and in which the canonical coimage-image comparison morphism is always an isomorphism. * Every epimorphism is a cokernel of its kernel. Every monomorphism is a kernel of its cokernel. * The pullback of an epimorphism is an epimorphism. The pushout of a monomorphism is a monomorphism. (This is not to be confused with the fact that the pullback of a monomorphism is a monomorphism, which is true in any category). ## Implementation notes The typeclass `abelian` does not extend `non_preadditive_abelian`, to avoid having to deal with comparing the two `has_zero_morphisms` instances (one from `preadditive` in `abelian`, and the other a field of `non_preadditive_abelian`). As a consequence, at the beginning of this file we trivially build a `non_preadditive_abelian` instance from an `abelian` instance, and use this to restate a number of theorems, in each case just reusing the proof from `non_preadditive_abelian.lean`. We don't show this yet, but abelian categories are finitely complete and finitely cocomplete. However, the limits we can construct at this level of generality will most likely be less nice than the ones that can be created in specific applications. For this reason, we adopt the following convention: * If the statement of a theorem involves limits, the existence of these limits should be made an explicit typeclass parameter. * If a limit only appears in a proof, but not in the statement of a theorem, the limit should not be a typeclass parameter, but instead be created using `abelian.has_pullbacks` or a similar definition. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] * [P. Aluffi, *Algebra: Chapter 0*][aluffi2016] -/ noncomputable theory open category_theory open category_theory.preadditive open category_theory.limits universes v u namespace category_theory variables {C : Type u} [category.{v} C] variables (C) /-- A (preadditive) category `C` is called abelian if it has all finite products, all kernels and cokernels, and if every monomorphism is the kernel of some morphism and every epimorphism is the cokernel of some morphism. (This definition implies the existence of zero objects: finite products give a terminal object, and in a preadditive category any terminal object is a zero object.) -/ class abelian extends preadditive C, normal_mono_category C, normal_epi_category C := [has_finite_products : has_finite_products C] [has_kernels : has_kernels C] [has_cokernels : has_cokernels C] attribute [instance, priority 100] abelian.has_finite_products attribute [instance, priority 100] abelian.has_kernels abelian.has_cokernels end category_theory open category_theory /-! We begin by providing an alternative constructor: a preadditive category with kernels, cokernels, and finite products, in which the coimage-image comparison morphism is always an isomorphism, is an abelian category. -/ namespace category_theory.abelian variables {C : Type u} [category.{v} C] [preadditive C] variables [limits.has_kernels C] [limits.has_cokernels C] namespace of_coimage_image_comparison_is_iso /-- The factorisation of a morphism through its abelian image. -/ @[simps] def image_mono_factorisation {X Y : C} (f : X ⟶ Y) : mono_factorisation f := { I := abelian.image f, m := kernel.ι _, m_mono := infer_instance, e := kernel.lift _ f (cokernel.condition _), fac' := kernel.lift_ι _ _ _ } lemma image_mono_factorisation_e' {X Y : C} (f : X ⟶ Y) : (image_mono_factorisation f).e = cokernel.π _ ≫ abelian.coimage_image_comparison f := begin ext, simp only [abelian.coimage_image_comparison, image_mono_factorisation_e, category.assoc, cokernel.π_desc_assoc], end /-- If the coimage-image comparison morphism for a morphism `f` is an isomorphism, we obtain an image factorisation of `f`. -/ def image_factorisation {X Y : C} (f : X ⟶ Y) [is_iso (abelian.coimage_image_comparison f)] : image_factorisation f := { F := image_mono_factorisation f, is_image := { lift := λ F, inv (abelian.coimage_image_comparison f) ≫ cokernel.desc _ F.e F.kernel_ι_comp, lift_fac' := λ F, begin simp only [image_mono_factorisation_m, is_iso.inv_comp_eq, category.assoc, abelian.coimage_image_comparison], ext, rw [limits.coequalizer.π_desc_assoc, limits.coequalizer.π_desc_assoc, F.fac, kernel.lift_ι] end } } instance [has_zero_object C] {X Y : C} (f : X ⟶ Y) [mono f] [is_iso (abelian.coimage_image_comparison f)] : is_iso (image_mono_factorisation f).e := by { rw image_mono_factorisation_e', exact is_iso.comp_is_iso } instance [has_zero_object C] {X Y : C} (f : X ⟶ Y) [epi f] : is_iso (image_mono_factorisation f).m := by { dsimp, apply_instance } variables [∀ {X Y : C} (f : X ⟶ Y), is_iso (abelian.coimage_image_comparison f)] /-- A category in which coimage-image comparisons are all isomorphisms has images. -/ lemma has_images : has_images C := { has_image := λ X Y f, { exists_image := ⟨image_factorisation f⟩ } } variables [limits.has_finite_products C] local attribute [instance] limits.has_finite_biproducts.of_has_finite_products /-- A category with finite products in which coimage-image comparisons are all isomorphisms is a normal mono category. -/ def normal_mono_category : normal_mono_category C := { normal_mono_of_mono := λ X Y f m, { Z := _, g := cokernel.π f, w := by simp, is_limit := begin haveI : limits.has_images C := has_images, haveI : has_equalizers C := preadditive.has_equalizers_of_has_kernels, haveI : has_zero_object C := limits.has_zero_object_of_has_finite_biproducts _, have aux : _ := _, refine is_limit_aux _ (λ A, limit.lift _ _ ≫ inv (image_mono_factorisation f).e) aux _, { intros A g hg, rw [kernel_fork.ι_of_ι] at hg, rw [← cancel_mono f, hg, ← aux, kernel_fork.ι_of_ι], }, { intro A, simp only [kernel_fork.ι_of_ι, category.assoc], convert limit.lift_π _ _ using 2, rw [is_iso.inv_comp_eq, eq_comm], exact (image_mono_factorisation f).fac, }, end }, } /-- A category with finite products in which coimage-image comparisons are all isomorphisms is a normal epi category. -/ def normal_epi_category : normal_epi_category C := { normal_epi_of_epi := λ X Y f m, { W := kernel f, g := kernel.ι _, w := kernel.condition _, is_colimit := begin haveI : limits.has_images C := has_images, haveI : has_equalizers C := preadditive.has_equalizers_of_has_kernels, haveI : has_zero_object C := limits.has_zero_object_of_has_finite_biproducts _, have aux : _ := _, refine is_colimit_aux _ (λ A, inv (image_mono_factorisation f).m ≫ inv (abelian.coimage_image_comparison f) ≫ colimit.desc _ _) aux _, { intros A g hg, rw [cokernel_cofork.π_of_π] at hg, rw [← cancel_epi f, hg, ← aux, cokernel_cofork.π_of_π], }, { intro A, simp only [cokernel_cofork.π_of_π, ← category.assoc], convert colimit.ι_desc _ _ using 2, rw [is_iso.comp_inv_eq, is_iso.comp_inv_eq, eq_comm, ←image_mono_factorisation_e'], exact (image_mono_factorisation f).fac, } end }, } end of_coimage_image_comparison_is_iso variables [∀ {X Y : C} (f : X ⟶ Y), is_iso (abelian.coimage_image_comparison f)] [limits.has_finite_products C] local attribute [instance] of_coimage_image_comparison_is_iso.normal_mono_category local attribute [instance] of_coimage_image_comparison_is_iso.normal_epi_category /-- A preadditive category with kernels, cokernels, and finite products, in which the coimage-image comparison morphism is always an isomorphism, is an abelian category. The Stacks project uses this characterisation at the definition of an abelian category. See <https://stacks.math.columbia.edu/tag/0109>. -/ def of_coimage_image_comparison_is_iso : abelian C := {} end category_theory.abelian namespace category_theory.abelian variables {C : Type u} [category.{v} C] [abelian C] /-- An abelian category has finite biproducts. -/ @[priority 100] instance has_finite_biproducts : has_finite_biproducts C := limits.has_finite_biproducts.of_has_finite_products @[priority 100] instance has_binary_biproducts : has_binary_biproducts C := limits.has_binary_biproducts_of_finite_biproducts _ @[priority 100] instance has_zero_object : has_zero_object C := has_zero_object_of_has_initial_object section to_non_preadditive_abelian /-- Every abelian category is, in particular, `non_preadditive_abelian`. -/ def non_preadditive_abelian : non_preadditive_abelian C := { ..‹abelian C› } end to_non_preadditive_abelian section /-! We now promote some instances that were constructed using `non_preadditive_abelian`. -/ local attribute [instance] non_preadditive_abelian variables {P Q : C} (f : P ⟶ Q) /-- The map `p : P ⟶ image f` is an epimorphism -/ instance : epi (abelian.factor_thru_image f) := by apply_instance instance is_iso_factor_thru_image [mono f] : is_iso (abelian.factor_thru_image f) := by apply_instance /-- The canonical morphism `i : coimage f ⟶ Q` is a monomorphism -/ instance : mono (abelian.factor_thru_coimage f) := by apply_instance instance is_iso_factor_thru_coimage [epi f] : is_iso (abelian.factor_thru_coimage f) := by apply_instance end section factor local attribute [instance] non_preadditive_abelian variables {P Q : C} (f : P ⟶ Q) section lemma mono_of_kernel_ι_eq_zero (h : kernel.ι f = 0) : mono f := mono_of_kernel_zero h lemma epi_of_cokernel_π_eq_zero (h : cokernel.π f = 0) : epi f := begin apply normal_mono_category.epi_of_zero_cokernel _ (cokernel f), simp_rw ←h, exact is_colimit.of_iso_colimit (colimit.is_colimit (parallel_pair f 0)) (iso_of_π _) end end section variables {f} lemma image_ι_comp_eq_zero {R : C} {g : Q ⟶ R} (h : f ≫ g = 0) : abelian.image.ι f ≫ g = 0 := zero_of_epi_comp (abelian.factor_thru_image f) $ by simp [h] lemma comp_coimage_π_eq_zero {R : C} {g : Q ⟶ R} (h : f ≫ g = 0) : f ≫ abelian.coimage.π g = 0 := zero_of_comp_mono (abelian.factor_thru_coimage g) $ by simp [h] end /-- Factoring through the image is a strong epi-mono factorisation. -/ @[simps] def image_strong_epi_mono_factorisation : strong_epi_mono_factorisation f := { I := abelian.image f, m := image.ι f, m_mono := by apply_instance, e := abelian.factor_thru_image f, e_strong_epi := strong_epi_of_epi _ } /-- Factoring through the coimage is a strong epi-mono factorisation. -/ @[simps] def coimage_strong_epi_mono_factorisation : strong_epi_mono_factorisation f := { I := abelian.coimage f, m := abelian.factor_thru_coimage f, m_mono := by apply_instance, e := coimage.π f, e_strong_epi := strong_epi_of_epi _ } end factor section has_strong_epi_mono_factorisations /-- An abelian category has strong epi-mono factorisations. -/ @[priority 100] instance : has_strong_epi_mono_factorisations C := has_strong_epi_mono_factorisations.mk $ λ X Y f, image_strong_epi_mono_factorisation f /- In particular, this means that it has well-behaved images. -/ example : has_images C := by apply_instance example : has_image_maps C := by apply_instance end has_strong_epi_mono_factorisations section images variables {X Y : C} (f : X ⟶ Y) /-- The coimage-image comparison morphism is always an isomorphism in an abelian category. See `category_theory.abelian.of_coimage_image_comparison_is_iso` for the converse. -/ instance : is_iso (coimage_image_comparison f) := begin convert is_iso.of_iso (is_image.iso_ext (coimage_strong_epi_mono_factorisation f).to_mono_is_image (image_strong_epi_mono_factorisation f).to_mono_is_image), ext, change _ = _ ≫ (image_strong_epi_mono_factorisation f).m, simp [-image_strong_epi_mono_factorisation_to_mono_factorisation_m] end /-- There is a canonical isomorphism between the abelian coimage and the abelian image of a morphism. -/ abbreviation coimage_iso_image : abelian.coimage f ≅ abelian.image f := as_iso (coimage_image_comparison f) /-- There is a canonical isomorphism between the abelian coimage and the categorical image of a morphism. -/ abbreviation coimage_iso_image' : abelian.coimage f ≅ image f := is_image.iso_ext (coimage_strong_epi_mono_factorisation f).to_mono_is_image (image.is_image f) /-- There is a canonical isomorphism between the abelian image and the categorical image of a morphism. -/ abbreviation image_iso_image : abelian.image f ≅ image f := is_image.iso_ext (image_strong_epi_mono_factorisation f).to_mono_is_image (image.is_image f) end images section cokernel_of_kernel variables {X Y : C} {f : X ⟶ Y} local attribute [instance] non_preadditive_abelian /-- In an abelian category, an epi is the cokernel of its kernel. More precisely: If `f` is an epimorphism and `s` is some limit kernel cone on `f`, then `f` is a cokernel of `fork.ι s`. -/ def epi_is_cokernel_of_kernel [epi f] (s : fork f 0) (h : is_limit s) : is_colimit (cokernel_cofork.of_π f (kernel_fork.condition s)) := non_preadditive_abelian.epi_is_cokernel_of_kernel s h /-- In an abelian category, a mono is the kernel of its cokernel. More precisely: If `f` is a monomorphism and `s` is some colimit cokernel cocone on `f`, then `f` is a kernel of `cofork.π s`. -/ def mono_is_kernel_of_cokernel [mono f] (s : cofork f 0) (h : is_colimit s) : is_limit (kernel_fork.of_ι f (cokernel_cofork.condition s)) := non_preadditive_abelian.mono_is_kernel_of_cokernel s h variables (f) /-- In an abelian category, any morphism that turns to zero when precomposed with the kernel of an epimorphism factors through that epimorphism. -/ def epi_desc [epi f] {T : C} (g : X ⟶ T) (hg : kernel.ι f ≫ g = 0) : Y ⟶ T := (epi_is_cokernel_of_kernel _ (limit.is_limit _)).desc (cokernel_cofork.of_π _ hg) @[simp, reassoc] lemma comp_epi_desc [epi f] {T : C} (g : X ⟶ T) (hg : kernel.ι f ≫ g = 0) : f ≫ epi_desc f g hg = g := (epi_is_cokernel_of_kernel _ (limit.is_limit _)).fac (cokernel_cofork.of_π _ hg) walking_parallel_pair.one /-- In an abelian category, any morphism that turns to zero when postcomposed with the cokernel of a monomorphism factors through that monomorphism. -/ def mono_lift [mono f] {T : C} (g : T ⟶ Y) (hg : g ≫ cokernel.π f = 0) : T ⟶ X := (mono_is_kernel_of_cokernel _ (colimit.is_colimit _)).lift (kernel_fork.of_ι _ hg) @[simp, reassoc] lemma mono_lift_comp [mono f] {T : C} (g : T ⟶ Y) (hg : g ≫ cokernel.π f = 0) : mono_lift f g hg ≫ f = g := (mono_is_kernel_of_cokernel _ (colimit.is_colimit _)).fac (kernel_fork.of_ι _ hg) walking_parallel_pair.zero end cokernel_of_kernel section @[priority 100] instance has_equalizers : has_equalizers C := preadditive.has_equalizers_of_has_kernels /-- Any abelian category has pullbacks -/ @[priority 100] instance has_pullbacks : has_pullbacks C := has_pullbacks_of_has_binary_products_of_has_equalizers C end section @[priority 100] instance has_coequalizers : has_coequalizers C := preadditive.has_coequalizers_of_has_cokernels /-- Any abelian category has pushouts -/ @[priority 100] instance has_pushouts : has_pushouts C := has_pushouts_of_has_binary_coproducts_of_has_coequalizers C @[priority 100] instance has_finite_limits : has_finite_limits C := limits.finite_limits_from_equalizers_and_finite_products @[priority 100] instance has_finite_colimits : has_finite_colimits C := limits.finite_colimits_from_coequalizers_and_finite_coproducts end namespace pullback_to_biproduct_is_kernel variables [limits.has_pullbacks C] {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) /-! This section contains a slightly technical result about pullbacks and biproducts. We will need it in the proof that the pullback of an epimorphism is an epimorpism. -/ /-- The canonical map `pullback f g ⟶ X ⊞ Y` -/ abbreviation pullback_to_biproduct : pullback f g ⟶ X ⊞ Y := biprod.lift pullback.fst pullback.snd /-- The canonical map `pullback f g ⟶ X ⊞ Y` induces a kernel cone on the map `biproduct X Y ⟶ Z` induced by `f` and `g`. A slightly more intuitive way to think of this may be that it induces an equalizer fork on the maps induced by `(f, 0)` and `(0, g)`. -/ abbreviation pullback_to_biproduct_fork : kernel_fork (biprod.desc f (-g)) := kernel_fork.of_ι (pullback_to_biproduct f g) $ by rw [biprod.lift_desc, comp_neg, pullback.condition, add_right_neg] /-- The canonical map `pullback f g ⟶ X ⊞ Y` is a kernel of the map induced by `(f, -g)`. -/ def is_limit_pullback_to_biproduct : is_limit (pullback_to_biproduct_fork f g) := fork.is_limit.mk _ (λ s, pullback.lift (fork.ι s ≫ biprod.fst) (fork.ι s ≫ biprod.snd) $ sub_eq_zero.1 $ by rw [category.assoc, category.assoc, ←comp_sub, sub_eq_add_neg, ←comp_neg, ←biprod.desc_eq, kernel_fork.condition s]) (λ s, begin ext; rw [fork.ι_of_ι, category.assoc], { rw [biprod.lift_fst, pullback.lift_fst] }, { rw [biprod.lift_snd, pullback.lift_snd] } end) (λ s m h, by ext; simp [←h]) end pullback_to_biproduct_is_kernel namespace biproduct_to_pushout_is_cokernel variables [limits.has_pushouts C] {W X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) /-- The canonical map `Y ⊞ Z ⟶ pushout f g` -/ abbreviation biproduct_to_pushout : Y ⊞ Z ⟶ pushout f g := biprod.desc pushout.inl pushout.inr /-- The canonical map `Y ⊞ Z ⟶ pushout f g` induces a cokernel cofork on the map `X ⟶ Y ⊞ Z` induced by `f` and `-g`. -/ abbreviation biproduct_to_pushout_cofork : cokernel_cofork (biprod.lift f (-g)) := cokernel_cofork.of_π (biproduct_to_pushout f g) $ by rw [biprod.lift_desc, neg_comp, pushout.condition, add_right_neg] /-- The cofork induced by the canonical map `Y ⊞ Z ⟶ pushout f g` is in fact a colimit cokernel cofork. -/ def is_colimit_biproduct_to_pushout : is_colimit (biproduct_to_pushout_cofork f g) := cofork.is_colimit.mk _ (λ s, pushout.desc (biprod.inl ≫ cofork.π s) (biprod.inr ≫ cofork.π s) $ sub_eq_zero.1 $ by rw [←category.assoc, ←category.assoc, ←sub_comp, sub_eq_add_neg, ←neg_comp, ←biprod.lift_eq, cofork.condition s, zero_comp]) (λ s, by ext; simp) (λ s m h, by ext; simp [←h] ) end biproduct_to_pushout_is_cokernel section epi_pullback variables [limits.has_pullbacks C] {W X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) /-- In an abelian category, the pullback of an epimorphism is an epimorphism. Proof from [aluffi2016, IX.2.3], cf. [borceux-vol2, 1.7.6] -/ instance epi_pullback_of_epi_f [epi f] : epi (pullback.snd : pullback f g ⟶ Y) := -- It will suffice to consider some morphism e : Y ⟶ R such that -- pullback.snd ≫ e = 0 and show that e = 0. epi_of_cancel_zero _ $ λ R e h, begin -- Consider the morphism u := (0, e) : X ⊞ Y⟶ R. let u := biprod.desc (0 : X ⟶ R) e, -- The composite pullback f g ⟶ X ⊞ Y ⟶ R is zero by assumption. have hu : pullback_to_biproduct_is_kernel.pullback_to_biproduct f g ≫ u = 0 := by simpa, -- pullback_to_biproduct f g is a kernel of (f, -g), so (f, -g) is a -- cokernel of pullback_to_biproduct f g have := epi_is_cokernel_of_kernel _ (pullback_to_biproduct_is_kernel.is_limit_pullback_to_biproduct f g), -- We use this fact to obtain a factorization of u through (f, -g) via some d : Z ⟶ R. obtain ⟨d, hd⟩ := cokernel_cofork.is_colimit.desc' this u hu, change Z ⟶ R at d, change biprod.desc f (-g) ≫ d = u at hd, -- But then f ≫ d = 0: have : f ≫ d = 0, calc f ≫ d = (biprod.inl ≫ biprod.desc f (-g)) ≫ d : by rw biprod.inl_desc ... = biprod.inl ≫ u : by rw [category.assoc, hd] ... = 0 : biprod.inl_desc _ _, -- But f is an epimorphism, so d = 0... have : d = 0 := (cancel_epi f).1 (by simpa), -- ...or, in other words, e = 0. calc e = biprod.inr ≫ u : by rw biprod.inr_desc ... = biprod.inr ≫ biprod.desc f (-g) ≫ d : by rw ←hd ... = biprod.inr ≫ biprod.desc f (-g) ≫ 0 : by rw this ... = (biprod.inr ≫ biprod.desc f (-g)) ≫ 0 : by rw ←category.assoc ... = 0 : has_zero_morphisms.comp_zero _ _ end /-- In an abelian category, the pullback of an epimorphism is an epimorphism. -/ instance epi_pullback_of_epi_g [epi g] : epi (pullback.fst : pullback f g ⟶ X) := -- It will suffice to consider some morphism e : X ⟶ R such that -- pullback.fst ≫ e = 0 and show that e = 0. epi_of_cancel_zero _ $ λ R e h, begin -- Consider the morphism u := (e, 0) : X ⊞ Y ⟶ R. let u := biprod.desc e (0 : Y ⟶ R), -- The composite pullback f g ⟶ X ⊞ Y ⟶ R is zero by assumption. have hu : pullback_to_biproduct_is_kernel.pullback_to_biproduct f g ≫ u = 0 := by simpa, -- pullback_to_biproduct f g is a kernel of (f, -g), so (f, -g) is a -- cokernel of pullback_to_biproduct f g have := epi_is_cokernel_of_kernel _ (pullback_to_biproduct_is_kernel.is_limit_pullback_to_biproduct f g), -- We use this fact to obtain a factorization of u through (f, -g) via some d : Z ⟶ R. obtain ⟨d, hd⟩ := cokernel_cofork.is_colimit.desc' this u hu, change Z ⟶ R at d, change biprod.desc f (-g) ≫ d = u at hd, -- But then (-g) ≫ d = 0: have : (-g) ≫ d = 0, calc (-g) ≫ d = (biprod.inr ≫ biprod.desc f (-g)) ≫ d : by rw biprod.inr_desc ... = biprod.inr ≫ u : by rw [category.assoc, hd] ... = 0 : biprod.inr_desc _ _, -- But g is an epimorphism, thus so is -g, so d = 0... have : d = 0 := (cancel_epi (-g)).1 (by simpa), -- ...or, in other words, e = 0. calc e = biprod.inl ≫ u : by rw biprod.inl_desc ... = biprod.inl ≫ biprod.desc f (-g) ≫ d : by rw ←hd ... = biprod.inl ≫ biprod.desc f (-g) ≫ 0 : by rw this ... = (biprod.inl ≫ biprod.desc f (-g)) ≫ 0 : by rw ←category.assoc ... = 0 : has_zero_morphisms.comp_zero _ _ end lemma epi_snd_of_is_limit [epi f] {s : pullback_cone f g} (hs : is_limit s) : epi s.snd := begin convert epi_of_epi_fac (is_limit.cone_point_unique_up_to_iso_hom_comp (limit.is_limit _) hs _), { refl }, { exact abelian.epi_pullback_of_epi_f _ _ } end lemma epi_fst_of_is_limit [epi g] {s : pullback_cone f g} (hs : is_limit s) : epi s.fst := begin convert epi_of_epi_fac (is_limit.cone_point_unique_up_to_iso_hom_comp (limit.is_limit _) hs _), { refl }, { exact abelian.epi_pullback_of_epi_g _ _ } end /-- Suppose `f` and `g` are two morphisms with a common codomain and suppose we have written `g` as an epimorphism followed by a monomorphism. If `f` factors through the mono part of this factorization, then any pullback of `g` along `f` is an epimorphism. -/ lemma epi_fst_of_factor_thru_epi_mono_factorization (g₁ : Y ⟶ W) [epi g₁] (g₂ : W ⟶ Z) [mono g₂] (hg : g₁ ≫ g₂ = g) (f' : X ⟶ W) (hf : f' ≫ g₂ = f) (t : pullback_cone f g) (ht : is_limit t) : epi t.fst := by apply epi_fst_of_is_limit _ _ (pullback_cone.is_limit_of_factors f g g₂ f' g₁ hf hg t ht) end epi_pullback section mono_pushout variables [limits.has_pushouts C] {W X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) instance mono_pushout_of_mono_f [mono f] : mono (pushout.inr : Z ⟶ pushout f g) := mono_of_cancel_zero _ $ λ R e h, begin let u := biprod.lift (0 : R ⟶ Y) e, have hu : u ≫ biproduct_to_pushout_is_cokernel.biproduct_to_pushout f g = 0 := by simpa, have := mono_is_kernel_of_cokernel _ (biproduct_to_pushout_is_cokernel.is_colimit_biproduct_to_pushout f g), obtain ⟨d, hd⟩ := kernel_fork.is_limit.lift' this u hu, change R ⟶ X at d, change d ≫ biprod.lift f (-g) = u at hd, have : d ≫ f = 0, calc d ≫ f = d ≫ biprod.lift f (-g) ≫ biprod.fst : by rw biprod.lift_fst ... = u ≫ biprod.fst : by rw [←category.assoc, hd] ... = 0 : biprod.lift_fst _ _, have : d = 0 := (cancel_mono f).1 (by simpa), calc e = u ≫ biprod.snd : by rw biprod.lift_snd ... = (d ≫ biprod.lift f (-g)) ≫ biprod.snd : by rw ←hd ... = (0 ≫ biprod.lift f (-g)) ≫ biprod.snd : by rw this ... = 0 ≫ biprod.lift f (-g) ≫ biprod.snd : by rw category.assoc ... = 0 : zero_comp end instance mono_pushout_of_mono_g [mono g] : mono (pushout.inl : Y ⟶ pushout f g) := mono_of_cancel_zero _ $ λ R e h, begin let u := biprod.lift e (0 : R ⟶ Z), have hu : u ≫ biproduct_to_pushout_is_cokernel.biproduct_to_pushout f g = 0 := by simpa, have := mono_is_kernel_of_cokernel _ (biproduct_to_pushout_is_cokernel.is_colimit_biproduct_to_pushout f g), obtain ⟨d, hd⟩ := kernel_fork.is_limit.lift' this u hu, change R ⟶ X at d, change d ≫ biprod.lift f (-g) = u at hd, have : d ≫ (-g) = 0, calc d ≫ (-g) = d ≫ biprod.lift f (-g) ≫ biprod.snd : by rw biprod.lift_snd ... = u ≫ biprod.snd : by rw [←category.assoc, hd] ... = 0 : biprod.lift_snd _ _, have : d = 0 := (cancel_mono (-g)).1 (by simpa), calc e = u ≫ biprod.fst : by rw biprod.lift_fst ... = (d ≫ biprod.lift f (-g)) ≫ biprod.fst : by rw ←hd ... = (0 ≫ biprod.lift f (-g)) ≫ biprod.fst : by rw this ... = 0 ≫ biprod.lift f (-g) ≫ biprod.fst : by rw category.assoc ... = 0 : zero_comp end lemma mono_inr_of_is_colimit [mono f] {s : pushout_cocone f g} (hs : is_colimit s) : mono s.inr := begin convert mono_of_mono_fac (is_colimit.comp_cocone_point_unique_up_to_iso_hom hs (colimit.is_colimit _) _), { refl }, { exact abelian.mono_pushout_of_mono_f _ _ } end lemma mono_inl_of_is_colimit [mono g] {s : pushout_cocone f g} (hs : is_colimit s) : mono s.inl := begin convert mono_of_mono_fac (is_colimit.comp_cocone_point_unique_up_to_iso_hom hs (colimit.is_colimit _) _), { refl }, { exact abelian.mono_pushout_of_mono_g _ _ } end /-- Suppose `f` and `g` are two morphisms with a common domain and suppose we have written `g` as an epimorphism followed by a monomorphism. If `f` factors through the epi part of this factorization, then any pushout of `g` along `f` is a monomorphism. -/ lemma mono_inl_of_factor_thru_epi_mono_factorization (f : X ⟶ Y) (g : X ⟶ Z) (g₁ : X ⟶ W) [epi g₁] (g₂ : W ⟶ Z) [mono g₂] (hg : g₁ ≫ g₂ = g) (f' : W ⟶ Y) (hf : g₁ ≫ f' = f) (t : pushout_cocone f g) (ht : is_colimit t) : mono t.inl := by apply mono_inl_of_is_colimit _ _ (pushout_cocone.is_colimit_of_factors _ _ _ _ _ hf hg t ht) end mono_pushout end category_theory.abelian namespace category_theory.non_preadditive_abelian variables (C : Type u) [category.{v} C] [non_preadditive_abelian C] /-- Every non_preadditive_abelian category can be promoted to an abelian category. -/ def abelian : abelian C := { has_finite_products := by apply_instance, /- We need the `convert`s here because the instances we have are slightly different from the instances we need: `has_kernels` depends on an instance of `has_zero_morphisms`. In the case of `non_preadditive_abelian`, this instance is an explicit argument. However, in the case of `abelian`, the `has_zero_morphisms` instance is derived from `preadditive`. So we need to transform an instance of "has kernels with non_preadditive_abelian.has_zero_morphisms" to an instance of "has kernels with non_preadditive_abelian.preadditive.has_zero_morphisms". Luckily, we have a `subsingleton` instance for `has_zero_morphisms`, so `convert` can immediately close the goal it creates for the two instances of `has_zero_morphisms`, and the proof is complete. -/ has_kernels := by convert (by apply_instance : limits.has_kernels C), has_cokernels := by convert (by apply_instance : limits.has_cokernels C), normal_mono_of_mono := by { introsI, convert normal_mono_of_mono f }, normal_epi_of_epi := by { introsI, convert normal_epi_of_epi f }, ..non_preadditive_abelian.preadditive } end category_theory.non_preadditive_abelian
9e0953d361cb42553c231aeca5c47479147fcdef
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/data/mv_polynomial.lean
38b4f83b81f449ee38f3d91ddb8b0ca5af8dc13e
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
58,301
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, Johan Commelin, Mario Carneiro, Shing Tak Lam -/ import data.polynomial import data.equiv.ring import data.equiv.fin import tactic.omega /-! # Multivariate polynomials This file defines polynomial rings over a base ring (or even semiring), with variables from a general type `σ` (which could be infinite). ## Important definitions Let `R` be a commutative ring (or a semiring) and let `σ` be an arbitrary type. This file creates the type `mv_polynomial σ R`, which mathematicians might denote `R[X_i : i ∈ σ]`. It is the type of multivariate (a.k.a. multivariable) polynomials, with variables corresponding to the terms in `σ`, and coefficients in `R`. ### Notation In the definitions below, we use the following notation: + `σ : Type*` (indexing the variables) + `R : Type*` `[comm_semiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s` + `a : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial σ R` ### Definitions * `mv_polynomial σ R` : the type of polynomials with variables of type `σ` and coefficients in the commutative semiring `R` * `monomial s a` : the monomial which mathematically would be denoted `a * X^s` * `C a` : the constant polynomial with value `a` * `X i` : the degree one monomial corresponding to i; mathematically this might be denoted `Xᵢ`. * `coeff s p` : the coefficient of `s` in `p`. * `eval₂ (f : R → S) (g : σ → S) p` : given a semiring homomorphism from `R` to another semiring `S`, and a map `σ → S`, evaluates `p` at this valuation, returning a term of type `S`. Note that `eval₂` can be made using `eval` and `map` (see below), and it has been suggested that sticking to `eval` and `map` might make the code less brittle. * `eval (g : σ → R) p` : given a map `σ → R`, evaluates `p` at this valuation, returning a term of type `R` * `map (f : R → S) p` : returns the multivariate polynomial obtained from `p` by the change of coefficient semiring corresponding to `f` * `degrees p` : the multiset of variables representing the union of the multisets corresponding to each non-zero monomial in `p`. For example if `7 ≠ 0` in `R` and `p = x²y+7y³` then `degrees p = {x, x, y, y, y}` * `vars p` : the finset of variables occurring in `p`. For example if `p = x⁴y+yz` then `vars p = {x, y, z}` * `degree_of n p : ℕ` -- the total degree of `p` with respect to the variable `n`. For example if `p = x⁴y+yz` then `degree_of y p = 1`. * `total_degree p : ℕ` -- the max of the sizes of the multisets `s` whose monomials `X^s` occur in `p`. For example if `p = x⁴y+yz` then `total_degree p = 5`. * `pderivative i p` : the partial derivative of `p` with respect to `i`. ## Implementation notes Recall that if `Y` has a zero, then `X →₀ Y` is the type of functions from `X` to `Y` with finite support, i.e. such that only finitely many elements of `X` get sent to non-zero terms in `Y`. The definition of `mv_polynomial σ α` is `(σ →₀ ℕ) →₀ α` ; here `σ →₀ ℕ` denotes the space of all monomials in the variables, and the function to `α` sends a monomial to its coefficient in the polynomial being represented. ## Tags polynomial, multivariate polynomial, multivariable polynomial -/ noncomputable theory local attribute [instance, priority 100] classical.prop_decidable open set function finsupp add_monoid_algebra universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} /-- Multivariate polynomial, where `σ` is the index set of the variables and `α` is the coefficient ring -/ def mv_polynomial (σ : Type*) (α : Type*) [comm_semiring α] := add_monoid_algebra α (σ →₀ ℕ) namespace mv_polynomial variables {σ : Type*} {a a' a₁ a₂ : α} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section comm_semiring variables [comm_semiring α] {p q : mv_polynomial σ α} instance decidable_eq_mv_polynomial [decidable_eq σ] [decidable_eq α] : decidable_eq (mv_polynomial σ α) := finsupp.decidable_eq instance : comm_semiring (mv_polynomial σ α) := add_monoid_algebra.comm_semiring instance : inhabited (mv_polynomial σ α) := ⟨0⟩ /-- the coercion turning an `mv_polynomial` into the function which reports the coefficient of a given monomial -/ def coeff_coe_to_fun : has_coe_to_fun (mv_polynomial σ α) := finsupp.has_coe_to_fun local attribute [instance] coeff_coe_to_fun /-- `monomial s a` is the monomial `a * X^s` -/ def monomial (s : σ →₀ ℕ) (a : α) : mv_polynomial σ α := single s a /-- `C a` is the constant polynomial with value `a` -/ def C (a : α) : mv_polynomial σ α := monomial 0 a /-- `X n` is the degree `1` monomial `1*n` -/ def X (n : σ) : mv_polynomial σ α := monomial (single n 1) 1 @[simp] lemma C_0 : C 0 = (0 : mv_polynomial σ α) := by simp [C, monomial]; refl @[simp] lemma C_1 : C 1 = (1 : mv_polynomial σ α) := rfl lemma C_mul_monomial : C a * monomial s a' = monomial s (a * a') := by simp [C, monomial, single_mul_single] @[simp] lemma C_add : (C (a + a') : mv_polynomial σ α) = C a + C a' := single_add @[simp] lemma C_mul : (C (a * a') : mv_polynomial σ α) = C a * C a' := C_mul_monomial.symm @[simp] lemma C_pow (a : α) (n : ℕ) : (C (a^n) : mv_polynomial σ α) = (C a)^n := by induction n; simp [pow_succ, *] instance : is_semiring_hom (C : α → mv_polynomial σ α) := { map_zero := C_0, map_one := C_1, map_add := λ a a', C_add, map_mul := λ a a', C_mul } lemma C_eq_coe_nat (n : ℕ) : (C ↑n : mv_polynomial σ α) = n := by induction n; simp [nat.succ_eq_add_one, *] lemma X_pow_eq_single : X n ^ e = monomial (single n e) (1 : α) := begin induction e, { simp [X], refl }, { simp [pow_succ, e_ih], simp [X, monomial, single_mul_single, nat.succ_eq_add_one, add_comm] } end lemma monomial_add_single : monomial (s + single n e) a = (monomial s a * X n ^ e) := by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp lemma monomial_single_add : monomial (single n e + s) a = (X n ^ e * monomial s a) := by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp lemma single_eq_C_mul_X {s : σ} {a : α} {n : ℕ} : monomial (single s n) a = C a * (X s)^n := by rw [← zero_add (single s n), monomial_add_single, C] @[simp] lemma monomial_add {s : σ →₀ ℕ} {a b : α} : monomial s a + monomial s b = monomial s (a + b) := by simp [monomial] @[simp] lemma monomial_mul {s s' : σ →₀ ℕ} {a b : α} : monomial s a * monomial s' b = monomial (s + s') (a * b) := by rw [monomial, monomial, monomial, add_monoid_algebra.single_mul_single] @[simp] lemma monomial_zero {s : σ →₀ ℕ}: monomial s (0 : α) = 0 := by rw [monomial, single_zero]; refl @[simp] lemma sum_monomial {A : Type*} [add_comm_monoid A] {u : σ →₀ ℕ} {r : α} {b : (σ →₀ ℕ) → α → A} (w : b u 0 = 0) : sum (monomial u r) b = b u r := sum_single_index w lemma monomial_eq : monomial s a = C a * (s.prod $ λn e, X n ^ e : mv_polynomial σ α) := begin apply @finsupp.induction σ ℕ _ _ s, { simp only [C, prod_zero_index]; exact (mul_one _).symm }, { assume n e s hns he ih, rw [monomial_single_add, ih, prod_add_index, prod_single_index, mul_left_comm], { simp only [pow_zero], }, { intro a, simp only [pow_zero], }, { intros, rw pow_add, }, } end @[recursor 5] lemma induction_on {M : mv_polynomial σ α → Prop} (p : mv_polynomial σ α) (h_C : ∀a, M (C a)) (h_add : ∀p q, M p → M q → M (p + q)) (h_X : ∀p n, M p → M (p * X n)) : M p := have ∀s a, M (monomial s a), begin assume s a, apply @finsupp.induction σ ℕ _ _ s, { show M (monomial 0 a), from h_C a, }, { assume n e p hpn he ih, have : ∀e:ℕ, M (monomial p a * X n ^ e), { intro e, induction e, { simp [ih] }, { simp [ih, pow_succ', (mul_assoc _ _ _).symm, h_X, e_ih] } }, simp [add_comm, monomial_add_single, this] } end, finsupp.induction p (by have : M (C 0) := h_C 0; rwa [C_0] at this) (assume s a p hsp ha hp, h_add _ _ (this s a) hp) theorem induction_on' {P : mv_polynomial σ α → Prop} (p : mv_polynomial σ α) (h1 : ∀ (u : σ →₀ ℕ) (a : α), P (monomial u a)) (h2 : ∀ (p q : mv_polynomial σ α), P p → P q → P (p + q)) : P p := finsupp.induction p (suffices P (monomial 0 0), by rwa monomial_zero at this, show P (monomial 0 0), from h1 0 0) (λ a b f ha hb hPf, h2 _ _ (h1 _ _) hPf) lemma hom_eq_hom [semiring γ] (f g : mv_polynomial σ α → γ) (hf : is_semiring_hom f) (hg : is_semiring_hom g) (hC : ∀a:α, f (C a) = g (C a)) (hX : ∀n:σ, f (X n) = g (X n)) (p : mv_polynomial σ α) : f p = g p := mv_polynomial.induction_on p hC begin assume p q hp hq, rw [is_semiring_hom.map_add f, is_semiring_hom.map_add g, hp, hq] end begin assume p n hp, rw [is_semiring_hom.map_mul f, is_semiring_hom.map_mul g, hp, hX] end lemma is_id (f : mv_polynomial σ α → mv_polynomial σ α) (hf : is_semiring_hom f) (hC : ∀a:α, f (C a) = (C a)) (hX : ∀n:σ, f (X n) = (X n)) (p : mv_polynomial σ α) : f p = p := hom_eq_hom f id hf is_semiring_hom.id hC hX p section coeff section -- While setting up `coeff`, we make `mv_polynomial` reducible so we can treat it as a function. local attribute [reducible] mv_polynomial /-- The coefficient of the monomial `m` in the multi-variable polynomial `p`. -/ def coeff (m : σ →₀ ℕ) (p : mv_polynomial σ α) : α := p m end lemma ext (p q : mv_polynomial σ α) : (∀ m, coeff m p = coeff m q) → p = q := ext lemma ext_iff (p q : mv_polynomial σ α) : (∀ m, coeff m p = coeff m q) ↔ p = q := ⟨ext p q, λ h m, by rw h⟩ @[simp] lemma coeff_add (m : σ →₀ ℕ) (p q : mv_polynomial σ α) : coeff m (p + q) = coeff m p + coeff m q := add_apply @[simp] lemma coeff_zero (m : σ →₀ ℕ) : coeff m (0 : mv_polynomial σ α) = 0 := rfl @[simp] lemma coeff_zero_X (i : σ) : coeff 0 (X i : mv_polynomial σ α) = 0 := single_eq_of_ne (λ h, by cases single_eq_zero.1 h) instance coeff.is_add_monoid_hom (m : σ →₀ ℕ) : is_add_monoid_hom (coeff m : mv_polynomial σ α → α) := { map_add := coeff_add m, map_zero := coeff_zero m } lemma coeff_sum {X : Type*} (s : finset X) (f : X → mv_polynomial σ α) (m : σ →₀ ℕ) : coeff m (s.sum f) = s.sum (λ x, coeff m (f x)) := (s.sum_hom _).symm lemma monic_monomial_eq (m) : monomial m (1:α) = (m.prod $ λn e, X n ^ e : mv_polynomial σ α) := by simp [monomial_eq] @[simp] lemma coeff_monomial (m n) (a) : coeff m (monomial n a : mv_polynomial σ α) = if n = m then a else 0 := by convert single_apply @[simp] lemma coeff_C (m) (a) : coeff m (C a : mv_polynomial σ α) = if 0 = m then a else 0 := by convert single_apply lemma coeff_X_pow (i : σ) (m) (k : ℕ) : coeff m (X i ^ k : mv_polynomial σ α) = if single i k = m then 1 else 0 := begin have := coeff_monomial m (finsupp.single i k) (1:α), rwa [@monomial_eq _ _ (1:α) (finsupp.single i k) _, C_1, one_mul, finsupp.prod_single_index] at this, exact pow_zero _ end lemma coeff_X' (i : σ) (m) : coeff m (X i : mv_polynomial σ α) = if single i 1 = m then 1 else 0 := by rw [← coeff_X_pow, pow_one] @[simp] lemma coeff_X (i : σ) : coeff (single i 1) (X i : mv_polynomial σ α) = 1 := by rw [coeff_X', if_pos rfl] @[simp] lemma coeff_C_mul (m) (a : α) (p : mv_polynomial σ α) : coeff m (C a * p) = a * coeff m p := begin rw [mul_def, C, monomial], rw sum_single_index, { simp only [zero_add], convert sum_apply, simp only [single_apply, finsupp.sum], rw finset.sum_eq_single m, { rw if_pos rfl, refl }, { intros m' hm' H, apply if_neg, exact H }, { intros hm, rw if_pos rfl, rw not_mem_support_iff at hm, simp [hm] } }, simp only [zero_mul, single_zero, zero_add], exact sum_zero, -- TODO doesn't work if we put this inside the simp end lemma coeff_mul (p q : mv_polynomial σ α) (n : σ →₀ ℕ) : coeff n (p * q) = finset.sum (antidiagonal n).support (λ x, coeff x.1 p * coeff x.2 q) := begin rw mul_def, have := @finset.sum_sigma (σ →₀ ℕ) α _ _ p.support (λ _, q.support) (λ x, if (x.1 + x.2 = n) then coeff x.1 p * coeff x.2 q else 0), convert this.symm using 1; clear this, { rw [coeff], repeat {rw sum_apply, apply finset.sum_congr rfl, intros, dsimp only}, convert single_apply }, { have : (antidiagonal n).support.filter (λ x, x.1 ∈ p.support ∧ x.2 ∈ q.support) ⊆ (antidiagonal n).support := finset.filter_subset _, rw [← finset.sum_sdiff this, finset.sum_eq_zero, zero_add], swap, { intros x hx, rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter), not_and, not_and, not_mem_support_iff] at hx, by_cases H : x.1 ∈ p.support, { rw [coeff, coeff, hx.2 hx.1 H, mul_zero] }, { rw not_mem_support_iff at H, rw [coeff, H, zero_mul] } }, symmetry, rw [← finset.sum_sdiff (finset.filter_subset _), finset.sum_eq_zero, zero_add], swap, { intros x hx, rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter), not_and] at hx, simp only [if_neg (hx.2 hx.1)] }, { apply finset.sum_bij, swap 5, { intros x hx, exact (x.1, x.2) }, { intros x hx, rw [finset.mem_filter, finset.mem_sigma] at hx, simpa [finset.mem_filter, mem_antidiagonal_support] using hx.symm }, { intros x hx, rw finset.mem_filter at hx, simp only [if_pos hx.2], }, { rintros ⟨i,j⟩ ⟨k,l⟩ hij hkl, simpa using and.intro }, { rintros ⟨i,j⟩ hij, refine ⟨⟨i,j⟩, _, _⟩, { apply_instance }, { rw [finset.mem_filter, mem_antidiagonal_support] at hij, simpa [finset.mem_filter, finset.mem_sigma] using hij.symm }, { refl } } }, all_goals { apply_instance } } end @[simp] lemma coeff_mul_X (m) (s : σ) (p : mv_polynomial σ α) : coeff (m + single s 1) (p * X s) = coeff m p := begin have : (m, single s 1) ∈ (m + single s 1).antidiagonal.support := mem_antidiagonal_support.2 rfl, rw [coeff_mul, ← finset.insert_erase this, finset.sum_insert (finset.not_mem_erase _ _), finset.sum_eq_zero, add_zero, coeff_X, mul_one], rintros ⟨i,j⟩ hij, rw [finset.mem_erase, mem_antidiagonal_support] at hij, by_cases H : single s 1 = j, { subst j, simpa using hij }, { rw [coeff_X', if_neg H, mul_zero] }, end lemma coeff_mul_X' (m) (s : σ) (p : mv_polynomial σ α) : coeff m (p * X s) = if s ∈ m.support then coeff (m - single s 1) p else 0 := begin split_ifs with h h, { conv_rhs {rw ← coeff_mul_X _ s}, congr' 1, ext t, by_cases hj : s = t, { subst t, simp only [nat_sub_apply, add_apply, single_eq_same], refine (nat.sub_add_cancel $ nat.pos_of_ne_zero _).symm, rwa mem_support_iff at h }, { simp [single_eq_of_ne hj] } }, { delta coeff, rw ← not_mem_support_iff, intro hm, apply h, have H := support_mul _ _ hm, simp only [finset.mem_bind] at H, rcases H with ⟨j, hj, i', hi', H⟩, delta X monomial at hi', rw mem_support_single at hi', cases hi', subst i', erw finset.mem_singleton at H, subst m, rw [mem_support_iff, add_apply, single_apply, if_pos rfl], intro H, rw [_root_.add_eq_zero_iff] at H, exact one_ne_zero H.2 } end end coeff section as_sum @[simp] lemma support_sum_monomial_coeff (p : mv_polynomial σ α) : p.support.sum (λ v, monomial v (coeff v p)) = p := finsupp.sum_single p lemma as_sum (p : mv_polynomial σ α) : p = p.support.sum (λ v, monomial v (coeff v p)) := (support_sum_monomial_coeff p).symm end as_sum section eval₂ variables [comm_semiring β] variables (f : α → β) (g : σ → β) /-- Evaluate a polynomial `p` given a valuation `g` of all the variables and a ring hom `f` from the scalar ring to the target -/ def eval₂ (p : mv_polynomial σ α) : β := p.sum (λs a, f a * s.prod (λn e, g n ^ e)) @[simp] lemma eval₂_zero : (0 : mv_polynomial σ α).eval₂ f g = 0 := finsupp.sum_zero_index section variables [is_semiring_hom f] @[simp] lemma eval₂_add : (p + q).eval₂ f g = p.eval₂ f g + q.eval₂ f g := finsupp.sum_add_index (by simp [is_semiring_hom.map_zero f]) (by simp [add_mul, is_semiring_hom.map_add f]) @[simp] lemma eval₂_monomial : (monomial s a).eval₂ f g = f a * s.prod (λn e, g n ^ e) := finsupp.sum_single_index (by simp [is_semiring_hom.map_zero f]) @[simp] lemma eval₂_C (a) : (C a).eval₂ f g = f a := by simp [eval₂_monomial, C, prod_zero_index] @[simp] lemma eval₂_one : (1 : mv_polynomial σ α).eval₂ f g = 1 := (eval₂_C _ _ _).trans (is_semiring_hom.map_one f) @[simp] lemma eval₂_X (n) : (X n).eval₂ f g = g n := by simp [eval₂_monomial, is_semiring_hom.map_one f, X, prod_single_index, pow_one] lemma eval₂_mul_monomial : ∀{s a}, (p * monomial s a).eval₂ f g = p.eval₂ f g * f a * s.prod (λn e, g n ^ e) := begin apply mv_polynomial.induction_on p, { assume a' s a, simp [C_mul_monomial, eval₂_monomial, is_semiring_hom.map_mul f] }, { assume p q ih_p ih_q, simp [add_mul, eval₂_add, ih_p, ih_q] }, { assume p n ih s a, from calc (p * X n * monomial s a).eval₂ f g = (p * monomial (single n 1 + s) a).eval₂ f g : by simp [monomial_single_add, -add_comm, pow_one, mul_assoc] ... = (p * monomial (single n 1) 1).eval₂ f g * f a * s.prod (λn e, g n ^ e) : by simp [ih, prod_single_index, prod_add_index, pow_one, pow_add, mul_assoc, mul_left_comm, is_semiring_hom.map_one f, -add_comm] } end @[simp] lemma eval₂_mul : ∀{p}, (p * q).eval₂ f g = p.eval₂ f g * q.eval₂ f g := begin apply mv_polynomial.induction_on q, { simp [C, eval₂_monomial, eval₂_mul_monomial, prod_zero_index] }, { simp [mul_add, eval₂_add] {contextual := tt} }, { simp [X, eval₂_monomial, eval₂_mul_monomial, (mul_assoc _ _ _).symm] { contextual := tt} } end @[simp] lemma eval₂_pow {p:mv_polynomial σ α} : ∀{n:ℕ}, (p ^ n).eval₂ f g = (p.eval₂ f g)^n | 0 := eval₂_one _ _ | (n + 1) := by rw [pow_add, pow_one, pow_add, pow_one, eval₂_mul, eval₂_pow] instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f g) := { map_zero := eval₂_zero _ _, map_one := eval₂_one _ _, map_add := λ p q, eval₂_add _ _, map_mul := λ p q, eval₂_mul _ _ } /-- `mv_polynomial.eval₂` as a `ring_hom`. -/ def eval₂_hom (f : α →+* β) (g : σ → β) : mv_polynomial σ α →+* β := ring_hom.of (eval₂ f g) @[simp] lemma coe_eval₂_hom (f : α →+* β) (g : σ → β) : ⇑(eval₂_hom f g) = eval₂ f g := rfl end section local attribute [instance, priority 10] is_semiring_hom.comp lemma eval₂_comp_left {γ} [comm_semiring γ] (k : β → γ) [is_semiring_hom k] (f : α → β) [is_semiring_hom f] (g : σ → β) (p) : k (eval₂ f g p) = eval₂ (k ∘ f) (k ∘ g) p := by apply mv_polynomial.induction_on p; simp [ eval₂_add, is_semiring_hom.map_add k, eval₂_mul, is_semiring_hom.map_mul k] {contextual := tt} end @[simp] lemma eval₂_eta (p : mv_polynomial σ α) : eval₂ C X p = p := by apply mv_polynomial.induction_on p; simp [eval₂_add, eval₂_mul] {contextual := tt} lemma eval₂_congr (g₁ g₂ : σ → β) (h : ∀ {i : σ} {c : σ →₀ ℕ}, i ∈ c.support → coeff c p ≠ 0 → g₁ i = g₂ i) : p.eval₂ f g₁ = p.eval₂ f g₂ := begin apply finset.sum_congr rfl, intros c hc, dsimp, congr' 1, apply finset.prod_congr rfl, intros i hi, dsimp, congr' 1, apply h hi, rwa finsupp.mem_support_iff at hc end variables [is_semiring_hom f] @[simp] lemma eval₂_prod (s : finset γ) (p : γ → mv_polynomial σ α) : eval₂ f g (s.prod p) = s.prod (λ x, eval₂ f g $ p x) := (s.prod_hom _).symm @[simp] lemma eval₂_sum (s : finset γ) (p : γ → mv_polynomial σ α) : eval₂ f g (s.sum p) = s.sum (λ x, eval₂ f g $ p x) := (s.sum_hom _).symm attribute [to_additive] eval₂_prod lemma eval₂_assoc (q : γ → mv_polynomial σ α) (p : mv_polynomial γ α) : eval₂ f (λ t, eval₂ f g (q t)) p = eval₂ f g (eval₂ C q p) := by { rw eval₂_comp_left (eval₂ f g), congr, funext, simp } end eval₂ section eval variables {f : σ → α} /-- Evaluate a polynomial `p` given a valuation `f` of all the variables -/ def eval (f : σ → α) : mv_polynomial σ α → α := eval₂ id f @[simp] lemma eval_zero : (0 : mv_polynomial σ α).eval f = 0 := eval₂_zero _ _ @[simp] lemma eval_one : (1 : mv_polynomial σ α).eval f = 1 := eval₂_one _ _ @[simp] lemma eval_add : (p + q).eval f = p.eval f + q.eval f := eval₂_add _ _ lemma eval_monomial : (monomial s a).eval f = a * s.prod (λn e, f n ^ e) := eval₂_monomial _ _ @[simp] lemma eval_C : ∀ a, (C a).eval f = a := eval₂_C _ _ @[simp] lemma eval_X : ∀ n, (X n).eval f = f n := eval₂_X _ _ @[simp] lemma eval_mul : (p * q).eval f = p.eval f * q.eval f := eval₂_mul _ _ @[simp] lemma eval_pow (n : ℕ) : (p ^ n).eval f = (p.eval f) ^ n := eval₂_pow _ _ instance eval.is_semiring_hom : is_semiring_hom (eval f) := eval₂.is_semiring_hom _ _ theorem eval_assoc {τ} (f : σ → mv_polynomial τ α) (g : τ → α) (p : mv_polynomial σ α) : p.eval (eval g ∘ f) = (eval₂ C f p).eval g := begin rw eval₂_comp_left (eval g), unfold eval, congr; funext a; simp end end eval section map variables [comm_semiring β] variables (f : α → β) /-- `map f p` maps a polynomial `p` across a ring hom `f` -/ def map : mv_polynomial σ α → mv_polynomial σ β := eval₂ (C ∘ f) X variables [is_semiring_hom f] instance is_semiring_hom_C_f : is_semiring_hom ((C : β → mv_polynomial σ β) ∘ f) := is_semiring_hom.comp _ _ @[simp] theorem map_monomial (s : σ →₀ ℕ) (a : α) : map f (monomial s a) = monomial s (f a) := (eval₂_monomial _ _).trans monomial_eq.symm @[simp] theorem map_C : ∀ (a : α), map f (C a : mv_polynomial σ α) = C (f a) := map_monomial _ _ @[simp] theorem map_X : ∀ (n : σ), map f (X n : mv_polynomial σ α) = X n := eval₂_X _ _ @[simp] theorem map_one : map f (1 : mv_polynomial σ α) = 1 := eval₂_one _ _ @[simp] theorem map_add (p q : mv_polynomial σ α) : map f (p + q) = map f p + map f q := eval₂_add _ _ @[simp] theorem map_mul (p q : mv_polynomial σ α) : map f (p * q) = map f p * map f q := eval₂_mul _ _ @[simp] lemma map_pow (p : mv_polynomial σ α) (n : ℕ) : map f (p^n) = (map f p)^n := eval₂_pow _ _ instance map.is_semiring_hom : is_semiring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) := eval₂.is_semiring_hom _ _ theorem map_id : ∀ (p : mv_polynomial σ α), map id p = p := eval₂_eta theorem map_map [comm_semiring γ] (g : β → γ) [is_semiring_hom g] (p : mv_polynomial σ α) : map g (map f p) = map (g ∘ f) p := (eval₂_comp_left (map g) (C ∘ f) X p).trans $ by congr; funext a; simp theorem eval₂_eq_eval_map (g : σ → β) (p : mv_polynomial σ α) : p.eval₂ f g = (map f p).eval g := begin unfold map eval, rw eval₂_comp_left (eval₂ id g), congr; funext a; simp end lemma eval₂_comp_right {γ} [comm_semiring γ] (k : β → γ) [is_semiring_hom k] (f : α → β) [is_semiring_hom f] (g : σ → β) (p) : k (eval₂ f g p) = eval₂ k (k ∘ g) (map f p) := begin apply mv_polynomial.induction_on p, { intro r, rw [eval₂_C, map_C, eval₂_C] }, { intros p q hp hq, rw [eval₂_add, is_semiring_hom.map_add k, map_add, eval₂_add, hp, hq] }, { intros p s hp, rw [eval₂_mul, is_semiring_hom.map_mul k, map_mul, eval₂_mul, map_X, hp, eval₂_X, eval₂_X] } end lemma map_eval₂ (f : α → β) [is_semiring_hom f] (g : γ → mv_polynomial δ α) (p : mv_polynomial γ α) : map f (eval₂ C g p) = eval₂ C (map f ∘ g) (map f p) := begin apply mv_polynomial.induction_on p, { intro r, rw [eval₂_C, map_C, map_C, eval₂_C] }, { intros p q hp hq, rw [eval₂_add, map_add, hp, hq, map_add, eval₂_add] }, { intros p s hp, rw [eval₂_mul, map_mul, hp, map_mul, map_X, eval₂_mul, eval₂_X, eval₂_X] } end lemma coeff_map (p : mv_polynomial σ α) : ∀ (m : σ →₀ ℕ), coeff m (p.map f) = f (coeff m p) := begin apply mv_polynomial.induction_on p; clear p, { intros r m, rw [map_C], simp only [coeff_C], split_ifs, {refl}, rw is_semiring_hom.map_zero f }, { intros p q hp hq m, simp only [hp, hq, map_add, coeff_add], rw is_semiring_hom.map_add f }, { intros p i hp m, simp only [hp, map_mul, map_X], simp only [hp, mem_support_iff, coeff_mul_X'], split_ifs, {refl}, rw is_semiring_hom.map_zero f } end lemma map_injective (hf : function.injective f) : function.injective (map f : mv_polynomial σ α → mv_polynomial σ β) := λ p q h, ext _ _ $ λ m, hf $ begin rw ← ext_iff at h, specialize h m, rw [coeff_map, coeff_map] at h, exact h end end map section degrees section comm_semiring /-- The maximal degrees of each variable in a multi-variable polynomial, expressed as a multiset. (For example, `degrees (x^2 * y + y^3)` would be `{x, x, y, y, y}`.) -/ def degrees (p : mv_polynomial σ α) : multiset σ := p.support.sup (λs:σ →₀ ℕ, s.to_multiset) lemma degrees_monomial (s : σ →₀ ℕ) (a : α) : degrees (monomial s a) ≤ s.to_multiset := finset.sup_le $ assume t h, begin have := finsupp.support_single_subset h, rw [finset.singleton_eq_singleton, finset.mem_singleton] at this, rw this end lemma degrees_monomial_eq (s : σ →₀ ℕ) (a : α) (ha : a ≠ 0) : degrees (monomial s a) = s.to_multiset := le_antisymm (degrees_monomial s a) $ finset.le_sup $ by rw [monomial, finsupp.support_single_ne_zero ha, finset.singleton_eq_singleton, finset.mem_singleton] lemma degrees_C (a : α) : degrees (C a : mv_polynomial σ α) = 0 := multiset.le_zero.1 $ degrees_monomial _ _ lemma degrees_X (n : σ) : degrees (X n : mv_polynomial σ α) ≤ {n} := le_trans (degrees_monomial _ _) $ le_of_eq $ to_multiset_single _ _ lemma degrees_zero : degrees (0 : mv_polynomial σ α) = 0 := by { rw ← C_0, exact degrees_C 0 } lemma degrees_one : degrees (1 : mv_polynomial σ α) = 0 := degrees_C 1 lemma degrees_add (p q : mv_polynomial σ α) : (p + q).degrees ≤ p.degrees ⊔ q.degrees := begin refine finset.sup_le (assume b hb, _), have := finsupp.support_add hb, rw finset.mem_union at this, cases this, { exact le_sup_left_of_le (finset.le_sup this) }, { exact le_sup_right_of_le (finset.le_sup this) }, end lemma degrees_sum {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) : (s.sum f).degrees ≤ s.sup (λi, (f i).degrees) := begin refine s.induction _ _, { simp only [finset.sum_empty, finset.sup_empty, degrees_zero], exact le_refl _ }, { assume i s his ih, rw [finset.sup_insert, finset.sum_insert his], exact le_trans (degrees_add _ _) (sup_le_sup_left ih _) } end lemma degrees_mul (p q : mv_polynomial σ α) : (p * q).degrees ≤ p.degrees + q.degrees := begin refine finset.sup_le (assume b hb, _), have := support_mul p q hb, simp only [finset.mem_bind, finset.singleton_eq_singleton, finset.mem_singleton] at this, rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩, rw [finsupp.to_multiset_add], exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) end lemma degrees_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) : (s.prod f).degrees ≤ s.sum (λi, (f i).degrees) := begin refine s.induction _ _, { simp only [finset.prod_empty, finset.sum_empty, degrees_one] }, { assume i s his ih, rw [finset.prod_insert his, finset.sum_insert his], exact le_trans (degrees_mul _ _) (add_le_add_left ih _) } end lemma degrees_pow (p : mv_polynomial σ α) : ∀(n : ℕ), (p^n).degrees ≤ add_monoid.smul n p.degrees | 0 := begin rw [pow_zero, degrees_one], exact multiset.zero_le _ end | (n + 1) := le_trans (degrees_mul _ _) (add_le_add_left (degrees_pow n) _) end comm_semiring end degrees section vars /-- `vars p` is the set of variables appearing in the polynomial `p` -/ def vars (p : mv_polynomial σ α) : finset σ := p.degrees.to_finset @[simp] lemma vars_0 : (0 : mv_polynomial σ α).vars = ∅ := by rw [vars, degrees_zero, multiset.to_finset_zero] @[simp] lemma vars_monomial (h : a ≠ 0) : (monomial s a).vars = s.support := by rw [vars, degrees_monomial_eq _ _ h, finsupp.to_finset_to_multiset] @[simp] lemma vars_C : (C a : mv_polynomial σ α).vars = ∅ := by rw [vars, degrees_C, multiset.to_finset_zero] @[simp] lemma vars_X (h : 0 ≠ (1 : α)) : (X n : mv_polynomial σ α).vars = {n} := by rw [X, vars_monomial h.symm, finsupp.support_single_ne_zero zero_ne_one.symm] lemma mem_support_not_mem_vars_zero {f : mv_polynomial σ α} {x : σ →₀ ℕ} (H : x ∈ f.support) {v : σ} (h : v ∉ vars f) : x v = 0 := begin rw [vars, multiset.mem_to_finset] at h, rw ←not_mem_support_iff, contrapose! h, unfold degrees, rw (show f.support = insert x f.support, from eq.symm $ finset.insert_eq_of_mem H), rw finset.sup_insert, simp only [multiset.mem_union, multiset.sup_eq_union], left, rwa [←to_finset_to_multiset, multiset.mem_to_finset] at h, end end vars section degree_of /-- `degree_of n p` gives the highest power of X_n that appears in `p` -/ def degree_of (n : σ) (p : mv_polynomial σ α) : ℕ := p.degrees.count n end degree_of section total_degree /-- `total_degree p` gives the maximum |s| over the monomials X^s in `p` -/ def total_degree (p : mv_polynomial σ α) : ℕ := p.support.sup (λs, s.sum $ λn e, e) lemma total_degree_eq (p : mv_polynomial σ α) : p.total_degree = p.support.sup (λm, m.to_multiset.card) := begin rw [total_degree], congr, funext m, exact (finsupp.card_to_multiset _).symm end lemma total_degree_le_degrees_card (p : mv_polynomial σ α) : p.total_degree ≤ p.degrees.card := begin rw [total_degree_eq], exact finset.sup_le (assume s hs, multiset.card_le_of_le $ finset.le_sup hs) end @[simp] lemma total_degree_C (a : α) : (C a : mv_polynomial σ α).total_degree = 0 := nat.eq_zero_of_le_zero $ finset.sup_le $ assume n hn, have _ := finsupp.support_single_subset hn, begin rw [finset.singleton_eq_singleton, finset.mem_singleton] at this, subst this, exact le_refl _ end @[simp] lemma total_degree_zero : (0 : mv_polynomial σ α).total_degree = 0 := by rw [← C_0]; exact total_degree_C (0 : α) @[simp] lemma total_degree_one : (1 : mv_polynomial σ α).total_degree = 0 := total_degree_C (1 : α) @[simp] lemma total_degree_X {α} [nonzero_comm_ring α] (s : σ) : (X s : mv_polynomial σ α).total_degree = 1 := begin rw [total_degree, X, monomial, finsupp.support_single_ne_zero one_ne_zero], simp only [finset.sup, sum_single_index, finset.insert_empty_eq_singleton, finset.fold_singleton, sup_bot_eq], end lemma total_degree_add (a b : mv_polynomial σ α) : (a + b).total_degree ≤ max a.total_degree b.total_degree := finset.sup_le $ assume n hn, have _ := finsupp.support_add hn, begin rw finset.mem_union at this, cases this, { exact le_max_left_of_le (finset.le_sup this) }, { exact le_max_right_of_le (finset.le_sup this) } end lemma total_degree_mul (a b : mv_polynomial σ α) : (a * b).total_degree ≤ a.total_degree + b.total_degree := finset.sup_le $ assume n hn, have _ := add_monoid_algebra.support_mul a b hn, begin simp only [finset.mem_bind, finset.mem_singleton, finset.singleton_eq_singleton] at this, rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩, rw [finsupp.sum_add_index], { exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) }, { assume a, refl }, { assume a b₁ b₂, refl } end lemma total_degree_pow (a : mv_polynomial σ α) (n : ℕ) : (a ^ n).total_degree ≤ n * a.total_degree := begin induction n with n ih, { simp only [nat.nat_zero_eq_zero, zero_mul, pow_zero, total_degree_one] }, rw pow_succ, calc total_degree (a * a ^ n) ≤ a.total_degree + (a^n).total_degree : total_degree_mul _ _ ... ≤ a.total_degree + n * a.total_degree : add_le_add_left ih _ ... = (n+1) * a.total_degree : by rw [add_mul, one_mul, add_comm] end lemma total_degree_list_prod : ∀(s : list (mv_polynomial σ α)), s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum | [] := by rw [@list.prod_nil (mv_polynomial σ α) _, total_degree_one]; refl | (p :: ps) := begin rw [@list.prod_cons (mv_polynomial σ α) _, list.map, list.sum_cons], exact le_trans (total_degree_mul _ _) (add_le_add_left (total_degree_list_prod ps) _) end lemma total_degree_multiset_prod (s : multiset (mv_polynomial σ α)) : s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum := begin refine quotient.induction_on s (assume l, _), rw [multiset.quot_mk_to_coe, multiset.coe_prod, multiset.coe_map, multiset.coe_sum], exact total_degree_list_prod l end lemma total_degree_finset_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) : (s.prod f).total_degree ≤ s.sum (λi, (f i).total_degree) := begin refine le_trans (total_degree_multiset_prod _) _, rw [multiset.map_map], refl end end total_degree end comm_semiring section comm_ring variable [comm_ring α] variables {p q : mv_polynomial σ α} instance : comm_ring (mv_polynomial σ α) := add_monoid_algebra.comm_ring instance : has_scalar α (mv_polynomial σ α) := finsupp.has_scalar instance : module α (mv_polynomial σ α) := finsupp.module (σ →₀ ℕ) α instance C.is_ring_hom : is_ring_hom (C : α → mv_polynomial σ α) := by apply is_ring_hom.of_semiring variables (σ a a') @[simp] lemma C_sub : (C (a - a') : mv_polynomial σ α) = C a - C a' := is_ring_hom.map_sub _ @[simp] lemma C_neg : (C (-a) : mv_polynomial σ α) = -C a := is_ring_hom.map_neg _ @[simp] lemma coeff_neg (m : σ →₀ ℕ) (p : mv_polynomial σ α) : coeff m (-p) = -coeff m p := finsupp.neg_apply @[simp] lemma coeff_sub (m : σ →₀ ℕ) (p q : mv_polynomial σ α) : coeff m (p - q) = coeff m p - coeff m q := finsupp.sub_apply instance coeff.is_add_group_hom (m : σ →₀ ℕ) : is_add_group_hom (coeff m : mv_polynomial σ α → α) := { map_add := coeff_add m } variables {σ} (p) theorem C_mul' : mv_polynomial.C a * p = a • p := begin apply finsupp.induction p, { exact (mul_zero $ mv_polynomial.C a).trans (@smul_zero α (mv_polynomial σ α) _ _ _ a).symm }, intros p b f haf hb0 ih, rw [mul_add, ih, @smul_add α (mv_polynomial σ α) _ _ _ a], congr' 1, rw [add_monoid_algebra.mul_def, finsupp.smul_single, mv_polynomial.C, mv_polynomial.monomial], rw [finsupp.sum_single_index, finsupp.sum_single_index, zero_add, smul_eq_mul], { rw [mul_zero, finsupp.single_zero] }, { rw finsupp.sum_single_index, all_goals { rw [zero_mul, finsupp.single_zero] }, } end lemma smul_eq_C_mul (p : mv_polynomial σ α) (a : α) : a • p = C a * p := begin rw [← finsupp.sum_single p, @finsupp.smul_sum (σ →₀ ℕ) α α, finsupp.mul_sum], refine finset.sum_congr rfl (assume n _, _), simp only [finsupp.smul_single], exact C_mul_monomial.symm end @[simp] lemma smul_eval (x) (p : mv_polynomial σ α) (s) : (s • p).eval x = s * p.eval x := by rw [smul_eq_C_mul, eval_mul, eval_C] section degrees lemma degrees_neg (p : mv_polynomial σ α) : (- p).degrees = p.degrees := by rw [degrees, finsupp.support_neg]; refl lemma degrees_sub (p q : mv_polynomial σ α) : (p - q).degrees ≤ p.degrees ⊔ q.degrees := le_trans (degrees_add p (-q)) $ by rw [degrees_neg] end degrees section eval₂ variables [comm_ring β] variables (f : α → β) [is_ring_hom f] (g : σ → β) instance eval₂.is_ring_hom : is_ring_hom (eval₂ f g) := by apply is_ring_hom.of_semiring @[simp] lemma eval₂_sub : (p - q).eval₂ f g = p.eval₂ f g - q.eval₂ f g := is_ring_hom.map_sub _ @[simp] lemma eval₂_neg : (-p).eval₂ f g = -(p.eval₂ f g) := is_ring_hom.map_neg _ lemma hom_C (f : mv_polynomial σ ℤ → β) [is_ring_hom f] (n : ℤ) : f (C n) = (n : β) := ((ring_hom.of f).comp (ring_hom.of C)).eq_int_cast n /-- A ring homomorphism f : Z[X_1, X_2, ...] → R is determined by the evaluations f(X_1), f(X_2), ... -/ @[simp] lemma eval₂_hom_X {α : Type u} (c : ℤ → β) [is_ring_hom c] (f : mv_polynomial α ℤ → β) [is_ring_hom f] (x : mv_polynomial α ℤ) : eval₂ c (f ∘ X) x = f x := mv_polynomial.induction_on x (λ n, by { rw [hom_C f, eval₂_C], exact (ring_hom.of c).eq_int_cast n }) (λ p q hp hq, by { rw [eval₂_add, hp, hq], exact (is_ring_hom.map_add f).symm }) (λ p n hp, by { rw [eval₂_mul, eval₂_X, hp], exact (is_ring_hom.map_mul f).symm }) /-- Ring homomorphisms out of integer polynomials on a type `σ` are the same as functions out of the type `σ`, -/ def hom_equiv : (mv_polynomial σ ℤ →+* β) ≃ (σ → β) := { to_fun := λ f, ⇑f ∘ X, inv_fun := λ f, eval₂_hom (int.cast_ring_hom β) f, left_inv := λ f, ring_hom.ext $ eval₂_hom_X _ _, right_inv := λ f, funext $ λ x, by simp only [coe_eval₂_hom, function.comp_app, eval₂_X] } end eval₂ section eval variables (f : σ → α) instance eval.is_ring_hom : is_ring_hom (eval f) := eval₂.is_ring_hom _ _ @[simp] lemma eval_sub : (p - q).eval f = p.eval f - q.eval f := is_ring_hom.map_sub _ @[simp] lemma eval_neg : (-p).eval f = -(p.eval f) := is_ring_hom.map_neg _ end eval section map variables [comm_ring β] variables (f : α → β) [is_ring_hom f] instance is_ring_hom_C_f : is_ring_hom ((C : β → mv_polynomial σ β) ∘ f) := is_ring_hom.comp _ _ instance map.is_ring_hom : is_ring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) := eval₂.is_ring_hom _ _ @[simp] lemma map_sub : (p - q).map f = p.map f - q.map f := is_ring_hom.map_sub _ @[simp] lemma map_neg : (-p).map f = -(p.map f) := is_ring_hom.map_neg _ end map section total_degree @[simp] lemma total_degree_neg (a : mv_polynomial σ α) : (-a).total_degree = a.total_degree := by simp only [total_degree, finsupp.support_neg] lemma total_degree_sub (a b : mv_polynomial σ α) : (a - b).total_degree ≤ max a.total_degree b.total_degree := calc (a - b).total_degree = (a + -b).total_degree : by rw sub_eq_add_neg ... ≤ max a.total_degree (-b).total_degree : total_degree_add a (-b) ... = max a.total_degree b.total_degree : by rw total_degree_neg end total_degree section aeval /-- The algebra of multivariate polynomials. -/ -- FIXME this causes a deterministic timeout with `-T50000` (but `-T60000` seems okay) instance mv_polynomial (R : Type u) [comm_ring R] (σ : Type v) : algebra R (mv_polynomial σ R) := { commutes' := λ _ _, mul_comm _ _, smul_def' := λ c p, (mv_polynomial.C_mul' c p).symm, .. ring_hom.of mv_polynomial.C, .. mv_polynomial.module } variables (R : Type u) (A : Type v) (f : σ → A) variables [comm_ring R] [comm_ring A] [algebra R A] /-- A map `σ → A` where `A` is an algebra over `R` generates an `R`-algebra homomorphism from multivariate polynomials over `σ` to `A`. -/ def aeval : mv_polynomial σ R →ₐ[R] A := { commutes' := λ r, eval₂_C _ _ _ .. eval₂_hom (algebra_map R A) f } theorem aeval_def (p : mv_polynomial σ R) : aeval R A f p = eval₂ (algebra_map R A) f p := rfl @[simp] lemma aeval_X (s : σ) : aeval R A f (X s) = f s := eval₂_X _ _ _ @[simp] lemma aeval_C (r : R) : aeval R A f (C r) = algebra_map R A r := eval₂_C _ _ _ theorem eval_unique (φ : mv_polynomial σ R →ₐ[R] A) : φ = aeval R A (φ ∘ X) := begin ext p, apply mv_polynomial.induction_on p, { intro r, rw aeval_C, exact φ.commutes r }, { intros f g ih1 ih2, rw [φ.map_add, ih1, ih2, alg_hom.map_add] }, { intros p j ih, rw [φ.map_mul, alg_hom.map_mul, aeval_X, ih] } end end aeval end comm_ring section rename variables {α} [comm_semiring α] /-- Rename all the variables in a multivariable polynomial. -/ def rename (f : β → γ) : mv_polynomial β α → mv_polynomial γ α := eval₂ C (X ∘ f) instance rename.is_semiring_hom (f : β → γ) : is_semiring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) := by unfold rename; apply_instance @[simp] lemma rename_C (f : β → γ) (a : α) : rename f (C a) = C a := eval₂_C _ _ _ @[simp] lemma rename_X (f : β → γ) (b : β) : rename f (X b : mv_polynomial β α) = X (f b) := eval₂_X _ _ _ @[simp] lemma rename_zero (f : β → γ) : rename f (0 : mv_polynomial β α) = 0 := eval₂_zero _ _ @[simp] lemma rename_one (f : β → γ) : rename f (1 : mv_polynomial β α) = 1 := eval₂_one _ _ @[simp] lemma rename_add (f : β → γ) (p q : mv_polynomial β α) : rename f (p + q) = rename f p + rename f q := eval₂_add _ _ @[simp] lemma rename_neg {α} [comm_ring α] (f : β → γ) (p : mv_polynomial β α) : rename f (-p) = -rename f p := eval₂_neg _ _ _ @[simp] lemma rename_sub {α} [comm_ring α] (f : β → γ) (p q : mv_polynomial β α) : rename f (p - q) = rename f p - rename f q := eval₂_sub _ _ _ @[simp] lemma rename_mul (f : β → γ) (p q : mv_polynomial β α) : rename f (p * q) = rename f p * rename f q := eval₂_mul _ _ @[simp] lemma rename_pow (f : β → γ) (p : mv_polynomial β α) (n : ℕ) : rename f (p^n) = (rename f p)^n := eval₂_pow _ _ lemma map_rename [comm_semiring β] (f : α → β) [is_semiring_hom f] (g : γ → δ) (p : mv_polynomial γ α) : map f (rename g p) = rename g (map f p) := mv_polynomial.induction_on p (λ a, by simp) (λ p q hp hq, by simp [hp, hq]) (λ p n hp, by simp [hp]) @[simp] lemma rename_rename (f : β → γ) (g : γ → δ) (p : mv_polynomial β α) : rename g (rename f p) = rename (g ∘ f) p := show rename g (eval₂ C (X ∘ f) p) = _, by simp only [eval₂_comp_left (rename g) C (X ∘ f) p, (∘), rename_C, rename_X]; refl @[simp] lemma rename_id (p : mv_polynomial β α) : rename id p = p := eval₂_eta p lemma rename_monomial (f : β → γ) (p : β →₀ ℕ) (a : α) : rename f (monomial p a) = monomial (p.map_domain f) a := begin rw [rename, eval₂_monomial, monomial_eq, finsupp.prod_map_domain_index], { exact assume n, pow_zero _ }, { exact assume n i₁ i₂, pow_add _ _ _ } end lemma rename_eq (f : β → γ) (p : mv_polynomial β α) : rename f p = finsupp.map_domain (finsupp.map_domain f) p := begin simp only [rename, eval₂, finsupp.map_domain], congr, ext s a : 2, rw [← monomial, monomial_eq, finsupp.prod_sum_index], congr, ext n i : 2, rw [finsupp.prod_single_index], exact pow_zero _, exact assume a, pow_zero _, exact assume a b c, pow_add _ _ _ end lemma injective_rename (f : β → γ) (hf : function.injective f) : function.injective (rename f : mv_polynomial β α → mv_polynomial γ α) := have (rename f : mv_polynomial β α → mv_polynomial γ α) = finsupp.map_domain (finsupp.map_domain f) := funext (rename_eq f), begin rw this, exact finsupp.injective_map_domain (finsupp.injective_map_domain hf) end lemma total_degree_rename_le (f : β → γ) (p : mv_polynomial β α) : (p.rename f).total_degree ≤ p.total_degree := finset.sup_le $ assume b, begin assume h, rw rename_eq at h, have h' := finsupp.map_domain_support h, rw finset.mem_image at h', rcases h' with ⟨s, hs, rfl⟩, rw finsupp.sum_map_domain_index, exact le_trans (le_refl _) (finset.le_sup hs), exact assume _, rfl, exact assume _ _ _, rfl end section variables [comm_semiring β] (f : α → β) [is_semiring_hom f] variables (k : γ → δ) (g : δ → β) (p : mv_polynomial γ α) lemma eval₂_rename : (p.rename k).eval₂ f g = p.eval₂ f (g ∘ k) := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma rename_eval₂ (g : δ → mv_polynomial γ α) : (p.eval₂ C (g ∘ k)).rename k = (p.rename k).eval₂ C (rename k ∘ g) := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma rename_prodmk_eval₂ (d : δ) (g : γ → mv_polynomial γ α) : (p.eval₂ C g).rename (prod.mk d) = p.eval₂ C (λ x, (g x).rename (prod.mk d)) := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma eval₂_rename_prodmk (g : δ × γ → β) (d : δ) : (rename (prod.mk d) p).eval₂ f g = eval₂ f (λ i, g (d, i)) p := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma eval_rename_prodmk (g : δ × γ → α) (d : δ) : (rename (prod.mk d) p).eval g = eval (λ i, g (d, i)) p := eval₂_rename_prodmk id _ _ _ end /-- Every polynomial is a polynomial in finitely many variables. -/ theorem exists_finset_rename (p : mv_polynomial γ α) : ∃ (s : finset γ) (q : mv_polynomial {x // x ∈ s} α), p = q.rename coe := begin apply induction_on p, { intro r, exact ⟨∅, C r, by rw rename_C⟩ }, { rintro p q ⟨s, p, rfl⟩ ⟨t, q, rfl⟩, refine ⟨s ∪ t, ⟨_, _⟩⟩, { exact rename (subtype.map id (finset.subset_union_left s t)) p + rename (subtype.map id (finset.subset_union_right s t)) q }, { rw [rename_add, rename_rename, rename_rename], refl } }, { rintro p n ⟨s, p, rfl⟩, refine ⟨insert n s, ⟨_, _⟩⟩, { exact rename (subtype.map id (finset.subset_insert n s)) p * X ⟨n, s.mem_insert_self n⟩ }, { rw [rename_mul, rename_X, rename_rename], refl } } end /-- Every polynomial is a polynomial in finitely many variables. -/ theorem exists_fin_rename (p : mv_polynomial γ α) : ∃ (n : ℕ) (f : fin n → γ) (hf : injective f) (q : mv_polynomial (fin n) α), p = q.rename f := begin obtain ⟨s, q, rfl⟩ := exists_finset_rename p, obtain ⟨n, ⟨e⟩⟩ := fintype.exists_equiv_fin {x // x ∈ s}, refine ⟨n, coe ∘ e.symm, injective_comp subtype.val_injective e.symm.injective, q.rename e, _⟩, rw [← rename_rename, rename_rename e], simp only [function.comp, equiv.symm_apply_apply, rename_rename] end end rename lemma eval₂_cast_comp {β : Type u} {γ : Type v} (f : γ → β) {α : Type w} [comm_ring α] (c : ℤ → α) [is_ring_hom c] (g : β → α) (x : mv_polynomial γ ℤ) : eval₂ c (g ∘ f) x = eval₂ c g (rename f x) := mv_polynomial.induction_on x (λ n, by simp only [eval₂_C, rename_C]) (λ p q hp hq, by simp only [hp, hq, rename, eval₂_add]) (λ p n hp, by simp only [hp, rename, eval₂_X, eval₂_mul]) instance rename.is_ring_hom {α} [comm_ring α] (f : β → γ) : is_ring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) := @is_ring_hom.of_semiring (mv_polynomial β α) (mv_polynomial γ α) _ _ (rename f) (rename.is_semiring_hom f) section equiv variables (α) [comm_semiring α] /-- The ring isomorphism between multivariable polynomials in no variables and the ground ring. -/ def pempty_ring_equiv : mv_polynomial pempty α ≃+* α := { to_fun := mv_polynomial.eval₂ id $ pempty.elim, inv_fun := C, left_inv := is_id _ (by apply_instance) (assume a, by rw [eval₂_C]; refl) (assume a, a.elim), right_inv := λ r, eval₂_C _ _ _, map_mul' := λ _ _, eval₂_mul _ _, map_add' := λ _ _, eval₂_add _ _ } /-- The ring isomorphism between multivariable polynomials in a single variable and polynomials over the ground ring. -/ def punit_ring_equiv : mv_polynomial punit α ≃+* polynomial α := { to_fun := eval₂ polynomial.C (λu:punit, polynomial.X), inv_fun := polynomial.eval₂ mv_polynomial.C (X punit.star), left_inv := begin refine is_id _ _ _ _, apply is_semiring_hom.comp (eval₂ polynomial.C (λu:punit, polynomial.X)) _; apply_instance, { assume a, rw [eval₂_C, polynomial.eval₂_C] }, { rintros ⟨⟩, rw [eval₂_X, polynomial.eval₂_X] } end, right_inv := assume p, polynomial.induction_on p (assume a, by rw [polynomial.eval₂_C, mv_polynomial.eval₂_C]) (assume p q hp hq, by rw [polynomial.eval₂_add, mv_polynomial.eval₂_add, hp, hq]) (assume p n hp, by rw [polynomial.eval₂_mul, polynomial.eval₂_pow, polynomial.eval₂_X, polynomial.eval₂_C, eval₂_mul, eval₂_C, eval₂_pow, eval₂_X]), map_mul' := λ _ _, eval₂_mul _ _, map_add' := λ _ _, eval₂_add _ _ } /-- The ring isomorphism between multivariable polynomials induced by an equivalence of the variables. -/ def ring_equiv_of_equiv (e : β ≃ γ) : mv_polynomial β α ≃+* mv_polynomial γ α := { to_fun := rename e, inv_fun := rename e.symm, left_inv := λ p, by simp only [rename_rename, (∘), e.symm_apply_apply]; exact rename_id p, right_inv := λ p, by simp only [rename_rename, (∘), e.apply_symm_apply]; exact rename_id p, map_mul' := rename_mul e, map_add' := rename_add e } /-- The ring isomorphism between multivariable polynomials induced by a ring isomorphism of the ground ring. -/ def ring_equiv_congr [comm_semiring γ] (e : α ≃+* γ) : mv_polynomial β α ≃+* mv_polynomial β γ := { to_fun := map e, inv_fun := map e.symm, left_inv := assume p, have (e.symm ∘ e) = id, { ext a, exact e.symm_apply_apply a }, by simp only [map_map, this, map_id], right_inv := assume p, have (e ∘ e.symm) = id, { ext a, exact e.apply_symm_apply a }, by simp only [map_map, this, map_id], map_mul' := map_mul _, map_add' := map_add _ } section variables (β γ δ) /-- The function from multivariable polynomials in a sum of two types, to multivariable polynomials in one of the types, with coefficents in multivariable polynomials in the other type. See `sum_ring_equiv` for the ring isomorphism. -/ def sum_to_iter : mv_polynomial (β ⊕ γ) α → mv_polynomial β (mv_polynomial γ α) := eval₂ (C ∘ C) (λbc, sum.rec_on bc X (C ∘ X)) instance is_semiring_hom_C_C : is_semiring_hom (C ∘ C : α → mv_polynomial β (mv_polynomial γ α)) := @is_semiring_hom.comp _ _ _ _ C mv_polynomial.is_semiring_hom _ _ C mv_polynomial.is_semiring_hom instance is_semiring_hom_sum_to_iter : is_semiring_hom (sum_to_iter α β γ) := eval₂.is_semiring_hom _ _ lemma sum_to_iter_C (a : α) : sum_to_iter α β γ (C a) = C (C a) := eval₂_C _ _ a lemma sum_to_iter_Xl (b : β) : sum_to_iter α β γ (X (sum.inl b)) = X b := eval₂_X _ _ (sum.inl b) lemma sum_to_iter_Xr (c : γ) : sum_to_iter α β γ (X (sum.inr c)) = C (X c) := eval₂_X _ _ (sum.inr c) /-- The function from multivariable polynomials in one type, with coefficents in multivariable polynomials in another type, to multivariable polynomials in the sum of the two types. See `sum_ring_equiv` for the ring isomorphism. -/ def iter_to_sum : mv_polynomial β (mv_polynomial γ α) → mv_polynomial (β ⊕ γ) α := eval₂ (eval₂ C (X ∘ sum.inr)) (X ∘ sum.inl) instance is_semiring_hom_iter_to_sum : is_semiring_hom (iter_to_sum α β γ) := eval₂.is_semiring_hom _ _ lemma iter_to_sum_C_C (a : α) : iter_to_sum α β γ (C (C a)) = C a := eq.trans (eval₂_C _ _ (C a)) (eval₂_C _ _ _) lemma iter_to_sum_X (b : β) : iter_to_sum α β γ (X b) = X (sum.inl b) := eval₂_X _ _ _ lemma iter_to_sum_C_X (c : γ) : iter_to_sum α β γ (C (X c)) = X (sum.inr c) := eq.trans (eval₂_C _ _ (X c)) (eval₂_X _ _ _) /-- A helper function for `sum_ring_equiv`. -/ def mv_polynomial_equiv_mv_polynomial [comm_semiring δ] (f : mv_polynomial β α → mv_polynomial γ δ) (hf : is_semiring_hom f) (g : mv_polynomial γ δ → mv_polynomial β α) (hg : is_semiring_hom g) (hfgC : ∀a, f (g (C a)) = C a) (hfgX : ∀n, f (g (X n)) = X n) (hgfC : ∀a, g (f (C a)) = C a) (hgfX : ∀n, g (f (X n)) = X n) : mv_polynomial β α ≃+* mv_polynomial γ δ := { to_fun := f, inv_fun := g, left_inv := is_id _ (is_semiring_hom.comp _ _) hgfC hgfX, right_inv := is_id _ (is_semiring_hom.comp _ _) hfgC hfgX, map_mul' := hf.map_mul, map_add' := hf.map_add } /-- The ring isomorphism between multivariable polynomials in a sum of two types, and multivariable polynomials in one of the types, with coefficents in multivariable polynomials in the other type. -/ def sum_ring_equiv : mv_polynomial (β ⊕ γ) α ≃+* mv_polynomial β (mv_polynomial γ α) := begin apply @mv_polynomial_equiv_mv_polynomial α (β ⊕ γ) _ _ _ _ (sum_to_iter α β γ) _ (iter_to_sum α β γ) _, { assume p, apply hom_eq_hom _ _ _ _ _ _ p, apply_instance, { apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _, apply_instance, apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _, apply_instance, { apply @mv_polynomial.is_semiring_hom }, { apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ }, { apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ } }, { apply mv_polynomial.is_semiring_hom }, { assume a, rw [iter_to_sum_C_C α β γ, sum_to_iter_C α β γ] }, { assume c, rw [iter_to_sum_C_X α β γ, sum_to_iter_Xr α β γ] } }, { assume b, rw [iter_to_sum_X α β γ, sum_to_iter_Xl α β γ] }, { assume a, rw [sum_to_iter_C α β γ, iter_to_sum_C_C α β γ] }, { assume n, cases n with b c, { rw [sum_to_iter_Xl, iter_to_sum_X] }, { rw [sum_to_iter_Xr, iter_to_sum_C_X] } }, { apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ }, { apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ } end /-- The ring isomorphism between multivariable polynomials in `option β` and polynomials with coefficients in `mv_polynomial β α`. -/ def option_equiv_left : mv_polynomial (option β) α ≃+* polynomial (mv_polynomial β α) := (ring_equiv_of_equiv α $ (equiv.option_equiv_sum_punit β).trans (equiv.sum_comm _ _)).trans $ (sum_ring_equiv α _ _).trans $ punit_ring_equiv _ /-- The ring isomorphism between multivariable polynomials in `option β` and multivariable polynomials with coefficients in polynomials. -/ def option_equiv_right : mv_polynomial (option β) α ≃+* mv_polynomial β (polynomial α) := (ring_equiv_of_equiv α $ equiv.option_equiv_sum_punit.{0} β).trans $ (sum_ring_equiv α β unit).trans $ ring_equiv_congr (mv_polynomial unit α) (punit_ring_equiv α) /-- The ring isomorphism between multivariable polynomials in `fin (n + 1)` and polynomials over multivariable polynomials in `fin n`. -/ def fin_succ_equiv (n : ℕ) : mv_polynomial (fin (n + 1)) α ≃+* polynomial (mv_polynomial (fin n) α) := (ring_equiv_of_equiv α (fin_succ_equiv n)).trans (option_equiv_left α (fin n)) end end equiv /-! ## Partial derivatives -/ section pderivative variables {R : Type} [comm_ring R] variable {S : Type} /-- `pderivative v p` is the partial derivative of `p` with respect to `v` -/ def pderivative (v : S) (p : mv_polynomial S R) : mv_polynomial S R := p.sum (λ A B, monomial (A - single v 1) (B * (A v))) @[simp] lemma pderivative_add {v : S} {f g : mv_polynomial S R} : pderivative v (f + g) = pderivative v f + pderivative v g := begin refine sum_add_index _ _, { simp }, simp [add_mul], end @[simp] lemma pderivative_monomial {v : S} {u : S →₀ ℕ} {a : R} : pderivative v (monomial u a) = monomial (u - single v 1) (a * (u v)) := by simp [pderivative] @[simp] lemma pderivative_C {v : S} {a : R} : pderivative v (C a) = 0 := suffices pderivative v (monomial 0 a) = 0, by simpa, by simp @[simp] lemma pderivative_zero {v : S} : pderivative v (0 : mv_polynomial S R) = 0 := suffices pderivative v (C 0 : mv_polynomial S R) = 0, by simpa, show pderivative v (C 0 : mv_polynomial S R) = 0, from pderivative_C section variables (R) /-- `pderivative : S → mv_polynomial S R → mv_polynomial S R` as an `add_monoid_hom` -/ def pderivative.add_monoid_hom (v : S) : mv_polynomial S R →+ mv_polynomial S R := { to_fun := pderivative v, map_zero' := pderivative_zero, map_add' := λ x y, pderivative_add, } @[simp] lemma pderivative.add_monoid_hom_apply (v : S) (p : mv_polynomial S R) : (pderivative.add_monoid_hom R v) p = pderivative v p := rfl end lemma pderivative_eq_zero_of_not_mem_vars {v : S} {f : mv_polynomial S R} (h : v ∉ f.vars) : pderivative v f = 0 := begin change (pderivative.add_monoid_hom R v) f = 0, rw [f.as_sum, add_monoid_hom.map_sum], apply finset.sum_eq_zero, intros, simp [mem_support_not_mem_vars_zero H h], end lemma pderivative_monomial_single {a : R} {v : S} {n : ℕ} : pderivative v (monomial (single v n) a) = monomial (single v (n-1)) (a * n) := by simp private lemma monomial_sub_single_one_add {v : S} {u u' : S →₀ ℕ} {r r' : R} : monomial (u - single v 1 + u') (r * (u v) * r') = monomial (u + u' - single v 1) (r * (u v) * r') := by by_cases h : u v = 0; simp [h, sub_single_one_add] private lemma monomial_add_sub_single_one {v : S} {u u' : S →₀ ℕ} {r r' : R} : monomial (u + (u' - single v 1)) (r * (r' * (u' v))) = monomial (u + u' - single v 1) (r * (r' * (u' v))) := by by_cases h : u' v = 0; simp [h, add_sub_single_one] lemma pderivative_monomial_mul {v : S} {u u' : S →₀ ℕ} {r r' : R} : pderivative v (monomial u r * monomial u' r') = pderivative v (monomial u r) * monomial u' r' + monomial u r * pderivative v (monomial u' r') := begin simp [monomial_sub_single_one_add, monomial_add_sub_single_one], congr, ring, end @[simp] lemma pderivative_mul {v : S} {f g : mv_polynomial S R} : pderivative v (f * g) = pderivative v f * g + f * pderivative v g := begin apply induction_on' f, { apply induction_on' g, { intros u r u' r', exact pderivative_monomial_mul }, { intros p q hp hq u r, rw [mul_add, pderivative_add, hp, hq, mul_add, pderivative_add], ring } }, { intros p q hp hq, simp [add_mul, hp, hq], ring, } end @[simp] lemma pderivative_C_mul {a : R} {f : mv_polynomial S R} {v : S} : pderivative v (C a * f) = C a * pderivative v f := by rw [pderivative_mul, pderivative_C, zero_mul, zero_add] end pderivative end mv_polynomial
2a8a8430e0e49e620a3d9d241c7eeb0b1780c51b
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/stage0/src/Init/Control/StateRef.lean
09eab326c5c8b416781c62f74a18a7074ea985f7
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,413
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich The State monad transformer using IO references. -/ prelude import Init.System.IO import Init.Control.State def StateRefT' (ω : Type) (σ : Type) (m : Type → Type) (α : Type) : Type := ReaderT (ST.Ref ω σ) m α /- Recall that `StateRefT` is a macro that infers `ω` from the `m`. -/ @[inline] def StateRefT'.run {ω σ : Type} {m : Type → Type} [Monad m] [MonadLiftT (ST ω) m] {α : Type} (x : StateRefT' ω σ m α) (s : σ) : m (α × σ) := do let ref ← ST.mkRef s let a ← x ref let s ← ref.get pure (a, s) @[inline] def StateRefT'.run' {ω σ : Type} {m : Type → Type} [Monad m] [MonadLiftT (ST ω) m] {α : Type} (x : StateRefT' ω σ m α) (s : σ) : m α := do let (a, _) ← x.run s pure a namespace StateRefT' variables {ω σ : Type} {m : Type → Type} {α : Type} @[inline] protected def lift (x : m α) : StateRefT' ω σ m α := fun _ => x instance [Monad m] : Monad (StateRefT' ω σ m) := inferInstanceAs (Monad (ReaderT _ _)) instance : MonadLift m (StateRefT' ω σ m) := ⟨StateRefT'.lift⟩ instance (σ m) [Monad m] : MonadFunctor m (StateRefT' ω σ m) := inferInstanceAs (MonadFunctor m (ReaderT _ _)) @[inline] protected def get [Monad m] [MonadLiftT (ST ω) m] : StateRefT' ω σ m σ := fun ref => ref.get @[inline] protected def set [Monad m] [MonadLiftT (ST ω) m] (s : σ) : StateRefT' ω σ m PUnit := fun ref => ref.set s @[inline] protected def modifyGet [Monad m] [MonadLiftT (ST ω) m] (f : σ → α × σ) : StateRefT' ω σ m α := fun ref => ref.modifyGet f instance [MonadLiftT (ST ω) m] [Monad m] : MonadStateOf σ (StateRefT' ω σ m) := { get := StateRefT'.get set := StateRefT'.set modifyGet := StateRefT'.modifyGet } instance (ε) [MonadExceptOf ε m] : MonadExceptOf ε (StateRefT' ω σ m) := { throw := StateRefT'.lift ∘ throwThe ε tryCatch := fun x c s => tryCatchThe ε (x s) (fun e => c e s) } end StateRefT' instance (ω σ : Type) (m : Type → Type) : MonadControl m (StateRefT' ω σ m) := inferInstanceAs (MonadControl m (ReaderT _ _)) instance {m : Type → Type} {ω σ : Type} [MonadFinally m] [Monad m] : MonadFinally (StateRefT' ω σ m) := inferInstanceAs (MonadFinally (ReaderT _ _))
191e079a7330fb3b90fb780cc8d68140aedb2bc1
c777c32c8e484e195053731103c5e52af26a25d1
/src/algebra/order/monoid/lemmas.lean
2f2ca1f8b5c177c2a3d3ec15dc58ae46a2f7627b
[ "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
48,190
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl, Damiano Testa, Yuyang Zhao -/ import algebra.covariant_and_contravariant import order.min_max /-! # Ordered monoids > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file develops the basics of ordered monoids. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. ## Remark Almost no monoid is actually present in this file: most assumptions have been generalized to `has_mul` or `mul_one_class`. -/ -- TODO: If possible, uniformize lemma names, taking special care of `'`, -- after the `ordered`-refactor is done. open function variables {α β : Type*} section has_mul variables [has_mul α] section has_le variables [has_le α] /- The prime on this lemma is present only on the multiplicative version. The unprimed version is taken by the analogous lemma for semiring, with an extra non-negativity assumption. -/ @[to_additive add_le_add_left] lemma mul_le_mul_left' [covariant_class α α (*) (≤)] {b c : α} (bc : b ≤ c) (a : α) : a * b ≤ a * c := covariant_class.elim _ bc @[to_additive le_of_add_le_add_left] lemma le_of_mul_le_mul_left' [contravariant_class α α (*) (≤)] {a b c : α} (bc : a * b ≤ a * c) : b ≤ c := contravariant_class.elim _ bc /- The prime on this lemma is present only on the multiplicative version. The unprimed version is taken by the analogous lemma for semiring, with an extra non-negativity assumption. -/ @[to_additive add_le_add_right] lemma mul_le_mul_right' [covariant_class α α (swap (*)) (≤)] {b c : α} (bc : b ≤ c) (a : α) : b * a ≤ c * a := covariant_class.elim a bc @[to_additive le_of_add_le_add_right] lemma le_of_mul_le_mul_right' [contravariant_class α α (swap (*)) (≤)] {a b c : α} (bc : b * a ≤ c * a) : b ≤ c := contravariant_class.elim a bc @[simp, to_additive] lemma mul_le_mul_iff_left [covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)] (a : α) {b c : α} : a * b ≤ a * c ↔ b ≤ c := rel_iff_cov α α (*) (≤) a @[simp, to_additive] lemma mul_le_mul_iff_right [covariant_class α α (swap (*)) (≤)] [contravariant_class α α (swap (*)) (≤)] (a : α) {b c : α} : b * a ≤ c * a ↔ b ≤ c := rel_iff_cov α α (swap (*)) (≤) a end has_le section has_lt variables [has_lt α] @[simp, to_additive] lemma mul_lt_mul_iff_left [covariant_class α α (*) (<)] [contravariant_class α α (*) (<)] (a : α) {b c : α} : a * b < a * c ↔ b < c := rel_iff_cov α α (*) (<) a @[simp, to_additive] lemma mul_lt_mul_iff_right [covariant_class α α (swap (*)) (<)] [contravariant_class α α (swap (*)) (<)] (a : α) {b c : α} : b * a < c * a ↔ b < c := rel_iff_cov α α (swap (*)) (<) a @[to_additive add_lt_add_left] lemma mul_lt_mul_left' [covariant_class α α (*) (<)] {b c : α} (bc : b < c) (a : α) : a * b < a * c := covariant_class.elim _ bc @[to_additive lt_of_add_lt_add_left] lemma lt_of_mul_lt_mul_left' [contravariant_class α α (*) (<)] {a b c : α} (bc : a * b < a * c) : b < c := contravariant_class.elim _ bc @[to_additive add_lt_add_right] lemma mul_lt_mul_right' [covariant_class α α (swap (*)) (<)] {b c : α} (bc : b < c) (a : α) : b * a < c * a := covariant_class.elim a bc @[to_additive lt_of_add_lt_add_right] lemma lt_of_mul_lt_mul_right' [contravariant_class α α (swap (*)) (<)] {a b c : α} (bc : b * a < c * a) : b < c := contravariant_class.elim a bc end has_lt section preorder variables [preorder α] @[to_additive] lemma mul_lt_mul_of_lt_of_lt [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (<)] {a b c d : α} (h₁ : a < b) (h₂ : c < d) : a * c < b * d := calc a * c < a * d : mul_lt_mul_left' h₂ a ... < b * d : mul_lt_mul_right' h₁ d alias add_lt_add_of_lt_of_lt ← add_lt_add @[to_additive] lemma mul_lt_mul_of_le_of_lt [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (≤)] {a b c d : α} (h₁ : a ≤ b) (h₂ : c < d) : a * c < b * d := (mul_le_mul_right' h₁ _).trans_lt (mul_lt_mul_left' h₂ b) @[to_additive] lemma mul_lt_mul_of_lt_of_le [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (<)] {a b c d : α} (h₁ : a < b) (h₂ : c ≤ d) : a * c < b * d := (mul_le_mul_left' h₂ _).trans_lt (mul_lt_mul_right' h₁ d) /-- Only assumes left strict covariance. -/ @[to_additive "Only assumes left strict covariance"] lemma left.mul_lt_mul [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (≤)] {a b c d : α} (h₁ : a < b) (h₂ : c < d) : a * c < b * d := mul_lt_mul_of_le_of_lt h₁.le h₂ /-- Only assumes right strict covariance. -/ @[to_additive "Only assumes right strict covariance"] lemma right.mul_lt_mul [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (<)] {a b c d : α} (h₁ : a < b) (h₂ : c < d) : a * c < b * d := mul_lt_mul_of_lt_of_le h₁ h₂.le @[to_additive add_le_add] lemma mul_le_mul' [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)] {a b c d : α} (h₁ : a ≤ b) (h₂ : c ≤ d) : a * c ≤ b * d := (mul_le_mul_left' h₂ _).trans (mul_le_mul_right' h₁ d) @[to_additive] lemma mul_le_mul_three [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)] {a b c d e f : α} (h₁ : a ≤ d) (h₂ : b ≤ e) (h₃ : c ≤ f) : a * b * c ≤ d * e * f := mul_le_mul' (mul_le_mul' h₁ h₂) h₃ @[to_additive] lemma mul_lt_of_mul_lt_left [covariant_class α α (*) (≤)] {a b c d : α} (h : a * b < c) (hle : d ≤ b) : a * d < c := (mul_le_mul_left' hle a).trans_lt h @[to_additive] lemma mul_le_of_mul_le_left [covariant_class α α (*) (≤)] {a b c d : α} (h : a * b ≤ c) (hle : d ≤ b) : a * d ≤ c := @act_rel_of_rel_of_act_rel _ _ _ (≤) _ ⟨λ _ _ _, le_trans⟩ a _ _ _ hle h @[to_additive] lemma mul_lt_of_mul_lt_right [covariant_class α α (swap (*)) (≤)] {a b c d : α} (h : a * b < c) (hle : d ≤ a) : d * b < c := (mul_le_mul_right' hle b).trans_lt h @[to_additive] lemma mul_le_of_mul_le_right [covariant_class α α (swap (*)) (≤)] {a b c d : α} (h : a * b ≤ c) (hle : d ≤ a) : d * b ≤ c := (mul_le_mul_right' hle b).trans h @[to_additive] lemma lt_mul_of_lt_mul_left [covariant_class α α (*) (≤)] {a b c d : α} (h : a < b * c) (hle : c ≤ d) : a < b * d := h.trans_le (mul_le_mul_left' hle b) @[to_additive] lemma le_mul_of_le_mul_left [covariant_class α α (*) (≤)] {a b c d : α} (h : a ≤ b * c) (hle : c ≤ d) : a ≤ b * d := @rel_act_of_rel_of_rel_act _ _ _ (≤) _ ⟨λ _ _ _, le_trans⟩ b _ _ _ hle h @[to_additive] lemma lt_mul_of_lt_mul_right [covariant_class α α (swap (*)) (≤)] {a b c d : α} (h : a < b * c) (hle : b ≤ d) : a < d * c := h.trans_le (mul_le_mul_right' hle c) @[to_additive] lemma le_mul_of_le_mul_right [covariant_class α α (swap (*)) (≤)] {a b c d : α} (h : a ≤ b * c) (hle : b ≤ d) : a ≤ d * c := h.trans (mul_le_mul_right' hle c) end preorder section partial_order variables [partial_order α] @[to_additive] lemma mul_left_cancel'' [contravariant_class α α (*) (≤)] {a b c : α} (h : a * b = a * c) : b = c := (le_of_mul_le_mul_left' h.le).antisymm (le_of_mul_le_mul_left' h.ge) @[to_additive] lemma mul_right_cancel'' [contravariant_class α α (swap (*)) (≤)] {a b c : α} (h : a * b = c * b) : a = c := le_antisymm (le_of_mul_le_mul_right' h.le) (le_of_mul_le_mul_right' h.ge) end partial_order section linear_order variables [linear_order α] {a b c d : α} [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (<)] @[to_additive] lemma min_le_max_of_mul_le_mul (h : a * b ≤ c * d) : min a b ≤ max c d := by { simp_rw [min_le_iff, le_max_iff], contrapose! h, exact mul_lt_mul_of_lt_of_lt h.1.1 h.2.2 } end linear_order end has_mul -- using one section mul_one_class variables [mul_one_class α] section has_le variables [has_le α] @[to_additive le_add_of_nonneg_right] lemma le_mul_of_one_le_right' [covariant_class α α (*) (≤)] {a b : α} (h : 1 ≤ b) : a ≤ a * b := calc a = a * 1 : (mul_one a).symm ... ≤ a * b : mul_le_mul_left' h a @[to_additive add_le_of_nonpos_right] lemma mul_le_of_le_one_right' [covariant_class α α (*) (≤)] {a b : α} (h : b ≤ 1) : a * b ≤ a := calc a * b ≤ a * 1 : mul_le_mul_left' h a ... = a : mul_one a @[to_additive le_add_of_nonneg_left] lemma le_mul_of_one_le_left' [covariant_class α α (swap (*)) (≤)] {a b : α} (h : 1 ≤ b) : a ≤ b * a := calc a = 1 * a : (one_mul a).symm ... ≤ b * a : mul_le_mul_right' h a @[to_additive add_le_of_nonpos_left] lemma mul_le_of_le_one_left' [covariant_class α α (swap (*)) (≤)] {a b : α} (h : b ≤ 1) : b * a ≤ a := calc b * a ≤ 1 * a : mul_le_mul_right' h a ... = a : one_mul a @[to_additive] lemma one_le_of_le_mul_right [contravariant_class α α (*) (≤)] {a b : α} (h : a ≤ a * b) : 1 ≤ b := le_of_mul_le_mul_left' $ by simpa only [mul_one] @[to_additive] lemma le_one_of_mul_le_right [contravariant_class α α (*) (≤)] {a b : α} (h : a * b ≤ a) : b ≤ 1 := le_of_mul_le_mul_left' $ by simpa only [mul_one] @[to_additive] lemma one_le_of_le_mul_left [contravariant_class α α (swap (*)) (≤)] {a b : α} (h : b ≤ a * b) : 1 ≤ a := le_of_mul_le_mul_right' $ by simpa only [one_mul] @[to_additive] lemma le_one_of_mul_le_left [contravariant_class α α (swap (*)) (≤)] {a b : α} (h : a * b ≤ b) : a ≤ 1 := le_of_mul_le_mul_right' $ by simpa only [one_mul] @[simp, to_additive le_add_iff_nonneg_right] lemma le_mul_iff_one_le_right' [covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)] (a : α) {b : α} : a ≤ a * b ↔ 1 ≤ b := iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a) @[simp, to_additive le_add_iff_nonneg_left] lemma le_mul_iff_one_le_left' [covariant_class α α (swap (*)) (≤)] [contravariant_class α α (swap (*)) (≤)] (a : α) {b : α} : a ≤ b * a ↔ 1 ≤ b := iff.trans (by rw one_mul) (mul_le_mul_iff_right a) @[simp, to_additive add_le_iff_nonpos_right] lemma mul_le_iff_le_one_right' [covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)] (a : α) {b : α} : a * b ≤ a ↔ b ≤ 1 := iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a) @[simp, to_additive add_le_iff_nonpos_left] lemma mul_le_iff_le_one_left' [covariant_class α α (swap (*)) (≤)] [contravariant_class α α (swap (*)) (≤)] {a b : α} : a * b ≤ b ↔ a ≤ 1 := iff.trans (by rw one_mul) (mul_le_mul_iff_right b) end has_le section has_lt variable [has_lt α] @[to_additive lt_add_of_pos_right] lemma lt_mul_of_one_lt_right' [covariant_class α α (*) (<)] (a : α) {b : α} (h : 1 < b) : a < a * b := calc a = a * 1 : (mul_one a).symm ... < a * b : mul_lt_mul_left' h a @[to_additive add_lt_of_neg_right] lemma mul_lt_of_lt_one_right' [covariant_class α α (*) (<)] (a : α) {b : α} (h : b < 1) : a * b < a := calc a * b < a * 1 : mul_lt_mul_left' h a ... = a : mul_one a @[to_additive lt_add_of_pos_left] lemma lt_mul_of_one_lt_left' [covariant_class α α (swap (*)) (<)] (a : α) {b : α} (h : 1 < b) : a < b * a := calc a = 1 * a : (one_mul a).symm ... < b * a : mul_lt_mul_right' h a @[to_additive add_lt_of_neg_left] lemma mul_lt_of_lt_one_left' [covariant_class α α (swap (*)) (<)] (a : α) {b : α} (h : b < 1) : b * a < a := calc b * a < 1 * a : mul_lt_mul_right' h a ... = a : one_mul a @[to_additive] lemma one_lt_of_lt_mul_right [contravariant_class α α (*) (<)] {a b : α} (h : a < a * b) : 1 < b := lt_of_mul_lt_mul_left' $ by simpa only [mul_one] @[to_additive] lemma lt_one_of_mul_lt_right [contravariant_class α α (*) (<)] {a b : α} (h : a * b < a) : b < 1 := lt_of_mul_lt_mul_left' $ by simpa only [mul_one] @[to_additive] lemma one_lt_of_lt_mul_left [contravariant_class α α (swap (*)) (<)] {a b : α} (h : b < a * b) : 1 < a := lt_of_mul_lt_mul_right' $ by simpa only [one_mul] @[to_additive] lemma lt_one_of_mul_lt_left [contravariant_class α α (swap (*)) (<)] {a b : α} (h : a * b < b) : a < 1 := lt_of_mul_lt_mul_right' $ by simpa only [one_mul] @[simp, to_additive lt_add_iff_pos_right] lemma lt_mul_iff_one_lt_right' [covariant_class α α (*) (<)] [contravariant_class α α (*) (<)] (a : α) {b : α} : a < a * b ↔ 1 < b := iff.trans (by rw mul_one) (mul_lt_mul_iff_left a) @[simp, to_additive lt_add_iff_pos_left] lemma lt_mul_iff_one_lt_left' [covariant_class α α (swap (*)) (<)] [contravariant_class α α (swap (*)) (<)] (a : α) {b : α} : a < b * a ↔ 1 < b := iff.trans (by rw one_mul) (mul_lt_mul_iff_right a) @[simp, to_additive add_lt_iff_neg_left] lemma mul_lt_iff_lt_one_left' [covariant_class α α (*) (<)] [contravariant_class α α (*) (<)] {a b : α} : a * b < a ↔ b < 1 := iff.trans (by rw mul_one) (mul_lt_mul_iff_left a) @[simp, to_additive add_lt_iff_neg_right] lemma mul_lt_iff_lt_one_right' [covariant_class α α (swap (*)) (<)] [contravariant_class α α (swap (*)) (<)] {a : α} (b : α) : a * b < b ↔ a < 1 := iff.trans (by rw one_mul) (mul_lt_mul_iff_right b) end has_lt section preorder variable [preorder α] /-! Lemmas of the form `b ≤ c → a ≤ 1 → b * a ≤ c`, which assume left covariance. -/ @[to_additive] lemma mul_le_of_le_of_le_one [covariant_class α α (*) (≤)] {a b c : α} (hbc : b ≤ c) (ha : a ≤ 1) : b * a ≤ c := calc b * a ≤ b * 1 : mul_le_mul_left' ha b ... = b : mul_one b ... ≤ c : hbc @[to_additive] lemma mul_lt_of_le_of_lt_one [covariant_class α α (*) (<)] {a b c : α} (hbc : b ≤ c) (ha : a < 1) : b * a < c := calc b * a < b * 1 : mul_lt_mul_left' ha b ... = b : mul_one b ... ≤ c : hbc @[to_additive] lemma mul_lt_of_lt_of_le_one [covariant_class α α (*) (≤)] {a b c : α} (hbc : b < c) (ha : a ≤ 1) : b * a < c := calc b * a ≤ b * 1 : mul_le_mul_left' ha b ... = b : mul_one b ... < c : hbc @[to_additive] lemma mul_lt_of_lt_of_lt_one [covariant_class α α (*) (<)] {a b c : α} (hbc : b < c) (ha : a < 1) : b * a < c := calc b * a < b * 1 : mul_lt_mul_left' ha b ... = b : mul_one b ... < c : hbc @[to_additive] lemma mul_lt_of_lt_of_lt_one' [covariant_class α α (*) (≤)] {a b c : α} (hbc : b < c) (ha : a < 1) : b * a < c := mul_lt_of_lt_of_le_one hbc ha.le /-- Assumes left covariance. The lemma assuming right covariance is `right.mul_le_one`. -/ @[to_additive "Assumes left covariance. The lemma assuming right covariance is `right.add_nonpos`."] lemma left.mul_le_one [covariant_class α α (*) (≤)] {a b : α} (ha : a ≤ 1) (hb : b ≤ 1) : a * b ≤ 1 := mul_le_of_le_of_le_one ha hb /-- Assumes left covariance. The lemma assuming right covariance is `right.mul_lt_one_of_le_of_lt`. -/ @[to_additive left.add_neg_of_nonpos_of_neg "Assumes left covariance. The lemma assuming right covariance is `right.add_neg_of_nonpos_of_neg`."] lemma left.mul_lt_one_of_le_of_lt [covariant_class α α (*) (<)] {a b : α} (ha : a ≤ 1) (hb : b < 1) : a * b < 1 := mul_lt_of_le_of_lt_one ha hb /-- Assumes left covariance. The lemma assuming right covariance is `right.mul_lt_one_of_lt_of_le`. -/ @[to_additive left.add_neg_of_neg_of_nonpos "Assumes left covariance. The lemma assuming right covariance is `right.add_neg_of_neg_of_nonpos`."] lemma left.mul_lt_one_of_lt_of_le [covariant_class α α (*) (≤)] {a b : α} (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := mul_lt_of_lt_of_le_one ha hb /-- Assumes left covariance. The lemma assuming right covariance is `right.mul_lt_one`. -/ @[to_additive "Assumes left covariance. The lemma assuming right covariance is `right.add_neg`."] lemma left.mul_lt_one [covariant_class α α (*) (<)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := mul_lt_of_lt_of_lt_one ha hb /-- Assumes left covariance. The lemma assuming right covariance is `right.mul_lt_one'`. -/ @[to_additive "Assumes left covariance. The lemma assuming right covariance is `right.add_neg'`."] lemma left.mul_lt_one' [covariant_class α α (*) (≤)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := mul_lt_of_lt_of_lt_one' ha hb /-! Lemmas of the form `b ≤ c → 1 ≤ a → b ≤ c * a`, which assume left covariance. -/ @[to_additive] lemma le_mul_of_le_of_one_le [covariant_class α α (*) (≤)] {a b c : α} (hbc : b ≤ c) (ha : 1 ≤ a) : b ≤ c * a := calc b ≤ c : hbc ... = c * 1 : (mul_one c).symm ... ≤ c * a : mul_le_mul_left' ha c @[to_additive] lemma lt_mul_of_le_of_one_lt [covariant_class α α (*) (<)] {a b c : α} (hbc : b ≤ c) (ha : 1 < a) : b < c * a := calc b ≤ c : hbc ... = c * 1 : (mul_one c).symm ... < c * a : mul_lt_mul_left' ha c @[to_additive] lemma lt_mul_of_lt_of_one_le [covariant_class α α (*) (≤)] {a b c : α} (hbc : b < c) (ha : 1 ≤ a) : b < c * a := calc b < c : hbc ... = c * 1 : (mul_one c).symm ... ≤ c * a : mul_le_mul_left' ha c @[to_additive] lemma lt_mul_of_lt_of_one_lt [covariant_class α α (*) (<)] {a b c : α} (hbc : b < c) (ha : 1 < a) : b < c * a := calc b < c : hbc ... = c * 1 : (mul_one c).symm ... < c * a : mul_lt_mul_left' ha c @[to_additive] lemma lt_mul_of_lt_of_one_lt' [covariant_class α α (*) (≤)] {a b c : α} (hbc : b < c) (ha : 1 < a) : b < c * a := lt_mul_of_lt_of_one_le hbc ha.le /-- Assumes left covariance. The lemma assuming right covariance is `right.one_le_mul`. -/ @[to_additive left.add_nonneg "Assumes left covariance. The lemma assuming right covariance is `right.add_nonneg`."] lemma left.one_le_mul [covariant_class α α (*) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := le_mul_of_le_of_one_le ha hb /-- Assumes left covariance. The lemma assuming right covariance is `right.one_lt_mul_of_le_of_lt`. -/ @[to_additive left.add_pos_of_nonneg_of_pos "Assumes left covariance. The lemma assuming right covariance is `right.add_pos_of_nonneg_of_pos`."] lemma left.one_lt_mul_of_le_of_lt [covariant_class α α (*) (<)] {a b : α} (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := lt_mul_of_le_of_one_lt ha hb /-- Assumes left covariance. The lemma assuming right covariance is `right.one_lt_mul_of_lt_of_le`. -/ @[to_additive left.add_pos_of_pos_of_nonneg "Assumes left covariance. The lemma assuming right covariance is `right.add_pos_of_pos_of_nonneg`."] lemma left.one_lt_mul_of_lt_of_le [covariant_class α α (*) (≤)] {a b : α} (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := lt_mul_of_lt_of_one_le ha hb /-- Assumes left covariance. The lemma assuming right covariance is `right.one_lt_mul`. -/ @[to_additive left.add_pos "Assumes left covariance. The lemma assuming right covariance is `right.add_pos`."] lemma left.one_lt_mul [covariant_class α α (*) (<)] {a b : α} (ha : 1 < a) (hb : 1 < b) : 1 < a * b := lt_mul_of_lt_of_one_lt ha hb /-- Assumes left covariance. The lemma assuming right covariance is `right.one_lt_mul'`. -/ @[to_additive left.add_pos' "Assumes left covariance. The lemma assuming right covariance is `right.add_pos'`."] lemma left.one_lt_mul' [covariant_class α α (*) (≤)] {a b : α} (ha : 1 < a) (hb : 1 < b) : 1 < a * b := lt_mul_of_lt_of_one_lt' ha hb /-! Lemmas of the form `a ≤ 1 → b ≤ c → a * b ≤ c`, which assume right covariance. -/ @[to_additive] lemma mul_le_of_le_one_of_le [covariant_class α α (swap (*)) (≤)] {a b c : α} (ha : a ≤ 1) (hbc : b ≤ c) : a * b ≤ c := calc a * b ≤ 1 * b : mul_le_mul_right' ha b ... = b : one_mul b ... ≤ c : hbc @[to_additive] lemma mul_lt_of_lt_one_of_le [covariant_class α α (swap (*)) (<)] {a b c : α} (ha : a < 1) (hbc : b ≤ c) : a * b < c := calc a * b < 1 * b : mul_lt_mul_right' ha b ... = b : one_mul b ... ≤ c : hbc @[to_additive] lemma mul_lt_of_le_one_of_lt [covariant_class α α (swap (*)) (≤)] {a b c : α} (ha : a ≤ 1) (hb : b < c) : a * b < c := calc a * b ≤ 1 * b : mul_le_mul_right' ha b ... = b : one_mul b ... < c : hb @[to_additive] lemma mul_lt_of_lt_one_of_lt [covariant_class α α (swap (*)) (<)] {a b c : α} (ha : a < 1) (hb : b < c) : a * b < c := calc a * b < 1 * b : mul_lt_mul_right' ha b ... = b : one_mul b ... < c : hb @[to_additive] lemma mul_lt_of_lt_one_of_lt' [covariant_class α α (swap (*)) (≤)] {a b c : α} (ha : a < 1) (hbc : b < c) : a * b < c := mul_lt_of_le_one_of_lt ha.le hbc /-- Assumes right covariance. The lemma assuming left covariance is `left.mul_le_one`. -/ @[to_additive "Assumes right covariance. The lemma assuming left covariance is `left.add_nonpos`."] lemma right.mul_le_one [covariant_class α α (swap (*)) (≤)] {a b : α} (ha : a ≤ 1) (hb : b ≤ 1) : a * b ≤ 1 := mul_le_of_le_one_of_le ha hb /-- Assumes right covariance. The lemma assuming left covariance is `left.mul_lt_one_of_lt_of_le`. -/ @[to_additive right.add_neg_of_neg_of_nonpos "Assumes right covariance. The lemma assuming left covariance is `left.add_neg_of_neg_of_nonpos`."] lemma right.mul_lt_one_of_lt_of_le [covariant_class α α (swap (*)) (<)] {a b : α} (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := mul_lt_of_lt_one_of_le ha hb /-- Assumes right covariance. The lemma assuming left covariance is `left.mul_lt_one_of_le_of_lt`. -/ @[to_additive right.add_neg_of_nonpos_of_neg "Assumes right covariance. The lemma assuming left covariance is `left.add_neg_of_nonpos_of_neg`."] lemma right.mul_lt_one_of_le_of_lt [covariant_class α α (swap (*)) (≤)] {a b : α} (ha : a ≤ 1) (hb : b < 1) : a * b < 1 := mul_lt_of_le_one_of_lt ha hb /-- Assumes right covariance. The lemma assuming left covariance is `left.mul_lt_one`. -/ @[to_additive "Assumes right covariance. The lemma assuming left covariance is `left.add_neg`."] lemma right.mul_lt_one [covariant_class α α (swap (*)) (<)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := mul_lt_of_lt_one_of_lt ha hb /-- Assumes right covariance. The lemma assuming left covariance is `left.mul_lt_one'`. -/ @[to_additive "Assumes right covariance. The lemma assuming left covariance is `left.add_neg'`."] lemma right.mul_lt_one' [covariant_class α α (swap (*)) (≤)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := mul_lt_of_lt_one_of_lt' ha hb /-! Lemmas of the form `1 ≤ a → b ≤ c → b ≤ a * c`, which assume right covariance. -/ @[to_additive] lemma le_mul_of_one_le_of_le [covariant_class α α (swap (*)) (≤)] {a b c : α} (ha : 1 ≤ a) (hbc : b ≤ c) : b ≤ a * c := calc b ≤ c : hbc ... = 1 * c : (one_mul c).symm ... ≤ a * c : mul_le_mul_right' ha c @[to_additive] lemma lt_mul_of_one_lt_of_le [covariant_class α α (swap (*)) (<)] {a b c : α} (ha : 1 < a) (hbc : b ≤ c) : b < a * c := calc b ≤ c : hbc ... = 1 * c : (one_mul c).symm ... < a * c : mul_lt_mul_right' ha c @[to_additive] lemma lt_mul_of_one_le_of_lt [covariant_class α α (swap (*)) (≤)] {a b c : α} (ha : 1 ≤ a) (hbc : b < c) : b < a * c := calc b < c : hbc ... = 1 * c : (one_mul c).symm ... ≤ a * c : mul_le_mul_right' ha c @[to_additive] lemma lt_mul_of_one_lt_of_lt [covariant_class α α (swap (*)) (<)] {a b c : α} (ha : 1 < a) (hbc : b < c) : b < a * c := calc b < c : hbc ... = 1 * c : (one_mul c).symm ... < a * c : mul_lt_mul_right' ha c @[to_additive] lemma lt_mul_of_one_lt_of_lt' [covariant_class α α (swap (*)) (≤)] {a b c : α} (ha : 1 < a) (hbc : b < c) : b < a * c := lt_mul_of_one_le_of_lt ha.le hbc /-- Assumes right covariance. The lemma assuming left covariance is `left.one_le_mul`. -/ @[to_additive right.add_nonneg "Assumes right covariance. The lemma assuming left covariance is `left.add_nonneg`."] lemma right.one_le_mul [covariant_class α α (swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := le_mul_of_one_le_of_le ha hb /-- Assumes right covariance. The lemma assuming left covariance is `left.one_lt_mul_of_lt_of_le`. -/ @[to_additive right.add_pos_of_pos_of_nonneg "Assumes right covariance. The lemma assuming left covariance is `left.add_pos_of_pos_of_nonneg`."] lemma right.one_lt_mul_of_lt_of_le [covariant_class α α (swap (*)) (<)] {a b : α} (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := lt_mul_of_one_lt_of_le ha hb /-- Assumes right covariance. The lemma assuming left covariance is `left.one_lt_mul_of_le_of_lt`. -/ @[to_additive right.add_pos_of_nonneg_of_pos "Assumes right covariance. The lemma assuming left covariance is `left.add_pos_of_nonneg_of_pos`."] lemma right.one_lt_mul_of_le_of_lt [covariant_class α α (swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := lt_mul_of_one_le_of_lt ha hb /-- Assumes right covariance. The lemma assuming left covariance is `left.one_lt_mul`. -/ @[to_additive right.add_pos "Assumes right covariance. The lemma assuming left covariance is `left.add_pos`."] lemma right.one_lt_mul [covariant_class α α (swap (*)) (<)] {a b : α} (ha : 1 < a) (hb : 1 < b) : 1 < a * b := lt_mul_of_one_lt_of_lt ha hb /-- Assumes right covariance. The lemma assuming left covariance is `left.one_lt_mul'`. -/ @[to_additive right.add_pos' "Assumes right covariance. The lemma assuming left covariance is `left.add_pos'`."] lemma right.one_lt_mul' [covariant_class α α (swap (*)) (≤)] {a b : α} (ha : 1 < a) (hb : 1 < b) : 1 < a * b := lt_mul_of_one_lt_of_lt' ha hb alias left.mul_le_one ← mul_le_one' alias left.mul_lt_one_of_le_of_lt ← mul_lt_one_of_le_of_lt alias left.mul_lt_one_of_lt_of_le ← mul_lt_one_of_lt_of_le alias left.mul_lt_one ← mul_lt_one alias left.mul_lt_one' ← mul_lt_one' attribute [to_additive add_nonpos "**Alias** of `left.add_nonpos`."] mul_le_one' attribute [to_additive add_neg_of_nonpos_of_neg "**Alias** of `left.add_neg_of_nonpos_of_neg`."] mul_lt_one_of_le_of_lt attribute [to_additive add_neg_of_neg_of_nonpos "**Alias** of `left.add_neg_of_neg_of_nonpos`."] mul_lt_one_of_lt_of_le attribute [to_additive "**Alias** of `left.add_neg`."] mul_lt_one attribute [to_additive "**Alias** of `left.add_neg'`."] mul_lt_one' alias left.one_le_mul ← one_le_mul alias left.one_lt_mul_of_le_of_lt ← one_lt_mul_of_le_of_lt' alias left.one_lt_mul_of_lt_of_le ← one_lt_mul_of_lt_of_le' alias left.one_lt_mul ← one_lt_mul' alias left.one_lt_mul' ← one_lt_mul'' attribute [to_additive add_nonneg "**Alias** of `left.add_nonneg`."] one_le_mul attribute [to_additive add_pos_of_nonneg_of_pos "**Alias** of `left.add_pos_of_nonneg_of_pos`."] one_lt_mul_of_le_of_lt' attribute [to_additive add_pos_of_pos_of_nonneg "**Alias** of `left.add_pos_of_pos_of_nonneg`."] one_lt_mul_of_lt_of_le' attribute [to_additive add_pos "**Alias** of `left.add_pos`."] one_lt_mul' attribute [to_additive add_pos' "**Alias** of `left.add_pos'`."] one_lt_mul'' @[to_additive] lemma lt_of_mul_lt_of_one_le_left [covariant_class α α (*) (≤)] {a b c : α} (h : a * b < c) (hle : 1 ≤ b) : a < c := (le_mul_of_one_le_right' hle).trans_lt h @[to_additive] lemma le_of_mul_le_of_one_le_left [covariant_class α α (*) (≤)] {a b c : α} (h : a * b ≤ c) (hle : 1 ≤ b) : a ≤ c := (le_mul_of_one_le_right' hle).trans h @[to_additive] lemma lt_of_lt_mul_of_le_one_left [covariant_class α α (*) (≤)] {a b c : α} (h : a < b * c) (hle : c ≤ 1) : a < b := h.trans_le (mul_le_of_le_one_right' hle) @[to_additive] lemma le_of_le_mul_of_le_one_left [covariant_class α α (*) (≤)] {a b c : α} (h : a ≤ b * c) (hle : c ≤ 1) : a ≤ b := h.trans (mul_le_of_le_one_right' hle) @[to_additive] lemma lt_of_mul_lt_of_one_le_right [covariant_class α α (swap (*)) (≤)] {a b c : α} (h : a * b < c) (hle : 1 ≤ a) : b < c := (le_mul_of_one_le_left' hle).trans_lt h @[to_additive] lemma le_of_mul_le_of_one_le_right [covariant_class α α (swap (*)) (≤)] {a b c : α} (h : a * b ≤ c) (hle : 1 ≤ a) : b ≤ c := (le_mul_of_one_le_left' hle).trans h @[to_additive] lemma lt_of_lt_mul_of_le_one_right [covariant_class α α (swap (*)) (≤)] {a b c : α} (h : a < b * c) (hle : b ≤ 1) : a < c := h.trans_le (mul_le_of_le_one_left' hle) @[to_additive] lemma le_of_le_mul_of_le_one_right [covariant_class α α (swap (*)) (≤)] {a b c : α} (h : a ≤ b * c) (hle : b ≤ 1) : a ≤ c := h.trans (mul_le_of_le_one_left' hle) end preorder section partial_order variables [partial_order α] @[to_additive] lemma mul_eq_one_iff' [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := iff.intro (assume hab : a * b = 1, have a ≤ 1, from hab ▸ le_mul_of_le_of_one_le le_rfl hb, have a = 1, from le_antisymm this ha, have b ≤ 1, from hab ▸ le_mul_of_one_le_of_le ha le_rfl, have b = 1, from le_antisymm this hb, and.intro ‹a = 1› ‹b = 1›) (assume ⟨ha', hb'⟩, by rw [ha', hb', mul_one]) @[to_additive] lemma mul_le_mul_iff_of_ge [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)] [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (<)] {a₁ a₂ b₁ b₂ : α} (ha : a₁ ≤ a₂) (hb : b₁ ≤ b₂) : a₂ * b₂ ≤ a₁ * b₁ ↔ a₁ = a₂ ∧ b₁ = b₂ := begin refine ⟨λ h, _, by { rintro ⟨rfl, rfl⟩, refl }⟩, simp only [eq_iff_le_not_lt, ha, hb, true_and], refine ⟨λ ha, h.not_lt _, λ hb, h.not_lt _⟩, { exact mul_lt_mul_of_lt_of_le ha hb }, { exact mul_lt_mul_of_le_of_lt ha hb } end section left variables [covariant_class α α (*) (≤)] {a b : α} @[to_additive eq_zero_of_add_nonneg_left] lemma eq_one_of_one_le_mul_left (ha : a ≤ 1) (hb : b ≤ 1) (hab : 1 ≤ a * b) : a = 1 := ha.eq_of_not_lt $ λ h, hab.not_lt $ mul_lt_one_of_lt_of_le h hb @[to_additive] lemma eq_one_of_mul_le_one_left (ha : 1 ≤ a) (hb : 1 ≤ b) (hab : a * b ≤ 1) : a = 1 := ha.eq_of_not_gt $ λ h, hab.not_lt $ one_lt_mul_of_lt_of_le' h hb end left section right variables [covariant_class α α (swap (*)) (≤)] {a b : α} @[to_additive eq_zero_of_add_nonneg_right] lemma eq_one_of_one_le_mul_right (ha : a ≤ 1) (hb : b ≤ 1) (hab : 1 ≤ a * b) : b = 1 := hb.eq_of_not_lt $ λ h, hab.not_lt $ right.mul_lt_one_of_le_of_lt ha h @[to_additive] lemma eq_one_of_mul_le_one_right (ha : 1 ≤ a) (hb : 1 ≤ b) (hab : a * b ≤ 1) : b = 1 := hb.eq_of_not_gt $ λ h, hab.not_lt $ right.one_lt_mul_of_le_of_lt ha h end right end partial_order section linear_order variables [linear_order α] lemma exists_square_le [covariant_class α α (*) (<)] (a : α) : ∃ (b : α), b * b ≤ a := begin by_cases h : a < 1, { use a, have : a*a < a*1, exact mul_lt_mul_left' h a, rw mul_one at this, exact le_of_lt this }, { use 1, push_neg at h, rwa mul_one } end end linear_order end mul_one_class section semigroup variables [semigroup α] section partial_order variables [partial_order α] /- This is not instance, since we want to have an instance from `left_cancel_semigroup`s to the appropriate `covariant_class`. -/ /-- A semigroup with a partial order and satisfying `left_cancel_semigroup` (i.e. `a * c < b * c → a < b`) is a `left_cancel semigroup`. -/ @[to_additive "An additive semigroup with a partial order and satisfying `left_cancel_add_semigroup` (i.e. `c + a < c + b → a < b`) is a `left_cancel add_semigroup`."] def contravariant.to_left_cancel_semigroup [contravariant_class α α (*) (≤)] : left_cancel_semigroup α := { mul_left_cancel := λ a b c, mul_left_cancel'' ..‹semigroup α› } /- This is not instance, since we want to have an instance from `right_cancel_semigroup`s to the appropriate `covariant_class`. -/ /-- A semigroup with a partial order and satisfying `right_cancel_semigroup` (i.e. `a * c < b * c → a < b`) is a `right_cancel semigroup`. -/ @[to_additive "An additive semigroup with a partial order and satisfying `right_cancel_add_semigroup` (`a + c < b + c → a < b`) is a `right_cancel add_semigroup`."] def contravariant.to_right_cancel_semigroup [contravariant_class α α (swap (*)) (≤)] : right_cancel_semigroup α := { mul_right_cancel := λ a b c, mul_right_cancel'' ..‹semigroup α› } @[to_additive] lemma left.mul_eq_mul_iff_eq_and_eq [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (≤)] [contravariant_class α α (*) (≤)] [contravariant_class α α (swap (*)) (≤)] {a b c d : α} (hac : a ≤ c) (hbd : b ≤ d) : a * b = c * d ↔ a = c ∧ b = d := begin refine ⟨λ h, _, λ h, congr_arg2 (*) h.1 h.2⟩, rcases hac.eq_or_lt with rfl | hac, { exact ⟨rfl, mul_left_cancel'' h⟩ }, rcases eq_or_lt_of_le hbd with rfl | hbd, { exact ⟨mul_right_cancel'' h, rfl⟩ }, exact ((left.mul_lt_mul hac hbd).ne h).elim, end @[to_additive] lemma right.mul_eq_mul_iff_eq_and_eq [covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (<)] [contravariant_class α α (swap (*)) (≤)] {a b c d : α} (hac : a ≤ c) (hbd : b ≤ d) : a * b = c * d ↔ a = c ∧ b = d := begin refine ⟨λ h, _, λ h, congr_arg2 (*) h.1 h.2⟩, rcases hac.eq_or_lt with rfl | hac, { exact ⟨rfl, mul_left_cancel'' h⟩ }, rcases eq_or_lt_of_le hbd with rfl | hbd, { exact ⟨mul_right_cancel'' h, rfl⟩ }, exact ((right.mul_lt_mul hac hbd).ne h).elim, end alias left.mul_eq_mul_iff_eq_and_eq ← mul_eq_mul_iff_eq_and_eq attribute [to_additive] mul_eq_mul_iff_eq_and_eq end partial_order end semigroup section mono variables [has_mul α] [preorder α] [preorder β] {f g : β → α} {s : set β} @[to_additive const_add] lemma monotone.const_mul' [covariant_class α α (*) (≤)] (hf : monotone f) (a : α) : monotone (λ x, a * f x) := λ x y h, mul_le_mul_left' (hf h) a @[to_additive const_add] lemma monotone_on.const_mul' [covariant_class α α (*) (≤)] (hf : monotone_on f s) (a : α) : monotone_on (λ x, a * f x) s := λ x hx y hy h, mul_le_mul_left' (hf hx hy h) a @[to_additive const_add] lemma antitone.const_mul' [covariant_class α α (*) (≤)] (hf : antitone f) (a : α) : antitone (λ x, a * f x) := λ x y h, mul_le_mul_left' (hf h) a @[to_additive const_add] lemma antitone_on.const_mul' [covariant_class α α (*) (≤)] (hf : antitone_on f s) (a : α) : antitone_on (λ x, a * f x) s := λ x hx y hy h, mul_le_mul_left' (hf hx hy h) a @[to_additive add_const] lemma monotone.mul_const' [covariant_class α α (swap (*)) (≤)] (hf : monotone f) (a : α) : monotone (λ x, f x * a) := λ x y h, mul_le_mul_right' (hf h) a @[to_additive add_const] lemma monotone_on.mul_const' [covariant_class α α (swap (*)) (≤)] (hf : monotone_on f s) (a : α) : monotone_on (λ x, f x * a) s := λ x hx y hy h, mul_le_mul_right' (hf hx hy h) a @[to_additive add_const] lemma antitone.mul_const' [covariant_class α α (swap (*)) (≤)] (hf : antitone f) (a : α) : antitone (λ x, f x * a) := λ x y h, mul_le_mul_right' (hf h) a @[to_additive add_const] lemma antitone_on.mul_const' [covariant_class α α (swap (*)) (≤)] (hf : antitone_on f s) (a : α) : antitone_on (λ x, f x * a) s := λ x hx y hy h, mul_le_mul_right' (hf hx hy h) a /-- The product of two monotone functions is monotone. -/ @[to_additive add "The sum of two monotone functions is monotone."] lemma monotone.mul' [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)] (hf : monotone f) (hg : monotone g) : monotone (λ x, f x * g x) := λ x y h, mul_le_mul' (hf h) (hg h) /-- The product of two monotone functions is monotone. -/ @[to_additive add "The sum of two monotone functions is monotone."] lemma monotone_on.mul' [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)] (hf : monotone_on f s) (hg : monotone_on g s) : monotone_on (λ x, f x * g x) s := λ x hx y hy h, mul_le_mul' (hf hx hy h) (hg hx hy h) /-- The product of two antitone functions is antitone. -/ @[to_additive add "The sum of two antitone functions is antitone."] lemma antitone.mul' [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)] (hf : antitone f) (hg : antitone g) : antitone (λ x, f x * g x) := λ x y h, mul_le_mul' (hf h) (hg h) /-- The product of two antitone functions is antitone. -/ @[to_additive add "The sum of two antitone functions is antitone."] lemma antitone_on.mul' [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)] (hf : antitone_on f s) (hg : antitone_on g s) : antitone_on (λ x, f x * g x) s := λ x hx y hy h, mul_le_mul' (hf hx hy h) (hg hx hy h) section left variables [covariant_class α α (*) (<)] @[to_additive const_add] lemma strict_mono.const_mul' (hf : strict_mono f) (c : α) : strict_mono (λ x, c * f x) := λ a b ab, mul_lt_mul_left' (hf ab) c @[to_additive const_add] lemma strict_mono_on.const_mul' (hf : strict_mono_on f s) (c : α) : strict_mono_on (λ x, c * f x) s := λ a ha b hb ab, mul_lt_mul_left' (hf ha hb ab) c @[to_additive const_add] lemma strict_anti.const_mul' (hf : strict_anti f) (c : α) : strict_anti (λ x, c * f x) := λ a b ab, mul_lt_mul_left' (hf ab) c @[to_additive const_add] lemma strict_anti_on.const_mul' (hf : strict_anti_on f s) (c : α) : strict_anti_on (λ x, c * f x) s := λ a ha b hb ab, mul_lt_mul_left' (hf ha hb ab) c end left section right variables [covariant_class α α (swap (*)) (<)] @[to_additive add_const] lemma strict_mono.mul_const' (hf : strict_mono f) (c : α) : strict_mono (λ x, f x * c) := λ a b ab, mul_lt_mul_right' (hf ab) c @[to_additive add_const] lemma strict_mono_on.mul_const' (hf : strict_mono_on f s) (c : α) : strict_mono_on (λ x, f x * c) s := λ a ha b hb ab, mul_lt_mul_right' (hf ha hb ab) c @[to_additive add_const] lemma strict_anti.mul_const' (hf : strict_anti f) (c : α) : strict_anti (λ x, f x * c) := λ a b ab, mul_lt_mul_right' (hf ab) c @[to_additive add_const] lemma strict_anti_on.mul_const' (hf : strict_anti_on f s) (c : α) : strict_anti_on (λ x, f x * c) s := λ a ha b hb ab, mul_lt_mul_right' (hf ha hb ab) c end right /-- The product of two strictly monotone functions is strictly monotone. -/ @[to_additive add "The sum of two strictly monotone functions is strictly monotone."] lemma strict_mono.mul' [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (<)] (hf : strict_mono f) (hg : strict_mono g) : strict_mono (λ x, f x * g x) := λ a b ab, mul_lt_mul_of_lt_of_lt (hf ab) (hg ab) /-- The product of two strictly monotone functions is strictly monotone. -/ @[to_additive add "The sum of two strictly monotone functions is strictly monotone."] lemma strict_mono_on.mul' [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (<)] (hf : strict_mono_on f s) (hg : strict_mono_on g s) : strict_mono_on (λ x, f x * g x) s := λ a ha b hb ab, mul_lt_mul_of_lt_of_lt (hf ha hb ab) (hg ha hb ab) /-- The product of two strictly antitone functions is strictly antitone. -/ @[to_additive add "The sum of two strictly antitone functions is strictly antitone."] lemma strict_anti.mul' [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (<)] (hf : strict_anti f) (hg : strict_anti g) : strict_anti (λ x, f x * g x) := λ a b ab, mul_lt_mul_of_lt_of_lt (hf ab) (hg ab) /-- The product of two strictly antitone functions is strictly antitone. -/ @[to_additive add "The sum of two strictly antitone functions is strictly antitone."] lemma strict_anti_on.mul' [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (<)] (hf : strict_anti_on f s) (hg : strict_anti_on g s) : strict_anti_on (λ x, f x * g x) s := λ a ha b hb ab, mul_lt_mul_of_lt_of_lt (hf ha hb ab) (hg ha hb ab) /-- The product of a monotone function and a strictly monotone function is strictly monotone. -/ @[to_additive add_strict_mono "The sum of a monotone function and a strictly monotone function is strictly monotone."] lemma monotone.mul_strict_mono' [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (≤)] {f g : β → α} (hf : monotone f) (hg : strict_mono g) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul_of_le_of_lt (hf h.le) (hg h) /-- The product of a monotone function and a strictly monotone function is strictly monotone. -/ @[to_additive add_strict_mono "The sum of a monotone function and a strictly monotone function is strictly monotone."] lemma monotone_on.mul_strict_mono' [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (≤)] {f g : β → α} (hf : monotone_on f s) (hg : strict_mono_on g s) : strict_mono_on (λ x, f x * g x) s := λ x hx y hy h, mul_lt_mul_of_le_of_lt (hf hx hy h.le) (hg hx hy h) /-- The product of a antitone function and a strictly antitone function is strictly antitone. -/ @[to_additive add_strict_anti "The sum of a antitone function and a strictly antitone function is strictly antitone."] lemma antitone.mul_strict_anti' [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (≤)] {f g : β → α} (hf : antitone f) (hg : strict_anti g) : strict_anti (λ x, f x * g x) := λ x y h, mul_lt_mul_of_le_of_lt (hf h.le) (hg h) /-- The product of a antitone function and a strictly antitone function is strictly antitone. -/ @[to_additive add_strict_anti "The sum of a antitone function and a strictly antitone function is strictly antitone."] lemma antitone_on.mul_strict_anti' [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (≤)] {f g : β → α} (hf : antitone_on f s) (hg : strict_anti_on g s) : strict_anti_on (λ x, f x * g x) s := λ x hx y hy h, mul_lt_mul_of_le_of_lt (hf hx hy h.le) (hg hx hy h) variables [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (<)] /-- The product of a strictly monotone function and a monotone function is strictly monotone. -/ @[to_additive add_monotone "The sum of a strictly monotone function and a monotone function is strictly monotone."] lemma strict_mono.mul_monotone' (hf : strict_mono f) (hg : monotone g) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul_of_lt_of_le (hf h) (hg h.le) /-- The product of a strictly monotone function and a monotone function is strictly monotone. -/ @[to_additive add_monotone "The sum of a strictly monotone function and a monotone function is strictly monotone."] lemma strict_mono_on.mul_monotone' (hf : strict_mono_on f s) (hg : monotone_on g s) : strict_mono_on (λ x, f x * g x) s := λ x hx y hy h, mul_lt_mul_of_lt_of_le (hf hx hy h) (hg hx hy h.le) /-- The product of a strictly antitone function and a antitone function is strictly antitone. -/ @[to_additive add_antitone "The sum of a strictly antitone function and a antitone function is strictly antitone."] lemma strict_anti.mul_antitone' (hf : strict_anti f) (hg : antitone g) : strict_anti (λ x, f x * g x) := λ x y h, mul_lt_mul_of_lt_of_le (hf h) (hg h.le) /-- The product of a strictly antitone function and a antitone function is strictly antitone. -/ @[to_additive add_antitone "The sum of a strictly antitone function and a antitone function is strictly antitone."] lemma strict_anti_on.mul_antitone' (hf : strict_anti_on f s) (hg : antitone_on g s) : strict_anti_on (λ x, f x * g x) s := λ x hx y hy h, mul_lt_mul_of_lt_of_le (hf hx hy h) (hg hx hy h.le) @[simp, to_additive cmp_add_left] lemma cmp_mul_left' {α : Type*} [has_mul α] [linear_order α] [covariant_class α α (*) (<)] (a b c : α) : cmp (a * b) (a * c) = cmp b c := (strict_mono_id.const_mul' a).cmp_map_eq b c @[simp, to_additive cmp_add_right] lemma cmp_mul_right' {α : Type*} [has_mul α] [linear_order α] [covariant_class α α (swap (*)) (<)] (a b c : α) : cmp (a * c) (b * c) = cmp a b := (strict_mono_id.mul_const' c).cmp_map_eq a b end mono /-- An element `a : α` is `mul_le_cancellable` if `x ↦ a * x` is order-reflecting. We will make a separate version of many lemmas that require `[contravariant_class α α (*) (≤)]` with `mul_le_cancellable` assumptions instead. These lemmas can then be instantiated to specific types, like `ennreal`, where we can replace the assumption `add_le_cancellable x` by `x ≠ ∞`. -/ @[to_additive /-" An element `a : α` is `add_le_cancellable` if `x ↦ a + x` is order-reflecting. We will make a separate version of many lemmas that require `[contravariant_class α α (+) (≤)]` with `mul_le_cancellable` assumptions instead. These lemmas can then be instantiated to specific types, like `ennreal`, where we can replace the assumption `add_le_cancellable x` by `x ≠ ∞`. "-/ ] def mul_le_cancellable [has_mul α] [has_le α] (a : α) : Prop := ∀ ⦃b c⦄, a * b ≤ a * c → b ≤ c @[to_additive] lemma contravariant.mul_le_cancellable [has_mul α] [has_le α] [contravariant_class α α (*) (≤)] {a : α} : mul_le_cancellable a := λ b c, le_of_mul_le_mul_left' @[to_additive] lemma mul_le_cancellable_one [monoid α] [has_le α] : mul_le_cancellable (1 : α) := λ a b, by simpa only [one_mul] using id namespace mul_le_cancellable @[to_additive] protected lemma injective [has_mul α] [partial_order α] {a : α} (ha : mul_le_cancellable a) : injective ((*) a) := λ b c h, le_antisymm (ha h.le) (ha h.ge) @[to_additive] protected lemma inj [has_mul α] [partial_order α] {a b c : α} (ha : mul_le_cancellable a) : a * b = a * c ↔ b = c := ha.injective.eq_iff @[to_additive] protected lemma injective_left [comm_semigroup α] [partial_order α] {a : α} (ha : mul_le_cancellable a) : injective (* a) := λ b c h, ha.injective $ by rwa [mul_comm a, mul_comm a] @[to_additive] protected lemma inj_left [comm_semigroup α] [partial_order α] {a b c : α} (hc : mul_le_cancellable c) : a * c = b * c ↔ a = b := hc.injective_left.eq_iff variable [has_le α] @[to_additive] protected lemma mul_le_mul_iff_left [has_mul α] [covariant_class α α (*) (≤)] {a b c : α} (ha : mul_le_cancellable a) : a * b ≤ a * c ↔ b ≤ c := ⟨λ h, ha h, λ h, mul_le_mul_left' h a⟩ @[to_additive] protected lemma mul_le_mul_iff_right [comm_semigroup α] [covariant_class α α (*) (≤)] {a b c : α} (ha : mul_le_cancellable a) : b * a ≤ c * a ↔ b ≤ c := by rw [mul_comm b, mul_comm c, ha.mul_le_mul_iff_left] @[to_additive] protected lemma le_mul_iff_one_le_right [mul_one_class α] [covariant_class α α (*) (≤)] {a b : α} (ha : mul_le_cancellable a) : a ≤ a * b ↔ 1 ≤ b := iff.trans (by rw [mul_one]) ha.mul_le_mul_iff_left @[to_additive] protected lemma mul_le_iff_le_one_right [mul_one_class α] [covariant_class α α (*) (≤)] {a b : α} (ha : mul_le_cancellable a) : a * b ≤ a ↔ b ≤ 1 := iff.trans (by rw [mul_one]) ha.mul_le_mul_iff_left @[to_additive] protected lemma le_mul_iff_one_le_left [comm_monoid α] [covariant_class α α (*) (≤)] {a b : α} (ha : mul_le_cancellable a) : a ≤ b * a ↔ 1 ≤ b := by rw [mul_comm, ha.le_mul_iff_one_le_right] @[to_additive] protected lemma mul_le_iff_le_one_left [comm_monoid α] [covariant_class α α (*) (≤)] {a b : α} (ha : mul_le_cancellable a) : b * a ≤ a ↔ b ≤ 1 := by rw [mul_comm, ha.mul_le_iff_le_one_right] end mul_le_cancellable section bit variables [has_add α] [preorder α] lemma bit0_mono [covariant_class α α (+) (≤)] [covariant_class α α (swap (+)) (≤)] : monotone (bit0 : α → α) := λ a b h, add_le_add h h lemma bit0_strict_mono [covariant_class α α (+) (<)] [covariant_class α α (swap (+)) (<)] : strict_mono (bit0 : α → α) := λ a b h, add_lt_add h h end bit
7f193a7e033ca4df1cd4555abfce656ed5413451
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Meta/SynthInstance.lean
dd2cda5b5ff8393d9500ca6e945c3ee327c88e3a
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
24,717
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam, Leonardo de Moura Type class instance synthesizer using tabled resolution. -/ import Init.Lean.Meta.Basic import Init.Lean.Meta.Instances import Init.Lean.Meta.LevelDefEq import Init.Lean.Meta.AbstractMVars import Init.Lean.Meta.WHNF namespace Lean namespace Meta namespace SynthInstance def mkInferTCGoalsLRAttr : IO TagAttribute := registerTagAttribute `inferTCGoalsLR "instruct type class resolution procedure to solve goals from left to right for this instance" @[init mkInferTCGoalsLRAttr] constant inferTCGoalsLRAttr : TagAttribute := arbitrary _ def hasInferTCGoalsLRAttribute (env : Environment) (constName : Name) : Bool := inferTCGoalsLRAttr.hasTag env constName structure GeneratorNode := (mvar : Expr) (key : Expr) (mctx : MetavarContext) (instances : Array Expr) (currInstanceIdx : Nat) instance GeneratorNode.inhabited : Inhabited GeneratorNode := ⟨⟨arbitrary _, arbitrary _, arbitrary _, arbitrary _, 0⟩⟩ structure ConsumerNode := (mvar : Expr) (key : Expr) (mctx : MetavarContext) (subgoals : List Expr) instance Consumernode.inhabited : Inhabited ConsumerNode := ⟨⟨arbitrary _, arbitrary _, arbitrary _, []⟩⟩ inductive Waiter | consumerNode : ConsumerNode → Waiter | root : Waiter def Waiter.isRoot : Waiter → Bool | Waiter.consumerNode _ => false | Waiter.root => true /- In tabled resolution, we creating a mapping from goals (e.g., `HasCoe Nat ?x`) to answers and waiters. Waiters are consumer nodes that are waiting for answers for a particular node. We implement this mapping using a `HashMap` where the keys are normalized expressions. That is, we replace assignable metavariables with auxiliary free variables of the form `_tc.<idx>`. We do not declare these free variables in any local context, and we should view them as "normalized names" for metavariables. For example, the term `f ?m ?m ?n` is normalized as `f _tc.0 _tc.0 _tc.1`. This approach is structural, and we may visit the same goal more than once if the different occurrences are just definitionally equal, but not structurally equal. Remark: a metavariable is assignable only if its depth is equal to the metavar context depth. -/ namespace MkTableKey structure State := (nextIdx : Nat := 0) (lmap : HashMap MVarId Level := {}) (emap : HashMap MVarId Expr := {}) abbrev M := ReaderT MetavarContext (StateM State) partial def normLevel : Level → M Level | u => if !u.hasMVar then pure u else match u with | Level.succ v _ => do v ← normLevel v; pure $ u.updateSucc! v | Level.max v w _ => do v ← normLevel v; w ← normLevel w; pure $ u.updateMax! v w | Level.imax v w _ => do v ← normLevel v; w ← normLevel w; pure $ u.updateIMax! v w | Level.mvar mvarId _ => do mctx ← read; if !mctx.isLevelAssignable mvarId then pure u else do s ← get; match s.lmap.find? mvarId with | some u' => pure u' | none => do let u' := mkLevelParam $ mkNameNum `_tc s.nextIdx; modify $ fun s => { nextIdx := s.nextIdx + 1, lmap := s.lmap.insert mvarId u', .. s }; pure u' | u => pure u partial def normExpr : Expr → M Expr | e => if !e.hasMVar then pure e else match e with | Expr.const _ us _ => do us ← us.mapM normLevel; pure $ e.updateConst! us | Expr.sort u _ => do u ← normLevel u; pure $ e.updateSort! u | Expr.app f a _ => do f ← normExpr f; a ← normExpr a; pure $ e.updateApp! f a | Expr.letE _ t v b _ => do t ← normExpr t; v ← normExpr v; b ← normExpr b; pure $ e.updateLet! t v b | Expr.forallE _ d b _ => do d ← normExpr d; b ← normExpr b; pure $ e.updateForallE! d b | Expr.lam _ d b _ => do d ← normExpr d; b ← normExpr b; pure $ e.updateLambdaE! d b | Expr.mdata _ b _ => do b ← normExpr b; pure $ e.updateMData! b | Expr.proj _ _ b _ => do b ← normExpr b; pure $ e.updateProj! b | Expr.mvar mvarId _ => do mctx ← read; if !mctx.isExprAssignable mvarId then pure e else do s ← get; match s.emap.find? mvarId with | some e' => pure e' | none => do let e' := mkFVar $ mkNameNum `_tc s.nextIdx; modify $ fun s => { nextIdx := s.nextIdx + 1, emap := s.emap.insert mvarId e', .. s }; pure e' | _ => pure e end MkTableKey /- Remark: `mkTableKey` assumes `e` does not contain assigned metavariables. -/ def mkTableKey (mctx : MetavarContext) (e : Expr) : Expr := (MkTableKey.normExpr e mctx).run' {} structure Answer := (result : AbstractMVarsResult) (resultType : Expr) instance Answer.inhabited : Inhabited Answer := ⟨⟨arbitrary _, arbitrary _⟩⟩ structure TableEntry := (waiters : Array Waiter) (answers : Array Answer := #[]) /- Remark: the SynthInstance.State is not really an extension of `Meta.State`. The field `postponed` is not needed, and the field `mctx` is misleading since `synthInstance` methods operate over different `MetavarContext`s simultaneously. That being said, we still use `extends` because it makes it simpler to move from `M` to `MetaM`. -/ structure State extends Meta.State := (result : Option Expr := none) (generatorStack : Array GeneratorNode := #[]) (resumeStack : Array (ConsumerNode × Answer) := #[]) (tableEntries : HashMap Expr TableEntry := {}) abbrev SynthM := ReaderT Context (EStateM Exception State) instance SynthM.inhabited {α} : Inhabited (SynthM α) := ⟨throw $ Exception.other ""⟩ def getTraceState : SynthM TraceState := do s ← get; pure s.traceState def getOptions : SynthM Options := do ctx ← read; pure ctx.config.opts def addContext (msg : MessageData) : SynthM MessageData := do ctx ← read; s ← get; pure $ MessageData.withContext { env := s.env, mctx := s.mctx, lctx := ctx.lctx, opts := ctx.config.opts } msg instance tracer : SimpleMonadTracerAdapter SynthM := { getOptions := getOptions, getTraceState := getTraceState, addContext := addContext, modifyTraceState := fun f => modify $ fun s => { traceState := f s.traceState, .. s } } @[inline] def liftMeta {α} (x : MetaM α) : SynthM α := adaptState (fun (s : State) => (s.toState, s)) (fun s' s => { toState := s', .. s }) x instance meta2Synth {α} : HasCoe (MetaM α) (SynthM α) := ⟨liftMeta⟩ @[inline] def withMCtx {α} (mctx : MetavarContext) (x : SynthM α) : SynthM α := do mctx' ← getMCtx; modify $ fun s => { mctx := mctx, .. s }; finally x (modify $ fun s => { mctx := mctx', .. s }) /-- Return globals and locals instances that may unify with `type` -/ def getInstances (type : Expr) : MetaM (Array Expr) := forallTelescopeReducing type $ fun _ type => do className? ← isClass type; match className? with | none => throwEx $ Exception.notInstance type | some className => do globalInstances ← getGlobalInstances; result ← globalInstances.getUnify type; result ← result.mapM $ fun c => match c with | Expr.const constName us _ => do us ← us.mapM (fun _ => mkFreshLevelMVar); pure $ c.updateConst! us | _ => panic! "global instance is not a constant"; trace! `Meta.synthInstance.globalInstances (type ++ " " ++ result); localInstances ← getLocalInstances; let result := localInstances.foldl (fun (result : Array Expr) linst => if linst.className == className then result.push linst.fvar else result) result; pure result /-- Create a new generator node for `mvar` and add `waiter` as its waiter. `key` must be `mkTableKey mctx mvarType`. -/ def newSubgoal (mctx : MetavarContext) (key : Expr) (mvar : Expr) (waiter : Waiter) : SynthM Unit := withMCtx mctx $ do trace! `Meta.synthInstance.newSubgoal key; mvarType ← inferType mvar; mvarType ← instantiateMVars mvarType; instances ← getInstances mvarType; mctx ← getMCtx; if instances.isEmpty then pure () else do let node : GeneratorNode := { mvar := mvar, key := key, mctx := mctx, instances := instances, currInstanceIdx := instances.size }; let entry : TableEntry := { waiters := #[waiter] }; modify $ fun s => { generatorStack := s.generatorStack.push node, tableEntries := s.tableEntries.insert key entry, .. s } def findEntry? (key : Expr) : SynthM (Option TableEntry) := do s ← get; pure $ s.tableEntries.find? key def getEntry (key : Expr) : SynthM TableEntry := do entry? ← findEntry? key; match entry? with | none => panic! "invalid key at synthInstance" | some entry => pure entry /-- Create a `key` for the goal associated with the given metavariable. That is, we create a key for the type of the metavariable. We must instantiate assigned metavariables before we invoke `mkTableKey`. -/ def mkTableKeyFor (mctx : MetavarContext) (mvar : Expr) : SynthM Expr := withMCtx mctx $ do mvarType ← inferType mvar; mvarType ← instantiateMVars mvarType; pure $ mkTableKey mctx mvarType /- See `getSubgoals` and `getSubgoalsAux` We use the parameter `j` to reduce the number of `instantiate*` invocations. It is the same approach we use at `forallTelescope` and `lambdaTelescope`. Given `getSubgoalsAux args j subgoals instVal type`, we have that `type.instantiateRevRange j args.size args` does not have loose bound variables. -/ structure SubgoalsResult : Type := (subgoals : List Expr) (instVal : Expr) (instTypeBody : Expr) private partial def getSubgoalsAux (lctx : LocalContext) (localInsts : LocalInstances) (xs : Array Expr) : Array Expr → Nat → List Expr → Expr → Expr → MetaM SubgoalsResult | args, j, subgoals, instVal, Expr.forallE n d b c => do let d := d.instantiateRevRange j args.size args; mvarType ← mkForall xs d; mvar ← mkFreshExprMVarAt lctx localInsts mvarType; let arg := mkAppN mvar xs; let instVal := mkApp instVal arg; let subgoals := if c.binderInfo.isInstImplicit then mvar::subgoals else subgoals; let args := args.push (mkAppN mvar xs); getSubgoalsAux args j subgoals instVal b | args, j, subgoals, instVal, type => do let type := type.instantiateRevRange j args.size args; type ← whnf type; if type.isForall then getSubgoalsAux args args.size subgoals instVal type else pure ⟨subgoals, instVal, type⟩ /-- `getSubgoals lctx localInsts xs inst` creates the subgoals for the instance `inst`. The subgoals are in the context of the free variables `xs`, and `(lctx, localInsts)` is the local context and instances before we added the free variables to it. This extra complication is required because 1- We want all metavariables created by `synthInstance` to share the same local context. 2- We want to ensure that applications such as `mvar xs` are higher order patterns. The method `getGoals` create a new metavariable for each parameter of `inst`. For example, suppose the type of `inst` is `forall (x_1 : A_1) ... (x_n : A_n), B x_1 ... x_n`. Then, we create the metavariables `?m_i : forall xs, A_i`, and return the subset of these metavariables that are instance implicit arguments, and the expressions: - `inst (?m_1 xs) ... (?m_n xs)` (aka `instVal`) - `B (?m_1 xs) ... (?m_n xs)` -/ def getSubgoals (lctx : LocalContext) (localInsts : LocalInstances) (xs : Array Expr) (inst : Expr) : MetaM SubgoalsResult := do instType ← inferType inst; result ← getSubgoalsAux lctx localInsts xs #[] 0 [] inst instType; match inst.getAppFn with | Expr.const constName _ _ => do env ← getEnv; if hasInferTCGoalsLRAttribute env constName then pure { subgoals := result.subgoals.reverse, .. result } else pure result | _ => pure result def tryResolveCore (mvar : Expr) (inst : Expr) : MetaM (Option (MetavarContext × List Expr)) := do mvarType ← inferType mvar; lctx ← getLCtx; localInsts ← getLocalInstances; forallTelescopeReducing mvarType $ fun xs mvarTypeBody => do ⟨subgoals, instVal, instTypeBody⟩ ← getSubgoals lctx localInsts xs inst; trace! `Meta.synthInstance.tryResolve (mvarTypeBody ++ " =?= " ++ instTypeBody); condM (isDefEq mvarTypeBody instTypeBody) (do instVal ← mkLambda xs instVal; condM (isDefEq mvar instVal) (do trace! `Meta.synthInstance.tryResolve "success"; mctx ← getMCtx; pure (some (mctx, subgoals))) (do trace! `Meta.synthInstance.tryResolve "failure assigning"; pure none)) (do trace! `Meta.synthInstance.tryResolve "failure"; pure none) /-- Try to synthesize metavariable `mvar` using the instance `inst`. Remark: `mctx` contains `mvar`. If it succeeds, the result is a new updated metavariable context and a new list of subgoals. A subgoal is created for each instance implicit parameter of `inst`. -/ def tryResolve (mctx : MetavarContext) (mvar : Expr) (inst : Expr) : SynthM (Option (MetavarContext × List Expr)) := traceCtx `Meta.synthInstance.tryResolve $ withMCtx mctx $ tryResolveCore mvar inst /-- Assign a precomputed answer to `mvar`. If it succeeds, the result is a new updated metavariable context and a new list of subgoals. -/ def tryAnswer (mctx : MetavarContext) (mvar : Expr) (answer : Answer) : SynthM (Option MetavarContext) := withMCtx mctx $ do (_, _, val) ← openAbstractMVarsResult answer.result; condM (isDefEq mvar val) (do mctx ← getMCtx; pure $ some mctx) (pure none) /-- Move waiters that are waiting for the given answer to the resume stack. -/ def wakeUp (answer : Answer) : Waiter → SynthM Unit | Waiter.root => if answer.result.paramNames.isEmpty && answer.result.numMVars == 0 then do modify $ fun s => { result := answer.result.expr, .. s } else do (_, _, answerExpr) ← openAbstractMVarsResult answer.result; trace! `Meta.synthInstance ("skip answer containing metavariables " ++ answerExpr); pure () | Waiter.consumerNode cNode => modify $ fun s => { resumeStack := s.resumeStack.push (cNode, answer), .. s } def isNewAnswer (oldAnswers : Array Answer) (answer : Answer) : Bool := oldAnswers.all $ fun oldAnswer => do -- Remark: isDefEq here is too expensive. TODO: if `==` is too imprecise, add some light normalization to `resultType` at `addAnswer` -- iseq ← isDefEq oldAnswer.resultType answer.resultType; pure (!iseq) oldAnswer.resultType != answer.resultType /-- Create a new answer after `cNode` resolved all subgoals. That is, `cNode.subgoals == []`. And then, store it in the tabled entries map, and wakeup waiters. -/ def addAnswer (cNode : ConsumerNode) : SynthM Unit := do answer ← withMCtx cNode.mctx $ do { traceM `Meta.synthInstance.newAnswer $ do { mvarType ← inferType cNode.mvar; pure mvarType }; val ← instantiateMVars cNode.mvar; result ← abstractMVars val; -- assignable metavariables become parameters resultType ← inferType result.expr; pure { Answer . result := result, resultType := resultType } }; -- Remark: `answer` does not contain assignable or assigned metavariables. let key := cNode.key; entry ← getEntry key; when (isNewAnswer entry.answers answer) $ do let newEntry := { answers := entry.answers.push answer, .. entry }; modify $ fun s => { tableEntries := s.tableEntries.insert key newEntry, .. s }; entry.waiters.forM (wakeUp answer) /-- Process the next subgoal in the given consumer node. -/ def consume (cNode : ConsumerNode) : SynthM Unit := match cNode.subgoals with | [] => addAnswer cNode | mvar::_ => do let waiter := Waiter.consumerNode cNode; key ← mkTableKeyFor cNode.mctx mvar; entry? ← findEntry? key; match entry? with | none => newSubgoal cNode.mctx key mvar waiter | some entry => modify $ fun s => { resumeStack := entry.answers.foldl (fun s answer => s.push (cNode, answer)) s.resumeStack, tableEntries := s.tableEntries.insert key { waiters := entry.waiters.push waiter, .. entry }, .. s } def getTop : SynthM GeneratorNode := do s ← get; pure s.generatorStack.back @[inline] def modifyTop (f : GeneratorNode → GeneratorNode) : SynthM Unit := modify $ fun s => { generatorStack := s.generatorStack.modify (s.generatorStack.size - 1) f, .. s } /-- Try the next instance in the node on the top of the generator stack. -/ def generate : SynthM Unit := do gNode ← getTop; if gNode.currInstanceIdx == 0 then modify $ fun s => { generatorStack := s.generatorStack.pop, .. s } else do let key := gNode.key; let idx := gNode.currInstanceIdx - 1; let inst := gNode.instances.get! idx; let mctx := gNode.mctx; let mvar := gNode.mvar; trace! `Meta.synthInstance.generate ("instance " ++ inst); modifyTop $ fun gNode => { currInstanceIdx := idx, .. gNode }; result? ← tryResolve mctx mvar inst; match result? with | none => pure () | some (mctx, subgoals) => consume { key := key, mvar := mvar, subgoals := subgoals, mctx := mctx } def getNextToResume : SynthM (ConsumerNode × Answer) := do s ← get; let r := s.resumeStack.back; modify $ fun s => { resumeStack := s.resumeStack.pop, .. s }; pure r /-- Given `(cNode, answer)` on the top of the resume stack, continue execution by using `answer` to solve the next subgoal. -/ def resume : SynthM Unit := do (cNode, answer) ← getNextToResume; match cNode.subgoals with | [] => panic! "resume found no remaining subgoals" | mvar::rest => do result? ← tryAnswer cNode.mctx mvar answer; match result? with | none => pure () | some mctx => do withMCtx mctx $ traceM `Meta.synthInstance.resume $ do { goal ← inferType cNode.mvar; subgoal ← inferType mvar; pure (goal ++ " <== " ++ subgoal) }; consume { key := cNode.key, mvar := cNode.mvar, subgoals := rest, mctx := mctx } def step : SynthM Bool := do s ← get; if !s.resumeStack.isEmpty then do resume; pure true else if !s.generatorStack.isEmpty then do generate; pure true else pure false def getResult : SynthM (Option Expr) := do s ← get; pure s.result def synth : Nat → SynthM (Option Expr) | 0 => do trace! `Meta.synthInstance "synthInstance is out of fuel"; pure none | fuel+1 => do trace! `Meta.synthInstance ("remaining fuel " ++ toString fuel); condM step (do result? ← getResult; match result? with | none => synth fuel | some result => pure result) (do trace! `Meta.synthInstance "failed"; pure none) def main (type : Expr) (fuel : Nat) : MetaM (Option Expr) := traceCtx `Meta.synthInstance $ do trace! `Meta.synthInstance ("main goal " ++ type); mvar ← mkFreshExprMVar type; mctx ← getMCtx; let key := mkTableKey mctx type; adaptState' (fun (s : Meta.State) => { State . .. s }) (fun (s : State) => s.toState) $ do { newSubgoal mctx key mvar Waiter.root; synth fuel } end SynthInstance /- Type class parameters can be annotated with `outParam` annotations. Given `C a_1 ... a_n`, we replace `a_i` with a fresh metavariable `?m_i` IF `a_i` is an `outParam`. The result is type correct because we reject type class declarations IF it contains a regular parameter X that depends on an `out` parameter Y. Then, we execute type class resolution as usual. If it succeeds, and metavariables ?m_i have been assigned, we try to unify the original type `C a_1 ... a_n` witht the normalized one. -/ private def preprocess (type : Expr) : MetaM Expr := forallTelescopeReducing type $ fun xs type => do type ← whnf type; mkForall xs type private def preprocessLevels (us : List Level) : MetaM (List Level) := do us ← us.foldlM (fun (r : List Level) (u : Level) => do u ← instantiateLevelMVars u; if u.hasMVar then do u' ← mkFreshLevelMVar; pure (u'::r) else pure (u::r)) []; pure $ us.reverse private partial def preprocessArgs : Expr → Nat → Array Expr → MetaM (Array Expr) | type, i, args => if h : i < args.size then do type ← whnf type; match type with | Expr.forallE _ d b _ => do let arg := args.get ⟨i, h⟩; arg ← if isOutParam d then mkFreshExprMVar d else pure arg; let args := args.set ⟨i, h⟩ arg; preprocessArgs (b.instantiate1 arg) (i+1) args | _ => throw $ Exception.other "type class resolution failed, insufficient number of arguments" -- TODO improve error message else pure args private def preprocessOutParam (type : Expr) : MetaM Expr := forallTelescope type $ fun xs typeBody => match typeBody.getAppFn with | c@(Expr.const constName us _) => do env ← getEnv; if !hasOutParams env constName then pure type else do let args := typeBody.getAppArgs; us ← preprocessLevels us; let c := mkConst constName us; cType ← inferType c; args ← preprocessArgs cType 0 args; mkForall xs (mkAppN c args) | _ => pure type @[init] def maxStepsOption : IO Unit := registerOption `synthInstance.maxSteps { defValue := (10000 : Nat), group := "", descr := "maximum steps for the type class instance synthesis procedure" } private def getMaxSteps (opts : Options) : Nat := opts.getNat `synthInstance.maxSteps 10000 def synthInstance? (type : Expr) : MetaM (Option Expr) := do opts ← getOptions; let fuel := getMaxSteps opts; inputConfig ← getConfig; withConfig (fun config => { transparency := TransparencyMode.reducible, foApprox := true, ctxApprox := true, .. config }) $ do type ← instantiateMVars type; type ← preprocess type; s ← get; match s.cache.synthInstance.find? type with | some result => pure result | none => do result? ← withNewMCtxDepth $ do { normType ← preprocessOutParam type; trace! `Meta.synthInstance (type ++ " ==> " ++ normType); result? ← SynthInstance.main normType fuel; match result? with | none => pure none | some result => do trace! `Meta.synthInstance ("FOUND result " ++ result); result ← instantiateMVars result; condM (hasAssignableMVar result) (pure none) (pure (some result)) }; result? ← match result? with | none => pure none | some result => do { trace! `Meta.synthInstance ("result " ++ result); resultType ← inferType result; condM (withConfig (fun _ => inputConfig) $ isDefEq type resultType) (do result ← instantiateMVars result; pure (some result)) (pure none) }; if type.hasMVar then pure result? else do modify $ fun s => { cache := { synthInstance := s.cache.synthInstance.insert type result?, .. s.cache }, .. s }; pure result? /-- Return `LOption.some r` if succeeded, `LOption.none` if it failed, and `LOption.undef` if instance cannot be synthesized right now because `type` contains metavariables. -/ def trySynthInstance (type : Expr) : MetaM (LOption Expr) := adaptReader (fun (ctx : Context) => { config := { isDefEqStuckEx := true, .. ctx.config }, .. ctx }) $ catch (toLOptionM $ synthInstance? type) (fun ex => match ex with | Exception.isExprDefEqStuck _ _ _ => pure LOption.undef | Exception.isLevelDefEqStuck _ _ _ => pure LOption.undef | _ => throw ex) def synthInstance (type : Expr) : MetaM Expr := do result? ← synthInstance? type; match result? with | some result => pure result | none => throwEx $ Exception.synthInstance type def synthPendingImp (mvarId : MVarId) : MetaM Bool := do mvarDecl ← getMVarDecl mvarId; match mvarDecl.kind with | MetavarKind.synthetic => do c? ← isClass mvarDecl.type; match c? with | none => pure false | some _ => do val? ← synthInstance? mvarDecl.type; match val? with | none => pure false | some val => condM (isExprMVarAssigned mvarId) (pure false) $ do assignExprMVar mvarId val; pure true | _ => pure false @[init] def setSynthPendingRef : IO Unit := synthPendingRef.set synthPendingImp @[init] private def regTraceClasses : IO Unit := do registerTraceClass `Meta.synthInstance; registerTraceClass `Meta.synthInstance.globalInstances; registerTraceClass `Meta.synthInstance.newSubgoal; registerTraceClass `Meta.synthInstance.tryResolve; registerTraceClass `Meta.synthInstance.resume; registerTraceClass `Meta.synthInstance.generate end Meta end Lean
ed69ba68ce874594728bb8c20334fd4b00ad36b9
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/src/Lean/Elab/Extra.lean
409997436c0b6387f058fd75b5c8a8e0faa3c2aa
[ "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
3,727
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.App /- Auxiliary elaboration functions: AKA custom elaborators -/ namespace Lean.Elab.Term open Meta @[builtinTermElab binrel] def elabBinRel : TermElab := fun stx expectedType? => do match (← resolveId? stx[1]) with | some f => let s ← saveAllState let (lhs, rhs) ← withSynthesize (mayPostpone := true) do let mut lhs ← elabTerm stx[2] none let mut rhs ← elabTerm stx[3] none if lhs.isAppOfArity `OfNat.ofNat 3 then lhs ← ensureHasType (← inferType rhs) lhs else if rhs.isAppOfArity `OfNat.ofNat 3 then rhs ← ensureHasType (← inferType lhs) rhs return (lhs, rhs) let lhsType ← inferType lhs let rhsType ← inferType rhs let (lhs, rhs) ← try pure (lhs, ← withRef stx[3] do ensureHasType lhsType rhs) catch _ => try pure (← withRef stx[2] do ensureHasType rhsType lhs, rhs) catch _ => s.restore -- Use default approach let lhs ← elabTerm stx[2] none let rhs ← elabTerm stx[3] none let lhsType ← inferType lhs let rhsType ← inferType rhs pure (lhs, ← withRef stx[3] do ensureHasType lhsType rhs) elabAppArgs f #[] #[Arg.expr lhs, Arg.expr rhs] expectedType? (explicit := false) (ellipsis := false) | none => throwUnknownConstant stx[1].getId @[builtinTermElab forInMacro] def elabForIn : TermElab := fun stx expectedType? => do match stx with | `(forIn! $col $init $body) => match (← isLocalIdent? col) with | none => elabTerm (← `(let col := $col; forIn! col $init $body)) expectedType? | some colFVar => tryPostponeIfNoneOrMVar expectedType? let m ← getMonad expectedType? let colType ← inferType colFVar let elemType ← mkFreshExprMVar (mkSort (mkLevelSucc (← mkFreshLevelMVar))) let forInInstance ← try mkAppM `ForIn #[m, colType, elemType] catch ex => tryPostpone; throwError! "failed to construct 'ForIn' instance for collection{indentExpr colType}\nand monad{indentExpr m}" match (← trySynthInstance forInInstance) with | LOption.some val => let ref ← getRef let forInFn ← mkConst ``forIn let namedArgs : Array NamedArg := #[ { ref := ref, name := `m, val := Arg.expr m}, { ref := ref, name := `ρ, val := Arg.expr colType}, { ref := ref, name := `α, val := Arg.expr elemType}, { ref := ref, name := `self, val := Arg.expr forInInstance}, { ref := ref, name := `inst, val := Arg.expr val} ] elabAppArgs forInFn #[] #[Arg.stx col, Arg.stx init, Arg.stx body] expectedType? (explicit := false) (ellipsis := false) | LOption.undef => tryPostpone; throwFailure forInInstance | LOption.none => throwFailure forInInstance | _ => throwUnsupportedSyntax where getMonad (expectedType? : Option Expr) : TermElabM Expr := do match expectedType? with | none => throwError "invalid 'forIn!' notation, expected type is not available" | some expectedType => match (← isTypeApp? expectedType) with | some (m, _) => return m | none => throwError! "invalid 'forIn!' notation, expected type is not of of the form `M α`{indentExpr expectedType}" throwFailure (forInInstance : Expr) : TermElabM Expr := throwError! "failed to synthesize instance for 'forIn!' notation{indentExpr forInInstance}" end Lean.Elab.Term
12f09f70f3a5ab59628ec6e1a675bef10adc20a0
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/unicode.lean
637479edff865fd925a25953266a923677f8cb52
[ "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
127
lean
constant N : Type constant α : N constant β₁ : N check β₁ constant δ : N check δ constant δ₁₁ : N check δ₁₁
2ddc0228689d4cff1175175025b98e2ff86166e1
3268ab3a126f0fef71459fbf170dc38efe5d0506
/algebra/exact_couple.hlean
2cfdfadd75f16b2ee7c54a1c4b1e976845035ea4
[ "Apache-2.0" ]
permissive
soraismus/Spectral
f043fed1a4e02ddfeba531769b2980eb817471f4
32512bf47db3a1b932856e7ed7c7830b1fc07ef0
refs/heads/master
1,585,628,705,579
1,538,609,948,000
1,538,609,974,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
36,684
hlean
/- Exact couples of graded (left-) R-modules. This file includes: - Constructing exact couples from sequences of maps - Deriving an exact couple - The convergence theorem for exact couples -/ -- Author: Floris van Doorn import .graded ..spectrum.basic .product_group .ring open algebra is_trunc left_module is_equiv equiv eq function nat sigma set_quotient group left_module /- exact couples -/ namespace exact_couple structure exact_couple (R : Ring) (I : AddAbGroup) : Type := (D E : graded_module R I) (i : D →gm D) (j : D →gm E) (k : E →gm D) (ij : is_exact_gmod i j) (jk : is_exact_gmod j k) (ki : is_exact_gmod k i) open exact_couple.exact_couple definition exact_couple_reindex [constructor] {R : Ring} {I J : AddAbGroup} (e : AddGroup_of_AddAbGroup J ≃g AddGroup_of_AddAbGroup I) (X : exact_couple R I) : exact_couple R J := ⦃exact_couple, D := λy, D X (e y), E := λy, E X (e y), i := graded_hom_reindex e (i X), j := graded_hom_reindex e (j X), k := graded_hom_reindex e (k X), ij := is_exact_gmod_reindex e (ij X), jk := is_exact_gmod_reindex e (jk X), ki := is_exact_gmod_reindex e (ki X)⦄ namespace derived_couple section parameters {R : Ring} {I : AddAbGroup} (X : exact_couple R I) local abbreviation D := D X local abbreviation E := E X local abbreviation i := i X local abbreviation j := j X local abbreviation k := k X local abbreviation ij := ij X local abbreviation jk := jk X local abbreviation ki := ki X definition d : E →gm E := j ∘gm k definition D' : graded_module R I := graded_image i definition E' : graded_module R I := graded_homology d d definition is_contr_E' {x : I} (H : is_contr (E x)) : is_contr (E' x) := is_contr_homology _ _ _ definition is_contr_D' {x : I} (H : is_contr (D x)) : is_contr (D' x) := is_contr_image_module _ _ definition E'_isomorphism {x : I} (H1 : is_contr (E ((deg d)⁻¹ᵉ x))) (H2 : is_contr (E (deg d x))) : E' x ≃lm E x := homology_isomorphism _ _ H1 H2 definition i' : D' →gm D' := graded_image_lift i ∘gm graded_submodule_incl (λx, image (i ← x)) lemma is_surjective_i' {x y : I} (p : deg i' x = y) (H : Π⦃z⦄ (q : deg i z = x), is_surjective (i ↘ q)) : is_surjective (i' ↘ p) := begin apply is_surjective_graded_hom_compose, { intro y q, apply is_surjective_graded_image_lift }, { intro y q, apply is_surjective_of_is_equiv, induction q, exact to_is_equiv (equiv_of_isomorphism (image_module_isomorphism (i ← x) (H _))) } end lemma j_lemma1 ⦃x : I⦄ (m : D x) : d ((deg j) x) (j x m) = 0 := begin rewrite [graded_hom_compose_fn,↑d,graded_hom_compose_fn], refine ap (graded_hom_fn j (deg k (deg j x))) _ ⬝ !to_respect_zero, exact compose_constant.elim (gmod_im_in_ker (jk)) x m end lemma j_lemma2 : Π⦃x : I⦄ ⦃m : D x⦄ (p : i x m = 0), (graded_homology_intro _ _ ∘gm graded_hom_lift _ j j_lemma1) x m = 0 :> E' _ := begin have Π⦃x y : I⦄ (q : deg k x = y) (r : deg d x = deg j y) (s : ap (deg j) q = r) ⦃m : D y⦄ (p : i y m = 0), image (d ↘ r) (j y m), begin intros, induction s, induction q, note m_in_im_k := is_exact.ker_in_im (ki idp _) _ p, induction m_in_im_k with e q, induction q, apply image.mk e idp end, have Π⦃x : I⦄ ⦃m : D x⦄ (p : i x m = 0), image (d ← (deg j x)) (j x m), begin intros, refine this _ _ _ p, exact to_right_inv (deg k) _ ⬝ to_left_inv (deg j) x, apply is_set.elim -- rewrite [ap_con, -adj], end, intros, rewrite [graded_hom_compose_fn], exact @quotient_map_eq_zero _ _ _ _ _ (this p) end definition j' : D' →gm E' := graded_image_elim (graded_homology_intro d d ∘gm graded_hom_lift _ j j_lemma1) j_lemma2 -- degree deg j - deg i lemma k_lemma1 ⦃x : I⦄ (m : E x) (p : d x m = 0) : image (i ← (deg k x)) (k x m) := gmod_ker_in_im (exact_couple.ij X) (k x m) p definition k₂ : graded_kernel d →gm D' := graded_submodule_functor k k_lemma1 lemma k_lemma2 ⦃x : I⦄ (m : E x) (h₁ : lm_kernel (d x) m) (h₂ : image (d ← x) m) : k₂ x ⟨m, h₁⟩ = 0 := begin assert H₁ : Π⦃x' y z w : I⦄ (p : deg k x' = y) (q : deg j y = z) (r : deg k z = w) (n : E x'), k ↘ r (j ↘ q (k ↘ p n)) = 0, { intros, exact gmod_im_in_ker (exact_couple.jk X) q r (k ↘ p n) }, induction h₂ with n p, assert H₂ : k x m = 0, { rewrite [-p], refine ap (k x) (graded_hom_compose_fn_out j k x n) ⬝ _, apply H₁ }, exact subtype_eq H₂ end definition k' : E' →gm D' := @graded_quotient_elim _ _ _ _ _ _ k₂ (by intro x m h; cases m with [m1, m2]; exact k_lemma2 m1 m2 h) open sigma.ops definition i'_eq ⦃x : I⦄ (m : D x) (h : image (i ← x) m) : (i' x ⟨m, h⟩).1 = i x m := by reflexivity definition k'_eq ⦃x : I⦄ (m : E x) (h : d x m = 0) : (k' x (class_of ⟨m, h⟩)).1 = k x m := by reflexivity lemma j'_eq {x : I} (m : D x) : j' ↘ (ap (deg j) (left_inv (deg i) x)) (graded_image_lift i x m) = class_of (graded_hom_lift _ j proof j_lemma1 qed x m) := begin refine graded_image_elim_destruct _ _ _ idp _ m, apply is_set.elim, end definition deg_i' : deg i' ~ deg i := by reflexivity definition deg_j' : deg j' ~ deg j ∘ (deg i)⁻¹ := by reflexivity definition deg_k' : deg k' ~ deg k := by reflexivity open group set_option pp.coercions true lemma i'j' : is_exact_gmod i' j' := begin intro x, refine equiv_rect (deg i) _ _, intros y z p q, revert z q x p, refine eq.rec_grading (deg i ⬝e deg j') (deg j) (ap (deg j) (left_inv (deg i) y)) _, intro x, revert y, refine eq.rec_equiv (deg i) _, apply transport (λx, is_exact_mod x _) (idpath (i' x)), apply transport (λx, is_exact_mod _ (j' ↘ (ap (deg j) (left_inv (deg i) x)))) (idpath x), apply is_exact_mod.mk, { revert x, refine equiv_rect (deg i) _ _, intro x, refine graded_image.rec _, intro m, transitivity j' ↘ _ (graded_image_lift i (deg i x) (i x m)), apply ap (λx, j' ↘ _ x), apply subtype_eq, apply i'_eq, refine !j'_eq ⬝ _, apply ap class_of, apply subtype_eq, exact is_exact.im_in_ker (exact_couple.ij X idp idp) m }, { revert x, refine equiv_rect (deg k) _ _, intro x, refine graded_image.rec _, intro m p, assert q : graded_homology_intro d d (deg j (deg k x)) (graded_hom_lift _ j j_lemma1 (deg k x) m) = 0, { exact !j'_eq⁻¹ ⬝ p }, note q2 := image_of_graded_homology_intro_eq_zero idp (graded_hom_lift _ j _ _ m) q, induction q2 with n r, assert s : j (deg k x) (m - k x n) = 0, { refine respect_sub (j (deg k x)) m (k x n) ⬝ _, refine ap (sub _) r ⬝ _, apply sub_self }, assert t : trunctype.carrier (image (i ← (deg k x)) (m - k x n)), { exact is_exact.ker_in_im (exact_couple.ij X _ _) _ s }, refine image.mk ⟨m - k x n, t⟩ _, apply subtype_eq, refine !i'_eq ⬝ !to_respect_sub ⬝ _, refine ap (@sub (D (deg i (deg k x))) _ _) _ ⬝ @sub_zero _ _ _, apply is_exact.im_in_ker (exact_couple.ki X _ _) } end lemma j'k' : is_exact_gmod j' k' := begin refine equiv_rect (deg i) _ _, intros x y z p, revert y p z, refine eq.rec_grading (deg i ⬝e deg j') (deg j) (ap (deg j) (left_inv (deg i) x)) _, intro z q, induction q, apply is_exact_mod.mk, { refine graded_image.rec _, intro m, refine ap (k' _) (j'_eq m) ⬝ _, apply subtype_eq, refine k'_eq _ _ ⬝ _, exact is_exact.im_in_ker (exact_couple.jk X idp idp) m }, { intro m p, induction m using set_quotient.rec_prop with m, induction m with m h, note q := (k'_eq m h)⁻¹ ⬝ ap pr1 p, induction is_exact.ker_in_im (exact_couple.jk X idp idp) m q with n r, apply image.mk (graded_image_lift i x n), refine !j'_eq ⬝ _, apply ap class_of, apply subtype_eq, exact r } end lemma k'i' : is_exact_gmod k' i' := begin apply is_exact_gmod.mk, { intro x m, induction m using set_quotient.rec_prop with m, cases m with m p, apply subtype_eq, change i (deg k x) (k x m) = 0, exact is_exact.im_in_ker (exact_couple.ki X idp idp) m }, { intro x m, induction m with m h, intro p, have i (deg k x) m = 0, from ap pr1 p, induction is_exact.ker_in_im (exact_couple.ki X idp idp) m this with n q, have j (deg k x) m = 0, from @(is_exact.im_in_ker2 (exact_couple.ij X _ _)) m h, have d x n = 0, from ap (j (deg k x)) q ⬝ this, exact image.mk (class_of ⟨n, this⟩) (subtype_eq q) } end end end derived_couple open derived_couple definition derived_couple [constructor] {R : Ring} {I : AddAbGroup} (X : exact_couple R I) : exact_couple R I := ⦃exact_couple, D := D' X, E := E' X, i := i' X, j := j' X, k := k' X, ij := i'j' X, jk := j'k' X, ki := k'i' X⦄ /- if an exact couple is bounded, we can prove the convergence theorem for it -/ structure is_bounded {R : Ring} {I : AddAbGroup} (X : exact_couple R I) : Type := mk' :: (B B' : I → ℕ) (Dub : Π⦃x y⦄ ⦃s : ℕ⦄, (deg (i X))^[s] x = y → B x ≤ s → is_contr (D X y)) (Dlb : Π⦃x y z⦄ ⦃s : ℕ⦄ (p : deg (i X) x = y), (deg (i X))^[s] y = z → B' z ≤ s → is_surjective (i X ↘ p)) (Elb : Π⦃x y⦄ ⦃s : ℕ⦄, (deg (i X))⁻¹ᵉ^[s] x = y → B x ≤ s → is_contr (E X y)) /- Note: Elb proves Dlb for some bound B', but we want tight control over when B' = 0 -/ protected definition is_bounded.mk [constructor] {R : Ring} {I : AddAbGroup} {X : exact_couple R I} (B B' B'' : I → ℕ) (Dub : Π⦃x : I⦄ ⦃s : ℕ⦄, B x ≤ s → is_contr (D X ((deg (i X))^[s] x))) (Dlb : Π⦃x : I⦄ ⦃s : ℕ⦄, B' x ≤ s → is_surjective (i X (((deg (i X))⁻¹ᵉ^[s + 1] x)))) (Elb : Π⦃x : I⦄ ⦃s : ℕ⦄, B'' x ≤ s → is_contr (E X ((deg (i X))⁻¹ᵉ^[s] x))) : is_bounded X := begin apply is_bounded.mk' (λx, max (B x) (B'' x)) B', { intro x y s p h, induction p, exact Dub (le.trans !le_max_left h) }, { exact abstract begin intro x y z s p q h, induction p, induction q, refine transport (λx, is_surjective (i X x)) _ (Dlb h), rewrite [-iterate_succ], apply iterate_left_inv end end }, { intro x y s p h, induction p, exact Elb (le.trans !le_max_right h) } end open is_bounded definition is_bounded_reindex [constructor] {R : Ring} {I J : AddAbGroup} (e : AddGroup_of_AddAbGroup J ≃g AddGroup_of_AddAbGroup I) {X : exact_couple R I} (HH : is_bounded X) : is_bounded (exact_couple_reindex e X) := begin apply is_bounded.mk' (B HH ∘ e) (B' HH ∘ e), { intros x y s p h, refine Dub HH _ h, refine (iterate_hsquare e _ s x)⁻¹ ⬝ ap e p, intro x, exact to_right_inv (group.equiv_of_isomorphism e) _ }, { intros x y z s p q h, refine Dlb HH _ _ h, refine (iterate_hsquare e _ s y)⁻¹ ⬝ ap e q, intro x, exact to_right_inv (group.equiv_of_isomorphism e) _ }, { intro x y s p h, refine Elb HH _ h, refine (iterate_hsquare e _ s x)⁻¹ ⬝ ap e p, intro x, exact to_right_inv (group.equiv_of_isomorphism e) _ }, end namespace convergence_theorem section parameters {R : Ring} {I : AddAbGroup} (X : exact_couple R I) (HH : is_bounded X) local abbreviation B := B HH local abbreviation B' := B' HH local abbreviation Dub := Dub HH local abbreviation Dlb := Dlb HH local abbreviation Elb := Elb HH /- We start counting pages at 0, which corresponds to what is usually called the second page -/ definition page (r : ℕ) : exact_couple R I := iterate derived_couple r X definition is_contr_E (r : ℕ) (x : I) (h : is_contr (E X x)) : is_contr (E (page r) x) := by induction r with r IH; exact h; exact is_contr_E' (page r) IH definition is_contr_D (r : ℕ) (x : I) (h : is_contr (D X x)) : is_contr (D (page r) x) := by induction r with r IH; exact h; exact is_contr_D' (page r) IH definition deg_i (r : ℕ) : deg (i (page r)) ~ deg (i X) := begin induction r with r IH, { reflexivity }, { exact IH } end definition deg_k (r : ℕ) : deg (k (page r)) ~ deg (k X) := begin induction r with r IH, { reflexivity }, { exact IH } end definition deg_j (r : ℕ) : deg (j (page r)) ~ deg (j X) ∘ iterate (deg (i X))⁻¹ r := begin induction r with r IH, { reflexivity }, { refine hwhisker_left (deg (j (page r))) (to_inv_homotopy_inv (deg_i r)) ⬝hty _, refine hwhisker_right _ IH ⬝hty _, apply hwhisker_left, symmetry, apply iterate_succ } end definition deg_j_inv (r : ℕ) : (deg (j (page r)))⁻¹ ~ iterate (deg (i X)) r ∘ (deg (j X))⁻¹ := have H : deg (j (page r)) ~ iterate_equiv (deg (i X))⁻¹ᵉ r ⬝e deg (j X), from deg_j r, λx, to_inv_homotopy_inv H x ⬝ iterate_inv (deg (i X))⁻¹ᵉ r ((deg (j X))⁻¹ x) definition deg_dr (r : ℕ) : deg (d (page r)) ~ deg (j X) ∘ iterate (deg (i X))⁻¹ r ∘ deg (k X) := compose2 (deg_j r) (deg_k r) definition deg_dr_inv (r : ℕ) : (deg (d (page r)))⁻¹ ~ (deg (k X))⁻¹ ∘ iterate (deg (i X)) r ∘ (deg (j X))⁻¹ := compose2 (to_inv_homotopy_inv (deg_k r)) (deg_j_inv r) -- definition E_isomorphism' {x : I} {r₁ r₂ : ℕ} (H : r₁ ≤ r₂) -- (H1 : Π⦃r⦄, r₁ ≤ r → r < r₂ → is_contr (E X ((deg (d (page r)))⁻¹ᵉ x))) -- (H2 : Π⦃r⦄, r₁ ≤ r → r < r₂ → is_contr (E X (deg (d (page r)) x))) : -- E (page r₂) x ≃lm E (page r₁) x := -- begin -- assert H2 : Π⦃r⦄, r₁ ≤ r → r ≤ r₂ → E (page r) x ≃lm E (page r₁) x, -- { intro r Hr₁ Hr₂, -- induction Hr₁ with r Hr₁ IH, reflexivity, -- let Hr₂' := le_of_succ_le Hr₂, -- exact E'_isomorphism (page r) (is_contr_E r _ (H1 Hr₁ Hr₂)) (is_contr_E r _ (H2 Hr₁ Hr₂)) -- ⬝lm IH Hr₂' }, -- exact H2 H (le.refl _) -- end -- definition E_isomorphism0' {x : I} {r : ℕ} -- (H1 : Πr, is_contr (E X ((deg (d (page r)))⁻¹ᵉ x))) -- (H2 : Πr, is_contr (E X (deg (d (page r)) x))) : E (page r) x ≃lm E X x := -- E_isomorphism' (zero_le r) _ _ parameter {X} definition deg_iterate_ik_commute (n : ℕ) : hsquare (deg (k X)) (deg (k X)) ((deg (i X))^[n]) ((deg (i X))^[n]) := iterate_commute n (deg_commute (k X) (i X)) definition deg_iterate_ij_commute (n : ℕ) : hsquare (deg (j X)) (deg (j X)) ((deg (i X))⁻¹ᵉ^[n]) ((deg (i X))⁻¹ᵉ^[n]) := iterate_commute n (deg_commute (j X) (i X))⁻¹ʰᵗʸᵛ definition B2 (x : I) : ℕ := max (B (deg (k X) x)) (B ((deg (j X))⁻¹ x)) definition Eub ⦃x y : I⦄ ⦃s : ℕ⦄ (p : (deg (i X))^[s] x = y) (h : B2 x ≤ s) : is_contr (E X y) := begin induction p, refine @(is_contr_middle_of_is_exact (exact_couple.jk X (right_inv (deg (j X)) _) idp)) _ _ _, exact Dub (iterate_commute s (deg_commute (j X) (i X))⁻¹ʰᵗʸʰ x) (le.trans !le_max_right h), exact Dub (deg_iterate_ik_commute s x) (le.trans !le_max_left h) end definition B3 (x : I) : ℕ := max (B (deg (j X) (deg (k X) x))) (B2 ((deg (k X))⁻¹ ((deg (j X))⁻¹ x))) definition Estable {x : I} {r : ℕ} (H : B3 x ≤ r) : E (page (r + 1)) x ≃lm E (page r) x := begin change homology (d (page r) x) (d (page r) ← x) ≃lm E (page r) x, apply homology_isomorphism: apply is_contr_E, exact Eub (hhinverse (deg_iterate_ik_commute r) _ ⬝ (deg_dr_inv r x)⁻¹) (le.trans !le_max_right H), exact Elb (deg_iterate_ij_commute r _ ⬝ (deg_dr r x)⁻¹) (le.trans !le_max_left H) end definition is_surjective_i {x y z : I} {r s : ℕ} (H : B' z ≤ s + r) (p : deg (i (page r)) x = y) (q : iterate (deg (i X)) s y = z) : is_surjective (i (page r) ↘ p) := begin revert x y z s H p q, induction r with r IH: intro x y z s H p q, { exact Dlb p q H }, { change is_surjective (i' (page r) ↘ p), apply is_surjective_i', intro z' q', refine IH _ _ _ _ (le.trans H (le_of_eq (succ_add s r)⁻¹)) _ _, refine !iterate_succ ⬝ ap ((deg (i X))^[s]) _ ⬝ q, exact !deg_i⁻¹ ⬝ p } end definition Dstable {x : I} {r : ℕ} (H : B' x ≤ r) : D (page (r + 1)) x ≃lm D (page r) x := begin change image_module (i (page r) ← x) ≃lm D (page r) x, refine image_module_isomorphism (i (page r) ← x) (is_surjective_i (le.trans H (le_of_eq !zero_add⁻¹)) _ _), reflexivity end /- the infinity pages of E and D -/ definition Einf : graded_module R I := λx, E (page (B3 x)) x definition Dinf : graded_module R I := λx, D (page (B' x)) x definition Einfstable {x y : I} {r : ℕ} (Hr : B3 y ≤ r) (p : x = y) : Einf y ≃lm E (page r) x := by symmetry; induction p; induction Hr with r Hr IH; reflexivity; exact Estable Hr ⬝lm IH definition Dinfstable {x y : I} {r : ℕ} (Hr : B' y ≤ r) (p : x = y) : Dinf y ≃lm D (page r) x := by symmetry; induction p; induction Hr with r Hr IH; reflexivity; exact Dstable Hr ⬝lm IH -- definition Einf_isomorphism' {x : I} (r₁ : ℕ) -- (H1 : Π⦃r⦄, r₁ ≤ r → is_contr (E X ((deg (d (page r)))⁻¹ᵉ x))) -- (H2 : Π⦃r⦄, r₁ ≤ r → is_contr (E X (deg (d (page r)) x))) : -- Einf x ≃lm E (page r₁) x := -- begin -- cases le.total r₁ (B3 x) with Hr Hr, -- exact E_isomorphism' Hr (λr Hr₁ Hr₂, H1 Hr₁) (λr Hr₁ Hr₂, H2 Hr₁), -- exact Einfstable Hr idp -- end -- definition Einf_isomorphism0' {x : I} -- (H1 : Π⦃r⦄, is_contr (E X ((deg (d (page r)))⁻¹ᵉ x))) -- (H2 : Π⦃r⦄, is_contr (E X (deg (d (page r)) x))) : -- Einf x ≃lm E X x := -- E_isomorphism0' _ _ parameters (x : I) definition rb (n : ℕ) : ℕ := max (max (B (deg (j X) (deg (k X) x)) + n + 1) (B3 ((deg (i X))^[n] x))) (max (B' (deg (k X) ((deg (i X))^[n] x))) (max (B' (deg (k X) ((deg (i X))^[n+1] x))) (B ((deg (j X))⁻¹ ((deg (i X))^[n] x))))) lemma rb0 (n : ℕ) : rb n ≥ n + 1 := ge.trans !le_max_left (ge.trans !le_max_left !le_add_left) lemma rb1 (n : ℕ) : B (deg (j X) (deg (k X) x)) ≤ rb n - (n + 1) := nat.le_sub_of_add_le (le.trans !le_max_left !le_max_left) lemma rb2 (n : ℕ) : B3 ((deg (i X))^[n] x) ≤ rb n := le.trans !le_max_right !le_max_left lemma rb3 (n : ℕ) : B' (deg (k X) ((deg (i X))^[n] x)) ≤ rb n := le.trans !le_max_left !le_max_right lemma rb4 (n : ℕ) : B' (deg (k X) ((deg (i X))^[n+1] x)) ≤ rb n := le.trans (le.trans !le_max_left !le_max_right) !le_max_right lemma rb5 (n : ℕ) : B ((deg (j X))⁻¹ ((deg (i X))^[n] x)) ≤ rb n := le.trans (le.trans !le_max_right !le_max_right) !le_max_right definition Einfdiag : graded_module R ℕ := λn, Einf ((deg (i X))^[n] x) definition Dinfdiag : graded_module R ℕ := λn, Dinf (deg (k X) ((deg (i X))^[n] x)) definition short_exact_mod_page_r (n : ℕ) : short_exact_mod (E (page (rb n)) ((deg (i X))^[n] x)) (D (page (rb n)) (deg (k (page (rb n))) ((deg (i X))^[n] x))) (D (page (rb n)) (deg (i (page (rb n))) (deg (k (page (rb n))) ((deg (i X))^[n] x)))) := begin fapply short_exact_mod_of_is_exact, { exact j (page (rb n)) ← ((deg (i X))^[n] x) }, { exact k (page (rb n)) ((deg (i X))^[n] x) }, { exact i (page (rb n)) (deg (k (page (rb n))) ((deg (i X))^[n] x)) }, { exact j (page (rb n)) _ }, { apply is_contr_D, refine Dub !deg_j_inv⁻¹ (rb5 n) }, { apply is_contr_E, refine Elb _ (rb1 n), refine !deg_iterate_ij_commute ⬝ _, refine ap (deg (j X)) _ ⬝ !deg_j⁻¹, refine iterate_sub _ !rb0 _ ⬝ _, apply ap (_^[rb n]), exact ap (deg (i X)) (!deg_iterate_ik_commute ⬝ !deg_k⁻¹) ⬝ !deg_i⁻¹ }, { apply jk (page (rb n)) }, { apply ki (page (rb n)) }, { apply ij (page (rb n)) } end definition short_exact_mod_infpage (n : ℕ) : short_exact_mod (Einfdiag n) (Dinfdiag n) (Dinfdiag (n+1)) := begin refine short_exact_mod_isomorphism _ _ _ (short_exact_mod_page_r n), { exact Einfstable !rb2 idp }, { exact Dinfstable !rb3 !deg_k }, { exact Dinfstable !rb4 (!deg_i ⬝ ap (deg (i X)) !deg_k ⬝ deg_commute (k X) (i X) _) } end definition Dinfdiag0 (bound_zero : B' (deg (k X) x) = 0) : Dinfdiag 0 ≃lm D X (deg (k X) x) := Dinfstable (le_of_eq bound_zero) idp lemma Dinfdiag_stable {s : ℕ} (h : B (deg (k X) x) ≤ s) : is_contr (Dinfdiag s) := is_contr_D _ _ (Dub !deg_iterate_ik_commute h) /- some useful immediate properties -/ definition short_exact_mod_infpage0 (bound_zero : B' (deg (k X) x) = 0) : short_exact_mod (Einfdiag 0) (D X (deg (k X) x)) (Dinfdiag 1) := begin refine short_exact_mod_isomorphism _ _ _ (short_exact_mod_infpage 0), { reflexivity }, { exact (Dinfdiag0 bound_zero)⁻¹ˡᵐ }, { reflexivity } end /- the convergence theorem is the following result -/ definition is_built_from_infpage (bound_zero : B' (deg (k X) x) = 0) : is_built_from (D X (deg (k X) x)) Einfdiag := is_built_from.mk Dinfdiag short_exact_mod_infpage (Dinfdiag0 bound_zero) (B (deg (k X) x)) (λs, Dinfdiag_stable) end definition deg_dr_reindex {R : Ring} {I J : AddAbGroup} (e : AddGroup_of_AddAbGroup J ≃g AddGroup_of_AddAbGroup I) (X : exact_couple R I) (r : ℕ) : deg (d (page (exact_couple_reindex e X) r)) ~ e⁻¹ᵍ ∘ deg (d (page X r)) ∘ e := begin intro x, refine !deg_dr ⬝ _ ⬝ ap e⁻¹ᵍ !deg_dr⁻¹, apply ap (e⁻¹ᵍ ∘ deg (j X)), note z := @iterate_hsquare _ _ (deg (i (exact_couple_reindex e X)))⁻¹ᵉ (deg (i X))⁻¹ᵉ e (λx, proof to_right_inv (group.equiv_of_isomorphism e) _ qed)⁻¹ʰᵗʸʰ r, refine z _ ⬝ _, apply ap ((deg (i X))⁻¹ᵉ^[r]), exact to_right_inv (group.equiv_of_isomorphism e) _ end end convergence_theorem open int group prod convergence_theorem prod.ops local attribute [coercion] AddAbGroup_of_Ring definition Z2 [constructor] : AddAbGroup := rℤ ×aa rℤ structure convergent_exact_couple.{u v w} {R : Ring} (E' : ℤ → ℤ → LeftModule.{u v} R) (Dinf : ℤ → LeftModule.{u w} R) : Type.{max u (v+1) (w+1)} := (X : exact_couple.{u 0 w v} R Z2) (HH : is_bounded X) (st : ℤ → Z2) (HB : Π(n : ℤ), is_bounded.B' HH (deg (k X) (st n)) = 0) (e : Π(x : Z2), exact_couple.E X x ≃lm E' x.1 x.2) (f : Π(n : ℤ), exact_couple.D X (deg (k X) (st n)) ≃lm Dinf n) (deg_d : ℕ → Z2) (deg_d_eq0 : Π(r : ℕ), deg (d (page X r)) 0 = deg_d r) infix ` ⟹ `:25 := convergent_exact_couple -- todo: maybe this should define convergent_spectral_sequence definition convergent_exact_couple_g [reducible] (E' : ℤ → ℤ → AbGroup) (Dinf : ℤ → AbGroup) : Type := (λn s, LeftModule_int_of_AbGroup (E' n s)) ⟹ (λn, LeftModule_int_of_AbGroup (Dinf n)) infix ` ⟹ᵍ `:25 := convergent_exact_couple_g section exact_couple open convergent_exact_couple parameters {R : Ring} {E' : ℤ → ℤ → LeftModule R} {Dinf : ℤ → LeftModule R} (c : E' ⟹ Dinf) local abbreviation X := X c local abbreviation i := i X local abbreviation HH := HH c local abbreviation st := st c local abbreviation Dinfdiag (n : ℤ) (k : ℕ) := Dinfdiag HH (st n) k local abbreviation Einfdiag (n : ℤ) (k : ℕ) := Einfdiag HH (st n) k definition deg_d_eq (r : ℕ) (ns : Z2) : (deg (d (page X r))) ns = ns + deg_d c r := !deg_eq ⬝ ap (λx, _ + x) (deg_d_eq0 c r) definition deg_d_inv_eq (r : ℕ) (ns : Z2) : (deg (d (page X r)))⁻¹ᵉ ns = ns - deg_d c r := inv_eq_of_eq (!deg_d_eq ⬝ proof !neg_add_cancel_right qed)⁻¹ definition convergent_exact_couple_isomorphism {E'' : ℤ → ℤ → LeftModule R} {Dinf' : graded_module R ℤ} (e' : Πn s, E' n s ≃lm E'' n s) (f' : Πn, Dinf n ≃lm Dinf' n) : E'' ⟹ Dinf' := convergent_exact_couple.mk X HH st (HB c) begin intro x, induction x with n s, exact e c (n, s) ⬝lm e' n s end (λn, f c n ⬝lm f' n) (deg_d c) (deg_d_eq0 c) include c definition convergent_exact_couple_reindex (i : agℤ ×ag agℤ ≃g agℤ ×ag agℤ) : (λp q, E' (i (p, q)).1 (i (p, q)).2) ⟹ Dinf := begin fapply convergent_exact_couple.mk (exact_couple_reindex i X) (is_bounded_reindex i HH), { exact λn, i⁻¹ᵍ (st n) }, { intro n, refine ap (B' HH) _ ⬝ HB c n, refine to_right_inv (group.equiv_of_isomorphism i) _ ⬝ _, apply ap (deg (k X)), exact to_right_inv (group.equiv_of_isomorphism i) _ }, { intro x, induction x with p q, exact e c (i (p, q)) }, { intro n, refine _ ⬝lm f c n, refine isomorphism_of_eq (ap (D X) _), refine to_right_inv (group.equiv_of_isomorphism i) _ ⬝ _, apply ap (deg (k X)), exact to_right_inv (group.equiv_of_isomorphism i) _ }, { exact λn, i⁻¹ᵍ (deg_d c n) }, { intro r, esimp, refine !deg_dr_reindex ⬝ ap i⁻¹ᵍ (ap (deg (d _)) (group.to_respect_zero i) ⬝ deg_d_eq0 c r) } end definition convergent_exact_couple_negate_abutment : E' ⟹ (λn, Dinf (-n)) := convergent_exact_couple.mk X HH (st ∘ neg) (λn, HB c (-n)) (e c) (λn, f c (-n)) (deg_d c) (deg_d_eq0 c) omit c definition is_built_from_of_convergent_exact_couple (n : ℤ) : is_built_from (Dinf n) (Einfdiag n) := is_built_from_isomorphism_left (f c n) (is_built_from_infpage HH (st n) (HB c n)) theorem is_contr_convergent_exact_couple_precise (n : ℤ) (H : Π(n : ℤ) (l : ℕ), is_contr (E' ((deg i)^[l] (st n)).1 ((deg i)^[l] (st n)).2)) : is_contr (Dinf n) := begin assert H2 : Π(l : ℕ), is_contr (Einfdiag n l), { intro l, apply is_contr_E, refine is_trunc_equiv_closed_rev -2 (equiv_of_isomorphism (e c _)) (H n l) }, exact is_contr_total (is_built_from_of_convergent_exact_couple n) H2 end theorem is_contr_convergent_exact_couple (n : ℤ) (H : Π(n s : ℤ), is_contr (E' n s)) : is_contr (Dinf n) := is_contr_convergent_exact_couple_precise n (λn s, !H) -- definition E_isomorphism {r₁ r₂ : ℕ} {n s : ℤ} (H : r₁ ≤ r₂) -- (H1 : Π⦃r⦄, r₁ ≤ r → r < r₂ → is_contr (E X ((n, s) - deg_d c r))) -- (H2 : Π⦃r⦄, r₁ ≤ r → r < r₂ → is_contr (E X ((n, s) + deg_d c r))) : -- E (page X r₂) (n, s) ≃lm E (page X r₁) (n, s) := -- E_isomorphism' X H -- (λr Hr₁ Hr₂, transport (is_contr ∘ E X) (deg_d_inv_eq r (n, s))⁻¹ᵖ (H1 Hr₁ Hr₂)) -- (λr Hr₁ Hr₂, transport (is_contr ∘ E X) (deg_d_eq r (n, s))⁻¹ᵖ (H2 Hr₁ Hr₂)) -- definition E_isomorphism0 {r : ℕ} {n s : ℤ} (H1 : Πr, is_contr (E X ((n, s) - deg_d c r))) -- (H2 : Πr, is_contr (E X ((n, s) + deg_d c r))) : E (page X r) (n, s) ≃lm E' n s := -- E_isomorphism (zero_le r) _ _ ⬝lm e c (n, s) -- definition Einf_isomorphism (r₁ : ℕ) {n s : ℤ} -- (H1 : Π⦃r⦄, r₁ ≤ r → is_contr (E X ((n,s) - deg_d c r))) -- (H2 : Π⦃r⦄, r₁ ≤ r → is_contr (E X ((n,s) + deg_d c r))) : -- Einf HH (n, s) ≃lm E (page X r₁) (n, s) := -- Einf_isomorphism' HH r₁ (λr Hr₁, transport (is_contr ∘ E X) (deg_d_inv_eq r (n, s))⁻¹ᵖ (H1 Hr₁)) -- (λr Hr₁, transport (is_contr ∘ E X) (deg_d_eq r (n, s))⁻¹ᵖ (H2 Hr₁)) -- definition Einf_isomorphism0 {n s : ℤ} -- (H1 : Π⦃r⦄, is_contr (E X ((n,s) - deg_d c r))) -- (H2 : Π⦃r⦄, is_contr (E X ((n,s) + deg_d c r))) : -- Einf HH (n, s) ≃lm E' n s := -- E_isomorphism0 _ _ end exact_couple variables {E' : ℤ → ℤ → AbGroup} {Dinf : ℤ → AbGroup} definition convergent_exact_couple_g_isomorphism {E'' : ℤ → ℤ → AbGroup} {Dinf' : ℤ → AbGroup} (c : E' ⟹ᵍ Dinf) (e' : Πn s, E' n s ≃g E'' n s) (f' : Πn, Dinf n ≃g Dinf' n) : E'' ⟹ᵍ Dinf' := convergent_exact_couple_isomorphism c (λn s, lm_iso_int.mk (e' n s)) (λn, lm_iso_int.mk (f' n)) end exact_couple open exact_couple namespace pointed open pointed int group is_trunc trunc is_conn definition homotopy_group_conn_nat (n : ℕ) (A : Type*[1]) : AbGroup := AbGroup.mk (π[n] A) (ab_group_homotopy_group_of_is_conn n A) definition homotopy_group_conn : Π(n : ℤ) (A : Type*[1]), AbGroup | (of_nat n) A := homotopy_group_conn_nat n A | (-[1+ n]) A := trivial_ab_group_lift notation `πc[`:95 n:0 `]`:0 := homotopy_group_conn n definition homotopy_group_conn_nat_functor (n : ℕ) {A B : Type*[1]} (f : A →* B) : homotopy_group_conn_nat n A →g homotopy_group_conn_nat n B := begin cases n with n, { apply trivial_homomorphism }, cases n with n, { apply trivial_homomorphism }, exact π→g[n+2] f end definition homotopy_group_conn_functor : Π(n : ℤ) {A B : Type*[1]} (f : A →* B), πc[n] A →g πc[n] B | (of_nat n) A B f := homotopy_group_conn_nat_functor n f | (-[1+ n]) A B f := trivial_homomorphism _ _ notation `π→c[`:95 n:0 `]`:0 := homotopy_group_conn_functor n section open prod prod.ops fiber parameters {A : ℤ → Type*[1]} (f : Π(n : ℤ), A n →* A (n - 1)) [Hf : Πn, is_conn_fun 1 (f n)] include Hf local abbreviation I [constructor] := Z2 -- definition D_sequence : graded_module rℤ I := -- λv, LeftModule_int_of_AbGroup (πc[v.2] (A (v.1))) -- definition E_sequence : graded_module rℤ I := -- λv, LeftModule_int_of_AbGroup (πc[v.2] (pconntype.mk (pfiber (f (v.1))) !Hf pt)) /- first need LES of these connected homotopy groups -/ -- definition exact_couple_sequence : exact_couple rℤ I := -- exact_couple.mk D_sequence E_sequence sorry sorry sorry sorry sorry sorry end end pointed namespace spectrum open pointed int group is_trunc trunc is_conn prod prod.ops group fin chain_complex section parameters {A : ℤ → spectrum} (f : Π(s : ℤ), A s →ₛ A (s - 1)) local abbreviation I [constructor] := Z2 definition D_sequence : graded_module rℤ I := λv, LeftModule_int_of_AbGroup (πₛ[v.1] (A (v.2))) definition E_sequence : graded_module rℤ I := λv, LeftModule_int_of_AbGroup (πₛ[v.1] (sfiber (f (v.2)))) include f definition i_sequence : D_sequence →gm D_sequence := begin fapply graded_hom.mk, exact (prod_equiv_prod erfl (add_right_action (- 1))), { intro i, exact pair_eq !add_zero⁻¹ idp }, intro v, apply lm_hom_int.mk, esimp, rexact πₛ→[v.1] (f v.2) end definition deg_j_seq_inv [constructor] : I ≃ I := prod_equiv_prod (add_right_action 1) (add_right_action (- 1)) definition fn_j_sequence [unfold 3] (x : I) : D_sequence (deg_j_seq_inv x) →lm E_sequence x := begin induction x with n s, apply lm_hom_int.mk, esimp, rexact shomotopy_groups_fun (f s) (n, 2) end definition j_sequence : D_sequence →gm E_sequence := graded_hom.mk_out deg_j_seq_inv⁻¹ᵉ (λi, idp) fn_j_sequence definition k_sequence : E_sequence →gm D_sequence := begin fapply graded_hom.mk erfl deg_eq_id, intro v, induction v with n s, apply lm_hom_int.mk, esimp, exact πₛ→[n] (spoint (f s)) end lemma ij_sequence : is_exact_gmod i_sequence j_sequence := begin intro x y z p q, revert y z q p, refine eq.rec_right_inv (deg j_sequence) _, intro y, induction x with n s, induction y with m t, refine equiv_rect !pair_eq_pair_equiv⁻¹ᵉ _ _, intro pq, esimp at pq, induction pq with p q, revert t q, refine eq.rec_equiv (add_right_action (- 1)) _, induction p using eq.rec_symm, apply is_exact_homotopy homotopy.rfl, { symmetry, exact graded_hom_mk_out_destruct deg_j_seq_inv⁻¹ᵉ (λi, idp) fn_j_sequence }, rexact is_exact_of_is_exact_at (is_exact_LES_of_shomotopy_groups (f s) (m, 2)), end lemma jk_sequence : is_exact_gmod j_sequence k_sequence := begin intro x y z p q, induction q, revert x y p, refine eq.rec_right_inv (deg j_sequence) _, intro x, induction x with n s, apply is_exact_homotopy, { symmetry, exact graded_hom_mk_out_destruct deg_j_seq_inv⁻¹ᵉ (λi, idp) fn_j_sequence }, { reflexivity }, rexact is_exact_of_is_exact_at (is_exact_LES_of_shomotopy_groups (f s) (n, 1)), end lemma ki_sequence : is_exact_gmod k_sequence i_sequence := begin intro x y z p q, induction p, induction q, induction x with n s, rexact is_exact_of_is_exact_at (is_exact_LES_of_shomotopy_groups (f s) (n, 0)), end definition exact_couple_sequence [constructor] : exact_couple rℤ I := exact_couple.mk D_sequence E_sequence i_sequence j_sequence k_sequence ij_sequence jk_sequence ki_sequence open int parameters (ub : ℤ → ℤ) (lb : ℤ → ℤ) (Aub : Π(s n : ℤ), s ≥ ub n + 1 → is_equiv (πₛ→[n] (f s))) (Alb : Π(s n : ℤ), s ≤ lb n → is_contr (πₛ[n] (A s))) definition B : I → ℕ | (n, s) := max0 (s - lb n) definition B' : I → ℕ | (n, s) := max0 (ub n - s) definition B'' : I → ℕ | (n, s) := max0 (max (ub n + 1 - s) (ub (n+1) + 1 - s)) lemma iterate_deg_i (n s : ℤ) (m : ℕ) : (deg i_sequence)^[m] (n, s) = (n, s - m) := begin induction m with m IH, { exact prod_eq idp !sub_zero⁻¹ }, { exact ap (deg i_sequence) IH ⬝ (prod_eq idp !sub_sub) } end lemma iterate_deg_i_inv (n s : ℤ) (m : ℕ) : (deg i_sequence)⁻¹ᵉ^[m] (n, s) = (n, s + m) := begin induction m with m IH, { exact prod_eq idp !add_zero⁻¹ }, { exact ap (deg i_sequence)⁻¹ᵉ IH ⬝ (prod_eq idp !add.assoc) } end include Aub Alb lemma Dub ⦃x : I⦄ ⦃t : ℕ⦄ (h : B x ≤ t) : is_contr (D_sequence ((deg i_sequence)^[t] x)) := begin apply Alb, induction x with n s, rewrite [iterate_deg_i], apply sub_le_of_sub_le, exact le_of_max0_le h, end lemma Dlb ⦃x : I⦄ ⦃t : ℕ⦄ (h : B' x ≤ t) : is_surjective (i_sequence ((deg i_sequence)⁻¹ᵉ^[t+1] x)) := begin apply is_surjective_of_is_equiv, apply Aub, induction x with n s, rewrite [iterate_deg_i_inv, ▸*, of_nat_add, -add.assoc], apply add_le_add_right, apply le_add_of_sub_left_le, exact le_of_max0_le h end lemma Elb ⦃x : I⦄ ⦃t : ℕ⦄ (h : B'' x ≤ t) : is_contr (E_sequence ((deg i_sequence)⁻¹ᵉ^[t] x)) := begin induction x with n s, rewrite [iterate_deg_i_inv, ▸*], apply is_contr_shomotopy_group_sfiber_of_is_equiv, apply Aub, apply le_add_of_sub_left_le, apply le_of_max0_le, refine le.trans _ h, apply max0_monotone, apply le_max_left, apply Aub, apply le_add_of_sub_left_le, apply le_of_max0_le, refine le.trans _ h, apply max0_monotone, apply le_max_right end definition is_bounded_sequence [constructor] : is_bounded exact_couple_sequence := is_bounded.mk B B' B'' Dub Dlb Elb -- (by intro x; reflexivity) -- begin -- intro x, induction x with n s, apply pair_eq, esimp, esimp, esimp [j_sequence, i_sequence], -- refine !add.assoc ⬝ ap (add s) !add.comm ⬝ !add.assoc⁻¹, -- end definition convergent_exact_couple_sequence : (λn s, πₛ[n] (sfiber (f s))) ⟹ᵍ (λn, πₛ[n] (A (ub n))) := begin fapply convergent_exact_couple.mk, { exact exact_couple_sequence }, { exact is_bounded_sequence }, { exact λn, (n, ub n) }, { intro n, change max0 (ub n - ub n) = 0, exact ap max0 (sub_self (ub n)) }, { intro ns, reflexivity }, { intro n, reflexivity }, { intro r, exact (- 1, r + 1) }, { intro r, refine !convergence_theorem.deg_dr ⬝ _, refine ap (deg j_sequence) !iterate_deg_i_inv ⬝ _, exact prod_eq idp !zero_add } end end -- Uncomment the next line to see that the proof uses univalence, but that there are no holes --('sorry') in the proof: -- print axioms is_bounded_sequence -- It depends on univalence in an essential way. The reason is that the long exact sequence -- of homotopy groups already depends on univalence. Namely, in that proof we need to show that if f -- : A → B and g : B → C are exact at B, then ∥A∥₀ → ∥B∥₀ → ∥C∥₀ is exact at ∥B∥₀. For this we need -- that the equality |b|₀ = |b'|₀ is equivalent to ∥b = b'∥₋₁, which requires univalence. end spectrum
4cadca640915f28f1318f5f9a35892c712cc0a07
2c41ae31b2b771ad5646ad880201393f5269a7f0
/Lean/Qualities/MissionEffective.lean
4fedfd52218ad6653681dff3dd5368fa2e1f746b
[]
no_license
kevinsullivan/Boehm
926f25bc6f1a8b6bd47d333d936fdfc278228312
55208395bff20d48a598b7fa33a4d55a2447a9cf
refs/heads/master
1,586,127,134,302
1,488,252,326,000
1,488,252,326,000
32,836,930
0
0
null
null
null
null
UTF-8
Lean
false
false
1,176
lean
import SystemModel.System import Qualities.PhysicalCapable import Qualities.CyberCapable import Qualities.HumanUsable import Qualities.Speed import Qualities.Endurable import Qualities.Maneuverable import Qualities.Accurate import Qualities.Impactful import Qualities.Scalable import Qualities.Versatile import Qualities.Interoperable -- Mission Effective /- [MissionEffective] is parameterized by an instance of type [SystemType]. The constituent attributes of [MissionEffective] are the things whether it can be produced and maintained in a mission effective way. Informally, in English: An instance of type [SystemType] is deemed [MissionEffective] if and only if all the requirements of its sub-attributes are satisfied given the same conditions. -/ inductive MissionEffective (sys_type: SystemType): Prop | intro: PhysicalCapable sys_type -> CyberCapable sys_type -> HumanUsable sys_type -> Speed sys_type -> Endurable sys_type -> Maneuverable sys_type -> Accurate sys_type -> Impactful sys_type -> Scalable sys_type -> Versatile sys_type -> Interoperable sys_type -> MissionEffective
300c28103d9824dc54072da4b44d0ae23bb30641
ed27983dd289b3bcad416f0b1927105d6ef19db8
/src/inClassNotes/higherOrderFunctions/higherOrderFunctions-example.lean
ca25d6c326f52748ad91f308a006530cd7550e98
[]
no_license
liuxin-James/complogic-s21
0d55b76dbe25024473d31d98b5b83655c365f811
13e03e0114626643b44015c654151fb651603486
refs/heads/master
1,681,109,264,463
1,618,848,261,000
1,618,848,261,000
337,599,491
0
0
null
1,613,141,619,000
1,612,925,555,000
null
UTF-8
Lean
false
false
601
lean
def func (n : nat) : (nat → nat) := λ m, m + n def foo (n : nat) : (nat → (string → bool)) := fun (m : nat), fun (s : string), tt #check foo 1 2 #check foo def x := func 3 #check func -- func 3 -- λ (m : ℕ), 3 + m -- x := λ (m : ℕ), 3 + m #eval x 6 -- 3 + m -- 3 + 6 -- 9 /- def func (3 : nat) : (nat → nat) := fun (m : ℕ), m + 1 -/ /- 6 + 1 -/ -- ℕ → (ℕ → ℕ) #check nat.add #eval nat.add 3 4 /- def func (n : nat) : (nat → nat) := λ (m : ℕ), n + m -/ -- func 5 -- λ (m : ℕ), 5 + m #eval (λ (m : ℕ), 5 + m) 7 -- 5 + m -- 5 + 7 -- 12
8c972352e1ca5bb374a538d7bc8574cb22d3912c
7cef822f3b952965621309e88eadf618da0c8ae9
/src/category_theory/adjunction/fully_faithful.lean
42b9367fb29c4b350a245eda0d711cce1f0769c7
[ "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
2,286
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.adjunction.basic import category_theory.yoneda open category_theory namespace category_theory universes v₁ v₂ u₁ u₂ open category open opposite variables {C : Type u₁} [𝒞 : category.{v₁} C] variables {D : Type u₂} [𝒟 : category.{v₂} D] include 𝒞 𝒟 variables {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R) -- Lemma 4.5.13 from [Riehl][riehl2017] -- Proof in <https://stacks.math.columbia.edu/tag/0036> -- or at <https://math.stackexchange.com/a/2727177> instance unit_is_iso_of_L_fully_faithful [full L] [faithful L] : is_iso (adjunction.unit h) := @nat_iso.is_iso_of_is_iso_app _ _ _ _ _ _ (adjunction.unit h) $ λ X, @yoneda.is_iso _ _ _ _ ((adjunction.unit h).app X) { inv := { app := λ Y f, L.preimage ((h.hom_equiv (unop Y) (L.obj X)).symm f) }, inv_hom_id' := begin ext1, ext1, dsimp, simp only [adjunction.hom_equiv_counit, preimage_comp, preimage_map, category.assoc], rw ←h.unit_naturality, simp, end, hom_inv_id' := begin ext1, ext1, dsimp, apply L.injectivity, simp, end }. instance counit_is_iso_of_R_fully_faithful [full R] [faithful R] : is_iso (adjunction.counit h) := @nat_iso.is_iso_of_is_iso_app _ _ _ _ _ _ (adjunction.counit h) $ λ X, @is_iso_of_op _ _ _ _ _ $ @coyoneda.is_iso _ _ _ _ ((adjunction.counit h).app X).op { inv := { app := λ Y f, R.preimage ((h.hom_equiv (R.obj X) Y) f) }, inv_hom_id' := begin ext1, ext1, dsimp, simp only [adjunction.hom_equiv_unit, preimage_comp, preimage_map], rw ←h.counit_naturality, simp, end, hom_inv_id' := begin ext1, ext1, dsimp, apply R.injectivity, simp, end } -- TODO also prove the converses? -- def L_full_of_unit_is_iso [is_iso (adjunction.unit h)] : full L := sorry -- def L_faithful_of_unit_is_iso [is_iso (adjunction.unit h)] : faithful L := sorry -- def R_full_of_counit_is_iso [is_iso (adjunction.counit h)] : full R := sorry -- def R_faithful_of_counit_is_iso [is_iso (adjunction.counit h)] : faithful R := sorry -- TODO also do the statements from Riehl 4.5.13 for full and faithful separately? end category_theory
57630d4d2f5e1dae3072f6d955f0c5cd9c541cd1
63abd62053d479eae5abf4951554e1064a4c45b4
/src/linear_algebra/matrix.lean
d240e409faf79aaaf9dc4fd7b7bb0741d82ad458
[ "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
35,535
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl, Patrick Massot, Casper Putz -/ import linear_algebra.finite_dimensional import linear_algebra.nonsingular_inverse import linear_algebra.multilinear import linear_algebra.dual /-! # Linear maps and matrices This file defines the maps to send matrices to a linear map, and to send linear maps between modules with a finite bases to matrices. This defines a linear equivalence between linear maps between finite-dimensional vector spaces and matrices indexed by the respective bases. It also defines the trace of an endomorphism, and the determinant of a family of vectors with respect to some basis. Some results are proved about the linear map corresponding to a diagonal matrix (`range`, `ker` and `rank`). ## Main definitions In the list below, and in all this file, `R` is a commutative ring (semiring is sometimes enough), `M` and its variations are `R`-modules, `ι`, `κ`, `n` and `m` are finite types used for indexing. * `linear_map.to_matrix`: given bases `v₁ : ι → M₁` and `v₂ : κ → M₂`, the `R`-linear equivalence from `M₁ →ₗ[R] M₂` to `matrix κ ι R` * `matrix.to_lin`: the inverse of `linear_map.to_matrix` * `linear_map.to_matrix'`: the `R`-linear equivalence from `(n → R) →ₗ[R] (m → R)` to `matrix n m R` (with the standard basis on `n → R` and `m → R`) * `matrix.to_lin'`: the inverse of `linear_map.to_matrix'` * `alg_equiv_matrix`: given a basis indexed by `n`, the `R`-algebra equivalence between `R`-endomorphisms of `M` and `matrix n n R` * `matrix.trace`: the trace of a square matrix * `linear_map.trace`: the trace of an endomorphism * `is_basis.to_matrix`: the matrix whose columns are a given family of vectors in a given basis * `is_basis.to_matrix_equiv`: given a basis, the linear equivalence between families of vectors and matrices arising from `is_basis.to_matrix` * `is_basis.det`: the determinant of a family of vectors with respect to a basis, as a multilinear map ## Tags linear_map, matrix, linear_equiv, diagonal, det, trace -/ noncomputable theory open linear_map matrix set submodule open_locale big_operators open_locale matrix universes u v w section to_matrix' variables {R : Type*} [comm_ring R] variables {l m n : Type*} [fintype l] [fintype m] [fintype n] instance [decidable_eq m] [decidable_eq n] (R) [fintype R] : fintype (matrix m n R) := by unfold matrix; apply_instance /-- `matrix.mul_vec M` is a linear map. -/ def matrix.mul_vec_lin (M : matrix m n R) : (n → R) →ₗ[R] (m → R) := { to_fun := M.mul_vec, map_add' := λ v w, funext (λ i, dot_product_add _ _ _), map_smul' := λ c v, funext (λ i, dot_product_smul _ _ _) } @[simp] lemma matrix.mul_vec_lin_apply (M : matrix m n R) (v : n → R) : matrix.mul_vec_lin M v = M.mul_vec v := rfl variables [decidable_eq n] @[simp] lemma matrix.mul_vec_std_basis (M : matrix m n R) (i j) : M.mul_vec (std_basis R (λ _, R) j 1) i = M i j := begin have : (∑ j', M i j' * if j = j' then 1 else 0) = M i j, { simp_rw [mul_boole, finset.sum_ite_eq, finset.mem_univ, if_true] }, convert this, ext, split_ifs with h; simp only [std_basis_apply], { rw [h, function.update_same] }, { rw [function.update_noteq (ne.symm h), pi.zero_apply] } end /-- Linear maps `(n → R) →ₗ[R] (m → R)` are linearly equivalent to `matrix m n R`. -/ def linear_map.to_matrix' : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] matrix m n R := { to_fun := λ f i j, f (std_basis R (λ _, R) j 1) i, inv_fun := matrix.mul_vec_lin, right_inv := λ M, by { ext i j, simp only [matrix.mul_vec_std_basis, matrix.mul_vec_lin_apply] }, left_inv := λ f, begin apply (pi.is_basis_fun R n).ext, intro j, ext i, simp only [matrix.mul_vec_std_basis, matrix.mul_vec_lin_apply], congr end, map_add' := λ f g, by { ext i j, simp only [pi.add_apply, linear_map.add_apply] }, map_smul' := λ c f, by { ext i j, simp only [pi.smul_apply, linear_map.smul_apply] } } /-- A `matrix m n R` is linearly equivalent to a linear map `(n → R) →ₗ[R] (m → R)`. -/ def matrix.to_lin' : matrix m n R ≃ₗ[R] ((n → R) →ₗ[R] (m → R)) := linear_map.to_matrix'.symm @[simp] lemma linear_map.to_matrix'_symm : (linear_map.to_matrix'.symm : matrix m n R ≃ₗ[R] _) = matrix.to_lin' := rfl @[simp] lemma matrix.to_lin'_symm : (matrix.to_lin'.symm : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] _) = linear_map.to_matrix' := rfl @[simp] lemma linear_map.to_matrix'_to_lin' (M : matrix m n R) : linear_map.to_matrix' (matrix.to_lin' M) = M := linear_map.to_matrix'.apply_symm_apply M @[simp] lemma matrix.to_lin'_to_matrix' (f : (n → R) →ₗ[R] (m → R)) : matrix.to_lin' (linear_map.to_matrix' f) = f := matrix.to_lin'.apply_symm_apply f @[simp] lemma linear_map.to_matrix'_apply (f : (n → R) →ₗ[R] (m → R)) (i j) : linear_map.to_matrix' f i j = f (λ j', if j' = j then 1 else 0) i := begin simp only [linear_map.to_matrix', linear_equiv.mk_apply], congr, ext j', split_ifs with h, { rw [h, std_basis_same] }, apply std_basis_ne _ _ _ _ h end @[simp] lemma matrix.to_lin'_apply (M : matrix m n R) (v : n → R) : matrix.to_lin' M v = M.mul_vec v := rfl @[simp] lemma matrix.to_lin'_one : matrix.to_lin' (1 : matrix n n R) = id := by { ext, simp } @[simp] lemma linear_map.to_matrix'_id : (linear_map.to_matrix' (linear_map.id : (n → R) →ₗ[R] (n → R))) = 1 := by { ext, rw [matrix.one_apply, linear_map.to_matrix'_apply, id_apply] } @[simp] lemma matrix.to_lin'_mul [decidable_eq m] (M : matrix l m R) (N : matrix m n R) : matrix.to_lin' (M ⬝ N) = (matrix.to_lin' M).comp (matrix.to_lin' N) := by { ext, simp } lemma linear_map.to_matrix'_comp [decidable_eq l] (f : (n → R) →ₗ[R] (m → R)) (g : (l → R) →ₗ[R] (n → R)) : (f.comp g).to_matrix' = f.to_matrix' ⬝ g.to_matrix' := suffices (f.comp g) = (f.to_matrix' ⬝ g.to_matrix').to_lin', by rw [this, linear_map.to_matrix'_to_lin'], by rw [matrix.to_lin'_mul, matrix.to_lin'_to_matrix', matrix.to_lin'_to_matrix'] lemma linear_map.to_matrix'_mul [decidable_eq m] (f g : (m → R) →ₗ[R] (m → R)) : (f * g).to_matrix' = f.to_matrix' ⬝ g.to_matrix' := linear_map.to_matrix'_comp f g end to_matrix' section to_matrix variables {R : Type*} [comm_ring R] variables {l m n : Type*} [fintype l] [fintype m] [fintype n] [decidable_eq n] variables {M₁ M₂ : Type*} [add_comm_group M₁] [add_comm_group M₂] [module R M₁] [module R M₂] variables {v₁ : n → M₁} (hv₁ : is_basis R v₁) {v₂ : m → M₂} (hv₂ : is_basis R v₂) /-- Given bases of two modules `M₁` and `M₂` over a commutative ring `R`, we get a linear equivalence between linear maps `M₁ →ₗ M₂` and matrices over `R` indexed by the bases. -/ def linear_map.to_matrix : (M₁ →ₗ[R] M₂) ≃ₗ[R] matrix m n R := linear_equiv.trans (linear_equiv.arrow_congr hv₁.equiv_fun hv₂.equiv_fun) linear_map.to_matrix' /-- Given bases of two modules `M₁` and `M₂` over a commutative ring `R`, we get a linear equivalence between matrices over `R` indexed by the bases and linear maps `M₁ →ₗ M₂`. -/ def matrix.to_lin : matrix m n R ≃ₗ[R] (M₁ →ₗ[R] M₂) := (linear_map.to_matrix hv₁ hv₂).symm @[simp] lemma linear_map.to_matrix_symm : (linear_map.to_matrix hv₁ hv₂).symm = matrix.to_lin hv₁ hv₂ := rfl @[simp] lemma matrix.to_lin_symm : (matrix.to_lin hv₁ hv₂).symm = linear_map.to_matrix hv₁ hv₂ := rfl @[simp] lemma matrix.to_lin_to_matrix (f : M₁ →ₗ[R] M₂) : matrix.to_lin hv₁ hv₂ (linear_map.to_matrix hv₁ hv₂ f) = f := by rw [← matrix.to_lin_symm, linear_equiv.apply_symm_apply] @[simp] lemma linear_map.to_matrix_to_lin (M : matrix m n R) : linear_map.to_matrix hv₁ hv₂ (matrix.to_lin hv₁ hv₂ M) = M := by rw [← matrix.to_lin_symm, linear_equiv.symm_apply_apply] lemma linear_map.to_matrix_apply (f : M₁ →ₗ[R] M₂) (i : m) (j : n) : linear_map.to_matrix hv₁ hv₂ f i j = hv₂.equiv_fun (f (v₁ j)) i := begin rw [linear_map.to_matrix, linear_equiv.trans_apply, linear_map.to_matrix'_apply, linear_equiv.arrow_congr_apply, is_basis.equiv_fun_symm_apply, finset.sum_eq_single j, if_pos rfl, one_smul], { intros j' _ hj', rw [if_neg hj', zero_smul] }, { intro hj, have := finset.mem_univ j, contradiction } end lemma linear_map.to_matrix_apply' (f : M₁ →ₗ[R] M₂) (i : m) (j : n) : linear_map.to_matrix hv₁ hv₂ f i j = hv₂.repr (f (v₁ j)) i := linear_map.to_matrix_apply hv₁ hv₂ f i j lemma matrix.to_lin_apply (M : matrix m n R) (v : M₁) : matrix.to_lin hv₁ hv₂ M v = ∑ j, M.mul_vec (hv₁.equiv_fun v) j • v₂ j := show hv₂.equiv_fun.symm (matrix.to_lin' M (hv₁.equiv_fun v)) = _, by rw [matrix.to_lin'_apply, hv₂.equiv_fun_symm_apply] @[simp] lemma matrix.to_lin_self (M : matrix m n R) (i : n) : matrix.to_lin hv₁ hv₂ M (v₁ i) = ∑ j, M j i • v₂ j := by simp only [matrix.to_lin_apply, matrix.mul_vec, dot_product, hv₁.equiv_fun_self, mul_boole, finset.sum_ite_eq, finset.mem_univ, if_true] @[simp] lemma linear_map.to_matrix_id : linear_map.to_matrix hv₁ hv₁ id = 1 := begin ext i j, simp [linear_map.to_matrix_apply, is_basis.equiv_fun, matrix.one_apply, finsupp.single, eq_comm] end @[simp] lemma matrix.to_lin_one : matrix.to_lin hv₁ hv₁ 1 = id := by rw [← linear_map.to_matrix_id hv₁, matrix.to_lin_to_matrix] theorem linear_map.to_matrix_range [decidable_eq M₁] [decidable_eq M₂] (f : M₁ →ₗ[R] M₂) (k : m) (i : n) : linear_map.to_matrix hv₁.range hv₂.range f ⟨v₂ k, mem_range_self k⟩ ⟨v₁ i, mem_range_self i⟩ = linear_map.to_matrix hv₁ hv₂ f k i := by simp_rw [linear_map.to_matrix_apply, subtype.coe_mk, is_basis.equiv_fun_apply, hv₂.range_repr] variables {M₃ : Type*} [add_comm_group M₃] [module R M₃] {v₃ : l → M₃} (hv₃ : is_basis R v₃) lemma linear_map.to_matrix_comp [decidable_eq m] (f : M₂ →ₗ[R] M₃) (g : M₁ →ₗ[R] M₂) : linear_map.to_matrix hv₁ hv₃ (f.comp g) = linear_map.to_matrix hv₂ hv₃ f ⬝ linear_map.to_matrix hv₁ hv₂ g := by simp_rw [linear_map.to_matrix, linear_equiv.trans_apply, linear_equiv.arrow_congr_comp _ hv₂.equiv_fun, linear_map.to_matrix'_comp] lemma linear_map.to_matrix_mul (f g : M₁ →ₗ[R] M₁) : linear_map.to_matrix hv₁ hv₁ (f * g) = linear_map.to_matrix hv₁ hv₁ f ⬝ linear_map.to_matrix hv₁ hv₁ g := by { rw [show (@has_mul.mul (M₁ →ₗ[R] M₁) _) = linear_map.comp, from rfl, linear_map.to_matrix_comp hv₁ hv₁ hv₁ f g] } lemma matrix.to_lin_mul [decidable_eq m] (A : matrix l m R) (B : matrix m n R) : matrix.to_lin hv₁ hv₃ (A ⬝ B) = (matrix.to_lin hv₂ hv₃ A).comp (matrix.to_lin hv₁ hv₂ B) := begin apply (linear_map.to_matrix hv₁ hv₃).injective, haveI : decidable_eq l := λ _ _, classical.prop_decidable _, rw linear_map.to_matrix_comp hv₁ hv₂ hv₃, repeat { rw linear_map.to_matrix_to_lin }, end end to_matrix section is_basis_to_matrix variables {ι ι' : Type*} [fintype ι] [fintype ι'] variables {R M : Type*} [comm_ring R] [add_comm_group M] [module R M] open function matrix /-- From a basis `e : ι → M` and a family of vectors `v : ι' → M`, make the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def is_basis.to_matrix {e : ι → M} (he : is_basis R e) (v : ι' → M) : matrix ι ι' R := λ i j, he.equiv_fun (v j) i variables {e : ι → M} (he : is_basis R e) (v : ι' → M) (i : ι) (j : ι') namespace is_basis lemma to_matrix_apply : he.to_matrix v i j = he.equiv_fun (v j) i := rfl lemma to_matrix_eq_to_matrix_constr [decidable_eq ι] (v : ι → M) : he.to_matrix v = linear_map.to_matrix he he (he.constr v) := by { ext, simp [is_basis.to_matrix_apply, linear_map.to_matrix_apply] } @[simp] lemma to_matrix_self [decidable_eq ι] : he.to_matrix e = 1 := begin rw is_basis.to_matrix, ext i j, simp [is_basis.equiv_fun, matrix.one_apply, finsupp.single, eq_comm] end lemma to_matrix_update [decidable_eq ι'] (x : M) : he.to_matrix (function.update v j x) = matrix.update_column (he.to_matrix v) j (he.repr x) := begin ext i' k, rw [is_basis.to_matrix, matrix.update_column_apply, he.to_matrix_apply], split_ifs, { rw [h, update_same j x v, he.equiv_fun_apply] }, { rw update_noteq h }, end @[simp] lemma sum_to_matrix_smul_self : ∑ (i : ι), he.to_matrix v i j • e i = v j := begin conv_rhs { rw ← he.total_repr (v j) }, rw [finsupp.total_apply, finsupp.sum_fintype], { refl }, simp end @[simp] lemma to_lin_to_matrix [decidable_eq ι'] (hv : is_basis R v) : matrix.to_lin hv he (he.to_matrix v) = id := hv.ext (λ i, by rw [to_lin_self, id_apply, he.sum_to_matrix_smul_self]) /-- From a basis `e : ι → M`, build a linear equivalence between families of vectors `v : ι → M`, and matrices, making the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def to_matrix_equiv {e : ι → M} (he : is_basis R e) : (ι → M) ≃ₗ[R] matrix ι ι R := { to_fun := he.to_matrix, map_add' := λ v w, begin ext i j, change _ = _ + _, simp [he.to_matrix_apply] end, map_smul' := begin intros c v, ext i j, simp [he.to_matrix_apply] end, inv_fun := λ m j, ∑ i, (m i j) • e i, left_inv := begin intro v, ext j, simp [he.to_matrix_apply, he.equiv_fun_total (v j)] end, right_inv := begin intros x, ext k l, simp [he.to_matrix_apply, he.equiv_fun.map_sum, he.equiv_fun.map_smul, fintype.sum_apply k (λ i, x i l • he.equiv_fun (e i)), he.equiv_fun_self] end } end is_basis section mul_linear_map_to_matrix variables {N : Type*} [add_comm_group N] [module R N] variables {b : ι → M} {b' : ι' → M} {c : ι → N} {c' : ι' → N} variables (hb : is_basis R b) (hb' : is_basis R b') (hc : is_basis R c) (hc' : is_basis R c') variables (f : M →ₗ[R] N) @[simp] lemma is_basis_to_matrix_mul_linear_map_to_matrix [decidable_eq ι'] : hc.to_matrix c' ⬝ linear_map.to_matrix hb' hc' f = linear_map.to_matrix hb' hc f := (matrix.to_lin hb' hc).injective (by rw [to_lin_to_matrix, to_lin_mul hb' hc' hc, to_lin_to_matrix, hc.to_lin_to_matrix, id_comp]) @[simp] lemma linear_map_to_matrix_mul_is_basis_to_matrix [decidable_eq ι] [decidable_eq ι'] : linear_map.to_matrix hb' hc' f ⬝ hb'.to_matrix b = linear_map.to_matrix hb hc' f := (matrix.to_lin hb hc').injective (by rw [to_lin_to_matrix, to_lin_mul hb hb' hc', to_lin_to_matrix, hb'.to_lin_to_matrix, comp_id]) end mul_linear_map_to_matrix end is_basis_to_matrix open_locale matrix section det open linear_map matrix variables {R : Type} [comm_ring R] variables {M : Type*} [add_comm_group M] [module R M] variables {M' : Type*} [add_comm_group M'] [module R M'] variables {ι : Type*} [decidable_eq ι] [fintype ι] {v : ι → M} {v' : ι → M'} lemma linear_equiv.is_unit_det (f : M ≃ₗ[R] M') (hv : is_basis R v) (hv' : is_basis R v') : is_unit (linear_map.to_matrix hv hv' f).det := begin apply is_unit_det_of_left_inverse, simpa using (linear_map.to_matrix_comp hv hv' hv f.symm f).symm end /-- Builds a linear equivalence from a linear map whose determinant in some bases is a unit. -/ def linear_equiv.of_is_unit_det {f : M →ₗ[R] M'} {hv : is_basis R v} {hv' : is_basis R v'} (h : is_unit (linear_map.to_matrix hv hv' f).det) : M ≃ₗ[R] M' := { to_fun := f, map_add' := f.map_add, map_smul' := f.map_smul, inv_fun := to_lin hv' hv (to_matrix hv hv' f)⁻¹, left_inv := λ x, calc to_lin hv' hv (to_matrix hv hv' f)⁻¹ (f x) = to_lin hv hv ((to_matrix hv hv' f)⁻¹ ⬝ to_matrix hv hv' f) x : by { rw [to_lin_mul hv hv' hv, to_lin_to_matrix, linear_map.comp_apply] } ... = x : by simp [h], right_inv := λ x, calc f (to_lin hv' hv (to_matrix hv hv' f)⁻¹ x) = to_lin hv' hv' (to_matrix hv hv' f ⬝ (to_matrix hv hv' f)⁻¹) x : by { rw [to_lin_mul hv' hv hv', linear_map.comp_apply, to_lin_to_matrix hv hv'] } ... = x : by simp [h], } variables {e : ι → M} (he : is_basis R e) /-- The determinant of a family of vectors with respect to some basis, as a multilinear map. -/ def is_basis.det : multilinear_map R (λ i : ι, M) R := { to_fun := λ v, det (he.to_matrix v), map_add' := begin intros v i x y, simp only [he.to_matrix_update, linear_map.map_add], apply det_update_column_add end, map_smul' := begin intros u i c x, simp only [he.to_matrix_update, algebra.id.smul_eq_mul, map_smul_eq_smul_map], apply det_update_column_smul end } lemma is_basis.det_apply (v : ι → M) : he.det v = det (he.to_matrix v) := rfl lemma is_basis.det_self : he.det e = 1 := by simp [he.det_apply] lemma is_basis.iff_det {v : ι → M} : is_basis R v ↔ is_unit (he.det v) := begin split, { intro hv, suffices : is_unit (linear_map.to_matrix he he (equiv_of_is_basis he hv $ equiv.refl ι)).det, { rw [is_basis.det_apply, is_basis.to_matrix_eq_to_matrix_constr], exact this }, apply linear_equiv.is_unit_det }, { intro h, rw [is_basis.det_apply, is_basis.to_matrix_eq_to_matrix_constr] at h, convert linear_equiv.is_basis he (linear_equiv.of_is_unit_det h), ext i, exact (constr_basis he).symm }, end end det section transpose variables {K V₁ V₂ ι₁ ι₂ : Type*} [field K] [add_comm_group V₁] [vector_space K V₁] [add_comm_group V₂] [vector_space K V₂] [fintype ι₁] [fintype ι₂] [decidable_eq ι₁] [decidable_eq ι₂] {B₁ : ι₁ → V₁} (h₁ : is_basis K B₁) {B₂ : ι₂ → V₂} (h₂ : is_basis K B₂) @[simp] lemma linear_map.to_matrix_transpose (u : V₁ →ₗ[K] V₂) : linear_map.to_matrix h₂.dual_basis_is_basis h₁.dual_basis_is_basis (module.dual.transpose u) = (linear_map.to_matrix h₁ h₂ u)ᵀ := begin ext i j, simp only [linear_map.to_matrix_apply, module.dual.transpose_apply, h₁.dual_basis_equiv_fun, h₂.dual_basis_apply, matrix.transpose_apply, linear_map.comp_apply] end lemma linear_map.to_matrix_symm_transpose (M : matrix ι₁ ι₂ K) : (linear_map.to_matrix h₁.dual_basis_is_basis h₂.dual_basis_is_basis).symm Mᵀ = module.dual.transpose (matrix.to_lin h₂ h₁ M) := begin apply (linear_map.to_matrix h₁.dual_basis_is_basis h₂.dual_basis_is_basis).injective, rw [linear_equiv.apply_symm_apply], ext i j, simp only [linear_map.to_matrix_apply, module.dual.transpose_apply, h₂.dual_basis_equiv_fun, h₁.dual_basis_apply, matrix.transpose_apply, linear_map.comp_apply, if_true, matrix.to_lin_apply, linear_equiv.map_smul, mul_boole, algebra.id.smul_eq_mul, linear_equiv.map_sum, is_basis.equiv_fun_self, fintype.sum_apply, finset.sum_ite_eq', finset.sum_ite_eq, is_basis.equiv_fun_symm_apply, pi.smul_apply, matrix.to_lin_apply, matrix.mul_vec, matrix.dot_product, is_basis.equiv_fun_self, finset.mem_univ] end end transpose namespace matrix section trace variables {m : Type*} [fintype m] (n : Type*) [fintype n] variables (R : Type v) (M : Type w) [semiring R] [add_comm_monoid M] [semimodule R M] /-- The diagonal of a square matrix. -/ def diag : (matrix n n M) →ₗ[R] n → M := { to_fun := λ A i, A i i, map_add' := by { intros, ext, refl, }, map_smul' := by { intros, ext, refl, } } variables {n} {R} {M} @[simp] lemma diag_apply (A : matrix n n M) (i : n) : diag n R M A i = A i i := rfl @[simp] lemma diag_one [decidable_eq n] : diag n R R 1 = λ i, 1 := by { dunfold diag, ext, simp [one_apply_eq] } @[simp] lemma diag_transpose (A : matrix n n M) : diag n R M Aᵀ = diag n R M A := rfl variables (n) (R) (M) /-- The trace of a square matrix. -/ def trace : (matrix n n M) →ₗ[R] M := { to_fun := λ A, ∑ i, diag n R M A i, map_add' := by { intros, apply finset.sum_add_distrib, }, map_smul' := by { intros, simp [finset.smul_sum], } } variables {n} {R} {M} @[simp] lemma trace_diag (A : matrix n n M) : trace n R M A = ∑ i, diag n R M A i := rfl @[simp] lemma trace_one [decidable_eq n] : trace n R R 1 = fintype.card n := have h : trace n R R 1 = ∑ i, diag n R R 1 i := rfl, by simp_rw [h, diag_one, finset.sum_const, nsmul_one]; refl @[simp] lemma trace_transpose (A : matrix n n M) : trace n R M Aᵀ = trace n R M A := rfl @[simp] lemma trace_transpose_mul (A : matrix m n R) (B : matrix n m R) : trace n R R (Aᵀ ⬝ Bᵀ) = trace m R R (A ⬝ B) := finset.sum_comm lemma trace_mul_comm {S : Type v} [comm_ring S] (A : matrix m n S) (B : matrix n m S) : trace n S S (B ⬝ A) = trace m S S (A ⬝ B) := by rw [←trace_transpose, ←trace_transpose_mul, transpose_mul] end trace section ring variables {n : Type*} [fintype n] [decidable_eq n] {R : Type v} [comm_ring R] open linear_map matrix lemma proj_diagonal (i : n) (w : n → R) : (proj i).comp (to_lin' (diagonal w)) = (w i) • proj i := by ext j; simp [mul_vec_diagonal] lemma diagonal_comp_std_basis (w : n → R) (i : n) : (diagonal w).to_lin'.comp (std_basis R (λ_:n, R) i) = (w i) • std_basis R (λ_:n, R) i := begin ext j, simp_rw [linear_map.comp_apply, to_lin'_apply, mul_vec_diagonal, linear_map.smul_apply, pi.smul_apply, algebra.id.smul_eq_mul], by_cases i = j, { subst h }, { rw [std_basis_ne R (λ_:n, R) _ _ (ne.symm h), _root_.mul_zero, _root_.mul_zero] } end lemma diagonal_to_lin' (w : n → R) : (diagonal w).to_lin' = linear_map.pi (λi, w i • linear_map.proj i) := by ext v j; simp [mul_vec_diagonal] /-- An invertible matrix yields a linear equivalence from the free module to itself. -/ def to_linear_equiv (P : matrix n n R) (h : is_unit P) : (n → R) ≃ₗ[R] (n → R) := have h' : is_unit P.det := P.is_unit_iff_is_unit_det.mp h, { inv_fun := P⁻¹.to_lin', left_inv := λ v, show (P⁻¹.to_lin'.comp P.to_lin') v = v, by rw [← matrix.to_lin'_mul, P.nonsing_inv_mul h', matrix.to_lin'_one, linear_map.id_apply], right_inv := λ v, show (P.to_lin'.comp P⁻¹.to_lin') v = v, by rw [← matrix.to_lin'_mul, P.mul_nonsing_inv h', matrix.to_lin'_one, linear_map.id_apply], ..P.to_lin' } @[simp] lemma to_linear_equiv_apply (P : matrix n n R) (h : is_unit P) : (↑(P.to_linear_equiv h) : module.End R (n → R)) = P.to_lin' := rfl @[simp] lemma to_linear_equiv_symm_apply (P : matrix n n R) (h : is_unit P) : (↑(P.to_linear_equiv h).symm : module.End R (n → R)) = P⁻¹.to_lin' := rfl end ring section vector_space variables {m n : Type*} [fintype m] [fintype n] variables {K : Type u} [field K] -- maybe try to relax the universe constraint open linear_map matrix lemma rank_vec_mul_vec {m n : Type u} [fintype m] [fintype n] [decidable_eq n] (w : m → K) (v : n → K) : rank (vec_mul_vec w v).to_lin' ≤ 1 := begin rw [vec_mul_vec_eq, to_lin'_mul], refine le_trans (rank_comp_le1 _ _) _, refine le_trans (rank_le_domain _) _, rw [dim_fun', ← cardinal.lift_eq_nat_iff.mpr (cardinal.fintype_card unit), cardinal.mk_unit], exact le_of_eq (cardinal.lift_one) end lemma ker_diagonal_to_lin' [decidable_eq m] (w : m → K) : ker (diagonal w).to_lin' = (⨆i∈{i | w i = 0 }, range (std_basis K (λi, K) i)) := begin rw [← comap_bot, ← infi_ker_proj], simp only [comap_infi, (ker_comp _ _).symm, proj_diagonal, ker_smul'], have : univ ⊆ {i : m | w i = 0} ∪ {i : m | w i = 0}ᶜ, { rw set.union_compl_self }, exact (supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) (disjoint_compl_right {i | w i = 0}) this (finite.of_fintype _)).symm end lemma range_diagonal [decidable_eq m] (w : m → K) : (diagonal w).to_lin'.range = (⨆ i ∈ {i | w i ≠ 0}, (std_basis K (λi, K) i).range) := begin dsimp only [mem_set_of_eq], rw [← map_top, ← supr_range_std_basis, map_supr], congr, funext i, rw [← linear_map.range_comp, diagonal_comp_std_basis, ← range_smul'] end lemma rank_diagonal [decidable_eq m] [decidable_eq K] (w : m → K) : rank (diagonal w).to_lin' = fintype.card { i // w i ≠ 0 } := begin have hu : univ ⊆ {i : m | w i = 0}ᶜ ∪ {i : m | w i = 0}, { rw set.compl_union_self }, have hd : disjoint {i : m | w i ≠ 0} {i : m | w i = 0} := (disjoint_compl_right {i | w i = 0}).symm, have h₁ := supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) hd hu (finite.of_fintype _), have h₂ := @infi_ker_proj_equiv K _ _ (λi:m, K) _ _ _ _ (by simp; apply_instance) hd hu, rw [rank, range_diagonal, h₁, ←@dim_fun' K], apply linear_equiv.dim_eq, apply h₂, end end vector_space section finite_dimensional variables {m n : Type*} [fintype m] [fintype n] variables {R : Type v} [field R] instance : finite_dimensional R (matrix m n R) := linear_equiv.finite_dimensional (linear_equiv.uncurry R m n).symm /-- The dimension of the space of finite dimensional matrices is the product of the number of rows and columns. -/ @[simp] lemma findim_matrix : finite_dimensional.findim R (matrix m n R) = fintype.card m * fintype.card n := by rw [@linear_equiv.findim_eq R (matrix m n R) _ _ _ _ _ _ (linear_equiv.uncurry R m n), finite_dimensional.findim_fintype_fun_eq_card, fintype.card_prod] end finite_dimensional section reindexing variables {l m n : Type*} [fintype l] [fintype m] [fintype n] variables {l' m' n' : Type*} [fintype l'] [fintype m'] [fintype n'] variables {R : Type v} /-- The natural map that reindexes a matrix's rows and columns with equivalent types is an equivalence. -/ def reindex (eₘ : m ≃ m') (eₙ : n ≃ n') : matrix m n R ≃ matrix m' n' R := { to_fun := λ M i j, M (eₘ.symm i) (eₙ.symm j), inv_fun := λ M i j, M (eₘ i) (eₙ j), left_inv := λ M, by simp, right_inv := λ M, by simp, } @[simp] lemma reindex_apply (eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m n R) : reindex eₘ eₙ M = λ i j, M (eₘ.symm i) (eₙ.symm j) := rfl @[simp] lemma reindex_symm_apply (eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m' n' R) : (reindex eₘ eₙ).symm M = λ i j, M (eₘ i) (eₙ j) := rfl /-- The natural map that reindexes a matrix's rows and columns with equivalent types is a linear equivalence. -/ def reindex_linear_equiv [semiring R] (eₘ : m ≃ m') (eₙ : n ≃ n') : matrix m n R ≃ₗ[R] matrix m' n' R := { map_add' := λ M N, rfl, map_smul' := λ M N, rfl, ..(reindex eₘ eₙ)} @[simp] lemma reindex_linear_equiv_apply [semiring R] (eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m n R) : reindex_linear_equiv eₘ eₙ M = λ i j, M (eₘ.symm i) (eₙ.symm j) := rfl @[simp] lemma reindex_linear_equiv_symm_apply [semiring R] (eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m' n' R) : (reindex_linear_equiv eₘ eₙ).symm M = λ i j, M (eₘ i) (eₙ j) := rfl lemma reindex_mul [semiring R] (eₘ : m ≃ m') (eₙ : n ≃ n') (eₗ : l ≃ l') (M : matrix m n R) (N : matrix n l R) : (reindex_linear_equiv eₘ eₙ M) ⬝ (reindex_linear_equiv eₙ eₗ N) = reindex_linear_equiv eₘ eₗ (M ⬝ N) := begin ext i j, dsimp only [matrix.mul, matrix.dot_product], rw [←finset.univ_map_equiv_to_embedding eₙ, finset.sum_map finset.univ eₙ.to_embedding], simp, end /-- For square matrices, the natural map that reindexes a matrix's rows and columns with equivalent types is an equivalence of algebras. -/ def reindex_alg_equiv [comm_semiring R] [decidable_eq m] [decidable_eq n] (e : m ≃ n) : matrix m m R ≃ₐ[R] matrix n n R := { map_mul' := λ M N, by simp only [reindex_mul, linear_equiv.to_fun_apply, mul_eq_mul], commutes' := λ r, by { ext, simp [algebra_map, algebra.to_ring_hom], by_cases h : i = j; simp [h], }, ..(reindex_linear_equiv e e) } @[simp] lemma reindex_alg_equiv_apply [comm_semiring R] [decidable_eq m] [decidable_eq n] (e : m ≃ n) (M : matrix m m R) : reindex_alg_equiv e M = λ i j, M (e.symm i) (e.symm j) := rfl @[simp] lemma reindex_alg_equiv_symm_apply [comm_semiring R] [decidable_eq m] [decidable_eq n] (e : m ≃ n) (M : matrix n n R) : (reindex_alg_equiv e).symm M = λ i j, M (e i) (e j) := rfl lemma reindex_transpose (eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m n R) : (reindex eₘ eₙ M)ᵀ = (reindex eₙ eₘ Mᵀ) := rfl end reindexing end matrix namespace linear_map open_locale matrix /-- The trace of an endomorphism given a basis. -/ def trace_aux (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ι : Type w} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b) : (M →ₗ[R] M) →ₗ[R] R := (matrix.trace ι R R).comp $ linear_map.to_matrix hb hb @[simp] lemma trace_aux_def (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ι : Type w} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b) (f : M →ₗ[R] M) : trace_aux R hb f = matrix.trace ι R R (linear_map.to_matrix hb hb f) := rfl theorem trace_aux_eq' (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ι : Type w} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b) {κ : Type w} [decidable_eq κ] [fintype κ] {c : κ → M} (hc : is_basis R c) : trace_aux R hb = trace_aux R hc := linear_map.ext $ λ f, calc matrix.trace ι R R (linear_map.to_matrix hb hb f) = matrix.trace ι R R (linear_map.to_matrix hb hb ((linear_map.id.comp f).comp linear_map.id)) : by rw [linear_map.id_comp, linear_map.comp_id] ... = matrix.trace ι R R (linear_map.to_matrix hc hb linear_map.id ⬝ linear_map.to_matrix hc hc f ⬝ linear_map.to_matrix hb hc linear_map.id) : by rw [linear_map.to_matrix_comp _ hc, linear_map.to_matrix_comp _ hc] ... = matrix.trace κ R R (linear_map.to_matrix hc hc f ⬝ linear_map.to_matrix hb hc linear_map.id ⬝ linear_map.to_matrix hc hb linear_map.id) : by rw [matrix.mul_assoc, matrix.trace_mul_comm] ... = matrix.trace κ R R (linear_map.to_matrix hc hc ((f.comp linear_map.id).comp linear_map.id)) : by rw [linear_map.to_matrix_comp _ hb, linear_map.to_matrix_comp _ hc] ... = matrix.trace κ R R (linear_map.to_matrix hc hc f) : by rw [linear_map.comp_id, linear_map.comp_id] open_locale classical theorem trace_aux_range (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ι : Type w} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b) : trace_aux R hb.range = trace_aux R hb := linear_map.ext $ λ f, if H : 0 = 1 then eq_of_zero_eq_one H _ _ else begin haveI : nontrivial R := ⟨⟨0, 1, H⟩⟩, change ∑ i : set.range b, _ = ∑ i : ι, _, simp_rw [matrix.diag_apply], symmetry, convert (equiv.of_injective _ hb.injective).sum_comp _, ext i, exact (linear_map.to_matrix_range hb hb f i i).symm end /-- where `ι` and `κ` can reside in different universes -/ theorem trace_aux_eq (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ι : Type*} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b) {κ : Type*} [decidable_eq κ] [fintype κ] {c : κ → M} (hc : is_basis R c) : trace_aux R hb = trace_aux R hc := calc trace_aux R hb = trace_aux R hb.range : by rw trace_aux_range R hb ... = trace_aux R hc.range : trace_aux_eq' _ _ _ ... = trace_aux R hc : by rw trace_aux_range R hc /-- Trace of an endomorphism independent of basis. -/ def trace (R : Type u) [comm_ring R] (M : Type v) [add_comm_group M] [module R M] : (M →ₗ[R] M) →ₗ[R] R := if H : ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M) then trace_aux R (classical.some_spec H) else 0 theorem trace_eq_matrix_trace (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ι : Type w} [fintype ι] [decidable_eq ι] {b : ι → M} (hb : is_basis R b) (f : M →ₗ[R] M) : trace R M f = matrix.trace ι R R (linear_map.to_matrix hb hb f) := have ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M), from ⟨finset.univ.image b, by { rw [finset.coe_image, finset.coe_univ, set.image_univ], exact hb.range }⟩, by { rw [trace, dif_pos this, ← trace_aux_def], congr' 1, apply trace_aux_eq } theorem trace_mul_comm (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] (f g : M →ₗ[R] M) : trace R M (f * g) = trace R M (g * f) := if H : ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M) then let ⟨s, hb⟩ := H in by { simp_rw [trace_eq_matrix_trace R hb, linear_map.to_matrix_mul], apply matrix.trace_mul_comm } else by rw [trace, dif_neg H, linear_map.zero_apply, linear_map.zero_apply] section finite_dimensional variables {K : Type*} [field K] variables {V : Type*} [add_comm_group V] [vector_space K V] [finite_dimensional K V] variables {W : Type*} [add_comm_group W] [vector_space K W] [finite_dimensional K W] instance : finite_dimensional K (V →ₗ[K] W) := begin classical, cases finite_dimensional.exists_is_basis_finset K V with bV hbV, cases finite_dimensional.exists_is_basis_finset K W with bW hbW, apply linear_equiv.finite_dimensional (linear_map.to_matrix hbV hbW).symm, end /-- The dimension of the space of linear transformations is the product of the dimensions of the domain and codomain. -/ @[simp] lemma findim_linear_map : finite_dimensional.findim K (V →ₗ[K] W) = (finite_dimensional.findim K V) * (finite_dimensional.findim K W) := begin classical, cases finite_dimensional.exists_is_basis_finset K V with bV hbV, cases finite_dimensional.exists_is_basis_finset K W with bW hbW, rw [linear_equiv.findim_eq (linear_map.to_matrix hbV hbW), matrix.findim_matrix, finite_dimensional.findim_eq_card_basis hbV, finite_dimensional.findim_eq_card_basis hbW, mul_comm], end end finite_dimensional end linear_map /-- The natural equivalence between linear endomorphisms of finite free modules and square matrices is compatible with the algebra structures. -/ def alg_equiv_matrix' {R : Type v} [comm_ring R] {n : Type*} [fintype n] [decidable_eq n] : module.End R (n → R) ≃ₐ[R] matrix n n R := { map_mul' := linear_map.to_matrix'_comp, map_add' := linear_map.to_matrix'.map_add, commutes' := λ r, by { change (r • (linear_map.id : module.End R _)).to_matrix' = r • 1, rw ←linear_map.to_matrix'_id, refl, }, ..linear_map.to_matrix' } /-- A linear equivalence of two modules induces an equivalence of algebras of their endomorphisms. -/ def linear_equiv.alg_conj {R : Type v} [comm_ring R] {M₁ M₂ : Type*} [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] (e : M₁ ≃ₗ[R] M₂) : module.End R M₁ ≃ₐ[R] module.End R M₂ := { map_mul' := λ f g, by apply e.arrow_congr_comp, map_add' := e.conj.map_add, commutes' := λ r, by { change e.conj (r • linear_map.id) = r • linear_map.id, rw [linear_equiv.map_smul, linear_equiv.conj_id], }, ..e.conj } /-- A basis of a module induces an equivalence of algebras from the endomorphisms of the module to square matrices. -/ def alg_equiv_matrix {R : Type v} {M : Type w} {n : Type*} [fintype n] [comm_ring R] [add_comm_group M] [module R M] [decidable_eq n] {b : n → M} (h : is_basis R b) : module.End R M ≃ₐ[R] matrix n n R := h.equiv_fun.alg_conj.trans alg_equiv_matrix'
f441a79677ada0413f5fb35d40828a79dc8761d4
9028d228ac200bbefe3a711342514dd4e4458bff
/src/order/lexicographic.lean
1bcd573a9b3baf4d4ed4721a06abc99a7a13ef5a
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,668
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Scott Morrison, Minchao Wu Lexicographic preorder / partial_order / linear_order / decidable_linear_order, for pairs and dependent pairs. -/ import tactic.basic universes u v /-- The cartesian product, equipped with the lexicographic order. -/ def lex (α : Type u) (β : Type v) := α × β variables {α : Type u} {β : Type v} instance [decidable_eq α] [decidable_eq β] : decidable_eq (lex α β) := prod.decidable_eq instance [inhabited α] [inhabited β] : inhabited (lex α β) := prod.inhabited /-- Dictionary / lexicographic ordering on pairs. -/ instance lex_has_le [preorder α] [preorder β] : has_le (lex α β) := { le := prod.lex (<) (≤) } instance lex_has_lt [preorder α] [preorder β] : has_lt (lex α β) := { lt := prod.lex (<) (<) } /-- Dictionary / lexicographic preorder for pairs. -/ instance lex_preorder [preorder α] [preorder β] : preorder (lex α β) := { le_refl := λ ⟨l, r⟩, by { right, apply le_refl }, le_trans := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ ⟨a₃, b₃⟩ ⟨h₁l, h₁r⟩ ⟨h₂l, h₂r⟩, { left, apply lt_trans, repeat { assumption } }, { left, assumption }, { left, assumption }, { right, apply le_trans, repeat { assumption } } end, lt_iff_le_not_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, split, { rintros (⟨_, _, _, _, hlt⟩ | ⟨_, _, _, hlt⟩), { split, { left, assumption }, { rintro ⟨l,r⟩, { apply lt_asymm hlt, assumption }, { apply lt_irrefl _ hlt } } }, { split, { right, rw lt_iff_le_not_le at hlt, exact hlt.1 }, { rintro ⟨l,r⟩, { apply lt_irrefl a₁, assumption }, { rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } }, { rintros ⟨⟨h₁ll, h₁lr⟩, h₂r⟩, { left, assumption }, { right, rw lt_iff_le_not_le, split, { assumption }, { intro h, apply h₂r, right, exact h } } } end, .. lex_has_le, .. lex_has_lt } /-- Dictionary / lexicographic partial_order for pairs. -/ instance lex_partial_order [partial_order α] [partial_order β] : partial_order (lex α β) := { le_antisymm := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ (⟨_, _, _, _, hlt₁⟩ | ⟨_, _, _, hlt₁⟩) (⟨_, _, _, _, hlt₂⟩ | ⟨_, _, _, hlt₂⟩), { exfalso, exact lt_irrefl a₁ (lt_trans hlt₁ hlt₂) }, { exfalso, exact lt_irrefl a₁ hlt₁ }, { exfalso, exact lt_irrefl a₁ hlt₂ }, { have := le_antisymm hlt₁ hlt₂, simp [this] } end .. lex_preorder } /-- Dictionary / lexicographic linear_order for pairs. -/ instance lex_linear_order [linear_order α] [linear_order β] : linear_order (lex α β) := { le_total := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, rcases le_total a₁ a₂ with ha | ha; cases lt_or_eq_of_le ha with a_lt a_eq, -- Deal with the two goals with a₁ ≠ a₂ { left, left, exact a_lt }, swap, { right, left, exact a_lt }, -- Now deal with the two goals with a₁ = a₂ all_goals { subst a_eq, rcases le_total b₁ b₂ with hb | hb }, { left, right, exact hb }, { right, right, exact hb }, { left, right, exact hb }, { right, right, exact hb }, end .. lex_partial_order }. /-- Dictionary / lexicographic decidable_linear_order for pairs. -/ instance lex_decidable_linear_order [decidable_linear_order α] [decidable_linear_order β] : decidable_linear_order (lex α β) := { decidable_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, rcases decidable_linear_order.decidable_le a₁ a₂ with a_lt | a_le, { -- a₂ < a₁ left, rw not_le at a_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, apply lt_trans, repeat { assumption } }, { apply lt_irrefl a₁, assumption } }, { -- a₁ ≤ a₂ by_cases h : a₁ = a₂, { rw h, rcases decidable_linear_order.decidable_le b₁ b₂ with b_lt | b_le, { -- b₂ < b₁ left, rw not_le at b_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, assumption }, { apply lt_irrefl b₂, apply lt_of_lt_of_le, repeat { assumption } } }, -- b₁ ≤ b₂ { right, right, assumption } }, -- a₁ < a₂ { right, left, apply lt_of_le_of_ne, repeat { assumption } } } end, .. lex_linear_order } variables {Z : α → Type v} /-- Dictionary / lexicographic ordering on dependent pairs. The 'pointwise' partial order `prod.has_le` doesn't make sense for dependent pairs, so it's safe to mark these as instances here. -/ instance dlex_has_le [preorder α] [∀ a, preorder (Z a)] : has_le (Σ' a, Z a) := { le := psigma.lex (<) (λ a, (≤)) } instance dlex_has_lt [preorder α] [∀ a, preorder (Z a)] : has_lt (Σ' a, Z a) := { lt := psigma.lex (<) (λ a, (<)) } /-- Dictionary / lexicographic preorder on dependent pairs. -/ instance dlex_preorder [preorder α] [∀ a, preorder (Z a)] : preorder (Σ' a, Z a) := { le_refl := λ ⟨l, r⟩, by { right, apply le_refl }, le_trans := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ ⟨a₃, b₃⟩ ⟨h₁l, h₁r⟩ ⟨h₂l, h₂r⟩, { left, apply lt_trans, repeat { assumption } }, { left, assumption }, { left, assumption }, { right, apply le_trans, repeat { assumption } } end, lt_iff_le_not_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, split, { rintros (⟨_, _, _, _, hlt⟩ | ⟨_, _, _, hlt⟩), { split, { left, assumption }, { rintro ⟨l,r⟩, { apply lt_asymm hlt, assumption }, { apply lt_irrefl _ hlt } } }, { split, { right, rw lt_iff_le_not_le at hlt, exact hlt.1 }, { rintro ⟨l,r⟩, { apply lt_irrefl a₁, assumption }, { rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } }, { rintros ⟨⟨h₁ll, h₁lr⟩, h₂r⟩, { left, assumption }, { right, rw lt_iff_le_not_le, split, { assumption }, { intro h, apply h₂r, right, exact h } } } end, .. dlex_has_le, .. dlex_has_lt } /-- Dictionary / lexicographic partial_order for dependent pairs. -/ instance dlex_partial_order [partial_order α] [∀ a, partial_order (Z a)] : partial_order (Σ' a, Z a) := { le_antisymm := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ (⟨_, _, _, _, hlt₁⟩ | ⟨_, _, _, hlt₁⟩) (⟨_, _, _, _, hlt₂⟩ | ⟨_, _, _, hlt₂⟩), { exfalso, exact lt_irrefl a₁ (lt_trans hlt₁ hlt₂) }, { exfalso, exact lt_irrefl a₁ hlt₁ }, { exfalso, exact lt_irrefl a₁ hlt₂ }, { have := le_antisymm hlt₁ hlt₂, simp [this] } end .. dlex_preorder } /-- Dictionary / lexicographic linear_order for pairs. -/ instance dlex_linear_order [linear_order α] [∀ a, linear_order (Z a)] : linear_order (Σ' a, Z a) := { le_total := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, rcases le_total a₁ a₂ with ha | ha; cases lt_or_eq_of_le ha with a_lt a_eq, -- Deal with the two goals with a₁ ≠ a₂ { left, left, exact a_lt }, swap, { right, left, exact a_lt }, -- Now deal with the two goals with a₁ = a₂ all_goals { subst a_eq, rcases le_total b₁ b₂ with hb | hb }, { left, right, exact hb }, { right, right, exact hb }, { left, right, exact hb }, { right, right, exact hb }, end .. dlex_partial_order }. /-- Dictionary / lexicographic decidable_linear_order for dependent pairs. -/ instance dlex_decidable_linear_order [decidable_linear_order α] [∀ a, decidable_linear_order (Z a)] : decidable_linear_order (Σ' a, Z a) := { decidable_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, rcases decidable_linear_order.decidable_le a₁ a₂ with a_lt | a_le, { -- a₂ < a₁ left, rw not_le at a_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, apply lt_trans, repeat { assumption } }, { apply lt_irrefl a₁, assumption } }, { -- a₁ ≤ a₂ by_cases h : a₁ = a₂, { subst h, rcases decidable_linear_order.decidable_le b₁ b₂ with b_lt | b_le, { -- b₂ < b₁ left, rw not_le at b_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₁, assumption }, { apply lt_irrefl b₂, apply lt_of_lt_of_le, repeat { assumption } } }, -- b₁ ≤ b₂ { right, right, assumption } }, -- a₁ < a₂ { right, left, apply lt_of_le_of_ne, repeat { assumption } } } end, .. dlex_linear_order }
469dbdf1a9981ec46167c8ed27e54126f4dbdf26
ac49064e8a9a038e07cf5574b4fccd8a70d115c8
/hott/types/int/hott.hlean
90d4f691f89094f7ad206842abcb2a20b8d4998f
[ "Apache-2.0" ]
permissive
Bolt64/lean2
7c75016729569e04a3f403c7a4fc7c1de4377c9d
75fd8162488214a959dbe3303a185cbbb83f60f9
refs/heads/master
1,611,290,445,156
1,493,763,922,000
1,493,763,922,000
81,566,307
0
0
null
1,486,732,167,000
1,486,732,167,000
null
UTF-8
Lean
false
false
6,281
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Theorems about the integers specific to HoTT -/ import .basic types.eq arity algebra.bundled open core eq is_equiv equiv algebra is_trunc open nat (hiding pred) namespace int section open algebra /- we make these structures reducible, so that n * m in gℤ and agℤ can be interpreted as multiplication on ℤ. For this it's needed that the carriers of gℤ and agℤ reduce to ℤ unfolding only reducible definitions. -/ definition group_integers [reducible] [constructor] : AddGroup := AddGroup.mk ℤ _ notation `gℤ` := group_integers definition AbGroup_int [reducible] [constructor] : AddAbGroup := AddAbGroup.mk ℤ _ notation `agℤ` := AbGroup_int end definition is_equiv_succ [constructor] [instance] : is_equiv succ := adjointify succ pred (λa, !add_sub_cancel) (λa, !sub_add_cancel) definition equiv_succ [constructor] : ℤ ≃ ℤ := equiv.mk succ _ definition is_equiv_neg [constructor] [instance] : is_equiv (neg : ℤ → ℤ) := adjointify neg neg (λx, !neg_neg) (λa, !neg_neg) definition equiv_neg [constructor] : ℤ ≃ ℤ := equiv.mk neg _ definition iterate {A : Type} (f : A ≃ A) (a : ℤ) : A ≃ A := rec_nat_on a erfl (λb g, f ⬝e g) (λb g, g ⬝e f⁻¹ᵉ) -- definition iterate_trans {A : Type} (f : A ≃ A) (a : ℤ) -- : iterate f a ⬝e f = iterate f (a + 1) := -- sorry -- definition trans_iterate {A : Type} (f : A ≃ A) (a : ℤ) -- : f ⬝e iterate f a = iterate f (a + 1) := -- sorry -- definition iterate_trans_symm {A : Type} (f : A ≃ A) (a : ℤ) -- : iterate f a ⬝e f⁻¹e = iterate f (a - 1) := -- sorry -- definition symm_trans_iterate {A : Type} (f : A ≃ A) (a : ℤ) -- : f⁻¹e ⬝e iterate f a = iterate f (a - 1) := -- sorry -- definition iterate_neg {A : Type} (f : A ≃ A) (a : ℤ) -- : iterate f (-a) = (iterate f a)⁻¹e := -- rec_nat_on a idp -- (λn p, calc -- iterate f (-succ n) = iterate f (-n) ⬝e f⁻¹e : rec_nat_on_neg -- ... = (iterate f n)⁻¹e ⬝e f⁻¹e : by rewrite p -- ... = (f ⬝e iterate f n)⁻¹e : sorry -- ... = (iterate f (succ n))⁻¹e : idp) -- sorry -- definition iterate_add {A : Type} (f : A ≃ A) (a b : ℤ) -- : iterate f (a + b) = equiv.trans (iterate f a) (iterate f b) := -- sorry -- definition iterate_sub {A : Type} (f : A ≃ A) (a b : ℤ) -- : iterate f (a - b) = equiv.trans (iterate f a) (equiv.symm (iterate f b)) := -- sorry -- definition iterate_mul {A : Type} (f : A ≃ A) (a b : ℤ) -- : iterate f (a * b) = iterate (iterate f a) b := -- sorry end int open int namespace eq variables {A : Type} {a : A} (p : a = a) (b c : ℤ) (n : ℕ) definition power : a = a := rec_nat_on b idp (λc q, q ⬝ p) (λc q, q ⬝ p⁻¹) --iterate (equiv_eq_closed_right p a) b idp -- definition power_neg_succ (n : ℕ) : power p (-succ n) = power p (-n) ⬝ p⁻¹ := -- !rec_nat_on_neg -- local attribute nat.add int.add int.of_num nat.of_num int.succ [constructor] definition power_con : power p b ⬝ p = power p (succ b) := rec_nat_on b idp (λn IH, idp) (λn IH, calc power p (-succ n) ⬝ p = (power p (-int.of_nat n) ⬝ p⁻¹) ⬝ p : by krewrite [↑power,rec_nat_on_neg] ... = power p (-int.of_nat n) : inv_con_cancel_right ... = power p (succ (-succ n)) : by rewrite -succ_neg_succ) definition power_con_inv : power p b ⬝ p⁻¹ = power p (pred b) := rec_nat_on b idp (λn IH, calc power p (succ n) ⬝ p⁻¹ = power p n : by apply con_inv_cancel_right ... = power p (pred (succ n)) : by rewrite pred_nat_succ) (λn IH, calc power p (-int.of_nat (succ n)) ⬝ p⁻¹ = power p (-int.of_nat (succ (succ n))) : by krewrite [↑power,*rec_nat_on_neg] ... = power p (pred (-succ n)) : by rewrite -neg_succ) definition con_power : p ⬝ power p b = power p (succ b) := rec_nat_on b ( by rewrite ↑[power];exact !idp_con⁻¹) ( λn IH, proof calc p ⬝ power p (succ n) = (p ⬝ power p n) ⬝ p : con.assoc p _ p ... = power p (succ (succ n)) : by rewrite IH qed) ( λn IH, calc p ⬝ power p (-int.of_nat (succ n)) = p ⬝ (power p (-int.of_nat n) ⬝ p⁻¹) : by rewrite [↑power, rec_nat_on_neg] ... = (p ⬝ power p (-int.of_nat n)) ⬝ p⁻¹ : con.assoc ... = power p (succ (-int.of_nat n)) ⬝ p⁻¹ : by rewrite IH ... = power p (pred (succ (-int.of_nat n))) : power_con_inv ... = power p (succ (-int.of_nat (succ n))) : by rewrite [succ_neg_nat_succ,int.pred_succ]) definition inv_con_power : p⁻¹ ⬝ power p b = power p (pred b) := rec_nat_on b ( by rewrite ↑[power];exact !idp_con⁻¹) (λn IH, calc p⁻¹ ⬝ power p (succ n) = p⁻¹ ⬝ power p n ⬝ p : con.assoc ... = power p (pred n) ⬝ p : by rewrite IH ... = power p (succ (pred n)) : power_con ... = power p (pred (succ n)) : by rewrite [succ_pred,-int.pred_succ n]) ( λn IH, calc p⁻¹ ⬝ power p (-int.of_nat (succ n)) = p⁻¹ ⬝ (power p (-int.of_nat n) ⬝ p⁻¹) : by rewrite [↑power,rec_nat_on_neg] ... = (p⁻¹ ⬝ power p (-int.of_nat n)) ⬝ p⁻¹ : con.assoc ... = power p (pred (-int.of_nat n)) ⬝ p⁻¹ : by rewrite IH ... = power p (-int.of_nat (succ n)) ⬝ p⁻¹ : by rewrite -neg_succ ... = power p (-succ (succ n)) : by krewrite [↑power,*rec_nat_on_neg] ... = power p (pred (-succ n)) : by rewrite -neg_succ) definition power_con_power : Π(b : ℤ), power p b ⬝ power p c = power p (b + c) := rec_nat_on c (λb, by rewrite int.add_zero) (λn IH b, by rewrite [-con_power,-con.assoc,power_con,IH,↑succ,add.assoc, add.comm (int.of_nat n)]) (λn IH b, by rewrite [neg_nat_succ,-inv_con_power,-con.assoc,power_con_inv,IH,↑pred, +sub_eq_add_neg,add.assoc,add.comm (-n)]) end eq
86d24f7c607d4f7123d206185a17d62c1247254e
76df16d6c3760cb415f1294caee997cc4736e09b
/lean/src/sym_factory/sym_factory.lean
b710f03ca13a28dab2c430c355ce44428e9def92
[ "MIT" ]
permissive
uw-unsat/leanette-popl22-artifact
70409d9cbd8921d794d27b7992bf1d9a4087e9fe
80fea2519e61b45a283fbf7903acdf6d5528dbe7
refs/heads/master
1,681,592,449,670
1,637,037,431,000
1,637,037,431,000
414,331,908
6
1
null
null
null
null
UTF-8
Lean
false
false
7,022
lean
import ..tactic.all import ..cs.lang import ..cs.sym import ..cs.conc import ..op.sym import ..cs.util import .defs import ..cs.lib import ..op.simp import ..basic.basic import ..basic.sym import ..op.basic import .basic import .opS namespace sym_factory open sym open op.sym open op.lang.op lemma cast_choices.sound.valid_input {m : model} (gvs : choices' val_atom) (w : op.lang.val) (h_clos : lang.val.is_clos w) (h_e : choices.eval (make_ev evalVA) m op.lang.undef gvs = w) (h_one : choices.one (make_ev evalVA) m gvs) : choices.one ev.clos m (cast_choices gvs) ∧ choices.eval ev.clos m w (cast_choices gvs) = w := begin obtain ⟨pre, post, g, va, h_part, h_pre, h_post, h_true⟩ := choices.one_implies_one_true h_one, simp only [h_part, cast_choices.append], simp only [ h_part, choices.eval_skips_false h_pre, sym.choices.eval, h_true, if_true, list.singleton_append ] at h_e, replace h_pre := cast_choices.preserves_none h_pre, replace h_post := cast_choices.preserves_none h_post, simp only [choices.one, choices.true, list.map_append, list.filter_append], cases va, case term : τ t { cases τ, case bool { cases bool.evals_to_bool h_e with _ h_w, subst h_w, contradiction, }, case int { cases int.evals_to_int h_e with _ h_w, subst h_w, contradiction, }, }, case list { replace h_e := list.evals_to_list h_e, cases w, all_goals { contra at h_e h_clos }, }, case clos { split_c, case left { simp only [choices.none, choices.true] at h_pre h_post, simp only [h_pre, h_post, add_zero, list.length_append, zero_add], simp [cast_choices, list.filter, ev.clos, h_true], }, case right { simp only [choices.eval_skips_false h_pre, cast_choices], simp only [evalVA] at h_e, simp [sym.choices.eval, ev.clos, h_true, ← clos.eval_lift, h_e], }, }, end lemma cast_choices.sound.invalid_input {m : model} {gvs : choices' val_atom} {w : op.lang.val} (h : gvs.eval ev.atom m op.lang.undef = w) (h_clos : ¬ w.is_clos) (h_lone : gvs.lone ev.atom m) : (cast_choices gvs).none ev.clos m := begin cases @choices.lone_implies_one_or_none _ _ _ _ ev.atom _ _ h_lone, case inl : h_none { apply cast_choices.preserves_none, assumption, }, case inr : h_one { obtain ⟨pre, post, g, va, h_part, h_pre, h_post, h_true⟩ := choices.one_implies_one_true h_one, simp only [h_part] at ⊢ h, simp only [cast_choices.append, ← choices.none_append], split_c, case left { apply cast_choices.preserves_none, assumption }, case right left { cases va, case clos { rw choices.eval_skips_false h_pre at h, simp only [sym.choices.eval, h_true, if_true, list.singleton_append] at h, simp only [ev.atom, evalVA] at h, replace h := clos.evals_to_clos h, contra [h] at h_clos, }, all_goals { simp [cast_choices, choices.none, choices.true] }, }, case right right { apply cast_choices.preserves_none, assumption }, }, end lemma cast.sound.valid_input {m : model} {v : val} (h_clos : (evalV m v).is_clos) : (cast v).one ev.clos m ∧ (cast v).eval ev.clos m (evalV m v) = evalV m v := begin cases v, case atom { generalize_hyp h : evalV _ _ = w at h_clos, simp only [evalV, val.eval] at h, cases v, case clos { simp [ cast, choices.one, choices.true, list.filter, ev.clos, mk_tt.sound, sym.choices.eval, evalV, val.eval, clos.eval_lift ], }, case term : τ t { cases τ, case bool { cases bool.evals_to_bool h with _ h', contra [h'] at h_clos, }, case int { cases int.evals_to_int h with _ h', contra [h'] at h_clos, }, }, case list { replace h := list.evals_to_list h, cases w, all_goals { contra at h_clos h }, }, }, case union : gvs { generalize_hyp h_e : evalV m (val.union gvs) = w at h_clos ⊢, simp only [cast], simp only [evalV, val.eval] at h_e, simp only [choices.eval_lift, ev.atom] at h_e, rw ← adjust.sound at h_e, have h_eval : choices.eval ev.atom m op.lang.undef (adjust gvs) ≠ op.lang.undef := by { simp only [ev.atom], cases choices.eval (make_ev evalVA) m op.lang.undef (adjust gvs), case clos { simp [op.lang.undef] }, all_goals { subst_vars, contradiction }, }, replace h_eval := choices.not_none_implies_not_none h_eval, have h_lone := @adjust.lone _ _ m gvs evalVA, simp only [ev.atom] at h_eval, replace h_lone := choices.lone_implies_one_or_none h_lone, simp only [h_eval, false_or] at h_lone, apply cast_choices.sound.valid_input, all_goals { assumption }, }, end lemma cast.sound.invalid_input {m : model} {v : val} (h_clos : ¬(evalV m v).is_clos) : (cast v).none ev.clos m := begin cases v, case atom { cases v, case clos { generalize_hyp h : evalV _ _ = w at h_clos, simp only [evalV, val.eval] at h, contra [clos.evals_to_clos h] at h_clos, }, all_goals { simp [cast, choices.none, choices.true] }, }, case union : gvs { simp only [cast], generalize_hyp h : evalV _ _ = w at h_clos, simp only [evalV] at h, simp only [val.eval, choices.eval_lift, ev.atom] at h, rw ← @adjust.sound _ _ _ _ evalVA at h, apply_c cast_choices.sound.invalid_input, case h_clos { assumption }, case h { assumption }, case h_lone { exact @adjust.lone _ _ _ _ evalVA }, }, end -- A complete symbolic factory @[reducible] def sym_factory : factory _ _ _ _ _ := { mk_tt := mk_tt, mk_ff := mk_ff, is_tt := is_tt, is_ff := is_ff, not := pe_not, and := pe_and, or := pe_or, imp := pe_imp, truth := truth, bval := bval, dval := dval, cval := cval, cast := cast, merge := merge, opC := op.lang.opC, opS := opS, evalB := op.sym.evalB, evalV := op.sym.evalV, mk_tt_sound := by { simp [mk_tt.sound] }, mk_ff_sound := by { simp [mk_ff.sound] }, is_tt_sound := by { simp [is_tt.sound] }, is_ff_sound := by { simp [is_ff.sound] }, not_sound := by { simp [pe_not.sound] }, and_sound := by { simp [pe_and.is_true, evalB] }, or_sound := by { simp [pe_or.is_true, evalB] }, imp_sound := by { simp [pe_imp.sound] }, truth_sound := by { apply truth.sound }, bval_sound := by { simp [bval.sound] }, cval_sound := by { apply cval.sound }, dval_sound := by { simp [dval.sound] }, cast_sound := by { intros, split_c, case left { apply cast.sound.valid_input }, case right { apply cast.sound.invalid_input }, }, merge_sound := by { intros, simp only [default, evalV, val.eval, op.sym.choices.eval], apply merge.sound, apply choices.one_implies_lone, assumption, }, opS_sound := by { intros, split_c, case left { apply opS.sound }, case right { apply opS.legal }, }, } end sym_factory
d763adcb3991ad47eae2b17feef313a93c70e436
690889011852559ee5ac4dfea77092de8c832e7e
/src/category_theory/products/basic.lean
36b7fa75e9a4aefb4b903081fb9e32f646c7e395
[ "Apache-2.0" ]
permissive
williamdemeo/mathlib
f6df180148f8acc91de9ba5e558976ab40a872c7
1fa03c29f9f273203bbffb79d10d31f696b3d317
refs/heads/master
1,584,785,260,929
1,572,195,914,000
1,572,195,913,000
138,435,193
0
0
Apache-2.0
1,529,789,739,000
1,529,789,739,000
null
UTF-8
Lean
false
false
6,934
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison -/ import category_theory.equivalence import category_theory.eq_to_hom import tactic.interactive namespace category_theory universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ -- declare the `v`'s first; see `category_theory.category` for an explanation section variables (C : Type u₁) [𝒞 : category.{v₁} C] (D : Type u₂) [𝒟 : category.{v₂} D] include 𝒞 𝒟 /-- `prod C D` gives the cartesian product of two categories. -/ instance prod : category.{max v₁ v₂} (C × D) := { hom := λ X Y, ((X.1) ⟶ (Y.1)) × ((X.2) ⟶ (Y.2)), id := λ X, ⟨ 𝟙 (X.1), 𝟙 (X.2) ⟩, comp := λ _ _ _ f g, (f.1 ≫ g.1, f.2 ≫ g.2) } -- rfl lemmas for category.prod @[simp] lemma prod_id (X : C) (Y : D) : 𝟙 (X, Y) = (𝟙 X, 𝟙 Y) := rfl @[simp] lemma prod_comp {P Q R : C} {S T U : D} (f : (P, S) ⟶ (Q, T)) (g : (Q, T) ⟶ (R, U)) : f ≫ g = (f.1 ≫ g.1, f.2 ≫ g.2) := rfl @[simp] lemma prod_id_fst (X : prod C D) : _root_.prod.fst (𝟙 X) = 𝟙 X.fst := rfl @[simp] lemma prod_id_snd (X : prod C D) : _root_.prod.snd (𝟙 X) = 𝟙 X.snd := rfl @[simp] lemma prod_comp_fst {X Y Z : prod C D} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g).1 = f.1 ≫ g.1 := rfl @[simp] lemma prod_comp_snd {X Y Z : prod C D} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g).2 = f.2 ≫ g.2 := rfl end section variables (C : Type u₁) [𝒞 : category.{v₁} C] (D : Type u₁) [𝒟 : category.{v₁} D] include 𝒞 𝒟 /-- `prod.category.uniform C D` is an additional instance specialised so both factors have the same universe levels. This helps typeclass resolution. -/ instance uniform_prod : category (C × D) := category_theory.prod C D end -- Next we define the natural functors into and out of product categories. For now this doesn't address the universal properties. namespace prod variables (C : Type u₁) [𝒞 : category.{v₁} C] (D : Type u₂) [𝒟 : category.{v₂} D] include 𝒞 𝒟 /-- `inl C Z` is the functor `X ↦ (X, Z)`. -/ def inl (Z : D) : C ⥤ C × D := { obj := λ X, (X, Z), map := λ X Y f, (f, 𝟙 Z) } @[simp] lemma inl_obj (Z : D) (X : C) : (inl C D Z).obj X = (X, Z) := rfl @[simp] lemma inl_map (Z : D) {X Y : C} {f : X ⟶ Y} : (inl C D Z).map f = (f, 𝟙 Z) := rfl /-- `inr D Z` is the functor `X ↦ (Z, X)`. -/ def inr (Z : C) : D ⥤ C × D := { obj := λ X, (Z, X), map := λ X Y f, (𝟙 Z, f) } @[simp] lemma inr_obj (Z : C) (X : D) : (inr C D Z).obj X = (Z, X) := rfl @[simp] lemma inr_map (Z : C) {X Y : D} {f : X ⟶ Y} : (inr C D Z).map f = (𝟙 Z, f) := rfl /-- `fst` is the functor `(X, Y) ↦ X`. -/ def fst : C × D ⥤ C := { obj := λ X, X.1, map := λ X Y f, f.1 } @[simp] lemma fst_obj (X : C × D) : (fst C D).obj X = X.1 := rfl @[simp] lemma fst_map {X Y : C × D} {f : X ⟶ Y} : (fst C D).map f = f.1 := rfl /-- `snd` is the functor `(X, Y) ↦ Y`. -/ def snd : C × D ⥤ D := { obj := λ X, X.2, map := λ X Y f, f.2 } @[simp] lemma snd_obj (X : C × D) : (snd C D).obj X = X.2 := rfl @[simp] lemma snd_map {X Y : C × D} {f : X ⟶ Y} : (snd C D).map f = f.2 := rfl def swap : C × D ⥤ D × C := { obj := λ X, (X.2, X.1), map := λ _ _ f, (f.2, f.1) } @[simp] lemma swap_obj (X : C × D) : (swap C D).obj X = (X.2, X.1) := rfl @[simp] lemma swap_map {X Y : C × D} {f : X ⟶ Y} : (swap C D).map f = (f.2, f.1) := rfl def symmetry : swap C D ⋙ swap D C ≅ 𝟭 (C × D) := { hom := { app := λ X, 𝟙 X }, inv := { app := λ X, 𝟙 X } } def braiding : C × D ≌ D × C := equivalence.mk (swap C D) (swap D C) (nat_iso.of_components (λ X, eq_to_iso (by simp)) (by tidy)) (nat_iso.of_components (λ X, eq_to_iso (by simp)) (by tidy)) instance swap_is_equivalence : is_equivalence (swap C D) := (by apply_instance : is_equivalence (braiding C D).functor) end prod section variables (C : Type u₁) [𝒞 : category.{v₁} C] (D : Type u₂) [𝒟 : category.{v₂} D] include 𝒞 𝒟 def evaluation : C ⥤ (C ⥤ D) ⥤ D := { obj := λ X, { obj := λ F, F.obj X, map := λ F G α, α.app X, }, map := λ X Y f, { app := λ F, F.map f, naturality' := λ F G α, eq.symm (α.naturality f) } } @[simp] lemma evaluation_obj_obj (X) (F) : ((evaluation C D).obj X).obj F = F.obj X := rfl @[simp] lemma evaluation_obj_map (X) {F G} (α : F ⟶ G) : ((evaluation C D).obj X).map α = α.app X := rfl @[simp] lemma evaluation_map_app {X Y} (f : X ⟶ Y) (F) : ((evaluation C D).map f).app F = F.map f := rfl def evaluation_uncurried : C × (C ⥤ D) ⥤ D := { obj := λ p, p.2.obj p.1, map := λ x y f, (x.2.map f.1) ≫ (f.2.app y.1), map_comp' := λ X Y Z f g, begin cases g, cases f, cases Z, cases Y, cases X, simp only [prod_comp, nat_trans.comp_app, functor.map_comp, category.assoc], rw [←nat_trans.comp_app, nat_trans.naturality, nat_trans.comp_app, category.assoc, nat_trans.naturality], end } @[simp] lemma evaluation_uncurried_obj (p) : (evaluation_uncurried C D).obj p = p.2.obj p.1 := rfl @[simp] lemma evaluation_uncurried_map {x y} (f : x ⟶ y) : (evaluation_uncurried C D).map f = (x.2.map f.1) ≫ (f.2.app y.1) := rfl end variables {A : Type u₁} [𝒜 : category.{v₁} A] {B : Type u₂} [ℬ : category.{v₂} B] {C : Type u₃} [𝒞 : category.{v₃} C] {D : Type u₄} [𝒟 : category.{v₄} D] include 𝒜 ℬ 𝒞 𝒟 namespace functor /-- The cartesian product of two functors. -/ def prod (F : A ⥤ B) (G : C ⥤ D) : A × C ⥤ B × D := { obj := λ X, (F.obj X.1, G.obj X.2), map := λ _ _ f, (F.map f.1, G.map f.2) } /- Because of limitations in Lean 3's handling of notations, we do not setup a notation `F × G`. You can use `F.prod G` as a "poor man's infix", or just write `functor.prod F G`. -/ @[simp] lemma prod_obj (F : A ⥤ B) (G : C ⥤ D) (a : A) (c : C) : (F.prod G).obj (a, c) = (F.obj a, G.obj c) := rfl @[simp] lemma prod_map (F : A ⥤ B) (G : C ⥤ D) {a a' : A} {c c' : C} (f : (a, c) ⟶ (a', c')) : (F.prod G).map f = (F.map f.1, G.map f.2) := rfl end functor namespace nat_trans /-- The cartesian product of two natural transformations. -/ def prod {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟶ G) (β : H ⟶ I) : F.prod H ⟶ G.prod I := { app := λ X, (α.app X.1, β.app X.2), naturality' := λ X Y f, begin cases X, cases Y, simp only [functor.prod_map, prod.mk.inj_iff, prod_comp], split; rw naturality end } /- Again, it is inadvisable in Lean 3 to setup a notation `α × β`; use instead `α.prod β` or `nat_trans.prod α β`. -/ @[simp] lemma prod_app {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟶ G) (β : H ⟶ I) (a : A) (c : C) : (nat_trans.prod α β).app (a, c) = (α.app a, β.app c) := rfl end nat_trans end category_theory
4cb17f0f1002da6be24e821acf18a1523e2434d0
92b50235facfbc08dfe7f334827d47281471333b
/hott/types/int/basic.hlean
2e7f123c8fa2974d8b98569f2ca2c4fb2e680ae4
[ "Apache-2.0" ]
permissive
htzh/lean
24f6ed7510ab637379ec31af406d12584d31792c
d70c79f4e30aafecdfc4a60b5d3512199200ab6e
refs/heads/master
1,607,677,731,270
1,437,089,952,000
1,437,089,952,000
37,078,816
0
0
null
1,433,780,956,000
1,433,780,955,000
null
UTF-8
Lean
false
false
37,412
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jeremy Avigad The integers, with addition, multiplication, and subtraction. The representation of the integers is chosen to compute efficiently. To faciliate proving things about these operations, we show that the integers are a quotient of ℕ × ℕ with the usual equivalence relation, ≡, and functions abstr : ℕ × ℕ → ℤ repr : ℤ → ℕ × ℕ satisfying: abstr_repr (a : ℤ) : abstr (repr a) = a repr_abstr (p : ℕ × ℕ) : repr (abstr p) ≡ p abstr_eq (p q : ℕ × ℕ) : p ≡ q → abstr p = abstr q For example, to "lift" statements about add to statements about padd, we need to prove the following: repr_add (a b : ℤ) : repr (a + b) = padd (repr a) (repr b) padd_congr (p p' q q' : ℕ × ℕ) (H1 : p ≡ p') (H2 : q ≡ q') : padd p q ≡ p' q' Ported from standard library -/ import types.nat.sub algebra.relation types.prod open core nat decidable prod relation prod /- the type of integers -/ inductive int : Type := | of_nat : nat → int | neg_succ_of_nat : nat → int notation `ℤ` := int attribute int.of_nat [coercion] definition int.of_num [coercion] [reducible] [constructor] (n : num) : ℤ := int.of_nat (nat.of_num n) namespace int /- definitions of basic functions -/ definition neg_of_nat (m : ℕ) : ℤ := nat.cases_on m 0 (take m', neg_succ_of_nat m') definition sub_nat_nat (m n : ℕ) : ℤ := nat.cases_on (n - m) (of_nat (m - n)) -- m ≥ n (take k, neg_succ_of_nat k) -- m < n, and n - m = succ k definition neg (a : ℤ) : ℤ := int.cases_on a (take m, -- a = of_nat m nat.cases_on m 0 (take m', neg_succ_of_nat m')) (take m, of_nat (succ m)) -- a = neg_succ_of_nat m definition add (a b : ℤ) : ℤ := int.cases_on a (take m, -- a = of_nat m int.cases_on b (take n, of_nat (m + n)) -- b = of_nat n (take n, sub_nat_nat m (succ n))) -- b = neg_succ_of_nat n (take m, -- a = neg_succ_of_nat m int.cases_on b (take n, sub_nat_nat n (succ m)) -- b = of_nat n (take n, neg_of_nat (succ m + succ n))) -- b = neg_succ_of_nat n definition mul (a b : ℤ) : ℤ := int.cases_on a (take m, -- a = of_nat m int.cases_on b (take n, of_nat (m * n)) -- b = of_nat n (take n, neg_of_nat (m * succ n))) -- b = neg_succ_of_nat n (take m, -- a = neg_succ_of_nat m int.cases_on b (take n, neg_of_nat (succ m * n)) -- b = of_nat n (take n, of_nat (succ m * succ n))) -- b = neg_succ_of_nat n /- notation -/ notation `-[` n `+1]` := int.neg_succ_of_nat n -- for pretty-printing output prefix - := int.neg infix + := int.add infix * := int.mul /- some basic functions and properties -/ definition of_nat.inj {m n : ℕ} (H : of_nat m = of_nat n) : m = n := by injection H; assumption definition neg_succ_of_nat.inj {m n : ℕ} (H : neg_succ_of_nat m = neg_succ_of_nat n) : m = n := by injection H; assumption definition neg_succ_of_nat_eq (n : ℕ) : -[n +1] = -(n + 1) := rfl definition has_decidable_eq [instance] : decidable_eq ℤ := take a b, int.cases_on a (take m, int.cases_on b (take n, if H : m = n then inl (ap of_nat H) else inr (take H1, H (of_nat.inj H1))) (take n', inr (by contradiction))) (take m', int.cases_on b (take n, inr (by contradiction)) (take n', (if H : m' = n' then inl (ap neg_succ_of_nat H) else inr (take H1, H (neg_succ_of_nat.inj H1))))) definition of_nat_add_of_nat (n m : nat) : of_nat n + of_nat m = #nat n + m := rfl definition of_nat_succ (n : ℕ) : of_nat (succ n) = of_nat n + 1 := rfl definition of_nat_mul_of_nat (n m : ℕ) : of_nat n * of_nat m = n * m := rfl definition sub_nat_nat_of_ge {m n : ℕ} (H : m ≥ n) : sub_nat_nat m n = of_nat (m - n) := have H1 : n - m = 0, from sub_eq_zero_of_le H, calc sub_nat_nat m n = nat.cases_on 0 (of_nat (m - n)) _ : H1 ▸ rfl ... = of_nat (m - n) : rfl section local attribute sub_nat_nat [reducible] definition sub_nat_nat_of_lt {m n : ℕ} (H : m < n) : sub_nat_nat m n = neg_succ_of_nat (pred (n - m)) := have H1 : n - m = succ (pred (n - m)), from (succ_pred_of_pos (sub_pos_of_lt H))⁻¹, calc sub_nat_nat m n = nat.cases_on (succ (pred (n - m))) (of_nat (m - n)) (take k, neg_succ_of_nat k) : H1 ▸ rfl ... = neg_succ_of_nat (pred (n - m)) : rfl end definition nat_abs (a : ℤ) : ℕ := int.cases_on a (take n, n) (take n', succ n') definition nat_abs_of_nat (n : ℕ) : nat_abs (of_nat n) = n := rfl definition nat_abs_eq_zero {a : ℤ} : nat_abs a = 0 → a = 0 := int.cases_on a (take m, assume H : nat_abs (of_nat m) = 0, ap of_nat H) (take m', assume H : nat_abs (neg_succ_of_nat m') = 0, absurd H (succ_ne_zero _)) /- int is a quotient of ordered pairs of natural numbers -/ definition int_equiv (p q : ℕ × ℕ) : Type₀ := pr1 p + pr2 q = pr2 p + pr1 q local infix `≡` := int_equiv protected theorem int_equiv.refl [refl] {p : ℕ × ℕ} : p ≡ p := !add.comm protected theorem int_equiv.symm [symm] {p q : ℕ × ℕ} (H : p ≡ q) : q ≡ p := calc pr1 q + pr2 p = pr2 p + pr1 q : !add.comm ... = pr1 p + pr2 q : H⁻¹ ... = pr2 q + pr1 p : !add.comm protected theorem int_equiv.trans [trans] {p q r : ℕ × ℕ} (H1 : p ≡ q) (H2 : q ≡ r) : p ≡ r := have H3 : pr1 p + pr2 r + pr2 q = pr2 p + pr1 r + pr2 q, from calc pr1 p + pr2 r + pr2 q = pr1 p + pr2 q + pr2 r : by exact sorry ... = pr2 p + pr1 q + pr2 r : {H1} ... = pr2 p + (pr1 q + pr2 r) : by exact sorry ... = pr2 p + (pr2 q + pr1 r) : {H2} ... = pr2 p + pr1 r + pr2 q : by exact sorry, show pr1 p + pr2 r = pr2 p + pr1 r, from add.cancel_right H3 definition int_equiv_int_equiv : is_equivalence int_equiv := is_equivalence.mk @int_equiv.refl @int_equiv.symm @int_equiv.trans definition int_equiv_cases {p q : ℕ × ℕ} (H : int_equiv p q) : (pr1 p ≥ pr2 p × pr1 q ≥ pr2 q) ⊎ (pr1 p < pr2 p × pr1 q < pr2 q) := sum.rec_on (@le_or_gt (pr2 p) (pr1 p)) (assume H1: pr1 p ≥ pr2 p, have H2 : pr2 p + pr1 q ≥ pr2 p + pr2 q, from H ▸ add_le_add_right H1 (pr2 q), sum.inl (pair H1 (le_of_add_le_add_left H2))) (assume H1: pr1 p < pr2 p, have H2 : pr2 p + pr1 q < pr2 p + pr2 q, from H ▸ add_lt_add_right H1 (pr2 q), sum.inr (pair H1 (lt_of_add_lt_add_left H2))) definition int_equiv_of_eq {p q : ℕ × ℕ} (H : p = q) : p ≡ q := H ▸ int_equiv.refl /- the representation and abstraction functions -/ definition abstr (a : ℕ × ℕ) : ℤ := sub_nat_nat (pr1 a) (pr2 a) definition abstr_of_ge {p : ℕ × ℕ} (H : pr1 p ≥ pr2 p) : abstr p = of_nat (pr1 p - pr2 p) := sub_nat_nat_of_ge H definition abstr_of_lt {p : ℕ × ℕ} (H : pr1 p < pr2 p) : abstr p = neg_succ_of_nat (pred (pr2 p - pr1 p)) := sub_nat_nat_of_lt H definition repr (a : ℤ) : ℕ × ℕ := int.cases_on a (take m, (m, 0)) (take m, (0, succ m)) definition abstr_repr (a : ℤ) : abstr (repr a) = a := int.cases_on a (take m, (sub_nat_nat_of_ge (zero_le m))) (take m, rfl) definition repr_sub_nat_nat (m n : ℕ) : repr (sub_nat_nat m n) ≡ (m, n) := sum.rec_on (@le_or_gt n m) (take H : m ≥ n, have H1 : repr (sub_nat_nat m n) = (m - n, 0), from sub_nat_nat_of_ge H ▸ rfl, H1⁻¹ ▸ (calc m - n + n = m : sub_add_cancel H ... = 0 + m : zero_add)) (take H : m < n, have H1 : repr (sub_nat_nat m n) = (0, succ (pred (n - m))), from sub_nat_nat_of_lt H ▸ rfl, H1⁻¹ ▸ (calc 0 + n = n : zero_add ... = n - m + m : sub_add_cancel (le_of_lt H) ... = succ (pred (n - m)) + m : (succ_pred_of_pos (sub_pos_of_lt H))⁻¹ᵖ)) definition repr_abstr (p : ℕ × ℕ) : repr (abstr p) ≡ p := !prod.eta ▸ !repr_sub_nat_nat definition abstr_eq {p q : ℕ × ℕ} (Hint_equiv : p ≡ q) : abstr p = abstr q := sum.rec_on (int_equiv_cases Hint_equiv) (assume H2, have H3 : pr1 p ≥ pr2 p, from prod.pr1 H2, have H4 : pr1 q ≥ pr2 q, from prod.pr2 H2, have H5 : pr1 p = pr1 q - pr2 q + pr2 p, from calc pr1 p = pr1 p + pr2 q - pr2 q : add_sub_cancel ... = pr2 p + pr1 q - pr2 q : by rewrite [↑int_equiv at Hint_equiv,Hint_equiv] ... = pr2 p + (pr1 q - pr2 q) : add_sub_assoc H4 ... = pr1 q - pr2 q + pr2 p : add.comm, have H6 : pr1 p - pr2 p = pr1 q - pr2 q, from calc pr1 p - pr2 p = pr1 q - pr2 q + pr2 p - pr2 p : H5 ... = pr1 q - pr2 q : add_sub_cancel, abstr_of_ge H3 ⬝ ap of_nat H6 ⬝ (abstr_of_ge H4)⁻¹) (assume H2, have H3 : pr1 p < pr2 p, from prod.pr1 H2, have H4 : pr1 q < pr2 q, from prod.pr2 H2, have H5 : pr2 p = pr2 q - pr1 q + pr1 p, from calc pr2 p = pr2 p + pr1 q - pr1 q : add_sub_cancel ... = pr1 p + pr2 q - pr1 q : by rewrite [↑int_equiv at Hint_equiv,Hint_equiv] ... = pr1 p + (pr2 q - pr1 q) : add_sub_assoc (le_of_lt H4) ... = pr2 q - pr1 q + pr1 p : add.comm, have H6 : pr2 p - pr1 p = pr2 q - pr1 q, from calc pr2 p - pr1 p = pr2 q - pr1 q + pr1 p - pr1 p : H5 ... = pr2 q - pr1 q : add_sub_cancel, abstr_of_lt H3 ⬝ ap neg_succ_of_nat (ap pred H6)⬝ (abstr_of_lt H4)⁻¹) definition int_equiv_iff (p q : ℕ × ℕ) : (p ≡ q) ↔ ((p ≡ p) × (q ≡ q) × (abstr p = abstr q)) := iff.intro (assume H : int_equiv p q, pair !int_equiv.refl (pair !int_equiv.refl (abstr_eq H))) (assume H : int_equiv p p × int_equiv q q × abstr p = abstr q, have H1 : abstr p = abstr q, from prod.pr2 (prod.pr2 H), int_equiv.trans (H1 ▸ int_equiv.symm (repr_abstr p)) (repr_abstr q)) definition eq_abstr_of_int_equiv_repr {a : ℤ} {p : ℕ × ℕ} (Hint_equiv : repr a ≡ p) : a = abstr p := calc a = abstr (repr a) : abstr_repr ... = abstr p : abstr_eq Hint_equiv definition eq_of_repr_int_equiv_repr {a b : ℤ} (H : repr a ≡ repr b) : a = b := calc a = abstr (repr a) : abstr_repr ... = abstr (repr b) : abstr_eq H ... = b : abstr_repr section local attribute abstr [reducible] local attribute dist [reducible] definition nat_abs_abstr (p : ℕ × ℕ) : nat_abs (abstr p) = dist (pr1 p) (pr2 p) := let m := pr1 p, n := pr2 p in sum.rec_on (@le_or_gt n m) (assume H : m ≥ n, calc nat_abs (abstr (m, n)) = nat_abs (of_nat (m - n)) : int.abstr_of_ge H ... = dist m n : dist_eq_sub_of_ge H) (assume H : m < n, calc nat_abs (abstr (m, n)) = nat_abs (neg_succ_of_nat (pred (n - m))) : int.abstr_of_lt H ... = succ (pred (n - m)) : rfl ... = n - m : succ_pred_of_pos (sub_pos_of_lt H) ... = dist m n : dist_eq_sub_of_le (le_of_lt H)) end definition cases_of_nat (a : ℤ) : (Σn : ℕ, a = of_nat n) ⊎ (Σn : ℕ, a = - of_nat n) := int.cases_on a (take n, sum.inl (sigma.mk n rfl)) (take n', sum.inr (sigma.mk (succ n') rfl)) definition cases_of_nat_succ (a : ℤ) : (Σn : ℕ, a = of_nat n) ⊎ (Σn : ℕ, a = - (of_nat (succ n))) := int.cases_on a (take m, sum.inl (sigma.mk _ rfl)) (take m, sum.inr (sigma.mk _ rfl)) definition by_cases_of_nat {P : ℤ → Type} (a : ℤ) (H1 : Πn : ℕ, P (of_nat n)) (H2 : Πn : ℕ, P (- of_nat n)) : P a := sum.rec_on (cases_of_nat a) (assume H, obtain (n : ℕ) (H3 : a = n), from H, H3⁻¹ ▸ H1 n) (assume H, obtain (n : ℕ) (H3 : a = -n), from H, H3⁻¹ ▸ H2 n) definition by_cases_of_nat_succ {P : ℤ → Type} (a : ℤ) (H1 : Πn : ℕ, P (of_nat n)) (H2 : Πn : ℕ, P (- of_nat (succ n))) : P a := sum.rec_on (cases_of_nat_succ a) (assume H, obtain (n : ℕ) (H3 : a = n), from H, H3⁻¹ ▸ H1 n) (assume H, obtain (n : ℕ) (H3 : a = -(succ n)), from H, H3⁻¹ ▸ H2 n) /- int is a ring -/ /- addition -/ definition padd (p q : ℕ × ℕ) : ℕ × ℕ := (pr1 p + pr1 q, pr2 p + pr2 q) definition repr_add (a b : ℤ) : repr (add a b) ≡ padd (repr a) (repr b) := int.cases_on a (take m, int.cases_on b (take n, !int_equiv.refl) (take n', have H1 : int_equiv (repr (add (of_nat m) (neg_succ_of_nat n'))) (m, succ n'), from !repr_sub_nat_nat, have H2 : padd (repr (of_nat m)) (repr (neg_succ_of_nat n')) = (m, 0 + succ n'), from rfl, (!zero_add ▸ H2)⁻¹ ▸ H1)) (take m', int.cases_on b (take n, have H1 : int_equiv (repr (add (neg_succ_of_nat m') (of_nat n))) (n, succ m'), from !repr_sub_nat_nat, have H2 : padd (repr (neg_succ_of_nat m')) (repr (of_nat n)) = (0 + n, succ m'), from rfl, (!zero_add ▸ H2)⁻¹ ▸ H1) (take n',!repr_sub_nat_nat)) definition padd_congr {p p' q q' : ℕ × ℕ} (Ha : p ≡ p') (Hb : q ≡ q') : padd p q ≡ padd p' q' := calc pr1 (padd p q) + pr2 (padd p' q') = pr1 p + pr2 p' + (pr1 q + pr2 q') : by exact sorry ... = pr2 p + pr1 p' + (pr1 q + pr2 q') : {Ha} ... = pr2 p + pr1 p' + (pr2 q + pr1 q') : {Hb} ... = pr2 (padd p q) + pr1 (padd p' q') : by exact sorry definition padd_comm (p q : ℕ × ℕ) : padd p q = padd q p := calc padd p q = (pr1 p + pr1 q, pr2 p + pr2 q) : rfl ... = (pr1 q + pr1 p, pr2 p + pr2 q) : add.comm ... = (pr1 q + pr1 p, pr2 q + pr2 p) : add.comm ... = padd q p : rfl definition padd_assoc (p q r : ℕ × ℕ) : padd (padd p q) r = padd p (padd q r) := calc padd (padd p q) r = (pr1 p + pr1 q + pr1 r, pr2 p + pr2 q + pr2 r) : rfl ... = (pr1 p + (pr1 q + pr1 r), pr2 p + pr2 q + pr2 r) : add.assoc ... = (pr1 p + (pr1 q + pr1 r), pr2 p + (pr2 q + pr2 r)) : add.assoc ... = padd p (padd q r) : rfl definition add.comm (a b : ℤ) : a + b = b + a := begin apply eq_of_repr_int_equiv_repr, apply int_equiv.trans, apply repr_add, apply int_equiv.symm, apply eq.subst (padd_comm (repr b) (repr a)), apply repr_add end definition add.assoc (a b c : ℤ) : a + b + c = a + (b + c) := assert H1 : repr (a + b + c) ≡ padd (padd (repr a) (repr b)) (repr c), from int_equiv.trans (repr_add (a + b) c) (padd_congr !repr_add !int_equiv.refl), assert H2 : repr (a + (b + c)) ≡ padd (repr a) (padd (repr b) (repr c)), from int_equiv.trans (repr_add a (b + c)) (padd_congr !int_equiv.refl !repr_add), begin apply eq_of_repr_int_equiv_repr, apply int_equiv.trans, apply H1, apply eq.subst (padd_assoc _ _ _)⁻¹, apply int_equiv.symm, apply H2 end definition add_zero (a : ℤ) : a + 0 = a := int.cases_on a (take m, rfl) (take m', rfl) definition zero_add (a : ℤ) : 0 + a = a := add.comm a 0 ▸ add_zero a /- negation -/ definition pneg (p : ℕ × ℕ) : ℕ × ℕ := (pr2 p, pr1 p) -- note: this is =, not just ≡ definition repr_neg (a : ℤ) : repr (- a) = pneg (repr a) := int.cases_on a (take m, nat.cases_on m rfl (take m', rfl)) (take m', rfl) definition pneg_congr {p p' : ℕ × ℕ} (H : p ≡ p') : pneg p ≡ pneg p' := inverse H definition pneg_pneg (p : ℕ × ℕ) : pneg (pneg p) = p := !prod.eta definition nat_abs_neg (a : ℤ) : nat_abs (-a) = nat_abs a := calc nat_abs (-a) = nat_abs (abstr (repr (-a))) : abstr_repr ... = nat_abs (abstr (pneg (repr a))) : repr_neg ... = dist (pr1 (pneg (repr a))) (pr2 (pneg (repr a))) : nat_abs_abstr ... = dist (pr2 (pneg (repr a))) (pr1 (pneg (repr a))) : dist.comm ... = nat_abs (abstr (repr a)) : nat_abs_abstr ... = nat_abs a : abstr_repr definition padd_pneg (p : ℕ × ℕ) : padd p (pneg p) ≡ (0, 0) := show pr1 p + pr2 p + 0 = pr2 p + pr1 p + 0, from !nat.add.comm ▸ rfl definition padd_padd_pneg (p q : ℕ × ℕ) : padd (padd p q) (pneg q) ≡ p := show pr1 p + pr1 q + pr2 q + pr2 p = pr2 p + pr2 q + pr1 q + pr1 p, from by exact sorry definition add.left_inv (a : ℤ) : -a + a = 0 := have H : repr (-a + a) ≡ repr 0, from calc repr (-a + a) ≡ padd (repr (neg a)) (repr a) : repr_add ... ≡ padd (pneg (repr a)) (repr a) : sorry ... ≡ repr 0 : padd_pneg, eq_of_repr_int_equiv_repr H /- nat abs -/ definition pabs (p : ℕ × ℕ) : ℕ := dist (pr1 p) (pr2 p) definition pabs_congr {p q : ℕ × ℕ} (H : p ≡ q) : pabs p = pabs q := calc pabs p = nat_abs (abstr p) : nat_abs_abstr ... = nat_abs (abstr q) : abstr_eq H ... = pabs q : nat_abs_abstr definition nat_abs_eq_pabs_repr (a : ℤ) : nat_abs a = pabs (repr a) := calc nat_abs a = nat_abs (abstr (repr a)) : abstr_repr ... = pabs (repr a) : nat_abs_abstr definition nat_abs_add_le (a b : ℤ) : nat_abs (a + b) ≤ nat_abs a + nat_abs b := have H : nat_abs (a + b) = pabs (padd (repr a) (repr b)), from calc nat_abs (a + b) = pabs (repr (a + b)) : nat_abs_eq_pabs_repr ... = pabs (padd (repr a) (repr b)) : pabs_congr !repr_add, have H1 : nat_abs a = pabs (repr a), from !nat_abs_eq_pabs_repr, have H2 : nat_abs b = pabs (repr b), from !nat_abs_eq_pabs_repr, have H3 : pabs (padd (repr a) (repr b)) ≤ pabs (repr a) + pabs (repr b), from !dist_add_add_le_add_dist_dist, H⁻¹ ▸ H1⁻¹ ▸ H2⁻¹ ▸ H3 section local attribute nat_abs [reducible] definition mul_nat_abs (a b : ℤ) : nat_abs (a * b) = #nat (nat_abs a) * (nat_abs b) := int.cases_on a (take m, int.cases_on b (take n, rfl) (take n', !nat_abs_neg ▸ rfl)) (take m', int.cases_on b (take n, !nat_abs_neg ▸ rfl) (take n', rfl)) end /- multiplication -/ definition pmul (p q : ℕ × ℕ) : ℕ × ℕ := (pr1 p * pr1 q + pr2 p * pr2 q, pr1 p * pr2 q + pr2 p * pr1 q) definition repr_neg_of_nat (m : ℕ) : repr (neg_of_nat m) = (0, m) := nat.cases_on m rfl (take m', rfl) -- note: we have =, not just ≡ definition repr_mul (a b : ℤ) : repr (mul a b) = pmul (repr a) (repr b) := int.cases_on a (take m, int.cases_on b (take n, (calc pmul (repr m) (repr n) = (m * n + 0 * 0, m * 0 + 0 * n) : rfl ... = (m * n + 0 * 0, m * 0 + 0) : zero_mul)⁻¹) (take n', (calc pmul (repr m) (repr (neg_succ_of_nat n')) = (m * 0 + 0 * succ n', m * succ n' + 0 * 0) : rfl ... = (m * 0 + 0, m * succ n' + 0 * 0) : zero_mul ... = repr (mul m (neg_succ_of_nat n')) : repr_neg_of_nat)⁻¹)) (take m', int.cases_on b (take n, (calc pmul (repr (neg_succ_of_nat m')) (repr n) = (0 * n + succ m' * 0, 0 * 0 + succ m' * n) : rfl ... = (0 + succ m' * 0, 0 * 0 + succ m' * n) : zero_mul ... = (0 + succ m' * 0, succ m' * n) : {!nat.zero_add} ... = repr (mul (neg_succ_of_nat m') n) : repr_neg_of_nat)⁻¹) (take n', (calc pmul (repr (neg_succ_of_nat m')) (repr (neg_succ_of_nat n')) = (0 + succ m' * succ n', 0 * succ n') : rfl ... = (succ m' * succ n', 0 * succ n') : nat.zero_add ... = (succ m' * succ n', 0) : zero_mul ... = repr (mul (neg_succ_of_nat m') (neg_succ_of_nat n')) : rfl)⁻¹)) definition int_equiv_mul_prep {xa ya xb yb xn yn xm ym : ℕ} (H1 : xa + yb = ya + xb) (H2 : xn + ym = yn + xm) : xa * xn + ya * yn + (xb * ym + yb * xm) = xa * yn + ya * xn + (xb * xm + yb * ym) := have H3 : xa * xn + ya * yn + (xb * ym + yb * xm) + (yb * xn + xb * yn + (xb * xn + yb * yn)) = xa * yn + ya * xn + (xb * xm + yb * ym) + (yb * xn + xb * yn + (xb * xn + yb * yn)), from calc xa * xn + ya * yn + (xb * ym + yb * xm) + (yb * xn + xb * yn + (xb * xn + yb * yn)) = xa * xn + yb * xn + (ya * yn + xb * yn) + (xb * xn + xb * ym + (yb * yn + yb * xm)) : by exact sorry ... = (xa + yb) * xn + (ya + xb) * yn + (xb * (xn + ym) + yb * (yn + xm)) : by exact sorry ... = (ya + xb) * xn + (xa + yb) * yn + (xb * (yn + xm) + yb * (xn + ym)) : by exact sorry ... = ya * xn + xb * xn + (xa * yn + yb * yn) + (xb * yn + xb * xm + (yb*xn + yb*ym)) : by exact sorry ... = xa * yn + ya * xn + (xb * xm + yb * ym) + (yb * xn + xb * yn + (xb * xn + yb * yn)) : by exact sorry, nat.add.cancel_right H3 definition pmul_congr {p p' q q' : ℕ × ℕ} (H1 : p ≡ p') (H2 : q ≡ q') : pmul p q ≡ pmul p' q' := int_equiv_mul_prep H1 H2 definition pmul_comm (p q : ℕ × ℕ) : pmul p q = pmul q p := calc (pr1 p * pr1 q + pr2 p * pr2 q, pr1 p * pr2 q + pr2 p * pr1 q) = (pr1 q * pr1 p + pr2 p * pr2 q, pr1 p * pr2 q + pr2 p * pr1 q) : mul.comm ... = (pr1 q * pr1 p + pr2 q * pr2 p, pr1 p * pr2 q + pr2 p * pr1 q) : mul.comm ... = (pr1 q * pr1 p + pr2 q * pr2 p, pr2 q * pr1 p + pr2 p * pr1 q) : mul.comm ... = (pr1 q * pr1 p + pr2 q * pr2 p, pr2 q * pr1 p + pr1 q * pr2 p) : mul.comm ... = (pr1 q * pr1 p + pr2 q * pr2 p, pr1 q * pr2 p + pr2 q * pr1 p) : nat.add.comm definition mul.comm (a b : ℤ) : a * b = b * a := eq_of_repr_int_equiv_repr ((calc repr (a * b) = pmul (repr a) (repr b) : repr_mul ... = pmul (repr b) (repr a) : pmul_comm ... = repr (b * a) : repr_mul) ▸ !int_equiv.refl) definition pmul_assoc (p q r: ℕ × ℕ) : pmul (pmul p q) r = pmul p (pmul q r) := by exact sorry definition mul.assoc (a b c : ℤ) : (a * b) * c = a * (b * c) := eq_of_repr_int_equiv_repr ((calc repr (a * b * c) = pmul (repr (a * b)) (repr c) : repr_mul ... = pmul (pmul (repr a) (repr b)) (repr c) : repr_mul ... = pmul (repr a) (pmul (repr b) (repr c)) : pmul_assoc ... = pmul (repr a) (repr (b * c)) : repr_mul ... = repr (a * (b * c)) : repr_mul) ▸ !int_equiv.refl) definition mul_one (a : ℤ) : a * 1 = a := eq_of_repr_int_equiv_repr (int_equiv_of_eq ((calc repr (a * 1) = pmul (repr a) (repr 1) : repr_mul ... = (pr1 (repr a), pr2 (repr a)) : by exact sorry ... = repr a : prod.eta))) definition one_mul (a : ℤ) : 1 * a = a := mul.comm a 1 ▸ mul_one a definition mul.right_distrib (a b c : ℤ) : (a + b) * c = a * c + b * c := eq_of_repr_int_equiv_repr (calc repr ((a + b) * c) = pmul (repr (a + b)) (repr c) : repr_mul ... ≡ pmul (padd (repr a) (repr b)) (repr c) : pmul_congr !repr_add int_equiv.refl ... = padd (pmul (repr a) (repr c)) (pmul (repr b) (repr c)) : by exact sorry ... = padd (repr (a * c)) (pmul (repr b) (repr c)) : {(repr_mul a c)⁻¹} ... = padd (repr (a * c)) (repr (b * c)) : repr_mul ... ≡ repr (a * c + b * c) : int_equiv.symm !repr_add) definition mul.left_distrib (a b c : ℤ) : a * (b + c) = a * b + a * c := calc a * (b + c) = (b + c) * a : mul.comm a (b + c) ... = b * a + c * a : mul.right_distrib b c a ... = a * b + c * a : {mul.comm b a} ... = a * b + a * c : {mul.comm c a} definition zero_ne_one : (0 : int) ≠ 1 := assume H : 0 = 1, show empty, from succ_ne_zero 0 ((of_nat.inj H)⁻¹) definition eq_zero_or_eq_zero_of_mul_eq_zero {a b : ℤ} (H : a * b = 0) : a = 0 ⊎ b = 0 := have H2 : (nat_abs a) * (nat_abs b) = nat.zero, from calc (nat_abs a) * (nat_abs b) = (nat_abs (a * b)) : (mul_nat_abs a b)⁻¹ ... = (nat_abs 0) : {H} ... = nat.zero : nat_abs_of_nat nat.zero, have H3 : (nat_abs a) = nat.zero ⊎ (nat_abs b) = nat.zero, from eq_zero_or_eq_zero_of_mul_eq_zero H2, sum_of_sum_of_imp_of_imp H3 (assume H : (nat_abs a) = nat.zero, nat_abs_eq_zero H) (assume H : (nat_abs b) = nat.zero, nat_abs_eq_zero H) section open [classes] algebra protected definition integral_domain [instance] [reducible] : algebra.integral_domain int := ⦃algebra.integral_domain, add := add, add_assoc := add.assoc, zero := zero, zero_add := zero_add, add_zero := add_zero, neg := neg, add_left_inv := add.left_inv, add_comm := add.comm, mul := mul, mul_assoc := mul.assoc, one := (of_num 1), one_mul := one_mul, mul_one := mul_one, left_distrib := mul.left_distrib, right_distrib := mul.right_distrib, mul_comm := mul.comm, eq_zero_or_eq_zero_of_mul_eq_zero := @eq_zero_or_eq_zero_of_mul_eq_zero, is_hset_carrier := is_hset_of_decidable_eq⦄ end /- instantiate ring theorems to int -/ section port_algebra open [classes] algebra definition mul.left_comm : Πa b c : ℤ, a * (b * c) = b * (a * c) := algebra.mul.left_comm definition mul.right_comm : Πa b c : ℤ, (a * b) * c = (a * c) * b := algebra.mul.right_comm definition add.left_comm : Πa b c : ℤ, a + (b + c) = b + (a + c) := algebra.add.left_comm definition add.right_comm : Πa b c : ℤ, (a + b) + c = (a + c) + b := algebra.add.right_comm definition add.left_cancel : Π{a b c : ℤ}, a + b = a + c → b = c := @algebra.add.left_cancel _ _ definition add.right_cancel : Π{a b c : ℤ}, a + b = c + b → a = c := @algebra.add.right_cancel _ _ definition neg_add_cancel_left : Πa b : ℤ, -a + (a + b) = b := algebra.neg_add_cancel_left definition neg_add_cancel_right : Πa b : ℤ, a + -b + b = a := algebra.neg_add_cancel_right definition neg_eq_of_add_eq_zero : Π{a b : ℤ}, a + b = 0 → -a = b := @algebra.neg_eq_of_add_eq_zero _ _ definition neg_zero : -0 = 0 := algebra.neg_zero definition neg_neg : Πa : ℤ, -(-a) = a := algebra.neg_neg definition neg.inj : Π{a b : ℤ}, -a = -b → a = b := @algebra.neg.inj _ _ definition neg_eq_neg_iff_eq : Πa b : ℤ, -a = -b ↔ a = b := algebra.neg_eq_neg_iff_eq definition neg_eq_zero_iff_eq_zero : Πa : ℤ, -a = 0 ↔ a = 0 := algebra.neg_eq_zero_iff_eq_zero definition eq_neg_of_eq_neg : Π{a b : ℤ}, a = -b → b = -a := @algebra.eq_neg_of_eq_neg _ _ definition eq_neg_iff_eq_neg : Π{a b : ℤ}, a = -b ↔ b = -a := @algebra.eq_neg_iff_eq_neg _ _ definition add.right_inv : Πa : ℤ, a + -a = 0 := algebra.add.right_inv definition add_neg_cancel_left : Πa b : ℤ, a + (-a + b) = b := algebra.add_neg_cancel_left definition add_neg_cancel_right : Πa b : ℤ, a + b + -b = a := algebra.add_neg_cancel_right definition neg_add_rev : Πa b : ℤ, -(a + b) = -b + -a := algebra.neg_add_rev definition eq_add_neg_of_add_eq : Π{a b c : ℤ}, a + c = b → a = b + -c := @algebra.eq_add_neg_of_add_eq _ _ definition eq_neg_add_of_add_eq : Π{a b c : ℤ}, b + a = c → a = -b + c := @algebra.eq_neg_add_of_add_eq _ _ definition neg_add_eq_of_eq_add : Π{a b c : ℤ}, b = a + c → -a + b = c := @algebra.neg_add_eq_of_eq_add _ _ definition add_neg_eq_of_eq_add : Π{a b c : ℤ}, a = c + b → a + -b = c := @algebra.add_neg_eq_of_eq_add _ _ definition eq_add_of_add_neg_eq : Π{a b c : ℤ}, a + -c = b → a = b + c := @algebra.eq_add_of_add_neg_eq _ _ definition eq_add_of_neg_add_eq : Π{a b c : ℤ}, -b + a = c → a = b + c := @algebra.eq_add_of_neg_add_eq _ _ definition add_eq_of_eq_neg_add : Π{a b c : ℤ}, b = -a + c → a + b = c := @algebra.add_eq_of_eq_neg_add _ _ definition add_eq_of_eq_add_neg : Π{a b c : ℤ}, a = c + -b → a + b = c := @algebra.add_eq_of_eq_add_neg _ _ definition add_eq_iff_eq_neg_add : Πa b c : ℤ, a + b = c ↔ b = -a + c := @algebra.add_eq_iff_eq_neg_add _ _ definition add_eq_iff_eq_add_neg : Πa b c : ℤ, a + b = c ↔ a = c + -b := @algebra.add_eq_iff_eq_add_neg _ _ definition sub (a b : ℤ) : ℤ := algebra.sub a b infix - := int.sub definition sub_eq_add_neg : Πa b : ℤ, a - b = a + -b := algebra.sub_eq_add_neg definition sub_self : Πa : ℤ, a - a = 0 := algebra.sub_self definition sub_add_cancel : Πa b : ℤ, a - b + b = a := algebra.sub_add_cancel definition add_sub_cancel : Πa b : ℤ, a + b - b = a := algebra.add_sub_cancel definition eq_of_sub_eq_zero : Π{a b : ℤ}, a - b = 0 → a = b := @algebra.eq_of_sub_eq_zero _ _ definition eq_iff_sub_eq_zero : Πa b : ℤ, a = b ↔ a - b = 0 := algebra.eq_iff_sub_eq_zero definition zero_sub : Πa : ℤ, 0 - a = -a := algebra.zero_sub definition sub_zero : Πa : ℤ, a - 0 = a := algebra.sub_zero definition sub_neg_eq_add : Πa b : ℤ, a - (-b) = a + b := algebra.sub_neg_eq_add definition neg_sub : Πa b : ℤ, -(a - b) = b - a := algebra.neg_sub definition add_sub : Πa b c : ℤ, a + (b - c) = a + b - c := algebra.add_sub definition sub_add_eq_sub_sub_swap : Πa b c : ℤ, a - (b + c) = a - c - b := algebra.sub_add_eq_sub_sub_swap definition sub_eq_iff_eq_add : Πa b c : ℤ, a - b = c ↔ a = c + b := algebra.sub_eq_iff_eq_add definition eq_sub_iff_add_eq : Πa b c : ℤ, a = b - c ↔ a + c = b := algebra.eq_sub_iff_add_eq definition eq_iff_eq_of_sub_eq_sub : Π{a b c d : ℤ}, a - b = c - d → (a = b ↔ c = d) := @algebra.eq_iff_eq_of_sub_eq_sub _ _ definition eq_sub_of_add_eq : Π{a b c : ℤ}, a + c = b → a = b - c := @algebra.eq_sub_of_add_eq _ _ definition sub_eq_of_eq_add : Π{a b c : ℤ}, a = c + b → a - b = c := @algebra.sub_eq_of_eq_add _ _ definition eq_add_of_sub_eq : Π{a b c : ℤ}, a - c = b → a = b + c := @algebra.eq_add_of_sub_eq _ _ definition add_eq_of_eq_sub : Π{a b c : ℤ}, a = c - b → a + b = c := @algebra.add_eq_of_eq_sub _ _ definition sub_add_eq_sub_sub : Πa b c : ℤ, a - (b + c) = a - b - c := algebra.sub_add_eq_sub_sub definition neg_add_eq_sub : Πa b : ℤ, -a + b = b - a := algebra.neg_add_eq_sub definition neg_add : Πa b : ℤ, -(a + b) = -a + -b := algebra.neg_add definition sub_add_eq_add_sub : Πa b c : ℤ, a - b + c = a + c - b := algebra.sub_add_eq_add_sub definition sub_sub_ : Πa b c : ℤ, a - b - c = a - (b + c) := algebra.sub_sub definition add_sub_add_left_eq_sub : Πa b c : ℤ, (c + a) - (c + b) = a - b := algebra.add_sub_add_left_eq_sub definition eq_sub_of_add_eq' : Π{a b c : ℤ}, c + a = b → a = b - c := @algebra.eq_sub_of_add_eq' _ _ definition sub_eq_of_eq_add' : Π{a b c : ℤ}, a = b + c → a - b = c := @algebra.sub_eq_of_eq_add' _ _ definition eq_add_of_sub_eq' : Π{a b c : ℤ}, a - b = c → a = b + c := @algebra.eq_add_of_sub_eq' _ _ definition add_eq_of_eq_sub' : Π{a b c : ℤ}, b = c - a → a + b = c := @algebra.add_eq_of_eq_sub' _ _ definition ne_zero_of_mul_ne_zero_right : Π{a b : ℤ}, a * b ≠ 0 → a ≠ 0 := @algebra.ne_zero_of_mul_ne_zero_right _ _ definition ne_zero_of_mul_ne_zero_left : Π{a b : ℤ}, a * b ≠ 0 → b ≠ 0 := @algebra.ne_zero_of_mul_ne_zero_left _ _ definition dvd (a b : ℤ) : Type₀ := algebra.dvd a b notation a ∣ b := dvd a b definition dvd.intro : Π{a b c : ℤ} (H : a * c = b), a ∣ b := @algebra.dvd.intro _ _ definition dvd.intro_left : Π{a b c : ℤ} (H : c * a = b), a ∣ b := @algebra.dvd.intro_left _ _ definition exists_eq_mul_right_of_dvd : Π{a b : ℤ} (H : a ∣ b), Σc, b = a * c := @algebra.exists_eq_mul_right_of_dvd _ _ definition dvd.elim : Π{P : Type} {a b : ℤ} (H₁ : a ∣ b) (H₂ : Πc, b = a * c → P), P := @algebra.dvd.elim _ _ definition exists_eq_mul_left_of_dvd : Π{a b : ℤ} (H : a ∣ b), Σc, b = c * a := @algebra.exists_eq_mul_left_of_dvd _ _ definition dvd.elim_left : Π{P : Type} {a b : ℤ} (H₁ : a ∣ b) (H₂ : Πc, b = c * a → P), P := @algebra.dvd.elim_left _ _ definition dvd.refl : Πa : ℤ, (a ∣ a) := algebra.dvd.refl definition dvd.trans : Π{a b c : ℤ} (H₁ : a ∣ b) (H₂ : b ∣ c), a ∣ c := @algebra.dvd.trans _ _ definition eq_zero_of_zero_dvd : Π{a : ℤ} (H : 0 ∣ a), a = 0 := @algebra.eq_zero_of_zero_dvd _ _ definition dvd_zero : Πa : ℤ, a ∣ 0 := algebra.dvd_zero definition one_dvd : Πa : ℤ, 1 ∣ a := algebra.one_dvd definition dvd_mul_right : Πa b : ℤ, a ∣ a * b := algebra.dvd_mul_right definition dvd_mul_left : Πa b : ℤ, a ∣ b * a := algebra.dvd_mul_left definition dvd_mul_of_dvd_left : Π{a b : ℤ} (H : a ∣ b) (c : ℤ), a ∣ b * c := @algebra.dvd_mul_of_dvd_left _ _ definition dvd_mul_of_dvd_right : Π{a b : ℤ} (H : a ∣ b) (c : ℤ), a ∣ c * b := @algebra.dvd_mul_of_dvd_right _ _ definition mul_dvd_mul : Π{a b c d : ℤ}, a ∣ b → c ∣ d → a * c ∣ b * d := @algebra.mul_dvd_mul _ _ definition dvd_of_mul_right_dvd : Π{a b c : ℤ}, a * b ∣ c → a ∣ c := @algebra.dvd_of_mul_right_dvd _ _ definition dvd_of_mul_left_dvd : Π{a b c : ℤ}, a * b ∣ c → b ∣ c := @algebra.dvd_of_mul_left_dvd _ _ definition dvd_add : Π{a b c : ℤ}, a ∣ b → a ∣ c → a ∣ b + c := @algebra.dvd_add _ _ definition zero_mul : Πa : ℤ, 0 * a = 0 := algebra.zero_mul definition mul_zero : Πa : ℤ, a * 0 = 0 := algebra.mul_zero definition neg_mul_eq_neg_mul : Πa b : ℤ, -(a * b) = -a * b := algebra.neg_mul_eq_neg_mul definition neg_mul_eq_mul_neg : Πa b : ℤ, -(a * b) = a * -b := algebra.neg_mul_eq_mul_neg definition neg_mul_neg : Πa b : ℤ, -a * -b = a * b := algebra.neg_mul_neg definition neg_mul_comm : Πa b : ℤ, -a * b = a * -b := algebra.neg_mul_comm definition neg_eq_neg_one_mul : Πa : ℤ, -a = -1 * a := algebra.neg_eq_neg_one_mul definition mul_sub_left_distrib : Πa b c : ℤ, a * (b - c) = a * b - a * c := algebra.mul_sub_left_distrib definition mul_sub_right_distrib : Πa b c : ℤ, (a - b) * c = a * c - b * c := algebra.mul_sub_right_distrib definition mul_add_eq_mul_add_iff_sub_mul_add_eq : Πa b c d e : ℤ, a * e + c = b * e + d ↔ (a - b) * e + c = d := algebra.mul_add_eq_mul_add_iff_sub_mul_add_eq definition mul_self_sub_mul_self_eq : Πa b : ℤ, a * a - b * b = (a + b) * (a - b) := algebra.mul_self_sub_mul_self_eq definition mul_self_sub_one_eq : Πa : ℤ, a * a - 1 = (a + 1) * (a - 1) := algebra.mul_self_sub_one_eq definition dvd_neg_iff_dvd : Πa b : ℤ, a ∣ -b ↔ a ∣ b := algebra.dvd_neg_iff_dvd definition neg_dvd_iff_dvd : Πa b : ℤ, -a ∣ b ↔ a ∣ b := algebra.neg_dvd_iff_dvd definition dvd_sub : Πa b c : ℤ, a ∣ b → a ∣ c → a ∣ b - c := algebra.dvd_sub definition mul_ne_zero : Π{a b : ℤ}, a ≠ 0 → b ≠ 0 → a * b ≠ 0 := @algebra.mul_ne_zero _ _ definition eq_of_mul_eq_mul_right : Π{a b c : ℤ}, a ≠ 0 → b * a = c * a → b = c := @algebra.eq_of_mul_eq_mul_right _ _ definition eq_of_mul_eq_mul_left : Π{a b c : ℤ}, a ≠ 0 → a * b = a * c → b = c := @algebra.eq_of_mul_eq_mul_left _ _ definition mul_self_eq_mul_self_iff : Πa b : ℤ, a * a = b * b ↔ a = b ⊎ a = -b := algebra.mul_self_eq_mul_self_iff definition mul_self_eq_one_iff : Πa : ℤ, a * a = 1 ↔ a = 1 ⊎ a = -1 := algebra.mul_self_eq_one_iff definition dvd_of_mul_dvd_mul_left : Π{a b c : ℤ}, a ≠ 0 → a*b ∣ a*c → b ∣ c := @algebra.dvd_of_mul_dvd_mul_left _ _ definition dvd_of_mul_dvd_mul_right : Π{a b c : ℤ}, a ≠ 0 → b*a ∣ c*a → b ∣ c := @algebra.dvd_of_mul_dvd_mul_right _ _ end port_algebra /- additional properties -/ definition of_nat_sub_of_nat {m n : ℕ} (H : #nat m ≥ n) : of_nat m - of_nat n = of_nat (#nat m - n) := have H1 : m = (#nat m - n + n), from (nat.sub_add_cancel H)⁻¹, have H2 : m = (#nat m - n) + n, from ap of_nat H1, sub_eq_of_eq_add H2 definition neg_succ_of_nat_eq' (m : ℕ) : -[m +1] = -m - 1 := by rewrite [neg_succ_of_nat_eq, -of_nat_add_of_nat, neg_add] definition succ (a : ℤ) := a + (nat.succ zero) definition pred (a : ℤ) := a - (nat.succ zero) definition pred_succ (a : ℤ) : pred (succ a) = a := !sub_add_cancel definition succ_pred (a : ℤ) : succ (pred a) = a := !add_sub_cancel definition neg_succ (a : ℤ) : -succ a = pred (-a) := by rewrite [↑succ,neg_add] definition succ_neg_succ (a : ℤ) : succ (-succ a) = -a := by rewrite [neg_succ,succ_pred] definition neg_pred (a : ℤ) : -pred a = succ (-a) := by rewrite [↑pred,neg_sub,sub_eq_add_neg,add.comm] definition pred_neg_pred (a : ℤ) : pred (-pred a) = -a := by rewrite [neg_pred,pred_succ] definition pred_nat_succ (n : ℕ) : pred (nat.succ n) = n := pred_succ n definition neg_nat_succ (n : ℕ) : -nat.succ n = pred (-n) := !neg_succ definition succ_neg_nat_succ (n : ℕ) : succ (-nat.succ n) = -n := !succ_neg_succ definition rec_nat_on [unfold 2] {P : ℤ → Type} (z : ℤ) (H0 : P 0) (Hsucc : Π⦃n : ℕ⦄, P n → P (succ n)) (Hpred : Π⦃n : ℕ⦄, P (-n) → P (-nat.succ n)) : P z := begin induction z with n n, {exact nat.rec_on n H0 Hsucc}, {induction n with m ih, exact Hpred H0, exact Hpred ih} end --the only computation rule of rec_nat_on which is not definitional definition rec_nat_on_neg {P : ℤ → Type} (n : nat) (H0 : P zero) (Hsucc : Π⦃n : nat⦄, P n → P (succ n)) (Hpred : Π⦃n : nat⦄, P (-n) → P (-nat.succ n)) : rec_nat_on (-nat.succ n) H0 Hsucc Hpred = Hpred (rec_nat_on (-n) H0 Hsucc Hpred) := nat.rec_on n rfl (λn H, rfl) end int
b98789c3bf3962c3725303eeaa6ba1c8010be1a1
59e5eb5b0b465111c363c967363a954891f7de2e
/Leancode/fptp/src/ElGamal.lean
e6e87c179f3080ae378b1a5cfd8eb88fe9b9e141
[ "Apache-2.0" ]
permissive
mukeshtiwari/formal-fptp
1987fb35e5594ea047b1b2f7e36a804dece6272f
883a6a245cfdce7af0fc3cc2c3769281fe60afa0
refs/heads/master
1,650,002,177,929
1,585,742,596,000
1,585,742,596,000
249,091,342
0
0
null
null
null
null
UTF-8
Lean
false
false
4,811
lean
import data.zmod.basic data.nat.prime data.zmod.quadratic_reciprocity tactic.find tactic.omega data.vector list_lemma namespace ElGamal /- A Schnorr group is a large prime-order subgroup of ℤ∗𝑝, the multiplicative group of integers modulo 𝑝. To generate such a group, we find 𝑝=𝑞𝑟+1 such that 𝑝 and 𝑞 are prime. Then, we choose any ℎ in the range 1<ℎ<𝑝 such that ℎ^r ≠ 1 (mod𝑝) The value 𝑔=ℎ^𝑟(mod𝑝) is a generator of a subgroup ℤ∗𝑝 of order 𝑞. By Fermat's little theorem g^q = h^(rq) = h^(p-1) = 1 (mod p) -/ variables (p : ℕ) (q : ℕ) (r : ℕ) (Hr : 2 ≤ r) (Hp : nat.prime p) (Hq : nat.prime q) (Hdiv : p = q * r + 1) (h : zmodp p Hp) (Hh₁ : h ≠ 0) (Hh₂ : h^r ≠ 1) (g : zmodp p Hp) /- generator of a subgroup of ℤ⋆p of order q -/ (Hg : g = h^r) section include Hg Hdiv Hh₁ theorem generator_proof : g ^ q = 1 := begin rw [Hg, <- pow_mul, mul_comm], have Ht : p - 1 = q * r := nat.pred_eq_of_eq_succ Hdiv, rw <- Ht, exact zmodp.fermat_little Hp Hh₁ end end variables (prikey : zmodp q Hq) /- private key -/ (pubkey : zmodp p Hp) /- public key -/ (Hrel : pubkey = g^prikey.val) def elgamal_enc (m : zmodp p Hp) (r : zmodp q Hq) := (g^r.val, g^m.val * pubkey^r.val) def elgamal_dec (c : zmodp p Hp × zmodp p Hp) := c.2 * (c.1^prikey.val)⁻¹ def elgamal_reenc (c : zmodp p Hp × zmodp p Hp) (r : zmodp q Hq) := (c.1 * g^r.val, c.2 * pubkey^r.val) def ciphertext_mult (c : zmodp p Hp × zmodp p Hp) (d : zmodp p Hp × zmodp p Hp) := (c.1 * d.1, c.2 * d.2) def vector_elegamal_enc {n : ℕ} : vector (zmodp p Hp) n -> vector (zmodp q Hq) n -> vector (zmodp p Hp × zmodp p Hp) n | ⟨ms, Hm⟩ ⟨rs, Hr⟩ := ⟨list.zip_with (elgamal_enc p q Hp Hq g pubkey) ms rs, begin have Ht : list.length ms = list.length rs := begin rw [Hm, Hr] end, rw <- Hm, apply zip_with_len_l, exact Ht end ⟩ def vector_elegamal_dec {n : ℕ} : vector (zmodp p Hp × zmodp p Hp) n -> vector (zmodp p Hp) n | ⟨cs, Hc⟩ := ⟨list.map (elgamal_dec p q Hp Hq prikey) cs, begin rw <- Hc, apply map_with_len_l, end ⟩ def vector_elegamal_reenc {n : ℕ} : vector (zmodp p Hp × zmodp p Hp) n -> vector (zmodp q Hq) n -> vector (zmodp p Hp × zmodp p Hp) n | ⟨cs, Hc⟩ ⟨rs, Hr⟩ := ⟨list.zip_with (elgamal_reenc p q Hp Hq g pubkey) cs rs, begin have Ht : list.length cs = list.length rs := begin rw [Hc, Hr] end, rw <- Hc, apply zip_with_len_l, exact Ht end ⟩ def vector_ciphertext_mult {n : ℕ} : vector (zmodp p Hp × zmodp p Hp) n -> vector (zmodp p Hp × zmodp p Hp) n -> vector (zmodp p Hp × zmodp p Hp) n | ⟨cs₁ , Hc₁⟩ ⟨cs₂, Hc₂⟩ := ⟨list.zip_with (ciphertext_mult p Hp) cs₁ cs₂, begin have Ht : list.length cs₁ = list.length cs₂ := begin rw [Hc₁, Hc₂] end, rw <- Hc₁, apply zip_with_len_l, exact Ht, end ⟩ #print vector_ciphertext_mult._main /- inductive count 1. a ballot encrypted with [g^0, g^0, ....] 2. a ballot is valid add it to the running margin 3. invalid, discard it and permute it by a secret permutation 4. when no more ballot left, then honest decryption Zero-Knowledge-Proof framework Wikstrom Shuffle proof Extend it to the Coq as well, plug these proofs in Coq. -/ include Hrel Hg Hh₁ theorem elgama_enc_dec_identity : ∀ m r', elgamal_dec p q Hp Hq prikey (elgamal_enc p q Hp Hq g pubkey m r') = g^m.val := begin unfold elgamal_enc elgamal_dec, intros, simp, rw [Hrel, <- pow_mul, <- pow_mul], have Ht : g ≠ 0 := begin rw Hg, exact pow_ne_zero r Hh₁ end, have Ht₁ : g ^ (prikey.val * r'.val) ≠ 0 := pow_ne_zero _ Ht, have Ht₂ : r'.val * prikey.val = prikey.val * r'.val := mul_comm r'.val prikey.val, rw [Ht₂, mul_assoc, mul_inv_cancel Ht₁], ring end theorem additive_homomorphic_property : forall c d m₁ m₂ r₁ r₂, c = elgamal_enc p q Hp Hq g pubkey m₁ r₁ -> d = elgamal_enc p q Hp Hq g pubkey m₂ r₂ -> (g^(r₁.val + r₂.val), g^(m₁.val + m₂.val) * pubkey^(r₁.val + r₂.val)) = ciphertext_mult p Hp c d := begin unfold elgamal_enc ciphertext_mult, intros c d m₁ m₂ r₁ r₂ Hc Hd, simp, have Ht₁ : g ^ (r₁.val + r₂.val) = c.fst * d.fst := begin rw [Hc, Hd], simp, exact pow_add g r₁.val r₂.val end, have Ht₂ : g ^ (m₁.val + m₂.val) * pubkey ^ (r₁.val + r₂.val) = c.snd * d.snd := begin rw [Hc, Hd, pow_add, pow_add], simp, ring end, exact and.intro Ht₁ Ht₂ end end ElGamal #check ElGamal.elgamal_enc
9e1566cd6e113f731151064365dac9c879cced5e
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/Semiring.lean
aa350ecfc56188b81d7a3ec63ecec55574ca3eec
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
12,570
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section Semiring structure Semiring (A : Type) : Type := (zero : A) (plus : (A → (A → A))) (times : (A → (A → A))) (associative_times : (∀ {x y z : A} , (times (times x y) z) = (times x (times y z)))) (one : A) (lunit_one : (∀ {x : A} , (times one x) = x)) (runit_one : (∀ {x : A} , (times x one) = x)) (lunit_zero : (∀ {x : A} , (plus zero x) = x)) (runit_zero : (∀ {x : A} , (plus x zero) = x)) (associative_plus : (∀ {x y z : A} , (plus (plus x y) z) = (plus x (plus y z)))) (commutative_plus : (∀ {x y : A} , (plus x y) = (plus y x))) (leftDistributive_times_plus : (∀ {x y z : A} , (times x (plus y z)) = (plus (times x y) (times x z)))) (rightDistributive_times_plus : (∀ {x y z : A} , (times (plus y z) x) = (plus (times y x) (times z x)))) (leftZero_op_zero : (∀ {x : A} , (times zero x) = zero)) (rightZero_op_zero : (∀ {x : A} , (times x zero) = zero)) open Semiring structure Sig (AS : Type) : Type := (zeroS : AS) (plusS : (AS → (AS → AS))) (timesS : (AS → (AS → AS))) (oneS : AS) structure Product (A : Type) : Type := (zeroP : (Prod A A)) (plusP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (timesP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (oneP : (Prod A A)) (associative_timesP : (∀ {xP yP zP : (Prod A A)} , (timesP (timesP xP yP) zP) = (timesP xP (timesP yP zP)))) (lunit_1P : (∀ {xP : (Prod A A)} , (timesP oneP xP) = xP)) (runit_1P : (∀ {xP : (Prod A A)} , (timesP xP oneP) = xP)) (lunit_0P : (∀ {xP : (Prod A A)} , (plusP zeroP xP) = xP)) (runit_0P : (∀ {xP : (Prod A A)} , (plusP xP zeroP) = xP)) (associative_plusP : (∀ {xP yP zP : (Prod A A)} , (plusP (plusP xP yP) zP) = (plusP xP (plusP yP zP)))) (commutative_plusP : (∀ {xP yP : (Prod A A)} , (plusP xP yP) = (plusP yP xP))) (leftDistributive_times_plusP : (∀ {xP yP zP : (Prod A A)} , (timesP xP (plusP yP zP)) = (plusP (timesP xP yP) (timesP xP zP)))) (rightDistributive_times_plusP : (∀ {xP yP zP : (Prod A A)} , (timesP (plusP yP zP) xP) = (plusP (timesP yP xP) (timesP zP xP)))) (leftZero_op_0P : (∀ {xP : (Prod A A)} , (timesP zeroP xP) = zeroP)) (rightZero_op_0P : (∀ {xP : (Prod A A)} , (timesP xP zeroP) = zeroP)) structure Hom {A1 : Type} {A2 : Type} (Se1 : (Semiring A1)) (Se2 : (Semiring A2)) : Type := (hom : (A1 → A2)) (pres_zero : (hom (zero Se1)) = (zero Se2)) (pres_plus : (∀ {x1 x2 : A1} , (hom ((plus Se1) x1 x2)) = ((plus Se2) (hom x1) (hom x2)))) (pres_times : (∀ {x1 x2 : A1} , (hom ((times Se1) x1 x2)) = ((times Se2) (hom x1) (hom x2)))) (pres_one : (hom (one Se1)) = (one Se2)) structure RelInterp {A1 : Type} {A2 : Type} (Se1 : (Semiring A1)) (Se2 : (Semiring A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_zero : (interp (zero Se1) (zero Se2))) (interp_plus : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((plus Se1) x1 x2) ((plus Se2) y1 y2)))))) (interp_times : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((times Se1) x1 x2) ((times Se2) y1 y2)))))) (interp_one : (interp (one Se1) (one Se2))) inductive SemiringTerm : Type | zeroL : SemiringTerm | plusL : (SemiringTerm → (SemiringTerm → SemiringTerm)) | timesL : (SemiringTerm → (SemiringTerm → SemiringTerm)) | oneL : SemiringTerm open SemiringTerm inductive ClSemiringTerm (A : Type) : Type | sing : (A → ClSemiringTerm) | zeroCl : ClSemiringTerm | plusCl : (ClSemiringTerm → (ClSemiringTerm → ClSemiringTerm)) | timesCl : (ClSemiringTerm → (ClSemiringTerm → ClSemiringTerm)) | oneCl : ClSemiringTerm open ClSemiringTerm inductive OpSemiringTerm (n : ℕ) : Type | v : ((fin n) → OpSemiringTerm) | zeroOL : OpSemiringTerm | plusOL : (OpSemiringTerm → (OpSemiringTerm → OpSemiringTerm)) | timesOL : (OpSemiringTerm → (OpSemiringTerm → OpSemiringTerm)) | oneOL : OpSemiringTerm open OpSemiringTerm inductive OpSemiringTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpSemiringTerm2) | sing2 : (A → OpSemiringTerm2) | zeroOL2 : OpSemiringTerm2 | plusOL2 : (OpSemiringTerm2 → (OpSemiringTerm2 → OpSemiringTerm2)) | timesOL2 : (OpSemiringTerm2 → (OpSemiringTerm2 → OpSemiringTerm2)) | oneOL2 : OpSemiringTerm2 open OpSemiringTerm2 def simplifyCl {A : Type} : ((ClSemiringTerm A) → (ClSemiringTerm A)) | (timesCl oneCl x) := x | (timesCl x oneCl) := x | (plusCl zeroCl x) := x | (plusCl x zeroCl) := x | zeroCl := zeroCl | (plusCl x1 x2) := (plusCl (simplifyCl x1) (simplifyCl x2)) | (timesCl x1 x2) := (timesCl (simplifyCl x1) (simplifyCl x2)) | oneCl := oneCl | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpSemiringTerm n) → (OpSemiringTerm n)) | (timesOL oneOL x) := x | (timesOL x oneOL) := x | (plusOL zeroOL x) := x | (plusOL x zeroOL) := x | zeroOL := zeroOL | (plusOL x1 x2) := (plusOL (simplifyOpB x1) (simplifyOpB x2)) | (timesOL x1 x2) := (timesOL (simplifyOpB x1) (simplifyOpB x2)) | oneOL := oneOL | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpSemiringTerm2 n A) → (OpSemiringTerm2 n A)) | (timesOL2 oneOL2 x) := x | (timesOL2 x oneOL2) := x | (plusOL2 zeroOL2 x) := x | (plusOL2 x zeroOL2) := x | zeroOL2 := zeroOL2 | (plusOL2 x1 x2) := (plusOL2 (simplifyOp x1) (simplifyOp x2)) | (timesOL2 x1 x2) := (timesOL2 (simplifyOp x1) (simplifyOp x2)) | oneOL2 := oneOL2 | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((Semiring A) → (SemiringTerm → A)) | Se zeroL := (zero Se) | Se (plusL x1 x2) := ((plus Se) (evalB Se x1) (evalB Se x2)) | Se (timesL x1 x2) := ((times Se) (evalB Se x1) (evalB Se x2)) | Se oneL := (one Se) def evalCl {A : Type} : ((Semiring A) → ((ClSemiringTerm A) → A)) | Se (sing x1) := x1 | Se zeroCl := (zero Se) | Se (plusCl x1 x2) := ((plus Se) (evalCl Se x1) (evalCl Se x2)) | Se (timesCl x1 x2) := ((times Se) (evalCl Se x1) (evalCl Se x2)) | Se oneCl := (one Se) def evalOpB {A : Type} {n : ℕ} : ((Semiring A) → ((vector A n) → ((OpSemiringTerm n) → A))) | Se vars (v x1) := (nth vars x1) | Se vars zeroOL := (zero Se) | Se vars (plusOL x1 x2) := ((plus Se) (evalOpB Se vars x1) (evalOpB Se vars x2)) | Se vars (timesOL x1 x2) := ((times Se) (evalOpB Se vars x1) (evalOpB Se vars x2)) | Se vars oneOL := (one Se) def evalOp {A : Type} {n : ℕ} : ((Semiring A) → ((vector A n) → ((OpSemiringTerm2 n A) → A))) | Se vars (v2 x1) := (nth vars x1) | Se vars (sing2 x1) := x1 | Se vars zeroOL2 := (zero Se) | Se vars (plusOL2 x1 x2) := ((plus Se) (evalOp Se vars x1) (evalOp Se vars x2)) | Se vars (timesOL2 x1 x2) := ((times Se) (evalOp Se vars x1) (evalOp Se vars x2)) | Se vars oneOL2 := (one Se) def inductionB {P : (SemiringTerm → Type)} : ((P zeroL) → ((∀ (x1 x2 : SemiringTerm) , ((P x1) → ((P x2) → (P (plusL x1 x2))))) → ((∀ (x1 x2 : SemiringTerm) , ((P x1) → ((P x2) → (P (timesL x1 x2))))) → ((P oneL) → (∀ (x : SemiringTerm) , (P x)))))) | p0l pplusl ptimesl p1l zeroL := p0l | p0l pplusl ptimesl p1l (plusL x1 x2) := (pplusl _ _ (inductionB p0l pplusl ptimesl p1l x1) (inductionB p0l pplusl ptimesl p1l x2)) | p0l pplusl ptimesl p1l (timesL x1 x2) := (ptimesl _ _ (inductionB p0l pplusl ptimesl p1l x1) (inductionB p0l pplusl ptimesl p1l x2)) | p0l pplusl ptimesl p1l oneL := p1l def inductionCl {A : Type} {P : ((ClSemiringTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((P zeroCl) → ((∀ (x1 x2 : (ClSemiringTerm A)) , ((P x1) → ((P x2) → (P (plusCl x1 x2))))) → ((∀ (x1 x2 : (ClSemiringTerm A)) , ((P x1) → ((P x2) → (P (timesCl x1 x2))))) → ((P oneCl) → (∀ (x : (ClSemiringTerm A)) , (P x))))))) | psing p0cl ppluscl ptimescl p1cl (sing x1) := (psing x1) | psing p0cl ppluscl ptimescl p1cl zeroCl := p0cl | psing p0cl ppluscl ptimescl p1cl (plusCl x1 x2) := (ppluscl _ _ (inductionCl psing p0cl ppluscl ptimescl p1cl x1) (inductionCl psing p0cl ppluscl ptimescl p1cl x2)) | psing p0cl ppluscl ptimescl p1cl (timesCl x1 x2) := (ptimescl _ _ (inductionCl psing p0cl ppluscl ptimescl p1cl x1) (inductionCl psing p0cl ppluscl ptimescl p1cl x2)) | psing p0cl ppluscl ptimescl p1cl oneCl := p1cl def inductionOpB {n : ℕ} {P : ((OpSemiringTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((P zeroOL) → ((∀ (x1 x2 : (OpSemiringTerm n)) , ((P x1) → ((P x2) → (P (plusOL x1 x2))))) → ((∀ (x1 x2 : (OpSemiringTerm n)) , ((P x1) → ((P x2) → (P (timesOL x1 x2))))) → ((P oneOL) → (∀ (x : (OpSemiringTerm n)) , (P x))))))) | pv p0ol pplusol ptimesol p1ol (v x1) := (pv x1) | pv p0ol pplusol ptimesol p1ol zeroOL := p0ol | pv p0ol pplusol ptimesol p1ol (plusOL x1 x2) := (pplusol _ _ (inductionOpB pv p0ol pplusol ptimesol p1ol x1) (inductionOpB pv p0ol pplusol ptimesol p1ol x2)) | pv p0ol pplusol ptimesol p1ol (timesOL x1 x2) := (ptimesol _ _ (inductionOpB pv p0ol pplusol ptimesol p1ol x1) (inductionOpB pv p0ol pplusol ptimesol p1ol x2)) | pv p0ol pplusol ptimesol p1ol oneOL := p1ol def inductionOp {n : ℕ} {A : Type} {P : ((OpSemiringTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((P zeroOL2) → ((∀ (x1 x2 : (OpSemiringTerm2 n A)) , ((P x1) → ((P x2) → (P (plusOL2 x1 x2))))) → ((∀ (x1 x2 : (OpSemiringTerm2 n A)) , ((P x1) → ((P x2) → (P (timesOL2 x1 x2))))) → ((P oneOL2) → (∀ (x : (OpSemiringTerm2 n A)) , (P x)))))))) | pv2 psing2 p0ol2 pplusol2 ptimesol2 p1ol2 (v2 x1) := (pv2 x1) | pv2 psing2 p0ol2 pplusol2 ptimesol2 p1ol2 (sing2 x1) := (psing2 x1) | pv2 psing2 p0ol2 pplusol2 ptimesol2 p1ol2 zeroOL2 := p0ol2 | pv2 psing2 p0ol2 pplusol2 ptimesol2 p1ol2 (plusOL2 x1 x2) := (pplusol2 _ _ (inductionOp pv2 psing2 p0ol2 pplusol2 ptimesol2 p1ol2 x1) (inductionOp pv2 psing2 p0ol2 pplusol2 ptimesol2 p1ol2 x2)) | pv2 psing2 p0ol2 pplusol2 ptimesol2 p1ol2 (timesOL2 x1 x2) := (ptimesol2 _ _ (inductionOp pv2 psing2 p0ol2 pplusol2 ptimesol2 p1ol2 x1) (inductionOp pv2 psing2 p0ol2 pplusol2 ptimesol2 p1ol2 x2)) | pv2 psing2 p0ol2 pplusol2 ptimesol2 p1ol2 oneOL2 := p1ol2 def stageB : (SemiringTerm → (Staged SemiringTerm)) | zeroL := (Now zeroL) | (plusL x1 x2) := (stage2 plusL (codeLift2 plusL) (stageB x1) (stageB x2)) | (timesL x1 x2) := (stage2 timesL (codeLift2 timesL) (stageB x1) (stageB x2)) | oneL := (Now oneL) def stageCl {A : Type} : ((ClSemiringTerm A) → (Staged (ClSemiringTerm A))) | (sing x1) := (Now (sing x1)) | zeroCl := (Now zeroCl) | (plusCl x1 x2) := (stage2 plusCl (codeLift2 plusCl) (stageCl x1) (stageCl x2)) | (timesCl x1 x2) := (stage2 timesCl (codeLift2 timesCl) (stageCl x1) (stageCl x2)) | oneCl := (Now oneCl) def stageOpB {n : ℕ} : ((OpSemiringTerm n) → (Staged (OpSemiringTerm n))) | (v x1) := (const (code (v x1))) | zeroOL := (Now zeroOL) | (plusOL x1 x2) := (stage2 plusOL (codeLift2 plusOL) (stageOpB x1) (stageOpB x2)) | (timesOL x1 x2) := (stage2 timesOL (codeLift2 timesOL) (stageOpB x1) (stageOpB x2)) | oneOL := (Now oneOL) def stageOp {n : ℕ} {A : Type} : ((OpSemiringTerm2 n A) → (Staged (OpSemiringTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | zeroOL2 := (Now zeroOL2) | (plusOL2 x1 x2) := (stage2 plusOL2 (codeLift2 plusOL2) (stageOp x1) (stageOp x2)) | (timesOL2 x1 x2) := (stage2 timesOL2 (codeLift2 timesOL2) (stageOp x1) (stageOp x2)) | oneOL2 := (Now oneOL2) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (zeroT : (Repr A)) (plusT : ((Repr A) → ((Repr A) → (Repr A)))) (timesT : ((Repr A) → ((Repr A) → (Repr A)))) (oneT : (Repr A)) end Semiring
7209def54abe2828447a028c6a009458ce790ffb
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/algebra/big_operators/nat_antidiagonal.lean
4abb48d4be2229dce53b821926adacaab2572df9
[ "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
958
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import data.finset.nat_antidiagonal import algebra.big_operators.basic /-! # Big operators for `nat_antidiagonal` This file contains theorems relevant to big operators over `finset.nat.antidiagonal`. -/ open_locale big_operators variables {α : Type*} [add_comm_monoid α] namespace finset namespace nat lemma sum_antidiagonal_succ {n : ℕ} {f : ℕ × ℕ → α} : (antidiagonal (n + 1)).sum f = f (0, n + 1) + ((antidiagonal n).map (function.embedding.prod_map ⟨nat.succ, nat.succ_injective⟩ (function.embedding.refl _))).sum f := begin rw [antidiagonal_succ, sum_insert], intro con, rcases mem_map.1 con with ⟨⟨a,b⟩, ⟨h1, h2⟩⟩, simp only [prod.mk.inj_iff, function.embedding.coe_prod_map, prod.map_mk] at h2, apply nat.succ_ne_zero a h2.1, end end nat end finset
00822c5094877d9f0ee038a1f03b332bb3a664f2
64874bd1010548c7f5a6e3e8902efa63baaff785
/library/algebra/order.lean
788fb8cf0b0823ed2815044bbcbf536e021eda0a
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,869
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: algebra.order Author: Jeremy Avigad Various types of orders. We develop weak orders "≤" and strict orders "<" separately. We also consider structures with both, where the two are related by x < y ↔ (x ≤ y ∧ x ≠ y) (order_pair) x ≤ y ↔ (x < y ∨ x = y) (strong_order_pair) These might not hold constructively in some applications, but we can define additional structures with both < and ≤ as needed. -/ import logic.eq logic.connectives open eq eq.ops namespace algebra variable {A : Type} /- overloaded symbols -/ structure has_le [class] (A : Type) := (le : A → A → Prop) structure has_lt [class] (A : Type) := (lt : A → A → Prop) infixl `<=` := has_le.le infixl `≤` := has_le.le infixl `<` := has_lt.lt definition has_le.ge [reducible] {A : Type} [s : has_le A] (a b : A) := b ≤ a notation a ≥ b := has_le.ge a b notation a >= b := has_le.ge a b definition has_lt.gt [reducible] {A : Type} [s : has_lt A] (a b : A) := b < a notation a > b := has_lt.gt a b theorem le_of_eq_of_le {A : Type} [s : has_le A] {a b c : A} (H1 : a = b) (H2 : b ≤ c) : a ≤ c := H1⁻¹ ▸ H2 theorem le_of_le_of_eq {A : Type} [s : has_le A] {a b c : A} (H1 : a ≤ b) (H2 : b = c) : a ≤ c := H2 ▸ H1 theorem lt_of_eq_of_lt {A : Type} [s : has_lt A] {a b c : A} (H1 : a = b) (H2 : b < c) : a < c := H1⁻¹ ▸ H2 theorem lt_of_lt_of_eq {A : Type} [s : has_lt A] {a b c : A} (H1 : a < b) (H2 : b = c) : a < c := H2 ▸ H1 theorem ge_of_eq_of_ge {A : Type} [s : has_le A] {a b c : A} (H1 : a = b) (H2 : b ≥ c) : a ≥ c := H1⁻¹ ▸ H2 theorem ge_of_ge_of_eq {A : Type} [s : has_le A] {a b c : A} (H1 : a ≥ b) (H2 : b = c) : a ≥ c := H2 ▸ H1 theorem gt_of_eq_of_gt {A : Type} [s : has_lt A] {a b c : A} (H1 : a = b) (H2 : b > c) : a > c := H1⁻¹ ▸ H2 theorem gt_of_gt_of_eq {A : Type} [s : has_lt A] {a b c : A} (H1 : a > b) (H2 : b = c) : a > c := H2 ▸ H1 calc_trans le_of_eq_of_le calc_trans le_of_le_of_eq calc_trans lt_of_eq_of_lt calc_trans lt_of_lt_of_eq calc_trans ge_of_eq_of_ge calc_trans ge_of_ge_of_eq calc_trans gt_of_eq_of_gt calc_trans gt_of_gt_of_eq /- weak orders -/ structure weak_order [class] (A : Type) extends has_le A := (le_refl : ∀a, le a a) (le_trans : ∀a b c, le a b → le b c → le a c) (le_antisymm : ∀a b, le a b → le b a → a = b) section variable [s : weak_order A] include s theorem le.refl (a : A) : a ≤ a := !weak_order.le_refl theorem le.trans {a b c : A} : a ≤ b → b ≤ c → a ≤ c := !weak_order.le_trans calc_trans le.trans theorem ge.trans {a b c : A} (H1 : a ≥ b) (H2: b ≥ c) : a ≥ c := le.trans H2 H1 calc_trans ge.trans theorem le.antisymm {a b : A} : a ≤ b → b ≤ a → a = b := !weak_order.le_antisymm end structure linear_weak_order [class] (A : Type) extends weak_order A := (le_total : ∀a b, le a b ∨ le b a) theorem le.total [s : linear_weak_order A] (a b : A) : a ≤ b ∨ b ≤ a := !linear_weak_order.le_total /- strict orders -/ structure strict_order [class] (A : Type) extends has_lt A := (lt_irrefl : ∀a, ¬ lt a a) (lt_trans : ∀a b c, lt a b → lt b c → lt a c) section variable [s : strict_order A] include s theorem lt.irrefl (a : A) : ¬ a < a := !strict_order.lt_irrefl theorem lt.trans {a b c : A} : a < b → b < c → a < c := !strict_order.lt_trans calc_trans lt.trans theorem gt.trans {a b c : A} (H1 : a > b) (H2: b > c) : a > c := lt.trans H2 H1 calc_trans gt.trans theorem ne_of_lt {a b : A} : a < b → a ≠ b := assume lt_ab : a < b, assume eq_ab : a = b, show false, from lt.irrefl b (eq_ab ▸ lt_ab) theorem lt.asymm {a b : A} (H : a < b) : ¬ b < a := assume H1 : b < a, lt.irrefl _ (lt.trans H H1) end /- well-founded orders -/ -- TODO: do these duplicate what Leo has done? if so, eliminate structure wf_strict_order [class] (A : Type) extends strict_order A := (wf_rec : ∀P : A → Type, (∀x, (∀y, lt y x → P y) → P x) → ∀x, P x) definition wf.rec_on {A : Type} [s : wf_strict_order A] {P : A → Type} (x : A) (H : ∀x, (∀y, wf_strict_order.lt y x → P y) → P x) : P x := wf_strict_order.wf_rec P H x theorem wf.ind_on.{u v} {A : Type.{u}} [s : wf_strict_order.{u 0} A] {P : A → Prop} (x : A) (H : ∀x, (∀y, wf_strict_order.lt y x → P y) → P x) : P x := wf.rec_on x H /- structures with a weak and a strict order -/ structure order_pair [class] (A : Type) extends weak_order A, has_lt A := (lt_iff_le_ne : ∀a b, lt a b ↔ (le a b ∧ a ≠ b)) section variable [s : order_pair A] variables {a b c : A} include s theorem lt_iff_le_and_ne : a < b ↔ (a ≤ b ∧ a ≠ b) := !order_pair.lt_iff_le_ne theorem le_of_lt (H : a < b) : a ≤ b := and.elim_left (iff.mp lt_iff_le_and_ne H) theorem lt_of_le_of_ne (H1 : a ≤ b) (H2 : a ≠ b) : a < b := iff.mp (iff.symm lt_iff_le_and_ne) (and.intro H1 H2) private theorem lt_irrefl (s : order_pair A) (a : A) : ¬ a < a := assume H : a < a, have H1 : a ≠ a, from and.elim_right (iff.mp !lt_iff_le_and_ne H), H1 rfl private theorem lt_trans (s : order_pair A) (a b c: A) (lt_ab : a < b) (lt_bc : b < c) : a < c := have le_ab : a ≤ b, from le_of_lt lt_ab, have le_bc : b ≤ c, from le_of_lt lt_bc, have le_ac : a ≤ c, from le.trans le_ab le_bc, have ne_ac : a ≠ c, from assume eq_ac : a = c, have le_ba : b ≤ a, from eq_ac⁻¹ ▸ le_bc, have eq_ab : a = b, from le.antisymm le_ab le_ba, have ne_ab : a ≠ b, from and.elim_right (iff.mp lt_iff_le_and_ne lt_ab), ne_ab eq_ab, show a < c, from lt_of_le_of_ne le_ac ne_ac definition order_pair.to_strict_order [instance] [coercion] [reducible] [s : order_pair A] : strict_order A := ⦃ strict_order, s, lt_irrefl := lt_irrefl s, lt_trans := lt_trans s ⦄ theorem lt_of_lt_of_le : a < b → b ≤ c → a < c := assume lt_ab : a < b, assume le_bc : b ≤ c, have le_ac : a ≤ c, from le.trans (le_of_lt lt_ab) le_bc, have ne_ac : a ≠ c, from assume eq_ac : a = c, have le_ba : b ≤ a, from eq_ac⁻¹ ▸ le_bc, have eq_ab : a = b, from le.antisymm (le_of_lt lt_ab) le_ba, show false, from ne_of_lt lt_ab eq_ab, show a < c, from lt_of_le_of_ne le_ac ne_ac theorem lt_of_le_of_lt : a ≤ b → b < c → a < c := assume le_ab : a ≤ b, assume lt_bc : b < c, have le_ac : a ≤ c, from le.trans le_ab (le_of_lt lt_bc), have ne_ac : a ≠ c, from assume eq_ac : a = c, have le_cb : c ≤ b, from eq_ac ▸ le_ab, have eq_bc : b = c, from le.antisymm (le_of_lt lt_bc) le_cb, show false, from ne_of_lt lt_bc eq_bc, show a < c, from lt_of_le_of_ne le_ac ne_ac theorem gt_of_gt_of_ge (H1 : a > b) (H2 : b ≥ c) : a > c := lt_of_le_of_lt H2 H1 theorem gt_of_ge_of_gt (H1 : a ≥ b) (H2 : b > c) : a > c := lt_of_lt_of_le H2 H1 calc_trans lt_of_lt_of_le calc_trans lt_of_le_of_lt calc_trans gt_of_gt_of_ge calc_trans gt_of_ge_of_gt theorem not_le_of_lt (H : a < b) : ¬ b ≤ a := assume H1 : b ≤ a, lt.irrefl _ (lt_of_lt_of_le H H1) theorem not_lt_of_le (H : a ≤ b) : ¬ b < a := assume H1 : b < a, lt.irrefl _ (lt_of_le_of_lt H H1) end structure strong_order_pair [class] (A : Type) extends order_pair A := (le_iff_lt_or_eq : ∀a b, le a b ↔ lt a b ∨ a = b) theorem le_iff_lt_or_eq [s : strong_order_pair A] {a b : A} : a ≤ b ↔ a < b ∨ a = b := !strong_order_pair.le_iff_lt_or_eq theorem lt_or_eq_of_le [s : strong_order_pair A] {a b : A} (le_ab : a ≤ b) : a < b ∨ a = b := iff.mp le_iff_lt_or_eq le_ab -- We can also construct a strong order pair by defining a strict order, and then defining -- x ≤ y ↔ x < y ∨ x = y structure strict_order_with_le [class] (A : Type) extends strict_order A, has_le A := (le_iff_lt_or_eq : ∀a b, le a b ↔ lt a b ∨ a = b) private theorem le_refl (s : strict_order_with_le A) (a : A) : a ≤ a := iff.mp (iff.symm !strict_order_with_le.le_iff_lt_or_eq) (or.intro_right _ rfl) private theorem le_trans (s : strict_order_with_le A) (a b c : A) (le_ab : a ≤ b) (le_bc : b ≤ c) : a ≤ c := or.elim (iff.mp !strict_order_with_le.le_iff_lt_or_eq le_ab) (assume lt_ab : a < b, or.elim (iff.mp !strict_order_with_le.le_iff_lt_or_eq le_bc) (assume lt_bc : b < c, iff.elim_right !strict_order_with_le.le_iff_lt_or_eq (or.intro_left _ (lt.trans lt_ab lt_bc))) (assume eq_bc : b = c, eq_bc ▸ le_ab)) (assume eq_ab : a = b, eq_ab⁻¹ ▸ le_bc) private theorem le_antisymm (s : strict_order_with_le A) (a b : A) (le_ab : a ≤ b) (le_ba : b ≤ a) : a = b := or.elim (iff.mp !strict_order_with_le.le_iff_lt_or_eq le_ab) (assume lt_ab : a < b, or.elim (iff.mp !strict_order_with_le.le_iff_lt_or_eq le_ba) (assume lt_ba : b < a, absurd (lt.trans lt_ab lt_ba) (lt.irrefl a)) (assume eq_ba : b = a, eq_ba⁻¹)) (assume eq_ab : a = b, eq_ab) private theorem lt_iff_le_ne (s : strict_order_with_le A) (a b : A) : a < b ↔ a ≤ b ∧ a ≠ b := iff.intro (assume lt_ab : a < b, have le_ab : a ≤ b, from iff.elim_right !strict_order_with_le.le_iff_lt_or_eq (or.intro_left _ lt_ab), show a ≤ b ∧ a ≠ b, from and.intro le_ab (ne_of_lt lt_ab)) (assume H : a ≤ b ∧ a ≠ b, have H1 : a < b ∨ a = b, from iff.mp !strict_order_with_le.le_iff_lt_or_eq (and.elim_left H), show a < b, from or_resolve_left H1 (and.elim_right H)) definition strict_order_with_le.to_order_pair [instance] [coercion] [reducible] [s : strict_order_with_le A] : strong_order_pair A := ⦃ strong_order_pair, s, le_refl := le_refl s, le_trans := le_trans s, le_antisymm := le_antisymm s, lt_iff_le_ne := lt_iff_le_ne s ⦄ /- linear orders -/ structure linear_order_pair [class] (A : Type) extends order_pair A, linear_weak_order A structure linear_strong_order_pair [class] (A : Type) extends strong_order_pair A, linear_weak_order A section variable [s : linear_strong_order_pair A] variables (a b c : A) include s theorem lt.trichotomy : a < b ∨ a = b ∨ b < a := or.elim (le.total a b) (assume H : a ≤ b, or.elim (iff.mp !le_iff_lt_or_eq H) (assume H1, or.inl H1) (assume H1, or.inr (or.inl H1))) (assume H : b ≤ a, or.elim (iff.mp !le_iff_lt_or_eq H) (assume H1, or.inr (or.inr H1)) (assume H1, or.inr (or.inl (H1⁻¹)))) theorem lt.by_cases {a b : A} {P : Prop} (H1 : a < b → P) (H2 : a = b → P) (H3 : b < a → P) : P := or.elim !lt.trichotomy (assume H, H1 H) (assume H, or.elim H (assume H', H2 H') (assume H', H3 H')) definition linear_strong_order_pair.to_linear_order_pair [instance] [coercion] [reducible] [s : linear_strong_order_pair A] : linear_order_pair A := ⦃ linear_order_pair, s ⦄ theorem le_of_not_lt {a b : A} (H : ¬ a < b) : b ≤ a := lt.by_cases (assume H', absurd H' H) (assume H', H' ▸ !le.refl) (assume H', le_of_lt H') theorem lt_of_not_le {a b : A} (H : ¬ a ≤ b) : b < a := lt.by_cases (assume H', absurd (le_of_lt H') H) (assume H', absurd (H' ▸ !le.refl) H) (assume H', H') theorem lt_or_ge : a < b ∨ a ≥ b := lt.by_cases (assume H1 : a < b, or.inl H1) (assume H1 : a = b, or.inr (H1 ▸ le.refl a)) (assume H1 : a > b, or.inr (le_of_lt H1)) theorem le_or_gt : a ≤ b ∨ a > b := !or.swap (lt_or_ge b a) theorem lt_or_gt_of_ne {a b : A} (H : a ≠ b) : a < b ∨ a > b := lt.by_cases (assume H1, or.inl H1) (assume H1, absurd H1 H) (assume H1, or.inr H1) end structure decidable_linear_order [class] (A : Type) extends linear_strong_order_pair A := (decidable_lt : decidable_rel lt) section variable [s : decidable_linear_order A] variables {a b c d : A} include s open decidable definition decidable_lt [instance] : decidable (a < b) := @decidable_linear_order.decidable_lt _ _ _ _ definition decidable_le [instance] : decidable (a ≤ b) := by_cases (assume H : a < b, inl (le_of_lt H)) (assume H : ¬ a < b, have H1 : b ≤ a, from le_of_not_lt H, by_cases (assume H2 : b < a, inr (not_le_of_lt H2)) (assume H2 : ¬ b < a, inl (le_of_not_lt H2))) definition decidable_eq [instance] : decidable (a = b) := by_cases (assume H : a ≤ b, by_cases (assume H1 : b ≤ a, inl (le.antisymm H H1)) (assume H1 : ¬ b ≤ a, inr (assume H2 : a = b, H1 (H2 ▸ le.refl a)))) (assume H : ¬ a ≤ b, (inr (assume H1 : a = b, H (H1 ▸ !le.refl)))) -- testing equality first may result in more definitional equalities definition lt.cases {B : Type} (a b : A) (t_lt t_eq t_gt : B) : B := if a = b then t_eq else (if a < b then t_lt else t_gt) theorem lt.cases_of_eq {B : Type} {a b : A} {t_lt t_eq t_gt : B} (H : a = b) : lt.cases a b t_lt t_eq t_gt = t_eq := if_pos H theorem lt.cases_of_lt {B : Type} {a b : A} {t_lt t_eq t_gt : B} (H : a < b) : lt.cases a b t_lt t_eq t_gt = t_lt := if_neg (ne_of_lt H) ⬝ if_pos H theorem lt.cases_of_gt {B : Type} {a b : A} {t_lt t_eq t_gt : B} (H : a > b) : lt.cases a b t_lt t_eq t_gt = t_gt := if_neg (ne.symm (ne_of_lt H)) ⬝ if_neg (lt.asymm H) end end algebra /- For reference, these are all the transitivity rules defined in this file: calc_trans le_of_eq_of_le calc_trans le_of_le_of_eq calc_trans lt_of_eq_of_lt calc_trans lt_of_lt_of_eq calc_trans le.trans calc_trans lt.trans calc_trans lt_of_lt_of_le calc_trans lt_of_le_of_lt calc_trans ge_of_eq_of_ge calc_trans ge_of_ge_of_eq calc_trans gt_of_eq_of_gt calc_trans gt_of_gt_of_eq calc_trans ge.trans calc_trans gt.trans calc_trans gt_of_gt_of_ge calc_trans gt_of_ge_of_gt -/
058747be5548083b932d8d013c5acb113ef8337c
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/sheaves/sheaf_condition/sites.lean
7cb8f2bfb00d0bd98c25a1a1c66f1add0e111b7a
[ "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
22,057
lean
/- Copyright (c) 2021 Justus Springer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Justus Springer -/ import category_theory.sites.spaces import topology.sheaves.sheaf import category_theory.sites.dense_subsite /-! # The sheaf condition in terms of sites. The theory of sheaves on sites was developed independently from sheaves on spaces in `category_theory/sites`. In this file, we connect the two theories: We show that for a topological space `X`, a presheaf `F : (opens X)ᵒᵖ ⥤ C` is a sheaf on the site `opens X` if and only if it is a sheaf on `X` in terms of `is_sheaf_equalizer_products`. Recall that a presheaf `F : (opens X)ᵒᵖ ⥤ C` is called a *sheaf* on the space `X`, if for every family of opens `U : ι → opens X`, the object `F.obj (op (supr U))` is the limit of some fork diagram. On the other hand, `F` is called a *sheaf* on the site `opens X`, if for every open set `U : opens X` and every presieve `R : presieve U`, the object `F.obj (op U)` is the limit of a very similar fork diagram. In this file, we will construct the two functions `covering_of_presieve` and `presieve_of_covering`, which translate between the two concepts. We then prove a bunch of naturality lemmas relating the two fork diagrams to each other. ## Main statements * `is_sheaf_iff_is_sheaf_equalizer_products`. A presheaf `F : (opens X)ᵒᵖ ⥤ C` is a sheaf on the site `opens X` if and only if it is a sheaf on the space `X`. -/ noncomputable theory universes u v w namespace Top.presheaf open category_theory topological_space Top category_theory.limits opposite open Top.presheaf.sheaf_condition_equalizer_products variables {C : Type u} [category.{v} C] [has_products.{v} C] variables {X : Top.{v}} (F : presheaf C X) /-- Given a presieve `R` on `U`, we obtain a covering family of open sets in `X`, by taking as index type the type of dependent pairs `(V, f)`, where `f : V ⟶ U` is in `R`. -/ def covering_of_presieve (U : opens X) (R : presieve U) : (Σ V, {f : V ⟶ U // R f}) → opens X := λ f, f.1 @[simp] lemma covering_of_presieve_apply (U : opens X) (R : presieve U) (f : Σ V, {f : V ⟶ U // R f}) : covering_of_presieve U R f = f.1 := rfl namespace covering_of_presieve variables (U : opens X) (R : presieve U) /-! In this section, we will relate two different fork diagrams to each other. The first one is the defining fork diagram for the sheaf condition in terms of sites, applied to the presieve `R`. It will henceforth be called the _sites diagram_. Its objects are called `presheaf.first_obj` and `presheaf.second_obj` and its morphisms are `presheaf.first_map` and `presheaf.second_obj`. The fork map into this diagram is called `presheaf.fork_map`. The second one is the defining fork diagram for the sheaf condition in terms of spaces, applied to the family of opens `covering_of_presieve U R`. It will henceforth be called the _spaces diagram_. Its objects are called `pi_opens` and `pi_inters` and its morphisms are `left_res` and `right_res`. The fork map into this diagram is called `res`. -/ /-- If `R` is a presieve in the grothendieck topology on `opens X`, the covering family associated to `R` really is _covering_, i.e. the union of all open sets equals `U`. -/ lemma supr_eq_of_mem_grothendieck (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : supr (covering_of_presieve U R) = U := begin apply le_antisymm, { refine supr_le _, intro f, exact f.2.1.le, }, intros x hxU, rw [opens.mem_coe, opens.mem_supr], obtain ⟨V, iVU, ⟨W, iVW, iWU, hiWU, -⟩, hxV⟩ := hR x hxU, exact ⟨⟨W, ⟨iWU, hiWU⟩⟩, iVW.le hxV⟩, end /-- The first object in the sites diagram is isomorphic to the first object in the spaces diagram. Actually, they are even definitionally equal, but it is convenient to give this isomorphism a name. -/ def first_obj_iso_pi_opens : presheaf.first_obj R F ≅ pi_opens F (covering_of_presieve U R) := eq_to_iso rfl /-- The isomorphism `first_obj_iso_pi_opens` is compatible with canonical projections out of the product. -/ lemma first_obj_iso_pi_opens_π (f : Σ V, {f : V ⟶ U // R f}) : (first_obj_iso_pi_opens F U R).hom ≫ pi.π _ f = pi.π _ f := category.id_comp _ /-- The second object in the sites diagram is isomorphic to the second object in the spaces diagram. -/ def second_obj_iso_pi_inters : presheaf.second_obj R F ≅ pi_inters F (covering_of_presieve U R) := has_limit.iso_of_nat_iso $ discrete.nat_iso $ λ i, F.map_iso (eq_to_iso (complete_lattice.pullback_eq_inf _ _).symm).op /-- The isomorphism `second_obj_iso_pi_inters` is compatible with canonical projections out of the product. Here, we have to insert an `eq_to_hom` arrow to pass from `F.obj (op (pullback f.2.1 g.2.1))` to `F.obj (op (f.1 ⊓ g.1))`. -/ lemma second_obj_iso_pi_inters_π (f g : Σ V, {f : V ⟶ U // R f}) : (second_obj_iso_pi_inters F U R).hom ≫ pi.π _ (f, g) = pi.π _ (f, g) ≫ F.map (eq_to_hom (complete_lattice.pullback_eq_inf f.2.1 g.2.1).symm).op := begin dunfold second_obj_iso_pi_inters, rw has_limit.iso_of_nat_iso_hom_π, refl, end /-- Composing the fork map of the sites diagram with the isomorphism `first_obj_iso_pi_opens` is the same as the fork map of the spaces diagram (modulo an `eq_to_hom` arrow). -/ lemma fork_map_comp_first_obj_iso_pi_opens_eq (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : presheaf.fork_map R F ≫ (first_obj_iso_pi_opens F U R).hom = F.map (eq_to_hom (supr_eq_of_mem_grothendieck U R hR)).op ≫ res F (covering_of_presieve U R) := begin ext ⟨f⟩, rw [category.assoc, category.assoc, first_obj_iso_pi_opens_π], dunfold presheaf.fork_map res, rw [limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, ← F.map_comp], congr, end /-- First naturality condition. Under the isomorphisms `first_obj_iso_pi_opens` and `second_obj_iso_pi_inters`, the map `presheaf.first_map` corresponds to `left_res`. -/ lemma first_obj_iso_comp_left_res_eq : presheaf.first_map R F ≫ (second_obj_iso_pi_inters F U R).hom = (first_obj_iso_pi_opens F U R).hom ≫ left_res F (covering_of_presieve U R) := begin ext ⟨f, g⟩, rw [category.assoc, category.assoc, second_obj_iso_pi_inters_π], dunfold left_res presheaf.first_map, rw [limit.lift_π, fan.mk_π_app, limit.lift_π_assoc, fan.mk_π_app, ← category.assoc], erw [first_obj_iso_pi_opens_π, category.assoc, ← F.map_comp], refl, end /-- Second naturality condition. Under the isomorphisms `first_obj_iso_pi_opens` and `second_obj_iso_pi_inters`, the map `presheaf.second_map` corresponds to `right_res`. -/ lemma first_obj_iso_comp_right_res_eq : presheaf.second_map R F ≫ (second_obj_iso_pi_inters F U R).hom = (first_obj_iso_pi_opens F U R).hom ≫ right_res F (covering_of_presieve U R) := begin ext ⟨f, g⟩, dunfold right_res presheaf.second_map, rw [category.assoc, category.assoc, second_obj_iso_pi_inters_π, limit.lift_π, fan.mk_π_app, limit.lift_π_assoc, fan.mk_π_app, ← category.assoc, first_obj_iso_pi_opens_π, category.assoc, ← F.map_comp], refl, end /-- The natural isomorphism between the sites diagram and the spaces diagram. -/ @[simps] def diagram_nat_iso : parallel_pair (presheaf.first_map R F) (presheaf.second_map R F) ≅ diagram F (covering_of_presieve U R) := nat_iso.of_components (λ i, walking_parallel_pair.cases_on i (first_obj_iso_pi_opens F U R) (second_obj_iso_pi_inters F U R)) $ begin intros i j f, cases i, { cases j, { cases f, simp }, { cases f, { exact first_obj_iso_comp_left_res_eq F U R, }, { exact first_obj_iso_comp_right_res_eq F U R, } } }, { cases j, { cases f, }, { cases f, simp } }, end /-- Postcomposing the given fork of the _sites_ diagram with the natural isomorphism between the diagrams gives us a fork of the _spaces_ diagram. We construct a morphism from this fork to the given fork of the _spaces_ diagram. This is shown to be an isomorphism below. -/ @[simps] def postcompose_diagram_fork_hom (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : (cones.postcompose (diagram_nat_iso F U R).hom).obj (fork.of_ι _ (presheaf.w R F)) ⟶ fork F (covering_of_presieve U R) := fork.mk_hom (F.map (eq_to_hom (supr_eq_of_mem_grothendieck U R hR)).op) (fork_map_comp_first_obj_iso_pi_opens_eq F U R hR).symm instance is_iso_postcompose_diagram_fork_hom_hom (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : is_iso (postcompose_diagram_fork_hom F U R hR).hom := begin rw [postcompose_diagram_fork_hom_hom, eq_to_hom_map], apply eq_to_hom.is_iso, end instance is_iso_postcompose_diagram_fork_hom (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : is_iso (postcompose_diagram_fork_hom F U R hR) := cones.cone_iso_of_hom_iso _ /-- See `postcompose_diagram_fork_hom`. -/ def postcompose_diagram_fork_iso (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : (cones.postcompose (diagram_nat_iso F U R).hom).obj (fork.of_ι _ (presheaf.w R F)) ≅ fork F (covering_of_presieve U R) := as_iso (postcompose_diagram_fork_hom F U R hR) end covering_of_presieve lemma is_sheaf_of_is_sheaf_equalizer_products (Fsh : F.is_sheaf_equalizer_products) : F.is_sheaf := begin delta is_sheaf, rw presheaf.is_sheaf_iff_is_sheaf', intros U R hR, refine ⟨_⟩, apply (is_limit.of_cone_equiv (cones.postcompose_equivalence (covering_of_presieve.diagram_nat_iso F U R : _))).to_fun, apply (is_limit.equiv_iso_limit (covering_of_presieve.postcompose_diagram_fork_iso F U R hR)).inv_fun, exact (Fsh (covering_of_presieve U R)).some, end /-- Given a family of opens `U : ι → opens X` and any open `Y : opens X`, we obtain a presieve on `Y` by declaring that a morphism `f : V ⟶ Y` is a member of the presieve if and only if there exists an index `i : ι` such that `V = U i`. -/ def presieve_of_covering_aux {ι : Type v} (U : ι → opens X) (Y : opens X) : presieve Y := λ V f, ∃ i, V = U i /-- Take `Y` to be `supr U` and obtain a presieve over `supr U`. -/ def presieve_of_covering {ι : Type v} (U : ι → opens X) : presieve (supr U) := presieve_of_covering_aux U (supr U) /-- Given a presieve `R` on `Y`, if we take its associated family of opens via `covering_of_presieve` (which may not cover `Y` if `R` is not covering), and take the presieve on `Y` associated to the family of opens via `presieve_of_covering_aux`, then we get back the original presieve `R`. -/ @[simp] lemma covering_presieve_eq_self {Y : opens X} (R : presieve Y) : presieve_of_covering_aux (covering_of_presieve Y R) Y = R := by { ext Z f, exact ⟨λ ⟨⟨_,_,h⟩,rfl⟩, by convert h, λ h, ⟨⟨Z,f,h⟩,rfl⟩⟩ } namespace presieve_of_covering /-! In this section, we will relate two different fork diagrams to each other. The first one is the defining fork diagram for the sheaf condition in terms of spaces, applied to the family of opens `U`. It will henceforth be called the _spaces diagram_. Its objects are called `pi_opens` and `pi_inters` and its morphisms are `left_res` and `right_res`. The fork map into this diagram is called `res`. The second one is the defining fork diagram for the sheaf condition in terms of sites, applied to the presieve `presieve_of_covering U`. It will henceforth be called the _sites diagram_. Its objects are called `presheaf.first_obj` and `presheaf.second_obj` and its morphisms are `presheaf.first_map` and `presheaf.second_obj`. The fork map into this diagram is called `presheaf.fork_map`. -/ variables {ι : Type v} (U : ι → opens X) /-- The sieve generated by `presieve_of_covering U` is a member of the grothendieck topology. -/ lemma mem_grothendieck_topology : sieve.generate (presieve_of_covering U) ∈ opens.grothendieck_topology X (supr U) := begin intros x hx, obtain ⟨i, hxi⟩ := opens.mem_supr.mp hx, exact ⟨U i, opens.le_supr U i, ⟨U i, 𝟙 _, opens.le_supr U i, ⟨i, rfl⟩, category.id_comp _⟩, hxi⟩, end /-- An index `i : ι` can be turned into a dependent pair `(V, f)`, where `V` is an open set and `f : V ⟶ supr U` is a member of `presieve_of_covering U f`. -/ def hom_of_index (i : ι) : Σ V, {f : V ⟶ supr U // presieve_of_covering U f} := ⟨U i, opens.le_supr U i, i, rfl⟩ /-- By using the axiom of choice, a dependent pair `(V, f)` where `f : V ⟶ supr U` is a member of `presieve_of_covering U f` can be turned into an index `i : ι`, such that `V = U i`. -/ def index_of_hom (f : Σ V, {f : V ⟶ supr U // presieve_of_covering U f}) : ι := f.2.2.some lemma index_of_hom_spec (f : Σ V, {f : V ⟶ supr U // presieve_of_covering U f}) : f.1 = U (index_of_hom U f) := f.2.2.some_spec /-- The canonical morphism from the first object in the sites diagram to the first object in the spaces diagram. Note that this is *not* an isomorphism, as the product `pi_opens F U` may contain duplicate factors, i.e. `U : ι → opens X` may not be injective. -/ def first_obj_to_pi_opens : presheaf.first_obj (presieve_of_covering U) F ⟶ pi_opens F U := pi.lift (λ i, pi.π _ (hom_of_index U i)) /-- The canonical morphism from the first object in the spaces diagram to the first object in the sites diagram. Note that this is *not* an isomorphism, as the product `pi_opens F U` may contain duplicate factors, i.e. `U : ι → opens X` may not be injective. -/ def pi_opens_to_first_obj : pi_opens F U ⟶ presheaf.first_obj.{v v u} (presieve_of_covering U) F := pi.lift (λ f, pi.π _ (index_of_hom U f) ≫ F.map (eq_to_hom (index_of_hom_spec U f)).op) /-- Even though `first_obj_to_pi_opens` and `pi_opens_to_first_obj` are not inverse to each other, applying them both after a fork map `s.ι` does nothing. The intuition here is that a compatible family `s : Π i : ι, F.obj (op (U i))` does not care about duplicate open sets: If `U i = U j` the compatible family coincides on the intersection `U i ⊓ U j = U i = U j`, hence `s i = s j` (module an `eq_to_hom` arrow). -/ lemma fork_ι_comp_pi_opens_to_first_obj_to_pi_opens_eq (s : limits.fork (left_res F U) (right_res F U)) : s.ι ≫ pi_opens_to_first_obj F U ≫ first_obj_to_pi_opens F U = s.ι := begin ext ⟨j⟩, dunfold first_obj_to_pi_opens pi_opens_to_first_obj, rw [category.assoc, category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app], -- The issue here is that `index_of_hom U (hom_of_index U j)` need not be equal to `j`. -- But `U j = U (index_of_hom U (hom_of_index U j))` and hence we obtain the following -- `eq_to_hom` arrow: have i_eq : U j ⟶ U j ⊓ U (index_of_hom U (hom_of_index U j)), { apply eq_to_hom, rw ← index_of_hom_spec U, exact inf_idem.symm, }, -- Since `s` is a fork, we know that `s.ι ≫ left_res F U = s.ι ≫ right_res F U`. -- We compose both sides of this equality with the canonical projection at the index pair -- `(j, index_of_hom U (hom_of_index U j)` and the restriction along `i_eq`. have := congr_arg (λ f, f ≫ pi.π (λ p : ι × ι, F.obj (op (U p.1 ⊓ U p.2))) (j, index_of_hom U (hom_of_index U j)) ≫ F.map i_eq.op) s.condition, dsimp at this, rw [category.assoc, category.assoc] at this, symmetry, -- We claim that this is equality is our goal convert this using 2, { dunfold left_res, rw [limit.lift_π_assoc, fan.mk_π_app, category.assoc, ← F.map_comp], erw F.map_id, rw category.comp_id }, { dunfold right_res, rw [limit.lift_π_assoc, fan.mk_π_app, category.assoc, ← F.map_comp], congr, } end /-- The canonical morphism from the second object of the spaces diagram to the second object of the sites diagram. -/ def pi_inters_to_second_obj : pi_inters F U ⟶ presheaf.second_obj.{v v u} (presieve_of_covering U) F := pi.lift (λ f, pi.π _ (index_of_hom U f.fst, index_of_hom U f.snd) ≫ F.map (eq_to_hom (by rw [complete_lattice.pullback_eq_inf, ← index_of_hom_spec U, ← index_of_hom_spec U])).op) lemma pi_opens_to_first_obj_comp_fist_map_eq : pi_opens_to_first_obj F U ≫ presheaf.first_map (presieve_of_covering U) F = left_res F U ≫ pi_inters_to_second_obj F U := begin ext ⟨f, g⟩, dunfold pi_opens_to_first_obj presheaf.first_map left_res pi_inters_to_second_obj, rw [category.assoc, category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, ← category.assoc, ← category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, category.assoc, category.assoc, ← F.map_comp, ← F.map_comp], refl, end lemma pi_opens_to_first_obj_comp_second_map_eq : pi_opens_to_first_obj F U ≫ presheaf.second_map (presieve_of_covering U) F = right_res F U ≫ pi_inters_to_second_obj F U := begin ext ⟨f, g⟩, dunfold pi_opens_to_first_obj presheaf.second_map right_res pi_inters_to_second_obj, rw [category.assoc, category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, ← category.assoc, ← category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, category.assoc, category.assoc, ← F.map_comp, ← F.map_comp], refl, end lemma fork_map_comp_first_map_to_pi_opens_eq : presheaf.fork_map (presieve_of_covering U) F ≫ first_obj_to_pi_opens F U = res F U := begin ext i, dsimp [presheaf.fork_map, first_obj_to_pi_opens, res], rw [category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app], refl, end lemma res_comp_pi_opens_to_first_obj_eq : res F U ≫ pi_opens_to_first_obj F U = presheaf.fork_map (presieve_of_covering U) F := begin ext f, dunfold res pi_opens_to_first_obj presheaf.fork_map, rw [category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, ← category.assoc, limit.lift_π, fan.mk_π_app, ← F.map_comp], congr, end end presieve_of_covering open presieve_of_covering lemma is_sheaf_equalizer_products_of_is_sheaf (Fsh : F.is_sheaf) : F.is_sheaf_equalizer_products := begin intros ι U, delta is_sheaf at Fsh, rw presheaf.is_sheaf_iff_is_sheaf' at Fsh, -- We know that the sites diagram for `presieve_of_covering U` is a limit fork obtain ⟨h_limit⟩ := Fsh (supr U) (presieve_of_covering U) (presieve_of_covering.mem_grothendieck_topology U), refine ⟨fork.is_limit.mk' _ _⟩, -- Here, we are given an arbitrary fork of the spaces diagram and need to show that it factors -- uniquely through our limit fork. intro s, -- Composing `s.ι` with `pi_opens_to_first_obj F U` gives a fork of the sites diagram, which -- must factor through `presheaf.fork_map`. obtain ⟨l, hl⟩ := fork.is_limit.lift' h_limit (s.ι ≫ pi_opens_to_first_obj F U) _, swap, { rw [category.assoc, category.assoc, pi_opens_to_first_obj_comp_fist_map_eq, pi_opens_to_first_obj_comp_second_map_eq, ← category.assoc, ← category.assoc, s.condition] }, -- We claim that `l` also gives a factorization of `s.ι` refine ⟨l, _, _⟩, { rw [← fork_ι_comp_pi_opens_to_first_obj_to_pi_opens_eq F U s, ← category.assoc, ← hl, category.assoc, fork.ι_of_ι, fork_map_comp_first_map_to_pi_opens_eq], refl }, { intros m hm, apply fork.is_limit.hom_ext h_limit, rw [hl, fork.ι_of_ι], simp_rw ← res_comp_pi_opens_to_first_obj_eq, erw [← category.assoc, hm], }, end lemma is_sheaf_iff_is_sheaf_equalizer_products : F.is_sheaf ↔ F.is_sheaf_equalizer_products := iff.intro (is_sheaf_equalizer_products_of_is_sheaf F) (is_sheaf_of_is_sheaf_equalizer_products F) end Top.presheaf namespace Top.opens open category_theory topological_space variables {X : Top} {ι : Type*} lemma cover_dense_iff_is_basis [category ι] (B : ι ⥤ opens X) : cover_dense (opens.grothendieck_topology X) B ↔ opens.is_basis (set.range B.obj) := begin rw opens.is_basis_iff_nbhd, split, intros hd U x hx, rcases hd.1 U x hx with ⟨V,f,⟨i,f₁,f₂,hc⟩,hV⟩, exact ⟨B.obj i, ⟨i,rfl⟩, f₁.le hV, f₂.le⟩, intro hb, split, intros U x hx, rcases hb hx with ⟨_,⟨i,rfl⟩,hx,hi⟩, exact ⟨B.obj i, ⟨⟨hi⟩⟩, ⟨⟨i, 𝟙 _, ⟨⟨hi⟩⟩, rfl⟩⟩, hx⟩, end lemma cover_dense_induced_functor {B : ι → opens X} (h : opens.is_basis (set.range B)) : cover_dense (opens.grothendieck_topology X) (induced_functor B) := (cover_dense_iff_is_basis _).2 h end Top.opens namespace Top.sheaf open category_theory topological_space Top opposite variables {C : Type u} [category.{v} C] variables {X : Top.{v}} {ι : Type*} {B : ι → opens X} variables (F : X.presheaf C) (F' : sheaf C X) (h : opens.is_basis (set.range B)) /-- The empty component of a sheaf is terminal -/ def is_terminal_of_empty (F : sheaf C X) : limits.is_terminal (F.val.obj (op ∅)) := F.is_terminal_of_bot_cover ∅ (by tidy) /-- A variant of `is_terminal_of_empty` that is easier to `apply`. -/ def is_terminal_of_eq_empty (F : X.sheaf C) {U : opens X} (h : U = ∅) : limits.is_terminal (F.val.obj (op U)) := by convert F.is_terminal_of_empty /-- If a family `B` of open sets forms a basis of the topology on `X`, and if `F'` is a sheaf on `X`, then a homomorphism between a presheaf `F` on `X` and `F'` is equivalent to a homomorphism between their restrictions to the indexing type `ι` of `B`, with the induced category structure on `ι`. -/ def restrict_hom_equiv_hom : ((induced_functor B).op ⋙ F ⟶ (induced_functor B).op ⋙ F'.1) ≃ (F ⟶ F'.1) := @cover_dense.restrict_hom_equiv_hom _ _ _ _ _ _ _ _ (opens.cover_dense_induced_functor h) _ F F' @[simp] lemma extend_hom_app (α : ((induced_functor B).op ⋙ F ⟶ (induced_functor B).op ⋙ F'.1)) (i : ι) : (restrict_hom_equiv_hom F F' h α).app (op (B i)) = α.app (op i) := by { nth_rewrite 1 ← (restrict_hom_equiv_hom F F' h).left_inv α, refl } include h lemma hom_ext {α β : F ⟶ F'.1} (he : ∀ i, α.app (op (B i)) = β.app (op (B i))) : α = β := by { apply (restrict_hom_equiv_hom F F' h).symm.injective, ext i, exact he i.unop } end Top.sheaf
a99cfec8fcb050d50b33de3ab22d73094be63429
36938939954e91f23dec66a02728db08a7acfcf9
/lean/runelf.lean
9c08ea6c4a32089a9a2f36b68c092a06124a2d28
[ "Apache-2.0" ]
permissive
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
5,712
lean
/- This file ties together the evaluator, elf support, and the decoder -/ import system.io import x86_semantics.machine_memory import .elf import .translate import decodex86 open x86 def get_filename_arg : io (string × string) := do args ← io.cmdline_args, match args with | [decoder, name ] := do return ( decoder, name ) | _ := do io.fail "Usage: CMD decoder elf_file" end -- def evaluate_one (s : machine_state) : (ℕ × sum unknown_byte instruction) -> except string machine_state -- | (n, sum.inl err) := throw "Got an unknown byte" -- | (n, sum.inr inst) := eval_instruction {s with ip := s.ip + bitvec.of_nat _ n} inst def get_text_segment (e : elf.ehdr) (phdrs : list (elf.phdr e.elf_class)) : option (elf.phdr e.elf_class) := phdrs.find (λp, p.flags.has_X) namespace sysv_abi namespace x86_64 -- c.f. https://www.uclibc.org/docs/psABI-x86_64.pdf -- Basically: -- - rFLAGS are all 0 (ff) -- - (%rsp) has argc -- - 8(%rsp) has an an argc-long array of 64 bit words -- - (8 + 8 * argc)(%rsp) is 0 -- - followed by a 0 terminated array of envps -- - followed by auxiliary vectors (16 bytes each), terminated by an essentially 0 entry. -- - At some higher address the strings -- Also: -- - %rsp should be 16 byte aligned -- -- For now we just pick a reasonably rsp, initialise s.t. argc == 0 def initialise { ost : Type } (st : system_state ost) : system_state ost := let rsp_idx := 4 in -- FIXME let stack_top := bitvec.of_nat 64 (2 ^ 47) in let words := [ 0 /- argc -/, 0 /- argv term. -/, 0 /- envp term -/, 0, 0 /- auxv term (2 words) -/ ] in let f (acc : (bitvec 64 × machine_state)) (v : ℕ) : bitvec 64 × machine_state := (acc.fst + bitvec.of_nat _ 8, acc.snd.store_word acc.fst (bitvec.of_nat (8 * 8) v)) in let s' := list.foldl f (stack_top, st.machine_state) words in { st with machine_state := machine_state.update_gpreg rsp_idx (λ_, stack_top) s'.snd } end x86_64 end sysv_abi namespace linux namespace x86_64 open mc_semantics def rax_idx : fin 16 := 0 def os_state := unit def os_state.empty := () def simple_syscall (f : system_state os_state -> machine_word) : system_m os_state unit := modify (λs, { s with machine_state := s.machine_state.update_gpreg rax_idx (λ_, f s) }) def sys_getuid : system_m os_state unit := let res := bitvec.of_nat 64 4242 in simple_syscall (λ_, res) -- FIXME: maybe use the euid of the current (lean) process? We could -- also forward these to the underlying (Linux) kernel def sys_geteuid : system_m os_state unit := let res := bitvec.of_nat 64 4242 in simple_syscall (λ_, res) def sys_getgid : system_m os_state unit := let res := bitvec.of_nat 64 4242 in simple_syscall (λ_, res) -- FIXME: maybe use the euid of the current (lean) process? We could -- also forward these to the underlying (Linux) kernel def sys_getegid : system_m os_state unit := let res := bitvec.of_nat 64 4242 in simple_syscall (λ_, res) def syscalls : rbmap ℕ (system_m os_state unit) := rbmap.from_list [ (0x66, sys_geteuid) , (0x6b, sys_geteuid) , (0x68, sys_getgid) , (0x6c, sys_getegid) ] def syscall_handler : system_m os_state unit := do s <- get, let syscall_no := (s.machine_state.get_gpreg rax_idx).to_nat in match syscalls.find syscall_no with | none := throw ("Unknown syscall: " ++ repr syscall_no) | (some m) := m end end x86_64 end linux -- def lift_eval {α : Type *} | evaluator α) : io a def dump_state (s : system_state linux.x86_64.os_state) : io unit := do let line := s.machine_state.ip.pp_hex ++ ": " ++ s.machine_state.print_regs ++ " " ++ s.machine_state.print_set_flags in io.put_str_ln line def decode_loop (d : decodex86.decoder) : ℕ -> system_state linux.x86_64.os_state -> io unit | nat.zero _ := io.fail "Out of fuel" | (nat.succ n) s := do dump_state s, inst <- decodex86.decode d s.machine_state.ip.to_nat, match inst with | (sum.inl err) := io.fail ("Decode error: " ++ err) | (sum.inr (sum.inl b)) := io.fail ("Unknown byte") | (sum.inr (sum.inr i)) := match eval_instruction { s with machine_state := { s.machine_state with ip := s.machine_state.ip + bitvec.of_nat _ i.nbytes} } linux.x86_64.syscall_handler i with | (except.error e) := io.fail ("Eval failed: (" ++ repr i ++ ") " ++ e) | (except.ok s') := decode_loop n s' end end def doit : (string × string) -> io unit | (decoder, elffile) := do ((sigma.mk ehdr phdrs), init_mem) <- elf.read_info_from_file elffile, text_phdr <- match get_text_segment ehdr phdrs with | none := io.fail "No executable segment" | (some p) := return p end, d <- decodex86.mk_decoder decoder elffile text_phdr.vaddr.val text_phdr.offset.val text_phdr.filesz.val, let init_state := system_state.mk { machine_state.empty with ip := bitvec.of_nat _ ehdr.entry.val, mem := memory.from_init init_mem } linux.x86_64.os_state.empty in let init_state_abi := sysv_abi.x86_64.initialise init_state in let fuel : ℕ := 100000 in decode_loop d fuel init_state_abi def main : io unit := get_filename_arg >>= doit -- set_option profiler true -- #eval doit ("../testfiles/two", "../llvm-tablegen-support/llvm-tablegen-support", 1530, 1544) -- #eval doit ("../testfiles/mixed_mem", "../llvm-tablegen-support/llvm-tablegen-support", 1530, 1536) -- #eval doit ("../../sample-binaries/tiny/test-conditional.x86_64-exe", "../llvm-tablegen-support/llvm-tablegen-support", 4194704, 4194711)
0e6f85c201c6fae35703d76665a4cc78d0e5ecc3
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/examples/hello/lakefile.lean
6248d47afbdcabdf7de63ab445daa873f48e59e7
[ "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
111
lean
import Lake open Lake DSL package hello lean_lib Hello @[default_target] lean_exe hello { root := `Main }
a7bc587c79be2636e4685f5ad7e9cd2a20bc92c2
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/filter/at_top_bot.lean
086cf2580565ce9aa6343a948f469f4ee44d1431
[ "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
80,942
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, Jeremy Avigad, Yury Kudryashov, Patrick Massot -/ import algebra.order.field.basic import data.finset.preimage import data.set.intervals.disjoint import data.set.intervals.order_iso import order.filter.bases import algebra.order.group.min_max /-! # `at_top` and `at_bot` filters on preorded sets, monoids and groups. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define the filters * `at_top`: corresponds to `n → +∞`; * `at_bot`: corresponds to `n → -∞`. Then we prove many lemmas like “if `f → +∞`, then `f ± c → +∞`”. -/ variables {ι ι' α β γ : Type*} open set open_locale classical filter big_operators namespace filter /-- `at_top` is the filter representing the limit `→ ∞` on an ordered set. It is generated by the collection of up-sets `{b | a ≤ b}`. (The preorder need not have a top element for this to be well defined, and indeed is trivial when a top element exists.) -/ def at_top [preorder α] : filter α := ⨅ a, 𝓟 (Ici a) /-- `at_bot` is the filter representing the limit `→ -∞` on an ordered set. It is generated by the collection of down-sets `{b | b ≤ a}`. (The preorder need not have a bottom element for this to be well defined, and indeed is trivial when a bottom element exists.) -/ def at_bot [preorder α] : filter α := ⨅ a, 𝓟 (Iic a) lemma mem_at_top [preorder α] (a : α) : {b : α | a ≤ b} ∈ @at_top α _ := mem_infi_of_mem a $ subset.refl _ lemma Ici_mem_at_top [preorder α] (a : α) : Ici a ∈ (at_top : filter α) := mem_at_top a lemma Ioi_mem_at_top [preorder α] [no_max_order α] (x : α) : Ioi x ∈ (at_top : filter α) := let ⟨z, hz⟩ := exists_gt x in mem_of_superset (mem_at_top z) $ λ y h, lt_of_lt_of_le hz h lemma mem_at_bot [preorder α] (a : α) : {b : α | b ≤ a} ∈ @at_bot α _ := mem_infi_of_mem a $ subset.refl _ lemma Iic_mem_at_bot [preorder α] (a : α) : Iic a ∈ (at_bot : filter α) := mem_at_bot a lemma Iio_mem_at_bot [preorder α] [no_min_order α] (x : α) : Iio x ∈ (at_bot : filter α) := let ⟨z, hz⟩ := exists_lt x in mem_of_superset (mem_at_bot z) $ λ y h, lt_of_le_of_lt h hz lemma disjoint_at_bot_principal_Ioi [preorder α] (x : α) : disjoint at_bot (𝓟 (Ioi x)) := disjoint_of_disjoint_of_mem (Iic_disjoint_Ioi le_rfl) (Iic_mem_at_bot x) (mem_principal_self _) lemma disjoint_at_top_principal_Iio [preorder α] (x : α) : disjoint at_top (𝓟 (Iio x)) := @disjoint_at_bot_principal_Ioi αᵒᵈ _ _ lemma disjoint_at_top_principal_Iic [preorder α] [no_max_order α] (x : α) : disjoint at_top (𝓟 (Iic x)) := disjoint_of_disjoint_of_mem (Iic_disjoint_Ioi le_rfl).symm (Ioi_mem_at_top x) (mem_principal_self _) lemma disjoint_at_bot_principal_Ici [preorder α] [no_min_order α] (x : α) : disjoint at_bot (𝓟 (Ici x)) := @disjoint_at_top_principal_Iic αᵒᵈ _ _ _ lemma disjoint_pure_at_top [preorder α] [no_max_order α] (x : α) : disjoint (pure x) at_top := disjoint.symm ((disjoint_at_top_principal_Iic x).mono_right $ le_principal_iff.2 le_rfl) lemma disjoint_pure_at_bot [preorder α] [no_min_order α] (x : α) : disjoint (pure x) at_bot := @disjoint_pure_at_top αᵒᵈ _ _ _ lemma not_tendsto_const_at_top [preorder α] [no_max_order α] (x : α) (l : filter β) [l.ne_bot] : ¬tendsto (λ _, x) l at_top := tendsto_const_pure.not_tendsto (disjoint_pure_at_top x) lemma not_tendsto_const_at_bot [preorder α] [no_min_order α] (x : α) (l : filter β) [l.ne_bot] : ¬tendsto (λ _, x) l at_bot := tendsto_const_pure.not_tendsto (disjoint_pure_at_bot x) lemma disjoint_at_bot_at_top [partial_order α] [nontrivial α] : disjoint (at_bot : filter α) at_top := begin rcases exists_pair_ne α with ⟨x, y, hne⟩, by_cases hle : x ≤ y, { refine disjoint_of_disjoint_of_mem _ (Iic_mem_at_bot x) (Ici_mem_at_top y), exact Iic_disjoint_Ici.2 (hle.lt_of_ne hne).not_le }, { refine disjoint_of_disjoint_of_mem _ (Iic_mem_at_bot y) (Ici_mem_at_top x), exact Iic_disjoint_Ici.2 hle } end lemma disjoint_at_top_at_bot [partial_order α] [nontrivial α] : disjoint (at_top : filter α) at_bot := disjoint_at_bot_at_top.symm lemma at_top_basis [nonempty α] [semilattice_sup α] : (@at_top α _).has_basis (λ _, true) Ici := has_basis_infi_principal (directed_of_sup $ λ a b, Ici_subset_Ici.2) lemma at_top_basis' [semilattice_sup α] (a : α) : (@at_top α _).has_basis (λ x, a ≤ x) Ici := ⟨λ t, (@at_top_basis α ⟨a⟩ _).mem_iff.trans ⟨λ ⟨x, _, hx⟩, ⟨x ⊔ a, le_sup_right, λ y hy, hx (le_trans le_sup_left hy)⟩, λ ⟨x, _, hx⟩, ⟨x, trivial, hx⟩⟩⟩ lemma at_bot_basis [nonempty α] [semilattice_inf α] : (@at_bot α _).has_basis (λ _, true) Iic := @at_top_basis αᵒᵈ _ _ lemma at_bot_basis' [semilattice_inf α] (a : α) : (@at_bot α _).has_basis (λ x, x ≤ a) Iic := @at_top_basis' αᵒᵈ _ _ @[instance] lemma at_top_ne_bot [nonempty α] [semilattice_sup α] : ne_bot (at_top : filter α) := at_top_basis.ne_bot_iff.2 $ λ a _, nonempty_Ici @[instance] lemma at_bot_ne_bot [nonempty α] [semilattice_inf α] : ne_bot (at_bot : filter α) := @at_top_ne_bot αᵒᵈ _ _ @[simp] lemma mem_at_top_sets [nonempty α] [semilattice_sup α] {s : set α} : s ∈ (at_top : filter α) ↔ ∃a:α, ∀b≥a, b ∈ s := at_top_basis.mem_iff.trans $ exists_congr $ λ _, exists_const _ @[simp] lemma mem_at_bot_sets [nonempty α] [semilattice_inf α] {s : set α} : s ∈ (at_bot : filter α) ↔ ∃a:α, ∀b≤a, b ∈ s := @mem_at_top_sets αᵒᵈ _ _ _ @[simp] lemma eventually_at_top [semilattice_sup α] [nonempty α] {p : α → Prop} : (∀ᶠ x in at_top, p x) ↔ (∃ a, ∀ b ≥ a, p b) := mem_at_top_sets @[simp] lemma eventually_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop} : (∀ᶠ x in at_bot, p x) ↔ (∃ a, ∀ b ≤ a, p b) := mem_at_bot_sets lemma eventually_ge_at_top [preorder α] (a : α) : ∀ᶠ x in at_top, a ≤ x := mem_at_top a lemma eventually_le_at_bot [preorder α] (a : α) : ∀ᶠ x in at_bot, x ≤ a := mem_at_bot a lemma eventually_gt_at_top [preorder α] [no_max_order α] (a : α) : ∀ᶠ x in at_top, a < x := Ioi_mem_at_top a lemma eventually_ne_at_top [preorder α] [no_max_order α] (a : α) : ∀ᶠ x in at_top, x ≠ a := (eventually_gt_at_top a).mono $ λ x, ne_of_gt lemma tendsto.eventually_gt_at_top [preorder β] [no_max_order β] {f : α → β} {l : filter α} (hf : tendsto f l at_top) (c : β) : ∀ᶠ x in l, c < f x := hf.eventually (eventually_gt_at_top c) lemma tendsto.eventually_ge_at_top [preorder β] {f : α → β} {l : filter α} (hf : tendsto f l at_top) (c : β) : ∀ᶠ x in l, c ≤ f x := hf.eventually (eventually_ge_at_top c) lemma tendsto.eventually_ne_at_top [preorder β] [no_max_order β] {f : α → β} {l : filter α} (hf : tendsto f l at_top) (c : β) : ∀ᶠ x in l, f x ≠ c := hf.eventually (eventually_ne_at_top c) lemma tendsto.eventually_ne_at_top' [preorder β] [no_max_order β] {f : α → β} {l : filter α} (hf : tendsto f l at_top) (c : α) : ∀ᶠ x in l, x ≠ c := (hf.eventually_ne_at_top (f c)).mono $ λ x, ne_of_apply_ne f lemma eventually_lt_at_bot [preorder α] [no_min_order α] (a : α) : ∀ᶠ x in at_bot, x < a := Iio_mem_at_bot a lemma eventually_ne_at_bot [preorder α] [no_min_order α] (a : α) : ∀ᶠ x in at_bot, x ≠ a := (eventually_lt_at_bot a).mono $ λ x, ne_of_lt lemma tendsto.eventually_lt_at_bot [preorder β] [no_min_order β] {f : α → β} {l : filter α} (hf : tendsto f l at_bot) (c : β) : ∀ᶠ x in l, f x < c := hf.eventually (eventually_lt_at_bot c) lemma tendsto.eventually_le_at_bot [preorder β] {f : α → β} {l : filter α} (hf : tendsto f l at_bot) (c : β) : ∀ᶠ x in l, f x ≤ c := hf.eventually (eventually_le_at_bot c) lemma tendsto.eventually_ne_at_bot [preorder β] [no_min_order β] {f : α → β} {l : filter α} (hf : tendsto f l at_bot) (c : β) : ∀ᶠ x in l, f x ≠ c := hf.eventually (eventually_ne_at_bot c) lemma at_top_basis_Ioi [nonempty α] [semilattice_sup α] [no_max_order α] : (@at_top α _).has_basis (λ _, true) Ioi := at_top_basis.to_has_basis (λ a ha, ⟨a, ha, Ioi_subset_Ici_self⟩) $ λ a ha, (exists_gt a).imp $ λ b hb, ⟨ha, Ici_subset_Ioi.2 hb⟩ lemma at_top_countable_basis [nonempty α] [semilattice_sup α] [countable α] : has_countable_basis (at_top : filter α) (λ _, true) Ici := { countable := to_countable _, .. at_top_basis } lemma at_bot_countable_basis [nonempty α] [semilattice_inf α] [countable α] : has_countable_basis (at_bot : filter α) (λ _, true) Iic := { countable := to_countable _, .. at_bot_basis } @[priority 200] instance at_top.is_countably_generated [preorder α] [countable α] : (at_top : filter $ α).is_countably_generated := is_countably_generated_seq _ @[priority 200] instance at_bot.is_countably_generated [preorder α] [countable α] : (at_bot : filter $ α).is_countably_generated := is_countably_generated_seq _ lemma order_top.at_top_eq (α) [partial_order α] [order_top α] : (at_top : filter α) = pure ⊤ := le_antisymm (le_pure_iff.2 $ (eventually_ge_at_top ⊤).mono $ λ b, top_unique) (le_infi $ λ b, le_principal_iff.2 le_top) lemma order_bot.at_bot_eq (α) [partial_order α] [order_bot α] : (at_bot : filter α) = pure ⊥ := @order_top.at_top_eq αᵒᵈ _ _ @[nontriviality] lemma subsingleton.at_top_eq (α) [subsingleton α] [preorder α] : (at_top : filter α) = ⊤ := begin refine top_unique (λ s hs x, _), letI : unique α := ⟨⟨x⟩, λ y, subsingleton.elim y x⟩, rw [at_top, infi_unique, unique.default_eq x, mem_principal] at hs, exact hs left_mem_Ici end @[nontriviality] lemma subsingleton.at_bot_eq (α) [subsingleton α] [preorder α] : (at_bot : filter α) = ⊤ := @subsingleton.at_top_eq αᵒᵈ _ _ lemma tendsto_at_top_pure [partial_order α] [order_top α] (f : α → β) : tendsto f at_top (pure $ f ⊤) := (order_top.at_top_eq α).symm ▸ tendsto_pure_pure _ _ lemma tendsto_at_bot_pure [partial_order α] [order_bot α] (f : α → β) : tendsto f at_bot (pure $ f ⊥) := @tendsto_at_top_pure αᵒᵈ _ _ _ _ lemma eventually.exists_forall_of_at_top [semilattice_sup α] [nonempty α] {p : α → Prop} (h : ∀ᶠ x in at_top, p x) : ∃ a, ∀ b ≥ a, p b := eventually_at_top.mp h lemma eventually.exists_forall_of_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop} (h : ∀ᶠ x in at_bot, p x) : ∃ a, ∀ b ≤ a, p b := eventually_at_bot.mp h lemma frequently_at_top [semilattice_sup α] [nonempty α] {p : α → Prop} : (∃ᶠ x in at_top, p x) ↔ (∀ a, ∃ b ≥ a, p b) := by simp [at_top_basis.frequently_iff] lemma frequently_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop} : (∃ᶠ x in at_bot, p x) ↔ (∀ a, ∃ b ≤ a, p b) := @frequently_at_top αᵒᵈ _ _ _ lemma frequently_at_top' [semilattice_sup α] [nonempty α] [no_max_order α] {p : α → Prop} : (∃ᶠ x in at_top, p x) ↔ (∀ a, ∃ b > a, p b) := by simp [at_top_basis_Ioi.frequently_iff] lemma frequently_at_bot' [semilattice_inf α] [nonempty α] [no_min_order α] {p : α → Prop} : (∃ᶠ x in at_bot, p x) ↔ (∀ a, ∃ b < a, p b) := @frequently_at_top' αᵒᵈ _ _ _ _ lemma frequently.forall_exists_of_at_top [semilattice_sup α] [nonempty α] {p : α → Prop} (h : ∃ᶠ x in at_top, p x) : ∀ a, ∃ b ≥ a, p b := frequently_at_top.mp h lemma frequently.forall_exists_of_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop} (h : ∃ᶠ x in at_bot, p x) : ∀ a, ∃ b ≤ a, p b := frequently_at_bot.mp h lemma map_at_top_eq [nonempty α] [semilattice_sup α] {f : α → β} : at_top.map f = (⨅a, 𝓟 $ f '' {a' | a ≤ a'}) := (at_top_basis.map _).eq_infi lemma map_at_bot_eq [nonempty α] [semilattice_inf α] {f : α → β} : at_bot.map f = (⨅a, 𝓟 $ f '' {a' | a' ≤ a}) := @map_at_top_eq αᵒᵈ _ _ _ _ lemma tendsto_at_top [preorder β] {m : α → β} {f : filter α} : tendsto m f at_top ↔ (∀b, ∀ᶠ a in f, b ≤ m a) := by simp only [at_top, tendsto_infi, tendsto_principal, mem_Ici] lemma tendsto_at_bot [preorder β] {m : α → β} {f : filter α} : tendsto m f at_bot ↔ (∀b, ∀ᶠ a in f, m a ≤ b) := @tendsto_at_top α βᵒᵈ _ m f lemma tendsto_at_top_mono' [preorder β] (l : filter α) ⦃f₁ f₂ : α → β⦄ (h : f₁ ≤ᶠ[l] f₂) : tendsto f₁ l at_top → tendsto f₂ l at_top := assume h₁, tendsto_at_top.2 $ λ b, mp_mem (tendsto_at_top.1 h₁ b) (monotone_mem (λ a ha ha₁, le_trans ha₁ ha) h) lemma tendsto_at_bot_mono' [preorder β] (l : filter α) ⦃f₁ f₂ : α → β⦄ (h : f₁ ≤ᶠ[l] f₂) : tendsto f₂ l at_bot → tendsto f₁ l at_bot := @tendsto_at_top_mono' _ βᵒᵈ _ _ _ _ h lemma tendsto_at_top_mono [preorder β] {l : filter α} {f g : α → β} (h : ∀ n, f n ≤ g n) : tendsto f l at_top → tendsto g l at_top := tendsto_at_top_mono' l $ eventually_of_forall h lemma tendsto_at_bot_mono [preorder β] {l : filter α} {f g : α → β} (h : ∀ n, f n ≤ g n) : tendsto g l at_bot → tendsto f l at_bot := @tendsto_at_top_mono _ βᵒᵈ _ _ _ _ h end filter namespace order_iso open filter variables [preorder α] [preorder β] @[simp] lemma comap_at_top (e : α ≃o β) : comap e at_top = at_top := by simp [at_top, ← e.surjective.infi_comp] @[simp] lemma comap_at_bot (e : α ≃o β) : comap e at_bot = at_bot := e.dual.comap_at_top @[simp] lemma map_at_top (e : α ≃o β) : map (e : α → β) at_top = at_top := by rw [← e.comap_at_top, map_comap_of_surjective e.surjective] @[simp] lemma map_at_bot (e : α ≃o β) : map (e : α → β) at_bot = at_bot := e.dual.map_at_top lemma tendsto_at_top (e : α ≃o β) : tendsto e at_top at_top := e.map_at_top.le lemma tendsto_at_bot (e : α ≃o β) : tendsto e at_bot at_bot := e.map_at_bot.le @[simp] lemma tendsto_at_top_iff {l : filter γ} {f : γ → α} (e : α ≃o β) : tendsto (λ x, e (f x)) l at_top ↔ tendsto f l at_top := by rw [← e.comap_at_top, tendsto_comap_iff] @[simp] lemma tendsto_at_bot_iff {l : filter γ} {f : γ → α} (e : α ≃o β) : tendsto (λ x, e (f x)) l at_bot ↔ tendsto f l at_bot := e.dual.tendsto_at_top_iff end order_iso namespace filter /-! ### Sequences -/ lemma inf_map_at_top_ne_bot_iff [semilattice_sup α] [nonempty α] {F : filter β} {u : α → β} : ne_bot (F ⊓ (map u at_top)) ↔ ∀ U ∈ F, ∀ N, ∃ n ≥ N, u n ∈ U := by simp_rw [inf_ne_bot_iff_frequently_left, frequently_map, frequently_at_top]; refl lemma inf_map_at_bot_ne_bot_iff [semilattice_inf α] [nonempty α] {F : filter β} {u : α → β} : ne_bot (F ⊓ (map u at_bot)) ↔ ∀ U ∈ F, ∀ N, ∃ n ≤ N, u n ∈ U := @inf_map_at_top_ne_bot_iff αᵒᵈ _ _ _ _ _ lemma extraction_of_frequently_at_top' {P : ℕ → Prop} (h : ∀ N, ∃ n > N, P n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) := begin choose u hu using h, cases forall_and_distrib.mp hu with hu hu', exact ⟨u ∘ (nat.rec 0 (λ n v, u v)), strict_mono_nat_of_lt_succ (λ n, hu _), λ n, hu' _⟩, end lemma extraction_of_frequently_at_top {P : ℕ → Prop} (h : ∃ᶠ n in at_top, P n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) := begin rw frequently_at_top' at h, exact extraction_of_frequently_at_top' h, end lemma extraction_of_eventually_at_top {P : ℕ → Prop} (h : ∀ᶠ n in at_top, P n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) := extraction_of_frequently_at_top h.frequently lemma extraction_forall_of_frequently {P : ℕ → ℕ → Prop} (h : ∀ n, ∃ᶠ k in at_top, P n k) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P n (φ n) := begin simp only [frequently_at_top'] at h, choose u hu hu' using h, use (λ n, nat.rec_on n (u 0 0) (λ n v, u (n+1) v) : ℕ → ℕ), split, { apply strict_mono_nat_of_lt_succ, intro n, apply hu }, { intros n, cases n ; simp [hu'] }, end lemma extraction_forall_of_eventually {P : ℕ → ℕ → Prop} (h : ∀ n, ∀ᶠ k in at_top, P n k) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P n (φ n) := extraction_forall_of_frequently (λ n, (h n).frequently) lemma extraction_forall_of_eventually' {P : ℕ → ℕ → Prop} (h : ∀ n, ∃ N, ∀ k ≥ N, P n k) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P n (φ n) := extraction_forall_of_eventually (by simp [eventually_at_top, h]) lemma exists_le_of_tendsto_at_top [semilattice_sup α] [preorder β] {u : α → β} (h : tendsto u at_top at_top) (a : α) (b : β) : ∃ a' ≥ a, b ≤ u a' := begin have : ∀ᶠ x in at_top, a ≤ x ∧ b ≤ u x := (eventually_ge_at_top a).and (h.eventually $ eventually_ge_at_top b), haveI : nonempty α := ⟨a⟩, rcases this.exists with ⟨a', ha, hb⟩, exact ⟨a', ha, hb⟩ end @[nolint ge_or_gt] -- see Note [nolint_ge] lemma exists_le_of_tendsto_at_bot [semilattice_sup α] [preorder β] {u : α → β} (h : tendsto u at_top at_bot) : ∀ a b, ∃ a' ≥ a, u a' ≤ b := @exists_le_of_tendsto_at_top _ βᵒᵈ _ _ _ h lemma exists_lt_of_tendsto_at_top [semilattice_sup α] [preorder β] [no_max_order β] {u : α → β} (h : tendsto u at_top at_top) (a : α) (b : β) : ∃ a' ≥ a, b < u a' := begin cases exists_gt b with b' hb', rcases exists_le_of_tendsto_at_top h a b' with ⟨a', ha', ha''⟩, exact ⟨a', ha', lt_of_lt_of_le hb' ha''⟩ end @[nolint ge_or_gt] -- see Note [nolint_ge] lemma exists_lt_of_tendsto_at_bot [semilattice_sup α] [preorder β] [no_min_order β] {u : α → β} (h : tendsto u at_top at_bot) : ∀ a b, ∃ a' ≥ a, u a' < b := @exists_lt_of_tendsto_at_top _ βᵒᵈ _ _ _ _ h /-- If `u` is a sequence which is unbounded above, then after any point, it reaches a value strictly greater than all previous values. -/ lemma high_scores [linear_order β] [no_max_order β] {u : ℕ → β} (hu : tendsto u at_top at_top) : ∀ N, ∃ n ≥ N, ∀ k < n, u k < u n := begin intros N, obtain ⟨k : ℕ, hkn : k ≤ N, hku : ∀ l ≤ N, u l ≤ u k⟩ : ∃ k ≤ N, ∀ l ≤ N, u l ≤ u k, from exists_max_image _ u (finite_le_nat N) ⟨N, le_refl N⟩, have ex : ∃ n ≥ N, u k < u n, from exists_lt_of_tendsto_at_top hu _ _, obtain ⟨n : ℕ, hnN : n ≥ N, hnk : u k < u n, hn_min : ∀ m, m < n → N ≤ m → u m ≤ u k⟩ : ∃ n ≥ N, u k < u n ∧ ∀ m, m < n → N ≤ m → u m ≤ u k, { rcases nat.find_x ex with ⟨n, ⟨hnN, hnk⟩, hn_min⟩, push_neg at hn_min, exact ⟨n, hnN, hnk, hn_min⟩ }, use [n, hnN], rintros (l : ℕ) (hl : l < n), have hlk : u l ≤ u k, { cases (le_total l N : l ≤ N ∨ N ≤ l) with H H, { exact hku l H }, { exact hn_min l hl H } }, calc u l ≤ u k : hlk ... < u n : hnk end /-- If `u` is a sequence which is unbounded below, then after any point, it reaches a value strictly smaller than all previous values. -/ @[nolint ge_or_gt] -- see Note [nolint_ge] lemma low_scores [linear_order β] [no_min_order β] {u : ℕ → β} (hu : tendsto u at_top at_bot) : ∀ N, ∃ n ≥ N, ∀ k < n, u n < u k := @high_scores βᵒᵈ _ _ _ hu /-- If `u` is a sequence which is unbounded above, then it `frequently` reaches a value strictly greater than all previous values. -/ lemma frequently_high_scores [linear_order β] [no_max_order β] {u : ℕ → β} (hu : tendsto u at_top at_top) : ∃ᶠ n in at_top, ∀ k < n, u k < u n := by simpa [frequently_at_top] using high_scores hu /-- If `u` is a sequence which is unbounded below, then it `frequently` reaches a value strictly smaller than all previous values. -/ lemma frequently_low_scores [linear_order β] [no_min_order β] {u : ℕ → β} (hu : tendsto u at_top at_bot) : ∃ᶠ n in at_top, ∀ k < n, u n < u k := @frequently_high_scores βᵒᵈ _ _ _ hu lemma strict_mono_subseq_of_tendsto_at_top {β : Type*} [linear_order β] [no_max_order β] {u : ℕ → β} (hu : tendsto u at_top at_top) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ strict_mono (u ∘ φ) := let ⟨φ, h, h'⟩ := extraction_of_frequently_at_top (frequently_high_scores hu) in ⟨φ, h, λ n m hnm, h' m _ (h hnm)⟩ lemma strict_mono_subseq_of_id_le {u : ℕ → ℕ} (hu : ∀ n, n ≤ u n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ strict_mono (u ∘ φ) := strict_mono_subseq_of_tendsto_at_top (tendsto_at_top_mono hu tendsto_id) lemma _root_.strict_mono.tendsto_at_top {φ : ℕ → ℕ} (h : strict_mono φ) : tendsto φ at_top at_top := tendsto_at_top_mono h.id_le tendsto_id section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] {l : filter α} {f g : α → β} lemma tendsto_at_top_add_nonneg_left' (hf : ∀ᶠ x in l, 0 ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_mono' l (hf.mono (λ x, le_add_of_nonneg_left)) hg lemma tendsto_at_bot_add_nonpos_left' (hf : ∀ᶠ x in l, f x ≤ 0) (hg : tendsto g l at_bot) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_nonneg_left' _ βᵒᵈ _ _ _ _ hf hg lemma tendsto_at_top_add_nonneg_left (hf : ∀ x, 0 ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_nonneg_left' (eventually_of_forall hf) hg lemma tendsto_at_bot_add_nonpos_left (hf : ∀ x, f x ≤ 0) (hg : tendsto g l at_bot) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_nonneg_left _ βᵒᵈ _ _ _ _ hf hg lemma tendsto_at_top_add_nonneg_right' (hf : tendsto f l at_top) (hg : ∀ᶠ x in l, 0 ≤ g x) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_mono' l (monotone_mem (λ x, le_add_of_nonneg_right) hg) hf lemma tendsto_at_bot_add_nonpos_right' (hf : tendsto f l at_bot) (hg : ∀ᶠ x in l, g x ≤ 0) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_nonneg_right' _ βᵒᵈ _ _ _ _ hf hg lemma tendsto_at_top_add_nonneg_right (hf : tendsto f l at_top) (hg : ∀ x, 0 ≤ g x) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_nonneg_right' hf (eventually_of_forall hg) lemma tendsto_at_bot_add_nonpos_right (hf : tendsto f l at_bot) (hg : ∀ x, g x ≤ 0) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_nonneg_right _ βᵒᵈ _ _ _ _ hf hg lemma tendsto_at_top_add (hf : tendsto f l at_top) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_nonneg_left' (tendsto_at_top.mp hf 0) hg lemma tendsto_at_bot_add (hf : tendsto f l at_bot) (hg : tendsto g l at_bot) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add _ βᵒᵈ _ _ _ _ hf hg lemma tendsto.nsmul_at_top (hf : tendsto f l at_top) {n : ℕ} (hn : 0 < n) : tendsto (λ x, n • f x) l at_top := tendsto_at_top.2 $ λ y, (tendsto_at_top.1 hf y).mp $ (tendsto_at_top.1 hf 0).mono $ λ x h₀ hy, calc y ≤ f x : hy ... = 1 • f x : (one_nsmul _).symm ... ≤ n • f x : nsmul_le_nsmul h₀ hn lemma tendsto.nsmul_at_bot (hf : tendsto f l at_bot) {n : ℕ} (hn : 0 < n) : tendsto (λ x, n • f x) l at_bot := @tendsto.nsmul_at_top α βᵒᵈ _ l f hf n hn lemma tendsto_bit0_at_top : tendsto bit0 (at_top : filter β) at_top := tendsto_at_top_add tendsto_id tendsto_id lemma tendsto_bit0_at_bot : tendsto bit0 (at_bot : filter β) at_bot := tendsto_at_bot_add tendsto_id tendsto_id end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid β] {l : filter α} {f g : α → β} lemma tendsto_at_top_of_add_const_left (C : β) (hf : tendsto (λ x, C + f x) l at_top) : tendsto f l at_top := tendsto_at_top.2 $ assume b, (tendsto_at_top.1 hf (C + b)).mono (λ x, le_of_add_le_add_left) lemma tendsto_at_bot_of_add_const_left (C : β) (hf : tendsto (λ x, C + f x) l at_bot) : tendsto f l at_bot := @tendsto_at_top_of_add_const_left _ βᵒᵈ _ _ _ C hf lemma tendsto_at_top_of_add_const_right (C : β) (hf : tendsto (λ x, f x + C) l at_top) : tendsto f l at_top := tendsto_at_top.2 $ assume b, (tendsto_at_top.1 hf (b + C)).mono (λ x, le_of_add_le_add_right) lemma tendsto_at_bot_of_add_const_right (C : β) (hf : tendsto (λ x, f x + C) l at_bot) : tendsto f l at_bot := @tendsto_at_top_of_add_const_right _ βᵒᵈ _ _ _ C hf lemma tendsto_at_top_of_add_bdd_above_left' (C) (hC : ∀ᶠ x in l, f x ≤ C) (h : tendsto (λ x, f x + g x) l at_top) : tendsto g l at_top := tendsto_at_top_of_add_const_left C (tendsto_at_top_mono' l (hC.mono (λ x hx, add_le_add_right hx (g x))) h) lemma tendsto_at_bot_of_add_bdd_below_left' (C) (hC : ∀ᶠ x in l, C ≤ f x) (h : tendsto (λ x, f x + g x) l at_bot) : tendsto g l at_bot := @tendsto_at_top_of_add_bdd_above_left' _ βᵒᵈ _ _ _ _ C hC h lemma tendsto_at_top_of_add_bdd_above_left (C) (hC : ∀ x, f x ≤ C) : tendsto (λ x, f x + g x) l at_top → tendsto g l at_top := tendsto_at_top_of_add_bdd_above_left' C (univ_mem' hC) lemma tendsto_at_bot_of_add_bdd_below_left (C) (hC : ∀ x, C ≤ f x) : tendsto (λ x, f x + g x) l at_bot → tendsto g l at_bot := @tendsto_at_top_of_add_bdd_above_left _ βᵒᵈ _ _ _ _ C hC lemma tendsto_at_top_of_add_bdd_above_right' (C) (hC : ∀ᶠ x in l, g x ≤ C) (h : tendsto (λ x, f x + g x) l at_top) : tendsto f l at_top := tendsto_at_top_of_add_const_right C (tendsto_at_top_mono' l (hC.mono (λ x hx, add_le_add_left hx (f x))) h) lemma tendsto_at_bot_of_add_bdd_below_right' (C) (hC : ∀ᶠ x in l, C ≤ g x) (h : tendsto (λ x, f x + g x) l at_bot) : tendsto f l at_bot := @tendsto_at_top_of_add_bdd_above_right' _ βᵒᵈ _ _ _ _ C hC h lemma tendsto_at_top_of_add_bdd_above_right (C) (hC : ∀ x, g x ≤ C) : tendsto (λ x, f x + g x) l at_top → tendsto f l at_top := tendsto_at_top_of_add_bdd_above_right' C (univ_mem' hC) lemma tendsto_at_bot_of_add_bdd_below_right (C) (hC : ∀ x, C ≤ g x) : tendsto (λ x, f x + g x) l at_bot → tendsto f l at_bot := @tendsto_at_top_of_add_bdd_above_right _ βᵒᵈ _ _ _ _ C hC end ordered_cancel_add_comm_monoid section ordered_group variables [ordered_add_comm_group β] (l : filter α) {f g : α → β} lemma tendsto_at_top_add_left_of_le' (C : β) (hf : ∀ᶠ x in l, C ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := @tendsto_at_top_of_add_bdd_above_left' _ _ _ l (λ x, -(f x)) (λ x, f x + g x) (-C) (by simpa) (by simpa) lemma tendsto_at_bot_add_left_of_ge' (C : β) (hf : ∀ᶠ x in l, f x ≤ C) (hg : tendsto g l at_bot) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_left_of_le' _ βᵒᵈ _ _ _ _ C hf hg lemma tendsto_at_top_add_left_of_le (C : β) (hf : ∀ x, C ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_left_of_le' l C (univ_mem' hf) hg lemma tendsto_at_bot_add_left_of_ge (C : β) (hf : ∀ x, f x ≤ C) (hg : tendsto g l at_bot) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_left_of_le _ βᵒᵈ _ _ _ _ C hf hg lemma tendsto_at_top_add_right_of_le' (C : β) (hf : tendsto f l at_top) (hg : ∀ᶠ x in l, C ≤ g x) : tendsto (λ x, f x + g x) l at_top := @tendsto_at_top_of_add_bdd_above_right' _ _ _ l (λ x, f x + g x) (λ x, -(g x)) (-C) (by simp [hg]) (by simp [hf]) lemma tendsto_at_bot_add_right_of_ge' (C : β) (hf : tendsto f l at_bot) (hg : ∀ᶠ x in l, g x ≤ C) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_right_of_le' _ βᵒᵈ _ _ _ _ C hf hg lemma tendsto_at_top_add_right_of_le (C : β) (hf : tendsto f l at_top) (hg : ∀ x, C ≤ g x) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_right_of_le' l C hf (univ_mem' hg) lemma tendsto_at_bot_add_right_of_ge (C : β) (hf : tendsto f l at_bot) (hg : ∀ x, g x ≤ C) : tendsto (λ x, f x + g x) l at_bot := @tendsto_at_top_add_right_of_le _ βᵒᵈ _ _ _ _ C hf hg lemma tendsto_at_top_add_const_left (C : β) (hf : tendsto f l at_top) : tendsto (λ x, C + f x) l at_top := tendsto_at_top_add_left_of_le' l C (univ_mem' $ λ _, le_refl C) hf lemma tendsto_at_bot_add_const_left (C : β) (hf : tendsto f l at_bot) : tendsto (λ x, C + f x) l at_bot := @tendsto_at_top_add_const_left _ βᵒᵈ _ _ _ C hf lemma tendsto_at_top_add_const_right (C : β) (hf : tendsto f l at_top) : tendsto (λ x, f x + C) l at_top := tendsto_at_top_add_right_of_le' l C hf (univ_mem' $ λ _, le_refl C) lemma tendsto_at_bot_add_const_right (C : β) (hf : tendsto f l at_bot) : tendsto (λ x, f x + C) l at_bot := @tendsto_at_top_add_const_right _ βᵒᵈ _ _ _ C hf lemma map_neg_at_bot : map (has_neg.neg : β → β) at_bot = at_top := (order_iso.neg β).map_at_bot lemma map_neg_at_top : map (has_neg.neg : β → β) at_top = at_bot := (order_iso.neg β).map_at_top @[simp] lemma comap_neg_at_bot : comap (has_neg.neg : β → β) at_bot = at_top := (order_iso.neg β).comap_at_top @[simp] lemma comap_neg_at_top : comap (has_neg.neg : β → β) at_top = at_bot := (order_iso.neg β).comap_at_bot lemma tendsto_neg_at_top_at_bot : tendsto (has_neg.neg : β → β) at_top at_bot := (order_iso.neg β).tendsto_at_top lemma tendsto_neg_at_bot_at_top : tendsto (has_neg.neg : β → β) at_bot at_top := @tendsto_neg_at_top_at_bot βᵒᵈ _ variable {l} @[simp] lemma tendsto_neg_at_top_iff : tendsto (λ x, -f x) l at_top ↔ tendsto f l at_bot := (order_iso.neg β).tendsto_at_bot_iff @[simp] lemma tendsto_neg_at_bot_iff : tendsto (λ x, -f x) l at_bot ↔ tendsto f l at_top := (order_iso.neg β).tendsto_at_top_iff end ordered_group section strict_ordered_semiring variables [strict_ordered_semiring α] {l : filter β} {f g : β → α} lemma tendsto_bit1_at_top : tendsto bit1 (at_top : filter α) at_top := tendsto_at_top_add_nonneg_right tendsto_bit0_at_top (λ _, zero_le_one) lemma tendsto.at_top_mul_at_top (hf : tendsto f l at_top) (hg : tendsto g l at_top) : tendsto (λ x, f x * g x) l at_top := begin refine tendsto_at_top_mono' _ _ hg, filter_upwards [hg.eventually (eventually_ge_at_top 0), hf.eventually (eventually_ge_at_top 1)] with _ using le_mul_of_one_le_left, end lemma tendsto_mul_self_at_top : tendsto (λ x : α, x * x) at_top at_top := tendsto_id.at_top_mul_at_top tendsto_id /-- The monomial function `x^n` tends to `+∞` at `+∞` for any positive natural `n`. A version for positive real powers exists as `tendsto_rpow_at_top`. -/ lemma tendsto_pow_at_top {n : ℕ} (hn : n ≠ 0) : tendsto (λ x : α, x ^ n) at_top at_top := tendsto_at_top_mono' _ ((eventually_ge_at_top 1).mono $ λ x hx, le_self_pow hx hn) tendsto_id end strict_ordered_semiring lemma zero_pow_eventually_eq [monoid_with_zero α] : (λ n : ℕ, (0 : α) ^ n) =ᶠ[at_top] (λ n, 0) := eventually_at_top.2 ⟨1, λ n hn, zero_pow (zero_lt_one.trans_le hn)⟩ section strict_ordered_ring variables [strict_ordered_ring α] {l : filter β} {f g : β → α} lemma tendsto.at_top_mul_at_bot (hf : tendsto f l at_top) (hg : tendsto g l at_bot) : tendsto (λ x, f x * g x) l at_bot := have _ := (hf.at_top_mul_at_top $ tendsto_neg_at_bot_at_top.comp hg), by simpa only [(∘), neg_mul_eq_mul_neg, neg_neg] using tendsto_neg_at_top_at_bot.comp this lemma tendsto.at_bot_mul_at_top (hf : tendsto f l at_bot) (hg : tendsto g l at_top) : tendsto (λ x, f x * g x) l at_bot := have tendsto (λ x, (-f x) * g x) l at_top := ( (tendsto_neg_at_bot_at_top.comp hf).at_top_mul_at_top hg), by simpa only [(∘), neg_mul_eq_neg_mul, neg_neg] using tendsto_neg_at_top_at_bot.comp this lemma tendsto.at_bot_mul_at_bot (hf : tendsto f l at_bot) (hg : tendsto g l at_bot) : tendsto (λ x, f x * g x) l at_top := have tendsto (λ x, (-f x) * (-g x)) l at_top := (tendsto_neg_at_bot_at_top.comp hf).at_top_mul_at_top (tendsto_neg_at_bot_at_top.comp hg), by simpa only [neg_mul_neg] using this end strict_ordered_ring section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group α] /-- $\lim_{x\to+\infty}|x|=+\infty$ -/ lemma tendsto_abs_at_top_at_top : tendsto (abs : α → α) at_top at_top := tendsto_at_top_mono le_abs_self tendsto_id /-- $\lim_{x\to-\infty}|x|=+\infty$ -/ lemma tendsto_abs_at_bot_at_top : tendsto (abs : α → α) at_bot at_top := tendsto_at_top_mono neg_le_abs_self tendsto_neg_at_bot_at_top @[simp] lemma comap_abs_at_top : comap (abs : α → α) at_top = at_bot ⊔ at_top := begin refine le_antisymm (((at_top_basis.comap _).le_basis_iff (at_bot_basis.sup at_top_basis)).2 _) (sup_le tendsto_abs_at_bot_at_top.le_comap tendsto_abs_at_top_at_top.le_comap), rintro ⟨a, b⟩ -, refine ⟨max (-a) b, trivial, λ x hx, _⟩, rw [mem_preimage, mem_Ici, le_abs', max_le_iff, ← min_neg_neg, le_min_iff, neg_neg] at hx, exact hx.imp and.left and.right end end linear_ordered_add_comm_group section linear_ordered_semiring variables [linear_ordered_semiring α] {l : filter β} {f : β → α} lemma tendsto.at_top_of_const_mul {c : α} (hc : 0 < c) (hf : tendsto (λ x, c * f x) l at_top) : tendsto f l at_top := tendsto_at_top.2 $ λ b, (tendsto_at_top.1 hf (c * b)).mono $ λ x hx, le_of_mul_le_mul_left hx hc lemma tendsto.at_top_of_mul_const {c : α} (hc : 0 < c) (hf : tendsto (λ x, f x * c) l at_top) : tendsto f l at_top := tendsto_at_top.2 $ λ b, (tendsto_at_top.1 hf (b * c)).mono $ λ x hx, le_of_mul_le_mul_right hx hc @[simp] lemma tendsto_pow_at_top_iff {n : ℕ} : tendsto (λ x : α, x ^ n) at_top at_top ↔ n ≠ 0 := ⟨λ h hn, by simpa only [hn, pow_zero, not_tendsto_const_at_top] using h, tendsto_pow_at_top⟩ end linear_ordered_semiring lemma nonneg_of_eventually_pow_nonneg [linear_ordered_ring α] {a : α} (h : ∀ᶠ n in at_top, 0 ≤ a ^ (n : ℕ)) : 0 ≤ a := let ⟨n, hn⟩ := (tendsto_bit1_at_top.eventually h).exists in pow_bit1_nonneg_iff.1 hn lemma not_tendsto_pow_at_top_at_bot [linear_ordered_ring α] : ∀ {n : ℕ}, ¬tendsto (λ x : α, x ^ n) at_top at_bot | 0 := by simp [not_tendsto_const_at_bot] | (n + 1) := (tendsto_pow_at_top n.succ_ne_zero).not_tendsto disjoint_at_top_at_bot section linear_ordered_semifield variables [linear_ordered_semifield α] {l : filter β} {f : β → α} {r c : α} {n : ℕ} /-! ### Multiplication by constant: iff lemmas -/ /-- If `r` is a positive constant, then `λ x, r * f x` tends to infinity along a filter if and only if `f` tends to infinity along the same filter. -/ lemma tendsto_const_mul_at_top_of_pos (hr : 0 < r) : tendsto (λ x, r * f x) l at_top ↔ tendsto f l at_top := ⟨λ h, h.at_top_of_const_mul hr, λ h, tendsto.at_top_of_const_mul (inv_pos.2 hr) $ by simpa only [inv_mul_cancel_left₀ hr.ne']⟩ /-- If `r` is a positive constant, then `λ x, f x * r` tends to infinity along a filter if and only if `f` tends to infinity along the same filter. -/ lemma tendsto_mul_const_at_top_of_pos (hr : 0 < r) : tendsto (λ x, f x * r) l at_top ↔ tendsto f l at_top := by simpa only [mul_comm] using tendsto_const_mul_at_top_of_pos hr /-- If `f` tends to infinity along a nontrivial filter `l`, then `λ x, r * f x` tends to infinity if and only if `0 < r. `-/ lemma tendsto_const_mul_at_top_iff_pos [ne_bot l] (h : tendsto f l at_top) : tendsto (λ x, r * f x) l at_top ↔ 0 < r := begin refine ⟨λ hrf, not_le.mp $ λ hr, _, λ hr, (tendsto_const_mul_at_top_of_pos hr).mpr h⟩, rcases ((h.eventually_ge_at_top 0).and (hrf.eventually_gt_at_top 0)).exists with ⟨x, hx, hrx⟩, exact (mul_nonpos_of_nonpos_of_nonneg hr hx).not_lt hrx end /-- If `f` tends to infinity along a nontrivial filter `l`, then `λ x, f x * r` tends to infinity if and only if `0 < r. `-/ lemma tendsto_mul_const_at_top_iff_pos [ne_bot l] (h : tendsto f l at_top) : tendsto (λ x, f x * r) l at_top ↔ 0 < r := by simp only [mul_comm _ r, tendsto_const_mul_at_top_iff_pos h] /-- If a function tends to infinity along a filter, then this function multiplied by a positive constant (on the left) also tends to infinity. For a version working in `ℕ` or `ℤ`, use `filter.tendsto.const_mul_at_top'` instead. -/ lemma tendsto.const_mul_at_top (hr : 0 < r) (hf : tendsto f l at_top) : tendsto (λx, r * f x) l at_top := (tendsto_const_mul_at_top_of_pos hr).2 hf /-- If a function tends to infinity along a filter, then this function multiplied by a positive constant (on the right) also tends to infinity. For a version working in `ℕ` or `ℤ`, use `filter.tendsto.at_top_mul_const'` instead. -/ lemma tendsto.at_top_mul_const (hr : 0 < r) (hf : tendsto f l at_top) : tendsto (λx, f x * r) l at_top := (tendsto_mul_const_at_top_of_pos hr).2 hf /-- If a function tends to infinity along a filter, then this function divided by a positive constant also tends to infinity. -/ lemma tendsto.at_top_div_const (hr : 0 < r) (hf : tendsto f l at_top) : tendsto (λx, f x / r) l at_top := by simpa only [div_eq_mul_inv] using hf.at_top_mul_const (inv_pos.2 hr) lemma tendsto_const_mul_pow_at_top (hn : n ≠ 0) (hc : 0 < c) : tendsto (λ x, c * x^n) at_top at_top := tendsto.const_mul_at_top hc (tendsto_pow_at_top hn) lemma tendsto_const_mul_pow_at_top_iff : tendsto (λ x, c * x^n) at_top at_top ↔ n ≠ 0 ∧ 0 < c := begin refine ⟨λ h, ⟨_, _⟩, λ h, tendsto_const_mul_pow_at_top h.1 h.2⟩, { rintro rfl, simpa only [pow_zero, not_tendsto_const_at_top] using h }, { rcases ((h.eventually_gt_at_top 0).and (eventually_ge_at_top 0)).exists with ⟨k, hck, hk⟩, exact pos_of_mul_pos_left hck (pow_nonneg hk _) }, end end linear_ordered_semifield section linear_ordered_field variables [linear_ordered_field α] {l : filter β} {f : β → α} {r : α} /-- If `r` is a positive constant, then `λ x, r * f x` tends to negative infinity along a filter if and only if `f` tends to negative infinity along the same filter. -/ lemma tendsto_const_mul_at_bot_of_pos (hr : 0 < r) : tendsto (λ x, r * f x) l at_bot ↔ tendsto f l at_bot := by simpa only [← mul_neg, ← tendsto_neg_at_top_iff] using tendsto_const_mul_at_top_of_pos hr /-- If `r` is a positive constant, then `λ x, f x * r` tends to negative infinity along a filter if and only if `f` tends to negative infinity along the same filter. -/ lemma tendsto_mul_const_at_bot_of_pos (hr : 0 < r) : tendsto (λ x, f x * r) l at_bot ↔ tendsto f l at_bot := by simpa only [mul_comm] using tendsto_const_mul_at_bot_of_pos hr /-- If `r` is a negative constant, then `λ x, r * f x` tends to infinity along a filter if and only if `f` tends to negative infinity along the same filter. -/ lemma tendsto_const_mul_at_top_of_neg (hr : r < 0) : tendsto (λ x, r * f x) l at_top ↔ tendsto f l at_bot := by simpa only [neg_mul, tendsto_neg_at_bot_iff] using tendsto_const_mul_at_bot_of_pos (neg_pos.2 hr) /-- If `r` is a negative constant, then `λ x, f x * r` tends to infinity along a filter if and only if `f` tends to negative infinity along the same filter. -/ lemma tendsto_mul_const_at_top_of_neg (hr : r < 0) : tendsto (λ x, f x * r) l at_top ↔ tendsto f l at_bot := by simpa only [mul_comm] using tendsto_const_mul_at_top_of_neg hr /-- If `r` is a negative constant, then `λ x, r * f x` tends to negative infinity along a filter if and only if `f` tends to infinity along the same filter. -/ lemma tendsto_const_mul_at_bot_of_neg (hr : r < 0) : tendsto (λ x, r * f x) l at_bot ↔ tendsto f l at_top := by simpa only [neg_mul, tendsto_neg_at_top_iff] using tendsto_const_mul_at_top_of_pos (neg_pos.2 hr) /-- If `r` is a negative constant, then `λ x, f x * r` tends to negative infinity along a filter if and only if `f` tends to infinity along the same filter. -/ lemma tendsto_mul_const_at_bot_of_neg (hr : r < 0) : tendsto (λ x, f x * r) l at_bot ↔ tendsto f l at_top := by simpa only [mul_comm] using tendsto_const_mul_at_bot_of_neg hr /-- The function `λ x, r * f x` tends to infinity along a nontrivial filter if and only if `r > 0` and `f` tends to infinity or `r < 0` and `f` tends to negative infinity. -/ lemma tendsto_const_mul_at_top_iff [ne_bot l] : tendsto (λ x, r * f x) l at_top ↔ 0 < r ∧ tendsto f l at_top ∨ r < 0 ∧ tendsto f l at_bot := begin rcases lt_trichotomy r 0 with hr|rfl|hr, { simp [hr, hr.not_lt, tendsto_const_mul_at_top_of_neg] }, { simp [not_tendsto_const_at_top] }, { simp [hr, hr.not_lt, tendsto_const_mul_at_top_of_pos] } end /-- The function `λ x, f x * r` tends to infinity along a nontrivial filter if and only if `r > 0` and `f` tends to infinity or `r < 0` and `f` tends to negative infinity. -/ lemma tendsto_mul_const_at_top_iff [ne_bot l] : tendsto (λ x, f x * r) l at_top ↔ 0 < r ∧ tendsto f l at_top ∨ r < 0 ∧ tendsto f l at_bot := by simp only [mul_comm _ r, tendsto_const_mul_at_top_iff] /-- The function `λ x, r * f x` tends to negative infinity along a nontrivial filter if and only if `r > 0` and `f` tends to negative infinity or `r < 0` and `f` tends to infinity. -/ lemma tendsto_const_mul_at_bot_iff [ne_bot l] : tendsto (λ x, r * f x) l at_bot ↔ 0 < r ∧ tendsto f l at_bot ∨ r < 0 ∧ tendsto f l at_top := by simp only [← tendsto_neg_at_top_iff, ← mul_neg, tendsto_const_mul_at_top_iff, neg_neg] /-- The function `λ x, f x * r` tends to negative infinity along a nontrivial filter if and only if `r > 0` and `f` tends to negative infinity or `r < 0` and `f` tends to infinity. -/ lemma tendsto_mul_const_at_bot_iff [ne_bot l] : tendsto (λ x, f x * r) l at_bot ↔ 0 < r ∧ tendsto f l at_bot ∨ r < 0 ∧ tendsto f l at_top := by simp only [mul_comm _ r, tendsto_const_mul_at_bot_iff] /-- If `f` tends to negative infinity along a nontrivial filter `l`, then `λ x, r * f x` tends to infinity if and only if `r < 0. `-/ lemma tendsto_const_mul_at_top_iff_neg [ne_bot l] (h : tendsto f l at_bot) : tendsto (λ x, r * f x) l at_top ↔ r < 0 := by simp [tendsto_const_mul_at_top_iff, h, h.not_tendsto disjoint_at_bot_at_top] /-- If `f` tends to negative infinity along a nontrivial filter `l`, then `λ x, f x * r` tends to infinity if and only if `r < 0. `-/ lemma tendsto_mul_const_at_top_iff_neg [ne_bot l] (h : tendsto f l at_bot) : tendsto (λ x, f x * r) l at_top ↔ r < 0 := by simp only [mul_comm _ r, tendsto_const_mul_at_top_iff_neg h] /-- If `f` tends to negative infinity along a nontrivial filter `l`, then `λ x, r * f x` tends to negative infinity if and only if `0 < r. `-/ lemma tendsto_const_mul_at_bot_iff_pos [ne_bot l] (h : tendsto f l at_bot) : tendsto (λ x, r * f x) l at_bot ↔ 0 < r := by simp [tendsto_const_mul_at_bot_iff, h, h.not_tendsto disjoint_at_bot_at_top] /-- If `f` tends to negative infinity along a nontrivial filter `l`, then `λ x, f x * r` tends to negative infinity if and only if `0 < r. `-/ lemma tendsto_mul_const_at_bot_iff_pos [ne_bot l] (h : tendsto f l at_bot) : tendsto (λ x, f x * r) l at_bot ↔ 0 < r := by simp only [mul_comm _ r, tendsto_const_mul_at_bot_iff_pos h] /-- If `f` tends to infinity along a nontrivial filter `l`, then `λ x, r * f x` tends to negative infinity if and only if `r < 0. `-/ lemma tendsto_const_mul_at_bot_iff_neg [ne_bot l] (h : tendsto f l at_top) : tendsto (λ x, r * f x) l at_bot ↔ r < 0 := by simp [tendsto_const_mul_at_bot_iff, h, h.not_tendsto disjoint_at_top_at_bot] /-- If `f` tends to infinity along a nontrivial filter `l`, then `λ x, f x * r` tends to negative infinity if and only if `r < 0. `-/ lemma tendsto_mul_const_at_bot_iff_neg [ne_bot l] (h : tendsto f l at_top) : tendsto (λ x, f x * r) l at_bot ↔ r < 0 := by simp only [mul_comm _ r, tendsto_const_mul_at_bot_iff_neg h] /-- If a function tends to infinity along a filter, then this function multiplied by a negative constant (on the left) tends to negative infinity. -/ lemma tendsto.neg_const_mul_at_top (hr : r < 0) (hf : tendsto f l at_top) : tendsto (λ x, r * f x) l at_bot := (tendsto_const_mul_at_bot_of_neg hr).2 hf /-- If a function tends to infinity along a filter, then this function multiplied by a negative constant (on the right) tends to negative infinity. -/ lemma tendsto.at_top_mul_neg_const (hr : r < 0) (hf : tendsto f l at_top) : tendsto (λ x, f x * r) l at_bot := (tendsto_mul_const_at_bot_of_neg hr).2 hf /-- If a function tends to negative infinity along a filter, then this function multiplied by a positive constant (on the left) also tends to negative infinity. -/ lemma tendsto.const_mul_at_bot (hr : 0 < r) (hf : tendsto f l at_bot) : tendsto (λx, r * f x) l at_bot := (tendsto_const_mul_at_bot_of_pos hr).2 hf /-- If a function tends to negative infinity along a filter, then this function multiplied by a positive constant (on the right) also tends to negative infinity. -/ lemma tendsto.at_bot_mul_const (hr : 0 < r) (hf : tendsto f l at_bot) : tendsto (λx, f x * r) l at_bot := (tendsto_mul_const_at_bot_of_pos hr).2 hf /-- If a function tends to negative infinity along a filter, then this function divided by a positive constant also tends to negative infinity. -/ lemma tendsto.at_bot_div_const (hr : 0 < r) (hf : tendsto f l at_bot) : tendsto (λx, f x / r) l at_bot := by simpa only [div_eq_mul_inv] using hf.at_bot_mul_const (inv_pos.2 hr) /-- If a function tends to negative infinity along a filter, then this function multiplied by a negative constant (on the left) tends to positive infinity. -/ lemma tendsto.neg_const_mul_at_bot (hr : r < 0) (hf : tendsto f l at_bot) : tendsto (λ x, r * f x) l at_top := (tendsto_const_mul_at_top_of_neg hr).2 hf /-- If a function tends to negative infinity along a filter, then this function multiplied by a negative constant (on the right) tends to positive infinity. -/ lemma tendsto.at_bot_mul_neg_const (hr : r < 0) (hf : tendsto f l at_bot) : tendsto (λ x, f x * r) l at_top := (tendsto_mul_const_at_top_of_neg hr).2 hf lemma tendsto_neg_const_mul_pow_at_top {c : α} {n : ℕ} (hn : n ≠ 0) (hc : c < 0) : tendsto (λ x, c * x^n) at_top at_bot := tendsto.neg_const_mul_at_top hc (tendsto_pow_at_top hn) lemma tendsto_const_mul_pow_at_bot_iff {c : α} {n : ℕ} : tendsto (λ x, c * x^n) at_top at_bot ↔ n ≠ 0 ∧ c < 0 := by simp only [← tendsto_neg_at_top_iff, ← neg_mul, tendsto_const_mul_pow_at_top_iff, neg_pos] end linear_ordered_field open_locale filter lemma tendsto_at_top' [nonempty α] [semilattice_sup α] {f : α → β} {l : filter β} : tendsto f at_top l ↔ (∀s ∈ l, ∃a, ∀b≥a, f b ∈ s) := by simp only [tendsto_def, mem_at_top_sets]; refl lemma tendsto_at_bot' [nonempty α] [semilattice_inf α] {f : α → β} {l : filter β} : tendsto f at_bot l ↔ (∀s ∈ l, ∃a, ∀b≤a, f b ∈ s) := @tendsto_at_top' αᵒᵈ _ _ _ _ _ theorem tendsto_at_top_principal [nonempty β] [semilattice_sup β] {f : β → α} {s : set α} : tendsto f at_top (𝓟 s) ↔ ∃N, ∀n≥N, f n ∈ s := by rw [tendsto_iff_comap, comap_principal, le_principal_iff, mem_at_top_sets]; refl theorem tendsto_at_bot_principal [nonempty β] [semilattice_inf β] {f : β → α} {s : set α} : tendsto f at_bot (𝓟 s) ↔ ∃N, ∀n≤N, f n ∈ s := @tendsto_at_top_principal _ βᵒᵈ _ _ _ _ /-- A function `f` grows to `+∞` independent of an order-preserving embedding `e`. -/ lemma tendsto_at_top_at_top [nonempty α] [semilattice_sup α] [preorder β] {f : α → β} : tendsto f at_top at_top ↔ ∀ b : β, ∃ i : α, ∀ a : α, i ≤ a → b ≤ f a := iff.trans tendsto_infi $ forall_congr $ assume b, tendsto_at_top_principal lemma tendsto_at_top_at_bot [nonempty α] [semilattice_sup α] [preorder β] {f : α → β} : tendsto f at_top at_bot ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), i ≤ a → f a ≤ b := @tendsto_at_top_at_top α βᵒᵈ _ _ _ f lemma tendsto_at_bot_at_top [nonempty α] [semilattice_inf α] [preorder β] {f : α → β} : tendsto f at_bot at_top ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), a ≤ i → b ≤ f a := @tendsto_at_top_at_top αᵒᵈ β _ _ _ f lemma tendsto_at_bot_at_bot [nonempty α] [semilattice_inf α] [preorder β] {f : α → β} : tendsto f at_bot at_bot ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), a ≤ i → f a ≤ b := @tendsto_at_top_at_top αᵒᵈ βᵒᵈ _ _ _ f lemma tendsto_at_top_at_top_of_monotone [preorder α] [preorder β] {f : α → β} (hf : monotone f) (h : ∀ b, ∃ a, b ≤ f a) : tendsto f at_top at_top := tendsto_infi.2 $ λ b, tendsto_principal.2 $ let ⟨a, ha⟩ := h b in mem_of_superset (mem_at_top a) $ λ a' ha', le_trans ha (hf ha') lemma tendsto_at_bot_at_bot_of_monotone [preorder α] [preorder β] {f : α → β} (hf : monotone f) (h : ∀ b, ∃ a, f a ≤ b) : tendsto f at_bot at_bot := tendsto_infi.2 $ λ b, tendsto_principal.2 $ let ⟨a, ha⟩ := h b in mem_of_superset (mem_at_bot a) $ λ a' ha', le_trans (hf ha') ha lemma tendsto_at_top_at_top_iff_of_monotone [nonempty α] [semilattice_sup α] [preorder β] {f : α → β} (hf : monotone f) : tendsto f at_top at_top ↔ ∀ b : β, ∃ a : α, b ≤ f a := tendsto_at_top_at_top.trans $ forall_congr $ λ b, exists_congr $ λ a, ⟨λ h, h a (le_refl a), λ h a' ha', le_trans h $ hf ha'⟩ lemma tendsto_at_bot_at_bot_iff_of_monotone [nonempty α] [semilattice_inf α] [preorder β] {f : α → β} (hf : monotone f) : tendsto f at_bot at_bot ↔ ∀ b : β, ∃ a : α, f a ≤ b := tendsto_at_bot_at_bot.trans $ forall_congr $ λ b, exists_congr $ λ a, ⟨λ h, h a (le_refl a), λ h a' ha', le_trans (hf ha') h⟩ alias tendsto_at_top_at_top_of_monotone ← _root_.monotone.tendsto_at_top_at_top alias tendsto_at_bot_at_bot_of_monotone ← _root_.monotone.tendsto_at_bot_at_bot alias tendsto_at_top_at_top_iff_of_monotone ← _root_.monotone.tendsto_at_top_at_top_iff alias tendsto_at_bot_at_bot_iff_of_monotone ← _root_.monotone.tendsto_at_bot_at_bot_iff lemma comap_embedding_at_top [preorder β] [preorder γ] {e : β → γ} (hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, c ≤ e b) : comap e at_top = at_top := le_antisymm (le_infi $ λ b, le_principal_iff.2 $ mem_comap.2 ⟨Ici (e b), mem_at_top _, λ x, (hm _ _).1⟩) (tendsto_at_top_at_top_of_monotone (λ _ _, (hm _ _).2) hu).le_comap lemma comap_embedding_at_bot [preorder β] [preorder γ] {e : β → γ} (hm : ∀ b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, e b ≤ c) : comap e at_bot = at_bot := @comap_embedding_at_top βᵒᵈ γᵒᵈ _ _ e (function.swap hm) hu lemma tendsto_at_top_embedding [preorder β] [preorder γ] {f : α → β} {e : β → γ} {l : filter α} (hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, c ≤ e b) : tendsto (e ∘ f) l at_top ↔ tendsto f l at_top := by rw [← comap_embedding_at_top hm hu, tendsto_comap_iff] /-- A function `f` goes to `-∞` independent of an order-preserving embedding `e`. -/ lemma tendsto_at_bot_embedding [preorder β] [preorder γ] {f : α → β} {e : β → γ} {l : filter α} (hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, e b ≤ c) : tendsto (e ∘ f) l at_bot ↔ tendsto f l at_bot := @tendsto_at_top_embedding α βᵒᵈ γᵒᵈ _ _ f e l (function.swap hm) hu lemma tendsto_finset_range : tendsto finset.range at_top at_top := finset.range_mono.tendsto_at_top_at_top finset.exists_nat_subset_range lemma at_top_finset_eq_infi : (at_top : filter $ finset α) = ⨅ x : α, 𝓟 (Ici {x}) := begin refine le_antisymm (le_infi (λ i, le_principal_iff.2 $ mem_at_top {i})) _, refine le_infi (λ s, le_principal_iff.2 $ mem_infi_of_Inter s.finite_to_set (λ i, mem_principal_self _) _), simp only [subset_def, mem_Inter, set_coe.forall, mem_Ici, finset.le_iff_subset, finset.mem_singleton, finset.subset_iff, forall_eq], dsimp, exact λ t, id end /-- If `f` is a monotone sequence of `finset`s and each `x` belongs to one of `f n`, then `tendsto f at_top at_top`. -/ lemma tendsto_at_top_finset_of_monotone [preorder β] {f : β → finset α} (h : monotone f) (h' : ∀ x : α, ∃ n, x ∈ f n) : tendsto f at_top at_top := begin simp only [at_top_finset_eq_infi, tendsto_infi, tendsto_principal], intro a, rcases h' a with ⟨b, hb⟩, exact eventually.mono (mem_at_top b) (λ b' hb', le_trans (finset.singleton_subset_iff.2 hb) (h hb')), end alias tendsto_at_top_finset_of_monotone ← _root_.monotone.tendsto_at_top_finset lemma tendsto_finset_image_at_top_at_top {i : β → γ} {j : γ → β} (h : function.left_inverse j i) : tendsto (finset.image j) at_top at_top := (finset.image_mono j).tendsto_at_top_finset $ assume a, ⟨{i a}, by simp only [finset.image_singleton, h a, finset.mem_singleton]⟩ lemma tendsto_finset_preimage_at_top_at_top {f : α → β} (hf : function.injective f) : tendsto (λ s : finset β, s.preimage f (hf.inj_on _)) at_top at_top := (finset.monotone_preimage hf).tendsto_at_top_finset $ λ x, ⟨{f x}, finset.mem_preimage.2 $ finset.mem_singleton_self _⟩ lemma prod_at_top_at_top_eq {β₁ β₂ : Type*} [semilattice_sup β₁] [semilattice_sup β₂] : (at_top : filter β₁) ×ᶠ (at_top : filter β₂) = (at_top : filter (β₁ × β₂)) := begin casesI (is_empty_or_nonempty β₁).symm, casesI (is_empty_or_nonempty β₂).symm, { simp [at_top, prod_infi_left, prod_infi_right, infi_prod], exact infi_comm, }, { simp only [at_top.filter_eq_bot_of_is_empty, prod_bot] }, { simp only [at_top.filter_eq_bot_of_is_empty, bot_prod] }, end lemma prod_at_bot_at_bot_eq {β₁ β₂ : Type*} [semilattice_inf β₁] [semilattice_inf β₂] : (at_bot : filter β₁) ×ᶠ (at_bot : filter β₂) = (at_bot : filter (β₁ × β₂)) := @prod_at_top_at_top_eq β₁ᵒᵈ β₂ᵒᵈ _ _ lemma prod_map_at_top_eq {α₁ α₂ β₁ β₂ : Type*} [semilattice_sup β₁] [semilattice_sup β₂] (u₁ : β₁ → α₁) (u₂ : β₂ → α₂) : (map u₁ at_top) ×ᶠ (map u₂ at_top) = map (prod.map u₁ u₂) at_top := by rw [prod_map_map_eq, prod_at_top_at_top_eq, prod.map_def] lemma prod_map_at_bot_eq {α₁ α₂ β₁ β₂ : Type*} [semilattice_inf β₁] [semilattice_inf β₂] (u₁ : β₁ → α₁) (u₂ : β₂ → α₂) : (map u₁ at_bot) ×ᶠ (map u₂ at_bot) = map (prod.map u₁ u₂) at_bot := @prod_map_at_top_eq _ _ β₁ᵒᵈ β₂ᵒᵈ _ _ _ _ lemma tendsto.subseq_mem {F : filter α} {V : ℕ → set α} (h : ∀ n, V n ∈ F) {u : ℕ → α} (hu : tendsto u at_top F) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, u (φ n) ∈ V n := extraction_forall_of_eventually' (λ n, tendsto_at_top'.mp hu _ (h n) : ∀ n, ∃ N, ∀ k ≥ N, u k ∈ V n) lemma tendsto_at_bot_diagonal [semilattice_inf α] : tendsto (λ a : α, (a, a)) at_bot at_bot := by { rw ← prod_at_bot_at_bot_eq, exact tendsto_id.prod_mk tendsto_id } lemma tendsto_at_top_diagonal [semilattice_sup α] : tendsto (λ a : α, (a, a)) at_top at_top := by { rw ← prod_at_top_at_top_eq, exact tendsto_id.prod_mk tendsto_id } lemma tendsto.prod_map_prod_at_bot [semilattice_inf γ] {F : filter α} {G : filter β} {f : α → γ} {g : β → γ} (hf : tendsto f F at_bot) (hg : tendsto g G at_bot) : tendsto (prod.map f g) (F ×ᶠ G) at_bot := by { rw ← prod_at_bot_at_bot_eq, exact hf.prod_map hg, } lemma tendsto.prod_map_prod_at_top [semilattice_sup γ] {F : filter α} {G : filter β} {f : α → γ} {g : β → γ} (hf : tendsto f F at_top) (hg : tendsto g G at_top) : tendsto (prod.map f g) (F ×ᶠ G) at_top := by { rw ← prod_at_top_at_top_eq, exact hf.prod_map hg, } lemma tendsto.prod_at_bot [semilattice_inf α] [semilattice_inf γ] {f g : α → γ} (hf : tendsto f at_bot at_bot) (hg : tendsto g at_bot at_bot) : tendsto (prod.map f g) at_bot at_bot := by { rw ← prod_at_bot_at_bot_eq, exact hf.prod_map_prod_at_bot hg, } lemma tendsto.prod_at_top [semilattice_sup α] [semilattice_sup γ] {f g : α → γ} (hf : tendsto f at_top at_top) (hg : tendsto g at_top at_top) : tendsto (prod.map f g) at_top at_top := by { rw ← prod_at_top_at_top_eq, exact hf.prod_map_prod_at_top hg, } lemma eventually_at_bot_prod_self [semilattice_inf α] [nonempty α] {p : α × α → Prop} : (∀ᶠ x in at_bot, p x) ↔ (∃ a, ∀ k l, k ≤ a → l ≤ a → p (k, l)) := by simp [← prod_at_bot_at_bot_eq, at_bot_basis.prod_self.eventually_iff] lemma eventually_at_top_prod_self [semilattice_sup α] [nonempty α] {p : α × α → Prop} : (∀ᶠ x in at_top, p x) ↔ (∃ a, ∀ k l, a ≤ k → a ≤ l → p (k, l)) := by simp [← prod_at_top_at_top_eq, at_top_basis.prod_self.eventually_iff] lemma eventually_at_bot_prod_self' [semilattice_inf α] [nonempty α] {p : α × α → Prop} : (∀ᶠ x in at_bot, p x) ↔ (∃ a, ∀ k ≤ a, ∀ l ≤ a, p (k, l)) := begin rw filter.eventually_at_bot_prod_self, apply exists_congr, tauto, end lemma eventually_at_top_prod_self' [semilattice_sup α] [nonempty α] {p : α × α → Prop} : (∀ᶠ x in at_top, p x) ↔ (∃ a, ∀ k ≥ a, ∀ l ≥ a, p (k, l)) := begin rw filter.eventually_at_top_prod_self, apply exists_congr, tauto, end lemma eventually_at_top_curry [semilattice_sup α] [semilattice_sup β] {p : α × β → Prop} (hp : ∀ᶠ (x : α × β) in filter.at_top, p x) : ∀ᶠ k in at_top, ∀ᶠ l in at_top, p (k, l) := begin rw ← prod_at_top_at_top_eq at hp, exact hp.curry, end lemma eventually_at_bot_curry [semilattice_inf α] [semilattice_inf β] {p : α × β → Prop} (hp : ∀ᶠ (x : α × β) in filter.at_bot, p x) : ∀ᶠ k in at_bot, ∀ᶠ l in at_bot, p (k, l) := @eventually_at_top_curry αᵒᵈ βᵒᵈ _ _ _ hp /-- A function `f` maps upwards closed sets (at_top sets) to upwards closed sets when it is a Galois insertion. The Galois "insertion" and "connection" is weakened to only require it to be an insertion and a connetion above `b'`. -/ lemma map_at_top_eq_of_gc [semilattice_sup α] [semilattice_sup β] {f : α → β} (g : β → α) (b' : β) (hf : monotone f) (gc : ∀a, ∀b≥b', f a ≤ b ↔ a ≤ g b) (hgi : ∀b≥b', b ≤ f (g b)) : map f at_top = at_top := begin refine le_antisymm (hf.tendsto_at_top_at_top $ λ b, ⟨g (b ⊔ b'), le_sup_left.trans $ hgi _ le_sup_right⟩) _, rw [@map_at_top_eq _ _ ⟨g b'⟩], refine le_infi (λ a, infi_le_of_le (f a ⊔ b') $ principal_mono.2 $ λ b hb, _), rw [mem_Ici, sup_le_iff] at hb, exact ⟨g b, (gc _ _ hb.2).1 hb.1, le_antisymm ((gc _ _ hb.2).2 le_rfl) (hgi _ hb.2)⟩ end lemma map_at_bot_eq_of_gc [semilattice_inf α] [semilattice_inf β] {f : α → β} (g : β → α) (b' : β) (hf : monotone f) (gc : ∀a, ∀b≤b', b ≤ f a ↔ g b ≤ a) (hgi : ∀b≤b', f (g b) ≤ b) : map f at_bot = at_bot := @map_at_top_eq_of_gc αᵒᵈ βᵒᵈ _ _ _ _ _ hf.dual gc hgi lemma map_coe_at_top_of_Ici_subset [semilattice_sup α] {a : α} {s : set α} (h : Ici a ⊆ s) : map (coe : s → α) at_top = at_top := begin have : directed (≥) (λ x : s, 𝓟 (Ici x)), { intros x y, use ⟨x ⊔ y ⊔ a, h le_sup_right⟩, simp only [ge_iff_le, principal_mono, Ici_subset_Ici, ← subtype.coe_le_coe, subtype.coe_mk], exact ⟨le_sup_left.trans le_sup_left, le_sup_right.trans le_sup_left⟩ }, haveI : nonempty s := ⟨⟨a, h le_rfl⟩⟩, simp only [le_antisymm_iff, at_top, le_infi_iff, le_principal_iff, mem_map, mem_set_of_eq, map_infi_eq this, map_principal], split, { intro x, refine mem_of_superset (mem_infi_of_mem ⟨x ⊔ a, h le_sup_right⟩ (mem_principal_self _)) _, rintro _ ⟨y, hy, rfl⟩, exact le_trans le_sup_left (subtype.coe_le_coe.2 hy) }, { intro x, filter_upwards [mem_at_top (↑x ⊔ a)] with b hb, exact ⟨⟨b, h $ le_sup_right.trans hb⟩, subtype.coe_le_coe.1 (le_sup_left.trans hb), rfl⟩, }, end /-- The image of the filter `at_top` on `Ici a` under the coercion equals `at_top`. -/ @[simp] lemma map_coe_Ici_at_top [semilattice_sup α] (a : α) : map (coe : Ici a → α) at_top = at_top := map_coe_at_top_of_Ici_subset (subset.refl _) /-- The image of the filter `at_top` on `Ioi a` under the coercion equals `at_top`. -/ @[simp] lemma map_coe_Ioi_at_top [semilattice_sup α] [no_max_order α] (a : α) : map (coe : Ioi a → α) at_top = at_top := let ⟨b, hb⟩ := exists_gt a in map_coe_at_top_of_Ici_subset $ Ici_subset_Ioi.2 hb /-- The `at_top` filter for an open interval `Ioi a` comes from the `at_top` filter in the ambient order. -/ lemma at_top_Ioi_eq [semilattice_sup α] (a : α) : at_top = comap (coe : Ioi a → α) at_top := begin nontriviality, rcases nontrivial_iff_nonempty.1 ‹_› with ⟨b, hb⟩, rw [← map_coe_at_top_of_Ici_subset (Ici_subset_Ioi.2 hb), comap_map subtype.coe_injective] end /-- The `at_top` filter for an open interval `Ici a` comes from the `at_top` filter in the ambient order. -/ lemma at_top_Ici_eq [semilattice_sup α] (a : α) : at_top = comap (coe : Ici a → α) at_top := by rw [← map_coe_Ici_at_top a, comap_map subtype.coe_injective] /-- The `at_bot` filter for an open interval `Iio a` comes from the `at_bot` filter in the ambient order. -/ @[simp] lemma map_coe_Iio_at_bot [semilattice_inf α] [no_min_order α] (a : α) : map (coe : Iio a → α) at_bot = at_bot := @map_coe_Ioi_at_top αᵒᵈ _ _ _ /-- The `at_bot` filter for an open interval `Iio a` comes from the `at_bot` filter in the ambient order. -/ lemma at_bot_Iio_eq [semilattice_inf α] (a : α) : at_bot = comap (coe : Iio a → α) at_bot := @at_top_Ioi_eq αᵒᵈ _ _ /-- The `at_bot` filter for an open interval `Iic a` comes from the `at_bot` filter in the ambient order. -/ @[simp] lemma map_coe_Iic_at_bot [semilattice_inf α] (a : α) : map (coe : Iic a → α) at_bot = at_bot := @map_coe_Ici_at_top αᵒᵈ _ _ /-- The `at_bot` filter for an open interval `Iic a` comes from the `at_bot` filter in the ambient order. -/ lemma at_bot_Iic_eq [semilattice_inf α] (a : α) : at_bot = comap (coe : Iic a → α) at_bot := @at_top_Ici_eq αᵒᵈ _ _ lemma tendsto_Ioi_at_top [semilattice_sup α] {a : α} {f : β → Ioi a} {l : filter β} : tendsto f l at_top ↔ tendsto (λ x, (f x : α)) l at_top := by rw [at_top_Ioi_eq, tendsto_comap_iff] lemma tendsto_Iio_at_bot [semilattice_inf α] {a : α} {f : β → Iio a} {l : filter β} : tendsto f l at_bot ↔ tendsto (λ x, (f x : α)) l at_bot := by rw [at_bot_Iio_eq, tendsto_comap_iff] lemma tendsto_Ici_at_top [semilattice_sup α] {a : α} {f : β → Ici a} {l : filter β} : tendsto f l at_top ↔ tendsto (λ x, (f x : α)) l at_top := by rw [at_top_Ici_eq, tendsto_comap_iff] lemma tendsto_Iic_at_bot [semilattice_inf α] {a : α} {f : β → Iic a} {l : filter β} : tendsto f l at_bot ↔ tendsto (λ x, (f x : α)) l at_bot := by rw [at_bot_Iic_eq, tendsto_comap_iff] @[simp] lemma tendsto_comp_coe_Ioi_at_top [semilattice_sup α] [no_max_order α] {a : α} {f : α → β} {l : filter β} : tendsto (λ x : Ioi a, f x) at_top l ↔ tendsto f at_top l := by rw [← map_coe_Ioi_at_top a, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Ici_at_top [semilattice_sup α] {a : α} {f : α → β} {l : filter β} : tendsto (λ x : Ici a, f x) at_top l ↔ tendsto f at_top l := by rw [← map_coe_Ici_at_top a, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Iio_at_bot [semilattice_inf α] [no_min_order α] {a : α} {f : α → β} {l : filter β} : tendsto (λ x : Iio a, f x) at_bot l ↔ tendsto f at_bot l := by rw [← map_coe_Iio_at_bot a, tendsto_map'_iff] @[simp] lemma tendsto_comp_coe_Iic_at_bot [semilattice_inf α] {a : α} {f : α → β} {l : filter β} : tendsto (λ x : Iic a, f x) at_bot l ↔ tendsto f at_bot l := by rw [← map_coe_Iic_at_bot a, tendsto_map'_iff] lemma map_add_at_top_eq_nat (k : ℕ) : map (λa, a + k) at_top = at_top := map_at_top_eq_of_gc (λa, a - k) k (assume a b h, add_le_add_right h k) (assume a b h, (le_tsub_iff_right h).symm) (assume a h, by rw [tsub_add_cancel_of_le h]) lemma map_sub_at_top_eq_nat (k : ℕ) : map (λa, a - k) at_top = at_top := map_at_top_eq_of_gc (λa, a + k) 0 (assume a b h, tsub_le_tsub_right h _) (assume a b _, tsub_le_iff_right) (assume b _, by rw [add_tsub_cancel_right]) lemma tendsto_add_at_top_nat (k : ℕ) : tendsto (λa, a + k) at_top at_top := le_of_eq (map_add_at_top_eq_nat k) lemma tendsto_sub_at_top_nat (k : ℕ) : tendsto (λa, a - k) at_top at_top := le_of_eq (map_sub_at_top_eq_nat k) lemma tendsto_add_at_top_iff_nat {f : ℕ → α} {l : filter α} (k : ℕ) : tendsto (λn, f (n + k)) at_top l ↔ tendsto f at_top l := show tendsto (f ∘ (λn, n + k)) at_top l ↔ tendsto f at_top l, by rw [← tendsto_map'_iff, map_add_at_top_eq_nat] lemma map_div_at_top_eq_nat (k : ℕ) (hk : 0 < k) : map (λa, a / k) at_top = at_top := map_at_top_eq_of_gc (λb, b * k + (k - 1)) 1 (assume a b h, nat.div_le_div_right h) (assume a b _, calc a / k ≤ b ↔ a / k < b + 1 : by rw [← nat.succ_eq_add_one, nat.lt_succ_iff] ... ↔ a < (b + 1) * k : nat.div_lt_iff_lt_mul hk ... ↔ _ : begin cases k, exact (lt_irrefl _ hk).elim, rw [add_mul, one_mul, nat.succ_sub_succ_eq_sub, tsub_zero, nat.add_succ, nat.lt_succ_iff], end) (assume b _, calc b = (b * k) / k : by rw [nat.mul_div_cancel b hk] ... ≤ (b * k + (k - 1)) / k : nat.div_le_div_right $ nat.le_add_right _ _) /-- If `u` is a monotone function with linear ordered codomain and the range of `u` is not bounded above, then `tendsto u at_top at_top`. -/ lemma tendsto_at_top_at_top_of_monotone' [preorder ι] [linear_order α] {u : ι → α} (h : monotone u) (H : ¬bdd_above (range u)) : tendsto u at_top at_top := begin apply h.tendsto_at_top_at_top, intro b, rcases not_bdd_above_iff.1 H b with ⟨_, ⟨N, rfl⟩, hN⟩, exact ⟨N, le_of_lt hN⟩, end /-- If `u` is a monotone function with linear ordered codomain and the range of `u` is not bounded below, then `tendsto u at_bot at_bot`. -/ lemma tendsto_at_bot_at_bot_of_monotone' [preorder ι] [linear_order α] {u : ι → α} (h : monotone u) (H : ¬bdd_below (range u)) : tendsto u at_bot at_bot := @tendsto_at_top_at_top_of_monotone' ιᵒᵈ αᵒᵈ _ _ _ h.dual H lemma unbounded_of_tendsto_at_top [nonempty α] [semilattice_sup α] [preorder β] [no_max_order β] {f : α → β} (h : tendsto f at_top at_top) : ¬ bdd_above (range f) := begin rintros ⟨M, hM⟩, cases mem_at_top_sets.mp (h $ Ioi_mem_at_top M) with a ha, apply lt_irrefl M, calc M < f a : ha a le_rfl ... ≤ M : hM (set.mem_range_self a) end lemma unbounded_of_tendsto_at_bot [nonempty α] [semilattice_sup α] [preorder β] [no_min_order β] {f : α → β} (h : tendsto f at_top at_bot) : ¬ bdd_below (range f) := @unbounded_of_tendsto_at_top _ βᵒᵈ _ _ _ _ _ h lemma unbounded_of_tendsto_at_top' [nonempty α] [semilattice_inf α] [preorder β] [no_max_order β] {f : α → β} (h : tendsto f at_bot at_top) : ¬ bdd_above (range f) := @unbounded_of_tendsto_at_top αᵒᵈ _ _ _ _ _ _ h lemma unbounded_of_tendsto_at_bot' [nonempty α] [semilattice_inf α] [preorder β] [no_min_order β] {f : α → β} (h : tendsto f at_bot at_bot) : ¬ bdd_below (range f) := @unbounded_of_tendsto_at_top αᵒᵈ βᵒᵈ _ _ _ _ _ h /-- If a monotone function `u : ι → α` tends to `at_top` along *some* non-trivial filter `l`, then it tends to `at_top` along `at_top`. -/ lemma tendsto_at_top_of_monotone_of_filter [preorder ι] [preorder α] {l : filter ι} {u : ι → α} (h : monotone u) [ne_bot l] (hu : tendsto u l at_top) : tendsto u at_top at_top := h.tendsto_at_top_at_top $ λ b, (hu.eventually (mem_at_top b)).exists /-- If a monotone function `u : ι → α` tends to `at_bot` along *some* non-trivial filter `l`, then it tends to `at_bot` along `at_bot`. -/ lemma tendsto_at_bot_of_monotone_of_filter [preorder ι] [preorder α] {l : filter ι} {u : ι → α} (h : monotone u) [ne_bot l] (hu : tendsto u l at_bot) : tendsto u at_bot at_bot := @tendsto_at_top_of_monotone_of_filter ιᵒᵈ αᵒᵈ _ _ _ _ h.dual _ hu lemma tendsto_at_top_of_monotone_of_subseq [preorder ι] [preorder α] {u : ι → α} {φ : ι' → ι} (h : monotone u) {l : filter ι'} [ne_bot l] (H : tendsto (u ∘ φ) l at_top) : tendsto u at_top at_top := tendsto_at_top_of_monotone_of_filter h (tendsto_map' H) lemma tendsto_at_bot_of_monotone_of_subseq [preorder ι] [preorder α] {u : ι → α} {φ : ι' → ι} (h : monotone u) {l : filter ι'} [ne_bot l] (H : tendsto (u ∘ φ) l at_bot) : tendsto u at_bot at_bot := tendsto_at_bot_of_monotone_of_filter h (tendsto_map' H) /-- Let `f` and `g` be two maps to the same commutative monoid. This lemma gives a sufficient condition for comparison of the filter `at_top.map (λ s, ∏ b in s, f b)` with `at_top.map (λ s, ∏ b in s, g b)`. This is useful to compare the set of limit points of `Π b in s, f b` as `s → at_top` with the similar set for `g`. -/ @[to_additive "Let `f` and `g` be two maps to the same commutative additive monoid. This lemma gives a sufficient condition for comparison of the filter `at_top.map (λ s, ∑ b in s, f b)` with `at_top.map (λ s, ∑ b in s, g b)`. This is useful to compare the set of limit points of `∑ b in s, f b` as `s → at_top` with the similar set for `g`."] lemma map_at_top_finset_prod_le_of_prod_eq [comm_monoid α] {f : β → α} {g : γ → α} (h_eq : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ ∏ x in u', g x = ∏ b in v', f b) : at_top.map (λs:finset β, ∏ b in s, f b) ≤ at_top.map (λs:finset γ, ∏ x in s, g x) := by rw [map_at_top_eq, map_at_top_eq]; from (le_infi $ assume b, let ⟨v, hv⟩ := h_eq b in infi_le_of_le v $ by simp [set.image_subset_iff]; exact hv) lemma has_antitone_basis.eventually_subset [preorder ι] {l : filter α} {s : ι → set α} (hl : l.has_antitone_basis s) {t : set α} (ht : t ∈ l) : ∀ᶠ i in at_top, s i ⊆ t := let ⟨i, _, hi⟩ := hl.to_has_basis.mem_iff.1 ht in (eventually_ge_at_top i).mono $ λ j hj, (hl.antitone hj).trans hi protected lemma has_antitone_basis.tendsto [preorder ι] {l : filter α} {s : ι → set α} (hl : l.has_antitone_basis s) {φ : ι → α} (h : ∀ i : ι, φ i ∈ s i) : tendsto φ at_top l := λ t ht, mem_map.2 $ (hl.eventually_subset ht).mono $ λ i hi, hi (h i) lemma has_antitone_basis.comp_mono [semilattice_sup ι] [nonempty ι] [preorder ι'] {l : filter α} {s : ι' → set α} (hs : l.has_antitone_basis s) {φ : ι → ι'} (φ_mono : monotone φ) (hφ : tendsto φ at_top at_top) : l.has_antitone_basis (s ∘ φ) := ⟨hs.to_has_basis.to_has_basis (λ n hn, (hφ.eventually (eventually_ge_at_top n)).exists.imp $ λ m hm, ⟨trivial, hs.antitone hm⟩) (λ n hn, ⟨φ n, trivial, subset.rfl⟩), hs.antitone.comp_monotone φ_mono⟩ lemma has_antitone_basis.comp_strict_mono {l : filter α} {s : ℕ → set α} (hs : l.has_antitone_basis s) {φ : ℕ → ℕ} (hφ : strict_mono φ) : l.has_antitone_basis (s ∘ φ) := hs.comp_mono hφ.monotone hφ.tendsto_at_top /-- Given an antitone basis `s : ℕ → set α` of a filter, extract an antitone subbasis `s ∘ φ`, `φ : ℕ → ℕ`, such that `m < n` implies `r (φ m) (φ n)`. This lemma can be used to extract an antitone basis with basis sets decreasing "sufficiently fast". -/ lemma has_antitone_basis.subbasis_with_rel {f : filter α} {s : ℕ → set α} (hs : f.has_antitone_basis s) {r : ℕ → ℕ → Prop} (hr : ∀ m, ∀ᶠ n in at_top, r m n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ (∀ ⦃m n⦄, m < n → r (φ m) (φ n)) ∧ f.has_antitone_basis (s ∘ φ) := begin rsuffices ⟨φ, hφ, hrφ⟩ : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ m n, m < n → r (φ m) (φ n), { exact ⟨φ, hφ, hrφ, hs.comp_strict_mono hφ⟩ }, have : ∀ t : set ℕ, t.finite → ∀ᶠ n in at_top, ∀ m ∈ t, m < n ∧ r m n, from λ t ht, (eventually_all_finite ht).2 (λ m hm, (eventually_gt_at_top m).and (hr _)), rcases seq_of_forall_finite_exists (λ t ht, (this t ht).exists) with ⟨φ, hφ⟩, simp only [ball_image_iff, forall_and_distrib, mem_Iio] at hφ, exact ⟨φ, forall_swap.2 hφ.1, forall_swap.2 hφ.2⟩ end /-- If `f` is a nontrivial countably generated filter, then there exists a sequence that converges to `f`. -/ lemma exists_seq_tendsto (f : filter α) [is_countably_generated f] [ne_bot f] : ∃ x : ℕ → α, tendsto x at_top f := begin obtain ⟨B, h⟩ := f.exists_antitone_basis, choose x hx using λ n, filter.nonempty_of_mem (h.mem n), exact ⟨x, h.tendsto hx⟩ end /-- An abstract version of continuity of sequentially continuous functions on metric spaces: if a filter `k` is countably generated then `tendsto f k l` iff for every sequence `u` converging to `k`, `f ∘ u` tends to `l`. -/ lemma tendsto_iff_seq_tendsto {f : α → β} {k : filter α} {l : filter β} [k.is_countably_generated] : tendsto f k l ↔ (∀ x : ℕ → α, tendsto x at_top k → tendsto (f ∘ x) at_top l) := begin refine ⟨λ h x hx, h.comp hx, λ H s hs, _⟩, contrapose! H, haveI : ne_bot (k ⊓ 𝓟 (f ⁻¹' sᶜ)), by simpa [ne_bot_iff, inf_principal_eq_bot], rcases (k ⊓ 𝓟 (f ⁻¹' sᶜ)).exists_seq_tendsto with ⟨x, hx⟩, rw [tendsto_inf, tendsto_principal] at hx, refine ⟨x, hx.1, λ h, _⟩, rcases (hx.2.and (h hs)).exists with ⟨N, hnmem, hmem⟩, exact hnmem hmem end lemma tendsto_of_seq_tendsto {f : α → β} {k : filter α} {l : filter β} [k.is_countably_generated] : (∀ x : ℕ → α, tendsto x at_top k → tendsto (f ∘ x) at_top l) → tendsto f k l := tendsto_iff_seq_tendsto.2 lemma tendsto_iff_forall_eventually_mem {α ι : Type*} {x : ι → α} {f : filter α} {l : filter ι} : tendsto x l f ↔ ∀ s ∈ f, ∀ᶠ n in l, x n ∈ s := by { rw tendsto_def, refine forall_congr (λ s, imp_congr_right (λ hsf, _)), refl, } lemma not_tendsto_iff_exists_frequently_nmem {α ι : Type*} {x : ι → α} {f : filter α} {l : filter ι} : ¬ tendsto x l f ↔ ∃ s ∈ f, ∃ᶠ n in l, x n ∉ s := begin rw tendsto_iff_forall_eventually_mem, push_neg, refine exists_congr (λ s, _), rw [not_eventually, exists_prop], end lemma frequently_iff_seq_frequently {ι : Type*} {l : filter ι} {p : ι → Prop} [hl : l.is_countably_generated] : (∃ᶠ n in l, p n) ↔ ∃ (x : ℕ → ι), tendsto x at_top l ∧ ∃ᶠ (n : ℕ) in at_top, p (x n) := begin refine ⟨λ h_freq, _, λ h_exists_freq, _⟩, { haveI : ne_bot (l ⊓ 𝓟 {x : ι | p x}), by simpa [ne_bot_iff, inf_principal_eq_bot], obtain ⟨x, hx⟩ := exists_seq_tendsto (l ⊓ (𝓟 {x : ι | p x})), rw tendsto_inf at hx, cases hx with hx_l hx_p, refine ⟨x, hx_l, _⟩, rw tendsto_principal at hx_p, exact hx_p.frequently, }, { obtain ⟨x, hx_tendsto, hx_freq⟩ := h_exists_freq, simp_rw [filter.frequently, filter.eventually] at hx_freq ⊢, have : {n : ℕ | ¬p (x n)} = {n | x n ∈ {y | ¬ p y}} := rfl, rw [this, ← mem_map'] at hx_freq, contrapose! hx_freq, exact hx_tendsto hx_freq, }, end lemma eventually_iff_seq_eventually {ι : Type*} {l : filter ι} {p : ι → Prop} [hl : l.is_countably_generated] : (∀ᶠ n in l, p n) ↔ ∀ (x : ℕ → ι), tendsto x at_top l → ∀ᶠ (n : ℕ) in at_top, p (x n) := begin have : (∀ᶠ n in l, p n) ↔ ¬ ∃ᶠ n in l, ¬(p n), { rw not_frequently, simp_rw not_not, }, rw [this, frequently_iff_seq_frequently], push_neg, simp_rw [not_frequently, not_not], end lemma subseq_forall_of_frequently {ι : Type*} {x : ℕ → ι} {p : ι → Prop} {l : filter ι} (h_tendsto : tendsto x at_top l) (h : ∃ᶠ n in at_top, p (x n)) : ∃ ns : ℕ → ℕ, tendsto (λ n, x (ns n)) at_top l ∧ ∀ n, p (x (ns n)) := begin rw tendsto_iff_seq_tendsto at h_tendsto, choose ns hge hns using frequently_at_top.1 h, exact ⟨ns, h_tendsto ns (tendsto_at_top_mono hge tendsto_id), hns⟩, end lemma exists_seq_forall_of_frequently {ι : Type*} {l : filter ι} {p : ι → Prop} [hl : l.is_countably_generated] (h : ∃ᶠ n in l, p n) : ∃ ns : ℕ → ι, tendsto ns at_top l ∧ ∀ n, p (ns n) := begin rw frequently_iff_seq_frequently at h, obtain ⟨x, hx_tendsto, hx_freq⟩ := h, obtain ⟨n_to_n, h_tendsto, h_freq⟩ := subseq_forall_of_frequently hx_tendsto hx_freq, exact ⟨x ∘ n_to_n, h_tendsto, h_freq⟩, end /-- A sequence converges if every subsequence has a convergent subsequence. -/ lemma tendsto_of_subseq_tendsto {α ι : Type*} {x : ι → α} {f : filter α} {l : filter ι} [l.is_countably_generated] (hxy : ∀ ns : ℕ → ι, tendsto ns at_top l → ∃ ms : ℕ → ℕ, tendsto (λ n, x (ns $ ms n)) at_top f) : tendsto x l f := begin by_contra h, obtain ⟨s, hs, hfreq⟩ : ∃ s ∈ f, ∃ᶠ n in l, x n ∉ s, by rwa not_tendsto_iff_exists_frequently_nmem at h, obtain ⟨y, hy_tendsto, hy_freq⟩ := exists_seq_forall_of_frequently hfreq, specialize hxy y hy_tendsto, obtain ⟨ms, hms_tendsto⟩ := hxy, specialize hms_tendsto hs, rw mem_map at hms_tendsto, have hms_freq : ∀ (n : ℕ), x (y (ms n)) ∉ s, from λ n, hy_freq (ms n), have h_empty : (λ (n : ℕ), x (y (ms n))) ⁻¹' s = ∅, { ext1 n, simp only [set.mem_preimage, set.mem_empty_iff_false, iff_false], exact hms_freq n, }, rw h_empty at hms_tendsto, exact empty_not_mem at_top hms_tendsto, end lemma subseq_tendsto_of_ne_bot {f : filter α} [is_countably_generated f] {u : ℕ → α} (hx : ne_bot (f ⊓ map u at_top)) : ∃ (θ : ℕ → ℕ), (strict_mono θ) ∧ (tendsto (u ∘ θ) at_top f) := begin obtain ⟨B, h⟩ := f.exists_antitone_basis, have : ∀ N, ∃ n ≥ N, u n ∈ B N, from λ N, filter.inf_map_at_top_ne_bot_iff.mp hx _ (h.to_has_basis.mem_of_mem trivial) N, choose φ hφ using this, cases forall_and_distrib.mp hφ with φ_ge φ_in, have lim_uφ : tendsto (u ∘ φ) at_top f, from h.tendsto φ_in, have lim_φ : tendsto φ at_top at_top, from (tendsto_at_top_mono φ_ge tendsto_id), obtain ⟨ψ, hψ, hψφ⟩ : ∃ ψ : ℕ → ℕ, strict_mono ψ ∧ strict_mono (φ ∘ ψ), from strict_mono_subseq_of_tendsto_at_top lim_φ, exact ⟨φ ∘ ψ, hψφ, lim_uφ.comp hψ.tendsto_at_top⟩, end end filter open filter finset section variables {R : Type*} [linear_ordered_semiring R] lemma exists_lt_mul_self (a : R) : ∃ x ≥ 0, a < x * x := let ⟨x, hxa, hx0⟩ := ((tendsto_mul_self_at_top.eventually (eventually_gt_at_top a)).and (eventually_ge_at_top 0)).exists in ⟨x, hx0, hxa⟩ lemma exists_le_mul_self (a : R) : ∃ x ≥ 0, a ≤ x * x := let ⟨x, hx0, hxa⟩ := exists_lt_mul_self a in ⟨x, hx0, hxa.le⟩ end /-- Let `g : γ → β` be an injective function and `f : β → α` be a function from the codomain of `g` to a commutative monoid. Suppose that `f x = 1` outside of the range of `g`. Then the filters `at_top.map (λ s, ∏ i in s, f (g i))` and `at_top.map (λ s, ∏ i in s, f i)` coincide. The additive version of this lemma is used to prove the equality `∑' x, f (g x) = ∑' y, f y` under the same assumptions.-/ @[to_additive] lemma function.injective.map_at_top_finset_prod_eq [comm_monoid α] {g : γ → β} (hg : function.injective g) {f : β → α} (hf : ∀ x ∉ set.range g, f x = 1) : map (λ s, ∏ i in s, f (g i)) at_top = map (λ s, ∏ i in s, f i) at_top := begin apply le_antisymm; refine map_at_top_finset_prod_le_of_prod_eq (λ s, _), { refine ⟨s.preimage g (hg.inj_on _), λ t ht, _⟩, refine ⟨t.image g ∪ s, finset.subset_union_right _ _, _⟩, rw [← finset.prod_image (hg.inj_on _)], refine (prod_subset (subset_union_left _ _) _).symm, simp only [finset.mem_union, finset.mem_image], refine λ y hy hyt, hf y (mt _ hyt), rintros ⟨x, rfl⟩, exact ⟨x, ht (finset.mem_preimage.2 $ hy.resolve_left hyt), rfl⟩ }, { refine ⟨s.image g, λ t ht, _⟩, simp only [← prod_preimage _ _ (hg.inj_on _) _ (λ x _, hf x)], exact ⟨_, (image_subset_iff_subset_preimage _).1 ht, rfl⟩ } end /-- Let `g : γ → β` be an injective function and `f : β → α` be a function from the codomain of `g` to an additive commutative monoid. Suppose that `f x = 0` outside of the range of `g`. Then the filters `at_top.map (λ s, ∑ i in s, f (g i))` and `at_top.map (λ s, ∑ i in s, f i)` coincide. This lemma is used to prove the equality `∑' x, f (g x) = ∑' y, f y` under the same assumptions.-/ add_decl_doc function.injective.map_at_top_finset_sum_eq
158e51bfff49edc43d3da90f1424e08fe4a1387d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/special_functions/pow.lean
448127aefa8fa328ee830a85bfe63eccae315e68
[ "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
92,757
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Sébastien Gouëzel, Rémy Degenne, David Loeffler -/ import analysis.special_functions.complex.log /-! # Power function on `ℂ`, `ℝ`, `ℝ≥0`, and `ℝ≥0∞` We construct the power functions `x ^ y` where * `x` and `y` are complex numbers, * or `x` and `y` are real numbers, * or `x` is a nonnegative real number and `y` is a real number; * or `x` is a number from `[0, +∞]` (a.k.a. `ℝ≥0∞`) and `y` is a real number. We also prove basic properties of these functions. -/ noncomputable theory open_locale classical real topological_space nnreal ennreal filter big_operators asymptotics open filter finset set namespace complex /-- The complex power function `x^y`, given by `x^y = exp(y log x)` (where `log` is the principal determination of the logarithm), unless `x = 0` where one sets `0^0 = 1` and `0^y = 0` for `y ≠ 0`. -/ noncomputable def cpow (x y : ℂ) : ℂ := if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) noncomputable instance : has_pow ℂ ℂ := ⟨cpow⟩ @[simp] lemma cpow_eq_pow (x y : ℂ) : cpow x y = x ^ y := rfl lemma cpow_def (x y : ℂ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := rfl lemma cpow_def_of_ne_zero {x : ℂ} (hx : x ≠ 0) (y : ℂ) : x ^ y = exp (log x * y) := if_neg hx @[simp] lemma cpow_zero (x : ℂ) : x ^ (0 : ℂ) = 1 := by simp [cpow_def] @[simp] lemma cpow_eq_zero_iff (x y : ℂ) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [cpow_def], split_ifs; simp [*, exp_ne_zero] } @[simp] lemma zero_cpow {x : ℂ} (h : x ≠ 0) : (0 : ℂ) ^ x = 0 := by simp [cpow_def, *] lemma zero_cpow_eq_iff {x : ℂ} {a : ℂ} : 0 ^ x = a ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := begin split, { intros hyp, simp only [cpow_def, eq_self_iff_true, if_true] at hyp, by_cases x = 0, { subst h, simp only [if_true, eq_self_iff_true] at hyp, right, exact ⟨rfl, hyp.symm⟩}, { rw if_neg h at hyp, left, exact ⟨h, hyp.symm⟩, }, }, { rintro (⟨h, rfl⟩|⟨rfl,rfl⟩), { exact zero_cpow h, }, { exact cpow_zero _, }, }, end lemma eq_zero_cpow_iff {x : ℂ} {a : ℂ} : a = 0 ^ x ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := by rw [←zero_cpow_eq_iff, eq_comm] @[simp] lemma cpow_one (x : ℂ) : x ^ (1 : ℂ) = x := if hx : x = 0 then by simp [hx, cpow_def] else by rw [cpow_def, if_neg (one_ne_zero : (1 : ℂ) ≠ 0), if_neg hx, mul_one, exp_log hx] @[simp] lemma one_cpow (x : ℂ) : (1 : ℂ) ^ x = 1 := by rw cpow_def; split_ifs; simp [one_ne_zero, *] at * lemma cpow_add {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := by simp only [cpow_def, ite_mul, boole_mul, mul_ite, mul_boole]; simp [*, exp_add, mul_add] at * lemma cpow_mul {x y : ℂ} (z : ℂ) (h₁ : -π < (log x * y).im) (h₂ : (log x * y).im ≤ π) : x ^ (y * z) = (x ^ y) ^ z := begin simp only [cpow_def], split_ifs; simp [*, exp_ne_zero, log_exp h₁ h₂, mul_assoc] at * end lemma cpow_neg (x y : ℂ) : x ^ -y = (x ^ y)⁻¹ := by simp only [cpow_def, neg_eq_zero, mul_neg]; split_ifs; simp [exp_neg] lemma cpow_sub {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := by rw [sub_eq_add_neg, cpow_add _ _ hx, cpow_neg, div_eq_mul_inv] lemma cpow_neg_one (x : ℂ) : x ^ (-1 : ℂ) = x⁻¹ := by simpa using cpow_neg x 1 @[simp, norm_cast] lemma cpow_nat_cast (x : ℂ) : ∀ (n : ℕ), x ^ (n : ℂ) = x ^ n | 0 := by simp | (n + 1) := if hx : x = 0 then by simp only [hx, pow_succ, complex.zero_cpow (nat.cast_ne_zero.2 (nat.succ_ne_zero _)), zero_mul] else by simp [cpow_add, hx, pow_add, cpow_nat_cast n] @[simp] lemma cpow_two (x : ℂ) : x ^ (2 : ℂ) = x ^ 2 := by { rw ← cpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] } @[simp, norm_cast] lemma cpow_int_cast (x : ℂ) : ∀ (n : ℤ), x ^ (n : ℂ) = x ^ n | (n : ℕ) := by simp | -[1+ n] := by rw zpow_neg_succ_of_nat; simp only [int.neg_succ_of_nat_coe, int.cast_neg, complex.cpow_neg, inv_eq_one_div, int.cast_coe_nat, cpow_nat_cast] lemma cpow_nat_inv_pow (x : ℂ) {n : ℕ} (hn : n ≠ 0) : (x ^ (n⁻¹ : ℂ)) ^ n = x := begin suffices : im (log x * n⁻¹) ∈ Ioc (-π) π, { rw [← cpow_nat_cast, ← cpow_mul _ this.1 this.2, inv_mul_cancel, cpow_one], exact_mod_cast hn }, rw [mul_comm, ← of_real_nat_cast, ← of_real_inv, of_real_mul_im, ← div_eq_inv_mul], rw [← pos_iff_ne_zero] at hn, have hn' : 0 < (n : ℝ), by assumption_mod_cast, have hn1 : 1 ≤ (n : ℝ), by exact_mod_cast (nat.succ_le_iff.2 hn), split, { rw lt_div_iff hn', calc -π * n ≤ -π * 1 : mul_le_mul_of_nonpos_left hn1 (neg_nonpos.2 real.pi_pos.le) ... = -π : mul_one _ ... < im (log x) : neg_pi_lt_log_im _ }, { rw div_le_iff hn', calc im (log x) ≤ π : log_im_le_pi _ ... = π * 1 : (mul_one π).symm ... ≤ π * n : mul_le_mul_of_nonneg_left hn1 real.pi_pos.le } end end complex section lim open complex variables {α : Type*} lemma zero_cpow_eq_nhds {b : ℂ} (hb : b ≠ 0) : (0 : ℂ).cpow =ᶠ[𝓝 b] 0 := begin suffices : ∀ᶠ (x : ℂ) in (𝓝 b), x ≠ 0, from this.mono (λ x hx, by rw [cpow_eq_pow, zero_cpow hx, pi.zero_apply]), exact is_open.eventually_mem is_open_ne hb, end lemma cpow_eq_nhds {a b : ℂ} (ha : a ≠ 0) : (λ x, x.cpow b) =ᶠ[𝓝 a] λ x, exp (log x * b) := begin suffices : ∀ᶠ (x : ℂ) in (𝓝 a), x ≠ 0, from this.mono (λ x hx, by { dsimp only, rw [cpow_eq_pow, cpow_def_of_ne_zero hx], }), exact is_open.eventually_mem is_open_ne ha, end lemma cpow_eq_nhds' {p : ℂ × ℂ} (hp_fst : p.fst ≠ 0) : (λ x, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) := begin suffices : ∀ᶠ (x : ℂ × ℂ) in (𝓝 p), x.1 ≠ 0, from this.mono (λ x hx, by { dsimp only, rw cpow_def_of_ne_zero hx, }), refine is_open.eventually_mem _ hp_fst, change is_open {x : ℂ × ℂ | x.1 = 0}ᶜ, rw is_open_compl_iff, exact is_closed_eq continuous_fst continuous_const, end lemma continuous_at_const_cpow {a b : ℂ} (ha : a ≠ 0) : continuous_at (cpow a) b := begin have cpow_eq : cpow a = λ b, exp (log a * b), by { ext1 b, rw [cpow_eq_pow, cpow_def_of_ne_zero ha], }, rw cpow_eq, exact continuous_exp.continuous_at.comp (continuous_at.mul continuous_at_const continuous_at_id), end lemma continuous_at_const_cpow' {a b : ℂ} (h : b ≠ 0) : continuous_at (cpow a) b := begin by_cases ha : a = 0, { rw [ha, continuous_at_congr (zero_cpow_eq_nhds h)], exact continuous_at_const, }, { exact continuous_at_const_cpow ha, }, end /-- The function `z ^ w` is continuous in `(z, w)` provided that `z` does not belong to the interval `(-∞, 0]` on the real line. See also `complex.continuous_at_cpow_zero_of_re_pos` for a version that works for `z = 0` but assumes `0 < re w`. -/ lemma continuous_at_cpow {p : ℂ × ℂ} (hp_fst : 0 < p.fst.re ∨ p.fst.im ≠ 0) : continuous_at (λ x : ℂ × ℂ, x.1 ^ x.2) p := begin have hp_fst_ne_zero : p.fst ≠ 0, by { intro h, cases hp_fst; { rw h at hp_fst, simpa using hp_fst, }, }, rw continuous_at_congr (cpow_eq_nhds' hp_fst_ne_zero), refine continuous_exp.continuous_at.comp _, refine continuous_at.mul (continuous_at.comp _ continuous_fst.continuous_at) continuous_snd.continuous_at, exact continuous_at_clog hp_fst, end lemma continuous_at_cpow_const {a b : ℂ} (ha : 0 < a.re ∨ a.im ≠ 0) : continuous_at (λ x, cpow x b) a := tendsto.comp (@continuous_at_cpow (a, b) ha) (continuous_at_id.prod continuous_at_const) lemma filter.tendsto.cpow {l : filter α} {f g : α → ℂ} {a b : ℂ} (hf : tendsto f l (𝓝 a)) (hg : tendsto g l (𝓝 b)) (ha : 0 < a.re ∨ a.im ≠ 0) : tendsto (λ x, f x ^ g x) l (𝓝 (a ^ b)) := (@continuous_at_cpow (a,b) ha).tendsto.comp (hf.prod_mk_nhds hg) lemma filter.tendsto.const_cpow {l : filter α} {f : α → ℂ} {a b : ℂ} (hf : tendsto f l (𝓝 b)) (h : a ≠ 0 ∨ b ≠ 0) : tendsto (λ x, a ^ f x) l (𝓝 (a ^ b)) := begin cases h, { exact (continuous_at_const_cpow h).tendsto.comp hf, }, { exact (continuous_at_const_cpow' h).tendsto.comp hf, }, end variables [topological_space α] {f g : α → ℂ} {s : set α} {a : α} lemma continuous_within_at.cpow (hf : continuous_within_at f s a) (hg : continuous_within_at g s a) (h0 : 0 < (f a).re ∨ (f a).im ≠ 0) : continuous_within_at (λ x, f x ^ g x) s a := hf.cpow hg h0 lemma continuous_within_at.const_cpow {b : ℂ} (hf : continuous_within_at f s a) (h : b ≠ 0 ∨ f a ≠ 0) : continuous_within_at (λ x, b ^ f x) s a := hf.const_cpow h lemma continuous_at.cpow (hf : continuous_at f a) (hg : continuous_at g a) (h0 : 0 < (f a).re ∨ (f a).im ≠ 0) : continuous_at (λ x, f x ^ g x) a := hf.cpow hg h0 lemma continuous_at.const_cpow {b : ℂ} (hf : continuous_at f a) (h : b ≠ 0 ∨ f a ≠ 0) : continuous_at (λ x, b ^ f x) a := hf.const_cpow h lemma continuous_on.cpow (hf : continuous_on f s) (hg : continuous_on g s) (h0 : ∀ a ∈ s, 0 < (f a).re ∨ (f a).im ≠ 0) : continuous_on (λ x, f x ^ g x) s := λ a ha, (hf a ha).cpow (hg a ha) (h0 a ha) lemma continuous_on.const_cpow {b : ℂ} (hf : continuous_on f s) (h : b ≠ 0 ∨ ∀ a ∈ s, f a ≠ 0) : continuous_on (λ x, b ^ f x) s := λ a ha, (hf a ha).const_cpow (h.imp id $ λ h, h a ha) lemma continuous.cpow (hf : continuous f) (hg : continuous g) (h0 : ∀ a, 0 < (f a).re ∨ (f a).im ≠ 0) : continuous (λ x, f x ^ g x) := continuous_iff_continuous_at.2 $ λ a, (hf.continuous_at.cpow hg.continuous_at (h0 a)) lemma continuous.const_cpow {b : ℂ} (hf : continuous f) (h : b ≠ 0 ∨ ∀ a, f a ≠ 0) : continuous (λ x, b ^ f x) := continuous_iff_continuous_at.2 $ λ a, (hf.continuous_at.const_cpow $ h.imp id $ λ h, h a) lemma continuous_on.cpow_const {b : ℂ} (hf : continuous_on f s) (h : ∀ (a : α), a ∈ s → 0 < (f a).re ∨ (f a).im ≠ 0) : continuous_on (λ x, (f x) ^ b) s := hf.cpow continuous_on_const h end lim namespace real /-- The real power function `x^y`, defined as the real part of the complex power function. For `x > 0`, it is equal to `exp(y log x)`. For `x = 0`, one sets `0^0=1` and `0^y=0` for `y ≠ 0`. For `x < 0`, the definition is somewhat arbitary as it depends on the choice of a complex determination of the logarithm. With our conventions, it is equal to `exp (y log x) cos (πy)`. -/ noncomputable def rpow (x y : ℝ) := ((x : ℂ) ^ (y : ℂ)).re noncomputable instance : has_pow ℝ ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x y : ℝ) : rpow x y = x ^ y := rfl lemma rpow_def (x y : ℝ) : x ^ y = ((x : ℂ) ^ (y : ℂ)).re := rfl lemma rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := by simp only [rpow_def, complex.cpow_def]; split_ifs; simp [*, (complex.of_real_log hx).symm, -complex.of_real_mul, -is_R_or_C.of_real_mul, (complex.of_real_mul _ _).symm, complex.exp_of_real_re] at * lemma rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) := by rw [rpow_def_of_nonneg (le_of_lt hx), if_neg (ne_of_gt hx)] lemma exp_mul (x y : ℝ) : exp (x * y) = (exp x) ^ y := by rw [rpow_def_of_pos (exp_pos _), log_exp] @[simp] lemma exp_one_rpow (x : ℝ) : exp 1 ^ x = exp x := by rw [←exp_mul, one_mul] lemma rpow_eq_zero_iff_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [rpow_def_of_nonneg hx], split_ifs; simp [*, exp_ne_zero] } open_locale real lemma rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log x * y) * cos (y * π) := begin rw [rpow_def, complex.cpow_def, if_neg], have : complex.log x * y = ↑(log(-x) * y) + ↑(y * π) * complex.I, { simp only [complex.log, abs_of_neg hx, complex.arg_of_real_of_neg hx, complex.abs_of_real, complex.of_real_mul], ring }, { rw [this, complex.exp_add_mul_I, ← complex.of_real_exp, ← complex.of_real_cos, ← complex.of_real_sin, mul_add, ← complex.of_real_mul, ← mul_assoc, ← complex.of_real_mul, complex.add_re, complex.of_real_re, complex.mul_re, complex.I_re, complex.of_real_im, real.log_neg_eq_log], ring }, { rw complex.of_real_eq_zero, exact ne_of_lt hx } end lemma rpow_def_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) * cos (y * π) := by split_ifs; simp [rpow_def, *]; exact rpow_def_of_neg (lt_of_le_of_ne hx h) _ lemma rpow_pos_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : 0 < x ^ y := by rw rpow_def_of_pos hx; apply exp_pos @[simp] lemma rpow_zero (x : ℝ) : x ^ (0 : ℝ) = 1 := by simp [rpow_def] @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ) ^ x = 0 := by simp [rpow_def, *] lemma zero_rpow_eq_iff {x : ℝ} {a : ℝ} : 0 ^ x = a ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := begin split, { intros hyp, simp only [rpow_def, complex.of_real_zero] at hyp, by_cases x = 0, { subst h, simp only [complex.one_re, complex.of_real_zero, complex.cpow_zero] at hyp, exact or.inr ⟨rfl, hyp.symm⟩}, { rw complex.zero_cpow (complex.of_real_ne_zero.mpr h) at hyp, exact or.inl ⟨h, hyp.symm⟩, }, }, { rintro (⟨h,rfl⟩|⟨rfl,rfl⟩), { exact zero_rpow h, }, { exact rpow_zero _, }, }, end lemma eq_zero_rpow_iff {x : ℝ} {a : ℝ} : a = 0 ^ x ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := by rw [←zero_rpow_eq_iff, eq_comm] @[simp] lemma rpow_one (x : ℝ) : x ^ (1 : ℝ) = x := by simp [rpow_def] @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ) ^ x = 1 := by simp [rpow_def] lemma zero_rpow_le_one (x : ℝ) : (0 : ℝ) ^ x ≤ 1 := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma zero_rpow_nonneg (x : ℝ) : 0 ≤ (0 : ℝ) ^ x := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma rpow_nonneg_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : 0 ≤ x ^ y := by rw [rpow_def_of_nonneg hx]; split_ifs; simp only [zero_le_one, le_refl, le_of_lt (exp_pos _)] lemma abs_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : |x ^ y| = |x| ^ y := begin have h_rpow_nonneg : 0 ≤ x ^ y, from real.rpow_nonneg_of_nonneg hx_nonneg _, rw [abs_eq_self.mpr hx_nonneg, abs_eq_self.mpr h_rpow_nonneg], end lemma abs_rpow_le_abs_rpow (x y : ℝ) : |x ^ y| ≤ |x| ^ y := begin cases le_or_lt 0 x with hx hx, { rw [abs_rpow_of_nonneg hx] }, { rw [abs_of_neg hx, rpow_def_of_neg hx, rpow_def_of_pos (neg_pos.2 hx), log_neg_eq_log, abs_mul, abs_of_pos (exp_pos _)], exact mul_le_of_le_one_right (exp_pos _).le (abs_cos_le_one _) } end lemma abs_rpow_le_exp_log_mul (x y : ℝ) : |x ^ y| ≤ exp (log x * y) := begin refine (abs_rpow_le_abs_rpow x y).trans _, by_cases hx : x = 0, { by_cases hy : y = 0; simp [hx, hy, zero_le_one] }, { rw [rpow_def_of_pos (abs_pos.2 hx), log_abs] } end lemma norm_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : ‖x ^ y‖ = ‖x‖ ^ y := by { simp_rw real.norm_eq_abs, exact abs_rpow_of_nonneg hx_nonneg, } end real namespace complex lemma of_real_cpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : ((x ^ y : ℝ) : ℂ) = (x : ℂ) ^ (y : ℂ) := by simp only [real.rpow_def_of_nonneg hx, complex.cpow_def, of_real_eq_zero]; split_ifs; simp [complex.of_real_log hx] lemma of_real_cpow_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℂ) : (x : ℂ) ^ y = ((-x) : ℂ) ^ y * exp (π * I * y) := begin rcases hx.eq_or_lt with rfl|hlt, { rcases eq_or_ne y 0 with rfl|hy; simp * }, have hne : (x : ℂ) ≠ 0, from of_real_ne_zero.mpr hlt.ne, rw [cpow_def_of_ne_zero hne, cpow_def_of_ne_zero (neg_ne_zero.2 hne), ← exp_add, ← add_mul, log, log, abs.map_neg, arg_of_real_of_neg hlt, ← of_real_neg, arg_of_real_of_nonneg (neg_nonneg.2 hx), of_real_zero, zero_mul, add_zero] end lemma abs_cpow_of_ne_zero {z : ℂ} (hz : z ≠ 0) (w : ℂ) : abs (z ^ w) = abs z ^ w.re / real.exp (arg z * im w) := by rw [cpow_def_of_ne_zero hz, abs_exp, mul_re, log_re, log_im, real.exp_sub, real.rpow_def_of_pos (abs.pos hz)] lemma abs_cpow_of_imp {z w : ℂ} (h : z = 0 → w.re = 0 → w = 0) : abs (z ^ w) = abs z ^ w.re / real.exp (arg z * im w) := begin rcases ne_or_eq z 0 with hz|rfl; [exact (abs_cpow_of_ne_zero hz w), rw map_zero], cases eq_or_ne w.re 0 with hw hw, { simp [hw, h rfl hw] }, { rw [real.zero_rpow hw, zero_div, zero_cpow, map_zero], exact ne_of_apply_ne re hw } end lemma abs_cpow_le (z w : ℂ) : abs (z ^ w) ≤ abs z ^ w.re / real.exp (arg z * im w) := begin rcases ne_or_eq z 0 with hz|rfl; [exact (abs_cpow_of_ne_zero hz w).le, rw map_zero], rcases eq_or_ne w 0 with rfl|hw, { simp }, rw [zero_cpow hw, map_zero], exact div_nonneg (real.rpow_nonneg_of_nonneg le_rfl _) (real.exp_pos _).le end section variables {α : Type*} {l : filter α} {f g : α → ℂ} open asymptotics lemma is_Theta_exp_arg_mul_im (hl : is_bounded_under (≤) l (λ x, |(g x).im|)) : (λ x, real.exp (arg (f x) * im (g x))) =Θ[l] (λ x, (1 : ℝ)) := begin rcases hl with ⟨b, hb⟩, refine real.is_Theta_exp_comp_one.2 ⟨π * b, _⟩, rw eventually_map at hb ⊢, refine hb.mono (λ x hx, _), erw [abs_mul], exact mul_le_mul (abs_arg_le_pi _) hx (abs_nonneg _) real.pi_pos.le end lemma is_O_cpow_rpow (hl : is_bounded_under (≤) l (λ x, |(g x).im|)) : (λ x, f x ^ g x) =O[l] (λ x, abs (f x) ^ (g x).re) := calc (λ x, f x ^ g x) =O[l] (λ x, abs (f x) ^ (g x).re / real.exp (arg (f x) * im (g x))) : is_O_of_le _ $ λ x, (abs_cpow_le _ _).trans (le_abs_self _) ... =Θ[l] (λ x, abs (f x) ^ (g x).re / (1 : ℝ)) : (is_Theta_refl _ _).div (is_Theta_exp_arg_mul_im hl) ... =ᶠ[l] (λ x, abs (f x) ^ (g x).re) : by simp only [of_real_one, div_one] lemma is_Theta_cpow_rpow (hl_im : is_bounded_under (≤) l (λ x, |(g x).im|)) (hl : ∀ᶠ x in l, f x = 0 → re (g x) = 0 → g x = 0): (λ x, f x ^ g x) =Θ[l] (λ x, abs (f x) ^ (g x).re) := calc (λ x, f x ^ g x) =Θ[l] (λ x, abs (f x) ^ (g x).re / real.exp (arg (f x) * im (g x))) : is_Theta_of_norm_eventually_eq' $ hl.mono $ λ x, abs_cpow_of_imp ... =Θ[l] (λ x, abs (f x) ^ (g x).re / (1 : ℝ)) : (is_Theta_refl _ _).div (is_Theta_exp_arg_mul_im hl_im) ... =ᶠ[l] (λ x, abs (f x) ^ (g x).re) : by simp only [of_real_one, div_one] lemma is_Theta_cpow_const_rpow {b : ℂ} (hl : b.re = 0 → b ≠ 0 → ∀ᶠ x in l, f x ≠ 0) : (λ x, f x ^ b) =Θ[l] (λ x, abs (f x) ^ b.re) := is_Theta_cpow_rpow is_bounded_under_const $ by simpa only [eventually_imp_distrib_right, ne.def, ← not_frequently, not_imp_not, imp.swap] using hl end @[simp] lemma abs_cpow_real (x : ℂ) (y : ℝ) : abs (x ^ (y : ℂ)) = x.abs ^ y := by rcases eq_or_ne x 0 with rfl|hx; [rcases eq_or_ne y 0 with rfl|hy, skip]; simp [*, abs_cpow_of_ne_zero] @[simp] lemma abs_cpow_inv_nat (x : ℂ) (n : ℕ) : abs (x ^ (n⁻¹ : ℂ)) = x.abs ^ (n⁻¹ : ℝ) := by rw ← abs_cpow_real; simp [-abs_cpow_real] lemma abs_cpow_eq_rpow_re_of_pos {x : ℝ} (hx : 0 < x) (y : ℂ) : abs (x ^ y) = x ^ y.re := by rw [abs_cpow_of_ne_zero (of_real_ne_zero.mpr hx.ne'), arg_of_real_of_nonneg hx.le, zero_mul, real.exp_zero, div_one, abs_of_nonneg hx.le] lemma abs_cpow_eq_rpow_re_of_nonneg {x : ℝ} (hx : 0 ≤ x) {y : ℂ} (hy : re y ≠ 0) : abs (x ^ y) = x ^ re y := begin rcases hx.eq_or_lt with rfl|hlt, { rw [of_real_zero, zero_cpow, map_zero, real.zero_rpow hy], exact ne_of_apply_ne re hy }, { exact abs_cpow_eq_rpow_re_of_pos hlt y } end end complex namespace real variables {x y z : ℝ} lemma rpow_add (hx : 0 < x) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := by simp only [rpow_def_of_pos hx, mul_add, exp_add] lemma rpow_add' (hx : 0 ≤ x) (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := begin rcases hx.eq_or_lt with rfl|pos, { rw [zero_rpow h, zero_eq_mul], have : y ≠ 0 ∨ z ≠ 0, from not_and_distrib.1 (λ ⟨hy, hz⟩, h $ hy.symm ▸ hz.symm ▸ zero_add 0), exact this.imp zero_rpow zero_rpow }, { exact rpow_add pos _ _ } end lemma rpow_add_of_nonneg (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 ≤ z) : x ^ (y + z) = x ^ y * x ^ z := begin rcases hy.eq_or_lt with rfl|hy, { rw [zero_add, rpow_zero, one_mul] }, exact rpow_add' hx (ne_of_gt $ add_pos_of_pos_of_nonneg hy hz) end /-- For `0 ≤ x`, the only problematic case in the equality `x ^ y * x ^ z = x ^ (y + z)` is for `x = 0` and `y + z = 0`, where the right hand side is `1` while the left hand side can vanish. The inequality is always true, though, and given in this lemma. -/ lemma le_rpow_add {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ y * x ^ z ≤ x ^ (y + z) := begin rcases le_iff_eq_or_lt.1 hx with H|pos, { by_cases h : y + z = 0, { simp only [H.symm, h, rpow_zero], calc (0 : ℝ) ^ y * 0 ^ z ≤ 1 * 1 : mul_le_mul (zero_rpow_le_one y) (zero_rpow_le_one z) (zero_rpow_nonneg z) zero_le_one ... = 1 : by simp }, { simp [rpow_add', ← H, h] } }, { simp [rpow_add pos] } end lemma rpow_sum_of_pos {ι : Type*} {a : ℝ} (ha : 0 < a) (f : ι → ℝ) (s : finset ι) : a ^ (∑ x in s, f x) = ∏ x in s, a ^ f x := @add_monoid_hom.map_sum ℝ ι (additive ℝ) _ _ ⟨λ x : ℝ, (a ^ x : ℝ), rpow_zero a, rpow_add ha⟩ f s lemma rpow_sum_of_nonneg {ι : Type*} {a : ℝ} (ha : 0 ≤ a) {s : finset ι} {f : ι → ℝ} (h : ∀ x ∈ s, 0 ≤ f x) : a ^ (∑ x in s, f x) = ∏ x in s, a ^ f x := begin induction s using finset.cons_induction with i s hi ihs, { rw [sum_empty, finset.prod_empty, rpow_zero] }, { rw forall_mem_cons at h, rw [sum_cons, prod_cons, ← ihs h.2, rpow_add_of_nonneg ha h.1 (sum_nonneg h.2)] } end lemma rpow_mul {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := by rw [← complex.of_real_inj, complex.of_real_cpow (rpow_nonneg_of_nonneg hx _), complex.of_real_cpow hx, complex.of_real_mul, complex.cpow_mul, complex.of_real_cpow hx]; simp only [(complex.of_real_mul _ _).symm, (complex.of_real_log hx).symm, complex.of_real_im, neg_lt_zero, pi_pos, le_of_lt pi_pos] lemma rpow_neg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := by simp only [rpow_def_of_nonneg hx]; split_ifs; simp [*, exp_neg] at * lemma rpow_sub {x : ℝ} (hx : 0 < x) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := by simp only [sub_eq_add_neg, rpow_add hx, rpow_neg (le_of_lt hx), div_eq_mul_inv] lemma rpow_sub' {x : ℝ} (hx : 0 ≤ x) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := by { simp only [sub_eq_add_neg] at h ⊢, simp only [rpow_add' hx h, rpow_neg hx, div_eq_mul_inv] } lemma rpow_add_int {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y + n) = x ^ y * x ^ n := by rw [rpow_def, complex.of_real_add, complex.cpow_add _ _ (complex.of_real_ne_zero.mpr hx), complex.of_real_int_cast, complex.cpow_int_cast, ← complex.of_real_zpow, mul_comm, complex.of_real_mul_re, ← rpow_def, mul_comm] lemma rpow_add_nat {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y + n) = x ^ y * x ^ n := rpow_add_int hx y n lemma rpow_sub_int {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y - n) = x ^ y / x ^ n := by simpa using rpow_add_int hx y (-n) lemma rpow_sub_nat {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y - n) = x ^ y / x ^ n := rpow_sub_int hx y n lemma rpow_add_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y + 1) = x ^ y * x := by simpa using rpow_add_nat hx y 1 lemma rpow_sub_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y - 1) = x ^ y / x := by simpa using rpow_sub_nat hx y 1 @[simp, norm_cast] lemma rpow_int_cast (x : ℝ) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, ← complex.of_real_zpow, complex.cpow_int_cast, complex.of_real_int_cast, complex.of_real_re] @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ) (n : ℕ) : x ^ (n : ℝ) = x ^ n := rpow_int_cast x n @[simp] lemma rpow_two (x : ℝ) : x ^ (2 : ℝ) = x ^ 2 := by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] } lemma rpow_neg_one (x : ℝ) : x ^ (-1 : ℝ) = x⁻¹ := begin suffices H : x ^ ((-1 : ℤ) : ℝ) = x⁻¹, by rwa [int.cast_neg, int.cast_one] at H, simp only [rpow_int_cast, zpow_one, zpow_neg], end lemma mul_rpow {x y z : ℝ} (h : 0 ≤ x) (h₁ : 0 ≤ y) : (x*y)^z = x^z * y^z := begin iterate 3 { rw real.rpow_def_of_nonneg }, split_ifs; simp * at *, { have hx : 0 < x, { cases lt_or_eq_of_le h with h₂ h₂, { exact h₂ }, exfalso, apply h_2, exact eq.symm h₂ }, have hy : 0 < y, { cases lt_or_eq_of_le h₁ with h₂ h₂, { exact h₂ }, exfalso, apply h_3, exact eq.symm h₂ }, rw [log_mul (ne_of_gt hx) (ne_of_gt hy), add_mul, exp_add]}, { exact h₁ }, { exact h }, { exact mul_nonneg h h₁ }, end lemma inv_rpow (hx : 0 ≤ x) (y : ℝ) : (x⁻¹)^y = (x^y)⁻¹ := by simp only [← rpow_neg_one, ← rpow_mul hx, mul_comm] lemma div_rpow (hx : 0 ≤ x) (hy : 0 ≤ y) (z : ℝ) : (x / y) ^ z = x^z / y^z := by simp only [div_eq_mul_inv, mul_rpow hx (inv_nonneg.2 hy), inv_rpow hy] lemma log_rpow {x : ℝ} (hx : 0 < x) (y : ℝ) : log (x^y) = y * (log x) := begin apply exp_injective, rw [exp_log (rpow_pos_of_pos hx y), ← exp_log hx, mul_comm, rpow_def_of_pos (exp_pos (log x)) y], end lemma rpow_lt_rpow (hx : 0 ≤ x) (hxy : x < y) (hz : 0 < z) : x^z < y^z := begin rw le_iff_eq_or_lt at hx, cases hx, { rw [← hx, zero_rpow (ne_of_gt hz)], exact rpow_pos_of_pos (by rwa ← hx at hxy) _ }, rw [rpow_def_of_pos hx, rpow_def_of_pos (lt_trans hx hxy), exp_lt_exp], exact mul_lt_mul_of_pos_right (log_lt_log hx hxy) hz end lemma rpow_le_rpow {x y z: ℝ} (h : 0 ≤ x) (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := begin rcases eq_or_lt_of_le h₁ with rfl|h₁', { refl }, rcases eq_or_lt_of_le h₂ with rfl|h₂', { simp }, exact le_of_lt (rpow_lt_rpow h h₁' h₂') end lemma rpow_lt_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := ⟨lt_imp_lt_of_le_imp_le $ λ h, rpow_le_rpow hy h (le_of_lt hz), λ h, rpow_lt_rpow hx h hz⟩ lemma rpow_le_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := le_iff_le_iff_lt_iff_lt.2 $ rpow_lt_rpow_iff hy hx hz lemma le_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ≤ y ^ z⁻¹ ↔ y ≤ x ^ z := begin have hz' : 0 < -z := by rwa [lt_neg, neg_zero], have hxz : 0 < x ^ (-z) := real.rpow_pos_of_pos hx _, have hyz : 0 < y ^ z⁻¹ := real.rpow_pos_of_pos hy _, rw [←real.rpow_le_rpow_iff hx.le hyz.le hz', ←real.rpow_mul hy.le], simp only [ne_of_lt hz, real.rpow_neg_one, mul_neg, inv_mul_cancel, ne.def, not_false_iff], rw [le_inv hxz hy, ←real.rpow_neg_one, ←real.rpow_mul hx.le], simp, end lemma lt_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x < y ^ z⁻¹ ↔ y < x ^ z := begin have hz' : 0 < -z := by rwa [lt_neg, neg_zero], have hxz : 0 < x ^ (-z) := real.rpow_pos_of_pos hx _, have hyz : 0 < y ^ z⁻¹ := real.rpow_pos_of_pos hy _, rw [←real.rpow_lt_rpow_iff hx.le hyz.le hz', ←real.rpow_mul hy.le], simp only [ne_of_lt hz, real.rpow_neg_one, mul_neg, inv_mul_cancel, ne.def, not_false_iff], rw [lt_inv hxz hy, ←real.rpow_neg_one, ←real.rpow_mul hx.le], simp, end lemma rpow_inv_lt_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ < y ↔ y ^ z < x := begin convert lt_rpow_inv_iff_of_neg (real.rpow_pos_of_pos hx _) (real.rpow_pos_of_pos hy _) hz; simp [←real.rpow_mul hx.le, ←real.rpow_mul hy.le, ne_of_lt hz], end lemma rpow_inv_le_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ ≤ y ↔ y ^ z ≤ x := begin convert le_rpow_inv_iff_of_neg (real.rpow_pos_of_pos hx _) (real.rpow_pos_of_pos hy _) hz; simp [←real.rpow_mul hx.le, ←real.rpow_mul hy.le, ne_of_lt hz], end lemma rpow_lt_rpow_of_exponent_lt (hx : 1 < x) (hyz : y < z) : x^y < x^z := begin repeat {rw [rpow_def_of_pos (lt_trans zero_lt_one hx)]}, rw exp_lt_exp, exact mul_lt_mul_of_pos_left hyz (log_pos hx), end lemma rpow_le_rpow_of_exponent_le (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos (lt_of_lt_of_le zero_lt_one hx)]}, rw exp_le_exp, exact mul_le_mul_of_nonneg_left hyz (log_nonneg hx), end @[simp] lemma rpow_le_rpow_left_iff (hx : 1 < x) : x ^ y ≤ x ^ z ↔ y ≤ z := begin have x_pos : 0 < x := lt_trans zero_lt_one hx, rw [←log_le_log (rpow_pos_of_pos x_pos y) (rpow_pos_of_pos x_pos z), log_rpow x_pos, log_rpow x_pos, mul_le_mul_right (log_pos hx)], end @[simp] lemma rpow_lt_rpow_left_iff (hx : 1 < x) : x ^ y < x ^ z ↔ y < z := by rw [lt_iff_not_le, rpow_le_rpow_left_iff hx, lt_iff_not_le] lemma rpow_lt_rpow_of_exponent_gt (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_lt_exp, exact mul_lt_mul_of_neg_left hyz (log_neg hx0 hx1), end lemma rpow_le_rpow_of_exponent_ge (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_le_exp, exact mul_le_mul_of_nonpos_left hyz (log_nonpos (le_of_lt hx0) hx1), end @[simp] lemma rpow_le_rpow_left_iff_of_base_lt_one (hx0 : 0 < x) (hx1 : x < 1) : x ^ y ≤ x ^ z ↔ z ≤ y := begin rw [←log_le_log (rpow_pos_of_pos hx0 y) (rpow_pos_of_pos hx0 z), log_rpow hx0, log_rpow hx0, mul_le_mul_right_of_neg (log_neg hx0 hx1)], end @[simp] lemma rpow_lt_rpow_left_iff_of_base_lt_one (hx0 : 0 < x) (hx1 : x < 1) : x ^ y < x ^ z ↔ z < y := by rw [lt_iff_not_le, rpow_le_rpow_left_iff_of_base_lt_one hx0 hx1, lt_iff_not_le] lemma rpow_lt_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x < 1) (hz : 0 < z) : x^z < 1 := by { rw ← one_rpow z, exact rpow_lt_rpow hx1 hx2 hz } lemma rpow_le_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := by { rw ← one_rpow z, exact rpow_le_rpow hx1 hx2 hz } lemma rpow_lt_one_of_one_lt_of_neg {x z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := by { convert rpow_lt_rpow_of_exponent_lt hx hz, exact (rpow_zero x).symm } lemma rpow_le_one_of_one_le_of_nonpos {x z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := by { convert rpow_le_rpow_of_exponent_le hx hz, exact (rpow_zero x).symm } lemma one_lt_rpow {x z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := by { rw ← one_rpow z, exact rpow_lt_rpow zero_le_one hx hz } lemma one_le_rpow {x z : ℝ} (hx : 1 ≤ x) (hz : 0 ≤ z) : 1 ≤ x^z := by { rw ← one_rpow z, exact rpow_le_rpow zero_le_one hx hz } lemma one_lt_rpow_of_pos_of_lt_one_of_neg (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := by { convert rpow_lt_rpow_of_exponent_gt hx1 hx2 hz, exact (rpow_zero x).symm } lemma one_le_rpow_of_pos_of_le_one_of_nonpos (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := by { convert rpow_le_rpow_of_exponent_ge hx1 hx2 hz, exact (rpow_zero x).symm } lemma rpow_lt_one_iff_of_pos (hx : 0 < x) : x ^ y < 1 ↔ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := by rw [rpow_def_of_pos hx, exp_lt_one_iff, mul_neg_iff, log_pos_iff hx, log_neg_iff hx] lemma rpow_lt_one_iff (hx : 0 ≤ x) : x ^ y < 1 ↔ x = 0 ∧ y ≠ 0 ∨ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := begin rcases hx.eq_or_lt with (rfl|hx), { rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, zero_lt_one] }, { simp [rpow_lt_one_iff_of_pos hx, hx.ne.symm] } end lemma one_lt_rpow_iff_of_pos (hx : 0 < x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ x < 1 ∧ y < 0 := by rw [rpow_def_of_pos hx, one_lt_exp_iff, mul_pos_iff, log_pos_iff hx, log_neg_iff hx] lemma one_lt_rpow_iff (hx : 0 ≤ x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ 0 < x ∧ x < 1 ∧ y < 0 := begin rcases hx.eq_or_lt with (rfl|hx), { rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, (zero_lt_one' ℝ).not_lt] }, { simp [one_lt_rpow_iff_of_pos hx, hx] } end lemma rpow_le_rpow_of_exponent_ge' (hx0 : 0 ≤ x) (hx1 : x ≤ 1) (hz : 0 ≤ z) (hyz : z ≤ y) : x^y ≤ x^z := begin rcases eq_or_lt_of_le hx0 with rfl | hx0', { rcases eq_or_lt_of_le hz with rfl | hz', { exact (rpow_zero 0).symm ▸ (rpow_le_one hx0 hx1 hyz), }, rw [zero_rpow, zero_rpow]; linarith, }, { exact rpow_le_rpow_of_exponent_ge hx0' hx1 hyz, }, end lemma rpow_left_inj_on {x : ℝ} (hx : x ≠ 0) : inj_on (λ y : ℝ, y^x) {y : ℝ | 0 ≤ y} := begin rintros y hy z hz (hyz : y ^ x = z ^ x), rw [←rpow_one y, ←rpow_one z, ←_root_.mul_inv_cancel hx, rpow_mul hy, rpow_mul hz, hyz] end lemma le_rpow_iff_log_le (hx : 0 < x) (hy : 0 < y) : x ≤ y^z ↔ real.log x ≤ z * real.log y := by rw [←real.log_le_log hx (real.rpow_pos_of_pos hy z), real.log_rpow hy] lemma le_rpow_of_log_le (hx : 0 ≤ x) (hy : 0 < y) (h : real.log x ≤ z * real.log y) : x ≤ y^z := begin obtain hx | rfl := hx.lt_or_eq, { exact (le_rpow_iff_log_le hx hy).2 h }, exact (real.rpow_pos_of_pos hy z).le, end lemma lt_rpow_iff_log_lt (hx : 0 < x) (hy : 0 < y) : x < y^z ↔ real.log x < z * real.log y := by rw [←real.log_lt_log_iff hx (real.rpow_pos_of_pos hy z), real.log_rpow hy] lemma lt_rpow_of_log_lt (hx : 0 ≤ x) (hy : 0 < y) (h : real.log x < z * real.log y) : x < y^z := begin obtain hx | rfl := hx.lt_or_eq, { exact (lt_rpow_iff_log_lt hx hy).2 h }, exact real.rpow_pos_of_pos hy z, end lemma rpow_le_one_iff_of_pos (hx : 0 < x) : x ^ y ≤ 1 ↔ 1 ≤ x ∧ y ≤ 0 ∨ x ≤ 1 ∧ 0 ≤ y := by rw [rpow_def_of_pos hx, exp_le_one_iff, mul_nonpos_iff, log_nonneg_iff hx, log_nonpos_iff hx] /-- Bound for `|log x * x ^ t|` in the interval `(0, 1]`, for positive real `t`. -/ lemma abs_log_mul_self_rpow_lt (x t : ℝ) (h1 : 0 < x) (h2 : x ≤ 1) (ht : 0 < t) : |log x * x ^ t| < 1 / t := begin rw lt_div_iff ht, have := abs_log_mul_self_lt (x ^ t) (rpow_pos_of_pos h1 t) (rpow_le_one h1.le h2 ht.le), rwa [log_rpow h1, mul_assoc, abs_mul, abs_of_pos ht, mul_comm] at this end lemma pow_nat_rpow_nat_inv {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : n ≠ 0) : (x ^ n) ^ (n⁻¹ : ℝ) = x := have hn0 : (n : ℝ) ≠ 0, from nat.cast_ne_zero.2 hn, by rw [← rpow_nat_cast, ← rpow_mul hx, mul_inv_cancel hn0, rpow_one] lemma rpow_nat_inv_pow_nat {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : n ≠ 0) : (x ^ (n⁻¹ : ℝ)) ^ n = x := have hn0 : (n : ℝ) ≠ 0, from nat.cast_ne_zero.2 hn, by rw [← rpow_nat_cast, ← rpow_mul hx, inv_mul_cancel hn0, rpow_one] lemma continuous_at_const_rpow {a b : ℝ} (h : a ≠ 0) : continuous_at (rpow a) b := begin have : rpow a = λ x : ℝ, ((a : ℂ) ^ (x : ℂ)).re, by { ext1 x, rw [rpow_eq_pow, rpow_def], }, rw this, refine complex.continuous_re.continuous_at.comp _, refine (continuous_at_const_cpow _).comp complex.continuous_of_real.continuous_at, norm_cast, exact h, end lemma continuous_at_const_rpow' {a b : ℝ} (h : b ≠ 0) : continuous_at (rpow a) b := begin have : rpow a = λ x : ℝ, ((a : ℂ) ^ (x : ℂ)).re, by { ext1 x, rw [rpow_eq_pow, rpow_def], }, rw this, refine complex.continuous_re.continuous_at.comp _, refine (continuous_at_const_cpow' _).comp complex.continuous_of_real.continuous_at, norm_cast, exact h, end lemma rpow_eq_nhds_of_neg {p : ℝ × ℝ} (hp_fst : p.fst < 0) : (λ x : ℝ × ℝ, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) * cos (x.2 * π) := begin suffices : ∀ᶠ (x : ℝ × ℝ) in (𝓝 p), x.1 < 0, from this.mono (λ x hx, by { dsimp only, rw rpow_def_of_neg hx, }), exact is_open.eventually_mem (is_open_lt continuous_fst continuous_const) hp_fst, end lemma rpow_eq_nhds_of_pos {p : ℝ × ℝ} (hp_fst : 0 < p.fst) : (λ x : ℝ × ℝ, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) := begin suffices : ∀ᶠ (x : ℝ × ℝ) in (𝓝 p), 0 < x.1, from this.mono (λ x hx, by { dsimp only, rw rpow_def_of_pos hx, }), exact is_open.eventually_mem (is_open_lt continuous_const continuous_fst) hp_fst, end lemma continuous_at_rpow_of_ne (p : ℝ × ℝ) (hp : p.1 ≠ 0) : continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p := begin rw ne_iff_lt_or_gt at hp, cases hp, { rw continuous_at_congr (rpow_eq_nhds_of_neg hp), refine continuous_at.mul _ (continuous_cos.continuous_at.comp _), { refine continuous_exp.continuous_at.comp (continuous_at.mul _ continuous_snd.continuous_at), refine (continuous_at_log _).comp continuous_fst.continuous_at, exact hp.ne, }, { exact continuous_snd.continuous_at.mul continuous_at_const, }, }, { rw continuous_at_congr (rpow_eq_nhds_of_pos hp), refine continuous_exp.continuous_at.comp (continuous_at.mul _ continuous_snd.continuous_at), refine (continuous_at_log _).comp continuous_fst.continuous_at, exact hp.lt.ne.symm, }, end lemma continuous_at_rpow_of_pos (p : ℝ × ℝ) (hp : 0 < p.2) : continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p := begin cases p with x y, obtain hx|rfl := ne_or_eq x 0, { exact continuous_at_rpow_of_ne (x, y) hx }, have A : tendsto (λ p : ℝ × ℝ, exp (log p.1 * p.2)) (𝓝[≠] 0 ×ᶠ 𝓝 y) (𝓝 0) := tendsto_exp_at_bot.comp ((tendsto_log_nhds_within_zero.comp tendsto_fst).at_bot_mul hp tendsto_snd), have B : tendsto (λ p : ℝ × ℝ, p.1 ^ p.2) (𝓝[≠] 0 ×ᶠ 𝓝 y) (𝓝 0) := squeeze_zero_norm (λ p, abs_rpow_le_exp_log_mul p.1 p.2) A, have C : tendsto (λ p : ℝ × ℝ, p.1 ^ p.2) (𝓝[{0}] 0 ×ᶠ 𝓝 y) (pure 0), { rw [nhds_within_singleton, tendsto_pure, pure_prod, eventually_map], exact (lt_mem_nhds hp).mono (λ y hy, zero_rpow hy.ne') }, simpa only [← sup_prod, ← nhds_within_union, compl_union_self, nhds_within_univ, nhds_prod_eq, continuous_at, zero_rpow hp.ne'] using B.sup (C.mono_right (pure_le_nhds _)) end lemma continuous_at_rpow (p : ℝ × ℝ) (h : p.1 ≠ 0 ∨ 0 < p.2) : continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p := h.elim (λ h, continuous_at_rpow_of_ne p h) (λ h, continuous_at_rpow_of_pos p h) lemma continuous_at_rpow_const (x : ℝ) (q : ℝ) (h : x ≠ 0 ∨ 0 < q) : continuous_at (λ (x : ℝ), x ^ q) x := begin change continuous_at ((λ p : ℝ × ℝ, p.1 ^ p.2) ∘ (λ y : ℝ, (y, q))) x, apply continuous_at.comp, { exact continuous_at_rpow (x, q) h }, { exact (continuous_id'.prod_mk continuous_const).continuous_at } end end real section variable {α : Type*} lemma filter.tendsto.rpow {l : filter α} {f g : α → ℝ} {x y : ℝ} (hf : tendsto f l (𝓝 x)) (hg : tendsto g l (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) : tendsto (λ t, f t ^ g t) l (𝓝 (x ^ y)) := (real.continuous_at_rpow (x, y) h).tendsto.comp (hf.prod_mk_nhds hg) lemma filter.tendsto.rpow_const {l : filter α} {f : α → ℝ} {x p : ℝ} (hf : tendsto f l (𝓝 x)) (h : x ≠ 0 ∨ 0 ≤ p) : tendsto (λ a, f a ^ p) l (𝓝 (x ^ p)) := if h0 : 0 = p then h0 ▸ by simp [tendsto_const_nhds] else hf.rpow tendsto_const_nhds (h.imp id $ λ h', h'.lt_of_ne h0) variables [topological_space α] {f g : α → ℝ} {s : set α} {x : α} {p : ℝ} lemma continuous_at.rpow (hf : continuous_at f x) (hg : continuous_at g x) (h : f x ≠ 0 ∨ 0 < g x) : continuous_at (λ t, f t ^ g t) x := hf.rpow hg h lemma continuous_within_at.rpow (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) (h : f x ≠ 0 ∨ 0 < g x) : continuous_within_at (λ t, f t ^ g t) s x := hf.rpow hg h lemma continuous_on.rpow (hf : continuous_on f s) (hg : continuous_on g s) (h : ∀ x ∈ s, f x ≠ 0 ∨ 0 < g x) : continuous_on (λ t, f t ^ g t) s := λ t ht, (hf t ht).rpow (hg t ht) (h t ht) lemma continuous.rpow (hf : continuous f) (hg : continuous g) (h : ∀ x, f x ≠ 0 ∨ 0 < g x) : continuous (λ x, f x ^ g x) := continuous_iff_continuous_at.2 $ λ x, (hf.continuous_at.rpow hg.continuous_at (h x)) lemma continuous_within_at.rpow_const (hf : continuous_within_at f s x) (h : f x ≠ 0 ∨ 0 ≤ p) : continuous_within_at (λ x, f x ^ p) s x := hf.rpow_const h lemma continuous_at.rpow_const (hf : continuous_at f x) (h : f x ≠ 0 ∨ 0 ≤ p) : continuous_at (λ x, f x ^ p) x := hf.rpow_const h lemma continuous_on.rpow_const (hf : continuous_on f s) (h : ∀ x ∈ s, f x ≠ 0 ∨ 0 ≤ p) : continuous_on (λ x, f x ^ p) s := λ x hx, (hf x hx).rpow_const (h x hx) lemma continuous.rpow_const (hf : continuous f) (h : ∀ x, f x ≠ 0 ∨ 0 ≤ p) : continuous (λ x, f x ^ p) := continuous_iff_continuous_at.2 $ λ x, hf.continuous_at.rpow_const (h x) end namespace real variables {z x y : ℝ} section sqrt lemma sqrt_eq_rpow (x : ℝ) : sqrt x = x ^ (1/(2:ℝ)) := begin obtain h | h := le_or_lt 0 x, { rw [← mul_self_inj_of_nonneg (sqrt_nonneg _) (rpow_nonneg_of_nonneg h _), mul_self_sqrt h, ← sq, ← rpow_nat_cast, ← rpow_mul h], norm_num }, { have : 1 / (2:ℝ) * π = π / (2:ℝ), ring, rw [sqrt_eq_zero_of_nonpos h.le, rpow_def_of_neg h, this, cos_pi_div_two, mul_zero] } end lemma rpow_div_two_eq_sqrt {x : ℝ} (r : ℝ) (hx : 0 ≤ x) : x ^ (r/2) = (sqrt x) ^ r := begin rw [sqrt_eq_rpow, ← rpow_mul hx], congr, ring, end end sqrt end real section limits open real filter /-- The function `x ^ y` tends to `+∞` at `+∞` for any positive real `y`. -/ lemma tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ x : ℝ, x ^ y) at_top at_top := begin rw tendsto_at_top_at_top, intro b, use (max b 0) ^ (1/y), intros x hx, exact le_of_max_le_left (by { convert rpow_le_rpow (rpow_nonneg_of_nonneg (le_max_right b 0) (1/y)) hx (le_of_lt hy), rw [← rpow_mul (le_max_right b 0), (eq_div_iff (ne_of_gt hy)).mp rfl, rpow_one] }), end /-- The function `x ^ (-y)` tends to `0` at `+∞` for any positive real `y`. -/ lemma tendsto_rpow_neg_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ x : ℝ, x ^ (-y)) at_top (𝓝 0) := tendsto.congr' (eventually_eq_of_mem (Ioi_mem_at_top 0) (λ x hx, (rpow_neg (le_of_lt hx) y).symm)) (tendsto_rpow_at_top hy).inv_tendsto_at_top /-- The function `x ^ (a / (b * x + c))` tends to `1` at `+∞`, for any real numbers `a`, `b`, and `c` such that `b` is nonzero. -/ lemma tendsto_rpow_div_mul_add (a b c : ℝ) (hb : 0 ≠ b) : tendsto (λ x, x ^ (a / (b*x+c))) at_top (𝓝 1) := begin refine tendsto.congr' _ ((tendsto_exp_nhds_0_nhds_1.comp (by simpa only [mul_zero, pow_one] using ((@tendsto_const_nhds _ _ _ a _).mul (tendsto_div_pow_mul_exp_add_at_top b c 1 hb)))).comp tendsto_log_at_top), apply eventually_eq_of_mem (Ioi_mem_at_top (0:ℝ)), intros x hx, simp only [set.mem_Ioi, function.comp_app] at hx ⊢, rw [exp_log hx, ← exp_log (rpow_pos_of_pos hx (a / (b * x + c))), log_rpow hx (a / (b * x + c))], field_simp, end /-- The function `x ^ (1 / x)` tends to `1` at `+∞`. -/ lemma tendsto_rpow_div : tendsto (λ x, x ^ ((1:ℝ) / x)) at_top (𝓝 1) := by { convert tendsto_rpow_div_mul_add (1:ℝ) _ (0:ℝ) zero_ne_one, funext, congr' 2, ring } /-- The function `x ^ (-1 / x)` tends to `1` at `+∞`. -/ lemma tendsto_rpow_neg_div : tendsto (λ x, x ^ (-(1:ℝ) / x)) at_top (𝓝 1) := by { convert tendsto_rpow_div_mul_add (-(1:ℝ)) _ (0:ℝ) zero_ne_one, funext, congr' 2, ring } /-- The function `exp(x) / x ^ s` tends to `+∞` at `+∞`, for any real number `s`. -/ lemma tendsto_exp_div_rpow_at_top (s : ℝ) : tendsto (λ x : ℝ, exp x / x ^ s) at_top at_top := begin cases archimedean_iff_nat_lt.1 (real.archimedean) s with n hn, refine tendsto_at_top_mono' _ _ (tendsto_exp_div_pow_at_top n), filter_upwards [eventually_gt_at_top (0 : ℝ), eventually_ge_at_top (1 : ℝ)] with x hx₀ hx₁, rw [div_le_div_left (exp_pos _) (pow_pos hx₀ _) (rpow_pos_of_pos hx₀ _), ←rpow_nat_cast], exact rpow_le_rpow_of_exponent_le hx₁ hn.le, end /-- The function `exp (b * x) / x ^ s` tends to `+∞` at `+∞`, for any real `s` and `b > 0`. -/ lemma tendsto_exp_mul_div_rpow_at_top (s : ℝ) (b : ℝ) (hb : 0 < b) : tendsto (λ x : ℝ, exp (b * x) / x ^ s) at_top at_top := begin refine ((tendsto_rpow_at_top hb).comp (tendsto_exp_div_rpow_at_top (s / b))).congr' _, filter_upwards [eventually_ge_at_top (0 : ℝ)] with x hx₀, simp [div_rpow, (exp_pos x).le, rpow_nonneg_of_nonneg, ←rpow_mul, ←exp_mul, mul_comm x, hb.ne', *] end /-- The function `x ^ s * exp (-b * x)` tends to `0` at `+∞`, for any real `s` and `b > 0`. -/ lemma tendsto_rpow_mul_exp_neg_mul_at_top_nhds_0 (s : ℝ) (b : ℝ) (hb : 0 < b): tendsto (λ x : ℝ, x ^ s * exp (-b * x)) at_top (𝓝 0) := begin refine (tendsto_exp_mul_div_rpow_at_top s b hb).inv_tendsto_at_top.congr' _, filter_upwards with x using by simp [exp_neg, inv_div, div_eq_mul_inv _ (exp _)] end namespace asymptotics variables {α : Type*} {r c : ℝ} {l : filter α} {f g : α → ℝ} lemma is_O_with.rpow (h : is_O_with c l f g) (hc : 0 ≤ c) (hr : 0 ≤ r) (hg : 0 ≤ᶠ[l] g) : is_O_with (c ^ r) l (λ x, f x ^ r) (λ x, g x ^ r) := begin apply is_O_with.of_bound, filter_upwards [hg, h.bound] with x hgx hx, calc |f x ^ r| ≤ |f x| ^ r : abs_rpow_le_abs_rpow _ _ ... ≤ (c * |g x|) ^ r : rpow_le_rpow (abs_nonneg _) hx hr ... = c ^ r * |g x ^ r| : by rw [mul_rpow hc (abs_nonneg _), abs_rpow_of_nonneg hgx] end lemma is_O.rpow (hr : 0 ≤ r) (hg : 0 ≤ᶠ[l] g) (h : f =O[l] g) : (λ x, f x ^ r) =O[l] (λ x, g x ^ r) := let ⟨c, hc, h'⟩ := h.exists_nonneg in (h'.rpow hc hr hg).is_O lemma is_o.rpow (hr : 0 < r) (hg : 0 ≤ᶠ[l] g) (h : f =o[l] g) : (λ x, f x ^ r) =o[l] (λ x, g x ^ r) := is_o.of_is_O_with $ λ c hc, ((h.forall_is_O_with (rpow_pos_of_pos hc r⁻¹)).rpow (rpow_nonneg_of_nonneg hc.le _) hr.le hg).congr_const (by rw [←rpow_mul hc.le, inv_mul_cancel hr.ne', rpow_one]) end asymptotics open asymptotics /-- `x ^ s = o(exp(b * x))` as `x → ∞` for any real `s` and positive `b`. -/ lemma is_o_rpow_exp_pos_mul_at_top (s : ℝ) {b : ℝ} (hb : 0 < b) : (λ x : ℝ, x ^ s) =o[at_top] (λ x, exp (b * x)) := iff.mpr (is_o_iff_tendsto $ λ x h, absurd h (exp_pos _).ne') $ by simpa only [div_eq_mul_inv, exp_neg, neg_mul] using tendsto_rpow_mul_exp_neg_mul_at_top_nhds_0 s b hb /-- `x ^ k = o(exp(b * x))` as `x → ∞` for any integer `k` and positive `b`. -/ lemma is_o_zpow_exp_pos_mul_at_top (k : ℤ) {b : ℝ} (hb : 0 < b) : (λ x : ℝ, x ^ k) =o[at_top] (λ x, exp (b * x)) := by simpa only [rpow_int_cast] using is_o_rpow_exp_pos_mul_at_top k hb /-- `x ^ k = o(exp(b * x))` as `x → ∞` for any natural `k` and positive `b`. -/ lemma is_o_pow_exp_pos_mul_at_top (k : ℕ) {b : ℝ} (hb : 0 < b) : (λ x : ℝ, x ^ k) =o[at_top] (λ x, exp (b * x)) := is_o_zpow_exp_pos_mul_at_top k hb /-- `x ^ s = o(exp x)` as `x → ∞` for any real `s`. -/ lemma is_o_rpow_exp_at_top (s : ℝ) : (λ x : ℝ, x ^ s) =o[at_top] exp := by simpa only [one_mul] using is_o_rpow_exp_pos_mul_at_top s one_pos lemma is_o_log_rpow_at_top {r : ℝ} (hr : 0 < r) : log =o[at_top] (λ x, x ^ r) := calc log =O[at_top] (λ x, r * log x) : is_O_self_const_mul _ hr.ne' _ _ ... =ᶠ[at_top] (λ x, log (x ^ r)) : (eventually_gt_at_top 0).mono $ λ x hx, (log_rpow hx _).symm ... =o[at_top] (λ x, x ^ r) : is_o_log_id_at_top.comp_tendsto (tendsto_rpow_at_top hr) lemma is_o_log_rpow_rpow_at_top {s : ℝ} (r : ℝ) (hs : 0 < s) : (λ x, log x ^ r) =o[at_top] (λ x, x ^ s) := let r' := max r 1 in have hr : 0 < r', from lt_max_iff.2 $ or.inr one_pos, have H : 0 < s / r', from div_pos hs hr, calc (λ x, log x ^ r) =O[at_top] (λ x, log x ^ r') : is_O.of_bound 1 $ (tendsto_log_at_top.eventually_ge_at_top 1).mono $ λ x hx, have hx₀ : 0 ≤ log x, from zero_le_one.trans hx, by simp [norm_eq_abs, abs_rpow_of_nonneg, abs_rpow_of_nonneg hx₀, rpow_le_rpow_of_exponent_le (hx.trans (le_abs_self _))] ... =o[at_top] (λ x, (x ^ (s / r')) ^ r') : (is_o_log_rpow_at_top H).rpow hr $ (tendsto_rpow_at_top H).eventually $ eventually_ge_at_top 0 ... =ᶠ[at_top] (λ x, x ^ s) : (eventually_ge_at_top 0).mono $ λ x hx, by simp only [← rpow_mul hx, div_mul_cancel _ hr.ne'] lemma is_o_abs_log_rpow_rpow_nhds_zero {s : ℝ} (r : ℝ) (hs : s < 0) : (λ x, |log x| ^ r) =o[𝓝[>] 0] (λ x, x ^ s) := ((is_o_log_rpow_rpow_at_top r (neg_pos.2 hs)).comp_tendsto tendsto_inv_zero_at_top).congr' (mem_of_superset (Icc_mem_nhds_within_Ioi $ set.left_mem_Ico.2 one_pos) $ λ x hx, by simp [abs_of_nonpos, log_nonpos hx.1 hx.2]) (eventually_mem_nhds_within.mono $ λ x hx, by rw [function.comp_app, inv_rpow hx.out.le, rpow_neg hx.out.le, inv_inv]) lemma is_o_log_rpow_nhds_zero {r : ℝ} (hr : r < 0) : log =o[𝓝[>] 0] (λ x, x ^ r) := (is_o_abs_log_rpow_rpow_nhds_zero 1 hr).neg_left.congr' (mem_of_superset (Icc_mem_nhds_within_Ioi $ set.left_mem_Ico.2 one_pos) $ λ x hx, by simp [abs_of_nonpos (log_nonpos hx.1 hx.2)]) eventually_eq.rfl lemma tendsto_log_div_rpow_nhds_zero {r : ℝ} (hr : r < 0) : tendsto (λ x, log x / x ^ r) (𝓝[>] 0) (𝓝 0) := (is_o_log_rpow_nhds_zero hr).tendsto_div_nhds_zero lemma tendsto_log_mul_rpow_nhds_zero {r : ℝ} (hr : 0 < r) : tendsto (λ x, log x * x ^ r) (𝓝[>] 0) (𝓝 0) := (tendsto_log_div_rpow_nhds_zero $ neg_lt_zero.2 hr).congr' $ eventually_mem_nhds_within.mono $ λ x hx, by rw [rpow_neg hx.out.le, div_inv_eq_mul] end limits namespace complex /-- See also `complex.continuous_at_cpow` and `complex.continuous_at_cpow_of_re_pos`. -/ lemma continuous_at_cpow_zero_of_re_pos {z : ℂ} (hz : 0 < z.re) : continuous_at (λ x : ℂ × ℂ, x.1 ^ x.2) (0, z) := begin have hz₀ : z ≠ 0, from ne_of_apply_ne re hz.ne', rw [continuous_at, zero_cpow hz₀, tendsto_zero_iff_norm_tendsto_zero], refine squeeze_zero (λ _, norm_nonneg _) (λ _, abs_cpow_le _ _) _, simp only [div_eq_mul_inv, ← real.exp_neg], refine tendsto.zero_mul_is_bounded_under_le _ _, { convert (continuous_fst.norm.tendsto _).rpow ((continuous_re.comp continuous_snd).tendsto _) _; simp [hz, real.zero_rpow hz.ne'] }, { simp only [(∘), real.norm_eq_abs, abs_of_pos (real.exp_pos _)], rcases exists_gt (|im z|) with ⟨C, hC⟩, refine ⟨real.exp (π * C), eventually_map.2 _⟩, refine (((continuous_im.comp continuous_snd).abs.tendsto (_, z)).eventually (gt_mem_nhds hC)).mono (λ z hz, real.exp_le_exp.2 $ (neg_le_abs_self _).trans _), rw _root_.abs_mul, exact mul_le_mul (abs_le.2 ⟨(neg_pi_lt_arg _).le, arg_le_pi _⟩) hz.le (_root_.abs_nonneg _) real.pi_pos.le } end /-- See also `complex.continuous_at_cpow` for a version that assumes `p.1 ≠ 0` but makes no assumptions about `p.2`. -/ lemma continuous_at_cpow_of_re_pos {p : ℂ × ℂ} (h₁ : 0 ≤ p.1.re ∨ p.1.im ≠ 0) (h₂ : 0 < p.2.re) : continuous_at (λ x : ℂ × ℂ, x.1 ^ x.2) p := begin cases p with z w, rw [← not_lt_zero_iff, lt_iff_le_and_ne, not_and_distrib, ne.def, not_not, not_le_zero_iff] at h₁, rcases h₁ with h₁|(rfl : z = 0), exacts [continuous_at_cpow h₁, continuous_at_cpow_zero_of_re_pos h₂] end /-- See also `complex.continuous_at_cpow_const` for a version that assumes `z ≠ 0` but makes no assumptions about `w`. -/ lemma continuous_at_cpow_const_of_re_pos {z w : ℂ} (hz : 0 ≤ re z ∨ im z ≠ 0) (hw : 0 < re w) : continuous_at (λ x, x ^ w) z := tendsto.comp (@continuous_at_cpow_of_re_pos (z, w) hz hw) (continuous_at_id.prod continuous_at_const) lemma continuous_of_real_cpow_const {y : ℂ} (hs : 0 < y.re) : continuous (λ x, x ^ y : ℝ → ℂ) := begin rw continuous_iff_continuous_at, intro x, cases le_or_lt 0 x with hx hx, { refine (continuous_at_cpow_const_of_re_pos _ hs).comp continuous_of_real.continuous_at, exact or.inl hx }, { suffices : continuous_on (λ x, x ^ y : ℝ → ℂ) (set.Iio 0), from continuous_on.continuous_at this (Iio_mem_nhds hx), have : eq_on (λ x, x ^ y : ℝ → ℂ) (λ x, ((-x) : ℂ) ^ y * exp (π * I * y)) (set.Iio 0), from λ y hy, of_real_cpow_of_nonpos (le_of_lt hy) _, refine (continuous_on.mul (λ y hy, _) continuous_on_const).congr this, refine continuous_of_real.continuous_within_at.neg.cpow continuous_within_at_const _, left, simpa using hy } end end complex namespace nnreal /-- The nonnegative real power function `x^y`, defined for `x : ℝ≥0` and `y : ℝ ` as the restriction of the real power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0 = 1` and `0 ^ y = 0` for `y ≠ 0`. -/ noncomputable def rpow (x : ℝ≥0) (y : ℝ) : ℝ≥0 := ⟨(x : ℝ) ^ y, real.rpow_nonneg_of_nonneg x.2 y⟩ noncomputable instance : has_pow ℝ≥0 ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ℝ≥0) (y : ℝ) : rpow x y = x ^ y := rfl @[simp, norm_cast] lemma coe_rpow (x : ℝ≥0) (y : ℝ) : ((x ^ y : ℝ≥0) : ℝ) = (x : ℝ) ^ y := rfl @[simp] lemma rpow_zero (x : ℝ≥0) : x ^ (0 : ℝ) = 1 := nnreal.eq $ real.rpow_zero _ @[simp] lemma rpow_eq_zero_iff {x : ℝ≥0} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := begin rw [← nnreal.coe_eq, coe_rpow, ← nnreal.coe_eq_zero], exact real.rpow_eq_zero_iff_of_nonneg x.2 end @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ≥0) ^ x = 0 := nnreal.eq $ real.zero_rpow h @[simp] lemma rpow_one (x : ℝ≥0) : x ^ (1 : ℝ) = x := nnreal.eq $ real.rpow_one _ @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0) ^ x = 1 := nnreal.eq $ real.one_rpow _ lemma rpow_add {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add (pos_iff_ne_zero.2 hx) _ _ lemma rpow_add' (x : ℝ≥0) {y z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add' x.2 h lemma rpow_mul (x : ℝ≥0) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := nnreal.eq $ real.rpow_mul x.2 y z lemma rpow_neg (x : ℝ≥0) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := nnreal.eq $ real.rpow_neg x.2 _ lemma rpow_neg_one (x : ℝ≥0) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_sub {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub (pos_iff_ne_zero.2 hx) y z lemma rpow_sub' (x : ℝ≥0) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub' x.2 h lemma rpow_inv_rpow_self {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ y) ^ (1 / y) = x := by field_simp [← rpow_mul] lemma rpow_self_rpow_inv {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ (1 / y)) ^ y = x := by field_simp [← rpow_mul] lemma inv_rpow (x : ℝ≥0) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ := nnreal.eq $ real.inv_rpow x.2 y lemma div_rpow (x y : ℝ≥0) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z := nnreal.eq $ real.div_rpow x.2 y.2 z lemma sqrt_eq_rpow (x : ℝ≥0) : sqrt x = x ^ (1/(2:ℝ)) := begin refine nnreal.eq _, push_cast, exact real.sqrt_eq_rpow x.1, end @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0) (n : ℕ) : x ^ (n : ℝ) = x ^ n := nnreal.eq $ by simpa only [coe_rpow, coe_pow] using real.rpow_nat_cast x n @[simp] lemma rpow_two (x : ℝ≥0) : x ^ (2 : ℝ) = x ^ 2 := by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] } lemma mul_rpow {x y : ℝ≥0} {z : ℝ} : (x*y)^z = x^z * y^z := nnreal.eq $ real.mul_rpow x.2 y.2 lemma rpow_le_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := real.rpow_le_rpow x.2 h₁ h₂ lemma rpow_lt_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := real.rpow_lt_rpow x.2 h₁ h₂ lemma rpow_lt_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := real.rpow_lt_rpow_iff x.2 y.2 hz lemma rpow_le_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := real.rpow_le_rpow_iff x.2 y.2 hz lemma le_rpow_one_div_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ≤ y ^ (1 / z) ↔ x ^ z ≤ y := by rw [← rpow_le_rpow_iff hz, rpow_self_rpow_inv hz.ne'] lemma rpow_one_div_le_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ (1 / z) ≤ y ↔ x ≤ y ^ z := by rw [← rpow_le_rpow_iff hz, rpow_self_rpow_inv hz.ne'] lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0} {y z : ℝ} (hx : 1 < x) (hyz : y < z) : x^y < x^z := real.rpow_lt_rpow_of_exponent_lt hx hyz lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_le hx hyz lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := real.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_ge hx0 hx1 hyz lemma rpow_pos {p : ℝ} {x : ℝ≥0} (hx_pos : 0 < x) : 0 < x^p := begin have rpow_pos_of_nonneg : ∀ {p : ℝ}, 0 < p → 0 < x^p, { intros p hp_pos, rw ←zero_rpow hp_pos.ne', exact rpow_lt_rpow hx_pos hp_pos }, rcases lt_trichotomy 0 p with hp_pos|rfl|hp_neg, { exact rpow_pos_of_nonneg hp_pos }, { simp only [zero_lt_one, rpow_zero] }, { rw [←neg_neg p, rpow_neg, inv_pos], exact rpow_pos_of_nonneg (neg_pos.mpr hp_neg) }, end lemma rpow_lt_one {x : ℝ≥0} {z : ℝ} (hx1 : x < 1) (hz : 0 < z) : x^z < 1 := real.rpow_lt_one (coe_nonneg x) hx1 hz lemma rpow_le_one {x : ℝ≥0} {z : ℝ} (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := real.rpow_le_one x.2 hx2 hz lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := real.rpow_lt_one_of_one_lt_of_neg hx hz lemma rpow_le_one_of_one_le_of_nonpos {x : ℝ≥0} {z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := real.rpow_le_one_of_one_le_of_nonpos hx hz lemma one_lt_rpow {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := real.one_lt_rpow hx hz lemma one_le_rpow {x : ℝ≥0} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z := real.one_le_rpow h h₁ lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := real.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz lemma one_le_rpow_of_pos_of_le_one_of_nonpos {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := real.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 hz lemma rpow_le_self_of_le_one {x : ℝ≥0} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x := begin rcases eq_bot_or_bot_lt x with rfl | (h : 0 < x), { have : z ≠ 0 := by linarith, simp [this] }, nth_rewrite 1 ←nnreal.rpow_one x, exact nnreal.rpow_le_rpow_of_exponent_ge h hx h_one_le, end lemma rpow_left_injective {x : ℝ} (hx : x ≠ 0) : function.injective (λ y : ℝ≥0, y^x) := λ y z hyz, by simpa only [rpow_inv_rpow_self hx] using congr_arg (λ y, y ^ (1 / x)) hyz lemma rpow_eq_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ z = y ^ z ↔ x = y := (rpow_left_injective hz).eq_iff lemma rpow_left_surjective {x : ℝ} (hx : x ≠ 0) : function.surjective (λ y : ℝ≥0, y^x) := λ y, ⟨y ^ x⁻¹, by simp_rw [←rpow_mul, _root_.inv_mul_cancel hx, rpow_one]⟩ lemma rpow_left_bijective {x : ℝ} (hx : x ≠ 0) : function.bijective (λ y : ℝ≥0, y^x) := ⟨rpow_left_injective hx, rpow_left_surjective hx⟩ lemma eq_rpow_one_div_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x = y ^ (1 / z) ↔ x ^ z = y := by rw [← rpow_eq_rpow_iff hz, rpow_self_rpow_inv hz] lemma rpow_one_div_eq_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ (1 / z) = y ↔ x = y ^ z := by rw [← rpow_eq_rpow_iff hz, rpow_self_rpow_inv hz] lemma pow_nat_rpow_nat_inv (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) : (x ^ n) ^ (n⁻¹ : ℝ) = x := by { rw [← nnreal.coe_eq, coe_rpow, nnreal.coe_pow], exact real.pow_nat_rpow_nat_inv x.2 hn } lemma rpow_nat_inv_pow_nat (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) : (x ^ (n⁻¹ : ℝ)) ^ n = x := by { rw [← nnreal.coe_eq, nnreal.coe_pow, coe_rpow], exact real.rpow_nat_inv_pow_nat x.2 hn } lemma continuous_at_rpow {x : ℝ≥0} {y : ℝ} (h : x ≠ 0 ∨ 0 < y) : continuous_at (λp:ℝ≥0×ℝ, p.1^p.2) (x, y) := begin have : (λp:ℝ≥0×ℝ, p.1^p.2) = real.to_nnreal ∘ (λp:ℝ×ℝ, p.1^p.2) ∘ (λp:ℝ≥0 × ℝ, (p.1.1, p.2)), { ext p, rw [coe_rpow, real.coe_to_nnreal _ (real.rpow_nonneg_of_nonneg p.1.2 _)], refl }, rw this, refine continuous_real_to_nnreal.continuous_at.comp (continuous_at.comp _ _), { apply real.continuous_at_rpow, simp only [ne.def] at h, rw ← (nnreal.coe_eq_zero x) at h, exact h }, { exact ((continuous_subtype_val.comp continuous_fst).prod_mk continuous_snd).continuous_at } end lemma _root_.real.to_nnreal_rpow_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : real.to_nnreal (x ^ y) = (real.to_nnreal x) ^ y := begin nth_rewrite 0 ← real.coe_to_nnreal x hx, rw [←nnreal.coe_rpow, real.to_nnreal_coe], end lemma eventually_pow_one_div_le (x : ℝ≥0) {y : ℝ≥0} (hy : 1 < y) : ∀ᶠ (n : ℕ) in at_top, x ^ (1 / n : ℝ) ≤ y := begin obtain ⟨m, hm⟩ := add_one_pow_unbounded_of_pos x (tsub_pos_of_lt hy), rw [tsub_add_cancel_of_le hy.le] at hm, refine eventually_at_top.2 ⟨m + 1, λ n hn, _⟩, simpa only [nnreal.rpow_one_div_le_iff (nat.cast_pos.2 $ m.succ_pos.trans_le hn), nnreal.rpow_nat_cast] using hm.le.trans (pow_le_pow hy.le (m.le_succ.trans hn)), end end nnreal namespace real variables {n : ℕ} lemma exists_rat_pow_btwn_rat_aux (hn : n ≠ 0) (x y : ℝ) (h : x < y) (hy : 0 < y) : ∃ q : ℚ, 0 < q ∧ x < q^n ∧ ↑q^n < y := begin have hn' : 0 < (n : ℝ) := by exact_mod_cast hn.bot_lt, obtain ⟨q, hxq, hqy⟩ := exists_rat_btwn (rpow_lt_rpow (le_max_left 0 x) (max_lt hy h) $ inv_pos.mpr hn'), have := rpow_nonneg_of_nonneg (le_max_left 0 x) n⁻¹, have hq := this.trans_lt hxq, replace hxq := rpow_lt_rpow this hxq hn', replace hqy := rpow_lt_rpow hq.le hqy hn', rw [rpow_nat_cast, rpow_nat_cast, rpow_nat_inv_pow_nat _ hn] at hxq hqy, exact ⟨q, by exact_mod_cast hq, (le_max_right _ _).trans_lt hxq, hqy⟩, { exact le_max_left _ _ }, { exact hy.le } end lemma exists_rat_pow_btwn_rat (hn : n ≠ 0) {x y : ℚ} (h : x < y) (hy : 0 < y) : ∃ q : ℚ, 0 < q ∧ x < q^n ∧ q^n < y := by apply_mod_cast exists_rat_pow_btwn_rat_aux hn x y; assumption /-- There is a rational power between any two positive elements of an archimedean ordered field. -/ lemma exists_rat_pow_btwn {α : Type*} [linear_ordered_field α] [archimedean α] (hn : n ≠ 0) {x y : α} (h : x < y) (hy : 0 < y) : ∃ q : ℚ, 0 < q ∧ x < q^n ∧ (q^n : α) < y := begin obtain ⟨q₂, hx₂, hy₂⟩ := exists_rat_btwn (max_lt h hy), obtain ⟨q₁, hx₁, hq₁₂⟩ := exists_rat_btwn hx₂, have : (0 : α) < q₂ := (le_max_right _ _).trans_lt hx₂, norm_cast at hq₁₂ this, obtain ⟨q, hq, hq₁, hq₂⟩ := exists_rat_pow_btwn_rat hn hq₁₂ this, refine ⟨q, hq, (le_max_left _ _).trans_lt $ hx₁.trans _, hy₂.trans' _⟩; assumption_mod_cast, end end real open filter lemma filter.tendsto.nnrpow {α : Type*} {f : filter α} {u : α → ℝ≥0} {v : α → ℝ} {x : ℝ≥0} {y : ℝ} (hx : tendsto u f (𝓝 x)) (hy : tendsto v f (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) : tendsto (λ a, (u a) ^ (v a)) f (𝓝 (x ^ y)) := tendsto.comp (nnreal.continuous_at_rpow h) (hx.prod_mk_nhds hy) namespace nnreal lemma continuous_at_rpow_const {x : ℝ≥0} {y : ℝ} (h : x ≠ 0 ∨ 0 ≤ y) : continuous_at (λ z, z^y) x := h.elim (λ h, tendsto_id.nnrpow tendsto_const_nhds (or.inl h)) $ λ h, h.eq_or_lt.elim (λ h, h ▸ by simp only [rpow_zero, continuous_at_const]) (λ h, tendsto_id.nnrpow tendsto_const_nhds (or.inr h)) lemma continuous_rpow_const {y : ℝ} (h : 0 ≤ y) : continuous (λ x : ℝ≥0, x^y) := continuous_iff_continuous_at.2 $ λ x, continuous_at_rpow_const (or.inr h) theorem tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ (x : ℝ≥0), x ^ y) at_top at_top := begin rw filter.tendsto_at_top_at_top, intros b, obtain ⟨c, hc⟩ := tendsto_at_top_at_top.mp (tendsto_rpow_at_top hy) b, use c.to_nnreal, intros a ha, exact_mod_cast hc a (real.to_nnreal_le_iff_le_coe.mp ha), end end nnreal namespace ennreal /-- The real power function `x^y` on extended nonnegative reals, defined for `x : ℝ≥0∞` and `y : ℝ` as the restriction of the real power function if `0 < x < ⊤`, and with the natural values for `0` and `⊤` (i.e., `0 ^ x = 0` for `x > 0`, `1` for `x = 0` and `⊤` for `x < 0`, and `⊤ ^ x = 1 / 0 ^ x`). -/ noncomputable def rpow : ℝ≥0∞ → ℝ → ℝ≥0∞ | (some x) y := if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) | none y := if 0 < y then ⊤ else if y = 0 then 1 else 0 noncomputable instance : has_pow ℝ≥0∞ ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ℝ≥0∞) (y : ℝ) : rpow x y = x ^ y := rfl @[simp] lemma rpow_zero {x : ℝ≥0∞} : x ^ (0 : ℝ) = 1 := by cases x; { dsimp only [(^), rpow], simp [lt_irrefl] } lemma top_rpow_def (y : ℝ) : (⊤ : ℝ≥0∞) ^ y = if 0 < y then ⊤ else if y = 0 then 1 else 0 := rfl @[simp] lemma top_rpow_of_pos {y : ℝ} (h : 0 < y) : (⊤ : ℝ≥0∞) ^ y = ⊤ := by simp [top_rpow_def, h] @[simp] lemma top_rpow_of_neg {y : ℝ} (h : y < 0) : (⊤ : ℝ≥0∞) ^ y = 0 := by simp [top_rpow_def, asymm h, ne_of_lt h] @[simp] lemma zero_rpow_of_pos {y : ℝ} (h : 0 < y) : (0 : ℝ≥0∞) ^ y = 0 := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, asymm h, ne_of_gt h], end @[simp] lemma zero_rpow_of_neg {y : ℝ} (h : y < 0) : (0 : ℝ≥0∞) ^ y = ⊤ := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, ne_of_gt h], end lemma zero_rpow_def (y : ℝ) : (0 : ℝ≥0∞) ^ y = if 0 < y then 0 else if y = 0 then 1 else ⊤ := begin rcases lt_trichotomy 0 y with H|rfl|H, { simp [H, ne_of_gt, zero_rpow_of_pos, lt_irrefl] }, { simp [lt_irrefl] }, { simp [H, asymm H, ne_of_lt, zero_rpow_of_neg] } end @[simp] lemma zero_rpow_mul_self (y : ℝ) : (0 : ℝ≥0∞) ^ y * 0 ^ y = 0 ^ y := by { rw zero_rpow_def, split_ifs, exacts [zero_mul _, one_mul _, top_mul_top] } @[norm_cast] lemma coe_rpow_of_ne_zero {x : ℝ≥0} (h : x ≠ 0) (y : ℝ) : (x : ℝ≥0∞) ^ y = (x ^ y : ℝ≥0) := begin rw [← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h] end @[norm_cast] lemma coe_rpow_of_nonneg (x : ℝ≥0) {y : ℝ} (h : 0 ≤ y) : (x : ℝ≥0∞) ^ y = (x ^ y : ℝ≥0) := begin by_cases hx : x = 0, { rcases le_iff_eq_or_lt.1 h with H|H, { simp [hx, H.symm] }, { simp [hx, zero_rpow_of_pos H, nnreal.zero_rpow (ne_of_gt H)] } }, { exact coe_rpow_of_ne_zero hx _ } end lemma coe_rpow_def (x : ℝ≥0) (y : ℝ) : (x : ℝ≥0∞) ^ y = if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) := rfl @[simp] lemma rpow_one (x : ℝ≥0∞) : x ^ (1 : ℝ) = x := begin cases x, { exact dif_pos zero_lt_one }, { change ite _ _ _ = _, simp only [nnreal.rpow_one, some_eq_coe, ite_eq_right_iff, top_ne_coe, and_imp], exact λ _, zero_le_one.not_lt } end @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0∞) ^ x = 1 := by { rw [← coe_one, coe_rpow_of_ne_zero one_ne_zero], simp } @[simp] lemma rpow_eq_zero_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = 0 ↔ (x = 0 ∧ 0 < y) ∨ (x = ⊤ ∧ y < 0) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end @[simp] lemma rpow_eq_top_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = ⊤ ↔ (x = 0 ∧ y < 0) ∨ (x = ⊤ ∧ 0 < y) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end lemma rpow_eq_top_iff_of_pos {x : ℝ≥0∞} {y : ℝ} (hy : 0 < y) : x ^ y = ⊤ ↔ x = ⊤ := by simp [rpow_eq_top_iff, hy, asymm hy] lemma rpow_eq_top_of_nonneg (x : ℝ≥0∞) {y : ℝ} (hy0 : 0 ≤ y) : x ^ y = ⊤ → x = ⊤ := begin rw ennreal.rpow_eq_top_iff, intro h, cases h, { exfalso, rw lt_iff_not_ge at h, exact h.right hy0, }, { exact h.left, }, end lemma rpow_ne_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y ≠ ⊤ := mt (ennreal.rpow_eq_top_of_nonneg x hy0) h lemma rpow_lt_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y < ⊤ := lt_top_iff_ne_top.mpr (ennreal.rpow_ne_top_of_nonneg hy0 h) lemma rpow_add {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y + z) = x ^ y * x ^ z := begin cases x, { exact (h'x rfl).elim }, have : x ≠ 0 := λ h, by simpa [h] using hx, simp [coe_rpow_of_ne_zero this, nnreal.rpow_add this] end lemma rpow_neg (x : ℝ≥0∞) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [top_rpow_of_pos, top_rpow_of_neg, H, neg_pos.mpr] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, zero_rpow_of_pos, zero_rpow_of_neg, H, neg_pos.mpr] }, { have A : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, ← coe_inv A, nnreal.rpow_neg] } } end lemma rpow_sub {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y - z) = x ^ y / x ^ z := by rw [sub_eq_add_neg, rpow_add _ _ hx h'x, rpow_neg, div_eq_mul_inv] lemma rpow_neg_one (x : ℝ≥0∞) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_mul (x : ℝ≥0∞) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [h, Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { have : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, coe_rpow_of_ne_zero this, nnreal.rpow_mul] } } end @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0∞) (n : ℕ) : x ^ (n : ℝ) = x ^ n := begin cases x, { cases n; simp [top_rpow_of_pos (nat.cast_add_one_pos _), top_pow (nat.succ_pos _)] }, { simp [coe_rpow_of_nonneg _ (nat.cast_nonneg n)] } end @[simp] lemma rpow_two (x : ℝ≥0∞) : x ^ (2 : ℝ) = x ^ 2 := by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] } lemma mul_rpow_eq_ite (x y : ℝ≥0∞) (z : ℝ) : (x * y) ^ z = if (x = 0 ∧ y = ⊤ ∨ x = ⊤ ∧ y = 0) ∧ z < 0 then ⊤ else x ^ z * y ^ z := begin rcases eq_or_ne z 0 with rfl|hz, { simp }, replace hz := hz.lt_or_lt, wlog hxy : x ≤ y := le_total x y using [x y, y x] tactic.skip, { rcases eq_or_ne x 0 with rfl|hx0, { induction y using with_top.rec_top_coe; cases hz with hz hz; simp [*, hz.not_lt] }, rcases eq_or_ne y 0 with rfl|hy0, { exact (hx0 (bot_unique hxy)).elim }, induction x using with_top.rec_top_coe, { cases hz with hz hz; simp [hz, top_unique hxy] }, induction y using with_top.rec_top_coe, { cases hz with hz hz; simp * }, simp only [*, false_and, and_false, false_or, if_false], norm_cast at *, rw [coe_rpow_of_ne_zero (mul_ne_zero hx0 hy0), nnreal.mul_rpow] }, { convert this using 2; simp only [mul_comm, and_comm, or_comm] } end lemma mul_rpow_of_ne_top {x y : ℝ≥0∞} (hx : x ≠ ⊤) (hy : y ≠ ⊤) (z : ℝ) : (x * y) ^ z = x^z * y^z := by simp [*, mul_rpow_eq_ite] @[norm_cast] lemma coe_mul_rpow (x y : ℝ≥0) (z : ℝ) : ((x : ℝ≥0∞) * y) ^ z = x^z * y^z := mul_rpow_of_ne_top coe_ne_top coe_ne_top z lemma mul_rpow_of_ne_zero {x y : ℝ≥0∞} (hx : x ≠ 0) (hy : y ≠ 0) (z : ℝ) : (x * y) ^ z = x ^ z * y ^ z := by simp [*, mul_rpow_eq_ite] lemma mul_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) : (x * y) ^ z = x ^ z * y ^ z := by simp [hz.not_lt, mul_rpow_eq_ite] lemma inv_rpow (x : ℝ≥0∞) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ := begin rcases eq_or_ne y 0 with rfl|hy, { simp only [rpow_zero, inv_one] }, replace hy := hy.lt_or_lt, rcases eq_or_ne x 0 with rfl|h0, { cases hy; simp * }, rcases eq_or_ne x ⊤ with rfl|h_top, { cases hy; simp * }, apply eq_inv_of_mul_eq_one_left, rw [← mul_rpow_of_ne_zero (inv_ne_zero.2 h_top) h0, inv_mul_cancel h0 h_top, one_rpow] end lemma div_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) : (x / y) ^ z = x ^ z / y ^ z := by rw [div_eq_mul_inv, mul_rpow_of_nonneg _ _ hz, inv_rpow, div_eq_mul_inv] lemma strict_mono_rpow_of_pos {z : ℝ} (h : 0 < z) : strict_mono (λ x : ℝ≥0∞, x ^ z) := begin intros x y hxy, lift x to ℝ≥0 using ne_top_of_lt hxy, rcases eq_or_ne y ∞ with rfl|hy, { simp only [top_rpow_of_pos h, coe_rpow_of_nonneg _ h.le, coe_lt_top] }, { lift y to ℝ≥0 using hy, simp only [coe_rpow_of_nonneg _ h.le, nnreal.rpow_lt_rpow (coe_lt_coe.1 hxy) h, coe_lt_coe] } end lemma monotone_rpow_of_nonneg {z : ℝ} (h : 0 ≤ z) : monotone (λ x : ℝ≥0∞, x ^ z) := h.eq_or_lt.elim (λ h0, h0 ▸ by simp only [rpow_zero, monotone_const]) (λ h0, (strict_mono_rpow_of_pos h0).monotone) /-- Bundles `λ x : ℝ≥0∞, x ^ y` into an order isomorphism when `y : ℝ` is positive, where the inverse is `λ x : ℝ≥0∞, x ^ (1 / y)`. -/ @[simps apply] def order_iso_rpow (y : ℝ) (hy : 0 < y) : ℝ≥0∞ ≃o ℝ≥0∞ := (strict_mono_rpow_of_pos hy).order_iso_of_right_inverse (λ x, x ^ y) (λ x, x ^ (1 / y)) (λ x, by { dsimp, rw [←rpow_mul, one_div_mul_cancel hy.ne.symm, rpow_one] }) lemma order_iso_rpow_symm_apply (y : ℝ) (hy : 0 < y) : (order_iso_rpow y hy).symm = order_iso_rpow (1 / y) (one_div_pos.2 hy) := by { simp only [order_iso_rpow, one_div_one_div], refl } lemma rpow_le_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := monotone_rpow_of_nonneg h₂ h₁ lemma rpow_lt_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := strict_mono_rpow_of_pos h₂ h₁ lemma rpow_le_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := (strict_mono_rpow_of_pos hz).le_iff_le lemma rpow_lt_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := (strict_mono_rpow_of_pos hz).lt_iff_lt lemma le_rpow_one_div_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ≤ y ^ (1 / z) ↔ x ^ z ≤ y := begin nth_rewrite 0 ←rpow_one x, nth_rewrite 0 ←@_root_.mul_inv_cancel _ _ z hz.ne', rw [rpow_mul, ←one_div, @rpow_le_rpow_iff _ _ (1/z) (by simp [hz])], end lemma lt_rpow_one_div_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x < y ^ (1 / z) ↔ x ^ z < y := begin nth_rewrite 0 ←rpow_one x, nth_rewrite 0 ←@_root_.mul_inv_cancel _ _ z (ne_of_lt hz).symm, rw [rpow_mul, ←one_div, @rpow_lt_rpow_iff _ _ (1/z) (by simp [hz])], end lemma rpow_one_div_le_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ (1 / z) ≤ y ↔ x ≤ y ^ z := begin nth_rewrite 0 ← ennreal.rpow_one y, nth_rewrite 1 ← @_root_.mul_inv_cancel _ _ z hz.ne.symm, rw [ennreal.rpow_mul, ← one_div, ennreal.rpow_le_rpow_iff (one_div_pos.2 hz)], end lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0∞} {y z : ℝ} (hx : 1 < x) (hx' : x ≠ ⊤) (hyz : y < z) : x^y < x^z := begin lift x to ℝ≥0 using hx', rw [one_lt_coe_iff] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), nnreal.rpow_lt_rpow_of_exponent_lt hx hyz] end lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0∞} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, top_rpow_of_neg, top_rpow_of_pos, le_refl]; linarith }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), nnreal.rpow_le_rpow_of_exponent_le hx hyz] } end lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0∞} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx1 le_top), simp only [coe_lt_one_iff, coe_pos] at hx0 hx1, simp [coe_rpow_of_ne_zero (ne_of_gt hx0), nnreal.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz] end lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0∞} {y z : ℝ} (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx1 coe_lt_top), by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, h, zero_rpow_of_neg, zero_rpow_of_pos, le_refl]; linarith }, { rw [coe_le_one_iff] at hx1, simp [coe_rpow_of_ne_zero h, nnreal.rpow_le_rpow_of_exponent_ge (bot_lt_iff_ne_bot.mpr h) hx1 hyz] } end lemma rpow_le_self_of_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x := begin nth_rewrite 1 ←ennreal.rpow_one x, exact ennreal.rpow_le_rpow_of_exponent_ge hx h_one_le, end lemma le_rpow_self_of_one_le {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (h_one_le : 1 ≤ z) : x ≤ x ^ z := begin nth_rewrite 0 ←ennreal.rpow_one x, exact ennreal.rpow_le_rpow_of_exponent_le hx h_one_le, end lemma rpow_pos_of_nonneg {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hp_nonneg : 0 ≤ p) : 0 < x^p := begin by_cases hp_zero : p = 0, { simp [hp_zero, ennreal.zero_lt_one], }, { rw ←ne.def at hp_zero, have hp_pos := lt_of_le_of_ne hp_nonneg hp_zero.symm, rw ←zero_rpow_of_pos hp_pos, exact rpow_lt_rpow hx_pos hp_pos, }, end lemma rpow_pos {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hx_ne_top : x ≠ ⊤) : 0 < x^p := begin cases lt_or_le 0 p with hp_pos hp_nonpos, { exact rpow_pos_of_nonneg hx_pos (le_of_lt hp_pos), }, { rw [←neg_neg p, rpow_neg, inv_pos], exact rpow_ne_top_of_nonneg (right.nonneg_neg_iff.mpr hp_nonpos) hx_ne_top, }, end lemma rpow_lt_one {x : ℝ≥0∞} {z : ℝ} (hx : x < 1) (hz : 0 < z) : x^z < 1 := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx le_top), simp only [coe_lt_one_iff] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.rpow_lt_one hx hz], end lemma rpow_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx coe_lt_top), simp only [coe_le_one_iff] at hx, simp [coe_rpow_of_nonneg _ hz, nnreal.rpow_le_one hx hz], end lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := begin cases x, { simp [top_rpow_of_neg hz, ennreal.zero_lt_one] }, { simp only [some_eq_coe, one_lt_coe_iff] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), nnreal.rpow_lt_one_of_one_lt_of_neg hx hz] }, end lemma rpow_le_one_of_one_le_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : z < 0) : x^z ≤ 1 := begin cases x, { simp [top_rpow_of_neg hz, ennreal.zero_lt_one] }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), nnreal.rpow_le_one_of_one_le_of_nonpos hx (le_of_lt hz)] }, end lemma one_lt_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := begin cases x, { simp [top_rpow_of_pos hz] }, { simp only [some_eq_coe, one_lt_coe_iff] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_lt_rpow hx hz] } end lemma one_le_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : 0 < z) : 1 ≤ x^z := begin cases x, { simp [top_rpow_of_pos hz] }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_le_rpow hx (le_of_lt hz)] }, end lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0∞} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx2 le_top), simp only [coe_lt_one_iff, coe_pos] at ⊢ hx1 hx2, simp [coe_rpow_of_ne_zero (ne_of_gt hx1), nnreal.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz], end lemma one_le_rpow_of_pos_of_le_one_of_neg {x : ℝ≥0∞} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z < 0) : 1 ≤ x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx2 coe_lt_top), simp only [coe_le_one_iff, coe_pos] at ⊢ hx1 hx2, simp [coe_rpow_of_ne_zero (ne_of_gt hx1), nnreal.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 (le_of_lt hz)], end lemma to_nnreal_rpow (x : ℝ≥0∞) (z : ℝ) : (x.to_nnreal) ^ z = (x ^ z).to_nnreal := begin rcases lt_trichotomy z 0 with H|H|H, { cases x, { simp [H, ne_of_lt] }, by_cases hx : x = 0, { simp [hx, H, ne_of_lt] }, { simp [coe_rpow_of_ne_zero hx] } }, { simp [H] }, { cases x, { simp [H, ne_of_gt] }, simp [coe_rpow_of_nonneg _ (le_of_lt H)] } end lemma to_real_rpow (x : ℝ≥0∞) (z : ℝ) : (x.to_real) ^ z = (x ^ z).to_real := by rw [ennreal.to_real, ennreal.to_real, ←nnreal.coe_rpow, ennreal.to_nnreal_rpow] lemma of_real_rpow_of_pos {x p : ℝ} (hx_pos : 0 < x) : ennreal.of_real x ^ p = ennreal.of_real (x ^ p) := begin simp_rw ennreal.of_real, rw [coe_rpow_of_ne_zero, coe_eq_coe, real.to_nnreal_rpow_of_nonneg hx_pos.le], simp [hx_pos], end lemma of_real_rpow_of_nonneg {x p : ℝ} (hx_nonneg : 0 ≤ x) (hp_nonneg : 0 ≤ p) : ennreal.of_real x ^ p = ennreal.of_real (x ^ p) := begin by_cases hp0 : p = 0, { simp [hp0], }, by_cases hx0 : x = 0, { rw ← ne.def at hp0, have hp_pos : 0 < p := lt_of_le_of_ne hp_nonneg hp0.symm, simp [hx0, hp_pos, hp_pos.ne.symm], }, rw ← ne.def at hx0, exact of_real_rpow_of_pos (hx_nonneg.lt_of_ne hx0.symm), end lemma rpow_left_injective {x : ℝ} (hx : x ≠ 0) : function.injective (λ y : ℝ≥0∞, y^x) := begin intros y z hyz, dsimp only at hyz, rw [←rpow_one y, ←rpow_one z, ←_root_.mul_inv_cancel hx, rpow_mul, rpow_mul, hyz], end lemma rpow_left_surjective {x : ℝ} (hx : x ≠ 0) : function.surjective (λ y : ℝ≥0∞, y^x) := λ y, ⟨y ^ x⁻¹, by simp_rw [←rpow_mul, _root_.inv_mul_cancel hx, rpow_one]⟩ lemma rpow_left_bijective {x : ℝ} (hx : x ≠ 0) : function.bijective (λ y : ℝ≥0∞, y^x) := ⟨rpow_left_injective hx, rpow_left_surjective hx⟩ theorem tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ (x : ℝ≥0∞), x ^ y) (𝓝 ⊤) (𝓝 ⊤) := begin rw tendsto_nhds_top_iff_nnreal, intros x, obtain ⟨c, _, hc⟩ := (at_top_basis_Ioi.tendsto_iff at_top_basis_Ioi).mp (nnreal.tendsto_rpow_at_top hy) x trivial, have hc' : set.Ioi (↑c) ∈ 𝓝 (⊤ : ℝ≥0∞) := Ioi_mem_nhds coe_lt_top, refine eventually_of_mem hc' _, intros a ha, by_cases ha' : a = ⊤, { simp [ha', hy] }, lift a to ℝ≥0 using ha', change ↑c < ↑a at ha, rw coe_rpow_of_nonneg _ hy.le, exact_mod_cast hc a (by exact_mod_cast ha), end lemma eventually_pow_one_div_le {x : ℝ≥0∞} (hx : x ≠ ∞) {y : ℝ≥0∞} (hy : 1 < y) : ∀ᶠ (n : ℕ) in at_top, x ^ (1 / n : ℝ) ≤ y := begin lift x to ℝ≥0 using hx, by_cases y = ∞, { exact eventually_of_forall (λ n, h.symm ▸ le_top) }, { lift y to ℝ≥0 using h, have := nnreal.eventually_pow_one_div_le x (by exact_mod_cast hy : 1 < y), refine this.congr (eventually_of_forall $ λ n, _), rw [coe_rpow_of_nonneg x (by positivity : 0 ≤ (1 / n : ℝ)), coe_le_coe] }, end private lemma continuous_at_rpow_const_of_pos {x : ℝ≥0∞} {y : ℝ} (h : 0 < y) : continuous_at (λ a : ℝ≥0∞, a ^ y) x := begin by_cases hx : x = ⊤, { rw [hx, continuous_at], convert tendsto_rpow_at_top h, simp [h] }, lift x to ℝ≥0 using hx, rw continuous_at_coe_iff, convert continuous_coe.continuous_at.comp (nnreal.continuous_at_rpow_const (or.inr h.le)) using 1, ext1 x, simp [coe_rpow_of_nonneg _ h.le] end @[continuity] lemma continuous_rpow_const {y : ℝ} : continuous (λ a : ℝ≥0∞, a ^ y) := begin apply continuous_iff_continuous_at.2 (λ x, _), rcases lt_trichotomy 0 y with hy|rfl|hy, { exact continuous_at_rpow_const_of_pos hy }, { simp only [rpow_zero], exact continuous_at_const }, { obtain ⟨z, hz⟩ : ∃ z, y = -z := ⟨-y, (neg_neg _).symm⟩, have z_pos : 0 < z, by simpa [hz] using hy, simp_rw [hz, rpow_neg], exact continuous_inv.continuous_at.comp (continuous_at_rpow_const_of_pos z_pos) } end lemma tendsto_const_mul_rpow_nhds_zero_of_pos {c : ℝ≥0∞} (hc : c ≠ ∞) {y : ℝ} (hy : 0 < y) : tendsto (λ x : ℝ≥0∞, c * x ^ y) (𝓝 0) (𝓝 0) := begin convert ennreal.tendsto.const_mul (ennreal.continuous_rpow_const.tendsto 0) _, { simp [hy] }, { exact or.inr hc } end end ennreal lemma filter.tendsto.ennrpow_const {α : Type*} {f : filter α} {m : α → ℝ≥0∞} {a : ℝ≥0∞} (r : ℝ) (hm : tendsto m f (𝓝 a)) : tendsto (λ x, (m x) ^ r) f (𝓝 (a ^ r)) := (ennreal.continuous_rpow_const.tendsto a).comp hm namespace norm_num open tactic theorem rpow_pos (a b : ℝ) (b' : ℕ) (c : ℝ) (hb : (b':ℝ) = b) (h : a ^ b' = c) : a ^ b = c := by rw [← h, ← hb, real.rpow_nat_cast] theorem rpow_neg (a b : ℝ) (b' : ℕ) (c c' : ℝ) (a0 : 0 ≤ a) (hb : (b':ℝ) = b) (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' := by rw [← hc, ← h, ← hb, real.rpow_neg a0, real.rpow_nat_cast] /-- Evaluate `real.rpow a b` where `a` is a rational numeral and `b` is an integer. (This cannot go via the generalized version `prove_rpow'` because `rpow_pos` has a side condition; we do not attempt to evaluate `a ^ b` where `a` and `b` are both negative because it comes out to some garbage.) -/ meta def prove_rpow (a b : expr) : tactic (expr × expr) := do na ← a.to_rat, ic ← mk_instance_cache `(ℝ), match match_sign b with | sum.inl b := do (ic, a0) ← guard (na ≥ 0) >> prove_nonneg ic a, nc ← mk_instance_cache `(ℕ), (ic, nc, b', hb) ← prove_nat_uncast ic nc b, (ic, c, h) ← prove_pow a na ic b', cr ← c.to_rat, (ic, c', hc) ← prove_inv ic c cr, pure (c', (expr.const ``rpow_neg []).mk_app [a, b, b', c, c', a0, hb, h, hc]) | sum.inr ff := pure (`(1:ℝ), expr.const ``real.rpow_zero [] a) | sum.inr tt := do nc ← mk_instance_cache `(ℕ), (ic, nc, b', hb) ← prove_nat_uncast ic nc b, (ic, c, h) ← prove_pow a na ic b', pure (c, (expr.const ``rpow_pos []).mk_app [a, b, b', c, hb, h]) end /-- Generalized version of `prove_cpow`, `prove_nnrpow`, `prove_ennrpow`. -/ meta def prove_rpow' (pos neg zero : name) (α β one a b : expr) : tactic (expr × expr) := do na ← a.to_rat, icα ← mk_instance_cache α, icβ ← mk_instance_cache β, match match_sign b with | sum.inl b := do nc ← mk_instance_cache `(ℕ), (icβ, nc, b', hb) ← prove_nat_uncast icβ nc b, (icα, c, h) ← prove_pow a na icα b', cr ← c.to_rat, (icα, c', hc) ← prove_inv icα c cr, pure (c', (expr.const neg []).mk_app [a, b, b', c, c', hb, h, hc]) | sum.inr ff := pure (one, expr.const zero [] a) | sum.inr tt := do nc ← mk_instance_cache `(ℕ), (icβ, nc, b', hb) ← prove_nat_uncast icβ nc b, (icα, c, h) ← prove_pow a na icα b', pure (c, (expr.const pos []).mk_app [a, b, b', c, hb, h]) end open_locale nnreal ennreal theorem cpow_pos (a b : ℂ) (b' : ℕ) (c : ℂ) (hb : b = b') (h : a ^ b' = c) : a ^ b = c := by rw [← h, hb, complex.cpow_nat_cast] theorem cpow_neg (a b : ℂ) (b' : ℕ) (c c' : ℂ) (hb : b = b') (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' := by rw [← hc, ← h, hb, complex.cpow_neg, complex.cpow_nat_cast] theorem nnrpow_pos (a : ℝ≥0) (b : ℝ) (b' : ℕ) (c : ℝ≥0) (hb : b = b') (h : a ^ b' = c) : a ^ b = c := by rw [← h, hb, nnreal.rpow_nat_cast] theorem nnrpow_neg (a : ℝ≥0) (b : ℝ) (b' : ℕ) (c c' : ℝ≥0) (hb : b = b') (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' := by rw [← hc, ← h, hb, nnreal.rpow_neg, nnreal.rpow_nat_cast] theorem ennrpow_pos (a : ℝ≥0∞) (b : ℝ) (b' : ℕ) (c : ℝ≥0∞) (hb : b = b') (h : a ^ b' = c) : a ^ b = c := by rw [← h, hb, ennreal.rpow_nat_cast] theorem ennrpow_neg (a : ℝ≥0∞) (b : ℝ) (b' : ℕ) (c c' : ℝ≥0∞) (hb : b = b') (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' := by rw [← hc, ← h, hb, ennreal.rpow_neg, ennreal.rpow_nat_cast] /-- Evaluate `complex.cpow a b` where `a` is a rational numeral and `b` is an integer. -/ meta def prove_cpow : expr → expr → tactic (expr × expr) := prove_rpow' ``cpow_pos ``cpow_neg ``complex.cpow_zero `(ℂ) `(ℂ) `(1:ℂ) /-- Evaluate `nnreal.rpow a b` where `a` is a rational numeral and `b` is an integer. -/ meta def prove_nnrpow : expr → expr → tactic (expr × expr) := prove_rpow' ``nnrpow_pos ``nnrpow_neg ``nnreal.rpow_zero `(ℝ≥0) `(ℝ) `(1:ℝ≥0) /-- Evaluate `ennreal.rpow a b` where `a` is a rational numeral and `b` is an integer. -/ meta def prove_ennrpow : expr → expr → tactic (expr × expr) := prove_rpow' ``ennrpow_pos ``ennrpow_neg ``ennreal.rpow_zero `(ℝ≥0∞) `(ℝ) `(1:ℝ≥0∞) /-- Evaluates expressions of the form `rpow a b`, `cpow a b` and `a ^ b` in the special case where `b` is an integer and `a` is a positive rational (so it's really just a rational power). -/ @[norm_num] meta def eval_rpow_cpow : expr → tactic (expr × expr) | `(@has_pow.pow _ _ real.has_pow %%a %%b) := b.to_int >> prove_rpow a b | `(real.rpow %%a %%b) := b.to_int >> prove_rpow a b | `(@has_pow.pow _ _ complex.has_pow %%a %%b) := b.to_int >> prove_cpow a b | `(complex.cpow %%a %%b) := b.to_int >> prove_cpow a b | `(@has_pow.pow _ _ nnreal.real.has_pow %%a %%b) := b.to_int >> prove_nnrpow a b | `(nnreal.rpow %%a %%b) := b.to_int >> prove_nnrpow a b | `(@has_pow.pow _ _ ennreal.real.has_pow %%a %%b) := b.to_int >> prove_ennrpow a b | `(ennreal.rpow %%a %%b) := b.to_int >> prove_ennrpow a b | _ := tactic.failed end norm_num namespace tactic namespace positivity /-- Auxiliary definition for the `positivity` tactic to handle real powers of reals. -/ meta def prove_rpow (a b : expr) : tactic strictness := do strictness_a ← core a, match strictness_a with | nonnegative p := nonnegative <$> mk_app ``real.rpow_nonneg_of_nonneg [p, b] | positive p := positive <$> mk_app ``real.rpow_pos_of_pos [p, b] | _ := failed end private lemma nnrpow_pos {a : ℝ≥0} (ha : 0 < a) (b : ℝ) : 0 < a ^ b := nnreal.rpow_pos ha /-- Auxiliary definition for the `positivity` tactic to handle real powers of nonnegative reals. -/ meta def prove_nnrpow (a b : expr) : tactic strictness := do strictness_a ← core a, match strictness_a with | positive p := positive <$> mk_app ``nnrpow_pos [p, b] | _ := failed -- We already know `0 ≤ x` for all `x : ℝ≥0` end private lemma ennrpow_pos {a : ℝ≥0∞} {b : ℝ} (ha : 0 < a) (hb : 0 < b) : 0 < a ^ b := ennreal.rpow_pos_of_nonneg ha hb.le /-- Auxiliary definition for the `positivity` tactic to handle real powers of extended nonnegative reals. -/ meta def prove_ennrpow (a b : expr) : tactic strictness := do strictness_a ← core a, strictness_b ← core b, match strictness_a, strictness_b with | positive pa, positive pb := positive <$> mk_app ``ennrpow_pos [pa, pb] | positive pa, nonnegative pb := positive <$> mk_app ``ennreal.rpow_pos_of_nonneg [pa, pb] | _, _ := failed -- We already know `0 ≤ x` for all `x : ℝ≥0∞` end end positivity open positivity /-- Extension for the `positivity` tactic: exponentiation by a real number is nonnegative when the base is nonnegative and positive when the base is positive. -/ @[positivity] meta def positivity_rpow : expr → tactic strictness | `(@has_pow.pow _ _ real.has_pow %%a %%b) := prove_rpow a b | `(real.rpow %%a %%b) := prove_rpow a b | `(@has_pow.pow _ _ nnreal.real.has_pow %%a %%b) := prove_nnrpow a b | `(nnreal.rpow %%a %%b) := prove_nnrpow a b | `(@has_pow.pow _ _ ennreal.real.has_pow %%a %%b) := prove_ennrpow a b | `(ennreal.rpow %%a %%b) := prove_ennrpow a b | _ := failed end tactic
05486d2f446b5d789e369a8ae340047f79d4fc92
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/control/monad/cont.lean
b5767c8228f7906c88859827b2d19e7ce298745d
[ "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
9,399
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon Monad encapsulating continuation passing programming style, similar to Haskell's `Cont`, `ContT` and `MonadCont`: <http://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Cont.html> -/ import control.monad.basic import control.monad.writer universes u v w u₀ u₁ v₀ v₁ structure monad_cont.label (α : Type w) (m : Type u → Type v) (β : Type u) := (apply : α → m β) def monad_cont.goto {α β} {m : Type u → Type v} (f : monad_cont.label α m β) (x : α) := f.apply x class monad_cont (m : Type u → Type v) := (call_cc : Π {α β}, ((monad_cont.label α m β) → m α) → m α) open monad_cont class is_lawful_monad_cont (m : Type u → Type v) [monad m] [monad_cont m] extends is_lawful_monad m := (call_cc_bind_right {α ω γ} (cmd : m α) (next : (label ω m γ) → α → m ω) : call_cc (λ f, cmd >>= next f) = cmd >>= λ x, call_cc (λ f, next f x)) (call_cc_bind_left {α} (β) (x : α) (dead : label α m β → β → m α) : call_cc (λ f : label α m β, goto f x >>= dead f) = pure x) (call_cc_dummy {α β} (dummy : m α) : call_cc (λ f : label α m β, dummy) = dummy) export is_lawful_monad_cont def cont_t (r : Type u) (m : Type u → Type v) (α : Type w) := (α → m r) → m r @[reducible] def cont (r : Type u) (α : Type w) := cont_t r id α namespace cont_t export monad_cont (label goto) variables {r : Type u} {m : Type u → Type v} {α β γ ω : Type w} def run : cont_t r m α → (α → m r) → m r := id def map (f : m r → m r) (x : cont_t r m α) : cont_t r m α := f ∘ x lemma run_cont_t_map_cont_t (f : m r → m r) (x : cont_t r m α) : run (map f x) = f ∘ run x := rfl def with_cont_t (f : (β → m r) → α → m r) (x : cont_t r m α) : cont_t r m β := λ g, x $ f g lemma run_with_cont_t (f : (β → m r) → α → m r) (x : cont_t r m α) : run (with_cont_t f x) = run x ∘ f := rfl @[ext] protected lemma ext {x y : cont_t r m α} (h : ∀ f, x.run f = y.run f) : x = y := by { ext; apply h } instance : monad (cont_t r m) := { pure := λ α x f, f x, bind := λ α β x f g, x $ λ i, f i g } instance : is_lawful_monad (cont_t r m) := { id_map := by { intros, refl }, pure_bind := by { intros, ext, refl }, bind_assoc := by { intros, ext, refl } } def monad_lift [monad m] {α} : m α → cont_t r m α := λ x f, x >>= f instance [monad m] : has_monad_lift m (cont_t r m) := { monad_lift := λ α, cont_t.monad_lift } lemma monad_lift_bind [monad m] [is_lawful_monad m] {α β} (x : m α) (f : α → m β) : (monad_lift (x >>= f) : cont_t r m β) = monad_lift x >>= monad_lift ∘ f := begin ext, simp only [monad_lift,has_monad_lift.monad_lift,(∘),(>>=),bind_assoc,id.def,run,cont_t.monad_lift] end instance : monad_cont (cont_t r m) := { call_cc := λ α β f g, f ⟨λ x h, g x⟩ g } instance : is_lawful_monad_cont (cont_t r m) := { call_cc_bind_right := by intros; ext; refl, call_cc_bind_left := by intros; ext; refl, call_cc_dummy := by intros; ext; refl } instance (ε) [monad_except ε m] : monad_except ε (cont_t r m) := { throw := λ x e f, throw e, catch := λ α act h f, catch (act f) (λ e, h e f) } instance : monad_run (λ α, (α → m r) → ulift.{u v} (m r)) (cont_t.{u v u} r m) := { run := λ α f x, ⟨ f x ⟩ } end cont_t variables {m : Type u → Type v} [monad m] def except_t.mk_label {α β ε} : label (except.{u u} ε α) m β → label α (except_t ε m) β | ⟨ f ⟩ := ⟨ λ a, monad_lift $ f (except.ok a) ⟩ lemma except_t.goto_mk_label {α β ε : Type*} (x : label (except.{u u} ε α) m β) (i : α) : goto (except_t.mk_label x) i = ⟨ except.ok <$> goto x (except.ok i) ⟩ := by cases x; refl def except_t.call_cc {ε} [monad_cont m] {α β : Type*} (f : label α (except_t ε m) β → except_t ε m α) : except_t ε m α := except_t.mk (call_cc $ λ x : label _ m β, except_t.run $ f (except_t.mk_label x) : m (except ε α)) instance {ε} [monad_cont m] : monad_cont (except_t ε m) := { call_cc := λ α β, except_t.call_cc } instance {ε} [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (except_t ε m) := { call_cc_bind_right := by { intros, simp [call_cc,except_t.call_cc,call_cc_bind_right], ext, dsimp, congr' with ⟨ ⟩; simp [except_t.bind_cont,@call_cc_dummy m _], }, call_cc_bind_left := by { intros, simp [call_cc,except_t.call_cc,call_cc_bind_right,except_t.goto_mk_label,map_eq_bind_pure_comp, bind_assoc,@call_cc_bind_left m _], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,except_t.call_cc,@call_cc_dummy m _], ext, refl }, } def option_t.mk_label {α β} : label (option.{u} α) m β → label α (option_t m) β | ⟨ f ⟩ := ⟨ λ a, monad_lift $ f (some a) ⟩ lemma option_t.goto_mk_label {α β : Type*} (x : label (option.{u} α) m β) (i : α) : goto (option_t.mk_label x) i = ⟨ some <$> goto x (some i) ⟩ := by cases x; refl def option_t.call_cc [monad_cont m] {α β : Type*} (f : label α (option_t m) β → option_t m α) : option_t m α := option_t.mk (call_cc $ λ x : label _ m β, option_t.run $ f (option_t.mk_label x) : m (option α)) instance [monad_cont m] : monad_cont (option_t m) := { call_cc := λ α β, option_t.call_cc } instance [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (option_t m) := { call_cc_bind_right := by { intros, simp [call_cc,option_t.call_cc,call_cc_bind_right], ext, dsimp, congr' with ⟨ ⟩; simp [option_t.bind_cont,@call_cc_dummy m _], }, call_cc_bind_left := by { intros, simp [call_cc,option_t.call_cc,call_cc_bind_right, option_t.goto_mk_label,map_eq_bind_pure_comp,bind_assoc,@call_cc_bind_left m _], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,option_t.call_cc,@call_cc_dummy m _], ext, refl }, } def writer_t.mk_label {α β ω} [has_one ω] : label (α × ω) m β → label α (writer_t ω m) β | ⟨ f ⟩ := ⟨ λ a, monad_lift $ f (a,1) ⟩ lemma writer_t.goto_mk_label {α β ω : Type*} [has_one ω] (x : label (α × ω) m β) (i : α) : goto (writer_t.mk_label x) i = monad_lift (goto x (i,1)) := by cases x; refl def writer_t.call_cc [monad_cont m] {α β ω : Type*} [has_one ω] (f : label α (writer_t ω m) β → writer_t ω m α) : writer_t ω m α := ⟨ call_cc (writer_t.run ∘ f ∘ writer_t.mk_label : label (α × ω) m β → m (α × ω)) ⟩ instance (ω) [monad m] [has_one ω] [monad_cont m] : monad_cont (writer_t ω m) := { call_cc := λ α β, writer_t.call_cc } def state_t.mk_label {α β σ : Type u} : label (α × σ) m (β × σ) → label α (state_t σ m) β | ⟨ f ⟩ := ⟨ λ a, ⟨ λ s, f (a,s) ⟩ ⟩ lemma state_t.goto_mk_label {α β σ : Type u} (x : label (α × σ) m (β × σ)) (i : α) : goto (state_t.mk_label x) i = ⟨ λ s, (goto x (i,s)) ⟩ := by cases x; refl def state_t.call_cc {σ} [monad_cont m] {α β : Type*} (f : label α (state_t σ m) β → state_t σ m α) : state_t σ m α := ⟨ λ r, call_cc (λ f', (f $ state_t.mk_label f').run r) ⟩ instance {σ} [monad_cont m] : monad_cont (state_t σ m) := { call_cc := λ α β, state_t.call_cc } instance {σ} [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (state_t σ m) := { call_cc_bind_right := by { intros, simp [call_cc,state_t.call_cc,call_cc_bind_right,(>>=),state_t.bind], ext, dsimp, congr' with ⟨x₀,x₁⟩, refl }, call_cc_bind_left := by { intros, simp [call_cc,state_t.call_cc,call_cc_bind_left,(>>=), state_t.bind,state_t.goto_mk_label], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,state_t.call_cc,call_cc_bind_right,(>>=), state_t.bind,@call_cc_dummy m _], ext, refl }, } def reader_t.mk_label {α β} (ρ) : label α m β → label α (reader_t ρ m) β | ⟨ f ⟩ := ⟨ monad_lift ∘ f ⟩ lemma reader_t.goto_mk_label {α ρ β} (x : label α m β) (i : α) : goto (reader_t.mk_label ρ x) i = monad_lift (goto x i) := by cases x; refl def reader_t.call_cc {ε} [monad_cont m] {α β : Type*} (f : label α (reader_t ε m) β → reader_t ε m α) : reader_t ε m α := ⟨ λ r, call_cc (λ f', (f $ reader_t.mk_label _ f').run r) ⟩ instance {ρ} [monad_cont m] : monad_cont (reader_t ρ m) := { call_cc := λ α β, reader_t.call_cc } instance {ρ} [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (reader_t ρ m) := { call_cc_bind_right := by { intros, simp [call_cc,reader_t.call_cc,call_cc_bind_right], ext, refl }, call_cc_bind_left := by { intros, simp [call_cc,reader_t.call_cc,call_cc_bind_left, reader_t.goto_mk_label], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,reader_t.call_cc,@call_cc_dummy m _], ext, refl } } /-- reduce the equivalence between two continuation passing monads to the equivalence between their underlying monad -/ def cont_t.equiv {m₁ : Type u₀ → Type v₀} {m₂ : Type u₁ → Type v₁} {α₁ r₁ : Type u₀} {α₂ r₂ : Type u₁} (F : m₁ r₁ ≃ m₂ r₂) (G : α₁ ≃ α₂) : cont_t r₁ m₁ α₁ ≃ cont_t r₂ m₂ α₂ := { to_fun := λ f r, F $ f $ λ x, F.symm $ r $ G x, inv_fun := λ f r, F.symm $ f $ λ x, F $ r $ G.symm x, left_inv := λ f, by funext r; simp, right_inv := λ f, by funext r; simp }
b6b42201948fff9ab10a89c6f0378759bd01f69e
64874bd1010548c7f5a6e3e8902efa63baaff785
/library/init/measurable.lean
93e5079ae4e4cd34f0cd2ce31f360b09fc21ef2a
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,443
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: init.measurable Authors: Leonardo de Moura Types with a nat-valued complexity measure. -/ prelude import init.nat open nat inductive measurable [class] (A : Type) := mk : (A → nat) → measurable A definition size_of {A : Type} [s : measurable A] (a : A) : nat := measurable.rec_on s (λ f, f) a definition nat.measurable [instance] : measurable nat := measurable.mk (λ a, a) definition option.measurable [instance] (A : Type) (s : measurable A) : measurable (option A) := measurable.mk (λ a, option.cases_on a zero (λ a, size_of a)) definition prod.measurable [instance] (A B : Type) (sa : measurable A) (sb : measurable B) : measurable (prod A B) := measurable.mk (λ p, prod.cases_on p (λ a b, size_of a + size_of b)) definition sum.measurable [instance] (A B : Type) (sa : measurable A) (sb : measurable B) : measurable (sum A B) := measurable.mk (λ s, sum.cases_on s (λ a, size_of a) (λ b, size_of b)) definition bool.measurable [instance] : measurable bool := measurable.mk (λb, zero) definition Prop.measurable [instance] : measurable Prop := measurable.mk (λp, zero) definition unit.measurable [instance] : measurable unit := measurable.mk (λu, zero) definition fn.measurable [instance] (A : Type) (B : A → Type) : measurable (Π x, B x) := measurable.mk (λf, zero)
749d49bb3f9e87910071f411307aea32df13a737
aa2345b30d710f7e75f13157a35845ee6d48c017
/category_theory/products.lean
162dd3f08ba9dd3de224803c52a046f046ba7aee
[ "Apache-2.0" ]
permissive
CohenCyril/mathlib
5241b20a3fd0ac0133e48e618a5fb7761ca7dcbe
a12d5a192f5923016752f638d19fc1a51610f163
refs/heads/master
1,586,031,957,957
1,541,432,824,000
1,541,432,824,000
156,246,337
0
0
Apache-2.0
1,541,434,514,000
1,541,434,513,000
null
UTF-8
Lean
false
false
4,909
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import category_theory.functor_category import category_theory.isomorphism import tactic.interactive namespace category_theory universes u₁ v₁ u₂ v₂ u₃ v₃ u₄ v₄ section variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] (D : Type u₂) [𝒟 : category.{u₂ v₂} D] include 𝒞 𝒟 /-- `prod.category C D` gives the cartesian product of two categories. -/ instance prod : category.{(max u₁ u₂) (max v₁ v₂)} (C × D) := { hom := λ X Y, ((X.1) ⟶ (Y.1)) × ((X.2) ⟶ (Y.2)), id := λ X, ⟨ 𝟙 (X.1), 𝟙 (X.2) ⟩, comp := λ _ _ _ f g, (f.1 ≫ g.1, f.2 ≫ g.2) } -- rfl lemmas for category.prod @[simp] lemma prod_id (X : C) (Y : D) : 𝟙 (X, Y) = (𝟙 X, 𝟙 Y) := rfl @[simp] lemma prod_comp {P Q R : C} {S T U : D} (f : (P, S) ⟶ (Q, T)) (g : (Q, T) ⟶ (R, U)) : f ≫ g = (f.1 ≫ g.1, f.2 ≫ g.2) := rfl end section variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] (D : Type u₁) [𝒟 : category.{u₁ v₁} D] include 𝒞 𝒟 /-- `prod.category.uniform C D` is an additional instance specialised so both factors have the same universe levels. This helps typeclass resolution. -/ instance uniform_prod : category (C × D) := category_theory.prod C D end -- Next we define the natural functors into and out of product categories. For now this doesn't address the universal properties. namespace prod variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] (D : Type u₂) [𝒟 : category.{u₂ v₂} D] include 𝒞 𝒟 /-- `inl C Z` is the functor `X ↦ (X, Z)`. -/ def inl (Z : D) : C ⥤ (C × D) := { obj := λ X, (X, Z), map' := λ X Y f, (f, 𝟙 Z) } /-- `inr D Z` is the functor `X ↦ (Z, X)`. -/ def inr (Z : C) : D ⥤ (C × D) := { obj := λ X, (Z, X), map' := λ X Y f, (𝟙 Z, f) } /-- `fst` is the functor `(X, Y) ↦ X`. -/ def fst : (C × D) ⥤ C := { obj := λ X, X.1, map' := λ X Y f, f.1 } /-- `snd` is the functor `(X, Y) ↦ Y`. -/ def snd : (C × D) ⥤ D := { obj := λ X, X.2, map' := λ X Y f, f.2 } def swap : (C × D) ⥤ (D × C) := { obj := λ X, (X.2, X.1), map' := λ _ _ f, (f.2, f.1) } def symmetry : ((swap C D) ⋙ (swap D C)) ≅ (functor.id (C × D)) := { hom := { app := λ X, 𝟙 X, naturality' := begin intros, erw [category.comp_id (C × D), category.id_comp (C × D)], dsimp [swap], simp, end }, inv := { app := λ X, 𝟙 X, naturality' := begin intros, erw [category.comp_id (C × D), category.id_comp (C × D)], dsimp [swap], simp, end } } end prod section variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] (D : Type u₂) [𝒟 : category.{u₂ v₂} D] include 𝒞 𝒟 -- TODO, later this can be defined by uncurrying `functor.id (C ⥤ D)` def evaluation : ((C ⥤ D) × C) ⥤ D := { obj := λ p, p.1 p.2, map' := λ x y f, (x.1.map f.2) ≫ (f.1 y.2), map_comp' := begin intros X Y Z f g, cases g, cases f, cases Z, cases Y, cases X, dsimp at *, simp at *, erw [←nat_trans.vcomp_app, nat_trans.naturality, category.assoc, nat_trans.naturality] end } end variables {A : Type u₁} [𝒜 : category.{u₁ v₁} A] {B : Type u₂} [ℬ : category.{u₂ v₂} B] {C : Type u₃} [𝒞 : category.{u₃ v₃} C] {D : Type u₄} [𝒟 : category.{u₄ v₄} D] include 𝒜 ℬ 𝒞 𝒟 namespace functor /-- The cartesian product of two functors. -/ def prod (F : A ⥤ B) (G : C ⥤ D) : (A × C) ⥤ (B × D) := { obj := λ X, (F X.1, G X.2), map' := λ _ _ f, (F.map f.1, G.map f.2) } /- Because of limitations in Lean 3's handling of notations, we do not setup a notation `F × G`. You can use `F.prod G` as a "poor man's infix", or just write `functor.prod F G`. -/ @[simp] lemma prod_obj (F : A ⥤ B) (G : C ⥤ D) (a : A) (c : C) : (F.prod G) (a, c) = (F a, G c) := rfl @[simp] lemma prod_map (F : A ⥤ B) (G : C ⥤ D) {a a' : A} {c c' : C} (f : (a, c) ⟶ (a', c')) : (F.prod G).map f = (F.map f.1, G.map f.2) := rfl end functor namespace nat_trans /-- The cartesian product of two natural transformations. -/ def prod {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟹ G) (β : H ⟹ I) : F.prod H ⟹ G.prod I := { app := λ X, (α X.1, β X.2), naturality' := begin /- `obviously'` says: -/ intros, cases f, cases Y, cases X, dsimp at *, simp, split, rw naturality, rw naturality end } /- Again, it is inadvisable in Lean 3 to setup a notation `α × β`; use instead `α.prod β` or `nat_trans.prod α β`. -/ @[simp] lemma prod_app {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟹ G) (β : H ⟹ I) (a : A) (c : C) : (nat_trans.prod α β) (a, c) = (α a, β c) := rfl end nat_trans end category_theory
dde01a44d1f77a06bd5e1f4ea2b8df8bebb045b7
bb31430994044506fa42fd667e2d556327e18dfe
/src/number_theory/well_approximable.lean
621d66baf8be11ebcaa34b5ed07de8672e8a5bae
[ "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
16,750
lean
/- Copyright (c) 2022 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import dynamics.ergodic.add_circle import measure_theory.covering.liminf_limsup import data.nat.totient /-! # Well-approximable numbers and Gallagher's ergodic theorem Gallagher's ergodic theorem is a result in metric number theory. It thus belongs to that branch of mathematics concerning arithmetic properties of real numbers which hold almost eveywhere with respect to the Lebesgue measure. Gallagher's theorem concerns the approximation of real numbers by rational numbers. The input is a sequence of distances `δ₁, δ₂, ...`, and the theorem concerns the set of real numbers `x` for which there is an infinity of solutions to: $$ |x - m/n| < δₙ, $$ where the rational number `m/n` is in lowest terms. The result is that for any `δ`, this set is either almost all `x` or almost no `x`. This result was proved by Gallagher in 1959 [P. Gallagher, *Approximation by reduced fractions*](Gallagher1961). It is formalised here as `add_circle.add_well_approximable_ae_empty_or_univ` except with `x` belonging to the circle `ℝ ⧸ ℤ` since this turns out to be more natural. Given a particular `δ`, the Duffin-Schaeffer conjecture (now a theorem) gives a criterion for deciding which of the two cases in the conclusion of Gallagher's theorem actually occurs. It was proved by Koukoulopoulos and Maynard in 2019 [D. Koukoulopoulos, J. Maynard, *On the Duffin-Schaeffer conjecture*](KoukoulopoulosMaynard2020). We do *not* include a formalisation of the Koukoulopoulos-Maynard result here. ## Main definitions and results: * `approx_order_of`: in a seminormed group `A`, given `n : ℕ` and `δ : ℝ`, `approx_order_of A n δ` is the set of elements within a distance `δ` of a point of order `n`. * `well_approximable`: in a seminormed group `A`, given a sequence of distances `δ₁, δ₂, ...`, `well_approximable A δ` is the limsup as `n → ∞` of the sets `approx_order_of A n δₙ`. Thus, it is the set of points that lie in infinitely many of the sets `approx_order_of A n δₙ`. * `add_circle.add_well_approximable_ae_empty_or_univ`: *Gallagher's ergodic theorem* says that for for the (additive) circle `𝕊`, for any sequence of distances `δ`, the set `add_well_approximable 𝕊 δ` is almost empty or almost full. ## TODO: The hypothesis `hδ` in `add_circle.add_well_approximable_ae_empty_or_univ` can be dropped. An elementary (non-measure-theoretic) argument shows that if `¬ hδ` holds then `add_well_approximable 𝕊 δ = univ` (provided `δ` is non-negative). -/ open set filter function metric measure_theory open_locale measure_theory topological_space pointwise /-- In a seminormed group `A`, given `n : ℕ` and `δ : ℝ`, `approx_order_of A n δ` is the set of elements within a distance `δ` of a point of order `n`. -/ @[to_additive approx_add_order_of "In a seminormed additive group `A`, given `n : ℕ` and `δ : ℝ`, `approx_add_order_of A n δ` is the set of elements within a distance `δ` of a point of order `n`."] def approx_order_of (A : Type*) [seminormed_group A] (n : ℕ) (δ : ℝ) : set A := thickening δ {y | order_of y = n} @[to_additive mem_approx_add_order_of_iff] lemma mem_approx_order_of_iff {A : Type*} [seminormed_group A] {n : ℕ} {δ : ℝ} {a : A} : a ∈ approx_order_of A n δ ↔ ∃ (b : A), order_of b = n ∧ a ∈ ball b δ := by simp only [approx_order_of, thickening_eq_bUnion_ball, mem_Union₂, mem_set_of_eq, exists_prop] /-- In a seminormed group `A`, given a sequence of distances `δ₁, δ₂, ...`, `well_approximable A δ` is the limsup as `n → ∞` of the sets `approx_order_of A n δₙ`. Thus, it is the set of points that lie in infinitely many of the sets `approx_order_of A n δₙ`. -/ @[to_additive add_well_approximable "In a seminormed additive group `A`, given a sequence of distances `δ₁, δ₂, ...`, `add_well_approximable A δ` is the limsup as `n → ∞` of the sets `approx_add_order_of A n δₙ`. Thus, it is the set of points that lie in infinitely many of the sets `approx_add_order_of A n δₙ`."] def well_approximable (A : Type*) [seminormed_group A] (δ : ℕ → ℝ) : set A := blimsup (λ n, approx_order_of A n (δ n)) at_top (λ n, 0 < n) @[to_additive mem_add_well_approximable_iff] lemma mem_well_approximable_iff {A : Type*} [seminormed_group A] {δ : ℕ → ℝ} {a : A} : a ∈ well_approximable A δ ↔ a ∈ blimsup (λ n, approx_order_of A n (δ n)) at_top (λ n, 0 < n) := iff.rfl namespace approx_order_of variables {A : Type*} [seminormed_comm_group A] {a : A} {m n : ℕ} (δ : ℝ) @[to_additive] lemma image_pow_subset_of_coprime (hm : 0 < m) (hmn : n.coprime m) : (λ y, y^m) '' (approx_order_of A n δ) ⊆ approx_order_of A n (m * δ) := begin rintros - ⟨a, ha, rfl⟩, obtain ⟨b, hb, hab⟩ := mem_approx_order_of_iff.mp ha, replace hb : b^m ∈ {u : A | order_of u = n}, { rw ← hb at hmn ⊢, exact order_of_pow_coprime hmn }, apply ball_subset_thickening hb ((m : ℝ) • δ), convert pow_mem_ball hm hab using 1, simp only [nsmul_eq_mul, algebra.id.smul_eq_mul], end @[to_additive] lemma image_pow_subset (n : ℕ) (hm : 0 < m) : (λ y, y^m) '' (approx_order_of A (n * m) δ) ⊆ approx_order_of A n (m * δ) := begin rintros - ⟨a, ha, rfl⟩, obtain ⟨b, hb : order_of b = n * m, hab : a ∈ ball b δ⟩ := mem_approx_order_of_iff.mp ha, replace hb : b^m ∈ {y : A | order_of y = n}, { rw [mem_set_of_eq, order_of_pow' b hm.ne', hb, nat.gcd_mul_left_left, n.mul_div_cancel hm], }, apply ball_subset_thickening hb (m * δ), convert pow_mem_ball hm hab, simp only [nsmul_eq_mul], end @[to_additive] lemma smul_subset_of_coprime (han : (order_of a).coprime n) : a • approx_order_of A n δ ⊆ approx_order_of A ((order_of a) * n) δ := begin simp_rw [approx_order_of, thickening_eq_bUnion_ball, ← image_smul, image_Union₂, image_smul, smul_ball'', smul_eq_mul, mem_set_of_eq], refine Union₂_subset_iff.mpr (λ b hb c hc, _), simp only [mem_Union, exists_prop], refine ⟨a * b, _, hc⟩, rw ← hb at ⊢ han, exact (commute.all a b).order_of_mul_eq_mul_order_of_of_coprime han, end @[to_additive vadd_eq_of_mul_dvd] lemma smul_eq_of_mul_dvd (hn : 0 < n) (han : (order_of a)^2 ∣ n) : a • approx_order_of A n δ = approx_order_of A n δ := begin simp_rw [approx_order_of, thickening_eq_bUnion_ball, ← image_smul, image_Union₂, image_smul, smul_ball'', smul_eq_mul, mem_set_of_eq], replace han : ∀ {b : A}, order_of b = n → order_of (a * b) = n, { intros b hb, rw ← hb at han hn, rw sq at han, rwa [(commute.all a b).order_of_mul_eq_right_of_forall_prime_mul_dvd (order_of_pos_iff.mp hn) (λ p hp hp', dvd_trans (mul_dvd_mul_right hp' $ order_of a) han)], }, let f : {b : A | order_of b = n} → {b : A | order_of b = n} := λ b, ⟨a * b, han b.property⟩, have hf : surjective f, { rintros ⟨b, hb⟩, refine ⟨⟨a⁻¹ * b, _⟩, _⟩, { rw [mem_set_of_eq, ← order_of_inv, mul_inv_rev, inv_inv, mul_comm], apply han, simpa, }, { simp only [subtype.mk_eq_mk, subtype.coe_mk, mul_inv_cancel_left], }, }, simpa only [f, mem_set_of_eq, subtype.coe_mk, Union_coe_set] using hf.Union_comp (λ b, ball (b : A) δ), end end approx_order_of namespace unit_add_circle lemma mem_approx_add_order_of_iff {δ : ℝ} {x : unit_add_circle} {n : ℕ} (hn : 0 < n) : x ∈ approx_add_order_of unit_add_circle n δ ↔ ∃ m < n, gcd m n = 1 ∧ ‖x - ↑((m : ℝ) / n)‖ < δ := begin haveI := real.fact_zero_lt_one, simp only [mem_approx_add_order_of_iff, mem_set_of_eq, ball, exists_prop, dist_eq_norm, add_circle.add_order_of_eq_pos_iff hn, mul_one], split, { rintros ⟨y, ⟨m, hm₁, hm₂, rfl⟩, hx⟩, exact ⟨m, hm₁, hm₂, hx⟩, }, { rintros ⟨m, hm₁, hm₂, hx⟩, exact ⟨↑((m : ℝ) / n), ⟨m, hm₁, hm₂, rfl⟩, hx⟩, }, end lemma mem_add_well_approximable_iff (δ : ℕ → ℝ) (x : unit_add_circle) : x ∈ add_well_approximable unit_add_circle δ ↔ {n : ℕ | ∃ m < n, gcd m n = 1 ∧ ‖x - ↑((m : ℝ) / n)‖ < δ n}.infinite := begin simp only [mem_add_well_approximable_iff, ← nat.cofinite_eq_at_top, cofinite.blimsup_set_eq, mem_set_of_eq], refine iff_of_eq (congr_arg set.infinite $ ext (λ n, ⟨λ hn, _, λ hn, _⟩)), { exact (mem_approx_add_order_of_iff hn.1).mp hn.2, }, { have h : 0 < n := by { obtain ⟨m, hm₁, hm₂, hm₃⟩ := hn, exact pos_of_gt hm₁, }, exact ⟨h, (mem_approx_add_order_of_iff h).mpr hn⟩, }, end end unit_add_circle namespace add_circle variables {T : ℝ} [hT : fact (0 < T)] include hT local notation a `∤` b := ¬ a ∣ b local notation a `∣∣` b := (a ∣ b) ∧ (a*a ∤ b) local notation `𝕊` := add_circle T /-- *Gallagher's ergodic theorem* on Diophantine approximation. -/ theorem add_well_approximable_ae_empty_or_univ (δ : ℕ → ℝ) (hδ : tendsto δ at_top (𝓝 0)) : (∀ᵐ x, ¬ add_well_approximable 𝕊 δ x) ∨ ∀ᵐ x, add_well_approximable 𝕊 δ x := begin /- Sketch of proof: Let `E := add_well_approximable 𝕊 δ`. For each prime `p : ℕ`, we can partition `E` into three pieces `E = (A p) ∪ (B p) ∪ (C p)` where: `A p = blimsup (approx_add_order_of 𝕊 n (δ n)) at_top (λ n, 0 < n ∧ (p ∤ n))` `B p = blimsup (approx_add_order_of 𝕊 n (δ n)) at_top (λ n, 0 < n ∧ (p ∣∣ n))` `C p = blimsup (approx_add_order_of 𝕊 n (δ n)) at_top (λ n, 0 < n ∧ (p*p ∣ n))`. (In other words, `A p` is the set of points `x` for which there exist infinitely-many `n` such that `x` is within a distance `δ n` of a point of order `n` and `p ∤ n`. Similarly for `B`, `C`.) These sets have the following key properties: 1. `A p` is almost invariant under the ergodic map `y ↦ p • y` 2. `B p` is almost invariant under the ergodic map `y ↦ p • y + 1/p` 3. `C p` is invariant under the map `y ↦ y + 1/p` To prove 1 and 2 we need the key result `blimsup_thickening_mul_ae_eq` but 3 is elementary. It follows from `add_circle.ergodic_nsmul_add` and `ergodic.ae_empty_or_univ_of_image_ae_le` that if either `A p` or `B p` is not almost empty for any `p`, then it is almost full and thus so is `E`. We may therefore assume that both `A p` and `B p` are almost empty for all `p`. We thus have `E` is almost equal to `C p` for every prime. Combining this with 3 we find that `E` is almost invariant under the map `y ↦ y + 1/p` for every prime `p`. The required result then follows from `add_circle.ae_empty_or_univ_of_forall_vadd_ae_eq_self`. -/ letI : semilattice_sup nat.primes := nat.subtype.semilattice_sup _, set μ : measure 𝕊 := volume, set u : nat.primes → 𝕊 := λ p, ↑(((↑(1 : ℕ) : ℝ) / p) * T), have hu₀ : ∀ (p : nat.primes), add_order_of (u p) = (p : ℕ), { rintros ⟨p, hp⟩, exact add_order_of_div_of_gcd_eq_one hp.pos (gcd_one_left p), }, have hu : tendsto (add_order_of ∘ u) at_top at_top, { rw (funext hu₀ : add_order_of ∘ u = coe), have h_mono : monotone (coe : nat.primes → ℕ) := λ p q hpq, hpq, refine h_mono.tendsto_at_top_at_top (λ n, _), obtain ⟨p, hp, hp'⟩ := n.exists_infinite_primes, exact ⟨⟨p, hp'⟩, hp⟩, }, set E := add_well_approximable 𝕊 δ, set X : ℕ → set 𝕊 := λ n, approx_add_order_of 𝕊 n (δ n), set A : ℕ → set 𝕊 := λ p, blimsup X at_top (λ n, 0 < n ∧ (p ∤ n)), set B : ℕ → set 𝕊 := λ p, blimsup X at_top (λ n, 0 < n ∧ (p ∣∣ n)), set C : ℕ → set 𝕊 := λ p, blimsup X at_top (λ n, 0 < n ∧ (p^2 ∣ n)), have hA₀ : ∀ p, measurable_set (A p) := λ p, measurable_set.measurable_set_blimsup (λ n hn, is_open_thickening.measurable_set), have hB₀ : ∀ p, measurable_set (B p) := λ p, measurable_set.measurable_set_blimsup (λ n hn, is_open_thickening.measurable_set), have hE₀ : null_measurable_set E μ, { refine (measurable_set.measurable_set_blimsup (λ n hn, is_open.measurable_set _)).null_measurable_set, exact is_open_thickening, }, have hE₁ : ∀ p, E = (A p) ∪ (B p) ∪ (C p), { intros p, simp only [E, add_well_approximable, ← blimsup_or_eq_sup, ← and_or_distrib_left, ← sup_eq_union, sq], congr, refine funext (λ n, propext $ iff_self_and.mpr (λ hn, _)), -- `tauto` can finish from here but unfortunately it's very slow. simp only [(em (p ∣ n)).symm, (em (p*p ∣ n)).symm, or_and_distrib_left, or_true, true_and, or_assoc], }, have hE₂ : ∀ (p : nat.primes), A p =ᵐ[μ] (∅ : set 𝕊) ∧ B p =ᵐ[μ] (∅ : set 𝕊) → E =ᵐ[μ] C p, { rintros p ⟨hA, hB⟩, rw hE₁ p, exact union_ae_eq_right_of_ae_eq_empty ((union_ae_eq_right_of_ae_eq_empty hA).trans hB), }, have hA : ∀ (p : nat.primes), A p =ᵐ[μ] (∅ : set 𝕊) ∨ A p =ᵐ[μ] univ, { rintros ⟨p, hp⟩, let f : 𝕊 → 𝕊 := λ y, (p : ℕ) • y, suffices : f '' (A p) ⊆ blimsup (λ n, approx_add_order_of 𝕊 n (p * δ n)) at_top (λ n, 0 < n ∧ (p ∤ n)), { apply (ergodic_nsmul hp.one_lt).ae_empty_or_univ_of_image_ae_le (hA₀ p), apply (has_subset.subset.eventually_le this).congr eventually_eq.rfl, exact blimsup_thickening_mul_ae_eq μ (λ n, 0 < n ∧ (p ∤ n)) (λ n, {y | add_order_of y = n}) (nat.cast_pos.mpr hp.pos) _ hδ, }, refine (Sup_hom.set_image f).apply_blimsup_le.trans (mono_blimsup $ λ n hn, _), replace hn := nat.coprime_comm.mp (hp.coprime_iff_not_dvd.2 hn.2), exact approx_add_order_of.image_nsmul_subset_of_coprime (δ n) hp.pos hn, }, have hB : ∀ (p : nat.primes), B p =ᵐ[μ] (∅ : set 𝕊) ∨ B p =ᵐ[μ] univ, { rintros ⟨p, hp⟩, let x := u ⟨p, hp⟩, let f : 𝕊 → 𝕊 := λ y, p • y + x, suffices : f '' (B p) ⊆ blimsup (λ n, approx_add_order_of 𝕊 n (p * δ n)) at_top (λ n, 0 < n ∧ (p ∣∣ n)), { apply (ergodic_nsmul_add x hp.one_lt).ae_empty_or_univ_of_image_ae_le (hB₀ p), apply (has_subset.subset.eventually_le this).congr eventually_eq.rfl, exact blimsup_thickening_mul_ae_eq μ (λ n, 0 < n ∧ (p ∣∣ n)) (λ n, {y | add_order_of y = n}) (nat.cast_pos.mpr hp.pos) _ hδ, }, refine (Sup_hom.set_image f).apply_blimsup_le.trans (mono_blimsup _), rintros n ⟨hn, h_div, h_ndiv⟩, have h_cop : (add_order_of x).coprime (n/p), { obtain ⟨q, rfl⟩ := h_div, rw [hu₀, subtype.coe_mk, hp.coprime_iff_not_dvd, q.mul_div_cancel_left hp.pos], exact λ contra, h_ndiv (mul_dvd_mul_left p contra), }, replace h_div : n / p * p = n := nat.div_mul_cancel h_div, have hf : f = (λ y, x + y) ∘ (λ y, p • y), { ext, simp [add_comm x], }, simp_rw [comp_app], rw [le_eq_subset, Sup_hom.set_image_to_fun, hf, image_comp], have := @monotone_image 𝕊 𝕊 (λ y, x + y), specialize this (approx_add_order_of.image_nsmul_subset (δ n) (n/p) hp.pos), simp only [h_div] at this ⊢, refine this.trans _, convert approx_add_order_of.vadd_subset_of_coprime (p * δ n) h_cop, simp only [hu₀, subtype.coe_mk, h_div, mul_comm p], }, change (∀ᵐ x, x ∉ E) ∨ E ∈ volume.ae, rw [← eventually_eq_empty, ← eventually_eq_univ], have hC : ∀ (p : nat.primes), (u p) +ᵥ C p = C p, { intros p, let e := (add_action.to_perm (u p) : equiv.perm 𝕊).to_order_iso_set, change e (C p) = C p, rw [e.apply_blimsup, ← hu₀ p], exact blimsup_congr (eventually_of_forall $ λ n hn, approx_add_order_of.vadd_eq_of_mul_dvd (δ n) hn.1 hn.2), }, by_cases h : ∀ (p : nat.primes), A p =ᵐ[μ] (∅ : set 𝕊) ∧ B p =ᵐ[μ] (∅ : set 𝕊), { replace h : ∀ (p : nat.primes), ((u p) +ᵥ E : set _) =ᵐ[μ] E, { intros p, replace hE₂ : E =ᵐ[μ] C p := hE₂ p (h p), have h_qmp : measure_theory.measure.quasi_measure_preserving ((+ᵥ) (-u p)) μ μ := (measure_preserving_vadd _ μ).quasi_measure_preserving, refine (h_qmp.vadd_ae_eq_of_ae_eq (u p) hE₂).trans (ae_eq_trans _ hE₂.symm), rw hC, }, exact ae_empty_or_univ_of_forall_vadd_ae_eq_self hE₀ h hu, }, { right, simp only [not_forall, not_and_distrib] at h, obtain ⟨p, hp⟩ := h, rw hE₁ p, cases hp, { cases hA p, { contradiction, }, simp only [h, union_ae_eq_univ_of_ae_eq_univ_left], }, { cases hB p, { contradiction, }, simp only [h, union_ae_eq_univ_of_ae_eq_univ_left, union_ae_eq_univ_of_ae_eq_univ_right], } }, end end add_circle
3599b7dd6b51a65c09f330081cbae68209edfc41
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/combinatorics/simple_graph/coloring.lean
d6544452730adce7dd617e046e433da1219c0814
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
15,575
lean
/- Copyright (c) 2021 Arthur Paulino. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Arthur Paulino, Kyle Miller -/ import combinatorics.simple_graph.subgraph import combinatorics.simple_graph.clique import data.nat.lattice import data.setoid.partition import order.antichain /-! # Graph Coloring This module defines colorings of simple graphs (also known as proper colorings in the literature). A graph coloring is the attribution of "colors" to all of its vertices such that adjacent vertices have different colors. A coloring can be represented as a homomorphism into a complete graph, whose vertices represent the colors. ## Main definitions * `G.coloring α` is the type of `α`-colorings of a simple graph `G`, with `α` being the set of available colors. The type is defined to be homomorphisms from `G` into the complete graph on `α`, and colorings have a coercion to `V → α`. * `G.colorable n` is the proposition that `G` is `n`-colorable, which is whether there exists a coloring with at most *n* colors. * `G.chromatic_number` is the minimal `n` such that `G` is `n`-colorable, or `0` if it cannot be colored with finitely many colors. * `C.color_class c` is the set of vertices colored by `c : α` in the coloring `C : G.coloring α`. * `C.color_classes` is the set containing all color classes. ## Todo: * Gather material from: * https://github.com/leanprover-community/mathlib/blob/simple_graph_matching/src/combinatorics/simple_graph/coloring.lean * https://github.com/kmill/lean-graphcoloring/blob/master/src/graph.lean * Trees * Planar graphs * Chromatic polynomials * develop API for partial colorings, likely as colorings of subgraphs (`H.coe.coloring α`) -/ universes u v namespace simple_graph variables {V : Type u} (G : simple_graph V) /-- An `α`-coloring of a simple graph `G` is a homomorphism of `G` into the complete graph on `α`. This is also known as a proper coloring. -/ abbreviation coloring (α : Type v) := G →g (⊤ : simple_graph α) variables {G} {α : Type v} (C : G.coloring α) lemma coloring.valid {v w : V} (h : G.adj v w) : C v ≠ C w := C.map_rel h /-- Construct a term of `simple_graph.coloring` using a function that assigns vertices to colors and a proof that it is as proper coloring. (Note: this is a definitionally the constructor for `simple_graph.hom`, but with a syntactically better proper coloring hypothesis.) -/ @[pattern] def coloring.mk (color : V → α) (valid : ∀ {v w : V}, G.adj v w → color v ≠ color w) : G.coloring α := ⟨color, @valid⟩ /-- The color class of a given color. -/ def coloring.color_class (c : α) : set V := {v : V | C v = c} /-- The set containing all color classes. -/ def coloring.color_classes : set (set V) := (setoid.ker C).classes lemma coloring.mem_color_class (v : V) : v ∈ C.color_class (C v) := by exact rfl lemma coloring.color_classes_is_partition : setoid.is_partition C.color_classes := setoid.is_partition_classes (setoid.ker C) lemma coloring.mem_color_classes {v : V} : C.color_class (C v) ∈ C.color_classes := ⟨v, rfl⟩ lemma coloring.color_classes_finite [finite α] : C.color_classes.finite := set.finite_coe_iff.1 $ setoid.finite_classes_ker _ lemma coloring.card_color_classes_le [fintype α] [fintype C.color_classes] : fintype.card C.color_classes ≤ fintype.card α := setoid.card_classes_ker_le C lemma coloring.not_adj_of_mem_color_class {c : α} {v w : V} (hv : v ∈ C.color_class c) (hw : w ∈ C.color_class c) : ¬G.adj v w := λ h, C.valid h (eq.trans hv (eq.symm hw)) lemma coloring.color_classes_independent (c : α) : is_antichain G.adj (C.color_class c) := λ v hv w hw h, C.not_adj_of_mem_color_class hv hw -- TODO make this computable noncomputable instance [fintype V] [fintype α] : fintype (coloring G α) := begin classical, change fintype (rel_hom G.adj (⊤ : simple_graph α).adj), apply fintype.of_injective _ rel_hom.coe_fn_injective, apply_instance, end variables (G) /-- Whether a graph can be colored by at most `n` colors. -/ def colorable (n : ℕ) : Prop := nonempty (G.coloring (fin n)) /-- The coloring of an empty graph. -/ def coloring_of_is_empty [is_empty V] : G.coloring α := coloring.mk is_empty_elim (λ v, is_empty_elim) lemma colorable_of_is_empty [is_empty V] (n : ℕ) : G.colorable n := ⟨G.coloring_of_is_empty⟩ lemma is_empty_of_colorable_zero (h : G.colorable 0) : is_empty V := begin split, intro v, obtain ⟨i, hi⟩ := h.some v, exact nat.not_lt_zero _ hi, end /-- The "tautological" coloring of a graph, using the vertices of the graph as colors. -/ def self_coloring : G.coloring V := coloring.mk id (λ v w, G.ne_of_adj) /-- The chromatic number of a graph is the minimal number of colors needed to color it. If `G` isn't colorable with finitely many colors, this will be 0. -/ noncomputable def chromatic_number : ℕ := Inf { n : ℕ | G.colorable n } /-- Given an embedding, there is an induced embedding of colorings. -/ def recolor_of_embedding {α β : Type*} (f : α ↪ β) : G.coloring α ↪ G.coloring β := { to_fun := λ C, (embedding.complete_graph f).to_hom.comp C, inj' := begin -- this was strangely painful; seems like missing lemmas about embeddings intros C C' h, dsimp only at h, ext v, apply (embedding.complete_graph f).inj', change ((embedding.complete_graph f).to_hom.comp C) v = _, rw h, refl, end } /-- Given an equivalence, there is an induced equivalence between colorings. -/ def recolor_of_equiv {α β : Type*} (f : α ≃ β) : G.coloring α ≃ G.coloring β := { to_fun := G.recolor_of_embedding f.to_embedding, inv_fun := G.recolor_of_embedding f.symm.to_embedding, left_inv := λ C, by { ext v, apply equiv.symm_apply_apply }, right_inv := λ C, by { ext v, apply equiv.apply_symm_apply } } /-- There is a noncomputable embedding of `α`-colorings to `β`-colorings if `β` has at least as large a cardinality as `α`. -/ noncomputable def recolor_of_card_le {α β : Type*} [fintype α] [fintype β] (hn : fintype.card α ≤ fintype.card β) : G.coloring α ↪ G.coloring β := G.recolor_of_embedding $ (function.embedding.nonempty_of_card_le hn).some variables {G} lemma colorable.mono {n m : ℕ} (h : n ≤ m) (hc : G.colorable n) : G.colorable m := ⟨G.recolor_of_card_le (by simp [h]) hc.some⟩ lemma coloring.to_colorable [fintype α] (C : G.coloring α) : G.colorable (fintype.card α) := ⟨G.recolor_of_card_le (by simp) C⟩ lemma colorable_of_fintype (G : simple_graph V) [fintype V] : G.colorable (fintype.card V) := G.self_coloring.to_colorable /-- Noncomputably get a coloring from colorability. -/ noncomputable def colorable.to_coloring [fintype α] {n : ℕ} (hc : G.colorable n) (hn : n ≤ fintype.card α) : G.coloring α := begin rw ←fintype.card_fin n at hn, exact G.recolor_of_card_le hn hc.some, end lemma colorable.of_embedding {V' : Type*} {G' : simple_graph V'} (f : G ↪g G') {n : ℕ} (h : G'.colorable n) : G.colorable n := ⟨(h.to_coloring (by simp)).comp f⟩ lemma colorable_iff_exists_bdd_nat_coloring (n : ℕ) : G.colorable n ↔ ∃ (C : G.coloring ℕ), ∀ v, C v < n := begin split, { rintro hc, have C : G.coloring (fin n) := hc.to_coloring (by simp), let f := embedding.complete_graph (fin.coe_embedding n).to_embedding, use f.to_hom.comp C, intro v, cases C with color valid, exact fin.is_lt (color v), }, { rintro ⟨C, Cf⟩, refine ⟨coloring.mk _ _⟩, { exact λ v, ⟨C v, Cf v⟩, }, { rintro v w hvw, simp only [subtype.mk_eq_mk, ne.def], exact C.valid hvw, } } end lemma colorable_set_nonempty_of_colorable {n : ℕ} (hc : G.colorable n) : {n : ℕ | G.colorable n}.nonempty := ⟨n, hc⟩ lemma chromatic_number_bdd_below : bdd_below {n : ℕ | G.colorable n} := ⟨0, λ _ _, zero_le _⟩ lemma chromatic_number_le_of_colorable {n : ℕ} (hc : G.colorable n) : G.chromatic_number ≤ n := begin rw chromatic_number, apply cInf_le chromatic_number_bdd_below, fsplit, exact classical.choice hc, end lemma chromatic_number_le_card [fintype α] (C : G.coloring α) : G.chromatic_number ≤ fintype.card α := cInf_le chromatic_number_bdd_below C.to_colorable lemma colorable_chromatic_number {m : ℕ} (hc : G.colorable m) : G.colorable G.chromatic_number := begin dsimp only [chromatic_number], rw nat.Inf_def, apply nat.find_spec, exact colorable_set_nonempty_of_colorable hc, end lemma colorable_chromatic_number_of_fintype (G : simple_graph V) [finite V] : G.colorable G.chromatic_number := by { casesI nonempty_fintype V, exact colorable_chromatic_number G.colorable_of_fintype } lemma chromatic_number_le_one_of_subsingleton (G : simple_graph V) [subsingleton V] : G.chromatic_number ≤ 1 := begin rw chromatic_number, apply cInf_le chromatic_number_bdd_below, fsplit, refine coloring.mk (λ _, 0) _, intros v w, rw subsingleton.elim v w, simp, end lemma chromatic_number_eq_zero_of_isempty (G : simple_graph V) [is_empty V] : G.chromatic_number = 0 := begin rw ←nonpos_iff_eq_zero, apply cInf_le chromatic_number_bdd_below, apply colorable_of_is_empty, end lemma is_empty_of_chromatic_number_eq_zero (G : simple_graph V) [finite V] (h : G.chromatic_number = 0) : is_empty V := begin have h' := G.colorable_chromatic_number_of_fintype, rw h at h', exact G.is_empty_of_colorable_zero h', end lemma chromatic_number_pos [nonempty V] {n : ℕ} (hc : G.colorable n) : 0 < G.chromatic_number := begin apply le_cInf (colorable_set_nonempty_of_colorable hc), intros m hm, by_contra h', simp only [not_le, nat.lt_one_iff] at h', subst h', obtain ⟨i, hi⟩ := hm.some (classical.arbitrary V), exact nat.not_lt_zero _ hi, end lemma colorable_of_chromatic_number_pos (h : 0 < G.chromatic_number) : G.colorable G.chromatic_number := begin obtain ⟨h, hn⟩ := nat.nonempty_of_pos_Inf h, exact colorable_chromatic_number hn, end lemma colorable.mono_left {G' : simple_graph V} (h : G ≤ G') {n : ℕ} (hc : G'.colorable n) : G.colorable n := ⟨hc.some.comp (hom.map_spanning_subgraphs h)⟩ lemma colorable.chromatic_number_le_of_forall_imp {V' : Type*} {G' : simple_graph V'} {m : ℕ} (hc : G'.colorable m) (h : ∀ n, G'.colorable n → G.colorable n) : G.chromatic_number ≤ G'.chromatic_number := begin apply cInf_le chromatic_number_bdd_below, apply h, apply colorable_chromatic_number hc, end lemma colorable.chromatic_number_mono (G' : simple_graph V) {m : ℕ} (hc : G'.colorable m) (h : G ≤ G') : G.chromatic_number ≤ G'.chromatic_number := hc.chromatic_number_le_of_forall_imp (λ n, colorable.mono_left h) lemma colorable.chromatic_number_mono_of_embedding {V' : Type*} {G' : simple_graph V'} {n : ℕ} (h : G'.colorable n) (f : G ↪g G') : G.chromatic_number ≤ G'.chromatic_number := h.chromatic_number_le_of_forall_imp (λ _, colorable.of_embedding f) lemma chromatic_number_eq_card_of_forall_surj [fintype α] (C : G.coloring α) (h : ∀ (C' : G.coloring α), function.surjective C') : G.chromatic_number = fintype.card α := begin apply le_antisymm, { apply chromatic_number_le_card C, }, { by_contra hc, rw not_le at hc, obtain ⟨n, cn, hc⟩ := exists_lt_of_cInf_lt (colorable_set_nonempty_of_colorable C.to_colorable) hc, rw ←fintype.card_fin n at hc, have f := (function.embedding.nonempty_of_card_le (le_of_lt hc)).some, have C' := cn.some, specialize h (G.recolor_of_embedding f C'), change function.surjective (f ∘ C') at h, have h1 : function.surjective f := function.surjective.of_comp h, have h2 := fintype.card_le_of_surjective _ h1, exact nat.lt_le_antisymm hc h2, }, end lemma chromatic_number_bot [nonempty V] : (⊥ : simple_graph V).chromatic_number = 1 := begin let C : (⊥ : simple_graph V).coloring (fin 1) := coloring.mk (λ _, 0) (λ v w h, false.elim h), apply le_antisymm, { exact chromatic_number_le_card C, }, { exact chromatic_number_pos C.to_colorable, }, end @[simp] lemma chromatic_number_top [fintype V] : (⊤ : simple_graph V).chromatic_number = fintype.card V := begin apply chromatic_number_eq_card_of_forall_surj (self_coloring _), intro C, rw ←finite.injective_iff_surjective, intros v w, contrapose, intro h, exact C.valid h, end lemma chromatic_number_top_eq_zero_of_infinite (V : Type*) [infinite V] : (⊤ : simple_graph V).chromatic_number = 0 := begin let n := (⊤ : simple_graph V).chromatic_number, by_contra hc, replace hc := pos_iff_ne_zero.mpr hc, apply nat.not_succ_le_self n, convert_to (⊤ : simple_graph {m | m < n + 1}).chromatic_number ≤ _, { simp, }, refine (colorable_of_chromatic_number_pos hc).chromatic_number_mono_of_embedding _, apply embedding.complete_graph, exact (function.embedding.subtype _).trans (infinite.nat_embedding V), end /-- The bicoloring of a complete bipartite graph using whether a vertex is on the left or on the right. -/ def complete_bipartite_graph.bicoloring (V W : Type*) : (complete_bipartite_graph V W).coloring bool := coloring.mk (λ v, v.is_right) begin intros v w, cases v; cases w; simp, end lemma complete_bipartite_graph.chromatic_number {V W : Type*} [nonempty V] [nonempty W] : (complete_bipartite_graph V W).chromatic_number = 2 := begin apply chromatic_number_eq_card_of_forall_surj (complete_bipartite_graph.bicoloring V W), intros C b, have v := classical.arbitrary V, have w := classical.arbitrary W, have h : (complete_bipartite_graph V W).adj (sum.inl v) (sum.inr w) := by simp, have hn := C.valid h, by_cases he : C (sum.inl v) = b, { exact ⟨_, he⟩ }, { by_cases he' : C (sum.inr w) = b, { exact ⟨_, he'⟩ }, { exfalso, cases b; simp only [eq_tt_eq_not_eq_ff, eq_ff_eq_not_eq_tt] at he he'; rw [he, he'] at hn; contradiction }, }, end /-! ### Cliques -/ lemma is_clique.card_le_of_coloring {s : finset V} (h : G.is_clique s) [fintype α] (C : G.coloring α) : s.card ≤ fintype.card α := begin rw is_clique_iff_induce_eq at h, have f : G.induce ↑s ↪g G := embedding.induce ↑s, rw h at f, convert fintype.card_le_of_injective _ (C.comp f.to_hom).injective_of_top_hom using 1, simp, end lemma is_clique.card_le_of_colorable {s : finset V} (h : G.is_clique s) {n : ℕ} (hc : G.colorable n) : s.card ≤ n := begin convert h.card_le_of_coloring hc.some, simp, end -- TODO eliminate `finite V` constraint once chromatic numbers are refactored. -- This is just to ensure the chromatic number exists. lemma is_clique.card_le_chromatic_number [finite V] {s : finset V} (h : G.is_clique s) : s.card ≤ G.chromatic_number := by { casesI nonempty_fintype V, exact h.card_le_of_colorable G.colorable_chromatic_number_of_fintype } protected lemma colorable.clique_free {n m : ℕ} (hc : G.colorable n) (hm : n < m) : G.clique_free m := begin by_contra h, simp only [clique_free, is_n_clique_iff, not_forall, not_not] at h, obtain ⟨s, h, rfl⟩ := h, exact nat.lt_le_antisymm hm (h.card_le_of_colorable hc), end -- TODO eliminate `finite V` constraint once chromatic numbers are refactored. -- This is just to ensure the chromatic number exists. lemma clique_free_of_chromatic_number_lt [finite V] {n : ℕ} (hc : G.chromatic_number < n) : G.clique_free n := G.colorable_chromatic_number_of_fintype.clique_free hc end simple_graph
e5c584328ec6ab15e6c5de813595c3734933b11c
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/rw_set4.lean
274962455f741bd1592607fb5589deb8ddc3876b
[ "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
423
lean
open tactic attribute [congr, priority std.priority.default+1] theorem forall_congr_prop_eq {P₁ P₂ Q₁ Q₂ : Prop} : P₁ = P₂ → (P₂ → Q₁ = Q₂) → (P₁ → Q₁) = (P₂ → Q₂) := sorry #print [congr] default example (A : Type) (a b c : A) : (a = b) → (a = c) → a = b := by simp {contextual := tt} example (A : Type) (a b c : A) : (a = c) → (a = b) → a = b := by simp {contextual := tt}
e0813f1bb76b0ce4cee197efb4c5cb41b39ecbcb
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/logic/relation.lean
995a7ec1905616805d9917036c6a85e0f2b45d56
[ "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
18,313
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 tactic.basic /-! # Relation closures This file defines the reflexive, transitive, and reflexive transitive closures of relations. It also proves some basic results on definitions in core, such as `eqv_gen`. Note that this is about unbundled relations, that is terms of types of the form `α → β → Prop`. For the bundled version, see `rel`. ## Definitions * `relation.refl_gen`: Reflexive closure. `refl_gen r` relates everything `r` related, plus for all `a` it relates `a` with itself. So `refl_gen r a b ↔ r a b ∨ a = b`. * `relation.trans_gen`: Transitive closure. `trans_gen r` relates everything `r` related transitively. So `trans_gen r a b ↔ ∃ x₀ ... xₙ, r a x₀ ∧ r x₀ x₁ ∧ ... ∧ r xₙ b`. * `relation.refl_trans_gen`: Reflexive transitive closure. `refl_trans_gen r` relates everything `r` related transitively, plus for all `a` it relates `a` with itself. So `refl_trans_gen r a b ↔ (∃ x₀ ... xₙ, r a x₀ ∧ r x₀ x₁ ∧ ... ∧ r xₙ b) ∨ a = b`. It is the same as the reflexive closure of the transitive closure, or the transitive closure of the reflexive closure. In terms of rewriting systems, this means that `a` can be rewritten to `b` in a number of rewrites. * `relation.comp`: Relation composition. We provide notation `∘r`. For `r : α → β → Prop` and `s : β → γ → Prop`, `r ∘r s`relates `a : α` and `c : γ` iff there exists `b : β` that's related to both. * `relation.map`: Image of a relation under a pair of maps. For `r : α → β → Prop`, `f : α → γ`, `g : β → δ`, `map r f g` is the relation `γ → δ → Prop` relating `f a` and `g b` for all `a`, `b` related by `r`. * `relation.join`: Join of a relation. For `r : α → α → Prop`, `join r a b ↔ ∃ c, r a c ∧ r b c`. In terms of rewriting systems, this means that `a` and `b` can be rewritten to the same term. -/ variables {α β γ δ : Type*} section ne_imp variable {r : α → α → Prop} lemma is_refl.reflexive [is_refl α r] : reflexive r := λ x, is_refl.refl x /-- To show a reflexive relation `r : α → α → Prop` holds over `x y : α`, it suffices to show it holds when `x ≠ y`. -/ lemma reflexive.rel_of_ne_imp (h : reflexive r) {x y : α} (hr : x ≠ y → r x y) : r x y := begin by_cases hxy : x = y, { exact hxy ▸ h x }, { exact hr hxy } end /-- If a reflexive relation `r : α → α → Prop` holds over `x y : α`, then it holds whether or not `x ≠ y`. -/ lemma reflexive.ne_imp_iff (h : reflexive r) {x y : α} : (x ≠ y → r x y) ↔ r x y := ⟨h.rel_of_ne_imp, λ hr _, hr⟩ /-- If a reflexive relation `r : α → α → Prop` holds over `x y : α`, then it holds whether or not `x ≠ y`. Unlike `reflexive.ne_imp_iff`, this uses `[is_refl α r]`. -/ lemma reflexive_ne_imp_iff [is_refl α r] {x y : α} : (x ≠ y → r x y) ↔ r x y := is_refl.reflexive.ne_imp_iff protected lemma symmetric.iff (H : symmetric r) (x y : α) : r x y ↔ r y x := ⟨λ h, H h, λ h, H h⟩ end ne_imp section comap variables {r : β → β → Prop} lemma reflexive.comap (h : reflexive r) (f : α → β) : reflexive (r on f) := λ a, h (f a) lemma symmetric.comap (h : symmetric r) (f : α → β) : symmetric (r on f) := λ a b hab, h hab lemma transitive.comap (h : transitive r) (f : α → β) : transitive (r on f) := λ a b c hab hbc, h hab hbc end comap namespace relation section comp variables {r : α → β → Prop} {p : β → γ → Prop} {q : γ → δ → Prop} /-- The composition of two relations, yielding a new relation. The result relates a term of `α` and a term of `γ` if there is an intermediate term of `β` related to both. -/ def comp (r : α → β → Prop) (p : β → γ → Prop) (a : α) (c : γ) : Prop := ∃ b, r a b ∧ p b c local infixr ` ∘r ` : 80 := relation.comp lemma comp_eq : r ∘r (=) = r := funext $ λ a, funext $ λ b, propext $ iff.intro (λ ⟨c, h, eq⟩, eq ▸ h) (λ h, ⟨b, h, rfl⟩) lemma eq_comp : (=) ∘r r = r := funext $ λ a, funext $ λ b, propext $ iff.intro (λ ⟨c, eq, h⟩, eq.symm ▸ h) (λ h, ⟨a, rfl, h⟩) lemma iff_comp {r : Prop → α → Prop} : (↔) ∘r r = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, eq_comp] lemma comp_iff {r : α → Prop → Prop} : r ∘r (↔) = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, comp_eq] lemma comp_assoc : (r ∘r p) ∘r q = r ∘r p ∘r q := begin funext a d, apply propext, split, exact λ ⟨c, ⟨b, hab, hbc⟩, hcd⟩, ⟨b, hab, c, hbc, hcd⟩, exact λ ⟨b, hab, c, hbc, hcd⟩, ⟨c, ⟨b, hab, hbc⟩, hcd⟩ end lemma flip_comp : flip (r ∘r p) = (flip p) ∘r (flip r) := begin funext c a, apply propext, split, exact λ ⟨b, hab, hbc⟩, ⟨b, hbc, hab⟩, exact λ ⟨b, hbc, hab⟩, ⟨b, hab, hbc⟩ end end comp /-- The map of a relation `r` through a pair of functions pushes the relation to the codomains of the functions. The resulting relation is defined by having pairs of terms related if they have preimages related by `r`. -/ protected def map (r : α → β → Prop) (f : α → γ) (g : β → δ) : γ → δ → Prop := λ c d, ∃ a b, r a b ∧ f a = c ∧ g b = d variables {r : α → α → Prop} {a b c d : α} /-- `refl_trans_gen r`: reflexive transitive closure of `r` -/ @[mk_iff relation.refl_trans_gen.cases_tail_iff] inductive refl_trans_gen (r : α → α → Prop) (a : α) : α → Prop | refl : refl_trans_gen a | tail {b c} : refl_trans_gen b → r b c → refl_trans_gen c attribute [refl] refl_trans_gen.refl /-- `refl_gen r`: reflexive closure of `r` -/ @[mk_iff] inductive refl_gen (r : α → α → Prop) (a : α) : α → Prop | refl : refl_gen a | single {b} : r a b → refl_gen b /-- `trans_gen r`: transitive closure of `r` -/ @[mk_iff] inductive trans_gen (r : α → α → Prop) (a : α) : α → Prop | single {b} : r a b → trans_gen b | tail {b c} : trans_gen b → r b c → trans_gen c attribute [refl] refl_gen.refl lemma refl_gen.to_refl_trans_gen : ∀ {a b}, refl_gen r a b → refl_trans_gen r a b | a _ refl_gen.refl := by refl | a b (refl_gen.single h) := refl_trans_gen.tail refl_trans_gen.refl h namespace refl_trans_gen @[trans] lemma trans (hab : refl_trans_gen r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma single (hab : r a b) : refl_trans_gen r a b := refl.tail hab lemma head (hab : r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { exact refl.tail hab }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma symmetric (h : symmetric r) : symmetric (refl_trans_gen r) := begin intros x y h, induction h with z w a b c, { refl }, { apply relation.refl_trans_gen.head (h b) c } end lemma cases_tail : refl_trans_gen r a b → b = a ∨ (∃ c, refl_trans_gen r a c ∧ r c b) := (cases_tail_iff r a b).1 @[elab_as_eliminator] lemma head_induction_on {P : ∀ (a:α), refl_trans_gen r a b → Prop} {a : α} (h : refl_trans_gen r a b) (refl : P b refl) (head : ∀ {a c} (h' : r a c) (h : refl_trans_gen r c b), P c h → P a (h.head h')) : P a h := begin induction h generalizing P, case refl_trans_gen.refl { exact refl }, case refl_trans_gen.tail : b c hab hbc ih { apply ih, show P b _, from head hbc _ refl, show ∀ a a', r a a' → refl_trans_gen r a' b → P a' _ → P a _, from λ a a' hab hbc, head hab _ } end @[elab_as_eliminator] lemma trans_induction_on {P : ∀ {a b : α}, refl_trans_gen r a b → Prop} {a b : α} (h : refl_trans_gen r a b) (ih₁ : ∀ a, @P a a refl) (ih₂ : ∀ {a b} (h : r a b), P (single h)) (ih₃ : ∀ {a b c} (h₁ : refl_trans_gen r a b) (h₂ : refl_trans_gen r b c), P h₁ → P h₂ → P (h₁.trans h₂)) : P h := begin induction h, case refl_trans_gen.refl { exact ih₁ a }, case refl_trans_gen.tail : b c hab hbc ih { exact ih₃ hab (single hbc) ih (ih₂ hbc) } end lemma cases_head (h : refl_trans_gen r a b) : a = b ∨ (∃ c, r a c ∧ refl_trans_gen r c b) := begin induction h using relation.refl_trans_gen.head_induction_on, { left, refl }, { right, existsi _, split; assumption } end lemma cases_head_iff : refl_trans_gen r a b ↔ a = b ∨ (∃ c, r a c ∧ refl_trans_gen r c b) := begin use cases_head, rintro (rfl | ⟨c, hac, hcb⟩), { refl }, { exact head hac hcb } end lemma total_of_right_unique (U : relator.right_unique r) (ab : refl_trans_gen r a b) (ac : refl_trans_gen r a c) : refl_trans_gen r b c ∨ refl_trans_gen r c b := begin induction ab with b d ab bd IH, { exact or.inl ac }, { rcases IH with IH | IH, { rcases cases_head IH with rfl | ⟨e, be, ec⟩, { exact or.inr (single bd) }, { cases U.unique bd be, exact or.inl ec } }, { exact or.inr (IH.tail bd) } } end end refl_trans_gen namespace trans_gen lemma to_refl {a b} (h : trans_gen r a b) : refl_trans_gen r a b := begin induction h with b h b c _ bc ab, exact refl_trans_gen.single h, exact refl_trans_gen.tail ab bc end @[trans] lemma trans_left (hab : trans_gen r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := begin induction hbc, case refl_trans_gen.refl : { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end @[trans] lemma trans (hab : trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := trans_left hab hbc.to_refl lemma head' (hab : r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := trans_left (single hab) hbc lemma tail' (hab : refl_trans_gen r a b) (hbc : r b c) : trans_gen r a c := begin induction hab generalizing c, case refl_trans_gen.refl : c hac { exact single hac }, case refl_trans_gen.tail : d b hab hdb IH { exact tail (IH hdb) hbc } end @[trans] lemma trans_right (hab : refl_trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := begin induction hbc, case trans_gen.single : c hbc { exact tail' hab hbc }, case trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma head (hab : r a b) (hbc : trans_gen r b c) : trans_gen r a c := head' hab hbc.to_refl lemma tail'_iff : trans_gen r a c ↔ ∃ b, refl_trans_gen r a b ∧ r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, tail' hab hbc⟩, cases h with _ hac b _ hab hbc, { exact ⟨_, by refl, hac⟩ }, { exact ⟨_, hab.to_refl, hbc⟩ } end lemma head'_iff : trans_gen r a c ↔ ∃ b, r a b ∧ refl_trans_gen r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, head' hab hbc⟩, induction h, case trans_gen.single : c hac { exact ⟨_, hac, by refl⟩ }, case trans_gen.tail : b c hab hbc IH { rcases IH with ⟨d, had, hdb⟩, exact ⟨_, had, hdb.tail hbc⟩ } end lemma trans_gen_eq_self (trans : transitive r) : trans_gen r = r := funext $ λ a, funext $ λ b, propext $ ⟨λ h, begin induction h, case trans_gen.single : c hc { exact hc }, case trans_gen.tail : c d hac hcd hac { exact trans hac hcd } end, trans_gen.single⟩ lemma transitive_trans_gen : transitive (trans_gen r) := λ a b c, trans lemma trans_gen_idem : trans_gen (trans_gen r) = trans_gen r := trans_gen_eq_self transitive_trans_gen lemma trans_gen_lift {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → p (f a) (f b)) (hab : trans_gen r a b) : trans_gen p (f a) (f b) := begin induction hab, case trans_gen.single : c hac { exact trans_gen.single (h a c hac) }, case trans_gen.tail : c d hac hcd hac { exact trans_gen.tail hac (h c d hcd) } end lemma trans_gen_lift' {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → trans_gen p (f a) (f b)) (hab : trans_gen r a b) : trans_gen p (f a) (f b) := by simpa [trans_gen_idem] using trans_gen_lift f h hab lemma trans_gen_closed {p : α → α → Prop} : (∀ a b, r a b → trans_gen p a b) → trans_gen r a b → trans_gen p a b := trans_gen_lift' id end trans_gen section refl_trans_gen open refl_trans_gen lemma refl_trans_gen_iff_eq (h : ∀ b, ¬ r a b) : refl_trans_gen r a b ↔ b = a := by rw [cases_head_iff]; simp [h, eq_comm] lemma refl_trans_gen_iff_eq_or_trans_gen : refl_trans_gen r a b ↔ b = a ∨ trans_gen r a b := begin refine ⟨λ h, _, λ h, _⟩, { cases h with c _ hac hcb, { exact or.inl rfl }, { exact or.inr (trans_gen.tail' hac hcb) } }, { rcases h with rfl | h, {refl}, {exact h.to_refl} } end lemma refl_trans_gen_lift {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := refl_trans_gen.trans_induction_on hab (λ a, refl) (λ a b, refl_trans_gen.single ∘ h _ _) (λ a b c _ _, trans) lemma refl_trans_gen_mono {p : α → α → Prop} : (∀ a b, r a b → p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen_lift id lemma refl_trans_gen_eq_self (refl : reflexive r) (trans : transitive r) : refl_trans_gen r = r := funext $ λ a, funext $ λ b, propext $ ⟨λ h, begin induction h with b c h₁ h₂ IH, {apply refl}, exact trans IH h₂, end, single⟩ lemma reflexive_refl_trans_gen : reflexive (refl_trans_gen r) := λ a, refl lemma transitive_refl_trans_gen : transitive (refl_trans_gen r) := λ a b c, trans lemma refl_trans_gen_idem : refl_trans_gen (refl_trans_gen r) = refl_trans_gen r := refl_trans_gen_eq_self reflexive_refl_trans_gen transitive_refl_trans_gen lemma refl_trans_gen_lift' {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → refl_trans_gen p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := by simpa [refl_trans_gen_idem] using refl_trans_gen_lift f h hab lemma refl_trans_gen_closed {p : α → α → Prop} : (∀ a b, r a b → refl_trans_gen p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen_lift' id end refl_trans_gen /-- The join of a relation on a single type is a new relation for which pairs of terms are related if there is a third term they are both related to. For example, if `r` is a relation representing rewrites in a term rewriting system, then *confluence* is the property that if `a` rewrites to both `b` and `c`, then `join r` relates `b` and `c` (see `relation.church_rosser`). -/ def join (r : α → α → Prop) : α → α → Prop := λ a b, ∃ c, r a c ∧ r b c section join open refl_trans_gen refl_gen /-- A sufficient condition for the Church-Rosser property. -/ lemma church_rosser (h : ∀ a b c, r a b → r a c → ∃ d, refl_gen r b d ∧ refl_trans_gen r c d) (hab : refl_trans_gen r a b) (hac : refl_trans_gen r a c) : join (refl_trans_gen r) b c := begin induction hab, case refl_trans_gen.refl { exact ⟨c, hac, refl⟩ }, case refl_trans_gen.tail : d e had hde ih { clear hac had a, rcases ih with ⟨b, hdb, hcb⟩, have : ∃ a, refl_trans_gen r e a ∧ refl_gen r b a, { clear hcb, induction hdb, case refl_trans_gen.refl { exact ⟨e, refl, refl_gen.single hde⟩ }, case refl_trans_gen.tail : f b hdf hfb ih { rcases ih with ⟨a, hea, hfa⟩, cases hfa with _ hfa, { exact ⟨b, hea.tail hfb, refl_gen.refl⟩ }, { rcases h _ _ _ hfb hfa with ⟨c, hbc, hac⟩, exact ⟨c, hea.trans hac, hbc⟩ } } }, rcases this with ⟨a, hea, hba⟩, cases hba with _ hba, { exact ⟨b, hea, hcb⟩ }, { exact ⟨a, hea, hcb.tail hba⟩ } } end lemma join_of_single (h : reflexive r) (hab : r a b) : join r a b := ⟨b, hab, h b⟩ lemma symmetric_join : symmetric (join r) := λ a b ⟨c, hac, hcb⟩, ⟨c, hcb, hac⟩ lemma reflexive_join (h : reflexive r) : reflexive (join r) := λ a, ⟨a, h a, h a⟩ lemma transitive_join (ht : transitive r) (h : ∀ a b c, r a b → r a c → join r b c) : transitive (join r) := λ a b c ⟨x, hax, hbx⟩ ⟨y, hby, hcy⟩, let ⟨z, hxz, hyz⟩ := h b x y hbx hby in ⟨z, ht hax hxz, ht hcy hyz⟩ lemma equivalence_join (hr : reflexive r) (ht : transitive r) (h : ∀ a b c, r a b → r a c → join r b c) : equivalence (join r) := ⟨reflexive_join hr, symmetric_join, transitive_join ht h⟩ lemma equivalence_join_refl_trans_gen (h : ∀ a b c, r a b → r a c → ∃ d, refl_gen r b d ∧ refl_trans_gen r c d) : equivalence (join (refl_trans_gen r)) := equivalence_join reflexive_refl_trans_gen transitive_refl_trans_gen (λ a b c, church_rosser h) lemma join_of_equivalence {r' : α → α → Prop} (hr : equivalence r) (h : ∀ a b, r' a b → r a b) : join r' a b → r a b | ⟨c, hac, hbc⟩ := hr.2.2 (h _ _ hac) (hr.2.1 $ h _ _ hbc) lemma refl_trans_gen_of_transitive_reflexive {r' : α → α → Prop} (hr : reflexive r) (ht : transitive r) (h : ∀ a b, r' a b → r a b) (h' : refl_trans_gen r' a b) : r a b := begin induction h' with b c hab hbc ih, { exact hr _ }, { exact ht ih (h _ _ hbc) } end lemma refl_trans_gen_of_equivalence {r' : α → α → Prop} (hr : equivalence r) : (∀ a b, r' a b → r a b) → refl_trans_gen r' a b → r a b := refl_trans_gen_of_transitive_reflexive hr.1 hr.2.2 end join section eqv_gen lemma eqv_gen_iff_of_equivalence (h : equivalence r) : eqv_gen r a b ↔ r a b := iff.intro begin intro h, induction h, case eqv_gen.rel { assumption }, case eqv_gen.refl { exact h.1 _ }, case eqv_gen.symm { apply h.2.1, assumption }, case eqv_gen.trans : a b c _ _ hab hbc { exact h.2.2 hab hbc } end (eqv_gen.rel a b) lemma eqv_gen_mono {r p : α → α → Prop} (hrp : ∀ a b, r a b → p a b) (h : eqv_gen r a b) : eqv_gen p a b := begin induction h, case eqv_gen.rel : a b h { exact eqv_gen.rel _ _ (hrp _ _ h) }, case eqv_gen.refl : { exact eqv_gen.refl _ }, case eqv_gen.symm : a b h ih { exact eqv_gen.symm _ _ ih }, case eqv_gen.trans : a b c ih1 ih2 hab hbc { exact eqv_gen.trans _ _ _ hab hbc } end end eqv_gen end relation
0c598868587ff4f32d4a3eb5cd22b43c65bfc142
92b50235facfbc08dfe7f334827d47281471333b
/library/data/list/comb.lean
42e1bf5b9ceb9c1a7d96192636c8d0c02048a8fc
[ "Apache-2.0" ]
permissive
htzh/lean
24f6ed7510ab637379ec31af406d12584d31792c
d70c79f4e30aafecdfc4a60b5d3512199200ab6e
refs/heads/master
1,607,677,731,270
1,437,089,952,000
1,437,089,952,000
37,078,816
0
0
null
1,433,780,956,000
1,433,780,955,000
null
UTF-8
Lean
false
false
19,560
lean
/- Copyright (c) 2015 Leonardo de Moura. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Haitao Zhang List combinators. -/ import data.list.basic data.equiv open nat prod decidable function helper_tactics namespace list variables {A B C : Type} /- map -/ definition map (f : A → B) : list A → list B | [] := [] | (a :: l) := f a :: map l theorem map_nil (f : A → B) : map f [] = [] theorem map_cons (f : A → B) (a : A) (l : list A) : map f (a :: l) = f a :: map f l lemma map_append (f : A → B) : ∀ l₁ l₂, map f (l₁++l₂) = (map f l₁)++(map f l₂) | nil := take l, rfl | (a::l) := take l', begin rewrite [append_cons, *map_cons, append_cons, map_append] end lemma map_singleton (f : A → B) (a : A) : map f [a] = [f a] := rfl theorem map_id : ∀ l : list A, map id l = l | [] := rfl | (x::xs) := begin rewrite [map_cons, map_id] end theorem map_id' {f : A → A} (H : ∀x, f x = x) : ∀ l : list A, map f l = l | [] := rfl | (x::xs) := begin rewrite [map_cons, H, map_id'] end theorem map_map (g : B → C) (f : A → B) : ∀ l, map g (map f l) = map (g ∘ f) l | [] := rfl | (a :: l) := show (g ∘ f) a :: map g (map f l) = map (g ∘ f) (a :: l), by rewrite (map_map l) theorem length_map (f : A → B) : ∀ l : list A, length (map f l) = length l | [] := by esimp | (a :: l) := show length (map f l) + 1 = length l + 1, by rewrite (length_map l) theorem mem_map {A B : Type} (f : A → B) : ∀ {a l}, a ∈ l → f a ∈ map f l | a [] i := absurd i !not_mem_nil | a (x::xs) i := or.elim (eq_or_mem_of_mem_cons i) (λ aeqx : a = x, by rewrite [aeqx, map_cons]; apply mem_cons) (λ ainxs : a ∈ xs, or.inr (mem_map ainxs)) theorem exists_of_mem_map {A B : Type} {f : A → B} {b : B} : ∀{l}, b ∈ map f l → ∃a, a ∈ l ∧ f a = b | [] H := false.elim H | (c::l) H := or.elim (iff.mp !mem_cons_iff H) (assume H1 : b = f c, exists.intro c (and.intro !mem_cons (eq.symm H1))) (assume H1 : b ∈ map f l, obtain a (Hl : a ∈ l) (Hr : f a = b), from exists_of_mem_map H1, exists.intro a (and.intro (mem_cons_of_mem _ Hl) Hr)) theorem eq_of_map_const {A B : Type} {b₁ b₂ : B} : ∀ {l : list A}, b₁ ∈ map (const A b₂) l → b₁ = b₂ | [] h := absurd h !not_mem_nil | (a::l) h := or.elim (eq_or_mem_of_mem_cons h) (λ b₁eqb₂ : b₁ = b₂, b₁eqb₂) (λ b₁inl : b₁ ∈ map (const A b₂) l, eq_of_map_const b₁inl) definition map₂ (f : A → B → C) : list A → list B → list C | [] _ := [] | _ [] := [] | (x::xs) (y::ys) := f x y :: map₂ xs ys /- filter -/ definition filter (p : A → Prop) [h : decidable_pred p] : list A → list A | [] := [] | (a::l) := if p a then a :: filter l else filter l theorem filter_nil (p : A → Prop) [h : decidable_pred p] : filter p [] = [] theorem filter_cons_of_pos {p : A → Prop} [h : decidable_pred p] {a : A} : ∀ l, p a → filter p (a::l) = a :: filter p l := λ l pa, if_pos pa theorem filter_cons_of_neg {p : A → Prop} [h : decidable_pred p] {a : A} : ∀ l, ¬ p a → filter p (a::l) = filter p l := λ l pa, if_neg pa theorem of_mem_filter {p : A → Prop} [h : decidable_pred p] {a : A} : ∀ {l}, a ∈ filter p l → p a | [] ain := absurd ain !not_mem_nil | (b::l) ain := by_cases (λ pb : p b, have aux : a ∈ b :: filter p l, by rewrite [filter_cons_of_pos _ pb at ain]; exact ain, or.elim (eq_or_mem_of_mem_cons aux) (λ aeqb : a = b, by rewrite [-aeqb at pb]; exact pb) (λ ainl, of_mem_filter ainl)) (λ npb : ¬ p b, by rewrite [filter_cons_of_neg _ npb at ain]; exact (of_mem_filter ain)) theorem mem_of_mem_filter {p : A → Prop} [h : decidable_pred p] {a : A} : ∀ {l}, a ∈ filter p l → a ∈ l | [] ain := absurd ain !not_mem_nil | (b::l) ain := by_cases (λ pb : p b, have aux : a ∈ b :: filter p l, by rewrite [filter_cons_of_pos _ pb at ain]; exact ain, or.elim (eq_or_mem_of_mem_cons aux) (λ aeqb : a = b, by rewrite [aeqb]; exact !mem_cons) (λ ainl, mem_cons_of_mem _ (mem_of_mem_filter ainl))) (λ npb : ¬ p b, by rewrite [filter_cons_of_neg _ npb at ain]; exact (mem_cons_of_mem _ (mem_of_mem_filter ain))) theorem mem_filter_of_mem {p : A → Prop} [h : decidable_pred p] {a : A} : ∀ {l}, a ∈ l → p a → a ∈ filter p l | [] ain pa := absurd ain !not_mem_nil | (b::l) ain pa := by_cases (λ pb : p b, or.elim (eq_or_mem_of_mem_cons ain) (λ aeqb : a = b, by rewrite [filter_cons_of_pos _ pb, aeqb]; exact !mem_cons) (λ ainl : a ∈ l, by rewrite [filter_cons_of_pos _ pb]; exact (mem_cons_of_mem _ (mem_filter_of_mem ainl pa)))) (λ npb : ¬ p b, or.elim (eq_or_mem_of_mem_cons ain) (λ aeqb : a = b, absurd (eq.rec_on aeqb pa) npb) (λ ainl : a ∈ l, by rewrite [filter_cons_of_neg _ npb]; exact (mem_filter_of_mem ainl pa))) theorem filter_sub {p : A → Prop} [h : decidable_pred p] (l : list A) : filter p l ⊆ l := λ a ain, mem_of_mem_filter ain theorem filter_append {p : A → Prop} [h : decidable_pred p] : ∀ (l₁ l₂ : list A), filter p (l₁++l₂) = filter p l₁ ++ filter p l₂ | [] l₂ := rfl | (a::l₁) l₂ := by_cases (λ pa : p a, by rewrite [append_cons, *filter_cons_of_pos _ pa, filter_append]) (λ npa : ¬ p a, by rewrite [append_cons, *filter_cons_of_neg _ npa, filter_append]) /- foldl & foldr -/ definition foldl (f : A → B → A) : A → list B → A | a [] := a | a (b :: l) := foldl (f a b) l theorem foldl_nil (f : A → B → A) (a : A) : foldl f a [] = a theorem foldl_cons (f : A → B → A) (a : A) (b : B) (l : list B) : foldl f a (b::l) = foldl f (f a b) l definition foldr (f : A → B → B) : B → list A → B | b [] := b | b (a :: l) := f a (foldr b l) theorem foldr_nil (f : A → B → B) (b : B) : foldr f b [] = b theorem foldr_cons (f : A → B → B) (b : B) (a : A) (l : list A) : foldr f b (a::l) = f a (foldr f b l) section foldl_eq_foldr -- foldl and foldr coincide when f is commutative and associative parameters {α : Type} {f : α → α → α} hypothesis (Hcomm : ∀ a b, f a b = f b a) hypothesis (Hassoc : ∀ a b c, f (f a b) c = f a (f b c)) include Hcomm Hassoc 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) := begin change foldl f (f (f a b) c) l = f b (foldl f (f a c) l), rewrite -foldl_eq_of_comm_of_assoc, change foldl f (f (f a b) c) l = foldl f (f (f a c) b) l, have H₁ : f (f a b) c = f (f a c) b, by rewrite [Hassoc, Hassoc, Hcomm b c], rewrite H₁ end theorem foldl_eq_foldr : ∀ a l, foldl f a l = foldr f a l | a nil := rfl | a (b :: l) := begin rewrite foldl_eq_of_comm_of_assoc, esimp, change f b (foldl f a l) = f b (foldr f a l), rewrite foldl_eq_foldr end end foldl_eq_foldr theorem foldl_append (f : B → A → B) : ∀ (b : B) (l₁ l₂ : list A), foldl f b (l₁++l₂) = foldl f (foldl f b l₁) l₂ | b [] l₂ := rfl | b (a::l₁) l₂ := by rewrite [append_cons, *foldl_cons, foldl_append] theorem foldr_append (f : A → B → B) : ∀ (b : B) (l₁ l₂ : list A), foldr f b (l₁++l₂) = foldr f (foldr f b l₂) l₁ | b [] l₂ := rfl | b (a::l₁) l₂ := by rewrite [append_cons, *foldr_cons, foldr_append] /- all & any -/ definition all (l : list A) (p : A → Prop) : Prop := foldr (λ a r, p a ∧ r) true l definition any (l : list A) (p : A → Prop) : Prop := foldr (λ a r, p a ∨ r) false l theorem all_nil_eq (p : A → Prop) : all [] p = true theorem all_nil (p : A → Prop) : all [] p := trivial theorem all_cons_eq (p : A → Prop) (a : A) (l : list A) : all (a::l) p = (p a ∧ all l p) theorem all_cons {p : A → Prop} {a : A} {l : list A} (H1 : p a) (H2 : all l p) : all (a::l) p := and.intro H1 H2 theorem all_of_all_cons {p : A → Prop} {a : A} {l : list A} : all (a::l) p → all l p := assume h, by rewrite [all_cons_eq at h]; exact (and.elim_right h) theorem of_all_cons {p : A → Prop} {a : A} {l : list A} : all (a::l) p → p a := assume h, by rewrite [all_cons_eq at h]; exact (and.elim_left h) theorem all_cons_of_all {p : A → Prop} {a : A} {l : list A} : p a → all l p → all (a::l) p := assume pa alllp, and.intro pa alllp theorem all_implies {p q : A → Prop} : ∀ {l}, all l p → (∀ x, p x → q x) → all l q | [] h₁ h₂ := trivial | (a::l) h₁ h₂ := have allq : all l q, from all_implies (all_of_all_cons h₁) h₂, have qa : q a, from h₂ a (of_all_cons h₁), all_cons_of_all qa allq theorem of_mem_of_all {p : A → Prop} {a : A} : ∀ {l}, a ∈ l → all l p → p a | [] h₁ h₂ := absurd h₁ !not_mem_nil | (b::l) h₁ h₂ := or.elim (eq_or_mem_of_mem_cons h₁) (λ aeqb : a = b, by rewrite [all_cons_eq at h₂, -aeqb at h₂]; exact (and.elim_left h₂)) (λ ainl : a ∈ l, have allp : all l p, by rewrite [all_cons_eq at h₂]; exact (and.elim_right h₂), of_mem_of_all ainl allp) theorem all_of_forall {p : A → Prop} : ∀ {l}, (∀a, a ∈ l → p a) → all l p | [] H := !all_nil | (a::l) H := all_cons (H a !mem_cons) (all_of_forall (λ a' H', H a' (mem_cons_of_mem _ H'))) theorem any_nil (p : A → Prop) : any [] p = false theorem any_cons (p : A → Prop) (a : A) (l : list A) : any (a::l) p = (p a ∨ any l p) theorem any_of_mem {p : A → Prop} {a : A} : ∀ {l}, a ∈ l → p a → any l p | [] i h := absurd i !not_mem_nil | (b::l) i h := or.elim (eq_or_mem_of_mem_cons i) (λ aeqb : a = b, by rewrite [-aeqb]; exact (or.inl h)) (λ ainl : a ∈ l, have anyl : any l p, from any_of_mem ainl h, or.inr anyl) theorem exists_of_any {p : A → Prop} : ∀{l : list A}, any l p → ∃a, a ∈ l ∧ p a | [] H := false.elim H | (b::l) H := or.elim H (assume H1 : p b, exists.intro b (and.intro !mem_cons H1)) (assume H1 : any l p, obtain a (H2l : a ∈ l) (H2r : p a), from exists_of_any H1, exists.intro a (and.intro (mem_cons_of_mem b H2l) H2r)) definition decidable_all (p : A → Prop) [H : decidable_pred p] : ∀ l, decidable (all l p) | [] := decidable_true | (a :: l) := match H a with | inl Hp₁ := match decidable_all l with | inl Hp₂ := inl (and.intro Hp₁ Hp₂) | inr Hn₂ := inr (not_and_of_not_right (p a) Hn₂) end | inr Hn := inr (not_and_of_not_left (all l p) Hn) end definition decidable_any (p : A → Prop) [H : decidable_pred p] : ∀ l, decidable (any l p) | [] := decidable_false | (a :: l) := match H a with | inl Hp := inl (or.inl Hp) | inr Hn₁ := match decidable_any l with | inl Hp₂ := inl (or.inr Hp₂) | inr Hn₂ := inr (not_or Hn₁ Hn₂) end end /- zip & unzip -/ definition zip (l₁ : list A) (l₂ : list B) : list (A × B) := map₂ (λ a b, (a, b)) l₁ l₂ definition unzip : list (A × B) → list A × list B | [] := ([], []) | ((a, b) :: l) := match unzip l with | (la, lb) := (a :: la, b :: lb) end theorem unzip_nil : unzip (@nil (A × B)) = ([], []) theorem unzip_cons (a : A) (b : B) (l : list (A × B)) : unzip ((a, b) :: l) = match unzip l with (la, lb) := (a :: la, b :: lb) end := rfl theorem zip_unzip : ∀ (l : list (A × B)), zip (pr₁ (unzip l)) (pr₂ (unzip l)) = l | [] := rfl | ((a, b) :: l) := begin rewrite unzip_cons, have r : zip (pr₁ (unzip l)) (pr₂ (unzip l)) = l, from zip_unzip l, revert r, eapply prod.cases_on (unzip l), intro la lb r, rewrite -r end /- flat -/ definition flat (l : list (list A)) : list A := foldl append nil l /- product -/ section product definition product : list A → list B → list (A × B) | [] l₂ := [] | (a::l₁) l₂ := map (λ b, (a, b)) l₂ ++ product l₁ l₂ theorem nil_product (l : list B) : product (@nil A) l = [] theorem product_cons (a : A) (l₁ : list A) (l₂ : list B) : product (a::l₁) l₂ = map (λ b, (a, b)) l₂ ++ product l₁ l₂ theorem product_nil : ∀ (l : list A), product l (@nil B) = [] | [] := rfl | (a::l) := by rewrite [product_cons, map_nil, product_nil] theorem eq_of_mem_map_pair₁ {a₁ a : A} {b₁ : B} {l : list B} : (a₁, b₁) ∈ map (λ b, (a, b)) l → a₁ = a := assume ain, assert h₁ : pr1 (a₁, b₁) ∈ map pr1 (map (λ b, (a, b)) l), from mem_map pr1 ain, assert h₂ : a₁ ∈ map (λb, a) l, by rewrite [map_map at h₁, ↑pr1 at h₁]; exact h₁, eq_of_map_const h₂ theorem mem_of_mem_map_pair₁ {a₁ a : A} {b₁ : B} {l : list B} : (a₁, b₁) ∈ map (λ b, (a, b)) l → b₁ ∈ l := assume ain, assert h₁ : pr2 (a₁, b₁) ∈ map pr2 (map (λ b, (a, b)) l), from mem_map pr2 ain, assert h₂ : b₁ ∈ map (λx, x) l, by rewrite [map_map at h₁, ↑pr2 at h₁]; exact h₁, by rewrite [map_id at h₂]; exact h₂ theorem mem_product {a : A} {b : B} : ∀ {l₁ l₂}, a ∈ l₁ → b ∈ l₂ → (a, b) ∈ product l₁ l₂ | [] l₂ h₁ h₂ := absurd h₁ !not_mem_nil | (x::l₁) l₂ h₁ h₂ := or.elim (eq_or_mem_of_mem_cons h₁) (λ aeqx : a = x, assert aux : (a, b) ∈ map (λ b, (a, b)) l₂, from mem_map _ h₂, begin rewrite [-aeqx, product_cons], exact mem_append_left _ aux end) (λ ainl₁ : a ∈ l₁, assert inl₁l₂ : (a, b) ∈ product l₁ l₂, from mem_product ainl₁ h₂, begin rewrite [product_cons], exact mem_append_right _ inl₁l₂ end) theorem mem_of_mem_product_left {a : A} {b : B} : ∀ {l₁ l₂}, (a, b) ∈ product l₁ l₂ → a ∈ l₁ | [] l₂ h := absurd h !not_mem_nil | (x::l₁) l₂ h := or.elim (mem_or_mem_of_mem_append h) (λ ain : (a, b) ∈ map (λ b, (x, b)) l₂, assert aeqx : a = x, from eq_of_mem_map_pair₁ ain, by rewrite [aeqx]; exact !mem_cons) (λ ain : (a, b) ∈ product l₁ l₂, have ainl₁ : a ∈ l₁, from mem_of_mem_product_left ain, mem_cons_of_mem _ ainl₁) theorem mem_of_mem_product_right {a : A} {b : B} : ∀ {l₁ l₂}, (a, b) ∈ product l₁ l₂ → b ∈ l₂ | [] l₂ h := absurd h !not_mem_nil | (x::l₁) l₂ h := or.elim (mem_or_mem_of_mem_append h) (λ abin : (a, b) ∈ map (λ b, (x, b)) l₂, mem_of_mem_map_pair₁ abin) (λ abin : (a, b) ∈ product l₁ l₂, mem_of_mem_product_right abin) theorem length_product : ∀ (l₁ : list A) (l₂ : list B), length (product l₁ l₂) = length l₁ * length l₂ | [] l₂ := by rewrite [length_nil, zero_mul] | (x::l₁) l₂ := assert ih : length (product l₁ l₂) = length l₁ * length l₂, from length_product l₁ l₂, by rewrite [product_cons, length_append, length_cons, length_map, ih, mul.right_distrib, one_mul, add.comm] end product -- new for list/comb dependent map theory definition dinj₁ (p : A → Prop) (f : Π a, p a → B) := ∀ ⦃a1 a2⦄ (h1 : p a1) (h2 : p a2), a1 ≠ a2 → (f a1 h1) ≠ (f a2 h2) definition dinj (p : A → Prop) (f : Π a, p a → B) := ∀ ⦃a1 a2⦄ (h1 : p a1) (h2 : p a2), (f a1 h1) = (f a2 h2) → a1 = a2 definition dmap (p : A → Prop) [h : decidable_pred p] (f : Π a, p a → B) : list A → list B | [] := [] | (a::l) := if P : (p a) then cons (f a P) (dmap l) else (dmap l) -- properties of dmap section dmap variable {p : A → Prop} variable [h : decidable_pred p] include h variable {f : Π a, p a → B} lemma dmap_nil : dmap p f [] = [] := rfl lemma dmap_cons_of_pos {a : A} (P : p a) : ∀ l, dmap p f (a::l) = (f a P) :: dmap p f l := λ l, dif_pos P lemma dmap_cons_of_neg {a : A} (P : ¬ p a) : ∀ l, dmap p f (a::l) = dmap p f l := λ l, dif_neg P lemma mem_dmap : ∀ {l : list A} {a} (Pa : p a), a ∈ l → (f a Pa) ∈ dmap p f l | [] := take a Pa Pinnil, by contradiction | (a::l) := take b Pb Pbin, or.elim (eq_or_mem_of_mem_cons Pbin) (assume Pbeqa, begin rewrite [eq.symm Pbeqa, dmap_cons_of_pos Pb], exact !mem_cons end) (assume Pbinl, decidable.rec_on (h a) (assume Pa, begin rewrite [dmap_cons_of_pos Pa], apply mem_cons_of_mem, exact mem_dmap Pb Pbinl end) (assume nPa, begin rewrite [dmap_cons_of_neg nPa], exact mem_dmap Pb Pbinl end)) lemma exists_of_mem_dmap : ∀ {l : list A} {b : B}, b ∈ dmap p f l → ∃ a P, a ∈ l ∧ b = f a P | [] := take b, by rewrite dmap_nil; contradiction | (a::l) := take b, decidable.rec_on (h a) (assume Pa, begin rewrite [dmap_cons_of_pos Pa, mem_cons_iff], intro Pb, cases Pb with Peq Pin, exact exists.intro a (exists.intro Pa (and.intro !mem_cons Peq)), assert Pex : ∃ (a : A) (P : p a), a ∈ l ∧ b = f a P, exact exists_of_mem_dmap Pin, cases Pex with a' Pex', cases Pex' with Pa' P', exact exists.intro a' (exists.intro Pa' (and.intro (mem_cons_of_mem a (and.left P')) (and.right P'))) end) (assume nPa, begin rewrite [dmap_cons_of_neg nPa], intro Pin, assert Pex : ∃ (a : A) (P : p a), a ∈ l ∧ b = f a P, exact exists_of_mem_dmap Pin, cases Pex with a' Pex', cases Pex' with Pa' P', exact exists.intro a' (exists.intro Pa' (and.intro (mem_cons_of_mem a (and.left P')) (and.right P'))) end) lemma map_dmap_of_inv_of_pos {g : B → A} (Pinv : ∀ a (Pa : p a), g (f a Pa) = a) : ∀ {l : list A}, (∀ ⦃a⦄, a ∈ l → p a) → map g (dmap p f l) = l | [] := assume Pl, by rewrite [dmap_nil, map_nil] | (a::l) := assume Pal, assert Pa : p a, from Pal a !mem_cons, assert Pl : ∀ a, a ∈ l → p a, from take x Pxin, Pal x (mem_cons_of_mem a Pxin), by rewrite [dmap_cons_of_pos Pa, map_cons, Pinv, map_dmap_of_inv_of_pos Pl] lemma mem_of_dinj_of_mem_dmap (Pdi : dinj p f) : ∀ {l : list A} {a} (Pa : p a), (f a Pa) ∈ dmap p f l → a ∈ l | [] := take a Pa Pinnil, by contradiction | (b::l) := take a Pa Pmap, decidable.rec_on (h b) (λ Pb, begin rewrite (dmap_cons_of_pos Pb) at Pmap, rewrite mem_cons_iff at Pmap, rewrite mem_cons_iff, apply (or_of_or_of_imp_of_imp Pmap), apply Pdi, apply mem_of_dinj_of_mem_dmap Pa end) (λ nPb, begin rewrite (dmap_cons_of_neg nPb) at Pmap, apply mem_cons_of_mem, exact mem_of_dinj_of_mem_dmap Pa Pmap end) lemma not_mem_dmap_of_dinj_of_not_mem (Pdi : dinj p f) {l : list A} {a} (Pa : p a) : a ∉ l → (f a Pa) ∉ dmap p f l := not_imp_not_of_imp (mem_of_dinj_of_mem_dmap Pdi Pa) end dmap section open equiv lemma list_equiv_of_equiv {A B : Type} : A ≃ B → list A ≃ list B | (mk f g l r) := mk (map f) (map g) begin intros, rewrite [map_map, id_of_left_inverse l, map_id] end begin intros, rewrite [map_map, id_of_righ_inverse r, map_id] end end end list attribute list.decidable_any [instance] attribute list.decidable_all [instance]
6c59731eee20c6b04cf7e262fbc28a437f9d1f47
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/j4.lean
fbc5101e7f34bfba10483762dc1c422220e5d05a
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
1,490
lean
import macros import tactic using Nat definition dvd (a b : Nat) : Bool := ∃ c, a * c = b infix 50 | : dvd theorem dvd_elim {a b : Nat} (H : a | b) : ∃ c, a * c = b := H theorem dvd_intro {a b : Nat} (c : Nat) (H : a * c = b) : a | b := exists_intro c H set_opaque dvd true theorem dvd_trans {a b c} (H1 : a | b) (H2 : b | c) : a | c := obtain (w1 : Nat) (Hw1 : a * w1 = b), from dvd_elim H1, obtain (w2 : Nat) (Hw2 : b * w2 = c), from dvd_elim H2, dvd_intro (w1 * w2) (calc a * (w1 * w2) = a * w1 * w2 : symm (mul_assoc a w1 w2) ... = b * w2 : { Hw1 } ... = c : Hw2) definition prime p := p ≥ 2 ∧ forall m, m | p → m = 1 ∨ m = p theorem not_prime_eq (n : Nat) (H1 : n ≥ 2) (H2 : ¬ prime n) : ∃ m, m | n ∧ m ≠ 1 ∧ m ≠ n := have H3 : ¬ n ≥ 2 ∨ ¬ (∀ m : Nat, m | n → m = 1 ∨ m = n), from (not_and _ _ ◂ H2), have H4 : ¬ ¬ n ≥ 2, by skip, -- Ignore this hole obtain (m : Nat) (H5 : ¬ (m | n → m = 1 ∨ m = n)), from (not_forall_elim (resolve1 H3 H4)), have H6 : m | n ∧ ¬ (m = 1 ∨ m = n), by id, -- <<< id is the "do-nothing" tactic, it will fail and Lean will display the proof state for this hole have H7 : ¬ (m = 1 ∨ m = n) ↔ (m ≠ 1 ∧ m ≠ n), from (not_or (m = 1) (m = n)), have H8 : m | n ∧ m ≠ 1 ∧ m ≠ n, from subst H6 H7, show ∃ m, m | n ∧ m ≠ 1 ∧ m ≠ n, from exists_intro m H8
d83e6c406be30b595c262ae188bb4cade42b9aae
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/category_theory/const.lean
dead4eb73693885ec4e8256744717486e2ba97b1
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
2,513
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.functor_category import category_theory.opposites universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation open category_theory namespace category_theory.functor variables (J : Type u₁) [𝒥 : category.{v₁} J] variables {C : Type u₂} [𝒞 : category.{v₂} C] include 𝒥 𝒞 def const : C ⥤ (J ⥤ C) := { obj := λ X, { obj := λ j, X, map := λ j j' f, 𝟙 X }, map := λ X Y f, { app := λ j, f } } namespace const open opposite variables {J} @[simp] lemma obj_obj (X : C) (j : J) : ((const J).obj X).obj j = X := rfl @[simp] lemma obj_map (X : C) {j j' : J} (f : j ⟶ j') : ((const J).obj X).map f = 𝟙 X := rfl @[simp] lemma map_app {X Y : C} (f : X ⟶ Y) (j : J) : ((const J).map f).app j = f := rfl def op_obj_op (X : C) : (const Jᵒᵖ).obj (op X) ≅ ((const J).obj X).op := { hom := { app := λ j, 𝟙 _ }, inv := { app := λ j, 𝟙 _ } } @[simp] lemma op_obj_op_hom_app (X : C) (j : Jᵒᵖ) : (op_obj_op X).hom.app j = 𝟙 _ := rfl @[simp] lemma op_obj_op_inv_app (X : C) (j : Jᵒᵖ) : (op_obj_op X).inv.app j = 𝟙 _ := rfl def op_obj_unop (X : Cᵒᵖ) : (const Jᵒᵖ).obj (unop X) ≅ ((const J).obj X).left_op := { hom := { app := λ j, 𝟙 _ }, inv := { app := λ j, 𝟙 _ } } -- Lean needs some help with universes here. @[simp] lemma op_obj_unop_hom_app (X : Cᵒᵖ) (j : Jᵒᵖ) : (op_obj_unop.{v₁ v₂} X).hom.app j = 𝟙 _ := rfl @[simp] lemma op_obj_unop_inv_app (X : Cᵒᵖ) (j : Jᵒᵖ) : (op_obj_unop.{v₁ v₂} X).inv.app j = 𝟙 _ := rfl end const section variables {D : Type u₃} [𝒟 : category.{v₃} D] include 𝒟 /-- These are actually equal, of course, but not definitionally equal (the equality requires F.map (𝟙 _) = 𝟙 _). A natural isomorphism is more convenient than an equality between functors (compare id_to_iso). -/ @[simp] def const_comp (X : C) (F : C ⥤ D) : (const J).obj X ⋙ F ≅ (const J).obj (F.obj X) := { hom := { app := λ _, 𝟙 _ }, inv := { app := λ _, 𝟙 _ } } @[simp] lemma const_comp_hom_app (X : C) (F : C ⥤ D) (j : J) : (const_comp J X F).hom.app j = 𝟙 _ := rfl @[simp] lemma const_comp_inv_app (X : C) (F : C ⥤ D) (j : J) : (const_comp J X F).inv.app j = 𝟙 _ := rfl end end category_theory.functor
c81f323b0688f1e93574e1bcdfcd5d0b5718686a
4fc5f02f6ed9423b87987589bcc202f985d7e9ff
/src/game/world5/level1.lean
3c38460eece8f8c43a367851b6922bb52ca008af
[ "Apache-2.0" ]
permissive
grthomson/natural_number_game
2937533df0b83e73e6a873c0779ee21e30f09778
0a23a327ca724b95406c510ee27c55f5b97e612f
refs/heads/master
1,599,994,591,355
1,588,869,073,000
1,588,869,073,000
222,750,177
0
0
Apache-2.0
1,574,183,960,000
1,574,183,960,000
null
UTF-8
Lean
false
false
4,428
lean
-- World name : Function world /- # Function world. If you have beaten Addition World, then you have got quite good at manipulating equalities in Lean using the `rw` tactic. But there are plenty of levels later on which will require you to manipulate functions, and `rw` is not the tool for you here. To manipulate functions effectively, we need to learn about a new collection of tactics, namely `exact`, `intro`, `have` and `apply`. These tactics are specially designed for dealing with functions. Of course we are ultimately interested in using these tactics to prove theorems about the natural numbers &ndash; but in this world there is little point in working with specific sets like `mynat`, everything works for general sets. So our notation for this level is: $P$, $Q$, $R$ and so on denote general sets, and $h$, $j$, $k$ and so on denote general functions between them. What we will learn in this world is how to use Lean to move elements around between these sets using the functions we are given, and the tactics we will learn. A word of warning &ndash; even though there's no harm at all in thinking of $P$ being a set and $p$ being an element, you will not see Lean using the notation $p\in P$, because internally Lean stores $P$ as a "Type" and $p$ as a "term", and it uses `p : P` to mean "$p$ is a term of type $P$", Lean's way of expressing the idea that $p$ is an element of the set $P$. You have seen this already &ndash; Lean has been writing `n : mynat` to mean that $n$ is a natural number. ## A new kind of goal. All through addition world, our goals have been theorems, and it was our job to find the proofs. **The levels in function world aren't theorems**. This is the only world where the levels aren't theorems in fact. In function world the object of a level is to create an element of the set in the goal. The goal will look like `⊢ X` with $X$ a set and you get rid of the goal by constructing an element of $X$. I don't know if you noticed this, but you finished essentially every goal of Addition World (and Multiplication World and Power World, if you played them) with `refl`. This tactic is no use to us here. We are going to have to learn a new way of solving goals &ndash; the `exact` tactic. If you delete the sorry below then your local context will look like this: ``` P Q : Type, p : P, h : P → Q ⊢ Q ``` In this situation, we have sets $P$ and $Q$ (but Lean calls them types), and an element $p$ of $P$ (written `p : P` but meaning $p\in P$). We also have a function $h$ from $P$ to $Q$, and our goal is to construct an element of the set $Q$. It's clear what to do *mathematically* to solve this goal -- we can make an element of $Q$ by applying the function $h$ to the element $p$. But how to do it in Lean? There are at least two ways to explain this idea to Lean, and here we will learn about one of them, namely the method which uses the `exact` tactic. ## The `exact` tactic. If you can explicitly see how to make an element of of your goal set, i.e. you have a formula for it, then you can just write `exact <formula>` and this will close the goal. ### Example If your local context looks like this ``` P Q : Type, p : P, h : P → Q ⊢ Q ``` then $h(p)$ is an element of $Q$ so you can just write `exact h(p),` to close the goal. ## Important note Note that `exact h(P),` won't work (with a capital $P$); this is a common error I see from beginners. $P$ is not an element of $P$, it's $p$ that is an element of $P$. ## Level 1: the `exact` tactic. -/ /- Definition Given an element of $P$ and a function from $P$ to $Q$, we define an element of $Q$. -/ example (P Q : Type) (p : P) (h : P → Q) : Q := begin exact h(p), end /- Tactic : exact ## Summary If the goal is `⊢ X` then `exact x` will close the goal if and only if `x` is a term of type `X`. ## Details Say $P$, $Q$ and $R$ are types (i.e., what a mathematician might think of as either sets or propositions), and the local context looks like this: ``` p : P, h : P → Q, j : Q → R ⊢ R ``` If you can spot how to make a term of type `R`, then you can just make it and say you're done using the `exact` tactic together with the formula you have spotted. For example the above goal could be solved with `exact j(h(p)),` because $j(h(p))$ is easily checked to be a term of type $R$ (i.e., an element of the set $R$, or a proof of the proposition $R$). -/
321546646332c5af7e08356b3172604e9369077d
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/monotonicity/lemmas.lean
4912d311ab8c801dc5441c43c80cd98298117400
[ "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
1,345
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import algebra.order.group.abs import algebra.order.ring.defs import algebra.order.sub.canonical import data.set.lattice import tactic.monotonicity.basic variables {α : Type*} open set attribute [mono] inter_subset_inter union_subset_union sUnion_mono Union₂_mono sInter_subset_sInter Inter₂_mono image_subset preimage_mono prod_mono monotone.set_prod seq_mono image2_subset order_embedding.monotone attribute [mono] upper_bounds_mono_set lower_bounds_mono_set upper_bounds_mono_mem lower_bounds_mono_mem upper_bounds_mono lower_bounds_mono bdd_above.mono bdd_below.mono attribute [mono] add_le_add mul_le_mul neg_le_neg mul_lt_mul_of_pos_left mul_lt_mul_of_pos_right mul_le_mul_of_nonneg_left mul_le_mul_of_nonneg_right mul_le_mul_of_nonpos_left mul_le_mul_of_nonpos_right imp_imp_imp le_implies_le_of_le_of_le tsub_lt_tsub_left_of_le tsub_lt_tsub_right_of_le tsub_le_tsub abs_le_abs sup_le_sup inf_le_inf attribute [mono left] add_lt_add_of_le_of_lt mul_lt_mul' attribute [mono right] add_lt_add_of_lt_of_le mul_lt_mul
c21e7af027dc8913172e6a1b2ebbe45aacdff031
05b503addd423dd68145d68b8cde5cd595d74365
/src/data/monoid_algebra.lean
f7a25f24ce417ba77d62495b3118857127cb789c
[ "Apache-2.0" ]
permissive
aestriplex/mathlib
77513ff2b176d74a3bec114f33b519069788811d
e2fa8b2b1b732d7c25119229e3cdfba8370cb00f
refs/heads/master
1,621,969,960,692
1,586,279,279,000
1,586,279,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,503
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, Scott Morrison -/ import data.finsupp /-! # Monoid algebras When the domain of a `finsupp` has a multiplicative or additive structure, we can define a convolution product. To mathematicians this structure is known as the "monoid algebra", i.e. the finite formal linear combinations over a given semiring of elements of the monoid. The "group ring" ℤ[G] or the "group algebra" k[G] are typical uses. In this file we define `monoid_algebra k G := G →₀ k`, and `add_monoid_algebra k G` in the same way, and then define the convolution product on these. When the domain is additive, this is used to define polynomials: ``` polynomial α := add_monoid_algebra ℕ α mv_polynominal σ α := add_monoid_algebra (σ →₀ ℕ) α ``` When the domain is multiplicative, e.g. a group, this will be used to define the group ring. ## Implementation note Unfortunately because additive and multiplicative structures both appear in both cases, it doesn't appear to be possible to make much use of `to_additive`, and we just settle for saying everything twice. Similarly, I attempted to just define `add_monoid_algebra k G := monoid_algebra k (multiplicative G)`, but the definitional equality `multiplicative G = G` leaks through everywhere, and seems impossible to use. -/ noncomputable theory open_locale classical open finset finsupp universes u₁ u₂ variables (k : Type u₁) (G : Type u₂) section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid]] def monoid_algebra : Type (max u₁ u₂) := G →₀ k end namespace monoid_algebra variables {k G} local attribute [reducible] monoid_algebra section variables [semiring k] [monoid G] /-- The product of `f g : monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x * y = a`. (Think of the group ring of a group.) -/ instance : has_mul (monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)⟩ lemma mul_def {f g : monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)) := rfl lemma mul_apply (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ * a₂ = x then b₁ * b₂ else 0) := begin rw [mul_def], simp only [sum_apply, single_apply], end end section variables [semiring k] [monoid G] lemma support_mul (a b : monoid_algebra k G) : (a * b).support ⊆ a.support.bind (λa₁, b.support.bind $ λa₂, {a₁ * a₂}) := subset.trans support_sum $ bind_mono $ assume a₁ _, subset.trans support_sum $ bind_mono $ assume a₂ _, support_single_subset /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `1` and zero elsewhere. -/ instance : has_one (monoid_algebra k G) := ⟨single 1 1⟩ lemma one_def : (1 : monoid_algebra k G) = single 1 1 := rfl -- TODO: the simplifier unfolds 0 in the instance proof! private lemma zero_mul (f : monoid_algebra k G) : 0 * f = 0 := by simp only [mul_def, sum_zero_index] private lemma mul_zero (f : monoid_algebra k G) : f * 0 = 0 := by simp only [mul_def, sum_zero_index, sum_zero] private lemma left_distrib (a b c : monoid_algebra k G) : a * (b + c) = a * b + a * c := by simp only [mul_def, sum_add_index, mul_add, _root_.mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add] private lemma right_distrib (a b c : monoid_algebra k G) : (a + b) * c = a * c + b * c := by simp only [mul_def, sum_add_index, add_mul, _root_.mul_zero, _root_.zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add] instance : semiring (monoid_algebra k G) := { one := 1, mul := (*), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], zero_mul := zero_mul, mul_zero := mul_zero, mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, _root_.zero_mul, _root_.mul_zero, sum_zero, sum_add], left_distrib := left_distrib, right_distrib := right_distrib, .. finsupp.add_comm_monoid } end instance [comm_semiring k] [comm_monoid G] : comm_semiring (monoid_algebra k G) := { mul_comm := assume f g, begin simp only [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp only [mul_comm] end, .. monoid_algebra.semiring } instance [ring k] : has_neg (monoid_algebra k G) := by apply_instance instance [ring k] [monoid G] : ring (monoid_algebra k G) := { neg := has_neg.neg, add_left_neg := add_left_neg, .. monoid_algebra.semiring } instance [comm_ring k] [comm_monoid G] : comm_ring (monoid_algebra k G) := { mul_comm := mul_comm, .. monoid_algebra.ring} instance [semiring k] : has_scalar k (monoid_algebra k G) := finsupp.has_scalar instance [semiring k] : semimodule k (monoid_algebra k G) := finsupp.semimodule G k instance [ring k] : module k (monoid_algebra k G) := finsupp.module G k lemma single_mul_single [semiring k] [monoid G] {a₁ a₂ : G} {b₁ b₂ : k} : (single a₁ b₁ : monoid_algebra k G) * single a₂ b₂ = single (a₁ * a₂) (b₁ * b₂) := (sum_single_index (by simp only [_root_.zero_mul, single_zero, sum_zero])).trans (sum_single_index (by rw [_root_.mul_zero, single_zero])) universe ui variable {ι : Type ui} lemma prod_single [comm_semiring k] [comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : s.prod (λi, single (a i) (b i)) = single (s.prod a) (s.prod b) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, prod_insert has, prod_insert has] section -- We now prove some additional statements that hold for group algebras. variables [semiring k] [group G] lemma mul_apply_left (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, b₁ * (g (a₁⁻¹ * x))) := begin rw mul_apply, congr, funext, -- We need to massage the condition in the if statement first: have t : ∀ a₁ a₂, a₁⁻¹ * x = a₂ ↔ a₁ * a₂ = x := by { intros, split; rintro rfl; simp, }, conv_lhs { congr, skip, funext, rw ←t, }, -- but it's downhill from here. simp only [mem_support_iff, ne.def, finsupp.sum_ite_eq], split_ifs, { simp [h], }, { refl, }, end @[simp] lemma mul_single_apply (f : monoid_algebra k G) (r : k) (x y : G) : (f * single x r) y = f (y * x⁻¹) * r := begin rw mul_apply_left, -- Again, we need to unpack the `single` into a correctly positioned `ite`: have t : ∀ a₁, x = a₁⁻¹ * y ↔ y * x⁻¹ = a₁ := by { intros, split; rintro rfl; simp, }, simp [single_apply, t], -- After this, `simp [finsupp.sum_ite_eq]` should surely progress, but doesn't. :-( convert f.sum_ite_eq (y * x⁻¹) (λ x v, v * r), { funext, congr, }, { simp only [mem_support_iff, ne.def], split_ifs, { simp [h], }, { refl, }, } end -- If we'd assumed `comm_semiring`, we could deduce this from `mul_apply_left`. lemma mul_apply_right (f g : monoid_algebra k G) (x : G) : (f * g) x = (g.sum $ λa₂ b₂, (f (x * a₂⁻¹)) * b₂) := begin rw mul_apply, rw finsupp.sum_comm, congr, funext, have t : ∀ a₁, a₁ * x' = x ↔ a₁ = x * x'⁻¹ := by { intros, split; rintro rfl; simp, }, conv_lhs { congr, skip, funext, rw t, }, simp only [mem_support_iff, ne.def, finsupp.sum_ite_eq'], split_ifs, { simp [h], }, { refl, }, end @[simp] lemma single_mul_apply (r : k) (x : G) (f : monoid_algebra k G) (y : G) : (single x r * f) y = r * f (x⁻¹ * y) := begin rw mul_apply_right, have t : ∀ a₂, x = y * a₂⁻¹ ↔ x⁻¹ * y = a₂ := by { intros, split; rintro rfl; simp, }, simp [single_apply, t], -- After this, `simp [finsupp.sum_ite_eq]` should surely progress, but doesn't. :-( convert f.sum_ite_eq (x⁻¹ * y) (λ x v, r * v), { funext, congr, }, { simp only [mem_support_iff, ne.def], split_ifs, { simp [h], }, { refl, }, } end end end monoid_algebra section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the additive monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid]] def add_monoid_algebra := G →₀ k end namespace add_monoid_algebra variables {k G} local attribute [reducible] add_monoid_algebra section variables [semiring k] [add_monoid G] /-- The product of `f g : add_monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x + y = a`. (Think of the product of multivariate polynomials where `α` is the additive monoid of monomial exponents.) -/ instance : has_mul (add_monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)⟩ lemma mul_def {f g : add_monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)) := rfl lemma support_mul (a b : add_monoid_algebra k G) : (a * b).support ⊆ a.support.bind (λa₁, b.support.bind $ λa₂, {a₁ + a₂}) := subset.trans support_sum $ bind_mono $ assume a₁ _, subset.trans support_sum $ bind_mono $ assume a₂ _, support_single_subset /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `0` and zero elsewhere. -/ instance : has_one (add_monoid_algebra k G) := ⟨single 0 1⟩ lemma one_def : (1 : add_monoid_algebra k G) = single 0 1 := rfl -- TODO: the simplifier unfolds 0 in the instance proof! private lemma zero_mul (f : add_monoid_algebra k G) : 0 * f = 0 := by simp only [mul_def, sum_zero_index] private lemma mul_zero (f : add_monoid_algebra k G) : f * 0 = 0 := by simp only [mul_def, sum_zero_index, sum_zero] private lemma left_distrib (a b c : add_monoid_algebra k G) : a * (b + c) = a * b + a * c := by simp only [mul_def, sum_add_index, mul_add, _root_.mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add] private lemma right_distrib (a b c : add_monoid_algebra k G) : (a + b) * c = a * c + b * c := by simp only [mul_def, sum_add_index, add_mul, _root_.mul_zero, _root_.zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add] instance : semiring (add_monoid_algebra k G) := { one := 1, mul := (*), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], zero_mul := zero_mul, mul_zero := mul_zero, mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, _root_.zero_mul, _root_.mul_zero, sum_zero, sum_add], left_distrib := left_distrib, right_distrib := right_distrib, .. finsupp.add_comm_monoid } end instance [comm_semiring k] [add_comm_monoid G] : comm_semiring (add_monoid_algebra k G) := { mul_comm := assume f g, begin simp only [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp only [add_comm] end, .. add_monoid_algebra.semiring } instance [ring k] : has_neg (add_monoid_algebra k G) := by apply_instance instance [ring k] [add_monoid G] : ring (add_monoid_algebra k G) := { neg := has_neg.neg, add_left_neg := add_left_neg, .. add_monoid_algebra.semiring } instance [comm_ring k] [add_comm_monoid G] : comm_ring (add_monoid_algebra k G) := { mul_comm := mul_comm, .. add_monoid_algebra.ring} instance [semiring k] : has_scalar k (add_monoid_algebra k G) := finsupp.has_scalar instance [semiring k] : semimodule k (add_monoid_algebra k G) := finsupp.semimodule G k instance [ring k] : module k (add_monoid_algebra k G) := finsupp.module G k lemma single_mul_single [semiring k] [add_monoid G] {a₁ a₂ : G} {b₁ b₂ : k}: (single a₁ b₁ : add_monoid_algebra k G) * single a₂ b₂ = single (a₁ + a₂) (b₁ * b₂) := (sum_single_index (by simp only [_root_.zero_mul, single_zero, sum_zero])).trans (sum_single_index (by rw [_root_.mul_zero, single_zero])) universe ui variable {ι : Type ui} lemma prod_single [comm_semiring k] [add_comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : s.prod (λi, single (a i) (b i)) = single (s.sum a) (s.prod b) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, sum_insert has, prod_insert has] end add_monoid_algebra
3b7f50fd7e270649117374fa889b070de61979a2
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/int/cast/prod.lean
8482a3857906ba454007333ad72a0687f0c4e7d1
[ "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
718
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.int.cast.lemmas import data.nat.cast.prod /-! # The product of two `add_group_with_one`s. -/ namespace prod variables {α β : Type*} [add_group_with_one α] [add_group_with_one β] instance : add_group_with_one (α × β) := { int_cast := λ n, (n, n), int_cast_of_nat := λ _, by simp; refl, int_cast_neg_succ_of_nat := λ _, by simp; refl, .. prod.add_monoid_with_one, .. prod.add_group } @[simp] lemma fst_int_cast (n : ℤ) : (n : α × β).fst = n := rfl @[simp] lemma snd_int_cast (n : ℤ) : (n : α × β).snd = n := rfl end prod
fb00dca6068375b8bc65f5d7be0f64637a00833b
367134ba5a65885e863bdc4507601606690974c1
/src/ring_theory/algebraic.lean
fd7e6937f6b23694850dd898e85fdb64df21ea4f
[ "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
7,790
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 linear_algebra.finite_dimensional import ring_theory.integral_closure import data.polynomial.integral_normalization /-! # Algebraic elements and algebraic extensions An element of an R-algebra is algebraic over R if it is the root of a nonzero polynomial. An R-algebra is algebraic over R if and only if all its elements are algebraic over R. The main result in this file proves transitivity of algebraicity: a tower of algebraic field extensions is algebraic. -/ universe variables u v open_locale classical open polynomial section variables (R : Type u) {A : Type v} [comm_ring R] [ring A] [algebra R A] /-- An element of an R-algebra is algebraic over R if it is the root of a nonzero polynomial. -/ def is_algebraic (x : A) : Prop := ∃ p : polynomial R, p ≠ 0 ∧ aeval x p = 0 /-- An element of an R-algebra is transcendental over R if it is not algebraic over R. -/ def transcendental (x : A) : Prop := ¬ is_algebraic R x variables {R} /-- A subalgebra is algebraic if all its elements are algebraic. -/ def subalgebra.is_algebraic (S : subalgebra R A) : Prop := ∀ x ∈ S, is_algebraic R x variables (R A) /-- An algebra is algebraic if all its elements are algebraic. -/ def algebra.is_algebraic : Prop := ∀ x : A, is_algebraic R x variables {R A} /-- A subalgebra is algebraic if and only if it is algebraic an algebra. -/ lemma subalgebra.is_algebraic_iff (S : subalgebra R A) : S.is_algebraic ↔ @algebra.is_algebraic R S _ _ (S.algebra) := begin delta algebra.is_algebraic subalgebra.is_algebraic, rw [subtype.forall'], apply forall_congr, rintro ⟨x, hx⟩, apply exists_congr, intro p, apply and_congr iff.rfl, have h : function.injective (S.val) := subtype.val_injective, conv_rhs { rw [← h.eq_iff, alg_hom.map_zero], }, rw [← aeval_alg_hom_apply, S.val_apply] end /-- An algebra is algebraic if and only if it is algebraic as a subalgebra. -/ lemma algebra.is_algebraic_iff : algebra.is_algebraic R A ↔ (⊤ : subalgebra R A).is_algebraic := begin delta algebra.is_algebraic subalgebra.is_algebraic, simp only [algebra.mem_top, forall_prop_of_true, iff_self], end end section zero_ne_one variables (R : Type u) {A : Type v} [comm_ring R] [nontrivial R] [ring A] [algebra R A] /-- An integral element of an algebra is algebraic.-/ lemma is_integral.is_algebraic {x : A} (h : is_integral R x) : is_algebraic R x := by { rcases h with ⟨p, hp, hpx⟩, exact ⟨p, hp.ne_zero, hpx⟩ } end zero_ne_one section field variables (K : Type u) {A : Type v} [field K] [ring A] [algebra K A] /-- An element of an algebra over a field is algebraic if and only if it is integral.-/ lemma is_algebraic_iff_is_integral {x : A} : is_algebraic K x ↔ is_integral K x := begin refine ⟨_, is_integral.is_algebraic K⟩, rintro ⟨p, hp, hpx⟩, refine ⟨_, monic_mul_leading_coeff_inv hp, _⟩, rw [← aeval_def, alg_hom.map_mul, hpx, zero_mul], end lemma is_algebraic_iff_is_integral' : algebra.is_algebraic K A ↔ algebra.is_integral K A := ⟨λ h x, (is_algebraic_iff_is_integral K).mp (h x), λ h x, (is_algebraic_iff_is_integral K).mpr (h x)⟩ end field namespace algebra variables {K : Type*} {L : Type*} {A : Type*} variables [field K] [field L] [comm_ring A] variables [algebra K L] [algebra L A] [algebra K A] [is_scalar_tower K L A] /-- If L is an algebraic field extension of K and A is an algebraic algebra over L, then A is algebraic over K. -/ lemma is_algebraic_trans (L_alg : is_algebraic K L) (A_alg : is_algebraic L A) : is_algebraic K A := begin simp only [is_algebraic, is_algebraic_iff_is_integral] at L_alg A_alg ⊢, exact is_integral_trans L_alg A_alg, end /-- A field extension is algebraic if it is finite. -/ lemma is_algebraic_of_finite [finite : finite_dimensional K L] : is_algebraic K L := λ x, (is_algebraic_iff_is_integral _).mpr (is_integral_of_submodule_noetherian ⊤ (is_noetherian_of_submodule_of_noetherian _ _ _ finite) x algebra.mem_top) end algebra variables {R S : Type*} [integral_domain R] [comm_ring S] lemma exists_integral_multiple [algebra R S] {z : S} (hz : is_algebraic R z) (inj : ∀ x, algebra_map R S x = 0 → x = 0) : ∃ (x : integral_closure R S) (y ≠ (0 : integral_closure R S)), z * y = x := begin rcases hz with ⟨p, p_ne_zero, px⟩, set a := p.leading_coeff with a_def, have a_ne_zero : a ≠ 0 := mt polynomial.leading_coeff_eq_zero.mp p_ne_zero, have y_integral : is_integral R (algebra_map R S a) := is_integral_algebra_map, have x_integral : is_integral R (z * algebra_map R S a) := ⟨ p.integral_normalization, monic_integral_normalization p_ne_zero, integral_normalization_aeval_eq_zero p_ne_zero px inj ⟩, refine ⟨⟨_, x_integral⟩, ⟨_, y_integral⟩, _, rfl⟩, exact λ h, a_ne_zero (inj _ (subtype.ext_iff_val.mp h)) end section field variables {K L : Type*} [field K] [field L] [algebra K L] (A : subalgebra K L) lemma inv_eq_of_aeval_div_X_ne_zero {x : L} {p : polynomial K} (aeval_ne : aeval x (div_X p) ≠ 0) : x⁻¹ = aeval x (div_X p) / (aeval x p - algebra_map _ _ (p.coeff 0)) := begin rw [inv_eq_iff, inv_div, div_eq_iff, sub_eq_iff_eq_add, mul_comm], conv_lhs { rw ← div_X_mul_X_add p }, rw [alg_hom.map_add, alg_hom.map_mul, aeval_X, aeval_C], exact aeval_ne end lemma inv_eq_of_root_of_coeff_zero_ne_zero {x : L} {p : polynomial K} (aeval_eq : aeval x p = 0) (coeff_zero_ne : p.coeff 0 ≠ 0) : x⁻¹ = - (aeval x (div_X p) / algebra_map _ _ (p.coeff 0)) := begin convert inv_eq_of_aeval_div_X_ne_zero (mt (λ h, (algebra_map K L).injective _) coeff_zero_ne), { rw [aeval_eq, zero_sub, div_neg] }, rw ring_hom.map_zero, convert aeval_eq, conv_rhs { rw ← div_X_mul_X_add p }, rw [alg_hom.map_add, alg_hom.map_mul, h, zero_mul, zero_add, aeval_C] end lemma subalgebra.inv_mem_of_root_of_coeff_zero_ne_zero {x : A} {p : polynomial K} (aeval_eq : aeval x p = 0) (coeff_zero_ne : p.coeff 0 ≠ 0) : (x⁻¹ : L) ∈ A := begin have : (x⁻¹ : L) = aeval x (div_X p) / (aeval x p - algebra_map _ _ (p.coeff 0)), { rw [aeval_eq, submodule.coe_zero, zero_sub, div_neg], convert inv_eq_of_root_of_coeff_zero_ne_zero _ coeff_zero_ne, { rw subalgebra.aeval_coe }, { simpa using aeval_eq } }, rw [this, div_eq_mul_inv, aeval_eq, submodule.coe_zero, zero_sub, ← ring_hom.map_neg, ← ring_hom.map_inv], exact A.mul_mem (aeval x p.div_X).2 (A.algebra_map_mem _), end lemma subalgebra.inv_mem_of_algebraic {x : A} (hx : is_algebraic K (x : L)) : (x⁻¹ : L) ∈ A := begin obtain ⟨p, ne_zero, aeval_eq⟩ := hx, replace aeval_eq : aeval x p = 0, { rw ← submodule.coe_eq_zero, convert aeval_eq, exact is_scalar_tower.algebra_map_aeval K A L _ _ }, revert ne_zero aeval_eq, refine p.rec_on_horner _ _ _, { intro h, contradiction }, { intros p a hp ha ih ne_zero aeval_eq, refine A.inv_mem_of_root_of_coeff_zero_ne_zero aeval_eq _, rwa [coeff_add, hp, zero_add, coeff_C, if_pos rfl] }, { intros p hp ih ne_zero aeval_eq, rw [alg_hom.map_mul, aeval_X, mul_eq_zero] at aeval_eq, cases aeval_eq with aeval_eq x_eq, { exact ih hp aeval_eq }, { rw [x_eq, submodule.coe_zero, inv_zero], exact A.zero_mem } } end /-- In an algebraic extension L/K, an intermediate subalgebra is a field. -/ lemma subalgebra.is_field_of_algebraic (hKL : algebra.is_algebraic K L) : is_field A := { mul_inv_cancel := λ a ha, ⟨ ⟨a⁻¹, A.inv_mem_of_algebraic (hKL a)⟩, subtype.ext (mul_inv_cancel (mt submodule.coe_eq_zero.mp ha))⟩, .. subalgebra.integral_domain A } end field
1799f38e44c5efd697ee3404687226fc5b0f4c9a
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/algebra/big_operators.lean
e4b3bfd69b6aac3f7bc688d6fe6b5b296dcc4019
[ "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
30,768
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 Some big operators for lists and finite sets. -/ import tactic.tauto data.list.basic data.finset import algebra.group algebra.ordered_group algebra.group_power universes u v w variables {α : Type u} {β : Type v} {γ : Type w} theorem directed.finset_le {r : α → α → Prop} [is_trans α r] {ι} (hι : nonempty ι) {f : ι → α} (D : directed r f) (s : finset ι) : ∃ z, ∀ i ∈ s, r (f i) (f z) := show ∃ z, ∀ i ∈ s.1, r (f i) (f z), from multiset.induction_on s.1 (let ⟨z⟩ := hι in ⟨z, λ _, false.elim⟩) $ λ i s ⟨j, H⟩, let ⟨k, h₁, h₂⟩ := D i j in ⟨k, λ a h, or.cases_on (multiset.mem_cons.1 h) (λ h, h.symm ▸ h₁) (λ h, trans (H _ h) h₂)⟩ namespace finset variables {s s₁ s₂ : finset α} {a : α} {f g : α → β} /-- `prod s f` is the product of `f x` as `x` ranges over the elements of the finite set `s`. -/ @[to_additive finset.sum] protected def prod [comm_monoid β] (s : finset α) (f : α → β) : β := (s.1.map f).prod attribute [to_additive finset.sum.equations._eqn_1] finset.prod.equations._eqn_1 @[to_additive finset.sum_eq_fold] theorem prod_eq_fold [comm_monoid β] (s : finset α) (f : α → β) : s.prod f = s.fold (*) 1 f := rfl section comm_monoid variables [comm_monoid β] @[simp, to_additive finset.sum_empty] lemma prod_empty {α : Type u} {f : α → β} : (∅:finset α).prod f = 1 := rfl @[simp, to_additive finset.sum_insert] lemma prod_insert [decidable_eq α] : a ∉ s → (insert a s).prod f = f a * s.prod f := fold_insert @[simp, to_additive finset.sum_singleton] lemma prod_singleton : (singleton a).prod f = f a := eq.trans fold_singleton $ mul_one _ @[simp] lemma prod_const_one : s.prod (λx, (1 : β)) = 1 := by simp only [finset.prod, multiset.map_const, multiset.prod_repeat, one_pow] @[simp] lemma sum_const_zero {β} {s : finset α} [add_comm_monoid β] : s.sum (λx, (0 : β)) = 0 := @prod_const_one _ (multiplicative β) _ _ attribute [to_additive finset.sum_const_zero] prod_const_one @[simp, to_additive finset.sum_image] lemma prod_image [decidable_eq α] {s : finset γ} {g : γ → α} : (∀x∈s, ∀y∈s, g x = g y → x = y) → (s.image g).prod f = s.prod (λx, f (g x)) := fold_image @[simp, to_additive sum_map] lemma prod_map (s : finset α) (e : α ↪ γ) (f : γ → β): (s.map e).prod f = s.prod (λa, f (e a)) := by rw [finset.prod, finset.map_val, multiset.map_map]; refl @[congr, to_additive finset.sum_congr] lemma prod_congr (h : s₁ = s₂) : (∀x∈s₂, f x = g x) → s₁.prod f = s₂.prod g := by rw [h]; exact fold_congr attribute [congr] finset.sum_congr @[to_additive finset.sum_union_inter] lemma prod_union_inter [decidable_eq α] : (s₁ ∪ s₂).prod f * (s₁ ∩ s₂).prod f = s₁.prod f * s₂.prod f := fold_union_inter @[to_additive finset.sum_union] lemma prod_union [decidable_eq α] (h : s₁ ∩ s₂ = ∅) : (s₁ ∪ s₂).prod f = s₁.prod f * s₂.prod f := by rw [←prod_union_inter, h]; exact (mul_one _).symm @[to_additive finset.sum_sdiff] lemma prod_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) : (s₂ \ s₁).prod f * s₁.prod f = s₂.prod f := by rw [←prod_union (sdiff_inter_self _ _), sdiff_union_of_subset h] @[to_additive finset.sum_bind] lemma prod_bind [decidable_eq α] {s : finset γ} {t : γ → finset α} : (∀x∈s, ∀y∈s, x ≠ y → t x ∩ t y = ∅) → (s.bind t).prod f = s.prod (λx, (t x).prod f) := by haveI := classical.dec_eq γ; exact finset.induction_on s (λ _, by simp only [bind_empty, prod_empty]) (assume x s hxs ih hd, have hd' : ∀x∈s, ∀y∈s, x ≠ y → t x ∩ t y = ∅, from assume _ hx _ hy, hd _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy), have t x ∩ finset.bind s t = ∅, from eq_empty_of_forall_not_mem $ assume a, by rw [mem_inter, mem_bind]; rintro ⟨h₁, y, hys, hy₂⟩; exact eq_empty_iff_forall_not_mem.1 (hd _ (mem_insert_self _ _) _ (mem_insert_of_mem hys) (assume h, hxs (h.symm ▸ hys))) _ (mem_inter_of_mem h₁ hy₂), by simp only [bind_insert, prod_insert hxs, prod_union this, ih hd']) @[to_additive finset.sum_product] lemma prod_product {s : finset γ} {t : finset α} {f : γ×α → β} : (s.product t).prod f = s.prod (λx, t.prod $ λy, f (x, y)) := begin haveI := classical.dec_eq α, haveI := classical.dec_eq γ, rw [product_eq_bind, prod_bind (λ x hx y hy h, eq_empty_of_forall_not_mem _)], { congr, funext, exact prod_image (λ _ _ _ _ H, (prod.mk.inj H).2) }, simp only [mem_inter, mem_image], rintro ⟨_, _⟩ ⟨⟨_, _, _⟩, ⟨_, _, _⟩⟩, apply h, cc end @[to_additive finset.sum_sigma] lemma prod_sigma {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} {f : sigma σ → β} : (s.sigma t).prod f = s.prod (λa, (t a).prod $ λs, f ⟨a, s⟩) := by haveI := classical.dec_eq α; haveI := (λ a, classical.dec_eq (σ a)); exact calc (s.sigma t).prod f = (s.bind (λa, (t a).image (λs, sigma.mk a s))).prod f : by rw sigma_eq_bind ... = s.prod (λa, ((t a).image (λs, sigma.mk a s)).prod f) : prod_bind $ assume a₁ ha a₂ ha₂ h, eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_image]; rintro ⟨_, _⟩ ⟨⟨_, _, _⟩, ⟨_, _, _⟩⟩; apply h; cc ... = (s.prod $ λa, (t a).prod $ λs, f ⟨a, s⟩) : prod_congr rfl $ λ _ _, prod_image $ λ _ _ _ _ _, by cc @[to_additive finset.sum_image'] lemma prod_image' [decidable_eq α] {s : finset γ} {g : γ → α} (h : γ → β) (eq : ∀c∈s, f (g c) = (s.filter (λc', g c' = g c)).prod h) : (s.image g).prod f = s.prod h := begin letI := classical.dec_eq γ, rw [← image_bind_filter_eq s g] {occs := occurrences.pos [2]}, rw [finset.prod_bind], { refine finset.prod_congr rfl (assume a ha, _), rcases finset.mem_image.1 ha with ⟨b, hb, rfl⟩, exact eq b hb }, assume a₀ _ a₁ _ ne, refine disjoint_iff_inter_eq_empty.1 (disjoint_iff_ne.2 _), assume c₀ h₀ c₁ h₁, rcases mem_filter.1 h₀ with ⟨h₀, rfl⟩, rcases mem_filter.1 h₁ with ⟨h₁, rfl⟩, exact mt (congr_arg g) ne end @[to_additive finset.sum_add_distrib] lemma prod_mul_distrib : s.prod (λx, f x * g x) = s.prod f * s.prod g := eq.trans (by rw one_mul; refl) fold_op_distrib @[to_additive finset.sum_comm] lemma prod_comm [decidable_eq γ] {s : finset γ} {t : finset α} {f : γ → α → β} : s.prod (λx, t.prod $ f x) = t.prod (λy, s.prod $ λx, f x y) := finset.induction_on s (by simp only [prod_empty, prod_const_one]) $ λ _ _ H ih, by simp only [prod_insert H, prod_mul_distrib, ih] lemma prod_hom [comm_monoid γ] (g : β → γ) [is_monoid_hom g] : s.prod (λx, g (f x)) = g (s.prod f) := eq.trans (by rw is_monoid_hom.map_one g; refl) (fold_hom (λ _ _, is_monoid_hom.map_mul g)) @[to_additive finset.sum_hom_rel] lemma prod_hom_rel [comm_monoid γ] {r : β → γ → Prop} {f : α → β} {g : α → γ} {s : finset α} (h₁ : r 1 1) (h₂ : ∀a b c, r b c → r (f a * b) (g a * c)) : r (s.prod f) (s.prod g) := begin letI := classical.dec_eq α, refine finset.induction_on s h₁ (assume a s has ih, _), rw [prod_insert has, prod_insert has], exact h₂ a (s.prod f) (s.prod g) ih, end @[to_additive finset.sum_subset] lemma prod_subset (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → f x = 1) : s₁.prod f = s₂.prod f := by haveI := classical.dec_eq α; exact have (s₂ \ s₁).prod f = (s₂ \ s₁).prod (λx, 1), from prod_congr rfl $ by simpa only [mem_sdiff, and_imp], by rw [←prod_sdiff h]; simp only [this, prod_const_one, one_mul] @[to_additive sum_filter] lemma prod_filter (p : α → Prop) [decidable_pred p] (f : α → β) : (s.filter p).prod f = s.prod (λa, if p a then f a else 1) := calc (s.filter p).prod f = (s.filter p).prod (λa, if p a then f a else 1) : prod_congr rfl (assume a h, by rw [if_pos (mem_filter.1 h).2]) ... = s.prod (λa, if p a then f a else 1) : begin refine prod_subset (filter_subset s) (assume x hs h, _), rw [mem_filter, not_and] at h, exact if_neg (h hs) end @[to_additive finset.sum_eq_single] lemma prod_eq_single {s : finset α} {f : α → β} (a : α) (h₀ : ∀b∈s, b ≠ a → f b = 1) (h₁ : a ∉ s → f a = 1) : s.prod f = f a := by haveI := classical.dec_eq α; from classical.by_cases (assume : a ∈ s, calc s.prod f = ({a} : finset α).prod f : begin refine (prod_subset _ _).symm, { intros _ H, rwa mem_singleton.1 H }, { simpa only [mem_singleton] } end ... = f a : prod_singleton) (assume : a ∉ s, (prod_congr rfl $ λ b hb, h₀ b hb $ by rintro rfl; cc).trans $ prod_const_one.trans (h₁ this).symm) @[to_additive finset.sum_attach] lemma prod_attach {f : α → β} : s.attach.prod (λx, f x.val) = s.prod f := by haveI := classical.dec_eq α; exact calc s.attach.prod (λx, f x.val) = ((s.attach).image subtype.val).prod f : by rw [prod_image]; exact assume x _ y _, subtype.eq ... = _ : by rw [attach_image_val] @[to_additive finset.sum_bij] lemma prod_bij {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, γ) (hi : ∀a ha, i a ha ∈ t) (h : ∀a ha, f a = g (i a ha)) (i_inj : ∀a₁ a₂ ha₁ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀b∈t, ∃a ha, b = i a ha) : s.prod f = t.prod g := by haveI := classical.prop_decidable; exact calc s.prod f = s.attach.prod (λx, f x.val) : prod_attach.symm ... = s.attach.prod (λx, g (i x.1 x.2)) : prod_congr rfl $ assume x hx, h _ _ ... = (s.attach.image $ λx:{x // x ∈ s}, i x.1 x.2).prod g : (prod_image $ assume (a₁:{x // x ∈ s}) _ a₂ _ eq, subtype.eq $ i_inj a₁.1 a₂.1 a₁.2 a₂.2 eq).symm ... = t.prod g : prod_subset (by simp only [subset_iff, mem_image, mem_attach]; rintro _ ⟨⟨_, _⟩, _, rfl⟩; solve_by_elim) (assume b hb hb1, false.elim $ hb1 $ by rcases i_surj b hb with ⟨a, ha, rfl⟩; exact mem_image.2 ⟨⟨_, _⟩, mem_attach _ _, rfl⟩) @[to_additive finset.sum_bij_ne_zero] lemma prod_bij_ne_one {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, f a ≠ 1 → γ) (hi₁ : ∀a h₁ h₂, i a h₁ h₂ ∈ t) (hi₂ : ∀a₁ a₂ h₁₁ h₁₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂) (hi₃ : ∀b∈t, g b ≠ 1 → ∃a h₁ h₂, b = i a h₁ h₂) (h : ∀a h₁ h₂, f a = g (i a h₁ h₂)) : s.prod f = t.prod g := by haveI := classical.prop_decidable; exact calc s.prod f = (s.filter $ λx, f x ≠ 1).prod f : (prod_subset (filter_subset _) $ by simp only [not_imp_comm, mem_filter]; exact λ _, and.intro).symm ... = (t.filter $ λx, g x ≠ 1).prod g : prod_bij (assume a ha, i a (mem_filter.mp ha).1 (mem_filter.mp ha).2) (assume a ha, (mem_filter.mp ha).elim $ λh₁ h₂, mem_filter.mpr ⟨hi₁ a h₁ h₂, λ hg, h₂ (hg ▸ h a h₁ h₂)⟩) (assume a ha, (mem_filter.mp ha).elim $ h a) (assume a₁ a₂ ha₁ ha₂, (mem_filter.mp ha₁).elim $ λha₁₁ ha₁₂, (mem_filter.mp ha₂).elim $ λha₂₁ ha₂₂, hi₂ a₁ a₂ _ _ _ _) (assume b hb, (mem_filter.mp hb).elim $ λh₁ h₂, let ⟨a, ha₁, ha₂, eq⟩ := hi₃ b h₁ h₂ in ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩) ... = t.prod g : (prod_subset (filter_subset _) $ by simp only [not_imp_comm, mem_filter]; exact λ _, and.intro) @[to_additive finset.exists_ne_zero_of_sum_ne_zero] lemma exists_ne_one_of_prod_ne_one : s.prod f ≠ 1 → ∃a∈s, f a ≠ 1 := by haveI := classical.dec_eq α; exact finset.induction_on s (λ H, (H rfl).elim) (assume a s has ih h, classical.by_cases (assume ha : f a = 1, let ⟨a, ha, hfa⟩ := ih (by rwa [prod_insert has, ha, one_mul] at h) in ⟨a, mem_insert_of_mem ha, hfa⟩) (assume hna : f a ≠ 1, ⟨a, mem_insert_self _ _, hna⟩)) @[to_additive finset.sum_range_succ] lemma prod_range_succ (f : ℕ → β) (n : ℕ) : (range (nat.succ n)).prod f = f n * (range n).prod f := by rw [range_succ, prod_insert not_mem_range_self] lemma prod_range_succ' (f : ℕ → β) : ∀ n : ℕ, (range (nat.succ n)).prod f = (range n).prod (f ∘ nat.succ) * f 0 | 0 := (prod_range_succ _ _).trans $ mul_comm _ _ | (n + 1) := by rw [prod_range_succ (λ m, f (nat.succ m)), mul_assoc, ← prod_range_succ']; exact prod_range_succ _ _ @[simp] lemma prod_const (b : β) : s.prod (λ a, b) = b ^ s.card := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by rw [prod_insert has, card_insert_of_not_mem has, pow_succ, ih]) lemma prod_pow (s : finset α) (n : ℕ) (f : α → β) : s.prod (λ x, f x ^ n) = s.prod f ^ n := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [_root_.mul_pow] {contextual := tt}) lemma prod_nat_pow (s : finset α) (n : ℕ) (f : α → ℕ) : s.prod (λ x, f x ^ n) = s.prod f ^ n := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [nat.mul_pow] {contextual := tt}) @[to_additive finset.sum_involution] lemma prod_involution {s : finset α} {f : α → β} : ∀ (g : Π a ∈ s, α) (h₁ : ∀ a ha, f a * f (g a ha) = 1) (h₂ : ∀ a ha, f a ≠ 1 → g a ha ≠ a) (h₃ : ∀ a ha, g a ha ∈ s) (h₄ : ∀ a ha, g (g a ha) (h₃ a ha) = a), s.prod f = 1 := by haveI := classical.dec_eq α; haveI := classical.dec_eq β; exact finset.strong_induction_on s (λ s ih g h₁ h₂ h₃ h₄, if hs : s = ∅ then hs.symm ▸ rfl else let ⟨x, hx⟩ := exists_mem_of_ne_empty hs in have hmem : ∀ y ∈ (s.erase x).erase (g x hx), y ∈ s, from λ y hy, (mem_of_mem_erase (mem_of_mem_erase hy)), have g_inj : ∀ {x hx y hy}, g x hx = g y hy → x = y, from λ x hx y hy h, by rw [← h₄ x hx, ← h₄ y hy]; simp [h], have ih': (erase (erase s x) (g x hx)).prod f = (1 : β) := ih ((s.erase x).erase (g x hx)) ⟨subset.trans (erase_subset _ _) (erase_subset _ _), λ h, not_mem_erase (g x hx) (s.erase x) (h (h₃ x hx))⟩ (λ y hy, g y (hmem y hy)) (λ y hy, h₁ y (hmem y hy)) (λ y hy, h₂ y (hmem y hy)) (λ y hy, mem_erase.2 ⟨λ (h : g y _ = g x hx), by simpa [g_inj h] using hy, mem_erase.2 ⟨λ (h : g y _ = x), have y = g x hx, from h₄ y (hmem y hy) ▸ by simp [h], by simpa [this] using hy, h₃ y (hmem y hy)⟩⟩) (λ y hy, h₄ y (hmem y hy)), if hx1 : f x = 1 then ih' ▸ eq.symm (prod_subset hmem (λ y hy hy₁, have y = x ∨ y = g x hx, by simp [hy] at hy₁; tauto, this.elim (λ h, h.symm ▸ hx1) (λ h, h₁ x hx ▸ h ▸ hx1.symm ▸ (one_mul _).symm))) else by rw [← insert_erase hx, prod_insert (not_mem_erase _ _), ← insert_erase (mem_erase.2 ⟨h₂ x hx hx1, h₃ x hx⟩), prod_insert (not_mem_erase _ _), ih', mul_one, h₁ x hx]) @[to_additive finset.sum_eq_zero] lemma prod_eq_one [comm_monoid β] {f : α → β} {s : finset α} (h : ∀x∈s, f x = 1) : s.prod f = 1 := calc s.prod f = s.prod (λx, 1) : finset.prod_congr rfl h ... = 1 : finset.prod_const_one end comm_monoid lemma sum_hom [add_comm_monoid β] [add_comm_monoid γ] (g : β → γ) [is_add_monoid_hom g] : s.sum (λx, g (f x)) = g (s.sum f) := eq.trans (by rw is_add_monoid_hom.map_zero g; refl) (fold_hom (λ _ _, is_add_monoid_hom.map_add g)) attribute [to_additive finset.sum_hom] prod_hom lemma sum_smul [add_comm_monoid β] (s : finset α) (n : ℕ) (f : α → β) : s.sum (λ x, add_monoid.smul n (f x)) = add_monoid.smul n (s.sum f) := @prod_pow _ (multiplicative β) _ _ _ _ attribute [to_additive finset.sum_smul] prod_pow @[simp] lemma sum_const [add_comm_monoid β] (b : β) : s.sum (λ a, b) = add_monoid.smul s.card b := @prod_const _ (multiplicative β) _ _ _ attribute [to_additive finset.sum_const] prod_const lemma sum_range_succ' [add_comm_monoid β] (f : ℕ → β) : ∀ n : ℕ, (range (nat.succ n)).sum f = (range n).sum (f ∘ nat.succ) + f 0 := @prod_range_succ' (multiplicative β) _ _ attribute [to_additive finset.sum_range_succ'] prod_range_succ' lemma sum_nat_cast [add_comm_monoid β] [has_one β] (s : finset α) (f : α → ℕ) : ↑(s.sum f) = s.sum (λa, f a : α → β) := (sum_hom _).symm lemma le_sum_of_subadditive [add_comm_monoid α] [ordered_comm_monoid β] (f : α → β) (h_zero : f 0 = 0) (h_add : ∀x y, f (x + y) ≤ f x + f y) (s : finset γ) (g : γ → α) : f (s.sum g) ≤ s.sum (λc, f (g c)):= begin refine le_trans (multiset.le_sum_of_subadditive f h_zero h_add _) _, rw [multiset.map_map], refl end lemma abs_sum_le_sum_abs [discrete_linear_ordered_field α] {f : β → α} {s : finset β} : abs (s.sum f) ≤ s.sum (λa, abs (f a)) := le_sum_of_subadditive _ abs_zero abs_add s f section comm_group variables [comm_group β] @[simp, to_additive finset.sum_neg_distrib] lemma prod_inv_distrib : s.prod (λx, (f x)⁻¹) = (s.prod f)⁻¹ := prod_hom has_inv.inv end comm_group @[simp] theorem card_sigma {σ : α → Type*} (s : finset α) (t : Π a, finset (σ a)) : card (s.sigma t) = s.sum (λ a, card (t a)) := multiset.card_sigma _ _ lemma card_bind [decidable_eq β] {s : finset α} {t : α → finset β} (h : ∀ x ∈ s, ∀ y ∈ s, x ≠ y → t x ∩ t y = ∅) : (s.bind t).card = s.sum (λ u, card (t u)) := calc (s.bind t).card = (s.bind t).sum (λ _, 1) : by simp ... = s.sum (λ a, (t a).sum (λ _, 1)) : finset.sum_bind h ... = s.sum (λ u, card (t u)) : by simp lemma card_bind_le [decidable_eq β] {s : finset α} {t : α → finset β} : (s.bind t).card ≤ s.sum (λ a, (t a).card) := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (λ a s has ih, calc ((insert a s).bind t).card ≤ (t a).card + (s.bind t).card : by rw bind_insert; exact finset.card_union_le _ _ ... ≤ (insert a s).sum (λ a, card (t a)) : by rw sum_insert has; exact add_le_add_left ih _) lemma gsmul_sum [add_comm_group β] {f : α → β} {s : finset α} (z : ℤ) : gsmul z (s.sum f) = s.sum (λa, gsmul z (f a)) := (finset.sum_hom (gsmul z)).symm end finset namespace finset variables {s s₁ s₂ : finset α} {f g : α → β} {b : β} {a : α} @[simp] lemma sum_sub_distrib [add_comm_group β] : s.sum (λx, f x - g x) = s.sum f - s.sum g := sum_add_distrib.trans $ congr_arg _ sum_neg_distrib section ordered_cancel_comm_monoid variables [decidable_eq α] [ordered_cancel_comm_monoid β] lemma sum_le_sum : (∀x∈s, f x ≤ g x) → s.sum f ≤ s.sum g := finset.induction_on s (λ _, le_refl _) $ assume a s ha ih h, have f a + s.sum f ≤ g a + s.sum g, from add_le_add (h _ (mem_insert_self _ _)) (ih $ assume x hx, h _ $ mem_insert_of_mem hx), by simpa only [sum_insert ha] lemma zero_le_sum (h : ∀x∈s, 0 ≤ f x) : 0 ≤ s.sum f := le_trans (by rw [sum_const_zero]) (sum_le_sum h) lemma sum_le_zero (h : ∀x∈s, f x ≤ 0) : s.sum f ≤ 0 := le_trans (sum_le_sum h) (by rw [sum_const_zero]) end ordered_cancel_comm_monoid section semiring variables [semiring β] lemma sum_mul : s.sum f * b = s.sum (λx, f x * b) := (sum_hom (λx, x * b)).symm lemma mul_sum : b * s.sum f = s.sum (λx, b * f x) := (sum_hom (λx, b * x)).symm end semiring section comm_semiring variables [decidable_eq α] [comm_semiring β] lemma prod_eq_zero (ha : a ∈ s) (h : f a = 0) : s.prod f = 0 := calc s.prod f = (insert a (erase s a)).prod f : by rw insert_erase ha ... = 0 : by rw [prod_insert (not_mem_erase _ _), h, zero_mul] lemma prod_sum {δ : α → Type*} [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa, finset (δ a)} {f : Πa, δ a → β} : s.prod (λa, (t a).sum (λb, f a b)) = (s.pi t).sum (λp, s.attach.prod (λx, f x.1 (p x.1 x.2))) := begin induction s using finset.induction with a s ha ih, { rw [pi_empty, sum_singleton], refl }, { have h₁ : ∀x ∈ t a, ∀y ∈ t a, ∀h : x ≠ y, image (pi.cons s a x) (pi s t) ∩ image (pi.cons s a y) (pi s t) = ∅, { assume x hx y hy h, apply eq_empty_of_forall_not_mem, simp only [mem_inter, mem_image], rintro p₁ ⟨⟨p₂, hp, eq⟩, ⟨p₃, hp₃, rfl⟩⟩, have : pi.cons s a x p₂ a (mem_insert_self _ _) = pi.cons s a y p₃ a (mem_insert_self _ _), { rw [eq] }, rw [pi.cons_same, pi.cons_same] at this, exact h this }, rw [prod_insert ha, pi_insert ha, ih, sum_mul, sum_bind h₁], refine sum_congr rfl (λ b _, _), have h₂ : ∀p₁∈pi s t, ∀p₂∈pi s t, pi.cons s a b p₁ = pi.cons s a b p₂ → p₁ = p₂, from assume p₁ h₁ p₂ h₂ eq, injective_pi_cons ha eq, rw [sum_image h₂, mul_sum], refine sum_congr rfl (λ g _, _), rw [attach_insert, prod_insert, prod_image], { simp only [pi.cons_same], congr', ext ⟨v, hv⟩, congr', exact (pi.cons_ne (by rintro rfl; exact ha hv)).symm }, { exact λ _ _ _ _, subtype.eq ∘ subtype.mk.inj }, { simp only [mem_image], rintro ⟨⟨_, hm⟩, _, rfl⟩, exact ha hm } } end end comm_semiring section integral_domain /- add integral_semi_domain to support nat and ennreal -/ variables [decidable_eq α] [integral_domain β] lemma prod_eq_zero_iff : s.prod f = 0 ↔ (∃a∈s, f a = 0) := finset.induction_on s ⟨not.elim one_ne_zero, λ ⟨_, H, _⟩, H.elim⟩ $ λ a s ha ih, by rw [prod_insert ha, mul_eq_zero_iff_eq_zero_or_eq_zero, bex_def, exists_mem_insert, ih, ← bex_def] end integral_domain section ordered_comm_monoid variables [decidable_eq α] [ordered_comm_monoid β] lemma sum_le_sum' : (∀x∈s, f x ≤ g x) → s.sum f ≤ s.sum g := finset.induction_on s (λ _, le_refl _) $ assume a s ha ih h, have f a + s.sum f ≤ g a + s.sum g, from add_le_add' (h _ (mem_insert_self _ _)) (ih $ assume x hx, h _ $ mem_insert_of_mem hx), by simpa only [sum_insert ha] lemma zero_le_sum' (h : ∀x∈s, 0 ≤ f x) : 0 ≤ s.sum f := le_trans (by rw [sum_const_zero]) (sum_le_sum' h) lemma sum_le_zero' (h : ∀x∈s, f x ≤ 0) : s.sum f ≤ 0 := le_trans (sum_le_sum' h) (by rw [sum_const_zero]) lemma sum_le_sum_of_subset_of_nonneg (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → 0 ≤ f x) : s₁.sum f ≤ s₂.sum f := calc s₁.sum f ≤ (s₂ \ s₁).sum f + s₁.sum f : le_add_of_nonneg_left' $ zero_le_sum' $ by simpa only [mem_sdiff, and_imp] ... = (s₂ \ s₁ ∪ s₁).sum f : (sum_union (sdiff_inter_self _ _)).symm ... = s₂.sum f : by rw [sdiff_union_of_subset h] lemma sum_eq_zero_iff_of_nonneg : (∀x∈s, 0 ≤ f x) → (s.sum f = 0 ↔ ∀x∈s, f x = 0) := finset.induction_on s (λ _, ⟨λ _ _, false.elim, λ _, rfl⟩) $ λ a s ha ih H, have ∀ x ∈ s, 0 ≤ f x, from λ _, H _ ∘ mem_insert_of_mem, by rw [sum_insert ha, add_eq_zero_iff' (H _ $ mem_insert_self _ _) (zero_le_sum' this), forall_mem_insert, ih this] lemma single_le_sum (hf : ∀x∈s, 0 ≤ f x) {a} (h : a ∈ s) : f a ≤ s.sum f := have (singleton a).sum f ≤ s.sum f, from sum_le_sum_of_subset_of_nonneg (λ x e, (mem_singleton.1 e).symm ▸ h) (λ x h _, hf x h), by rwa sum_singleton at this end ordered_comm_monoid section canonically_ordered_monoid variables [decidable_eq α] [canonically_ordered_monoid β] [@decidable_rel β (≤)] lemma sum_le_sum_of_subset (h : s₁ ⊆ s₂) : s₁.sum f ≤ s₂.sum f := sum_le_sum_of_subset_of_nonneg h $ assume x h₁ h₂, zero_le _ lemma sum_le_sum_of_ne_zero (h : ∀x∈s₁, f x ≠ 0 → x ∈ s₂) : s₁.sum f ≤ s₂.sum f := calc s₁.sum f = (s₁.filter (λx, f x = 0)).sum f + (s₁.filter (λx, f x ≠ 0)).sum f : by rw [←sum_union, filter_union_filter_neg_eq]; apply filter_inter_filter_neg_eq ... ≤ s₂.sum f : add_le_of_nonpos_of_le' (sum_le_zero' $ by simp only [mem_filter, and_imp]; exact λ _ _, le_of_eq) (sum_le_sum_of_subset $ by simpa only [subset_iff, mem_filter, and_imp]) end canonically_ordered_monoid @[simp] lemma card_pi [decidable_eq α] {δ : α → Type*} (s : finset α) (t : Π a, finset (δ a)) : (s.pi t).card = s.prod (λ a, card (t a)) := multiset.card_pi _ _ @[simp] lemma prod_range_id_eq_fact (n : ℕ) : ((range n.succ).erase 0).prod (λ x, x) = nat.fact n := calc ((range n.succ).erase 0).prod (λ x, x) = (range n).prod nat.succ : eq.symm (prod_bij (λ x _, nat.succ x) (λ a h₁, mem_erase.2 ⟨nat.succ_ne_zero _, mem_range.2 $ nat.succ_lt_succ $ by simpa using h₁⟩) (by simp) (λ _ _ _ _, nat.succ_inj) (λ b h, have b.pred.succ = b, from nat.succ_pred_eq_of_pos $ by simp [nat.pos_iff_ne_zero] at *; tauto, ⟨nat.pred b, mem_range.2 $ nat.lt_of_succ_lt_succ (by simp [*] at *), this.symm⟩)) ... = nat.fact n : by induction n; simp [*, range_succ] end finset section geom_sum open finset theorem geom_sum_mul_add [semiring α] (x : α) : ∀ (n : ℕ), ((range n).sum (λ i, (x+1)^i)) * x + 1 = (x+1)^n | 0 := by simp | (n+1) := calc (range (n + 1)).sum (λi, (x + 1) ^ i) * x + 1 = (x + 1)^n * x + (((range n).sum (λ i, (x+1)^i)) * x + 1) : by simp [range_add_one, add_mul] ... = (x + 1)^n * x + (x + 1)^n : by rw geom_sum_mul_add n ... = (x + 1) ^ (n + 1) : by simp [pow_add, mul_add] theorem geom_sum_mul [ring α] (x : α) (n : ℕ) : ((range n).sum (λ i, x^i)) * (x-1) = x^n-1 := have _ := geom_sum_mul_add (x-1) n, by rw [sub_add_cancel] at this; rw [← this, add_sub_cancel] theorem geom_sum [division_ring α] {x : α} (h : x ≠ 1) (n : ℕ) : (range n).sum (λ i, x^i) = (x^n-1)/(x-1) := have x - 1 ≠ 0, by simp [*, -sub_eq_add_neg, sub_eq_iff_eq_add] at *, by rw [← geom_sum_mul, mul_div_cancel _ this] lemma geom_sum_inv [division_ring α] {x : α} (hx1 : x ≠ 1) (hx0 : x ≠ 0) (n : ℕ) : (range n).sum (λ m, x⁻¹ ^ m) = (x - 1)⁻¹ * (x - x⁻¹ ^ n * x) := have h₁ : x⁻¹ ≠ 1, by rwa [inv_eq_one_div, ne.def, div_eq_iff_mul_eq hx0, one_mul], have h₂ : x⁻¹ - 1 ≠ 0, from mt sub_eq_zero.1 h₁, have h₃ : x - 1 ≠ 0, from mt sub_eq_zero.1 hx1, have h₄ : x * x⁻¹ ^ n = x⁻¹ ^ n * x, from nat.cases_on n (by simp) (λ _, by conv { to_rhs, rw [pow_succ', mul_assoc, inv_mul_cancel hx0, mul_one] }; rw [pow_succ, ← mul_assoc, mul_inv_cancel hx0, one_mul]), by rw [geom_sum h₁, div_eq_iff_mul_eq h₂, ← domain.mul_left_inj h₃, ← mul_assoc, ← mul_assoc, mul_inv_cancel h₃]; simp [mul_add, add_mul, mul_inv_cancel hx0, mul_assoc, h₄] end geom_sum namespace finset section gauss_sum /-- Gauss' summation formula -/ lemma sum_range_id_mul_two : ∀(n : ℕ), (finset.range n).sum (λi, i) * 2 = n * (n - 1) | 0 := rfl | 1 := rfl | ((n + 1) + 1) := begin rw [sum_range_succ, add_mul, sum_range_id_mul_two (n + 1), mul_comm, two_mul, nat.add_sub_cancel, nat.add_sub_cancel, mul_comm _ n], simp only [add_mul, one_mul, add_comm, add_assoc, add_left_comm] end /-- Gauss' summation formula -/ lemma sum_range_id (n : ℕ) : (finset.range n).sum (λi, i) = (n * (n - 1)) / 2 := by rw [← sum_range_id_mul_two n, nat.mul_div_cancel]; exact dec_trivial end gauss_sum end finset section group open list variables [group α] [group β] @[to_additive is_add_group_hom.map_sum] theorem is_group_hom.map_prod (f : α → β) [is_group_hom f] (l : list α) : f (prod l) = prod (map f l) := by induction l; simp only [*, is_group_hom.map_mul f, is_group_hom.map_one f, prod_nil, prod_cons, map] theorem is_group_anti_hom.map_prod (f : α → β) [is_group_anti_hom f] (l : list α) : f (prod l) = prod (map f (reverse l)) := by induction l with hd tl ih; [exact is_group_anti_hom.map_one f, simp only [prod_cons, is_group_anti_hom.map_mul f, ih, reverse_cons, map_append, prod_append, map_singleton, prod_cons, prod_nil, mul_one]] theorem inv_prod : ∀ l : list α, (prod l)⁻¹ = prod (map (λ x, x⁻¹) (reverse l)) := λ l, @is_group_anti_hom.map_prod _ _ _ _ _ inv_is_group_anti_hom l -- TODO there is probably a cleaner proof of this end group section comm_group variables [comm_group α] [comm_group β] (f : α → β) [is_group_hom f] @[to_additive is_add_group_hom.multiset_sum] lemma is_group_hom.map_multiset_prod (m : multiset α) : f m.prod = (m.map f).prod := quotient.induction_on m $ assume l, by simp [is_group_hom.map_prod f l] @[to_additive is_add_group_hom.finset_sum] lemma is_group_hom.finset_prod (g : γ → α) (s : finset γ) : f (s.prod g) = s.prod (f ∘ g) := show f (s.val.map g).prod = (s.val.map (f ∘ g)).prod, by rw [is_group_hom.map_multiset_prod f]; simp end comm_group @[to_additive is_add_group_hom_finset_sum] lemma is_group_hom_finset_prod {α β γ} [group α] [comm_group β] (s : finset γ) (f : γ → α → β) [∀c, is_group_hom (f c)] : is_group_hom (λa, s.prod (λc, f c a)) := ⟨assume a b, by simp only [λc, is_group_hom.map_mul (f c), finset.prod_mul_distrib]⟩ attribute [instance] is_group_hom_finset_prod is_add_group_hom_finset_sum namespace multiset variables [decidable_eq α] @[simp] lemma to_finset_sum_count_eq (s : multiset α) : s.to_finset.sum (λa, s.count a) = s.card := multiset.induction_on s rfl (assume a s ih, calc (to_finset (a :: s)).sum (λx, count x (a :: s)) = (to_finset (a :: s)).sum (λx, (if x = a then 1 else 0) + count x s) : finset.sum_congr rfl $ λ _ _, by split_ifs; [simp only [h, count_cons_self, nat.one_add], simp only [count_cons_of_ne h, zero_add]] ... = card (a :: s) : begin by_cases a ∈ s.to_finset, { have : (to_finset s).sum (λx, ite (x = a) 1 0) = (finset.singleton a).sum (λx, ite (x = a) 1 0), { apply (finset.sum_subset _ _).symm, { intros _ H, rwa mem_singleton.1 H }, { exact λ _ _ H, if_neg (mt finset.mem_singleton.2 H) } }, rw [to_finset_cons, finset.insert_eq_of_mem h, finset.sum_add_distrib, ih, this, finset.sum_singleton, if_pos rfl, add_comm, card_cons] }, { have ha : a ∉ s, by rwa mem_to_finset at h, have : (to_finset s).sum (λx, ite (x = a) 1 0) = (to_finset s).sum (λx, 0), from finset.sum_congr rfl (λ x hx, if_neg $ by rintro rfl; cc), rw [to_finset_cons, finset.sum_insert h, if_pos rfl, finset.sum_add_distrib, this, finset.sum_const_zero, ih, count_eq_zero_of_not_mem ha, zero_add, add_comm, card_cons] } end) end multiset
1903e6955ec514112a7546a85fca39c11f0bbc08
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/big_operators/fin.lean
6196b758c417f4c95c64aaeecb594df7ed696cf6
[ "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
974
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import algebra.big_operators.basic import data.fintype.fin /-! # Big operators and `fin` Some results about products and sums over the type `fin`. -/ open_locale big_operators open finset namespace fin @[to_additive] lemma prod_filter_zero_lt {M : Type*} [comm_monoid M] {n : ℕ} {v : fin n.succ → M} : ∏ i in univ.filter (λ (i : fin n.succ), 0 < i), v i = ∏ (j : fin n), v j.succ := by rw [univ_filter_zero_lt, finset.prod_map, rel_embedding.coe_fn_to_embedding, coe_succ_embedding] @[to_additive] lemma prod_filter_succ_lt {M : Type*} [comm_monoid M] {n : ℕ} (j : fin n) (v : fin n.succ → M) : ∏ i in univ.filter (λ i, j.succ < i), v i = ∏ j in univ.filter (λ i, j < i), v j.succ := by rw [univ_filter_succ_lt, finset.prod_map, rel_embedding.coe_fn_to_embedding, coe_succ_embedding] end fin
476acceeeb1a570c93b025b107d78fe10d5c5680
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/data/int/modeq.lean
332fb68aa262adae350e13587d0e39db1ebbae54
[ "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
7,498
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.nat.modeq import tactic.ring /-! # Congruences modulo an integer This file defines the equivalence relation `a ≡ b [ZMOD n]` on the integers, similarly to how `data.nat.modeq` defines them for the natural numbers. The notation is short for `n.modeq a b`, which is defined to be `a % n = b % n` for integers `a b n`. ## Tags modeq, congruence, mod, MOD, modulo, integers -/ namespace int /-- `a ≡ b [ZMOD n]` when `a % n = b % n`. -/ @[derive decidable] def modeq (n a b : ℤ) := a % n = b % n notation a ` ≡ `:50 b ` [ZMOD `:50 n `]`:0 := modeq n a b variables {m n a b c d : ℤ} namespace modeq @[refl] protected theorem refl (a : ℤ) : a ≡ a [ZMOD n] := @rfl _ _ protected theorem rfl : a ≡ a [ZMOD n] := modeq.refl _ @[symm] protected theorem symm : a ≡ b [ZMOD n] → b ≡ a [ZMOD n] := eq.symm @[trans] protected theorem trans : a ≡ b [ZMOD n] → b ≡ c [ZMOD n] → a ≡ c [ZMOD n] := eq.trans end modeq lemma coe_nat_modeq_iff {a b n : ℕ} : a ≡ b [ZMOD n] ↔ a ≡ b [MOD n] := by unfold modeq nat.modeq; rw ← int.coe_nat_eq_coe_nat_iff; simp [coe_nat_mod] theorem modeq_zero_iff_dvd : a ≡ 0 [ZMOD n] ↔ n ∣ a := by rw [modeq, zero_mod, dvd_iff_mod_eq_zero] lemma _root_.has_dvd.dvd.modeq_zero_int (h : n ∣ a) : a ≡ 0 [ZMOD n] := modeq_zero_iff_dvd.2 h lemma _root_.has_dvd.dvd.zero_modeq_int (h : n ∣ a) : 0 ≡ a [ZMOD n] := h.modeq_zero_int.symm theorem modeq_iff_dvd : a ≡ b [ZMOD n] ↔ n ∣ b - a := by rw [modeq, eq_comm]; simp [mod_eq_mod_iff_mod_sub_eq_zero, dvd_iff_mod_eq_zero, -euclidean_domain.mod_eq_zero] theorem modeq.dvd : a ≡ b [ZMOD n] → n ∣ b - a := modeq_iff_dvd.1 theorem modeq_of_dvd : n ∣ b - a → a ≡ b [ZMOD n] := modeq_iff_dvd.2 theorem mod_modeq (a n) : a % n ≡ a [ZMOD n] := mod_mod _ _ namespace modeq protected theorem modeq_of_dvd (d : m ∣ n) (h : a ≡ b [ZMOD n]) : a ≡ b [ZMOD m] := modeq_iff_dvd.2 $ dvd_trans d h.dvd protected theorem mul_left' (hc : 0 ≤ c) (h : a ≡ b [ZMOD n]) : c * a ≡ c * b [ZMOD (c * n)] := or.cases_on hc.lt_or_eq (λ hc, by unfold modeq; simp [mul_mod_mul_of_pos hc, (show _ = _, from h)] ) (λ hc, by simp [hc.symm]) protected theorem mul_right' (hc : 0 ≤ c) (h : a ≡ b [ZMOD n]) : a * c ≡ b * c [ZMOD (n * c)] := by rw [mul_comm a, mul_comm b, mul_comm n]; exact h.mul_left' hc protected theorem add (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a + c ≡ b + d [ZMOD n] := modeq_iff_dvd.2 $ by { convert dvd_add h₁.dvd h₂.dvd, ring } protected theorem add_left (c : ℤ) (h : a ≡ b [ZMOD n]) : c + a ≡ c + b [ZMOD n] := modeq.rfl.add h protected theorem add_right (c : ℤ) (h : a ≡ b [ZMOD n]) : a + c ≡ b + c [ZMOD n] := h.add modeq.rfl protected theorem add_left_cancel (h₁ : a ≡ b [ZMOD n]) (h₂ : a + c ≡ b + d [ZMOD n]) : c ≡ d [ZMOD n] := have d - c = b + d - (a + c) - (b - a) := by ring, modeq_iff_dvd.2 $ by { rw [this], exact dvd_sub h₂.dvd h₁.dvd } protected theorem add_left_cancel' (c : ℤ) (h : c + a ≡ c + b [ZMOD n]) : a ≡ b [ZMOD n] := modeq.rfl.add_left_cancel h protected theorem add_right_cancel (h₁ : c ≡ d [ZMOD n]) (h₂ : a + c ≡ b + d [ZMOD n]) : a ≡ b [ZMOD n] := by { rw [add_comm a, add_comm b] at h₂, exact h₁.add_left_cancel h₂ } protected theorem add_right_cancel' (c : ℤ) (h : a + c ≡ b + c [ZMOD n]) : a ≡ b [ZMOD n] := modeq.rfl.add_right_cancel h protected theorem neg (h : a ≡ b [ZMOD n]) : -a ≡ -b [ZMOD n] := h.add_left_cancel (by simp_rw [←sub_eq_add_neg, sub_self]) protected theorem sub (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a - c ≡ b - d [ZMOD n] := by { rw [sub_eq_add_neg, sub_eq_add_neg], exact h₁.add h₂.neg } protected theorem sub_left (c : ℤ) (h : a ≡ b [ZMOD n]) : c - a ≡ c - b [ZMOD n] := modeq.rfl.sub h protected theorem sub_right (c : ℤ) (h : a ≡ b [ZMOD n]) : a - c ≡ b - c [ZMOD n] := h.sub modeq.rfl protected theorem mul_left (c : ℤ) (h : a ≡ b [ZMOD n]) : c * a ≡ c * b [ZMOD n] := or.cases_on (le_total 0 c) (λ hc, (h.mul_left' hc).modeq_of_dvd (dvd_mul_left _ _) ) (λ hc, by rw [← neg_neg c, ← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul _ b]; exact ((h.mul_left' $ neg_nonneg.2 hc).modeq_of_dvd (dvd_mul_left _ _)).neg) protected theorem mul_right (c : ℤ) (h : a ≡ b [ZMOD n]) : a * c ≡ b * c [ZMOD n] := by { rw [mul_comm a, mul_comm b], exact h.mul_left c } protected theorem mul (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a * c ≡ b * d [ZMOD n] := (h₂.mul_left _).trans (h₁.mul_right _) protected theorem pow (m : ℕ) (h : a ≡ b [ZMOD n]) : a ^ m ≡ b ^ m [ZMOD n] := begin induction m with d hd, {refl}, rw [pow_succ, pow_succ], exact h.mul hd, end theorem of_modeq_mul_left (m : ℤ) (h : a ≡ b [ZMOD m * n]) : a ≡ b [ZMOD n] := by rw [modeq_iff_dvd] at *; exact (dvd_mul_left n m).trans h theorem of_modeq_mul_right (m : ℤ) : a ≡ b [ZMOD n * m] → a ≡ b [ZMOD n] := mul_comm m n ▸ of_modeq_mul_left _ end modeq theorem modeq_one : a ≡ b [ZMOD 1] := modeq_of_dvd (one_dvd _) lemma modeq_sub (a b : ℤ) : a ≡ b [ZMOD a - b] := (modeq_of_dvd $ dvd_refl _).symm lemma modeq_and_modeq_iff_modeq_mul {a b m n : ℤ} (hmn : m.nat_abs.coprime n.nat_abs) : a ≡ b [ZMOD m] ∧ a ≡ b [ZMOD n] ↔ (a ≡ b [ZMOD m * n]) := ⟨λ h, begin rw [modeq_iff_dvd, modeq_iff_dvd] at h, rw [modeq_iff_dvd, ← nat_abs_dvd, ← dvd_nat_abs, coe_nat_dvd, nat_abs_mul], refine hmn.mul_dvd_of_dvd_of_dvd _ _; rw [← coe_nat_dvd, nat_abs_dvd, dvd_nat_abs]; tauto end, λ h, ⟨h.of_modeq_mul_right _, h.of_modeq_mul_left _⟩⟩ lemma gcd_a_modeq (a b : ℕ) : (a : ℤ) * nat.gcd_a a b ≡ nat.gcd a b [ZMOD b] := by { rw [← add_zero ((a : ℤ) * _), nat.gcd_eq_gcd_ab], exact (dvd_mul_right _ _).zero_modeq_int.add_left _ } theorem modeq_add_fac {a b n : ℤ} (c : ℤ) (ha : a ≡ b [ZMOD n]) : a + n*c ≡ b [ZMOD n] := calc a + n*c ≡ b + n*c [ZMOD n] : ha.add_right _ ... ≡ b + 0 [ZMOD n] : (dvd_mul_right _ _).modeq_zero_int.add_left _ ... ≡ b [ZMOD n] : by rw add_zero lemma mod_coprime {a b : ℕ} (hab : nat.coprime a b) : ∃ y : ℤ, a * y ≡ 1 [ZMOD b] := ⟨ nat.gcd_a a b, have hgcd : nat.gcd a b = 1, from nat.coprime.gcd_eq_one hab, calc ↑a * nat.gcd_a a b ≡ ↑a * nat.gcd_a a b + ↑b * nat.gcd_b a b [ZMOD ↑b] : modeq.symm $ modeq_add_fac _ $ modeq.refl _ ... ≡ 1 [ZMOD ↑b] : by rw [← nat.gcd_eq_gcd_ab, hgcd]; reflexivity ⟩ lemma exists_unique_equiv (a : ℤ) {b : ℤ} (hb : 0 < b) : ∃ z : ℤ, 0 ≤ z ∧ z < b ∧ z ≡ a [ZMOD b] := ⟨ a % b, mod_nonneg _ (ne_of_gt hb), have a % b < abs b, from mod_lt _ (ne_of_gt hb), by rwa abs_of_pos hb at this, by simp [modeq] ⟩ lemma exists_unique_equiv_nat (a : ℤ) {b : ℤ} (hb : 0 < b) : ∃ z : ℕ, ↑z < b ∧ ↑z ≡ a [ZMOD b] := let ⟨z, hz1, hz2, hz3⟩ := exists_unique_equiv a hb in ⟨z.nat_abs, by split; rw [←of_nat_eq_coe, of_nat_nat_abs_eq_of_nonneg hz1]; assumption⟩ @[simp] lemma mod_mul_right_mod (a b c : ℤ) : a % (b * c) % b = a % b := (mod_modeq _ _).of_modeq_mul_right _ @[simp] lemma mod_mul_left_mod (a b c : ℤ) : a % (b * c) % c = a % c := (mod_modeq _ _).of_modeq_mul_left _ end int
e49855eff7d38fc808761af9605568dd7171147d
4727251e0cd73359b15b664c3170e5d754078599
/src/data/buffer/parser/numeral.lean
bbc5f7571d4128a92950ff9858ddc48cbdfea88b
[ "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
4,491
lean
/- Copyright (c) 2020 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import data.buffer.parser.basic /-! # Numeral parsers This file expands on the existing `nat : parser ℕ` to provide parsers into any type `α` that can be represented by a numeral, which relies on `α` having a 0, 1, and addition operation. There are also convenience parsers that ensure that the numeral parsed in is not larger than the cardinality of the type `α` , if it is known that `fintype α`. Additionally, there are convenience parsers that parse in starting from "1", which can be useful for positive ordinals; or parser from a given character or character range. ## Main definitions * `parser.numeral` : The parser which uses `nat.cast` to map the result of `parser.nat` to the desired `α` * `parser.numeral.of_fintype` : The parser which `guard`s to make sure the parsed numeral is within the cardinality of the target `fintype` type `α`. ## Implementation details When the `parser.numeral` or related parsers are invoked, the desired type is provided explicitly. In many cases, it can be inferred, so one can write, for example ```lean def get_fin : string → fin 5 := sum.elim (λ _, 0) id ∘ parser.run_string (parser.numeral.of_fintype _) ``` In the definitions of the parsers (except for `parser.numeral`), there is an explicit `nat.bin_cast` instead an explicit or implicit `nat.cast`. -/ open parser parse_result namespace parser variables (α : Type) [has_zero α] [has_one α] [has_add α] /-- Parse a string of digits as a numeral while casting it to target type `α`. -/ @[derive [mono, bounded, prog]] def numeral : parser α := nat.bin_cast <$> nat /-- Parse a string of digits as a numeral while casting it to target type `α`, which has a `[fintype α]` constraint. The parser ensures that the numeral parsed in is within the cardinality of the type `α`. -/ @[derive [mono, bounded, prog]] def numeral.of_fintype [fintype α] : parser α := do c ← nat, decorate_error (sformat!"<numeral less than {to_string (fintype.card α)}>") (guard (c < fintype.card α)), pure $ nat.bin_cast c /-- Parse a string of digits as a numeral while casting it to target type `α`. The parsing starts at "1", so `"1"` is parsed in as `nat.cast 0`. Providing `"0"` to the parser causes a failure. -/ @[derive [mono, bounded, prog]] def numeral.from_one : parser α := do c ← nat, decorate_error ("<positive numeral>") (guard (0 < c)), pure $ nat.bin_cast (c - 1) /-- Parse a string of digits as a numeral while casting it to target type `α`, which has a `[fintype α]` constraint. The parser ensures that the numeral parsed in is within the cardinality of the type `α`. The parsing starts at "1", so `"1"` is parsed in as `nat.cast 0`. Providing `"0"` to the parser causes a failure. -/ @[derive [mono, bounded, prog]] def numeral.from_one.of_fintype [fintype α] : parser α := do c ← nat, decorate_error (sformat!"<positive numeral less than or equal to {to_string (fintype.card α)}>") (guard (0 < c ∧ c ≤ fintype.card α)), pure $ nat.bin_cast (c - 1) /-- Parse a character as a numeral while casting it to target type `α`, The parser ensures that the character parsed in is within the bounds set by `fromc` and `toc`, and subtracts the value of `fromc` from the parsed in character. -/ @[derive [mono, bounded, err_static, step]] def numeral.char (fromc toc : char) : parser α := do c ← decorate_error (sformat!"<char between '{fromc.to_string}' to '{toc.to_string}' inclusively>") (sat (λ c, fromc ≤ c ∧ c ≤ toc)), pure $ nat.bin_cast (c.to_nat - fromc.to_nat) /-- Parse a character as a numeral while casting it to target type `α`, which has a `[fintype α]` constraint. The parser ensures that the character parsed in is greater or equal to `fromc` and and subtracts the value of `fromc` from the parsed in character. There is also a check that the resulting value is within the cardinality of the type `α`. -/ @[derive [mono, bounded, err_static, step]] def numeral.char.of_fintype [fintype α] (fromc : char) : parser α := do c ← decorate_error (sformat!"<char from '{fromc.to_string}' to ' { (char.of_nat (fromc.to_nat + fintype.card α - 1)).to_string}' inclusively>") (sat (λ c, fromc ≤ c ∧ c.to_nat - fintype.card α < fromc.to_nat)), pure $ nat.bin_cast (c.to_nat - fromc.to_nat) end parser
27e511b73c897f3e461eceb2e0e24e7e46133ac0
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/linear_algebra/linear_independent.lean
7d3ad051d1b7639e9dd8b34445dad7f485c99926
[ "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
56,969
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Alexander Bentkamp, Anne Baanen -/ import linear_algebra.finsupp import linear_algebra.prod import data.equiv.fin import set_theory.cardinal /-! # Linear independence This file defines linear independence in a module or vector space. It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light. We define `linear_independent R v` as `ker (finsupp.total ι M R v) = ⊥`. Here `finsupp.total` is the linear map sending a function `f : ι →₀ R` with finite support to the linear combination of vectors from `v` with these coefficients. Then we prove that several other statements are equivalent to this one, including injectivity of `finsupp.total ι M R v` and some versions with explicitly written linear combinations. ## Main definitions All definitions are given for families of vectors, i.e. `v : ι → M` where `M` is the module or vector space and `ι : Type*` is an arbitrary indexing type. * `linear_independent R v` states that the elements of the family `v` are linearly independent. * `linear_independent.repr hv x` returns the linear combination representing `x : span R (range v)` on the linearly independent vectors `v`, given `hv : linear_independent R v` (using classical choice). `linear_independent.repr hv` is provided as a linear map. ## Main statements We prove several specialized tests for linear independence of families of vectors and of sets of vectors. * `fintype.linear_independent_iff`: if `ι` is a finite type, then any function `f : ι → R` has finite support, so we can reformulate the statement using `∑ i : ι, f i • v i` instead of a sum over an auxiliary `s : finset ι`; * `linear_independent_empty_type`: a family indexed by an empty type is linearly independent; * `linear_independent_unique_iff`: if `ι` is a singleton, then `linear_independent K v` is equivalent to `v (default ι) ≠ 0`; * linear_independent_option`, `linear_independent_sum`, `linear_independent_fin_cons`, `linear_independent_fin_succ`: type-specific tests for linear independence of families of vector fields; * `linear_independent_insert`, `linear_independent_union`, `linear_independent_pair`, `linear_independent_singleton`: linear independence tests for set operations. In many cases we additionally provide dot-style operations (e.g., `linear_independent.union`) to make the linear independence tests usable as `hv.insert ha` etc. We also prove that, when working over a division ring, any family of vectors includes a linear independent subfamily spanning the same subspace. ## Implementation notes We use families instead of sets because it allows us to say that two identical vectors are linearly dependent. If you want to use sets, use the family `(λ x, x : s → M)` given a set `s : set M`. The lemmas `linear_independent.to_subtype_range` and `linear_independent.of_subtype_range` connect those two worlds. ## Tags linearly dependent, linear dependence, linearly independent, linear independence -/ noncomputable theory open function set submodule open_locale classical big_operators cardinal universes u variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*} variables {M : Type*} {M' M'' : Type*} {V : Type u} {V' : Type*} section module variables {v : ι → M} variables [semiring R] [add_comm_monoid M] [add_comm_monoid M'] [add_comm_monoid M''] variables [module R M] [module R M'] [module R M''] variables {a b : R} {x y : M} variables (R) (v) /-- `linear_independent R v` states the family of vectors `v` is linearly independent over `R`. -/ def linear_independent : Prop := (finsupp.total ι M R v).ker = ⊥ variables {R} {v} theorem linear_independent_iff : linear_independent R v ↔ ∀l, finsupp.total ι M R v l = 0 → l = 0 := by simp [linear_independent, linear_map.ker_eq_bot'] theorem linear_independent_iff' : linear_independent R v ↔ ∀ s : finset ι, ∀ g : ι → R, ∑ i in s, g i • v i = 0 → ∀ i ∈ s, g i = 0 := linear_independent_iff.trans ⟨λ hf s g hg i his, have h : _ := hf (∑ i in s, finsupp.single i (g i)) $ by simpa only [linear_map.map_sum, finsupp.total_single] using hg, calc g i = (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single i (g i)) : by rw [finsupp.lapply_apply, finsupp.single_eq_same] ... = ∑ j in s, (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single j (g j)) : eq.symm $ finset.sum_eq_single i (λ j hjs hji, by rw [finsupp.lapply_apply, finsupp.single_eq_of_ne hji]) (λ hnis, hnis.elim his) ... = (∑ j in s, finsupp.single j (g j)) i : (finsupp.lapply i : (ι →₀ R) →ₗ[R] R).map_sum.symm ... = 0 : finsupp.ext_iff.1 h i, λ hf l hl, finsupp.ext $ λ i, classical.by_contradiction $ λ hni, hni $ hf _ _ hl _ $ finsupp.mem_support_iff.2 hni⟩ theorem linear_independent_iff'' : linear_independent R v ↔ ∀ (s : finset ι) (g : ι → R) (hg : ∀ i ∉ s, g i = 0), ∑ i in s, g i • v i = 0 → ∀ i, g i = 0 := linear_independent_iff'.trans ⟨λ H s g hg hv i, if his : i ∈ s then H s g hv i his else hg i his, λ H s g hg i hi, by { convert H s (λ j, if j ∈ s then g j else 0) (λ j hj, if_neg hj) (by simp_rw [ite_smul, zero_smul, finset.sum_extend_by_zero, hg]) i, exact (if_pos hi).symm }⟩ theorem linear_dependent_iff : ¬ linear_independent R v ↔ ∃ s : finset ι, ∃ g : ι → R, (∑ i in s, g i • v i) = 0 ∧ (∃ i ∈ s, g i ≠ 0) := begin rw linear_independent_iff', simp only [exists_prop, not_forall], end theorem fintype.linear_independent_iff [fintype ι] : linear_independent R v ↔ ∀ g : ι → R, ∑ i, g i • v i = 0 → ∀ i, g i = 0 := begin refine ⟨λ H g, by simpa using linear_independent_iff'.1 H finset.univ g, λ H, linear_independent_iff''.2 $ λ s g hg hs i, H _ _ _⟩, rw ← hs, refine (finset.sum_subset (finset.subset_univ _) (λ i _ hi, _)).symm, rw [hg i hi, zero_smul] end /-- A finite family of vectors `v i` is linear independent iff the linear map that sends `c : ι → R` to `∑ i, c i • v i` has the trivial kernel. -/ theorem fintype.linear_independent_iff' [fintype ι] : linear_independent R v ↔ (linear_map.lsum R (λ i : ι, R) ℕ (λ i, linear_map.id.smul_right (v i))).ker = ⊥ := by simp [fintype.linear_independent_iff, linear_map.ker_eq_bot', funext_iff] lemma linear_independent_empty_type [is_empty ι] : linear_independent R v := linear_independent_iff.mpr $ λ v hv, subsingleton.elim v 0 lemma linear_independent.ne_zero [nontrivial R] (i : ι) (hv : linear_independent R v) : v i ≠ 0 := λ h, @zero_ne_one R _ _ $ eq.symm begin suffices : (finsupp.single i 1 : ι →₀ R) i = 0, {simpa}, rw linear_independent_iff.1 hv (finsupp.single i 1), { simp }, { simp [h] } end /-- A subfamily of a linearly independent family (i.e., a composition with an injective map) is a linearly independent family. -/ lemma linear_independent.comp (h : linear_independent R v) (f : ι' → ι) (hf : injective f) : linear_independent R (v ∘ f) := begin rw [linear_independent_iff, finsupp.total_comp], intros l hl, have h_map_domain : ∀ x, (finsupp.map_domain f l) (f x) = 0, by rw linear_independent_iff.1 h (finsupp.map_domain f l) hl; simp, ext x, convert h_map_domain x, rw [finsupp.map_domain_apply hf] end lemma linear_independent.coe_range (i : linear_independent R v) : linear_independent R (coe : range v → M) := by simpa using i.comp _ (range_splitting_injective v) /-- If `v` is a linearly independent family of vectors and the kernel of a linear map `f` is disjoint with the submodule spanned by the vectors of `v`, then `f ∘ v` is a linearly independent family of vectors. See also `linear_independent.map'` for a special case assuming `ker f = ⊥`. -/ lemma linear_independent.map (hv : linear_independent R v) {f : M →ₗ[R] M'} (hf_inj : disjoint (span R (range v)) f.ker) : linear_independent R (f ∘ v) := begin rw [disjoint, ← set.image_univ, finsupp.span_image_eq_map_total, map_inf_eq_map_inf_comap, map_le_iff_le_comap, comap_bot, finsupp.supported_univ, top_inf_eq] at hf_inj, unfold linear_independent at hv ⊢, rw [hv, le_bot_iff] at hf_inj, haveI : inhabited M := ⟨0⟩, rw [finsupp.total_comp, @finsupp.lmap_domain_total _ _ R _ _ _ _ _ _ _ _ _ _ f, linear_map.ker_comp, hf_inj], exact λ _, rfl, end /-- An injective linear map sends linearly independent families of vectors to linearly independent families of vectors. See also `linear_independent.map` for a more general statement. -/ lemma linear_independent.map' (hv : linear_independent R v) (f : M →ₗ[R] M') (hf_inj : f.ker = ⊥) : linear_independent R (f ∘ v) := hv.map $ by simp [hf_inj] /-- If the image of a family of vectors under a linear map is linearly independent, then so is the original family. -/ lemma linear_independent.of_comp (f : M →ₗ[R] M') (hfv : linear_independent R (f ∘ v)) : linear_independent R v := linear_independent_iff'.2 $ λ s g hg i his, have ∑ (i : ι) in s, g i • f (v i) = 0, by simp_rw [← f.map_smul, ← f.map_sum, hg, f.map_zero], linear_independent_iff'.1 hfv s g this i his /-- If `f` is an injective linear map, then the family `f ∘ v` is linearly independent if and only if the family `v` is linearly independent. -/ protected lemma linear_map.linear_independent_iff (f : M →ₗ[R] M') (hf_inj : f.ker = ⊥) : linear_independent R (f ∘ v) ↔ linear_independent R v := ⟨λ h, h.of_comp f, λ h, h.map $ by simp only [hf_inj, disjoint_bot_right]⟩ @[nontriviality] lemma linear_independent_of_subsingleton [subsingleton R] : linear_independent R v := linear_independent_iff.2 (λ l hl, subsingleton.elim _ _) theorem linear_independent_equiv (e : ι ≃ ι') {f : ι' → M} : linear_independent R (f ∘ e) ↔ linear_independent R f := ⟨λ h, function.comp.right_id f ▸ e.self_comp_symm ▸ h.comp _ e.symm.injective, λ h, h.comp _ e.injective⟩ theorem linear_independent_equiv' (e : ι ≃ ι') {f : ι' → M} {g : ι → M} (h : f ∘ e = g) : linear_independent R g ↔ linear_independent R f := h ▸ linear_independent_equiv e theorem linear_independent_subtype_range {ι} {f : ι → M} (hf : injective f) : linear_independent R (coe : range f → M) ↔ linear_independent R f := iff.symm $ linear_independent_equiv' (equiv.of_injective f hf) rfl alias linear_independent_subtype_range ↔ linear_independent.of_subtype_range _ theorem linear_independent_image {ι} {s : set ι} {f : ι → M} (hf : set.inj_on f s) : linear_independent R (λ x : s, f x) ↔ linear_independent R (λ x : f '' s, (x : M)) := linear_independent_equiv' (equiv.set.image_of_inj_on _ _ hf) rfl lemma linear_independent_span (hs : linear_independent R v) : @linear_independent ι R (span R (range v)) (λ i : ι, ⟨v i, subset_span (mem_range_self i)⟩) _ _ _ := linear_independent.of_comp (span R (range v)).subtype hs /-- See `linear_independent.fin_cons` for a family of elements in a vector space. -/ lemma linear_independent.fin_cons' {m : ℕ} (x : M) (v : fin m → M) (hli : linear_independent R v) (x_ortho : (∀ (c : R) (y : submodule.span R (set.range v)), c • x + y = (0 : M) → c = 0)) : linear_independent R (fin.cons x v : fin m.succ → M) := begin rw fintype.linear_independent_iff at hli ⊢, rintros g total_eq j, have zero_not_mem : (0 : fin m.succ) ∉ finset.univ.image (fin.succ : fin m → fin m.succ), { rw finset.mem_image, rintro ⟨x, hx, succ_eq⟩, exact fin.succ_ne_zero _ succ_eq }, simp only [submodule.coe_mk, fin.univ_succ, finset.sum_insert zero_not_mem, fin.cons_zero, fin.cons_succ, forall_true_iff, imp_self, fin.succ_inj, finset.sum_image] at total_eq, have : g 0 = 0, { refine x_ortho (g 0) ⟨∑ (i : fin m), g i.succ • v i, _⟩ total_eq, exact sum_mem _ (λ i _, smul_mem _ _ (subset_span ⟨i, rfl⟩)) }, refine fin.cases this (λ j, _) j, apply hli (λ i, g i.succ), simpa only [this, zero_smul, zero_add] using total_eq end /-- A set of linearly independent vectors in a module `M` over a semiring `K` is also linearly independent over a subring `R` of `K`. The implementation uses minimal assumptions about the relationship between `R`, `K` and `M`. The version where `K` is an `R`-algebra is `linear_independent.restrict_scalars_algebras`. -/ lemma linear_independent.restrict_scalars [semiring K] [smul_with_zero R K] [module K M] [is_scalar_tower R K M] (hinj : function.injective (λ r : R, r • (1 : K))) (li : linear_independent K v) : linear_independent R v := begin refine linear_independent_iff'.mpr (λ s g hg i hi, hinj (eq.trans _ (zero_smul _ _).symm)), refine (linear_independent_iff'.mp li : _) _ _ _ i hi, simp_rw [smul_assoc, one_smul], exact hg, end /-- Every finite subset of a linearly independent set is linearly independent. -/ lemma linear_independent_finset_map_embedding_subtype (s : set M) (li : linear_independent R (coe : s → M)) (t : finset s) : linear_independent R (coe : (finset.map (embedding.subtype s) t) → M) := begin let f : t.map (embedding.subtype s) → s := λ x, ⟨x.1, begin obtain ⟨x, h⟩ := x, rw [finset.mem_map] at h, obtain ⟨a, ha, rfl⟩ := h, simp only [subtype.coe_prop, embedding.coe_subtype], end⟩, convert linear_independent.comp li f _, rintros ⟨x, hx⟩ ⟨y, hy⟩, rw [finset.mem_map] at hx hy, obtain ⟨a, ha, rfl⟩ := hx, obtain ⟨b, hb, rfl⟩ := hy, simp only [imp_self, subtype.mk_eq_mk], end /-- If every finite set of linearly independent vectors has cardinality at most `n`, then the same is true for arbitrary sets of linearly independent vectors. -/ lemma linear_independent_bounded_of_finset_linear_independent_bounded {n : ℕ} (H : ∀ s : finset M, linear_independent R (λ i : s, (i : M)) → s.card ≤ n) : ∀ s : set M, linear_independent R (coe : s → M) → #s ≤ n := begin intros s li, apply cardinal.card_le_of, intro t, rw ← finset.card_map (embedding.subtype s), apply H, apply linear_independent_finset_map_embedding_subtype _ li, end section subtype /-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/ theorem linear_independent_comp_subtype {s : set ι} : linear_independent R (v ∘ coe : s → M) ↔ ∀ l ∈ (finsupp.supported R R s), (finsupp.total ι M R v) l = 0 → l = 0 := begin simp only [linear_independent_iff, (∘), finsupp.mem_supported, finsupp.total_apply, set.subset_def, finset.mem_coe], split, { intros h l hl₁ hl₂, have := h (l.subtype_domain s) ((finsupp.sum_subtype_domain_index hl₁).trans hl₂), exact (finsupp.subtype_domain_eq_zero_iff hl₁).1 this }, { intros h l hl, refine finsupp.emb_domain_eq_zero.1 (h (l.emb_domain $ function.embedding.subtype s) _ _), { suffices : ∀ i hi, ¬l ⟨i, hi⟩ = 0 → i ∈ s, by simpa, intros, assumption }, { rwa [finsupp.emb_domain_eq_map_domain, finsupp.sum_map_domain_index], exacts [λ _, zero_smul _ _, λ _ _ _, add_smul _ _ _] } } end lemma linear_dependent_comp_subtype' {s : set ι} : ¬ linear_independent R (v ∘ coe : s → M) ↔ ∃ f : ι →₀ R, f ∈ finsupp.supported R R s ∧ finsupp.total ι M R v f = 0 ∧ f ≠ 0 := by simp [linear_independent_comp_subtype] /-- A version of `linear_dependent_comp_subtype'` with `finsupp.total` unfolded. -/ lemma linear_dependent_comp_subtype {s : set ι} : ¬ linear_independent R (v ∘ coe : s → M) ↔ ∃ f : ι →₀ R, f ∈ finsupp.supported R R s ∧ ∑ i in f.support, f i • v i = 0 ∧ f ≠ 0 := linear_dependent_comp_subtype' theorem linear_independent_subtype {s : set M} : linear_independent R (λ x, x : s → M) ↔ ∀ l ∈ (finsupp.supported R R s), (finsupp.total M M R id) l = 0 → l = 0 := by apply @linear_independent_comp_subtype _ _ _ id theorem linear_independent_comp_subtype_disjoint {s : set ι} : linear_independent R (v ∘ coe : s → M) ↔ disjoint (finsupp.supported R R s) (finsupp.total ι M R v).ker := by rw [linear_independent_comp_subtype, linear_map.disjoint_ker] theorem linear_independent_subtype_disjoint {s : set M} : linear_independent R (λ x, x : s → M) ↔ disjoint (finsupp.supported R R s) (finsupp.total M M R id).ker := by apply @linear_independent_comp_subtype_disjoint _ _ _ id theorem linear_independent_iff_total_on {s : set M} : linear_independent R (λ x, x : s → M) ↔ (finsupp.total_on M M R id s).ker = ⊥ := by rw [finsupp.total_on, linear_map.ker, linear_map.comap_cod_restrict, map_bot, comap_bot, linear_map.ker_comp, linear_independent_subtype_disjoint, disjoint, ← map_comap_subtype, map_le_iff_le_comap, comap_bot, ker_subtype, le_bot_iff] lemma linear_independent.restrict_of_comp_subtype {s : set ι} (hs : linear_independent R (v ∘ coe : s → M)) : linear_independent R (s.restrict v) := hs variables (R M) lemma linear_independent_empty : linear_independent R (λ x, x : (∅ : set M) → M) := by simp [linear_independent_subtype_disjoint] variables {R M} lemma linear_independent.mono {t s : set M} (h : t ⊆ s) : linear_independent R (λ x, x : s → M) → linear_independent R (λ x, x : t → M) := begin simp only [linear_independent_subtype_disjoint], exact (disjoint.mono_left (finsupp.supported_mono h)) end lemma linear_independent_of_finite (s : set M) (H : ∀ t ⊆ s, finite t → linear_independent R (λ x, x : t → M)) : linear_independent R (λ x, x : s → M) := linear_independent_subtype.2 $ λ l hl, linear_independent_subtype.1 (H _ hl (finset.finite_to_set _)) l (subset.refl _) lemma linear_independent_Union_of_directed {η : Type*} {s : η → set M} (hs : directed (⊆) s) (h : ∀ i, linear_independent R (λ x, x : s i → M)) : linear_independent R (λ x, x : (⋃ i, s i) → M) := begin by_cases hη : nonempty η, { resetI, refine linear_independent_of_finite (⋃ i, s i) (λ t ht ft, _), rcases finite_subset_Union ft ht with ⟨I, fi, hI⟩, rcases hs.finset_le fi.to_finset with ⟨i, hi⟩, exact (h i).mono (subset.trans hI $ bUnion_subset $ λ j hj, hi j (fi.mem_to_finset.2 hj)) }, { refine (linear_independent_empty _ _).mono _, rintro _ ⟨_, ⟨i, _⟩, _⟩, exact hη ⟨i⟩ } end lemma linear_independent_sUnion_of_directed {s : set (set M)} (hs : directed_on (⊆) s) (h : ∀ a ∈ s, linear_independent R (λ x, x : (a : set M) → M)) : linear_independent R (λ x, x : (⋃₀ s) → M) := by rw sUnion_eq_Union; exact linear_independent_Union_of_directed hs.directed_coe (by simpa using h) lemma linear_independent_bUnion_of_directed {η} {s : set η} {t : η → set M} (hs : directed_on (t ⁻¹'o (⊆)) s) (h : ∀a∈s, linear_independent R (λ x, x : t a → M)) : linear_independent R (λ x, x : (⋃a∈s, t a) → M) := by rw bUnion_eq_Union; exact linear_independent_Union_of_directed (directed_comp.2 $ hs.directed_coe) (by simpa using h) end subtype end module /-! ### Properties which require `ring R` -/ section module variables {v : ι → M} variables [ring R] [add_comm_group M] [add_comm_group M'] [add_comm_group M''] variables [module R M] [module R M'] [module R M''] variables {a b : R} {x y : M} theorem linear_independent_iff_injective_total : linear_independent R v ↔ function.injective (finsupp.total ι M R v) := linear_independent_iff.trans (finsupp.total ι M R v).to_add_monoid_hom.injective_iff.symm alias linear_independent_iff_injective_total ↔ linear_independent.injective_total _ lemma linear_independent.injective [nontrivial R] (hv : linear_independent R v) : injective v := begin intros i j hij, let l : ι →₀ R := finsupp.single i (1 : R) - finsupp.single j 1, have h_total : finsupp.total ι M R v l = 0, { simp_rw [linear_map.map_sub, finsupp.total_apply], simp [hij] }, have h_single_eq : finsupp.single i (1 : R) = finsupp.single j 1, { rw linear_independent_iff at hv, simp [eq_add_of_sub_eq' (hv l h_total)] }, simpa [finsupp.single_eq_single_iff] using h_single_eq end theorem linear_independent.to_subtype_range {ι} {f : ι → M} (hf : linear_independent R f) : linear_independent R (coe : range f → M) := begin nontriviality R, exact (linear_independent_subtype_range hf.injective).2 hf end theorem linear_independent.to_subtype_range' {ι} {f : ι → M} (hf : linear_independent R f) {t} (ht : range f = t) : linear_independent R (coe : t → M) := ht ▸ hf.to_subtype_range theorem linear_independent.image_of_comp {ι ι'} (s : set ι) (f : ι → ι') (g : ι' → M) (hs : linear_independent R (λ x : s, g (f x))) : linear_independent R (λ x : f '' s, g x) := begin nontriviality R, have : inj_on f s, from inj_on_iff_injective.2 hs.injective.of_comp, exact (linear_independent_equiv' (equiv.set.image_of_inj_on f s this) rfl).1 hs end theorem linear_independent.image {ι} {s : set ι} {f : ι → M} (hs : linear_independent R (λ x : s, f x)) : linear_independent R (λ x : f '' s, (x : M)) := by convert linear_independent.image_of_comp s f id hs lemma linear_independent.group_smul {G : Type*} [hG : group G] [distrib_mul_action G R] [distrib_mul_action G M] [is_scalar_tower G R M] [smul_comm_class G R M] {v : ι → M} (hv : linear_independent R v) (w : ι → G) : linear_independent R (w • v) := begin rw linear_independent_iff'' at hv ⊢, intros s g hgs hsum i, refine (smul_eq_zero_iff_eq (w i)).1 _, refine hv s (λ i, w i • g i) (λ i hi, _) _ i, { dsimp only, exact (hgs i hi).symm ▸ smul_zero _ }, { rw [← hsum, finset.sum_congr rfl _], intros, erw [pi.smul_apply, smul_assoc, smul_comm] }, end -- This lemma cannot be proved with `linear_independent.group_smul` since the action of -- `units R` on `R` is not commutative. lemma linear_independent.units_smul {v : ι → M} (hv : linear_independent R v) (w : ι → units R) : linear_independent R (w • v) := begin rw linear_independent_iff'' at hv ⊢, intros s g hgs hsum i, rw ← (w i).mul_left_eq_zero, refine hv s (λ i, g i • w i) (λ i hi, _) _ i, { dsimp only, exact (hgs i hi).symm ▸ zero_smul _ _ }, { rw [← hsum, finset.sum_congr rfl _], intros, erw [pi.smul_apply, smul_assoc], refl } end section maximal universes v w /-- A linearly independent family is maximal if there is no strictly larger linearly independent family. -/ @[nolint unused_arguments] def linear_independent.maximal {ι : Type w} {R : Type u} [semiring R] {M : Type v} [add_comm_monoid M] [module R M] {v : ι → M} (i : linear_independent R v) : Prop := ∀ (s : set M) (i' : linear_independent R (coe : s → M)) (h : range v ≤ s), range v = s /-- An alternative characterization of a maximal linearly independent family, quantifying over types (in the same universe as `M`) into which the indexing family injects. -/ lemma linear_independent.maximal_iff {ι : Type w} {R : Type u} [ring R] [nontrivial R] {M : Type v} [add_comm_group M] [module R M] {v : ι → M} (i : linear_independent R v) : i.maximal ↔ ∀ (κ : Type v) (w : κ → M) (i' : linear_independent R w) (j : ι → κ) (h : w ∘ j = v), surjective j := begin fsplit, { rintros p κ w i' j rfl, specialize p (range w) i'.coe_range (range_comp_subset_range _ _), rw [range_comp, ←@image_univ _ _ w] at p, exact range_iff_surjective.mp (image_injective.mpr i'.injective p), }, { intros p w i' h, specialize p w (coe : w → M) i' (λ i, ⟨v i, range_subset_iff.mp h i⟩) (by { ext, simp, }), have q := congr_arg (λ s, (coe : w → M) '' s) p.range_eq, dsimp at q, rw [←image_univ, image_image] at q, simpa using q, }, end end maximal /-- Linear independent families are injective, even if you multiply either side. -/ lemma linear_independent.eq_of_smul_apply_eq_smul_apply {M : Type*} [add_comm_group M] [module R M] {v : ι → M} (li : linear_independent R v) (c d : R) (i j : ι) (hc : c ≠ 0) (h : c • v i = d • v j) : i = j := begin let l : ι →₀ R := finsupp.single i c - finsupp.single j d, have h_total : finsupp.total ι M R v l = 0, { simp_rw [linear_map.map_sub, finsupp.total_apply], simp [h] }, have h_single_eq : finsupp.single i c = finsupp.single j d, { rw linear_independent_iff at li, simp [eq_add_of_sub_eq' (li l h_total)] }, rcases (finsupp.single_eq_single_iff _ _ _ _).mp h_single_eq with ⟨this, _⟩ | ⟨hc, _⟩, { exact this }, { contradiction }, end section subtype /-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/ lemma linear_independent.disjoint_span_image (hv : linear_independent R v) {s t : set ι} (hs : disjoint s t) : disjoint (submodule.span R $ v '' s) (submodule.span R $ v '' t) := begin simp only [disjoint_def, finsupp.mem_span_image_iff_total], rintros _ ⟨l₁, hl₁, rfl⟩ ⟨l₂, hl₂, H⟩, rw [hv.injective_total.eq_iff] at H, subst l₂, have : l₁ = 0 := finsupp.disjoint_supported_supported hs (submodule.mem_inf.2 ⟨hl₁, hl₂⟩), simp [this] end lemma linear_independent.not_mem_span_image [nontrivial R] (hv : linear_independent R v) {s : set ι} {x : ι} (h : x ∉ s) : v x ∉ submodule.span R (v '' s) := begin have h' : v x ∈ submodule.span R (v '' {x}), { rw set.image_singleton, exact mem_span_singleton_self (v x), }, intro w, apply linear_independent.ne_zero x hv, refine disjoint_def.1 (hv.disjoint_span_image _) (v x) h' w, simpa using h, end lemma linear_independent.total_ne_of_not_mem_support [nontrivial R] (hv : linear_independent R v) {x : ι} (f : ι →₀ R) (h : x ∉ f.support) : finsupp.total ι M R v f ≠ v x := begin replace h : x ∉ (f.support : set ι) := h, have p := hv.not_mem_span_image h, intro w, rw ←w at p, rw finsupp.span_image_eq_map_total at p, simp only [not_exists, not_and, mem_map] at p, exact p f (f.mem_supported_support R) rfl, end lemma linear_independent_sum {v : ι ⊕ ι' → M} : linear_independent R v ↔ linear_independent R (v ∘ sum.inl) ∧ linear_independent R (v ∘ sum.inr) ∧ disjoint (submodule.span R (range (v ∘ sum.inl))) (submodule.span R (range (v ∘ sum.inr))) := begin rw [range_comp v, range_comp v], refine ⟨λ h, ⟨h.comp _ sum.inl_injective, h.comp _ sum.inr_injective, h.disjoint_span_image is_compl_range_inl_range_inr.1⟩, _⟩, rintro ⟨hl, hr, hlr⟩, rw [linear_independent_iff'] at *, intros s g hg i hi, have : ∑ i in s.preimage sum.inl (sum.inl_injective.inj_on _), (λ x, g x • v x) (sum.inl i) + ∑ i in s.preimage sum.inr (sum.inr_injective.inj_on _), (λ x, g x • v x) (sum.inr i) = 0, { rw [finset.sum_preimage', finset.sum_preimage', ← finset.sum_union, ← finset.filter_or], { simpa only [← mem_union, range_inl_union_range_inr, mem_univ, finset.filter_true] }, { exact finset.disjoint_filter.2 (λ x hx, disjoint_left.1 is_compl_range_inl_range_inr.1) } }, { rw ← eq_neg_iff_add_eq_zero at this, rw [disjoint_def'] at hlr, have A := hlr _ (sum_mem _ $ λ i hi, _) _ (neg_mem _ $ sum_mem _ $ λ i hi, _) this, { cases i with i i, { exact hl _ _ A i (finset.mem_preimage.2 hi) }, { rw [this, neg_eq_zero] at A, exact hr _ _ A i (finset.mem_preimage.2 hi) } }, { exact smul_mem _ _ (subset_span ⟨sum.inl i, mem_range_self _, rfl⟩) }, { exact smul_mem _ _ (subset_span ⟨sum.inr i, mem_range_self _, rfl⟩) } } end lemma linear_independent.sum_type {v' : ι' → M} (hv : linear_independent R v) (hv' : linear_independent R v') (h : disjoint (submodule.span R (range v)) (submodule.span R (range v'))) : linear_independent R (sum.elim v v') := linear_independent_sum.2 ⟨hv, hv', h⟩ lemma linear_independent.union {s t : set M} (hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M)) (hst : disjoint (span R s) (span R t)) : linear_independent R (λ x, x : (s ∪ t) → M) := (hs.sum_type ht $ by simpa).to_subtype_range' $ by simp lemma linear_independent_Union_finite_subtype {ι : Type*} {f : ι → set M} (hl : ∀i, linear_independent R (λ x, x : f i → M)) (hd : ∀i, ∀t:set ι, finite t → i ∉ t → disjoint (span R (f i)) (⨆i∈t, span R (f i))) : linear_independent R (λ x, x : (⋃i, f i) → M) := begin rw [Union_eq_Union_finset f], apply linear_independent_Union_of_directed, { apply directed_of_sup, exact (λ t₁ t₂ ht, Union_subset_Union $ λ i, Union_subset_Union_const $ λ h, ht h) }, assume t, induction t using finset.induction_on with i s his ih, { refine (linear_independent_empty _ _).mono _, simp }, { rw [finset.set_bUnion_insert], refine (hl _).union ih _, refine (hd i s s.finite_to_set his).mono_right _, simp only [(span_Union _).symm], refine span_mono (@supr_le_supr2 (set M) _ _ _ _ _ _), exact λ i, ⟨i, le_rfl⟩ } end lemma linear_independent_Union_finite {η : Type*} {ιs : η → Type*} {f : Π j : η, ιs j → M} (hindep : ∀j, linear_independent R (f j)) (hd : ∀i, ∀t:set η, finite t → i ∉ t → disjoint (span R (range (f i))) (⨆i∈t, span R (range (f i)))) : linear_independent R (λ ji : Σ j, ιs j, f ji.1 ji.2) := begin nontriviality R, apply linear_independent.of_subtype_range, { rintros ⟨x₁, x₂⟩ ⟨y₁, y₂⟩ hxy, by_cases h_cases : x₁ = y₁, subst h_cases, { apply sigma.eq, rw linear_independent.injective (hindep _) hxy, refl }, { have h0 : f x₁ x₂ = 0, { apply disjoint_def.1 (hd x₁ {y₁} (finite_singleton y₁) (λ h, h_cases (eq_of_mem_singleton h))) (f x₁ x₂) (subset_span (mem_range_self _)), rw supr_singleton, simp only at hxy, rw hxy, exact (subset_span (mem_range_self y₂)) }, exact false.elim ((hindep x₁).ne_zero _ h0) } }, rw range_sigma_eq_Union_range, apply linear_independent_Union_finite_subtype (λ j, (hindep j).to_subtype_range) hd, end end subtype section repr variables (hv : linear_independent R v) /-- Canonical isomorphism between linear combinations and the span of linearly independent vectors. -/ @[simps] def linear_independent.total_equiv (hv : linear_independent R v) : (ι →₀ R) ≃ₗ[R] span R (range v) := begin apply linear_equiv.of_bijective (linear_map.cod_restrict (span R (range v)) (finsupp.total ι M R v) _), { rw [← linear_map.ker_eq_bot, linear_map.ker_cod_restrict], apply hv }, { rw [← linear_map.range_eq_top, linear_map.range_eq_map, linear_map.map_cod_restrict, ← linear_map.range_le_iff_comap, range_subtype, map_top], rw finsupp.range_total, apply le_refl (span R (range v)) }, { intro l, rw ← finsupp.range_total, rw linear_map.mem_range, apply mem_range_self l } end /-- Linear combination representing a vector in the span of linearly independent vectors. Given a family of linearly independent vectors, we can represent any vector in their span as a linear combination of these vectors. These are provided by this linear map. It is simply one direction of `linear_independent.total_equiv`. -/ def linear_independent.repr (hv : linear_independent R v) : span R (range v) →ₗ[R] ι →₀ R := hv.total_equiv.symm @[simp] lemma linear_independent.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x := subtype.ext_iff.1 (linear_equiv.apply_symm_apply hv.total_equiv x) lemma linear_independent.total_comp_repr : (finsupp.total ι M R v).comp hv.repr = submodule.subtype _ := linear_map.ext $ hv.total_repr lemma linear_independent.repr_ker : hv.repr.ker = ⊥ := by rw [linear_independent.repr, linear_equiv.ker] lemma linear_independent.repr_range : hv.repr.range = ⊤ := by rw [linear_independent.repr, linear_equiv.range] lemma linear_independent.repr_eq {l : ι →₀ R} {x} (eq : finsupp.total ι M R v l = ↑x) : hv.repr x = l := begin have : ↑((linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l) = finsupp.total ι M R v l := rfl, have : (linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l = x, { rw eq at this, exact subtype.ext_iff.2 this }, rw ←linear_equiv.symm_apply_apply hv.total_equiv l, rw ←this, refl, end lemma linear_independent.repr_eq_single (i) (x) (hx : ↑x = v i) : hv.repr x = finsupp.single i 1 := begin apply hv.repr_eq, simp [finsupp.total_single, hx] end lemma linear_independent.span_repr_eq [nontrivial R] (x) : span.repr R (set.range v) x = (hv.repr x).equiv_map_domain (equiv.of_injective _ hv.injective) := begin have p : (span.repr R (set.range v) x).equiv_map_domain (equiv.of_injective _ hv.injective).symm = hv.repr x, { apply (linear_independent.total_equiv hv).injective, ext, simp, }, ext ⟨_, ⟨i, rfl⟩⟩, simp [←p], end -- TODO: why is this so slow? lemma linear_independent_iff_not_smul_mem_span : linear_independent R v ↔ (∀ (i : ι) (a : R), a • (v i) ∈ span R (v '' (univ \ {i})) → a = 0) := ⟨ λ hv i a ha, begin rw [finsupp.span_image_eq_map_total, mem_map] at ha, rcases ha with ⟨l, hl, e⟩, rw sub_eq_zero.1 (linear_independent_iff.1 hv (l - finsupp.single i a) (by simp [e])) at hl, by_contra hn, exact (not_mem_of_mem_diff (hl $ by simp [hn])) (mem_singleton _), end, λ H, linear_independent_iff.2 $ λ l hl, begin ext i, simp only [finsupp.zero_apply], by_contra hn, refine hn (H i _ _), refine (finsupp.mem_span_image_iff_total _).2 ⟨finsupp.single i (l i) - l, _, _⟩, { rw finsupp.mem_supported', intros j hj, have hij : j = i := not_not.1 (λ hij : j ≠ i, hj ((mem_diff _).2 ⟨mem_univ _, λ h, hij (eq_of_mem_singleton h)⟩)), simp [hij] }, { simp [hl] } end⟩ variable (R) lemma exists_maximal_independent' (s : ι → M) : ∃ I : set ι, linear_independent R (λ x : I, s x) ∧ ∀ J : set ι, I ⊆ J → linear_independent R (λ x : J, s x) → I = J := begin let indep : set ι → Prop := λ I, linear_independent R (s ∘ coe : I → M), let X := { I : set ι // indep I }, let r : X → X → Prop := λ I J, I.1 ⊆ J.1, have key : ∀ c : set X, zorn.chain r c → indep (⋃ (I : X) (H : I ∈ c), I), { intros c hc, dsimp [indep], rw [linear_independent_comp_subtype], intros f hsupport hsum, rcases eq_empty_or_nonempty c with rfl | ⟨a, hac⟩, { simpa using hsupport }, haveI : is_refl X r := ⟨λ _, set.subset.refl _⟩, obtain ⟨I, I_mem, hI⟩ : ∃ I ∈ c, (f.support : set ι) ⊆ I := finset.exists_mem_subset_of_subset_bUnion_of_directed_on hac hc.directed_on hsupport, exact linear_independent_comp_subtype.mp I.2 f hI hsum }, have trans : transitive r := λ I J K, set.subset.trans, obtain ⟨⟨I, hli : indep I⟩, hmax : ∀ a, r ⟨I, hli⟩ a → r a ⟨I, hli⟩⟩ := @zorn.exists_maximal_of_chains_bounded _ r (λ c hc, ⟨⟨⋃ I ∈ c, (I : set ι), key c hc⟩, λ I, set.subset_bUnion_of_mem⟩) trans, exact ⟨I, hli, λ J hsub hli, set.subset.antisymm hsub (hmax ⟨J, hli⟩ hsub)⟩, end lemma exists_maximal_independent (s : ι → M) : ∃ I : set ι, linear_independent R (λ x : I, s x) ∧ ∀ i ∉ I, ∃ a : R, a ≠ 0 ∧ a • s i ∈ span R (s '' I) := begin classical, rcases exists_maximal_independent' R s with ⟨I, hIlinind, hImaximal⟩, use [I, hIlinind], intros i hi, specialize hImaximal (I ∪ {i}) (by simp), set J := I ∪ {i} with hJ, have memJ : ∀ {x}, x ∈ J ↔ x = i ∨ x ∈ I, by simp [hJ], have hiJ : i ∈ J := by simp, have h := mt hImaximal _, swap, { intro h2, rw h2 at hi, exact absurd hiJ hi }, obtain ⟨f, supp_f, sum_f, f_ne⟩ := linear_dependent_comp_subtype.mp h, have hfi : f i ≠ 0, { contrapose hIlinind, refine linear_dependent_comp_subtype.mpr ⟨f, _, sum_f, f_ne⟩, simp only [finsupp.mem_supported, hJ] at ⊢ supp_f, rintro x hx, refine (memJ.mp (supp_f hx)).resolve_left _, rintro rfl, exact hIlinind (finsupp.mem_support_iff.mp hx) }, use [f i, hfi], have hfi' : i ∈ f.support := finsupp.mem_support_iff.mpr hfi, rw [← finset.insert_erase hfi', finset.sum_insert (finset.not_mem_erase _ _), add_eq_zero_iff_eq_neg] at sum_f, rw sum_f, refine neg_mem _ (sum_mem _ (λ c hc, smul_mem _ _ (subset_span ⟨c, _, rfl⟩))), exact (memJ.mp (supp_f (finset.erase_subset _ _ hc))).resolve_left (finset.ne_of_mem_erase hc), end end repr lemma surjective_of_linear_independent_of_span [nontrivial R] (hv : linear_independent R v) (f : ι' ↪ ι) (hss : range v ⊆ span R (range (v ∘ f))) : surjective f := begin intros i, let repr : (span R (range (v ∘ f)) : Type*) → ι' →₀ R := (hv.comp f f.injective).repr, let l := (repr ⟨v i, hss (mem_range_self i)⟩).map_domain f, have h_total_l : finsupp.total ι M R v l = v i, { dsimp only [l], rw finsupp.total_map_domain, rw (hv.comp f f.injective).total_repr, { refl }, { exact f.injective } }, have h_total_eq : (finsupp.total ι M R v) l = (finsupp.total ι M R v) (finsupp.single i 1), by rw [h_total_l, finsupp.total_single, one_smul], have l_eq : l = _ := linear_map.ker_eq_bot.1 hv h_total_eq, dsimp only [l] at l_eq, rw ←finsupp.emb_domain_eq_map_domain at l_eq, rcases finsupp.single_of_emb_domain_single (repr ⟨v i, _⟩) f i (1 : R) zero_ne_one.symm l_eq with ⟨i', hi'⟩, use i', exact hi'.2 end lemma eq_of_linear_independent_of_span_subtype [nontrivial R] {s t : set M} (hs : linear_independent R (λ x, x : s → M)) (h : t ⊆ s) (hst : s ⊆ span R t) : s = t := begin let f : t ↪ s := ⟨λ x, ⟨x.1, h x.2⟩, λ a b hab, subtype.coe_injective (subtype.mk.inj hab)⟩, have h_surj : surjective f, { apply surjective_of_linear_independent_of_span hs f _, convert hst; simp [f, comp], }, show s = t, { apply subset.antisymm _ h, intros x hx, rcases h_surj ⟨x, hx⟩ with ⟨y, hy⟩, convert y.mem, rw ← subtype.mk.inj hy, refl } end open linear_map lemma linear_independent.image_subtype {s : set M} {f : M →ₗ[R] M'} (hs : linear_independent R (λ x, x : s → M)) (hf_inj : disjoint (span R s) f.ker) : linear_independent R (λ x, x : f '' s → M') := begin rw [← @subtype.range_coe _ s] at hf_inj, refine (hs.map hf_inj).to_subtype_range' _, simp [set.range_comp f] end lemma linear_independent.inl_union_inr {s : set M} {t : set M'} (hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M')) : linear_independent R (λ x, x : inl R M M' '' s ∪ inr R M M' '' t → M × M') := begin refine (hs.image_subtype _).union (ht.image_subtype _) _; [simp, simp, skip], simp only [span_image], simp [disjoint_iff, prod_inf_prod] end lemma linear_independent_inl_union_inr' {v : ι → M} {v' : ι' → M'} (hv : linear_independent R v) (hv' : linear_independent R v') : linear_independent R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) := (hv.map' (inl R M M') ker_inl).sum_type (hv'.map' (inr R M M') ker_inr) $ begin refine is_compl_range_inl_inr.disjoint.mono _ _; simp only [span_le, range_coe, range_comp_subset_range], end /-- Dedekind's linear independence of characters -/ -- See, for example, Keith Conrad's note -- <https://kconrad.math.uconn.edu/blurbs/galoistheory/linearchar.pdf> theorem linear_independent_monoid_hom (G : Type*) [monoid G] (L : Type*) [comm_ring L] [no_zero_divisors L] : @linear_independent _ L (G → L) (λ f, f : (G →* L) → (G → L)) _ _ _ := by letI := classical.dec_eq (G →* L); letI : mul_action L L := distrib_mul_action.to_mul_action; -- We prove linear independence by showing that only the trivial linear combination vanishes. exact linear_independent_iff'.2 -- To do this, we use `finset` induction, (λ s, finset.induction_on s (λ g hg i, false.elim) $ λ a s has ih g hg, -- Here -- * `a` is a new character we will insert into the `finset` of characters `s`, -- * `ih` is the fact that only the trivial linear combination of characters in `s` is zero -- * `hg` is the fact that `g` are the coefficients of a linear combination summing to zero -- and it remains to prove that `g` vanishes on `insert a s`. -- We now make the key calculation: -- For any character `i` in the original `finset`, we have `g i • i = g i • a` as functions on the -- monoid `G`. have h1 : ∀ i ∈ s, (g i • i : G → L) = g i • a, from λ i his, funext $ λ x : G, -- We prove these expressions are equal by showing -- the differences of their values on each monoid element `x` is zero eq_of_sub_eq_zero $ ih (λ j, g j * j x - g j * a x) (funext $ λ y : G, calc -- After that, it's just a chase scene. (∑ i in s, ((g i * i x - g i * a x) • i : G → L)) y = ∑ i in s, (g i * i x - g i * a x) * i y : finset.sum_apply _ _ _ ... = ∑ i in s, (g i * i x * i y - g i * a x * i y) : finset.sum_congr rfl (λ _ _, sub_mul _ _ _) ... = ∑ i in s, g i * i x * i y - ∑ i in s, g i * a x * i y : finset.sum_sub_distrib ... = (g a * a x * a y + ∑ i in s, g i * i x * i y) - (g a * a x * a y + ∑ i in s, g i * a x * i y) : by rw add_sub_add_left_eq_sub ... = ∑ i in insert a s, g i * i x * i y - ∑ i in insert a s, g i * a x * i y : by rw [finset.sum_insert has, finset.sum_insert has] ... = ∑ i in insert a s, g i * i (x * y) - ∑ i in insert a s, a x * (g i * i y) : congr (congr_arg has_sub.sub (finset.sum_congr rfl $ λ i _, by rw [i.map_mul, mul_assoc])) (finset.sum_congr rfl $ λ _ _, by rw [mul_assoc, mul_left_comm]) ... = (∑ i in insert a s, (g i • i : G → L)) (x * y) - a x * (∑ i in insert a s, (g i • i : G → L)) y : by rw [finset.sum_apply, finset.sum_apply, finset.mul_sum]; refl ... = 0 - a x * 0 : by rw hg; refl ... = 0 : by rw [mul_zero, sub_zero]) i his, -- On the other hand, since `a` is not already in `s`, for any character `i ∈ s` -- there is some element of the monoid on which it differs from `a`. have h2 : ∀ i : G →* L, i ∈ s → ∃ y, i y ≠ a y, from λ i his, classical.by_contradiction $ λ h, have hia : i = a, from monoid_hom.ext $ λ y, classical.by_contradiction $ λ hy, h ⟨y, hy⟩, has $ hia ▸ his, -- From these two facts we deduce that `g` actually vanishes on `s`, have h3 : ∀ i ∈ s, g i = 0, from λ i his, let ⟨y, hy⟩ := h2 i his in have h : g i • i y = g i • a y, from congr_fun (h1 i his) y, or.resolve_right (mul_eq_zero.1 $ by rw [mul_sub, sub_eq_zero]; exact h) (sub_ne_zero_of_ne hy), -- And so, using the fact that the linear combination over `s` and over `insert a s` both vanish, -- we deduce that `g a = 0`. have h4 : g a = 0, from calc g a = g a * 1 : (mul_one _).symm ... = (g a • a : G → L) 1 : by rw ← a.map_one; refl ... = (∑ i in insert a s, (g i • i : G → L)) 1 : begin rw finset.sum_eq_single a, { intros i his hia, rw finset.mem_insert at his, rw [h3 i (his.resolve_left hia), zero_smul] }, { intros haas, exfalso, apply haas, exact finset.mem_insert_self a s } end ... = 0 : by rw hg; refl, -- Now we're done; the last two facts together imply that `g` vanishes on every element -- of `insert a s`. (finset.forall_mem_insert _ _ _).2 ⟨h4, h3⟩) lemma le_of_span_le_span [nontrivial R] {s t u: set M} (hl : linear_independent R (coe : u → M )) (hsu : s ⊆ u) (htu : t ⊆ u) (hst : span R s ≤ span R t) : s ⊆ t := begin have := eq_of_linear_independent_of_span_subtype (hl.mono (set.union_subset hsu htu)) (set.subset_union_right _ _) (set.union_subset (set.subset.trans subset_span hst) subset_span), rw ← this, apply set.subset_union_left end lemma span_le_span_iff [nontrivial R] {s t u: set M} (hl : linear_independent R (coe : u → M)) (hsu : s ⊆ u) (htu : t ⊆ u) : span R s ≤ span R t ↔ s ⊆ t := ⟨le_of_span_le_span hl hsu htu, span_mono⟩ end module section nontrivial variables [ring R] [nontrivial R] [add_comm_group M] [add_comm_group M'] variables [module R M] [no_zero_smul_divisors R M] [module R M'] variables {v : ι → M} {s t : set M} {x y z : M} lemma linear_independent_unique_iff (v : ι → M) [unique ι] : linear_independent R v ↔ v (default ι) ≠ 0 := begin simp only [linear_independent_iff, finsupp.total_unique, smul_eq_zero], refine ⟨λ h hv, _, λ hv l hl, finsupp.unique_ext $ hl.resolve_right hv⟩, have := h (finsupp.single (default ι) 1) (or.inr hv), exact one_ne_zero (finsupp.single_eq_zero.1 this) end alias linear_independent_unique_iff ↔ _ linear_independent_unique lemma linear_independent_singleton {x : M} (hx : x ≠ 0) : linear_independent R (λ x, x : ({x} : set M) → M) := linear_independent_unique coe hx end nontrivial /-! ### Properties which require `division_ring K` These can be considered generalizations of properties of linear independence in vector spaces. -/ section module variables [division_ring K] [add_comm_group V] [add_comm_group V'] variables [module K V] [module K V'] variables {v : ι → V} {s t : set V} {x y z : V} open submodule /- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class (instead of a data containing type class) -/ lemma mem_span_insert_exchange : x ∈ span K (insert y s) → x ∉ span K s → y ∈ span K (insert x s) := begin simp [mem_span_insert], rintro a z hz rfl h, refine ⟨a⁻¹, -a⁻¹ • z, smul_mem _ _ hz, _⟩, have a0 : a ≠ 0, {rintro rfl, simp * at *}, simp [a0, smul_add, smul_smul] end lemma linear_independent_iff_not_mem_span : linear_independent K v ↔ (∀i, v i ∉ span K (v '' (univ \ {i}))) := begin apply linear_independent_iff_not_smul_mem_span.trans, split, { intros h i h_in_span, apply one_ne_zero (h i 1 (by simp [h_in_span])) }, { intros h i a ha, by_contradiction ha', exact false.elim (h _ ((smul_mem_iff _ ha').1 ha)) } end lemma linear_independent.insert (hs : linear_independent K (λ b, b : s → V)) (hx : x ∉ span K s) : linear_independent K (λ b, b : insert x s → V) := begin rw ← union_singleton, have x0 : x ≠ 0 := mt (by rintro rfl; apply zero_mem _) hx, apply hs.union (linear_independent_singleton x0), rwa [disjoint_span_singleton' x0] end lemma linear_independent_option' : linear_independent K (λ o, option.cases_on' o x v : option ι → V) ↔ linear_independent K v ∧ (x ∉ submodule.span K (range v)) := begin rw [← linear_independent_equiv (equiv.option_equiv_sum_punit ι).symm, linear_independent_sum, @range_unique _ punit, @linear_independent_unique_iff punit, disjoint_span_singleton], dsimp [(∘)], refine ⟨λ h, ⟨h.1, λ hx, h.2.1 $ h.2.2 hx⟩, λ h, ⟨h.1, _, λ hx, (h.2 hx).elim⟩⟩, rintro rfl, exact h.2 (zero_mem _) end lemma linear_independent.option (hv : linear_independent K v) (hx : x ∉ submodule.span K (range v)) : linear_independent K (λ o, option.cases_on' o x v : option ι → V) := linear_independent_option'.2 ⟨hv, hx⟩ lemma linear_independent_option {v : option ι → V} : linear_independent K v ↔ linear_independent K (v ∘ coe : ι → V) ∧ v none ∉ submodule.span K (range (v ∘ coe : ι → V)) := by simp only [← linear_independent_option', option.cases_on'_none_coe] theorem linear_independent_insert' {ι} {s : set ι} {a : ι} {f : ι → V} (has : a ∉ s) : linear_independent K (λ x : insert a s, f x) ↔ linear_independent K (λ x : s, f x) ∧ f a ∉ submodule.span K (f '' s) := by { rw [← linear_independent_equiv ((equiv.option_equiv_sum_punit _).trans (equiv.set.insert has).symm), linear_independent_option], simp [(∘), range_comp f] } theorem linear_independent_insert (hxs : x ∉ s) : linear_independent K (λ b : insert x s, (b : V)) ↔ linear_independent K (λ b : s, (b : V)) ∧ x ∉ submodule.span K s := (@linear_independent_insert' _ _ _ _ _ _ _ _ id hxs).trans $ by simp lemma linear_independent_pair {x y : V} (hx : x ≠ 0) (hy : ∀ a : K, a • x ≠ y) : linear_independent K (coe : ({x, y} : set V) → V) := pair_comm y x ▸ (linear_independent_singleton hx).insert $ mt mem_span_singleton.1 (not_exists.2 hy) lemma linear_independent_fin_cons {n} {v : fin n → V} : linear_independent K (fin.cons x v : fin (n + 1) → V) ↔ linear_independent K v ∧ x ∉ submodule.span K (range v) := begin rw [← linear_independent_equiv (fin_succ_equiv n).symm, linear_independent_option], convert iff.rfl, { ext, -- TODO: why doesn't simp use `fin_succ_equiv_symm_coe` here? rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] }, { ext, rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] } end lemma linear_independent_fin_snoc {n} {v : fin n → V} : linear_independent K (fin.snoc v x : fin (n + 1) → V) ↔ linear_independent K v ∧ x ∉ submodule.span K (range v) := by rw [fin.snoc_eq_cons_rotate, linear_independent_equiv, linear_independent_fin_cons] /-- See `linear_independent.fin_cons'` for an uglier version that works if you only have a module over a semiring. -/ lemma linear_independent.fin_cons {n} {v : fin n → V} (hv : linear_independent K v) (hx : x ∉ submodule.span K (range v)) : linear_independent K (fin.cons x v : fin (n + 1) → V) := linear_independent_fin_cons.2 ⟨hv, hx⟩ lemma linear_independent_fin_succ {n} {v : fin (n + 1) → V} : linear_independent K v ↔ linear_independent K (fin.tail v) ∧ v 0 ∉ submodule.span K (range $ fin.tail v) := by rw [← linear_independent_fin_cons, fin.cons_self_tail] lemma linear_independent_fin_succ' {n} {v : fin (n + 1) → V} : linear_independent K v ↔ linear_independent K (fin.init v) ∧ v (fin.last _) ∉ submodule.span K (range $ fin.init v) := by rw [← linear_independent_fin_snoc, fin.snoc_init_self] lemma linear_independent_fin2 {f : fin 2 → V} : linear_independent K f ↔ f 1 ≠ 0 ∧ ∀ a : K, a • f 1 ≠ f 0 := by rw [linear_independent_fin_succ, linear_independent_unique_iff, range_unique, mem_span_singleton, not_exists, show fin.tail f (default (fin 1)) = f 1, by rw ← fin.succ_zero_eq_one; refl] lemma exists_linear_independent_extension (hs : linear_independent K (coe : s → V)) (hst : s ⊆ t) : ∃b⊆t, s ⊆ b ∧ t ⊆ span K b ∧ linear_independent K (coe : b → V) := begin rcases zorn.zorn_subset_nonempty {b | b ⊆ t ∧ linear_independent K (coe : b → V)} _ _ ⟨hst, hs⟩ with ⟨b, ⟨bt, bi⟩, sb, h⟩, { refine ⟨b, bt, sb, λ x xt, _, bi⟩, by_contra hn, apply hn, rw ← h _ ⟨insert_subset.2 ⟨xt, bt⟩, bi.insert hn⟩ (subset_insert _ _), exact subset_span (mem_insert _ _) }, { refine λ c hc cc c0, ⟨⋃₀ c, ⟨_, _⟩, λ x, _⟩, { exact sUnion_subset (λ x xc, (hc xc).1) }, { exact linear_independent_sUnion_of_directed cc.directed_on (λ x xc, (hc xc).2) }, { exact subset_sUnion_of_mem } } end variables (K t) lemma exists_linear_independent : ∃ b ⊆ t, span K b = span K t ∧ linear_independent K (coe : b → V) := begin obtain ⟨b, hb₁, -, hb₂, hb₃⟩ := exists_linear_independent_extension (linear_independent_empty K V) (set.empty_subset t), exact ⟨b, hb₁, (span_eq_of_le _ hb₂ (submodule.span_mono hb₁)).symm, hb₃⟩, end variables {K t} /-- `linear_independent.extend` adds vectors to a linear independent set `s ⊆ t` until it spans all elements of `t`. -/ noncomputable def linear_independent.extend (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : set V := classical.some (exists_linear_independent_extension hs hst) lemma linear_independent.extend_subset (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : hs.extend hst ⊆ t := let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hbt lemma linear_independent.subset_extend (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : s ⊆ hs.extend hst := let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hsb lemma linear_independent.subset_span_extend (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : t ⊆ span K (hs.extend hst) := let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in htb lemma linear_independent.linear_independent_extend (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : linear_independent K (coe : hs.extend hst → V) := let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hli variables {K V} -- TODO(Mario): rewrite? lemma exists_of_linear_independent_of_finite_span {t : finset V} (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ (span K ↑t : submodule K V)) : ∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = t.card := have ∀t, ∀(s' : finset V), ↑s' ⊆ s → s ∩ ↑t = ∅ → s ⊆ (span K ↑(s' ∪ t) : submodule K V) → ∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = (s' ∪ t).card := assume t, finset.induction_on t (assume s' hs' _ hss', have s = ↑s', from eq_of_linear_independent_of_span_subtype hs hs' $ by simpa using hss', ⟨s', by simp [this]⟩) (assume b₁ t hb₁t ih s' hs' hst hss', have hb₁s : b₁ ∉ s, from assume h, have b₁ ∈ s ∩ ↑(insert b₁ t), from ⟨h, finset.mem_insert_self _ _⟩, by rwa [hst] at this, have hb₁s' : b₁ ∉ s', from assume h, hb₁s $ hs' h, have hst : s ∩ ↑t = ∅, from eq_empty_of_subset_empty $ subset.trans (by simp [inter_subset_inter, subset.refl]) (le_of_eq hst), classical.by_cases (assume : s ⊆ (span K ↑(s' ∪ t) : submodule K V), let ⟨u, hust, hsu, eq⟩ := ih _ hs' hst this in have hb₁u : b₁ ∉ u, from assume h, (hust h).elim hb₁s hb₁t, ⟨insert b₁ u, by simp [insert_subset_insert hust], subset.trans hsu (by simp), by simp [eq, hb₁t, hb₁s', hb₁u]⟩) (assume : ¬ s ⊆ (span K ↑(s' ∪ t) : submodule K V), let ⟨b₂, hb₂s, hb₂t⟩ := not_subset.mp this in have hb₂t' : b₂ ∉ s' ∪ t, from assume h, hb₂t $ subset_span h, have s ⊆ (span K ↑(insert b₂ s' ∪ t) : submodule K V), from assume b₃ hb₃, have ↑(s' ∪ insert b₁ t) ⊆ insert b₁ (insert b₂ ↑(s' ∪ t) : set V), by simp [insert_eq, -singleton_union, -union_singleton, union_subset_union, subset.refl, subset_union_right], have hb₃ : b₃ ∈ span K (insert b₁ (insert b₂ ↑(s' ∪ t) : set V)), from span_mono this (hss' hb₃), have s ⊆ (span K (insert b₁ ↑(s' ∪ t)) : submodule K V), by simpa [insert_eq, -singleton_union, -union_singleton] using hss', have hb₁ : b₁ ∈ span K (insert b₂ ↑(s' ∪ t)), from mem_span_insert_exchange (this hb₂s) hb₂t, by rw [span_insert_eq_span hb₁] at hb₃; simpa using hb₃, let ⟨u, hust, hsu, eq⟩ := ih _ (by simp [insert_subset, hb₂s, hs']) hst this in ⟨u, subset.trans hust $ union_subset_union (subset.refl _) (by simp [subset_insert]), hsu, by simp [eq, hb₂t', hb₁t, hb₁s']⟩)), begin have eq : t.filter (λx, x ∈ s) ∪ t.filter (λx, x ∉ s) = t, { ext1 x, by_cases x ∈ s; simp * }, apply exists.elim (this (t.filter (λx, x ∉ s)) (t.filter (λx, x ∈ s)) (by simp [set.subset_def]) (by simp [set.ext_iff] {contextual := tt}) (by rwa [eq])), intros u h, exact ⟨u, subset.trans h.1 (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}), h.2.1, by simp only [h.2.2, eq]⟩ end lemma exists_finite_card_le_of_finite_of_linear_independent_of_span (ht : finite t) (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ span K t) : ∃h : finite s, h.to_finset.card ≤ ht.to_finset.card := have s ⊆ (span K ↑(ht.to_finset) : submodule K V), by simp; assumption, let ⟨u, hust, hsu, eq⟩ := exists_of_linear_independent_of_finite_span hs this in have finite s, from u.finite_to_set.subset hsu, ⟨this, by rw [←eq]; exact (finset.card_le_of_subset $ finset.coe_subset.mp $ by simp [hsu])⟩ end module
b7bb1033541326de955c86f125a6d6288c8ee7d8
4727251e0cd73359b15b664c3170e5d754078599
/src/logic/denumerable.lean
0f72967e03d89d8c64a7223cf8df65c0ba6973d0
[ "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
10,766
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 data.fintype.basic import data.list.min_max import logic.encodable.basic /-! # Denumerable types This file defines denumerable (countably infinite) types as a typeclass extending `encodable`. This is used to provide explicit encode/decode functions from and to `ℕ`, with the information that those functions are inverses of each other. ## Implementation notes This property already has a name, namely `α ≃ ℕ`, but here we are interested in using it as a typeclass. -/ /-- A denumerable type is (constructively) bijective with `ℕ`. Typeclass equivalent of `α ≃ ℕ`. -/ class denumerable (α : Type*) extends encodable α := (decode_inv : ∀ n, ∃ a ∈ decode n, encode a = n) open nat namespace denumerable section variables {α : Type*} {β : Type*} [denumerable α] [denumerable β] open encodable theorem decode_is_some (α) [denumerable α] (n : ℕ) : (decode α n).is_some := option.is_some_iff_exists.2 $ (decode_inv n).imp $ λ a, Exists.fst /-- Returns the `n`-th element of `α` indexed by the decoding. -/ def of_nat (α) [f : denumerable α] (n : ℕ) : α := option.get (decode_is_some α n) @[simp, priority 900] theorem decode_eq_of_nat (α) [denumerable α] (n : ℕ) : decode α n = some (of_nat α n) := option.eq_some_of_is_some _ @[simp] theorem of_nat_of_decode {n b} (h : decode α n = some b) : of_nat α n = b := option.some.inj $ (decode_eq_of_nat _ _).symm.trans h @[simp] theorem encode_of_nat (n) : encode (of_nat α n) = n := let ⟨a, h, e⟩ := decode_inv n in by rwa [of_nat_of_decode h] @[simp] theorem of_nat_encode (a) : of_nat α (encode a) = a := of_nat_of_decode (encodek _) /-- A denumerable type is equivalent to `ℕ`. -/ def eqv (α) [denumerable α] : α ≃ ℕ := ⟨encode, of_nat α, of_nat_encode, encode_of_nat⟩ @[priority 100] -- See Note [lower instance priority] instance : infinite α := infinite.of_surjective _ (eqv α).surjective /-- A type equivalent to `ℕ` is denumerable. -/ def mk' {α} (e : α ≃ ℕ) : denumerable α := { encode := e, decode := some ∘ e.symm, encodek := λ a, congr_arg some (e.symm_apply_apply _), decode_inv := λ n, ⟨_, rfl, e.apply_symm_apply _⟩ } /-- Denumerability is conserved by equivalences. This is transitivity of equivalence the denumerable way. -/ def of_equiv (α) {β} [denumerable α] (e : β ≃ α) : denumerable β := { decode_inv := λ n, by simp, ..encodable.of_equiv _ e } @[simp] theorem of_equiv_of_nat (α) {β} [denumerable α] (e : β ≃ α) (n) : @of_nat β (of_equiv _ e) n = e.symm (of_nat α n) := by apply of_nat_of_decode; show option.map _ _ = _; simp /-- All denumerable types are equivalent. -/ def equiv₂ (α β) [denumerable α] [denumerable β] : α ≃ β := (eqv α).trans (eqv β).symm instance nat : denumerable ℕ := ⟨λ n, ⟨_, rfl, rfl⟩⟩ @[simp] theorem of_nat_nat (n) : of_nat ℕ n = n := rfl /-- If `α` is denumerable, then so is `option α`. -/ instance option : denumerable (option α) := ⟨λ n, begin cases n, { refine ⟨none, _, encode_none⟩, rw [decode_option_zero, option.mem_def] }, refine ⟨some (of_nat α n), _, _⟩, { rw [decode_option_succ, decode_eq_of_nat, option.map_some', option.mem_def] }, rw [encode_some, encode_of_nat], end⟩ /-- If `α` and `β` are denumerable, then so is their sum. -/ instance sum : denumerable (α ⊕ β) := ⟨λ n, begin suffices : ∃ a ∈ @decode_sum α β _ _ n, encode_sum a = bit (bodd n) (div2 n), {simpa [bit_decomp]}, simp [decode_sum]; cases bodd n; simp [decode_sum, bit, encode_sum] end⟩ section sigma variables {γ : α → Type*} [∀ a, denumerable (γ a)] /-- A denumerable collection of denumerable types is denumerable. -/ instance sigma : denumerable (sigma γ) := ⟨λ n, by simp [decode_sigma]; exact ⟨_, _, ⟨rfl, heq.rfl⟩, by simp⟩⟩ @[simp] theorem sigma_of_nat_val (n : ℕ) : of_nat (sigma γ) n = ⟨of_nat α (unpair n).1, of_nat (γ _) (unpair n).2⟩ := option.some.inj $ by rw [← decode_eq_of_nat, decode_sigma_val]; simp; refl end sigma /-- If `α` and `β` are denumerable, then so is their product. -/ instance prod : denumerable (α × β) := of_equiv _ (equiv.sigma_equiv_prod α β).symm @[simp] theorem prod_of_nat_val (n : ℕ) : of_nat (α × β) n = (of_nat α (unpair n).1, of_nat β (unpair n).2) := by simp; refl @[simp] theorem prod_nat_of_nat : of_nat (ℕ × ℕ) = unpair := by funext; simp instance int : denumerable ℤ := denumerable.mk' equiv.int_equiv_nat instance pnat : denumerable ℕ+ := denumerable.mk' equiv.pnat_equiv_nat /-- The lift of a denumerable type is denumerable. -/ instance ulift : denumerable (ulift α) := of_equiv _ equiv.ulift /-- The lift of a denumerable type is denumerable. -/ instance plift : denumerable (plift α) := of_equiv _ equiv.plift /-- If `α` is denumerable, then `α × α` and `α` are equivalent. -/ def pair : α × α ≃ α := equiv₂ _ _ end end denumerable namespace nat.subtype open function encodable /-! ### Subsets of `ℕ` -/ variables {s : set ℕ} [infinite s] section classical open_locale classical lemma exists_succ (x : s) : ∃ n, ↑x + n + 1 ∈ s := classical.by_contradiction $ λ h, have ∀ (a : ℕ) (ha : a ∈ s), a < succ x, from λ a ha, lt_of_not_ge (λ hax, h ⟨a - (x + 1), by rwa [add_right_comm, add_tsub_cancel_of_le hax]⟩), fintype.false ⟨(((multiset.range (succ x)).filter (∈ s)).pmap (λ (y : ℕ) (hy : y ∈ s), subtype.mk y hy) (by simp [-multiset.range_succ])).to_finset, by simpa [subtype.ext_iff_val, multiset.mem_filter, -multiset.range_succ]⟩ end classical variable [decidable_pred (∈ s)] /-- Returns the next natural in a set, according to the usual ordering of `ℕ`. -/ def succ (x : s) : s := have h : ∃ m, ↑x + m + 1 ∈ s, from exists_succ x, ⟨↑x + nat.find h + 1, nat.find_spec h⟩ lemma succ_le_of_lt {x y : s} (h : y < x) : succ y ≤ x := have hx : ∃ m, ↑y + m + 1 ∈ s, from exists_succ _, let ⟨k, hk⟩ := nat.exists_eq_add_of_lt h in have nat.find hx ≤ k, from nat.find_min' _ (hk ▸ x.2), show (y : ℕ) + nat.find hx + 1 ≤ x, by rw hk; exact add_le_add_right (add_le_add_left this _) _ lemma le_succ_of_forall_lt_le {x y : s} (h : ∀ z < x, z ≤ y) : x ≤ succ y := have hx : ∃ m, ↑y + m + 1 ∈ s, from exists_succ _, show ↑x ≤ ↑y + nat.find hx + 1, from le_of_not_gt $ λ hxy, (h ⟨_, nat.find_spec hx⟩ hxy).not_lt $ calc ↑y ≤ ↑y + nat.find hx : le_add_of_nonneg_right (nat.zero_le _) ... < ↑y + nat.find hx + 1 : nat.lt_succ_self _ lemma lt_succ_self (x : s) : x < succ x := calc (x : ℕ) ≤ x + _ : le_self_add ... < succ x : nat.lt_succ_self (x + _) lemma lt_succ_iff_le {x y : s} : x < succ y ↔ x ≤ y := ⟨λ h, le_of_not_gt (λ h', not_le_of_gt h (succ_le_of_lt h')), λ h, lt_of_le_of_lt h (lt_succ_self _)⟩ /-- Returns the `n`-th element of a set, according to the usual ordering of `ℕ`. -/ def of_nat (s : set ℕ) [decidable_pred (∈ s)] [infinite s] : ℕ → s | 0 := ⊥ | (n+1) := succ (of_nat n) lemma of_nat_surjective_aux : ∀ {x : ℕ} (hx : x ∈ s), ∃ n, of_nat s n = ⟨x, hx⟩ | x := λ hx, let t : list s := ((list.range x).filter (λ y, y ∈ s)).pmap (λ (y : ℕ) (hy : y ∈ s), ⟨y, hy⟩) (by simp) in have hmt : ∀ {y : s}, y ∈ t ↔ y < ⟨x, hx⟩, by simp [list.mem_filter, subtype.ext_iff_val, t]; intros; refl, have wf : ∀ m : s, list.maximum t = m → ↑m < x, from λ m hmax, by simpa [hmt] using list.maximum_mem hmax, begin cases hmax : list.maximum t with m, { exact ⟨0, le_antisymm bot_le (le_of_not_gt (λ h, list.not_mem_nil (⊥ : s) $ by rw [← list.maximum_eq_none.1 hmax, hmt]; exact h))⟩ }, cases of_nat_surjective_aux m.2 with a ha, exact ⟨a + 1, le_antisymm (by rw of_nat; exact succ_le_of_lt (by rw ha; exact wf _ hmax)) $ by rw of_nat; exact le_succ_of_forall_lt_le (λ z hz, by rw ha; cases m; exact list.le_maximum_of_mem (hmt.2 hz) hmax)⟩ end using_well_founded {dec_tac := `[tauto]} lemma of_nat_surjective : surjective (of_nat s) := λ ⟨x, hx⟩, of_nat_surjective_aux hx @[simp] lemma of_nat_range : set.range (of_nat s) = set.univ := of_nat_surjective.range_eq @[simp] lemma coe_comp_of_nat_range : set.range (coe ∘ of_nat s : ℕ → ℕ) = s := by rw [set.range_comp coe, of_nat_range, set.image_univ, subtype.range_coe] private def to_fun_aux (x : s) : ℕ := (list.range x).countp (∈ s) private lemma to_fun_aux_eq (x : s) : to_fun_aux x = ((finset.range x).filter (∈ s)).card := by rw [to_fun_aux, list.countp_eq_length_filter]; refl open finset private lemma right_inverse_aux : ∀ n, to_fun_aux (of_nat s n) = n | 0 := begin rw [to_fun_aux_eq, card_eq_zero, eq_empty_iff_forall_not_mem], rintro n hn, rw [mem_filter, of_nat, mem_range] at hn, exact bot_le.not_lt (show (⟨n, hn.2⟩ : s) < ⊥, from hn.1), end | (n+1) := have ih : to_fun_aux (of_nat s n) = n, from right_inverse_aux n, have h₁ : (of_nat s n : ℕ) ∉ (range (of_nat s n)).filter (∈ s), by simp, have h₂ : (range (succ (of_nat s n))).filter (∈ s) = insert (of_nat s n) ((range (of_nat s n)).filter (∈ s)), begin simp only [finset.ext_iff, mem_insert, mem_range, mem_filter], exact λ m, ⟨λ h, by simp only [h.2, and_true]; exact or.symm (lt_or_eq_of_le ((@lt_succ_iff_le _ _ _ ⟨m, h.2⟩ _).1 h.1)), λ h, h.elim (λ h, h.symm ▸ ⟨lt_succ_self _, (of_nat s n).prop⟩) (λ h, ⟨h.1.trans (lt_succ_self _), h.2⟩)⟩, end, begin simp only [to_fun_aux_eq, of_nat, range_succ] at ⊢ ih, conv {to_rhs, rw [← ih, ← card_insert_of_not_mem h₁, ← h₂] }, end /-- Any infinite set of naturals is denumerable. -/ def denumerable (s : set ℕ) [decidable_pred (∈ s)] [infinite s] : denumerable s := denumerable.of_equiv ℕ { to_fun := to_fun_aux, inv_fun := of_nat s, left_inv := left_inverse_of_surjective_of_right_inverse of_nat_surjective right_inverse_aux, right_inv := right_inverse_aux } end nat.subtype namespace denumerable open encodable /-- An infinite encodable type is denumerable. -/ def of_encodable_of_infinite (α : Type*) [encodable α] [infinite α] : denumerable α := begin letI := @decidable_range_encode α _; letI : infinite (set.range (@encode α _)) := infinite.of_injective _ (equiv.of_injective _ encode_injective).injective, letI := nat.subtype.denumerable (set.range (@encode α _)), exact denumerable.of_equiv (set.range (@encode α _)) (equiv_range_encode α), end end denumerable
daacc3c2e5c0904e9ce0afb9a5c4b27908e86bff
08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4
/src/Init/Data/List/Basic.lean
fca0ee920d237f174bb869764d3a90ceade968b1
[ "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", "Apache-2.0", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
gebner/lean4
d51c4922640a52a6f7426536ea669ef18a1d9af5
8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f
refs/heads/master
1,685,732,780,391
1,672,962,627,000
1,673,459,398,000
373,307,283
0
0
Apache-2.0
1,691,316,730,000
1,622,669,271,000
Lean
UTF-8
Lean
false
false
29,112
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.SimpLemmas import Init.Data.Nat.Basic set_option linter.missingDocs true -- keep it documented open Decidable List universe u v w variable {α : Type u} {β : Type v} {γ : Type w} namespace List instance : GetElem (List α) Nat α fun as i => i < as.length where getElem as i h := as.get ⟨i, h⟩ @[simp] theorem cons_getElem_zero (a : α) (as : List α) (h : 0 < (a :: as).length) : getElem (a :: as) 0 h = a := by rfl @[simp] theorem cons_getElem_succ (a : α) (as : List α) (i : Nat) (h : i + 1 < (a :: as).length) : getElem (a :: as) (i+1) h = getElem as i (Nat.lt_of_succ_lt_succ h) := by rfl theorem length_add_eq_lengthTRAux (as : List α) (n : Nat) : as.length + n = as.lengthTRAux n := by induction as generalizing n with | nil => simp [length, lengthTRAux] | cons a as ih => simp [length, lengthTRAux, ← ih, Nat.succ_add] rfl @[csimp] theorem length_eq_lengthTR : @List.length = @List.lengthTR := by apply funext; intro α; apply funext; intro as simp [lengthTR, ← length_add_eq_lengthTRAux] @[simp] theorem length_nil : length ([] : List α) = 0 := rfl /-- Auxiliary for `List.reverse`. `List.reverseAux l r = l.reverse ++ r`, but it is defined directly. -/ def reverseAux : List α → List α → List α | [], r => r | a::l, r => reverseAux l (a::r) /-- `O(|as|)`. Reverse of a list: * `[1, 2, 3, 4].reverse = [4, 3, 2, 1]` Note that because of the "functional but in place" optimization implemented by Lean's compiler, this function works without any allocations provided that the input list is unshared: it simply walks the linked list and reverses all the node pointers. -/ def reverse (as : List α) : List α := reverseAux as [] theorem reverseAux_reverseAux_nil (as bs : List α) : reverseAux (reverseAux as bs) [] = reverseAux bs as := by induction as generalizing bs with | nil => rfl | cons a as ih => simp [reverseAux, ih] theorem reverseAux_reverseAux (as bs cs : List α) : reverseAux (reverseAux as bs) cs = reverseAux bs (reverseAux (reverseAux as []) cs) := by induction as generalizing bs cs with | nil => rfl | cons a as ih => simp [reverseAux, ih (a::bs), ih [a]] @[simp] theorem reverse_reverse (as : List α) : as.reverse.reverse = as := by simp [reverse]; rw [reverseAux_reverseAux_nil]; rfl /-- `O(|xs|)`: append two lists. `[1, 2, 3] ++ [4, 5] = [1, 2, 3, 4, 5]`. It takes time proportional to the first list. -/ protected def append : (xs ys : List α) → List α | [], bs => bs | a::as, bs => a :: List.append as bs /-- Tail-recursive version of `List.append`. -/ def appendTR (as bs : List α) : List α := reverseAux as.reverse bs @[csimp] theorem append_eq_appendTR : @List.append = @appendTR := by apply funext; intro α; apply funext; intro as; apply funext; intro bs simp [appendTR, reverse] induction as with | nil => rfl | cons a as ih => simp [reverseAux, List.append, ih, reverseAux_reverseAux] instance : Append (List α) := ⟨List.append⟩ @[simp] theorem nil_append (as : List α) : [] ++ as = as := rfl @[simp] theorem append_nil (as : List α) : as ++ [] = as := by induction as with | nil => rfl | cons a as ih => simp_all [HAppend.hAppend, Append.append, List.append] @[simp] theorem cons_append (a : α) (as bs : List α) : (a::as) ++ bs = a::(as ++ bs) := rfl @[simp] theorem List.append_eq (as bs : List α) : List.append as bs = as ++ bs := rfl theorem append_assoc (as bs cs : List α) : (as ++ bs) ++ cs = as ++ (bs ++ cs) := by induction as with | nil => rfl | cons a as ih => simp [ih] theorem append_cons (as : List α) (b : α) (bs : List α) : as ++ b :: bs = as ++ [b] ++ bs := by induction as with | nil => simp | cons a as ih => simp [ih] instance : EmptyCollection (List α) := ⟨List.nil⟩ /-- `O(|l|)`. `erase l a` removes the first occurrence of `a` from `l`. * `erase [1, 5, 3, 2, 5] 5 = [1, 3, 2, 5]` * `erase [1, 5, 3, 2, 5] 6 = [1, 5, 3, 2, 5]` -/ protected def erase {α} [BEq α] : List α → α → List α | [], _ => [] | a::as, b => match a == b with | true => as | false => a :: List.erase as b /-- `O(i)`. `eraseIdx l i` removes the `i`'th element of the list `l`. * `erase [a, b, c, d, e] 0 = [b, c, d, e]` * `erase [a, b, c, d, e] 1 = [a, c, d, e]` * `erase [a, b, c, d, e] 5 = [a, b, c, d, e]` -/ def eraseIdx : List α → Nat → List α | [], _ => [] | _::as, 0 => as | a::as, n+1 => a :: eraseIdx as n /-- `O(1)`. `isEmpty l` is true if the list is empty. * `isEmpty [] = true` * `isEmpty [a] = false` * `isEmpty [a, b] = false` -/ def isEmpty : List α → Bool | [] => true | _ :: _ => false /-- `O(|l|)`. `map f l` applies `f` to each element of the list. * `map f [a, b, c] = [f a, f b, f c]` -/ @[specialize] def map (f : α → β) : List α → List β | [] => [] | a::as => f a :: map f as /-- Tail-recursive version of `List.map`. -/ @[inline] def mapTR (f : α → β) (as : List α) : List β := loop as [] where @[specialize] loop : List α → List β → List β | [], bs => bs.reverse | a::as, bs => loop as (f a :: bs) theorem reverseAux_eq_append (as bs : List α) : reverseAux as bs = reverseAux as [] ++ bs := by induction as generalizing bs with | nil => simp [reverseAux] | cons a as ih => simp [reverseAux] rw [ih (a :: bs), ih [a], append_assoc] rfl @[simp] theorem reverse_nil : reverse ([] : List α) = [] := rfl @[simp] theorem reverse_cons (a : α) (as : List α) : reverse (a :: as) = reverse as ++ [a] := by simp [reverse, reverseAux] rw [← reverseAux_eq_append] @[simp] theorem reverse_append (as bs : List α) : (as ++ bs).reverse = bs.reverse ++ as.reverse := by induction as generalizing bs with | nil => simp | cons a as ih => simp [ih]; rw [append_assoc] theorem mapTR_loop_eq (f : α → β) (as : List α) (bs : List β) : mapTR.loop f as bs = bs.reverse ++ map f as := by induction as generalizing bs with | nil => simp [mapTR.loop, map] | cons a as ih => simp [mapTR.loop, map] rw [ih (f a :: bs), reverse_cons, append_assoc] rfl @[csimp] theorem map_eq_mapTR : @map = @mapTR := funext fun α => funext fun β => funext fun f => funext fun as => by simp [mapTR, mapTR_loop_eq] /-- `O(|join L|)`. `join L` concatenates all the lists in `L` into one list. * `join [[a], [], [b, c], [d, e, f]] = [a, b, c, d, e, f]` -/ def join : List (List α) → List α | [] => [] | a :: as => a ++ join as /-- `O(|l|)`. `filterMap f l` takes a function `f : α → Option β` and applies it to each element of `l`; the resulting non-`none` values are collected to form the output list. ``` filterMap (fun x => if x > 2 then some (2 * x) else none) [1, 2, 5, 2, 7, 7] = [10, 14, 14] ``` -/ @[specialize] def filterMap (f : α → Option β) : List α → List β | [] => [] | a::as => match f a with | none => filterMap f as | some b => b :: filterMap f as /-- `O(|l|)`. `filter f l` returns the list of elements in `l` for which `f` returns true. ``` filter (· > 2) [1, 2, 5, 2, 7, 7] = [5, 7, 7] ``` -/ def filter (p : α → Bool) : List α → List α | [] => [] | a::as => match p a with | true => a :: filter p as | false => filter p as /-- Tail-recursive version of `List.filter`. -/ @[inline] def filterTR (p : α → Bool) (as : List α) : List α := loop as [] where @[specialize] loop : List α → List α → List α | [], rs => rs.reverse | a::as, rs => match p a with | true => loop as (a::rs) | false => loop as rs theorem filterTR_loop_eq (p : α → Bool) (as bs : List α) : filterTR.loop p as bs = bs.reverse ++ filter p as := by induction as generalizing bs with | nil => simp [filterTR.loop, filter] | cons a as ih => simp [filterTR.loop, filter] split next => rw [ih, reverse_cons, append_assoc]; simp next => rw [ih] @[csimp] theorem filter_eq_filterTR : @filter = @filterTR := by apply funext; intro α; apply funext; intro p; apply funext; intro as simp [filterTR, filterTR_loop_eq] /-- `O(|l|)`. `partition p l` calls `p` on each element of `l`, partitioning the list into two lists `(l_true, l_false)` where `l_true` has the elements where `p` was true and `l_false` has the elements where `p` is false. `partition p l = (filter p l, filter (not ∘ p) l)`, but it is slightly more efficient since it only has to do one pass over the list. ``` partition (· > 2) [1, 2, 5, 2, 7, 7] = ([5, 7, 7], [1, 2, 2]) ``` -/ @[inline] def partition (p : α → Bool) (as : List α) : List α × List α := loop as ([], []) where @[specialize] loop : List α → List α × List α → List α × List α | [], (bs, cs) => (bs.reverse, cs.reverse) | a::as, (bs, cs) => match p a with | true => loop as (a::bs, cs) | false => loop as (bs, a::cs) /-- `O(|l|)`. `dropWhile p l` removes elements from the list until it finds the first element for which `p` returns false; this element and everything after it is returned. ``` dropWhile (· < 4) [1, 3, 2, 4, 2, 7, 4] = [4, 2, 7, 4] ``` -/ def dropWhile (p : α → Bool) : List α → List α | [] => [] | a::l => match p a with | true => dropWhile p l | false => a::l /-- `O(|l|)`. `find? p l` returns the first element for which `p` returns true, or `none` if no such element is found. * `find? (· < 5) [7, 6, 5, 8, 1, 2, 6] = some 1` * `find? (· < 1) [7, 6, 5, 8, 1, 2, 6] = none` -/ def find? (p : α → Bool) : List α → Option α | [] => none | a::as => match p a with | true => some a | false => find? p as /-- `O(|l|)`. `findSome? f l` applies `f` to each element of `l`, and returns the first non-`none` result. * `findSome? (fun x => if x < 5 then some (10 * x) else none) [7, 6, 5, 8, 1, 2, 6] = some 10` -/ def findSome? (f : α → Option β) : List α → Option β | [] => none | a::as => match f a with | some b => some b | none => findSome? f as /-- `O(|l|)`. `replace l a b` replaces the first element in the list equal to `a` with `b`. * `replace [1, 4, 2, 3, 3, 7] 3 6 = [1, 4, 2, 6, 3, 7]` * `replace [1, 4, 2, 3, 3, 7] 5 6 = [1, 4, 2, 3, 3, 7]` -/ def replace [BEq α] : List α → α → α → List α | [], _, _ => [] | a::as, b, c => match a == b with | true => c::as | false => a :: replace as b c /-- `O(|l|)`. `elem a l` or `l.contains a` is true if there is an element in `l` equal to `a`. * `elem 3 [1, 4, 2, 3, 3, 7] = true` * `elem 5 [1, 4, 2, 3, 3, 7] = false` -/ def elem [BEq α] (a : α) : List α → Bool | [] => false | b::bs => match a == b with | true => true | false => elem a bs /-- `notElem a l` is `!(elem a l)`. -/ def notElem [BEq α] (a : α) (as : List α) : Bool := !(as.elem a) @[inherit_doc elem] abbrev contains [BEq α] (as : List α) (a : α) : Bool := elem a as /-- `a ∈ l` is a predicate which asserts that `a` is in the list `l`. Unlike `elem`, this uses `=` instead of `==` and is suited for mathematical reasoning. * `a ∈ [x, y, z] ↔ a = x ∨ a = y ∨ a = z` -/ inductive Mem (a : α) : List α → Prop /-- The head of a list is a member: `a ∈ a :: as`. -/ | head (as : List α) : Mem a (a::as) /-- A member of the tail of a list is a member of the list: `a ∈ l → a ∈ b :: l`. -/ | tail (b : α) {as : List α} : Mem a as → Mem a (b::as) instance : Membership α (List α) where mem := Mem theorem mem_of_elem_eq_true [DecidableEq α] {a : α} {as : List α} : elem a as = true → a ∈ as := by match as with | [] => simp [elem] | a'::as => simp [elem] split next h => intros; simp [BEq.beq] at h; subst h; apply Mem.head next _ => intro h; exact Mem.tail _ (mem_of_elem_eq_true h) theorem elem_eq_true_of_mem [DecidableEq α] {a : α} {as : List α} (h : a ∈ as) : elem a as = true := by induction h with | head _ => simp [elem] | tail _ _ ih => simp [elem]; split; rfl; assumption instance [DecidableEq α] (a : α) (as : List α) : Decidable (a ∈ as) := decidable_of_decidable_of_iff (Iff.intro mem_of_elem_eq_true elem_eq_true_of_mem) theorem mem_append_of_mem_left {a : α} {as : List α} (bs : List α) : a ∈ as → a ∈ as ++ bs := by intro h induction h with | head => apply Mem.head | tail => apply Mem.tail; assumption theorem mem_append_of_mem_right {b : α} {bs : List α} (as : List α) : b ∈ bs → b ∈ as ++ bs := by intro h induction as with | nil => simp [h] | cons => apply Mem.tail; assumption /-- `O(|l|^2)`. Erase duplicated elements in the list. Keeps the first occurrence of duplicated elements. * `eraseDups [1, 3, 2, 2, 3, 5] = [1, 3, 2, 5]` -/ def eraseDups {α} [BEq α] (as : List α) : List α := loop as [] where loop : List α → List α → List α | [], bs => bs.reverse | a::as, bs => match bs.elem a with | true => loop as bs | false => loop as (a::bs) /-- `O(|l|)`. Erase repeated adjacent elements. Keeps the first occurrence of each run. * `eraseReps [1, 3, 2, 2, 2, 3, 5] = [1, 3, 2, 3, 5]` -/ def eraseReps {α} [BEq α] : List α → List α | [] => [] | a::as => loop a as [] where loop {α} [BEq α] : α → List α → List α → List α | a, [], rs => (a::rs).reverse | a, a'::as, rs => match a == a' with | true => loop a as rs | false => loop a' as (a::rs) /-- `O(|l|)`. `span p l` splits the list `l` into two parts, where the first part contains the longest initial segment for which `p` returns true and the second part is everything else. * `span (· > 5) [6, 8, 9, 5, 2, 9] = ([6, 8, 9], [5, 2, 9])` * `span (· > 10) [6, 8, 9, 5, 2, 9] = ([6, 8, 9, 5, 2, 9], [])` -/ @[inline] def span (p : α → Bool) (as : List α) : List α × List α := loop as [] where @[specialize] loop : List α → List α → List α × List α | [], rs => (rs.reverse, []) | a::as, rs => match p a with | true => loop as (a::rs) | false => (rs.reverse, a::as) /-- `O(|l|)`. `groupBy R l` splits `l` into chains of elements such that adjacent elements are related by `R`. * `groupBy (·==·) [1, 1, 2, 2, 2, 3, 2] = [[1, 1], [2, 2, 2], [3], [2]]` * `groupBy (·<·) [1, 2, 5, 4, 5, 1, 4] = [[1, 2, 5], [4, 5], [1, 4]]` -/ @[specialize] def groupBy (R : α → α → Bool) : List α → List (List α) | [] => [] | a::as => loop as a [] [] where @[specialize] loop : List α → α → List α → List (List α) → List (List α) | a::as, ag, g, gs => match R ag a with | true => loop as a (ag::g) gs | false => loop as a [] ((ag::g).reverse::gs) | [], ag, g, gs => ((ag::g).reverse::gs).reverse /-- `O(|l|)`. `lookup a l` treats `l : List (α × β)` like an association list, and returns the first `β` value corresponding to an `α` value in the list equal to `a`. * `lookup 3 [(1, 2), (3, 4), (3, 5)] = some 4` * `lookup 2 [(1, 2), (3, 4), (3, 5)] = none` -/ def lookup [BEq α] : α → List (α × β) → Option β | _, [] => none | a, (k,b)::es => match a == k with | true => some b | false => lookup a es /-- `O(|xs|)`. Computes the "set difference" of lists, by filtering out all elements of `xs` which are also in `ys`. * `removeAll [1, 1, 5, 1, 2, 4, 5] [1, 2, 2] = [5, 4, 5]` -/ def removeAll [BEq α] (xs ys : List α) : List α := xs.filter (fun x => ys.notElem x) /-- `O(min n |xs|)`. Removes the first `n` elements of `xs`. * `drop 0 [a, b, c, d, e] = [a, b, c, d, e]` * `drop 3 [a, b, c, d, e] = [d, e]` * `drop 6 [a, b, c, d, e] = []` -/ def drop : Nat → List α → List α | 0, a => a | _+1, [] => [] | n+1, _::as => drop n as @[simp] theorem drop_nil : ([] : List α).drop i = [] := by cases i <;> rfl theorem get_drop_eq_drop (as : List α) (i : Nat) (h : i < as.length) : as[i] :: as.drop (i+1) = as.drop i := match as, i with | _::_, 0 => rfl | _::_, i+1 => get_drop_eq_drop _ i _ /-- `O(min n |xs|)`. Returns the first `n` elements of `xs`, or the whole list if `n` is too large. * `take 0 [a, b, c, d, e] = []` * `take 3 [a, b, c, d, e] = [a, b, c]` * `take 6 [a, b, c, d, e] = [a, b, c, d, e]` -/ def take : Nat → List α → List α | 0, _ => [] | _+1, [] => [] | n+1, a::as => a :: take n as /-- `O(|xs|)`. Returns the longest initial segment of `xs` for which `p` returns true. * `takeWhile (· > 5) [7, 6, 4, 8] = [7, 6]` * `takeWhile (· > 5) [7, 6, 6, 8] = [7, 6, 6, 8]` -/ def takeWhile (p : α → Bool) : (xs : List α) → List α | [] => [] | hd :: tl => match p hd with | true => hd :: takeWhile p tl | false => [] /-- `O(|l|)`. Applies function `f` to all of the elements of the list, from right to left. * `foldr f init [a, b, c] = f a <| f b <| f c <| init` -/ @[specialize] def foldr (f : α → β → β) (init : β) : List α → β | [] => init | a :: l => f a (foldr f init l) /-- `O(|l|)`. Returns true if `p` is true for any element of `l`. * `any p [a, b, c] = p a || p b || p c` -/ @[inline] def any (l : List α) (p : α → Bool) : Bool := foldr (fun a r => p a || r) false l /-- `O(|l|)`. Returns true if `p` is true for every element of `l`. * `any p [a, b, c] = p a && p b && p c` -/ @[inline] def all (l : List α) (p : α → Bool) : Bool := foldr (fun a r => p a && r) true l /-- `O(|l|)`. Returns true if `true` is an element of the list of booleans `l`. * `or [a, b, c] = a || b || c` -/ def or (bs : List Bool) : Bool := bs.any id /-- `O(|l|)`. Returns true if every element of `l` is the value `true`. * `and [a, b, c] = a && b && c` -/ def and (bs : List Bool) : Bool := bs.all id /-- `O(min |xs| |ys|)`. Applies `f` to the two lists in parallel, stopping at the shorter list. * `zipWith f [x₁, x₂, x₃] [y₁, y₂, y₃, y₄] = [f x₁ y₁, f x₂ y₂, f x₃ y₃]` -/ @[specialize] def zipWith (f : α → β → γ) : (xs : List α) → (ys : List β) → List γ | x::xs, y::ys => f x y :: zipWith f xs ys | _, _ => [] /-- `O(min |xs| |ys|)`. Combines the two lists into a list of pairs, with one element from each list. The longer list is truncated to match the shorter list. * `zip [x₁, x₂, x₃] [y₁, y₂, y₃, y₄] = [(x₁, y₁), (x₂, y₂), (x₃, y₃)]` -/ def zip : List α → List β → List (Prod α β) := zipWith Prod.mk /-- `O(|l|)`. Separates a list of pairs into two lists containing the first components and second components. * `unzip [(x₁, y₁), (x₂, y₂), (x₃, y₃)] = ([x₁, x₂, x₃], [y₁, y₂, y₃])` -/ def unzip : List (α × β) → List α × List β | [] => ([], []) | (a, b) :: t => match unzip t with | (al, bl) => (a::al, b::bl) /-- `O(n)`. `range n` is the numbers from `0` to `n` exclusive, in increasing order. * `range 5 = [0, 1, 2, 3, 4]` -/ def range (n : Nat) : List Nat := loop n [] where loop : Nat → List Nat → List Nat | 0, ns => ns | n+1, ns => loop n (n::ns) /-- `O(n)`. `iota n` is the numbers from `1` to `n` inclusive, in decreasing order. * `iota 5 = [5, 4, 3, 2, 1]` -/ def iota : Nat → List Nat | 0 => [] | m@(n+1) => m :: iota n /-- Tail-recursive version of `iota`. -/ def iotaTR (n : Nat) : List Nat := let rec go : Nat → List Nat → List Nat | 0, r => r.reverse | m@(n+1), r => go n (m::r) go n [] @[csimp] theorem iota_eq_iotaTR : @iota = @iotaTR := have aux (n : Nat) (r : List Nat) : iotaTR.go n r = r.reverse ++ iota n := by induction n generalizing r with | zero => simp [iota, iotaTR.go] | succ n ih => simp [iota, iotaTR.go, ih, append_assoc] funext fun n => by simp [iotaTR, aux] /-- `O(|l|)`. `enumFrom n l` is like `enum` but it allows you to specify the initial index. * `enumFrom 5 [a, b, c] = [(5, a), (6, b), (7, c)]` -/ def enumFrom : Nat → List α → List (Nat × α) | _, [] => nil | n, x :: xs => (n, x) :: enumFrom (n + 1) xs /-- `O(|l|)`. `enum l` pairs up each element with its index in the list. * `enum [a, b, c] = [(0, a), (1, b), (2, c)]` -/ def enum : List α → List (Nat × α) := enumFrom 0 /-- `O(|l|)`. `intersperse sep l` alternates `sep` and the elements of `l`: * `intersperse sep [] = []` * `intersperse sep [a] = [a]` * `intersperse sep [a, b] = [a, sep, b]` * `intersperse sep [a, b, c] = [a, sep, b, sep, c]` -/ def intersperse (sep : α) : List α → List α | [] => [] | [x] => [x] | x::xs => x :: sep :: intersperse sep xs /-- `O(|xs|)`. `intercalate sep xs` alternates `sep` and the elements of `xs`: * `intercalate sep [] = []` * `intercalate sep [a] = a` * `intercalate sep [a, b] = a ++ sep ++ b` * `intercalate sep [a, b, c] = a ++ sep ++ b ++ sep ++ c` -/ def intercalate (sep : List α) (xs : List (List α)) : List α := join (intersperse sep xs) /-- `bind xs f` is the bind operation of the list monad. It applies `f` to each element of `xs` to get a list of lists, and then concatenates them all together. * `[2, 3, 2].bind range = [0, 1, 0, 1, 2, 0, 1]` -/ @[inline] protected def bind {α : Type u} {β : Type v} (a : List α) (b : α → List β) : List β := join (map b a) /-- `pure x = [x]` is the `pure` operation of the list monad. -/ @[inline] protected def pure {α : Type u} (a : α) : List α := [a] /-- The lexicographic order on lists. `[] < a::as`, and `a::as < b::bs` if `a < b` or if `a` and `b` are equivalent and `as < bs`. -/ inductive lt [LT α] : List α → List α → Prop where /-- `[]` is the smallest element in the order. -/ | nil (b : α) (bs : List α) : lt [] (b::bs) /-- If `a < b` then `a::as < b::bs`. -/ | head {a : α} (as : List α) {b : α} (bs : List α) : a < b → lt (a::as) (b::bs) /-- If `a` and `b` are equivalent and `as < bs`, then `a::as < b::bs`. -/ | tail {a : α} {as : List α} {b : α} {bs : List α} : ¬ a < b → ¬ b < a → lt as bs → lt (a::as) (b::bs) instance [LT α] : LT (List α) := ⟨List.lt⟩ instance hasDecidableLt [LT α] [h : DecidableRel (α:=α) (·<·)] : (l₁ l₂ : List α) → Decidable (l₁ < l₂) | [], [] => isFalse (fun h => nomatch h) | [], _::_ => isTrue (List.lt.nil _ _) | _::_, [] => isFalse (fun h => nomatch h) | a::as, b::bs => match h a b with | isTrue h₁ => isTrue (List.lt.head _ _ h₁) | isFalse h₁ => match h b a with | isTrue h₂ => isFalse (fun h => match h with | List.lt.head _ _ h₁' => absurd h₁' h₁ | List.lt.tail _ h₂' _ => absurd h₂ h₂') | isFalse h₂ => match hasDecidableLt as bs with | isTrue h₃ => isTrue (List.lt.tail h₁ h₂ h₃) | isFalse h₃ => isFalse (fun h => match h with | List.lt.head _ _ h₁' => absurd h₁' h₁ | List.lt.tail _ _ h₃' => absurd h₃' h₃) /-- The lexicographic order on lists. -/ @[reducible] protected def le [LT α] (a b : List α) : Prop := ¬ b < a instance [LT α] : LE (List α) := ⟨List.le⟩ instance [LT α] [DecidableRel ((· < ·) : α → α → Prop)] : (l₁ l₂ : List α) → Decidable (l₁ ≤ l₂) := fun _ _ => inferInstanceAs (Decidable (Not _)) /-- `isPrefixOf l₁ l₂` returns `true` Iff `l₁` is a prefix of `l₂`. That is, there exists a `t` such that `l₂ == l₁ ++ t`. -/ def isPrefixOf [BEq α] : List α → List α → Bool | [], _ => true | _, [] => false | a::as, b::bs => a == b && isPrefixOf as bs /-- `isPrefixOf? l₁ l₂` returns `some t` when `l₂ == l₁ ++ t`. -/ def isPrefixOf? [BEq α] : List α → List α → Option (List α) | [], l₂ => some l₂ | _, [] => none | (x₁ :: l₁), (x₂ :: l₂) => if x₁ == x₂ then isPrefixOf? l₁ l₂ else none /-- `isSuffixOf l₁ l₂` returns `true` Iff `l₁` is a suffix of `l₂`. That is, there exists a `t` such that `l₂ == t ++ l₁`. -/ def isSuffixOf [BEq α] (l₁ l₂ : List α) : Bool := isPrefixOf l₁.reverse l₂.reverse /-- `isSuffixOf? l₁ l₂` returns `some t` when `l₂ == t ++ l₁`.-/ def isSuffixOf? [BEq α] (l₁ l₂ : List α) : Option (List α) := Option.map List.reverse <| isPrefixOf? l₁.reverse l₂.reverse /-- `O(min |as| |bs|)`. Returns true if `as` and `bs` have the same length, and they are pairwise related by `eqv`. -/ @[specialize] def isEqv : (as bs : List α) → (eqv : α → α → Bool) → Bool | [], [], _ => true | a::as, b::bs, eqv => eqv a b && isEqv as bs eqv | _, _, _ => false /-- The equality relation on lists asserts that they have the same length and they are pairwise `BEq`. -/ protected def beq [BEq α] : List α → List α → Bool | [], [] => true | a::as, b::bs => a == b && List.beq as bs | _, _ => false instance [BEq α] : BEq (List α) := ⟨List.beq⟩ /-- `replicate n a` is `n` copies of `a`: * `replicate 5 a = [a, a, a, a, a]` -/ @[simp] def replicate : (n : Nat) → (a : α) → List α | 0, _ => [] | n+1, a => a :: replicate n a /-- Tail-recursive version of `List.replicate`. -/ def replicateTR {α : Type u} (n : Nat) (a : α) : List α := let rec loop : Nat → List α → List α | 0, as => as | n+1, as => loop n (a::as) loop n [] theorem replicateTR_loop_replicate_eq (a : α) (m n : Nat) : replicateTR.loop a n (replicate m a) = replicate (n + m) a := by induction n generalizing m with simp [replicateTR.loop] | succ n ih => simp [Nat.succ_add]; exact ih (m+1) @[csimp] theorem replicate_eq_replicateTR : @List.replicate = @List.replicateTR := by apply funext; intro α; apply funext; intro n; apply funext; intro a exact (replicateTR_loop_replicate_eq _ 0 n).symm /-- Removes the last element of the list. * `dropLast [] = []` * `dropLast [a] = []` * `dropLast [a, b, c] = [a, b]` -/ def dropLast {α} : List α → List α | [] => [] | [_] => [] | a::as => a :: dropLast as @[simp] theorem length_replicate (n : Nat) (a : α) : (replicate n a).length = n := by induction n <;> simp_all @[simp] theorem length_concat (as : List α) (a : α) : (concat as a).length = as.length + 1 := by induction as with | nil => rfl | cons _ xs ih => simp [concat, ih] @[simp] theorem length_set (as : List α) (i : Nat) (a : α) : (as.set i a).length = as.length := by induction as generalizing i with | nil => rfl | cons x xs ih => cases i with | zero => rfl | succ i => simp [set, ih] @[simp] theorem length_dropLast_cons (a : α) (as : List α) : (a :: as).dropLast.length = as.length := by match as with | [] => rfl | b::bs => have ih := length_dropLast_cons b bs simp[dropLast, ih] @[simp] theorem length_append (as bs : List α) : (as ++ bs).length = as.length + bs.length := by induction as with | nil => simp | cons _ as ih => simp [ih, Nat.succ_add] @[simp] theorem length_map (as : List α) (f : α → β) : (as.map f).length = as.length := by induction as with | nil => simp [List.map] | cons _ as ih => simp [List.map, ih] @[simp] theorem length_reverse (as : List α) : (as.reverse).length = as.length := by induction as with | nil => rfl | cons a as ih => simp [ih] /-- Returns the largest element of the list, if it is not empty. * `[].maximum? = none` * `[4].maximum? = some 4` * `[1, 4, 2, 10, 6].maximum? = some 10` -/ def maximum? [Max α] : List α → Option α | [] => none | a::as => some <| as.foldl max a /-- Returns the smallest element of the list, if it is not empty. * `[].minimum? = none` * `[4].minimum? = some 4` * `[1, 4, 2, 10, 6].minimum? = some 1` -/ def minimum? [Min α] : List α → Option α | [] => none | a::as => some <| as.foldl min a instance [BEq α] [LawfulBEq α] : LawfulBEq (List α) where eq_of_beq {as bs} := by induction as generalizing bs with | nil => intro h; cases bs <;> first | rfl | contradiction | cons a as ih => cases bs with | nil => intro h; contradiction | cons b bs => simp [show (a::as == b::bs) = (a == b && as == bs) from rfl] intro ⟨h₁, h₂⟩ exact ⟨h₁, ih h₂⟩ rfl {as} := by induction as with | nil => rfl | cons a as ih => simp [BEq.beq, List.beq, LawfulBEq.rfl]; exact ih theorem of_concat_eq_concat {as bs : List α} {a b : α} (h : as.concat a = bs.concat b) : as = bs ∧ a = b := by match as, bs with | [], [] => simp [concat] at h; simp [h] | [_], [] => simp [concat] at h | _::_::_, [] => simp [concat] at h | [], [_] => simp [concat] at h | [], _::_::_ => simp [concat] at h | _::_, _::_ => simp [concat] at h; simp [h]; apply of_concat_eq_concat h.2 theorem concat_eq_append (as : List α) (a : α) : as.concat a = as ++ [a] := by induction as <;> simp [concat, *] theorem drop_eq_nil_of_le {as : List α} {i : Nat} (h : as.length ≤ i) : as.drop i = [] := by match as, i with | [], i => simp | _::_, 0 => simp at h | _::as, i+1 => simp at h; exact @drop_eq_nil_of_le as i (Nat.le_of_succ_le_succ h) end List
85b68b8386758e273d05138ab4d8ee23879b721d
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/algebra/tower.lean
fdbf486098571697e40bf272fa5a11941cb702d4
[ "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
14,025
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.algebra.subalgebra.basic import algebra.algebra.bilinear /-! # Towers of algebras In this file we prove basic facts about towers of algebra. An algebra tower A/S/R is expressed by having instances of `algebra A S`, `algebra R S`, `algebra R A` and `is_scalar_tower R S A`, the later asserting the compatibility condition `(r • s) • a = r • (s • a)`. An important definition is `to_alg_hom R S A`, the canonical `R`-algebra homomorphism `S →ₐ[R] A`. -/ open_locale pointwise universes u v w u₁ v₁ variables (R : Type u) (S : Type v) (A : Type w) (B : Type u₁) (M : Type v₁) namespace algebra variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M] variables {A} /-- The `R`-algebra morphism `A → End (M)` corresponding to the representation of the algebra `A` on the `R`-module `M`. This is a stronger version of `distrib_mul_action.to_linear_map`, and could also have been called `algebra.to_module_End`. -/ def lsmul : A →ₐ[R] module.End R M := { to_fun := distrib_mul_action.to_linear_map R M, map_one' := linear_map.ext $ λ _, one_smul A _, map_mul' := λ a b, linear_map.ext $ smul_assoc a b, map_zero' := linear_map.ext $ λ _, zero_smul A _, map_add' := λ a b, linear_map.ext $ λ _, add_smul _ _ _, commutes' := λ r, linear_map.ext $ algebra_map_smul A r, } @[simp] lemma lsmul_coe (a : A) : (lsmul R M a : M → M) = (•) a := rfl lemma lmul_algebra_map (x : R) : lmul R A (algebra_map R A x) = algebra.lsmul R A x := eq.symm $ linear_map.ext $ smul_def x end algebra namespace is_scalar_tower section module variables [comm_semiring R] [semiring A] [algebra R A] variables [has_scalar R M] [mul_action A M] [is_scalar_tower R A M] variables {R} (A) {M} theorem algebra_map_smul (r : R) (x : M) : algebra_map R A r • x = r • x := by rw [algebra.algebra_map_eq_smul_one, smul_assoc, one_smul] end module section semiring variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] variables {R S A} theorem of_algebra_map_eq [algebra R A] (h : ∀ x, algebra_map R A x = algebra_map S A (algebra_map R S x)) : is_scalar_tower R S A := ⟨λ x y z, by simp_rw [algebra.smul_def, ring_hom.map_mul, mul_assoc, h]⟩ /-- See note [partially-applied ext lemmas]. -/ theorem of_algebra_map_eq' [algebra R A] (h : algebra_map R A = (algebra_map S A).comp (algebra_map R S)) : is_scalar_tower R S A := of_algebra_map_eq $ ring_hom.ext_iff.1 h variables (R S A) instance subalgebra (S₀ : subalgebra R S) : is_scalar_tower S₀ S A := of_algebra_map_eq $ λ x, rfl variables [algebra R A] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] theorem algebra_map_eq : algebra_map R A = (algebra_map S A).comp (algebra_map R S) := ring_hom.ext $ λ x, by simp_rw [ring_hom.comp_apply, algebra.algebra_map_eq_smul_one, smul_assoc, one_smul] theorem algebra_map_apply (x : R) : algebra_map R A x = algebra_map S A (algebra_map R S x) := by rw [algebra_map_eq R S A, ring_hom.comp_apply] instance subalgebra' (S₀ : subalgebra R S) : is_scalar_tower R S₀ A := @is_scalar_tower.of_algebra_map_eq R S₀ A _ _ _ _ _ _ $ λ _, (is_scalar_tower.algebra_map_apply R S A _ : _) @[ext] lemma algebra.ext {S : Type u} {A : Type v} [comm_semiring S] [semiring A] (h1 h2 : algebra S A) (h : ∀ (r : S) (x : A), (by haveI := h1; exact r • x) = r • x) : h1 = h2 := algebra.algebra_ext _ _ $ λ r, by simpa only [@algebra.smul_def _ _ _ _ h1, @algebra.smul_def _ _ _ _ h2, mul_one] using h r 1 /-- In a tower, the canonical map from the middle element to the top element is an algebra homomorphism over the bottom element. -/ def to_alg_hom : S →ₐ[R] A := { commutes' := λ _, (algebra_map_apply _ _ _ _).symm, .. algebra_map S A } lemma to_alg_hom_apply (y : S) : to_alg_hom R S A y = algebra_map S A y := rfl @[simp] lemma coe_to_alg_hom : ↑(to_alg_hom R S A) = algebra_map S A := ring_hom.ext $ λ _, rfl @[simp] lemma coe_to_alg_hom' : (to_alg_hom R S A : S → A) = algebra_map S A := rfl variables {R S A B} @[simp, priority 900] lemma _root_.alg_hom.map_algebra_map (f : A →ₐ[S] B) (r : R) : f (algebra_map R A r) = algebra_map R B r := by rw [algebra_map_apply R S A r, f.commutes, ← algebra_map_apply R S B] variables (R) @[simp, priority 900] lemma _root_.alg_hom.comp_algebra_map_of_tower (f : A →ₐ[S] B) : (f : A →+* B).comp (algebra_map R A) = algebra_map R B := ring_hom.ext f.map_algebra_map variables (R) {S A B} -- conflicts with is_scalar_tower.subalgebra @[priority 999] instance subsemiring (U : subsemiring S) : is_scalar_tower U S A := of_algebra_map_eq $ λ x, rfl @[nolint instance_priority] instance of_ring_hom {R A B : Type*} [comm_semiring R] [comm_semiring A] [comm_semiring B] [algebra R A] [algebra R B] (f : A →ₐ[R] B) : @is_scalar_tower R A B _ (f.to_ring_hom.to_algebra.to_has_scalar) _ := by { letI := (f : A →+* B).to_algebra, exact of_algebra_map_eq (λ x, (f.commutes x).symm) } end semiring end is_scalar_tower section homs variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] variables [algebra R A] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] variables (R) {A S B} open is_scalar_tower namespace alg_hom /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ def restrict_scalars (f : A →ₐ[S] B) : A →ₐ[R] B := { commutes' := λ r, by { rw [algebra_map_apply R S A, algebra_map_apply R S B], exact f.commutes (algebra_map R S r) }, .. (f : A →+* B) } lemma restrict_scalars_apply (f : A →ₐ[S] B) (x : A) : f.restrict_scalars R x = f x := rfl @[simp] lemma coe_restrict_scalars (f : A →ₐ[S] B) : (f.restrict_scalars R : A →+* B) = f := rfl @[simp] lemma coe_restrict_scalars' (f : A →ₐ[S] B) : (restrict_scalars R f : A → B) = f := rfl lemma restrict_scalars_injective : function.injective (restrict_scalars R : (A →ₐ[S] B) → (A →ₐ[R] B)) := λ f g h, alg_hom.ext (alg_hom.congr_fun h : _) end alg_hom namespace alg_equiv /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ def restrict_scalars (f : A ≃ₐ[S] B) : A ≃ₐ[R] B := { commutes' := λ r, by { rw [algebra_map_apply R S A, algebra_map_apply R S B], exact f.commutes (algebra_map R S r) }, .. (f : A ≃+* B) } lemma restrict_scalars_apply (f : A ≃ₐ[S] B) (x : A) : f.restrict_scalars R x = f x := rfl @[simp] lemma coe_restrict_scalars (f : A ≃ₐ[S] B) : (f.restrict_scalars R : A ≃+* B) = f := rfl @[simp] lemma coe_restrict_scalars' (f : A ≃ₐ[S] B) : (restrict_scalars R f : A → B) = f := rfl lemma restrict_scalars_injective : function.injective (restrict_scalars R : (A ≃ₐ[S] B) → (A ≃ₐ[R] B)) := λ f g h, alg_equiv.ext (alg_equiv.congr_fun h : _) end alg_equiv end homs namespace subalgebra open is_scalar_tower section semiring variables (R) {S A B} [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra R A] [algebra S B] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] /-- Given a scalar tower `R`, `S`, `A` of algebras, reinterpret an `S`-subalgebra of `A` an as an `R`-subalgebra. -/ def restrict_scalars (U : subalgebra S A) : subalgebra R A := { algebra_map_mem' := λ x, by { rw algebra_map_apply R S A, exact U.algebra_map_mem _ }, .. U } @[simp] lemma coe_restrict_scalars {U : subalgebra S A} : (restrict_scalars R U : set A) = (U : set A) := rfl @[simp] lemma restrict_scalars_top : restrict_scalars R (⊤ : subalgebra S A) = ⊤ := set_like.coe_injective rfl @[simp] lemma restrict_scalars_to_submodule {U : subalgebra S A} : (U.restrict_scalars R).to_submodule = U.to_submodule.restrict_scalars R := set_like.coe_injective rfl @[simp] lemma mem_restrict_scalars {U : subalgebra S A} {x : A} : x ∈ restrict_scalars R U ↔ x ∈ U := iff.rfl lemma restrict_scalars_injective : function.injective (restrict_scalars R : subalgebra S A → subalgebra R A) := λ U V H, ext $ λ x, by rw [← mem_restrict_scalars R, H, mem_restrict_scalars] /-- Produces an `R`-algebra map from `U.restrict_scalars R` given an `S`-algebra map from `U`. This is a special case of `alg_hom.restrict_scalars` that can be helpful in elaboration. -/ @[simp] def of_restrict_scalars (U : subalgebra S A) (f : U →ₐ[S] B) : U.restrict_scalars R →ₐ[R] B := f.restrict_scalars R end semiring end subalgebra namespace algebra variables {R A} [comm_semiring R] [semiring A] [algebra R A] variables {M} [add_comm_monoid M] [module A M] [module R M] [is_scalar_tower R A M] lemma span_restrict_scalars_eq_span_of_surjective (h : function.surjective (algebra_map R A)) (s : set M) : (submodule.span A s).restrict_scalars R = submodule.span R s := begin refine le_antisymm (λ x hx, _) (submodule.span_subset_span _ _ _), refine submodule.span_induction hx _ _ _ _, { exact λ x hx, submodule.subset_span hx }, { exact submodule.zero_mem _ }, { exact λ x y, submodule.add_mem _ }, { intros c x hx, obtain ⟨c', rfl⟩ := h c, rw is_scalar_tower.algebra_map_smul, exact submodule.smul_mem _ _ hx }, end lemma coe_span_eq_span_of_surjective (h : function.surjective (algebra_map R A)) (s : set M) : (submodule.span A s : set M) = submodule.span R s := congr_arg coe (algebra.span_restrict_scalars_eq_span_of_surjective h s) end algebra namespace is_scalar_tower open subalgebra variables [comm_semiring R] [comm_semiring S] [comm_semiring A] variables [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] theorem adjoin_range_to_alg_hom (t : set A) : (algebra.adjoin (to_alg_hom R S A).range t).restrict_scalars R = (algebra.adjoin S t).restrict_scalars R := subalgebra.ext $ λ z, show z ∈ subsemiring.closure (set.range (algebra_map (to_alg_hom R S A).range A) ∪ t : set A) ↔ z ∈ subsemiring.closure (set.range (algebra_map S A) ∪ t : set A), from suffices set.range (algebra_map (to_alg_hom R S A).range A) = set.range (algebra_map S A), by rw this, by { ext z, exact ⟨λ ⟨⟨x, y, h1⟩, h2⟩, ⟨y, h2 ▸ h1⟩, λ ⟨y, hy⟩, ⟨⟨z, y, hy⟩, rfl⟩⟩ } end is_scalar_tower section semiring variables {R S A} namespace submodule section module variables [semiring R] [semiring S] [add_comm_monoid A] variables [module R S] [module S A] [module R A] [is_scalar_tower R S A] open is_scalar_tower theorem smul_mem_span_smul_of_mem {s : set S} {t : set A} {k : S} (hks : k ∈ span R s) {x : A} (hx : x ∈ t) : k • x ∈ span R (s • t) := span_induction hks (λ c hc, subset_span $ set.mem_smul.2 ⟨c, x, hc, hx, rfl⟩) (by { rw zero_smul, exact zero_mem _ }) (λ c₁ c₂ ih₁ ih₂, by { rw add_smul, exact add_mem ih₁ ih₂ }) (λ b c hc, by { rw is_scalar_tower.smul_assoc, exact smul_mem _ _ hc }) variables [smul_comm_class R S A] theorem smul_mem_span_smul {s : set S} (hs : span R s = ⊤) {t : set A} {k : S} {x : A} (hx : x ∈ span R t) : k • x ∈ span R (s • t) := span_induction hx (λ x hx, smul_mem_span_smul_of_mem (hs.symm ▸ mem_top) hx) (by { rw smul_zero, exact zero_mem _ }) (λ x y ihx ihy, by { rw smul_add, exact add_mem ihx ihy }) (λ c x hx, smul_comm c k x ▸ smul_mem _ _ hx) theorem smul_mem_span_smul' {s : set S} (hs : span R s = ⊤) {t : set A} {k : S} {x : A} (hx : x ∈ span R (s • t)) : k • x ∈ span R (s • t) := span_induction hx (λ x hx, let ⟨p, q, hp, hq, hpq⟩ := set.mem_smul.1 hx in by { rw [← hpq, smul_smul], exact smul_mem_span_smul_of_mem (hs.symm ▸ mem_top) hq }) (by { rw smul_zero, exact zero_mem _ }) (λ x y ihx ihy, by { rw smul_add, exact add_mem ihx ihy }) (λ c x hx, smul_comm c k x ▸ smul_mem _ _ hx) theorem span_smul {s : set S} (hs : span R s = ⊤) (t : set A) : span R (s • t) = (span S t).restrict_scalars R := le_antisymm (span_le.2 $ λ x hx, let ⟨p, q, hps, hqt, hpqx⟩ := set.mem_smul.1 hx in hpqx ▸ (span S t).smul_mem p (subset_span hqt)) $ λ p hp, span_induction hp (λ x hx, one_smul S x ▸ smul_mem_span_smul hs (subset_span hx)) (zero_mem _) (λ _ _, add_mem) (λ k x hx, smul_mem_span_smul' hs hx) end module section algebra variables [comm_semiring R] [semiring S] [add_comm_monoid A] variables [algebra R S] [module S A] [module R A] [is_scalar_tower R S A] /-- A variant of `submodule.span_image` for `algebra_map`. -/ lemma span_algebra_map_image (a : set R) : submodule.span R (algebra_map R S '' a) = (submodule.span R a).map (algebra.linear_map R S) := (submodule.span_image $ algebra.linear_map R S).trans rfl lemma span_algebra_map_image_of_tower {S T : Type*} [comm_semiring S] [semiring T] [module R S] [is_scalar_tower R S S] [algebra R T] [algebra S T] [is_scalar_tower R S T] (a : set S) : submodule.span R (algebra_map S T '' a) = (submodule.span R a).map ((algebra.linear_map S T).restrict_scalars R) := (submodule.span_image $ (algebra.linear_map S T).restrict_scalars R).trans rfl lemma map_mem_span_algebra_map_image {S T : Type*} [comm_semiring S] [semiring T] [algebra R S] [algebra R T] [algebra S T] [is_scalar_tower R S T] (x : S) (a : set S) (hx : x ∈ submodule.span R a) : algebra_map S T x ∈ submodule.span R (algebra_map S T '' a) := by { rw [span_algebra_map_image_of_tower, mem_map], exact ⟨x, hx, rfl⟩ } end algebra end submodule end semiring section ring namespace algebra variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_group M] [module A M] [module R M] [is_scalar_tower R A M] lemma lsmul_injective [no_zero_smul_divisors A M] {x : A} (hx : x ≠ 0) : function.injective (lsmul R M x) := smul_right_injective _ hx end algebra end ring
9b4625063bcdadc64e7d710712bacdfb19df14e4
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/inner_product_space/euclidean_dist.lean
2fd3110be79a76d1fb573f884a00f18de4cdd441
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,704
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.inner_product_space.calculus import analysis.inner_product_space.pi_L2 /-! # Euclidean distance on a finite dimensional space When we define a smooth bump function on a normed space, it is useful to have a smooth distance on the space. Since the default distance is not guaranteed to be smooth, we define `to_euclidean` to be an equivalence between a finite dimensional normed space and the standard Euclidean space of the same dimension. Then we define `euclidean.dist x y = dist (to_euclidean x) (to_euclidean y)` and provide some definitions (`euclidean.ball`, `euclidean.closed_ball`) and simple lemmas about this distance. This way we hide the usage of `to_euclidean` behind an API. -/ open_locale topological_space open set variables {E : Type*} [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] noncomputable theory /-- If `E` is a finite dimensional space over `ℝ`, then `to_euclidean` is a continuous `ℝ`-linear equivalence between `E` and the Euclidean space of the same dimension. -/ def to_euclidean : E ≃L[ℝ] euclidean_space ℝ (fin $ finite_dimensional.finrank ℝ E) := continuous_linear_equiv.of_finrank_eq finrank_euclidean_space_fin.symm namespace euclidean /-- If `x` and `y` are two points in a finite dimensional space over `ℝ`, then `euclidean.dist x y` is the distance between these points in the metric defined by some inner product space structure on `E`. -/ def dist (x y : E) : ℝ := dist (to_euclidean x) (to_euclidean y) /-- Closed ball w.r.t. the euclidean distance. -/ def closed_ball (x : E) (r : ℝ) : set E := {y | dist y x ≤ r} /-- Open ball w.r.t. the euclidean distance. -/ def ball (x : E) (r : ℝ) : set E := {y | dist y x < r} lemma ball_eq_preimage (x : E) (r : ℝ) : ball x r = to_euclidean ⁻¹' (metric.ball (to_euclidean x) r) := rfl lemma closed_ball_eq_preimage (x : E) (r : ℝ) : closed_ball x r = to_euclidean ⁻¹' (metric.closed_ball (to_euclidean x) r) := rfl lemma ball_subset_closed_ball {x : E} {r : ℝ} : ball x r ⊆ closed_ball x r := λ y (hy : _ < _), le_of_lt hy lemma is_open_ball {x : E} {r : ℝ} : is_open (ball x r) := metric.is_open_ball.preimage to_euclidean.continuous lemma mem_ball_self {x : E} {r : ℝ} (hr : 0 < r) : x ∈ ball x r := metric.mem_ball_self hr lemma closed_ball_eq_image (x : E) (r : ℝ) : closed_ball x r = to_euclidean.symm '' metric.closed_ball (to_euclidean x) r := by rw [to_euclidean.image_symm_eq_preimage, closed_ball_eq_preimage] lemma is_compact_closed_ball {x : E} {r : ℝ} : is_compact (closed_ball x r) := begin rw closed_ball_eq_image, exact (proper_space.is_compact_closed_ball _ _).image to_euclidean.symm.continuous end lemma is_closed_closed_ball {x : E} {r : ℝ} : is_closed (closed_ball x r) := is_compact_closed_ball.is_closed lemma closure_ball (x : E) {r : ℝ} (h : 0 < r) : closure (ball x r) = closed_ball x r := by rw [ball_eq_preimage, ← to_euclidean.preimage_closure, closure_ball (to_euclidean x) h, closed_ball_eq_preimage] lemma exists_pos_lt_subset_ball {R : ℝ} {s : set E} {x : E} (hR : 0 < R) (hs : is_closed s) (h : s ⊆ ball x R) : ∃ r ∈ Ioo 0 R, s ⊆ ball x r := begin rw [ball_eq_preimage, ← image_subset_iff] at h, rcases exists_pos_lt_subset_ball hR (to_euclidean.is_closed_image.2 hs) h with ⟨r, hr, hsr⟩, exact ⟨r, hr, image_subset_iff.1 hsr⟩ end lemma nhds_basis_closed_ball {x : E} : (𝓝 x).has_basis (λ r : ℝ, 0 < r) (closed_ball x) := begin rw [to_euclidean.to_homeomorph.nhds_eq_comap], exact metric.nhds_basis_closed_ball.comap _ end lemma closed_ball_mem_nhds {x : E} {r : ℝ} (hr : 0 < r) : closed_ball x r ∈ 𝓝 x := nhds_basis_closed_ball.mem_of_mem hr lemma nhds_basis_ball {x : E} : (𝓝 x).has_basis (λ r : ℝ, 0 < r) (ball x) := begin rw [to_euclidean.to_homeomorph.nhds_eq_comap], exact metric.nhds_basis_ball.comap _ end lemma ball_mem_nhds {x : E} {r : ℝ} (hr : 0 < r) : ball x r ∈ 𝓝 x := nhds_basis_ball.mem_of_mem hr end euclidean variables {F : Type*} [normed_group F] [normed_space ℝ F] {f g : F → E} {n : with_top ℕ} lemma times_cont_diff.euclidean_dist (hf : times_cont_diff ℝ n f) (hg : times_cont_diff ℝ n g) (h : ∀ x, f x ≠ g x) : times_cont_diff ℝ n (λ x, euclidean.dist (f x) (g x)) := begin simp only [euclidean.dist], apply @times_cont_diff.dist ℝ, exacts [(@to_euclidean E _ _ _).times_cont_diff.comp hf, (@to_euclidean E _ _ _).times_cont_diff.comp hg, λ x, to_euclidean.injective.ne (h x)] end
7ed1cf93c544d07f0a8bbb64fd650d6bf725993b
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/field_theory/perfect_closure.lean
cde76f595d70f8a32532940f70a5e1ab1bd5f7d7
[ "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
17,004
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import algebra.char_p data.equiv.ring /-! # The perfect closure of a field -/ universes u v /-- A perfect field is a field of characteristic p that has p-th root. -/ class perfect_field (α : Type u) [field α] (p : ℕ) [char_p α p] [nat.prime p] : Type u := (pth_root' : α → α) (frobenius_pth_root' : ∀ x, frobenius α p (pth_root' x) = x) /-- Frobenius automorphism of a perfect field. -/ def frobenius_equiv (α : Type u) [field α] (p : ℕ) [char_p α p] [nat.prime p] [perfect_field α p] : α ≃+* α := { inv_fun := perfect_field.pth_root' p, left_inv := λ x, frobenius_inj α p $ perfect_field.frobenius_pth_root' _, right_inv := perfect_field.frobenius_pth_root', .. frobenius α p } /-- `p`-th root of a number in a `perfect_field` as a `ring_hom`. -/ def pth_root (α : Type u) [field α] (p : ℕ) [nat.prime p] [char_p α p] [perfect_field α p] : α →+* α := (frobenius_equiv α p).symm.to_ring_hom section variables {α : Type u} [field α] {β : Type v} [field β] (f : α →* β) (g : α →+* β) {p : ℕ} [nat.prime p] [char_p α p] [perfect_field α p] [char_p β p] [perfect_field β p] @[simp] lemma coe_frobenius_equiv : ⇑(frobenius_equiv α p) = frobenius α p := rfl @[simp] lemma coe_frobenius_equiv_symm : ⇑(frobenius_equiv α p).symm = pth_root α p := rfl @[simp] theorem frobenius_pth_root (x : α) : frobenius α p (pth_root α p x) = x := (frobenius_equiv α p).apply_symm_apply x @[simp] theorem pth_root_frobenius (x : α) : pth_root α p (frobenius α p x) = x := (frobenius_equiv α p).symm_apply_apply x theorem eq_pth_root_iff {x y : α} : x = pth_root α p y ↔ frobenius α p x = y := (frobenius_equiv α p).to_equiv.eq_symm_apply theorem pth_root_eq_iff {x y : α} : pth_root α p x = y ↔ x = frobenius α p y := (frobenius_equiv α p).to_equiv.symm_apply_eq theorem monoid_hom.map_pth_root (x : α) : f (pth_root α p x) = pth_root β p (f x) := eq_pth_root_iff.2 $ by rw [← f.map_frobenius, frobenius_pth_root] theorem monoid_hom.map_iterate_pth_root (x : α) (n : ℕ) : f (pth_root α p^[n] x) = (pth_root β p^[n] (f x)) := (nat.iterate₁ $ λ x, (f.map_pth_root x).symm).symm theorem ring_hom.map_pth_root (x : α) : g (pth_root α p x) = pth_root β p (g x) := g.to_monoid_hom.map_pth_root x theorem ring_hom.map_iterate_pth_root (x : α) (n : ℕ) : g (pth_root α p^[n] x) = (pth_root β p^[n] (g x)) := g.to_monoid_hom.map_iterate_pth_root x n end section variables (α : Type u) [comm_ring α] (p : ℕ) [nat.prime p] [char_p α p] /-- `perfect_closure α p` is the quotient by this relation. -/ inductive perfect_closure.r : (ℕ × α) → (ℕ × α) → Prop | intro : ∀ n x, perfect_closure.r (n, x) (n+1, frobenius α p x) run_cmd tactic.mk_iff_of_inductive_prop `perfect_closure.r `perfect_closure.r_iff /-- The perfect closure is the smallest extension that makes frobenius surjective. -/ def perfect_closure : Type u := quot (perfect_closure.r α p) end namespace perfect_closure variables (α : Type u) section ring variables [comm_ring α] (p : ℕ) [nat.prime p] [char_p α p] /-- Constructor for `perfect_closure`. -/ def mk (x : ℕ × α) : perfect_closure α p := quot.mk (r α p) x @[simp] lemma quot_mk_eq_mk (x : ℕ × α) : (quot.mk (r α p) x : perfect_closure α p) = mk α p x := rfl variables {α p} /-- Lift a function `ℕ × α → β` to a function on `perfect_closure α p`. -/ @[elab_as_eliminator] def lift_on {β : Type*} (x : perfect_closure α p) (f : ℕ × α → β) (hf : ∀ x y, r α p x y → f x = f y) : β := quot.lift_on x f hf @[simp] lemma lift_on_mk {β : Sort*} (f : ℕ × α → β) (hf : ∀ x y, r α p x y → f x = f y) (x : ℕ × α) : (mk α p x).lift_on f hf = f x := rfl @[elab_as_eliminator] lemma induction_on (x : perfect_closure α p) {q : perfect_closure α p → Prop} (h : ∀ x, q (mk α p x)) : q x := quot.induction_on x h variables (α p) private lemma mul_aux_left (x1 x2 y : ℕ × α) (H : r α p x1 x2) : mk α p (x1.1 + y.1, ((frobenius α p)^[y.1] x1.2) * ((frobenius α p)^[x1.1] y.2)) = mk α p (x2.1 + y.1, ((frobenius α p)^[y.1] x2.2) * ((frobenius α p)^[x2.1] y.2)) := match x1, x2, H with | _, _, r.intro n x := quot.sound $ by rw [← nat.iterate_succ, nat.iterate_succ', nat.iterate_succ', ← frobenius_mul, nat.succ_add]; apply r.intro end private lemma mul_aux_right (x y1 y2 : ℕ × α) (H : r α p y1 y2) : mk α p (x.1 + y1.1, ((frobenius α p)^[y1.1] x.2) * ((frobenius α p)^[x.1] y1.2)) = mk α p (x.1 + y2.1, ((frobenius α p)^[y2.1] x.2) * ((frobenius α p)^[x.1] y2.2)) := match y1, y2, H with | _, _, r.intro n y := quot.sound $ by rw [← nat.iterate_succ, nat.iterate_succ', nat.iterate_succ', ← frobenius_mul]; apply r.intro end instance : has_mul (perfect_closure α p) := ⟨quot.lift (λ x:ℕ×α, quot.lift (λ y:ℕ×α, mk α p (x.1 + y.1, ((frobenius α p)^[y.1] x.2) * ((frobenius α p)^[x.1] y.2))) (mul_aux_right α p x)) (λ x1 x2 (H : r α p x1 x2), funext $ λ e, quot.induction_on e $ λ y, mul_aux_left α p x1 x2 y H)⟩ @[simp] lemma mk_mul_mk (x y : ℕ × α) : mk α p x * mk α p y = mk α p (x.1 + y.1, ((frobenius α p)^[y.1] x.2) * ((frobenius α p)^[x.1] y.2)) := rfl instance : comm_monoid (perfect_closure α p) := { mul_assoc := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, congr_arg (quot.mk _) $ by simp only [add_assoc, mul_assoc, nat.iterate₂ (frobenius_mul _), ← nat.iterate_add, add_comm, add_left_comm], one := mk α p (0, 1), one_mul := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [nat.iterate₀ (frobenius_one _), nat.iterate_zero, one_mul, zero_add]), mul_one := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [nat.iterate₀ (frobenius_one _), nat.iterate_zero, mul_one, add_zero]), mul_comm := λ e f, quot.induction_on e (λ ⟨m, x⟩, quot.induction_on f (λ ⟨n, y⟩, congr_arg (quot.mk _) $ by simp only [add_comm, mul_comm])), .. (infer_instance : has_mul (perfect_closure α p)) } lemma one_def : (1 : perfect_closure α p) = mk α p (0, 1) := rfl instance : inhabited (perfect_closure α p) := ⟨1⟩ private lemma add_aux_left (x1 x2 y : ℕ × α) (H : r α p x1 x2) : mk α p (x1.1 + y.1, ((frobenius α p)^[y.1] x1.2) + ((frobenius α p)^[x1.1] y.2)) = mk α p (x2.1 + y.1, ((frobenius α p)^[y.1] x2.2) + ((frobenius α p)^[x2.1] y.2)) := match x1, x2, H with | _, _, r.intro n x := quot.sound $ by rw [← nat.iterate_succ, nat.iterate_succ', nat.iterate_succ', ← frobenius_add, nat.succ_add]; apply r.intro end private lemma add_aux_right (x y1 y2 : ℕ × α) (H : r α p y1 y2) : mk α p (x.1 + y1.1, ((frobenius α p)^[y1.1] x.2) + ((frobenius α p)^[x.1] y1.2)) = mk α p (x.1 + y2.1, ((frobenius α p)^[y2.1] x.2) + ((frobenius α p)^[x.1] y2.2)) := match y1, y2, H with | _, _, r.intro n y := quot.sound $ by rw [← nat.iterate_succ, nat.iterate_succ', nat.iterate_succ', ← frobenius_add]; apply r.intro end instance : has_add (perfect_closure α p) := ⟨quot.lift (λ x:ℕ×α, quot.lift (λ y:ℕ×α, mk α p (x.1 + y.1, ((frobenius α p)^[y.1] x.2) + ((frobenius α p)^[x.1] y.2))) (add_aux_right α p x)) (λ x1 x2 (H : r α p x1 x2), funext $ λ e, quot.induction_on e $ λ y, add_aux_left α p x1 x2 y H)⟩ @[simp] lemma mk_add_mk (x y : ℕ × α) : mk α p x + mk α p y = mk α p (x.1 + y.1, ((frobenius α p)^[y.1] x.2) + ((frobenius α p)^[x.1] y.2)) := rfl instance : has_neg (perfect_closure α p) := ⟨quot.lift (λ x:ℕ×α, mk α p (x.1, -x.2)) (λ x y (H : r α p x y), match x, y, H with | _, _, r.intro n x := quot.sound $ by rw ← frobenius_neg; apply r.intro end)⟩ @[simp] lemma neg_mk (x : ℕ × α) : - mk α p x = mk α p (x.1, -x.2) := rfl instance : has_zero (perfect_closure α p) := ⟨mk α p (0, 0)⟩ lemma zero_def : (0 : perfect_closure α p) = mk α p (0, 0) := rfl theorem mk_zero (n : ℕ) : mk α p (n, 0) = 0 := by induction n with n ih; [refl, rw ← ih]; symmetry; apply quot.sound; have := r.intro n (0:α); rwa [frobenius_zero α p] at this theorem r.sound (m n : ℕ) (x y : α) (H : frobenius α p^[m] x = y) : mk α p (n, x) = mk α p (m + n, y) := by subst H; induction m with m ih; [simp only [zero_add, nat.iterate_zero], rw [ih, nat.succ_add, nat.iterate_succ']]; apply quot.sound; apply r.intro instance : comm_ring (perfect_closure α p) := { add_assoc := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, congr_arg (quot.mk _) $ by simp only [add_assoc, nat.iterate₂ (frobenius_add α p), (nat.iterate_add _ _ _ _).symm, add_comm, add_left_comm], zero := 0, zero_add := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [nat.iterate₀ (frobenius_zero α p), nat.iterate_zero, zero_add]), add_zero := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [nat.iterate₀ (frobenius_zero α p), nat.iterate_zero, add_zero]), add_left_neg := λ e, quot.induction_on e (λ ⟨n, x⟩, by simp only [quot_mk_eq_mk, neg_mk, mk_add_mk, nat.iterate₁ (frobenius_neg α p), add_left_neg, mk_zero]), add_comm := λ e f, quot.induction_on e (λ ⟨m, x⟩, quot.induction_on f (λ ⟨n, y⟩, congr_arg (quot.mk _) $ by simp only [add_comm])), left_distrib := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, show quot.mk _ _ = quot.mk _ _, by simp only [add_assoc, add_comm, add_left_comm]; apply r.sound; simp only [nat.iterate₂ (frobenius_mul p), nat.iterate₂ (frobenius_add α p), (nat.iterate_add _ _ _ _).symm, mul_add, add_comm, add_left_comm], right_distrib := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, show quot.mk _ _ = quot.mk _ _, by simp only [add_assoc, add_comm _ s, add_left_comm _ s]; apply r.sound; simp only [nat.iterate₂ (frobenius_mul p), nat.iterate₂ (frobenius_add α p), (nat.iterate_add _ _ _ _).symm, add_mul, add_comm, add_left_comm], .. (infer_instance : has_add (perfect_closure α p)), .. (infer_instance : has_neg (perfect_closure α p)), .. (infer_instance : comm_monoid (perfect_closure α p)) } theorem eq_iff' (x y : ℕ × α) : mk α p x = mk α p y ↔ ∃ z, (frobenius α p^[y.1 + z] x.2) = (frobenius α p^[x.1 + z] y.2) := begin split, { intro H, replace H := quot.exact _ H, induction H, case eqv_gen.rel : x y H { cases H with n x, exact ⟨0, rfl⟩ }, case eqv_gen.refl : H { exact ⟨0, rfl⟩ }, case eqv_gen.symm : x y H ih { cases ih with w ih, exact ⟨w, ih.symm⟩ }, case eqv_gen.trans : x y z H1 H2 ih1 ih2 { cases ih1 with z1 ih1, cases ih2 with z2 ih2, existsi z2+(y.1+z1), rw [← add_assoc, nat.iterate_add, ih1], rw [← nat.iterate_add, add_comm, nat.iterate_add, ih2], rw [← nat.iterate_add], simp only [add_comm, add_left_comm] } }, intro H, cases x with m x, cases y with n y, cases H with z H, dsimp only at H, rw [r.sound α p (n+z) m x _ rfl, r.sound α p (m+z) n y _ rfl, H], rw [add_assoc, add_comm, add_comm z] end theorem nat_cast (n x : ℕ) : (x : perfect_closure α p) = mk α p (n, x) := begin induction n with n ih, { induction x with x ih, {refl}, rw [nat.cast_succ, nat.cast_succ, ih], refl }, rw ih, apply quot.sound, conv {congr, skip, skip, rw ← frobenius_nat_cast α p x}, apply r.intro end theorem int_cast (x : ℤ) : (x : perfect_closure α p) = mk α p (0, x) := by induction x; simp only [int.cast_of_nat, int.cast_neg_succ_of_nat, nat_cast α p 0]; refl theorem nat_cast_eq_iff (x y : ℕ) : (x : perfect_closure α p) = y ↔ (x : α) = y := begin split; intro H, { rw [nat_cast α p 0, nat_cast α p 0, eq_iff'] at H, cases H with z H, simpa only [zero_add, nat.iterate₀ (frobenius_nat_cast α p _)] using H }, rw [nat_cast α p 0, nat_cast α p 0, H] end instance : char_p (perfect_closure α p) p := begin constructor, intro x, rw ← char_p.cast_eq_zero_iff α, rw [← nat.cast_zero, nat_cast_eq_iff, nat.cast_zero] end theorem frobenius_mk (x : ℕ × α) : (frobenius (perfect_closure α p) p : perfect_closure α p → perfect_closure α p) (mk α p x) = mk _ _ (x.1, x.2^p) := begin simp only [frobenius_def], cases x with n x, dsimp only, suffices : ∀ p':ℕ, mk α p (n, x) ^ p' = mk α p (n, x ^ p'), { apply this }, intro p, induction p with p ih, case nat.zero { apply r.sound, rw [(frobenius _ _).iterate_map_one, pow_zero] }, case nat.succ { rw [pow_succ, ih], symmetry, apply r.sound, simp only [pow_succ, (frobenius _ _).iterate_map_mul] } end /-- Embedding of `α` into `perfect_closure α p` -/ def of : α →+* perfect_closure α p := { to_fun := λ x, mk _ _ (0, x), map_one' := rfl, map_mul' := λ x y, rfl, map_zero' := rfl, map_add' := λ x y, rfl } lemma of_apply (x : α) : of α p x = mk _ _ (0, x) := rfl end ring theorem eq_iff [integral_domain α] (p : ℕ) [nat.prime p] [char_p α p] (x y : ℕ × α) : quot.mk (r α p) x = quot.mk (r α p) y ↔ (frobenius α p^[y.1] x.2) = (frobenius α p^[x.1] y.2) := (eq_iff' α p x y).trans ⟨λ ⟨z, H⟩, nat.iterate_inj (frobenius_inj α p) z _ _ $ by simpa only [add_comm, nat.iterate_add] using H, λ H, ⟨0, H⟩⟩ section field variables [field α] (p : ℕ) [nat.prime p] [char_p α p] instance : has_inv (perfect_closure α p) := ⟨quot.lift (λ x:ℕ×α, quot.mk (r α p) (x.1, x.2⁻¹)) (λ x y (H : r α p x y), match x, y, H with | _, _, r.intro n x := quot.sound $ by simp only [frobenius_def]; rw ← inv_pow'; apply r.intro end)⟩ instance : field (perfect_closure α p) := { zero_ne_one := λ H, zero_ne_one ((eq_iff _ _ _ _).1 H), mul_inv_cancel := λ e, induction_on e $ λ ⟨m, x⟩ H, have _ := mt (eq_iff _ _ _ _).2 H, (eq_iff _ _ _ _).2 (by simp only [(frobenius _ _).iterate_map_one, (frobenius α p).iterate_map_zero, nat.iterate_zero, ← (frobenius _ p).iterate_map_mul] at this ⊢; rw [mul_inv_cancel this, (frobenius _ _).iterate_map_one]), inv_zero := congr_arg (quot.mk (r α p)) (by rw [inv_zero]), .. (infer_instance : has_inv (perfect_closure α p)), .. (infer_instance : comm_ring (perfect_closure α p)) } instance : perfect_field (perfect_closure α p) p := { pth_root' := λ e, lift_on e (λ x, mk α p (x.1 + 1, x.2)) (λ x y H, match x, y, H with | _, _, r.intro n x := quot.sound (r.intro _ _) end), frobenius_pth_root' := λ e, induction_on e (λ ⟨n, x⟩, by { simp only [lift_on_mk, frobenius_mk], exact (quot.sound $ r.intro _ _).symm }) } theorem eq_pth_root (x : ℕ × α) : mk α p x = (pth_root (perfect_closure α p) p^[x.1] (of α p x.2)) := begin rcases x with ⟨m, x⟩, induction m with m ih, {refl}, rw [nat.iterate_succ', ← ih]; refl end /-- Given a field `α` of characteristic `p` and a perfect field `β` of the same characteristic, any homomorphism `α →+* β` can be lifted to `perfect_closure α p`. -/ def lift (β : Type v) [field β] [char_p β p] [perfect_field β p] : (α →+* β) ≃ (perfect_closure α p →+* β) := begin refine_struct { .. }, field to_fun { intro f, refine_struct { .. }, field to_fun { refine λ e, lift_on e (λ x, pth_root β p^[x.1] (f x.2)) _, rintro a b ⟨n⟩, simp only [f.map_frobenius, nat.iterate_succ, pth_root_frobenius] }, field map_one' { exact f.map_one }, field map_zero' { exact f.map_zero }, field map_mul' { rintro ⟨x⟩ ⟨y⟩, simp only [quot_mk_eq_mk, lift_on_mk, mk_mul_mk, ring_hom.map_iterate_frobenius, ring_hom.iterate_map_mul, ring_hom.map_mul], rw [nat.iterate_add, nat.iterate_cancel, add_comm, nat.iterate_add, nat.iterate_cancel]; exact pth_root_frobenius }, field map_add' { rintro ⟨x⟩ ⟨y⟩, simp only [quot_mk_eq_mk, lift_on_mk, mk_add_mk, ring_hom.map_iterate_frobenius, ring_hom.iterate_map_add, ring_hom.map_add], rw [nat.iterate_add, nat.iterate_cancel, add_comm x.1, nat.iterate_add, nat.iterate_cancel]; exact pth_root_frobenius } }, field inv_fun { exact λ f, f.comp (of α p) }, field left_inv { intro f, ext x, refl }, field right_inv { intro f, ext ⟨x⟩, simp only [ring_hom.coe_mk, quot_mk_eq_mk, ring_hom.comp_apply, lift_on_mk], rw [eq_pth_root, ring_hom.map_iterate_pth_root] } end end field end perfect_closure
aa27daa44694f34ea61c119aade3adcf9e15285a
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/array/lemmas.lean
6a05b8ca6d2f3c8b0889c1904061113425650169
[ "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
9,450
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.list.basic category.traversable.equiv data.vector2 universes u w namespace d_array variables {n : ℕ} {α : fin n → Type u} instance [∀ i, inhabited (α i)] : inhabited (d_array n α) := ⟨⟨λ _, default _⟩⟩ end d_array namespace array instance {n α} [inhabited α] : inhabited (array n α) := d_array.inhabited theorem to_list_of_heq {n₁ n₂ α} {a₁ : array n₁ α} {a₂ : array n₂ α} (hn : n₁ = n₂) (ha : a₁ == a₂) : a₁.to_list = a₂.to_list := by congr; assumption /- rev_list -/ section rev_list variables {n : ℕ} {α : Type u} {a : array n α} theorem rev_list_reverse_aux : ∀ i (h : i ≤ n) (t : list α), (a.iterate_aux (λ _, (::)) i h []).reverse_core t = a.rev_iterate_aux (λ _, (::)) i h t | 0 h t := rfl | (i+1) h t := rev_list_reverse_aux i _ _ @[simp] theorem rev_list_reverse : a.rev_list.reverse = a.to_list := rev_list_reverse_aux _ _ _ @[simp] theorem to_list_reverse : a.to_list.reverse = a.rev_list := by rw [←rev_list_reverse, list.reverse_reverse] end rev_list /- mem -/ section mem variables {n : ℕ} {α : Type u} {v : α} {a : array n α} theorem mem.def : v ∈ a ↔ ∃ i, a.read i = v := iff.rfl theorem mem_rev_list_aux : ∀ {i} (h : i ≤ n), (∃ (j : fin n), j.1 < i ∧ read a j = v) ↔ v ∈ a.iterate_aux (λ _, (::)) i h [] | 0 _ := ⟨λ ⟨i, n, _⟩, absurd n i.val.not_lt_zero, false.elim⟩ | (i+1) h := let IH := mem_rev_list_aux (le_of_lt h) in ⟨λ ⟨j, ji1, e⟩, or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ ji1) (λ ji, list.mem_cons_of_mem _ $ IH.1 ⟨j, ji, e⟩) (λ je, by simp [d_array.iterate_aux]; apply or.inl; unfold read at e; have H : j = ⟨i, h⟩ := fin.eq_of_veq je; rwa [←H, e]), λ m, begin simp [d_array.iterate_aux, list.mem] at m, cases m with e m', exact ⟨⟨i, h⟩, nat.lt_succ_self _, eq.symm e⟩, exact let ⟨j, ji, e⟩ := IH.2 m' in ⟨j, nat.le_succ_of_le ji, e⟩ end⟩ @[simp] theorem mem_rev_list : v ∈ a.rev_list ↔ v ∈ a := iff.symm $ iff.trans (exists_congr $ λ j, iff.symm $ show j.1 < n ∧ read a j = v ↔ read a j = v, from and_iff_right j.2) (mem_rev_list_aux _) @[simp] theorem mem_to_list : v ∈ a.to_list ↔ v ∈ a := by rw ←rev_list_reverse; exact list.mem_reverse.trans mem_rev_list end mem /- foldr -/ section foldr variables {n : ℕ} {α : Type u} {β : Type w} {b : β} {f : α → β → β} {a : array n α} theorem rev_list_foldr_aux : ∀ {i} (h : i ≤ n), (d_array.iterate_aux a (λ _, (::)) i h []).foldr f b = d_array.iterate_aux a (λ _, f) i h b | 0 h := rfl | (j+1) h := congr_arg (f (read a ⟨j, h⟩)) (rev_list_foldr_aux _) theorem rev_list_foldr : a.rev_list.foldr f b = a.foldl b f := rev_list_foldr_aux _ end foldr /- foldl -/ section foldl variables {n : ℕ} {α : Type u} {β : Type w} {b : β} {f : β → α → β} {a : array n α} theorem to_list_foldl : a.to_list.foldl f b = a.foldl b (function.swap f) := by rw [←rev_list_reverse, list.foldl_reverse, rev_list_foldr] end foldl /- length -/ section length variables {n : ℕ} {α : Type u} theorem rev_list_length_aux (a : array n α) (i h) : (a.iterate_aux (λ _, (::)) i h []).length = i := by induction i; simp [*, d_array.iterate_aux] @[simp] theorem rev_list_length (a : array n α) : a.rev_list.length = n := rev_list_length_aux a _ _ @[simp] theorem to_list_length (a : array n α) : a.to_list.length = n := by rw[←rev_list_reverse, list.length_reverse, rev_list_length] end length /- nth -/ section nth variables {n : ℕ} {α : Type u} {a : array n α} theorem to_list_nth_le_aux (i : ℕ) (ih : i < n) : ∀ j {jh t h'}, (∀ k tl, j + k = i → list.nth_le t k tl = a.read ⟨i, ih⟩) → (a.rev_iterate_aux (λ _, (::)) j jh t).nth_le i h' = a.read ⟨i, ih⟩ | 0 _ _ _ al := al i _ $ zero_add _ | (j+1) jh t h' al := to_list_nth_le_aux j $ λ k tl hjk, show list.nth_le (a.read ⟨j, jh⟩ :: t) k tl = a.read ⟨i, ih⟩, from match k, hjk, tl with | 0, e, tl := match i, e, ih with ._, rfl, _ := rfl end | k'+1, _, tl := by simp[list.nth_le]; exact al _ _ (by simp [*]) end theorem to_list_nth_le (i : ℕ) (h h') : list.nth_le a.to_list i h' = a.read ⟨i, h⟩ := to_list_nth_le_aux _ _ _ (λ k tl, absurd tl k.not_lt_zero) @[simp] theorem to_list_nth_le' (a : array n α) (i : fin n) (h') : list.nth_le a.to_list i.1 h' = a.read i := by cases i; apply to_list_nth_le theorem to_list_nth {i v} : list.nth a.to_list i = some v ↔ ∃ h, a.read ⟨i, h⟩ = v := begin rw list.nth_eq_some, have ll := to_list_length a, split; intro h; cases h with h e; subst v, { exact ⟨ll ▸ h, (to_list_nth_le _ _ _).symm⟩ }, { exact ⟨ll.symm ▸ h, to_list_nth_le _ _ _⟩ } end theorem write_to_list {i v} : (a.write i v).to_list = a.to_list.update_nth i.1 v := list.ext_le (by simp) $ λ j h₁ h₂, begin have h₃ : j < n, {simpa using h₁}, rw [to_list_nth_le _ h₃], refine let ⟨_, e⟩ := list.nth_eq_some.1 _ in e.symm, by_cases ij : i.1 = j, { subst j, rw [show fin.mk i.val h₃ = i, from fin.eq_of_veq rfl, array.read_write, list.nth_update_nth_of_lt], simp [h₃] }, { rw [list.nth_update_nth_ne _ _ ij, a.read_write_of_ne, to_list_nth.2 ⟨h₃, rfl⟩], exact fin.ne_of_vne ij } end end nth /- enum -/ section enum variables {n : ℕ} {α : Type u} {a : array n α} theorem mem_to_list_enum {i v} : (i, v) ∈ a.to_list.enum ↔ ∃ h, a.read ⟨i, h⟩ = v := by simp [list.mem_iff_nth, to_list_nth, and.comm, and.assoc, and.left_comm] end enum /- to_array -/ section to_array variables {n : ℕ} {α : Type u} @[simp] theorem to_list_to_array (a : array n α) : a.to_list.to_array == a := heq_of_heq_of_eq (@@eq.drec_on (λ m (e : a.to_list.length = m), (d_array.mk (λ v, a.to_list.nth_le v.1 v.2)) == (@d_array.mk m (λ _, α) $ λ v, a.to_list.nth_le v.1 $ e.symm ▸ v.2)) a.to_list_length heq.rfl) $ d_array.ext $ λ ⟨i, h⟩, to_list_nth_le i h _ @[simp] theorem to_array_to_list (l : list α) : l.to_array.to_list = l := list.ext_le (to_list_length _) $ λ n h1 h2, to_list_nth_le _ _ _ end to_array /- push_back -/ section push_back variables {n : ℕ} {α : Type u} {v : α} {a : array n α} lemma push_back_rev_list_aux : ∀ i h h', d_array.iterate_aux (a.push_back v) (λ _, (::)) i h [] = d_array.iterate_aux a (λ _, (::)) i h' [] | 0 h h' := rfl | (i+1) h h' := begin simp [d_array.iterate_aux], refine ⟨_, push_back_rev_list_aux _ _ _⟩, dsimp [read, d_array.read, push_back], rw [dif_neg], refl, exact ne_of_lt h', end @[simp] theorem push_back_rev_list : (a.push_back v).rev_list = v :: a.rev_list := begin unfold push_back rev_list foldl iterate d_array.iterate, dsimp [d_array.iterate_aux, read, d_array.read, push_back], rw [dif_pos (eq.refl n)], apply congr_arg, apply push_back_rev_list_aux end @[simp] theorem push_back_to_list : (a.push_back v).to_list = a.to_list ++ [v] := by rw [←rev_list_reverse, ←rev_list_reverse, push_back_rev_list, list.reverse_cons] end push_back /- foreach -/ section foreach variables {n : ℕ} {α : Type u} {i : fin n} {f : fin n → α → α} {a : array n α} theorem read_foreach_aux : ∀ i h (b : array n α) (j : fin n), j.1 < i → (d_array.iterate_aux a (λ i v a', write a' i (f i v)) i h b).read j = f j (a.read j) | 0 hi a ⟨j, hj⟩ ji := absurd ji (nat.not_lt_zero _) | (i+1) hi a ⟨j, hj⟩ ji := begin dsimp [d_array.iterate_aux], dsimp at ji, by_cases e : (⟨i, hi⟩ : fin _) = ⟨j, hj⟩, { rw [e], simp, refl }, { rw [read_write_of_ne _ _ e, read_foreach_aux _ _ _ ⟨j, hj⟩], exact (lt_or_eq_of_le (nat.le_of_lt_succ ji)).resolve_right (ne.symm $ mt (@fin.eq_of_veq _ ⟨i, hi⟩ ⟨j, hj⟩) e) } end theorem read_foreach : (foreach a f).read i = f i (a.read i) := read_foreach_aux _ _ _ _ i.2 end foreach /- map -/ section map variables {n : ℕ} {α : Type u} {i : fin n} {f : α → α} {a : array n α} theorem read_map : (map f a).read i = f (a.read i) := read_foreach end map /- map₂ -/ section map₂ variables {n : ℕ} {α : Type u} {i : fin n} {f : α → α → α} {a₁ a₂ : array n α} theorem read_map₂ : (map₂ f a₁ a₂).read i = f (a₁.read i) (a₂.read i) := read_foreach end map₂ end array namespace equiv def d_array_equiv_fin {n : ℕ} (α : fin n → Type*) : d_array n α ≃ (∀ i, α i) := ⟨d_array.read, d_array.mk, λ ⟨f⟩, rfl, λ f, rfl⟩ def array_equiv_fin (n : ℕ) (α : Type*) : array n α ≃ (fin n → α) := d_array_equiv_fin _ def vector_equiv_fin (α : Type*) (n : ℕ) : vector α n ≃ (fin n → α) := ⟨vector.nth, vector.of_fn, vector.of_fn_nth, λ f, funext $ vector.nth_of_fn f⟩ def vector_equiv_array (α : Type*) (n : ℕ) : vector α n ≃ array n α := (vector_equiv_fin _ _).trans (array_equiv_fin _ _).symm end equiv namespace array open function variable {n : ℕ} instance : traversable (array n) := @equiv.traversable (flip vector n) _ (λ α, equiv.vector_equiv_array α n) _ instance : is_lawful_traversable (array n) := @equiv.is_lawful_traversable (flip vector n) _ (λ α, equiv.vector_equiv_array α n) _ _ end array
3f88be27854f4cf2438bafe9d4e6a57ecea779e5
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/run/tree.lean
7a488a79a03a9386b18e1574bfee58914d16a798
[ "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
1,979
lean
import logic data.prod open eq.ops prod inductive tree (A : Type) := | leaf : A → tree A | node : tree A → tree A → tree A inductive one.{l} : Type.{max 1 l} := star : one set_option pp.universes true namespace tree namespace manual section universe variables l₁ l₂ variable {A : Type.{l₁}} variable (C : tree A → Type.{l₂}) definition below (t : tree A) : Type := tree.rec_on t (λ a, one.{l₂}) (λ t₁ t₂ r₁ r₂, C t₁ × C t₂ × r₁ × r₂) end section universe variables l₁ l₂ variable {A : Type.{l₁}} variable {C : tree A → Type.{l₂}} definition below_rec_on (t : tree A) (H : Π (n : tree A), below C n → C n) : C t := have general : C t × below C t, from tree.rec_on t (λa, (H (leaf a) one.star, one.star)) (λ (l r : tree A) (Hl : C l × below C l) (Hr : C r × below C r), have b : below C (node l r), from (pr₁ Hl, pr₁ Hr, pr₂ Hl, pr₂ Hr), have c : C (node l r), from H (node l r) b, (c, b)), pr₁ general end end manual section universe variables l₁ l₂ variable {A : Type.{l₁}} variable {C : tree A → Type.{l₂+1}} definition below_rec_on (t : tree A) (H : Π (n : tree A), @tree.below A C n → C n) : C t := have general : C t × @tree.below A C t, from tree.rec_on t (λa, (H (leaf a) unit.star, unit.star)) (λ (l r : tree A) (Hl : C l × @tree.below A C l) (Hr : C r × @tree.below A C r), have b : @tree.below A C (node l r), from ((pr₁ Hl, pr₂ Hl), (pr₁ Hr, pr₂ Hr)), have c : C (node l r), from H (node l r) b, (c, b)), pr₁ general end set_option pp.universes true theorem leaf_ne_tree {A : Type} (a : A) (l r : tree A) : leaf a ≠ node l r := assume h : leaf a = node l r, tree.no_confusion h end tree
639ce3f4d55e0b2594c2fefd6ead07395db67056
618003631150032a5676f229d13a079ac875ff77
/src/measure_theory/decomposition.lean
3e7f0169e6428192db23ca06967e00a30702a980
[ "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
8,590
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 Hahn decomposition theorem TODO: * introduce finite measures (into nnreal) * show general for signed measures (into ℝ) -/ import measure_theory.measure_space open set filter open_locale classical topological_space namespace measure_theory variables {α : Type*} [measurable_space α] {μ ν : measure α} -- suddenly this is necessary?! private lemma aux {m : ℕ} {γ d : ℝ} (h : γ - (1 / 2) ^ m < d) : γ - 2 * (1 / 2) ^ m + (1 / 2) ^ m ≤ d := by linarith lemma hahn_decomposition (hμ : μ univ < ⊤) (hν : ν univ < ⊤) : ∃s, is_measurable s ∧ (∀t, is_measurable t → t ⊆ s → ν t ≤ μ t) ∧ (∀t, is_measurable t → t ⊆ - s → μ t ≤ ν t) := begin let d : set α → ℝ := λs, ((μ s).to_nnreal : ℝ) - (ν s).to_nnreal, let c : set ℝ := d '' {s | is_measurable s }, let γ : ℝ := Sup c, have hμ : ∀s, μ s < ⊤ := assume s, lt_of_le_of_lt (measure_mono $ subset_univ _) hμ, have hν : ∀s, ν s < ⊤ := assume s, lt_of_le_of_lt (measure_mono $ subset_univ _) hν, have to_nnreal_μ : ∀s, ((μ s).to_nnreal : ennreal) = μ s := (assume s, ennreal.coe_to_nnreal $ ne_top_of_lt $ hμ _), have to_nnreal_ν : ∀s, ((ν s).to_nnreal : ennreal) = ν s := (assume s, ennreal.coe_to_nnreal $ ne_top_of_lt $ hν _), have d_empty : d ∅ = 0, { simp [d], rw [measure_empty, measure_empty], simp }, have d_split : ∀s t, is_measurable s → is_measurable t → d s = d (s \ t) + d (s ∩ t), { assume s t hs ht, simp only [d], rw [measure_eq_inter_diff hs ht, measure_eq_inter_diff hs ht, ennreal.to_nnreal_add (hμ _) (hμ _), ennreal.to_nnreal_add (hν _) (hν _), nnreal.coe_add, nnreal.coe_add], simp only [sub_eq_add_neg, neg_add], ac_refl }, have d_Union : ∀(s : ℕ → set α), (∀n, is_measurable (s n)) → monotone s → tendsto (λn, d (s n)) at_top (𝓝 (d (⋃n, s n))), { assume s hs hm, refine tendsto.sub _ _; refine (nnreal.tendsto_coe.2 $ (ennreal.tendsto_to_nnreal $ @ne_top_of_lt _ _ _ ⊤ _).comp $ tendsto_measure_Union hs hm), exact hμ _, exact hν _ }, have d_Inter : ∀(s : ℕ → set α), (∀n, is_measurable (s n)) → (∀n m, n ≤ m → s m ⊆ s n) → tendsto (λn, d (s n)) at_top (𝓝 (d (⋂n, s n))), { assume s hs hm, refine tendsto.sub _ _; refine (nnreal.tendsto_coe.2 $ (ennreal.tendsto_to_nnreal $ @ne_top_of_lt _ _ _ ⊤ _).comp $ tendsto_measure_Inter hs hm _), exact hμ _, exact ⟨0, hμ _⟩, exact hν _, exact ⟨0, hν _⟩ }, have bdd_c : bdd_above c, { use (μ univ).to_nnreal, rintros r ⟨s, hs, rfl⟩, refine le_trans (sub_le_self _ $ nnreal.coe_nonneg _) _, rw [nnreal.coe_le_coe, ← ennreal.coe_le_coe, to_nnreal_μ, to_nnreal_μ], exact measure_mono (subset_univ _) }, have c_nonempty : c.nonempty := nonempty.image _ ⟨_, is_measurable.empty⟩, have d_le_γ : ∀s, is_measurable s → d s ≤ γ := assume s hs, le_cSup bdd_c ⟨s, hs, rfl⟩, have : ∀n:ℕ, ∃s : set α, is_measurable s ∧ γ - (1/2)^n < d s, { assume n, have : γ - (1/2)^n < γ := sub_lt_self γ (pow_pos (half_pos zero_lt_one) n), rcases exists_lt_of_lt_cSup c_nonempty this with ⟨r, ⟨s, hs, rfl⟩, hlt⟩, exact ⟨s, hs, hlt⟩ }, rcases classical.axiom_of_choice this with ⟨e, he⟩, change ℕ → set α at e, have he₁ : ∀n, is_measurable (e n) := assume n, (he n).1, have he₂ : ∀n, γ - (1/2)^n < d (e n) := assume n, (he n).2, let f : ℕ → ℕ → set α := λn m, (finset.Ico n (m + 1)).inf e, have hf : ∀n m, is_measurable (f n m), { assume n m, simp only [f, finset.inf_eq_infi], exact is_measurable.bInter (countable_encodable _) (assume i _, he₁ _) }, have f_subset_f : ∀{a b c d}, a ≤ b → c ≤ d → f a d ⊆ f b c, { assume a b c d hab hcd, dsimp only [f], rw [finset.inf_eq_infi, finset.inf_eq_infi], refine bInter_subset_bInter_left _, simp, rintros j ⟨hbj, hjc⟩, exact ⟨le_trans hab hbj, lt_of_lt_of_le hjc $ add_le_add_right hcd 1⟩ }, have f_succ : ∀n m, n ≤ m → f n (m + 1) = f n m ∩ e (m + 1), { assume n m hnm, have : n ≤ m + 1 := le_of_lt (nat.succ_le_succ hnm), simp only [f], rw [finset.Ico.succ_top this, finset.inf_insert, set.inter_comm], refl }, have le_d_f : ∀n m, m ≤ n → γ - 2 * ((1 / 2) ^ m) + (1 / 2) ^ n ≤ d (f m n), { assume n m h, refine nat.le_induction _ _ n h, { have := he₂ m, simp only [f], rw [finset.Ico.succ_singleton, finset.inf_singleton], exact aux this }, { assume n (hmn : m ≤ n) ih, have : γ + (γ - 2 * (1 / 2)^m + (1 / 2) ^ (n + 1)) ≤ γ + d (f m (n + 1)), { calc γ + (γ - 2 * (1 / 2)^m + (1 / 2) ^ (n+1)) ≤ γ + (γ - 2 * (1 / 2)^m + ((1 / 2) ^ n - (1/2)^(n+1))) : begin refine add_le_add_left (add_le_add_left _ _) γ, simp only [pow_add, pow_one, le_sub_iff_add_le], linarith end ... = (γ - (1 / 2)^(n+1)) + (γ - 2 * (1 / 2)^m + (1 / 2)^n) : by simp only [sub_eq_add_neg]; ac_refl ... ≤ d (e (n + 1)) + d (f m n) : add_le_add (le_of_lt $ he₂ _) ih ... ≤ d (e (n + 1)) + d (f m n \ e (n + 1)) + d (f m (n + 1)) : by rw [f_succ _ _ hmn, d_split (f m n) (e (n + 1)) (hf _ _) (he₁ _), add_assoc] ... = d (e (n + 1) ∪ f m n) + d (f m (n + 1)) : begin rw [d_split (e (n + 1) ∪ f m n) (e (n + 1)), union_diff_left, union_inter_cancel_left], ac_refl, exact (he₁ _).union (hf _ _), exact (he₁ _) end ... ≤ γ + d (f m (n + 1)) : add_le_add_right (d_le_γ _ $ (he₁ _).union (hf _ _)) _ }, exact (add_le_add_iff_left γ).1 this } }, let s := ⋃ m, ⋂n, f m n, have γ_le_d_s : γ ≤ d s, { have hγ : tendsto (λm:ℕ, γ - 2 * (1/2)^m) at_top (𝓝 γ), { suffices : tendsto (λm:ℕ, γ - 2 * (1/2)^m) at_top (𝓝 (γ - 2 * 0)), { simpa }, exact (tendsto_const_nhds.sub $ tendsto_const_nhds.mul $ tendsto_pow_at_top_nhds_0_of_lt_1 (le_of_lt $ half_pos $ zero_lt_one) (half_lt_self zero_lt_one)) }, have hd : tendsto (λm, d (⋂n, f m n)) at_top (𝓝 (d (⋃ m, ⋂ n, f m n))), { refine d_Union _ _ _, { assume n, exact is_measurable.Inter (assume m, hf _ _) }, { exact assume n m hnm, subset_Inter (assume i, subset.trans (Inter_subset (f n) i) $ f_subset_f hnm $ le_refl _) } }, refine le_of_tendsto_of_tendsto' at_top_ne_bot hγ hd (assume m, _), have : tendsto (λn, d (f m n)) at_top (𝓝 (d (⋂ n, f m n))), { refine d_Inter _ _ _, { assume n, exact hf _ _ }, { assume n m hnm, exact f_subset_f (le_refl _) hnm } }, refine ge_of_tendsto (@at_top_ne_bot ℕ _ _) this (eventually_at_top.2 ⟨m, assume n hmn, _⟩), change γ - 2 * (1 / 2) ^ m ≤ d (f m n), refine le_trans _ (le_d_f _ _ hmn), exact le_add_of_le_of_nonneg (le_refl _) (pow_nonneg (le_of_lt $ half_pos $ zero_lt_one) _) }, have hs : is_measurable s := is_measurable.Union (assume n, is_measurable.Inter (assume m, hf _ _)), refine ⟨s, hs, _, _⟩, { assume t ht hts, have : 0 ≤ d t := ((add_le_add_iff_left γ).1 $ calc γ + 0 ≤ d s : by rw [add_zero]; exact γ_le_d_s ... = d (s \ t) + d t : by rw [d_split _ _ hs ht, inter_eq_self_of_subset_right hts] ... ≤ γ + d t : add_le_add (d_le_γ _ (hs.diff ht)) (le_refl _)), rw [← to_nnreal_μ, ← to_nnreal_ν, ennreal.coe_le_coe, ← nnreal.coe_le_coe], simpa only [d, le_sub_iff_add_le, zero_add] using this }, { assume t ht hts, have : d t ≤ 0, exact ((add_le_add_iff_left γ).1 $ calc γ + d t ≤ d s + d t : add_le_add γ_le_d_s (le_refl _) ... = d (s ∪ t) : begin rw [d_split _ _ (hs.union ht) ht, union_diff_right, union_inter_cancel_right, diff_eq_self.2], exact assume a ⟨hat, has⟩, hts hat has end ... ≤ γ + 0 : by rw [add_zero]; exact d_le_γ _ (hs.union ht)), rw [← to_nnreal_μ, ← to_nnreal_ν, ennreal.coe_le_coe, ← nnreal.coe_le_coe], simpa only [d, sub_le_iff_le_add, zero_add] using this } end end measure_theory
9b8a9a56b43c52c135f7c1c7b97fc7dc15f08ee4
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/foApprox.lean
475d409be2703d5b2383d0796ca2c3dd817f5cb7
[ "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
265
lean
def f : Prop → Prop := id class Foo (foo : Prop → Prop) where l : x → foo x instance : Foo f where l := by simp_all [f] theorem test' (h : x = True) : f x := by have _ := True apply Foo.l -- `?foo ?x =?= [mdata noImplicitLambda:1 f x]` simp [h]
af41c935731d3d2f9b84d45e1f7a5443f5a732c4
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/measure_theory/measure/finite_measure_weak_convergence.lean
aed6f9a29353940d895f7da785693525e91e25dd
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
56,037
lean
/- Copyright (c) 2021 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä -/ import measure_theory.measure.measure_space import measure_theory.integral.set_integral import measure_theory.integral.average import topology.continuous_function.bounded import topology.algebra.module.weak_dual import topology.metric_space.thickened_indicator /-! # Weak convergence of (finite) measures This file defines the topology of weak convergence of finite measures and probability measures on topological spaces. The topology of weak convergence is the coarsest topology w.r.t. which for every bounded continuous `ℝ≥0`-valued function `f`, the integration of `f` against the measure is continuous. TODOs: * Include the portmanteau theorem on characterizations of weak convergence of (Borel) probability measures. ## Main definitions The main definitions are the * types `measure_theory.finite_measure α` and `measure_theory.probability_measure α` with the topologies of weak convergence; * `measure_theory.finite_measure.normalize`, normalizing a finite measure to a probability measure (returns junk for the zero measure); * `measure_theory.finite_measure.to_weak_dual_bcnn : finite_measure α → (weak_dual ℝ≥0 (α →ᵇ ℝ≥0))` allowing to interpret a finite measure as a continuous linear functional on the space of bounded continuous nonnegative functions on `α`. This is used for the definition of the topology of weak convergence. ## Main results * Finite measures `μ` on `α` give rise to continuous linear functionals on the space of bounded continuous nonnegative functions on `α` via integration: `measure_theory.finite_measure.to_weak_dual_bcnn : finite_measure α → (weak_dual ℝ≥0 (α →ᵇ ℝ≥0))` * `measure_theory.finite_measure.tendsto_iff_forall_integral_tendsto` and `measure_theory.probability_measure.tendsto_iff_forall_integral_tendsto`: Convergence of finite measures and probability measures is characterized by the convergence of integrals of all bounded continuous functions. This shows that the chosen definition of topology coincides with the common textbook definition of weak convergence of measures. Similar characterizations by the convergence of integrals (in the `measure_theory.lintegral` sense) of all bounded continuous nonnegative functions are `measure_theory.finite_measure.tendsto_iff_forall_lintegral_tendsto` and `measure_theory.probability_measure.tendsto_iff_forall_lintegral_tendsto`. * `measure_theory.finite_measure.tendsto_normalize_iff_tendsto`: The convergence of finite measures to a nonzero limit is characterized by the convergence of the probability-normalized versions and of the total masses. TODO: * Portmanteau theorem: * `measure_theory.finite_measure.limsup_measure_closed_le_of_tendsto` proves one implication. The current formulation assumes `pseudo_emetric_space`. The only reason is to have bounded continuous pointwise approximations to the indicator function of a closed set. Clearly for example metrizability or pseudo-emetrizability would be sufficient assumptions. The typeclass assumptions should be later adjusted in a way that takes into account use cases, but the proof will presumably remain essentially the same. * Prove the rest of the implications. ## Notations No new notation is introduced. ## Implementation notes The topology of weak convergence of finite Borel measures will be defined using a mapping from `measure_theory.finite_measure α` to `weak_dual ℝ≥0 (α →ᵇ ℝ≥0)`, inheriting the topology from the latter. The current implementation of `measure_theory.finite_measure α` and `measure_theory.probability_measure α` is directly as subtypes of `measure_theory.measure α`, and the coercion to a function is the composition `ennreal.to_nnreal` and the coercion to function of `measure_theory.measure α`. Another alternative would be to use a bijection with `measure_theory.vector_measure α ℝ≥0` as an intermediate step. The choice of implementation should not have drastic downstream effects, so it can be changed later if appropriate. Potential advantages of using the `nnreal`-valued vector measure alternative: * The coercion to function would avoid need to compose with `ennreal.to_nnreal`, the `nnreal`-valued API could be more directly available. Potential drawbacks of the vector measure alternative: * The coercion to function would lose monotonicity, as non-measurable sets would be defined to have measure 0. * No integration theory directly. E.g., the topology definition requires `measure_theory.lintegral` w.r.t. a coercion to `measure_theory.measure α` in any case. ## References * [Billingsley, *Convergence of probability measures*][billingsley1999] ## Tags weak convergence of measures, finite measure, probability measure -/ noncomputable theory open measure_theory open set open filter open bounded_continuous_function open_locale topological_space ennreal nnreal bounded_continuous_function namespace measure_theory namespace finite_measure section finite_measure /-! ### Finite measures In this section we define the `Type` of `finite_measure α`, when `α` is a measurable space. Finite measures on `α` are a module over `ℝ≥0`. If `α` is moreover a topological space and the sigma algebra on `α` is finer than the Borel sigma algebra (i.e. `[opens_measurable_space α]`), then `finite_measure α` is equipped with the topology of weak convergence of measures. This is implemented by defining a pairing of finite measures `μ` on `α` with continuous bounded nonnegative functions `f : α →ᵇ ℝ≥0` via integration, and using the associated weak topology (essentially the weak-star topology on the dual of `α →ᵇ ℝ≥0`). -/ variables {α : Type*} [measurable_space α] /-- Finite measures are defined as the subtype of measures that have the property of being finite measures (i.e., their total mass is finite). -/ def _root_.measure_theory.finite_measure (α : Type*) [measurable_space α] : Type* := {μ : measure α // is_finite_measure μ} /-- A finite measure can be interpreted as a measure. -/ instance : has_coe (finite_measure α) (measure_theory.measure α) := coe_subtype instance is_finite_measure (μ : finite_measure α) : is_finite_measure (μ : measure α) := μ.prop instance : has_coe_to_fun (finite_measure α) (λ _, set α → ℝ≥0) := ⟨λ μ s, (μ s).to_nnreal⟩ lemma coe_fn_eq_to_nnreal_coe_fn_to_measure (ν : finite_measure α) : (ν : set α → ℝ≥0) = λ s, ((ν : measure α) s).to_nnreal := rfl @[simp] lemma ennreal_coe_fn_eq_coe_fn_to_measure (ν : finite_measure α) (s : set α) : (ν s : ℝ≥0∞) = (ν : measure α) s := ennreal.coe_to_nnreal (measure_lt_top ↑ν s).ne @[simp] lemma val_eq_to_measure (ν : finite_measure α) : ν.val = (ν : measure α) := rfl lemma coe_injective : function.injective (coe : finite_measure α → measure α) := subtype.coe_injective /-- The (total) mass of a finite measure `μ` is `μ univ`, i.e., the cast to `nnreal` of `(μ : measure α) univ`. -/ def mass (μ : finite_measure α) : ℝ≥0 := μ univ @[simp] lemma ennreal_mass {μ : finite_measure α} : (μ.mass : ℝ≥0∞) = (μ : measure α) univ := ennreal_coe_fn_eq_coe_fn_to_measure μ set.univ instance has_zero : has_zero (finite_measure α) := { zero := ⟨0, measure_theory.is_finite_measure_zero⟩ } @[simp] lemma zero.mass : (0 : finite_measure α).mass = 0 := rfl @[simp] lemma mass_zero_iff (μ : finite_measure α) : μ.mass = 0 ↔ μ = 0 := begin refine ⟨λ μ_mass, _, (λ hμ, by simp only [hμ, zero.mass])⟩, ext1, apply measure.measure_univ_eq_zero.mp, rwa [← ennreal_mass, ennreal.coe_eq_zero], end lemma mass_nonzero_iff (μ : finite_measure α) : μ.mass ≠ 0 ↔ μ ≠ 0 := begin rw not_iff_not, exact finite_measure.mass_zero_iff μ, end @[ext] lemma extensionality (μ ν : finite_measure α) (h : ∀ (s : set α), measurable_set s → μ s = ν s) : μ = ν := begin ext1, ext1 s s_mble, simpa [ennreal_coe_fn_eq_coe_fn_to_measure] using congr_arg (coe : ℝ≥0 → ℝ≥0∞) (h s s_mble), end instance : inhabited (finite_measure α) := ⟨0⟩ instance : has_add (finite_measure α) := { add := λ μ ν, ⟨μ + ν, measure_theory.is_finite_measure_add⟩ } variables {R : Type*} [has_smul R ℝ≥0] [has_smul R ℝ≥0∞] [is_scalar_tower R ℝ≥0 ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] instance : has_smul R (finite_measure α) := { smul := λ (c : R) μ, ⟨c • μ, measure_theory.is_finite_measure_smul_of_nnreal_tower⟩, } @[simp, norm_cast] lemma coe_zero : (coe : finite_measure α → measure α) 0 = 0 := rfl @[simp, norm_cast] lemma coe_add (μ ν : finite_measure α) : ↑(μ + ν) = (↑μ + ↑ν : measure α) := rfl @[simp, norm_cast] lemma coe_smul (c : R) (μ : finite_measure α) : ↑(c • μ) = (c • ↑μ : measure α) := rfl @[simp, norm_cast] lemma coe_fn_zero : (⇑(0 : finite_measure α) : set α → ℝ≥0) = (0 : set α → ℝ≥0) := by { funext, refl, } @[simp, norm_cast] lemma coe_fn_add (μ ν : finite_measure α) : (⇑(μ + ν) : set α → ℝ≥0) = (⇑μ + ⇑ν : set α → ℝ≥0) := by { funext, simp [← ennreal.coe_eq_coe], } @[simp, norm_cast] lemma coe_fn_smul [is_scalar_tower R ℝ≥0 ℝ≥0] (c : R) (μ : finite_measure α) : (⇑(c • μ) : set α → ℝ≥0) = c • (⇑μ : set α → ℝ≥0) := by { funext, simp [← ennreal.coe_eq_coe, ennreal.coe_smul], } instance : add_comm_monoid (finite_measure α) := coe_injective.add_comm_monoid coe coe_zero coe_add (λ _ _, coe_smul _ _) /-- Coercion is an `add_monoid_hom`. -/ @[simps] def coe_add_monoid_hom : finite_measure α →+ measure α := { to_fun := coe, map_zero' := coe_zero, map_add' := coe_add } instance {α : Type*} [measurable_space α] : module ℝ≥0 (finite_measure α) := function.injective.module _ coe_add_monoid_hom coe_injective coe_smul @[simp] lemma coe_fn_smul_apply [is_scalar_tower R ℝ≥0 ℝ≥0] (c : R) (μ : finite_measure α) (s : set α) : (c • μ) s = c • (μ s) := by { simp only [coe_fn_smul, pi.smul_apply], } variables [topological_space α] /-- The pairing of a finite (Borel) measure `μ` with a nonnegative bounded continuous function is obtained by (Lebesgue) integrating the (test) function against the measure. This is `finite_measure.test_against_nn`. -/ def test_against_nn (μ : finite_measure α) (f : α →ᵇ ℝ≥0) : ℝ≥0 := (∫⁻ x, f x ∂(μ : measure α)).to_nnreal lemma _root_.bounded_continuous_function.nnreal.to_ennreal_comp_measurable {α : Type*} [topological_space α] [measurable_space α] [opens_measurable_space α] (f : α →ᵇ ℝ≥0) : measurable (λ x, (f x : ℝ≥0∞)) := measurable_coe_nnreal_ennreal.comp f.continuous.measurable lemma _root_.measure_theory.lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : measure α) [is_finite_measure μ] (f : α →ᵇ ℝ≥0) : ∫⁻ x, f x ∂μ < ∞ := begin apply is_finite_measure.lintegral_lt_top_of_bounded_to_ennreal, use nndist f 0, intros x, have key := bounded_continuous_function.nnreal.upper_bound f x, rw ennreal.coe_le_coe, have eq : nndist f 0 = ⟨dist f 0, dist_nonneg⟩, { ext, simp only [real.coe_to_nnreal', max_eq_left_iff, subtype.coe_mk, coe_nndist], }, rwa eq at key, end @[simp] lemma test_against_nn_coe_eq {μ : finite_measure α} {f : α →ᵇ ℝ≥0} : (μ.test_against_nn f : ℝ≥0∞) = ∫⁻ x, f x ∂(μ : measure α) := ennreal.coe_to_nnreal (lintegral_lt_top_of_bounded_continuous_to_nnreal _ f).ne lemma test_against_nn_const (μ : finite_measure α) (c : ℝ≥0) : μ.test_against_nn (bounded_continuous_function.const α c) = c * μ.mass := by simp [← ennreal.coe_eq_coe] lemma test_against_nn_mono (μ : finite_measure α) {f g : α →ᵇ ℝ≥0} (f_le_g : (f : α → ℝ≥0) ≤ g) : μ.test_against_nn f ≤ μ.test_against_nn g := begin simp only [←ennreal.coe_le_coe, test_against_nn_coe_eq], exact lintegral_mono (λ x, ennreal.coe_mono (f_le_g x)), end @[simp] lemma test_against_nn_zero (μ : finite_measure α) : μ.test_against_nn 0 = 0 := by simpa only [zero_mul] using μ.test_against_nn_const 0 @[simp] lemma test_against_nn_one (μ : finite_measure α) : μ.test_against_nn 1 = μ.mass := begin simp only [test_against_nn, coe_one, pi.one_apply, ennreal.coe_one, lintegral_one], refl, end @[simp] lemma zero.test_against_nn_apply (f : α →ᵇ ℝ≥0) : (0 : finite_measure α).test_against_nn f = 0 := by simp only [test_against_nn, coe_zero, lintegral_zero_measure, ennreal.zero_to_nnreal] lemma zero.test_against_nn : (0 : finite_measure α).test_against_nn = 0 := by { funext, simp only [zero.test_against_nn_apply, pi.zero_apply], } @[simp] lemma smul_test_against_nn_apply (c : ℝ≥0) (μ : finite_measure α) (f : α →ᵇ ℝ≥0) : (c • μ).test_against_nn f = c • (μ.test_against_nn f) := by simp only [test_against_nn, coe_smul, smul_eq_mul, ← ennreal.smul_to_nnreal, ennreal.smul_def, lintegral_smul_measure] variables [opens_measurable_space α] lemma test_against_nn_add (μ : finite_measure α) (f₁ f₂ : α →ᵇ ℝ≥0) : μ.test_against_nn (f₁ + f₂) = μ.test_against_nn f₁ + μ.test_against_nn f₂ := begin simp only [←ennreal.coe_eq_coe, bounded_continuous_function.coe_add, ennreal.coe_add, pi.add_apply, test_against_nn_coe_eq], exact lintegral_add_left (bounded_continuous_function.nnreal.to_ennreal_comp_measurable _) _ end lemma test_against_nn_smul [is_scalar_tower R ℝ≥0 ℝ≥0] [pseudo_metric_space R] [has_zero R] [has_bounded_smul R ℝ≥0] (μ : finite_measure α) (c : R) (f : α →ᵇ ℝ≥0) : μ.test_against_nn (c • f) = c • μ.test_against_nn f := begin simp only [←ennreal.coe_eq_coe, bounded_continuous_function.coe_smul, test_against_nn_coe_eq, ennreal.coe_smul], simp_rw [←smul_one_smul ℝ≥0∞ c (f _ : ℝ≥0∞), ←smul_one_smul ℝ≥0∞ c (lintegral _ _ : ℝ≥0∞), smul_eq_mul], exact @lintegral_const_mul _ _ (μ : measure α) (c • 1) _ (bounded_continuous_function.nnreal.to_ennreal_comp_measurable f), end lemma test_against_nn_lipschitz_estimate (μ : finite_measure α) (f g : α →ᵇ ℝ≥0) : μ.test_against_nn f ≤ μ.test_against_nn g + (nndist f g) * μ.mass := begin simp only [←μ.test_against_nn_const (nndist f g), ←test_against_nn_add, ←ennreal.coe_le_coe, bounded_continuous_function.coe_add, const_apply, ennreal.coe_add, pi.add_apply, coe_nnreal_ennreal_nndist, test_against_nn_coe_eq], apply lintegral_mono, have le_dist : ∀ x, dist (f x) (g x) ≤ nndist f g := bounded_continuous_function.dist_coe_le_dist, intros x, have le' : f(x) ≤ g(x) + nndist f g, { apply (nnreal.le_add_nndist (f x) (g x)).trans, rw add_le_add_iff_left, exact dist_le_coe.mp (le_dist x), }, have le : (f(x) : ℝ≥0∞) ≤ (g(x) : ℝ≥0∞) + (nndist f g), by { rw ←ennreal.coe_add, exact ennreal.coe_mono le', }, rwa [coe_nnreal_ennreal_nndist] at le, end lemma test_against_nn_lipschitz (μ : finite_measure α) : lipschitz_with μ.mass (λ (f : α →ᵇ ℝ≥0), μ.test_against_nn f) := begin rw lipschitz_with_iff_dist_le_mul, intros f₁ f₂, suffices : abs (μ.test_against_nn f₁ - μ.test_against_nn f₂ : ℝ) ≤ μ.mass * (dist f₁ f₂), { rwa nnreal.dist_eq, }, apply abs_le.mpr, split, { have key' := μ.test_against_nn_lipschitz_estimate f₂ f₁, rw mul_comm at key', suffices : ↑(μ.test_against_nn f₂) ≤ ↑(μ.test_against_nn f₁) + ↑(μ.mass) * dist f₁ f₂, { linarith, }, have key := nnreal.coe_mono key', rwa [nnreal.coe_add, nnreal.coe_mul, nndist_comm] at key, }, { have key' := μ.test_against_nn_lipschitz_estimate f₁ f₂, rw mul_comm at key', suffices : ↑(μ.test_against_nn f₁) ≤ ↑(μ.test_against_nn f₂) + ↑(μ.mass) * dist f₁ f₂, { linarith, }, have key := nnreal.coe_mono key', rwa [nnreal.coe_add, nnreal.coe_mul] at key, }, end /-- Finite measures yield elements of the `weak_dual` of bounded continuous nonnegative functions via `measure_theory.finite_measure.test_against_nn`, i.e., integration. -/ def to_weak_dual_bcnn (μ : finite_measure α) : weak_dual ℝ≥0 (α →ᵇ ℝ≥0) := { to_fun := λ f, μ.test_against_nn f, map_add' := test_against_nn_add μ, map_smul' := test_against_nn_smul μ, cont := μ.test_against_nn_lipschitz.continuous, } @[simp] lemma coe_to_weak_dual_bcnn (μ : finite_measure α) : ⇑μ.to_weak_dual_bcnn = μ.test_against_nn := rfl @[simp] lemma to_weak_dual_bcnn_apply (μ : finite_measure α) (f : α →ᵇ ℝ≥0) : μ.to_weak_dual_bcnn f = (∫⁻ x, f x ∂(μ : measure α)).to_nnreal := rfl /-- The topology of weak convergence on `measure_theory.finite_measure α` is inherited (induced) from the weak-* topology on `weak_dual ℝ≥0 (α →ᵇ ℝ≥0)` via the function `measure_theory.finite_measure.to_weak_dual_bcnn`. -/ instance : topological_space (finite_measure α) := topological_space.induced to_weak_dual_bcnn infer_instance lemma to_weak_dual_bcnn_continuous : continuous (@to_weak_dual_bcnn α _ _ _) := continuous_induced_dom /- Integration of (nonnegative bounded continuous) test functions against finite Borel measures depends continuously on the measure. -/ lemma continuous_test_against_nn_eval (f : α →ᵇ ℝ≥0) : continuous (λ (μ : finite_measure α), μ.test_against_nn f) := (by apply (weak_bilin.eval_continuous _ _).comp to_weak_dual_bcnn_continuous : continuous ((λ φ : weak_dual ℝ≥0 (α →ᵇ ℝ≥0), φ f) ∘ to_weak_dual_bcnn)) /-- The total mass of a finite measure depends continuously on the measure. -/ lemma continuous_mass : continuous (λ (μ : finite_measure α), μ.mass) := by { simp_rw ←test_against_nn_one, exact continuous_test_against_nn_eval 1, } /-- Convergence of finite measures implies the convergence of their total masses. -/ lemma _root_.filter.tendsto.mass {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} {μ : finite_measure α} (h : tendsto μs F (𝓝 μ)) : tendsto (λ i, (μs i).mass) F (𝓝 μ.mass) := (continuous_mass.tendsto μ).comp h lemma tendsto_iff_weak_star_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} {μ : finite_measure α} : tendsto μs F (𝓝 μ) ↔ tendsto (λ i, (μs(i)).to_weak_dual_bcnn) F (𝓝 μ.to_weak_dual_bcnn) := inducing.tendsto_nhds_iff ⟨rfl⟩ theorem tendsto_iff_forall_to_weak_dual_bcnn_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} {μ : finite_measure α} : tendsto μs F (𝓝 μ) ↔ ∀ (f : α →ᵇ ℝ≥0), tendsto (λ i, (μs i).to_weak_dual_bcnn f) F (𝓝 (μ.to_weak_dual_bcnn f)) := by { rw [tendsto_iff_weak_star_tendsto, tendsto_iff_forall_eval_tendsto_top_dual_pairing], refl, } theorem tendsto_iff_forall_test_against_nn_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} {μ : finite_measure α} : tendsto μs F (𝓝 μ) ↔ ∀ (f : α →ᵇ ℝ≥0), tendsto (λ i, (μs i).test_against_nn f) F (𝓝 (μ.test_against_nn f)) := by { rw finite_measure.tendsto_iff_forall_to_weak_dual_bcnn_tendsto, refl, } /-- If the total masses of finite measures tend to zero, then the measures tend to zero. This formulation concerns the associated functionals on bounded continuous nonnegative test functions. See `finite_measure.tendsto_zero_of_tendsto_zero_mass` for a formulation stating the weak convergence of measures. -/ lemma tendsto_zero_test_against_nn_of_tendsto_zero_mass {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} (mass_lim : tendsto (λ i, (μs i).mass) F (𝓝 0)) (f : α →ᵇ ℝ≥0) : tendsto (λ i, (μs i).test_against_nn f) F (𝓝 0) := begin apply tendsto_iff_dist_tendsto_zero.mpr, have obs := λ i, (μs i).test_against_nn_lipschitz_estimate f 0, simp_rw [test_against_nn_zero, zero_add] at obs, simp_rw (show ∀ i, dist ((μs i).test_against_nn f) 0 = (μs i).test_against_nn f, by simp only [dist_nndist, nnreal.nndist_zero_eq_val', eq_self_iff_true, implies_true_iff]), refine squeeze_zero (λ i, nnreal.coe_nonneg _) obs _, simp_rw nnreal.coe_mul, have lim_pair : tendsto (λ i, (⟨nndist f 0, (μs i).mass⟩ : ℝ × ℝ)) F (𝓝 (⟨nndist f 0, 0⟩)), { refine (prod.tendsto_iff _ _).mpr ⟨tendsto_const_nhds, _⟩, exact (nnreal.continuous_coe.tendsto 0).comp mass_lim, }, have key := tendsto_mul.comp lim_pair, rwa mul_zero at key, end /-- If the total masses of finite measures tend to zero, then the measures tend to zero. -/ lemma tendsto_zero_of_tendsto_zero_mass {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} (mass_lim : tendsto (λ i, (μs i).mass) F (𝓝 0)) : tendsto μs F (𝓝 0) := begin rw tendsto_iff_forall_test_against_nn_tendsto, intro f, convert tendsto_zero_test_against_nn_of_tendsto_zero_mass mass_lim f, rw [zero.test_against_nn_apply], end /-- A characterization of weak convergence in terms of integrals of bounded continuous nonnegative functions. -/ theorem tendsto_iff_forall_lintegral_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} {μ : finite_measure α} : tendsto μs F (𝓝 μ) ↔ ∀ (f : α →ᵇ ℝ≥0), tendsto (λ i, (∫⁻ x, (f x) ∂(μs(i) : measure α))) F (𝓝 ((∫⁻ x, (f x) ∂(μ : measure α)))) := begin rw tendsto_iff_forall_to_weak_dual_bcnn_tendsto, simp_rw [to_weak_dual_bcnn_apply _ _, ←test_against_nn_coe_eq, ennreal.tendsto_coe, ennreal.to_nnreal_coe], end end finite_measure -- section section finite_measure_bounded_convergence /-! ### Bounded convergence results for finite measures This section is about bounded convergence theorems for finite measures. -/ variables {α : Type*} [measurable_space α] [topological_space α] [opens_measurable_space α] /-- A bounded convergence theorem for a finite measure: If bounded continuous non-negative functions are uniformly bounded by a constant and tend to a limit, then their integrals against the finite measure tend to the integral of the limit. This formulation assumes: * the functions tend to a limit along a countably generated filter; * the limit is in the almost everywhere sense; * boundedness holds almost everywhere; * integration is `measure_theory.lintegral`, i.e., the functions and their integrals are `ℝ≥0∞`-valued. -/ lemma tendsto_lintegral_nn_filter_of_le_const {ι : Type*} {L : filter ι} [L.is_countably_generated] (μ : measure α) [is_finite_measure μ] {fs : ι → (α →ᵇ ℝ≥0)} {c : ℝ≥0} (fs_le_const : ∀ᶠ i in L, ∀ᵐ (a : α) ∂(μ : measure α), fs i a ≤ c) {f : α → ℝ≥0} (fs_lim : ∀ᵐ (a : α) ∂(μ : measure α), tendsto (λ i, fs i a) L (𝓝 (f a))) : tendsto (λ i, (∫⁻ a, fs i a ∂μ)) L (𝓝 (∫⁻ a, (f a) ∂μ)) := begin simpa only using tendsto_lintegral_filter_of_dominated_convergence (λ _, c) (eventually_of_forall ((λ i, (ennreal.continuous_coe.comp (fs i).continuous).measurable))) _ ((@lintegral_const_lt_top _ _ (μ : measure α) _ _ (@ennreal.coe_ne_top c)).ne) _, { simpa only [ennreal.coe_le_coe] using fs_le_const, }, { simpa only [ennreal.tendsto_coe] using fs_lim, }, end /-- A bounded convergence theorem for a finite measure: If a sequence of bounded continuous non-negative functions are uniformly bounded by a constant and tend pointwise to a limit, then their integrals (`measure_theory.lintegral`) against the finite measure tend to the integral of the limit. A related result with more general assumptions is `measure_theory.finite_measure.tendsto_lintegral_nn_filter_of_le_const`. -/ lemma tendsto_lintegral_nn_of_le_const (μ : finite_measure α) {fs : ℕ → (α →ᵇ ℝ≥0)} {c : ℝ≥0} (fs_le_const : ∀ n a, fs n a ≤ c) {f : α → ℝ≥0} (fs_lim : ∀ a, tendsto (λ n, fs n a) at_top (𝓝 (f a))) : tendsto (λ n, (∫⁻ a, fs n a ∂(μ : measure α))) at_top (𝓝 (∫⁻ a, (f a) ∂(μ : measure α))) := tendsto_lintegral_nn_filter_of_le_const μ (eventually_of_forall (λ n, eventually_of_forall (fs_le_const n))) (eventually_of_forall fs_lim) /-- A bounded convergence theorem for a finite measure: If bounded continuous non-negative functions are uniformly bounded by a constant and tend to a limit, then their integrals against the finite measure tend to the integral of the limit. This formulation assumes: * the functions tend to a limit along a countably generated filter; * the limit is in the almost everywhere sense; * boundedness holds almost everywhere; * integration is the pairing against non-negative continuous test functions (`measure_theory.finite_measure.test_against_nn`). A related result using `measure_theory.lintegral` for integration is `measure_theory.finite_measure.tendsto_lintegral_nn_filter_of_le_const`. -/ lemma tendsto_test_against_nn_filter_of_le_const {ι : Type*} {L : filter ι} [L.is_countably_generated] {μ : finite_measure α} {fs : ι → (α →ᵇ ℝ≥0)} {c : ℝ≥0} (fs_le_const : ∀ᶠ i in L, ∀ᵐ (a : α) ∂(μ : measure α), fs i a ≤ c) {f : α →ᵇ ℝ≥0} (fs_lim : ∀ᵐ (a : α) ∂(μ : measure α), tendsto (λ i, fs i a) L (𝓝 (f a))) : tendsto (λ i, μ.test_against_nn (fs i)) L (𝓝 (μ.test_against_nn f)) := begin apply (ennreal.tendsto_to_nnreal (lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : measure α) f).ne).comp, exact tendsto_lintegral_nn_filter_of_le_const μ fs_le_const fs_lim, end /-- A bounded convergence theorem for a finite measure: If a sequence of bounded continuous non-negative functions are uniformly bounded by a constant and tend pointwise to a limit, then their integrals (`measure_theory.finite_measure.test_against_nn`) against the finite measure tend to the integral of the limit. Related results: * `measure_theory.finite_measure.tendsto_test_against_nn_filter_of_le_const`: more general assumptions * `measure_theory.finite_measure.tendsto_lintegral_nn_of_le_const`: using `measure_theory.lintegral` for integration. -/ lemma tendsto_test_against_nn_of_le_const {μ : finite_measure α} {fs : ℕ → (α →ᵇ ℝ≥0)} {c : ℝ≥0} (fs_le_const : ∀ n a, fs n a ≤ c) {f : α →ᵇ ℝ≥0} (fs_lim : ∀ a, tendsto (λ n, fs n a) at_top (𝓝 (f a))) : tendsto (λ n, μ.test_against_nn (fs n)) at_top (𝓝 (μ.test_against_nn f)) := tendsto_test_against_nn_filter_of_le_const (eventually_of_forall (λ n, eventually_of_forall (fs_le_const n))) (eventually_of_forall fs_lim) end finite_measure_bounded_convergence -- section section finite_measure_convergence_by_bounded_continuous_functions /-! ### Weak convergence of finite measures with bounded continuous real-valued functions In this section we characterize the weak convergence of finite measures by the usual (defining) condition that the integrals of all bounded continuous real-valued functions converge. -/ variables {α : Type*} [measurable_space α] [topological_space α] [opens_measurable_space α] lemma integrable_of_bounded_continuous_to_nnreal (μ : measure α) [is_finite_measure μ] (f : α →ᵇ ℝ≥0) : integrable ((coe : ℝ≥0 → ℝ) ∘ ⇑f) μ := begin refine ⟨(nnreal.continuous_coe.comp f.continuous).measurable.ae_strongly_measurable, _⟩, simp only [has_finite_integral, nnreal.nnnorm_eq], exact lintegral_lt_top_of_bounded_continuous_to_nnreal _ f, end lemma integrable_of_bounded_continuous_to_real (μ : measure α) [is_finite_measure μ] (f : α →ᵇ ℝ) : integrable ⇑f μ := begin refine ⟨f.continuous.measurable.ae_strongly_measurable, _⟩, have aux : (coe : ℝ≥0 → ℝ) ∘ ⇑f.nnnorm = (λ x, ∥f x∥), { ext x, simp only [function.comp_app, bounded_continuous_function.nnnorm_coe_fun_eq, coe_nnnorm], }, apply (has_finite_integral_iff_norm ⇑f).mpr, rw ← of_real_integral_eq_lintegral_of_real, { exact ennreal.of_real_lt_top, }, { exact aux ▸ integrable_of_bounded_continuous_to_nnreal μ f.nnnorm, }, { exact eventually_of_forall (λ x, norm_nonneg (f x)), }, end lemma _root_.bounded_continuous_function.integral_eq_integral_nnreal_part_sub (μ : measure α) [is_finite_measure μ] (f : α →ᵇ ℝ) : ∫ x, f x ∂μ = ∫ x, f.nnreal_part x ∂μ - ∫ x, (-f).nnreal_part x ∂μ := by simp only [f.self_eq_nnreal_part_sub_nnreal_part_neg, pi.sub_apply, integral_sub, integrable_of_bounded_continuous_to_nnreal] lemma lintegral_lt_top_of_bounded_continuous_to_real {α : Type*} [measurable_space α] [topological_space α] (μ : measure α) [is_finite_measure μ] (f : α →ᵇ ℝ) : ∫⁻ x, ennreal.of_real (f x) ∂μ < ∞ := lintegral_lt_top_of_bounded_continuous_to_nnreal _ f.nnreal_part theorem tendsto_of_forall_integral_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} {μ : finite_measure α} (h : (∀ (f : α →ᵇ ℝ), tendsto (λ i, (∫ x, (f x) ∂(μs i : measure α))) F (𝓝 ((∫ x, (f x) ∂(μ : measure α)))))) : tendsto μs F (𝓝 μ) := begin apply (@tendsto_iff_forall_lintegral_tendsto α _ _ _ γ F μs μ).mpr, intro f, have key := @ennreal.tendsto_to_real_iff _ F _ (λ i, (lintegral_lt_top_of_bounded_continuous_to_nnreal (μs i : measure α) f).ne) _ (lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : measure α) f).ne, simp only [ennreal.of_real_coe_nnreal] at key, apply key.mp, have lip : lipschitz_with 1 (coe : ℝ≥0 → ℝ), from isometry_subtype_coe.lipschitz, set f₀ := bounded_continuous_function.comp _ lip f with def_f₀, have f₀_eq : ⇑f₀ = (coe : ℝ≥0 → ℝ) ∘ ⇑f, by refl, have f₀_nn : 0 ≤ ⇑f₀, from λ _, by simp only [f₀_eq, pi.zero_apply, nnreal.zero_le_coe], have f₀_ae_nn : 0 ≤ᵐ[(μ : measure α)] ⇑f₀, from eventually_of_forall f₀_nn, have f₀_ae_nns : ∀ i, 0 ≤ᵐ[(μs i : measure α)] ⇑f₀, from λ i, eventually_of_forall f₀_nn, have aux := integral_eq_lintegral_of_nonneg_ae f₀_ae_nn f₀.continuous.measurable.ae_strongly_measurable, have auxs := λ i, integral_eq_lintegral_of_nonneg_ae (f₀_ae_nns i) f₀.continuous.measurable.ae_strongly_measurable, simp only [f₀_eq, ennreal.of_real_coe_nnreal] at aux auxs, simpa only [←aux, ←auxs] using h f₀, end lemma _root_.bounded_continuous_function.nnreal.to_real_lintegral_eq_integral (f : α →ᵇ ℝ≥0) (μ : measure α) : (∫⁻ x, (f x : ℝ≥0∞) ∂μ).to_real = (∫ x, f x ∂μ) := begin rw integral_eq_lintegral_of_nonneg_ae _ (nnreal.continuous_coe.comp f.continuous).measurable.ae_strongly_measurable, { simp only [ennreal.of_real_coe_nnreal], }, { apply eventually_of_forall, simp only [pi.zero_apply, nnreal.zero_le_coe, implies_true_iff], }, end /-- A characterization of weak convergence in terms of integrals of bounded continuous real-valued functions. -/ theorem tendsto_iff_forall_integral_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} {μ : finite_measure α} : tendsto μs F (𝓝 μ) ↔ ∀ (f : α →ᵇ ℝ), tendsto (λ i, (∫ x, (f x) ∂(μs i : measure α))) F (𝓝 ((∫ x, (f x) ∂(μ : measure α)))) := begin refine ⟨_, tendsto_of_forall_integral_tendsto⟩, rw tendsto_iff_forall_lintegral_tendsto, intros h f, simp_rw bounded_continuous_function.integral_eq_integral_nnreal_part_sub, set f_pos := f.nnreal_part with def_f_pos, set f_neg := (-f).nnreal_part with def_f_neg, have tends_pos := (ennreal.tendsto_to_real ((lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : measure α) f_pos).ne)).comp (h f_pos), have tends_neg := (ennreal.tendsto_to_real ((lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : measure α) f_neg).ne)).comp (h f_neg), have aux : ∀ (g : α →ᵇ ℝ≥0), ennreal.to_real ∘ (λ (i : γ), ∫⁻ (x : α), ↑(g x) ∂(μs i : measure α)) = λ (i : γ), (∫⁻ (x : α), ↑(g x) ∂(μs i : measure α)).to_real, from λ _, rfl, simp_rw [aux, bounded_continuous_function.nnreal.to_real_lintegral_eq_integral] at tends_pos tends_neg, exact tendsto.sub tends_pos tends_neg, end end finite_measure_convergence_by_bounded_continuous_functions -- section end finite_measure -- namespace section probability_measure /-! ### Probability measures In this section we define the type of probability measures on a measurable space `α`, denoted by `measure_theory.probability_measure α`. TODO: Probability measures form a convex space. If `α` is moreover a topological space and the sigma algebra on `α` is finer than the Borel sigma algebra (i.e. `[opens_measurable_space α]`), then `measure_theory.probability_measure α` is equipped with the topology of weak convergence of measures. Since every probability measure is a finite measure, this is implemented as the induced topology from the coercion `measure_theory.probability_measure.to_finite_measure`. -/ /-- Probability measures are defined as the subtype of measures that have the property of being probability measures (i.e., their total mass is one). -/ def probability_measure (α : Type*) [measurable_space α] : Type* := {μ : measure α // is_probability_measure μ} namespace probability_measure variables {α : Type*} [measurable_space α] instance [inhabited α] : inhabited (probability_measure α) := ⟨⟨measure.dirac default, measure.dirac.is_probability_measure⟩⟩ /-- A probability measure can be interpreted as a measure. -/ instance : has_coe (probability_measure α) (measure_theory.measure α) := coe_subtype instance : has_coe_to_fun (probability_measure α) (λ _, set α → ℝ≥0) := ⟨λ μ s, (μ s).to_nnreal⟩ instance (μ : probability_measure α) : is_probability_measure (μ : measure α) := μ.prop lemma coe_fn_eq_to_nnreal_coe_fn_to_measure (ν : probability_measure α) : (ν : set α → ℝ≥0) = λ s, ((ν : measure α) s).to_nnreal := rfl @[simp] lemma val_eq_to_measure (ν : probability_measure α) : ν.val = (ν : measure α) := rfl lemma coe_injective : function.injective (coe : probability_measure α → measure α) := subtype.coe_injective @[simp] lemma coe_fn_univ (ν : probability_measure α) : ν univ = 1 := congr_arg ennreal.to_nnreal ν.prop.measure_univ /-- A probability measure can be interpreted as a finite measure. -/ def to_finite_measure (μ : probability_measure α) : finite_measure α := ⟨μ, infer_instance⟩ @[simp] lemma coe_comp_to_finite_measure_eq_coe (ν : probability_measure α) : (ν.to_finite_measure : measure α) = (ν : measure α) := rfl @[simp] lemma coe_fn_comp_to_finite_measure_eq_coe_fn (ν : probability_measure α) : (ν.to_finite_measure : set α → ℝ≥0) = (ν : set α → ℝ≥0) := rfl @[simp] lemma ennreal_coe_fn_eq_coe_fn_to_measure (ν : probability_measure α) (s : set α) : (ν s : ℝ≥0∞) = (ν : measure α) s := by rw [← coe_fn_comp_to_finite_measure_eq_coe_fn, finite_measure.ennreal_coe_fn_eq_coe_fn_to_measure, coe_comp_to_finite_measure_eq_coe] @[ext] lemma extensionality (μ ν : probability_measure α) (h : ∀ (s : set α), measurable_set s → μ s = ν s) : μ = ν := begin ext1, ext1 s s_mble, simpa [ennreal_coe_fn_eq_coe_fn_to_measure] using congr_arg (coe : ℝ≥0 → ℝ≥0∞) (h s s_mble), end @[simp] lemma mass_to_finite_measure (μ : probability_measure α) : μ.to_finite_measure.mass = 1 := μ.coe_fn_univ lemma to_finite_measure_nonzero (μ : probability_measure α) : μ.to_finite_measure ≠ 0 := begin intro maybe_zero, have mass_zero := (finite_measure.mass_zero_iff _).mpr maybe_zero, rw μ.mass_to_finite_measure at mass_zero, exact one_ne_zero mass_zero, end variables [topological_space α] [opens_measurable_space α] lemma test_against_nn_lipschitz (μ : probability_measure α) : lipschitz_with 1 (λ (f : α →ᵇ ℝ≥0), μ.to_finite_measure.test_against_nn f) := μ.mass_to_finite_measure ▸ μ.to_finite_measure.test_against_nn_lipschitz /-- The topology of weak convergence on `measure_theory.probability_measure α`. This is inherited (induced) from the topology of weak convergence of finite measures via the inclusion `measure_theory.probability_measure.to_finite_measure`. -/ instance : topological_space (probability_measure α) := topological_space.induced to_finite_measure infer_instance lemma to_finite_measure_continuous : continuous (to_finite_measure : probability_measure α → finite_measure α) := continuous_induced_dom /-- Probability measures yield elements of the `weak_dual` of bounded continuous nonnegative functions via `measure_theory.finite_measure.test_against_nn`, i.e., integration. -/ def to_weak_dual_bcnn : probability_measure α → weak_dual ℝ≥0 (α →ᵇ ℝ≥0) := finite_measure.to_weak_dual_bcnn ∘ to_finite_measure @[simp] lemma coe_to_weak_dual_bcnn (μ : probability_measure α) : ⇑μ.to_weak_dual_bcnn = μ.to_finite_measure.test_against_nn := rfl @[simp] lemma to_weak_dual_bcnn_apply (μ : probability_measure α) (f : α →ᵇ ℝ≥0) : μ.to_weak_dual_bcnn f = (∫⁻ x, f x ∂(μ : measure α)).to_nnreal := rfl lemma to_weak_dual_bcnn_continuous : continuous (λ (μ : probability_measure α), μ.to_weak_dual_bcnn) := finite_measure.to_weak_dual_bcnn_continuous.comp to_finite_measure_continuous /- Integration of (nonnegative bounded continuous) test functions against Borel probability measures depends continuously on the measure. -/ lemma continuous_test_against_nn_eval (f : α →ᵇ ℝ≥0) : continuous (λ (μ : probability_measure α), μ.to_finite_measure.test_against_nn f) := (finite_measure.continuous_test_against_nn_eval f).comp to_finite_measure_continuous /- The canonical mapping from probability measures to finite measures is an embedding. -/ lemma to_finite_measure_embedding (α : Type*) [measurable_space α] [topological_space α] [opens_measurable_space α] : embedding (to_finite_measure : probability_measure α → finite_measure α) := { induced := rfl, inj := λ μ ν h, subtype.eq (by convert congr_arg coe h) } lemma tendsto_nhds_iff_to_finite_measures_tendsto_nhds {δ : Type*} (F : filter δ) {μs : δ → probability_measure α} {μ₀ : probability_measure α} : tendsto μs F (𝓝 μ₀) ↔ tendsto (to_finite_measure ∘ μs) F (𝓝 (μ₀.to_finite_measure)) := embedding.tendsto_nhds_iff (to_finite_measure_embedding α) /-- A characterization of weak convergence of probability measures by the condition that the integrals of every continuous bounded nonnegative function converge to the integral of the function against the limit measure. -/ theorem tendsto_iff_forall_lintegral_tendsto {γ : Type*} {F : filter γ} {μs : γ → probability_measure α} {μ : probability_measure α} : tendsto μs F (𝓝 μ) ↔ ∀ (f : α →ᵇ ℝ≥0), tendsto (λ i, (∫⁻ x, (f x) ∂(μs(i) : measure α))) F (𝓝 ((∫⁻ x, (f x) ∂(μ : measure α)))) := begin rw tendsto_nhds_iff_to_finite_measures_tendsto_nhds, exact finite_measure.tendsto_iff_forall_lintegral_tendsto, end /-- The characterization of weak convergence of probability measures by the usual (defining) condition that the integrals of every continuous bounded function converge to the integral of the function against the limit measure. -/ theorem tendsto_iff_forall_integral_tendsto {γ : Type*} {F : filter γ} {μs : γ → probability_measure α} {μ : probability_measure α} : tendsto μs F (𝓝 μ) ↔ ∀ (f : α →ᵇ ℝ), tendsto (λ i, (∫ x, (f x) ∂(μs i : measure α))) F (𝓝 ((∫ x, (f x) ∂(μ : measure α)))) := begin rw tendsto_nhds_iff_to_finite_measures_tendsto_nhds, rw finite_measure.tendsto_iff_forall_integral_tendsto, simp only [coe_comp_to_finite_measure_eq_coe], end end probability_measure -- namespace end probability_measure -- section section normalize_finite_measure /-! ### Normalization of finite measures to probability measures This section is about normalizing finite measures to probability measures. The weak convergence of finite measures to nonzero limit measures is characterized by the convergence of the total mass and the convergence of the normalized probability measures. -/ namespace finite_measure variables {α : Type*} [nonempty α] {m0 : measurable_space α} (μ : finite_measure α) /-- Normalize a finite measure so that it becomes a probability measure, i.e., divide by the total mass. -/ def normalize : probability_measure α := if zero : μ.mass = 0 then ⟨measure.dirac ‹nonempty α›.some, measure.dirac.is_probability_measure⟩ else { val := (μ.mass)⁻¹ • μ, property := begin refine ⟨_⟩, simp only [mass, measure.coe_nnreal_smul_apply, ←ennreal_coe_fn_eq_coe_fn_to_measure μ univ], norm_cast, exact inv_mul_cancel zero, end } @[simp] lemma self_eq_mass_mul_normalize (s : set α) : μ s = μ.mass * μ.normalize s := begin by_cases μ = 0, { rw h, simp only [zero.mass, coe_fn_zero, pi.zero_apply, zero_mul], }, have mass_nonzero : μ.mass ≠ 0, by rwa μ.mass_nonzero_iff, simp only [(show μ ≠ 0, from h), mass_nonzero, normalize, not_false_iff, dif_neg], change μ s = μ.mass * ((μ.mass)⁻¹ • μ) s, rw coe_fn_smul_apply, simp only [mass_nonzero, algebra.id.smul_eq_mul, mul_inv_cancel_left₀, ne.def, not_false_iff], end lemma self_eq_mass_smul_normalize : μ = μ.mass • μ.normalize.to_finite_measure := begin ext s s_mble, rw [μ.self_eq_mass_mul_normalize s, coe_fn_smul_apply], refl, end lemma normalize_eq_of_nonzero (nonzero : μ ≠ 0) (s : set α) : μ.normalize s = (μ.mass)⁻¹ * (μ s) := by simp only [μ.self_eq_mass_mul_normalize, μ.mass_nonzero_iff.mpr nonzero, inv_mul_cancel_left₀, ne.def, not_false_iff] lemma normalize_eq_inv_mass_smul_of_nonzero (nonzero : μ ≠ 0) : μ.normalize.to_finite_measure = (μ.mass)⁻¹ • μ := begin nth_rewrite 2 μ.self_eq_mass_smul_normalize, rw ← smul_assoc, simp only [μ.mass_nonzero_iff.mpr nonzero, algebra.id.smul_eq_mul, inv_mul_cancel, ne.def, not_false_iff, one_smul], end lemma coe_normalize_eq_of_nonzero (nonzero : μ ≠ 0) : (μ.normalize : measure α) = (μ.mass)⁻¹ • μ := begin ext1 s s_mble, simp only [← μ.normalize.ennreal_coe_fn_eq_coe_fn_to_measure s, μ.normalize_eq_of_nonzero nonzero s, ennreal.coe_mul, ennreal_coe_fn_eq_coe_fn_to_measure, measure.coe_nnreal_smul_apply], end @[simp] lemma _root_.probability_measure.to_finite_measure_normalize_eq_self {m0 : measurable_space α} (μ : probability_measure α) : μ.to_finite_measure.normalize = μ := begin ext s s_mble, rw μ.to_finite_measure.normalize_eq_of_nonzero μ.to_finite_measure_nonzero s, simp only [probability_measure.mass_to_finite_measure, inv_one, one_mul], refl, end /-- Averaging with respect to a finite measure is the same as integraing against `measure_theory.finite_measure.normalize`. -/ lemma average_eq_integral_normalize {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] (nonzero : μ ≠ 0) (f : α → E) : average (μ : measure α) f = ∫ x, f x ∂(μ.normalize : measure α) := begin rw [μ.coe_normalize_eq_of_nonzero nonzero, average], congr, simp only [ring_hom.to_fun_eq_coe, ennreal.coe_of_nnreal_hom, ennreal.coe_inv (μ.mass_nonzero_iff.mpr nonzero), ennreal_mass], end variables [topological_space α] lemma test_against_nn_eq_mass_mul (f : α →ᵇ ℝ≥0) : μ.test_against_nn f = μ.mass * μ.normalize.to_finite_measure.test_against_nn f := begin nth_rewrite 0 μ.self_eq_mass_smul_normalize, rw μ.normalize.to_finite_measure.smul_test_against_nn_apply μ.mass f, refl, end lemma normalize_test_against_nn (nonzero : μ ≠ 0) (f : α →ᵇ ℝ≥0) : μ.normalize.to_finite_measure.test_against_nn f = (μ.mass)⁻¹ * μ.test_against_nn f := by simp [μ.test_against_nn_eq_mass_mul, μ.mass_nonzero_iff.mpr nonzero] variables [opens_measurable_space α] variables {μ} lemma tendsto_test_against_nn_of_tendsto_normalize_test_against_nn_of_tendsto_mass {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} (μs_lim : tendsto (λ i, (μs i).normalize) F (𝓝 μ.normalize)) (mass_lim : tendsto (λ i, (μs i).mass) F (𝓝 μ.mass)) (f : α →ᵇ ℝ≥0) : tendsto (λ i, (μs i).test_against_nn f) F (𝓝 (μ.test_against_nn f)) := begin by_cases h_mass : μ.mass = 0, { simp only [μ.mass_zero_iff.mp h_mass, zero.test_against_nn_apply, zero.mass, eq_self_iff_true] at *, exact tendsto_zero_test_against_nn_of_tendsto_zero_mass mass_lim f, }, simp_rw [(λ i, (μs i).test_against_nn_eq_mass_mul f), μ.test_against_nn_eq_mass_mul f], rw probability_measure.tendsto_nhds_iff_to_finite_measures_tendsto_nhds at μs_lim, rw tendsto_iff_forall_test_against_nn_tendsto at μs_lim, have lim_pair : tendsto (λ i, (⟨(μs i).mass, (μs i).normalize.to_finite_measure.test_against_nn f⟩ : ℝ≥0 × ℝ≥0)) F (𝓝 (⟨μ.mass, μ.normalize.to_finite_measure.test_against_nn f⟩)), from (prod.tendsto_iff _ _).mpr ⟨mass_lim, μs_lim f⟩, exact tendsto_mul.comp lim_pair, end lemma tendsto_normalize_test_against_nn_of_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} (μs_lim : tendsto μs F (𝓝 μ)) (nonzero : μ ≠ 0) (f : α →ᵇ ℝ≥0) : tendsto (λ i, (μs i).normalize.to_finite_measure.test_against_nn f) F (𝓝 (μ.normalize.to_finite_measure.test_against_nn f)) := begin have lim_mass := μs_lim.mass, have aux : {(0 : ℝ≥0)}ᶜ ∈ 𝓝 (μ.mass), from is_open_compl_singleton.mem_nhds (μ.mass_nonzero_iff.mpr nonzero), have eventually_nonzero : ∀ᶠ i in F, μs i ≠ 0, { simp_rw ← mass_nonzero_iff, exact lim_mass aux, }, have eve : ∀ᶠ i in F, (μs i).normalize.to_finite_measure.test_against_nn f = ((μs i).mass)⁻¹ * (μs i).test_against_nn f, { filter_upwards [eventually_iff.mp eventually_nonzero], intros i hi, apply normalize_test_against_nn _ hi, }, simp_rw [tendsto_congr' eve, μ.normalize_test_against_nn nonzero], have lim_pair : tendsto (λ i, (⟨((μs i).mass)⁻¹, (μs i).test_against_nn f⟩ : ℝ≥0 × ℝ≥0)) F (𝓝 (⟨(μ.mass)⁻¹, μ.test_against_nn f⟩)), { refine (prod.tendsto_iff _ _).mpr ⟨_, _⟩, { exact (continuous_on_inv₀.continuous_at aux).tendsto.comp lim_mass, }, { exact tendsto_iff_forall_test_against_nn_tendsto.mp μs_lim f, }, }, exact tendsto_mul.comp lim_pair, end /-- If the normalized versions of finite measures converge weakly and their total masses also converge, then the finite measures themselves converge weakly. -/ lemma tendsto_of_tendsto_normalize_test_against_nn_of_tendsto_mass {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} (μs_lim : tendsto (λ i, (μs i).normalize) F (𝓝 μ.normalize)) (mass_lim : tendsto (λ i, (μs i).mass) F (𝓝 μ.mass)) : tendsto μs F (𝓝 μ) := begin rw tendsto_iff_forall_test_against_nn_tendsto, exact λ f, tendsto_test_against_nn_of_tendsto_normalize_test_against_nn_of_tendsto_mass μs_lim mass_lim f, end /-- If finite measures themselves converge weakly to a nonzero limit measure, then their normalized versions also converge weakly. -/ lemma tendsto_normalize_of_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} (μs_lim : tendsto μs F (𝓝 μ)) (nonzero : μ ≠ 0) : tendsto (λ i, (μs i).normalize) F (𝓝 (μ.normalize)) := begin rw [probability_measure.tendsto_nhds_iff_to_finite_measures_tendsto_nhds, tendsto_iff_forall_test_against_nn_tendsto], exact λ f, tendsto_normalize_test_against_nn_of_tendsto μs_lim nonzero f, end /-- The weak convergence of finite measures to a nonzero limit can be characterized by the weak convergence of both their normalized versions (probability measures) and their total masses. -/ theorem tendsto_normalize_iff_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure α} (nonzero : μ ≠ 0) : tendsto (λ i, (μs i).normalize) F (𝓝 (μ.normalize)) ∧ tendsto (λ i, (μs i).mass) F (𝓝 (μ.mass)) ↔ tendsto μs F (𝓝 μ) := begin split, { rintros ⟨normalized_lim, mass_lim⟩, exact tendsto_of_tendsto_normalize_test_against_nn_of_tendsto_mass normalized_lim mass_lim, }, { intro μs_lim, refine ⟨tendsto_normalize_of_tendsto μs_lim nonzero, μs_lim.mass⟩, }, end end finite_measure --namespace end normalize_finite_measure -- section section convergence_implies_limsup_closed_le /-! ### Portmanteau implication: weak convergence implies a limsup condition for closed sets In this section we prove, under the assumption that the underlying topological space `α` is pseudo-emetrizable, that the weak convergence of measures on `measure_theory.finite_measure α` implies that for any closed set `F` in `α` the limsup of the measures of `F` is at most the limit measure of `F`. This is one implication of the portmanteau theorem characterizing weak convergence of measures. -/ variables {α : Type*} [measurable_space α] /-- If bounded continuous functions tend to the indicator of a measurable set and are uniformly bounded, then their integrals against a finite measure tend to the measure of the set. This formulation assumes: * the functions tend to a limit along a countably generated filter; * the limit is in the almost everywhere sense; * boundedness holds almost everywhere. -/ lemma measure_of_cont_bdd_of_tendsto_filter_indicator {ι : Type*} {L : filter ι} [L.is_countably_generated] [topological_space α] [opens_measurable_space α] (μ : measure α) [is_finite_measure μ] {c : ℝ≥0} {E : set α} (E_mble : measurable_set E) (fs : ι → (α →ᵇ ℝ≥0)) (fs_bdd : ∀ᶠ i in L, ∀ᵐ (a : α) ∂μ, fs i a ≤ c) (fs_lim : ∀ᵐ (a : α) ∂μ, tendsto (λ (i : ι), (coe_fn : (α →ᵇ ℝ≥0) → (α → ℝ≥0)) (fs i) a) L (𝓝 (indicator E (λ x, (1 : ℝ≥0)) a))) : tendsto (λ n, lintegral μ (λ a, fs n a)) L (𝓝 (μ E)) := begin convert finite_measure.tendsto_lintegral_nn_filter_of_le_const μ fs_bdd fs_lim, have aux : ∀ a, indicator E (λ x, (1 : ℝ≥0∞)) a = ↑(indicator E (λ x, (1 : ℝ≥0)) a), from λ a, by simp only [ennreal.coe_indicator, ennreal.coe_one], simp_rw [←aux, lintegral_indicator _ E_mble], simp only [lintegral_one, measure.restrict_apply, measurable_set.univ, univ_inter], end /-- If a sequence of bounded continuous functions tends to the indicator of a measurable set and the functions are uniformly bounded, then their integrals against a finite measure tend to the measure of the set. A similar result with more general assumptions is `measure_theory.measure_of_cont_bdd_of_tendsto_filter_indicator`. -/ lemma measure_of_cont_bdd_of_tendsto_indicator [topological_space α] [opens_measurable_space α] (μ : measure α) [is_finite_measure μ] {c : ℝ≥0} {E : set α} (E_mble : measurable_set E) (fs : ℕ → (α →ᵇ ℝ≥0)) (fs_bdd : ∀ n a, fs n a ≤ c) (fs_lim : tendsto (λ (n : ℕ), (coe_fn : (α →ᵇ ℝ≥0) → (α → ℝ≥0)) (fs n)) at_top (𝓝 (indicator E (λ x, (1 : ℝ≥0))))) : tendsto (λ n, lintegral μ (λ a, fs n a)) at_top (𝓝 (μ E)) := begin have fs_lim' : ∀ a, tendsto (λ (n : ℕ), (fs n a : ℝ≥0)) at_top (𝓝 (indicator E (λ x, (1 : ℝ≥0)) a)), by { rw tendsto_pi_nhds at fs_lim, exact λ a, fs_lim a, }, apply measure_of_cont_bdd_of_tendsto_filter_indicator μ E_mble fs (eventually_of_forall (λ n, eventually_of_forall (fs_bdd n))) (eventually_of_forall fs_lim'), end /-- The integrals of thickened indicators of a closed set against a finite measure tend to the measure of the closed set if the thickening radii tend to zero. -/ lemma tendsto_lintegral_thickened_indicator_of_is_closed {α : Type*} [measurable_space α] [pseudo_emetric_space α] [opens_measurable_space α] (μ : measure α) [is_finite_measure μ] {F : set α} (F_closed : is_closed F) {δs : ℕ → ℝ} (δs_pos : ∀ n, 0 < δs n) (δs_lim : tendsto δs at_top (𝓝 0)) : tendsto (λ n, lintegral μ (λ a, (thickened_indicator (δs_pos n) F a : ℝ≥0∞))) at_top (𝓝 (μ F)) := begin apply measure_of_cont_bdd_of_tendsto_indicator μ F_closed.measurable_set (λ n, thickened_indicator (δs_pos n) F) (λ n a, thickened_indicator_le_one (δs_pos n) F a), have key := thickened_indicator_tendsto_indicator_closure δs_pos δs_lim F, rwa F_closed.closure_eq at key, end /-- One implication of the portmanteau theorem: Weak convergence of finite measures implies that the limsup of the measures of any closed set is at most the measure of the closed set under the limit measure. -/ lemma finite_measure.limsup_measure_closed_le_of_tendsto {α ι : Type*} {L : filter ι} [measurable_space α] [pseudo_emetric_space α] [opens_measurable_space α] {μ : finite_measure α} {μs : ι → finite_measure α} (μs_lim : tendsto μs L (𝓝 μ)) {F : set α} (F_closed : is_closed F) : L.limsup (λ i, (μs i : measure α) F) ≤ (μ : measure α) F := begin by_cases L = ⊥, { simp only [h, limsup, filter.map_bot, Limsup_bot, ennreal.bot_eq_zero, zero_le], }, apply ennreal.le_of_forall_pos_le_add, intros ε ε_pos μ_F_finite, set δs := λ (n : ℕ), (1 : ℝ) / (n+1) with def_δs, have δs_pos : ∀ n, 0 < δs n, from λ n, nat.one_div_pos_of_nat, have δs_lim : tendsto δs at_top (𝓝 0), from tendsto_one_div_add_at_top_nhds_0_nat, have key₁ := tendsto_lintegral_thickened_indicator_of_is_closed (μ : measure α) F_closed δs_pos δs_lim, have room₁ : (μ : measure α) F < (μ : measure α) F + ε / 2, { apply ennreal.lt_add_right (measure_lt_top (μ : measure α) F).ne ((ennreal.div_pos_iff.mpr ⟨(ennreal.coe_pos.mpr ε_pos).ne.symm, ennreal.two_ne_top⟩).ne.symm), }, rcases eventually_at_top.mp (eventually_lt_of_tendsto_lt room₁ key₁) with ⟨M, hM⟩, have key₂ := finite_measure.tendsto_iff_forall_lintegral_tendsto.mp μs_lim (thickened_indicator (δs_pos M) F), have room₂ : lintegral (μ : measure α) (λ a, thickened_indicator (δs_pos M) F a) < lintegral (μ : measure α) (λ a, thickened_indicator (δs_pos M) F a) + ε / 2, { apply ennreal.lt_add_right (lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : measure α) _).ne ((ennreal.div_pos_iff.mpr ⟨(ennreal.coe_pos.mpr ε_pos).ne.symm, ennreal.two_ne_top⟩).ne.symm), }, have ev_near := eventually.mono (eventually_lt_of_tendsto_lt room₂ key₂) (λ n, le_of_lt), have aux := λ n, le_trans (measure_le_lintegral_thickened_indicator (μs n : measure α) F_closed.measurable_set (δs_pos M)), have ev_near' := eventually.mono ev_near aux, apply (filter.limsup_le_limsup ev_near').trans, haveI : ne_bot L, from ⟨h⟩, rw limsup_const, apply le_trans (add_le_add (hM M rfl.le).le (le_refl (ε/2 : ℝ≥0∞))), simp only [add_assoc, ennreal.add_halves, le_refl], end end convergence_implies_limsup_closed_le --section end measure_theory --namespace
af4f7f327a1f013df21d53d094cd3a8240af79dc
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/topology/nhds_set.lean
9b4dded88b23ab3fad39229cab9b2efed7d0e39a
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
3,971
lean
/- Copyright (c) 2022 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Patrick Massot -/ import topology.basic /-! # Neighborhoods of a set In this file we define the filter `𝓝ˢ s` or `nhds_set s` consisting of all neighborhoods of a set `s`. ## Main Properties There are a couple different notions equivalent to `s ∈ 𝓝ˢ t`: * `s ⊆ interior t` using `subset_interior_iff_mem_nhds_set` * `∀ (x : α), x ∈ t → s ∈ 𝓝 x` using `mem_nhds_set_iff_forall` * `∃ U : set α, is_open U ∧ t ⊆ U ∧ U ⊆ s` using `mem_nhds_set_iff_exists` Furthermore, we have the following results: * `monotone_nhds_set`: `𝓝ˢ` is monotone * In T₁-spaces, `𝓝ˢ`is strictly monotone and hence injective: `strict_mono_nhds_set`/`injective_nhds_set`. These results are in `topology.separation`. -/ open set filter open_locale topological_space filter variables {α β : Type*} [topological_space α] [topological_space β] {s t s₁ s₂ t₁ t₂ : set α} {x : α} /-- The filter of neighborhoods of a set in a topological space. -/ def nhds_set (s : set α) : filter α := Sup (nhds '' s) localized "notation `𝓝ˢ` := nhds_set" in topological_space lemma mem_nhds_set_iff_forall : s ∈ 𝓝ˢ t ↔ ∀ (x : α), x ∈ t → s ∈ 𝓝 x := by simp_rw [nhds_set, filter.mem_Sup, ball_image_iff] lemma subset_interior_iff_mem_nhds_set : s ⊆ interior t ↔ t ∈ 𝓝ˢ s := by simp_rw [mem_nhds_set_iff_forall, subset_interior_iff_nhds] lemma mem_nhds_set_iff_exists : s ∈ 𝓝ˢ t ↔ ∃ U : set α, is_open U ∧ t ⊆ U ∧ U ⊆ s := by { rw [← subset_interior_iff_mem_nhds_set, subset_interior_iff] } lemma has_basis_nhds_set (s : set α) : (𝓝ˢ s).has_basis (λ U, is_open U ∧ s ⊆ U) (λ U, U) := ⟨λ t, by simp [mem_nhds_set_iff_exists, and_assoc]⟩ lemma is_open.mem_nhds_set (hU : is_open s) : s ∈ 𝓝ˢ t ↔ t ⊆ s := by rw [← subset_interior_iff_mem_nhds_set, interior_eq_iff_open.mpr hU] lemma principal_le_nhds_set : 𝓟 s ≤ 𝓝ˢ s := λ s hs, (subset_interior_iff_mem_nhds_set.mpr hs).trans interior_subset @[simp] lemma nhds_set_eq_principal_iff : 𝓝ˢ s = 𝓟 s ↔ is_open s := by rw [← principal_le_nhds_set.le_iff_eq, le_principal_iff, mem_nhds_set_iff_forall, is_open_iff_mem_nhds] alias nhds_set_eq_principal_iff ↔ _ is_open.nhds_set_eq @[simp] lemma nhds_set_interior : 𝓝ˢ (interior s) = 𝓟 (interior s) := is_open_interior.nhds_set_eq @[simp] lemma nhds_set_singleton : 𝓝ˢ {x} = 𝓝 x := by { ext, rw [← subset_interior_iff_mem_nhds_set, ← mem_interior_iff_mem_nhds, singleton_subset_iff] } lemma mem_nhds_set_interior : s ∈ 𝓝ˢ (interior s) := subset_interior_iff_mem_nhds_set.mp subset.rfl @[simp] lemma nhds_set_empty : 𝓝ˢ (∅ : set α) = ⊥ := by rw [is_open_empty.nhds_set_eq, principal_empty] lemma mem_nhds_set_empty : s ∈ 𝓝ˢ (∅ : set α) := by simp @[simp] lemma nhds_set_univ : 𝓝ˢ (univ : set α) = ⊤ := by rw [is_open_univ.nhds_set_eq, principal_univ] lemma monotone_nhds_set : monotone (𝓝ˢ : set α → filter α) := λ s t hst, Sup_le_Sup $ image_subset _ hst @[simp] lemma nhds_set_union (s t : set α) : 𝓝ˢ (s ∪ t) = 𝓝ˢ s ⊔ 𝓝ˢ t := by simp only [nhds_set, image_union, Sup_union] lemma union_mem_nhds_set (h₁ : s₁ ∈ 𝓝ˢ t₁) (h₂ : s₂ ∈ 𝓝ˢ t₂) : s₁ ∪ s₂ ∈ 𝓝ˢ (t₁ ∪ t₂) := by { rw nhds_set_union, exact union_mem_sup h₁ h₂ } /-- Preimage of a set neighborhood of `t` under a continuous map `f` is a set neighborhood of `s` provided that `f` maps `s` to `t`. -/ lemma continuous.tendsto_nhds_set {f : α → β} {t : set β} (hf : continuous f) (hst : maps_to f s t) : tendsto f (𝓝ˢ s) (𝓝ˢ t) := ((has_basis_nhds_set s).tendsto_iff (has_basis_nhds_set t)).mpr $ λ U hU, ⟨f ⁻¹' U, ⟨hU.1.preimage hf, hst.mono subset.rfl hU.2⟩, λ x, id⟩
864f8f7a6b011a35a9701476dbbf3bcc8265e72c
54c9ed381c63410c9b6af3b0a1722c41152f037f
/experiments/metacheck/MetaCheck.lean
9b6601a9045265c2300210298a963ce7390d8779
[ "Apache-2.0" ]
permissive
dselsam/binport
0233f1aa961a77c4fc96f0dccc780d958c5efc6c
aef374df0e169e2c3f1dc911de240c076315805c
refs/heads/master
1,687,453,448,108
1,627,483,296,000
1,627,483,296,000
333,825,622
0
0
null
null
null
null
UTF-8
Lean
false
false
4,430
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam -/ import Lean import Std.Data.HashSet open Lean Lean.Meta open Std (HashSet mkHashSet) namespace MetaCheckExperiment def BLACK_LIST : HashSet Name := ({} : HashSet Name) def WHITE_LIST : HashSet Name := ({} : HashSet Name) inductive MetaCheckResult where | success : MetaCheckResult | failed : MetaCheckResult | timeout : MetaCheckResult | other : MetaCheckResult deriving Inhabited instance : ToString MetaCheckResult := ⟨fun | MetaCheckResult.success => "success" | MetaCheckResult.failed => "failed" | MetaCheckResult.timeout => "timeout" | MetaCheckResult.other => "other" ⟩ structure DataPoint where declName : Name result : MetaCheckResult abbrev Job := Task (Except IO.Error (Array DataPoint)) instance : Inhabited Job := ⟨⟨pure #[]⟩⟩ structure Context where structure State where datapoints : Array DataPoint := #[] abbrev MetaCheckExperimentM := ReaderT Context (StateRefT State MetaM) def emit (datapoint : DataPoint) : MetaCheckExperimentM Unit := modify fun s => { s with datapoints := s.datapoints.push datapoint } def checkTypeValue (declName : Name) (type value : Expr) : MetaCheckExperimentM Unit := do try let typeType ← inferType type -- (sanity test) let valueType ← inferType value let b ← isDefEq type valueType match b with | true => emit { declName := declName, result := MetaCheckResult.success } | false => emit { declName := declName, result := MetaCheckResult.failed } catch ex => let msg ← ex.toMessageData.toString println! "[warn:checkTypeValue:{declName}] {msg}" emit { declName := declName, result := MetaCheckResult.other } def checkConstant (env : Environment) (opts : Options) (cinfo : ConstantInfo) : IO (Array DataPoint) := do let ((_, s), _, _) ← MetaM.toIO ((core.run {}).run {}) { options := opts } { env := env } pure s.datapoints where core : MetaCheckExperimentM (Array DataPoint) := do match cinfo.value? with | some value => checkTypeValue cinfo.name cinfo.type value | _ => pure () (← get).datapoints def runMetaCheckExperiment (env : Environment) (opts : Options) : IO Unit := do let jobs ← env.constants.map₁.foldM (init := #[]) collectJobs let jobs ← env.constants.map₂.foldlM (init := jobs) collectJobs IO.FS.withFile "results.csv" IO.FS.Mode.write fun handle => do println! "-- waiting for {jobs.size} jobs --" let mut i := 0 for (declName, job) in jobs do println! "[{i}] {declName}" (← IO.getStdout).flush i := i + 1 match ← IO.wait job with | Except.ok datapoints => dumpResults handle datapoints | Except.error err => println! "[warn:task] {err}" where collectJobs (jobs : Array (Name × Job)) (name : Name) (cinfo : ConstantInfo) : IO (Array (Name × Job)) := do if !WHITE_LIST.isEmpty && !WHITE_LIST.contains name then return jobs if BLACK_LIST.contains name then return jobs if isPrivateName name then return jobs if name.isInternal then return jobs if not ((`Mathlib).isPrefixOf name) then return jobs jobs.push (name, ← IO.asTask $ checkConstant env opts cinfo) dumpResults handle datapoints := do for ⟨declName, result⟩ in datapoints do handle.putStr s!"{declName} {toString result}\n" end MetaCheckExperiment open MetaCheckExperiment unsafe def withEnvOpts {α : Type} (f : Environment → Options → IO α) : IO α := do let imports := [{ module := `Init : Import }, { module := `Mathlib : Import }] let some LEAN_PATH ← IO.getEnv "LEAN_PATH" | throw (IO.userError "LEAN_PATH not set") initSearchPath LEAN_PATH let opts : Options := {} let opts : Options := opts.setNat `maxHeartbeats 100000 let opts : Options := opts.setNat `synthInstance.maxHeartbeats 50000 let imports : List Import := [ -- { module := `Init : Import }, -- { module := `PrePort : Import }, -- { module := `Lean3Lib.init.data.nat.basic : Import }, { module := `Mathlib : Import } -- { module := `PostPort : Import } ] withImportModules imports (opts := opts) (trustLevel := 0) fun env => f env opts unsafe def main : IO Unit := withEnvOpts runMetaCheckExperiment
231bb3f03de4fc22cdc53765f1fcd31173d763c0
49bd2218ae088932d847f9030c8dbff1c5607bb7
/src/measure_theory/integration.lean
d7c36e66ace9f39804e01cfb49ae4ecda69f510b
[ "Apache-2.0" ]
permissive
FredericLeRoux/mathlib
e8f696421dd3e4edc8c7edb3369421c8463d7bac
3645bf8fb426757e0a20af110d1fdded281d286e
refs/heads/master
1,607,062,870,732
1,578,513,186,000
1,578,513,186,000
231,653,181
0
0
Apache-2.0
1,578,080,327,000
1,578,080,326,000
null
UTF-8
Lean
false
false
55,152
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 Lebesgue integral on `ennreal`. We define simple functions and show that each Borel measurable function on `ennreal` can be approximated by a sequence of simple functions. -/ import algebra.pi_instances measure_theory.measure_space measure_theory.borel_space noncomputable theory open lattice set filter open_locale classical topological_space namespace measure_theory variables {α : Type*} {β : Type*} {γ : Type*} {δ : 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_sn : ∀ x, is_measurable (to_fun ⁻¹' {x})) (finite : (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⟩ @[ext] theorem ext {f g : α →ₛ β} (H : ∀ a, f a = g a) : f = g := by cases f; cases g; congr; exact funext H /-- Range of a simple function `α →ₛ β` as a `finset β`. -/ protected def range (f : α →ₛ β) : finset β := f.finite.to_finset @[simp] theorem mem_range {f : α →ₛ β} {b} : b ∈ f.range ↔ ∃ a, f a = b := finite.mem_to_finset lemma preimage_eq_empty_iff (f : α →ₛ β) (b : β) : f ⁻¹' {b} = ∅ ↔ b ∉ f.range := iff.intro (by simp [set.eq_empty_iff_forall_not_mem, mem_range]) (by simp [set.eq_empty_iff_forall_not_mem, mem_range]) /-- Constant function as a `simple_func`. -/ def const (α) {β} [measurable_space α] (b : β) : α →ₛ β := ⟨λ a, b, λ x, is_measurable.const _, finite_subset (set.finite_singleton b) $ by rintro _ ⟨a, rfl⟩; simp⟩ @[simp] theorem const_apply (a : α) (b : β) : (const α b) a = b := rfl lemma range_const (α) [measurable_space α] [ne : nonempty α] (b : β) : (const α b).range = {b} := begin ext b', simp [mem_range], exact ⟨assume ⟨_, h⟩, h.symm, assume h, ne.elim $ λa, ⟨a, h.symm⟩⟩ end lemma is_measurable_cut (p : α → β → Prop) (f : α →ₛ β) (h : ∀b, is_measurable {a | p a b}) : is_measurable {a | p a (f a)} := begin rw (_ : {a | p a (f a)} = ⋃ b ∈ set.range f, {a | p a b} ∩ f ⁻¹' {b}), { exact is_measurable.bUnion (countable_finite f.finite) (λ b _, is_measurable.inter (h b) (f.measurable_sn _)) }, ext a, simp, exact ⟨λ h, ⟨_, ⟨a, rfl⟩, h, rfl⟩, λ ⟨_, ⟨a', rfl⟩, h', e⟩, e.symm ▸ h'⟩ end theorem preimage_measurable (f : α →ₛ β) (s) : is_measurable (f ⁻¹' s) := is_measurable_cut (λ _ b, b ∈ s) f (λ b, by simp [is_measurable.const]) /-- A simple function is measurable -/ theorem measurable [measurable_space β] (f : α →ₛ β) : measurable f := λ s _, preimage_measurable f s def ite {s : set α} (hs : is_measurable s) (f g : α →ₛ β) : α →ₛ β := ⟨λ a, if a ∈ s then f a else g a, λ x, by letI : measurable_space β := ⊤; exact measurable.if hs f.measurable g.measurable _ trivial, finite_subset (finite_union f.finite g.finite) begin rintro _ ⟨a, rfl⟩, by_cases a ∈ s; simp [h], exacts [or.inl ⟨_, rfl⟩, or.inr ⟨_, rfl⟩] end⟩ @[simp] theorem ite_apply {s : set α} (hs : is_measurable s) (f g : α →ₛ β) (a) : ite hs f g a = if a ∈ s then f a else g a := rfl /-- 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, is_measurable_cut (λa b, g b a ∈ ({c} : set γ)) f (λ b, (g b).measurable_sn c), finite_subset (finite_bUnion f.finite (λ b, (g b).finite)) $ by rintro _ ⟨a, rfl⟩; simp; exact ⟨_, ⟨a, rfl⟩, _, rfl⟩⟩ @[simp] theorem bind_apply (f : α →ₛ β) (g : β → α →ₛ γ) (a) : f.bind g a = g (f a) a := rfl /-- 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 [has_zero β] (f : α →ₛ β) (s : set α) : α →ₛ β := if hs : is_measurable s then ite hs f (const α 0) else const α 0 @[simp] theorem restrict_apply [has_zero β] (f : α →ₛ β) {s : set α} (hs : is_measurable s) (a) : restrict f s a = if a ∈ s then f a else 0 := by unfold_coes; simp [restrict, hs]; apply ite_apply hs theorem restrict_preimage [has_zero β] (f : α →ₛ β) {s : set α} (hs : is_measurable s) {t : set β} (ht : (0:β) ∉ t) : restrict f s ⁻¹' t = s ∩ f ⁻¹' t := by ext a; dsimp [preimage]; rw [restrict_apply]; by_cases a ∈ s; simp [h, hs, ht] /-- 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) @[simp] 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 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 := begin ext c, simp only [mem_range, exists_prop, mem_range, finset.mem_image, map_apply], split, { rintros ⟨a, rfl⟩, exact ⟨f a, ⟨_, rfl⟩, rfl⟩ }, { rintros ⟨_, ⟨a, rfl⟩, rfl⟩, exact ⟨_, rfl⟩ } end lemma map_preimage (f : α →ₛ β) (g : β → γ) (s : set γ) : (f.map g) ⁻¹' s = (⋃b∈f.range.filter (λb, g b ∈ s), f ⁻¹' {b}) := begin /- True because `f` only takes finitely many values. -/ ext a', simp only [mem_Union, set.mem_preimage, exists_prop, set.mem_preimage, map_apply, finset.mem_filter, mem_range, mem_singleton_iff, exists_eq_right'], split, { assume eq, exact ⟨⟨_, rfl⟩, eq⟩ }, { rintros ⟨_, eq⟩, exact eq } end lemma map_preimage_singleton (f : α →ₛ β) (g : β → γ) (c : γ) : (f.map g) ⁻¹' {c} = (⋃b∈f.range.filter (λb, g b = c), f ⁻¹' {b}) := begin rw map_preimage, have : (λb, g b = c) = λb, g b ∈ _root_.singleton c, funext, rw [eq_iff_iff, mem_singleton_iff], rw this end /-- 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 ← prod_singleton_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] lemma sup_apply [has_sup β] (f g : α →ₛ β) (a : α) : (f ⊔ g) a = f a ⊔ g a := rfl @[simp] 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 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 instance [add_monoid β] : add_monoid (α →ₛ β) := { add := (+), zero := 0, add_assoc := assume f g h, ext (assume a, add_assoc _ _ _), zero_add := assume f, ext (assume a, zero_add _), add_zero := assume f, ext (assume a, add_zero _) } instance add_comm_monoid [add_comm_monoid β] : add_comm_monoid (α →ₛ β) := { add_comm := λ f g, ext (λa, add_comm _ _), .. simple_func.add_monoid } instance [has_neg β] : has_neg (α →ₛ β) := ⟨λf, f.map (has_neg.neg)⟩ instance [add_group β] : add_group (α →ₛ β) := { neg := has_neg.neg, add_left_neg := λf, ext (λa, add_left_neg _), .. simple_func.add_monoid } instance [add_comm_group β] : add_comm_group (α →ₛ β) := { add_comm := λ f g, ext (λa, add_comm _ _) , .. simple_func.add_group } variables {K : Type*} instance [has_scalar K β] : has_scalar K (α →ₛ β) := ⟨λk f, f.map (λb, k • b)⟩ instance [semiring K] [add_comm_monoid β] [semimodule K β] : semimodule K (α →ₛ β) := { one_smul := λ f, ext (λa, one_smul _ _), mul_smul := λ x y f, ext (λa, mul_smul _ _ _), smul_add := λ r f g, ext (λa, smul_add _ _ _), smul_zero := λ r, ext (λa, smul_zero _), add_smul := λ r s f, ext (λa, add_smul _ _ _), zero_smul := λ f, ext (λa, zero_smul _ _) } instance [ring K] [add_comm_group β] [module K β] : module K (α →ₛ β) := { .. simple_func.semimodule } instance [discrete_field K] [add_comm_group β] [module K β] : vector_space K (α →ₛ β) := { .. simple_func.module } lemma smul_apply [has_scalar K β] (k : K) (f : α →ₛ β) (a : α) : (k • f) a = k • f a := rfl lemma smul_eq_map [has_scalar K β] (k : K) (f : α →ₛ β) : k • f = f.map (λb, k • b) := 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.lattice.semilattice_sup,.. simple_func.lattice.order_bot } instance [lattice β] : lattice (α →ₛ β) := { .. simple_func.lattice.semilattice_sup,.. simple_func.lattice.semilattice_inf } instance [bounded_lattice β] : bounded_lattice (α →ₛ β) := { .. simple_func.lattice.lattice, .. simple_func.lattice.order_bot, .. simple_func.lattice.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 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 `β = ennreal` 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 β] [ordered_topology β] {i : ℕ → β} {f : α → β} {n : ℕ} (a : α) (hf : _root_.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.preimage $ is_measurable_of_is_closed $ is_closed_ge' _) 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 β] [ordered_topology β] [measurable_space γ] {i : ℕ → β} {f : γ → β} {g : α → γ} {n : ℕ} (a : α) (hf : _root_.measurable f) (hg : _root_.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 β] [ordered_topology β] [has_zero β] (i : ℕ → β) (f : α → β) (a : α) (hf : _root_.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 `ennreal`s such that its range is the set of non-negative rational numbers. -/ def ennreal_rat_embed (n : ℕ) : ennreal := nnreal.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 def eapprox : (α → ennreal) → ℕ → α →ₛ ennreal := approx ennreal_rat_embed lemma monotone_eapprox (f : α → ennreal) : monotone (eapprox f) := monotone_approx _ f lemma supr_eapprox_apply (f : α → ennreal) (hf : _root_.measurable f) (a : α) : (⨆n, (eapprox f n : α →ₛ ennreal) 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 : ennreal) ≤ (⨆ (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 : γ → ennreal} {g : α → γ} {n : ℕ} (hf : _root_.measurable f) (hg : _root_.measurable g) : (eapprox (f ∘ g) n : α → ennreal) = (eapprox f n : γ →ₛ ennreal) ∘ g := funext $ assume a, approx_comp a hf hg end eapprox end measurable section measure variables [measure_space α] lemma volume_bUnion_preimage (s : finset β) (f : α →ₛ β) : volume (⋃b ∈ s, f ⁻¹' {b}) = s.sum (λb, volume (f ⁻¹' {b})) := begin /- Taking advantage of the fact that `f ⁻¹' {b}` are disjoint for `b ∈ s`. -/ rw [volume_bUnion_finset], { simp only [pairwise_on, (on), finset.mem_coe, ne.def], rintros _ _ _ _ ne _ ⟨h₁, h₂⟩, simp only [mem_singleton_iff, mem_preimage] at h₁ h₂, rw [← h₁, h₂] at ne, exact ne rfl }, exact assume a ha, preimage_measurable _ _ end /-- Integral of a simple function whose codomain is `ennreal`. -/ def integral (f : α →ₛ ennreal) : ennreal := f.range.sum (λ x, x * volume (f ⁻¹' {x})) /-- Calculate the integral of `(g ∘ f)`, where `g : β → ennreal` and `f : α →ₛ β`. -/ lemma map_integral (g : β → ennreal) (f : α →ₛ β) : (f.map g).integral = f.range.sum (λ x, g x * volume (f ⁻¹' {x})) := begin simp only [integral, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, rw [map_preimage_singleton, volume_bUnion_preimage, finset.mul_sum], refine finset.sum_congr _ _, { congr }, { assume x, simp only [finset.mem_filter], rintro ⟨_, h⟩, rw h } end lemma zero_integral : (0 : α →ₛ ennreal).integral = 0 := begin refine (finset.sum_eq_zero_iff_of_nonneg $ assume _ _, zero_le _).2 _, assume r hr, rcases mem_range.1 hr with ⟨a, rfl⟩, exact zero_mul _ end lemma add_integral (f g : α →ₛ ennreal) : (f + g).integral = f.integral + g.integral := calc (f + g).integral = (pair f g).range.sum (λx, x.1 * volume (pair f g ⁻¹' {x}) + x.2 * volume (pair f g ⁻¹' {x})) : by rw [add_eq_map₂, map_integral]; exact finset.sum_congr rfl (assume a ha, add_mul _ _ _) ... = (pair f g).range.sum (λx, x.1 * volume (pair f g ⁻¹' {x})) + (pair f g).range.sum (λx, x.2 * volume (pair f g ⁻¹' {x})) : by rw [finset.sum_add_distrib] ... = ((pair f g).map prod.fst).integral + ((pair f g).map prod.snd).integral : by rw [map_integral, map_integral] ... = integral f + integral g : rfl lemma const_mul_integral (f : α →ₛ ennreal) (x : ennreal) : (const α x * f).integral = x * f.integral := calc (f.map (λa, x * a)).integral = f.range.sum (λr, x * r * volume (f ⁻¹' {r})) : by rw [map_integral] ... = f.range.sum (λr, x * (r * volume (f ⁻¹' {r}))) : finset.sum_congr rfl (assume a ha, mul_assoc _ _ _) ... = x * f.integral : finset.mul_sum.symm lemma mem_restrict_range [has_zero β] {r : β} {s : set α} {f : α →ₛ β} (hs : is_measurable s) : r ∈ (restrict f s).range ↔ (r = 0 ∧ s ≠ univ) ∨ (∃a∈s, f a = r) := begin simp only [mem_range, restrict_apply, hs], split, { rintros ⟨a, ha⟩, split_ifs at ha, { exact or.inr ⟨a, h, ha⟩ }, { exact or.inl ⟨ha.symm, assume eq, h $ eq.symm ▸ trivial⟩ } }, { rintros (⟨rfl, h⟩ | ⟨a, ha, rfl⟩), { have : ¬ ∀a, a ∈ s := assume this, h $ eq_univ_of_forall this, rcases not_forall.1 this with ⟨a, ha⟩, refine ⟨a, _⟩, rw [if_neg ha] }, { refine ⟨a, _⟩, rw [if_pos ha] } } end lemma restrict_preimage' {r : ennreal} {s : set α} (f : α →ₛ ennreal) (hs : is_measurable s) (hr : r ≠ 0) : (restrict f s) ⁻¹' {r} = (f ⁻¹' {r} ∩ s) := begin ext a, by_cases a ∈ s; simp [hs, h, hr.symm] end lemma restrict_integral (f : α →ₛ ennreal) (s : set α) (hs : is_measurable s) : (restrict f s).integral = f.range.sum (λr, r * volume (f ⁻¹' {r} ∩ s)) := begin refine finset.sum_bij_ne_zero (λr _ _, r) _ _ _ _, { assume r hr, rcases (mem_restrict_range hs).1 hr with ⟨rfl, h⟩ | ⟨a, ha, rfl⟩, { simp }, { assume _, exact mem_range.2 ⟨a, rfl⟩ } }, { assume a b _ _ _ _ h, exact h }, { assume r hr, by_cases r0 : r = 0, { simp [r0] }, assume h0, rcases mem_range.1 hr with ⟨a, rfl⟩, have : f ⁻¹' {f a} ∩ s ≠ ∅, { assume h, simpa [h] using h0 }, rcases ne_empty_iff_exists_mem.1 this with ⟨a', eq', ha'⟩, refine ⟨_, (mem_restrict_range hs).2 (or.inr ⟨a', ha', _⟩), _, rfl⟩, { simpa using eq' }, { rwa [restrict_preimage' _ hs r0] } }, { assume r hr ne, by_cases r = 0, { simp [h] }, rw [restrict_preimage' _ hs h] } end lemma restrict_const_integral (c : ennreal) (s : set α) (hs : is_measurable s) : (restrict (const α c) s).integral = c * volume s := have (@const α ennreal _ c) ⁻¹' {c} = univ, begin refine eq_univ_of_forall (assume a, _), simp, end, calc (restrict (const α c) s).integral = c * volume ((const α c) ⁻¹' {c} ∩ s) : begin rw [restrict_integral (const α c) s hs], refine finset.sum_eq_single c _ _, { assume r hr, rcases mem_range.1 hr with ⟨a, rfl⟩, contradiction }, { by_cases nonempty α, { assume ne, rcases h with ⟨a⟩, exfalso, exact ne (mem_range.2 ⟨a, rfl⟩) }, { assume empty, have : (@const α ennreal _ c) ⁻¹' {c} ∩ s = ∅, { ext a, exfalso, exact h ⟨a⟩ }, simp only [this, volume_empty, mul_zero] } } end ... = c * volume s : by rw [this, univ_inter] lemma integral_sup_le (f g : α →ₛ ennreal) : f.integral ⊔ g.integral ≤ (f ⊔ g).integral := calc f.integral ⊔ g.integral = ((pair f g).map prod.fst).integral ⊔ ((pair f g).map prod.snd).integral : rfl ... ≤ (pair f g).range.sum (λx, (x.1 ⊔ x.2) * volume (pair f g ⁻¹' {x})) : begin rw [map_integral, map_integral], 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).integral : by rw [sup_eq_map₂, map_integral] lemma integral_le_integral (f g : α →ₛ ennreal) (h : f ≤ g) : f.integral ≤ g.integral := calc f.integral ≤ f.integral ⊔ g.integral : le_sup_left ... ≤ (f ⊔ g).integral : integral_sup_le _ _ ... = g.integral : by rw [sup_of_le_right h] lemma integral_congr (f g : α →ₛ ennreal) (h : {a | f a = g a} ∈ (@measure_space.μ α _).a_e) : f.integral = g.integral := show ((pair f g).map prod.fst).integral = ((pair f g).map prod.snd).integral, from begin rw [map_integral, map_integral], refine finset.sum_congr rfl (assume p hp, _), rcases mem_range.1 hp with ⟨a, rfl⟩, by_cases eq : f a = g a, { dsimp only [pair_apply], rw eq }, { have : volume ((pair f g) ⁻¹' {(f a, g a)}) = 0, { refine volume_mono_null (assume a' ha', _) h, simp at ha', show f a' ≠ g a', rwa [ha'.1, ha'.2] }, simp [this] } end lemma integral_map {β} [measure_space β] (f : α →ₛ ennreal) (g : β →ₛ ennreal)(m : α → β) (eq : ∀a:α, f a = g (m a)) (h : ∀s:set β, is_measurable s → volume s = volume (m ⁻¹' s)) : f.integral = g.integral := have f_eq : (f : α → ennreal) = g ∘ m := funext eq, have vol_f : ∀r, volume (f ⁻¹' {r}) = volume (g ⁻¹' {r}), by { assume r, rw [h, f_eq, preimage_comp], exact measurable_sn _ _ }, begin simp [integral, vol_f], refine finset.sum_subset _ _, { simp [finset.subset_iff, f_eq], rintros r a rfl, exact ⟨_, rfl⟩ }, { assume r hrg hrf, rw [simple_func.mem_range, not_exists] at hrf, have : f ⁻¹' {r} = ∅ := set.eq_empty_of_subset_empty (assume a, by simpa using hrf a), simp [(vol_f _).symm, this] } end end measure section fin_vol_supp variables [measure_space α] [has_zero β] [has_zero γ] open finset ennreal protected def fin_vol_supp (f : α →ₛ β) : Prop := ∀b ≠ 0, volume (f ⁻¹' {b}) < ⊤ lemma fin_vol_supp_map {f : α →ₛ β} {g : β → γ} (hf : f.fin_vol_supp) (hg : g 0 = 0) : (f.map g).fin_vol_supp := begin assume c hc, simp only [map_preimage, volume_bUnion_preimage], apply sum_lt_top, intro b, simp only [mem_filter, mem_range, mem_singleton_iff, and_imp, exists_imp_distrib], intros a fab gbc, apply hf, intro b0, rw [b0, hg] at gbc, rw gbc at hc, contradiction end lemma fin_vol_supp_of_fin_vol_supp_map (f : α →ₛ β) {g : β → γ} (h : (f.map g).fin_vol_supp) (hg : ∀b, g b = 0 → b = 0) : f.fin_vol_supp := begin assume b hb, by_cases b_mem : b ∈ f.range, { have gb0 : g b ≠ 0, { assume h, have := hg b h, contradiction }, have : f ⁻¹' {b} ⊆ (f.map g) ⁻¹' {g b}, rw [coe_map, @preimage_comp _ _ _ f g, preimage_subset_preimage_iff], { simp only [set.mem_preimage, set.mem_singleton, set.singleton_subset_iff] }, { rw singleton_subset_iff, rw mem_range at b_mem, exact b_mem }, exact lt_of_le_of_lt (volume_mono this) (h (g b) gb0) }, { rw ← preimage_eq_empty_iff at b_mem, rw [b_mem, volume_empty], exact with_top.zero_lt_top } end lemma fin_vol_supp_pair {f : α →ₛ β} {g : α →ₛ γ} (hf : f.fin_vol_supp) (hg : g.fin_vol_supp) : (pair f g).fin_vol_supp := begin rintros ⟨b, c⟩ hbc, rw [pair_preimage_singleton], rw [ne.def, prod.eq_iff_fst_eq_snd_eq, not_and_distrib] at hbc, refine or.elim hbc (λ h : b≠0, _) (λ h : c≠0, _), { calc _ ≤ volume (f ⁻¹' {b}) : volume_mono (set.inter_subset_left _ _) ... < ⊤ : hf _ h }, { calc _ ≤ volume (g ⁻¹' {c}) : volume_mono (set.inter_subset_right _ _) ... < ⊤ : hg _ h }, end lemma integral_lt_top_of_fin_vol_supp {f : α →ₛ ennreal} (h₁ : ∀ₘ a, f a < ⊤) (h₂ : f.fin_vol_supp) : integral f < ⊤ := begin rw integral, apply sum_lt_top, intros a ha, have : f ⁻¹' {⊤} = -{a : α | f a < ⊤}, { ext, simp }, have vol_top : volume (f ⁻¹' {⊤}) = 0, { rw [this, volume, ← measure.mem_a_e_iff], exact h₁ }, by_cases hat : a = ⊤, { rw [hat, vol_top, mul_zero], exact with_top.zero_lt_top }, { by_cases haz : a = 0, { rw [haz, zero_mul], exact with_top.zero_lt_top }, apply mul_lt_top, { rw ennreal.lt_top_iff_ne_top, exact hat }, apply h₂, exact haz } end lemma fin_vol_supp_of_integral_lt_top {f : α →ₛ ennreal} (h : integral f < ⊤) : f.fin_vol_supp := begin assume b hb, rw [integral, 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, volume_empty], exact with_top.zero_lt_top } end /-- A technical lemma dealing with the definition of `integrable` in `l1_space.lean`. -/ lemma integral_map_coe_lt_top {f : α →ₛ β} {g : β → nnreal} (h : f.fin_vol_supp) (hg : g 0 = 0) : integral (f.map ((coe : nnreal → ennreal) ∘ g)) < ⊤ := integral_lt_top_of_fin_vol_supp (by { filter_upwards[], assume a, simp only [mem_set_of_eq, map_apply], exact ennreal.coe_lt_top}) (by { apply fin_vol_supp_map h, simp only [hg, function.comp_app, ennreal.coe_zero] }) end fin_vol_supp end simple_func section lintegral open simple_func variable [measure_space α] /-- The lower Lebesgue integral -/ def lintegral (f : α → ennreal) : ennreal := ⨆ (s : α →ₛ ennreal) (hf : f ≥ s), s.integral notation `∫⁻` binders `, ` r:(scoped f, lintegral f) := r theorem simple_func.lintegral_eq_integral (f : α →ₛ ennreal) : (∫⁻ a, f a) = f.integral := le_antisymm (supr_le $ assume s, supr_le $ assume hs, integral_le_integral _ _ hs) (le_supr_of_le f $ le_supr_of_le (le_refl f) $ le_refl _) lemma lintegral_le_lintegral (f g : α → ennreal) (h : f ≤ g) : (∫⁻ a, f a) ≤ (∫⁻ a, g a) := supr_le_supr $ assume s, supr_le $ assume hs, le_supr_of_le (le_trans hs h) (le_refl _) lemma lintegral_eq_nnreal (f : α → ennreal) : (∫⁻ a, f a) = (⨆ (s : α →ₛ nnreal) (hf : f ≥ s.map (coe : nnreal → ennreal)), (s.map (coe : nnreal → ennreal)).integral) := begin let c : nnreal → ennreal := coe, refine le_antisymm (supr_le $ assume s, supr_le $ assume hs, _) (supr_le $ assume s, supr_le $ assume hs, le_supr_of_le (s.map c) $ le_supr _ hs), by_cases {a | s a ≠ ⊤} ∈ (@measure_space.μ α _).a_e, { have : f ≥ (s.map ennreal.to_nnreal).map c := le_trans (assume a, ennreal.coe_to_nnreal_le_self) hs, refine le_supr_of_le (s.map ennreal.to_nnreal) (le_supr_of_le this (le_of_eq $ integral_congr _ _ _)), exact filter.mem_sets_of_superset h (assume a ha, (ennreal.coe_to_nnreal ha).symm) }, { have h_vol_s : volume {a : α | s a = ⊤} ≠ 0, { simp [measure.a_e, set.compl_set_of] at h, assumption }, let n : ℕ → (α →ₛ nnreal) := λn, restrict (const α (n : nnreal)) (s ⁻¹' {⊤}), have n_le_s : ∀i, (n i).map c ≤ s, { assume i a, dsimp [n, c], rw [restrict_apply _ (s.preimage_measurable _)], split_ifs with ha, { simp at ha, exact ha.symm ▸ le_top }, { exact zero_le _ } }, have approx_s : ∀ (i : ℕ), ↑i * volume {a : α | s a = ⊤} ≤ integral (map c (n i)), { assume i, have : {a : α | s a = ⊤} = s ⁻¹' {⊤}, { ext a, simp }, rw [this, ← restrict_const_integral _ _ (s.preimage_measurable _)], { refine integral_le_integral _ _ (assume a, le_of_eq _), simp [n, c, restrict_apply, s.preimage_measurable], split_ifs; simp [ennreal.coe_nat] }, }, calc s.integral ≤ ⊤ : le_top ... = (⨆i:ℕ, (i : ennreal) * volume {a | s a = ⊤}) : by rw [← ennreal.supr_mul, ennreal.supr_coe_nat, ennreal.top_mul, if_neg h_vol_s] ... ≤ (⨆i, ((n i).map c).integral) : supr_le_supr approx_s ... ≤ ⨆ (s : α →ₛ nnreal) (hf : f ≥ s.map c), (s.map c).integral : have ∀i, ((n i).map c : α → ennreal) ≤ f := assume i, le_trans (n_le_s i) hs, (supr_le $ assume i, le_supr_of_le (n i) (le_supr (λh, ((n i).map c).integral) (this i))) } end /-- Monotone convergence theorem -- somtimes called Beppo-Levi convergence. See `lintegral_supr_directed` for a more general form. -/ theorem lintegral_supr {f : ℕ → α → ennreal} (hf : ∀n, measurable (f n)) (h_mono : monotone f) : (∫⁻ a, ⨆n, f n a) = (⨆n, ∫⁻ a, f n a) := let c : nnreal → ennreal := coe in let F (a:α) := ⨆n, f n a in have hF : measurable F := measurable.supr hf, show (∫⁻ a, F a) = (⨆n, ∫⁻ a, f n a), begin refine le_antisymm _ _, { rw [lintegral_eq_nnreal], refine supr_le (assume s, supr_le (assume hsf, _)), refine ennreal.le_of_forall_lt_one_mul_lt (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 : α →ₛ ennreal) * 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 (zero_lt_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:ennreal, 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, is_measurable {a : α | ⇑(map c rs) a ≤ f n a} := assume n, is_measurable_le (simple_func.measurable _) (hf n), calc (r:ennreal) * integral (s.map c) = (rs.map c).range.sum (λr, r * volume ((rs.map c) ⁻¹' {r})) : by rw [← const_mul_integral, integral, eq_rs] ... ≤ (rs.map c).range.sum (λr, r * volume (⋃n, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a})) : le_of_eq (finset.sum_congr rfl $ assume x hx, by rw ← eq) ... ≤ (rs.map c).range.sum (λr, (⨆n, r * volume ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}))) : le_of_eq (finset.sum_congr rfl $ assume x hx, begin rw [volume, measure_Union_eq_supr_nat _ (mono x), ennreal.mul_supr], { assume i, refine is_measurable.inter ((rs.map c).preimage_measurable _) _, refine (hf i).preimage _, exact is_measurable_of_is_closed (is_closed_ge' _) } end) ... ≤ ⨆n, (rs.map c).range.sum (λr, r * volume ((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 _) (volume_mono $ mono p h) end ... ≤ (⨆n:ℕ, ((rs.map c).restrict {a | (rs.map c) a ≤ f n a}).integral) : begin refine supr_le_supr (assume n, _), rw [restrict_integral _ _ (h_meas n)], { refine le_of_eq (finset.sum_congr rfl $ assume r hr, _), congr' 2, ext 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_integral], refine lintegral_le_lintegral _ _ (assume a, _), dsimp, rw [restrict_apply], split_ifs; simp, simpa using h, exact h_meas n end }, { exact supr_le (assume n, lintegral_le_lintegral _ _ $ assume a, le_supr _ n) } end lemma lintegral_eq_supr_eapprox_integral {f : α → ennreal} (hf : measurable f) : (∫⁻ a, f a) = (⨆n, (eapprox f n).integral) := calc (∫⁻ a, f a) = (∫⁻ a, ⨆n, (eapprox f n : α → ennreal) a) : by congr; ext a; rw [supr_eapprox_apply f hf] ... = (⨆n, ∫⁻ a, (eapprox f n : α → ennreal) 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).integral) : by congr; ext n; rw [(eapprox f n).lintegral_eq_integral] lemma lintegral_add {f g : α → ennreal} (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 : α → ennreal) a) + (⨆n, (eapprox g n : α → ennreal) a)) : by congr; funext a; rw [supr_eapprox_apply f hf, supr_eapprox_apply g hg] ... = (∫⁻ a, (⨆n, (eapprox f n + eapprox g n : α → ennreal) 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).integral + (eapprox g n).integral) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.add_integral, ← simple_func.lintegral_eq_integral], 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).integral) + (⨆n, (eapprox g n).integral) : by refine (ennreal.supr_add_supr_of_monotone _ _).symm; { assume i j h, exact simple_func.integral_le_integral _ _ (monotone_eapprox _ h) } ... = (∫⁻ a, f a) + (∫⁻ a, g a) : by rw [lintegral_eq_supr_eapprox_integral hf, lintegral_eq_supr_eapprox_integral hg] @[simp] lemma lintegral_zero : (∫⁻ a:α, 0) = 0 := show (∫⁻ a:α, (0 : α →ₛ ennreal) a) = 0, by rw [simple_func.lintegral_eq_integral, zero_integral] lemma lintegral_finset_sum (s : finset β) {f : β → α → ennreal} (hf : ∀b, measurable (f b)) : (∫⁻ a, s.sum (λb, f b a)) = s.sum (λb, ∫⁻ a, f b a) := begin refine finset.induction_on s _ _, { simp }, { assume a s has ih, simp [has], rw [lintegral_add (hf _) (measurable_finset_sum s hf), ih] } end lemma lintegral_const_mul (r : ennreal) {f : α → ennreal} (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).integral) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.const_mul_integral, ← simple_func.lintegral_eq_integral] }, { 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_integral hf] lemma lintegral_const_mul_le (r : ennreal) (f : α → ennreal) : 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_integral, 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 : ennreal) (f : α → ennreal) (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_supr_const (r : ennreal) {s : set α} (hs : is_measurable s) : (∫⁻ a, ⨆(h : a ∈ s), r) = r * volume s := begin rw [← restrict_const_integral r s hs, ← (restrict (const α r) s).lintegral_eq_integral], congr; ext a; by_cases a ∈ s; simp [h, hs] end lemma lintegral_le_lintegral_ae {f g : α → ennreal} (h : ∀ₘ a, f a ≤ g a) : (∫⁻ a, f a) ≤ (∫⁻ a, g a) := begin rcases exists_is_measurable_superset_of_measure_eq_zero h with ⟨t, hts, ht, ht0⟩, have : - t ∈ (@measure_space.μ α _).a_e, { rw [measure.mem_a_e_iff, lattice.neg_neg, 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, simple_func.restrict_apply, ht.compl], exact le_trans (hfs a) (by_contradiction $ assume hnfg, h (hts hnfg)) }, { refine le_of_eq (s.integral_congr _ _), filter_upwards [this], refine assume a hnt, _, by_cases hat : a ∈ t; simp [hat, ht.compl], exact (hnt hat).elim } end lemma lintegral_congr_ae {f g : α → ennreal} (h : ∀ₘ a, f a = g a) : (∫⁻ a, f a) = (∫⁻ a, g a) := le_antisymm (lintegral_le_lintegral_ae $ by filter_upwards [h] assume a h, le_of_eq h) (lintegral_le_lintegral_ae $ by filter_upwards [h] assume a h, le_of_eq h.symm) -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rw₁ {f f' : α → β} (h : ∀ₘ a, f a = f' a) (g : β → ennreal) : (∫⁻ a, g (f a)) = (∫⁻ a, g (f' a)) := begin apply lintegral_congr_ae, filter_upwards [h], assume a, simp only [mem_set_of_eq], assume h, rw h end -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rw₂ {f₁ f₁' : α → β} {f₂ f₂' : α → γ} (h₁ : ∀ₘ a, f₁ a = f₁' a) (h₂ : ∀ₘ a, f₂ a = f₂' a) (g : β → γ → ennreal) : (∫⁻ a, g (f₁ a) (f₂ a)) = (∫⁻ a, g (f₁' a) (f₂' a)) := begin apply lintegral_congr_ae, filter_upwards [h₁, h₂], assume a, simp only [mem_set_of_eq], repeat { assume h, rw h } end lemma simple_func.lintegral_map (f : α →ₛ β) (g : β → ennreal) : (∫⁻ a, (f.map g) a) = ∫⁻ a, g (f a) := by { apply lintegral_congr_ae, filter_upwards [], assume a, exact map_apply _ _ _ } lemma lintegral_eq_zero_iff {f : α → ennreal} (hf : measurable f) : lintegral f = 0 ↔ (∀ₘ a, f a = 0) := begin refine iff.intro (assume h, _) (assume h, _), { have : ∀n:ℕ, ∀ₘ a, f a < n⁻¹, { assume n, have : is_measurable {a : α | f a ≥ n⁻¹ }, { exact hf _ (is_measurable_of_is_closed $ is_closed_ge' _) }, have : (n : ennreal)⁻¹ * volume {a | f a ≥ n⁻¹ } = 0, { rw [← simple_func.restrict_const_integral _ _ this, ← le_zero_iff_eq, ← simple_func.lintegral_eq_integral], refine le_trans (lintegral_le_lintegral _ _ _) (le_of_eq h), assume a, by_cases h : (n : ennreal)⁻¹ ≤ f a; simp [h, (≥), this] }, rw [ennreal.mul_eq_zero, ennreal.inv_eq_zero] at this, simpa [ennreal.nat_ne_top, all_ae_iff] using this }, filter_upwards [all_ae_all_iff.2 this], dsimp, assume 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 lintegral f = lintegral (λa:α, 0) : lintegral_congr_ae h ... = 0 : lintegral_zero } end /-- Weaker version of the monotone convergence theorem-/ lemma lintegral_supr_ae {f : ℕ → α → ennreal} (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_is_measurable_superset_of_measure_eq_zero (all_ae_iff.1 (all_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, begin have := hs.2.2, rw [← compl_compl s] at this, filter_upwards [(measure.mem_a_e_iff (-s)).2 this] assume a ha n, if_neg ha end, calc (∫⁻ a, ⨆n, f n a) = (∫⁻ a, ⨆n, g n a) : lintegral_congr_ae begin filter_upwards [g_eq_f], assume a ha, congr, funext, exact (ha n).symm end ... = ⨆n, (∫⁻ a, g n a) : lintegral_supr (assume n, measurable.if hs.2.1 measurable_const (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) : begin congr, funext, apply lintegral_congr_ae, filter_upwards [g_eq_f] assume a ha, ha n end lemma lintegral_sub {f g : α → ennreal} (hf : measurable f) (hg : measurable g) (hg_fin : lintegral g < ⊤) (h_le : ∀ₘ a, g a ≤ f a) : (∫⁻ a, f a - g a) = (∫⁻ a, f a) - (∫⁻ a, g a) := begin rw [← ennreal.add_right_inj hg_fin, ennreal.sub_add_cancel_of_le (lintegral_le_lintegral_ae h_le), ← lintegral_add (ennreal.measurable.sub hf hg) hg], show (∫⁻ (a : α), f a - g a + g a) = ∫⁻ (a : α), f a, apply lintegral_congr_ae, filter_upwards [h_le], simp only [add_comm, mem_set_of_eq], assume a ha, exact ennreal.add_sub_cancel_of_le ha end /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi_ae {f : ℕ → α → ennreal} (h_meas : ∀n, measurable (f n)) (h_mono : ∀n:ℕ, ∀ₘ a, f n.succ a ≤ f n a) (h_fin : lintegral (f 0) < ⊤) : (∫⁻ a, ⨅n, f n a) = (⨅n, ∫⁻ a, f n a) := have fn_le_f0 : (∫⁻ a, ⨅n, f n a) ≤ lintegral (f 0), from lintegral_le_lintegral _ _ (assume a, infi_le_of_le 0 (le_refl _)), have fn_le_f0' : (⨅n, ∫⁻ a, f n a) ≤ lintegral (f 0), from infi_le_of_le 0 (le_refl _), (ennreal.sub_left_inj h_fin fn_le_f0 fn_le_f0').1 $ show lintegral (f 0) - (∫⁻ a, ⨅n, f n a) = lintegral (f 0) - (⨅n, ∫⁻ a, f n a), from calc lintegral (f 0) - (∫⁻ 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) ≤ lintegral (f 0) : lintegral_le_lintegral _ _ (assume a, infi_le _ _) ... < ⊤ : h_fin ) (all_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, ennreal.measurable.sub (h_meas 0) (h_meas n)) (assume n, by filter_upwards [h_mono n] assume a ha, ennreal.sub_le_sub (le_refl _) ha) ... = ⨆n, lintegral (f 0) - ∫⁻ a, f n a : have h_mono : ∀ₘ a, ∀n:ℕ, f n.succ a ≤ f n a := all_ae_all_iff.2 h_mono, have h_mono : ∀n, ∀ₘa, f n a ≤ f 0 a := assume n, begin filter_upwards [h_mono], simp only [mem_set_of_eq], assume a, assume h, 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_le_lintegral_ae $ h_mono n ... < ⊤ : h_fin) (h_mono n)) ... = lintegral (f 0) - (⨅n, ∫⁻ a, f n a) : ennreal.sub_infi.symm section priority -- for some reason the next proof fails without changing the priority of this instance local attribute [instance, priority 1000] classical.prop_decidable /-- Known as Fatou's lemma -/ lemma lintegral_liminf_le {f : ℕ → α → ennreal} (h_meas : ∀n, measurable (f n)) : (∫⁻ a, liminf at_top (λ n, f n a)) ≤ liminf at_top (λ n, lintegral (f n)) := calc (∫⁻ a, liminf at_top (λ n, f n a)) = ∫⁻ a, ⨆n:ℕ, ⨅i≥n, f i a : congr rfl (funext (assume a, liminf_eq_supr_infi_of_nat)) ... = ⨆n:ℕ, ∫⁻ a, ⨅i≥n, f i a : lintegral_supr begin assume n, apply measurable.infi, assume i, by_cases h : i ≥ n, {convert h_meas i, simp [h]}, {convert measurable_const, simp [h]} end begin assume n m hnm a, simp only [le_infi_iff], assume i hi, refine infi_le_of_le i (infi_le_of_le (le_trans hnm hi) (le_refl _)) end ... ≤ ⨆n:ℕ, ⨅i≥n, lintegral (f i) : supr_le_supr $ assume n, le_infi $ assume i, le_infi $ assume hi, lintegral_le_lintegral _ _ $ assume a, infi_le_of_le i $ infi_le_of_le hi $ le_refl _ ... = liminf at_top (λ n, lintegral (f n)) : liminf_eq_supr_infi_of_nat.symm end priority lemma limsup_lintegral_le {f : ℕ → α → ennreal} {g : α → ennreal} (hf_meas : ∀ n, measurable (f n)) (h_bound : ∀n, ∀ₘa, f n a ≤ g a) (h_fin : lintegral g < ⊤) : limsup at_top (λn, lintegral (f n)) ≤ ∫⁻ a, limsup at_top (λn, f n a) := calc limsup at_top (λn, lintegral (f n)) = ⨅n:ℕ, ⨆i≥n, lintegral (f i) : limsup_eq_infi_supr_of_nat ... ≤ ⨅n:ℕ, ∫⁻ a, ⨆i≥n, f i a : infi_le_infi $ assume n, supr_le $ assume i, supr_le $ assume hi, lintegral_le_lintegral _ _ $ assume a, le_supr_of_le i $ le_supr_of_le hi (le_refl _) ... = ∫⁻ a, ⨅n:ℕ, ⨆i≥n, f i a : (lintegral_infi_ae (assume n, @measurable.supr _ _ _ _ _ _ _ _ _ (λ i a, supr (λ (h : i ≥ n), f i a)) (assume i, measurable.supr_Prop (hf_meas i))) (assume n, all_ae_of_all $ assume a, begin simp only [supr_le_iff], assume i hi, refine le_supr_of_le i _, rw [supr_pos _], exact le_refl _, exact nat.le_of_succ_le hi end ) (lt_of_le_of_lt (lintegral_le_lintegral_ae begin filter_upwards [all_ae_all_iff.2 h_bound], simp only [supr_le_iff, mem_set_of_eq], assume a ha i hi, exact ha i end ) h_fin)).symm ... = ∫⁻ a, limsup at_top (λn, f n a) : lintegral_congr_ae $ all_ae_of_all $ assume a, limsup_eq_infi_supr_of_nat.symm /-- Dominated convergence theorem for nonnegative functions -/ lemma tendsto_lintegral_of_dominated_convergence {F : ℕ → α → ennreal} {f : α → ennreal} {g : α → ennreal} (hF_meas : ∀n, measurable (F n)) (h_bound : ∀n, ∀ₘ a, F n a ≤ g a) (h_fin : lintegral g < ⊤) (h_lim : ∀ₘ a, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, lintegral (F n)) at_top (𝓝 (lintegral f)) := begin have limsup_le_lintegral := calc limsup at_top (λ (n : ℕ), lintegral (F n)) ≤ ∫⁻ (a : α), limsup at_top (λn, F n a) : limsup_lintegral_le hF_meas h_bound h_fin ... = lintegral f : lintegral_congr_ae $ by filter_upwards [h_lim] assume a h, limsup_eq_of_tendsto at_top_ne_bot h, have lintegral_le_liminf := calc lintegral f = ∫⁻ (a : α), liminf at_top (λ (n : ℕ), F n a) : lintegral_congr_ae $ by filter_upwards [h_lim] assume a h, (liminf_eq_of_tendsto at_top_ne_bot h).symm ... ≤ liminf at_top (λ n, lintegral (F n)) : lintegral_liminf_le hF_meas, have liminf_eq_limsup := le_antisymm (liminf_le_limsup (map_ne_bot at_top_ne_bot)) (le_trans limsup_le_lintegral lintegral_le_liminf), have liminf_eq_lintegral : liminf at_top (λ n, lintegral (F n)) = lintegral f := le_antisymm (by convert limsup_le_lintegral) lintegral_le_liminf, have limsup_eq_lintegral : limsup at_top (λ n, lintegral (F n)) = lintegral f := le_antisymm limsup_le_lintegral begin convert lintegral_le_liminf, exact liminf_eq_limsup.symm end, exact tendsto_of_liminf_eq_limsup ⟨liminf_eq_lintegral, limsup_eq_lintegral⟩ 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 : β → α → ennreal} (hf : ∀b, measurable (f b)) (h_directed : directed (≤) f) : (∫⁻ a, ⨆b, f b a) = (⨆b, ∫⁻ a, f b a) := begin by_cases hβ : ¬ nonempty β, { have : ∀f : β → ennreal, (⨆(b : β), f b) = 0 := assume f, supr_eq_bot.2 (assume b, (hβ ⟨b⟩).elim), simp [this] }, cases of_not_not hβ with b, haveI iβ : inhabited β := ⟨b⟩, clear hβ b, 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, lintegral (f b)) _ }, { exact le_supr_of_le (encode b + 1) (lintegral_le_lintegral _ _ $ h_directed.le_sequence b) } end end end lemma lintegral_tsum [encodable β] {f : β → α → ennreal} (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 _ hf] }, { assume b, exact measurable_finset_sum _ hf }, { 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 end lintegral namespace measure def integral [measurable_space α] (m : measure α) (f : α → ennreal) : ennreal := @lintegral α { μ := m } f variables [measurable_space α] {m : measure α} @[simp] lemma integral_zero : m.integral (λa, 0) = 0 := @lintegral_zero α { μ := m } lemma integral_map [measurable_space β] {f : β → ennreal} {g : α → β} (hf : measurable f) (hg : measurable g) : (map g m).integral f = m.integral (f ∘ g) := begin rw [integral, integral, lintegral_eq_supr_eapprox_integral, lintegral_eq_supr_eapprox_integral], { congr, funext n, symmetry, apply simple_func.integral_map, { assume a, exact congr_fun (simple_func.eapprox_comp hf hg) a }, { assume s hs, exact map_apply hg hs } }, exact hf.comp hg, assumption end lemma integral_dirac (a : α) {f : α → ennreal} (hf : measurable f) : (dirac a).integral f = f a := have ∀f:α →ₛ ennreal, @simple_func.integral α {μ := dirac a} f = f a, begin assume f, have : ∀r, @volume α { μ := dirac a } (⇑f ⁻¹' {r}) = ⨆ h : f a = r, 1, { assume r, transitivity, apply dirac_apply, apply simple_func.measurable_sn, refine supr_congr_Prop _ _; simp }, transitivity, apply finset.sum_eq_single (f a), { assume b hb h, simp [this, ne.symm h], }, { assume h, simp at h, exact (h a rfl).elim }, { rw [this], simp } end, begin rw [integral, lintegral_eq_supr_eapprox_integral], { simp [this, simple_func.supr_eapprox_apply f hf] }, assumption end def with_density (m : measure α) (f : α → ennreal) : measure α := if hf : measurable f then measure.of_measurable (λs hs, m.integral (λa, ⨆(h : a ∈ s), f a)) (by simp) begin assume s hs hd, have : ∀a, (⨆ (h : a ∈ ⋃i, s i), f a) = (∑i, (⨆ (h : a ∈ s i), f a)), { assume a, by_cases ha : ∃j, a ∈ s j, { rcases ha with ⟨j, haj⟩, have : ∀i, a ∈ s i ↔ j = i := assume i, iff.intro (assume hai, by_contradiction $ assume hij, hd j i hij ⟨haj, hai⟩) (by rintros rfl; assumption), simp [this, ennreal.tsum_supr_eq] }, { have : ∀i, ¬ a ∈ s i, { simpa using ha }, simp [this] } }, simp only [this], apply lintegral_tsum, { assume i, simp [supr_eq_if], exact measurable.if (hs i) hf measurable_const } end else 0 lemma with_density_apply {m : measure α} {f : α → ennreal} {s : set α} (hf : measurable f) (hs : is_measurable s) : m.with_density f s = m.integral (λa, ⨆(h : a ∈ s), f a) := by rw [with_density, dif_pos hf]; exact measure.of_measurable_apply s hs end measure end measure_theory
b744c0b10d23b997e8a66290183f76e46da4ed52
626e312b5c1cb2d88fca108f5933076012633192
/src/topology/metric_space/hausdorff_distance.lean
0f50188e99c76a21f05f1178f07f4f6cd3e68ae5
[ "Apache-2.0" ]
permissive
Bioye97/mathlib
9db2f9ee54418d29dd06996279ba9dc874fd6beb
782a20a27ee83b523f801ff34efb1a9557085019
refs/heads/master
1,690,305,956,488
1,631,067,774,000
1,631,067,774,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
32,614
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.metric_space.isometry import topology.instances.ennreal import analysis.specific_limits /-! # Hausdorff distance The Hausdorff distance on subsets of a metric (or emetric) space. Given two subsets `s` and `t` of a metric space, their Hausdorff distance is the smallest `d` such that any point `s` is within `d` of a point in `t`, and conversely. This quantity is often infinite (think of `s` bounded and `t` unbounded), and therefore better expressed in the setting of emetric spaces. ## Main definitions This files introduces: * `inf_edist x s`, the infimum edistance of a point `x` to a set `s` in an emetric space * `Hausdorff_edist s t`, the Hausdorff edistance of two sets in an emetric space * Versions of these notions on metric spaces, called respectively `inf_dist` and `Hausdorff_dist`. -/ noncomputable theory open_locale classical nnreal ennreal topological_space universes u v w open classical set function topological_space filter namespace emetric section inf_edist variables {α : Type u} {β : Type v} [pseudo_emetric_space α] [pseudo_emetric_space β] {x y : α} {s t : set α} {Φ : α → β} /-! ### Distance of a point to a set as a function into `ℝ≥0∞`. -/ /-- The minimal edistance of a point to a set -/ def inf_edist (x : α) (s : set α) : ℝ≥0∞ := ⨅ y ∈ s, edist x y @[simp] lemma inf_edist_empty : inf_edist x ∅ = ∞ := infi_emptyset lemma le_inf_edist {d} : d ≤ inf_edist x s ↔ ∀ y ∈ s, d ≤ edist x y := by simp only [inf_edist, le_infi_iff] /-- The edist to a union is the minimum of the edists -/ @[simp] lemma inf_edist_union : inf_edist x (s ∪ t) = inf_edist x s ⊓ inf_edist x t := infi_union /-- The edist to a singleton is the edistance to the single point of this singleton -/ @[simp] lemma inf_edist_singleton : inf_edist x {y} = edist x y := infi_singleton /-- The edist to a set is bounded above by the edist to any of its points -/ lemma inf_edist_le_edist_of_mem (h : y ∈ s) : inf_edist x s ≤ edist x y := binfi_le _ h /-- If a point `x` belongs to `s`, then its edist to `s` vanishes -/ lemma inf_edist_zero_of_mem (h : x ∈ s) : inf_edist x s = 0 := nonpos_iff_eq_zero.1 $ @edist_self _ _ x ▸ inf_edist_le_edist_of_mem h /-- The edist is monotonous with respect to inclusion -/ lemma inf_edist_le_inf_edist_of_subset (h : s ⊆ t) : inf_edist x t ≤ inf_edist x s := infi_le_infi_of_subset h /-- If the edist to a set is `< r`, there exists a point in the set at edistance `< r` -/ lemma exists_edist_lt_of_inf_edist_lt {r : ℝ≥0∞} (h : inf_edist x s < r) : ∃y∈s, edist x y < r := by simpa only [inf_edist, infi_lt_iff] using h /-- The edist of `x` to `s` is bounded by the sum of the edist of `y` to `s` and the edist from `x` to `y` -/ lemma inf_edist_le_inf_edist_add_edist : inf_edist x s ≤ inf_edist y s + edist x y := calc (⨅ z ∈ s, edist x z) ≤ ⨅ z ∈ s, edist y z + edist x y : binfi_le_binfi $ λ z hz, (edist_triangle _ _ _).trans_eq (add_comm _ _) ... = (⨅ z ∈ s, edist y z) + edist x y : by simp only [ennreal.infi_add] /-- The edist to a set depends continuously on the point -/ @[continuity] lemma continuous_inf_edist : continuous (λx, inf_edist x s) := continuous_of_le_add_edist 1 (by simp) $ by simp only [one_mul, inf_edist_le_inf_edist_add_edist, forall_2_true_iff] /-- The edist to a set and to its closure coincide -/ lemma inf_edist_closure : inf_edist x (closure s) = inf_edist x s := begin refine le_antisymm (inf_edist_le_inf_edist_of_subset subset_closure) _, refine ennreal.le_of_forall_pos_le_add (λε εpos h, _), have εpos' : (0 : ℝ≥0∞) < ε := by simpa, have : inf_edist x (closure s) < inf_edist x (closure s) + ε/2 := ennreal.lt_add_right h (ennreal.half_pos εpos'), rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ycs, hy⟩, -- y : α, ycs : y ∈ closure s, hy : edist x y < inf_edist x (closure s) + ↑ε / 2 rcases emetric.mem_closure_iff.1 ycs (ε/2) (ennreal.half_pos εpos') with ⟨z, zs, dyz⟩, -- z : α, zs : z ∈ s, dyz : edist y z < ↑ε / 2 calc inf_edist x s ≤ edist x z : inf_edist_le_edist_of_mem zs ... ≤ edist x y + edist y z : edist_triangle _ _ _ ... ≤ (inf_edist x (closure s) + ε / 2) + (ε/2) : add_le_add (le_of_lt hy) (le_of_lt dyz) ... = inf_edist x (closure s) + ↑ε : by rw [add_assoc, ennreal.add_halves] end /-- A point belongs to the closure of `s` iff its infimum edistance to this set vanishes -/ lemma mem_closure_iff_inf_edist_zero : x ∈ closure s ↔ inf_edist x s = 0 := ⟨λh, by rw ← inf_edist_closure; exact inf_edist_zero_of_mem h, λh, emetric.mem_closure_iff.2 $ λε εpos, exists_edist_lt_of_inf_edist_lt (by rwa h)⟩ /-- Given a closed set `s`, a point belongs to `s` iff its infimum edistance to this set vanishes -/ lemma mem_iff_inf_edist_zero_of_closed (h : is_closed s) : x ∈ s ↔ inf_edist x s = 0 := begin convert ← mem_closure_iff_inf_edist_zero, exact h.closure_eq end /-- The infimum edistance is invariant under isometries -/ lemma inf_edist_image (hΦ : isometry Φ) : inf_edist (Φ x) (Φ '' t) = inf_edist x t := by simp only [inf_edist, infi_image, hΦ.edist_eq] lemma _root_.is_open.exists_Union_is_closed {U : set α} (hU : is_open U) : ∃ F : ℕ → set α, (∀ n, is_closed (F n)) ∧ (∀ n, F n ⊆ U) ∧ ((⋃ n, F n) = U) ∧ (monotone F) := begin obtain ⟨a, a_pos, a_lt_one⟩ : ∃ (a : ℝ≥0∞), 0 < a ∧ a < 1 := exists_between (ennreal.zero_lt_one), let F := λ (n : ℕ), (λ x, inf_edist x Uᶜ) ⁻¹' (Ici (a^n)), have F_subset : ∀ n, F n ⊆ U, { assume n x hx, by_contra h, rw [← mem_compl_iff, mem_iff_inf_edist_zero_of_closed (is_open.is_closed_compl hU)] at h, have : 0 < inf_edist x Uᶜ := lt_of_lt_of_le (ennreal.pow_pos a_pos _) hx, rw h at this, exact lt_irrefl _ this }, refine ⟨F, λ n, is_closed.preimage continuous_inf_edist is_closed_Ici, F_subset, _, _⟩, show monotone F, { assume m n hmn x hx, simp only [mem_Ici, mem_preimage] at hx ⊢, apply le_trans (ennreal.pow_le_pow_of_le_one a_lt_one.le hmn) hx }, show (⋃ n, F n) = U, { refine subset.antisymm (by simp only [Union_subset_iff, F_subset, forall_const]) (λ x hx, _), have : ¬(x ∈ Uᶜ), by simpa using hx, rw mem_iff_inf_edist_zero_of_closed (is_open.is_closed_compl hU) at this, have B : 0 < inf_edist x Uᶜ, by simpa [pos_iff_ne_zero] using this, have : filter.tendsto (λ n, a^n) at_top (𝓝 0) := ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 a_lt_one, rcases ((tendsto_order.1 this).2 _ B).exists with ⟨n, hn⟩, simp only [mem_Union, mem_Ici, mem_preimage], exact ⟨n, hn.le⟩ }, end end inf_edist --section /-! ### The Hausdorff distance as a function into `ℝ≥0∞`. -/ /-- The Hausdorff edistance between two sets is the smallest `r` such that each set is contained in the `r`-neighborhood of the other one -/ @[irreducible] def Hausdorff_edist {α : Type u} [pseudo_emetric_space α] (s t : set α) : ℝ≥0∞ := (⨆ x ∈ s, inf_edist x t) ⊔ (⨆ y ∈ t, inf_edist y s) lemma Hausdorff_edist_def {α : Type u} [pseudo_emetric_space α] (s t : set α) : Hausdorff_edist s t = (⨆ x ∈ s, inf_edist x t) ⊔ (⨆ y ∈ t, inf_edist y s) := by rw Hausdorff_edist section Hausdorff_edist variables {α : Type u} {β : Type v} [pseudo_emetric_space α] [pseudo_emetric_space β] {x y : α} {s t u : set α} {Φ : α → β} /-- The Hausdorff edistance of a set to itself vanishes -/ @[simp] lemma Hausdorff_edist_self : Hausdorff_edist s s = 0 := begin simp only [Hausdorff_edist_def, sup_idem, ennreal.supr_eq_zero], exact λ x hx, inf_edist_zero_of_mem hx end /-- The Haudorff edistances of `s` to `t` and of `t` to `s` coincide -/ lemma Hausdorff_edist_comm : Hausdorff_edist s t = Hausdorff_edist t s := by unfold Hausdorff_edist; apply sup_comm /-- Bounding the Hausdorff edistance by bounding the edistance of any point in each set to the other set -/ lemma Hausdorff_edist_le_of_inf_edist {r : ℝ≥0∞} (H1 : ∀x ∈ s, inf_edist x t ≤ r) (H2 : ∀x ∈ t, inf_edist x s ≤ r) : Hausdorff_edist s t ≤ r := begin simp only [Hausdorff_edist, sup_le_iff, supr_le_iff], exact ⟨H1, H2⟩ end /-- Bounding the Hausdorff edistance by exhibiting, for any point in each set, another point in the other set at controlled distance -/ lemma Hausdorff_edist_le_of_mem_edist {r : ℝ≥0∞} (H1 : ∀x ∈ s, ∃y ∈ t, edist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, edist x y ≤ r) : Hausdorff_edist s t ≤ r := begin refine Hausdorff_edist_le_of_inf_edist _ _, { assume x xs, rcases H1 x xs with ⟨y, yt, hy⟩, exact le_trans (inf_edist_le_edist_of_mem yt) hy }, { assume x xt, rcases H2 x xt with ⟨y, ys, hy⟩, exact le_trans (inf_edist_le_edist_of_mem ys) hy } end /-- The distance to a set is controlled by the Hausdorff distance -/ lemma inf_edist_le_Hausdorff_edist_of_mem (h : x ∈ s) : inf_edist x t ≤ Hausdorff_edist s t := begin rw Hausdorff_edist_def, refine le_trans _ le_sup_left, exact le_bsupr x h end /-- If the Hausdorff distance is `<r`, then any point in one of the sets has a corresponding point at distance `<r` in the other set -/ lemma exists_edist_lt_of_Hausdorff_edist_lt {r : ℝ≥0∞} (h : x ∈ s) (H : Hausdorff_edist s t < r) : ∃y∈t, edist x y < r := exists_edist_lt_of_inf_edist_lt $ calc inf_edist x t ≤ Hausdorff_edist s t : inf_edist_le_Hausdorff_edist_of_mem h ... < r : H /-- The distance from `x` to `s` or `t` is controlled in terms of the Hausdorff distance between `s` and `t` -/ lemma inf_edist_le_inf_edist_add_Hausdorff_edist : inf_edist x t ≤ inf_edist x s + Hausdorff_edist s t := ennreal.le_of_forall_pos_le_add $ λε εpos h, begin have εpos' : (0 : ℝ≥0∞) < ε := by simpa, have : inf_edist x s < inf_edist x s + ε/2 := ennreal.lt_add_right (ennreal.add_lt_top.1 h).1 (ennreal.half_pos εpos'), rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ys, dxy⟩, -- y : α, ys : y ∈ s, dxy : edist x y < inf_edist x s + ↑ε / 2 have : Hausdorff_edist s t < Hausdorff_edist s t + ε/2 := ennreal.lt_add_right (ennreal.add_lt_top.1 h).2 (ennreal.half_pos εpos'), rcases exists_edist_lt_of_Hausdorff_edist_lt ys this with ⟨z, zt, dyz⟩, -- z : α, zt : z ∈ t, dyz : edist y z < Hausdorff_edist s t + ↑ε / 2 calc inf_edist x t ≤ edist x z : inf_edist_le_edist_of_mem zt ... ≤ edist x y + edist y z : edist_triangle _ _ _ ... ≤ (inf_edist x s + ε/2) + (Hausdorff_edist s t + ε/2) : add_le_add dxy.le dyz.le ... = inf_edist x s + Hausdorff_edist s t + ε : by simp [ennreal.add_halves, add_comm, add_left_comm] end /-- The Hausdorff edistance is invariant under eisometries -/ lemma Hausdorff_edist_image (h : isometry Φ) : Hausdorff_edist (Φ '' s) (Φ '' t) = Hausdorff_edist s t := by simp only [Hausdorff_edist_def, supr_image, inf_edist_image h] /-- The Hausdorff distance is controlled by the diameter of the union -/ lemma Hausdorff_edist_le_ediam (hs : s.nonempty) (ht : t.nonempty) : Hausdorff_edist s t ≤ diam (s ∪ t) := begin rcases hs with ⟨x, xs⟩, rcases ht with ⟨y, yt⟩, refine Hausdorff_edist_le_of_mem_edist _ _, { intros z hz, exact ⟨y, yt, edist_le_diam_of_mem (subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ }, { intros z hz, exact ⟨x, xs, edist_le_diam_of_mem (subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ } end /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_edist_triangle : Hausdorff_edist s u ≤ Hausdorff_edist s t + Hausdorff_edist t u := begin rw Hausdorff_edist_def, simp only [sup_le_iff, supr_le_iff], split, show ∀x ∈ s, inf_edist x u ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xs, calc inf_edist x u ≤ inf_edist x t + Hausdorff_edist t u : inf_edist_le_inf_edist_add_Hausdorff_edist ... ≤ Hausdorff_edist s t + Hausdorff_edist t u : add_le_add_right (inf_edist_le_Hausdorff_edist_of_mem xs) _, show ∀x ∈ u, inf_edist x s ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xu, calc inf_edist x s ≤ inf_edist x t + Hausdorff_edist t s : inf_edist_le_inf_edist_add_Hausdorff_edist ... ≤ Hausdorff_edist u t + Hausdorff_edist t s : add_le_add_right (inf_edist_le_Hausdorff_edist_of_mem xu) _ ... = Hausdorff_edist s t + Hausdorff_edist t u : by simp [Hausdorff_edist_comm, add_comm] end /-- Two sets are at zero Hausdorff edistance if and only if they have the same closure -/ lemma Hausdorff_edist_zero_iff_closure_eq_closure : Hausdorff_edist s t = 0 ↔ closure s = closure t := calc Hausdorff_edist s t = 0 ↔ s ⊆ closure t ∧ t ⊆ closure s : by simp only [Hausdorff_edist_def, ennreal.sup_eq_zero, ennreal.supr_eq_zero, ← mem_closure_iff_inf_edist_zero, subset_def] ... ↔ closure s = closure t : ⟨λ h, subset.antisymm (closure_minimal h.1 is_closed_closure) (closure_minimal h.2 is_closed_closure), λ h, ⟨h ▸ subset_closure, h.symm ▸ subset_closure⟩⟩ /-- The Hausdorff edistance between a set and its closure vanishes -/ @[simp, priority 1100] lemma Hausdorff_edist_self_closure : Hausdorff_edist s (closure s) = 0 := by rw [Hausdorff_edist_zero_iff_closure_eq_closure, closure_closure] /-- Replacing a set by its closure does not change the Hausdorff edistance. -/ @[simp] lemma Hausdorff_edist_closure₁ : Hausdorff_edist (closure s) t = Hausdorff_edist s t := begin refine le_antisymm _ _, { calc _ ≤ Hausdorff_edist (closure s) s + Hausdorff_edist s t : Hausdorff_edist_triangle ... = Hausdorff_edist s t : by simp [Hausdorff_edist_comm] }, { calc _ ≤ Hausdorff_edist s (closure s) + Hausdorff_edist (closure s) t : Hausdorff_edist_triangle ... = Hausdorff_edist (closure s) t : by simp } end /-- Replacing a set by its closure does not change the Hausdorff edistance. -/ @[simp] lemma Hausdorff_edist_closure₂ : Hausdorff_edist s (closure t) = Hausdorff_edist s t := by simp [@Hausdorff_edist_comm _ _ s _] /-- The Hausdorff edistance between sets or their closures is the same -/ @[simp] lemma Hausdorff_edist_closure : Hausdorff_edist (closure s) (closure t) = Hausdorff_edist s t := by simp /-- Two closed sets are at zero Hausdorff edistance if and only if they coincide -/ lemma Hausdorff_edist_zero_iff_eq_of_closed (hs : is_closed s) (ht : is_closed t) : Hausdorff_edist s t = 0 ↔ s = t := by rw [Hausdorff_edist_zero_iff_closure_eq_closure, hs.closure_eq, ht.closure_eq] /-- The Haudorff edistance to the empty set is infinite -/ lemma Hausdorff_edist_empty (ne : s.nonempty) : Hausdorff_edist s ∅ = ∞ := begin rcases ne with ⟨x, xs⟩, have : inf_edist x ∅ ≤ Hausdorff_edist s ∅ := inf_edist_le_Hausdorff_edist_of_mem xs, simpa using this, end /-- If a set is at finite Hausdorff edistance of a nonempty set, it is nonempty -/ lemma nonempty_of_Hausdorff_edist_ne_top (hs : s.nonempty) (fin : Hausdorff_edist s t ≠ ⊤) : t.nonempty := t.eq_empty_or_nonempty.elim (λ ht, (fin $ ht.symm ▸ Hausdorff_edist_empty hs).elim) id lemma empty_or_nonempty_of_Hausdorff_edist_ne_top (fin : Hausdorff_edist s t ≠ ⊤) : s = ∅ ∧ t = ∅ ∨ s.nonempty ∧ t.nonempty := begin cases s.eq_empty_or_nonempty with hs hs, { cases t.eq_empty_or_nonempty with ht ht, { exact or.inl ⟨hs, ht⟩ }, { rw Hausdorff_edist_comm at fin, exact or.inr ⟨nonempty_of_Hausdorff_edist_ne_top ht fin, ht⟩ } }, { exact or.inr ⟨hs, nonempty_of_Hausdorff_edist_ne_top hs fin⟩ } end end Hausdorff_edist -- section end emetric --namespace /-! Now, we turn to the same notions in metric spaces. To avoid the difficulties related to `Inf` and `Sup` on `ℝ` (which is only conditionally complete), we use the notions in `ℝ≥0∞` formulated in terms of the edistance, and coerce them to `ℝ`. Then their properties follow readily from the corresponding properties in `ℝ≥0∞`, modulo some tedious rewriting of inequalities from one to the other. -/ namespace metric section variables {α : Type u} {β : Type v} [pseudo_metric_space α] [pseudo_metric_space β] {s t u : set α} {x y : α} {Φ : α → β} open emetric /-! ### Distance of a point to a set as a function into `ℝ`. -/ /-- The minimal distance of a point to a set -/ def inf_dist (x : α) (s : set α) : ℝ := ennreal.to_real (inf_edist x s) /-- the minimal distance is always nonnegative -/ lemma inf_dist_nonneg : 0 ≤ inf_dist x s := by simp [inf_dist] /-- the minimal distance to the empty set is 0 (if you want to have the more reasonable value ∞ instead, use `inf_edist`, which takes values in ℝ≥0∞) -/ @[simp] lemma inf_dist_empty : inf_dist x ∅ = 0 := by simp [inf_dist] /-- In a metric space, the minimal edistance to a nonempty set is finite -/ lemma inf_edist_ne_top (h : s.nonempty) : inf_edist x s ≠ ⊤ := begin rcases h with ⟨y, hy⟩, apply lt_top_iff_ne_top.1, calc inf_edist x s ≤ edist x y : inf_edist_le_edist_of_mem hy ... < ⊤ : lt_top_iff_ne_top.2 (edist_ne_top _ _) end /-- The minimal distance of a point to a set containing it vanishes -/ lemma inf_dist_zero_of_mem (h : x ∈ s) : inf_dist x s = 0 := by simp [inf_edist_zero_of_mem h, inf_dist] /-- The minimal distance to a singleton is the distance to the unique point in this singleton -/ @[simp] lemma inf_dist_singleton : inf_dist x {y} = dist x y := by simp [inf_dist, inf_edist, dist_edist] /-- The minimal distance to a set is bounded by the distance to any point in this set -/ lemma inf_dist_le_dist_of_mem (h : y ∈ s) : inf_dist x s ≤ dist x y := begin rw [dist_edist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ⟨_, h⟩) (edist_ne_top _ _)], exact inf_edist_le_edist_of_mem h end /-- The minimal distance is monotonous with respect to inclusion -/ lemma inf_dist_le_inf_dist_of_subset (h : s ⊆ t) (hs : s.nonempty) : inf_dist x t ≤ inf_dist x s := begin have ht : t.nonempty := hs.mono h, rw [inf_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) (inf_edist_ne_top hs)], exact inf_edist_le_inf_edist_of_subset h end /-- If the minimal distance to a set is `<r`, there exists a point in this set at distance `<r` -/ lemma exists_dist_lt_of_inf_dist_lt {r : real} (h : inf_dist x s < r) (hs : s.nonempty) : ∃y∈s, dist x y < r := begin have rpos : 0 < r := lt_of_le_of_lt inf_dist_nonneg h, have : inf_edist x s < ennreal.of_real r, { rwa [inf_dist, ← ennreal.to_real_of_real (le_of_lt rpos), ennreal.to_real_lt_to_real (inf_edist_ne_top hs)] at h, simp }, rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ys, hy⟩, rw [edist_dist, ennreal.of_real_lt_of_real_iff rpos] at hy, exact ⟨y, ys, hy⟩, end /-- The minimal distance from `x` to `s` is bounded by the distance from `y` to `s`, modulo the distance between `x` and `y` -/ lemma inf_dist_le_inf_dist_add_dist : inf_dist x s ≤ inf_dist y s + dist x y := begin cases s.eq_empty_or_nonempty with hs hs, { by simp [hs, dist_nonneg] }, { rw [inf_dist, inf_dist, dist_edist, ← ennreal.to_real_add (inf_edist_ne_top hs) (edist_ne_top _ _), ennreal.to_real_le_to_real (inf_edist_ne_top hs)], { apply inf_edist_le_inf_edist_add_edist }, { simp [ennreal.add_eq_top, inf_edist_ne_top hs, edist_ne_top] }} end variable (s) /-- The minimal distance to a set is Lipschitz in point with constant 1 -/ lemma lipschitz_inf_dist_pt : lipschitz_with 1 (λx, inf_dist x s) := lipschitz_with.of_le_add $ λ x y, inf_dist_le_inf_dist_add_dist /-- The minimal distance to a set is uniformly continuous in point -/ lemma uniform_continuous_inf_dist_pt : uniform_continuous (λx, inf_dist x s) := (lipschitz_inf_dist_pt s).uniform_continuous /-- The minimal distance to a set is continuous in point -/ @[continuity] lemma continuous_inf_dist_pt : continuous (λx, inf_dist x s) := (uniform_continuous_inf_dist_pt s).continuous variable {s} /-- The minimal distance to a set and its closure coincide -/ lemma inf_dist_eq_closure : inf_dist x (closure s) = inf_dist x s := by simp [inf_dist, inf_edist_closure] /-- A point belongs to the closure of `s` iff its infimum distance to this set vanishes -/ lemma mem_closure_iff_inf_dist_zero (h : s.nonempty) : x ∈ closure s ↔ inf_dist x s = 0 := by simp [mem_closure_iff_inf_edist_zero, inf_dist, ennreal.to_real_eq_zero_iff, inf_edist_ne_top h] /-- Given a closed set `s`, a point belongs to `s` iff its infimum distance to this set vanishes -/ lemma mem_iff_inf_dist_zero_of_closed (h : is_closed s) (hs : s.nonempty) : x ∈ s ↔ inf_dist x s = 0 := begin have := @mem_closure_iff_inf_dist_zero _ _ s x hs, rwa h.closure_eq at this end /-- The infimum distance is invariant under isometries -/ lemma inf_dist_image (hΦ : isometry Φ) : inf_dist (Φ x) (Φ '' t) = inf_dist x t := by simp [inf_dist, inf_edist_image hΦ] /-! ### Distance of a point to a set as a function into `ℝ≥0`. -/ /-- The minimal distance of a point to a set as a `ℝ≥0` -/ def inf_nndist (x : α) (s : set α) : ℝ≥0 := ennreal.to_nnreal (inf_edist x s) @[simp] lemma coe_inf_nndist : (inf_nndist x s : ℝ) = inf_dist x s := rfl /-- The minimal distance to a set (as `ℝ≥0`) is Lipschitz in point with constant 1 -/ lemma lipschitz_inf_nndist_pt (s : set α) : lipschitz_with 1 (λx, inf_nndist x s) := lipschitz_with.of_le_add $ λ x y, inf_dist_le_inf_dist_add_dist /-- The minimal distance to a set (as `ℝ≥0`) is uniformly continuous in point -/ lemma uniform_continuous_inf_nndist_pt (s : set α) : uniform_continuous (λx, inf_nndist x s) := (lipschitz_inf_nndist_pt s).uniform_continuous /-- The minimal distance to a set (as `ℝ≥0`) is continuous in point -/ lemma continuous_inf_nndist_pt (s : set α) : continuous (λx, inf_nndist x s) := (uniform_continuous_inf_nndist_pt s).continuous /-! ### The Hausdorff distance as a function into `ℝ`. -/ /-- The Hausdorff distance between two sets is the smallest nonnegative `r` such that each set is included in the `r`-neighborhood of the other. If there is no such `r`, it is defined to be `0`, arbitrarily -/ def Hausdorff_dist (s t : set α) : ℝ := ennreal.to_real (Hausdorff_edist s t) /-- The Hausdorff distance is nonnegative -/ lemma Hausdorff_dist_nonneg : 0 ≤ Hausdorff_dist s t := by simp [Hausdorff_dist] /-- If two sets are nonempty and bounded in a metric space, they are at finite Hausdorff edistance. -/ lemma Hausdorff_edist_ne_top_of_nonempty_of_bounded (hs : s.nonempty) (ht : t.nonempty) (bs : bounded s) (bt : bounded t) : Hausdorff_edist s t ≠ ⊤ := begin rcases hs with ⟨cs, hcs⟩, rcases ht with ⟨ct, hct⟩, rcases (bounded_iff_subset_ball ct).1 bs with ⟨rs, hrs⟩, rcases (bounded_iff_subset_ball cs).1 bt with ⟨rt, hrt⟩, have : Hausdorff_edist s t ≤ ennreal.of_real (max rs rt), { apply Hausdorff_edist_le_of_mem_edist, { assume x xs, existsi [ct, hct], have : dist x ct ≤ max rs rt := le_trans (hrs xs) (le_max_left _ _), rwa [edist_dist, ennreal.of_real_le_of_real_iff], exact le_trans dist_nonneg this }, { assume x xt, existsi [cs, hcs], have : dist x cs ≤ max rs rt := le_trans (hrt xt) (le_max_right _ _), rwa [edist_dist, ennreal.of_real_le_of_real_iff], exact le_trans dist_nonneg this }}, exact ennreal.lt_top_iff_ne_top.1 (lt_of_le_of_lt this (by simp [lt_top_iff_ne_top])) end /-- The Hausdorff distance between a set and itself is zero -/ @[simp] lemma Hausdorff_dist_self_zero : Hausdorff_dist s s = 0 := by simp [Hausdorff_dist] /-- The Hausdorff distance from `s` to `t` and from `t` to `s` coincide -/ lemma Hausdorff_dist_comm : Hausdorff_dist s t = Hausdorff_dist t s := by simp [Hausdorff_dist, Hausdorff_edist_comm] /-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable value ∞ instead, use `Hausdorff_edist`, which takes values in ℝ≥0∞) -/ @[simp] lemma Hausdorff_dist_empty : Hausdorff_dist s ∅ = 0 := begin cases s.eq_empty_or_nonempty with h h, { simp [h] }, { simp [Hausdorff_dist, Hausdorff_edist_empty h] } end /-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable value ∞ instead, use `Hausdorff_edist`, which takes values in ℝ≥0∞) -/ @[simp] lemma Hausdorff_dist_empty' : Hausdorff_dist ∅ s = 0 := by simp [Hausdorff_dist_comm] /-- Bounding the Hausdorff distance by bounding the distance of any point in each set to the other set -/ lemma Hausdorff_dist_le_of_inf_dist {r : ℝ} (hr : 0 ≤ r) (H1 : ∀x ∈ s, inf_dist x t ≤ r) (H2 : ∀x ∈ t, inf_dist x s ≤ r) : Hausdorff_dist s t ≤ r := begin by_cases h1 : Hausdorff_edist s t = ⊤, by rwa [Hausdorff_dist, h1, ennreal.top_to_real], cases s.eq_empty_or_nonempty with hs hs, by rwa [hs, Hausdorff_dist_empty'], cases t.eq_empty_or_nonempty with ht ht, by rwa [ht, Hausdorff_dist_empty], have : Hausdorff_edist s t ≤ ennreal.of_real r, { apply Hausdorff_edist_le_of_inf_edist _ _, { assume x hx, have I := H1 x hx, rwa [inf_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real (inf_edist_ne_top ht) ennreal.of_real_ne_top] at I }, { assume x hx, have I := H2 x hx, rwa [inf_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real (inf_edist_ne_top hs) ennreal.of_real_ne_top] at I }}, rwa [Hausdorff_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real h1 ennreal.of_real_ne_top] end /-- Bounding the Hausdorff distance by exhibiting, for any point in each set, another point in the other set at controlled distance -/ lemma Hausdorff_dist_le_of_mem_dist {r : ℝ} (hr : 0 ≤ r) (H1 : ∀x ∈ s, ∃y ∈ t, dist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, dist x y ≤ r) : Hausdorff_dist s t ≤ r := begin apply Hausdorff_dist_le_of_inf_dist hr, { assume x xs, rcases H1 x xs with ⟨y, yt, hy⟩, exact le_trans (inf_dist_le_dist_of_mem yt) hy }, { assume x xt, rcases H2 x xt with ⟨y, ys, hy⟩, exact le_trans (inf_dist_le_dist_of_mem ys) hy } end /-- The Hausdorff distance is controlled by the diameter of the union -/ lemma Hausdorff_dist_le_diam (hs : s.nonempty) (bs : bounded s) (ht : t.nonempty) (bt : bounded t) : Hausdorff_dist s t ≤ diam (s ∪ t) := begin rcases hs with ⟨x, xs⟩, rcases ht with ⟨y, yt⟩, refine Hausdorff_dist_le_of_mem_dist diam_nonneg _ _, { exact λz hz, ⟨y, yt, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩) (subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ }, { exact λz hz, ⟨x, xs, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩) (subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ } end /-- The distance to a set is controlled by the Hausdorff distance -/ lemma inf_dist_le_Hausdorff_dist_of_mem (hx : x ∈ s) (fin : Hausdorff_edist s t ≠ ⊤) : inf_dist x t ≤ Hausdorff_dist s t := begin have ht : t.nonempty := nonempty_of_Hausdorff_edist_ne_top ⟨x, hx⟩ fin, rw [Hausdorff_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) fin], exact inf_edist_le_Hausdorff_edist_of_mem hx end /-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance `<r` of a point in the other set -/ lemma exists_dist_lt_of_Hausdorff_dist_lt {r : ℝ} (h : x ∈ s) (H : Hausdorff_dist s t < r) (fin : Hausdorff_edist s t ≠ ⊤) : ∃y∈t, dist x y < r := begin have r0 : 0 < r := lt_of_le_of_lt (Hausdorff_dist_nonneg) H, have : Hausdorff_edist s t < ennreal.of_real r, by rwa [Hausdorff_dist, ← ennreal.to_real_of_real (le_of_lt r0), ennreal.to_real_lt_to_real fin (ennreal.of_real_ne_top)] at H, rcases exists_edist_lt_of_Hausdorff_edist_lt h this with ⟨y, hy, yr⟩, rw [edist_dist, ennreal.of_real_lt_of_real_iff r0] at yr, exact ⟨y, hy, yr⟩ end /-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance `<r` of a point in the other set -/ lemma exists_dist_lt_of_Hausdorff_dist_lt' {r : ℝ} (h : y ∈ t) (H : Hausdorff_dist s t < r) (fin : Hausdorff_edist s t ≠ ⊤) : ∃x∈s, dist x y < r := begin rw Hausdorff_dist_comm at H, rw Hausdorff_edist_comm at fin, simpa [dist_comm] using exists_dist_lt_of_Hausdorff_dist_lt h H fin end /-- The infimum distance to `s` and `t` are the same, up to the Hausdorff distance between `s` and `t` -/ lemma inf_dist_le_inf_dist_add_Hausdorff_dist (fin : Hausdorff_edist s t ≠ ⊤) : inf_dist x t ≤ inf_dist x s + Hausdorff_dist s t := begin rcases empty_or_nonempty_of_Hausdorff_edist_ne_top fin with ⟨hs,ht⟩|⟨hs,ht⟩, { simp only [hs, ht, Hausdorff_dist_empty, inf_dist_empty, zero_add] }, rw [inf_dist, inf_dist, Hausdorff_dist, ← ennreal.to_real_add (inf_edist_ne_top hs) fin, ennreal.to_real_le_to_real (inf_edist_ne_top ht)], { exact inf_edist_le_inf_edist_add_Hausdorff_edist }, { exact ennreal.add_ne_top.2 ⟨inf_edist_ne_top hs, fin⟩ } end /-- The Hausdorff distance is invariant under isometries -/ lemma Hausdorff_dist_image (h : isometry Φ) : Hausdorff_dist (Φ '' s) (Φ '' t) = Hausdorff_dist s t := by simp [Hausdorff_dist, Hausdorff_edist_image h] /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_dist_triangle (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u := begin by_cases Hausdorff_edist s u = ⊤, { calc Hausdorff_dist s u = 0 + 0 : by simp [Hausdorff_dist, h] ... ≤ Hausdorff_dist s t + Hausdorff_dist t u : add_le_add (Hausdorff_dist_nonneg) (Hausdorff_dist_nonneg) }, { have Dtu : Hausdorff_edist t u < ⊤ := calc Hausdorff_edist t u ≤ Hausdorff_edist t s + Hausdorff_edist s u : Hausdorff_edist_triangle ... = Hausdorff_edist s t + Hausdorff_edist s u : by simp [Hausdorff_edist_comm] ... < ⊤ : by simp [ennreal.add_lt_top]; simp [ennreal.lt_top_iff_ne_top, h, fin], rw [Hausdorff_dist, Hausdorff_dist, Hausdorff_dist, ← ennreal.to_real_add fin (lt_top_iff_ne_top.1 Dtu), ennreal.to_real_le_to_real h], { exact Hausdorff_edist_triangle }, { simp [ennreal.add_eq_top, lt_top_iff_ne_top.1 Dtu, fin] }} end /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_dist_triangle' (fin : Hausdorff_edist t u ≠ ⊤) : Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u := begin rw Hausdorff_edist_comm at fin, have I : Hausdorff_dist u s ≤ Hausdorff_dist u t + Hausdorff_dist t s := Hausdorff_dist_triangle fin, simpa [add_comm, Hausdorff_dist_comm] using I end /-- The Hausdorff distance between a set and its closure vanish -/ @[simp, priority 1100] lemma Hausdorff_dist_self_closure : Hausdorff_dist s (closure s) = 0 := by simp [Hausdorff_dist] /-- Replacing a set by its closure does not change the Hausdorff distance. -/ @[simp] lemma Hausdorff_dist_closure₁ : Hausdorff_dist (closure s) t = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- Replacing a set by its closure does not change the Hausdorff distance. -/ @[simp] lemma Hausdorff_dist_closure₂ : Hausdorff_dist s (closure t) = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- The Hausdorff distance between two sets and their closures coincide -/ @[simp] lemma Hausdorff_dist_closure : Hausdorff_dist (closure s) (closure t) = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- Two sets are at zero Hausdorff distance if and only if they have the same closures -/ lemma Hausdorff_dist_zero_iff_closure_eq_closure (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s t = 0 ↔ closure s = closure t := by simp [Hausdorff_edist_zero_iff_closure_eq_closure.symm, Hausdorff_dist, ennreal.to_real_eq_zero_iff, fin] /-- Two closed sets are at zero Hausdorff distance if and only if they coincide -/ lemma Hausdorff_dist_zero_iff_eq_of_closed (hs : is_closed s) (ht : is_closed t) (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s t = 0 ↔ s = t := by simp [(Hausdorff_edist_zero_iff_eq_of_closed hs ht).symm, Hausdorff_dist, ennreal.to_real_eq_zero_iff, fin] end --section end metric --namespace
fac166b63c5fcc7f9689ad0c22ce856f84415599
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/monoidal/functorial.lean
70225b04057d0682a68cce87f58da5e6364e5e4c
[ "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,681
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.monoidal.functor import category_theory.functor.functorial /-! # Unbundled lax monoidal functors ## Design considerations The essential problem I've encountered that requires unbundled functors is having an existing (non-monoidal) functor `F : C ⥤ D` between monoidal categories, and wanting to assert that it has an extension to a lax monoidal functor. The two options seem to be 1. Construct a separate `F' : lax_monoidal_functor C D`, and assert `F'.to_functor ≅ F`. 2. Introduce unbundled functors and unbundled lax monoidal functors, and construct `lax_monoidal F.obj`, then construct `F' := lax_monoidal_functor.of F.obj`. Both have costs, but as for option 2. the cost is in library design, while in option 1. the cost is users having to carry around additional isomorphisms forever, I wanted to introduce unbundled functors. TODO: later, we may want to do this for strong monoidal functors as well, but the immediate application, for enriched categories, only requires this notion. -/ open category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ open category_theory.category open category_theory.functor namespace category_theory open monoidal_category variables {C : Type u₁} [category.{v₁} C] [monoidal_category.{v₁} C] {D : Type u₂} [category.{v₂} D] [monoidal_category.{v₂} D] /-- An unbundled description of lax monoidal functors. -/ -- Perhaps in the future we'll redefine `lax_monoidal_functor` in terms of this, -- but that isn't the immediate plan. class lax_monoidal (F : C → D) [functorial.{v₁ v₂} F] := -- unit morphism (ε [] : 𝟙_ D ⟶ F (𝟙_ C)) -- tensorator (μ [] : Π X Y : C, (F X) ⊗ (F Y) ⟶ F (X ⊗ Y)) (μ_natural' : ∀ {X Y X' Y' : C} (f : X ⟶ Y) (g : X' ⟶ Y'), ((map F f) ⊗ (map F g)) ≫ μ Y Y' = μ X X' ≫ map F (f ⊗ g) . obviously) -- associativity of the tensorator (associativity' : ∀ (X Y Z : C), (μ X Y ⊗ 𝟙 (F Z)) ≫ μ (X ⊗ Y) Z ≫ map F (α_ X Y Z).hom = (α_ (F X) (F Y) (F Z)).hom ≫ (𝟙 (F X) ⊗ μ Y Z) ≫ μ X (Y ⊗ Z) . obviously) -- unitality (left_unitality' : ∀ X : C, (λ_ (F X)).hom = (ε ⊗ 𝟙 (F X)) ≫ μ (𝟙_ C) X ≫ map F (λ_ X).hom . obviously) (right_unitality' : ∀ X : C, (ρ_ (F X)).hom = (𝟙 (F X) ⊗ ε) ≫ μ X (𝟙_ C) ≫ map F (ρ_ X).hom . obviously) restate_axiom lax_monoidal.μ_natural' attribute [simp] lax_monoidal.μ_natural restate_axiom lax_monoidal.left_unitality' restate_axiom lax_monoidal.right_unitality' -- The unitality axioms cannot be used as simp lemmas because they require -- higher-order matching to figure out the `F` and `X` from `F X`. restate_axiom lax_monoidal.associativity' attribute [simp] lax_monoidal.associativity namespace lax_monoidal_functor /-- Construct a bundled `lax_monoidal_functor` from the object level function and `functorial` and `lax_monoidal` typeclasses. -/ @[simps] def of (F : C → D) [I₁ : functorial.{v₁ v₂} F] [I₂ : lax_monoidal.{v₁ v₂} F] : lax_monoidal_functor.{v₁ v₂} C D := { obj := F, ..I₁, ..I₂ } end lax_monoidal_functor instance (F : lax_monoidal_functor.{v₁ v₂} C D) : lax_monoidal.{v₁ v₂} (F.obj) := { .. F } section instance lax_monoidal_id : lax_monoidal.{v₁ v₁} (id : C → C) := { ε := 𝟙 _, μ := λ X Y, 𝟙 _ } end -- TODO instances for composition, as required -- TODO `strong_monoidal`, as well as `lax_monoidal` end category_theory
d4074be63c5d68c1ecf2803b5ba47c54f7d6bae2
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/data/family.lean
f3b1b7ca85c25b48bb02bc8ed753114737b8c4d6
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,893
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland This file sets up some definitions for working with parametrised families of types, and morphisms between them. In other words, we essentially work with the category of arrows in the category of types. A key issue is as follows. Suppose we have a family X : B → Type*, and two terms b0 and b1 of B, and terms x0 : (X b0) and x1 : (X b1) of the corresponding fibres. Suppose that b0 and b1 are provably equal, but not definitionally equal. Lean will then regard the expression x0 = x1 as not being meaningful. There are two ways around this. One way is to use the heterogenous equality relation x0 == x1 instead of x0 = x1. The other is to find an explicit term e of type b0 = b1, then (eq.mp e x0) will have type (X b1) and so (eq.mp e x0) = x1 will be meaningful. A large part of this file deals with the properties of these two approaches and the relationship between them. It is far from clear that the approach taken here is optimal. -/ import data.equiv.basic import data.heq_extra /- This defines the notion of a morphism between two parametrised families. -/ structure family_fun {B : Type*} (X : B → Type*) {C : Type*} (Y : C → Type*) := (base : B → C) (fiber : ∀ {b : B}, (X b) → (Y (base b))) /- The following lemma says roughly the following: if some property P makes sense for heterogenously equal pairs, and is true for definitionally equal pairs, then it is true for all heterogenously equal pairs. -/ lemma family_eq_rec {B : Type*} {X : B → Type*} (P : ∀ {b0 b1 : B} {x0 : X b0} {x1 : X b1} (eb : b0 = b1) (ex : x0 == x1), Prop) (p : ∀ (b : B) (x : X b), @P b b x x (@rfl B b) (heq.refl x)) : ∀ b0 b1 x0 x1 eb ex, @P b0 b1 x0 x1 eb ex := begin intros b0 b1 x0 x1 eb ex, rcases eb, rcases (eq_of_heq ex), exact p b0 x0 end namespace family_fun def total : ∀ {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f : family_fun X Y) (x : Σ (b : B), X b), (Σ (c : C), Y c) := begin intros,exact ⟨f.base x.1, f.fiber x.2⟩ end /- This just makes b explicit as an argument of f.fiber -/ def fiber_alt {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f : family_fun X Y) : ∀ (b : B), (X b) → (Y (f.base b)) := f.fiber /- The identity morphism of a parametrised family -/ def id {B : Type*} (X : B → Type*) : family_fun X X := { base := @id B, fiber := λ b, (@id (X b))} /- Composition of morphisms of parametrised families -/ def compose {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} {D : Type*} {Z : D → Type*} (g : family_fun Y Z) (f : family_fun X Y) : (family_fun X Z) := { base := g.base ∘ f.base, fiber := λ (b : B) (x : X b), g.fiber (f.fiber x) } /- Morphisms preserve heterogenous equality -/ def congr_args {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f : family_fun X Y) {b0 b1 : B} (eb : b0 = b1) {x0 : X b0} {x1 : X b1} (ex : x0 == x1) : f.fiber x0 == f.fiber x1 := heq_subst f.base f.fiber eb ex /- We now define a notion of equivalence for morphisms: morphisms f0 and f1 are equivalent if they are pointwise equal on the base and pointwise heterogenously equal on fibres. Later we prove a kind of function extensionality result, that equivalent morphisms are equal. However, I think that this result is not "computationally pure" and so inhibits virtual machine compilation; so it is preferable to avoid it and work with equivalence. But I do not have a good understanding of all the relevant issues. -/ def hequiv {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f0 : family_fun X Y) (f1 : family_fun X Y) : Prop := (∀ (b : B), f0.base b = f1.base b) ∧ (forall (b : B) (x : X b), f0.fiber x == f1.fiber x) lemma heq_rec (B : Type*) (X : B → Type*) (b0 b1 : B) (eb : b0 = b1) (x : X b0) : x == (@eq.rec_on B b0 X b1 eb x) := begin exact eq.dcases_on eb (heq.refl x) end /- The next three lemmas show that we have indeed defined an equivalence relation on morphisms. -/ lemma hequiv.refl {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f : family_fun X Y) : hequiv f f := begin split, exact λ (b : B), rfl, exact λ (b : B) (x : X b), heq.refl (f.fiber x) end lemma hequiv.symm {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f0 f1 : family_fun X Y) : (hequiv f0 f1) → (hequiv f1 f0) := begin intro e, let eb := λ (b : B), (e.left b).symm, let ex := λ (b : B) (x : X b), (e.right b x).symm, exact and.intro eb ex end lemma hequiv.trans {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f0 f1 f2 : family_fun X Y) : (hequiv f0 f1) → (hequiv f1 f2) → (hequiv f0 f2) := begin intros e01 e12,split, {intro b, exact (e01.left b).trans(e12.left b), },{ intros b x, exact (e01.right b x).trans(e12.right b x), } end /- We now have three different results, all of which prove that two different morphisms are equal. The hypotheses involve several different combinations of pointwise/global (heterogenous) equality on the base or fibres. -/ lemma ext0 {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f0 f1 : family_fun X Y) (eb : f0.base = f1.base) (et : f0.fiber == f1.fiber) : f0 = f1 := begin cases f0 with f0b f0t, cases f1 with f1b f1t, exact @heq_sigma (B → C) (λ fb, Π b,(X b) → (Y (fb b))) (family_fun X Y) family_fun.mk f0b f1b @f0t @f1t eb et end lemma ext1 {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f0 f1 : family_fun X Y) (eb : f0.base = f1.base) (et : ∀ b, f0.fiber_alt b == f1.fiber_alt b) : f0 = f1 := begin let M : (B → C) → (B → C) → Prop := λ g0 g1, (∀ h0 : (∀ b, (X b) → (Y (g0 b))), ∀ h1 : (∀ b, (X b) → (Y (g1 b))), ∀ eh : (∀ b, (h0 b) == (h1 b)), h0 == h1), have m : ∀ g : B → C, M g g := begin intros g h0 h1 eh, have eh1 : ∀ b, h0 b = h1 b := λ b,eq_of_heq (eh b), exact heq_of_eq (@funext B (λ b, (X b) → (Y (g b))) h0 h1 eh1), end, let N : ∀ (g0 g1 : (B → C)), g0 = g1 → M g0 g1 := begin intros g0 g1 e,cases e, exact m g0, end, have et1 : f0.fiber == f1.fiber := begin exact N f0.base f1.base eb f0.fiber f1.fiber et, end, exact ext0 f0 f1 eb et1 end lemma ext {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f0 f1 : family_fun X Y) (e : hequiv f0 f1) : f0 = f1 := begin have eb : f0.base = f1.base := funext e.left, let M : (B → C) → (B → C) → Prop := λ g0 g1, (∀ h0 : (∀ b, (X b) → (Y (g0 b))), ∀ h1 : (∀ b, (X b) → (Y (g1 b))), ∀ eh : (∀ b x, (h0 b x) == (h1 b x)), ∀ b, h0 b == h1 b), have m : ∀ (g : B → C), M g g := begin intros g h0 h1 e b, have e1 : ∀ x, h0 b x = h1 b x := λ x,eq_of_heq (e b x), exact heq_of_eq (@funext (X b) (λ _,Y (g b)) (h0 b) (h1 b) e1), end, let N : ∀ (g0 g1 : (B → C)), g0 = g1 → M g0 g1 := begin intros g0 g1 e,cases e, exact m g0, end, have et : ∀ b, f0.fiber_alt b == f1.fiber_alt b := begin exact N f0.base f1.base eb f0.fiber f1.fiber e.right, end, exact ext1 f0 f1 eb et end end family_fun /- We now have some definitions and results about equivalences between parametrised families. Such an equivalence is encoded as a structure involving two morphisms of families, together with proofs that the relevant composites are equivalent to identities with respect to the equivalence relation defined above. -/ structure family_equiv {B : Type*} (X : B → Type*) {C : Type*} (Y : C → Type*) := (to_fun : family_fun X Y) (inv_fun : family_fun Y X) (left_inv : family_fun.hequiv (family_fun.compose inv_fun to_fun) (family_fun.id X)) (right_inv : family_fun.hequiv (family_fun.compose to_fun inv_fun) (family_fun.id Y)) namespace family_equiv /- In the standard library there is an existing notion of an equivalence between two types. In the next two results, we prove that an equivalence of families gives an equivalence between base types, and also equivalences between fiber types. -/ def base {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f : family_equiv X Y) : equiv B C := { to_fun := f.to_fun.base, inv_fun := f.inv_fun.base, left_inv := f.left_inv.left, right_inv := f.right_inv.left } def fiber {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f : family_equiv X Y) (b : B) : equiv (X b) (Y (f.to_fun.base b)) := begin let c : C := f.to_fun.base b, let b0 : B := f.inv_fun.base c, let eb : b0 = b := f.left_inv.left b, let tfn := f.to_fun.fiber_alt b, let ifn : (Y c) → (X b) := λ y, @eq.rec_on B (f.inv_fun.base c) X b eb (f.inv_fun.fiber y), let e0 : ∀ (y : Y c), ifn y == f.inv_fun.fiber y := λ y, (heq_rec eb (f.inv_fun.fiber y)).symm, let li : ∀ x : (X b), ifn (tfn x) = x := λ x, eq_of_heq ((e0 (tfn x)).trans (f.left_inv.right b x)), let ri : ∀ y : (Y c), tfn (ifn y) = y := begin intro y, let x0 := ifn y, let x1 := f.inv_fun.fiber y, let y0 := f.to_fun.fiber x0, let y1 := f.to_fun.fiber x1, let e3 : y1 == y0 := heq_subst f.to_fun.base f.to_fun.fiber eb (e0 y).symm, exact eq_of_heq (e3.symm.trans (f.right_inv.right c y)), end, exact { to_fun := tfn, inv_fun := ifn, left_inv := li, right_inv := ri } end /- We next show that there are identity equivalences of families, and that equivalences of families can be reversed or composed. The corresponding results for equivalences of types are proved in the standard library. We follow the notational conventions established there by writing refl, symm and trans rather than referring to identities, reversal and composition. -/ def refl {B : Type*} (X : B → Type*) : family_equiv X X := { to_fun := family_fun.id X, inv_fun := family_fun.id X, left_inv := and.intro (λ (b : B),@rfl B b) (λ (b : B) (x : X b),heq.refl x), right_inv := and.intro (λ (b : B),@rfl B b) (λ (b : B) (x : X b),heq.refl x), } def symm {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} (f : family_equiv X Y) : family_equiv Y X := { to_fun := f.inv_fun, inv_fun := f.to_fun, left_inv := f.right_inv, right_inv := f.left_inv } def trans {B : Type*} {X : B → Type*} {C : Type*} {Y : C → Type*} {D : Type*} {Z : D → Type*} (f : family_equiv X Y) (g : family_equiv Y Z) : (family_equiv X Z) := { to_fun := family_fun.compose g.to_fun f.to_fun, inv_fun := family_fun.compose f.inv_fun g.inv_fun, left_inv := begin split, {intro b, let e0 := f.left_inv.left b, let e1 := g.left_inv.left (f.to_fun.base b), dsimp[family_fun.compose,family_fun.id] at e0 e1 ⊢, exact (congr_arg f.inv_fun.base e1).trans e0 },{ intros b x, let c := f.to_fun.base b, let y := f.to_fun.fiber x, let eb0 := f.left_inv.left b, let eb1 := g.left_inv.left c, let et0 := f.left_inv.right b x, let et1 := g.left_inv.right c y, dsimp[family_fun.compose,family_fun.id] at eb0 eb1 et0 et1 ⊢, exact (family_fun.congr_args f.inv_fun eb1 et1).trans et0, } end, right_inv := begin split, {intro d, let e0 := g.right_inv.left d, let e1 := f.right_inv.left (g.inv_fun.base d), dsimp[family_fun.compose,family_fun.id] at e0 e1 ⊢, exact (congr_arg g.to_fun.base e1).trans e0 },{ intros d z, let c := g.inv_fun.base d, let y := g.inv_fun.fiber z, let eb0 := g.right_inv.left d, let eb1 := f.right_inv.left c, let et0 := g.right_inv.right d z, let et1 := f.right_inv.right c y, dsimp[family_fun.compose,family_fun.id] at eb0 eb1 et0 et1 ⊢, exact (family_fun.congr_args g.to_fun eb1 et1).trans et0, } end, } end family_equiv
bdf8f7ba9a88fa01334204cebde1603af02a2453
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/category_theory/limits/shapes/constructions/equalizers.lean
486b0149f5233d6d60f57e03c931550faf2b8873
[ "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
2,701
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.binary_products import category_theory.limits.shapes.pullbacks /-! # Constructing equalizers from pullbacks and binary products. If a category has pullbacks and binary products, then it has equalizers. TODO: provide the dual result. -/ universes v u open category_theory category_theory.category namespace category_theory.limits variables {C : Type u} [category.{v} C] [has_binary_products C] [has_pullbacks C] -- We hide the "implementation details" inside a namespace namespace has_equalizers_of_pullbacks_and_binary_products /-- Define the equalizing object -/ @[reducible] def construct_equalizer (F : walking_parallel_pair ⥤ C) : C := pullback (prod.lift (𝟙 _) (F.map walking_parallel_pair_hom.left)) (prod.lift (𝟙 _) (F.map walking_parallel_pair_hom.right)) /-- Define the equalizing morphism -/ abbreviation pullback_fst (F : walking_parallel_pair ⥤ C) : construct_equalizer F ⟶ F.obj walking_parallel_pair.zero := pullback.fst lemma pullback_fst_eq_pullback_snd (F : walking_parallel_pair ⥤ C) : pullback_fst F = pullback.snd := by convert pullback.condition =≫ limits.prod.fst; simp /-- Define the equalizing cone -/ @[reducible] def equalizer_cone (F : walking_parallel_pair ⥤ C) : cone F := cone.of_fork (fork.of_ι (pullback_fst F) (begin conv_rhs { rw pullback_fst_eq_pullback_snd, }, convert pullback.condition =≫ limits.prod.snd using 1; simp end)) /-- Show the equalizing cone is a limit -/ def equalizer_cone_is_limit (F : walking_parallel_pair ⥤ C) : is_limit (equalizer_cone F) := { lift := begin intro c, apply pullback.lift (c.π.app _) (c.π.app _), apply limit.hom_ext, rintro (_ | _); simp end, fac' := by rintros c (_ | _); simp, uniq' := begin intros c _ J, have J0 := J walking_parallel_pair.zero, simp at J0, apply pullback.hom_ext, { rwa limit.lift_π }, { erw [limit.lift_π, ← J0, pullback_fst_eq_pullback_snd] } end } end has_equalizers_of_pullbacks_and_binary_products open has_equalizers_of_pullbacks_and_binary_products /-- Any category with pullbacks and binary products, has equalizers. -/ -- This is not an instance, as it is not always how one wants to construct equalizers! def has_equalizers_of_pullbacks_and_binary_products : has_equalizers C := { has_limits_of_shape := { has_limit := λ F, { cone := equalizer_cone F, is_limit := equalizer_cone_is_limit F } } } end category_theory.limits
f16ea858be45e083cfdbdc7fa4218a6cc2005abe
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/equiv_rw.lean
ca74919c3eecc81378240cdbdc49c22ce8bd0d1e
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
13,474
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import logic.equiv.defs import tactic.clear import tactic.simp_result import tactic.apply import control.equiv_functor.instances -- these make equiv_rw more powerful! import logic.equiv.functor -- so do these! /-! # The `equiv_rw` tactic transports goals or hypotheses along equivalences. The basic syntax is `equiv_rw e`, where `e : α ≃ β` is an equivalence. This will try to replace occurrences of `α` in the goal with `β`, for example transforming * `⊢ α` to `⊢ β`, * `⊢ option α` to `⊢ option β` * `⊢ {a // P}` to `{b // P (⇑(equiv.symm e) b)}` The tactic can also be used to rewrite hypotheses, using the syntax `equiv_rw e at h`. ## Implementation details The main internal function is `equiv_rw_type e t`, which attempts to turn an expression `e : α ≃ β` into a new equivalence with left hand side `t`. As an example, with `t = option α`, it will generate `functor.map_equiv option e`. This is achieved by generating a new synthetic goal `%%t ≃ _`, and calling `solve_by_elim` with an appropriate set of congruence lemmas. To avoid having to specify the relevant congruence lemmas by hand, we mostly rely on `equiv_functor.map_equiv` and `bifunctor.map_equiv` along with some structural congruence lemmas such as * `equiv.arrow_congr'`, * `equiv.subtype_equiv_of_subtype'`, * `equiv.sigma_congr_left'`, and * `equiv.Pi_congr_left'`. The main `equiv_rw` function, when operating on the goal, simply generates a new equivalence `e'` with left hand side matching the target, and calls `apply e'.inv_fun`. When operating on a hypothesis `x : α`, we introduce a new fact `h : x = e.symm (e x)`, revert this, and then attempt to `generalize`, replacing all occurrences of `e x` with a new constant `y`, before `intro`ing and `subst`ing `h`, and renaming `y` back to `x`. ## Future improvements In a future PR I anticipate that `derive equiv_functor` should work on many examples, (internally using `transport`, which is in turn based on `equiv_rw`) and we can incrementally bootstrap the strength of `equiv_rw`. An ambitious project might be to add `equiv_rw!`, a tactic which, when failing to find appropriate `equiv_functor` instances, attempts to `derive` them on the spot. For now `equiv_rw` is entirely based on `equiv`, but the framework can readily be generalised to also work with other types of equivalences, for example specific notations such as ring equivalence (`≃+*`), or general categorical isomorphisms (`≅`). This will allow us to transport across more general types of equivalences, but this will wait for another subsequent PR. -/ namespace tactic /-- A list of lemmas used for constructing congruence equivalences. -/ -- Although this looks 'hard-coded', in fact the lemma `equiv_functor.map_equiv` -- allows us to extend `equiv_rw` simply by constructing new instance so `equiv_functor`. -- TODO: We should also use `category_theory.functorial` and `category_theory.hygienic` instances. -- (example goal: we could rewrite along an isomorphism of rings (either as `R ≅ S` or `R ≃+* S`) -- and turn an `x : mv_polynomial σ R` into an `x : mv_polynomial σ S`.). meta def equiv_congr_lemmas : list (tactic expr) := [ `equiv.of_iff, -- TODO decide what to do with this; it's an equiv_bifunctor? `equiv.equiv_congr, -- The function arrow is technically a bifunctor `Typeᵒᵖ → Type → Type`, -- but the pattern matcher will never see this. `equiv.arrow_congr', -- Allow rewriting in subtypes: `equiv.subtype_equiv_of_subtype', -- Allow rewriting in the first component of a sigma-type: `equiv.sigma_congr_left', -- Allow rewriting ∀s: -- (You might think that repeated application of `equiv.forall_congr' -- would handle the higher arity cases, but unfortunately unification is not clever enough.) `equiv.forall₃_congr', `equiv.forall₂_congr', `equiv.forall_congr', -- Allow rewriting in argument of Pi types: `equiv.Pi_congr_left', -- Handles `sum` and `prod`, and many others: `bifunctor.map_equiv, -- Handles `list`, `option`, `unique`, and many others: `equiv_functor.map_equiv, -- We have to filter results to ensure we don't cheat and use exclusively -- `equiv.refl` and `iff.refl`! `equiv.refl, `iff.refl ].map (λ n, mk_const n) declare_trace equiv_rw_type /-- Configuration structure for `equiv_rw`. * `max_depth` bounds the search depth for equivalences to rewrite along. The default value is 10. (e.g., if you're rewriting along `e : α ≃ β`, and `max_depth := 2`, you can rewrite `option (option α))` but not `option (option (option α))`. -/ meta structure equiv_rw_cfg := (max_depth : ℕ := 10) /-- Implementation of `equiv_rw_type`, using `solve_by_elim`. Expects a goal of the form `t ≃ _`, and tries to solve it using `eq : α ≃ β` and congruence lemmas. -/ meta def equiv_rw_type_core (eq : expr) (cfg : equiv_rw_cfg) : tactic unit := do /- We now call `solve_by_elim` to try to generate the requested equivalence. There are a few subtleties! * We make sure that `eq` is the first lemma, so it is applied whenever possible. * In `equiv_congr_lemmas`, we put `equiv.refl` last so it is only used when it is not possible to descend further. * Since some congruence lemmas generate subgoals with `∀` statements, we use the `pre_apply` subtactic of `solve_by_elim` to preprocess each new goal with `intros`. -/ solve_by_elim { use_symmetry := false, use_exfalso := false, lemma_thunks := some (pure eq :: equiv_congr_lemmas), ctx_thunk := pure [], max_depth := cfg.max_depth, -- Subgoals may contain function types, -- and we want to continue trying to construct equivalences after the binders. pre_apply := tactic.intros >> skip, backtrack_all_goals := tt, -- If solve_by_elim gets stuck, make sure it isn't because there's a later `≃` or `↔` goal -- that we should still attempt. discharger := `[success_if_fail { match_target _ ≃ _ }] >> `[success_if_fail { match_target _ ↔ _ }] >> (`[show _ ≃ _] <|> `[show _ ↔ _]) <|> trace_if_enabled `equiv_rw_type "Failed, no congruence lemma applied!" >> failed, -- We use the `accept` tactic in `solve_by_elim` to provide tracing. accept := λ goals, lock_tactic_state (do when_tracing `equiv_rw_type (do goals.mmap pp >>= λ goals, trace format!"So far, we've built: {goals}"), done <|> when_tracing `equiv_rw_type (do gs ← get_goals, gs ← gs.mmap (λ g, infer_type g >>= pp), trace format!"Attempting to adapt to {gs}")) } /-- `equiv_rw_type e t` rewrites the type `t` using the equivalence `e : α ≃ β`, returning a new equivalence `t ≃ t'`. -/ meta def equiv_rw_type (eqv : expr) (ty : expr) (cfg : equiv_rw_cfg) : tactic expr := do when_tracing `equiv_rw_type (do ty_pp ← pp ty, eqv_pp ← pp eqv, eqv_ty_pp ← infer_type eqv >>= pp, trace format!"Attempting to rewrite the type `{ty_pp}` using `{eqv_pp} : {eqv_ty_pp}`."), `(_ ≃ _) ← infer_type eqv | fail format!"{eqv} must be an `equiv`", -- We prepare a synthetic goal of type `(%%ty ≃ _)`, for some placeholder right hand side. equiv_ty ← to_expr ``(%%ty ≃ _), -- Now call `equiv_rw_type_core`. new_eqv ← prod.snd <$> (solve_aux equiv_ty $ equiv_rw_type_core eqv cfg), -- Check that we actually used the equivalence `eq` -- (`equiv_rw_type_core` will always find `equiv.refl`, -- but hopefully only after all other possibilities) new_eqv ← instantiate_mvars new_eqv, -- We previously had `guard (eqv.occurs new_eqv)` here, but `kdepends_on` is more reliable. kdepends_on new_eqv eqv >>= guardb <|> (do eqv_pp ← pp eqv, ty_pp ← pp ty, fail format!"Could not construct an equivalence from {eqv_pp} of the form: {ty_pp} ≃ _"), -- Finally we simplify the resulting equivalence, -- to compress away some `map_equiv equiv.refl` subexpressions. prod.fst <$> new_eqv.simp {fail_if_unchanged := ff} /-- Attempt to replace the hypothesis with name `x` by transporting it along the equivalence in `e : α ≃ β`. -/ meta def equiv_rw_hyp (x : name) (e : expr) (cfg : equiv_rw_cfg := {}) : tactic unit := -- We call `dsimp_result` to perform the beta redex introduced by `revert` dsimp_result (do x' ← get_local x, x_ty ← infer_type x', -- Adapt `e` to an equivalence with left-hand-side `x_ty`. e ← equiv_rw_type e x_ty cfg, eq ← to_expr ``(%%x' = equiv.symm %%e (equiv.to_fun %%e %%x')), prf ← to_expr ``((equiv.symm_apply_apply %%e %%x').symm), h ← note_anon eq prf, -- Revert the new hypothesis, so it is also part of the goal. revert h, ex ← to_expr ``(equiv.to_fun %%e %%x'), -- Now call `generalize`, -- attempting to replace all occurrences of `e x`, -- calling it for now `j : β`, with `k : x = e.symm j`. generalize ex (by apply_opt_param) transparency.none, -- Reintroduce `x` (now of type `b`), and the hypothesis `h`. intro x, h ← intro1, -- Finally, if we're working on properties, substitute along `h`, then do some cleanup, -- and if we're working on data, just throw out the old `x`. b ← target >>= is_prop, if b then do subst h, `[try { simp only with equiv_rw_simp }] else -- We may need to unfreeze `x` before we can `clear` it. unfreezing_hyp x' (clear' tt [x']) <|> fail format!"equiv_rw expected to be able to clear the original hypothesis {x}, but couldn't.", skip) {fail_if_unchanged := ff} tt -- call `dsimp_result` with `no_defaults := tt`. /-- Rewrite the goal using an equiv `e`. -/ meta def equiv_rw_target (e : expr) (cfg : equiv_rw_cfg := {}) : tactic unit := do t ← target, e ← equiv_rw_type e t cfg, s ← to_expr ``(equiv.inv_fun %%e), tactic.eapply s, skip end tactic namespace tactic.interactive open tactic setup_tactic_parser /-- Auxiliary function to call `equiv_rw_hyp` on a `list pexpr` recursively. -/ meta def equiv_rw_hyp_aux (hyp : name) (cfg : equiv_rw_cfg) (permissive : bool := ff) : list expr → itactic | [] := skip | (e :: t) := do if permissive then equiv_rw_hyp hyp e cfg <|> skip else equiv_rw_hyp hyp e cfg, equiv_rw_hyp_aux t /-- Auxiliary function to call `equiv_rw_target` on a `list pexpr` recursively. -/ meta def equiv_rw_target_aux (cfg : equiv_rw_cfg) (permissive : bool) : list expr → itactic | [] := skip | (e :: t) := do if permissive then equiv_rw_target e cfg <|> skip else equiv_rw_target e cfg, equiv_rw_target_aux t /-- `equiv_rw e at h₁ h₂ ⋯`, where each `hᵢ : α` is a hypothesis, and `e : α ≃ β`, will attempt to transport each `hᵢ` along `e`, producing a new hypothesis `hᵢ : β`, with all occurrences of `hᵢ` in other hypotheses and the goal replaced with `e.symm hᵢ`. `equiv_rw e` will attempt to transport the goal along an equivalence `e : α ≃ β`. In its minimal form it replaces the goal `⊢ α` with `⊢ β` by calling `apply e.inv_fun`. `equiv_rw [e₁, e₂, ⋯] at h₁ h₂ ⋯` is equivalent to `{ equiv_rw [e₁, e₂, ⋯] at h₁, equiv_rw [e₁, e₂, ⋯] at h₂, ⋯ }`. `equiv_rw [e₁, e₂, ⋯] at *` will attempt to apply `equiv_rw [e₁, e₂, ⋯]` on the goal and on each expression available in the local context (except on the `eᵢ`s themselves), failing silently when it can't. Failing on a rewrite for a certain `eᵢ` at a certain hypothesis `h` doesn't stop `equiv_rw` from trying the other equivalences on the list at `h`. This only happens for the wildcard location. `equiv_rw` will also try rewriting under (equiv_)functors, so it can turn a hypothesis `h : list α` into `h : list β` or a goal `⊢ unique α` into `⊢ unique β`. The maximum search depth for rewriting in subexpressions is controlled by `equiv_rw e {max_depth := n}`. -/ meta def equiv_rw (l : parse pexpr_list_or_texpr) (locat : parse location) (cfg : equiv_rw_cfg := {}) : itactic := do es ← l.mmap (λ e, to_expr e), match locat with | loc.wildcard := do equiv_rw_target_aux cfg tt es, ctx ← local_context, ctx.mmap (λ e, if e ∈ es then skip else equiv_rw_hyp_aux e.local_pp_name cfg tt es), skip | loc.ns names := do names.mmap (λ hyp', match hyp' with | some hyp := equiv_rw_hyp_aux hyp cfg ff es | none := equiv_rw_target_aux cfg ff es end), skip end add_tactic_doc { name := "equiv_rw", category := doc_category.tactic, decl_names := [`tactic.interactive.equiv_rw], tags := ["rewriting", "equiv", "transport"] } /-- Solve a goal of the form `t ≃ _`, by constructing an equivalence from `e : α ≃ β`. This is the same equivalence that `equiv_rw` would use to rewrite a term of type `t`. A typical usage might be: ``` have e' : option α ≃ option β := by equiv_rw_type e ``` -/ meta def equiv_rw_type (e : parse texpr) (cfg : equiv_rw_cfg := {}) : itactic := do `(%%t ≃ _) ← target | fail "`equiv_rw_type` solves goals of the form `t ≃ _`.", e ← to_expr e, tactic.equiv_rw_type e t cfg >>= tactic.exact add_tactic_doc { name := "equiv_rw_type", category := doc_category.tactic, decl_names := [`tactic.interactive.equiv_rw_type], tags := ["rewriting", "equiv", "transport"] } end tactic.interactive
fb4441ddcd79b340bf1c1302b99938851e8cf7f5
efce24474b28579aba3272fdb77177dc2b11d7aa
/src/homotopy_theory/topological_spaces/fundamental_groupoid.lean
61ac89e6ce73a55ac0a6a2e4367a1e3629bc1fd7
[ "Apache-2.0" ]
permissive
rwbarton/lean-homotopy-theory
cff499f24268d60e1c546e7c86c33f58c62888ed
39e1b4ea1ed1b0eca2f68bc64162dde6a6396dee
refs/heads/lean-3.4.2
1,622,711,883,224
1,598,550,958,000
1,598,550,958,000
136,023,667
12
6
Apache-2.0
1,573,187,573,000
1,528,116,262,000
Lean
UTF-8
Lean
false
false
1,541
lean
import category_theory.bundled import category_theory.induced import homotopy_theory.formal.cofibrations.track import homotopy_theory.formal.i_category.cofibration_category import .pi_n noncomputable theory open category_theory local notation f ` ∘ `:80 g:80 := g ≫ f namespace homotopy_theory.topological_spaces open homotopy_theory.cofibrations open Top local notation `Top` := Top.{0} def Pi₁_ (X : Top) : Type := X instance {X : Top} : groupoid (Pi₁_ X) := show groupoid (induced_category (homotopy_class_groupoid (all_objects_cofibrant.cofibrant Top.point) (canonical_cylinder.{0 1} Top.point) (all_objects_fibrant X)) (λ x, (Top.const x : Top.point ⟶ X))), by apply_instance def Pi₁_induced {X Y : Top} (f : X ⟶ Y) : Pi₁_ X ↝ Pi₁_ Y := induced_functor' _ _ (homotopy_class_functor (all_objects_fibrant X) (all_objects_fibrant Y) f) f (by intros; refl) def Pi₁ : Top ↝ Gpd := { obj := λ X, Gpd.mk_ob (Pi₁_ X), map := λ X Y f, Gpd.mk_hom (Pi₁_induced f), map_id' := λ X, begin dsimp [Pi₁_induced], simp only [homotopy_class_functor.map_id], apply induced_functor_id end, map_comp' := λ X Y Z f g, begin dsimp [Gpd.mk_hom, Pi₁_induced, induced_functor, Gpd.category], have : ∀ W : Top, fibrant W := all_objects_fibrant, simp only [homotopy_class_functor.map_comp (all_objects_fibrant X) (all_objects_fibrant Y) _ (all_objects_fibrant Z)], apply induced_functor_comp end } end homotopy_theory.topological_spaces
ca43b493222910d9daa46bd8230bca87a9c29f60
bc6b522ca01a7d1eddd58687225d93b4bb90fc65
/test/simple.lean
a96111c4e28acba551fb78adf809004a515d86bd
[]
no_license
cipher1024/olean-rs
c8fcf0a47570e922be4dfd33b5f3b6ac9571e6f7
4707d26177733753ee4195b4b0883b4089e85f11
refs/heads/master
1,588,599,805,382
1,557,346,494,000
1,557,346,494,000
179,171,289
1
0
null
1,554,245,858,000
1,554,245,858,000
null
UTF-8
Lean
false
false
17
lean
import system.io
b9b7735e53b2969c95eb27a3bbdd3ff4265e771b
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/conj.lean
5eacf2faf14ab575ba60f855eb5c823fa2f0dea1
[ "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
5,801
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 category_theory.endomorphism import algebra.group_power /-! # Conjugate morphisms by isomorphisms An isomorphism `α : X ≅ Y` defines - a monoid isomorphism `conj : End X ≃* End Y` by `α.conj f = α.inv ≫ f ≫ α.hom`; - a group isomorphism `conj_Aut : Aut X ≃* Aut Y` by `α.conj_Aut f = α.symm ≪≫ f ≪≫ α`. For completeness, we also define `hom_congr : (X ≅ X₁) → (Y ≅ Y₁) → (X ⟶ Y) ≃ (X₁ ⟶ Y₁)`, cf. `equiv.arrow_congr`. -/ universes v u namespace category_theory namespace iso variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 /- If `X` is isomorphic to `X₁` and `Y` is isomorphic to `Y₁`, then there is a natural bijection between `X ⟶ Y` and `X₁ ⟶ Y₁`. See also `equiv.arrow_congr`. -/ def hom_congr {X Y X₁ Y₁ : C} (α : X ≅ X₁) (β : Y ≅ Y₁) : (X ⟶ Y) ≃ (X₁ ⟶ Y₁) := { to_fun := λ f, α.inv ≫ f ≫ β.hom, inv_fun := λ f, α.hom ≫ f ≫ β.inv, left_inv := λ f, show α.hom ≫ (α.inv ≫ f ≫ β.hom) ≫ β.inv = f, by rw [category.assoc, category.assoc, β.hom_inv_id, α.hom_inv_id_assoc, category.comp_id], right_inv := λ f, show α.inv ≫ (α.hom ≫ f ≫ β.inv) ≫ β.hom = f, by rw [category.assoc, category.assoc, β.inv_hom_id, α.inv_hom_id_assoc, category.comp_id] } lemma hom_congr_apply {X Y X₁ Y₁ : C} (α : X ≅ X₁) (β : Y ≅ Y₁) (f : X ⟶ Y) : α.hom_congr β f = α.inv ≫ f ≫ β.hom := rfl lemma hom_congr_comp {X Y Z X₁ Y₁ Z₁ : C} (α : X ≅ X₁) (β : Y ≅ Y₁) (γ : Z ≅ Z₁) (f : X ⟶ Y) (g : Y ⟶ Z) : α.hom_congr γ (f ≫ g) = (α.hom_congr β f) ≫ (hom_congr β γ g) := by simp only [hom_congr_apply, category.assoc, β.hom_inv_id_assoc] @[simp] lemma hom_congr_refl {X Y : C} (f : X ⟶ Y) : (iso.refl X).hom_congr (iso.refl Y) f = f := by simp only [hom_congr_apply, iso.refl, category.comp_id, category.id_comp] @[simp] lemma hom_congr_trans {X₁ Y₁ X₂ Y₂ X₃ Y₃ : C} (α₁ : X₁ ≅ X₂) (β₁ : Y₁ ≅ Y₂) (α₂ : X₂ ≅ X₃) (β₂ : Y₂ ≅ Y₃) (f : X₁ ⟶ Y₁) : (α₁ ≪≫ α₂).hom_congr (β₁ ≪≫ β₂) f = (α₁.hom_congr β₁).trans (α₂.hom_congr β₂) f := by simp only [hom_congr_apply, equiv.trans_apply, iso.trans, category.assoc] @[simp] lemma hom_congr_symm {X₁ Y₁ X₂ Y₂ : C} (α : X₁ ≅ X₂) (β : Y₁ ≅ Y₂) : (α.hom_congr β).symm = α.symm.hom_congr β.symm := rfl variables {X Y : C} (α : X ≅ Y) /-- An isomorphism between two objects defines a monoid isomorphism between their monoid of endomorphisms. -/ def conj : End X ≃* End Y := { map_mul' := λ f g, hom_congr_comp α α α g f, .. hom_congr α α } lemma conj_apply (f : End X) : α.conj f = α.inv ≫ f ≫ α.hom := rfl @[simp] lemma conj_comp (f g : End X) : α.conj (f ≫ g) = (α.conj f) ≫ (α.conj g) := α.conj.map_mul g f @[simp] lemma conj_id : α.conj (𝟙 X) = 𝟙 Y := is_monoid_hom.map_one α.conj @[simp] lemma refl_conj (f : End X) : (@iso.refl C 𝒞 X).conj f = f := by rw [conj_apply, iso.refl_inv, iso.refl_hom, category.id_comp, category.comp_id] @[simp] lemma trans_conj {Z : C} (β : Y ≅ Z) (f : End X) : (α ≪≫ β).conj f = β.conj (α.conj f) := hom_congr_trans α α β β f @[simp] lemma symm_self_conj (f : End X) : α.symm.conj (α.conj f) = f := by rw [← trans_conj, α.self_symm_id, refl_conj] @[simp] lemma self_symm_conj (f : End Y) : α.conj (α.symm.conj f) = f := α.symm.symm_self_conj f @[simp] lemma conj_pow (f : End X) (n : ℕ) : α.conj (f^n) = (α.conj f)^n := α.conj.to_monoid_hom.map_pow f n /-- `conj` defines a group isomorphisms between groups of automorphisms -/ def conj_Aut : Aut X ≃* Aut Y := (Aut.units_End_equiv_Aut X).symm.trans $ (units.map_equiv α.conj).trans $ Aut.units_End_equiv_Aut Y lemma conj_Aut_apply (f : Aut X) : α.conj_Aut f = α.symm ≪≫ f ≪≫ α := by cases f; cases α; ext; refl @[simp] lemma conj_Aut_hom (f : Aut X) : (α.conj_Aut f).hom = α.conj f.hom := rfl @[simp] lemma trans_conj_Aut {Z : C} (β : Y ≅ Z) (f : Aut X) : (α ≪≫ β).conj_Aut f = β.conj_Aut (α.conj_Aut f) := by simp only [conj_Aut_apply, iso.trans_symm, iso.trans_assoc] @[simp] lemma conj_Aut_mul (f g : Aut X) : α.conj_Aut (f * g) = α.conj_Aut f * α.conj_Aut g := α.conj_Aut.map_mul f g @[simp] lemma conj_Aut_trans (f g : Aut X) : α.conj_Aut (f ≪≫ g) = α.conj_Aut f ≪≫ α.conj_Aut g := conj_Aut_mul α g f @[simp] lemma conj_Aut_pow (f : Aut X) (n : ℕ) : α.conj_Aut (f^n) = (α.conj_Aut f)^n := α.conj_Aut.to_monoid_hom.map_pow f n @[simp] lemma conj_Aut_gpow (f : Aut X) (n : ℤ) : α.conj_Aut (f^n) = (α.conj_Aut f)^n := α.conj_Aut.to_monoid_hom.map_gpow f n end iso namespace functor universes v₁ u₁ variables {C : Type u} [𝒞 : category.{v} C] {D : Type u₁} [𝒟 : category.{v₁} D] (F : C ⥤ D) include 𝒞 𝒟 lemma map_hom_congr {X Y X₁ Y₁ : C} (α : X ≅ X₁) (β : Y ≅ Y₁) (f : X ⟶ Y) : F.map (iso.hom_congr α β f) = iso.hom_congr (F.map_iso α) (F.map_iso β) (F.map f) := by simp only [iso.hom_congr_apply, F.map_comp, F.map_iso_inv, F.map_iso_hom] lemma map_conj {X Y : C} (α : X ≅ Y) (f : End X) : F.map (α.conj f) = (F.map_iso α).conj (F.map f) := map_hom_congr F α α f lemma map_conj_Aut (F : C ⥤ D) {X Y : C} (α : X ≅ Y) (f : Aut X) : F.map_iso (α.conj_Aut f) = (F.map_iso α).conj_Aut (F.map_iso f) := by ext; simp only [map_iso_hom, iso.conj_Aut_hom, F.map_conj] -- alternative proof: by simp only [iso.conj_Aut_apply, F.map_iso_trans, F.map_iso_symm] end functor end category_theory
8d54b5798f0854d5a4d4016d4c886223bac848ef
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Lean/Structure.lean
b69738bead64b41cb7888e25451344cd8615bbd5
[ "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
7,307
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 Helper functions for retrieving structure information. -/ import Lean.Environment import Lean.ProjFns /- TODO: We currently assume that the projection function for field `fieldName` at structure `structName` is `structName ++ fieldName`. This is incorrect for private projections. We will fix this by storing a mapping from `structure` + `fieldName` to projection function name in the environment. This modification will impact functions such as `getStructureFields` and `getProjFnForField?` -/ namespace Lean /-- Return true iff `constName` is the a non-recursive inductive datatype that has only one constructor. -/ def isStructureLike (env : Environment) (constName : Name) : Bool := match env.find? constName with | some (ConstantInfo.inductInfo { isRec := false, ctors := [ctor], .. }) => true | _ => false /-- We mark subobject fields by prefixing them with "_" in the structure's intro rule. -/ def mkInternalSubobjectFieldName (fieldName : Name) : Name := fieldName.appendBefore "_" def isInternalSubobjectFieldName : Name → Bool | Name.str _ s _ => s.length > 0 && s.get 0 == '_' | _ => false def deinternalizeFieldName : Name → Name | n@(Name.str p s _) => if s.length > 0 && s.get 0 == '_' then Name.mkStr p (s.drop 1) else n | n => n def getStructureCtor (env : Environment) (constName : Name) : ConstructorVal := match env.find? constName with | some (ConstantInfo.inductInfo { isRec := false, ctors := [ctorName], .. }) => match env.find? ctorName with | some (ConstantInfo.ctorInfo val) => val | _ => panic! "ill-formed environment" | _ => panic! "structure expected" private def getStructureFieldsAux (numParams : Nat) : Nat → Expr → Array Name → Array Name | i, Expr.forallE n d b _, fieldNames => if i < numParams then getStructureFieldsAux numParams (i+1) b fieldNames else getStructureFieldsAux numParams (i+1) b <| fieldNames.push <| deinternalizeFieldName n | _, _, fieldNames => fieldNames -- TODO: fix. See comment in the beginning of the file def getStructureFields (env : Environment) (structName : Name) : Array Name := let ctor := getStructureCtor env structName; getStructureFieldsAux ctor.numParams 0 ctor.type #[] private def isSubobjectFieldAux (numParams : Nat) (target : Name) : Nat → Expr → Option Name | i, Expr.forallE n d b _ => if i < numParams then isSubobjectFieldAux numParams target (i+1) b else if n == target then match d.getAppFn with | Expr.const parentStructName _ _ => some parentStructName | _ => panic! "ill-formed structure" else isSubobjectFieldAux numParams target (i+1) b | _, _ => none -- TODO: fix. See comment in the beginning of the file /-- If `fieldName` represents the relation to a parent structure `S`, return `S` -/ def isSubobjectField? (env : Environment) (structName : Name) (fieldName : Name) : Option Name := let ctor := getStructureCtor env structName; isSubobjectFieldAux ctor.numParams (mkInternalSubobjectFieldName fieldName) 0 ctor.type /-- Return immediate parent structures -/ def getParentStructures (env : Environment) (structName : Name) : Array Name := let fieldNames := getStructureFields env structName; fieldNames.foldl (init := #[]) fun acc fieldName => match isSubobjectField? env structName fieldName with | some parentStructName => acc.push parentStructName | none => acc /-- Return all parent structures -/ partial def getAllParentStructures (env : Environment) (structName : Name) : Array Name := visit structName |>.run #[] |>.2 where visit (structName : Name) : StateT (Array Name) Id Unit := do for p in getParentStructures env structName do modify fun s => s.push p visit p /-- `findField? env S fname`. If `fname` is defined in a parent `S'` of `S`, return `S'` -/ partial def findField? (env : Environment) (structName : Name) (fieldName : Name) : Option Name := if (getStructureFields env structName).contains fieldName then some structName else getParentStructures env structName |>.findSome? fun parentStructName => findField? env parentStructName fieldName private partial def getStructureFieldsFlattenedAux (env : Environment) (structName : Name) (fullNames : Array Name) (includeSubobjectFields : Bool) : Array Name := (getStructureFields env structName).foldl (init := fullNames) fun fullNames fieldName => match isSubobjectField? env structName fieldName with | some parentStructName => let fullNames := if includeSubobjectFields then fullNames.push fieldName else fullNames getStructureFieldsFlattenedAux env parentStructName fullNames includeSubobjectFields | none => fullNames.push fieldName def getStructureFieldsFlattened (env : Environment) (structName : Name) (includeSubobjectFields := true) : Array Name := getStructureFieldsFlattenedAux env structName #[] includeSubobjectFields -- TODO: fix. See comment in the beginning of the file private def hasProjFn (env : Environment) (structName : Name) (numParams : Nat) : Nat → Expr → Bool | i, Expr.forallE n d b _ => if i < numParams then hasProjFn env structName numParams (i+1) b else let fullFieldName := structName ++ deinternalizeFieldName n; env.isProjectionFn fullFieldName | _, _ => false /-- Return true if `constName` is the name of an inductive datatype created using the `structure` or `class` commands. We perform the check by testing whether auxiliary projection functions have been created. -/ def isStructure (env : Environment) (constName : Name) : Bool := if isStructureLike env constName then let ctor := getStructureCtor env constName; hasProjFn env constName ctor.numParams 0 ctor.type else false -- TODO: fix. See comment in the beginning of the file def getProjFnForField? (env : Environment) (structName : Name) (fieldName : Name) : Option Name := let fieldNames := getStructureFields env structName; if fieldNames.any fun n => fieldName == n then some (structName ++ fieldName) else none partial def getPathToBaseStructureAux (env : Environment) (baseStructName : Name) (structName : Name) (path : List Name) : Option (List Name) := if baseStructName == structName then some path.reverse else let fieldNames := getStructureFields env structName; fieldNames.findSome? fun fieldName => match isSubobjectField? env structName fieldName with | none => none | some parentStructName => match getProjFnForField? env structName fieldName with | none => none | some projFn => getPathToBaseStructureAux env baseStructName parentStructName (projFn :: path) /-- If `baseStructName` is an ancestor structure for `structName`, then return a sequence of projection functions to go from `structName` to `baseStructName`. -/ def getPathToBaseStructure? (env : Environment) (baseStructName : Name) (structName : Name) : Option (List Name) := getPathToBaseStructureAux env baseStructName structName [] end Lean
2828fb1961a5581ee3e184e00867dfc8b9390353
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/group_theory/submonoid/membership.lean
92c94d4d69e32b1f11f57197e8ba838b96ef343b
[ "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
14,423
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, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import group_theory.submonoid.operations import algebra.big_operators.basic import algebra.free_monoid /-! # Submonoids: membership criteria In this file we prove various facts about membership in a submonoid: * `list_prod_mem`, `multiset_prod_mem`, `prod_mem`: if each element of a collection belongs to a multiplicative submonoid, then so does their product; * `list_sum_mem`, `multiset_sum_mem`, `sum_mem`: if each element of a collection belongs to an additive submonoid, then so does their sum; * `pow_mem`, `nsmul_mem`: if `x ∈ S` where `S` is a multiplicative (resp., additive) submonoid and `n` is a natural number, then `x^n` (resp., `n • x`) belongs to `S`; * `mem_supr_of_directed`, `coe_supr_of_directed`, `mem_Sup_of_directed_on`, `coe_Sup_of_directed_on`: the supremum of a directed collection of submonoid is their union. * `sup_eq_range`, `mem_sup`: supremum of two submonoids `S`, `T` of a commutative monoid is the set of products; * `closure_singleton_eq`, `mem_closure_singleton`: the multiplicative (resp., additive) closure of `{x}` consists of powers (resp., natural multiples) of `x`. ## Tags submonoid, submonoids -/ open_locale big_operators variables {M : Type*} variables {A : Type*} namespace submonoid section assoc variables [monoid M] (S : submonoid M) @[simp, norm_cast, to_additive coe_nsmul] theorem coe_pow (x : S) (n : ℕ) : ↑(x ^ n) = (x ^ n : M) := S.subtype.map_pow x n @[simp, norm_cast, to_additive] theorem coe_list_prod (l : list S) : (l.prod : M) = (l.map coe).prod := S.subtype.map_list_prod l @[simp, norm_cast, to_additive] theorem coe_multiset_prod {M} [comm_monoid M] (S : submonoid M) (m : multiset S) : (m.prod : M) = (m.map coe).prod := S.subtype.map_multiset_prod m @[simp, norm_cast, to_additive] theorem coe_finset_prod {ι M} [comm_monoid M] (S : submonoid M) (f : ι → S) (s : finset ι) : ↑(∏ i in s, f i) = (∏ i in s, f i : M) := S.subtype.map_prod f s /-- Product of a list of elements in a submonoid is in the submonoid. -/ @[to_additive "Sum of a list of elements in an `add_submonoid` is in the `add_submonoid`."] lemma list_prod_mem {l : list M} (hl : ∀ x ∈ l, x ∈ S) : l.prod ∈ S := by { lift l to list S using hl, rw ← coe_list_prod, exact l.prod.coe_prop } /-- Product of a multiset of elements in a submonoid of a `comm_monoid` is in the submonoid. -/ @[to_additive "Sum of a multiset of elements in an `add_submonoid` of an `add_comm_monoid` is in the `add_submonoid`."] lemma multiset_prod_mem {M} [comm_monoid M] (S : submonoid M) (m : multiset M) (hm : ∀ a ∈ m, a ∈ S) : m.prod ∈ S := by { lift m to multiset S using hm, rw ← coe_multiset_prod, exact m.prod.coe_prop } /-- Product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is in the submonoid. -/ @[to_additive "Sum of elements in an `add_submonoid` of an `add_comm_monoid` indexed by a `finset` is in the `add_submonoid`."] lemma prod_mem {M : Type*} [comm_monoid M] (S : submonoid M) {ι : Type*} {t : finset ι} {f : ι → M} (h : ∀c ∈ t, f c ∈ S) : ∏ c in t, f c ∈ S := S.multiset_prod_mem (t.1.map f) $ λ x hx, let ⟨i, hi, hix⟩ := multiset.mem_map.1 hx in hix ▸ h i hi @[to_additive nsmul_mem] lemma pow_mem {x : M} (hx : x ∈ S) (n : ℕ) : x ^ n ∈ S := by simpa only [coe_pow] using ((⟨x, hx⟩ : S) ^ n).coe_prop end assoc section non_assoc variables [mul_one_class M] (S : submonoid M) open set @[to_additive] lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S) {x : M} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i := begin refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr S i) hi⟩, suffices : x ∈ closure (⋃ i, (S i : set M)) → ∃ i, x ∈ S i, by simpa only [closure_Union, closure_eq (S _)] using this, refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _), { exact hι.elim (λ i, ⟨i, (S i).one_mem⟩) }, { rintros x y ⟨i, hi⟩ ⟨j, hj⟩, rcases hS i j with ⟨k, hki, hkj⟩, exact ⟨k, (S k).mul_mem (hki hi) (hkj hj)⟩ } end @[to_additive] lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S) : ((⨆ i, S i : submonoid M) : set M) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] @[to_additive] lemma mem_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≤) S) {x : M} : x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk] end @[to_additive] lemma coe_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≤) S) : (↑(Sup S) : set M) = ⋃ s ∈ S, ↑s := set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS] @[to_additive] lemma mem_sup_left {S T : submonoid M} : ∀ {x : M}, x ∈ S → x ∈ S ⊔ T := show S ≤ S ⊔ T, from le_sup_left @[to_additive] lemma mem_sup_right {S T : submonoid M} : ∀ {x : M}, x ∈ T → x ∈ S ⊔ T := show T ≤ S ⊔ T, from le_sup_right @[to_additive] lemma mul_mem_sup {S T : submonoid M} {x y : M} (hx : x ∈ S) (hy : y ∈ T) : x * y ∈ S ⊔ T := (S ⊔ T).mul_mem (mem_sup_left hx) (mem_sup_right hy) @[to_additive] lemma mem_supr_of_mem {ι : Type*} {S : ι → submonoid M} (i : ι) : ∀ {x : M}, x ∈ S i → x ∈ supr S := show S i ≤ supr S, from le_supr _ _ @[to_additive] lemma mem_Sup_of_mem {S : set (submonoid M)} {s : submonoid M} (hs : s ∈ S) : ∀ {x : M}, x ∈ s → x ∈ Sup S := show s ≤ Sup S, from le_Sup hs end non_assoc end submonoid namespace free_monoid variables {α : Type*} open submonoid @[to_additive] theorem closure_range_of : closure (set.range $ @of α) = ⊤ := eq_top_iff.2 $ λ x hx, free_monoid.rec_on x (one_mem _) $ λ x xs hxs, mul_mem _ (subset_closure $ set.mem_range_self _) hxs end free_monoid namespace submonoid variables [monoid M] open monoid_hom lemma closure_singleton_eq (x : M) : closure ({x} : set M) = (powers_hom M x).mrange := closure_eq_of_le (set.singleton_subset_iff.2 ⟨multiplicative.of_add 1, pow_one x⟩) $ λ x ⟨n, hn⟩, hn ▸ pow_mem _ (subset_closure $ set.mem_singleton _) _ /-- The submonoid generated by an element of a monoid equals the set of natural number powers of the element. -/ lemma mem_closure_singleton {x y : M} : y ∈ closure ({x} : set M) ↔ ∃ n:ℕ, x^n=y := by rw [closure_singleton_eq, mem_mrange]; refl lemma mem_closure_singleton_self {y : M} : y ∈ closure ({y} : set M) := mem_closure_singleton.2 ⟨1, pow_one y⟩ lemma closure_singleton_one : closure ({1} : set M) = ⊥ := by simp [eq_bot_iff_forall, mem_closure_singleton] @[to_additive] lemma closure_eq_mrange (s : set M) : closure s = (free_monoid.lift (coe : s → M)).mrange := by rw [mrange_eq_map, ← free_monoid.closure_range_of, map_mclosure, ← set.range_comp, free_monoid.lift_comp_of, subtype.range_coe] @[to_additive] lemma exists_list_of_mem_closure {s : set M} {x : M} (hx : x ∈ closure s) : ∃ (l : list M) (hl : ∀ y ∈ l, y ∈ s), l.prod = x := begin rw [closure_eq_mrange, mem_mrange] at hx, rcases hx with ⟨l, hx⟩, exact ⟨list.map coe l, λ y hy, let ⟨z, hz, hy⟩ := list.mem_map.1 hy in hy ▸ z.2, hx⟩ end @[to_additive] lemma exists_multiset_of_mem_closure {M : Type*} [comm_monoid M] {s : set M} {x : M} (hx : x ∈ closure s) : ∃ (l : multiset M) (hl : ∀ y ∈ l, y ∈ s), l.prod = x := begin obtain ⟨l, h1, h2⟩ := exists_list_of_mem_closure hx, exact ⟨l, h1, (multiset.coe_prod l).trans h2⟩, end /-- The submonoid generated by an element. -/ def powers (n : M) : submonoid M := submonoid.copy (powers_hom M n).mrange (set.range ((^) n : ℕ → M)) $ set.ext (λ n, exists_congr $ λ i, by simp; refl) @[simp] lemma mem_powers (n : M) : n ∈ powers n := ⟨1, pow_one _⟩ lemma mem_powers_iff (x z : M) : x ∈ powers z ↔ ∃ n : ℕ, z ^ n = x := iff.rfl lemma powers_eq_closure (n : M) : powers n = closure {n} := by { ext, exact mem_closure_singleton.symm } lemma powers_subset {n : M} {P : submonoid M} (h : n ∈ P) : powers n ≤ P := λ x hx, match x, hx with _, ⟨i, rfl⟩ := P.pow_mem h i end /-- Exponentiation map from natural numbers to powers. -/ def pow (n : M) (m : ℕ) : powers n := ⟨n ^ m, m, rfl⟩ /-- Logarithms from powers to natural numbers. -/ def log [decidable_eq M] {n : M} (p : powers n) : ℕ := nat.find $ (mem_powers_iff p.val n).mp p.prop @[simp] theorem pow_log_eq_self [decidable_eq M] {n : M} (p : powers n) : pow n (log p) = p := subtype.ext $ nat.find_spec p.prop lemma pow_right_injective_iff_pow_injective {n : M} : function.injective (λ m : ℕ, n ^ m) ↔ function.injective (pow n) := subtype.coe_injective.of_comp_iff (pow n) theorem log_pow_eq_self [decidable_eq M] {n : M} (h : function.injective (λ m : ℕ, n ^ m)) (m : ℕ) : log (pow n m) = m := pow_right_injective_iff_pow_injective.mp h $ pow_log_eq_self _ theorem log_pow_int_eq_self {x : ℤ} (h : 1 < x.nat_abs) (m : ℕ) : log (pow x m) = m := log_pow_eq_self (int.pow_right_injective h) _ end submonoid namespace submonoid variables {N : Type*} [comm_monoid N] open monoid_hom @[to_additive] lemma sup_eq_range (s t : submonoid N) : s ⊔ t = (s.subtype.coprod t.subtype).mrange := by rw [mrange_eq_map, ← mrange_inl_sup_mrange_inr, map_sup, map_mrange, coprod_comp_inl, map_mrange, coprod_comp_inr, range_subtype, range_subtype] @[to_additive] lemma mem_sup {s t : submonoid N} {x : N} : x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x := by simp only [sup_eq_range, mem_mrange, coprod_apply, prod.exists, set_like.exists, coe_subtype, subtype.coe_mk] end submonoid namespace add_submonoid variables [add_monoid A] open set lemma closure_singleton_eq (x : A) : closure ({x} : set A) = (multiples_hom A x).mrange := closure_eq_of_le (set.singleton_subset_iff.2 ⟨1, one_nsmul x⟩) $ λ x ⟨n, hn⟩, hn ▸ nsmul_mem _ (subset_closure $ set.mem_singleton _) _ /-- The `add_submonoid` generated by an element of an `add_monoid` 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 := by rw [closure_singleton_eq, add_monoid_hom.mem_mrange]; refl lemma closure_singleton_zero : closure ({0} : set A) = ⊥ := by simp [eq_bot_iff_forall, mem_closure_singleton, nsmul_zero] /-- The additive submonoid generated by an element. -/ def multiples (x : A) : add_submonoid A := add_submonoid.copy (multiples_hom A x).mrange (set.range (λ i, i • x : ℕ → A)) $ set.ext (λ n, exists_congr $ λ i, by simp; refl) @[simp] lemma mem_multiples (x : A) : x ∈ multiples x := ⟨1, one_nsmul _⟩ lemma mem_multiples_iff (x z : A) : x ∈ multiples z ↔ ∃ n : ℕ, n • z = x := iff.rfl lemma multiples_eq_closure (x : A) : multiples x = closure {x} := by { ext, exact mem_closure_singleton.symm } lemma multiples_subset {x : A} {P : add_submonoid A} (h : x ∈ P) : multiples x ≤ P := λ x hx, match x, hx with _, ⟨i, rfl⟩ := P.nsmul_mem h i end attribute [to_additive add_submonoid.multiples] submonoid.powers attribute [to_additive add_submonoid.mem_multiples] submonoid.mem_powers attribute [to_additive add_submonoid.mem_multiples_iff] submonoid.mem_powers_iff attribute [to_additive add_submonoid.multiples_eq_closure] submonoid.powers_eq_closure attribute [to_additive add_submonoid.multiples_subset] submonoid.powers_subset end add_submonoid /-! Lemmas about additive closures of `submonoid`. -/ namespace submonoid variables {R : Type*} [non_assoc_semiring R] (S : submonoid R) {a b : R} /-- The product of an element of the additive closure of a multiplicative submonoid `M` and an element of `M` is contained in the additive closure of `M`. -/ lemma mul_right_mem_add_closure (ha : a ∈ add_submonoid.closure (S : set R)) (hb : b ∈ S) : a * b ∈ add_submonoid.closure (S : set R) := begin revert b, refine add_submonoid.closure_induction ha _ _ _; clear ha a, { exact λ r hr b hb, add_submonoid.mem_closure.mpr (λ y hy, hy (S.mul_mem hr hb)) }, { exact λ b hb, by simp only [zero_mul, (add_submonoid.closure (S : set R)).zero_mem] }, { simp_rw add_mul, exact λ r s hr hs b hb, (add_submonoid.closure (S : set R)).add_mem (hr hb) (hs hb) } end /-- The product of two elements of the additive closure of a submonoid `M` is an element of the additive closure of `M`. -/ lemma mul_mem_add_closure (ha : a ∈ add_submonoid.closure (S : set R)) (hb : b ∈ add_submonoid.closure (S : set R)) : a * b ∈ add_submonoid.closure (S : set R) := begin revert a, refine add_submonoid.closure_induction hb _ _ _; clear hb b, { exact λ r hr b hb, S.mul_right_mem_add_closure hb hr }, { exact λ b hb, by simp only [mul_zero, (add_submonoid.closure (S : set R)).zero_mem] }, { simp_rw mul_add, exact λ r s hr hs b hb, (add_submonoid.closure (S : set R)).add_mem (hr hb) (hs hb) } end /-- The product of an element of `S` and an element of the additive closure of a multiplicative submonoid `S` is contained in the additive closure of `S`. -/ lemma mul_left_mem_add_closure (ha : a ∈ S) (hb : b ∈ add_submonoid.closure (S : set R)) : a * b ∈ add_submonoid.closure (S : set R) := S.mul_mem_add_closure (add_submonoid.mem_closure.mpr (λ sT hT, hT ha)) hb end submonoid section mul_add lemma of_mul_image_powers_eq_multiples_of_mul [monoid M] {x : M} : additive.of_mul '' ((submonoid.powers x) : set M) = add_submonoid.multiples (additive.of_mul x) := begin ext, split, { rintros ⟨y, ⟨n, hy1⟩, hy2⟩, use n, simpa [← of_mul_pow, hy1] }, { rintros ⟨n, hn⟩, refine ⟨x ^ n, ⟨n, rfl⟩, _⟩, rwa of_mul_pow } end lemma of_add_image_multiples_eq_powers_of_add [add_monoid A] {x : A} : multiplicative.of_add '' ((add_submonoid.multiples x) : set A) = submonoid.powers (multiplicative.of_add x) := begin symmetry, rw equiv.eq_image_iff_symm_image_eq, exact of_mul_image_powers_eq_multiples_of_mul, end end mul_add
56ef587b35262f88597866bf0c77fe8a1959ebef
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/limits/shapes/default.lean
ce39844b6f1e1e4abd9f3e72d785b8c050a1e923
[ "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
585
lean
import category_theory.limits.shapes.terminal import category_theory.limits.shapes.binary_products import category_theory.limits.shapes.products import category_theory.limits.shapes.finite_products import category_theory.limits.shapes.finite_limits import category_theory.limits.shapes.biproducts import category_theory.limits.shapes.images import category_theory.limits.shapes.zero_morphisms import category_theory.limits.shapes.kernels import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.wide_pullbacks import category_theory.limits.shapes.pullbacks
ec622913f6e12aef668eaba29a372452e2836b2c
fe208a542cea7b2d6d7ff79f94d535f6d11d814a
/src/Logic/one_liner.lean
6f716908b80a47990dc263e932ffefcf99aecc72
[]
no_license
ImperialCollegeLondon/M1F_room_342_questions
c4b98b14113fe900a7f388762269305faff73e63
63de9a6ab9c27a433039dd5530bc9b10b1d227f7
refs/heads/master
1,585,807,312,561
1,545,232,972,000
1,545,232,972,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,294
lean
inductive fml | atom (i : ℕ) | imp (a b : fml) | not (a : fml) open fml infixr ` →' `:50 := imp -- right associative prefix `¬' `:51 := fml.not inductive prf : fml → Type | axk (p q) : prf (p →' q →' p) | axs (p q r) : prf $ (p →' q →' r) →' (p →' q) →' (p →' r) | axX (p q) : prf $ ((¬' q) →' (¬' p)) →' p →' q | mp {p q} : prf p → prf (p →' q) → prf q open prf theorem not_not_p_of_p (p q : fml) : prf (p →' (¬' (¬' p))) := mp (mp (mp (axk (¬' p) (¬' p)) (axk (¬' p →' ¬' p →' ¬' p) (¬' ¬' ¬' p)) ) (mp (mp (mp (mp (mp (axk (¬' ¬' ¬' p) (¬' ¬' ¬' p)) (mp (axk (¬' ¬' ¬' p) (¬' ¬' ¬' p →' ¬' ¬' ¬' p)) (axs (¬' ¬' ¬' p) (¬' ¬' ¬' p →' ¬' ¬' ¬' p) (¬' ¬' ¬' p)) ) ) (mp (mp (axk (¬' ¬' ¬' p) (¬' ¬' (¬' p →' ¬' p →' ¬' p))) (axk (¬' ¬' ¬' p →' (¬' ¬' (¬' p →' ¬' p →' ¬' p)) →' ¬' ¬' ¬' p) (¬' ¬' ¬' p)) ) (axs (¬' ¬' ¬' p) (¬' ¬' ¬' p) (¬' ¬' (¬' p →' ¬' p →' ¬' p) →' ¬' ¬' ¬' p)) ) ) (mp (mp (axX (¬' ¬' p) ¬' (¬' p →' ¬' p →' ¬' p)) (axk ((¬' ¬' (¬' p →' ¬' p →' ¬' p) →' ¬' ¬' ¬' p) →' ¬' ¬' p →' ¬' (¬' p →' ¬' p →' ¬' p)) (¬' ¬' ¬' p)) ) (axs (¬' ¬' ¬' p) (¬' ¬' (¬' p →' ¬' p →' ¬' p) →' ¬' ¬' ¬' p) (¬' ¬' p →' ¬' (¬' p →' ¬' p →' ¬' p))) ) ) (mp (mp (axX (¬' p →' ¬' p →' ¬' p) (¬' p)) (axk ((¬' (¬' p) →' ¬' (¬' p →' ¬' p →' ¬' p)) →' (¬' p →' ¬' p →' ¬' p) →' ¬' p) (¬' ¬' ¬' p)) ) (axs (¬' ¬' ¬' p) (¬' ¬' p →' ¬' (¬' p →' ¬' p →' ¬' p)) ((¬' p →' ¬' p →' ¬' p) →' ¬' p)) ) ) (axs (¬' ¬' ¬' p) (¬' p →' ¬' p →' ¬' p) (¬' p)) ) ) (axX p (¬' ¬' p))
2bed56d4a2346753e1df9c8e99777abc135ceef4
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/nested_common_subexpr_issue.lean
a34bab48366c4a08391739d098a278922beb5c19
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
332
lean
-- set_option trace.compiler true def fib_aux : ℕ → ℕ × ℕ | 0 := (0, 1) | (n+1) := let p := fib_aux n in (p.2, p.1 + p.2) def fib n := (fib_aux n).2 #eval fib 10000 def fib_aux2 : ℕ → ℕ × ℕ | 0 := (0, 1) | (n+1) := let (a, b) := fib_aux2 n in (b, a + b) def fib2 n := (fib_aux2 n).2 #eval fib2 10000
eefb9d49009a3930ae90756e2ec802a1d465316f
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/compiler/lazylist.lean
7034deed238ed90795e656d38a1918e6e6661bd7
[ "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
4,048
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ universe u v w inductive LazyList (α : Type u) where | nil : LazyList α | cons (hd : α) (tl : LazyList α) : LazyList α | delayed (t : Thunk $ LazyList α) : LazyList α @[extern c inline "#2"] def List.toLazy {α : Type u} : List α → LazyList α | [] => LazyList.nil | h::t => LazyList.cons h (toLazy t) namespace LazyList variable {α : Type u} {β : Type v} {δ : Type w} instance : Inhabited (LazyList α) := ⟨nil⟩ @[inline] def pure : α → LazyList α | a => cons a nil partial def isEmpty : LazyList α → Bool | nil => true | cons _ _ => false | delayed as => isEmpty as.get partial def toList : LazyList α → List α | nil => [] | cons a as => a :: toList as | delayed as => toList as.get partial def head [Inhabited α] : LazyList α → α | nil => arbitrary | cons a as => a | delayed as => head as.get partial def tail : LazyList α → LazyList α | nil => nil | cons a as => as | delayed as => tail as.get partial def append : LazyList α → (Unit → LazyList α) → LazyList α | nil, bs => bs () | cons a as, bs => delayed (cons a (append as bs)) | delayed as, bs => delayed (append as.get bs) instance : Append (LazyList α) where append a b := LazyList.append a (fun _ => b) partial def interleave : LazyList α → LazyList α → LazyList α | nil, bs => bs | cons a as, bs => delayed (cons a (interleave bs as)) | delayed as, bs => delayed (interleave as.get bs) partial def map (f : α → β) : LazyList α → LazyList β | nil => nil | cons a as => delayed (cons (f a) (map f as)) | delayed as => delayed (map f as.get) partial def map₂ (f : α → β → δ) : LazyList α → LazyList β → LazyList δ | nil, _ => nil | _, nil => nil | cons a as, cons b bs => delayed (cons (f a b) (map₂ f as bs)) | delayed as, bs => delayed (map₂ f as.get bs) | as, delayed bs => delayed (map₂ f as bs.get) @[inline] def zip : LazyList α → LazyList β → LazyList (α × β) := map₂ Prod.mk partial def join : LazyList (LazyList α) → LazyList α | nil => nil | cons a as => delayed (append a fun _ => join as) | delayed as => delayed (join as.get) @[inline] partial def bind (x : LazyList α) (f : α → LazyList β) : LazyList β := join (x.map f) instance isMonad : Monad LazyList where pure := LazyList.pure bind := LazyList.bind map := LazyList.map instance : Alternative LazyList where failure := nil orElse := LazyList.append partial def approx : Nat → LazyList α → List α | 0, as => [] | _, nil => [] | i+1, cons a as => a :: approx i as | i+1, delayed as => approx (i+1) as.get partial def iterate (f : α → α) : α → LazyList α | x => cons x (delayed (iterate f (f x))) partial def iterate₂ (f : α → α → α) : α → α → LazyList α | x, y => cons x (delayed (iterate₂ f y (f x y))) partial def filter (p : α → Bool) : LazyList α → LazyList α | nil => nil | cons a as => delayed (if p a then cons a (filter p as) else filter p as) | delayed as => delayed (filter p as.get) end LazyList def fib : LazyList Nat := LazyList.iterate₂ (· + ·) 0 1 def iota (i : Nat := 0) : LazyList Nat := LazyList.iterate Nat.succ i def tst : LazyList String := do let x ← [1, 2, 3].toLazy let y ← [2, 3, 4].toLazy guard (x + y > 5) pure s!"{x} + {y} = {x+y}" def main : IO Unit := do let n := 40 IO.println tst.isEmpty IO.println tst.head IO.println <| fib.interleave (iota.map (· + 100)) |>.approx n IO.println <| iota.map (· + 10) |>.filter (· % 2 == 0) |>.approx n
008af05731fca96e0563c5b07e6ac1d16072db94
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/polynomial/identities.lean
34412f8293ccb4e8a3028013f35b9ae1c3506dc0
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
3,523
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.derivative /-! # Theory of univariate polynomials The main def is `binom_expansion`. -/ noncomputable theory namespace polynomial universes u v w x y z variables {R : Type u} {S : Type v} {T : Type w} {ι : Type x} {k : Type y} {A : Type z} {a b : R} {m n : ℕ} 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_exp Maybe use data.nat.choose to prove it. -/ def pow_add_expansion {R : Type*} [comm_semiring R] (x y : R) : ∀ (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 R] private def poly_binom_aux1 (x y : R) (e : ℕ) (a : R) : {k : R // 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 R) (x y : R) : 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' with n z, apply (poly_binom_aux1 x y _ _).property end private lemma poly_binom_aux3 (f : polynomial R) (x y : R) : 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] lemma derivative_eval (p : polynomial R) (x : R) : p.derivative.eval x = p.sum (λ n a, (a * n)*x^(n-1)) := by simp only [derivative, eval_sum, eval_pow, eval_C, eval_X, eval_nat_cast, eval_mul] def binom_expansion (f : polynomial R) (x y : R) : {k : R // 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 [←eval_eq_sum], }, { rw derivative_eval, symmetry, apply finsupp.sum_mul }, { symmetry, apply finsupp.sum_mul } end def pow_sub_pow_factor (x y : R) : Π (i : ℕ), {z : R // 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 R) (x y : R) : {z : R // f.eval x - f.eval y = z * (x - y)} := begin refine ⟨f.sum (λ i r, r * (pow_sub_pow_factor x y i).val), _⟩, delta eval eval₂, rw ← finsupp.sum_sub, rw finsupp.sum_mul, delta finsupp.sum, congr' with i r, dsimp, rw [mul_assoc, ←(pow_sub_pow_factor x y _).prop, mul_sub], end end identities end polynomial
a223cd0f52dbb59e574d5f190ae36c1682d5cdc4
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/lexicographic.lean
a9f0b737899129a2cfde1aa0e18b509b9a6b7fa7
[ "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
5,039
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Minchao Wu -/ import data.equiv.basic import data.prod import tactic.basic /-! # Lexicographic order This file defines the lexicographic relation for pairs and dependent pairs of orders, partial orders and linear orders. ## Main declarations * `lex α`: A type synonym of `α` to equip it with its lexicographic order. * `prod.lex.<pre/partial_/linear_>order`: Instances lifting the orders on `α` and `β` to `α ×ₗ β`. ## Notation * `α ×ₗ β`: `α × β` equipped with the lexicographic order ## See also Related files are: * `data.finset.colex`: Colexicographic order on finite sets. * `data.list.lex`: Lexicographic order on lists. * `data.psigma.order`: Lexicographic order on `Σ' i, α i`. * `data.sigma.order`: Lexicographic order on `Σ i, α i`. -/ universes u v /-- A type synonym to equip a type with its lexicographic order. -/ def lex (α : Type u) := α variables {α : Type u} {β : Type v} {γ : Type*} /-- `to_lex` is the identity function to the `lex` of a type. -/ @[pattern] def to_lex : α ≃ lex α := ⟨id, id, λ h, rfl, λ h, rfl⟩ /-- `of_lex` is the identity function from the `lex` of a type. -/ @[pattern] def of_lex : lex α ≃ α := to_lex.symm @[simp] lemma to_lex_symm_eq : (@to_lex α).symm = of_lex := rfl @[simp] lemma of_lex_symm_eq : (@of_lex α).symm = to_lex := rfl @[simp] lemma to_lex_of_lex (a : lex α) : to_lex (of_lex a) = a := rfl @[simp] lemma of_lex_to_lex (a : α) : of_lex (to_lex a) = a := rfl @[simp] lemma to_lex_inj {a b : α} : to_lex a = to_lex b ↔ a = b := iff.rfl @[simp] lemma of_lex_inj {a b : lex α} : of_lex a = of_lex b ↔ a = b := iff.rfl /-- A recursor for `lex`. Use as `induction x using lex.rec`. -/ protected def lex.rec {β : lex α → Sort*} (h : Π a, β (to_lex a)) : Π a, β a := λ a, h (of_lex a) namespace prod.lex notation α ` ×ₗ `:35 β:34 := lex (prod α β) meta instance [has_to_format α] [has_to_format β] : has_to_format (α ×ₗ β) := prod.has_to_format instance decidable_eq (α β : Type*) [decidable_eq α] [decidable_eq β] : decidable_eq (α ×ₗ β) := prod.decidable_eq instance inhabited (α β : Type*) [inhabited α] [inhabited β] : inhabited (α ×ₗ β) := prod.inhabited /-- Dictionary / lexicographic ordering on pairs. -/ instance has_le (α β : Type*) [has_lt α] [has_le β] : has_le (α ×ₗ β) := { le := prod.lex (<) (≤) } instance has_lt (α β : Type*) [has_lt α] [has_lt β] : has_lt (α ×ₗ β) := { lt := prod.lex (<) (<) } lemma le_iff [has_lt α] [has_le β] (a b : α × β) : to_lex a ≤ to_lex b ↔ a.1 < b.1 ∨ a.1 = b.1 ∧ a.2 ≤ b.2 := prod.lex_def (<) (≤) lemma lt_iff [has_lt α] [has_lt β] (a b : α × β) : to_lex a < to_lex b ↔ a.1 < b.1 ∨ a.1 = b.1 ∧ a.2 < b.2 := prod.lex_def (<) (<) /-- Dictionary / lexicographic preorder for pairs. -/ instance preorder (α β : Type*) [preorder α] [preorder β] : preorder (α ×ₗ β) := { le_refl := by { haveI : is_refl β (≤) := ⟨le_refl⟩, exact refl_of (prod.lex _ _), }, le_trans := λ _ _ _, by { haveI : is_trans α (<) := ⟨λ _ _ _, lt_trans⟩, haveI : is_trans β (≤) := ⟨λ _ _ _, le_trans⟩, exact trans_of (prod.lex _ _) }, lt_iff_le_not_le := λ x₁ x₂, match x₁, x₂ with | to_lex (a₁, b₁), to_lex (a₂, b₂) := begin split, { rintros (⟨_, _, _, _, hlt⟩ | ⟨_, _, _, hlt⟩), { split, { left, assumption }, { rintro ⟨l,r⟩, { apply lt_asymm hlt, assumption }, { apply lt_irrefl _ hlt } } }, { split, { right, rw lt_iff_le_not_le at hlt, exact hlt.1 }, { rintro ⟨l,r⟩, { apply lt_irrefl a₁, assumption }, { rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } }, { rintros ⟨⟨h₁ll, h₁lr⟩, h₂r⟩, { left, assumption }, { right, rw lt_iff_le_not_le, split, { assumption }, { intro h, apply h₂r, right, exact h } } } end end, .. prod.lex.has_le α β, .. prod.lex.has_lt α β } /-- Dictionary / lexicographic partial_order for pairs. -/ instance partial_order (α β : Type*) [partial_order α] [partial_order β] : partial_order (α ×ₗ β) := { le_antisymm := by { haveI : is_strict_order α (<) := { irrefl := lt_irrefl, trans := λ _ _ _, lt_trans }, haveI : is_antisymm β (≤) := ⟨λ _ _, le_antisymm⟩, exact @antisymm _ (prod.lex _ _) _, }, .. prod.lex.preorder α β } /-- Dictionary / lexicographic linear_order for pairs. -/ instance linear_order (α β : Type*) [linear_order α] [linear_order β] : linear_order (α ×ₗ β) := { le_total := total_of (prod.lex _ _), decidable_le := prod.lex.decidable _ _, decidable_lt := prod.lex.decidable _ _, decidable_eq := lex.decidable_eq _ _, .. prod.lex.partial_order α β } end prod.lex
554a789a5901be18f2ccd80bb3d7fd9337031b92
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/set_theory/ordinal/natural_ops.lean
c57041816c21de29e481f456baf53b97d3ded219
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
12,060
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 set_theory.ordinal.arithmetic /-! # Natural operations on ordinals The goal of this file is to define natural addition and multiplication on ordinals, also known as the Hessenberg sum and product, and provide a basic API. The natural addition of two ordinals `a ♯ b` is recursively defined as the least ordinal greater than `a' ♯ b` and `a ♯ b'` for `a' < a` and `b' < b`. The natural multiplication `a ⨳ b` is likewise recursively defined as the least ordinal such that `a ⨳ b ♯ a' ⨳ b'` is greater than `a' ⨳ b ♯ a ⨳ b'` for any `a' < a` and `b' < b`. These operations form a rich algebraic structure: they're commutative, associative, preserve order, have the usual `0` and `1` from ordinals, and distribute over one another. Moreover, these operations are the addition and multiplication of ordinals when viewed as combinatorial `game`s. This makes them particularly useful for game theory. Finally, both operations admit simple, intuitive descriptions in terms of the Cantor normal form. The natural addition of two ordinals corresponds to adding their Cantor normal forms as if they were polynomials in `ω`. Likewise, their natural multiplication corresponds to multiplying the Cantor normal forms as polynomials. # Implementation notes Given the rich algebraic structure of these two operations, we choose to create a type synonym `nat_ordinal`, where we provide the appropriate instances. However, to avoid casting back and forth between both types, we attempt to prove and state most results on `ordinal`. # Todo - Define natural multiplication and provide a basic API. - Prove the characterizations of natural addition and multiplication in terms of the Cantor normal form. -/ universes u v open function order noncomputable theory /-- A type synonym for ordinals with natural addition and multiplication. -/ @[derive [has_zero, inhabited, has_one, linear_order, succ_order, has_well_founded]] def nat_ordinal : Type* := ordinal /-- The identity function between `ordinal` and `nat_ordinal`. -/ @[pattern] def ordinal.to_nat_ordinal : ordinal ≃o nat_ordinal := order_iso.refl _ /-- The identity function between `nat_ordinal` and `ordinal`. -/ @[pattern] def nat_ordinal.to_ordinal : nat_ordinal ≃o ordinal := order_iso.refl _ open ordinal namespace nat_ordinal variables {a b c : nat_ordinal.{u}} @[simp] theorem to_ordinal_symm_eq : nat_ordinal.to_ordinal.symm = ordinal.to_nat_ordinal := rfl @[simp] theorem to_ordinal_to_nat_ordinal (a : nat_ordinal) : a.to_ordinal.to_nat_ordinal = a := rfl theorem lt_wf : @well_founded nat_ordinal (<) := ordinal.lt_wf instance : well_founded_lt nat_ordinal := ordinal.well_founded_lt instance : is_well_order nat_ordinal (<) := ordinal.has_lt.lt.is_well_order @[simp] theorem to_ordinal_zero : to_ordinal 0 = 0 := rfl @[simp] theorem to_ordinal_one : to_ordinal 1 = 1 := rfl @[simp] theorem to_ordinal_eq_zero (a) : to_ordinal a = 0 ↔ a = 0 := iff.rfl @[simp] theorem to_ordinal_eq_one (a) : to_ordinal a = 1 ↔ a = 1 := iff.rfl @[simp] theorem to_ordinal_max : (max a b).to_ordinal = max a.to_ordinal b.to_ordinal := rfl @[simp] theorem to_ordinal_min : (min a b).to_ordinal = min a.to_ordinal b.to_ordinal := rfl theorem succ_def (a : nat_ordinal) : succ a = (a.to_ordinal + 1).to_nat_ordinal := rfl /-- A recursor for `nat_ordinal`. Use as `induction x using nat_ordinal.rec`. -/ protected def rec {β : nat_ordinal → Sort*} (h : Π a, β (to_nat_ordinal a)) : Π a, β a := λ a, h a.to_ordinal /-- `ordinal.induction` but for `nat_ordinal`. -/ theorem induction {p : nat_ordinal → Prop} : ∀ i (h : ∀ j, (∀ k, k < j → p k) → p j), p i := ordinal.induction end nat_ordinal namespace ordinal variables {a b c : ordinal.{u}} @[simp] theorem to_nat_ordinal_symm_eq : to_nat_ordinal.symm = nat_ordinal.to_ordinal := rfl @[simp] theorem to_nat_ordinal_to_ordinal (a : ordinal) : a.to_nat_ordinal.to_ordinal = a := rfl @[simp] theorem to_nat_ordinal_zero : to_nat_ordinal 0 = 0 := rfl @[simp] theorem to_nat_ordinal_one : to_nat_ordinal 1 = 1 := rfl @[simp] theorem to_nat_ordinal_eq_zero (a) : to_nat_ordinal a = 0 ↔ a = 0 := iff.rfl @[simp] theorem to_nat_ordinal_eq_one (a) : to_nat_ordinal a = 1 ↔ a = 1 := iff.rfl @[simp] theorem to_nat_ordinal_max : to_nat_ordinal (max a b) = max a.to_nat_ordinal b.to_nat_ordinal := rfl @[simp] theorem to_nat_ordinal_min : (linear_order.min a b).to_nat_ordinal = linear_order.min a.to_nat_ordinal b.to_nat_ordinal := rfl /-- Natural addition on ordinals `a ♯ b`, also known as the Hessenberg sum, is recursively defined as the least ordinal greater than `a' ♯ b` and `a ♯ b'` for all `a' < a` and `b' < b`. In contrast to normal ordinal addition, it is commutative. Natural addition can equivalently be characterized as the ordinal resulting from adding up corresponding coefficients in the Cantor normal forms of `a` and `b`. -/ noncomputable def nadd : ordinal → ordinal → ordinal | a b := max (blsub.{u u} a $ λ a' h, nadd a' b) (blsub.{u u} b $ λ b' h, nadd a b') using_well_founded { dec_tac := `[solve_by_elim [psigma.lex.left, psigma.lex.right]] } localized "infix ` ♯ `:65 := ordinal.nadd" in natural_ops theorem nadd_def (a b : ordinal) : a ♯ b = max (blsub.{u u} a $ λ a' h, a' ♯ b) (blsub.{u u} b $ λ b' h, a ♯ b') := by rw nadd theorem lt_nadd_iff : a < b ♯ c ↔ (∃ b' < b, a ≤ b' ♯ c) ∨ ∃ c' < c, a ≤ b ♯ c' := by { rw nadd_def, simp [lt_blsub_iff] } theorem nadd_le_iff : b ♯ c ≤ a ↔ (∀ b' < b, b' ♯ c < a) ∧ ∀ c' < c, b ♯ c' < a := by { rw nadd_def, simp [blsub_le_iff] } theorem nadd_lt_nadd_left (h : b < c) (a) : a ♯ b < a ♯ c := lt_nadd_iff.2 (or.inr ⟨b, h, le_rfl⟩) theorem nadd_lt_nadd_right (h : b < c) (a) : b ♯ a < c ♯ a := lt_nadd_iff.2 (or.inl ⟨b, h, le_rfl⟩) theorem nadd_le_nadd_left (h : b ≤ c) (a) : a ♯ b ≤ a ♯ c := begin rcases lt_or_eq_of_le h with h | rfl, { exact (nadd_lt_nadd_left h a).le }, { exact le_rfl } end theorem nadd_le_nadd_right (h : b ≤ c) (a) : b ♯ a ≤ c ♯ a := begin rcases lt_or_eq_of_le h with h | rfl, { exact (nadd_lt_nadd_right h a).le }, { exact le_rfl } end variables (a b) theorem nadd_comm : ∀ a b, a ♯ b = b ♯ a | a b := begin rw [nadd_def, nadd_def, max_comm], congr; ext c hc; apply nadd_comm end using_well_founded { dec_tac := `[solve_by_elim [psigma.lex.left, psigma.lex.right]] } theorem blsub_nadd_of_mono {f : Π c < a ♯ b, ordinal.{max u v}} (hf : ∀ {i j} hi hj, i ≤ j → f i hi ≤ f j hj) : blsub _ f = max (blsub.{u v} a (λ a' ha', f (a' ♯ b) $ nadd_lt_nadd_right ha' b)) (blsub.{u v} b (λ b' hb', f (a ♯ b') $ nadd_lt_nadd_left hb' a)) := begin apply (blsub_le_iff.2 (λ i h, _)).antisymm (max_le _ _), { rcases lt_nadd_iff.1 h with ⟨a', ha', hi⟩ | ⟨b', hb', hi⟩, { exact lt_max_of_lt_left ((hf h (nadd_lt_nadd_right ha' b) hi).trans_lt (lt_blsub _ _ _)) }, { exact lt_max_of_lt_right ((hf h (nadd_lt_nadd_left hb' a) hi).trans_lt (lt_blsub _ _ _)) } }, all_goals { apply blsub_le_of_brange_subset.{u u v}, rintro c ⟨d, hd, rfl⟩, apply mem_brange_self } end theorem nadd_assoc : ∀ a b c, a ♯ b ♯ c = a ♯ (b ♯ c) | a b c := begin rw [nadd_def a (b ♯ c), nadd_def, blsub_nadd_of_mono, blsub_nadd_of_mono, max_assoc], { congr; ext d hd; apply nadd_assoc }, { exact λ i j _ _ h, nadd_le_nadd_left h a }, { exact λ i j _ _ h, nadd_le_nadd_right h c } end using_well_founded { dec_tac := `[solve_by_elim [psigma.lex.left, psigma.lex.right]] } @[simp] theorem nadd_zero : a ♯ 0 = a := begin induction a using ordinal.induction with a IH, rw [nadd_def, blsub_zero, max_zero_right], convert blsub_id a, ext b hb, exact IH _ hb end @[simp] theorem zero_nadd : 0 ♯ a = a := by rw [nadd_comm, nadd_zero] @[simp] theorem nadd_one : a ♯ 1 = succ a := begin induction a using ordinal.induction with a IH, rw [nadd_def, blsub_one, nadd_zero, max_eq_right_iff, blsub_le_iff], intros i hi, rwa [IH i hi, succ_lt_succ_iff] end @[simp] theorem one_nadd : 1 ♯ a = succ a := by rw [nadd_comm, nadd_one] theorem nadd_succ : a ♯ succ b = succ (a ♯ b) := by rw [←nadd_one (a ♯ b), nadd_assoc, nadd_one] theorem succ_nadd : succ a ♯ b = succ (a ♯ b) := by rw [←one_nadd (a ♯ b), ←nadd_assoc, one_nadd] @[simp] theorem nadd_nat (n : ℕ) : a ♯ n = a + n := begin induction n with n hn, { simp }, { rw [nat.cast_succ, add_one_eq_succ, nadd_succ, add_succ, hn] } end @[simp] theorem nat_nadd (n : ℕ) : ↑n ♯ a = a + n := by rw [nadd_comm, nadd_nat] theorem add_le_nadd : a + b ≤ a ♯ b := begin apply b.limit_rec_on, { simp }, { intros c h, rwa [add_succ, nadd_succ, succ_le_succ_iff] }, { intros c hc H, rw [←is_normal.blsub_eq.{u u} (add_is_normal a) hc, blsub_le_iff], exact λ i hi, (H i hi).trans_lt (nadd_lt_nadd_left hi a) } end end ordinal open ordinal namespace nat_ordinal instance : has_add nat_ordinal := ⟨nadd⟩ instance add_covariant_class_lt : covariant_class nat_ordinal.{u} nat_ordinal.{u} (+) (<) := ⟨λ a b c h, nadd_lt_nadd_left h a⟩ instance add_covariant_class_le : covariant_class nat_ordinal.{u} nat_ordinal.{u} (+) (≤) := ⟨λ a b c h, nadd_le_nadd_left h a⟩ instance add_contravariant_class_le : contravariant_class nat_ordinal.{u} nat_ordinal.{u} (+) (≤) := ⟨λ a b c h, by { by_contra' h', exact h.not_lt (add_lt_add_left h' a) }⟩ instance : ordered_cancel_add_comm_monoid nat_ordinal := { add := (+), add_assoc := nadd_assoc, add_left_cancel := λ a b c, add_left_cancel'', add_le_add_left := λ a b, add_le_add_left, le_of_add_le_add_left := λ a b c, le_of_add_le_add_left, zero := 0, zero_add := zero_nadd, add_zero := nadd_zero, add_comm := nadd_comm, ..nat_ordinal.linear_order } instance : add_monoid_with_one nat_ordinal := add_monoid_with_one.unary @[simp] theorem add_one_eq_succ : ∀ a : nat_ordinal, a + 1 = succ a := nadd_one @[simp] theorem to_ordinal_cast_nat (n : ℕ) : to_ordinal n = n := begin induction n with n hn, { refl }, { change nadd (to_ordinal n) 1 = n + 1, rw hn, apply nadd_one } end end nat_ordinal open nat_ordinal open_locale natural_ops namespace ordinal @[simp] theorem to_nat_ordinal_cast_nat (n : ℕ) : to_nat_ordinal n = n := by { rw ←to_ordinal_cast_nat n, refl } theorem lt_of_nadd_lt_nadd_left : ∀ {a b c}, a ♯ b < a ♯ c → b < c := @lt_of_add_lt_add_left nat_ordinal _ _ _ theorem lt_of_nadd_lt_nadd_right : ∀ {a b c}, b ♯ a < c ♯ a → b < c := @_root_.lt_of_add_lt_add_right nat_ordinal _ _ _ theorem le_of_nadd_le_nadd_left : ∀ {a b c}, a ♯ b ≤ a ♯ c → b ≤ c := @le_of_add_le_add_left nat_ordinal _ _ _ theorem le_of_nadd_le_nadd_right : ∀ {a b c}, b ♯ a ≤ c ♯ a → b ≤ c := @le_of_add_le_add_right nat_ordinal _ _ _ theorem nadd_lt_nadd_iff_left : ∀ a {b c}, a ♯ b < a ♯ c ↔ b < c := @add_lt_add_iff_left nat_ordinal _ _ _ _ theorem nadd_lt_nadd_iff_right : ∀ a {b c}, b ♯ a < c ♯ a ↔ b < c := @add_lt_add_iff_right nat_ordinal _ _ _ _ theorem nadd_le_nadd_iff_left : ∀ a {b c}, a ♯ b ≤ a ♯ c ↔ b ≤ c := @add_le_add_iff_left nat_ordinal _ _ _ _ theorem nadd_le_nadd_iff_right : ∀ a {b c}, b ♯ a ≤ c ♯ a ↔ b ≤ c := @_root_.add_le_add_iff_right nat_ordinal _ _ _ _ theorem nadd_left_cancel : ∀ {a b c}, a ♯ b = a ♯ c → b = c := @_root_.add_left_cancel nat_ordinal _ theorem nadd_right_cancel : ∀ {a b c}, a ♯ b = c ♯ b → a = c := @_root_.add_right_cancel nat_ordinal _ theorem nadd_left_cancel_iff : ∀ {a b c}, a ♯ b = a ♯ c ↔ b = c := @add_left_cancel_iff nat_ordinal _ theorem nadd_right_cancel_iff : ∀ {a b c}, b ♯ a = c ♯ a ↔ b = c := @add_right_cancel_iff nat_ordinal _ end ordinal
7f37cb06659c04af3698b55241937c8ad66f8ee4
efa51dd2edbbbbd6c34bd0ce436415eb405832e7
/20161026_ICTAC_Tutorial/ex17.lean
0e1c25f0aa3ee5d557808556df5bda80781cf7d1
[ "Apache-2.0" ]
permissive
leanprover/presentations
dd031a05bcb12c8855676c77e52ed84246bd889a
3ce2d132d299409f1de269fa8e95afa1333d644e
refs/heads/master
1,688,703,388,796
1,686,838,383,000
1,687,465,742,000
29,750,158
12
9
Apache-2.0
1,540,211,670,000
1,422,042,683,000
Lean
UTF-8
Lean
false
false
145
lean
check let y := 2 + 2 in y * y -- ℕ eval let y := 2 + 2 in y * y -- 16 def t (x : ℕ) : ℕ := let y := x + x in y * y eval t 2 -- 16
c85f08b14d86d80b4c4d131d0aa17b797c22e260
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/num4.lean
f5fd4d67065fba51fdefd01286f3539413c54daa
[ "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
278
lean
import data.num set_option pp.notation false set_option pp.implicit true namespace foo constant N : Type.{1} constant z : N constant o : N constant a : N notation 0 := z notation 1 := o check a = 0 end foo check 2 = 1 check #foo foo.a = 1 open foo check a = 1
1bec69063227540e3c5d17db839e720d6cf7e0b0
9a2260f3b7b64ae9c50bf326fdb0387f5ac64683
/src/NITRO/JavaScript.lean
a84766d9eaf8e698bd022d67afa1e68ab9cbd47d
[ "LicenseRef-scancode-mit-taylor-variant", "LicenseRef-scancode-warranty-disclaimer" ]
permissive
o89/nitro
e8e1c7f3e7baeb7a659f899c605d37b1d3e553a6
c62a67398a89d6e6a2e9da62350adaf5f17cfda9
refs/heads/master
1,670,068,174,264
1,669,086,407,000
1,669,086,407,000
204,147,098
3
5
null
null
null
null
UTF-8
Lean
false
false
2,273
lean
import N2O.Network.Default import NITRO.Elements def action (x : String) : Result := let term := λ b => Term.tuple [ Term.atom "io", Term.binary b, Term.binary ByteArray.empty ]; match Put.run (Put.unicode x) with | Sum.ok bytes => match writeTerm (term bytes) with | Sum.ok v => Result.reply (Msg.binary v) | Sum.fail s => Result.error s | Sum.fail s => Result.error s variable (α : Type) [BERT α] def update (target : String) (elem : Elem α) : Result := let (html, js) := render elem; action $ "qi('" ++ target ++ "').outerHTML='" ++ html ++ "';" ++ js def updateText (target text : String) : Result := action $ "qi('" ++ target ++ "').innerText='" ++ text ++ "';" def insertTagTop (tag target : String) (elem : Elem α) : Result := let (html, js) := render elem; action $ "qi('" ++ target ++ "').insertBefore(" ++ "(function(){ var div = qn('" ++ tag ++ "'); div.innerHTML = '" ++ html ++ "'; return div.firstChild; })()," ++ "qi('" ++ target ++ "').firstChild);" ++ js def insertTagBottom (tag target : String) (elem : Elem α) : Result := let (html, js) := render elem; action $ "(function(){ var div = qn('" ++ tag ++ "'); div.innerHTML = '" ++ html ++ "';qi('" ++ target ++ "').appendChild(div.firstChild); })();" ++ js def insertTop := insertTagTop α "div" def insertBottom := insertTagBottom α "div" def insertAdjacent (position target : String) (elem : Elem α) := let (html, js) := render elem; action $ "qi('" ++ target ++ "').insertAdjacentHTML('" ++ position ++ "', '" ++ html ++ "');" ++ js def insertBefore := insertAdjacent α "beforebegin" def insertAfter := insertAdjacent α "afterend" def clear (target : String) := action $ "(function(){var x = qi('" ++ target ++ "'); while (x.firstChild) x.removeChild(x.firstChild);})();" def remove (target : String) := action $ "(function(){var x=qi('" ++ target ++ "'); x && x.parentNode.removeChild(x);})();" def redirect (url : String) := action $ "(function(){document.location = '" ++ url ++ "';})();" def display (elem status : String) := action $ "(function(){var x = qi('" ++ elem ++ "'); if (x) x.style.display = '" ++ status ++ "';})();" def show' (elem : String) := display elem "block" def hide' (elem : String) := display elem "none"
8c1ebb39b9b3611286fed9c546ac7b007b712efe
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/congr_tac2.lean
8036aaece99a68c5237b33174ac4930e59781cc7
[ "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
1,305
lean
import data.finset open finset list example (A : Type) (f : nat → nat → nat → nat) (a b : nat) : a = b → f a = f b := begin intros, congruence, assumption end structure finite_set [class] {T : Type} (xs : set T) := (to_finset : finset T) (is_equiv : to_set to_finset = xs) definition finset_set.is_subsingleton [instance] (T : Type) (xs : set T) : subsingleton (finite_set xs) := begin constructor, intro a b, induction a with f₁ h₁, induction b with f₂ h₂, subst xs, let e := to_set.inj h₂, subst e end open finite_set definition card {T : Type} (xs : set T) [fxs : finite_set xs] := finset.card (to_finset xs) example (A : Type) (s₁ s₂ : set A) [fxs₁ : finite_set s₁] [fxs₂ : finite_set s₂] : s₁ = s₂ → card s₁ = card s₂ := begin intros, congruence, assumption end example {A : Type} (l₁ l₂ : list A) (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) : l₁ = l₂ → last l₁ h₁ = last l₂ h₂ := begin intros, congruence, assumption end example (A : Type) (last₁ last₂ : Π l : list A, l ≠ [] → A) (l₁ l₂ : list A) (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) : last₁ = last₂ → l₁ = l₂ → last₁ l₁ h₁ = last₂ l₂ h₂ := begin intro e₁ e₂, congruence, repeat assumption end
7008e910bab33892d08f98a0ab7f382e6e9bf0fc
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Util/ReplaceLevel.lean
9c1beb5401bf16b6186c7edcfc5c6ffa8f2936c6
[ "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
3,420
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Expr namespace Lean namespace Level partial def replace (f? : Level → Option Level) (u : Level) : Level := match f? u with | some v => v | none => match u with | max v₁ v₂ _ => mkLevelMax' (replace f? v₁) (replace f? v₂) | imax v₁ v₂ _ => mkLevelIMax' (replace f? v₁) (replace f? v₂) | succ v _ => mkLevelSucc (replace f? v) | _ => u end Level namespace Expr namespace ReplaceLevelImpl abbrev cacheSize : USize := 8192 structure State where keys : Array Expr -- Remark: our "unsafe" implementation relies on the fact that `()` is not a valid Expr results : Array Expr abbrev ReplaceM := StateM State @[inline] unsafe def cache (i : USize) (key : Expr) (result : Expr) : ReplaceM Expr := do modify fun s => { keys := s.keys.uset i key lcProof, results := s.results.uset i result lcProof }; pure result @[specialize] unsafe def replaceUnsafeM (f? : Level → Option Level) (size : USize) (e : Expr) : ReplaceM Expr := do let rec visit (e : Expr) := do let c ← get let h := ptrAddrUnsafe e let i := h % size if ptrAddrUnsafe (c.keys.uget i lcProof) == h then pure <| c.results.uget i lcProof else match e with | Expr.forallE _ d b _ => cache i e <| e.updateForallE! (← visit d) (← visit b) | Expr.lam _ d b _ => cache i e <| e.updateLambdaE! (← visit d) (← visit b) | Expr.mdata _ b _ => cache i e <| e.updateMData! (← visit b) | Expr.letE _ t v b _ => cache i e <| e.updateLet! (← visit t) (← visit v) (← visit b) | Expr.app f a _ => cache i e <| e.updateApp! (← visit f) (← visit a) | Expr.proj _ _ b _ => cache i e <| e.updateProj! (← visit b) | Expr.sort u _ => cache i e <| e.updateSort! (u.replace f?) | Expr.const n us _ => cache i e <| e.updateConst! (us.map (Level.replace f?)) | e => pure e visit e unsafe def initCache : State := { keys := mkArray cacheSize.toNat (cast lcProof ()), -- `()` is not a valid `Expr` results := mkArray cacheSize.toNat arbitrary } @[inline] unsafe def replaceUnsafe (f? : Level → Option Level) (e : Expr) : Expr := (replaceUnsafeM f? cacheSize e).run' initCache end ReplaceLevelImpl @[implementedBy ReplaceLevelImpl.replaceUnsafe] partial def replaceLevel (f? : Level → Option Level) : Expr → Expr | e@(Expr.forallE _ d b _) => let d := replaceLevel f? d; let b := replaceLevel f? b; e.updateForallE! d b | e@(Expr.lam _ d b _) => let d := replaceLevel f? d; let b := replaceLevel f? b; e.updateLambdaE! d b | e@(Expr.mdata _ b _) => let b := replaceLevel f? b; e.updateMData! b | e@(Expr.letE _ t v b _) => let t := replaceLevel f? t; let v := replaceLevel f? v; let b := replaceLevel f? b; e.updateLet! t v b | e@(Expr.app f a _) => let f := replaceLevel f? f; let a := replaceLevel f? a; e.updateApp! f a | e@(Expr.proj _ _ b _) => let b := replaceLevel f? b; e.updateProj! b | e@(Expr.sort u _) => e.updateSort! (u.replace f?) | e@(Expr.const n us _) => e.updateConst! (us.map (Level.replace f?)) | e => e end Expr end Lean
a8c49e8ac5aafdee153c6edf3a35473c8cfacf4a
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/specific_limits.lean
3d92eb40ae189be7d7554afae5d33e4b61fbbbf5
[ "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
47,627
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.geom_sum import analysis.asymptotics.asymptotics import order.filter.archimedean import order.iterate import topology.instances.ennreal /-! # A collection of specific limit computations -/ noncomputable theory open classical set function filter finset metric asymptotics open_locale classical topological_space nat big_operators uniformity nnreal ennreal variables {α : Type*} {β : Type*} {ι : Type*} lemma tendsto_norm_at_top_at_top : tendsto (norm : ℝ → ℝ) at_top at_top := tendsto_abs_at_top_at_top lemma summable_of_absolute_convergence_real {f : ℕ → ℝ} : (∃r, tendsto (λn, (∑ i in range n, |f i|)) at_top (𝓝 r)) → summable f | ⟨r, hr⟩ := begin refine summable_of_summable_norm ⟨r, (has_sum_iff_tendsto_nat_of_nonneg _ _).2 _⟩, exact assume i, norm_nonneg _, simpa only using hr end lemma tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ)⁻¹) at_top (𝓝 0) := tendsto_inv_at_top_zero.comp tendsto_coe_nat_at_top_at_top lemma tendsto_const_div_at_top_nhds_0_nat (C : ℝ) : tendsto (λ n : ℕ, C / n) at_top (𝓝 0) := by simpa only [mul_zero] using tendsto_const_nhds.mul tendsto_inverse_at_top_nhds_0_nat lemma nnreal.tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ≥0)⁻¹) at_top (𝓝 0) := by { rw ← nnreal.tendsto_coe, convert tendsto_inverse_at_top_nhds_0_nat, simp } lemma nnreal.tendsto_const_div_at_top_nhds_0_nat (C : ℝ≥0) : tendsto (λ n : ℕ, C / n) at_top (𝓝 0) := by simpa using tendsto_const_nhds.mul nnreal.tendsto_inverse_at_top_nhds_0_nat lemma tendsto_one_div_add_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1 / ((n : ℝ) + 1)) at_top (𝓝 0) := suffices tendsto (λ n : ℕ, 1 / (↑(n + 1) : ℝ)) at_top (𝓝 0), by simpa, (tendsto_add_at_top_iff_nat 1).2 (tendsto_const_div_at_top_nhds_0_nat 1) /-! ### Powers -/ lemma tendsto_add_one_pow_at_top_at_top_of_pos [linear_ordered_semiring α] [archimedean α] {r : α} (h : 0 < r) : tendsto (λ n:ℕ, (r + 1)^n) at_top at_top := tendsto_at_top_at_top_of_monotone' (λ n m, pow_le_pow (le_add_of_nonneg_left (le_of_lt h))) $ not_bdd_above_iff.2 $ λ x, set.exists_range_iff.2 $ add_one_pow_unbounded_of_pos _ h lemma tendsto_pow_at_top_at_top_of_one_lt [linear_ordered_ring α] [archimedean α] {r : α} (h : 1 < r) : tendsto (λn:ℕ, r ^ n) at_top at_top := sub_add_cancel r 1 ▸ tendsto_add_one_pow_at_top_at_top_of_pos (sub_pos.2 h) lemma nat.tendsto_pow_at_top_at_top_of_one_lt {m : ℕ} (h : 1 < m) : tendsto (λn:ℕ, m ^ n) at_top at_top := tsub_add_cancel_of_le (le_of_lt h) ▸ tendsto_add_one_pow_at_top_at_top_of_pos (tsub_pos_of_lt h) lemma tendsto_norm_zero' {𝕜 : Type*} [normed_group 𝕜] : tendsto (norm : 𝕜 → ℝ) (𝓝[{0}ᶜ] 0) (𝓝[set.Ioi 0] 0) := tendsto_norm_zero.inf $ tendsto_principal_principal.2 $ λ x hx, norm_pos_iff.2 hx namespace normed_field lemma tendsto_norm_inverse_nhds_within_0_at_top {𝕜 : Type*} [normed_field 𝕜] : tendsto (λ x:𝕜, ∥x⁻¹∥) (𝓝[{0}ᶜ] 0) at_top := (tendsto_inv_zero_at_top.comp tendsto_norm_zero').congr $ λ x, (normed_field.norm_inv x).symm lemma tendsto_norm_zpow_nhds_within_0_at_top {𝕜 : Type*} [normed_field 𝕜] {m : ℤ} (hm : m < 0) : tendsto (λ x : 𝕜, ∥x ^ m∥) (𝓝[{0}ᶜ] 0) at_top := begin rcases neg_surjective m with ⟨m, rfl⟩, rw neg_lt_zero at hm, lift m to ℕ using hm.le, rw int.coe_nat_pos at hm, simp only [normed_field.norm_pow, zpow_neg₀, zpow_coe_nat, ← inv_pow₀], exact (tendsto_pow_at_top hm).comp normed_field.tendsto_norm_inverse_nhds_within_0_at_top end @[simp] lemma continuous_at_zpow {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {m : ℤ} {x : 𝕜} : continuous_at (λ x, x ^ m) x ↔ x ≠ 0 ∨ 0 ≤ m := begin refine ⟨_, continuous_at_zpow _ _⟩, contrapose!, rintro ⟨rfl, hm⟩ hc, exact not_tendsto_at_top_of_tendsto_nhds (hc.tendsto.mono_left nhds_within_le_nhds).norm (tendsto_norm_zpow_nhds_within_0_at_top hm) end @[simp] lemma continuous_at_inv {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {x : 𝕜} : continuous_at has_inv.inv x ↔ x ≠ 0 := by simpa [(@zero_lt_one ℤ _ _).not_le] using @continuous_at_zpow _ _ (-1) x end normed_field lemma tendsto_pow_at_top_nhds_0_of_lt_1 {𝕜 : Type*} [linear_ordered_field 𝕜] [archimedean 𝕜] [topological_space 𝕜] [order_topology 𝕜] {r : 𝕜} (h₁ : 0 ≤ r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (𝓝 0) := h₁.eq_or_lt.elim (assume : 0 = r, (tendsto_add_at_top_iff_nat 1).mp $ by simp [pow_succ, ← this, tendsto_const_nhds]) (assume : 0 < r, have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (𝓝 0), from tendsto_inv_at_top_zero.comp (tendsto_pow_at_top_at_top_of_one_lt $ one_lt_inv this h₂), this.congr (λ n, by simp)) lemma tendsto_pow_at_top_nhds_within_0_of_lt_1 {𝕜 : Type*} [linear_ordered_field 𝕜] [archimedean 𝕜] [topological_space 𝕜] [order_topology 𝕜] {r : 𝕜} (h₁ : 0 < r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (𝓝[Ioi 0] 0) := tendsto_inf.2 ⟨tendsto_pow_at_top_nhds_0_of_lt_1 h₁.le h₂, tendsto_principal.2 $ eventually_of_forall $ λ n, pow_pos h₁ _⟩ lemma is_o_pow_pow_of_lt_left {r₁ r₂ : ℝ} (h₁ : 0 ≤ r₁) (h₂ : r₁ < r₂) : is_o (λ n : ℕ, r₁ ^ n) (λ n, r₂ ^ n) at_top := have H : 0 < r₂ := h₁.trans_lt h₂, is_o_of_tendsto (λ n hn, false.elim $ H.ne' $ pow_eq_zero hn) $ (tendsto_pow_at_top_nhds_0_of_lt_1 (div_nonneg h₁ (h₁.trans h₂.le)) ((div_lt_one H).2 h₂)).congr (λ n, div_pow _ _ _) lemma is_O_pow_pow_of_le_left {r₁ r₂ : ℝ} (h₁ : 0 ≤ r₁) (h₂ : r₁ ≤ r₂) : is_O (λ n : ℕ, r₁ ^ n) (λ n, r₂ ^ n) at_top := h₂.eq_or_lt.elim (λ h, h ▸ is_O_refl _ _) (λ h, (is_o_pow_pow_of_lt_left h₁ h).is_O) lemma is_o_pow_pow_of_abs_lt_left {r₁ r₂ : ℝ} (h : |r₁| < |r₂|) : is_o (λ n : ℕ, r₁ ^ n) (λ n, r₂ ^ n) at_top := begin refine (is_o.of_norm_left _).of_norm_right, exact (is_o_pow_pow_of_lt_left (abs_nonneg r₁) h).congr (pow_abs r₁) (pow_abs r₂) end /-- Various statements equivalent to the fact that `f n` grows exponentially slower than `R ^ n`. * 0: $f n = o(a ^ n)$ for some $-R < a < R$; * 1: $f n = o(a ^ n)$ for some $0 < a < R$; * 2: $f n = O(a ^ n)$ for some $-R < a < R$; * 3: $f n = O(a ^ n)$ for some $0 < a < R$; * 4: there exist `a < R` and `C` such that one of `C` and `R` is positive and $|f n| ≤ Ca^n$ for all `n`; * 5: there exists `0 < a < R` and a positive `C` such that $|f n| ≤ Ca^n$ for all `n`; * 6: there exists `a < R` such that $|f n| ≤ a ^ n$ for sufficiently large `n`; * 7: there exists `0 < a < R` such that $|f n| ≤ a ^ n$ for sufficiently large `n`. NB: For backwards compatibility, if you add more items to the list, please append them at the end of the list. -/ lemma tfae_exists_lt_is_o_pow (f : ℕ → ℝ) (R : ℝ) : tfae [∃ a ∈ Ioo (-R) R, is_o f (pow a) at_top, ∃ a ∈ Ioo 0 R, is_o f (pow a) at_top, ∃ a ∈ Ioo (-R) R, is_O f (pow a) at_top, ∃ a ∈ Ioo 0 R, is_O f (pow a) at_top, ∃ (a < R) C (h₀ : 0 < C ∨ 0 < R), ∀ n, |f n| ≤ C * a ^ n, ∃ (a ∈ Ioo 0 R) (C > 0), ∀ n, |f n| ≤ C * a ^ n, ∃ a < R, ∀ᶠ n in at_top, |f n| ≤ a ^ n, ∃ a ∈ Ioo 0 R, ∀ᶠ n in at_top, |f n| ≤ a ^ n] := begin have A : Ico 0 R ⊆ Ioo (-R) R, from λ x hx, ⟨(neg_lt_zero.2 (hx.1.trans_lt hx.2)).trans_le hx.1, hx.2⟩, have B : Ioo 0 R ⊆ Ioo (-R) R := subset.trans Ioo_subset_Ico_self A, -- First we prove that 1-4 are equivalent using 2 → 3 → 4, 1 → 3, and 2 → 1 tfae_have : 1 → 3, from λ ⟨a, ha, H⟩, ⟨a, ha, H.is_O⟩, tfae_have : 2 → 1, from λ ⟨a, ha, H⟩, ⟨a, B ha, H⟩, tfae_have : 3 → 2, { rintro ⟨a, ha, H⟩, rcases exists_between (abs_lt.2 ha) with ⟨b, hab, hbR⟩, exact ⟨b, ⟨(abs_nonneg a).trans_lt hab, hbR⟩, H.trans_is_o (is_o_pow_pow_of_abs_lt_left (hab.trans_le (le_abs_self b)))⟩ }, tfae_have : 2 → 4, from λ ⟨a, ha, H⟩, ⟨a, ha, H.is_O⟩, tfae_have : 4 → 3, from λ ⟨a, ha, H⟩, ⟨a, B ha, H⟩, -- Add 5 and 6 using 4 → 6 → 5 → 3 tfae_have : 4 → 6, { rintro ⟨a, ha, H⟩, rcases bound_of_is_O_nat_at_top H with ⟨C, hC₀, hC⟩, refine ⟨a, ha, C, hC₀, λ n, _⟩, simpa only [real.norm_eq_abs, abs_pow, abs_of_nonneg ha.1.le] using hC (pow_ne_zero n ha.1.ne') }, tfae_have : 6 → 5, from λ ⟨a, ha, C, H₀, H⟩, ⟨a, ha.2, C, or.inl H₀, H⟩, tfae_have : 5 → 3, { rintro ⟨a, ha, C, h₀, H⟩, rcases sign_cases_of_C_mul_pow_nonneg (λ n, (abs_nonneg _).trans (H n)) with rfl | ⟨hC₀, ha₀⟩, { obtain rfl : f = 0, by { ext n, simpa using H n }, simp only [lt_irrefl, false_or] at h₀, exact ⟨0, ⟨neg_lt_zero.2 h₀, h₀⟩, is_O_zero _ _⟩ }, exact ⟨a, A ⟨ha₀, ha⟩, is_O_of_le' _ (λ n, (H n).trans $ mul_le_mul_of_nonneg_left (le_abs_self _) hC₀.le)⟩ }, -- Add 7 and 8 using 2 → 8 → 7 → 3 tfae_have : 2 → 8, { rintro ⟨a, ha, H⟩, refine ⟨a, ha, (H.def zero_lt_one).mono (λ n hn, _)⟩, rwa [real.norm_eq_abs, real.norm_eq_abs, one_mul, abs_pow, abs_of_pos ha.1] at hn }, tfae_have : 8 → 7, from λ ⟨a, ha, H⟩, ⟨a, ha.2, H⟩, tfae_have : 7 → 3, { rintro ⟨a, ha, H⟩, have : 0 ≤ a, from nonneg_of_eventually_pow_nonneg (H.mono $ λ n, (abs_nonneg _).trans), refine ⟨a, A ⟨this, ha⟩, is_O.of_bound 1 _⟩, simpa only [real.norm_eq_abs, one_mul, abs_pow, abs_of_nonneg this] }, tfae_finish end lemma uniformity_basis_dist_pow_of_lt_1 {α : Type*} [pseudo_metric_space α] {r : ℝ} (h₀ : 0 < r) (h₁ : r < 1) : (𝓤 α).has_basis (λ k : ℕ, true) (λ k, {p : α × α | dist p.1 p.2 < r ^ k}) := metric.mk_uniformity_basis (λ i _, pow_pos h₀ _) $ λ ε ε0, (exists_pow_lt_of_lt_one ε0 h₁).imp $ λ k hk, ⟨trivial, hk.le⟩ lemma geom_lt {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) {n : ℕ} (hn : 0 < n) (h : ∀ k < n, c * u k < u (k + 1)) : c ^ n * u 0 < u n := begin refine (monotone_mul_left_of_nonneg hc).seq_pos_lt_seq_of_le_of_lt hn _ _ h, { simp }, { simp [pow_succ, mul_assoc, le_refl] } end lemma geom_le {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) (n : ℕ) (h : ∀ k < n, c * u k ≤ u (k + 1)) : c ^ n * u 0 ≤ u n := by refine (monotone_mul_left_of_nonneg hc).seq_le_seq n _ _ h; simp [pow_succ, mul_assoc, le_refl] lemma lt_geom {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) {n : ℕ} (hn : 0 < n) (h : ∀ k < n, u (k + 1) < c * u k) : u n < c ^ n * u 0 := begin refine (monotone_mul_left_of_nonneg hc).seq_pos_lt_seq_of_lt_of_le hn _ h _, { simp }, { simp [pow_succ, mul_assoc, le_refl] } end lemma le_geom {u : ℕ → ℝ} {c : ℝ} (hc : 0 ≤ c) (n : ℕ) (h : ∀ k < n, u (k + 1) ≤ c * u k) : u n ≤ (c ^ n) * u 0 := by refine (monotone_mul_left_of_nonneg hc).seq_le_seq n _ h _; simp [pow_succ, mul_assoc, le_refl] /-- For any natural `k` and a real `r > 1` we have `n ^ k = o(r ^ n)` as `n → ∞`. -/ lemma is_o_pow_const_const_pow_of_one_lt {R : Type*} [normed_ring R] (k : ℕ) {r : ℝ} (hr : 1 < r) : is_o (λ n, n ^ k : ℕ → R) (λ n, r ^ n) at_top := begin have : tendsto (λ x : ℝ, x ^ k) (𝓝[Ioi 1] 1) (𝓝 1), from ((continuous_id.pow k).tendsto' (1 : ℝ) 1 (one_pow _)).mono_left inf_le_left, obtain ⟨r' : ℝ, hr' : r' ^ k < r, h1 : 1 < r'⟩ := ((this.eventually (gt_mem_nhds hr)).and self_mem_nhds_within).exists, have h0 : 0 ≤ r' := zero_le_one.trans h1.le, suffices : is_O _ (λ n : ℕ, (r' ^ k) ^ n) at_top, from this.trans_is_o (is_o_pow_pow_of_lt_left (pow_nonneg h0 _) hr'), conv in ((r' ^ _) ^ _) { rw [← pow_mul, mul_comm, pow_mul] }, suffices : ∀ n : ℕ, ∥(n : R)∥ ≤ (r' - 1)⁻¹ * ∥(1 : R)∥ * ∥r' ^ n∥, from (is_O_of_le' _ this).pow _, intro n, rw mul_right_comm, refine n.norm_cast_le.trans (mul_le_mul_of_nonneg_right _ (norm_nonneg _)), simpa [div_eq_inv_mul, real.norm_eq_abs, abs_of_nonneg h0] using n.cast_le_pow_div_sub h1 end /-- For a real `r > 1` we have `n = o(r ^ n)` as `n → ∞`. -/ lemma is_o_coe_const_pow_of_one_lt {R : Type*} [normed_ring R] {r : ℝ} (hr : 1 < r) : is_o (coe : ℕ → R) (λ n, r ^ n) at_top := by simpa only [pow_one] using is_o_pow_const_const_pow_of_one_lt 1 hr /-- If `∥r₁∥ < r₂`, then for any naturak `k` we have `n ^ k r₁ ^ n = o (r₂ ^ n)` as `n → ∞`. -/ lemma is_o_pow_const_mul_const_pow_const_pow_of_norm_lt {R : Type*} [normed_ring R] (k : ℕ) {r₁ : R} {r₂ : ℝ} (h : ∥r₁∥ < r₂) : is_o (λ n, n ^ k * r₁ ^ n : ℕ → R) (λ n, r₂ ^ n) at_top := begin by_cases h0 : r₁ = 0, { refine (is_o_zero _ _).congr' (mem_at_top_sets.2 $ ⟨1, λ n hn, _⟩) eventually_eq.rfl, simp [zero_pow (zero_lt_one.trans_le hn), h0] }, rw [← ne.def, ← norm_pos_iff] at h0, have A : is_o (λ n, n ^ k : ℕ → R) (λ n, (r₂ / ∥r₁∥) ^ n) at_top, from is_o_pow_const_const_pow_of_one_lt k ((one_lt_div h0).2 h), suffices : is_O (λ n, r₁ ^ n) (λ n, ∥r₁∥ ^ n) at_top, by simpa [div_mul_cancel _ (pow_pos h0 _).ne'] using A.mul_is_O this, exact is_O.of_bound 1 (by simpa using eventually_norm_pow_le r₁) end lemma tendsto_pow_const_div_const_pow_of_one_lt (k : ℕ) {r : ℝ} (hr : 1 < r) : tendsto (λ n, n ^ k / r ^ n : ℕ → ℝ) at_top (𝓝 0) := (is_o_pow_const_const_pow_of_one_lt k hr).tendsto_0 /-- If `|r| < 1`, then `n ^ k r ^ n` tends to zero for any natural `k`. -/ lemma tendsto_pow_const_mul_const_pow_of_abs_lt_one (k : ℕ) {r : ℝ} (hr : |r| < 1) : tendsto (λ n, n ^ k * r ^ n : ℕ → ℝ) at_top (𝓝 0) := begin by_cases h0 : r = 0, { exact tendsto_const_nhds.congr' (mem_at_top_sets.2 ⟨1, λ n hn, by simp [zero_lt_one.trans_le hn, h0]⟩) }, have hr' : 1 < (|r|)⁻¹, from one_lt_inv (abs_pos.2 h0) hr, rw tendsto_zero_iff_norm_tendsto_zero, simpa [div_eq_mul_inv] using tendsto_pow_const_div_const_pow_of_one_lt k hr' end /-- If a sequence `v` of real numbers satisfies `k * v n ≤ v (n+1)` with `1 < k`, then it goes to +∞. -/ lemma tendsto_at_top_of_geom_le {v : ℕ → ℝ} {c : ℝ} (h₀ : 0 < v 0) (hc : 1 < c) (hu : ∀ n, c * v n ≤ v (n + 1)) : tendsto v at_top at_top := tendsto_at_top_mono (λ n, geom_le (zero_le_one.trans hc.le) n (λ k hk, hu k)) $ (tendsto_pow_at_top_at_top_of_one_lt hc).at_top_mul_const h₀ lemma nnreal.tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ≥0} (hr : r < 1) : tendsto (λ n:ℕ, r^n) at_top (𝓝 0) := nnreal.tendsto_coe.1 $ by simp only [nnreal.coe_pow, nnreal.coe_zero, tendsto_pow_at_top_nhds_0_of_lt_1 r.coe_nonneg hr] lemma ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ≥0∞} (hr : r < 1) : tendsto (λ n:ℕ, r^n) at_top (𝓝 0) := begin rcases ennreal.lt_iff_exists_coe.1 hr with ⟨r, rfl, hr'⟩, rw [← ennreal.coe_zero], norm_cast at *, apply nnreal.tendsto_pow_at_top_nhds_0_of_lt_1 hr end /-- In a normed ring, the powers of an element x with `∥x∥ < 1` tend to zero. -/ lemma tendsto_pow_at_top_nhds_0_of_norm_lt_1 {R : Type*} [normed_ring R] {x : R} (h : ∥x∥ < 1) : tendsto (λ (n : ℕ), x ^ n) at_top (𝓝 0) := begin apply squeeze_zero_norm' (eventually_norm_pow_le x), exact tendsto_pow_at_top_nhds_0_of_lt_1 (norm_nonneg _) h, end lemma tendsto_pow_at_top_nhds_0_of_abs_lt_1 {r : ℝ} (h : |r| < 1) : tendsto (λn:ℕ, r^n) at_top (𝓝 0) := tendsto_pow_at_top_nhds_0_of_norm_lt_1 h /-! ### Geometric series-/ section geometric lemma has_sum_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : has_sum (λn:ℕ, r ^ n) (1 - r)⁻¹ := have r ≠ 1, from ne_of_lt h₂, have tendsto (λn, (r ^ n - 1) * (r - 1)⁻¹) at_top (𝓝 ((0 - 1) * (r - 1)⁻¹)), from ((tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂).sub tendsto_const_nhds).mul tendsto_const_nhds, have (λ n, (∑ i in range n, r ^ i)) = (λ n, geom_sum r n) := rfl, (has_sum_iff_tendsto_nat_of_nonneg (pow_nonneg h₁) _).mpr $ by simp [neg_inv, geom_sum_eq, div_eq_mul_inv, *] at * lemma summable_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : summable (λn:ℕ, r ^ n) := ⟨_, has_sum_geometric_of_lt_1 h₁ h₂⟩ lemma tsum_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := (has_sum_geometric_of_lt_1 h₁ h₂).tsum_eq lemma has_sum_geometric_two : has_sum (λn:ℕ, ((1:ℝ)/2) ^ n) 2 := by convert has_sum_geometric_of_lt_1 _ _; norm_num lemma summable_geometric_two : summable (λn:ℕ, ((1:ℝ)/2) ^ n) := ⟨_, has_sum_geometric_two⟩ lemma tsum_geometric_two : ∑'n:ℕ, ((1:ℝ)/2) ^ n = 2 := has_sum_geometric_two.tsum_eq lemma sum_geometric_two_le (n : ℕ) : ∑ (i : ℕ) in range n, (1 / (2 : ℝ)) ^ i ≤ 2 := begin have : ∀ i, 0 ≤ (1 / (2 : ℝ)) ^ i, { intro i, apply pow_nonneg, norm_num }, convert sum_le_tsum (range n) (λ i _, this i) summable_geometric_two, exact tsum_geometric_two.symm end lemma has_sum_geometric_two' (a : ℝ) : has_sum (λn:ℕ, (a / 2) / 2 ^ n) a := begin convert has_sum.mul_left (a / 2) (has_sum_geometric_of_lt_1 (le_of_lt one_half_pos) one_half_lt_one), { funext n, simp, refl, }, { norm_num } end lemma summable_geometric_two' (a : ℝ) : summable (λ n:ℕ, (a / 2) / 2 ^ n) := ⟨a, has_sum_geometric_two' a⟩ lemma tsum_geometric_two' (a : ℝ) : ∑' n:ℕ, (a / 2) / 2^n = a := (has_sum_geometric_two' a).tsum_eq /-- **Sum of a Geometric Series** -/ lemma nnreal.has_sum_geometric {r : ℝ≥0} (hr : r < 1) : has_sum (λ n : ℕ, r ^ n) (1 - r)⁻¹ := begin apply nnreal.has_sum_coe.1, push_cast, rw [nnreal.coe_sub (le_of_lt hr)], exact has_sum_geometric_of_lt_1 r.coe_nonneg hr end lemma nnreal.summable_geometric {r : ℝ≥0} (hr : r < 1) : summable (λn:ℕ, r ^ n) := ⟨_, nnreal.has_sum_geometric hr⟩ lemma tsum_geometric_nnreal {r : ℝ≥0} (hr : r < 1) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := (nnreal.has_sum_geometric hr).tsum_eq /-- The series `pow r` converges to `(1-r)⁻¹`. For `r < 1` the RHS is a finite number, and for `1 ≤ r` the RHS equals `∞`. -/ @[simp] lemma ennreal.tsum_geometric (r : ℝ≥0∞) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := begin cases lt_or_le r 1 with hr hr, { rcases ennreal.lt_iff_exists_coe.1 hr with ⟨r, rfl, hr'⟩, norm_cast at *, convert ennreal.tsum_coe_eq (nnreal.has_sum_geometric hr), rw [ennreal.coe_inv $ ne_of_gt $ tsub_pos_iff_lt.2 hr] }, { rw [tsub_eq_zero_iff_le.mpr hr, ennreal.inv_zero, ennreal.tsum_eq_supr_nat, supr_eq_top], refine λ a ha, (ennreal.exists_nat_gt (lt_top_iff_ne_top.1 ha)).imp (λ n hn, lt_of_lt_of_le hn _), calc (n:ℝ≥0∞) = ∑ i in range n, 1 : by rw [sum_const, nsmul_one, card_range] ... ≤ ∑ i in range n, r ^ i : sum_le_sum (λ k _, one_le_pow_of_one_le' hr k) } end variables {K : Type*} [normed_field K] {ξ : K} lemma has_sum_geometric_of_norm_lt_1 (h : ∥ξ∥ < 1) : has_sum (λn:ℕ, ξ ^ n) (1 - ξ)⁻¹ := begin have xi_ne_one : ξ ≠ 1, by { contrapose! h, simp [h] }, have A : tendsto (λn, (ξ ^ n - 1) * (ξ - 1)⁻¹) at_top (𝓝 ((0 - 1) * (ξ - 1)⁻¹)), from ((tendsto_pow_at_top_nhds_0_of_norm_lt_1 h).sub tendsto_const_nhds).mul tendsto_const_nhds, have B : (λ n, (∑ i in range n, ξ ^ i)) = (λ n, geom_sum ξ n) := rfl, rw [has_sum_iff_tendsto_nat_of_summable_norm, B], { simpa [geom_sum_eq, xi_ne_one, neg_inv, div_eq_mul_inv] using A }, { simp [normed_field.norm_pow, summable_geometric_of_lt_1 (norm_nonneg _) h] } end lemma summable_geometric_of_norm_lt_1 (h : ∥ξ∥ < 1) : summable (λn:ℕ, ξ ^ n) := ⟨_, has_sum_geometric_of_norm_lt_1 h⟩ lemma tsum_geometric_of_norm_lt_1 (h : ∥ξ∥ < 1) : ∑'n:ℕ, ξ ^ n = (1 - ξ)⁻¹ := (has_sum_geometric_of_norm_lt_1 h).tsum_eq lemma has_sum_geometric_of_abs_lt_1 {r : ℝ} (h : |r| < 1) : has_sum (λn:ℕ, r ^ n) (1 - r)⁻¹ := has_sum_geometric_of_norm_lt_1 h lemma summable_geometric_of_abs_lt_1 {r : ℝ} (h : |r| < 1) : summable (λn:ℕ, r ^ n) := summable_geometric_of_norm_lt_1 h lemma tsum_geometric_of_abs_lt_1 {r : ℝ} (h : |r| < 1) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := tsum_geometric_of_norm_lt_1 h /-- A geometric series in a normed field is summable iff the norm of the common ratio is less than one. -/ @[simp] lemma summable_geometric_iff_norm_lt_1 : summable (λ n : ℕ, ξ ^ n) ↔ ∥ξ∥ < 1 := begin refine ⟨λ h, _, summable_geometric_of_norm_lt_1⟩, obtain ⟨k : ℕ, hk : dist (ξ ^ k) 0 < 1⟩ := (h.tendsto_cofinite_zero.eventually (ball_mem_nhds _ zero_lt_one)).exists, simp only [normed_field.norm_pow, dist_zero_right] at hk, rw [← one_pow k] at hk, exact lt_of_pow_lt_pow _ zero_le_one hk end end geometric section mul_geometric lemma summable_norm_pow_mul_geometric_of_norm_lt_1 {R : Type*} [normed_ring R] (k : ℕ) {r : R} (hr : ∥r∥ < 1) : summable (λ n : ℕ, ∥(n ^ k * r ^ n : R)∥) := begin rcases exists_between hr with ⟨r', hrr', h⟩, exact summable_of_is_O_nat (summable_geometric_of_lt_1 ((norm_nonneg _).trans hrr'.le) h) (is_o_pow_const_mul_const_pow_const_pow_of_norm_lt _ hrr').is_O.norm_left end lemma summable_pow_mul_geometric_of_norm_lt_1 {R : Type*} [normed_ring R] [complete_space R] (k : ℕ) {r : R} (hr : ∥r∥ < 1) : summable (λ n, n ^ k * r ^ n : ℕ → R) := summable_of_summable_norm $ summable_norm_pow_mul_geometric_of_norm_lt_1 _ hr /-- If `∥r∥ < 1`, then `∑' n : ℕ, n * r ^ n = r / (1 - r) ^ 2`, `has_sum` version. -/ lemma has_sum_coe_mul_geometric_of_norm_lt_1 {𝕜 : Type*} [normed_field 𝕜] [complete_space 𝕜] {r : 𝕜} (hr : ∥r∥ < 1) : has_sum (λ n, n * r ^ n : ℕ → 𝕜) (r / (1 - r) ^ 2) := begin have A : summable (λ n, n * r ^ n : ℕ → 𝕜), by simpa using summable_pow_mul_geometric_of_norm_lt_1 1 hr, have B : has_sum (pow r : ℕ → 𝕜) (1 - r)⁻¹, from has_sum_geometric_of_norm_lt_1 hr, refine A.has_sum_iff.2 _, have hr' : r ≠ 1, by { rintro rfl, simpa [lt_irrefl] using hr }, set s : 𝕜 := ∑' n : ℕ, n * r ^ n, calc s = (1 - r) * s / (1 - r) : (mul_div_cancel_left _ (sub_ne_zero.2 hr'.symm)).symm ... = (s - r * s) / (1 - r) : by rw [sub_mul, one_mul] ... = ((0 : ℕ) * r ^ 0 + (∑' n : ℕ, (n + 1) * r ^ (n + 1)) - r * s) / (1 - r) : by { congr, exact tsum_eq_zero_add A } ... = (r * (∑' n : ℕ, (n + 1) * r ^ n) - r * s) / (1 - r) : by simp [pow_succ, mul_left_comm _ r, tsum_mul_left] ... = r / (1 - r) ^ 2 : by simp [add_mul, tsum_add A B.summable, mul_add, B.tsum_eq, ← div_eq_mul_inv, sq, div_div_eq_div_mul] end /-- If `∥r∥ < 1`, then `∑' n : ℕ, n * r ^ n = r / (1 - r) ^ 2`. -/ lemma tsum_coe_mul_geometric_of_norm_lt_1 {𝕜 : Type*} [normed_field 𝕜] [complete_space 𝕜] {r : 𝕜} (hr : ∥r∥ < 1) : (∑' n : ℕ, n * r ^ n : 𝕜) = (r / (1 - r) ^ 2) := (has_sum_coe_mul_geometric_of_norm_lt_1 hr).tsum_eq end mul_geometric /-! ### Sequences with geometrically decaying distance in metric spaces In this paragraph, we discuss sequences in metric spaces or emetric spaces for which the distance between two consecutive terms decays geometrically. We show that such sequences are Cauchy sequences, and bound their distances to the limit. We also discuss series with geometrically decaying terms. -/ section edist_le_geometric variables [pseudo_emetric_space α] (r C : ℝ≥0∞) (hr : r < 1) (hC : C ≠ ⊤) {f : ℕ → α} (hu : ∀n, edist (f n) (f (n+1)) ≤ C * r^n) include hr hC hu /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, `C ≠ ∞`, `r < 1`, then `f` is a Cauchy sequence.-/ lemma cauchy_seq_of_edist_le_geometric : cauchy_seq f := begin refine cauchy_seq_of_edist_le_of_tsum_ne_top _ hu _, rw [ennreal.tsum_mul_left, ennreal.tsum_geometric], refine ennreal.mul_ne_top hC (ennreal.inv_ne_top.2 _), exact (tsub_pos_iff_lt.2 hr).ne' end omit hr hC /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, then the distance from `f n` to the limit of `f` is bounded above by `C * r^n / (1 - r)`. -/ lemma edist_le_of_edist_le_geometric_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : edist (f n) a ≤ (C * r^n) / (1 - r) := begin convert edist_le_tsum_of_edist_le_of_tendsto _ hu ha _, simp only [pow_add, ennreal.tsum_mul_left, ennreal.tsum_geometric, div_eq_mul_inv, mul_assoc] end /-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, then the distance from `f 0` to the limit of `f` is bounded above by `C / (1 - r)`. -/ lemma edist_le_of_edist_le_geometric_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) : edist (f 0) a ≤ C / (1 - r) := by simpa only [pow_zero, mul_one] using edist_le_of_edist_le_geometric_of_tendsto r C hu ha 0 end edist_le_geometric section edist_le_geometric_two variables [pseudo_emetric_space α] (C : ℝ≥0∞) (hC : C ≠ ⊤) {f : ℕ → α} (hu : ∀n, edist (f n) (f (n+1)) ≤ C / 2^n) {a : α} (ha : tendsto f at_top (𝓝 a)) include hC hu /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then `f` is a Cauchy sequence.-/ lemma cauchy_seq_of_edist_le_geometric_two : cauchy_seq f := begin simp only [div_eq_mul_inv, ennreal.inv_pow] at hu, refine cauchy_seq_of_edist_le_geometric 2⁻¹ C _ hC hu, simp [ennreal.one_lt_two] end omit hC include ha /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then the distance from `f n` to the limit of `f` is bounded above by `2 * C * 2^-n`. -/ lemma edist_le_of_edist_le_geometric_two_of_tendsto (n : ℕ) : edist (f n) a ≤ 2 * C / 2^n := begin simp only [div_eq_mul_inv, ennreal.inv_pow] at *, rw [mul_assoc, mul_comm], convert edist_le_of_edist_le_geometric_of_tendsto 2⁻¹ C hu ha n, rw [ennreal.one_sub_inv_two, ennreal.inv_inv] end /-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then the distance from `f 0` to the limit of `f` is bounded above by `2 * C`. -/ lemma edist_le_of_edist_le_geometric_two_of_tendsto₀: edist (f 0) a ≤ 2 * C := by simpa only [pow_zero, div_eq_mul_inv, ennreal.inv_one, mul_one] using edist_le_of_edist_le_geometric_two_of_tendsto C hu ha 0 end edist_le_geometric_two section le_geometric variables [pseudo_metric_space α] {r C : ℝ} (hr : r < 1) {f : ℕ → α} (hu : ∀n, dist (f n) (f (n+1)) ≤ C * r^n) include hr hu lemma aux_has_sum_of_le_geometric : has_sum (λ n : ℕ, C * r^n) (C / (1 - r)) := begin rcases sign_cases_of_C_mul_pow_nonneg (λ n, dist_nonneg.trans (hu n)) with rfl | ⟨C₀, r₀⟩, { simp [has_sum_zero] }, { refine has_sum.mul_left C _, simpa using has_sum_geometric_of_lt_1 r₀ hr } end variables (r C) /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then `f` is a Cauchy sequence. Note that this lemma does not assume `0 ≤ C` or `0 ≤ r`. -/ lemma cauchy_seq_of_le_geometric : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ hu ⟨_, aux_has_sum_of_le_geometric hr hu⟩ /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then the distance from `f n` to the limit of `f` is bounded above by `C * r^n / (1 - r)`. -/ lemma dist_le_of_le_geometric_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ C / (1 - r) := (aux_has_sum_of_le_geometric hr hu).tsum_eq ▸ dist_le_tsum_of_dist_le_of_tendsto₀ _ hu ⟨_, aux_has_sum_of_le_geometric hr hu⟩ ha /-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then the distance from `f 0` to the limit of `f` is bounded above by `C / (1 - r)`. -/ lemma dist_le_of_le_geometric_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : dist (f n) a ≤ (C * r^n) / (1 - r) := begin have := aux_has_sum_of_le_geometric hr hu, convert dist_le_tsum_of_dist_le_of_tendsto _ hu ⟨_, this⟩ ha n, simp only [pow_add, mul_left_comm C, mul_div_right_comm], rw [mul_comm], exact (this.mul_left _).tsum_eq.symm end omit hr hu variable (hu₂ : ∀ n, dist (f n) (f (n+1)) ≤ (C / 2) / 2^n) /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then `f` is a Cauchy sequence. -/ lemma cauchy_seq_of_le_geometric_two : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ hu₂ $ ⟨_, has_sum_geometric_two' C⟩ /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then the distance from `f 0` to the limit of `f` is bounded above by `C`. -/ lemma dist_le_of_le_geometric_two_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ C := (tsum_geometric_two' C) ▸ dist_le_tsum_of_dist_le_of_tendsto₀ _ hu₂ (summable_geometric_two' C) ha include hu₂ /-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then the distance from `f n` to the limit of `f` is bounded above by `C / 2^n`. -/ lemma dist_le_of_le_geometric_two_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : dist (f n) a ≤ C / 2^n := begin convert dist_le_tsum_of_dist_le_of_tendsto _ hu₂ (summable_geometric_two' C) ha n, simp only [add_comm n, pow_add, ← div_div_eq_div_mul], symmetry, exact ((has_sum_geometric_two' C).div_const _).tsum_eq end end le_geometric section summable_le_geometric variables [semi_normed_group α] {r C : ℝ} {f : ℕ → α} lemma semi_normed_group.cauchy_seq_of_le_geometric {C : ℝ} {r : ℝ} (hr : r < 1) {u : ℕ → α} (h : ∀ n, ∥u n - u (n + 1)∥ ≤ C*r^n) : cauchy_seq u := cauchy_seq_of_le_geometric r C hr (by simpa [dist_eq_norm] using h) lemma dist_partial_sum_le_of_le_geometric (hf : ∀n, ∥f n∥ ≤ C * r^n) (n : ℕ) : dist (∑ i in range n, f i) (∑ i in range (n+1), f i) ≤ C * r ^ n := begin rw [sum_range_succ, dist_eq_norm, ← norm_neg, neg_sub, add_sub_cancel'], exact hf n, end /-- If `∥f n∥ ≤ C * r ^ n` for all `n : ℕ` and some `r < 1`, then the partial sums of `f` form a Cauchy sequence. This lemma does not assume `0 ≤ r` or `0 ≤ C`. -/ lemma cauchy_seq_finset_of_geometric_bound (hr : r < 1) (hf : ∀n, ∥f n∥ ≤ C * r^n) : cauchy_seq (λ s : finset (ℕ), ∑ x in s, f x) := cauchy_seq_finset_of_norm_bounded _ (aux_has_sum_of_le_geometric hr (dist_partial_sum_le_of_le_geometric hf)).summable hf /-- If `∥f n∥ ≤ C * r ^ n` for all `n : ℕ` and some `r < 1`, then the partial sums of `f` are within distance `C * r ^ n / (1 - r)` of the sum of the series. This lemma does not assume `0 ≤ r` or `0 ≤ C`. -/ lemma norm_sub_le_of_geometric_bound_of_has_sum (hr : r < 1) (hf : ∀n, ∥f n∥ ≤ C * r^n) {a : α} (ha : has_sum f a) (n : ℕ) : ∥(∑ x in finset.range n, f x) - a∥ ≤ (C * r ^ n) / (1 - r) := begin rw ← dist_eq_norm, apply dist_le_of_le_geometric_of_tendsto r C hr (dist_partial_sum_le_of_le_geometric hf), exact ha.tendsto_sum_nat end @[simp] lemma dist_partial_sum (u : ℕ → α) (n : ℕ) : dist (∑ k in range (n + 1), u k) (∑ k in range n, u k) = ∥u n∥ := by simp [dist_eq_norm, sum_range_succ] @[simp] lemma dist_partial_sum' (u : ℕ → α) (n : ℕ) : dist (∑ k in range n, u k) (∑ k in range (n+1), u k) = ∥u n∥ := by simp [dist_eq_norm', sum_range_succ] lemma cauchy_series_of_le_geometric {C : ℝ} {u : ℕ → α} {r : ℝ} (hr : r < 1) (h : ∀ n, ∥u n∥ ≤ C*r^n) : cauchy_seq (λ n, ∑ k in range n, u k) := cauchy_seq_of_le_geometric r C hr (by simp [h]) lemma normed_group.cauchy_series_of_le_geometric' {C : ℝ} {u : ℕ → α} {r : ℝ} (hr : r < 1) (h : ∀ n, ∥u n∥ ≤ C*r^n) : cauchy_seq (λ n, ∑ k in range (n + 1), u k) := begin by_cases hC : C = 0, { subst hC, simp at h, exact cauchy_seq_of_le_geometric 0 0 zero_lt_one (by simp [h]) }, have : 0 ≤ C, { simpa using (norm_nonneg _).trans (h 0) }, replace hC : 0 < C, from (ne.symm hC).le_iff_lt.mp this, have : 0 ≤ r, { have := (norm_nonneg _).trans (h 1), rw pow_one at this, exact (zero_le_mul_left hC).mp this }, simp_rw finset.sum_range_succ_comm, have : cauchy_seq u, { apply tendsto.cauchy_seq, apply squeeze_zero_norm h, rw show 0 = C*0, by simp, exact tendsto_const_nhds.mul (tendsto_pow_at_top_nhds_0_of_lt_1 this hr) }, exact this.add (cauchy_series_of_le_geometric hr h), end lemma normed_group.cauchy_series_of_le_geometric'' {C : ℝ} {u : ℕ → α} {N : ℕ} {r : ℝ} (hr₀ : 0 < r) (hr₁ : r < 1) (h : ∀ n ≥ N, ∥u n∥ ≤ C*r^n) : cauchy_seq (λ n, ∑ k in range (n + 1), u k) := begin set v : ℕ → α := λ n, if n < N then 0 else u n, have hC : 0 ≤ C, from (zero_le_mul_right $ pow_pos hr₀ N).mp ((norm_nonneg _).trans $ h N $ le_refl N), have : ∀ n ≥ N, u n = v n, { intros n hn, simp [v, hn, if_neg (not_lt.mpr hn)] }, refine cauchy_seq_sum_of_eventually_eq this (normed_group.cauchy_series_of_le_geometric' hr₁ _), { exact C }, intro n, dsimp [v], split_ifs with H H, { rw norm_zero, exact mul_nonneg hC (pow_nonneg hr₀.le _) }, { push_neg at H, exact h _ H } end end summable_le_geometric section normed_ring_geometric variables {R : Type*} [normed_ring R] [complete_space R] open normed_space /-- A geometric series in a complete normed ring is summable. Proved above (same name, different namespace) for not-necessarily-complete normed fields. -/ lemma normed_ring.summable_geometric_of_norm_lt_1 (x : R) (h : ∥x∥ < 1) : summable (λ (n:ℕ), x ^ n) := begin have h1 : summable (λ (n:ℕ), ∥x∥ ^ n) := summable_geometric_of_lt_1 (norm_nonneg _) h, refine summable_of_norm_bounded_eventually _ h1 _, rw nat.cofinite_eq_at_top, exact eventually_norm_pow_le x, end /-- Bound for the sum of a geometric series in a normed ring. This formula does not assume that the normed ring satisfies the axiom `∥1∥ = 1`. -/ lemma normed_ring.tsum_geometric_of_norm_lt_1 (x : R) (h : ∥x∥ < 1) : ∥∑' n:ℕ, x ^ n∥ ≤ ∥(1:R)∥ - 1 + (1 - ∥x∥)⁻¹ := begin rw tsum_eq_zero_add (normed_ring.summable_geometric_of_norm_lt_1 x h), simp only [pow_zero], refine le_trans (norm_add_le _ _) _, have : ∥∑' b : ℕ, (λ n, x ^ (n + 1)) b∥ ≤ (1 - ∥x∥)⁻¹ - 1, { refine tsum_of_norm_bounded _ (λ b, norm_pow_le' _ (nat.succ_pos b)), convert (has_sum_nat_add_iff' 1).mpr (has_sum_geometric_of_lt_1 (norm_nonneg x) h), simp }, linarith end lemma geom_series_mul_neg (x : R) (h : ∥x∥ < 1) : (∑' i:ℕ, x ^ i) * (1 - x) = 1 := begin have := ((normed_ring.summable_geometric_of_norm_lt_1 x h).has_sum.mul_right (1 - x)), refine tendsto_nhds_unique this.tendsto_sum_nat _, have : tendsto (λ (n : ℕ), 1 - x ^ n) at_top (𝓝 1), { simpa using tendsto_const_nhds.sub (tendsto_pow_at_top_nhds_0_of_norm_lt_1 h) }, convert ← this, ext n, rw [←geom_sum_mul_neg, geom_sum_def, finset.sum_mul], end lemma mul_neg_geom_series (x : R) (h : ∥x∥ < 1) : (1 - x) * ∑' i:ℕ, x ^ i = 1 := begin have := (normed_ring.summable_geometric_of_norm_lt_1 x h).has_sum.mul_left (1 - x), refine tendsto_nhds_unique this.tendsto_sum_nat _, have : tendsto (λ (n : ℕ), 1 - x ^ n) at_top (nhds 1), { simpa using tendsto_const_nhds.sub (tendsto_pow_at_top_nhds_0_of_norm_lt_1 h) }, convert ← this, ext n, rw [←mul_neg_geom_sum, geom_sum_def, finset.mul_sum] end end normed_ring_geometric /-! ### Summability tests based on comparison with geometric series -/ lemma summable_of_ratio_norm_eventually_le {α : Type*} [semi_normed_group α] [complete_space α] {f : ℕ → α} {r : ℝ} (hr₁ : r < 1) (h : ∀ᶠ n in at_top, ∥f (n+1)∥ ≤ r * ∥f n∥) : summable f := begin by_cases hr₀ : 0 ≤ r, { rw eventually_at_top at h, rcases h with ⟨N, hN⟩, rw ← @summable_nat_add_iff α _ _ _ _ N, refine summable_of_norm_bounded (λ n, ∥f N∥ * r^n) (summable.mul_left _ $ summable_geometric_of_lt_1 hr₀ hr₁) (λ n, _), conv_rhs {rw [mul_comm, ← zero_add N]}, refine le_geom hr₀ n (λ i _, _), convert hN (i + N) (N.le_add_left i) using 3, ac_refl }, { push_neg at hr₀, refine summable_of_norm_bounded_eventually 0 summable_zero _, rw nat.cofinite_eq_at_top, filter_upwards [h], intros n hn, by_contra h, push_neg at h, exact not_lt.mpr (norm_nonneg _) (lt_of_le_of_lt hn $ mul_neg_of_neg_of_pos hr₀ h) } end lemma summable_of_ratio_test_tendsto_lt_one {α : Type*} [normed_group α] [complete_space α] {f : ℕ → α} {l : ℝ} (hl₁ : l < 1) (hf : ∀ᶠ n in at_top, f n ≠ 0) (h : tendsto (λ n, ∥f (n+1)∥/∥f n∥) at_top (𝓝 l)) : summable f := begin rcases exists_between hl₁ with ⟨r, hr₀, hr₁⟩, refine summable_of_ratio_norm_eventually_le hr₁ _, filter_upwards [eventually_le_of_tendsto_lt hr₀ h, hf], intros n h₀ h₁, rwa ← div_le_iff (norm_pos_iff.mpr h₁) end lemma not_summable_of_ratio_norm_eventually_ge {α : Type*} [semi_normed_group α] {f : ℕ → α} {r : ℝ} (hr : 1 < r) (hf : ∃ᶠ n in at_top, ∥f n∥ ≠ 0) (h : ∀ᶠ n in at_top, r * ∥f n∥ ≤ ∥f (n+1)∥) : ¬ summable f := begin rw eventually_at_top at h, rcases h with ⟨N₀, hN₀⟩, rw frequently_at_top at hf, rcases hf N₀ with ⟨N, hNN₀ : N₀ ≤ N, hN⟩, rw ← @summable_nat_add_iff α _ _ _ _ N, refine mt summable.tendsto_at_top_zero (λ h', not_tendsto_at_top_of_tendsto_nhds (tendsto_norm_zero.comp h') _), convert tendsto_at_top_of_geom_le _ hr _, { refine lt_of_le_of_ne (norm_nonneg _) _, intro h'', specialize hN₀ N hNN₀, simp only [comp_app, zero_add] at h'', exact hN h''.symm }, { intro i, dsimp only [comp_app], convert (hN₀ (i + N) (hNN₀.trans (N.le_add_left i))) using 3, ac_refl } end lemma not_summable_of_ratio_test_tendsto_gt_one {α : Type*} [semi_normed_group α] {f : ℕ → α} {l : ℝ} (hl : 1 < l) (h : tendsto (λ n, ∥f (n+1)∥/∥f n∥) at_top (𝓝 l)) : ¬ summable f := begin have key : ∀ᶠ n in at_top, ∥f n∥ ≠ 0, { filter_upwards [eventually_ge_of_tendsto_gt hl h], intros n hn hc, rw [hc, div_zero] at hn, linarith }, rcases exists_between hl with ⟨r, hr₀, hr₁⟩, refine not_summable_of_ratio_norm_eventually_ge hr₀ key.frequently _, filter_upwards [eventually_ge_of_tendsto_gt hr₁ h, key], intros n h₀ h₁, rwa ← le_div_iff (lt_of_le_of_ne (norm_nonneg _) h₁.symm) end /-- A series whose terms are bounded by the terms of a converging geometric series converges. -/ lemma summable_one_div_pow_of_le {m : ℝ} {f : ℕ → ℕ} (hm : 1 < m) (fi : ∀ i, i ≤ f i) : summable (λ i, 1 / m ^ f i) := begin refine summable_of_nonneg_of_le (λ a, one_div_nonneg.mpr (pow_nonneg (zero_le_one.trans hm.le) _)) (λ a, _) (summable_geometric_of_lt_1 (one_div_nonneg.mpr (zero_le_one.trans hm.le)) ((one_div_lt (zero_lt_one.trans hm) zero_lt_one).mpr (one_div_one.le.trans_lt hm))), rw [div_pow, one_pow], refine (one_div_le_one_div _ _).mpr (pow_le_pow hm.le (fi a)); exact pow_pos (zero_lt_one.trans hm) _ end /-! ### Positive sequences with small sums on encodable types -/ /-- For any positive `ε`, define on an encodable type a positive sequence with sum less than `ε` -/ def pos_sum_of_encodable {ε : ℝ} (hε : 0 < ε) (ι) [encodable ι] : {ε' : ι → ℝ // (∀ i, 0 < ε' i) ∧ ∃ c, has_sum ε' c ∧ c ≤ ε} := begin let f := λ n, (ε / 2) / 2 ^ n, have hf : has_sum f ε := has_sum_geometric_two' _, have f0 : ∀ n, 0 < f n := λ n, div_pos (half_pos hε) (pow_pos zero_lt_two _), refine ⟨f ∘ encodable.encode, λ i, f0 _, _⟩, rcases hf.summable.comp_injective (@encodable.encode_injective ι _) with ⟨c, hg⟩, refine ⟨c, hg, has_sum_le_inj _ (@encodable.encode_injective ι _) _ _ hg hf⟩, { assume i _, exact le_of_lt (f0 _) }, { assume n, exact le_refl _ } end lemma set.countable.exists_pos_has_sum_le {ι : Type*} {s : set ι} (hs : s.countable) {ε : ℝ} (hε : 0 < ε) : ∃ ε' : ι → ℝ, (∀ i, 0 < ε' i) ∧ ∃ c, has_sum (λ i : s, ε' i) c ∧ c ≤ ε := begin haveI := hs.to_encodable, rcases pos_sum_of_encodable hε s with ⟨f, hf0, ⟨c, hfc, hcε⟩⟩, refine ⟨λ i, if h : i ∈ s then f ⟨i, h⟩ else 1, λ i, _, ⟨c, _, hcε⟩⟩, { split_ifs, exacts [hf0 _, zero_lt_one] }, { simpa only [subtype.coe_prop, dif_pos, subtype.coe_eta] } end lemma set.countable.exists_pos_forall_sum_le {ι : Type*} {s : set ι} (hs : s.countable) {ε : ℝ} (hε : 0 < ε) : ∃ ε' : ι → ℝ, (∀ i, 0 < ε' i) ∧ ∀ t : finset ι, ↑t ⊆ s → ∑ i in t, ε' i ≤ ε := begin rcases hs.exists_pos_has_sum_le hε with ⟨ε', hpos, c, hε'c, hcε⟩, refine ⟨ε', hpos, λ t ht, _⟩, rw [← sum_subtype_of_mem _ ht], refine (sum_le_has_sum _ _ hε'c).trans hcε, exact λ _ _, (hpos _).le end namespace nnreal theorem exists_pos_sum_of_encodable {ε : ℝ≥0} (hε : ε ≠ 0) (ι) [encodable ι] : ∃ ε' : ι → ℝ≥0, (∀ i, 0 < ε' i) ∧ ∃c, has_sum ε' c ∧ c < ε := let ⟨a, a0, aε⟩ := exists_between (pos_iff_ne_zero.2 hε) in let ⟨ε', hε', c, hc, hcε⟩ := pos_sum_of_encodable a0 ι in ⟨ λi, ⟨ε' i, le_of_lt $ hε' i⟩, assume i, nnreal.coe_lt_coe.1 $ hε' i, ⟨c, has_sum_le (assume i, le_of_lt $ hε' i) has_sum_zero hc ⟩, nnreal.has_sum_coe.1 hc, lt_of_le_of_lt (nnreal.coe_le_coe.1 hcε) aε ⟩ end nnreal namespace ennreal theorem exists_pos_sum_of_encodable {ε : ℝ≥0∞} (hε : ε ≠ 0) (ι) [encodable ι] : ∃ ε' : ι → ℝ≥0, (∀ i, 0 < ε' i) ∧ ∑' i, (ε' i : ℝ≥0∞) < ε := begin rcases exists_between (pos_iff_ne_zero.2 hε) with ⟨r, h0r, hrε⟩, rcases lt_iff_exists_coe.1 hrε with ⟨x, rfl, hx⟩, rcases nnreal.exists_pos_sum_of_encodable (coe_pos.1 h0r).ne' ι with ⟨ε', hp, c, hc, hcr⟩, exact ⟨ε', hp, (ennreal.tsum_coe_eq hc).symm ▸ lt_trans (coe_lt_coe.2 hcr) hrε⟩ end theorem exists_pos_sum_of_encodable' {ε : ℝ≥0∞} (hε : ε ≠ 0) (ι) [encodable ι] : ∃ ε' : ι → ℝ≥0∞, (∀ i, 0 < ε' i) ∧ (∑' i, ε' i) < ε := let ⟨δ, δpos, hδ⟩ := exists_pos_sum_of_encodable hε ι in ⟨λ i, δ i, λ i, ennreal.coe_pos.2 (δpos i), hδ⟩ theorem exists_pos_tsum_mul_lt_of_encodable {ε : ℝ≥0∞} (hε : ε ≠ 0) {ι} [encodable ι] (w : ι → ℝ≥0∞) (hw : ∀ i, w i ≠ ∞) : ∃ δ : ι → ℝ≥0, (∀ i, 0 < δ i) ∧ ∑' i, (w i * δ i : ℝ≥0∞) < ε := begin lift w to ι → ℝ≥0 using hw, rcases exists_pos_sum_of_encodable hε ι with ⟨δ', Hpos, Hsum⟩, have : ∀ i, 0 < max 1 (w i), from λ i, zero_lt_one.trans_le (le_max_left _ _), refine ⟨λ i, δ' i / max 1 (w i), λ i, nnreal.div_pos (Hpos _) (this i), _⟩, refine lt_of_le_of_lt (ennreal.tsum_le_tsum $ λ i, _) Hsum, rw [coe_div (this i).ne'], refine mul_le_of_le_div' (ennreal.mul_le_mul le_rfl $ ennreal.inv_le_inv.2 _), exact coe_le_coe.2 (le_max_right _ _) end end ennreal /-! ### Factorial -/ lemma factorial_tendsto_at_top : tendsto nat.factorial at_top at_top := tendsto_at_top_at_top_of_monotone nat.monotone_factorial (λ n, ⟨n, n.self_le_factorial⟩) lemma tendsto_factorial_div_pow_self_at_top : tendsto (λ n, n! / n^n : ℕ → ℝ) at_top (𝓝 0) := tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds (tendsto_const_div_at_top_nhds_0_nat 1) (eventually_of_forall $ λ n, div_nonneg (by exact_mod_cast n.factorial_pos.le) (pow_nonneg (by exact_mod_cast n.zero_le) _)) begin refine (eventually_gt_at_top 0).mono (λ n hn, _), rcases nat.exists_eq_succ_of_ne_zero hn.ne.symm with ⟨k, rfl⟩, rw [← prod_range_add_one_eq_factorial, pow_eq_prod_const, div_eq_mul_inv, ← inv_eq_one_div, prod_nat_cast, nat.cast_succ, ← prod_inv_distrib', ← prod_mul_distrib, finset.prod_range_succ'], simp only [prod_range_succ', one_mul, nat.cast_add, zero_add, nat.cast_one], refine mul_le_of_le_one_left (inv_nonneg.mpr $ by exact_mod_cast hn.le) (prod_le_one _ _); intros x hx; rw finset.mem_range at hx, { refine mul_nonneg _ (inv_nonneg.mpr _); norm_cast; linarith }, { refine (div_le_one $ by exact_mod_cast hn).mpr _, norm_cast, linarith } end /-- The series `∑' n, x ^ n / n!` is summable of any `x : ℝ`. See also `exp_series_field_summable` for a version that also works in `ℂ`, and `exp_series_summable'` for a version that works in any normed algebra over `ℝ` or `ℂ`. -/ lemma real.summable_pow_div_factorial (x : ℝ) : summable (λ n, x ^ n / n! : ℕ → ℝ) := begin -- We start with trivial extimates have A : (0 : ℝ) < ⌊∥x∥⌋₊ + 1, from zero_lt_one.trans_le (by simp), have B : ∥x∥ / (⌊∥x∥⌋₊ + 1) < 1, from (div_lt_one A).2 (nat.lt_floor_add_one _), -- Then we apply the ratio test. The estimate works for `n ≥ ⌊∥x∥⌋₊`. suffices : ∀ n ≥ ⌊∥x∥⌋₊, ∥x ^ (n + 1) / (n + 1)!∥ ≤ ∥x∥ / (⌊∥x∥⌋₊ + 1) * ∥x ^ n / ↑n!∥, from summable_of_ratio_norm_eventually_le B (eventually_at_top.2 ⟨⌊∥x∥⌋₊, this⟩), -- Finally, we prove the upper estimate intros n hn, calc ∥x ^ (n + 1) / (n + 1)!∥ = (∥x∥ / (n + 1)) * ∥x ^ n / n!∥ : by rw [pow_succ, nat.factorial_succ, nat.cast_mul, ← div_mul_div, normed_field.norm_mul, normed_field.norm_div, real.norm_coe_nat, nat.cast_succ] ... ≤ (∥x∥ / (⌊∥x∥⌋₊ + 1)) * ∥x ^ n / n!∥ : by mono* with [0 ≤ ∥x ^ n / n!∥, 0 ≤ ∥x∥]; apply norm_nonneg end lemma real.tendsto_pow_div_factorial_at_top (x : ℝ) : tendsto (λ n, x ^ n / n! : ℕ → ℝ) at_top (𝓝 0) := (real.summable_pow_div_factorial x).tendsto_at_top_zero /-! ### Ceil and floor -/ section variables {R : Type*} [topological_space R] [linear_ordered_field R] [order_topology R] [floor_ring R] lemma tendsto_nat_floor_mul_div_at_top {a : R} (ha : 0 ≤ a) : tendsto (λ x, (⌊a * x⌋₊ : R) / x) at_top (𝓝 a) := begin have A : tendsto (λ (x : R), a - x⁻¹) at_top (𝓝 (a - 0)) := tendsto_const_nhds.sub tendsto_inv_at_top_zero, rw sub_zero at A, apply tendsto_of_tendsto_of_tendsto_of_le_of_le' A tendsto_const_nhds, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, simp only [le_div_iff (zero_lt_one.trans_le hx), sub_mul, inv_mul_cancel (zero_lt_one.trans_le hx).ne'], have := nat.lt_floor_add_one (a * x), linarith }, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, rw div_le_iff (zero_lt_one.trans_le hx), simp [nat.floor_le (mul_nonneg ha (zero_le_one.trans hx))] } end lemma tendsto_nat_ceil_mul_div_at_top {a : R} (ha : 0 ≤ a) : tendsto (λ x, (⌈a * x⌉₊ : R) / x) at_top (𝓝 a) := begin have A : tendsto (λ (x : R), a + x⁻¹) at_top (𝓝 (a + 0)) := tendsto_const_nhds.add tendsto_inv_at_top_zero, rw add_zero at A, apply tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds A, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, rw le_div_iff (zero_lt_one.trans_le hx), exact nat.le_ceil _ }, { refine eventually_at_top.2 ⟨1, λ x hx, _⟩, simp [div_le_iff (zero_lt_one.trans_le hx), inv_mul_cancel (zero_lt_one.trans_le hx).ne', (nat.ceil_lt_add_one ((mul_nonneg ha (zero_le_one.trans hx)))).le, add_mul] } end end
c011191607bccab8f6e82714f31b9c2aa0ceb686
8930e38ac0fae2e5e55c28d0577a8e44e2639a6d
/order/lattice.lean
d942ec5b7d27b645f8357880c7a7847d245ed7ee
[ "Apache-2.0" ]
permissive
SG4316/mathlib
3d64035d02a97f8556ad9ff249a81a0a51a3321a
a7846022507b531a8ab53b8af8a91953fceafd3a
refs/heads/master
1,584,869,960,527
1,530,718,645,000
1,530,724,110,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,956
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 Defines the inf/sup (semi)-lattice with optionally top/bot type class hierarchy. -/ import order.basic set_option old_structure_cmd true universes u v w -- TODO: move this eventually, if we decide to use them attribute [ematch] le_trans lt_of_le_of_lt lt_of_lt_of_le lt_trans section variable {α : Type u} -- TODO: this seems crazy, but it also seems to work reasonably well @[ematch] theorem le_antisymm' [partial_order α] : ∀ {a b : α}, (: a ≤ b :) → b ≤ a → a = b := @le_antisymm _ _ end /- TODO: automatic construction of dual definitions / theorems -/ namespace lattice reserve infixl ` ⊓ `:70 reserve infixl ` ⊔ `:65 /-- Typeclass for the `⊔` (`\lub`) notation -/ class has_sup (α : Type u) := (sup : α → α → α) /-- Typeclass for the `⊓` (`\glb`) notation -/ class has_inf (α : Type u) := (inf : α → α → α) infix ⊔ := has_sup.sup infix ⊓ := has_inf.inf /-- A `semilattice_sup` is a join-semilattice, that is, a partial order with a join (a.k.a. lub / least upper bound, sup / supremum) operation `⊔` which is the least element larger than both factors. -/ class semilattice_sup (α : Type u) extends has_sup α, partial_order α := (le_sup_left : ∀ a b : α, a ≤ a ⊔ b) (le_sup_right : ∀ a b : α, b ≤ a ⊔ b) (sup_le : ∀ a b c : α, a ≤ c → b ≤ c → a ⊔ b ≤ c) section semilattice_sup variables {α : Type u} [semilattice_sup α] {a b c d : α} @[simp] theorem le_sup_left : a ≤ a ⊔ b := semilattice_sup.le_sup_left a b @[ematch] theorem le_sup_left' : a ≤ (: a ⊔ b :) := semilattice_sup.le_sup_left a b @[simp] theorem le_sup_right : b ≤ a ⊔ b := semilattice_sup.le_sup_right a b @[ematch] theorem le_sup_right' : b ≤ (: a ⊔ b :) := semilattice_sup.le_sup_right a b theorem le_sup_left_of_le (h : c ≤ a) : c ≤ a ⊔ b := by finish theorem le_sup_right_of_le (h : c ≤ b) : c ≤ a ⊔ b := by finish theorem sup_le : a ≤ c → b ≤ c → a ⊔ b ≤ c := semilattice_sup.sup_le a b c @[simp] theorem sup_le_iff : a ⊔ b ≤ c ↔ a ≤ c ∧ b ≤ c := ⟨assume h : a ⊔ b ≤ c, ⟨le_trans le_sup_left h, le_trans le_sup_right h⟩, assume ⟨h₁, h₂⟩, sup_le h₁ h₂⟩ -- TODO: if we just write le_antisymm, Lean doesn't know which ≤ we want to use -- Can we do anything about that? theorem sup_of_le_left (h : b ≤ a) : a ⊔ b = a := by apply le_antisymm; finish theorem sup_of_le_right (h : a ≤ b) : a ⊔ b = b := by apply le_antisymm; finish theorem sup_le_sup (h₁ : a ≤ b) (h₂ : c ≤ d) : a ⊔ c ≤ b ⊔ d := by finish theorem sup_le_sup_left (h₁ : a ≤ b) (c) : c ⊔ a ≤ c ⊔ b := by finish theorem sup_le_sup_right (h₁ : a ≤ b) (c) : a ⊔ c ≤ b ⊔ c := by finish theorem le_of_sup_eq (h : a ⊔ b = b) : a ≤ b := by finish @[simp] theorem sup_idem : a ⊔ a = a := by apply le_antisymm; finish instance sup_is_idempotent : is_idempotent α (⊔) := ⟨@sup_idem _ _⟩ theorem sup_comm : a ⊔ b = b ⊔ a := by apply le_antisymm; finish instance sup_is_commutative : is_commutative α (⊔) := ⟨@sup_comm _ _⟩ theorem sup_assoc : a ⊔ b ⊔ c = a ⊔ (b ⊔ c) := by apply le_antisymm; finish instance sup_is_associative : is_associative α (⊔) := ⟨@sup_assoc _ _⟩ lemma forall_le_or_exists_lt_sup (a : α) : (∀b, b ≤ a) ∨ (∃b, a < b) := suffices (∃b, ¬b ≤ a) → (∃b, a < b), by rwa [classical.or_iff_not_imp_left, classical.not_forall], assume ⟨b, hb⟩, have a ≠ a ⊔ b, from assume eq, hb $ eq.symm ▸ le_sup_right, ⟨a ⊔ b, lt_of_le_of_ne le_sup_left ‹a ≠ a ⊔ b›⟩ end semilattice_sup /-- A `semilattice_inf` is a meet-semilattice, that is, a partial order with a meet (a.k.a. glb / greatest lower bound, inf / infimum) operation `⊓` which is the greatest element smaller than both factors. -/ class semilattice_inf (α : Type u) extends has_inf α, partial_order α := (inf_le_left : ∀ a b : α, a ⊓ b ≤ a) (inf_le_right : ∀ a b : α, a ⊓ b ≤ b) (le_inf : ∀ a b c : α, a ≤ b → a ≤ c → a ≤ b ⊓ c) section semilattice_inf variables {α : Type u} [semilattice_inf α] {a b c d : α} @[simp] theorem inf_le_left : a ⊓ b ≤ a := semilattice_inf.inf_le_left a b @[ematch] theorem inf_le_left' : (: a ⊓ b :) ≤ a := semilattice_inf.inf_le_left a b @[simp] theorem inf_le_right : a ⊓ b ≤ b := semilattice_inf.inf_le_right a b @[ematch] theorem inf_le_right' : (: a ⊓ b :) ≤ b := semilattice_inf.inf_le_right a b theorem le_inf : a ≤ b → a ≤ c → a ≤ b ⊓ c := semilattice_inf.le_inf a b c theorem inf_le_left_of_le (h : a ≤ c) : a ⊓ b ≤ c := le_trans inf_le_left h theorem inf_le_right_of_le (h : b ≤ c) : a ⊓ b ≤ c := le_trans inf_le_right h @[simp] theorem le_inf_iff : a ≤ b ⊓ c ↔ a ≤ b ∧ a ≤ c := ⟨assume h : a ≤ b ⊓ c, ⟨le_trans h inf_le_left, le_trans h inf_le_right⟩, assume ⟨h₁, h₂⟩, le_inf h₁ h₂⟩ theorem inf_of_le_left (h : a ≤ b) : a ⊓ b = a := by apply le_antisymm; finish theorem inf_of_le_right (h : b ≤ a) : a ⊓ b = b := by apply le_antisymm; finish theorem inf_le_inf (h₁ : a ≤ b) (h₂ : c ≤ d) : a ⊓ c ≤ b ⊓ d := by finish theorem le_of_inf_eq (h : a ⊓ b = a) : a ≤ b := by finish @[simp] theorem inf_idem : a ⊓ a = a := by apply le_antisymm; finish instance inf_is_idempotent : is_idempotent α (⊓) := ⟨@inf_idem _ _⟩ theorem inf_comm : a ⊓ b = b ⊓ a := by apply le_antisymm; finish instance inf_is_commutative : is_commutative α (⊓) := ⟨@inf_comm _ _⟩ theorem inf_assoc : a ⊓ b ⊓ c = a ⊓ (b ⊓ c) := by apply le_antisymm; finish instance inf_is_associative : is_associative α (⊓) := ⟨@inf_assoc _ _⟩ lemma forall_le_or_exists_lt_inf (a : α) : (∀b, a ≤ b) ∨ (∃b, b < a) := suffices (∃b, ¬a ≤ b) → (∃b, b < a), by rwa [classical.or_iff_not_imp_left, classical.not_forall], assume ⟨b, hb⟩, have a ⊓ b ≠ a, from assume eq, hb $ eq ▸ inf_le_right, ⟨a ⊓ b, lt_of_le_of_ne inf_le_left ‹a ⊓ b ≠ a›⟩ end semilattice_inf /- Lattices -/ /-- A lattice is a join-semilattice which is also a meet-semilattice. -/ class lattice (α : Type u) extends semilattice_sup α, semilattice_inf α section lattice variables {α : Type u} [lattice α] {a b c d : α} /- Distributivity laws -/ /- TODO: better names? -/ theorem sup_inf_le : a ⊔ (b ⊓ c) ≤ (a ⊔ b) ⊓ (a ⊔ c) := by finish theorem le_inf_sup : (a ⊓ b) ⊔ (a ⊓ c) ≤ a ⊓ (b ⊔ c) := by finish theorem inf_sup_self : a ⊓ (a ⊔ b) = a := le_antisymm (by finish) (by finish) theorem sup_inf_self : a ⊔ (a ⊓ b) = a := le_antisymm (by finish) (by finish) end lattice variables {α : Type u} {x y z w : α} /-- A distributive lattice is a lattice that satisfies any of four equivalent distribution properties (of sup over inf or inf over sup, on the left or right). A classic example of a distributive lattice is the lattice of subsets of a set, and in fact this example is generic in the sense that every distributive lattice is realizable as a sublattice of a powerset lattice. -/ class distrib_lattice α extends lattice α := (le_sup_inf : ∀x y z : α, (x ⊔ y) ⊓ (x ⊔ z) ≤ x ⊔ (y ⊓ z)) section distrib_lattice variables [distrib_lattice α] theorem le_sup_inf : ∀{x y z : α}, (x ⊔ y) ⊓ (x ⊔ z) ≤ x ⊔ (y ⊓ z) := distrib_lattice.le_sup_inf theorem sup_inf_left : x ⊔ (y ⊓ z) = (x ⊔ y) ⊓ (x ⊔ z) := le_antisymm sup_inf_le le_sup_inf theorem sup_inf_right : (y ⊓ z) ⊔ x = (y ⊔ x) ⊓ (z ⊔ x) := by simp [sup_inf_left, λy:α, @sup_comm α _ y x] theorem inf_sup_left : x ⊓ (y ⊔ z) = (x ⊓ y) ⊔ (x ⊓ z) := calc x ⊓ (y ⊔ z) = (x ⊓ (x ⊔ z)) ⊓ (y ⊔ z) : by rw [inf_sup_self] ... = x ⊓ ((x ⊓ y) ⊔ z) : by simp [inf_assoc, sup_inf_right] ... = (x ⊔ (x ⊓ y)) ⊓ ((x ⊓ y) ⊔ z) : by rw [sup_inf_self] ... = ((x ⊓ y) ⊔ x) ⊓ ((x ⊓ y) ⊔ z) : by rw [sup_comm] ... = (x ⊓ y) ⊔ (x ⊓ z) : by rw [sup_inf_left] theorem inf_sup_right : (y ⊔ z) ⊓ x = (y ⊓ x) ⊔ (z ⊓ x) := by simp [inf_sup_left, λy:α, @inf_comm α _ y x] lemma eq_of_sup_eq_inf_eq {α : Type u} [distrib_lattice α] {a b c : α} (h₁ : b ⊓ a = c ⊓ a) (h₂ : b ⊔ a = c ⊔ a) : b = c := le_antisymm (calc b ≤ (c ⊓ a) ⊔ b : le_sup_right ... = (c ⊔ b) ⊓ (a ⊔ b) : sup_inf_right ... = c ⊔ (c ⊓ a) : by rw [←h₁, sup_inf_left, ←h₂]; simp [sup_comm] ... = c : sup_inf_self) (calc c ≤ (b ⊓ a) ⊔ c : le_sup_right ... = (b ⊔ c) ⊓ (a ⊔ c) : sup_inf_right ... = b ⊔ (b ⊓ a) : by rw [h₁, sup_inf_left, h₂]; simp [sup_comm] ... = b : sup_inf_self) end distrib_lattice /- Lattices derived from linear orders -/ instance lattice_of_decidable_linear_order {α : Type u} [o : decidable_linear_order α] : lattice α := { sup := max, le_sup_left := le_max_left, le_sup_right := le_max_right, sup_le := assume a b c, max_le, inf := min, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := assume a b c, le_min, ..o } instance distrib_lattice_of_decidable_linear_order {α : Type u} [o : decidable_linear_order α] : distrib_lattice α := { le_sup_inf := assume a b c, match le_total b c with | or.inl h := inf_le_left_of_le $ sup_le_sup_left (le_inf (le_refl b) h) _ | or.inr h := inf_le_right_of_le $ sup_le_sup_left (le_inf h (le_refl c)) _ end, ..lattice.lattice_of_decidable_linear_order } instance nat.distrib_lattice : distrib_lattice ℕ := by apply_instance end lattice