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
faa8a35aeb3b1dbb13bdd3ea1380ed5037afb042
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/normed_space/exponential.lean
d8dcf870514384d8deb5bba4ba89327281561e08
[ "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
26,253
lean
/- Copyright (c) 2021 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker, Eric Wieser -/ import analysis.specific_limits.basic import analysis.analytic.basic import analysis.complex.basic import data.nat.choose.cast import data.finset.noncomm_prod /-! # Exponential in a Banach algebra In this file, we define `exp 𝕂 : 𝔸 → 𝔸`, the exponential map in a topological algebra `𝔸` over a field `𝕂`. While for most interesting results we need `𝔸` to be normed algebra, we do not require this in the definition in order to make `exp` independent of a particular choice of norm. The definition also does not require that `𝔸` be complete, but we need to assume it for most results. We then prove some basic results, but we avoid importing derivatives here to minimize dependencies. Results involving derivatives and comparisons with `real.exp` and `complex.exp` can be found in `analysis/special_functions/exponential`. ## Main results We prove most result for an arbitrary field `𝕂`, and then specialize to `𝕂 = ℝ` or `𝕂 = ℂ`. ### General case - `exp_add_of_commute_of_mem_ball` : if `𝕂` has characteristic zero, then given two commuting elements `x` and `y` in the disk of convergence, we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` - `exp_add_of_mem_ball` : if `𝕂` has characteristic zero and `𝔸` is commutative, then given two elements `x` and `y` in the disk of convergence, we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` - `exp_neg_of_mem_ball` : if `𝕂` has characteristic zero and `𝔸` is a division ring, then given an element `x` in the disk of convergence, we have `exp 𝕂 (-x) = (exp 𝕂 x)⁻¹`. ### `𝕂 = ℝ` or `𝕂 = ℂ` - `exp_series_radius_eq_top` : the `formal_multilinear_series` defining `exp 𝕂` has infinite radius of convergence - `exp_add_of_commute` : given two commuting elements `x` and `y`, we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` - `exp_add` : if `𝔸` is commutative, then we have `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` for any `x` and `y` - `exp_neg` : if `𝔸` is a division ring, then we have `exp 𝕂 (-x) = (exp 𝕂 x)⁻¹`. - `exp_sum_of_commute` : the analogous result to `exp_add_of_commute` for `finset.sum`. - `exp_sum` : the analogous result to `exp_add` for `finset.sum`. - `exp_nsmul` : repeated addition in the domain corresponds to repeated multiplication in the codomain. - `exp_zsmul` : repeated addition in the domain corresponds to repeated multiplication in the codomain. ### Other useful compatibility results - `exp_eq_exp` : if `𝔸` is a normed algebra over two fields `𝕂` and `𝕂'`, then `exp 𝕂 = exp 𝕂' 𝔸` -/ open filter is_R_or_C continuous_multilinear_map normed_field asymptotics open_locale nat topological_space big_operators ennreal section topological_algebra variables (𝕂 𝔸 : Type*) [field 𝕂] [ring 𝔸] [algebra 𝕂 𝔸] [topological_space 𝔸] [topological_ring 𝔸] /-- `exp_series 𝕂 𝔸` is the `formal_multilinear_series` whose `n`-th term is the map `(xᵢ) : 𝔸ⁿ ↦ (1/n! : 𝕂) • ∏ xᵢ`. Its sum is the exponential map `exp 𝕂 : 𝔸 → 𝔸`. -/ def exp_series : formal_multilinear_series 𝕂 𝔸 𝔸 := λ n, (n!⁻¹ : 𝕂) • continuous_multilinear_map.mk_pi_algebra_fin 𝕂 n 𝔸 variables {𝔸} /-- `exp 𝕂 : 𝔸 → 𝔸` is the exponential map determined by the action of `𝕂` on `𝔸`. It is defined as the sum of the `formal_multilinear_series` `exp_series 𝕂 𝔸`. Note that when `𝔸 = matrix n n 𝕂`, this is the **Matrix Exponential**; see [`analysis.normed_space.matrix_exponential`](../matrix_exponential) for lemmas specific to that case. -/ noncomputable def exp (x : 𝔸) : 𝔸 := (exp_series 𝕂 𝔸).sum x variables {𝕂} lemma exp_series_apply_eq (x : 𝔸) (n : ℕ) : exp_series 𝕂 𝔸 n (λ _, x) = (n!⁻¹ : 𝕂) • x^n := by simp [exp_series] lemma exp_series_apply_eq' (x : 𝔸) : (λ n, exp_series 𝕂 𝔸 n (λ _, x)) = (λ n, (n!⁻¹ : 𝕂) • x^n) := funext (exp_series_apply_eq x) lemma exp_series_sum_eq (x : 𝔸) : (exp_series 𝕂 𝔸).sum x = ∑' (n : ℕ), (n!⁻¹ : 𝕂) • x^n := tsum_congr (λ n, exp_series_apply_eq x n) lemma exp_eq_tsum : exp 𝕂 = (λ x : 𝔸, ∑' (n : ℕ), (n!⁻¹ : 𝕂) • x^n) := funext exp_series_sum_eq @[simp] lemma exp_zero [t2_space 𝔸] : exp 𝕂 (0 : 𝔸) = 1 := begin suffices : (λ x : 𝔸, ∑' (n : ℕ), (n!⁻¹ : 𝕂) • x^n) 0 = ∑' (n : ℕ), if n = 0 then 1 else 0, { have key : ∀ n ∉ ({0} : finset ℕ), (if n = 0 then (1 : 𝔸) else 0) = 0, from λ n hn, if_neg (finset.not_mem_singleton.mp hn), rw [exp_eq_tsum, this, tsum_eq_sum key, finset.sum_singleton], simp }, refine tsum_congr (λ n, _), split_ifs with h h; simp [h] end @[simp] lemma exp_op [t2_space 𝔸] (x : 𝔸) : exp 𝕂 (mul_opposite.op x) = mul_opposite.op (exp 𝕂 x) := by simp_rw [exp, exp_series_sum_eq, ←mul_opposite.op_pow, ←mul_opposite.op_smul, tsum_op] @[simp] lemma exp_unop [t2_space 𝔸] (x : 𝔸ᵐᵒᵖ) : exp 𝕂 (mul_opposite.unop x) = mul_opposite.unop (exp 𝕂 x) := by simp_rw [exp, exp_series_sum_eq, ←mul_opposite.unop_pow, ←mul_opposite.unop_smul, tsum_unop] lemma star_exp [t2_space 𝔸] [star_ring 𝔸] [has_continuous_star 𝔸] (x : 𝔸) : star (exp 𝕂 x) = exp 𝕂 (star x) := by simp_rw [exp_eq_tsum, ←star_pow, ←star_inv_nat_cast_smul, ←tsum_star] variables (𝕂) lemma commute.exp_right [t2_space 𝔸] {x y : 𝔸} (h : commute x y) : commute x (exp 𝕂 y) := begin rw exp_eq_tsum, exact commute.tsum_right x (λ n, (h.pow_right n).smul_right _), end lemma commute.exp_left [t2_space 𝔸] {x y : 𝔸} (h : commute x y) : commute (exp 𝕂 x) y := (h.symm.exp_right 𝕂).symm lemma commute.exp [t2_space 𝔸] {x y : 𝔸} (h : commute x y) : commute (exp 𝕂 x) (exp 𝕂 y) := (h.exp_left _).exp_right _ end topological_algebra section topological_division_algebra variables {𝕂 𝔸 : Type*} [field 𝕂] [division_ring 𝔸] [algebra 𝕂 𝔸] [topological_space 𝔸] [topological_ring 𝔸] lemma exp_series_apply_eq_div (x : 𝔸) (n : ℕ) : exp_series 𝕂 𝔸 n (λ _, x) = x^n / n! := by rw [div_eq_mul_inv, ←(nat.cast_commute n! (x ^ n)).inv_left₀.eq, ←smul_eq_mul, exp_series_apply_eq, inv_nat_cast_smul_eq _ _ _ _] lemma exp_series_apply_eq_div' (x : 𝔸) : (λ n, exp_series 𝕂 𝔸 n (λ _, x)) = (λ n, x^n / n!) := funext (exp_series_apply_eq_div x) lemma exp_series_sum_eq_div (x : 𝔸) : (exp_series 𝕂 𝔸).sum x = ∑' (n : ℕ), x^n / n! := tsum_congr (exp_series_apply_eq_div x) lemma exp_eq_tsum_div : exp 𝕂 = (λ x : 𝔸, ∑' (n : ℕ), x^n / n!) := funext exp_series_sum_eq_div end topological_division_algebra section normed section any_field_any_algebra variables {𝕂 𝔸 𝔹 : Type*} [nondiscrete_normed_field 𝕂] variables [normed_ring 𝔸] [normed_ring 𝔹] [normed_algebra 𝕂 𝔸] [normed_algebra 𝕂 𝔹] lemma norm_exp_series_summable_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, ∥exp_series 𝕂 𝔸 n (λ _, x)∥) := (exp_series 𝕂 𝔸).summable_norm_apply hx lemma norm_exp_series_summable_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, ∥(n!⁻¹ : 𝕂) • x^n∥) := begin change summable (norm ∘ _), rw ← exp_series_apply_eq', exact norm_exp_series_summable_of_mem_ball x hx end section complete_algebra variables [complete_space 𝔸] lemma exp_series_summable_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, exp_series 𝕂 𝔸 n (λ _, x)) := summable_of_summable_norm (norm_exp_series_summable_of_mem_ball x hx) lemma exp_series_summable_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, (n!⁻¹ : 𝕂) • x^n) := summable_of_summable_norm (norm_exp_series_summable_of_mem_ball' x hx) lemma exp_series_has_sum_exp_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_sum (λ n, exp_series 𝕂 𝔸 n (λ _, x)) (exp 𝕂 x) := formal_multilinear_series.has_sum (exp_series 𝕂 𝔸) hx lemma exp_series_has_sum_exp_of_mem_ball' (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_sum (λ n, (n!⁻¹ : 𝕂) • x^n) (exp 𝕂 x):= begin rw ← exp_series_apply_eq', exact exp_series_has_sum_exp_of_mem_ball x hx end lemma has_fpower_series_on_ball_exp_of_radius_pos (h : 0 < (exp_series 𝕂 𝔸).radius) : has_fpower_series_on_ball (exp 𝕂) (exp_series 𝕂 𝔸) 0 (exp_series 𝕂 𝔸).radius := (exp_series 𝕂 𝔸).has_fpower_series_on_ball h lemma has_fpower_series_at_exp_zero_of_radius_pos (h : 0 < (exp_series 𝕂 𝔸).radius) : has_fpower_series_at (exp 𝕂) (exp_series 𝕂 𝔸) 0 := (has_fpower_series_on_ball_exp_of_radius_pos h).has_fpower_series_at lemma continuous_on_exp : continuous_on (exp 𝕂 : 𝔸 → 𝔸) (emetric.ball 0 (exp_series 𝕂 𝔸).radius) := formal_multilinear_series.continuous_on lemma analytic_at_exp_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : analytic_at 𝕂 (exp 𝕂) x:= begin by_cases h : (exp_series 𝕂 𝔸).radius = 0, { rw h at hx, exact (ennreal.not_lt_zero hx).elim }, { have h := pos_iff_ne_zero.mpr h, exact (has_fpower_series_on_ball_exp_of_radius_pos h).analytic_at_of_mem hx } end /-- In a Banach-algebra `𝔸` over a normed field `𝕂` of characteristic zero, if `x` and `y` are in the disk of convergence and commute, then `exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y)`. -/ lemma exp_add_of_commute_of_mem_ball [char_zero 𝕂] {x y : 𝔸} (hxy : commute x y) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) (hy : y ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := begin rw [exp_eq_tsum, tsum_mul_tsum_eq_tsum_sum_antidiagonal_of_summable_norm (norm_exp_series_summable_of_mem_ball' x hx) (norm_exp_series_summable_of_mem_ball' y hy)], dsimp only, conv_lhs {congr, funext, rw [hxy.add_pow' _, finset.smul_sum]}, refine tsum_congr (λ n, finset.sum_congr rfl $ λ kl hkl, _), rw [nsmul_eq_smul_cast 𝕂, smul_smul, smul_mul_smul, ← (finset.nat.mem_antidiagonal.mp hkl), nat.cast_add_choose, (finset.nat.mem_antidiagonal.mp hkl)], congr' 1, have : (n! : 𝕂) ≠ 0 := nat.cast_ne_zero.mpr n.factorial_ne_zero, field_simp [this] end /-- `exp 𝕂 x` has explicit two-sided inverse `exp 𝕂 (-x)`. -/ noncomputable def invertible_exp_of_mem_ball [char_zero 𝕂] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : invertible (exp 𝕂 x) := { inv_of := exp 𝕂 (-x), inv_of_mul_self := begin have hnx : -x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius, { rw [emetric.mem_ball, ←neg_zero, edist_neg_neg], exact hx }, rw [←exp_add_of_commute_of_mem_ball (commute.neg_left $ commute.refl x) hnx hx, neg_add_self, exp_zero], end, mul_inv_of_self := begin have hnx : -x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius, { rw [emetric.mem_ball, ←neg_zero, edist_neg_neg], exact hx }, rw [←exp_add_of_commute_of_mem_ball (commute.neg_right $ commute.refl x) hx hnx, add_neg_self, exp_zero], end } lemma is_unit_exp_of_mem_ball [char_zero 𝕂] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : is_unit (exp 𝕂 x) := @is_unit_of_invertible _ _ _ (invertible_exp_of_mem_ball hx) lemma inv_of_exp_of_mem_ball [char_zero 𝕂] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) [invertible (exp 𝕂 x)] : ⅟(exp 𝕂 x) = exp 𝕂 (-x) := by { letI := invertible_exp_of_mem_ball hx, convert (rfl : ⅟(exp 𝕂 x) = _) } /-- Any continuous ring homomorphism commutes with `exp`. -/ lemma map_exp_of_mem_ball {F} [ring_hom_class F 𝔸 𝔹] (f : F) (hf : continuous f) (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : f (exp 𝕂 x) = exp 𝕂 (f x) := begin rw [exp_eq_tsum, exp_eq_tsum], refine ((exp_series_summable_of_mem_ball' _ hx).has_sum.map f hf).tsum_eq.symm.trans _, dsimp only [function.comp], simp_rw [one_div, map_inv_nat_cast_smul f 𝕂 𝕂, map_pow], end end complete_algebra lemma algebra_map_exp_comm_of_mem_ball [complete_space 𝕂] (x : 𝕂) (hx : x ∈ emetric.ball (0 : 𝕂) (exp_series 𝕂 𝕂).radius) : algebra_map 𝕂 𝔸 (exp 𝕂 x) = exp 𝕂 (algebra_map 𝕂 𝔸 x) := map_exp_of_mem_ball _ (algebra_map_clm _ _).continuous _ hx end any_field_any_algebra section any_field_division_algebra variables {𝕂 𝔸 : Type*} [nondiscrete_normed_field 𝕂] [normed_division_ring 𝔸] [normed_algebra 𝕂 𝔸] variables (𝕂) lemma norm_exp_series_div_summable_of_mem_ball (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, ∥x^n / n!∥) := begin change summable (norm ∘ _), rw ← exp_series_apply_eq_div' x, exact norm_exp_series_summable_of_mem_ball x hx end lemma exp_series_div_summable_of_mem_ball [complete_space 𝔸] (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : summable (λ n, x^n / n!) := summable_of_summable_norm (norm_exp_series_div_summable_of_mem_ball 𝕂 x hx) lemma exp_series_div_has_sum_exp_of_mem_ball [complete_space 𝔸] (x : 𝔸) (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : has_sum (λ n, x^n / n!) (exp 𝕂 x) := begin rw ← exp_series_apply_eq_div' x, exact exp_series_has_sum_exp_of_mem_ball x hx end variables {𝕂} lemma exp_neg_of_mem_ball [char_zero 𝕂] [complete_space 𝔸] {x : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : exp 𝕂 (-x) = (exp 𝕂 x)⁻¹ := begin letI := invertible_exp_of_mem_ball hx, exact inv_of_eq_inv (exp 𝕂 x), end end any_field_division_algebra section any_field_comm_algebra variables {𝕂 𝔸 : Type*} [nondiscrete_normed_field 𝕂] [normed_comm_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] /-- In a commutative Banach-algebra `𝔸` over a normed field `𝕂` of characteristic zero, `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)` for all `x`, `y` in the disk of convergence. -/ lemma exp_add_of_mem_ball [char_zero 𝕂] {x y : 𝔸} (hx : x ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) (hy : y ∈ emetric.ball (0 : 𝔸) (exp_series 𝕂 𝔸).radius) : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := exp_add_of_commute_of_mem_ball (commute.all x y) hx hy end any_field_comm_algebra section is_R_or_C section any_algebra variables (𝕂 𝔸 𝔹 : Type*) [is_R_or_C 𝕂] [normed_ring 𝔸] [normed_algebra 𝕂 𝔸] variables [normed_ring 𝔹] [normed_algebra 𝕂 𝔹] /-- In a normed algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, the series defining the exponential map has an infinite radius of convergence. -/ lemma exp_series_radius_eq_top : (exp_series 𝕂 𝔸).radius = ∞ := begin refine (exp_series 𝕂 𝔸).radius_eq_top_of_summable_norm (λ r, _), refine summable_of_norm_bounded_eventually _ (real.summable_pow_div_factorial r) _, filter_upwards [eventually_cofinite_ne 0] with n hn, rw [norm_mul, norm_norm (exp_series 𝕂 𝔸 n), exp_series, norm_smul, norm_inv, norm_pow, nnreal.norm_eq, norm_eq_abs, abs_cast_nat, mul_comm, ←mul_assoc, ←div_eq_mul_inv], have : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕂 n 𝔸∥ ≤ 1 := norm_mk_pi_algebra_fin_le_of_pos (nat.pos_of_ne_zero hn), exact mul_le_of_le_one_right (div_nonneg (pow_nonneg r.coe_nonneg n) n!.cast_nonneg) this end lemma exp_series_radius_pos : 0 < (exp_series 𝕂 𝔸).radius := begin rw exp_series_radius_eq_top, exact with_top.zero_lt_top end variables {𝕂 𝔸 𝔹} lemma norm_exp_series_summable (x : 𝔸) : summable (λ n, ∥exp_series 𝕂 𝔸 n (λ _, x)∥) := norm_exp_series_summable_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma norm_exp_series_summable' (x : 𝔸) : summable (λ n, ∥(n!⁻¹ : 𝕂) • x^n∥) := norm_exp_series_summable_of_mem_ball' x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) section complete_algebra variables [complete_space 𝔸] lemma exp_series_summable (x : 𝔸) : summable (λ n, exp_series 𝕂 𝔸 n (λ _, x)) := summable_of_summable_norm (norm_exp_series_summable x) lemma exp_series_summable' (x : 𝔸) : summable (λ n, (n!⁻¹ : 𝕂) • x^n) := summable_of_summable_norm (norm_exp_series_summable' x) lemma exp_series_has_sum_exp (x : 𝔸) : has_sum (λ n, exp_series 𝕂 𝔸 n (λ _, x)) (exp 𝕂 x) := exp_series_has_sum_exp_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma exp_series_has_sum_exp' (x : 𝔸) : has_sum (λ n, (n!⁻¹ : 𝕂) • x^n) (exp 𝕂 x):= exp_series_has_sum_exp_of_mem_ball' x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) lemma exp_has_fpower_series_on_ball : has_fpower_series_on_ball (exp 𝕂) (exp_series 𝕂 𝔸) 0 ∞ := exp_series_radius_eq_top 𝕂 𝔸 ▸ has_fpower_series_on_ball_exp_of_radius_pos (exp_series_radius_pos _ _) lemma exp_has_fpower_series_at_zero : has_fpower_series_at (exp 𝕂) (exp_series 𝕂 𝔸) 0 := exp_has_fpower_series_on_ball.has_fpower_series_at lemma exp_continuous : continuous (exp 𝕂 : 𝔸 → 𝔸) := begin rw [continuous_iff_continuous_on_univ, ← metric.eball_top_eq_univ (0 : 𝔸), ← exp_series_radius_eq_top 𝕂 𝔸], exact continuous_on_exp end lemma exp_analytic (x : 𝔸) : analytic_at 𝕂 (exp 𝕂) x := analytic_at_exp_of_mem_ball x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) /-- In a Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, if `x` and `y` commute, then `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)`. -/ lemma exp_add_of_commute {x y : 𝔸} (hxy : commute x y) : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := exp_add_of_commute_of_mem_ball hxy ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) section variables (𝕂) /-- `exp 𝕂 x` has explicit two-sided inverse `exp 𝕂 (-x)`. -/ noncomputable def invertible_exp (x : 𝔸) : invertible (exp 𝕂 x) := invertible_exp_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma is_unit_exp (x : 𝔸) : is_unit (exp 𝕂 x) := is_unit_exp_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma inv_of_exp (x : 𝔸) [invertible (exp 𝕂 x)] : ⅟(exp 𝕂 x) = exp 𝕂 (-x) := inv_of_exp_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma ring.inverse_exp (x : 𝔸) : ring.inverse (exp 𝕂 x) = exp 𝕂 (-x) := begin letI := invertible_exp 𝕂 x, exact ring.inverse_invertible _, end end /-- In a Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, if a family of elements `f i` mutually commute then `exp 𝕂 (∑ i, f i) = ∏ i, exp 𝕂 (f i)`. -/ lemma exp_sum_of_commute {ι} (s : finset ι) (f : ι → 𝔸) (h : ∀ (i ∈ s) (j ∈ s), commute (f i) (f j)) : exp 𝕂 (∑ i in s, f i) = s.noncomm_prod (λ i, exp 𝕂 (f i)) (λ i hi j hj, (h i hi j hj).exp 𝕂) := begin classical, induction s using finset.induction_on with a s ha ih, { simp }, rw [finset.noncomm_prod_insert_of_not_mem _ _ _ _ ha, finset.sum_insert ha, exp_add_of_commute, ih], refine commute.sum_right _ _ _ _, intros i hi, exact h _ (finset.mem_insert_self _ _) _ (finset.mem_insert_of_mem hi), end lemma exp_nsmul (n : ℕ) (x : 𝔸) : exp 𝕂 (n • x) = exp 𝕂 x ^ n := begin induction n with n ih, { rw [zero_smul, pow_zero, exp_zero], }, { rw [succ_nsmul, pow_succ, exp_add_of_commute ((commute.refl x).smul_right n), ih] } end variables (𝕂) /-- Any continuous ring homomorphism commutes with `exp`. -/ lemma map_exp {F} [ring_hom_class F 𝔸 𝔹] (f : F) (hf : continuous f) (x : 𝔸) : f (exp 𝕂 x) = exp 𝕂 (f x) := map_exp_of_mem_ball f hf x $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma exp_smul {G} [monoid G] [mul_semiring_action G 𝔸] [has_continuous_const_smul G 𝔸] (g : G) (x : 𝔸) : exp 𝕂 (g • x) = g • exp 𝕂 x := (map_exp 𝕂 (mul_semiring_action.to_ring_hom G 𝔸 g) (continuous_const_smul _) x).symm lemma exp_units_conj (y : 𝔸ˣ) (x : 𝔸) : exp 𝕂 (y * x * ↑(y⁻¹) : 𝔸) = y * exp 𝕂 x * ↑(y⁻¹) := exp_smul _ (conj_act.to_conj_act y) x lemma exp_units_conj' (y : 𝔸ˣ) (x : 𝔸) : exp 𝕂 (↑(y⁻¹) * x * y) = ↑(y⁻¹) * exp 𝕂 x * y := exp_units_conj _ _ _ @[simp] lemma prod.fst_exp [complete_space 𝔹] (x : 𝔸 × 𝔹) : (exp 𝕂 x).fst = exp 𝕂 x.fst := map_exp _ (ring_hom.fst 𝔸 𝔹) continuous_fst x @[simp] lemma prod.snd_exp [complete_space 𝔹] (x : 𝔸 × 𝔹) : (exp 𝕂 x).snd = exp 𝕂 x.snd := map_exp _ (ring_hom.snd 𝔸 𝔹) continuous_snd x @[simp] lemma pi.exp_apply {ι : Type*} {𝔸 : ι → Type*} [fintype ι] [Π i, normed_ring (𝔸 i)] [Π i, normed_algebra 𝕂 (𝔸 i)] [Π i, complete_space (𝔸 i)] (x : Π i, 𝔸 i) (i : ι) : exp 𝕂 x i = exp 𝕂 (x i) := begin -- Lean struggles to infer this instance due to it wanting `[Π i, semi_normed_ring (𝔸 i)]` letI : normed_algebra 𝕂 (Π i, 𝔸 i) := pi.normed_algebra _, exact map_exp _ (pi.eval_ring_hom 𝔸 i) (continuous_apply _) x end lemma pi.exp_def {ι : Type*} {𝔸 : ι → Type*} [fintype ι] [Π i, normed_ring (𝔸 i)] [Π i, normed_algebra 𝕂 (𝔸 i)] [Π i, complete_space (𝔸 i)] (x : Π i, 𝔸 i) : exp 𝕂 x = λ i, exp 𝕂 (x i) := funext $ pi.exp_apply 𝕂 x lemma function.update_exp {ι : Type*} {𝔸 : ι → Type*} [fintype ι] [decidable_eq ι] [Π i, normed_ring (𝔸 i)] [Π i, normed_algebra 𝕂 (𝔸 i)] [Π i, complete_space (𝔸 i)] (x : Π i, 𝔸 i) (j : ι) (xj : 𝔸 j) : function.update (exp 𝕂 x) j (exp 𝕂 xj) = exp 𝕂 (function.update x j xj) := begin ext i, simp_rw [pi.exp_def], exact (function.apply_update (λ i, exp 𝕂) x j xj i).symm, end end complete_algebra lemma algebra_map_exp_comm (x : 𝕂) : algebra_map 𝕂 𝔸 (exp 𝕂 x) = exp 𝕂 (algebra_map 𝕂 𝔸 x) := algebra_map_exp_comm_of_mem_ball x $ (exp_series_radius_eq_top 𝕂 𝕂).symm ▸ edist_lt_top _ _ end any_algebra section division_algebra variables {𝕂 𝔸 : Type*} [is_R_or_C 𝕂] [normed_division_ring 𝔸] [normed_algebra 𝕂 𝔸] variables (𝕂) lemma norm_exp_series_div_summable (x : 𝔸) : summable (λ n, ∥x^n / n!∥) := norm_exp_series_div_summable_of_mem_ball 𝕂 x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) variables [complete_space 𝔸] lemma exp_series_div_summable (x : 𝔸) : summable (λ n, x^n / n!) := summable_of_summable_norm (norm_exp_series_div_summable 𝕂 x) lemma exp_series_div_has_sum_exp (x : 𝔸) : has_sum (λ n, x^n / n!) (exp 𝕂 x):= exp_series_div_has_sum_exp_of_mem_ball 𝕂 x ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) variables {𝕂} lemma exp_neg (x : 𝔸) : exp 𝕂 (-x) = (exp 𝕂 x)⁻¹ := exp_neg_of_mem_ball $ (exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _ lemma exp_zsmul (z : ℤ) (x : 𝔸) : exp 𝕂 (z • x) = (exp 𝕂 x) ^ z := begin obtain ⟨n, rfl | rfl⟩ := z.eq_coe_or_neg, { rw [zpow_coe_nat, coe_nat_zsmul, exp_nsmul] }, { rw [zpow_neg₀, zpow_coe_nat, neg_smul, exp_neg, coe_nat_zsmul, exp_nsmul] }, end lemma exp_conj (y : 𝔸) (x : 𝔸) (hy : y ≠ 0) : exp 𝕂 (y * x * y⁻¹) = y * exp 𝕂 x * y⁻¹ := exp_units_conj _ (units.mk0 y hy) x lemma exp_conj' (y : 𝔸) (x : 𝔸) (hy : y ≠ 0) : exp 𝕂 (y⁻¹ * x * y) = y⁻¹ * exp 𝕂 x * y := exp_units_conj' _ (units.mk0 y hy) x end division_algebra section comm_algebra variables {𝕂 𝔸 : Type*} [is_R_or_C 𝕂] [normed_comm_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] /-- In a commutative Banach-algebra `𝔸` over `𝕂 = ℝ` or `𝕂 = ℂ`, `exp 𝕂 (x+y) = (exp 𝕂 x) * (exp 𝕂 y)`. -/ lemma exp_add {x y : 𝔸} : exp 𝕂 (x + y) = (exp 𝕂 x) * (exp 𝕂 y) := exp_add_of_mem_ball ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) ((exp_series_radius_eq_top 𝕂 𝔸).symm ▸ edist_lt_top _ _) /-- A version of `exp_sum_of_commute` for a commutative Banach-algebra. -/ lemma exp_sum {ι} (s : finset ι) (f : ι → 𝔸) : exp 𝕂 (∑ i in s, f i) = ∏ i in s, exp 𝕂 (f i) := begin rw [exp_sum_of_commute, finset.noncomm_prod_eq_prod], exact λ i hi j hj, commute.all _ _, end end comm_algebra end is_R_or_C end normed section scalar_tower variables (𝕂 𝕂' 𝔸 : Type*) [field 𝕂] [field 𝕂'] [ring 𝔸] [algebra 𝕂 𝔸] [algebra 𝕂' 𝔸] [topological_space 𝔸] [topological_ring 𝔸] /-- If a normed ring `𝔸` is a normed algebra over two fields, then they define the same `exp_series` on `𝔸`. -/ lemma exp_series_eq_exp_series (n : ℕ) (x : 𝔸) : (exp_series 𝕂 𝔸 n (λ _, x)) = (exp_series 𝕂' 𝔸 n (λ _, x)) := by rw [exp_series_apply_eq, exp_series_apply_eq, inv_nat_cast_smul_eq 𝕂 𝕂'] /-- If a normed ring `𝔸` is a normed algebra over two fields, then they define the same exponential function on `𝔸`. -/ lemma exp_eq_exp : (exp 𝕂 : 𝔸 → 𝔸) = exp 𝕂' := begin ext, rw [exp, exp], refine tsum_congr (λ n, _), rw exp_series_eq_exp_series 𝕂 𝕂' 𝔸 n x end lemma exp_ℝ_ℂ_eq_exp_ℂ_ℂ : (exp ℝ : ℂ → ℂ) = exp ℂ := exp_eq_exp ℝ ℂ ℂ end scalar_tower
4d70ea66fc00adf548b672c637cc26c473f21809
82e44445c70db0f03e30d7be725775f122d72f3e
/src/tactic/rcases.lean
5a90c3cc1426080614e2d9f25b081f9180f4d853
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
39,269
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.dlist import tactic.core import tactic.clear /-! # Recursive cases (`rcases`) tactic and related tactics `rcases` is a tactic that will perform `cases` recursively, according to a pattern. It is used to destructure hypotheses or expressions composed of inductive types like `h1 : a ∧ b ∧ c ∨ d` or `h2 : ∃ x y, trans_rel R x y`. Usual usage might be `rcases h1 with ⟨ha, hb, hc⟩ | hd` or `rcases h2 with ⟨x, y, _ | ⟨z, hxz, hzy⟩⟩` for these examples. Each element of an `rcases` pattern is matched against a particular local hypothesis (most of which are generated during the execution of `rcases` and represent individual elements destructured from the input expression). An `rcases` pattern has the following grammar: * A name like `x`, which names the active hypothesis as `x`. * A blank `_`, which does nothing (letting the automatic naming system used by `cases` name the hypothesis). * A hyphen `-`, which clears the active hypothesis and any dependents. * The keyword `rfl`, which expects the hypothesis to be `h : a = b`, and calls `subst` on the hypothesis (which has the effect of replacing `b` with `a` everywhere or vice versa). * A type ascription `p : ty`, which sets the type of the hypothesis to `ty` and then matches it against `p`. (Of course, `ty` must unify with the actual type of `h` for this to work.) * A tuple pattern `⟨p1, p2, p3⟩`, which matches a constructor with many arguments, or a series of nested conjunctions or existentials. For example if the active hypothesis is `a ∧ b ∧ c`, then the conjunction will be destructured, and `p1` will be matched against `a`, `p2` against `b` and so on. * An alteration pattern `p1 | p2 | p3`, which matches an inductive type with multiple constructors, or a nested disjunction like `a ∨ b ∨ c`. The patterns are fairly liberal about the exact shape of the constructors, and will insert additional alternation branches and tuple arguments if there are not enough arguments provided, and reuse the tail for further matches if there are too many arguments provided to alternation and tuple patterns. This file also contains the `obtain` and `rintro` tactics, which use the same syntax of `rcases` patterns but with a slightly different use case: * `rintro` (or `rintros`) is used like `rintro x ⟨y, z⟩` and is the same as `intros` followed by `rcases` on the newly introduced arguments. * `obtain` is the same as `rcases` but with a syntax styled after `have` rather than `cases`. `obtain ⟨hx, hy⟩ | hz := foo` is equivalent to `rcases foo with ⟨hx, hy⟩ | hz`. Unlike `rcases`, `obtain` also allows one to omit `:= foo`, although a type must be provided in this case, as in `obtain ⟨hx, hy⟩ | hz : a ∧ b ∨ c`, in which case it produces a subgoal for proving `a ∧ b ∨ c` in addition to the subgoals `hx : a, hy : b |- goal` and `hz : c |- goal`. ## Tags rcases, rintro, obtain, destructuring, cases, pattern matching, match -/ open lean lean.parser namespace tactic /-! These synonyms for `list` are used to clarify the meanings of the many usages of lists in this module. - `listΣ` is used where a list represents a disjunction, such as the list of possible constructors of an inductive type. - `listΠ` is used where a list represents a conjunction, such as the list of arguments of an individual constructor. These are merely type synonyms, and so are not checked for consistency by the compiler. The `def`/`local notation` combination makes Lean retain these annotations in reported types. -/ /-- A list, with a disjunctive meaning (like a list of inductive constructors, or subgoals) -/ @[reducible] def list_Sigma := list /-- A list, with a conjunctive meaning (like a list of constructor arguments, or hypotheses) -/ @[reducible] def list_Pi := list local notation `listΣ` := list_Sigma local notation `listΠ` := list_Pi /-- A metavariable representing a subgoal, together with a list of local constants to clear. -/ @[reducible] meta def uncleared_goal := list expr × expr /-- An `rcases` pattern can be one of the following, in a nested combination: * A name like `foo` * The special keyword `rfl` (for pattern matching on equality using `subst`) * A hyphen `-`, which clears the active hypothesis and any dependents. * A type ascription like `pat : ty` (parentheses are optional) * A tuple constructor like `⟨p1, p2, p3⟩` * An alternation / variant pattern `p1 | p2 | p3` Parentheses can be used for grouping; alternation is higher precedence than type ascription, so `p1 | p2 | p3 : ty` means `(p1 | p2 | p3) : ty`. N-ary alternations are treated as a group, so `p1 | p2 | p3` is not the same as `p1 | (p2 | p3)`, and similarly for tuples. However, note that an n-ary alternation or tuple can match an n-ary conjunction or disjunction, because if the number of patterns exceeds the number of constructors in the type being destructed, the extra patterns will match on the last element, meaning that `p1 | p2 | p3` will act like `p1 | (p2 | p3)` when matching `a1 ∨ a2 ∨ a3`. If matching against a type with 3 constructors, `p1 | (p2 | p3)` will act like `p1 | (p2 | p3) | _` instead. -/ meta inductive rcases_patt : Type | one : name → rcases_patt | clear : rcases_patt | typed : rcases_patt → pexpr → rcases_patt | tuple : listΠ rcases_patt → rcases_patt | alts : listΣ rcases_patt → rcases_patt namespace rcases_patt meta instance inhabited : inhabited rcases_patt := ⟨one `_⟩ /-- Get the name from a pattern, if provided -/ meta def name : rcases_patt → option name | (one `_) := none | (one `rfl) := none | (one n) := some n | (typed p _) := p.name | (alts [p]) := p.name | _ := none /-- Interpret an rcases pattern as a tuple, where `p` becomes `⟨p⟩` if `p` is not already a tuple. -/ meta def as_tuple : rcases_patt → listΠ rcases_patt | (tuple ps) := ps | p := [p] /-- Interpret an rcases pattern as an alternation, where non-alternations are treated as one alternative. -/ meta def as_alts : rcases_patt → listΣ rcases_patt | (alts ps) := ps | p := [p] /-- Convert a list of patterns to a tuple pattern, but mapping `[p]` to `p` instead of `⟨p⟩`. -/ meta def tuple' : listΠ rcases_patt → rcases_patt | [p] := p | ps := tuple ps /-- Convert a list of patterns to an alternation pattern, but mapping `[p]` to `p` instead of a unary alternation `|p`. -/ meta def alts' : listΣ rcases_patt → rcases_patt | [p] := p | ps := alts ps /-- This function is used for producing rcases patterns based on a case tree. Suppose that we have a list of patterns `ps` that will match correctly against the branches of the case tree for one constructor. This function will merge tuples at the end of the list, so that `[a, b, ⟨c, d⟩]` becomes `⟨a, b, c, d⟩` instead of `⟨a, b, ⟨c, d⟩⟩`. We must be careful to turn `[a, ⟨⟩]` into `⟨a, ⟨⟩⟩` instead of `⟨a⟩` (which will not perform the nested match). -/ meta def tuple₁_core : listΠ rcases_patt → listΠ rcases_patt | [] := [] | [tuple []] := [tuple []] | [tuple ps] := ps | (p :: ps) := p :: tuple₁_core ps /-- This function is used for producing rcases patterns based on a case tree. This is like `tuple₁_core` but it produces a pattern instead of a tuple pattern list, converting `[n]` to `n` instead of `⟨n⟩` and `[]` to `_`, and otherwise just converting `[a, b, c]` to `⟨a, b, c⟩`. -/ meta def tuple₁ : listΠ rcases_patt → rcases_patt | [] := default _ | [one n] := one n | ps := tuple (tuple₁_core ps) /-- This function is used for producing rcases patterns based on a case tree. Here we are given the list of patterns to apply to each argument of each constructor after the main case, and must produce a list of alternatives with the same effect. This function calls `tuple₁` to make the individual alternatives, and handles merging `[a, b, c | d]` to `a | b | c | d` instead of `a | b | (c | d)`. -/ meta def alts₁_core : listΣ (listΠ rcases_patt) → listΣ rcases_patt | [] := [] | [[alts ps]] := ps | (p :: ps) := tuple₁ p :: alts₁_core ps /-- This function is used for producing rcases patterns based on a case tree. This is like `alts₁_core`, but it produces a cases pattern directly instead of a list of alternatives. We specially translate the empty alternation to `⟨⟩`, and translate `|(a | b)` to `⟨a | b⟩` (because we don't have any syntax for unary alternation). Otherwise we can use the regular merging of alternations at the last argument so that `a | b | (c | d)` becomes `a | b | c | d`. -/ meta def alts₁ : listΣ (listΠ rcases_patt) → rcases_patt | [[]] := tuple [] | [[alts ps]] := tuple [alts ps] | ps := alts' (alts₁_core ps) meta instance has_reflect : has_reflect rcases_patt | (one n) := `(_) | clear := `(_) | (typed l e) := (`(typed).subst (has_reflect l)).subst (reflect e) | (tuple l) := `(λ l, tuple l).subst $ by haveI := has_reflect; exact list.reflect l | (alts l) := `(λ l, alts l).subst $ by haveI := has_reflect; exact list.reflect l /-- Formats an `rcases` pattern. If the `bracket` argument is true, then it will be printed at high precedence, i.e. it will have parentheses around it if it is not already a tuple or atomic name. -/ protected meta def format : ∀ bracket : bool, rcases_patt → tactic _root_.format | _ (one n) := pure $ to_fmt n | _ clear := pure "-" | _ (tuple []) := pure "⟨⟩" | _ (tuple ls) := do fs ← ls.mmap $ format ff, pure $ "⟨" ++ _root_.format.group (_root_.format.nest 1 $ _root_.format.join $ list.intersperse ("," ++ _root_.format.line) fs) ++ "⟩" | br (alts ls) := do fs ← ls.mmap $ format tt, let fmt := _root_.format.join $ list.intersperse (↑" |" ++ _root_.format.space) fs, pure $ if br then _root_.format.bracket "(" ")" fmt else fmt | br (typed p e) := do fp ← format ff p, fe ← pp e, let fmt := fp ++ " : " ++ fe, pure $ if br then _root_.format.bracket "(" ")" fmt else fmt meta instance has_to_tactic_format : has_to_tactic_format rcases_patt := ⟨rcases_patt.format ff⟩ end rcases_patt /-- Takes the number of fields of a single constructor and patterns to match its fields against (not necessarily the same number). The returned lists each contain one element per field of the constructor. The `name` is the name which will be used in the top-level `cases` tactic, and the `rcases_patt` is the pattern which the field will be matched against by subsequent `cases` tactics. -/ meta def rcases.process_constructor : nat → listΠ rcases_patt → listΠ name × listΠ rcases_patt | 0 ps := ([], []) | 1 [] := ([`_], [default _]) | 1 [p] := ([p.name.get_or_else `_], [p]) -- The interesting case: we matched the last field against multiple -- patterns, so split off the remaining patterns into a subsequent -- match. This handles matching `α × β × γ` against `⟨a, b, c⟩`. | 1 ps := ([`_], [rcases_patt.tuple ps]) | (n+1) ps := let hd := ps.head, (ns, tl) := rcases.process_constructor n ps.tail in (hd.name.get_or_else `_ :: ns, hd :: tl) /-- Takes a list of constructor names, and an (alternation) list of patterns, and matches each pattern against its constructor. It returns the list of names that will be passed to `cases`, and the list of `(constructor name, patterns)` for each constructor, where `patterns` is the (conjunctive) list of patterns to apply to each constructor argument. -/ meta def rcases.process_constructors (params : nat) : listΣ name → listΣ rcases_patt → tactic (dlist name × listΣ (name × listΠ rcases_patt)) | [] ps := pure (dlist.empty, []) | (c::cs) ps := do n ← mk_const c >>= get_arity, let (h, t) := (match cs, ps.tail with -- We matched the last constructor against multiple patterns, -- so split off the remaining constructors. This handles matching -- `α ⊕ β ⊕ γ` against `a|b|c`. | [], _::_ := ([rcases_patt.alts ps], []) | _, _ := (ps.head.as_tuple, ps.tail) end : _), let (ns, ps) := rcases.process_constructor (n - params) h, (l, r) ← rcases.process_constructors cs t, pure (dlist.of_list ns ++ l, (c, ps) :: r) /-- Like `zip`, but only elements satisfying a matching predicate `p` will go in the list, and elements of the first list that fail to match the second list will be skipped. -/ private def align {α β} (p : α → β → Prop) [∀ a b, decidable (p a b)] : list α → list β → list (α × β) | (a::as) (b::bs) := if p a b then (a, b) :: align as bs else align as (b::bs) | _ _ := [] /-- Given a local constant `e`, get its type. *But* if `e` does not exist, go find a hypothesis with the same pretty name as `e` and get it instead. This is needed because we can sometimes lose track of the unique names of hypotheses when they are revert/intro'd by `change` and `cases`. (A better solution would be for these tactics to return a map of renamed hypotheses so that we don't lose track of them.) -/ private meta def get_local_and_type (e : expr) : tactic (expr × expr) := (do t ← infer_type e, pure (t, e)) <|> (do e ← get_local e.local_pp_name, t ← infer_type e, pure (t, e)) /-- * `rcases_core p e` will match a pattern `p` against a local hypothesis `e`. It returns the list of subgoals that were produced. * `rcases.continue pes` will match a (conjunctive) list of `(p, e)` pairs which refer to patterns and local hypotheses to match against, and applies all of them. Note that this can involve matching later arguments multiple times given earlier arguments, for example `⟨a | b, ⟨c, d⟩⟩` performs the `⟨c, d⟩` match twice, once on the `a` branch and once on `b`. -/ meta mutual def rcases_core, rcases.continue with rcases_core : rcases_patt → expr → tactic (list uncleared_goal) | (rcases_patt.one `rfl) e := do (t, e) ← get_local_and_type e, subst' e, list.map (prod.mk []) <$> get_goals -- If the pattern is any other name, we already bound the name in the -- top-level `cases` tactic, so there is no more work to do for it. | (rcases_patt.one _) _ := list.map (prod.mk []) <$> get_goals | rcases_patt.clear e := do m ← try_core (get_local_and_type e), list.map (prod.mk $ m.elim [] (λ ⟨_, e⟩, [e])) <$> get_goals | (rcases_patt.typed p ty) e := do (t, e) ← get_local_and_type e, ty ← i_to_expr_no_subgoals ``(%%ty : Sort*), unify t ty, t ← instantiate_mvars t, ty ← instantiate_mvars ty, e ← if t =ₐ ty then pure e else change_core ty (some e) >> get_local e.local_pp_name, rcases_core p e | (rcases_patt.alts [p]) e := rcases_core p e | pat e := do (t, e) ← get_local_and_type e, t ← whnf t, env ← get_env, let I := t.get_app_fn.const_name, let pat := pat.as_alts, (ids, r, l) ← (if I ≠ `quot then do when (¬env.is_inductive I) $ fail format!"rcases tactic failed: {e} : {I} is not an inductive datatype", let params := env.inductive_num_params I, let c := env.constructors_of I, (ids, r) ← rcases.process_constructors params c pat, l ← cases_core e ids.to_list, pure (ids, r, l) else do (ids, r) ← rcases.process_constructors 2 [`quot.mk] pat, [(_, d)] ← induction e ids.to_list `quot.induction_on | fail format!"quotient induction on {e} failed. Maybe goal is not in Prop?", -- the result from `induction` is missing the information that the original constructor was -- `quot.mk` so we fix this up: pure (ids, r, [(`quot.mk, d)])), gs ← get_goals, -- `cases_core` may not generate a new goal for every constructor, -- as some constructors may be impossible for type reasons. (See its -- documentation.) Match up the new goals with our remaining work -- by constructor name. let ls := align (λ (a : name × _) (b : _ × name × _), a.1 = b.2.1) r (gs.zip l), list.join <$> ls.mmap (λ⟨⟨_, ps⟩, g, _, hs, _⟩, set_goals [g] >> rcases.continue (ps.zip hs)) with rcases.continue : listΠ (rcases_patt × expr) → tactic (list uncleared_goal) | [] := list.map (prod.mk []) <$> get_goals | ((pat, e) :: pes) := do gs ← rcases_core pat e, list.join <$> gs.mmap (λ ⟨cs, g⟩, do set_goals [g], ugs ← rcases.continue pes, pure $ ugs.map $ λ ⟨cs', gs⟩, (cs ++ cs', gs)) /-- Given a list of `uncleared_goal`s, each of which is a goal metavariable and a list of variables to clear, actually perform the clear and set the goals with the result. -/ meta def clear_goals (ugs : list uncleared_goal) : tactic unit := do gs ← ugs.mmap (λ ⟨cs, g⟩, do set_goals [g], cs ← cs.mfoldr (λ c cs, (do (_, c) ← get_local_and_type c, pure (c :: cs)) <|> pure cs) [], clear' tt cs, [g] ← get_goals, pure g), set_goals gs /-- `rcases h e pat` performs case distinction on `e` using `pat` to name the arising new variables and assumptions. If `h` is `some` name, a new assumption `h : e = pat` will relate the expression `e` with the current pattern. See the module comment for the syntax of `pat`. -/ meta def rcases (h : option name) (p : pexpr) (pat : rcases_patt) : tactic unit := do let p := match pat with | rcases_patt.typed _ ty := ``(%%p : %%ty) | _ := p end, e ← match h with | some h := do x ← get_unused_name $ pat.name.get_or_else `this, interactive.generalize h () (p, x), get_local x | none := i_to_expr p end, if e.is_local_constant then focus1 (rcases_core pat e >>= clear_goals) else do x ← pat.name.elim mk_fresh_name pure, n ← revert_kdependencies e semireducible, tactic.generalize e x <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, pure ()), h ← tactic.intro1, focus1 (rcases_core pat h >>= clear_goals) /-- `rcases_many es pats` performs case distinction on the `es` using `pat` to name the arising new variables and assumptions. See the module comment for the syntax of `pat`. -/ meta def rcases_many (ps : listΠ pexpr) (pat : rcases_patt) : tactic unit := do let (_, pats) := rcases.process_constructor ps.length pat.as_tuple, pes ← (ps.zip pats).mmap (λ ⟨p, pat⟩, do let p := match pat with | rcases_patt.typed _ ty := ``(%%p : %%ty) | _ := p end, e ← i_to_expr p, if e.is_local_constant then pure (pat, e) else do x ← pat.name.elim mk_fresh_name pure, n ← revert_kdependencies e semireducible, tactic.generalize e x <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, pure ()), prod.mk pat <$> tactic.intro1), focus1 (rcases.continue pes >>= clear_goals) /-- `rintro pat₁ pat₂ ... patₙ` introduces `n` arguments, then pattern matches on the `patᵢ` using the same syntax as `rcases`. -/ meta def rintro (ids : listΠ rcases_patt) : tactic unit := do l ← ids.mmap (λ id, do e ← intro $ id.name.get_or_else `_, pure (id, e)), focus1 (rcases.continue l >>= clear_goals) /-- Like `zip_with`, but if the lists don't match in length, the excess elements will be put at the end of the result. -/ def merge_list {α} (m : α → α → α) : list α → list α → list α | [] l₂ := l₂ | l₁ [] := l₁ | (a :: l₁) (b :: l₂) := m a b :: merge_list l₁ l₂ /-- Merge two `rcases` patterns. This is used to underapproximate a case tree by an `rcases` pattern. The two patterns come from cases in two branches, that due to the syntax of `rcases` patterns are forced to overlap. The rule here is that we take only the case splits that are in common between both branches. For example if one branch does `⟨a, b⟩` and the other does `c`, then we return `c` because we don't know that a case on `c` would be safe to do. -/ meta def rcases_patt.merge : rcases_patt → rcases_patt → rcases_patt | (rcases_patt.alts p₁) p₂ := rcases_patt.alts (merge_list rcases_patt.merge p₁ p₂.as_alts) | p₁ (rcases_patt.alts p₂) := rcases_patt.alts (merge_list rcases_patt.merge p₁.as_alts p₂) | (rcases_patt.tuple p₁) p₂ := rcases_patt.tuple (merge_list rcases_patt.merge p₁ p₂.as_tuple) | p₁ (rcases_patt.tuple p₂) := rcases_patt.tuple (merge_list rcases_patt.merge p₁.as_tuple p₂) | (rcases_patt.typed p₁ e) p₂ := rcases_patt.typed (p₁.merge p₂) e | p₁ (rcases_patt.typed p₂ e) := rcases_patt.typed (p₁.merge p₂) e | (rcases_patt.one `rfl) (rcases_patt.one `rfl) := rcases_patt.one `rfl | (rcases_patt.one `_) p := p | p (rcases_patt.one `_) := p | rcases_patt.clear p := p | p rcases_patt.clear := p | (rcases_patt.one n) _ := rcases_patt.one n /-- * `rcases_hint_core depth e` does the same as `rcases p e`, except the pattern `p` is an output instead of an input, controlled only by the case depth argument `depth`. We use `cases` to depth `depth` and then reconstruct an `rcases` pattern `p` that would, if passed to `rcases`, perform the same thing as the case tree we just constructed (or at least, the nearest expressible approximation to this.) * `rcases_hint.process_constructors depth cs l` takes a list of constructor names `cs` and a matching list `l` of elements `(g, c', hs, _)` where `c'` is a constructor name (used for alignment with `cs`), `g` is the subgoal, and `hs` is the list of local hypotheses created by `cases` in that subgoal. It matches on all of them, and then produces a `ΣΠ`-list of `rcases` patterns describing the result, and the list of generated subgoals. * `rcases_hint.continue depth es` does the same as `rcases.continue (ps.zip es)`, except the patterns `ps` are an output instead of an input, created by matching on everything to depth `depth` and recording the successful cases. It returns `ps`, and the list of generated subgoals. -/ meta mutual def rcases_hint_core, rcases_hint.process_constructors, rcases_hint.continue with rcases_hint_core : ℕ → expr → tactic (rcases_patt × list expr) | depth e := do (t, e) ← get_local_and_type e, t ← whnf t, env ← get_env, let I := t.get_app_fn.const_name, (do guard (I = ``eq), subst' e, prod.mk (rcases_patt.one `rfl) <$> get_goals) <|> (do let c := env.constructors_of I, some l ← try_core (guard (depth ≠ 0) >> cases_core e) | let n := match e.local_pp_name with name.anonymous := `_ | n := n end in prod.mk (rcases_patt.one n) <$> get_goals, gs ← get_goals, if gs.empty then pure (rcases_patt.tuple [], []) else do (ps, gs') ← rcases_hint.process_constructors (depth - 1) c (gs.zip l), pure (rcases_patt.alts₁ ps, gs')) with rcases_hint.process_constructors : ℕ → listΣ name → list (expr × name × listΠ expr × list (name × expr)) → tactic (listΣ (listΠ rcases_patt) × list expr) | depth [] _ := pure ([], []) | depth cs [] := pure (cs.map (λ _, []), []) | depth (c::cs) ls@((g, c', hs, _) :: l) := if c ≠ c' then do (ps, gs) ← rcases_hint.process_constructors depth cs ls, pure ([] :: ps, gs) else do (p, gs) ← set_goals [g] >> rcases_hint.continue depth hs, (ps, gs') ← rcases_hint.process_constructors depth cs l, pure (p :: ps, gs ++ gs') with rcases_hint.continue : ℕ → listΠ expr → tactic (listΠ rcases_patt × list expr) | depth [] := prod.mk [] <$> get_goals | depth (e :: es) := do (p, gs) ← rcases_hint_core depth e, (ps, gs') ← gs.mfoldl (λ (r : listΠ rcases_patt × list expr) g, do (ps, gs') ← set_goals [g] >> rcases_hint.continue depth es, pure (merge_list rcases_patt.merge r.1 ps, r.2 ++ gs')) ([], []), pure (p :: ps, gs') /-- * `rcases? e` is like `rcases e with ...`, except it generates `...` by matching on everything it can, and it outputs an `rcases` invocation that should have the same effect. * `rcases? e : n` can be used to control the depth of case splits (especially important for recursive types like `nat`, which can be cased as many times as you like). -/ meta def rcases_hint (p : pexpr) (depth : nat) : tactic rcases_patt := do e ← i_to_expr p, if e.is_local_constant then focus1 $ do (p, gs) ← rcases_hint_core depth e, set_goals gs, pure p else do x ← mk_fresh_name, n ← revert_kdependencies e semireducible, tactic.generalize e x <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, pure ()), h ← tactic.intro1, focus1 $ do (p, gs) ← rcases_hint_core depth h, set_goals gs, pure p /-- * `rcases? ⟨e1, e2, e3⟩` is like `rcases ⟨e1, e2, e3⟩ with ...`, except it generates `...` by matching on everything it can, and it outputs an `rcases` invocation that should have the same effect. * `rcases? ⟨e1, e2, e3⟩ : n` can be used to control the depth of case splits (especially important for recursive types like `nat`, which can be cased as many times as you like). -/ meta def rcases_hint_many (ps : list pexpr) (depth : nat) : tactic (listΠ rcases_patt) := do es ← ps.mmap (λ p, do e ← i_to_expr p, if e.is_local_constant then pure e else do x ← mk_fresh_name, n ← revert_kdependencies e semireducible, tactic.generalize e x <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, pure ()), tactic.intro1), focus1 $ do (ps, gs) ← rcases_hint.continue depth es, set_goals gs, pure ps /-- * `rintro?` is like `rintro ...`, except it generates `...` by introducing and matching on everything it can, and it outputs an `rintro` invocation that should have the same effect. * `rintro? : n` can be used to control the depth of case splits (especially important for recursive types like `nat`, which can be cased as many times as you like). -/ meta def rintro_hint (depth : nat) : tactic (listΠ rcases_patt) := do l ← intros, focus1 $ do (p, gs) ← rcases_hint.continue depth l, set_goals gs, pure p setup_tactic_parser /-- * `rcases_patt_parse tt` will parse a high precedence `rcases` pattern, `patt_hi`. This means only tuples and identifiers are allowed; alternations and type ascriptions require `(...)` instead, which switches to `patt`. * `rcases_patt_parse ff` will parse a low precedence `rcases` pattern, `patt`. This consists of a `patt_med` (which deals with alternations), optionally followed by a `: ty` type ascription. The expression `ty` is at `texpr` precedence because it can appear at the end of a tactic, for example in `rcases e with x : ty <|> skip`. * `rcases_patt_parse_list` will parse an alternation list, `patt_med`, one or more `patt` patterns separated by `|`. It does not parse a `:` at the end, so that `a | b : ty` parses as `(a | b) : ty` where `a | b` is the `patt_med` part. * `rcases_patt_parse_list_rest a` parses an alternation list after the initial pattern, `| b | c`. ```lean patt ::= patt_med (":" expr)? patt_med ::= (patt_hi "|")* patt_hi patt_hi ::= id | "rfl" | "_" | "⟨" (patt ",")* patt "⟩" | "(" patt ")" ``` -/ meta mutual def rcases_patt_parse, rcases_patt_parse_list, rcases_patt_parse_list_rest with rcases_patt_parse : bool → parser rcases_patt | tt := with_desc "patt_hi" $ (brackets "(" ")" (rcases_patt_parse ff)) <|> (rcases_patt.tuple <$> brackets "⟨" "⟩" (sep_by (tk ",") (rcases_patt_parse ff))) <|> (tk "-" $> rcases_patt.clear) <|> (rcases_patt.one <$> ident_) | ff := with_desc "patt" $ do pat ← rcases_patt.alts' <$> rcases_patt_parse_list, (tk ":" *> pat.typed <$> texpr) <|> pure pat with rcases_patt_parse_list : parser (listΣ rcases_patt) | x := (with_desc "patt_med" $ rcases_patt_parse tt >>= rcases_patt_parse_list_rest) x with rcases_patt_parse_list_rest : rcases_patt → parser (listΣ rcases_patt) | pat := (tk "|" *> list.cons pat <$> rcases_patt_parse_list) <|> -- hack to support `-|-` patterns, because `|-` is a token (tk "|-" *> list.cons pat <$> rcases_patt_parse_list_rest rcases_patt.clear) <|> pure [pat] /-- Parse the optional depth argument `(: n)?` of `rcases?` and `rintro?`, with default depth 5. -/ meta def rcases_parse_depth : parser nat := do o ← (tk ":" *> small_nat)?, pure $ o.get_or_else 5 /-- The arguments to `rcases`, which in fact dispatch to several other tactics. * `rcases? expr (: n)?` or `rcases? ⟨expr, ...⟩ (: n)?` calls `rcases_hint` * `rcases? ⟨expr, ...⟩ (: n)?` calls `rcases_hint_many` * `rcases (h :)? expr (with patt)?` calls `rcases` * `rcases ⟨expr, ...⟩ (with patt)?` calls `rcases_many` -/ @[derive has_reflect] meta inductive rcases_args | hint (tgt : pexpr ⊕ list pexpr) (depth : nat) | rcases (name : option name) (tgt : pexpr) (pat : rcases_patt) | rcases_many (tgt : listΠ pexpr) (pat : rcases_patt) /-- Syntax for a `rcases` pattern: * `rcases? expr (: n)?` * `rcases (h :)? expr (with patt_list (: expr)?)?`. -/ meta def rcases_parse : parser rcases_args := with_desc "('?' expr (: n)?) | ((h :)? expr (with patt)?)" $ do hint ← (tk "?")?, p ← (sum.inr <$> brackets "⟨" "⟩" (sep_by (tk ",") (parser.pexpr 0))) <|> (sum.inl <$> texpr), match hint with | none := do p ← (do sum.inl (expr.local_const h _ _ _) ← pure p, tk ":" *> (@sum.inl _ (pexpr ⊕ list pexpr) ∘ prod.mk h) <$> texpr) <|> pure (sum.inr p), ids ← (tk "with" *> rcases_patt_parse ff)?, let ids := ids.get_or_else (rcases_patt.tuple []), pure $ match p with | sum.inl (name, tgt) := rcases_args.rcases (some name) tgt ids | sum.inr (sum.inl tgt) := rcases_args.rcases none tgt ids | sum.inr (sum.inr tgts) := rcases_args.rcases_many tgts ids end | some _ := do depth ← rcases_parse_depth, pure $ rcases_args.hint p depth end /-- `rintro_patt_parse_hi` and `rintro_patt_parse` are like `rcases_patt_parse`, but is used for parsing top level `rintro` patterns, which allow sequences like `(x y : t)` in addition to simple `rcases` patterns. * `rintro_patt_parse_hi` will parse a high precedence `rcases` pattern, `rintro_patt_hi` below. This means only tuples and identifiers are allowed; alternations and type ascriptions require `(...)` instead, which switches to `patt`. * `rintro_patt_parse tt` will parse a low precedence `rcases` pattern, `rintro_patt` below. This consists of either a sequence of patterns `p1 p2 p3` or an alternation list `p1 | p2 | p3` treated as a single pattern, optionally followed by a `: ty` type ascription, which applies to every pattern in the list. * `rintro_patt_parse ff` parses `rintro_patt_low`, which is the same as `rintro_patt_parse tt` but it does not permit an unparenthesized alternation list, it must have the form `p1 p2 p3 (: ty)?`. ```lean rintro_patt ::= (rintro_patt_hi+ | patt_med) (":" expr)? rintro_patt_low ::= rintro_patt_hi* (":" expr)? rintro_patt_hi ::= patt_hi | "(" rintro_patt ")" ``` -/ meta mutual def rintro_patt_parse_hi, rintro_patt_parse with rintro_patt_parse_hi : parser (listΠ rcases_patt) | x := (with_desc "rintro_patt_hi" $ brackets "(" ")" (rintro_patt_parse tt) <|> (do p ← rcases_patt_parse tt, pure [p])) x with rintro_patt_parse : bool → parser (listΠ rcases_patt) | med := with_desc "rintro_patt" $ do ll ← rintro_patt_parse_hi*, pats ← match med, ll.join with | tt, [] := failure | tt, [pat] := do l ← rcases_patt_parse_list_rest pat, pure [rcases_patt.alts' l] | _, pats := pure pats end, (do tk ":", e ← texpr, pure (pats.map (λ p, rcases_patt.typed p e))) <|> pure pats /-- Syntax for a `rintro` pattern: `('?' (: n)?) | rintro_patt`. -/ meta def rintro_parse : parser (listΠ rcases_patt ⊕ nat) := with_desc "('?' (: n)?) | patt*" $ (tk "?" >> sum.inr <$> rcases_parse_depth) <|> sum.inl <$> rintro_patt_parse ff namespace interactive open interactive interactive.types expr /-- `rcases` is a tactic that will perform `cases` recursively, according to a pattern. It is used to destructure hypotheses or expressions composed of inductive types like `h1 : a ∧ b ∧ c ∨ d` or `h2 : ∃ x y, trans_rel R x y`. Usual usage might be `rcases h1 with ⟨ha, hb, hc⟩ | hd` or `rcases h2 with ⟨x, y, _ | ⟨z, hxz, hzy⟩⟩` for these examples. Each element of an `rcases` pattern is matched against a particular local hypothesis (most of which are generated during the execution of `rcases` and represent individual elements destructured from the input expression). An `rcases` pattern has the following grammar: * A name like `x`, which names the active hypothesis as `x`. * A blank `_`, which does nothing (letting the automatic naming system used by `cases` name the hypothesis). * A hyphen `-`, which clears the active hypothesis and any dependents. * The keyword `rfl`, which expects the hypothesis to be `h : a = b`, and calls `subst` on the hypothesis (which has the effect of replacing `b` with `a` everywhere or vice versa). * A type ascription `p : ty`, which sets the type of the hypothesis to `ty` and then matches it against `p`. (Of course, `ty` must unify with the actual type of `h` for this to work.) * A tuple pattern `⟨p1, p2, p3⟩`, which matches a constructor with many arguments, or a series of nested conjunctions or existentials. For example if the active hypothesis is `a ∧ b ∧ c`, then the conjunction will be destructured, and `p1` will be matched against `a`, `p2` against `b` and so on. * An alteration pattern `p1 | p2 | p3`, which matches an inductive type with multiple constructors, or a nested disjunction like `a ∨ b ∨ c`. A pattern like `⟨a, b, c⟩ | ⟨d, e⟩` will do a split over the inductive datatype, naming the first three parameters of the first constructor as `a,b,c` and the first two of the second constructor `d,e`. If the list is not as long as the number of arguments to the constructor or the number of constructors, the remaining variables will be automatically named. If there are nested brackets such as `⟨⟨a⟩, b | c⟩ | d` then these will cause more case splits as necessary. If there are too many arguments, such as `⟨a, b, c⟩` for splitting on `∃ x, ∃ y, p x`, then it will be treated as `⟨a, ⟨b, c⟩⟩`, splitting the last parameter as necessary. `rcases` also has special support for quotient types: quotient induction into Prop works like matching on the constructor `quot.mk`. `rcases h : e with PAT` will do the same as `rcases e with PAT` with the exception that an assumption `h : e = PAT` will be added to the context. `rcases? e` will perform case splits on `e` in the same way as `rcases e`, but rather than accepting a pattern, it does a maximal cases and prints the pattern that would produce this case splitting. The default maximum depth is 5, but this can be modified with `rcases? e : n`. -/ meta def rcases : parse rcases_parse → tactic unit | (rcases_args.rcases h p ids) := tactic.rcases h p ids | (rcases_args.rcases_many ps ids) := tactic.rcases_many ps ids | (rcases_args.hint p depth) := do (pe, patt) ← match p with | sum.inl p := prod.mk <$> pp p <*> rcases_hint p depth | sum.inr ps := do patts ← rcases_hint_many ps depth, pes ← ps.mmap pp, pure (format.bracket "⟨" "⟩" (format.comma_separated pes), rcases_patt.tuple patts) end, ppat ← pp patt, trace $ ↑"Try this: rcases " ++ pe ++ " with " ++ ppat add_tactic_doc { name := "rcases", category := doc_category.tactic, decl_names := [`tactic.interactive.rcases], tags := ["induction"] } /-- The `rintro` tactic is a combination of the `intros` tactic with `rcases` to allow for destructuring patterns while introducing variables. See `rcases` for a description of supported patterns. For example, `rintro (a | ⟨b, c⟩) ⟨d, e⟩` will introduce two variables, and then do case splits on both of them producing two subgoals, one with variables `a d e` and the other with `b c d e`. `rintro`, unlike `rcases`, also supports the form `(x y : ty)` for introducing and type-ascripting multiple variables at once, similar to binders. `rintro?` will introduce and case split on variables in the same way as `rintro`, but will also print the `rintro` invocation that would have the same result. Like `rcases?`, `rintro? : n` allows for modifying the depth of splitting; the default is 5. `rintros` is an alias for `rintro`. -/ meta def rintro : parse rintro_parse → tactic unit | (sum.inl []) := intros [] | (sum.inl l) := tactic.rintro l | (sum.inr depth) := do ps ← tactic.rintro_hint depth, fs ← ps.mmap (λ p, do f ← pp $ p.format tt, pure $ format.space ++ format.group f), trace $ ↑"Try this: rintro" ++ format.join fs /-- Alias for `rintro`. -/ meta def rintros := rintro add_tactic_doc { name := "rintro", category := doc_category.tactic, decl_names := [`tactic.interactive.rintro, `tactic.interactive.rintros], tags := ["induction"], inherit_description_from := `tactic.interactive.rintro } setup_tactic_parser /-- Parses `patt? (: expr)? (:= expr)?`, the arguments for `obtain`. (This is almost the same as `rcases_patt_parse ff`, but it allows the pattern part to be empty.) -/ meta def obtain_parse : parser ((option rcases_patt × option pexpr) × option (pexpr ⊕ list pexpr)) := with_desc "patt? (: expr)? (:= expr)?" $ do (pat, tp) ← (do pat ← rcases_patt_parse ff, pure $ match pat with | rcases_patt.typed pat tp := (some pat, some tp) | _ := (some pat, none) end) <|> prod.mk none <$> (tk ":" >> texpr)?, prod.mk (pat, tp) <$> (do tk ":=", (guard tp.is_none >> sum.inr <$> brackets "⟨" "⟩" (sep_by (tk ",") (parser.pexpr 0))) <|> (sum.inl <$> texpr))? /-- The `obtain` tactic is a combination of `have` and `rcases`. See `rcases` for a description of supported patterns. ```lean obtain ⟨patt⟩ : type, { ... } ``` is equivalent to ```lean have h : type, { ... }, rcases h with ⟨patt⟩ ``` The syntax `obtain ⟨patt⟩ : type := proof` is also supported. If `⟨patt⟩` is omitted, `rcases` will try to infer the pattern. If `type` is omitted, `:= proof` is required. -/ meta def obtain : parse obtain_parse → tactic unit | ((pat, _), some (sum.inr val)) := tactic.rcases_many val (pat.get_or_else (default _)) | ((pat, none), some (sum.inl val)) := tactic.rcases none val (pat.get_or_else (default _)) | ((pat, some tp), some (sum.inl val)) := tactic.rcases none val $ (pat.get_or_else (default _)).typed tp | ((pat, some tp), none) := do nm ← mk_fresh_name, e ← to_expr tp >>= assert nm, (g :: gs) ← get_goals, set_goals gs, tactic.rcases none ``(%%e) (pat.get_or_else (rcases_patt.one `this)), gs ← get_goals, set_goals (g::gs) | ((pat, none), none) := fail $ "`obtain` requires either an expected type or a value.\n" ++ "usage: `obtain ⟨patt⟩? : type (:= val)?` or `obtain ⟨patt⟩? (: type)? := val`" add_tactic_doc { name := "obtain", category := doc_category.tactic, decl_names := [`tactic.interactive.obtain], tags := ["induction"] } end interactive end tactic
ad12546c4d59f949b84d1b56f6f33a40d9702b9a
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/stage0/src/Lean/Meta/ExprDefEq.lean
98894e00360d261c202a93467212a7a50c5b5f69
[ "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
48,088
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.ProjFns import Lean.Meta.WHNF import Lean.Meta.InferType import Lean.Meta.FunInfo import Lean.Meta.LevelDefEq import Lean.Meta.Check import Lean.Meta.Offset namespace Lean.Meta /-- Try to solve `a := (fun x => t) =?= b` by eta-expanding `b`. Remark: eta-reduction is not a good alternative even in a system without universe cumulativity like Lean. Example: ``` (fun x : A => f ?m) =?= f ``` The left-hand side of the constraint above it not eta-reduced because `?m` is a metavariable. -/ private def isDefEqEta (a b : Expr) : MetaM Bool := do if a.isLambda && !b.isLambda then let bType ← inferType b let bType ← whnfD bType match bType with | Expr.forallE n d _ c => let b' := mkLambda n c.binderInfo d (mkApp b (mkBVar 0)) commitWhen $ Meta.isExprDefEqAux a b' | _ => pure false else pure false /-- Support for `Lean.reduceBool` and `Lean.reduceNat` -/ def isDefEqNative (s t : Expr) : MetaM LBool := do let isDefEq (s t) : MetaM LBool := toLBoolM $ Meta.isExprDefEqAux s t let s? ← reduceNative? s let t? ← reduceNative? t match s?, t? with | some s, some t => isDefEq s t | some s, none => isDefEq s t | none, some t => isDefEq s t | none, none => pure LBool.undef /-- Support for reducing Nat basic operations. -/ def isDefEqNat (s t : Expr) : MetaM LBool := do let isDefEq (s t) : MetaM LBool := toLBoolM $ Meta.isExprDefEqAux s t if s.hasFVar || s.hasMVar || t.hasFVar || t.hasMVar then pure LBool.undef else let s? ← reduceNat? s let t? ← reduceNat? t match s?, t? with | some s, some t => isDefEq s t | some s, none => isDefEq s t | none, some t => isDefEq s t | none, none => pure LBool.undef /-- Support for constraints of the form `("..." =?= String.mk cs)` -/ def isDefEqStringLit (s t : Expr) : MetaM LBool := do let isDefEq (s t) : MetaM LBool := toLBoolM $ Meta.isExprDefEqAux s t if s.isStringLit && t.isAppOf `String.mk then isDefEq (toCtorIfLit s) t else if s.isAppOf `String.mk && t.isStringLit then isDefEq s (toCtorIfLit t) else pure LBool.undef /-- Return `true` if `e` is of the form `fun (x_1 ... x_n) => ?m x_1 ... x_n)`, and `?m` is unassigned. Remark: `n` may be 0. -/ def isEtaUnassignedMVar (e : Expr) : MetaM Bool := do match e.etaExpanded? with | some (Expr.mvar mvarId _) => if (← isReadOnlyOrSyntheticOpaqueExprMVar mvarId) then pure false else if (← isExprMVarAssigned mvarId) then pure false else pure true | _ => pure false /- First pass for `isDefEqArgs`. We unify explicit arguments, *and* easy cases Here, we say a case is easy if it is of the form ?m =?= t or t =?= ?m where `?m` is unassigned. These easy cases are not just an optimization. When `?m` is a function, by assigning it to t, we make sure a unification constraint (in the explicit part) ``` ?m t =?= f s ``` is not higher-order. We also handle the eta-expanded cases: ``` fun x₁ ... xₙ => ?m x₁ ... xₙ =?= t t =?= fun x₁ ... xₙ => ?m x₁ ... xₙ This is important because type inference often produces eta-expanded terms, and without this extra case, we could introduce counter intuitive behavior. Pre: `paramInfo.size <= args₁.size = args₂.size` -/ private partial def isDefEqArgsFirstPass (paramInfo : Array ParamInfo) (args₁ args₂ : Array Expr) : MetaM (Option (Array Nat)) := do let rec loop (i : Nat) (postponed : Array Nat) := do if h : i < paramInfo.size then let info := paramInfo.get ⟨i, h⟩ let a₁ := args₁[i] let a₂ := args₂[i] if info.implicit || info.instImplicit then if (← isEtaUnassignedMVar a₁ <||> isEtaUnassignedMVar a₂) then if (← Meta.isExprDefEqAux a₁ a₂) then loop (i+1) postponed else pure none else loop (i+1) (postponed.push i) else if (← Meta.isExprDefEqAux a₁ a₂) then loop (i+1) postponed else pure none else pure (some postponed) loop 0 #[] @[specialize] private def trySynthPending (e : Expr) : MetaM Bool := do let mvarId? ← getStuckMVar? e match mvarId? with | some mvarId => Meta.synthPending mvarId | none => pure false private partial def isDefEqArgs (f : Expr) (args₁ args₂ : Array Expr) : MetaM Bool := if h : args₁.size = args₂.size then do let finfo ← getFunInfoNArgs f args₁.size let (some postponed) ← isDefEqArgsFirstPass finfo.paramInfo args₁ args₂ | pure false let rec processOtherArgs (i : Nat) : MetaM Bool := do if h₁ : i < args₁.size then let a₁ := args₁.get ⟨i, h₁⟩ let a₂ := args₂.get ⟨i, Eq.subst h h₁⟩ if (← Meta.isExprDefEqAux a₁ a₂) then processOtherArgs (i+1) else pure false else pure true if (← processOtherArgs finfo.paramInfo.size) then postponed.allM fun i => do /- Second pass: unify implicit arguments. In the second pass, we make sure we are unfolding at least non reducible definitions (default setting). -/ let a₁ := args₁[i] let a₂ := args₂[i] let info := finfo.paramInfo[i] if info.instImplicit then trySynthPending a₁ trySynthPending a₂ pure () withAtLeastTransparency TransparencyMode.default $ Meta.isExprDefEqAux a₁ a₂ else pure false else pure false /-- Check whether the types of the free variables at `fvars` are definitionally equal to the types at `ds₂`. Pre: `fvars.size == ds₂.size` This method also updates the set of local instances, and invokes the continuation `k` with the updated set. We can't use `withNewLocalInstances` because the `isDeq fvarType d₂` may use local instances. -/ @[specialize] partial def isDefEqBindingDomain (fvars : Array Expr) (ds₂ : Array Expr) (k : MetaM Bool) : MetaM Bool := let rec loop (i : Nat) := do if h : i < fvars.size then do let fvar := fvars.get ⟨i, h⟩ let fvarDecl ← getFVarLocalDecl fvar let fvarType := fvarDecl.type let d₂ := ds₂[i] if (← Meta.isExprDefEqAux fvarType d₂) then match (← isClass? fvarType) with | some className => withNewLocalInstance className fvar $ loop (i+1) | none => loop (i+1) else pure false else k loop 0 /- Auxiliary function for `isDefEqBinding` for handling binders `forall/fun`. It accumulates the new free variables in `fvars`, and declare them at `lctx`. We use the domain types of `e₁` to create the new free variables. We store the domain types of `e₂` at `ds₂`. -/ private partial def isDefEqBindingAux (lctx : LocalContext) (fvars : Array Expr) (e₁ e₂ : Expr) (ds₂ : Array Expr) : MetaM Bool := let process (n : Name) (d₁ d₂ b₁ b₂ : Expr) : MetaM Bool := do let d₁ := d₁.instantiateRev fvars let d₂ := d₂.instantiateRev fvars let fvarId ← mkFreshId let lctx := lctx.mkLocalDecl fvarId n d₁ let fvars := fvars.push (mkFVar fvarId) isDefEqBindingAux lctx fvars b₁ b₂ (ds₂.push d₂) match e₁, e₂ with | Expr.forallE n d₁ b₁ _, Expr.forallE _ d₂ b₂ _ => process n d₁ d₂ b₁ b₂ | Expr.lam n d₁ b₁ _, Expr.lam _ d₂ b₂ _ => process n d₁ d₂ b₁ b₂ | _, _ => withReader (fun ctx => { ctx with lctx := lctx }) do isDefEqBindingDomain fvars ds₂ $ Meta.isExprDefEqAux (e₁.instantiateRev fvars) (e₂.instantiateRev fvars) @[inline] private def isDefEqBinding (a b : Expr) : MetaM Bool := do let lctx ← getLCtx isDefEqBindingAux lctx #[] a b #[] private def checkTypesAndAssign (mvar : Expr) (v : Expr) : MetaM Bool := traceCtx `Meta.isDefEq.assign.checkTypes do -- must check whether types are definitionally equal or not, before assigning and returning true let mvarType ← inferType mvar let vType ← inferType v if (← withTransparency TransparencyMode.default $ Meta.isExprDefEqAux mvarType vType) then trace[Meta.isDefEq.assign.final]! "{mvar} := {v}" assignExprMVar mvar.mvarId! v pure true else trace[Meta.isDefEq.assign.typeMismatch]! "{mvar} : {mvarType} := {v} : {vType}" pure false /- Each metavariable is declared in a particular local context. We use the notation `C |- ?m : t` to denote a metavariable `?m` that was declared at the local context `C` with type `t` (see `MetavarDecl`). We also use `?m@C` as a shorthand for `C |- ?m : t` where `t` is the type of `?m`. The following method process the unification constraint ?m@C a₁ ... aₙ =?= t We say the unification constraint is a pattern IFF 1) `a₁ ... aₙ` are pairwise distinct free variables that are ​*not*​ let-variables. 2) `a₁ ... aₙ` are not in `C` 3) `t` only contains free variables in `C` and/or `{a₁, ..., aₙ}` 4) For every metavariable `?m'@C'` occurring in `t`, `C'` is a subprefix of `C` 5) `?m` does not occur in `t` Claim: we don't have to check free variable declarations. That is, if `t` contains a reference to `x : A := v`, we don't need to check `v`. Reason: The reference to `x` is a free variable, and it must be in `C` (by 1 and 3). If `x` is in `C`, then any metavariable occurring in `v` must have been defined in a strict subprefix of `C`. So, condition 4 and 5 are satisfied. If the conditions above have been satisfied, then the solution for the unification constrain is ?m := fun a₁ ... aₙ => t Now, we consider some workarounds/approximations. A1) Suppose `t` contains a reference to `x : A := v` and `x` is not in `C` (failed condition 3) (precise) solution: unfold `x` in `t`. A2) Suppose some `aᵢ` is in `C` (failed condition 2) (approximated) solution (when `config.ctxApprox` is set to true) : ignore condition and also use ?m := fun a₁ ... aₙ => t Here is an example where this approximation fails: Given `C` containing `a : nat`, consider the following two constraints ?m@C a =?= a ?m@C b =?= a If we use the approximation in the first constraint, we get ?m := fun x => x when we apply this solution to the second one we get a failure. IMPORTANT: When applying this approximation we need to make sure the abstracted term `fun a₁ ... aₙ => t` is type correct. The check can only be skipped in the pattern case described above. Consider the following example. Given the local context (α : Type) (a : α) we try to solve ?m α =?= @id α a If we use the approximation above we obtain: ?m := (fun α' => @id α' a) which is a type incorrect term. `a` has type `α` but it is expected to have type `α'`. The problem occurs because the right hand side contains a free variable `a` that depends on the free variable `α` being abstracted. Note that this dependency cannot occur in patterns. We can address this by type checking the term after abstraction. This is not a significant performance bottleneck because this case doesn't happen very often in practice (262 times when compiling stdlib on Jan 2018). The second example is trickier, but it also occurs less frequently (8 times when compiling stdlib on Jan 2018, and all occurrences were at Init/Control when we define monads and auxiliary combinators for them). We considered three options for the addressing the issue on the second example: A3) `a₁ ... aₙ` are not pairwise distinct (failed condition 1). In Lean3, we would try to approximate this case using an approach similar to A2. However, this approximation complicates the code, and is never used in the Lean3 stdlib and mathlib. A4) `t` contains a metavariable `?m'@C'` where `C'` is not a subprefix of `C`. If `?m'` is assigned, we substitute. If not, we create an auxiliary metavariable with a smaller scope. Actually, we let `elimMVarDeps` at `MetavarContext.lean` to perform this step. A5) If some `aᵢ` is not a free variable, then we use first-order unification (if `config.foApprox` is set to true) ?m a_1 ... a_i a_{i+1} ... a_{i+k} =?= f b_1 ... b_k reduces to ?M a_1 ... a_i =?= f a_{i+1} =?= b_1 ... a_{i+k} =?= b_k A6) If (m =?= v) is of the form ?m a_1 ... a_n =?= ?m b_1 ... b_k then we use first-order unification (if `config.foApprox` is set to true) A7) When `foApprox`, we may use another approximation (`constApprox`) for solving constraints of the form ``` ?m s₁ ... sₙ =?= t ``` where `s₁ ... sₙ` are arbitrary terms. We solve them by assigning the constant function to `?m`. ``` ?m := fun _ ... _ => t ``` In general, this approximation may produce bad solutions, and may prevent coercions from being tried. For example, consider the term `pure (x > 0)` with inferred type `?m Prop` and expected type `IO Bool`. In this situation, the elaborator generates the unification constraint ``` ?m Prop =?= IO Bool ``` It is not a higher-order pattern, nor first-order approximation is applicable. However, constant approximation produces the bogus solution `?m := fun _ => IO Bool`, and prevents the system from using the coercion from the decidable proposition `x > 0` to `Bool`. On the other hand, the constant approximation is desirable for elaborating the term ``` let f (x : _) := pure "hello"; f () ``` with expected type `IO String`. In this example, the following unification contraint is generated. ``` ?m () String =?= IO String ``` It is not a higher-order pattern, first-order approximation reduces it to ``` ?m () =?= IO ``` which fails to be solved. However, constant approximation solves it by assigning ``` ?m := fun _ => IO ``` Note that `f`s type is `(x : ?α) -> ?m x String`. The metavariable `?m` may depend on `x`. If `constApprox` is set to true, we use constant approximation. Otherwise, we use a heuristic to decide whether we should apply it or not. The heuristic is based on observing where the constraints above come from. In the first example, the constraint `?m Prop =?= IO Bool` come from polymorphic method where `?m` is expected to be a **function** of type `Type -> Type`. In the second example, the first argument of `?m` is used to model a **potential** dependency on `x`. By using constant approximation here, we are just saying the type of `f` does **not** depend on `x`. We claim this is a reasonable approximation in practice. Moreover, it is expected by any functional programmer used to non-dependently type languages (e.g., Haskell). We distinguish the two cases above by using the field `numScopeArgs` at `MetavarDecl`. This fiels tracks how many metavariable arguments are representing dependencies. -/ def mkAuxMVar (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (numScopeArgs : Nat := 0) : MetaM Expr := do mkFreshExprMVarAt lctx localInsts type MetavarKind.natural Name.anonymous numScopeArgs namespace CheckAssignment builtin_initialize checkAssignmentExceptionId : InternalExceptionId ← registerInternalExceptionId `checkAssignment builtin_initialize outOfScopeExceptionId : InternalExceptionId ← registerInternalExceptionId `outOfScope structure State := (cache : ExprStructMap Expr := {}) structure Context := (mvarId : MVarId) (mvarDecl : MetavarDecl) (fvars : Array Expr) (hasCtxLocals : Bool) (rhs : Expr) abbrev CheckAssignmentM := ReaderT Context $ StateRefT State MetaM def throwCheckAssignmentFailure {α} : CheckAssignmentM α := throw $ Exception.internal checkAssignmentExceptionId def throwOutOfScopeFVar {α} : CheckAssignmentM α := throw $ Exception.internal outOfScopeExceptionId private def findCached? (e : Expr) : CheckAssignmentM (Option Expr) := do return (← get).cache.find? e private def cache (e r : Expr) : CheckAssignmentM Unit := do modify fun s => { s with cache := s.cache.insert e r } instance : MonadCache Expr Expr CheckAssignmentM := { findCached? := findCached?, cache := cache } @[inline] private def visit (f : Expr → CheckAssignmentM Expr) (e : Expr) : CheckAssignmentM Expr := if !e.hasExprMVar && !e.hasFVar then pure e else checkCache e f private def addAssignmentInfo (msg : MessageData) : CheckAssignmentM MessageData := do let ctx ← read return msg!" @ {mkMVar ctx.mvarId} {ctx.fvars} := {ctx.rhs}" @[specialize] def checkFVar (check : Expr → CheckAssignmentM Expr) (fvar : Expr) : CheckAssignmentM Expr := do let ctxMeta ← readThe Meta.Context let ctx ← read if ctx.mvarDecl.lctx.containsFVar fvar then pure fvar else let lctx := ctxMeta.lctx match lctx.findFVar? fvar with | some (LocalDecl.ldecl _ _ _ _ v _) => visit check v | _ => if ctx.fvars.contains fvar then pure fvar else traceM `Meta.isDefEq.assign.outOfScopeFVar do addAssignmentInfo fvar throwOutOfScopeFVar @[specialize] def checkMVar (check : Expr → CheckAssignmentM Expr) (mvar : Expr) : CheckAssignmentM Expr := do let mvarId := mvar.mvarId! let ctx ← read let mctx ← getMCtx if mvarId == ctx.mvarId then traceM `Meta.isDefEq.assign.occursCheck $ addAssignmentInfo "occurs check failed" throwCheckAssignmentFailure else match mctx.getExprAssignment? mvarId with | some v => check v | none => match mctx.findDecl? mvarId with | none => throwUnknownMVar mvarId | some mvarDecl => if ctx.hasCtxLocals then throwCheckAssignmentFailure -- It is not a pattern, then we fail and fall back to FO unification else if mvarDecl.lctx.isSubPrefixOf ctx.mvarDecl.lctx ctx.fvars then /- The local context of `mvar` - free variables being abstracted is a subprefix of the metavariable being assigned. We "substract" variables being abstracted because we use `elimMVarDeps` -/ pure mvar else if mvarDecl.depth != mctx.depth || mvarDecl.kind.isSyntheticOpaque then traceM `Meta.isDefEq.assign.readOnlyMVarWithBiggerLCtx $ addAssignmentInfo (mkMVar mvarId) throwCheckAssignmentFailure else let ctxMeta ← readThe Meta.Context if ctxMeta.config.ctxApprox && ctx.mvarDecl.lctx.isSubPrefixOf mvarDecl.lctx then /- Create an auxiliary metavariable with a smaller context and "checked" type. Note that `mvarType` may be different from `mvarDecl.type`. Example: `mvarType` contains a metavariable that we also need to reduce the context. We remove from `ctx.mvarDecl.lctx` any variable that is not in `mvarDecl.lctx` or in `ctx.fvars`. We don't need to remove the ones in `ctx.fvars` because `elimMVarDeps` will take care of them. First, we collect `toErase` the variables that need to be erased. Notat that if a variable is `ctx.fvars`, but it depends on variable at `toErase`, we must also erase it. -/ let toErase := mvarDecl.lctx.foldl (init := #[]) fun toErase localDecl => if ctx.mvarDecl.lctx.contains localDecl.fvarId then toErase else if ctx.fvars.any fun fvar => fvar.fvarId! == localDecl.fvarId then if mctx.findLocalDeclDependsOn localDecl fun fvarId => toErase.contains fvarId then -- localDecl depends on a variable that will be erased. So, we must add it to `toErase` too toErase.push localDecl.fvarId else toErase else toErase.push localDecl.fvarId let lctx := toErase.foldl (init := mvarDecl.lctx) fun lctx toEraseFVar => lctx.erase toEraseFVar /- Compute new set of local instances. -/ let localInsts := mvarDecl.localInstances.filter fun localInst => toErase.contains localInst.fvar.fvarId! let mvarType ← check mvarDecl.type let newMVar ← mkAuxMVar lctx localInsts mvarType mvarDecl.numScopeArgs modifyThe Meta.State fun s => { s with mctx := s.mctx.assignExpr mvarId newMVar } pure newMVar else pure mvar /- Auxiliary function used to "fix" subterms of the form `?m x_1 ... x_n` where `x_i`s are free variables, and one of them is out-of-scope. See `Expr.app` case at `check`. If `ctxApprox` is true, then we solve this case by creating a fresh metavariable ?n with the correct scope, an assigning `?m := fun _ ... _ => ?n` -/ def assignToConstFun (mvar : Expr) (numArgs : Nat) (newMVar : Expr) : MetaM Bool := do let mvarType ← inferType mvar forallBoundedTelescope mvarType numArgs fun xs _ => do if xs.size != numArgs then pure false else let v ← mkLambdaFVars xs newMVar checkTypesAndAssign mvar v partial def check (e : Expr) : CheckAssignmentM Expr := do match e with | Expr.mdata _ b _ => return e.updateMData! (← visit check b) | Expr.proj _ _ s _ => return e.updateProj! (← visit check s) | Expr.lam _ d b _ => return e.updateLambdaE! (← visit check d) (← visit check b) | Expr.forallE _ d b _ => return e.updateForallE! (← visit check d) (← visit check b) | Expr.letE _ t v b _ => return e.updateLet! (← visit check t) (← visit check v) (← visit check b) | Expr.bvar .. => return e | Expr.sort .. => return e | Expr.const .. => return e | Expr.lit .. => return e | Expr.fvar .. => visit (checkFVar check) e | Expr.mvar .. => visit (checkMVar check) e | Expr.app .. => e.withApp fun f args => do let ctxMeta ← readThe Meta.Context if f.isMVar && ctxMeta.config.ctxApprox && args.all Expr.isFVar then let f ← visit (checkMVar check) f catchInternalId outOfScopeExceptionId (do let args ← args.mapM (visit check) pure $ mkAppN f args) (fun ex => do if (← isDelayedAssigned f.mvarId!) then throw ex else let eType ← inferType e let mvarType ← check eType /- Create an auxiliary metavariable with a smaller context and "checked" type, assign `?f := fun _ => ?newMVar` Note that `mvarType` may be different from `eType`. -/ let ctx ← read let newMVar ← mkAuxMVar ctx.mvarDecl.lctx ctx.mvarDecl.localInstances mvarType if (← assignToConstFun f args.size newMVar) then pure newMVar else throw ex) else let f ← visit check f let args ← args.mapM (visit check) pure $ mkAppN f args @[inline] def run (x : CheckAssignmentM Expr) (mvarId : MVarId) (fvars : Array Expr) (hasCtxLocals : Bool) (v : Expr) : MetaM (Option Expr) := do let mvarDecl ← getMVarDecl mvarId let ctx := { mvarId := mvarId, mvarDecl := mvarDecl, fvars := fvars, hasCtxLocals := hasCtxLocals, rhs := v : Context } let x : CheckAssignmentM (Option Expr) := catchInternalIds [outOfScopeExceptionId, checkAssignmentExceptionId] (do let e ← x; pure $ some e) (fun _ => pure none) x.run ctx $.run' {} end CheckAssignment namespace CheckAssignmentQuick partial def check (hasCtxLocals ctxApprox : Bool) (mctx : MetavarContext) (lctx : LocalContext) (mvarDecl : MetavarDecl) (mvarId : MVarId) (fvars : Array Expr) (e : Expr) : Bool := let rec visit (e : Expr) : Bool := if !e.hasExprMVar && !e.hasFVar then true else match e with | Expr.mdata _ b _ => visit b | Expr.proj _ _ s _ => visit s | Expr.app f a _ => visit f && visit a | Expr.lam _ d b _ => visit d && visit b | Expr.forallE _ d b _ => visit d && visit b | Expr.letE _ t v b _ => visit t && visit v && visit b | Expr.bvar .. => true | Expr.sort .. => true | Expr.const .. => true | Expr.lit .. => true | Expr.fvar fvarId .. => if mvarDecl.lctx.contains fvarId then true else match lctx.find? fvarId with | some (LocalDecl.ldecl _ _ _ _ v _) => false -- need expensive CheckAssignment.check | _ => if fvars.any $ fun x => x.fvarId! == fvarId then true else false -- We could throw an exception here, but we would have to use ExceptM. So, we let CheckAssignment.check do it | Expr.mvar mvarId' _ => match mctx.getExprAssignment? mvarId' with | some _ => false -- use CheckAssignment.check to instantiate | none => if mvarId' == mvarId then false -- occurs check failed, use CheckAssignment.check to throw exception else match mctx.findDecl? mvarId' with | none => false | some mvarDecl' => if hasCtxLocals then false -- use CheckAssignment.check else if mvarDecl'.lctx.isSubPrefixOf mvarDecl.lctx fvars then true else if mvarDecl'.depth != mctx.depth || mvarDecl'.kind.isSyntheticOpaque then false -- use CheckAssignment.check else if ctxApprox && mvarDecl.lctx.isSubPrefixOf mvarDecl'.lctx then false -- use CheckAssignment.check else true visit e end CheckAssignmentQuick -- See checkAssignment def checkAssignmentAux (mvarId : MVarId) (fvars : Array Expr) (hasCtxLocals : Bool) (v : Expr) : MetaM (Option Expr) := do CheckAssignment.run (CheckAssignment.check v) mvarId fvars hasCtxLocals v /-- Auxiliary function for handling constraints of the form `?m a₁ ... aₙ =?= v`. It will check whether we can perform the assignment ``` ?m := fun fvars => t ``` The result is `none` if the assignment can't be performed. The result is `some newV` where `newV` is a possibly updated `v`. This method may need to unfold let-declarations. -/ def checkAssignment (mvarId : MVarId) (fvars : Array Expr) (v : Expr) : MetaM (Option Expr) := do if !v.hasExprMVar && !v.hasFVar then pure (some v) else let mvarDecl ← getMVarDecl mvarId let hasCtxLocals := fvars.any $ fun fvar => mvarDecl.lctx.containsFVar fvar let ctx ← read let mctx ← getMCtx if CheckAssignmentQuick.check hasCtxLocals ctx.config.ctxApprox mctx ctx.lctx mvarDecl mvarId fvars v then pure (some v) else let v ← instantiateMVars v checkAssignmentAux mvarId fvars hasCtxLocals v private def processAssignmentFOApproxAux (mvar : Expr) (args : Array Expr) (v : Expr) : MetaM Bool := match v with | Expr.app f a _ => Meta.isExprDefEqAux args.back a <&&> Meta.isExprDefEqAux (mkAppRange mvar 0 (args.size - 1) args) f | _ => pure false /- Auxiliary method for applying first-order unification. It is an approximation. Remark: this method is trying to solve the unification constraint: ?m a₁ ... aₙ =?= v It is uses processAssignmentFOApproxAux, if it fails, it tries to unfold `v`. We have added support for unfolding here because we want to be able to solve unification problems such as ?m Unit =?= ITactic where `ITactic` is defined as def ITactic := Tactic Unit -/ private partial def processAssignmentFOApprox (mvar : Expr) (args : Array Expr) (v : Expr) : MetaM Bool := let rec loop (v : Expr) := do let cfg ← getConfig if !cfg.foApprox then pure false else trace[Meta.isDefEq.foApprox]! "{mvar} {args} := {v}" if (← commitWhen $ processAssignmentFOApproxAux mvar args v) then pure true else match (← unfoldDefinition? v) with | none => pure false | some v => loop v loop v private partial def simpAssignmentArgAux : Expr → MetaM Expr | Expr.mdata _ e _ => simpAssignmentArgAux e | e@(Expr.fvar fvarId _) => do let decl ← getLocalDecl fvarId match decl.value? with | some value => simpAssignmentArgAux value | _ => pure e | e => pure e /- Auxiliary procedure for processing `?m a₁ ... aₙ =?= v`. We apply it to each `aᵢ`. It instantiates assigned metavariables if `aᵢ` is of the form `f[?n] b₁ ... bₘ`, and then removes metadata, and zeta-expand let-decls. -/ private def simpAssignmentArg (arg : Expr) : MetaM Expr := do let arg ← if arg.getAppFn.hasExprMVar then instantiateMVars arg else pure arg simpAssignmentArgAux arg /- Assign `mvar := fun a_1 ... a_{numArgs} => v`. We use it at `processConstApprox` and `isDefEqMVarSelf` -/ private def assignConst (mvar : Expr) (numArgs : Nat) (v : Expr) : MetaM Bool := do let mvarDecl ← getMVarDecl mvar.mvarId! forallBoundedTelescope mvarDecl.type numArgs fun xs _ => do if xs.size != numArgs then pure false else let v ← mkLambdaFVars xs v trace[Meta.isDefEq.constApprox]! "{mvar} := {v}" checkTypesAndAssign mvar v private def processConstApprox (mvar : Expr) (numArgs : Nat) (v : Expr) : MetaM Bool := do let cfg ← getConfig let mvarId := mvar.mvarId! let mvarDecl ← getMVarDecl mvarId if mvarDecl.numScopeArgs == numArgs || cfg.constApprox then match (← checkAssignment mvarId #[] v) with | none => pure false | some v => assignConst mvar numArgs v else pure false /-- Tries to solve `?m a₁ ... aₙ =?= v` by assigning `?m`. It assumes `?m` is unassigned. -/ private partial def processAssignment (mvarApp : Expr) (v : Expr) : MetaM Bool := traceCtx `Meta.isDefEq.assign do trace[Meta.isDefEq.assign]! "{mvarApp} := {v}" let mvar := mvarApp.getAppFn let mvarDecl ← getMVarDecl mvar.mvarId! let rec process (i : Nat) (args : Array Expr) (v : Expr) := do let cfg ← getConfig let useFOApprox (args : Array Expr) : MetaM Bool := processAssignmentFOApprox mvar args v <||> processConstApprox mvar args.size v if h : i < args.size then let arg := args.get ⟨i, h⟩ let arg ← simpAssignmentArg arg let args := args.set ⟨i, h⟩ arg match arg with | Expr.fvar fvarId _ => if args[0:i].any fun prevArg => prevArg == arg then useFOApprox args else if mvarDecl.lctx.contains fvarId && !cfg.quasiPatternApprox then useFOApprox args else process (i+1) args v | _ => useFOApprox args else let v ← instantiateMVars v -- enforce A4 if v.getAppFn == mvar then -- using A6 useFOApprox args else let mvarId := mvar.mvarId! match (← checkAssignment mvarId args v) with | none => useFOApprox args | some v => do trace[Meta.isDefEq.assign.beforeMkLambda]! "{mvar} {args} := {v}" let v ← mkLambdaFVars args v if args.any (fun arg => mvarDecl.lctx.containsFVar arg) then /- We need to type check `v` because abstraction using `mkLambdaFVars` may have produced a type incorrect term. See discussion at A2 -/ if (← isTypeCorrect v) then checkTypesAndAssign mvar v else trace[Meta.isDefEq.assign.typeError]! "{mvar} := {v}" useFOApprox args else checkTypesAndAssign mvar v process 0 mvarApp.getAppArgs v private def isDeltaCandidate? (t : Expr) : MetaM (Option ConstantInfo) := match t.getAppFn with | Expr.const c _ _ => getConst? c | _ => pure none /-- Auxiliary method for isDefEqDelta -/ private def isListLevelDefEq (us vs : List Level) : MetaM LBool := toLBoolM $ isListLevelDefEqAux us vs /-- Auxiliary method for isDefEqDelta -/ private def isDefEqLeft (fn : Name) (t s : Expr) : MetaM LBool := do trace[Meta.isDefEq.delta.unfoldLeft]! fn toLBoolM $ Meta.isExprDefEqAux t s /-- Auxiliary method for isDefEqDelta -/ private def isDefEqRight (fn : Name) (t s : Expr) : MetaM LBool := do trace[Meta.isDefEq.delta.unfoldRight]! fn toLBoolM $ Meta.isExprDefEqAux t s /-- Auxiliary method for isDefEqDelta -/ private def isDefEqLeftRight (fn : Name) (t s : Expr) : MetaM LBool := do trace[Meta.isDefEq.delta.unfoldLeftRight]! fn toLBoolM $ Meta.isExprDefEqAux t s /-- Try to solve `f a₁ ... aₙ =?= f b₁ ... bₙ` by solving `a₁ =?= b₁, ..., aₙ =?= bₙ`. Auxiliary method for isDefEqDelta -/ private def tryHeuristic (t s : Expr) : MetaM Bool := let tFn := t.getAppFn let sFn := s.getAppFn traceCtx `Meta.isDefEq.delta do commitWhen do let b ← isDefEqArgs tFn t.getAppArgs s.getAppArgs <&&> isListLevelDefEqAux tFn.constLevels! sFn.constLevels! unless b do trace[Meta.isDefEq.delta]! "heuristic failed {t} =?= {s}" pure b /-- Auxiliary method for isDefEqDelta -/ private abbrev unfold {α} (e : Expr) (failK : MetaM α) (successK : Expr → MetaM α) : MetaM α := do match (← unfoldDefinition? e) with | some e => successK e | none => failK /-- Auxiliary method for isDefEqDelta -/ private def unfoldBothDefEq (fn : Name) (t s : Expr) : MetaM LBool := do match t, s with | Expr.const _ ls₁ _, Expr.const _ ls₂ _ => isListLevelDefEq ls₁ ls₂ | Expr.app _ _ _, Expr.app _ _ _ => if (← tryHeuristic t s) then pure LBool.true else unfold t (unfold s (pure LBool.false) (fun s => isDefEqRight fn t s)) (fun t => unfold s (isDefEqLeft fn t s) (fun s => isDefEqLeftRight fn t s)) | _, _ => pure LBool.false private def sameHeadSymbol (t s : Expr) : Bool := match t.getAppFn, s.getAppFn with | Expr.const c₁ _ _, Expr.const c₂ _ _ => true | _, _ => false /-- - If headSymbol (unfold t) == headSymbol s, then unfold t - If headSymbol (unfold s) == headSymbol t, then unfold s - Otherwise unfold t and s if possible. Auxiliary method for isDefEqDelta -/ private def unfoldComparingHeadsDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool := unfold t (unfold s (pure LBool.undef) -- `t` and `s` failed to be unfolded (fun s => isDefEqRight sInfo.name t s)) (fun tNew => if sameHeadSymbol tNew s then isDefEqLeft tInfo.name tNew s else unfold s (isDefEqLeft tInfo.name tNew s) (fun sNew => if sameHeadSymbol t sNew then isDefEqRight sInfo.name t sNew else isDefEqLeftRight tInfo.name tNew sNew)) /-- If `t` and `s` do not contain metavariables, then use kernel definitional equality heuristics. Otherwise, use `unfoldComparingHeadsDefEq`. Auxiliary method for isDefEqDelta -/ private def unfoldDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool := if !t.hasExprMVar && !s.hasExprMVar then /- If `t` and `s` do not contain metavariables, we simulate strategy used in the kernel. -/ if tInfo.hints.lt sInfo.hints then unfold t (unfoldComparingHeadsDefEq tInfo sInfo t s) $ fun t => isDefEqLeft tInfo.name t s else if sInfo.hints.lt tInfo.hints then unfold s (unfoldComparingHeadsDefEq tInfo sInfo t s) $ fun s => isDefEqRight sInfo.name t s else unfoldComparingHeadsDefEq tInfo sInfo t s else unfoldComparingHeadsDefEq tInfo sInfo t s /-- When `TransparencyMode` is set to `default` or `all`. If `t` is reducible and `s` is not ==> `isDefEqLeft (unfold t) s` If `s` is reducible and `t` is not ==> `isDefEqRight t (unfold s)` Otherwise, use `unfoldDefEq` Auxiliary method for isDefEqDelta -/ private def unfoldReducibeDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool := do if (← shouldReduceReducibleOnly) then unfoldDefEq tInfo sInfo t s else let tReducible ← isReducible tInfo.name let sReducible ← isReducible sInfo.name if tReducible && !sReducible then unfold t (unfoldDefEq tInfo sInfo t s) fun t => isDefEqLeft tInfo.name t s else if !tReducible && sReducible then unfold s (unfoldDefEq tInfo sInfo t s) fun s => isDefEqRight sInfo.name t s else unfoldDefEq tInfo sInfo t s /-- If `t` is a projection function application and `s` is not ==> `isDefEqRight t (unfold s)` If `s` is a projection function application and `t` is not ==> `isDefEqRight (unfold t) s` Otherwise, use `unfoldReducibeDefEq` Auxiliary method for isDefEqDelta -/ private def unfoldNonProjFnDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool := do let env ← getEnv let tProj? := env.isProjectionFn tInfo.name let sProj? := env.isProjectionFn sInfo.name if tProj? && !sProj? then unfold s (unfoldDefEq tInfo sInfo t s) $ fun s => isDefEqRight sInfo.name t s else if !tProj? && sProj? then unfold t (unfoldDefEq tInfo sInfo t s) $ fun t => isDefEqLeft tInfo.name t s else unfoldReducibeDefEq tInfo sInfo t s /-- isDefEq by lazy delta reduction. This method implements many different heuristics: 1- If only `t` can be unfolded => then unfold `t` and continue 2- If only `s` can be unfolded => then unfold `s` and continue 3- If `t` and `s` can be unfolded and they have the same head symbol, then a) First try to solve unification by unifying arguments. b) If it fails, unfold both and continue. Implemented by `unfoldBothDefEq` 4- If `t` is a projection function application and `s` is not => then unfold `s` and continue. 5- If `s` is a projection function application and `t` is not => then unfold `t` and continue. Remark: 4&5 are implemented by `unfoldNonProjFnDefEq` 6- If `t` is reducible and `s` is not => then unfold `t` and continue. 7- If `s` is reducible and `t` is not => then unfold `s` and continue Remark: 6&7 are implemented by `unfoldReducibeDefEq` 8- If `t` and `s` do not contain metavariables, then use heuristic used in the Kernel. Implemented by `unfoldDefEq` 9- If `headSymbol (unfold t) == headSymbol s`, then unfold t and continue. 10- If `headSymbol (unfold s) == headSymbol t`, then unfold s 11- Otherwise, unfold `t` and `s` and continue. Remark: 9&10&11 are implemented by `unfoldComparingHeadsDefEq` -/ private def isDefEqDelta (t s : Expr) : MetaM LBool := do let tInfo? ← isDeltaCandidate? t.getAppFn let sInfo? ← isDeltaCandidate? s.getAppFn match tInfo?, sInfo? with | none, none => pure LBool.undef | some tInfo, none => unfold t (pure LBool.undef) fun t => isDefEqLeft tInfo.name t s | none, some sInfo => unfold s (pure LBool.undef) fun s => isDefEqRight sInfo.name t s | some tInfo, some sInfo => if tInfo.name == sInfo.name then unfoldBothDefEq tInfo.name t s else unfoldNonProjFnDefEq tInfo sInfo t s private def isAssigned : Expr → MetaM Bool | Expr.mvar mvarId _ => isExprMVarAssigned mvarId | _ => pure false private def isDelayedAssignedHead (tFn : Expr) (t : Expr) : MetaM Bool := do match tFn with | Expr.mvar mvarId _ => if (← isDelayedAssigned mvarId) then let tNew ← instantiateMVars t pure $ tNew != t else pure false | _ => pure false private def isSynthetic : Expr → MetaM Bool | Expr.mvar mvarId _ => do let mvarDecl ← getMVarDecl mvarId match mvarDecl.kind with | MetavarKind.synthetic => pure true | MetavarKind.syntheticOpaque => pure true | MetavarKind.natural => pure false | _ => pure false private def isAssignable : Expr → MetaM Bool | Expr.mvar mvarId _ => do let b ← isReadOnlyOrSyntheticOpaqueExprMVar mvarId; pure (!b) | _ => pure false private def etaEq (t s : Expr) : Bool := match t.etaExpanded? with | some t => t == s | none => false private def isLetFVar (fvarId : FVarId) : MetaM Bool := do let decl ← getLocalDecl fvarId pure decl.isLet private def isDefEqProofIrrel (t s : Expr) : MetaM LBool := do let status ← isProofQuick t match status with | LBool.false => pure LBool.undef | LBool.true => let tType ← inferType t let sType ← inferType s toLBoolM $ Meta.isExprDefEqAux tType sType | LBool.undef => let tType ← inferType t if (← isProp tType) then let sType ← inferType s toLBoolM $ Meta.isExprDefEqAux tType sType else pure LBool.undef /- Try to solve constraint of the form `?m args₁ =?= ?m args₂`. - First try to unify `args₁` and `args₂`, and return true if successful - Otherwise, try to assign `?m` to a constant function of the form `fun x_1 ... x_n => ?n` where `?n` is a fresh metavariable. See `processConstApprox`. -/ private def isDefEqMVarSelf (mvar : Expr) (args₁ args₂ : Array Expr) : MetaM Bool := do if args₁.size != args₂.size then pure false else if (← isDefEqArgs mvar args₁ args₂) then pure true else if !(← isAssignable mvar) then pure false else let cfg ← getConfig let mvarId := mvar.mvarId! let mvarDecl ← getMVarDecl mvarId if mvarDecl.numScopeArgs == args₁.size || cfg.constApprox then let type ← inferType (mkAppN mvar args₁) let auxMVar ← mkAuxMVar mvarDecl.lctx mvarDecl.localInstances type assignConst mvar args₁.size auxMVar else pure false /- Remove unnecessary let-decls -/ private def consumeLet : Expr → Expr | e@(Expr.letE _ _ _ b _) => if b.hasLooseBVars then e else consumeLet b | e => e mutual private partial def isDefEqQuick (t s : Expr) : MetaM LBool := let t := consumeLet t let s := consumeLet s match t, s with | Expr.lit l₁ _, Expr.lit l₂ _ => pure (l₁ == l₂).toLBool | Expr.sort u _, Expr.sort v _ => toLBoolM $ isLevelDefEqAux u v | t@(Expr.lam _ _ _ _), s@(Expr.lam _ _ _ _) => if t == s then pure LBool.true else toLBoolM $ isDefEqBinding t s | t@(Expr.forallE _ _ _ _), s@(Expr.forallE _ _ _ _) => if t == s then pure LBool.true else toLBoolM $ isDefEqBinding t s | Expr.mdata _ t _, s => isDefEqQuick t s | t, Expr.mdata _ s _ => isDefEqQuick t s | t@(Expr.fvar fvarId₁ _), s@(Expr.fvar fvarId₂ _) => do if (← isLetFVar fvarId₁ <||> isLetFVar fvarId₂) then pure LBool.undef else if fvarId₁ == fvarId₂ then pure LBool.true else isDefEqProofIrrel t s | t, s => isDefEqQuickOther t s private partial def isDefEqQuickOther (t s : Expr) : MetaM LBool := do if t == s then pure LBool.true else if etaEq t s || etaEq s t then pure LBool.true -- t =?= (fun xs => t xs) else let tFn := t.getAppFn let sFn := s.getAppFn if !tFn.isMVar && !sFn.isMVar then pure LBool.undef else if (← isAssigned tFn) then let t ← instantiateMVars t isDefEqQuick t s else if (← isAssigned sFn) then let s ← instantiateMVars s isDefEqQuick t s else if (← isDelayedAssignedHead tFn t) then let t ← instantiateMVars t isDefEqQuick t s else if (← isDelayedAssignedHead sFn s) then let s ← instantiateMVars s isDefEqQuick t s else if (← isSynthetic tFn <&&> trySynthPending tFn) then let t ← instantiateMVars t isDefEqQuick t s else if (← isSynthetic sFn <&&> trySynthPending sFn) then let s ← instantiateMVars s isDefEqQuick t s else if tFn.isMVar && sFn.isMVar && tFn == sFn then Bool.toLBool <$> isDefEqMVarSelf tFn t.getAppArgs s.getAppArgs else let tAssign? ← isAssignable tFn let sAssign? ← isAssignable sFn let assignableMsg (b : Bool) := if b then "[assignable]" else "[nonassignable]" trace[Meta.isDefEq]! "{t} {assignableMsg tAssign?} =?= {s} {assignableMsg sAssign?}" let assign (t s : Expr) : MetaM LBool := toLBoolM $ processAssignment t s if tAssign? && !sAssign? then toLBoolM $ processAssignment t s else if !tAssign? && sAssign? then toLBoolM $ processAssignment s t else if !tAssign? && !sAssign? then if tFn.isMVar || sFn.isMVar then let ctx ← read if ctx.config.isDefEqStuckEx then do trace[Meta.isDefEq.stuck]! "{t} =?= {s}" Meta.throwIsDefEqStuck else pure LBool.false else pure LBool.undef else isDefEqQuickMVarMVar t s -- Both `t` and `s` are terms of the form `?m ...` private partial def isDefEqQuickMVarMVar (t s : Expr) : MetaM LBool := do let tFn := t.getAppFn let sFn := s.getAppFn let tMVarDecl ← getMVarDecl tFn.mvarId! let sMVarDecl ← getMVarDecl sFn.mvarId! if s.isMVar && !t.isMVar then /- Solve `?m t =?= ?n` by trying first `?n := ?m t`. Reason: this assignment is precise. -/ if (← commitWhen (processAssignment s t)) then pure LBool.true else toLBoolM $ processAssignment t s else if (← commitWhen (processAssignment t s)) then pure LBool.true else toLBoolM $ processAssignment s t end @[inline] def whenUndefDo (x : MetaM LBool) (k : MetaM Bool) : MetaM Bool := do let status ← x match status with | LBool.true => pure true | LBool.false => pure false | LBool.undef => k @[specialize] private def unstuckMVar (e : Expr) (successK : Expr → MetaM Bool) (failK : MetaM Bool): MetaM Bool := do match (← getStuckMVar? e) with | some mvarId => if (← Meta.synthPending mvarId) then let e ← instantiateMVars e successK e else failK | none => failK private def isDefEqOnFailure (t s : Expr) : MetaM Bool := unstuckMVar t (fun t => Meta.isExprDefEqAux t s) $ unstuckMVar s (fun s => Meta.isExprDefEqAux t s) $ pure false private def isDefEqProj : Expr → Expr → MetaM Bool | Expr.proj _ i t _, Expr.proj _ j s _ => pure (i == j) <&&> Meta.isExprDefEqAux t s | _, _ => pure false partial def isExprDefEqAuxImpl (t : Expr) (s : Expr) : MetaM Bool := do trace[Meta.isDefEq.step]! "{t} =?= {s}" withNestedTraces do whenUndefDo (isDefEqQuick t s) $ whenUndefDo (isDefEqProofIrrel t s) do let t' ← whnfCore t let s' ← whnfCore s if t != t' || s != s' then isExprDefEqAuxImpl t' s' else do if (← (isDefEqEta t s <||> isDefEqEta s t)) then pure true else if (← isDefEqProj t s) then pure true else whenUndefDo (isDefEqNative t s) do whenUndefDo (isDefEqNat t s) do whenUndefDo (isDefEqOffset t s) do whenUndefDo (isDefEqDelta t s) do match t, s with | Expr.const c us _, Expr.const d vs _ => if c == d then isListLevelDefEqAux us vs else pure false | Expr.app _ _ _, Expr.app _ _ _ => let tFn := t.getAppFn if (← commitWhen (Meta.isExprDefEqAux tFn s.getAppFn <&&> isDefEqArgs tFn t.getAppArgs s.getAppArgs)) then pure true else isDefEqOnFailure t s | _, _ => whenUndefDo (isDefEqStringLit t s) $ isDefEqOnFailure t s builtin_initialize isExprDefEqAuxRef.set isExprDefEqAuxImpl builtin_initialize registerTraceClass `Meta.isDefEq registerTraceClass `Meta.isDefEq.foApprox registerTraceClass `Meta.isDefEq.constApprox registerTraceClass `Meta.isDefEq.delta registerTraceClass `Meta.isDefEq.step registerTraceClass `Meta.isDefEq.assign end Lean.Meta
d266da3cb336ce0317c999ecbbc30430a10b37a6
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/geometry/manifold/mfderiv.lean
1dbd812eeb894e21219a8370ce672e538650241b
[ "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
79,282
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import geometry.manifold.local_invariant_properties import geometry.manifold.tangent_bundle /-! # The derivative of functions between smooth manifolds Let `M` and `M'` be two smooth manifolds with corners over a field `𝕜` (with respective models with corners `I` on `(E, H)` and `I'` on `(E', H')`), and let `f : M → M'`. We define the derivative of the function at a point, within a set or along the whole space, mimicking the API for (Fréchet) derivatives. It is denoted by `mfderiv I I' f x`, where "m" stands for "manifold" and "f" for "Fréchet" (as in the usual derivative `fderiv 𝕜 f x`). ## Main definitions * `unique_mdiff_on I s` : predicate saying that, at each point of the set `s`, a function can have at most one derivative. This technical condition is important when we define `mfderiv_within` below, as otherwise there is an arbitrary choice in the derivative, and many properties will fail (for instance the chain rule). This is analogous to `unique_diff_on 𝕜 s` in a vector space. Let `f` be a map between smooth manifolds. The following definitions follow the `fderiv` API. * `mfderiv I I' f x` : the derivative of `f` at `x`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. If the map is not differentiable, this is `0`. * `mfderiv_within I I' f s x` : the derivative of `f` at `x` within `s`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. If the map is not differentiable within `s`, this is `0`. * `mdifferentiable_at I I' f x` : Prop expressing whether `f` is differentiable at `x`. * `mdifferentiable_within_at 𝕜 f s x` : Prop expressing whether `f` is differentiable within `s` at `x`. * `has_mfderiv_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative at `x`. * `has_mfderiv_within_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative within `s` at `x`. * `mdifferentiable_on I I' f s` : Prop expressing that `f` is differentiable on the set `s`. * `mdifferentiable I I' f` : Prop expressing that `f` is differentiable everywhere. * `tangent_map I I' f` : the derivative of `f`, as a map from the tangent bundle of `M` to the tangent bundle of `M'`. We also establish results on the differential of the identity, constant functions, charts, extended charts. For functions between vector spaces, we show that the usual notions and the manifold notions coincide. ## Implementation notes The tangent bundle is constructed using the machinery of topological fiber bundles, for which one can define bundled morphisms and construct canonically maps from the total space of one bundle to the total space of another one. One could use this mechanism to construct directly the derivative of a smooth map. However, we want to define the derivative of any map (and let it be zero if the map is not differentiable) to avoid proof arguments everywhere. This means we have to go back to the details of the definition of the total space of a fiber bundle constructed from core, to cook up a suitable definition of the derivative. It is the following: at each point, we have a preferred chart (used to identify the fiber above the point with the model vector space in fiber bundles). Then one should read the function using these preferred charts at `x` and `f x`, and take the derivative of `f` in these charts. Due to the fact that we are working in a model with corners, with an additional embedding `I` of the model space `H` in the model vector space `E`, the charts taking values in `E` are not the original charts of the manifold, but those ones composed with `I`, called extended charts. We define `written_in_ext_chart I I' x f` for the function `f` written in the preferred extended charts. Then the manifold derivative of `f`, at `x`, is just the usual derivative of `written_in_ext_chart I I' x f`, at the point `(ext_chart_at I x) x`. There is a subtelty with respect to continuity: if the function is not continuous, then the image of a small open set around `x` will not be contained in the source of the preferred chart around `f x`, which means that when reading `f` in the chart one is losing some information. To avoid this, we include continuity in the definition of differentiablity (which is reasonable since with any definition, differentiability implies continuity). *Warning*: the derivative (even within a subset) is a linear map on the whole tangent space. Suppose that one is given a smooth submanifold `N`, and a function which is smooth on `N` (i.e., its restriction to the subtype `N` is smooth). Then, in the whole manifold `M`, the property `mdifferentiable_on I I' f N` holds. However, `mfderiv_within I I' f N` is not uniquely defined (what values would one choose for vectors that are transverse to `N`?), which can create issues down the road. The problem here is that knowing the value of `f` along `N` does not determine the differential of `f` in all directions. This is in contrast to the case where `N` would be an open subset, or a submanifold with boundary of maximal dimension, where this issue does not appear. The predicate `unique_mdiff_on I N` indicates that the derivative along `N` is unique if it exists, and is an assumption in most statements requiring a form of uniqueness. On a vector space, the manifold derivative and the usual derivative are equal. This means in particular that they live on the same space, i.e., the tangent space is defeq to the original vector space. To get this property is a motivation for our definition of the tangent space as a single copy of the vector space, instead of more usual definitions such as the space of derivations, or the space of equivalence classes of smooth curves in the manifold. ## Tags Derivative, manifold -/ noncomputable theory open_locale classical topological_space manifold open set universe u section derivatives_definitions /-! ### Derivative of maps between manifolds The derivative of a smooth map `f` between smooth manifold `M` and `M'` at `x` is a bounded linear map from the tangent space to `M` at `x`, to the tangent space to `M'` at `f x`. Since we defined the tangent space using one specific chart, the formula for the derivative is written in terms of this specific chart. We use the names `mdifferentiable` and `mfderiv`, where the prefix letter `m` means "manifold". -/ variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H') {M' : Type*} [topological_space M'] [charted_space H' M'] /-- Property in the model space of a model with corners of being differentiable within at set at a point, when read in the model vector space. This property will be lifted to manifolds to define differentiable functions between manifolds. -/ def differentiable_within_at_prop (f : H → H') (s : set H) (x : H) : Prop := differentiable_within_at 𝕜 (I' ∘ f ∘ (I.symm)) (⇑(I.symm) ⁻¹' s ∩ set.range I) (I x) /-- Being differentiable in the model space is a local property, invariant under smooth maps. Therefore, it will lift nicely to manifolds. -/ lemma differentiable_within_at_local_invariant_prop : (cont_diff_groupoid ⊤ I).local_invariant_prop (cont_diff_groupoid ⊤ I') (differentiable_within_at_prop I I') := { is_local := begin assume s x u f u_open xu, have : I.symm ⁻¹' (s ∩ u) ∩ set.range I = (I.symm ⁻¹' s ∩ set.range I) ∩ I.symm ⁻¹' u, by simp only [set.inter_right_comm, set.preimage_inter], rw [differentiable_within_at_prop, differentiable_within_at_prop, this], symmetry, apply differentiable_within_at_inter, have : u ∈ 𝓝 (I.symm (I x)), by { rw [model_with_corners.left_inv], exact is_open.mem_nhds u_open xu }, apply continuous_at.preimage_mem_nhds I.continuous_symm.continuous_at this, end, right_invariance' := begin assume s x f e he hx h, rw differentiable_within_at_prop at h ⊢, have : I x = (I ∘ e.symm ∘ I.symm) (I (e x)), by simp only [hx] with mfld_simps, rw this at h, have : I (e x) ∈ (I.symm) ⁻¹' e.target ∩ set.range I, by simp only [hx] with mfld_simps, have := ((mem_groupoid_of_pregroupoid.2 he).2.cont_diff_within_at this), convert (h.comp' _ (this.differentiable_within_at le_top)).mono_of_mem _ using 1, { ext y, simp only with mfld_simps }, refine mem_nhds_within.mpr ⟨I.symm ⁻¹' e.target, e.open_target.preimage I.continuous_symm, by simp_rw [set.mem_preimage, I.left_inv, e.maps_to hx], _⟩, mfld_set_tac end, congr_of_forall := begin assume s x f g h hx hf, apply hf.congr, { assume y hy, simp only with mfld_simps at hy, simp only [h, hy] with mfld_simps }, { simp only [hx] with mfld_simps } end, left_invariance' := begin assume s x f e' he' hs hx h, rw differentiable_within_at_prop at h ⊢, have A : (I' ∘ f ∘ I.symm) (I x) ∈ (I'.symm ⁻¹' e'.source ∩ set.range I'), by simp only [hx] with mfld_simps, have := ((mem_groupoid_of_pregroupoid.2 he').1.cont_diff_within_at A), convert (this.differentiable_within_at le_top).comp _ h _, { ext y, simp only with mfld_simps }, { assume y hy, simp only with mfld_simps at hy, simpa only [hy] with mfld_simps using hs hy.1 } end } /-- Predicate ensuring that, at a point and within a set, a function can have at most one derivative. This is expressed using the preferred chart at the considered point. -/ def unique_mdiff_within_at (s : set M) (x : M) := unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) /-- Predicate ensuring that, at all points of a set, a function can have at most one derivative. -/ def unique_mdiff_on (s : set M) := ∀x∈s, unique_mdiff_within_at I s x /-- `mdifferentiable_within_at I I' f s x` indicates that the function `f` between manifolds has a derivative at the point `x` within the set `s`. This is a generalization of `differentiable_within_at` to manifolds. We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def mdifferentiable_within_at (f : M → M') (s : set M) (x : M) := continuous_within_at f s x ∧ differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) lemma mdifferentiable_within_at_iff_lift_prop_within_at (f : M → M') (s : set M) (x : M) : mdifferentiable_within_at I I' f s x ↔ lift_prop_within_at (differentiable_within_at_prop I I') f s x := by refl /-- `mdifferentiable_at I I' f x` indicates that the function `f` between manifolds has a derivative at the point `x`. This is a generalization of `differentiable_at` to manifolds. We require continuity in the definition, as otherwise points close to `x` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def mdifferentiable_at (f : M → M') (x : M) := continuous_at f x ∧ differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) (range I) ((ext_chart_at I x) x) lemma mdifferentiable_at_iff_lift_prop_at (f : M → M') (x : M) : mdifferentiable_at I I' f x ↔ lift_prop_at (differentiable_within_at_prop I I') f x := begin congrm _ ∧ _, { rw continuous_within_at_univ }, { simp [differentiable_within_at_prop, set.univ_inter] } end /-- `mdifferentiable_on I I' f s` indicates that the function `f` between manifolds has a derivative within `s` at all points of `s`. This is a generalization of `differentiable_on` to manifolds. -/ def mdifferentiable_on (f : M → M') (s : set M) := ∀x ∈ s, mdifferentiable_within_at I I' f s x /-- `mdifferentiable I I' f` indicates that the function `f` between manifolds has a derivative everywhere. This is a generalization of `differentiable` to manifolds. -/ def mdifferentiable (f : M → M') := ∀x, mdifferentiable_at I I' f x /-- Prop registering if a local homeomorphism is a local diffeomorphism on its source -/ def local_homeomorph.mdifferentiable (f : local_homeomorph M M') := (mdifferentiable_on I I' f f.source) ∧ (mdifferentiable_on I' I f.symm f.target) variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M'] /-- `has_mfderiv_within_at I I' f s x f'` indicates that the function `f` between manifolds has, at the point `x` and within the set `s`, the derivative `f'`. Here, `f'` is a continuous linear map from the tangent space at `x` to the tangent space at `f x`. This is a generalization of `has_fderiv_within_at` to manifolds (as indicated by the prefix `m`). The order of arguments is changed as the type of the derivative `f'` depends on the choice of `x`. We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def has_mfderiv_within_at (f : M → M') (s : set M) (x : M) (f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) := continuous_within_at f s x ∧ has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f' ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) /-- `has_mfderiv_at I I' f x f'` indicates that the function `f` between manifolds has, at the point `x`, the derivative `f'`. Here, `f'` is a continuous linear map from the tangent space at `x` to the tangent space at `f x`. We require continuity in the definition, as otherwise points close to `x` `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def has_mfderiv_at (f : M → M') (x : M) (f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) := continuous_at f x ∧ has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f' (range I) ((ext_chart_at I x) x) /-- Let `f` be a function between two smooth manifolds. Then `mfderiv_within I I' f s x` is the derivative of `f` at `x` within `s`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. -/ def mfderiv_within (f : M → M') (s : set M) (x : M) : tangent_space I x →L[𝕜] tangent_space I' (f x) := if h : mdifferentiable_within_at I I' f s x then (fderiv_within 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) : _) else 0 /-- Let `f` be a function between two smooth manifolds. Then `mfderiv I I' f x` is the derivative of `f` at `x`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. -/ def mfderiv (f : M → M') (x : M) : tangent_space I x →L[𝕜] tangent_space I' (f x) := if h : mdifferentiable_at I I' f x then (fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : E → E') (range I) ((ext_chart_at I x) x) : _) else 0 /-- The derivative within a set, as a map between the tangent bundles -/ def tangent_map_within (f : M → M') (s : set M) : tangent_bundle I M → tangent_bundle I' M' := λp, ⟨f p.1, (mfderiv_within I I' f s p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩ /-- The derivative, as a map between the tangent bundles -/ def tangent_map (f : M → M') : tangent_bundle I M → tangent_bundle I' M' := λp, ⟨f p.1, (mfderiv I I' f p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩ end derivatives_definitions section derivatives_properties /-! ### Unique differentiability sets in manifolds -/ variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] -- {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} {M' : Type*} [topological_space M'] [charted_space H' M'] {E'' : Type*} [normed_add_comm_group E''] [normed_space 𝕜 E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''} {M'' : Type*} [topological_space M''] [charted_space H'' M''] {f f₀ f₁ : M → M'} {x : M} {s t : set M} {g : M' → M''} {u : set M'} lemma unique_mdiff_within_at_univ : unique_mdiff_within_at I univ x := begin unfold unique_mdiff_within_at, simp only [preimage_univ, univ_inter], exact I.unique_diff _ (mem_range_self _) end variable {I} lemma unique_mdiff_within_at_iff {s : set M} {x : M} : unique_mdiff_within_at I s x ↔ unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ (ext_chart_at I x).target) ((ext_chart_at I x) x) := begin apply unique_diff_within_at_congr, rw [nhds_within_inter, nhds_within_inter, nhds_within_ext_chart_at_target_eq] end lemma unique_mdiff_within_at.mono (h : unique_mdiff_within_at I s x) (st : s ⊆ t) : unique_mdiff_within_at I t x := unique_diff_within_at.mono h $ inter_subset_inter (preimage_mono st) (subset.refl _) lemma unique_mdiff_within_at.inter' (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝[s] x) : unique_mdiff_within_at I (s ∩ t) x := begin rw [unique_mdiff_within_at, ext_chart_at_preimage_inter_eq], exact unique_diff_within_at.inter' hs (ext_chart_at_preimage_mem_nhds_within I x ht) end lemma unique_mdiff_within_at.inter (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝 x) : unique_mdiff_within_at I (s ∩ t) x := begin rw [unique_mdiff_within_at, ext_chart_at_preimage_inter_eq], exact unique_diff_within_at.inter hs (ext_chart_at_preimage_mem_nhds I x ht) end lemma is_open.unique_mdiff_within_at (xs : x ∈ s) (hs : is_open s) : unique_mdiff_within_at I s x := begin have := unique_mdiff_within_at.inter (unique_mdiff_within_at_univ I) (is_open.mem_nhds hs xs), rwa univ_inter at this end lemma unique_mdiff_on.inter (hs : unique_mdiff_on I s) (ht : is_open t) : unique_mdiff_on I (s ∩ t) := λx hx, unique_mdiff_within_at.inter (hs _ hx.1) (is_open.mem_nhds ht hx.2) lemma is_open.unique_mdiff_on (hs : is_open s) : unique_mdiff_on I s := λx hx, is_open.unique_mdiff_within_at hx hs lemma unique_mdiff_on_univ : unique_mdiff_on I (univ : set M) := is_open_univ.unique_mdiff_on /- We name the typeclass variables related to `smooth_manifold_with_corners` structure as they are necessary in lemmas mentioning the derivative, but not in lemmas about differentiability, so we want to include them or omit them when necessary. -/ variables [Is : smooth_manifold_with_corners I M] [I's : smooth_manifold_with_corners I' M'] [I''s : smooth_manifold_with_corners I'' M''] {f' f₀' f₁' : tangent_space I x →L[𝕜] tangent_space I' (f x)} {g' : tangent_space I' (f x) →L[𝕜] tangent_space I'' (g (f x))} /-- `unique_mdiff_within_at` achieves its goal: it implies the uniqueness of the derivative. -/ theorem unique_mdiff_within_at.eq (U : unique_mdiff_within_at I s x) (h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') : f' = f₁' := U.eq h.2 h₁.2 theorem unique_mdiff_on.eq (U : unique_mdiff_on I s) (hx : x ∈ s) (h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') : f' = f₁' := unique_mdiff_within_at.eq (U _ hx) h h₁ /-! ### General lemmas on derivatives of functions between manifolds We mimick the API for functions between vector spaces -/ lemma mdifferentiable_within_at_iff {f : M → M'} {s : set M} {x : M} : mdifferentiable_within_at I I' f s x ↔ continuous_within_at f s x ∧ differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' s) ((ext_chart_at I x) x) := begin refine and_congr iff.rfl (exists_congr $ λ f', _), rw [inter_comm], simp only [has_fderiv_within_at, nhds_within_inter, nhds_within_ext_chart_at_target_eq] end include Is I's /-- One can reformulate differentiability within a set at a point as continuity within this set at this point, and differentiability in any chart containing that point. -/ lemma mdifferentiable_within_at_iff_of_mem_source {x' : M} {y : M'} (hx : x' ∈ (charted_space.chart_at H x).source) (hy : f x' ∈ (charted_space.chart_at H' y).source) : mdifferentiable_within_at I I' f s x' ↔ continuous_within_at f s x' ∧ differentiable_within_at 𝕜 ((ext_chart_at I' y) ∘ f ∘ ((ext_chart_at I x).symm)) (((ext_chart_at I x).symm) ⁻¹' s ∩ set.range I) ((ext_chart_at I x) x') := (differentiable_within_at_local_invariant_prop I I').lift_prop_within_at_indep_chart (structure_groupoid.chart_mem_maximal_atlas _ x) hx (structure_groupoid.chart_mem_maximal_atlas _ y) hy lemma mfderiv_within_zero_of_not_mdifferentiable_within_at (h : ¬ mdifferentiable_within_at I I' f s x) : mfderiv_within I I' f s x = 0 := by simp only [mfderiv_within, h, dif_neg, not_false_iff] lemma mfderiv_zero_of_not_mdifferentiable_at (h : ¬ mdifferentiable_at I I' f x) : mfderiv I I' f x = 0 := by simp only [mfderiv, h, dif_neg, not_false_iff] theorem has_mfderiv_within_at.mono (h : has_mfderiv_within_at I I' f t x f') (hst : s ⊆ t) : has_mfderiv_within_at I I' f s x f' := ⟨ continuous_within_at.mono h.1 hst, has_fderiv_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩ theorem has_mfderiv_at.has_mfderiv_within_at (h : has_mfderiv_at I I' f x f') : has_mfderiv_within_at I I' f s x f' := ⟨ continuous_at.continuous_within_at h.1, has_fderiv_within_at.mono h.2 (inter_subset_right _ _) ⟩ lemma has_mfderiv_within_at.mdifferentiable_within_at (h : has_mfderiv_within_at I I' f s x f') : mdifferentiable_within_at I I' f s x := ⟨h.1, ⟨f', h.2⟩⟩ lemma has_mfderiv_at.mdifferentiable_at (h : has_mfderiv_at I I' f x f') : mdifferentiable_at I I' f x := ⟨h.1, ⟨f', h.2⟩⟩ @[simp, mfld_simps] lemma has_mfderiv_within_at_univ : has_mfderiv_within_at I I' f univ x f' ↔ has_mfderiv_at I I' f x f' := by simp only [has_mfderiv_within_at, has_mfderiv_at, continuous_within_at_univ] with mfld_simps theorem has_mfderiv_at_unique (h₀ : has_mfderiv_at I I' f x f₀') (h₁ : has_mfderiv_at I I' f x f₁') : f₀' = f₁' := begin rw ← has_mfderiv_within_at_univ at h₀ h₁, exact (unique_mdiff_within_at_univ I).eq h₀ h₁ end lemma has_mfderiv_within_at_inter' (h : t ∈ 𝓝[s] x) : has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' := begin rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_at_preimage_inter_eq, has_fderiv_within_at_inter', continuous_within_at_inter' h], exact ext_chart_at_preimage_mem_nhds_within I x h, end lemma has_mfderiv_within_at_inter (h : t ∈ 𝓝 x) : has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' := begin rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_at_preimage_inter_eq, has_fderiv_within_at_inter, continuous_within_at_inter h], exact ext_chart_at_preimage_mem_nhds I x h, end lemma has_mfderiv_within_at.union (hs : has_mfderiv_within_at I I' f s x f') (ht : has_mfderiv_within_at I I' f t x f') : has_mfderiv_within_at I I' f (s ∪ t) x f' := begin split, { exact continuous_within_at.union hs.1 ht.1 }, { convert has_fderiv_within_at.union hs.2 ht.2, simp only [union_inter_distrib_right, preimage_union] } end lemma has_mfderiv_within_at.nhds_within (h : has_mfderiv_within_at I I' f s x f') (ht : s ∈ 𝓝[t] x) : has_mfderiv_within_at I I' f t x f' := (has_mfderiv_within_at_inter' ht).1 (h.mono (inter_subset_right _ _)) lemma has_mfderiv_within_at.has_mfderiv_at (h : has_mfderiv_within_at I I' f s x f') (hs : s ∈ 𝓝 x) : has_mfderiv_at I I' f x f' := by rwa [← univ_inter s, has_mfderiv_within_at_inter hs, has_mfderiv_within_at_univ] at h lemma mdifferentiable_within_at.has_mfderiv_within_at (h : mdifferentiable_within_at I I' f s x) : has_mfderiv_within_at I I' f s x (mfderiv_within I I' f s x) := begin refine ⟨h.1, _⟩, simp only [mfderiv_within, h, dif_pos] with mfld_simps, exact differentiable_within_at.has_fderiv_within_at h.2 end lemma mdifferentiable_within_at.mfderiv_within (h : mdifferentiable_within_at I I' f s x) : (mfderiv_within I I' f s x) = fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) := by simp only [mfderiv_within, h, dif_pos] lemma mdifferentiable_at.has_mfderiv_at (h : mdifferentiable_at I I' f x) : has_mfderiv_at I I' f x (mfderiv I I' f x) := begin refine ⟨h.1, _⟩, simp only [mfderiv, h, dif_pos] with mfld_simps, exact differentiable_within_at.has_fderiv_within_at h.2 end lemma mdifferentiable_at.mfderiv (h : mdifferentiable_at I I' f x) : (mfderiv I I' f x) = fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) (range I) ((ext_chart_at I x) x) := by simp only [mfderiv, h, dif_pos] lemma has_mfderiv_at.mfderiv (h : has_mfderiv_at I I' f x f') : mfderiv I I' f x = f' := (has_mfderiv_at_unique h h.mdifferentiable_at.has_mfderiv_at).symm lemma has_mfderiv_within_at.mfderiv_within (h : has_mfderiv_within_at I I' f s x f') (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' f s x = f' := by { ext, rw hxs.eq h h.mdifferentiable_within_at.has_mfderiv_within_at } lemma mdifferentiable.mfderiv_within (h : mdifferentiable_at I I' f x) (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' f s x = mfderiv I I' f x := begin apply has_mfderiv_within_at.mfderiv_within _ hxs, exact h.has_mfderiv_at.has_mfderiv_within_at end lemma mfderiv_within_subset (st : s ⊆ t) (hs : unique_mdiff_within_at I s x) (h : mdifferentiable_within_at I I' f t x) : mfderiv_within I I' f s x = mfderiv_within I I' f t x := ((mdifferentiable_within_at.has_mfderiv_within_at h).mono st).mfderiv_within hs omit Is I's lemma mdifferentiable_within_at.mono (hst : s ⊆ t) (h : mdifferentiable_within_at I I' f t x) : mdifferentiable_within_at I I' f s x := ⟨ continuous_within_at.mono h.1 hst, differentiable_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩ lemma mdifferentiable_within_at_univ : mdifferentiable_within_at I I' f univ x ↔ mdifferentiable_at I I' f x := by simp only [mdifferentiable_within_at, mdifferentiable_at, continuous_within_at_univ] with mfld_simps lemma mdifferentiable_within_at_inter (ht : t ∈ 𝓝 x) : mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x := begin rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_at_preimage_inter_eq, differentiable_within_at_inter, continuous_within_at_inter ht], exact ext_chart_at_preimage_mem_nhds I x ht end lemma mdifferentiable_within_at_inter' (ht : t ∈ 𝓝[s] x) : mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x := begin rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_at_preimage_inter_eq, differentiable_within_at_inter', continuous_within_at_inter' ht], exact ext_chart_at_preimage_mem_nhds_within I x ht end lemma mdifferentiable_at.mdifferentiable_within_at (h : mdifferentiable_at I I' f x) : mdifferentiable_within_at I I' f s x := mdifferentiable_within_at.mono (subset_univ _) (mdifferentiable_within_at_univ.2 h) lemma mdifferentiable_within_at.mdifferentiable_at (h : mdifferentiable_within_at I I' f s x) (hs : s ∈ 𝓝 x) : mdifferentiable_at I I' f x := begin have : s = univ ∩ s, by rw univ_inter, rwa [this, mdifferentiable_within_at_inter hs, mdifferentiable_within_at_univ] at h, end lemma mdifferentiable_on.mono (h : mdifferentiable_on I I' f t) (st : s ⊆ t) : mdifferentiable_on I I' f s := λx hx, (h x (st hx)).mono st lemma mdifferentiable_on_univ : mdifferentiable_on I I' f univ ↔ mdifferentiable I I' f := by { simp only [mdifferentiable_on, mdifferentiable_within_at_univ] with mfld_simps, refl } lemma mdifferentiable.mdifferentiable_on (h : mdifferentiable I I' f) : mdifferentiable_on I I' f s := (mdifferentiable_on_univ.2 h).mono (subset_univ _) lemma mdifferentiable_on_of_locally_mdifferentiable_on (h : ∀x∈s, ∃u, is_open u ∧ x ∈ u ∧ mdifferentiable_on I I' f (s ∩ u)) : mdifferentiable_on I I' f s := begin assume x xs, rcases h x xs with ⟨t, t_open, xt, ht⟩, exact (mdifferentiable_within_at_inter (is_open.mem_nhds t_open xt)).1 (ht x ⟨xs, xt⟩) end include Is I's @[simp, mfld_simps] lemma mfderiv_within_univ : mfderiv_within I I' f univ = mfderiv I I' f := begin ext x : 1, simp only [mfderiv_within, mfderiv] with mfld_simps, rw mdifferentiable_within_at_univ end lemma mfderiv_within_inter (ht : t ∈ 𝓝 x) (hs : unique_mdiff_within_at I s x) : mfderiv_within I I' f (s ∩ t) x = mfderiv_within I I' f s x := by rw [mfderiv_within, mfderiv_within, ext_chart_at_preimage_inter_eq, mdifferentiable_within_at_inter ht, fderiv_within_inter (ext_chart_at_preimage_mem_nhds I x ht) hs] lemma mdifferentiable_at_iff_of_mem_source {x' : M} {y : M'} (hx : x' ∈ (charted_space.chart_at H x).source) (hy : f x' ∈ (charted_space.chart_at H' y).source) : mdifferentiable_at I I' f x' ↔ continuous_at f x' ∧ differentiable_within_at 𝕜 ((ext_chart_at I' y) ∘ f ∘ ((ext_chart_at I x).symm)) (set.range I) ((ext_chart_at I x) x') := mdifferentiable_within_at_univ.symm.trans $ (mdifferentiable_within_at_iff_of_mem_source hx hy).trans $ by rw [continuous_within_at_univ, set.preimage_univ, set.univ_inter] omit Is I's /-! ### Deriving continuity from differentiability on manifolds -/ theorem has_mfderiv_within_at.continuous_within_at (h : has_mfderiv_within_at I I' f s x f') : continuous_within_at f s x := h.1 theorem has_mfderiv_at.continuous_at (h : has_mfderiv_at I I' f x f') : continuous_at f x := h.1 lemma mdifferentiable_within_at.continuous_within_at (h : mdifferentiable_within_at I I' f s x) : continuous_within_at f s x := h.1 lemma mdifferentiable_at.continuous_at (h : mdifferentiable_at I I' f x) : continuous_at f x := h.1 lemma mdifferentiable_on.continuous_on (h : mdifferentiable_on I I' f s) : continuous_on f s := λx hx, (h x hx).continuous_within_at lemma mdifferentiable.continuous (h : mdifferentiable I I' f) : continuous f := continuous_iff_continuous_at.2 $ λx, (h x).continuous_at include Is I's lemma tangent_map_within_subset {p : tangent_bundle I M} (st : s ⊆ t) (hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_within_at I I' f t p.1) : tangent_map_within I I' f s p = tangent_map_within I I' f t p := begin simp only [tangent_map_within] with mfld_simps, rw mfderiv_within_subset st hs h, end lemma tangent_map_within_univ : tangent_map_within I I' f univ = tangent_map I I' f := by { ext p : 1, simp only [tangent_map_within, tangent_map] with mfld_simps } lemma tangent_map_within_eq_tangent_map {p : tangent_bundle I M} (hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_at I I' f p.1) : tangent_map_within I I' f s p = tangent_map I I' f p := begin rw ← mdifferentiable_within_at_univ at h, rw ← tangent_map_within_univ, exact tangent_map_within_subset (subset_univ _) hs h, end @[simp, mfld_simps] lemma tangent_map_within_tangent_bundle_proj {p : tangent_bundle I M} : tangent_bundle.proj I' M' (tangent_map_within I I' f s p) = f (tangent_bundle.proj I M p) := rfl @[simp, mfld_simps] lemma tangent_map_within_proj {p : tangent_bundle I M} : (tangent_map_within I I' f s p).1 = f p.1 := rfl @[simp, mfld_simps] lemma tangent_map_tangent_bundle_proj {p : tangent_bundle I M} : tangent_bundle.proj I' M' (tangent_map I I' f p) = f (tangent_bundle.proj I M p) := rfl @[simp, mfld_simps] lemma tangent_map_proj {p : tangent_bundle I M} : (tangent_map I I' f p).1 = f p.1 := rfl omit Is I's /-! ### Congruence lemmas for derivatives on manifolds -/ lemma has_mfderiv_within_at.congr_of_eventually_eq (h : has_mfderiv_within_at I I' f s x f') (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : has_mfderiv_within_at I I' f₁ s x f' := begin refine ⟨continuous_within_at.congr_of_eventually_eq h.1 h₁ hx, _⟩, apply has_fderiv_within_at.congr_of_eventually_eq h.2, { have : (ext_chart_at I x).symm ⁻¹' {y | f₁ y = f y} ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := ext_chart_at_preimage_mem_nhds_within I x h₁, apply filter.mem_of_superset this (λy, _), simp only [hx] with mfld_simps {contextual := tt} }, { simp only [hx] with mfld_simps }, end lemma has_mfderiv_within_at.congr_mono (h : has_mfderiv_within_at I I' f s x f') (ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : has_mfderiv_within_at I I' f₁ t x f' := (h.mono h₁).congr_of_eventually_eq (filter.mem_inf_of_right ht) hx lemma has_mfderiv_at.congr_of_eventually_eq (h : has_mfderiv_at I I' f x f') (h₁ : f₁ =ᶠ[𝓝 x] f) : has_mfderiv_at I I' f₁ x f' := begin rw ← has_mfderiv_within_at_univ at ⊢ h, apply h.congr_of_eventually_eq _ (mem_of_mem_nhds h₁ : _), rwa nhds_within_univ end include Is I's lemma mdifferentiable_within_at.congr_of_eventually_eq (h : mdifferentiable_within_at I I' f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x := (h.has_mfderiv_within_at.congr_of_eventually_eq h₁ hx).mdifferentiable_within_at variables (I I') lemma filter.eventually_eq.mdifferentiable_within_at_iff (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f s x ↔ mdifferentiable_within_at I I' f₁ s x := begin split, { assume h, apply h.congr_of_eventually_eq h₁ hx }, { assume h, apply h.congr_of_eventually_eq _ hx.symm, apply h₁.mono, intro y, apply eq.symm } end variables {I I'} lemma mdifferentiable_within_at.congr_mono (h : mdifferentiable_within_at I I' f s x) (ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : mdifferentiable_within_at I I' f₁ t x := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx h₁).mdifferentiable_within_at lemma mdifferentiable_within_at.congr (h : mdifferentiable_within_at I I' f s x) (ht : ∀x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx (subset.refl _)).mdifferentiable_within_at lemma mdifferentiable_on.congr_mono (h : mdifferentiable_on I I' f s) (h' : ∀x ∈ t, f₁ x = f x) (h₁ : t ⊆ s) : mdifferentiable_on I I' f₁ t := λ x hx, (h x (h₁ hx)).congr_mono h' (h' x hx) h₁ lemma mdifferentiable_at.congr_of_eventually_eq (h : mdifferentiable_at I I' f x) (hL : f₁ =ᶠ[𝓝 x] f) : mdifferentiable_at I I' f₁ x := ((h.has_mfderiv_at).congr_of_eventually_eq hL).mdifferentiable_at lemma mdifferentiable_within_at.mfderiv_within_congr_mono (h : mdifferentiable_within_at I I' f s x) (hs : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (hxt : unique_mdiff_within_at I t x) (h₁ : t ⊆ s) : mfderiv_within I I' f₁ t x = (mfderiv_within I I' f s x : _) := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at hs hx h₁).mfderiv_within hxt lemma filter.eventually_eq.mfderiv_within_eq (hs : unique_mdiff_within_at I s x) (hL : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) := begin by_cases h : mdifferentiable_within_at I I' f s x, { exact ((h.has_mfderiv_within_at).congr_of_eventually_eq hL hx).mfderiv_within hs }, { unfold mfderiv_within, rw [dif_neg h, dif_neg], rwa ← hL.mdifferentiable_within_at_iff I I' hx } end lemma mfderiv_within_congr (hs : unique_mdiff_within_at I s x) (hL : ∀ x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) := filter.eventually_eq.mfderiv_within_eq hs (filter.eventually_eq_of_mem (self_mem_nhds_within) hL) hx lemma tangent_map_within_congr (h : ∀ x ∈ s, f x = f₁ x) (p : tangent_bundle I M) (hp : p.1 ∈ s) (hs : unique_mdiff_within_at I s p.1) : tangent_map_within I I' f s p = tangent_map_within I I' f₁ s p := begin simp only [tangent_map_within, h p.fst hp, true_and, eq_self_iff_true, heq_iff_eq, sigma.mk.inj_iff], congr' 1, exact mfderiv_within_congr hs h (h _ hp) end lemma filter.eventually_eq.mfderiv_eq (hL : f₁ =ᶠ[𝓝 x] f) : mfderiv I I' f₁ x = (mfderiv I I' f x : _) := begin have A : f₁ x = f x := (mem_of_mem_nhds hL : _), rw [← mfderiv_within_univ, ← mfderiv_within_univ], rw ← nhds_within_univ at hL, exact hL.mfderiv_within_eq (unique_mdiff_within_at_univ I) A end /-! ### Composition lemmas -/ omit Is I's lemma written_in_ext_chart_comp (h : continuous_within_at f s x) : {y | written_in_ext_chart_at I I'' x (g ∘ f) y = ((written_in_ext_chart_at I' I'' (f x) g) ∘ (written_in_ext_chart_at I I' x f)) y} ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := begin apply @filter.mem_of_superset _ _ ((f ∘ (ext_chart_at I x).symm)⁻¹' (ext_chart_at I' (f x)).source) _ (ext_chart_at_preimage_mem_nhds_within I x (h.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))), mfld_set_tac, end variable (x) include Is I's I''s theorem has_mfderiv_within_at.comp (hg : has_mfderiv_within_at I' I'' g u (f x) g') (hf : has_mfderiv_within_at I I' f s x f') (hst : s ⊆ f ⁻¹' u) : has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') := begin refine ⟨continuous_within_at.comp hg.1 hf.1 hst, _⟩, have A : has_fderiv_within_at ((written_in_ext_chart_at I' I'' (f x) g) ∘ (written_in_ext_chart_at I I' x f)) (continuous_linear_map.comp g' f' : E →L[𝕜] E'') ((ext_chart_at I x).symm ⁻¹' s ∩ range (I)) ((ext_chart_at I x) x), { have : (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' (f x)).source) ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := (ext_chart_at_preimage_mem_nhds_within I x (hf.1.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))), unfold has_mfderiv_within_at at *, rw [← has_fderiv_within_at_inter' this, ← ext_chart_at_preimage_inter_eq] at hf ⊢, have : written_in_ext_chart_at I I' x f ((ext_chart_at I x) x) = (ext_chart_at I' (f x)) (f x), by simp only with mfld_simps, rw ← this at hg, apply has_fderiv_within_at.comp ((ext_chart_at I x) x) hg.2 hf.2 _, assume y hy, simp only with mfld_simps at hy, have : f (((chart_at H x).symm : H → M) (I.symm y)) ∈ u := hst hy.1.1, simp only [hy, this] with mfld_simps }, apply A.congr_of_eventually_eq (written_in_ext_chart_comp hf.1), simp only with mfld_simps end /-- The chain rule. -/ theorem has_mfderiv_at.comp (hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_at I I' f x f') : has_mfderiv_at I I'' (g ∘ f) x (g'.comp f') := begin rw ← has_mfderiv_within_at_univ at *, exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ end theorem has_mfderiv_at.comp_has_mfderiv_within_at (hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_within_at I I' f s x f') : has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') := begin rw ← has_mfderiv_within_at_univ at *, exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ end lemma mdifferentiable_within_at.comp (hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x) (h : s ⊆ f ⁻¹' u) : mdifferentiable_within_at I I'' (g ∘ f) s x := begin rcases hf.2 with ⟨f', hf'⟩, have F : has_mfderiv_within_at I I' f s x f' := ⟨hf.1, hf'⟩, rcases hg.2 with ⟨g', hg'⟩, have G : has_mfderiv_within_at I' I'' g u (f x) g' := ⟨hg.1, hg'⟩, exact (has_mfderiv_within_at.comp x G F h).mdifferentiable_within_at end lemma mdifferentiable_at.comp (hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) : mdifferentiable_at I I'' (g ∘ f) x := (hg.has_mfderiv_at.comp x hf.has_mfderiv_at).mdifferentiable_at lemma mfderiv_within_comp (hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x) (h : s ⊆ f ⁻¹' u) (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I'' (g ∘ f) s x = (mfderiv_within I' I'' g u (f x)).comp (mfderiv_within I I' f s x) := begin apply has_mfderiv_within_at.mfderiv_within _ hxs, exact has_mfderiv_within_at.comp x hg.has_mfderiv_within_at hf.has_mfderiv_within_at h end lemma mfderiv_comp (hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) : mfderiv I I'' (g ∘ f) x = (mfderiv I' I'' g (f x)).comp (mfderiv I I' f x) := begin apply has_mfderiv_at.mfderiv, exact has_mfderiv_at.comp x hg.has_mfderiv_at hf.has_mfderiv_at end lemma mdifferentiable_on.comp (hg : mdifferentiable_on I' I'' g u) (hf : mdifferentiable_on I I' f s) (st : s ⊆ f ⁻¹' u) : mdifferentiable_on I I'' (g ∘ f) s := λx hx, mdifferentiable_within_at.comp x (hg (f x) (st hx)) (hf x hx) st lemma mdifferentiable.comp (hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) : mdifferentiable I I'' (g ∘ f) := λx, mdifferentiable_at.comp x (hg (f x)) (hf x) lemma tangent_map_within_comp_at (p : tangent_bundle I M) (hg : mdifferentiable_within_at I' I'' g u (f p.1)) (hf : mdifferentiable_within_at I I' f s p.1) (h : s ⊆ f ⁻¹' u) (hps : unique_mdiff_within_at I s p.1) : tangent_map_within I I'' (g ∘ f) s p = tangent_map_within I' I'' g u (tangent_map_within I I' f s p) := begin simp only [tangent_map_within] with mfld_simps, rw mfderiv_within_comp p.1 hg hf h hps, refl end lemma tangent_map_comp_at (p : tangent_bundle I M) (hg : mdifferentiable_at I' I'' g (f p.1)) (hf : mdifferentiable_at I I' f p.1) : tangent_map I I'' (g ∘ f) p = tangent_map I' I'' g (tangent_map I I' f p) := begin simp only [tangent_map] with mfld_simps, rw mfderiv_comp p.1 hg hf, refl end lemma tangent_map_comp (hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) : tangent_map I I'' (g ∘ f) = (tangent_map I' I'' g) ∘ (tangent_map I I' f) := by { ext p : 1, exact tangent_map_comp_at _ (hg _) (hf _) } end derivatives_properties section mfderiv_fderiv /-! ### Relations between vector space derivative and manifold derivative The manifold derivative `mfderiv`, when considered on the model vector space with its trivial manifold structure, coincides with the usual Frechet derivative `fderiv`. In this section, we prove this and related statements. -/ variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E'] {f : E → E'} {s : set E} {x : E} lemma unique_mdiff_within_at_iff_unique_diff_within_at : unique_mdiff_within_at (𝓘(𝕜, E)) s x ↔ unique_diff_within_at 𝕜 s x := by simp only [unique_mdiff_within_at] with mfld_simps alias unique_mdiff_within_at_iff_unique_diff_within_at ↔ unique_mdiff_within_at.unique_diff_within_at unique_diff_within_at.unique_mdiff_within_at lemma unique_mdiff_on_iff_unique_diff_on : unique_mdiff_on (𝓘(𝕜, E)) s ↔ unique_diff_on 𝕜 s := by simp [unique_mdiff_on, unique_diff_on, unique_mdiff_within_at_iff_unique_diff_within_at] alias unique_mdiff_on_iff_unique_diff_on ↔ unique_mdiff_on.unique_diff_on unique_diff_on.unique_mdiff_on @[simp, mfld_simps] lemma written_in_ext_chart_model_space : written_in_ext_chart_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) x f = f := rfl lemma has_mfderiv_within_at_iff_has_fderiv_within_at {f'} : has_mfderiv_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x f' ↔ has_fderiv_within_at f f' s x := by simpa only [has_mfderiv_within_at, and_iff_right_iff_imp] with mfld_simps using has_fderiv_within_at.continuous_within_at alias has_mfderiv_within_at_iff_has_fderiv_within_at ↔ has_mfderiv_within_at.has_fderiv_within_at has_fderiv_within_at.has_mfderiv_within_at lemma has_mfderiv_at_iff_has_fderiv_at {f'} : has_mfderiv_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x f' ↔ has_fderiv_at f f' x := by rw [← has_mfderiv_within_at_univ, has_mfderiv_within_at_iff_has_fderiv_within_at, has_fderiv_within_at_univ] alias has_mfderiv_at_iff_has_fderiv_at ↔ has_mfderiv_at.has_fderiv_at has_fderiv_at.has_mfderiv_at /-- For maps between vector spaces, `mdifferentiable_within_at` and `fdifferentiable_within_at` coincide -/ theorem mdifferentiable_within_at_iff_differentiable_within_at : mdifferentiable_within_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s x ↔ differentiable_within_at 𝕜 f s x := begin simp only [mdifferentiable_within_at] with mfld_simps, exact ⟨λH, H.2, λH, ⟨H.continuous_within_at, H⟩⟩ end alias mdifferentiable_within_at_iff_differentiable_within_at ↔ mdifferentiable_within_at.differentiable_within_at differentiable_within_at.mdifferentiable_within_at /-- For maps between vector spaces, `mdifferentiable_at` and `differentiable_at` coincide -/ theorem mdifferentiable_at_iff_differentiable_at : mdifferentiable_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) f x ↔ differentiable_at 𝕜 f x := begin simp only [mdifferentiable_at, differentiable_within_at_univ] with mfld_simps, exact ⟨λH, H.2, λH, ⟨H.continuous_at, H⟩⟩ end alias mdifferentiable_at_iff_differentiable_at ↔ mdifferentiable_at.differentiable_at differentiable_at.mdifferentiable_at /-- For maps between vector spaces, `mdifferentiable_on` and `differentiable_on` coincide -/ theorem mdifferentiable_on_iff_differentiable_on : mdifferentiable_on (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s ↔ differentiable_on 𝕜 f s := by simp only [mdifferentiable_on, differentiable_on, mdifferentiable_within_at_iff_differentiable_within_at] alias mdifferentiable_on_iff_differentiable_on ↔ mdifferentiable_on.differentiable_on differentiable_on.mdifferentiable_on /-- For maps between vector spaces, `mdifferentiable` and `differentiable` coincide -/ theorem mdifferentiable_iff_differentiable : mdifferentiable (𝓘(𝕜, E)) (𝓘(𝕜, E')) f ↔ differentiable 𝕜 f := by simp only [mdifferentiable, differentiable, mdifferentiable_at_iff_differentiable_at] alias mdifferentiable_iff_differentiable ↔ mdifferentiable.differentiable differentiable.mdifferentiable /-- For maps between vector spaces, `mfderiv_within` and `fderiv_within` coincide -/ @[simp] theorem mfderiv_within_eq_fderiv_within : mfderiv_within (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s x = fderiv_within 𝕜 f s x := begin by_cases h : mdifferentiable_within_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s x, { simp only [mfderiv_within, h, dif_pos] with mfld_simps }, { simp only [mfderiv_within, h, dif_neg, not_false_iff], rw [mdifferentiable_within_at_iff_differentiable_within_at] at h, exact (fderiv_within_zero_of_not_differentiable_within_at h).symm } end /-- For maps between vector spaces, `mfderiv` and `fderiv` coincide -/ @[simp] theorem mfderiv_eq_fderiv : mfderiv (𝓘(𝕜, E)) (𝓘(𝕜, E')) f x = fderiv 𝕜 f x := begin rw [← mfderiv_within_univ, ← fderiv_within_univ], exact mfderiv_within_eq_fderiv_within end end mfderiv_fderiv section specific_functions /-! ### Differentiability of specific functions -/ variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H') {M' : Type*} [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M'] namespace continuous_linear_map variables (f : E →L[𝕜] E') {s : set E} {x : E} protected lemma has_mfderiv_within_at : has_mfderiv_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x f := f.has_fderiv_within_at.has_mfderiv_within_at protected lemma has_mfderiv_at : has_mfderiv_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x f := f.has_fderiv_at.has_mfderiv_at protected lemma mdifferentiable_within_at : mdifferentiable_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x := f.differentiable_within_at.mdifferentiable_within_at protected lemma mdifferentiable_on : mdifferentiable_on 𝓘(𝕜, E) 𝓘(𝕜, E') f s := f.differentiable_on.mdifferentiable_on protected lemma mdifferentiable_at : mdifferentiable_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x := f.differentiable_at.mdifferentiable_at protected lemma mdifferentiable : mdifferentiable 𝓘(𝕜, E) 𝓘(𝕜, E') f := f.differentiable.mdifferentiable lemma mfderiv_eq : mfderiv 𝓘(𝕜, E) 𝓘(𝕜, E') f x = f := f.has_mfderiv_at.mfderiv lemma mfderiv_within_eq (hs : unique_mdiff_within_at 𝓘(𝕜, E) s x) : mfderiv_within 𝓘(𝕜, E) 𝓘(𝕜, E') f s x = f := f.has_mfderiv_within_at.mfderiv_within hs end continuous_linear_map namespace continuous_linear_equiv variables (f : E ≃L[𝕜] E') {s : set E} {x : E} protected lemma has_mfderiv_within_at : has_mfderiv_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x (f : E →L[𝕜] E') := f.has_fderiv_within_at.has_mfderiv_within_at protected lemma has_mfderiv_at : has_mfderiv_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x (f : E →L[𝕜] E') := f.has_fderiv_at.has_mfderiv_at protected lemma mdifferentiable_within_at : mdifferentiable_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x := f.differentiable_within_at.mdifferentiable_within_at protected lemma mdifferentiable_on : mdifferentiable_on 𝓘(𝕜, E) 𝓘(𝕜, E') f s := f.differentiable_on.mdifferentiable_on protected lemma mdifferentiable_at : mdifferentiable_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x := f.differentiable_at.mdifferentiable_at protected lemma mdifferentiable : mdifferentiable 𝓘(𝕜, E) 𝓘(𝕜, E') f := f.differentiable.mdifferentiable lemma mfderiv_eq : mfderiv 𝓘(𝕜, E) 𝓘(𝕜, E') f x = (f : E →L[𝕜] E') := f.has_mfderiv_at.mfderiv lemma mfderiv_within_eq (hs : unique_mdiff_within_at 𝓘(𝕜, E) s x) : mfderiv_within 𝓘(𝕜, E) 𝓘(𝕜, E') f s x = (f : E →L[𝕜] E') := f.has_mfderiv_within_at.mfderiv_within hs end continuous_linear_equiv variables {s : set M} {x : M} section id /-! #### Identity -/ lemma has_mfderiv_at_id (x : M) : has_mfderiv_at I I (@_root_.id M) x (continuous_linear_map.id 𝕜 (tangent_space I x)) := begin refine ⟨continuous_id.continuous_at, _⟩, have : ∀ᶠ y in 𝓝[range I] ((ext_chart_at I x) x), ((ext_chart_at I x) ∘ (ext_chart_at I x).symm) y = id y, { apply filter.mem_of_superset (ext_chart_at_target_mem_nhds_within I x), mfld_set_tac }, apply has_fderiv_within_at.congr_of_eventually_eq (has_fderiv_within_at_id _ _) this, simp only with mfld_simps end theorem has_mfderiv_within_at_id (s : set M) (x : M) : has_mfderiv_within_at I I (@_root_.id M) s x (continuous_linear_map.id 𝕜 (tangent_space I x)) := (has_mfderiv_at_id I x).has_mfderiv_within_at lemma mdifferentiable_at_id : mdifferentiable_at I I (@_root_.id M) x := (has_mfderiv_at_id I x).mdifferentiable_at lemma mdifferentiable_within_at_id : mdifferentiable_within_at I I (@_root_.id M) s x := (mdifferentiable_at_id I).mdifferentiable_within_at lemma mdifferentiable_id : mdifferentiable I I (@_root_.id M) := λx, mdifferentiable_at_id I lemma mdifferentiable_on_id : mdifferentiable_on I I (@_root_.id M) s := (mdifferentiable_id I).mdifferentiable_on @[simp, mfld_simps] lemma mfderiv_id : mfderiv I I (@_root_.id M) x = (continuous_linear_map.id 𝕜 (tangent_space I x)) := has_mfderiv_at.mfderiv (has_mfderiv_at_id I x) lemma mfderiv_within_id (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I (@_root_.id M) s x = (continuous_linear_map.id 𝕜 (tangent_space I x)) := begin rw mdifferentiable.mfderiv_within (mdifferentiable_at_id I) hxs, exact mfderiv_id I end @[simp, mfld_simps] lemma tangent_map_id : tangent_map I I (id : M → M) = id := by { ext1 ⟨x, v⟩, simp [tangent_map] } lemma tangent_map_within_id {p : tangent_bundle I M} (hs : unique_mdiff_within_at I s (tangent_bundle.proj I M p)) : tangent_map_within I I (id : M → M) s p = p := begin simp only [tangent_map_within, id.def], rw mfderiv_within_id, { rcases p, refl }, { exact hs } end end id section const /-! #### Constants -/ variables {c : M'} lemma has_mfderiv_at_const (c : M') (x : M) : has_mfderiv_at I I' (λy : M, c) x (0 : tangent_space I x →L[𝕜] tangent_space I' c) := begin refine ⟨continuous_const.continuous_at, _⟩, simp only [written_in_ext_chart_at, (∘), has_fderiv_within_at_const] end theorem has_mfderiv_within_at_const (c : M') (s : set M) (x : M) : has_mfderiv_within_at I I' (λy : M, c) s x (0 : tangent_space I x →L[𝕜] tangent_space I' c) := (has_mfderiv_at_const I I' c x).has_mfderiv_within_at lemma mdifferentiable_at_const : mdifferentiable_at I I' (λy : M, c) x := (has_mfderiv_at_const I I' c x).mdifferentiable_at lemma mdifferentiable_within_at_const : mdifferentiable_within_at I I' (λy : M, c) s x := (mdifferentiable_at_const I I').mdifferentiable_within_at lemma mdifferentiable_const : mdifferentiable I I' (λy : M, c) := λx, mdifferentiable_at_const I I' lemma mdifferentiable_on_const : mdifferentiable_on I I' (λy : M, c) s := (mdifferentiable_const I I').mdifferentiable_on @[simp, mfld_simps] lemma mfderiv_const : mfderiv I I' (λy : M, c) x = (0 : tangent_space I x →L[𝕜] tangent_space I' c) := has_mfderiv_at.mfderiv (has_mfderiv_at_const I I' c x) lemma mfderiv_within_const (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' (λy : M, c) s x = (0 : tangent_space I x →L[𝕜] tangent_space I' c) := (has_mfderiv_within_at_const _ _ _ _ _).mfderiv_within hxs end const section arithmetic /-! #### Arithmetic Note that in the in `has_mfderiv_at` lemmas there is an abuse of the defeq between `E'` and `tangent_space 𝓘(𝕜, E') (f z)` (similarly for `g',F',p',q'`). In general this defeq is not canonical, but in this case (the tangent space of a vector space) it is canonical. -/ variables { z : M} {F' : Type*} [normed_comm_ring F'] [normed_algebra 𝕜 F'] {f g : M → E'} {p q : M → F'} {I} {f' g' : tangent_space I z →L[𝕜] E'} {p' q' : tangent_space I z →L[𝕜] F'} lemma has_mfderiv_at.add (hf : has_mfderiv_at I 𝓘(𝕜, E') f z f') (hg : has_mfderiv_at I 𝓘(𝕜, E') g z g') : has_mfderiv_at I 𝓘(𝕜, E') (f + g) z (f' + g') := ⟨hf.1.add hg.1, hf.2.add hg.2⟩ lemma mdifferentiable_at.add (hf : mdifferentiable_at I 𝓘(𝕜, E') f z) (hg : mdifferentiable_at I 𝓘(𝕜, E') g z) : mdifferentiable_at I 𝓘(𝕜, E') (f + g) z := (hf.has_mfderiv_at.add hg.has_mfderiv_at).mdifferentiable_at lemma mdifferentiable.add (hf : mdifferentiable I 𝓘(𝕜, E') f) (hg : mdifferentiable I 𝓘(𝕜, E') g) : mdifferentiable I 𝓘(𝕜, E') (f + g) := λ x, (hf x).add (hg x) lemma has_mfderiv_at.mul (hp : has_mfderiv_at I 𝓘(𝕜, F') p z p') (hq : has_mfderiv_at I 𝓘(𝕜, F') q z q') : has_mfderiv_at I 𝓘(𝕜, F') (p * q) z (p z • q' + q z • p' : E →L[𝕜] F') := ⟨hp.1.mul hq.1, by simpa only with mfld_simps using hp.2.mul hq.2⟩ lemma mdifferentiable_at.mul (hp : mdifferentiable_at I 𝓘(𝕜, F') p z) (hq : mdifferentiable_at I 𝓘(𝕜, F') q z) : mdifferentiable_at I 𝓘(𝕜, F') (p * q) z := (hp.has_mfderiv_at.mul hq.has_mfderiv_at).mdifferentiable_at lemma mdifferentiable.mul {f g : M → F'} (hf : mdifferentiable I 𝓘(𝕜, F') f) (hg : mdifferentiable I 𝓘(𝕜, F') g) : mdifferentiable I 𝓘(𝕜, F') (f * g) := λ x, (hf x).mul (hg x) lemma has_mfderiv_at.const_smul (hf : has_mfderiv_at I 𝓘(𝕜, E') f z f') (s : 𝕜) : has_mfderiv_at I 𝓘(𝕜, E') (s • f) z (s • f') := ⟨hf.1.const_smul s, hf.2.const_smul s⟩ lemma mdifferentiable_at.const_smul (hf : mdifferentiable_at I 𝓘(𝕜, E') f z) (s : 𝕜) : mdifferentiable_at I 𝓘(𝕜, E') (s • f) z := (hf.has_mfderiv_at.const_smul s).mdifferentiable_at lemma mdifferentiable.const_smul {f : M → E'} (s : 𝕜) (hf : mdifferentiable I 𝓘(𝕜, E') f) : mdifferentiable I 𝓘(𝕜, E') (s • f) := λ x, (hf x).const_smul s lemma has_mfderiv_at.neg (hf : has_mfderiv_at I 𝓘(𝕜, E') f z f') : has_mfderiv_at I 𝓘(𝕜, E') (-f) z (-f') := ⟨hf.1.neg, hf.2.neg⟩ lemma mdifferentiable_at.neg (hf : mdifferentiable_at I 𝓘(𝕜, E') f z) : mdifferentiable_at I 𝓘(𝕜, E') (-f) z := (hf.has_mfderiv_at.neg ).mdifferentiable_at lemma mdifferentiable.neg {f : M → E'} (hf : mdifferentiable I 𝓘(𝕜, E') f) : mdifferentiable I 𝓘(𝕜, E') (-f) := λ x, (hf x).neg lemma has_mfderiv_at.sub (hf : has_mfderiv_at I 𝓘(𝕜, E') f z f') (hg : has_mfderiv_at I 𝓘(𝕜, E') g z g') : has_mfderiv_at I 𝓘(𝕜, E') (f - g) z (f'- g') := ⟨hf.1.sub hg.1, hf.2.sub hg.2⟩ lemma mdifferentiable_at.sub (hf : mdifferentiable_at I 𝓘(𝕜, E') f z) (hg : mdifferentiable_at I 𝓘(𝕜, E') g z) : mdifferentiable_at I 𝓘(𝕜, E') (f - g) z := (hf.has_mfderiv_at.sub hg.has_mfderiv_at).mdifferentiable_at lemma mdifferentiable.sub {f : M → E'} (hf : mdifferentiable I 𝓘(𝕜, E') f) (hg : mdifferentiable I 𝓘(𝕜, E') g) : mdifferentiable I 𝓘(𝕜, E') (f - g) := λ x, (hf x).sub (hg x) end arithmetic namespace model_with_corners /-! #### Model with corners -/ protected lemma has_mfderiv_at {x} : has_mfderiv_at I 𝓘(𝕜, E) I x (continuous_linear_map.id _ _) := ⟨I.continuous_at, (has_fderiv_within_at_id _ _).congr' I.right_inv_on (mem_range_self _)⟩ protected lemma has_mfderiv_within_at {s x} : has_mfderiv_within_at I 𝓘(𝕜, E) I s x (continuous_linear_map.id _ _) := I.has_mfderiv_at.has_mfderiv_within_at protected lemma mdifferentiable_within_at {s x} : mdifferentiable_within_at I 𝓘(𝕜, E) I s x := I.has_mfderiv_within_at.mdifferentiable_within_at protected lemma mdifferentiable_at {x} : mdifferentiable_at I 𝓘(𝕜, E) I x := I.has_mfderiv_at.mdifferentiable_at protected lemma mdifferentiable_on {s} : mdifferentiable_on I 𝓘(𝕜, E) I s := λ x hx, I.mdifferentiable_within_at protected lemma mdifferentiable : mdifferentiable I (𝓘(𝕜, E)) I := λ x, I.mdifferentiable_at lemma has_mfderiv_within_at_symm {x} (hx : x ∈ range I) : has_mfderiv_within_at 𝓘(𝕜, E) I I.symm (range I) x (continuous_linear_map.id _ _) := ⟨I.continuous_within_at_symm, (has_fderiv_within_at_id _ _).congr' (λ y hy, I.right_inv_on hy.1) ⟨hx, mem_range_self _⟩⟩ lemma mdifferentiable_on_symm : mdifferentiable_on (𝓘(𝕜, E)) I I.symm (range I) := λ x hx, (I.has_mfderiv_within_at_symm hx).mdifferentiable_within_at end model_with_corners section charts variable {e : local_homeomorph M H} lemma mdifferentiable_at_atlas (h : e ∈ atlas H M) {x : M} (hx : x ∈ e.source) : mdifferentiable_at I I e x := begin refine ⟨(e.continuous_on x hx).continuous_at (is_open.mem_nhds e.open_source hx), _⟩, have mem : I ((chart_at H x : M → H) x) ∈ I.symm ⁻¹' ((chart_at H x).symm ≫ₕ e).source ∩ range I, by simp only [hx] with mfld_simps, have : (chart_at H x).symm.trans e ∈ cont_diff_groupoid ∞ I := has_groupoid.compatible _ (chart_mem_atlas H x) h, have A : cont_diff_on 𝕜 ∞ (I ∘ ((chart_at H x).symm.trans e) ∘ I.symm) (I.symm ⁻¹' ((chart_at H x).symm.trans e).source ∩ range I) := this.1, have B := A.differentiable_on le_top (I ((chart_at H x : M → H) x)) mem, simp only with mfld_simps at B, rw [inter_comm, differentiable_within_at_inter] at B, { simpa only with mfld_simps }, { apply is_open.mem_nhds ((local_homeomorph.open_source _).preimage I.continuous_symm) mem.1 } end lemma mdifferentiable_on_atlas (h : e ∈ atlas H M) : mdifferentiable_on I I e e.source := λx hx, (mdifferentiable_at_atlas I h hx).mdifferentiable_within_at lemma mdifferentiable_at_atlas_symm (h : e ∈ atlas H M) {x : H} (hx : x ∈ e.target) : mdifferentiable_at I I e.symm x := begin refine ⟨(e.continuous_on_symm x hx).continuous_at (is_open.mem_nhds e.open_target hx), _⟩, have mem : I x ∈ I.symm ⁻¹' (e.symm ≫ₕ chart_at H (e.symm x)).source ∩ range (I), by simp only [hx] with mfld_simps, have : e.symm.trans (chart_at H (e.symm x)) ∈ cont_diff_groupoid ∞ I := has_groupoid.compatible _ h (chart_mem_atlas H _), have A : cont_diff_on 𝕜 ∞ (I ∘ (e.symm.trans (chart_at H (e.symm x))) ∘ I.symm) (I.symm ⁻¹' (e.symm.trans (chart_at H (e.symm x))).source ∩ range I) := this.1, have B := A.differentiable_on le_top (I x) mem, simp only with mfld_simps at B, rw [inter_comm, differentiable_within_at_inter] at B, { simpa only with mfld_simps }, { apply (is_open.mem_nhds ((local_homeomorph.open_source _).preimage I.continuous_symm) mem.1) } end lemma mdifferentiable_on_atlas_symm (h : e ∈ atlas H M) : mdifferentiable_on I I e.symm e.target := λx hx, (mdifferentiable_at_atlas_symm I h hx).mdifferentiable_within_at lemma mdifferentiable_of_mem_atlas (h : e ∈ atlas H M) : e.mdifferentiable I I := ⟨mdifferentiable_on_atlas I h, mdifferentiable_on_atlas_symm I h⟩ lemma mdifferentiable_chart (x : M) : (chart_at H x).mdifferentiable I I := mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _) /-- The derivative of the chart at a base point is the chart of the tangent bundle, composed with the identification between the tangent bundle of the model space and the product space. -/ lemma tangent_map_chart {p q : tangent_bundle I M} (h : q.1 ∈ (chart_at H p.1).source) : tangent_map I I (chart_at H p.1) q = (equiv.sigma_equiv_prod _ _).symm ((chart_at (model_prod H E) p : tangent_bundle I M → model_prod H E) q) := begin dsimp [tangent_map], rw mdifferentiable_at.mfderiv, { refl }, { exact mdifferentiable_at_atlas _ (chart_mem_atlas _ _) h } end /-- The derivative of the inverse of the chart at a base point is the inverse of the chart of the tangent bundle, composed with the identification between the tangent bundle of the model space and the product space. -/ lemma tangent_map_chart_symm {p : tangent_bundle I M} {q : tangent_bundle I H} (h : q.1 ∈ (chart_at H p.1).target) : tangent_map I I (chart_at H p.1).symm q = ((chart_at (model_prod H E) p).symm : model_prod H E → tangent_bundle I M) ((equiv.sigma_equiv_prod H E) q) := begin dsimp only [tangent_map], rw mdifferentiable_at.mfderiv (mdifferentiable_at_atlas_symm _ (chart_mem_atlas _ _) h), -- a trivial instance is needed after the rewrite, handle it right now. rotate, { apply_instance }, simp only [continuous_linear_map.coe_coe, basic_smooth_vector_bundle_core.chart, h, tangent_bundle_core, basic_smooth_vector_bundle_core.to_vector_bundle_core, chart_at, sigma.mk.inj_iff] with mfld_simps, end end charts end specific_functions /-! ### Differentiable local homeomorphisms -/ namespace local_homeomorph.mdifferentiable variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} {M : Type*} [topological_space M] [charted_space H M] {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} {M' : Type*} [topological_space M'] [charted_space H' M'] {E'' : Type*} [normed_add_comm_group E''] [normed_space 𝕜 E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''} {M'' : Type*} [topological_space M''] [charted_space H'' M''] {e : local_homeomorph M M'} (he : e.mdifferentiable I I') {e' : local_homeomorph M' M''} include he lemma symm : e.symm.mdifferentiable I' I := ⟨he.2, he.1⟩ protected lemma mdifferentiable_at {x : M} (hx : x ∈ e.source) : mdifferentiable_at I I' e x := (he.1 x hx).mdifferentiable_at (is_open.mem_nhds e.open_source hx) lemma mdifferentiable_at_symm {x : M'} (hx : x ∈ e.target) : mdifferentiable_at I' I e.symm x := (he.2 x hx).mdifferentiable_at (is_open.mem_nhds e.open_target hx) variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M'] [smooth_manifold_with_corners I'' M''] lemma symm_comp_deriv {x : M} (hx : x ∈ e.source) : (mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) = continuous_linear_map.id 𝕜 (tangent_space I x) := begin have : (mfderiv I I (e.symm ∘ e) x) = (mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) := mfderiv_comp x (he.mdifferentiable_at_symm (e.map_source hx)) (he.mdifferentiable_at hx), rw ← this, have : mfderiv I I (_root_.id : M → M) x = continuous_linear_map.id _ _ := mfderiv_id I, rw ← this, apply filter.eventually_eq.mfderiv_eq, have : e.source ∈ 𝓝 x := is_open.mem_nhds e.open_source hx, exact filter.mem_of_superset this (by mfld_set_tac) end lemma comp_symm_deriv {x : M'} (hx : x ∈ e.target) : (mfderiv I I' e (e.symm x)).comp (mfderiv I' I e.symm x) = continuous_linear_map.id 𝕜 (tangent_space I' x) := he.symm.symm_comp_deriv hx /-- The derivative of a differentiable local homeomorphism, as a continuous linear equivalence between the tangent spaces at `x` and `e x`. -/ protected def mfderiv {x : M} (hx : x ∈ e.source) : tangent_space I x ≃L[𝕜] tangent_space I' (e x) := { inv_fun := (mfderiv I' I e.symm (e x)), continuous_to_fun := (mfderiv I I' e x).cont, continuous_inv_fun := (mfderiv I' I e.symm (e x)).cont, left_inv := λy, begin have : (continuous_linear_map.id _ _ : tangent_space I x →L[𝕜] tangent_space I x) y = y := rfl, conv_rhs { rw [← this, ← he.symm_comp_deriv hx] }, refl end, right_inv := λy, begin have : (continuous_linear_map.id 𝕜 _ : tangent_space I' (e x) →L[𝕜] tangent_space I' (e x)) y = y := rfl, conv_rhs { rw [← this, ← he.comp_symm_deriv (e.map_source hx)] }, rw e.left_inv hx, refl end, .. mfderiv I I' e x } lemma mfderiv_bijective {x : M} (hx : x ∈ e.source) : function.bijective (mfderiv I I' e x) := (he.mfderiv hx).bijective lemma mfderiv_injective {x : M} (hx : x ∈ e.source) : function.injective (mfderiv I I' e x) := (he.mfderiv hx).injective lemma mfderiv_surjective {x : M} (hx : x ∈ e.source) : function.surjective (mfderiv I I' e x) := (he.mfderiv hx).surjective lemma ker_mfderiv_eq_bot {x : M} (hx : x ∈ e.source) : linear_map.ker (mfderiv I I' e x) = ⊥ := (he.mfderiv hx).to_linear_equiv.ker lemma range_mfderiv_eq_top {x : M} (hx : x ∈ e.source) : linear_map.range (mfderiv I I' e x) = ⊤ := (he.mfderiv hx).to_linear_equiv.range lemma range_mfderiv_eq_univ {x : M} (hx : x ∈ e.source) : range (mfderiv I I' e x) = univ := (he.mfderiv_surjective hx).range_eq lemma trans (he': e'.mdifferentiable I' I'') : (e.trans e').mdifferentiable I I'' := begin split, { assume x hx, simp only with mfld_simps at hx, exact ((he'.mdifferentiable_at hx.2).comp _ (he.mdifferentiable_at hx.1)).mdifferentiable_within_at }, { assume x hx, simp only with mfld_simps at hx, exact ((he.symm.mdifferentiable_at hx.2).comp _ (he'.symm.mdifferentiable_at hx.1)).mdifferentiable_within_at } end end local_homeomorph.mdifferentiable /-! ### Differentiability of `ext_chart_at` -/ section ext_chart_at variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] {s : set M} {x y : M} lemma has_mfderiv_at_ext_chart_at (h : y ∈ (chart_at H x).source) : has_mfderiv_at I 𝓘(𝕜, E) (ext_chart_at I x) y (mfderiv I I (chart_at H x) y : _) := I.has_mfderiv_at.comp y ((mdifferentiable_chart I x).mdifferentiable_at h).has_mfderiv_at lemma has_mfderiv_within_at_ext_chart_at (h : y ∈ (chart_at H x).source) : has_mfderiv_within_at I 𝓘(𝕜, E) (ext_chart_at I x) s y (mfderiv I I (chart_at H x) y : _) := (has_mfderiv_at_ext_chart_at I h).has_mfderiv_within_at lemma mdifferentiable_at_ext_chart_at (h : y ∈ (chart_at H x).source) : mdifferentiable_at I 𝓘(𝕜, E) (ext_chart_at I x) y := (has_mfderiv_at_ext_chart_at I h).mdifferentiable_at lemma mdifferentiable_on_ext_chart_at : mdifferentiable_on I 𝓘(𝕜, E) (ext_chart_at I x) (chart_at H x).source := λ y hy, (has_mfderiv_within_at_ext_chart_at I hy).mdifferentiable_within_at end ext_chart_at /-! ### Unique derivative sets in manifolds -/ section unique_mdiff variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} {M' : Type*} [topological_space M'] [charted_space H' M'] {s : set M} /-- If a set has the unique differential property, then its image under a local diffeomorphism also has the unique differential property. -/ lemma unique_mdiff_on.unique_mdiff_on_preimage [smooth_manifold_with_corners I' M'] (hs : unique_mdiff_on I s) {e : local_homeomorph M M'} (he : e.mdifferentiable I I') : unique_mdiff_on I' (e.target ∩ e.symm ⁻¹' s) := begin /- Start from a point `x` in the image, and let `z` be its preimage. Then the unique derivative property at `x` is expressed through `ext_chart_at I' x`, and the unique derivative property at `z` is expressed through `ext_chart_at I z`. We will argue that the composition of these two charts with `e` is a local diffeomorphism in vector spaces, and therefore preserves the unique differential property thanks to lemma `has_fderiv_within_at.unique_diff_within_at`, saying that a differentiable function with onto derivative preserves the unique derivative property.-/ assume x hx, let z := e.symm x, have z_source : z ∈ e.source, by simp only [hx.1] with mfld_simps, have zx : e z = x, by simp only [z, hx.1] with mfld_simps, let F := ext_chart_at I z, -- the unique derivative property at `z` is expressed through its preferred chart, -- that we call `F`. have B : unique_diff_within_at 𝕜 (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z), { have : unique_mdiff_within_at I s z := hs _ hx.2, have S : e.source ∩ e ⁻¹' ((ext_chart_at I' x).source) ∈ 𝓝 z, { apply is_open.mem_nhds, apply e.continuous_on.preimage_open_of_open e.open_source (is_open_ext_chart_at_source I' x), simp only [z_source, zx] with mfld_simps }, have := this.inter S, rw [unique_mdiff_within_at_iff] at this, exact this }, -- denote by `G` the change of coordinate, i.e., the composition of the two extended charts and -- of `e` let G := F.symm ≫ e.to_local_equiv ≫ (ext_chart_at I' x), -- `G` is differentiable have Diff : ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).mdifferentiable I I', { have A := mdifferentiable_of_mem_atlas I (chart_mem_atlas H z), have B := mdifferentiable_of_mem_atlas I' (chart_mem_atlas H' x), exact A.symm.trans (he.trans B) }, have Mmem : (chart_at H z : M → H) z ∈ ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).source, by simp only [z_source, zx] with mfld_simps, have A : differentiable_within_at 𝕜 G (range I) (F z), { refine (Diff.mdifferentiable_at Mmem).2.congr (λp hp, _) _; simp only [G, F] with mfld_simps }, -- let `G'` be its derivative let G' := fderiv_within 𝕜 G (range I) (F z), have D₁ : has_fderiv_within_at G G' (range I) (F z) := A.has_fderiv_within_at, have D₂ : has_fderiv_within_at G G' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z) := D₁.mono (by mfld_set_tac), -- The derivative `G'` is onto, as it is the derivative of a local diffeomorphism, the composition -- of the two charts and of `e`. have C : dense_range (G' : E → E'), { have : G' = mfderiv I I' ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)) ((chart_at H z : M → H) z), by { rw (Diff.mdifferentiable_at Mmem).mfderiv, refl }, rw this, exact (Diff.mfderiv_surjective Mmem).dense_range }, -- key step: thanks to what we have proved about it, `G` preserves the unique derivative property have key : unique_diff_within_at 𝕜 (G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target)) (G (F z)) := D₂.unique_diff_within_at B C, have : G (F z) = (ext_chart_at I' x) x, by { dsimp [G, F], simp only [hx.1] with mfld_simps }, rw this at key, apply key.mono, show G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) ⊆ (ext_chart_at I' x).symm ⁻¹' e.target ∩ (ext_chart_at I' x).symm ⁻¹' (e.symm ⁻¹' s) ∩ range (I'), rw image_subset_iff, mfld_set_tac end /-- If a set in a manifold has the unique derivative property, then its pullback by any extended chart, in the vector space, also has the unique derivative property. -/ lemma unique_mdiff_on.unique_diff_on_target_inter (hs : unique_mdiff_on I s) (x : M) : unique_diff_on 𝕜 ((ext_chart_at I x).target ∩ ((ext_chart_at I x).symm ⁻¹' s)) := begin -- this is just a reformulation of `unique_mdiff_on.unique_mdiff_on_preimage`, using as `e` -- the local chart at `x`. assume z hz, simp only with mfld_simps at hz, have : (chart_at H x).mdifferentiable I I := mdifferentiable_chart _ _, have T := (hs.unique_mdiff_on_preimage this) (I.symm z), simp only [hz.left.left, hz.left.right, hz.right, unique_mdiff_within_at] with mfld_simps at ⊢ T, convert T using 1, rw @preimage_comp _ _ _ _ (chart_at H x).symm, mfld_set_tac end /-- When considering functions between manifolds, this statement shows up often. It entails the unique differential of the pullback in extended charts of the set where the function can be read in the charts. -/ lemma unique_mdiff_on.unique_diff_on_inter_preimage (hs : unique_mdiff_on I s) (x : M) (y : M') {f : M → M'} (hf : continuous_on f s) : unique_diff_on 𝕜 ((ext_chart_at I x).target ∩ ((ext_chart_at I x).symm ⁻¹' (s ∩ f⁻¹' (ext_chart_at I' y).source))) := begin have : unique_mdiff_on I (s ∩ f ⁻¹' (ext_chart_at I' y).source), { assume z hz, apply (hs z hz.1).inter', apply (hf z hz.1).preimage_mem_nhds_within, exact (is_open_ext_chart_at_source I' y).mem_nhds hz.2 }, exact this.unique_diff_on_target_inter _ end variables {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] (Z : basic_smooth_vector_bundle_core I M F) /-- In a smooth fiber bundle constructed from core, the preimage under the projection of a set with unique differential in the basis also has unique differential. -/ lemma unique_mdiff_on.smooth_bundle_preimage (hs : unique_mdiff_on I s) : unique_mdiff_on (I.prod (𝓘(𝕜, F))) (Z.to_vector_bundle_core.proj ⁻¹' s) := begin /- Using a chart (and the fact that unique differentiability is invariant under charts), we reduce the situation to the model space, where we can use the fact that products respect unique differentiability. -/ assume p hp, replace hp : p.fst ∈ s, by simpa only with mfld_simps using hp, let e₀ := chart_at H p.1, let e := chart_at (model_prod H F) p, -- It suffices to prove unique differentiability in a chart suffices h : unique_mdiff_on (I.prod (𝓘(𝕜, F))) (e.target ∩ e.symm⁻¹' (Z.to_vector_bundle_core.proj ⁻¹' s)), { have A : unique_mdiff_on (I.prod (𝓘(𝕜, F))) (e.symm.target ∩ e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (Z.to_vector_bundle_core.proj ⁻¹' s))), { apply h.unique_mdiff_on_preimage, exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)).symm, apply_instance }, have : p ∈ e.symm.target ∩ e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (Z.to_vector_bundle_core.proj ⁻¹' s)), by simp only [e, hp] with mfld_simps, apply (A _ this).mono, assume q hq, simp only [e, local_homeomorph.left_inv _ hq.1] with mfld_simps at hq, simp only [hq] with mfld_simps }, -- rewrite the relevant set in the chart as a direct product have : (λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' e.target ∩ (λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' (e.symm ⁻¹' (sigma.fst ⁻¹' s)) ∩ (range I ×ˢ univ) = (I.symm ⁻¹' (e₀.target ∩ e₀.symm⁻¹' s) ∩ range I) ×ˢ univ, by mfld_set_tac, assume q hq, replace hq : q.1 ∈ (chart_at H p.1).target ∧ ((chart_at H p.1).symm : H → M) q.1 ∈ s, by simpa only with mfld_simps using hq, simp only [unique_mdiff_within_at, model_with_corners.prod, preimage_inter, this] with mfld_simps, -- apply unique differentiability of products to conclude apply unique_diff_on.prod _ unique_diff_on_univ, { simp only [hq] with mfld_simps }, { assume x hx, have A : unique_mdiff_on I (e₀.target ∩ e₀.symm⁻¹' s), { apply hs.unique_mdiff_on_preimage, exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)), apply_instance }, simp only [unique_mdiff_on, unique_mdiff_within_at, preimage_inter] with mfld_simps at A, have B := A (I.symm x) hx.1.1 hx.1.2, rwa [← preimage_inter, model_with_corners.right_inv _ hx.2] at B } end lemma unique_mdiff_on.tangent_bundle_proj_preimage (hs : unique_mdiff_on I s): unique_mdiff_on I.tangent ((tangent_bundle.proj I M) ⁻¹' s) := hs.smooth_bundle_preimage _ end unique_mdiff
6df1fa9f39316e3bb526bd93438bc0176dfba49c
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/boolean_algebra.lean
6bb520e1d9aaf942bb53d9e60b09ae6a7558da76
[ "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
30,799
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, Bryan Gin-ge Chen -/ import order.heyting.basic /-! # (Generalized) Boolean algebras > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > https://github.com/leanprover-community/mathlib4/pull/794 > Any changes to this file require a corresponding PR to mathlib4. A Boolean algebra is a bounded distributive lattice with a complement operator. Boolean algebras generalize the (classical) logic of propositions and the lattice of subsets of a set. Generalized Boolean algebras may be less familiar, but they are essentially Boolean algebras which do not necessarily have a top element (`⊤`) (and hence not all elements may have complements). One example in mathlib is `finset α`, the type of all finite subsets of an arbitrary (not-necessarily-finite) type `α`. `generalized_boolean_algebra α` is defined to be a distributive lattice with bottom (`⊥`) admitting a *relative* complement operator, written using "set difference" notation as `x \ y` (`sdiff x y`). For convenience, the `boolean_algebra` type class is defined to extend `generalized_boolean_algebra` so that it is also bundled with a `\` operator. (A terminological point: `x \ y` is the complement of `y` relative to the interval `[⊥, x]`. We do not yet have relative complements for arbitrary intervals, as we do not even have lattice intervals.) ## Main declarations * `generalized_boolean_algebra`: a type class for generalized Boolean algebras * `boolean_algebra`: a type class for Boolean algebras. * `Prop.boolean_algebra`: the Boolean algebra instance on `Prop` ## Implementation notes The `sup_inf_sdiff` and `inf_inf_sdiff` axioms for the relative complement operator in `generalized_boolean_algebra` are taken from [Wikipedia](https://en.wikipedia.org/wiki/Boolean_algebra_(structure)#Generalizations). [Stone's paper introducing generalized Boolean algebras][Stone1935] does not define a relative complement operator `a \ b` for all `a`, `b`. Instead, the postulates there amount to an assumption that for all `a, b : α` where `a ≤ b`, the equations `x ⊔ a = b` and `x ⊓ a = ⊥` have a solution `x`. `disjoint.sdiff_unique` proves that this `x` is in fact `b \ a`. ## References * <https://en.wikipedia.org/wiki/Boolean_algebra_(structure)#Generalizations> * [*Postulates for Boolean Algebras and Generalized Boolean Algebras*, M.H. Stone][Stone1935] * [*Lattice Theory: Foundation*, George Grätzer][Gratzer2011] ## Tags generalized Boolean algebras, Boolean algebras, lattices, sdiff, compl -/ set_option old_structure_cmd true open function order_dual universes u v variables {α : Type u} {β : Type*} {w x y z : α} /-! ### Generalized Boolean algebras Some of the lemmas in this section are from: * [*Lattice Theory: Foundation*, George Grätzer][Gratzer2011] * <https://ncatlab.org/nlab/show/relative+complement> * <https://people.math.gatech.edu/~mccuan/courses/4317/symmetricdifference.pdf> -/ /-- A generalized Boolean algebra is a distributive lattice with `⊥` and a relative complement operation `\` (called `sdiff`, after "set difference") satisfying `(a ⊓ b) ⊔ (a \ b) = a` and `(a ⊓ b) ⊓ (a \ b) = ⊥`, i.e. `a \ b` is the complement of `b` in `a`. This is a generalization of Boolean algebras which applies to `finset α` for arbitrary (not-necessarily-`fintype`) `α`. -/ class generalized_boolean_algebra (α : Type u) extends distrib_lattice α, has_sdiff α, has_bot α := (sup_inf_sdiff : ∀a b:α, (a ⊓ b) ⊔ (a \ b) = a) (inf_inf_sdiff : ∀a b:α, (a ⊓ b) ⊓ (a \ b) = ⊥) -- We might want a `is_compl_of` predicate (for relative complements) generalizing `is_compl`, -- however we'd need another type class for lattices with bot, and all the API for that. section generalized_boolean_algebra variables [generalized_boolean_algebra α] @[simp] theorem sup_inf_sdiff (x y : α) : (x ⊓ y) ⊔ (x \ y) = x := generalized_boolean_algebra.sup_inf_sdiff _ _ @[simp] theorem inf_inf_sdiff (x y : α) : (x ⊓ y) ⊓ (x \ y) = ⊥ := generalized_boolean_algebra.inf_inf_sdiff _ _ @[simp] theorem sup_sdiff_inf (x y : α) : (x \ y) ⊔ (x ⊓ y) = x := by rw [sup_comm, sup_inf_sdiff] @[simp] theorem inf_sdiff_inf (x y : α) : (x \ y) ⊓ (x ⊓ y) = ⊥ := by rw [inf_comm, inf_inf_sdiff] @[priority 100] -- see Note [lower instance priority] instance generalized_boolean_algebra.to_order_bot : order_bot α := { bot_le := λ a, by { rw [←inf_inf_sdiff a a, inf_assoc], exact inf_le_left }, ..generalized_boolean_algebra.to_has_bot α } theorem disjoint_inf_sdiff : disjoint (x ⊓ y) (x \ y) := disjoint_iff_inf_le.mpr (inf_inf_sdiff x y).le -- TODO: in distributive lattices, relative complements are unique when they exist theorem sdiff_unique (s : (x ⊓ y) ⊔ z = x) (i : (x ⊓ y) ⊓ z = ⊥) : x \ y = z := begin conv_rhs at s { rw [←sup_inf_sdiff x y, sup_comm] }, rw sup_comm at s, conv_rhs at i { rw [←inf_inf_sdiff x y, inf_comm] }, rw inf_comm at i, exact (eq_of_inf_eq_sup_eq i s).symm, end -- Use `sdiff_le` private lemma sdiff_le' : x \ y ≤ x := calc x \ y ≤ (x ⊓ y) ⊔ (x \ y) : le_sup_right ... = x : sup_inf_sdiff x y -- Use `sdiff_sup_self` private lemma sdiff_sup_self' : y \ x ⊔ x = y ⊔ x := calc y \ x ⊔ x = y \ x ⊔ (x ⊔ x ⊓ y) : by rw sup_inf_self ... = (y ⊓ x) ⊔ y \ x ⊔ x : by ac_refl ... = y ⊔ x : by rw sup_inf_sdiff @[simp] lemma sdiff_inf_sdiff : x \ y ⊓ (y \ x) = ⊥ := eq.symm $ calc ⊥ = (x ⊓ y) ⊓ (x \ y) : by rw inf_inf_sdiff ... = (x ⊓ (y ⊓ x ⊔ y \ x)) ⊓ (x \ y) : by rw sup_inf_sdiff ... = (x ⊓ (y ⊓ x) ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by rw inf_sup_left ... = (y ⊓ (x ⊓ x) ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by ac_refl ... = (y ⊓ x ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by rw inf_idem ... = (x ⊓ y ⊓ (x \ y)) ⊔ (x ⊓ (y \ x) ⊓ (x \ y)) : by rw [inf_sup_right, @inf_comm _ _ x y] ... = x ⊓ (y \ x) ⊓ (x \ y) : by rw [inf_inf_sdiff, bot_sup_eq] ... = x ⊓ (x \ y) ⊓ (y \ x) : by ac_refl ... = (x \ y) ⊓ (y \ x) : by rw inf_of_le_right sdiff_le' lemma disjoint_sdiff_sdiff : disjoint (x \ y) (y \ x) := disjoint_iff_inf_le.mpr sdiff_inf_sdiff.le @[simp] theorem inf_sdiff_self_right : x ⊓ (y \ x) = ⊥ := calc x ⊓ (y \ x) = ((x ⊓ y) ⊔ (x \ y)) ⊓ (y \ x) : by rw sup_inf_sdiff ... = (x ⊓ y) ⊓ (y \ x) ⊔ (x \ y) ⊓ (y \ x) : by rw inf_sup_right ... = ⊥ : by rw [@inf_comm _ _ x y, inf_inf_sdiff, sdiff_inf_sdiff, bot_sup_eq] @[simp] theorem inf_sdiff_self_left : (y \ x) ⊓ x = ⊥ := by rw [inf_comm, inf_sdiff_self_right] @[priority 100] -- see Note [lower instance priority] instance generalized_boolean_algebra.to_generalized_coheyting_algebra : generalized_coheyting_algebra α := { sdiff := (\), sdiff_le_iff := λ y x z, ⟨λ h, le_of_inf_le_sup_le (le_of_eq (calc y ⊓ (y \ x) = y \ x : inf_of_le_right sdiff_le' ... = (x ⊓ (y \ x)) ⊔ (z ⊓ (y \ x)) : by rw [inf_eq_right.2 h, inf_sdiff_self_right, bot_sup_eq] ... = (x ⊔ z) ⊓ (y \ x) : inf_sup_right.symm)) (calc y ⊔ y \ x = y : sup_of_le_left sdiff_le' ... ≤ y ⊔ (x ⊔ z) : le_sup_left ... = ((y \ x) ⊔ x) ⊔ z : by rw [←sup_assoc, ←@sdiff_sup_self' _ x y] ... = x ⊔ z ⊔ y \ x : by ac_refl), λ h, le_of_inf_le_sup_le (calc y \ x ⊓ x = ⊥ : inf_sdiff_self_left ... ≤ z ⊓ x : bot_le) (calc y \ x ⊔ x = y ⊔ x : sdiff_sup_self' ... ≤ (x ⊔ z) ⊔ x : sup_le_sup_right h x ... ≤ z ⊔ x : by rw [sup_assoc, sup_comm, sup_assoc, sup_idem])⟩, ..‹generalized_boolean_algebra α›, ..generalized_boolean_algebra.to_order_bot } theorem disjoint_sdiff_self_left : disjoint (y \ x) x := disjoint_iff_inf_le.mpr inf_sdiff_self_left.le theorem disjoint_sdiff_self_right : disjoint x (y \ x) := disjoint_iff_inf_le.mpr inf_sdiff_self_right.le /- TODO: we could make an alternative constructor for `generalized_boolean_algebra` using `disjoint x (y \ x)` and `x ⊔ (y \ x) = y` as axioms. -/ theorem disjoint.sdiff_eq_of_sup_eq (hi : disjoint x z) (hs : x ⊔ z = y) : y \ x = z := have h : y ⊓ x = x := inf_eq_right.2 $ le_sup_left.trans hs.le, sdiff_unique (by rw [h, hs]) (by rw [h, hi.eq_bot]) protected theorem disjoint.sdiff_unique (hd : disjoint x z) (hz : z ≤ y) (hs : y ≤ x ⊔ z) : y \ x = z := sdiff_unique (begin rw ←inf_eq_right at hs, rwa [sup_inf_right, inf_sup_right, @sup_comm _ _ x, inf_sup_self, inf_comm, @sup_comm _ _ z, hs, sup_eq_left], end) (by rw [inf_assoc, hd.eq_bot, inf_bot_eq]) -- cf. `is_compl.disjoint_left_iff` and `is_compl.disjoint_right_iff` lemma disjoint_sdiff_iff_le (hz : z ≤ y) (hx : x ≤ y) : disjoint z (y \ x) ↔ z ≤ x := ⟨λ H, le_of_inf_le_sup_le (le_trans H.le_bot bot_le) (begin rw sup_sdiff_cancel_right hx, refine le_trans (sup_le_sup_left sdiff_le z) _, rw sup_eq_right.2 hz, end), λ H, disjoint_sdiff_self_right.mono_left H⟩ -- cf. `is_compl.le_left_iff` and `is_compl.le_right_iff` lemma le_iff_disjoint_sdiff (hz : z ≤ y) (hx : x ≤ y) : z ≤ x ↔ disjoint z (y \ x) := (disjoint_sdiff_iff_le hz hx).symm -- cf. `is_compl.inf_left_eq_bot_iff` and `is_compl.inf_right_eq_bot_iff` lemma inf_sdiff_eq_bot_iff (hz : z ≤ y) (hx : x ≤ y) : z ⊓ (y \ x) = ⊥ ↔ z ≤ x := by { rw ←disjoint_iff, exact disjoint_sdiff_iff_le hz hx } -- cf. `is_compl.left_le_iff` and `is_compl.right_le_iff` lemma le_iff_eq_sup_sdiff (hz : z ≤ y) (hx : x ≤ y) : x ≤ z ↔ y = z ⊔ (y \ x) := ⟨λ H, begin apply le_antisymm, { conv_lhs { rw ←sup_inf_sdiff y x, }, apply sup_le_sup_right, rwa inf_eq_right.2 hx, }, { apply le_trans, { apply sup_le_sup_right hz, }, { rw sup_sdiff_left, } } end, λ H, begin conv_lhs at H { rw ←sup_sdiff_cancel_right hx, }, refine le_of_inf_le_sup_le _ H.le, rw inf_sdiff_self_right, exact bot_le, end⟩ -- cf. `is_compl.sup_inf` lemma sdiff_sup : y \ (x ⊔ z) = (y \ x) ⊓ (y \ z) := sdiff_unique (calc y ⊓ (x ⊔ z) ⊔ y \ x ⊓ (y \ z) = (y ⊓ (x ⊔ z) ⊔ y \ x) ⊓ (y ⊓ (x ⊔ z) ⊔ (y \ z)) : by rw sup_inf_left ... = (y ⊓ x ⊔ y ⊓ z ⊔ y \ x) ⊓ (y ⊓ x ⊔ y ⊓ z ⊔ (y \ z)) : by rw @inf_sup_left _ _ y ... = (y ⊓ z ⊔ (y ⊓ x ⊔ y \ x)) ⊓ (y ⊓ x ⊔ (y ⊓ z ⊔ (y \ z))) : by ac_refl ... = (y ⊓ z ⊔ y) ⊓ (y ⊓ x ⊔ y) : by rw [sup_inf_sdiff, sup_inf_sdiff] ... = (y ⊔ y ⊓ z) ⊓ (y ⊔ y ⊓ x) : by ac_refl ... = y : by rw [sup_inf_self, sup_inf_self, inf_idem]) (calc y ⊓ (x ⊔ z) ⊓ ((y \ x) ⊓ (y \ z)) = (y ⊓ x ⊔ y ⊓ z) ⊓ ((y \ x) ⊓ (y \ z)) : by rw inf_sup_left ... = ((y ⊓ x) ⊓ ((y \ x) ⊓ (y \ z))) ⊔ ((y ⊓ z) ⊓ ((y \ x) ⊓ (y \ z))) : by rw inf_sup_right ... = ((y ⊓ x) ⊓ (y \ x) ⊓ (y \ z)) ⊔ ((y \ x) ⊓ ((y \ z) ⊓ (y ⊓ z))) : by ac_refl ... = ⊥ : by rw [inf_inf_sdiff, bot_inf_eq, bot_sup_eq, @inf_comm _ _ (y \ z), inf_inf_sdiff, inf_bot_eq]) lemma sdiff_eq_sdiff_iff_inf_eq_inf : y \ x = y \ z ↔ y ⊓ x = y ⊓ z := ⟨λ h, eq_of_inf_eq_sup_eq (by rw [inf_inf_sdiff, h, inf_inf_sdiff]) (by rw [sup_inf_sdiff, h, sup_inf_sdiff]), λ h, by rw [←sdiff_inf_self_right, ←sdiff_inf_self_right z y, inf_comm, h, inf_comm]⟩ theorem sdiff_eq_self_iff_disjoint : x \ y = x ↔ disjoint y x := calc x \ y = x ↔ x \ y = x \ ⊥ : by rw sdiff_bot ... ↔ x ⊓ y = x ⊓ ⊥ : sdiff_eq_sdiff_iff_inf_eq_inf ... ↔ disjoint y x : by rw [inf_bot_eq, inf_comm, disjoint_iff] theorem sdiff_eq_self_iff_disjoint' : x \ y = x ↔ disjoint x y := by rw [sdiff_eq_self_iff_disjoint, disjoint.comm] lemma sdiff_lt (hx : y ≤ x) (hy : y ≠ ⊥) : x \ y < x := begin refine sdiff_le.lt_of_ne (λ h, hy _), rw [sdiff_eq_self_iff_disjoint', disjoint_iff] at h, rw [←h, inf_eq_right.mpr hx], end @[simp] lemma le_sdiff_iff : x ≤ y \ x ↔ x = ⊥ := ⟨λ h, disjoint_self.1 (disjoint_sdiff_self_right.mono_right h), λ h, h.le.trans bot_le⟩ lemma sdiff_lt_sdiff_right (h : x < y) (hz : z ≤ x) : x \ z < y \ z := (sdiff_le_sdiff_right h.le).lt_of_not_le $ λ h', h.not_le $ le_sdiff_sup.trans $ sup_le_of_le_sdiff_right h' hz lemma sup_inf_inf_sdiff : (x ⊓ y) ⊓ z ⊔ (y \ z) = (x ⊓ y) ⊔ (y \ z) := calc (x ⊓ y) ⊓ z ⊔ (y \ z) = x ⊓ (y ⊓ z) ⊔ (y \ z) : by rw inf_assoc ... = (x ⊔ (y \ z)) ⊓ y : by rw [sup_inf_right, sup_inf_sdiff] ... = (x ⊓ y) ⊔ (y \ z) : by rw [inf_sup_right, inf_sdiff_left] lemma sdiff_sdiff_right : x \ (y \ z) = (x \ y) ⊔ (x ⊓ y ⊓ z) := begin rw [sup_comm, inf_comm, ←inf_assoc, sup_inf_inf_sdiff], apply sdiff_unique, { calc x ⊓ (y \ z) ⊔ (z ⊓ x ⊔ x \ y) = (x ⊔ (z ⊓ x ⊔ x \ y)) ⊓ (y \ z ⊔ (z ⊓ x ⊔ x \ y)) : by rw sup_inf_right ... = (x ⊔ x ⊓ z ⊔ x \ y) ⊓ (y \ z ⊔ (x ⊓ z ⊔ x \ y)) : by ac_refl ... = x ⊓ (y \ z ⊔ x ⊓ z ⊔ x \ y) : by rw [sup_inf_self, sup_sdiff_left, ←sup_assoc] ... = x ⊓ (y \ z ⊓ (z ⊔ y) ⊔ x ⊓ (z ⊔ y) ⊔ x \ y) : by rw [sup_inf_left, sdiff_sup_self', inf_sup_right, @sup_comm _ _ y] ... = x ⊓ (y \ z ⊔ (x ⊓ z ⊔ x ⊓ y) ⊔ x \ y) : by rw [inf_sdiff_sup_right, @inf_sup_left _ _ x z y] ... = x ⊓ (y \ z ⊔ (x ⊓ z ⊔ (x ⊓ y ⊔ x \ y))) : by ac_refl ... = x ⊓ (y \ z ⊔ (x ⊔ x ⊓ z)) : by rw [sup_inf_sdiff, @sup_comm _ _ (x ⊓ z)] ... = x : by rw [sup_inf_self, sup_comm, inf_sup_self] }, { calc x ⊓ (y \ z) ⊓ (z ⊓ x ⊔ x \ y) = x ⊓ (y \ z) ⊓ (z ⊓ x) ⊔ x ⊓ (y \ z) ⊓ (x \ y) : by rw inf_sup_left ... = x ⊓ (y \ z ⊓ z ⊓ x) ⊔ x ⊓ (y \ z) ⊓ (x \ y) : by ac_refl ... = x ⊓ (y \ z) ⊓ (x \ y) : by rw [inf_sdiff_self_left, bot_inf_eq, inf_bot_eq, bot_sup_eq] ... = x ⊓ (y \ z ⊓ y) ⊓ (x \ y) : by conv_lhs { rw ←inf_sdiff_left } ... = x ⊓ (y \ z ⊓ (y ⊓ (x \ y))) : by ac_refl ... = ⊥ : by rw [inf_sdiff_self_right, inf_bot_eq, inf_bot_eq] } end lemma sdiff_sdiff_right' : x \ (y \ z) = (x \ y) ⊔ (x ⊓ z) := calc x \ (y \ z) = (x \ y) ⊔ (x ⊓ y ⊓ z) : sdiff_sdiff_right ... = z ⊓ x ⊓ y ⊔ (x \ y) : by ac_refl ... = (x \ y) ⊔ (x ⊓ z) : by rw [sup_inf_inf_sdiff, sup_comm, inf_comm] lemma sdiff_sdiff_eq_sdiff_sup (h : z ≤ x) : x \ (y \ z) = x \ y ⊔ z := by rw [sdiff_sdiff_right', inf_eq_right.2 h] @[simp] lemma sdiff_sdiff_right_self : x \ (x \ y) = x ⊓ y := by rw [sdiff_sdiff_right, inf_idem, sdiff_self, bot_sup_eq] lemma sdiff_sdiff_eq_self (h : y ≤ x) : x \ (x \ y) = y := by rw [sdiff_sdiff_right_self, inf_of_le_right h] lemma sdiff_eq_symm (hy : y ≤ x) (h : x \ y = z) : x \ z = y := by rw [←h, sdiff_sdiff_eq_self hy] lemma sdiff_eq_comm (hy : y ≤ x) (hz : z ≤ x) : x \ y = z ↔ x \ z = y := ⟨sdiff_eq_symm hy, sdiff_eq_symm hz⟩ lemma eq_of_sdiff_eq_sdiff (hxz : x ≤ z) (hyz : y ≤ z) (h : z \ x = z \ y) : x = y := by rw [←sdiff_sdiff_eq_self hxz, h, sdiff_sdiff_eq_self hyz] lemma sdiff_sdiff_left' : (x \ y) \ z = (x \ y) ⊓ (x \ z) := by rw [sdiff_sdiff_left, sdiff_sup] lemma sdiff_sdiff_sup_sdiff : z \ (x \ y ⊔ y \ x) = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) := calc z \ (x \ y ⊔ y \ x) = (z \ x ⊔ z ⊓ x ⊓ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sdiff_sup, sdiff_sdiff_right, sdiff_sdiff_right] ... = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sup_inf_left, sup_comm, sup_inf_sdiff] ... = z ⊓ (z \ x ⊔ y) ⊓ (z ⊓ (z \ y ⊔ x)) : by rw [sup_inf_left, @sup_comm _ _ (z \ y), sup_inf_sdiff] ... = z ⊓ z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) : by ac_refl ... = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) : by rw inf_idem lemma sdiff_sdiff_sup_sdiff' : z \ (x \ y ⊔ y \ x) = z ⊓ x ⊓ y ⊔ ((z \ x) ⊓ (z \ y)) := calc z \ (x \ y ⊔ y \ x) = z \ (x \ y) ⊓ (z \ (y \ x)) : sdiff_sup ... = (z \ x ⊔ z ⊓ x ⊓ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sdiff_sdiff_right, sdiff_sdiff_right] ... = (z \ x ⊔ z ⊓ y ⊓ x) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by ac_refl ... = (z \ x) ⊓ (z \ y) ⊔ z ⊓ y ⊓ x : sup_inf_right.symm ... = z ⊓ x ⊓ y ⊔ ((z \ x) ⊓ (z \ y)) : by ac_refl lemma inf_sdiff : (x ⊓ y) \ z = (x \ z) ⊓ (y \ z) := sdiff_unique (calc (x ⊓ y) ⊓ z ⊔ ((x \ z) ⊓ (y \ z)) = ((x ⊓ y) ⊓ z ⊔ (x \ z)) ⊓ ((x ⊓ y) ⊓ z ⊔ (y \ z)) : by rw [sup_inf_left] ... = (x ⊓ y ⊓ (z ⊔ x) ⊔ x \ z) ⊓ (x ⊓ y ⊓ z ⊔ y \ z) : by rw [sup_inf_right, sup_sdiff_self_right, inf_sup_right, inf_sdiff_sup_right] ... = (y ⊓ (x ⊓ (x ⊔ z)) ⊔ x \ z) ⊓ (x ⊓ y ⊓ z ⊔ y \ z) : by ac_refl ... = ((y ⊓ x) ⊔ (x \ z)) ⊓ ((x ⊓ y) ⊔ (y \ z)) : by rw [inf_sup_self, sup_inf_inf_sdiff] ... = (x ⊓ y) ⊔ ((x \ z) ⊓ (y \ z)) : by rw [@inf_comm _ _ y, sup_inf_left] ... = x ⊓ y : sup_eq_left.2 (inf_le_inf sdiff_le sdiff_le)) (calc (x ⊓ y) ⊓ z ⊓ ((x \ z) ⊓ (y \ z)) = x ⊓ y ⊓ (z ⊓ (x \ z)) ⊓ (y \ z) : by ac_refl ... = ⊥ : by rw [inf_sdiff_self_right, inf_bot_eq, bot_inf_eq]) lemma inf_sdiff_assoc : (x ⊓ y) \ z = x ⊓ (y \ z) := sdiff_unique (calc x ⊓ y ⊓ z ⊔ x ⊓ (y \ z) = x ⊓ (y ⊓ z) ⊔ x ⊓ (y \ z) : by rw inf_assoc ... = x ⊓ ((y ⊓ z) ⊔ y \ z) : inf_sup_left.symm ... = x ⊓ y : by rw sup_inf_sdiff) (calc x ⊓ y ⊓ z ⊓ (x ⊓ (y \ z)) = x ⊓ x ⊓ ((y ⊓ z) ⊓ (y \ z)) : by ac_refl ... = ⊥ : by rw [inf_inf_sdiff, inf_bot_eq]) lemma inf_sdiff_right_comm : x \ z ⊓ y = (x ⊓ y) \ z := by rw [@inf_comm _ _ x, inf_comm, inf_sdiff_assoc] lemma inf_sdiff_distrib_left (a b c : α) : a ⊓ b \ c = (a ⊓ b) \ (a ⊓ c) := by rw [sdiff_inf, sdiff_eq_bot_iff.2 inf_le_left, bot_sup_eq, inf_sdiff_assoc] lemma inf_sdiff_distrib_right (a b c : α) : a \ b ⊓ c = (a ⊓ c) \ (b ⊓ c) := by simp_rw [@inf_comm _ _ _ c, inf_sdiff_distrib_left] lemma sup_eq_sdiff_sup_sdiff_sup_inf : x ⊔ y = (x \ y) ⊔ (y \ x) ⊔ (x ⊓ y) := eq.symm $ calc (x \ y) ⊔ (y \ x) ⊔ (x ⊓ y) = ((x \ y) ⊔ (y \ x) ⊔ x) ⊓ ((x \ y) ⊔ (y \ x) ⊔ y) : by rw sup_inf_left ... = ((x \ y) ⊔ x ⊔ (y \ x)) ⊓ ((x \ y) ⊔ ((y \ x) ⊔ y)) : by ac_refl ... = (x ⊔ (y \ x)) ⊓ ((x \ y) ⊔ y) : by rw [sup_sdiff_right, sup_sdiff_right] ... = x ⊔ y : by rw [sup_sdiff_self_right, sup_sdiff_self_left, inf_idem] lemma sup_lt_of_lt_sdiff_left (h : y < z \ x) (hxz : x ≤ z) : x ⊔ y < z := begin rw ←sup_sdiff_cancel_right hxz, refine (sup_le_sup_left h.le _).lt_of_not_le (λ h', h.not_le _), rw ←sdiff_idem, exact (sdiff_le_sdiff_of_sup_le_sup_left h').trans sdiff_le, end lemma sup_lt_of_lt_sdiff_right (h : x < z \ y) (hyz : y ≤ z) : x ⊔ y < z := begin rw ←sdiff_sup_cancel hyz, refine (sup_le_sup_right h.le _).lt_of_not_le (λ h', h.not_le _), rw ←sdiff_idem, exact (sdiff_le_sdiff_of_sup_le_sup_right h').trans sdiff_le, end instance pi.generalized_boolean_algebra {α : Type u} {β : Type v} [generalized_boolean_algebra β] : generalized_boolean_algebra (α → β) := by pi_instance end generalized_boolean_algebra /-! ### Boolean algebras -/ /-- A Boolean algebra is a bounded distributive lattice with a complement operator `ᶜ` such that `x ⊓ xᶜ = ⊥` and `x ⊔ xᶜ = ⊤`. For convenience, it must also provide a set difference operation `\` and a Heyting implication `⇨` satisfying `x \ y = x ⊓ yᶜ` and `x ⇨ y = y ⊔ xᶜ`. This is a generalization of (classical) logic of propositions, or the powerset lattice. Since `bounded_order`, `order_bot`, and `order_top` are mixins that require `has_le` to be present at define-time, the `extends` mechanism does not work with them. Instead, we extend using the underlying `has_bot` and `has_top` data typeclasses, and replicate the order axioms of those classes here. A "forgetful" instance back to `bounded_order` is provided. -/ class boolean_algebra (α : Type u) extends distrib_lattice α, has_compl α, has_sdiff α, has_himp α, has_top α, has_bot α := (inf_compl_le_bot : ∀x:α, x ⊓ xᶜ ≤ ⊥) (top_le_sup_compl : ∀x:α, ⊤ ≤ x ⊔ xᶜ) (le_top : ∀ a : α, a ≤ ⊤) (bot_le : ∀ a : α, ⊥ ≤ a) (sdiff := λ x y, x ⊓ yᶜ) (himp := λ x y, y ⊔ xᶜ) (sdiff_eq : ∀ x y : α, x \ y = x ⊓ yᶜ . obviously) (himp_eq : ∀ x y : α, x ⇨ y = y ⊔ xᶜ . obviously) @[priority 100] -- see Note [lower instance priority] instance boolean_algebra.to_bounded_order [h : boolean_algebra α] : bounded_order α := { ..h } /-- A bounded generalized boolean algebra is a boolean algebra. -/ @[reducible] -- See note [reducible non instances] def generalized_boolean_algebra.to_boolean_algebra [generalized_boolean_algebra α] [order_top α] : boolean_algebra α := { compl := λ a, ⊤ \ a, inf_compl_le_bot := λ _, disjoint_sdiff_self_right.le_bot, top_le_sup_compl := λ _, le_sup_sdiff, sdiff_eq := λ _ _, by { rw [←inf_sdiff_assoc, inf_top_eq], refl }, ..‹generalized_boolean_algebra α›, ..generalized_boolean_algebra.to_order_bot, ..‹order_top α› } section boolean_algebra variables [boolean_algebra α] @[simp] lemma inf_compl_eq_bot' : x ⊓ xᶜ = ⊥ := bot_unique $ boolean_algebra.inf_compl_le_bot x @[simp] lemma sup_compl_eq_top : x ⊔ xᶜ = ⊤ := top_unique $ boolean_algebra.top_le_sup_compl x @[simp] lemma compl_sup_eq_top : xᶜ ⊔ x = ⊤ := sup_comm.trans sup_compl_eq_top lemma is_compl_compl : is_compl x xᶜ := is_compl.of_eq inf_compl_eq_bot' sup_compl_eq_top lemma sdiff_eq : x \ y = x ⊓ yᶜ := boolean_algebra.sdiff_eq x y lemma himp_eq : x ⇨ y = y ⊔ xᶜ := boolean_algebra.himp_eq x y @[priority 100] instance boolean_algebra.to_complemented_lattice : complemented_lattice α := ⟨λ x, ⟨xᶜ, is_compl_compl⟩⟩ @[priority 100] -- see Note [lower instance priority] instance boolean_algebra.to_generalized_boolean_algebra : generalized_boolean_algebra α := { sup_inf_sdiff := λ a b, by rw [sdiff_eq, ←inf_sup_left, sup_compl_eq_top, inf_top_eq], inf_inf_sdiff := λ a b, by { rw [sdiff_eq, ←inf_inf_distrib_left, inf_compl_eq_bot', inf_bot_eq], congr }, ..‹boolean_algebra α› } @[priority 100] -- See note [lower instance priority] instance boolean_algebra.to_biheyting_algebra : biheyting_algebra α := { hnot := compl, le_himp_iff := λ a b c, by rw [himp_eq, is_compl_compl.le_sup_right_iff_inf_left_le], himp_bot := λ _, himp_eq.trans bot_sup_eq, top_sdiff := λ a, by rw [sdiff_eq, top_inf_eq], ..‹boolean_algebra α›, ..generalized_boolean_algebra.to_generalized_coheyting_algebra } @[simp] lemma hnot_eq_compl : ¬x = xᶜ := rfl @[simp] lemma top_sdiff : ⊤ \ x = xᶜ := top_sdiff' _ theorem eq_compl_iff_is_compl : x = yᶜ ↔ is_compl x y := ⟨λ h, by { rw h, exact is_compl_compl.symm }, is_compl.eq_compl⟩ theorem compl_eq_iff_is_compl : xᶜ = y ↔ is_compl x y := ⟨λ h, by { rw ←h, exact is_compl_compl }, is_compl.compl_eq⟩ theorem compl_eq_comm : xᶜ = y ↔ yᶜ = x := by rw [eq_comm, compl_eq_iff_is_compl, eq_compl_iff_is_compl] theorem eq_compl_comm : x = yᶜ ↔ y = xᶜ := by rw [eq_comm, compl_eq_iff_is_compl, eq_compl_iff_is_compl] @[simp] theorem compl_compl (x : α) : xᶜᶜ = x := (@is_compl_compl _ x _).symm.compl_eq theorem compl_comp_compl : compl ∘ compl = @id α := funext compl_compl @[simp] theorem compl_involutive : function.involutive (compl : α → α) := compl_compl theorem compl_bijective : function.bijective (compl : α → α) := compl_involutive.bijective theorem compl_surjective : function.surjective (compl : α → α) := compl_involutive.surjective theorem compl_injective : function.injective (compl : α → α) := compl_involutive.injective @[simp] theorem compl_inj_iff : xᶜ = yᶜ ↔ x = y := compl_injective.eq_iff theorem is_compl.compl_eq_iff (h : is_compl x y) : zᶜ = y ↔ z = x := h.compl_eq ▸ compl_inj_iff @[simp] theorem compl_eq_top : xᶜ = ⊤ ↔ x = ⊥ := is_compl_bot_top.compl_eq_iff @[simp] theorem compl_eq_bot : xᶜ = ⊥ ↔ x = ⊤ := is_compl_top_bot.compl_eq_iff @[simp] theorem compl_inf : (x ⊓ y)ᶜ = xᶜ ⊔ yᶜ := hnot_inf_distrib _ _ @[simp] theorem compl_le_compl_iff_le : yᶜ ≤ xᶜ ↔ x ≤ y := ⟨assume h, by have h := compl_le_compl h; simp at h; assumption, compl_le_compl⟩ theorem compl_le_of_compl_le (h : yᶜ ≤ x) : xᶜ ≤ y := by simpa only [compl_compl] using compl_le_compl h theorem compl_le_iff_compl_le : xᶜ ≤ y ↔ yᶜ ≤ x := ⟨compl_le_of_compl_le, compl_le_of_compl_le⟩ @[simp] lemma sdiff_compl : x \ yᶜ = x ⊓ y := by rw [sdiff_eq, compl_compl] instance : boolean_algebra αᵒᵈ := { compl := λ a, to_dual (of_dual a)ᶜ, sdiff := λ a b, to_dual (of_dual b ⇨ of_dual a), himp := λ a b, to_dual (of_dual b \ of_dual a), inf_compl_le_bot := λ a, (@codisjoint_hnot_right _ _ (of_dual a)).top_le, top_le_sup_compl := λ a, (@disjoint_compl_right _ _ (of_dual a)).le_bot, sdiff_eq := λ _ _, himp_eq, himp_eq := λ _ _, sdiff_eq, ..order_dual.distrib_lattice α, ..order_dual.bounded_order α } @[simp] lemma sup_inf_inf_compl : (x ⊓ y) ⊔ (x ⊓ yᶜ) = x := by rw [← sdiff_eq, sup_inf_sdiff _ _] @[simp] lemma compl_sdiff : (x \ y)ᶜ = x ⇨ y := by rw [sdiff_eq, himp_eq, compl_inf, compl_compl, sup_comm] @[simp] lemma compl_himp : (x ⇨ y)ᶜ = x \ y := @compl_sdiff αᵒᵈ _ _ _ @[simp] lemma compl_sdiff_compl : xᶜ \ yᶜ = y \ x := by rw [sdiff_compl, sdiff_eq, inf_comm] @[simp] lemma compl_himp_compl : xᶜ ⇨ yᶜ = y ⇨ x := @compl_sdiff_compl αᵒᵈ _ _ _ lemma disjoint_compl_left_iff : disjoint xᶜ y ↔ y ≤ x := by rw [←le_compl_iff_disjoint_left, compl_compl] lemma disjoint_compl_right_iff : disjoint x yᶜ ↔ x ≤ y := by rw [←le_compl_iff_disjoint_right, compl_compl] end boolean_algebra instance Prop.boolean_algebra : boolean_algebra Prop := { compl := not, himp_eq := λ p q, propext imp_iff_or_not, inf_compl_le_bot := λ p ⟨Hp, Hpc⟩, Hpc Hp, top_le_sup_compl := λ p H, classical.em p, .. Prop.heyting_algebra, ..generalized_heyting_algebra.to_distrib_lattice } instance pi.boolean_algebra {ι : Type u} {α : ι → Type v} [∀ i, boolean_algebra (α i)] : boolean_algebra (Π i, α i) := { sdiff_eq := λ x y, funext $ λ i, sdiff_eq, himp_eq := λ x y, funext $ λ i, himp_eq, inf_compl_le_bot := λ _ _, boolean_algebra.inf_compl_le_bot _, top_le_sup_compl := λ _ _, boolean_algebra.top_le_sup_compl _, .. pi.has_sdiff, .. pi.heyting_algebra, .. pi.distrib_lattice } instance : boolean_algebra bool := { sup := bor, le_sup_left := bool.left_le_bor, le_sup_right := bool.right_le_bor, sup_le := λ _ _ _, bool.bor_le, inf := band, inf_le_left := bool.band_le_left, inf_le_right := bool.band_le_right, le_inf := λ _ _ _, bool.le_band, le_sup_inf := dec_trivial, compl := bnot, inf_compl_le_bot := λ a, a.band_bnot_self.le, top_le_sup_compl := λ a, a.bor_bnot_self.ge, ..bool.linear_order, ..bool.bounded_order } @[simp] lemma bool.sup_eq_bor : (⊔) = bor := rfl @[simp] lemma bool.inf_eq_band : (⊓) = band := rfl @[simp] lemma bool.compl_eq_bnot : has_compl.compl = bnot := rfl section lift /-- Pullback a `generalized_boolean_algebra` along an injection. -/ @[reducible] -- See note [reducible non-instances] protected def function.injective.generalized_boolean_algebra [has_sup α] [has_inf α] [has_bot α] [has_sdiff α] [generalized_boolean_algebra β] (f : α → β) (hf : injective f) (map_sup : ∀ a b, f (a ⊔ b) = f a ⊔ f b) (map_inf : ∀ a b, f (a ⊓ b) = f a ⊓ f b) (map_bot : f ⊥ = ⊥) (map_sdiff : ∀ a b, f (a \ b) = f a \ f b) : generalized_boolean_algebra α := { sup_inf_sdiff := λ a b, hf $ by erw [map_sup, map_sdiff, map_inf, sup_inf_sdiff], inf_inf_sdiff := λ a b, hf $ by erw [map_inf, map_sdiff, map_inf, inf_inf_sdiff, map_bot], ..hf.generalized_coheyting_algebra f map_sup map_inf map_bot map_sdiff, ..hf.distrib_lattice f map_sup map_inf } /-- Pullback a `boolean_algebra` along an injection. -/ @[reducible] -- See note [reducible non-instances] protected def function.injective.boolean_algebra [has_sup α] [has_inf α] [has_top α] [has_bot α] [has_compl α] [has_sdiff α] [boolean_algebra β] (f : α → β) (hf : injective f) (map_sup : ∀ a b, f (a ⊔ b) = f a ⊔ f b) (map_inf : ∀ a b, f (a ⊓ b) = f a ⊓ f b) (map_top : f ⊤ = ⊤) (map_bot : f ⊥ = ⊥) (map_compl : ∀ a, f aᶜ = (f a)ᶜ) (map_sdiff : ∀ a b, f (a \ b) = f a \ f b) : boolean_algebra α := { compl := compl, top := ⊤, le_top := λ a, (@le_top β _ _ _).trans map_top.ge, bot_le := λ a, map_bot.le.trans bot_le, inf_compl_le_bot := λ a, ((map_inf _ _).trans $ by rw [map_compl, inf_compl_eq_bot, map_bot]).le, top_le_sup_compl := λ a, ((map_sup _ _).trans $ by rw [map_compl, sup_compl_eq_top, map_top]).ge, sdiff_eq := λ a b, hf $ (map_sdiff _ _).trans $ sdiff_eq.trans $ by { convert (map_inf _ _).symm, exact (map_compl _).symm }, ..hf.generalized_boolean_algebra f map_sup map_inf map_bot map_sdiff } end lift instance : boolean_algebra punit := by refine_struct { ..punit.biheyting_algebra }; intros; trivial <|> exact subsingleton.elim _ _
51d408308602e3ac1842886b46533c70414b13e9
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/geometry/manifold/mfderiv.lean
374aff8d3eb8c3a7372b3ea593a21f60ea464d9e
[ "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
71,146
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import geometry.manifold.basic_smooth_bundle /-! # The derivative of functions between smooth manifolds Let `M` and `M'` be two smooth manifolds with corners over a field `𝕜` (with respective models with corners `I` on `(E, H)` and `I'` on `(E', H')`), and let `f : M → M'`. We define the derivative of the function at a point, within a set or along the whole space, mimicking the API for (Fréchet) derivatives. It is denoted by `mfderiv I I' f x`, where "m" stands for "manifold" and "f" for "Fréchet" (as in the usual derivative `fderiv 𝕜 f x`). ## Main definitions * `unique_mdiff_on I s` : predicate saying that, at each point of the set `s`, a function can have at most one derivative. This technical condition is important when we define `mfderiv_within` below, as otherwise there is an arbitrary choice in the derivative, and many properties will fail (for instance the chain rule). This is analogous to `unique_diff_on 𝕜 s` in a vector space. Let `f` be a map between smooth manifolds. The following definitions follow the `fderiv` API. * `mfderiv I I' f x` : the derivative of `f` at `x`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. If the map is not differentiable, this is `0`. * `mfderiv_within I I' f s x` : the derivative of `f` at `x` within `s`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. If the map is not differentiable within `s`, this is `0`. * `mdifferentiable_at I I' f x` : Prop expressing whether `f` is differentiable at `x`. * `mdifferentiable_within_at 𝕜 f s x` : Prop expressing whether `f` is differentiable within `s` at `x`. * `has_mfderiv_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative at `x`. * `has_mfderiv_within_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative within `s` at `x`. * `mdifferentiable_on I I' f s` : Prop expressing that `f` is differentiable on the set `s`. * `mdifferentiable I I' f` : Prop expressing that `f` is differentiable everywhere. * `tangent_map I I' f` : the derivative of `f`, as a map from the tangent bundle of `M` to the tangent bundle of `M'`. We also establish results on the differential of the identity, constant functions, charts, extended charts. For functions between vector spaces, we show that the usual notions and the manifold notions coincide. ## Implementation notes The tangent bundle is constructed using the machinery of topological fiber bundles, for which one can define bundled morphisms and construct canonically maps from the total space of one bundle to the total space of another one. One could use this mechanism to construct directly the derivative of a smooth map. However, we want to define the derivative of any map (and let it be zero if the map is not differentiable) to avoid proof arguments everywhere. This means we have to go back to the details of the definition of the total space of a fiber bundle constructed from core, to cook up a suitable definition of the derivative. It is the following: at each point, we have a preferred chart (used to identify the fiber above the point with the model vector space in fiber bundles). Then one should read the function using these preferred charts at `x` and `f x`, and take the derivative of `f` in these charts. Due to the fact that we are working in a model with corners, with an additional embedding `I` of the model space `H` in the model vector space `E`, the charts taking values in `E` are not the original charts of the manifold, but those ones composed with `I`, called extended charts. We define `written_in_ext_chart I I' x f` for the function `f` written in the preferred extended charts. Then the manifold derivative of `f`, at `x`, is just the usual derivative of `written_in_ext_chart I I' x f`, at the point `(ext_chart_at I x) x`. There is a subtelty with respect to continuity: if the function is not continuous, then the image of a small open set around `x` will not be contained in the source of the preferred chart around `f x`, which means that when reading `f` in the chart one is losing some information. To avoid this, we include continuity in the definition of differentiablity (which is reasonable since with any definition, differentiability implies continuity). *Warning*: the derivative (even within a subset) is a linear map on the whole tangent space. Suppose that one is given a smooth submanifold `N`, and a function which is smooth on `N` (i.e., its restriction to the subtype `N` is smooth). Then, in the whole manifold `M`, the property `mdifferentiable_on I I' f N` holds. However, `mfderiv_within I I' f N` is not uniquely defined (what values would one choose for vectors that are transverse to `N`?), which can create issues down the road. The problem here is that knowing the value of `f` along `N` does not determine the differential of `f` in all directions. This is in contrast to the case where `N` would be an open subset, or a submanifold with boundary of maximal dimension, where this issue does not appear. The predicate `unique_mdiff_on I N` indicates that the derivative along `N` is unique if it exists, and is an assumption in most statements requiring a form of uniqueness. On a vector space, the manifold derivative and the usual derivative are equal. This means in particular that they live on the same space, i.e., the tangent space is defeq to the original vector space. To get this property is a motivation for our definition of the tangent space as a single copy of the vector space, instead of more usual definitions such as the space of derivations, or the space of equivalence classes of smooth curves in the manifold. ## Tags Derivative, manifold -/ noncomputable theory open_locale classical topological_space manifold open set universe u section derivatives_definitions /-! ### Derivative of maps between manifolds The derivative of a smooth map `f` between smooth manifold `M` and `M'` at `x` is a bounded linear map from the tangent space to `M` at `x`, to the tangent space to `M'` at `f x`. Since we defined the tangent space using one specific chart, the formula for the derivative is written in terms of this specific chart. We use the names `mdifferentiable` and `mfderiv`, where the prefix letter `m` means "manifold". -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H') {M' : Type*} [topological_space M'] [charted_space H' M'] /-- Predicate ensuring that, at a point and within a set, a function can have at most one derivative. This is expressed using the preferred chart at the considered point. -/ def unique_mdiff_within_at (s : set M) (x : M) := unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) /-- Predicate ensuring that, at all points of a set, a function can have at most one derivative. -/ def unique_mdiff_on (s : set M) := ∀x∈s, unique_mdiff_within_at I s x /-- Conjugating a function to write it in the preferred charts around `x`. The manifold derivative of `f` will just be the derivative of this conjugated function. -/ @[simp, mfld_simps] def written_in_ext_chart_at (x : M) (f : M → M') : E → E' := (ext_chart_at I' (f x)) ∘ f ∘ (ext_chart_at I x).symm /-- `mdifferentiable_within_at I I' f s x` indicates that the function `f` between manifolds has a derivative at the point `x` within the set `s`. This is a generalization of `differentiable_within_at` to manifolds. We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def mdifferentiable_within_at (f : M → M') (s : set M) (x : M) := continuous_within_at f s x ∧ differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) /-- `mdifferentiable_at I I' f x` indicates that the function `f` between manifolds has a derivative at the point `x`. This is a generalization of `differentiable_at` to manifolds. We require continuity in the definition, as otherwise points close to `x` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def mdifferentiable_at (f : M → M') (x : M) := continuous_at f x ∧ differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) (range I) ((ext_chart_at I x) x) /-- `mdifferentiable_on I I' f s` indicates that the function `f` between manifolds has a derivative within `s` at all points of `s`. This is a generalization of `differentiable_on` to manifolds. -/ def mdifferentiable_on (f : M → M') (s : set M) := ∀x ∈ s, mdifferentiable_within_at I I' f s x /-- `mdifferentiable I I' f` indicates that the function `f` between manifolds has a derivative everywhere. This is a generalization of `differentiable` to manifolds. -/ def mdifferentiable (f : M → M') := ∀x, mdifferentiable_at I I' f x /-- Prop registering if a local homeomorphism is a local diffeomorphism on its source -/ def local_homeomorph.mdifferentiable (f : local_homeomorph M M') := (mdifferentiable_on I I' f f.source) ∧ (mdifferentiable_on I' I f.symm f.target) variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M'] /-- `has_mfderiv_within_at I I' f s x f'` indicates that the function `f` between manifolds has, at the point `x` and within the set `s`, the derivative `f'`. Here, `f'` is a continuous linear map from the tangent space at `x` to the tangent space at `f x`. This is a generalization of `has_fderiv_within_at` to manifolds (as indicated by the prefix `m`). The order of arguments is changed as the type of the derivative `f'` depends on the choice of `x`. We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def has_mfderiv_within_at (f : M → M') (s : set M) (x : M) (f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) := continuous_within_at f s x ∧ has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f' ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) /-- `has_mfderiv_at I I' f x f'` indicates that the function `f` between manifolds has, at the point `x`, the derivative `f'`. Here, `f'` is a continuous linear map from the tangent space at `x` to the tangent space at `f x`. We require continuity in the definition, as otherwise points close to `x` `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def has_mfderiv_at (f : M → M') (x : M) (f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) := continuous_at f x ∧ has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f' (range I) ((ext_chart_at I x) x) /-- Let `f` be a function between two smooth manifolds. Then `mfderiv_within I I' f s x` is the derivative of `f` at `x` within `s`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. -/ def mfderiv_within (f : M → M') (s : set M) (x : M) : tangent_space I x →L[𝕜] tangent_space I' (f x) := if h : mdifferentiable_within_at I I' f s x then (fderiv_within 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) : _) else 0 /-- Let `f` be a function between two smooth manifolds. Then `mfderiv I I' f x` is the derivative of `f` at `x`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. -/ def mfderiv (f : M → M') (x : M) : tangent_space I x →L[𝕜] tangent_space I' (f x) := if h : mdifferentiable_at I I' f x then (fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : E → E') (range I) ((ext_chart_at I x) x) : _) else 0 /-- The derivative within a set, as a map between the tangent bundles -/ def tangent_map_within (f : M → M') (s : set M) : tangent_bundle I M → tangent_bundle I' M' := λp, ⟨f p.1, (mfderiv_within I I' f s p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩ /-- The derivative, as a map between the tangent bundles -/ def tangent_map (f : M → M') : tangent_bundle I M → tangent_bundle I' M' := λp, ⟨f p.1, (mfderiv I I' f p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩ end derivatives_definitions section derivatives_properties /-! ### Unique differentiability sets in manifolds -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] -- {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} {M' : Type*} [topological_space M'] [charted_space H' M'] {E'' : Type*} [normed_group E''] [normed_space 𝕜 E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''} {M'' : Type*} [topological_space M''] [charted_space H'' M''] {f f₀ f₁ : M → M'} {x : M} {s t : set M} {g : M' → M''} {u : set M'} lemma unique_mdiff_within_at_univ : unique_mdiff_within_at I univ x := begin unfold unique_mdiff_within_at, simp only [preimage_univ, univ_inter], exact I.unique_diff _ (mem_range_self _) end variable {I} lemma unique_mdiff_within_at_iff {s : set M} {x : M} : unique_mdiff_within_at I s x ↔ unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ (ext_chart_at I x).target) ((ext_chart_at I x) x) := begin apply unique_diff_within_at_congr, rw [nhds_within_inter, nhds_within_inter, nhds_within_ext_chart_target_eq] end lemma unique_mdiff_within_at.mono (h : unique_mdiff_within_at I s x) (st : s ⊆ t) : unique_mdiff_within_at I t x := unique_diff_within_at.mono h $ inter_subset_inter (preimage_mono st) (subset.refl _) lemma unique_mdiff_within_at.inter' (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝[s] x) : unique_mdiff_within_at I (s ∩ t) x := begin rw [unique_mdiff_within_at, ext_chart_preimage_inter_eq], exact unique_diff_within_at.inter' hs (ext_chart_preimage_mem_nhds_within I x ht) end lemma unique_mdiff_within_at.inter (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝 x) : unique_mdiff_within_at I (s ∩ t) x := begin rw [unique_mdiff_within_at, ext_chart_preimage_inter_eq], exact unique_diff_within_at.inter hs (ext_chart_preimage_mem_nhds I x ht) end lemma is_open.unique_mdiff_within_at (xs : x ∈ s) (hs : is_open s) : unique_mdiff_within_at I s x := begin have := unique_mdiff_within_at.inter (unique_mdiff_within_at_univ I) (is_open.mem_nhds hs xs), rwa univ_inter at this end lemma unique_mdiff_on.inter (hs : unique_mdiff_on I s) (ht : is_open t) : unique_mdiff_on I (s ∩ t) := λx hx, unique_mdiff_within_at.inter (hs _ hx.1) (is_open.mem_nhds ht hx.2) lemma is_open.unique_mdiff_on (hs : is_open s) : unique_mdiff_on I s := λx hx, is_open.unique_mdiff_within_at hx hs lemma unique_mdiff_on_univ : unique_mdiff_on I (univ : set M) := is_open_univ.unique_mdiff_on /- We name the typeclass variables related to `smooth_manifold_with_corners` structure as they are necessary in lemmas mentioning the derivative, but not in lemmas about differentiability, so we want to include them or omit them when necessary. -/ variables [Is : smooth_manifold_with_corners I M] [I's : smooth_manifold_with_corners I' M'] [I''s : smooth_manifold_with_corners I'' M''] {f' f₀' f₁' : tangent_space I x →L[𝕜] tangent_space I' (f x)} {g' : tangent_space I' (f x) →L[𝕜] tangent_space I'' (g (f x))} /-- `unique_mdiff_within_at` achieves its goal: it implies the uniqueness of the derivative. -/ theorem unique_mdiff_within_at.eq (U : unique_mdiff_within_at I s x) (h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') : f' = f₁' := U.eq h.2 h₁.2 theorem unique_mdiff_on.eq (U : unique_mdiff_on I s) (hx : x ∈ s) (h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') : f' = f₁' := unique_mdiff_within_at.eq (U _ hx) h h₁ /-! ### General lemmas on derivatives of functions between manifolds We mimick the API for functions between vector spaces -/ lemma mdifferentiable_within_at_iff {f : M → M'} {s : set M} {x : M} : mdifferentiable_within_at I I' f s x ↔ continuous_within_at f s x ∧ differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' s) ((ext_chart_at I x) x) := begin refine and_congr iff.rfl (exists_congr $ λ f', _), rw [inter_comm], simp only [has_fderiv_within_at, nhds_within_inter, nhds_within_ext_chart_target_eq] end include Is I's lemma mfderiv_within_zero_of_not_mdifferentiable_within_at (h : ¬ mdifferentiable_within_at I I' f s x) : mfderiv_within I I' f s x = 0 := by simp only [mfderiv_within, h, dif_neg, not_false_iff] lemma mfderiv_zero_of_not_mdifferentiable_at (h : ¬ mdifferentiable_at I I' f x) : mfderiv I I' f x = 0 := by simp only [mfderiv, h, dif_neg, not_false_iff] theorem has_mfderiv_within_at.mono (h : has_mfderiv_within_at I I' f t x f') (hst : s ⊆ t) : has_mfderiv_within_at I I' f s x f' := ⟨ continuous_within_at.mono h.1 hst, has_fderiv_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩ theorem has_mfderiv_at.has_mfderiv_within_at (h : has_mfderiv_at I I' f x f') : has_mfderiv_within_at I I' f s x f' := ⟨ continuous_at.continuous_within_at h.1, has_fderiv_within_at.mono h.2 (inter_subset_right _ _) ⟩ lemma has_mfderiv_within_at.mdifferentiable_within_at (h : has_mfderiv_within_at I I' f s x f') : mdifferentiable_within_at I I' f s x := ⟨h.1, ⟨f', h.2⟩⟩ lemma has_mfderiv_at.mdifferentiable_at (h : has_mfderiv_at I I' f x f') : mdifferentiable_at I I' f x := ⟨h.1, ⟨f', h.2⟩⟩ @[simp, mfld_simps] lemma has_mfderiv_within_at_univ : has_mfderiv_within_at I I' f univ x f' ↔ has_mfderiv_at I I' f x f' := by simp only [has_mfderiv_within_at, has_mfderiv_at, continuous_within_at_univ] with mfld_simps theorem has_mfderiv_at_unique (h₀ : has_mfderiv_at I I' f x f₀') (h₁ : has_mfderiv_at I I' f x f₁') : f₀' = f₁' := begin rw ← has_mfderiv_within_at_univ at h₀ h₁, exact (unique_mdiff_within_at_univ I).eq h₀ h₁ end lemma has_mfderiv_within_at_inter' (h : t ∈ 𝓝[s] x) : has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' := begin rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_preimage_inter_eq, has_fderiv_within_at_inter', continuous_within_at_inter' h], exact ext_chart_preimage_mem_nhds_within I x h, end lemma has_mfderiv_within_at_inter (h : t ∈ 𝓝 x) : has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' := begin rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_preimage_inter_eq, has_fderiv_within_at_inter, continuous_within_at_inter h], exact ext_chart_preimage_mem_nhds I x h, end lemma has_mfderiv_within_at.union (hs : has_mfderiv_within_at I I' f s x f') (ht : has_mfderiv_within_at I I' f t x f') : has_mfderiv_within_at I I' f (s ∪ t) x f' := begin split, { exact continuous_within_at.union hs.1 ht.1 }, { convert has_fderiv_within_at.union hs.2 ht.2, simp only [union_inter_distrib_right, preimage_union] } end lemma has_mfderiv_within_at.nhds_within (h : has_mfderiv_within_at I I' f s x f') (ht : s ∈ 𝓝[t] x) : has_mfderiv_within_at I I' f t x f' := (has_mfderiv_within_at_inter' ht).1 (h.mono (inter_subset_right _ _)) lemma has_mfderiv_within_at.has_mfderiv_at (h : has_mfderiv_within_at I I' f s x f') (hs : s ∈ 𝓝 x) : has_mfderiv_at I I' f x f' := by rwa [← univ_inter s, has_mfderiv_within_at_inter hs, has_mfderiv_within_at_univ] at h lemma mdifferentiable_within_at.has_mfderiv_within_at (h : mdifferentiable_within_at I I' f s x) : has_mfderiv_within_at I I' f s x (mfderiv_within I I' f s x) := begin refine ⟨h.1, _⟩, simp only [mfderiv_within, h, dif_pos] with mfld_simps, exact differentiable_within_at.has_fderiv_within_at h.2 end lemma mdifferentiable_within_at.mfderiv_within (h : mdifferentiable_within_at I I' f s x) : (mfderiv_within I I' f s x) = fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) := by simp only [mfderiv_within, h, dif_pos] lemma mdifferentiable_at.has_mfderiv_at (h : mdifferentiable_at I I' f x) : has_mfderiv_at I I' f x (mfderiv I I' f x) := begin refine ⟨h.1, _⟩, simp only [mfderiv, h, dif_pos] with mfld_simps, exact differentiable_within_at.has_fderiv_within_at h.2 end lemma mdifferentiable_at.mfderiv (h : mdifferentiable_at I I' f x) : (mfderiv I I' f x) = fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) (range I) ((ext_chart_at I x) x) := by simp only [mfderiv, h, dif_pos] lemma has_mfderiv_at.mfderiv (h : has_mfderiv_at I I' f x f') : mfderiv I I' f x = f' := (has_mfderiv_at_unique h h.mdifferentiable_at.has_mfderiv_at).symm lemma has_mfderiv_within_at.mfderiv_within (h : has_mfderiv_within_at I I' f s x f') (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' f s x = f' := by { ext, rw hxs.eq h h.mdifferentiable_within_at.has_mfderiv_within_at } lemma mdifferentiable.mfderiv_within (h : mdifferentiable_at I I' f x) (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' f s x = mfderiv I I' f x := begin apply has_mfderiv_within_at.mfderiv_within _ hxs, exact h.has_mfderiv_at.has_mfderiv_within_at end lemma mfderiv_within_subset (st : s ⊆ t) (hs : unique_mdiff_within_at I s x) (h : mdifferentiable_within_at I I' f t x) : mfderiv_within I I' f s x = mfderiv_within I I' f t x := ((mdifferentiable_within_at.has_mfderiv_within_at h).mono st).mfderiv_within hs omit Is I's lemma mdifferentiable_within_at.mono (hst : s ⊆ t) (h : mdifferentiable_within_at I I' f t x) : mdifferentiable_within_at I I' f s x := ⟨ continuous_within_at.mono h.1 hst, differentiable_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩ lemma mdifferentiable_within_at_univ : mdifferentiable_within_at I I' f univ x ↔ mdifferentiable_at I I' f x := by simp only [mdifferentiable_within_at, mdifferentiable_at, continuous_within_at_univ] with mfld_simps lemma mdifferentiable_within_at_inter (ht : t ∈ 𝓝 x) : mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x := begin rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_preimage_inter_eq, differentiable_within_at_inter, continuous_within_at_inter ht], exact ext_chart_preimage_mem_nhds I x ht end lemma mdifferentiable_within_at_inter' (ht : t ∈ 𝓝[s] x) : mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x := begin rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_preimage_inter_eq, differentiable_within_at_inter', continuous_within_at_inter' ht], exact ext_chart_preimage_mem_nhds_within I x ht end lemma mdifferentiable_at.mdifferentiable_within_at (h : mdifferentiable_at I I' f x) : mdifferentiable_within_at I I' f s x := mdifferentiable_within_at.mono (subset_univ _) (mdifferentiable_within_at_univ.2 h) lemma mdifferentiable_within_at.mdifferentiable_at (h : mdifferentiable_within_at I I' f s x) (hs : s ∈ 𝓝 x) : mdifferentiable_at I I' f x := begin have : s = univ ∩ s, by rw univ_inter, rwa [this, mdifferentiable_within_at_inter hs, mdifferentiable_within_at_univ] at h, end lemma mdifferentiable_on.mono (h : mdifferentiable_on I I' f t) (st : s ⊆ t) : mdifferentiable_on I I' f s := λx hx, (h x (st hx)).mono st lemma mdifferentiable_on_univ : mdifferentiable_on I I' f univ ↔ mdifferentiable I I' f := by { simp only [mdifferentiable_on, mdifferentiable_within_at_univ] with mfld_simps, refl } lemma mdifferentiable.mdifferentiable_on (h : mdifferentiable I I' f) : mdifferentiable_on I I' f s := (mdifferentiable_on_univ.2 h).mono (subset_univ _) lemma mdifferentiable_on_of_locally_mdifferentiable_on (h : ∀x∈s, ∃u, is_open u ∧ x ∈ u ∧ mdifferentiable_on I I' f (s ∩ u)) : mdifferentiable_on I I' f s := begin assume x xs, rcases h x xs with ⟨t, t_open, xt, ht⟩, exact (mdifferentiable_within_at_inter (is_open.mem_nhds t_open xt)).1 (ht x ⟨xs, xt⟩) end include Is I's @[simp, mfld_simps] lemma mfderiv_within_univ : mfderiv_within I I' f univ = mfderiv I I' f := begin ext x : 1, simp only [mfderiv_within, mfderiv] with mfld_simps, rw mdifferentiable_within_at_univ end lemma mfderiv_within_inter (ht : t ∈ 𝓝 x) (hs : unique_mdiff_within_at I s x) : mfderiv_within I I' f (s ∩ t) x = mfderiv_within I I' f s x := by rw [mfderiv_within, mfderiv_within, ext_chart_preimage_inter_eq, mdifferentiable_within_at_inter ht, fderiv_within_inter (ext_chart_preimage_mem_nhds I x ht) hs] omit Is I's /-! ### Deriving continuity from differentiability on manifolds -/ theorem has_mfderiv_within_at.continuous_within_at (h : mdifferentiable_within_at I I' f s x) : continuous_within_at f s x := h.1 theorem has_mfderiv_at.continuous_at (h : has_mfderiv_at I I' f x f') : continuous_at f x := h.1 lemma mdifferentiable_within_at.continuous_within_at (h : mdifferentiable_within_at I I' f s x) : continuous_within_at f s x := h.1 lemma mdifferentiable_at.continuous_at (h : mdifferentiable_at I I' f x) : continuous_at f x := h.1 lemma mdifferentiable_on.continuous_on (h : mdifferentiable_on I I' f s) : continuous_on f s := λx hx, (h x hx).continuous_within_at lemma mdifferentiable.continuous (h : mdifferentiable I I' f) : continuous f := continuous_iff_continuous_at.2 $ λx, (h x).continuous_at include Is I's lemma tangent_map_within_subset {p : tangent_bundle I M} (st : s ⊆ t) (hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_within_at I I' f t p.1) : tangent_map_within I I' f s p = tangent_map_within I I' f t p := begin simp only [tangent_map_within] with mfld_simps, rw mfderiv_within_subset st hs h, end lemma tangent_map_within_univ : tangent_map_within I I' f univ = tangent_map I I' f := by { ext p : 1, simp only [tangent_map_within, tangent_map] with mfld_simps } lemma tangent_map_within_eq_tangent_map {p : tangent_bundle I M} (hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_at I I' f p.1) : tangent_map_within I I' f s p = tangent_map I I' f p := begin rw ← mdifferentiable_within_at_univ at h, rw ← tangent_map_within_univ, exact tangent_map_within_subset (subset_univ _) hs h, end @[simp, mfld_simps] lemma tangent_map_within_tangent_bundle_proj {p : tangent_bundle I M} : tangent_bundle.proj I' M' (tangent_map_within I I' f s p) = f (tangent_bundle.proj I M p) := rfl @[simp, mfld_simps] lemma tangent_map_within_proj {p : tangent_bundle I M} : (tangent_map_within I I' f s p).1 = f p.1 := rfl @[simp, mfld_simps] lemma tangent_map_tangent_bundle_proj {p : tangent_bundle I M} : tangent_bundle.proj I' M' (tangent_map I I' f p) = f (tangent_bundle.proj I M p) := rfl @[simp, mfld_simps] lemma tangent_map_proj {p : tangent_bundle I M} : (tangent_map I I' f p).1 = f p.1 := rfl omit Is I's /-! ### Congruence lemmas for derivatives on manifolds -/ lemma has_mfderiv_within_at.congr_of_eventually_eq (h : has_mfderiv_within_at I I' f s x f') (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : has_mfderiv_within_at I I' f₁ s x f' := begin refine ⟨continuous_within_at.congr_of_eventually_eq h.1 h₁ hx, _⟩, apply has_fderiv_within_at.congr_of_eventually_eq h.2, { have : (ext_chart_at I x).symm ⁻¹' {y | f₁ y = f y} ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := ext_chart_preimage_mem_nhds_within I x h₁, apply filter.mem_of_superset this (λy, _), simp only [hx] with mfld_simps {contextual := tt} }, { simp only [hx] with mfld_simps }, end lemma has_mfderiv_within_at.congr_mono (h : has_mfderiv_within_at I I' f s x f') (ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : has_mfderiv_within_at I I' f₁ t x f' := (h.mono h₁).congr_of_eventually_eq (filter.mem_inf_of_right ht) hx lemma has_mfderiv_at.congr_of_eventually_eq (h : has_mfderiv_at I I' f x f') (h₁ : f₁ =ᶠ[𝓝 x] f) : has_mfderiv_at I I' f₁ x f' := begin rw ← has_mfderiv_within_at_univ at ⊢ h, apply h.congr_of_eventually_eq _ (mem_of_mem_nhds h₁ : _), rwa nhds_within_univ end include Is I's lemma mdifferentiable_within_at.congr_of_eventually_eq (h : mdifferentiable_within_at I I' f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x := (h.has_mfderiv_within_at.congr_of_eventually_eq h₁ hx).mdifferentiable_within_at variables (I I') lemma filter.eventually_eq.mdifferentiable_within_at_iff (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f s x ↔ mdifferentiable_within_at I I' f₁ s x := begin split, { assume h, apply h.congr_of_eventually_eq h₁ hx }, { assume h, apply h.congr_of_eventually_eq _ hx.symm, apply h₁.mono, intro y, apply eq.symm } end variables {I I'} lemma mdifferentiable_within_at.congr_mono (h : mdifferentiable_within_at I I' f s x) (ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : mdifferentiable_within_at I I' f₁ t x := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx h₁).mdifferentiable_within_at lemma mdifferentiable_within_at.congr (h : mdifferentiable_within_at I I' f s x) (ht : ∀x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx (subset.refl _)).mdifferentiable_within_at lemma mdifferentiable_on.congr_mono (h : mdifferentiable_on I I' f s) (h' : ∀x ∈ t, f₁ x = f x) (h₁ : t ⊆ s) : mdifferentiable_on I I' f₁ t := λ x hx, (h x (h₁ hx)).congr_mono h' (h' x hx) h₁ lemma mdifferentiable_at.congr_of_eventually_eq (h : mdifferentiable_at I I' f x) (hL : f₁ =ᶠ[𝓝 x] f) : mdifferentiable_at I I' f₁ x := ((h.has_mfderiv_at).congr_of_eventually_eq hL).mdifferentiable_at lemma mdifferentiable_within_at.mfderiv_within_congr_mono (h : mdifferentiable_within_at I I' f s x) (hs : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (hxt : unique_mdiff_within_at I t x) (h₁ : t ⊆ s) : mfderiv_within I I' f₁ t x = (mfderiv_within I I' f s x : _) := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at hs hx h₁).mfderiv_within hxt lemma filter.eventually_eq.mfderiv_within_eq (hs : unique_mdiff_within_at I s x) (hL : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) := begin by_cases h : mdifferentiable_within_at I I' f s x, { exact ((h.has_mfderiv_within_at).congr_of_eventually_eq hL hx).mfderiv_within hs }, { unfold mfderiv_within, rw [dif_neg h, dif_neg], rwa ← hL.mdifferentiable_within_at_iff I I' hx } end lemma mfderiv_within_congr (hs : unique_mdiff_within_at I s x) (hL : ∀ x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) := filter.eventually_eq.mfderiv_within_eq hs (filter.eventually_eq_of_mem (self_mem_nhds_within) hL) hx lemma tangent_map_within_congr (h : ∀ x ∈ s, f x = f₁ x) (p : tangent_bundle I M) (hp : p.1 ∈ s) (hs : unique_mdiff_within_at I s p.1) : tangent_map_within I I' f s p = tangent_map_within I I' f₁ s p := begin simp only [tangent_map_within, h p.fst hp, true_and, eq_self_iff_true, heq_iff_eq, sigma.mk.inj_iff], congr' 1, exact mfderiv_within_congr hs h (h _ hp) end lemma filter.eventually_eq.mfderiv_eq (hL : f₁ =ᶠ[𝓝 x] f) : mfderiv I I' f₁ x = (mfderiv I I' f x : _) := begin have A : f₁ x = f x := (mem_of_mem_nhds hL : _), rw [← mfderiv_within_univ, ← mfderiv_within_univ], rw ← nhds_within_univ at hL, exact hL.mfderiv_within_eq (unique_mdiff_within_at_univ I) A end /-! ### Composition lemmas -/ omit Is I's lemma written_in_ext_chart_comp (h : continuous_within_at f s x) : {y | written_in_ext_chart_at I I'' x (g ∘ f) y = ((written_in_ext_chart_at I' I'' (f x) g) ∘ (written_in_ext_chart_at I I' x f)) y} ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := begin apply @filter.mem_of_superset _ _ ((f ∘ (ext_chart_at I x).symm)⁻¹' (ext_chart_at I' (f x)).source) _ (ext_chart_preimage_mem_nhds_within I x (h.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))), mfld_set_tac, end variable (x) include Is I's I''s theorem has_mfderiv_within_at.comp (hg : has_mfderiv_within_at I' I'' g u (f x) g') (hf : has_mfderiv_within_at I I' f s x f') (hst : s ⊆ f ⁻¹' u) : has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') := begin refine ⟨continuous_within_at.comp hg.1 hf.1 hst, _⟩, have A : has_fderiv_within_at ((written_in_ext_chart_at I' I'' (f x) g) ∘ (written_in_ext_chart_at I I' x f)) (continuous_linear_map.comp g' f' : E →L[𝕜] E'') ((ext_chart_at I x).symm ⁻¹' s ∩ range (I)) ((ext_chart_at I x) x), { have : (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' (f x)).source) ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := (ext_chart_preimage_mem_nhds_within I x (hf.1.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))), unfold has_mfderiv_within_at at *, rw [← has_fderiv_within_at_inter' this, ← ext_chart_preimage_inter_eq] at hf ⊢, have : written_in_ext_chart_at I I' x f ((ext_chart_at I x) x) = (ext_chart_at I' (f x)) (f x), by simp only with mfld_simps, rw ← this at hg, apply has_fderiv_within_at.comp ((ext_chart_at I x) x) hg.2 hf.2 _, assume y hy, simp only with mfld_simps at hy, have : f (((chart_at H x).symm : H → M) (I.symm y)) ∈ u := hst hy.1.1, simp only [hy, this] with mfld_simps }, apply A.congr_of_eventually_eq (written_in_ext_chart_comp hf.1), simp only with mfld_simps end /-- The chain rule. -/ theorem has_mfderiv_at.comp (hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_at I I' f x f') : has_mfderiv_at I I'' (g ∘ f) x (g'.comp f') := begin rw ← has_mfderiv_within_at_univ at *, exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ end theorem has_mfderiv_at.comp_has_mfderiv_within_at (hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_within_at I I' f s x f') : has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') := begin rw ← has_mfderiv_within_at_univ at *, exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ end lemma mdifferentiable_within_at.comp (hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x) (h : s ⊆ f ⁻¹' u) : mdifferentiable_within_at I I'' (g ∘ f) s x := begin rcases hf.2 with ⟨f', hf'⟩, have F : has_mfderiv_within_at I I' f s x f' := ⟨hf.1, hf'⟩, rcases hg.2 with ⟨g', hg'⟩, have G : has_mfderiv_within_at I' I'' g u (f x) g' := ⟨hg.1, hg'⟩, exact (has_mfderiv_within_at.comp x G F h).mdifferentiable_within_at end lemma mdifferentiable_at.comp (hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) : mdifferentiable_at I I'' (g ∘ f) x := (hg.has_mfderiv_at.comp x hf.has_mfderiv_at).mdifferentiable_at lemma mfderiv_within_comp (hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x) (h : s ⊆ f ⁻¹' u) (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I'' (g ∘ f) s x = (mfderiv_within I' I'' g u (f x)).comp (mfderiv_within I I' f s x) := begin apply has_mfderiv_within_at.mfderiv_within _ hxs, exact has_mfderiv_within_at.comp x hg.has_mfderiv_within_at hf.has_mfderiv_within_at h end lemma mfderiv_comp (hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) : mfderiv I I'' (g ∘ f) x = (mfderiv I' I'' g (f x)).comp (mfderiv I I' f x) := begin apply has_mfderiv_at.mfderiv, exact has_mfderiv_at.comp x hg.has_mfderiv_at hf.has_mfderiv_at end lemma mdifferentiable_on.comp (hg : mdifferentiable_on I' I'' g u) (hf : mdifferentiable_on I I' f s) (st : s ⊆ f ⁻¹' u) : mdifferentiable_on I I'' (g ∘ f) s := λx hx, mdifferentiable_within_at.comp x (hg (f x) (st hx)) (hf x hx) st lemma mdifferentiable.comp (hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) : mdifferentiable I I'' (g ∘ f) := λx, mdifferentiable_at.comp x (hg (f x)) (hf x) lemma tangent_map_within_comp_at (p : tangent_bundle I M) (hg : mdifferentiable_within_at I' I'' g u (f p.1)) (hf : mdifferentiable_within_at I I' f s p.1) (h : s ⊆ f ⁻¹' u) (hps : unique_mdiff_within_at I s p.1) : tangent_map_within I I'' (g ∘ f) s p = tangent_map_within I' I'' g u (tangent_map_within I I' f s p) := begin simp only [tangent_map_within] with mfld_simps, rw mfderiv_within_comp p.1 hg hf h hps, refl end lemma tangent_map_comp_at (p : tangent_bundle I M) (hg : mdifferentiable_at I' I'' g (f p.1)) (hf : mdifferentiable_at I I' f p.1) : tangent_map I I'' (g ∘ f) p = tangent_map I' I'' g (tangent_map I I' f p) := begin simp only [tangent_map] with mfld_simps, rw mfderiv_comp p.1 hg hf, refl end lemma tangent_map_comp (hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) : tangent_map I I'' (g ∘ f) = (tangent_map I' I'' g) ∘ (tangent_map I I' f) := by { ext p : 1, exact tangent_map_comp_at _ (hg _) (hf _) } end derivatives_properties section mfderiv_fderiv /-! ### Relations between vector space derivative and manifold derivative The manifold derivative `mfderiv`, when considered on the model vector space with its trivial manifold structure, coincides with the usual Frechet derivative `fderiv`. In this section, we prove this and related statements. -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {f : E → E'} {s : set E} {x : E} lemma unique_mdiff_within_at_iff_unique_diff_within_at : unique_mdiff_within_at (𝓘(𝕜, E)) s x ↔ unique_diff_within_at 𝕜 s x := by simp only [unique_mdiff_within_at] with mfld_simps alias unique_mdiff_within_at_iff_unique_diff_within_at ↔ unique_mdiff_within_at.unique_diff_within_at unique_diff_within_at.unique_mdiff_within_at lemma unique_mdiff_on_iff_unique_diff_on : unique_mdiff_on (𝓘(𝕜, E)) s ↔ unique_diff_on 𝕜 s := by simp [unique_mdiff_on, unique_diff_on, unique_mdiff_within_at_iff_unique_diff_within_at] alias unique_mdiff_on_iff_unique_diff_on ↔ unique_mdiff_on.unique_diff_on unique_diff_on.unique_mdiff_on @[simp, mfld_simps] lemma written_in_ext_chart_model_space : written_in_ext_chart_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) x f = f := rfl lemma has_mfderiv_within_at_iff_has_fderiv_within_at {f'} : has_mfderiv_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x f' ↔ has_fderiv_within_at f f' s x := by simpa only [has_mfderiv_within_at, and_iff_right_iff_imp] with mfld_simps using has_fderiv_within_at.continuous_within_at alias has_mfderiv_within_at_iff_has_fderiv_within_at ↔ has_mfderiv_within_at.has_fderiv_within_at has_fderiv_within_at.has_mfderiv_within_at lemma has_mfderiv_at_iff_has_fderiv_at {f'} : has_mfderiv_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x f' ↔ has_fderiv_at f f' x := by rw [← has_mfderiv_within_at_univ, has_mfderiv_within_at_iff_has_fderiv_within_at, has_fderiv_within_at_univ] alias has_mfderiv_at_iff_has_fderiv_at ↔ has_mfderiv_at.has_fderiv_at has_fderiv_at.has_mfderiv_at /-- For maps between vector spaces, `mdifferentiable_within_at` and `fdifferentiable_within_at` coincide -/ theorem mdifferentiable_within_at_iff_differentiable_within_at : mdifferentiable_within_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s x ↔ differentiable_within_at 𝕜 f s x := begin simp only [mdifferentiable_within_at] with mfld_simps, exact ⟨λH, H.2, λH, ⟨H.continuous_within_at, H⟩⟩ end alias mdifferentiable_within_at_iff_differentiable_within_at ↔ mdifferentiable_within_at.differentiable_within_at differentiable_within_at.mdifferentiable_within_at /-- For maps between vector spaces, `mdifferentiable_at` and `differentiable_at` coincide -/ theorem mdifferentiable_at_iff_differentiable_at : mdifferentiable_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) f x ↔ differentiable_at 𝕜 f x := begin simp only [mdifferentiable_at, differentiable_within_at_univ] with mfld_simps, exact ⟨λH, H.2, λH, ⟨H.continuous_at, H⟩⟩ end alias mdifferentiable_at_iff_differentiable_at ↔ mdifferentiable_at.differentiable_at differentiable_at.mdifferentiable_at /-- For maps between vector spaces, `mdifferentiable_on` and `differentiable_on` coincide -/ theorem mdifferentiable_on_iff_differentiable_on : mdifferentiable_on (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s ↔ differentiable_on 𝕜 f s := by simp only [mdifferentiable_on, differentiable_on, mdifferentiable_within_at_iff_differentiable_within_at] alias mdifferentiable_on_iff_differentiable_on ↔ mdifferentiable_on.differentiable_on differentiable_on.mdifferentiable_on /-- For maps between vector spaces, `mdifferentiable` and `differentiable` coincide -/ theorem mdifferentiable_iff_differentiable : mdifferentiable (𝓘(𝕜, E)) (𝓘(𝕜, E')) f ↔ differentiable 𝕜 f := by simp only [mdifferentiable, differentiable, mdifferentiable_at_iff_differentiable_at] alias mdifferentiable_iff_differentiable ↔ mdifferentiable.differentiable differentiable.mdifferentiable /-- For maps between vector spaces, `mfderiv_within` and `fderiv_within` coincide -/ @[simp] theorem mfderiv_within_eq_fderiv_within : mfderiv_within (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s x = fderiv_within 𝕜 f s x := begin by_cases h : mdifferentiable_within_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s x, { simp only [mfderiv_within, h, dif_pos] with mfld_simps }, { simp only [mfderiv_within, h, dif_neg, not_false_iff], rw [mdifferentiable_within_at_iff_differentiable_within_at] at h, exact (fderiv_within_zero_of_not_differentiable_within_at h).symm } end /-- For maps between vector spaces, `mfderiv` and `fderiv` coincide -/ @[simp] theorem mfderiv_eq_fderiv : mfderiv (𝓘(𝕜, E)) (𝓘(𝕜, E')) f x = fderiv 𝕜 f x := begin rw [← mfderiv_within_univ, ← fderiv_within_univ], exact mfderiv_within_eq_fderiv_within end end mfderiv_fderiv section specific_functions /-! ### Differentiability of specific functions -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H') {M' : Type*} [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M'] namespace continuous_linear_map variables (f : E →L[𝕜] E') {s : set E} {x : E} protected lemma has_mfderiv_within_at : has_mfderiv_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x f := f.has_fderiv_within_at.has_mfderiv_within_at protected lemma has_mfderiv_at : has_mfderiv_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x f := f.has_fderiv_at.has_mfderiv_at protected lemma mdifferentiable_within_at : mdifferentiable_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x := f.differentiable_within_at.mdifferentiable_within_at protected lemma mdifferentiable_on : mdifferentiable_on 𝓘(𝕜, E) 𝓘(𝕜, E') f s := f.differentiable_on.mdifferentiable_on protected lemma mdifferentiable_at : mdifferentiable_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x := f.differentiable_at.mdifferentiable_at protected lemma mdifferentiable : mdifferentiable 𝓘(𝕜, E) 𝓘(𝕜, E') f := f.differentiable.mdifferentiable lemma mfderiv_eq : mfderiv 𝓘(𝕜, E) 𝓘(𝕜, E') f x = f := f.has_mfderiv_at.mfderiv lemma mfderiv_within_eq (hs : unique_mdiff_within_at 𝓘(𝕜, E) s x) : mfderiv_within 𝓘(𝕜, E) 𝓘(𝕜, E') f s x = f := f.has_mfderiv_within_at.mfderiv_within hs end continuous_linear_map namespace continuous_linear_equiv variables (f : E ≃L[𝕜] E') {s : set E} {x : E} protected lemma has_mfderiv_within_at : has_mfderiv_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x (f : E →L[𝕜] E') := f.has_fderiv_within_at.has_mfderiv_within_at protected lemma has_mfderiv_at : has_mfderiv_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x (f : E →L[𝕜] E') := f.has_fderiv_at.has_mfderiv_at protected lemma mdifferentiable_within_at : mdifferentiable_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x := f.differentiable_within_at.mdifferentiable_within_at protected lemma mdifferentiable_on : mdifferentiable_on 𝓘(𝕜, E) 𝓘(𝕜, E') f s := f.differentiable_on.mdifferentiable_on protected lemma mdifferentiable_at : mdifferentiable_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x := f.differentiable_at.mdifferentiable_at protected lemma mdifferentiable : mdifferentiable 𝓘(𝕜, E) 𝓘(𝕜, E') f := f.differentiable.mdifferentiable lemma mfderiv_eq : mfderiv 𝓘(𝕜, E) 𝓘(𝕜, E') f x = (f : E →L[𝕜] E') := f.has_mfderiv_at.mfderiv lemma mfderiv_within_eq (hs : unique_mdiff_within_at 𝓘(𝕜, E) s x) : mfderiv_within 𝓘(𝕜, E) 𝓘(𝕜, E') f s x = (f : E →L[𝕜] E') := f.has_mfderiv_within_at.mfderiv_within hs end continuous_linear_equiv variables {s : set M} {x : M} section id /-! #### Identity -/ lemma has_mfderiv_at_id (x : M) : has_mfderiv_at I I (@_root_.id M) x (continuous_linear_map.id 𝕜 (tangent_space I x)) := begin refine ⟨continuous_id.continuous_at, _⟩, have : ∀ᶠ y in 𝓝[range I] ((ext_chart_at I x) x), ((ext_chart_at I x) ∘ (ext_chart_at I x).symm) y = id y, { apply filter.mem_of_superset (ext_chart_at_target_mem_nhds_within I x), mfld_set_tac }, apply has_fderiv_within_at.congr_of_eventually_eq (has_fderiv_within_at_id _ _) this, simp only with mfld_simps end theorem has_mfderiv_within_at_id (s : set M) (x : M) : has_mfderiv_within_at I I (@_root_.id M) s x (continuous_linear_map.id 𝕜 (tangent_space I x)) := (has_mfderiv_at_id I x).has_mfderiv_within_at lemma mdifferentiable_at_id : mdifferentiable_at I I (@_root_.id M) x := (has_mfderiv_at_id I x).mdifferentiable_at lemma mdifferentiable_within_at_id : mdifferentiable_within_at I I (@_root_.id M) s x := (mdifferentiable_at_id I).mdifferentiable_within_at lemma mdifferentiable_id : mdifferentiable I I (@_root_.id M) := λx, mdifferentiable_at_id I lemma mdifferentiable_on_id : mdifferentiable_on I I (@_root_.id M) s := (mdifferentiable_id I).mdifferentiable_on @[simp, mfld_simps] lemma mfderiv_id : mfderiv I I (@_root_.id M) x = (continuous_linear_map.id 𝕜 (tangent_space I x)) := has_mfderiv_at.mfderiv (has_mfderiv_at_id I x) lemma mfderiv_within_id (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I (@_root_.id M) s x = (continuous_linear_map.id 𝕜 (tangent_space I x)) := begin rw mdifferentiable.mfderiv_within (mdifferentiable_at_id I) hxs, exact mfderiv_id I end @[simp, mfld_simps] lemma tangent_map_id : tangent_map I I (id : M → M) = id := by { ext1 ⟨x, v⟩, simp [tangent_map] } lemma tangent_map_within_id {p : tangent_bundle I M} (hs : unique_mdiff_within_at I s (tangent_bundle.proj I M p)) : tangent_map_within I I (id : M → M) s p = p := begin simp only [tangent_map_within, id.def], rw mfderiv_within_id, { rcases p, refl }, { exact hs } end end id section const /-! #### Constants -/ variables {c : M'} lemma has_mfderiv_at_const (c : M') (x : M) : has_mfderiv_at I I' (λy : M, c) x (0 : tangent_space I x →L[𝕜] tangent_space I' c) := begin refine ⟨continuous_const.continuous_at, _⟩, simp only [written_in_ext_chart_at, (∘), has_fderiv_within_at_const] end theorem has_mfderiv_within_at_const (c : M') (s : set M) (x : M) : has_mfderiv_within_at I I' (λy : M, c) s x (0 : tangent_space I x →L[𝕜] tangent_space I' c) := (has_mfderiv_at_const I I' c x).has_mfderiv_within_at lemma mdifferentiable_at_const : mdifferentiable_at I I' (λy : M, c) x := (has_mfderiv_at_const I I' c x).mdifferentiable_at lemma mdifferentiable_within_at_const : mdifferentiable_within_at I I' (λy : M, c) s x := (mdifferentiable_at_const I I').mdifferentiable_within_at lemma mdifferentiable_const : mdifferentiable I I' (λy : M, c) := λx, mdifferentiable_at_const I I' lemma mdifferentiable_on_const : mdifferentiable_on I I' (λy : M, c) s := (mdifferentiable_const I I').mdifferentiable_on @[simp, mfld_simps] lemma mfderiv_const : mfderiv I I' (λy : M, c) x = (0 : tangent_space I x →L[𝕜] tangent_space I' c) := has_mfderiv_at.mfderiv (has_mfderiv_at_const I I' c x) lemma mfderiv_within_const (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' (λy : M, c) s x = (0 : tangent_space I x →L[𝕜] tangent_space I' c) := (has_mfderiv_within_at_const _ _ _ _ _).mfderiv_within hxs end const namespace model_with_corners /-! #### Model with corners -/ protected lemma has_mfderiv_at {x} : has_mfderiv_at I 𝓘(𝕜, E) I x (continuous_linear_map.id _ _) := ⟨I.continuous_at, (has_fderiv_within_at_id _ _).congr' I.right_inv_on (mem_range_self _)⟩ protected lemma has_mfderiv_within_at {s x} : has_mfderiv_within_at I 𝓘(𝕜, E) I s x (continuous_linear_map.id _ _) := I.has_mfderiv_at.has_mfderiv_within_at protected lemma mdifferentiable_within_at {s x} : mdifferentiable_within_at I 𝓘(𝕜, E) I s x := I.has_mfderiv_within_at.mdifferentiable_within_at protected lemma mdifferentiable_at {x} : mdifferentiable_at I 𝓘(𝕜, E) I x := I.has_mfderiv_at.mdifferentiable_at protected lemma mdifferentiable_on {s} : mdifferentiable_on I 𝓘(𝕜, E) I s := λ x hx, I.mdifferentiable_within_at protected lemma mdifferentiable : mdifferentiable I (𝓘(𝕜, E)) I := λ x, I.mdifferentiable_at lemma has_mfderiv_within_at_symm {x} (hx : x ∈ range I) : has_mfderiv_within_at 𝓘(𝕜, E) I I.symm (range I) x (continuous_linear_map.id _ _) := ⟨I.continuous_within_at_symm, (has_fderiv_within_at_id _ _).congr' (λ y hy, I.right_inv_on hy.1) ⟨hx, mem_range_self _⟩⟩ lemma mdifferentiable_on_symm : mdifferentiable_on (𝓘(𝕜, E)) I I.symm (range I) := λ x hx, (I.has_mfderiv_within_at_symm hx).mdifferentiable_within_at end model_with_corners section charts variable {e : local_homeomorph M H} lemma mdifferentiable_at_atlas (h : e ∈ atlas H M) {x : M} (hx : x ∈ e.source) : mdifferentiable_at I I e x := begin refine ⟨(e.continuous_on x hx).continuous_at (is_open.mem_nhds e.open_source hx), _⟩, have mem : I ((chart_at H x : M → H) x) ∈ I.symm ⁻¹' ((chart_at H x).symm ≫ₕ e).source ∩ range I, by simp only [hx] with mfld_simps, have : (chart_at H x).symm.trans e ∈ times_cont_diff_groupoid ∞ I := has_groupoid.compatible _ (chart_mem_atlas H x) h, have A : times_cont_diff_on 𝕜 ∞ (I ∘ ((chart_at H x).symm.trans e) ∘ I.symm) (I.symm ⁻¹' ((chart_at H x).symm.trans e).source ∩ range I) := this.1, have B := A.differentiable_on le_top (I ((chart_at H x : M → H) x)) mem, simp only with mfld_simps at B, rw [inter_comm, differentiable_within_at_inter] at B, { simpa only with mfld_simps }, { apply is_open.mem_nhds ((local_homeomorph.open_source _).preimage I.continuous_symm) mem.1 } end lemma mdifferentiable_on_atlas (h : e ∈ atlas H M) : mdifferentiable_on I I e e.source := λx hx, (mdifferentiable_at_atlas I h hx).mdifferentiable_within_at lemma mdifferentiable_at_atlas_symm (h : e ∈ atlas H M) {x : H} (hx : x ∈ e.target) : mdifferentiable_at I I e.symm x := begin refine ⟨(e.continuous_on_symm x hx).continuous_at (is_open.mem_nhds e.open_target hx), _⟩, have mem : I x ∈ I.symm ⁻¹' (e.symm ≫ₕ chart_at H (e.symm x)).source ∩ range (I), by simp only [hx] with mfld_simps, have : e.symm.trans (chart_at H (e.symm x)) ∈ times_cont_diff_groupoid ∞ I := has_groupoid.compatible _ h (chart_mem_atlas H _), have A : times_cont_diff_on 𝕜 ∞ (I ∘ (e.symm.trans (chart_at H (e.symm x))) ∘ I.symm) (I.symm ⁻¹' (e.symm.trans (chart_at H (e.symm x))).source ∩ range I) := this.1, have B := A.differentiable_on le_top (I x) mem, simp only with mfld_simps at B, rw [inter_comm, differentiable_within_at_inter] at B, { simpa only with mfld_simps }, { apply (is_open.mem_nhds ((local_homeomorph.open_source _).preimage I.continuous_symm) mem.1) } end lemma mdifferentiable_on_atlas_symm (h : e ∈ atlas H M) : mdifferentiable_on I I e.symm e.target := λx hx, (mdifferentiable_at_atlas_symm I h hx).mdifferentiable_within_at lemma mdifferentiable_of_mem_atlas (h : e ∈ atlas H M) : e.mdifferentiable I I := ⟨mdifferentiable_on_atlas I h, mdifferentiable_on_atlas_symm I h⟩ lemma mdifferentiable_chart (x : M) : (chart_at H x).mdifferentiable I I := mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _) /-- The derivative of the chart at a base point is the chart of the tangent bundle, composed with the identification between the tangent bundle of the model space and the product space. -/ lemma tangent_map_chart {p q : tangent_bundle I M} (h : q.1 ∈ (chart_at H p.1).source) : tangent_map I I (chart_at H p.1) q = (equiv.sigma_equiv_prod _ _).symm ((chart_at (model_prod H E) p : tangent_bundle I M → model_prod H E) q) := begin dsimp [tangent_map], rw mdifferentiable_at.mfderiv, { refl }, { exact mdifferentiable_at_atlas _ (chart_mem_atlas _ _) h } end /-- The derivative of the inverse of the chart at a base point is the inverse of the chart of the tangent bundle, composed with the identification between the tangent bundle of the model space and the product space. -/ lemma tangent_map_chart_symm {p : tangent_bundle I M} {q : tangent_bundle I H} (h : q.1 ∈ (chart_at H p.1).target) : tangent_map I I (chart_at H p.1).symm q = ((chart_at (model_prod H E) p).symm : model_prod H E → tangent_bundle I M) ((equiv.sigma_equiv_prod H E) q) := begin dsimp only [tangent_map], rw mdifferentiable_at.mfderiv (mdifferentiable_at_atlas_symm _ (chart_mem_atlas _ _) h), -- a trivial instance is needed after the rewrite, handle it right now. rotate, { apply_instance }, simp only [chart_at, basic_smooth_bundle_core.chart, subtype.coe_mk, tangent_bundle_core, h, basic_smooth_bundle_core.to_topological_fiber_bundle_core, equiv.sigma_equiv_prod_apply] with mfld_simps, end end charts end specific_functions /-! ### Differentiable local homeomorphisms -/ namespace local_homeomorph.mdifferentiable variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} {M : Type*} [topological_space M] [charted_space H M] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} {M' : Type*} [topological_space M'] [charted_space H' M'] {E'' : Type*} [normed_group E''] [normed_space 𝕜 E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''} {M'' : Type*} [topological_space M''] [charted_space H'' M''] {e : local_homeomorph M M'} (he : e.mdifferentiable I I') {e' : local_homeomorph M' M''} include he lemma symm : e.symm.mdifferentiable I' I := ⟨he.2, he.1⟩ protected lemma mdifferentiable_at {x : M} (hx : x ∈ e.source) : mdifferentiable_at I I' e x := (he.1 x hx).mdifferentiable_at (is_open.mem_nhds e.open_source hx) lemma mdifferentiable_at_symm {x : M'} (hx : x ∈ e.target) : mdifferentiable_at I' I e.symm x := (he.2 x hx).mdifferentiable_at (is_open.mem_nhds e.open_target hx) variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M'] [smooth_manifold_with_corners I'' M''] lemma symm_comp_deriv {x : M} (hx : x ∈ e.source) : (mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) = continuous_linear_map.id 𝕜 (tangent_space I x) := begin have : (mfderiv I I (e.symm ∘ e) x) = (mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) := mfderiv_comp x (he.mdifferentiable_at_symm (e.map_source hx)) (he.mdifferentiable_at hx), rw ← this, have : mfderiv I I (_root_.id : M → M) x = continuous_linear_map.id _ _ := mfderiv_id I, rw ← this, apply filter.eventually_eq.mfderiv_eq, have : e.source ∈ 𝓝 x := is_open.mem_nhds e.open_source hx, exact filter.mem_of_superset this (by mfld_set_tac) end lemma comp_symm_deriv {x : M'} (hx : x ∈ e.target) : (mfderiv I I' e (e.symm x)).comp (mfderiv I' I e.symm x) = continuous_linear_map.id 𝕜 (tangent_space I' x) := he.symm.symm_comp_deriv hx /-- The derivative of a differentiable local homeomorphism, as a continuous linear equivalence between the tangent spaces at `x` and `e x`. -/ protected def mfderiv {x : M} (hx : x ∈ e.source) : tangent_space I x ≃L[𝕜] tangent_space I' (e x) := { inv_fun := (mfderiv I' I e.symm (e x)), continuous_to_fun := (mfderiv I I' e x).cont, continuous_inv_fun := (mfderiv I' I e.symm (e x)).cont, left_inv := λy, begin have : (continuous_linear_map.id _ _ : tangent_space I x →L[𝕜] tangent_space I x) y = y := rfl, conv_rhs { rw [← this, ← he.symm_comp_deriv hx] }, refl end, right_inv := λy, begin have : (continuous_linear_map.id 𝕜 _ : tangent_space I' (e x) →L[𝕜] tangent_space I' (e x)) y = y := rfl, conv_rhs { rw [← this, ← he.comp_symm_deriv (e.map_source hx)] }, rw e.left_inv hx, refl end, .. mfderiv I I' e x } lemma mfderiv_bijective {x : M} (hx : x ∈ e.source) : function.bijective (mfderiv I I' e x) := (he.mfderiv hx).bijective lemma mfderiv_injective {x : M} (hx : x ∈ e.source) : function.injective (mfderiv I I' e x) := (he.mfderiv hx).injective lemma mfderiv_surjective {x : M} (hx : x ∈ e.source) : function.surjective (mfderiv I I' e x) := (he.mfderiv hx).surjective lemma ker_mfderiv_eq_bot {x : M} (hx : x ∈ e.source) : (mfderiv I I' e x).ker = ⊥ := (he.mfderiv hx).to_linear_equiv.ker lemma range_mfderiv_eq_top {x : M} (hx : x ∈ e.source) : (mfderiv I I' e x).range = ⊤ := (he.mfderiv hx).to_linear_equiv.range lemma range_mfderiv_eq_univ {x : M} (hx : x ∈ e.source) : range (mfderiv I I' e x) = univ := (he.mfderiv_surjective hx).range_eq lemma trans (he': e'.mdifferentiable I' I'') : (e.trans e').mdifferentiable I I'' := begin split, { assume x hx, simp only with mfld_simps at hx, exact ((he'.mdifferentiable_at hx.2).comp _ (he.mdifferentiable_at hx.1)).mdifferentiable_within_at }, { assume x hx, simp only with mfld_simps at hx, exact ((he.symm.mdifferentiable_at hx.2).comp _ (he'.symm.mdifferentiable_at hx.1)).mdifferentiable_within_at } end end local_homeomorph.mdifferentiable /-! ### Differentiability of `ext_chart_at` -/ section ext_chart_at variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] {s : set M} {x y : M} lemma has_mfderiv_at_ext_chart_at (h : y ∈ (chart_at H x).source) : has_mfderiv_at I 𝓘(𝕜, E) (ext_chart_at I x) y (mfderiv I I (chart_at H x) y : _) := I.has_mfderiv_at.comp y ((mdifferentiable_chart I x).mdifferentiable_at h).has_mfderiv_at lemma has_mfderiv_within_at_ext_chart_at (h : y ∈ (chart_at H x).source) : has_mfderiv_within_at I 𝓘(𝕜, E) (ext_chart_at I x) s y (mfderiv I I (chart_at H x) y : _) := (has_mfderiv_at_ext_chart_at I h).has_mfderiv_within_at lemma mdifferentiable_at_ext_chart_at (h : y ∈ (chart_at H x).source) : mdifferentiable_at I 𝓘(𝕜, E) (ext_chart_at I x) y := (has_mfderiv_at_ext_chart_at I h).mdifferentiable_at lemma mdifferentiable_on_ext_chart_at : mdifferentiable_on I 𝓘(𝕜, E) (ext_chart_at I x) (chart_at H x).source := λ y hy, (has_mfderiv_within_at_ext_chart_at I hy).mdifferentiable_within_at end ext_chart_at /-! ### Unique derivative sets in manifolds -/ section unique_mdiff variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} {M' : Type*} [topological_space M'] [charted_space H' M'] {s : set M} /-- If a set has the unique differential property, then its image under a local diffeomorphism also has the unique differential property. -/ lemma unique_mdiff_on.unique_mdiff_on_preimage [smooth_manifold_with_corners I' M'] (hs : unique_mdiff_on I s) {e : local_homeomorph M M'} (he : e.mdifferentiable I I') : unique_mdiff_on I' (e.target ∩ e.symm ⁻¹' s) := begin /- Start from a point `x` in the image, and let `z` be its preimage. Then the unique derivative property at `x` is expressed through `ext_chart_at I' x`, and the unique derivative property at `z` is expressed through `ext_chart_at I z`. We will argue that the composition of these two charts with `e` is a local diffeomorphism in vector spaces, and therefore preserves the unique differential property thanks to lemma `has_fderiv_within_at.unique_diff_within_at`, saying that a differentiable function with onto derivative preserves the unique derivative property.-/ assume x hx, let z := e.symm x, have z_source : z ∈ e.source, by simp only [hx.1] with mfld_simps, have zx : e z = x, by simp only [z, hx.1] with mfld_simps, let F := ext_chart_at I z, -- the unique derivative property at `z` is expressed through its preferred chart, -- that we call `F`. have B : unique_diff_within_at 𝕜 (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z), { have : unique_mdiff_within_at I s z := hs _ hx.2, have S : e.source ∩ e ⁻¹' ((ext_chart_at I' x).source) ∈ 𝓝 z, { apply is_open.mem_nhds, apply e.continuous_on.preimage_open_of_open e.open_source (ext_chart_at_open_source I' x), simp only [z_source, zx] with mfld_simps }, have := this.inter S, rw [unique_mdiff_within_at_iff] at this, exact this }, -- denote by `G` the change of coordinate, i.e., the composition of the two extended charts and -- of `e` let G := F.symm ≫ e.to_local_equiv ≫ (ext_chart_at I' x), -- `G` is differentiable have Diff : ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).mdifferentiable I I', { have A := mdifferentiable_of_mem_atlas I (chart_mem_atlas H z), have B := mdifferentiable_of_mem_atlas I' (chart_mem_atlas H' x), exact A.symm.trans (he.trans B) }, have Mmem : (chart_at H z : M → H) z ∈ ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).source, by simp only [z_source, zx] with mfld_simps, have A : differentiable_within_at 𝕜 G (range I) (F z), { refine (Diff.mdifferentiable_at Mmem).2.congr (λp hp, _) _; simp only [G, F] with mfld_simps }, -- let `G'` be its derivative let G' := fderiv_within 𝕜 G (range I) (F z), have D₁ : has_fderiv_within_at G G' (range I) (F z) := A.has_fderiv_within_at, have D₂ : has_fderiv_within_at G G' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z) := D₁.mono (by mfld_set_tac), -- The derivative `G'` is onto, as it is the derivative of a local diffeomorphism, the composition -- of the two charts and of `e`. have C : dense_range (G' : E → E'), { have : G' = mfderiv I I' ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)) ((chart_at H z : M → H) z), by { rw (Diff.mdifferentiable_at Mmem).mfderiv, refl }, rw this, exact (Diff.mfderiv_surjective Mmem).dense_range }, -- key step: thanks to what we have proved about it, `G` preserves the unique derivative property have key : unique_diff_within_at 𝕜 (G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target)) (G (F z)) := D₂.unique_diff_within_at B C, have : G (F z) = (ext_chart_at I' x) x, by { dsimp [G, F], simp only [hx.1] with mfld_simps }, rw this at key, apply key.mono, show G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) ⊆ (ext_chart_at I' x).symm ⁻¹' e.target ∩ (ext_chart_at I' x).symm ⁻¹' (e.symm ⁻¹' s) ∩ range (I'), rw image_subset_iff, mfld_set_tac end /-- If a set in a manifold has the unique derivative property, then its pullback by any extended chart, in the vector space, also has the unique derivative property. -/ lemma unique_mdiff_on.unique_diff_on_target_inter (hs : unique_mdiff_on I s) (x : M) : unique_diff_on 𝕜 ((ext_chart_at I x).target ∩ ((ext_chart_at I x).symm ⁻¹' s)) := begin -- this is just a reformulation of `unique_mdiff_on.unique_mdiff_on_preimage`, using as `e` -- the local chart at `x`. assume z hz, simp only with mfld_simps at hz, have : (chart_at H x).mdifferentiable I I := mdifferentiable_chart _ _, have T := (hs.unique_mdiff_on_preimage this) (I.symm z), simp only [hz.left.left, hz.left.right, hz.right, unique_mdiff_within_at] with mfld_simps at ⊢ T, convert T using 1, rw @preimage_comp _ _ _ _ (chart_at H x).symm, mfld_set_tac end /-- When considering functions between manifolds, this statement shows up often. It entails the unique differential of the pullback in extended charts of the set where the function can be read in the charts. -/ lemma unique_mdiff_on.unique_diff_on_inter_preimage (hs : unique_mdiff_on I s) (x : M) (y : M') {f : M → M'} (hf : continuous_on f s) : unique_diff_on 𝕜 ((ext_chart_at I x).target ∩ ((ext_chart_at I x).symm ⁻¹' (s ∩ f⁻¹' (ext_chart_at I' y).source))) := begin have : unique_mdiff_on I (s ∩ f ⁻¹' (ext_chart_at I' y).source), { assume z hz, apply (hs z hz.1).inter', apply (hf z hz.1).preimage_mem_nhds_within, exact is_open.mem_nhds (ext_chart_at_open_source I' y) hz.2 }, exact this.unique_diff_on_target_inter _ end variables {F : Type*} [normed_group F] [normed_space 𝕜 F] (Z : basic_smooth_bundle_core I M F) /-- In a smooth fiber bundle constructed from core, the preimage under the projection of a set with unique differential in the basis also has unique differential. -/ lemma unique_mdiff_on.smooth_bundle_preimage (hs : unique_mdiff_on I s) : unique_mdiff_on (I.prod (𝓘(𝕜, F))) (Z.to_topological_fiber_bundle_core.proj ⁻¹' s) := begin /- Using a chart (and the fact that unique differentiability is invariant under charts), we reduce the situation to the model space, where we can use the fact that products respect unique differentiability. -/ assume p hp, replace hp : p.fst ∈ s, by simpa only with mfld_simps using hp, let e₀ := chart_at H p.1, let e := chart_at (model_prod H F) p, -- It suffices to prove unique differentiability in a chart suffices h : unique_mdiff_on (I.prod (𝓘(𝕜, F))) (e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s)), { have A : unique_mdiff_on (I.prod (𝓘(𝕜, F))) (e.symm.target ∩ e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s))), { apply h.unique_mdiff_on_preimage, exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)).symm, apply_instance }, have : p ∈ e.symm.target ∩ e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s)), by simp only [e, hp] with mfld_simps, apply (A _ this).mono, assume q hq, simp only [e, local_homeomorph.left_inv _ hq.1] with mfld_simps at hq, simp only [hq] with mfld_simps }, -- rewrite the relevant set in the chart as a direct product have : (λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' e.target ∩ (λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' (e.symm ⁻¹' (sigma.fst ⁻¹' s)) ∩ ((range I).prod univ) = set.prod (I.symm ⁻¹' (e₀.target ∩ e₀.symm⁻¹' s) ∩ range I) univ, by mfld_set_tac, assume q hq, replace hq : q.1 ∈ (chart_at H p.1).target ∧ ((chart_at H p.1).symm : H → M) q.1 ∈ s, by simpa only with mfld_simps using hq, simp only [unique_mdiff_within_at, model_with_corners.prod, preimage_inter, this] with mfld_simps, -- apply unique differentiability of products to conclude apply unique_diff_on.prod _ unique_diff_on_univ, { simp only [hq] with mfld_simps }, { assume x hx, have A : unique_mdiff_on I (e₀.target ∩ e₀.symm⁻¹' s), { apply hs.unique_mdiff_on_preimage, exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)), apply_instance }, simp only [unique_mdiff_on, unique_mdiff_within_at, preimage_inter] with mfld_simps at A, have B := A (I.symm x) hx.1.1 hx.1.2, rwa [← preimage_inter, model_with_corners.right_inv _ hx.2] at B } end lemma unique_mdiff_on.tangent_bundle_proj_preimage (hs : unique_mdiff_on I s): unique_mdiff_on I.tangent ((tangent_bundle.proj I M) ⁻¹' s) := hs.smooth_bundle_preimage _ end unique_mdiff
14ade23f79bbaa59fdaba761f3159c37bccf201d
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/analysis/normed_space/inner_product.lean
8e1f8bb840e8259efbc6b2d52a9d4a7eeed97759
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
122,807
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Sébastien Gouëzel, Frédéric Dupuis, Heather Macbeth -/ import linear_algebra.bilinear_form import linear_algebra.sesquilinear_form import data.complex.is_R_or_C import analysis.special_functions.sqrt import analysis.complex.basic /-! # Inner Product Space This file defines inner product spaces and proves its basic properties. An inner product space is a vector space endowed with an inner product. It generalizes the notion of dot product in `ℝ^n` and provides the means of defining the length of a vector and the angle between two vectors. In particular vectors `x` and `y` are orthogonal if their inner product equals zero. We define both the real and complex cases at the same time using the `is_R_or_C` typeclass. This file proves general results on inner product spaces. For the specific construction of an inner product structure on `n → 𝕜` for `𝕜 = ℝ` or `ℂ`, see `euclidean_space` in `analysis.pi_Lp`. ## Main results - We define the class `inner_product_space 𝕜 E` extending `normed_space 𝕜 E` with a number of basic properties, most notably the Cauchy-Schwarz inequality. Here `𝕜` is understood to be either `ℝ` or `ℂ`, through the `is_R_or_C` typeclass. - We show that if `f i` is an inner product space for each `i`, then so is `Π i, f i` - Existence of orthogonal projection onto nonempty complete subspace: Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace. Then there exists a unique `v` in `K` that minimizes the distance `∥u - v∥` to `u`. The point `v` is usually called the orthogonal projection of `u` onto `K`. - We define `orthonormal`, a predicate on a function `v : ι → E`. We prove the existence of a maximal orthonormal set, `exists_maximal_orthonormal`, and also prove that a maximal orthonormal set is a basis (`maximal_orthonormal_iff_basis_of_finite_dimensional`), if `E` is finite- dimensional, or in general (`maximal_orthonormal_iff_dense_span`) a set whose span is dense (i.e., a Hilbert basis, although we do not make that definition). ## Notation We globally denote the real and complex inner products by `⟪·, ·⟫_ℝ` and `⟪·, ·⟫_ℂ` respectively. We also provide two notation namespaces: `real_inner_product_space`, `complex_inner_product_space`, which respectively introduce the plain notation `⟪·, ·⟫` for the the real and complex inner product. The orthogonal complement of a submodule `K` is denoted by `Kᗮ`. ## Implementation notes We choose the convention that inner products are conjugate linear in the first argument and linear in the second. ## Tags inner product space, norm ## References * [Clément & Martin, *The Lax-Milgram Theorem. A detailed proof to be formalized in Coq*] * [Clément & Martin, *A Coq formal proof of the Lax–Milgram theorem*] The Coq code is available at the following address: <http://www.lri.fr/~sboldo/elfic/index.html> -/ noncomputable theory open is_R_or_C real filter open_locale big_operators classical topological_space variables {𝕜 E F : Type*} [is_R_or_C 𝕜] /-- Syntactic typeclass for types endowed with an inner product -/ class has_inner (𝕜 E : Type*) := (inner : E → E → 𝕜) export has_inner (inner) notation `⟪`x`, `y`⟫_ℝ` := @inner ℝ _ _ x y notation `⟪`x`, `y`⟫_ℂ` := @inner ℂ _ _ x y section notations localized "notation `⟪`x`, `y`⟫` := @inner ℝ _ _ x y" in real_inner_product_space localized "notation `⟪`x`, `y`⟫` := @inner ℂ _ _ x y" in complex_inner_product_space end notations /-- An inner product space is a vector space with an additional operation called inner product. The norm could be derived from the inner product, instead we require the existence of a norm and the fact that `∥x∥^2 = re ⟪x, x⟫` to be able to put instances on `𝕂` or product spaces. To construct a norm from an inner product, see `inner_product_space.of_core`. -/ class inner_product_space (𝕜 : Type*) (E : Type*) [is_R_or_C 𝕜] extends normed_group E, normed_space 𝕜 E, has_inner 𝕜 E := (norm_sq_eq_inner : ∀ (x : E), ∥x∥^2 = re (inner x x)) (conj_sym : ∀ x y, conj (inner y x) = inner x y) (add_left : ∀ x y z, inner (x + y) z = inner x z + inner y z) (smul_left : ∀ x y r, inner (r • x) y = (conj r) * inner x y) attribute [nolint dangerous_instance] inner_product_space.to_normed_group -- note [is_R_or_C instance] /-! ### Constructing a normed space structure from an inner product In the definition of an inner product space, we require the existence of a norm, which is equal (but maybe not defeq) to the square root of the scalar product. This makes it possible to put an inner product space structure on spaces with a preexisting norm (for instance `ℝ`), with good properties. However, sometimes, one would like to define the norm starting only from a well-behaved scalar product. This is what we implement in this paragraph, starting from a structure `inner_product_space.core` stating that we have a nice scalar product. Our goal here is not to develop a whole theory with all the supporting API, as this will be done below for `inner_product_space`. Instead, we implement the bare minimum to go as directly as possible to the construction of the norm and the proof of the triangular inequality. Warning: Do not use this `core` structure if the space you are interested in already has a norm instance defined on it, otherwise this will create a second non-defeq norm instance! -/ /-- A structure requiring that a scalar product is positive definite and symmetric, from which one can construct an `inner_product_space` instance in `inner_product_space.of_core`. -/ @[nolint has_inhabited_instance] structure inner_product_space.core (𝕜 : Type*) (F : Type*) [is_R_or_C 𝕜] [add_comm_group F] [module 𝕜 F] := (inner : F → F → 𝕜) (conj_sym : ∀ x y, conj (inner y x) = inner x y) (nonneg_re : ∀ x, 0 ≤ re (inner x x)) (definite : ∀ x, inner x x = 0 → x = 0) (add_left : ∀ x y z, inner (x + y) z = inner x z + inner y z) (smul_left : ∀ x y r, inner (r • x) y = (conj r) * inner x y) /- We set `inner_product_space.core` to be a class as we will use it as such in the construction of the normed space structure that it produces. However, all the instances we will use will be local to this proof. -/ attribute [class] inner_product_space.core namespace inner_product_space.of_core variables [add_comm_group F] [module 𝕜 F] [c : inner_product_space.core 𝕜 F] include c local notation `⟪`x`, `y`⟫` := @inner 𝕜 F _ x y local notation `norm_sqK` := @is_R_or_C.norm_sq 𝕜 _ local notation `reK` := @is_R_or_C.re 𝕜 _ local notation `absK` := @is_R_or_C.abs 𝕜 _ local notation `ext_iff` := @is_R_or_C.ext_iff 𝕜 _ local postfix `†`:90 := @is_R_or_C.conj 𝕜 _ /-- Inner product defined by the `inner_product_space.core` structure. -/ def to_has_inner : has_inner 𝕜 F := { inner := c.inner } local attribute [instance] to_has_inner /-- The norm squared function for `inner_product_space.core` structure. -/ def norm_sq (x : F) := reK ⟪x, x⟫ local notation `norm_sqF` := @norm_sq 𝕜 F _ _ _ _ lemma inner_conj_sym (x y : F) : ⟪y, x⟫† = ⟪x, y⟫ := c.conj_sym x y lemma inner_self_nonneg {x : F} : 0 ≤ re ⟪x, x⟫ := c.nonneg_re _ lemma inner_self_nonneg_im {x : F} : im ⟪x, x⟫ = 0 := by rw [← @of_real_inj 𝕜, im_eq_conj_sub]; simp [inner_conj_sym] lemma inner_self_im_zero {x : F} : im ⟪x, x⟫ = 0 := inner_self_nonneg_im lemma inner_add_left {x y z : F} : ⟪x + y, z⟫ = ⟪x, z⟫ + ⟪y, z⟫ := c.add_left _ _ _ lemma inner_add_right {x y z : F} : ⟪x, y + z⟫ = ⟪x, y⟫ + ⟪x, z⟫ := by rw [←inner_conj_sym, inner_add_left, ring_hom.map_add]; simp only [inner_conj_sym] lemma inner_norm_sq_eq_inner_self (x : F) : (norm_sqF x : 𝕜) = ⟪x, x⟫ := begin rw ext_iff, exact ⟨by simp only [of_real_re]; refl, by simp only [inner_self_nonneg_im, of_real_im]⟩ end lemma inner_re_symm {x y : F} : re ⟪x, y⟫ = re ⟪y, x⟫ := by rw [←inner_conj_sym, conj_re] lemma inner_im_symm {x y : F} : im ⟪x, y⟫ = -im ⟪y, x⟫ := by rw [←inner_conj_sym, conj_im] lemma inner_smul_left {x y : F} {r : 𝕜} : ⟪r • x, y⟫ = r† * ⟪x, y⟫ := c.smul_left _ _ _ lemma inner_smul_right {x y : F} {r : 𝕜} : ⟪x, r • y⟫ = r * ⟪x, y⟫ := by rw [←inner_conj_sym, inner_smul_left]; simp only [conj_conj, inner_conj_sym, ring_hom.map_mul] lemma inner_zero_left {x : F} : ⟪0, x⟫ = 0 := by rw [←zero_smul 𝕜 (0 : F), inner_smul_left]; simp only [zero_mul, ring_hom.map_zero] lemma inner_zero_right {x : F} : ⟪x, 0⟫ = 0 := by rw [←inner_conj_sym, inner_zero_left]; simp only [ring_hom.map_zero] lemma inner_self_eq_zero {x : F} : ⟪x, x⟫ = 0 ↔ x = 0 := iff.intro (c.definite _) (by { rintro rfl, exact inner_zero_left }) lemma inner_self_re_to_K {x : F} : (re ⟪x, x⟫ : 𝕜) = ⟪x, x⟫ := by norm_num [ext_iff, inner_self_nonneg_im] lemma inner_abs_conj_sym {x y : F} : abs ⟪x, y⟫ = abs ⟪y, x⟫ := by rw [←inner_conj_sym, abs_conj] lemma inner_neg_left {x y : F} : ⟪-x, y⟫ = -⟪x, y⟫ := by { rw [← neg_one_smul 𝕜 x, inner_smul_left], simp } lemma inner_neg_right {x y : F} : ⟪x, -y⟫ = -⟪x, y⟫ := by rw [←inner_conj_sym, inner_neg_left]; simp only [ring_hom.map_neg, inner_conj_sym] lemma inner_sub_left {x y z : F} : ⟪x - y, z⟫ = ⟪x, z⟫ - ⟪y, z⟫ := by { simp [sub_eq_add_neg, inner_add_left, inner_neg_left] } lemma inner_sub_right {x y z : F} : ⟪x, y - z⟫ = ⟪x, y⟫ - ⟪x, z⟫ := by { simp [sub_eq_add_neg, inner_add_right, inner_neg_right] } lemma inner_mul_conj_re_abs {x y : F} : re (⟪x, y⟫ * ⟪y, x⟫) = abs (⟪x, y⟫ * ⟪y, x⟫) := by { rw[←inner_conj_sym, mul_comm], exact re_eq_abs_of_mul_conj (inner y x), } /-- Expand `inner (x + y) (x + y)` -/ lemma inner_add_add_self {x y : F} : ⟪x + y, x + y⟫ = ⟪x, x⟫ + ⟪x, y⟫ + ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_add_left, inner_add_right]; ring /- Expand `inner (x - y) (x - y)` -/ lemma inner_sub_sub_self {x y : F} : ⟪x - y, x - y⟫ = ⟪x, x⟫ - ⟪x, y⟫ - ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_sub_left, inner_sub_right]; ring /-- Cauchy–Schwarz inequality. This proof follows "Proof 2" on Wikipedia. We need this for the `core` structure to prove the triangle inequality below when showing the core is a normed group. -/ lemma inner_mul_inner_self_le (x y : F) : abs ⟪x, y⟫ * abs ⟪y, x⟫ ≤ re ⟪x, x⟫ * re ⟪y, y⟫ := begin by_cases hy : y = 0, { rw [hy], simp only [is_R_or_C.abs_zero, inner_zero_left, mul_zero, add_monoid_hom.map_zero] }, { change y ≠ 0 at hy, have hy' : ⟪y, y⟫ ≠ 0 := λ h, by rw [inner_self_eq_zero] at h; exact hy h, set T := ⟪y, x⟫ / ⟪y, y⟫ with hT, have h₁ : re ⟪y, x⟫ = re ⟪x, y⟫ := inner_re_symm, have h₂ : im ⟪y, x⟫ = -im ⟪x, y⟫ := inner_im_symm, have h₃ : ⟪y, x⟫ * ⟪x, y⟫ * ⟪y, y⟫ / (⟪y, y⟫ * ⟪y, y⟫) = ⟪y, x⟫ * ⟪x, y⟫ / ⟪y, y⟫, { rw [mul_div_assoc], have : ⟪y, y⟫ / (⟪y, y⟫ * ⟪y, y⟫) = 1 / ⟪y, y⟫ := by rw [div_mul_eq_div_mul_one_div, div_self hy', one_mul], rw [this, div_eq_mul_inv, one_mul, ←div_eq_mul_inv] }, have h₄ : ⟪y, y⟫ = re ⟪y, y⟫ := by simp only [inner_self_re_to_K], have h₅ : re ⟪y, y⟫ > 0, { refine lt_of_le_of_ne inner_self_nonneg _, intro H, apply hy', rw ext_iff, exact ⟨by simp only [H, zero_re'], by simp only [inner_self_nonneg_im, add_monoid_hom.map_zero]⟩ }, have h₆ : re ⟪y, y⟫ ≠ 0 := ne_of_gt h₅, have hmain := calc 0 ≤ re ⟪x - T • y, x - T • y⟫ : inner_self_nonneg ... = re ⟪x, x⟫ - re ⟪T • y, x⟫ - re ⟪x, T • y⟫ + re ⟪T • y, T • y⟫ : by simp only [inner_sub_sub_self, inner_smul_left, inner_smul_right, h₁, h₂, neg_mul_eq_neg_mul_symm, add_monoid_hom.map_add, mul_re, conj_im, add_monoid_hom.map_sub, mul_neg_eq_neg_mul_symm, conj_re, neg_neg] ... = re ⟪x, x⟫ - re (T† * ⟪y, x⟫) - re (T * ⟪x, y⟫) + re (T * T† * ⟪y, y⟫) : by simp only [inner_smul_left, inner_smul_right, mul_assoc] ... = re ⟪x, x⟫ - re (⟪x, y⟫ / ⟪y, y⟫ * ⟪y, x⟫) : by field_simp [-mul_re, inner_conj_sym, hT, conj_div, h₁, h₃] ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫ / ⟪y, y⟫) : by rw [div_mul_eq_mul_div_comm, ←mul_div_assoc] ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫ / re ⟪y, y⟫) : by conv_lhs { rw [h₄] } ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫) / re ⟪y, y⟫ : by rw [div_re_of_real] ... = re ⟪x, x⟫ - abs (⟪x, y⟫ * ⟪y, x⟫) / re ⟪y, y⟫ : by rw [inner_mul_conj_re_abs] ... = re ⟪x, x⟫ - abs ⟪x, y⟫ * abs ⟪y, x⟫ / re ⟪y, y⟫ : by rw is_R_or_C.abs_mul, have hmain' : abs ⟪x, y⟫ * abs ⟪y, x⟫ / re ⟪y, y⟫ ≤ re ⟪x, x⟫ := by linarith, have := (mul_le_mul_right h₅).mpr hmain', rwa [div_mul_cancel (abs ⟪x, y⟫ * abs ⟪y, x⟫) h₆] at this } end /-- Norm constructed from a `inner_product_space.core` structure, defined to be the square root of the scalar product. -/ def to_has_norm : has_norm F := { norm := λ x, sqrt (re ⟪x, x⟫) } local attribute [instance] to_has_norm lemma norm_eq_sqrt_inner (x : F) : ∥x∥ = sqrt (re ⟪x, x⟫) := rfl lemma inner_self_eq_norm_sq (x : F) : re ⟪x, x⟫ = ∥x∥ * ∥x∥ := by rw[norm_eq_sqrt_inner, ←sqrt_mul inner_self_nonneg (re ⟪x, x⟫), sqrt_mul_self inner_self_nonneg] lemma sqrt_norm_sq_eq_norm {x : F} : sqrt (norm_sqF x) = ∥x∥ := rfl /-- Cauchy–Schwarz inequality with norm -/ lemma abs_inner_le_norm (x y : F) : abs ⟪x, y⟫ ≤ ∥x∥ * ∥y∥ := nonneg_le_nonneg_of_sq_le_sq (mul_nonneg (sqrt_nonneg _) (sqrt_nonneg _)) begin have H : ∥x∥ * ∥y∥ * (∥x∥ * ∥y∥) = re ⟪y, y⟫ * re ⟪x, x⟫, { simp only [inner_self_eq_norm_sq], ring, }, rw H, conv begin to_lhs, congr, rw[inner_abs_conj_sym], end, exact inner_mul_inner_self_le y x, end /-- Normed group structure constructed from an `inner_product_space.core` structure -/ def to_normed_group : normed_group F := normed_group.of_core F { norm_eq_zero_iff := assume x, begin split, { intro H, change sqrt (re ⟪x, x⟫) = 0 at H, rw [sqrt_eq_zero inner_self_nonneg] at H, apply (inner_self_eq_zero : ⟪x, x⟫ = 0 ↔ x = 0).mp, rw ext_iff, exact ⟨by simp [H], by simp [inner_self_im_zero]⟩ }, { rintro rfl, change sqrt (re ⟪0, 0⟫) = 0, simp only [sqrt_zero, inner_zero_right, add_monoid_hom.map_zero] } end, triangle := assume x y, begin have h₁ : abs ⟪x, y⟫ ≤ ∥x∥ * ∥y∥ := abs_inner_le_norm _ _, have h₂ : re ⟪x, y⟫ ≤ abs ⟪x, y⟫ := re_le_abs _, have h₃ : re ⟪x, y⟫ ≤ ∥x∥ * ∥y∥ := by linarith, have h₄ : re ⟪y, x⟫ ≤ ∥x∥ * ∥y∥ := by rwa [←inner_conj_sym, conj_re], have : ∥x + y∥ * ∥x + y∥ ≤ (∥x∥ + ∥y∥) * (∥x∥ + ∥y∥), { simp [←inner_self_eq_norm_sq, inner_add_add_self, add_mul, mul_add, mul_comm], linarith }, exact nonneg_le_nonneg_of_sq_le_sq (add_nonneg (sqrt_nonneg _) (sqrt_nonneg _)) this end, norm_neg := λ x, by simp only [norm, inner_neg_left, neg_neg, inner_neg_right] } local attribute [instance] to_normed_group /-- Normed space structure constructed from a `inner_product_space.core` structure -/ def to_normed_space : normed_space 𝕜 F := { norm_smul_le := assume r x, begin rw [norm_eq_sqrt_inner, inner_smul_left, inner_smul_right, ←mul_assoc], rw [conj_mul_eq_norm_sq_left, of_real_mul_re, sqrt_mul, ←inner_norm_sq_eq_inner_self, of_real_re], { simp [sqrt_norm_sq_eq_norm, is_R_or_C.sqrt_norm_sq_eq_norm] }, { exact norm_sq_nonneg r } end } end inner_product_space.of_core /-- Given a `inner_product_space.core` structure on a space, one can use it to turn the space into an inner product space, constructing the norm out of the inner product -/ def inner_product_space.of_core [add_comm_group F] [module 𝕜 F] (c : inner_product_space.core 𝕜 F) : inner_product_space 𝕜 F := begin letI : normed_group F := @inner_product_space.of_core.to_normed_group 𝕜 F _ _ _ c, letI : normed_space 𝕜 F := @inner_product_space.of_core.to_normed_space 𝕜 F _ _ _ c, exact { norm_sq_eq_inner := λ x, begin have h₁ : ∥x∥^2 = (sqrt (re (c.inner x x))) ^ 2 := rfl, have h₂ : 0 ≤ re (c.inner x x) := inner_product_space.of_core.inner_self_nonneg, simp [h₁, sq_sqrt, h₂], end, ..c } end /-! ### Properties of inner product spaces -/ variables [inner_product_space 𝕜 E] [inner_product_space ℝ F] local notation `⟪`x`, `y`⟫` := @inner 𝕜 _ _ x y local notation `IK` := @is_R_or_C.I 𝕜 _ local notation `absR` := _root_.abs local notation `absK` := @is_R_or_C.abs 𝕜 _ local postfix `†`:90 := @is_R_or_C.conj 𝕜 _ local postfix `⋆`:90 := complex.conj export inner_product_space (norm_sq_eq_inner) section basic_properties @[simp] lemma inner_conj_sym (x y : E) : ⟪y, x⟫† = ⟪x, y⟫ := inner_product_space.conj_sym _ _ lemma real_inner_comm (x y : F) : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := inner_conj_sym x y lemma inner_eq_zero_sym {x y : E} : ⟪x, y⟫ = 0 ↔ ⟪y, x⟫ = 0 := ⟨λ h, by simp [←inner_conj_sym, h], λ h, by simp [←inner_conj_sym, h]⟩ @[simp] lemma inner_self_nonneg_im {x : E} : im ⟪x, x⟫ = 0 := by rw [← @of_real_inj 𝕜, im_eq_conj_sub]; simp lemma inner_self_im_zero {x : E} : im ⟪x, x⟫ = 0 := inner_self_nonneg_im lemma inner_add_left {x y z : E} : ⟪x + y, z⟫ = ⟪x, z⟫ + ⟪y, z⟫ := inner_product_space.add_left _ _ _ lemma inner_add_right {x y z : E} : ⟪x, y + z⟫ = ⟪x, y⟫ + ⟪x, z⟫ := by { rw [←inner_conj_sym, inner_add_left, ring_hom.map_add], simp only [inner_conj_sym] } lemma inner_re_symm {x y : E} : re ⟪x, y⟫ = re ⟪y, x⟫ := by rw [←inner_conj_sym, conj_re] lemma inner_im_symm {x y : E} : im ⟪x, y⟫ = -im ⟪y, x⟫ := by rw [←inner_conj_sym, conj_im] lemma inner_smul_left {x y : E} {r : 𝕜} : ⟪r • x, y⟫ = r† * ⟪x, y⟫ := inner_product_space.smul_left _ _ _ lemma real_inner_smul_left {x y : F} {r : ℝ} : ⟪r • x, y⟫_ℝ = r * ⟪x, y⟫_ℝ := inner_smul_left lemma inner_smul_real_left {x y : E} {r : ℝ} : ⟪(r : 𝕜) • x, y⟫ = r • ⟪x, y⟫ := by { rw [inner_smul_left, conj_of_real, algebra.smul_def], refl } lemma inner_smul_right {x y : E} {r : 𝕜} : ⟪x, r • y⟫ = r * ⟪x, y⟫ := by rw [←inner_conj_sym, inner_smul_left, ring_hom.map_mul, conj_conj, inner_conj_sym] lemma real_inner_smul_right {x y : F} {r : ℝ} : ⟪x, r • y⟫_ℝ = r * ⟪x, y⟫_ℝ := inner_smul_right lemma inner_smul_real_right {x y : E} {r : ℝ} : ⟪x, (r : 𝕜) • y⟫ = r • ⟪x, y⟫ := by { rw [inner_smul_right, algebra.smul_def], refl } /-- The inner product as a sesquilinear form. -/ @[simps] def sesq_form_of_inner : sesq_form 𝕜 E (conj_to_ring_equiv 𝕜) := { sesq := λ x y, ⟪y, x⟫, -- Note that sesquilinear forms are linear in the first argument sesq_add_left := λ x y z, inner_add_right, sesq_add_right := λ x y z, inner_add_left, sesq_smul_left := λ r x y, inner_smul_right, sesq_smul_right := λ r x y, inner_smul_left } /-- The real inner product as a bilinear form. -/ @[simps] def bilin_form_of_real_inner : bilin_form ℝ F := { bilin := inner, bilin_add_left := λ x y z, inner_add_left, bilin_smul_left := λ a x y, inner_smul_left, bilin_add_right := λ x y z, inner_add_right, bilin_smul_right := λ a x y, inner_smul_right } /-- An inner product with a sum on the left. -/ lemma sum_inner {ι : Type*} (s : finset ι) (f : ι → E) (x : E) : ⟪∑ i in s, f i, x⟫ = ∑ i in s, ⟪f i, x⟫ := sesq_form.sum_right (sesq_form_of_inner) _ _ _ /-- An inner product with a sum on the right. -/ lemma inner_sum {ι : Type*} (s : finset ι) (f : ι → E) (x : E) : ⟪x, ∑ i in s, f i⟫ = ∑ i in s, ⟪x, f i⟫ := sesq_form.sum_left (sesq_form_of_inner) _ _ _ /-- An inner product with a sum on the left, `finsupp` version. -/ lemma finsupp.sum_inner {ι : Type*} (l : ι →₀ 𝕜) (v : ι → E) (x : E) : ⟪l.sum (λ (i : ι) (a : 𝕜), a • v i), x⟫ = l.sum (λ (i : ι) (a : 𝕜), (is_R_or_C.conj a) • ⟪v i, x⟫) := by { convert sum_inner l.support (λ a, l a • v a) x, simp [inner_smul_left, finsupp.sum] } /-- An inner product with a sum on the right, `finsupp` version. -/ lemma finsupp.inner_sum {ι : Type*} (l : ι →₀ 𝕜) (v : ι → E) (x : E) : ⟪x, l.sum (λ (i : ι) (a : 𝕜), a • v i)⟫ = l.sum (λ (i : ι) (a : 𝕜), a • ⟪x, v i⟫) := by { convert inner_sum l.support (λ a, l a • v a) x, simp [inner_smul_right, finsupp.sum] } @[simp] lemma inner_zero_left {x : E} : ⟪0, x⟫ = 0 := by rw [← zero_smul 𝕜 (0:E), inner_smul_left, ring_hom.map_zero, zero_mul] lemma inner_re_zero_left {x : E} : re ⟪0, x⟫ = 0 := by simp only [inner_zero_left, add_monoid_hom.map_zero] @[simp] lemma inner_zero_right {x : E} : ⟪x, 0⟫ = 0 := by rw [←inner_conj_sym, inner_zero_left, ring_hom.map_zero] lemma inner_re_zero_right {x : E} : re ⟪x, 0⟫ = 0 := by simp only [inner_zero_right, add_monoid_hom.map_zero] lemma inner_self_nonneg {x : E} : 0 ≤ re ⟪x, x⟫ := by rw [←norm_sq_eq_inner]; exact pow_nonneg (norm_nonneg x) 2 lemma real_inner_self_nonneg {x : F} : 0 ≤ ⟪x, x⟫_ℝ := @inner_self_nonneg ℝ F _ _ x @[simp] lemma inner_self_eq_zero {x : E} : ⟪x, x⟫ = 0 ↔ x = 0 := begin split, { intro h, have h₁ : re ⟪x, x⟫ = 0 := by rw is_R_or_C.ext_iff at h; simp [h.1], rw [←norm_sq_eq_inner x] at h₁, rw [←norm_eq_zero], exact pow_eq_zero h₁ }, { rintro rfl, exact inner_zero_left } end @[simp] lemma inner_self_nonpos {x : E} : re ⟪x, x⟫ ≤ 0 ↔ x = 0 := begin split, { intro h, rw ←inner_self_eq_zero, have H₁ : re ⟪x, x⟫ ≥ 0, exact inner_self_nonneg, have H₂ : re ⟪x, x⟫ = 0, exact le_antisymm h H₁, rw is_R_or_C.ext_iff, exact ⟨by simp [H₂], by simp [inner_self_nonneg_im]⟩ }, { rintro rfl, simp only [inner_zero_left, add_monoid_hom.map_zero] } end lemma real_inner_self_nonpos {x : F} : ⟪x, x⟫_ℝ ≤ 0 ↔ x = 0 := by { have h := @inner_self_nonpos ℝ F _ _ x, simpa using h } @[simp] lemma inner_self_re_to_K {x : E} : (re ⟪x, x⟫ : 𝕜) = ⟪x, x⟫ := by rw is_R_or_C.ext_iff; exact ⟨by simp, by simp [inner_self_nonneg_im]⟩ lemma inner_self_eq_norm_sq_to_K (x : E) : ⟪x, x⟫ = (∥x∥ ^ 2 : 𝕜) := begin suffices : (is_R_or_C.re ⟪x, x⟫ : 𝕜) = ∥x∥ ^ 2, { simpa [inner_self_re_to_K] using this }, exact_mod_cast (norm_sq_eq_inner x).symm end lemma inner_self_re_abs {x : E} : re ⟪x, x⟫ = abs ⟪x, x⟫ := begin conv_rhs { rw [←inner_self_re_to_K] }, symmetry, exact is_R_or_C.abs_of_nonneg inner_self_nonneg, end lemma inner_self_abs_to_K {x : E} : (absK ⟪x, x⟫ : 𝕜) = ⟪x, x⟫ := by { rw[←inner_self_re_abs], exact inner_self_re_to_K } lemma real_inner_self_abs {x : F} : absR ⟪x, x⟫_ℝ = ⟪x, x⟫_ℝ := by { have h := @inner_self_abs_to_K ℝ F _ _ x, simpa using h } lemma inner_abs_conj_sym {x y : E} : abs ⟪x, y⟫ = abs ⟪y, x⟫ := by rw [←inner_conj_sym, abs_conj] @[simp] lemma inner_neg_left {x y : E} : ⟪-x, y⟫ = -⟪x, y⟫ := by { rw [← neg_one_smul 𝕜 x, inner_smul_left], simp } @[simp] lemma inner_neg_right {x y : E} : ⟪x, -y⟫ = -⟪x, y⟫ := by rw [←inner_conj_sym, inner_neg_left]; simp only [ring_hom.map_neg, inner_conj_sym] lemma inner_neg_neg {x y : E} : ⟪-x, -y⟫ = ⟪x, y⟫ := by simp @[simp] lemma inner_self_conj {x : E} : ⟪x, x⟫† = ⟪x, x⟫ := by rw [is_R_or_C.ext_iff]; exact ⟨by rw [conj_re], by rw [conj_im, inner_self_im_zero, neg_zero]⟩ lemma inner_sub_left {x y z : E} : ⟪x - y, z⟫ = ⟪x, z⟫ - ⟪y, z⟫ := by { simp [sub_eq_add_neg, inner_add_left] } lemma inner_sub_right {x y z : E} : ⟪x, y - z⟫ = ⟪x, y⟫ - ⟪x, z⟫ := by { simp [sub_eq_add_neg, inner_add_right] } lemma inner_mul_conj_re_abs {x y : E} : re (⟪x, y⟫ * ⟪y, x⟫) = abs (⟪x, y⟫ * ⟪y, x⟫) := by { rw[←inner_conj_sym, mul_comm], exact re_eq_abs_of_mul_conj (inner y x), } /-- Expand `⟪x + y, x + y⟫` -/ lemma inner_add_add_self {x y : E} : ⟪x + y, x + y⟫ = ⟪x, x⟫ + ⟪x, y⟫ + ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_add_left, inner_add_right]; ring /-- Expand `⟪x + y, x + y⟫_ℝ` -/ lemma real_inner_add_add_self {x y : F} : ⟪x + y, x + y⟫_ℝ = ⟪x, x⟫_ℝ + 2 * ⟪x, y⟫_ℝ + ⟪y, y⟫_ℝ := begin have : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := by rw [←inner_conj_sym]; refl, simp [inner_add_add_self, this], ring, end /- Expand `⟪x - y, x - y⟫` -/ lemma inner_sub_sub_self {x y : E} : ⟪x - y, x - y⟫ = ⟪x, x⟫ - ⟪x, y⟫ - ⟪y, x⟫ + ⟪y, y⟫ := by simp only [inner_sub_left, inner_sub_right]; ring /-- Expand `⟪x - y, x - y⟫_ℝ` -/ lemma real_inner_sub_sub_self {x y : F} : ⟪x - y, x - y⟫_ℝ = ⟪x, x⟫_ℝ - 2 * ⟪x, y⟫_ℝ + ⟪y, y⟫_ℝ := begin have : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := by rw [←inner_conj_sym]; refl, simp [inner_sub_sub_self, this], ring, end /-- Parallelogram law -/ lemma parallelogram_law {x y : E} : ⟪x + y, x + y⟫ + ⟪x - y, x - y⟫ = 2 * (⟪x, x⟫ + ⟪y, y⟫) := by simp [inner_add_add_self, inner_sub_sub_self, two_mul, sub_eq_add_neg, add_comm, add_left_comm] /-- Cauchy–Schwarz inequality. This proof follows "Proof 2" on Wikipedia. -/ lemma inner_mul_inner_self_le (x y : E) : abs ⟪x, y⟫ * abs ⟪y, x⟫ ≤ re ⟪x, x⟫ * re ⟪y, y⟫ := begin by_cases hy : y = 0, { rw [hy], simp only [is_R_or_C.abs_zero, inner_zero_left, mul_zero, add_monoid_hom.map_zero] }, { change y ≠ 0 at hy, have hy' : ⟪y, y⟫ ≠ 0 := λ h, by rw [inner_self_eq_zero] at h; exact hy h, set T := ⟪y, x⟫ / ⟪y, y⟫ with hT, have h₁ : re ⟪y, x⟫ = re ⟪x, y⟫ := inner_re_symm, have h₂ : im ⟪y, x⟫ = -im ⟪x, y⟫ := inner_im_symm, have h₃ : ⟪y, x⟫ * ⟪x, y⟫ * ⟪y, y⟫ / (⟪y, y⟫ * ⟪y, y⟫) = ⟪y, x⟫ * ⟪x, y⟫ / ⟪y, y⟫, { rw [mul_div_assoc], have : ⟪y, y⟫ / (⟪y, y⟫ * ⟪y, y⟫) = 1 / ⟪y, y⟫ := by rw [div_mul_eq_div_mul_one_div, div_self hy', one_mul], rw [this, div_eq_mul_inv, one_mul, ←div_eq_mul_inv] }, have h₄ : ⟪y, y⟫ = re ⟪y, y⟫ := by simp, have h₅ : re ⟪y, y⟫ > 0, { refine lt_of_le_of_ne inner_self_nonneg _, intro H, apply hy', rw is_R_or_C.ext_iff, exact ⟨by simp only [H, zero_re'], by simp only [inner_self_nonneg_im, add_monoid_hom.map_zero]⟩ }, have h₆ : re ⟪y, y⟫ ≠ 0 := ne_of_gt h₅, have hmain := calc 0 ≤ re ⟪x - T • y, x - T • y⟫ : inner_self_nonneg ... = re ⟪x, x⟫ - re ⟪T • y, x⟫ - re ⟪x, T • y⟫ + re ⟪T • y, T • y⟫ : by simp only [inner_sub_sub_self, inner_smul_left, inner_smul_right, h₁, h₂, neg_mul_eq_neg_mul_symm, add_monoid_hom.map_add, conj_im, add_monoid_hom.map_sub, mul_neg_eq_neg_mul_symm, conj_re, neg_neg, mul_re] ... = re ⟪x, x⟫ - re (T† * ⟪y, x⟫) - re (T * ⟪x, y⟫) + re (T * T† * ⟪y, y⟫) : by simp only [inner_smul_left, inner_smul_right, mul_assoc] ... = re ⟪x, x⟫ - re (⟪x, y⟫ / ⟪y, y⟫ * ⟪y, x⟫) : by field_simp [-mul_re, hT, conj_div, h₁, h₃, inner_conj_sym] ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫ / ⟪y, y⟫) : by rw [div_mul_eq_mul_div_comm, ←mul_div_assoc] ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫ / re ⟪y, y⟫) : by conv_lhs { rw [h₄] } ... = re ⟪x, x⟫ - re (⟪x, y⟫ * ⟪y, x⟫) / re ⟪y, y⟫ : by rw [div_re_of_real] ... = re ⟪x, x⟫ - abs (⟪x, y⟫ * ⟪y, x⟫) / re ⟪y, y⟫ : by rw [inner_mul_conj_re_abs] ... = re ⟪x, x⟫ - abs ⟪x, y⟫ * abs ⟪y, x⟫ / re ⟪y, y⟫ : by rw is_R_or_C.abs_mul, have hmain' : abs ⟪x, y⟫ * abs ⟪y, x⟫ / re ⟪y, y⟫ ≤ re ⟪x, x⟫ := by linarith, have := (mul_le_mul_right h₅).mpr hmain', rwa [div_mul_cancel (abs ⟪x, y⟫ * abs ⟪y, x⟫) h₆] at this } end /-- Cauchy–Schwarz inequality for real inner products. -/ lemma real_inner_mul_inner_self_le (x y : F) : ⟪x, y⟫_ℝ * ⟪x, y⟫_ℝ ≤ ⟪x, x⟫_ℝ * ⟪y, y⟫_ℝ := begin have h₁ : ⟪y, x⟫_ℝ = ⟪x, y⟫_ℝ := by rw [←inner_conj_sym]; refl, have h₂ := @inner_mul_inner_self_le ℝ F _ _ x y, dsimp at h₂, have h₃ := abs_mul_abs_self ⟪x, y⟫_ℝ, rw [h₁] at h₂, simpa [h₃] using h₂, end /-- A family of vectors is linearly independent if they are nonzero and orthogonal. -/ lemma linear_independent_of_ne_zero_of_inner_eq_zero {ι : Type*} {v : ι → E} (hz : ∀ i, v i ≠ 0) (ho : ∀ i j, i ≠ j → ⟪v i, v j⟫ = 0) : linear_independent 𝕜 v := begin rw linear_independent_iff', intros s g hg i hi, have h' : g i * inner (v i) (v i) = inner (v i) (∑ j in s, g j • v j), { rw inner_sum, symmetry, convert finset.sum_eq_single i _ _, { rw inner_smul_right }, { intros j hj hji, rw [inner_smul_right, ho i j hji.symm, mul_zero] }, { exact λ h, false.elim (h hi) } }, simpa [hg, hz] using h' end end basic_properties section orthonormal_sets variables {ι : Type*} (𝕜) include 𝕜 /-- An orthonormal set of vectors in an `inner_product_space` -/ def orthonormal (v : ι → E) : Prop := (∀ i, ∥v i∥ = 1) ∧ (∀ {i j}, i ≠ j → ⟪v i, v j⟫ = 0) omit 𝕜 variables {𝕜} /-- `if ... then ... else` characterization of an indexed set of vectors being orthonormal. (Inner product equals Kronecker delta.) -/ lemma orthonormal_iff_ite {v : ι → E} : orthonormal 𝕜 v ↔ ∀ i j, ⟪v i, v j⟫ = if i = j then (1:𝕜) else (0:𝕜) := begin split, { intros hv i j, split_ifs, { simp [h, inner_self_eq_norm_sq_to_K, hv.1] }, { exact hv.2 h } }, { intros h, split, { intros i, have h' : ∥v i∥ ^ 2 = 1 ^ 2 := by simp [norm_sq_eq_inner, h i i], have h₁ : 0 ≤ ∥v i∥ := norm_nonneg _, have h₂ : (0:ℝ) ≤ 1 := by norm_num, rwa eq_of_sq_eq_sq h₁ h₂ at h' }, { intros i j hij, simpa [hij] using h i j } } end /-- `if ... then ... else` characterization of a set of vectors being orthonormal. (Inner product equals Kronecker delta.) -/ theorem orthonormal_subtype_iff_ite {s : set E} : orthonormal 𝕜 (coe : s → E) ↔ (∀ v ∈ s, ∀ w ∈ s, ⟪v, w⟫ = if v = w then 1 else 0) := begin rw orthonormal_iff_ite, split, { intros h v hv w hw, convert h ⟨v, hv⟩ ⟨w, hw⟩ using 1, simp }, { rintros h ⟨v, hv⟩ ⟨w, hw⟩, convert h v hv w hw using 1, simp } end /-- The inner product of a linear combination of a set of orthonormal vectors with one of those vectors picks out the coefficient of that vector. -/ lemma orthonormal.inner_right_finsupp {v : ι → E} (hv : orthonormal 𝕜 v) (l : ι →₀ 𝕜) (i : ι) : ⟪v i, finsupp.total ι E 𝕜 v l⟫ = l i := by simp [finsupp.total_apply, finsupp.inner_sum, orthonormal_iff_ite.mp hv] /-- The inner product of a linear combination of a set of orthonormal vectors with one of those vectors picks out the coefficient of that vector. -/ lemma orthonormal.inner_right_fintype [fintype ι] {v : ι → E} (hv : orthonormal 𝕜 v) (l : ι → 𝕜) (i : ι) : ⟪v i, ∑ i : ι, (l i) • (v i)⟫ = l i := by simp [inner_sum, inner_smul_right, orthonormal_iff_ite.mp hv] /-- The inner product of a linear combination of a set of orthonormal vectors with one of those vectors picks out the coefficient of that vector. -/ lemma orthonormal.inner_left_finsupp {v : ι → E} (hv : orthonormal 𝕜 v) (l : ι →₀ 𝕜) (i : ι) : ⟪finsupp.total ι E 𝕜 v l, v i⟫ = conj (l i) := by rw [← inner_conj_sym, hv.inner_right_finsupp] /-- The inner product of a linear combination of a set of orthonormal vectors with one of those vectors picks out the coefficient of that vector. -/ lemma orthonormal.inner_left_fintype [fintype ι] {v : ι → E} (hv : orthonormal 𝕜 v) (l : ι → 𝕜) (i : ι) : ⟪∑ i : ι, (l i) • (v i), v i⟫ = conj (l i) := by simp [sum_inner, inner_smul_left, orthonormal_iff_ite.mp hv] /-- An orthonormal set is linearly independent. -/ lemma orthonormal.linear_independent {v : ι → E} (hv : orthonormal 𝕜 v) : linear_independent 𝕜 v := begin rw linear_independent_iff, intros l hl, ext i, have key : ⟪v i, finsupp.total ι E 𝕜 v l⟫ = ⟪v i, 0⟫ := by rw hl, simpa [hv.inner_right_finsupp] using key end /-- A subfamily of an orthonormal family (i.e., a composition with an injective map) is an orthonormal family. -/ lemma orthonormal.comp {ι' : Type*} {v : ι → E} (hv : orthonormal 𝕜 v) (f : ι' → ι) (hf : function.injective f) : orthonormal 𝕜 (v ∘ f) := begin rw orthonormal_iff_ite at ⊢ hv, intros i j, convert hv (f i) (f j) using 1, simp [hf.eq_iff] end /-- A linear combination of some subset of an orthonormal set is orthogonal to other members of the set. -/ lemma orthonormal.inner_finsupp_eq_zero {v : ι → E} (hv : orthonormal 𝕜 v) {s : set ι} {i : ι} (hi : i ∉ s) {l : ι →₀ 𝕜} (hl : l ∈ finsupp.supported 𝕜 𝕜 s) : ⟪finsupp.total ι E 𝕜 v l, v i⟫ = 0 := begin rw finsupp.mem_supported' at hl, simp [hv.inner_left_finsupp, hl i hi], end /- The material that follows, culminating in the existence of a maximal orthonormal subset, is adapted from the corresponding development of the theory of linearly independents sets. See `exists_linear_independent` in particular. -/ variables (𝕜 E) lemma orthonormal_empty : orthonormal 𝕜 (λ x, x : (∅ : set E) → E) := by simp [orthonormal_subtype_iff_ite] variables {𝕜 E} lemma orthonormal_Union_of_directed {η : Type*} {s : η → set E} (hs : directed (⊆) s) (h : ∀ i, orthonormal 𝕜 (λ x, x : s i → E)) : orthonormal 𝕜 (λ x, x : (⋃ i, s i) → E) := begin rw orthonormal_subtype_iff_ite, rintros x ⟨_, ⟨i, rfl⟩, hxi⟩ y ⟨_, ⟨j, rfl⟩, hyj⟩, obtain ⟨k, hik, hjk⟩ := hs i j, have h_orth : orthonormal 𝕜 (λ x, x : (s k) → E) := h k, rw orthonormal_subtype_iff_ite at h_orth, exact h_orth x (hik hxi) y (hjk hyj) end lemma orthonormal_sUnion_of_directed {s : set (set E)} (hs : directed_on (⊆) s) (h : ∀ a ∈ s, orthonormal 𝕜 (λ x, x : (a : set E) → E)) : orthonormal 𝕜 (λ x, x : (⋃₀ s) → E) := by rw set.sUnion_eq_Union; exact orthonormal_Union_of_directed hs.directed_coe (by simpa using h) /-- Given an orthonormal set `v` of vectors in `E`, there exists a maximal orthonormal set containing it. -/ lemma exists_maximal_orthonormal {s : set E} (hs : orthonormal 𝕜 (coe : s → E)) : ∃ w ⊇ s, orthonormal 𝕜 (coe : w → E) ∧ ∀ u ⊇ w, orthonormal 𝕜 (coe : u → E) → u = w := begin rcases zorn.zorn_subset_nonempty {b | orthonormal 𝕜 (coe : b → E)} _ _ hs with ⟨b, bi, sb, h⟩, { refine ⟨b, sb, bi, _⟩, exact λ u hus hu, h u hu hus }, { refine λ c hc cc c0, ⟨⋃₀ c, _, _⟩, { exact orthonormal_sUnion_of_directed cc.directed_on (λ x xc, hc xc) }, { exact λ _, set.subset_sUnion_of_mem } } end lemma orthonormal.ne_zero {v : ι → E} (hv : orthonormal 𝕜 v) (i : ι) : v i ≠ 0 := begin have : ∥v i∥ ≠ 0, { rw hv.1 i, norm_num }, simpa using this end open finite_dimensional /-- A family of orthonormal vectors with the correct cardinality forms a basis. -/ def basis_of_orthonormal_of_card_eq_finrank [fintype ι] [nonempty ι] {v : ι → E} (hv : orthonormal 𝕜 v) (card_eq : fintype.card ι = finrank 𝕜 E) : basis ι 𝕜 E := basis_of_linear_independent_of_card_eq_finrank hv.linear_independent card_eq @[simp] lemma coe_basis_of_orthonormal_of_card_eq_finrank [fintype ι] [nonempty ι] {v : ι → E} (hv : orthonormal 𝕜 v) (card_eq : fintype.card ι = finrank 𝕜 E) : (basis_of_orthonormal_of_card_eq_finrank hv card_eq : ι → E) = v := coe_basis_of_linear_independent_of_card_eq_finrank _ _ end orthonormal_sets section norm lemma norm_eq_sqrt_inner (x : E) : ∥x∥ = sqrt (re ⟪x, x⟫) := begin have h₁ : ∥x∥^2 = re ⟪x, x⟫ := norm_sq_eq_inner x, have h₂ := congr_arg sqrt h₁, simpa using h₂, end lemma norm_eq_sqrt_real_inner (x : F) : ∥x∥ = sqrt ⟪x, x⟫_ℝ := by { have h := @norm_eq_sqrt_inner ℝ F _ _ x, simpa using h } lemma inner_self_eq_norm_sq (x : E) : re ⟪x, x⟫ = ∥x∥ * ∥x∥ := by rw[norm_eq_sqrt_inner, ←sqrt_mul inner_self_nonneg (re ⟪x, x⟫), sqrt_mul_self inner_self_nonneg] lemma real_inner_self_eq_norm_sq (x : F) : ⟪x, x⟫_ℝ = ∥x∥ * ∥x∥ := by { have h := @inner_self_eq_norm_sq ℝ F _ _ x, simpa using h } /-- Expand the square -/ lemma norm_add_sq {x y : E} : ∥x + y∥^2 = ∥x∥^2 + 2 * (re ⟪x, y⟫) + ∥y∥^2 := begin repeat {rw [sq, ←inner_self_eq_norm_sq]}, rw[inner_add_add_self, two_mul], simp only [add_assoc, add_left_inj, add_right_inj, add_monoid_hom.map_add], rw [←inner_conj_sym, conj_re], end alias norm_add_sq ← norm_add_pow_two /-- Expand the square -/ lemma norm_add_sq_real {x y : F} : ∥x + y∥^2 = ∥x∥^2 + 2 * ⟪x, y⟫_ℝ + ∥y∥^2 := by { have h := @norm_add_sq ℝ F _ _, simpa using h } alias norm_add_sq_real ← norm_add_pow_two_real /-- Expand the square -/ lemma norm_add_mul_self {x y : E} : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + 2 * (re ⟪x, y⟫) + ∥y∥ * ∥y∥ := by { repeat {rw [← sq]}, exact norm_add_sq } /-- Expand the square -/ lemma norm_add_mul_self_real {x y : F} : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + 2 * ⟪x, y⟫_ℝ + ∥y∥ * ∥y∥ := by { have h := @norm_add_mul_self ℝ F _ _, simpa using h } /-- Expand the square -/ lemma norm_sub_sq {x y : E} : ∥x - y∥^2 = ∥x∥^2 - 2 * (re ⟪x, y⟫) + ∥y∥^2 := begin repeat {rw [sq, ←inner_self_eq_norm_sq]}, rw[inner_sub_sub_self], calc re (⟪x, x⟫ - ⟪x, y⟫ - ⟪y, x⟫ + ⟪y, y⟫) = re ⟪x, x⟫ - re ⟪x, y⟫ - re ⟪y, x⟫ + re ⟪y, y⟫ : by simp ... = -re ⟪y, x⟫ - re ⟪x, y⟫ + re ⟪x, x⟫ + re ⟪y, y⟫ : by ring ... = -re (⟪x, y⟫†) - re ⟪x, y⟫ + re ⟪x, x⟫ + re ⟪y, y⟫ : by rw[inner_conj_sym] ... = -re ⟪x, y⟫ - re ⟪x, y⟫ + re ⟪x, x⟫ + re ⟪y, y⟫ : by rw[conj_re] ... = re ⟪x, x⟫ - 2*re ⟪x, y⟫ + re ⟪y, y⟫ : by ring end alias norm_sub_sq ← norm_sub_pow_two /-- Expand the square -/ lemma norm_sub_sq_real {x y : F} : ∥x - y∥^2 = ∥x∥^2 - 2 * ⟪x, y⟫_ℝ + ∥y∥^2 := norm_sub_sq alias norm_sub_sq_real ← norm_sub_pow_two_real /-- Expand the square -/ lemma norm_sub_mul_self {x y : E} : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ - 2 * re ⟪x, y⟫ + ∥y∥ * ∥y∥ := by { repeat {rw [← sq]}, exact norm_sub_sq } /-- Expand the square -/ lemma norm_sub_mul_self_real {x y : F} : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ - 2 * ⟪x, y⟫_ℝ + ∥y∥ * ∥y∥ := by { have h := @norm_sub_mul_self ℝ F _ _, simpa using h } /-- Cauchy–Schwarz inequality with norm -/ lemma abs_inner_le_norm (x y : E) : abs ⟪x, y⟫ ≤ ∥x∥ * ∥y∥ := nonneg_le_nonneg_of_sq_le_sq (mul_nonneg (norm_nonneg _) (norm_nonneg _)) begin have : ∥x∥ * ∥y∥ * (∥x∥ * ∥y∥) = (re ⟪x, x⟫) * (re ⟪y, y⟫), simp only [inner_self_eq_norm_sq], ring, rw this, conv_lhs { congr, skip, rw [inner_abs_conj_sym] }, exact inner_mul_inner_self_le _ _ end /-- Cauchy–Schwarz inequality with norm -/ lemma abs_real_inner_le_norm (x y : F) : absR ⟪x, y⟫_ℝ ≤ ∥x∥ * ∥y∥ := by { have h := @abs_inner_le_norm ℝ F _ _ x y, simpa using h } /-- Cauchy–Schwarz inequality with norm -/ lemma real_inner_le_norm (x y : F) : ⟪x, y⟫_ℝ ≤ ∥x∥ * ∥y∥ := le_trans (le_abs_self _) (abs_real_inner_le_norm _ _) include 𝕜 lemma parallelogram_law_with_norm {x y : E} : ∥x + y∥ * ∥x + y∥ + ∥x - y∥ * ∥x - y∥ = 2 * (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥) := begin simp only [← inner_self_eq_norm_sq], rw[← re.map_add, parallelogram_law, two_mul, two_mul], simp only [re.map_add], end omit 𝕜 lemma parallelogram_law_with_norm_real {x y : F} : ∥x + y∥ * ∥x + y∥ + ∥x - y∥ * ∥x - y∥ = 2 * (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥) := by { have h := @parallelogram_law_with_norm ℝ F _ _ x y, simpa using h } /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ lemma re_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two (x y : E) : re ⟪x, y⟫ = (∥x + y∥ * ∥x + y∥ - ∥x∥ * ∥x∥ - ∥y∥ * ∥y∥) / 2 := by { rw norm_add_mul_self, ring } /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ lemma re_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two (x y : E) : re ⟪x, y⟫ = (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ - ∥x - y∥ * ∥x - y∥) / 2 := by { rw [norm_sub_mul_self], ring } /-- Polarization identity: The real part of the inner product, in terms of the norm. -/ lemma re_inner_eq_norm_add_mul_self_sub_norm_sub_mul_self_div_four (x y : E) : re ⟪x, y⟫ = (∥x + y∥ * ∥x + y∥ - ∥x - y∥ * ∥x - y∥) / 4 := by { rw [norm_add_mul_self, norm_sub_mul_self], ring } /-- Polarization identity: The imaginary part of the inner product, in terms of the norm. -/ lemma im_inner_eq_norm_sub_I_smul_mul_self_sub_norm_add_I_smul_mul_self_div_four (x y : E) : im ⟪x, y⟫ = (∥x - IK • y∥ * ∥x - IK • y∥ - ∥x + IK • y∥ * ∥x + IK • y∥) / 4 := by { simp only [norm_add_mul_self, norm_sub_mul_self, inner_smul_right, I_mul_re], ring } /-- Polarization identity: The inner product, in terms of the norm. -/ lemma inner_eq_sum_norm_sq_div_four (x y : E) : ⟪x, y⟫ = (∥x + y∥ ^ 2 - ∥x - y∥ ^ 2 + (∥x - IK • y∥ ^ 2 - ∥x + IK • y∥ ^ 2) * IK) / 4 := begin rw [← re_add_im ⟪x, y⟫, re_inner_eq_norm_add_mul_self_sub_norm_sub_mul_self_div_four, im_inner_eq_norm_sub_I_smul_mul_self_sub_norm_add_I_smul_mul_self_div_four], push_cast, simp only [sq, ← mul_div_right_comm, ← add_div] end section variables {E' : Type*} [inner_product_space 𝕜 E'] /-- A linear isometry preserves the inner product. -/ @[simp] lemma linear_isometry.inner_map_map (f : E →ₗᵢ[𝕜] E') (x y : E) : ⟪f x, f y⟫ = ⟪x, y⟫ := by simp [inner_eq_sum_norm_sq_div_four, ← f.norm_map] /-- A linear isometric equivalence preserves the inner product. -/ @[simp] lemma linear_isometry_equiv.inner_map_map (f : E ≃ₗᵢ[𝕜] E') (x y : E) : ⟪f x, f y⟫ = ⟪x, y⟫ := f.to_linear_isometry.inner_map_map x y /-- A linear map that preserves the inner product is a linear isometry. -/ def linear_map.isometry_of_inner (f : E →ₗ[𝕜] E') (h : ∀ x y, ⟪f x, f y⟫ = ⟪x, y⟫) : E →ₗᵢ[𝕜] E' := ⟨f, λ x, by simp only [norm_eq_sqrt_inner, h]⟩ @[simp] lemma linear_map.coe_isometry_of_inner (f : E →ₗ[𝕜] E') (h) : ⇑(f.isometry_of_inner h) = f := rfl @[simp] lemma linear_map.isometry_of_inner_to_linear_map (f : E →ₗ[𝕜] E') (h) : (f.isometry_of_inner h).to_linear_map = f := rfl /-- A linear equivalence that preserves the inner product is a linear isometric equivalence. -/ def linear_equiv.isometry_of_inner (f : E ≃ₗ[𝕜] E') (h : ∀ x y, ⟪f x, f y⟫ = ⟪x, y⟫) : E ≃ₗᵢ[𝕜] E' := ⟨f, ((f : E →ₗ[𝕜] E').isometry_of_inner h).norm_map⟩ @[simp] lemma linear_equiv.coe_isometry_of_inner (f : E ≃ₗ[𝕜] E') (h) : ⇑(f.isometry_of_inner h) = f := rfl @[simp] lemma linear_equiv.isometry_of_inner_to_linear_equiv (f : E ≃ₗ[𝕜] E') (h) : (f.isometry_of_inner h).to_linear_equiv = f := rfl end /-- Polarization identity: The real inner product, in terms of the norm. -/ lemma real_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two (x y : F) : ⟪x, y⟫_ℝ = (∥x + y∥ * ∥x + y∥ - ∥x∥ * ∥x∥ - ∥y∥ * ∥y∥) / 2 := re_to_real.symm.trans $ re_inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two x y /-- Polarization identity: The real inner product, in terms of the norm. -/ lemma real_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two (x y : F) : ⟪x, y⟫_ℝ = (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ - ∥x - y∥ * ∥x - y∥) / 2 := re_to_real.symm.trans $ re_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two x y /-- Pythagorean theorem, if-and-only-if vector inner product form. -/ lemma norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero (x y : F) : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ ↔ ⟪x, y⟫_ℝ = 0 := begin rw [norm_add_mul_self, add_right_cancel_iff, add_right_eq_self, mul_eq_zero], norm_num end /-- Pythagorean theorem, vector inner product form. -/ lemma norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero (x y : E) (h : ⟪x, y⟫ = 0) : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ := begin rw [norm_add_mul_self, add_right_cancel_iff, add_right_eq_self, mul_eq_zero], apply or.inr, simp only [h, zero_re'], end /-- Pythagorean theorem, vector inner product form. -/ lemma norm_add_sq_eq_norm_sq_add_norm_sq_real {x y : F} (h : ⟪x, y⟫_ℝ = 0) : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ := (norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero x y).2 h /-- Pythagorean theorem, subtracting vectors, if-and-only-if vector inner product form. -/ lemma norm_sub_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero (x y : F) : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ ↔ ⟪x, y⟫_ℝ = 0 := begin rw [norm_sub_mul_self, add_right_cancel_iff, sub_eq_add_neg, add_right_eq_self, neg_eq_zero, mul_eq_zero], norm_num end /-- Pythagorean theorem, subtracting vectors, vector inner product form. -/ lemma norm_sub_sq_eq_norm_sq_add_norm_sq_real {x y : F} (h : ⟪x, y⟫_ℝ = 0) : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ := (norm_sub_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero x y).2 h /-- The sum and difference of two vectors are orthogonal if and only if they have the same norm. -/ lemma real_inner_add_sub_eq_zero_iff (x y : F) : ⟪x + y, x - y⟫_ℝ = 0 ↔ ∥x∥ = ∥y∥ := begin conv_rhs { rw ←mul_self_inj_of_nonneg (norm_nonneg _) (norm_nonneg _) }, simp only [←inner_self_eq_norm_sq, inner_add_left, inner_sub_right, real_inner_comm y x, sub_eq_zero, re_to_real], split, { intro h, rw [add_comm] at h, linarith }, { intro h, linarith } end /-- The real inner product of two vectors, divided by the product of their norms, has absolute value at most 1. -/ lemma abs_real_inner_div_norm_mul_norm_le_one (x y : F) : absR (⟪x, y⟫_ℝ / (∥x∥ * ∥y∥)) ≤ 1 := begin rw _root_.abs_div, by_cases h : 0 = absR (∥x∥ * ∥y∥), { rw [←h, div_zero], norm_num }, { change 0 ≠ absR (∥x∥ * ∥y∥) at h, rw div_le_iff' (lt_of_le_of_ne (ge_iff_le.mp (_root_.abs_nonneg (∥x∥ * ∥y∥))) h), convert abs_real_inner_le_norm x y using 1, rw [_root_.abs_mul, _root_.abs_of_nonneg (norm_nonneg x), _root_.abs_of_nonneg (norm_nonneg y), mul_one] } end /-- The inner product of a vector with a multiple of itself. -/ lemma real_inner_smul_self_left (x : F) (r : ℝ) : ⟪r • x, x⟫_ℝ = r * (∥x∥ * ∥x∥) := by rw [real_inner_smul_left, ←real_inner_self_eq_norm_sq] /-- The inner product of a vector with a multiple of itself. -/ lemma real_inner_smul_self_right (x : F) (r : ℝ) : ⟪x, r • x⟫_ℝ = r * (∥x∥ * ∥x∥) := by rw [inner_smul_right, ←real_inner_self_eq_norm_sq] /-- The inner product of a nonzero vector with a nonzero multiple of itself, divided by the product of their norms, has absolute value 1. -/ lemma abs_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul {x : E} {r : 𝕜} (hx : x ≠ 0) (hr : r ≠ 0) : abs ⟪x, r • x⟫ / (∥x∥ * ∥r • x∥) = 1 := begin have hx' : ∥x∥ ≠ 0 := by simp [norm_eq_zero, hx], have hr' : abs r ≠ 0 := by simp [is_R_or_C.abs_eq_zero, hr], rw [inner_smul_right, is_R_or_C.abs_mul, ←inner_self_re_abs, inner_self_eq_norm_sq, norm_smul], rw [is_R_or_C.norm_eq_abs, ←mul_assoc, ←div_div_eq_div_mul, mul_div_cancel _ hx', ←div_div_eq_div_mul, mul_comm, mul_div_cancel _ hr', div_self hx'], end /-- The inner product of a nonzero vector with a nonzero multiple of itself, divided by the product of their norms, has absolute value 1. -/ lemma abs_real_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul {x : F} {r : ℝ} (hx : x ≠ 0) (hr : r ≠ 0) : absR ⟪x, r • x⟫_ℝ / (∥x∥ * ∥r • x∥) = 1 := begin rw ← abs_to_real, exact abs_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul hx hr end /-- The inner product of a nonzero vector with a positive multiple of itself, divided by the product of their norms, has value 1. -/ lemma real_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul {x : F} {r : ℝ} (hx : x ≠ 0) (hr : 0 < r) : ⟪x, r • x⟫_ℝ / (∥x∥ * ∥r • x∥) = 1 := begin rw [real_inner_smul_self_right, norm_smul, real.norm_eq_abs, ←mul_assoc ∥x∥, mul_comm _ (absR r), mul_assoc, _root_.abs_of_nonneg (le_of_lt hr), div_self], exact mul_ne_zero (ne_of_gt hr) (λ h, hx (norm_eq_zero.1 (eq_zero_of_mul_self_eq_zero h))) end /-- The inner product of a nonzero vector with a negative multiple of itself, divided by the product of their norms, has value -1. -/ lemma real_inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul {x : F} {r : ℝ} (hx : x ≠ 0) (hr : r < 0) : ⟪x, r • x⟫_ℝ / (∥x∥ * ∥r • x∥) = -1 := begin rw [real_inner_smul_self_right, norm_smul, real.norm_eq_abs, ←mul_assoc ∥x∥, mul_comm _ (absR r), mul_assoc, abs_of_neg hr, ←neg_mul_eq_neg_mul, div_neg_eq_neg_div, div_self], exact mul_ne_zero (ne_of_lt hr) (λ h, hx (norm_eq_zero.1 (eq_zero_of_mul_self_eq_zero h))) end /-- The inner product of two vectors, divided by the product of their norms, has absolute value 1 if and only if they are nonzero and one is a multiple of the other. One form of equality case for Cauchy-Schwarz. -/ lemma abs_inner_div_norm_mul_norm_eq_one_iff (x y : E) : abs (⟪x, y⟫ / (∥x∥ * ∥y∥)) = 1 ↔ (x ≠ 0 ∧ ∃ (r : 𝕜), r ≠ 0 ∧ y = r • x) := begin split, { intro h, have hx0 : x ≠ 0, { intro hx0, rw [hx0, inner_zero_left, zero_div] at h, norm_num at h, }, refine and.intro hx0 _, set r := ⟪x, y⟫ / (∥x∥ * ∥x∥) with hr, use r, set t := y - r • x with ht, have ht0 : ⟪x, t⟫ = 0, { rw [ht, inner_sub_right, inner_smul_right, hr], norm_cast, rw [←inner_self_eq_norm_sq, inner_self_re_to_K, div_mul_cancel _ (λ h, hx0 (inner_self_eq_zero.1 h)), sub_self] }, replace h : ∥r • x∥ / ∥t + r • x∥ = 1, { rw [←sub_add_cancel y (r • x), ←ht, inner_add_right, ht0, zero_add, inner_smul_right, is_R_or_C.abs_div, is_R_or_C.abs_mul, ←inner_self_re_abs, inner_self_eq_norm_sq] at h, norm_cast at h, rwa [_root_.abs_mul, abs_norm_eq_norm, abs_norm_eq_norm, ←mul_assoc, mul_comm, mul_div_mul_left _ _ (λ h, hx0 (norm_eq_zero.1 h)), ←is_R_or_C.norm_eq_abs, ←norm_smul] at h }, have hr0 : r ≠ 0, { intro hr0, rw [hr0, zero_smul, norm_zero, zero_div] at h, norm_num at h }, refine and.intro hr0 _, have h2 : ∥r • x∥ ^ 2 = ∥t + r • x∥ ^ 2, { rw [eq_of_div_eq_one h] }, replace h2 : ⟪r • x, r • x⟫ = ⟪t, t⟫ + ⟪t, r • x⟫ + ⟪r • x, t⟫ + ⟪r • x, r • x⟫, { rw [sq, sq, ←inner_self_eq_norm_sq, ←inner_self_eq_norm_sq ] at h2, have h2' := congr_arg (λ z : ℝ, (z : 𝕜)) h2, simp_rw [inner_self_re_to_K, inner_add_add_self] at h2', exact h2' }, conv at h2 in ⟪r • x, t⟫ { rw [inner_smul_left, ht0, mul_zero] }, symmetry' at h2, have h₁ : ⟪t, r • x⟫ = 0 := by { rw [inner_smul_right, ←inner_conj_sym, ht0], simp }, rw [add_zero, h₁, add_left_eq_self, add_zero, inner_self_eq_zero] at h2, rw h2 at ht, exact eq_of_sub_eq_zero ht.symm }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw [hy, is_R_or_C.abs_div], norm_cast, rw [_root_.abs_mul, abs_norm_eq_norm, abs_norm_eq_norm], exact abs_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul hx hr } end /-- The inner product of two vectors, divided by the product of their norms, has absolute value 1 if and only if they are nonzero and one is a multiple of the other. One form of equality case for Cauchy-Schwarz. -/ lemma abs_real_inner_div_norm_mul_norm_eq_one_iff (x y : F) : absR (⟪x, y⟫_ℝ / (∥x∥ * ∥y∥)) = 1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), r ≠ 0 ∧ y = r • x) := begin have := @abs_inner_div_norm_mul_norm_eq_one_iff ℝ F _ _ x y, simpa [coe_real_eq_id] using this, end /-- If the inner product of two vectors is equal to the product of their norms, then the two vectors are multiples of each other. One form of the equality case for Cauchy-Schwarz. Compare `inner_eq_norm_mul_iff`, which takes the stronger hypothesis `⟪x, y⟫ = ∥x∥ * ∥y∥`. -/ lemma abs_inner_eq_norm_iff (x y : E) (hx0 : x ≠ 0) (hy0 : y ≠ 0): abs ⟪x, y⟫ = ∥x∥ * ∥y∥ ↔ ∃ (r : 𝕜), r ≠ 0 ∧ y = r • x := begin have hx0' : ∥x∥ ≠ 0 := by simp [norm_eq_zero, hx0], have hy0' : ∥y∥ ≠ 0 := by simp [norm_eq_zero, hy0], have hxy0 : ∥x∥ * ∥y∥ ≠ 0 := by simp [hx0', hy0'], have h₁ : abs ⟪x, y⟫ = ∥x∥ * ∥y∥ ↔ abs (⟪x, y⟫ / (∥x∥ * ∥y∥)) = 1, { refine ⟨_ ,_⟩, { intro h, norm_cast, rw [is_R_or_C.abs_div, h, abs_of_real, _root_.abs_mul, abs_norm_eq_norm, abs_norm_eq_norm], exact div_self hxy0 }, { intro h, norm_cast at h, rwa [is_R_or_C.abs_div, abs_of_real, _root_.abs_mul, abs_norm_eq_norm, abs_norm_eq_norm, div_eq_one_iff_eq hxy0] at h } }, rw [h₁, abs_inner_div_norm_mul_norm_eq_one_iff x y], have : x ≠ 0 := λ h, (hx0' $ norm_eq_zero.mpr h), simp [this] end /-- The inner product of two vectors, divided by the product of their norms, has value 1 if and only if they are nonzero and one is a positive multiple of the other. -/ lemma real_inner_div_norm_mul_norm_eq_one_iff (x y : F) : ⟪x, y⟫_ℝ / (∥x∥ * ∥y∥) = 1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), 0 < r ∧ y = r • x) := begin split, { intro h, have ha := h, apply_fun absR at ha, norm_num at ha, rcases (abs_real_inner_div_norm_mul_norm_eq_one_iff x y).1 ha with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, use [hx, r], refine and.intro _ hy, by_contradiction hrneg, rw hy at h, rw real_inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul hx (lt_of_le_of_ne (le_of_not_lt hrneg) hr) at h, norm_num at h }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw hy, exact real_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul hx hr } end /-- The inner product of two vectors, divided by the product of their norms, has value -1 if and only if they are nonzero and one is a negative multiple of the other. -/ lemma real_inner_div_norm_mul_norm_eq_neg_one_iff (x y : F) : ⟪x, y⟫_ℝ / (∥x∥ * ∥y∥) = -1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), r < 0 ∧ y = r • x) := begin split, { intro h, have ha := h, apply_fun absR at ha, norm_num at ha, rcases (abs_real_inner_div_norm_mul_norm_eq_one_iff x y).1 ha with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, use [hx, r], refine and.intro _ hy, by_contradiction hrpos, rw hy at h, rw real_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul hx (lt_of_le_of_ne (le_of_not_lt hrpos) hr.symm) at h, norm_num at h }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw hy, exact real_inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul hx hr } end /-- If the inner product of two vectors is equal to the product of their norms (i.e., `⟪x, y⟫ = ∥x∥ * ∥y∥`), then the two vectors are nonnegative real multiples of each other. One form of the equality case for Cauchy-Schwarz. Compare `abs_inner_eq_norm_iff`, which takes the weaker hypothesis `abs ⟪x, y⟫ = ∥x∥ * ∥y∥`. -/ lemma inner_eq_norm_mul_iff {x y : E} : ⟪x, y⟫ = (∥x∥ : 𝕜) * ∥y∥ ↔ (∥y∥ : 𝕜) • x = (∥x∥ : 𝕜) • y := begin by_cases h : (x = 0 ∨ y = 0), -- WLOG `x` and `y` are nonzero { cases h; simp [h] }, calc ⟪x, y⟫ = (∥x∥ : 𝕜) * ∥y∥ ↔ ∥x∥ * ∥y∥ = re ⟪x, y⟫ : begin norm_cast, split, { intros h', simp [h'] }, { have cauchy_schwarz := abs_inner_le_norm x y, intros h', rw h' at ⊢ cauchy_schwarz, rwa re_eq_self_of_le } end ... ↔ 2 * ∥x∥ * ∥y∥ * (∥x∥ * ∥y∥ - re ⟪x, y⟫) = 0 : by simp [h, show (2:ℝ) ≠ 0, by norm_num, sub_eq_zero] ... ↔ ∥(∥y∥:𝕜) • x - (∥x∥:𝕜) • y∥ * ∥(∥y∥:𝕜) • x - (∥x∥:𝕜) • y∥ = 0 : begin simp only [norm_sub_mul_self, inner_smul_left, inner_smul_right, norm_smul, conj_of_real, is_R_or_C.norm_eq_abs, abs_of_real, of_real_im, of_real_re, mul_re, abs_norm_eq_norm], refine eq.congr _ rfl, ring end ... ↔ (∥y∥ : 𝕜) • x = (∥x∥ : 𝕜) • y : by simp [norm_sub_eq_zero_iff] end /-- If the inner product of two vectors is equal to the product of their norms (i.e., `⟪x, y⟫ = ∥x∥ * ∥y∥`), then the two vectors are nonnegative real multiples of each other. One form of the equality case for Cauchy-Schwarz. Compare `abs_inner_eq_norm_iff`, which takes the weaker hypothesis `abs ⟪x, y⟫ = ∥x∥ * ∥y∥`. -/ lemma inner_eq_norm_mul_iff_real {x y : F} : ⟪x, y⟫_ℝ = ∥x∥ * ∥y∥ ↔ ∥y∥ • x = ∥x∥ • y := inner_eq_norm_mul_iff /-- If the inner product of two unit vectors is `1`, then the two vectors are equal. One form of the equality case for Cauchy-Schwarz. -/ lemma inner_eq_norm_mul_iff_of_norm_one {x y : E} (hx : ∥x∥ = 1) (hy : ∥y∥ = 1) : ⟪x, y⟫ = 1 ↔ x = y := by { convert inner_eq_norm_mul_iff using 2; simp [hx, hy] } lemma inner_lt_norm_mul_iff_real {x y : F} : ⟪x, y⟫_ℝ < ∥x∥ * ∥y∥ ↔ ∥y∥ • x ≠ ∥x∥ • y := calc ⟪x, y⟫_ℝ < ∥x∥ * ∥y∥ ↔ ⟪x, y⟫_ℝ ≠ ∥x∥ * ∥y∥ : ⟨ne_of_lt, lt_of_le_of_ne (real_inner_le_norm _ _)⟩ ... ↔ ∥y∥ • x ≠ ∥x∥ • y : not_congr inner_eq_norm_mul_iff_real /-- If the inner product of two unit vectors is strictly less than `1`, then the two vectors are distinct. One form of the equality case for Cauchy-Schwarz. -/ lemma inner_lt_one_iff_real_of_norm_one {x y : F} (hx : ∥x∥ = 1) (hy : ∥y∥ = 1) : ⟪x, y⟫_ℝ < 1 ↔ x ≠ y := by { convert inner_lt_norm_mul_iff_real; simp [hx, hy] } /-- The inner product of two weighted sums, where the weights in each sum add to 0, in terms of the norms of pairwise differences. -/ lemma inner_sum_smul_sum_smul_of_sum_eq_zero {ι₁ : Type*} {s₁ : finset ι₁} {w₁ : ι₁ → ℝ} (v₁ : ι₁ → F) (h₁ : ∑ i in s₁, w₁ i = 0) {ι₂ : Type*} {s₂ : finset ι₂} {w₂ : ι₂ → ℝ} (v₂ : ι₂ → F) (h₂ : ∑ i in s₂, w₂ i = 0) : ⟪(∑ i₁ in s₁, w₁ i₁ • v₁ i₁), (∑ i₂ in s₂, w₂ i₂ • v₂ i₂)⟫_ℝ = (-∑ i₁ in s₁, ∑ i₂ in s₂, w₁ i₁ * w₂ i₂ * (∥v₁ i₁ - v₂ i₂∥ * ∥v₁ i₁ - v₂ i₂∥)) / 2 := by simp_rw [sum_inner, inner_sum, real_inner_smul_left, real_inner_smul_right, real_inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two, ←div_sub_div_same, ←div_add_div_same, mul_sub_left_distrib, left_distrib, finset.sum_sub_distrib, finset.sum_add_distrib, ←finset.mul_sum, ←finset.sum_mul, h₁, h₂, zero_mul, mul_zero, finset.sum_const_zero, zero_add, zero_sub, finset.mul_sum, neg_div, finset.sum_div, mul_div_assoc, mul_assoc] /-- The inner product with a fixed left element, as a continuous linear map. This can be upgraded to a continuous map which is jointly conjugate-linear in the left argument and linear in the right argument, once (TODO) conjugate-linear maps have been defined. -/ def inner_right (v : E) : E →L[𝕜] 𝕜 := linear_map.mk_continuous { to_fun := λ w, ⟪v, w⟫, map_add' := λ x y, inner_add_right, map_smul' := λ c x, inner_smul_right } ∥v∥ (by simpa [is_R_or_C.norm_eq_abs] using abs_inner_le_norm v) @[simp] lemma inner_right_coe (v : E) : (inner_right v : E → 𝕜) = λ w, ⟪v, w⟫ := rfl @[simp] lemma inner_right_apply (v w : E) : inner_right v w = ⟪v, w⟫ := rfl end norm /-- A field `𝕜` satisfying `is_R_or_C` is itself a `𝕜`-inner product space. -/ instance is_R_or_C.inner_product_space : inner_product_space 𝕜 𝕜 := { inner := (λ x y, (conj x) * y), norm_sq_eq_inner := λ x, by { unfold inner, rw [mul_comm, mul_conj, of_real_re, norm_sq_eq_def'] }, conj_sym := λ x y, by simp [mul_comm], add_left := λ x y z, by simp [inner, add_mul], smul_left := λ x y z, by simp [inner, mul_assoc] } @[simp] lemma is_R_or_C.inner_apply (x y : 𝕜) : ⟪x, y⟫ = (conj x) * y := rfl /-! ### Inner product space structure on subspaces -/ /-- Induced inner product on a submodule. -/ instance submodule.inner_product_space (W : submodule 𝕜 E) : inner_product_space 𝕜 W := { inner := λ x y, ⟪(x:E), (y:E)⟫, conj_sym := λ _ _, inner_conj_sym _ _ , norm_sq_eq_inner := λ _, norm_sq_eq_inner _, add_left := λ _ _ _ , inner_add_left, smul_left := λ _ _ _, inner_smul_left, ..submodule.normed_space W } /-- The inner product on submodules is the same as on the ambient space. -/ @[simp] lemma submodule.coe_inner (W : submodule 𝕜 E) (x y : W) : ⟪x, y⟫ = ⟪(x:E), ↑y⟫ := rfl section is_R_or_C_to_real variables {G : Type*} variables (𝕜 E) include 𝕜 /-- A general inner product implies a real inner product. This is not registered as an instance since it creates problems with the case `𝕜 = ℝ`. -/ def has_inner.is_R_or_C_to_real : has_inner ℝ E := { inner := λ x y, re ⟪x, y⟫ } /-- A general inner product space structure implies a real inner product structure. This is not registered as an instance since it creates problems with the case `𝕜 = ℝ`, but in can be used in a proof to obtain a real inner product space structure from a given `𝕜`-inner product space structure. -/ def inner_product_space.is_R_or_C_to_real : inner_product_space ℝ E := { norm_sq_eq_inner := norm_sq_eq_inner, conj_sym := λ x y, inner_re_symm, add_left := λ x y z, by { change re ⟪x + y, z⟫ = re ⟪x, z⟫ + re ⟪y, z⟫, simp [inner_add_left] }, smul_left := λ x y r, by { change re ⟪(r : 𝕜) • x, y⟫ = r * re ⟪x, y⟫, simp [inner_smul_left] }, ..has_inner.is_R_or_C_to_real 𝕜 E, ..normed_space.restrict_scalars ℝ 𝕜 E } variable {E} lemma real_inner_eq_re_inner (x y : E) : @has_inner.inner ℝ E (has_inner.is_R_or_C_to_real 𝕜 E) x y = re ⟪x, y⟫ := rfl omit 𝕜 /-- A complex inner product implies a real inner product -/ instance inner_product_space.complex_to_real [inner_product_space ℂ G] : inner_product_space ℝ G := inner_product_space.is_R_or_C_to_real ℂ G end is_R_or_C_to_real section deriv /-! ### Derivative of the inner product In this section we prove that the inner product and square of the norm in an inner space are infinitely `ℝ`-smooth. In order to state these results, we need a `normed_space ℝ E` instance. Though we can deduce this structure from `inner_product_space 𝕜 E`, this instance may be not definitionally equal to some other “natural” instance. So, we assume `[normed_space ℝ E]` and `[is_scalar_tower ℝ 𝕜 E]`. In both interesting cases `𝕜 = ℝ` and `𝕜 = ℂ` we have these instances. -/ variables [normed_space ℝ E] [is_scalar_tower ℝ 𝕜 E] lemma is_bounded_bilinear_map_inner : is_bounded_bilinear_map ℝ (λ p : E × E, ⟪p.1, p.2⟫) := { add_left := λ _ _ _, inner_add_left, smul_left := λ r x y, by simp only [← algebra_map_smul 𝕜 r x, algebra_map_eq_of_real, inner_smul_real_left], add_right := λ _ _ _, inner_add_right, smul_right := λ r x y, by simp only [← algebra_map_smul 𝕜 r y, algebra_map_eq_of_real, inner_smul_real_right], bound := ⟨1, zero_lt_one, λ x y, by { rw [one_mul, is_R_or_C.norm_eq_abs], exact abs_inner_le_norm x y, }⟩ } /-- Derivative of the inner product. -/ def fderiv_inner_clm (p : E × E) : E × E →L[ℝ] 𝕜 := is_bounded_bilinear_map_inner.deriv p @[simp] lemma fderiv_inner_clm_apply (p x : E × E) : fderiv_inner_clm p x = ⟪p.1, x.2⟫ + ⟪x.1, p.2⟫ := rfl lemma times_cont_diff_inner {n} : times_cont_diff ℝ n (λ p : E × E, ⟪p.1, p.2⟫) := is_bounded_bilinear_map_inner.times_cont_diff lemma times_cont_diff_at_inner {p : E × E} {n} : times_cont_diff_at ℝ n (λ p : E × E, ⟪p.1, p.2⟫) p := times_cont_diff_inner.times_cont_diff_at lemma differentiable_inner : differentiable ℝ (λ p : E × E, ⟪p.1, p.2⟫) := is_bounded_bilinear_map_inner.differentiable_at variables {G : Type*} [normed_group G] [normed_space ℝ G] {f g : G → E} {f' g' : G →L[ℝ] E} {s : set G} {x : G} {n : with_top ℕ} include 𝕜 lemma times_cont_diff_within_at.inner (hf : times_cont_diff_within_at ℝ n f s x) (hg : times_cont_diff_within_at ℝ n g s x) : times_cont_diff_within_at ℝ n (λ x, ⟪f x, g x⟫) s x := times_cont_diff_at_inner.comp_times_cont_diff_within_at x (hf.prod hg) lemma times_cont_diff_at.inner (hf : times_cont_diff_at ℝ n f x) (hg : times_cont_diff_at ℝ n g x) : times_cont_diff_at ℝ n (λ x, ⟪f x, g x⟫) x := hf.inner hg lemma times_cont_diff_on.inner (hf : times_cont_diff_on ℝ n f s) (hg : times_cont_diff_on ℝ n g s) : times_cont_diff_on ℝ n (λ x, ⟪f x, g x⟫) s := λ x hx, (hf x hx).inner (hg x hx) lemma times_cont_diff.inner (hf : times_cont_diff ℝ n f) (hg : times_cont_diff ℝ n g) : times_cont_diff ℝ n (λ x, ⟪f x, g x⟫) := times_cont_diff_inner.comp (hf.prod hg) lemma has_fderiv_within_at.inner (hf : has_fderiv_within_at f f' s x) (hg : has_fderiv_within_at g g' s x) : has_fderiv_within_at (λ t, ⟪f t, g t⟫) ((fderiv_inner_clm (f x, g x)).comp $ f'.prod g') s x := (is_bounded_bilinear_map_inner.has_fderiv_at (f x, g x)).comp_has_fderiv_within_at x (hf.prod hg) lemma has_fderiv_at.inner (hf : has_fderiv_at f f' x) (hg : has_fderiv_at g g' x) : has_fderiv_at (λ t, ⟪f t, g t⟫) ((fderiv_inner_clm (f x, g x)).comp $ f'.prod g') x := (is_bounded_bilinear_map_inner.has_fderiv_at (f x, g x)).comp x (hf.prod hg) lemma has_deriv_within_at.inner {f g : ℝ → E} {f' g' : E} {s : set ℝ} {x : ℝ} (hf : has_deriv_within_at f f' s x) (hg : has_deriv_within_at g g' s x) : has_deriv_within_at (λ t, ⟪f t, g t⟫) (⟪f x, g'⟫ + ⟪f', g x⟫) s x := by simpa using (hf.has_fderiv_within_at.inner hg.has_fderiv_within_at).has_deriv_within_at lemma has_deriv_at.inner {f g : ℝ → E} {f' g' : E} {x : ℝ} : has_deriv_at f f' x → has_deriv_at g g' x → has_deriv_at (λ t, ⟪f t, g t⟫) (⟪f x, g'⟫ + ⟪f', g x⟫) x := by simpa only [← has_deriv_within_at_univ] using has_deriv_within_at.inner lemma differentiable_within_at.inner (hf : differentiable_within_at ℝ f s x) (hg : differentiable_within_at ℝ g s x) : differentiable_within_at ℝ (λ x, ⟪f x, g x⟫) s x := ((differentiable_inner _).has_fderiv_at.comp_has_fderiv_within_at x (hf.prod hg).has_fderiv_within_at).differentiable_within_at lemma differentiable_at.inner (hf : differentiable_at ℝ f x) (hg : differentiable_at ℝ g x) : differentiable_at ℝ (λ x, ⟪f x, g x⟫) x := (differentiable_inner _).comp x (hf.prod hg) lemma differentiable_on.inner (hf : differentiable_on ℝ f s) (hg : differentiable_on ℝ g s) : differentiable_on ℝ (λ x, ⟪f x, g x⟫) s := λ x hx, (hf x hx).inner (hg x hx) lemma differentiable.inner (hf : differentiable ℝ f) (hg : differentiable ℝ g) : differentiable ℝ (λ x, ⟪f x, g x⟫) := λ x, (hf x).inner (hg x) lemma fderiv_inner_apply (hf : differentiable_at ℝ f x) (hg : differentiable_at ℝ g x) (y : G) : fderiv ℝ (λ t, ⟪f t, g t⟫) x y = ⟪f x, fderiv ℝ g x y⟫ + ⟪fderiv ℝ f x y, g x⟫ := by { rw [(hf.has_fderiv_at.inner hg.has_fderiv_at).fderiv], refl } lemma deriv_inner_apply {f g : ℝ → E} {x : ℝ} (hf : differentiable_at ℝ f x) (hg : differentiable_at ℝ g x) : deriv (λ t, ⟪f t, g t⟫) x = ⟪f x, deriv g x⟫ + ⟪deriv f x, g x⟫ := (hf.has_deriv_at.inner hg.has_deriv_at).deriv lemma times_cont_diff_norm_sq : times_cont_diff ℝ n (λ x : E, ∥x∥ ^ 2) := begin simp only [sq, ← inner_self_eq_norm_sq], exact (re_clm : 𝕜 →L[ℝ] ℝ).times_cont_diff.comp (times_cont_diff_id.inner times_cont_diff_id) end lemma times_cont_diff.norm_sq (hf : times_cont_diff ℝ n f) : times_cont_diff ℝ n (λ x, ∥f x∥ ^ 2) := times_cont_diff_norm_sq.comp hf lemma times_cont_diff_within_at.norm_sq (hf : times_cont_diff_within_at ℝ n f s x) : times_cont_diff_within_at ℝ n (λ y, ∥f y∥ ^ 2) s x := times_cont_diff_norm_sq.times_cont_diff_at.comp_times_cont_diff_within_at x hf lemma times_cont_diff_at.norm_sq (hf : times_cont_diff_at ℝ n f x) : times_cont_diff_at ℝ n (λ y, ∥f y∥ ^ 2) x := hf.norm_sq lemma times_cont_diff_at_norm {x : E} (hx : x ≠ 0) : times_cont_diff_at ℝ n norm x := have ∥id x∥ ^ 2 ≠ 0, from pow_ne_zero _ (norm_pos_iff.2 hx).ne', by simpa only [id, sqrt_sq, norm_nonneg] using times_cont_diff_at_id.norm_sq.sqrt this lemma times_cont_diff_at.norm (hf : times_cont_diff_at ℝ n f x) (h0 : f x ≠ 0) : times_cont_diff_at ℝ n (λ y, ∥f y∥) x := (times_cont_diff_at_norm h0).comp x hf lemma times_cont_diff_at.dist (hf : times_cont_diff_at ℝ n f x) (hg : times_cont_diff_at ℝ n g x) (hne : f x ≠ g x) : times_cont_diff_at ℝ n (λ y, dist (f y) (g y)) x := by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) } lemma times_cont_diff_within_at.norm (hf : times_cont_diff_within_at ℝ n f s x) (h0 : f x ≠ 0) : times_cont_diff_within_at ℝ n (λ y, ∥f y∥) s x := (times_cont_diff_at_norm h0).comp_times_cont_diff_within_at x hf lemma times_cont_diff_within_at.dist (hf : times_cont_diff_within_at ℝ n f s x) (hg : times_cont_diff_within_at ℝ n g s x) (hne : f x ≠ g x) : times_cont_diff_within_at ℝ n (λ y, dist (f y) (g y)) s x := by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) } lemma times_cont_diff_on.norm_sq (hf : times_cont_diff_on ℝ n f s) : times_cont_diff_on ℝ n (λ y, ∥f y∥ ^ 2) s := (λ x hx, (hf x hx).norm_sq) lemma times_cont_diff_on.norm (hf : times_cont_diff_on ℝ n f s) (h0 : ∀ x ∈ s, f x ≠ 0) : times_cont_diff_on ℝ n (λ y, ∥f y∥) s := λ x hx, (hf x hx).norm (h0 x hx) lemma times_cont_diff_on.dist (hf : times_cont_diff_on ℝ n f s) (hg : times_cont_diff_on ℝ n g s) (hne : ∀ x ∈ s, f x ≠ g x) : times_cont_diff_on ℝ n (λ y, dist (f y) (g y)) s := λ x hx, (hf x hx).dist (hg x hx) (hne x hx) lemma times_cont_diff.norm (hf : times_cont_diff ℝ n f) (h0 : ∀ x, f x ≠ 0) : times_cont_diff ℝ n (λ y, ∥f y∥) := times_cont_diff_iff_times_cont_diff_at.2 $ λ x, hf.times_cont_diff_at.norm (h0 x) lemma times_cont_diff.dist (hf : times_cont_diff ℝ n f) (hg : times_cont_diff ℝ n g) (hne : ∀ x, f x ≠ g x) : times_cont_diff ℝ n (λ y, dist (f y) (g y)) := times_cont_diff_iff_times_cont_diff_at.2 $ λ x, hf.times_cont_diff_at.dist hg.times_cont_diff_at (hne x) lemma differentiable_at.norm_sq (hf : differentiable_at ℝ f x) : differentiable_at ℝ (λ y, ∥f y∥ ^ 2) x := (times_cont_diff_at_id.norm_sq.differentiable_at le_rfl).comp x hf lemma differentiable_at.norm (hf : differentiable_at ℝ f x) (h0 : f x ≠ 0) : differentiable_at ℝ (λ y, ∥f y∥) x := ((times_cont_diff_at_norm h0).differentiable_at le_rfl).comp x hf lemma differentiable_at.dist (hf : differentiable_at ℝ f x) (hg : differentiable_at ℝ g x) (hne : f x ≠ g x) : differentiable_at ℝ (λ y, dist (f y) (g y)) x := by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) } lemma differentiable.norm_sq (hf : differentiable ℝ f) : differentiable ℝ (λ y, ∥f y∥ ^ 2) := λ x, (hf x).norm_sq lemma differentiable.norm (hf : differentiable ℝ f) (h0 : ∀ x, f x ≠ 0) : differentiable ℝ (λ y, ∥f y∥) := λ x, (hf x).norm (h0 x) lemma differentiable.dist (hf : differentiable ℝ f) (hg : differentiable ℝ g) (hne : ∀ x, f x ≠ g x) : differentiable ℝ (λ y, dist (f y) (g y)) := λ x, (hf x).dist (hg x) (hne x) lemma differentiable_within_at.norm_sq (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ y, ∥f y∥ ^ 2) s x := (times_cont_diff_at_id.norm_sq.differentiable_at le_rfl).comp_differentiable_within_at x hf lemma differentiable_within_at.norm (hf : differentiable_within_at ℝ f s x) (h0 : f x ≠ 0) : differentiable_within_at ℝ (λ y, ∥f y∥) s x := ((times_cont_diff_at_id.norm h0).differentiable_at le_rfl).comp_differentiable_within_at x hf lemma differentiable_within_at.dist (hf : differentiable_within_at ℝ f s x) (hg : differentiable_within_at ℝ g s x) (hne : f x ≠ g x) : differentiable_within_at ℝ (λ y, dist (f y) (g y)) s x := by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) } lemma differentiable_on.norm_sq (hf : differentiable_on ℝ f s) : differentiable_on ℝ (λ y, ∥f y∥ ^ 2) s := λ x hx, (hf x hx).norm_sq lemma differentiable_on.norm (hf : differentiable_on ℝ f s) (h0 : ∀ x ∈ s, f x ≠ 0) : differentiable_on ℝ (λ y, ∥f y∥) s := λ x hx, (hf x hx).norm (h0 x hx) lemma differentiable_on.dist (hf : differentiable_on ℝ f s) (hg : differentiable_on ℝ g s) (hne : ∀ x ∈ s, f x ≠ g x) : differentiable_on ℝ (λ y, dist (f y) (g y)) s := λ x hx, (hf x hx).dist (hg x hx) (hne x hx) end deriv section continuous /-! ### Continuity of the inner product Since the inner product is `ℝ`-smooth, it is continuous. We do not need a `[normed_space ℝ E]` structure to *state* this fact and its corollaries, so we introduce them in the proof instead. -/ lemma continuous_inner : continuous (λ p : E × E, ⟪p.1, p.2⟫) := begin letI : inner_product_space ℝ E := inner_product_space.is_R_or_C_to_real 𝕜 E, letI : is_scalar_tower ℝ 𝕜 E := restrict_scalars.is_scalar_tower _ _ _, exact differentiable_inner.continuous end variables {α : Type*} lemma filter.tendsto.inner {f g : α → E} {l : filter α} {x y : E} (hf : tendsto f l (𝓝 x)) (hg : tendsto g l (𝓝 y)) : tendsto (λ t, ⟪f t, g t⟫) l (𝓝 ⟪x, y⟫) := (continuous_inner.tendsto _).comp (hf.prod_mk_nhds hg) variables [topological_space α] {f g : α → E} {x : α} {s : set α} include 𝕜 lemma continuous_within_at.inner (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (λ t, ⟪f t, g t⟫) s x := hf.inner hg lemma continuous_at.inner (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (λ t, ⟪f t, g t⟫) x := hf.inner hg lemma continuous_on.inner (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λ t, ⟪f t, g t⟫) s := λ x hx, (hf x hx).inner (hg x hx) lemma continuous.inner (hf : continuous f) (hg : continuous g) : continuous (λ t, ⟪f t, g t⟫) := continuous_iff_continuous_at.2 $ λ x, hf.continuous_at.inner hg.continuous_at end continuous /-! ### Orthogonal projection in inner product spaces -/ section orthogonal open filter /-- Existence of minimizers Let `u` be a point in a real inner product space, and let `K` be a nonempty complete convex subset. Then there exists a (unique) `v` in `K` that minimizes the distance `∥u - v∥` to `u`. -/ -- FIXME this monolithic proof causes a deterministic timeout with `-T50000` -- It should be broken in a sequence of more manageable pieces, -- perhaps with individual statements for the three steps below. theorem exists_norm_eq_infi_of_complete_convex {K : set F} (ne : K.nonempty) (h₁ : is_complete K) (h₂ : convex K) : ∀ u : F, ∃ v ∈ K, ∥u - v∥ = ⨅ w : K, ∥u - w∥ := assume u, begin let δ := ⨅ w : K, ∥u - w∥, letI : nonempty K := ne.to_subtype, have zero_le_δ : 0 ≤ δ := le_cinfi (λ _, norm_nonneg _), have δ_le : ∀ w : K, δ ≤ ∥u - w∥, from cinfi_le ⟨0, set.forall_range_iff.2 $ λ _, norm_nonneg _⟩, have δ_le' : ∀ w ∈ K, δ ≤ ∥u - w∥ := assume w hw, δ_le ⟨w, hw⟩, -- Step 1: since `δ` is the infimum, can find a sequence `w : ℕ → K` in `K` -- such that `∥u - w n∥ < δ + 1 / (n + 1)` (which implies `∥u - w n∥ --> δ`); -- maybe this should be a separate lemma have exists_seq : ∃ w : ℕ → K, ∀ n, ∥u - w n∥ < δ + 1 / (n + 1), { have hδ : ∀n:ℕ, δ < δ + 1 / (n + 1), from λ n, lt_add_of_le_of_pos (le_refl _) nat.one_div_pos_of_nat, have h := λ n, exists_lt_of_cinfi_lt (hδ n), let w : ℕ → K := λ n, classical.some (h n), exact ⟨w, λ n, classical.some_spec (h n)⟩ }, rcases exists_seq with ⟨w, hw⟩, have norm_tendsto : tendsto (λ n, ∥u - w n∥) at_top (nhds δ), { have h : tendsto (λ n:ℕ, δ) at_top (nhds δ) := tendsto_const_nhds, have h' : tendsto (λ n:ℕ, δ + 1 / (n + 1)) at_top (nhds δ), { convert h.add tendsto_one_div_add_at_top_nhds_0_nat, simp only [add_zero] }, exact tendsto_of_tendsto_of_tendsto_of_le_of_le h h' (λ x, δ_le _) (λ x, le_of_lt (hw _)) }, -- Step 2: Prove that the sequence `w : ℕ → K` is a Cauchy sequence have seq_is_cauchy : cauchy_seq (λ n, ((w n):F)), { rw cauchy_seq_iff_le_tendsto_0, -- splits into three goals let b := λ n:ℕ, (8 * δ * (1/(n+1)) + 4 * (1/(n+1)) * (1/(n+1))), use (λn, sqrt (b n)), split, -- first goal : `∀ (n : ℕ), 0 ≤ sqrt (b n)` assume n, exact sqrt_nonneg _, split, -- second goal : `∀ (n m N : ℕ), N ≤ n → N ≤ m → dist ↑(w n) ↑(w m) ≤ sqrt (b N)` assume p q N hp hq, let wp := ((w p):F), let wq := ((w q):F), let a := u - wq, let b := u - wp, let half := 1 / (2:ℝ), let div := 1 / ((N:ℝ) + 1), have : 4 * ∥u - half • (wq + wp)∥ * ∥u - half • (wq + wp)∥ + ∥wp - wq∥ * ∥wp - wq∥ = 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) := calc 4 * ∥u - half•(wq + wp)∥ * ∥u - half•(wq + wp)∥ + ∥wp - wq∥ * ∥wp - wq∥ = (2*∥u - half•(wq + wp)∥) * (2 * ∥u - half•(wq + wp)∥) + ∥wp-wq∥*∥wp-wq∥ : by ring ... = (absR ((2:ℝ)) * ∥u - half•(wq + wp)∥) * (absR ((2:ℝ)) * ∥u - half•(wq+wp)∥) + ∥wp-wq∥*∥wp-wq∥ : by { rw _root_.abs_of_nonneg, exact zero_le_two } ... = ∥(2:ℝ) • (u - half • (wq + wp))∥ * ∥(2:ℝ) • (u - half • (wq + wp))∥ + ∥wp-wq∥ * ∥wp-wq∥ : by simp [norm_smul] ... = ∥a + b∥ * ∥a + b∥ + ∥a - b∥ * ∥a - b∥ : begin rw [smul_sub, smul_smul, mul_one_div_cancel (_root_.two_ne_zero : (2 : ℝ) ≠ 0), ← one_add_one_eq_two, add_smul], simp only [one_smul], have eq₁ : wp - wq = a - b, from (sub_sub_sub_cancel_left _ _ _).symm, have eq₂ : u + u - (wq + wp) = a + b, show u + u - (wq + wp) = (u - wq) + (u - wp), abel, rw [eq₁, eq₂], end ... = 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) : parallelogram_law_with_norm, have eq : δ ≤ ∥u - half • (wq + wp)∥, { rw smul_add, apply δ_le', apply h₂, repeat {exact subtype.mem _}, repeat {exact le_of_lt one_half_pos}, exact add_halves 1 }, have eq₁ : 4 * δ * δ ≤ 4 * ∥u - half • (wq + wp)∥ * ∥u - half • (wq + wp)∥, { mono, mono, norm_num, apply mul_nonneg, norm_num, exact norm_nonneg _ }, have eq₂ : ∥a∥ * ∥a∥ ≤ (δ + div) * (δ + div) := mul_self_le_mul_self (norm_nonneg _) (le_trans (le_of_lt $ hw q) (add_le_add_left (nat.one_div_le_one_div hq) _)), have eq₂' : ∥b∥ * ∥b∥ ≤ (δ + div) * (δ + div) := mul_self_le_mul_self (norm_nonneg _) (le_trans (le_of_lt $ hw p) (add_le_add_left (nat.one_div_le_one_div hp) _)), rw dist_eq_norm, apply nonneg_le_nonneg_of_sq_le_sq, { exact sqrt_nonneg _ }, rw mul_self_sqrt, exact calc ∥wp - wq∥ * ∥wp - wq∥ = 2 * (∥a∥*∥a∥ + ∥b∥*∥b∥) - 4 * ∥u - half • (wq+wp)∥ * ∥u - half • (wq+wp)∥ : by { rw ← this, simp } ... ≤ 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) - 4 * δ * δ : sub_le_sub_left eq₁ _ ... ≤ 2 * ((δ + div) * (δ + div) + (δ + div) * (δ + div)) - 4 * δ * δ : sub_le_sub_right (mul_le_mul_of_nonneg_left (add_le_add eq₂ eq₂') (by norm_num)) _ ... = 8 * δ * div + 4 * div * div : by ring, exact add_nonneg (mul_nonneg (mul_nonneg (by norm_num) zero_le_δ) (le_of_lt nat.one_div_pos_of_nat)) (mul_nonneg (mul_nonneg (by norm_num) nat.one_div_pos_of_nat.le) nat.one_div_pos_of_nat.le), -- third goal : `tendsto (λ (n : ℕ), sqrt (b n)) at_top (𝓝 0)` apply tendsto.comp, { convert continuous_sqrt.continuous_at, exact sqrt_zero.symm }, have eq₁ : tendsto (λ (n : ℕ), 8 * δ * (1 / (n + 1))) at_top (nhds (0:ℝ)), { convert (@tendsto_const_nhds _ _ _ (8 * δ) _).mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, have : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1))) at_top (nhds (0:ℝ)), { convert (@tendsto_const_nhds _ _ _ (4:ℝ) _).mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, have eq₂ : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1)) * (1 / (n + 1))) at_top (nhds (0:ℝ)), { convert this.mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, convert eq₁.add eq₂, simp only [add_zero] }, -- Step 3: By completeness of `K`, let `w : ℕ → K` converge to some `v : K`. -- Prove that it satisfies all requirements. rcases cauchy_seq_tendsto_of_is_complete h₁ (λ n, _) seq_is_cauchy with ⟨v, hv, w_tendsto⟩, use v, use hv, have h_cont : continuous (λ v, ∥u - v∥) := continuous.comp continuous_norm (continuous.sub continuous_const continuous_id), have : tendsto (λ n, ∥u - w n∥) at_top (nhds ∥u - v∥), convert (tendsto.comp h_cont.continuous_at w_tendsto), exact tendsto_nhds_unique this norm_tendsto, exact subtype.mem _ end /-- Characterization of minimizers for the projection on a convex set in a real inner product space. -/ theorem norm_eq_infi_iff_real_inner_le_zero {K : set F} (h : convex K) {u : F} {v : F} (hv : v ∈ K) : ∥u - v∥ = (⨅ w : K, ∥u - w∥) ↔ ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0 := iff.intro begin assume eq w hw, let δ := ⨅ w : K, ∥u - w∥, let p := ⟪u - v, w - v⟫_ℝ, let q := ∥w - v∥^2, letI : nonempty K := ⟨⟨v, hv⟩⟩, have zero_le_δ : 0 ≤ δ, apply le_cinfi, intro, exact norm_nonneg _, have δ_le : ∀ w : K, δ ≤ ∥u - w∥, assume w, apply cinfi_le, use (0:ℝ), rintros _ ⟨_, rfl⟩, exact norm_nonneg _, have δ_le' : ∀ w ∈ K, δ ≤ ∥u - w∥ := assume w hw, δ_le ⟨w, hw⟩, have : ∀θ:ℝ, 0 < θ → θ ≤ 1 → 2 * p ≤ θ * q, assume θ hθ₁ hθ₂, have : ∥u - v∥^2 ≤ ∥u - v∥^2 - 2 * θ * ⟪u - v, w - v⟫_ℝ + θ*θ*∥w - v∥^2 := calc ∥u - v∥^2 ≤ ∥u - (θ•w + (1-θ)•v)∥^2 : begin simp only [sq], apply mul_self_le_mul_self (norm_nonneg _), rw [eq], apply δ_le', apply h hw hv, exacts [le_of_lt hθ₁, sub_nonneg.2 hθ₂, add_sub_cancel'_right _ _], end ... = ∥(u - v) - θ • (w - v)∥^2 : begin have : u - (θ•w + (1-θ)•v) = (u - v) - θ • (w - v), { rw [smul_sub, sub_smul, one_smul], simp only [sub_eq_add_neg, add_comm, add_left_comm, add_assoc, neg_add_rev] }, rw this end ... = ∥u - v∥^2 - 2 * θ * inner (u - v) (w - v) + θ*θ*∥w - v∥^2 : begin rw [norm_sub_sq, inner_smul_right, norm_smul], simp only [sq], show ∥u-v∥*∥u-v∥-2*(θ*inner(u-v)(w-v))+absR (θ)*∥w-v∥*(absR (θ)*∥w-v∥)= ∥u-v∥*∥u-v∥-2*θ*inner(u-v)(w-v)+θ*θ*(∥w-v∥*∥w-v∥), rw abs_of_pos hθ₁, ring end, have eq₁ : ∥u-v∥^2-2*θ*inner(u-v)(w-v)+θ*θ*∥w-v∥^2=∥u-v∥^2+(θ*θ*∥w-v∥^2-2*θ*inner(u-v)(w-v)), by abel, rw [eq₁, le_add_iff_nonneg_right] at this, have eq₂ : θ*θ*∥w-v∥^2-2*θ*inner(u-v)(w-v)=θ*(θ*∥w-v∥^2-2*inner(u-v)(w-v)), ring, rw eq₂ at this, have := le_of_sub_nonneg (nonneg_of_mul_nonneg_left this hθ₁), exact this, by_cases hq : q = 0, { rw hq at this, have : p ≤ 0, have := this (1:ℝ) (by norm_num) (by norm_num), linarith, exact this }, { have q_pos : 0 < q, apply lt_of_le_of_ne, exact sq_nonneg _, intro h, exact hq h.symm, by_contradiction hp, rw not_le at hp, let θ := min (1:ℝ) (p / q), have eq₁ : θ*q ≤ p := calc θ*q ≤ (p/q) * q : mul_le_mul_of_nonneg_right (min_le_right _ _) (sq_nonneg _) ... = p : div_mul_cancel _ hq, have : 2 * p ≤ p := calc 2 * p ≤ θ*q : by { refine this θ (lt_min (by norm_num) (div_pos hp q_pos)) (by norm_num) } ... ≤ p : eq₁, linarith } end begin assume h, letI : nonempty K := ⟨⟨v, hv⟩⟩, apply le_antisymm, { apply le_cinfi, assume w, apply nonneg_le_nonneg_of_sq_le_sq (norm_nonneg _), have := h w w.2, exact calc ∥u - v∥ * ∥u - v∥ ≤ ∥u - v∥ * ∥u - v∥ - 2 * inner (u - v) ((w:F) - v) : by linarith ... ≤ ∥u - v∥^2 - 2 * inner (u - v) ((w:F) - v) + ∥(w:F) - v∥^2 : by { rw sq, refine le_add_of_nonneg_right _, exact sq_nonneg _ } ... = ∥(u - v) - (w - v)∥^2 : norm_sub_sq.symm ... = ∥u - w∥ * ∥u - w∥ : by { have : (u - v) - (w - v) = u - w, abel, rw [this, sq] } }, { show (⨅ (w : K), ∥u - w∥) ≤ (λw:K, ∥u - w∥) ⟨v, hv⟩, apply cinfi_le, use 0, rintros y ⟨z, rfl⟩, exact norm_nonneg _ } end variables (K : submodule 𝕜 E) /-- Existence of projections on complete subspaces. Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace. Then there exists a (unique) `v` in `K` that minimizes the distance `∥u - v∥` to `u`. This point `v` is usually called the orthogonal projection of `u` onto `K`. -/ theorem exists_norm_eq_infi_of_complete_subspace (h : is_complete (↑K : set E)) : ∀ u : E, ∃ v ∈ K, ∥u - v∥ = ⨅ w : (K : set E), ∥u - w∥ := begin letI : inner_product_space ℝ E := inner_product_space.is_R_or_C_to_real 𝕜 E, letI : module ℝ E := restrict_scalars.module ℝ 𝕜 E, letI : is_scalar_tower ℝ 𝕜 E := restrict_scalars.is_scalar_tower _ _ _, let K' : submodule ℝ E := submodule.restrict_scalars ℝ K, exact exists_norm_eq_infi_of_complete_convex ⟨0, K'.zero_mem⟩ h K'.convex end /-- Characterization of minimizers in the projection on a subspace, in the real case. Let `u` be a point in a real inner product space, and let `K` be a nonempty subspace. Then point `v` minimizes the distance `∥u - v∥` over points in `K` if and only if for all `w ∈ K`, `⟪u - v, w⟫ = 0` (i.e., `u - v` is orthogonal to the subspace `K`). This is superceded by `norm_eq_infi_iff_inner_eq_zero` that gives the same conclusion over any `is_R_or_C` field. -/ theorem norm_eq_infi_iff_real_inner_eq_zero (K : submodule ℝ F) {u : F} {v : F} (hv : v ∈ K) : ∥u - v∥ = (⨅ w : (↑K : set F), ∥u - w∥) ↔ ∀ w ∈ K, ⟪u - v, w⟫_ℝ = 0 := iff.intro begin assume h, have h : ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0, { rwa [norm_eq_infi_iff_real_inner_le_zero] at h, exacts [K.convex, hv] }, assume w hw, have le : ⟪u - v, w⟫_ℝ ≤ 0, let w' := w + v, have : w' ∈ K := submodule.add_mem _ hw hv, have h₁ := h w' this, have h₂ : w' - v = w, simp only [add_neg_cancel_right, sub_eq_add_neg], rw h₂ at h₁, exact h₁, have ge : ⟪u - v, w⟫_ℝ ≥ 0, let w'' := -w + v, have : w'' ∈ K := submodule.add_mem _ (submodule.neg_mem _ hw) hv, have h₁ := h w'' this, have h₂ : w'' - v = -w, simp only [neg_inj, add_neg_cancel_right, sub_eq_add_neg], rw [h₂, inner_neg_right] at h₁, linarith, exact le_antisymm le ge end begin assume h, have : ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0, assume w hw, let w' := w - v, have : w' ∈ K := submodule.sub_mem _ hw hv, have h₁ := h w' this, exact le_of_eq h₁, rwa norm_eq_infi_iff_real_inner_le_zero, exacts [submodule.convex _, hv] end /-- Characterization of minimizers in the projection on a subspace. Let `u` be a point in an inner product space, and let `K` be a nonempty subspace. Then point `v` minimizes the distance `∥u - v∥` over points in `K` if and only if for all `w ∈ K`, `⟪u - v, w⟫ = 0` (i.e., `u - v` is orthogonal to the subspace `K`) -/ theorem norm_eq_infi_iff_inner_eq_zero {u : E} {v : E} (hv : v ∈ K) : ∥u - v∥ = (⨅ w : (↑K : set E), ∥u - w∥) ↔ ∀ w ∈ K, ⟪u - v, w⟫ = 0 := begin letI : inner_product_space ℝ E := inner_product_space.is_R_or_C_to_real 𝕜 E, letI : module ℝ E := restrict_scalars.module ℝ 𝕜 E, letI : is_scalar_tower ℝ 𝕜 E := restrict_scalars.is_scalar_tower _ _ _, let K' : submodule ℝ E := K.restrict_scalars ℝ, split, { assume H, have A : ∀ w ∈ K, re ⟪u - v, w⟫ = 0 := (norm_eq_infi_iff_real_inner_eq_zero K' hv).1 H, assume w hw, apply ext, { simp [A w hw] }, { symmetry, calc im (0 : 𝕜) = 0 : im.map_zero ... = re ⟪u - v, (-I) • w⟫ : (A _ (K.smul_mem (-I) hw)).symm ... = re ((-I) * ⟪u - v, w⟫) : by rw inner_smul_right ... = im ⟪u - v, w⟫ : by simp } }, { assume H, have : ∀ w ∈ K', ⟪u - v, w⟫_ℝ = 0, { assume w hw, rw [real_inner_eq_re_inner, H w hw], exact zero_re' }, exact (norm_eq_infi_iff_real_inner_eq_zero K' hv).2 this } end section orthogonal_projection variables [complete_space K] /-- The orthogonal projection onto a complete subspace, as an unbundled function. This definition is only intended for use in setting up the bundled version `orthogonal_projection` and should not be used once that is defined. -/ def orthogonal_projection_fn (v : E) := (exists_norm_eq_infi_of_complete_subspace K (complete_space_coe_iff_is_complete.mp ‹_›) v).some variables {K} /-- The unbundled orthogonal projection is in the given subspace. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma orthogonal_projection_fn_mem (v : E) : orthogonal_projection_fn K v ∈ K := (exists_norm_eq_infi_of_complete_subspace K (complete_space_coe_iff_is_complete.mp ‹_›) v).some_spec.some /-- The characterization of the unbundled orthogonal projection. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma orthogonal_projection_fn_inner_eq_zero (v : E) : ∀ w ∈ K, ⟪v - orthogonal_projection_fn K v, w⟫ = 0 := begin rw ←norm_eq_infi_iff_inner_eq_zero K (orthogonal_projection_fn_mem v), exact (exists_norm_eq_infi_of_complete_subspace K (complete_space_coe_iff_is_complete.mp ‹_›) v).some_spec.some_spec end /-- The unbundled orthogonal projection is the unique point in `K` with the orthogonality property. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero {u v : E} (hvm : v ∈ K) (hvo : ∀ w ∈ K, ⟪u - v, w⟫ = 0) : orthogonal_projection_fn K u = v := begin rw [←sub_eq_zero, ←inner_self_eq_zero], have hvs : orthogonal_projection_fn K u - v ∈ K := submodule.sub_mem K (orthogonal_projection_fn_mem u) hvm, have huo : ⟪u - orthogonal_projection_fn K u, orthogonal_projection_fn K u - v⟫ = 0 := orthogonal_projection_fn_inner_eq_zero u _ hvs, have huv : ⟪u - v, orthogonal_projection_fn K u - v⟫ = 0 := hvo _ hvs, have houv : ⟪(u - v) - (u - orthogonal_projection_fn K u), orthogonal_projection_fn K u - v⟫ = 0, { rw [inner_sub_left, huo, huv, sub_zero] }, rwa sub_sub_sub_cancel_left at houv end variables (K) lemma orthogonal_projection_fn_norm_sq (v : E) : ∥v∥ * ∥v∥ = ∥v - (orthogonal_projection_fn K v)∥ * ∥v - (orthogonal_projection_fn K v)∥ + ∥orthogonal_projection_fn K v∥ * ∥orthogonal_projection_fn K v∥ := begin set p := orthogonal_projection_fn K v, have h' : ⟪v - p, p⟫ = 0, { exact orthogonal_projection_fn_inner_eq_zero _ _ (orthogonal_projection_fn_mem v) }, convert norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero (v - p) p h' using 2; simp, end /-- The orthogonal projection onto a complete subspace. -/ def orthogonal_projection : E →L[𝕜] K := linear_map.mk_continuous { to_fun := λ v, ⟨orthogonal_projection_fn K v, orthogonal_projection_fn_mem v⟩, map_add' := λ x y, begin have hm : orthogonal_projection_fn K x + orthogonal_projection_fn K y ∈ K := submodule.add_mem K (orthogonal_projection_fn_mem x) (orthogonal_projection_fn_mem y), have ho : ∀ w ∈ K, ⟪x + y - (orthogonal_projection_fn K x + orthogonal_projection_fn K y), w⟫ = 0, { intros w hw, rw [add_sub_comm, inner_add_left, orthogonal_projection_fn_inner_eq_zero _ w hw, orthogonal_projection_fn_inner_eq_zero _ w hw, add_zero] }, ext, simp [eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hm ho] end, map_smul' := λ c x, begin have hm : c • orthogonal_projection_fn K x ∈ K := submodule.smul_mem K _ (orthogonal_projection_fn_mem x), have ho : ∀ w ∈ K, ⟪c • x - c • orthogonal_projection_fn K x, w⟫ = 0, { intros w hw, rw [←smul_sub, inner_smul_left, orthogonal_projection_fn_inner_eq_zero _ w hw, mul_zero] }, ext, simp [eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hm ho] end } 1 (λ x, begin simp only [one_mul, linear_map.coe_mk], refine le_of_pow_le_pow 2 (norm_nonneg _) (by norm_num) _, change ∥orthogonal_projection_fn K x∥ ^ 2 ≤ ∥x∥ ^ 2, nlinarith [orthogonal_projection_fn_norm_sq K x] end) variables {K} @[simp] lemma orthogonal_projection_fn_eq (v : E) : orthogonal_projection_fn K v = (orthogonal_projection K v : E) := rfl /-- The characterization of the orthogonal projection. -/ @[simp] lemma orthogonal_projection_inner_eq_zero (v : E) : ∀ w ∈ K, ⟪v - orthogonal_projection K v, w⟫ = 0 := orthogonal_projection_fn_inner_eq_zero v /-- The orthogonal projection is the unique point in `K` with the orthogonality property. -/ lemma eq_orthogonal_projection_of_mem_of_inner_eq_zero {u v : E} (hvm : v ∈ K) (hvo : ∀ w ∈ K, ⟪u - v, w⟫ = 0) : (orthogonal_projection K u : E) = v := eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hvm hvo /-- The orthogonal projections onto equal subspaces are coerced back to the same point in `E`. -/ lemma eq_orthogonal_projection_of_eq_submodule {K' : submodule 𝕜 E} [complete_space K'] (h : K = K') (u : E) : (orthogonal_projection K u : E) = (orthogonal_projection K' u : E) := begin change orthogonal_projection_fn K u = orthogonal_projection_fn K' u, congr, exact h end /-- The orthogonal projection sends elements of `K` to themselves. -/ @[simp] lemma orthogonal_projection_mem_subspace_eq_self (v : K) : orthogonal_projection K v = v := by { ext, apply eq_orthogonal_projection_of_mem_of_inner_eq_zero; simp } local attribute [instance] finite_dimensional_bot /-- The orthogonal projection onto the trivial submodule is the zero map. -/ @[simp] lemma orthogonal_projection_bot : orthogonal_projection (⊥ : submodule 𝕜 E) = 0 := by ext variables (K) /-- The orthogonal projection has norm `≤ 1`. -/ lemma orthogonal_projection_norm_le : ∥orthogonal_projection K∥ ≤ 1 := linear_map.mk_continuous_norm_le _ (by norm_num) _ variables (𝕜) lemma smul_orthogonal_projection_singleton {v : E} (w : E) : (∥v∥ ^ 2 : 𝕜) • (orthogonal_projection (𝕜 ∙ v) w : E) = ⟪v, w⟫ • v := begin suffices : ↑(orthogonal_projection (𝕜 ∙ v) ((∥v∥ ^ 2 : 𝕜) • w)) = ⟪v, w⟫ • v, { simpa using this }, apply eq_orthogonal_projection_of_mem_of_inner_eq_zero, { rw submodule.mem_span_singleton, use ⟪v, w⟫ }, { intros x hx, obtain ⟨c, rfl⟩ := submodule.mem_span_singleton.mp hx, have hv : ↑∥v∥ ^ 2 = ⟪v, v⟫ := by { norm_cast, simp [norm_sq_eq_inner] }, simp [inner_sub_left, inner_smul_left, inner_smul_right, is_R_or_C.conj_div, mul_comm, hv, inner_product_space.conj_sym, hv] } end /-- Formula for orthogonal projection onto a single vector. -/ lemma orthogonal_projection_singleton {v : E} (w : E) : (orthogonal_projection (𝕜 ∙ v) w : E) = (⟪v, w⟫ / ∥v∥ ^ 2) • v := begin by_cases hv : v = 0, { rw [hv, eq_orthogonal_projection_of_eq_submodule submodule.span_zero_singleton], { simp }, { apply_instance } }, have hv' : ∥v∥ ≠ 0 := ne_of_gt (norm_pos_iff.mpr hv), have key : ((∥v∥ ^ 2 : 𝕜)⁻¹ * ∥v∥ ^ 2) • ↑(orthogonal_projection (𝕜 ∙ v) w) = ((∥v∥ ^ 2 : 𝕜)⁻¹ * ⟪v, w⟫) • v, { simp [mul_smul, smul_orthogonal_projection_singleton 𝕜 w] }, convert key; field_simp [hv'] end /-- Formula for orthogonal projection onto a single unit vector. -/ lemma orthogonal_projection_unit_singleton {v : E} (hv : ∥v∥ = 1) (w : E) : (orthogonal_projection (𝕜 ∙ v) w : E) = ⟪v, w⟫ • v := by { rw ← smul_orthogonal_projection_singleton 𝕜 w, simp [hv] } end orthogonal_projection /-- The subspace of vectors orthogonal to a given subspace. -/ def submodule.orthogonal : submodule 𝕜 E := { carrier := {v | ∀ u ∈ K, ⟪u, v⟫ = 0}, zero_mem' := λ _ _, inner_zero_right, add_mem' := λ x y hx hy u hu, by rw [inner_add_right, hx u hu, hy u hu, add_zero], smul_mem' := λ c x hx u hu, by rw [inner_smul_right, hx u hu, mul_zero] } notation K`ᗮ`:1200 := submodule.orthogonal K /-- When a vector is in `Kᗮ`. -/ lemma submodule.mem_orthogonal (v : E) : v ∈ Kᗮ ↔ ∀ u ∈ K, ⟪u, v⟫ = 0 := iff.rfl /-- When a vector is in `Kᗮ`, with the inner product the other way round. -/ lemma submodule.mem_orthogonal' (v : E) : v ∈ Kᗮ ↔ ∀ u ∈ K, ⟪v, u⟫ = 0 := by simp_rw [submodule.mem_orthogonal, inner_eq_zero_sym] variables {K} /-- A vector in `K` is orthogonal to one in `Kᗮ`. -/ lemma submodule.inner_right_of_mem_orthogonal {u v : E} (hu : u ∈ K) (hv : v ∈ Kᗮ) : ⟪u, v⟫ = 0 := (K.mem_orthogonal v).1 hv u hu /-- A vector in `Kᗮ` is orthogonal to one in `K`. -/ lemma submodule.inner_left_of_mem_orthogonal {u v : E} (hu : u ∈ K) (hv : v ∈ Kᗮ) : ⟪v, u⟫ = 0 := by rw [inner_eq_zero_sym]; exact submodule.inner_right_of_mem_orthogonal hu hv /-- A vector in `(𝕜 ∙ u)ᗮ` is orthogonal to `u`. -/ lemma inner_right_of_mem_orthogonal_singleton (u : E) {v : E} (hv : v ∈ (𝕜 ∙ u)ᗮ) : ⟪u, v⟫ = 0 := submodule.inner_right_of_mem_orthogonal (submodule.mem_span_singleton_self u) hv /-- A vector in `(𝕜 ∙ u)ᗮ` is orthogonal to `u`. -/ lemma inner_left_of_mem_orthogonal_singleton (u : E) {v : E} (hv : v ∈ (𝕜 ∙ u)ᗮ) : ⟪v, u⟫ = 0 := submodule.inner_left_of_mem_orthogonal (submodule.mem_span_singleton_self u) hv variables (K) /-- `K` and `Kᗮ` have trivial intersection. -/ lemma submodule.inf_orthogonal_eq_bot : K ⊓ Kᗮ = ⊥ := begin rw submodule.eq_bot_iff, intros x, rw submodule.mem_inf, exact λ ⟨hx, ho⟩, inner_self_eq_zero.1 (ho x hx) end /-- `K` and `Kᗮ` have trivial intersection. -/ lemma submodule.orthogonal_disjoint : disjoint K Kᗮ := by simp [disjoint_iff, K.inf_orthogonal_eq_bot] /-- `Kᗮ` can be characterized as the intersection of the kernels of the operations of inner product with each of the elements of `K`. -/ lemma orthogonal_eq_inter : Kᗮ = ⨅ v : K, (inner_right (v:E)).ker := begin apply le_antisymm, { rw le_infi_iff, rintros ⟨v, hv⟩ w hw, simpa using hw _ hv }, { intros v hv w hw, simp only [submodule.mem_infi] at hv, exact hv ⟨w, hw⟩ } end /-- The orthogonal complement of any submodule `K` is closed. -/ lemma submodule.is_closed_orthogonal : is_closed (Kᗮ : set E) := begin rw orthogonal_eq_inter K, convert is_closed_Inter (λ v : K, (inner_right (v:E)).is_closed_ker), simp end /-- In a complete space, the orthogonal complement of any submodule `K` is complete. -/ instance [complete_space E] : complete_space Kᗮ := K.is_closed_orthogonal.complete_space_coe variables (𝕜 E) /-- `submodule.orthogonal` gives a `galois_connection` between `submodule 𝕜 E` and its `order_dual`. -/ lemma submodule.orthogonal_gc : @galois_connection (submodule 𝕜 E) (order_dual $ submodule 𝕜 E) _ _ submodule.orthogonal submodule.orthogonal := λ K₁ K₂, ⟨λ h v hv u hu, submodule.inner_left_of_mem_orthogonal hv (h hu), λ h v hv u hu, submodule.inner_left_of_mem_orthogonal hv (h hu)⟩ variables {𝕜 E} /-- `submodule.orthogonal` reverses the `≤` ordering of two subspaces. -/ lemma submodule.orthogonal_le {K₁ K₂ : submodule 𝕜 E} (h : K₁ ≤ K₂) : K₂ᗮ ≤ K₁ᗮ := (submodule.orthogonal_gc 𝕜 E).monotone_l h /-- `submodule.orthogonal.orthogonal` preserves the `≤` ordering of two subspaces. -/ lemma submodule.orthogonal_orthogonal_monotone {K₁ K₂ : submodule 𝕜 E} (h : K₁ ≤ K₂) : K₁ᗮᗮ ≤ K₂ᗮᗮ := submodule.orthogonal_le (submodule.orthogonal_le h) /-- `K` is contained in `Kᗮᗮ`. -/ lemma submodule.le_orthogonal_orthogonal : K ≤ Kᗮᗮ := (submodule.orthogonal_gc 𝕜 E).le_u_l _ /-- The inf of two orthogonal subspaces equals the subspace orthogonal to the sup. -/ lemma submodule.inf_orthogonal (K₁ K₂ : submodule 𝕜 E) : K₁ᗮ ⊓ K₂ᗮ = (K₁ ⊔ K₂)ᗮ := (submodule.orthogonal_gc 𝕜 E).l_sup.symm /-- The inf of an indexed family of orthogonal subspaces equals the subspace orthogonal to the sup. -/ lemma submodule.infi_orthogonal {ι : Type*} (K : ι → submodule 𝕜 E) : (⨅ i, (K i)ᗮ) = (supr K)ᗮ := (submodule.orthogonal_gc 𝕜 E).l_supr.symm /-- The inf of a set of orthogonal subspaces equals the subspace orthogonal to the sup. -/ lemma submodule.Inf_orthogonal (s : set $ submodule 𝕜 E) : (⨅ K ∈ s, Kᗮ) = (Sup s)ᗮ := (submodule.orthogonal_gc 𝕜 E).l_Sup.symm /-- If `K₁` is complete and contained in `K₂`, `K₁` and `K₁ᗮ ⊓ K₂` span `K₂`. -/ lemma submodule.sup_orthogonal_inf_of_is_complete {K₁ K₂ : submodule 𝕜 E} (h : K₁ ≤ K₂) (hc : is_complete (K₁ : set E)) : K₁ ⊔ (K₁ᗮ ⊓ K₂) = K₂ := begin ext x, rw submodule.mem_sup, rcases exists_norm_eq_infi_of_complete_subspace K₁ hc x with ⟨v, hv, hvm⟩, rw norm_eq_infi_iff_inner_eq_zero K₁ hv at hvm, split, { rintro ⟨y, hy, z, hz, rfl⟩, exact K₂.add_mem (h hy) hz.2 }, { exact λ hx, ⟨v, hv, x - v, ⟨(K₁.mem_orthogonal' _).2 hvm, K₂.sub_mem hx (h hv)⟩, add_sub_cancel'_right _ _⟩ } end variables {K} /-- If `K` is complete, `K` and `Kᗮ` span the whole space. -/ lemma submodule.sup_orthogonal_of_is_complete (h : is_complete (K : set E)) : K ⊔ Kᗮ = ⊤ := begin convert submodule.sup_orthogonal_inf_of_is_complete (le_top : K ≤ ⊤) h, simp end /-- If `K` is complete, `K` and `Kᗮ` span the whole space. Version using `complete_space`. -/ lemma submodule.sup_orthogonal_of_complete_space [complete_space K] : K ⊔ Kᗮ = ⊤ := submodule.sup_orthogonal_of_is_complete (complete_space_coe_iff_is_complete.mp ‹_›) variables (K) /-- If `K` is complete, any `v` in `E` can be expressed as a sum of elements of `K` and `Kᗮ`. -/ lemma submodule.exists_sum_mem_mem_orthogonal [complete_space K] (v : E) : ∃ (y ∈ K) (z ∈ Kᗮ), v = y + z := begin have h_mem : v ∈ K ⊔ Kᗮ := by simp [submodule.sup_orthogonal_of_complete_space], obtain ⟨y, hy, z, hz, hyz⟩ := submodule.mem_sup.mp h_mem, exact ⟨y, hy, z, hz, hyz.symm⟩ end /-- If `K` is complete, then the orthogonal complement of its orthogonal complement is itself. -/ @[simp] lemma submodule.orthogonal_orthogonal [complete_space K] : Kᗮᗮ = K := begin ext v, split, { obtain ⟨y, hy, z, hz, rfl⟩ := K.exists_sum_mem_mem_orthogonal v, intros hv, have hz' : z = 0, { have hyz : ⟪z, y⟫ = 0 := by simp [hz y hy, inner_eq_zero_sym], simpa [inner_add_right, hyz] using hv z hz }, simp [hy, hz'] }, { intros hv w hw, rw inner_eq_zero_sym, exact hw v hv } end lemma submodule.orthogonal_orthogonal_eq_closure [complete_space E] : Kᗮᗮ = K.topological_closure := begin refine le_antisymm _ _, { convert submodule.orthogonal_orthogonal_monotone K.submodule_topological_closure, haveI : complete_space K.topological_closure := K.is_closed_topological_closure.complete_space_coe, rw K.topological_closure.orthogonal_orthogonal }, { exact K.topological_closure_minimal K.le_orthogonal_orthogonal Kᗮ.is_closed_orthogonal } end variables {K} /-- If `K` is complete, `K` and `Kᗮ` are complements of each other. -/ lemma submodule.is_compl_orthogonal_of_is_complete (h : is_complete (K : set E)) : is_compl K Kᗮ := ⟨K.orthogonal_disjoint, le_of_eq (submodule.sup_orthogonal_of_is_complete h).symm⟩ @[simp] lemma submodule.top_orthogonal_eq_bot : (⊤ : submodule 𝕜 E)ᗮ = ⊥ := begin ext, rw [submodule.mem_bot, submodule.mem_orthogonal], exact ⟨λ h, inner_self_eq_zero.mp (h x submodule.mem_top), by { rintro rfl, simp }⟩ end @[simp] lemma submodule.bot_orthogonal_eq_top : (⊥ : submodule 𝕜 E)ᗮ = ⊤ := begin rw [← submodule.top_orthogonal_eq_bot, eq_top_iff], exact submodule.le_orthogonal_orthogonal ⊤ end @[simp] lemma submodule.orthogonal_eq_bot_iff (hK : is_complete (K : set E)) : Kᗮ = ⊥ ↔ K = ⊤ := begin refine ⟨_, by { rintro rfl, exact submodule.top_orthogonal_eq_bot }⟩, intro h, have : K ⊔ Kᗮ = ⊤ := submodule.sup_orthogonal_of_is_complete hK, rwa [h, sup_comm, bot_sup_eq] at this, end @[simp] lemma submodule.orthogonal_eq_top_iff : Kᗮ = ⊤ ↔ K = ⊥ := begin refine ⟨_, by { rintro rfl, exact submodule.bot_orthogonal_eq_top }⟩, intro h, have : K ⊓ Kᗮ = ⊥ := K.orthogonal_disjoint.eq_bot, rwa [h, inf_comm, top_inf_eq] at this end /-- A point in `K` with the orthogonality property (here characterized in terms of `Kᗮ`) must be the orthogonal projection. -/ lemma eq_orthogonal_projection_of_mem_orthogonal [complete_space K] {u v : E} (hv : v ∈ K) (hvo : u - v ∈ Kᗮ) : (orthogonal_projection K u : E) = v := eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hv (λ w, inner_eq_zero_sym.mp ∘ (hvo w)) /-- A point in `K` with the orthogonality property (here characterized in terms of `Kᗮ`) must be the orthogonal projection. -/ lemma eq_orthogonal_projection_of_mem_orthogonal' [complete_space K] {u v z : E} (hv : v ∈ K) (hz : z ∈ Kᗮ) (hu : u = v + z) : (orthogonal_projection K u : E) = v := eq_orthogonal_projection_of_mem_orthogonal hv (by simpa [hu]) /-- The orthogonal projection onto `K` of an element of `Kᗮ` is zero. -/ lemma orthogonal_projection_mem_subspace_orthogonal_complement_eq_zero [complete_space K] {v : E} (hv : v ∈ Kᗮ) : orthogonal_projection K v = 0 := by { ext, convert eq_orthogonal_projection_of_mem_orthogonal _ _; simp [hv] } /-- The orthogonal projection onto `Kᗮ` of an element of `K` is zero. -/ lemma orthogonal_projection_mem_subspace_orthogonal_precomplement_eq_zero [complete_space E] {v : E} (hv : v ∈ K) : orthogonal_projection Kᗮ v = 0 := orthogonal_projection_mem_subspace_orthogonal_complement_eq_zero (K.le_orthogonal_orthogonal hv) /-- The orthogonal projection onto `(𝕜 ∙ v)ᗮ` of `v` is zero. -/ lemma orthogonal_projection_orthogonal_complement_singleton_eq_zero [complete_space E] (v : E) : orthogonal_projection (𝕜 ∙ v)ᗮ v = 0 := orthogonal_projection_mem_subspace_orthogonal_precomplement_eq_zero (submodule.mem_span_singleton_self v) variables (K) /-- In a complete space `E`, a vector splits as the sum of its orthogonal projections onto a complete submodule `K` and onto the orthogonal complement of `K`.-/ lemma eq_sum_orthogonal_projection_self_orthogonal_complement [complete_space E] [complete_space K] (w : E) : w = (orthogonal_projection K w : E) + (orthogonal_projection Kᗮ w : E) := begin obtain ⟨y, hy, z, hz, hwyz⟩ := K.exists_sum_mem_mem_orthogonal w, convert hwyz, { exact eq_orthogonal_projection_of_mem_orthogonal' hy hz hwyz }, { rw add_comm at hwyz, refine eq_orthogonal_projection_of_mem_orthogonal' hz _ hwyz, simp [hy] } end /-- In a complete space `E`, the projection maps onto a complete subspace `K` and its orthogonal complement sum to the identity. -/ lemma id_eq_sum_orthogonal_projection_self_orthogonal_complement [complete_space E] [complete_space K] : continuous_linear_map.id 𝕜 E = K.subtypeL.comp (orthogonal_projection K) + Kᗮ.subtypeL.comp (orthogonal_projection Kᗮ) := by { ext w, exact eq_sum_orthogonal_projection_self_orthogonal_complement K w } /-- The orthogonal projection is self-adjoint. -/ lemma inner_orthogonal_projection_left_eq_right [complete_space E] [complete_space K] (u v : E) : ⟪↑(orthogonal_projection K u), v⟫ = ⟪u, orthogonal_projection K v⟫ := begin nth_rewrite 0 eq_sum_orthogonal_projection_self_orthogonal_complement K v, nth_rewrite 1 eq_sum_orthogonal_projection_self_orthogonal_complement K u, rw [inner_add_left, inner_add_right, submodule.inner_right_of_mem_orthogonal (submodule.coe_mem (orthogonal_projection K u)) (submodule.coe_mem (orthogonal_projection Kᗮ v)), submodule.inner_left_of_mem_orthogonal (submodule.coe_mem (orthogonal_projection K v)) (submodule.coe_mem (orthogonal_projection Kᗮ u))], end open finite_dimensional /-- Given a finite-dimensional subspace `K₂`, and a subspace `K₁` containined in it, the dimensions of `K₁` and the intersection of its orthogonal subspace with `K₂` add to that of `K₂`. -/ lemma submodule.finrank_add_inf_finrank_orthogonal {K₁ K₂ : submodule 𝕜 E} [finite_dimensional 𝕜 K₂] (h : K₁ ≤ K₂) : finrank 𝕜 K₁ + finrank 𝕜 (K₁ᗮ ⊓ K₂ : submodule 𝕜 E) = finrank 𝕜 K₂ := begin haveI := submodule.finite_dimensional_of_le h, have hd := submodule.dim_sup_add_dim_inf_eq K₁ (K₁ᗮ ⊓ K₂), rw [←inf_assoc, (submodule.orthogonal_disjoint K₁).eq_bot, bot_inf_eq, finrank_bot, submodule.sup_orthogonal_inf_of_is_complete h (submodule.complete_of_finite_dimensional _)] at hd, rw add_zero at hd, exact hd.symm end /-- Given a finite-dimensional subspace `K₂`, and a subspace `K₁` containined in it, the dimensions of `K₁` and the intersection of its orthogonal subspace with `K₂` add to that of `K₂`. -/ lemma submodule.finrank_add_inf_finrank_orthogonal' {K₁ K₂ : submodule 𝕜 E} [finite_dimensional 𝕜 K₂] (h : K₁ ≤ K₂) {n : ℕ} (h_dim : finrank 𝕜 K₁ + n = finrank 𝕜 K₂) : finrank 𝕜 (K₁ᗮ ⊓ K₂ : submodule 𝕜 E) = n := by { rw ← add_right_inj (finrank 𝕜 K₁), simp [submodule.finrank_add_inf_finrank_orthogonal h, h_dim] } /-- Given a finite-dimensional space `E` and subspace `K`, the dimensions of `K` and `Kᗮ` add to that of `E`. -/ lemma submodule.finrank_add_finrank_orthogonal [finite_dimensional 𝕜 E] {K : submodule 𝕜 E} : finrank 𝕜 K + finrank 𝕜 Kᗮ = finrank 𝕜 E := begin convert submodule.finrank_add_inf_finrank_orthogonal (le_top : K ≤ ⊤) using 1, { rw inf_top_eq }, { simp } end /-- Given a finite-dimensional space `E` and subspace `K`, the dimensions of `K` and `Kᗮ` add to that of `E`. -/ lemma submodule.finrank_add_finrank_orthogonal' [finite_dimensional 𝕜 E] {K : submodule 𝕜 E} {n : ℕ} (h_dim : finrank 𝕜 K + n = finrank 𝕜 E) : finrank 𝕜 Kᗮ = n := by { rw ← add_right_inj (finrank 𝕜 K), simp [submodule.finrank_add_finrank_orthogonal, h_dim] } local attribute [instance] finite_dimensional_of_finrank_eq_succ /-- In a finite-dimensional inner product space, the dimension of the orthogonal complement of the span of a nonzero vector is one less than the dimension of the space. -/ lemma finrank_orthogonal_span_singleton {n : ℕ} [_i : fact (finrank 𝕜 E = n + 1)] {v : E} (hv : v ≠ 0) : finrank 𝕜 (𝕜 ∙ v)ᗮ = n := submodule.finrank_add_finrank_orthogonal' $ by simp [finrank_span_singleton hv, _i.elim, add_comm] end orthogonal section orthonormal_basis /-! ### Existence of Hilbert basis, orthonormal basis, etc. -/ variables {𝕜 E} {v : set E} open finite_dimensional submodule set /-- An orthonormal set in an `inner_product_space` is maximal, if and only if the orthogonal complement of its span is empty. -/ lemma maximal_orthonormal_iff_orthogonal_complement_eq_bot (hv : orthonormal 𝕜 (coe : v → E)) : (∀ u ⊇ v, orthonormal 𝕜 (coe : u → E) → u = v) ↔ (span 𝕜 v)ᗮ = ⊥ := begin rw submodule.eq_bot_iff, split, { contrapose!, -- ** direction 1: nonempty orthogonal complement implies nonmaximal rintros ⟨x, hx', hx⟩, -- take a nonzero vector and normalize it let e := (∥x∥⁻¹ : 𝕜) • x, have he : ∥e∥ = 1 := by simp [e, norm_smul_inv_norm hx], have he' : e ∈ (span 𝕜 v)ᗮ := smul_mem' _ _ hx', have he'' : e ∉ v, { intros hev, have : e = 0, { have : e ∈ (span 𝕜 v) ⊓ (span 𝕜 v)ᗮ := ⟨subset_span hev, he'⟩, simpa [(span 𝕜 v).inf_orthogonal_eq_bot] using this }, have : e ≠ 0 := hv.ne_zero ⟨e, hev⟩, contradiction }, -- put this together with `v` to provide a candidate orthonormal basis for the whole space refine ⟨v.insert e, v.subset_insert e, ⟨_, _⟩, (v.ne_insert_of_not_mem he'').symm⟩, { -- show that the elements of `v.insert e` have unit length rintros ⟨a, ha'⟩, cases eq_or_mem_of_mem_insert ha' with ha ha, { simp [ha, he] }, { exact hv.1 ⟨a, ha⟩ } }, { -- show that the elements of `v.insert e` are orthogonal have h_end : ∀ a ∈ v, ⟪a, e⟫ = 0, { intros a ha, exact he' a (submodule.subset_span ha) }, rintros ⟨a, ha'⟩, cases eq_or_mem_of_mem_insert ha' with ha ha, { rintros ⟨b, hb'⟩ hab', have hb : b ∈ v, { refine mem_of_mem_insert_of_ne hb' _, intros hbe', apply hab', simp [ha, hbe'] }, rw inner_eq_zero_sym, simpa [ha] using h_end b hb }, rintros ⟨b, hb'⟩ hab', cases eq_or_mem_of_mem_insert hb' with hb hb, { simpa [hb] using h_end a ha }, have : (⟨a, ha⟩ : v) ≠ ⟨b, hb⟩, { intros hab'', apply hab', simpa using hab'' }, exact hv.2 this } }, { -- ** direction 2: empty orthogonal complement implies maximal simp only [subset.antisymm_iff], rintros h u (huv : v ⊆ u) hu, refine ⟨_, huv⟩, intros x hxu, refine ((mt (h x)) (hu.ne_zero ⟨x, hxu⟩)).imp_symm _, intros hxv y hy, have hxv' : (⟨x, hxu⟩ : u) ∉ (coe ⁻¹' v : set u) := by simp [huv, hxv], obtain ⟨l, hl, rfl⟩ : ∃ l ∈ finsupp.supported 𝕜 𝕜 (coe ⁻¹' v : set u), (finsupp.total ↥u E 𝕜 coe) l = y, { rw ← finsupp.mem_span_image_iff_total, simp [huv, inter_eq_self_of_subset_left, hy] }, exact hu.inner_finsupp_eq_zero hxv' hl } end /-- An orthonormal set in an `inner_product_space` is maximal, if and only if the closure of its span is the whole space. -/ lemma maximal_orthonormal_iff_dense_span [complete_space E] (hv : orthonormal 𝕜 (coe : v → E)) : (∀ u ⊇ v, orthonormal 𝕜 (coe : u → E) → u = v) ↔ (span 𝕜 v).topological_closure = ⊤ := by rw [maximal_orthonormal_iff_orthogonal_complement_eq_bot hv, ← submodule.orthogonal_eq_top_iff, (span 𝕜 v).orthogonal_orthogonal_eq_closure] /-- Any orthonormal subset can be extended to an orthonormal set whose span is dense. -/ lemma exists_subset_is_orthonormal_dense_span [complete_space E] (hv : orthonormal 𝕜 (coe : v → E)) : ∃ u ⊇ v, orthonormal 𝕜 (coe : u → E) ∧ (span 𝕜 u).topological_closure = ⊤ := begin obtain ⟨u, hus, hu, hu_max⟩ := exists_maximal_orthonormal hv, rw maximal_orthonormal_iff_dense_span hu at hu_max, exact ⟨u, hus, hu, hu_max⟩ end variables (𝕜 E) /-- An inner product space admits an orthonormal set whose span is dense. -/ lemma exists_is_orthonormal_dense_span [complete_space E] : ∃ u : set E, orthonormal 𝕜 (coe : u → E) ∧ (span 𝕜 u).topological_closure = ⊤ := let ⟨u, hus, hu, hu_max⟩ := exists_subset_is_orthonormal_dense_span (orthonormal_empty 𝕜 E) in ⟨u, hu, hu_max⟩ variables {𝕜 E} /-- An orthonormal set in a finite-dimensional `inner_product_space` is maximal, if and only if it is a basis. -/ lemma maximal_orthonormal_iff_basis_of_finite_dimensional [finite_dimensional 𝕜 E] (hv : orthonormal 𝕜 (coe : v → E)) : (∀ u ⊇ v, orthonormal 𝕜 (coe : u → E) → u = v) ↔ ∃ b : basis v 𝕜 E, ⇑b = coe := begin rw maximal_orthonormal_iff_orthogonal_complement_eq_bot hv, have hv_compl : is_complete (span 𝕜 v : set E) := (span 𝕜 v).complete_of_finite_dimensional, rw submodule.orthogonal_eq_bot_iff hv_compl, have hv_coe : range (coe : v → E) = v := by simp, split, { refine λ h, ⟨basis.mk hv.linear_independent _, basis.coe_mk _ _⟩, convert h }, { rintros ⟨h, coe_h⟩, rw [← h.span_eq, coe_h, hv_coe] } end /-- In a finite-dimensional `inner_product_space`, any orthonormal subset can be extended to an orthonormal basis. -/ lemma exists_subset_is_orthonormal_basis [finite_dimensional 𝕜 E] (hv : orthonormal 𝕜 (coe : v → E)) : ∃ (u ⊇ v) (b : basis u 𝕜 E), orthonormal 𝕜 b ∧ ⇑b = coe := begin obtain ⟨u, hus, hu, hu_max⟩ := exists_maximal_orthonormal hv, obtain ⟨b, hb⟩ := (maximal_orthonormal_iff_basis_of_finite_dimensional hu).mp hu_max, exact ⟨u, hus, b, by rwa hb, hb⟩ end variables (𝕜 E) /-- Index for an arbitrary orthonormal basis on a finite-dimensional `inner_product_space`. -/ def orthonormal_basis_index [finite_dimensional 𝕜 E] : set E := classical.some (exists_subset_is_orthonormal_basis (orthonormal_empty 𝕜 E)) /-- A finite-dimensional `inner_product_space` has an orthonormal basis. -/ def orthonormal_basis [finite_dimensional 𝕜 E] : basis (orthonormal_basis_index 𝕜 E) 𝕜 E := (exists_subset_is_orthonormal_basis (orthonormal_empty 𝕜 E)).some_spec.some_spec.some lemma orthonormal_basis_orthonormal [finite_dimensional 𝕜 E] : orthonormal 𝕜 (orthonormal_basis 𝕜 E) := (exists_subset_is_orthonormal_basis (orthonormal_empty 𝕜 E)).some_spec.some_spec.some_spec.1 @[simp] lemma coe_orthonormal_basis [finite_dimensional 𝕜 E] : ⇑(orthonormal_basis 𝕜 E) = coe := (exists_subset_is_orthonormal_basis (orthonormal_empty 𝕜 E)).some_spec.some_spec.some_spec.2 instance [finite_dimensional 𝕜 E] : fintype (orthonormal_basis_index 𝕜 E) := is_noetherian.fintype_basis_index (orthonormal_basis 𝕜 E) variables {𝕜 E} /-- An `n`-dimensional `inner_product_space` has an orthonormal basis indexed by `fin n`. -/ def fin_orthonormal_basis [finite_dimensional 𝕜 E] {n : ℕ} (hn : finrank 𝕜 E = n) : basis (fin n) 𝕜 E := have h : fintype.card (orthonormal_basis_index 𝕜 E) = n, by rw [← finrank_eq_card_basis (orthonormal_basis 𝕜 E), hn], (orthonormal_basis 𝕜 E).reindex (fintype.equiv_fin_of_card_eq h) lemma fin_orthonormal_basis_orthonormal [finite_dimensional 𝕜 E] {n : ℕ} (hn : finrank 𝕜 E = n) : orthonormal 𝕜 (fin_orthonormal_basis hn) := suffices orthonormal 𝕜 (orthonormal_basis _ _ ∘ equiv.symm _), by { simp only [fin_orthonormal_basis, basis.coe_reindex], assumption }, -- why doesn't simpa work? (orthonormal_basis_orthonormal 𝕜 E).comp _ (equiv.injective _) end orthonormal_basis
98a857683d94a886191e976a84e26f6d146c81cc
37583bc2df4ea4f4e29b43ee1302f004c24cfa59
/tut5.lean
8b51566e7323d9d08250e0846d66e15a7ffebe10
[ "MIT" ]
permissive
Vtec234/tpil-solutions
9cd8576953eb7ea34e5959878bf00f73b035ba4e
70c1b5c5e33a9c26296edb17cf828327d7f69842
refs/heads/master
1,585,389,330,863
1,540,413,754,000
1,540,413,754,000
148,069,811
1
0
null
null
null
null
UTF-8
Lean
false
false
2,067
lean
import data.list.basic -- Go back to the exercises in Chapter 3 and Chapter 4 and redo as many as you can now -- with tactic proofs, using also rw and simp as appropriate. section ex1 -- Almost all of these can be proven by `safe` or `solve_iff`: meta def solve_iff : tactic unit := `[ apply iff.intro, repeat { safe } ] -- So I only left out the ones that couldn't. -- chapter 3 automated away section chapter4 -- ex. 1 automated away section ex2 variables (α : Type) (p q : α → Prop) variable r : Prop example : α → ((∀ x : α, r) ↔ r) := begin intro, apply iff.intro, safe, have : r, from a_1 a, -- is it fine to rely on auto-generated names? repeat { safe } end end ex2 section ex3 variables (men : Type) (barber : men) variable (shaves : men → men → Prop) example (h : ∀ x : men, shaves barber x ↔ ¬ shaves x x) : false := begin have h', from h barber, safe end end ex3 -- ex. 4 contains no proofs section ex5 variables (α : Type) (p q : α → Prop) variable a : α include a -- use non-emptiness of α in tactic proofs variable r : Prop example : r → (∃ x : α, r) := begin intro, apply exists.intro, repeat { assumption } end example : (∃ x, p x → r) ↔ (∀ x, p x) → r := begin apply iff.intro, safe, safe, show false, from a_2 a end example : (∃ x, r → p x) ↔ (r → ∃ x, p x) := begin apply iff.intro, safe, safe, show false, from a_2 a end end ex5 -- ex. 6 automated away -- ex. 7 automated away end chapter4 end ex1 -- Use tactic combinators to obtain a one line proof of the following: section ex2 example (p q r: Prop) (hp: p) : (p ∨ q ∨ r) ∧ (q ∨ p ∨ r) ∧ (q ∨ r ∨ p) := by simp * -- was i supposed to do something more low-level here? :) end ex2
9df72458350bef53ae23286f274d07d7d4a7dd63
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/analysis/special_functions/log/deriv.lean
dc732754a436dd9bb8b5292152e6db0b855a4e04
[ "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,637
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 -/ import analysis.special_functions.log.basic import analysis.special_functions.exp_deriv /-! # Derivative and series expansion of real logarithm In this file we prove that `real.log` is infinitely smooth at all nonzero `x : ℝ`. We also prove that the series `∑' n : ℕ, x ^ (n + 1) / (n + 1)` converges to `(-real.log (1 - x))` for all `x : ℝ`, `|x| < 1`. ## Tags logarithm, derivative -/ open filter finset set open_locale topological_space big_operators namespace real variables {x : ℝ} lemma has_strict_deriv_at_log_of_pos (hx : 0 < x) : has_strict_deriv_at log x⁻¹ x := have has_strict_deriv_at log (exp $ log x)⁻¹ x, from (has_strict_deriv_at_exp $ log x).of_local_left_inverse (continuous_at_log hx.ne') (ne_of_gt $ exp_pos _) $ eventually.mono (lt_mem_nhds hx) @exp_log, by rwa [exp_log hx] at this lemma has_strict_deriv_at_log (hx : x ≠ 0) : has_strict_deriv_at log x⁻¹ x := begin cases hx.lt_or_lt with hx hx, { convert (has_strict_deriv_at_log_of_pos (neg_pos.mpr hx)).comp x (has_strict_deriv_at_neg x), { ext y, exact (log_neg_eq_log y).symm }, { field_simp [hx.ne] } }, { exact has_strict_deriv_at_log_of_pos hx } end lemma has_deriv_at_log (hx : x ≠ 0) : has_deriv_at log x⁻¹ x := (has_strict_deriv_at_log hx).has_deriv_at lemma differentiable_at_log (hx : x ≠ 0) : differentiable_at ℝ log x := (has_deriv_at_log hx).differentiable_at lemma differentiable_on_log : differentiable_on ℝ log {0}ᶜ := λ x hx, (differentiable_at_log hx).differentiable_within_at @[simp] lemma differentiable_at_log_iff : differentiable_at ℝ log x ↔ x ≠ 0 := ⟨λ h, continuous_at_log_iff.1 h.continuous_at, differentiable_at_log⟩ lemma deriv_log (x : ℝ) : deriv log x = x⁻¹ := if hx : x = 0 then by rw [deriv_zero_of_not_differentiable_at (mt differentiable_at_log_iff.1 (not_not.2 hx)), hx, inv_zero] else (has_deriv_at_log hx).deriv @[simp] lemma deriv_log' : deriv log = has_inv.inv := funext deriv_log lemma cont_diff_on_log {n : with_top ℕ} : cont_diff_on ℝ n log {0}ᶜ := begin suffices : cont_diff_on ℝ ⊤ log {0}ᶜ, from this.of_le le_top, refine (cont_diff_on_top_iff_deriv_of_open is_open_compl_singleton).2 _, simp [differentiable_on_log, cont_diff_on_inv] end lemma cont_diff_at_log {n : with_top ℕ} : cont_diff_at ℝ n log x ↔ x ≠ 0 := ⟨λ h, continuous_at_log_iff.1 h.continuous_at, λ hx, (cont_diff_on_log x hx).cont_diff_at $ is_open.mem_nhds is_open_compl_singleton hx⟩ end real section log_differentiable open real section deriv variables {f : ℝ → ℝ} {x f' : ℝ} {s : set ℝ} lemma has_deriv_within_at.log (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) : has_deriv_within_at (λ y, log (f y)) (f' / (f x)) s x := begin rw div_eq_inv_mul, exact (has_deriv_at_log hx).comp_has_deriv_within_at x hf end lemma has_deriv_at.log (hf : has_deriv_at f f' x) (hx : f x ≠ 0) : has_deriv_at (λ y, log (f y)) (f' / f x) x := begin rw ← has_deriv_within_at_univ at *, exact hf.log hx end lemma has_strict_deriv_at.log (hf : has_strict_deriv_at f f' x) (hx : f x ≠ 0) : has_strict_deriv_at (λ y, log (f y)) (f' / f x) x := begin rw div_eq_inv_mul, exact (has_strict_deriv_at_log hx).comp x hf end lemma deriv_within.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, log (f x)) s x = (deriv_within f s x) / (f x) := (hf.has_deriv_within_at.log hx).deriv_within hxs @[simp] lemma deriv.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : deriv (λx, log (f x)) x = (deriv f x) / (f x) := (hf.has_deriv_at.log hx).deriv end deriv section fderiv variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] {f : E → ℝ} {x : E} {f' : E →L[ℝ] ℝ} {s : set E} lemma has_fderiv_within_at.log (hf : has_fderiv_within_at f f' s x) (hx : f x ≠ 0) : has_fderiv_within_at (λ x, log (f x)) ((f x)⁻¹ • f') s x := (has_deriv_at_log hx).comp_has_fderiv_within_at x hf lemma has_fderiv_at.log (hf : has_fderiv_at f f' x) (hx : f x ≠ 0) : has_fderiv_at (λ x, log (f x)) ((f x)⁻¹ • f') x := (has_deriv_at_log hx).comp_has_fderiv_at x hf lemma has_strict_fderiv_at.log (hf : has_strict_fderiv_at f f' x) (hx : f x ≠ 0) : has_strict_fderiv_at (λ x, log (f x)) ((f x)⁻¹ • f') x := (has_strict_deriv_at_log hx).comp_has_strict_fderiv_at x hf lemma differentiable_within_at.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) : differentiable_within_at ℝ (λx, log (f x)) s x := (hf.has_fderiv_within_at.log hx).differentiable_within_at @[simp] lemma differentiable_at.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : differentiable_at ℝ (λx, log (f x)) x := (hf.has_fderiv_at.log hx).differentiable_at lemma cont_diff_at.log {n} (hf : cont_diff_at ℝ n f x) (hx : f x ≠ 0) : cont_diff_at ℝ n (λ x, log (f x)) x := (cont_diff_at_log.2 hx).comp x hf lemma cont_diff_within_at.log {n} (hf : cont_diff_within_at ℝ n f s x) (hx : f x ≠ 0) : cont_diff_within_at ℝ n (λ x, log (f x)) s x := (cont_diff_at_log.2 hx).comp_cont_diff_within_at x hf lemma cont_diff_on.log {n} (hf : cont_diff_on ℝ n f s) (hs : ∀ x ∈ s, f x ≠ 0) : cont_diff_on ℝ n (λ x, log (f x)) s := λ x hx, (hf x hx).log (hs x hx) lemma cont_diff.log {n} (hf : cont_diff ℝ n f) (h : ∀ x, f x ≠ 0) : cont_diff ℝ n (λ x, log (f x)) := cont_diff_iff_cont_diff_at.2 $ λ x, hf.cont_diff_at.log (h x) lemma differentiable_on.log (hf : differentiable_on ℝ f s) (hx : ∀ x ∈ s, f x ≠ 0) : differentiable_on ℝ (λx, log (f x)) s := λx h, (hf x h).log (hx x h) @[simp] lemma differentiable.log (hf : differentiable ℝ f) (hx : ∀ x, f x ≠ 0) : differentiable ℝ (λx, log (f x)) := λx, (hf x).log (hx x) lemma fderiv_within.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : fderiv_within ℝ (λx, log (f x)) s x = (f x)⁻¹ • fderiv_within ℝ f s x := (hf.has_fderiv_within_at.log hx).fderiv_within hxs @[simp] lemma fderiv.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : fderiv ℝ (λx, log (f x)) x = (f x)⁻¹ • fderiv ℝ f x := (hf.has_fderiv_at.log hx).fderiv end fderiv end log_differentiable namespace real /-- The function `x * log (1 + t / x)` tends to `t` at `+∞`. -/ lemma tendsto_mul_log_one_plus_div_at_top (t : ℝ) : tendsto (λ x, x * log (1 + t / x)) at_top (𝓝 t) := begin have h₁ : tendsto (λ h, h⁻¹ * log (1 + t * h)) (𝓝[≠] 0) (𝓝 t), { simpa [has_deriv_at_iff_tendsto_slope, slope_fun_def] using (((has_deriv_at_id (0 : ℝ)).const_mul t).const_add 1).log (by simp) }, have h₂ : tendsto (λ x : ℝ, x⁻¹) at_top (𝓝[≠] 0) := tendsto_inv_at_top_zero'.mono_right (nhds_within_mono _ (λ x hx, (set.mem_Ioi.mp hx).ne')), simpa only [(∘), inv_inv] using h₁.comp h₂ end open_locale big_operators /-- A crude lemma estimating the difference between `log (1-x)` and its Taylor series at `0`, where the main point of the bound is that it tends to `0`. The goal is to deduce the series expansion of the logarithm, in `has_sum_pow_div_log_of_abs_lt_1`. -/ lemma abs_log_sub_add_sum_range_le {x : ℝ} (h : |x| < 1) (n : ℕ) : |((∑ i in range n, x^(i+1)/(i+1)) + log (1-x))| ≤ (|x|)^(n+1) / (1 - |x|) := begin /- For the proof, we show that the derivative of the function to be estimated is small, and then apply the mean value inequality. -/ let F : ℝ → ℝ := λ x, ∑ i in range n, x^(i+1)/(i+1) + log (1-x), -- First step: compute the derivative of `F` have A : ∀ y ∈ Ioo (-1 : ℝ) 1, deriv F y = - (y^n) / (1 - y), { assume y hy, have : (∑ i in range n, (↑i + 1) * y ^ i / (↑i + 1)) = (∑ i in range n, y ^ i), { congr' with i, exact mul_div_cancel_left _ (nat.cast_add_one_pos i).ne' }, field_simp [F, this, geom_sum_eq (ne_of_lt hy.2), sub_ne_zero_of_ne (ne_of_gt hy.2), sub_ne_zero_of_ne (ne_of_lt hy.2)], ring }, -- second step: show that the derivative of `F` is small have B : ∀ y ∈ Icc (-|x|) (|x|), |deriv F y| ≤ |x|^n / (1 - |x|), { assume y hy, have : y ∈ Ioo (-(1 : ℝ)) 1 := ⟨lt_of_lt_of_le (neg_lt_neg h) hy.1, lt_of_le_of_lt hy.2 h⟩, calc |deriv F y| = | -(y^n) / (1 - y)| : by rw [A y this] ... ≤ |x|^n / (1 - |x|) : begin have : |y| ≤ |x| := abs_le.2 hy, have : 0 < 1 - |x|, by linarith, have : 1 - |x| ≤ |1 - y| := le_trans (by linarith [hy.2]) (le_abs_self _), simp only [← pow_abs, abs_div, abs_neg], apply_rules [div_le_div, pow_nonneg, abs_nonneg, pow_le_pow_of_le_left] end }, -- third step: apply the mean value inequality have C : ∥F x - F 0∥ ≤ (|x|^n / (1 - |x|)) * ∥x - 0∥, { have : ∀ y ∈ Icc (- |x|) (|x|), differentiable_at ℝ F y, { assume y hy, have : 1 - y ≠ 0 := sub_ne_zero_of_ne (ne_of_gt (lt_of_le_of_lt hy.2 h)), simp [F, this] }, apply convex.norm_image_sub_le_of_norm_deriv_le this B (convex_Icc _ _) _ _, { simp }, { simp [le_abs_self x, neg_le.mp (neg_le_abs_self x)] } }, -- fourth step: conclude by massaging the inequality of the third step simpa [F, norm_eq_abs, div_mul_eq_mul_div, pow_succ'] using C end /-- Power series expansion of the logarithm around `1`. -/ theorem has_sum_pow_div_log_of_abs_lt_1 {x : ℝ} (h : |x| < 1) : has_sum (λ (n : ℕ), x ^ (n + 1) / (n + 1)) (-log (1 - x)) := begin rw summable.has_sum_iff_tendsto_nat, show tendsto (λ (n : ℕ), ∑ (i : ℕ) in range n, x ^ (i + 1) / (i + 1)) at_top (𝓝 (-log (1 - x))), { rw [tendsto_iff_norm_tendsto_zero], simp only [norm_eq_abs, sub_neg_eq_add], refine squeeze_zero (λ n, abs_nonneg _) (abs_log_sub_add_sum_range_le h) _, suffices : tendsto (λ (t : ℕ), |x| ^ (t + 1) / (1 - |x|)) at_top (𝓝 (|x| * 0 / (1 - |x|))), by simpa, simp only [pow_succ], refine (tendsto_const_nhds.mul _).div_const, exact tendsto_pow_at_top_nhds_0_of_lt_1 (abs_nonneg _) h }, show summable (λ (n : ℕ), x ^ (n + 1) / (n + 1)), { refine summable_of_norm_bounded _ (summable_geometric_of_lt_1 (abs_nonneg _) h) (λ i, _), calc ∥x ^ (i + 1) / (i + 1)∥ = |x| ^ (i + 1) / (i + 1) : begin have : (0 : ℝ) ≤ i + 1 := le_of_lt (nat.cast_add_one_pos i), rw [norm_eq_abs, abs_div, ← pow_abs, abs_of_nonneg this], end ... ≤ |x| ^ (i + 1) / (0 + 1) : begin apply_rules [div_le_div_of_le_left, pow_nonneg, abs_nonneg, add_le_add_right, i.cast_nonneg], norm_num, end ... ≤ |x| ^ i : by simpa [pow_succ'] using mul_le_of_le_one_right (pow_nonneg (abs_nonneg x) i) (le_of_lt h) } end /-- Power series expansion of `log(1 + x) - log(1 - x)` for `|x| < 1`. -/ lemma has_sum_log_sub_log_of_abs_lt_1 {x : ℝ} (h : |x| < 1) : has_sum (λ k : ℕ, (2 : ℝ) * (1 / (2 * k + 1)) * x ^ (2 * k + 1)) (log (1 + x) - log(1 - x)) := begin let term := λ n : ℕ, (-1) * ((-x) ^ (n + 1) / ((n : ℝ) + 1)) + x ^ (n + 1) / (n + 1), have h_term_eq_goal : term ∘ (*) 2 = λ k : ℕ, 2 * (1 / (2 * k + 1)) * x ^ (2 * k + 1), { ext n, dsimp [term], rw [odd.neg_pow (⟨n, rfl⟩ : odd (2 * n + 1)) x], push_cast, ring_nf, }, rw [← h_term_eq_goal, (nat.mul_right_injective two_pos).has_sum_iff], { have h₁ := (has_sum_pow_div_log_of_abs_lt_1 (eq.trans_lt (abs_neg x) h)).mul_left (-1), convert h₁.add (has_sum_pow_div_log_of_abs_lt_1 h), ring_nf }, { intros m hm, rw [range_two_mul, set.mem_set_of_eq, ← nat.even_add_one] at hm, dsimp [term], rw [even.neg_pow hm, neg_one_mul, neg_add_self] }, end /-- Expansion of `log (1 + a⁻¹)` as a series in powers of `1 / (2 * a + 1)`. -/ theorem has_sum_log_one_add_inv {a : ℝ} (h : 0 < a) : has_sum (λ k : ℕ, (2 : ℝ) * (1 / (2 * k + 1)) * (1 / (2 * a + 1)) ^ (2 * k + 1)) (log (1 + a⁻¹)) := begin have h₁ : |1 / (2 * a + 1)| < 1, { rw [abs_of_pos, div_lt_one], { linarith, }, { linarith, }, { exact div_pos one_pos (by linarith), }, }, convert has_sum_log_sub_log_of_abs_lt_1 h₁, have h₂ : (2 : ℝ) * a + 1 ≠ 0 := by linarith, have h₃ := h.ne', rw ← log_div, { congr, field_simp, linarith, }, { field_simp, linarith } , { field_simp }, end end real
c274a0d46bdd1fd6360642473d20970d3be9d6d6
5d166a16ae129621cb54ca9dde86c275d7d2b483
/library/data/list/set.lean
76df0eddcb3e25191dc8296b528765e39089bf90
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
7,886
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, Jeremy Avigad Set-like operations on lists. -/ universe variables uu vv variables {α : Type uu} {β : Type vv} namespace list /- disjoint -/ section disjoint def disjoint (l₁ l₂ : list α) : Prop := ∀ ⦃a⦄, (a ∈ l₁ → a ∈ l₂ → false) lemma disjoint_left {l₁ l₂ : list α} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₁ → a ∉ l₂ := λ d, d lemma disjoint_right {l₁ l₂ : list α} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₂ → a ∉ l₁ := λ d a i₂ i₁, d i₁ i₂ lemma disjoint.comm {l₁ l₂ : list α} : disjoint l₁ l₂ → disjoint l₂ l₁ := λ d a i₂ i₁, d i₁ i₂ lemma disjoint_of_subset_left {l₁ l₂ l : list α} : list.subset l₁ l → disjoint l l₂ → disjoint l₁ l₂ := λ ss d x xinl₁, d (ss xinl₁) lemma disjoint_of_subset_right {l₁ l₂ l : list α} : list.subset l₂ l → disjoint l₁ l → disjoint l₁ l₂ := λ ss d x xinl xinl₁, d xinl (ss xinl₁) lemma disjoint_of_disjoint_cons_left {a : α} {l₁ l₂} : disjoint (a::l₁) l₂ → disjoint l₁ l₂ := disjoint_of_subset_left (list.subset_cons _ _) lemma disjoint_of_disjoint_cons_right {a : α} {l₁ l₂} : disjoint l₁ (a::l₂) → disjoint l₁ l₂ := disjoint_of_subset_right (list.subset_cons _ _) lemma disjoint_nil_left (l : list α) : disjoint [] l := λ a ab, absurd ab (not_mem_nil a) lemma disjoint_nil_right (l : list α) : disjoint l [] := disjoint.comm (disjoint_nil_left l) lemma disjoint_cons_of_not_mem_of_disjoint {a : α} {l₁ l₂ : list α} : a ∉ l₂ → disjoint l₁ l₂ → disjoint (a::l₁) l₂ := λ nainl₂ d x (xinal₁ : x ∈ a::l₁), or.elim (eq_or_mem_of_mem_cons xinal₁) (λ xeqa : x = a, eq.symm xeqa ▸ nainl₂) (λ xinl₁ : x ∈ l₁, disjoint_left d xinl₁) lemma disjoint_append_of_disjoint_left {l₁ l₂ l : list α} : disjoint l₁ l → disjoint l₂ l → disjoint (l₁++l₂) l := λ d₁ d₂ x h, or.elim (mem_or_mem_of_mem_append h) (@d₁ x) (@d₂ x) lemma disjoint_of_disjoint_append_left_left {l₁ l₂ l : list α} : disjoint (l₁++l₂) l → disjoint l₁ l := disjoint_of_subset_left (list.subset_append_left _ _) lemma disjoint_of_disjoint_append_left_right {l₁ l₂ l : list α} : disjoint (l₁++l₂) l → disjoint l₂ l := disjoint_of_subset_left (list.subset_append_right _ _) lemma disjoint_of_disjoint_append_right_left {l₁ l₂ l : list α} : disjoint l (l₁++l₂) → disjoint l l₁ := disjoint_of_subset_right (list.subset_append_left _ _) lemma disjoint_of_disjoint_append_right_right {l₁ l₂ l : list α} : disjoint l (l₁++l₂) → disjoint l l₂ := disjoint_of_subset_right (list.subset_append_right _ _) end disjoint /- no duplicates predicate -/ inductive nodup {α : Type uu} : list α → Prop | ndnil : nodup [] | ndcons : ∀ {a : α} {l : list α}, a ∉ l → nodup l → nodup (a::l) section nodup open nodup theorem nodup_nil : @nodup α [] := ndnil theorem nodup_cons {a : α} {l : list α} : a ∉ l → nodup l → nodup (a::l) := λ i n, ndcons i n theorem nodup_singleton (a : α) : nodup [a] := nodup_cons (not_mem_nil a) nodup_nil theorem nodup_of_nodup_cons : ∀ {a : α} {l : list α}, nodup (a::l) → nodup l | a xs (ndcons i n) := n theorem not_mem_of_nodup_cons : ∀ {a : α} {l : list α}, nodup (a::l) → a ∉ l | a xs (ndcons i n) := i theorem not_nodup_cons_of_mem {a : α} {l : list α} : a ∈ l → ¬ nodup (a :: l) := λ ainl d, absurd ainl (not_mem_of_nodup_cons d) theorem nodup_of_sublist : Π {l₁ l₂ : list α}, l₁ <+ l₂ → nodup l₂ → nodup l₁ | ._ ._ sublist.slnil h := h | ._ ._ (sublist.cons l₁ l₂ a s) (ndcons i n) := nodup_of_sublist s n | ._ ._ (sublist.cons2 l₁ l₂ a s) (ndcons i n) := ndcons (λh, i (subset_of_sublist s h)) (nodup_of_sublist s n) theorem not_nodup_cons_of_not_nodup {a : α} {l : list α} : ¬ nodup l → ¬ nodup (a :: l) := mt nodup_of_nodup_cons theorem nodup_of_nodup_append_left {l₁ l₂ : list α} : nodup (l₁++l₂) → nodup l₁ := nodup_of_sublist (sublist_append_left l₁ l₂) theorem nodup_of_nodup_append_right : ∀ {l₁ l₂ : list α}, nodup (l₁++l₂) → nodup l₂ | [] l₂ n := n | (x::xs) l₂ n := nodup_of_nodup_append_right (nodup_of_nodup_cons n) theorem disjoint_of_nodup_append : ∀ {l₁ l₂ : list α}, nodup (l₁++l₂) → disjoint l₁ l₂ | [] l₂ d := disjoint_nil_left l₂ | (x::xs) l₂ d := have nodup (x::(xs++l₂)), from d, have x ∉ xs++l₂, from not_mem_of_nodup_cons this, have nxinl₂ : x ∉ l₂, from not_mem_of_not_mem_append_right this, take a, suppose a ∈ x::xs, or.elim (eq_or_mem_of_mem_cons this) (suppose a = x, eq.symm this ▸ nxinl₂) (suppose ainxs : a ∈ xs, have nodup (x::(xs++l₂)), from d, have nodup (xs++l₂), from nodup_of_nodup_cons this, have disjoint xs l₂, from disjoint_of_nodup_append this, disjoint_left this ainxs) theorem nodup_append_of_nodup_of_nodup_of_disjoint : ∀ {l₁ l₂ : list α}, nodup l₁ → nodup l₂ → disjoint l₁ l₂ → nodup (l₁++l₂) | [] l₂ d₁ d₂ dsj := begin rw [nil_append], exact d₂ end | (x::xs) l₂ d₁ d₂ dsj := have ndxs : nodup xs, from nodup_of_nodup_cons d₁, have disjoint xs l₂, from disjoint_of_disjoint_cons_left dsj, have ndxsl₂ : nodup (xs++l₂), from nodup_append_of_nodup_of_nodup_of_disjoint ndxs d₂ this, have nxinxs : x ∉ xs, from not_mem_of_nodup_cons d₁, have x ∉ l₂, from disjoint_left dsj (mem_cons_self x xs), have x ∉ xs++l₂, from not_mem_append nxinxs this, nodup_cons this ndxsl₂ theorem nodup_app_comm {l₁ l₂ : list α} (d : nodup (l₁++l₂)) : nodup (l₂++l₁) := have d₁ : nodup l₁, from nodup_of_nodup_append_left d, have d₂ : nodup l₂, from nodup_of_nodup_append_right d, have dsj : disjoint l₁ l₂, from disjoint_of_nodup_append d, nodup_append_of_nodup_of_nodup_of_disjoint d₂ d₁ (disjoint.comm dsj) theorem nodup_head {a : α} {l₁ l₂ : list α} (d : nodup (l₁++(a::l₂))) : nodup (a::(l₁++l₂)) := have d₁ : nodup (a::(l₂++l₁)), from nodup_app_comm d, have d₂ : nodup (l₂++l₁), from nodup_of_nodup_cons d₁, have d₃ : nodup (l₁++l₂), from nodup_app_comm d₂, have nain : a ∉ l₂++l₁, from not_mem_of_nodup_cons d₁, have nain₂ : a ∉ l₂, from not_mem_of_not_mem_append_left nain, have nain₁ : a ∉ l₁, from not_mem_of_not_mem_append_right nain, nodup_cons (not_mem_append nain₁ nain₂) d₃ theorem nodup_middle {a : α} {l₁ l₂ : list α} (d : nodup (a::(l₁++l₂))) : nodup (l₁++(a::l₂)) := have d₁ : nodup (l₁++l₂), from nodup_of_nodup_cons d, have nain : a ∉ l₁++l₂, from not_mem_of_nodup_cons d, have disj : disjoint l₁ l₂, from disjoint_of_nodup_append d₁, have d₂ : nodup l₁, from nodup_of_nodup_append_left d₁, have d₃ : nodup l₂, from nodup_of_nodup_append_right d₁, have nain₂ : a ∉ l₂, from not_mem_of_not_mem_append_right nain, have nain₁ : a ∉ l₁, from not_mem_of_not_mem_append_left nain, have d₄ : nodup (a::l₂), from nodup_cons nain₂ d₃, have disj₂ : disjoint l₁ (a::l₂), from disjoint.comm (disjoint_cons_of_not_mem_of_disjoint nain₁ (disjoint.comm disj)), nodup_append_of_nodup_of_nodup_of_disjoint d₂ d₄ disj₂ end nodup end list
9c510d0f420403cf90b5c8309ec8dd8c7fb95534
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/library/data/set/comm_semiring.lean
7d9345a2fe77d1f457acab19e534bc28f7f3ba8b
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
864
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura (set A) is an instance of a commutative semiring -/ import data.set.basic algebra.ring open algebra set definition set_comm_semiring [instance] (A : Type) : comm_semiring (set A) := ⦃ comm_semiring, add := union, mul := inter, zero := empty, one := univ, add_assoc := union.assoc, add_comm := union.comm, zero_add := empty_union, add_zero := union_empty, mul_assoc := inter.assoc, mul_comm := inter.comm, zero_mul := empty_inter, mul_zero := inter_empty, one_mul := univ_inter, mul_one := inter_univ, left_distrib := inter.distrib_left, right_distrib := inter.distrib_right ⦄
556680ab0dea17a2e0c4c7f264d286af5a389279
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/ring_theory/polynomial/content.lean
8a315f7d1b890e00c42f85b8a0d928a6814410d1
[ "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
17,561
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 algebra.gcd_monoid.finset import data.polynomial.field_division import data.polynomial.erase_lead import data.polynomial.cancel_leads /-! # GCD structures on polynomials Definitions and basic results about polynomials over GCD domains, particularly their contents and primitive polynomials. ## Main Definitions Let `p : polynomial R`. - `p.content` is the `gcd` of the coefficients of `p`. - `p.is_primitive` indicates that `p.content = 1`. ## Main Results - `polynomial.content_mul`: If `p q : polynomial R`, then `(p * q).content = p.content * q.content`. - `polynomial.normalized_gcd_monoid`: The polynomial ring of a GCD domain is itself a GCD domain. -/ namespace polynomial section primitive variables {R : Type*} [comm_semiring R] /-- A polynomial is primitive when the only constant polynomials dividing it are units -/ def is_primitive (p : polynomial R) : Prop := ∀ (r : R), C r ∣ p → is_unit r lemma is_primitive_iff_is_unit_of_C_dvd {p : polynomial R} : p.is_primitive ↔ ∀ (r : R), C r ∣ p → is_unit r := iff.rfl @[simp] lemma is_primitive_one : is_primitive (1 : polynomial R) := λ r h, is_unit_C.mp (is_unit_of_dvd_one (C r) h) lemma monic.is_primitive {p : polynomial R} (hp : p.monic) : p.is_primitive := begin rintros r ⟨q, h⟩, exact is_unit_of_mul_eq_one r (q.coeff p.nat_degree) (by rwa [←coeff_C_mul, ←h]), end lemma is_primitive.ne_zero [nontrivial R] {p : polynomial R} (hp : p.is_primitive) : p ≠ 0 := begin rintro rfl, exact (hp 0 (dvd_zero (C 0))).ne_zero rfl, end end primitive variables {R : Type*} [comm_ring R] [is_domain R] section normalized_gcd_monoid variable [normalized_gcd_monoid R] /-- `p.content` is the `gcd` of the coefficients of `p`. -/ def content (p : polynomial R) : R := (p.support).gcd p.coeff lemma content_dvd_coeff {p : polynomial R} (n : ℕ) : p.content ∣ p.coeff n := begin by_cases h : n ∈ p.support, { apply finset.gcd_dvd h }, rw [mem_support_iff, not_not] at h, rw h, apply dvd_zero, end @[simp] lemma content_C {r : R} : (C r).content = normalize r := begin rw content, by_cases h0 : r = 0, { simp [h0] }, have h : (C r).support = {0} := support_monomial _ _ h0, simp [h], end @[simp] lemma content_zero : content (0 : polynomial R) = 0 := by rw [← C_0, content_C, normalize_zero] @[simp] lemma content_one : content (1 : polynomial R) = 1 := by rw [← C_1, content_C, normalize_one] lemma content_X_mul {p : polynomial R} : content (X * p) = content p := begin rw [content, content, finset.gcd_def, finset.gcd_def], refine congr rfl _, have h : (X * p).support = p.support.map ⟨nat.succ, nat.succ_injective⟩, { ext a, simp only [exists_prop, finset.mem_map, function.embedding.coe_fn_mk, ne.def, mem_support_iff], cases a, { simp [coeff_X_mul_zero, nat.succ_ne_zero] }, rw [mul_comm, coeff_mul_X], split, { intro h, use a, simp [h] }, { rintros ⟨b, ⟨h1, h2⟩⟩, rw ← nat.succ_injective h2, apply h1 } }, rw h, simp only [finset.map_val, function.comp_app, function.embedding.coe_fn_mk, multiset.map_map], refine congr (congr rfl _) rfl, ext a, rw mul_comm, simp [coeff_mul_X], end @[simp] lemma content_X_pow {k : ℕ} : content ((X : polynomial R) ^ k) = 1 := begin induction k with k hi, { simp }, rw [pow_succ, content_X_mul, hi] end @[simp] lemma content_X : content (X : polynomial R) = 1 := by { rw [← mul_one X, content_X_mul, content_one] } lemma content_C_mul (r : R) (p : polynomial R) : (C r * p).content = normalize r * p.content := begin by_cases h0 : r = 0, { simp [h0] }, rw content, rw content, rw ← finset.gcd_mul_left, refine congr (congr rfl _) _; ext; simp [h0, mem_support_iff] end @[simp] lemma content_monomial {r : R} {k : ℕ} : content (monomial k r) = normalize r := by { rw [monomial_eq_C_mul_X, content_C_mul, content_X_pow, mul_one] } lemma content_eq_zero_iff {p : polynomial R} : content p = 0 ↔ p = 0 := begin rw [content, finset.gcd_eq_zero_iff], split; intro h, { ext n, by_cases h0 : n ∈ p.support, { rw [h n h0, coeff_zero], }, { rw mem_support_iff at h0, push_neg at h0, simp [h0] } }, { intros x h0, simp [h] } end @[simp] lemma normalize_content {p : polynomial R} : normalize p.content = p.content := finset.normalize_gcd lemma content_eq_gcd_range_of_lt (p : polynomial R) (n : ℕ) (h : p.nat_degree < n) : p.content = (finset.range n).gcd p.coeff := begin apply dvd_antisymm_of_normalize_eq normalize_content finset.normalize_gcd, { rw finset.dvd_gcd_iff, intros i hi, apply content_dvd_coeff _ }, { apply finset.gcd_mono, intro i, simp only [nat.lt_succ_iff, mem_support_iff, ne.def, finset.mem_range], contrapose!, intro h1, apply coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le h h1), } end lemma content_eq_gcd_range_succ (p : polynomial R) : p.content = (finset.range p.nat_degree.succ).gcd p.coeff := content_eq_gcd_range_of_lt _ _ (nat.lt_succ_self _) lemma content_eq_gcd_leading_coeff_content_erase_lead (p : polynomial R) : p.content = gcd_monoid.gcd p.leading_coeff (erase_lead p).content := begin by_cases h : p = 0, { simp [h] }, rw [← leading_coeff_eq_zero, leading_coeff, ← ne.def, ← mem_support_iff] at h, rw [content, ← finset.insert_erase h, finset.gcd_insert, leading_coeff, content, erase_lead_support], refine congr rfl (finset.gcd_congr rfl (λ i hi, _)), rw finset.mem_erase at hi, rw [erase_lead_coeff, if_neg hi.1], end lemma dvd_content_iff_C_dvd {p : polynomial R} {r : R} : r ∣ p.content ↔ C r ∣ p := begin rw C_dvd_iff_dvd_coeff, split, { intros h i, apply h.trans (content_dvd_coeff _) }, { intro h, rw [content, finset.dvd_gcd_iff], intros i hi, apply h i } end lemma C_content_dvd (p : polynomial R) : C p.content ∣ p := dvd_content_iff_C_dvd.1 dvd_rfl lemma is_primitive_iff_content_eq_one {p : polynomial R} : p.is_primitive ↔ p.content = 1 := begin rw [←normalize_content, normalize_eq_one, is_primitive], simp_rw [←dvd_content_iff_C_dvd], exact ⟨λ h, h p.content (dvd_refl p.content), λ h r hdvd, is_unit_of_dvd_unit hdvd h⟩, end lemma is_primitive.content_eq_one {p : polynomial R} (hp : p.is_primitive) : p.content = 1 := is_primitive_iff_content_eq_one.mp hp open_locale classical noncomputable theory section prim_part /-- The primitive part of a polynomial `p` is the primitive polynomial gained by dividing `p` by `p.content`. If `p = 0`, then `p.prim_part = 1`. -/ def prim_part (p : polynomial R) : polynomial R := if p = 0 then 1 else classical.some (C_content_dvd p) lemma eq_C_content_mul_prim_part (p : polynomial R) : p = C p.content * p.prim_part := begin by_cases h : p = 0, { simp [h] }, rw [prim_part, if_neg h, ← classical.some_spec (C_content_dvd p)], end @[simp] lemma prim_part_zero : prim_part (0 : polynomial R) = 1 := if_pos rfl lemma is_primitive_prim_part (p : polynomial R) : p.prim_part.is_primitive := begin by_cases h : p = 0, { simp [h] }, rw ← content_eq_zero_iff at h, rw is_primitive_iff_content_eq_one, apply mul_left_cancel₀ h, conv_rhs { rw [p.eq_C_content_mul_prim_part, mul_one, content_C_mul, normalize_content] } end lemma content_prim_part (p : polynomial R) : p.prim_part.content = 1 := p.is_primitive_prim_part.content_eq_one lemma prim_part_ne_zero (p : polynomial R) : p.prim_part ≠ 0 := p.is_primitive_prim_part.ne_zero lemma nat_degree_prim_part (p : polynomial R) : p.prim_part.nat_degree = p.nat_degree := begin by_cases h : C p.content = 0, { rw [C_eq_zero, content_eq_zero_iff] at h, simp [h] }, conv_rhs { rw [p.eq_C_content_mul_prim_part, nat_degree_mul h p.prim_part_ne_zero, nat_degree_C, zero_add] }, end @[simp] lemma is_primitive.prim_part_eq {p : polynomial R} (hp : p.is_primitive) : p.prim_part = p := by rw [← one_mul p.prim_part, ← C_1, ← hp.content_eq_one, ← p.eq_C_content_mul_prim_part] lemma is_unit_prim_part_C (r : R) : is_unit (C r).prim_part := begin by_cases h0 : r = 0, { simp [h0] }, unfold is_unit, refine ⟨⟨C ↑(norm_unit r)⁻¹, C ↑(norm_unit r), by rw [← ring_hom.map_mul, units.inv_mul, C_1], by rw [← ring_hom.map_mul, units.mul_inv, C_1]⟩, _⟩, rw [← normalize_eq_zero, ← C_eq_zero] at h0, apply mul_left_cancel₀ h0, conv_rhs { rw [← content_C, ← (C r).eq_C_content_mul_prim_part], }, simp only [units.coe_mk, normalize_apply, ring_hom.map_mul], rw [mul_assoc, ← ring_hom.map_mul, units.mul_inv, C_1, mul_one], end lemma prim_part_dvd (p : polynomial R) : p.prim_part ∣ p := dvd.intro_left (C p.content) p.eq_C_content_mul_prim_part.symm end prim_part lemma gcd_content_eq_of_dvd_sub {a : R} {p q : polynomial R} (h : C a ∣ p - q) : gcd_monoid.gcd a p.content = gcd_monoid.gcd a q.content := begin rw content_eq_gcd_range_of_lt p (max p.nat_degree q.nat_degree).succ (lt_of_le_of_lt (le_max_left _ _) (nat.lt_succ_self _)), rw content_eq_gcd_range_of_lt q (max p.nat_degree q.nat_degree).succ (lt_of_le_of_lt (le_max_right _ _) (nat.lt_succ_self _)), apply finset.gcd_eq_of_dvd_sub, intros x hx, cases h with w hw, use w.coeff x, rw [← coeff_sub, hw, coeff_C_mul] end lemma content_mul_aux {p q : polynomial R} : gcd_monoid.gcd (p * q).erase_lead.content p.leading_coeff = gcd_monoid.gcd (p.erase_lead * q).content p.leading_coeff := begin rw [gcd_comm (content _) _, gcd_comm (content _) _], apply gcd_content_eq_of_dvd_sub, rw [← self_sub_C_mul_X_pow, ← self_sub_C_mul_X_pow, sub_mul, sub_sub, add_comm, sub_add, sub_sub_cancel, leading_coeff_mul, ring_hom.map_mul, mul_assoc, mul_assoc], apply dvd_sub (dvd.intro _ rfl) (dvd.intro _ rfl), end @[simp] theorem content_mul {p q : polynomial R} : (p * q).content = p.content * q.content := begin classical, suffices h : ∀ (n : ℕ) (p q : polynomial R), ((p * q).degree < n) → (p * q).content = p.content * q.content, { apply h, apply (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 (nat.lt_succ_self _))) }, intro n, induction n with n ih, { intros p q hpq, rw [with_bot.coe_zero, nat.with_bot.lt_zero_iff, degree_eq_bot, mul_eq_zero] at hpq, rcases hpq with rfl | rfl; simp }, intros p q hpq, by_cases p0 : p = 0, { simp [p0] }, by_cases q0 : q = 0, { simp [q0] }, rw [degree_eq_nat_degree (mul_ne_zero p0 q0), with_bot.coe_lt_coe, nat.lt_succ_iff_lt_or_eq, ← with_bot.coe_lt_coe, ← degree_eq_nat_degree (mul_ne_zero p0 q0), nat_degree_mul p0 q0] at hpq, rcases hpq with hlt | heq, { apply ih _ _ hlt }, rw [← p.nat_degree_prim_part, ← q.nat_degree_prim_part, ← with_bot.coe_eq_coe, with_bot.coe_add, ← degree_eq_nat_degree p.prim_part_ne_zero, ← degree_eq_nat_degree q.prim_part_ne_zero] at heq, rw [p.eq_C_content_mul_prim_part, q.eq_C_content_mul_prim_part], suffices h : (q.prim_part * p.prim_part).content = 1, { rw [mul_assoc, content_C_mul, content_C_mul, mul_comm p.prim_part, mul_assoc, content_C_mul, content_C_mul, h, mul_one, content_prim_part, content_prim_part, mul_one, mul_one] }, rw [← normalize_content, normalize_eq_one, is_unit_iff_dvd_one, content_eq_gcd_leading_coeff_content_erase_lead, leading_coeff_mul, gcd_comm], apply (gcd_mul_dvd_mul_gcd _ _ _).trans, rw [content_mul_aux, ih, content_prim_part, mul_one, gcd_comm, ← content_eq_gcd_leading_coeff_content_erase_lead, content_prim_part, one_mul, mul_comm q.prim_part, content_mul_aux, ih, content_prim_part, mul_one, gcd_comm, ← content_eq_gcd_leading_coeff_content_erase_lead, content_prim_part], { rw [← heq, degree_mul, with_bot.add_lt_add_iff_right], { apply degree_erase_lt p.prim_part_ne_zero }, { rw [ne.def, degree_eq_bot], apply q.prim_part_ne_zero } }, { rw [mul_comm, ← heq, degree_mul, with_bot.add_lt_add_iff_left], { apply degree_erase_lt q.prim_part_ne_zero }, { rw [ne.def, degree_eq_bot], apply p.prim_part_ne_zero } } end theorem is_primitive.mul {p q : polynomial R} (hp : p.is_primitive) (hq : q.is_primitive) : (p * q).is_primitive := by rw [is_primitive_iff_content_eq_one, content_mul, hp.content_eq_one, hq.content_eq_one, mul_one] @[simp] theorem prim_part_mul {p q : polynomial R} (h0 : p * q ≠ 0) : (p * q).prim_part = p.prim_part * q.prim_part := begin rw [ne.def, ← content_eq_zero_iff, ← C_eq_zero] at h0, apply mul_left_cancel₀ h0, conv_lhs { rw [← (p * q).eq_C_content_mul_prim_part, p.eq_C_content_mul_prim_part, q.eq_C_content_mul_prim_part] }, rw [content_mul, ring_hom.map_mul], ring, end lemma is_primitive.is_primitive_of_dvd {p q : polynomial R} (hp : p.is_primitive) (hdvd : q ∣ p) : q.is_primitive := begin rcases hdvd with ⟨r, rfl⟩, rw [is_primitive_iff_content_eq_one, ← normalize_content, normalize_eq_one, is_unit_iff_dvd_one], apply dvd.intro r.content, rwa [is_primitive_iff_content_eq_one, content_mul] at hp, end lemma is_primitive.dvd_prim_part_iff_dvd {p q : polynomial R} (hp : p.is_primitive) (hq : q ≠ 0) : p ∣ q.prim_part ↔ p ∣ q := begin refine ⟨λ h, h.trans (dvd.intro_left _ q.eq_C_content_mul_prim_part.symm), λ h, _⟩, rcases h with ⟨r, rfl⟩, apply dvd.intro _, rw [prim_part_mul hq, hp.prim_part_eq], end theorem exists_primitive_lcm_of_is_primitive {p q : polynomial R} (hp : p.is_primitive) (hq : q.is_primitive) : ∃ r : polynomial R, r.is_primitive ∧ (∀ s : polynomial R, p ∣ s ∧ q ∣ s ↔ r ∣ s) := begin classical, have h : ∃ (n : ℕ) (r : polynomial R), r.nat_degree = n ∧ r.is_primitive ∧ p ∣ r ∧ q ∣ r := ⟨(p * q).nat_degree, p * q, rfl, hp.mul hq, dvd_mul_right _ _, dvd_mul_left _ _⟩, rcases nat.find_spec h with ⟨r, rdeg, rprim, pr, qr⟩, refine ⟨r, rprim, λ s, ⟨_, λ rs, ⟨pr.trans rs, qr.trans rs⟩⟩⟩, suffices hs : ∀ (n : ℕ) (s : polynomial R), s.nat_degree = n → (p ∣ s ∧ q ∣ s → r ∣ s), { apply hs s.nat_degree s rfl }, clear s, by_contra con, push_neg at con, rcases nat.find_spec con with ⟨s, sdeg, ⟨ps, qs⟩, rs⟩, have s0 : s ≠ 0, { contrapose! rs, simp [rs] }, have hs := nat.find_min' h ⟨_, s.nat_degree_prim_part, s.is_primitive_prim_part, (hp.dvd_prim_part_iff_dvd s0).2 ps, (hq.dvd_prim_part_iff_dvd s0).2 qs⟩, rw ← rdeg at hs, by_cases sC : s.nat_degree ≤ 0, { rw [eq_C_of_nat_degree_le_zero (le_trans hs sC), is_primitive_iff_content_eq_one, content_C, normalize_eq_one] at rprim, rw [eq_C_of_nat_degree_le_zero (le_trans hs sC), ← dvd_content_iff_C_dvd] at rs, apply rs rprim.dvd }, have hcancel := nat_degree_cancel_leads_lt_of_nat_degree_le_nat_degree hs (lt_of_not_ge sC), rw sdeg at hcancel, apply nat.find_min con hcancel, refine ⟨_, rfl, ⟨dvd_cancel_leads_of_dvd_of_dvd pr ps, dvd_cancel_leads_of_dvd_of_dvd qr qs⟩, λ rcs, rs _⟩, rw ← rprim.dvd_prim_part_iff_dvd s0, rw [cancel_leads, tsub_eq_zero_iff_le.mpr hs, pow_zero, mul_one] at rcs, have h := dvd_add rcs (dvd.intro_left _ rfl), have hC0 := rprim.ne_zero, rw [ne.def, ← leading_coeff_eq_zero, ← C_eq_zero] at hC0, rw [sub_add_cancel, ← rprim.dvd_prim_part_iff_dvd (mul_ne_zero hC0 s0)] at h, rcases is_unit_prim_part_C r.leading_coeff with ⟨u, hu⟩, apply h.trans (associated.symm ⟨u, _⟩).dvd, rw [prim_part_mul (mul_ne_zero hC0 s0), hu, mul_comm], end lemma dvd_iff_content_dvd_content_and_prim_part_dvd_prim_part {p q : polynomial R} (hq : q ≠ 0) : p ∣ q ↔ p.content ∣ q.content ∧ p.prim_part ∣ q.prim_part := begin split; intro h, { rcases h with ⟨r, rfl⟩, rw [content_mul, p.is_primitive_prim_part.dvd_prim_part_iff_dvd hq], exact ⟨dvd.intro _ rfl, p.prim_part_dvd.trans (dvd.intro _ rfl)⟩ }, { rw [p.eq_C_content_mul_prim_part, q.eq_C_content_mul_prim_part], exact mul_dvd_mul (ring_hom.map_dvd C h.1) h.2 } end @[priority 100] instance normalized_gcd_monoid : normalized_gcd_monoid (polynomial R) := normalized_gcd_monoid_of_exists_lcm $ λ p q, begin rcases exists_primitive_lcm_of_is_primitive p.is_primitive_prim_part q.is_primitive_prim_part with ⟨r, rprim, hr⟩, refine ⟨C (lcm p.content q.content) * r, λ s, _⟩, by_cases hs : s = 0, { simp [hs] }, by_cases hpq : C (lcm p.content q.content) = 0, { rw [C_eq_zero, lcm_eq_zero_iff, content_eq_zero_iff, content_eq_zero_iff] at hpq, rcases hpq with hpq | hpq; simp [hpq, hs] }, iterate 3 { rw dvd_iff_content_dvd_content_and_prim_part_dvd_prim_part hs }, rw [content_mul, rprim.content_eq_one, mul_one, content_C, normalize_lcm, lcm_dvd_iff, prim_part_mul (mul_ne_zero hpq rprim.ne_zero), rprim.prim_part_eq, is_unit.mul_left_dvd _ _ _ (is_unit_prim_part_C (lcm p.content q.content)), ← hr s.prim_part], tauto, end lemma degree_gcd_le_left {p : polynomial R} (hp : p ≠ 0) (q) : (gcd p q).degree ≤ p.degree := begin by_cases hq : q = 0, { simp [hq] }, have := nat_degree_le_iff_degree_le.mp (nat_degree_le_of_dvd (gcd_dvd_left p q) hp), rwa degree_eq_nat_degree hp end lemma degree_gcd_le_right (p) {q : polynomial R} (hq : q ≠ 0) : (gcd p q).degree ≤ q.degree := by { rw [gcd_comm], exact degree_gcd_le_left hq p } end normalized_gcd_monoid end polynomial
0455c63b60bdb298323c2cae441f52f2b80b5467
b561a44b48979a98df50ade0789a21c79ee31288
/stage0/src/Lean/Elab/Tactic/Basic.lean
f107e518696c69b60ac66f6dd0d5311458bc27f0
[ "Apache-2.0" ]
permissive
3401ijk/lean4
97659c475ebd33a034fed515cb83a85f75ccfb06
a5b1b8de4f4b038ff752b9e607b721f15a9a4351
refs/heads/master
1,693,933,007,651
1,636,424,845,000
1,636,424,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,701
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Util.CollectMVars import Lean.Parser.Command import Lean.Meta.PPGoal import Lean.Meta.Tactic.Assumption import Lean.Meta.Tactic.Contradiction import Lean.Meta.Tactic.Intro import Lean.Meta.Tactic.Clear import Lean.Meta.Tactic.Revert import Lean.Meta.Tactic.Subst import Lean.Elab.Util import Lean.Elab.Term import Lean.Elab.Binders namespace Lean.Elab open Meta /- Assign `mvarId := sorry` -/ def admitGoal (mvarId : MVarId) : MetaM Unit := withMVarContext mvarId do let mvarType ← inferType (mkMVar mvarId) assignExprMVar mvarId (← mkSorry mvarType (synthetic := true)) def goalsToMessageData (goals : List MVarId) : MessageData := MessageData.joinSep (goals.map $ MessageData.ofGoal) m!"\n\n" def Term.reportUnsolvedGoals (goals : List MVarId) : TermElabM Unit := withPPInaccessibleNames do logError <| MessageData.tagged `Tactic.unsolvedGoals <| m!"unsolved goals\n{goalsToMessageData goals}" goals.forM fun mvarId => admitGoal mvarId namespace Tactic structure Context where main : MVarId -- declaration name of the executing elaborator, used by `mkTacticInfo` to persist it in the info tree elaborator : Name structure State where goals : List MVarId deriving Inhabited structure SavedState where term : Term.SavedState tactic : State abbrev TacticM := ReaderT Context $ StateRefT State TermElabM abbrev Tactic := Syntax → TacticM Unit -- Make the compiler generate specialized `pure`/`bind` so we do not have to optimize through the -- whole monad stack at every use site. May eventually be covered by `deriving`. instance : Monad TacticM := let i := inferInstanceAs (Monad TacticM); { pure := i.pure, bind := i.bind } def getGoals : TacticM (List MVarId) := return (← get).goals def setGoals (mvarIds : List MVarId) : TacticM Unit := modify fun s => { s with goals := mvarIds } def pruneSolvedGoals : TacticM Unit := do let gs ← getGoals let gs ← gs.filterM fun g => not <$> isExprMVarAssigned g setGoals gs def getUnsolvedGoals : TacticM (List MVarId) := do pruneSolvedGoals getGoals @[inline] private def TacticM.runCore (x : TacticM α) (ctx : Context) (s : State) : TermElabM (α × State) := x ctx |>.run s @[inline] private def TacticM.runCore' (x : TacticM α) (ctx : Context) (s : State) : TermElabM α := Prod.fst <$> x.runCore ctx s def run (mvarId : MVarId) (x : TacticM Unit) : TermElabM (List MVarId) := withMVarContext mvarId do let savedSyntheticMVars := (← get).syntheticMVars modify fun s => { s with syntheticMVars := [] } let aux : TacticM (List MVarId) := /- Important: the following `try` does not backtrack the state. This is intentional because we don't want to backtrack the error messages when we catch the "abort internal exception" We must define `run` here because we define `MonadExcept` instance for `TacticM` -/ try x; getUnsolvedGoals catch ex => if isAbortTacticException ex then getUnsolvedGoals else throw ex try aux.runCore' { main := mvarId, elaborator := Name.anonymous } { goals := [mvarId] } finally modify fun s => { s with syntheticMVars := savedSyntheticMVars } protected def saveState : TacticM SavedState := return { term := (← Term.saveState), tactic := (← get) } def SavedState.restore (b : SavedState) : TacticM Unit := do b.term.restore set b.tactic protected def getCurrMacroScope : TacticM MacroScope := do pure (← readThe Term.Context).currMacroScope protected def getMainModule : TacticM Name := do pure (← getEnv).mainModule unsafe def mkTacticAttribute : IO (KeyedDeclsAttribute Tactic) := mkElabAttribute Tactic `Lean.Elab.Tactic.tacticElabAttribute `builtinTactic `tactic `Lean.Parser.Tactic `Lean.Elab.Tactic.Tactic "tactic" @[builtinInit mkTacticAttribute] constant tacticElabAttribute : KeyedDeclsAttribute Tactic def mkTacticInfo (mctxBefore : MetavarContext) (goalsBefore : List MVarId) (stx : Syntax) : TacticM Info := return Info.ofTacticInfo { elaborator := (← read).elaborator mctxBefore := mctxBefore goalsBefore := goalsBefore stx := stx mctxAfter := (← getMCtx) goalsAfter := (← getUnsolvedGoals) } def mkInitialTacticInfo (stx : Syntax) : TacticM (TacticM Info) := do let mctxBefore ← getMCtx let goalsBefore ← getUnsolvedGoals return mkTacticInfo mctxBefore goalsBefore stx @[inline] def withTacticInfoContext (stx : Syntax) (x : TacticM α) : TacticM α := do withInfoContext x (← mkInitialTacticInfo stx) /- Important: we must define `evalTacticUsing` and `expandTacticMacroFns` before we define the instance `MonadExcept` for `TacticM` since it backtracks the state including error messages, and this is bad when rethrowing the exception at the `catch` block in these methods. We marked these places with a `(*)` in these methods. -/ private def evalTacticUsing (s : SavedState) (stx : Syntax) (tactics : List (KeyedDeclsAttribute.AttributeEntry Tactic)) : TacticM Unit := do let rec loop | [] => throwErrorAt stx "unexpected syntax {indentD stx}" | evalFn::evalFns => do try withReader ({ · with elaborator := evalFn.declName }) <| withTacticInfoContext stx <| evalFn.value stx catch | ex@(Exception.error _ _) => match evalFns with | [] => throw ex -- (*) | evalFns => s.restore; loop evalFns | ex@(Exception.internal id _) => if id == unsupportedSyntaxExceptionId then s.restore; loop evalFns else throw ex loop tactics mutual partial def expandTacticMacroFns (stx : Syntax) (macros : List (KeyedDeclsAttribute.AttributeEntry Macro)) : TacticM Unit := let rec loop | [] => throwErrorAt stx "tactic '{stx.getKind}' has not been implemented" | m::ms => do let scp ← getCurrMacroScope try withReader ({ · with elaborator := m.declName }) do withTacticInfoContext stx do let stx' ← adaptMacro m.value stx evalTactic stx' catch ex => if ms.isEmpty then throw ex -- (*) loop ms loop macros partial def expandTacticMacro (stx : Syntax) : TacticM Unit := do expandTacticMacroFns stx (macroAttribute.getEntries (← getEnv) stx.getKind) partial def evalTacticAux (stx : Syntax) : TacticM Unit := withRef stx $ withIncRecDepth $ withFreshMacroScope $ match stx with | Syntax.node _ k args => if k == nullKind then -- Macro writers create a sequence of tactics `t₁ ... tₙ` using `mkNullNode #[t₁, ..., tₙ]` stx.getArgs.forM evalTactic else do trace[Elab.step] "{stx}" let s ← Tactic.saveState match tacticElabAttribute.getEntries (← getEnv) stx.getKind with | [] => expandTacticMacro stx | evalFns => evalTacticUsing s stx evalFns | _ => throwError m!"unexpected tactic{indentD stx}" partial def evalTactic (stx : Syntax) : TacticM Unit := evalTacticAux stx end def throwNoGoalsToBeSolved : TacticM α := throwError "no goals to be solved" def done : TacticM Unit := do let gs ← getUnsolvedGoals unless gs.isEmpty do Term.reportUnsolvedGoals gs throwAbortTactic def focus (x : TacticM α) : TacticM α := do let mvarId :: mvarIds ← getUnsolvedGoals | throwNoGoalsToBeSolved setGoals [mvarId] let a ← x let mvarIds' ← getUnsolvedGoals setGoals (mvarIds' ++ mvarIds) pure a def focusAndDone (tactic : TacticM α) : TacticM α := focus do let a ← tactic done pure a /- Close the main goal using the given tactic. If it fails, log the error and `admit` -/ def closeUsingOrAdmit (tac : TacticM Unit) : TacticM Unit := do /- Important: we must define `closeUsingOrAdmit` before we define the instance `MonadExcept` for `TacticM` since it backtracks the state including error messages. -/ let mvarId :: mvarIds ← getUnsolvedGoals | throwNoGoalsToBeSolved try focusAndDone tac catch ex => logException ex admitGoal mvarId setGoals mvarIds instance : MonadBacktrack SavedState TacticM where saveState := Tactic.saveState restoreState b := b.restore @[inline] protected def tryCatch {α} (x : TacticM α) (h : Exception → TacticM α) : TacticM α := do let b ← saveState try x catch ex => b.restore; h ex instance : MonadExcept Exception TacticM where throw := throw tryCatch := Tactic.tryCatch @[inline] protected def orElse {α} (x : TacticM α) (y : Unit → TacticM α) : TacticM α := do try x catch _ => y () instance {α} : OrElse (TacticM α) where orElse := Tactic.orElse instance : Alternative TacticM where failure := fun {α} => throwError "failed" orElse := Tactic.orElse /- Save the current tactic state for a token `stx`. This method is a no-op if `stx` has no position information. We use this method to save the tactic state at punctuation such as `;` -/ def saveTacticInfoForToken (stx : Syntax) : TacticM Unit := do unless stx.getPos?.isNone do withTacticInfoContext stx (pure ()) /- Elaborate `x` with `stx` on the macro stack -/ @[inline] def withMacroExpansion {α} (beforeStx afterStx : Syntax) (x : TacticM α) : TacticM α := withMacroExpansionInfo beforeStx afterStx do withTheReader Term.Context (fun ctx => { ctx with macroStack := { before := beforeStx, after := afterStx } :: ctx.macroStack }) x /-- Adapt a syntax transformation to a regular tactic evaluator. -/ def adaptExpander (exp : Syntax → TacticM Syntax) : Tactic := fun stx => do let stx' ← exp stx withMacroExpansion stx stx' $ evalTactic stx' def appendGoals (mvarIds : List MVarId) : TacticM Unit := modify fun s => { s with goals := s.goals ++ mvarIds } def replaceMainGoal (mvarIds : List MVarId) : TacticM Unit := do let (mvarId :: mvarIds') ← getGoals | throwNoGoalsToBeSolved modify fun s => { s with goals := mvarIds ++ mvarIds' } /-- Return the first goal. -/ def getMainGoal : TacticM MVarId := do loop (← getGoals) where loop : List MVarId → TacticM MVarId | [] => throwNoGoalsToBeSolved | mvarId :: mvarIds => do if (← isExprMVarAssigned mvarId) then loop mvarIds else setGoals (mvarId :: mvarIds) return mvarId /-- Return the main goal metavariable declaration. -/ def getMainDecl : TacticM MetavarDecl := do getMVarDecl (← getMainGoal) /-- Return the main goal tag. -/ def getMainTag : TacticM Name := return (← getMainDecl).userName /-- Return expected type for the main goal. -/ def getMainTarget : TacticM Expr := do instantiateMVars (← getMainDecl).type /-- Execute `x` using the main goal local context and instances -/ def withMainContext (x : TacticM α) : TacticM α := do withMVarContext (← getMainGoal) x /-- Evaluate `tac` at `mvarId`, and return the list of resulting subgoals. -/ def evalTacticAt (tac : Syntax) (mvarId : MVarId) : TacticM (List MVarId) := do let gs ← getGoals try setGoals [mvarId] evalTactic tac pruneSolvedGoals getGoals finally setGoals gs def ensureHasNoMVars (e : Expr) : TacticM Unit := do let e ← instantiateMVars e let pendingMVars ← getMVars e discard <| Term.logUnassignedUsingErrorInfos pendingMVars if e.hasExprMVar then throwError "tactic failed, resulting expression contains metavariables{indentExpr e}" /-- Close main goal using the given expression. If `checkUnassigned == true`, then `val` must not contain unassinged metavariables. -/ def closeMainGoal (val : Expr) (checkUnassigned := true): TacticM Unit := do if checkUnassigned then ensureHasNoMVars val assignExprMVar (← getMainGoal) val replaceMainGoal [] @[inline] def liftMetaMAtMain (x : MVarId → MetaM α) : TacticM α := do withMainContext do x (← getMainGoal) @[inline] def liftMetaTacticAux (tac : MVarId → MetaM (α × List MVarId)) : TacticM α := do withMainContext do let (a, mvarIds) ← tac (← getMainGoal) replaceMainGoal mvarIds pure a @[inline] def liftMetaTactic (tactic : MVarId → MetaM (List MVarId)) : TacticM Unit := liftMetaTacticAux fun mvarId => do let gs ← tactic mvarId pure ((), gs) @[inline] def liftMetaTactic1 (tactic : MVarId → MetaM (Option MVarId)) : TacticM Unit := withMainContext do if let some mvarId ← tactic (← getMainGoal) then replaceMainGoal [mvarId] else replaceMainGoal [] def tryTactic? (tactic : TacticM α) : TacticM (Option α) := do try pure (some (← tactic)) catch _ => pure none def tryTactic (tactic : TacticM α) : TacticM Bool := do try discard tactic pure true catch _ => pure false /-- Use `parentTag` to tag untagged goals at `newGoals`. If there are multiple new untagged goals, they are named using `<parentTag>.<newSuffix>_<idx>` where `idx > 0`. If there is only one new untagged goal, then we just use `parentTag` -/ def tagUntaggedGoals (parentTag : Name) (newSuffix : Name) (newGoals : List MVarId) : TacticM Unit := do let mctx ← getMCtx let mut numAnonymous := 0 for g in newGoals do if mctx.isAnonymousMVar g then numAnonymous := numAnonymous + 1 modifyMCtx fun mctx => do let mut mctx := mctx let mut idx := 1 for g in newGoals do if mctx.isAnonymousMVar g then if numAnonymous == 1 then mctx := mctx.renameMVar g parentTag else mctx := mctx.renameMVar g (parentTag ++ newSuffix.appendIndexAfter idx) idx := idx + 1 pure mctx /- Recall that `ident' := ident <|> Term.hole` -/ def getNameOfIdent' (id : Syntax) : Name := if id.isIdent then id.getId else `_ def getFVarId (id : Syntax) : TacticM FVarId := withRef id do let fvar? ← Term.isLocalIdent? id; match fvar? with | some fvar => pure fvar.fvarId! | none => throwError "unknown variable '{id.getId}'" def getFVarIds (ids : Array Syntax) : TacticM (Array FVarId) := do withMainContext do ids.mapM getFVarId /-- Use position of `=> $body` for error messages. If there is a line break before `body`, the message will be displayed on `=>` only, but the "full range" for the info view will still include `body`. -/ def withCaseRef [Monad m] [MonadRef m] (arrow body : Syntax) (x : m α) : m α := withRef (mkNullNode #[arrow, body]) x builtin_initialize registerTraceClass `Elab.tactic end Lean.Elab.Tactic
b55480b3180ab9d3386d97043b5f5e4f8f123841
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/num/lemmas.lean
8410b70912bdd519630639fda746a92076dc063b
[]
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
32,376
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.num.bitwise import Mathlib.data.int.char_zero import Mathlib.data.nat.gcd import Mathlib.data.nat.psub import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Properties of the binary representation of integers -/ namespace pos_num @[simp] theorem cast_one {α : Type u_1} [HasOne α] [Add α] : ↑1 = 1 := rfl @[simp] theorem cast_one' {α : Type u_1} [HasOne α] [Add α] : ↑one = 1 := rfl @[simp] theorem cast_bit0 {α : Type u_1} [HasOne α] [Add α] (n : pos_num) : ↑(bit0 n) = bit0 ↑n := rfl @[simp] theorem cast_bit1 {α : Type u_1} [HasOne α] [Add α] (n : pos_num) : ↑(bit1 n) = bit1 ↑n := rfl @[simp] theorem cast_to_nat {α : Type u_1} [add_monoid α] [HasOne α] (n : pos_num) : ↑↑n = ↑n := sorry @[simp] theorem to_nat_to_int (n : pos_num) : ↑↑n = ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = ↑n)) (Eq.symm (int.nat_cast_eq_coe_nat ↑n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = ↑n)) (cast_to_nat n))) (Eq.refl ↑n)) @[simp] theorem cast_to_int {α : Type u_1} [add_group α] [HasOne α] (n : pos_num) : ↑↑n = ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = ↑n)) (Eq.symm (to_nat_to_int n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑↑↑n = ↑n)) (int.cast_coe_nat ↑n))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = ↑n)) (cast_to_nat n))) (Eq.refl ↑n))) theorem succ_to_nat (n : pos_num) : ↑(succ n) = ↑n + 1 := sorry theorem one_add (n : pos_num) : 1 + n = succ n := pos_num.cases_on n (Eq.refl (1 + one)) (fun (n : pos_num) => Eq.refl (1 + bit1 n)) fun (n : pos_num) => Eq.refl (1 + bit0 n) theorem add_one (n : pos_num) : n + 1 = succ n := pos_num.cases_on n (Eq.refl (one + 1)) (fun (n : pos_num) => Eq.refl (bit1 n + 1)) fun (n : pos_num) => Eq.refl (bit0 n + 1) theorem add_to_nat (m : pos_num) (n : pos_num) : ↑(m + n) = ↑m + ↑n := sorry theorem add_succ (m : pos_num) (n : pos_num) : m + succ n = succ (m + n) := sorry theorem bit0_of_bit0 (n : pos_num) : bit0 n = bit0 n := sorry theorem bit1_of_bit1 (n : pos_num) : bit1 n = bit1 n := (fun (this : bit0 n + 1 = bit1 n) => this) (eq.mpr (id (Eq._oldrec (Eq.refl (bit0 n + 1 = bit1 n)) (add_one (bit0 n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (succ (bit0 n) = bit1 n)) (bit0_of_bit0 n))) (Eq.refl (succ (bit0 n))))) theorem mul_to_nat (m : pos_num) (n : pos_num) : ↑(m * n) = ↑m * ↑n := sorry theorem to_nat_pos (n : pos_num) : 0 < ↑n := sorry theorem cmp_to_nat_lemma {m : pos_num} {n : pos_num} : ↑m < ↑n → ↑(bit1 m) < ↑(bit0 n) := sorry theorem cmp_swap (m : pos_num) (n : pos_num) : ordering.swap (cmp m n) = cmp n m := sorry theorem cmp_to_nat (m : pos_num) (n : pos_num) : ordering.cases_on (cmp m n) (↑m < ↑n) (m = n) (↑n < ↑m) := sorry theorem lt_to_nat {m : pos_num} {n : pos_num} : ↑m < ↑n ↔ m < n := sorry theorem le_to_nat {m : pos_num} {n : pos_num} : ↑m ≤ ↑n ↔ m ≤ n := eq.mpr (id (Eq._oldrec (Eq.refl (↑m ≤ ↑n ↔ m ≤ n)) (Eq.symm (propext not_lt)))) (not_congr lt_to_nat) end pos_num namespace num theorem add_zero (n : num) : n + 0 = n := num.cases_on n (Eq.refl (zero + 0)) fun (n : pos_num) => Eq.refl (pos n + 0) theorem zero_add (n : num) : 0 + n = n := num.cases_on n (Eq.refl (0 + zero)) fun (n : pos_num) => Eq.refl (0 + pos n) theorem add_one (n : num) : n + 1 = succ n := sorry theorem add_succ (m : num) (n : num) : m + succ n = succ (m + n) := sorry @[simp] theorem add_of_nat (m : ℕ) (n : ℕ) : ↑(m + n) = ↑m + ↑n := sorry theorem bit0_of_bit0 (n : num) : bit0 n = num.bit0 n := num.cases_on n (idRhs (bit0 0 = bit0 0) rfl) fun (n : pos_num) => idRhs (pos (bit0 n) = pos (pos_num.bit0 n)) (congr_arg pos (pos_num.bit0_of_bit0 n)) theorem bit1_of_bit1 (n : num) : bit1 n = num.bit1 n := num.cases_on n (idRhs (bit1 0 = bit1 0) rfl) fun (n : pos_num) => idRhs (pos (bit1 n) = pos (pos_num.bit1 n)) (congr_arg pos (pos_num.bit1_of_bit1 n)) @[simp] theorem cast_zero {α : Type u_1} [HasZero α] [HasOne α] [Add α] : ↑0 = 0 := rfl @[simp] theorem cast_zero' {α : Type u_1} [HasZero α] [HasOne α] [Add α] : ↑zero = 0 := rfl @[simp] theorem cast_one {α : Type u_1} [HasZero α] [HasOne α] [Add α] : ↑1 = 1 := rfl @[simp] theorem cast_pos {α : Type u_1} [HasZero α] [HasOne α] [Add α] (n : pos_num) : ↑(pos n) = ↑n := rfl theorem succ'_to_nat (n : num) : ↑(succ' n) = ↑n + 1 := num.cases_on n (idRhs (↑(succ' 0) = 0 + ↑(succ' 0)) (Eq.symm (zero_add ↑(succ' 0)))) fun (n : pos_num) => idRhs (↑(pos_num.succ n) = ↑n + 1) (pos_num.succ_to_nat n) theorem succ_to_nat (n : num) : ↑(succ n) = ↑n + 1 := succ'_to_nat n @[simp] theorem cast_to_nat {α : Type u_1} [add_monoid α] [HasOne α] (n : num) : ↑↑n = ↑n := num.cases_on n (idRhs (↑0 = 0) nat.cast_zero) fun (n : pos_num) => idRhs (↑↑n = ↑n) (pos_num.cast_to_nat n) @[simp] theorem to_nat_to_int (n : num) : ↑↑n = ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = ↑n)) (Eq.symm (int.nat_cast_eq_coe_nat ↑n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = ↑n)) (cast_to_nat n))) (Eq.refl ↑n)) @[simp] theorem cast_to_int {α : Type u_1} [add_group α] [HasOne α] (n : num) : ↑↑n = ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = ↑n)) (Eq.symm (to_nat_to_int n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑↑↑n = ↑n)) (int.cast_coe_nat ↑n))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = ↑n)) (cast_to_nat n))) (Eq.refl ↑n))) theorem to_of_nat (n : ℕ) : ↑↑n = n := sorry @[simp] theorem of_nat_cast {α : Type u_1} [add_monoid α] [HasOne α] (n : ℕ) : ↑↑n = ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = ↑n)) (Eq.symm (cast_to_nat ↑n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑↑↑n = ↑n)) (to_of_nat n))) (Eq.refl ↑n)) theorem of_nat_inj {m : ℕ} {n : ℕ} : ↑m = ↑n ↔ m = n := { mp := fun (h : ↑m = ↑n) => function.left_inverse.injective to_of_nat h, mpr := congr_arg fun (x : ℕ) => ↑x } theorem add_to_nat (m : num) (n : num) : ↑(m + n) = ↑m + ↑n := sorry theorem mul_to_nat (m : num) (n : num) : ↑(m * n) = ↑m * ↑n := sorry theorem cmp_to_nat (m : num) (n : num) : ordering.cases_on (cmp m n) (↑m < ↑n) (m = n) (↑n < ↑m) := sorry theorem lt_to_nat {m : num} {n : num} : ↑m < ↑n ↔ m < n := sorry theorem le_to_nat {m : num} {n : num} : ↑m ≤ ↑n ↔ m ≤ n := eq.mpr (id (Eq._oldrec (Eq.refl (↑m ≤ ↑n ↔ m ≤ n)) (Eq.symm (propext not_lt)))) (not_congr lt_to_nat) end num namespace pos_num @[simp] theorem of_to_nat (n : pos_num) : ↑↑n = num.pos n := sorry end pos_num namespace num @[simp] theorem of_to_nat (n : num) : ↑↑n = n := num.cases_on n (idRhs (↑↑0 = ↑↑0) rfl) fun (n : pos_num) => idRhs (↑↑n = pos n) (pos_num.of_to_nat n) theorem to_nat_inj {m : num} {n : num} : ↑m = ↑n ↔ m = n := { mp := fun (h : ↑m = ↑n) => function.left_inverse.injective of_to_nat h, mpr := congr_arg fun (x : num) => ↑x } /-- This tactic tries to turn an (in)equality about `num`s to one about `nat`s by rewriting. ```lean example (n : num) (m : num) : n ≤ n + m := begin num.transfer_rw, exact nat.le_add_right _ _ end ``` -/ /-- This tactic tries to prove (in)equalities about `num`s by transfering them to the `nat` world and then trying to call `simp`. ```lean example (n : num) (m : num) : n ≤ n + m := by num.transfer ``` -/ protected instance comm_semiring : comm_semiring num := comm_semiring.mk Add.add sorry 0 zero_add add_zero sorry Mul.mul sorry 1 sorry sorry sorry sorry sorry sorry sorry protected instance ordered_cancel_add_comm_monoid : ordered_cancel_add_comm_monoid num := ordered_cancel_add_comm_monoid.mk comm_semiring.add comm_semiring.add_assoc sorry comm_semiring.zero comm_semiring.zero_add comm_semiring.add_zero comm_semiring.add_comm sorry LessEq Less sorry sorry sorry sorry sorry protected instance linear_ordered_semiring : linear_ordered_semiring num := linear_ordered_semiring.mk comm_semiring.add comm_semiring.add_assoc comm_semiring.zero comm_semiring.zero_add comm_semiring.add_zero comm_semiring.add_comm comm_semiring.mul comm_semiring.mul_assoc comm_semiring.one comm_semiring.one_mul comm_semiring.mul_one comm_semiring.zero_mul comm_semiring.mul_zero comm_semiring.left_distrib comm_semiring.right_distrib ordered_cancel_add_comm_monoid.add_left_cancel ordered_cancel_add_comm_monoid.add_right_cancel ordered_cancel_add_comm_monoid.le ordered_cancel_add_comm_monoid.lt ordered_cancel_add_comm_monoid.le_refl ordered_cancel_add_comm_monoid.le_trans ordered_cancel_add_comm_monoid.le_antisymm ordered_cancel_add_comm_monoid.add_le_add_left ordered_cancel_add_comm_monoid.le_of_add_le_add_left sorry sorry sorry sorry num.decidable_le num.decidable_eq num.decidable_lt sorry theorem dvd_to_nat (m : num) (n : num) : ↑m ∣ ↑n ↔ m ∣ n := sorry end num namespace pos_num theorem to_nat_inj {m : pos_num} {n : pos_num} : ↑m = ↑n ↔ m = n := sorry theorem pred'_to_nat (n : pos_num) : ↑(pred' n) = Nat.pred ↑n := sorry @[simp] theorem pred'_succ' (n : num) : pred' (num.succ' n) = n := sorry @[simp] theorem succ'_pred' (n : pos_num) : num.succ' (pred' n) = n := sorry protected instance has_dvd : has_dvd pos_num := has_dvd.mk fun (m n : pos_num) => num.pos m ∣ num.pos n theorem dvd_to_nat {m : pos_num} {n : pos_num} : ↑m ∣ ↑n ↔ m ∣ n := num.dvd_to_nat (num.pos m) (num.pos n) theorem size_to_nat (n : pos_num) : ↑(size n) = nat.size ↑n := sorry theorem size_eq_nat_size (n : pos_num) : ↑(size n) = nat_size n := sorry theorem nat_size_to_nat (n : pos_num) : nat_size n = nat.size ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (nat_size n = nat.size ↑n)) (Eq.symm (size_eq_nat_size n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(size n) = nat.size ↑n)) (size_to_nat n))) (Eq.refl (nat.size ↑n))) theorem nat_size_pos (n : pos_num) : 0 < nat_size n := pos_num.cases_on n (nat.succ_pos 0) (fun (n : pos_num) => nat.succ_pos (nat_size n)) fun (n : pos_num) => nat.succ_pos (nat_size n) /-- This tactic tries to turn an (in)equality about `pos_num`s to one about `nat`s by rewriting. ```lean example (n : pos_num) (m : pos_num) : n ≤ n + m := begin pos_num.transfer_rw, exact nat.le_add_right _ _ end ``` -/ /-- This tactic tries to prove (in)equalities about `pos_num`s by transferring them to the `nat` world and then trying to call `simp`. ```lean example (n : pos_num) (m : pos_num) : n ≤ n + m := by pos_num.transfer ``` -/ protected instance add_comm_semigroup : add_comm_semigroup pos_num := add_comm_semigroup.mk Add.add sorry sorry protected instance comm_monoid : comm_monoid pos_num := comm_monoid.mk Mul.mul sorry 1 sorry sorry sorry protected instance distrib : distrib pos_num := distrib.mk Mul.mul Add.add sorry sorry protected instance linear_order : linear_order pos_num := linear_order.mk LessEq Less sorry sorry sorry sorry (fun (a b : pos_num) => pos_num.decidable_le a b) (fun (a b : pos_num) => pos_num.decidable_eq a b) fun (a b : pos_num) => pos_num.decidable_lt a b @[simp] theorem cast_to_num (n : pos_num) : ↑n = num.pos n := eq.mpr (id (Eq._oldrec (Eq.refl (↑n = num.pos n)) (Eq.symm (cast_to_nat n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = num.pos n)) (Eq.symm (of_to_nat n)))) (Eq.refl ↑↑n)) @[simp] theorem bit_to_nat (b : Bool) (n : pos_num) : ↑(bit b n) = nat.bit b ↑n := bool.cases_on b (Eq.refl ↑(bit false n)) (Eq.refl ↑(bit tt n)) @[simp] theorem cast_add {α : Type u_1} [add_monoid α] [HasOne α] (m : pos_num) (n : pos_num) : ↑(m + n) = ↑m + ↑n := sorry @[simp] theorem cast_succ {α : Type u_1} [add_monoid α] [HasOne α] (n : pos_num) : ↑(succ n) = ↑n + 1 := eq.mpr (id (Eq._oldrec (Eq.refl (↑(succ n) = ↑n + 1)) (Eq.symm (add_one n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(n + 1) = ↑n + 1)) (cast_add n 1))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑n + ↑1 = ↑n + 1)) cast_one)) (Eq.refl (↑n + 1)))) @[simp] theorem cast_inj {α : Type u_1} [add_monoid α] [HasOne α] [char_zero α] {m : pos_num} {n : pos_num} : ↑m = ↑n ↔ m = n := sorry @[simp] theorem one_le_cast {α : Type u_1} [linear_ordered_semiring α] (n : pos_num) : 1 ≤ ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (1 ≤ ↑n)) (Eq.symm (cast_to_nat n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (1 ≤ ↑↑n)) (Eq.symm nat.cast_one))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑1 ≤ ↑↑n)) (propext nat.cast_le))) (to_nat_pos n))) @[simp] theorem cast_pos {α : Type u_1} [linear_ordered_semiring α] (n : pos_num) : 0 < ↑n := lt_of_lt_of_le zero_lt_one (one_le_cast n) @[simp] theorem cast_mul {α : Type u_1} [semiring α] (m : pos_num) (n : pos_num) : ↑(m * n) = ↑m * ↑n := sorry @[simp] theorem cmp_eq (m : pos_num) (n : pos_num) : cmp m n = ordering.eq ↔ m = n := sorry @[simp] theorem cast_lt {α : Type u_1} [linear_ordered_semiring α] {m : pos_num} {n : pos_num} : ↑m < ↑n ↔ m < n := sorry @[simp] theorem cast_le {α : Type u_1} [linear_ordered_semiring α] {m : pos_num} {n : pos_num} : ↑m ≤ ↑n ↔ m ≤ n := eq.mpr (id (Eq._oldrec (Eq.refl (↑m ≤ ↑n ↔ m ≤ n)) (Eq.symm (propext not_lt)))) (not_congr cast_lt) end pos_num namespace num theorem bit_to_nat (b : Bool) (n : num) : ↑(bit b n) = nat.bit b ↑n := bool.cases_on b (num.cases_on n (Eq.refl ↑(bit false zero)) fun (n : pos_num) => Eq.refl ↑(bit false (pos n))) (num.cases_on n (Eq.refl ↑(bit tt zero)) fun (n : pos_num) => Eq.refl ↑(bit tt (pos n))) theorem cast_succ' {α : Type u_1} [add_monoid α] [HasOne α] (n : num) : ↑(succ' n) = ↑n + 1 := sorry theorem cast_succ {α : Type u_1} [add_monoid α] [HasOne α] (n : num) : ↑(succ n) = ↑n + 1 := cast_succ' n @[simp] theorem cast_add {α : Type u_1} [semiring α] (m : num) (n : num) : ↑(m + n) = ↑m + ↑n := sorry @[simp] theorem cast_bit0 {α : Type u_1} [semiring α] (n : num) : ↑(num.bit0 n) = bit0 ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (↑(num.bit0 n) = bit0 ↑n)) (Eq.symm (bit0_of_bit0 n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(bit0 n) = bit0 ↑n)) (bit0.equations._eqn_1 n))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(n + n) = bit0 ↑n)) (cast_add n n))) (Eq.refl (↑n + ↑n)))) @[simp] theorem cast_bit1 {α : Type u_1} [semiring α] (n : num) : ↑(num.bit1 n) = bit1 ↑n := sorry @[simp] theorem cast_mul {α : Type u_1} [semiring α] (m : num) (n : num) : ↑(m * n) = ↑m * ↑n := sorry theorem size_to_nat (n : num) : ↑(size n) = nat.size ↑n := num.cases_on n (idRhs (0 = nat.size 0) (Eq.symm nat.size_zero)) fun (n : pos_num) => idRhs (↑(pos_num.size n) = nat.size ↑n) (pos_num.size_to_nat n) theorem size_eq_nat_size (n : num) : ↑(size n) = nat_size n := num.cases_on n (idRhs (↑(size 0) = ↑(size 0)) rfl) fun (n : pos_num) => idRhs (↑(pos_num.size n) = pos_num.nat_size n) (pos_num.size_eq_nat_size n) theorem nat_size_to_nat (n : num) : nat_size n = nat.size ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (nat_size n = nat.size ↑n)) (Eq.symm (size_eq_nat_size n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(size n) = nat.size ↑n)) (size_to_nat n))) (Eq.refl (nat.size ↑n))) @[simp] theorem of_nat'_eq (n : ℕ) : of_nat' n = ↑n := sorry theorem zneg_to_znum (n : num) : -to_znum n = to_znum_neg n := num.cases_on n (Eq.refl (-to_znum zero)) fun (n : pos_num) => Eq.refl (-to_znum (pos n)) theorem zneg_to_znum_neg (n : num) : -to_znum_neg n = to_znum n := num.cases_on n (Eq.refl (-to_znum_neg zero)) fun (n : pos_num) => Eq.refl (-to_znum_neg (pos n)) theorem to_znum_inj {m : num} {n : num} : to_znum m = to_znum n ↔ m = n := sorry @[simp] theorem cast_to_znum {α : Type u_1} [HasZero α] [HasOne α] [Add α] [Neg α] (n : num) : ↑(to_znum n) = ↑n := num.cases_on n (idRhs (↑(to_znum 0) = ↑(to_znum 0)) rfl) fun (n : pos_num) => idRhs (↑(to_znum (pos n)) = ↑(to_znum (pos n))) rfl @[simp] theorem cast_to_znum_neg {α : Type u_1} [add_group α] [HasOne α] (n : num) : ↑(to_znum_neg n) = -↑n := num.cases_on n (idRhs (0 = -0) (Eq.symm neg_zero)) fun (n : pos_num) => idRhs (↑(to_znum_neg (pos n)) = ↑(to_znum_neg (pos n))) rfl @[simp] theorem add_to_znum (m : num) (n : num) : to_znum (m + n) = to_znum m + to_znum n := num.cases_on m (num.cases_on n (Eq.refl (to_znum (zero + zero))) fun (n : pos_num) => Eq.refl (to_znum (zero + pos n))) fun (m : pos_num) => num.cases_on n (Eq.refl (to_znum (pos m + zero))) fun (n : pos_num) => Eq.refl (to_znum (pos m + pos n)) end num namespace pos_num theorem pred_to_nat {n : pos_num} (h : 1 < n) : ↑(pred n) = Nat.pred ↑n := sorry theorem sub'_one (a : pos_num) : sub' a 1 = num.to_znum (pred' a) := pos_num.cases_on a (Eq.refl (sub' one 1)) (fun (a : pos_num) => Eq.refl (sub' (bit1 a) 1)) fun (a : pos_num) => Eq.refl (sub' (bit0 a) 1) theorem one_sub' (a : pos_num) : sub' 1 a = num.to_znum_neg (pred' a) := pos_num.cases_on a (Eq.refl (sub' 1 one)) (fun (a : pos_num) => Eq.refl (sub' 1 (bit1 a))) fun (a : pos_num) => Eq.refl (sub' 1 (bit0 a)) theorem lt_iff_cmp {m : pos_num} {n : pos_num} : m < n ↔ cmp m n = ordering.lt := iff.rfl theorem le_iff_cmp {m : pos_num} {n : pos_num} : m ≤ n ↔ cmp m n ≠ ordering.gt := sorry end pos_num namespace num theorem pred_to_nat (n : num) : ↑(pred n) = Nat.pred ↑n := sorry theorem ppred_to_nat (n : num) : coe <$> ppred n = nat.ppred ↑n := sorry theorem cmp_swap (m : num) (n : num) : ordering.swap (cmp m n) = cmp n m := sorry theorem cmp_eq (m : num) (n : num) : cmp m n = ordering.eq ↔ m = n := sorry @[simp] theorem cast_lt {α : Type u_1} [linear_ordered_semiring α] {m : num} {n : num} : ↑m < ↑n ↔ m < n := sorry @[simp] theorem cast_le {α : Type u_1} [linear_ordered_semiring α] {m : num} {n : num} : ↑m ≤ ↑n ↔ m ≤ n := eq.mpr (id (Eq._oldrec (Eq.refl (↑m ≤ ↑n ↔ m ≤ n)) (Eq.symm (propext not_lt)))) (not_congr cast_lt) @[simp] theorem cast_inj {α : Type u_1} [linear_ordered_semiring α] {m : num} {n : num} : ↑m = ↑n ↔ m = n := sorry theorem lt_iff_cmp {m : num} {n : num} : m < n ↔ cmp m n = ordering.lt := iff.rfl theorem le_iff_cmp {m : num} {n : num} : m ≤ n ↔ cmp m n ≠ ordering.gt := sorry theorem bitwise_to_nat {f : num → num → num} {g : Bool → Bool → Bool} (p : pos_num → pos_num → num) (gff : g false false = false) (f00 : f 0 0 = 0) (f0n : ∀ (n : pos_num), f 0 (pos n) = cond (g false tt) (pos n) 0) (fn0 : ∀ (n : pos_num), f (pos n) 0 = cond (g tt false) (pos n) 0) (fnn : ∀ (m n : pos_num), f (pos m) (pos n) = p m n) (p11 : p 1 1 = cond (g tt tt) 1 0) (p1b : ∀ (b : Bool) (n : pos_num), p 1 (pos_num.bit b n) = bit (g tt b) (cond (g false tt) (pos n) 0)) (pb1 : ∀ (a : Bool) (m : pos_num), p (pos_num.bit a m) 1 = bit (g a tt) (cond (g tt false) (pos m) 0)) (pbb : ∀ (a b : Bool) (m n : pos_num), p (pos_num.bit a m) (pos_num.bit b n) = bit (g a b) (p m n)) (m : num) (n : num) : ↑(f m n) = nat.bitwise g ↑m ↑n := sorry @[simp] theorem lor_to_nat (m : num) (n : num) : ↑(lor m n) = nat.lor ↑m ↑n := sorry @[simp] theorem land_to_nat (m : num) (n : num) : ↑(land m n) = nat.land ↑m ↑n := sorry @[simp] theorem ldiff_to_nat (m : num) (n : num) : ↑(ldiff m n) = nat.ldiff ↑m ↑n := sorry @[simp] theorem lxor_to_nat (m : num) (n : num) : ↑(lxor m n) = nat.lxor ↑m ↑n := sorry @[simp] theorem shiftl_to_nat (m : num) (n : ℕ) : ↑(shiftl m n) = nat.shiftl (↑m) n := sorry @[simp] theorem shiftr_to_nat (m : num) (n : ℕ) : ↑(shiftr m n) = nat.shiftr (↑m) n := sorry @[simp] theorem test_bit_to_nat (m : num) (n : ℕ) : test_bit m n = nat.test_bit (↑m) n := sorry end num namespace znum @[simp] theorem cast_zero {α : Type u_1} [HasZero α] [HasOne α] [Add α] [Neg α] : ↑0 = 0 := rfl @[simp] theorem cast_zero' {α : Type u_1} [HasZero α] [HasOne α] [Add α] [Neg α] : ↑zero = 0 := rfl @[simp] theorem cast_one {α : Type u_1} [HasZero α] [HasOne α] [Add α] [Neg α] : ↑1 = 1 := rfl @[simp] theorem cast_pos {α : Type u_1} [HasZero α] [HasOne α] [Add α] [Neg α] (n : pos_num) : ↑(pos n) = ↑n := rfl @[simp] theorem cast_neg {α : Type u_1} [HasZero α] [HasOne α] [Add α] [Neg α] (n : pos_num) : ↑(neg n) = -↑n := rfl @[simp] theorem cast_zneg {α : Type u_1} [add_group α] [HasOne α] (n : znum) : ↑(-n) = -↑n := znum.cases_on n (idRhs (0 = -0) (Eq.symm neg_zero)) (fun (n : pos_num) => idRhs (↑(-pos n) = ↑(-pos n)) rfl) fun (n : pos_num) => idRhs (↑(-neg n) = --↑(-neg n)) (Eq.symm (neg_neg ↑(-neg n))) theorem neg_zero : -0 = 0 := rfl theorem zneg_pos (n : pos_num) : -pos n = neg n := rfl theorem zneg_neg (n : pos_num) : -neg n = pos n := rfl theorem zneg_zneg (n : znum) : --n = n := znum.cases_on n (Eq.refl ( --zero)) (fun (n : pos_num) => Eq.refl ( --pos n)) fun (n : pos_num) => Eq.refl ( --neg n) theorem zneg_bit1 (n : znum) : -znum.bit1 n = znum.bitm1 (-n) := znum.cases_on n (Eq.refl (-znum.bit1 zero)) (fun (n : pos_num) => Eq.refl (-znum.bit1 (pos n))) fun (n : pos_num) => Eq.refl (-znum.bit1 (neg n)) theorem zneg_bitm1 (n : znum) : -znum.bitm1 n = znum.bit1 (-n) := znum.cases_on n (Eq.refl (-znum.bitm1 zero)) (fun (n : pos_num) => Eq.refl (-znum.bitm1 (pos n))) fun (n : pos_num) => Eq.refl (-znum.bitm1 (neg n)) theorem zneg_succ (n : znum) : -succ n = pred (-n) := sorry theorem zneg_pred (n : znum) : -pred n = succ (-n) := eq.mpr (id (Eq._oldrec (Eq.refl (-pred n = succ (-n))) (Eq.symm (zneg_zneg (succ (-n)))))) (eq.mpr (id (Eq._oldrec (Eq.refl (-pred n = --succ (-n))) (zneg_succ (-n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (-pred n = -pred ( --n))) (zneg_zneg n))) (Eq.refl (-pred n)))) @[simp] theorem neg_of_int (n : ℤ) : ↑(-n) = -↑n := int.cases_on n (fun (n : ℕ) => nat.cases_on n (idRhs (↑(-0) = ↑(-0)) rfl) fun (n : ℕ) => idRhs (↑(-↑(n + 1)) = ↑(-↑(n + 1))) rfl) fun (n : ℕ) => idRhs (↑(-Int.negSucc n) = --↑(-Int.negSucc n)) (Eq.symm (zneg_zneg ↑(-Int.negSucc n))) @[simp] theorem abs_to_nat (n : znum) : ↑(abs n) = int.nat_abs ↑n := sorry @[simp] theorem abs_to_znum (n : num) : abs (num.to_znum n) = n := num.cases_on n (idRhs (abs (num.to_znum 0) = abs (num.to_znum 0)) rfl) fun (n : pos_num) => idRhs (abs (num.to_znum (num.pos n)) = abs (num.to_znum (num.pos n))) rfl @[simp] theorem cast_to_int {α : Type u_1} [add_group α] [HasOne α] (n : znum) : ↑↑n = ↑n := sorry theorem bit0_of_bit0 (n : znum) : bit0 n = znum.bit0 n := znum.cases_on n (idRhs (bit0 0 = bit0 0) rfl) (fun (n : pos_num) => idRhs (pos (bit0 n) = pos (pos_num.bit0 n)) (congr_arg pos (pos_num.bit0_of_bit0 n))) fun (n : pos_num) => idRhs (neg (bit0 n) = neg (pos_num.bit0 n)) (congr_arg neg (pos_num.bit0_of_bit0 n)) theorem bit1_of_bit1 (n : znum) : bit1 n = znum.bit1 n := sorry @[simp] theorem cast_bit0 {α : Type u_1} [add_group α] [HasOne α] (n : znum) : ↑(znum.bit0 n) = bit0 ↑n := sorry @[simp] theorem cast_bit1 {α : Type u_1} [add_group α] [HasOne α] (n : znum) : ↑(znum.bit1 n) = bit1 ↑n := sorry @[simp] theorem cast_bitm1 {α : Type u_1} [add_group α] [HasOne α] (n : znum) : ↑(znum.bitm1 n) = bit0 ↑n - 1 := sorry theorem add_zero (n : znum) : n + 0 = n := znum.cases_on n (Eq.refl (zero + 0)) (fun (n : pos_num) => Eq.refl (pos n + 0)) fun (n : pos_num) => Eq.refl (neg n + 0) theorem zero_add (n : znum) : 0 + n = n := znum.cases_on n (Eq.refl (0 + zero)) (fun (n : pos_num) => Eq.refl (0 + pos n)) fun (n : pos_num) => Eq.refl (0 + neg n) theorem add_one (n : znum) : n + 1 = succ n := sorry end znum namespace pos_num theorem cast_to_znum (n : pos_num) : ↑n = znum.pos n := sorry theorem cast_sub' {α : Type u_1} [add_group α] [HasOne α] (m : pos_num) (n : pos_num) : ↑(sub' m n) = ↑m - ↑n := sorry theorem to_nat_eq_succ_pred (n : pos_num) : ↑n = ↑(pred' n) + 1 := eq.mpr (id (Eq._oldrec (Eq.refl (↑n = ↑(pred' n) + 1)) (Eq.symm (num.succ'_to_nat (pred' n))))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑n = ↑(num.succ' (pred' n)))) (succ'_pred' n))) (Eq.refl ↑n)) theorem to_int_eq_succ_pred (n : pos_num) : ↑n = ↑↑(pred' n) + 1 := eq.mpr (id (Eq._oldrec (Eq.refl (↑n = ↑↑(pred' n) + 1)) (Eq.symm (to_nat_to_int n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = ↑↑(pred' n) + 1)) (to_nat_eq_succ_pred n))) (Eq.refl ↑(↑(pred' n) + 1))) end pos_num namespace num @[simp] theorem cast_sub' {α : Type u_1} [add_group α] [HasOne α] (m : num) (n : num) : ↑(sub' m n) = ↑m - ↑n := sorry @[simp] theorem of_nat_to_znum (n : ℕ) : to_znum ↑n = ↑n := sorry @[simp] theorem of_nat_to_znum_neg (n : ℕ) : to_znum_neg ↑n = -↑n := eq.mpr (id (Eq._oldrec (Eq.refl (to_znum_neg ↑n = -↑n)) (Eq.symm (of_nat_to_znum n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (to_znum_neg ↑n = -to_znum ↑n)) (zneg_to_znum ↑n))) (Eq.refl (to_znum_neg ↑n))) theorem mem_of_znum' {m : num} {n : znum} : m ∈ of_znum' n ↔ n = to_znum m := sorry theorem of_znum'_to_nat (n : znum) : coe <$> of_znum' n = int.to_nat' ↑n := sorry @[simp] theorem of_znum_to_nat (n : znum) : ↑(of_znum n) = int.to_nat ↑n := sorry @[simp] theorem cast_of_znum {α : Type u_1} [add_group α] [HasOne α] (n : znum) : ↑(of_znum n) = ↑(int.to_nat ↑n) := eq.mpr (id (Eq._oldrec (Eq.refl (↑(of_znum n) = ↑(int.to_nat ↑n))) (Eq.symm (cast_to_nat (of_znum n))))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑↑(of_znum n) = ↑(int.to_nat ↑n))) (of_znum_to_nat n))) (Eq.refl ↑(int.to_nat ↑n))) @[simp] theorem sub_to_nat (m : num) (n : num) : ↑(m - n) = ↑m - ↑n := sorry end num namespace znum @[simp] theorem cast_add {α : Type u_1} [add_group α] [HasOne α] (m : znum) (n : znum) : ↑(m + n) = ↑m + ↑n := sorry @[simp] theorem cast_succ {α : Type u_1} [add_group α] [HasOne α] (n : znum) : ↑(succ n) = ↑n + 1 := eq.mpr (id (Eq._oldrec (Eq.refl (↑(succ n) = ↑n + 1)) (Eq.symm (add_one n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(n + 1) = ↑n + 1)) (cast_add n 1))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑n + ↑1 = ↑n + 1)) cast_one)) (Eq.refl (↑n + 1)))) @[simp] theorem mul_to_int (m : znum) (n : znum) : ↑(m * n) = ↑m * ↑n := sorry theorem cast_mul {α : Type u_1} [ring α] (m : znum) (n : znum) : ↑(m * n) = ↑m * ↑n := sorry @[simp] theorem of_to_int (n : znum) : ↑↑n = n := sorry theorem to_of_int (n : ℤ) : ↑↑n = n := sorry theorem to_int_inj {m : znum} {n : znum} : ↑m = ↑n ↔ m = n := { mp := fun (h : ↑m = ↑n) => function.left_inverse.injective of_to_int h, mpr := congr_arg fun (x : znum) => ↑x } @[simp] theorem of_int_cast {α : Type u_1} [add_group α] [HasOne α] (n : ℤ) : ↑↑n = ↑n := eq.mpr (id (Eq._oldrec (Eq.refl (↑↑n = ↑n)) (Eq.symm (cast_to_int ↑n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑↑↑n = ↑n)) (to_of_int n))) (Eq.refl ↑n)) @[simp] theorem of_nat_cast {α : Type u_1} [add_group α] [HasOne α] (n : ℕ) : ↑↑n = ↑n := of_int_cast ↑n @[simp] theorem of_int'_eq (n : ℤ) : of_int' n = ↑n := sorry theorem cmp_to_int (m : znum) (n : znum) : ordering.cases_on (cmp m n) (↑m < ↑n) (m = n) (↑n < ↑m) := sorry theorem lt_to_int {m : znum} {n : znum} : ↑m < ↑n ↔ m < n := sorry theorem le_to_int {m : znum} {n : znum} : ↑m ≤ ↑n ↔ m ≤ n := eq.mpr (id (Eq._oldrec (Eq.refl (↑m ≤ ↑n ↔ m ≤ n)) (Eq.symm (propext not_lt)))) (not_congr lt_to_int) @[simp] theorem cast_lt {α : Type u_1} [linear_ordered_ring α] {m : znum} {n : znum} : ↑m < ↑n ↔ m < n := sorry @[simp] theorem cast_le {α : Type u_1} [linear_ordered_ring α] {m : znum} {n : znum} : ↑m ≤ ↑n ↔ m ≤ n := eq.mpr (id (Eq._oldrec (Eq.refl (↑m ≤ ↑n ↔ m ≤ n)) (Eq.symm (propext not_lt)))) (not_congr cast_lt) @[simp] theorem cast_inj {α : Type u_1} [linear_ordered_ring α] {m : znum} {n : znum} : ↑m = ↑n ↔ m = n := sorry /-- This tactic tries to turn an (in)equality about `znum`s to one about `int`s by rewriting. ```lean example (n : znum) (m : znum) : n ≤ n + m * m := begin znum.transfer_rw, exact le_add_of_nonneg_right (mul_self_nonneg _) end ``` -/ /-- This tactic tries to prove (in)equalities about `znum`s by transfering them to the `int` world and then trying to call `simp`. ```lean example (n : znum) (m : znum) : n ≤ n + m * m := begin znum.transfer, exact mul_self_nonneg _ end ``` -/ protected instance linear_order : linear_order znum := linear_order.mk LessEq Less sorry sorry sorry sorry znum.decidable_le znum.decidable_eq znum.decidable_lt protected instance add_comm_group : add_comm_group znum := add_comm_group.mk Add.add sorry 0 zero_add add_zero Neg.neg (add_group.sub._default Add.add sorry 0 zero_add add_zero Neg.neg) sorry sorry protected instance linear_ordered_comm_ring : linear_ordered_comm_ring znum := linear_ordered_comm_ring.mk add_comm_group.add add_comm_group.add_assoc add_comm_group.zero add_comm_group.zero_add add_comm_group.add_zero add_comm_group.neg add_comm_group.sub add_comm_group.add_left_neg add_comm_group.add_comm Mul.mul sorry 1 sorry sorry sorry sorry linear_order.le linear_order.lt linear_order.le_refl linear_order.le_trans linear_order.le_antisymm sorry sorry sorry linear_order.le_total linear_order.decidable_le linear_order.decidable_eq linear_order.decidable_lt sorry sorry @[simp] theorem dvd_to_int (m : znum) (n : znum) : ↑m ∣ ↑n ↔ m ∣ n := sorry end znum namespace pos_num theorem divmod_to_nat_aux {n : pos_num} {d : pos_num} {q : num} {r : num} (h₁ : ↑r + ↑d * bit0 ↑q = ↑n) (h₂ : ↑r < bit0 1 * ↑d) : ↑(prod.snd (divmod_aux d q r)) + ↑d * ↑(prod.fst (divmod_aux d q r)) = ↑n ∧ ↑(prod.snd (divmod_aux d q r)) < ↑d := sorry theorem divmod_to_nat (d : pos_num) (n : pos_num) : ↑n / ↑d = ↑(prod.fst (divmod d n)) ∧ ↑n % ↑d = ↑(prod.snd (divmod d n)) := sorry @[simp] theorem div'_to_nat (n : pos_num) (d : pos_num) : ↑(div' n d) = ↑n / ↑d := Eq.symm (and.left (divmod_to_nat d n)) @[simp] theorem mod'_to_nat (n : pos_num) (d : pos_num) : ↑(mod' n d) = ↑n % ↑d := Eq.symm (and.right (divmod_to_nat d n)) end pos_num namespace num @[simp] theorem div_to_nat (n : num) (d : num) : ↑(n / d) = ↑n / ↑d := sorry @[simp] theorem mod_to_nat (n : num) (d : num) : ↑(n % d) = ↑n % ↑d := sorry theorem gcd_to_nat_aux {n : ℕ} {a : num} {b : num} : a ≤ b → nat_size (a * b) ≤ n → ↑(gcd_aux n a b) = nat.gcd ↑a ↑b := sorry @[simp] theorem gcd_to_nat (a : num) (b : num) : ↑(gcd a b) = nat.gcd ↑a ↑b := sorry theorem dvd_iff_mod_eq_zero {m : num} {n : num} : m ∣ n ↔ n % m = 0 := sorry protected instance decidable_dvd : DecidableRel has_dvd.dvd := sorry end num protected instance pos_num.decidable_dvd : DecidableRel has_dvd.dvd := sorry namespace znum @[simp] theorem div_to_int (n : znum) (d : znum) : ↑(n / d) = ↑n / ↑d := sorry @[simp] theorem mod_to_int (n : znum) (d : znum) : ↑(n % d) = ↑n % ↑d := sorry @[simp] theorem gcd_to_nat (a : znum) (b : znum) : ↑(gcd a b) = int.gcd ↑a ↑b := sorry theorem dvd_iff_mod_eq_zero {m : znum} {n : znum} : m ∣ n ↔ n % m = 0 := sorry protected instance has_dvd.dvd.decidable_rel : DecidableRel has_dvd.dvd := sorry end znum namespace int /-- Cast a `snum` to the corresponding integer. -/ def of_snum : snum → ℤ := snum.rec' (fun (a : Bool) => cond a (-1) 0) fun (a : Bool) (p : snum) (IH : ℤ) => cond a (bit1 IH) (bit0 IH) end int protected instance int.snum_coe : has_coe snum ℤ := has_coe.mk int.of_snum protected instance snum.has_lt : HasLess snum := { Less := fun (a b : snum) => ↑a < ↑b } protected instance snum.has_le : HasLessEq snum := { LessEq := fun (a b : snum) => ↑a ≤ ↑b }
7ec4891f70b2684ccf50fbc86f8f2902110913f5
4b846d8dabdc64e7ea03552bad8f7fa74763fc67
/tests/lean/run/my_tac_class.lean
f20974ca5e1dd34dd28a2b3d30cdbe560be99555
[ "Apache-2.0" ]
permissive
pacchiano/lean
9324b33f3ac3b5c5647285160f9f6ea8d0d767dc
fdadada3a970377a6df8afcd629a6f2eab6e84e8
refs/heads/master
1,611,357,380,399
1,489,870,101,000
1,489,870,101,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,739
lean
meta def mytac := state_t nat tactic meta instance : monad mytac := state_t.monad _ _ meta instance : monad.has_monad_lift tactic mytac := monad.monad_transformer_lift (state_t nat) tactic meta instance (α : Type) : has_coe (tactic α) (mytac α) := ⟨monad.monad_lift⟩ namespace mytac meta def step {α : Type} (t : mytac α) : mytac unit := t >> return () meta def rstep {α : Type} (line : nat) (col : nat) (t : mytac α) : mytac unit := λ v s, result.cases_on (@scope_trace _ line col (t v s)) (λ ⟨a, v⟩ new_s, result.success ((), v) new_s) (λ opt_msg_thunk e new_s, match opt_msg_thunk with | some msg_thunk := let msg := msg_thunk () ++ format.line ++ to_fmt "value: " ++ to_fmt v ++ format.line ++ to_fmt "state:" ++ format.line ++ new_s^.to_format in (tactic.report_error line col msg >> interaction_monad.silent_fail) new_s | none := interaction_monad.silent_fail new_s end) meta def execute (tac : mytac unit) : tactic unit := tac 0 >> return () meta def save_info (p : pos) : mytac unit := do v ← state_t.read, s ← tactic.read, tactic.save_info_thunk p (λ _, to_fmt "Custom state: " ++ to_fmt v ++ format.line ++ tactic_state.to_format s) namespace interactive meta def intros : mytac unit := tactic.intros >> return () meta def constructor : mytac unit := tactic.constructor meta def trace (s : string) : mytac unit := tactic.trace s meta def assumption : mytac unit := tactic.assumption meta def inc : mytac unit := do v ← state_t.read, state_t.write (v+1) end interactive end mytac example (p q : Prop) : p → q → p ∧ q := begin [mytac] intros, inc, trace "test", constructor, inc, assumption, assumption end
cb141e2250f86aa57a85d7b339df2cdc4ac25e88
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/type_check_mvar.lean
d5a1c4396b69daf21de31c175062a40ce029607c
[ "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
197
lean
open tactic example : true := by do eq ← mk_const ``eq, ty ← mk_meta_univ >>= mk_meta_var ∘ expr.sort, let zero := `(0 : ℕ), let e := eq ty zero zero, success_if_fail $ type_check e, triv
1b884395ba13381e70db78b5e1c70e57b83ff5eb
aa5a655c05e5359a70646b7154e7cac59f0b4132
/src/Init/Data/Nat/Basic.lean
379186c83ca0857b1f0fae5f51751d45947e9909
[ "Apache-2.0" ]
permissive
lambdaxymox/lean4
ae943c960a42247e06eff25c35338268d07454cb
278d47c77270664ef29715faab467feac8a0f446
refs/heads/master
1,677,891,867,340
1,612,500,005,000
1,612,500,005,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,115
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import Init.Core universes u namespace Nat @[specialize] def foldAux {α : Type u} (f : Nat → α → α) (s : Nat) : Nat → α → α | 0, a => a | succ n, a => foldAux f s n (f (s - (succ n)) a) @[inline] def fold {α : Type u} (f : Nat → α → α) (n : Nat) (init : α) : α := foldAux f n n init @[inline] def foldRev {α : Type u} (f : Nat → α → α) (n : Nat) (init : α) : α := let rec @[specialize] loop | 0, a => a | succ n, a => loop n (f n a) loop n init @[specialize] def anyAux (f : Nat → Bool) (s : Nat) : Nat → Bool | 0 => false | succ n => f (s - (succ n)) || anyAux f s n /- `any f n = true` iff there is `i in [0, n-1]` s.t. `f i = true` -/ @[inline] def any (f : Nat → Bool) (n : Nat) : Bool := anyAux f n n @[inline] def all (f : Nat → Bool) (n : Nat) : Bool := !any (fun i => !f i) n @[inline] def repeat {α : Type u} (f : α → α) (n : Nat) (a : α) : α := let rec @[specialize] loop | 0, a => a | succ n, a => loop n (f a) loop n a /- Nat.add theorems -/ protected theorem zeroAdd : ∀ (n : Nat), 0 + n = n | 0 => rfl | n+1 => congrArg succ (Nat.zeroAdd n) theorem succAdd : ∀ (n m : Nat), (succ n) + m = succ (n + m) | n, 0 => rfl | n, m+1 => congrArg succ (succAdd n m) theorem addSucc (n m : Nat) : n + succ m = succ (n + m) := rfl protected theorem addZero (n : Nat) : n + 0 = n := rfl theorem addOne (n : Nat) : n + 1 = succ n := rfl theorem succEqAddOne (n : Nat) : succ n = n + 1 := rfl protected theorem addComm : ∀ (n m : Nat), n + m = m + n | n, 0 => Eq.symm (Nat.zeroAdd n) | n, m+1 => by have succ (n + m) = succ (m + n) by apply congrArg; apply Nat.addComm rw [succAdd m n] apply this protected theorem addAssoc : ∀ (n m k : Nat), (n + m) + k = n + (m + k) | n, m, 0 => rfl | n, m, succ k => congrArg succ (Nat.addAssoc n m k) protected theorem addLeftComm (n m k : Nat) : n + (m + k) = m + (n + k) := by rw [← Nat.addAssoc, Nat.addComm n m, Nat.addAssoc] protected theorem addRightComm (n m k : Nat) : (n + m) + k = (n + k) + m := by rw [Nat.addAssoc, Nat.addComm m k, ← Nat.addAssoc] protected theorem addLeftCancel : ∀ {n m k : Nat}, n + m = n + k → m = k | 0, m, k, h => Nat.zeroAdd m ▸ Nat.zeroAdd k ▸ h | succ n, m, k, h => have n+m = n+k from have succ (n + m) = succ (n + k) from succAdd n m ▸ succAdd n k ▸ h Nat.noConfusion this id Nat.addLeftCancel this protected theorem addRightCancel {n m k : Nat} (h : n + m = k + m) : n = k := have m + n = m + k from Nat.addComm n m ▸ Nat.addComm k m ▸ h Nat.addLeftCancel this /- Nat.mul theorems -/ protected theorem mulZero (n : Nat) : n * 0 = 0 := rfl theorem mulSucc (n m : Nat) : n * succ m = n * m + n := rfl protected theorem zeroMul : ∀ (n : Nat), 0 * n = 0 | 0 => rfl | succ n => mulSucc 0 n ▸ (Nat.zeroMul n).symm ▸ rfl theorem succMul : ∀ (n m : Nat), (succ n) * m = (n * m) + m | n, 0 => rfl | n, succ m => by have succ (n * m + m + n) = succ (n * m + n + m) from congrArg succ (Nat.addRightComm ..) rw [mulSucc n m, mulSucc (succ n) m, succMul n m] assumption protected theorem mulComm : ∀ (n m : Nat), n * m = m * n | n, 0 => (Nat.zeroMul n).symm ▸ (Nat.mulZero n).symm ▸ rfl | n, succ m => (mulSucc n m).symm ▸ (succMul m n).symm ▸ (Nat.mulComm n m).symm ▸ rfl protected theorem mulOne : ∀ (n : Nat), n * 1 = n := Nat.zeroAdd protected theorem oneMul (n : Nat) : 1 * n = n := Nat.mulComm n 1 ▸ Nat.mulOne n protected theorem leftDistrib : ∀ (n m k : Nat), n * (m + k) = n * m + n * k | 0, m, k => (Nat.zeroMul (m + k)).symm ▸ (Nat.zeroMul m).symm ▸ (Nat.zeroMul k).symm ▸ rfl | succ n, m, k => have h₁ : succ n * (m + k) = n * (m + k) + (m + k) from succMul .. have h₂ : n * (m + k) + (m + k) = (n * m + n * k) + (m + k) from Nat.leftDistrib n m k ▸ rfl have h₃ : (n * m + n * k) + (m + k) = n * m + (n * k + (m + k)) from Nat.addAssoc .. have h₄ : n * m + (n * k + (m + k)) = n * m + (m + (n * k + k)) from congrArg (fun x => n*m + x) (Nat.addLeftComm ..) have h₅ : n * m + (m + (n * k + k)) = (n * m + m) + (n * k + k) from (Nat.addAssoc ..).symm have h₆ : (n * m + m) + (n * k + k) = (n * m + m) + succ n * k from succMul n k ▸ rfl have h₇ : (n * m + m) + succ n * k = succ n * m + succ n * k from succMul n m ▸ rfl (((((h₁.trans h₂).trans h₃).trans h₄).trans h₅).trans h₆).trans h₇ protected theorem rightDistrib (n m k : Nat) : (n + m) * k = n * k + m * k := have h₁ : (n + m) * k = k * (n + m) from Nat.mulComm .. have h₂ : k * (n + m) = k * n + k * m from Nat.leftDistrib .. have h₃ : k * n + k * m = n * k + k * m from Nat.mulComm n k ▸ rfl have h₄ : n * k + k * m = n * k + m * k from Nat.mulComm m k ▸ rfl ((h₁.trans h₂).trans h₃).trans h₄ protected theorem mulAssoc : ∀ (n m k : Nat), (n * m) * k = n * (m * k) | n, m, 0 => rfl | n, m, succ k => have h₁ : n * m * succ k = n * m * (k + 1) from rfl have h₂ : n * m * (k + 1) = (n * m * k) + n * m * 1 from Nat.leftDistrib .. have h₃ : (n * m * k) + n * m * 1 = (n * m * k) + n * m by rw [Nat.mulOne (n*m)] have h₄ : (n * m * k) + n * m = (n * (m * k)) + n * m by rw [Nat.mulAssoc n m k] have h₅ : (n * (m * k)) + n * m = n * (m * k + m) from (Nat.leftDistrib n (m*k) m).symm have h₆ : n * (m * k + m) = n * (m * succ k) from Nat.mulSucc m k ▸ rfl ((((h₁.trans h₂).trans h₃).trans h₄).trans h₅).trans h₆ /- Inequalities -/ theorem succLtSucc {n m : Nat} : n < m → succ n < succ m := succLeSucc theorem ltSuccOfLe {n m : Nat} : n ≤ m → n < succ m := succLeSucc protected theorem subZero (n : Nat) : n - 0 = n := rfl theorem succSubSuccEqSub (n m : Nat) : succ n - succ m = n - m := by induction m with | zero => exact rfl | succ m ih => apply congrArg pred ih theorem notSuccLeSelf (n : Nat) : ¬succ n ≤ n := by induction n with | zero => intro h; apply notSuccLeZero 0 h | succ n ih => intro h; exact ih (leOfSuccLeSucc h) protected theorem ltIrrefl (n : Nat) : ¬n < n := notSuccLeSelf n theorem predLe : ∀ (n : Nat), pred n ≤ n | zero => rfl | succ n => leSucc _ theorem predLt : ∀ {n : Nat}, n ≠ 0 → pred n < n | zero, h => absurd rfl h | succ n, h => ltSuccOfLe (Nat.leRefl _) theorem subLe (n m : Nat) : n - m ≤ n := by induction m with | zero => exact Nat.leRefl (n - 0) | succ m ih => apply Nat.leTrans (predLe (n - m)) ih theorem subLt : ∀ {n m : Nat}, 0 < n → 0 < m → n - m < n | 0, m, h1, h2 => absurd h1 (Nat.ltIrrefl 0) | n+1, 0, h1, h2 => absurd h2 (Nat.ltIrrefl 0) | n+1, m+1, h1, h2 => Eq.symm (succSubSuccEqSub n m) ▸ show n - m < succ n from ltSuccOfLe (subLe n m) protected theorem ltOfLtOfLe {n m k : Nat} : n < m → m ≤ k → n < k := Nat.leTrans protected theorem ltOfLtOfEq {n m k : Nat} : n < m → m = k → n < k := fun h₁ h₂ => h₂ ▸ h₁ protected theorem leOfEq {n m : Nat} (p : n = m) : n ≤ m := p ▸ Nat.leRefl n theorem leOfSuccLe {n m : Nat} (h : succ n ≤ m) : n ≤ m := Nat.leTrans (leSucc n) h protected theorem leOfLt {n m : Nat} (h : n < m) : n ≤ m := leOfSuccLe h def lt.step {n m : Nat} : n < m → n < succ m := leStep def succPos := zeroLtSucc theorem eqZeroOrPos : ∀ (n : Nat), n = 0 ∨ n > 0 | 0 => Or.inl rfl | n+1 => Or.inr (succPos _) protected theorem ltOfLeOfLt {n m k : Nat} (h₁ : n ≤ m) : m < k → n < k := Nat.leTrans (succLeSucc h₁) def lt.base (n : Nat) : n < succ n := Nat.leRefl (succ n) theorem ltSuccSelf (n : Nat) : n < succ n := lt.base n protected theorem leTotal (m n : Nat) : m ≤ n ∨ n ≤ m := match Nat.ltOrGe m n with | Or.inl h => Or.inl (Nat.leOfLt h) | Or.inr h => Or.inr h protected theorem ltOfLeAndNe {m n : Nat} (h₁ : m ≤ n) (h₂ : m ≠ n) : m < n := match Nat.eqOrLtOfLe h₁ with | Or.inl h => absurd h h₂ | Or.inr h => h theorem eqZeroOfLeZero {n : Nat} (h : n ≤ 0) : n = 0 := Nat.leAntisymm h (zeroLe _) theorem ltOfSuccLt {n m : Nat} : succ n < m → n < m := leOfSuccLe theorem ltOfSuccLtSucc {n m : Nat} : succ n < succ m → n < m := leOfSuccLeSucc theorem ltOfSuccLe {n m : Nat} (h : succ n ≤ m) : n < m := h theorem succLeOfLt {n m : Nat} (h : n < m) : succ n ≤ m := h theorem ltOrEqOrLeSucc {m n : Nat} (h : m ≤ succ n) : m ≤ n ∨ m = succ n := Decidable.byCases (fun (h' : m = succ n) => Or.inr h') (fun (h' : m ≠ succ n) => have m < succ n from Nat.ltOfLeAndNe h h' have succ m ≤ succ n from succLeOfLt this Or.inl (leOfSuccLeSucc this)) theorem leAddRight : ∀ (n k : Nat), n ≤ n + k | n, 0 => Nat.leRefl n | n, k+1 => leSuccOfLe (leAddRight n k) theorem leAddLeft (n m : Nat): n ≤ m + n := Nat.addComm n m ▸ leAddRight n m theorem le.dest : ∀ {n m : Nat}, n ≤ m → Exists (fun k => n + k = m) | zero, zero, h => ⟨0, rfl⟩ | zero, succ n, h => ⟨succ n, Nat.addComm 0 (succ n) ▸ rfl⟩ | succ n, zero, h => Bool.noConfusion h | succ n, succ m, h => have n ≤ m from h have Exists (fun k => n + k = m) from dest this match this with | ⟨k, h⟩ => ⟨k, show succ n + k = succ m from ((succAdd n k).symm ▸ h ▸ rfl)⟩ theorem le.intro {n m k : Nat} (h : n + k = m) : n ≤ m := h ▸ leAddRight n k protected theorem notLeOfGt {n m : Nat} (h : n > m) : ¬ n ≤ m := fun h₁ => match Nat.ltOrGe n m with | Or.inl h₂ => absurd (Nat.ltTrans h h₂) (Nat.ltIrrefl _) | Or.inr h₂ => have Heq : n = m from Nat.leAntisymm h₁ h₂ absurd (@Eq.subst _ _ _ _ Heq h) (Nat.ltIrrefl m) theorem gtOfNotLe {n m : Nat} (h : ¬ n ≤ m) : n > m := match Nat.ltOrGe m n with | Or.inl h₁ => h₁ | Or.inr h₁ => absurd h₁ h protected theorem addLeAddLeft {n m : Nat} (h : n ≤ m) (k : Nat) : k + n ≤ k + m := match le.dest h with | ⟨w, hw⟩ => have h₁ : k + n + w = k + (n + w) from Nat.addAssoc .. have h₂ : k + (n + w) = k + m from congrArg _ hw le.intro <| h₁.trans h₂ protected theorem addLeAddRight {n m : Nat} (h : n ≤ m) (k : Nat) : n + k ≤ m + k := by rw [Nat.addComm n k, Nat.addComm m k] apply Nat.addLeAddLeft assumption protected theorem addLtAddLeft {n m : Nat} (h : n < m) (k : Nat) : k + n < k + m := ltOfSuccLe (addSucc k n ▸ Nat.addLeAddLeft (succLeOfLt h) k) protected theorem addLtAddRight {n m : Nat} (h : n < m) (k : Nat) : n + k < m + k := Nat.addComm k m ▸ Nat.addComm k n ▸ Nat.addLtAddLeft h k protected theorem zeroLtOne : 0 < (1:Nat) := zeroLtSucc 0 theorem addLeAdd {a b c d : Nat} (h₁ : a ≤ b) (h₂ : c ≤ d) : a + c ≤ b + d := Nat.leTrans (Nat.addLeAddRight h₁ c) (Nat.addLeAddLeft h₂ b) theorem addLtAdd {a b c d : Nat} (h₁ : a < b) (h₂ : c < d) : a + c < b + d := Nat.ltTrans (Nat.addLtAddRight h₁ c) (Nat.addLtAddLeft h₂ b) /- Basic theorems for comparing numerals -/ theorem natZeroEqZero : Nat.zero = 0 := rfl protected theorem oneNeZero : 1 ≠ (0 : Nat) := fun h => Nat.noConfusion h protected theorem zeroNeOne : 0 ≠ (1 : Nat) := fun h => Nat.noConfusion h theorem succNeZero (n : Nat) : succ n ≠ 0 := fun h => Nat.noConfusion h /- mul + order -/ theorem mulLeMulLeft {n m : Nat} (k : Nat) (h : n ≤ m) : k * n ≤ k * m := match le.dest h with | ⟨l, hl⟩ => have k * n + k * l = k * m from Nat.leftDistrib k n l ▸ hl.symm ▸ rfl le.intro this theorem mulLeMulRight {n m : Nat} (k : Nat) (h : n ≤ m) : n * k ≤ m * k := Nat.mulComm k m ▸ Nat.mulComm k n ▸ mulLeMulLeft k h protected theorem mulLeMul {n₁ m₁ n₂ m₂ : Nat} (h₁ : n₁ ≤ n₂) (h₂ : m₁ ≤ m₂) : n₁ * m₁ ≤ n₂ * m₂ := Nat.leTrans (mulLeMulRight _ h₁) (mulLeMulLeft _ h₂) protected theorem mulLtMulOfPosLeft {n m k : Nat} (h : n < m) (hk : k > 0) : k * n < k * m := Nat.ltOfLtOfLe (Nat.addLtAddLeft hk _) (Nat.mulSucc k n ▸ Nat.mulLeMulLeft k (succLeOfLt h)) protected theorem mulLtMulOfPosRight {n m k : Nat} (h : n < m) (hk : k > 0) : n * k < m * k := Nat.mulComm k m ▸ Nat.mulComm k n ▸ Nat.mulLtMulOfPosLeft h hk protected theorem mulPos {n m : Nat} (ha : n > 0) (hb : m > 0) : n * m > 0 := have h : 0 * m < n * m from Nat.mulLtMulOfPosRight ha hb Nat.zeroMul m ▸ h /- power -/ theorem powSucc (n m : Nat) : n^(succ m) = n^m * n := rfl theorem powZero (n : Nat) : n^0 = 1 := rfl theorem powLePowOfLeLeft {n m : Nat} (h : n ≤ m) : ∀ (i : Nat), n^i ≤ m^i | 0 => Nat.leRefl _ | succ i => Nat.mulLeMul (powLePowOfLeLeft h i) h theorem powLePowOfLeRight {n : Nat} (hx : n > 0) {i : Nat} : ∀ {j}, i ≤ j → n^i ≤ n^j | 0, h => have i = 0 from eqZeroOfLeZero h this.symm ▸ Nat.leRefl _ | succ j, h => match ltOrEqOrLeSucc h with | Or.inl h => show n^i ≤ n^j * n from have n^i * 1 ≤ n^j * n from Nat.mulLeMul (powLePowOfLeRight hx h) hx Nat.mulOne (n^i) ▸ this | Or.inr h => h.symm ▸ Nat.leRefl _ theorem posPowOfPos {n : Nat} (m : Nat) (h : 0 < n) : 0 < n^m := powLePowOfLeRight h (Nat.zeroLe _) /- min/max -/ protected def min (n m : Nat) : Nat := if n ≤ m then n else m protected def max (n m : Nat) : Nat := if n ≤ m then m else n end Nat namespace Prod @[inline] def foldI {α : Type u} (f : Nat → α → α) (i : Nat × Nat) (a : α) : α := Nat.foldAux f i.2 (i.2 - i.1) a @[inline] def anyI (f : Nat → Bool) (i : Nat × Nat) : Bool := Nat.anyAux f i.2 (i.2 - i.1) @[inline] def allI (f : Nat → Bool) (i : Nat × Nat) : Bool := Nat.anyAux (fun a => !f a) i.2 (i.2 - i.1) end Prod
33cd6f01399aacfca6fea7a0b7382aa5d3a437f8
6950a6e5cebf75da9b91f42789baf52514655111
/hol/problem_translation_hol.lean
816b1a5dda73a2e1ed6ed636ebbad3fdcd835928
[]
no_license
phlippe/Lean_hammer
a6d0a1af09fbce0c58b801032099b9b91d49ecf0
2116279b9c6b334f5b661e4abf4561368cca2391
refs/heads/master
1,587,486,769,513
1,561,466,931,000
1,561,466,931,000
169,705,506
0
1
null
1,550,228,564,000
1,549,614,939,000
Lean
UTF-8
Lean
false
false
4,554
lean
import .datastructures_hol import .translation_hol import .boolean_free_HOL --###################################### --## Translating declarations for HOL ## --###################################### meta def translate_axiom_expression: expr → hammer_tactic unit -- | `() | `(%%l = %%r) := -- c = t : τ do (τ, _) ← using_hammer $ tactic.infer_type r, -- Get type of right side -- tactic.trace l, -- tactic.trace r, lip <- lives_in_prop_p τ, -- Check whether τ is from type prop (and therefore right side a proof) or not if lip then -- If yes, add new axiom of F(τ) (TODO: and name c) do fe1 ← (hammer_f τ), Cn ← mk_fresh_name, add_axiom Cn fe1 else -- Otherwise, add type checker G(c,τ) as new axiom do lit <- lives_in_prop_p r, -- Check if τ=Prop (or rather t : Prop) if lit then -- If yes, add new axiom c ↔ F(t) do (fe1,_) <- using_hammer (hammer_f l), (fe2,_) <- using_hammer (hammer_f r), Cn ← mk_fresh_name, add_axiom Cn (holform.iff fe1 fe2) else -- Otherwise, check whether τ is from type Set or Type, or another type do Cn <- mk_fresh_name, (Cc,_) <- using_hammer (hammer_c r), (Ct,_) <- using_hammer (hammer_c l), add_axiom Cn (holform.eq Cc Ct) -- Inductive declarations are handled in advance. Thus we don't have to check for them anymore | `(%%c : _) := -- c : τ do (τ, _) ← using_hammer $ tactic.infer_type c, -- Get type of right side -- tactic.trace c, lip <- lives_in_prop_p τ, -- Check whether τ is from type prop (and therefore right side a proof) or not -- tactic.trace τ, -- Note that if τ is Prop itself, then the type checker G ... will take care of it. if lip then do Cn <- mk_fresh_name, (Fτ,_) <- using_hammer (hammer_f τ), add_axiom Cn Fτ else -- Additional check which is not in the paper. If the axiom is of type Prop, we want to add it as statement which must be true. -- Thus, declarations like (Π(x:ℕ) x+x=2*x) are translated by applying F. do lip <- lives_in_prop_p c, if lip then do Cn <- mk_fresh_name, (Fc,_) <- using_hammer (hammer_f c), add_axiom Cn Fc else do Cn <- mk_fresh_name, (Gcτ,_) <- using_hammer (hammer_g τ), add_type_def Cn (remove_suffix_of_string c.to_string.to_list).as_string Gcτ meta def process_declarations : list name → hammer_tactic unit | [] := tactic.skip | (x :: xs) := do d ← tactic.get_decl x, translate_axiom_expression d.value, process_declarations xs --############################### --## Final problem translation ## --############################### meta def translate_declaration (e : name) : hammer_tactic unit := do env <- tactic.get_env, d <- tactic.get_decl e, l ← tactic.get_eqn_lemmas_for tt e, process_declarations (l.append [e]) meta def translate_problem: list name → list expr → hammer_tactic unit | [] [] := tactic.skip | [] (x::xs) := do translate_axiom_expression x, translate_problem [] xs | (y::ys) xs := do translate_declaration y, translate_problem ys xs meta def problem_to_hol_format (declr: list name) (clauses: list expr) (conjecture: expr) : hammer_tactic format := do -- DTT to HOL ⟨cl,cl_state⟩ <- using_hammer (translate_problem declr clauses), ⟨conj,conj_state⟩ <- using_hammer (hammer_f conjecture), let cl_list := (hammer_state.axiomas cl_state), -- For debugging, if no simplification should be applied let td_list := (hammer_state.type_definitions cl_state), -- For debugging, if no simplification should be applied -- HOL to Boolean-free HOL let conj := hol_formula_wo_bool conj, let cl_list := axiom_list_wo_bool cl_list, let td_list := boolean_type_replacements ++ (typedef_list_wo_bool td_list), return $ export_formula td_list cl_list conj run_cmd do ⟨f,_⟩ <- using_hammer $ problem_to_hol_format [`fib] [`(Π(x:ℕ), x + 1 = nat.succ x), `(nat.succ 0 = 1), `(nat.succ 1 = 2)] -- , `(0:ℕ), `(1:ℕ), `(2:ℕ), `(3:ℕ), `(4:ℕ), `(5:ℕ), `(6:ℕ), `(7:ℕ), `(8:ℕ), `(Π x y : ℕ, sum_two x y =x+y) `((fib 2 = 2)), tactic.trace f
e66a79485a69aab9c450e27b6099fd16a15eef20
63abd62053d479eae5abf4951554e1064a4c45b4
/src/ring_theory/polynomial/homogeneous.lean
34242ba7b1f2d6b5d83632c38325c3591a33116e
[ "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
9,466
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.mv_polynomial import data.fintype.card /-! # Homogeneous polynomials A multivariate polynomial `φ` is homogeneous of degree `n` if all monomials occuring in `φ` have degree `n`. ## Main definitions/lemmas * `is_homogeneous φ n`: a predicate that asserts that `φ` is homogeneous of degree `n`. * `homogeneous_component n`: the additive morphism that projects polynomials onto their summand that is homogeneous of degree `n`. * `sum_homogeneous_component`: every polynomial is the sum of its homogeneous components -/ open_locale big_operators namespace mv_polynomial variables {σ : Type*} {τ : Type*} {R : Type*} {S : Type*} /- TODO * create definition for `∑ i in d.support, d i` * define graded rings, and show that mv_polynomial is an example -/ /-- A multivariate polynomial `φ` is homogeneous of degree `n` if all monomials occuring in `φ` have degree `n`. -/ def is_homogeneous [comm_semiring R] (φ : mv_polynomial σ R) (n : ℕ) := ∀ ⦃d⦄, coeff d φ ≠ 0 → ∑ i in d.support, d i = n section variables [comm_semiring R] variables {σ R} lemma is_homogeneous_monomial (d : σ →₀ ℕ) (r : R) (n : ℕ) (hn : ∑ i in d.support, d i = n) : is_homogeneous (monomial d r) n := begin intros c hc, rw coeff_monomial at hc, split_ifs at hc with h, { subst c, exact hn }, { contradiction } end variables (σ) {R} lemma is_homogeneous_C (r : R) : is_homogeneous (C r : mv_polynomial σ R) 0 := begin apply is_homogeneous_monomial, simp only [finsupp.zero_apply, finset.sum_const_zero], end variables (σ R) lemma is_homogeneous_zero (n : ℕ) : is_homogeneous (0 : mv_polynomial σ R) n := λ d hd, false.elim (hd $ coeff_zero _) lemma is_homogeneous_one : is_homogeneous (1 : mv_polynomial σ R) 0 := is_homogeneous_C _ _ variables {σ} (R) lemma is_homogeneous_X (i : σ) : is_homogeneous (X i : mv_polynomial σ R) 1 := begin apply is_homogeneous_monomial, simp only [finsupp.support_single_ne_zero one_ne_zero, finset.sum_singleton], exact finsupp.single_eq_same end end namespace is_homogeneous variables [comm_semiring R] {φ ψ : mv_polynomial σ R} {m n : ℕ} lemma coeff_eq_zero (hφ : is_homogeneous φ n) (d : σ →₀ ℕ) (hd : ∑ i in d.support, d i ≠ n) : coeff d φ = 0 := by { have aux := mt (@hφ d) hd, classical, rwa not_not at aux } lemma inj_right (hm : is_homogeneous φ m) (hn : is_homogeneous φ n) (hφ : φ ≠ 0) : m = n := begin obtain ⟨d, hd⟩ : ∃ d, coeff d φ ≠ 0 := exists_coeff_ne_zero hφ, rw [← hm hd, ← hn hd] end lemma add (hφ : is_homogeneous φ n) (hψ : is_homogeneous ψ n) : is_homogeneous (φ + ψ) n := begin intros c hc, rw coeff_add at hc, obtain h|h : coeff c φ ≠ 0 ∨ coeff c ψ ≠ 0, { contrapose! hc, simp only [hc, add_zero] }, { exact hφ h }, { exact hψ h } end lemma sum {ι : Type*} (s : finset ι) (φ : ι → mv_polynomial σ R) (n : ℕ) (h : ∀ i ∈ s, is_homogeneous (φ i) n) : is_homogeneous (∑ i in s, φ i) n := begin classical, revert h, apply finset.induction_on s, { intro, simp only [is_homogeneous_zero, finset.sum_empty] }, { intros i s his IH h, simp only [his, finset.sum_insert, not_false_iff], apply (h i (finset.mem_insert_self _ _)).add (IH _), intros j hjs, exact h j (finset.mem_insert_of_mem hjs) } end lemma mul (hφ : is_homogeneous φ m) (hψ : is_homogeneous ψ n) : is_homogeneous (φ * ψ) (m + n) := begin intros c hc, rw [coeff_mul] at hc, obtain ⟨⟨d, e⟩, hde, H⟩ := finset.exists_ne_zero_of_sum_ne_zero hc, have aux : coeff d φ ≠ 0 ∧ coeff e ψ ≠ 0, { contrapose! H, by_cases h : coeff d φ = 0; simp only [*, ne.def, not_false_iff, zero_mul, mul_zero] at * }, specialize hφ aux.1, specialize hψ aux.2, rw finsupp.mem_antidiagonal_support at hde, classical, have hd' : d.support ⊆ d.support ∪ e.support := finset.subset_union_left _ _, have he' : e.support ⊆ d.support ∪ e.support := finset.subset_union_right _ _, rw [← hde, ← hφ, ← hψ, finset.sum_subset (finsupp.support_add), finset.sum_subset hd', finset.sum_subset he', ← finset.sum_add_distrib], { congr }, all_goals { intros i hi, apply finsupp.not_mem_support_iff.mp } end lemma prod {ι : Type*} (s : finset ι) (φ : ι → mv_polynomial σ R) (n : ι → ℕ) (h : ∀ i ∈ s, is_homogeneous (φ i) (n i)) : is_homogeneous (∏ i in s, φ i) (∑ i in s, n i) := begin classical, revert h, apply finset.induction_on s, { intro, simp only [is_homogeneous_one, finset.sum_empty, finset.prod_empty] }, { intros i s his IH h, simp only [his, finset.prod_insert, finset.sum_insert, not_false_iff], apply (h i (finset.mem_insert_self _ _)).mul (IH _), intros j hjs, exact h j (finset.mem_insert_of_mem hjs) } end lemma total_degree (hφ : is_homogeneous φ n) (h : φ ≠ 0) : total_degree φ = n := begin rw total_degree, apply le_antisymm, { apply finset.sup_le, intros d hd, rw finsupp.mem_support_iff at hd, rw [finsupp.sum, hφ hd], }, { obtain ⟨d, hd⟩ : ∃ d, coeff d φ ≠ 0 := exists_coeff_ne_zero h, simp only [← hφ hd, finsupp.sum], replace hd := finsupp.mem_support_iff.mpr hd, exact finset.le_sup hd, } end end is_homogeneous section noncomputable theory open_locale classical open finset /-- `homogeneous_component n φ` is the part of `φ` that is homogeneous of degree `n`. See `sum_homogeneous_component` for the statement that `φ` is equal to the sum of all its homogeneous components. -/ def homogeneous_component [comm_semiring R] (n : ℕ) : mv_polynomial σ R →+ mv_polynomial σ R := { to_fun := λ φ, ∑ d in φ.support.filter (λ d, ∑ i in d.support, d i = n), monomial d (coeff d φ), map_zero' := by simp only [monomial_zero, sum_const_zero, coeff_zero], map_add' := begin intros φ ψ, rw ext_iff, intro d, rw [coeff_add], by_cases hd : ∑ i in d.support, d i = n, { iterate 3 { rw [coeff_sum, sum_eq_single d], }, { simp only [if_true, eq_self_iff_true, coeff_add, coeff_monomial], }, all_goals { try { intros _ _ h, rw [coeff_monomial, if_neg h], }, }, all_goals { intro h, rw [mem_filter, not_and'] at h, specialize h hd, rw [finsupp.not_mem_support_iff] at h, rwa [coeff_monomial, if_pos rfl], }, }, { iterate 3 { rw [coeff_sum, sum_eq_zero], }, { rw [add_zero] }, all_goals { intros _ h, rw [mem_filter] at h, rw [coeff_monomial, if_neg], rintro rfl, exact hd h.2, }, } end } section homogeneous_component open finset variables [comm_semiring R] (n : ℕ) (φ : mv_polynomial σ R) lemma homogeneous_component_apply : homogeneous_component n φ = ∑ d in φ.support.filter (λ d, ∑ i in d.support, d i = n), monomial d (coeff d φ) := rfl lemma homogeneous_component_is_homogeneous : (homogeneous_component n φ).is_homogeneous n := begin rw [homogeneous_component_apply], intros d hd, rw [coeff_sum] at hd, obtain ⟨d', hd', H⟩ := exists_ne_zero_of_sum_ne_zero hd, dsimp at H, rw [coeff_monomial] at H, split_ifs at H with h, { cases h, rw [mem_filter] at hd', exact hd'.2 }, { contradiction } end lemma homogeneous_component_zero : homogeneous_component 0 φ = C (coeff 0 φ) := begin rw [homogeneous_component_apply, sum_eq_single (0 : σ →₀ ℕ)], { refl, }, { intros d hd hd', rw [mem_filter, sum_eq_zero_iff] at hd, exfalso, apply hd', ext i, by_cases hi : i ∈ d.support, { rw [hd.2 i hi, finsupp.zero_apply], }, { rw finsupp.not_mem_support_iff at hi, rwa finsupp.zero_apply } }, { rw [mem_filter, not_and'], intro h, suffices : coeff 0 φ = 0, { rw [this, monomial_zero], }, rw finsupp.not_mem_support_iff at h, apply h, rw sum_eq_zero, intros, rw finsupp.zero_apply } end lemma homogeneous_component_eq_zero' (h : ∀ d : σ →₀ ℕ, d ∈ φ.support → ∑ i in d.support, d i ≠ n) : homogeneous_component n φ = 0 := begin rw [homogeneous_component_apply, sum_eq_zero], intros d hd, rw mem_filter at hd, exfalso, exact h _ hd.1 hd.2 end lemma homogeneous_component_eq_zero (h : φ.total_degree < n) : homogeneous_component n φ = 0 := begin apply homogeneous_component_eq_zero', rw [total_degree, finset.sup_lt_iff] at h, { intros d hd, exact ne_of_lt (h d hd), }, { exact lt_of_le_of_lt (nat.zero_le _) h, } end lemma sum_homogeneous_component : ∑ i in range (φ.total_degree + 1), homogeneous_component i φ = φ := begin rw ext_iff, intro d, rw [coeff_sum, sum_eq_single (∑ i in d.support, d i)], { rw [homogeneous_component_apply, coeff_sum, sum_eq_single d, coeff_monomial, if_pos rfl], { intros _ _ h, rw [coeff_monomial, if_neg h], }, { rw [mem_filter, not_and'], intro h, suffices : coeff d φ = 0, { rw [this, monomial_zero, coeff_zero], }, { rw finsupp.not_mem_support_iff at h, apply h rfl, } } }, { intros n hn, contrapose!, rw eq_comm, apply homogeneous_component_is_homogeneous n φ, }, { rw [mem_range, not_lt, nat.succ_le_iff], intro h, apply coeff_eq_zero_of_total_degree_lt, apply lt_of_le_of_lt _ h, rw [homogeneous_component_eq_zero _ _ h], exact nat.zero_le _, } end end homogeneous_component end end mv_polynomial
634a07d35eeb8853d39cd6058ecb72141e655626
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/hott/axioms/funext.lean
1ad2df7fad75811d040dc382d0d6484a5a33c330
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,076
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Jeremy Avigad, Jakob von Raumer -- Ported from Coq HoTT -- TODO: take a look at the Coq tricks import hott.path hott.equiv open path -- Funext -- ------ -- Define function extensionality as a type class inductive funext [class] : Type := mk : (Π (A : Type) (P : A → Type ) (f g : Π x, P x), IsEquiv (@apD10 A P f g)) → funext namespace funext context universe variables l k parameters [F : funext.{l k}] {A : Type.{l}} {P : A → Type.{k}} (f g : Π x, P x) protected definition ap [instance] : IsEquiv (@apD10 A P f g) := rec_on F (λ (H : Π A P f g, _), !H) definition path_forall : f ∼ g → f ≈ g := @IsEquiv.inv _ _ (@apD10 A P f g) ap end definition path_forall2 [F : funext] {A B : Type} {P : A → B → Type} (f g : Πx y, P x y) : (Πx y, f x y ≈ g x y) → f ≈ g := λ E, path_forall f g (λx, path_forall (f x) (g x) (E x)) end funext
d6b1648087e058c1f41a33191f7bb7ae23472612
4727251e0cd73359b15b664c3170e5d754078599
/src/tactic/choose.lean
0a87a5f4483db4836435f355e99dbaf803eabe08
[ "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
7,318
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, Floris van Doorn, Mario Carneiro -/ import logic.function.basic import tactic.core /-! # `choose` tactic Performs Skolemization, that is, given `h : ∀ a:α, ∃ b:β, p a b |- G` produces `f : α → β, hf: ∀ a, p a (f a) |- G`. -/ namespace tactic /-- Given `α : Sort u`, `nonemp : nonempty α`, `p : α → Prop`, a context of local variables `ctxt`, and a pair of an element `val : α` and `spec : p val`, `mk_sometimes u α nonemp p ctx (val, spec)` produces another pair `val', spec'` such that `val'` does not have any free variables from elements of `ctxt` whose types are propositions. This is done by applying `function.sometimes` to abstract over all the propositional arguments. -/ meta def mk_sometimes (u : level) (α nonemp p : expr) : list expr → expr × expr → tactic (expr × expr) | [] (val, spec) := pure (val, spec) | (e :: ctxt) (val, spec) := do (val, spec) ← mk_sometimes ctxt (val, spec), t ← infer_type e, b ← is_prop t, pure $ if b then let val' := expr.bind_lambda val e in (expr.const ``function.sometimes [level.zero, u] t α nonemp val', expr.const ``function.sometimes_spec [u] t α nonemp p val' e spec) else (val, spec) /-- Changes `(h : ∀xs, ∃a:α, p a) ⊢ g` to `(d : ∀xs, a) (s : ∀xs, p (d xs)) ⊢ g` and `(h : ∀xs, p xs ∧ q xs) ⊢ g` to `(d : ∀xs, p xs) (s : ∀xs, q xs) ⊢ g`. `choose1` returns a pair of the second local constant it introduces, and the error result (see below). If `nondep` is true and `α` is inhabited, then it will remove the dependency of `d` on all propositional assumptions in `xs`. For example if `ys` are propositions then `(h : ∀xs ys, ∃a:α, p a) ⊢ g` becomes `(d : ∀xs, a) (s : ∀xs ys, p (d xs)) ⊢ g`. The second value returned by `choose1` is the result of nondep elimination: * `none`: nondep elimination was not attempted or was not applicable * `some none`: nondep elimination was successful * ``some (some `(nonempty α))``: nondep elimination was unsuccessful because we could not find a `nonempty α` instance -/ meta def choose1 (nondep : bool) (h : expr) (data : name) (spec : name) : tactic (expr × option (option expr)) := do t ← infer_type h, (ctxt, t) ← whnf t >>= open_pis, t ← whnf t transparency.all, match t with | `(@Exists %%α %%p) := do α_t ← infer_type α, expr.sort u ← whnf α_t transparency.all, (ne_fail, nonemp) ← if nondep then do let ne := expr.const ``nonempty [u] α, nonemp ← try_core (mk_instance ne <|> retrieve' (do m ← mk_meta_var ne, set_goals [m], ctxt.mmap' (λ e, do b ← is_proof e, monad.unlessb b $ (mk_app ``nonempty.intro [e] >>= note_anon none) $> ()), reset_instance_cache, apply_instance, instantiate_mvars m)), pure (some (option.guard (λ _, nonemp.is_none) ne), nonemp) else pure (none, none), ctxt' ← if nonemp.is_some then ctxt.mfilter (λ e, bnot <$> is_proof e) else pure ctxt, value ← mk_local_def data (α.pis ctxt'), t' ← head_beta (p.app (value.mk_app ctxt')), spec ← mk_local_def spec (t'.pis ctxt), (value_proof, spec_proof) ← nonemp.elim pure (λ nonemp, mk_sometimes u α nonemp p ctxt) (expr.const ``classical.some [u] α p (h.mk_app ctxt), expr.const ``classical.some_spec [u] α p (h.mk_app ctxt)), dependent_pose_core [(value, value_proof.lambdas ctxt'), (spec, spec_proof.lambdas ctxt)], try (tactic.clear h), intro1, e ← intro1, pure (e, ne_fail) | `(%%p ∧ %%q) := do mk_app ``and.elim_left [h.mk_app ctxt] >>= lambdas ctxt >>= note data none, hq ← mk_app ``and.elim_right [h.mk_app ctxt] >>= lambdas ctxt >>= note spec none, try (tactic.clear h), pure (hq, none) | _ := fail "expected a term of the shape `∀xs, ∃a, p xs a` or `∀xs, p xs ∧ q xs`" end /-- Changes `(h : ∀xs, ∃as, p as ∧ q as) ⊢ g` to a list of functions `as`, and a final hypothesis on `p as` and `q as`. If `nondep` is true then the functions will be made to not depend on propositional arguments, when possible. The last argument is an internal recursion variable, indicating whether nondep elimination has been useful so far. The tactic fails if `nondep` is true, and nondep elimination is attempted at least once, and it fails every time it is attempted, in which case it returns an error complaining about the first attempt. -/ meta def choose (nondep : bool) : expr → list name → opt_param (option (option expr)) none → tactic unit | h [] _ := fail "expect list of variables" | h [n] (some (some ne)) := do g ← mk_meta_var ne, set_goals [g], -- make a reasonable error state fail "choose: failed to synthesize nonempty instance" | h [n] _ := do cnt ← revert h, intro n, intron (cnt - 1), return () | h (n::ns) ne_fail₁ := do (v, ne_fail₂) ← get_unused_name >>= choose1 nondep h n, choose v ns $ match ne_fail₁, ne_fail₂ with | none, _ := ne_fail₂ | some none, _ := some none | _, some none := some none | _, _ := ne_fail₁ end namespace interactive setup_tactic_parser /-- `choose a b h h' using hyp` takes an hypothesis `hyp` of the form `∀ (x : X) (y : Y), ∃ (a : A) (b : B), P x y a b ∧ Q x y a b` for some `P Q : X → Y → A → B → Prop` and outputs into context a function `a : X → Y → A`, `b : X → Y → B` and two assumptions: `h : ∀ (x : X) (y : Y), P x y (a x y) (b x y)` and `h' : ∀ (x : X) (y : Y), Q x y (a x y) (b x y)`. It also works with dependent versions. `choose! a b h h' using hyp` does the same, except that it will remove dependency of the functions on propositional arguments if possible. For example if `Y` is a proposition and `A` and `B` are nonempty in the above example then we will instead get `a : X → A`, `b : X → B`, and the assumptions `h : ∀ (x : X) (y : Y), P x y (a x) (b x)` and `h' : ∀ (x : X) (y : Y), Q x y (a x) (b x)`. Examples: ```lean example (h : ∀n m : ℕ, ∃i j, m = n + i ∨ m + j = n) : true := begin choose i j h using h, guard_hyp i : ℕ → ℕ → ℕ, guard_hyp j : ℕ → ℕ → ℕ, guard_hyp h : ∀ (n m : ℕ), m = n + i n m ∨ m + j n m = n, trivial end ``` ```lean example (h : ∀ i : ℕ, i < 7 → ∃ j, i < j ∧ j < i+i) : true := begin choose! f h h' using h, guard_hyp f : ℕ → ℕ, guard_hyp h : ∀ (i : ℕ), i < 7 → i < f i, guard_hyp h' : ∀ (i : ℕ), i < 7 → f i < i + i, trivial, end ``` -/ meta def choose (nondep : parse (tk "!")?) (first : parse ident) (names : parse ident*) (tgt : parse (tk "using" *> texpr)?) : tactic unit := do tgt ← match tgt with | none := get_local `this | some e := tactic.i_to_expr_strict e end, tactic.choose nondep.is_some tgt (first :: names), try (interactive.simp none none tt [simp_arg_type.expr ``(exists_prop)] [] (loc.ns $ some <$> names)), try (tactic.clear tgt) add_tactic_doc { name := "choose", category := doc_category.tactic, decl_names := [`tactic.interactive.choose], tags := ["classical logic"] } end interactive end tactic
f535b92d4fc6df5989228255b0f1c6740e4459d8
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/tactic10.lean
3a782c6967999d6fe6cac933f89e42e3dff5ada5
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
201
lean
import logic theorem tst (a b : Prop) (H : a ↔ b) : b ↔ a := by apply iff.intro; intro Hb; apply (iff.elim_right H Hb); intro Ha; apply (iff.elim_left H Ha) check tst
49b69d3057be9276f324106dbae798f97ead2c10
626e312b5c1cb2d88fca108f5933076012633192
/src/algebra/euclidean_absolute_value.lean
fb3529b2343c6602058cbd54061c92eb487a2ebc
[ "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
2,059
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.absolute_value import algebra.euclidean_domain /-! # Euclidean absolute values This file defines a predicate `absolute_value.is_euclidean abv` stating the absolute value is compatible with the Euclidean domain structure on its domain. ## Main definitions * `absolute_value.is_euclidean abv` is a predicate on absolute values on `R` mapping to `S` that preserve the order on `R` arising from the Euclidean domain structure. * `absolute_value.abs_is_euclidean` shows the "standard" absolute value on `ℤ`, mapping negative `x` to `-x`, is euclidean. -/ local infix ` ≺ `:50 := euclidean_domain.r namespace absolute_value variables {R S : Type*} [euclidean_domain R] [ordered_semiring S] variables (abv : absolute_value R S) /-- An absolute value `abv : R → S` is Euclidean if it is compatible with the `euclidean_domain` structure on `R`, namely `abv` is strictly monotone with respect to the well founded relation `≺` on `R`. -/ structure is_euclidean : Prop := (map_lt_map_iff' : ∀ {x y}, abv x < abv y ↔ x ≺ y) namespace is_euclidean variables {abv} -- Rearrange the parameters to `map_lt_map_iff'` so it elaborates better. lemma map_lt_map_iff {x y : R} (h : abv.is_euclidean) : abv x < abv y ↔ x ≺ y := map_lt_map_iff' h attribute [simp] map_lt_map_iff lemma sub_mod_lt (h : abv.is_euclidean) (a : R) {b : R} (hb : b ≠ 0) : abv (a % b) < abv b := h.map_lt_map_iff.mpr (euclidean_domain.mod_lt a hb) end is_euclidean section int open int -- TODO: generalize to `linear_ordered_euclidean_domain`s if we ever get a definition of those /-- `abs : ℤ → ℤ` is a Euclidean absolute value -/ protected lemma abs_is_euclidean : is_euclidean (absolute_value.abs : absolute_value ℤ ℤ) := { map_lt_map_iff' := λ x y, show abs x < abs y ↔ nat_abs x < nat_abs y, by rw [abs_eq_nat_abs, abs_eq_nat_abs, coe_nat_lt] } end int end absolute_value
0ef486b785ce31bfc9364defafb1e1be2fc663c2
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/matrix/pequiv.lean
66d9cb41ffc1a7be5a614bf2c492e6853238c122
[ "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
5,760
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.matrix.basic import data.pequiv /-! # partial equivalences for matrices Using partial equivalences to represent matrices. This file introduces the function `pequiv.to_matrix`, which returns a matrix containing ones and zeros. For any partial equivalence `f`, `f.to_matrix i j = 1 ↔ f i = some j`. The following important properties of this function are proved `to_matrix_trans : (f.trans g).to_matrix = f.to_matrix ⬝ g.to_matrix` `to_matrix_symm : f.symm.to_matrix = f.to_matrixᵀ` `to_matrix_refl : (pequiv.refl n).to_matrix = 1` `to_matrix_bot : ⊥.to_matrix = 0` This theory gives the matrix representation of projection linear maps, and their right inverses. For example, the matrix `(single (0 : fin 1) (i : fin n)).to_matrix` corresponds to the ith projection map from R^n to R. Any injective function `fin m → fin n` gives rise to a `pequiv`, whose matrix is the projection map from R^m → R^n represented by the same function. The transpose of this matrix is the right inverse of this map, sending anything not in the image to zero. ## notations This file uses the notation ` ⬝ ` for `matrix.mul` and `ᵀ` for `matrix.transpose`. -/ namespace pequiv open matrix universes u v variables {k l m n : Type*} variables {α : Type v} open_locale matrix /-- `to_matrix` returns a matrix containing ones and zeros. `f.to_matrix i j` is `1` if `f i = some j` and `0` otherwise -/ def to_matrix [decidable_eq n] [has_zero α] [has_one α] (f : m ≃. n) : matrix m n α | i j := if j ∈ f i then 1 else 0 lemma mul_matrix_apply [fintype m] [decidable_eq m] [semiring α] (f : l ≃. m) (M : matrix m n α) (i j) : (f.to_matrix ⬝ M) i j = option.cases_on (f i) 0 (λ fi, M fi j) := begin dsimp [to_matrix, matrix.mul_apply], cases h : f i with fi, { simp [h] }, { rw finset.sum_eq_single fi; simp [h, eq_comm] {contextual := tt} } end lemma to_matrix_symm [decidable_eq m] [decidable_eq n] [has_zero α] [has_one α] (f : m ≃. n) : (f.symm.to_matrix : matrix n m α) = f.to_matrixᵀ := by ext; simp only [transpose, mem_iff_mem f, to_matrix]; congr @[simp] lemma to_matrix_refl [decidable_eq n] [has_zero α] [has_one α] : ((pequiv.refl n).to_matrix : matrix n n α) = 1 := by ext; simp [to_matrix, one_apply]; congr lemma matrix_mul_apply [fintype m] [semiring α] [decidable_eq n] (M : matrix l m α) (f : m ≃. n) (i j) : (M ⬝ f.to_matrix) i j = option.cases_on (f.symm j) 0 (λ fj, M i fj) := begin dsimp [to_matrix, matrix.mul_apply], cases h : f.symm j with fj, { simp [h, ← f.eq_some_iff] }, { rw finset.sum_eq_single fj, { simp [h, ← f.eq_some_iff], }, { intros b H n, simp [h, ← f.eq_some_iff, n.symm], }, { simp, } } end lemma to_pequiv_mul_matrix [fintype m] [decidable_eq m] [semiring α] (f : m ≃ m) (M : matrix m n α) : (f.to_pequiv.to_matrix ⬝ M) = λ i, M (f i) := by { ext i j, rw [mul_matrix_apply, equiv.to_pequiv_apply] } lemma to_matrix_trans [fintype m] [decidable_eq m] [decidable_eq n] [semiring α] (f : l ≃. m) (g : m ≃. n) : ((f.trans g).to_matrix : matrix l n α) = f.to_matrix ⬝ g.to_matrix := begin ext i j, rw [mul_matrix_apply], dsimp [to_matrix, pequiv.trans], cases f i; simp end @[simp] lemma to_matrix_bot [decidable_eq n] [has_zero α] [has_one α] : ((⊥ : pequiv m n).to_matrix : matrix m n α) = 0 := rfl lemma to_matrix_injective [decidable_eq n] [monoid_with_zero α] [nontrivial α] : function.injective (@to_matrix m n α _ _ _) := begin classical, assume f g, refine not_imp_not.1 _, simp only [matrix.ext_iff.symm, to_matrix, pequiv.ext_iff, not_forall, exists_imp_distrib], assume i hi, use i, cases hf : f i with fi, { cases hg : g i with gi, { cc }, { use gi, simp, } }, { use fi, simp [hf.symm, ne.symm hi] } end lemma to_matrix_swap [decidable_eq n] [ring α] (i j : n) : (equiv.swap i j).to_pequiv.to_matrix = (1 : matrix n n α) - (single i i).to_matrix - (single j j).to_matrix + (single i j).to_matrix + (single j i).to_matrix := begin ext, dsimp [to_matrix, single, equiv.swap_apply_def, equiv.to_pequiv, one_apply], split_ifs; {simp * at *} <|> { exfalso, assumption }, end @[simp] lemma single_mul_single [fintype n] [decidable_eq k] [decidable_eq m] [decidable_eq n] [semiring α] (a : m) (b : n) (c : k) : ((single a b).to_matrix : matrix _ _ α) ⬝ (single b c).to_matrix = (single a c).to_matrix := by rw [← to_matrix_trans, single_trans_single] lemma single_mul_single_of_ne [fintype n] [decidable_eq n] [decidable_eq k] [decidable_eq m] [semiring α] {b₁ b₂ : n} (hb : b₁ ≠ b₂) (a : m) (c : k) : ((single a b₁).to_matrix : matrix _ _ α) ⬝ (single b₂ c).to_matrix = 0 := by rw [← to_matrix_trans, single_trans_single_of_ne hb, to_matrix_bot] /-- Restatement of `single_mul_single`, which will simplify expressions in `simp` normal form, when associativity may otherwise need to be carefully applied. -/ @[simp] lemma single_mul_single_right [fintype n] [fintype k] [decidable_eq n] [decidable_eq k] [decidable_eq m] [semiring α] (a : m) (b : n) (c : k) (M : matrix k l α) : (single a b).to_matrix ⬝ ((single b c).to_matrix ⬝ M) = (single a c).to_matrix ⬝ M := by rw [← matrix.mul_assoc, single_mul_single] /-- We can also define permutation matrices by permuting the rows of the identity matrix. -/ lemma equiv_to_pequiv_to_matrix [decidable_eq n] [has_zero α] [has_one α] (σ : equiv n n) (i j : n) : σ.to_pequiv.to_matrix i j = (1 : matrix n n α) (σ i) j := if_congr option.some_inj rfl rfl end pequiv
40347d4365e2c696a3c4e05acceec6a0b53b5ee9
1fbca480c1574e809ae95a3eda58188ff42a5e41
/src/util/data/minimum/lemmas.lean
b280d9855ffb556391e1419b3731fa76cf6186ab
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
381
lean
import util.meta.tactic import util.data.minimum.basic variable {α : Type*} variable [has_minimum α] open has_minimum (minimum) lemma lt_minimum_iff (x : α) {xs : set α} (h : xs ≠ ∅) : x < minimum xs ↔ (∀ y ∈ xs, x < y) := begin split, { intros, apply lt_of_lt_of_le a, apply minimum_le H, }, { intros, apply_assumption, apply minimum_mem h }, end
d14723d6ec8f7238e10a5eba59e0bebb8f8fe636
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/impbug3.lean
5c9eb720af7623526ca448c2d1d6fc4e78eef507
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
600
lean
-- category definition Prop := Type.{0} constant eq {A : Type} : A → A → Prop infix `=`:50 := eq constant ob : Type.{1} constant mor : ob → ob → Type.{1} inductive category : Type := mk : Π (id : Π (A : ob), mor A A), (Π (A B : ob) (f : mor A A), id A = f) → category definition id (Cat : category) := category.rec (λ id idl, id) Cat constant Cat : category theorem id_left (A : ob) (f : mor A A) : @eq (mor A A) (id Cat A) f := @category.rec (λ (C : category), @eq (mor A A) (id C A) f) (λ (id : Π (T : ob), mor T T) (idl : Π (T : ob), _), idl A A f) Cat
0bfa2cb0fd3734f32a6bf0648e2950d97a4fa406
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/discrete_quotient.lean
524a9463c435e20be6f64346493957ded37c0492
[ "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,373
lean
/- Copyright (c) 2021 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Calle Sönne, Adam Topaz -/ import topology.separation import topology.subset_properties import topology.locally_constant.basic /-! # Discrete quotients of a topological space. This file defines the type of discrete quotients of a topological space, denoted `discrete_quotient X`. To avoid quantifying over types, we model such quotients as setoids whose equivalence classes are clopen. ## Definitions 1. `discrete_quotient X` is the type of discrete quotients of `X`. It is endowed with a coercion to `Type`, which is defined as the quotient associated to the setoid in question, and each such quotient is endowed with the discrete topology. 2. Given `S : discrete_quotient X`, the projection `X → S` is denoted `S.proj`. 3. When `X` is compact and `S : discrete_quotient X`, the space `S` is endowed with a `fintype` instance. ## Order structure The type `discrete_quotient X` is endowed with an instance of a `semilattice_inf_top`. The partial ordering `A ≤ B` mathematically means that `B.proj` factors through `A.proj`. The top element `⊤` is the trivial quotient, meaning that every element of `X` is collapsed to a point. Given `h : A ≤ B`, the map `A → B` is `discrete_quotient.of_le h`. Whenever `X` is discrete, the type `discrete_quotient X` is also endowed with an instance of a `semilattice_inf_bot`, where the bot element `⊥` is `X` itself. Given `f : X → Y` and `h : continuous f`, we define a predicate `le_comap h A B` for `A : discrete_quotient X` and `B : discrete_quotient Y`, asserting that `f` descends to `A → B`. If `cond : le_comap h A B`, the function `A → B` is obtained by `discrete_quotient.map cond`. ## Theorems The two main results proved in this file are: 1. `discrete_quotient.eq_of_proj_eq` which states that when `X` is compact, t2 and totally disconnected, any two elements of `X` agree if their projections in `Q` agree for all `Q : discrete_quotient X`. 2. `discrete_quotient.exists_of_compat` which states that when `X` is compact, then any system of elements of `Q` as `Q : discrete_quotient X` varies, which is compatible with respect to `discrete_quotient.of_le`, must arise from some element of `X`. ## Remarks The constructions in this file will be used to show that any profinite space is a limit of finite discrete spaces. -/ variables (X : Type*) [topological_space X] /-- The type of discrete quotients of a topological space. -/ @[ext] structure discrete_quotient := (rel : X → X → Prop) (equiv : equivalence rel) (clopen : ∀ x, is_clopen (set_of (rel x))) namespace discrete_quotient variables {X} (S : discrete_quotient X) /-- Construct a discrete quotient from a clopen set. -/ def of_clopen {A : set X} (h : is_clopen A) : discrete_quotient X := { rel := λ x y, x ∈ A ∧ y ∈ A ∨ x ∉ A ∧ y ∉ A, equiv := ⟨by tauto!, by tauto!, by tauto!⟩, clopen := begin intros x, by_cases hx : x ∈ A, { apply is_clopen.union, { convert h, ext, exact ⟨λ i, i.2, λ i, ⟨hx,i⟩⟩ }, { convert is_clopen_empty, tidy } }, { apply is_clopen.union, { convert is_clopen_empty, tidy }, { convert is_clopen.compl h, ext, exact ⟨λ i, i.2, λ i, ⟨hx, i⟩⟩ } }, end } lemma refl : ∀ x : X, S.rel x x := S.equiv.1 lemma symm : ∀ x y : X, S.rel x y → S.rel y x := S.equiv.2.1 lemma trans : ∀ x y z : X, S.rel x y → S.rel y z → S.rel x z := S.equiv.2.2 /-- The setoid whose quotient yields the discrete quotient. -/ def setoid : setoid X := ⟨S.rel, S.equiv⟩ instance : has_coe_to_sort (discrete_quotient X) Type* := ⟨λ S, quotient S.setoid⟩ instance : topological_space S := ⊥ /-- The projection from `X` to the given discrete quotient. -/ def proj : X → S := quotient.mk' lemma proj_surjective : function.surjective S.proj := quotient.surjective_quotient_mk' lemma fiber_eq (x : X) : S.proj ⁻¹' {S.proj x} = set_of (S.rel x) := begin ext1 y, simp only [set.mem_preimage, set.mem_singleton_iff, quotient.eq', discrete_quotient.proj.equations._eqn_1, set.mem_set_of_eq], exact ⟨λ h, S.symm _ _ h, λ h, S.symm _ _ h⟩, end lemma proj_is_locally_constant : is_locally_constant S.proj := begin rw (is_locally_constant.tfae S.proj).out 0 3, intros x, rcases S.proj_surjective x with ⟨x,rfl⟩, simp [fiber_eq, (S.clopen x).1], end lemma proj_continuous : continuous S.proj := is_locally_constant.continuous $ proj_is_locally_constant _ lemma fiber_closed (A : set S) : is_closed (S.proj ⁻¹' A) := is_closed.preimage S.proj_continuous ⟨trivial⟩ lemma fiber_open (A : set S) : is_open (S.proj ⁻¹' A) := is_open.preimage S.proj_continuous trivial lemma fiber_clopen (A : set S) : is_clopen (S.proj ⁻¹' A) := ⟨fiber_open _ _, fiber_closed _ _⟩ instance : partial_order (discrete_quotient X) := { le := λ A B, ∀ x y : X, A.rel x y → B.rel x y, le_refl := λ a, by tauto, le_trans := λ a b c h1 h2, by tauto, le_antisymm := λ a b h1 h2, by { ext, tauto } } instance : order_top (discrete_quotient X) := { top := ⟨λ a b, true, ⟨by tauto, by tauto, by tauto⟩, λ _, is_clopen_univ⟩, le_top := λ a, by tauto } instance : semilattice_inf_top (discrete_quotient X) := { inf := λ A B, { rel := λ x y, A.rel x y ∧ B.rel x y, equiv := ⟨λ a, ⟨A.refl _,B.refl _⟩, λ a b h, ⟨A.symm _ _ h.1, B.symm _ _ h.2⟩, λ a b c h1 h2, ⟨A.trans _ _ _ h1.1 h2.1, B.trans _ _ _ h1.2 h2.2⟩⟩, clopen := λ x, is_clopen.inter (A.clopen _) (B.clopen _) }, inf_le_left := λ a b, by tauto, inf_le_right := λ a b, by tauto, le_inf := λ a b c h1 h2, by tauto, ..discrete_quotient.order_top, ..discrete_quotient.partial_order } instance : inhabited (discrete_quotient X) := ⟨⊤⟩ section comap variables {Y : Type*} [topological_space Y] {f : Y → X} (cont : continuous f) /-- Comap a discrete quotient along a continuous map. -/ def comap : discrete_quotient Y := { rel := λ a b, S.rel (f a) (f b), equiv := ⟨λ a, S.refl _, λ a b h, S.symm _ _ h, λ a b c h1 h2, S.trans _ _ _ h1 h2⟩, clopen := λ y, ⟨is_open.preimage cont (S.clopen _).1, is_closed.preimage cont (S.clopen _).2⟩ } @[simp] lemma comap_id : S.comap (continuous_id : continuous (id : X → X)) = S := by { ext, refl } @[simp] lemma comap_comp {Z : Type*} [topological_space Z] {g : Z → Y} (cont' : continuous g) : S.comap (continuous.comp cont cont') = (S.comap cont).comap cont' := by { ext, refl } lemma comap_mono {A B : discrete_quotient X} (h : A ≤ B) : A.comap cont ≤ B.comap cont := by tauto end comap section of_le /-- The map induced by a refinement of a discrete quotient. -/ def of_le {A B : discrete_quotient X} (h : A ≤ B) : A → B := λ a, quotient.lift_on' a (λ x, B.proj x) (λ a b i, quotient.sound' (h _ _ i)) @[simp] lemma of_le_refl {A : discrete_quotient X} : of_le (le_refl A) = id := by { ext ⟨⟩, refl } lemma of_le_refl_apply {A : discrete_quotient X} (a : A) : of_le (le_refl A) a = a := by simp @[simp] lemma of_le_comp {A B C : discrete_quotient X} (h1 : A ≤ B) (h2 : B ≤ C) : of_le (le_trans h1 h2) = of_le h2 ∘ of_le h1 := by { ext ⟨⟩, refl } lemma of_le_comp_apply {A B C : discrete_quotient X} (h1 : A ≤ B) (h2 : B ≤ C) (a : A) : of_le (le_trans h1 h2) a = of_le h2 (of_le h1 a) := by simp lemma of_le_continuous {A B : discrete_quotient X} (h : A ≤ B) : continuous (of_le h) := continuous_of_discrete_topology @[simp] lemma of_le_proj {A B : discrete_quotient X} (h : A ≤ B) : of_le h ∘ A.proj = B.proj := by { ext, exact quotient.sound' (B.refl _) } @[simp] lemma of_le_proj_apply {A B : discrete_quotient X} (h : A ≤ B) (x : X) : of_le h (A.proj x) = B.proj x := by { change (of_le h ∘ A.proj) x = _, simp } end of_le /-- When X is discrete, there is a `semilattice_inf_bot` instance on `discrete_quotient X` -/ instance [discrete_topology X] : semilattice_inf_bot (discrete_quotient X) := { bot := { rel := (=), equiv := eq_equivalence, clopen := λ x, is_clopen_discrete _ }, bot_le := by { rintro S a b (h : a = b), rw h, exact S.refl _ }, ..(infer_instance : semilattice_inf _) } lemma proj_bot_injective [discrete_topology X] : function.injective (⊥ : discrete_quotient X).proj := λ a b h, quotient.exact' h lemma proj_bot_bijective [discrete_topology X] : function.bijective (⊥ : discrete_quotient X).proj := ⟨proj_bot_injective, proj_surjective _⟩ section map variables {Y : Type*} [topological_space Y] {f : Y → X} (cont : continuous f) (A : discrete_quotient Y) (B : discrete_quotient X) /-- Given `cont : continuous f`, `le_comap cont A B` is defined as `A ≤ B.comap f`. Mathematically this means that `f` descends to a morphism `A → B`. -/ def le_comap : Prop := A ≤ B.comap cont variables {cont A B} lemma le_comap_id (A : discrete_quotient X) : le_comap continuous_id A A := by tauto lemma le_comap_comp {Z : Type*} [topological_space Z] {g : Z → Y} {cont' : continuous g} {C : discrete_quotient Z} : le_comap cont' C A → le_comap cont A B → le_comap (continuous.comp cont cont') C B := by tauto lemma le_comap_trans {C : discrete_quotient X} : le_comap cont A B → B ≤ C → le_comap cont A C := λ h1 h2, le_trans h1 $ comap_mono _ h2 /-- Map a discrete quotient along a continuous map. -/ def map (cond : le_comap cont A B) : A → B := quotient.map' f cond lemma map_continuous (cond : le_comap cont A B) : continuous (map cond) := continuous_of_discrete_topology @[simp] lemma map_proj (cond : le_comap cont A B) : map cond ∘ A.proj = B.proj ∘ f := rfl @[simp] lemma map_proj_apply (cond : le_comap cont A B) (y : Y) : map cond (A.proj y) = B.proj (f y) := rfl @[simp] lemma map_id : map (le_comap_id A) = id := by { ext ⟨⟩, refl } @[simp] lemma map_comp {Z : Type*} [topological_space Z] {g : Z → Y} {cont' : continuous g} {C : discrete_quotient Z} (h1 : le_comap cont' C A) (h2 : le_comap cont A B) : map (le_comap_comp h1 h2) = map h2 ∘ map h1 := by { ext ⟨⟩, refl } @[simp] lemma of_le_map {C : discrete_quotient X} (cond : le_comap cont A B) (h : B ≤ C) : map (le_comap_trans cond h) = of_le h ∘ map cond := by { ext ⟨⟩, refl } @[simp] lemma of_le_map_apply {C : discrete_quotient X} (cond : le_comap cont A B) (h : B ≤ C) (a : A) : map (le_comap_trans cond h) a = of_le h (map cond a) := by { rcases a, refl } @[simp] lemma map_of_le {C : discrete_quotient Y} (cond : le_comap cont A B) (h : C ≤ A) : map (le_trans h cond) = map cond ∘ of_le h := by { ext ⟨⟩, refl } @[simp] lemma map_of_le_apply {C : discrete_quotient Y} (cond : le_comap cont A B) (h : C ≤ A) (c : C) : map (le_trans h cond) c = map cond (of_le h c) := by { rcases c, refl } end map lemma eq_of_proj_eq [t2_space X] [compact_space X] [disc : totally_disconnected_space X] {x y : X} : (∀ Q : discrete_quotient X, Q.proj x = Q.proj y) → x = y := begin intro h, change x ∈ ({y} : set X), rw totally_disconnected_space_iff_connected_component_singleton at disc, rw [← disc y, connected_component_eq_Inter_clopen], rintros U ⟨⟨U, hU1, hU2⟩, rfl⟩, replace h : _ ∨ _ := quotient.exact' (h (of_clopen hU1)), tauto, end lemma fiber_le_of_le {A B : discrete_quotient X} (h : A ≤ B) (a : A) : A.proj ⁻¹' {a} ≤ B.proj ⁻¹' {of_le h a} := begin induction a, erw [fiber_eq, fiber_eq], tidy, end lemma exists_of_compat [compact_space X] (Qs : Π (Q : discrete_quotient X), Q) (compat : ∀ (A B : discrete_quotient X) (h : A ≤ B), of_le h (Qs _) = Qs _) : ∃ x : X, ∀ Q : discrete_quotient X, Q.proj x = Qs _ := begin obtain ⟨x,hx⟩ := is_compact.nonempty_Inter_of_directed_nonempty_compact_closed (λ (Q : discrete_quotient X), Q.proj ⁻¹' {Qs _}) (λ A B, _) (λ i, _) (λ i, (fiber_closed _ _).is_compact) (λ i, fiber_closed _ _), { refine ⟨x, λ Q, _⟩, specialize hx _ ⟨Q,rfl⟩, dsimp at hx, rcases proj_surjective _ (Qs Q) with ⟨y,hy⟩, rw ← hy at *, rw fiber_eq at hx, exact quotient.sound' (Q.symm y x hx) }, { refine ⟨A ⊓ B, λ a ha, _, λ a ha, _⟩, { dsimp only, erw ← compat (A ⊓ B) A inf_le_left, exact fiber_le_of_le _ _ ha }, { dsimp only, erw ← compat (A ⊓ B) B inf_le_right, exact fiber_le_of_le _ _ ha } }, { obtain ⟨x,hx⟩ := i.proj_surjective (Qs i), refine ⟨x,_⟩, dsimp only, rw [← hx, fiber_eq], apply i.refl }, end noncomputable instance [compact_space X] : fintype S := begin have cond : is_compact (⊤ : set X) := compact_univ, rw is_compact_iff_finite_subcover at cond, have h := @cond S (λ s, S.proj ⁻¹' {s}) (λ s, fiber_open _ _) (λ x hx, ⟨S.proj ⁻¹' {S.proj x}, ⟨S.proj x, rfl⟩, rfl⟩), let T := classical.some h, have hT := classical.some_spec h, refine ⟨T,λ s, _⟩, rcases S.proj_surjective s with ⟨x,rfl⟩, rcases hT (by tauto : x ∈ ⊤) with ⟨j, ⟨j,rfl⟩, h1, ⟨hj, rfl⟩, h2⟩, dsimp only at h2, suffices : S.proj x = j, by rwa this, rcases j with ⟨j⟩, apply quotient.sound', erw fiber_eq at h2, exact S.symm _ _ h2 end end discrete_quotient namespace locally_constant variables {X} {α : Type*} (f : locally_constant X α) /-- Any locally constant function induces a discrete quotient. -/ def discrete_quotient : discrete_quotient X := { rel := λ a b, f b = f a, equiv := ⟨by tauto, by tauto, λ a b c h1 h2, by rw [h2, h1]⟩, clopen := λ x, f.is_locally_constant.is_clopen_fiber _ } /-- The function from the discrete quotient associated to a locally constant function. -/ def lift : f.discrete_quotient → α := λ a, quotient.lift_on' a f (λ a b h, h.symm) lemma lift_is_locally_constant : _root_.is_locally_constant f.lift := λ A, trivial /-- A locally constant version of `locally_constant.lift`. -/ def locally_constant_lift : locally_constant f.discrete_quotient α := ⟨f.lift, f.lift_is_locally_constant⟩ @[simp] lemma lift_eq_coe : f.lift = f.locally_constant_lift := rfl @[simp] lemma factors : f.locally_constant_lift ∘ f.discrete_quotient.proj = f := by { ext, refl } end locally_constant
d4ec2018262bfb963346a38dd9705c38984e8a29
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/limits/preserves/shapes/kernels.lean
0662414ba6c4e4adde2b2a83c2a880fde973e709
[ "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
7,551
lean
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.shapes.kernels import category_theory.limits.preserves.shapes.equalizers import category_theory.limits.preserves.shapes.zero /-! # Preserving (co)kernels Constructions to relate the notions of preserving (co)kernels and reflecting (co)kernels to concrete (co)forks. In particular, we show that `kernel_comparison f g G` is an isomorphism iff `G` preserves the limit of the parallel pair `f,0`, as well as the dual result. -/ noncomputable theory universes v u₁ u₂ open category_theory category_theory.category category_theory.limits variables {C : Type u₁} [category.{v} C] [has_zero_morphisms C] variables {D : Type u₂} [category.{v} D] [has_zero_morphisms D] variables (G : C ⥤ D) [functor.preserves_zero_morphisms G] namespace category_theory.limits section kernels variables {X Y Z : C} {f : X ⟶ Y} {h : Z ⟶ X} (w : h ≫ f = 0) /-- The map of a kernel fork is a limit iff the kernel fork consisting of the mapped morphisms is a limit. This essentially lets us commute `kernel_fork.of_ι` with `functor.map_cone`. This is a variant of `is_limit_map_cone_fork_equiv` for equalizers, which we can't use directly between `G.map 0 = 0` does not hold definitionally. -/ def is_limit_map_cone_fork_equiv' : is_limit (G.map_cone (kernel_fork.of_ι h w)) ≃ is_limit (kernel_fork.of_ι (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) : fork (G.map f) 0) := begin refine (is_limit.postcompose_hom_equiv _ _).symm.trans (is_limit.equiv_iso_limit _), refine parallel_pair.ext (iso.refl _) (iso.refl _) _ _; simp, refine fork.ext (iso.refl _) _, simp [fork.ι] end /-- The property of preserving kernels expressed in terms of kernel forks. This is a variant of `is_limit_fork_map_of_is_limit` for equalizers, which we can't use directly between `G.map 0 = 0` does not hold definitionally. -/ def is_limit_fork_map_of_is_limit' [preserves_limit (parallel_pair f 0) G] (l : is_limit (kernel_fork.of_ι h w)) : is_limit (kernel_fork.of_ι (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) : fork (G.map f) 0) := is_limit_map_cone_fork_equiv' G w (preserves_limit.preserves l) variables (f) [has_kernel f] /-- If `G` preserves kernels and `C` has them, then the fork constructed of the mapped morphisms of a kernel fork is a limit. -/ def is_limit_of_has_kernel_of_preserves_limit [preserves_limit (parallel_pair f 0) G] : is_limit (fork.of_ι (G.map (kernel.ι f)) (by simp only [←G.map_comp, equalizer.condition, comp_zero, functor.map_zero]) : fork (G.map f) 0) := is_limit_fork_map_of_is_limit' G (kernel.condition f) (kernel_is_kernel f) instance [preserves_limit (parallel_pair f 0) G] : has_kernel (G.map f) := { exists_limit := ⟨⟨_, is_limit_of_has_kernel_of_preserves_limit G f⟩⟩, } variables [has_kernel (G.map f)] /-- If the kernel comparison map for `G` at `f` is an isomorphism, then `G` preserves the kernel of `f`. -/ def preserves_kernel.of_iso_comparison [i : is_iso (kernel_comparison f G)] : preserves_limit (parallel_pair f 0) G := begin apply preserves_limit_of_preserves_limit_cone (kernel_is_kernel f), apply (is_limit_map_cone_fork_equiv' G (kernel.condition f)).symm _, apply is_limit.of_point_iso (limit.is_limit (parallel_pair (G.map f) 0)), apply i, end variables [preserves_limit (parallel_pair f 0) G] /-- If `G` preserves the kernel of `f`, then the kernel comparison map for `G` at `f` is an isomorphism. -/ def preserves_kernel.iso : G.obj (kernel f) ≅ kernel (G.map f) := is_limit.cone_point_unique_up_to_iso (is_limit_of_has_kernel_of_preserves_limit G f) (limit.is_limit _) @[simp] lemma preserves_kernel.iso_hom : (preserves_kernel.iso G f).hom = kernel_comparison f G := rfl instance : is_iso (kernel_comparison f G) := begin rw ← preserves_kernel.iso_hom, apply_instance end end kernels section cokernels variables {X Y Z : C} {f : X ⟶ Y} {h : Y ⟶ Z} (w : f ≫ h = 0) /-- The map of a cokernel cofork is a colimit iff the cokernel cofork consisting of the mapped morphisms is a colimit. This essentially lets us commute `cokernel_cofork.of_π` with `functor.map_cocone`. This is a variant of `is_colimit_map_cocone_cofork_equiv` for equalizers, which we can't use directly between `G.map 0 = 0` does not hold definitionally. -/ def is_colimit_map_cocone_cofork_equiv' : is_colimit (G.map_cocone (cokernel_cofork.of_π h w)) ≃ is_colimit (cokernel_cofork.of_π (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) : cofork (G.map f) 0) := begin refine (is_colimit.precompose_hom_equiv _ _).symm.trans (is_colimit.equiv_iso_colimit _), refine parallel_pair.ext (iso.refl _) (iso.refl _) _ _; simp, refine cofork.ext (iso.refl _) _, simp only [cofork.π, iso.refl_hom, id_comp, cocones.precompose_obj_ι, nat_trans.comp_app, parallel_pair.ext_hom_app, functor.map_cocone_ι_app, cofork.of_π_ι_app], apply category.comp_id end /-- The property of preserving cokernels expressed in terms of cokernel coforks. This is a variant of `is_colimit_cofork_map_of_is_colimit` for equalizers, which we can't use directly between `G.map 0 = 0` does not hold definitionally. -/ def is_colimit_cofork_map_of_is_colimit' [preserves_colimit (parallel_pair f 0) G] (l : is_colimit (cokernel_cofork.of_π h w)) : is_colimit (cokernel_cofork.of_π (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) : cofork (G.map f) 0) := is_colimit_map_cocone_cofork_equiv' G w (preserves_colimit.preserves l) variables (f) [has_cokernel f] /-- If `G` preserves cokernels and `C` has them, then the cofork constructed of the mapped morphisms of a cokernel cofork is a colimit. -/ def is_colimit_of_has_cokernel_of_preserves_colimit [preserves_colimit (parallel_pair f 0) G] : is_colimit (cofork.of_π (G.map (cokernel.π f)) (by simp only [←G.map_comp, coequalizer.condition, zero_comp, functor.map_zero]) : cofork (G.map f) 0) := is_colimit_cofork_map_of_is_colimit' G (cokernel.condition f) (cokernel_is_cokernel f) instance [preserves_colimit (parallel_pair f 0) G] : has_cokernel (G.map f) := { exists_colimit := ⟨⟨_, is_colimit_of_has_cokernel_of_preserves_colimit G f⟩⟩, } variables [has_cokernel (G.map f)] /-- If the cokernel comparison map for `G` at `f` is an isomorphism, then `G` preserves the cokernel of `f`. -/ def preserves_cokernel.of_iso_comparison [i : is_iso (cokernel_comparison f G)] : preserves_colimit (parallel_pair f 0) G := begin apply preserves_colimit_of_preserves_colimit_cocone (cokernel_is_cokernel f), apply (is_colimit_map_cocone_cofork_equiv' G (cokernel.condition f)).symm _, apply is_colimit.of_point_iso (colimit.is_colimit (parallel_pair (G.map f) 0)), apply i, end variables [preserves_colimit (parallel_pair f 0) G] /-- If `G` preserves the cokernel of `f`, then the cokernel comparison map for `G` at `f` is an isomorphism. -/ def preserves_cokernel.iso : G.obj (cokernel f) ≅ cokernel (G.map f) := is_colimit.cocone_point_unique_up_to_iso (is_colimit_of_has_cokernel_of_preserves_colimit G f) (colimit.is_colimit _) @[simp] lemma preserves_cokernel.iso_inv : (preserves_cokernel.iso G f).inv = cokernel_comparison f G := rfl instance : is_iso (cokernel_comparison f G) := begin rw ← preserves_cokernel.iso_inv, apply_instance end end cokernels end category_theory.limits
164c501a7d4c5f2372a3df538a9aa2107acedfbf
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/injIssue.lean
ec0a95f0c7c8259dad8d4795d89ec59a50ad341b
[ "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
99
lean
theorem ex1 (n m : Nat) : some n = some m → n = m := by intro h injection h with h exact h
0df8d834e8018b938e901adc98866d16bccd0efc
b7f22e51856f4989b970961f794f1c435f9b8f78
/hott/algebra/category/constructions/comma.hlean
ea89813b8fcc0437e8d9548d50f1b863529d2fab
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
6,767
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Comma category -/ import ..functor.basic ..strict ..category open eq functor equiv sigma sigma.ops is_trunc iso is_equiv namespace category structure comma_object {A B C : Precategory} (S : A ⇒ C) (T : B ⇒ C) := (a : A) (b : B) (f : S a ⟶ T b) abbreviation ob1 [unfold 6] := @comma_object.a abbreviation ob2 [unfold 6] := @comma_object.b abbreviation mor [unfold 6] := @comma_object.f variables {A B C : Precategory} (S : A ⇒ C) (T : B ⇒ C) definition comma_object_sigma_char : (Σ(a : A) (b : B), S a ⟶ T b) ≃ comma_object S T := begin fapply equiv.MK, { intro u, exact comma_object.mk u.1 u.2.1 u.2.2}, { intro x, cases x with a b f, exact ⟨a, b, f⟩}, { intro x, cases x, reflexivity}, { intro u, cases u with u1 u2, cases u2, reflexivity}, end theorem is_trunc_comma_object (n : trunc_index) [HA : is_trunc n A] [HB : is_trunc n B] [H : Π(s d : C), is_trunc n (hom s d)] : is_trunc n (comma_object S T) := by apply is_trunc_equiv_closed;apply comma_object_sigma_char variables {S T} definition comma_object_eq' {x y : comma_object S T} (p : ob1 x = ob1 y) (q : ob2 x = ob2 y) (r : mor x =[ap011 (@hom C C) (ap (to_fun_ob S) p) (ap (to_fun_ob T) q)] mor y) : x = y := begin cases x with a b f, cases y with a' b' f', cases p, cases q, esimp [ap011,congr,ap,subst] at r, eapply (idp_rec_on r), reflexivity end --TODO: remove. This is a different version where Hq is not in square brackets -- definition eq_comp_inverse_of_comp_eq' {ob : Type} {C : precategory ob} {d c b : ob} {r : hom c d} -- {q : hom b c} {x : hom b d} {Hq : is_iso q} (p : r ∘ q = x) : r = x ∘ q⁻¹ʰ := -- sorry -- := sorry --eq_inverse_comp_of_comp_eq p definition comma_object_eq {x y : comma_object S T} (p : ob1 x = ob1 y) (q : ob2 x = ob2 y) (r : T (hom_of_eq q) ∘ mor x ∘ S (inv_of_eq p) = mor y) : x = y := begin cases x with a b f, cases y with a' b' f', cases p, cases q, apply ap (comma_object.mk a' b'), rewrite [▸* at r, -r, +respect_id, id_leftright] end definition ap_ob1_comma_object_eq' (x y : comma_object S T) (p : ob1 x = ob1 y) (q : ob2 x = ob2 y) (r : mor x =[ap011 (@hom C C) (ap (to_fun_ob S) p) (ap (to_fun_ob T) q)] mor y) : ap ob1 (comma_object_eq' p q r) = p := begin cases x with a b f, cases y with a' b' f', cases p, cases q, eapply (idp_rec_on r), reflexivity end definition ap_ob2_comma_object_eq' (x y : comma_object S T) (p : ob1 x = ob1 y) (q : ob2 x = ob2 y) (r : mor x =[ap011 (@hom C C) (ap (to_fun_ob S) p) (ap (to_fun_ob T) q)] mor y) : ap ob2 (comma_object_eq' p q r) = q := begin cases x with a b f, cases y with a' b' f', cases p, cases q, eapply (idp_rec_on r), reflexivity end structure comma_morphism (x y : comma_object S T) := mk' :: (g : ob1 x ⟶ ob1 y) (h : ob2 x ⟶ ob2 y) (p : T h ∘ mor x = mor y ∘ S g) (p' : mor y ∘ S g = T h ∘ mor x) abbreviation mor1 := @comma_morphism.g abbreviation mor2 := @comma_morphism.h abbreviation coh := @comma_morphism.p abbreviation coh' := @comma_morphism.p' protected definition comma_morphism.mk [constructor] [reducible] {x y : comma_object S T} (g h p) : comma_morphism x y := comma_morphism.mk' g h p p⁻¹ variables (x y z w : comma_object S T) definition comma_morphism_sigma_char : (Σ(g : ob1 x ⟶ ob1 y) (h : ob2 x ⟶ ob2 y), T h ∘ mor x = mor y ∘ S g) ≃ comma_morphism x y := begin fapply equiv.MK, { intro u, exact (comma_morphism.mk u.1 u.2.1 u.2.2)}, { intro f, cases f with g h p p', exact ⟨g, h, p⟩}, { intro f, cases f with g h p p', esimp, apply ap (comma_morphism.mk' g h p), apply is_prop.elim}, { intro u, cases u with u1 u2, cases u2 with u2 u3, reflexivity}, end theorem is_trunc_comma_morphism (n : trunc_index) [H1 : is_trunc n (ob1 x ⟶ ob1 y)] [H2 : is_trunc n (ob2 x ⟶ ob2 y)] [Hp : Πm1 m2, is_trunc n (T m2 ∘ mor x = mor y ∘ S m1)] : is_trunc n (comma_morphism x y) := by apply is_trunc_equiv_closed; apply comma_morphism_sigma_char variables {x y z w} definition comma_morphism_eq {f f' : comma_morphism x y} (p : mor1 f = mor1 f') (q : mor2 f = mor2 f') : f = f' := begin cases f with g h p₁ p₁', cases f' with g' h' p₂ p₂', cases p, cases q, apply ap011 (comma_morphism.mk' g' h'), apply is_prop.elim, apply is_prop.elim end definition comma_compose (g : comma_morphism y z) (f : comma_morphism x y) : comma_morphism x z := comma_morphism.mk (mor1 g ∘ mor1 f) (mor2 g ∘ mor2 f) (by rewrite [+respect_comp,-assoc,coh,assoc,coh,-assoc]) local infix ` ∘∘ `:60 := comma_compose definition comma_id : comma_morphism x x := comma_morphism.mk id id (by rewrite [+respect_id,id_left,id_right]) theorem comma_assoc (h : comma_morphism z w) (g : comma_morphism y z) (f : comma_morphism x y) : h ∘∘ (g ∘∘ f) = (h ∘∘ g) ∘∘ f := comma_morphism_eq !assoc !assoc theorem comma_id_left (f : comma_morphism x y) : comma_id ∘∘ f = f := comma_morphism_eq !id_left !id_left theorem comma_id_right (f : comma_morphism x y) : f ∘∘ comma_id = f := comma_morphism_eq !id_right !id_right variables (S T) definition comma_category [constructor] : Precategory := precategory.MK (comma_object S T) comma_morphism (λa b, !is_trunc_comma_morphism) (@comma_compose _ _ _ _ _) (@comma_id _ _ _ _ _) (@comma_assoc _ _ _ _ _) (@comma_id_left _ _ _ _ _) (@comma_id_right _ _ _ _ _) --TODO: this definition doesn't use category structure of A and B definition strict_precategory_comma [HA : strict_precategory A] [HB : strict_precategory B] : strict_precategory (comma_object S T) := strict_precategory.mk (comma_category S T) !is_trunc_comma_object /- --set_option pp.notation false definition is_univalent_comma (HA : is_univalent A) (HB : is_univalent B) : is_univalent (comma_category S T) := begin intros c d, fapply adjointify, { intro i, cases i with f s, cases s with g l r, cases f with fA fB fp, cases g with gA gB gp, esimp at *, fapply comma_object_eq, {apply iso_of_eq⁻¹ᶠ, exact (iso.MK fA gA (ap mor1 l) (ap mor1 r))}, {apply iso_of_eq⁻¹ᶠ, exact (iso.MK fB gB (ap mor2 l) (ap mor2 r))}, { apply sorry /-rewrite hom_of_eq_eq_of_iso,-/ }}, { apply sorry}, { apply sorry}, end -/ end category
3fd5af2c7a47d870c2a9a11d81660bcdbcf6ad52
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/set/intervals/default.lean
b071ccd6fd329d16467c945275239278d1a2d7b6
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
80
lean
import data.set.intervals.disjoint import data.set.intervals.unordered_interval
aff21f49977c99ab0a4fc38196509e793b3c6911
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/sites/compatible_plus.lean
75a5242fe1bf1946977958d332794bb9918ea2e5
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
8,839
lean
/- Copyright (c) 2021 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import category_theory.sites.whiskering import category_theory.sites.plus /-! > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we prove that the plus functor is compatible with functors which preserve the correct limits and colimits. See `category_theory/sites/compatible_sheafification` for the compatibility of sheafification, which follows easily from the content in this file. -/ namespace category_theory.grothendieck_topology open category_theory open category_theory.limits open opposite universes w₁ w₂ v u variables {C : Type u} [category.{v} C] (J : grothendieck_topology C) variables {D : Type w₁} [category.{max v u} D] variables {E : Type w₂} [category.{max v u} E] variables (F : D ⥤ E) noncomputable theory variables [∀ (α β : Type (max v u)) (fst snd : β → α), has_limits_of_shape (walking_multicospan fst snd) D] variables [∀ (α β : Type (max v u)) (fst snd : β → α), has_limits_of_shape (walking_multicospan fst snd) E] variables [∀ (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D), preserves_limit (W.index P).multicospan F] variables (P : Cᵒᵖ ⥤ D) /-- The diagram used to define `P⁺`, composed with `F`, is isomorphic to the diagram used to define `P ⋙ F`. -/ def diagram_comp_iso (X : C) : J.diagram P X ⋙ F ≅ J.diagram (P ⋙ F) X := nat_iso.of_components (λ W, begin refine _ ≪≫ has_limit.iso_of_nat_iso (W.unop.multicospan_comp _ _).symm, refine (is_limit_of_preserves F (limit.is_limit _)).cone_point_unique_up_to_iso (limit.is_limit _) end) begin intros A B f, ext, dsimp, simp only [functor.map_cone_π_app, multiequalizer.multifork_π_app_left, iso.symm_hom, multiequalizer.lift_ι, eq_to_hom_refl, category.comp_id, limit.cone_point_unique_up_to_iso_hom_comp, grothendieck_topology.cover.multicospan_comp_hom_inv_left, has_limit.iso_of_nat_iso_hom_π, category.assoc], simp only [← F.map_comp, multiequalizer.lift_ι], end @[simp, reassoc] lemma diagram_comp_iso_hom_ι (X : C) (W : (J.cover X)ᵒᵖ) (i : W.unop.arrow): (J.diagram_comp_iso F P X).hom.app W ≫ multiequalizer.ι _ i = F.map (multiequalizer.ι _ _) := begin delta diagram_comp_iso, dsimp, simp, end variables [∀ (X : C), has_colimits_of_shape (J.cover X)ᵒᵖ D] variables [∀ (X : C), has_colimits_of_shape (J.cover X)ᵒᵖ E] variables [∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ F] /-- The isomorphism between `P⁺ ⋙ F` and `(P ⋙ F)⁺`. -/ def plus_comp_iso : J.plus_obj P ⋙ F ≅ J.plus_obj (P ⋙ F) := nat_iso.of_components (λ X, begin refine _ ≪≫ has_colimit.iso_of_nat_iso (J.diagram_comp_iso F P X.unop), refine (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P (unop X)))).cocone_point_unique_up_to_iso (colimit.is_colimit _) end) begin intros X Y f, apply (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P X.unop))).hom_ext, intros W, dsimp [plus_obj, plus_map], simp only [functor.map_comp, category.assoc], slice_rhs 1 2 { erw (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P X.unop))).fac }, slice_lhs 1 3 { simp only [← F.map_comp], dsimp [colim_map, is_colimit.map, colimit.pre], simp only [colimit.ι_desc_assoc, colimit.ι_desc], dsimp [cocones.precompose], rw [category.assoc, colimit.ι_desc], dsimp [cocone.whisker], rw F.map_comp }, simp only [category.assoc], slice_lhs 2 3 { erw (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P Y.unop))).fac }, dsimp, simp only [has_colimit.iso_of_nat_iso_ι_hom_assoc, grothendieck_topology.diagram_pullback_app, colimit.ι_pre, has_colimit.iso_of_nat_iso_ι_hom, ι_colim_map_assoc], simp only [← category.assoc], congr' 1, ext, dsimp, simp only [category.assoc], erw [multiequalizer.lift_ι, diagram_comp_iso_hom_ι, diagram_comp_iso_hom_ι, ← F.map_comp, multiequalizer.lift_ι], end @[simp, reassoc] lemma ι_plus_comp_iso_hom (X) (W) : F.map (colimit.ι _ W) ≫ (J.plus_comp_iso F P).hom.app X = (J.diagram_comp_iso F P X.unop).hom.app W ≫ colimit.ι _ W := begin delta diagram_comp_iso plus_comp_iso, simp only [is_colimit.desc_cocone_morphism_hom, is_colimit.unique_up_to_iso_hom, cocones.forget_map, iso.trans_hom, nat_iso.of_components_hom_app, functor.map_iso_hom, ← category.assoc], erw (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P (unop X)))).fac, simp only [category.assoc, has_limit.iso_of_nat_iso_hom_π, iso.symm_hom, cover.multicospan_comp_hom_inv_left, eq_to_hom_refl, category.comp_id, limit.cone_point_unique_up_to_iso_hom_comp, functor.map_cone_π_app, multiequalizer.multifork_π_app_left, multiequalizer.lift_ι, functor.map_comp, eq_self_iff_true, category.assoc, iso.trans_hom, iso.cancel_iso_hom_left, nat_iso.of_components_hom_app, colimit.cocone_ι, category.assoc, has_colimit.iso_of_nat_iso_ι_hom], end @[simp, reassoc] lemma plus_comp_iso_whisker_left {F G : D ⥤ E} (η : F ⟶ G) (P : Cᵒᵖ ⥤ D) [∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ F] [∀ (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D), preserves_limit (W.index P).multicospan F] [∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ G] [∀ (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D), preserves_limit (W.index P).multicospan G] : whisker_left _ η ≫ (J.plus_comp_iso G P).hom = (J.plus_comp_iso F P).hom ≫ J.plus_map (whisker_left _ η) := begin ext X, apply (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P X.unop))).hom_ext, intros W, dsimp [plus_obj, plus_map], simp only [ι_plus_comp_iso_hom, ι_colim_map, whisker_left_app, ι_plus_comp_iso_hom_assoc, nat_trans.naturality_assoc, grothendieck_topology.diagram_nat_trans_app], simp only [← category.assoc], congr' 1, ext, dsimp, simpa, end /-- The isomorphism between `P⁺ ⋙ F` and `(P ⋙ F)⁺`, functorially in `F`. -/ @[simps hom_app inv_app] def plus_functor_whisker_left_iso (P : Cᵒᵖ ⥤ D) [∀ (F : D ⥤ E) (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ F] [∀ (F : D ⥤ E) (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D), preserves_limit (W.index P).multicospan F] : (whiskering_left _ _ E).obj (J.plus_obj P) ≅ (whiskering_left _ _ _).obj P ⋙ J.plus_functor E := nat_iso.of_components (λ X, plus_comp_iso _ _ _) $ λ F G η, plus_comp_iso_whisker_left _ _ _ @[simp, reassoc] lemma plus_comp_iso_whisker_right {P Q : Cᵒᵖ ⥤ D} (η : P ⟶ Q) : whisker_right (J.plus_map η) F ≫ (J.plus_comp_iso F Q).hom = (J.plus_comp_iso F P).hom ≫ J.plus_map (whisker_right η F) := begin ext X, apply (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P X.unop))).hom_ext, intros W, dsimp [plus_obj, plus_map], simp only [ι_colim_map, whisker_right_app, ι_plus_comp_iso_hom_assoc, grothendieck_topology.diagram_nat_trans_app], simp only [← category.assoc, ← F.map_comp], dsimp [colim_map, is_colimit.map], simp only [colimit.ι_desc], dsimp [cocones.precompose], simp only [functor.map_comp, category.assoc, ι_plus_comp_iso_hom], simp only [← category.assoc], congr' 1, ext, dsimp, simp only [diagram_comp_iso_hom_ι_assoc, multiequalizer.lift_ι, diagram_comp_iso_hom_ι, category.assoc], simp only [← F.map_comp, multiequalizer.lift_ι], end /-- The isomorphism between `P⁺ ⋙ F` and `(P ⋙ F)⁺`, functorially in `P`. -/ @[simps hom_app inv_app] def plus_functor_whisker_right_iso : J.plus_functor D ⋙ (whiskering_right _ _ _).obj F ≅ (whiskering_right _ _ _).obj F ⋙ J.plus_functor E := nat_iso.of_components (λ P, J.plus_comp_iso _ _) $ λ P Q η, plus_comp_iso_whisker_right _ _ _ @[simp, reassoc] lemma whisker_right_to_plus_comp_plus_comp_iso_hom : whisker_right (J.to_plus _) _ ≫ (J.plus_comp_iso F P).hom = J.to_plus _ := begin ext, dsimp [to_plus], simp only [ι_plus_comp_iso_hom, functor.map_comp, category.assoc], simp only [← category.assoc], congr' 1, ext, delta cover.to_multiequalizer, simp only [diagram_comp_iso_hom_ι, category.assoc, ← F.map_comp], erw [multiequalizer.lift_ι, multiequalizer.lift_ι], refl, end @[simp] lemma to_plus_comp_plus_comp_iso_inv : J.to_plus _ ≫ (J.plus_comp_iso F P).inv = whisker_right (J.to_plus _) _ := by simp [iso.comp_inv_eq] lemma plus_comp_iso_inv_eq_plus_lift (hP : presheaf.is_sheaf J ((J.plus_obj P) ⋙ F)) : (J.plus_comp_iso F P).inv = J.plus_lift (whisker_right (J.to_plus _) _) hP := by { apply J.plus_lift_unique, simp [iso.comp_inv_eq] } end category_theory.grothendieck_topology
f685b732eee6e79ff47bd9f3a6a1d2b084a2619a
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/special_functions/sqrt.lean
4d47dc7972a8b24e3c8d7c547f013d1ec8cef98b
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
6,367
lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.calculus.cont_diff /-! # Smoothness of `real.sqrt` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove that `real.sqrt` is infinitely smooth at all points `x ≠ 0` and provide some dot-notation lemmas. ## Tags sqrt, differentiable -/ open set open_locale topology namespace real /-- Local homeomorph between `(0, +∞)` and `(0, +∞)` with `to_fun = λ x, x ^ 2` and `inv_fun = sqrt`. -/ noncomputable def sq_local_homeomorph : local_homeomorph ℝ ℝ := { to_fun := λ x, x ^ 2, inv_fun := sqrt, source := Ioi 0, target := Ioi 0, map_source' := λ x hx, mem_Ioi.2 (pow_pos hx _), map_target' := λ x hx, mem_Ioi.2 (sqrt_pos.2 hx), left_inv' := λ x hx, sqrt_sq (le_of_lt hx), right_inv' := λ x hx, sq_sqrt (le_of_lt hx), open_source := is_open_Ioi, open_target := is_open_Ioi, continuous_to_fun := (continuous_pow 2).continuous_on, continuous_inv_fun := continuous_on_id.sqrt } lemma deriv_sqrt_aux {x : ℝ} (hx : x ≠ 0) : has_strict_deriv_at sqrt (1 / (2 * sqrt x)) x ∧ ∀ n, cont_diff_at ℝ n sqrt x := begin cases hx.lt_or_lt with hx hx, { rw [sqrt_eq_zero_of_nonpos hx.le, mul_zero, div_zero], have : sqrt =ᶠ[𝓝 x] (λ _, 0) := (gt_mem_nhds hx).mono (λ x hx, sqrt_eq_zero_of_nonpos hx.le), exact ⟨(has_strict_deriv_at_const x (0 : ℝ)).congr_of_eventually_eq this.symm, λ n, cont_diff_at_const.congr_of_eventually_eq this⟩ }, { have : ↑2 * sqrt x ^ (2 - 1) ≠ 0, by simp [(sqrt_pos.2 hx).ne', @two_ne_zero ℝ], split, { simpa using sq_local_homeomorph.has_strict_deriv_at_symm hx this (has_strict_deriv_at_pow 2 _) }, { exact λ n, sq_local_homeomorph.cont_diff_at_symm_deriv this hx (has_deriv_at_pow 2 (sqrt x)) (cont_diff_at_id.pow 2) } } end lemma has_strict_deriv_at_sqrt {x : ℝ} (hx : x ≠ 0) : has_strict_deriv_at sqrt (1 / (2 * sqrt x)) x := (deriv_sqrt_aux hx).1 lemma cont_diff_at_sqrt {x : ℝ} {n : ℕ∞} (hx : x ≠ 0) : cont_diff_at ℝ n sqrt x := (deriv_sqrt_aux hx).2 n lemma has_deriv_at_sqrt {x : ℝ} (hx : x ≠ 0) : has_deriv_at sqrt (1 / (2 * sqrt x)) x := (has_strict_deriv_at_sqrt hx).has_deriv_at end real open real section deriv variables {f : ℝ → ℝ} {s : set ℝ} {f' x : ℝ} lemma has_deriv_within_at.sqrt (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) : has_deriv_within_at (λ y, sqrt (f y)) (f' / (2 * sqrt (f x))) s x := by simpa only [(∘), div_eq_inv_mul, mul_one] using (has_deriv_at_sqrt hx).comp_has_deriv_within_at x hf lemma has_deriv_at.sqrt (hf : has_deriv_at f f' x) (hx : f x ≠ 0) : has_deriv_at (λ y, sqrt (f y)) (f' / (2 * sqrt(f x))) x := by simpa only [(∘), div_eq_inv_mul, mul_one] using (has_deriv_at_sqrt hx).comp x hf lemma has_strict_deriv_at.sqrt (hf : has_strict_deriv_at f f' x) (hx : f x ≠ 0) : has_strict_deriv_at (λ t, sqrt (f t)) (f' / (2 * sqrt (f x))) x := by simpa only [(∘), div_eq_inv_mul, mul_one] using (has_strict_deriv_at_sqrt hx).comp x hf lemma deriv_within_sqrt (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, sqrt (f x)) s x = (deriv_within f s x) / (2 * sqrt (f x)) := (hf.has_deriv_within_at.sqrt hx).deriv_within hxs @[simp] lemma deriv_sqrt (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : deriv (λx, sqrt (f x)) x = (deriv f x) / (2 * sqrt (f x)) := (hf.has_deriv_at.sqrt hx).deriv end deriv section fderiv variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] {f : E → ℝ} {n : ℕ∞} {s : set E} {x : E} {f' : E →L[ℝ] ℝ} lemma has_fderiv_at.sqrt (hf : has_fderiv_at f f' x) (hx : f x ≠ 0) : has_fderiv_at (λ y, sqrt (f y)) ((1 / (2 * sqrt (f x))) • f') x := (has_deriv_at_sqrt hx).comp_has_fderiv_at x hf lemma has_strict_fderiv_at.sqrt (hf : has_strict_fderiv_at f f' x) (hx : f x ≠ 0) : has_strict_fderiv_at (λ y, sqrt (f y)) ((1 / (2 * sqrt (f x))) • f') x := (has_strict_deriv_at_sqrt hx).comp_has_strict_fderiv_at x hf lemma has_fderiv_within_at.sqrt (hf : has_fderiv_within_at f f' s x) (hx : f x ≠ 0) : has_fderiv_within_at (λ y, sqrt (f y)) ((1 / (2 * sqrt (f x))) • f') s x := (has_deriv_at_sqrt hx).comp_has_fderiv_within_at x hf lemma differentiable_within_at.sqrt (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) : differentiable_within_at ℝ (λ y, sqrt (f y)) s x := (hf.has_fderiv_within_at.sqrt hx).differentiable_within_at lemma differentiable_at.sqrt (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : differentiable_at ℝ (λ y, sqrt (f y)) x := (hf.has_fderiv_at.sqrt hx).differentiable_at lemma differentiable_on.sqrt (hf : differentiable_on ℝ f s) (hs : ∀ x ∈ s, f x ≠ 0) : differentiable_on ℝ (λ y, sqrt (f y)) s := λ x hx, (hf x hx).sqrt (hs x hx) lemma differentiable.sqrt (hf : differentiable ℝ f) (hs : ∀ x, f x ≠ 0) : differentiable ℝ (λ y, sqrt (f y)) := λ x, (hf x).sqrt (hs x) lemma fderiv_within_sqrt (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : fderiv_within ℝ (λx, sqrt (f x)) s x = (1 / (2 * sqrt (f x))) • fderiv_within ℝ f s x := (hf.has_fderiv_within_at.sqrt hx).fderiv_within hxs @[simp] lemma fderiv_sqrt (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : fderiv ℝ (λx, sqrt (f x)) x = (1 / (2 * sqrt (f x))) • fderiv ℝ f x := (hf.has_fderiv_at.sqrt hx).fderiv lemma cont_diff_at.sqrt (hf : cont_diff_at ℝ n f x) (hx : f x ≠ 0) : cont_diff_at ℝ n (λ y, sqrt (f y)) x := (cont_diff_at_sqrt hx).comp x hf lemma cont_diff_within_at.sqrt (hf : cont_diff_within_at ℝ n f s x) (hx : f x ≠ 0) : cont_diff_within_at ℝ n (λ y, sqrt (f y)) s x := (cont_diff_at_sqrt hx).comp_cont_diff_within_at x hf lemma cont_diff_on.sqrt (hf : cont_diff_on ℝ n f s) (hs : ∀ x ∈ s, f x ≠ 0) : cont_diff_on ℝ n (λ y, sqrt (f y)) s := λ x hx, (hf x hx).sqrt (hs x hx) lemma cont_diff.sqrt (hf : cont_diff ℝ n f) (h : ∀ x, f x ≠ 0) : cont_diff ℝ n (λ y, sqrt (f y)) := cont_diff_iff_cont_diff_at.2 $ λ x, (hf.cont_diff_at.sqrt (h x)) end fderiv
d3a087dba91c929b4fb6414c18b1fd6245fc1743
6e41ee3ac9b96e8980a16295cc21f131e731884f
/library/algebra/category/constructions.lean
88b007ff5e75b188ba3fc3d7bdf82bd6696be9f6
[ "Apache-2.0" ]
permissive
EgbertRijke/lean
3426cfa0e5b3d35d12fc3fd7318b35574cb67dc3
4f2e0c6d7fc9274d953cfa1c37ab2f3e799ab183
refs/heads/master
1,610,834,871,476
1,422,159,801,000
1,422,159,801,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,851
lean
-- Copyright (c) 2014 Floris van Doorn. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Floris van Doorn -- This file contains basic constructions on categories, including common categories import .natural_transformation import data.unit data.sigma data.prod data.empty data.bool open eq eq.ops prod namespace category namespace opposite section definition opposite [reducible] {ob : Type} (C : category ob) : category ob := mk (λa b, hom b a) (λ a b c f g, g ∘ f) (λ a, id) (λ a b c d f g h, symm !assoc) (λ a b f, !id_right) (λ a b f, !id_left) definition Opposite [reducible] (C : Category) : Category := Mk (opposite C) --direct construction: -- MK C -- (λa b, hom b a) -- (λ a b c f g, g ∘ f) -- (λ a, id) -- (λ a b c d f g h, symm !assoc) -- (λ a b f, !id_right) -- (λ a b f, !id_left) infixr `∘op`:60 := @compose _ (opposite _) _ _ _ variables {C : Category} {a b c : C} theorem compose_op {f : hom a b} {g : hom b c} : f ∘op g = g ∘ f := rfl theorem op_op' {ob : Type} (C : category ob) : opposite (opposite C) = C := category.rec (λ hom comp id assoc idl idr, refl (mk _ _ _ _ _ _)) C theorem op_op : Opposite (Opposite C) = C := (@congr_arg _ _ _ _ (Category.mk C) (op_op' C)) ⬝ !Category.equal end end opposite definition type_category [reducible] : category Type := mk (λa b, a → b) (λ a b c, function.compose) (λ a, function.id) (λ a b c d h g f, symm (function.compose.assoc h g f)) (λ a b f, function.compose.left_id f) (λ a b f, function.compose.right_id f) definition Type_category [reducible] : Category := Mk type_category section open decidable unit empty variables {A : Type} [H : decidable_eq A] include H definition set_hom [reducible] (a b : A) := decidable.rec_on (H a b) (λh, unit) (λh, empty) theorem set_hom_subsingleton [instance] (a b : A) : subsingleton (set_hom a b) := _ definition set_compose [reducible] {a b c : A} (g : set_hom b c) (f : set_hom a b) : set_hom a c := decidable.rec_on (H b c) (λ Hbc g, decidable.rec_on (H a b) (λ Hab f, rec_on_true (trans Hab Hbc) ⋆) (λh f, empty.rec _ f) f) (λh (g : empty), empty.rec _ g) g omit H definition discrete_category (A : Type) [H : decidable_eq A] : category A := mk (λa b, set_hom a b) (λ a b c g f, set_compose g f) (λ a, decidable.rec_on_true rfl ⋆) (λ a b c d h g f, @subsingleton.elim (set_hom a d) _ _ _) (λ a b f, @subsingleton.elim (set_hom a b) _ _ _) (λ a b f, @subsingleton.elim (set_hom a b) _ _ _) attribute discrete_category [reducible] definition Discrete_category (A : Type) [H : decidable_eq A] := Mk (discrete_category A) context attribute discrete_category [instance] include H theorem dicrete_category.endomorphism {a b : A} (f : a ⟶ b) : a = b := decidable.rec_on (H a b) (λh f, h) (λh f, empty.rec _ f) f theorem dicrete_category.discrete {a b : A} (f : a ⟶ b) : eq.rec_on (dicrete_category.endomorphism f) f = (ID b) := @subsingleton.elim _ !set_hom_subsingleton _ _ definition discrete_category.rec_on {P : Πa b, a ⟶ b → Type} {a b : A} (f : a ⟶ b) (H : ∀a, P a a id) : P a b f := cast (dcongr_arg3 P rfl (dicrete_category.endomorphism f⁻¹) (@subsingleton.elim _ !set_hom_subsingleton _ _) ⁻¹) (H a) end end section open unit bool definition category_one := discrete_category unit definition Category_one := Mk category_one definition category_two := discrete_category bool definition Category_two := Mk category_two end namespace product section open prod definition prod_category [reducible] {obC obD : Type} (C : category obC) (D : category obD) : category (obC × obD) := mk (λa b, hom (pr1 a) (pr1 b) × hom (pr2 a) (pr2 b)) (λ a b c g f, (pr1 g ∘ pr1 f , pr2 g ∘ pr2 f) ) (λ a, (id,id)) (λ a b c d h g f, pair_eq !assoc !assoc ) (λ a b f, prod.equal !id_left !id_left ) (λ a b f, prod.equal !id_right !id_right) definition Prod_category [reducible] (C D : Category) : Category := Mk (prod_category C D) end end product namespace ops notation `type`:max := Type_category notation 1 := Category_one notation 2 := Category_two postfix `ᵒᵖ`:max := opposite.Opposite infixr `×c`:30 := product.Prod_category persistent attribute type_category [instance] persistent attribute category_one [instance] persistent attribute category_two [instance] persistent attribute product.prod_category [instance] end ops open ops namespace opposite section open functor definition opposite_functor [reducible] {C D : Category} (F : C ⇒ D) : Cᵒᵖ ⇒ Dᵒᵖ := @functor.mk (Cᵒᵖ) (Dᵒᵖ) (λ a, F a) (λ a b f, F f) (λ a, respect_id F a) (λ a b c g f, respect_comp F f g) end end opposite namespace product section open ops functor definition prod_functor [reducible] {C C' D D' : Category} (F : C ⇒ D) (G : C' ⇒ D') : C ×c C' ⇒ D ×c D' := functor.mk (λ a, pair (F (pr1 a)) (G (pr2 a))) (λ a b f, pair (F (pr1 f)) (G (pr2 f))) (λ a, pair_eq !respect_id !respect_id) (λ a b c g f, pair_eq !respect_comp !respect_comp) end end product namespace ops infixr `×f`:30 := product.prod_functor infixr `ᵒᵖᶠ`:max := opposite.opposite_functor end ops section functor_category variables (C D : Category) definition functor_category : category (functor C D) := mk (λa b, natural_transformation a b) (λ a b c g f, natural_transformation.compose g f) (λ a, natural_transformation.id) (λ a b c d h g f, !natural_transformation.assoc) (λ a b f, !natural_transformation.id_left) (λ a b f, !natural_transformation.id_right) end functor_category namespace slice open sigma function variables {ob : Type} {C : category ob} {c : ob} protected definition slice_obs (C : category ob) (c : ob) := Σ(b : ob), hom b c variables {a b : slice_obs C c} protected definition to_ob (a : slice_obs C c) : ob := sigma.pr1 a protected definition to_ob_def (a : slice_obs C c) : to_ob a = sigma.pr1 a := rfl protected definition ob_hom (a : slice_obs C c) : hom (to_ob a) c := sigma.pr2 a -- protected theorem slice_obs_equal (H₁ : to_ob a = to_ob b) -- (H₂ : eq.drec_on H₁ (ob_hom a) = ob_hom b) : a = b := -- sigma.equal H₁ H₂ protected definition slice_hom (a b : slice_obs C c) : Type := Σ(g : hom (to_ob a) (to_ob b)), ob_hom b ∘ g = ob_hom a protected definition hom_hom (f : slice_hom a b) : hom (to_ob a) (to_ob b) := sigma.pr1 f protected definition commute (f : slice_hom a b) : ob_hom b ∘ (hom_hom f) = ob_hom a := sigma.pr2 f -- protected theorem slice_hom_equal (f g : slice_hom a b) (H : hom_hom f = hom_hom g) : f = g := -- sigma.equal H !proof_irrel definition slice_category (C : category ob) (c : ob) : category (slice_obs C c) := mk (λa b, slice_hom a b) (λ a b c g f, sigma.mk (hom_hom g ∘ hom_hom f) (show ob_hom c ∘ (hom_hom g ∘ hom_hom f) = ob_hom a, proof calc ob_hom c ∘ (hom_hom g ∘ hom_hom f) = (ob_hom c ∘ hom_hom g) ∘ hom_hom f : !assoc ... = ob_hom b ∘ hom_hom f : {commute g} ... = ob_hom a : {commute f} qed)) (λ a, sigma.mk id !id_right) (λ a b c d h g f, dpair_eq !assoc !proof_irrel) (λ a b f, sigma.equal !id_left !proof_irrel) (λ a b f, sigma.equal !id_right !proof_irrel) -- We use !proof_irrel instead of rfl, to give the unifier an easier time -- definition slice_category {ob : Type} (C : category ob) (c : ob) : category (Σ(b : ob), hom b c) -- := -- mk (λa b, Σ(g : hom (dpr1 a) (dpr1 b)), dpr2 b ∘ g = dpr2 a) -- (λ a b c g f, dpair (dpr1 g ∘ dpr1 f) -- (show dpr2 c ∘ (dpr1 g ∘ dpr1 f) = dpr2 a, -- proof -- calc -- dpr2 c ∘ (dpr1 g ∘ dpr1 f) = (dpr2 c ∘ dpr1 g) ∘ dpr1 f : !assoc -- ... = dpr2 b ∘ dpr1 f : {dpr2 g} -- ... = dpr2 a : {dpr2 f} -- qed)) -- (λ a, dpair id !id_right) -- (λ a b c d h g f, dpair_eq !assoc !proof_irrel) -- (λ a b f, sigma.equal !id_left !proof_irrel) -- (λ a b f, sigma.equal !id_right !proof_irrel) -- We use !proof_irrel instead of rfl, to give the unifier an easier time definition Slice_category [reducible] (C : Category) (c : C) := Mk (slice_category C c) open category.ops persistent attribute slice_category [instance] variables {D : Category} definition forgetful (x : D) : (Slice_category D x) ⇒ D := functor.mk (λ a, to_ob a) (λ a b f, hom_hom f) (λ a, rfl) (λ a b c g f, rfl) definition postcomposition_functor {x y : D} (h : x ⟶ y) : Slice_category D x ⇒ Slice_category D y := functor.mk (λ a, sigma.mk (to_ob a) (h ∘ ob_hom a)) (λ a b f, sigma.mk (hom_hom f) (calc (h ∘ ob_hom b) ∘ hom_hom f = h ∘ (ob_hom b ∘ hom_hom f) : assoc h (ob_hom b) (hom_hom f)⁻¹ ... = h ∘ ob_hom a : congr_arg (λx, h ∘ x) (commute f))) (λ a, rfl) (λ a b c g f, dpair_eq rfl !proof_irrel) -- -- in the following comment I tried to have (A = B) in the type of a == b, but that doesn't solve the problems -- definition heq2 {A B : Type} (H : A = B) (a : A) (b : B) := a == b -- definition heq2.intro {A B : Type} {a : A} {b : B} (H : a == b) : heq2 (heq.type_eq H) a b := H -- definition heq2.elim {A B : Type} {a : A} {b : B} (H : A = B) (H2 : heq2 H a b) : a == b := H2 -- definition heq2.proof_irrel {A B : Prop} (a : A) (b : B) (H : A = B) : heq2 H a b := -- hproof_irrel H a b -- theorem functor.mk_eq2 {C D : Category} {obF obG : C → D} {homF homG idF idG compF compG} -- (Hob : ∀x, obF x = obG x) -- (Hmor : ∀(a b : C) (f : a ⟶ b), heq2 (congr_arg (λ x, x a ⟶ x b) (funext Hob)) (homF a b f) (homG a b f)) -- : functor.mk obF homF idF compF = functor.mk obG homG idG compG := -- hddcongr_arg4 functor.mk -- (funext Hob) -- (hfunext (λ a, hfunext (λ b, hfunext (λ f, !Hmor)))) -- !proof_irrel -- !proof_irrel -- set_option pp.implicit true -- set_option pp.coercions true -- definition slice_functor : D ⇒ Category_of_categories := -- functor.mk (λ a, Category.mk (slice_obs D a) (slice_category D a)) -- (λ a b f, postcomposition_functor f) -- (λ a, functor.mk_heq -- (λx, sigma.equal rfl !id_left) -- (λb c f, sigma.hequal sorry !heq.refl (hproof_irrel sorry _ _))) -- (λ a b c g f, functor.mk_heq -- (λx, sigma.equal (sorry ⬝ refl (dpr1 x)) sorry) -- (λb c f, sorry)) --the error message generated here is really confusing: the type of the above refl should be -- "@dpr1 D (λ (a_1 : D), a_1 ⟶ a) x = @dpr1 D (λ (a_1 : D), a_1 ⟶ c) x", but the second dpr1 is not even well-typed end slice -- section coslice -- open sigma -- definition coslice {ob : Type} (C : category ob) (c : ob) : category (Σ(b : ob), hom c b) := -- mk (λa b, Σ(g : hom (dpr1 a) (dpr1 b)), g ∘ dpr2 a = dpr2 b) -- (λ a b c g f, dpair (dpr1 g ∘ dpr1 f) -- (show (dpr1 g ∘ dpr1 f) ∘ dpr2 a = dpr2 c, -- proof -- calc -- (dpr1 g ∘ dpr1 f) ∘ dpr2 a = dpr1 g ∘ (dpr1 f ∘ dpr2 a): symm !assoc -- ... = dpr1 g ∘ dpr2 b : {dpr2 f} -- ... = dpr2 c : {dpr2 g} -- qed)) -- (λ a, dpair id !id_left) -- (λ a b c d h g f, dpair_eq !assoc !proof_irrel) -- (λ a b f, sigma.equal !id_left !proof_irrel) -- (λ a b f, sigma.equal !id_right !proof_irrel) -- -- theorem slice_coslice_opp {ob : Type} (C : category ob) (c : ob) : -- -- coslice C c = opposite (slice (opposite C) c) := -- -- sorry -- end coslice section arrow open sigma eq.ops -- theorem concat_commutative_squares {ob : Type} {C : category ob} {a1 a2 a3 b1 b2 b3 : ob} -- {f1 : a1 => b1} {f2 : a2 => b2} {f3 : a3 => b3} {g2 : a2 => a3} {g1 : a1 => a2} -- {h2 : b2 => b3} {h1 : b1 => b2} (H1 : f2 ∘ g1 = h1 ∘ f1) (H2 : f3 ∘ g2 = h2 ∘ f2) -- : f3 ∘ (g2 ∘ g1) = (h2 ∘ h1) ∘ f1 := -- calc -- f3 ∘ (g2 ∘ g1) = (f3 ∘ g2) ∘ g1 : assoc -- ... = (h2 ∘ f2) ∘ g1 : {H2} -- ... = h2 ∘ (f2 ∘ g1) : symm assoc -- ... = h2 ∘ (h1 ∘ f1) : {H1} -- ... = (h2 ∘ h1) ∘ f1 : assoc -- definition arrow {ob : Type} (C : category ob) : category (Σ(a b : ob), hom a b) := -- mk (λa b, Σ(g : hom (dpr1 a) (dpr1 b)) (h : hom (dpr2' a) (dpr2' b)), -- dpr3 b ∘ g = h ∘ dpr3 a) -- (λ a b c g f, dpair (dpr1 g ∘ dpr1 f) (dpair (dpr2' g ∘ dpr2' f) (concat_commutative_squares (dpr3 f) (dpr3 g)))) -- (λ a, dpair id (dpair id (id_right ⬝ (symm id_left)))) -- (λ a b c d h g f, dtrip_eq2 assoc assoc !proof_irrel) -- (λ a b f, trip.equal2 id_left id_left !proof_irrel) -- (λ a b f, trip.equal2 id_right id_right !proof_irrel) -- make these definitions private? variables {ob : Type} {C : category ob} protected definition arrow_obs (ob : Type) (C : category ob) := Σ(a b : ob), hom a b variables {a b : arrow_obs ob C} protected definition src (a : arrow_obs ob C) : ob := sigma.pr1 a protected definition dst (a : arrow_obs ob C) : ob := sigma.pr2' a protected definition to_hom (a : arrow_obs ob C) : hom (src a) (dst a) := sigma.pr3 a protected definition arrow_hom (a b : arrow_obs ob C) : Type := Σ (g : hom (src a) (src b)) (h : hom (dst a) (dst b)), to_hom b ∘ g = h ∘ to_hom a protected definition hom_src (m : arrow_hom a b) : hom (src a) (src b) := sigma.pr1 m protected definition hom_dst (m : arrow_hom a b) : hom (dst a) (dst b) := sigma.pr2' m protected definition commute (m : arrow_hom a b) : to_hom b ∘ (hom_src m) = (hom_dst m) ∘ to_hom a := sigma.pr3 m definition arrow (ob : Type) (C : category ob) : category (arrow_obs ob C) := mk (λa b, arrow_hom a b) (λ a b c g f, sigma.mk (hom_src g ∘ hom_src f) (sigma.mk (hom_dst g ∘ hom_dst f) (show to_hom c ∘ (hom_src g ∘ hom_src f) = (hom_dst g ∘ hom_dst f) ∘ to_hom a, proof calc to_hom c ∘ (hom_src g ∘ hom_src f) = (to_hom c ∘ hom_src g) ∘ hom_src f : !assoc ... = (hom_dst g ∘ to_hom b) ∘ hom_src f : {commute g} ... = hom_dst g ∘ (to_hom b ∘ hom_src f) : symm !assoc ... = hom_dst g ∘ (hom_dst f ∘ to_hom a) : {commute f} ... = (hom_dst g ∘ hom_dst f) ∘ to_hom a : !assoc qed) )) (λ a, sigma.mk id (sigma.mk id (!id_right ⬝ (symm !id_left)))) (λ a b c d h g f, ndtrip_eq !assoc !assoc !proof_irrel) (λ a b f, ndtrip_equal !id_left !id_left !proof_irrel) (λ a b f, ndtrip_equal !id_right !id_right !proof_irrel) end arrow end category -- definition foo : category (sorry) := -- mk (λa b, sorry) -- (λ a b c g f, sorry) -- (λ a, sorry) -- (λ a b c d h g f, sorry) -- (λ a b f, sorry) -- (λ a b f, sorry)
25fd6872c565092fdb3359c64147b1acc6712379
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/library/init/classical.lean
bf2227b7a7136c6397d725b592cfe58da93ed621
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
5,702
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ prelude import init.data.subtype.basic init.funext namespace classical universes u v /- the axiom -/ axiom choice {α : Sort u} : nonempty α → α @[irreducible] noncomputable def indefinite_description {α : Sort u} (p : α → Prop) (h : ∃ x, p x) : {x // p x} := choice $ let ⟨x, px⟩ := h in ⟨⟨x, px⟩⟩ noncomputable def some {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : α := (indefinite_description p h).val theorem some_spec {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : p (some h) := (indefinite_description p h).property /- Diaconescu's theorem: using function extensionality and propositional extensionality, we can get excluded middle from this. -/ section diaconescu parameter p : Prop private def U (x : Prop) : Prop := x = true ∨ p private def V (x : Prop) : Prop := x = false ∨ p private lemma exU : ∃ x, U x := ⟨true, or.inl rfl⟩ private lemma exV : ∃ x, V x := ⟨false, or.inl rfl⟩ /- TODO(Leo): check why the code generator is not ignoring (some exU) when we mark u as def. -/ private lemma u : Prop := some exU private lemma v : Prop := some exV set_option type_context.unfold_lemmas true private lemma u_def : U u := some_spec exU private lemma v_def : V v := some_spec exV private lemma not_uv_or_p : u ≠ v ∨ p := or.elim u_def (assume hut : u = true, or.elim v_def (assume hvf : v = false, have hne : u ≠ v, from hvf.symm ▸ hut.symm ▸ true_ne_false, or.inl hne) or.inr) or.inr private lemma p_implies_uv (hp : p) : u = v := have hpred : U = V, from funext (assume x : Prop, have hl : (x = true ∨ p) → (x = false ∨ p), from assume a, or.inr hp, have hr : (x = false ∨ p) → (x = true ∨ p), from assume a, or.inr hp, show (x = true ∨ p) = (x = false ∨ p), from propext (iff.intro hl hr)), have h₀ : ∀ exU exV, @some _ U exU = @some _ V exV, from hpred ▸ λ exU exV, rfl, show u = v, from h₀ _ _ theorem em : p ∨ ¬p := or.elim not_uv_or_p (assume hne : u ≠ v, or.inr (mt p_implies_uv hne)) or.inl end diaconescu theorem exists_true_of_nonempty {α : Sort u} : nonempty α → ∃ x : α, true | ⟨x⟩ := ⟨x, trivial⟩ noncomputable def inhabited_of_nonempty {α : Sort u} (h : nonempty α) : inhabited α := ⟨choice h⟩ noncomputable def inhabited_of_exists {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : inhabited α := inhabited_of_nonempty (exists.elim h (λ w hw, ⟨w⟩)) /- all propositions are decidable -/ noncomputable def prop_decidable (a : Prop) : decidable a := choice $ or.elim (em a) (assume ha, ⟨is_true ha⟩) (assume hna, ⟨is_false hna⟩) local attribute [instance] prop_decidable noncomputable def decidable_inhabited (a : Prop) : inhabited (decidable a) := ⟨prop_decidable a⟩ local attribute [instance] decidable_inhabited noncomputable def type_decidable_eq (α : Sort u) : decidable_eq α := λ x y, prop_decidable (x = y) noncomputable def type_decidable (α : Sort u) : psum α (α → false) := match (prop_decidable (nonempty α)) with | (is_true hp) := psum.inl (@inhabited.default _ (inhabited_of_nonempty hp)) | (is_false hn) := psum.inr (λ a, absurd (nonempty.intro a) hn) end @[irreducible] noncomputable def strong_indefinite_description {α : Sort u} (p : α → Prop) (h : nonempty α) : {x : α // (∃ y : α, p y) → p x} := if hp : ∃ x : α, p x then let xp := indefinite_description _ hp in ⟨xp.val, λ h', xp.property⟩ else ⟨choice h, λ h, absurd h hp⟩ /- the Hilbert epsilon function -/ noncomputable def epsilon {α : Sort u} [h : nonempty α] (p : α → Prop) : α := (strong_indefinite_description p h).val theorem epsilon_spec_aux {α : Sort u} (h : nonempty α) (p : α → Prop) : (∃ y, p y) → p (@epsilon α h p) := (strong_indefinite_description p h).property theorem epsilon_spec {α : Sort u} {p : α → Prop} (hex : ∃ y, p y) : p (@epsilon α (nonempty_of_exists hex) p) := epsilon_spec_aux (nonempty_of_exists hex) p hex theorem epsilon_singleton {α : Sort u} (x : α) : @epsilon α ⟨x⟩ (λ y, y = x) = x := @epsilon_spec α (λ y, y = x) ⟨x, rfl⟩ /- the axiom of choice -/ theorem axiom_of_choice {α : Sort u} {β : α → Sort v} {r : Π x, β x → Prop} (h : ∀ x, ∃ y, r x y) : ∃ (f : Π x, β x), ∀ x, r x (f x) := ⟨_, λ x, some_spec (h x)⟩ theorem skolem {α : Sort u} {b : α → Sort v} {p : Π x, b x → Prop} : (∀ x, ∃ y, p x y) ↔ ∃ (f : Π x, b x), ∀ x, p x (f x) := ⟨axiom_of_choice, λ ⟨f, hw⟩ x, ⟨f x, hw x⟩⟩ theorem prop_complete (a : Prop) : a = true ∨ a = false := or.elim (em a) (λ t, or.inl (eq_true_intro t)) (λ f, or.inr (eq_false_intro f)) section aux attribute [elab_as_eliminator] theorem cases_true_false (p : Prop → Prop) (h1 : p true) (h2 : p false) (a : Prop) : p a := or.elim (prop_complete a) (assume ht : a = true, ht.symm ▸ h1) (assume hf : a = false, hf.symm ▸ h2) theorem cases_on (a : Prop) {p : Prop → Prop} (h1 : p true) (h2 : p false) : p a := cases_true_false p h1 h2 a -- this supercedes by_cases in decidable lemma by_cases {p q : Prop} (hpq : p → q) (hnpq : ¬p → q) : q := decidable.by_cases hpq hnpq -- this supercedes by_contradiction in decidable theorem by_contradiction {p : Prop} (h : ¬p → false) : p := decidable.by_contradiction h theorem eq_false_or_eq_true (a : Prop) : a = false ∨ a = true := (prop_complete a).symm end aux end classical
60692c7e150a81156d7b97b7ec406e6de15b68b1
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/TwoPointed01.lean
523ec9636c229386429e499b4808ea383fd2ea33
[]
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
5,034
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 TwoPointed01 structure TwoPointed01 (A : Type) : Type := (e1 : A) (e2 : A) open TwoPointed01 structure Sig (AS : Type) : Type := (e1S : AS) (e2S : AS) structure Product (A : Type) : Type := (e1P : (Prod A A)) (e2P : (Prod A A)) structure Hom {A1 : Type} {A2 : Type} (Tw1 : (TwoPointed01 A1)) (Tw2 : (TwoPointed01 A2)) : Type := (hom : (A1 → A2)) (pres_e1 : (hom (e1 Tw1)) = (e1 Tw2)) (pres_e2 : (hom (e2 Tw1)) = (e2 Tw2)) structure RelInterp {A1 : Type} {A2 : Type} (Tw1 : (TwoPointed01 A1)) (Tw2 : (TwoPointed01 A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_e1 : (interp (e1 Tw1) (e1 Tw2))) (interp_e2 : (interp (e2 Tw1) (e2 Tw2))) inductive TwoPointed01Term : Type | e1L : TwoPointed01Term | e2L : TwoPointed01Term open TwoPointed01Term inductive ClTwoPointed01Term (A : Type) : Type | sing : (A → ClTwoPointed01Term) | e1Cl : ClTwoPointed01Term | e2Cl : ClTwoPointed01Term open ClTwoPointed01Term inductive OpTwoPointed01Term (n : ℕ) : Type | v : ((fin n) → OpTwoPointed01Term) | e1OL : OpTwoPointed01Term | e2OL : OpTwoPointed01Term open OpTwoPointed01Term inductive OpTwoPointed01Term2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpTwoPointed01Term2) | sing2 : (A → OpTwoPointed01Term2) | e1OL2 : OpTwoPointed01Term2 | e2OL2 : OpTwoPointed01Term2 open OpTwoPointed01Term2 def simplifyCl {A : Type} : ((ClTwoPointed01Term A) → (ClTwoPointed01Term A)) | e1Cl := e1Cl | e2Cl := e2Cl | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpTwoPointed01Term n) → (OpTwoPointed01Term n)) | e1OL := e1OL | e2OL := e2OL | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpTwoPointed01Term2 n A) → (OpTwoPointed01Term2 n A)) | e1OL2 := e1OL2 | e2OL2 := e2OL2 | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((TwoPointed01 A) → (TwoPointed01Term → A)) | Tw e1L := (e1 Tw) | Tw e2L := (e2 Tw) def evalCl {A : Type} : ((TwoPointed01 A) → ((ClTwoPointed01Term A) → A)) | Tw (sing x1) := x1 | Tw e1Cl := (e1 Tw) | Tw e2Cl := (e2 Tw) def evalOpB {A : Type} {n : ℕ} : ((TwoPointed01 A) → ((vector A n) → ((OpTwoPointed01Term n) → A))) | Tw vars (v x1) := (nth vars x1) | Tw vars e1OL := (e1 Tw) | Tw vars e2OL := (e2 Tw) def evalOp {A : Type} {n : ℕ} : ((TwoPointed01 A) → ((vector A n) → ((OpTwoPointed01Term2 n A) → A))) | Tw vars (v2 x1) := (nth vars x1) | Tw vars (sing2 x1) := x1 | Tw vars e1OL2 := (e1 Tw) | Tw vars e2OL2 := (e2 Tw) def inductionB {P : (TwoPointed01Term → Type)} : ((P e1L) → ((P e2L) → (∀ (x : TwoPointed01Term) , (P x)))) | pe1l pe2l e1L := pe1l | pe1l pe2l e2L := pe2l def inductionCl {A : Type} {P : ((ClTwoPointed01Term A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((P e1Cl) → ((P e2Cl) → (∀ (x : (ClTwoPointed01Term A)) , (P x))))) | psing pe1cl pe2cl (sing x1) := (psing x1) | psing pe1cl pe2cl e1Cl := pe1cl | psing pe1cl pe2cl e2Cl := pe2cl def inductionOpB {n : ℕ} {P : ((OpTwoPointed01Term n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((P e1OL) → ((P e2OL) → (∀ (x : (OpTwoPointed01Term n)) , (P x))))) | pv pe1ol pe2ol (v x1) := (pv x1) | pv pe1ol pe2ol e1OL := pe1ol | pv pe1ol pe2ol e2OL := pe2ol def inductionOp {n : ℕ} {A : Type} {P : ((OpTwoPointed01Term2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((P e1OL2) → ((P e2OL2) → (∀ (x : (OpTwoPointed01Term2 n A)) , (P x)))))) | pv2 psing2 pe1ol2 pe2ol2 (v2 x1) := (pv2 x1) | pv2 psing2 pe1ol2 pe2ol2 (sing2 x1) := (psing2 x1) | pv2 psing2 pe1ol2 pe2ol2 e1OL2 := pe1ol2 | pv2 psing2 pe1ol2 pe2ol2 e2OL2 := pe2ol2 def stageB : (TwoPointed01Term → (Staged TwoPointed01Term)) | e1L := (Now e1L) | e2L := (Now e2L) def stageCl {A : Type} : ((ClTwoPointed01Term A) → (Staged (ClTwoPointed01Term A))) | (sing x1) := (Now (sing x1)) | e1Cl := (Now e1Cl) | e2Cl := (Now e2Cl) def stageOpB {n : ℕ} : ((OpTwoPointed01Term n) → (Staged (OpTwoPointed01Term n))) | (v x1) := (const (code (v x1))) | e1OL := (Now e1OL) | e2OL := (Now e2OL) def stageOp {n : ℕ} {A : Type} : ((OpTwoPointed01Term2 n A) → (Staged (OpTwoPointed01Term2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | e1OL2 := (Now e1OL2) | e2OL2 := (Now e2OL2) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (e1T : (Repr A)) (e2T : (Repr A)) end TwoPointed01
17ff00cb171ac82803a232b9e5d47425009c05d4
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/meta/congr_lemma.lean
ad9cb7de5d5f098480a3801fdd24090602f38475
[ "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
6,391
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.tactic init.meta.format init.function /-- This is a kind attached to an argument of a congruence lemma that tells the simplifier how to fill it in. - `fixed`: It is a parameter for the congruence lemma, the parameter occurs in the left and right hand sides. For example the α in the congruence generated from `f: Π {α : Type} α → α`. - `fixed_no_param`: It is not a parameter for the congruence lemma, the lemma was specialized for this parameter. This only happens if the parameter is a subsingleton/proposition, and other parameters depend on it. - `eq`: The lemma contains three parameters for this kind of argument `a_i`, `b_i` and `(eq_i : a_i = b_i)`. `a_i` and `b_i` represent the left and right hand sides, and `eq_i` is a proof for their equality. For example the second argument in `f: Π {α : Type}, α → α`. - `cast`: corresponds to arguments that are subsingletons/propositions. For example the `p` in the congruence generated from `f : Π (x y : ℕ) (p: x < y), ℕ`. - `heq` The lemma contains three parameters for this kind of argument `a_i`, `b_i` and `(eq_i : a_i == b_i)`. `a_i` and `b_i` represent the left and right hand sides, and eq_i is a proof for their heterogeneous equality. -/ inductive congr_arg_kind | fixed | fixed_no_param | eq | cast | heq | subsingleton_inst namespace congr_arg_kind def to_string : congr_arg_kind → string | fixed := "fixed" | fixed_no_param := "fixed_no_param" | eq := "eq" | cast := "cast" | heq := "heq" | subsingleton_inst := "subsingleton_inst" instance : has_repr congr_arg_kind := ⟨to_string⟩ meta instance : has_to_format congr_arg_kind := ⟨λ x, to_string x⟩ end congr_arg_kind /-- A congruence lemma is a proof that two terms are equal using a congruence proof generated by `mk_congr_lemma_simp` and friends. See the docstring for `mk_congr_lemma_simp` and `congr_arg_kind` for more information. The conclusion is prepended by a set of arguments. `arg_kinds` gives a suggestion of how that argument should be filled in using a simplifier. -/ meta structure congr_lemma := (type : expr) (proof : expr) (arg_kinds : list congr_arg_kind) namespace tactic /-- `mk_congr_lemma_simp f nargs md` creates a congruence lemma for the simplifier for the given function argument `f`. If `nargs` is not none, then it tries to create a lemma for an application of arity `nargs`. If `nargs` is none then the number of arguments will be guessed from the type signature of `f`. That is, given `f : Π {α β γ δ : Type}, α → β → γ → δ` and `nargs = some 6`, we get a congruence lemma: ``` lean { type := ∀ (α β γ δ : Type), ∀ (a₁ a₂ : α), a₁ = a₂ → ∀ (b₁ b₂ : β), b₁ = b₂ → f a₁ b₁ = f a₂ b₂ , proof := ... , arg_kinds := [fixed, fixed, fixed, fixed, eq,eq] } ``` See the docstrings for the cases of `congr_arg_kind` for more detail on how `arg_kinds` are chosen. The system chooses the `arg_kinds` depending on what the other arguments depend on and whether the arguments have subsingleton types. Note that the number of arguments that `proof` takes can be inferred from `arg_kinds`: `arg_kinds.sum (fixed,cast ↦ 1 | eq,heq ↦ 3 | fixed_no_param ↦ 0)`. From `congr_lemma.cpp`: > Create a congruence lemma that is useful for the simplifier. > In this kind of lemma, if the i-th argument is a Cast argument, then the lemma > contains an input a_i representing the i-th argument in the left-hand-side, and > it appears with a cast (e.g., eq.drec ... a_i ...) in the right-hand-side. > The idea is that the right-hand-side of this lemma "tells" the simplifier > how the resulting term looks like. -/ meta constant mk_congr_lemma_simp (f : expr) (nargs : option nat := none) (md := semireducible) : tactic congr_lemma /-- Create a specialized theorem using (a prefix of) the arguments of the given application. An example of usage can be found in `tests/lean/simp_subsingleton.lean`. For more information on specialization see the comment in the method body for `get_specialization_prefix_size` in `src/library/fun_info.cpp`. -/ meta constant mk_specialized_congr_lemma_simp (h : expr) (md : transparency := semireducible) : tactic congr_lemma /-- Similar to `mk_congr_lemma_simp`, this will make a `congr_lemma` object. The difference is that for each `congr_arg_kind.cast` argument, two proof arguments are generated. Consider some function `f : Π (x : ℕ) (p : x < 4), ℕ`. - `mk_congr_simp` will produce a congruence lemma with type `∀ (x x_1 : ℕ) (e_1 : x = x_1) (p : x < 4), f x p = f x_1 _`. - `mk_congr` will produce a congruence lemma with type `∀ (x x_1 : ℕ) (e_1 : x = x_1) (p : x < 4) (p_1 : x_1 < 4), f x p = f x_1 p_1`. From `congr_lemma.cpp`: > Create a congruence lemma for the congruence closure module. > In this kind of lemma, if the i-th argument is a Cast argument, then the lemma > contains two inputs a_i and b_i representing the i-th argument in the left-hand-side and > right-hand-side. > This lemma is based on the congruence lemma for the simplifier. > It uses subsinglenton elimination to show that the congr-simp lemma right-hand-side > is equal to the right-hand-side of this lemma. -/ meta constant mk_congr_lemma (h : expr) (nargs : option nat := none) (md := semireducible) : tactic congr_lemma /-- Create a specialized theorem using (a prefix of) the arguments of the given application. For more information on specialization see the comment in the method body for `get_specialization_prefix_size` in `src/library/fun_info.cpp`. -/ meta constant mk_specialized_congr_lemma (h : expr) (md := semireducible) : tactic congr_lemma /-- Make a congruence lemma using hetrogeneous equality `heq` instead of `eq`. For example `mk_hcongr_lemma (f : Π (α : ℕ → Type) (n:ℕ) (b:α n), ℕ` )` will make ``` lean { type := ∀ α α', α = α' → ∀ n n', n = n' → ∀ (b : α n) (b' : α' n'), b == b' → f α n b == f α' n' b' , proof := ... , arg_kinds := [eq,eq,heq] } ``` (Using merely `mk_congr_lemma` instead will produce `[fixed,fixed,eq]` instaed.) -/ meta constant mk_hcongr_lemma (h : expr) (nargs : option nat := none) (md := semireducible) : tactic congr_lemma end tactic
5d78c4291152ebfc032fb043c90b5ca68cdb07a0
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Server/FileWorker/WidgetRequests.lean
059ca84ae0f5c24d874e1e63661197beefd1f76a
[ "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,230
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Lean.Widget.InteractiveCode import Lean.Widget.InteractiveGoal import Lean.Widget.InteractiveDiagnostic import Lean.Server.Rpc.RequestHandling import Lean.Server.FileWorker.RequestHandling /-! Registers all widget-related RPC procedures. -/ namespace Lean.Widget open Server structure MsgToInteractive where msg : WithRpcRef MessageData indent : Nat deriving Inhabited, RpcEncoding builtin_initialize registerRpcCallHandler `Lean.Widget.InteractiveDiagnostics.msgToInteractive MsgToInteractive (TaggedText MsgEmbed) fun ⟨⟨m⟩, i⟩ => RequestM.asTask do msgToInteractive m i structure InfoPopup where type : Option CodeWithInfos exprExplicit : Option CodeWithInfos doc : Option String deriving Inhabited, RpcEncoding builtin_initialize registerRpcCallHandler `Lean.Widget.InteractiveDiagnostics.infoToInteractive (WithRpcRef InfoWithCtx) InfoPopup fun ⟨i⟩ => RequestM.asTask do i.ctx.runMetaM i.info.lctx do let type? ← match (← i.info.type?) with | some type => some <$> exprToInteractive type | none => none let exprExplicit? ← match i.info with | Elab.Info.ofTermInfo ti => some <$> exprToInteractiveExplicit ti.expr | Elab.Info.ofFieldInfo fi => some (TaggedText.text fi.fieldName.toString) | _ => none return { type := type? exprExplicit := exprExplicit? doc := ← i.info.docString? : InfoPopup } builtin_initialize registerRpcCallHandler `Lean.Widget.getInteractiveGoals Lsp.PlainGoalParams (Option InteractiveGoals) FileWorker.getInteractiveGoals registerRpcCallHandler `Lean.Widget.getInteractiveTermGoal Lsp.PlainTermGoalParams (Option InteractiveTermGoal) FileWorker.getInteractiveTermGoal structure GetInteractiveDiagnosticsParams where /-- Return diagnostics for these lines only if present, otherwise return all diagnostics. -/ lineRange? : Option Lsp.LineRange deriving Inhabited, FromJson, ToJson open RequestM in def getInteractiveDiagnostics (params : GetInteractiveDiagnosticsParams) : RequestM (RequestTask (Array InteractiveDiagnostic)) := do let doc ← readDoc let rangeEnd ← params.lineRange?.map fun range => doc.meta.text.lspPosToUtf8Pos ⟨range.«end», 0⟩ let t ← doc.cmdSnaps.waitAll fun snap => rangeEnd.all (snap.beginPos < ·) t.map fun (snaps, _) => let diags? := snaps.getLast?.map fun snap => snap.interactiveDiags.toArray.filter fun diag => params.lineRange?.all fun ⟨s, e⟩ => -- does [s,e) intersect [diag.fullRange.start.line,diag.fullRange.end.line)? s ≤ diag.fullRange.start.line ∧ diag.fullRange.start.line < e ∨ diag.fullRange.start.line ≤ s ∧ s < diag.fullRange.end.line diags?.getD #[] builtin_initialize registerRpcCallHandler `Lean.Widget.getInteractiveDiagnostics GetInteractiveDiagnosticsParams (Array InteractiveDiagnostic) getInteractiveDiagnostics end Lean.Widget
0d43c1d59d52ae4e495836fdaddd63f2f1b55fac
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/ring_quot_auto.lean
cd345788c1ddd84aece5590942051b5addffad30
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
12,758
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.algebra.basic import Mathlib.ring_theory.ideal.basic import Mathlib.PostPort universes u₁ u₂ u₃ u₄ namespace Mathlib /-! # Quotients of non-commutative rings Unfortunately, ideals have only been developed in the commutative case as `ideal`, and it's not immediately clear how one should formalise ideals in the non-commutative case. In this file, we directly define the quotient of a semiring by any relation, by building a bigger relation that represents the ideal generated by that relation. We prove the universal properties of the quotient, and recommend avoiding relying on the actual definition! Since everything runs in parallel for quotients of `R`-algebras, we do that case at the same time. -/ namespace ring_quot /-- Given an arbitrary relation `r` on a ring, we strengthen it to a relation `rel r`, such that the equivalence relation generated by `rel r` has `x ~ y` if and only if `x - y` is in the ideal generated by elements `a - b` such that `r a b`. -/ inductive rel {R : Type u₁} [semiring R] (r : R → R → Prop) : R → R → Prop where | of : ∀ {x y : R}, r x y → rel r x y | add_left : ∀ {a b c : R}, rel r a b → rel r (a + c) (b + c) | mul_left : ∀ {a b c : R}, rel r a b → rel r (a * c) (b * c) | mul_right : ∀ {a b c : R}, rel r b c → rel r (a * b) (a * c) theorem rel.add_right {R : Type u₁} [semiring R] {r : R → R → Prop} {a : R} {b : R} {c : R} (h : rel r b c) : rel r (a + b) (a + c) := eq.mpr (id (Eq._oldrec (Eq.refl (rel r (a + b) (a + c))) (add_comm a b))) (eq.mpr (id (Eq._oldrec (Eq.refl (rel r (b + a) (a + c))) (add_comm a c))) (rel.add_left h)) theorem rel.neg {R : Type u₁} [ring R] {r : R → R → Prop} {a : R} {b : R} (h : rel r a b) : rel r (-a) (-b) := sorry theorem rel.smul {S : Type u₂} [comm_semiring S] {A : Type u₃} [semiring A] [algebra S A] {r : A → A → Prop} (k : S) {a : A} {b : A} (h : rel r a b) : rel r (k • a) (k • b) := sorry end ring_quot /-- The quotient of a ring by an arbitrary relation. -/ def ring_quot {R : Type u₁} [semiring R] (r : R → R → Prop) := Quot sorry namespace ring_quot protected instance semiring {R : Type u₁} [semiring R] (r : R → R → Prop) : semiring (ring_quot r) := semiring.mk (quot.map₂ Add.add rel.add_right rel.add_left) sorry (Quot.mk (rel r) 0) sorry sorry sorry (quot.map₂ Mul.mul rel.mul_right rel.mul_left) sorry (Quot.mk (rel r) 1) sorry sorry sorry sorry sorry sorry protected instance ring {R : Type u₁} [ring R] (r : R → R → Prop) : ring (ring_quot r) := ring.mk semiring.add sorry semiring.zero sorry sorry (quot.map (fun (a : R) => -a) rel.neg) (add_comm_group.sub._default semiring.add sorry semiring.zero sorry sorry (quot.map (fun (a : R) => -a) rel.neg)) sorry sorry semiring.mul sorry semiring.one sorry sorry sorry sorry protected instance comm_semiring {R : Type u₁} [comm_semiring R] (r : R → R → Prop) : comm_semiring (ring_quot r) := comm_semiring.mk semiring.add sorry semiring.zero sorry sorry sorry semiring.mul sorry semiring.one sorry sorry sorry sorry sorry sorry sorry protected instance comm_ring {R : Type u₁} [comm_ring R] (r : R → R → Prop) : comm_ring (ring_quot r) := comm_ring.mk comm_semiring.add sorry comm_semiring.zero sorry sorry ring.neg ring.sub sorry sorry comm_semiring.mul sorry comm_semiring.one sorry sorry sorry sorry sorry protected instance algebra {S : Type u₂} [comm_semiring S] {A : Type u₃} [semiring A] [algebra S A] (s : A → A → Prop) : algebra S (ring_quot s) := algebra.mk (ring_hom.mk (fun (r : S) => Quot.mk (rel s) (coe_fn (algebra_map S A) r)) sorry sorry sorry sorry) sorry sorry protected instance inhabited {R : Type u₁} [semiring R] (r : R → R → Prop) : Inhabited (ring_quot r) := { default := 0 } /-- The quotient map from a ring to its quotient, as a homomorphism of rings. -/ def mk_ring_hom {R : Type u₁} [semiring R] (r : R → R → Prop) : R →+* ring_quot r := ring_hom.mk (Quot.mk (rel r)) sorry sorry sorry sorry theorem mk_ring_hom_rel {R : Type u₁} [semiring R] {r : R → R → Prop} {x : R} {y : R} (w : r x y) : coe_fn (mk_ring_hom r) x = coe_fn (mk_ring_hom r) y := quot.sound (rel.of w) theorem mk_ring_hom_surjective {R : Type u₁} [semiring R] (r : R → R → Prop) : function.surjective ⇑(mk_ring_hom r) := sorry theorem ring_quot_ext {R : Type u₁} [semiring R] {T : Type u₄} [semiring T] {r : R → R → Prop} (f : ring_quot r →+* T) (g : ring_quot r →+* T) (w : ring_hom.comp f (mk_ring_hom r) = ring_hom.comp g (mk_ring_hom r)) : f = g := sorry /-- Any ring homomorphism `f : R →+* T` which respects a relation `r : R → R → Prop` factors uniquely through a morphism `ring_quot r →+* T`. -/ def lift {R : Type u₁} [semiring R] {T : Type u₄} [semiring T] {r : R → R → Prop} : (Subtype fun (f : R →+* T) => ∀ {x y : R}, r x y → coe_fn f x = coe_fn f y) ≃ (ring_quot r →+* T) := equiv.mk (fun (f' : Subtype fun (f : R →+* T) => ∀ {x y : R}, r x y → coe_fn f x = coe_fn f y) => let f : R →+* T := ↑f'; ring_hom.mk (Quot.lift ⇑f sorry) (ring_hom.map_one f) sorry (ring_hom.map_zero f) sorry) (fun (F : ring_quot r →+* T) => { val := ring_hom.comp F (mk_ring_hom r), property := sorry }) sorry sorry @[simp] theorem lift_mk_ring_hom_apply {R : Type u₁} [semiring R] {T : Type u₄} [semiring T] (f : R →+* T) {r : R → R → Prop} (w : ∀ {x y : R}, r x y → coe_fn f x = coe_fn f y) (x : R) : coe_fn (coe_fn lift { val := f, property := w }) (coe_fn (mk_ring_hom r) x) = coe_fn f x := rfl -- note this is essentially `lift.symm_apply_eq.mp h` theorem lift_unique {R : Type u₁} [semiring R] {T : Type u₄} [semiring T] (f : R →+* T) {r : R → R → Prop} (w : ∀ {x y : R}, r x y → coe_fn f x = coe_fn f y) (g : ring_quot r →+* T) (h : ring_hom.comp g (mk_ring_hom r) = f) : g = coe_fn lift { val := f, property := w } := sorry theorem eq_lift_comp_mk_ring_hom {R : Type u₁} [semiring R] {T : Type u₄} [semiring T] {r : R → R → Prop} (f : ring_quot r →+* T) : f = coe_fn lift { val := ring_hom.comp f (mk_ring_hom r), property := fun (x y : R) (h : r x y) => id (eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn f (coe_fn (mk_ring_hom r) x) = coe_fn f (coe_fn (mk_ring_hom r) y))) (mk_ring_hom_rel h))) (Eq.refl (coe_fn f (coe_fn (mk_ring_hom r) y)))) } := Eq.symm (equiv.apply_symm_apply lift f) /-! We now verify that in the case of a commutative ring, the `ring_quot` construction agrees with the quotient by the appropriate ideal. -/ /-- The universal ring homomorphism from `ring_quot r` to `(ideal.of_rel r).quotient`. -/ def ring_quot_to_ideal_quotient {B : Type u₁} [comm_ring B] (r : B → B → Prop) : ring_quot r →+* ideal.quotient (ideal.of_rel r) := coe_fn lift { val := ideal.quotient.mk (ideal.of_rel r), property := sorry } @[simp] theorem ring_quot_to_ideal_quotient_apply {B : Type u₁} [comm_ring B] (r : B → B → Prop) (x : B) : coe_fn (ring_quot_to_ideal_quotient r) (coe_fn (mk_ring_hom r) x) = coe_fn (ideal.quotient.mk (ideal.of_rel r)) x := rfl /-- The universal ring homomorphism from `(ideal.of_rel r).quotient` to `ring_quot r`. -/ def ideal_quotient_to_ring_quot {B : Type u₁} [comm_ring B] (r : B → B → Prop) : ideal.quotient (ideal.of_rel r) →+* ring_quot r := ideal.quotient.lift (ideal.of_rel r) (mk_ring_hom r) sorry @[simp] theorem ideal_quotient_to_ring_quot_apply {B : Type u₁} [comm_ring B] (r : B → B → Prop) (x : B) : coe_fn (ideal_quotient_to_ring_quot r) (coe_fn (ideal.quotient.mk (ideal.of_rel r)) x) = coe_fn (mk_ring_hom r) x := rfl /-- The ring equivalence between `ring_quot r` and `(ideal.of_rel r).quotient` -/ def ring_quot_equiv_ideal_quotient {B : Type u₁} [comm_ring B] (r : B → B → Prop) : ring_quot r ≃+* ideal.quotient (ideal.of_rel r) := ring_equiv.of_hom_inv (ring_quot_to_ideal_quotient r) (ideal_quotient_to_ring_quot r) sorry sorry /-- Transfer a star_ring instance through a quotient, if the quotient is invariant to `star` -/ def star_ring {R : Type u₁} [semiring R] [star_ring R] (r : R → R → Prop) (hr : ∀ {a b : R}, r a b → r (star a) (star b)) : star_ring (ring_quot r) := star_ring.mk sorry /-- The quotient map from an `S`-algebra to its quotient, as a homomorphism of `S`-algebras. -/ def mk_alg_hom (S : Type u₂) [comm_semiring S] {A : Type u₃} [semiring A] [algebra S A] (s : A → A → Prop) : alg_hom S A (ring_quot s) := alg_hom.mk (ring_hom.to_fun (mk_ring_hom s)) sorry sorry sorry sorry sorry @[simp] theorem mk_alg_hom_coe (S : Type u₂) [comm_semiring S] {A : Type u₃} [semiring A] [algebra S A] (s : A → A → Prop) : ↑(mk_alg_hom S s) = mk_ring_hom s := rfl theorem mk_alg_hom_rel (S : Type u₂) [comm_semiring S] {A : Type u₃} [semiring A] [algebra S A] {s : A → A → Prop} {x : A} {y : A} (w : s x y) : coe_fn (mk_alg_hom S s) x = coe_fn (mk_alg_hom S s) y := quot.sound (rel.of w) theorem mk_alg_hom_surjective (S : Type u₂) [comm_semiring S] {A : Type u₃} [semiring A] [algebra S A] (s : A → A → Prop) : function.surjective ⇑(mk_alg_hom S s) := sorry theorem ring_quot_ext' (S : Type u₂) [comm_semiring S] {A : Type u₃} [semiring A] [algebra S A] {B : Type u₄} [semiring B] [algebra S B] {s : A → A → Prop} (f : alg_hom S (ring_quot s) B) (g : alg_hom S (ring_quot s) B) (w : alg_hom.comp f (mk_alg_hom S s) = alg_hom.comp g (mk_alg_hom S s)) : f = g := sorry /-- Any `S`-algebra homomorphism `f : A →ₐ[S] B` which respects a relation `s : A → A → Prop` factors uniquely through a morphism `ring_quot s →ₐ[S] B`. -/ def lift_alg_hom (S : Type u₂) [comm_semiring S] {A : Type u₃} [semiring A] [algebra S A] {B : Type u₄} [semiring B] [algebra S B] {s : A → A → Prop} : (Subtype fun (f : alg_hom S A B) => ∀ {x y : A}, s x y → coe_fn f x = coe_fn f y) ≃ alg_hom S (ring_quot s) B := equiv.mk (fun (f' : Subtype fun (f : alg_hom S A B) => ∀ {x y : A}, s x y → coe_fn f x = coe_fn f y) => let f : alg_hom S A B := ↑f'; alg_hom.mk (Quot.lift ⇑f sorry) (alg_hom.map_one f) sorry (alg_hom.map_zero f) sorry sorry) (fun (F : alg_hom S (ring_quot s) B) => { val := alg_hom.comp F (mk_alg_hom S s), property := sorry }) sorry sorry @[simp] theorem lift_alg_hom_mk_alg_hom_apply (S : Type u₂) [comm_semiring S] {A : Type u₃} [semiring A] [algebra S A] {B : Type u₄} [semiring B] [algebra S B] (f : alg_hom S A B) {s : A → A → Prop} (w : ∀ {x y : A}, s x y → coe_fn f x = coe_fn f y) (x : A) : coe_fn (coe_fn (lift_alg_hom S) { val := f, property := w }) (coe_fn (mk_alg_hom S s) x) = coe_fn f x := rfl -- note this is essentially `(lift_alg_hom S).symm_apply_eq.mp h` theorem lift_alg_hom_unique (S : Type u₂) [comm_semiring S] {A : Type u₃} [semiring A] [algebra S A] {B : Type u₄} [semiring B] [algebra S B] (f : alg_hom S A B) {s : A → A → Prop} (w : ∀ {x y : A}, s x y → coe_fn f x = coe_fn f y) (g : alg_hom S (ring_quot s) B) (h : alg_hom.comp g (mk_alg_hom S s) = f) : g = coe_fn (lift_alg_hom S) { val := f, property := w } := sorry theorem eq_lift_alg_hom_comp_mk_alg_hom (S : Type u₂) [comm_semiring S] {A : Type u₃} [semiring A] [algebra S A] {B : Type u₄} [semiring B] [algebra S B] {s : A → A → Prop} (f : alg_hom S (ring_quot s) B) : f = coe_fn (lift_alg_hom S) { val := alg_hom.comp f (mk_alg_hom S s), property := fun (x y : A) (h : s x y) => id (eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn f (coe_fn (mk_alg_hom S s) x) = coe_fn f (coe_fn (mk_alg_hom S s) y))) (mk_alg_hom_rel S h))) (Eq.refl (coe_fn f (coe_fn (mk_alg_hom S s) y)))) } := Eq.symm (equiv.apply_symm_apply (lift_alg_hom S) f) end Mathlib
92f179cad2d33d059a7093343ab5513262a055e1
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/partial_explicit1.lean
b1ec3de3cecc077af5b1c3f844c82b0fc1f4f376
[ "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
128
lean
def eq_rect (A : Type) (x : A) (P : A → Type) (f : P x) (y : A) (e : x = y) : P y := @eq.rec_on _ _ (λ (y : A), P y) _ e f
512ec88b72d438c14a5c1fd52c3352ba10f4fa63
ec62863c729b7eedee77b86d974f2c529fa79d25
/11/b.lean
dbea09cc03d47cf394f020580c78675fd7aff188
[]
no_license
rwbarton/advent-of-lean-4
2ac9b17ba708f66051e3d8cd694b0249bc433b65
417c7e2718253ba7148c0279fcb251b6fc291477
refs/heads/main
1,675,917,092,057
1,609,864,581,000
1,609,864,581,000
317,700,289
24
0
null
null
null
null
UTF-8
Lean
false
false
1,958
lean
def Array.mkFun {α : Type} (n : Nat) (f : Nat → α) : Array α := Array.mk ((List.range n).map f) inductive Seat | floor | empty | occupied open Seat def Seat.beq : Seat → Seat → Bool | floor, floor => true | empty, empty => true | occupied, occupied => true | _, _ => false instance : BEq Seat := ⟨Seat.beq⟩ instance : Inhabited Seat := ⟨floor⟩ def State := Array (Array Seat) instance : BEq State := show BEq (Array (Array Seat)) from inferInstance partial def State.get_first_in (s : State) (row col : Int) (dr dc : Int) : Seat := let r' := row + dr let c' := col + dc if r' < 0 || r' ≥ s.size then floor else if c' < 0 || c' ≥ (s.get! r'.toNat).size then floor else match (s.get! r'.toNat).get! c'.toNat with | floor => s.get_first_in r' c' dr dc | empty => empty | occupied => occupied instance : Monad List := { pure := λ a => [a], map := List.map, bind := λ x f => (x.map f).join } def diffs : List (Int × Int) := do let dr ← [(-1 : Int), 0, 1] let dc ← [(-1 : Int), 0, 1] if dr == 0 && dc == 0 then [] pure (dr, dc) def step (s : State) : State := Array.mkFun s.size $ λ row => Array.mkFun (s.get! 0).size $ λ col => let nbrs := (diffs.filter (λ ⟨dr, dc⟩ => s.get_first_in row col dr dc == occupied)).length match (s.get! row).get! col with | floor => floor | empty => if nbrs == 0 then occupied else empty | occupied => if nbrs ≥ 5 then empty else occupied def count (s : State) : Int := s.foldl (λ c s' => c + s'.foldl (λ c' seat => c' + if seat == occupied then 1 else 0) 0) 0 partial def solve (s : State) : Int := -- dbgTrace s!"{count s}" $ λ _ => let s' := step s if s == s' then count s else solve s' def parse (str : Array String) : State := str.map $ λ s => Array.mk $ s.toList.map $ λ c => match c with | '.' => floor | 'L' => empty | '#' => occupied | _ => panic! "invalid input" def main : IO Unit := do let input ← IO.FS.lines "a.in" IO.print s!"{solve (parse input)}\n"
b4223203ae69e757e9dc67d2448811448466e7c2
0942a74cc0b397d09bcd1059762fa076b1a5d308
/src/trivial2.lean
ae3e68bdc84c243b105580fc89f8246dc36fa303
[ "Unlicense" ]
permissive
sguzman/lean-examples
81df2010e204be1a9fde6ae18295e299e69cebca
c7428b2982d0468d0adb4453766a27e1550a72e8
refs/heads/master
1,668,438,655,034
1,594,156,605,000
1,594,156,605,000
277,626,098
0
0
null
null
null
null
UTF-8
Lean
false
false
55
lean
example : (2:int)-(3:int)=(-1:int) := begin trivial end
638bb4500340be9b2cdec2ab33473fa0e5316e75
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/calculus/taylor.lean
31527b5521359368fa841899346c4f611aaf2032
[ "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
18,626
lean
/- Copyright (c) 2022 Moritz Doll. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Moritz Doll -/ import analysis.calculus.iterated_deriv import analysis.calculus.mean_value import data.polynomial.basic import data.polynomial.module /-! # Taylor's theorem This file defines the Taylor polynomial of a real function `f : ℝ → E`, where `E` is a normed vector space over `ℝ` and proves Taylor's theorem, which states that if `f` is sufficiently smooth, then `f` can be approximated by the Taylor polynomial up to an explicit error term. ## Main definitions * `taylor_coeff_within`: the Taylor coefficient using `deriv_within` * `taylor_within`: the Taylor polynomial using `deriv_within` ## Main statements * `taylor_mean_remainder`: Taylor's theorem with the general form of the remainder term * `taylor_mean_remainder_lagrange`: Taylor's theorem with the Lagrange remainder * `taylor_mean_remainder_cauchy`: Taylor's theorem with the Cauchy remainder * `exists_taylor_mean_remainder_bound`: Taylor's theorem for vector valued functions with a polynomial bound on the remainder ## TODO * the Peano form of the remainder * the integral form of the remainder * Generalization to higher dimensions ## Tags Taylor polynomial, Taylor's theorem -/ open_locale big_operators interval topological_space nat open set variables {𝕜 E F : Type*} variables [normed_add_comm_group E] [normed_space ℝ E] /-- The `k`th coefficient of the Taylor polynomial. -/ noncomputable def taylor_coeff_within (f : ℝ → E) (k : ℕ) (s : set ℝ) (x₀ : ℝ) : E := (k! : ℝ)⁻¹ • (iterated_deriv_within k f s x₀) /-- The Taylor polynomial with derivatives inside of a set `s`. The Taylor polynomial is given by $$∑_{k=0}^n \frac{(x - x₀)^k}{k!} f^{(k)}(x₀),$$ where $f^{(k)}(x₀)$ denotes the iterated derivative in the set `s`. -/ noncomputable def taylor_within (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ : ℝ) : polynomial_module ℝ E := (finset.range (n+1)).sum (λ k, polynomial_module.comp (polynomial.X - polynomial.C x₀) (polynomial_module.single ℝ k (taylor_coeff_within f k s x₀))) /-- The Taylor polynomial with derivatives inside of a set `s` considered as a function `ℝ → E`-/ noncomputable def taylor_within_eval (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ x : ℝ) : E := polynomial_module.eval x (taylor_within f n s x₀) lemma taylor_within_succ (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ : ℝ) : taylor_within f (n+1) s x₀ = taylor_within f n s x₀ + polynomial_module.comp (polynomial.X - polynomial.C x₀) (polynomial_module.single ℝ (n+1) (taylor_coeff_within f (n+1) s x₀)) := begin dunfold taylor_within, rw finset.sum_range_succ, end @[simp] lemma taylor_within_eval_succ (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ x : ℝ) : taylor_within_eval f (n+1) s x₀ x = taylor_within_eval f n s x₀ x + (((n + 1 : ℝ) * n!)⁻¹ * (x - x₀)^(n+1)) • iterated_deriv_within (n + 1) f s x₀ := begin simp_rw [taylor_within_eval, taylor_within_succ, linear_map.map_add, polynomial_module.comp_eval], congr, simp only [polynomial.eval_sub, polynomial.eval_X, polynomial.eval_C, polynomial_module.eval_single, mul_inv_rev], dunfold taylor_coeff_within, rw [←mul_smul, mul_comm, nat.factorial_succ, nat.cast_mul, nat.cast_add, nat.cast_one, mul_inv_rev], end /-- The Taylor polynomial of order zero evaluates to `f x`. -/ @[simp] lemma taylor_within_zero_eval (f : ℝ → E) (s : set ℝ) (x₀ x : ℝ) : taylor_within_eval f 0 s x₀ x = f x₀ := begin dunfold taylor_within_eval, dunfold taylor_within, dunfold taylor_coeff_within, simp, end /-- Evaluating the Taylor polynomial at `x = x₀` yields `f x`. -/ @[simp] lemma taylor_within_eval_self (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ : ℝ) : taylor_within_eval f n s x₀ x₀ = f x₀ := begin induction n with k hk, { exact taylor_within_zero_eval _ _ _ _}, simp [hk] end lemma taylor_within_apply (f : ℝ → E) (n : ℕ) (s : set ℝ) (x₀ x : ℝ) : taylor_within_eval f n s x₀ x = ∑ k in finset.range (n+1), ((k! : ℝ)⁻¹ * (x - x₀)^k) • iterated_deriv_within k f s x₀ := begin induction n with k hk, { simp }, rw [taylor_within_eval_succ, finset.sum_range_succ, hk], simp, end /-- If `f` is `n` times continuous differentiable on a set `s`, then the Taylor polynomial `taylor_within_eval f n s x₀ x` is continuous in `x₀`. -/ lemma continuous_on_taylor_within_eval {f : ℝ → E} {x : ℝ} {n : ℕ} {s : set ℝ} (hs : unique_diff_on ℝ s) (hf : cont_diff_on ℝ n f s) : continuous_on (λ t, taylor_within_eval f n s t x) s := begin simp_rw taylor_within_apply, refine continuous_on_finset_sum (finset.range (n+1)) (λ i hi, _), refine (continuous_on_const.mul ((continuous_on_const.sub continuous_on_id).pow _)).smul _, rw cont_diff_on_iff_continuous_on_differentiable_on_deriv hs at hf, cases hf, specialize hf_left i, simp only [finset.mem_range] at hi, refine (hf_left _), simp only [with_top.coe_le_coe], exact nat.lt_succ_iff.mp hi, end /-- Helper lemma for calculating the derivative of the monomial that appears in Taylor expansions.-/ lemma monomial_has_deriv_aux (t x : ℝ) (n : ℕ) : has_deriv_at (λ y, (x - y)^(n+1)) (-(n+1) * (x - t)^n) t := begin simp_rw sub_eq_neg_add, rw [←neg_one_mul, mul_comm (-1 : ℝ), mul_assoc, mul_comm (-1 : ℝ), ←mul_assoc], convert @has_deriv_at.pow _ _ _ _ _ (n+1) ((has_deriv_at_id t).neg.add_const x), simp only [nat.cast_add, nat.cast_one], end lemma has_deriv_within_at_taylor_coeff_within {f : ℝ → E} {x y : ℝ} {k : ℕ} {s s' : set ℝ} (hs'_unique : unique_diff_within_at ℝ s' y) (hs' : s' ∈ 𝓝[s] y) (hy : y ∈ s') (h : s' ⊆ s) (hf' : differentiable_on ℝ (iterated_deriv_within (k+1) f s) s') : has_deriv_within_at (λ t, (((k+1 : ℝ) * k!)⁻¹ * (x - t)^(k+1)) • iterated_deriv_within (k+1) f s t) ((((k+1 : ℝ) * k!)⁻¹ * (x - y)^(k+1)) • iterated_deriv_within (k+2) f s y - ((k! : ℝ)⁻¹ * (x - y)^k) • iterated_deriv_within (k+1) f s y) s' y := begin have hf'' : has_deriv_within_at (λ t, iterated_deriv_within (k+1) f s t) (iterated_deriv_within (k+2) f s y) s' y := begin convert (hf' y hy).has_deriv_within_at, rw iterated_deriv_within_succ (hs'_unique.mono h), refine (deriv_within_subset h hs'_unique _).symm, exact (hf' y hy).antimono h hs', end, have : has_deriv_within_at (λ t, (((k+1 : ℝ) * k!)⁻¹ * (x - t)^(k+1))) (-((k! : ℝ)⁻¹ * (x - y)^k)) s' y := begin -- Commuting the factors: have : (-((k! : ℝ)⁻¹ * (x - y)^k)) = (((k+1 : ℝ) * k!)⁻¹ * (-(k+1) *(x - y)^k)) := by { field_simp [nat.cast_add_one_ne_zero k, nat.factorial_ne_zero k], ring_nf }, rw this, exact (monomial_has_deriv_aux y x _).has_deriv_within_at.const_mul _, end, convert this.smul hf'', field_simp [nat.cast_add_one_ne_zero k, nat.factorial_ne_zero k], rw [neg_div, neg_smul, sub_eq_add_neg], end /-- Calculate the derivative of the Taylor polynomial with respect to `x₀`. Version for arbitrary sets -/ lemma has_deriv_within_at_taylor_within_eval {f : ℝ → E} {x y : ℝ} {n : ℕ} {s s' : set ℝ} (hs'_unique : unique_diff_within_at ℝ s' y) (hs_unique : unique_diff_on ℝ s) (hs' : s' ∈ 𝓝[s] y) (hy : y ∈ s') (h : s' ⊆ s) (hf : cont_diff_on ℝ n f s) (hf' : differentiable_on ℝ (iterated_deriv_within n f s) s') : has_deriv_within_at (λ t, taylor_within_eval f n s t x) (((n! : ℝ)⁻¹ * (x - y)^n) • (iterated_deriv_within (n+1) f s y)) s' y := begin induction n with k hk, { simp only [taylor_within_zero_eval, nat.factorial_zero, nat.cast_one, inv_one, pow_zero, mul_one, zero_add, one_smul], simp only [iterated_deriv_within_zero] at hf', rw iterated_deriv_within_one hs_unique (h hy), refine has_deriv_within_at.mono _ h, refine differentiable_within_at.has_deriv_within_at _, exact (hf' y hy).antimono h hs' }, simp_rw [nat.add_succ, taylor_within_eval_succ], simp only [add_zero, nat.factorial_succ, nat.cast_mul, nat.cast_add, nat.cast_one], have hdiff : differentiable_on ℝ (iterated_deriv_within k f s) s' := begin have coe_lt_succ : (k : with_top ℕ) < k.succ := by { rw [with_top.coe_lt_coe], exact lt_add_one k }, refine differentiable_on.mono _ h, exact hf.differentiable_on_iterated_deriv_within coe_lt_succ hs_unique, end, specialize hk (cont_diff_on.of_succ hf) hdiff, convert hk.add (has_deriv_within_at_taylor_coeff_within hs'_unique hs' hy h hf'), exact (add_sub_cancel'_right _ _).symm, end /-- Calculate the derivative of the Taylor polynomial with respect to `x₀`. Version for open intervals -/ lemma taylor_within_eval_has_deriv_at_Ioo {f : ℝ → E} {a b t : ℝ} (x : ℝ) {n : ℕ} (hx : a < b) (ht : t ∈ Ioo a b) (hf : cont_diff_on ℝ n f (Icc a b)) (hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc a b)) (Ioo a b)) : has_deriv_at (λ y, taylor_within_eval f n (Icc a b) y x) (((n! : ℝ)⁻¹ * (x - t)^n) • (iterated_deriv_within (n+1) f (Icc a b) t)) t := begin have h_nhds := is_open.mem_nhds is_open_Ioo ht, exact (has_deriv_within_at_taylor_within_eval (unique_diff_within_at_Ioo ht) (unique_diff_on_Icc hx) (nhds_within_le_nhds h_nhds) ht Ioo_subset_Icc_self hf hf') .has_deriv_at h_nhds, end /-- Calculate the derivative of the Taylor polynomial with respect to `x₀`. Version for closed intervals -/ lemma has_deriv_within_taylor_within_eval_at_Icc {f : ℝ → E} {a b t : ℝ} (x : ℝ) {n : ℕ} (hx : a < b) (ht : t ∈ Icc a b) (hf : cont_diff_on ℝ n f (Icc a b)) (hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc a b)) (Icc a b)) : has_deriv_within_at (λ y, taylor_within_eval f n (Icc a b) y x) (((n! : ℝ)⁻¹ * (x - t)^n) • (iterated_deriv_within (n+1) f (Icc a b) t)) (Icc a b) t := has_deriv_within_at_taylor_within_eval (unique_diff_on_Icc hx t ht) (unique_diff_on_Icc hx) self_mem_nhds_within ht rfl.subset hf hf' /-! ### Taylor's theorem with mean value type remainder estimate -/ /-- **Taylor's theorem** with the general mean value form of the remainder. We assume that `f` is `n+1`-times continuously differentiable in the closed set `Icc x₀ x` and `n+1`-times differentiable on the open set `Ioo x₀ x`, and `g` is a differentiable function on `Ioo x₀ x` and continuous on `Icc x₀ x`. Then there exists a `x' ∈ Ioo x₀ x` such that $$f(x) - (P_n f)(x₀, x) = \frac{(x - x')^n}{n!} \frac{g(x) - g(x₀)}{g' x'},$$ where $P_n f$ denotes the Taylor polynomial of degree $n$. -/ lemma taylor_mean_remainder {f : ℝ → ℝ} {g g' : ℝ → ℝ} {x x₀ : ℝ} {n : ℕ} (hx : x₀ < x) (hf : cont_diff_on ℝ n f (Icc x₀ x)) (hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc x₀ x)) (Ioo x₀ x)) (gcont : continuous_on g (Icc x₀ x)) (gdiff : ∀ (x_1 : ℝ), x_1 ∈ Ioo x₀ x → has_deriv_at g (g' x_1) x_1) (g'_ne : ∀ (x_1 : ℝ), x_1 ∈ Ioo x₀ x → g' x_1 ≠ 0) : ∃ (x' : ℝ) (hx' : x' ∈ Ioo x₀ x), f x - taylor_within_eval f n (Icc x₀ x) x₀ x = ((x - x')^n /n! * (g x - g x₀) / g' x') • (iterated_deriv_within (n+1) f (Icc x₀ x) x') := begin -- We apply the mean value theorem rcases exists_ratio_has_deriv_at_eq_ratio_slope (λ t, taylor_within_eval f n (Icc x₀ x) t x) (λ t, ((n! : ℝ)⁻¹ * (x - t)^n) • (iterated_deriv_within (n+1) f (Icc x₀ x) t)) hx (continuous_on_taylor_within_eval (unique_diff_on_Icc hx) hf) (λ _ hy, taylor_within_eval_has_deriv_at_Ioo x hx hy hf hf') g g' gcont gdiff with ⟨y, hy, h⟩, use [y, hy], -- The rest is simplifications and trivial calculations simp only [taylor_within_eval_self] at h, rw [mul_comm, ←div_left_inj' (g'_ne y hy), mul_div_cancel _ (g'_ne y hy)] at h, rw ←h, field_simp [g'_ne y hy, n.factorial_ne_zero], ring, end /-- **Taylor's theorem** with the Lagrange form of the remainder. We assume that `f` is `n+1`-times continuously differentiable in the closed set `Icc x₀ x` and `n+1`-times differentiable on the open set `Ioo x₀ x`. Then there exists a `x' ∈ Ioo x₀ x` such that $$f(x) - (P_n f)(x₀, x) = \frac{f^{(n+1)}(x') (x - x₀)^{n+1}}{(n+1)!},$$ where $P_n f$ denotes the Taylor polynomial of degree $n$ and $f^{(n+1)}$ is the $n+1$-th iterated derivative. -/ lemma taylor_mean_remainder_lagrange {f : ℝ → ℝ} {x x₀ : ℝ} {n : ℕ} (hx : x₀ < x) (hf : cont_diff_on ℝ n f (Icc x₀ x)) (hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc x₀ x)) (Ioo x₀ x)) : ∃ (x' : ℝ) (hx' : x' ∈ Ioo x₀ x), f x - taylor_within_eval f n (Icc x₀ x) x₀ x = (iterated_deriv_within (n+1) f (Icc x₀ x) x') * (x - x₀)^(n+1) /(n+1)! := begin have gcont : continuous_on (λ (t : ℝ), (x - t) ^ (n + 1)) (Icc x₀ x) := by { refine continuous.continuous_on _, continuity }, have xy_ne : ∀ (y : ℝ), y ∈ Ioo x₀ x → (x - y)^n ≠ 0 := begin intros y hy, refine pow_ne_zero _ _, rw [mem_Ioo] at hy, rw sub_ne_zero, exact hy.2.ne.symm, end, have hg' : ∀ (y : ℝ), y ∈ Ioo x₀ x → -(↑n + 1) * (x - y) ^ n ≠ 0 := λ y hy, mul_ne_zero (neg_ne_zero.mpr (nat.cast_add_one_ne_zero n)) (xy_ne y hy), -- We apply the general theorem with g(t) = (x - t)^(n+1) rcases taylor_mean_remainder hx hf hf' gcont (λ y _, monomial_has_deriv_aux y x _) hg' with ⟨y, hy, h⟩, use [y, hy], simp only [sub_self, zero_pow', ne.def, nat.succ_ne_zero, not_false_iff, zero_sub, mul_neg] at h, rw [h, neg_div, ←div_neg, neg_mul, neg_neg], field_simp [n.cast_add_one_ne_zero, n.factorial_ne_zero, xy_ne y hy], ring, end /-- **Taylor's theorem** with the Cauchy form of the remainder. We assume that `f` is `n+1`-times continuously differentiable on the closed set `Icc x₀ x` and `n+1`-times differentiable on the open set `Ioo x₀ x`. Then there exists a `x' ∈ Ioo x₀ x` such that $$f(x) - (P_n f)(x₀, x) = \frac{f^{(n+1)}(x') (x - x')^n (x-x₀)}{n!},$$ where $P_n f$ denotes the Taylor polynomial of degree $n$ and $f^{(n+1)}$ is the $n+1$-th iterated derivative. -/ lemma taylor_mean_remainder_cauchy {f : ℝ → ℝ} {x x₀ : ℝ} {n : ℕ} (hx : x₀ < x) (hf : cont_diff_on ℝ n f (Icc x₀ x)) (hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc x₀ x)) (Ioo x₀ x)) : ∃ (x' : ℝ) (hx' : x' ∈ Ioo x₀ x), f x - taylor_within_eval f n (Icc x₀ x) x₀ x = (iterated_deriv_within (n+1) f (Icc x₀ x) x') * (x - x')^n /n! * (x - x₀) := begin have gcont : continuous_on id (Icc x₀ x) := continuous.continuous_on (by continuity), have gdiff : (∀ (x_1 : ℝ), x_1 ∈ Ioo x₀ x → has_deriv_at id ((λ (t : ℝ), (1 : ℝ)) x_1) x_1) := λ _ _, has_deriv_at_id _, -- We apply the general theorem with g = id rcases taylor_mean_remainder hx hf hf' gcont gdiff (λ _ _, by simp) with ⟨y, hy, h⟩, use [y, hy], rw h, field_simp [n.factorial_ne_zero], ring, end /-- **Taylor's theorem** with a polynomial bound on the remainder We assume that `f` is `n+1`-times continuously differentiable on the closed set `Icc a b`. The difference of `f` and its `n`-th Taylor polynomial can be estimated by `C * (x - a)^(n+1) / n!` where `C` is a bound for the `n+1`-th iterated derivative of `f`. -/ lemma taylor_mean_remainder_bound {f : ℝ → E} {a b C x : ℝ} {n : ℕ} (hab : a ≤ b) (hf : cont_diff_on ℝ (n+1) f (Icc a b)) (hx : x ∈ Icc a b) (hC : ∀ y ∈ Icc a b, ‖iterated_deriv_within (n + 1) f (Icc a b) y‖ ≤ C) : ‖f x - taylor_within_eval f n (Icc a b) a x‖ ≤ C * (x - a)^(n+1) / n! := begin rcases eq_or_lt_of_le hab with rfl|h, { rw [Icc_self, mem_singleton_iff] at hx, simp [hx] }, -- The nth iterated derivative is differentiable have hf' : differentiable_on ℝ (iterated_deriv_within n f (Icc a b)) (Icc a b) := hf.differentiable_on_iterated_deriv_within (with_top.coe_lt_coe.mpr n.lt_succ_self) (unique_diff_on_Icc h), -- We can uniformly bound the derivative of the Taylor polynomial have h' : ∀ (y : ℝ) (hy : y ∈ Ico a x), ‖((n! : ℝ)⁻¹ * (x - y) ^ n) • iterated_deriv_within (n + 1) f (Icc a b) y‖ ≤ (n! : ℝ)⁻¹ * |(x - a)|^n * C, { rintro y ⟨hay, hyx⟩, rw [norm_smul, real.norm_eq_abs], -- Estimate the iterated derivative by `C` refine mul_le_mul _ (hC y ⟨hay, hyx.le.trans hx.2⟩) (by positivity) (by positivity), -- The rest is a trivial calculation rw [abs_mul, abs_pow, abs_inv, nat.abs_cast], mono* with [0 ≤ (n! : ℝ)⁻¹], any_goals { positivity }, linarith [hx.1, hyx] }, -- Apply the mean value theorem for vector valued functions: have A : ∀ t ∈ Icc a x, has_deriv_within_at (λ y, taylor_within_eval f n (Icc a b) y x) (((↑n!)⁻¹ * (x - t) ^ n) • iterated_deriv_within (n + 1) f (Icc a b) t) (Icc a x) t, { assume t ht, have I : Icc a x ⊆ Icc a b := Icc_subset_Icc_right hx.2, exact (has_deriv_within_taylor_within_eval_at_Icc x h (I ht) hf.of_succ hf').mono I }, have := norm_image_sub_le_of_norm_deriv_le_segment' A h' x (right_mem_Icc.2 hx.1), simp only [taylor_within_eval_self] at this, refine this.trans_eq _, -- The rest is a trivial calculation rw [abs_of_nonneg (sub_nonneg.mpr hx.1)], ring_exp, end /-- **Taylor's theorem** with a polynomial bound on the remainder We assume that `f` is `n+1`-times continuously differentiable on the closed set `Icc a b`. There exists a constant `C` such that for all `x ∈ Icc a b` the difference of `f` and its `n`-th Taylor polynomial can be estimated by `C * (x - a)^(n+1)`. -/ lemma exists_taylor_mean_remainder_bound {f : ℝ → E} {a b : ℝ} {n : ℕ} (hab : a ≤ b) (hf : cont_diff_on ℝ (n+1) f (Icc a b)) : ∃ C, ∀ x ∈ Icc a b, ‖f x - taylor_within_eval f n (Icc a b) a x‖ ≤ C * (x - a)^(n+1) := begin rcases eq_or_lt_of_le hab with rfl|h, { refine ⟨0, λ x hx, _⟩, have : a = x, by simpa [← le_antisymm_iff] using hx, simp [← this] }, -- We estimate by the supremum of the norm of the iterated derivative let g : ℝ → ℝ := λ y, ‖iterated_deriv_within (n + 1) f (Icc a b) y‖, use [has_Sup.Sup (g '' Icc a b) / n!], intros x hx, rw div_mul_eq_mul_div₀, refine taylor_mean_remainder_bound hab hf hx (λ y, _), exact (hf.continuous_on_iterated_deriv_within rfl.le $ unique_diff_on_Icc h) .norm.le_Sup_image_Icc, end
83b9757ff052e07bf2e88ce98d52f74f34ca46e8
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/abstract_zeta.lean
c164bc63cf11ead6241426d41159e2d0884830dd
[ "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
468
lean
-- The type of h depends on the zeta-expansion of n. When we abstract the -- subterm proving `n < 5`, we need to zeta-expand n not just in the subterm, -- but also in the local context. lemma bug₁ : fin 5 := let n : ℕ := 3 in have h : n < 5, from dec_trivial, ⟨n, by abstract { exact h }⟩ def bug₂ : fin 5 := let n : ℕ := 3 in have h : n < 5, from dec_trivial, ⟨n, show n < 5, from h⟩ -- ^^^^ the show is only used to trigger automatic abstraction
63c4dae3c8183f1866f5747218a1616317c4dc7f
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/topology/discrete_quotient.lean
f044d7e06a9a610b93ae0d1375a3fe25d585d2c6
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
12,389
lean
/- Copyright (c) 2021 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Calle Sönne, Adam Topaz -/ import topology.separation import topology.subset_properties import topology.locally_constant.basic /-! # Discrete quotients of a topological space. This file defines the type of discrete quotients of a topological space, denoted `discrete_quotient X`. To avoid quantifying over types, we model such quotients as setoids whose equivalence classes are clopen. ## Definitions 1. `discrete_quotient X` is the type of discrete quotients of `X`. It is endowed with a coercion to `Type`, which is defined as the quotient associated to the setoid in question, and each such quotient is endowed with the discrete topology. 2. Given `S : discrete_quotient X`, the projection `X → S` is denoted `S.proj`. 3. When `X` is compact and `S : discrete_quotient X`, the space `S` is endowed with a `fintype` instance. ## Order structure The type `discrete_quotient X` is endowed with an instance of a `semilattice_inf_top`. The partial ordering `A ≤ B` mathematically means that `B.proj` factors through `A.proj`. The top element `⊤` is the trivial quotient, meaning that every element of `X` is collapsed to a point. Given `h : A ≤ B`, the map `A → B` is `discrete_quotient.of_le h`. Given `f : X → Y` and `h : continuous f`, we define a predicate `le_comap h A B` for `A : discrete_quotient X` and `B : discrete_quotient Y`, asserting that `f` descends to `A → B`. If `cond : le_comap h A B`, the function `A → B` is obtained by `discrete_quotient.map cond`. ## Theorems The two main results proved in this file are: 1. `discrete_quotient.eq_of_proj_eq` which states that when `X` is compact, t2 and totally disconnected, any two elements of `X` agree if their projections in `Q` agree for all `Q : discrete_quotient X`. 2. `discrete_quotient.exists_of_compat` which states that when `X` is compact, then any system of elements of `Q` as `Q : discrete_quotient X` varies, which is compatible with respect to `discrete_quotient.of_le`, must arise from some element of `X`. ## Remarks The constructions in this file will be used to show that any profinite space is a limit of finite discrete spaces. -/ variables (X : Type*) [topological_space X] /-- The type of discrete quotients of a topological space. -/ @[ext] structure discrete_quotient := (rel : X → X → Prop) (equiv : equivalence rel) (clopen : ∀ x, is_clopen (set_of (rel x))) namespace discrete_quotient variables {X} (S : discrete_quotient X) /-- Construct a discrete quotient from a clopen set. -/ def of_clopen {A : set X} (h : is_clopen A) : discrete_quotient X := { rel := λ x y, x ∈ A ∧ y ∈ A ∨ x ∉ A ∧ y ∉ A, equiv := ⟨by tauto!, by tauto!, by tauto!⟩, clopen := begin intros x, by_cases hx : x ∈ A, { apply is_clopen.union, { convert h, ext, exact ⟨λ i, i.2, λ i, ⟨hx,i⟩⟩ }, { convert is_clopen_empty, tidy } }, { apply is_clopen.union, { convert is_clopen_empty, tidy }, { convert is_clopen.compl h, ext, exact ⟨λ i, i.2, λ i, ⟨hx, i⟩⟩ } }, end } lemma refl : ∀ x : X, S.rel x x := S.equiv.1 lemma symm : ∀ x y : X, S.rel x y → S.rel y x := S.equiv.2.1 lemma trans : ∀ x y z : X, S.rel x y → S.rel y z → S.rel x z := S.equiv.2.2 /-- The setoid whose quotient yields the discrete quotient. -/ def setoid : setoid X := ⟨S.rel, S.equiv⟩ instance : has_coe_to_sort (discrete_quotient X) := ⟨Type*, λ S, quotient S.setoid⟩ instance : topological_space S := ⊥ /-- The projection from `X` to the given discrete quotient. -/ def proj : X → S := quotient.mk' lemma proj_surjective : function.surjective S.proj := quotient.surjective_quotient_mk' lemma fiber_eq (x : X) : S.proj ⁻¹' {S.proj x} = set_of (S.rel x) := begin ext1 y, simp only [set.mem_preimage, set.mem_singleton_iff, quotient.eq', discrete_quotient.proj.equations._eqn_1, set.mem_set_of_eq], exact ⟨λ h, S.symm _ _ h, λ h, S.symm _ _ h⟩, end lemma proj_is_locally_constant : is_locally_constant S.proj := begin rw (is_locally_constant.tfae S.proj).out 0 3, intros x, rcases S.proj_surjective x with ⟨x,rfl⟩, simp [fiber_eq, (S.clopen x).1], end lemma proj_continuous : continuous S.proj := is_locally_constant.continuous $ proj_is_locally_constant _ lemma fiber_closed (A : set S) : is_closed (S.proj ⁻¹' A) := is_closed.preimage S.proj_continuous ⟨trivial⟩ lemma fiber_open (A : set S) : is_open (S.proj ⁻¹' A) := is_open.preimage S.proj_continuous trivial lemma fiber_clopen (A : set S) : is_clopen (S.proj ⁻¹' A) := ⟨fiber_open _ _, fiber_closed _ _⟩ instance : semilattice_inf_top (discrete_quotient X) := { top := ⟨λ a b, true, ⟨by tauto, by tauto, by tauto⟩, λ _, is_clopen_univ⟩, inf := λ A B, { rel := λ x y, A.rel x y ∧ B.rel x y, equiv := ⟨λ a, ⟨A.refl _,B.refl _⟩, λ a b h, ⟨A.symm _ _ h.1, B.symm _ _ h.2⟩, λ a b c h1 h2, ⟨A.trans _ _ _ h1.1 h2.1, B.trans _ _ _ h1.2 h2.2⟩⟩, clopen := λ x, is_clopen.inter (A.clopen _) (B.clopen _) }, le := λ A B, ∀ x y : X, A.rel x y → B.rel x y, le_refl := λ a, by tauto, le_trans := λ a b c h1 h2, by tauto, le_antisymm := λ a b h1 h2, by { ext, tauto }, inf_le_left := λ a b, by tauto, inf_le_right := λ a b, by tauto, le_inf := λ a b c h1 h2, by tauto, le_top := λ a, by tauto } instance : inhabited (discrete_quotient X) := ⟨⊤⟩ section comap variables {Y : Type*} [topological_space Y] {f : Y → X} (cont : continuous f) /-- Comap a discrete quotient along a continuous map. -/ def comap : discrete_quotient Y := { rel := λ a b, S.rel (f a) (f b), equiv := ⟨λ a, S.refl _, λ a b h, S.symm _ _ h, λ a b c h1 h2, S.trans _ _ _ h1 h2⟩, clopen := λ y, ⟨is_open.preimage cont (S.clopen _).1, is_closed.preimage cont (S.clopen _).2⟩ } @[simp] lemma comap_id : S.comap (continuous_id : continuous (id : X → X)) = S := by { ext, refl } @[simp] lemma comap_comp {Z : Type*} [topological_space Z] {g : Z → Y} (cont' : continuous g) : S.comap (continuous.comp cont cont') = (S.comap cont).comap cont' := by { ext, refl } lemma comap_mono {A B : discrete_quotient X} (h : A ≤ B) : A.comap cont ≤ B.comap cont := by tauto end comap section of_le /-- The map induced by a refinement of a discrete quotient. -/ def of_le {A B : discrete_quotient X} (h : A ≤ B) : A → B := λ a, quotient.lift_on' a (λ x, B.proj x) (λ a b i, quotient.sound' (h _ _ i)) @[simp] lemma of_le_refl {A : discrete_quotient X} : of_le (le_refl A) = id := by { ext ⟨⟩, refl } lemma of_le_refl_apply {A : discrete_quotient X} (a : A) : of_le (le_refl A) a = a := by simp @[simp] lemma of_le_comp {A B C : discrete_quotient X} (h1 : A ≤ B) (h2 : B ≤ C) : of_le (le_trans h1 h2) = of_le h2 ∘ of_le h1 := by { ext ⟨⟩, refl } lemma of_le_comp_apply {A B C : discrete_quotient X} (h1 : A ≤ B) (h2 : B ≤ C) (a : A) : of_le (le_trans h1 h2) a = of_le h2 (of_le h1 a) := by simp lemma of_le_continuous {A B : discrete_quotient X} (h : A ≤ B) : continuous (of_le h) := continuous_of_discrete_topology @[simp] lemma of_le_proj {A B : discrete_quotient X} (h : A ≤ B) : of_le h ∘ A.proj = B.proj := by { ext, exact quotient.sound' (B.refl _) } @[simp] lemma of_le_proj_apply {A B : discrete_quotient X} (h : A ≤ B) (x : X) : of_le h (A.proj x) = B.proj x := by { change (of_le h ∘ A.proj) x = _, simp } end of_le section map variables {Y : Type*} [topological_space Y] {f : Y → X} (cont : continuous f) (A : discrete_quotient Y) (B : discrete_quotient X) /-- Given `cont : continuous f`, `le_comap cont A B` is defined as `A ≤ B.comap f`. Mathematically this means that `f` descends to a morphism `A → B`. -/ def le_comap : Prop := A ≤ B.comap cont variables {cont A B} lemma le_comap_id (A : discrete_quotient X) : le_comap continuous_id A A := by tauto lemma le_comap_comp {Z : Type*} [topological_space Z] {g : Z → Y} {cont' : continuous g} {C : discrete_quotient Z} : le_comap cont' C A → le_comap cont A B → le_comap (continuous.comp cont cont') C B := by tauto lemma le_comap_trans {C : discrete_quotient X} : le_comap cont A B → B ≤ C → le_comap cont A C := λ h1 h2, le_trans h1 $ comap_mono _ h2 /-- Map a discrete quotient along a continuous map. -/ def map (cond : le_comap cont A B) : A → B := quotient.map' f cond lemma map_continuous (cond : le_comap cont A B) : continuous (map cond) := continuous_of_discrete_topology @[simp] lemma map_proj (cond : le_comap cont A B) : map cond ∘ A.proj = B.proj ∘ f := rfl @[simp] lemma map_proj_apply (cond : le_comap cont A B) (y : Y) : map cond (A.proj y) = B.proj (f y) := rfl @[simp] lemma map_id : map (le_comap_id A) = id := by { ext ⟨⟩, refl } @[simp] lemma map_comp {Z : Type*} [topological_space Z] {g : Z → Y} {cont' : continuous g} {C : discrete_quotient Z} (h1 : le_comap cont' C A) (h2 : le_comap cont A B) : map (le_comap_comp h1 h2) = map h2 ∘ map h1 := by { ext ⟨⟩, refl } @[simp] lemma of_le_map {C : discrete_quotient X} (cond : le_comap cont A B) (h : B ≤ C) : map (le_comap_trans cond h) = of_le h ∘ map cond := by { ext ⟨⟩, refl } @[simp] lemma of_le_map_apply {C : discrete_quotient X} (cond : le_comap cont A B) (h : B ≤ C) (a : A) : map (le_comap_trans cond h) a = of_le h (map cond a) := by { rcases a, refl } @[simp] lemma map_of_le {C : discrete_quotient Y} (cond : le_comap cont A B) (h : C ≤ A) : map (le_trans h cond) = map cond ∘ of_le h := by { ext ⟨⟩, refl } @[simp] lemma map_of_le_apply {C : discrete_quotient Y} (cond : le_comap cont A B) (h : C ≤ A) (c : C) : map (le_trans h cond) c = map cond (of_le h c) := by { rcases c, refl } end map lemma eq_of_proj_eq [t2_space X] [compact_space X] [disc : totally_disconnected_space X] {x y : X} : (∀ Q : discrete_quotient X, Q.proj x = Q.proj y) → x = y := begin intro h, change x ∈ ({y} : set X), rw totally_disconnected_space_iff_connected_component_singleton at disc, rw [← disc y, connected_component_eq_Inter_clopen], rintros U ⟨⟨U, hU1, hU2⟩, rfl⟩, replace h : _ ∨ _ := quotient.exact' (h (of_clopen hU1)), tauto, end lemma fiber_le_of_le {A B : discrete_quotient X} (h : A ≤ B) (a : A) : A.proj ⁻¹' {a} ≤ B.proj ⁻¹' {of_le h a} := begin induction a, erw [fiber_eq, fiber_eq], tidy, end lemma exists_of_compat [compact_space X] (Qs : Π (Q : discrete_quotient X), Q) (compat : ∀ (A B : discrete_quotient X) (h : A ≤ B), of_le h (Qs _) = Qs _) : ∃ x : X, ∀ Q : discrete_quotient X, Q.proj x = Qs _ := begin obtain ⟨x,hx⟩ := is_compact.nonempty_Inter_of_directed_nonempty_compact_closed (λ (Q : discrete_quotient X), Q.proj ⁻¹' {Qs _}) (λ A B, _) (λ i, _) (λ i, (fiber_closed _ _).is_compact) (λ i, fiber_closed _ _), { refine ⟨x, λ Q, _⟩, specialize hx _ ⟨Q,rfl⟩, dsimp at hx, rcases proj_surjective _ (Qs Q) with ⟨y,hy⟩, rw ← hy at *, rw fiber_eq at hx, exact quotient.sound' (Q.symm y x hx) }, { refine ⟨A ⊓ B, λ a ha, _, λ a ha, _⟩, { dsimp only, erw ← compat (A ⊓ B) A inf_le_left, exact fiber_le_of_le _ _ ha }, { dsimp only, erw ← compat (A ⊓ B) B inf_le_right, exact fiber_le_of_le _ _ ha } }, { obtain ⟨x,hx⟩ := i.proj_surjective (Qs i), refine ⟨x,_⟩, dsimp only, rw [← hx, fiber_eq], apply i.refl }, end noncomputable instance [compact_space X] : fintype S := begin have cond : is_compact (⊤ : set X) := compact_univ, rw is_compact_iff_finite_subcover at cond, have h := @cond S (λ s, S.proj ⁻¹' {s}) (λ s, fiber_open _ _) (λ x hx, ⟨S.proj ⁻¹' {S.proj x}, ⟨S.proj x, rfl⟩, rfl⟩), let T := classical.some h, have hT := classical.some_spec h, refine ⟨T,λ s, _⟩, rcases S.proj_surjective s with ⟨x,rfl⟩, rcases hT (by tauto : x ∈ ⊤) with ⟨j, ⟨j,rfl⟩, h1, ⟨hj, rfl⟩, h2⟩, dsimp only at h2, suffices : S.proj x = j, by rwa this, rcases j with ⟨j⟩, apply quotient.sound', erw fiber_eq at h2, exact S.symm _ _ h2 end end discrete_quotient
9db6d709555ba9e169aa09b51c9cf0a468e41081
c777c32c8e484e195053731103c5e52af26a25d1
/src/number_theory/dioph.lean
5b5d2b23291d114928a9f2c2b736cc026e01f3b8
[ "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
26,435
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.fin.fin2 import data.pfun import data.vector3 import number_theory.pell_matiyasevic /-! # Diophantine functions and Matiyasevic's theorem Hilbert's tenth problem asked whether there exists an algorithm which for a given integer polynomial determines whether this polynomial has integer solutions. It was answered in the negative in 1970, the final step being completed by Matiyasevic who showed that the power function is Diophantine. Here a function is called Diophantine if its graph is Diophantine as a set. A subset `S ⊆ ℕ ^ α` in turn is called Diophantine if there exists an integer polynomial on `α ⊕ β` such that `v ∈ S` iff there exists `t : ℕ^β` with `p (v, t) = 0`. ## Main definitions * `is_poly`: a predicate stating that a function is a multivariate integer polynomial. * `poly`: the type of multivariate integer polynomial functions. * `dioph`: a predicate stating that a set is Diophantine, i.e. a set `S ⊆ ℕ^α` is Diophantine if there exists a polynomial on `α ⊕ β` such that `v ∈ S` iff there exists `t : ℕ^β` with `p (v, t) = 0`. * `dioph_fn`: a predicate on a function stating that it is Diophantine in the sense that its graph is Diophantine as a set. ## Main statements * `pell_dioph` states that solutions to Pell's equation form a Diophantine set. * `pow_dioph` states that the power function is Diophantine, a version of Matiyasevic's theorem. ## References * [M. Carneiro, _A Lean formalization of Matiyasevic's theorem_][carneiro2018matiyasevic] * [M. Davis, _Hilbert's tenth problem is unsolvable_][MR317916] ## Tags Matiyasevic's theorem, Hilbert's tenth problem ## TODO * Finish the solution of Hilbert's tenth problem. * Connect `poly` to `mv_polynomial` -/ open fin2 function nat sum local infixr ` ::ₒ `:67 := option.elim local infixr ` ⊗ `:65 := sum.elim universe u /-! ### Multivariate integer polynomials Note that this duplicates `mv_polynomial`. -/ section polynomials variables {α β γ : Type*} /-- A predicate asserting that a function is a multivariate integer polynomial. (We are being a bit lazy here by allowing many representations for multiplication, rather than only allowing monomials and addition, but the definition is equivalent and this is easier to use.) -/ inductive is_poly : ((α → ℕ) → ℤ) → Prop | proj : ∀ i, is_poly (λ x : α → ℕ, x i) | const : Π (n : ℤ), is_poly (λ x : α → ℕ, n) | sub : Π {f g : (α → ℕ) → ℤ}, is_poly f → is_poly g → is_poly (λ x, f x - g x) | mul : Π {f g : (α → ℕ) → ℤ}, is_poly f → is_poly g → is_poly (λ x, f x * g x) lemma is_poly.neg {f : (α → ℕ) → ℤ} : is_poly f → is_poly (-f) := by { rw ←zero_sub, exact (is_poly.const 0).sub } lemma is_poly.add {f g : (α → ℕ) → ℤ} (hf : is_poly f) (hg : is_poly g) : is_poly (f + g) := by { rw ←sub_neg_eq_add, exact hf.sub hg.neg } /-- The type of multivariate integer polynomials -/ def poly (α : Type u) := {f : (α → ℕ) → ℤ // is_poly f} namespace poly section instance fun_like : fun_like (poly α) (α → ℕ) (λ _, ℤ) := ⟨subtype.val, subtype.val_injective⟩ /-- Helper instance for when there are too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (poly α) (λ _, (α → ℕ) → ℤ) := fun_like.has_coe_to_fun /-- The underlying function of a `poly` is a polynomial -/ protected lemma is_poly (f : poly α) : is_poly f := f.2 /-- Extensionality for `poly α` -/ @[ext] lemma ext {f g : poly α} : (∀ x, f x = g x) → f = g := fun_like.ext _ _ /-- The `i`th projection function, `x_i`. -/ def proj (i) : poly α := ⟨_, is_poly.proj i⟩ @[simp] lemma proj_apply (i : α) (x) : proj i x = x i := rfl /-- The constant function with value `n : ℤ`. -/ def const (n) : poly α := ⟨_, is_poly.const n⟩ @[simp] lemma const_apply (n) (x : α → ℕ) : const n x = n := rfl instance : has_zero (poly α) := ⟨const 0⟩ instance : has_one (poly α) := ⟨const 1⟩ instance : has_neg (poly α) := ⟨λ f, ⟨-f, f.2.neg⟩⟩ instance : has_add (poly α) := ⟨λ f g, ⟨f + g, f.2.add g.2⟩⟩ instance : has_sub (poly α) := ⟨λ f g, ⟨f - g, f.2.sub g.2⟩⟩ instance : has_mul (poly α) := ⟨λ f g, ⟨f * g, f.2.mul g.2⟩⟩ @[simp] lemma coe_zero : ⇑(0 : poly α) = const 0 := rfl @[simp] lemma coe_one : ⇑(1 : poly α) = const 1 := rfl @[simp] lemma coe_neg (f : poly α) : ⇑(-f) = -f := rfl @[simp] lemma coe_add (f g : poly α) : ⇑(f + g) = f + g := rfl @[simp] lemma coe_sub (f g : poly α) : ⇑(f - g) = f - g := rfl @[simp] lemma coe_mul (f g : poly α) : ⇑(f * g) = f * g := rfl @[simp] lemma zero_apply (x) : (0 : poly α) x = 0 := rfl @[simp] lemma one_apply (x) : (1 : poly α) x = 1 := rfl @[simp] lemma neg_apply (f : poly α) (x) : (-f) x = -f x := rfl @[simp] lemma add_apply (f g : poly α) (x : α → ℕ) : (f + g) x = f x + g x := rfl @[simp] lemma sub_apply (f g : poly α) (x : α → ℕ) : (f - g) x = f x - g x := rfl @[simp] lemma mul_apply (f g : poly α) (x : α → ℕ) : (f * g) x = f x * g x := rfl instance (α : Type*) : inhabited (poly α) := ⟨0⟩ instance : add_comm_group (poly α) := by refine_struct { add := ((+) : poly α → poly α → poly α), neg := (has_neg.neg : poly α → poly α), sub := (has_sub.sub), zero := 0, zsmul := @zsmul_rec _ ⟨(0 : poly α)⟩ ⟨(+)⟩ ⟨has_neg.neg⟩, nsmul := @nsmul_rec _ ⟨(0 : poly α)⟩ ⟨(+)⟩ }; intros; try { refl }; refine ext (λ _, _); simp [sub_eq_add_neg, add_comm, add_assoc] instance : add_group_with_one (poly α) := { one := 1, nat_cast := λ n, poly.const n, int_cast := poly.const, .. poly.add_comm_group } instance : comm_ring (poly α) := by refine_struct { add := ((+) : poly α → poly α → poly α), zero := 0, mul := (*), one := 1, npow := @npow_rec _ ⟨(1 : poly α)⟩ ⟨(*)⟩, .. poly.add_group_with_one, .. poly.add_comm_group }; intros; try { refl }; refine ext (λ _, _); simp [sub_eq_add_neg, mul_add, mul_left_comm, mul_comm, add_comm, add_assoc] lemma induction {C : poly α → Prop} (H1 : ∀i, C (proj i)) (H2 : ∀n, C (const n)) (H3 : ∀f g, C f → C g → C (f - g)) (H4 : ∀f g, C f → C g → C (f * g)) (f : poly α) : C f := begin cases f with f pf, induction pf with i n f g pf pg ihf ihg f g pf pg ihf ihg, apply H1, apply H2, apply H3 _ _ ihf ihg, apply H4 _ _ ihf ihg end /-- The sum of squares of a list of polynomials. This is relevant for Diophantine equations, because it means that a list of equations can be encoded as a single equation: `x = 0 ∧ y = 0 ∧ z = 0` is equivalent to `x^2 + y^2 + z^2 = 0`. -/ def sumsq : list (poly α) → poly α | [] := 0 | (p::ps) := p*p + sumsq ps lemma sumsq_nonneg (x : α → ℕ) : ∀ l, 0 ≤ sumsq l x | [] := le_refl 0 | (p::ps) := by rw sumsq; simp [-add_comm]; exact add_nonneg (mul_self_nonneg _) (sumsq_nonneg ps) lemma sumsq_eq_zero (x) : ∀ l, sumsq l x = 0 ↔ l.all₂ (λ a : poly α, a x = 0) | [] := eq_self_iff_true _ | (p::ps) := by rw [list.all₂_cons, ← sumsq_eq_zero ps]; rw sumsq; simp [-add_comm]; exact ⟨λ (h : p x * p x + sumsq ps x = 0), have p x = 0, from eq_zero_of_mul_self_eq_zero $ le_antisymm (by rw ← h; have t := add_le_add_left (sumsq_nonneg x ps) (p x * p x); rwa [add_zero] at t) (mul_self_nonneg _), ⟨this, by simp [this] at h; exact h⟩, λ ⟨h1, h2⟩, by rw [h1, h2]; refl⟩ end /-- Map the index set of variables, replacing `x_i` with `x_(f i)`. -/ def map {α β} (f : α → β) (g : poly α) : poly β := ⟨λ v, g $ v ∘ f, g.induction (λ i, by simp; apply is_poly.proj) (λ n, by simp; apply is_poly.const) (λ f g pf pg, by simp; apply is_poly.sub pf pg) (λ f g pf pg, by simp; apply is_poly.mul pf pg)⟩ @[simp] lemma map_apply {α β} (f : α → β) (g : poly α) (v) : map f g v = g (v ∘ f) := rfl end poly end polynomials /-! ### Diophantine sets -/ /-- A set `S ⊆ ℕ^α` is Diophantine if there exists a polynomial on `α ⊕ β` such that `v ∈ S` iff there exists `t : ℕ^β` with `p (v, t) = 0`. -/ def dioph {α : Type u} (S : set (α → ℕ)) : Prop := ∃ {β : Type u} (p : poly (α ⊕ β)), ∀ v, S v ↔ ∃ t, p (v ⊗ t) = 0 namespace dioph section variables {α β γ : Type u} {S S' : set (α → ℕ)} lemma ext (d : dioph S) (H : ∀ v, v ∈ S ↔ v ∈ S') : dioph S' := by rwa ←set.ext H lemma of_no_dummies (S : set (α → ℕ)) (p : poly α) (h : ∀ v, S v ↔ p v = 0) : dioph S := ⟨pempty, p.map inl, λ v, (h v).trans ⟨λ h, ⟨pempty.rec _, h⟩, λ ⟨t, ht⟩, ht⟩⟩ lemma inject_dummies_lem (f : β → γ) (g : γ → option β) (inv : ∀ x, g (f x) = some x) (p : poly (α ⊕ β)) (v : α → ℕ) : (∃ t, p (v ⊗ t) = 0) ↔ ∃ t, p.map (inl ⊗ inr ∘ f) (v ⊗ t) = 0 := begin dsimp, refine ⟨λ t, _, λ t, _⟩; cases t with t ht, { have : (v ⊗ (0 ::ₒ t) ∘ g) ∘ (inl ⊗ inr ∘ f) = v ⊗ t := funext (λ s, by cases s with a b; dsimp [(∘)]; try {rw inv}; refl), exact ⟨(0 ::ₒ t) ∘ g, by rwa this⟩ }, { have : v ⊗ t ∘ f = (v ⊗ t) ∘ (inl ⊗ inr ∘ f) := funext (λ s, by cases s with a b; refl), exact ⟨t ∘ f, by rwa this⟩ } end lemma inject_dummies (f : β → γ) (g : γ → option β) (inv : ∀ x, g (f x) = some x) (p : poly (α ⊕ β)) (h : ∀ v, S v ↔ ∃ t, p (v ⊗ t) = 0) : ∃ q : poly (α ⊕ γ), ∀ v, S v ↔ ∃ t, q (v ⊗ t) = 0 := ⟨p.map (inl ⊗ (inr ∘ f)), λ v, (h v).trans $ inject_dummies_lem f g inv _ _⟩ variables (β) lemma reindex_dioph (f : α → β) : Π (d : dioph S), dioph {v | v ∘ f ∈ S} | ⟨γ, p, pe⟩ := ⟨γ, p.map ((inl ∘ f) ⊗ inr), λ v, (pe _).trans $ exists_congr $ λ t, suffices v ∘ f ⊗ t = (v ⊗ t) ∘ (inl ∘ f ⊗ inr), by simp [this], funext $ λ s, by cases s with a b; refl⟩ variables {β} lemma dioph_list.all₂ (l : list (set $ α → ℕ)) (d : l.all₂ dioph) : dioph {v | l.all₂ (λ S : set (α → ℕ), v ∈ S)} := suffices ∃ β (pl : list (poly (α ⊕ β))), ∀ v, list.all₂ (λ S : set _, S v) l ↔ ∃ t, list.all₂ (λ p : poly (α ⊕ β), p (v ⊗ t) = 0) pl, from let ⟨β, pl, h⟩ := this in ⟨β, poly.sumsq pl, λ v, (h v).trans $ exists_congr $ λ t, (poly.sumsq_eq_zero _ _).symm⟩, begin induction l with S l IH, exact ⟨ulift empty, [], λ v, by simp; exact ⟨λ ⟨t⟩, empty.rec _ t, trivial⟩⟩, simp at d, exact let ⟨⟨β, p, pe⟩, dl⟩ := d, ⟨γ, pl, ple⟩ := IH dl in ⟨β ⊕ γ, p.map (inl ⊗ inr ∘ inl) :: pl.map (λ q, q.map (inl ⊗ (inr ∘ inr))), λ v, by simp; exact iff.trans (and_congr (pe v) (ple v)) ⟨λ ⟨⟨m, hm⟩, ⟨n, hn⟩⟩, ⟨m ⊗ n, by rw [ show (v ⊗ m ⊗ n) ∘ (inl ⊗ inr ∘ inl) = v ⊗ m, from funext $ λ s, by cases s with a b; refl]; exact hm, by { refine list.all₂.imp (λ q hq, _) hn, dsimp [(∘)], rw [show (λ (x : α ⊕ γ), (v ⊗ m ⊗ n) ((inl ⊗ λ (x : γ), inr (inr x)) x)) = v ⊗ n, from funext $ λ s, by cases s with a b; refl]; exact hq }⟩, λ ⟨t, hl, hr⟩, ⟨⟨t ∘ inl, by rwa [ show (v ⊗ t) ∘ (inl ⊗ inr ∘ inl) = v ⊗ t ∘ inl, from funext $ λ s, by cases s with a b; refl] at hl⟩, ⟨t ∘ inr, by { refine list.all₂.imp (λ q hq, _) hr, dsimp [(∘)] at hq, rwa [show (λ (x : α ⊕ γ), (v ⊗ t) ((inl ⊗ λ (x : γ), inr (inr x)) x)) = v ⊗ t ∘ inr, from funext $ λ s, by cases s with a b; refl] at hq }⟩⟩⟩⟩ end lemma inter (d : dioph S) (d' : dioph S') : dioph (S ∩ S') := dioph_list.all₂ [S, S'] ⟨d, d'⟩ lemma union : ∀ (d : dioph S) (d' : dioph S'), dioph (S ∪ S') | ⟨β, p, pe⟩ ⟨γ, q, qe⟩ := ⟨β ⊕ γ, p.map (inl ⊗ inr ∘ inl) * q.map (inl ⊗ inr ∘ inr), λ v, begin refine iff.trans (or_congr ((pe v).trans _) ((qe v).trans _)) (exists_or_distrib.symm.trans (exists_congr $ λ t, (@mul_eq_zero _ _ _ (p ((v ⊗ t) ∘ (inl ⊗ inr ∘ inl))) (q ((v ⊗ t) ∘ (inl ⊗ inr ∘ inr)))).symm)), exact inject_dummies_lem _ (some ⊗ (λ _, none)) (λ x, rfl) _ _, exact inject_dummies_lem _ ((λ _, none) ⊗ some) (λ x, rfl) _ _, end⟩ /-- A partial function is Diophantine if its graph is Diophantine. -/ def dioph_pfun (f : (α → ℕ) →. ℕ) : Prop := dioph {v : option α → ℕ | f.graph (v ∘ some, v none)} /-- A function is Diophantine if its graph is Diophantine. -/ def dioph_fn (f : (α → ℕ) → ℕ) : Prop := dioph {v : option α → ℕ | f (v ∘ some) = v none} lemma reindex_dioph_fn {f : (α → ℕ) → ℕ} (g : α → β) (d : dioph_fn f) : dioph_fn (λ v, f (v ∘ g)) := by convert reindex_dioph (option β) (option.map g) d lemma ex_dioph {S : set (α ⊕ β → ℕ)} : dioph S → dioph {v | ∃ x, v ⊗ x ∈ S} | ⟨γ, p, pe⟩ := ⟨β ⊕ γ, p.map ((inl ⊗ inr ∘ inl) ⊗ inr ∘ inr), λ v, ⟨λ ⟨x, hx⟩, let ⟨t, ht⟩ := (pe _).1 hx in ⟨x ⊗ t, by simp; rw [ show (v ⊗ x ⊗ t) ∘ ((inl ⊗ inr ∘ inl) ⊗ inr ∘ inr) = (v ⊗ x) ⊗ t, from funext $ λ s, by cases s with a b; try {cases a}; refl]; exact ht⟩, λ ⟨t, ht⟩, ⟨t ∘ inl, (pe _).2 ⟨t ∘ inr, by simp at ht; rwa [ show (v ⊗ t) ∘ ((inl ⊗ inr ∘ inl) ⊗ inr ∘ inr) = (v ⊗ t ∘ inl) ⊗ t ∘ inr, from funext $ λ s, by cases s with a b; try {cases a}; refl] at ht⟩⟩⟩⟩ lemma ex1_dioph {S : set (option α → ℕ)} : dioph S → dioph {v | ∃ x, x ::ₒ v ∈ S} | ⟨β, p, pe⟩ := ⟨option β, p.map (inr none ::ₒ inl ⊗ inr ∘ some), λ v, ⟨λ ⟨x, hx⟩, let ⟨t, ht⟩ := (pe _).1 hx in ⟨x ::ₒ t, by simp; rw [ show (v ⊗ x ::ₒ t) ∘ (inr none ::ₒ inl ⊗ inr ∘ some) = x ::ₒ v ⊗ t, from funext $ λ s, by cases s with a b; try {cases a}; refl]; exact ht⟩, λ ⟨t, ht⟩, ⟨t none, (pe _).2 ⟨t ∘ some, by simp at ht; rwa [ show (v ⊗ t) ∘ (inr none ::ₒ inl ⊗ inr ∘ some) = t none ::ₒ v ⊗ t ∘ some, from funext $ λ s, by cases s with a b; try {cases a}; refl] at ht⟩⟩⟩⟩ theorem dom_dioph {f : (α → ℕ) →. ℕ} (d : dioph_pfun f) : dioph f.dom := cast (congr_arg dioph $ set.ext $ λ v, (pfun.dom_iff_graph _ _).symm) (ex1_dioph d) theorem dioph_fn_iff_pfun (f : (α → ℕ) → ℕ) : dioph_fn f = @dioph_pfun α f := by refine congr_arg dioph (set.ext $ λ v, _); exact pfun.lift_graph.symm lemma abs_poly_dioph (p : poly α) : dioph_fn (λ v, (p v).nat_abs) := of_no_dummies _ ((p.map some - poly.proj none) * (p.map some + poly.proj none)) $ λ v, by { dsimp, exact int.eq_nat_abs_iff_mul_eq_zero } theorem proj_dioph (i : α) : dioph_fn (λ v, v i) := abs_poly_dioph (poly.proj i) theorem dioph_pfun_comp1 {S : set (option α → ℕ)} (d : dioph S) {f} (df : dioph_pfun f) : dioph {v : α → ℕ | ∃ h : f.dom v, f.fn v h ::ₒ v ∈ S} := ext (ex1_dioph (d.inter df)) $ λ v, ⟨λ ⟨x, hS, (h: Exists _)⟩, by rw [show (x ::ₒ v) ∘ some = v, from funext $ λ s, rfl] at h; cases h with hf h; refine ⟨hf, _⟩; rw [pfun.fn, h]; exact hS, λ ⟨x, hS⟩, ⟨f.fn v x, hS, show Exists _, by rw [show (f.fn v x ::ₒ v) ∘ some = v, from funext $ λ s, rfl]; exact ⟨x, rfl⟩⟩⟩ theorem dioph_fn_comp1 {S : set (option α → ℕ)} (d : dioph S) {f : (α → ℕ) → ℕ} (df : dioph_fn f) : dioph {v | f v ::ₒ v ∈ S} := ext (dioph_pfun_comp1 d $ cast (dioph_fn_iff_pfun f) df) $ λ v, ⟨λ ⟨_, h⟩, h, λ h, ⟨trivial, h⟩⟩ end section variables {α β : Type} {n : ℕ} open vector3 open_locale vector3 local attribute [reducible] vector3 lemma dioph_fn_vec_comp1 {S : set (vector3 ℕ (succ n))} (d : dioph S) {f : vector3 ℕ n → ℕ} (df : dioph_fn f) : dioph {v : vector3 ℕ n | f v :: v ∈ S} := ext (dioph_fn_comp1 (reindex_dioph _ (none :: some) d) df) $ λ v, by { dsimp, congr', ext x, cases x; refl } theorem vec_ex1_dioph (n) {S : set (vector3 ℕ (succ n))} (d : dioph S) : dioph {v : fin2 n → ℕ | ∃ x, x :: v ∈ S} := ext (ex1_dioph $ reindex_dioph _ (none :: some) d) $ λ v, exists_congr $ λ x, by { dsimp, rw [show option.elim x v ∘ cons none some = x :: v, from funext $ λ s, by cases s with a b; refl] } lemma dioph_fn_vec (f : vector3 ℕ n → ℕ) : dioph_fn f ↔ dioph {v | f (v ∘ fs) = v fz} := ⟨reindex_dioph _ (fz ::ₒ fs), reindex_dioph _ (none :: some)⟩ lemma dioph_pfun_vec (f : vector3 ℕ n →. ℕ) : dioph_pfun f ↔ dioph {v | f.graph (v ∘ fs, v fz)} := ⟨reindex_dioph _ (fz ::ₒ fs), reindex_dioph _ (none :: some)⟩ lemma dioph_fn_compn : ∀ {n} {S : set (α ⊕ fin2 n → ℕ)} (d : dioph S) {f : vector3 ((α → ℕ) → ℕ) n} (df : vector_allp dioph_fn f), dioph {v : α → ℕ | v ⊗ (λ i, f i v) ∈ S} | 0 S d f := λ df, ext (reindex_dioph _ (id ⊗ fin2.elim0) d) $ λ v, by { dsimp, congr', ext x, obtain (_ | _ | _) := x, refl } | (succ n) S d f := f.cons_elim $ λ f fl, by simp; exact λ df dfl, have dioph {v |v ∘ inl ⊗ f (v ∘ inl) :: v ∘ inr ∈ S}, from ext (dioph_fn_comp1 (reindex_dioph _ (some ∘ inl ⊗ none :: some ∘ inr) d) $ reindex_dioph_fn inl df) $ λ v, by { dsimp, congr', ext x, obtain (_ | _ | _) := x; refl }, have dioph {v | v ⊗ f v :: (λ (i : fin2 n), fl i v) ∈ S}, from @dioph_fn_compn n (λ v, S (v ∘ inl ⊗ f (v ∘ inl) :: v ∘ inr)) this _ dfl, ext this $ λ v, by { dsimp, congr', ext x, obtain _ | _ | _ := x; refl } lemma dioph_comp {S : set (vector3 ℕ n)} (d : dioph S) (f : vector3 ((α → ℕ) → ℕ) n) (df : vector_allp dioph_fn f) : dioph {v | (λ i, f i v) ∈ S} := dioph_fn_compn (reindex_dioph _ inr d) df lemma dioph_fn_comp {f : vector3 ℕ n → ℕ} (df : dioph_fn f) (g : vector3 ((α → ℕ) → ℕ) n) (dg : vector_allp dioph_fn g) : dioph_fn (λ v, f (λ i, g i v)) := dioph_comp ((dioph_fn_vec _).1 df) ((λ v, v none) :: λ i v, g i (v ∘ some)) $ by simp; exact ⟨proj_dioph none, (vector_allp_iff_forall _ _).2 $ λ i, reindex_dioph_fn _ $ (vector_allp_iff_forall _ _).1 dg _⟩ localized "notation (name := dioph.inter) x ` D∧ `:35 y := dioph.inter x y" in dioph localized "notation (name := dioph.union) x ` D∨ `:35 y := dioph.union x y" in dioph localized "notation (name := dioph.vec_ex1_dioph) `D∃`:30 := dioph.vec_ex1_dioph" in dioph localized "prefix (name := fin2.of_nat') `&`:max := fin2.of_nat'" in dioph theorem proj_dioph_of_nat {n : ℕ} (m : ℕ) [is_lt m n] : dioph_fn (λ v : vector3 ℕ n, v &m) := proj_dioph &m localized "prefix (name := proj_dioph_of_nat) `D&`:100 := dioph.proj_dioph_of_nat" in dioph theorem const_dioph (n : ℕ) : dioph_fn (const (α → ℕ) n) := abs_poly_dioph (poly.const n) localized "prefix (name := const_dioph) `D.`:100 := dioph.const_dioph" in dioph variables {f g : (α → ℕ) → ℕ} (df : dioph_fn f) (dg : dioph_fn g) include df dg lemma dioph_comp2 {S : ℕ → ℕ → Prop} (d : dioph (λ v:vector3 ℕ 2, S (v &0) (v &1))) : dioph (λ v, S (f v) (g v)) := dioph_comp d [f, g] (by exact ⟨df, dg⟩) lemma dioph_fn_comp2 {h : ℕ → ℕ → ℕ} (d : dioph_fn (λ v:vector3 ℕ 2, h (v &0) (v &1))) : dioph_fn (λ v, h (f v) (g v)) := dioph_fn_comp d [f, g] (by exact ⟨df, dg⟩) lemma eq_dioph : dioph (λ v, f v = g v) := dioph_comp2 df dg $ of_no_dummies _ (poly.proj &0 - poly.proj &1) (λ v, (int.coe_nat_eq_coe_nat_iff _ _).symm.trans ⟨@sub_eq_zero_of_eq ℤ _ (v &0) (v &1), eq_of_sub_eq_zero⟩) localized "infix (name := eq_dioph) ` D= `:50 := dioph.eq_dioph" in dioph lemma add_dioph : dioph_fn (λ v, f v + g v) := dioph_fn_comp2 df dg $ abs_poly_dioph (poly.proj &0 + poly.proj &1) localized "infix (name := add_dioph) ` D+ `:80 := dioph.add_dioph" in dioph lemma mul_dioph : dioph_fn (λ v, f v * g v) := dioph_fn_comp2 df dg $ abs_poly_dioph (poly.proj &0 * poly.proj &1) localized "infix (name := mul_dioph) ` D* `:90 := dioph.mul_dioph" in dioph lemma le_dioph : dioph {v | f v ≤ g v} := dioph_comp2 df dg $ ext (D∃2 $ D&1 D+ D&0 D= D&2) (λ v, ⟨λ ⟨x, hx⟩, le.intro hx, le.dest⟩) localized "infix (name := le_dioph) ` D≤ `:50 := dioph.le_dioph" in dioph lemma lt_dioph : dioph {v | f v < g v} := df D+ (D. 1) D≤ dg localized "infix (name := lt_dioph) ` D< `:50 := dioph.lt_dioph" in dioph lemma ne_dioph : dioph {v | f v ≠ g v} := ext (df D< dg D∨ dg D< df) $ λ v, by { dsimp, exact lt_or_lt_iff_ne } localized "infix (name := ne_dioph) ` D≠ `:50 := dioph.ne_dioph" in dioph lemma sub_dioph : dioph_fn (λ v, f v - g v) := dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ ext (D&1 D= D&0 D+ D&2 D∨ D&1 D≤ D&2 D∧ D&0 D= D.0) $ (vector_all_iff_forall _).1 $ λ x y z, show (y = x + z ∨ y ≤ z ∧ x = 0) ↔ y - z = x, from ⟨λ o, begin rcases o with ae | ⟨yz, x0⟩, { rw [ae, add_tsub_cancel_right] }, { rw [x0, tsub_eq_zero_iff_le.mpr yz] } end, begin rintro rfl, cases le_total y z with yz zy, { exact or.inr ⟨yz, tsub_eq_zero_iff_le.mpr yz⟩ }, { exact or.inl (tsub_add_cancel_of_le zy).symm }, end⟩ localized "infix (name := sub_dioph) ` D- `:80 := dioph.sub_dioph" in dioph lemma dvd_dioph : dioph (λ v, f v ∣ g v) := dioph_comp (D∃2 $ D&2 D= D&1 D* D&0) [f, g] (by exact ⟨df, dg⟩) localized "infix (name := dvd_dioph) ` D∣ `:50 := dioph.dvd_dioph" in dioph lemma mod_dioph : dioph_fn (λ v, f v % g v) := have dioph (λ v : vector3 ℕ 3, (v &2 = 0 ∨ v &0 < v &2) ∧ ∃ (x : ℕ), v &0 + v &2 * x = v &1), from (D&2 D= D.0 D∨ D&0 D< D&2) D∧ (D∃3 $ D&1 D+ D&3 D* D&0 D= D&2), dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ ext this $ (vector_all_iff_forall _).1 $ λ z x y, show ((y = 0 ∨ z < y) ∧ ∃ c, z + y * c = x) ↔ x % y = z, from ⟨λ ⟨h, c, hc⟩, begin rw ← hc; simp; cases h with x0 hl, rw [x0, mod_zero], exact mod_eq_of_lt hl end, λ e, by rw ← e; exact ⟨or_iff_not_imp_left.2 $ λ h, mod_lt _ (nat.pos_of_ne_zero h), x / y, mod_add_div _ _⟩⟩ localized "infix (name := mod_dioph) ` D% `:80 := dioph.mod_dioph" in dioph lemma modeq_dioph {h : (α → ℕ) → ℕ} (dh : dioph_fn h) : dioph (λ v, f v ≡ g v [MOD h v]) := df D% dh D= dg D% dh localized "notation (name := modeq_dioph) ` D≡ ` := dioph.modeq_dioph" in dioph lemma div_dioph : dioph_fn (λ v, f v / g v) := have dioph (λ v : vector3 ℕ 3, v &2 = 0 ∧ v &0 = 0 ∨ v &0 * v &2 ≤ v &1 ∧ v &1 < (v &0 + 1) * v &2), from (D&2 D= D.0 D∧ D&0 D= D.0) D∨ D&0 D* D&2 D≤ D&1 D∧ D&1 D< (D&0 D+ D.1) D* D&2, dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ ext this $ (vector_all_iff_forall _).1 $ λ z x y, show y = 0 ∧ z = 0 ∨ z * y ≤ x ∧ x < (z + 1) * y ↔ x / y = z, by refine iff.trans _ eq_comm; exact y.eq_zero_or_pos.elim (λ y0, by rw [y0, nat.div_zero]; exact ⟨λ o, (o.resolve_right $ λ ⟨_, h2⟩, nat.not_lt_zero _ h2).right, λ z0, or.inl ⟨rfl, z0⟩⟩) (λ ypos, iff.trans ⟨λ o, o.resolve_left $ λ ⟨h1, _⟩, ne_of_gt ypos h1, or.inr⟩ (le_antisymm_iff.trans $ and_congr (nat.le_div_iff_mul_le ypos) $ iff.trans ⟨lt_succ_of_le, le_of_lt_succ⟩ (div_lt_iff_lt_mul ypos)).symm) localized "infix (name := div_dioph) ` D/ `:80 := dioph.div_dioph" in dioph omit df dg open pell lemma pell_dioph : dioph (λ v:vector3 ℕ 4, ∃ h : 1 < v &0, xn h (v &1) = v &2 ∧ yn h (v &1) = v &3) := have dioph {v : vector3 ℕ 4 | 1 < v &0 ∧ v &1 ≤ v &3 ∧ (v &2 = 1 ∧ v &3 = 0 ∨ ∃ (u w s t b : ℕ), v &2 * v &2 - (v &0 * v &0 - 1) * v &3 * v &3 = 1 ∧ u * u - (v &0 * v &0 - 1) * w * w = 1 ∧ s * s - (b * b - 1) * t * t = 1 ∧ 1 < b ∧ (b ≡ 1 [MOD 4 * v &3]) ∧ (b ≡ v &0 [MOD u]) ∧ 0 < w ∧ v &3 * v &3 ∣ w ∧ (s ≡ v &2 [MOD u]) ∧ (t ≡ v &1 [MOD 4 * v &3]))}, from D.1 D< D&0 D∧ D&1 D≤ D&3 D∧ ((D&2 D= D.1 D∧ D&3 D= D.0) D∨ (D∃4 $ D∃5 $ D∃6 $ D∃7 $ D∃8 $ D&7 D* D&7 D- (D&5 D* D&5 D- D.1) D* D&8 D* D&8 D= D.1 D∧ D&4 D* D&4 D- (D&5 D* D&5 D- D.1) D* D&3 D* D&3 D= D.1 D∧ D&2 D* D&2 D- (D&0 D* D&0 D- D.1) D* D&1 D* D&1 D= D.1 D∧ D.1 D< D&0 D∧ (D≡ (D&0) (D.1) (D.4 D* D&8)) D∧ (D≡ (D&0) (D&5) D&4) D∧ D.0 D< D&3 D∧ D&8 D* D&8 D∣ D&3 D∧ (D≡ (D&2) (D&7) D&4) D∧ (D≡ (D&1) (D&6) (D.4 D* D&8)))), dioph.ext this $ λ v, matiyasevic.symm lemma xn_dioph : dioph_pfun (λ v:vector3 ℕ 2, ⟨1 < v &0, λ h, xn h (v &1)⟩) := have dioph (λ v:vector3 ℕ 3, ∃ y, ∃ h : 1 < v &1, xn h (v &2) = v &0 ∧ yn h (v &2) = y), from let D_pell := pell_dioph.reindex_dioph (fin2 4) [&2, &3, &1, &0] in D∃3 D_pell, (dioph_pfun_vec _).2 $ dioph.ext this $ λ v, ⟨λ ⟨y, h, xe, ye⟩, ⟨h, xe⟩, λ ⟨h, xe⟩, ⟨_, h, xe, rfl⟩⟩ include df dg /-- A version of **Matiyasevic's theorem** -/ theorem pow_dioph : dioph_fn (λ v, f v ^ g v) := have dioph {v : vector3 ℕ 3 | v &2 = 0 ∧ v &0 = 1 ∨ 0 < v &2 ∧ (v &1 = 0 ∧ v &0 = 0 ∨ 0 < v &1 ∧ ∃ (w a t z x y : ℕ), (∃ (a1 : 1 < a), xn a1 (v &2) = x ∧ yn a1 (v &2) = y) ∧ (x ≡ y * (a - v &1) + v &0 [MOD t]) ∧ 2 * a * v &1 = t + (v &1 * v &1 + 1) ∧ v &0 < t ∧ v &1 ≤ w ∧ v &2 ≤ w ∧ a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)}, from let D_pell := pell_dioph.reindex_dioph (fin2 9) [&4, &8, &1, &0] in (D&2 D= D.0 D∧ D&0 D= D.1) D∨ (D.0 D< D&2 D∧ ((D&1 D= D.0 D∧ D&0 D= D.0) D∨ (D.0 D< D&1 D∧ (D∃3 $ D∃4 $ D∃5 $ D∃6 $ D∃7 $ D∃8 $ D_pell D∧ (D≡ (D&1) (D&0 D* (D&4 D- D&7) D+ D&6) (D&3)) D∧ D.2 D* D&4 D* D&7 D= D&3 D+ (D&7 D* D&7 D+ D.1) D∧ D&6 D< D&3 D∧ D&7 D≤ D&5 D∧ D&8 D≤ D&5 D∧ D&4 D* D&4 D- ((D&5 D+ D.1) D* (D&5 D+ D.1) D- D.1) D* (D&5 D* D&2) D* (D&5 D* D&2) D= D.1)))), dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ dioph.ext this $ λ v, iff.symm $ eq_pow_of_pell.trans $ or_congr iff.rfl $ and_congr iff.rfl $ or_congr iff.rfl $ and_congr iff.rfl $ ⟨λ ⟨w, a, t, z, a1, h⟩, ⟨w, a, t, z, _, _, ⟨a1, rfl, rfl⟩, h⟩, λ ⟨w, a, t, z, ._, ._, ⟨a1, rfl, rfl⟩, h⟩, ⟨w, a, t, z, a1, h⟩⟩ end end dioph
dfe933ec93608629e4e8ff520a349f010e22f925
26ac254ecb57ffcb886ff709cf018390161a9225
/src/topology/uniform_space/absolute_value.lean
ca4bf19d973fe370cb5dee8bb5dc13652f9ba96e
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
2,981
lean
/- Copyright (c) 2019 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import data.real.cau_seq import topology.uniform_space.basic /-! # Uniform structure induced by an absolute value We build a uniform space structure on a commutative ring `R` equipped with an absolute value into a linear ordered field `𝕜`. Of course in the case `R` is `ℚ`, `ℝ` or `ℂ` and `𝕜 = ℝ`, we get the same thing as the metric space construction, and the general construction follows exactly the same path. ## Implementation details Note that we import `data.real.cau_seq` because this is where absolute values are defined, but the current file does not depend on real numbers. TODO: extract absolute values from that `data.real` folder. ## References * [N. Bourbaki, *Topologie générale*][bourbaki1966] ## Tags absolute value, uniform spaces -/ open set function filter uniform_space open_locale filter namespace is_absolute_value variables {𝕜 : Type*} [discrete_linear_ordered_field 𝕜] variables {R : Type*} [comm_ring R] (abv : R → 𝕜) [is_absolute_value abv] /-- The uniformity coming from an absolute value. -/ def uniform_space_core : uniform_space.core R := { uniformity := (⨅ ε>0, 𝓟 {p:R×R | abv (p.2 - p.1) < ε}), refl := le_infi $ assume ε, le_infi $ assume ε_pos, principal_mono.2 (λ ⟨x, y⟩ h, by simpa [show x = y, from h, abv_zero abv]), symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h, tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ λ ⟨x, y⟩ h, have h : abv (y - x) < ε, by simpa [-sub_eq_add_neg] using h, by rwa abv_sub abv at h, comp := le_infi $ assume ε, le_infi $ assume h, lift'_le (mem_infi_sets (ε / 2) $ mem_infi_sets (div_pos_of_pos_of_pos h two_pos) (subset.refl _)) $ have ∀ (a b c : R), abv (c-a) < ε / 2 → abv (b-c) < ε / 2 → abv (b-a) < ε, from assume a b c hac hcb, calc abv (b - a) ≤ _ : abv_sub_le abv b c a ... = abv (c - a) + abv (b - c) : add_comm _ _ ... < ε / 2 + ε / 2 : add_lt_add hac hcb ... = ε : by rw [div_add_div_same, add_self_div_two], by simpa [comp_rel] } /-- The uniform structure coming from an absolute value. -/ def uniform_space : uniform_space R := uniform_space.of_core (uniform_space_core abv) @[nolint ge_or_gt] -- see Note [nolint_ge] theorem mem_uniformity {s : set (R×R)} : s ∈ (uniform_space_core abv).uniformity ↔ (∃ε>0, ∀{a b:R}, abv (b - a) < ε → (a, b) ∈ s) := begin suffices : s ∈ (⨅ ε: {ε : 𝕜 // ε > 0}, 𝓟 {p:R×R | abv (p.2 - p.1) < ε.val}) ↔ _, { rw infi_subtype at this, exact this }, rw mem_infi, { simp [subset_def] }, { exact assume ⟨r, hr⟩ ⟨p, hp⟩, ⟨⟨min r p, lt_min hr hp⟩, by simp [lt_min_iff, (≥)] {contextual := tt}⟩, }, { exact ⟨⟨1, zero_lt_one⟩⟩ } end end is_absolute_value
3a00c61732d34e9b7a95c0a997835bae87f77851
f1b175e38ffc5cc1c7c5551a72d0dbaf70786f83
/group_theory/order_of_element.lean
c729b6f107bd8e06ff49a42bb0ffd5d06f8849f1
[ "Apache-2.0" ]
permissive
mjendrusch/mathlib
df3ae884dd5ce38c7edf452bcbfd3baf4e3a6214
5c209edb7eb616a26f64efe3500f2b1ba95b8d55
refs/heads/master
1,585,663,284,800
1,539,062,055,000
1,539,062,055,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,846
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import data.set.finite group_theory.coset open set function variables {α : Type*} {s : set α} {a a₁ a₂ b c: α} -- TODO this lemma isn't used anywhere in this file, and should be moved elsewhere. namespace finset open finset lemma mem_range_iff_mem_finset_range_of_mod_eq [decidable_eq α] {f : ℤ → α} {a : α} {n : ℕ} (hn : 0 < n) (h : ∀i, f (i % n) = f i) : a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) := suffices (∃i, f (i % n) = a) ↔ ∃i, i < n ∧ f ↑i = a, by simpa [h], have hn' : 0 < (n : ℤ), from int.coe_nat_lt.mpr hn, iff.intro (assume ⟨i, hi⟩, have 0 ≤ i % ↑n, from int.mod_nonneg _ (ne_of_gt hn'), ⟨int.to_nat (i % n), by rw [←int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos i hn', hi⟩⟩) (assume ⟨i, hi, ha⟩, ⟨i, by rw [int.mod_eq_of_lt (int.coe_zero_le _) (int.coe_nat_lt_coe_nat_of_lt hi), ha]⟩) end finset section order_of variables [group α] [fintype α] [decidable_eq α] lemma exists_gpow_eq_one (a : α) : ∃i≠0, a ^ (i:ℤ) = 1 := have ¬ injective (λi, a ^ i), from not_injective_int_fintype, let ⟨i, j, a_eq, ne⟩ := show ∃(i j : ℤ), a ^ i = a ^ j ∧ i ≠ j, by rw [injective] at this; simpa [classical.not_forall] in have a ^ (i - j) = 1, by simp [gpow_add, gpow_neg, a_eq], ⟨i - j, sub_ne_zero.mpr ne, this⟩ lemma exists_pow_eq_one (a : α) : ∃i > 0, a ^ i = 1 := let ⟨i, hi, eq⟩ := exists_gpow_eq_one a in begin cases i, { exact ⟨i, nat.pos_of_ne_zero (by simp [int.of_nat_eq_coe, *] at *), eq⟩ }, { exact ⟨i + 1, dec_trivial, inv_eq_one.1 eq⟩ } end /-- `order_of a` is the order of the element `a`, i.e. the `n ≥ 1`, s.t. `a ^ n = 1` -/ def order_of (a : α) : ℕ := nat.find (exists_pow_eq_one a) lemma pow_order_of_eq_one (a : α) : a ^ order_of a = 1 := let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₂ lemma order_of_pos (a : α) : order_of a > 0 := let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₁ private lemma pow_injective_aux {n m : ℕ} (a : α) (h : n ≤ m) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := decidable.by_contradiction $ assume ne : n ≠ m, have h₁ : m - n > 0, from nat.pos_of_ne_zero (by simp [nat.sub_eq_iff_eq_add h, ne.symm]), have h₂ : a ^ (m - n) = 1, by simp [pow_sub _ h, eq], have le : order_of a ≤ m - n, from nat.find_min' (exists_pow_eq_one a) ⟨h₁, h₂⟩, have lt : m - n < order_of a, from (nat.sub_lt_left_iff_lt_add h).mpr $ nat.lt_add_left _ _ _ hm, lt_irrefl _ (lt_of_le_of_lt le lt) lemma pow_injective_of_lt_order_of {n m : ℕ} (a : α) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := (le_total n m).elim (assume h, pow_injective_aux a h hn hm eq) (assume h, (pow_injective_aux a h hm hn eq.symm).symm) lemma order_of_le_card_univ : order_of a ≤ fintype.card α := finset.card_le_of_inj_on ((^) a) (assume n _, fintype.complete _) (assume i j, pow_injective_of_lt_order_of a) lemma pow_eq_mod_order_of {n : ℕ} : a ^ n = a ^ (n % order_of a) := calc a ^ n = a ^ (n % order_of a + order_of a * (n / order_of a)) : by rw [nat.mod_add_div] ... = a ^ (n % order_of a) : by simp [pow_add, pow_mul, pow_order_of_eq_one] lemma gpow_eq_mod_order_of {i : ℤ} : a ^ i = a ^ (i % order_of a) := calc a ^ i = a ^ (i % order_of a + order_of a * (i / order_of a)) : by rw [int.mod_add_div] ... = a ^ (i % order_of a) : by simp [gpow_add, gpow_mul, pow_order_of_eq_one] lemma mem_gpowers_iff_mem_range_order_of {a a' : α} : a' ∈ gpowers a ↔ a' ∈ (finset.range (order_of a)).image ((^) a : ℕ → α) := finset.mem_range_iff_mem_finset_range_of_mod_eq (order_of_pos a) (assume i, gpow_eq_mod_order_of.symm) instance decidable_gpowers : decidable_pred (gpowers a) := assume a', decidable_of_iff' (a' ∈ (finset.range (order_of a)).image ((^) a)) mem_gpowers_iff_mem_range_order_of lemma order_of_dvd_of_pow_eq_one {n : ℕ} (h : a ^ n = 1) : order_of a ∣ n := by_contradiction (λ h₁, nat.find_min _ (show n % order_of a < order_of a, from nat.mod_lt _ (order_of_pos _)) ⟨nat.pos_of_ne_zero (mt nat.dvd_of_mod_eq_zero h₁), by rwa ← pow_eq_mod_order_of⟩) lemma order_of_le_of_pow_eq_one {n : ℕ} (hn : 0 < n) (h : a ^ n = 1) : order_of a ≤ n := nat.find_min' (exists_pow_eq_one a) ⟨hn, h⟩ lemma sum_card_order_of_eq_card_pow_eq_one {n : ℕ} (hn : 0 < n) : ((finset.range n.succ).filter (∣ n)).sum (λ m, (finset.univ.filter (λ a : α, order_of a = m)).card) = (finset.univ.filter (λ a : α, a ^ n = 1)).card := calc ((finset.range n.succ).filter (∣ n)).sum (λ m, (finset.univ.filter (λ a : α, order_of a = m)).card) = _ : (finset.card_bind (by simp [finset.ext]; cc)).symm ... = _ : congr_arg finset.card (finset.ext.2 (begin assume a, suffices : order_of a ≤ n ∧ order_of a ∣ n ↔ a ^ n = 1, { simpa [-finset.range_succ, nat.lt_succ_iff], }, exact ⟨λ h, let ⟨m, hm⟩ := h.2 in by rw [hm, pow_mul, pow_order_of_eq_one, _root_.one_pow], λ h, ⟨order_of_le_of_pow_eq_one hn h, order_of_dvd_of_pow_eq_one h⟩⟩ end)) section local attribute [instance] set_fintype lemma order_eq_card_gpowers : order_of a = fintype.card (gpowers a) := begin refine (finset.card_eq_of_bijective _ _ _ _).symm, { exact λn hn, ⟨gpow a n, ⟨n, rfl⟩⟩ }, { exact assume ⟨_, i, rfl⟩ _, have pos: (0:int) < order_of a, from int.coe_nat_lt.mpr $ order_of_pos a, have 0 ≤ i % (order_of a), from int.mod_nonneg _ $ ne_of_gt pos, ⟨int.to_nat (i % order_of a), by rw [← int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos _ pos, subtype.eq gpow_eq_mod_order_of.symm⟩⟩ }, { intros, exact finset.mem_univ _ }, { exact assume i j hi hj eq, pow_injective_of_lt_order_of a hi hj $ by simpa using eq } end section classical local attribute [instance] classical.prop_decidable open quotient_group /- TODO: use cardinal theory, introduce `card : set α → ℕ`, or setup decidability for cosets -/ lemma order_of_dvd_card_univ : order_of a ∣ fintype.card α := have ft_prod : fintype (quotient (gpowers a) × (gpowers a)), from fintype.of_equiv α (gpowers.is_subgroup a).group_equiv_quotient_times_subgroup, have ft_s : fintype (gpowers a), from @fintype.fintype_prod_right _ _ _ ft_prod _, have ft_cosets : fintype (quotient (gpowers a)), from @fintype.fintype_prod_left _ _ _ ft_prod ⟨⟨1, is_submonoid.one_mem (gpowers a)⟩⟩, have ft : fintype (quotient (gpowers a) × (gpowers a)), from @prod.fintype _ _ ft_cosets ft_s, have eq₁ : fintype.card α = @fintype.card _ ft_cosets * @fintype.card _ ft_s, from calc fintype.card α = @fintype.card _ ft_prod : @fintype.card_congr _ _ _ ft_prod (gpowers.is_subgroup a).group_equiv_quotient_times_subgroup ... = @fintype.card _ (@prod.fintype _ _ ft_cosets ft_s) : congr_arg (@fintype.card _) $ subsingleton.elim _ _ ... = @fintype.card _ ft_cosets * @fintype.card _ ft_s : @fintype.card_prod _ _ ft_cosets ft_s, have eq₂ : order_of a = @fintype.card _ ft_s, from calc order_of a = _ : order_eq_card_gpowers ... = _ : congr_arg (@fintype.card _) $ subsingleton.elim _ _, dvd.intro (@fintype.card (quotient (gpowers a)) ft_cosets) $ by rw [eq₁, eq₂, mul_comm] end classical @[simp] lemma pow_card_eq_one (a : α) : a ^ fintype.card α = 1 := let ⟨m, hm⟩ := @order_of_dvd_card_univ _ a _ _ _ in by simp [hm, pow_mul, pow_order_of_eq_one] lemma powers_eq_gpowers (a : α) : powers a = gpowers a := set.ext (λ x, ⟨λ ⟨n, hn⟩, ⟨n, by simp * at *⟩, λ ⟨i, hi⟩, ⟨(i % order_of a).nat_abs, by rwa [← gpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← gpow_eq_mod_order_of]⟩⟩) end section cyclic local attribute [instance] set_fintype class is_cyclic (α : Type*) [group α] : Prop := (exists_generator : ∃ g : α, ∀ x, x ∈ gpowers g) lemma is_cyclic_of_order_of_eq_card [group α] [fintype α] [decidable_eq α] (x : α) (hx : order_of x = fintype.card α) : is_cyclic α := ⟨⟨x, set.eq_univ_iff_forall.1 $ set.eq_of_subset_of_card_le (set.subset_univ _) (by rw [fintype.card_congr (equiv.set.univ α), ← hx, order_eq_card_gpowers])⟩⟩ lemma order_of_eq_card_of_forall_mem_gppowers [group α] [fintype α] [decidable_eq α] {g : α} (hx : ∀ x, x ∈ gpowers g) : order_of g = fintype.card α := by rw [← fintype.card_congr (equiv.set.univ α), order_eq_card_gpowers]; simp [hx]; congr end cyclic end order_of
56f29b42ac98bd2890766f6fd3ed408034ef38d7
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/combinatorics/hall/finite.lean
888995534e72d7939bd29cae29d6751483cebd2c
[ "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
11,619
lean
/- Copyright (c) 2021 Alena Gusakov, Bhavik Mehta, Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alena Gusakov, Bhavik Mehta, Kyle Miller -/ import data.fintype.basic import data.set.finite /-! # Hall's Marriage Theorem for finite index types This module proves the basic form of Hall's theorem. In constrast to the theorem described in `combinatorics.hall.basic`, this version requires that the indexed family `t : ι → finset α` have `ι` be a `fintype`. The `combinatorics.hall.basic` module applies a compactness argument to this version to remove the `fintype` constraint on `ι`. The modules are split like this since the generalized statement depends on the topology and category theory libraries, but the finite case in this module has few dependencies. A description of this formalization is in [Gusakov2021]. ## Main statements * `finset.all_card_le_bUnion_card_iff_exists_injective'` is Hall's theorem with a finite index set. This is elsewhere generalized to `finset.all_card_le_bUnion_card_iff_exists_injective`. ## Tags Hall's Marriage Theorem, indexed families -/ open finset universes u v namespace hall_marriage_theorem variables {ι : Type u} {α : Type v} [fintype ι] theorem hall_hard_inductive_zero (t : ι → finset α) (hn : fintype.card ι = 0) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin rw fintype.card_eq_zero_iff at hn, exactI ⟨is_empty_elim, is_empty_elim, is_empty_elim⟩, end variables {t : ι → finset α} [decidable_eq α] lemma hall_cond_of_erase {x : ι} (a : α) (ha : ∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card) (s' : finset {x' : ι | x' ≠ x}) : s'.card ≤ (s'.bUnion (λ x', (t x').erase a)).card := begin haveI := classical.dec_eq ι, specialize ha (s'.image coe), rw [nonempty.image_iff, finset.card_image_of_injective s' subtype.coe_injective] at ha, by_cases he : s'.nonempty, { have ha' : s'.card < (s'.bUnion (λ x, t x)).card, { specialize ha he (λ h, by { have h' := mem_univ x, rw ←h at h', simpa using h' }), convert ha using 2, ext x, simp only [mem_image, mem_bUnion, exists_prop, set_coe.exists, exists_and_distrib_right, exists_eq_right, subtype.coe_mk], }, rw ←erase_bUnion, by_cases hb : a ∈ s'.bUnion (λ x, t x), { rw card_erase_of_mem hb, exact nat.le_pred_of_lt ha' }, { rw erase_eq_of_not_mem hb, exact nat.le_of_lt ha' }, }, { rw [nonempty_iff_ne_empty, not_not] at he, subst s', simp }, end /-- First case of the inductive step: assuming that `∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card` and that the statement of **Hall's Marriage Theorem** is true for all `ι'` of cardinality ≤ `n`, then it is true for `ι` of cardinality `n + 1`. -/ lemma hall_hard_inductive_step_A {n : ℕ} (hn : fintype.card ι = n + 1) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (ih : ∀ {ι' : Type u} [fintype ι'] (t' : ι' → finset α), by exactI fintype.card ι' ≤ n → (∀ (s' : finset ι'), s'.card ≤ (s'.bUnion t').card) → ∃ (f : ι' → α), function.injective f ∧ ∀ x, f x ∈ t' x) (ha : ∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin haveI : nonempty ι := fintype.card_pos_iff.mp (hn.symm ▸ nat.succ_pos _), haveI := classical.dec_eq ι, /- Choose an arbitrary element `x : ι` and `y : t x`. -/ let x := classical.arbitrary ι, have tx_ne : (t x).nonempty, { rw ←finset.card_pos, apply nat.lt_of_lt_of_le nat.one_pos, convert ht {x}, rw finset.singleton_bUnion, }, rcases classical.indefinite_description _ tx_ne with ⟨y, hy⟩, /- Restrict to everything except `x` and `y`. -/ let ι' := {x' : ι | x' ≠ x}, let t' : ι' → finset α := λ x', (t x').erase y, have card_ι' : fintype.card ι' = n, { convert congr_arg (λ m, m - 1) hn, convert set.card_ne_eq _, }, rcases ih t' card_ι'.le (hall_cond_of_erase y ha) with ⟨f', hfinj, hfr⟩, /- Extend the resulting function. -/ refine ⟨λ z, if h : z = x then y else f' ⟨z, h⟩, _, _⟩, { rintro z₁ z₂, have key : ∀ {x}, y ≠ f' x, { intros x h, specialize hfr x, rw ←h at hfr, simpa using hfr, }, by_cases h₁ : z₁ = x; by_cases h₂ : z₂ = x; simp [h₁, h₂, hfinj.eq_iff, key, key.symm], }, { intro z, split_ifs with hz, { rwa hz }, { specialize hfr ⟨z, hz⟩, rw mem_erase at hfr, exact hfr.2, }, }, end lemma hall_cond_of_restrict {ι : Type u} {t : ι → finset α} {s : finset ι} (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (s' : finset (s : set ι)) : s'.card ≤ (s'.bUnion (λ a', t a')).card := begin haveI := classical.dec_eq ι, convert ht (s'.image coe) using 1, { rw card_image_of_injective _ subtype.coe_injective, }, { apply congr_arg, ext y, simp, }, end lemma hall_cond_of_compl {ι : Type u} {t : ι → finset α} {s : finset ι} (hus : s.card = (s.bUnion t).card) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (s' : finset (sᶜ : set ι)) : s'.card ≤ (s'.bUnion (λ x', t x' \ s.bUnion t)).card := begin haveI := classical.dec_eq ι, have : s'.card = (s ∪ s'.image coe).card - s.card, { rw [card_disjoint_union, add_tsub_cancel_left, card_image_of_injective _ subtype.coe_injective], simp only [disjoint_left, not_exists, mem_image, exists_prop, set_coe.exists, exists_and_distrib_right, exists_eq_right, subtype.coe_mk], intros x hx hc h, exact (hc hx).elim }, rw [this, hus], apply (tsub_le_tsub_right (ht _) _).trans _, rw ← card_sdiff, { have : (s ∪ s'.image subtype.val).bUnion t \ s.bUnion t ⊆ s'.bUnion (λ x', t x' \ s.bUnion t), { intros t, simp only [mem_bUnion, mem_sdiff, not_exists, mem_image, and_imp, mem_union, exists_and_distrib_right, exists_imp_distrib], rintro x (hx | ⟨x', hx', rfl⟩) rat hs, { exact (hs x hx rat).elim }, { exact ⟨⟨x', hx', rat⟩, hs⟩, } }, exact (card_le_of_subset this).trans le_rfl, }, { apply bUnion_subset_bUnion_of_subset_left, apply subset_union_left } end /-- Second case of the inductive step: assuming that `∃ (s : finset ι), s ≠ univ → s.card = (s.bUnion t).card` and that the statement of Hall's Marriage Theorem is true for all `ι'` of cardinality ≤ `n`, then it is true for `ι` of cardinality `n + 1`. -/ lemma hall_hard_inductive_step_B {n : ℕ} (hn : fintype.card ι = n + 1) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (ih : ∀ {ι' : Type u} [fintype ι'] (t' : ι' → finset α), by exactI fintype.card ι' ≤ n → (∀ (s' : finset ι'), s'.card ≤ (s'.bUnion t').card) → ∃ (f : ι' → α), function.injective f ∧ ∀ x, f x ∈ t' x) (s : finset ι) (hs : s.nonempty) (hns : s ≠ univ) (hus : s.card = (s.bUnion t).card) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin haveI := classical.dec_eq ι, /- Restrict to `s` -/ let t' : s → finset α := λ x', t x', rw nat.add_one at hn, have card_ι'_le : fintype.card s ≤ n, { apply nat.le_of_lt_succ, rw ←hn, convert (card_lt_iff_ne_univ _).mpr hns, convert fintype.card_coe _ }, rcases ih t' card_ι'_le (hall_cond_of_restrict ht) with ⟨f', hf', hsf'⟩, /- Restrict to `sᶜ` in the domain and `(s.bUnion t)ᶜ` in the codomain. -/ set ι'' := (s : set ι)ᶜ with ι''_def, let t'' : ι'' → finset α := λ a'', t a'' \ s.bUnion t, have card_ι''_le : fintype.card ι'' ≤ n, { simp_rw [← nat.lt_succ_iff, ← hn, ι'', ← finset.coe_compl, coe_sort_coe], rw [fintype.card_congr' rfl, fintype.card_coe], rwa card_compl_lt_iff_nonempty }, rcases ih t'' card_ι''_le (hall_cond_of_compl hus ht) with ⟨f'', hf'', hsf''⟩, /- Put them together -/ have f'_mem_bUnion : ∀ {x'} (hx' : x' ∈ s), f' ⟨x', hx'⟩ ∈ s.bUnion t, { intros x' hx', rw mem_bUnion, exact ⟨x', hx', hsf' _⟩, }, have f''_not_mem_bUnion : ∀ {x''} (hx'' : ¬ x'' ∈ s), ¬ f'' ⟨x'', hx''⟩ ∈ s.bUnion t, { intros x'' hx'', have h := hsf'' ⟨x'', hx''⟩, rw mem_sdiff at h, exact h.2, }, have im_disj : ∀ {x' x'' : ι} {hx' : x' ∈ s} {hx'' : ¬x'' ∈ s}, f' ⟨x', hx'⟩ ≠ f'' ⟨x'', hx''⟩, { intros _ _ hx' hx'' h, apply f''_not_mem_bUnion hx'', rw ←h, apply f'_mem_bUnion, }, refine ⟨λ x, if h : x ∈ s then f' ⟨x, h⟩ else f'' ⟨x, h⟩, _, _⟩, { exact hf'.dite _ hf'' @im_disj }, { intro x, split_ifs, { exact hsf' ⟨x, h⟩ }, { exact sdiff_subset _ _ (hsf'' ⟨x, h⟩) } } end /-- If `ι` has cardinality `n + 1` and the statement of Hall's Marriage Theorem is true for all `ι'` of cardinality ≤ `n`, then it is true for `ι`. -/ theorem hall_hard_inductive_step {n : ℕ} (hn : fintype.card ι = n + 1) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (ih : ∀ {ι' : Type u} [fintype ι'] (t' : ι' → finset α), by exactI fintype.card ι' ≤ n → (∀ (s' : finset ι'), s'.card ≤ (s'.bUnion t').card) → ∃ (f : ι' → α), function.injective f ∧ ∀ x, f x ∈ t' x) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin by_cases h : ∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card, { exact hall_hard_inductive_step_A hn ht @ih h, }, { push_neg at h, rcases h with ⟨s, sne, snu, sle⟩, have seq := nat.le_antisymm (ht _) sle, exact hall_hard_inductive_step_B hn ht @ih s sne snu seq, }, end /-- Here we combine the base case and the inductive step into a full strong induction proof, thus completing the proof of the second direction. -/ theorem hall_hard_inductive {n : ℕ} (hn : fintype.card ι = n) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin unfreezingI { revert ι }, refine nat.strong_induction_on n (λ n' ih, _), intros _ _ t hn ht, rcases n' with (_|_), { exactI hall_hard_inductive_zero t hn }, { resetI, apply hall_hard_inductive_step hn ht, introsI ι' _ _ hι', exact ih (fintype.card ι') (nat.lt_succ_of_le hι') rfl, }, end end hall_marriage_theorem /-- This is the version of **Hall's Marriage Theorem** in terms of indexed families of finite sets `t : ι → finset α` with `ι` a `fintype`. It states that there is a set of distinct representatives if and only if every union of `k` of the sets has at least `k` elements. See `finset.all_card_le_bUnion_card_iff_exists_injective` for a version where the `fintype ι` constraint is removed. -/ theorem finset.all_card_le_bUnion_card_iff_exists_injective' {ι α : Type*} [fintype ι] [decidable_eq α] (t : ι → finset α) : (∀ (s : finset ι), s.card ≤ (s.bUnion t).card) ↔ (∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x) := begin split, { exact hall_marriage_theorem.hall_hard_inductive rfl }, { rintro ⟨f, hf₁, hf₂⟩ s, rw ←card_image_of_injective s hf₁, apply card_le_of_subset, intro _, rw [mem_image, mem_bUnion], rintros ⟨x, hx, rfl⟩, exact ⟨x, hx, hf₂ x⟩, }, end
2c7351abd12a44341e2e112a0eadbe02dc1b5f82
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/algebra/module/determinant.lean
e57201fa2bb0c0191e5bcd8414169f3a187fd02f
[ "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
1,078
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Sébastien Gouëzel, Jean Lo, Yury Kudryashov, Frédéric Dupuis, Heather Macbeth -/ import topology.algebra.module.basic import linear_algebra.determinant /-! # The determinant of a continuous linear map. -/ namespace continuous_linear_map /-- The determinant of a continuous linear map, mainly as a convenience device to be able to write `A.det` instead of `(A : M →ₗ[R] M).det`. -/ @[reducible] noncomputable def det {R : Type*} [comm_ring R] {M : Type*} [topological_space M] [add_comm_group M] [module R M] (A : M →L[R] M) : R := linear_map.det (A : M →ₗ[R] M) end continuous_linear_map namespace continuous_linear_equiv @[simp] lemma det_coe_symm {R : Type*} [field R] {M : Type*} [topological_space M] [add_comm_group M] [module R M] (A : M ≃L[R] M) : (A.symm : M →L[R] M).det = (A : M →L[R] M).det ⁻¹ := linear_equiv.det_coe_symm A.to_linear_equiv end continuous_linear_equiv
7c0b9da7be159e9aaab5bfe3b71fff9fb1400c3c
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/category_theory/abelian/basic.lean
26d77b57664979c6b6b9b2b83b853b5258839b6f
[ "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
21,659
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.limits.shapes.constructions.pullbacks import category_theory.limits.shapes.regular_mono import category_theory.limits.shapes.biproducts import category_theory.limits.shapes.images 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 preadditive structure the specific category they are working with has natively. ## 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 instantiated this in such a way that `abelian.image f` is definitionally equal to `limits.image f`, and * there is a canonical isomorphism `coimage_iso_image : coimage f ≅ image f` such that `coimage.π f ≫ (coimage_iso_image f).hom ≫ image.ι f = f`. The lemma stating this is called `full_image_factorisation`. * 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: Chaper 0*][aluffi2016] -/ 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) section prio set_option default_priority 100 /-- 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 := [has_finite_products : has_finite_products C] [has_kernels : has_kernels C] [has_cokernels : has_cokernels C] (normal_mono : Π {X Y : C} (f : X ⟶ Y) [mono f], normal_mono f) (normal_epi : Π {X Y : C} (f : X ⟶ Y) [epi f], normal_epi f) attribute [instance] abelian.has_finite_products attribute [instance] abelian.has_kernels abelian.has_cokernels end prio end category_theory open category_theory namespace category_theory.abelian variables {C : Type u} [category.{v} C] [abelian C] /-- An abelian category has finite biproducts. -/ def has_finite_biproducts : has_finite_biproducts C := limits.has_finite_biproducts.of_has_finite_products section to_non_preadditive_abelian local attribute [instance] has_finite_biproducts @[priority 100] instance non_preadditive_abelian : non_preadditive_abelian C := { ..‹abelian C› } end to_non_preadditive_abelian section strong local attribute [instance] abelian.normal_epi /-- In an abelian category, every epimorphism is strong. -/ def strong_epi_of_epi {P Q : C} (f : P ⟶ Q) [epi f] : strong_epi f := by apply_instance end strong section mono_epi_iso variables {X Y : C} (f : X ⟶ Y) local attribute [instance] strong_epi_of_epi /-- In an abelian category, a monomorphism which is also an epimorphism is an isomorphism. -/ def is_iso_of_mono_of_epi [mono f] [epi f] : is_iso f := is_iso_of_mono_of_strong_epi _ end mono_epi_iso section factor variables {P Q : C} (f : P ⟶ Q) /-- The kernel of the cokernel of `f` is called the image of `f`. -/ protected abbreviation image : C := kernel (cokernel.π f) /-- The inclusion of the image into the codomain. -/ protected abbreviation image.ι : abelian.image f ⟶ Q := kernel.ι (cokernel.π f) /-- There is a canonical epimorphism `p : P ⟶ image f` for every `f`. -/ protected abbreviation factor_thru_image : P ⟶ abelian.image f := kernel.lift (cokernel.π f) f $ cokernel.condition f /-- `f` factors through its image via the canonical morphism `p`. -/ @[simp, reassoc] protected lemma image.fac : abelian.factor_thru_image f ≫ image.ι f = f := kernel.lift_ι _ _ _ /-- The map `p : P ⟶ image f` is an epimorphism -/ instance : epi (abelian.factor_thru_image f) := show epi (non_preadditive_abelian.factor_thru_image f), by apply_instance instance mono_factor_thru_image [mono f] : mono (abelian.factor_thru_image f) := mono_of_mono_fac $ image.fac f instance is_iso_factor_thru_image [mono f] : is_iso (abelian.factor_thru_image f) := is_iso_of_mono_of_epi _ /-- 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 _ } /-- The cokernel of the kernel of `f` is called the coimage of `f`. -/ protected abbreviation coimage : C := cokernel (kernel.ι f) /-- The projection onto the coimage. -/ protected abbreviation coimage.π : P ⟶ abelian.coimage f := cokernel.π (kernel.ι f) /-- There is a canonical monomorphism `i : coimage f ⟶ Q`. -/ protected abbreviation factor_thru_coimage : abelian.coimage f ⟶ Q := cokernel.desc (kernel.ι f) f $ kernel.condition f /-- `f` factors through its coimage via the canonical morphism `p`. -/ protected lemma coimage.fac : coimage.π f ≫ abelian.factor_thru_coimage f = f := cokernel.π_desc _ _ _ /-- The canonical morphism `i : coimage f ⟶ Q` is a monomorphism -/ instance : mono (abelian.factor_thru_coimage f) := show mono (non_preadditive_abelian.factor_thru_coimage f), by apply_instance instance epi_factor_thru_coimage [epi f] : epi (abelian.factor_thru_coimage f) := epi_of_epi_fac $ coimage.fac f instance is_iso_factor_thru_coimage [epi f] : is_iso (abelian.factor_thru_coimage f) := is_iso_of_mono_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 := ⟨λ 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) lemma image_eq_image : limits.image f = abelian.image f := rfl /-- There is a canonical isomorphism between the coimage and the image of a morphism. -/ abbreviation coimage_iso_image : abelian.coimage f ≅ abelian.image f := is_image.iso_ext (coimage_strong_epi_mono_factorisation f).to_mono_is_image (image_strong_epi_mono_factorisation f).to_mono_is_image lemma full_image_factorisation : coimage.π f ≫ (coimage_iso_image f).hom ≫ image.ι f = f := by rw [limits.is_image.iso_ext_hom, ←image_strong_epi_mono_factorisation_to_mono_factorisation_m, is_image.lift_fac, coimage_strong_epi_mono_factorisation_to_mono_factorisation_m, coimage.fac] end images section cokernel_of_kernel variables {X Y : C} {f : X ⟶ Y} /-- 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 end cokernel_of_kernel section local attribute [instance] preadditive.has_equalizers_of_has_kernels /-- Any abelian category has pullbacks -/ def has_pullbacks : has_pullbacks C := has_pullbacks_of_has_binary_products_of_has_equalizers C end section local attribute [instance] preadditive.has_coequalizers_of_has_cokernels local attribute [instance] has_binary_biproducts.of_has_binary_products /-- Any abelian category has pushouts -/ def has_pushouts : has_pushouts C := has_pushouts_of_has_binary_coproducts_of_has_coequalizers C end namespace pullback_to_biproduct_is_kernel variables [limits.has_pullbacks C] {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) local attribute [instance] has_binary_biproducts.of_has_binary_products /-! 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] local attribute [irreducible] has_limit_cospan_of_has_limit_pair_of_has_limit_parallel_pair /-- 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 [prod.lift_fst, pullback.lift_fst] }, { rw [prod.lift_snd, pullback.lift_snd] } end) (λ s m h, by ext; simp [fork.ι_eq_app_zero, ←h walking_parallel_pair.zero]) end pullback_to_biproduct_is_kernel namespace biproduct_to_pushout_is_cokernel variables [limits.has_pushouts C] {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) local attribute [instance] has_binary_biproducts.of_has_binary_products /-- 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, has_zero_morphisms.zero_comp]) (λ s, by ext; simp) (λ s m h, by ext; simp [cofork.π_eq_app_one, ←h walking_parallel_pair.one] ) end biproduct_to_pushout_is_cokernel section epi_pullback variables [limits.has_pullbacks C] {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) local attribute [instance] has_binary_biproducts.of_has_binary_products /-- 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 coprod.inl_desc ... = biprod.inl ≫ u : by rw [category.assoc, hd] ... = 0 : coprod.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 coprod.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 coprod.inr_desc ... = biprod.inr ≫ u : by rw [category.assoc, hd] ... = 0 : coprod.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 coprod.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 end epi_pullback section mono_pushout variables [limits.has_pushouts C] {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) local attribute [instance] has_binary_biproducts.of_has_binary_products 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 prod.lift_fst ... = u ≫ biprod.fst : by rw [←category.assoc, hd] ... = 0 : prod.lift_fst _ _, have : d = 0 := (cancel_mono f).1 (by simpa), calc e = u ≫ biprod.snd : by rw prod.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 : has_zero_morphisms.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 prod.lift_snd ... = u ≫ biprod.snd : by rw [←category.assoc, hd] ... = 0 : prod.lift_snd _ _, have : d = 0 := (cancel_mono (-g)).1 (by simpa), calc e = u ≫ biprod.fst : by rw prod.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 : has_zero_morphisms.zero_comp _ _ end 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 := infer_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 (infer_instance : limits.has_kernels C), has_cokernels := by convert (infer_instance : limits.has_cokernels C), normal_mono := by { introsI, convert normal_mono f }, normal_epi := by { introsI, convert normal_epi f }, ..non_preadditive_abelian.preadditive } end category_theory.non_preadditive_abelian
84b10799ebe15457f5e74ccb827702ae385f160b
94e33a31faa76775069b071adea97e86e218a8ee
/src/measure_theory/function/lp_order.lean
733d44f3a135bed6ee6c55bdd3fe6afa3de9631c
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
3,456
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import measure_theory.function.lp_space import analysis.normed_space.lattice_ordered_group /-! # Order related properties of Lp spaces ### Results - `Lp E p μ` is an `ordered_add_comm_group` when `E` is a `normed_lattice_add_comm_group`. ### TODO - move definitions of `Lp.pos_part` and `Lp.neg_part` to this file, and define them as `has_pos_part.pos` and `has_pos_part.neg` given by the lattice structure. -/ open topological_space measure_theory lattice_ordered_comm_group open_locale ennreal variables {α E : Type*} {m : measurable_space α} {μ : measure α} {p : ℝ≥0∞} namespace measure_theory namespace Lp section order variables [normed_lattice_add_comm_group E] lemma coe_fn_le (f g : Lp E p μ) : f ≤ᵐ[μ] g ↔ f ≤ g := by rw [← subtype.coe_le_coe, ← ae_eq_fun.coe_fn_le, ← coe_fn_coe_base, ← coe_fn_coe_base] lemma coe_fn_nonneg (f : Lp E p μ) : 0 ≤ᵐ[μ] f ↔ 0 ≤ f := begin rw ← coe_fn_le, have h0 := Lp.coe_fn_zero E p μ, split; intro h; filter_upwards [h, h0] with _ _ h2, { rwa h2, }, { rwa ← h2, }, end instance : covariant_class (Lp E p μ) (Lp E p μ) (+) (≤) := begin refine ⟨λ f g₁ g₂ hg₁₂, _⟩, rw ← coe_fn_le at hg₁₂ ⊢, filter_upwards [coe_fn_add f g₁, coe_fn_add f g₂, hg₁₂] with _ h1 h2 h3, rw [h1, h2, pi.add_apply, pi.add_apply], exact add_le_add le_rfl h3, end instance : ordered_add_comm_group (Lp E p μ) := { add_le_add_left := λ f g, add_le_add_left, ..subtype.partial_order _, ..add_subgroup.to_add_comm_group _} lemma _root_.measure_theory.mem_ℒp.sup {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : mem_ℒp (f ⊔ g) p μ := mem_ℒp.mono' (hf.norm.add hg.norm) (hf.1.sup hg.1) (filter.eventually_of_forall (λ x, norm_sup_le_add (f x) (g x))) lemma _root_.measure_theory.mem_ℒp.inf {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : mem_ℒp (f ⊓ g) p μ := mem_ℒp.mono' (hf.norm.add hg.norm) (hf.1.inf hg.1) (filter.eventually_of_forall (λ x, norm_inf_le_add (f x) (g x))) lemma _root_.measure_theory.mem_ℒp.abs {f : α → E} (hf : mem_ℒp f p μ) : mem_ℒp (|f|) p μ := hf.sup hf.neg instance : lattice (Lp E p μ) := subtype.lattice (λ f g hf hg, by { rw mem_Lp_iff_mem_ℒp at *, exact (mem_ℒp_congr_ae (ae_eq_fun.coe_fn_sup _ _)).mpr (hf.sup hg), }) (λ f g hf hg, by { rw mem_Lp_iff_mem_ℒp at *, exact (mem_ℒp_congr_ae (ae_eq_fun.coe_fn_inf _ _)).mpr (hf.inf hg),}) lemma coe_fn_sup (f g : Lp E p μ) : ⇑(f ⊔ g) =ᵐ[μ] ⇑f ⊔ ⇑g := ae_eq_fun.coe_fn_sup _ _ lemma coe_fn_inf (f g : Lp E p μ) : ⇑(f ⊓ g) =ᵐ[μ] ⇑f ⊓ ⇑g := ae_eq_fun.coe_fn_inf _ _ lemma coe_fn_abs (f : Lp E p μ) : ⇑|f| =ᵐ[μ] λ x, |f x| := ae_eq_fun.coe_fn_abs _ noncomputable instance [fact (1 ≤ p)] : normed_lattice_add_comm_group (Lp E p μ) := { add_le_add_left := λ f g, add_le_add_left, solid := λ f g hfg, begin rw ← coe_fn_le at hfg, simp_rw [Lp.norm_def, ennreal.to_real_le_to_real (Lp.snorm_ne_top f) (Lp.snorm_ne_top g)], refine snorm_mono_ae _, filter_upwards [hfg, Lp.coe_fn_abs f, Lp.coe_fn_abs g] with x hx hxf hxg, rw [hxf, hxg] at hx, exact solid hx, end, ..Lp.lattice, ..Lp.normed_group, } end order end Lp end measure_theory
fefb45845b59fc478487bca4abf674e60104b392
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/group/inj_surj.lean
d9c43e3513bddf68907298e72fccfcac34956358
[ "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
25,421
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.group.defs import logic.function.basic import data.int.cast.basic /-! # Lifting algebraic data classes along injective/surjective maps > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file provides definitions that are meant to deal with situations such as the following: Suppose that `G` is a group, and `H` is a type endowed with `has_one H`, `has_mul H`, and `has_inv H`. Suppose furthermore, that `f : G → H` is a surjective map that respects the multiplication, and the unit elements. Then `H` satisfies the group axioms. The relevant definition in this case is `function.surjective.group`. Dually, there is also `function.injective.group`. And there are versions for (additive) (commutative) semigroups/monoids. -/ namespace function /-! ### Injective -/ namespace injective variables {M₁ : Type*} {M₂ : Type*} [has_mul M₁] /-- A type endowed with `*` is a semigroup, if it admits an injective map that preserves `*` to a semigroup. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `+` is an additive semigroup, if it admits an injective map that preserves `+` to an additive semigroup."] protected def semigroup [semigroup M₂] (f : M₁ → M₂) (hf : injective f) (mul : ∀ x y, f (x * y) = f x * f y) : semigroup M₁ := { mul_assoc := λ x y z, hf $ by erw [mul, mul, mul, mul, mul_assoc], ..‹has_mul M₁› } /-- A type endowed with `*` is a commutative semigroup, if it admits an injective map that preserves `*` to a commutative semigroup. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `+` is an additive commutative semigroup, if it admits an injective map that preserves `+` to an additive commutative semigroup."] protected def comm_semigroup [comm_semigroup M₂] (f : M₁ → M₂) (hf : injective f) (mul : ∀ x y, f (x * y) = f x * f y) : comm_semigroup M₁ := { mul_comm := λ x y, hf $ by erw [mul, mul, mul_comm], .. hf.semigroup f mul } /-- A type endowed with `*` is a left cancel semigroup, if it admits an injective map that preserves `*` to a left cancel semigroup. See note [reducible non-instances]. -/ @[reducible, to_additive add_left_cancel_semigroup "A type endowed with `+` is an additive left cancel semigroup, if it admits an injective map that preserves `+` to an additive left cancel semigroup."] protected def left_cancel_semigroup [left_cancel_semigroup M₂] (f : M₁ → M₂) (hf : injective f) (mul : ∀ x y, f (x * y) = f x * f y) : left_cancel_semigroup M₁ := { mul := (*), mul_left_cancel := λ x y z H, hf $ (mul_right_inj (f x)).1 $ by erw [← mul, ← mul, H]; refl, .. hf.semigroup f mul } /-- A type endowed with `*` is a right cancel semigroup, if it admits an injective map that preserves `*` to a right cancel semigroup. See note [reducible non-instances]. -/ @[reducible, to_additive add_right_cancel_semigroup "A type endowed with `+` is an additive right cancel semigroup, if it admits an injective map that preserves `+` to an additive right cancel semigroup."] protected def right_cancel_semigroup [right_cancel_semigroup M₂] (f : M₁ → M₂) (hf : injective f) (mul : ∀ x y, f (x * y) = f x * f y) : right_cancel_semigroup M₁ := { mul := (*), mul_right_cancel := λ x y z H, hf $ (mul_left_inj (f y)).1 $ by erw [← mul, ← mul, H]; refl, .. hf.semigroup f mul } variables [has_one M₁] /-- A type endowed with `1` and `*` is a mul_one_class, if it admits an injective map that preserves `1` and `*` to a mul_one_class. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `0` and `+` is an add_zero_class, if it admits an injective map that preserves `0` and `+` to an add_zero_class."] protected def mul_one_class [mul_one_class M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : mul_one_class M₁ := { one_mul := λ x, hf $ by erw [mul, one, one_mul], mul_one := λ x, hf $ by erw [mul, one, mul_one], ..‹has_one M₁›, ..‹has_mul M₁› } variables [has_pow M₁ ℕ] /-- A type endowed with `1` and `*` is a monoid, if it admits an injective map that preserves `1` and `*` to a monoid. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `0` and `+` is an additive monoid, if it admits an injective map that preserves `0` and `+` to an additive monoid. This version takes a custom `nsmul` as a `[has_smul ℕ M₁]` argument."] protected def monoid [monoid M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : monoid M₁ := { npow := λ n x, x ^ n, npow_zero' := λ x, hf $ by erw [npow, one, pow_zero], npow_succ' := λ n x, hf $ by erw [npow, pow_succ, mul, npow], .. hf.semigroup f mul, .. hf.mul_one_class f one mul } /-- A type endowed with `0`, `1` and `+` is an additive monoid with one, if it admits an injective map that preserves `0`, `1` and `+` to an additive monoid with one. See note [reducible non-instances]. -/ @[reducible] protected def add_monoid_with_one {M₁} [has_zero M₁] [has_one M₁] [has_add M₁] [has_smul ℕ M₁] [has_nat_cast M₁] [add_monoid_with_one M₂] (f : M₁ → M₂) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (nat_cast : ∀ n : ℕ, f n = n) : add_monoid_with_one M₁ := { nat_cast := coe, nat_cast_zero := hf (by erw [nat_cast, nat.cast_zero, zero]), nat_cast_succ := λ n, hf (by erw [nat_cast, nat.cast_succ, add, one, nat_cast]), one := 1, .. hf.add_monoid f zero add nsmul } /-- A type endowed with `1` and `*` is a left cancel monoid, if it admits an injective map that preserves `1` and `*` to a left cancel monoid. See note [reducible non-instances]. -/ @[reducible, to_additive add_left_cancel_monoid "A type endowed with `0` and `+` is an additive left cancel monoid, if it admits an injective map that preserves `0` and `+` to an additive left cancel monoid."] protected def left_cancel_monoid [left_cancel_monoid M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : left_cancel_monoid M₁ := { .. hf.left_cancel_semigroup f mul, .. hf.monoid f one mul npow } /-- A type endowed with `1` and `*` is a right cancel monoid, if it admits an injective map that preserves `1` and `*` to a right cancel monoid. See note [reducible non-instances]. -/ @[reducible, to_additive add_right_cancel_monoid "A type endowed with `0` and `+` is an additive left cancel monoid, if it admits an injective map that preserves `0` and `+` to an additive left cancel monoid."] protected def right_cancel_monoid [right_cancel_monoid M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : right_cancel_monoid M₁ := { .. hf.right_cancel_semigroup f mul, .. hf.monoid f one mul npow } /-- A type endowed with `1` and `*` is a cancel monoid, if it admits an injective map that preserves `1` and `*` to a cancel monoid. See note [reducible non-instances]. -/ @[reducible, to_additive add_cancel_monoid "A type endowed with `0` and `+` is an additive left cancel monoid, if it admits an injective map that preserves `0` and `+` to an additive left cancel monoid."] protected def cancel_monoid [cancel_monoid M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : cancel_monoid M₁ := { .. hf.left_cancel_monoid f one mul npow, .. hf.right_cancel_monoid f one mul npow } /-- A type endowed with `1` and `*` is a commutative monoid, if it admits an injective map that preserves `1` and `*` to a commutative monoid. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `0` and `+` is an additive commutative monoid, if it admits an injective map that preserves `0` and `+` to an additive commutative monoid."] protected def comm_monoid [comm_monoid M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : comm_monoid M₁ := { .. hf.comm_semigroup f mul, .. hf.monoid f one mul npow } /-- A type endowed with `1` and `*` is a cancel commutative monoid, if it admits an injective map that preserves `1` and `*` to a cancel commutative monoid. See note [reducible non-instances]. -/ @[reducible, to_additive add_cancel_comm_monoid "A type endowed with `0` and `+` is an additive cancel commutative monoid, if it admits an injective map that preserves `0` and `+` to an additive cancel commutative monoid."] protected def cancel_comm_monoid [cancel_comm_monoid M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : cancel_comm_monoid M₁ := { .. hf.left_cancel_semigroup f mul, .. hf.comm_monoid f one mul npow } /-- A type has an involutive inversion if it admits a surjective map that preserves `⁻¹` to a type which has an involutive inversion. -/ @[reducible, to_additive "A type has an involutive negation if it admits a surjective map that preserves `⁻¹` to a type which has an involutive inversion."] --See note [reducible non-instances] protected def has_involutive_inv {M₁ : Type*} [has_inv M₁][has_involutive_inv M₂] (f : M₁ → M₂) (hf : injective f) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) : has_involutive_inv M₁ := { inv := has_inv.inv, inv_inv := λ x, hf $ by rw [inv, inv, inv_inv] } variables [has_inv M₁] [has_div M₁] [has_pow M₁ ℤ] /-- A type endowed with `1`, `*`, `⁻¹`, and `/` is a `div_inv_monoid` if it admits an injective map that preserves `1`, `*`, `⁻¹`, and `/` to a `div_inv_monoid`. See note [reducible non-instances]. -/ @[reducible, to_additive sub_neg_monoid "A type endowed with `0`, `+`, unary `-`, and binary `-` is a `sub_neg_monoid` if it admits an injective map that preserves `0`, `+`, unary `-`, and binary `-` to a `sub_neg_monoid`. This version takes custom `nsmul` and `zsmul` as `[has_smul ℕ M₁]` and `[has_smul ℤ M₁]` arguments."] protected def div_inv_monoid [div_inv_monoid M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) : div_inv_monoid M₁ := { zpow := λ n x, x ^ n, zpow_zero' := λ x, hf $ by erw [zpow, zpow_zero, one], zpow_succ' := λ n x, hf $ by erw [zpow, mul, zpow_of_nat, pow_succ, zpow, zpow_of_nat], zpow_neg' := λ n x, hf $ by erw [zpow, zpow_neg_succ_of_nat, inv, zpow, zpow_coe_nat], div_eq_mul_inv := λ x y, hf $ by erw [div, mul, inv, div_eq_mul_inv], .. hf.monoid f one mul npow, .. ‹has_inv M₁›, .. ‹has_div M₁› } /-- A type endowed with `1`, `*`, `⁻¹`, and `/` is a `division_monoid` if it admits an injective map that preserves `1`, `*`, `⁻¹`, and `/` to a `division_monoid`. -/ @[reducible, to_additive "A type endowed with `0`, `+`, unary `-`, and binary `-` is a `subtraction_monoid` if it admits an injective map that preserves `0`, `+`, unary `-`, and binary `-` to a `subtraction_monoid`. This version takes custom `nsmul` and `zsmul` as `[has_smul ℕ M₁]` and `[has_smul ℤ M₁]` arguments."] -- See note [reducible non-instances] protected def division_monoid [division_monoid M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) : division_monoid M₁ := { mul_inv_rev := λ x y, hf $ by erw [inv, mul, mul_inv_rev, mul, inv, inv], inv_eq_of_mul := λ x y h, hf $ by erw [inv, inv_eq_of_mul_eq_one_right (by erw [←mul, h, one])], ..hf.div_inv_monoid f one mul inv div npow zpow, ..hf.has_involutive_inv f inv } /-- A type endowed with `1`, `*`, `⁻¹`, and `/` is a `division_comm_monoid` if it admits an injective map that preserves `1`, `*`, `⁻¹`, and `/` to a `division_comm_monoid`. See note [reducible non-instances]. -/ @[reducible, to_additive subtraction_comm_monoid "A type endowed with `0`, `+`, unary `-`, and binary `-` is a `subtraction_comm_monoid` if it admits an injective map that preserves `0`, `+`, unary `-`, and binary `-` to a `subtraction_comm_monoid`. This version takes custom `nsmul` and `zsmul` as `[has_smul ℕ M₁]` and `[has_smul ℤ M₁]` arguments."] -- See note [reducible non-instances] protected def division_comm_monoid [division_comm_monoid M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) : division_comm_monoid M₁ := { ..hf.division_monoid f one mul inv div npow zpow, .. hf.comm_semigroup f mul } /-- A type endowed with `1`, `*` and `⁻¹` is a group, if it admits an injective map that preserves `1`, `*` and `⁻¹` to a group. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `0` and `+` is an additive group, if it admits an injective map that preserves `0` and `+` to an additive group."] protected def group [group M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) : group M₁ := { mul_left_inv := λ x, hf $ by erw [mul, inv, mul_left_inv, one], .. hf.div_inv_monoid f one mul inv div npow zpow } /-- A type endowed with `0`, `1` and `+` is an additive group with one, if it admits an injective map that preserves `0`, `1` and `+` to an additive group with one. See note [reducible non-instances]. -/ @[reducible] protected def add_group_with_one {M₁} [has_zero M₁] [has_one M₁] [has_add M₁] [has_smul ℕ M₁] [has_neg M₁] [has_sub M₁] [has_smul ℤ M₁] [has_nat_cast M₁] [has_int_cast M₁] [add_group_with_one M₂] (f : M₁ → M₂) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (neg : ∀ x, f (- x) = - f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) (nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) : add_group_with_one M₁ := { int_cast := coe, int_cast_of_nat := λ n, hf (by simp only [nat_cast, int_cast, int.cast_coe_nat]), int_cast_neg_succ_of_nat := λ n, hf (by erw [int_cast, neg, nat_cast, int.cast_neg, int.cast_coe_nat]), .. hf.add_group f zero add neg sub nsmul zsmul, .. hf.add_monoid_with_one f zero one add nsmul nat_cast } /-- A type endowed with `1`, `*` and `⁻¹` is a commutative group, if it admits an injective map that preserves `1`, `*` and `⁻¹` to a commutative group. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `0` and `+` is an additive commutative group, if it admits an injective map that preserves `0` and `+` to an additive commutative group."] protected def comm_group [comm_group M₂] (f : M₁ → M₂) (hf : injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) : comm_group M₁ := { .. hf.comm_monoid f one mul npow, .. hf.group f one mul inv div npow zpow } end injective /-! ### Surjective -/ namespace surjective variables {M₁ : Type*} {M₂ : Type*} [has_mul M₂] /-- A type endowed with `*` is a semigroup, if it admits a surjective map that preserves `*` from a semigroup. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `+` is an additive semigroup, if it admits a surjective map that preserves `+` from an additive semigroup."] protected def semigroup [semigroup M₁] (f : M₁ → M₂) (hf : surjective f) (mul : ∀ x y, f (x * y) = f x * f y) : semigroup M₂ := { mul_assoc := hf.forall₃.2 $ λ x y z, by simp only [← mul, mul_assoc], ..‹has_mul M₂› } /-- A type endowed with `*` is a commutative semigroup, if it admits a surjective map that preserves `*` from a commutative semigroup. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `+` is an additive commutative semigroup, if it admits a surjective map that preserves `+` from an additive commutative semigroup."] protected def comm_semigroup [comm_semigroup M₁] (f : M₁ → M₂) (hf : surjective f) (mul : ∀ x y, f (x * y) = f x * f y) : comm_semigroup M₂ := { mul_comm := hf.forall₂.2 $ λ x y, by erw [← mul, ← mul, mul_comm], .. hf.semigroup f mul } variables [has_one M₂] /-- A type endowed with `1` and `*` is a mul_one_class, if it admits a surjective map that preserves `1` and `*` from a mul_one_class. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `0` and `+` is an add_zero_class, if it admits a surjective map that preserves `0` and `+` to an add_zero_class."] protected def mul_one_class [mul_one_class M₁] (f : M₁ → M₂) (hf : surjective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : mul_one_class M₂ := { one_mul := hf.forall.2 $ λ x, by erw [← one, ← mul, one_mul], mul_one := hf.forall.2 $ λ x, by erw [← one, ← mul, mul_one], ..‹has_one M₂›, ..‹has_mul M₂› } variables [has_pow M₂ ℕ] /-- A type endowed with `1` and `*` is a monoid, if it admits a surjective map that preserves `1` and `*` to a monoid. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `0` and `+` is an additive monoid, if it admits a surjective map that preserves `0` and `+` to an additive monoid. This version takes a custom `nsmul` as a `[has_smul ℕ M₂]` argument."] protected def monoid [monoid M₁] (f : M₁ → M₂) (hf : surjective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : monoid M₂ := { npow := λ n x, x ^ n, npow_zero' := hf.forall.2 $ λ x, by erw [←npow, pow_zero, ←one], npow_succ' := λ n, hf.forall.2 $ λ x, by erw [←npow, pow_succ, ←npow, ←mul], .. hf.semigroup f mul, .. hf.mul_one_class f one mul } /-- A type endowed with `0`, `1` and `+` is an additive monoid with one, if it admits a surjective map that preserves `0`, `1` and `*` from an additive monoid with one. See note [reducible non-instances]. -/ @[reducible] protected def add_monoid_with_one {M₂} [has_zero M₂] [has_one M₂] [has_add M₂] [has_smul ℕ M₂] [has_nat_cast M₂] [add_monoid_with_one M₁] (f : M₁ → M₂) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (nat_cast : ∀ n : ℕ, f n = n) : add_monoid_with_one M₂ := { nat_cast := coe, nat_cast_zero := by { rw [← nat_cast, nat.cast_zero, zero], refl }, nat_cast_succ := λ n, by { rw [← nat_cast, nat.cast_succ, add, one, nat_cast], refl }, one := 1, .. hf.add_monoid f zero add nsmul } /-- A type endowed with `1` and `*` is a commutative monoid, if it admits a surjective map that preserves `1` and `*` from a commutative monoid. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `0` and `+` is an additive commutative monoid, if it admits a surjective map that preserves `0` and `+` to an additive commutative monoid."] protected def comm_monoid [comm_monoid M₁] (f : M₁ → M₂) (hf : surjective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : comm_monoid M₂ := { .. hf.comm_semigroup f mul, .. hf.monoid f one mul npow } /-- A type has an involutive inversion if it admits a surjective map that preserves `⁻¹` to a type which has an involutive inversion. -/ @[reducible, to_additive "A type has an involutive negation if it admits a surjective map that preserves `⁻¹` to a type which has an involutive inversion."] --See note [reducible non-instances] protected def has_involutive_inv {M₂ : Type*} [has_inv M₂] [has_involutive_inv M₁] (f : M₁ → M₂) (hf : surjective f) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) : has_involutive_inv M₂ := { inv := has_inv.inv, inv_inv := hf.forall.2 $ λ x, by erw [←inv, ←inv, inv_inv] } variables [has_inv M₂] [has_div M₂] [has_pow M₂ ℤ] /-- A type endowed with `1`, `*`, `⁻¹`, and `/` is a `div_inv_monoid` if it admits a surjective map that preserves `1`, `*`, `⁻¹`, and `/` to a `div_inv_monoid`. See note [reducible non-instances]. -/ @[reducible, to_additive sub_neg_monoid "A type endowed with `0`, `+`, unary `-`, and binary `-` is a `sub_neg_monoid` if it admits a surjective map that preserves `0`, `+`, unary `-`, and binary `-` to a `sub_neg_monoid`."] protected def div_inv_monoid [div_inv_monoid M₁] (f : M₁ → M₂) (hf : surjective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) : div_inv_monoid M₂ := { zpow := λ n x, x ^ n, zpow_zero' := hf.forall.2 $ λ x, by erw [←zpow, zpow_zero, ←one], zpow_succ' := λ n, hf.forall.2 $ λ x, by erw [←zpow, ←zpow, zpow_of_nat, zpow_of_nat, pow_succ, ←mul], zpow_neg' := λ n, hf.forall.2 $ λ x, by erw [←zpow, ←zpow, zpow_neg_succ_of_nat, zpow_coe_nat, inv], div_eq_mul_inv := hf.forall₂.2 $ λ x y, by erw [← inv, ← mul, ← div, div_eq_mul_inv], .. hf.monoid f one mul npow, .. ‹has_div M₂›, .. ‹has_inv M₂› } /-- A type endowed with `1`, `*` and `⁻¹` is a group, if it admits a surjective map that preserves `1`, `*` and `⁻¹` to a group. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `0` and `+` is an additive group, if it admits a surjective map that preserves `0` and `+` to an additive group."] protected def group [group M₁] (f : M₁ → M₂) (hf : surjective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) : group M₂ := { mul_left_inv := hf.forall.2 $ λ x, by erw [← inv, ← mul, mul_left_inv, one]; refl, .. hf.div_inv_monoid f one mul inv div npow zpow } /-- A type endowed with `0`, `1`, `+` is an additive group with one, if it admits a surjective map that preserves `0`, `1`, and `+` to an additive group with one. See note [reducible non-instances]. -/ protected def add_group_with_one {M₂} [has_zero M₂] [has_one M₂] [has_add M₂] [has_neg M₂] [has_sub M₂] [has_smul ℕ M₂] [has_smul ℤ M₂] [has_nat_cast M₂] [has_int_cast M₂] [add_group_with_one M₁] (f : M₁ → M₂) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (neg : ∀ x, f (- x) = - f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) (nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) : add_group_with_one M₂ := { int_cast := coe, int_cast_of_nat := λ n, by rw [← int_cast, int.cast_coe_nat, nat_cast], int_cast_neg_succ_of_nat := λ n, by { rw [← int_cast, int.cast_neg, int.cast_coe_nat, neg, nat_cast], refl }, .. hf.add_monoid_with_one f zero one add nsmul nat_cast, .. hf.add_group f zero add neg sub nsmul zsmul } /-- A type endowed with `1`, `*`, `⁻¹`, and `/` is a commutative group, if it admits a surjective map that preserves `1`, `*`, `⁻¹`, and `/` from a commutative group. See note [reducible non-instances]. -/ @[reducible, to_additive "A type endowed with `0` and `+` is an additive commutative group, if it admits a surjective map that preserves `0` and `+` to an additive commutative group."] protected def comm_group [comm_group M₁] (f : M₁ → M₂) (hf : surjective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) : comm_group M₂ := { .. hf.comm_monoid f one mul npow, .. hf.group f one mul inv div npow zpow } end surjective end function
0b795d564e92067954eff107d681d95548b9ee27
e1da55f4222dac91b940ca052928eaace09762da
/src/energy.lean
69d4dd5e15929452a304dce3244c109fda90839c
[]
no_license
b-mehta/regularity-lemma
c5826e22c280d0b073a4e62dba731f4dd3d1b69f
cf26082b0c88fa54276e6fdc3338c15e607c52c6
refs/heads/master
1,658,209,524,267
1,644,406,456,000
1,644,406,456,000
457,327,371
1
0
null
null
null
null
UTF-8
Lean
false
false
3,674
lean
/- Copyright (c) 2021 Yaël Dillies, Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Bhavik Mehta -/ import .mathlib import combinatorics.choose.bounds import data.sym.card /-! # Index -/ open_locale big_operators open finset fintype function relation variables {α : Type*} /-! ## finpartition.is_uniform -/ variables [decidable_eq α] {s : finset α} (P : finpartition s) variables (G : simple_graph α) [decidable_rel G.adj] namespace finpartition open_locale classical open finset noncomputable def non_uniform_pairs (ε : ℝ) : finset (finset α × finset α) := P.parts.off_diag.filter (λ UV, ¬G.is_uniform ε UV.1 UV.2) lemma mem_non_uniform_pairs (U V : finset α) (ε : ℝ) : (U, V) ∈ P.non_uniform_pairs G ε ↔ U ∈ P.parts ∧ V ∈ P.parts ∧ U ≠ V ∧ ¬G.is_uniform ε U V := by rw [non_uniform_pairs, mem_filter, mem_off_diag, and_assoc, and_assoc] /-- An finpartition is `ε-uniform` iff at most a proportion of `ε` of its pairs of parts are not `ε-uniform`. -/ def is_uniform (ε : ℝ) : Prop := ((P.non_uniform_pairs G ε).card : ℝ) ≤ (P.parts.card * (P.parts.card - 1) : ℕ) * ε lemma empty_is_uniform {P : finpartition s} {G : simple_graph α} {ε : ℝ} (hP : P.parts = ∅) : P.is_uniform G ε := by simp [is_uniform, hP, non_uniform_pairs] lemma nonempty_of_not_uniform {P : finpartition s} {G : simple_graph α} {ε : ℝ} (h : ¬ P.is_uniform G ε) : P.parts.nonempty := nonempty_of_ne_empty (λ h₁, h (empty_is_uniform h₁)) lemma is_uniform_of_one_le {ε : ℝ} (hε : 1 ≤ ε) : P.is_uniform G ε := begin apply le_trans _ (mul_le_mul_of_nonneg_left hε (nat.cast_nonneg _)), rw [mul_one, nat.cast_le], apply le_trans (card_filter_le _ _), rw [off_diag_card, nat.mul_sub_left_distrib, mul_one], end /-- The energy is the auxiliary quantity that drives the induction process in the proof of Szemerédi's Regularity Lemma (see `increment`). As long as we do not have a suitable equipartition, we will find a new one that has an energy greater than the previous one plus some fixed constant. Then `energy_le_one` ensures this process only happens finitely many times. -/ noncomputable def energy (P : finpartition s) : ℝ := (∑ UV in P.parts.off_diag, G.edge_density UV.1 UV.2^2)/P.parts.card^2 lemma energy_nonneg (P : finpartition s) : 0 ≤ P.energy G := div_nonneg (finset.sum_nonneg (λ _ _, sq_nonneg _)) (sq_nonneg _) lemma energy_le_one (P : finpartition s) : P.energy G ≤ 1 := begin refine div_le_of_nonneg_of_le_mul (sq_nonneg _) zero_le_one _, suffices h : ∑ UV in P.parts.off_diag, G.edge_density UV.1 UV.2^2 ≤ P.parts.off_diag.card, { apply h.trans, rw [off_diag_card, one_mul, ←nat.cast_pow, nat.cast_le, sq], apply tsub_le_self }, rw ←nat.smul_one_eq_coe, refine sum_le_of_forall_le _ _ 1 (λ UV _, _), rw sq_le_one_iff (G.edge_density_nonneg _ _), exact G.edge_density_le_one _ _, end lemma non_uniform_pairs_bot {ε : ℝ} (hε : 0 < ε) : (⊥ : finpartition s).non_uniform_pairs G ε = ∅ := begin rw eq_empty_iff_forall_not_mem, rintro ⟨U, V⟩, simp only [finpartition.mem_non_uniform_pairs, finpartition.parts_bot, mem_map, not_and, not_not, embedding.coe_fn_mk, exists_imp_distrib], rintro x hx rfl y hy rfl h, exact G.is_uniform_singleton hε, end lemma bot_is_uniform {ε : ℝ} (hε : 0 < ε) : (⊥ : finpartition s).is_uniform G ε := begin rw [finpartition.is_uniform, finpartition.card_bot, non_uniform_pairs_bot _ hε, finset.card_empty, nat.cast_zero], exact mul_nonneg (nat.cast_nonneg _) hε.le, end end finpartition
7469c9f7b378b00cebac4e4cb4c06487dc7841b6
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/polynomial/degree/lemmas.lean
5bf2503324f5d42a1db3809277ed85731c397ab6
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
12,644
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.eval import tactic.interval_cases /-! # Theory of degrees of polynomials Some of the main results include - `nat_degree_comp_le` : The degree of the composition is at most the product of degrees -/ noncomputable theory open_locale classical polynomial open finsupp finset namespace polynomial universes u v w variables {R : Type u} {S : Type v} { ι : Type w} {a b : R} {m n : ℕ} section semiring variables [semiring R] {p q r : R[X]} section degree lemma nat_degree_comp_le : nat_degree (p.comp q) ≤ nat_degree p * nat_degree q := if h0 : p.comp q = 0 then by rw [h0, nat_degree_zero]; exact nat.zero_le _ else with_bot.coe_le_coe.1 $ calc ↑(nat_degree (p.comp q)) = degree (p.comp q) : (degree_eq_nat_degree h0).symm ... = _ : congr_arg degree comp_eq_sum_left ... ≤ _ : degree_sum_le _ _ ... ≤ _ : sup_le (λ n hn, calc degree (C (coeff p n) * q ^ n) ≤ degree (C (coeff p n)) + degree (q ^ n) : degree_mul_le _ _ ... ≤ nat_degree (C (coeff p n)) + n • (degree q) : add_le_add degree_le_nat_degree (degree_pow_le _ _) ... ≤ nat_degree (C (coeff p n)) + n • (nat_degree q) : add_le_add_left (nsmul_le_nsmul_of_le_right (@degree_le_nat_degree _ _ q) n) _ ... = (n * nat_degree q : ℕ) : by rw [nat_degree_C, with_bot.coe_zero, zero_add, ← with_bot.coe_nsmul, nsmul_eq_mul]; simp ... ≤ (nat_degree p * nat_degree q : ℕ) : with_bot.coe_le_coe.2 $ mul_le_mul_of_nonneg_right (le_nat_degree_of_ne_zero (mem_support_iff.1 hn)) (nat.zero_le _)) lemma degree_pos_of_root {p : R[X]} (hp : p ≠ 0) (h : is_root p a) : 0 < degree p := lt_of_not_ge $ λ hlt, begin have := eq_C_of_degree_le_zero hlt, rw [is_root, this, eval_C] at h, simp only [h, ring_hom.map_zero] at this, exact hp this, end lemma nat_degree_le_iff_coeff_eq_zero : p.nat_degree ≤ n ↔ ∀ N : ℕ, n < N → p.coeff N = 0 := by simp_rw [nat_degree_le_iff_degree_le, degree_le_iff_coeff_zero, with_bot.coe_lt_coe] lemma nat_degree_add_le_iff_left {n : ℕ} (p q : R[X]) (qn : q.nat_degree ≤ n) : (p + q).nat_degree ≤ n ↔ p.nat_degree ≤ n := begin refine ⟨λ h, _, λ h, nat_degree_add_le_of_degree_le h qn⟩, refine nat_degree_le_iff_coeff_eq_zero.mpr (λ m hm, _), convert nat_degree_le_iff_coeff_eq_zero.mp h m hm using 1, rw [coeff_add, nat_degree_le_iff_coeff_eq_zero.mp qn _ hm, add_zero], end lemma nat_degree_add_le_iff_right {n : ℕ} (p q : R[X]) (pn : p.nat_degree ≤ n) : (p + q).nat_degree ≤ n ↔ q.nat_degree ≤ n := begin rw add_comm, exact nat_degree_add_le_iff_left _ _ pn, end lemma nat_degree_C_mul_le (a : R) (f : R[X]) : (C a * f).nat_degree ≤ f.nat_degree := calc (C a * f).nat_degree ≤ (C a).nat_degree + f.nat_degree : nat_degree_mul_le ... = 0 + f.nat_degree : by rw nat_degree_C a ... = f.nat_degree : zero_add _ lemma nat_degree_mul_C_le (f : R[X]) (a : R) : (f * C a).nat_degree ≤ f.nat_degree := calc (f * C a).nat_degree ≤ f.nat_degree + (C a).nat_degree : nat_degree_mul_le ... = f.nat_degree + 0 : by rw nat_degree_C a ... = f.nat_degree : add_zero _ lemma eq_nat_degree_of_le_mem_support (pn : p.nat_degree ≤ n) (ns : n ∈ p.support) : p.nat_degree = n := le_antisymm pn (le_nat_degree_of_mem_supp _ ns) lemma nat_degree_C_mul_eq_of_mul_eq_one {ai : R} (au : ai * a = 1) : (C a * p).nat_degree = p.nat_degree := le_antisymm (nat_degree_C_mul_le a p) (calc p.nat_degree = (1 * p).nat_degree : by nth_rewrite 0 [← one_mul p] ... = (C ai * (C a * p)).nat_degree : by rw [← C_1, ← au, ring_hom.map_mul, ← mul_assoc] ... ≤ (C a * p).nat_degree : nat_degree_C_mul_le ai (C a * p)) lemma nat_degree_mul_C_eq_of_mul_eq_one {ai : R} (au : a * ai = 1) : (p * C a).nat_degree = p.nat_degree := le_antisymm (nat_degree_mul_C_le p a) (calc p.nat_degree = (p * 1).nat_degree : by nth_rewrite 0 [← mul_one p] ... = ((p * C a) * C ai).nat_degree : by rw [← C_1, ← au, ring_hom.map_mul, ← mul_assoc] ... ≤ (p * C a).nat_degree : nat_degree_mul_C_le (p * C a) ai) /-- Although not explicitly stated, the assumptions of lemma `nat_degree_mul_C_eq_of_mul_ne_zero` force the polynomial `p` to be non-zero, via `p.leading_coeff ≠ 0`. -/ lemma nat_degree_mul_C_eq_of_mul_ne_zero (h : p.leading_coeff * a ≠ 0) : (p * C a).nat_degree = p.nat_degree := begin refine eq_nat_degree_of_le_mem_support (nat_degree_mul_C_le p a) _, refine mem_support_iff.mpr _, rwa coeff_mul_C, end /-- Although not explicitly stated, the assumptions of lemma `nat_degree_C_mul_eq_of_mul_ne_zero` force the polynomial `p` to be non-zero, via `p.leading_coeff ≠ 0`. -/ lemma nat_degree_C_mul_eq_of_mul_ne_zero (h : a * p.leading_coeff ≠ 0) : (C a * p).nat_degree = p.nat_degree := begin refine eq_nat_degree_of_le_mem_support (nat_degree_C_mul_le a p) _, refine mem_support_iff.mpr _, rwa coeff_C_mul, end lemma nat_degree_add_coeff_mul (f g : R[X]) : (f * g).coeff (f.nat_degree + g.nat_degree) = f.coeff f.nat_degree * g.coeff g.nat_degree := by simp only [coeff_nat_degree, coeff_mul_degree_add_degree] lemma nat_degree_lt_coeff_mul (h : p.nat_degree + q.nat_degree < m + n) : (p * q).coeff (m + n) = 0 := coeff_eq_zero_of_nat_degree_lt (nat_degree_mul_le.trans_lt h) lemma coeff_mul_of_nat_degree_le (pm : p.nat_degree ≤ m) (qn : q.nat_degree ≤ n) : (p * q).coeff (m + n) = p.coeff m * q.coeff n := begin rcases eq_or_lt_of_le pm with rfl | hm; rcases eq_or_lt_of_le qn with rfl | hn, { exact nat_degree_add_coeff_mul _ _ }, { rw [coeff_eq_zero_of_nat_degree_lt hn, mul_zero], exact nat_degree_lt_coeff_mul (add_lt_add_left hn _) }, { rw [coeff_eq_zero_of_nat_degree_lt hm, zero_mul], exact nat_degree_lt_coeff_mul (add_lt_add_right hm _) }, { rw [coeff_eq_zero_of_nat_degree_lt hn, mul_zero], exact nat_degree_lt_coeff_mul (add_lt_add hm hn) }, end lemma coeff_pow_of_nat_degree_le (pn : p.nat_degree ≤ n) : (p ^ m).coeff (n * m) = (p.coeff n) ^ m := begin induction m with m hm, { simp }, { rw [pow_succ', pow_succ', ← hm, nat.mul_succ, coeff_mul_of_nat_degree_le _ pn], refine nat_degree_pow_le.trans (le_trans _ (mul_comm _ _).le), exact mul_le_mul_of_nonneg_left pn m.zero_le } end lemma coeff_add_eq_left_of_lt (qn : q.nat_degree < n) : (p + q).coeff n = p.coeff n := (coeff_add _ _ _).trans $ (congr_arg _ $ coeff_eq_zero_of_nat_degree_lt $ qn).trans $ add_zero _ lemma coeff_add_eq_right_of_lt (pn : p.nat_degree < n) : (p + q).coeff n = q.coeff n := by { rw add_comm, exact coeff_add_eq_left_of_lt pn } lemma degree_sum_eq_of_disjoint (f : S → R[X]) (s : finset S) (h : set.pairwise { i | i ∈ s ∧ f i ≠ 0 } (ne on (degree ∘ f))) : degree (s.sum f) = s.sup (λ i, degree (f i)) := begin induction s using finset.induction_on with x s hx IH, { simp }, { simp only [hx, finset.sum_insert, not_false_iff, finset.sup_insert], specialize IH (h.mono (λ _, by simp {contextual := tt})), rcases lt_trichotomy (degree (f x)) (degree (s.sum f)) with H|H|H, { rw [←IH, sup_eq_right.mpr H.le, degree_add_eq_right_of_degree_lt H] }, { rcases s.eq_empty_or_nonempty with rfl|hs, { simp }, obtain ⟨y, hy, hy'⟩ := finset.exists_mem_eq_sup s hs (λ i, degree (f i)), rw [IH, hy'] at H, by_cases hx0 : f x = 0, { simp [hx0, IH] }, have hy0 : f y ≠ 0, { contrapose! H, simpa [H, degree_eq_bot] using hx0 }, refine absurd H (h _ _ (λ H, hx _)), { simp [hx0] }, { simp [hy, hy0] }, { exact H.symm ▸ hy } }, { rw [←IH, sup_eq_left.mpr H.le, degree_add_eq_left_of_degree_lt H] } } end lemma nat_degree_sum_eq_of_disjoint (f : S → R[X]) (s : finset S) (h : set.pairwise { i | i ∈ s ∧ f i ≠ 0 } (ne on (nat_degree ∘ f))) : nat_degree (s.sum f) = s.sup (λ i, nat_degree (f i)) := begin by_cases H : ∃ x ∈ s, f x ≠ 0, { obtain ⟨x, hx, hx'⟩ := H, have hs : s.nonempty := ⟨x, hx⟩, refine nat_degree_eq_of_degree_eq_some _, rw degree_sum_eq_of_disjoint, { rw [←finset.sup'_eq_sup hs, ←finset.sup'_eq_sup hs, finset.coe_sup', ←finset.sup'_eq_sup hs], refine le_antisymm _ _, { rw finset.sup'_le_iff, intros b hb, by_cases hb' : f b = 0, { simpa [hb'] using hs }, rw degree_eq_nat_degree hb', exact finset.le_sup' _ hb }, { rw finset.sup'_le_iff, intros b hb, simp only [finset.le_sup'_iff, exists_prop, function.comp_app], by_cases hb' : f b = 0, { refine ⟨x, hx, _⟩, contrapose! hx', simpa [hb', degree_eq_bot] using hx' }, exact ⟨b, hb, (degree_eq_nat_degree hb').ge⟩ } }, { exact h.imp (λ x y hxy hxy', hxy (nat_degree_eq_of_degree_eq hxy')) } }, { push_neg at H, rw [finset.sum_eq_zero H, nat_degree_zero, eq_comm, show 0 = ⊥, from rfl, finset.sup_eq_bot_iff], intros x hx, simp [H x hx] } end lemma nat_degree_bit0 (a : R[X]) : (bit0 a).nat_degree ≤ a.nat_degree := (nat_degree_add_le _ _).trans (max_self _).le lemma nat_degree_bit1 (a : R[X]) : (bit1 a).nat_degree ≤ a.nat_degree := (nat_degree_add_le _ _).trans (by simp [nat_degree_bit0]) variables [semiring S] lemma nat_degree_pos_of_eval₂_root {p : R[X]} (hp : p ≠ 0) (f : R →+* S) {z : S} (hz : eval₂ f z p = 0) (inj : ∀ (x : R), f x = 0 → x = 0) : 0 < nat_degree p := lt_of_not_ge $ λ hlt, begin have A : p = C (p.coeff 0) := eq_C_of_nat_degree_le_zero hlt, rw [A, eval₂_C] at hz, simp only [inj (p.coeff 0) hz, ring_hom.map_zero] at A, exact hp A end lemma degree_pos_of_eval₂_root {p : R[X]} (hp : p ≠ 0) (f : R →+* S) {z : S} (hz : eval₂ f z p = 0) (inj : ∀ (x : R), f x = 0 → x = 0) : 0 < degree p := nat_degree_pos_iff_degree_pos.mp (nat_degree_pos_of_eval₂_root hp f hz inj) @[simp] lemma coe_lt_degree {p : R[X]} {n : ℕ} : ((n : with_bot ℕ) < degree p) ↔ n < nat_degree p := begin by_cases h : p = 0, { simp [h] }, rw [degree_eq_nat_degree h, with_bot.coe_lt_coe], end end degree end semiring section ring variables [ring R] {p q : R[X]} lemma nat_degree_sub : (p - q).nat_degree = (q - p).nat_degree := by rw [← nat_degree_neg, neg_sub] lemma nat_degree_sub_le_iff_left (qn : q.nat_degree ≤ n) : (p - q).nat_degree ≤ n ↔ p.nat_degree ≤ n := begin rw ← nat_degree_neg at qn, rw [sub_eq_add_neg, nat_degree_add_le_iff_left _ _ qn], end lemma nat_degree_sub_le_iff_right (pn : p.nat_degree ≤ n) : (p - q).nat_degree ≤ n ↔ q.nat_degree ≤ n := by rwa [nat_degree_sub, nat_degree_sub_le_iff_left] lemma coeff_sub_eq_left_of_lt (dg : q.nat_degree < n) : (p - q).coeff n = p.coeff n := begin rw ← nat_degree_neg at dg, rw [sub_eq_add_neg, coeff_add_eq_left_of_lt dg], end lemma coeff_sub_eq_neg_right_of_lt (df : p.nat_degree < n) : (p - q).coeff n = - q.coeff n := by rwa [sub_eq_add_neg, coeff_add_eq_right_of_lt, coeff_neg] end ring section no_zero_divisors variables [semiring R] [no_zero_divisors R] {p q : R[X]} lemma degree_mul_C (a0 : a ≠ 0) : (p * C a).degree = p.degree := by rw [degree_mul, degree_C a0, add_zero] lemma degree_C_mul (a0 : a ≠ 0) : (C a * p).degree = p.degree := by rw [degree_mul, degree_C a0, zero_add] lemma nat_degree_mul_C (a0 : a ≠ 0) : (p * C a).nat_degree = p.nat_degree := by simp only [nat_degree, degree_mul_C a0] lemma nat_degree_C_mul (a0 : a ≠ 0) : (C a * p).nat_degree = p.nat_degree := by simp only [nat_degree, degree_C_mul a0] lemma nat_degree_comp : nat_degree (p.comp q) = nat_degree p * nat_degree q := begin by_cases q0 : q.nat_degree = 0, { rw [degree_le_zero_iff.mp (nat_degree_eq_zero_iff_degree_le_zero.mp q0), comp_C, nat_degree_C, nat_degree_C, mul_zero] }, { by_cases p0 : p = 0, { simp only [p0, zero_comp, nat_degree_zero, zero_mul] }, refine le_antisymm nat_degree_comp_le (le_nat_degree_of_ne_zero _), simp only [coeff_comp_degree_mul_degree q0, p0, mul_eq_zero, leading_coeff_eq_zero, or_self, ne_zero_of_nat_degree_gt (nat.pos_of_ne_zero q0), pow_ne_zero, ne.def, not_false_iff] } end lemma leading_coeff_comp (hq : nat_degree q ≠ 0) : leading_coeff (p.comp q) = leading_coeff p * leading_coeff q ^ nat_degree p := by rw [← coeff_comp_degree_mul_degree hq, ← nat_degree_comp, coeff_nat_degree] end no_zero_divisors end polynomial
5dabc4503ea922a310d1fbc95d70af6960bfc5c2
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/conv_tac1.lean
d25e2cc6676383628dc61848646584e96f3d74b5
[ "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
3,463
lean
example (a b : nat) : (λ x, a + x) 0 = b + 1 + a := begin conv in (_ + 1) { change nat.succ b }, guard_target (λ x, a + x) 0 = nat.succ b + a, admit end def Div : nat → nat → nat | x y := if h : 0 < y ∧ y ≤ x then have x - y < x, from sorry, Div (x - y) y + 1 else 0 example (x y : nat) : 0 < y → y ≤ x → Div x y = Div (x - y) y + 1 := begin intros h1 h2, -- Use conv to focus on the lhs conv { to_lhs, simp [Div] {single_pass := tt}, simp [h1, h2] }, guard_target 1 + Div (x - y) y = Div (x - y) y + 1, simp end example (x y : nat) (f : nat → nat) (h : f (0 + x + y) = 0 + y) : f (x + y) = 0 + y := begin -- use conv to rewrite subterm of a hypothesis conv at h in (0 + _) { simp }, assumption end example (x y : nat) (f : nat → nat) (h : f (0 + x + y) = 0 + y) : f (x + y) = 0 + y := begin -- use conv to rewrite subterm of a hypothesis conv at h in (0 + x) { simp }, assumption end example (x y : nat) (f : nat → nat) (h : f (0 + x + y) = 0 + y) : f (x + y) = 0 + y := begin -- use congr primitive to find term to be modified conv at h { guard_lhs f (0 + x + y) = 0 + y, congr, { guard_lhs f (0 + x + y), congr, congr, simp }, { guard_lhs 0 + y } }, assumption end example (x y : nat) (f : nat → nat) (h : f (0 + x + y) = 0 + y) : f (x + y) = 0 + y := begin -- use congr primitive to find term to be modified conv at h { guard_lhs f (0 + x + y) = 0 + y, to_lhs, guard_lhs f (0 + x + y), congr, guard_lhs 0 + x + y, congr, simp, }, assumption end example (x y : nat) (f : nat → nat) (h : f (0 + x + y) = 0 + y) : f (x + y) = 0 + y := begin -- use conv to rewrite subterm of a hypothesis conv at h in (0 + _) { rw [zero_add] }, assumption end example (x y : nat) (f : nat → nat) (h : f (0 + x + y) = 0 + y) : f (0 + x + y) = y := begin -- use conv to rewrite rhs a hypothesis conv at h { to_rhs, rw [zero_add] }, assumption end example (x : nat) (f : nat → nat) (h₁ : x = 0) (h₂ : ∀ x, f x = x + x) : f x = x := begin conv { to_rhs, rw [h₁, <- add_zero 0, <- h₁], }, exact h₂ x end lemma addz (x : nat) : x + 0 = x := rfl example (x : nat) (g : nat → nat) (f : nat → (nat → nat) → nat) (h : f (x + 0) (λ x, g x) = x) : f x g = x := begin conv at h {dsimp [addz] {eta := ff}}, conv at h {dsimp}, exact h, end example (x : nat) (g : nat → nat) (f : nat → (nat → nat) → nat) (h : f (x + 0) (λ x, g x) = x) : f x g = x := begin conv at h {dsimp [addz] {eta := ff}, guard_lhs f x (λ x, g x) = x, dsimp, guard_lhs f x g = x}, exact h, end def f (x y : nat) : nat := x + y + 1 example (x y : nat) : f x y + f x x + f y y = x + y + 1 + y + y + 1 + f x x := begin conv { -- execute `rw [f]` for 1st and 3rd occurrences of f-applications for (f _ _) [1, 3] { rw [f] }, guard_lhs (x + y + 1) + f x x + (y + y + 1) = x + y + 1 + y + y + 1 + f x x, simp } end example (x : nat) : f x x + f x x + f x x = f x x + x + x + x + x + 1 + 1 := begin conv { -- execute `rw [f]` for 1st and 3rd occurrences of f-applications for (f _ _) [1, 3] { rw [f] }, guard_lhs (x + x + 1) + f x x + (x + x + 1) = f x x + x + x + x + x + 1 + 1, simp } end example (x : nat) : f x x + f x x = f x x + x + x + 1 := begin conv in (f _ _) { rw [f] }, guard_target (x + x + 1) + f x x = f x x + x + x + 1, simp end
8594460f1c3d777bad1d082268c1d0f8a27eb20b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/free.lean
99a6de4152255104ead101a802531648c4d104af
[]
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
21,287
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.equiv.basic import Mathlib.control.applicative import Mathlib.control.traversable.basic import Mathlib.PostPort universes u l v u_1 namespace Mathlib /-! # Free constructions ## Main definitions * `free_magma α`: free magma (structure with binary operation without any axioms) over alphabet `α`, defined inductively, with traversable instance and decidable equality. * `magma.free_semigroup α`: free semigroup over magma `α`. * `free_semigroup α`: free semigroup over alphabet `α`, defined as a synonym for `α × list α` (i.e. nonempty lists), with traversable instance and decidable equality. * `free_semigroup_free_magma α`: isomorphism between `magma.free_semigroup (free_magma α)` and `free_semigroup α`. -/ /-- Free magma over a given alphabet. -/ inductive free_magma (α : Type u) where | of : α → free_magma α | mul : free_magma α → free_magma α → free_magma α /-- Free nonabelian additive magma over a given alphabet. -/ inductive free_add_magma (α : Type u) where | of : α → free_add_magma α | add : free_add_magma α → free_add_magma α → free_add_magma α namespace free_magma protected instance Mathlib.free_add_magma.inhabited {α : Type u} [Inhabited α] : Inhabited (free_add_magma α) := { default := free_add_magma.of Inhabited.default } protected instance Mathlib.free_add_magma.has_add {α : Type u} : Add (free_add_magma α) := { add := free_add_magma.add } @[simp] theorem Mathlib.free_add_magma.add_eq {α : Type u} (x : free_add_magma α) (y : free_add_magma α) : free_add_magma.add x y = x + y := rfl /-- Recursor for `free_magma` using `x * y` instead of `free_magma.mul x y`. -/ def Mathlib.free_add_magma.rec_on' {α : Type u} {C : free_add_magma α → Sort l} (x : free_add_magma α) (ih1 : (x : α) → C (free_add_magma.of x)) (ih2 : (x y : free_add_magma α) → C x → C y → C (x + y)) : C x := free_add_magma.rec_on x ih1 ih2 end free_magma /-- Lifts a function `α → β` to a magma homomorphism `free_magma α → β` given a magma `β`. -/ def free_magma.lift {α : Type u} {β : Type v} [Mul β] (f : α → β) : free_magma α → β := sorry /-- Lifts a function `α → β` to an additive magma homomorphism `free_add_magma α → β` given an additive magma `β`. -/ def free_add_magma.lift {α : Type u} {β : Type v} [Add β] (f : α → β) : free_add_magma α → β := sorry namespace free_magma @[simp] theorem Mathlib.free_add_magma.lift_of {α : Type u} {β : Type v} [Add β] (f : α → β) (x : α) : free_add_magma.lift f (free_add_magma.of x) = f x := rfl @[simp] theorem lift_mul {α : Type u} {β : Type v} [Mul β] (f : α → β) (x : free_magma α) (y : free_magma α) : lift f (x * y) = lift f x * lift f y := rfl theorem lift_unique {α : Type u} {β : Type v} [Mul β] (f : free_magma α → β) (hf : ∀ (x y : free_magma α), f (x * y) = f x * f y) : f = lift (f ∘ of) := sorry end free_magma /-- The unique magma homomorphism `free_magma α → free_magma β` that sends each `of x` to `of (f x)`. -/ def free_magma.map {α : Type u} {β : Type v} (f : α → β) : free_magma α → free_magma β := sorry /-- The unique additive magma homomorphism `free_add_magma α → free_add_magma β` that sends each `of x` to `of (f x)`. -/ def free_add_magma.map {α : Type u} {β : Type v} (f : α → β) : free_add_magma α → free_add_magma β := sorry namespace free_magma @[simp] theorem Mathlib.free_add_magma.map_of {α : Type u} {β : Type v} (f : α → β) (x : α) : free_add_magma.map f (free_add_magma.of x) = free_add_magma.of (f x) := rfl @[simp] theorem Mathlib.free_add_magma.map_add {α : Type u} {β : Type v} (f : α → β) (x : free_add_magma α) (y : free_add_magma α) : free_add_magma.map f (x + y) = free_add_magma.map f x + free_add_magma.map f y := rfl protected instance Mathlib.free_add_magma.monad : Monad free_add_magma := sorry /-- Recursor on `free_magma` using `pure` instead of `of`. -/ protected def Mathlib.free_add_magma.rec_on'' {α : Type u} {C : free_add_magma α → Sort l} (x : free_add_magma α) (ih1 : (x : α) → C (pure x)) (ih2 : (x y : free_add_magma α) → C x → C y → C (x + y)) : C x := free_add_magma.rec_on' x ih1 ih2 @[simp] theorem Mathlib.free_add_magma.map_pure {α : Type u} {β : Type u} (f : α → β) (x : α) : f <$> pure x = pure (f x) := rfl @[simp] theorem Mathlib.free_add_magma.map_add' {α : Type u} {β : Type u} (f : α → β) (x : free_add_magma α) (y : free_add_magma α) : f <$> (x + y) = f <$> x + f <$> y := rfl @[simp] theorem Mathlib.free_add_magma.pure_bind {α : Type u} {β : Type u} (f : α → free_add_magma β) (x : α) : pure x >>= f = f x := rfl @[simp] theorem mul_bind {α : Type u} {β : Type u} (f : α → free_magma β) (x : free_magma α) (y : free_magma α) : x * y >>= f = (x >>= f) * (y >>= f) := rfl @[simp] theorem Mathlib.free_add_magma.pure_seq {α : Type u} {β : Type u} {f : α → β} {x : free_add_magma α} : pure f <*> x = f <$> x := rfl @[simp] theorem mul_seq {α : Type u} {β : Type u} {f : free_magma (α → β)} {g : free_magma (α → β)} {x : free_magma α} : f * g <*> x = (f <*> x) * (g <*> x) := rfl protected instance Mathlib.free_add_magma.is_lawful_monad : is_lawful_monad free_add_magma := sorry end free_magma /-- `free_magma` is traversable. -/ protected def free_magma.traverse {m : Type u → Type u} [Applicative m] {α : Type u} {β : Type u} (F : α → m β) : free_magma α → m (free_magma β) := sorry /-- `free_add_magma` is traversable. -/ protected def free_add_magma.traverse {m : Type u → Type u} [Applicative m] {α : Type u} {β : Type u} (F : α → m β) : free_add_magma α → m (free_add_magma β) := sorry namespace free_magma protected instance Mathlib.free_add_magma.traversable : traversable free_add_magma := traversable.mk free_add_magma.traverse @[simp] theorem Mathlib.free_add_magma.traverse_pure {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) (x : α) : traverse F (pure x) = pure <$> F x := rfl @[simp] theorem Mathlib.free_add_magma.traverse_pure' {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) : traverse F ∘ pure = fun (x : α) => pure <$> F x := rfl @[simp] theorem Mathlib.free_add_magma.traverse_add {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) (x : free_add_magma α) (y : free_add_magma α) : traverse F (x + y) = Add.add <$> traverse F x <*> traverse F y := rfl @[simp] theorem Mathlib.free_add_magma.traverse_add' {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) : function.comp (traverse F) ∘ Add.add = fun (x y : free_add_magma α) => Add.add <$> traverse F x <*> traverse F y := rfl @[simp] theorem Mathlib.free_add_magma.traverse_eq {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) (x : free_add_magma α) : free_add_magma.traverse F x = traverse F x := rfl @[simp] theorem mul_map_seq {α : Type u} (x : free_magma α) (y : free_magma α) : Mul.mul <$> x <*> y = x * y := rfl protected instance Mathlib.free_add_magma.is_lawful_traversable : is_lawful_traversable free_add_magma := is_lawful_traversable.mk sorry sorry sorry sorry end free_magma /-- Representation of an element of a free magma. -/ protected def free_magma.repr {α : Type u} [has_repr α] : free_magma α → string := sorry /-- Representation of an element of a free additive magma. -/ protected def free_add_magma.repr {α : Type u} [has_repr α] : free_add_magma α → string := sorry protected instance free_add_magma.has_repr {α : Type u} [has_repr α] : has_repr (free_add_magma α) := has_repr.mk free_add_magma.repr /-- Length of an element of a free magma. -/ def free_magma.length {α : Type u} : free_magma α → ℕ := sorry /-- Length of an element of a free additive magma. -/ def free_add_magma.length {α : Type u} : free_add_magma α → ℕ := sorry /-- Associativity relations for a magma. -/ inductive magma.free_semigroup.r (α : Type u) [Mul α] : α → α → Prop where | intro : ∀ (x y z : α), magma.free_semigroup.r α (x * y * z) (x * (y * z)) | left : ∀ (w x y z : α), magma.free_semigroup.r α (w * (x * y * z)) (w * (x * (y * z))) /-- Associativity relations for an additive magma. -/ inductive add_magma.free_add_semigroup.r (α : Type u) [Add α] : α → α → Prop where | intro : ∀ (x y z : α), add_magma.free_add_semigroup.r α (x + y + z) (x + (y + z)) | left : ∀ (w x y z : α), add_magma.free_add_semigroup.r α (w + (x + y + z)) (w + (x + (y + z))) namespace magma /-- Free semigroup over a magma. -/ def free_semigroup (α : Type u) [Mul α] := Quot sorry namespace free_semigroup /-- Embedding from magma to its free semigroup. -/ def Mathlib.add_magma.free_add_semigroup.of {α : Type u} [Add α] : α → add_magma.free_add_semigroup α := Quot.mk (add_magma.free_add_semigroup.r α) protected instance Mathlib.add_magma.free_add_semigroup.inhabited {α : Type u} [Add α] [Inhabited α] : Inhabited (add_magma.free_add_semigroup α) := { default := add_magma.free_add_semigroup.of Inhabited.default } protected theorem Mathlib.add_magma.free_add_semigroup.induction_on {α : Type u} [Add α] {C : add_magma.free_add_semigroup α → Prop} (x : add_magma.free_add_semigroup α) (ih : ∀ (x : α), C (add_magma.free_add_semigroup.of x)) : C x := quot.induction_on x ih theorem of_mul_assoc {α : Type u} [Mul α] (x : α) (y : α) (z : α) : of (x * y * z) = of (x * (y * z)) := quot.sound (r.intro x y z) theorem of_mul_assoc_left {α : Type u} [Mul α] (w : α) (x : α) (y : α) (z : α) : of (w * (x * y * z)) = of (w * (x * (y * z))) := quot.sound (r.left w x y z) theorem of_mul_assoc_right {α : Type u} [Mul α] (w : α) (x : α) (y : α) (z : α) : of (w * x * y * z) = of (w * (x * y) * z) := sorry protected instance semigroup {α : Type u} [Mul α] : semigroup (free_semigroup α) := semigroup.mk (fun (x y : free_semigroup α) => quot.lift_on x (fun (p : α) => quot.lift_on y (fun (q : α) => Quot.mk (r α) (p * q)) sorry) sorry) sorry theorem Mathlib.add_magma.free_add_semigroup.of_add {α : Type u} [Add α] (x : α) (y : α) : add_magma.free_add_semigroup.of (x + y) = add_magma.free_add_semigroup.of x + add_magma.free_add_semigroup.of y := rfl /-- Lifts a magma homomorphism `α → β` to a semigroup homomorphism `magma.free_semigroup α → β` given a semigroup `β`. -/ def lift {α : Type u} [Mul α] {β : Type v} [semigroup β] (f : α → β) (hf : ∀ (x y : α), f (x * y) = f x * f y) : free_semigroup α → β := Quot.lift f sorry @[simp] theorem lift_of {α : Type u} [Mul α] {β : Type v} [semigroup β] (f : α → β) {hf : ∀ (x y : α), f (x * y) = f x * f y} (x : α) : lift f hf (of x) = f x := rfl @[simp] theorem lift_mul {α : Type u} [Mul α] {β : Type v} [semigroup β] (f : α → β) {hf : ∀ (x y : α), f (x * y) = f x * f y} (x : free_semigroup α) (y : free_semigroup α) : lift f hf (x * y) = lift f hf x * lift f hf y := quot.induction_on x fun (p : α) => quot.induction_on y fun (q : α) => hf p q theorem Mathlib.add_magma.free_add_semigroup.lift_unique {α : Type u} [Add α] {β : Type v} [add_semigroup β] (f : add_magma.free_add_semigroup α → β) (hf : ∀ (x y : add_magma.free_add_semigroup α), f (x + y) = f x + f y) : f = add_magma.free_add_semigroup.lift (f ∘ add_magma.free_add_semigroup.of) fun (p q : α) => hf (add_magma.free_add_semigroup.of p) (add_magma.free_add_semigroup.of q) := funext fun (x : add_magma.free_add_semigroup α) => quot.induction_on x fun (p : α) => rfl /-- From a magma homomorphism `α → β` to a semigroup homomorphism `magma.free_semigroup α → magma.free_semigroup β`. -/ def Mathlib.add_magma.free_add_semigroup.map {α : Type u} [Add α] {β : Type v} [Add β] (f : α → β) (hf : ∀ (x y : α), f (x + y) = f x + f y) : add_magma.free_add_semigroup α → add_magma.free_add_semigroup β := add_magma.free_add_semigroup.lift (add_magma.free_add_semigroup.of ∘ f) sorry @[simp] theorem Mathlib.add_magma.free_add_semigroup.map_of {α : Type u} [Add α] {β : Type v} [Add β] (f : α → β) {hf : ∀ (x y : α), f (x + y) = f x + f y} (x : α) : add_magma.free_add_semigroup.map f hf (add_magma.free_add_semigroup.of x) = add_magma.free_add_semigroup.of (f x) := rfl @[simp] theorem map_mul {α : Type u} [Mul α] {β : Type v} [Mul β] (f : α → β) {hf : ∀ (x y : α), f (x * y) = f x * f y} (x : free_semigroup α) (y : free_semigroup α) : map f hf (x * y) = map f hf x * map f hf y := lift_mul (of ∘ f) x y end free_semigroup end magma /-- Free semigroup over a given alphabet. (Note: In this definition, the free semigroup does not contain the empty word.) -/ def free_semigroup (α : Type u) := α × List α namespace free_semigroup protected instance semigroup {α : Type u} : semigroup (free_semigroup α) := semigroup.mk (fun (L1 L2 : free_semigroup α) => (prod.fst L1, prod.snd L1 ++ prod.fst L2 :: prod.snd L2)) sorry /-- The embedding `α → free_semigroup α`. -/ def Mathlib.free_add_semigroup.of {α : Type u} (x : α) : free_add_semigroup α := (x, []) protected instance Mathlib.free_add_semigroup.inhabited {α : Type u} [Inhabited α] : Inhabited (free_add_semigroup α) := { default := free_add_semigroup.of Inhabited.default } /-- Recursor for free semigroup using `of` and `*`. -/ protected def Mathlib.free_add_semigroup.rec_on {α : Type u} {C : free_add_semigroup α → Sort l} (x : free_add_semigroup α) (ih1 : (x : α) → C (free_add_semigroup.of x)) (ih2 : (x : α) → (y : free_add_semigroup α) → C (free_add_semigroup.of x) → C y → C (free_add_semigroup.of x + y)) : C x := prod.rec_on x fun (f : α) (s : List α) => list.rec_on s ih1 (fun (hd : α) (tl : List α) (ih : (_a : α) → C (_a, tl)) (f : α) => ih2 f (hd, tl) (ih1 f) (ih hd)) f end free_semigroup /-- Auxiliary function for `free_semigroup.lift`. -/ def free_semigroup.lift' {α : Type u} {β : Type v} [semigroup β] (f : α → β) : α → List α → β := sorry /-- Auxiliary function for `free_semigroup.lift`. -/ def free_add_semigroup.lift' {α : Type u} {β : Type v} [add_semigroup β] (f : α → β) : α → List α → β := sorry namespace free_semigroup /-- Lifts a function `α → β` to a semigroup homomorphism `free_semigroup α → β` given a semigroup `β`. -/ def lift {α : Type u} {β : Type v} [semigroup β] (f : α → β) (x : free_semigroup α) : β := lift' f (prod.fst x) (prod.snd x) @[simp] theorem lift_of {α : Type u} {β : Type v} [semigroup β] (f : α → β) (x : α) : lift f (of x) = f x := rfl theorem lift_of_mul {α : Type u} {β : Type v} [semigroup β] (f : α → β) (x : α) (y : free_semigroup α) : lift f (of x * y) = f x * lift f y := rfl @[simp] theorem Mathlib.free_add_semigroup.lift_add {α : Type u} {β : Type v} [add_semigroup β] (f : α → β) (x : free_add_semigroup α) (y : free_add_semigroup α) : free_add_semigroup.lift f (x + y) = free_add_semigroup.lift f x + free_add_semigroup.lift f y := sorry theorem Mathlib.free_add_semigroup.lift_unique {α : Type u} {β : Type v} [add_semigroup β] (f : free_add_semigroup α → β) (hf : ∀ (x y : free_add_semigroup α), f (x + y) = f x + f y) : f = free_add_semigroup.lift (f ∘ free_add_semigroup.of) := sorry /-- The unique semigroup homomorphism that sends `of x` to `of (f x)`. -/ def Mathlib.free_add_semigroup.map {α : Type u} {β : Type v} (f : α → β) : free_add_semigroup α → free_add_semigroup β := free_add_semigroup.lift (free_add_semigroup.of ∘ f) @[simp] theorem Mathlib.free_add_semigroup.map_of {α : Type u} {β : Type v} (f : α → β) (x : α) : free_add_semigroup.map f (free_add_semigroup.of x) = free_add_semigroup.of (f x) := rfl @[simp] theorem Mathlib.free_add_semigroup.map_add {α : Type u} {β : Type v} (f : α → β) (x : free_add_semigroup α) (y : free_add_semigroup α) : free_add_semigroup.map f (x + y) = free_add_semigroup.map f x + free_add_semigroup.map f y := free_add_semigroup.lift_add (free_add_semigroup.of ∘ f) x y protected instance Mathlib.free_add_semigroup.monad : Monad free_add_semigroup := sorry /-- Recursor that uses `pure` instead of `of`. -/ def rec_on' {α : Type u} {C : free_semigroup α → Sort l} (x : free_semigroup α) (ih1 : (x : α) → C (pure x)) (ih2 : (x : α) → (y : free_semigroup α) → C (pure x) → C y → C (pure x * y)) : C x := free_semigroup.rec_on x ih1 ih2 @[simp] theorem map_pure {α : Type u} {β : Type u} (f : α → β) (x : α) : f <$> pure x = pure (f x) := rfl @[simp] theorem map_mul' {α : Type u} {β : Type u} (f : α → β) (x : free_semigroup α) (y : free_semigroup α) : f <$> (x * y) = f <$> x * f <$> y := map_mul f x y @[simp] theorem pure_bind {α : Type u} {β : Type u} (f : α → free_semigroup β) (x : α) : pure x >>= f = f x := rfl @[simp] theorem mul_bind {α : Type u} {β : Type u} (f : α → free_semigroup β) (x : free_semigroup α) (y : free_semigroup α) : x * y >>= f = (x >>= f) * (y >>= f) := lift_mul f x y @[simp] theorem Mathlib.free_add_semigroup.pure_seq {α : Type u} {β : Type u} {f : α → β} {x : free_add_semigroup α} : pure f <*> x = f <$> x := rfl @[simp] theorem mul_seq {α : Type u} {β : Type u} {f : free_semigroup (α → β)} {g : free_semigroup (α → β)} {x : free_semigroup α} : f * g <*> x = (f <*> x) * (g <*> x) := mul_bind (fun (_x : α → β) => (fun (α β : Type u) (f : α → β) (x : free_semigroup α) => lift (of ∘ f) x) α β _x x) f g protected instance Mathlib.free_add_semigroup.is_lawful_monad : is_lawful_monad free_add_semigroup := sorry /-- `free_semigroup` is traversable. -/ protected def Mathlib.free_add_semigroup.traverse {m : Type u → Type u} [Applicative m] {α : Type u} {β : Type u} (F : α → m β) (x : free_add_semigroup α) : m (free_add_semigroup β) := free_add_semigroup.rec_on' x (fun (x : α) => pure <$> F x) fun (x : α) (y : free_add_semigroup α) (ihx ihy : m (free_add_semigroup β)) => Add.add <$> ihx <*> ihy protected instance Mathlib.free_add_semigroup.traversable : traversable free_add_semigroup := traversable.mk free_add_semigroup.traverse @[simp] theorem traverse_pure {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) (x : α) : traverse F (pure x) = pure <$> F x := rfl @[simp] theorem Mathlib.free_add_semigroup.traverse_pure' {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) : traverse F ∘ pure = fun (x : α) => pure <$> F x := rfl @[simp] theorem Mathlib.free_add_semigroup.traverse_add {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) [is_lawful_applicative m] (x : free_add_semigroup α) (y : free_add_semigroup α) : traverse F (x + y) = Add.add <$> traverse F x <*> traverse F y := sorry @[simp] theorem Mathlib.free_add_semigroup.traverse_add' {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) [is_lawful_applicative m] : function.comp (traverse F) ∘ Add.add = fun (x y : free_add_semigroup α) => Add.add <$> traverse F x <*> traverse F y := funext fun (x : free_add_semigroup α) => funext fun (y : free_add_semigroup α) => free_add_semigroup.traverse_add F x y @[simp] theorem Mathlib.free_add_semigroup.traverse_eq {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) (x : free_add_semigroup α) : free_add_semigroup.traverse F x = traverse F x := rfl @[simp] theorem Mathlib.free_add_semigroup.add_map_seq {α : Type u} (x : free_add_semigroup α) (y : free_add_semigroup α) : Add.add <$> x <*> y = x + y := rfl protected instance Mathlib.free_add_semigroup.is_lawful_traversable : is_lawful_traversable free_add_semigroup := is_lawful_traversable.mk sorry sorry sorry sorry protected instance Mathlib.free_add_semigroup.decidable_eq {α : Type u} [DecidableEq α] : DecidableEq (free_add_semigroup α) := prod.decidable_eq end free_semigroup /-- Isomorphism between `magma.free_semigroup (free_magma α)` and `free_semigroup α`. -/ def free_add_semigroup_free_add_magma (α : Type u) : add_magma.free_add_semigroup (free_add_magma α) ≃ free_add_semigroup α := equiv.mk (add_magma.free_add_semigroup.lift (free_add_magma.lift free_add_semigroup.of) sorry) (free_add_semigroup.lift (add_magma.free_add_semigroup.of ∘ free_add_magma.of)) sorry sorry @[simp] theorem free_semigroup_free_magma_mul {α : Type u} (x : magma.free_semigroup (free_magma α)) (y : magma.free_semigroup (free_magma α)) : coe_fn (free_semigroup_free_magma α) (x * y) = coe_fn (free_semigroup_free_magma α) x * coe_fn (free_semigroup_free_magma α) y := magma.free_semigroup.lift_mul (free_magma.lift free_semigroup.of) x y
77a36b1ef21d50bf40929be36d4c7cba4208e863
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Elab/Print.lean
8e22327e1d1dcaf077c949b38fa0bd6ae8376214
[ "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
5,988
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.FoldConsts import Lean.Elab.Command namespace Lean.Elab.Command private def throwUnknownId (id : Name) : CommandElabM Unit := throwError "unknown identifier '{mkConst id}'" private def levelParamsToMessageData (levelParams : List Name) : MessageData := match levelParams with | [] => "" | u::us => Id.run do let mut m := m!".\{{u}" for u in us do m := m ++ ", " ++ toMessageData u return m ++ "}" private def mkHeader (kind : String) (id : Name) (levelParams : List Name) (type : Expr) (safety : DefinitionSafety) : CommandElabM MessageData := do let m : MessageData := match (← getReducibilityStatus id) with | ReducibilityStatus.irreducible => "@[irreducible] " | ReducibilityStatus.reducible => "@[reducible] " | ReducibilityStatus.semireducible => "" let m := m ++ match safety with | DefinitionSafety.unsafe => "unsafe " | DefinitionSafety.partial => "partial " | DefinitionSafety.safe => "" let m := if isProtected (← getEnv) id then m ++ "protected " else m let (m, id) := match privateToUserName? id with | some id => (m ++ "private ", id) | none => (m, id) let m := m ++ kind ++ " " ++ id ++ levelParamsToMessageData levelParams ++ " : " ++ type pure m private def mkHeader' (kind : String) (id : Name) (levelParams : List Name) (type : Expr) (isUnsafe : Bool) : CommandElabM MessageData := mkHeader kind id levelParams type (if isUnsafe then DefinitionSafety.unsafe else DefinitionSafety.safe) private def printDefLike (kind : String) (id : Name) (levelParams : List Name) (type : Expr) (value : Expr) (safety := DefinitionSafety.safe) : CommandElabM Unit := do let m ← mkHeader kind id levelParams type safety let m := m ++ " :=" ++ Format.line ++ value logInfo m private def printAxiomLike (kind : String) (id : Name) (levelParams : List Name) (type : Expr) (isUnsafe := false) : CommandElabM Unit := do logInfo (← mkHeader' kind id levelParams type isUnsafe) private def printQuot (id : Name) (levelParams : List Name) (type : Expr) : CommandElabM Unit := do printAxiomLike "Quotient primitive" id levelParams type private def printInduct (id : Name) (levelParams : List Name) (numParams : Nat) (type : Expr) (ctors : List Name) (isUnsafe : Bool) : CommandElabM Unit := do let mut m ← mkHeader' "inductive" id levelParams type isUnsafe m := m ++ Format.line ++ "number of parameters: " ++ toString numParams m := m ++ Format.line ++ "constructors:" for ctor in ctors do let cinfo ← getConstInfo ctor m := m ++ Format.line ++ ctor ++ " : " ++ cinfo.type logInfo m private def printIdCore (id : Name) : CommandElabM Unit := do match (← getEnv).find? id with | ConstantInfo.axiomInfo { levelParams := us, type := t, isUnsafe := u, .. } => printAxiomLike "axiom" id us t u | ConstantInfo.defnInfo { levelParams := us, type := t, value := v, safety := s, .. } => printDefLike "def" id us t v s | ConstantInfo.thmInfo { levelParams := us, type := t, value := v, .. } => printDefLike "theorem" id us t v | ConstantInfo.opaqueInfo { levelParams := us, type := t, isUnsafe := u, .. } => printAxiomLike "opaque" id us t u | ConstantInfo.quotInfo { levelParams := us, type := t, .. } => printQuot id us t | ConstantInfo.ctorInfo { levelParams := us, type := t, isUnsafe := u, .. } => printAxiomLike "constructor" id us t u | ConstantInfo.recInfo { levelParams := us, type := t, isUnsafe := u, .. } => printAxiomLike "recursor" id us t u | ConstantInfo.inductInfo { levelParams := us, numParams, type := t, ctors, isUnsafe := u, .. } => printInduct id us numParams t ctors u | none => throwUnknownId id private def printId (id : Syntax) : CommandElabM Unit := do addCompletionInfo <| CompletionInfo.id id id.getId (danglingDot := false) {} none let cs ← resolveGlobalConstWithInfos id cs.forM printIdCore @[builtin_command_elab «print»] def elabPrint : CommandElab | `(#print%$tk $id:ident) => withRef tk <| printId id | `(#print%$tk $s:str) => logInfoAt tk s.getString | _ => throwError "invalid #print command" namespace CollectAxioms structure State where visited : NameSet := {} axioms : Array Name := #[] abbrev M := ReaderT Environment $ StateM State partial def collect (c : Name) : M Unit := do let collectExpr (e : Expr) : M Unit := e.getUsedConstants.forM collect let s ← get unless s.visited.contains c do modify fun s => { s with visited := s.visited.insert c } let env ← read match env.find? c with | some (ConstantInfo.axiomInfo _) => modify fun s => { s with axioms := s.axioms.push c } | some (ConstantInfo.defnInfo v) => collectExpr v.type *> collectExpr v.value | some (ConstantInfo.thmInfo v) => collectExpr v.type *> collectExpr v.value | some (ConstantInfo.opaqueInfo v) => collectExpr v.type *> collectExpr v.value | some (ConstantInfo.quotInfo _) => pure () | some (ConstantInfo.ctorInfo v) => collectExpr v.type | some (ConstantInfo.recInfo v) => collectExpr v.type | some (ConstantInfo.inductInfo v) => collectExpr v.type *> v.ctors.forM collect | none => pure () end CollectAxioms private def printAxiomsOf (constName : Name) : CommandElabM Unit := do let env ← getEnv let (_, s) := ((CollectAxioms.collect constName).run env).run {} if s.axioms.isEmpty then logInfo m!"'{constName}' does not depend on any axioms" else logInfo m!"'{constName}' depends on axioms: {s.axioms.toList}" @[builtin_command_elab «printAxioms»] def elabPrintAxioms : CommandElab | `(#print%$tk axioms $id) => withRef tk do let cs ← resolveGlobalConstWithInfos id cs.forM printAxiomsOf | _ => throwUnsupportedSyntax end Lean.Elab.Command
81a16e27ceccc8917d6cae0614ff7fbcf1cb7c9c
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/category/Module/abelian.lean
788ce6e479ca93c64f985e627e4b9b5cdece5a2b
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
3,291
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import algebra.category.Module.kernels import algebra.category.Module.limits import category_theory.abelian.exact /-! # The category of left R-modules is abelian. Additionally, two linear maps are exact in the categorical sense iff `range f = ker g`. -/ open category_theory open category_theory.limits noncomputable theory universes v u namespace Module variables {R : Type u} [ring R] {M N : Module.{v} R} (f : M ⟶ N) /-- In the category of modules, every monomorphism is normal. -/ def normal_mono (hf : mono f) : normal_mono f := { Z := of R f.range.quotient, g := f.range.mkq, w := linear_map.range_mkq_comp _, is_limit := is_kernel.iso_kernel _ _ (kernel_is_limit _) /- The following [invalid Lean code](https://github.com/leanprover-community/lean/issues/341) might help you understand what's going on here: ``` calc M ≃ₗ[R] f.ker.quotient : (submodule.quot_equiv_of_eq_bot _ (ker_eq_bot_of_mono _)).symm ... ≃ₗ[R] f.range : linear_map.quot_ker_equiv_range f ... ≃ₗ[R] r.range.mkq.ker : linear_equiv.of_eq _ _ (submodule.ker_mkq _).symm ``` -/ (linear_equiv.to_Module_iso' (linear_equiv.trans (submodule.quot_equiv_of_eq_bot _ (ker_eq_bot_of_mono _)).symm (linear_equiv.trans (linear_map.quot_ker_equiv_range f) (linear_equiv.of_eq _ _ (submodule.ker_mkq _).symm)))) $ by { ext, refl } } /-- In the category of modules, every epimorphism is normal. -/ def normal_epi (hf : epi f) : normal_epi f := { W := of R f.ker, g := f.ker.subtype, w := linear_map.comp_ker_subtype _, is_colimit := is_cokernel.cokernel_iso _ _ (cokernel_is_colimit _) (linear_equiv.to_Module_iso' /- The following invalid Lean code might help you understand what's going on here: ``` calc f.ker.subtype.range.quotient ≃ₗ[R] f.ker.quotient : submodule.quot_equiv_of_eq _ _ (submodule.range_subtype _) ... ≃ₗ[R] f.range : linear_map.quot_ker_equiv_range f ... ≃ₗ[R] N : linear_equiv.of_top _ (range_eq_top_of_epi _) ``` -/ (linear_equiv.trans (linear_equiv.trans (submodule.quot_equiv_of_eq _ _ (submodule.range_subtype _)) (linear_map.quot_ker_equiv_range f)) (linear_equiv.of_top _ (range_eq_top_of_epi _)))) $ by { ext, refl } } /-- The category of R-modules is abelian. -/ instance : abelian (Module R) := { has_finite_products := ⟨by apply_instance⟩, has_kernels := by apply_instance, has_cokernels := has_cokernels_Module, normal_mono := λ X Y, normal_mono, normal_epi := λ X Y, normal_epi } variables {O : Module.{v} R} (g : N ⟶ O) open linear_map local attribute [instance] preadditive.has_equalizers_of_has_kernels theorem exact_iff : exact f g ↔ f.range = g.ker := begin rw abelian.exact_iff' f g (kernel_is_limit _) (cokernel_is_colimit _), exact ⟨λ h, le_antisymm (range_le_ker_iff.2 h.1) (ker_le_range_iff.2 h.2), λ h, ⟨range_le_ker_iff.1 $ le_of_eq h, ker_le_range_iff.1 $ le_of_eq h.symm⟩⟩ end end Module
e32be0514b1b807fc2fe68cfae914514a0244af9
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/algebra/continued_fractions/default.lean
fc9fd8b946a80959bf689e480249b34887fcf243
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
371
lean
/- Copyright (c) 2019 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import algebra.continued_fractions.continuants_recurrence import algebra.continued_fractions.terminated_stable import algebra.continued_fractions.convergents_equiv /-! # Default Exports for Continued Fractions -/
ec2b98435aff1a2c3e1b9e72a1cf96da318a0a6a
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/multiset/erase_dup.lean
a9c997940fbf8e37eaf9f77ece1e4e7c54527b1a
[]
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
4,292
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.multiset.nodup import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Erasing duplicates in a multiset. -/ namespace multiset /-! ### erase_dup -/ /-- `erase_dup s` removes duplicates from `s`, yielding a `nodup` multiset. -/ def erase_dup {α : Type u_1} [DecidableEq α] (s : multiset α) : multiset α := quot.lift_on s (fun (l : List α) => ↑(list.erase_dup l)) sorry @[simp] theorem coe_erase_dup {α : Type u_1} [DecidableEq α] (l : List α) : erase_dup ↑l = ↑(list.erase_dup l) := rfl @[simp] theorem erase_dup_zero {α : Type u_1} [DecidableEq α] : erase_dup 0 = 0 := rfl @[simp] theorem mem_erase_dup {α : Type u_1} [DecidableEq α] {a : α} {s : multiset α} : a ∈ erase_dup s ↔ a ∈ s := quot.induction_on s fun (l : List α) => list.mem_erase_dup @[simp] theorem erase_dup_cons_of_mem {α : Type u_1} [DecidableEq α] {a : α} {s : multiset α} : a ∈ s → erase_dup (a ::ₘ s) = erase_dup s := quot.induction_on s fun (l : List α) (m : a ∈ Quot.mk setoid.r l) => congr_arg coe (list.erase_dup_cons_of_mem m) @[simp] theorem erase_dup_cons_of_not_mem {α : Type u_1} [DecidableEq α] {a : α} {s : multiset α} : ¬a ∈ s → erase_dup (a ::ₘ s) = a ::ₘ erase_dup s := quot.induction_on s fun (l : List α) (m : ¬a ∈ Quot.mk setoid.r l) => congr_arg coe (list.erase_dup_cons_of_not_mem m) theorem erase_dup_le {α : Type u_1} [DecidableEq α] (s : multiset α) : erase_dup s ≤ s := quot.induction_on s fun (l : List α) => list.sublist.subperm (list.erase_dup_sublist l) theorem erase_dup_subset {α : Type u_1} [DecidableEq α] (s : multiset α) : erase_dup s ⊆ s := subset_of_le (erase_dup_le s) theorem subset_erase_dup {α : Type u_1} [DecidableEq α] (s : multiset α) : s ⊆ erase_dup s := fun (a : α) => iff.mpr mem_erase_dup @[simp] theorem erase_dup_subset' {α : Type u_1} [DecidableEq α] {s : multiset α} {t : multiset α} : erase_dup s ⊆ t ↔ s ⊆ t := { mp := subset.trans (subset_erase_dup s), mpr := subset.trans (erase_dup_subset s) } @[simp] theorem subset_erase_dup' {α : Type u_1} [DecidableEq α] {s : multiset α} {t : multiset α} : s ⊆ erase_dup t ↔ s ⊆ t := { mp := fun (h : s ⊆ erase_dup t) => subset.trans h (erase_dup_subset t), mpr := fun (h : s ⊆ t) => subset.trans h (subset_erase_dup t) } @[simp] theorem nodup_erase_dup {α : Type u_1} [DecidableEq α] (s : multiset α) : nodup (erase_dup s) := quot.induction_on s list.nodup_erase_dup theorem erase_dup_eq_self {α : Type u_1} [DecidableEq α] {s : multiset α} : erase_dup s = s ↔ nodup s := sorry theorem erase_dup_eq_zero {α : Type u_1} [DecidableEq α] {s : multiset α} : erase_dup s = 0 ↔ s = 0 := { mp := fun (h : erase_dup s = 0) => eq_zero_of_subset_zero (h ▸ subset_erase_dup s), mpr := fun (h : s = 0) => Eq.symm h ▸ erase_dup_zero } @[simp] theorem erase_dup_singleton {α : Type u_1} [DecidableEq α] {a : α} : erase_dup (a ::ₘ 0) = a ::ₘ 0 := iff.mpr erase_dup_eq_self (nodup_singleton a) theorem le_erase_dup {α : Type u_1} [DecidableEq α] {s : multiset α} {t : multiset α} : s ≤ erase_dup t ↔ s ≤ t ∧ nodup s := sorry theorem erase_dup_ext {α : Type u_1} [DecidableEq α] {s : multiset α} {t : multiset α} : erase_dup s = erase_dup t ↔ ∀ (a : α), a ∈ s ↔ a ∈ t := sorry theorem erase_dup_map_erase_dup_eq {α : Type u_1} {β : Type u_2} [DecidableEq α] [DecidableEq β] (f : α → β) (s : multiset α) : erase_dup (map f (erase_dup s)) = erase_dup (map f s) := sorry @[simp] theorem erase_dup_nsmul {α : Type u_1} [DecidableEq α] {s : multiset α} {n : ℕ} (h0 : n ≠ 0) : erase_dup (n •ℕ s) = erase_dup s := sorry theorem nodup.le_erase_dup_iff_le {α : Type u_1} [DecidableEq α] {s : multiset α} {t : multiset α} (hno : nodup s) : s ≤ erase_dup t ↔ s ≤ t := sorry end multiset theorem multiset.nodup.le_nsmul_iff_le {α : Type u_1} {s : multiset α} {t : multiset α} {n : ℕ} (h : multiset.nodup s) (hn : n ≠ 0) : s ≤ n •ℕ t ↔ s ≤ t := sorry
4bb668b052218a646417d1731838ba0947afc03d
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/sheaves/sheaf_condition/pairwise_intersections.lean
76b0373c9bb67dcd9bf3fd5d39150e9c24042f5a
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,916
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import topology.sheaves.sheaf import category_theory.limits.preserves.basic import category_theory.category.pairwise /-! # Equivalent formulations of the sheaf condition We give an equivalent formulation of the sheaf condition. Given any indexed type `ι`, we define `overlap ι`, a category with objects corresponding to * individual open sets, `single i`, and * intersections of pairs of open sets, `pair i j`, with morphisms from `pair i j` to both `single i` and `single j`. Any open cover `U : ι → opens X` provides a functor `diagram U : overlap ι ⥤ (opens X)ᵒᵖ`. There is a canonical cone over this functor, `cone U`, whose cone point is `supr U`, and in fact this is a limit cone. A presheaf `F : presheaf C X` is a sheaf precisely if it preserves this limit. We express this in two equivalent ways, as * `is_limit (F.map_cone (cone U))`, or * `preserves_limit (diagram U) F` -/ noncomputable theory universes v u open topological_space open Top open opposite open category_theory open category_theory.limits namespace Top.presheaf variables {X : Top.{v}} variables {C : Type u} [category.{v} C] /-- An alternative formulation of the sheaf condition (which we prove equivalent to the usual one below as `is_sheaf_iff_is_sheaf_pairwise_intersections`). A presheaf is a sheaf if `F` sends the cone `(pairwise.cocone U).op` to a limit cone. (Recall `pairwise.cocone U` has cone point `supr U`, mapping down to the `U i` and the `U i ⊓ U j`.) -/ def is_sheaf_pairwise_intersections (F : presheaf C X) : Prop := ∀ ⦃ι : Type v⦄ (U : ι → opens X), nonempty (is_limit (F.map_cone (pairwise.cocone U).op)) /-- An alternative formulation of the sheaf condition (which we prove equivalent to the usual one below as `is_sheaf_iff_is_sheaf_preserves_limit_pairwise_intersections`). A presheaf is a sheaf if `F` preserves the limit of `pairwise.diagram U`. (Recall `pairwise.diagram U` is the diagram consisting of the pairwise intersections `U i ⊓ U j` mapping into the open sets `U i`. This diagram has limit `supr U`.) -/ def is_sheaf_preserves_limit_pairwise_intersections (F : presheaf C X) : Prop := ∀ ⦃ι : Type v⦄ (U : ι → opens X), nonempty (preserves_limit (pairwise.diagram U).op F) /-! The remainder of this file shows that these conditions are equivalent to the usual sheaf condition. -/ variables [has_products C] namespace sheaf_condition_pairwise_intersections open category_theory.pairwise category_theory.pairwise.hom open sheaf_condition_equalizer_products /-- Implementation of `sheaf_condition_pairwise_intersections.cone_equiv`. -/ @[simps] def cone_equiv_functor_obj (F : presheaf C X) ⦃ι : Type v⦄ (U : ι → opens ↥X) (c : limits.cone ((diagram U).op ⋙ F)) : limits.cone (sheaf_condition_equalizer_products.diagram F U) := { X := c.X, π := { app := λ Z, walking_parallel_pair.cases_on Z (pi.lift (λ (i : ι), c.π.app (op (single i)))) (pi.lift (λ (b : ι × ι), c.π.app (op (pair b.1 b.2)))), naturality' := λ Y Z f, begin cases Y; cases Z; cases f, { ext i, dsimp, simp only [limit.lift_π, category.id_comp, fan.mk_π_app, category_theory.functor.map_id, category.assoc], dsimp, simp only [limit.lift_π, category.id_comp, fan.mk_π_app], }, { ext ⟨i, j⟩, dsimp [sheaf_condition_equalizer_products.left_res], simp only [limit.lift_π, limit.lift_π_assoc, category.id_comp, fan.mk_π_app, category.assoc], have h := c.π.naturality (quiver.hom.op (hom.left i j)), dsimp at h, simpa using h, }, { ext ⟨i, j⟩, dsimp [sheaf_condition_equalizer_products.right_res], simp only [limit.lift_π, limit.lift_π_assoc, category.id_comp, fan.mk_π_app, category.assoc], have h := c.π.naturality (quiver.hom.op (hom.right i j)), dsimp at h, simpa using h, }, { ext i, dsimp, simp only [limit.lift_π, category.id_comp, fan.mk_π_app, category_theory.functor.map_id, category.assoc], dsimp, simp only [limit.lift_π, category.id_comp, fan.mk_π_app], }, end, }, } section local attribute [tidy] tactic.case_bash /-- Implementation of `sheaf_condition_pairwise_intersections.cone_equiv`. -/ @[simps] def cone_equiv_functor (F : presheaf C X) ⦃ι : Type v⦄ (U : ι → opens ↥X) : limits.cone ((diagram U).op ⋙ F) ⥤ limits.cone (sheaf_condition_equalizer_products.diagram F U) := { obj := λ c, cone_equiv_functor_obj F U c, map := λ c c' f, { hom := f.hom, w' := λ j, begin cases j; { ext, simp only [limits.fan.mk_π_app, limits.cone_morphism.w, limits.limit.lift_π, category.assoc, cone_equiv_functor_obj_π_app], }, end }, }. end /-- Implementation of `sheaf_condition_pairwise_intersections.cone_equiv`. -/ @[simps] def cone_equiv_inverse_obj (F : presheaf C X) ⦃ι : Type v⦄ (U : ι → opens ↥X) (c : limits.cone (sheaf_condition_equalizer_products.diagram F U)) : limits.cone ((diagram U).op ⋙ F) := { X := c.X, π := { app := begin intro x, induction x using opposite.rec, rcases x with (⟨i⟩|⟨i,j⟩), { exact c.π.app (walking_parallel_pair.zero) ≫ pi.π _ i, }, { exact c.π.app (walking_parallel_pair.one) ≫ pi.π _ (i, j), } end, naturality' := begin intros x y f, induction x using opposite.rec, induction y using opposite.rec, have ef : f = f.unop.op := rfl, revert ef, generalize : f.unop = f', rintro rfl, rcases x with ⟨i⟩|⟨⟩; rcases y with ⟨⟩|⟨j,j⟩; rcases f' with ⟨⟩, { dsimp, erw [F.map_id], simp, }, { dsimp, simp only [category.id_comp, category.assoc], have h := c.π.naturality (walking_parallel_pair_hom.left), dsimp [sheaf_condition_equalizer_products.left_res] at h, simp only [category.id_comp] at h, have h' := h =≫ pi.π _ (i, j), rw h', simp, refl, }, { dsimp, simp only [category.id_comp, category.assoc], have h := c.π.naturality (walking_parallel_pair_hom.right), dsimp [sheaf_condition_equalizer_products.right_res] at h, simp only [category.id_comp] at h, have h' := h =≫ pi.π _ (j, i), rw h', simp, refl, }, { dsimp, erw [F.map_id], simp, }, end, }, } /-- Implementation of `sheaf_condition_pairwise_intersections.cone_equiv`. -/ @[simps] def cone_equiv_inverse (F : presheaf C X) ⦃ι : Type v⦄ (U : ι → opens ↥X) : limits.cone (sheaf_condition_equalizer_products.diagram F U) ⥤ limits.cone ((diagram U).op ⋙ F) := { obj := λ c, cone_equiv_inverse_obj F U c, map := λ c c' f, { hom := f.hom, w' := begin intro x, induction x using opposite.rec, rcases x with (⟨i⟩|⟨i,j⟩), { dsimp, rw [←(f.w walking_parallel_pair.zero), category.assoc], }, { dsimp, rw [←(f.w walking_parallel_pair.one), category.assoc], }, end }, }. /-- Implementation of `sheaf_condition_pairwise_intersections.cone_equiv`. -/ @[simps] def cone_equiv_unit_iso_app (F : presheaf C X) ⦃ι : Type v⦄ (U : ι → opens ↥X) (c : cone ((diagram U).op ⋙ F)) : (𝟭 (cone ((diagram U).op ⋙ F))).obj c ≅ (cone_equiv_functor F U ⋙ cone_equiv_inverse F U).obj c := { hom := { hom := 𝟙 _, w' := λ j, begin induction j using opposite.rec, rcases j; { dsimp, simp only [limits.fan.mk_π_app, category.id_comp, limits.limit.lift_π], } end, }, inv := { hom := 𝟙 _, w' := λ j, begin induction j using opposite.rec, rcases j; { dsimp, simp only [limits.fan.mk_π_app, category.id_comp, limits.limit.lift_π], } end }, hom_inv_id' := begin ext, simp only [category.comp_id, limits.cone.category_comp_hom, limits.cone.category_id_hom], end, inv_hom_id' := begin ext, simp only [category.comp_id, limits.cone.category_comp_hom, limits.cone.category_id_hom], end, } /-- Implementation of `sheaf_condition_pairwise_intersections.cone_equiv`. -/ @[simps] def cone_equiv_unit_iso (F : presheaf C X) ⦃ι : Type v⦄ (U : ι → opens X) : 𝟭 (limits.cone ((diagram U).op ⋙ F)) ≅ cone_equiv_functor F U ⋙ cone_equiv_inverse F U := nat_iso.of_components (cone_equiv_unit_iso_app F U) (by tidy) /-- Implementation of `sheaf_condition_pairwise_intersections.cone_equiv`. -/ @[simps] def cone_equiv_counit_iso (F : presheaf C X) ⦃ι : Type v⦄ (U : ι → opens X) : cone_equiv_inverse F U ⋙ cone_equiv_functor F U ≅ 𝟭 (limits.cone (sheaf_condition_equalizer_products.diagram F U)) := nat_iso.of_components (λ c, { hom := { hom := 𝟙 _, w' := begin rintro ⟨_|_⟩, { ext, dsimp, simp only [category.id_comp, limits.fan.mk_π_app, limits.limit.lift_π], }, { ext ⟨i,j⟩, dsimp, simp only [category.id_comp, limits.fan.mk_π_app, limits.limit.lift_π], }, end }, inv := { hom := 𝟙 _, w' := begin rintro ⟨_|_⟩, { ext, dsimp, simp only [category.id_comp, limits.fan.mk_π_app, limits.limit.lift_π], }, { ext ⟨i,j⟩, dsimp, simp only [category.id_comp, limits.fan.mk_π_app, limits.limit.lift_π], }, end, }, hom_inv_id' := by { ext, dsimp, simp only [category.comp_id], }, inv_hom_id' := by { ext, dsimp, simp only [category.comp_id], }, }) (λ c d f, by { ext, dsimp, simp only [category.comp_id, category.id_comp], }) /-- Cones over `diagram U ⋙ F` are the same as a cones over the usual sheaf condition equalizer diagram. -/ @[simps] def cone_equiv (F : presheaf C X) ⦃ι : Type v⦄ (U : ι → opens X) : limits.cone ((diagram U).op ⋙ F) ≌ limits.cone (sheaf_condition_equalizer_products.diagram F U) := { functor := cone_equiv_functor F U, inverse := cone_equiv_inverse F U, unit_iso := cone_equiv_unit_iso F U, counit_iso := cone_equiv_counit_iso F U, } local attribute [reducible] sheaf_condition_equalizer_products.res sheaf_condition_equalizer_products.left_res /-- If `sheaf_condition_equalizer_products.fork` is an equalizer, then `F.map_cone (cone U)` is a limit cone. -/ def is_limit_map_cone_of_is_limit_sheaf_condition_fork (F : presheaf C X) ⦃ι : Type v⦄ (U : ι → opens X) (P : is_limit (sheaf_condition_equalizer_products.fork F U)) : is_limit (F.map_cone (cocone U).op) := is_limit.of_iso_limit ((is_limit.of_cone_equiv (cone_equiv F U).symm).symm P) { hom := { hom := 𝟙 _, w' := begin intro x, induction x using opposite.rec, rcases x with ⟨⟩, { dsimp, simp, refl, }, { dsimp, simp only [limit.lift_π, limit.lift_π_assoc, category.id_comp, fan.mk_π_app, category.assoc], rw ←F.map_comp, refl, } end }, inv := { hom := 𝟙 _, w' := begin intro x, induction x using opposite.rec, rcases x with ⟨⟩, { dsimp, simp, refl, }, { dsimp, simp only [limit.lift_π, limit.lift_π_assoc, category.id_comp, fan.mk_π_app, category.assoc], rw ←F.map_comp, refl, } end }, hom_inv_id' := by { ext, dsimp, simp only [category.comp_id], }, inv_hom_id' := by { ext, dsimp, simp only [category.comp_id], }, } /-- If `F.map_cone (cone U)` is a limit cone, then `sheaf_condition_equalizer_products.fork` is an equalizer. -/ def is_limit_sheaf_condition_fork_of_is_limit_map_cone (F : presheaf C X) ⦃ι : Type v⦄ (U : ι → opens X) (Q : is_limit (F.map_cone (cocone U).op)) : is_limit (sheaf_condition_equalizer_products.fork F U) := is_limit.of_iso_limit ((is_limit.of_cone_equiv (cone_equiv F U)).symm Q) { hom := { hom := 𝟙 _, w' := begin rintro ⟨⟩, { dsimp, simp, refl, }, { dsimp, ext ⟨i, j⟩, simp only [limit.lift_π, limit.lift_π_assoc, category.id_comp, fan.mk_π_app, category.assoc], rw ←F.map_comp, refl, } end }, inv := { hom := 𝟙 _, w' := begin rintro ⟨⟩, { dsimp, simp, refl, }, { dsimp, ext ⟨i, j⟩, simp only [limit.lift_π, limit.lift_π_assoc, category.id_comp, fan.mk_π_app, category.assoc], rw ←F.map_comp, refl, } end }, hom_inv_id' := by { ext, dsimp, simp only [category.comp_id], }, inv_hom_id' := by { ext, dsimp, simp only [category.comp_id], }, } end sheaf_condition_pairwise_intersections open sheaf_condition_pairwise_intersections /-- The sheaf condition in terms of an equalizer diagram is equivalent to the reformulation in terms of a limit diagram over `U i` and `U i ⊓ U j`. -/ lemma is_sheaf_iff_is_sheaf_pairwise_intersections (F : presheaf C X) : F.is_sheaf ↔ F.is_sheaf_pairwise_intersections := iff.intro (λ h ι U, ⟨is_limit_map_cone_of_is_limit_sheaf_condition_fork F U (h U).some⟩) (λ h ι U, ⟨is_limit_sheaf_condition_fork_of_is_limit_map_cone F U (h U).some⟩) /-- The sheaf condition in terms of an equalizer diagram is equivalent to the reformulation in terms of the presheaf preserving the limit of the diagram consisting of the `U i` and `U i ⊓ U j`. -/ lemma is_sheaf_iff_is_sheaf_preserves_limit_pairwise_intersections (F : presheaf C X) : F.is_sheaf ↔ F.is_sheaf_preserves_limit_pairwise_intersections := begin rw is_sheaf_iff_is_sheaf_pairwise_intersections, split, { intros h ι U, exact ⟨preserves_limit_of_preserves_limit_cone (pairwise.cocone_is_colimit U).op (h U).some⟩ }, { intros h ι U, haveI := (h U).some, exact ⟨preserves_limit.preserves (pairwise.cocone_is_colimit U).op⟩ } end end Top.presheaf
51aa3ac37e086ededba25f2d2df16d67d2f811e0
9dc8cecdf3c4634764a18254e94d43da07142918
/archive/100-theorems-list/30_ballot_problem.lean
9e95d36c42195d97623ee339997408c9b72ee755
[ "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
20,393
lean
/- Copyright (c) 2022 Bhavik Mehta, Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Kexing Ying -/ import probability.cond_count /-! # Ballot problem This file proves Theorem 30 from the [100 Theorems List](https://www.cs.ru.nl/~freek/100/). The ballot problem asks, if in an election, candidate A receives `p` votes whereas candidate B receives `q` votes where `p > q`, what is the probability that candidate A is strictly ahead throughout the count. The probability of this is `(p - q) / (p + q)`. ## Main definitions * `counted_sequence`: given natural numbers `p` and `q`, `counted_sequence p q` is the set of all lists containing `p` of `1`s and `q` of `-1`s representing the votes of candidate A and B respectively. * `stays_positive`: is the set of lists of integers which suffix has positive sum. In particular, the intersection of this set with `counted_sequence` is the set of lists where candidate A is strictly ahead. ## Main result * `ballot`: the ballot problem. -/ open set probability_theory measure_theory namespace ballot /-- The set of nonempty lists of integers which suffix has positive sum. -/ def stays_positive : set (list ℤ) := {l | ∀ l₂, l₂ ≠ [] → l₂ <:+ l → 0 < l₂.sum} @[simp] lemma stays_positive_nil : [] ∈ stays_positive := λ l hl hl₁, (hl (list.eq_nil_of_suffix_nil hl₁)).elim lemma stays_positive_cons_pos (x : ℤ) (hx : 0 < x) (l : list ℤ) : (x :: l) ∈ stays_positive ↔ l ∈ stays_positive := begin split, { intros hl l₁ hl₁ hl₂, apply hl l₁ hl₁ (hl₂.trans (list.suffix_cons _ _)) }, { intros hl l₁ hl₁ hl₂, rw list.suffix_cons_iff at hl₂, rcases hl₂ with (rfl | hl₂), { rw list.sum_cons, apply add_pos_of_pos_of_nonneg hx, cases l with hd tl, { simp }, { apply le_of_lt (hl (hd :: tl) (list.cons_ne_nil hd tl) (hd :: tl).suffix_refl) } }, { apply hl _ hl₁ hl₂ } } end /-- `counted_sequence p q` is the set of lists of integers for which every element is `+1` or `-1`, there are `p` lots of `+1` and `q` lots of `-1`. This represents vote sequences where candidate `+1` receives `p` votes and candidate `-1` receives `q` votes. -/ def counted_sequence (p q : ℕ) : set (list ℤ) := {l | l.count 1 = p ∧ l.count (-1) = q ∧ ∀ x ∈ l, x = (1 : ℤ) ∨ x = -1} @[simp] lemma counted_right_zero (p : ℕ) : counted_sequence p 0 = {list.repeat 1 p} := begin ext l, rw [counted_sequence, mem_singleton_iff], split, { rintro ⟨hl₀, hl₁, hl₂⟩, rw list.eq_repeat, have : ∀ x ∈ l, (1 : ℤ) = x, { intros x hx, obtain rfl | rfl := hl₂ x hx, { refl }, { exact false.elim (list.not_mem_of_count_eq_zero hl₁ hx) } }, split, { rwa ← list.count_eq_length.2 this }, { exact λ x hx, (this x hx).symm } }, { rintro rfl, simp only [mem_set_of_eq, list.count_repeat, eq_self_iff_true, true_and], refine ⟨list.count_eq_zero_of_not_mem _, λ x, _⟩; rw list.mem_repeat, { norm_num }, { rintro ⟨-, rfl⟩, exact or.inl rfl } } end @[simp] lemma counted_left_zero (q : ℕ) : counted_sequence 0 q = {list.repeat (-1) q} := begin ext l, rw [counted_sequence, mem_singleton_iff], split, { rintro ⟨hl₀, hl₁, hl₂⟩, rw list.eq_repeat, have : ∀ x ∈ l, (-1 : ℤ) = x, { intros x hx, obtain rfl | rfl := hl₂ x hx, { exact false.elim (list.not_mem_of_count_eq_zero hl₀ hx) }, { refl } }, split, { rwa ← list.count_eq_length.2 this }, { exact λ x hx, (this x hx).symm } }, { rintro rfl, simp only [mem_set_of_eq, list.count_repeat, eq_self_iff_true, true_and], refine ⟨list.count_eq_zero_of_not_mem _, λ x, _⟩; rw list.mem_repeat, { norm_num }, { rintro ⟨-, rfl⟩, exact or.inr rfl } } end lemma counted_ne_nil_left {p q : ℕ} (hp : p ≠ 0) {l : list ℤ} (hl : l ∈ counted_sequence p q) : l ≠ list.nil := begin obtain ⟨hl₀, hl₁, hl₂⟩ := hl, rintro rfl, rw list.count_nil at hl₀, exact hp hl₀.symm, end lemma counted_ne_nil_right {p q : ℕ} (hp : q ≠ 0) {l : list ℤ} (hl : l ∈ counted_sequence p q) : l ≠ list.nil := begin obtain ⟨hl₀, hl₁, hl₂⟩ := hl, rintro rfl, rw list.count_nil at hl₁, exact hp hl₁.symm, end lemma counted_succ_succ (p q : ℕ) : counted_sequence (p + 1) (q + 1) = (counted_sequence p (q + 1)).image (list.cons 1) ∪ (counted_sequence (p + 1) q).image (list.cons (-1)) := begin ext l, rw [counted_sequence, counted_sequence, counted_sequence], split, { intro hl, have hlnil := counted_ne_nil_left (nat.succ_ne_zero p) hl, obtain ⟨hl₀, hl₁, hl₂⟩ := hl, obtain hlast | hlast := hl₂ l.head (list.head_mem_self hlnil), { refine or.inl ⟨l.tail, ⟨_, _, _⟩, _⟩, { rw [list.count_tail l 1 (list.length_pos_of_ne_nil hlnil), hl₀, if_pos, nat.add_succ_sub_one, add_zero], rw [list.nth_le_zero, hlast] }, { rw [list.count_tail l (-1) (list.length_pos_of_ne_nil hlnil), hl₁, if_neg, nat.sub_zero], rw [list.nth_le_zero, hlast], norm_num }, { exact λ x hx, hl₂ x (list.mem_of_mem_tail hx) }, { rw [← hlast, list.cons_head_tail hlnil] } }, { refine or.inr ⟨l.tail, ⟨_, _, _⟩, _⟩, { rw [list.count_tail l 1 (list.length_pos_of_ne_nil hlnil), hl₀, if_neg, nat.sub_zero], rw [list.nth_le_zero, hlast], norm_num }, { rw [list.count_tail l (-1) (list.length_pos_of_ne_nil hlnil), hl₁, if_pos, nat.add_succ_sub_one, add_zero], rw [list.nth_le_zero, hlast] }, { exact λ x hx, hl₂ x (list.mem_of_mem_tail hx) }, { rw [← hlast, list.cons_head_tail hlnil] } } }, { rintro (⟨t, ⟨ht₀, ht₁, ht₂⟩, rfl⟩ | ⟨t, ⟨ht₀, ht₁, ht₂⟩, rfl⟩), { refine ⟨_, _, _⟩, { rw [list.count_cons, if_pos rfl, ht₀] }, { rw [list.count_cons, if_neg, ht₁], norm_num }, { rintro x (hx | hx), exacts [or.inl hx, ht₂ x hx] } }, { refine ⟨_, _, _⟩, { rw [list.count_cons, if_neg, ht₀], norm_num }, { rw [list.count_cons, if_pos rfl, ht₁] }, { rintro x (hx | hx), exacts [or.inr hx, ht₂ x hx] } } } end lemma counted_sequence_finite : ∀ (p q : ℕ), (counted_sequence p q).finite | 0 q := by simp | (p + 1) 0 := by simp | (p + 1) (q + 1) := begin rw [counted_succ_succ, set.finite_union, set.finite_image_iff (list.cons_injective.inj_on _), set.finite_image_iff (list.cons_injective.inj_on _)], exact ⟨counted_sequence_finite _ _, counted_sequence_finite _ _⟩ end lemma counted_sequence_nonempty : ∀ (p q : ℕ), (counted_sequence p q).nonempty | 0 q := by simp | (p + 1) 0 := by simp | (p + 1) (q + 1) := begin rw [counted_succ_succ, union_nonempty, nonempty_image_iff], exact or.inl (counted_sequence_nonempty _ _), end lemma sum_of_mem_counted_sequence : ∀ {p q : ℕ} {l : list ℤ} (hl : l ∈ counted_sequence p q), l.sum = p - q | 0 q l hl := begin rw [counted_left_zero, mem_singleton_iff] at hl, simp [hl], end | p 0 l hl := begin rw [counted_right_zero, mem_singleton_iff] at hl, simp [hl], end | (p + 1) (q + 1) l hl := begin simp only [counted_succ_succ, mem_union, mem_image] at hl, rcases hl with (⟨l, hl, rfl⟩ | ⟨l, hl, rfl⟩), { rw [list.sum_cons, sum_of_mem_counted_sequence hl], push_cast, ring }, { rw [list.sum_cons, sum_of_mem_counted_sequence hl], push_cast, ring } end lemma mem_of_mem_counted_sequence : ∀ {p q : ℕ} {l} (hl : l ∈ counted_sequence p q) {x : ℤ} (hx : x ∈ l), x = 1 ∨ x = -1 | 0 q l hl x hx := begin rw [counted_left_zero, mem_singleton_iff] at hl, subst hl, exact or.inr (list.eq_of_mem_repeat hx), end | p 0 l hl x hx := begin rw [counted_right_zero, mem_singleton_iff] at hl, subst hl, exact or.inl (list.eq_of_mem_repeat hx), end | (p + 1) (q + 1) l hl x hx := begin simp only [counted_succ_succ, mem_union, mem_image] at hl, rcases hl with (⟨l, hl, rfl⟩ | ⟨l, hl, rfl⟩); rcases hx with (rfl | hx), { left, refl }, { exact mem_of_mem_counted_sequence hl hx }, { right, refl }, { exact mem_of_mem_counted_sequence hl hx }, end lemma length_of_mem_counted_sequence : ∀ {p q : ℕ} {l : list ℤ} (hl : l ∈ counted_sequence p q), l.length = p + q | 0 q l hl := begin rw [counted_left_zero, mem_singleton_iff] at hl, simp [hl], end | p 0 l hl := begin rw [counted_right_zero, mem_singleton_iff] at hl, simp [hl], end | (p + 1) (q + 1) l hl := begin simp only [counted_succ_succ, mem_union, mem_image] at hl, rcases hl with (⟨l, hl, rfl⟩ | ⟨l, hl, rfl⟩), { rw [list.length_cons, length_of_mem_counted_sequence hl, add_right_comm] }, { rw [list.length_cons, length_of_mem_counted_sequence hl, ←add_assoc] } end lemma disjoint_bits (p q : ℕ) : disjoint ((counted_sequence p (q + 1)).image (list.cons 1)) ((counted_sequence (p + 1) q).image (list.cons (-1))) := begin simp_rw [disjoint_left, mem_image, not_exists, exists_imp_distrib], rintros _ _ ⟨_, rfl⟩ _ ⟨_, _, _⟩, end open measure_theory.measure private def measureable_space_list_int : measurable_space (list ℤ) := ⊤ local attribute [instance] measureable_space_list_int private def measurable_singleton_class_list_int : measurable_singleton_class (list ℤ) := { measurable_set_singleton := λ s, trivial } local attribute [instance] measurable_singleton_class_list_int private lemma list_int_measurable_set {s : set (list ℤ)} : measurable_set s := trivial lemma count_counted_sequence : ∀ p q : ℕ, count (counted_sequence p q) = (p + q).choose p | p 0 := by simp [counted_right_zero, count_singleton] | 0 q := by simp [counted_left_zero, count_singleton] | (p + 1) (q + 1) := begin rw [counted_succ_succ, measure_union (disjoint_bits _ _) list_int_measurable_set, count_injective_image list.cons_injective, count_counted_sequence, count_injective_image list.cons_injective, count_counted_sequence], { norm_cast, rw [add_assoc, add_comm 1 q, ← nat.choose_succ_succ, nat.succ_eq_add_one, add_right_comm] }, all_goals { try { apply_instance } }, end lemma first_vote_pos : ∀ p q, 0 < p + q → cond_count (counted_sequence p q : set (list ℤ)) {l | l.head = 1} = p / (p + q) | (p + 1) 0 h := begin rw [counted_right_zero, cond_count_singleton], simp [ennreal.div_self _ _], end | 0 (q + 1) _ := begin rw [counted_left_zero, cond_count_singleton], simpa, end | (p + 1) (q + 1) h := begin simp_rw [counted_succ_succ], rw [← cond_count_disjoint_union ((counted_sequence_finite _ _).image _) ((counted_sequence_finite _ _).image _) (disjoint_bits _ _), ← counted_succ_succ, cond_count_eq_one_of ((counted_sequence_finite p (q + 1)).image _) (nonempty_image_iff.2 (counted_sequence_nonempty _ _))], { have : list.cons (-1) '' counted_sequence (p + 1) q ∩ {l : list ℤ | l.head = 1} = ∅, { ext, simp only [mem_inter_eq, mem_image, mem_set_of_eq, mem_empty_eq, iff_false, not_and, forall_exists_index, and_imp], rintro l _ rfl, norm_num }, have hint : counted_sequence (p + 1) (q + 1) ∩ list.cons 1 '' counted_sequence p (q + 1) = list.cons 1 '' counted_sequence p (q + 1), { rw [inter_eq_right_iff_subset, counted_succ_succ], exact subset_union_left _ _ }, rw [(cond_count_eq_zero_iff $ (counted_sequence_finite _ _).image _).2 this, cond_count, cond_apply _ list_int_measurable_set, hint, count_injective_image list.cons_injective, count_counted_sequence, count_counted_sequence, one_mul, zero_mul, add_zero, nat.cast_add, nat.cast_one], { rw [mul_comm, ← div_eq_mul_inv, ennreal.div_eq_div_iff], { norm_cast, rw [mul_comm _ (p + 1), ← nat.succ_eq_add_one p, nat.succ_add, nat.succ_mul_choose_eq, mul_comm] }, all_goals { simp [(nat.choose_pos $ (le_add_iff_nonneg_right _).2 zero_le').ne.symm] } }, all_goals { apply_instance } }, { simp }, { apply_instance } end lemma head_mem_of_nonempty {α : Type*} [inhabited α] : ∀ {l : list α} (hl : l ≠ []), l.head ∈ l | [] h := h rfl | (x :: l) _ := or.inl rfl lemma first_vote_neg (p q : ℕ) (h : 0 < p + q) : cond_count (counted_sequence p q) {l | l.head = 1}ᶜ = q / (p + q) := begin have := cond_count_compl {l : list ℤ | l.head = 1}ᶜ (counted_sequence_finite p q) (counted_sequence_nonempty p q), rw [compl_compl, first_vote_pos _ _ h] at this, rw [(_ : (q / (p + q) : ennreal) = 1 - p / (p + q)), ← this, ennreal.add_sub_cancel_right], { simp only [ne.def, ennreal.div_eq_top, nat.cast_eq_zero, add_eq_zero_iff, ennreal.nat_ne_top, false_and, or_false, not_and], intros, contradiction }, rw [eq_comm, ennreal.eq_div_iff, ennreal.mul_sub, ennreal.mul_div_cancel'], all_goals { simp, try { rintro rfl, rw zero_add at h, exact h.ne.symm } }, end lemma ballot_same (p : ℕ) : cond_count (counted_sequence (p + 1) (p + 1)) stays_positive = 0 := begin rw [cond_count_eq_zero_iff (counted_sequence_finite _ _), eq_empty_iff_forall_not_mem], rintro x ⟨hx, t⟩, apply ne_of_gt (t x _ x.suffix_refl), { simpa using sum_of_mem_counted_sequence hx }, { refine list.ne_nil_of_length_pos _, rw length_of_mem_counted_sequence hx, exact nat.add_pos_left (nat.succ_pos _) _ }, end lemma ballot_edge (p : ℕ) : cond_count (counted_sequence (p + 1) 0) stays_positive = 1 := begin rw counted_right_zero, refine cond_count_eq_one_of (finite_singleton _) (singleton_nonempty _) _, { intros l hl, rw mem_singleton_iff at hl, subst hl, refine λ l hl₁ hl₂, list.sum_pos _ (λ x hx, _) hl₁, rw list.eq_of_mem_repeat (list.mem_of_mem_suffix hx hl₂), norm_num }, end lemma counted_sequence_int_pos_counted_succ_succ (p q : ℕ) : (counted_sequence (p + 1) (q + 1)) ∩ {l | l.head = 1} = (counted_sequence p (q + 1)).image (list.cons 1) := begin rw [counted_succ_succ, union_inter_distrib_right, (_ : list.cons (-1) '' counted_sequence (p + 1) q ∩ {l | l.head = 1} = ∅), union_empty]; { ext, simp only [mem_inter_eq, mem_image, mem_set_of_eq, and_iff_left_iff_imp, mem_empty_eq, iff_false, not_and, forall_exists_index, and_imp], rintro y hy rfl, norm_num } end lemma ballot_pos (p q : ℕ) : cond_count ((counted_sequence (p + 1) (q + 1)) ∩ {l | l.head = 1}) stays_positive = cond_count (counted_sequence p (q + 1)) stays_positive := begin rw [counted_sequence_int_pos_counted_succ_succ, cond_count, cond_count, cond_apply _ list_int_measurable_set, cond_apply _ list_int_measurable_set, count_injective_image list.cons_injective], all_goals { try { apply_instance } }, congr' 1, have : (counted_sequence p (q + 1)).image (list.cons 1) ∩ stays_positive = (counted_sequence p (q + 1) ∩ stays_positive).image (list.cons 1), { ext t, simp only [mem_inter_eq, mem_image], split, { simp only [and_imp, exists_imp_distrib], rintro l hl rfl t, refine ⟨l, ⟨hl, _⟩, rfl⟩, rwa stays_positive_cons_pos at t, norm_num }, { simp only [and_imp, exists_imp_distrib], rintro l hl₁ hl₂ rfl, refine ⟨⟨_, hl₁, rfl⟩, _⟩, rwa stays_positive_cons_pos, norm_num } }, rw [this, count_injective_image], exact list.cons_injective, end lemma counted_sequence_int_neg_counted_succ_succ (p q : ℕ) : (counted_sequence (p + 1) (q + 1)) ∩ {l | l.head = 1}ᶜ = (counted_sequence (p + 1) q).image (list.cons (-1)) := begin rw [counted_succ_succ, union_inter_distrib_right, (_ : list.cons 1 '' counted_sequence p (q + 1) ∩ {l : list ℤ | l.head = 1}ᶜ = ∅), empty_union]; { ext, simp only [mem_inter_eq, mem_image, mem_set_of_eq, and_iff_left_iff_imp, mem_empty_eq, iff_false, not_and, forall_exists_index, and_imp], rintro y hy rfl, norm_num } end lemma ballot_neg (p q : ℕ) (qp : q < p) : cond_count ((counted_sequence (p + 1) (q + 1)) ∩ {l | l.head = 1}ᶜ) stays_positive = cond_count (counted_sequence (p + 1) q) stays_positive := begin rw [counted_sequence_int_neg_counted_succ_succ, cond_count, cond_count, cond_apply _ list_int_measurable_set, cond_apply _ list_int_measurable_set, count_injective_image list.cons_injective], all_goals { try { apply_instance } }, congr' 1, have : (counted_sequence (p + 1) q).image (list.cons (-1)) ∩ stays_positive = ((counted_sequence (p + 1) q) ∩ stays_positive).image (list.cons (-1)), { ext t, simp only [mem_inter_eq, mem_image], split, { simp only [and_imp, exists_imp_distrib], rintro l hl rfl t, exact ⟨_, ⟨hl, λ l₁ hl₁ hl₂, t l₁ hl₁ (hl₂.trans (list.suffix_cons _ _))⟩, rfl⟩ }, { simp only [and_imp, exists_imp_distrib], rintro l hl₁ hl₂ rfl, refine ⟨⟨l, hl₁, rfl⟩, λ l₁ hl₃ hl₄, _⟩, rw list.suffix_cons_iff at hl₄, rcases hl₄ with (rfl | hl₄), { simp [list.sum_cons, sum_of_mem_counted_sequence hl₁, sub_eq_add_neg, ← add_assoc, qp] }, exact hl₂ _ hl₃ hl₄ } }, rw [this, count_injective_image], exact list.cons_injective end theorem ballot_problem' : ∀ q p, q < p → (cond_count (counted_sequence p q) stays_positive).to_real = (p - q) / (p + q) := begin classical, apply nat.diag_induction, { intro p, rw ballot_same, simp }, { intro p, rw ballot_edge, simp only [ennreal.one_to_real, nat.cast_add, nat.cast_one, nat.cast_zero, sub_zero, add_zero], rw div_self , exact nat.cast_add_one_ne_zero p }, { intros q p qp h₁ h₂, haveI := cond_count_is_probability_measure (counted_sequence_finite p (q + 1)) (counted_sequence_nonempty _ _), haveI := cond_count_is_probability_measure (counted_sequence_finite (p + 1) q) (counted_sequence_nonempty _ _), have h₃ : p + 1 + (q + 1) > 0 := nat.add_pos_left (nat.succ_pos _) _, rw [← cond_count_add_compl_eq {l : list ℤ | l.head = 1} _ (counted_sequence_finite _ _), first_vote_pos _ _ h₃, first_vote_neg _ _ h₃, ballot_pos, ballot_neg _ _ qp], rw [ennreal.to_real_add, ennreal.to_real_mul, ennreal.to_real_mul, ← nat.cast_add, ennreal.to_real_div, ennreal.to_real_div, ennreal.to_real_nat, ennreal.to_real_nat, ennreal.to_real_nat, h₁, h₂], { have h₄ : (↑(p + 1) + ↑(q + 1)) ≠ (0 : ℝ), { apply ne_of_gt, assumption_mod_cast }, have h₅ : (↑(p + 1) + ↑q) ≠ (0 : ℝ), { apply ne_of_gt, norm_cast, linarith }, have h₆ : (↑p + ↑(q + 1)) ≠ (0 : ℝ), { apply ne_of_gt, norm_cast, linarith }, field_simp [h₄, h₅, h₆] at *, ring }, all_goals { refine (ennreal.mul_lt_top (measure_lt_top _ _).ne _).ne, simp [ne.def, ennreal.div_eq_top] } } end /-- The ballot problem. -/ theorem ballot_problem : ∀ q p, q < p → cond_count (counted_sequence p q) stays_positive = (p - q) / (p + q) := begin intros q p qp, haveI := cond_count_is_probability_measure (counted_sequence_finite p q) (counted_sequence_nonempty _ _), have : (cond_count (counted_sequence p q) stays_positive).to_real = ((p - q) / (p + q) : ennreal).to_real, { rw ballot_problem' q p qp, rw [ennreal.to_real_div, ← nat.cast_add, ← nat.cast_add, ennreal.to_real_nat, ennreal.to_real_sub_of_le, ennreal.to_real_nat, ennreal.to_real_nat], exacts [ennreal.coe_nat_le_coe_nat.2 qp.le, ennreal.nat_ne_top _] }, rwa ennreal.to_real_eq_to_real (measure_lt_top _ _).ne at this, { simp only [ne.def, ennreal.div_eq_top, tsub_eq_zero_iff_le, ennreal.coe_nat_le_coe_nat, not_le, add_eq_zero_iff, nat.cast_eq_zero, ennreal.add_eq_top, ennreal.nat_ne_top, or_self, not_false_iff, and_true], push_neg, exact ⟨λ _ _, by linarith, (lt_of_le_of_lt tsub_le_self (ennreal.nat_ne_top p).lt_top).ne⟩ }, apply_instance, end end ballot
27827248b77e61b24b30a26c8444fc63ab21a690
842b7df4a999c5c50bbd215b8617dd705e43c2e1
/nat_num_game/src/Advanced_Addition_World/adv_add_wrld5.lean
c79cad8873ad9ae595fca54153a69b57b72e67bd
[]
no_license
Samyak-Surti/LeanCode
1c245631f74b00057d20483c8ac75916e8643b14
944eac3e5f43e2614ed246083b97fbdf24181d83
refs/heads/master
1,669,023,730,828
1,595,534,784,000
1,595,534,784,000
282,037,186
0
0
null
null
null
null
UTF-8
Lean
false
false
234
lean
theorem add_right_cancel (a t b : ℕ) : a + t = b + t → a = b := begin intro h, induction t with d hd, repeat {rw add_zero at h}, exact h, repeat {rw nat.add_succ at h}, rw hd, apply nat.succ.inj h, end
a99751724e8e5c2c85f784ac7ee50b2563d581da
a162e54d0534f0863de20e911278e6c41ba96e28
/src/imo2008_q3.lean
14f1cc0f116023d6f6ced4a50b0248967d87b894
[ "Apache-2.0" ]
permissive
manuelcandales/imo-lean
0ef79d470b58064d16a94adfb83711ae3265a19a
fa54938100fc84c98fe04e1d19e20a424dd006e7
refs/heads/master
1,680,228,525,536
1,617,602,898,000
1,617,602,898,000
353,518,197
0
0
null
null
null
null
UTF-8
Lean
false
false
4,871
lean
/- Copyright (c) 2021 Manuel Candales. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Manuel Candales -/ import data.real.basic import data.real.sqrt import data.nat.prime import number_theory.primes_congruent_one import number_theory.quadratic_reciprocity /-! # IMO 2008 Q3 Prove that there exist infinitely many positive integers `n` such that `n^2 + 1` has a prime divisor which is greater than `2n + √(2n)`. # Solution We first prove the following lemma: for every prime `p > 20`, satisfying `p ≡ 1 [MOD 4]`, there exists `n ∈ ℕ` such that `p ∣ n^2 + 1` and `p > 2n + √(2n)`. Then the statement of the problem follows from the fact that there exist infinitely many primes `p ≡ 1 [MOD 4]`. To prove the lemma, notice that `p ≡ 1 [MOD 4]` implies `∃ n ∈ ℕ` such that `n^2 ≡ -1 [MOD p]` and we can take this `n` such that `n ≤ p/2`. Let `k = p - 2n ≥ 0`. Then we have: `k^2 + 4 = (p - 2n)^2 + 4 ≣ 4n^2 + 4 ≡ 0 [MOD p]`. Then `k^2 + 4 ≥ p` and so `k ≥ √(p - 4) > 4`. Then `p = 2n + k ≥ 2n + √(p - 4) = 2n + √(2n + k - 4) > √(2n)` and we are done. -/ lemma p_lemma (p : ℕ) (hpp : nat.prime p) (hp_mod_4_eq_1 : p ≡ 1 [MOD 4]) (hp_gt_20 : p > 20) : ∃ n : ℕ, p ∣ n^2 + 1 ∧ (p : ℝ) > 2*n + real.sqrt(2*n) := begin haveI := fact.mk hpp, have hp_mod_4_ne_3 : p % 4 ≠ 3, { linarith [(show p % 4 = 1, by exact hp_mod_4_eq_1)]}, obtain ⟨y, hy⟩ := (zmod.exists_pow_two_eq_neg_one_iff_mod_four_ne_three p).mpr hp_mod_4_ne_3, let m := zmod.val_min_abs y, let n := int.nat_abs (m), have hnat₁ : p ∣ n^2 + 1, { refine int.coe_nat_dvd.mp _, simp only [int.nat_abs_pow_two, int.coe_nat_pow, int.coe_nat_succ, int.coe_nat_dvd.mp], refine (zmod.int_coe_zmod_eq_zero_iff_dvd (m^2 + 1) p).mp _, simp only [int.cast_pow, int.cast_add, int.cast_one, zmod.coe_val_min_abs], rw hy, exact add_left_neg 1 }, have hnat₂ : n ≤ p/2 := zmod.nat_abs_val_min_abs_le y, have hnat₃ : p ≥ 2*n, { linarith [nat.div_mul_le_self p 2] }, set k : ℕ := p - 2*n with hnat₄, have hnat₅ : p ∣ k^2 + 4, { cases hnat₁ with x hx, let p₁ := (p : ℤ), let n₁ := (n : ℤ), let k₁ := (k : ℤ), let x₁ := (x : ℤ), have : p₁ ∣ k₁^2 + 4, { use p₁ - 4*n₁ + 4*x₁, calc k₁^2 + 4 = (p₁ - 2*n₁)^2 + 4 : by rw (show k₁ = p₁ - 2*n₁, by assumption_mod_cast) ... = p₁^2 - 4*p₁*n₁ + 4*(n₁^2 + 1) : by ring ... = p₁^2 - 4*p₁*n₁ + 4*(p₁*x₁) : by rw (show n₁^2 + 1 = p₁*x₁, by assumption_mod_cast) ... = p₁*(p₁ - 4*n₁ + 4*x₁) : by ring }, assumption_mod_cast }, have hnat₆ : k^2 + 4 ≥ p := nat.le_of_dvd (k^2 + 3).succ_pos hnat₅, let p₀ := (p : ℝ), let n₀ := (n : ℝ), let k₀ := (k : ℝ), have hreal₁ : p₀ = 2*n₀ + k₀, { linarith [(show k₀ = p₀ - 2*n₀, by assumption_mod_cast)] }, have hreal₂ : p₀ > 20, { assumption_mod_cast }, have hreal₃ : k₀^2 + 4 ≥ p₀, { assumption_mod_cast }, have hreal₄ : k₀ ≥ real.sqrt(p₀ - 4), { calc k₀ = real.sqrt(k₀^2) : eq.symm (real.sqrt_sqr (nat.cast_nonneg k)) ... ≥ real.sqrt(p₀-4) : real.sqrt_le_sqrt (by linarith [hreal₃]) }, have hreal₅ : k₀ > 4, { calc k₀ ≥ real.sqrt(p₀-4) : hreal₄ ... > real.sqrt(4^2) : (real.sqrt_lt (by linarith)).mpr (by linarith [hreal₂]) ... = 4 : real.sqrt_sqr (by linarith) }, have hreal₆ : p₀ > 2*n₀ + real.sqrt(2*n), { calc p₀ = 2*n₀ + k₀ : hreal₁ ... ≥ 2*n₀ + real.sqrt(p₀-4) : by linarith [hreal₄] ... = 2*n₀ + real.sqrt(2*n₀+k₀-4) : by rw hreal₁ ... > 2*n₀ + real.sqrt(2*n₀) : by { refine add_lt_add_left _ (2*n₀), refine (real.sqrt_lt _).mpr _, refine mul_nonneg zero_le_two (nat.cast_nonneg n), linarith [hreal₅] } }, exact ⟨n, hnat₁, hreal₆⟩, end theorem imo2008_q3 : ∀ N : ℕ, ∃ n : ℕ, n ≥ N ∧ ∃ p : ℕ, nat.prime p ∧ p ∣ n^2 + 1 ∧ (p : ℝ) > 2*n + real.sqrt(2*n) := begin intro N, obtain ⟨p, hpp, hineq₁, hp_mod_4_eq_1⟩ := nat.exists_prime_ge_modeq_one 4 (N^2+21) zero_lt_four, obtain ⟨n, hnat, hreal⟩ := p_lemma p hpp hp_mod_4_eq_1 (by linarith [hineq₁, nat.zero_le (N^2)]), have hineq₂ : n^2 + 1 ≥ p := nat.le_of_dvd (n^2).succ_pos hnat, have hineq₃ : n*n ≥ N*N, { linarith [hineq₁, hineq₂, (pow_two n), (pow_two N)] }, have hn_ge_N : n ≥ N := nat.mul_self_le_mul_self_iff.mpr hineq₃, exact ⟨n, hn_ge_N, p, hpp, hnat, hreal⟩, end
c6b44a375b6ce52bb21e6188be2590ac6dac2a5a
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Lean/Meta/Coe.lean
7c82e17f76a6944c582e0f9f391702f4af226d8a
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
1,123
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.Meta.WHNF import Lean.Meta.Transform namespace Lean.Meta /-- Return true iff `declName` is one of the auxiliary definitions/projections used to implement coercions. -/ def isCoeDecl (declName : Name) : Bool := declName == ``Coe.coe || declName == ``CoeTC.coe || declName == ``CoeHead.coe || declName == ``CoeTail.coe || declName == ``CoeHTCT.coe || declName == ``CoeDep.coe || declName == ``CoeT.coe || declName == ``CoeFun.coe || declName == ``CoeSort.coe || declName == ``Lean.Internal.liftCoeM || declName == ``Lean.Internal.coeM /-- Expand coercions occurring in `e` -/ partial def expandCoe (e : Expr) : MetaM Expr := withReducibleAndInstances do transform e fun e => do let f := e.getAppFn if f.isConst then let declName := f.constName! if isCoeDecl declName then if let some e ← unfoldDefinition? e then return .visit e.headBeta return .continue end Lean.Meta
5cbdfba05cf2da3d66c852e7288342025cfe96a2
ebf7140a9ea507409ff4c994124fa36e79b4ae35
/src/exercises_sources/tuesday/numbers.lean
25b095623b3684719041489cd12e553b8ad19bc3
[]
no_license
fundou/lftcm2020
3e88d58a92755ea5dd49f19c36239c35286ecf5e
99d11bf3bcd71ffeaef0250caa08ecc46e69b55b
refs/heads/master
1,685,610,799,304
1,624,070,416,000
1,624,070,416,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,264
lean
import tactic import data.real.basic import number_theory.padics import data.int.gcd /-! ## Exercises about numbers and casts -/ /-! ## First exercises These first examples are just to get you comfortable with `norm_num`, `norm_cast`, and friends. -/ example : 12345 < 67890 := begin sorry end example {α : Type} [linear_ordered_field α] : 123 + 45 < 67890/3 := begin sorry end example : nat.prime 17 := begin sorry end -- prove either this or its negation! example : 7/3 > 2 := begin sorry end example (x : ℝ) (hx : x < 50*50) : x < 25*100 := begin sorry end example (x : ℤ) (hx : (x : ℝ) < 25*100) : x < 25*100 := begin sorry end example (x : ℤ) (hx : (x : ℝ) < 2500) : x < 25*100 := begin sorry end example (p q r : ℕ) (h : r < p - q) (hpq : q ≤ p) : (r : ℝ) < p - q := begin sorry end example (p q r : ℕ) (hr : r < p + 2 - p) : (r : ℤ) < 5 := begin sorry end /-! ## Exercise 2 This comes from the development of the p-adic numbers. `norm_cast` is very useful here, since we need to talk about values in ℕ, ℤ, ℚ, ℚ_p, and ℤ_p. We've done some work to get you started. You might look for the lemmas: -/ open padic_val_rat #check fpow_le_of_le #check fpow_nonneg #check padic_val_rat_of_int example {p n : ℕ} (hp : p.prime) {z : ℤ} (hd : ↑(p^n) ∣ z) : padic_norm p z ≤ ↑p ^ (-n : ℤ) := begin -- This lemma will be useful later in the proof. -- Ignore the "inst" argument; just use `apply aux_lemma` when you need it! -- Note that we haven't finished it. Fill in that final sorry. have aux_lemma : ∀ inst, (n : ℤ) ≤ (multiplicity ↑p z).get inst, { intro, norm_cast, rw [← enat.coe_le_coe, enat.coe_get], apply multiplicity.le_multiplicity_of_pow_dvd, sorry }, unfold padic_norm, split_ifs with hz hz, { sorry }, { sorry } end /-! ## Exercise 3 This seems like a very natural way to write the theorem "If `a` and `b` are coprime, then there are coefficients `u` and `v` such that `u*a + v*b = 1`." But I've made a mistake! What did I do wrong? Correct the statement of the theorem and prove it. I've started you off with a lemma that will be useful. You might find the `specialize` tactic to be handy as well: if you have `h : ∀ (x y : T), R x y` and `a, b : T` in the context, then `specialize h a b` will change the type of `h` to `R a b`. -/ example (a b : ℕ) (h : nat.coprime a b) : ∃ u v, u*a+v*b = 1 := begin have := nat.gcd_eq_gcd_ab, sorry end /-! ## Exercise 4 We did an example together that was similar to this. This one takes a bit more arithmetic work. To save you some time, here are some lemmas that may be useful! (You may not need all of them, depending on how you approach it.) Remember you can also use `library_search` to try to find useful lemmas. A hint: you might find it helpful to do this once you've introduced `n`. ``` have n_pos: 0 < n, { ... } ``` -/ #check sub_le_iff_le_add #check add_le_add_iff_left #check div_le_iff #check mul_one_div_cancel #check mul_le_mul_left notation `|`x`|` := abs x def seq_limit (u : ℕ → ℝ) (l : ℝ) : Prop := ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε example : seq_limit (λ n : ℕ, (n+1)/n) 1 := begin sorry end
44b1570c7d98ea38d761a8106b1f87e6f5664f0b
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/field_theory/finite.lean
c039bb68b6aaf834dc27c532445dc353a922e909
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
12,194
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Joey van Langen, Casper Putz -/ import tactic.apply_fun import data.equiv.ring import data.zmod.basic import linear_algebra.basis import ring_theory.integral_domain import field_theory.separable /-! # Finite fields This file contains basic results about finite fields. Throughout most of this file, `K` denotes a finite field and `q` is notation for the cardinality of `K`. ## Main results 1. Every finite integral domain is a field (`field_of_integral_domain`). 2. The unit group of a finite field is a cyclic group of order `q - 1`. (`finite_field.is_cyclic` and `card_units`) 3. `sum_pow_units`: The sum of `x^i`, where `x` ranges over the units of `K`, is - `q-1` if `q-1 ∣ i` - `0` otherwise 4. `finite_field.card`: The cardinality `q` is a power of the characteristic of `K`. See `card'` for a variant. ## Notation Throughout most of this file, `K` denotes a finite field and `q` is notation for the cardinality of `K`. -/ variables {K : Type*} [field K] [fintype K] variables {R : Type*} [integral_domain R] local notation `q` := fintype.card K open_locale big_operators namespace finite_field open finset function section polynomial open polynomial /-- The cardinality of a field is at most `n` times the cardinality of the image of a degree `n` polynomial -/ lemma card_image_polynomial_eval [decidable_eq R] [fintype R] {p : polynomial R} (hp : 0 < p.degree) : fintype.card R ≤ nat_degree p * (univ.image (λ x, eval x p)).card := finset.card_le_mul_card_image _ _ (λ a _, calc _ = (p - C a).roots.card : congr_arg card (by simp [finset.ext_iff, mem_roots_sub_C hp, -sub_eq_add_neg]) ... ≤ _ : card_roots_sub_C' hp) /-- If `f` and `g` are quadratic polynomials, then the `f.eval a + g.eval b = 0` has a solution. -/ lemma exists_root_sum_quadratic [fintype R] {f g : polynomial R} (hf2 : degree f = 2) (hg2 : degree g = 2) (hR : fintype.card R % 2 = 1) : ∃ a b, f.eval a + g.eval b = 0 := by letI := classical.dec_eq R; exact suffices ¬ disjoint (univ.image (λ x : R, eval x f)) (univ.image (λ x : R, eval x (-g))), begin simp only [disjoint_left, mem_image] at this, push_neg at this, rcases this with ⟨x, ⟨a, _, ha⟩, ⟨b, _, hb⟩⟩, exact ⟨a, b, by rw [ha, ← hb, eval_neg, neg_add_self]⟩ end, assume hd : disjoint _ _, lt_irrefl (2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card) $ calc 2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card ≤ 2 * fintype.card R : nat.mul_le_mul_left _ (finset.card_le_of_subset (subset_univ _)) ... = fintype.card R + fintype.card R : two_mul _ ... < nat_degree f * (univ.image (λ x : R, eval x f)).card + nat_degree (-g) * (univ.image (λ x : R, eval x (-g))).card : add_lt_add_of_lt_of_le (lt_of_le_of_ne (card_image_polynomial_eval (by rw hf2; exact dec_trivial)) (mt (congr_arg (%2)) (by simp [nat_degree_eq_of_degree_eq_some hf2, hR]))) (card_image_polynomial_eval (by rw [degree_neg, hg2]; exact dec_trivial)) ... = 2 * (univ.image (λ x : R, eval x f) ∪ univ.image (λ x : R, eval x (-g))).card : by rw [card_disjoint_union hd]; simp [nat_degree_eq_of_degree_eq_some hf2, nat_degree_eq_of_degree_eq_some hg2, bit0, mul_add] end polynomial lemma card_units : fintype.card (units K) = fintype.card K - 1 := begin classical, rw [eq_comm, nat.sub_eq_iff_eq_add (fintype.card_pos_iff.2 ⟨(0 : K)⟩)], haveI := set_fintype {a : K | a ≠ 0}, haveI := set_fintype (@set.univ K), rw [fintype.card_congr (equiv.units_equiv_ne_zero _), ← @set.card_insert _ _ {a : K | a ≠ 0} _ (not_not.2 (eq.refl (0 : K))) (set.fintype_insert _ _), fintype.card_congr (equiv.set.univ K).symm], congr; simp [set.ext_iff, classical.em] end lemma prod_univ_units_id_eq_neg_one : (∏ x : units K, x) = (-1 : units K) := begin classical, have : (∏ x in (@univ (units K) _).erase (-1), x) = 1, from prod_involution (λ x _, x⁻¹) (by simp) (λ a, by simp [units.inv_eq_self_iff] {contextual := tt}) (λ a, by simp [@inv_eq_iff_inv_eq _ _ a, eq_comm] {contextual := tt}) (by simp), rw [← insert_erase (mem_univ (-1 : units K)), prod_insert (not_mem_erase _ _), this, mul_one] end lemma pow_card_sub_one_eq_one (a : K) (ha : a ≠ 0) : a ^ (q - 1) = 1 := calc a ^ (fintype.card K - 1) = (units.mk0 a ha ^ (fintype.card K - 1) : units K) : by rw [units.coe_pow, units.coe_mk0] ... = 1 : by { classical, rw [← card_units, pow_card_eq_one], refl } lemma pow_card (a : K) : a ^ q = a := begin have hp : fintype.card K > 0 := fintype.card_pos_iff.2 (by apply_instance), by_cases h : a = 0, { rw h, apply zero_pow hp }, rw [← nat.succ_pred_eq_of_pos hp, pow_succ, nat.pred_eq_sub_one, pow_card_sub_one_eq_one a h, mul_one], end variable (K) theorem card (p : ℕ) [char_p K p] : ∃ (n : ℕ+), nat.prime p ∧ q = p^(n : ℕ) := begin haveI hp : fact p.prime := char_p.char_is_prime K p, letI : vector_space (zmod p) K := { .. (zmod.cast_hom (dvd_refl _) K).to_semimodule }, obtain ⟨n, h⟩ := vector_space.card_fintype (zmod p) K, rw zmod.card at h, refine ⟨⟨n, _⟩, hp, h⟩, apply or.resolve_left (nat.eq_zero_or_pos n), rintro rfl, rw nat.pow_zero at h, have : (0 : K) = 1, { apply fintype.card_le_one_iff.mp (le_of_eq h) }, exact absurd this zero_ne_one, end theorem card' : ∃ (p : ℕ) (n : ℕ+), nat.prime p ∧ q = p^(n : ℕ) := let ⟨p, hc⟩ := char_p.exists K in ⟨p, @finite_field.card K _ _ p hc⟩ @[simp] lemma cast_card_eq_zero : (q : K) = 0 := begin rcases char_p.exists K with ⟨p, _char_p⟩, resetI, rcases card K p with ⟨n, hp, hn⟩, simp only [char_p.cast_eq_zero_iff K p, hn], conv { congr, rw [← nat.pow_one p] }, exact nat.pow_dvd_pow _ n.2, end lemma forall_pow_eq_one_iff (i : ℕ) : (∀ x : units K, x ^ i = 1) ↔ q - 1 ∣ i := begin obtain ⟨x, hx⟩ := is_cyclic.exists_generator (units K), classical, rw [← card_units, ← order_of_eq_card_of_forall_mem_gpowers hx, order_of_dvd_iff_pow_eq_one], split, { intro h, apply h }, { intros h y, simp_rw ← mem_powers_iff_mem_gpowers at hx, rcases hx y with ⟨j, rfl⟩, rw [← pow_mul, mul_comm, pow_mul, h, one_pow], } end /-- The sum of `x ^ i` as `x` ranges over the units of a finite field of cardinality `q` is equal to `0` unless `(q - 1) ∣ i`, in which case the sum is `q - 1`. -/ lemma sum_pow_units (i : ℕ) : ∑ x : units K, (x ^ i : K) = if (q - 1) ∣ i then -1 else 0 := begin let φ : units K →* K := { to_fun := λ x, x ^ i, map_one' := by rw [units.coe_one, one_pow], map_mul' := by { intros, rw [units.coe_mul, mul_pow] } }, haveI : decidable (φ = 1) := by { classical, apply_instance }, calc ∑ x : units K, φ x = if φ = 1 then fintype.card (units K) else 0 : sum_hom_units φ ... = if (q - 1) ∣ i then -1 else 0 : _, suffices : (q - 1) ∣ i ↔ φ = 1, { simp only [this], split_ifs with h h, swap, refl, rw [card_units, nat.cast_sub, cast_card_eq_zero, nat.cast_one, zero_sub], show 1 ≤ q, from fintype.card_pos_iff.mpr ⟨0⟩ }, rw [← forall_pow_eq_one_iff, monoid_hom.ext_iff], apply forall_congr, intro x, rw [units.ext_iff, units.coe_pow, units.coe_one, monoid_hom.one_apply], refl, end /-- The sum of `x ^ i` as `x` ranges over a finite field of cardinality `q` is equal to `0` if `i < q - 1`. -/ lemma sum_pow_lt_card_sub_one (i : ℕ) (h : i < q - 1) : ∑ x : K, x ^ i = 0 := begin by_cases hi : i = 0, { simp only [hi, nsmul_one, sum_const, pow_zero, card_univ, cast_card_eq_zero], }, classical, have hiq : ¬ (q - 1) ∣ i, { contrapose! h, exact nat.le_of_dvd (nat.pos_of_ne_zero hi) h }, let φ : units K ↪ K := ⟨coe, units.ext⟩, have : univ.map φ = univ \ {0}, { ext x, simp only [true_and, embedding.coe_fn_mk, mem_sdiff, units.exists_iff_ne_zero, mem_univ, mem_map, exists_prop_of_true, mem_singleton] }, calc ∑ x : K, x ^ i = ∑ x in univ \ {(0 : K)}, x ^ i : by rw [← sum_sdiff ({0} : finset K).subset_univ, sum_singleton, zero_pow (nat.pos_of_ne_zero hi), add_zero] ... = ∑ x : units K, x ^ i : by { rw [← this, univ.sum_map φ], refl } ... = 0 : by { rw [sum_pow_units K i, if_neg], exact hiq, } end variables {K} theorem frobenius_pow {p : ℕ} [fact p.prime] [char_p K p] {n : ℕ} (hcard : q = p^n) : (frobenius K p) ^ n = 1 := begin ext, conv_rhs { rw [ring_hom.one_def, ring_hom.id_apply, ← pow_card x, hcard], }, clear hcard, induction n, {simp}, rw [pow_succ, nat.pow_succ, pow_mul, ring_hom.mul_def, ring_hom.comp_apply, frobenius_def, n_ih] end open polynomial lemma expand_card (f : polynomial K) : expand K q f = f ^ q := begin cases char_p.exists K with p hp, letI := hp, rcases finite_field.card K p with ⟨⟨n, npos⟩, ⟨hp, hn⟩⟩, letI : fact p.prime := hp, dsimp at hn, rw hn at *, rw ← map_expand_pow_char, rw [frobenius_pow hn, ring_hom.one_def, map_id], end end finite_field namespace zmod open finite_field polynomial lemma sum_two_squares (p : ℕ) [hp : fact p.prime] (x : zmod p) : ∃ a b : zmod p, a^2 + b^2 = x := begin cases hp.eq_two_or_odd with hp2 hp_odd, { substI p, revert x, exact dec_trivial }, let f : polynomial (zmod p) := X^2, let g : polynomial (zmod p) := X^2 - C x, obtain ⟨a, b, hab⟩ : ∃ a b, f.eval a + g.eval b = 0 := @exists_root_sum_quadratic _ _ _ f g (degree_X_pow 2) (degree_X_pow_sub_C dec_trivial _) (by rw [zmod.card, hp_odd]), refine ⟨a, b, _⟩, rw ← sub_eq_zero, simpa only [eval_C, eval_X, eval_pow, eval_sub, ← add_sub_assoc] using hab, end end zmod namespace char_p lemma sum_two_squares (R : Type*) [integral_domain R] (p : ℕ) [fact (0 < p)] [char_p R p] (x : ℤ) : ∃ a b : ℕ, (a^2 + b^2 : R) = x := begin haveI := char_is_prime_of_pos R p, obtain ⟨a, b, hab⟩ := zmod.sum_two_squares p x, refine ⟨a.val, b.val, _⟩, simpa using congr_arg (zmod.cast_hom (dvd_refl _) R) hab end end char_p open_locale nat open zmod /-- The Fermat-Euler totient theorem. `nat.modeq.pow_totient` is an alternative statement of the same theorem. -/ @[simp] lemma zmod.pow_totient {n : ℕ} [fact (0 < n)] (x : units (zmod n)) : x ^ φ n = 1 := by rw [← card_units_eq_totient, pow_card_eq_one] /-- The Fermat-Euler totient theorem. `zmod.pow_totient` is an alternative statement of the same theorem. -/ lemma nat.modeq.pow_totient {x n : ℕ} (h : nat.coprime x n) : x ^ φ n ≡ 1 [MOD n] := begin cases n, {simp}, rw ← zmod.eq_iff_modeq_nat, let x' : units (zmod (n+1)) := zmod.unit_of_coprime _ h, have := zmod.pow_totient x', apply_fun (coe : units (zmod (n+1)) → zmod (n+1)) at this, simpa only [-zmod.pow_totient, nat.succ_eq_add_one, nat.cast_pow, units.coe_one, nat.cast_one, cast_unit_of_coprime, units.coe_pow], end open finite_field namespace zmod /-- A variation on Fermat's little theorem. See `zmod.pow_card_sub_one_eq_one` -/ @[simp] lemma pow_card {p : ℕ} [fact p.prime] (x : zmod p) : x ^ p = x := by { have h := finite_field.pow_card x, rwa zmod.card p at h } @[simp] lemma card_units (p : ℕ) [fact p.prime] : fintype.card (units (zmod p)) = p - 1 := by rw [card_units, card] /-- Fermat's Little Theorem: for every unit `a` of `zmod p`, we have `a ^ (p - 1) = 1`. -/ theorem units_pow_card_sub_one_eq_one (p : ℕ) [fact p.prime] (a : units (zmod p)) : a ^ (p - 1) = 1 := by rw [← card_units p, pow_card_eq_one] /-- Fermat's Little Theorem: for all nonzero `a : zmod p`, we have `a ^ (p - 1) = 1`. -/ theorem pow_card_sub_one_eq_one {p : ℕ} [fact p.prime] {a : zmod p} (ha : a ≠ 0) : a ^ (p - 1) = 1 := by { have h := pow_card_sub_one_eq_one a ha, rwa zmod.card p at h } open polynomial lemma expand_card {p : ℕ} [fact p.prime] (f : polynomial (zmod p)) : expand (zmod p) p f = f ^ p := by { have h := finite_field.expand_card f, rwa zmod.card p at h } end zmod
65cc77e72d7027a029e920004456ec93b4d61657
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/linear_algebra/general_linear_group.lean
edca38a9a67fbcc1ca7fa6e772120cbcb5d6094c
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,772
lean
/- Copyright (c) 2021 Chris Birkbeck. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Birkbeck -/ import linear_algebra.matrix import linear_algebra.matrix.nonsingular_inverse import linear_algebra.special_linear_group import linear_algebra.determinant /-! # The General Linear group $GL(n, R)$ This file defines the elements of the General Linear group `general_linear_group n R`, consisting of all invertible `n` by `n` `R`-matrices. ## Main definitions * `matrix.general_linear_group` is the type of matrices over R which are units in the matrix ring. * `matrix.GL_pos` gives the subgroup of matrices with positive determinant (over a linear ordered ring). ## Tags matrix group, group, matrix inverse -/ namespace matrix universes u v open_locale matrix open linear_map /-- `GL n R` is the group of `n` by `n` `R`-matrices with unit determinant. Defined as a subtype of matrices-/ abbreviation general_linear_group (n : Type u) (R : Type v) [decidable_eq n] [fintype n] [comm_ring R] : Type* := units (matrix n n R) notation `GL` := general_linear_group namespace general_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] /-- The determinant of a unit matrix is itself a unit. -/ @[simps] def det : GL n R →* units R := { to_fun := λ A, { val := (↑A : matrix n n R).det, inv := (↑(A⁻¹) : matrix n n R).det, val_inv := by rw [←det_mul, ←mul_eq_mul, A.mul_inv, det_one], inv_val := by rw [←det_mul, ←mul_eq_mul, A.inv_mul, det_one]}, map_one' := units.ext det_one, map_mul' := λ A B, units.ext $ det_mul _ _ } /--The `GL n R` and `general_linear_group R n` groups are multiplicatively equivalent-/ def to_lin : (GL n R) ≃* (linear_map.general_linear_group R (n → R)) := units.map_equiv to_lin_alg_equiv'.to_mul_equiv /--Given a matrix with invertible determinant we get an element of `GL n R`-/ def mk' (A : matrix n n R) (h : invertible (matrix.det A)) : GL n R := unit_of_det_invertible A /--Given a matrix with unit determinant we get an element of `GL n R`-/ noncomputable def mk'' (A : matrix n n R) (h : is_unit (matrix.det A)) : GL n R := nonsing_inv_unit A h instance coe_fun : has_coe_to_fun (GL n R) (λ _, n → n → R) := { coe := λ A, A.val } lemma ext_iff (A B : GL n R) : A = B ↔ (∀ i j, (A : matrix n n R) i j = (B : matrix n n R) i j) := units.ext_iff.trans matrix.ext_iff.symm /-- Not marked `@[ext]` as the `ext` tactic already solves this. -/ lemma ext ⦃A B : GL n R⦄ (h : ∀ i j, (A : matrix n n R) i j = (B : matrix n n R) i j) : A = B := units.ext $ matrix.ext h section coe_lemmas variables (A B : GL n R) @[simp] lemma coe_fn_eq_coe : ⇑A = (↑A : matrix n n R) := rfl @[simp] lemma coe_mul : ↑(A * B) = (↑A : matrix n n R) ⬝ (↑B : matrix n n R) := rfl @[simp] lemma coe_one : ↑(1 : GL n R) = (1 : matrix n n R) := rfl lemma coe_inv : ↑(A⁻¹) = (↑A : matrix n n R)⁻¹ := begin letI := A.invertible, exact inv_of_eq_nonsing_inv (↑A : matrix n n R), end end coe_lemmas end general_linear_group namespace special_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] instance has_coe_to_general_linear_group : has_coe (special_linear_group n R) (GL n R) := ⟨λ A, ⟨↑A, ↑(A⁻¹), congr_arg coe (mul_right_inv A), congr_arg coe (mul_left_inv A)⟩⟩ end special_linear_group section variables {n : Type u} {R : Type v} [decidable_eq n] [fintype n] [linear_ordered_comm_ring R ] section variables (n R) /-- This is the subgroup of `nxn` matrices with entries over a linear ordered ring and positive determinant. -/ def GL_pos : subgroup (GL n R) := (units.pos_subgroup R).comap general_linear_group.det end @[simp] lemma mem_GL_pos (A : GL n R) : A ∈ GL_pos n R ↔ 0 < (A.det : R) := iff.rfl end section has_neg variables {n : Type u} {R : Type v} [decidable_eq n] [fintype n] [linear_ordered_comm_ring R ] [fact (even (fintype.card n))] /-- Formal operation of negation on general linear group on even cardinality `n` given by negating each element. -/ instance : has_neg (GL_pos n R) := ⟨λ g, ⟨- g, begin simp only [mem_GL_pos, general_linear_group.coe_det_apply, units.coe_neg], have := det_smul g (-1), simp only [general_linear_group.coe_fn_eq_coe, one_smul, coe_fn_coe_base', neg_smul] at this, rw this, simp [nat.neg_one_pow_of_even (fact.out (even (fintype.card n)))], have gdet := g.property, simp only [mem_GL_pos, general_linear_group.coe_det_apply, subtype.val_eq_coe] at gdet, exact gdet, end⟩⟩ @[simp] lemma GL_pos_coe_neg (g : GL_pos n R) : ↑(- g) = - (↑g : matrix n n R) := rfl @[simp]lemma GL_pos_neg_elt (g : GL_pos n R): ∀ i j, ( ↑(-g): matrix n n R) i j= - (g i j):= begin simp [coe_fn_coe_base'], end end has_neg namespace special_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [linear_ordered_comm_ring R] /-- `special_linear_group n R` embeds into `GL_pos n R` -/ def to_GL_pos : special_linear_group n R →* GL_pos n R := { to_fun := λ A, ⟨(A : GL n R), show 0 < (↑A : matrix n n R).det, from A.prop.symm ▸ zero_lt_one⟩, map_one' := subtype.ext $ units.ext $ rfl, map_mul' := λ A₁ A₂, subtype.ext $ units.ext $ rfl } instance : has_coe (special_linear_group n R) (GL_pos n R) := ⟨to_GL_pos⟩ lemma coe_eq_to_GL_pos : (coe : special_linear_group n R → GL_pos n R) = to_GL_pos := rfl lemma to_GL_pos_injective : function.injective (to_GL_pos : special_linear_group n R → GL_pos n R) := (show function.injective ((coe : GL_pos n R → matrix n n R) ∘ to_GL_pos), from subtype.coe_injective).of_comp end special_linear_group end matrix
ec2aaf670acfc7c14cf1478729bc0adbd7c1349a
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/category_theory/single_obj.lean
52b3ca32e8e740aec9994a2da0c317c71cbf91cd
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
5,250
lean
import category_theory.endomorphism category_theory.groupoid category_theory.Cat import data.equiv.algebra algebra.Mon.basic import tactic.find /-! Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov # Single-object category Single object category with a given monoid of endomorphisms. It is defined to facilitate transfering some definitions and lemmas (e.g., conjugacy etc.) from category theory to monoids and groups. ## Main definitions Given a type `α` with a monoid structure, `single_obj α` is `unit` type with `category` structure such that `End (single_obj α).star` is the monoid `α`. This can be extended to a functor `Mon ⥤ Cat`. If `α` is a group, then `single_obj α` is a groupoid. An element `x : α` can be reinterpreted as an element of `End (single_obj.star α)` using `single_obj.to_End`. ## Implementation notes - `category_struct.comp` on `End (single_obj.star α)` is `flip (*)`, not `(*)`. This way multiplication on `End` agrees with the multiplication on `α`. - By default, Lean puts instances into `category_theory` namespace instead of `category_theory.single_obj`, so we give all names explicitly. -/ universes u v w namespace category_theory /-- Type tag on `unit` used to define single-object categories and groupoids. -/ def single_obj (α : Type u) : Type := unit namespace single_obj variables (α : Type u) /-- One and `flip (*)` become `id` and `comp` for morphisms of the single object category. -/ instance category_struct [has_one α] [has_mul α] : category_struct (single_obj α) := { hom := λ _ _, α, comp := λ _ _ _ x y, y * x, id := λ _, 1 } /-- Monoid laws become category laws for the single object category. -/ instance category [monoid α] : category (single_obj α) := { comp_id' := λ _ _, one_mul, id_comp' := λ _ _, mul_one, assoc' := λ _ _ _ _ x y z, (mul_assoc z y x).symm } /-- Groupoid structure on `single_obj α` -/ instance groupoid [group α] : groupoid (single_obj α) := { inv := λ _ _ x, x⁻¹, inv_comp' := λ _ _, mul_right_inv, comp_inv' := λ _ _, mul_left_inv } protected def star : single_obj α := unit.star /-- The endomorphisms monoid of the only object in `single_obj α` is equivalent to the original monoid α. -/ def to_End [monoid α] : α ≃* End (single_obj.star α) := { map_mul' := λ x y, rfl, .. equiv.refl α } lemma to_End_def [monoid α] (x : α) : to_End α x = x := rfl /-- There is a 1-1 correspondence between monoid homomorphisms `α → β` and functors between the corresponding single-object categories. It means that `single_obj` is a fully faithful functor. -/ def map_hom (α : Type u) (β : Type v) [monoid α] [monoid β] : (α →* β) ≃ (single_obj α) ⥤ (single_obj β) := { to_fun := λ f, { obj := id, map := λ _ _, ⇑f, map_id' := λ _, f.map_one, map_comp' := λ _ _ _ x y, f.map_mul y x }, inv_fun := λ f, { to_fun := @functor.map _ _ _ _ f (single_obj.star α) (single_obj.star α), map_one' := f.map_id _, map_mul' := λ x y, f.map_comp y x }, left_inv := λ ⟨f, h₁, h₂⟩, rfl, right_inv := λ f, by cases f; obviously } lemma map_hom_id (α : Type u) [monoid α] : map_hom α α (monoid_hom.id α) = 𝟭 _ := rfl lemma map_hom_comp {α : Type u} {β : Type v} [monoid α] [monoid β] (f : α →* β) {γ : Type w} [monoid γ] (g : β →* γ) : map_hom α γ (g.comp f) = map_hom α β f ⋙ map_hom β γ g := rfl end single_obj end category_theory open category_theory namespace monoid_hom /-- Reinterpret a monoid homomorphism `f : α → β` as a functor `(single_obj α) ⥤ (single_obj β)`. See also `category_theory.single_obj.map_hom` for an equivalence between these types. -/ @[reducible] def to_functor {α : Type u} {β : Type v} [monoid α] [monoid β] (f : α →* β) : (single_obj α) ⥤ (single_obj β) := single_obj.map_hom α β f @[simp] lemma id_to_functor (α : Type u) [monoid α] : (id α).to_functor = 𝟭 _ := rfl @[simp] lemma comp_to_functor {α : Type u} {β : Type v} [monoid α] [monoid β] (f : α →* β) {γ : Type w} [monoid γ] (g : β →* γ) : (g.comp f).to_functor = f.to_functor ⋙ g.to_functor := rfl end monoid_hom namespace units variables (α : Type u) [monoid α] def to_Aut : units α ≃* Aut (single_obj.star α) := (units.map_equiv (single_obj.to_End α)).trans $ Aut.units_End_eqv_Aut _ @[simp] lemma to_Aut_hom (x : units α) : (to_Aut α x).hom = single_obj.to_End α x := rfl @[simp] lemma to_Aut_inv (x : units α) : (to_Aut α x).inv = single_obj.to_End α (x⁻¹ : units α) := rfl end units namespace Mon open category_theory /-- The fully faithful functor from `Mon` to `Cat`. -/ def to_Cat : Mon ⥤ Cat := { obj := λ x, Cat.of (single_obj x), map := λ x y f, (Mon.hom_equiv_monoid_hom x y).trans (single_obj.map_hom x y) f } instance to_Cat_full : full to_Cat := { preimage := λ x y, ((Mon.hom_equiv_monoid_hom x y).trans (single_obj.map_hom x y)).inv_fun, witness' := λ x y, by apply equiv.right_inv } instance to_Cat_faithful : faithful to_Cat := { injectivity' := λ x y, by apply equiv.injective } end Mon
2e684b47c8e0d74c9bcccde3aae9daa67e84e690
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/control/fold.lean
c6ae1e3429888c48712de8e9beacbabba3ec8fd7
[]
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
14,971
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Sean Leather -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.free_monoid import Mathlib.algebra.opposites import Mathlib.control.traversable.instances import Mathlib.control.traversable.lemmas import Mathlib.category_theory.category.default import Mathlib.category_theory.endomorphism import Mathlib.category_theory.types import Mathlib.category_theory.category.Kleisli import Mathlib.PostPort universes u u_1 namespace Mathlib /-! # List folds generalized to `traversable` Informally, we can think of `foldl` as a special case of `traverse` where we do not care about the reconstructed data structure and, in a state monad, we care about the final state. The obvious way to define `foldl` would be to use the state monad but it is nicer to reason about a more abstract interface with `fold_map` as a primitive and `fold_map_hom` as a defining property. ``` def fold_map {α ω} [has_one ω] [has_mul ω] (f : α → ω) : t α → ω := ... lemma fold_map_hom (α β) [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] (g : γ → α) (x : t γ) : f (fold_map g x) = fold_map (f ∘ g) x := ... ``` `fold_map` uses a monoid ω to accumulate a value for every element of a data structure and `fold_map_hom` uses a monoid homomorphism to substitute the monoid used by `fold_map`. The two are sufficient to define `foldl`, `foldr` and `to_list`. `to_list` permits the formulation of specifications in terms of operations on lists. Each fold function can be defined using a specialized monoid. `to_list` uses a free monoid represented as a list with concatenation while `foldl` uses endofunctions together with function composition. The definition through monoids uses `traverse` together with the applicative functor `const m` (where `m` is the monoid). As an implementation, `const` guarantees that no resource is spent on reconstructing the structure during traversal. A special class could be defined for `foldable`, similarly to Haskell, but the author cannot think of instances of `foldable` that are not also `traversable`. -/ namespace monoid /-- For a list, foldl f x [y₀,y₁] reduces as follows calc foldl f x [y₀,y₁] = foldl f (f x y₀) [y₁] : rfl ... = foldl f (f (f x y₀) y₁) [] : rfl ... = f (f x y₀) y₁ : rfl with f : α → β → α x : α [y₀,y₁] : list β We can view the above as a composition of functions: ... = f (f x y₀) y₁ : rfl ... = flip f y₁ (flip f y₀ x) : rfl ... = (flip f y₁ ∘ flip f y₀) x : rfl We can use traverse and const to construct this composition: calc const.run (traverse (λ y, const.mk' (flip f y)) [y₀,y₁]) x = const.run ((::) <$> const.mk' (flip f y₀) <*> traverse (λ y, const.mk' (flip f y)) [y₁]) x ... = const.run ((::) <$> const.mk' (flip f y₀) <*> ( (::) <$> const.mk' (flip f y₁) <*> traverse (λ y, const.mk' (flip f y)) [] )) x ... = const.run ((::) <$> const.mk' (flip f y₀) <*> ( (::) <$> const.mk' (flip f y₁) <*> pure [] )) x ... = const.run ( ((::) <$> const.mk' (flip f y₁) <*> pure []) ∘ ((::) <$> const.mk' (flip f y₀)) ) x ... = const.run ( const.mk' (flip f y₁) ∘ const.mk' (flip f y₀) ) x ... = const.run ( flip f y₁ ∘ flip f y₀ ) x ... = f (f x y₀) y₁ And this is how `const` turns a monoid into an applicative functor and how the monoid of endofunctions define `foldl`. -/ def foldl (α : Type u) := category_theory.End αᵒᵖ def foldl.mk {α : Type u} (f : α → α) : foldl α := opposite.op f def foldl.get {α : Type u} (x : foldl α) : α → α := opposite.unop x def foldl.of_free_monoid {α : Type u} {β : Type u} (f : β → α → β) (xs : free_monoid α) : foldl β := opposite.op (flip (list.foldl f) xs) def foldr (α : Type u) := category_theory.End α def foldr.mk {α : Type u} (f : α → α) : foldr α := f def foldr.get {α : Type u} (x : foldr α) : α → α := x def foldr.of_free_monoid {α : Type u} {β : Type u} (f : α → β → β) (xs : free_monoid α) : foldr β := flip (list.foldr f) xs def mfoldl (m : Type u → Type u) [Monad m] (α : Type u) := category_theory.End (category_theory.Kleisli.mk m α)ᵒᵖ def mfoldl.mk {m : Type u → Type u} [Monad m] {α : Type u} (f : α → m α) : mfoldl m α := opposite.op f def mfoldl.get {m : Type u → Type u} [Monad m] {α : Type u} (x : mfoldl m α) : α → m α := opposite.unop x def mfoldl.of_free_monoid {m : Type u → Type u} [Monad m] {α : Type u} {β : Type u} (f : β → α → m β) (xs : free_monoid α) : mfoldl m β := opposite.op (flip (mfoldl f) xs) def mfoldr (m : Type u → Type u) [Monad m] (α : Type u) := category_theory.End (category_theory.Kleisli.mk m α) def mfoldr.mk {m : Type u → Type u} [Monad m] {α : Type u} (f : α → m α) : mfoldr m α := f def mfoldr.get {m : Type u → Type u} [Monad m] {α : Type u} (x : mfoldr m α) : α → m α := x def mfoldr.of_free_monoid {m : Type u → Type u} [Monad m] {α : Type u} {β : Type u} (f : α → β → m β) (xs : free_monoid α) : mfoldr m β := flip (list.mfoldr f) xs end monoid namespace traversable def fold_map {t : Type u → Type u} [traversable t] {α : Type u} {ω : Type u} [HasOne ω] [Mul ω] (f : α → ω) : t α → ω := traverse (functor.const.mk' ∘ f) def foldl {α : Type u} {β : Type u} {t : Type u → Type u} [traversable t] (f : α → β → α) (x : α) (xs : t β) : α := monoid.foldl.get (fold_map (monoid.foldl.mk ∘ flip f) xs) x def foldr {α : Type u} {β : Type u} {t : Type u → Type u} [traversable t] (f : α → β → β) (x : β) (xs : t α) : β := monoid.foldr.get (fold_map (monoid.foldr.mk ∘ f) xs) x /-- Conceptually, `to_list` collects all the elements of a collection in a list. This idea is formalized by `lemma to_list_spec (x : t α) : to_list x = fold_map free_monoid.mk x`. The definition of `to_list` is based on `foldl` and `list.cons` for speed. It is faster than using `fold_map free_monoid.mk` because, by using `foldl` and `list.cons`, each insertion is done in constant time. As a consequence, `to_list` performs in linear. On the other hand, `fold_map free_monoid.mk` creates a singleton list around each element and concatenates all the resulting lists. In `xs ++ ys`, concatenation takes a time proportional to `length xs`. Since the order in which concatenation is evaluated is unspecified, nothing prevents each element of the traversable to be appended at the end `xs ++ [x]` which would yield a `O(n²)` run time. -/ def to_list {α : Type u} {t : Type u → Type u} [traversable t] : t α → List α := list.reverse ∘ foldl (flip List.cons) [] def length {α : Type u} {t : Type u → Type u} [traversable t] (xs : t α) : ℕ := ulift.down (foldl (fun (l : ulift ℕ) (_x : α) => ulift.up (ulift.down l + 1)) (ulift.up 0) xs) def mfoldl {α : Type u} {β : Type u} {t : Type u → Type u} [traversable t] {m : Type u → Type u} [Monad m] (f : α → β → m α) (x : α) (xs : t β) : m α := monoid.mfoldl.get (fold_map (monoid.mfoldl.mk ∘ flip f) xs) x def mfoldr {α : Type u} {β : Type u} {t : Type u → Type u} [traversable t] {m : Type u → Type u} [Monad m] (f : α → β → m β) (x : β) (xs : t α) : m β := monoid.mfoldr.get (fold_map (monoid.mfoldr.mk ∘ f) xs) x def map_fold {α : Type u} {β : Type u} [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] : applicative_transformation (functor.const α) (functor.const β) := applicative_transformation.mk (fun (x : Type u_1) => f) sorry sorry def free.mk {α : Type u} : α → free_monoid α := list.ret def free.map {α : Type u} {β : Type u} (f : α → β) : free_monoid α → free_monoid β := list.map f theorem free.map_eq_map {α : Type u} {β : Type u} (f : α → β) (xs : List α) : f <$> xs = free.map f xs := rfl protected instance free.map.is_monoid_hom {α : Type u} {β : Type u} (f : α → β) : is_monoid_hom (free.map f) := is_monoid_hom.mk (eq.mpr (id ((fun (a a_1 : free_monoid β) (e_1 : a = a_1) (ᾰ ᾰ_1 : free_monoid β) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (free.map f 1) [] (Eq.trans (Eq.trans ((fun (f f_1 : α → β) (e_1 : f = f_1) (ᾰ ᾰ_1 : free_monoid α) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg free.map e_1) e_2) f f (Eq.refl f) 1 [] free_monoid.one_def) (congr_fun (free.map.equations._eqn_1 f) [])) (list.map.equations._eqn_1 f)) 1 [] free_monoid.one_def)) (Eq.refl [])) protected instance fold_foldl {α : Type u} {β : Type u} (f : β → α → β) : is_monoid_hom (monoid.foldl.of_free_monoid f) := is_monoid_hom.mk rfl theorem foldl.unop_of_free_monoid {α : Type u} {β : Type u} (f : β → α → β) (xs : free_monoid α) (a : β) : opposite.unop (monoid.foldl.of_free_monoid f xs) a = list.foldl f a xs := rfl protected instance fold_foldr {α : Type u} {β : Type u} (f : α → β → β) : is_monoid_hom (monoid.foldr.of_free_monoid f) := is_monoid_hom.mk rfl @[simp] theorem mfoldl.unop_of_free_monoid {α : Type u} {β : Type u} (m : Type u → Type u) [Monad m] [is_lawful_monad m] (f : β → α → m β) (xs : free_monoid α) (a : β) : opposite.unop (monoid.mfoldl.of_free_monoid f xs) a = mfoldl f a xs := rfl protected instance fold_mfoldl {α : Type u} {β : Type u} (m : Type u → Type u) [Monad m] [is_lawful_monad m] (f : β → α → m β) : is_monoid_hom (monoid.mfoldl.of_free_monoid f) := is_monoid_hom.mk rfl protected instance fold_mfoldr {α : Type u} {β : Type u} (m : Type u → Type u) [Monad m] [is_lawful_monad m] (f : α → β → m β) : is_monoid_hom (monoid.mfoldr.of_free_monoid f) := is_monoid_hom.mk rfl theorem fold_map_hom {α : Type u} {β : Type u} {γ : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] (g : γ → α) (x : t γ) : f (fold_map g x) = fold_map (f ∘ g) x := Eq.trans (Eq.trans (Eq.trans rfl rfl) (is_lawful_traversable.naturality (map_fold f) (functor.const.mk' ∘ g) x)) rfl theorem fold_map_hom_free {α : Type u} {β : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] [monoid β] (f : free_monoid α → β) [is_monoid_hom f] (x : t α) : f (fold_map free.mk x) = fold_map (f ∘ free.mk) x := fold_map_hom f free.mk x theorem fold_mfoldl_cons {α : Type u} {β : Type u} {m : Type u → Type u} [Monad m] [is_lawful_monad m] (f : α → β → m α) (x : β) (y : α) : mfoldl f y (free.mk x) = f y x := sorry theorem fold_mfoldr_cons {α : Type u} {β : Type u} {m : Type u → Type u} [Monad m] [is_lawful_monad m] (f : β → α → m α) (x : β) (y : α) : list.mfoldr f y (free.mk x) = f x y := sorry @[simp] theorem foldl.of_free_monoid_comp_free_mk {α : Type u} {β : Type u} (f : α → β → α) : monoid.foldl.of_free_monoid f ∘ free.mk = monoid.foldl.mk ∘ flip f := rfl @[simp] theorem foldr.of_free_monoid_comp_free_mk {α : Type u} {β : Type u} (f : β → α → α) : monoid.foldr.of_free_monoid f ∘ free.mk = monoid.foldr.mk ∘ f := rfl @[simp] theorem mfoldl.of_free_monoid_comp_free_mk {α : Type u} {β : Type u} {m : Type u → Type u} [Monad m] [is_lawful_monad m] (f : α → β → m α) : monoid.mfoldl.of_free_monoid f ∘ free.mk = monoid.mfoldl.mk ∘ flip f := sorry @[simp] theorem mfoldr.of_free_monoid_comp_free_mk {α : Type u} {β : Type u} {m : Type u → Type u} [Monad m] [is_lawful_monad m] (f : β → α → m α) : monoid.mfoldr.of_free_monoid f ∘ free.mk = monoid.mfoldr.mk ∘ f := sorry theorem to_list_spec {α : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] (xs : t α) : to_list xs = fold_map free.mk xs := sorry theorem fold_map_map {α : Type u} {β : Type u} {γ : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] [monoid γ] (f : α → β) (g : β → γ) (xs : t α) : fold_map g (f <$> xs) = fold_map (g ∘ f) xs := sorry theorem foldl_to_list {α : Type u} {β : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] (f : α → β → α) (xs : t β) (x : α) : foldl f x xs = list.foldl f x (to_list xs) := sorry theorem foldr_to_list {α : Type u} {β : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] (f : α → β → β) (xs : t α) (x : β) : foldr f x xs = list.foldr f x (to_list xs) := sorry theorem to_list_map {α : Type u} {β : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] (f : α → β) (xs : t α) : to_list (f <$> xs) = f <$> to_list xs := sorry @[simp] theorem foldl_map {α : Type u} {β : Type u} {γ : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] (g : β → γ) (f : α → γ → α) (a : α) (l : t β) : foldl f a (g <$> l) = foldl (fun (x : α) (y : β) => f x (g y)) a l := sorry @[simp] theorem foldr_map {α : Type u} {β : Type u} {γ : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] (g : β → γ) (f : γ → α → α) (a : α) (l : t β) : foldr f a (g <$> l) = foldr (f ∘ g) a l := sorry @[simp] theorem to_list_eq_self {α : Type u} {xs : List α} : to_list xs = xs := sorry theorem length_to_list {α : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] {xs : t α} : length xs = list.length (to_list xs) := sorry theorem mfoldl_to_list {α : Type u} {β : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] {m : Type u → Type u} [Monad m] [is_lawful_monad m] {f : α → β → m α} {x : α} {xs : t β} : mfoldl f x xs = mfoldl f x (to_list xs) := sorry theorem mfoldr_to_list {α : Type u} {β : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] {m : Type u → Type u} [Monad m] [is_lawful_monad m] (f : α → β → m β) (x : β) (xs : t α) : mfoldr f x xs = list.mfoldr f x (to_list xs) := sorry @[simp] theorem mfoldl_map {α : Type u} {β : Type u} {γ : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] {m : Type u → Type u} [Monad m] [is_lawful_monad m] (g : β → γ) (f : α → γ → m α) (a : α) (l : t β) : mfoldl f a (g <$> l) = mfoldl (fun (x : α) (y : β) => f x (g y)) a l := sorry @[simp] theorem mfoldr_map {α : Type u} {β : Type u} {γ : Type u} {t : Type u → Type u} [traversable t] [is_lawful_traversable t] {m : Type u → Type u} [Monad m] [is_lawful_monad m] (g : β → γ) (f : γ → α → m α) (a : α) (l : t β) : mfoldr f a (g <$> l) = mfoldr (f ∘ g) a l := sorry
1603b319f2405d18f7a769f21863f9a2c2090476
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/multiset/finset_ops.lean
453d1bcedfcf6fac74675da2030e9c95fa3abddc
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,727
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.multiset.erase_dup /-! # Preparations for defining operations on `finset`. The operations here ignore multiplicities, and preparatory for defining the corresponding operations on `finset`. -/ namespace multiset open list variables {α : Type*} [decidable_eq α] /-! ### finset insert -/ /-- `ndinsert a s` is the lift of the list `insert` operation. This operation does not respect multiplicities, unlike `cons`, but it is suitable as an insert operation on `finset`. -/ def ndinsert (a : α) (s : multiset α) : multiset α := quot.lift_on s (λ l, (l.insert a : multiset α)) (λ s t p, quot.sound (p.insert a)) @[simp] theorem coe_ndinsert (a : α) (l : list α) : ndinsert a l = (insert a l : list α) := rfl @[simp] theorem ndinsert_zero (a : α) : ndinsert a 0 = {a} := rfl @[simp, priority 980] theorem ndinsert_of_mem {a : α} {s : multiset α} : a ∈ s → ndinsert a s = s := quot.induction_on s $ λ l h, congr_arg coe $ insert_of_mem h @[simp, priority 980] theorem ndinsert_of_not_mem {a : α} {s : multiset α} : a ∉ s → ndinsert a s = a ::ₘ s := quot.induction_on s $ λ l h, congr_arg coe $ insert_of_not_mem h @[simp] theorem mem_ndinsert {a b : α} {s : multiset α} : a ∈ ndinsert b s ↔ a = b ∨ a ∈ s := quot.induction_on s $ λ l, mem_insert_iff @[simp] theorem le_ndinsert_self (a : α) (s : multiset α) : s ≤ ndinsert a s := quot.induction_on s $ λ l, (sublist_insert _ _).subperm @[simp] theorem mem_ndinsert_self (a : α) (s : multiset α) : a ∈ ndinsert a s := mem_ndinsert.2 (or.inl rfl) theorem mem_ndinsert_of_mem {a b : α} {s : multiset α} (h : a ∈ s) : a ∈ ndinsert b s := mem_ndinsert.2 (or.inr h) @[simp, priority 980] theorem length_ndinsert_of_mem {a : α} {s : multiset α} (h : a ∈ s) : card (ndinsert a s) = card s := by simp [h] @[simp, priority 980] theorem length_ndinsert_of_not_mem {a : α} {s : multiset α} (h : a ∉ s) : card (ndinsert a s) = card s + 1 := by simp [h] theorem erase_dup_cons {a : α} {s : multiset α} : erase_dup (a ::ₘ s) = ndinsert a (erase_dup s) := by by_cases a ∈ s; simp [h] theorem nodup_ndinsert (a : α) {s : multiset α} : nodup s → nodup (ndinsert a s) := quot.induction_on s $ λ l, nodup_insert theorem ndinsert_le {a : α} {s t : multiset α} : ndinsert a s ≤ t ↔ s ≤ t ∧ a ∈ t := ⟨λ h, ⟨le_trans (le_ndinsert_self _ _) h, mem_of_le h (mem_ndinsert_self _ _)⟩, λ ⟨l, m⟩, if h : a ∈ s then by simp [h, l] else by rw [ndinsert_of_not_mem h, ← cons_erase m, cons_le_cons_iff, ← le_cons_of_not_mem h, cons_erase m]; exact l⟩ lemma attach_ndinsert (a : α) (s : multiset α) : (s.ndinsert a).attach = ndinsert ⟨a, mem_ndinsert_self a s⟩ (s.attach.map $ λp, ⟨p.1, mem_ndinsert_of_mem p.2⟩) := have eq : ∀h : ∀(p : {x // x ∈ s}), p.1 ∈ s, (λ (p : {x // x ∈ s}), ⟨p.val, h p⟩ : {x // x ∈ s} → {x // x ∈ s}) = id, from assume h, funext $ assume p, subtype.eq rfl, have ∀t (eq : s.ndinsert a = t), t.attach = ndinsert ⟨a, eq ▸ mem_ndinsert_self a s⟩ (s.attach.map $ λp, ⟨p.1, eq ▸ mem_ndinsert_of_mem p.2⟩), begin intros t ht, by_cases a ∈ s, { rw [ndinsert_of_mem h] at ht, subst ht, rw [eq, map_id, ndinsert_of_mem (mem_attach _ _)] }, { rw [ndinsert_of_not_mem h] at ht, subst ht, simp [attach_cons, h] } end, this _ rfl @[simp] theorem disjoint_ndinsert_left {a : α} {s t : multiset α} : disjoint (ndinsert a s) t ↔ a ∉ t ∧ disjoint s t := iff.trans (by simp [disjoint]) disjoint_cons_left @[simp] theorem disjoint_ndinsert_right {a : α} {s t : multiset α} : disjoint s (ndinsert a t) ↔ a ∉ s ∧ disjoint s t := by rw [disjoint_comm, disjoint_ndinsert_left]; tauto /-! ### finset union -/ /-- `ndunion s t` is the lift of the list `union` operation. This operation does not respect multiplicities, unlike `s ∪ t`, but it is suitable as a union operation on `finset`. (`s ∪ t` would also work as a union operation on finset, but this is more efficient.) -/ def ndunion (s t : multiset α) : multiset α := quotient.lift_on₂ s t (λ l₁ l₂, (l₁.union l₂ : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound $ p₁.union p₂ @[simp] theorem coe_ndunion (l₁ l₂ : list α) : @ndunion α _ l₁ l₂ = (l₁ ∪ l₂ : list α) := rfl @[simp] theorem zero_ndunion (s : multiset α) : ndunion 0 s = s := quot.induction_on s $ λ l, rfl @[simp] theorem cons_ndunion (s t : multiset α) (a : α) : ndunion (a ::ₘ s) t = ndinsert a (ndunion s t) := quotient.induction_on₂ s t $ λ l₁ l₂, rfl @[simp] theorem mem_ndunion {s t : multiset α} {a : α} : a ∈ ndunion s t ↔ a ∈ s ∨ a ∈ t := quotient.induction_on₂ s t $ λ l₁ l₂, list.mem_union theorem le_ndunion_right (s t : multiset α) : t ≤ ndunion s t := quotient.induction_on₂ s t $ λ l₁ l₂, (suffix_union_right _ _).sublist.subperm theorem subset_ndunion_right (s t : multiset α) : t ⊆ ndunion s t := subset_of_le (le_ndunion_right s t) theorem ndunion_le_add (s t : multiset α) : ndunion s t ≤ s + t := quotient.induction_on₂ s t $ λ l₁ l₂, (union_sublist_append _ _).subperm theorem ndunion_le {s t u : multiset α} : ndunion s t ≤ u ↔ s ⊆ u ∧ t ≤ u := multiset.induction_on s (by simp) (by simp [ndinsert_le, and_comm, and.left_comm] {contextual := tt}) theorem subset_ndunion_left (s t : multiset α) : s ⊆ ndunion s t := λ a h, mem_ndunion.2 $ or.inl h theorem le_ndunion_left {s} (t : multiset α) (d : nodup s) : s ≤ ndunion s t := (le_iff_subset d).2 $ subset_ndunion_left _ _ theorem ndunion_le_union (s t : multiset α) : ndunion s t ≤ s ∪ t := ndunion_le.2 ⟨subset_of_le (le_union_left _ _), le_union_right _ _⟩ theorem nodup_ndunion (s : multiset α) {t : multiset α} : nodup t → nodup (ndunion s t) := quotient.induction_on₂ s t $ λ l₁ l₂, list.nodup_union _ @[simp, priority 980] theorem ndunion_eq_union {s t : multiset α} (d : nodup s) : ndunion s t = s ∪ t := le_antisymm (ndunion_le_union _ _) $ union_le (le_ndunion_left _ d) (le_ndunion_right _ _) theorem erase_dup_add (s t : multiset α) : erase_dup (s + t) = ndunion s (erase_dup t) := quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ erase_dup_append _ _ /-! ### finset inter -/ /-- `ndinter s t` is the lift of the list `∩` operation. This operation does not respect multiplicities, unlike `s ∩ t`, but it is suitable as an intersection operation on `finset`. (`s ∩ t` would also work as a union operation on finset, but this is more efficient.) -/ def ndinter (s t : multiset α) : multiset α := filter (∈ t) s @[simp] theorem coe_ndinter (l₁ l₂ : list α) : @ndinter α _ l₁ l₂ = (l₁ ∩ l₂ : list α) := rfl @[simp] theorem zero_ndinter (s : multiset α) : ndinter 0 s = 0 := rfl @[simp, priority 980] theorem cons_ndinter_of_mem {a : α} (s : multiset α) {t : multiset α} (h : a ∈ t) : ndinter (a ::ₘ s) t = a ::ₘ (ndinter s t) := by simp [ndinter, h] @[simp, priority 980] theorem ndinter_cons_of_not_mem {a : α} (s : multiset α) {t : multiset α} (h : a ∉ t) : ndinter (a ::ₘ s) t = ndinter s t := by simp [ndinter, h] @[simp] theorem mem_ndinter {s t : multiset α} {a : α} : a ∈ ndinter s t ↔ a ∈ s ∧ a ∈ t := mem_filter @[simp] theorem nodup_ndinter {s : multiset α} (t : multiset α) : nodup s → nodup (ndinter s t) := nodup_filter _ theorem le_ndinter {s t u : multiset α} : s ≤ ndinter t u ↔ s ≤ t ∧ s ⊆ u := by simp [ndinter, le_filter, subset_iff] theorem ndinter_le_left (s t : multiset α) : ndinter s t ≤ s := (le_ndinter.1 (le_refl _)).1 theorem ndinter_subset_left (s t : multiset α) : ndinter s t ⊆ s := subset_of_le (ndinter_le_left s t) theorem ndinter_subset_right (s t : multiset α) : ndinter s t ⊆ t := (le_ndinter.1 (le_refl _)).2 theorem ndinter_le_right {s} (t : multiset α) (d : nodup s) : ndinter s t ≤ t := (le_iff_subset $ nodup_ndinter _ d).2 (ndinter_subset_right _ _) theorem inter_le_ndinter (s t : multiset α) : s ∩ t ≤ ndinter s t := le_ndinter.2 ⟨inter_le_left _ _, subset_of_le $ inter_le_right _ _⟩ @[simp, priority 980] theorem ndinter_eq_inter {s t : multiset α} (d : nodup s) : ndinter s t = s ∩ t := le_antisymm (le_inter (ndinter_le_left _ _) (ndinter_le_right _ d)) (inter_le_ndinter _ _) theorem ndinter_eq_zero_iff_disjoint {s t : multiset α} : ndinter s t = 0 ↔ disjoint s t := by rw ← subset_zero; simp [subset_iff, disjoint] end multiset
79b4712d73ced244291c64b98c2042500f2ae019
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/generalize_proofs.lean
1f380aecab555e474045baa44eca25436d90a424
[ "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
3,168
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 tactic.doc_commands /-! # `generalize_proofs` A simple tactic to find and replace all occurrences of proof terms in the context and goal with new variables. -/ open interactive interactive.types lean.parser namespace tactic private meta def collect_proofs_in : expr → list expr → list name × list expr → tactic (list name × list expr) | e ctx (ns, hs) := let go (tac : list name × list expr → tactic (list name × list expr)) : tactic (list name × list expr) := do t ← infer_type e, mcond (is_prop t) (do first (hs.map $ λ h, do t' ← infer_type h, is_def_eq t t', g ← target, change $ g.replace (λ a n, if a = e then some h else none), return (ns, hs)) <|> (let (n, ns) := (match ns with | [] := (`_x, []) | (n :: ns) := (n, ns) end : name × list name) in do generalize e n, h ← intro n, return (ns, h::hs)) <|> return (ns, hs)) (tac (ns, hs)) in match e with | expr.const _ _ := go return | expr.local_const _ _ _ _ := do t ← infer_type e, collect_proofs_in t ctx (ns, hs) | expr.mvar _ _ _ := do e ← instantiate_mvars e, match e with | expr.mvar _ _ _ := do t ← infer_type e, collect_proofs_in t ctx (ns, hs) | _ := collect_proofs_in e ctx (ns, hs) end | expr.app f x := go (λ nh, collect_proofs_in f ctx nh >>= collect_proofs_in x ctx) | expr.lam n b d e := go (λ nh, do nh ← collect_proofs_in d ctx nh, var ← mk_local' n b d, collect_proofs_in (expr.instantiate_var e var) (var::ctx) nh) | expr.pi n b d e := do nh ← collect_proofs_in d ctx (ns, hs), var ← mk_local' n b d, collect_proofs_in (expr.instantiate_var e var) (var::ctx) nh | expr.elet n t d e := go (λ nh, do nh ← collect_proofs_in t ctx nh, nh ← collect_proofs_in d ctx nh, collect_proofs_in (expr.instantiate_var e d) ctx nh) | expr.macro m l := go (λ nh, mfoldl (λ x e, collect_proofs_in e ctx x) nh l) | _ := return (ns, hs) end /-- Generalize proofs in the goal, naming them with the provided list. -/ meta def generalize_proofs (ns : list name) (loc : interactive.loc) : tactic unit := do intros_dep, hs ← local_context >>= mfilter is_proof, n ← loc.get_locals >>= revert_lst, t ← target, collect_proofs_in t [] (ns, hs), intron n <|> (intros $> ()) local postfix (name := parser.many) *:9001 := many namespace interactive /-- Generalize proofs in the goal, naming them with the provided list. For example: ```lean example : list.nth_le [1, 2] 1 dec_trivial = 2 := begin -- ⊢ [1, 2].nth_le 1 _ = 2 generalize_proofs h, -- h : 1 < [1, 2].length -- ⊢ [1, 2].nth_le 1 h = 2 end ``` -/ meta def generalize_proofs : parse ident_* → parse location → tactic unit := tactic.generalize_proofs end interactive add_tactic_doc { name := "generalize_proofs", category := doc_category.tactic, decl_names := [`tactic.interactive.generalize_proofs], tags := ["context management"] } end tactic
eb9b65e5a04bcd5710f35a6a60f1921a624b4047
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/casePrime.lean
913b1fc7c1c18fc6d4b53b5ec0e413e43a84d286
[ "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
465
lean
example (hp : p) (hq : p → q) (hr : p → r) : (s ∨ q) ∧ (r ∨ s) := by constructor case' left => apply Or.inr case' right => apply Or.inl case' left => apply hq case' right => apply hr all_goals assumption example (hp : p) (hq : p → q) (hr : p → r) : (p ∧ q) ∧ (r ∧ p) := by constructor case' left => constructor case' right => constructor case' right.left => apply hr case' left.right => apply hq all_goals assumption
03b01df9b794a63de9ea44b9f027575296896ca3
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/field_theory/intermediate_field.lean
20a47a2d28e830a2cf9ecf93ec2e581e6ef7f5f7
[ "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
9,358
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import field_theory.subfield import ring_theory.algebra_tower /-! # Intermediate fields Let `L / K` be a field extension, given as an instance `algebra K L`. This file defines the type of fields in between `K` and `L`, `intermediate_field K L`. An `intermediate_field K L` is a subfield of `L` which contains (the image of) `K`, i.e. it is a `subfield L` and a `subalgebra K L`. ## Main definitions * `intermediate_field K L` : the type of intermediate fields between `K` and `L`. * `subalgebra.to_intermediate_field`: turns a subalgebra closed under `⁻¹` into an intermediate field * `subfield.to_intermediate_field`: turns a subfield containing the image of `K` into an intermediate field * `intermediate_field.map`: map an intermediate field along an `alg_hom` ## Implementation notes Intermediate fields are defined with a structure extending `subfield` and `subalgebra`. A `subalgebra` is closed under all operations except `⁻¹`, ## TODO * `field.adjoin` currently returns a `subalgebra`, this should become an `intermediate_field`. The lattice structure on `intermediate_field` will follow from the adjunction given by `field.adjoin`. ## Tags intermediate field, field extension -/ open_locale big_operators variables (K L : Type*) [field K] [field L] [algebra K L] section set_option old_structure_cmd true /-- `S : intermediate_field K L` is a subset of `L` such that there is a field tower `L / S / K`. -/ structure intermediate_field extends subalgebra K L, subfield L /-- Reinterpret an `intermediate_field` as a `subalgebra`. -/ add_decl_doc intermediate_field.to_subalgebra /-- Reinterpret an `intermediate_field` as a `subfield`. -/ add_decl_doc intermediate_field.to_subfield end variables {K L} (S : intermediate_field K L) namespace intermediate_field instance : has_coe (intermediate_field K L) (set L) := ⟨intermediate_field.carrier⟩ @[simp] lemma coe_to_subalgebra : (S.to_subalgebra : set L) = S := rfl @[simp] lemma coe_to_subfield : (S.to_subfield : set L) = S := rfl instance : has_coe_to_sort (intermediate_field K L) := ⟨Type*, λ S, S.carrier⟩ instance : has_mem L (intermediate_field K L) := ⟨λ m S, m ∈ (S : set L)⟩ @[simp] lemma mem_mk (s : set L) (hK : ∀ x, algebra_map K L x ∈ s) (ho hm hz ha hn hi) (x : L) : x ∈ intermediate_field.mk s ho hm hz ha hK hn hi ↔ x ∈ s := iff.rfl @[simp] lemma mem_coe (x : L) : x ∈ (S : set L) ↔ x ∈ S := iff.rfl @[simp] lemma mem_to_subalgebra (s : intermediate_field K L) (x : L) : x ∈ s.to_subalgebra ↔ x ∈ s := iff.rfl @[simp] lemma mem_to_subfield (s : intermediate_field K L) (x : L) : x ∈ s.to_subfield ↔ x ∈ s := iff.rfl /-- Two intermediate fields are equal if the underlying subsets are equal. -/ theorem ext' ⦃s t : intermediate_field K L⦄ (h : (s : set L) = t) : s = t := by { cases s, cases t, congr' } /-- Two intermediate fields are equal if and only if the underlying subsets are equal. -/ protected theorem ext'_iff {s t : intermediate_field K L} : s = t ↔ (s : set L) = t := ⟨λ h, h ▸ rfl, λ h, ext' h⟩ /-- Two intermediate fields are equal if they have the same elements. -/ @[ext] theorem ext {S T : intermediate_field K L} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h /-- An intermediate field contains the image of the smaller field. -/ theorem algebra_map_mem (x : K) : algebra_map K L x ∈ S := S.algebra_map_mem' x /-- An intermediate field contains the ring's 1. -/ theorem one_mem : (1 : L) ∈ S := S.one_mem' /-- An intermediate field contains the ring's 0. -/ theorem zero_mem : (0 : L) ∈ S := S.zero_mem' /-- An intermediate field is closed under multiplication. -/ theorem mul_mem : ∀ {x y : L}, x ∈ S → y ∈ S → x * y ∈ S := S.mul_mem' /-- An intermediate field is closed under scalar multiplication. -/ theorem smul_mem {y : L} : y ∈ S → ∀ {x : K}, x • y ∈ S := S.to_subalgebra.smul_mem /-- An intermediate field is closed under addition. -/ theorem add_mem : ∀ {x y : L}, x ∈ S → y ∈ S → x + y ∈ S := S.add_mem' /-- An intermediate field is closed under subtraction -/ theorem sub_mem {x y : L} (hx : x ∈ S) (hy : y ∈ S) : x - y ∈ S := S.to_subfield.sub_mem hx hy /-- An intermediate field is closed under negation. -/ theorem neg_mem : ∀ {x : L}, x ∈ S → -x ∈ S := S.neg_mem' /-- An intermediate field is closed under inverses. -/ theorem inv_mem : ∀ {x : L}, x ∈ S → x⁻¹ ∈ S := S.inv_mem' /-- An intermediate field is closed under division. -/ theorem div_mem {x y : L} (hx : x ∈ S) (hy : y ∈ S) : x / y ∈ S := S.to_subfield.div_mem hx hy /-- Product of a list of elements in an intermediate_field is in the intermediate_field. -/ lemma list_prod_mem {l : list L} : (∀ x ∈ l, x ∈ S) → l.prod ∈ S := S.to_subfield.list_prod_mem /-- Sum of a list of elements in an intermediate field is in the intermediate_field. -/ lemma list_sum_mem {l : list L} : (∀ x ∈ l, x ∈ S) → l.sum ∈ S := S.to_subfield.list_sum_mem /-- Product of a multiset of elements in an intermediate field is in the intermediate_field. -/ lemma multiset_prod_mem (m : multiset L) : (∀ a ∈ m, a ∈ S) → m.prod ∈ S := S.to_subfield.multiset_prod_mem m /-- Sum of a multiset of elements in a `intermediate_field` is in the `intermediate_field`. -/ lemma multiset_sum_mem (m : multiset L) : (∀ a ∈ m, a ∈ S) → m.sum ∈ S := S.to_subfield.multiset_sum_mem m /-- Product of elements of an intermediate field indexed by a `finset` is in the intermediate_field. -/ lemma prod_mem {ι : Type*} {t : finset ι} {f : ι → L} (h : ∀ c ∈ t, f c ∈ S) : ∏ i in t, f i ∈ S := S.to_subfield.prod_mem h /-- Sum of elements in a `intermediate_field` indexed by a `finset` is in the `intermediate_field`. -/ lemma sum_mem {ι : Type*} {t : finset ι} {f : ι → L} (h : ∀ c ∈ t, f c ∈ S) : ∑ i in t, f i ∈ S := S.to_subfield.sum_mem h lemma pow_mem {x : L} (hx : x ∈ S) (n : ℕ) : x^n ∈ S := S.to_subfield.pow_mem hx n lemma gsmul_mem {x : L} (hx : x ∈ S) (n : ℤ) : n •ℤ x ∈ S := S.to_subfield.gsmul_mem hx n lemma coe_int_mem (n : ℤ) : (n : L) ∈ S := by simp only [← gsmul_one, gsmul_mem, one_mem] instance : inhabited (intermediate_field K L) := ⟨{ neg_mem' := λ x hx, (⊤ : subalgebra K L).neg_mem hx, inv_mem' := λ x hx, algebra.mem_top, ..(⊤ : subalgebra K L) }⟩ end intermediate_field /-- Turn a subalgebra closed under inverses into an intermediate field -/ def subalgebra.to_intermediate_field (S : subalgebra K L) (inv_mem : ∀ x ∈ S, x⁻¹ ∈ S) : intermediate_field K L := { neg_mem' := λ x, S.neg_mem, inv_mem' := inv_mem, .. S } /-- Turn a subfield of `L` containing the image of `K` into an intermediate field -/ def subfield.to_intermediate_field (S : subfield L) (algebra_map_mem : ∀ x, algebra_map K L x ∈ S) : intermediate_field K L := { algebra_map_mem' := algebra_map_mem, .. S } namespace intermediate_field /-- An intermediate field inherits a field structure -/ instance to_field : field S := S.to_subfield.to_field @[simp, norm_cast] lemma coe_add (x y : S) : (↑(x + y) : L) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_neg (x : S) : (↑(-x) : L) = -↑x := rfl @[simp, norm_cast] lemma coe_mul (x y : S) : (↑(x * y) : L) = ↑x * ↑y := rfl @[simp, norm_cast] lemma coe_inv (x : S) : (↑(x⁻¹) : L) = (↑x)⁻¹ := rfl @[simp, norm_cast] lemma coe_zero : ((0 : S) : L) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : S) : L) = 1 := rfl instance algebra : algebra K S := S.to_subalgebra.algebra instance to_algebra : algebra S L := S.to_subalgebra.to_algebra instance : is_scalar_tower K S L := is_scalar_tower.subalgebra _ _ S.to_subalgebra variables {L' : Type*} [field L'] [algebra K L'] /-- If `f : L →+* L'` fixes `K`, `S.map f` is the intermediate field between `L'` and `K` such that `x ∈ S ↔ f x ∈ S.map f`. -/ def map (f : L →ₐ[K] L') : intermediate_field K L' := { inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, S.inv_mem hx, f.map_inv x⟩ }, neg_mem' := λ x hx, (S.to_subalgebra.map f).neg_mem hx, .. S.to_subalgebra.map f} /-- The embedding from an intermediate field of `L / K` to `L`. -/ def val : S →ₐ[K] L := S.to_subalgebra.val @[simp] theorem coe_val : ⇑S.val = coe := rfl @[simp] lemma val_mk {x : L} (hx : x ∈ S) : S.val ⟨x, hx⟩ = x := rfl variables {S} lemma to_subalgebra_injective {S S' : intermediate_field K L} (h : S.to_subalgebra = S'.to_subalgebra) : S = S' := by { ext, rw [← mem_to_subalgebra, ← mem_to_subalgebra, h] } instance : partial_order (intermediate_field K L) := { le := λ S T, (S : set L) ⊆ T, le_refl := λ S, set.subset.refl S, le_trans := λ _ _ _, set.subset.trans, le_antisymm := λ S T hst hts, ext $ λ x, ⟨@hst x, @hts x⟩ } variables (S) lemma set_range_subset : set.range (algebra_map K L) ⊆ S := S.to_subalgebra.range_subset lemma field_range_le : (algebra_map K L).field_range ≤ S.to_subfield := λ x hx, S.to_subalgebra.range_subset (by rwa [set.mem_range, ← ring_hom.mem_field_range]) variables {S} end intermediate_field
7a064456d2a93cac3c89b49bc9be886e56d887f9
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/compiler/phashmap2.lean
18745da5057bd33522f9372f35caf1e2e4e15da2
[ "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
1,574
lean
import Lean.Data.PersistentHashMap import Lean.Data.Format open Lean Std Lean.PersistentHashMap abbrev Map := PersistentHashMap Nat Nat partial def formatMap : Node Nat Nat → Format | Node.collision keys vals _ => Format.sbracket $ keys.size.fold (fun i fmt => let k := keys.get! i; let v := vals.get! i; let p := if i > 0 then fmt ++ format "," ++ Format.line else fmt; p ++ "c@" ++ Format.paren (format k ++ " => " ++ format v)) Format.nil | Node.entries entries => Format.sbracket $ entries.size.fold (fun i fmt => let entry := entries.get! i; let p := if i > 0 then fmt ++ format "," ++ Format.line else fmt; p ++ match entry with | Entry.null => "<null>" | Entry.ref node => formatMap node | Entry.entry k v => Format.paren (format k ++ " => " ++ format v)) Format.nil def main : IO Unit := do let a : Array Nat := [1, 2, 3].toArray; IO.println (a.indexOf? 2); let m : Map := PersistentHashMap.empty; let m := m.insert 1 1; let m := m.insert 33 2; let m := m.insert 65 3; -- IO.println (formatMap m.root); IO.println m.stats; let m := m.erase 33; IO.println (m.find? 1); IO.println (m.find? 33); IO.println (m.find? 65); IO.println m.stats; let m := m.erase 1; IO.println (m.find? 1); IO.println (m.find? 33); IO.println (m.find? 65); IO.println m.stats; let m := m.erase 1; IO.println (m.find? 1); IO.println (m.find? 33); IO.println (m.find? 65); let m := m.erase 65; IO.println (m.find? 1); IO.println (m.find? 33); IO.println (m.find? 65); IO.println m.stats
c21f9759d97b584355eb5a29c3fc2a98ba132292
367134ba5a65885e863bdc4507601606690974c1
/src/group_theory/submonoid/operations.lean
5c4a5c011456ff0c3a013b1fe9dd4c8752e18ffe
[ "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
24,221
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.basic import data.equiv.mul_add import algebra.group.prod import algebra.group.inj_surj /-! # Operations on `submonoid`s In this file we define various operations on `submonoid`s and `monoid_hom`s. ## Main definitions ### Conversion between multiplicative and additive definitions * `submonoid.to_add_submonoid`, `submonoid.of_add_submonoid`, `add_submonoid.to_submonoid`, `add_submonoid.of_submonoid`: convert between multiplicative and additive submonoids of `M`, `multiplicative M`, and `additive M`. * `submonoid.add_submonoid_equiv`: equivalence between `submonoid M` and `add_submonoid (additive M)`. ### (Commutative) monoid structure on a submonoid * `submonoid.to_monoid`, `submonoid.to_comm_monoid`: a submonoid inherits a (commutative) monoid structure. ### Operations on submonoids * `submonoid.comap`: preimage of a submonoid under a monoid homomorphism as a submonoid of the domain; * `submonoid.map`: image of a submonoid under a monoid homomorphism as a submonoid of the codomain; * `submonoid.prod`: product of two submonoids `s : submonoid M` and `t : submonoid N` as a submonoid of `M × N`; ### Monoid homomorphisms between submonoid * `submonoid.subtype`: embedding of a submonoid into the ambient monoid. * `submonoid.inclusion`: given two submonoids `S`, `T` such that `S ≤ T`, `S.inclusion T` is the inclusion of `S` into `T` as a monoid homomorphism; * `mul_equiv.submonoid_congr`: converts a proof of `S = T` into a monoid isomorphism between `S` and `T`. * `submonoid.prod_equiv`: monoid isomorphism between `s.prod t` and `s × t`; ### Operations on `monoid_hom`s * `monoid_hom.mrange`: range of a monoid homomorphism as a submonoid of the codomain; * `monoid_hom.mrestrict`: restrict a monoid homomorphism to a submonoid; * `monoid_hom.cod_mrestrict`: restrict the codomain of a monoid homomorphism to a submonoid; * `monoid_hom.mrange_restrict`: restrict a monoid homomorphism to its range; ## Tags submonoid, range, product, map, comap -/ variables {M N P : Type*} [monoid M] [monoid N] [monoid P] (S : submonoid M) /-! ### Conversion to/from `additive`/`multiplicative` -/ /-- Map from submonoids of monoid `M` to `add_submonoid`s of `additive M`. -/ def submonoid.to_add_submonoid {M : Type*} [monoid M] (S : submonoid M) : add_submonoid (additive M) := { carrier := S.carrier, zero_mem' := S.one_mem', add_mem' := S.mul_mem' } /-- Map from `add_submonoid`s of `additive M` to submonoids of `M`. -/ def submonoid.of_add_submonoid {M : Type*} [monoid M] (S : add_submonoid (additive M)) : submonoid M := { carrier := S.carrier, one_mem' := S.zero_mem', mul_mem' := S.add_mem' } /-- Map from `add_submonoid`s of `add_monoid M` to submonoids of `multiplicative M`. -/ def add_submonoid.to_submonoid {M : Type*} [add_monoid M] (S : add_submonoid M) : submonoid (multiplicative M) := { carrier := S.carrier, one_mem' := S.zero_mem', mul_mem' := S.add_mem' } /-- Map from submonoids of `multiplicative M` to `add_submonoid`s of `add_monoid M`. -/ def add_submonoid.of_submonoid {M : Type*} [add_monoid M] (S : submonoid (multiplicative M)) : add_submonoid M := { carrier := S.carrier, zero_mem' := S.one_mem', add_mem' := S.mul_mem' } /-- Submonoids of monoid `M` are isomorphic to additive submonoids of `additive M`. -/ def submonoid.add_submonoid_equiv (M : Type*) [monoid M] : submonoid M ≃ add_submonoid (additive M) := { to_fun := submonoid.to_add_submonoid, inv_fun := submonoid.of_add_submonoid, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl } namespace submonoid open set /-! ### `comap` and `map` -/ /-- The preimage of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The preimage of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def comap (f : M →* N) (S : submonoid N) : submonoid M := { carrier := (f ⁻¹' S), one_mem' := show f 1 ∈ S, by rw f.map_one; exact S.one_mem, mul_mem' := λ a b ha hb, show f (a * b) ∈ S, by rw f.map_mul; exact S.mul_mem ha hb } @[simp, to_additive] lemma coe_comap (S : submonoid N) (f : M →* N) : (S.comap f : set M) = f ⁻¹' S := rfl @[simp, to_additive] lemma mem_comap {S : submonoid N} {f : M →* N} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl @[to_additive] lemma comap_comap (S : submonoid P) (g : N →* P) (f : M →* N) : (S.comap g).comap f = S.comap (g.comp f) := rfl @[simp, to_additive] lemma comap_id (S : submonoid P) : S.comap (monoid_hom.id _) = S := ext (by simp) /-- The image of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The image of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def map (f : M →* N) (S : submonoid M) : submonoid N := { carrier := (f '' S), one_mem' := ⟨1, S.one_mem, f.map_one⟩, mul_mem' := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, exact ⟨x * y, S.mul_mem hx hy, by rw f.map_mul; refl⟩ end } @[simp, to_additive] lemma coe_map (f : M →* N) (S : submonoid M) : (S.map f : set N) = f '' S := rfl @[simp, to_additive] lemma mem_map {f : M →* N} {S : submonoid M} {y : N} : y ∈ S.map f ↔ ∃ x ∈ S, f x = y := mem_image_iff_bex @[to_additive] lemma mem_map_of_mem (f : M →* N) (x : S) : f x ∈ S.map f := mem_image_of_mem f x.2 @[to_additive] lemma map_map (g : N →* P) (f : M →* N) : (S.map f).map g = S.map (g.comp f) := ext' $ image_image _ _ _ @[to_additive] lemma map_le_iff_le_comap {f : M →* N} {S : submonoid M} {T : submonoid N} : S.map f ≤ T ↔ S ≤ T.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : M →* N) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap @[to_additive] lemma map_le_of_le_comap {T : submonoid N} {f : M →* N} : S ≤ T.comap f → S.map f ≤ T := (gc_map_comap f).l_le @[to_additive] lemma le_comap_of_map_le {T : submonoid N} {f : M →* N} : S.map f ≤ T → S ≤ T.comap f := (gc_map_comap f).le_u @[to_additive] lemma le_comap_map {f : M →* N} : S ≤ (S.map f).comap f := (gc_map_comap f).le_u_l _ @[to_additive] lemma map_comap_le {S : submonoid N} {f : M →* N} : (S.comap f).map f ≤ S := (gc_map_comap f).l_u_le _ @[to_additive] lemma monotone_map {f : M →* N} : monotone (map f) := (gc_map_comap f).monotone_l @[to_additive] lemma monotone_comap {f : M →* N} : monotone (comap f) := (gc_map_comap f).monotone_u @[simp, to_additive] lemma map_comap_map {f : M →* N} : ((S.map f).comap f).map f = S.map f := congr_fun ((gc_map_comap f).l_u_l_eq_l) _ @[simp, to_additive] lemma comap_map_comap {S : submonoid N} {f : M →* N} : ((S.comap f).map f).comap f = S.comap f := congr_fun ((gc_map_comap f).u_l_u_eq_u) _ @[to_additive] lemma map_sup (S T : submonoid M) (f : M →* N) : (S ⊔ T).map f = S.map f ⊔ T.map f := (gc_map_comap f).l_sup @[to_additive] lemma map_supr {ι : Sort*} (f : M →* N) (s : ι → submonoid M) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr @[to_additive] lemma comap_inf (S T : submonoid N) (f : M →* N) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f := (gc_map_comap f).u_inf @[to_additive] lemma comap_infi {ι : Sort*} (f : M →* N) (s : ι → submonoid N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp, to_additive] lemma map_bot (f : M →* N) : (⊥ : submonoid M).map f = ⊥ := (gc_map_comap f).l_bot @[simp, to_additive] lemma comap_top (f : M →* N) : (⊤ : submonoid N).comap f = ⊤ := (gc_map_comap f).u_top @[simp, to_additive] lemma map_id (S : submonoid M) : S.map (monoid_hom.id M) = S := ext (λ x, ⟨λ ⟨_, h, rfl⟩, h, λ h, ⟨_, h, rfl⟩⟩) section galois_coinsertion variables {ι : Type*} {f : M →* N} (hf : function.injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (λ S x, by simp [mem_comap, mem_map, hf.eq_iff]) lemma comap_map_eq_of_injective (S : submonoid M) : (S.map f).comap f = S := (gci_map_comap hf).u_l_eq _ lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective lemma comap_inf_map_of_injective (S T : submonoid M) : (S.map f ⊓ T.map f).comap f = S ⊓ T := (gci_map_comap hf).u_inf_l _ _ lemma comap_infi_map_of_injective (S : ι → submonoid M) : (⨅ i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ lemma comap_sup_map_of_injective (S T : submonoid M) : (S.map f ⊔ T.map f).comap f = S ⊔ T := (gci_map_comap hf).u_sup_l _ _ lemma comap_supr_map_of_injective (S : ι → submonoid M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ lemma map_le_map_iff_of_injective {S T : submonoid M} : S.map f ≤ T.map f ↔ S ≤ T := (gci_map_comap hf).l_le_l_iff lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion section galois_insertion variables {ι : Type*} {f : M →* N} (hf : function.surjective f) include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (λ S x h, let ⟨y, hy⟩ := hf x in mem_map.2 ⟨y, by simp [hy, h]⟩) lemma map_comap_eq_of_surjective (S : submonoid N) : (S.comap f).map f = S := (gi_map_comap hf).l_u_eq _ lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective lemma map_inf_comap_of_surjective (S T : submonoid N) : (S.comap f ⊓ T.comap f).map f = S ⊓ T := (gi_map_comap hf).l_inf_u _ _ lemma map_infi_comap_of_surjective (S : ι → submonoid N) : (⨅ i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ lemma map_sup_comap_of_surjective (S T : submonoid N) : (S.comap f ⊔ T.comap f).map f = S ⊔ T := (gi_map_comap hf).l_sup_u _ _ lemma map_supr_comap_of_surjective (S : ι → submonoid N) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ lemma comap_le_comap_iff_of_surjective {S T : submonoid N} : S.comap f ≤ T.comap f ↔ S ≤ T := (gi_map_comap hf).u_le_u_iff lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion /-- A submonoid of a monoid inherits a multiplication. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an addition."] instance has_mul : has_mul S := ⟨λ a b, ⟨a.1 * b.1, S.mul_mem a.2 b.2⟩⟩ /-- A submonoid of a monoid inherits a 1. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits a zero."] instance has_one : has_one S := ⟨⟨_, S.one_mem⟩⟩ @[simp, to_additive] lemma coe_mul (x y : S) : (↑(x * y) : M) = ↑x * ↑y := rfl @[simp, to_additive] lemma coe_one : ((1 : S) : M) = 1 := rfl attribute [norm_cast] coe_mul coe_one attribute [norm_cast] add_submonoid.coe_add add_submonoid.coe_zero /-- A submonoid of a monoid inherits a monoid structure. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an `add_monoid` structure."] instance to_monoid {M : Type*} [monoid M] (S : submonoid M) : monoid S := S.coe_injective.monoid coe rfl (λ _ _, rfl) /-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `add_comm_monoid` is an `add_comm_monoid`."] instance to_comm_monoid {M} [comm_monoid M] (S : submonoid M) : comm_monoid S := S.coe_injective.comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of an `ordered_comm_monoid` is an `ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_add_comm_monoid` is an `ordered_add_comm_monoid`."] instance to_ordered_comm_monoid {M} [ordered_comm_monoid M] (S : submonoid M) : ordered_comm_monoid S := S.coe_injective.ordered_comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of a `linear_ordered_comm_monoid` is a `linear_ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_add_comm_monoid` is a `linear_ordered_add_comm_monoid`."] instance to_linear_ordered_comm_monoid {M} [linear_ordered_comm_monoid M] (S : submonoid M) : linear_ordered_comm_monoid S := S.coe_injective.linear_ordered_comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of an `ordered_cancel_comm_monoid` is an `ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_cancel_add_comm_monoid` is an `ordered_cancel_add_comm_monoid`."] instance to_ordered_cancel_comm_monoid {M} [ordered_cancel_comm_monoid M] (S : submonoid M) : ordered_cancel_comm_monoid S := S.coe_injective.ordered_cancel_comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of a `linear_ordered_cancel_comm_monoid` is a `linear_ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_cancel_add_comm_monoid` is a `linear_ordered_cancel_add_comm_monoid`."] instance to_linear_ordered_cancel_comm_monoid {M} [linear_ordered_cancel_comm_monoid M] (S : submonoid M) : linear_ordered_cancel_comm_monoid S := S.coe_injective.linear_ordered_cancel_comm_monoid coe rfl (λ _ _, rfl) /-- The natural monoid hom from a submonoid of monoid `M` to `M`. -/ @[to_additive "The natural monoid hom from an `add_submonoid` of `add_monoid` `M` to `M`."] def subtype : S →* M := ⟨coe, rfl, λ _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑S.subtype = coe := rfl /-- An induction principle on elements of the type `submonoid.closure s`. If `p` holds for `1` and all elements of `s`, and is preserved under multiplication, then `p` holds for all elements of the closure of `s`. The difference with `submonoid.closure_induction` is that this acts on the subtype. -/ @[to_additive "An induction principle on elements of the type `add_submonoid.closure s`. If `p` holds for `0` and all elements of `s`, and is preserved under addition, then `p` holds for all elements of the closure of `s`. The difference with `add_submonoid.closure_induction` is that this acts on the subtype."] lemma closure_induction' (s : set M) {p : closure s → Prop} (Hs : ∀ x (h : x ∈ s), p ⟨x, subset_closure h⟩) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) (x : closure s) : p x := subtype.rec_on x $ λ x hx, begin refine exists.elim _ (λ (hx : x ∈ closure s) (hc : p ⟨x, hx⟩), hc), exact closure_induction hx (λ x hx, ⟨subset_closure hx, Hs x hx⟩) ⟨one_mem _, H1⟩ (λ x y hx hy, exists.elim hx $ λ hx' hx, exists.elim hy $ λ hy' hy, ⟨mul_mem _ hx' hy', Hmul _ _ hx hy⟩), end attribute [elab_as_eliminator] submonoid.closure_induction' add_submonoid.closure_induction' /-- Given `submonoid`s `s`, `t` of monoids `M`, `N` respectively, `s × t` as a submonoid of `M × N`. -/ @[to_additive prod "Given `add_submonoid`s `s`, `t` of `add_monoid`s `A`, `B` respectively, `s × t` as an `add_submonoid` of `A × B`."] def prod (s : submonoid M) (t : submonoid N) : submonoid (M × N) := { carrier := (s : set M).prod t, one_mem' := ⟨s.one_mem, t.one_mem⟩, mul_mem' := λ p q hp hq, ⟨s.mul_mem hp.1 hq.1, t.mul_mem hp.2 hq.2⟩ } @[to_additive coe_prod] lemma coe_prod (s : submonoid M) (t : submonoid N) : (s.prod t : set (M × N)) = (s : set M).prod (t : set N) := rfl @[to_additive mem_prod] lemma mem_prod {s : submonoid M} {t : submonoid N} {p : M × N} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[to_additive prod_mono] lemma prod_mono {s₁ s₂ : submonoid M} {t₁ t₂ : submonoid N} (hs : s₁ ≤ s₂) (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht @[to_additive prod_top] lemma prod_top (s : submonoid M) : s.prod (⊤ : submonoid N) = s.comap (monoid_hom.fst M N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (s : submonoid N) : (⊤ : submonoid M).prod s = s.comap (monoid_hom.snd M N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊤ : submonoid M).prod (⊤ : submonoid N) = ⊤ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (⊥ : submonoid M).prod (⊥ : submonoid N) = ⊥ := ext' $ by simp [coe_prod, prod.one_eq_mk] /-- The product of submonoids is isomorphic to their product as monoids. -/ @[to_additive prod_equiv "The product of additive submonoids is isomorphic to their product as additive monoids"] def prod_equiv (s : submonoid M) (t : submonoid N) : s.prod t ≃* s × t := { map_mul' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } open monoid_hom @[to_additive] lemma map_inl (s : submonoid M) : s.map (inl M N) = s.prod ⊥ := ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨hx, set.mem_singleton 1⟩, λ ⟨hps, hp1⟩, ⟨p.1, hps, prod.ext rfl $ (set.eq_of_mem_singleton hp1).symm⟩⟩ @[to_additive] lemma map_inr (s : submonoid N) : s.map (inr M N) = prod ⊥ s := ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨set.mem_singleton 1, hx⟩, λ ⟨hp1, hps⟩, ⟨p.2, hps, prod.ext (set.eq_of_mem_singleton hp1).symm rfl⟩⟩ @[simp, to_additive prod_bot_sup_bot_prod] lemma prod_bot_sup_bot_prod (s : submonoid M) (t : submonoid N) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono (le_refl s) bot_le) (prod_mono bot_le (le_refl t))) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem _ ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, set.mem_singleton 1⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨set.mem_singleton 1, hp.2⟩) end submonoid namespace monoid_hom open submonoid /-- The range of a monoid homomorphism is a submonoid. -/ @[to_additive "The range of an `add_monoid_hom` is an `add_submonoid`."] def mrange (f : M →* N) : submonoid N := (⊤ : submonoid M).map f @[simp, to_additive] lemma coe_mrange (f : M →* N) : (f.mrange : set N) = set.range f := set.image_univ @[simp, to_additive] lemma mem_mrange {f : M →* N} {y : N} : y ∈ f.mrange ↔ ∃ x, f x = y := by simp [mrange] @[to_additive] lemma map_mrange (g : N →* P) (f : M →* N) : f.mrange.map g = (g.comp f).mrange := (⊤ : submonoid M).map_map g f @[to_additive] lemma mrange_top_iff_surjective {N} [monoid N] {f : M →* N} : f.mrange = (⊤ : submonoid N) ↔ function.surjective f := submonoid.ext'_iff.trans $ iff.trans (by rw [coe_mrange, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid hom is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` hom is the whole of the codomain."] lemma mrange_top_of_surjective {N} [monoid N] (f : M →* N) (hf : function.surjective f) : f.mrange = (⊤ : submonoid N) := mrange_top_iff_surjective.2 hf @[to_additive] lemma mrange_eq_map (f : M →* N) : f.mrange = map f ⊤ := rfl @[to_additive] lemma mclosure_preimage_le (f : M →* N) (s : set N) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals the `add_submonoid` generated by the image of the set."] lemma map_mclosure (f : M →* N) (s : set M) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (mclosure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) /-- Restriction of a monoid hom to a submonoid of the domain. -/ @[to_additive "Restriction of an add_monoid hom to an `add_submonoid` of the domain."] def mrestrict {N : Type*} [monoid N] (f : M →* N) (S : submonoid M) : S →* N := f.comp S.subtype @[simp, to_additive] lemma mrestrict_apply {N : Type*} [monoid N] (f : M →* N) (x : S) : f.mrestrict S x = f x := rfl /-- Restriction of a monoid hom to a submonoid of the codomain. -/ @[to_additive "Restriction of an `add_monoid` hom to an `add_submonoid` of the codomain."] def cod_mrestrict (f : M →* N) (S : submonoid N) (h : ∀ x, f x ∈ S) : M →* S := { to_fun := λ n, ⟨f n, h n⟩, map_one' := subtype.eq f.map_one, map_mul' := λ x y, subtype.eq (f.map_mul x y) } /-- Restriction of a monoid hom to its range interpreted as a submonoid. -/ @[to_additive "Restriction of an `add_monoid` hom to its range interpreted as a submonoid."] def mrange_restrict {N} [monoid N] (f : M →* N) : M →* f.mrange := f.cod_mrestrict f.mrange $ λ x, ⟨x, submonoid.mem_top x, rfl⟩ @[simp, to_additive] lemma coe_mrange_restrict {N} [monoid N] (f : M →* N) (x : M) : (f.mrange_restrict x : N) = f x := rfl end monoid_hom namespace submonoid open monoid_hom @[to_additive] lemma mrange_inl : (inl M N).mrange = prod ⊤ ⊥ := map_inl ⊤ @[to_additive] lemma mrange_inr : (inr M N).mrange = prod ⊥ ⊤ := map_inr ⊤ @[to_additive] lemma mrange_inl' : (inl M N).mrange = comap (snd M N) ⊥ := mrange_inl.trans (top_prod _) @[to_additive] lemma mrange_inr' : (inr M N).mrange = comap (fst M N) ⊥ := mrange_inr.trans (prod_top _) @[simp, to_additive] lemma mrange_fst : (fst M N).mrange = ⊤ := (fst M N).mrange_top_of_surjective $ @prod.fst_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma mrange_snd : (snd M N).mrange = ⊤ := (snd M N).mrange_top_of_surjective $ @prod.snd_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma mrange_inl_sup_mrange_inr : (inl M N).mrange ⊔ (inr M N).mrange = ⊤ := by simp only [mrange_inl, mrange_inr, prod_bot_sup_bot_prod, top_prod_top] /-- The monoid hom associated to an inclusion of submonoids. -/ @[to_additive "The `add_monoid` hom associated to an inclusion of submonoids."] def inclusion {S T : submonoid M} (h : S ≤ T) : S →* T := S.subtype.cod_mrestrict _ (λ x, h x.2) @[simp, to_additive] lemma range_subtype (s : submonoid M) : s.subtype.mrange = s := ext' $ (coe_mrange _).trans $ subtype.range_coe @[to_additive] lemma eq_bot_iff_forall : S = ⊥ ↔ ∀ x ∈ S, x = (1 : M) := begin split, { intros h x x_in, rwa [h, mem_bot] at x_in }, { intros h, ext x, rw mem_bot, exact ⟨h x, by { rintros rfl, exact S.one_mem }⟩ }, end @[to_additive] lemma nontrivial_iff_exists_ne_one (S : submonoid M) : nontrivial S ↔ ∃ x ∈ S, x ≠ (1:M) := begin split, { introI h, rcases exists_ne (1 : S) with ⟨⟨h, h_in⟩, h_ne⟩, use [h, h_in], intro hyp, apply h_ne, simpa [hyp] }, { rintros ⟨x, x_in, hx⟩, apply nontrivial_of_ne (⟨x, x_in⟩ : S) 1, intro hyp, apply hx, simpa [has_one.one] using hyp }, end /-- A submonoid is either the trivial submonoid or nontrivial. -/ @[to_additive] lemma bot_or_nontrivial (S : submonoid M) : S = ⊥ ∨ nontrivial S := begin classical, by_cases h : ∀ x ∈ S, x = (1 : M), { left, exact S.eq_bot_iff_forall.mpr h }, { right, push_neg at h, simpa [nontrivial_iff_exists_ne_one] using h }, end /-- A submonoid is either the trivial submonoid or contains a nonzero element. -/ @[to_additive] lemma bot_or_exists_ne_one (S : submonoid M) : S = ⊥ ∨ ∃ x ∈ S, x ≠ (1:M) := begin convert S.bot_or_nontrivial, rw nontrivial_iff_exists_ne_one end end submonoid namespace mul_equiv variables {S} {T : submonoid M} /-- Makes the identity isomorphism from a proof that two submonoids of a multiplicative monoid are equal. -/ @[to_additive "Makes the identity additive isomorphism from a proof two submonoids of an additive monoid are equal."] def submonoid_congr (h : S = T) : S ≃* T := { map_mul' := λ _ _, rfl, ..equiv.set_congr $ submonoid.ext'_iff.1 h } end mul_equiv
ae01fc9c598839a564b08dd885aee1f86cfaf00d
ce89339993655da64b6ccb555c837ce6c10f9ef4
/bluejam/chap4_exercise7.lean
0ea89420768643f6ec80b21723b3f0626490dee6
[]
no_license
zeptometer/LearnLean
ef32dc36a22119f18d843f548d0bb42f907bff5d
bb84d5dbe521127ba134d4dbf9559b294a80b9f7
refs/heads/master
1,625,710,824,322
1,601,382,570,000
1,601,382,570,000
195,228,870
2
0
null
null
null
null
UTF-8
Lean
false
false
227
lean
#check sub_self set_option trace.check true example (x : ℤ) : x * 0 = 0 := calc x * 0 = x * (0 - 0) : by rw sub_self (0:ℤ) ... = x * 0 - x * 0 : by rw mul_sub x 0 0 ... = 0 : by rw sub_self
4e4d6f68e70494739a4a5a57cdb960d408e43309
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/data/finset/lattice.lean
5e3637a148ba5d3e2fbdd2fed2def36a4efb7754
[ "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
59,379
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.finset.fold import data.finset.option import data.finset.prod import data.multiset.lattice import order.complete_lattice /-! # Lattice operations on finsets -/ variables {α β γ ι : Type*} namespace finset open multiset order_dual /-! ### sup -/ section sup -- TODO: define with just `[has_bot α]` where some lemmas hold without requiring `[order_bot α]` variables [semilattice_sup α] [order_bot α] /-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/ def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f variables {s s₁ s₂ : finset β} {f g : β → α} lemma sup_def : s.sup f = (s.1.map f).sup := rfl @[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ := fold_empty @[simp] lemma sup_cons {b : β} (h : b ∉ s) : (cons b s h).sup f = f b ⊔ s.sup f := fold_cons h @[simp] lemma sup_insert [decidable_eq β] {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f := fold_insert_idem lemma sup_image [decidable_eq β] (s : finset γ) (f : γ → β) (g : β → α) : (s.image f).sup g = s.sup (g ∘ f) := fold_image_idem @[simp] lemma sup_map (s : finset γ) (f : γ ↪ β) (g : β → α) : (s.map f).sup g = s.sup (g ∘ f) := fold_map @[simp] lemma sup_singleton {b : β} : ({b} : finset β).sup f = f b := sup_singleton lemma sup_union [decidable_eq β] : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f := finset.induction_on s₁ (by rw [empty_union, sup_empty, bot_sup_eq]) $ λ a s has ih, by rw [insert_union, sup_insert, sup_insert, ih, sup_assoc] lemma sup_sup : s.sup (f ⊔ g) = s.sup f ⊔ s.sup g := begin refine finset.cons_induction_on s _ (λ b t _ h, _), { rw [sup_empty, sup_empty, sup_empty, bot_sup_eq] }, { rw [sup_cons, sup_cons, sup_cons, h], exact sup_sup_sup_comm _ _ _ _ } end theorem sup_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.sup f = s₂.sup g := by subst hs; exact finset.fold_congr hfg @[simp] protected lemma sup_le_iff {a : α} : s.sup f ≤ a ↔ (∀b ∈ s, f b ≤ a) := begin apply iff.trans multiset.sup_le, simp only [multiset.mem_map, and_imp, exists_imp_distrib], exact ⟨λ k b hb, k _ _ hb rfl, λ k a' b hb h, h ▸ k _ hb⟩, end @[simp] lemma sup_bUnion [decidable_eq β] (s : finset γ) (t : γ → finset β) : (s.bUnion t).sup f = s.sup (λ x, (t x).sup f) := eq_of_forall_ge_iff $ λ c, by simp [@forall_swap _ β] lemma sup_const {s : finset β} (h : s.nonempty) (c : α) : s.sup (λ _, c) = c := eq_of_forall_ge_iff $ λ b, finset.sup_le_iff.trans h.forall_const @[simp] lemma sup_bot (s : finset β) : s.sup (λ _, ⊥) = (⊥ : α) := begin obtain rfl | hs := s.eq_empty_or_nonempty, { exact sup_empty }, { exact sup_const hs _ } end lemma sup_ite (p : β → Prop) [decidable_pred p] : s.sup (λ i, ite (p i) (f i) (g i)) = (s.filter p).sup f ⊔ (s.filter (λ i, ¬ p i)).sup g := fold_ite _ lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a := finset.sup_le_iff.2 lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f := finset.sup_le_iff.1 le_rfl _ hb lemma sup_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.sup f ≤ s.sup g := sup_le (λ b hb, le_trans (h b hb) (le_sup hb)) lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f := sup_le $ assume b hb, le_sup (h hb) lemma sup_comm (s : finset β) (t : finset γ) (f : β → γ → α) : s.sup (λ b, t.sup (f b)) = t.sup (λ c, s.sup (λ b, f b c)) := begin refine eq_of_forall_ge_iff (λ a, _), simp_rw finset.sup_le_iff, exact ⟨λ h c hc b hb, h b hb c hc, λ h b hb c hc, h c hc b hb⟩, end @[simp] lemma sup_attach (s : finset β) (f : β → α) : s.attach.sup (λ x, f x) = s.sup f := (s.attach.sup_map (function.embedding.subtype _) f).symm.trans $ congr_arg _ attach_map_val /-- See also `finset.product_bUnion`. -/ lemma sup_product_left (s : finset β) (t : finset γ) (f : β × γ → α) : (s ×ˢ t).sup f = s.sup (λ i, t.sup $ λ i', f ⟨i, i'⟩) := begin refine le_antisymm _ (sup_le (λ i hi, sup_le $ λ i' hi', le_sup $ mem_product.2 ⟨hi, hi'⟩)), refine sup_le _, rintro ⟨i, i'⟩ hi, rw mem_product at hi, refine le_trans _ (le_sup hi.1), convert le_sup hi.2, end lemma sup_product_right (s : finset β) (t : finset γ) (f : β × γ → α) : (s ×ˢ t).sup f = t.sup (λ i', s.sup $ λ i, f ⟨i, i'⟩) := by rw [sup_product_left, sup_comm] @[simp] lemma sup_erase_bot [decidable_eq α] (s : finset α) : (s.erase ⊥).sup id = s.sup id := begin refine (sup_mono (s.erase_subset _)).antisymm (finset.sup_le_iff.2 $ λ a ha, _), obtain rfl | ha' := eq_or_ne a ⊥, { exact bot_le }, { exact le_sup (mem_erase.2 ⟨ha', ha⟩) } end lemma sup_sdiff_right {α β : Type*} [generalized_boolean_algebra α] (s : finset β) (f : β → α) (a : α) : s.sup (λ b, f b \ a) = s.sup f \ a := begin refine finset.cons_induction_on s _ (λ b t _ h, _), { rw [sup_empty, sup_empty, bot_sdiff] }, { rw [sup_cons, sup_cons, h, sup_sdiff] } end lemma comp_sup_eq_sup_comp [semilattice_sup γ] [order_bot γ] {s : finset β} {f : β → α} (g : α → γ) (g_sup : ∀ x y, g (x ⊔ y) = g x ⊔ g y) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) := finset.cons_induction_on s bot (λ c t hc ih, by rw [sup_cons, sup_cons, g_sup, ih]) /-- Computing `sup` in a subtype (closed under `sup`) is the same as computing it in `α`. -/ lemma sup_coe {P : α → Prop} {Pbot : P ⊥} {Psup : ∀{{x y}}, P x → P y → P (x ⊔ y)} (t : finset β) (f : β → {x : α // P x}) : (@sup _ _ (subtype.semilattice_sup Psup) (subtype.order_bot Pbot) t f : α) = t.sup (λ x, f x) := by { rw [comp_sup_eq_sup_comp coe]; intros; refl } @[simp] lemma sup_to_finset {α β} [decidable_eq β] (s : finset α) (f : α → multiset β) : (s.sup f).to_finset = s.sup (λ x, (f x).to_finset) := comp_sup_eq_sup_comp multiset.to_finset to_finset_union rfl lemma _root_.list.foldr_sup_eq_sup_to_finset [decidable_eq α] (l : list α) : l.foldr (⊔) ⊥ = l.to_finset.sup id := begin rw [←coe_fold_r, ←multiset.fold_dedup_idem, sup_def, ←list.to_finset_coe, to_finset_val, multiset.map_id], refl end theorem subset_range_sup_succ (s : finset ℕ) : s ⊆ range (s.sup id).succ := λ n hn, mem_range.2 $ nat.lt_succ_of_le $ le_sup hn theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n := ⟨_, s.subset_range_sup_succ⟩ lemma sup_induction {p : α → Prop} (hb : p ⊥) (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊔ a₂)) (hs : ∀ b ∈ s, p (f b)) : p (s.sup f) := begin induction s using finset.cons_induction with c s hc ih, { exact hb, }, { rw sup_cons, apply hp, { exact hs c (mem_cons.2 (or.inl rfl)), }, { exact ih (λ b h, hs b (mem_cons.2 (or.inr h))), }, }, end lemma sup_le_of_le_directed {α : Type*} [semilattice_sup α] [order_bot α] (s : set α) (hs : s.nonempty) (hdir : directed_on (≤) s) (t : finset α) : (∀ x ∈ t, ∃ y ∈ s, x ≤ y) → ∃ x, x ∈ s ∧ t.sup id ≤ x := begin classical, apply finset.induction_on t, { simpa only [forall_prop_of_true, and_true, forall_prop_of_false, bot_le, not_false_iff, sup_empty, forall_true_iff, not_mem_empty], }, { intros a r har ih h, have incs : ↑r ⊆ ↑(insert a r), by { rw finset.coe_subset, apply finset.subset_insert, }, -- x ∈ s is above the sup of r obtain ⟨x, ⟨hxs, hsx_sup⟩⟩ := ih (λ x hx, h x $ incs hx), -- y ∈ s is above a obtain ⟨y, hys, hay⟩ := h a (finset.mem_insert_self a r), -- z ∈ s is above x and y obtain ⟨z, hzs, ⟨hxz, hyz⟩⟩ := hdir x hxs y hys, use [z, hzs], rw [sup_insert, id.def, sup_le_iff], exact ⟨le_trans hay hyz, le_trans hsx_sup hxz⟩, }, end -- If we acquire sublattices -- the hypotheses should be reformulated as `s : subsemilattice_sup_bot` lemma sup_mem (s : set α) (w₁ : ⊥ ∈ s) (w₂ : ∀ x y ∈ s, x ⊔ y ∈ s) {ι : Type*} (t : finset ι) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) : t.sup p ∈ s := @sup_induction _ _ _ _ _ _ (∈ s) w₁ w₂ h @[simp] lemma sup_eq_bot_iff (f : β → α) (S : finset β) : S.sup f = ⊥ ↔ ∀ s ∈ S, f s = ⊥ := begin classical, induction S using finset.induction with a S haS hi; simp [*], end end sup lemma sup_eq_supr [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = (⨆a∈s, f a) := le_antisymm (finset.sup_le $ assume a ha, le_supr_of_le a $ le_supr _ ha) (supr_le $ assume a, supr_le $ assume ha, le_sup ha) lemma sup_id_eq_Sup [complete_lattice α] (s : finset α) : s.sup id = Sup s := by simp [Sup_eq_supr, sup_eq_supr] lemma sup_id_set_eq_sUnion (s : finset (set α)) : s.sup id = ⋃₀(↑s) := sup_id_eq_Sup _ @[simp] lemma sup_set_eq_bUnion (s : finset α) (f : α → set β) : s.sup f = ⋃ x ∈ s, f x := sup_eq_supr _ _ lemma sup_eq_Sup_image [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = Sup (f '' s) := begin classical, rw [←finset.coe_image, ←sup_id_eq_Sup, sup_image, function.comp.left_id], end /-! ### inf -/ section inf -- TODO: define with just `[has_top α]` where some lemmas hold without requiring `[order_top α]` variables [semilattice_inf α] [order_top α] /-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/ def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f variables {s s₁ s₂ : finset β} {f g : β → α} lemma inf_def : s.inf f = (s.1.map f).inf := rfl @[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ := fold_empty @[simp] lemma inf_cons {b : β} (h : b ∉ s) : (cons b s h).inf f = f b ⊓ s.inf f := @sup_cons αᵒᵈ _ _ _ _ _ _ h @[simp] lemma inf_insert [decidable_eq β] {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f := fold_insert_idem lemma inf_image [decidable_eq β] (s : finset γ) (f : γ → β) (g : β → α) : (s.image f).inf g = s.inf (g ∘ f) := fold_image_idem @[simp] lemma inf_map (s : finset γ) (f : γ ↪ β) (g : β → α) : (s.map f).inf g = s.inf (g ∘ f) := fold_map @[simp] lemma inf_singleton {b : β} : ({b} : finset β).inf f = f b := inf_singleton lemma inf_union [decidable_eq β] : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f := @sup_union αᵒᵈ _ _ _ _ _ _ _ lemma inf_inf : s.inf (f ⊓ g) = s.inf f ⊓ s.inf g := @sup_sup αᵒᵈ _ _ _ _ _ _ theorem inf_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.inf f = s₂.inf g := by subst hs; exact finset.fold_congr hfg @[simp] lemma inf_bUnion [decidable_eq β] (s : finset γ) (t : γ → finset β) : (s.bUnion t).inf f = s.inf (λ x, (t x).inf f) := @sup_bUnion αᵒᵈ _ _ _ _ _ _ _ _ lemma inf_const {s : finset β} (h : s.nonempty) (c : α) : s.inf (λ _, c) = c := @sup_const αᵒᵈ _ _ _ _ h _ @[simp] lemma inf_top (s : finset β) : s.inf (λ _, ⊤) = (⊤ : α) := @sup_bot αᵒᵈ _ _ _ _ lemma le_inf_iff {a : α} : a ≤ s.inf f ↔ ∀ b ∈ s, a ≤ f b := @finset.sup_le_iff αᵒᵈ _ _ _ _ _ _ lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b := le_inf_iff.1 le_rfl _ hb lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f := le_inf_iff.2 lemma inf_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.inf f ≤ s.inf g := le_inf (λ b hb, le_trans (inf_le hb) (h b hb)) lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f := le_inf $ assume b hb, inf_le (h hb) lemma inf_attach (s : finset β) (f : β → α) : s.attach.inf (λ x, f x) = s.inf f := @sup_attach αᵒᵈ _ _ _ _ _ lemma inf_comm (s : finset β) (t : finset γ) (f : β → γ → α) : s.inf (λ b, t.inf (f b)) = t.inf (λ c, s.inf (λ b, f b c)) := @sup_comm αᵒᵈ _ _ _ _ _ _ _ lemma inf_product_left (s : finset β) (t : finset γ) (f : β × γ → α) : (s ×ˢ t).inf f = s.inf (λ i, t.inf $ λ i', f ⟨i, i'⟩) := @sup_product_left αᵒᵈ _ _ _ _ _ _ _ lemma inf_product_right (s : finset β) (t : finset γ) (f : β × γ → α) : (s ×ˢ t).inf f = t.inf (λ i', s.inf $ λ i, f ⟨i, i'⟩) := @sup_product_right αᵒᵈ _ _ _ _ _ _ _ @[simp] lemma inf_erase_top [decidable_eq α] (s : finset α) : (s.erase ⊤).inf id = s.inf id := @sup_erase_bot αᵒᵈ _ _ _ _ lemma sup_sdiff_left {α β : Type*} [boolean_algebra α] (s : finset β) (f : β → α) (a : α) : s.sup (λ b, a \ f b) = a \ s.inf f := begin refine finset.cons_induction_on s _ (λ b t _ h, _), { rw [sup_empty, inf_empty, sdiff_top] }, { rw [sup_cons, inf_cons, h, sdiff_inf] } end lemma inf_sdiff_left {α β : Type*} [boolean_algebra α] {s : finset β} (hs : s.nonempty) (f : β → α) (a : α) : s.inf (λ b, a \ f b) = a \ s.sup f := begin induction hs using finset.nonempty.cons_induction with b b t _ _ h, { rw [sup_singleton, inf_singleton] }, { rw [sup_cons, inf_cons, h, sdiff_sup] } end lemma inf_sdiff_right {α β : Type*} [boolean_algebra α] {s : finset β} (hs : s.nonempty) (f : β → α) (a : α) : s.inf (λ b, f b \ a) = s.inf f \ a := begin induction hs using finset.nonempty.cons_induction with b b t _ _ h, { rw [inf_singleton, inf_singleton] }, { rw [inf_cons, inf_cons, h, inf_sdiff] } end lemma comp_inf_eq_inf_comp [semilattice_inf γ] [order_top γ] {s : finset β} {f : β → α} (g : α → γ) (g_inf : ∀ x y, g (x ⊓ y) = g x ⊓ g y) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) := @comp_sup_eq_sup_comp αᵒᵈ _ γᵒᵈ _ _ _ _ _ _ _ g_inf top /-- Computing `inf` in a subtype (closed under `inf`) is the same as computing it in `α`. -/ lemma inf_coe {P : α → Prop} {Ptop : P ⊤} {Pinf : ∀{{x y}}, P x → P y → P (x ⊓ y)} (t : finset β) (f : β → {x : α // P x}) : (@inf _ _ (subtype.semilattice_inf Pinf) (subtype.order_top Ptop) t f : α) = t.inf (λ x, f x) := @sup_coe αᵒᵈ _ _ _ _ Ptop Pinf t f lemma _root_.list.foldr_inf_eq_inf_to_finset [decidable_eq α] (l : list α) : l.foldr (⊓) ⊤ = l.to_finset.inf id := begin rw [←coe_fold_r, ←multiset.fold_dedup_idem, inf_def, ←list.to_finset_coe, to_finset_val, multiset.map_id], refl end lemma inf_induction {p : α → Prop} (ht : p ⊤) (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊓ a₂)) (hs : ∀ b ∈ s, p (f b)) : p (s.inf f) := @sup_induction αᵒᵈ _ _ _ _ _ _ ht hp hs lemma inf_mem (s : set α) (w₁ : ⊤ ∈ s) (w₂ : ∀ x y ∈ s, x ⊓ y ∈ s) {ι : Type*} (t : finset ι) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) : t.inf p ∈ s := @inf_induction _ _ _ _ _ _ (∈ s) w₁ w₂ h @[simp] lemma inf_eq_top_iff (f : β → α) (S : finset β) : S.inf f = ⊤ ↔ ∀ s ∈ S, f s = ⊤ := @finset.sup_eq_bot_iff αᵒᵈ _ _ _ _ _ end inf @[simp] lemma to_dual_sup [semilattice_sup α] [order_bot α] (s : finset β) (f : β → α) : to_dual (s.sup f) = s.inf (to_dual ∘ f) := rfl @[simp] lemma to_dual_inf [semilattice_inf α] [order_top α] (s : finset β) (f : β → α) : to_dual (s.inf f) = s.sup (to_dual ∘ f) := rfl @[simp] lemma of_dual_sup [semilattice_inf α] [order_top α] (s : finset β) (f : β → αᵒᵈ) : of_dual (s.sup f) = s.inf (of_dual ∘ f) := rfl @[simp] lemma of_dual_inf [semilattice_sup α] [order_bot α] (s : finset β) (f : β → αᵒᵈ) : of_dual (s.inf f) = s.sup (of_dual ∘ f) := rfl section distrib_lattice variables [distrib_lattice α] section order_bot variables [order_bot α] {s : finset β} {f : β → α} {a : α} lemma sup_inf_distrib_left (s : finset ι) (f : ι → α) (a : α) : a ⊓ s.sup f = s.sup (λ i, a ⊓ f i) := begin induction s using finset.cons_induction with i s hi h, { simp_rw [finset.sup_empty, inf_bot_eq] }, { rw [sup_cons, sup_cons, inf_sup_left, h] } end lemma sup_inf_distrib_right (s : finset ι) (f : ι → α) (a : α) : s.sup f ⊓ a = s.sup (λ i, f i ⊓ a) := by { rw [_root_.inf_comm, s.sup_inf_distrib_left], simp_rw _root_.inf_comm } lemma disjoint_sup_right : disjoint a (s.sup f) ↔ ∀ i ∈ s, disjoint a (f i) := by simp only [disjoint_iff, sup_inf_distrib_left, sup_eq_bot_iff] lemma disjoint_sup_left : disjoint (s.sup f) a ↔ ∀ i ∈ s, disjoint (f i) a := by simp only [disjoint_iff, sup_inf_distrib_right, sup_eq_bot_iff] end order_bot section order_top variables [order_top α] lemma inf_sup_distrib_left (s : finset ι) (f : ι → α) (a : α) : a ⊔ s.inf f = s.inf (λ i, a ⊔ f i) := @sup_inf_distrib_left αᵒᵈ _ _ _ _ _ _ lemma inf_sup_distrib_right (s : finset ι) (f : ι → α) (a : α) : s.inf f ⊔ a = s.inf (λ i, f i ⊔ a) := @sup_inf_distrib_right αᵒᵈ _ _ _ _ _ _ end order_top end distrib_lattice section linear_order variables [linear_order α] section order_bot variables [order_bot α] {s : finset ι} {f : ι → α} {a : α} lemma comp_sup_eq_sup_comp_of_is_total [semilattice_sup β] [order_bot β] (g : α → β) (mono_g : monotone g) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) := comp_sup_eq_sup_comp g mono_g.map_sup bot @[simp] protected lemma le_sup_iff (ha : ⊥ < a) : a ≤ s.sup f ↔ ∃ b ∈ s, a ≤ f b := ⟨finset.cons_induction_on s (λ h, absurd h (not_le_of_lt ha)) (λ c t hc ih, by simpa using @or.rec _ _ (∃ b, (b = c ∨ b ∈ t) ∧ a ≤ f b) (λ h, ⟨c, or.inl rfl, h⟩) (λ h, let ⟨b, hb, hle⟩ := ih h in ⟨b, or.inr hb, hle⟩)), (λ ⟨b, hb, hle⟩, trans hle (le_sup hb))⟩ @[simp] protected lemma lt_sup_iff : a < s.sup f ↔ ∃ b ∈ s, a < f b := ⟨finset.cons_induction_on s (λ h, absurd h not_lt_bot) (λ c t hc ih, by simpa using @or.rec _ _ (∃ b, (b = c ∨ b ∈ t) ∧ a < f b) (λ h, ⟨c, or.inl rfl, h⟩) (λ h, let ⟨b, hb, hlt⟩ := ih h in ⟨b, or.inr hb, hlt⟩)), (λ ⟨b, hb, hlt⟩, lt_of_lt_of_le hlt (le_sup hb))⟩ @[simp] protected lemma sup_lt_iff (ha : ⊥ < a) : s.sup f < a ↔ ∀ b ∈ s, f b < a := ⟨(λ hs b hb, lt_of_le_of_lt (le_sup hb) hs), finset.cons_induction_on s (λ _, ha) (λ c t hc, by simpa only [sup_cons, sup_lt_iff, mem_cons, forall_eq_or_imp] using and.imp_right)⟩ end order_bot section order_top variables [order_top α] {s : finset ι} {f : ι → α} {a : α} lemma comp_inf_eq_inf_comp_of_is_total [semilattice_inf β] [order_top β] (g : α → β) (mono_g : monotone g) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) := comp_inf_eq_inf_comp g mono_g.map_inf top @[simp] protected lemma inf_le_iff (ha : a < ⊤) : s.inf f ≤ a ↔ ∃ b ∈ s, f b ≤ a := @finset.le_sup_iff αᵒᵈ _ _ _ _ _ _ ha @[simp] protected lemma inf_lt_iff : s.inf f < a ↔ ∃ b ∈ s, f b < a := @finset.lt_sup_iff αᵒᵈ _ _ _ _ _ _ @[simp] protected lemma lt_inf_iff (ha : a < ⊤) : a < s.inf f ↔ ∀ b ∈ s, a < f b := @finset.sup_lt_iff αᵒᵈ _ _ _ _ _ _ ha end order_top end linear_order lemma inf_eq_infi [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = ⨅ a ∈ s, f a := @sup_eq_supr _ βᵒᵈ _ _ _ lemma inf_id_eq_Inf [complete_lattice α] (s : finset α) : s.inf id = Inf s := @sup_id_eq_Sup αᵒᵈ _ _ lemma inf_id_set_eq_sInter (s : finset (set α)) : s.inf id = ⋂₀(↑s) := inf_id_eq_Inf _ @[simp] lemma inf_set_eq_bInter (s : finset α) (f : α → set β) : s.inf f = ⋂ x ∈ s, f x := inf_eq_infi _ _ lemma inf_eq_Inf_image [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = Inf (f '' s) := @sup_eq_Sup_image _ βᵒᵈ _ _ _ section sup' variables [semilattice_sup α] lemma sup_of_mem {s : finset β} (f : β → α) {b : β} (h : b ∈ s) : ∃ (a : α), s.sup (coe ∘ f : β → with_bot α) = ↑a := Exists.imp (λ a, Exists.fst) (@le_sup (with_bot α) _ _ _ _ _ _ h (f b) rfl) /-- Given nonempty finset `s` then `s.sup' H f` is the supremum of its image under `f` in (possibly unbounded) join-semilattice `α`, where `H` is a proof of nonemptiness. If `α` has a bottom element you may instead use `finset.sup` which does not require `s` nonempty. -/ def sup' (s : finset β) (H : s.nonempty) (f : β → α) : α := with_bot.unbot (s.sup (coe ∘ f)) (by simpa using H) variables {s : finset β} (H : s.nonempty) (f : β → α) @[simp] lemma coe_sup' : ((s.sup' H f : α) : with_bot α) = s.sup (coe ∘ f) := by rw [sup', with_bot.coe_unbot] @[simp] lemma sup'_cons {b : β} {hb : b ∉ s} {h : (cons b s hb).nonempty} : (cons b s hb).sup' h f = f b ⊔ s.sup' H f := by { rw ←with_bot.coe_eq_coe, simp only [coe_sup', sup_cons, with_bot.coe_sup], } @[simp] lemma sup'_insert [decidable_eq β] {b : β} {h : (insert b s).nonempty} : (insert b s).sup' h f = f b ⊔ s.sup' H f := by { rw ←with_bot.coe_eq_coe, simp only [coe_sup', sup_insert, with_bot.coe_sup], } @[simp] lemma sup'_singleton {b : β} {h : ({b} : finset β).nonempty} : ({b} : finset β).sup' h f = f b := rfl lemma sup'_le {a : α} (hs : ∀ b ∈ s, f b ≤ a) : s.sup' H f ≤ a := by { rw [←with_bot.coe_le_coe, coe_sup'], exact sup_le (λ b h, with_bot.coe_le_coe.2 $ hs b h), } lemma le_sup' {b : β} (h : b ∈ s) : f b ≤ s.sup' ⟨b, h⟩ f := by { rw [←with_bot.coe_le_coe, coe_sup'], exact le_sup h, } @[simp] lemma sup'_const (a : α) : s.sup' H (λ b, a) = a := begin apply le_antisymm, { apply sup'_le, intros, exact le_rfl, }, { apply le_sup' (λ b, a) H.some_spec, } end @[simp] lemma sup'_le_iff {a : α} : s.sup' H f ≤ a ↔ ∀ b ∈ s, f b ≤ a := iff.intro (λ h b hb, trans (le_sup' f hb) h) (sup'_le H f) lemma sup'_bUnion [decidable_eq β] {s : finset γ} (Hs : s.nonempty) {t : γ → finset β} (Ht : ∀ b, (t b).nonempty) : (s.bUnion t).sup' (Hs.bUnion (λ b _, Ht b)) f = s.sup' Hs (λ b, (t b).sup' (Ht b) f) := eq_of_forall_ge_iff $ λ c, by simp [@forall_swap _ β] lemma comp_sup'_eq_sup'_comp [semilattice_sup γ] {s : finset β} (H : s.nonempty) {f : β → α} (g : α → γ) (g_sup : ∀ x y, g (x ⊔ y) = g x ⊔ g y) : g (s.sup' H f) = s.sup' H (g ∘ f) := begin rw [←with_bot.coe_eq_coe, coe_sup'], let g' := with_bot.map g, show g' ↑(s.sup' H f) = s.sup (λ a, g' ↑(f a)), rw coe_sup', refine comp_sup_eq_sup_comp g' _ rfl, intros f₁ f₂, induction f₁ using with_bot.rec_bot_coe, { rw [bot_sup_eq], exact bot_sup_eq.symm, }, { induction f₂ using with_bot.rec_bot_coe, { refl }, { exact congr_arg coe (g_sup f₁ f₂) } } end lemma sup'_induction {p : α → Prop} (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊔ a₂)) (hs : ∀ b ∈ s, p (f b)) : p (s.sup' H f) := begin show @with_bot.rec_bot_coe α (λ _, Prop) true p ↑(s.sup' H f), rw coe_sup', refine sup_induction trivial _ hs, rintro (_|a₁) h₁ a₂ h₂, { rw [with_bot.none_eq_bot, bot_sup_eq], exact h₂ }, cases a₂, exacts [h₁, hp a₁ h₁ a₂ h₂] end lemma sup'_mem (s : set α) (w : ∀ x y ∈ s, x ⊔ y ∈ s) {ι : Type*} (t : finset ι) (H : t.nonempty) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) : t.sup' H p ∈ s := sup'_induction H p w h @[congr] lemma sup'_congr {t : finset β} {f g : β → α} (h₁ : s = t) (h₂ : ∀ x ∈ s, f x = g x) : s.sup' H f = t.sup' (h₁ ▸ H) g := begin subst s, refine eq_of_forall_ge_iff (λ c, _), simp only [sup'_le_iff, h₂] { contextual := tt } end @[simp] lemma sup'_map {s : finset γ} {f : γ ↪ β} (g : β → α) (hs : (s.map f).nonempty) (hs': s.nonempty := finset.map_nonempty.mp hs) : (s.map f).sup' hs g = s.sup' hs' (g ∘ f) := by rw [←with_bot.coe_eq_coe, coe_sup', sup_map, coe_sup'] end sup' section inf' variables [semilattice_inf α] lemma inf_of_mem {s : finset β} (f : β → α) {b : β} (h : b ∈ s) : ∃ (a : α), s.inf (coe ∘ f : β → with_top α) = ↑a := @sup_of_mem αᵒᵈ _ _ _ f _ h /-- Given nonempty finset `s` then `s.inf' H f` is the infimum of its image under `f` in (possibly unbounded) meet-semilattice `α`, where `H` is a proof of nonemptiness. If `α` has a top element you may instead use `finset.inf` which does not require `s` nonempty. -/ def inf' (s : finset β) (H : s.nonempty) (f : β → α) : α := with_top.untop (s.inf (coe ∘ f)) (by simpa using H) variables {s : finset β} (H : s.nonempty) (f : β → α) {a : α} {b : β} @[simp] lemma coe_inf' : ((s.inf' H f : α) : with_top α) = s.inf (coe ∘ f) := @coe_sup' αᵒᵈ _ _ _ H f @[simp] lemma inf'_cons {b : β} {hb : b ∉ s} {h : (cons b s hb).nonempty} : (cons b s hb).inf' h f = f b ⊓ s.inf' H f := @sup'_cons αᵒᵈ _ _ _ H f _ _ h @[simp] lemma inf'_insert [decidable_eq β] {b : β} {h : (insert b s).nonempty} : (insert b s).inf' h f = f b ⊓ s.inf' H f := @sup'_insert αᵒᵈ _ _ _ H f _ _ h @[simp] lemma inf'_singleton {b : β} {h : ({b} : finset β).nonempty} : ({b} : finset β).inf' h f = f b := rfl lemma le_inf' (hs : ∀ b ∈ s, a ≤ f b) : a ≤ s.inf' H f := @sup'_le αᵒᵈ _ _ _ H f _ hs lemma inf'_le (h : b ∈ s) : s.inf' ⟨b, h⟩ f ≤ f b := @le_sup' αᵒᵈ _ _ _ f _ h @[simp] lemma inf'_const (a : α) : s.inf' H (λ b, a) = a := @sup'_const αᵒᵈ _ _ _ H _ @[simp] lemma le_inf'_iff : a ≤ s.inf' H f ↔ ∀ b ∈ s, a ≤ f b := @sup'_le_iff αᵒᵈ _ _ _ H f _ lemma inf'_bUnion [decidable_eq β] {s : finset γ} (Hs : s.nonempty) {t : γ → finset β} (Ht : ∀ b, (t b).nonempty) : (s.bUnion t).inf' (Hs.bUnion (λ b _, Ht b)) f = s.inf' Hs (λ b, (t b).inf' (Ht b) f) := @sup'_bUnion αᵒᵈ _ _ _ _ _ _ Hs _ Ht lemma comp_inf'_eq_inf'_comp [semilattice_inf γ] {s : finset β} (H : s.nonempty) {f : β → α} (g : α → γ) (g_inf : ∀ x y, g (x ⊓ y) = g x ⊓ g y) : g (s.inf' H f) = s.inf' H (g ∘ f) := @comp_sup'_eq_sup'_comp αᵒᵈ _ γᵒᵈ _ _ _ H f g g_inf lemma inf'_induction {p : α → Prop} (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊓ a₂)) (hs : ∀ b ∈ s, p (f b)) : p (s.inf' H f) := @sup'_induction αᵒᵈ _ _ _ H f _ hp hs lemma inf'_mem (s : set α) (w : ∀ x y ∈ s, x ⊓ y ∈ s) {ι : Type*} (t : finset ι) (H : t.nonempty) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) : t.inf' H p ∈ s := inf'_induction H p w h @[congr] lemma inf'_congr {t : finset β} {f g : β → α} (h₁ : s = t) (h₂ : ∀ x ∈ s, f x = g x) : s.inf' H f = t.inf' (h₁ ▸ H) g := @sup'_congr αᵒᵈ _ _ _ H _ _ _ h₁ h₂ @[simp] lemma inf'_map {s : finset γ} {f : γ ↪ β} (g : β → α) (hs : (s.map f).nonempty) (hs': s.nonempty := finset.map_nonempty.mp hs) : (s.map f).inf' hs g = s.inf' hs' (g ∘ f) := @sup'_map αᵒᵈ _ _ _ _ _ _ hs hs' end inf' section sup variables [semilattice_sup α] [order_bot α] lemma sup'_eq_sup {s : finset β} (H : s.nonempty) (f : β → α) : s.sup' H f = s.sup f := le_antisymm (sup'_le H f (λ b, le_sup)) (sup_le (λ b, le_sup' f)) lemma sup_closed_of_sup_closed {s : set α} (t : finset α) (htne : t.nonempty) (h_subset : ↑t ⊆ s) (h : ∀ a b ∈ s, a ⊔ b ∈ s) : t.sup id ∈ s := sup'_eq_sup htne id ▸ sup'_induction _ _ h h_subset lemma coe_sup_of_nonempty {s : finset β} (h : s.nonempty) (f : β → α) : (↑(s.sup f) : with_bot α) = s.sup (coe ∘ f) := by simp only [←sup'_eq_sup h, coe_sup' h] end sup section inf variables [semilattice_inf α] [order_top α] lemma inf'_eq_inf {s : finset β} (H : s.nonempty) (f : β → α) : s.inf' H f = s.inf f := @sup'_eq_sup αᵒᵈ _ _ _ _ H f lemma inf_closed_of_inf_closed {s : set α} (t : finset α) (htne : t.nonempty) (h_subset : ↑t ⊆ s) (h : ∀ a b ∈ s, a ⊓ b ∈ s) : t.inf id ∈ s := @sup_closed_of_sup_closed αᵒᵈ _ _ _ t htne h_subset h lemma coe_inf_of_nonempty {s : finset β} (h : s.nonempty) (f : β → α): (↑(s.inf f) : with_top α) = s.inf (λ i, f i) := @coe_sup_of_nonempty αᵒᵈ _ _ _ _ h f end inf section sup variables {C : β → Type*} [Π (b : β), semilattice_sup (C b)] [Π (b : β), order_bot (C b)] @[simp] protected lemma sup_apply (s : finset α) (f : α → Π (b : β), C b) (b : β) : s.sup f b = s.sup (λ a, f a b) := comp_sup_eq_sup_comp (λ x : Π b : β, C b, x b) (λ i j, rfl) rfl end sup section inf variables {C : β → Type*} [Π (b : β), semilattice_inf (C b)] [Π (b : β), order_top (C b)] @[simp] protected lemma inf_apply (s : finset α) (f : α → Π (b : β), C b) (b : β) : s.inf f b = s.inf (λ a, f a b) := @finset.sup_apply _ _ (λ b, (C b)ᵒᵈ) _ _ s f b end inf section sup' variables {C : β → Type*} [Π (b : β), semilattice_sup (C b)] @[simp] protected lemma sup'_apply {s : finset α} (H : s.nonempty) (f : α → Π (b : β), C b) (b : β) : s.sup' H f b = s.sup' H (λ a, f a b) := comp_sup'_eq_sup'_comp H (λ x : Π b : β, C b, x b) (λ i j, rfl) end sup' section inf' variables {C : β → Type*} [Π (b : β), semilattice_inf (C b)] @[simp] protected lemma inf'_apply {s : finset α} (H : s.nonempty) (f : α → Π (b : β), C b) (b : β) : s.inf' H f b = s.inf' H (λ a, f a b) := @finset.sup'_apply _ _ (λ b, (C b)ᵒᵈ) _ _ H f b end inf' @[simp] lemma to_dual_sup' [semilattice_sup α] {s : finset ι} (hs : s.nonempty) (f : ι → α) : to_dual (s.sup' hs f) = s.inf' hs (to_dual ∘ f) := rfl @[simp] lemma to_dual_inf' [semilattice_inf α] {s : finset ι} (hs : s.nonempty) (f : ι → α) : to_dual (s.inf' hs f) = s.sup' hs (to_dual ∘ f) := rfl @[simp] lemma of_dual_sup' [semilattice_inf α] {s : finset ι} (hs : s.nonempty) (f : ι → αᵒᵈ) : of_dual (s.sup' hs f) = s.inf' hs (of_dual ∘ f) := rfl @[simp] lemma of_dual_inf' [semilattice_sup α] {s : finset ι} (hs : s.nonempty) (f : ι → αᵒᵈ) : of_dual (s.inf' hs f) = s.sup' hs (of_dual ∘ f) := rfl section linear_order variables [linear_order α] {s : finset ι} (H : s.nonempty) {f : ι → α} {a : α} @[simp] lemma le_sup'_iff : a ≤ s.sup' H f ↔ ∃ b ∈ s, a ≤ f b := begin rw [←with_bot.coe_le_coe, coe_sup', finset.le_sup_iff (with_bot.bot_lt_coe a)], exact bex_congr (λ b hb, with_bot.coe_le_coe), end @[simp] lemma lt_sup'_iff : a < s.sup' H f ↔ ∃ b ∈ s, a < f b := begin rw [←with_bot.coe_lt_coe, coe_sup', finset.lt_sup_iff], exact bex_congr (λ b hb, with_bot.coe_lt_coe), end @[simp] lemma sup'_lt_iff : s.sup' H f < a ↔ ∀ i ∈ s, f i < a := begin rw [←with_bot.coe_lt_coe, coe_sup', finset.sup_lt_iff (with_bot.bot_lt_coe a)], exact ball_congr (λ b hb, with_bot.coe_lt_coe), end @[simp] lemma inf'_le_iff : s.inf' H f ≤ a ↔ ∃ i ∈ s, f i ≤ a := @le_sup'_iff αᵒᵈ _ _ _ H f _ @[simp] lemma inf'_lt_iff : s.inf' H f < a ↔ ∃ i ∈ s, f i < a := @lt_sup'_iff αᵒᵈ _ _ _ H f _ @[simp] lemma lt_inf'_iff : a < s.inf' H f ↔ ∀ i ∈ s, a < f i := @sup'_lt_iff αᵒᵈ _ _ _ H f _ lemma exists_mem_eq_sup' (f : ι → α) : ∃ i, i ∈ s ∧ s.sup' H f = f i := begin refine H.cons_induction (λ c, _) (λ c s hc hs ih, _), { exact ⟨c, mem_singleton_self c, rfl⟩, }, { rcases ih with ⟨b, hb, h'⟩, rw [sup'_cons hs, h'], cases total_of (≤) (f b) (f c) with h h, { exact ⟨c, mem_cons.2 (or.inl rfl), sup_eq_left.2 h⟩, }, { exact ⟨b, mem_cons.2 (or.inr hb), sup_eq_right.2 h⟩, }, }, end lemma exists_mem_eq_inf' (f : ι → α) : ∃ i, i ∈ s ∧ s.inf' H f = f i := @exists_mem_eq_sup' αᵒᵈ _ _ _ H f lemma exists_mem_eq_sup [order_bot α] (s : finset ι) (h : s.nonempty) (f : ι → α) : ∃ i, i ∈ s ∧ s.sup f = f i := sup'_eq_sup h f ▸ exists_mem_eq_sup' h f lemma exists_mem_eq_inf [order_top α] (s : finset ι) (h : s.nonempty) (f : ι → α) : ∃ i, i ∈ s ∧ s.inf f = f i := @exists_mem_eq_sup αᵒᵈ _ _ _ _ h f end linear_order /-! ### max and min of finite sets -/ section max_min variables [linear_order α] /-- Let `s` be a finset in a linear order. Then `s.max` is the maximum of `s` if `s` is not empty, and `⊥` otherwise. It belongs to `with_bot α`. If you want to get an element of `α`, see `s.max'`. -/ protected def max (s : finset α) : with_bot α := sup s coe lemma max_eq_sup_coe {s : finset α} : s.max = s.sup coe := rfl theorem max_eq_sup_with_bot (s : finset α) : s.max = sup s coe := rfl @[simp] theorem max_empty : (∅ : finset α).max = ⊥ := rfl @[simp] theorem max_insert {a : α} {s : finset α} : (insert a s).max = max a s.max := fold_insert_idem @[simp] theorem max_singleton {a : α} : finset.max {a} = (a : with_bot α) := by { rw [← insert_emptyc_eq], exact max_insert } theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ (b : α), s.max = b := (@le_sup (with_bot α) _ _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem max_of_nonempty {s : finset α} (h : s.nonempty) : ∃ (a : α), s.max = a := let ⟨a, ha⟩ := h in max_of_mem ha theorem max_eq_bot {s : finset α} : s.max = ⊥ ↔ s = ∅ := ⟨λ h, s.eq_empty_or_nonempty.elim id (λ H, let ⟨a, ha⟩ := max_of_nonempty H in by rw h at ha; cases ha), λ h, h.symm ▸ max_empty⟩ theorem mem_of_max {s : finset α} : ∀ {a : α}, s.max = a → a ∈ s := finset.induction_on s (λ _ H, by cases H) (λ b s _ (ih : ∀ {a : α}, s.max = a → a ∈ s) a (h : (insert b s).max = a), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases max_choice ↑b s.max with q q; rw [max_insert, q] at h, { cases h, cases p rfl }, { exact mem_insert_of_mem (ih h) } } end) lemma coe_le_max_of_mem {a : α} {s : finset α} (as : a ∈ s) : ↑a ≤ s.max := le_sup as theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : s.max = b) : a ≤ b := with_bot.coe_le_coe.mp $ (coe_le_max_of_mem h₁).trans h₂.le lemma max_mono {s t : finset α} (st : s ⊆ t) : s.max ≤ t.max := sup_mono st lemma max_le {M : with_bot α} {s : finset α} (st : ∀ a : α, a ∈ s → (a : with_bot α) ≤ M) : s.max ≤ M := sup_le st /-- Let `s` be a finset in a linear order. Then `s.min` is the minimum of `s` if `s` is not empty, and `⊤` otherwise. It belongs to `with_top α`. If you want to get an element of `α`, see `s.min'`. -/ protected def min (s : finset α) : with_top α := inf s coe theorem min_eq_inf_with_top (s : finset α) : s.min = inf s coe := rfl @[simp] theorem min_empty : (∅ : finset α).min = ⊤ := rfl @[simp] theorem min_insert {a : α} {s : finset α} : (insert a s).min = min ↑a s.min := fold_insert_idem @[simp] theorem min_singleton {a : α} : finset.min {a} = (a : with_top α) := by { rw ← insert_emptyc_eq, exact min_insert } theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b : α, s.min = b := (@inf_le (with_top α) _ _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem min_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a : α, s.min = a := let ⟨a, ha⟩ := h in min_of_mem ha theorem min_eq_top {s : finset α} : s.min = ⊤ ↔ s = ∅ := ⟨λ h, s.eq_empty_or_nonempty.elim id (λ H, let ⟨a, ha⟩ := min_of_nonempty H in by rw h at ha; cases ha), λ h, h.symm ▸ min_empty⟩ theorem mem_of_min {s : finset α} : ∀ {a : α}, s.min = a → a ∈ s := @mem_of_max αᵒᵈ _ s lemma min_le_coe_of_mem {a : α} {s : finset α} (as : a ∈ s) : s.min ≤ a := inf_le as theorem min_le_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : s.min = a) : a ≤ b := with_top.coe_le_coe.mp $ h₂.ge.trans (min_le_coe_of_mem h₁) lemma min_mono {s t : finset α} (st : s ⊆ t) : t.min ≤ s.min := inf_mono st lemma le_min {m : with_top α} {s : finset α} (st : ∀ a : α, a ∈ s → m ≤ a) : m ≤ s.min := le_inf st /-- Given a nonempty finset `s` in a linear order `α `, then `s.min' h` is its minimum, as an element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.min`, taking values in `with_top α`. -/ def min' (s : finset α) (H : s.nonempty) : α := inf' s H id /-- Given a nonempty finset `s` in a linear order `α `, then `s.max' h` is its maximum, as an element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.max`, taking values in `with_bot α`. -/ def max' (s : finset α) (H : s.nonempty) : α := sup' s H id variables (s : finset α) (H : s.nonempty) {x : α} theorem min'_mem : s.min' H ∈ s := mem_of_min $ by simp [min', finset.min] theorem min'_le (x) (H2 : x ∈ s) : s.min' ⟨x, H2⟩ ≤ x := min_le_of_mem H2 (with_top.coe_untop _ _).symm theorem le_min' (x) (H2 : ∀ y ∈ s, x ≤ y) : x ≤ s.min' H := H2 _ $ min'_mem _ _ theorem is_least_min' : is_least ↑s (s.min' H) := ⟨min'_mem _ _, min'_le _⟩ @[simp] lemma le_min'_iff {x} : x ≤ s.min' H ↔ ∀ y ∈ s, x ≤ y := le_is_glb_iff (is_least_min' s H).is_glb /-- `{a}.min' _` is `a`. -/ @[simp] lemma min'_singleton (a : α) : ({a} : finset α).min' (singleton_nonempty _) = a := by simp [min'] theorem max'_mem : s.max' H ∈ s := mem_of_max $ by simp [max', finset.max] theorem le_max' (x) (H2 : x ∈ s) : x ≤ s.max' ⟨x, H2⟩ := le_max_of_mem H2 (with_bot.coe_unbot _ _).symm theorem max'_le (x) (H2 : ∀ y ∈ s, y ≤ x) : s.max' H ≤ x := H2 _ $ max'_mem _ _ theorem is_greatest_max' : is_greatest ↑s (s.max' H) := ⟨max'_mem _ _, le_max' _⟩ @[simp] lemma max'_le_iff {x} : s.max' H ≤ x ↔ ∀ y ∈ s, y ≤ x := is_lub_le_iff (is_greatest_max' s H).is_lub @[simp] lemma max'_lt_iff {x} : s.max' H < x ↔ ∀ y ∈ s, y < x := ⟨λ Hlt y hy, (s.le_max' y hy).trans_lt Hlt, λ H, H _ $ s.max'_mem _⟩ @[simp] lemma lt_min'_iff : x < s.min' H ↔ ∀ y ∈ s, x < y := @max'_lt_iff αᵒᵈ _ _ H _ lemma max'_eq_sup' : s.max' H = s.sup' H id := eq_of_forall_ge_iff $ λ a, (max'_le_iff _ _).trans (sup'_le_iff _ _).symm lemma min'_eq_inf' : s.min' H = s.inf' H id := @max'_eq_sup' αᵒᵈ _ s H /-- `{a}.max' _` is `a`. -/ @[simp] lemma max'_singleton (a : α) : ({a} : finset α).max' (singleton_nonempty _) = a := by simp [max'] theorem min'_lt_max' {i j} (H1 : i ∈ s) (H2 : j ∈ s) (H3 : i ≠ j) : s.min' ⟨i, H1⟩ < s.max' ⟨i, H1⟩ := is_glb_lt_is_lub_of_ne (s.is_least_min' _).is_glb (s.is_greatest_max' _).is_lub H1 H2 H3 /-- If there's more than 1 element, the min' is less than the max'. An alternate version of `min'_lt_max'` which is sometimes more convenient. -/ lemma min'_lt_max'_of_card (h₂ : 1 < card s) : s.min' (finset.card_pos.mp $ lt_trans zero_lt_one h₂) < s.max' (finset.card_pos.mp $ lt_trans zero_lt_one h₂) := begin rcases one_lt_card.1 h₂ with ⟨a, ha, b, hb, hab⟩, exact s.min'_lt_max' ha hb hab end lemma map_of_dual_min (s : finset αᵒᵈ) : s.min.map of_dual = (s.image of_dual).max := by { rw [max_eq_sup_with_bot, sup_image], exact congr_fun option.map_id _ } lemma map_of_dual_max (s : finset αᵒᵈ) : s.max.map of_dual = (s.image of_dual).min := by { rw [min_eq_inf_with_top, inf_image], exact congr_fun option.map_id _ } lemma map_to_dual_min (s : finset α) : s.min.map to_dual = (s.image to_dual).max := by { rw [max_eq_sup_with_bot, sup_image], exact congr_fun option.map_id _ } lemma map_to_dual_max (s : finset α) : s.max.map to_dual = (s.image to_dual).min := by { rw [min_eq_inf_with_top, inf_image], exact congr_fun option.map_id _ } lemma of_dual_min' {s : finset αᵒᵈ} (hs : s.nonempty) : of_dual (min' s hs) = max' (s.image of_dual) (hs.image _) := by { convert rfl, exact image_id } lemma of_dual_max' {s : finset αᵒᵈ} (hs : s.nonempty) : of_dual (max' s hs) = min' (s.image of_dual) (hs.image _) := by { convert rfl, exact image_id } lemma to_dual_min' {s : finset α} (hs : s.nonempty) : to_dual (min' s hs) = max' (s.image to_dual) (hs.image _) := by { convert rfl, exact image_id } lemma to_dual_max' {s : finset α} (hs : s.nonempty) : to_dual (max' s hs) = min' (s.image to_dual) (hs.image _) := by { convert rfl, exact image_id } lemma max'_subset {s t : finset α} (H : s.nonempty) (hst : s ⊆ t) : s.max' H ≤ t.max' (H.mono hst) := le_max' _ _ (hst (s.max'_mem H)) lemma min'_subset {s t : finset α} (H : s.nonempty) (hst : s ⊆ t) : t.min' (H.mono hst) ≤ s.min' H := min'_le _ _ (hst (s.min'_mem H)) lemma max'_insert (a : α) (s : finset α) (H : s.nonempty) : (insert a s).max' (s.insert_nonempty a) = max (s.max' H) a := (is_greatest_max' _ _).unique $ by { rw [coe_insert, max_comm], exact (is_greatest_max' _ _).insert _ } lemma min'_insert (a : α) (s : finset α) (H : s.nonempty) : (insert a s).min' (s.insert_nonempty a) = min (s.min' H) a := (is_least_min' _ _).unique $ by { rw [coe_insert, min_comm], exact (is_least_min' _ _).insert _ } lemma lt_max'_of_mem_erase_max' [decidable_eq α] {a : α} (ha : a ∈ s.erase (s.max' H)) : a < s.max' H := lt_of_le_of_ne (le_max' _ _ (mem_of_mem_erase ha)) $ ne_of_mem_of_not_mem ha $ not_mem_erase _ _ lemma min'_lt_of_mem_erase_min' [decidable_eq α] {a : α} (ha : a ∈ s.erase (s.min' H)) : s.min' H < a := @lt_max'_of_mem_erase_max' αᵒᵈ _ s H _ a ha @[simp] lemma max'_image [linear_order β] {f : α → β} (hf : monotone f) (s : finset α) (h : (s.image f).nonempty) : (s.image f).max' h = f (s.max' ((nonempty.image_iff f).mp h)) := begin refine le_antisymm (max'_le _ _ _ (λ y hy, _)) (le_max' _ _ (mem_image.mpr ⟨_, max'_mem _ _, rfl⟩)), obtain ⟨x, hx, rfl⟩ := mem_image.mp hy, exact hf (le_max' _ _ hx) end @[simp] lemma min'_image [linear_order β] {f : α → β} (hf : monotone f) (s : finset α) (h : (s.image f).nonempty) : (s.image f).min' h = f (s.min' ((nonempty.image_iff f).mp h)) := begin convert @max'_image αᵒᵈ βᵒᵈ _ _ (λ a : αᵒᵈ, to_dual (f (of_dual a))) (by simpa) _ _; convert h, rw nonempty.image_iff, end lemma coe_max' {s : finset α} (hs : s.nonempty) : ↑(s.max' hs) = s.max := coe_sup' hs id lemma coe_min' {s : finset α} (hs : s.nonempty) : ↑(s.min' hs) = s.min := coe_inf' hs id lemma max'_erase_ne_self {s : finset α} (s0 : (s.erase x).nonempty) : (s.erase x).max' s0 ≠ x := ne_of_mem_erase (max'_mem _ s0) lemma min'_erase_ne_self {s : finset α} (s0 : (s.erase x).nonempty) : (s.erase x).min' s0 ≠ x := ne_of_mem_erase (min'_mem _ s0) lemma max_erase_ne_self {s : finset α} : (s.erase x).max ≠ x := begin by_cases s0 : (s.erase x).nonempty, { refine ne_of_eq_of_ne (coe_max' s0).symm _, exact with_bot.coe_eq_coe.not.mpr (max'_erase_ne_self _) }, { rw [not_nonempty_iff_eq_empty.mp s0, max_empty], exact with_bot.bot_ne_coe } end lemma min_erase_ne_self {s : finset α} : (s.erase x).min ≠ x := by convert @max_erase_ne_self αᵒᵈ _ _ _ /-- Induction principle for `finset`s in a linearly ordered type: a predicate is true on all `s : finset α` provided that: * it is true on the empty `finset`, * for every `s : finset α` and an element `a` strictly greater than all elements of `s`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_max [decidable_eq α] {p : finset α → Prop} (s : finset α) (h0 : p ∅) (step : ∀ a s, (∀ x ∈ s, x < a) → p s → p (insert a s)) : p s := begin induction s using finset.strong_induction_on with s ihs, rcases s.eq_empty_or_nonempty with rfl|hne, { exact h0 }, { have H : s.max' hne ∈ s, from max'_mem s hne, rw ← insert_erase H, exact step _ _ (λ x, s.lt_max'_of_mem_erase_max' hne) (ihs _ $ erase_ssubset H) } end /-- Induction principle for `finset`s in a linearly ordered type: a predicate is true on all `s : finset α` provided that: * it is true on the empty `finset`, * for every `s : finset α` and an element `a` strictly less than all elements of `s`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_min [decidable_eq α] {p : finset α → Prop} (s : finset α) (h0 : p ∅) (step : ∀ a s, (∀ x ∈ s, a < x) → p s → p (insert a s)) : p s := @induction_on_max αᵒᵈ _ _ _ s h0 step end max_min section max_min_induction_value variables [linear_order α] [linear_order β] /-- Induction principle for `finset`s in any type from which a given function `f` maps to a linearly ordered type : a predicate is true on all `s : finset α` provided that: * it is true on the empty `finset`, * for every `s : finset α` and an element `a` such that for elements of `s` denoted by `x` we have `f x ≤ f a`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_max_value [decidable_eq ι] (f : ι → α) {p : finset ι → Prop} (s : finset ι) (h0 : p ∅) (step : ∀ a s, a ∉ s → (∀ x ∈ s, f x ≤ f a) → p s → p (insert a s)) : p s := begin induction s using finset.strong_induction_on with s ihs, rcases (s.image f).eq_empty_or_nonempty with hne|hne, { simp only [image_eq_empty] at hne, simp only [hne, h0] }, { have H : (s.image f).max' hne ∈ (s.image f), from max'_mem (s.image f) hne, simp only [mem_image, exists_prop] at H, rcases H with ⟨a, has, hfa⟩, rw ← insert_erase has, refine step _ _ (not_mem_erase a s) (λ x hx, _) (ihs _ $ erase_ssubset has), rw hfa, exact le_max' _ _ (mem_image_of_mem _ $ mem_of_mem_erase hx) } end /-- Induction principle for `finset`s in any type from which a given function `f` maps to a linearly ordered type : a predicate is true on all `s : finset α` provided that: * it is true on the empty `finset`, * for every `s : finset α` and an element `a` such that for elements of `s` denoted by `x` we have `f a ≤ f x`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_min_value [decidable_eq ι] (f : ι → α) {p : finset ι → Prop} (s : finset ι) (h0 : p ∅) (step : ∀ a s, a ∉ s → (∀ x ∈ s, f a ≤ f x) → p s → p (insert a s)) : p s := @induction_on_max_value αᵒᵈ ι _ _ _ _ s h0 step end max_min_induction_value section exists_max_min variables [linear_order α] lemma exists_max_image (s : finset β) (f : β → α) (h : s.nonempty) : ∃ x ∈ s, ∀ x' ∈ s, f x' ≤ f x := begin cases max_of_nonempty (h.image f) with y hy, rcases mem_image.mp (mem_of_max hy) with ⟨x, hx, rfl⟩, exact ⟨x, hx, λ x' hx', le_max_of_mem (mem_image_of_mem f hx') hy⟩, end lemma exists_min_image (s : finset β) (f : β → α) (h : s.nonempty) : ∃ x ∈ s, ∀ x' ∈ s, f x ≤ f x' := @exists_max_image αᵒᵈ β _ s f h end exists_max_min end finset namespace multiset lemma map_finset_sup [decidable_eq α] [decidable_eq β] (s : finset γ) (f : γ → multiset β) (g : β → α) (hg : function.injective g) : map g (s.sup f) = s.sup (map g ∘ f) := finset.comp_sup_eq_sup_comp _ (λ _ _, map_union hg) (map_zero _) lemma count_finset_sup [decidable_eq β] (s : finset α) (f : α → multiset β) (b : β) : count b (s.sup f) = s.sup (λa, count b (f a)) := begin letI := classical.dec_eq α, refine s.induction _ _, { exact count_zero _ }, { assume i s his ih, rw [finset.sup_insert, sup_eq_union, count_union, finset.sup_insert, ih], refl } end lemma mem_sup {α β} [decidable_eq β] {s : finset α} {f : α → multiset β} {x : β} : x ∈ s.sup f ↔ ∃ v ∈ s, x ∈ f v := begin classical, apply s.induction_on, { simp }, { intros a s has hxs, rw [finset.sup_insert, multiset.sup_eq_union, multiset.mem_union], split, { intro hxi, cases hxi with hf hf, { refine ⟨a, _, hf⟩, simp only [true_or, eq_self_iff_true, finset.mem_insert] }, { rcases hxs.mp hf with ⟨v, hv, hfv⟩, refine ⟨v, _, hfv⟩, simp only [hv, or_true, finset.mem_insert] } }, { rintros ⟨v, hv, hfv⟩, rw [finset.mem_insert] at hv, rcases hv with rfl | hv, { exact or.inl hfv }, { refine or.inr (hxs.mpr ⟨v, hv, hfv⟩) } } }, end end multiset namespace finset lemma mem_sup {α β} [decidable_eq β] {s : finset α} {f : α → finset β} {x : β} : x ∈ s.sup f ↔ ∃ v ∈ s, x ∈ f v := begin change _ ↔ ∃ v ∈ s, x ∈ (f v).val, rw [←multiset.mem_sup, ←multiset.mem_to_finset, sup_to_finset], simp_rw [val_to_finset], end lemma sup_eq_bUnion {α β} [decidable_eq β] (s : finset α) (t : α → finset β) : s.sup t = s.bUnion t := by { ext, rw [mem_sup, mem_bUnion], } @[simp] lemma sup_singleton'' [decidable_eq α] (s : finset β) (f : β → α) : s.sup (λ b, {f b}) = s.image f := by { ext a, rw [mem_sup, mem_image], simp only [mem_singleton, eq_comm] } @[simp] lemma sup_singleton' [decidable_eq α] (s : finset α) : s.sup singleton = s := (s.sup_singleton'' _).trans image_id end finset section lattice variables {ι' : Sort*} [complete_lattice α] /-- Supremum of `s i`, `i : ι`, is equal to the supremum over `t : finset ι` of suprema `⨆ i ∈ t, s i`. This version assumes `ι` is a `Type*`. See `supr_eq_supr_finset'` for a version that works for `ι : Sort*`. -/ lemma supr_eq_supr_finset (s : ι → α) : (⨆i, s i) = (⨆t:finset ι, ⨆i∈t, s i) := begin classical, exact le_antisymm (supr_le $ assume b, le_supr_of_le {b} $ le_supr_of_le b $ le_supr_of_le (by simp) $ le_rfl) (supr_le $ assume t, supr_le $ assume b, supr_le $ assume hb, le_supr _ _) end /-- Supremum of `s i`, `i : ι`, is equal to the supremum over `t : finset ι` of suprema `⨆ i ∈ t, s i`. This version works for `ι : Sort*`. See `supr_eq_supr_finset` for a version that assumes `ι : Type*` but has no `plift`s. -/ lemma supr_eq_supr_finset' (s : ι' → α) : (⨆i, s i) = (⨆t:finset (plift ι'), ⨆i∈t, s (plift.down i)) := by rw [← supr_eq_supr_finset, ← equiv.plift.surjective.supr_comp]; refl /-- Infimum of `s i`, `i : ι`, is equal to the infimum over `t : finset ι` of infima `⨅ i ∈ t, s i`. This version assumes `ι` is a `Type*`. See `infi_eq_infi_finset'` for a version that works for `ι : Sort*`. -/ lemma infi_eq_infi_finset (s : ι → α) : (⨅ i, s i) = ⨅ (t : finset ι) (i ∈ t), s i := @supr_eq_supr_finset αᵒᵈ _ _ _ /-- Infimum of `s i`, `i : ι`, is equal to the infimum over `t : finset ι` of infima `⨅ i ∈ t, s i`. This version works for `ι : Sort*`. See `infi_eq_infi_finset` for a version that assumes `ι : Type*` but has no `plift`s. -/ lemma infi_eq_infi_finset' (s : ι' → α) : (⨅i, s i) = (⨅t:finset (plift ι'), ⨅i∈t, s (plift.down i)) := @supr_eq_supr_finset' αᵒᵈ _ _ _ end lattice namespace set variables {ι' : Sort*} /-- Union of an indexed family of sets `s : ι → set α` is equal to the union of the unions of finite subfamilies. This version assumes `ι : Type*`. See also `Union_eq_Union_finset'` for a version that works for `ι : Sort*`. -/ lemma Union_eq_Union_finset (s : ι → set α) : (⋃i, s i) = (⋃t:finset ι, ⋃i∈t, s i) := supr_eq_supr_finset s /-- Union of an indexed family of sets `s : ι → set α` is equal to the union of the unions of finite subfamilies. This version works for `ι : Sort*`. See also `Union_eq_Union_finset` for a version that assumes `ι : Type*` but avoids `plift`s in the right hand side. -/ lemma Union_eq_Union_finset' (s : ι' → set α) : (⋃i, s i) = (⋃t:finset (plift ι'), ⋃i∈t, s (plift.down i)) := supr_eq_supr_finset' s /-- Intersection of an indexed family of sets `s : ι → set α` is equal to the intersection of the intersections of finite subfamilies. This version assumes `ι : Type*`. See also `Inter_eq_Inter_finset'` for a version that works for `ι : Sort*`. -/ lemma Inter_eq_Inter_finset (s : ι → set α) : (⋂i, s i) = (⋂t:finset ι, ⋂i∈t, s i) := infi_eq_infi_finset s /-- Intersection of an indexed family of sets `s : ι → set α` is equal to the intersection of the intersections of finite subfamilies. This version works for `ι : Sort*`. See also `Inter_eq_Inter_finset` for a version that assumes `ι : Type*` but avoids `plift`s in the right hand side. -/ lemma Inter_eq_Inter_finset' (s : ι' → set α) : (⋂i, s i) = (⋂t:finset (plift ι'), ⋂i∈t, s (plift.down i)) := infi_eq_infi_finset' s end set namespace finset /-! ### Interaction with ordered algebra structures -/ lemma sup_mul_le_mul_sup_of_nonneg [linear_ordered_semiring α] [order_bot α] {a b : ι → α} (s : finset ι) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.sup (a * b) ≤ s.sup a * s.sup b := finset.sup_le $ λ i hi, mul_le_mul (le_sup hi) (le_sup hi) (hb _ hi) ((ha _ hi).trans $ le_sup hi) lemma mul_inf_le_inf_mul_of_nonneg [linear_ordered_semiring α] [order_top α] {a b : ι → α} (s : finset ι) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.inf a * s.inf b ≤ s.inf (a * b) := finset.le_inf $ λ i hi, mul_le_mul (inf_le hi) (inf_le hi) (finset.le_inf hb) (ha i hi) lemma sup'_mul_le_mul_sup'_of_nonneg [linear_ordered_semiring α] {a b : ι → α} (s : finset ι) (H : s.nonempty) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.sup' H (a * b) ≤ s.sup' H a * s.sup' H b := sup'_le _ _ $ λ i hi, mul_le_mul (le_sup' _ hi) (le_sup' _ hi) (hb _ hi) ((ha _ hi).trans $ le_sup' _ hi) lemma inf'_mul_le_mul_inf'_of_nonneg [linear_ordered_semiring α] {a b : ι → α} (s : finset ι) (H : s.nonempty) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.inf' H a * s.inf' H b ≤ s.inf' H (a * b) := le_inf' _ _ $ λ i hi, mul_le_mul (inf'_le _ hi) (inf'_le _ hi) (le_inf' _ _ hb) (ha _ hi) open function /-! ### Interaction with big lattice/set operations -/ section lattice lemma supr_coe [has_Sup β] (f : α → β) (s : finset α) : (⨆ x ∈ (↑s : set α), f x) = ⨆ x ∈ s, f x := rfl lemma infi_coe [has_Inf β] (f : α → β) (s : finset α) : (⨅ x ∈ (↑s : set α), f x) = ⨅ x ∈ s, f x := rfl variables [complete_lattice β] theorem supr_singleton (a : α) (s : α → β) : (⨆ x ∈ ({a} : finset α), s x) = s a := by simp theorem infi_singleton (a : α) (s : α → β) : (⨅ x ∈ ({a} : finset α), s x) = s a := by simp lemma supr_option_to_finset (o : option α) (f : α → β) : (⨆ x ∈ o.to_finset, f x) = ⨆ x ∈ o, f x := by simp lemma infi_option_to_finset (o : option α) (f : α → β) : (⨅ x ∈ o.to_finset, f x) = ⨅ x ∈ o, f x := @supr_option_to_finset _ βᵒᵈ _ _ _ variables [decidable_eq α] theorem supr_union {f : α → β} {s t : finset α} : (⨆ x ∈ s ∪ t, f x) = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) := by simp [supr_or, supr_sup_eq] theorem infi_union {f : α → β} {s t : finset α} : (⨅ x ∈ s ∪ t, f x) = (⨅ x ∈ s, f x) ⊓ (⨅ x ∈ t, f x) := @supr_union α βᵒᵈ _ _ _ _ _ lemma supr_insert (a : α) (s : finset α) (t : α → β) : (⨆ x ∈ insert a s, t x) = t a ⊔ (⨆ x ∈ s, t x) := by { rw insert_eq, simp only [supr_union, finset.supr_singleton] } lemma infi_insert (a : α) (s : finset α) (t : α → β) : (⨅ x ∈ insert a s, t x) = t a ⊓ (⨅ x ∈ s, t x) := @supr_insert α βᵒᵈ _ _ _ _ _ lemma supr_finset_image {f : γ → α} {g : α → β} {s : finset γ} : (⨆ x ∈ s.image f, g x) = (⨆ y ∈ s, g (f y)) := by rw [← supr_coe, coe_image, supr_image, supr_coe] lemma sup_finset_image {β γ : Type*} [semilattice_sup β] [order_bot β] (f : γ → α) (g : α → β) (s : finset γ) : (s.image f).sup g = s.sup (g ∘ f) := begin classical, induction s using finset.induction_on with a s' ha ih; simp * end lemma infi_finset_image {f : γ → α} {g : α → β} {s : finset γ} : (⨅ x ∈ s.image f, g x) = (⨅ y ∈ s, g (f y)) := by rw [← infi_coe, coe_image, infi_image, infi_coe] lemma supr_insert_update {x : α} {t : finset α} (f : α → β) {s : β} (hx : x ∉ t) : (⨆ (i ∈ insert x t), function.update f x s i) = (s ⊔ ⨆ (i ∈ t), f i) := begin simp only [finset.supr_insert, update_same], rcongr i hi, apply update_noteq, rintro rfl, exact hx hi end lemma infi_insert_update {x : α} {t : finset α} (f : α → β) {s : β} (hx : x ∉ t) : (⨅ (i ∈ insert x t), update f x s i) = (s ⊓ ⨅ (i ∈ t), f i) := @supr_insert_update α βᵒᵈ _ _ _ _ f _ hx lemma supr_bUnion (s : finset γ) (t : γ → finset α) (f : α → β) : (⨆ y ∈ s.bUnion t, f y) = ⨆ (x ∈ s) (y ∈ t x), f y := by simp [@supr_comm _ α, supr_and] lemma infi_bUnion (s : finset γ) (t : γ → finset α) (f : α → β) : (⨅ y ∈ s.bUnion t, f y) = ⨅ (x ∈ s) (y ∈ t x), f y := @supr_bUnion _ βᵒᵈ _ _ _ _ _ _ end lattice theorem set_bUnion_coe (s : finset α) (t : α → set β) : (⋃ x ∈ (↑s : set α), t x) = ⋃ x ∈ s, t x := rfl theorem set_bInter_coe (s : finset α) (t : α → set β) : (⋂ x ∈ (↑s : set α), t x) = ⋂ x ∈ s, t x := rfl theorem set_bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : finset α), s x) = s a := supr_singleton a s theorem set_bInter_singleton (a : α) (s : α → set β) : (⋂ x ∈ ({a} : finset α), s x) = s a := infi_singleton a s @[simp] lemma set_bUnion_preimage_singleton (f : α → β) (s : finset β) : (⋃ y ∈ s, f ⁻¹' {y}) = f ⁻¹' s := set.bUnion_preimage_singleton f s lemma set_bUnion_option_to_finset (o : option α) (f : α → set β) : (⋃ x ∈ o.to_finset, f x) = ⋃ x ∈ o, f x := supr_option_to_finset o f lemma set_bInter_option_to_finset (o : option α) (f : α → set β) : (⋂ x ∈ o.to_finset, f x) = ⋂ x ∈ o, f x := infi_option_to_finset o f lemma subset_set_bUnion_of_mem {s : finset α} {f : α → set β} {x : α} (h : x ∈ s) : f x ⊆ ⋃ (y ∈ s), f y := show f x ≤ (⨆ y ∈ s, f y), from le_supr_of_le x $ le_supr _ h variables [decidable_eq α] lemma set_bUnion_union (s t : finset α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union lemma set_bInter_inter (s t : finset α) (u : α → set β) : (⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) := infi_union lemma set_bUnion_insert (a : α) (s : finset α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := supr_insert a s t lemma set_bInter_insert (a : α) (s : finset α) (t : α → set β) : (⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) := infi_insert a s t lemma set_bUnion_finset_image {f : γ → α} {g : α → set β} {s : finset γ} : (⋃x ∈ s.image f, g x) = (⋃y ∈ s, g (f y)) := supr_finset_image lemma set_bInter_finset_image {f : γ → α} {g : α → set β} {s : finset γ} : (⋂ x ∈ s.image f, g x) = (⋂ y ∈ s, g (f y)) := infi_finset_image lemma set_bUnion_insert_update {x : α} {t : finset α} (f : α → set β) {s : set β} (hx : x ∉ t) : (⋃ (i ∈ insert x t), @update _ _ _ f x s i) = (s ∪ ⋃ (i ∈ t), f i) := supr_insert_update f hx lemma set_bInter_insert_update {x : α} {t : finset α} (f : α → set β) {s : set β} (hx : x ∉ t) : (⋂ (i ∈ insert x t), @update _ _ _ f x s i) = (s ∩ ⋂ (i ∈ t), f i) := infi_insert_update f hx lemma set_bUnion_bUnion (s : finset γ) (t : γ → finset α) (f : α → set β) : (⋃ y ∈ s.bUnion t, f y) = ⋃ (x ∈ s) (y ∈ t x), f y := supr_bUnion s t f lemma set_bInter_bUnion (s : finset γ) (t : γ → finset α) (f : α → set β) : (⋂ y ∈ s.bUnion t, f y) = ⋂ (x ∈ s) (y ∈ t x), f y := infi_bUnion s t f end finset
3c0d8f02f70960fd952f30bfed22ff43fd162b30
0408d6ea582e97d5cc55ac8a0980d26df896ce66
/src/tests.lean
99b9ddab56fb2a4ae29c49016da62d34585d8b67
[]
no_license
lean-forward/coe_tactic
bba94535c7fb6ef65fcd81bedc28062b238a1594
d5c30df244c3402de6323a538e99b5324779a2cb
refs/heads/master
1,588,474,971,049
1,557,135,293,000
1,557,135,293,000
178,387,237
1
0
null
1,556,542,286,000
1,553,856,413,000
Lean
UTF-8
Lean
false
false
2,518
lean
/- unit tests suggested by Kevin Buzzard -/ import data.complex.basic -- ℕ, ℤ, ℚ, ℝ, ℂ import norm_cast constants (an bn cn dn : ℕ) (az bz cz dz : ℤ) (aq bq cq dq : ℚ) constants (ar br cr dr : ℝ) (ac bc cc dc : ℂ) example : (an : ℤ) = bn → an = bn := λ h, by exact_mod_cast h -- by simp example : an = bn → (an : ℤ) = bn := λ h, by exact_mod_cast h -- by simp example : az = bz ↔ (az : ℚ) = bz := by norm_cast -- by simp example : (aq : ℝ) = br ↔ (aq : ℂ) = br := by norm_cast example : (an : ℚ) = bz ↔ (an : ℂ) = bz := by norm_cast example : (((an : ℤ) : ℚ) : ℝ) = bq ↔ ((an : ℚ) : ℂ) = (bq : ℝ) := by norm_cast example : (an : ℤ) < bn ↔ an < bn := by norm_cast -- by simp example : (an : ℚ) < bz ↔ (an : ℝ) < bz := by norm_cast example : ((an : ℤ) : ℝ) < bq ↔ (an : ℚ) < bq := by norm_cast example : (an : ℤ) ≠ (bn : ℤ) ↔ an ≠ bn := by norm_cast -- by simp -- zero and one cause special problems example : 0 < (bq : ℝ) ↔ 0 < bq := by norm_cast -- by simp example : az > (1 : ℕ) ↔ az > 1 := by norm_cast -- by simp example : az > (0 : ℕ) ↔ az > 0 := by norm_cast -- by simp example : (an : ℤ) ≠ 0 ↔ an ≠ 0 := by norm_cast -- by simp example : aq < (1 : ℕ) ↔ (aq : ℝ) < (1 : ℤ) := by norm_cast example : (an : ℤ) + bn = (an + bn : ℕ) := by norm_cast -- by simp example : (an : ℂ) + bq = ((an + bq) : ℚ) := by norm_cast -- by simp example : (((an : ℤ) : ℚ) : ℝ) + bn = (an + (bn : ℤ)) := by norm_cast -- by simp example : (((((an : ℚ) : ℝ) * bq) + (cq : ℝ) ^ dn) : ℂ) = (an : ℂ) * (bq : ℝ) + cq ^ dn := by norm_cast -- by simp example : ((an : ℤ) : ℝ) < bq ∧ (cr : ℂ) ^ 2 = dz ↔ (an : ℚ) < bq ∧ ((cr ^ 2) : ℂ) = dz := by norm_cast example : (an : ℤ) = 1 → an = 1 := λ h, by exact_mod_cast h example : (an : ℤ) < 5 → an < 5 := λ h, by exact_mod_cast h example : an < 5 → (an : ℤ) < 5 := λ h, by exact_mod_cast h example : (an + 5) < 10 → (an : ℤ) + 5 < 10 := λ h, by exact_mod_cast h example : (an : ℤ) + 5 < 10 → (an + 5) < 10 := λ h, by exact_mod_cast h example : ((an + 5 : ℕ) : ℤ) < 10 → an + 5 < 10 := λ h, by exact_mod_cast h example : an + 5 < 10 → ((an + 5 : ℕ) : ℤ) < 10 := λ h, by exact_mod_cast h example (h : (cz : ℚ) = az / bz) : (cz : ℝ) = az / bz := by rw_mod_cast [← rat.cast_coe_int az, h] example (h : bn ≤ an) : an - bn = 1 ↔ (an - bn : ℤ) = 1 := by norm_cast
c57a6de17899c3052bc268731e11d4ae66e8d2d6
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/special_functions/complex/log.lean
7965cd052fc0aeceabbbcd3b90c40688d2109d30
[ "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,401
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson -/ import analysis.special_functions.complex.arg import analysis.special_functions.log.basic /-! # The complex `log` function > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Basic properties, relationship with `exp`. -/ noncomputable theory namespace complex open set filter open_locale real topology complex_conjugate /-- Inverse of the `exp` function. Returns values such that `(log x).im > - π` and `(log x).im ≤ π`. `log 0 = 0`-/ @[pp_nodot] noncomputable def log (x : ℂ) : ℂ := x.abs.log + arg x * I lemma log_re (x : ℂ) : x.log.re = x.abs.log := by simp [log] lemma log_im (x : ℂ) : x.log.im = x.arg := by simp [log] lemma neg_pi_lt_log_im (x : ℂ) : -π < (log x).im := by simp only [log_im, neg_pi_lt_arg] lemma log_im_le_pi (x : ℂ) : (log x).im ≤ π := by simp only [log_im, arg_le_pi] lemma exp_log {x : ℂ} (hx : x ≠ 0) : exp (log x) = x := by rw [log, exp_add_mul_I, ← of_real_sin, sin_arg, ← of_real_cos, cos_arg hx, ← of_real_exp, real.exp_log (abs.pos hx), mul_add, of_real_div, of_real_div, mul_div_cancel' _ (of_real_ne_zero.2 $ abs.ne_zero hx), ← mul_assoc, mul_div_cancel' _ (of_real_ne_zero.2 $ abs.ne_zero hx), re_add_im] @[simp] lemma range_exp : range exp = {0}ᶜ := set.ext $ λ x, ⟨by { rintro ⟨x, rfl⟩, exact exp_ne_zero x }, λ hx, ⟨log x, exp_log hx⟩⟩ lemma log_exp {x : ℂ} (hx₁ : -π < x.im) (hx₂: x.im ≤ π) : log (exp x) = x := by rw [log, abs_exp, real.log_exp, exp_eq_exp_re_mul_sin_add_cos, ← of_real_exp, arg_mul_cos_add_sin_mul_I (real.exp_pos _) ⟨hx₁, hx₂⟩, re_add_im] lemma exp_inj_of_neg_pi_lt_of_le_pi {x y : ℂ} (hx₁ : -π < x.im) (hx₂ : x.im ≤ π) (hy₁ : - π < y.im) (hy₂ : y.im ≤ π) (hxy : exp x = exp y) : x = y := by rw [← log_exp hx₁ hx₂, ← log_exp hy₁ hy₂, hxy] lemma of_real_log {x : ℝ} (hx : 0 ≤ x) : (x.log : ℂ) = log x := complex.ext (by rw [log_re, of_real_re, abs_of_nonneg hx]) (by rw [of_real_im, log_im, arg_of_real_of_nonneg hx]) lemma log_of_real_re (x : ℝ) : (log (x : ℂ)).re = real.log x := by simp [log_re] lemma log_of_real_mul {r : ℝ} (hr : 0 < r) {x : ℂ} (hx : x ≠ 0) : log (r * x) = real.log r + log x := begin replace hx := complex.abs.ne_zero_iff.mpr hx, simp_rw [log, map_mul, abs_of_real, arg_real_mul _ hr, abs_of_pos hr, real.log_mul hr.ne' hx, of_real_add, add_assoc], end lemma log_mul_of_real (r : ℝ) (hr : 0 < r) (x : ℂ) (hx : x ≠ 0) : log (x * r) = real.log r + log x := by rw [mul_comm, log_of_real_mul hr hx, add_comm] @[simp] lemma log_zero : log 0 = 0 := by simp [log] @[simp] lemma log_one : log 1 = 0 := by simp [log] lemma log_neg_one : log (-1) = π * I := by simp [log] lemma log_I : log I = π / 2 * I := by simp [log] lemma log_neg_I : log (-I) = -(π / 2) * I := by simp [log] lemma log_conj_eq_ite (x : ℂ) : log (conj x) = if x.arg = π then log x else conj (log x) := begin simp_rw [log, abs_conj, arg_conj, map_add, map_mul, conj_of_real], split_ifs with hx, { rw hx }, simp_rw [of_real_neg, conj_I, mul_neg, neg_mul] end lemma log_conj (x : ℂ) (h : x.arg ≠ π) : log (conj x) = conj (log x) := by rw [log_conj_eq_ite, if_neg h] lemma log_inv_eq_ite (x : ℂ) : log (x⁻¹) = if x.arg = π then -conj (log x) else -log x := begin by_cases hx : x = 0, { simp [hx] }, rw [inv_def, log_mul_of_real, real.log_inv, of_real_neg, ←sub_eq_neg_add, log_conj_eq_ite], { simp_rw [log, map_add, map_mul, conj_of_real, conj_I, norm_sq_eq_abs, real.log_pow, nat.cast_two, of_real_mul, of_real_bit0, of_real_one, neg_add, mul_neg, two_mul, neg_neg], split_ifs, { rw [add_sub_right_comm, sub_add_cancel'] }, { rw [add_sub_right_comm, sub_add_cancel'] } }, { rwa [inv_pos, complex.norm_sq_pos] }, { rwa map_ne_zero }, end lemma log_inv (x : ℂ) (hx : x.arg ≠ π) : log (x⁻¹) = -log x := by rw [log_inv_eq_ite, if_neg hx] lemma two_pi_I_ne_zero : (2 * π * I : ℂ) ≠ 0 := by norm_num [real.pi_ne_zero, I_ne_zero] lemma exp_eq_one_iff {x : ℂ} : exp x = 1 ↔ ∃ n : ℤ, x = n * ((2 * π) * I) := begin split, { intro h, rcases exists_unique_add_zsmul_mem_Ioc real.two_pi_pos x.im (-π) with ⟨n, hn, -⟩, use -n, rw [int.cast_neg, neg_mul, eq_neg_iff_add_eq_zero], have : (x + n * (2 * π * I)).im ∈ Ioc (-π) π, by simpa [two_mul, mul_add] using hn, rw [← log_exp this.1 this.2, exp_periodic.int_mul n, h, log_one] }, { rintro ⟨n, rfl⟩, exact (exp_periodic.int_mul n).eq.trans exp_zero } end lemma exp_eq_exp_iff_exp_sub_eq_one {x y : ℂ} : exp x = exp y ↔ exp (x - y) = 1 := by rw [exp_sub, div_eq_one_iff_eq (exp_ne_zero _)] lemma exp_eq_exp_iff_exists_int {x y : ℂ} : exp x = exp y ↔ ∃ n : ℤ, x = y + n * ((2 * π) * I) := by simp only [exp_eq_exp_iff_exp_sub_eq_one, exp_eq_one_iff, sub_eq_iff_eq_add'] @[simp] lemma countable_preimage_exp {s : set ℂ} : (exp ⁻¹' s).countable ↔ s.countable := begin refine ⟨λ hs, _, λ hs, _⟩, { refine ((hs.image exp).insert 0).mono _, rw [image_preimage_eq_inter_range, range_exp, ← diff_eq, ← union_singleton, diff_union_self], exact subset_union_left _ _ }, { rw ← bUnion_preimage_singleton, refine hs.bUnion (λ z hz, _), rcases em (∃ w, exp w = z) with ⟨w, rfl⟩|hne, { simp only [preimage, mem_singleton_iff, exp_eq_exp_iff_exists_int, set_of_exists], exact countable_Union (λ m, countable_singleton _) }, { push_neg at hne, simp [preimage, hne] } } end alias countable_preimage_exp ↔ _ _root_.set.countable.preimage_cexp lemma tendsto_log_nhds_within_im_neg_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : tendsto log (𝓝[{z : ℂ | z.im < 0}] z) (𝓝 $ real.log (abs z) - π * I) := begin have := (continuous_of_real.continuous_at.comp_continuous_within_at (continuous_abs.continuous_within_at.log _)).tendsto.add (((continuous_of_real.tendsto _).comp $ tendsto_arg_nhds_within_im_neg_of_re_neg_of_im_zero hre him).mul tendsto_const_nhds), convert this, { simp [sub_eq_add_neg] }, { lift z to ℝ using him, simpa using hre.ne } end lemma continuous_within_at_log_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : continuous_within_at log {z : ℂ | 0 ≤ z.im} z := begin have := (continuous_of_real.continuous_at.comp_continuous_within_at (continuous_abs.continuous_within_at.log _)).tendsto.add ((continuous_of_real.continuous_at.comp_continuous_within_at $ continuous_within_at_arg_of_re_neg_of_im_zero hre him).mul tendsto_const_nhds), convert this, { lift z to ℝ using him, simpa using hre.ne } end lemma tendsto_log_nhds_within_im_nonneg_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : tendsto log (𝓝[{z : ℂ | 0 ≤ z.im}] z) (𝓝 $ real.log (abs z) + π * I) := by simpa only [log, arg_eq_pi_iff.2 ⟨hre, him⟩] using (continuous_within_at_log_of_re_neg_of_im_zero hre him).tendsto @[simp] lemma map_exp_comap_re_at_bot : map exp (comap re at_bot) = 𝓝[≠] 0 := by rw [← comap_exp_nhds_zero, map_comap, range_exp, nhds_within] @[simp] lemma map_exp_comap_re_at_top : map exp (comap re at_top) = comap abs at_top := begin rw [← comap_exp_comap_abs_at_top, map_comap, range_exp, inf_eq_left, le_principal_iff], exact eventually_ne_of_tendsto_norm_at_top tendsto_comap 0 end end complex section log_deriv open complex filter open_locale topology variables {α : Type*} lemma continuous_at_clog {x : ℂ} (h : 0 < x.re ∨ x.im ≠ 0) : continuous_at log x := begin refine continuous_at.add _ _, { refine continuous_of_real.continuous_at.comp _, refine (real.continuous_at_log _).comp complex.continuous_abs.continuous_at, rw complex.abs.ne_zero_iff, rintro rfl, simpa using h }, { have h_cont_mul : continuous (λ x : ℂ, x * I), from continuous_id'.mul continuous_const, refine h_cont_mul.continuous_at.comp (continuous_of_real.continuous_at.comp _), exact continuous_at_arg h, }, end lemma filter.tendsto.clog {l : filter α} {f : α → ℂ} {x : ℂ} (h : tendsto f l (𝓝 x)) (hx : 0 < x.re ∨ x.im ≠ 0) : tendsto (λ t, log (f t)) l (𝓝 $ log x) := (continuous_at_clog hx).tendsto.comp h variables [topological_space α] lemma continuous_at.clog {f : α → ℂ} {x : α} (h₁ : continuous_at f x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : continuous_at (λ t, log (f t)) x := h₁.clog h₂ lemma continuous_within_at.clog {f : α → ℂ} {s : set α} {x : α} (h₁ : continuous_within_at f s x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : continuous_within_at (λ t, log (f t)) s x := h₁.clog h₂ lemma continuous_on.clog {f : α → ℂ} {s : set α} (h₁ : continuous_on f s) (h₂ : ∀ x ∈ s, 0 < (f x).re ∨ (f x).im ≠ 0) : continuous_on (λ t, log (f t)) s := λ x hx, (h₁ x hx).clog (h₂ x hx) lemma continuous.clog {f : α → ℂ} (h₁ : continuous f) (h₂ : ∀ x, 0 < (f x).re ∨ (f x).im ≠ 0) : continuous (λ t, log (f t)) := continuous_iff_continuous_at.2 $ λ x, h₁.continuous_at.clog (h₂ x) end log_deriv
856232d0e0a8fa939bbf360a12db7cc9be5dddc2
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/data/padics/hensel.lean
05190cb5bb93d1c8da6de499c80b061480268847
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
21,547
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import data.padics.padic_integers import topology.metric_space.cau_seq_filter import analysis.specific_limits import topology.algebra.polynomial /-! # Hensel's lemma on ℤ_p This file proves Hensel's lemma on ℤ_p, roughly following Keith Conrad's writeup: <http://www.math.uconn.edu/~kconrad/blurbs/gradnumthy/hensel.pdf> Hensel's lemma gives a simple condition for the existence of a root of a polynomial. The proof and motivation are described in the paper [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019]. ## References * <http://www.math.uconn.edu/~kconrad/blurbs/gradnumthy/hensel.pdf> * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/Hensel%27s_lemma> ## Tags p-adic, p adic, padic, p-adic integer -/ noncomputable theory open_locale classical topological_space -- We begin with some general lemmas that are used below in the computation. lemma padic_polynomial_dist {p : ℕ} [fact p.prime] (F : polynomial ℤ_[p]) (x y : ℤ_[p]) : ∥F.eval x - F.eval y∥ ≤ ∥x - y∥ := let ⟨z, hz⟩ := F.eval_sub_factor x y in calc ∥F.eval x - F.eval y∥ = ∥z∥ * ∥x - y∥ : by simp [hz] ... ≤ 1 * ∥x - y∥ : mul_le_mul_of_nonneg_right (padic_norm_z.le_one _) (norm_nonneg _) ... = ∥x - y∥ : by simp open filter metric private lemma comp_tendsto_lim {p : ℕ} [fact p.prime] {F : polynomial ℤ_[p]} (ncs : cau_seq ℤ_[p] norm) : tendsto (λ i, F.eval (ncs i)) at_top (𝓝 (F.eval ncs.lim)) := (F.continuous_eval.tendsto _).comp ncs.tendsto_limit section parameters {p : ℕ} [fact p.prime] {ncs : cau_seq ℤ_[p] norm} {F : polynomial ℤ_[p]} {a : ℤ_[p]} (ncs_der_val : ∀ n, ∥F.derivative.eval (ncs n)∥ = ∥F.derivative.eval a∥) include ncs_der_val private lemma ncs_tendsto_const : tendsto (λ i, ∥F.derivative.eval (ncs i)∥) at_top (𝓝 ∥F.derivative.eval a∥) := by convert tendsto_const_nhds; ext; rw ncs_der_val private lemma ncs_tendsto_lim : tendsto (λ i, ∥F.derivative.eval (ncs i)∥) at_top (𝓝 (∥F.derivative.eval ncs.lim∥)) := tendsto.comp (continuous_iff_continuous_at.1 continuous_norm _) (comp_tendsto_lim _) private lemma norm_deriv_eq : ∥F.derivative.eval ncs.lim∥ = ∥F.derivative.eval a∥ := tendsto_nhds_unique at_top_ne_bot ncs_tendsto_lim ncs_tendsto_const end section parameters {p : ℕ} [fact p.prime] {ncs : cau_seq ℤ_[p] norm} {F : polynomial ℤ_[p]} (hnorm : tendsto (λ i, ∥F.eval (ncs i)∥) at_top (𝓝 0)) include hnorm private lemma tendsto_zero_of_norm_tendsto_zero : tendsto (λ i, F.eval (ncs i)) at_top (𝓝 0) := tendsto_iff_norm_tendsto_zero.2 (by simpa using hnorm) lemma limit_zero_of_norm_tendsto_zero : F.eval ncs.lim = 0 := tendsto_nhds_unique at_top_ne_bot (comp_tendsto_lim _) tendsto_zero_of_norm_tendsto_zero end section hensel open nat parameters {p : ℕ} [fact p.prime] {F : polynomial ℤ_[p]} {a : ℤ_[p]} (hnorm : ∥F.eval a∥ < ∥F.derivative.eval a∥^2) (hnsol : F.eval a ≠ 0) include hnorm /-- `T` is an auxiliary value that is used to control the behavior of the polynomial `F`. -/ private def T : ℝ := ∥(F.eval a).val / ((F.derivative.eval a).val)^2∥ private lemma deriv_sq_norm_pos : 0 < ∥F.derivative.eval a∥ ^ 2 := lt_of_le_of_lt (norm_nonneg _) hnorm private lemma deriv_sq_norm_ne_zero : ∥F.derivative.eval a∥^2 ≠ 0 := ne_of_gt deriv_sq_norm_pos private lemma deriv_norm_ne_zero : ∥F.derivative.eval a∥ ≠ 0 := λ h, deriv_sq_norm_ne_zero (by simp [*, _root_.pow_two]) private lemma deriv_norm_pos : 0 < ∥F.derivative.eval a∥ := lt_of_le_of_ne (norm_nonneg _) (ne.symm deriv_norm_ne_zero) private lemma deriv_ne_zero : F.derivative.eval a ≠ 0 := mt norm_eq_zero.2 deriv_norm_ne_zero private lemma T_def : T = ∥F.eval a∥ / ∥F.derivative.eval a∥^2 := calc T = ∥(F.eval a).val∥ / ∥((F.derivative.eval a).val)^2∥ : normed_field.norm_div _ _ ... = ∥F.eval a∥ / ∥(F.derivative.eval a)^2∥ : by simp [norm, padic_norm_z] ... = ∥F.eval a∥ / ∥(F.derivative.eval a)∥^2 : by simp [pow, monoid.pow] private lemma T_lt_one : T < 1 := let h := (div_lt_one_iff_lt deriv_sq_norm_pos).2 hnorm in by rw T_def; apply h private lemma T_pow {n : ℕ} (hn : n > 0) : T ^ n < 1 := have T ^ n ≤ T ^ 1, from pow_le_pow_of_le_one (norm_nonneg _) (le_of_lt T_lt_one) (succ_le_of_lt hn), lt_of_le_of_lt (by simpa) T_lt_one private lemma T_pow' (n : ℕ) : T ^ (2 ^ n) < 1 := (T_pow (nat.pow_pos (by norm_num) _)) private lemma T_pow_nonneg (n : ℕ) : T ^ n ≥ 0 := pow_nonneg (norm_nonneg _) _ /-- We will construct a sequence of elements of ℤ_p satisfying successive values of `ih`. -/ private def ih (n : ℕ) (z : ℤ_[p]) : Prop := ∥F.derivative.eval z∥ = ∥F.derivative.eval a∥ ∧ ∥F.eval z∥ ≤ ∥F.derivative.eval a∥^2 * T ^ (2^n) private lemma ih_0 : ih 0 a := ⟨ rfl, by simp [T_def, mul_div_cancel' _ (ne_of_gt (deriv_sq_norm_pos hnorm))] ⟩ private lemma calc_norm_le_one {n : ℕ} {z : ℤ_[p]} (hz : ih n z) : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1 := calc ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ = ∥(↑(F.eval z) : ℚ_[p])∥ / ∥(↑(F.derivative.eval z) : ℚ_[p])∥ : normed_field.norm_div _ _ ... = ∥F.eval z∥ / ∥F.derivative.eval a∥ : by simp [hz.1] ... ≤ ∥F.derivative.eval a∥^2 * T^(2^n) / ∥F.derivative.eval a∥ : (div_le_div_right deriv_norm_pos).2 hz.2 ... = ∥F.derivative.eval a∥ * T^(2^n) : div_sq_cancel (ne_of_gt deriv_norm_pos) _ ... ≤ 1 : mul_le_one (padic_norm_z.le_one _) (T_pow_nonneg _) (le_of_lt (T_pow' _)) private lemma calc_deriv_dist {z z' z1 : ℤ_[p]} (hz' : z' = z - z1) (hz1 : ∥z1∥ = ∥F.eval z∥ / ∥F.derivative.eval a∥) {n} (hz : ih n z) : ∥F.derivative.eval z' - F.derivative.eval z∥ < ∥F.derivative.eval a∥ := calc ∥F.derivative.eval z' - F.derivative.eval z∥ ≤ ∥z' - z∥ : padic_polynomial_dist _ _ _ ... = ∥z1∥ : by simp only [sub_eq_add_neg, add_assoc, hz', add_add_neg_cancel'_right, norm_neg] ... = ∥F.eval z∥ / ∥F.derivative.eval a∥ : hz1 ... ≤ ∥F.derivative.eval a∥^2 * T^(2^n) / ∥F.derivative.eval a∥ : (div_le_div_right deriv_norm_pos).2 hz.2 ... = ∥F.derivative.eval a∥ * T^(2^n) : div_sq_cancel deriv_norm_ne_zero _ ... < ∥F.derivative.eval a∥ : (mul_lt_iff_lt_one_right deriv_norm_pos).2 (T_pow (pow_pos (by norm_num) _)) private def calc_eval_z' {z z' z1 : ℤ_[p]} (hz' : z' = z - z1) {n} (hz : ih n z) (h1 : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1) (hzeq : z1 = ⟨_, h1⟩) : {q : ℤ_[p] // F.eval z' = q * z1^2} := have hdzne' : (↑(F.derivative.eval z) : ℚ_[p]) ≠ 0, from have hdzne : F.derivative.eval z ≠ 0, from mt norm_eq_zero.2 (by rw hz.1; apply deriv_norm_ne_zero; assumption), λ h, hdzne $ subtype.ext.2 h, let ⟨q, hq⟩ := F.binom_expansion z (-z1) in have ∥(↑(F.derivative.eval z) * (↑(F.eval z) / ↑(F.derivative.eval z)) : ℚ_[p])∥ ≤ 1, by {rw padic_norm_e.mul, apply mul_le_one, apply padic_norm_z.le_one, apply norm_nonneg, apply h1}, have F.derivative.eval z * (-z1) = -F.eval z, from calc F.derivative.eval z * (-z1) = (F.derivative.eval z) * -⟨↑(F.eval z) / ↑(F.derivative.eval z), h1⟩ : by rw [hzeq] ... = -((F.derivative.eval z) * ⟨↑(F.eval z) / ↑(F.derivative.eval z), h1⟩) : by simp ... = -(⟨↑(F.derivative.eval z) * (↑(F.eval z) / ↑(F.derivative.eval z)), this⟩) : subtype.ext.2 $ by simp ... = -(F.eval z) : by simp [mul_div_cancel' _ hdzne'], have heq : F.eval z' = q * z1^2, by simpa [this, hz'] using hq, ⟨q, heq⟩ private def calc_eval_z'_norm {z z' z1 : ℤ_[p]} {n} (hz : ih n z) {q} (heq : F.eval z' = q * z1^2) (h1 : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1) (hzeq : z1 = ⟨_, h1⟩) : ∥F.eval z'∥ ≤ ∥F.derivative.eval a∥^2 * T^(2^(n+1)) := calc ∥F.eval z'∥ = ∥q∥ * ∥z1∥^2 : by simp [heq] ... ≤ 1 * ∥z1∥^2 : mul_le_mul_of_nonneg_right (padic_norm_z.le_one _) (pow_nonneg (norm_nonneg _) _) ... = ∥F.eval z∥^2 / ∥F.derivative.eval a∥^2 : by simp [hzeq, hz.1, div_pow] ... ≤ (∥F.derivative.eval a∥^2 * T^(2^n))^2 / ∥F.derivative.eval a∥^2 : (div_le_div_right deriv_sq_norm_pos).2 (pow_le_pow_of_le_left (norm_nonneg _) hz.2 _) ... = (∥F.derivative.eval a∥^2)^2 * (T^(2^n))^2 / ∥F.derivative.eval a∥^2 : by simp only [_root_.mul_pow] ... = ∥F.derivative.eval a∥^2 * (T^(2^n))^2 : div_sq_cancel deriv_sq_norm_ne_zero _ ... = ∥F.derivative.eval a∥^2 * T^(2^(n + 1)) : by rw [←pow_mul]; refl set_option eqn_compiler.zeta true /-- Given `z : ℤ_[p]` satisfying `ih n z`, construct `z' : ℤ_[p]` satisfying `ih (n+1) z'`. We need the hypothesis `ih n z`, since otherwise `z'` is not necessarily an integer. -/ private def ih_n {n : ℕ} {z : ℤ_[p]} (hz : ih n z) : {z' : ℤ_[p] // ih (n+1) z'} := have h1 : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1, from calc_norm_le_one hz, let z1 : ℤ_[p] := ⟨_, h1⟩, z' : ℤ_[p] := z - z1 in ⟨ z', have hdist : ∥F.derivative.eval z' - F.derivative.eval z∥ < ∥F.derivative.eval a∥, from calc_deriv_dist rfl (by simp [z1, hz.1]) hz, have hfeq : ∥F.derivative.eval z'∥ = ∥F.derivative.eval a∥, begin rw [sub_eq_add_neg, ← hz.1, ←norm_neg (F.derivative.eval z)] at hdist, have := padic_norm_z.eq_of_norm_add_lt_right hdist, rwa [norm_neg, hz.1] at this end, let ⟨q, heq⟩ := calc_eval_z' rfl hz h1 rfl in have hnle : ∥F.eval z'∥ ≤ ∥F.derivative.eval a∥^2 * T^(2^(n+1)), from calc_eval_z'_norm hz heq h1 rfl, ⟨hfeq, hnle⟩⟩ set_option eqn_compiler.zeta false -- why doesn't "noncomputable theory" stick here? private noncomputable def newton_seq_aux : Π n : ℕ, {z : ℤ_[p] // ih n z} | 0 := ⟨a, ih_0⟩ | (k+1) := ih_n (newton_seq_aux k).2 private def newton_seq (n : ℕ) : ℤ_[p] := (newton_seq_aux n).1 private lemma newton_seq_deriv_norm (n : ℕ) : ∥F.derivative.eval (newton_seq n)∥ = ∥F.derivative.eval a∥ := (newton_seq_aux n).2.1 private lemma newton_seq_norm_le (n : ℕ) : ∥F.eval (newton_seq n)∥ ≤ ∥F.derivative.eval a∥^2 * T ^ (2^n) := (newton_seq_aux n).2.2 private lemma newton_seq_norm_eq (n : ℕ) : ∥newton_seq (n+1) - newton_seq n∥ = ∥F.eval (newton_seq n)∥ / ∥F.derivative.eval (newton_seq n)∥ := by induction n; simp [sub_eq_add_neg, add_left_comm, add_assoc, newton_seq, newton_seq_aux, ih_n] private lemma newton_seq_succ_dist (n : ℕ) : ∥newton_seq (n+1) - newton_seq n∥ ≤ ∥F.derivative.eval a∥ * T^(2^n) := calc ∥newton_seq (n+1) - newton_seq n∥ = ∥F.eval (newton_seq n)∥ / ∥F.derivative.eval (newton_seq n)∥ : newton_seq_norm_eq _ ... = ∥F.eval (newton_seq n)∥ / ∥F.derivative.eval a∥ : by rw newton_seq_deriv_norm ... ≤ ∥F.derivative.eval a∥^2 * T ^ (2^n) / ∥F.derivative.eval a∥ : (div_le_div_right deriv_norm_pos).2 (newton_seq_norm_le _) ... = ∥F.derivative.eval a∥ * T^(2^n) : div_sq_cancel (ne_of_gt deriv_norm_pos) _ include hnsol private lemma T_pos : T > 0 := begin rw T_def, exact div_pos_of_pos_of_pos (norm_pos_iff.2 hnsol) (deriv_sq_norm_pos hnorm) end private lemma newton_seq_succ_dist_weak (n : ℕ) : ∥newton_seq (n+2) - newton_seq (n+1)∥ < ∥F.eval a∥ / ∥F.derivative.eval a∥ := have 2 ≤ 2^(n+1), from have _, from pow_le_pow (by norm_num : 1 ≤ 2) (nat.le_add_left _ _ : 1 ≤ n + 1), by simpa using this, calc ∥newton_seq (n+2) - newton_seq (n+1)∥ ≤ ∥F.derivative.eval a∥ * T^(2^(n+1)) : newton_seq_succ_dist _ ... ≤ ∥F.derivative.eval a∥ * T^2 : mul_le_mul_of_nonneg_left (pow_le_pow_of_le_one (norm_nonneg _) (le_of_lt T_lt_one) this) (norm_nonneg _) ... < ∥F.derivative.eval a∥ * T^1 : mul_lt_mul_of_pos_left (pow_lt_pow_of_lt_one T_pos T_lt_one (by norm_num)) deriv_norm_pos ... = ∥F.eval a∥ / ∥F.derivative.eval a∥ : begin rw [T, _root_.pow_two, _root_.pow_one, normed_field.norm_div, ←mul_div_assoc, padic_norm_e.mul], apply mul_div_mul_left, apply deriv_norm_ne_zero; assumption end private lemma newton_seq_dist_aux (n : ℕ) : ∀ k : ℕ, ∥newton_seq (n + k) - newton_seq n∥ ≤ ∥F.derivative.eval a∥ * T^(2^n) | 0 := begin simp, apply mul_nonneg, {apply norm_nonneg}, {apply T_pow_nonneg} end | (k+1) := have 2^n ≤ 2^(n+k), by {rw [←nat.pow_eq_pow, ←nat.pow_eq_pow], apply pow_le_pow, norm_num, apply nat.le_add_right}, calc ∥newton_seq (n + (k + 1)) - newton_seq n∥ = ∥newton_seq ((n + k) + 1) - newton_seq n∥ : by rw add_assoc ... = ∥(newton_seq ((n + k) + 1) - newton_seq (n+k)) + (newton_seq (n+k) - newton_seq n)∥ : by rw ←sub_add_sub_cancel ... ≤ max (∥newton_seq ((n + k) + 1) - newton_seq (n+k)∥) (∥newton_seq (n+k) - newton_seq n∥) : padic_norm_z.nonarchimedean _ _ ... ≤ max (∥F.derivative.eval a∥ * T^(2^((n + k)))) (∥F.derivative.eval a∥ * T^(2^n)) : max_le_max (newton_seq_succ_dist _) (newton_seq_dist_aux _) ... = ∥F.derivative.eval a∥ * T^(2^n) : max_eq_right $ mul_le_mul_of_nonneg_left (pow_le_pow_of_le_one (norm_nonneg _) (le_of_lt T_lt_one) this) (norm_nonneg _) private lemma newton_seq_dist {n k : ℕ} (hnk : n ≤ k) : ∥newton_seq k - newton_seq n∥ ≤ ∥F.derivative.eval a∥ * T^(2^n) := have hex : ∃ m, k = n + m, from exists_eq_add_of_le hnk, let ⟨_, hex'⟩ := hex in by rw hex'; apply newton_seq_dist_aux; assumption private lemma newton_seq_dist_to_a : ∀ n : ℕ, 0 < n → ∥newton_seq n - a∥ = ∥F.eval a∥ / ∥F.derivative.eval a∥ | 1 h := by simp [sub_eq_add_neg, add_assoc, newton_seq, newton_seq_aux, ih_n]; apply normed_field.norm_div | (k+2) h := have hlt : ∥newton_seq (k+2) - newton_seq (k+1)∥ < ∥newton_seq (k+1) - a∥, by rw newton_seq_dist_to_a (k+1) (succ_pos _); apply newton_seq_succ_dist_weak; assumption, have hne' : ∥newton_seq (k + 2) - newton_seq (k+1)∥ ≠ ∥newton_seq (k+1) - a∥, from ne_of_lt hlt, calc ∥newton_seq (k + 2) - a∥ = ∥(newton_seq (k + 2) - newton_seq (k+1)) + (newton_seq (k+1) - a)∥ : by rw ←sub_add_sub_cancel ... = max (∥newton_seq (k + 2) - newton_seq (k+1)∥) (∥newton_seq (k+1) - a∥) : padic_norm_z.add_eq_max_of_ne hne' ... = ∥newton_seq (k+1) - a∥ : max_eq_right_of_lt hlt ... = ∥polynomial.eval a F∥ / ∥polynomial.eval a (polynomial.derivative F)∥ : newton_seq_dist_to_a (k+1) (succ_pos _) private lemma bound' : tendsto (λ n : ℕ, ∥F.derivative.eval a∥ * T^(2^n)) at_top (𝓝 0) := begin rw ←mul_zero (∥F.derivative.eval a∥), exact tendsto_const_nhds.mul (tendsto.comp (tendsto_pow_at_top_nhds_0_of_lt_1 (norm_nonneg _) (T_lt_one hnorm)) (tendsto_pow_at_top_at_top_of_gt_1_nat (by norm_num))) end private lemma bound : ∀ {ε}, ε > 0 → ∃ N : ℕ, ∀ {n}, n ≥ N → ∥F.derivative.eval a∥ * T^(2^n) < ε := have mtn : ∀ n : ℕ, ∥polynomial.eval a (polynomial.derivative F)∥ * T ^ (2 ^ n) ≥ 0, from λ n, mul_nonneg (norm_nonneg _) (T_pow_nonneg _), begin have := bound' hnorm hnsol, simp [tendsto, nhds] at this, intros ε hε, cases this (ball 0 ε) (mem_ball_self hε) (is_open_ball) with N hN, existsi N, intros n hn, simpa [normed_field.norm_mul, real.norm_eq_abs, abs_of_nonneg (mtn n)] using hN _ hn end private lemma bound'_sq : tendsto (λ n : ℕ, ∥F.derivative.eval a∥^2 * T^(2^n)) at_top (𝓝 0) := begin rw [←mul_zero (∥F.derivative.eval a∥), _root_.pow_two], simp only [mul_assoc], apply tendsto.mul, { apply tendsto_const_nhds }, { apply bound', assumption } end private theorem newton_seq_is_cauchy : is_cau_seq norm newton_seq := begin intros ε hε, cases bound hnorm hnsol hε with N hN, existsi N, intros j hj, apply lt_of_le_of_lt, { apply newton_seq_dist _ _ hj, assumption }, { apply hN, apply le_refl } end private def newton_cau_seq : cau_seq ℤ_[p] norm := ⟨_, newton_seq_is_cauchy⟩ private def soln : ℤ_[p] := newton_cau_seq.lim private lemma soln_spec {ε : ℝ} (hε : ε > 0) : ∃ (N : ℕ), ∀ {i : ℕ}, i ≥ N → ∥soln - newton_cau_seq i∥ < ε := setoid.symm (cau_seq.equiv_lim newton_cau_seq) _ hε private lemma soln_deriv_norm : ∥F.derivative.eval soln∥ = ∥F.derivative.eval a∥ := norm_deriv_eq newton_seq_deriv_norm private lemma newton_seq_norm_tendsto_zero : tendsto (λ i, ∥F.eval (newton_cau_seq i)∥) at_top (𝓝 0) := squeeze_zero (λ _, norm_nonneg _) newton_seq_norm_le bound'_sq private lemma newton_seq_dist_tendsto : tendsto (λ n, ∥newton_cau_seq n - a∥) at_top (𝓝 (∥F.eval a∥ / ∥F.derivative.eval a∥)) := tendsto.congr' (suffices ∃ k, ∀ n ≥ k, ∥F.eval a∥ / ∥F.derivative.eval a∥ = ∥newton_cau_seq n - a∥, by simpa, ⟨1, λ _ hx, (newton_seq_dist_to_a _ hx).symm⟩) (tendsto_const_nhds) private lemma newton_seq_dist_tendsto' : tendsto (λ n, ∥newton_cau_seq n - a∥) at_top (𝓝 ∥soln - a∥) := (continuous_norm.tendsto _).comp (newton_cau_seq.tendsto_limit.sub tendsto_const_nhds) private lemma soln_dist_to_a : ∥soln - a∥ = ∥F.eval a∥ / ∥F.derivative.eval a∥ := tendsto_nhds_unique at_top_ne_bot newton_seq_dist_tendsto' newton_seq_dist_tendsto private lemma soln_dist_to_a_lt_deriv : ∥soln - a∥ < ∥F.derivative.eval a∥ := begin rw soln_dist_to_a, apply div_lt_of_mul_lt_of_pos, { apply deriv_norm_pos; assumption }, { rwa _root_.pow_two at hnorm } end private lemma eval_soln : F.eval soln = 0 := limit_zero_of_norm_tendsto_zero newton_seq_norm_tendsto_zero private lemma soln_unique (z : ℤ_[p]) (hev : F.eval z = 0) (hnlt : ∥z - a∥ < ∥F.derivative.eval a∥) : z = soln := have soln_dist : ∥z - soln∥ < ∥F.derivative.eval a∥, from calc ∥z - soln∥ = ∥(z - a) + (a - soln)∥ : by rw sub_add_sub_cancel ... ≤ max (∥z - a∥) (∥a - soln∥) : padic_norm_z.nonarchimedean _ _ ... < ∥F.derivative.eval a∥ : max_lt hnlt (by rw norm_sub_rev; apply soln_dist_to_a_lt_deriv), let h := z - soln, ⟨q, hq⟩ := F.binom_expansion soln h in have (F.derivative.eval soln + q * h) * h = 0, from eq.symm (calc 0 = F.eval (soln + h) : by simp [hev, h] ... = F.derivative.eval soln * h + q * h^2 : by rw [hq, eval_soln, zero_add] ... = (F.derivative.eval soln + q * h) * h : by rw [_root_.pow_two, right_distrib, mul_assoc]), have h = 0, from by_contradiction $ λ hne, have F.derivative.eval soln + q * h = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right hne, have F.derivative.eval soln = (-q) * h, by simpa using eq_neg_of_add_eq_zero this, lt_irrefl ∥F.derivative.eval soln∥ (calc ∥F.derivative.eval soln∥ = ∥(-q) * h∥ : by rw this ... ≤ 1 * ∥h∥ : by rw [padic_norm_z.mul]; exact mul_le_mul_of_nonneg_right (padic_norm_z.le_one _) (norm_nonneg _) ... = ∥z - soln∥ : by simp [h] ... < ∥F.derivative.eval soln∥ : by rw soln_deriv_norm; apply soln_dist), eq_of_sub_eq_zero (by rw ←this; refl) end hensel variables {p : ℕ} [fact p.prime] {F : polynomial ℤ_[p]} {a : ℤ_[p]} private lemma a_soln_is_unique (ha : F.eval a = 0) (z' : ℤ_[p]) (hz' : F.eval z' = 0) (hnormz' : ∥z' - a∥ < ∥F.derivative.eval a∥) : z' = a := let h := z' - a, ⟨q, hq⟩ := F.binom_expansion a h in have (F.derivative.eval a + q * h) * h = 0, from eq.symm (calc 0 = F.eval (a + h) : show 0 = F.eval (a + (z' - a)), by rw add_comm; simp [hz'] ... = F.derivative.eval a * h + q * h^2 : by rw [hq, ha, zero_add] ... = (F.derivative.eval a + q * h) * h : by rw [_root_.pow_two, right_distrib, mul_assoc]), have h = 0, from by_contradiction $ λ hne, have F.derivative.eval a + q * h = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right hne, have F.derivative.eval a = (-q) * h, by simpa using eq_neg_of_add_eq_zero this, lt_irrefl ∥F.derivative.eval a∥ (calc ∥F.derivative.eval a∥ = ∥q∥*∥h∥ : by simp [this] ... ≤ 1*∥h∥ : mul_le_mul_of_nonneg_right (padic_norm_z.le_one _) (norm_nonneg _) ... < ∥F.derivative.eval a∥ : by simpa [h]), eq_of_sub_eq_zero (by rw ←this; refl) variable (hnorm : ∥F.eval a∥ < ∥F.derivative.eval a∥^2) include hnorm private lemma a_is_soln (ha : F.eval a = 0) : F.eval a = 0 ∧ ∥a - a∥ < ∥F.derivative.eval a∥ ∧ ∥F.derivative.eval a∥ = ∥F.derivative.eval a∥ ∧ ∀ z', F.eval z' = 0 → ∥z' - a∥ < ∥F.derivative.eval a∥ → z' = a := ⟨ha, by simp; apply deriv_norm_pos; apply hnorm, rfl, a_soln_is_unique ha⟩ lemma hensels_lemma : ∃ z : ℤ_[p], F.eval z = 0 ∧ ∥z - a∥ < ∥F.derivative.eval a∥ ∧ ∥F.derivative.eval z∥ = ∥F.derivative.eval a∥ ∧ ∀ z', F.eval z' = 0 → ∥z' - a∥ < ∥F.derivative.eval a∥ → z' = z := if ha : F.eval a = 0 then ⟨a, a_is_soln hnorm ha⟩ else by refine ⟨soln _ _, eval_soln _ _, soln_dist_to_a_lt_deriv _ _, soln_deriv_norm _ _, soln_unique _ _⟩; assumption
bb835126d1a7ea72a2318025d5e1d9724d2f655f
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/valuation/extend_to_localization.lean
03c7c8674849dec391d6c729d9e20708a872d33c
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
1,953
lean
/- Copyright (c) 2022 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import ring_theory.localization.at_prime import ring_theory.valuation.basic /-! # Extending valuations to a localization We show that, given a valuation `v` taking values in a linearly ordered commutative *group* with zero `Γ`, and a submonoid `S` of `v.supp.prime_compl`, the valuation `v` can be naturally extended to the localization `S⁻¹A`. -/ variables {A : Type*} [comm_ring A] {Γ : Type*} [linear_ordered_comm_group_with_zero Γ] (v : valuation A Γ) {S : submonoid A} (hS : S ≤ v.supp.prime_compl) (B : Type*) [comm_ring B] [algebra A B] [is_localization S B] /-- We can extend a valuation `v` on a ring to a localization at a submonoid of the complement of `v.supp`. -/ noncomputable def valuation.extend_to_localization : valuation B Γ := let f := is_localization.to_localization_map S B, h : ∀ s : S, is_unit (v.1.to_monoid_hom s) := λ s, is_unit_iff_ne_zero.2 (hS s.2) in { map_zero' := by convert f.lift_eq _ 0; simp, map_add_le_max' := λ x y, begin obtain ⟨a,b,s,rfl,rfl⟩ : ∃ (a b : A) (s : S), f.mk' a s = x ∧ f.mk' b s = y, { obtain ⟨a,s,rfl⟩ := f.mk'_surjective x, obtain ⟨b,t,rfl⟩ := f.mk'_surjective y, use [a * t, b * s, s * t], split; { rw [f.mk'_eq_iff_eq, submonoid.coe_mul], ring_nf } }, convert_to f.lift h (f.mk' (a+b) s) ≤ max (f.lift h _) (f.lift h _), { refine congr_arg (f.lift h) (is_localization.eq_mk'_iff_mul_eq.2 _), rw [add_mul, map_add], iterate 2 { erw is_localization.mk'_spec } }, iterate 3 { rw f.lift_mk' }, rw max_mul_mul_right, apply mul_le_mul_right' (v.map_add a b), end, ..f.lift h } @[simp] lemma valuation.extend_to_localization_apply_map_apply (a : A) : v.extend_to_localization hS B (algebra_map A B a) = v a := submonoid.localization_map.lift_eq _ _ a
15842750e811ba1ced32f0e51b21d19646710980
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/repr_issue.lean
6dc8997c897e63e787b5d1bb3c39b8cff4ab4163
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
638
lean
new_frontend def foo {m} [Monad m] [MonadExcept String m] [MonadState (Array Nat) m] : m Nat := «catch» (do modify $ fun (a : Array Nat) => a.set! 0 33; throw "error") (fun _ => do let a ← get; pure $ a.get! 0) def ex₁ : StateT (Array Nat) (ExceptT String Id) Nat := foo def ex₂ : ExceptT String (StateT (Array Nat) Id) Nat := foo -- The following examples were producing an element of Type `id (Except String Nat)`. -- Type class resolution was failing to produce an instance for `HasRepr (id (Except String Nat))` because `id` is not transparent. #eval run ex₁ (mkArray 10 1000) #eval run ex₂ (mkArray 10 1000)
4a46daa1d39f92c10db83a99bd683b660e64796b
98beff2e97d91a54bdcee52f922c4e1866a6c9b9
/src/power.lean
b2015633c12e75a72d6d3512d201b4a8b468ed8b
[]
no_license
b-mehta/topos
c3fc43fb04ba16bae1965ce5c26c6461172e5bc6
c9032b11789e36038bc841a1e2b486972421b983
refs/heads/master
1,629,609,492,867
1,609,907,263,000
1,609,907,263,000
240,943,034
43
3
null
1,598,210,062,000
1,581,877,668,000
Lean
UTF-8
Lean
false
false
56,952
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 import category_theory.limits.shapes.binary_products import category_theory.limits.shapes.finite_products import category_theory.limits.shapes.finite_limits import category_theory.limits.types import category_theory.monad.monadicity import category_theory.adjunction.limits import category_theory.adjunction.lifting import category_theory.monad.limits import category_theory.limits.opposites import category_theory.limits.over import category_theory.epi_mono import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.constructions.limits_of_products_and_equalizers import category_theory.limits.preserves.shapes.binary_products import category_theory.limits.preserves.shapes.equalizers import locally_cartesian_closed import subobject_classifier /-! # Power objects Define power objects. Show that power objects induce a (contravariant) functor `P_functor`. Show that this is self-adjoint on the right. Define the singleton arrow {} : B ⟶ PB and internal image (for monos only) and show the latter is functorial too. Show the existence of a subobject classifier given power objects and show -/ universes v u v₂ u₂ noncomputable theory namespace category_theory open category_theory category limits attribute [instance] has_finite_wide_pullbacks_of_has_finite_limits variables {C : Type u} [category.{v} C] variables [has_finite_limits.{v} C] abbreviation powerises {A PA niA B R : C} (memA : niA ⟶ PA ⨯ A) (m : R ⟶ B ⨯ A) (mhat : B ⟶ PA) := has_pullback_top m (limits.prod.map mhat (𝟙 A)) memA instance subsingleton_powerises {A PA niA B R : C} (memA : niA ⟶ PA ⨯ A) [mono memA] (m : R ⟶ B ⨯ A) (mhat : B ⟶ PA) : subsingleton (powerises memA m mhat) := ⟨by { intros P Q, cases P, cases Q, congr, rw [← cancel_mono memA, P_comm, Q_comm] }⟩ structure is_power_object {A PA niA : C} (memA : niA ⟶ PA ⨯ A) := (hat : ∀ {B R} (m : R ⟶ B ⨯ A) [mono.{v} m], B ⟶ PA) (powerises' : ∀ {B R} (m : R ⟶ B ⨯ A) [hm : mono m], powerises memA m (hat m)) (uniquely' : ∀ {B R} {m : R ⟶ B ⨯ A} [hm : mono m] {hat' : B ⟶ PA}, powerises memA m hat' → hat m = hat') class has_power_object (A : C) := (PA niA : C) (memA : niA ⟶ PA ⨯ A) [is_mono : mono memA] (is_power : is_power_object memA) attribute [instance] has_power_object.is_mono variable (C) class has_power_objects := (has_power_object : Π (A : C), has_power_object.{v} A) variable {C} attribute [instance, priority 990] has_power_objects.has_power_object attribute [simp] pullback.condition section convenience variables (A : C) [has_power_object.{v} A] def P : C := has_power_object.PA.{v} A def ni : C := has_power_object.niA.{v} A def mem : ni A ⟶ P A ⨯ A := has_power_object.memA def power_is_power : is_power_object (mem A) := has_power_object.is_power instance mono_mem : mono (mem A) := has_power_object.is_mono def mem_sub : sub (P A ⨯ A) := sub.mk' (mem A) def mem_subq : subq (P A ⨯ A) := ⟦mem_sub A⟧ variables {A} {B R : C} (m : R ⟶ B ⨯ A) [mono m] def hat : B ⟶ P A := (power_is_power A).hat m def hat_powerises : powerises (mem A) m (hat m) := (power_is_power A).powerises' m variable {m} lemma unique_hat {hat' : B ⟶ P A} (hp : powerises (mem A) m hat') : hat m = hat' := (power_is_power A).uniquely' hp end convenience lemma P_unique_aux {A : C} {PA₁ niA₁ PA₂ niA₂ : C} (memA₁ : niA₁ ⟶ PA₁ ⨯ A) (memA₂ : niA₂ ⟶ PA₂ ⨯ A) [mono memA₁] [mono memA₂] (h₁ : is_power_object memA₁) (h₂ : is_power_object memA₂) : h₁.hat memA₂ ≫ h₂.hat memA₁ = 𝟙 PA₂ := begin have: h₂.hat memA₂ = 𝟙 _, { apply h₂.uniquely', change has_pullback_top _ _ _, rw prod.map_id_id, apply top_iso_has_pullback_top (𝟙 _), rw [id_comp, comp_id] }, rw ← this, symmetry, apply h₂.uniquely', change has_pullback_top _ _ _, rw prod.map_comp_id, apply left_right_hpb_to_both_hpb _ (h₁.powerises' memA₂) (h₂.powerises' memA₁), end def P_unique_up_to_iso {A : C} {PA₁ niA₁ PA₂ niA₂ : C} {memA₁ : niA₁ ⟶ PA₁ ⨯ A} {memA₂ : niA₂ ⟶ PA₂ ⨯ A} [mono memA₁] [mono memA₂] (h₁ : is_power_object memA₁) (h₂ : is_power_object memA₂) : PA₁ ≅ PA₂ := { hom := h₂.hat memA₁, inv := h₁.hat memA₂, hom_inv_id' := P_unique_aux memA₂ memA₁ h₂ h₁, inv_hom_id' := P_unique_aux memA₁ memA₂ h₁ h₂ } variables {A B : C} [has_power_object.{v} A] lemma hat_lift_of_is_iso {B R₁ R₂ : C} {g₁ : R₁ ⟶ B ⨯ A} {g₂ : R₂ ⟶ B ⨯ A} [mono g₁] [mono g₂] (hom : R₁ ⟶ R₂) [is_iso hom] (k : hom ≫ g₂ = g₁) : hat g₁ = hat g₂ := begin apply unique_hat, change has_pullback_top _ _ _, rw [← id_comp (limits.prod.map _ _)], refine left_right_hpb_to_both_hpb g₂ (top_iso_has_pullback_top hom _ _ _ (by simp [k])) (hat_powerises g₂), end lemma hat_lift_of_iso {B R₁ R₂ : C} {g₁ : R₁ ⟶ B ⨯ A} {g₂ : R₂ ⟶ B ⨯ A} [mono g₁] [mono g₂] (h : R₁ ≅ R₂) (k : h.hom ≫ g₂ = g₁) : hat g₁ = hat g₂ := hat_lift_of_is_iso h.hom k -- We need to assume g₁ = hom ≫ g₂. From here if we know that hom,inv cancel then we get g₂ = inv ≫ g₁. -- Instead we assume this and derive that hom,inv cancel lemma lifting {A B R₁ R₂ : C} [has_power_object.{v} A] {g₁ : R₁ ⟶ B ⨯ A} {g₂ : R₂ ⟶ B ⨯ A} [mono g₁] [mono g₂] (hom : R₁ ⟶ R₂) (inv : R₂ ⟶ R₁) : hom ≫ g₂ = g₁ → inv ≫ g₁ = g₂ → hat g₁ = hat g₂ := begin intros k l, apply hat_lift_of_iso ⟨hom, inv, _, _⟩ k; simp [← cancel_mono g₁, ← cancel_mono g₂, l, k], end lemma liftable {B : C} (a b : sub (B ⨯ A)) (i : a ≈ b) : hat a.arrow = hat b.arrow := nonempty.elim i (λ i, lifting _ _ (sub.w i.hom) (sub.w i.inv)) def get_named_object {B : C} (k : B ⟶ P A) : C := pullback (mem A) (limits.prod.map k (𝟙 _)) def get_named_arrow {B : C} (k : B ⟶ P A) : get_named_object k ⟶ B ⨯ A := pullback.snd instance get_named_mono {B : C} (k : B ⟶ P A) : mono (get_named_arrow k) := pullback.snd_of_mono lemma hat_get_named_arrow {B : C} (k : B ⟶ P A) : hat (get_named_arrow k) = k := unique_hat has_pullback_top_of_pb def hat_natural_left {B B' R : C} (k : R ⟶ B ⨯ A) [mono k] (g : B' ⟶ B) : hat (pullback.snd : pullback k (limits.prod.map g (𝟙 A)) ⟶ B' ⨯ A) = g ≫ hat k := begin apply unique_hat, change has_pullback_top _ _ _, rw prod.map_comp_id, apply left_right_hpb_to_both_hpb _ has_pullback_top_of_pb (hat_powerises k), end @[simps] def name_bijection {A B : C} [has_power_object.{v} A] : (B ⟶ P A) ≃ subq (B ⨯ A) := { to_fun := λ k, ⟦sub.mk' (get_named_arrow k)⟧, inv_fun := quotient.lift (λ (f : sub (B ⨯ A)), hat f.arrow) liftable, left_inv := hat_get_named_arrow, right_inv := quotient.ind begin intro g, apply quotient.sound, exact equiv_of_both_ways (sub.hom_mk _ ((hat_powerises g.arrow).is_pb.fac _ walking_cospan.right)) (sub.hom_mk _ (pullback.lift_snd _ _ (hat_powerises g.arrow).comm)), end } abbreviation name_subobject {B : C} : subq (B ⨯ A) → (B ⟶ P A) := name_bijection.symm lemma get_named_subobject_eq_pullback_mem {B : C} (k : B ⟶ P A) : name_bijection k = (subq.pullback (limits.prod.map k (𝟙 _))).obj (mem_subq A) := rfl def get_named_subobject_natural_left {B B' : C} (k : B ⟶ P A) (g : B' ⟶ B) : name_bijection (g ≫ k) = (subq.pullback (limits.prod.map g (𝟙 A))).obj (name_bijection k) := by { rw [get_named_subobject_eq_pullback_mem, prod.map_comp_id, subq.pullback_comp], refl } lemma name_pullback {B' : C} (g : subq (B ⨯ A)) (f : B' ⟶ B) : name_subobject ((subq.pullback (limits.prod.map f (𝟙 _))).obj g) = f ≫ name_subobject g := quotient.induction_on g (λ a, hat_natural_left a.arrow _) lemma pullback_along_hat_eq_self {R : C} (m : R ⟶ B ⨯ A) [mono m] : (subq.pullback (limits.prod.map (hat m) (𝟙 A))).obj (mem_subq A) = ⟦sub.mk' m⟧ := begin rw ← get_named_subobject_eq_pullback_mem, erw name_bijection.apply_eq_iff_eq_symm_apply, refl end section functor_setup variables (f : A ⟶ B) [has_power_object.{v} B] def E : C := pullback (mem B) (limits.prod.map (𝟙 _) f) def Emap : E f ⟶ P B ⨯ A := pullback.snd instance Emap_mono : mono (Emap f) := pullback.snd_of_mono def Esubq : subq (P B ⨯ A) := (subq.pullback (limits.prod.map (𝟙 _) f)).obj (mem_subq B) lemma Esquare : (pullback.fst : E f ⟶ _) ≫ mem B = Emap f ≫ limits.prod.map (𝟙 _) f := pullback.condition lemma Epb : is_limit (pullback_cone.mk _ _ (Esquare f)) := cone_is_pullback _ _ variable [has_power_object.{v} A] def P_map : P B ⟶ P A := name_subobject (Esubq f) lemma hat_natural_right {D R : C} (m : R ⟶ D ⨯ B) [hm : mono m] : hat (pullback.snd : pullback m (limits.prod.map (𝟙 D) f) ⟶ D ⨯ A) = hat m ≫ P_map f := begin apply unique_hat, change has_pullback_top _ _ _, rw prod.map_comp_id, apply left_right_hpb_to_both_hpb _ _ (hat_powerises _), apply right_both_hpb_to_left_hpb _ _ _ has_pullback_top_of_pb, rw ← prod.map_swap, apply left_right_hpb_to_both_hpb m has_pullback_top_of_pb (hat_powerises _), end lemma name_other_pullback {D : C} : ∀ m, name_subobject ((subq.pullback (limits.prod.map (𝟙 D) f)).obj m) = name_subobject m ≫ P_map f := quotient.ind (by { intro a, apply hat_natural_right }) @[simp] lemma lift'_right {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {t : pullback_cone f g} (ht : is_limit t) {W : C} (h : W ⟶ X) (k : W ⟶ Y) (w : h ≫ f = k ≫ g) : (pullback_cone.is_limit.lift' ht h k w).val ≫ t.snd = k := (pullback_cone.is_limit.lift' ht h k w).2.2 def how_inj_is_hat {B R₁ R₂ : C} {f₁ : R₁ ⟶ B ⨯ A} {f₂ : R₂ ⟶ B ⨯ A} [mono f₁] [mono f₂] (h : hat f₁ = hat f₂) : R₁ ≅ R₂ := { hom := (pullback_cone.is_limit.lift' (hat_powerises f₂).is_pb (hat_powerises f₁).top f₁ (h ▸ (hat_powerises f₁).comm)).1, inv := (pullback_cone.is_limit.lift' (hat_powerises f₁).is_pb (hat_powerises f₂).top f₂ (h.symm ▸ (hat_powerises f₂).comm)).1, hom_inv_id' := by erw [← cancel_mono_id f₁, assoc, lift'_right, lift'_right], inv_hom_id' := by erw [← cancel_mono_id f₂, assoc, lift'_right, lift'_right] } lemma very_inj {B R₁ R₂ : C} {f₁ : R₁ ⟶ B ⨯ A} {f₂ : R₂ ⟶ B ⨯ A} [mono f₁] [mono f₂] (h : hat f₁ = hat f₂) : (how_inj_is_hat h).hom ≫ f₂ = f₁ := lift'_right _ _ _ _ lemma P_map_id (X : C) [has_power_object.{v} X] : P_map (𝟙 X) = 𝟙 (P X) := hat_get_named_arrow _ lemma P_map_comp {X Y Z : C} [has_power_object.{v} X] [has_power_object.{v} Y] [has_power_object.{v} Z] (f : X ⟶ Y) (g : Y ⟶ Z) : P_map (f ≫ g) = P_map g ≫ P_map f := by { erw [← name_other_pullback, Esubq, ← subq.pullback_comp, ← prod.map_id_comp], refl } @[simps] def P_functor [has_power_objects.{v} C] : Cᵒᵖ ⥤ C := { obj := λ X, P X.unop, map := λ X Y f, P_map f.unop, map_id' := λ X, P_map_id _, map_comp' := λ X Y Z f g, P_map_comp _ _ } end functor_setup instance self_adj [has_power_objects.{v} C] : is_right_adjoint (P_functor : Cᵒᵖ ⥤ C) := { left := P_functor.right_op, adj := adjunction.mk_of_hom_equiv { hom_equiv := λ A B, begin apply equiv.trans (op_equiv (opposite.op (P A)) B), apply equiv.trans name_bijection, apply equiv.trans _ name_bijection.symm, apply postcompose_sub_equiv_of_iso (limits.prod.braiding _ _), end, hom_equiv_naturality_left_symm' := λ X' X Y f g, begin rw ← has_hom.hom.unop_inj.eq_iff, change name_subobject ((subq.post _).obj (name_bijection (f ≫ g))) = name_subobject ((subq.post _).obj (name_bijection g)) ≫ P_map f, rw [← name_other_pullback, get_named_subobject_natural_left], congr' 1, exact postcompose_pullback_comm _ (pullback_square_iso _ _ _ _ (braid_natural _ _)) _, end, hom_equiv_naturality_right' := λ X Y Y' f g, begin change name_subobject ((subq.post _).obj (name_bijection (g.unop ≫ f.unop))) = name_subobject ((subq.post _).obj (name_bijection f.unop)) ≫ P_map g.unop, rw [← name_other_pullback, get_named_subobject_natural_left], congr' 1, exact postcompose_pullback_comm _ (pullback_square_iso _ _ _ _ (braid_natural _ _)) _, end } } def diagonal (A : C) : A ⟶ A ⨯ A := limits.prod.lift (𝟙 A) (𝟙 A) instance mono_diagonal (A : C) : mono.{v} (diagonal A) := category_theory.mono_prod_lift_of_left _ _ def diagonal_sub (A : C) : sub (A ⨯ A) := sub.mk' (diagonal A) def diagonal_subq (A : C) : subq (A ⨯ A) := ⟦diagonal_sub A⟧ -- @[reducible] def singleton_arrow (A : C) [has_power_object.{v} A] : A ⟶ P A := hat (diagonal A) lemma seven_six_one {A B : C} [has_power_object.{v} B] (f : A ⟶ B) : hat (limits.prod.lift (𝟙 A) f) = f ≫ singleton_arrow B := begin rw [singleton_arrow, ← hat_natural_left], apply lifting (pullback.lift f _ _) (pullback.snd ≫ limits.prod.fst) (pullback.lift_snd _ _ _) _, { rw [prod.lift_map, diagonal], apply prod.hom_ext; simp }, { apply prod.hom_ext, { simp only [prod.lift_fst, assoc, comp_id] }, { rw [assoc, prod.lift_snd, assoc, ← limits.prod.map_fst f (𝟙 _), ← comp_id limits.prod.snd, ← limits.prod.map_snd f _, ← pullback.condition_assoc, ← pullback.condition_assoc, diagonal], simp } } end lemma seven_six_two {A B : C} [has_power_object.{v} A] [has_power_object.{v} B] (f : A ⟶ B) : hat (limits.prod.lift f (𝟙 A)) = singleton_arrow B ≫ P_map f := begin rw [singleton_arrow, ← hat_natural_right], apply lifting (pullback.lift f _ _) (pullback.snd ≫ limits.prod.snd) (pullback.lift_snd _ _ _) _, { rw [prod.lift_map, diagonal], apply prod.hom_ext; simp }, { apply prod.hom_ext, { rw [assoc, prod.lift_fst, assoc, ← limits.prod.map_snd (𝟙 _) f, ← pullback.condition_assoc, ← comp_id limits.prod.fst, ← limits.prod.map_fst (𝟙 _) f, ← pullback.condition_assoc, diagonal], simp }, { simp only [assoc, comp_id, prod.lift_snd] } }, end instance singleton_mono (A : C) [has_power_object.{v} A] : mono (singleton_arrow A) := ⟨λ Z g h w, begin rw [← seven_six_one, ← seven_six_one] at w, have q := very_inj w =≫ limits.prod.fst, have r := very_inj w =≫ limits.prod.snd, simp only [prod.lift_fst, assoc, comp_id] at q, simpa [q] using r.symm, end⟩ lemma p_faithful {A B : C} [has_power_object.{v} A] [has_power_object.{v} B] {f g : A ⟶ B} (k : P_map f = P_map g) : f = g := begin have w := singleton_arrow _ ≫= k, rw [← seven_six_two, ← seven_six_two] at w, have q := very_inj w =≫ limits.prod.fst, have r := very_inj w =≫ limits.prod.snd, simp only [prod.lift_snd, assoc, comp_id] at r, simpa [r] using q.symm, end instance pfaithful [has_power_objects.{v} C] : faithful (P_functor : Cᵒᵖ ⥤ C) := ⟨λ A B f g k, has_hom.hom.unop_inj (p_faithful k)⟩ def internal_image {A B : C} [has_power_object.{v} A] [has_power_object.{v} B] (f : A ⟶ B) [mono f] : P A ⟶ P B := hat (mem A ≫ limits.prod.map (𝟙 (P A)) f) -- TODO: this doesn't use pasting so it's super long. can we make it nicer by using pasting? -- TODO: if not, it's still a horribly long proof which desperately needs a cleanup lemma naturalish {A B : C} [has_power_object.{v} A] [has_power_object.{v} B] (f : A ⟶ B) [mono f] {R D : C} (m : R ⟶ D ⨯ A) [mono m] : hat m ≫ internal_image f = hat (m ≫ limits.prod.map (𝟙 D) f) := begin have comm : limits.prod.map (hat m) (𝟙 _) ≫ limits.prod.map (𝟙 _) f = limits.prod.map (𝟙 _) f ≫ limits.prod.map (hat m) (𝟙 _), rw prod.map_swap, change hat m ≫ name_bijection.symm ((subq.post (limits.prod.map _ _)).obj (mem_subq A)) = name_bijection.symm ((subq.post _).obj ⟦sub.mk' m⟧), rw [← name_pullback, ← postcompose_pullback_comm comm _, pullback_along_hat_eq_self], refl, refine is_limit.mk''' _ _ _, exact (category_theory.mono_prod_map (𝟙 D) f), intro s, refine ⟨_, _⟩, apply prod.lift (s.snd ≫ limits.prod.fst) (s.fst ≫ limits.prod.snd), erw [prod.lift_map, comp_id, assoc, ← limits.prod.map_snd (𝟙 _), s.condition_assoc, limits.prod.map_snd, comp_id], apply prod.hom_ext; simp, end lemma internal_image_map_comp {X Y Z : C} [has_power_object.{v} X] [has_power_object.{v} Y] [has_power_object.{v} Z] (f : X ⟶ Y) (g : Y ⟶ Z) [mono f] [mono g] : internal_image (f ≫ g) = internal_image f ≫ internal_image g := begin erw [naturalish, internal_image], congr' 1, rw [assoc, prod.map_id_comp], end lemma internal_image_map_id {X : C} [has_power_object.{v} X] : internal_image (𝟙 X) = 𝟙 (P X) := begin change name_subobject ((subq.post (limits.prod.map _ _)).obj (mem_subq _)) = _, rw [name_bijection.symm_apply_eq, get_named_subobject_eq_pullback_mem], conv { for (limits.prod.map _ _) [1, 2] { rw prod.map_id_id } }, rw [subq.post_id, subq.pullback_id], end theorem beck_chevalley {A B C' D : C} [has_power_object.{v} A] [has_power_object.{v} B] [has_power_object.{v} C'] [has_power_object.{v} D] {h : D ⟶ A} {f : A ⟶ C'} {k : D ⟶ B} {g : B ⟶ C'} (comm : h ≫ f = k ≫ g) [mono f] [mono k] (t : is_limit (pullback_cone.mk h k comm)) : internal_image f ≫ P_map g = P_map h ≫ internal_image k := begin erw [← hat_natural_right, naturalish], change name_subobject ((subq.pullback _).obj ((subq.post (limits.prod.map _ _)).obj (mem_subq A))) = name_subobject ((subq.post (limits.prod.map _ _)).obj ((subq.pullback _).obj (mem_subq A))), rw equiv.apply_eq_iff_eq, symmetry, apply postcompose_pullback_comm _ _, rw [← prod.map_id_comp, comm, prod.map_id_comp], haveI : preserves_limits_of_shape walking_cospan _ := prod_preserves_connected_limits (P A), apply preserves_pullback_cone (prod.functor.obj (P A)) _ _ _ _ comm t, end variable (C) def weak_topos_has_subobj [has_power_object.{v} (⊤_ C)] : has_subobject_classifier.{v} C := { Ω := P (⊤_ C), Ω₀ := ni (⊤_ C), truth := mem (⊤_ C) ≫ (prod.right_unitor _).hom, is_subobj_classifier := { classifier_of := λ U X f hf, by exactI hat (f ≫ (prod.right_unitor _).inv), classifies' := λ U X f hf, by exactI begin change has_pullback_top _ _ _, conv {congr, rw [← comp_id f, ← (prod.right_unitor X).inv_hom_id, ← assoc] }, apply stretch_hpb_down _ _ _ limits.prod.fst _ _ (hat_powerises _) (limits.prod.map_fst _ _), apply pullback_flip (pullback_prod _ _), end, uniquely' := λ U X f hf χ₁ k, begin apply unique_hat, apply cut_hpb_up _ _ _ (prod.right_unitor _).hom (prod.right_unitor _).hom _ _ _ (pullback_flip (pullback_prod _ _)), { apply_instance }, { rw [assoc, (prod.right_unitor X).inv_hom_id, comp_id], exact k }, { apply limits.prod.map_fst } end } } variable {C} instance p_reflects_iso [has_power_objects.{v} C] : reflects_isomorphisms (P_functor : Cᵒᵖ ⥤ C) := { reflects := λ A B f i, by exactI begin suffices : is_iso f.unop, resetI, refine ⟨this.inv.op, has_hom.hom.unop_inj (is_iso.inv_hom_id f.unop), has_hom.hom.unop_inj (is_iso.hom_inv_id f.unop)⟩, haveI : has_subobject_classifier.{v} C := weak_topos_has_subobj _, haveI := reflects_isos _ (P_functor.right_op : C ⥤ _), haveI : is_iso (P_functor.right_op.map f.unop) := ⟨i.inv.op, has_hom.hom.unop_inj (is_iso.inv_hom_id _), has_hom.hom.unop_inj (is_iso.hom_inv_id _)⟩, refine is_iso_of_reflects_iso f.unop P_functor.right_op, end } def exists_power {A B : C} [has_power_object.{v} A] [has_power_object.{v} B] (f : A ⟶ B) [mono f] : internal_image f ≫ P_map f = 𝟙 (P A) := by rw [beck_chevalley _ (pullback_of_mono f), P_map_id, internal_image_map_id, comp_id] instance fin_category_op (J : Type v) [small_category J] [fcj : fin_category J] : fin_category Jᵒᵖ := { decidable_eq_obj := λ x y, decidable_of_decidable_of_iff infer_instance opposite.unop_injective.eq_iff, fintype_obj := { elems := finset.map ⟨opposite.op, opposite.op_injective⟩ _, complete := λ x, finset.mem_map_of_mem _ (fintype.complete x.unop) }, decidable_eq_hom := λ x y f g, decidable_of_decidable_of_iff infer_instance has_hom.hom.unop_inj.eq_iff, fintype_hom := λ X Y, { elems := (@fin_category.fintype_hom J _ fcj Y.unop X.unop).elems.map ⟨has_hom.hom.op, has_hom.hom.op_inj⟩, complete := λ f, finset.mem_map_of_mem _ (fintype.complete f.unop) } } local attribute [instance] has_colimits_of_shape_op_of_has_limits_of_shape instance pare [has_power_objects.{v} C] : monadic_right_adjoint (P_functor : Cᵒᵖ ⥤ C) := begin apply monad.monadic_of_has_preserves_reflexive_coequalizers_of_reflects_isomorphisms _, { apply_instance }, { apply_instance }, { apply_instance }, { introsI B' A' f' g' i, apply preserves_colimit_of_preserves_colimit_cocone (coequalizer_is_coequalizer f' g'), apply (is_colimit_map_cocone_cofork_equiv _ _).symm _, let e' := coequalizer.π f' g', let e : (coequalizer f' g').unop ⟶ A'.unop := e'.unop, haveI : split_mono g'.unop := ⟨(common_section f' g').unop, by { simp [←unop_comp] }⟩, change is_colimit (cofork.of_π (P_map e) _), have : is_split_coequalizer (P_functor.map f') (P_functor.map g') (P_functor.map e'), { refine ⟨internal_image e, internal_image g'.unop, _, exists_power _, exists_power _, _⟩, { rw [← functor.map_comp, coequalizer.condition, functor.map_comp] }, { apply beck_chevalley _ _, { rw [← unop_comp, ← coequalizer.condition, unop_comp] }, { refine is_limit.mk''' _ _ (λ s, _), { dsimp, apply_instance }, { have equal_legs : s.fst = s.snd, { simpa [← unop_comp] using s.condition =≫ (common_section f' g').unop }, refine ⟨(coequalizer.desc s.fst.op _).unop, _⟩, { rw [← has_hom.hom.unop_inj.eq_iff, unop_comp, unop_comp, has_hom.hom.unop_op, s.condition, equal_legs] }, { change (e' ≫ _).unop = _, rw coequalizer.π_desc, apply equal_legs } } } } }, apply this.is_coequalizer } end def some_colims (J : Type v) [small_category J] [has_power_objects.{v} C] [has_limits_of_shape Jᵒᵖ C] : has_colimits_of_shape J C := { has_colimit := λ F, by exactI begin suffices: has_colimit (F ⋙ (op_op_equivalence _).inverse), { resetI, apply adjunction.has_colimit_of_comp_equivalence F (op_op_equivalence _).inverse }, let F'' : Jᵒᵖ ⥤ Cᵒᵖ := (F ⋙ (op_op_equivalence _).inverse).left_op, suffices : has_limit F'', { resetI, apply limits.has_colimit_of_has_limit_left_op }, haveI := monadic_creates_limits (P_functor : Cᵒᵖ ⥤ C), suffices : has_limit (F'' ⋙ P_functor), refine has_limit_of_created _ P_functor, apply_instance end } namespace intersect variables {A} [has_power_object.{v} A] def intersect_names {B : C} (m n : B ⟶ P A) : B ⟶ P A := name_subobject $ name_bijection m ⊓ name_bijection n def intersect_names_natural {B B' : C} (f : B' ⟶ B) (m n : B ⟶ P A) : f ≫ intersect_names m n = intersect_names (f ≫ m) (f ≫ n) := begin dunfold intersect_names, rw [get_named_subobject_natural_left, get_named_subobject_natural_left, ← inf_pullback, name_bijection.eq_symm_apply, get_named_subobject_natural_left, name_bijection.apply_symm_apply], end def intersect (A : C) [has_power_object.{v} A] : P A ⨯ P A ⟶ P A := intersect_names limits.prod.fst limits.prod.snd end intersect @[priority 10000] instance [has_finite_limits.{v} C] {B : C} : has_finite_limits.{v} (over B) := begin haveI := has_finite_wide_pullbacks_of_has_finite_limits C, apply over.has_finite_limits, end def P₁_obj (A : C) [has_power_object.{v} A] : C := equalizer (intersect.intersect A) limits.prod.fst def P₁_arrow (A : C) [has_power_object.{v} A] : P₁_obj A ⟶ P A ⨯ P A := equalizer.ι (intersect.intersect A) limits.prod.fst instance P₁_arrow_mono (A : C) [has_power_object.{v} A] : mono (P₁_arrow A) := equalizer.ι_mono def P₁_sub (A : C) [has_power_object.{v} A] : subq (P A ⨯ P A) := ⟦sub.mk' (P₁_arrow A)⟧ lemma leq_prop' (A B : C) (m n : subq (B ⨯ A)) [has_power_object.{v} A] : m ≤ n ↔ limits.prod.lift (name_subobject m) (name_subobject n) ≫ intersect.intersect A = limits.prod.lift (name_subobject m) (name_subobject n) ≫ limits.prod.fst := begin rw [← inf_eq_left, intersect.intersect, intersect.intersect_names_natural, prod.lift_fst, prod.lift_snd, intersect.intersect_names, name_bijection.eq_symm_apply], simp only [name_bijection.apply_symm_apply], end lemma leq_prop (A B R₁ R₂ : C) [has_power_object.{v} A] (m : R₁ ⟶ B ⨯ A) (n : R₂ ⟶ B ⨯ A) [mono m] [mono n] : factors_through m n ↔ limits.prod.lift (hat m) (hat n) ≫ intersect.intersect A = limits.prod.lift (hat m) (hat n) ≫ limits.prod.fst := leq_prop' _ _ ⟦sub.mk' m⟧ ⟦sub.mk' n⟧ -- lemma leq_iff_factor (A B R₁ R₂ : C) [has_power_object.{v} A] (m : R₁ ⟶ B ⨯ A) (n : R₂ ⟶ B ⨯ A) [mono m] [mono n] : -- factors_through m n ↔ factors_through (prod.lift (hat m) (hat n)) (P₁_arrow A) := -- begin -- rw [leq_prop, factors_through], -- -- refine ⟨λ k, ⟨_, (equalizer.lift' _ k).2⟩, _⟩, -- -- rintro ⟨k, hk⟩, -- -- simp [←hk, P₁_arrow, equalizer.condition], -- end namespace slicing -- EVERYTHING FROM HERE DOWN NEEDS TIDYING!! -- def lift_exists_of_regular {X Y : C} {r : X ⟶ Y} [hr : regular_mono r] {Z : C} {l : Z ⟶ Y} (h : ∃ (q : Z ⟶ X), q ≫ r = l) : {q // q ≫ r = l} := -- begin -- apply fork.is_limit.lift' hr.is_limit l, -- cases h, -- simp [← h_h, hr.w], -- end -- def power_object_of_hats {A PA : C} (mem : sub'.{v} (PA ⨯ A)) (hats : Π {B} (f : sub'.{v} (B ⨯ A)), B ⟶ PA) -- [regular_mono mem.arrow.hom] -- (mediate : Π {B} (f : sub'.{v} (B ⨯ A)), { k : pullback mem.arrow.hom (limits.prod.map (hats f) (𝟙 _)) ≅ f.arrow.left // k.hom ≫ f.arrow.hom = pullback.snd }) : -- is_power_object.{v} mem.arrow.hom := -- { hat := λ B R m hm, by exactI hats (sub'.mk' m), -- powerises' := λ B R m hm, by exactI -- begin -- change has_pullback_top _ _ _, -- obtain ⟨⟨hom, inv, hom_inv_id, inv_hom_id⟩, hq⟩ := mediate (sub'.mk' m), -- dsimp at hom inv hom_inv_id inv_hom_id hq, -- -- let q' : R ⟶ pullback mem.arrow.hom (limits.prod.map (hats (sub'.mk' m)) (𝟙 A)) := pullback.lift _ m _, -- -- sorry, -- -- refine ⟨_, _, _⟩, -- end -- } variables {B} (f g : over B) -- def reflect_pullback (P Q R S : over B) (f : P ⟶ Q) (g : Q ⟶ S) (h : P ⟶ R) (k : R ⟶ S) -- (comm : f ≫ g = h ≫ k) (t : is_limit (pullback_cone.mk f.left h.left (begin exact congr_arg comma_morphism.left comm end))) : -- is_limit (pullback_cone.mk f h comm) := -- begin -- apply is_limit.mk', -- intro s, -- let s' : pullback_cone g.left k.left := pullback_cone.mk (pullback_cone.fst s).left (pullback_cone.snd s).left (congr_arg comma_morphism.left (pullback_cone.condition s)), -- refine ⟨over.hom_mk (t.lift s') _, _, _, _⟩, -- dsimp, change t.lift s' ≫ P.hom = _, rw ← over.w f, slice_lhs 1 2 {erw t.fac _ walking_cospan.left}, exact over.w (pullback_cone.fst s), -- ext1, dsimp, exact t.fac _ walking_cospan.left, -- ext1, dsimp, exact t.fac _ walking_cospan.right, -- intros m m₁ m₂, -- ext1, -- dsimp, -- refine t.hom_ext _, -- apply pullback_cone.equalizer_ext (pullback_cone.mk f.left h.left _), -- erw t.fac _ walking_cospan.left, -- exact congr_arg comma_morphism.left m₁, -- erw t.fac _ walking_cospan.right, -- exact congr_arg comma_morphism.left m₂, -- end -- def preserve_pullback {P Q R S : over B} {f : P ⟶ Q} {g : Q ⟶ S} {h : P ⟶ R} {k : R ⟶ S} -- {comm : f ≫ g = h ≫ k} (t : is_limit (pullback_cone.mk f h comm)) : -- is_limit (pullback_cone.mk f.left h.left (begin exact congr_arg comma_morphism.left comm end)) := -- begin -- apply is_limit.mk', -- intro s, -- let sX' : over B := over.mk (pullback_cone.snd s ≫ R.hom), -- have: pullback_cone.fst s ≫ Q.hom = pullback_cone.snd s ≫ R.hom, -- rw [← over.w g, pullback_cone.condition_assoc s, over.w k], -- let fst' : sX' ⟶ Q := over.hom_mk (pullback_cone.fst s) (by assumption), -- let snd' : sX' ⟶ R := over.hom_mk (pullback_cone.snd s), -- have comm': fst' ≫ g = snd' ≫ k, -- ext, dsimp, apply pullback_cone.condition s, -- let q : sX' ⟶ P := t.lift (pullback_cone.mk fst' snd' comm'), -- have qf : q ≫ f = fst' := t.fac _ walking_cospan.left, -- have qh : q ≫ h = snd' := t.fac _ walking_cospan.right, -- refine ⟨q.left, congr_arg comma_morphism.left qf, congr_arg comma_morphism.left qh, _⟩, -- intros m m₁ m₂, -- have z: m ≫ P.hom = pullback_cone.snd s ≫ R.hom, -- { rw [← over.w h, ← m₂, assoc], refl }, -- let m' : sX' ⟶ P := over.hom_mk m (by apply z), -- have: m' = q, -- apply t.hom_ext, -- refine pullback_cone.equalizer_ext (pullback_cone.mk f h comm) _ _, -- { erw qf, -- ext, -- dsimp, -- erw m₁ }, -- { erw qh, -- ext, -- dsimp, -- erw m₂ }, -- apply congr_arg comma_morphism.left this, -- end variables [has_power_object.{v} B] [has_power_object.{v} f.left] -- @[reducible] def bottom : P f.left ⨯ B ⟶ P f.left ⨯ P f.left := limits.prod.map (𝟙 _) (singleton_arrow B ≫ P_map f.hom) def Q : C := pullback (P₁_arrow f.left) (bottom f) def hk : Q f ⟶ P f.left ⨯ B := pullback.snd def k : Q f ⟶ B := hk f ≫ limits.prod.snd def h : Q f ⟶ P f.left := hk f ≫ limits.prod.fst def over_pow : over B := over.mk (k f) def up : C := pullback (mem f.left) (limits.prod.map (h f) (𝟙 f.left)) def h' : up f ⟶ Q f ⨯ f.left := pullback.snd instance mono_h' : mono (h' f) := pullback.snd_of_mono instance mono_hk : mono (hk f) := pullback.snd_of_mono def hat_h' : hat (h' f) = h f := unique_hat has_pullback_top_of_pb def over.ni (f : over B) [has_power_object.{v} B] [has_power_object.{v} f.left] : over B := over.mk (h' f ≫ limits.prod.snd ≫ f.hom) -- fix me. def prop (f : over B) [has_power_object.{v} B] [has_power_object.{v} f.left] : ∃ q, q ≫ (pullback.snd : pullback (prod.lift f.hom (𝟙 f.left)) (limits.prod.map ((k f) : _ ⟶ B) (𝟙 f.left)) ⟶ _) = h' f := begin have: pullback.fst ≫ P₁_arrow f.left = limits.prod.lift (h f) (k f ≫ singleton_arrow B ≫ P_map f.hom), rw [pullback.condition], dunfold bottom, apply prod.hom_ext, { rw [assoc, prod.lift_fst, h, hk, limits.prod.map_fst, comp_id] }, { rw [assoc, prod.lift_snd, k, hk, limits.prod.map_snd, assoc] }, rw [← seven_six_two, ← hat_natural_left, ← hat_h' f] at this, have: limits.prod.lift (hat (h' f)) (hat pullback.snd) ≫ intersect.intersect f.left = limits.prod.lift (hat (h' f)) (hat pullback.snd) ≫ limits.prod.fst, rw ← this, erw [assoc, assoc, equalizer.condition], refl, rw ← leq_prop at this, cases this with a, refine ⟨_, over.w a⟩, end -- @[reducible] def over.mem : over.ni f ⟶ over_pow f ⨯ f := begin apply prod.lift _ _, apply over.hom_mk _ _, exact h' f ≫ limits.prod.fst, dsimp [over_pow, over.ni, hk], change (((h' f : up f ⟶ _) ≫ (limits.prod.fst : Q f ⨯ f.left ⟶ Q f)) : _ ⟶ Q f) ≫ (k f : Q f ⟶ B) = (h' f : up f ⟶ Q f ⨯ f.left) ≫ (limits.prod.snd : Q f ⨯ f.left ⟶ f.left) ≫ (f.hom : f.left ⟶ B), obtain ⟨q, hq⟩ := prop f, have z : (pullback.fst : pullback (prod.lift f.hom (𝟙 f.left)) (limits.prod.map ((k f) : _ ⟶ B) (𝟙 f.left)) ⟶ _) ≫ _ = _ ≫ _ := pullback.condition, have z₁ := z =≫ limits.prod.fst, rw [assoc, prod.lift_fst, assoc, limits.prod.map_fst] at z₁, have z₂ := z =≫ limits.prod.snd, erw [assoc, assoc, prod.lift_snd, limits.prod.map_snd, comp_id, comp_id] at z₂, rw [← hq, assoc, assoc, ← z₁, z₂, assoc, assoc], apply over.hom_mk _ _, exact h' f ≫ limits.prod.snd, simp only [assoc, auto_param_eq], refl, end -- pretty gross. instance over.mem_mono : mono (over.mem f) := begin refine ⟨λ Z p q eq, _⟩, ext1, rw ← cancel_mono (h' f), apply prod.hom_ext, rw [assoc, assoc], have e₁ := eq =≫ limits.prod.fst, rw [over.mem, assoc, assoc, prod.lift_fst] at e₁, exact congr_arg comma_morphism.left e₁, have e₂ := eq =≫ limits.prod.snd, rw [over.mem, assoc, assoc, prod.lift_snd] at e₂, rw [assoc, assoc], exact congr_arg comma_morphism.left e₂, end section hat variables {f g} variables {r : over B} (m : r ⟶ g ⨯ f) [mono m] def m' : r.left ⟶ (g ⨯ f).left := m.left instance m'_mono : mono (m' m) := category_theory.over_mono m def m'' : r.left ⟶ g.left ⨯ f.left := m' m ≫ magic_arrow f g instance m''_mono : mono (m'' m) := mono_comp _ _ -- @[reducible] def mhat : g.left ⟶ P f.left := hat (m'' m) -- @[reducible] def l : g.left ⟶ P f.left ⨯ P f.left := prod.lift (mhat m) g.hom ≫ bottom f lemma l_eq : l m = prod.lift (hat (m'' m)) (g.hom ≫ (singleton_arrow B : B ⟶ P B) ≫ P_map f.hom) := begin rw [l, bottom, prod.lift_map, comp_id], refl, end lemma llem : l m ≫ intersect.intersect f.left = l m ≫ limits.prod.fst := begin have := l_eq m, erw [← seven_six_two, ← hat_natural_left] at this, rw [this, ← leq_prop], refine ⟨_⟩, apply over.hom_mk _ _, { apply pullback.lift (m'' m ≫ limits.prod.snd) (m'' m) _, apply prod.hom_ext, { erw [assoc, assoc, assoc, assoc, m'', assoc, prod.lift_fst, limits.prod.map_fst], slice_lhs 2 3 {rw prod.lift_snd}, slice_rhs 2 3 {rw prod.lift_fst}, rw over.w (limits.prod.fst : g ⨯ f ⟶ g), rw over.w (limits.prod.snd : g ⨯ f ⟶ f) }, { erw [assoc, assoc, assoc, assoc, assoc, prod.lift_snd, comp_id, limits.prod.map_snd, comp_id] } }, { dsimp, rw limit.lift_π, refl } end -- @[reducible] def top : g.left ⟶ P₁_obj f.left := equalizer.lift (l m) (llem m) -- @[reducible] def h'' : g.left ⟶ Q f := pullback.lift (top m) (prod.lift (mhat m) g.hom) (limit.lift_π _ _) -- @[reducible] def make_arrow : g ⟶ over_pow f := over.hom_mk (h'' m) $ by { dsimp [over_pow, hk, k, h''], simp } -- @[reducible] def square_top (m : r ⟶ g ⨯ f) [mono m] : r ⟶ over.ni f := begin refine over.hom_mk (pullback.lift (hat_powerises (m'' m)).top _ _) _, { apply (m'' m) ≫ limits.prod.map (h'' m) (𝟙 _) }, { rw (hat_powerises (m'' m)).comm, conv_rhs {rw [assoc, ← prod.map_comp_id]}, congr' 2, erw [h, hk, h'', limit.lift_π_assoc, prod.lift_fst, mhat] }, { dsimp [h'], erw [limit.lift_π_assoc, assoc, limits.prod.map_snd_assoc, id_comp], erw [← over.w m, assoc, prod.lift_snd_assoc, over.w (limits.prod.snd : g ⨯ f ⟶ f)], refl } end def alt_square_commutes : square_top m ≫ over.mem f ≫ limits.prod.fst = (m ≫ limits.prod.fst) ≫ make_arrow m := begin rw [assoc, over.mem, prod.lift_fst, make_arrow], ext1, dsimp [h', m'', magic_arrow, h'', square_top], rw limit.lift_π_assoc, dsimp, rw [assoc, limits.prod.map_fst, assoc, prod.lift_fst_assoc], refl end def square_commutes : square_top m ≫ over.mem f = m ≫ limits.prod.map (make_arrow m) (𝟙 _) := begin apply prod.hom_ext, { rw [assoc, alt_square_commutes, assoc, assoc, limits.prod.map_fst] }, { rw [assoc, over.mem, prod.lift_snd, assoc, limits.prod.map_snd, comp_id], ext1, dsimp [h', square_top], rw limit.lift_π_assoc, dsimp, rw [assoc, limits.prod.map_snd, comp_id], simp [m'', m'] } end def alt_square_pb : is_limit (pullback_cone.mk _ _ (alt_square_commutes m)) := begin apply reflects_pullback_cone (over.forget _), -- apply reflect_pullback, dsimp [square_top], refine is_limit.mk' _ _, intro s, have lem : (pullback_cone.fst s ≫ pullback.fst) ≫ mem f.left = prod.lift (pullback_cone.snd s) (pullback_cone.fst s ≫ pullback.fst ≫ mem f.left ≫ limits.prod.snd) ≫ limits.prod.map (hat (m'' m)) (𝟙 f.left), { apply prod.hom_ext, { rw [assoc, assoc, assoc, limits.prod.map_fst, prod.lift_fst_assoc, category_theory.limits.pullback.condition_assoc, limits.prod.map_fst], have : pullback_cone.fst s ≫ (over.mem f ≫ _).left = _ := pullback_cone.condition s, dsimp only [over.mem] at this, rw [prod.lift_fst] at this, dsimp [h'] at this, slice_lhs 1 3 {rw this}, dsimp [h, hk, make_arrow, h''], rw [assoc], rw [pullback.lift_snd_assoc, prod.lift_fst], refl }, { rw [assoc, assoc, assoc, limits.prod.map_snd, comp_id, prod.lift_snd] } }, let t : s.X ⟶ r.left := (hat_powerises (m'' m)).is_pb.lift (pullback_cone.mk _ _ lem), have t₃ : t ≫ m'' m ≫ limits.prod.fst = pullback_cone.snd s, rw ← assoc, erw (hat_powerises (m'' m)).is_pb.fac (pullback_cone.mk _ _ lem) walking_cospan.right, dsimp, rw prod.lift_fst, have t₂ : t ≫ m'' m ≫ limits.prod.snd = pullback_cone.fst s ≫ pullback.fst ≫ mem f.left ≫ limits.prod.snd, rw ← assoc, erw (hat_powerises (m'' m)).is_pb.fac (pullback_cone.mk _ _ lem) walking_cospan.right, dsimp, rw prod.lift_snd, have t₁: t ≫ (hat_powerises (m'' m)).top = pullback_cone.fst s ≫ pullback.fst, erw (hat_powerises (m'' m)).is_pb.fac (pullback_cone.mk _ _ lem) walking_cospan.left, refl, refine ⟨t, _, _, _⟩, { change t ≫ pullback.lift (hat_powerises (m'' m)).top (m'' m ≫ limits.prod.map (h'' m) (𝟙 f.left)) _ = s.π.app walking_cospan.left, apply pullback.hom_ext, { rw ← t₁, simp }, { rw [assoc], slice_lhs 2 3 {rw limit.lift_π}, dsimp, apply prod.hom_ext, { rw [assoc, assoc, limits.prod.map_fst], slice_lhs 1 3 {rw t₃}, rw [h''], erw ← pullback_cone.condition s, rw assoc, change pullback_cone.fst s ≫ (over.mem f ≫ limits.prod.fst).left = s.π.app walking_cospan.left ≫ pullback.snd ≫ limits.prod.fst, erw prod.lift_fst, refl }, { rw [assoc, assoc, limits.prod.map_snd, comp_id, t₂, category_theory.limits.pullback.condition_assoc, limits.prod.map_snd, comp_id, assoc] } } }, { dunfold pullback_cone.snd, dsimp, rw [m'', assoc, magic_arrow, prod.lift_fst] at t₃, exact t₃ }, { intros t' m₁ m₂, have m₁' : t' ≫ pullback.lift (hat_powerises (m'' m)).top (m'' m ≫ limits.prod.map (h'' m) (𝟙 f.left)) _ = pullback_cone.fst s := m₁, have m₂' : t' ≫ m.left ≫ _ = pullback_cone.snd s := m₂, clear m₁ m₂, rw ← cancel_mono (m'' m), change t' ≫ m' m ≫ magic_arrow f g = t ≫ m' m ≫ magic_arrow f g, apply prod.hom_ext, { rw [assoc, assoc], slice_lhs 3 4 {rw prod.lift_fst}, rw m', rw m₂', rw ← t₃, rw assoc, refl }, { conv_rhs {erw [assoc, t₂, ← m₁']}, rw [assoc, assoc, assoc], slice_rhs 2 3 {rw limit.lift_π}, dsimp, rw (hat_powerises (m'' m)).comm, rw [assoc, limits.prod.map_snd, comp_id], simp [m''] } }, refine ⟨λ K, by apply_instance⟩, end end hat def main' (f : over B) [has_power_object.{v} f.left] : is_power_object (over.mem f) := { hat := λ b r m hm, by exactI make_arrow m, powerises' := λ g r m hm, by exactI begin refine ⟨square_top m, square_commutes m, _⟩, apply is_limit.mk' _ _, intro s, have comm : pullback_cone.fst s ≫ over.mem f ≫ limits.prod.fst = (pullback_cone.snd s ≫ limits.prod.fst) ≫ make_arrow m, rw [pullback_cone.condition_assoc s, limits.prod.map_fst, assoc], let s' := pullback_cone.mk _ _ comm, let t := (alt_square_pb m).lift s', have t₁ : t ≫ square_top m = pullback_cone.fst s' := (alt_square_pb m).fac s' walking_cospan.left, have t₂ : t ≫ m ≫ limits.prod.fst = pullback_cone.snd s' := (alt_square_pb m).fac s' walking_cospan.right, have t₃ := t₁ =≫ over.mem f, rw [assoc, square_commutes m] at t₃, replace t₃ := t₃ =≫ limits.prod.snd, rw [assoc, assoc, assoc, limits.prod.map_snd, comp_id] at t₃, refine ⟨(alt_square_pb m).lift s', (alt_square_pb m).fac s' walking_cospan.left, _, _⟩, { change t ≫ m = pullback_cone.snd s, apply prod.hom_ext, { rw [assoc, t₂], refl }, { rw [assoc, t₃], erw [pullback_cone.condition_assoc s, limits.prod.map_snd, comp_id] } }, { intros t' m₁ m₂, apply (alt_square_pb m).hom_ext, apply pullback_cone.equalizer_ext (pullback_cone.mk (square_top m) (m ≫ limits.prod.fst) _), erw t₁, exact m₁, erw [t₂, ← assoc, m₂], refl } end, uniquely' := λ g r m hm hat' p, by exactI begin ext1, rw ← cancel_mono (hk f), have z₁: hat'.left ≫ k f = g.hom := over.w hat', suffices z₂: hat'.left ≫ h f = (make_arrow m).left ≫ h f, apply prod.hom_ext, { erw [assoc, assoc, z₂], refl }, { rw [assoc, assoc], change _ ≫ k f = _ ≫ k f, rw [z₁, make_arrow, over.hom_mk_left, h'', k, hk, pullback.lift_snd_assoc, prod.lift_snd] }, erw [make_arrow, limit.lift_π_assoc, prod.lift_fst], symmetry, apply unique_hat, change has_pullback_top _ _ _, rw prod.map_comp_id, apply left_right_hpb_to_both_hpb (h' f) _ has_pullback_top_of_pb, have: h' f = (over.mem f).left ≫ magic_arrow f (over_pow f), { apply prod.hom_ext, { rw [assoc, prod.lift_fst, ← over.comp_left, over.mem, prod.lift_fst], refl }, { rw [assoc, prod.lift_snd, ← over.comp_left, over.mem, prod.lift_snd], refl } }, rw this, apply stretch_hpb_down _ _ (limits.prod.map hat' (𝟙 f)).left _ _ _ _ _ (magic_pb _ _ f hat'), apply over_forget_preserves_hpb _ _ _ p, end } def main (f : over B) [has_power_object.{v} f.left] : has_power_object.{v} f := { PA := over_pow f, niA := over.ni f, memA := over.mem f, is_power := main' f } end slicing instance fundamental_theorem (B : C) [has_power_objects.{v} C] : has_power_objects.{v} (over B) := { has_power_object := λ f, slicing.main f } def comparison [has_power_objects.{v} C] {D : Type u₂} [category.{v} D] [has_finite_limits.{v} D] [has_power_objects.{v} D] (F : C ⥤ D) (h : Π (J : Type v) [𝒥₁ : small_category J] [@fin_category J 𝒥₁], @preserves_limits_of_shape _ _ _ _ J 𝒥₁ F) (A : C) : F.obj (P A) ⟶ P (F.obj A) := begin let m := F.map (mem A) ≫ (prod_comparison F (P A) A), letI : mono (F.map (mem A)) := preserves_mono_of_preserves_pullback F _ _ _, exact hat m, end def comp_natural' [has_power_objects.{v} C] {D : Type u₂} [category.{v} D] [has_finite_limits.{v} D] [has_power_objects.{v} D] (F : C ⥤ D) (h : Π (J : Type v) [𝒥₁ : small_category J] [@fin_category J 𝒥₁], @preserves_limits_of_shape _ _ _ _ J 𝒥₁ F) (A B : C) (f : B ⟶ A) : F.map (P_map f) ≫ comparison F h B = comparison F h A ≫ P_map (F.map f) := begin dsimp [comparison], rw [← hat_natural_left, ← hat_natural_right], let m₁ := F.map (mem A) ≫ (prod_comparison F (P A) A), let m₂ := F.map (mem B) ≫ (prod_comparison F (P B) B), letI : mono (F.map (mem A)) := preserves_mono_of_preserves_pullback F _ _ _, letI : mono (F.map (mem B)) := preserves_mono_of_preserves_pullback F _ _ _, letI : mono (F.map (Emap f)) := preserves_mono_of_preserves_pullback F _ _ _, let P₁ := pullback (F.map (mem B) ≫ (prod_comparison F (P B) B)) (limits.prod.map (F.map (P_map f)) (𝟙 (F.obj B))), let P₂ := pullback (F.map (mem A) ≫ (prod_comparison F (P A) A)) (limits.prod.map (𝟙 _) (F.map f)), let h₁ : P₁ ⟶ _ := pullback.snd, let h₂ : P₂ ⟶ _ := pullback.snd, change hat h₁ = hat h₂, let s₁ := (hat_powerises (Emap f)).is_pb, let s₂ := Epb f, let Fs₁ := preserves_pullback_cone F _ _ _ _ _ s₁, let Fs₂ := preserves_pullback_cone F _ _ _ _ _ s₂, have s₃comm : F.map (limits.prod.map (P_map f) (𝟙 B)) ≫ (prod_comparison F (P B) B) = (prod_comparison F (P A) B) ≫ limits.prod.map (F.map (P_map f)) (𝟙 (F.obj B)), rw [prod_comparison, prod_comparison], apply prod.hom_ext, { erw [assoc, prod.lift_fst, assoc, limits.prod.map_fst, ← F.map_comp, limits.prod.map_fst, prod.lift_fst_assoc, F.map_comp] }, { erw [assoc, prod.lift_snd, assoc, limits.prod.map_snd, comp_id, ← F.map_comp, limits.prod.map_snd, comp_id, prod.lift_snd] }, let s₃ := pullback_square_iso (F.map (limits.prod.map (P_map f) (𝟙 _))) (prod_comparison F (P A) B) (prod_comparison F (P B) B) (limits.prod.map (F.map (P_map f)) (𝟙 _)) s₃comm, let Fs₁s₃ := vpaste _ _ _ _ _ _ _ _ _ s₃ Fs₁, have eq₁: hat h₁ = hat (F.map (Emap f) ≫ (prod_comparison F (P A) B)), { apply lifting _ _ _ _, { apply Fs₁s₃.lift (limit.cone _) }, { apply limit.lift _ (pullback_cone.mk (F.map (hat_powerises (Emap f)).top) (F.map (Emap f) ≫ (prod_comparison F (P A) B)) _), rw [assoc, ← s₃comm, ← assoc, ← F.map_comp, (hat_powerises (Emap f)).comm, F.map_comp, assoc], refl }, { exact (Fs₁s₃.fac (limit.cone _) walking_cospan.right) }, { rw limit.lift_π, refl } }, have s₄comm : F.map (limits.prod.map (𝟙 (P A)) f) ≫ (prod_comparison F (P A) A) = (prod_comparison F (P A) B) ≫ limits.prod.map (𝟙 (F.obj (P A))) (F.map f), rw [prod_comparison, prod_comparison], apply prod.hom_ext, { rw [assoc, prod.lift_fst, assoc, limits.prod.map_fst, ← F.map_comp, limits.prod.map_fst, comp_id, comp_id, prod.lift_fst] }, { rw [assoc, prod.lift_snd, assoc, limits.prod.map_snd, ← F.map_comp, limits.prod.map_snd, prod.lift_snd_assoc, F.map_comp] }, let s₄ := pullback_square_iso (F.map (limits.prod.map (𝟙 _) f)) (prod_comparison F (P A) B) (prod_comparison F (P A) A) (limits.prod.map (𝟙 _) (F.map f)) s₄comm, let Fs₂s₄ := vpaste _ _ _ _ _ _ _ _ _ s₄ Fs₂, have eq₂: hat h₂ = hat (F.map (Emap f) ≫ (prod_comparison F (P A) B)), { apply lifting _ _ _ _, { apply Fs₂s₄.lift (limit.cone _) }, { apply limit.lift _ (pullback_cone.mk (F.map pullback.fst) (F.map (Emap f) ≫ (prod_comparison F (P A) B)) _), rw [assoc, ← s₄comm, ← assoc, ← F.map_comp, pullback.condition, F.map_comp, assoc], refl }, { exact (Fs₂s₄.fac (limit.cone _) walking_cospan.right) }, { rw limit.lift_π, refl } }, rw [eq₁, eq₂], end -- Define F as a logical functor if this is an iso. def comp_natural [has_power_objects.{v} C] {D : Type u₂} [category.{v} D] [has_finite_limits.{v} D] [has_power_objects.{v} D] (F : C ⥤ D) [h : Π (J : Type v) [𝒥₁ : small_category J] [@fin_category J 𝒥₁], @preserves_limits_of_shape _ _ _ _ J 𝒥₁ F] : (P_functor ⋙ F) ⟶ (F.op ⋙ P_functor) := { app := λ A, comparison F h A.unop, naturality' := λ A B g, comp_natural' F h A.unop B.unop g.unop } def star_power (A B : C) [has_power_object.{v} A] : (star B).obj (ni A) ⟶ (star B).obj (P A) ⨯ (star B).obj A := begin haveI := adjunction.right_adjoint_preserves_limits (forget_adj_star B), exact (star B).map (mem A) ≫ (prod_comparison (star B) (P A) A) end instance star_mono (A B : C) [has_power_object.{v} A] : mono (star_power A B) := begin haveI : mono ((star B).map (mem A)) := right_adjoint_preserves_mono (forget_adj_star B) (by apply_instance), haveI := adjunction.right_adjoint_preserves_limits (forget_adj_star B), rw star_power, haveI : is_iso (prod_comparison (star B) (P A) A) := by apply_instance, apply_instance end def alt_prod (A : C) {B : C} (g : over B) : over B := over.mk ((limits.prod.fst : g.left ⨯ A ⟶ g.left) ≫ g.hom) @[simps] def the_iso (A : C) {B : C} (g : over B) : g ⨯ (star B).obj A ≅ alt_prod A g := { hom := begin apply over.hom_mk _ _, apply prod.lift (limits.prod.fst : g ⨯ _ ⟶ _).left _, refine (limits.prod.snd : g ⨯ _ ⟶ _).left ≫ limits.prod.snd, erw limit.lift_π_assoc, exact over.w (limits.prod.fst : g ⨯ (star B).obj A ⟶ _), end, inv := begin apply prod.lift, refine over.hom_mk limits.prod.fst rfl, refine over.hom_mk (limits.prod.map g.hom (𝟙 _)) (limits.prod.map_fst _ _), end, hom_inv_id' := begin ext1, dsimp, rw ← cancel_mono (magic_arrow ((star B).obj A) g), rw id_comp, apply prod.hom_ext, rw [prod.lift_fst, assoc, prod.lift_fst, assoc, ← over.comp_left, prod.lift_fst, over.hom_mk_left, prod.lift_fst], rw [prod.lift_snd, assoc, prod.lift_snd, assoc, ← over.comp_left, prod.lift_snd, over.hom_mk_left], apply prod.hom_ext, rw [assoc, limits.prod.map_fst, prod.lift_fst_assoc, over.w (limits.prod.fst : g ⨯ (star B).obj A ⟶ _)], exact (over.w (limits.prod.snd : g ⨯ (star B).obj A ⟶ _)).symm, rw [assoc, limits.prod.map_snd, prod.lift_snd_assoc, comp_id], end, inv_hom_id' := begin ext, dsimp, rw [assoc, prod.lift_fst, ← over.comp_left, prod.lift_fst, id_comp], refl, rw [over.comp_left, assoc, over.hom_mk_left, prod.lift_snd, ← assoc, ← over.comp_left, prod.lift_snd, over.hom_mk_left, limits.prod.map_snd, over.id_left, id_comp, comp_id], end } def star_hat {A B : C} [has_power_object.{v} A] {g r : over B} (m : r ⟶ g ⨯ (star B).obj A) (k : g.left ⟶ P A) [mono m] : g ⟶ (star B).obj (P A):= over.hom_mk (prod.lift g.hom k) (limit.lift_π _ _) def seven_eleven_r_comm (A B : C) [has_power_object.{v} A] : 𝟙 (B ⨯ _) ≫ limits.prod.map (𝟙 _) (mem A) = (star_power A B ≫ (the_iso A ((star B).obj (P A))).hom).left ≫ (prod.associator B (P A) A).hom := begin dsimp [star_power, the_iso, prod_comparison], rw [assoc, assoc, id_comp], apply prod.hom_ext, rw [assoc, assoc, assoc, prod.lift_fst, prod.lift_fst_assoc, limits.prod.map_fst, comp_id], slice_rhs 2 3 {rw ← over.comp_left}, rw [prod.lift_fst, over.hom_mk_left, ← assoc, ← prod.map_id_comp, limits.prod.map_fst, comp_id], rw [assoc, assoc, assoc, prod.lift_snd, limits.prod.map_snd], apply prod.hom_ext, rw [assoc, assoc, assoc, assoc, prod.lift_fst, prod.lift_fst_assoc], slice_rhs 2 3 {rw ← over.comp_left}, rw [prod.lift_fst, over.hom_mk_left, ← prod.map_id_comp_assoc, limits.prod.map_snd], rw [assoc, assoc, assoc, assoc, prod.lift_snd, prod.lift_snd], slice_rhs 2 3 {rw ← over.comp_left}, rw [prod.lift_snd, over.hom_mk_left, ← prod.map_id_comp_assoc, limits.prod.map_snd], end def seven_eleven_aux (A B : C) [has_power_object.{v} A] (g r : over B) (m : r ⟶ g ⨯ (star B).obj A) [mono m] (k : g.left ⟶ P A) : powerises (mem A) (m ≫ (the_iso A g).hom).left k ≅ powerises (star_power A B) m (star_hat m k) := begin have bottom_comm : limits.prod.map (star_hat m k) (𝟙 _) ≫ (the_iso A _).hom = (the_iso A g).hom ≫ over.hom_mk (limits.prod.map (prod.lift g.hom k) (𝟙 A)) (by { dsimp, erw [limits.prod.map_fst_assoc, limits.prod.lift_fst], refl }), { dsimp [the_iso], ext : 2, { rw [over.comp_left, over.comp_left, over.hom_mk_left, assoc, prod.lift_fst, ← over.comp_left, limits.prod.map_fst, over.comp_left, star_hat, over.hom_mk_left, over.hom_mk_left, over.hom_mk_left, prod.lift_map, prod.lift_fst] }, { rw [over.comp_left, over.comp_left, over.hom_mk_left, assoc, prod.lift_snd, ← assoc, ← over.comp_left, limits.prod.map_snd, comp_id, over.hom_mk_left, over.hom_mk_left, prod.lift_map, comp_id, prod.lift_snd] } }, have b_pb := pullback_square_iso _ _ _ _ bottom_comm, have right₁_comm : 𝟙 (B ⨯ _) ≫ limits.prod.map (𝟙 _) (mem A) = (star_power A B ≫ (the_iso A ((star B).obj (P A))).hom).left ≫ (prod.associator B (P A) A).hom, apply seven_eleven_r_comm, have r₁_pb := pullback_square_iso' _ _ _ _ right₁_comm, have r₂_pb := pullback_prod' (mem A) B, have r_pb := (left_pb_to_both_pb _ _ _ _ _ _ _ _ _ r₁_pb) r₂_pb, have p : limits.prod.map (prod.lift g.hom k) (𝟙 A) ≫ (prod.associator B (P A) A).hom ≫ limits.prod.snd = limits.prod.map k (𝟙 A), rw [prod.associator_hom, prod.lift_snd], apply prod.hom_ext, { rw [assoc, prod.lift_fst, limits.prod.map_fst, prod.map_fst_assoc, prod.lift_snd] }, { rw [assoc, prod.lift_snd, limits.prod.map_snd, limits.prod.map_snd] }, refine ⟨_, _, subsingleton.elim _ _, subsingleton.elim _ _⟩, { intro q, refine cut_hpb_up _ _ _ _ _ _ _ _ b_pb, apply over_forget_reflects_hpb, refine right_both_hpb_to_left_hpb _ _ _ (has_pullback_top_of_is_pb r_pb), convert q }, { intro q, have := stretch_hpb_down _ _ _ _ _ _ q _ b_pb, have := over_forget_preserves_hpb _ _ _ this, change has_pullback_top _ _ _, have p' := p.symm, convert left_hpb_right_pb_to_both_hpb _ _ _ _ _ _ this _ r_pb } end def seven_eleven (A B : C) [has_power_object.{v} A] : is_power_object (star_power A B) := { hat := λ g r m hm, by exactI over.hom_mk (prod.lift g.hom (hat (m ≫ (the_iso A g).hom).left)) (limit.lift_π _ _), powerises' := λ g r m hm, by exactI begin apply (seven_eleven_aux A B g r m (hat (m ≫ (the_iso A g).hom).left)).hom, exact hat_powerises (m ≫ (the_iso A g).hom).left, end, uniquely' := λ g r m hm hat' pow, by exactI begin ext, rw [over.hom_mk_left, prod.lift_fst, ← over.w hat'], refl, rw [over.hom_mk_left, prod.lift_snd], apply unique_hat, apply (seven_eleven_aux A B g r m (hat'.left ≫ limits.prod.snd)).inv, convert pow, rw [star_hat], ext, rw [over.hom_mk_left, prod.lift_fst, ← over.w hat'], refl, rw [over.hom_mk_left, prod.lift_snd] end } def logical_star [has_power_objects.{v} C] (B : C) : P_functor ⋙ star B ≅ (star B).op ⋙ P_functor := begin apply nat_iso.of_components _ _, intro A, exact P_unique_up_to_iso (seven_eleven A.unop B) (power_is_power _), intros X Y g, haveI := adjunction.right_adjoint_preserves_limits (forget_adj_star B), apply comp_natural' (star B) infer_instance X.unop Y.unop g.unop, end local attribute [instance] has_finite_products_of_has_finite_limits def cc_of_pow [has_power_objects.{v} C] : cartesian_closed.{v} C := { closed := λ B, begin haveI : is_right_adjoint (star B) := ⟨over.forget _, forget_adj_star B⟩, haveI := monadic_adjoint_square_lift _ _ _ _ (logical_star B), refine exponentiable_of_star_is_left_adj B _, apply left_adjoint_of_right_adjoint_op, end } def lcc_of_pow [has_power_objects.{v} C] : is_locally_cartesian_closed.{v} C := { overs_cc := λ B, cc_of_pow } def subobj_hat {A B R : C} [exponentiable A] [has_subobject_classifier.{v} C] (m : R ⟶ B ⨯ A) [mono m] : B ⟶ A ⟹ classifier.Ω C := cartesian_closed.curry ((limits.prod.braiding _ _).inv ≫ classifier.classifier_of m) def power_of_subobj (A : C) [exponentiable A] [has_subobject_classifier.{v} C] : has_power_object.{v} A := { PA := A ⟹ classifier.Ω C, niA := pullback (classifier.truth C) ((limits.prod.braiding _ _).hom ≫ (ev A).app _), memA := pullback.snd, is_power := { hat := λ B R m hm, by exactI subobj_hat m, powerises' := λ B R m hm, begin haveI := hm, apply right_both_hpb_to_left_hpb _ _ _ has_pullback_top_of_pb, erw [braid_natural_assoc, subobj_hat, curry_eq, prod.map_id_comp, assoc, (ev _).naturality, ev_coev_assoc, iso.hom_inv_id_assoc], apply classifier.classifies m, end, uniquely' := λ B R m hm hat' p, begin rw [subobj_hat, curry_eq_iff, iso.inv_comp_eq], apply classifier.uniquely, change has_pullback_top _ _ _, rw [uncurry_eq, ← braid_natural_assoc], apply left_right_hpb_to_both_hpb pullback.snd p has_pullback_top_of_pb, end } } instance topos_has_power [has_subobject_classifier.{v} C] [cartesian_closed.{v} C] : has_power_objects.{v} C := ⟨λ A, power_of_subobj A⟩ instance topos_has_some_colims (J : Type v) [small_category J] [has_subobject_classifier.{v} C] [cartesian_closed.{v} C] [has_limits_of_shape Jᵒᵖ C] : has_colimits_of_shape J C := some_colims J instance topos_has_finite_colimits [has_subobject_classifier.{v} C] [cartesian_closed.{v} C] : has_finite_colimits.{v} C := λ _ _ _, by {resetI, apply_instance} instance topos_is_lcc [has_subobject_classifier.{v} C] [cartesian_closed.{v} C] : is_locally_cartesian_closed.{v} C := lcc_of_pow end category_theory
8db0a878d712bf69ee452f465ba9bf526ad29092
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/theories/analysis/metric_space.lean
c6b36609e300844e84150bec41dc97e9214143e8
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
27,970
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Metric spaces. Authors: Jeremy Avigad, Robert Y. Lewis -/ import data.real.complete data.pnat ..topology.continuous ..topology.limit data.set open nat real eq.ops classical set prod set.filter topology interval structure metric_space [class] (M : Type) : Type := (dist : M → M → ℝ) (dist_self : ∀ x : M, dist x x = 0) (eq_of_dist_eq_zero : ∀ {x y : M}, dist x y = 0 → x = y) (dist_comm : ∀ x y : M, dist x y = dist y x) (dist_triangle : ∀ x y z : M, dist x z ≤ dist x y + dist y z) namespace analysis section metric_space_M variables {M : Type} [metric_space M] definition dist (x y : M) : ℝ := metric_space.dist x y proposition dist_self (x : M) : dist x x = 0 := metric_space.dist_self x proposition eq_of_dist_eq_zero {x y : M} (H : dist x y = 0) : x = y := metric_space.eq_of_dist_eq_zero H proposition dist_comm (x y : M) : dist x y = dist y x := metric_space.dist_comm x y proposition dist_eq_zero_iff (x y : M) : dist x y = 0 ↔ x = y := iff.intro eq_of_dist_eq_zero (suppose x = y, this ▸ !dist_self) proposition dist_triangle (x y z : M) : dist x z ≤ dist x y + dist y z := metric_space.dist_triangle x y z proposition dist_nonneg (x y : M) : 0 ≤ dist x y := have dist x y + dist y x ≥ 0, by rewrite -(dist_self x); apply dist_triangle, have 2 * dist x y ≥ 0, by krewrite [-real.one_add_one, right_distrib, +one_mul, dist_comm at {2}]; apply this, nonneg_of_mul_nonneg_left this two_pos proposition dist_pos_of_ne {x y : M} (H : x ≠ y) : dist x y > 0 := lt_of_le_of_ne !dist_nonneg (suppose 0 = dist x y, H (iff.mp !dist_eq_zero_iff this⁻¹)) proposition ne_of_dist_pos {x y : M} (H : dist x y > 0) : x ≠ y := suppose x = y, have H1 : dist x x > 0, by rewrite this at {2}; exact H, by rewrite dist_self at H1; apply not_lt_self _ H1 proposition eq_of_forall_dist_le {x y : M} (H : ∀ ε, ε > 0 → dist x y ≤ ε) : x = y := eq_of_dist_eq_zero (eq_zero_of_nonneg_of_forall_le !dist_nonneg H) /- instantiate metric space as a topology -/ definition open_ball (x : M) (ε : ℝ) := {y | dist y x < ε} theorem open_ball_eq_empty_of_nonpos (x : M) {ε : ℝ} (Hε : ε ≤ 0) : open_ball x ε = ∅ := begin apply eq_empty_of_forall_not_mem, intro y Hlt, apply not_lt_of_ge (dist_nonneg y x), apply lt_of_lt_of_le Hlt Hε end theorem pos_of_mem_open_ball {x : M} {ε : ℝ} {u : M} (Hu : u ∈ open_ball x ε) : ε > 0 := begin apply lt_of_not_ge, intro Hge, note Hop := open_ball_eq_empty_of_nonpos x Hge, rewrite Hop at Hu, apply not_mem_empty _ Hu end theorem mem_open_ball (x : M) {ε : ℝ} (H : ε > 0) : x ∈ open_ball x ε := show dist x x < ε, by rewrite dist_self; assumption definition closed_ball (x : M) (ε : ℝ) := {y | dist y x ≤ ε} theorem closed_ball_eq_compl (x : M) (ε : ℝ) : closed_ball x ε = - {y | dist y x > ε} := ext (take y, iff.intro (suppose dist y x ≤ ε, not_lt_of_ge this) (suppose ¬ dist y x > ε, le_of_not_gt this)) variable (M) definition open_sets_basis : set (set M) := { s | ∃ x, ∃ ε, s = open_ball x ε } definition metric_topology [instance] : topology M := topology.generated_by (open_sets_basis M) variable {M} theorem open_ball_mem_open_sets_basis (x : M) (ε : ℝ) : open_ball x ε ∈ open_sets_basis M := exists.intro x (exists.intro ε rfl) theorem Open_open_ball (x : M) (ε : ℝ) : Open (open_ball x ε) := by apply generators_mem_topology_generated_by; apply open_ball_mem_open_sets_basis theorem closed_closed_ball (x : M) {ε : ℝ} (H : ε > 0) : closed (closed_ball x ε) := Open_of_forall_exists_Open_nbhd (take y, suppose ¬ dist y x ≤ ε, have dist y x > ε, from lt_of_not_ge this, let B := open_ball y (dist y x - ε) in have y ∈ B, from mem_open_ball y (sub_pos_of_lt this), have B ⊆ - closed_ball x ε, from take y', assume Hy'y : dist y' y < dist y x - ε, assume Hy'x : dist y' x ≤ ε, show false, from not_lt_self (dist y x) (calc dist y x ≤ dist y y' + dist y' x : dist_triangle ... < dist y x - ε + dist y' x : by rewrite dist_comm; apply add_lt_add_right Hy'y ... ≤ dist y x - ε + ε : add_le_add_left Hy'x ... = dist y x : by rewrite [sub_add_cancel]), exists.intro B (and.intro (Open_open_ball _ _) (and.intro `y ∈ B` this))) proposition open_ball_subset_open_ball_of_le (x : M) {r₁ r₂ : ℝ} (H : r₁ ≤ r₂) : open_ball x r₁ ⊆ open_ball x r₂ := take y, assume ymem, lt_of_lt_of_le ymem H theorem exists_open_ball_subset_of_Open_of_mem {U : set M} (HU : Open U) {x : M} (Hx : x ∈ U) : ∃ (r : ℝ), r > 0 ∧ open_ball x r ⊆ U := begin induction HU with s sbasis s t sbasis tbasis ihs iht S Sbasis ihS, {cases sbasis with x' aux, cases aux with ε seq, have x ∈ open_ball x' ε, by rewrite -seq; exact Hx, have εpos : ε > 0, from pos_of_mem_open_ball this, have ε - dist x x' > 0, from sub_pos_of_lt `x ∈ open_ball x' ε`, existsi (ε - dist x x'), split, exact this, rewrite seq, show open_ball x (ε - dist x x') ⊆ open_ball x' ε, from take y, suppose dist y x < ε - dist x x', calc dist y x' ≤ dist y x + dist x x' : dist_triangle ... < ε - dist x x' + dist x x' : add_lt_add_right this ... = ε : sub_add_cancel}, {existsi 1, split, exact zero_lt_one, exact subset_univ _}, {cases ihs (and.left Hx) with rs aux, cases aux with rspos ballrs_sub, cases iht (and.right Hx) with rt aux, cases aux with rtpos ballrt_sub, let rmin := min rs rt, existsi rmin, split, exact lt_min rspos rtpos, have open_ball x rmin ⊆ s, from subset.trans (open_ball_subset_open_ball_of_le x !min_le_left) ballrs_sub, have open_ball x rmin ⊆ t, from subset.trans (open_ball_subset_open_ball_of_le x !min_le_right) ballrt_sub, show open_ball x (min rs rt) ⊆ s ∩ t, by apply subset_inter; repeat assumption}, cases Hx with s aux, cases aux with sS xs, cases (ihS sS xs) with r aux, cases aux with rpos ballr_sub, existsi r, split, exact rpos, show open_ball x r ⊆ ⋃₀ S, from subset.trans ballr_sub (subset_sUnion_of_mem sS) end /- limits in metric spaces -/ proposition eventually_nhds_intro {P : M → Prop} {ε : ℝ} (εpos : ε > 0) {x : M} (H : ∀ x', dist x' x < ε → P x') : eventually P (nhds x) := topology.eventually_nhds_intro (Open_open_ball x ε) (mem_open_ball x εpos) H proposition eventually_nhds_dest {P : M → Prop} {x : M} (H : eventually P (nhds x)) : ∃ ε, ε > 0 ∧ ∀ x', dist x' x < ε → P x' := obtain s [(Os : Open s) [(xs : x ∈ s) (Hs : ∀₀ x' ∈ s, P x')]], from topology.eventually_nhds_dest H, obtain ε [(εpos : ε > 0) (Hε : open_ball x ε ⊆ s)], from exists_open_ball_subset_of_Open_of_mem Os xs, exists.intro ε (and.intro εpos (take x', suppose dist x' x < ε, have x' ∈ s, from Hε this, show P x', from Hs this)) proposition eventually_nhds_iff (P : M → Prop) (x : M) : eventually P (nhds x) ↔ (∃ ε, ε > 0 ∧ ∀ x', dist x' x < ε → P x') := iff.intro eventually_nhds_dest (assume H, obtain ε [εpos Hε], from H, eventually_nhds_intro εpos Hε) proposition eventually_dist_lt_nhds (x : M) {ε : ℝ} (εpos : ε > 0) : eventually (λ x', dist x' x < ε) (nhds x) := eventually_nhds_intro εpos (λ x' H, H) proposition eventually_at_within_intro {P : M → Prop} {ε : ℝ} (εpos : ε > 0) {x : M} {s : set M} (H : ∀₀ x' ∈ s, dist x' x < ε → x' ≠ x → P x') : eventually P [at x within s] := topology.eventually_at_within_intro (Open_open_ball x ε) (mem_open_ball x εpos) (λ x' x'mem x'ne x's, H x's x'mem x'ne) proposition eventually_at_within_dest {P : M → Prop} {x : M} {s : set M} (H : eventually P [at x within s]) : ∃ ε, ε > 0 ∧ ∀₀ x' ∈ s, dist x' x < ε → x' ≠ x → P x' := obtain t [(Ot : Open t) [(xt : x ∈ t) (Ht : ∀₀ x' ∈ t, x' ≠ x → x' ∈ s → P x')]], from topology.eventually_at_within_dest H, obtain ε [(εpos : ε > 0) (Hε : open_ball x ε ⊆ t)], from exists_open_ball_subset_of_Open_of_mem Ot xt, exists.intro ε (and.intro εpos (take x', assume x's distx'x x'nex, have x' ∈ t, from Hε distx'x, show P x', from Ht this x'nex x's)) proposition eventually_at_within_iff (P : M → Prop) (x : M) (s : set M) : eventually P [at x within s] ↔ ∃ ε, ε > 0 ∧ ∀₀ x' ∈ s, dist x' x < ε → x' ≠ x → P x' := iff.intro eventually_at_within_dest (λ H, obtain ε [εpos Hε], from H, eventually_at_within_intro εpos Hε) proposition eventually_at_intro {P : M → Prop} {ε : ℝ} (εpos : ε > 0) {x : M} (H : ∀ x', dist x' x < ε → x' ≠ x → P x') : eventually P [at x] := topology.eventually_at_intro (Open_open_ball x ε) (mem_open_ball x εpos) (λ x' x'mem x'ne, H x' x'mem x'ne) proposition eventually_at_dest {P : M → Prop} {x : M} (H : eventually P [at x]) : ∃ ε, ε > 0 ∧ ∀ ⦃x'⦄, dist x' x < ε → x' ≠ x → P x' := obtain ε [εpos Hε], from eventually_at_within_dest H, exists.intro ε (and.intro εpos (λ x', Hε x' (mem_univ x'))) proposition eventually_at_iff (P : M → Prop) (x : M) : eventually P [at x] ↔ ∃ ε, ε > 0 ∧ ∀ ⦃x'⦄, dist x' x < ε → x' ≠ x → P x' := iff.intro eventually_at_dest (λ H, obtain ε [εpos Hε], from H, eventually_at_intro εpos Hε) end metric_space_M namespace metric_space variables {M : Type} [metric_space M] section approaches variables {X : Type} {F : filter X} {f : X → M} {y : M} proposition approaches_intro (H : ∀ ε, ε > 0 → eventually (λ x, dist (f x) y < ε) F) : (f ⟶ y) F := tendsto_intro (take P, assume eventuallyP, obtain ε [(εpos : ε > 0) (Hε : ∀ x', dist x' y < ε → P x')], from eventually_nhds_dest eventuallyP, show eventually (λ x, P (f x)) F, from eventually_mono (H ε εpos) (λ x Hx, Hε (f x) Hx)) proposition approaches_dest (H : (f ⟶ y) F) {ε : ℝ} (εpos : ε > 0) : eventually (λ x, dist (f x) y < ε) F := tendsto_dest H (eventually_dist_lt_nhds y εpos) variables (F f y) proposition approaches_iff : (f ⟶ y) F ↔ (∀ ε, ε > 0 → eventually (λ x, dist (f x) y < ε) F) := iff.intro approaches_dest approaches_intro end approaches -- here we full unwrap two particular kinds of convergence3 -- TODO: put these in metric space namespace? (will have similar in normed_space proposition approaches_at_infty_intro {f : ℕ → M} {y : M} (H : ∀ ε, ε > 0 → ∃ N, ∀ n, n ≥ N → dist (f n) y < ε) : f ⟶ y [at ∞] := approaches_intro (λ ε εpos, obtain N HN, from H ε εpos, eventually_at_infty_intro HN) proposition approaches_at_infty_dest {f : ℕ → M} {y : M} (H : f ⟶ y [at ∞]) ⦃ε : ℝ⦄ (εpos : ε > 0) : ∃ N, ∀ ⦃n⦄, n ≥ N → dist (f n) y < ε := have eventually (λ x, dist (f x) y < ε) [at ∞], from approaches_dest H εpos, eventually_at_infty_dest this proposition approaches_at_infty_iff (f : ℕ → M) (y : M) : f ⟶ y [at ∞] ↔ (∀ ε, ε > 0 → ∃ N, ∀ ⦃n⦄, n ≥ N → dist (f n) y < ε) := iff.intro approaches_at_infty_dest approaches_at_infty_intro section metric_space_N variables {N : Type} [metric_space N] proposition approaches_at_dest {f : M → N} {y : N} {x : M} (H : f ⟶ y [at x]) ⦃ε : ℝ⦄ (εpos : ε > 0) : ∃ δ, δ > 0 ∧ ∀ ⦃x'⦄, dist x' x < δ → x' ≠ x → dist (f x') y < ε := have eventually (λ x, dist (f x) y < ε) [at x], from approaches_dest H εpos, eventually_at_dest this proposition approaches_at_intro {f : M → N} {y : N} {x : M} (H : ∀ ε, ε > 0 → ∃ δ, δ > 0 ∧ ∀ ⦃x'⦄, dist x' x < δ → x' ≠ x → dist (f x') y < ε) : f ⟶ y [at x] := approaches_intro (λ ε εpos, obtain δ [δpos Hδ], from H ε εpos, eventually_at_intro δpos Hδ) proposition approaches_at_iff (f : M → N) (y : N) (x : M) : f ⟶ y [at x] ↔ (∀ ⦃ε⦄, ε > 0 → ∃ δ, δ > 0 ∧ ∀ ⦃x'⦄, dist x' x < δ → x' ≠ x → dist (f x') y < ε) := iff.intro approaches_at_dest approaches_at_intro end metric_space_N end metric_space -- close namespace section metric_space_M variables {M : Type} [metric_space M] -- TODO: remove this. It is only here temporarily, because it is used in normed_space (JA) -- It is used in the definition of a complete metric space below, but I think it doesn't -- have to be a class (RL) abbreviation converges_seq [class] (X : ℕ → M) : Prop := ∃ y, X ⟶ y [at ∞] -- TODO: refactor -- the same, with ≤ in place of <; easier to prove, harder to use definition approaches_at_infty_intro' {X : ℕ → M} {y : M} (H : ∀ ⦃ε : ℝ⦄, ε > 0 → ∃ N : ℕ, ∀ {n}, n ≥ N → dist (X n) y ≤ ε) : (X ⟶ y) [at ∞] := metric_space.approaches_at_infty_intro take ε, assume epos : ε > 0, have e2pos : ε / 2 > 0, from div_pos_of_pos_of_pos `ε > 0` two_pos, obtain N HN, from H e2pos, exists.intro N (take n, suppose n ≥ N, calc dist (X n) y ≤ ε / 2 : HN _ `n ≥ N` ... < ε : div_two_lt_of_pos epos) -- TODO: prove more generally proposition approaches_at_infty_unique {X : ℕ → M} {y₁ y₂ : M} (H₁ : X ⟶ y₁ [at ∞]) (H₂ : X ⟶ y₂ [at ∞]) : y₁ = y₂ := eq_of_forall_dist_le (take ε, suppose ε > 0, have e2pos : ε / 2 > 0, from div_pos_of_pos_of_pos `ε > 0` two_pos, obtain N₁ (HN₁ : ∀ {n}, n ≥ N₁ → dist (X n) y₁ < ε / 2), from metric_space.approaches_at_infty_dest H₁ e2pos, obtain N₂ (HN₂ : ∀ {n}, n ≥ N₂ → dist (X n) y₂ < ε / 2), from metric_space.approaches_at_infty_dest H₂ e2pos, let N := max N₁ N₂ in have dN₁ : dist (X N) y₁ < ε / 2, from HN₁ !le_max_left, have dN₂ : dist (X N) y₂ < ε / 2, from HN₂ !le_max_right, have dist y₁ y₂ < ε, from calc dist y₁ y₂ ≤ dist y₁ (X N) + dist (X N) y₂ : dist_triangle ... = dist (X N) y₁ + dist (X N) y₂ : dist_comm ... < ε / 2 + ε / 2 : add_lt_add dN₁ dN₂ ... = ε : add_halves, show dist y₁ y₂ ≤ ε, from le_of_lt this) /- TODO: revise definition converges_seq [class] (X : ℕ → M) : Prop := ∃ y, X ⟶ y in ℕ noncomputable definition limit_seq (X : ℕ → M) [H : converges_seq X] : M := some H proposition converges_to_limit_seq (X : ℕ → M) [H : converges_seq X] : (X ⟶ limit_seq X in ℕ) := some_spec H proposition eq_limit_of_converges_to_seq {X : ℕ → M} {y : M} (H : X ⟶ y in ℕ) : y = @limit_seq M _ X (exists.intro y H) := converges_to_seq_unique H (@converges_to_limit_seq M _ X (exists.intro y H)) proposition converges_to_seq_offset_left {X : ℕ → M} {y : M} (k : ℕ) (H : X ⟶ y in ℕ) : (λ n, X (k + n)) ⟶ y in ℕ := have aux : (λ n, X (k + n)) = (λ n, X (n + k)), from funext (take n, by rewrite add.comm), by rewrite aux; exact converges_to_seq_offset k H proposition converges_to_seq_of_converges_to_seq_offset_left {X : ℕ → M} {y : M} {k : ℕ} (H : (λ n, X (k + n)) ⟶ y in ℕ) : X ⟶ y in ℕ := have aux : (λ n, X (k + n)) = (λ n, X (n + k)), from funext (take n, by rewrite add.comm), by rewrite aux at H; exact converges_to_seq_of_converges_to_seq_offset H -/ proposition bounded_of_converges_seq {X : ℕ → M} {x : M} (H : X ⟶ x [at ∞]) : ∃ K : ℝ, ∀ n : ℕ, dist (X n) x ≤ K := have eventually (λ n, dist (X n) x < 1) [at ∞], from metric_space.approaches_dest H zero_lt_one, obtain N (HN : ∀ n, n ≥ N → dist (X n) x < 1), from eventually_at_infty_dest this, let K := max 1 (Max i ∈ '(-∞, N), dist (X i) x) in exists.intro K (take n, if Hn : n < N then show dist (X n) x ≤ K, from le.trans (le_Max _ Hn) !le_max_right else show dist (X n) x ≤ K, from le.trans (le_of_lt (HN n (le_of_not_gt Hn))) !le_max_left) proposition bounded_of_converges {A : Type} {X : A → M} {x : M} {F} (H : (X ⟶ x) F) : ∃ K : ℝ, eventually (λ n, dist (X n) x ≤ K) F := begin note H' := metric_space.approaches_dest H zero_lt_one, existsi 1, apply eventually_mono H', intro x' Hx', apply le_of_lt Hx' end /-proposition converges_to_seq_of_converges_to_seq_offset_succ {X : ℕ → M} {y : M} (H : (λ n, X (succ n)) ⟶ y [at ∞]) : X ⟶ y [at ∞] := @converges_to_seq_of_converges_to_seq_offset M _ X y 1 H-/ /- proposition converges_to_seq_offset_iff (X : ℕ → M) (y : M) (k : ℕ) : ((λ n, X (n + k)) ⟶ y in ℕ) ↔ (X ⟶ y in ℕ) := iff.intro converges_to_seq_of_converges_to_seq_offset !converges_to_seq_offset proposition converges_to_seq_offset_left_iff (X : ℕ → M) (y : M) (k : ℕ) : ((λ n, X (k + n)) ⟶ y in ℕ) ↔ (X ⟶ y in ℕ) := iff.intro converges_to_seq_of_converges_to_seq_offset_left !converges_to_seq_offset_left proposition converges_to_seq_offset_succ_iff (X : ℕ → M) (y : M) : ((λ n, X (succ n)) ⟶ y in ℕ) ↔ (X ⟶ y in ℕ) := iff.intro converges_to_seq_of_converges_to_seq_offset_succ !converges_to_seq_offset_succ -/ /- cauchy sequences -/ definition cauchy (X : ℕ → M) : Prop := ∀ ε : ℝ, ε > 0 → ∃ N, ∀ m n, m ≥ N → n ≥ N → dist (X m) (X n) < ε proposition cauchy_of_converges_seq {X : ℕ → M} (H : ∃ y, X ⟶ y [at ∞]) : cauchy X := take ε, suppose ε > 0, obtain y (Hy : X ⟶ y [at ∞]), from H, have e2pos : ε / 2 > 0, from div_pos_of_pos_of_pos `ε > 0` two_pos, have eventually (λ n, dist (X n) y < ε / 2) [at ∞], from metric_space.approaches_dest Hy e2pos, obtain N (HN : ∀ {n}, n ≥ N → dist (X n) y < ε / 2), from eventually_at_infty_dest this, exists.intro N (take m n, suppose m ≥ N, suppose n ≥ N, have dN₁ : dist (X m) y < ε / 2, from HN `m ≥ N`, have dN₂ : dist (X n) y < ε / 2, from HN `n ≥ N`, show dist (X m) (X n) < ε, from calc dist (X m) (X n) ≤ dist (X m) y + dist y (X n) : dist_triangle ... = dist (X m) y + dist (X n) y : dist_comm ... < ε / 2 + ε / 2 : add_lt_add dN₁ dN₂ ... = ε : add_halves) end metric_space_M /- convergence of a function at a point -/ section metric_space_M_N variables {M N : Type} [metric_space M] [metric_space N] -- TODO: refactor section open pnat rat theorem cnv_real_of_cnv_nat {X : ℕ → M} {c : M} (H : ∀ n : ℕ, dist (X n) c < 1 / (real.of_nat n + 1)) : ∀ ε : ℝ, ε > 0 → ∃ N : ℕ, ∀ n : ℕ, n ≥ N → dist (X n) c < ε := begin intros ε Hε, cases ex_rat_pos_lower_bound_of_pos Hε with q Hq, cases Hq with Hq1 Hq2, cases pnat_bound Hq1 with p Hp, existsi pnat.nat_of_pnat p, intros n Hn, apply lt_of_lt_of_le, apply H, apply le.trans, rotate 1, apply Hq2, have Hrat : of_rat (inv p) ≤ of_rat q, from of_rat_le_of_rat_of_le Hp, apply le.trans, rotate 1, exact Hrat, change 1 / (of_nat n + 1) ≤ of_rat ((1 : ℚ) / (rat_of_pnat p)), rewrite [of_rat_divide, of_rat_one], eapply one_div_le_one_div_of_le, krewrite -of_rat_zero, apply of_rat_lt_of_rat_of_lt, apply rat_of_pnat_is_pos, change of_nat (nat_of_pnat p) ≤ n + 1, krewrite [-real.of_nat_add], apply real.of_nat_le_of_nat_of_le, apply le_add_of_le_right, assumption end end -- TODO : refactor theorem converges_to_at_of_all_conv_seqs {f : M → N} (c : M) (l : N) (Hseq : ∀ X : ℕ → M, (eventually (λ n, X n ≠ c) [at ∞] ∧ (X ⟶ c [at ∞])) → ((λ n : ℕ, f (X n)) ⟶ l [at ∞])) : f ⟶ l [at c] := begin eapply by_contradiction, intro Hnot, cases exists_not_of_not_forall (λ H, Hnot (metric_space.approaches_at_intro H)) with ε Hε, cases and_not_of_not_implies Hε with H1 H2, note H3' := forall_not_of_not_exists H2, have H3 : ∀ δ, δ > 0 → (∃ x', dist x' c < δ ∧ x' ≠ c ∧ dist (f x') l ≥ ε), begin intro δ Hδ, cases exists_not_of_not_forall (or.resolve_left (not_or_not_of_not_and' (H3' δ)) (not_not_intro Hδ)) with x' Hx', existsi x', rewrite [2 not_implies_iff_and_not at Hx', ge_iff_not_lt], exact Hx' end, let S := λ (n : ℕ) (x : M), 0 < dist x c ∧ dist x c < 1 / (of_nat n + 1) ∧ dist (f x) l ≥ ε, have HS : ∀ n : ℕ, ∃ m : M, S n m, begin intro k, have Hpos : 0 < of_nat k + 1, from of_nat_succ_pos k, cases H3 (1 / (k + 1)) (one_div_pos_of_pos Hpos) with x' Hx', cases Hx' with Hne Hx', cases Hx' with Hdistl Hdistg, existsi x', esimp, split, apply dist_pos_of_ne, assumption, split, repeat assumption end, let X := λ n, some (HS n), have H4 : (eventually (λ n, X n ≠ c) [at ∞]) ∧ (X ⟶ c [at ∞]), begin split, {fapply @eventually_at_infty_intro, exact 0, intro n Hn, note Hspec := some_spec (HS n), esimp, esimp at Hspec, cases Hspec, apply ne_of_dist_pos, assumption}, {intro, apply metric_space.approaches_at_infty_intro, apply cnv_real_of_cnv_nat, intro m, note Hspec := some_spec (HS m), esimp, esimp at Hspec, cases Hspec with Hspec1 Hspec2, cases Hspec2, assumption} end, have H5 : (λ n, f (X n)) ⟶ l [at ∞], from Hseq X H4, note H6 := metric_space.approaches_at_infty_dest H5 H1, cases H6 with Q HQ, note HQ' := HQ !le.refl, esimp at HQ', apply absurd HQ', apply not_lt_of_ge, note H7 := some_spec (HS Q), esimp at H7, cases H7 with H71 H72, cases H72, assumption end end metric_space_M_N namespace metric_space section continuity variables {M N : Type} [Hm : metric_space M] [Hn : metric_space N] include Hm Hn open topology set -- the ε - δ definition of continuity is equivalent to the topological definition theorem continuous_at_within_intro {f : M → N} {x : M} {s : set M} (H : ∀ ⦃ε⦄, ε > 0 → ∃ δ, δ > 0 ∧ ∀ ⦃x'⦄, x' ∈ s → dist x' x < δ → dist (f x') (f x) < ε) : continuous_at_on f x s := begin intro U Uopen HfU, cases exists_open_ball_subset_of_Open_of_mem Uopen HfU with r Hr, cases Hr with Hr HUr, cases H Hr with δ Hδ, cases Hδ with Hδ Hx'δ, existsi open_ball x δ, split, apply Open_open_ball, split, apply mem_open_ball, exact Hδ, intro y Hy, apply mem_preimage, apply HUr, apply Hx'δ, apply and.right Hy, apply and.left Hy end theorem continuous_at_on_dest {f : M → N} {x : M} {s : set M} (Hfx : continuous_at_on f x s) : ∀ ⦃ε⦄, ε > 0 → ∃ δ, δ > 0 ∧ ∀ ⦃x'⦄, x' ∈ s → dist x' x < δ → dist (f x') (f x) < ε := begin intros ε Hε, cases @Hfx (open_ball (f x) ε) !Open_open_ball (mem_open_ball _ Hε) with V HV, cases HV with HV HVx, cases HVx with HVx HVf, cases exists_open_ball_subset_of_Open_of_mem HV HVx with δ Hδ, cases Hδ with Hδ Hδx, existsi δ, split, exact Hδ, intro x' Hx's Hx', apply HVf, apply and.intro, apply Hδx, exact Hx', exact Hx's end theorem continuous_at_intro {f : M → N} {x : M} (H : ∀ ⦃ε⦄, ε > 0 → ∃ δ, δ > 0 ∧ ∀ ⦃x'⦄, dist x' x < δ → dist (f x') (f x) < ε) : continuous_at f x := continuous_at_of_continuous_at_on_univ (continuous_at_within_intro (take ε, suppose ε > 0, obtain δ (Hδ : δ > 0 ∧ ∀ ⦃x'⦄, dist x' x < δ → dist (f x') (f x) < ε), from H this, exists.intro δ (and.intro (show δ > 0, from and.left Hδ) (take x' H' Hx', and.right Hδ _ Hx')))) theorem continuous_at_dest {f : M → N} {x : M} (Hfx : continuous_at f x) : ∀ ⦃ε⦄, ε > 0 → ∃ δ, δ > 0 ∧ ∀ ⦃x'⦄, dist x' x < δ → dist (f x') (f x) < ε := begin intro ε Hε, cases continuous_at_on_dest (continuous_at_on_univ_of_continuous_at Hfx) Hε with δ Hδ, existsi δ, split, exact and.left Hδ, intro x' Hx', apply and.right Hδ, apply mem_univ, apply Hx' end theorem continuous_on_intro {f : M → N} {s : set M} (H : ∀ x ⦃ε⦄, ε > 0 → ∃ δ, δ > 0 ∧ ∀ ⦃x'⦄, x' ∈ s → dist x' x < δ → dist (f x') (f x) < ε) : continuous_on f s := continuous_on_of_forall_continuous_at_on (λ x, continuous_at_within_intro (H x)) theorem continuous_on_dest {f : M → N} {s : set M} (H : continuous_on f s) {x : M} (Hxs : x ∈ s) : ∀ ⦃ε⦄, ε > 0 → ∃ δ, δ > 0 ∧ ∀ ⦃x'⦄, x' ∈ s → dist x' x < δ → dist (f x') (f x) < ε := continuous_at_on_dest (continuous_at_on_of_continuous_on H Hxs) theorem continuous_intro {f : M → N} (H : ∀ x ⦃ε⦄, ε > 0 → ∃ δ, δ > 0 ∧ ∀ ⦃x'⦄, dist x' x < δ → dist (f x') (f x) < ε) : continuous f := continuous_of_forall_continuous_at (λ x, continuous_at_intro (H x)) theorem continuous_dest {f : M → N} (H : continuous f) (x : M) : ∀ ⦃ε⦄, ε > 0 → ∃ δ, δ > 0 ∧ ∀ ⦃x'⦄, dist x' x < δ → dist (f x') (f x) < ε := continuous_at_dest (forall_continuous_at_of_continuous H x) theorem continuous_at_of_converges_to_at {f : M → N} {x : M} (Hf : f ⟶ f x [at x]) : continuous_at f x := continuous_at_intro (take ε, suppose ε > 0, obtain δ Hδ, from approaches_at_dest Hf this, exists.intro δ (and.intro (and.left Hδ) (take x', suppose dist x' x < δ, if Heq : x' = x then by rewrite [-Heq, dist_self]; assumption else (suffices dist x' x < δ, from and.right Hδ x' this Heq, this)))) theorem converges_to_at_of_continuous_at {f : M → N} {x : M} (Hf : continuous_at f x) : f ⟶ f x [at x] := approaches_at_intro (take ε, suppose ε > 0, obtain δ [δpos Hδ], from continuous_at_dest Hf this, exists.intro δ (and.intro δpos (λ x' Hx' xnex', Hδ x' Hx'))) theorem converges_seq_comp_of_converges_seq_of_cts (X : ℕ → M) [HX : converges_seq X] {f : M → N} (Hf : continuous f) : converges_seq (λ n, f (X n)) := begin cases HX with xlim Hxlim, existsi f xlim, apply approaches_at_infty_intro, intros ε Hε, let Hcont := (continuous_at_dest (forall_continuous_at_of_continuous Hf xlim)) Hε, cases Hcont with δ Hδ, cases approaches_at_infty_dest Hxlim (and.left Hδ) with B HB, existsi B, intro n Hn, apply and.right Hδ, apply HB Hn end end continuity end metric_space end analysis /- complete metric spaces -/ structure complete_metric_space [class] (M : Type) extends metricM : metric_space M : Type := (complete : ∀ X, @analysis.cauchy M metricM X → @analysis.converges_seq M metricM X) namespace analysis proposition complete (M : Type) [cmM : complete_metric_space M] {X : ℕ → M} (H : cauchy X) : converges_seq X := complete_metric_space.complete X H end analysis /- the reals form a metric space -/ noncomputable definition metric_space_real [instance] : metric_space ℝ := ⦃ metric_space, dist := λ x y, abs (x - y), dist_self := λ x, abstract by rewrite [sub_self, abs_zero] end, eq_of_dist_eq_zero := λ x y, eq_of_abs_sub_eq_zero, dist_comm := abs_sub, dist_triangle := abs_sub_le ⦄
5d6626bbf3486d2ce7b87876d52cda6ff6d8893f
8930e38ac0fae2e5e55c28d0577a8e44e2639a6d
/data/fintype.lean
65afcb2fb339b1fed33e7d59ed01f8ac1544a652
[ "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
14,039
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Finite types. -/ import data.finset algebra.big_operators data.array.lemmas data.vector2 universes u v variables {α : Type*} {β : Type*} {γ : Type*} /-- `fintype α` means that `α` is finite, i.e. there are only finitely many distinct elements of type `α`. The evidence of this is a finset `elems` (a list up to permutation without duplicates), together with a proof that everything of type `α` is in the list. -/ class fintype (α : Type*) := (elems : finset α) (complete : ∀ x : α, x ∈ elems) namespace finset variable [fintype α] /-- `univ` is the universal finite set of type `finset α` implied from the assumption `fintype α`. -/ def univ : finset α := fintype.elems α @[simp] theorem mem_univ (x : α) : x ∈ (univ : finset α) := fintype.complete x @[simp] theorem mem_univ_val : ∀ x, x ∈ (univ : finset α).1 := mem_univ theorem subset_univ (s : finset α) : s ⊆ univ := λ a _, mem_univ a theorem eq_univ_iff_forall {s : finset α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext] end finset open finset namespace fintype instance decidable_pi_fintype {α} {β : α → Type*} [fintype α] [∀a, decidable_eq (β a)] : decidable_eq (Πa, β a) := assume f g, decidable_of_iff (∀ a ∈ fintype.elems α, f a = g a) (by simp [function.funext_iff, fintype.complete]) /-- Construct a proof of `fintype α` from a universal multiset -/ def of_multiset [decidable_eq α] (s : multiset α) (H : ∀ x : α, x ∈ s) : fintype α := ⟨s.to_finset, by simpa using H⟩ /-- Construct a proof of `fintype α` from a universal list -/ def of_list [decidable_eq α] (l : list α) (H : ∀ x : α, x ∈ l) : fintype α := ⟨l.to_finset, by simpa using H⟩ theorem exists_univ_list (α) [fintype α] : ∃ l : list α, l.nodup ∧ ∀ x : α, x ∈ l := let ⟨l, e⟩ := quotient.exists_rep (@univ α _).1 in by have := and.intro univ.2 mem_univ_val; exact ⟨_, by rwa ← e at this⟩ /-- `card α` is the number of elements in `α`, defined when `α` is a fintype. -/ def card (α) [fintype α] : ℕ := (@univ α _).card /-- There is (computably) a bijection between `α` and `fin n` where `n = card α`. Since it is not unique, and depends on which permutation of the universe list is used, the bijection is wrapped in `trunc` to preserve computability. -/ def equiv_fin (α) [fintype α] [decidable_eq α] : trunc (α ≃ fin (card α)) := by unfold card finset.card; exact quot.rec_on_subsingleton (@univ α _).1 (λ l (h : ∀ x:α, x ∈ l) (nd : l.nodup), trunc.mk ⟨λ a, ⟨_, list.index_of_lt_length.2 (h a)⟩, λ i, l.nth_le i.1 i.2, λ a, by simp, λ ⟨i, h⟩, fin.eq_of_veq $ list.nodup_iff_nth_le_inj.1 nd _ _ (list.index_of_lt_length.2 (list.nth_le_mem _ _ _)) h $ by simp⟩) mem_univ_val univ.2 theorem exists_equiv_fin (α) [fintype α] : ∃ n, nonempty (α ≃ fin n) := by haveI := classical.dec_eq α; exact ⟨card α, nonempty_of_trunc (equiv_fin α)⟩ instance (α : Type*) : subsingleton (fintype α) := ⟨λ ⟨s₁, h₁⟩ ⟨s₂, h₂⟩, by congr; simp [finset.ext, h₁, h₂]⟩ protected def subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : fintype {x // p x} := ⟨⟨multiset.pmap subtype.mk s.1 (λ x, (H x).1), multiset.nodup_pmap (λ a _ b _, congr_arg subtype.val) s.2⟩, λ ⟨x, px⟩, multiset.mem_pmap.2 ⟨x, (H x).2 px, rfl⟩⟩ theorem subtype_card {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : @card {x // p x} (fintype.subtype s H) = s.card := multiset.card_pmap _ _ _ theorem card_of_subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) [fintype {x // p x}] : card {x // p x} = s.card := by rw ← subtype_card s H; congr /-- If `f : α → β` is a bijection and `α` is a fintype, then `β` is also a fintype. -/ def of_bijective [fintype α] (f : α → β) (H : function.bijective f) : fintype β := ⟨univ.map ⟨f, H.1⟩, λ b, let ⟨a, e⟩ := H.2 b in e ▸ mem_map_of_mem _ (mem_univ _)⟩ /-- If `f : α → β` is a surjection and `α` is a fintype, then `β` is also a fintype. -/ def of_surjective [fintype α] [decidable_eq β] (f : α → β) (H : function.surjective f) : fintype β := ⟨univ.image f, λ b, let ⟨a, e⟩ := H b in e ▸ mem_image_of_mem _ (mem_univ _)⟩ /-- If `f : α ≃ β` and `α` is a fintype, then `β` is also a fintype. -/ def of_equiv (α : Type*) [fintype α] (f : α ≃ β) : fintype β := of_bijective _ f.bijective theorem of_equiv_card [fintype α] (f : α ≃ β) : @card β (of_equiv α f) = card α := multiset.card_map _ _ theorem card_congr {α β} [fintype α] [fintype β] (f : α ≃ β) : card α = card β := by rw ← of_equiv_card f; congr theorem card_eq {α β} [F : fintype α] [G : fintype β] : card α = card β ↔ nonempty (α ≃ β) := ⟨λ e, match F, G, e with ⟨⟨s, nd⟩, h⟩, ⟨⟨s', nd'⟩, h'⟩, e' := begin change multiset.card s = multiset.card s' at e', revert nd nd' h h' e', refine quotient.induction_on₂ s s' (λ l₁ l₂ (nd₁ : l₁.nodup) (nd₂ : l₂.nodup) (h₁ : ∀ x, x ∈ l₁) (h₂ : ∀ x, x ∈ l₂) (e' : l₁.length = l₂.length), _), haveI := classical.dec_eq α, refine ⟨equiv.of_bijective ⟨_, _⟩⟩, { refine λ a, l₂.nth_le (l₁.index_of a) _, rw ← e', exact list.index_of_lt_length.2 (h₁ a) }, { intros a b h, simpa [h₁] using congr_arg l₁.nth (list.nodup_iff_nth_le_inj.1 nd₂ _ _ _ _ h) }, { have := classical.dec_eq β, refine λ b, ⟨l₁.nth_le (l₂.index_of b) _, _⟩, { rw e', exact list.index_of_lt_length.2 (h₂ b) }, { simp [nd₁] } } end end, λ ⟨f⟩, card_congr f⟩ end fintype instance (n : ℕ) : fintype (fin n) := ⟨⟨list.pmap fin.mk (list.range n) (λ a, list.mem_range.1), list.nodup_pmap (λ a _ b _, congr_arg fin.val) (list.nodup_range _)⟩, λ ⟨m, h⟩, list.mem_pmap.2 ⟨m, list.mem_range.2 h, rfl⟩⟩ @[simp] theorem fintype.card_fin (n : ℕ) : fintype.card (fin n) = n := by rw [fin.fintype]; simp [fintype.card, card, univ] instance : fintype empty := ⟨∅, empty.rec _⟩ @[simp] theorem fintype.univ_empty : @univ empty _ = ∅ := rfl @[simp] theorem fintype.card_empty : fintype.card empty = 0 := rfl instance : fintype unit := ⟨⟨()::0, by simp⟩, λ ⟨⟩, by simp⟩ @[simp] theorem fintype.univ_unit : @univ unit _ = {()} := rfl @[simp] theorem fintype.card_unit : fintype.card unit = 1 := rfl instance : fintype bool := ⟨⟨tt::ff::0, by simp⟩, λ x, by cases x; simp⟩ @[simp] theorem fintype.univ_bool : @univ bool _ = {ff, tt} := rfl @[simp] theorem fintype.card_bool : fintype.card bool = 2 := rfl def finset.insert_none (s : finset α) : finset (option α) := ⟨none :: s.1.map some, multiset.nodup_cons.2 ⟨by simp, multiset.nodup_map (λ a b, option.some.inj) s.2⟩⟩ @[simp] theorem finset.mem_insert_none {s : finset α} : ∀ {o : option α}, o ∈ s.insert_none ↔ ∀ a ∈ o, a ∈ s | none := iff_of_true (multiset.mem_cons_self _ _) (λ a h, by cases h) | (some a) := multiset.mem_cons.trans $ by simp; refl theorem finset.some_mem_insert_none {s : finset α} {a : α} : some a ∈ s.insert_none ↔ a ∈ s := by simp instance {α : Type*} [fintype α] : fintype (option α) := ⟨univ.insert_none, λ a, by simp⟩ @[simp] theorem fintype.card_option {α : Type*} [fintype α] : fintype.card (option α) = fintype.card α + 1 := (multiset.card_cons _ _).trans (by rw multiset.card_map; refl) instance {α : Type*} (β : α → Type*) [fintype α] [∀ a, fintype (β a)] : fintype (sigma β) := ⟨univ.sigma (λ _, univ), λ ⟨a, b⟩, by simp⟩ @[simp] theorem fintype.card_sigma {α : Type*} (β : α → Type*) [fintype α] [∀ a, fintype (β a)] : fintype.card (sigma β) = univ.sum (λ a, fintype.card (β a)) := card_sigma _ _ instance (α β : Type*) [fintype α] [fintype β] : fintype (α × β) := ⟨univ.product univ, λ ⟨a, b⟩, by simp⟩ @[simp] theorem fintype.card_prod (α β : Type*) [fintype α] [fintype β] : fintype.card (α × β) = fintype.card α * fintype.card β := card_product _ _ def fintype.fintype_prod_left {α β} [decidable_eq α] [fintype (α × β)] [nonempty β] : fintype α := ⟨(fintype.elems (α × β)).image prod.fst, assume a, let ⟨b⟩ := ‹nonempty β› in by simp; exact ⟨b, fintype.complete _⟩⟩ def fintype.fintype_prod_right {α β} [decidable_eq β] [fintype (α × β)] [nonempty α] : fintype β := ⟨(fintype.elems (α × β)).image prod.snd, assume b, let ⟨a⟩ := ‹nonempty α› in by simp; exact ⟨a, fintype.complete _⟩⟩ instance (α : Type*) [fintype α] : fintype (ulift α) := fintype.of_equiv _ equiv.ulift.symm @[simp] theorem fintype.card_ulift (α : Type*) [fintype α] : fintype.card (ulift α) = fintype.card α := fintype.of_equiv_card _ instance (α : Type u) (β : Type v) [fintype α] [fintype β] : fintype (α ⊕ β) := @fintype.of_equiv _ _ (@sigma.fintype _ (λ b, cond b (ulift α) (ulift.{(max u v) v} β)) _ (λ b, by cases b; apply ulift.fintype)) ((equiv.sum_equiv_sigma_bool _ _).symm.trans (equiv.sum_congr equiv.ulift equiv.ulift)) @[simp] theorem fintype.card_sum (α β : Type*) [fintype α] [fintype β] : fintype.card (α ⊕ β) = fintype.card α + fintype.card β := by rw [sum.fintype, fintype.of_equiv_card]; simp instance list.subtype.fintype [decidable_eq α] (l : list α) : fintype {x // x ∈ l} := fintype.of_list l.attach l.mem_attach instance multiset.subtype.fintype [decidable_eq α] (s : multiset α) : fintype {x // x ∈ s} := fintype.of_multiset s.attach s.mem_attach instance finset.subtype.fintype (s : finset α) : fintype {x // x ∈ s} := ⟨s.attach, s.mem_attach⟩ instance plift.fintype (p : Prop) [decidable p] : fintype (plift p) := ⟨if h : p then finset.singleton ⟨h⟩ else ∅, λ ⟨h⟩, by simp [h]⟩ instance Prop.fintype : fintype Prop := ⟨⟨true::false::0, by simp [true_ne_false]⟩, classical.cases (by simp) (by simp)⟩ def set_fintype {α} [fintype α] (s : set α) [decidable_pred s] : fintype s := fintype.subtype (univ.filter (∈ s)) (by simp) instance pi.fintype {α : Type*} {β : α → Type*} [fintype α] [decidable_eq α] [∀a, fintype (β a)] : fintype (Πa, β a) := @fintype.of_equiv _ _ ⟨univ.pi $ λa:α, @univ (β a) _, λ f, finset.mem_pi.2 $ λ a ha, mem_univ _⟩ ⟨λ f a, f a (mem_univ _), λ f a _, f a, λ f, rfl, λ f, rfl⟩ instance d_array.fintype {n : ℕ} {α : fin n → Type*} [∀n, fintype (α n)] : fintype (d_array n α) := fintype.of_equiv _ (equiv.d_array_equiv_fin _).symm instance array.fintype {n : ℕ} {α : Type*} [fintype α] : fintype (array n α) := d_array.fintype instance vector.fintype {α : Type*} [fintype α] {n : ℕ} : fintype (vector α n) := fintype.of_equiv _ (equiv.vector_equiv_fin _ _).symm instance quotient.fintype [fintype α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] : fintype (quotient s) := fintype.of_surjective quotient.mk (λ x, quotient.induction_on x (λ x, ⟨x, rfl⟩)) instance finset.fintype [fintype α] : fintype (finset α) := ⟨univ.powerset, λ x, finset.mem_powerset.2 (finset.subset_univ _)⟩ instance subtype.fintype [fintype α] (p : α → Prop) [decidable_pred p] : fintype {x // p x} := set_fintype _ instance set.fintype [fintype α] [decidable_eq α] : fintype (set α) := pi.fintype def quotient.fin_choice_aux {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι), (∀ i ∈ l, quotient (S i)) → @quotient (Π i ∈ l, α i) (by apply_instance) | [] f := ⟦λ i, false.elim⟧ | (i::l) f := begin refine quotient.lift_on₂ (f i (list.mem_cons_self _ _)) (quotient.fin_choice_aux l (λ j h, f j (list.mem_cons_of_mem _ h))) _ _, exact λ a l, ⟦λ j h, if e : j = i then by rw e; exact a else l _ (h.resolve_left e)⟧, refine λ a₁ l₁ a₂ l₂ h₁ h₂, quotient.sound (λ j h, _), by_cases e : j = i; simp [e], { subst j, exact h₁ }, { exact h₂ _ _ } end theorem quotient.fin_choice_aux_eq {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι) (f : ∀ i ∈ l, α i), quotient.fin_choice_aux l (λ i h, ⟦f i h⟧) = ⟦f⟧ | [] f := quotient.sound (λ i h, h.elim) | (i::l) f := begin simp [quotient.fin_choice_aux, quotient.fin_choice_aux_eq l], refine quotient.sound (λ j h, _), by_cases e : j = i; simp [e], subst j, refl end def quotient.fin_choice {ι : Type*} [fintype ι] [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] (f : ∀ i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) := quotient.lift_on (@quotient.rec_on _ _ (λ l : multiset ι, @quotient (Π i ∈ l, α i) (by apply_instance)) finset.univ.1 (λ l, quotient.fin_choice_aux l (λ i _, f i)) (λ a b h, begin have := λ a, quotient.fin_choice_aux_eq a (λ i h, quotient.out (f i)), simp [quotient.out_eq] at this, simp [this], let g := λ a:multiset ι, ⟦λ (i : ι) (h : i ∈ a), quotient.out (f i)⟧, refine eq_of_heq ((eq_rec_heq _ _).trans (_ : g a == g b)), congr' 1, exact quotient.sound h, end)) (λ f, ⟦λ i, f i (finset.mem_univ _)⟧) (λ a b h, quotient.sound $ λ i, h _ _) theorem quotient.fin_choice_eq {ι : Type*} [fintype ι] [decidable_eq ι] {α : ι → Type*} [∀ i, setoid (α i)] (f : ∀ i, α i) : quotient.fin_choice (λ i, ⟦f i⟧) = ⟦f⟧ := begin let q, swap, change quotient.lift_on q _ _ = _, have : q = ⟦λ i h, f i⟧, { dsimp [q], exact quotient.induction_on (@finset.univ ι _).1 (λ l, quotient.fin_choice_aux_eq _ _) }, simp [this], exact setoid.refl _ end
7bbc2a01fb43dbef48d6cde40d6151128a86586b
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Elab/Tactic/Calc.lean
ace04ce2cab8c38b75a19926d8e864ade03ec7dc
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
1,823
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.Calc import Lean.Elab.Tactic.ElabTerm namespace Lean.Elab.Tactic open Meta def elabCalcSteps (steps : Array Syntax) : TacticM Expr := do /- If error recovery is disabled, we disable `Term.withoutErrToSorry` -/ if (← read).recover then go else Term.withoutErrToSorry go where go : TermElabM Expr := do let e ← Term.elabCalcSteps steps Term.synthesizeSyntheticMVars (mayPostpone := false) instantiateMVars e /-- Elaborator for the `calc` tactic mode variant. -/ @[builtinTactic calcTactic] def evalCalc : Tactic := fun stx => do withMainContext do let steps := #[stx[1]] ++ stx[2].getArgs let (val, mvarIds) ← withCollectingNewGoalsFrom (tagSuffix := `calc) do let val ← elabCalcSteps steps let valType ← inferType val let target ← getMainTarget if (← isDefEq valType target) then return val else let throwFailed {α} : TacticM α := throwError "'calc' tactic failed, has type{indentExpr valType}\nbut it is expected to have type{indentExpr target}" let some (_, _, rhs) ← Term.getCalcRelation? valType | throwFailed let some (r, _, rhs') ← Term.getCalcRelation? target | throwFailed let lastStep := mkApp2 r rhs rhs' let lastStepGoal ← mkFreshExprSyntheticOpaqueMVar lastStep (tag := (← getMainTag) ++ `calc.step) let (val, valType) ← Term.mkCalcTrans val valType lastStepGoal lastStep unless (← isDefEq valType target) do throwFailed return val let val ← instantiateMVars val let mvarId ← getMainGoal mvarId.assign val replaceMainGoal mvarIds
49df8bdf25a1ebee0c4dfd289771163cb9bb4c70
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/doNotation2.lean
4a5e059b0e005752db60c48c4e467c4263a22b20
[ "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,111
lean
def f (x : Nat) : IO Nat := do IO.println "hello world" let aux (y : Nat) (z : Nat) : IO Nat := do IO.println "aux started" IO.println s!"y: {y}, z: {z}" pure (x+y) discard <| aux x (x + 1) -- It is part of the application since it is indented discard <| aux x (x -- parentheses use `withoutPosition` -1) discard <| aux x x; aux x x #eval f 10 def g (xs : List Nat) : StateT Nat Id Nat := do let mut xs := xs if xs.isEmpty then xs := [← get] dbg_trace ">>> xs: {xs}" return xs.length #eval g [1, 2, 3] |>.run' 10 #eval g [] |>.run' 10 theorem ex1 : (g [1, 2, 4, 5] |>.run' 0) = 4 := rfl theorem ex2 : (g [] |>.run' 0) = 1 := rfl def h (x : Nat) (y : Nat) : Nat := Id.run <| do let mut x := x let mut y := y if x > 0 then let y' := x + 1 x := y' else y := y + 1 return x + y theorem ex3 (y : Nat) : h 0 y = 0 + (y + 1) := rfl theorem ex4 (y : Nat) : h 1 y = (1 + 1) + y := rfl def sumOdd (xs : List Nat) (threshold : Nat) : Nat := Id.run <| do let mut sum := 0 for x in xs do if x % 2 == 1 then sum := sum + x if sum > threshold then break unless x % 2 == 1 do continue dbg_trace ">> x: {x}" return sum #eval sumOdd [1, 2, 3, 4, 5, 6, 7, 9, 11, 101] 10 theorem ex5 : sumOdd [1, 2, 3, 4, 5, 6, 7, 9, 11, 101] 10 = 16 := rfl -- We need `Id.run` because we still have `Monad Option` def find? (xs : List Nat) (p : Nat → Bool) : Option Nat := Id.run do let mut result := none for x in xs do if p x then result := x break return result def sumDiff (ps : List (Nat × Nat)) : Nat := Id.run do let mut sum := 0 for (x, y) in ps do sum := sum + x - y return sum theorem ex7 : sumDiff [(2, 1), (10, 5)] = 6 := rfl def f1 (x : Nat) : IO Unit := do let rec loop : Nat → IO Unit | 0 => pure () | x+1 => do IO.println x; loop x loop x #eval f1 10 partial def f2 (x : Nat) : IO Unit := do let rec isEven : Nat → Bool | 0 => true | x+1 => isOdd x, isOdd : Nat → Bool | 0 => false | x+1 => isEven x IO.println ("isOdd(" ++ toString x ++ "): " ++ toString (isOdd x)) #eval f2 11 #eval f2 10 def split (xs : List Nat) : List Nat × List Nat := Id.run do let mut evens := [] let mut odds := [] for x in xs.reverse do if x % 2 == 0 then evens := x :: evens else odds := x :: odds return (evens, odds) theorem ex8 : split [1, 2, 3, 4] = ([2, 4], [1, 3]) := rfl def f3 (x : Nat) : IO Bool := do let y ← cond (x == 0) (do IO.println "hello"; true) false; !y def f4 (x y : Nat) : Nat × Nat := Id.run <| do let mut (x, y) := (x, y) match x with | 0 => y := y + 1 | _ => x := x + y return (x, y) #eval f4 0 10 #eval f4 5 10 theorem ex9 (y : Nat) : f4 0 y = (0, y+1) := rfl theorem ex10 (x y : Nat) : f4 (x+1) y = ((x+1)+y, y) := rfl def f5 (x y : Nat) : Nat × Nat := Id.run <| do let mut (x, y) := (x, y) match x with | 0 => y := y + 1 | z+1 => dbg_trace "z: {z}"; x := x + y return (x, y) #eval f5 5 6 theorem ex11 (x y : Nat) : f5 (x+1) y = ((x+1)+y, y) := rfl def f6 (x : Nat) : Nat := Id.run <| do let mut x := x if x > 10 then return 0 x := x + 1 return x theorem ex12 : f6 11 = 0 := rfl theorem ex13 : f6 5 = 6 := rfl def findOdd (xs : List Nat) : Nat := Id.run <| do for x in xs do if x % 2 == 1 then return x return 0 theorem ex14 : findOdd [2, 4, 5, 8, 7] = 5 := rfl theorem ex15 : findOdd [2, 4, 8, 10] = 0 := rfl def f7 (ref : IO.Ref (Option (Nat × Nat))) : IO Nat := do let some (x, y) ← ref.get | pure 100 IO.println (toString x ++ ", " ++ toString y) return x+y def f7Test : IO Unit := do unless (← f7 (← IO.mkRef (some (10, 20)))) == 30 do throw $ IO.userError "unexpected" unless (← f7 (← IO.mkRef none)) == 100 do throw $ IO.userError "unexpected" #eval f7Test def f8 (x : Nat) : IO Nat := do let y ← if x == 0 then IO.println "x is zero" return 100 -- returns from the `do`-block else pure (x + 1) IO.println ("y: " ++ toString y) return y def f8Test : IO Unit := do unless (← f8 0) == 100 do throw $ IO.userError "unexpected" unless (← f8 1) == 2 do throw $ IO.userError "unexpected" #eval f8Test
1c85a1676d9db8fbf521ac0bf639dfd8de597adc
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/bad10.lean
b63d6739065cec501748d67a2f86bea96d39fde1
[ "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
222
lean
set_option pp::implicit true. set_option pp::colors false. variable N : Type. definition T (a : N) (f : _ -> _) (H : f a = a) : f (f _) = f _ := substp (fun x : N, f (f a) = _) (refl (f (f _))) H. print environment 1.
5d56c52112218456c03614e0e56a38f57682117a
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/continued_fractions/computation/correctness_terminating_auto.lean
9b23db090fd9bea92c1c101ddbdb05f74acd96db
[]
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
6,403
lean
/- Copyright (c) 2020 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.continued_fractions.computation.translations import Mathlib.algebra.continued_fractions.terminated_stable import Mathlib.algebra.continued_fractions.continuants_recurrence import Mathlib.order.filter.at_top_bot import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Correctness of Terminating Continued Fraction Computations (`gcf.of`) ## Summary Let us write `gcf` for `generalized_continued_fraction`. We show the correctness of the algorithm computing continued fractions (`gcf.of`) in case of termination in the following sense: At every step `n : ℕ`, we can obtain the value `v` by adding a specific residual term to the last denominator of the fraction described by `(gcf.of v).convergents' n`. The residual term will be zero exactly when the continued fraction terminated; otherwise, the residual term will be given by the fractional part stored in `gcf.int_fract_pair.stream v n`. For an example, refer to `gcf.comp_exact_value_correctness_of_stream_eq_some` and for more information about the computation process, refer to `algebra.continued_fraction.computation.basic`. ## Main definitions - `gcf.comp_exact_value` can be used to compute the exact value approximated by the continued fraction `gcf.of v` by adding a residual term as described in the summary. ## Main Theorems - `gcf.comp_exact_value_correctness_of_stream_eq_some` shows that `gcf.comp_exact_value` indeed returns the value `v` when given the convergent and fractional part as described in the summary. - `gcf.of_correctness_of_terminated_at` shows the equality `v = (gcf.of v).convergents n` if `gcf.of v` terminated at position `n`. -/ namespace generalized_continued_fraction /-- Given two continuants `pconts` and `conts` and a value `fr`, this function returns - `conts.a / conts.b` if `fr = 0` - `exact_conts.a / exact_conts.b` where `exact_conts = next_continuants 1 fr⁻¹ pconts conts` otherwise. This function can be used to compute the exact value approxmated by a continued fraction `gcf.of v` as described in lemma `comp_exact_value_correctness_of_stream_eq_some`. -/ -- if the fractional part is zero, we exactly approximated the value by the last continuants protected def comp_exact_value {K : Type u_1} [linear_ordered_field K] (pconts : pair K) (conts : pair K) (fr : K) : K := ite (fr = 0) (pair.a conts / pair.b conts) (let exact_conts : pair K := next_continuants 1 (fr⁻¹) pconts conts; pair.a exact_conts / pair.b exact_conts) -- otherwise, we have to include the fractional part in a final continuants step. /-- Just a computational lemma we need for the next main proof. -/ protected theorem comp_exact_value_correctness_of_stream_eq_some_aux_comp {K : Type u_1} [linear_ordered_field K] [floor_ring K] {a : K} (b : K) (c : K) (fract_a_ne_zero : fract a ≠ 0) : (↑(floor a) * b + c) / fract a + b = (b * a + c) / fract a := sorry /-- Shows the correctness of `comp_exact_value` in case the continued fraction `gcf.of v` did not terminate at position `n`. That is, we obtain the value `v` if we pass the two successive (auxiliary) continuants at positions `n` and `n + 1` as well as the fractional part at `int_fract_pair.stream n` to `comp_exact_value`. The correctness might be seen more readily if one uses `convergents'` to evaluate the continued fraction. Here is an example to illustrate the idea: Let `(v : ℚ) := 3.4`. We have - `gcf.int_fract_pair.stream v 0 = some ⟨3, 0.4⟩`, and - `gcf.int_fract_pair.stream v 1 = some ⟨2, 0.5⟩`. Now `(gcf.of v).convergents' 1 = 3 + 1/2`, and our fractional term at position `2` is `0.5`. We hence have `v = 3 + 1/(2 + 0.5) = 3 + 1/2.5 = 3.4`. This computation corresponds exactly to the one using the recurrence equation in `comp_exact_value`. -/ theorem comp_exact_value_correctness_of_stream_eq_some {K : Type u_1} [linear_ordered_field K] {v : K} {n : ℕ} [floor_ring K] {ifp_n : int_fract_pair K} : int_fract_pair.stream v n = some ifp_n → v = generalized_continued_fraction.comp_exact_value (continuants_aux (generalized_continued_fraction.of v) n) (continuants_aux (generalized_continued_fraction.of v) (n + 1)) (int_fract_pair.fr ifp_n) := sorry /-- The convergent of `gcf.of v` at step `n - 1` is exactly `v` if the `int_fract_pair.stream` of the corresponding continued fraction terminated at step `n`. -/ theorem of_correctness_of_nth_stream_eq_none {K : Type u_1} [linear_ordered_field K] {v : K} {n : ℕ} [floor_ring K] (nth_stream_eq_none : int_fract_pair.stream v n = none) : v = convergents (generalized_continued_fraction.of v) (n - 1) := sorry /-- If `gcf.of v` terminated at step `n`, then the `n`th convergent is exactly `v`. -/ theorem of_correctness_of_terminated_at {K : Type u_1} [linear_ordered_field K] {v : K} {n : ℕ} [floor_ring K] (terminated_at_n : terminated_at (generalized_continued_fraction.of v) n) : v = convergents (generalized_continued_fraction.of v) n := (fun (this : int_fract_pair.stream v (n + 1) = none) => of_correctness_of_nth_stream_eq_none this) (iff.elim_left of_terminated_at_n_iff_succ_nth_int_fract_pair_stream_eq_none terminated_at_n) /-- If `gcf.of v` terminates, then there is `n : ℕ` such that the `n`th convergent is exactly `v`. -/ theorem of_correctness_of_terminates {K : Type u_1} [linear_ordered_field K] {v : K} [floor_ring K] (terminates : terminates (generalized_continued_fraction.of v)) : ∃ (n : ℕ), v = convergents (generalized_continued_fraction.of v) n := exists.elim terminates fun (n : ℕ) (terminated_at_n : seq.terminated_at (s (generalized_continued_fraction.of v)) n) => exists.intro n (of_correctness_of_terminated_at terminated_at_n) /-- If `gcf.of v` terminates, then its convergents will eventually always be `v`. -/ theorem of_correctness_at_top_of_terminates {K : Type u_1} [linear_ordered_field K] {v : K} [floor_ring K] (terminates : terminates (generalized_continued_fraction.of v)) : filter.eventually (fun (n : ℕ) => v = convergents (generalized_continued_fraction.of v) n) filter.at_top := sorry end Mathlib
fd7df370f3315b7c8692eb72d1654d081b22da21
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/lie/cartan_subalgebra.lean
92ad875f91281fd3e5de35ef68048393b52d4550
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
2,057
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.nilpotent import algebra.lie.centralizer /-! # Cartan subalgebras Cartan subalgebras are one of the most important concepts in Lie theory. We define them here. The standard example is the set of diagonal matrices in the Lie algebra of matrices. ## Main definitions * `lie_subalgebra.is_cartan_subalgebra` ## Tags lie subalgebra, normalizer, idealizer, cartan subalgebra -/ universes u v w w₁ w₂ variables {R : Type u} {L : Type v} variables [comm_ring R] [lie_ring L] [lie_algebra R L] (H : lie_subalgebra R L) namespace lie_subalgebra /-- A Cartan subalgebra is a nilpotent, self-normalizing subalgebra. -/ class is_cartan_subalgebra : Prop := (nilpotent : lie_algebra.is_nilpotent R H) (self_normalizing : H.normalizer = H) instance [H.is_cartan_subalgebra] : lie_algebra.is_nilpotent R H := is_cartan_subalgebra.nilpotent @[simp] lemma centralizer_eq_self_of_is_cartan_subalgebra (H : lie_subalgebra R L) [H.is_cartan_subalgebra] : H.to_lie_submodule.centralizer = H.to_lie_submodule := by rw [← lie_submodule.coe_to_submodule_eq_iff, coe_centralizer_eq_normalizer, is_cartan_subalgebra.self_normalizing, coe_to_lie_submodule] end lie_subalgebra @[simp] lemma lie_ideal.normalizer_eq_top {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) : (I : lie_subalgebra R L).normalizer = ⊤ := begin ext x, simpa only [lie_subalgebra.mem_normalizer_iff, lie_subalgebra.mem_top, iff_true] using λ y hy, I.lie_mem hy end open lie_ideal /-- A nilpotent Lie algebra is its own Cartan subalgebra. -/ instance lie_algebra.top_is_cartan_subalgebra_of_nilpotent [lie_algebra.is_nilpotent R L] : lie_subalgebra.is_cartan_subalgebra (⊤ : lie_subalgebra R L) := { nilpotent := infer_instance, self_normalizing := by { rw [← top_coe_lie_subalgebra, normalizer_eq_top, top_coe_lie_subalgebra], }, }
3517b128473129510adcd9561f03ddf6d7f67608
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/data/list/func.lean
c858f6342dc9b2f8f2890d96cc7c9aa60a93c5b0
[ "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
11,394
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Seul Baek -/ import data.nat.basic /-! # Lists as Functions Definitions for using lists as finite representations of finitely-supported functions with domain ℕ. These include pointwise operations on lists, as well as get and set operations. ## Notations An index notation is introduced in this file for setting a particular element of a list. With `as` as a list `m` as an index, and `a` as a new element, the notation is `as {m ↦ a}`. So, for example `[1, 3, 5] {1 ↦ 9}` would result in `[1, 9, 5]` This notation is in the locale `list.func`. -/ open list universes u v w variables {α : Type u} {β : Type v} {γ : Type w} namespace list namespace func variables {a : α} variables {as as1 as2 as3 : list α} /-- Elementwise negation of a list -/ def neg [has_neg α] (as : list α) := as.map (λ a, -a) variables [inhabited α] [inhabited β] /-- Update element of a list by index. If the index is out of range, extend the list with default elements -/ @[simp] def set (a : α) : list α → ℕ → list α | (_::as) 0 := a::as | [] 0 := [a] | (h::as) (k+1) := h::(set as k) | [] (k+1) := (default α)::(set ([] : list α) k) localized "notation as ` {` m ` ↦ ` a `}` := list.func.set a as m" in list.func /-- Get element of a list by index. If the index is out of range, return the default element -/ @[simp] def get : ℕ → list α → α | _ [] := default α | 0 (a::as) := a | (n+1) (a::as) := get n as /-- Pointwise equality of lists. If lists are different lengths, compare with the default element. -/ def equiv (as1 as2 : list α) : Prop := ∀ (m : nat), get m as1 = get m as2 /-- Pointwise operations on lists. If lists are different lengths, use the default element. -/ @[simp] def pointwise (f : α → β → γ) : list α → list β → list γ | [] [] := [] | [] (b::bs) := map (f $ default α) (b::bs) | (a::as) [] := map (λ x, f x $ default β) (a::as) | (a::as) (b::bs) := (f a b)::(pointwise as bs) /-- Pointwise addition on lists. If lists are different lengths, use zero. -/ def add {α : Type u} [has_zero α] [has_add α] : list α → list α → list α := @pointwise α α α ⟨0⟩ ⟨0⟩ (+) /-- Pointwise subtraction on lists. If lists are different lengths, use zero. -/ def sub {α : Type u} [has_zero α] [has_sub α] : list α → list α → list α := @pointwise α α α ⟨0⟩ ⟨0⟩ (@has_sub.sub α _) /- set -/ lemma length_set : ∀ {m : ℕ} {as : list α}, (as {m ↦ a}).length = _root_.max as.length (m+1) | 0 [] := rfl | 0 (a::as) := by {rw max_eq_left, refl, simp [nat.le_add_right]} | (m+1) [] := by simp only [set, nat.zero_max, length, @length_set m] | (m+1) (a::as) := by simp only [set, nat.max_succ_succ, length, @length_set m] @[simp] lemma get_nil {k : ℕ} : get k [] = default α := by {cases k; refl} lemma get_eq_default_of_le : ∀ (k : ℕ) {as : list α}, as.length ≤ k → get k as = default α | 0 [] h1 := rfl | 0 (a::as) h1 := by cases h1 | (k+1) [] h1 := rfl | (k+1) (a::as) h1 := begin apply get_eq_default_of_le k, rw ← nat.succ_le_succ_iff, apply h1, end @[simp] lemma get_set {a : α} : ∀ {k : ℕ} {as : list α}, get k (as {k ↦ a}) = a | 0 as := by {cases as; refl, } | (k+1) as := by {cases as; simp [get_set]} lemma eq_get_of_mem {a : α} : ∀ {as : list α}, a ∈ as → ∃ n : nat, ∀ d : α, a = (get n as) | [] h := by cases h | (b::as) h := begin rw mem_cons_iff at h, cases h, { existsi 0, intro d, apply h }, { cases eq_get_of_mem h with n h2, existsi (n+1), apply h2 } end lemma mem_get_of_le : ∀ {n : ℕ} {as : list α}, n < as.length → get n as ∈ as | _ [] h1 := by cases h1 | 0 (a::as) _ := or.inl rfl | (n+1) (a::as) h1 := begin apply or.inr, unfold get, apply mem_get_of_le, apply nat.lt_of_succ_lt_succ h1, end lemma mem_get_of_ne_zero : ∀ {n : ℕ} {as : list α}, get n as ≠ default α → get n as ∈ as | _ [] h1 := begin exfalso, apply h1, rw get_nil end | 0 (a::as) h1 := or.inl rfl | (n+1) (a::as) h1 := begin unfold get, apply (or.inr (mem_get_of_ne_zero _)), apply h1 end lemma get_set_eq_of_ne {a : α} : ∀ {as : list α} (k : ℕ) (m : ℕ), m ≠ k → get m (as {k ↦ a}) = get m as | as 0 m h1 := by { cases m, contradiction, cases as; simp only [set, get, get_nil] } | as (k+1) m h1 := begin cases as; cases m, simp only [set, get], { have h3 : get m (nil {k ↦ a}) = default α, { rw [get_set_eq_of_ne k m, get_nil], intro hc, apply h1, simp [hc] }, apply h3 }, simp only [set, get], { apply get_set_eq_of_ne k m, intro hc, apply h1, simp [hc], } end lemma get_map {f : α → β} : ∀ {n : ℕ} {as : list α}, n < as.length → get n (as.map f) = f (get n as) | _ [] h := by cases h | 0 (a::as) h := rfl | (n+1) (a::as) h1 := begin have h2 : n < length as, { rw [← nat.succ_le_iff, ← nat.lt_succ_iff], apply h1 }, apply get_map h2, end lemma get_map' {f : α → β} {n : ℕ} {as : list α} : f (default α) = (default β) → get n (as.map f) = f (get n as) := begin intro h1, by_cases h2 : n < as.length, { apply get_map h2, }, { rw not_lt at h2, rw [get_eq_default_of_le _ h2, get_eq_default_of_le, h1], rw [length_map], apply h2 } end lemma forall_val_of_forall_mem {as : list α} {p : α → Prop} : p (default α) → (∀ x ∈ as, p x) → (∀ n, p (get n as)) := begin intros h1 h2 n, by_cases h3 : n < as.length, { apply h2 _ (mem_get_of_le h3) }, { rw not_lt at h3, rw get_eq_default_of_le _ h3, apply h1 } end /- equiv -/ lemma equiv_refl : equiv as as := λ k, rfl lemma equiv_symm : equiv as1 as2 → equiv as2 as1 := λ h1 k, (h1 k).symm lemma equiv_trans : equiv as1 as2 → equiv as2 as3 → equiv as1 as3 := λ h1 h2 k, eq.trans (h1 k) (h2 k) lemma equiv_of_eq : as1 = as2 → equiv as1 as2 := begin intro h1, rw h1, apply equiv_refl end lemma eq_of_equiv : ∀ {as1 as2 : list α}, as1.length = as2.length → equiv as1 as2 → as1 = as2 | [] [] h1 h2 := rfl | (_::_) [] h1 h2 := by cases h1 | [] (_::_) h1 h2 := by cases h1 | (a1::as1) (a2::as2) h1 h2 := begin congr, { apply h2 0 }, have h3 : as1.length = as2.length, { simpa [add_left_inj, add_comm, length] using h1 }, apply eq_of_equiv h3, intro m, apply h2 (m+1) end end func -- We want to drop the `inhabited` instances for a moment, -- so we close and open the namespace namespace func /- neg -/ @[simp] lemma get_neg [add_group α] {k : ℕ} {as : list α} : @get α ⟨0⟩ k (neg as) = -(@get α ⟨0⟩ k as) := by {unfold neg, rw (@get_map' α α ⟨0⟩), apply neg_zero} @[simp] lemma length_neg [has_neg α] (as : list α) : (neg as).length = as.length := by simp only [neg, length_map] variables [inhabited α] [inhabited β] /- pointwise -/ lemma nil_pointwise {f : α → β → γ} : ∀ bs : list β, pointwise f [] bs = bs.map (f $ default α) | [] := rfl | (b::bs) := by simp only [nil_pointwise bs, pointwise, eq_self_iff_true, and_self, map] lemma pointwise_nil {f : α → β → γ} : ∀ as : list α, pointwise f as [] = as.map (λ a, f a $ default β) | [] := rfl | (a::as) := by simp only [pointwise_nil as, pointwise, eq_self_iff_true, and_self, list.map] lemma get_pointwise [inhabited γ] {f : α → β → γ} (h1 : f (default α) (default β) = default γ) : ∀ (k : nat) (as : list α) (bs : list β), get k (pointwise f as bs) = f (get k as) (get k bs) | k [] [] := by simp only [h1, get_nil, pointwise, get] | 0 [] (b::bs) := by simp only [get_pointwise, get_nil, pointwise, get, nat.nat_zero_eq_zero, map] | (k+1) [] (b::bs) := by { have : get k (map (f $ default α) bs) = f (default α) (get k bs), { simpa [nil_pointwise, get_nil] using (get_pointwise k [] bs) }, simpa [get, get_nil, pointwise, map] } | 0 (a::as) [] := by simp only [get_pointwise, get_nil, pointwise, get, nat.nat_zero_eq_zero, map] | (k+1) (a::as) [] := by simpa [get, get_nil, pointwise, map, pointwise_nil, get_nil] using get_pointwise k as [] | 0 (a::as) (b::bs) := by simp only [pointwise, get] | (k+1) (a::as) (b::bs) := by simp only [pointwise, get, get_pointwise k] lemma length_pointwise {f : α → β → γ} : ∀ {as : list α} {bs : list β}, (pointwise f as bs).length = _root_.max as.length bs.length | [] [] := rfl | [] (b::bs) := by simp only [pointwise, length, length_map, max_eq_right (nat.zero_le (length bs + 1))] | (a::as) [] := by simp only [pointwise, length, length_map, max_eq_left (nat.zero_le (length as + 1))] | (a::as) (b::bs) := by simp only [pointwise, length, nat.max_succ_succ, @length_pointwise as bs] end func namespace func /- add -/ @[simp] lemma get_add {α : Type u} [add_monoid α] {k : ℕ} {xs ys : list α} : @get α ⟨0⟩ k (add xs ys) = ( @get α ⟨0⟩ k xs + @get α ⟨0⟩ k ys) := by {apply get_pointwise, apply zero_add} @[simp] lemma length_add {α : Type u} [has_zero α] [has_add α] {xs ys : list α} : (add xs ys).length = _root_.max xs.length ys.length := @length_pointwise α α α ⟨0⟩ ⟨0⟩ _ _ _ @[simp] lemma nil_add {α : Type u} [add_monoid α] (as : list α) : add [] as = as := begin rw [add, @nil_pointwise α α α ⟨0⟩ ⟨0⟩], apply eq.trans _ (map_id as), congr' with x, have : @default α ⟨0⟩ = 0 := rfl, rw [this, zero_add], refl end @[simp] lemma add_nil {α : Type u} [add_monoid α] (as : list α) : add as [] = as := begin rw [add, @pointwise_nil α α α ⟨0⟩ ⟨0⟩], apply eq.trans _ (map_id as), congr' with x, have : @default α ⟨0⟩ = 0 := rfl, rw [this, add_zero], refl end lemma map_add_map {α : Type u} [add_monoid α] (f g : α → α) {as : list α} : add (as.map f) (as.map g) = as.map (λ x, f x + g x) := begin apply @eq_of_equiv _ (⟨0⟩ : inhabited α), { rw [length_map, length_add, max_eq_left, length_map], apply le_of_eq, rw [length_map, length_map] }, intros m, rw [get_add], by_cases h : m < length as, { repeat {rw [@get_map α α ⟨0⟩ ⟨0⟩ _ _ _ h]} }, rw not_lt at h, repeat {rw [get_eq_default_of_le m]}; try {rw length_map, apply h}, apply zero_add end /- sub -/ @[simp] lemma get_sub {α : Type u} [add_group α] {k : ℕ} {xs ys : list α} : @get α ⟨0⟩ k (sub xs ys) = (@get α ⟨0⟩ k xs - @get α ⟨0⟩ k ys) := by {apply get_pointwise, apply sub_zero} @[simp] lemma length_sub [has_zero α] [has_sub α] {xs ys : list α} : (sub xs ys).length = _root_.max xs.length ys.length := @length_pointwise α α α ⟨0⟩ ⟨0⟩ _ _ _ @[simp] lemma nil_sub {α : Type} [add_group α] (as : list α) : sub [] as = neg as := begin rw [sub, nil_pointwise], congr' with x, have : @default α ⟨0⟩ = 0 := rfl, rw [this, zero_sub] end @[simp] lemma sub_nil {α : Type} [add_group α] (as : list α) : sub as [] = as := begin rw [sub, pointwise_nil], apply eq.trans _ (map_id as), congr' with x, have : @default α ⟨0⟩ = 0 := rfl, rw [this, sub_zero], refl end end func end list
b29adf11c8b7344e6701e5148662db4ba4a4264f
c055f4b7c29cf1aac2223bd8c1ac8d181a7c6447
/src/categories/universal/cones.lean
3f1c3bb38f08423e6f2f2b951e1dbc0304905bc0
[ "Apache-2.0" ]
permissive
rwbarton/lean-category-theory-pr
77207b6674eeec1e258ec85dea58f3bff8d27065
591847d70c6a11c4d5561cd0eaf69b1fe85a70ab
refs/heads/master
1,584,595,111,303
1,528,029,041,000
1,528,029,041,000
135,919,126
0
0
null
1,528,041,805,000
1,528,041,805,000
null
UTF-8
Lean
false
false
10,960
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 .initial import ..functor open categories open categories.functor open categories.initial namespace categories.universal universes u v variables {J : Type v} [small_category J] variables {C : Type u} [𝒞 : category.{u v} C] include 𝒞 structure Cone (F : J ↝ C) : Type (max u v) := (cone_point : C) (cone_maps : Π j : J, cone_point ⟶ (F +> j)) (commutativity : Π {j k : J}, Π f : j ⟶ k, (cone_maps j) ≫ (F &> f) = cone_maps k . obviously) make_lemma Cone.commutativity attribute [simp,ematch] Cone.commutativity_lemma variable {F : J ↝ C} structure ConeMorphism (X Y : Cone F) : Type v := (cone_morphism : X.cone_point ⟶ Y.cone_point) (commutativity : Π j : J, cone_morphism ≫ (Y.cone_maps j) = (X.cone_maps j) . obviously) make_lemma ConeMorphism.commutativity attribute [simp,ematch] ConeMorphism.commutativity_lemma @[simp,ematch] def ConeMorphism.commutativity_lemma_assoc {X Y : Cone F} (c : ConeMorphism X Y) (j : J) {Z : C} (z : (F +> j) ⟶ Z): c.cone_morphism ≫ Y.cone_maps j ≫ z = X.cone_maps j ≫ z := begin rw ← category.associativity, simp, end @[applicable] lemma ConeMorphism_componentwise_equal {X Y : Cone F} {f g : ConeMorphism X Y} (w : f.cone_morphism = g.cone_morphism) : f = g := begin induction f, induction g, dsimp at w, induction w, refl, end instance Cones (F : J ↝ C) : category.{(max u v) v} (Cone F) := { Hom := λ X Y, ConeMorphism X Y, compose := λ X Y Z f g, { cone_morphism := f.cone_morphism ≫ g.cone_morphism, commutativity := begin -- `obviously'` says: intros, simp end }, identity := λ X, { cone_morphism := 𝟙 X.cone_point, commutativity := begin -- `obviously'` says: intros, simp end }, left_identity := begin -- `obviously'` says: intros, apply categories.universal.ConeMorphism_componentwise_equal, dsimp, simp end, right_identity := begin -- `obviously'` says: intros, apply categories.universal.ConeMorphism_componentwise_equal, dsimp, simp end, associativity := begin -- `obviously'` says: intros, apply categories.universal.ConeMorphism_componentwise_equal, dsimp, simp end } @[simp] lemma Cones.identity.cone_morphism {F : J ↝ C} (c : Cone F) : (𝟙 c : ConeMorphism c c).cone_morphism = 𝟙 (c.cone_point) := by refl @[simp] lemma Cones.compose.cone_morphism {F : J ↝ C} {c d e : Cone F} (f : c ⟶ d) (g : d ⟶ e) : ((f ≫ g) : ConeMorphism c e).cone_morphism = (f : ConeMorphism c d).cone_morphism ≫ (g : ConeMorphism d e).cone_morphism := by refl section variables {D : Type u} [𝒟 : category.{u v} D] include 𝒟 definition Cones_functoriality (F : J ↝ C) (G : C ↝ D) : (Cone F) ↝ (Cone (F ⋙ G)) := { onObjects := λ X, { cone_point := G +> X.cone_point, cone_maps := λ j, G &> (X.cone_maps j), commutativity := begin -- `obviously'` says: intros, simp, erw [←Functor.functoriality_lemma, Cone.commutativity_lemma] end }, onMorphisms := λ X Y f, { cone_morphism := G &> f.cone_morphism, commutativity := begin -- `obviously'` says: intros, dsimp, erw [←Functor.functoriality_lemma, ConeMorphism.commutativity_lemma] end }, identities := begin -- `obviously'` says: intros, apply categories.universal.ConeMorphism_componentwise_equal, dsimp, simp end, functoriality := begin -- `obviously'` says: intros, apply categories.universal.ConeMorphism_componentwise_equal, dsimp, simp end } end structure Cocone (F : J ↝ C) := (cocone_point : C) (cocone_maps : Π j : J, (F +> j) ⟶ cocone_point) (commutativity : Π {j k : J}, Π f : j ⟶ k, (F &> f) ≫ (cocone_maps k) = cocone_maps j . obviously) make_lemma Cocone.commutativity attribute [simp,ematch] Cocone.commutativity_lemma structure CoconeMorphism (X Y : Cocone F) := (cocone_morphism : X.cocone_point ⟶ Y.cocone_point) (commutativity : Π j : J, (X.cocone_maps j) ≫ cocone_morphism = (Y.cocone_maps j) . obviously) make_lemma CoconeMorphism.commutativity attribute [simp,ematch] CoconeMorphism.commutativity_lemma @[simp,ematch] def CoconeMorphism.commutativity_lemma_assoc {X Y : Cocone F} (c : CoconeMorphism X Y) (j : J) {Z : C} (z : Y.cocone_point ⟶ Z): (X.cocone_maps j) ≫ c.cocone_morphism ≫ z = (Y.cocone_maps j) ≫ z := begin rw ← category.associativity, simp, end @[applicable] lemma CoconeMorphism_componentwise_equal {X Y : Cocone F} {f g : CoconeMorphism X Y} (w : f.cocone_morphism = g.cocone_morphism) : f = g := begin induction f, induction g, dsimp at w, induction w, refl, end instance Cocones (F : J ↝ C) : category.{(max u v) v} (Cocone F) := { Hom := λ X Y, CoconeMorphism X Y, compose := λ X Y Z f g, { cocone_morphism := f.cocone_morphism ≫ g.cocone_morphism, commutativity := begin -- `obviously'` says: intros, simp end }, identity := λ X, { cocone_morphism := 𝟙 X.cocone_point, commutativity := begin -- `obviously'` says: intros, simp end }, left_identity := begin -- `obviously'` says: intros, apply categories.universal.CoconeMorphism_componentwise_equal, dsimp, simp end, right_identity := begin -- `obviously'` says: intros, apply categories.universal.CoconeMorphism_componentwise_equal, dsimp, simp end, associativity := begin -- `obviously'` says: intros, apply categories.universal.CoconeMorphism_componentwise_equal, dsimp, simp end } @[simp] lemma Cocones.identity.cone_morphism {F : J ↝ C} (c : Cocone F) : (𝟙 c : CoconeMorphism c c).cocone_morphism = 𝟙 (c.cocone_point) := by refl @[simp] lemma Cocones.compose.cone_morphism {F : J ↝ C} {c d e : Cocone F} (f : c ⟶ d) (g : d ⟶ e) : ((f ≫ g) : CoconeMorphism c e).cocone_morphism = (f : CoconeMorphism c d).cocone_morphism ≫ (g : CoconeMorphism d e).cocone_morphism := by refl section variables {D : Type u} [𝒟 : category.{u v} D] include 𝒟 definition Cocones_functoriality (F : J ↝ C) (G : C ↝ D) : (Cocone F) ↝ (Cocone (F ⋙ G)) := { onObjects := λ X, { cocone_point := G +> X.cocone_point, cocone_maps := λ j, G &> (X.cocone_maps j), commutativity := begin -- `obviously'` says: intros, simp, erw [←Functor.functoriality_lemma, Cocone.commutativity_lemma] end }, onMorphisms := λ X Y f, { cocone_morphism := G &> f.cocone_morphism, commutativity := begin -- `obviously'` says: intros, dsimp, erw [←Functor.functoriality_lemma, CoconeMorphism.commutativity_lemma] end }, identities := begin -- `obviously'` says intros, apply categories.universal.CoconeMorphism_componentwise_equal, dsimp, simp end, functoriality := begin -- `obviously'` says intros, apply categories.universal.CoconeMorphism_componentwise_equal, dsimp, simp end } end definition LimitCone (F : J ↝ C) := TerminalObject (Cone F) definition ColimitCocone (F : J ↝ C) := InitialObject (Cocone F) end categories.universal namespace categories.functor universes u v variables {J : Type v} [small_category J] variables {C : Type u} [category.{u v} C] {D : Type u} [category.{u v} D] variable {F : J ↝ C} open categories.universal definition Functor.onCones (G : C ↝ D) (c : Cone F) : Cone (F ⋙ G) := (Cones_functoriality F G) +> c definition Functor.onCocones (G : C ↝ D) (c : Cocone F) : Cocone (F ⋙ G) := (Cocones_functoriality F G) +> c end categories.functor
3c56891bb6abfd6607845ff3259ded286fd0b5f1
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/constructions.lean
04f87fe0e32d220c82ea2dce52ca43af45ec1d26
[ "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
57,971
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import topology.maps import topology.locally_finite import order.filter.pi /-! # Constructions of new topological spaces from old ones This file constructs products, sums, subtypes and quotients of topological spaces and sets up their basic theory, such as criteria for maps into or out of these constructions to be continuous; descriptions of the open sets, neighborhood filters, and generators of these constructions; and their behavior with respect to embeddings and other specific classes of maps. ## Implementation note The constructed topologies are defined using induced and coinduced topologies along with the complete lattice structure on topologies. Their universal properties (for example, a map `X → Y × Z` is continuous if and only if both projections `X → Y`, `X → Z` are) follow easily using order-theoretic descriptions of continuity. With more work we can also extract descriptions of the open sets, neighborhood filters and so on. ## Tags product, sum, disjoint union, subspace, quotient space -/ noncomputable theory open topological_space set filter function open_locale classical topological_space filter universes u v variables {α : Type u} {β : Type v} {γ δ ε ζ : Type*} section constructions instance {p : α → Prop} [t : topological_space α] : topological_space (subtype p) := induced coe t instance {r : α → α → Prop} [t : topological_space α] : topological_space (quot r) := coinduced (quot.mk r) t instance {s : setoid α} [t : topological_space α] : topological_space (quotient s) := coinduced quotient.mk t instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α × β) := induced prod.fst t₁ ⊓ induced prod.snd t₂ instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α ⊕ β) := coinduced sum.inl t₁ ⊔ coinduced sum.inr t₂ instance {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (sigma β) := ⨆a, coinduced (sigma.mk a) (t₂ a) instance Pi.topological_space {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (Πa, β a) := ⨅a, induced (λf, f a) (t₂ a) instance ulift.topological_space [t : topological_space α] : topological_space (ulift.{v u} α) := t.induced ulift.down /-! ### `additive`, `multiplicative` The topology on those type synonyms is inherited without change. -/ section variables [topological_space α] open additive multiplicative instance : topological_space (additive α) := ‹topological_space α› instance : topological_space (multiplicative α) := ‹topological_space α› instance [discrete_topology α] : discrete_topology (additive α) := ‹discrete_topology α› instance [discrete_topology α] : discrete_topology (multiplicative α) := ‹discrete_topology α› lemma continuous_of_mul : continuous (of_mul : α → additive α) := continuous_id lemma continuous_to_mul : continuous (to_mul : additive α → α) := continuous_id lemma continuous_of_add : continuous (of_add : α → multiplicative α) := continuous_id lemma continuous_to_add : continuous (to_add : multiplicative α → α) := continuous_id lemma is_open_map_of_mul : is_open_map (of_mul : α → additive α) := is_open_map.id lemma is_open_map_to_mul : is_open_map (to_mul : additive α → α) := is_open_map.id lemma is_open_map_of_add : is_open_map (of_add : α → multiplicative α) := is_open_map.id lemma is_open_map_to_add : is_open_map (to_add : multiplicative α → α) := is_open_map.id lemma is_closed_map_of_mul : is_closed_map (of_mul : α → additive α) := is_closed_map.id lemma is_closed_map_to_mul : is_closed_map (to_mul : additive α → α) := is_closed_map.id lemma is_closed_map_of_add : is_closed_map (of_add : α → multiplicative α) := is_closed_map.id lemma is_closed_map_to_add : is_closed_map (to_add : multiplicative α → α) := is_closed_map.id local attribute [semireducible] nhds lemma nhds_of_mul (a : α) : 𝓝 (of_mul a) = map of_mul (𝓝 a) := rfl lemma nhds_of_add (a : α) : 𝓝 (of_add a) = map of_add (𝓝 a) := rfl lemma nhds_to_mul (a : additive α) : 𝓝 (to_mul a) = map to_mul (𝓝 a) := rfl lemma nhds_to_add (a : multiplicative α) : 𝓝 (to_add a) = map to_add (𝓝 a) := rfl end /-! ### Order dual The topology on this type synonym is inherited without change. -/ section variables [topological_space α] open order_dual instance : topological_space αᵒᵈ := ‹topological_space α› instance [discrete_topology α] : discrete_topology (αᵒᵈ) := ‹discrete_topology α› lemma continuous_to_dual : continuous (to_dual : α → αᵒᵈ) := continuous_id lemma continuous_of_dual : continuous (of_dual : αᵒᵈ → α) := continuous_id lemma is_open_map_to_dual : is_open_map (to_dual : α → αᵒᵈ) := is_open_map.id lemma is_open_map_of_dual : is_open_map (of_dual : αᵒᵈ → α) := is_open_map.id lemma is_closed_map_to_dual : is_closed_map (to_dual : α → αᵒᵈ) := is_closed_map.id lemma is_closed_map_of_dual : is_closed_map (of_dual : αᵒᵈ → α) := is_closed_map.id local attribute [semireducible] nhds lemma nhds_to_dual (a : α) : 𝓝 (to_dual a) = map to_dual (𝓝 a) := rfl lemma nhds_of_dual (a : α) : 𝓝 (of_dual a) = map of_dual (𝓝 a) := rfl end lemma quotient.preimage_mem_nhds [topological_space α] [s : setoid α] {V : set $ quotient s} {a : α} (hs : V ∈ 𝓝 (quotient.mk a)) : quotient.mk ⁻¹' V ∈ 𝓝 a := preimage_nhds_coinduced hs /-- The image of a dense set under `quotient.mk` is a dense set. -/ lemma dense.quotient [setoid α] [topological_space α] {s : set α} (H : dense s) : dense (quotient.mk '' s) := (surjective_quotient_mk α).dense_range.dense_image continuous_coinduced_rng H /-- The composition of `quotient.mk` and a function with dense range has dense range. -/ lemma dense_range.quotient [setoid α] [topological_space α] {f : β → α} (hf : dense_range f) : dense_range (quotient.mk ∘ f) := (surjective_quotient_mk α).dense_range.comp hf continuous_coinduced_rng instance {p : α → Prop} [topological_space α] [discrete_topology α] : discrete_topology (subtype p) := ⟨bot_unique $ assume s hs, ⟨coe '' s, is_open_discrete _, (set.preimage_image_eq _ subtype.coe_injective)⟩⟩ instance sum.discrete_topology [topological_space α] [topological_space β] [hα : discrete_topology α] [hβ : discrete_topology β] : discrete_topology (α ⊕ β) := ⟨by unfold sum.topological_space; simp [hα.eq_bot, hβ.eq_bot]⟩ instance sigma.discrete_topology {β : α → Type v} [Πa, topological_space (β a)] [h : Πa, discrete_topology (β a)] : discrete_topology (sigma β) := ⟨by { unfold sigma.topological_space, simp [λ a, (h a).eq_bot] }⟩ section topα variable [topological_space α] /- The 𝓝 filter and the subspace topology. -/ theorem mem_nhds_subtype (s : set α) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) : t ∈ 𝓝 a ↔ ∃ u ∈ 𝓝 (a : α), coe ⁻¹' u ⊆ t := mem_nhds_induced coe a t theorem nhds_subtype (s : set α) (a : {x // x ∈ s}) : 𝓝 a = comap coe (𝓝 (a : α)) := nhds_induced coe a lemma nhds_within_subtype_eq_bot_iff {s t : set α} {x : s} : 𝓝[(coe : s → α) ⁻¹' t] x = ⊥ ↔ 𝓝[t] (x : α) ⊓ 𝓟 s = ⊥ := by rw [inf_principal_eq_bot_iff_comap, nhds_within, nhds_within, comap_inf, comap_principal, nhds_induced] lemma nhds_ne_subtype_eq_bot_iff {S : set α} {x : S} : 𝓝[{x}ᶜ] x = ⊥ ↔ 𝓝[{x}ᶜ] (x : α) ⊓ 𝓟 S = ⊥ := by rw [← nhds_within_subtype_eq_bot_iff, preimage_compl, ← image_singleton, subtype.coe_injective.preimage_image ] lemma nhds_ne_subtype_ne_bot_iff {S : set α} {x : S} : (𝓝[{x}ᶜ] x).ne_bot ↔ (𝓝[{x}ᶜ] (x : α) ⊓ 𝓟 S).ne_bot := by rw [ne_bot_iff, ne_bot_iff, not_iff_not, nhds_ne_subtype_eq_bot_iff] lemma discrete_topology_subtype_iff {S : set α} : discrete_topology S ↔ ∀ x ∈ S, 𝓝[≠] x ⊓ 𝓟 S = ⊥ := by simp_rw [discrete_topology_iff_nhds_ne, set_coe.forall', nhds_ne_subtype_eq_bot_iff] end topα /-- A type synonym equiped with the topology whose open sets are the empty set and the sets with finite complements. -/ def cofinite_topology (α : Type*) := α namespace cofinite_topology /-- The identity equivalence between `α` and `cofinite_topology α`. -/ def of : α ≃ cofinite_topology α := equiv.refl α instance [inhabited α] : inhabited (cofinite_topology α) := { default := of default } instance : topological_space (cofinite_topology α) := { is_open := λ s, s.nonempty → set.finite sᶜ, is_open_univ := by simp, is_open_inter := λ s t, begin rintros hs ht ⟨x, hxs, hxt⟩, rw compl_inter, exact (hs ⟨x, hxs⟩).union (ht ⟨x, hxt⟩), end, is_open_sUnion := begin rintros s h ⟨x, t, hts, hzt⟩, rw set.compl_sUnion, exact set.finite.sInter (mem_image_of_mem _ hts) (h t hts ⟨x, hzt⟩), end } lemma is_open_iff {s : set (cofinite_topology α)} : is_open s ↔ (s.nonempty → (sᶜ).finite) := iff.rfl lemma is_open_iff' {s : set (cofinite_topology α)} : is_open s ↔ (s = ∅ ∨ (sᶜ).finite) := by simp only [is_open_iff, ← ne_empty_iff_nonempty, or_iff_not_imp_left] lemma is_closed_iff {s : set (cofinite_topology α)} : is_closed s ↔ s = univ ∨ s.finite := by simp [← is_open_compl_iff, is_open_iff'] lemma nhds_eq (a : cofinite_topology α) : 𝓝 a = pure a ⊔ cofinite := begin ext U, rw mem_nhds_iff, split, { rintro ⟨V, hVU, V_op, haV⟩, exact mem_sup.mpr ⟨hVU haV, mem_of_superset (V_op ⟨_, haV⟩) hVU⟩ }, { rintros ⟨hU : a ∈ U, hU' : (Uᶜ).finite⟩, exact ⟨U, subset.rfl, λ h, hU', hU⟩ } end lemma mem_nhds_iff {a : cofinite_topology α} {s : set (cofinite_topology α)} : s ∈ 𝓝 a ↔ a ∈ s ∧ sᶜ.finite := by simp [nhds_eq] end cofinite_topology end constructions section prod variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] [topological_space ε] [topological_space ζ] @[continuity] lemma continuous_fst : continuous (@prod.fst α β) := continuous_inf_dom_left continuous_induced_dom /-- Postcomposing `f` with `prod.fst` is continuous -/ lemma continuous.fst {f : α → β × γ} (hf : continuous f) : continuous (λ a : α, (f a).1) := continuous_fst.comp hf /-- Precomposing `f` with `prod.fst` is continuous -/ lemma continuous.fst' {f : α → γ} (hf : continuous f) : continuous (λ x : α × β, f x.fst) := hf.comp continuous_fst lemma continuous_at_fst {p : α × β} : continuous_at prod.fst p := continuous_fst.continuous_at /-- Postcomposing `f` with `prod.fst` is continuous at `x` -/ lemma continuous_at.fst {f : α → β × γ} {x : α} (hf : continuous_at f x) : continuous_at (λ a : α, (f a).1) x := continuous_at_fst.comp hf /-- Precomposing `f` with `prod.fst` is continuous at `(x, y)` -/ lemma continuous_at.fst' {f : α → γ} {x : α} {y : β} (hf : continuous_at f x) : continuous_at (λ x : α × β, f x.fst) (x, y) := continuous_at.comp hf continuous_at_fst /-- Precomposing `f` with `prod.fst` is continuous at `x : α × β` -/ lemma continuous_at.fst'' {f : α → γ} {x : α × β} (hf : continuous_at f x.fst) : continuous_at (λ x : α × β, f x.fst) x := hf.comp continuous_at_fst @[continuity] lemma continuous_snd : continuous (@prod.snd α β) := continuous_inf_dom_right continuous_induced_dom /-- Postcomposing `f` with `prod.snd` is continuous -/ lemma continuous.snd {f : α → β × γ} (hf : continuous f) : continuous (λ a : α, (f a).2) := continuous_snd.comp hf /-- Precomposing `f` with `prod.snd` is continuous -/ lemma continuous.snd' {f : β → γ} (hf : continuous f) : continuous (λ x : α × β, f x.snd) := hf.comp continuous_snd lemma continuous_at_snd {p : α × β} : continuous_at prod.snd p := continuous_snd.continuous_at /-- Postcomposing `f` with `prod.snd` is continuous at `x` -/ lemma continuous_at.snd {f : α → β × γ} {x : α} (hf : continuous_at f x) : continuous_at (λ a : α, (f a).2) x := continuous_at_snd.comp hf /-- Precomposing `f` with `prod.snd` is continuous at `(x, y)` -/ lemma continuous_at.snd' {f : β → γ} {x : α} {y : β} (hf : continuous_at f y) : continuous_at (λ x : α × β, f x.snd) (x, y) := continuous_at.comp hf continuous_at_snd /-- Precomposing `f` with `prod.snd` is continuous at `x : α × β` -/ lemma continuous_at.snd'' {f : β → γ} {x : α × β} (hf : continuous_at f x.snd) : continuous_at (λ x : α × β, f x.snd) x := hf.comp continuous_at_snd @[continuity] lemma continuous.prod_mk {f : γ → α} {g : γ → β} (hf : continuous f) (hg : continuous g) : continuous (λx, (f x, g x)) := continuous_inf_rng.2 ⟨continuous_induced_rng.2 hf, continuous_induced_rng.2 hg⟩ @[simp] lemma continuous_prod_mk {f : α → β} {g : α → γ} : continuous (λ x, (f x, g x)) ↔ continuous f ∧ continuous g := ⟨λ h, ⟨h.fst, h.snd⟩, λ h, h.1.prod_mk h.2⟩ @[continuity] lemma continuous.prod.mk (a : α) : continuous (λ b : β, (a, b)) := continuous_const.prod_mk continuous_id' @[continuity] lemma continuous.prod.mk_left (b : β) : continuous (λ a : α, (a, b)) := continuous_id'.prod_mk continuous_const lemma continuous.comp₂ {g : α × β → γ} (hg : continuous g) {e : δ → α} (he : continuous e) {f : δ → β} (hf : continuous f) : continuous (λ x, g (e x, f x)) := hg.comp $ he.prod_mk hf lemma continuous.comp₃ {g : α × β × γ → ε} (hg : continuous g) {e : δ → α} (he : continuous e) {f : δ → β} (hf : continuous f) {k : δ → γ} (hk : continuous k) : continuous (λ x, g (e x, f x, k x)) := hg.comp₂ he $ hf.prod_mk hk lemma continuous.comp₄ {g : α × β × γ × ζ → ε} (hg : continuous g) {e : δ → α} (he : continuous e) {f : δ → β} (hf : continuous f) {k : δ → γ} (hk : continuous k) {l : δ → ζ} (hl : continuous l) : continuous (λ x, g (e x, f x, k x, l x)) := hg.comp₃ he hf $ hk.prod_mk hl lemma continuous.prod_map {f : γ → α} {g : δ → β} (hf : continuous f) (hg : continuous g) : continuous (λ x : γ × δ, (f x.1, g x.2)) := hf.fst'.prod_mk hg.snd' /-- A version of `continuous_inf_dom_left` for binary functions -/ lemma continuous_inf_dom_left₂ {α β γ} {f : α → β → γ} {ta1 ta2 : topological_space α} {tb1 tb2 : topological_space β} {tc1 : topological_space γ} (h : by haveI := ta1; haveI := tb1; exact continuous (λ p : α × β, f p.1 p.2)) : by haveI := ta1 ⊓ ta2; haveI := tb1 ⊓ tb2; exact continuous (λ p : α × β, f p.1 p.2) := begin have ha := @continuous_inf_dom_left _ _ id ta1 ta2 ta1 (@continuous_id _ (id _)), have hb := @continuous_inf_dom_left _ _ id tb1 tb2 tb1 (@continuous_id _ (id _)), have h_continuous_id := @continuous.prod_map _ _ _ _ ta1 tb1 (ta1 ⊓ ta2) (tb1 ⊓ tb2) _ _ ha hb, exact @continuous.comp _ _ _ (id _) (id _) _ _ _ h h_continuous_id, end /-- A version of `continuous_inf_dom_right` for binary functions -/ lemma continuous_inf_dom_right₂ {α β γ} {f : α → β → γ} {ta1 ta2 : topological_space α} {tb1 tb2 : topological_space β} {tc1 : topological_space γ} (h : by haveI := ta2; haveI := tb2; exact continuous (λ p : α × β, f p.1 p.2)) : by haveI := ta1 ⊓ ta2; haveI := tb1 ⊓ tb2; exact continuous (λ p : α × β, f p.1 p.2) := begin have ha := @continuous_inf_dom_right _ _ id ta1 ta2 ta2 (@continuous_id _ (id _)), have hb := @continuous_inf_dom_right _ _ id tb1 tb2 tb2 (@continuous_id _ (id _)), have h_continuous_id := @continuous.prod_map _ _ _ _ ta2 tb2 (ta1 ⊓ ta2) (tb1 ⊓ tb2) _ _ ha hb, exact @continuous.comp _ _ _ (id _) (id _) _ _ _ h h_continuous_id, end /-- A version of `continuous_Inf_dom` for binary functions -/ lemma continuous_Inf_dom₂ {α β γ} {f : α → β → γ} {tas : set (topological_space α)} {tbs : set (topological_space β)} {ta : topological_space α} {tb : topological_space β} {tc : topological_space γ} (ha : ta ∈ tas) (hb : tb ∈ tbs) (hf : continuous (λ p : α × β, f p.1 p.2)): by haveI := Inf tas; haveI := Inf tbs; exact @continuous _ _ _ tc (λ p : α × β, f p.1 p.2) := begin let t : topological_space (α × β) := prod.topological_space, have ha := continuous_Inf_dom ha continuous_id, have hb := continuous_Inf_dom hb continuous_id, have h_continuous_id := @continuous.prod_map _ _ _ _ ta tb (Inf tas) (Inf tbs) _ _ ha hb, exact @continuous.comp _ _ _ (id _) (id _) _ _ _ hf h_continuous_id, end lemma filter.eventually.prod_inl_nhds {p : α → Prop} {a : α} (h : ∀ᶠ x in 𝓝 a, p x) (b : β) : ∀ᶠ x in 𝓝 (a, b), p (x : α × β).1 := continuous_at_fst h lemma filter.eventually.prod_inr_nhds {p : β → Prop} {b : β} (h : ∀ᶠ x in 𝓝 b, p x) (a : α) : ∀ᶠ x in 𝓝 (a, b), p (x : α × β).2 := continuous_at_snd h lemma filter.eventually.prod_mk_nhds {pa : α → Prop} {a} (ha : ∀ᶠ x in 𝓝 a, pa x) {pb : β → Prop} {b} (hb : ∀ᶠ y in 𝓝 b, pb y) : ∀ᶠ p in 𝓝 (a, b), pa (p : α × β).1 ∧ pb p.2 := (ha.prod_inl_nhds b).and (hb.prod_inr_nhds a) lemma continuous_swap : continuous (prod.swap : α × β → β × α) := continuous_snd.prod_mk continuous_fst lemma continuous_uncurry_left {f : α → β → γ} (a : α) (h : continuous (uncurry f)) : continuous (f a) := show continuous (uncurry f ∘ (λ b, (a, b))), from h.comp (by continuity) lemma continuous_uncurry_right {f : α → β → γ} (b : β) (h : continuous (uncurry f)) : continuous (λ a, f a b) := show continuous (uncurry f ∘ (λ a, (a, b))), from h.comp (by continuity) lemma continuous_curry {g : α × β → γ} (a : α) (h : continuous g) : continuous (curry g a) := show continuous (g ∘ (λ b, (a, b))), from h.comp (by continuity) lemma is_open.prod {s : set α} {t : set β} (hs : is_open s) (ht : is_open t) : is_open (s ×ˢ t) := (hs.preimage continuous_fst).inter (ht.preimage continuous_snd) lemma nhds_prod_eq {a : α} {b : β} : 𝓝 (a, b) = 𝓝 a ×ᶠ 𝓝 b := by rw [filter.prod, prod.topological_space, nhds_inf, nhds_induced, nhds_induced] /-- If a function `f x y` is such that `y ↦ f x y` is continuous for all `x`, and `x` lives in a discrete space, then `f` is continuous. -/ lemma continuous_uncurry_of_discrete_topology [discrete_topology α] {f : α → β → γ} (hf : ∀ a, continuous (f a)) : continuous (uncurry f) := begin apply continuous_iff_continuous_at.2, rintros ⟨a, x⟩, change map _ _ ≤ _, rw [nhds_prod_eq, nhds_discrete, filter.map_pure_prod], exact (hf a).continuous_at end lemma mem_nhds_prod_iff {a : α} {b : β} {s : set (α × β)} : s ∈ 𝓝 (a, b) ↔ ∃ (u ∈ 𝓝 a) (v ∈ 𝓝 b), u ×ˢ v ⊆ s := by rw [nhds_prod_eq, mem_prod_iff] lemma mem_nhds_prod_iff' {a : α} {b : β} {s : set (α × β)} : s ∈ 𝓝 (a, b) ↔ ∃ (u : set α) (v : set β), is_open u ∧ a ∈ u ∧ is_open v ∧ b ∈ v ∧ u ×ˢ v ⊆ s := begin rw mem_nhds_prod_iff, split, { rintros ⟨u, Hu, v, Hv, h⟩, rcases mem_nhds_iff.1 Hu with ⟨u', u'u, u'_open, Hu'⟩, rcases mem_nhds_iff.1 Hv with ⟨v', v'v, v'_open, Hv'⟩, exact ⟨u', v', u'_open, Hu', v'_open, Hv', (set.prod_mono u'u v'v).trans h⟩ }, { rintros ⟨u, v, u_open, au, v_open, bv, huv⟩, exact ⟨u, u_open.mem_nhds au, v, v_open.mem_nhds bv, huv⟩ } end lemma _root_.prod.tendsto_iff {α} (seq : α → β × γ) {f : filter α} (x : β × γ) : tendsto seq f (𝓝 x) ↔ tendsto (λ n, (seq n).fst) f (𝓝 x.fst) ∧ tendsto (λ n, (seq n).snd) f (𝓝 x.snd) := by { cases x, rw [nhds_prod_eq, filter.tendsto_prod_iff'], } lemma filter.has_basis.prod_nhds {ιa ιb : Type*} {pa : ιa → Prop} {pb : ιb → Prop} {sa : ιa → set α} {sb : ιb → set β} {a : α} {b : β} (ha : (𝓝 a).has_basis pa sa) (hb : (𝓝 b).has_basis pb sb) : (𝓝 (a, b)).has_basis (λ i : ιa × ιb, pa i.1 ∧ pb i.2) (λ i, sa i.1 ×ˢ sb i.2) := by { rw nhds_prod_eq, exact ha.prod hb } lemma filter.has_basis.prod_nhds' {ιa ιb : Type*} {pa : ιa → Prop} {pb : ιb → Prop} {sa : ιa → set α} {sb : ιb → set β} {ab : α × β} (ha : (𝓝 ab.1).has_basis pa sa) (hb : (𝓝 ab.2).has_basis pb sb) : (𝓝 ab).has_basis (λ i : ιa × ιb, pa i.1 ∧ pb i.2) (λ i, sa i.1 ×ˢ sb i.2) := by { cases ab, exact ha.prod_nhds hb } instance [discrete_topology α] [discrete_topology β] : discrete_topology (α × β) := ⟨eq_of_nhds_eq_nhds $ assume ⟨a, b⟩, by rw [nhds_prod_eq, nhds_discrete α, nhds_discrete β, nhds_bot, filter.prod_pure_pure]⟩ lemma prod_mem_nhds_iff {s : set α} {t : set β} {a : α} {b : β} : s ×ˢ t ∈ 𝓝 (a, b) ↔ s ∈ 𝓝 a ∧ t ∈ 𝓝 b := by rw [nhds_prod_eq, prod_mem_prod_iff] lemma prod_mem_nhds {s : set α} {t : set β} {a : α} {b : β} (ha : s ∈ 𝓝 a) (hb : t ∈ 𝓝 b) : s ×ˢ t ∈ 𝓝 (a, b) := prod_mem_nhds_iff.2 ⟨ha, hb⟩ lemma filter.eventually.prod_nhds {p : α → Prop} {q : β → Prop} {a : α} {b : β} (ha : ∀ᶠ x in 𝓝 a, p x) (hb : ∀ᶠ y in 𝓝 b, q y) : ∀ᶠ z : α × β in 𝓝 (a, b), p z.1 ∧ q z.2 := prod_mem_nhds ha hb lemma nhds_swap (a : α) (b : β) : 𝓝 (a, b) = (𝓝 (b, a)).map prod.swap := by rw [nhds_prod_eq, filter.prod_comm, nhds_prod_eq]; refl lemma filter.tendsto.prod_mk_nhds {γ} {a : α} {b : β} {f : filter γ} {ma : γ → α} {mb : γ → β} (ha : tendsto ma f (𝓝 a)) (hb : tendsto mb f (𝓝 b)) : tendsto (λc, (ma c, mb c)) f (𝓝 (a, b)) := by rw [nhds_prod_eq]; exact filter.tendsto.prod_mk ha hb lemma filter.eventually.curry_nhds {p : α × β → Prop} {x : α} {y : β} (h : ∀ᶠ x in 𝓝 (x, y), p x) : ∀ᶠ x' in 𝓝 x, ∀ᶠ y' in 𝓝 y, p (x', y') := by { rw [nhds_prod_eq] at h, exact h.curry } lemma continuous_at.prod {f : α → β} {g : α → γ} {x : α} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (λx, (f x, g x)) x := hf.prod_mk_nhds hg lemma continuous_at.prod_map {f : α → γ} {g : β → δ} {p : α × β} (hf : continuous_at f p.fst) (hg : continuous_at g p.snd) : continuous_at (λ p : α × β, (f p.1, g p.2)) p := hf.fst''.prod hg.snd'' lemma continuous_at.prod_map' {f : α → γ} {g : β → δ} {x : α} {y : β} (hf : continuous_at f x) (hg : continuous_at g y) : continuous_at (λ p : α × β, (f p.1, g p.2)) (x, y) := hf.fst'.prod hg.snd' lemma prod_generate_from_generate_from_eq {α β : Type*} {s : set (set α)} {t : set (set β)} (hs : ⋃₀ s = univ) (ht : ⋃₀ t = univ) : @prod.topological_space α β (generate_from s) (generate_from t) = generate_from {g | ∃ u ∈ s, ∃ v ∈ t, g = u ×ˢ v} := let G := generate_from {g | ∃ u ∈ s, ∃ v ∈ t, g = u ×ˢ v} in le_antisymm (le_generate_from $ λ g ⟨u, hu, v, hv, g_eq⟩, g_eq.symm ▸ @is_open.prod _ _ (generate_from s) (generate_from t) _ _ (generate_open.basic _ hu) (generate_open.basic _ hv)) (le_inf (coinduced_le_iff_le_induced.mp $ le_generate_from $ λ u hu, have (⋃ v ∈ t, u ×ˢ v) = prod.fst ⁻¹' u, by simp_rw [← prod_Union, ← sUnion_eq_bUnion, ht, prod_univ], show G.is_open (prod.fst ⁻¹' u), by { rw [← this], exactI is_open_Union (λ v, is_open_Union $ λ hv, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩) }) (coinduced_le_iff_le_induced.mp $ le_generate_from $ λ v hv, have (⋃ u ∈ s, u ×ˢ v) = prod.snd ⁻¹' v, by simp_rw [← Union_prod_const, ← sUnion_eq_bUnion, hs, univ_prod], show G.is_open (prod.snd ⁻¹' v), by { rw [← this], exactI is_open_Union (λ u, is_open_Union $ λ hu, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩) })) lemma prod_eq_generate_from : prod.topological_space = generate_from {g | ∃(s:set α) (t:set β), is_open s ∧ is_open t ∧ g = s ×ˢ t} := le_antisymm (le_generate_from $ λ g ⟨s, t, hs, ht, g_eq⟩, g_eq.symm ▸ hs.prod ht) (le_inf (ball_image_of_ball $ λt ht, generate_open.basic _ ⟨t, univ, by simpa [set.prod_eq] using ht⟩) (ball_image_of_ball $ λt ht, generate_open.basic _ ⟨univ, t, by simpa [set.prod_eq] using ht⟩)) lemma is_open_prod_iff {s : set (α × β)} : is_open s ↔ (∀a b, (a, b) ∈ s → ∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ a ∈ u ∧ b ∈ v ∧ u ×ˢ v ⊆ s) := begin rw [is_open_iff_nhds], simp_rw [le_principal_iff, prod.forall, ((nhds_basis_opens _).prod_nhds (nhds_basis_opens _)).mem_iff, prod.exists, exists_prop], simp only [and_assoc, and.left_comm] end /-- A product of induced topologies is induced by the product map -/ lemma prod_induced_induced {α γ : Type*} (f : α → β) (g : γ → δ) : @prod.topological_space α γ (induced f ‹_›) (induced g ‹_›) = induced (λ p, (f p.1, g p.2)) prod.topological_space := by simp_rw [prod.topological_space, induced_inf, induced_compose] lemma continuous_uncurry_of_discrete_topology_left [discrete_topology α] {f : α → β → γ} (h : ∀ a, continuous (f a)) : continuous (uncurry f) := continuous_iff_continuous_at.2 $ λ ⟨a, b⟩, by simp only [continuous_at, nhds_prod_eq, nhds_discrete α, pure_prod, tendsto_map'_iff, (∘), uncurry, (h a).tendsto] /-- Given a neighborhood `s` of `(x, x)`, then `(x, x)` has a square open neighborhood that is a subset of `s`. -/ lemma exists_nhds_square {s : set (α × α)} {x : α} (hx : s ∈ 𝓝 (x, x)) : ∃ U : set α, is_open U ∧ x ∈ U ∧ U ×ˢ U ⊆ s := by simpa [nhds_prod_eq, (nhds_basis_opens x).prod_self.mem_iff, and.assoc, and.left_comm] using hx /-- `prod.fst` maps neighborhood of `x : α × β` within the section `prod.snd ⁻¹' {x.2}` to `𝓝 x.1`. -/ lemma map_fst_nhds_within (x : α × β) : map prod.fst (𝓝[prod.snd ⁻¹' {x.2}] x) = 𝓝 x.1 := begin refine le_antisymm (continuous_at_fst.mono_left inf_le_left) (λ s hs, _), rcases x with ⟨x, y⟩, rw [mem_map, nhds_within, mem_inf_principal, mem_nhds_prod_iff] at hs, rcases hs with ⟨u, hu, v, hv, H⟩, simp only [prod_subset_iff, mem_singleton_iff, mem_set_of_eq, mem_preimage] at H, exact mem_of_superset hu (λ z hz, H _ hz _ (mem_of_mem_nhds hv) rfl) end @[simp] lemma map_fst_nhds (x : α × β) : map prod.fst (𝓝 x) = 𝓝 x.1 := le_antisymm continuous_at_fst $ (map_fst_nhds_within x).symm.trans_le (map_mono inf_le_left) /-- The first projection in a product of topological spaces sends open sets to open sets. -/ lemma is_open_map_fst : is_open_map (@prod.fst α β) := is_open_map_iff_nhds_le.2 $ λ x, (map_fst_nhds x).ge /-- `prod.snd` maps neighborhood of `x : α × β` within the section `prod.fst ⁻¹' {x.1}` to `𝓝 x.2`. -/ lemma map_snd_nhds_within (x : α × β) : map prod.snd (𝓝[prod.fst ⁻¹' {x.1}] x) = 𝓝 x.2 := begin refine le_antisymm (continuous_at_snd.mono_left inf_le_left) (λ s hs, _), rcases x with ⟨x, y⟩, rw [mem_map, nhds_within, mem_inf_principal, mem_nhds_prod_iff] at hs, rcases hs with ⟨u, hu, v, hv, H⟩, simp only [prod_subset_iff, mem_singleton_iff, mem_set_of_eq, mem_preimage] at H, exact mem_of_superset hv (λ z hz, H _ (mem_of_mem_nhds hu) _ hz rfl) end @[simp] lemma map_snd_nhds (x : α × β) : map prod.snd (𝓝 x) = 𝓝 x.2 := le_antisymm continuous_at_snd $ (map_snd_nhds_within x).symm.trans_le (map_mono inf_le_left) /-- The second projection in a product of topological spaces sends open sets to open sets. -/ lemma is_open_map_snd : is_open_map (@prod.snd α β) := is_open_map_iff_nhds_le.2 $ λ x, (map_snd_nhds x).ge /-- A product set is open in a product space if and only if each factor is open, or one of them is empty -/ lemma is_open_prod_iff' {s : set α} {t : set β} : is_open (s ×ˢ t) ↔ (is_open s ∧ is_open t) ∨ (s = ∅) ∨ (t = ∅) := begin cases (s ×ˢ t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.1 h] }, { have st : s.nonempty ∧ t.nonempty, from prod_nonempty_iff.1 h, split, { assume H : is_open (s ×ˢ t), refine or.inl ⟨_, _⟩, show is_open s, { rw ← fst_image_prod s st.2, exact is_open_map_fst _ H }, show is_open t, { rw ← snd_image_prod st.1 t, exact is_open_map_snd _ H } }, { assume H, simp only [st.1.ne_empty, st.2.ne_empty, not_false_iff, or_false] at H, exact H.1.prod H.2 } } end lemma closure_prod_eq {s : set α} {t : set β} : closure (s ×ˢ t) = closure s ×ˢ closure t := set.ext $ assume ⟨a, b⟩, have (𝓝 a ×ᶠ 𝓝 b) ⊓ 𝓟 (s ×ˢ t) = (𝓝 a ⊓ 𝓟 s) ×ᶠ (𝓝 b ⊓ 𝓟 t), by rw [←prod_inf_prod, prod_principal_principal], by simp [closure_eq_cluster_pts, cluster_pt, nhds_prod_eq, this]; exact prod_ne_bot lemma interior_prod_eq (s : set α) (t : set β) : interior (s ×ˢ t) = interior s ×ˢ interior t := set.ext $ λ ⟨a, b⟩, by simp only [mem_interior_iff_mem_nhds, mem_prod, prod_mem_nhds_iff] lemma frontier_prod_eq (s : set α) (t : set β) : frontier (s ×ˢ t) = closure s ×ˢ frontier t ∪ frontier s ×ˢ closure t := by simp only [frontier, closure_prod_eq, interior_prod_eq, prod_diff_prod] @[simp] lemma frontier_prod_univ_eq (s : set α) : frontier (s ×ˢ (univ : set β)) = frontier s ×ˢ univ := by simp [frontier_prod_eq] @[simp] lemma frontier_univ_prod_eq (s : set β) : frontier ((univ : set α) ×ˢ s) = univ ×ˢ frontier s := by simp [frontier_prod_eq] lemma map_mem_closure₂ {f : α → β → γ} {a : α} {b : β} {s : set α} {t : set β} {u : set γ} (hf : continuous (uncurry f)) (ha : a ∈ closure s) (hb : b ∈ closure t) (h : ∀ (a ∈ s) (b ∈ t), f a b ∈ u) : f a b ∈ closure u := have H₁ : (a, b) ∈ closure (s ×ˢ t), by simpa only [closure_prod_eq] using mk_mem_prod ha hb, have H₂ : maps_to (uncurry f) (s ×ˢ t) u, from forall_prod_set.2 h, H₂.closure hf H₁ lemma is_closed.prod {s₁ : set α} {s₂ : set β} (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ×ˢ s₂) := closure_eq_iff_is_closed.mp $ by simp only [h₁.closure_eq, h₂.closure_eq, closure_prod_eq] /-- The product of two dense sets is a dense set. -/ lemma dense.prod {s : set α} {t : set β} (hs : dense s) (ht : dense t) : dense (s ×ˢ t) := λ x, by { rw closure_prod_eq, exact ⟨hs x.1, ht x.2⟩ } /-- If `f` and `g` are maps with dense range, then `prod.map f g` has dense range. -/ lemma dense_range.prod_map {ι : Type*} {κ : Type*} {f : ι → β} {g : κ → γ} (hf : dense_range f) (hg : dense_range g) : dense_range (prod.map f g) := by simpa only [dense_range, prod_range_range_eq] using hf.prod hg lemma inducing.prod_mk {f : α → β} {g : γ → δ} (hf : inducing f) (hg : inducing g) : inducing (λx:α×γ, (f x.1, g x.2)) := ⟨by rw [prod.topological_space, prod.topological_space, hf.induced, hg.induced, induced_compose, induced_compose, induced_inf, induced_compose, induced_compose]⟩ lemma embedding.prod_mk {f : α → β} {g : γ → δ} (hf : embedding f) (hg : embedding g) : embedding (λx:α×γ, (f x.1, g x.2)) := { inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, by simp; exact assume h₁ h₂, ⟨hf.inj h₁, hg.inj h₂⟩, ..hf.to_inducing.prod_mk hg.to_inducing } protected lemma is_open_map.prod {f : α → β} {g : γ → δ} (hf : is_open_map f) (hg : is_open_map g) : is_open_map (λ p : α × γ, (f p.1, g p.2)) := begin rw [is_open_map_iff_nhds_le], rintros ⟨a, b⟩, rw [nhds_prod_eq, nhds_prod_eq, ← filter.prod_map_map_eq], exact filter.prod_mono (hf.nhds_le a) (hg.nhds_le b) end protected lemma open_embedding.prod {f : α → β} {g : γ → δ} (hf : open_embedding f) (hg : open_embedding g) : open_embedding (λ x : α × γ, (f x.1, g x.2)) := open_embedding_of_embedding_open (hf.1.prod_mk hg.1) (hf.is_open_map.prod hg.is_open_map) lemma embedding_graph {f : α → β} (hf : continuous f) : embedding (λ x, (x, f x)) := embedding_of_embedding_compose (continuous_id.prod_mk hf) continuous_fst embedding_id end prod section sum open sum variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] @[continuity] lemma continuous_inl : continuous (@inl α β) := continuous_sup_rng_left continuous_coinduced_rng @[continuity] lemma continuous_inr : continuous (@inr α β) := continuous_sup_rng_right continuous_coinduced_rng lemma is_open_sum_iff {s : set (α ⊕ β)} : is_open s ↔ is_open (inl ⁻¹' s) ∧ is_open (inr ⁻¹' s) := iff.rfl lemma is_open_map_inl : is_open_map (@inl α β) := λ u hu, by simpa [is_open_sum_iff, preimage_image_eq u sum.inl_injective] lemma is_open_map_inr : is_open_map (@inr α β) := λ u hu, by simpa [is_open_sum_iff, preimage_image_eq u sum.inr_injective] lemma open_embedding_inl : open_embedding (@inl α β) := open_embedding_of_continuous_injective_open continuous_inl inl_injective is_open_map_inl lemma open_embedding_inr : open_embedding (@inr α β) := open_embedding_of_continuous_injective_open continuous_inr inr_injective is_open_map_inr lemma embedding_inl : embedding (@inl α β) := open_embedding_inl.1 lemma embedding_inr : embedding (@inr α β) := open_embedding_inr.1 lemma is_open_range_inl : is_open (range (inl : α → α ⊕ β)) := open_embedding_inl.2 lemma is_open_range_inr : is_open (range (inr : β → α ⊕ β)) := open_embedding_inr.2 lemma is_closed_range_inl : is_closed (range (inl : α → α ⊕ β)) := by { rw [← is_open_compl_iff, compl_range_inl], exact is_open_range_inr } lemma is_closed_range_inr : is_closed (range (inr : β → α ⊕ β)) := by { rw [← is_open_compl_iff, compl_range_inr], exact is_open_range_inl } lemma closed_embedding_inl : closed_embedding (inl : α → α ⊕ β) := ⟨embedding_inl, is_closed_range_inl⟩ lemma closed_embedding_inr : closed_embedding (inr : β → α ⊕ β) := ⟨embedding_inr, is_closed_range_inr⟩ lemma nhds_inl (x : α) : 𝓝 (inl x : α ⊕ β) = map inl (𝓝 x) := (open_embedding_inl.map_nhds_eq _).symm lemma nhds_inr (x : β) : 𝓝 (inr x : α ⊕ β) = map inr (𝓝 x) := (open_embedding_inr.map_nhds_eq _).symm lemma continuous_sum_elim {f : α → γ} {g : β → γ} : continuous (sum.elim f g) ↔ continuous f ∧ continuous g := by simp only [continuous_sup_dom, continuous_coinduced_dom, sum.elim_comp_inl, sum.elim_comp_inr] @[continuity] lemma continuous.sum_elim {f : α → γ} {g : β → γ} (hf : continuous f) (hg : continuous g) : continuous (sum.elim f g) := continuous_sum_elim.2 ⟨hf, hg⟩ @[simp] lemma continuous_sum_map {f : α → β} {g : γ → δ} : continuous (sum.map f g) ↔ continuous f ∧ continuous g := continuous_sum_elim.trans $ embedding_inl.continuous_iff.symm.and embedding_inr.continuous_iff.symm @[continuity] lemma continuous.sum_map {f : α → β} {g : γ → δ} (hf : continuous f) (hg : continuous g) : continuous (sum.map f g) := continuous_sum_map.2 ⟨hf, hg⟩ lemma is_open_map_sum {f : α ⊕ β → γ} : is_open_map f ↔ is_open_map (λ a, f (inl a)) ∧ is_open_map (λ b, f (inr b)) := by simp only [is_open_map_iff_nhds_le, sum.forall, nhds_inl, nhds_inr, filter.map_map] @[simp] lemma is_open_map_sum_elim {f : α → γ} {g : β → γ} : is_open_map (sum.elim f g) ↔ is_open_map f ∧ is_open_map g := by simp only [is_open_map_sum, elim_inl, elim_inr] lemma is_open_map.sum_elim {f : α → γ} {g : β → γ} (hf : is_open_map f) (hg : is_open_map g) : is_open_map (sum.elim f g) := is_open_map_sum_elim.2 ⟨hf, hg⟩ end sum section subtype variables [topological_space α] [topological_space β] [topological_space γ] {p : α → Prop} lemma inducing_coe {b : set β} : inducing (coe : b → β) := ⟨rfl⟩ lemma inducing.of_cod_restrict {f : α → β} {b : set β} (hb : ∀ a, f a ∈ b) (h : inducing (b.cod_restrict f hb)) : inducing f := inducing_coe.comp h lemma embedding_subtype_coe : embedding (coe : subtype p → α) := ⟨⟨rfl⟩, subtype.coe_injective⟩ lemma closed_embedding_subtype_coe (h : is_closed {a | p a}) : closed_embedding (coe : subtype p → α) := ⟨embedding_subtype_coe, by rwa [subtype.range_coe_subtype]⟩ @[continuity] lemma continuous_subtype_val : continuous (@subtype.val α p) := continuous_induced_dom lemma continuous_subtype_coe : continuous (coe : subtype p → α) := continuous_subtype_val lemma continuous.subtype_coe {f : β → subtype p} (hf : continuous f) : continuous (λ x, (f x : α)) := continuous_subtype_coe.comp hf lemma is_open.open_embedding_subtype_coe {s : set α} (hs : is_open s) : open_embedding (coe : s → α) := { induced := rfl, inj := subtype.coe_injective, open_range := (subtype.range_coe : range coe = s).symm ▸ hs } lemma is_open.is_open_map_subtype_coe {s : set α} (hs : is_open s) : is_open_map (coe : s → α) := hs.open_embedding_subtype_coe.is_open_map lemma is_open_map.restrict {f : α → β} (hf : is_open_map f) {s : set α} (hs : is_open s) : is_open_map (s.restrict f) := hf.comp hs.is_open_map_subtype_coe lemma is_closed.closed_embedding_subtype_coe {s : set α} (hs : is_closed s) : closed_embedding (coe : {x // x ∈ s} → α) := { induced := rfl, inj := subtype.coe_injective, closed_range := (subtype.range_coe : range coe = s).symm ▸ hs } @[continuity] lemma continuous.subtype_mk {f : β → α} (h : continuous f) (hp : ∀x, p (f x)) : continuous (λx, (⟨f x, hp x⟩ : subtype p)) := continuous_induced_rng.2 h lemma continuous.subtype_map {f : α → β} (h : continuous f) {q : β → Prop} (hpq : ∀ x, p x → q (f x)) : continuous (subtype.map f hpq) := (h.comp continuous_subtype_coe).subtype_mk _ lemma continuous_inclusion {s t : set α} (h : s ⊆ t) : continuous (inclusion h) := continuous_id.subtype_map h lemma continuous_at_subtype_coe {p : α → Prop} {a : subtype p} : continuous_at (coe : subtype p → α) a := continuous_iff_continuous_at.mp continuous_subtype_coe _ lemma subtype.dense_iff {s : set α} {t : set s} : dense t ↔ s ⊆ closure (coe '' t) := by { rw [inducing_coe.dense_iff, set_coe.forall], refl } lemma map_nhds_subtype_coe_eq {a : α} (ha : p a) (h : {a | p a} ∈ 𝓝 a) : map (coe : subtype p → α) (𝓝 ⟨a, ha⟩) = 𝓝 a := map_nhds_induced_of_mem $ by simpa only [subtype.coe_mk, subtype.range_coe] using h lemma nhds_subtype_eq_comap {a : α} {h : p a} : 𝓝 (⟨a, h⟩ : subtype p) = comap coe (𝓝 a) := nhds_induced _ _ lemma tendsto_subtype_rng {β : Type*} {p : α → Prop} {b : filter β} {f : β → subtype p} : ∀{a:subtype p}, tendsto f b (𝓝 a) ↔ tendsto (λx, (f x : α)) b (𝓝 (a : α)) | ⟨a, ha⟩ := by rw [nhds_subtype_eq_comap, tendsto_comap_iff, subtype.coe_mk] lemma continuous_subtype_nhds_cover {ι : Sort*} {f : α → β} {c : ι → α → Prop} (c_cover : ∀x:α, ∃i, {x | c i x} ∈ 𝓝 x) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x)) : continuous f := continuous_iff_continuous_at.mpr $ assume x, let ⟨i, (c_sets : {x | c i x} ∈ 𝓝 x)⟩ := c_cover x in let x' : subtype (c i) := ⟨x, mem_of_mem_nhds c_sets⟩ in calc map f (𝓝 x) = map f (map coe (𝓝 x')) : congr_arg (map f) (map_nhds_subtype_coe_eq _ $ c_sets).symm ... = map (λx:subtype (c i), f x) (𝓝 x') : rfl ... ≤ 𝓝 (f x) : continuous_iff_continuous_at.mp (f_cont i) x' lemma continuous_subtype_is_closed_cover {ι : Sort*} {f : α → β} (c : ι → α → Prop) (h_lf : locally_finite (λi, {x | c i x})) (h_is_closed : ∀i, is_closed {x | c i x}) (h_cover : ∀x, ∃i, c i x) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x)) : continuous f := continuous_iff_is_closed.mpr $ assume s hs, have ∀i, is_closed ((coe : {x | c i x} → α) '' (f ∘ coe ⁻¹' s)), from assume i, (closed_embedding_subtype_coe (h_is_closed _)).is_closed_map _ (hs.preimage (f_cont i)), have is_closed (⋃i, (coe : {x | c i x} → α) '' (f ∘ coe ⁻¹' s)), from locally_finite.is_closed_Union (h_lf.subset $ assume i x ⟨⟨x', hx'⟩, _, heq⟩, heq ▸ hx') this, have f ⁻¹' s = (⋃i, (coe : {x | c i x} → α) '' (f ∘ coe ⁻¹' s)), begin apply set.ext, have : ∀ (x : α), f x ∈ s ↔ ∃ (i : ι), c i x ∧ f x ∈ s := λ x, ⟨λ hx, let ⟨i, hi⟩ := h_cover x in ⟨i, hi, hx⟩, λ ⟨i, hi, hx⟩, hx⟩, simpa [and.comm, @and.left_comm (c _ _), ← exists_and_distrib_right], end, by rwa [this] lemma closure_subtype {x : {a // p a}} {s : set {a // p a}}: x ∈ closure s ↔ (x : α) ∈ closure ((coe : _ → α) '' s) := closure_induced lemma continuous_at_cod_restrict_iff {f : α → β} {t : set β} (h1 : ∀ x, f x ∈ t) {x : α} : continuous_at (cod_restrict f t h1) x ↔ continuous_at f x := by simp_rw [inducing_coe.continuous_at_iff, function.comp, coe_cod_restrict_apply] alias continuous_at_cod_restrict_iff ↔ _ continuous_at.cod_restrict lemma continuous_at.restrict {f : α → β} {s : set α} {t : set β} (h1 : maps_to f s t) {x : s} (h2 : continuous_at f x) : continuous_at (h1.restrict f s t) x := (h2.comp continuous_at_subtype_coe).cod_restrict _ lemma continuous_at.restrict_preimage {f : α → β} {s : set β} {x : f ⁻¹' s} (h : continuous_at f x) : continuous_at (s.restrict_preimage f) x := h.restrict _ @[continuity] lemma continuous.cod_restrict {f : α → β} {s : set β} (hf : continuous f) (hs : ∀ a, f a ∈ s) : continuous (s.cod_restrict f hs) := hf.subtype_mk hs lemma inducing.cod_restrict {e : α → β} (he : inducing e) {s : set β} (hs : ∀ x, e x ∈ s) : inducing (cod_restrict e s hs) := inducing_of_inducing_compose (he.continuous.cod_restrict hs) continuous_subtype_coe he lemma embedding.cod_restrict {e : α → β} (he : embedding e) (s : set β) (hs : ∀ x, e x ∈ s) : embedding (cod_restrict e s hs) := embedding_of_embedding_compose (he.continuous.cod_restrict hs) continuous_subtype_coe he end subtype section quotient variables [topological_space α] [topological_space β] [topological_space γ] variables {r : α → α → Prop} {s : setoid α} lemma quotient_map_quot_mk : quotient_map (@quot.mk α r) := ⟨quot.exists_rep, rfl⟩ @[continuity] lemma continuous_quot_mk : continuous (@quot.mk α r) := continuous_coinduced_rng @[continuity] lemma continuous_quot_lift {f : α → β} (hr : ∀ a b, r a b → f a = f b) (h : continuous f) : continuous (quot.lift f hr : quot r → β) := continuous_coinduced_dom.2 h lemma quotient_map_quotient_mk : quotient_map (@quotient.mk α s) := quotient_map_quot_mk lemma continuous_quotient_mk : continuous (@quotient.mk α s) := continuous_coinduced_rng lemma continuous.quotient_lift {f : α → β} (h : continuous f) (hs : ∀ a b, a ≈ b → f a = f b) : continuous (quotient.lift f hs : quotient s → β) := continuous_coinduced_dom.2 h lemma continuous.quotient_lift_on' {f : α → β} (h : continuous f) (hs : ∀ a b, @setoid.r _ s a b → f a = f b) : continuous (λ x, quotient.lift_on' x f hs : quotient s → β) := h.quotient_lift hs lemma continuous.quotient_map' {t : setoid β} {f : α → β} (hf : continuous f) (H : (s.r ⇒ t.r) f f) : continuous (quotient.map' f H) := (continuous_quotient_mk.comp hf).quotient_lift _ end quotient section pi variables {ι : Type*} {π : ι → Type*} {κ : Type*} [topological_space α] [∀i, topological_space (π i)] {f : α → Πi:ι, π i} lemma continuous_pi_iff : continuous f ↔ ∀ i, continuous (λ a, f a i) := by simp only [continuous_infi_rng, continuous_induced_rng] @[continuity] lemma continuous_pi (h : ∀ i, continuous (λ a, f a i)) : continuous f := continuous_pi_iff.2 h @[continuity] lemma continuous_apply (i : ι) : continuous (λp:Πi, π i, p i) := continuous_infi_dom continuous_induced_dom @[continuity] lemma continuous_apply_apply {ρ : κ → ι → Type*} [∀ j i, topological_space (ρ j i)] (j : κ) (i : ι) : continuous (λ p : (Π j, Π i, ρ j i), p j i) := (continuous_apply i).comp (continuous_apply j) lemma continuous_at_apply (i : ι) (x : Π i, π i) : continuous_at (λ p : Π i, π i, p i) x := (continuous_apply i).continuous_at lemma filter.tendsto.apply {l : filter β} {f : β → Π i, π i} {x : Π i, π i} (h : tendsto f l (𝓝 x)) (i : ι) : tendsto (λ a, f a i) l (𝓝 $ x i) := (continuous_at_apply i _).tendsto.comp h lemma nhds_pi {a : Πi, π i} : 𝓝 a = pi (λ i, 𝓝 (a i)) := by simp only [nhds_infi, nhds_induced, filter.pi] lemma tendsto_pi_nhds {f : β → Πi, π i} {g : Πi, π i} {u : filter β} : tendsto f u (𝓝 g) ↔ ∀ x, tendsto (λ i, f i x) u (𝓝 (g x)) := by rw [nhds_pi, filter.tendsto_pi] lemma continuous_at_pi {f : α → Π i, π i} {x : α} : continuous_at f x ↔ ∀ i, continuous_at (λ y, f y i) x := tendsto_pi_nhds lemma filter.tendsto.update [decidable_eq ι] {l : filter β} {f : β → Π i, π i} {x : Π i, π i} (hf : tendsto f l (𝓝 x)) (i : ι) {g : β → π i} {xi : π i} (hg : tendsto g l (𝓝 xi)) : tendsto (λ a, update (f a) i (g a)) l (𝓝 $ update x i xi) := tendsto_pi_nhds.2 $ λ j, by { rcases em (j = i) with rfl|hj; simp [*, hf.apply] } lemma continuous_at.update [decidable_eq ι] {a : α} (hf : continuous_at f a) (i : ι) {g : α → π i} (hg : continuous_at g a) : continuous_at (λ a, update (f a) i (g a)) a := hf.update i hg lemma continuous.update [decidable_eq ι] (hf : continuous f) (i : ι) {g : α → π i} (hg : continuous g) : continuous (λ a, update (f a) i (g a)) := continuous_iff_continuous_at.2 $ λ x, hf.continuous_at.update i hg.continuous_at /-- `update f i x` is continuous in `(f, x)`. -/ @[continuity] lemma continuous_update [decidable_eq ι] (i : ι) : continuous (λ f : (Π j, π j) × π i, update f.1 i f.2) := continuous_fst.update i continuous_snd lemma filter.tendsto.fin_insert_nth {n} {π : fin (n + 1) → Type*} [Π i, topological_space (π i)] (i : fin (n + 1)) {f : β → π i} {l : filter β} {x : π i} (hf : tendsto f l (𝓝 x)) {g : β → Π j : fin n, π (i.succ_above j)} {y : Π j, π (i.succ_above j)} (hg : tendsto g l (𝓝 y)) : tendsto (λ a, i.insert_nth (f a) (g a)) l (𝓝 $ i.insert_nth x y) := tendsto_pi_nhds.2 (λ j, fin.succ_above_cases i (by simpa) (by simpa using tendsto_pi_nhds.1 hg) j) lemma continuous_at.fin_insert_nth {n} {π : fin (n + 1) → Type*} [Π i, topological_space (π i)] (i : fin (n + 1)) {f : α → π i} {a : α} (hf : continuous_at f a) {g : α → Π j : fin n, π (i.succ_above j)} (hg : continuous_at g a) : continuous_at (λ a, i.insert_nth (f a) (g a)) a := hf.fin_insert_nth i hg lemma continuous.fin_insert_nth {n} {π : fin (n + 1) → Type*} [Π i, topological_space (π i)] (i : fin (n + 1)) {f : α → π i} (hf : continuous f) {g : α → Π j : fin n, π (i.succ_above j)} (hg : continuous g) : continuous (λ a, i.insert_nth (f a) (g a)) := continuous_iff_continuous_at.2 $ λ a, hf.continuous_at.fin_insert_nth i hg.continuous_at lemma is_open_set_pi {i : set ι} {s : Πa, set (π a)} (hi : i.finite) (hs : ∀a∈i, is_open (s a)) : is_open (pi i s) := by rw [pi_def]; exact (is_open_bInter hi $ assume a ha, (hs _ ha).preimage (continuous_apply _)) lemma is_closed_set_pi {i : set ι} {s : Πa, set (π a)} (hs : ∀a∈i, is_closed (s a)) : is_closed (pi i s) := by rw [pi_def]; exact (is_closed_Inter $ λ a, is_closed_Inter $ λ ha, (hs _ ha).preimage (continuous_apply _)) lemma mem_nhds_of_pi_mem_nhds {I : set ι} {s : Π i, set (π i)} (a : Π i, π i) (hs : I.pi s ∈ 𝓝 a) {i : ι} (hi : i ∈ I) : s i ∈ 𝓝 (a i) := by { rw nhds_pi at hs, exact mem_of_pi_mem_pi hs hi } lemma set_pi_mem_nhds {i : set ι} {s : Π a, set (π a)} {x : Π a, π a} (hi : i.finite) (hs : ∀ a ∈ i, s a ∈ 𝓝 (x a)) : pi i s ∈ 𝓝 x := by { rw [pi_def, bInter_mem hi], exact λ a ha, (continuous_apply a).continuous_at (hs a ha) } lemma set_pi_mem_nhds_iff {I : set ι} (hI : I.finite) {s : Π i, set (π i)} (a : Π i, π i) : I.pi s ∈ 𝓝 a ↔ ∀ (i : ι), i ∈ I → s i ∈ 𝓝 (a i) := by { rw [nhds_pi, pi_mem_pi_iff hI], apply_instance } lemma interior_pi_set {I : set ι} (hI : I.finite) {s : Π i, set (π i)} : interior (pi I s) = I.pi (λ i, interior (s i)) := by { ext a, simp only [set.mem_pi, mem_interior_iff_mem_nhds, set_pi_mem_nhds_iff hI] } lemma exists_finset_piecewise_mem_of_mem_nhds [decidable_eq ι] {s : set (Π a, π a)} {x : Π a, π a} (hs : s ∈ 𝓝 x) (y : Π a, π a) : ∃ I : finset ι, I.piecewise x y ∈ s := begin simp only [nhds_pi, filter.mem_pi'] at hs, rcases hs with ⟨I, t, htx, hts⟩, refine ⟨I, hts $ λ i hi, _⟩, simpa [finset.mem_coe.1 hi] using mem_of_mem_nhds (htx i) end lemma pi_eq_generate_from : Pi.topological_space = generate_from {g | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, is_open (s a)) ∧ g = pi ↑i s} := le_antisymm (le_generate_from $ assume g ⟨s, i, hi, eq⟩, eq.symm ▸ is_open_set_pi (finset.finite_to_set _) hi) (le_infi $ assume a s ⟨t, ht, s_eq⟩, generate_open.basic _ $ ⟨update (λa, univ) a t, {a}, by simpa using ht, s_eq ▸ by ext f; simp [set.pi]⟩) lemma pi_generate_from_eq {π : ι → Type*} {g : Πa, set (set (π a))} : @Pi.topological_space ι π (λa, generate_from (g a)) = generate_from {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ g a) ∧ t = pi ↑i s} := let G := {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ g a) ∧ t = pi ↑i s} in begin rw [pi_eq_generate_from], refine le_antisymm (generate_from_mono _) (le_generate_from _), exact assume s ⟨t, i, ht, eq⟩, ⟨t, i, assume a ha, generate_open.basic _ (ht a ha), eq⟩, { rintros s ⟨t, i, hi, rfl⟩, rw [pi_def], apply is_open_bInter (finset.finite_to_set _), assume a ha, show ((generate_from G).coinduced (λf:Πa, π a, f a)).is_open (t a), refine le_generate_from _ _ (hi a ha), exact assume s hs, generate_open.basic _ ⟨update (λa, univ) a s, {a}, by simp [hs]⟩ } end lemma pi_generate_from_eq_finite {π : ι → Type*} {g : Πa, set (set (π a))} [finite ι] (hg : ∀a, ⋃₀ g a = univ) : @Pi.topological_space ι π (λa, generate_from (g a)) = generate_from {t | ∃(s:Πa, set (π a)), (∀a, s a ∈ g a) ∧ t = pi univ s} := begin casesI nonempty_fintype ι, rw [pi_generate_from_eq], refine le_antisymm (generate_from_mono _) (le_generate_from _), exact assume s ⟨t, ht, eq⟩, ⟨t, finset.univ, by simp [ht, eq]⟩, { rintros s ⟨t, i, ht, rfl⟩, apply is_open_iff_forall_mem_open.2 _, assume f hf, choose c hc using show ∀a, ∃s, s ∈ g a ∧ f a ∈ s, { assume a, have : f a ∈ ⋃₀ g a, { rw [hg], apply mem_univ }, simpa }, refine ⟨pi univ (λa, if a ∈ i then t a else (c : Πa, set (π a)) a), _, _, _⟩, { simp [pi_if] }, { refine generate_open.basic _ ⟨_, assume a, _, rfl⟩, by_cases a ∈ i; simp [*, set.pi] at * }, { have : f ∈ pi {a | a ∉ i} c, { simp [*, set.pi] at * }, simpa [pi_if, hf] } } end /-- Suppose `π i` is a family of topological spaces indexed by `i : ι`, and `X` is a type endowed with a family of maps `f i : X → π i` for every `i : ι`, hence inducing a map `g : X → Π i, π i`. This lemma shows that infimum of the topologies on `X` induced by the `f i` as `i : ι` varies is simply the topology on `X` induced by `g : X → Π i, π i` where `Π i, π i` is endowed with the usual product topology. -/ lemma inducing_infi_to_pi {X : Type*} (f : Π i, X → π i) : @inducing X (Π i, π i) (⨅ i, induced (f i) infer_instance) _ (λ x i, f i x) := begin constructor, erw induced_infi, congr' 1, funext, erw induced_compose, end variables [finite ι] [∀ i, discrete_topology (π i)] /-- A finite product of discrete spaces is discrete. -/ instance Pi.discrete_topology : discrete_topology (Π i, π i) := singletons_open_iff_discrete.mp (λ x, begin rw show {x} = ⋂ i, {y : Π i, π i | y i = x i}, { ext, simp only [funext_iff, set.mem_singleton_iff, set.mem_Inter, set.mem_set_of_eq] }, exact is_open_Inter (λ i, (continuous_apply i).is_open_preimage {x i} (is_open_discrete {x i})) end) end pi section sigma variables {ι κ : Type*} {σ : ι → Type*} {τ : κ → Type*} [Π i, topological_space (σ i)] [Π k, topological_space (τ k)] [topological_space α] @[continuity] lemma continuous_sigma_mk {i : ι} : continuous (@sigma.mk ι σ i) := continuous_supr_rng continuous_coinduced_rng lemma is_open_sigma_iff {s : set (sigma σ)} : is_open s ↔ ∀ i, is_open (sigma.mk i ⁻¹' s) := by simp only [is_open_supr_iff, is_open_coinduced] lemma is_closed_sigma_iff {s : set (sigma σ)} : is_closed s ↔ ∀ i, is_closed (sigma.mk i ⁻¹' s) := by simp only [← is_open_compl_iff, is_open_sigma_iff, preimage_compl] lemma is_open_map_sigma_mk {i : ι} : is_open_map (@sigma.mk ι σ i) := begin intros s hs, rw is_open_sigma_iff, intro j, rcases eq_or_ne j i with (rfl|hne), { rwa set.preimage_image_eq _ sigma_mk_injective }, { rw [preimage_image_sigma_mk_of_ne hne], exact is_open_empty } end lemma is_open_range_sigma_mk {i : ι} : is_open (set.range (@sigma.mk ι σ i)) := is_open_map_sigma_mk.is_open_range lemma is_closed_map_sigma_mk {i : ι} : is_closed_map (@sigma.mk ι σ i) := begin intros s hs, rw is_closed_sigma_iff, intro j, rcases eq_or_ne j i with (rfl|hne), { rwa set.preimage_image_eq _ sigma_mk_injective }, { rw [preimage_image_sigma_mk_of_ne hne], exact is_closed_empty } end lemma is_closed_range_sigma_mk {i : ι} : is_closed (set.range (@sigma.mk ι σ i)) := is_closed_map_sigma_mk.closed_range lemma open_embedding_sigma_mk {i : ι} : open_embedding (@sigma.mk ι σ i) := open_embedding_of_continuous_injective_open continuous_sigma_mk sigma_mk_injective is_open_map_sigma_mk lemma closed_embedding_sigma_mk {i : ι} : closed_embedding (@sigma.mk ι σ i) := closed_embedding_of_continuous_injective_closed continuous_sigma_mk sigma_mk_injective is_closed_map_sigma_mk lemma embedding_sigma_mk {i : ι} : embedding (@sigma.mk ι σ i) := closed_embedding_sigma_mk.1 lemma sigma.nhds_mk (i : ι) (x : σ i) : 𝓝 (⟨i, x⟩ : sigma σ) = map (sigma.mk i) (𝓝 x) := (open_embedding_sigma_mk.map_nhds_eq x).symm lemma sigma.nhds_eq (x : sigma σ) : 𝓝 x = map (sigma.mk x.1) (𝓝 x.2) := by { cases x, apply sigma.nhds_mk } lemma comap_sigma_mk_nhds (i : ι) (x : σ i) : comap (sigma.mk i) (𝓝 ⟨i, x⟩) = 𝓝 x := (embedding_sigma_mk.to_inducing.nhds_eq_comap _).symm lemma is_open_sigma_fst_preimage (s : set ι) : is_open (sigma.fst ⁻¹' s : set (Σ a, σ a)) := begin rw [← bUnion_of_singleton s, preimage_Union₂], simp only [← range_sigma_mk], exact is_open_bUnion (λ _ _, is_open_range_sigma_mk) end /-- A map out of a sum type is continuous iff its restriction to each summand is. -/ @[simp] lemma continuous_sigma_iff {f : sigma σ → α} : continuous f ↔ ∀ i, continuous (λ a, f ⟨i, a⟩) := by simp only [continuous_supr_dom, continuous_coinduced_dom] /-- A map out of a sum type is continuous if its restriction to each summand is. -/ @[continuity] lemma continuous_sigma {f : sigma σ → α} (hf : ∀ i, continuous (λ a, f ⟨i, a⟩)) : continuous f := continuous_sigma_iff.2 hf @[simp] lemma continuous_sigma_map {f₁ : ι → κ} {f₂ : Π i, σ i → τ (f₁ i)} : continuous (sigma.map f₁ f₂) ↔ ∀ i, continuous (f₂ i) := continuous_sigma_iff.trans $ by simp only [sigma.map, embedding_sigma_mk.continuous_iff] @[continuity] lemma continuous.sigma_map {f₁ : ι → κ} {f₂ : Π i, σ i → τ (f₁ i)} (hf : ∀ i, continuous (f₂ i)) : continuous (sigma.map f₁ f₂) := continuous_sigma_map.2 hf lemma is_open_map_sigma {f : sigma σ → α} : is_open_map f ↔ ∀ i, is_open_map (λ a, f ⟨i, a⟩) := by simp only [is_open_map_iff_nhds_le, sigma.forall, sigma.nhds_eq, map_map] lemma is_open_map_sigma_map {f₁ : ι → κ} {f₂ : Π i, σ i → τ (f₁ i)} : is_open_map (sigma.map f₁ f₂) ↔ ∀ i, is_open_map (f₂ i) := is_open_map_sigma.trans $ forall_congr $ λ i, (@open_embedding_sigma_mk _ _ _ (f₁ i)).is_open_map_iff.symm lemma inducing_sigma_map {f₁ : ι → κ} {f₂ : Π i, σ i → τ (f₁ i)} (h₁ : injective f₁) : inducing (sigma.map f₁ f₂) ↔ ∀ i, inducing (f₂ i) := by simp only [inducing_iff_nhds, sigma.forall, sigma.nhds_mk, sigma.map, ← map_sigma_mk_comap h₁, map_inj sigma_mk_injective] lemma embedding_sigma_map {f₁ : ι → κ} {f₂ : Π i, σ i → τ (f₁ i)} (h : injective f₁) : embedding (sigma.map f₁ f₂) ↔ ∀ i, embedding (f₂ i) := by simp only [embedding_iff, injective.sigma_map, inducing_sigma_map h, forall_and_distrib, h.sigma_map_iff] lemma open_embedding_sigma_map {f₁ : ι → κ} {f₂ : Π i, σ i → τ (f₁ i)} (h : injective f₁) : open_embedding (sigma.map f₁ f₂) ↔ ∀ i, open_embedding (f₂ i) := by simp only [open_embedding_iff_embedding_open, is_open_map_sigma_map, embedding_sigma_map h, forall_and_distrib] end sigma section ulift @[continuity] lemma continuous_ulift_down [topological_space α] : continuous (ulift.down : ulift.{v u} α → α) := continuous_induced_dom @[continuity] lemma continuous_ulift_up [topological_space α] : continuous (ulift.up : α → ulift.{v u} α) := continuous_induced_rng.2 continuous_id end ulift
7f4ec859ae52349798af138deb4e8fc89d3a4039
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/synonym.lean
1de69a1fcc315499cbf90cdeef1e586622e2579c
[ "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
4,834
lean
/- Copyright (c) 2020 Johan Commelin, Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Damiano Testa, Yaël Dillies -/ import logic.equiv.defs import logic.nontrivial import order.basic /-! # Type synonyms > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > https://github.com/leanprover-community/mathlib4/pull/562 > Any changes to this file require a corresponding PR to mathlib4. This file provides two type synonyms for order theory: * `order_dual α`: Type synonym of `α` to equip it with the dual order (`a ≤ b` becomes `b ≤ a`). * `lex α`: Type synonym of `α` to equip it with its lexicographic order. The precise meaning depends on the type we take the lex of. Examples include `prod`, `sigma`, `list`, `finset`. ## Notation `αᵒᵈ` is notation for `order_dual α`. The general rule for notation of `lex` types is to append `ₗ` to the usual notation. ## Implementation notes One should not abuse definitional equality between `α` and `αᵒᵈ`/`lex α`. Instead, explicit coercions should be inserted: * `order_dual`: `order_dual.to_dual : α → αᵒᵈ` and `order_dual.of_dual : αᵒᵈ → α` * `lex`: `to_lex : α → lex α` and `of_lex : lex α → α`. In fact, those are bundled as `equiv`s to put goals in the right syntactic form for rewriting with the `equiv` API (`⇑to_lex a` where `⇑` is `coe_fn : (α ≃ lex α) → α → lex α`, instead of a bare `to_lex a`). ## See also This file is similar to `algebra.group.type_tags`. -/ variables {α β γ : Type*} /-! ### Order dual -/ namespace order_dual instance [h : nontrivial α] : nontrivial (αᵒᵈ) := h /-- `to_dual` is the identity function to the `order_dual` of a linear order. -/ def to_dual : α ≃ αᵒᵈ := equiv.refl _ /-- `of_dual` is the identity function from the `order_dual` of a linear order. -/ def of_dual : αᵒᵈ ≃ α := equiv.refl _ @[simp] lemma to_dual_symm_eq : (@to_dual α).symm = of_dual := rfl @[simp] lemma of_dual_symm_eq : (@of_dual α).symm = to_dual := rfl @[simp] lemma to_dual_of_dual (a : αᵒᵈ) : to_dual (of_dual a) = a := rfl @[simp] lemma of_dual_to_dual (a : α) : of_dual (to_dual a) = a := rfl @[simp] lemma to_dual_inj {a b : α} : to_dual a = to_dual b ↔ a = b := iff.rfl @[simp] lemma of_dual_inj {a b : αᵒᵈ} : of_dual a = of_dual b ↔ a = b := iff.rfl @[simp] lemma to_dual_le_to_dual [has_le α] {a b : α} : to_dual a ≤ to_dual b ↔ b ≤ a := iff.rfl @[simp] lemma to_dual_lt_to_dual [has_lt α] {a b : α} : to_dual a < to_dual b ↔ b < a := iff.rfl @[simp] lemma of_dual_le_of_dual [has_le α] {a b : αᵒᵈ} : of_dual a ≤ of_dual b ↔ b ≤ a := iff.rfl @[simp] lemma of_dual_lt_of_dual [has_lt α] {a b : αᵒᵈ} : of_dual a < of_dual b ↔ b < a := iff.rfl lemma le_to_dual [has_le α] {a : αᵒᵈ} {b : α} : a ≤ to_dual b ↔ b ≤ of_dual a := iff.rfl lemma lt_to_dual [has_lt α] {a : αᵒᵈ} {b : α} : a < to_dual b ↔ b < of_dual a := iff.rfl lemma to_dual_le [has_le α] {a : α} {b : αᵒᵈ} : to_dual a ≤ b ↔ of_dual b ≤ a := iff.rfl lemma to_dual_lt [has_lt α] {a : α} {b : αᵒᵈ} : to_dual a < b ↔ of_dual b < a := iff.rfl /-- Recursor for `αᵒᵈ`. -/ @[elab_as_eliminator] protected def rec {C : αᵒᵈ → Sort*} (h₂ : Π a : α, C (to_dual a)) : Π a : αᵒᵈ, C a := h₂ @[simp] protected lemma «forall» {p : αᵒᵈ → Prop} : (∀ a, p a) ↔ ∀ a, p (to_dual a) := iff.rfl @[simp] protected lemma «exists» {p : αᵒᵈ → Prop} : (∃ a, p a) ↔ ∃ a, p (to_dual a) := iff.rfl alias to_dual_le_to_dual ↔ _ _root_.has_le.le.dual alias to_dual_lt_to_dual ↔ _ _root_.has_lt.lt.dual alias of_dual_le_of_dual ↔ _ _root_.has_le.le.of_dual alias of_dual_lt_of_dual ↔ _ _root_.has_lt.lt.of_dual end order_dual /-! ### Lexicographic order -/ /-- A type synonym to equip a type with its lexicographic order. -/ def lex (α : Type*) := α /-- `to_lex` is the identity function to the `lex` of a type. -/ @[pattern] def to_lex : α ≃ lex α := equiv.refl _ /-- `of_lex` is the identity function from the `lex` of a type. -/ @[pattern] def of_lex : lex α ≃ α := equiv.refl _ @[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)
412642ec8bea67aa294d4f9f3808d57fe131e82b
26ac254ecb57ffcb886ff709cf018390161a9225
/src/data/pfunctor/univariate/basic.lean
3a7efe5e22436721d83fc0bee47a45bea157b938
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
6,124
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad -/ import data.W /-! # Polynomial functors This file defines polynomial functors and the W-type construction as a polynomial functor. (For the M-type construction, see pfunctor/M.lean.) -/ universe u /-- A polynomial functor `P` is given by a type `A` and a family `B` of types over `A`. `P` maps any type `α` to a new type `P.obj α`, which is defined as the sigma type `Σ x, P.B x → α`. An element of `P.obj α` is a pair `⟨a, f⟩`, where `a` is an element of a type `A` and `f : B a → α`. Think of `a` as the shape of the object and `f` as an index to the relevant elements of `α`. -/ structure pfunctor := (A : Type u) (B : A → Type u) namespace pfunctor instance : inhabited pfunctor := ⟨⟨default _, default _⟩⟩ variables (P : pfunctor) {α β : Type u} /-- Applying `P` to an object of `Type` -/ def obj (α : Type*) := Σ x : P.A, P.B x → α /-- Applying `P` to a morphism of `Type` -/ def map {α β : Type*} (f : α → β) : P.obj α → P.obj β := λ ⟨a, g⟩, ⟨a, f ∘ g⟩ instance obj.inhabited [inhabited P.A] [inhabited α] : inhabited (P.obj α) := ⟨ ⟨ default _, λ _, default _ ⟩ ⟩ instance : functor P.obj := {map := @map P} protected theorem map_eq {α β : Type*} (f : α → β) (a : P.A) (g : P.B a → α) : @functor.map P.obj _ _ _ f ⟨a, g⟩ = ⟨a, f ∘ g⟩ := rfl protected theorem id_map {α : Type*} : ∀ x : P.obj α, id <$> x = id x := λ ⟨a, b⟩, rfl protected theorem comp_map {α β γ : Type*} (f : α → β) (g : β → γ) : ∀ x : P.obj α, (g ∘ f) <$> x = g <$> (f <$> x) := λ ⟨a, b⟩, rfl instance : is_lawful_functor P.obj := {id_map := @pfunctor.id_map P, comp_map := @pfunctor.comp_map P} /-- re-export existing definition of W-types and adapt it to a packaged definition of polynomial functor -/ def W := _root_.W P.B /- inhabitants of W types is awkward to encode as an instance assumption because there needs to be a value `a : P.A` such that `P.B a` is empty to yield a finite tree -/ attribute [nolint has_inhabited_instance] W variables {P} /-- destructor for W-types -/ def W.dest : W P → P.obj (W P) | ⟨a, f⟩ := ⟨a, f⟩ /-- constructor for W-types -/ def W.mk : P.obj (W P) → W P | ⟨a, f⟩ := ⟨a, f⟩ @[simp] theorem W.dest_mk (p : P.obj (W P)) : W.dest (W.mk p) = p := by cases p; reflexivity @[simp] theorem W.mk_dest (p : W P) : W.mk (W.dest p) = p := by cases p; reflexivity variables (P) /-- `Idx` identifies a location inside the application of a pfunctor. For `F : pfunctor`, `x : F.obj α` and `i : F.Idx`, `i` can designate one part of `x` or is invalid, if `i.1 ≠ x.1` -/ def Idx := Σ x : P.A, P.B x instance Idx.inhabited [inhabited P.A] [inhabited (P.B (default _))] : inhabited P.Idx := ⟨ ⟨default _, default _⟩ ⟩ variables {P} /-- `x.iget i` takes the component of `x` designated by `i` if any is or returns a default value -/ def obj.iget [decidable_eq P.A] {α} [inhabited α] (x : P.obj α) (i : P.Idx) : α := if h : i.1 = x.1 then x.2 (cast (congr_arg _ h) i.2) else default _ @[simp] lemma fst_map {α β : Type u} (x : P.obj α) (f : α → β) : (f <$> x).1 = x.1 := by { cases x; refl } @[simp] lemma iget_map [decidable_eq P.A] {α β : Type u} [inhabited α] [inhabited β] (x : P.obj α) (f : α → β) (i : P.Idx) (h : i.1 = x.1) : (f <$> x).iget i = f (x.iget i) := by { simp only [obj.iget, fst_map, *, dif_pos, eq_self_iff_true], cases x, refl } end pfunctor /- Composition of polynomial functors. -/ namespace pfunctor /-- functor composition for polynomial functors -/ def comp (P₂ P₁ : pfunctor.{u}) : pfunctor.{u} := ⟨ Σ a₂ : P₂.1, P₂.2 a₂ → P₁.1, λ a₂a₁, Σ u : P₂.2 a₂a₁.1, P₁.2 (a₂a₁.2 u) ⟩ /-- constructor for composition -/ def comp.mk (P₂ P₁ : pfunctor.{u}) {α : Type} (x : P₂.obj (P₁.obj α)) : (comp P₂ P₁).obj α := ⟨ ⟨ x.1, sigma.fst ∘ x.2 ⟩, λ a₂a₁, (x.2 a₂a₁.1).2 a₂a₁.2 ⟩ /-- destructor for composition -/ def comp.get (P₂ P₁ : pfunctor.{u}) {α : Type} (x : (comp P₂ P₁).obj α) : P₂.obj (P₁.obj α) := ⟨ x.1.1, λ a₂, ⟨x.1.2 a₂, λ a₁, x.2 ⟨a₂,a₁⟩ ⟩ ⟩ end pfunctor /- Lifting predicates and relations. -/ namespace pfunctor variables {P : pfunctor.{u}} open functor theorem liftp_iff {α : Type u} (p : α → Prop) (x : P.obj α) : liftp p x ↔ ∃ a f, x = ⟨a, f⟩ ∧ ∀ i, p (f i) := begin split, { rintros ⟨y, hy⟩, cases h : y with a f, refine ⟨a, λ i, (f i).val, _, λ i, (f i).property⟩, rw [←hy, h, pfunctor.map_eq] }, rintros ⟨a, f, xeq, pf⟩, use ⟨a, λ i, ⟨f i, pf i⟩⟩, rw [xeq], reflexivity end theorem liftp_iff' {α : Type u} (p : α → Prop) (a : P.A) (f : P.B a → α) : @liftp.{u} P.obj _ α p ⟨a,f⟩ ↔ ∀ i, p (f i) := begin simp only [liftp_iff, sigma.mk.inj_iff]; split; intro, { casesm* [Exists _, _ ∧ _], subst_vars, assumption }, repeat { constructor <|> assumption } end theorem liftr_iff {α : Type u} (r : α → α → Prop) (x y : P.obj α) : liftr r x y ↔ ∃ a f₀ f₁, x = ⟨a, f₀⟩ ∧ y = ⟨a, f₁⟩ ∧ ∀ i, r (f₀ i) (f₁ i) := begin split, { rintros ⟨u, xeq, yeq⟩, cases h : u with a f, use [a, λ i, (f i).val.fst, λ i, (f i).val.snd], split, { rw [←xeq, h], refl }, split, { rw [←yeq, h], refl }, intro i, exact (f i).property }, rintros ⟨a, f₀, f₁, xeq, yeq, h⟩, use ⟨a, λ i, ⟨(f₀ i, f₁ i), h i⟩⟩, split, { rw [xeq], refl }, rw [yeq], refl end open set theorem supp_eq {α : Type u} (a : P.A) (f : P.B a → α) : @supp.{u} P.obj _ α (⟨a,f⟩ : P.obj α) = f '' univ := begin ext, simp only [supp, image_univ, mem_range, mem_set_of_eq], split; intro h, { apply @h (λ x, ∃ (y : P.B a), f y = x), rw liftp_iff', intro, refine ⟨_,rfl⟩ }, { simp only [liftp_iff'], cases h, subst x, tauto } end end pfunctor
f80fe6be3f561dd6dca3f35ebe2e252ac41dde16
200b12985a863d01fbbde6abfc9326bb82424a8b
/src/propLogic/Ex007.lean
cf3988dced606dd7e5c53bdf750b33942ccef6c4
[]
no_license
SvenWille/LeanLogicExercises
38eacd36733ac48e5a7aacf863c681c9a9a48271
2dbc920feadd63bbc50f87e69646c0081db26eba
refs/heads/master
1,629,676,667,365
1,512,161,459,000
1,512,161,459,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
350
lean
open classical theorem Ex007(a b : Prop): (( a → b) → a) → a := assume H1:( a → b) → a, have A:¬¬a,from not.intro ( assume H2:¬a, have B:a, from H1 ( assume H3:a, show b, from absurd H3 H2 ), show false, from H2 B ), by_contradiction ( assume C:¬a, show false, from A C )
4557f8f86b73a92ff77b3be4f5a12ee8f6544d3a
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/direct_limit.lean
a2bd0c5d46b71b227b58573df19a1a637760a44d
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
24,957
lean
/- Copyright (c) 2019 Kenny Lau, Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes -/ import data.finset.order import linear_algebra.direct_sum_module import ring_theory.free_comm_ring import ring_theory.ideal.operations /-! # Direct limit of modules, abelian groups, rings, and fields. See Atiyah-Macdonald PP.32-33, Matsumura PP.269-270 Generalizes the notion of "union", or "gluing", of incomparable modules over the same ring, or incomparable abelian groups, or rings, or fields. It is constructed as a quotient of the free module (for the module case) or quotient of the free commutative ring (for the ring case) instead of a quotient of the disjoint union so as to make the operations (addition etc.) "computable". -/ universes u v w u₁ open submodule variables {R : Type u} [ring R] variables {ι : Type v} variables [dec_ι : decidable_eq ι] [directed_order ι] variables (G : ι → Type w) /-- A directed system is a functor from the category (directed poset) to another category. This is used for abelian groups and rings and fields because their maps are not bundled. See module.directed_system -/ class directed_system (f : Π i j, i ≤ j → G i → G j) : Prop := (map_self [] : ∀ i x h, f i i h x = x) (map_map [] : ∀ i j k hij hjk x, f j k hjk (f i j hij x) = f i k (le_trans hij hjk) x) namespace module variables [Π i, add_comm_group (G i)] [Π i, module R (G i)] /-- A directed system is a functor from the category (directed poset) to the category of `R`-modules. -/ class directed_system (f : Π i j, i ≤ j → G i →ₗ[R] G j) : Prop := (map_self [] : ∀ i x h, f i i h x = x) (map_map [] : ∀ i j k hij hjk x, f j k hjk (f i j hij x) = f i k (le_trans hij hjk) x) variables (f : Π i j, i ≤ j → G i →ₗ[R] G j) include dec_ι /-- The direct limit of a directed system is the modules glued together along the maps. -/ def direct_limit : Type (max v w) := (span R $ { a | ∃ (i j) (H : i ≤ j) x, direct_sum.lof R ι G i x - direct_sum.lof R ι G j (f i j H x) = a }).quotient namespace direct_limit instance : add_comm_group (direct_limit G f) := quotient.add_comm_group _ instance : module R (direct_limit G f) := quotient.module _ instance : inhabited (direct_limit G f) := ⟨0⟩ variables (R ι) /-- The canonical map from a component to the direct limit. -/ def of (i) : G i →ₗ[R] direct_limit G f := (mkq _).comp $ direct_sum.lof R ι G i variables {R ι G f} @[simp] lemma of_f {i j hij x} : (of R ι G f j (f i j hij x)) = of R ι G f i x := eq.symm $ (submodule.quotient.eq _).2 $ subset_span ⟨i, j, hij, x, rfl⟩ /-- Every element of the direct limit corresponds to some element in some component of the directed system. -/ theorem exists_of [nonempty ι] (z : direct_limit G f) : ∃ i x, of R ι G f i x = z := nonempty.elim (by apply_instance) $ assume ind : ι, quotient.induction_on' z $ λ z, direct_sum.induction_on z ⟨ind, 0, linear_map.map_zero _⟩ (λ i x, ⟨i, x, rfl⟩) (λ p q ⟨i, x, ihx⟩ ⟨j, y, ihy⟩, let ⟨k, hik, hjk⟩ := directed_order.directed i j in ⟨k, f i k hik x + f j k hjk y, by rw [linear_map.map_add, of_f, of_f, ihx, ihy]; refl⟩) @[elab_as_eliminator] protected theorem induction_on [nonempty ι] {C : direct_limit G f → Prop} (z : direct_limit G f) (ih : ∀ i x, C (of R ι G f i x)) : C z := let ⟨i, x, h⟩ := exists_of z in h ▸ ih i x variables {P : Type u₁} [add_comm_group P] [module R P] (g : Π i, G i →ₗ[R] P) variables (Hg : ∀ i j hij x, g j (f i j hij x) = g i x) include Hg variables (R ι G f) /-- The universal property of the direct limit: maps from the components to another module that respect the directed system structure (i.e. make some diagram commute) give rise to a unique map out of the direct limit. -/ def lift : direct_limit G f →ₗ[R] P := liftq _ (direct_sum.to_module R ι P g) (span_le.2 $ λ a ⟨i, j, hij, x, hx⟩, by rw [← hx, set_like.mem_coe, linear_map.sub_mem_ker_iff, direct_sum.to_module_lof, direct_sum.to_module_lof, Hg]) variables {R ι G f} omit Hg lemma lift_of {i} (x) : lift R ι G f g Hg (of R ι G f i x) = g i x := direct_sum.to_module_lof R _ _ theorem lift_unique [nonempty ι] (F : direct_limit G f →ₗ[R] P) (x) : F x = lift R ι G f (λ i, F.comp $ of R ι G f i) (λ i j hij x, by rw [linear_map.comp_apply, of_f]; refl) x := direct_limit.induction_on x $ λ i x, by rw lift_of; refl section totalize open_locale classical variables (G f) omit dec_ι /-- `totalize G f i j` is a linear map from `G i` to `G j`, for *every* `i` and `j`. If `i ≤ j`, then it is the map `f i j` that comes with the directed system `G`, and otherwise it is the zero map. -/ noncomputable def totalize : Π i j, G i →ₗ[R] G j := λ i j, if h : i ≤ j then f i j h else 0 variables {G f} lemma totalize_apply (i j x) : totalize G f i j x = if h : i ≤ j then f i j h x else 0 := if h : i ≤ j then by dsimp only [totalize]; rw [dif_pos h, dif_pos h] else by dsimp only [totalize]; rw [dif_neg h, dif_neg h, linear_map.zero_apply] end totalize variables [directed_system G f] open_locale classical lemma to_module_totalize_of_le {x : direct_sum ι G} {i j : ι} (hij : i ≤ j) (hx : ∀ k ∈ x.support, k ≤ i) : direct_sum.to_module R ι (G j) (λ k, totalize G f k j) x = f i j hij (direct_sum.to_module R ι (G i) (λ k, totalize G f k i) x) := begin rw [← @dfinsupp.sum_single ι G _ _ _ x], unfold dfinsupp.sum, simp only [linear_map.map_sum], refine finset.sum_congr rfl (λ k hk, _), rw direct_sum.single_eq_lof R k (x k), simp [totalize_apply, hx k hk, le_trans (hx k hk) hij, directed_system.map_map f] end lemma of.zero_exact_aux [nonempty ι] {x : direct_sum ι G} (H : submodule.quotient.mk x = (0 : direct_limit G f)) : ∃ j, (∀ k ∈ x.support, k ≤ j) ∧ direct_sum.to_module R ι (G j) (λ i, totalize G f i j) x = (0 : G j) := nonempty.elim (by apply_instance) $ assume ind : ι, span_induction ((quotient.mk_eq_zero _).1 H) (λ x ⟨i, j, hij, y, hxy⟩, let ⟨k, hik, hjk⟩ := directed_order.directed i j in ⟨k, begin clear_, subst hxy, split, { intros i0 hi0, rw [dfinsupp.mem_support_iff, direct_sum.sub_apply, ← direct_sum.single_eq_lof, ← direct_sum.single_eq_lof, dfinsupp.single_apply, dfinsupp.single_apply] at hi0, split_ifs at hi0 with hi hj hj, { rwa hi at hik }, { rwa hi at hik }, { rwa hj at hjk }, exfalso, apply hi0, rw sub_zero }, simp [linear_map.map_sub, totalize_apply, hik, hjk, directed_system.map_map f, direct_sum.apply_eq_component, direct_sum.component.of], end⟩) ⟨ind, λ _ h, (finset.not_mem_empty _ h).elim, linear_map.map_zero _⟩ (λ x y ⟨i, hi, hxi⟩ ⟨j, hj, hyj⟩, let ⟨k, hik, hjk⟩ := directed_order.directed i j in ⟨k, λ l hl, (finset.mem_union.1 (dfinsupp.support_add hl)).elim (λ hl, le_trans (hi _ hl) hik) (λ hl, le_trans (hj _ hl) hjk), by simp [linear_map.map_add, hxi, hyj, to_module_totalize_of_le hik hi, to_module_totalize_of_le hjk hj]⟩) (λ a x ⟨i, hi, hxi⟩, ⟨i, λ k hk, hi k (direct_sum.support_smul _ _ hk), by simp [linear_map.map_smul, hxi]⟩) /-- A component that corresponds to zero in the direct limit is already zero in some bigger module in the directed system. -/ theorem of.zero_exact {i x} (H : of R ι G f i x = 0) : ∃ j hij, f i j hij x = (0 : G j) := by haveI : nonempty ι := ⟨i⟩; exact let ⟨j, hj, hxj⟩ := of.zero_exact_aux H in if hx0 : x = 0 then ⟨i, le_refl _, by simp [hx0]⟩ else have hij : i ≤ j, from hj _ $ by simp [direct_sum.apply_eq_component, hx0], ⟨j, hij, by simpa [totalize_apply, hij] using hxj⟩ end direct_limit end module namespace add_comm_group variables [Π i, add_comm_group (G i)] include dec_ι /-- The direct limit of a directed system is the abelian groups glued together along the maps. -/ def direct_limit (f : Π i j, i ≤ j → G i →+ G j) : Type* := @module.direct_limit ℤ _ ι _ _ G _ _ (λ i j hij, (f i j hij).to_int_linear_map) namespace direct_limit variables (f : Π i j, i ≤ j → G i →+ G j) omit dec_ι protected lemma directed_system [directed_system G (λ i j h, f i j h)] : module.directed_system G (λ i j hij, (f i j hij).to_int_linear_map) := ⟨directed_system.map_self (λ i j h, f i j h), directed_system.map_map (λ i j h, f i j h)⟩ include dec_ι local attribute [instance] direct_limit.directed_system instance : add_comm_group (direct_limit G f) := module.direct_limit.add_comm_group G (λ i j hij, (f i j hij).to_int_linear_map) instance : inhabited (direct_limit G f) := ⟨0⟩ /-- The canonical map from a component to the direct limit. -/ def of (i) : G i →ₗ[ℤ] direct_limit G f := module.direct_limit.of ℤ ι G (λ i j hij, (f i j hij).to_int_linear_map) i variables {G f} @[simp] lemma of_f {i j} (hij) (x) : of G f j (f i j hij x) = of G f i x := module.direct_limit.of_f @[elab_as_eliminator] protected theorem induction_on [nonempty ι] {C : direct_limit G f → Prop} (z : direct_limit G f) (ih : ∀ i x, C (of G f i x)) : C z := module.direct_limit.induction_on z ih /-- A component that corresponds to zero in the direct limit is already zero in some bigger module in the directed system. -/ theorem of.zero_exact [directed_system G (λ i j h, f i j h)] (i x) (h : of G f i x = 0) : ∃ j hij, f i j hij x = 0 := module.direct_limit.of.zero_exact h variables (P : Type u₁) [add_comm_group P] variables (g : Π i, G i →+ P) variables (Hg : ∀ i j hij x, g j (f i j hij x) = g i x) variables (G f) /-- The universal property of the direct limit: maps from the components to another abelian group that respect the directed system structure (i.e. make some diagram commute) give rise to a unique map out of the direct limit. -/ def lift : direct_limit G f →ₗ[ℤ] P := module.direct_limit.lift ℤ ι G (λ i j hij, (f i j hij).to_int_linear_map) (λ i, (g i).to_int_linear_map) Hg variables {G f} @[simp] lemma lift_of (i x) : lift G f P g Hg (of G f i x) = g i x := module.direct_limit.lift_of _ _ _ lemma lift_unique [nonempty ι] (F : direct_limit G f →+ P) (x) : F x = lift G f P (λ i, F.comp (of G f i).to_add_monoid_hom) (λ i j hij x, by simp) x := direct_limit.induction_on x $ λ i x, by simp end direct_limit end add_comm_group namespace ring variables [Π i, comm_ring (G i)] section variables (f : Π i j, i ≤ j → G i → G j) open free_comm_ring /-- The direct limit of a directed system is the rings glued together along the maps. -/ def direct_limit : Type (max v w) := (ideal.span { a | (∃ i j H x, of (⟨j, f i j H x⟩ : Σ i, G i) - of ⟨i, x⟩ = a) ∨ (∃ i, of (⟨i, 1⟩ : Σ i, G i) - 1 = a) ∨ (∃ i x y, of (⟨i, x + y⟩ : Σ i, G i) - (of ⟨i, x⟩ + of ⟨i, y⟩) = a) ∨ (∃ i x y, of (⟨i, x * y⟩ : Σ i, G i) - (of ⟨i, x⟩ * of ⟨i, y⟩) = a) }).quotient namespace direct_limit instance : comm_ring (direct_limit G f) := ideal.quotient.comm_ring _ instance : ring (direct_limit G f) := comm_ring.to_ring _ instance : inhabited (direct_limit G f) := ⟨0⟩ /-- The canonical map from a component to the direct limit. -/ def of (i) : G i →+* direct_limit G f := ring_hom.mk' { to_fun := λ x, ideal.quotient.mk _ (of (⟨i, x⟩ : Σ i, G i)), map_one' := ideal.quotient.eq.2 $ subset_span $ or.inr $ or.inl ⟨i, rfl⟩, map_mul' := λ x y, ideal.quotient.eq.2 $ subset_span $ or.inr $ or.inr $ or.inr ⟨i, x, y, rfl⟩, } (λ x y, ideal.quotient.eq.2 $ subset_span $ or.inr $ or.inr $ or.inl ⟨i, x, y, rfl⟩) variables {G f} @[simp] lemma of_f {i j} (hij) (x) : of G f j (f i j hij x) = of G f i x := ideal.quotient.eq.2 $ subset_span $ or.inl ⟨i, j, hij, x, rfl⟩ /-- Every element of the direct limit corresponds to some element in some component of the directed system. -/ theorem exists_of [nonempty ι] (z : direct_limit G f) : ∃ i x, of G f i x = z := nonempty.elim (by apply_instance) $ assume ind : ι, quotient.induction_on' z $ λ x, free_abelian_group.induction_on x ⟨ind, 0, (of _ _ ind).map_zero⟩ (λ s, multiset.induction_on s ⟨ind, 1, (of _ _ ind).map_one⟩ (λ a s ih, let ⟨i, x⟩ := a, ⟨j, y, hs⟩ := ih, ⟨k, hik, hjk⟩ := directed_order.directed i j in ⟨k, f i k hik x * f j k hjk y, by rw [(of _ _ _).map_mul, of_f, of_f, hs]; refl⟩)) (λ s ⟨i, x, ih⟩, ⟨i, -x, by rw [(of _ _ _).map_neg, ih]; refl⟩) (λ p q ⟨i, x, ihx⟩ ⟨j, y, ihy⟩, let ⟨k, hik, hjk⟩ := directed_order.directed i j in ⟨k, f i k hik x + f j k hjk y, by rw [(of _ _ _).map_add, of_f, of_f, ihx, ihy]; refl⟩) section open_locale classical open polynomial variables {f' : Π i j, i ≤ j → G i →+* G j} theorem polynomial.exists_of [nonempty ι] (q : polynomial (direct_limit G (λ i j h, f' i j h))) : ∃ i p, polynomial.map (of G (λ i j h, f' i j h) i) p = q := polynomial.induction_on q (λ z, let ⟨i, x, h⟩ := exists_of z in ⟨i, C x, by rw [map_C, h]⟩) (λ q₁ q₂ ⟨i₁, p₁, ih₁⟩ ⟨i₂, p₂, ih₂⟩, let ⟨i, h1, h2⟩ := directed_order.directed i₁ i₂ in ⟨i, p₁.map (f' i₁ i h1) + p₂.map (f' i₂ i h2), by { rw [polynomial.map_add, map_map, map_map, ← ih₁, ← ih₂], congr' 2; ext x; simp_rw [ring_hom.comp_apply, of_f] }⟩) (λ n z ih, let ⟨i, x, h⟩ := exists_of z in ⟨i, C x * X ^ (n + 1), by rw [polynomial.map_mul, map_C, h, polynomial.map_pow, map_X]⟩) end @[elab_as_eliminator] theorem induction_on [nonempty ι] {C : direct_limit G f → Prop} (z : direct_limit G f) (ih : ∀ i x, C (of G f i x)) : C z := let ⟨i, x, hx⟩ := exists_of z in hx ▸ ih i x section of_zero_exact open_locale classical variables (f' : Π i j, i ≤ j → G i →+* G j) variables [directed_system G (λ i j h, f' i j h)] variables (G f) lemma of.zero_exact_aux2 {x : free_comm_ring Σ i, G i} {s t} (hxs : is_supported x s) {j k} (hj : ∀ z : Σ i, G i, z ∈ s → z.1 ≤ j) (hk : ∀ z : Σ i, G i, z ∈ t → z.1 ≤ k) (hjk : j ≤ k) (hst : s ⊆ t) : f' j k hjk (lift (λ ix : s, f' ix.1.1 j (hj ix ix.2) ix.1.2) (restriction s x)) = lift (λ ix : t, f' ix.1.1 k (hk ix ix.2) ix.1.2) (restriction t x) := begin refine ring.in_closure.rec_on hxs _ _ _ _, { rw [(restriction _).map_one, (free_comm_ring.lift _).map_one, (f' j k hjk).map_one, (restriction _).map_one, (free_comm_ring.lift _).map_one] }, { rw [(restriction _).map_neg, (restriction _).map_one, (free_comm_ring.lift _).map_neg, (free_comm_ring.lift _).map_one, (f' j k hjk).map_neg, (f' j k hjk).map_one, (restriction _).map_neg, (restriction _).map_one, (free_comm_ring.lift _).map_neg, (free_comm_ring.lift _).map_one] }, { rintros _ ⟨p, hps, rfl⟩ n ih, rw [(restriction _).map_mul, (free_comm_ring.lift _).map_mul, (f' j k hjk).map_mul, ih, (restriction _).map_mul, (free_comm_ring.lift _).map_mul, restriction_of, dif_pos hps, lift_of, restriction_of, dif_pos (hst hps), lift_of], dsimp only, have := directed_system.map_map (λ i j h, f' i j h), dsimp only at this, rw this, refl }, { rintros x y ihx ihy, rw [(restriction _).map_add, (free_comm_ring.lift _).map_add, (f' j k hjk).map_add, ihx, ihy, (restriction _).map_add, (free_comm_ring.lift _).map_add] } end variables {G f f'} lemma of.zero_exact_aux [nonempty ι] {x : free_comm_ring Σ i, G i} (H : ideal.quotient.mk _ x = (0 : direct_limit G (λ i j h, f' i j h))) : ∃ j s, ∃ H : (∀ k : Σ i, G i, k ∈ s → k.1 ≤ j), is_supported x s ∧ lift (λ ix : s, f' ix.1.1 j (H ix ix.2) ix.1.2) (restriction s x) = (0 : G j) := begin refine span_induction (ideal.quotient.eq_zero_iff_mem.1 H) _ _ _ _, { rintros x (⟨i, j, hij, x, rfl⟩ | ⟨i, rfl⟩ | ⟨i, x, y, rfl⟩ | ⟨i, x, y, rfl⟩), { refine ⟨j, {⟨i, x⟩, ⟨j, f' i j hij x⟩}, _, is_supported_sub (is_supported_of.2 $ or.inr rfl) (is_supported_of.2 $ or.inl rfl), _⟩, { rintros k (rfl | ⟨rfl | _⟩), exact hij, refl }, { rw [(restriction _).map_sub, (free_comm_ring.lift _).map_sub, restriction_of, dif_pos, restriction_of, dif_pos, lift_of, lift_of], dsimp only, have := directed_system.map_map (λ i j h, f' i j h), dsimp only at this, rw this, exact sub_self _, exacts [or.inr rfl, or.inl rfl] } }, { refine ⟨i, {⟨i, 1⟩}, _, is_supported_sub (is_supported_of.2 rfl) is_supported_one, _⟩, { rintros k (rfl|h), refl }, { rw [(restriction _).map_sub, (free_comm_ring.lift _).map_sub, restriction_of, dif_pos, (restriction _).map_one, lift_of, (free_comm_ring.lift _).map_one], dsimp only, rw [(f' i i _).map_one, sub_self], { exact set.mem_singleton _ } } }, { refine ⟨i, {⟨i, x+y⟩, ⟨i, x⟩, ⟨i, y⟩}, _, is_supported_sub (is_supported_of.2 $ or.inl rfl) (is_supported_add (is_supported_of.2 $ or.inr $ or.inl rfl) (is_supported_of.2 $ or.inr $ or.inr rfl)), _⟩, { rintros k (rfl | ⟨rfl | ⟨rfl | hk⟩⟩); refl }, { rw [(restriction _).map_sub, (restriction _).map_add, restriction_of, restriction_of, restriction_of, dif_pos, dif_pos, dif_pos, (free_comm_ring.lift _).map_sub, (free_comm_ring.lift _).map_add, lift_of, lift_of, lift_of], dsimp only, rw (f' i i _).map_add, exact sub_self _, exacts [or.inl rfl, or.inr (or.inr rfl), or.inr (or.inl rfl)] } }, { refine ⟨i, {⟨i, x*y⟩, ⟨i, x⟩, ⟨i, y⟩}, _, is_supported_sub (is_supported_of.2 $ or.inl rfl) (is_supported_mul (is_supported_of.2 $ or.inr $ or.inl rfl) (is_supported_of.2 $ or.inr $ or.inr rfl)), _⟩, { rintros k (rfl | ⟨rfl | ⟨rfl | hk⟩⟩); refl }, { rw [(restriction _).map_sub, (restriction _).map_mul, restriction_of, restriction_of, restriction_of, dif_pos, dif_pos, dif_pos, (free_comm_ring.lift _).map_sub, (free_comm_ring.lift _).map_mul, lift_of, lift_of, lift_of], dsimp only, rw (f' i i _).map_mul, exacts [sub_self _, or.inl rfl, or.inr (or.inr rfl), or.inr (or.inl rfl)] } } }, { refine nonempty.elim (by apply_instance) (assume ind : ι, _), refine ⟨ind, ∅, λ _, false.elim, is_supported_zero, _⟩, rw [(restriction _).map_zero, (free_comm_ring.lift _).map_zero] }, { rintros x y ⟨i, s, hi, hxs, ihs⟩ ⟨j, t, hj, hyt, iht⟩, rcases directed_order.directed i j with ⟨k, hik, hjk⟩, have : ∀ z : Σ i, G i, z ∈ s ∪ t → z.1 ≤ k, { rintros z (hz | hz), exact le_trans (hi z hz) hik, exact le_trans (hj z hz) hjk }, refine ⟨k, s ∪ t, this, is_supported_add (is_supported_upwards hxs $ set.subset_union_left s t) (is_supported_upwards hyt $ set.subset_union_right s t), _⟩, { rw [(restriction _).map_add, (free_comm_ring.lift _).map_add, ← of.zero_exact_aux2 G f' hxs hi this hik (set.subset_union_left s t), ← of.zero_exact_aux2 G f' hyt hj this hjk (set.subset_union_right s t), ihs, (f' i k hik).map_zero, iht, (f' j k hjk).map_zero, zero_add] } }, { rintros x y ⟨j, t, hj, hyt, iht⟩, rw smul_eq_mul, rcases exists_finset_support x with ⟨s, hxs⟩, rcases (s.image sigma.fst).exists_le with ⟨i, hi⟩, rcases directed_order.directed i j with ⟨k, hik, hjk⟩, have : ∀ z : Σ i, G i, z ∈ ↑s ∪ t → z.1 ≤ k, { rintros z (hz | hz), exacts [(hi z.1 $ finset.mem_image.2 ⟨z, hz, rfl⟩).trans hik, (hj z hz).trans hjk] }, refine ⟨k, ↑s ∪ t, this, is_supported_mul (is_supported_upwards hxs $ set.subset_union_left ↑s t) (is_supported_upwards hyt $ set.subset_union_right ↑s t), _⟩, rw [(restriction _).map_mul, (free_comm_ring.lift _).map_mul, ← of.zero_exact_aux2 G f' hyt hj this hjk (set.subset_union_right ↑s t), iht, (f' j k hjk).map_zero, mul_zero] } end /-- A component that corresponds to zero in the direct limit is already zero in some bigger module in the directed system. -/ lemma of.zero_exact {i x} (hix : of G (λ i j h, f' i j h) i x = 0) : ∃ j (hij : i ≤ j), f' i j hij x = 0 := by haveI : nonempty ι := ⟨i⟩; exact let ⟨j, s, H, hxs, hx⟩ := of.zero_exact_aux hix in have hixs : (⟨i, x⟩ : Σ i, G i) ∈ s, from is_supported_of.1 hxs, ⟨j, H ⟨i, x⟩ hixs, by rw [restriction_of, dif_pos hixs, lift_of] at hx; exact hx⟩ end of_zero_exact variables (f' : Π i j, i ≤ j → G i →+* G j) /-- If the maps in the directed system are injective, then the canonical maps from the components to the direct limits are injective. -/ theorem of_injective [directed_system G (λ i j h, f' i j h)] (hf : ∀ i j hij, function.injective (f' i j hij)) (i) : function.injective (of G (λ i j h, f' i j h) i) := begin suffices : ∀ x, of G (λ i j h, f' i j h) i x = 0 → x = 0, { intros x y hxy, rw ← sub_eq_zero, apply this, rw [(of G _ i).map_sub, hxy, sub_self] }, intros x hx, rcases of.zero_exact hx with ⟨j, hij, hfx⟩, apply hf i j hij, rw [hfx, (f' i j hij).map_zero] end variables (P : Type u₁) [comm_ring P] variables (g : Π i, G i →+* P) variables (Hg : ∀ i j hij x, g j (f i j hij x) = g i x) include Hg open free_comm_ring variables (G f) /-- The universal property of the direct limit: maps from the components to another ring that respect the directed system structure (i.e. make some diagram commute) give rise to a unique map out of the direct limit. -/ def lift : direct_limit G f →+* P := ideal.quotient.lift _ (free_comm_ring.lift $ λ (x : Σ i, G i), g x.1 x.2) begin suffices : ideal.span _ ≤ ideal.comap (free_comm_ring.lift (λ (x : Σ (i : ι), G i), g (x.fst) (x.snd))) ⊥, { intros x hx, exact (mem_bot P).1 (this hx) }, rw ideal.span_le, intros x hx, rw [set_like.mem_coe, ideal.mem_comap, mem_bot], rcases hx with ⟨i, j, hij, x, rfl⟩ | ⟨i, rfl⟩ | ⟨i, x, y, rfl⟩ | ⟨i, x, y, rfl⟩; simp only [ring_hom.map_sub, lift_of, Hg, ring_hom.map_one, ring_hom.map_add, ring_hom.map_mul, (g i).map_one, (g i).map_add, (g i).map_mul, sub_self] end variables {G f} omit Hg @[simp] lemma lift_of (i x) : lift G f P g Hg (of G f i x) = g i x := free_comm_ring.lift_of _ _ theorem lift_unique [nonempty ι] (F : direct_limit G f →+* P) (x) : F x = lift G f P (λ i, F.comp $ of G f i) (λ i j hij x, by simp) x := direct_limit.induction_on x $ λ i x, by simp end direct_limit end end ring namespace field variables [nonempty ι] [Π i, field (G i)] variables (f : Π i j, i ≤ j → G i → G j) variables (f' : Π i j, i ≤ j → G i →+* G j) namespace direct_limit instance nontrivial [directed_system G (λ i j h, f' i j h)] : nontrivial (ring.direct_limit G (λ i j h, f' i j h)) := ⟨⟨0, 1, nonempty.elim (by apply_instance) $ assume i : ι, begin change (0 : ring.direct_limit G (λ i j h, f' i j h)) ≠ 1, rw ← (ring.direct_limit.of _ _ _).map_one, intros H, rcases ring.direct_limit.of.zero_exact H.symm with ⟨j, hij, hf⟩, rw (f' i j hij).map_one at hf, exact one_ne_zero hf end ⟩⟩ theorem exists_inv {p : ring.direct_limit G f} : p ≠ 0 → ∃ y, p * y = 1 := ring.direct_limit.induction_on p $ λ i x H, ⟨ring.direct_limit.of G f i (x⁻¹), by erw [← (ring.direct_limit.of _ _ _).map_mul, mul_inv_cancel (assume h : x = 0, H $ by rw [h, (ring.direct_limit.of _ _ _).map_zero]), (ring.direct_limit.of _ _ _).map_one]⟩ section open_locale classical /-- Noncomputable multiplicative inverse in a direct limit of fields. -/ noncomputable def inv (p : ring.direct_limit G f) : ring.direct_limit G f := if H : p = 0 then 0 else classical.some (direct_limit.exists_inv G f H) protected theorem mul_inv_cancel {p : ring.direct_limit G f} (hp : p ≠ 0) : p * inv G f p = 1 := by rw [inv, dif_neg hp, classical.some_spec (direct_limit.exists_inv G f hp)] protected theorem inv_mul_cancel {p : ring.direct_limit G f} (hp : p ≠ 0) : inv G f p * p = 1 := by rw [_root_.mul_comm, direct_limit.mul_inv_cancel G f hp] /-- Noncomputable field structure on the direct limit of fields. -/ protected noncomputable def field [directed_system G (λ i j h, f' i j h)] : field (ring.direct_limit G (λ i j h, f' i j h)) := { inv := inv G (λ i j h, f' i j h), mul_inv_cancel := λ p, direct_limit.mul_inv_cancel G (λ i j h, f' i j h), inv_zero := dif_pos rfl, .. ring.direct_limit.comm_ring G (λ i j h, f' i j h), .. direct_limit.nontrivial G (λ i j h, f' i j h) } end end direct_limit end field
36b4c0b46f1e32957e8968a4b40fd1b72e44551a
a49a42dda6e1ae18983e9264eb962585a900fa99
/src/gamma.lean
b9e6cdd98420e90a596a707d65195972197bb6c3
[]
no_license
jamesa9283/Lean-AreaOfACircle
a5d22cbc96bc2cf2f76b79221b0ec65628edc333
c5d5030eeab98bb6552693de55a77a9cf2c21d20
refs/heads/master
1,676,179,712,721
1,610,133,816,000
1,610,133,816,000
327,885,903
0
0
null
null
null
null
UTF-8
Lean
false
false
1,049
lean
import analysis.special_functions.exp_log analysis.special_functions.pow open_locale big_operators nat noncomputable theory open complex def poch (a : ℂ) (n : ℕ) := ∏ i in finset.range n, a + n def rΓ_series (z : ℂ) (n : ℕ) := poch z (n + 1) / (n! * exp (z * log n)) def Γ_series (z : ℂ) (n : ℕ) := (n! : ℂ) * exp(z * log n) / poch z (n + 1) def Γ_series' (z : ℂ) (n : ℕ) := ((n - 1)! : ℂ) * exp (z * log n) / poch z n lemma rΓ_eq_inv_Γ (z : ℂ) (n : ℕ) : Γ_series z n = (rΓ_series z n)⁻¹ := by field_simp [rΓ_series, Γ_series] def ln_Gamma_series (z : ℂ) (n : ℕ) := z * log (n) - log z - ∑ k in finset.range n, log(z / (k+1) + 1) lemma lnG_def (z : ℂ) (n : ℕ) : ln_Gamma_series z n = z * log (n) - log z - ∑ k in finset.range n, log(z / (k+1) + 1) := rfl def ln_Gamma (x : ℝ) := x -- let us asssume convergence, because I don't know how to prove it. lemma ln_Γ_complex_converges (z : ℂ) (n : ℕ) : filter.tendsto (ln_Gamma_series z) (filter.at_top) (nhds z) := begin sorry end