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
ee00f8dc5e42189716c2b2bb334d200658a810d7
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/algebra/geom_sum.lean
4ce9555372f01466d18920c15803edcfebcc1154
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
5,405
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland Sums of finite geometric series -/ import algebra.big_operators algebra.commute universe u variable {α : Type u} open finset /-- Sum of the finite geometric series $∑_{i=0}^{n-1} x^i$. -/ def geom_series [semiring α] (x : α) (n : ℕ) := (range n).sum (λ i, x ^ i) theorem geom_series_def [semiring α] (x : α) (n : ℕ) : geom_series x n = (range n).sum (λ i, x ^ i) := rfl @[simp] theorem geom_series_zero [semiring α] (x : α) : geom_series x 0 = 0 := rfl @[simp] theorem geom_series_one [semiring α] (x : α) : geom_series x 1 = 1 := by { rw [geom_series_def, sum_range_one, pow_zero] } /-- Sum of the finite geometric series $∑_{i=0}^{n-1} x^i y^{n-1-i}$. -/ def geom_series₂ [semiring α] (x y : α) (n : ℕ) := (range n).sum (λ i, x ^ i * (y ^ (n - 1 - i))) theorem geom_series₂_def [semiring α] (x y : α) (n : ℕ) : geom_series₂ x y n = (range n).sum (λ i, x ^ i * y ^ (n - 1 - i)) := rfl @[simp] theorem geom_series₂_zero [semiring α] (x y : α) : geom_series₂ x y 0 = 0 := rfl @[simp] theorem geom_series₂_one [semiring α] (x y : α) : geom_series₂ x y 1 = 1 := by { have : 1 - 1 - 0 = 0 := rfl, rw [geom_series₂_def, sum_range_one, this, pow_zero, pow_zero, mul_one] } @[simp] theorem geom_series₂_with_one [semiring α] (x : α) (n : ℕ) : geom_series₂ x 1 n = geom_series x n := sum_congr rfl (λ i _, by { rw [one_pow, mul_one] }) /-- $x^n-y^n=(x-y)∑x^ky^{n-1-k}$ reformulated without `-` signs. -/ protected theorem commute.geom_sum₂_mul_add [semiring α] {x y : α} (h : commute x y) (n : ℕ) : (geom_series₂ (x + y) y n) * x + y ^ n = (x + y) ^ n := begin let f := λ (m i : ℕ), (x + y) ^ i * y ^ (m - 1 - i), change ((range n).sum (f n)) * x + y ^ n = (x + y) ^ n, induction n with n ih, { rw [range_zero, sum_empty, zero_mul, zero_add, pow_zero, pow_zero] }, { have f_last : f n.succ n = (x + y) ^ n := by { dsimp [f], rw [nat.sub_sub, nat.add_comm, nat.sub_self, pow_zero, mul_one] }, have f_succ : ∀ i, i ∈ range n → f n.succ i = y * f n i := λ i hi, by { dsimp [f], have : commute y ((x + y) ^ i) := (h.symm.add_right (commute.refl y)).pow_right i, rw [← mul_assoc, this.eq, mul_assoc, ← pow_succ y (n - 1 - i)], congr' 2, rw [nat.succ_eq_add_one, nat.add_sub_cancel, nat.sub_sub, add_comm 1 i], have := nat.add_sub_of_le (mem_range.mp hi), rw [add_comm, nat.succ_eq_add_one] at this, rw [← this, nat.add_sub_cancel, add_comm i 1, ← add_assoc, nat.add_sub_cancel] }, rw [pow_succ (x + y), add_mul, sum_range_succ, f_last, add_mul, add_assoc], rw [(((commute.refl x).add_right h).pow_right n).eq], congr' 1, rw[sum_congr rfl f_succ, ← mul_sum, pow_succ y], rw[mul_assoc, ← mul_add y, ih] } end /-- $x^n-y^n=(x-y)∑x^ky^{n-1-k}$ reformulated without `-` signs. -/ theorem geom_sum₂_mul_add [comm_semiring α] (x y : α) (n : ℕ) : (geom_series₂ (x + y) y n) * x + y ^ n = (x + y) ^ n := (commute.all x y).geom_sum₂_mul_add n theorem geom_sum_mul_add [semiring α] (x : α) (n : ℕ) : (geom_series (x + 1) n) * x + 1 = (x + 1) ^ n := begin have := (commute.one_right x).geom_sum₂_mul_add n, rw [one_pow, geom_series₂_with_one] at this, exact this end theorem geom_sum₂_mul_comm [ring α] {x y : α} (h : commute x y) (n : ℕ) : (geom_series₂ x y n) * (x - y) = x ^ n - y ^ n := begin have := (h.sub_left (commute.refl y)).geom_sum₂_mul_add n, rw [sub_add_cancel] at this, rw [← this, add_sub_cancel] end theorem geom_sum₂_mul [comm_ring α] (x y : α) (n : ℕ) : (geom_series₂ x y n) * (x - y) = x ^ n - y ^ n := geom_sum₂_mul_comm (commute.all x y) n theorem geom_sum_mul [ring α] (x : α) (n : ℕ) : (geom_series x n) * (x - 1) = x ^ n - 1 := begin have := geom_sum₂_mul_comm (commute.one_right x) n, rw [one_pow, geom_series₂_with_one] at this, exact this end theorem geom_sum_mul_neg [ring α] (x : α) (n : ℕ) : (geom_series x n) * (1 - x) = 1 - x ^ n := begin have := congr_arg has_neg.neg (geom_sum_mul x n), rw [neg_sub, ← mul_neg_eq_neg_mul_symm, neg_sub] at this, exact this end theorem geom_sum [division_ring α] {x : α} (h : x ≠ 1) (n : ℕ) : (geom_series x n) = (x ^ n - 1) / (x - 1) := have x - 1 ≠ 0, by simp [*, -sub_eq_add_neg, sub_eq_iff_eq_add] at *, by rw [← geom_sum_mul, mul_div_cancel _ this] lemma geom_sum_inv [division_ring α] {x : α} (hx1 : x ≠ 1) (hx0 : x ≠ 0) (n : ℕ) : (geom_series x⁻¹ n) = (x - 1)⁻¹ * (x - x⁻¹ ^ n * x) := have h₁ : x⁻¹ ≠ 1, by rwa [inv_eq_one_div, ne.def, div_eq_iff_mul_eq hx0, one_mul], have h₂ : x⁻¹ - 1 ≠ 0, from mt sub_eq_zero.1 h₁, have h₃ : x - 1 ≠ 0, from mt sub_eq_zero.1 hx1, have h₄ : x * x⁻¹ ^ n = x⁻¹ ^ n * x, from nat.cases_on n (by simp) (λ _, by conv { to_rhs, rw [pow_succ', mul_assoc, inv_mul_cancel hx0, mul_one] }; rw [pow_succ, ← mul_assoc, mul_inv_cancel hx0, one_mul]), by rw [geom_sum h₁, div_eq_iff_mul_eq h₂, ← domain.mul_left_inj h₃, ← mul_assoc, ← mul_assoc, mul_inv_cancel h₃]; simp [mul_add, add_mul, mul_inv_cancel hx0, mul_assoc, h₄]
46b6ffb874fb0e92e6f52cd8f1c72ffc907f8b27
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/polynomial/basic.lean
0108ac8987e6e2460ada45b75992be85dea208b0
[ "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
50,304
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 algebra.char_p.basic import data.mv_polynomial.comm_ring import data.mv_polynomial.equiv import ring_theory.polynomial.content import ring_theory.unique_factorization_domain /-! # Ring-theoretic supplement of data.polynomial. ## Main results * `mv_polynomial.is_domain`: If a ring is an integral domain, then so is its polynomial ring over finitely many variables. * `polynomial.is_noetherian_ring`: Hilbert basis theorem, that if a ring is noetherian then so is its polynomial ring. * `polynomial.wf_dvd_monoid`: If an integral domain is a `wf_dvd_monoid`, then so is its polynomial ring. * `polynomial.unique_factorization_monoid`, `mv_polynomial.unique_factorization_monoid`: If an integral domain is a `unique_factorization_monoid`, then so is its polynomial ring (of any number of variables). -/ noncomputable theory open_locale classical big_operators polynomial open finset universes u v w variables {R : Type u} {S : Type*} namespace polynomial section semiring variables [semiring R] instance (p : ℕ) [h : char_p R p] : char_p R[X] p := let ⟨h⟩ := h in ⟨λ n, by rw [← map_nat_cast C, ← C_0, C_inj, h]⟩ variables (R) /-- The `R`-submodule of `R[X]` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R R[X] := ⨅ k : ℕ, ⨅ h : ↑k > n, (lcoeff R k).ker /-- The `R`-submodule of `R[X]` consisting of polynomials of degree < `n`. -/ def degree_lt (n : ℕ) : submodule R R[X] := ⨅ k : ℕ, ⨅ h : k ≥ n, (lcoeff R k).ker variable {R} theorem mem_degree_le {n : with_bot ℕ} {f : R[X]} : f ∈ degree_le R n ↔ degree f ≤ n := by simp only [degree_le, submodule.mem_infi, degree_le_iff_coeff_zero, linear_map.mem_ker]; refl @[mono] theorem degree_le_mono {m n : with_bot ℕ} (H : m ≤ n) : degree_le R m ≤ degree_le R n := λ f hf, mem_degree_le.2 (le_trans (mem_degree_le.1 hf) H) theorem degree_le_eq_span_X_pow {n : ℕ} : degree_le R n = submodule.span R ↑((finset.range (n+1)).image (λ n, (X : R[X])^n)) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_le.1 hp, rw [← polynomial.sum_monomial_eq p, polynomial.sum], refine submodule.sum_mem _ (λ k hk, _), show monomial _ _ ∈ _, have := with_bot.coe_le_coe.1 (finset.sup_le_iff.1 hp k hk), rw [monomial_eq_C_mul_X, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 (nat.lt_succ_of_le this), rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_le.2, exact (degree_X_pow_le _).trans (with_bot.coe_le_coe.2 $ nat.le_of_lt_succ $ finset.mem_range.1 hk) end theorem mem_degree_lt {n : ℕ} {f : R[X]} : f ∈ degree_lt R n ↔ degree f < n := by { simp_rw [degree_lt, submodule.mem_infi, linear_map.mem_ker, degree, finset.max_eq_sup_coe, finset.sup_lt_iff (with_bot.bot_lt_coe n), mem_support_iff, with_bot.coe_lt_coe, lt_iff_not_le, ne, not_imp_not], refl } @[mono] theorem degree_lt_mono {m n : ℕ} (H : m ≤ n) : degree_lt R m ≤ degree_lt R n := λ f hf, mem_degree_lt.2 (lt_of_lt_of_le (mem_degree_lt.1 hf) $ with_bot.coe_le_coe.2 H) theorem degree_lt_eq_span_X_pow {n : ℕ} : degree_lt R n = submodule.span R ↑((finset.range n).image (λ n, X^n) : finset R[X]) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_lt.1 hp, rw [← polynomial.sum_monomial_eq p, polynomial.sum], refine submodule.sum_mem _ (λ k hk, _), show monomial _ _ ∈ _, have := with_bot.coe_lt_coe.1 ((finset.sup_lt_iff $ with_bot.bot_lt_coe n).1 hp k hk), rw [monomial_eq_C_mul_X, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 this, rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_lt.2, exact lt_of_le_of_lt (degree_X_pow_le _) (with_bot.coe_lt_coe.2 $ finset.mem_range.1 hk) end /-- The first `n` coefficients on `degree_lt n` form a linear equivalence with `fin n → R`. -/ def degree_lt_equiv (R) [semiring R] (n : ℕ) : degree_lt R n ≃ₗ[R] (fin n → R) := { to_fun := λ p n, (↑p : R[X]).coeff n, inv_fun := λ f, ⟨∑ i : fin n, monomial i (f i), (degree_lt R n).sum_mem (λ i _, mem_degree_lt.mpr (lt_of_le_of_lt (degree_monomial_le i (f i)) (with_bot.coe_lt_coe.mpr i.is_lt)))⟩, map_add' := λ p q, by { ext, rw [submodule.coe_add, coeff_add], refl }, map_smul' := λ x p, by { ext, rw [submodule.coe_smul, coeff_smul], refl }, left_inv := begin rintro ⟨p, hp⟩, ext1, simp only [submodule.coe_mk], by_cases hp0 : p = 0, { subst hp0, simp only [coeff_zero, linear_map.map_zero, finset.sum_const_zero] }, rw [mem_degree_lt, degree_eq_nat_degree hp0, with_bot.coe_lt_coe] at hp, conv_rhs { rw [p.as_sum_range' n hp, ← fin.sum_univ_eq_sum_range] }, end, right_inv := begin intro f, ext i, simp only [finset_sum_coeff, submodule.coe_mk], rw [finset.sum_eq_single i, coeff_monomial, if_pos rfl], { rintro j - hji, rw [coeff_monomial, if_neg], rwa [← fin.ext_iff] }, { intro h, exact (h (finset.mem_univ _)).elim } end } @[simp] theorem degree_lt_equiv_eq_zero_iff_eq_zero {n : ℕ} {p : R[X]} (hp : p ∈ degree_lt R n) : degree_lt_equiv _ _ ⟨p, hp⟩ = 0 ↔ p = 0 := by rw [linear_equiv.map_eq_zero_iff, submodule.mk_eq_zero] theorem eval_eq_sum_degree_lt_equiv {n : ℕ} {p : R[X]} (hp : p ∈ degree_lt R n) (x : R) : p.eval x = ∑ i, degree_lt_equiv _ _ ⟨p, hp⟩ i * (x ^ (i : ℕ)) := begin simp_rw [eval_eq_sum], exact (sum_fin _ (by simp_rw [zero_mul, forall_const]) (mem_degree_lt.mp hp)).symm end /-- The finset of nonzero coefficients of a polynomial. -/ def frange (p : R[X]) : finset R := finset.image (λ n, p.coeff n) p.support lemma frange_zero : frange (0 : R[X]) = ∅ := rfl lemma mem_frange_iff {p : R[X]} {c : R} : c ∈ p.frange ↔ ∃ n ∈ p.support, c = p.coeff n := by simp [frange, eq_comm] lemma frange_one : frange (1 : R[X]) ⊆ {1} := begin simp [frange, finset.image_subset_iff], simp only [← C_1, coeff_C], assume n hn, simp only [exists_prop, ite_eq_right_iff, not_forall] at hn, simp [hn], end lemma coeff_mem_frange (p : R[X]) (n : ℕ) (h : p.coeff n ≠ 0) : p.coeff n ∈ p.frange := begin simp only [frange, exists_prop, mem_support_iff, finset.mem_image, ne.def], exact ⟨n, h, rfl⟩, end lemma geom_sum_X_comp_X_add_one_eq_sum (n : ℕ) : (∑ i in range n, (X : R[X]) ^ i).comp (X + 1) = (finset.range n).sum (λ (i : ℕ), (n.choose (i + 1) : R[X]) * X ^ i) := begin ext i, transitivity (n.choose (i + 1) : R), swap, { simp only [finset_sum_coeff, ← C_eq_nat_cast, coeff_C_mul_X_pow], rw [finset.sum_eq_single i, if_pos rfl], { simp only [@eq_comm _ i, if_false, eq_self_iff_true, implies_true_iff] {contextual := tt}, }, { simp only [nat.lt_add_one_iff, nat.choose_eq_zero_of_lt, nat.cast_zero, finset.mem_range, not_lt, eq_self_iff_true, if_true, implies_true_iff] {contextual := tt}, } }, induction n with n ih generalizing i, { simp only [geom_sum_zero, zero_comp, coeff_zero, nat.choose_zero_succ, nat.cast_zero], }, simp only [geom_sum_succ', ih, add_comp, X_pow_comp, coeff_add, nat.choose_succ_succ, nat.cast_add, coeff_X_add_one_pow], end lemma monic.geom_sum {P : R[X]} (hP : P.monic) (hdeg : 0 < P.nat_degree) {n : ℕ} (hn : n ≠ 0) : (∑ i in range n, P ^ i).monic := begin nontriviality R, cases n, { exact (hn rfl).elim }, rw [geom_sum_succ'], refine (hP.pow _).add_of_left _, refine lt_of_le_of_lt (degree_sum_le _ _) _, rw [finset.sup_lt_iff], { simp only [finset.mem_range, degree_eq_nat_degree (hP.pow _).ne_zero, with_bot.coe_lt_coe, hP.nat_degree_pow], intro k, exact nsmul_lt_nsmul hdeg }, { rw [bot_lt_iff_ne_bot, ne.def, degree_eq_bot], exact (hP.pow _).ne_zero } end lemma monic.geom_sum' {P : R[X]} (hP : P.monic) (hdeg : 0 < P.degree) {n : ℕ} (hn : n ≠ 0) : (∑ i in range n, P ^ i).monic := hP.geom_sum (nat_degree_pos_iff_degree_pos.2 hdeg) hn lemma monic_geom_sum_X {n : ℕ} (hn : n ≠ 0) : (∑ i in range n, (X : R[X]) ^ i).monic := begin nontriviality R, apply monic_X.geom_sum _ hn, simpa only [nat_degree_X] using zero_lt_one end end semiring section ring variables [ring R] /-- Given a polynomial, return the polynomial whose coefficients are in the ring closure of the original coefficients. -/ def restriction (p : R[X]) : polynomial (subring.closure (↑p.frange : set R)) := ∑ i in p.support, monomial i (⟨p.coeff i, if H : p.coeff i = 0 then H.symm ▸ (subring.closure _).zero_mem else subring.subset_closure (p.coeff_mem_frange _ H)⟩ : (subring.closure (↑p.frange : set R))) @[simp] theorem coeff_restriction {p : R[X]} {n : ℕ} : ↑(coeff (restriction p) n) = coeff p n := begin simp only [restriction, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ne.def, ite_not], split_ifs, { rw h, refl }, { refl } end @[simp] theorem coeff_restriction' {p : R[X]} {n : ℕ} : (coeff (restriction p) n).1 = coeff p n := coeff_restriction @[simp] lemma support_restriction (p : R[X]) : support (restriction p) = support p := begin ext i, simp only [mem_support_iff, not_iff_not, ne.def], conv_rhs { rw [← coeff_restriction] }, exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem map_restriction {R : Type u} [comm_ring R] (p : R[X]) : p.restriction.map (algebra_map _ _) = p := ext $ λ n, by rw [coeff_map, algebra.algebra_map_of_subring_apply, coeff_restriction] @[simp] theorem degree_restriction {p : R[X]} : (restriction p).degree = p.degree := by simp [degree] @[simp] theorem nat_degree_restriction {p : R[X]} : (restriction p).nat_degree = p.nat_degree := by simp [nat_degree] @[simp] theorem monic_restriction {p : R[X]} : monic (restriction p) ↔ monic p := begin simp only [monic, leading_coeff, nat_degree_restriction], rw [←@coeff_restriction _ _ p], exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem restriction_zero : restriction (0 : R[X]) = 0 := by simp only [restriction, finset.sum_empty, support_zero] @[simp] theorem restriction_one : restriction (1 : R[X]) = 1 := ext $ λ i, subtype.eq $ by rw [coeff_restriction', coeff_one, coeff_one]; split_ifs; refl variables [semiring S] {f : R →+* S} {x : S} theorem eval₂_restriction {p : R[X]} : eval₂ f x p = eval₂ (f.comp (subring.subtype (subring.closure (p.frange : set R)))) x p.restriction := begin simp only [eval₂_eq_sum, sum, support_restriction, ←@coeff_restriction _ _ p], refl, end section to_subring variables (p : R[X]) (T : subring R) /-- Given a polynomial `p` and a subring `T` that contains the coefficients of `p`, return the corresponding polynomial whose coefficients are in `T`. -/ def to_subring (hp : (↑p.frange : set R) ⊆ T) : T[X] := ∑ i in p.support, monomial i (⟨p.coeff i, if H : p.coeff i = 0 then H.symm ▸ T.zero_mem else hp (p.coeff_mem_frange _ H)⟩ : T) variables (hp : (↑p.frange : set R) ⊆ T) include hp @[simp] theorem coeff_to_subring {n : ℕ} : ↑(coeff (to_subring p T hp) n) = coeff p n := begin simp only [to_subring, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ne.def, ite_not], split_ifs, { rw h, refl }, { refl } end @[simp] theorem coeff_to_subring' {n : ℕ} : (coeff (to_subring p T hp) n).1 = coeff p n := coeff_to_subring _ _ hp @[simp] lemma support_to_subring : support (to_subring p T hp) = support p := begin ext i, simp only [mem_support_iff, not_iff_not, ne.def], conv_rhs { rw [← coeff_to_subring p T hp] }, exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem degree_to_subring : (to_subring p T hp).degree = p.degree := by simp [degree] @[simp] theorem nat_degree_to_subring : (to_subring p T hp).nat_degree = p.nat_degree := by simp [nat_degree] @[simp] theorem monic_to_subring : monic (to_subring p T hp) ↔ monic p := begin simp_rw [monic, leading_coeff, nat_degree_to_subring, ← coeff_to_subring p T hp], exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end omit hp @[simp] theorem to_subring_zero : to_subring (0 : R[X]) T (by simp [frange_zero]) = 0 := by { ext i, simp } @[simp] theorem to_subring_one : to_subring (1 : R[X]) T (set.subset.trans frange_one $finset.singleton_subset_set_iff.2 T.one_mem) = 1 := ext $ λ i, subtype.eq $ by rw [coeff_to_subring', coeff_one, coeff_one]; split_ifs; refl @[simp] theorem map_to_subring : (p.to_subring T hp).map (subring.subtype T) = p := by { ext n, simp [coeff_map] } end to_subring variables (T : subring R) /-- Given a polynomial whose coefficients are in some subring, return the corresponding polynomial whose coefficients are in the ambient ring. -/ def of_subring (p : T[X]) : R[X] := ∑ i in p.support, monomial i (p.coeff i : R) lemma coeff_of_subring (p : T[X]) (n : ℕ) : coeff (of_subring T p) n = (coeff p n : T) := begin simp only [of_subring, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ite_eq_right_iff, ne.def, ite_not, not_not, ite_eq_left_iff], assume h, rw h, refl end @[simp] theorem frange_of_subring {p : T[X]} : (↑(p.of_subring T).frange : set R) ⊆ T := begin assume i hi, simp only [frange, set.mem_image, mem_support_iff, ne.def, finset.mem_coe, finset.coe_image] at hi, rcases hi with ⟨n, hn, h'n⟩, rw [← h'n, coeff_of_subring], exact subtype.mem (coeff p n : T) end end ring section comm_ring variables [comm_ring R] section mod_by_monic variables {q : R[X]} lemma mem_ker_mod_by_monic (hq : q.monic) {p : R[X]} : p ∈ (mod_by_monic_hom q).ker ↔ q ∣ p := linear_map.mem_ker.trans (dvd_iff_mod_by_monic_eq_zero hq) @[simp] lemma ker_mod_by_monic_hom (hq : q.monic) : (polynomial.mod_by_monic_hom q).ker = (ideal.span {q}).restrict_scalars R := submodule.ext (λ f, (mem_ker_mod_by_monic hq).trans ideal.mem_span_singleton.symm) end mod_by_monic end comm_ring end polynomial namespace ideal open polynomial section semiring variables [semiring R] /-- Transport an ideal of `R[X]` to an `R`-submodule of `R[X]`. -/ def of_polynomial (I : ideal R[X]) : submodule R R[X] := { carrier := I.carrier, zero_mem' := I.zero_mem, add_mem' := λ _ _, I.add_mem, smul_mem' := λ c x H, by { rw [← C_mul'], exact I.mul_mem_left _ H } } variables {I : ideal R[X]} theorem mem_of_polynomial (x) : x ∈ I.of_polynomial ↔ x ∈ I := iff.rfl variables (I) /-- Given an ideal `I` of `R[X]`, make the `R`-submodule of `I` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R R[X] := degree_le R n ⊓ I.of_polynomial /-- Given an ideal `I` of `R[X]`, make the ideal in `R` of leading coefficients of polynomials in `I` with degree ≤ `n`. -/ def leading_coeff_nth (n : ℕ) : ideal R := (I.degree_le n).map $ lcoeff R n /-- Given an ideal `I` in `R[X]`, make the ideal in `R` of the leading coefficients in `I`. -/ def leading_coeff : ideal R := ⨆ n : ℕ, I.leading_coeff_nth n end semiring section comm_semiring variables [comm_semiring R] [semiring S] /-- If every coefficient of a polynomial is in an ideal `I`, then so is the polynomial itself -/ lemma polynomial_mem_ideal_of_coeff_mem_ideal (I : ideal R[X]) (p : R[X]) (hp : ∀ (n : ℕ), (p.coeff n) ∈ I.comap (C : R →+* R[X])) : p ∈ I := sum_C_mul_X_eq p ▸ submodule.sum_mem I (λ n hn, I.mul_mem_right _ (hp n)) /-- The push-forward of an ideal `I` of `R` to `polynomial R` via inclusion is exactly the set of polynomials whose coefficients are in `I` -/ theorem mem_map_C_iff {I : ideal R} {f : R[X]} : f ∈ (ideal.map (C : R →+* R[X]) I : ideal R[X]) ↔ ∀ n : ℕ, f.coeff n ∈ I := begin split, { intros hf, apply submodule.span_induction hf, { intros f hf n, cases (set.mem_image _ _ _).mp hf with x hx, rw [← hx.right, coeff_C], by_cases (n = 0), { simpa [h] using hx.left }, { simp [h] } }, { simp }, { exact λ f g hf hg n, by simp [I.add_mem (hf n) (hg n)] }, { refine λ f g hg n, _, rw [smul_eq_mul, coeff_mul], exact I.sum_mem (λ c hc, I.mul_mem_left (f.coeff c.fst) (hg c.snd)) } }, { intros hf, rw ← sum_monomial_eq f, refine (I.map C : ideal R[X]).sum_mem (λ n hn, _), simp [monomial_eq_C_mul_X], rw mul_comm, exact (I.map C : ideal R[X]).mul_mem_left _ (mem_map_of_mem _ (hf n)) } end lemma _root_.polynomial.ker_map_ring_hom (f : R →+* S) : (polynomial.map_ring_hom f).ker = f.ker.map (C : R →+* R[X]) := begin ext, rw [mem_map_C_iff, ring_hom.mem_ker, polynomial.ext_iff], simp_rw [coe_map_ring_hom, coeff_map, coeff_zero, ring_hom.mem_ker], end variable (I : ideal R[X]) theorem mem_leading_coeff_nth (n : ℕ) (x) : x ∈ I.leading_coeff_nth n ↔ ∃ p ∈ I, degree p ≤ n ∧ p.leading_coeff = x := begin simp only [leading_coeff_nth, degree_le, submodule.mem_map, lcoeff_apply, submodule.mem_inf, mem_degree_le], split, { rintro ⟨p, ⟨hpdeg, hpI⟩, rfl⟩, cases lt_or_eq_of_le hpdeg with hpdeg hpdeg, { refine ⟨0, I.zero_mem, bot_le, _⟩, rw [leading_coeff_zero, eq_comm], exact coeff_eq_zero_of_degree_lt hpdeg }, { refine ⟨p, hpI, le_of_eq hpdeg, _⟩, rw [polynomial.leading_coeff, nat_degree, hpdeg], refl } }, { rintro ⟨p, hpI, hpdeg, rfl⟩, have : nat_degree p + (n - nat_degree p) = n, { exact add_tsub_cancel_of_le (nat_degree_le_of_degree_le hpdeg) }, refine ⟨p * X ^ (n - nat_degree p), ⟨_, I.mul_mem_right _ hpI⟩, _⟩, { apply le_trans (degree_mul_le _ _) _, apply le_trans (add_le_add (degree_le_nat_degree) (degree_X_pow_le _)) _, rw [← with_bot.coe_add, this], exact le_rfl }, { rw [polynomial.leading_coeff, ← coeff_mul_X_pow p (n - nat_degree p), this] } } end theorem mem_leading_coeff_nth_zero (x) : x ∈ I.leading_coeff_nth 0 ↔ C x ∈ I := (mem_leading_coeff_nth _ _ _).trans ⟨λ ⟨p, hpI, hpdeg, hpx⟩, by rwa [← hpx, polynomial.leading_coeff, nat.eq_zero_of_le_zero (nat_degree_le_of_degree_le hpdeg), ← eq_C_of_degree_le_zero hpdeg], λ hx, ⟨C x, hx, degree_C_le, leading_coeff_C x⟩⟩ theorem leading_coeff_nth_mono {m n : ℕ} (H : m ≤ n) : I.leading_coeff_nth m ≤ I.leading_coeff_nth n := begin intros r hr, simp only [set_like.mem_coe, mem_leading_coeff_nth] at hr ⊢, rcases hr with ⟨p, hpI, hpdeg, rfl⟩, refine ⟨p * X ^ (n - m), I.mul_mem_right _ hpI, _, leading_coeff_mul_X_pow⟩, refine le_trans (degree_mul_le _ _) _, refine le_trans (add_le_add hpdeg (degree_X_pow_le _)) _, rw [← with_bot.coe_add, add_tsub_cancel_of_le H], exact le_rfl end theorem mem_leading_coeff (x) : x ∈ I.leading_coeff ↔ ∃ p ∈ I, polynomial.leading_coeff p = x := begin rw [leading_coeff, submodule.mem_supr_of_directed], simp only [mem_leading_coeff_nth], { split, { rintro ⟨i, p, hpI, hpdeg, rfl⟩, exact ⟨p, hpI, rfl⟩ }, rintro ⟨p, hpI, rfl⟩, exact ⟨nat_degree p, p, hpI, degree_le_nat_degree, rfl⟩ }, intros i j, exact ⟨i + j, I.leading_coeff_nth_mono (nat.le_add_right _ _), I.leading_coeff_nth_mono (nat.le_add_left _ _)⟩ end /-- If `I` is an ideal, and `pᵢ` is a finite family of polynomials each satisfying `∀ k, (pᵢ)ₖ ∈ Iⁿⁱ⁻ᵏ` for some `nᵢ`, then `p = ∏ pᵢ` also satisfies `∀ k, pₖ ∈ Iⁿ⁻ᵏ` with `n = ∑ nᵢ`. -/ lemma _root_.polynomial.coeff_prod_mem_ideal_pow_tsub {ι : Type*} (s : finset ι) (f : ι → R[X]) (I : ideal R) (n : ι → ℕ) (h : ∀ (i ∈ s) k, (f i).coeff k ∈ I ^ (n i - k)) (k : ℕ) : (s.prod f).coeff k ∈ I ^ (s.sum n - k) := begin classical, induction s using finset.induction with a s ha hs generalizing k, { rw [sum_empty, prod_empty, coeff_one, zero_tsub, pow_zero, ideal.one_eq_top], exact submodule.mem_top }, { rw [sum_insert ha, prod_insert ha, coeff_mul], apply sum_mem, rintro ⟨i, j⟩ e, obtain rfl : i + j = k := nat.mem_antidiagonal.mp e, apply ideal.pow_le_pow add_tsub_add_le_tsub_add_tsub, rw pow_add, exact ideal.mul_mem_mul (h _ (finset.mem_insert.mpr $ or.inl rfl) _) (hs (λ i hi k, h _ (finset.mem_insert.mpr $ or.inr hi) _) j) } end end comm_semiring section ring variables [ring R] /-- `polynomial R` is never a field for any ring `R`. -/ lemma polynomial_not_is_field : ¬ is_field R[X] := begin nontriviality R, intro hR, obtain ⟨p, hp⟩ := hR.mul_inv_cancel X_ne_zero, have hp0 : p ≠ 0, { rintro rfl, rw [mul_zero] at hp, exact zero_ne_one hp }, have := degree_lt_degree_mul_X hp0, rw [←X_mul, congr_arg degree hp, degree_one, nat.with_bot.lt_zero_iff, degree_eq_bot] at this, exact hp0 this, end /-- The only constant in a maximal ideal over a field is `0`. -/ lemma eq_zero_of_constant_mem_of_maximal (hR : is_field R) (I : ideal R[X]) [hI : I.is_maximal] (x : R) (hx : C x ∈ I) : x = 0 := begin refine classical.by_contradiction (λ hx0, hI.ne_top ((eq_top_iff_one I).2 _)), obtain ⟨y, hy⟩ := hR.mul_inv_cancel hx0, convert I.mul_mem_left (C y) hx, rw [← C.map_mul, hR.mul_comm y x, hy, ring_hom.map_one], end end ring section comm_ring variables [comm_ring R] lemma quotient_map_C_eq_zero {I : ideal R} : ∀ a ∈ I, ((quotient.mk (map (C : R →+* R[X]) I : ideal R[X])).comp C) a = 0 := begin intros a ha, rw [ring_hom.comp_apply, quotient.eq_zero_iff_mem], exact mem_map_of_mem _ ha, end lemma eval₂_C_mk_eq_zero {I : ideal R} : ∀ f ∈ (map (C : R →+* R[X]) I : ideal R[X]), eval₂_ring_hom (C.comp (quotient.mk I)) X f = 0 := begin intros a ha, rw ← sum_monomial_eq a, dsimp, rw eval₂_sum, refine finset.sum_eq_zero (λ n hn, _), dsimp, rw eval₂_monomial (C.comp (quotient.mk I)) X, refine mul_eq_zero_of_left (polynomial.ext (λ m, _)) (X ^ n), erw coeff_C, by_cases h : m = 0, { simpa [h] using quotient.eq_zero_iff_mem.2 ((mem_map_C_iff.1 ha) n) }, { simp [h] } end /-- If `I` is an ideal of `R`, then the ring polynomials over the quotient ring `I.quotient` is isomorphic to the quotient of `polynomial R` by the ideal `map C I`, where `map C I` contains exactly the polynomials whose coefficients all lie in `I` -/ def polynomial_quotient_equiv_quotient_polynomial (I : ideal R) : polynomial (R ⧸ I) ≃+* R[X] ⧸ (map C I : ideal R[X]) := { to_fun := eval₂_ring_hom (quotient.lift I ((quotient.mk (map C I : ideal R[X])).comp C) quotient_map_C_eq_zero) ((quotient.mk (map C I : ideal R[X]) X)), inv_fun := quotient.lift (map C I : ideal R[X]) (eval₂_ring_hom (C.comp (quotient.mk I)) X) eval₂_C_mk_eq_zero, map_mul' := λ f g, by simp only [coe_eval₂_ring_hom, eval₂_mul], map_add' := λ f g, by simp only [eval₂_add, coe_eval₂_ring_hom], left_inv := begin intro f, apply polynomial.induction_on' f, { intros p q hp hq, simp only [coe_eval₂_ring_hom] at hp, simp only [coe_eval₂_ring_hom] at hq, simp only [coe_eval₂_ring_hom, hp, hq, ring_hom.map_add] }, { rintros n ⟨x⟩, simp only [monomial_eq_smul_X, C_mul', quotient.lift_mk, submodule.quotient.quot_mk_eq_mk, quotient.mk_eq_mk, eval₂_X_pow, eval₂_smul, coe_eval₂_ring_hom, ring_hom.map_pow, eval₂_C, ring_hom.coe_comp, ring_hom.map_mul, eval₂_X] } end, right_inv := begin rintro ⟨f⟩, apply polynomial.induction_on' f, { simp_intros p q hp hq, rw [hp, hq] }, { intros n a, simp only [monomial_eq_smul_X, ← C_mul' a (X ^ n), quotient.lift_mk, submodule.quotient.quot_mk_eq_mk, quotient.mk_eq_mk, eval₂_X_pow, eval₂_smul, coe_eval₂_ring_hom, ring_hom.map_pow, eval₂_C, ring_hom.coe_comp, ring_hom.map_mul, eval₂_X] }, end, } @[simp] lemma polynomial_quotient_equiv_quotient_polynomial_symm_mk (I : ideal R) (f : R[X]) : I.polynomial_quotient_equiv_quotient_polynomial.symm (quotient.mk _ f) = f.map (quotient.mk I) := by rw [polynomial_quotient_equiv_quotient_polynomial, ring_equiv.symm_mk, ring_equiv.coe_mk, ideal.quotient.lift_mk, coe_eval₂_ring_hom, eval₂_eq_eval_map, ←polynomial.map_map, ←eval₂_eq_eval_map, polynomial.eval₂_C_X] @[simp] lemma polynomial_quotient_equiv_quotient_polynomial_map_mk (I : ideal R) (f : R[X]) : I.polynomial_quotient_equiv_quotient_polynomial (f.map I^.quotient.mk) = quotient.mk _ f := begin apply (polynomial_quotient_equiv_quotient_polynomial I).symm.injective, rw [ring_equiv.symm_apply_apply, polynomial_quotient_equiv_quotient_polynomial_symm_mk], end /-- If `P` is a prime ideal of `R`, then `R[x]/(P)` is an integral domain. -/ lemma is_domain_map_C_quotient {P : ideal R} (H : is_prime P) : is_domain (R[X] ⧸ (map (C : R →+* R[X]) P : ideal R[X])) := ring_equiv.is_domain (polynomial (R ⧸ P)) (polynomial_quotient_equiv_quotient_polynomial P).symm /-- If `P` is a prime ideal of `R`, then `P.R[x]` is a prime ideal of `R[x]`. -/ lemma is_prime_map_C_of_is_prime {P : ideal R} (H : is_prime P) : is_prime (map (C : R →+* R[X]) P : ideal R[X]) := (quotient.is_domain_iff_prime (map C P : ideal R[X])).mp (is_domain_map_C_quotient H) /-- Given any ring `R` and an ideal `I` of `polynomial R`, we get a map `R → R[x] → R[x]/I`. If we let `R` be the image of `R` in `R[x]/I` then we also have a map `R[x] → R'[x]`. In particular we can map `I` across this map, to get `I'` and a new map `R' → R'[x] → R'[x]/I`. This theorem shows `I'` will not contain any non-zero constant polynomials -/ lemma eq_zero_of_polynomial_mem_map_range (I : ideal R[X]) (x : ((quotient.mk I).comp C).range) (hx : C x ∈ (I.map (polynomial.map_ring_hom ((quotient.mk I).comp C).range_restrict))) : x = 0 := begin let i := ((quotient.mk I).comp C).range_restrict, have hi' : (polynomial.map_ring_hom i).ker ≤ I, { refine λ f hf, polynomial_mem_ideal_of_coeff_mem_ideal I f (λ n, _), rw [mem_comap, ← quotient.eq_zero_iff_mem, ← ring_hom.comp_apply], rw [ring_hom.mem_ker, coe_map_ring_hom] at hf, replace hf := congr_arg (λ (f : polynomial _), f.coeff n) hf, simp only [coeff_map, coeff_zero] at hf, rwa [subtype.ext_iff, ring_hom.coe_range_restrict] at hf }, obtain ⟨x, hx'⟩ := x, obtain ⟨y, rfl⟩ := (ring_hom.mem_range).1 hx', refine subtype.eq _, simp only [ring_hom.comp_apply, quotient.eq_zero_iff_mem, zero_mem_class.coe_zero, subtype.val_eq_coe], suffices : C (i y) ∈ (I.map (polynomial.map_ring_hom i)), { obtain ⟨f, hf⟩ := mem_image_of_mem_map_of_surjective (polynomial.map_ring_hom i) (polynomial.map_surjective _ (((quotient.mk I).comp C).range_restrict_surjective)) this, refine sub_add_cancel (C y) f ▸ I.add_mem (hi' _ : (C y - f) ∈ I) hf.1, rw [ring_hom.mem_ker, ring_hom.map_sub, hf.2, sub_eq_zero, coe_map_ring_hom, map_C] }, exact hx, end theorem is_fg_degree_le [is_noetherian_ring R] (I : ideal R[X]) (n : ℕ) : submodule.fg (I.degree_le n) := is_noetherian_submodule_left.1 (is_noetherian_of_fg_of_noetherian _ ⟨_, degree_le_eq_span_X_pow.symm⟩) _ end comm_ring end ideal variables {σ : Type v} {M : Type w} variables [comm_ring R] [comm_ring S] [add_comm_group M] [module R M] section prime variables (σ) {r : R} namespace polynomial lemma prime_C_iff : prime (C r) ↔ prime r := ⟨ comap_prime C (eval_ring_hom (0 : R)) (λ r, eval_C), λ hr, by { have := hr.1, rw ← ideal.span_singleton_prime at hr ⊢, { convert ideal.is_prime_map_C_of_is_prime hr using 1, rw [ideal.map_span, set.image_singleton] }, exacts [λ h, this (C_eq_zero.1 h), this] } ⟩ end polynomial namespace mv_polynomial private lemma prime_C_iff_of_fintype [fintype σ] : prime (C r : mv_polynomial σ R) ↔ prime r := begin rw (rename_equiv R (fintype.equiv_fin σ)).to_mul_equiv.prime_iff, convert_to prime (C r) ↔ _, { congr, apply rename_C }, { symmetry, induction fintype.card σ with d hd, { exact (is_empty_alg_equiv R (fin 0)).to_mul_equiv.symm.prime_iff }, { rw [hd, ← polynomial.prime_C_iff], convert (fin_succ_equiv R d).to_mul_equiv.symm.prime_iff, rw ← fin_succ_equiv_comp_C_eq_C, refl } }, end lemma prime_C_iff : prime (C r : mv_polynomial σ R) ↔ prime r := ⟨ comap_prime C constant_coeff constant_coeff_C, λ hr, ⟨ λ h, hr.1 $ by { rw [← C_inj, h], simp }, λ h, hr.2.1 $ by { rw ← constant_coeff_C r, exact h.map _ }, λ a b hd, begin obtain ⟨s,a',b',rfl,rfl⟩ := exists_finset_rename₂ a b, rw ← algebra_map_eq at hd, have : algebra_map R _ r ∣ a' * b', { convert (kill_compl subtype.coe_injective).to_ring_hom.map_dvd hd, simpa, simp }, rw ← rename_C (coe : s → σ), let f := (rename (coe : s → σ)).to_ring_hom, exact (((prime_C_iff_of_fintype s).2 hr).2.2 a' b' this).imp f.map_dvd f.map_dvd, end ⟩ ⟩ variable {σ} lemma prime_rename_iff (s : set σ) {p : mv_polynomial s R} : prime (rename (coe : s → σ) p) ↔ prime p := begin classical, symmetry, let eqv := (sum_alg_equiv R _ _).symm.trans (rename_equiv R $ (equiv.sum_comm ↥sᶜ s).trans $ equiv.set.sum_compl s), rw [← prime_C_iff ↥sᶜ, eqv.to_mul_equiv.prime_iff], convert iff.rfl, suffices : (rename coe).to_ring_hom = eqv.to_alg_hom.to_ring_hom.comp C, { apply ring_hom.congr_fun this }, { apply ring_hom_ext, { intro, dsimp [eqv], erw [iter_to_sum_C_C, rename_C, rename_C] }, { intro, dsimp [eqv], erw [iter_to_sum_C_X, rename_X, rename_X], refl } }, end end mv_polynomial end prime namespace polynomial @[priority 100] instance {R : Type*} [comm_ring R] [is_domain R] [wf_dvd_monoid R] : wf_dvd_monoid R[X] := { well_founded_dvd_not_unit := begin classical, refine rel_hom_class.well_founded (⟨λ (p : R[X]), ((if p = 0 then ⊤ else ↑p.degree : with_top (with_bot ℕ)), p.leading_coeff), _⟩ : dvd_not_unit →r prod.lex (<) dvd_not_unit) (prod.lex_wf (with_top.well_founded_lt $ with_bot.well_founded_lt nat.lt_wf) ‹wf_dvd_monoid R›.well_founded_dvd_not_unit), rintros a b ⟨ane0, ⟨c, ⟨not_unit_c, rfl⟩⟩⟩, rw [polynomial.degree_mul, if_neg ane0], split_ifs with hac, { rw [hac, polynomial.leading_coeff_zero], apply prod.lex.left, exact lt_of_le_of_ne le_top with_top.coe_ne_top }, have cne0 : c ≠ 0 := right_ne_zero_of_mul hac, simp only [cne0, ane0, polynomial.leading_coeff_mul], by_cases hdeg : c.degree = 0, { simp only [hdeg, add_zero], refine prod.lex.right _ ⟨_, ⟨c.leading_coeff, (λ unit_c, not_unit_c _), rfl⟩⟩, { rwa [ne, polynomial.leading_coeff_eq_zero] }, rw [polynomial.is_unit_iff, polynomial.eq_C_of_degree_eq_zero hdeg], use [c.leading_coeff, unit_c], rw [polynomial.leading_coeff, polynomial.nat_degree_eq_of_degree_eq_some hdeg] }, { apply prod.lex.left, rw polynomial.degree_eq_nat_degree cne0 at *, rw [with_top.coe_lt_coe, polynomial.degree_eq_nat_degree ane0, ← with_bot.coe_add, with_bot.coe_lt_coe], exact lt_add_of_pos_right _ (nat.pos_of_ne_zero (λ h, hdeg (h.symm ▸ with_bot.coe_zero))) }, end } end polynomial /-- Hilbert basis theorem: a polynomial ring over a noetherian ring is a noetherian ring. -/ protected theorem polynomial.is_noetherian_ring [is_noetherian_ring R] : is_noetherian_ring R[X] := is_noetherian_ring_iff.2 ⟨assume I : ideal R[X], let M := well_founded.min (is_noetherian_iff_well_founded.1 (by apply_instance)) (set.range I.leading_coeff_nth) ⟨_, ⟨0, rfl⟩⟩ in have hm : M ∈ set.range I.leading_coeff_nth := well_founded.min_mem _ _ _, let ⟨N, HN⟩ := hm, ⟨s, hs⟩ := I.is_fg_degree_le N in have hm2 : ∀ k, I.leading_coeff_nth k ≤ M := λ k, or.cases_on (le_or_lt k N) (λ h, HN ▸ I.leading_coeff_nth_mono h) (λ h x hx, classical.by_contradiction $ λ hxm, have ¬M < I.leading_coeff_nth k, by refine well_founded.not_lt_min (well_founded_submodule_gt _ _) _ _ _; exact ⟨k, rfl⟩, this ⟨HN ▸ I.leading_coeff_nth_mono (le_of_lt h), λ H, hxm (H hx)⟩), have hs2 : ∀ {x}, x ∈ I.degree_le N → x ∈ ideal.span (↑s : set R[X]), from hs ▸ λ x hx, submodule.span_induction hx (λ _ hx, ideal.subset_span hx) (ideal.zero_mem _) (λ _ _, ideal.add_mem _) (λ c f hf, f.C_mul' c ▸ ideal.mul_mem_left _ _ hf), ⟨s, le_antisymm (ideal.span_le.2 $ λ x hx, have x ∈ I.degree_le N, from hs ▸ submodule.subset_span hx, this.2) $ begin have : submodule.span R[X] ↑s = ideal.span ↑s, by refl, rw this, intros p hp, generalize hn : p.nat_degree = k, induction k using nat.strong_induction_on with k ih generalizing p, cases le_or_lt k N, { subst k, refine hs2 ⟨polynomial.mem_degree_le.2 (le_trans polynomial.degree_le_nat_degree $ with_bot.coe_le_coe.2 h), hp⟩ }, { have hp0 : p ≠ 0, { rintro rfl, cases hn, exact nat.not_lt_zero _ h }, have : (0 : R) ≠ 1, { intro h, apply hp0, ext i, refine (mul_one _).symm.trans _, rw [← h, mul_zero], refl }, haveI : nontrivial R := ⟨⟨0, 1, this⟩⟩, have : p.leading_coeff ∈ I.leading_coeff_nth N, { rw HN, exact hm2 k ((I.mem_leading_coeff_nth _ _).2 ⟨_, hp, hn ▸ polynomial.degree_le_nat_degree, rfl⟩) }, rw I.mem_leading_coeff_nth at this, rcases this with ⟨q, hq, hdq, hlqp⟩, have hq0 : q ≠ 0, { intro H, rw [← polynomial.leading_coeff_eq_zero] at H, rw [hlqp, polynomial.leading_coeff_eq_zero] at H, exact hp0 H }, have h1 : p.degree = (q * polynomial.X ^ (k - q.nat_degree)).degree, { rw [polynomial.degree_mul', polynomial.degree_X_pow], rw [polynomial.degree_eq_nat_degree hp0, polynomial.degree_eq_nat_degree hq0], rw [← with_bot.coe_add, add_tsub_cancel_of_le, hn], { refine le_trans (polynomial.nat_degree_le_of_degree_le hdq) (le_of_lt h) }, rw [polynomial.leading_coeff_X_pow, mul_one], exact mt polynomial.leading_coeff_eq_zero.1 hq0 }, have h2 : p.leading_coeff = (q * polynomial.X ^ (k - q.nat_degree)).leading_coeff, { rw [← hlqp, polynomial.leading_coeff_mul_X_pow] }, have := polynomial.degree_sub_lt h1 hp0 h2, rw [polynomial.degree_eq_nat_degree hp0] at this, rw ← sub_add_cancel p (q * polynomial.X ^ (k - q.nat_degree)), refine (ideal.span ↑s).add_mem _ ((ideal.span ↑s).mul_mem_right _ _), { by_cases hpq : p - q * polynomial.X ^ (k - q.nat_degree) = 0, { rw hpq, exact ideal.zero_mem _ }, refine ih _ _ (I.sub_mem hp (I.mul_mem_right _ hq)) rfl, rwa [polynomial.degree_eq_nat_degree hpq, with_bot.coe_lt_coe, hn] at this }, exact hs2 ⟨polynomial.mem_degree_le.2 hdq, hq⟩ } end⟩⟩ attribute [instance] polynomial.is_noetherian_ring namespace polynomial theorem exists_irreducible_of_degree_pos {R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R] {f : R[X]} (hf : 0 < f.degree) : ∃ g, irreducible g ∧ g ∣ f := wf_dvd_monoid.exists_irreducible_factor (λ huf, ne_of_gt hf $ degree_eq_zero_of_is_unit huf) (λ hf0, not_lt_of_lt hf $ hf0.symm ▸ (@degree_zero R _).symm ▸ with_bot.bot_lt_coe _) theorem exists_irreducible_of_nat_degree_pos {R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R] {f : R[X]} (hf : 0 < f.nat_degree) : ∃ g, irreducible g ∧ g ∣ f := exists_irreducible_of_degree_pos $ by { contrapose! hf, exact nat_degree_le_of_degree_le hf } theorem exists_irreducible_of_nat_degree_ne_zero {R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R] {f : R[X]} (hf : f.nat_degree ≠ 0) : ∃ g, irreducible g ∧ g ∣ f := exists_irreducible_of_nat_degree_pos $ nat.pos_of_ne_zero hf lemma linear_independent_powers_iff_aeval (f : M →ₗ[R] M) (v : M) : linear_independent R (λ n : ℕ, (f ^ n) v) ↔ ∀ (p : R[X]), aeval f p v = 0 → p = 0 := begin rw linear_independent_iff, simp only [finsupp.total_apply, aeval_endomorphism, forall_iff_forall_finsupp, sum, support, coeff, of_finsupp_eq_zero], exact iff.rfl, end lemma disjoint_ker_aeval_of_coprime (f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) : disjoint (aeval f p).ker (aeval f q).ker := begin intros v hv, rcases hpq with ⟨p', q', hpq'⟩, simpa [linear_map.mem_ker.1 (submodule.mem_inf.1 hv).1, linear_map.mem_ker.1 (submodule.mem_inf.1 hv).2] using congr_arg (λ p : R[X], aeval f p v) hpq'.symm, end lemma sup_aeval_range_eq_top_of_coprime (f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) : (aeval f p).range ⊔ (aeval f q).range = ⊤ := begin rw eq_top_iff, intros v hv, rw submodule.mem_sup, rcases hpq with ⟨p', q', hpq'⟩, use aeval f (p * p') v, use linear_map.mem_range.2 ⟨aeval f p' v, by simp only [linear_map.mul_apply, aeval_mul]⟩, use aeval f (q * q') v, use linear_map.mem_range.2 ⟨aeval f q' v, by simp only [linear_map.mul_apply, aeval_mul]⟩, simpa only [mul_comm p p', mul_comm q q', aeval_one, aeval_add] using congr_arg (λ p : R[X], aeval f p v) hpq' end lemma sup_ker_aeval_le_ker_aeval_mul {f : M →ₗ[R] M} {p q : R[X]} : (aeval f p).ker ⊔ (aeval f q).ker ≤ (aeval f (p * q)).ker := begin intros v hv, rcases submodule.mem_sup.1 hv with ⟨x, hx, y, hy, hxy⟩, have h_eval_x : aeval f (p * q) x = 0, { rw [mul_comm, aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hx, linear_map.map_zero] }, have h_eval_y : aeval f (p * q) y = 0, { rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hy, linear_map.map_zero] }, rw [linear_map.mem_ker, ←hxy, linear_map.map_add, h_eval_x, h_eval_y, add_zero], end lemma sup_ker_aeval_eq_ker_aeval_mul_of_coprime (f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) : (aeval f p).ker ⊔ (aeval f q).ker = (aeval f (p * q)).ker := begin apply le_antisymm sup_ker_aeval_le_ker_aeval_mul, intros v hv, rw submodule.mem_sup, rcases hpq with ⟨p', q', hpq'⟩, have h_eval₂_qpp' := calc aeval f (q * (p * p')) v = aeval f (p' * (p * q)) v : by rw [mul_comm, mul_assoc, mul_comm, mul_assoc, mul_comm q p] ... = 0 : by rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hv, linear_map.map_zero], have h_eval₂_pqq' := calc aeval f (p * (q * q')) v = aeval f (q' * (p * q)) v : by rw [←mul_assoc, mul_comm] ... = 0 : by rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hv, linear_map.map_zero], rw aeval_mul at h_eval₂_qpp' h_eval₂_pqq', refine ⟨aeval f (q * q') v, linear_map.mem_ker.1 h_eval₂_pqq', aeval f (p * p') v, linear_map.mem_ker.1 h_eval₂_qpp', _⟩, rw [add_comm, mul_comm p p', mul_comm q q'], simpa using congr_arg (λ p : R[X], aeval f p v) hpq' end end polynomial namespace mv_polynomial lemma is_noetherian_ring_fin_0 [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial (fin 0) R) := is_noetherian_ring_of_ring_equiv R ((mv_polynomial.is_empty_ring_equiv R pempty).symm.trans (rename_equiv R fin_zero_equiv'.symm).to_ring_equiv) theorem is_noetherian_ring_fin [is_noetherian_ring R] : ∀ {n : ℕ}, is_noetherian_ring (mv_polynomial (fin n) R) | 0 := is_noetherian_ring_fin_0 | (n+1) := @is_noetherian_ring_of_ring_equiv (polynomial (mv_polynomial (fin n) R)) _ _ _ (mv_polynomial.fin_succ_equiv _ n).to_ring_equiv.symm (@polynomial.is_noetherian_ring (mv_polynomial (fin n) R) _ (is_noetherian_ring_fin)) /-- The multivariate polynomial ring in finitely many variables over a noetherian ring is itself a noetherian ring. -/ instance is_noetherian_ring [finite σ] [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial σ R) := by casesI nonempty_fintype σ; exact @is_noetherian_ring_of_ring_equiv (mv_polynomial (fin (fintype.card σ)) R) _ _ _ (rename_equiv R (fintype.equiv_fin σ).symm).to_ring_equiv is_noetherian_ring_fin /-- Auxiliary lemma: Multivariate polynomials over an integral domain with variables indexed by `fin n` form an integral domain. This fact is proven inductively, and then used to prove the general case without any finiteness hypotheses. See `mv_polynomial.no_zero_divisors` for the general case. -/ lemma no_zero_divisors_fin (R : Type u) [comm_semiring R] [no_zero_divisors R] : ∀ (n : ℕ), no_zero_divisors (mv_polynomial (fin n) R) | 0 := (mv_polynomial.is_empty_alg_equiv R _).injective.no_zero_divisors _ (map_zero _) (map_mul _) | (n+1) := begin haveI := no_zero_divisors_fin n, exact (mv_polynomial.fin_succ_equiv R n).injective.no_zero_divisors _ (map_zero _) (map_mul _) end /-- Auxiliary definition: Multivariate polynomials in finitely many variables over an integral domain form an integral domain. This fact is proven by transport of structure from the `mv_polynomial.no_zero_divisors_fin`, and then used to prove the general case without finiteness hypotheses. See `mv_polynomial.no_zero_divisors` for the general case. -/ lemma no_zero_divisors_of_finite (R : Type u) (σ : Type v) [comm_semiring R] [finite σ] [no_zero_divisors R] : no_zero_divisors (mv_polynomial σ R) := begin casesI nonempty_fintype σ, haveI := no_zero_divisors_fin R (fintype.card σ), exact (rename_equiv R (fintype.equiv_fin σ)).injective.no_zero_divisors _ (map_zero _) (map_mul _) end instance {R : Type u} [comm_semiring R] [no_zero_divisors R] {σ : Type v} : no_zero_divisors (mv_polynomial σ R) := ⟨λ p q h, begin obtain ⟨s, p, rfl⟩ := exists_finset_rename p, obtain ⟨t, q, rfl⟩ := exists_finset_rename q, have : rename (subtype.map id (finset.subset_union_left s t) : {x // x ∈ s} → {x // x ∈ s ∪ t}) p * rename (subtype.map id (finset.subset_union_right s t) : {x // x ∈ t} → {x // x ∈ s ∪ t}) q = 0, { apply rename_injective _ subtype.val_injective, simpa using h }, letI := mv_polynomial.no_zero_divisors_of_finite R {x // x ∈ (s ∪ t)}, rw mul_eq_zero at this, cases this; [left, right], all_goals { simpa using congr_arg (rename subtype.val) this } end⟩ /-- The multivariate polynomial ring over an integral domain is an integral domain. -/ instance {R : Type u} {σ : Type v} [comm_ring R] [is_domain R] : is_domain (mv_polynomial σ R) := { .. mv_polynomial.no_zero_divisors, .. add_monoid_algebra.nontrivial } lemma map_mv_polynomial_eq_eval₂ {S : Type*} [comm_ring S] [finite σ] (ϕ : mv_polynomial σ R →+* S) (p : mv_polynomial σ R) : ϕ p = mv_polynomial.eval₂ (ϕ.comp mv_polynomial.C) (λ s, ϕ (mv_polynomial.X s)) p := begin casesI nonempty_fintype σ, refine trans (congr_arg ϕ (mv_polynomial.as_sum p)) _, rw [mv_polynomial.eval₂_eq', ϕ.map_sum], congr, ext, simp only [monomial_eq, ϕ.map_pow, ϕ.map_prod, ϕ.comp_apply, ϕ.map_mul, finsupp.prod_pow], end lemma quotient_map_C_eq_zero {I : ideal R} {i : R} (hi : i ∈ I) : (ideal.quotient.mk (ideal.map (C : R →+* mv_polynomial σ R) I : ideal (mv_polynomial σ R))).comp C i = 0 := begin simp only [function.comp_app, ring_hom.coe_comp, ideal.quotient.eq_zero_iff_mem], exact ideal.mem_map_of_mem _ hi end /-- If every coefficient of a polynomial is in an ideal `I`, then so is the polynomial itself, multivariate version. -/ lemma mem_ideal_of_coeff_mem_ideal (I : ideal (mv_polynomial σ R)) (p : mv_polynomial σ R) (hcoe : ∀ (m : σ →₀ ℕ), p.coeff m ∈ I.comap (C : R →+* mv_polynomial σ R)) : p ∈ I := begin rw as_sum p, suffices : ∀ m ∈ p.support, monomial m (mv_polynomial.coeff m p) ∈ I, { exact submodule.sum_mem I this }, intros m hm, rw [← mul_one (coeff m p), ← C_mul_monomial], suffices : C (coeff m p) ∈ I, { exact I.mul_mem_right (monomial m 1) this }, simpa [ideal.mem_comap] using hcoe m end /-- The push-forward of an ideal `I` of `R` to `mv_polynomial σ R` via inclusion is exactly the set of polynomials whose coefficients are in `I` -/ theorem mem_map_C_iff {I : ideal R} {f : mv_polynomial σ R} : f ∈ (ideal.map (C : R →+* mv_polynomial σ R) I : ideal (mv_polynomial σ R)) ↔ ∀ (m : σ →₀ ℕ), f.coeff m ∈ I := begin split, { intros hf, apply submodule.span_induction hf, { intros f hf n, cases (set.mem_image _ _ _).mp hf with x hx, rw [← hx.right, coeff_C], by_cases (n = 0), { simpa [h] using hx.left }, { simp [ne.symm h] } }, { simp }, { exact λ f g hf hg n, by simp [I.add_mem (hf n) (hg n)] }, { refine λ f g hg n, _, rw [smul_eq_mul, coeff_mul], exact I.sum_mem (λ c hc, I.mul_mem_left (f.coeff c.fst) (hg c.snd)) } }, { intros hf, rw as_sum f, suffices : ∀ m ∈ f.support, monomial m (coeff m f) ∈ (ideal.map C I : ideal (mv_polynomial σ R)), { exact submodule.sum_mem _ this }, intros m hm, rw [← mul_one (coeff m f), ← C_mul_monomial], suffices : C (coeff m f) ∈ (ideal.map C I : ideal (mv_polynomial σ R)), { exact ideal.mul_mem_right _ _ this }, apply ideal.mem_map_of_mem _, exact hf m } end lemma ker_map (f : R →+* S) : (map f : mv_polynomial σ R →+* mv_polynomial σ S).ker = f.ker.map (C : R →+* mv_polynomial σ R) := begin ext, rw [mv_polynomial.mem_map_C_iff, ring_hom.mem_ker, mv_polynomial.ext_iff], simp_rw [coeff_map, coeff_zero, ring_hom.mem_ker], end lemma eval₂_C_mk_eq_zero {I : ideal R} {a : mv_polynomial σ R} (ha : a ∈ (ideal.map (C : R →+* mv_polynomial σ R) I : ideal (mv_polynomial σ R))) : eval₂_hom (C.comp (ideal.quotient.mk I)) X a = 0 := begin rw as_sum a, rw [coe_eval₂_hom, eval₂_sum], refine finset.sum_eq_zero (λ n hn, _), simp only [eval₂_monomial, function.comp_app, ring_hom.coe_comp], refine mul_eq_zero_of_left _ _, suffices : coeff n a ∈ I, { rw [← @ideal.mk_ker R _ I, ring_hom.mem_ker] at this, simp only [this, C_0] }, exact mem_map_C_iff.1 ha n end /-- If `I` is an ideal of `R`, then the ring `mv_polynomial σ I.quotient` is isomorphic as an `R`-algebra to the quotient of `mv_polynomial σ R` by the ideal generated by `I`. -/ def quotient_equiv_quotient_mv_polynomial (I : ideal R) : mv_polynomial σ (R ⧸ I) ≃ₐ[R] mv_polynomial σ R ⧸ (ideal.map C I : ideal (mv_polynomial σ R)) := { to_fun := eval₂_hom (ideal.quotient.lift I ((ideal.quotient.mk (ideal.map C I : ideal (mv_polynomial σ R))).comp C) (λ i hi, quotient_map_C_eq_zero hi)) (λ i, ideal.quotient.mk (ideal.map C I : ideal (mv_polynomial σ R)) (X i)), inv_fun := ideal.quotient.lift (ideal.map C I : ideal (mv_polynomial σ R)) (eval₂_hom (C.comp (ideal.quotient.mk I)) X) (λ a ha, eval₂_C_mk_eq_zero ha), map_mul' := ring_hom.map_mul _, map_add' := ring_hom.map_add _, left_inv := begin intro f, apply induction_on f, { rintro ⟨r⟩, rw [coe_eval₂_hom, eval₂_C], simp only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, bind₂_C_right, ring_hom.coe_comp] }, { simp_intros p q hp hq only [ring_hom.map_add, mv_polynomial.coe_eval₂_hom, coe_eval₂_hom, mv_polynomial.eval₂_add, mv_polynomial.eval₂_hom_eq_bind₂, eval₂_hom_eq_bind₂], rw [hp, hq] }, { simp_intros p i hp only [eval₂_hom_eq_bind₂, coe_eval₂_hom], simp only [hp, eval₂_hom_eq_bind₂, coe_eval₂_hom, ideal.quotient.lift_mk, bind₂_X_right, eval₂_mul, ring_hom.map_mul, eval₂_X] } end, right_inv := begin rintro ⟨f⟩, apply induction_on f, { intros r, simp only [submodule.quotient.quot_mk_eq_mk, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, ring_hom.coe_comp, eval₂_hom_C] }, { simp_intros p q hp hq only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, eval₂_add, ring_hom.map_add, coe_eval₂_hom, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk], rw [hp, hq] }, { simp_intros p i hp only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, coe_eval₂_hom, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, bind₂_X_right, eval₂_mul, ring_hom.map_mul, eval₂_X], simp only [hp] } end, commutes' := λ r, eval₂_hom_C _ _ (ideal.quotient.mk I r) } end mv_polynomial section unique_factorization_domain variables {D : Type u} [comm_ring D] [is_domain D] [unique_factorization_monoid D] (σ) open unique_factorization_monoid namespace polynomial @[priority 100] instance unique_factorization_monoid : unique_factorization_monoid (polynomial D) := begin haveI := arbitrary (normalization_monoid D), haveI := to_normalized_gcd_monoid D, exact ufm_of_gcd_of_wf_dvd_monoid end end polynomial namespace mv_polynomial private lemma unique_factorization_monoid_of_fintype [fintype σ] : unique_factorization_monoid (mv_polynomial σ D) := (rename_equiv D (fintype.equiv_fin σ)).to_mul_equiv.symm.unique_factorization_monoid $ begin induction fintype.card σ with d hd, { apply (is_empty_alg_equiv D (fin 0)).to_mul_equiv.symm.unique_factorization_monoid, apply_instance }, { apply (fin_succ_equiv D d).to_mul_equiv.symm.unique_factorization_monoid, exactI polynomial.unique_factorization_monoid }, end @[priority 100] instance : unique_factorization_monoid (mv_polynomial σ D) := begin rw iff_exists_prime_factors, intros a ha, obtain ⟨s,a',rfl⟩ := exists_finset_rename a, obtain ⟨w,h,u,hw⟩ := iff_exists_prime_factors.1 (unique_factorization_monoid_of_fintype s) a' (λ h, ha $ by simp [h]), exact ⟨ w.map (rename coe), λ b hb, let ⟨b',hb',he⟩ := multiset.mem_map.1 hb in he ▸ (prime_rename_iff ↑s).2 (h b' hb'), units.map (@rename s σ D _ coe).to_ring_hom.to_monoid_hom u, by erw [multiset.prod_hom, ← map_mul, hw] ⟩, end end mv_polynomial end unique_factorization_domain
f9e4d592b21fe1ad7f60589e225137cf8b759591
c777c32c8e484e195053731103c5e52af26a25d1
/src/ring_theory/nakayama.lean
e1ee04629f7c5e66163675ff70c28c87ace912fd
[ "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
4,429
lean
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import ring_theory.jacobson_ideal /-! # Nakayama's lemma This file contains some alternative statements of Nakayama's Lemma as found in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV). ## Main statements * `submodule.eq_smul_of_le_smul_of_le_jacobson` - A version of (2) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV)., generalising to the Jacobson of any ideal. * `submodule.eq_bot_of_le_smul_of_le_jacobson_bot` - Statement (2) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV). * `submodule.smul_sup_eq_smul_sup_of_le_smul_of_le_jacobson` - A version of (4) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV)., generalising to the Jacobson of any ideal. * `submodule.smul_sup_eq_of_le_smul_of_le_jacobson_bot` - Statement (4) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV). Note that a version of Statement (1) in [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV) can be found in `ring_theory/noetherian` under the name `submodule.exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul` ## References * [Stacks: Nakayama's Lemma](https://stacks.math.columbia.edu/tag/00DV) ## Tags Nakayama, Jacobson -/ variables {R M : Type*} [comm_ring R] [add_comm_group M] [module R M] open ideal namespace submodule /-- *Nakayama's Lemma** - A slightly more general version of (2) in [Stacks 00DV](https://stacks.math.columbia.edu/tag/00DV). See also `eq_bot_of_le_smul_of_le_jacobson_bot` for the special case when `J = ⊥`. -/ lemma eq_smul_of_le_smul_of_le_jacobson {I J : ideal R} {N : submodule R M} (hN : N.fg) (hIN : N ≤ I • N) (hIjac : I ≤ jacobson J) : N = J • N := begin refine le_antisymm _ (submodule.smul_le.2 (λ _ _ _, submodule.smul_mem _ _)), intros n hn, cases submodule.exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul I N hN hIN with r hr, cases exists_mul_sub_mem_of_sub_one_mem_jacobson r (hIjac hr.1) with s hs, have : n = (-(s * r - 1) • n), { rw [neg_sub, sub_smul, mul_smul, hr.2 n hn, one_smul, smul_zero, sub_zero] }, rw this, exact submodule.smul_mem_smul (submodule.neg_mem _ hs) hn end /-- *Nakayama's Lemma** - Statement (2) in [Stacks 00DV](https://stacks.math.columbia.edu/tag/00DV). See also `eq_smul_of_le_smul_of_le_jacobson` for a generalisation to the `jacobson` of any ideal -/ lemma eq_bot_of_le_smul_of_le_jacobson_bot (I : ideal R) (N : submodule R M) (hN : N.fg) (hIN : N ≤ I • N) (hIjac : I ≤ jacobson ⊥) : N = ⊥ := by rw [eq_smul_of_le_smul_of_le_jacobson hN hIN hIjac, submodule.bot_smul] /-- *Nakayama's Lemma** - A slightly more general version of (4) in [Stacks 00DV](https://stacks.math.columbia.edu/tag/00DV). See also `smul_sup_eq_of_le_smul_of_le_jacobson_bot` for the special case when `J = ⊥`. -/ lemma smul_sup_eq_smul_sup_of_le_smul_of_le_jacobson {I J : ideal R} {N N' : submodule R M} (hN' : N'.fg) (hIJ : I ≤ jacobson J) (hNN : N ⊔ N' ≤ N ⊔ I • N') : N ⊔ I • N' = N ⊔ J • N' := begin have hNN' : N ⊔ N' = N ⊔ I • N', from le_antisymm hNN (sup_le_sup_left (submodule.smul_le.2 (λ _ _ _, submodule.smul_mem _ _)) _), have h_comap := submodule.comap_injective_of_surjective (linear_map.range_eq_top.1 (N.range_mkq)), have : (I • N').map N.mkq = N'.map N.mkq, { rw ←h_comap.eq_iff, simpa [comap_map_eq, sup_comm, eq_comm] using hNN' }, have := @submodule.eq_smul_of_le_smul_of_le_jacobson _ _ _ _ _ I J (N'.map N.mkq) (hN'.map _) (by rw [← map_smul'', this]; exact le_rfl) hIJ, rw [← map_smul'', ←h_comap.eq_iff, comap_map_eq, comap_map_eq, submodule.ker_mkq, sup_comm, hNN'] at this, rw [this, sup_comm] end /-- *Nakayama's Lemma** - Statement (4) in [Stacks 00DV](https://stacks.math.columbia.edu/tag/00DV). See also `smul_sup_eq_smul_sup_of_le_smul_of_le_jacobson` for a generalisation to the `jacobson` of any ideal -/ lemma smul_sup_le_of_le_smul_of_le_jacobson_bot {I : ideal R} {N N' : submodule R M} (hN' : N'.fg) (hIJ : I ≤ jacobson ⊥) (hNN : N ⊔ N' ≤ N ⊔ I • N') : I • N' ≤ N := by rw [← sup_eq_left, smul_sup_eq_smul_sup_of_le_smul_of_le_jacobson hN' hIJ hNN, bot_smul, sup_bot_eq] end submodule
c20cea9170a19977d9a7a3d258bcabf4dee8474a
9dc8cecdf3c4634764a18254e94d43da07142918
/src/combinatorics/simple_graph/regularity/bound.lean
4771c319aacf19a5b3e311bbf0ca20615c418bbd
[ "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
6,473
lean
/- Copyright (c) 2022 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 analysis.special_functions.pow import order.partition.equipartition /-! # Numerical bounds for Szemerédi Regularity Lemma This file gathers the numerical facts required by the proof of Szemerédi's regularity lemma. ## Main declarations * `szemeredi_regularity.step_bound`: During the inductive step, a partition of size `n` is blown to size at most `step_bound n`. * `szemeredi_regularity.initial_bound`: The size of the partition we start the induction with. * `szemeredi_regularity.bound`: The upper bound on the size of the partition produced by our version of Szemerédi's regularity lemma. -/ open finset fintype function real namespace szemeredi_regularity /-- Auxiliary function for Szemerédi's regularity lemma. Blowing up a partition of size `n` during the induction results in a partition of size at most `step_bound n`. -/ def step_bound (n : ℕ) : ℕ := n * 4 ^ n lemma le_step_bound : id ≤ step_bound := λ n, nat.le_mul_of_pos_right $ pow_pos (by norm_num) n lemma step_bound_mono : monotone step_bound := λ a b h, nat.mul_le_mul h $ nat.pow_le_pow_of_le_right (by norm_num) h lemma step_bound_pos_iff {n : ℕ} : 0 < step_bound n ↔ 0 < n := zero_lt_mul_right $ pow_pos (by norm_num) _ alias step_bound_pos_iff ↔ _ step_bound_pos variables {α : Type*} [decidable_eq α] [fintype α] {P : finpartition (univ : finset α)} {u : finset α} {ε : ℝ} local notation `m` := (card α/step_bound P.parts.card : ℕ) local notation `a` := (card α/P.parts.card - m * 4^P.parts.card : ℕ) lemma m_pos [nonempty α] (hPα : P.parts.card * 16^P.parts.card ≤ card α) : 0 < m := nat.div_pos ((nat.mul_le_mul_left _ $ nat.pow_le_pow_of_le_left (by norm_num) _).trans hPα) $ step_bound_pos (P.parts_nonempty $ univ_nonempty.ne_empty).card_pos lemma m_coe_pos [nonempty α] (hPα : P.parts.card * 16^P.parts.card ≤ card α) : (0 : ℝ) < m := nat.cast_pos.2 $ m_pos hPα lemma coe_m_add_one_pos : 0 < (m : ℝ) + 1 := nat.cast_add_one_pos _ lemma one_le_m_coe [nonempty α] (hPα : P.parts.card * 16^P.parts.card ≤ card α) : (1 : ℝ) ≤ m := nat.one_le_cast.2 $ m_pos hPα lemma eps_pow_five_pos (hPε : 100 ≤ 4^P.parts.card * ε^5) : 0 < ε^5 := pos_of_mul_pos_right ((by norm_num : (0 : ℝ) < 100).trans_le hPε) $ pow_nonneg (by norm_num) _ lemma eps_pos (hPε : 100 ≤ 4^P.parts.card * ε^5) : 0 < ε := pow_bit1_pos_iff.1 $ eps_pow_five_pos hPε lemma four_pow_pos {n : ℕ} : 0 < (4 : ℝ)^n := pow_pos (by norm_num) n lemma hundred_div_ε_pow_five_le_m [nonempty α] (hPα : P.parts.card * 16^P.parts.card ≤ card α) (hPε : 100 ≤ 4^P.parts.card * ε^5) : 100 / ε^5 ≤ m := (div_le_of_nonneg_of_le_mul (eps_pow_five_pos hPε).le four_pow_pos.le hPε).trans begin norm_cast, rwa [nat.le_div_iff_mul_le'(step_bound_pos (P.parts_nonempty $ univ_nonempty.ne_empty).card_pos), step_bound, mul_left_comm, ←mul_pow], end lemma hundred_le_m [nonempty α] (hPα : P.parts.card * 16^P.parts.card ≤ card α) (hPε : 100 ≤ 4^P.parts.card * ε^5) (hε : ε ≤ 1) : 100 ≤ m := by exact_mod_cast (le_div_self (by norm_num) (eps_pow_five_pos hPε) $ pow_le_one _ (eps_pos hPε).le hε).trans (hundred_div_ε_pow_five_le_m hPα hPε) lemma a_add_one_le_four_pow_parts_card : a + 1 ≤ 4^P.parts.card := begin have h : 1 ≤ 4^P.parts.card := one_le_pow_of_one_le (by norm_num) _, rw [step_bound, ←nat.div_div_eq_div_mul, ←nat.le_sub_iff_right h, tsub_le_iff_left, ←nat.add_sub_assoc h], exact nat.le_pred_of_lt (nat.lt_div_mul_add h), end lemma card_aux₁ (hucard : u.card = m * 4^P.parts.card + a) : (4^P.parts.card - a) * m + a * (m + 1) = u.card := by rw [hucard, mul_add, mul_one, ←add_assoc, ←add_mul, nat.sub_add_cancel ((nat.le_succ _).trans a_add_one_le_four_pow_parts_card), mul_comm] lemma card_aux₂ (hP : P.is_equipartition) (hu : u ∈ P.parts) (hucard : ¬u.card = m * 4^P.parts.card + a) : (4^P.parts.card - (a + 1)) * m + (a + 1) * (m + 1) = u.card := begin have : m * 4 ^ P.parts.card ≤ card α / P.parts.card, { rw [step_bound, ←nat.div_div_eq_div_mul], exact nat.div_mul_le_self _ _ }, rw nat.add_sub_of_le this at hucard, rw [(hP.card_parts_eq_average hu).resolve_left hucard, mul_add, mul_one, ←add_assoc, ←add_mul, nat.sub_add_cancel a_add_one_le_four_pow_parts_card, ←add_assoc, mul_comm, nat.add_sub_of_le this, card_univ], end lemma pow_mul_m_le_card_part (hP : P.is_equipartition) (hu : u ∈ P.parts) : (4 : ℝ) ^ P.parts.card * m ≤ u.card := begin norm_cast, rw [step_bound, ←nat.div_div_eq_div_mul], exact (nat.mul_div_le _ _).trans (hP.average_le_card_part hu), end variables (P ε) (l : ℕ) /-- Auxiliary function for Szemerédi's regularity lemma. The size of the partition by which we start blowing. -/ noncomputable def initial_bound : ℕ := max 7 $ max l $ ⌊log (100 / ε^5) / log 4⌋₊ + 1 lemma le_initial_bound : l ≤ initial_bound ε l := (le_max_left _ _).trans $ le_max_right _ _ lemma seven_le_initial_bound : 7 ≤ initial_bound ε l := le_max_left _ _ lemma initial_bound_pos : 0 < initial_bound ε l := nat.succ_pos'.trans_le $ seven_le_initial_bound _ _ lemma hundred_lt_pow_initial_bound_mul {ε : ℝ} (hε : 0 < ε) (l : ℕ) : 100 < 4^initial_bound ε l * ε^5 := begin rw [←rpow_nat_cast 4, ←div_lt_iff (pow_pos hε 5), lt_rpow_iff_log_lt _ zero_lt_four, ←div_lt_iff, initial_bound, nat.cast_max, nat.cast_max], { push_cast, exact lt_max_of_lt_right (lt_max_of_lt_right $ nat.lt_floor_add_one _) }, { exact log_pos (by norm_num) }, { exact div_pos (by norm_num) (pow_pos hε 5) } end /-- An explicit bound on the size of the equipartition whose existence is given by Szemerédi's regularity lemma. -/ noncomputable def bound : ℕ := (step_bound^[⌊4 / ε^5⌋₊] $ initial_bound ε l) * 16 ^ (step_bound^[⌊4 / ε^5⌋₊] $ initial_bound ε l) lemma initial_bound_le_bound : initial_bound ε l ≤ bound ε l := (id_le_iterate_of_id_le le_step_bound _ _).trans $ nat.le_mul_of_pos_right $ pow_pos (by norm_num) _ lemma le_bound : l ≤ bound ε l := (le_initial_bound ε l).trans $ initial_bound_le_bound ε l lemma bound_pos : 0 < bound ε l := (initial_bound_pos ε l).trans_le $ initial_bound_le_bound ε l end szemeredi_regularity
65db0a1d31935aa0fdfe0b2a5254215227c120dc
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/calculus/cont_diff.lean
2a8cced40e013911ce789b41a681cfc3a6fd2c68
[ "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
142,248
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Floris van Doorn -/ import analysis.calculus.cont_diff_def import analysis.calculus.mean_value import analysis.normed_space.finite_dimension import data.nat.choose.cast /-! # Higher differentiability of usual operations We prove that the usual operations (addition, multiplication, difference, composition, and so on) preserve `C^n` functions. We also expand the API around `C^n` functions. ## Main results * `cont_diff.comp` states that the composition of two `C^n` functions is `C^n`. * `norm_iterated_fderiv_comp_le` gives the bound `n! * C * D ^ n` for the `n`-th derivative of `g ∘ f` assuming that the derivatives of `g` are bounded by `C` and the `i`-th derivative of `f` is bounded by `D ^ i`. Similar results are given for `C^n` functions on domains. ## Notations We use the notation `E [×n]→L[𝕜] F` for the space of continuous multilinear maps on `E^n` with values in `F`. This is the space in which the `n`-th derivative of a function from `E` to `F` lives. In this file, we denote `⊤ : ℕ∞` with `∞`. ## Tags derivative, differentiability, higher derivative, `C^n`, multilinear, Taylor series, formal series -/ noncomputable theory open_locale classical big_operators nnreal nat local notation `∞` := (⊤ : ℕ∞) universes u v w uD uE uF uG local attribute [instance, priority 1001] normed_add_comm_group.to_add_comm_group normed_space.to_module' add_comm_group.to_add_comm_monoid namespace finset /- TODO porting note: move the next two lemmas to the file `data.nat.choose.sum` -/ /-- The sum of `(n+1).choose i * f i (n+1-i)` can be split into two sums at rank `n`, respectively of `n.choose i * f i (n+1-i)` and `n.choose i * f (i+1) (n-i)`. -/ lemma sum_choose_succ_mul {R : Type*} [semiring R] (f : ℕ → ℕ → R) (n : ℕ) : ∑ i in range (n+2), ((n+1).choose i : R) * f i (n + 1 - i) = ∑ i in range (n+1), (n.choose i : R) * f i (n + 1 - i) + ∑ i in range (n+1), (n.choose i : R) * f (i + 1) (n - i) := begin have A : ∑ i in range (n + 1), (n.choose (i+1) : R) * f (i + 1) (n - i) + f 0 (n + 1) = ∑ i in range (n+1), n.choose i * f i (n + 1 - i), { rw [finset.sum_range_succ, finset.sum_range_succ'], simp only [nat.choose_succ_self, algebra_map.coe_zero, zero_mul, add_zero, nat.succ_sub_succ_eq_sub, nat.choose_zero_right, algebra_map.coe_one, one_mul, tsub_zero] }, calc ∑ i in finset.range (n+2), ((n+1).choose i : R) * f i (n + 1 - i) = ∑ i in finset.range (n+1), ((n+1).choose (i+1) : R) * f (i+1) (n + 1 - (i+1)) + f 0 (n + 1 - 0) : begin rw finset.sum_range_succ', simp only [nat.choose_zero_right, algebra_map.coe_one, one_mul], end ... = ∑ i in finset.range (n+1), (n.choose i : R) * f i (n + 1 - i) + ∑ i in finset.range (n+1), n.choose i * f (i + 1) (n - i) : begin simp only [nat.choose_succ_succ, nat.cast_add, nat.succ_sub_succ_eq_sub, tsub_zero, add_mul], rw [finset.sum_add_distrib, ← A], abel, end end /-- The sum along the antidiagonal of `(n+1).choose i * f i j` can be split into two sums along the antidiagonal at rank `n`, respectively of `n.choose i * f i (j+1)` and `n.choose j * f (i+1) j`. -/ lemma sum_antidiagonal_choose_succ_mul {R : Type*} [semiring R] (f : ℕ → ℕ → R) (n : ℕ) : ∑ ij in nat.antidiagonal (n + 1), ((n + 1).choose ij.1 : R) * f ij.1 ij.2 = ∑ ij in nat.antidiagonal n, (n.choose ij.1 : R) * f ij.1 (ij.2 + 1) + ∑ ij in nat.antidiagonal n, (n.choose ij.2 : R) * f (ij.1 + 1) ij.2 := begin convert sum_choose_succ_mul f n using 1, { exact nat.sum_antidiagonal_eq_sum_range_succ (λ i j, ((n+1).choose i : R) * f i j) (n+1) }, congr' 1, { rw nat.sum_antidiagonal_eq_sum_range_succ (λ i j, (n.choose i : R) * f i (j + 1)) n, apply finset.sum_congr rfl (λ i hi, _), have : n + 1 - i = n - i + 1, from nat.sub_add_comm (nat.lt_succ_iff.1 (finset.mem_range.1 hi)), simp only [this] }, { suffices H : ∑ ij in nat.antidiagonal n, (n.choose ij.2 : R) * f (ij.1 + 1) ij.2 = ∑ ij in nat.antidiagonal n, (n.choose ij.1 : R) * f (ij.1 + 1) ij.2, by rw [H, nat.sum_antidiagonal_eq_sum_range_succ (λ i j, (n.choose i : R) * f (i + 1) j) n], apply finset.sum_congr rfl (λ i hi, _), congr' 2, apply nat.choose_symm_of_eq_add, rw [← nat.mem_antidiagonal.1 hi, add_comm] } end end finset open set fin filter function open_locale topology variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] {D : Type uD} [normed_add_comm_group D] [normed_space 𝕜 D] {E : Type uE} [normed_add_comm_group E] [normed_space 𝕜 E] {F : Type uF} [normed_add_comm_group F] [normed_space 𝕜 F] {G : Type uG} [normed_add_comm_group G] [normed_space 𝕜 G] {X : Type*} [normed_add_comm_group X] [normed_space 𝕜 X] {s s₁ t u : set E} {f f₁ : E → F} {g : F → G} {x x₀ : E} {c : F} {b : E × F → G} {m n : ℕ∞} {p : E → formal_multilinear_series 𝕜 E F} /-! ### Constants -/ @[simp] lemma iterated_fderiv_zero_fun {n : ℕ} : iterated_fderiv 𝕜 n (λ x : E, (0 : F)) = 0 := begin induction n with n IH, { ext m, simp }, { ext x m, rw [iterated_fderiv_succ_apply_left, IH], change (fderiv 𝕜 (λ (x : E), (0 : (E [×n]→L[𝕜] F))) x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) = _, rw fderiv_const, refl } end lemma cont_diff_zero_fun : cont_diff 𝕜 n (λ x : E, (0 : F)) := begin apply cont_diff_of_differentiable_iterated_fderiv (λm hm, _), rw iterated_fderiv_zero_fun, exact differentiable_const (0 : (E [×m]→L[𝕜] F)) end /-- Constants are `C^∞`. -/ lemma cont_diff_const {c : F} : cont_diff 𝕜 n (λx : E, c) := begin suffices h : cont_diff 𝕜 ∞ (λx : E, c), by exact h.of_le le_top, rw cont_diff_top_iff_fderiv, refine ⟨differentiable_const c, _⟩, rw fderiv_const, exact cont_diff_zero_fun end lemma cont_diff_on_const {c : F} {s : set E} : cont_diff_on 𝕜 n (λx : E, c) s := cont_diff_const.cont_diff_on lemma cont_diff_at_const {c : F} : cont_diff_at 𝕜 n (λx : E, c) x := cont_diff_const.cont_diff_at lemma cont_diff_within_at_const {c : F} : cont_diff_within_at 𝕜 n (λx : E, c) s x := cont_diff_at_const.cont_diff_within_at @[nontriviality] lemma cont_diff_of_subsingleton [subsingleton F] : cont_diff 𝕜 n f := by { rw [subsingleton.elim f (λ _, 0)], exact cont_diff_const } @[nontriviality] lemma cont_diff_at_of_subsingleton [subsingleton F] : cont_diff_at 𝕜 n f x := by { rw [subsingleton.elim f (λ _, 0)], exact cont_diff_at_const } @[nontriviality] lemma cont_diff_within_at_of_subsingleton [subsingleton F] : cont_diff_within_at 𝕜 n f s x := by { rw [subsingleton.elim f (λ _, 0)], exact cont_diff_within_at_const } @[nontriviality] lemma cont_diff_on_of_subsingleton [subsingleton F] : cont_diff_on 𝕜 n f s := by { rw [subsingleton.elim f (λ _, 0)], exact cont_diff_on_const } lemma iterated_fderiv_succ_const (n : ℕ) (c : F) : iterated_fderiv 𝕜 (n + 1) (λ (y : E), c) = 0 := begin ext x m, simp only [iterated_fderiv_succ_apply_right, fderiv_const, pi.zero_apply, iterated_fderiv_zero_fun, continuous_multilinear_map.zero_apply, continuous_linear_map.zero_apply], end lemma iterated_fderiv_const_of_ne {n : ℕ} (hn : n ≠ 0) (c : F) : iterated_fderiv 𝕜 n (λ (y : E), c) = 0 := begin cases nat.exists_eq_succ_of_ne_zero hn with k hk, rw [hk, iterated_fderiv_succ_const], end /-! ### Smoothness of linear functions -/ /-- Unbundled bounded linear functions are `C^∞`. -/ lemma is_bounded_linear_map.cont_diff (hf : is_bounded_linear_map 𝕜 f) : cont_diff 𝕜 n f := begin suffices h : cont_diff 𝕜 ∞ f, by exact h.of_le le_top, rw cont_diff_top_iff_fderiv, refine ⟨hf.differentiable, _⟩, simp_rw [hf.fderiv], exact cont_diff_const end lemma continuous_linear_map.cont_diff (f : E →L[𝕜] F) : cont_diff 𝕜 n f := f.is_bounded_linear_map.cont_diff lemma continuous_linear_equiv.cont_diff (f : E ≃L[𝕜] F) : cont_diff 𝕜 n f := (f : E →L[𝕜] F).cont_diff lemma linear_isometry.cont_diff (f : E →ₗᵢ[𝕜] F) : cont_diff 𝕜 n f := f.to_continuous_linear_map.cont_diff lemma linear_isometry_equiv.cont_diff (f : E ≃ₗᵢ[𝕜] F) : cont_diff 𝕜 n f := (f : E →L[𝕜] F).cont_diff /-- The identity is `C^∞`. -/ lemma cont_diff_id : cont_diff 𝕜 n (id : E → E) := is_bounded_linear_map.id.cont_diff lemma cont_diff_within_at_id {s x} : cont_diff_within_at 𝕜 n (id : E → E) s x := cont_diff_id.cont_diff_within_at lemma cont_diff_at_id {x} : cont_diff_at 𝕜 n (id : E → E) x := cont_diff_id.cont_diff_at lemma cont_diff_on_id {s} : cont_diff_on 𝕜 n (id : E → E) s := cont_diff_id.cont_diff_on /-- Bilinear functions are `C^∞`. -/ lemma is_bounded_bilinear_map.cont_diff (hb : is_bounded_bilinear_map 𝕜 b) : cont_diff 𝕜 n b := begin suffices h : cont_diff 𝕜 ∞ b, by exact h.of_le le_top, rw cont_diff_top_iff_fderiv, refine ⟨hb.differentiable, _⟩, simp [hb.fderiv], exact hb.is_bounded_linear_map_deriv.cont_diff end /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `g ∘ f` admits a Taylor series whose `k`-th term is given by `g ∘ (p k)`. -/ lemma has_ftaylor_series_up_to_on.continuous_linear_map_comp (g : F →L[𝕜] G) (hf : has_ftaylor_series_up_to_on n f p s) : has_ftaylor_series_up_to_on n (g ∘ f) (λ x k, g.comp_continuous_multilinear_map (p x k)) s := begin set L : Π m : ℕ, (E [×m]→L[𝕜] F) →L[𝕜] (E [×m]→L[𝕜] G) := λ m, continuous_linear_map.comp_continuous_multilinear_mapL 𝕜 (λ _, E) F G g, split, { exact λ x hx, congr_arg g (hf.zero_eq x hx) }, { intros m hm x hx, convert (L m).has_fderiv_at.comp_has_fderiv_within_at x (hf.fderiv_within m hm x hx) }, { intros m hm, convert (L m).continuous.comp_continuous_on (hf.cont m hm) } end /-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain at a point. -/ lemma cont_diff_within_at.continuous_linear_map_comp (g : F →L[𝕜] G) (hf : cont_diff_within_at 𝕜 n f s x) : cont_diff_within_at 𝕜 n (g ∘ f) s x := begin assume m hm, rcases hf m hm with ⟨u, hu, p, hp⟩, exact ⟨u, hu, _, hp.continuous_linear_map_comp g⟩, end /-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain at a point. -/ lemma cont_diff_at.continuous_linear_map_comp (g : F →L[𝕜] G) (hf : cont_diff_at 𝕜 n f x) : cont_diff_at 𝕜 n (g ∘ f) x := cont_diff_within_at.continuous_linear_map_comp g hf /-- Composition by continuous linear maps on the left preserves `C^n` functions on domains. -/ lemma cont_diff_on.continuous_linear_map_comp (g : F →L[𝕜] G) (hf : cont_diff_on 𝕜 n f s) : cont_diff_on 𝕜 n (g ∘ f) s := λ x hx, (hf x hx).continuous_linear_map_comp g /-- Composition by continuous linear maps on the left preserves `C^n` functions. -/ lemma cont_diff.continuous_linear_map_comp {f : E → F} (g : F →L[𝕜] G) (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λx, g (f x)) := cont_diff_on_univ.1 $ cont_diff_on.continuous_linear_map_comp _ (cont_diff_on_univ.2 hf) /-- The iterated derivative within a set of the composition with a linear map on the left is obtained by applying the linear map to the iterated derivative. -/ lemma continuous_linear_map.iterated_fderiv_within_comp_left {f : E → F} (g : F →L[𝕜] G) (hf : cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) {i : ℕ} (hi : (i : ℕ∞) ≤ n) : iterated_fderiv_within 𝕜 i (g ∘ f) s x = g.comp_continuous_multilinear_map (iterated_fderiv_within 𝕜 i f s x) := (((hf.ftaylor_series_within hs).continuous_linear_map_comp g).eq_ftaylor_series_of_unique_diff_on hi hs hx).symm /-- The iterated derivative of the composition with a linear map on the left is obtained by applying the linear map to the iterated derivative. -/ lemma continuous_linear_map.iterated_fderiv_comp_left {f : E → F} (g : F →L[𝕜] G) (hf : cont_diff 𝕜 n f) (x : E) {i : ℕ} (hi : (i : ℕ∞) ≤ n) : iterated_fderiv 𝕜 i (g ∘ f) x = g.comp_continuous_multilinear_map (iterated_fderiv 𝕜 i f x) := begin simp only [← iterated_fderiv_within_univ], exact g.iterated_fderiv_within_comp_left hf.cont_diff_on unique_diff_on_univ (mem_univ x) hi, end /-- The iterated derivative within a set of the composition with a linear equiv on the left is obtained by applying the linear equiv to the iterated derivative. This is true without differentiability assumptions. -/ lemma continuous_linear_equiv.iterated_fderiv_within_comp_left (g : F ≃L[𝕜] G) (f : E → F) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (i : ℕ) : iterated_fderiv_within 𝕜 i (g ∘ f) s x = (g : F →L[𝕜] G).comp_continuous_multilinear_map (iterated_fderiv_within 𝕜 i f s x) := begin induction i with i IH generalizing x, { ext1 m, simp only [iterated_fderiv_within_zero_apply, continuous_linear_equiv.coe_coe, continuous_linear_map.comp_continuous_multilinear_map_coe, embedding_like.apply_eq_iff_eq] }, { ext1 m, rw iterated_fderiv_within_succ_apply_left, have Z : fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i (g ∘ f) s) s x = fderiv_within 𝕜 (λ y, g.comp_continuous_multilinear_mapL (λ (j : fin i), E) (iterated_fderiv_within 𝕜 i f s y)) s x, from fderiv_within_congr' (hs x hx) (λ y hy, IH hy) hx, simp_rw Z, rw (g.comp_continuous_multilinear_mapL (λ (j : fin i), E)).comp_fderiv_within (hs x hx), simp only [continuous_linear_map.coe_comp', continuous_linear_equiv.coe_coe, comp_app, continuous_linear_equiv.comp_continuous_multilinear_mapL_apply, continuous_linear_map.comp_continuous_multilinear_map_coe, embedding_like.apply_eq_iff_eq], rw iterated_fderiv_within_succ_apply_left } end /-- Composition with a linear isometry on the left preserves the norm of the iterated derivative within a set. -/ lemma linear_isometry.norm_iterated_fderiv_within_comp_left {f : E → F} (g : F →ₗᵢ[𝕜] G) (hf : cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) {i : ℕ} (hi : (i : ℕ∞) ≤ n) : ‖iterated_fderiv_within 𝕜 i (g ∘ f) s x‖ = ‖iterated_fderiv_within 𝕜 i f s x‖ := begin have : iterated_fderiv_within 𝕜 i (g ∘ f) s x = g.to_continuous_linear_map.comp_continuous_multilinear_map (iterated_fderiv_within 𝕜 i f s x), from g.to_continuous_linear_map.iterated_fderiv_within_comp_left hf hs hx hi, rw this, apply linear_isometry.norm_comp_continuous_multilinear_map end /-- Composition with a linear isometry on the left preserves the norm of the iterated derivative. -/ lemma linear_isometry.norm_iterated_fderiv_comp_left {f : E → F} (g : F →ₗᵢ[𝕜] G) (hf : cont_diff 𝕜 n f) (x : E) {i : ℕ} (hi : (i : ℕ∞) ≤ n) : ‖iterated_fderiv 𝕜 i (g ∘ f) x‖ = ‖iterated_fderiv 𝕜 i f x‖ := begin simp only [← iterated_fderiv_within_univ], exact g.norm_iterated_fderiv_within_comp_left hf.cont_diff_on unique_diff_on_univ (mem_univ x) hi end /-- Composition with a linear isometry equiv on the left preserves the norm of the iterated derivative within a set. -/ lemma linear_isometry_equiv.norm_iterated_fderiv_within_comp_left (g : F ≃ₗᵢ[𝕜] G) (f : E → F) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (i : ℕ) : ‖iterated_fderiv_within 𝕜 i (g ∘ f) s x‖ = ‖iterated_fderiv_within 𝕜 i f s x‖ := begin have : iterated_fderiv_within 𝕜 i (g ∘ f) s x = (g : F →L[𝕜] G).comp_continuous_multilinear_map (iterated_fderiv_within 𝕜 i f s x), from g.to_continuous_linear_equiv.iterated_fderiv_within_comp_left f hs hx i, rw this, apply linear_isometry.norm_comp_continuous_multilinear_map g.to_linear_isometry, end /-- Composition with a linear isometry equiv on the left preserves the norm of the iterated derivative. -/ lemma linear_isometry_equiv.norm_iterated_fderiv_comp_left (g : F ≃ₗᵢ[𝕜] G) (f : E → F) (x : E) (i : ℕ) : ‖iterated_fderiv 𝕜 i (g ∘ f) x‖ = ‖iterated_fderiv 𝕜 i f x‖ := begin rw [← iterated_fderiv_within_univ, ← iterated_fderiv_within_univ], apply g.norm_iterated_fderiv_within_comp_left f unique_diff_on_univ (mem_univ x) i end /-- Composition by continuous linear equivs on the left respects higher differentiability at a point in a domain. -/ lemma continuous_linear_equiv.comp_cont_diff_within_at_iff (e : F ≃L[𝕜] G) : cont_diff_within_at 𝕜 n (e ∘ f) s x ↔ cont_diff_within_at 𝕜 n f s x := ⟨λ H, by simpa only [(∘), e.symm.coe_coe, e.symm_apply_apply] using H.continuous_linear_map_comp (e.symm : G →L[𝕜] F), λ H, H.continuous_linear_map_comp (e : F →L[𝕜] G)⟩ /-- Composition by continuous linear equivs on the left respects higher differentiability at a point. -/ lemma continuous_linear_equiv.comp_cont_diff_at_iff (e : F ≃L[𝕜] G) : cont_diff_at 𝕜 n (e ∘ f) x ↔ cont_diff_at 𝕜 n f x := by simp only [← cont_diff_within_at_univ, e.comp_cont_diff_within_at_iff] /-- Composition by continuous linear equivs on the left respects higher differentiability on domains. -/ lemma continuous_linear_equiv.comp_cont_diff_on_iff (e : F ≃L[𝕜] G) : cont_diff_on 𝕜 n (e ∘ f) s ↔ cont_diff_on 𝕜 n f s := by simp [cont_diff_on, e.comp_cont_diff_within_at_iff] /-- Composition by continuous linear equivs on the left respects higher differentiability. -/ lemma continuous_linear_equiv.comp_cont_diff_iff (e : F ≃L[𝕜] G) : cont_diff 𝕜 n (e ∘ f) ↔ cont_diff 𝕜 n f := by simp only [← cont_diff_on_univ, e.comp_cont_diff_on_iff] /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `f ∘ g` admits a Taylor series in `g ⁻¹' s`, whose `k`-th term is given by `p k (g v₁, ..., g vₖ)` . -/ lemma has_ftaylor_series_up_to_on.comp_continuous_linear_map (hf : has_ftaylor_series_up_to_on n f p s) (g : G →L[𝕜] E) : has_ftaylor_series_up_to_on n (f ∘ g) (λ x k, (p (g x) k).comp_continuous_linear_map (λ _, g)) (g ⁻¹' s) := begin let A : Π m : ℕ, (E [×m]→L[𝕜] F) → (G [×m]→L[𝕜] F) := λ m h, h.comp_continuous_linear_map (λ _, g), have hA : ∀ m, is_bounded_linear_map 𝕜 (A m) := λ m, is_bounded_linear_map_continuous_multilinear_map_comp_linear g, split, { assume x hx, simp only [(hf.zero_eq (g x) hx).symm, function.comp_app], change p (g x) 0 (λ (i : fin 0), g 0) = p (g x) 0 0, rw continuous_linear_map.map_zero, refl }, { assume m hm x hx, convert ((hA m).has_fderiv_at).comp_has_fderiv_within_at x ((hf.fderiv_within m hm (g x) hx).comp x (g.has_fderiv_within_at) (subset.refl _)), ext y v, change p (g x) (nat.succ m) (g ∘ (cons y v)) = p (g x) m.succ (cons (g y) (g ∘ v)), rw comp_cons }, { assume m hm, exact (hA m).continuous.comp_continuous_on ((hf.cont m hm).comp g.continuous.continuous_on (subset.refl _)) } end /-- Composition by continuous linear maps on the right preserves `C^n` functions at a point on a domain. -/ lemma cont_diff_within_at.comp_continuous_linear_map {x : G} (g : G →L[𝕜] E) (hf : cont_diff_within_at 𝕜 n f s (g x)) : cont_diff_within_at 𝕜 n (f ∘ g) (g ⁻¹' s) x := begin assume m hm, rcases hf m hm with ⟨u, hu, p, hp⟩, refine ⟨g ⁻¹' u, _, _, hp.comp_continuous_linear_map g⟩, apply continuous_within_at.preimage_mem_nhds_within', { exact g.continuous.continuous_within_at }, { apply nhds_within_mono (g x) _ hu, rw image_insert_eq, exact insert_subset_insert (image_preimage_subset g s) } end /-- Composition by continuous linear maps on the right preserves `C^n` functions on domains. -/ lemma cont_diff_on.comp_continuous_linear_map (hf : cont_diff_on 𝕜 n f s) (g : G →L[𝕜] E) : cont_diff_on 𝕜 n (f ∘ g) (g ⁻¹' s) := λ x hx, (hf (g x) hx).comp_continuous_linear_map g /-- Composition by continuous linear maps on the right preserves `C^n` functions. -/ lemma cont_diff.comp_continuous_linear_map {f : E → F} {g : G →L[𝕜] E} (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (f ∘ g) := cont_diff_on_univ.1 $ cont_diff_on.comp_continuous_linear_map (cont_diff_on_univ.2 hf) _ /-- The iterated derivative within a set of the composition with a linear map on the right is obtained by composing the iterated derivative with the linear map. -/ lemma continuous_linear_map.iterated_fderiv_within_comp_right {f : E → F} (g : G →L[𝕜] E) (hf : cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (h's : unique_diff_on 𝕜 (g⁻¹' s)) {x : G} (hx : g x ∈ s) {i : ℕ} (hi : (i : ℕ∞) ≤ n) : iterated_fderiv_within 𝕜 i (f ∘ g) (g ⁻¹' s) x = (iterated_fderiv_within 𝕜 i f s (g x)).comp_continuous_linear_map (λ _, g) := (((hf.ftaylor_series_within hs).comp_continuous_linear_map g).eq_ftaylor_series_of_unique_diff_on hi h's hx).symm /-- The iterated derivative within a set of the composition with a linear equiv on the right is obtained by composing the iterated derivative with the linear equiv. -/ lemma continuous_linear_equiv.iterated_fderiv_within_comp_right (g : G ≃L[𝕜] E) (f : E → F) (hs : unique_diff_on 𝕜 s) {x : G} (hx : g x ∈ s) (i : ℕ) : iterated_fderiv_within 𝕜 i (f ∘ g) (g ⁻¹' s) x = (iterated_fderiv_within 𝕜 i f s (g x)).comp_continuous_linear_map (λ _, g) := begin induction i with i IH generalizing x, { ext1 m, simp only [iterated_fderiv_within_zero_apply, continuous_multilinear_map.comp_continuous_linear_map_apply] }, { ext1 m, simp only [continuous_multilinear_map.comp_continuous_linear_map_apply, continuous_linear_equiv.coe_coe, iterated_fderiv_within_succ_apply_left], have : fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i (f ∘ ⇑g) (⇑g ⁻¹' s)) (⇑g ⁻¹' s) x = fderiv_within 𝕜 (λ y, continuous_multilinear_map.comp_continuous_linear_map_equivL _ (λ (_x : fin i), g) (iterated_fderiv_within 𝕜 i f s (g y))) (g ⁻¹' s) x, from fderiv_within_congr' (g.unique_diff_on_preimage_iff.2 hs x hx) (λ y hy, IH hy) hx, rw [this], rw continuous_linear_equiv.comp_fderiv_within _ (g.unique_diff_on_preimage_iff.2 hs x hx), simp only [continuous_linear_map.coe_comp', continuous_linear_equiv.coe_coe, comp_app, continuous_multilinear_map.comp_continuous_linear_map_equivL_apply, continuous_multilinear_map.comp_continuous_linear_map_apply], rw continuous_linear_equiv.comp_right_fderiv_within _ (g.unique_diff_on_preimage_iff.2 hs x hx), refl } end /-- The iterated derivative of the composition with a linear map on the right is obtained by composing the iterated derivative with the linear map. -/ lemma continuous_linear_map.iterated_fderiv_comp_right (g : G →L[𝕜] E) {f : E → F} (hf : cont_diff 𝕜 n f) (x : G) {i : ℕ} (hi : (i : ℕ∞) ≤ n) : iterated_fderiv 𝕜 i (f ∘ g) x = (iterated_fderiv 𝕜 i f (g x)).comp_continuous_linear_map (λ _, g) := begin simp only [← iterated_fderiv_within_univ], apply g.iterated_fderiv_within_comp_right hf.cont_diff_on unique_diff_on_univ unique_diff_on_univ (mem_univ _) hi, end /-- Composition with a linear isometry on the right preserves the norm of the iterated derivative within a set. -/ lemma linear_isometry_equiv.norm_iterated_fderiv_within_comp_right (g : G ≃ₗᵢ[𝕜] E) (f : E → F) (hs : unique_diff_on 𝕜 s) {x : G} (hx : g x ∈ s) (i : ℕ) : ‖iterated_fderiv_within 𝕜 i (f ∘ g) (g ⁻¹' s) x‖ = ‖iterated_fderiv_within 𝕜 i f s (g x)‖ := begin have : iterated_fderiv_within 𝕜 i (f ∘ g) (g ⁻¹' s) x = (iterated_fderiv_within 𝕜 i f s (g x)).comp_continuous_linear_map (λ _, g), from g.to_continuous_linear_equiv.iterated_fderiv_within_comp_right f hs hx i, rw [this, continuous_multilinear_map.norm_comp_continuous_linear_isometry_equiv] end /-- Composition with a linear isometry on the right preserves the norm of the iterated derivative within a set. -/ lemma linear_isometry_equiv.norm_iterated_fderiv_comp_right (g : G ≃ₗᵢ[𝕜] E) (f : E → F) (x : G) (i : ℕ) : ‖iterated_fderiv 𝕜 i (f ∘ g) x‖ = ‖iterated_fderiv 𝕜 i f (g x)‖ := begin simp only [← iterated_fderiv_within_univ], apply g.norm_iterated_fderiv_within_comp_right f unique_diff_on_univ (mem_univ (g x)) i, end /-- Composition by continuous linear equivs on the right respects higher differentiability at a point in a domain. -/ lemma continuous_linear_equiv.cont_diff_within_at_comp_iff (e : G ≃L[𝕜] E) : cont_diff_within_at 𝕜 n (f ∘ e) (e ⁻¹' s) (e.symm x) ↔ cont_diff_within_at 𝕜 n f s x := begin split, { assume H, simpa [← preimage_comp, (∘)] using H.comp_continuous_linear_map (e.symm : E →L[𝕜] G) }, { assume H, rw [← e.apply_symm_apply x, ← e.coe_coe] at H, exact H.comp_continuous_linear_map _ }, end /-- Composition by continuous linear equivs on the right respects higher differentiability at a point. -/ lemma continuous_linear_equiv.cont_diff_at_comp_iff (e : G ≃L[𝕜] E) : cont_diff_at 𝕜 n (f ∘ e) (e.symm x) ↔ cont_diff_at 𝕜 n f x := begin rw [← cont_diff_within_at_univ, ← cont_diff_within_at_univ, ← preimage_univ], exact e.cont_diff_within_at_comp_iff end /-- Composition by continuous linear equivs on the right respects higher differentiability on domains. -/ lemma continuous_linear_equiv.cont_diff_on_comp_iff (e : G ≃L[𝕜] E) : cont_diff_on 𝕜 n (f ∘ e) (e ⁻¹' s) ↔ cont_diff_on 𝕜 n f s := begin refine ⟨λ H, _, λ H, H.comp_continuous_linear_map (e : G →L[𝕜] E)⟩, have A : f = (f ∘ e) ∘ e.symm, by { ext y, simp only [function.comp_app], rw e.apply_symm_apply y }, have B : e.symm ⁻¹' (e ⁻¹' s) = s, by { rw [← preimage_comp, e.self_comp_symm], refl }, rw [A, ← B], exact H.comp_continuous_linear_map (e.symm : E →L[𝕜] G) end /-- Composition by continuous linear equivs on the right respects higher differentiability. -/ lemma continuous_linear_equiv.cont_diff_comp_iff (e : G ≃L[𝕜] E) : cont_diff 𝕜 n (f ∘ e) ↔ cont_diff 𝕜 n f := begin rw [← cont_diff_on_univ, ← cont_diff_on_univ, ← preimage_univ], exact e.cont_diff_on_comp_iff end /-- If two functions `f` and `g` admit Taylor series `p` and `q` in a set `s`, then the cartesian product of `f` and `g` admits the cartesian product of `p` and `q` as a Taylor series. -/ lemma has_ftaylor_series_up_to_on.prod (hf : has_ftaylor_series_up_to_on n f p s) {g : E → G} {q : E → formal_multilinear_series 𝕜 E G} (hg : has_ftaylor_series_up_to_on n g q s) : has_ftaylor_series_up_to_on n (λ y, (f y, g y)) (λ y k, (p y k).prod (q y k)) s := begin set L := λ m, continuous_multilinear_map.prodL 𝕜 (λ i : fin m, E) F G, split, { assume x hx, rw [← hf.zero_eq x hx, ← hg.zero_eq x hx], refl }, { assume m hm x hx, convert (L m).has_fderiv_at.comp_has_fderiv_within_at x ((hf.fderiv_within m hm x hx).prod (hg.fderiv_within m hm x hx)) }, { assume m hm, exact (L m).continuous.comp_continuous_on ((hf.cont m hm).prod (hg.cont m hm)) } end /-- The cartesian product of `C^n` functions at a point in a domain is `C^n`. -/ lemma cont_diff_within_at.prod {s : set E} {f : E → F} {g : E → G} (hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x) : cont_diff_within_at 𝕜 n (λx:E, (f x, g x)) s x := begin assume m hm, rcases hf m hm with ⟨u, hu, p, hp⟩, rcases hg m hm with ⟨v, hv, q, hq⟩, exact ⟨u ∩ v, filter.inter_mem hu hv, _, (hp.mono (inter_subset_left u v)).prod (hq.mono (inter_subset_right u v))⟩ end /-- The cartesian product of `C^n` functions on domains is `C^n`. -/ lemma cont_diff_on.prod {s : set E} {f : E → F} {g : E → G} (hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) : cont_diff_on 𝕜 n (λ x : E, (f x, g x)) s := λ x hx, (hf x hx).prod (hg x hx) /-- The cartesian product of `C^n` functions at a point is `C^n`. -/ lemma cont_diff_at.prod {f : E → F} {g : E → G} (hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x) : cont_diff_at 𝕜 n (λ x : E, (f x, g x)) x := cont_diff_within_at_univ.1 $ cont_diff_within_at.prod (cont_diff_within_at_univ.2 hf) (cont_diff_within_at_univ.2 hg) /-- The cartesian product of `C^n` functions is `C^n`.-/ lemma cont_diff.prod {f : E → F} {g : E → G} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) : cont_diff 𝕜 n (λ x : E, (f x, g x)) := cont_diff_on_univ.1 $ cont_diff_on.prod (cont_diff_on_univ.2 hf) (cont_diff_on_univ.2 hg) /-! ### Composition of `C^n` functions We show that the composition of `C^n` functions is `C^n`. One way to prove it would be to write the `n`-th derivative of the composition (this is Faà di Bruno's formula) and check its continuity, but this is very painful. Instead, we go for a simple inductive proof. Assume it is done for `n`. Then, to check it for `n+1`, one needs to check that the derivative of `g ∘ f` is `C^n`, i.e., that `Dg(f x) ⬝ Df(x)` is `C^n`. The term `Dg (f x)` is the composition of two `C^n` functions, so it is `C^n` by the inductive assumption. The term `Df(x)` is also `C^n`. Then, the matrix multiplication is the application of a bilinear map (which is `C^∞`, and therefore `C^n`) to `x ↦ (Dg(f x), Df x)`. As the composition of two `C^n` maps, it is again `C^n`, and we are done. There is a subtlety in this argument: we apply the inductive assumption to functions on other Banach spaces. In maths, one would say: prove by induction over `n` that, for all `C^n` maps between all pairs of Banach spaces, their composition is `C^n`. In Lean, this is fine as long as the spaces stay in the same universe. This is not the case in the above argument: if `E` lives in universe `u` and `F` lives in universe `v`, then linear maps from `E` to `F` (to which the derivative of `f` belongs) is in universe `max u v`. If one could quantify over finitely many universes, the above proof would work fine, but this is not the case. One could still write the proof considering spaces in any universe in `u, v, w, max u v, max v w, max u v w`, but it would be extremely tedious and lead to a lot of duplication. Instead, we formulate the above proof when all spaces live in the same universe (where everything is fine), and then we deduce the general result by lifting all our spaces to a common universe. We use the trick that any space `H` is isomorphic through a continuous linear equiv to `continuous_multilinear_map (λ (i : fin 0), E × F × G) H` to change the universe level, and then argue that composing with such a linear equiv does not change the fact of being `C^n`, which we have already proved previously. -/ /-- Auxiliary lemma proving that the composition of `C^n` functions on domains is `C^n` when all spaces live in the same universe. Use instead `cont_diff_on.comp` which removes the universe assumption (but is deduced from this one). -/ private lemma cont_diff_on.comp_same_univ {Eu : Type u} [normed_add_comm_group Eu] [normed_space 𝕜 Eu] {Fu : Type u} [normed_add_comm_group Fu] [normed_space 𝕜 Fu] {Gu : Type u} [normed_add_comm_group Gu] [normed_space 𝕜 Gu] {s : set Eu} {t : set Fu} {g : Fu → Gu} {f : Eu → Fu} (hg : cont_diff_on 𝕜 n g t) (hf : cont_diff_on 𝕜 n f s) (st : s ⊆ f ⁻¹' t) : cont_diff_on 𝕜 n (g ∘ f) s := begin unfreezingI { induction n using enat.nat_induction with n IH Itop generalizing Eu Fu Gu }, { rw cont_diff_on_zero at hf hg ⊢, exact continuous_on.comp hg hf st }, { rw cont_diff_on_succ_iff_has_fderiv_within_at at hg ⊢, assume x hx, rcases (cont_diff_on_succ_iff_has_fderiv_within_at.1 hf) x hx with ⟨u, hu, f', hf', f'_diff⟩, rcases hg (f x) (st hx) with ⟨v, hv, g', hg', g'_diff⟩, rw insert_eq_of_mem hx at hu ⊢, have xu : x ∈ u := mem_of_mem_nhds_within hx hu, let w := s ∩ (u ∩ f⁻¹' v), have wv : w ⊆ f ⁻¹' v := λ y hy, hy.2.2, have wu : w ⊆ u := λ y hy, hy.2.1, have ws : w ⊆ s := λ y hy, hy.1, refine ⟨w, _, λ y, (g' (f y)).comp (f' y), _, _⟩, show w ∈ 𝓝[s] x, { apply filter.inter_mem self_mem_nhds_within, apply filter.inter_mem hu, apply continuous_within_at.preimage_mem_nhds_within', { rw ← continuous_within_at_inter' hu, exact (hf' x xu).differentiable_within_at.continuous_within_at.mono (inter_subset_right _ _) }, { apply nhds_within_mono _ _ hv, exact subset.trans (image_subset_iff.mpr st) (subset_insert (f x) t) } }, show ∀ y ∈ w, has_fderiv_within_at (g ∘ f) ((g' (f y)).comp (f' y)) w y, { rintros y ⟨ys, yu, yv⟩, exact (hg' (f y) yv).comp y ((hf' y yu).mono wu) wv }, show cont_diff_on 𝕜 n (λ y, (g' (f y)).comp (f' y)) w, { have A : cont_diff_on 𝕜 n (λ y, g' (f y)) w := IH g'_diff ((hf.of_le (with_top.coe_le_coe.2 (nat.le_succ n))).mono ws) wv, have B : cont_diff_on 𝕜 n f' w := f'_diff.mono wu, have C : cont_diff_on 𝕜 n (λ y, (g' (f y), f' y)) w := A.prod B, have D : cont_diff_on 𝕜 n (λ p : (Fu →L[𝕜] Gu) × (Eu →L[𝕜] Fu), p.1.comp p.2) univ := is_bounded_bilinear_map_comp.cont_diff.cont_diff_on, exact IH D C (subset_univ _) } }, { rw cont_diff_on_top at hf hg ⊢, exact λ n, Itop n (hg n) (hf n) st } end /-- The composition of `C^n` functions on domains is `C^n`. -/ lemma cont_diff_on.comp {s : set E} {t : set F} {g : F → G} {f : E → F} (hg : cont_diff_on 𝕜 n g t) (hf : cont_diff_on 𝕜 n f s) (st : s ⊆ f ⁻¹' t) : cont_diff_on 𝕜 n (g ∘ f) s := begin /- we lift all the spaces to a common universe, as we have already proved the result in this situation. -/ let Eu : Type (max uE uF uG) := ulift E, let Fu : Type (max uE uF uG) := ulift.{(max uE uG) uF} F, let Gu : Type (max uE uF uG) := ulift.{(max uE uF) uG} G, -- declare the isomorphisms have isoE : Eu ≃L[𝕜] E := continuous_linear_equiv.ulift, have isoF : Fu ≃L[𝕜] F := continuous_linear_equiv.ulift, have isoG : Gu ≃L[𝕜] G := continuous_linear_equiv.ulift, -- lift the functions to the new spaces, check smoothness there, and then go back. let fu : Eu → Fu := (isoF.symm ∘ f) ∘ isoE, have fu_diff : cont_diff_on 𝕜 n fu (isoE ⁻¹' s), by rwa [isoE.cont_diff_on_comp_iff, isoF.symm.comp_cont_diff_on_iff], let gu : Fu → Gu := (isoG.symm ∘ g) ∘ isoF, have gu_diff : cont_diff_on 𝕜 n gu (isoF ⁻¹' t), by rwa [isoF.cont_diff_on_comp_iff, isoG.symm.comp_cont_diff_on_iff], have main : cont_diff_on 𝕜 n (gu ∘ fu) (isoE ⁻¹' s), { apply cont_diff_on.comp_same_univ gu_diff fu_diff, assume y hy, simp only [fu, continuous_linear_equiv.coe_apply, function.comp_app, mem_preimage], rw isoF.apply_symm_apply (f (isoE y)), exact st hy }, have : gu ∘ fu = (isoG.symm ∘ (g ∘ f)) ∘ isoE, { ext y, simp only [function.comp_apply, gu, fu], rw isoF.apply_symm_apply (f (isoE y)) }, rwa [this, isoE.cont_diff_on_comp_iff, isoG.symm.comp_cont_diff_on_iff] at main end /-- The composition of `C^n` functions on domains is `C^n`. -/ lemma cont_diff_on.comp' {s : set E} {t : set F} {g : F → G} {f : E → F} (hg : cont_diff_on 𝕜 n g t) (hf : cont_diff_on 𝕜 n f s) : cont_diff_on 𝕜 n (g ∘ f) (s ∩ f⁻¹' t) := hg.comp (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) /-- The composition of a `C^n` function on a domain with a `C^n` function is `C^n`. -/ lemma cont_diff.comp_cont_diff_on {s : set E} {g : F → G} {f : E → F} (hg : cont_diff 𝕜 n g) (hf : cont_diff_on 𝕜 n f s) : cont_diff_on 𝕜 n (g ∘ f) s := (cont_diff_on_univ.2 hg).comp hf subset_preimage_univ /-- The composition of `C^n` functions is `C^n`. -/ lemma cont_diff.comp {g : F → G} {f : E → F} (hg : cont_diff 𝕜 n g) (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (g ∘ f) := cont_diff_on_univ.1 $ cont_diff_on.comp (cont_diff_on_univ.2 hg) (cont_diff_on_univ.2 hf) (subset_univ _) /-- The composition of `C^n` functions at points in domains is `C^n`. -/ lemma cont_diff_within_at.comp {s : set E} {t : set F} {g : F → G} {f : E → F} (x : E) (hg : cont_diff_within_at 𝕜 n g t (f x)) (hf : cont_diff_within_at 𝕜 n f s x) (st : s ⊆ f ⁻¹' t) : cont_diff_within_at 𝕜 n (g ∘ f) s x := begin assume m hm, rcases hg.cont_diff_on hm with ⟨u, u_nhd, ut, hu⟩, rcases hf.cont_diff_on hm with ⟨v, v_nhd, vs, hv⟩, have xmem : x ∈ f ⁻¹' u ∩ v := ⟨(mem_of_mem_nhds_within (mem_insert (f x) _) u_nhd : _), mem_of_mem_nhds_within (mem_insert x s) v_nhd⟩, have : f ⁻¹' u ∈ 𝓝[insert x s] x, { apply hf.continuous_within_at.insert_self.preimage_mem_nhds_within', apply nhds_within_mono _ _ u_nhd, rw image_insert_eq, exact insert_subset_insert (image_subset_iff.mpr st) }, have Z := ((hu.comp (hv.mono (inter_subset_right (f ⁻¹' u) v)) (inter_subset_left _ _)) .cont_diff_within_at) xmem m le_rfl, have : 𝓝[f ⁻¹' u ∩ v] x = 𝓝[insert x s] x, { have A : f ⁻¹' u ∩ v = (insert x s) ∩ (f ⁻¹' u ∩ v), { apply subset.antisymm _ (inter_subset_right _ _), rintros y ⟨hy1, hy2⟩, simp [hy1, hy2, vs hy2] }, rw [A, ← nhds_within_restrict''], exact filter.inter_mem this v_nhd }, rwa [insert_eq_of_mem xmem, this] at Z, end /-- The composition of `C^n` functions at points in domains is `C^n`, with a weaker condition on `s` and `t`. -/ lemma cont_diff_within_at.comp_of_mem {s : set E} {t : set F} {g : F → G} {f : E → F} (x : E) (hg : cont_diff_within_at 𝕜 n g t (f x)) (hf : cont_diff_within_at 𝕜 n f s x) (hs : t ∈ 𝓝[f '' s] f x) : cont_diff_within_at 𝕜 n (g ∘ f) s x := (hg.mono_of_mem hs).comp x hf (subset_preimage_image f s) /-- The composition of `C^n` functions at points in domains is `C^n`. -/ lemma cont_diff_within_at.comp' {s : set E} {t : set F} {g : F → G} {f : E → F} (x : E) (hg : cont_diff_within_at 𝕜 n g t (f x)) (hf : cont_diff_within_at 𝕜 n f s x) : cont_diff_within_at 𝕜 n (g ∘ f) (s ∩ f⁻¹' t) x := hg.comp x (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) lemma cont_diff_at.comp_cont_diff_within_at {n} (x : E) (hg : cont_diff_at 𝕜 n g (f x)) (hf : cont_diff_within_at 𝕜 n f s x) : cont_diff_within_at 𝕜 n (g ∘ f) s x := hg.comp x hf (maps_to_univ _ _) /-- The composition of `C^n` functions at points is `C^n`. -/ lemma cont_diff_at.comp (x : E) (hg : cont_diff_at 𝕜 n g (f x)) (hf : cont_diff_at 𝕜 n f x) : cont_diff_at 𝕜 n (g ∘ f) x := hg.comp x hf subset_preimage_univ lemma cont_diff.comp_cont_diff_within_at {g : F → G} {f : E → F} (h : cont_diff 𝕜 n g) (hf : cont_diff_within_at 𝕜 n f t x) : cont_diff_within_at 𝕜 n (g ∘ f) t x := begin have : cont_diff_within_at 𝕜 n g univ (f x) := h.cont_diff_at.cont_diff_within_at, exact this.comp x hf (subset_univ _), end lemma cont_diff.comp_cont_diff_at {g : F → G} {f : E → F} (x : E) (hg : cont_diff 𝕜 n g) (hf : cont_diff_at 𝕜 n f x) : cont_diff_at 𝕜 n (g ∘ f) x := hg.comp_cont_diff_within_at hf /-! ### Smoothness of projections -/ /-- The first projection in a product is `C^∞`. -/ lemma cont_diff_fst : cont_diff 𝕜 n (prod.fst : E × F → E) := is_bounded_linear_map.cont_diff is_bounded_linear_map.fst /-- Postcomposing `f` with `prod.fst` is `C^n` -/ lemma cont_diff.fst {f : E → F × G} (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λ x, (f x).1) := cont_diff_fst.comp hf /-- Precomposing `f` with `prod.fst` is `C^n` -/ lemma cont_diff.fst' {f : E → G} (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λ x : E × F, f x.1) := hf.comp cont_diff_fst /-- The first projection on a domain in a product is `C^∞`. -/ lemma cont_diff_on_fst {s : set (E × F)} : cont_diff_on 𝕜 n (prod.fst : E × F → E) s := cont_diff.cont_diff_on cont_diff_fst lemma cont_diff_on.fst {f : E → F × G} {s : set E} (hf : cont_diff_on 𝕜 n f s) : cont_diff_on 𝕜 n (λ x, (f x).1) s := cont_diff_fst.comp_cont_diff_on hf /-- The first projection at a point in a product is `C^∞`. -/ lemma cont_diff_at_fst {p : E × F} : cont_diff_at 𝕜 n (prod.fst : E × F → E) p := cont_diff_fst.cont_diff_at /-- Postcomposing `f` with `prod.fst` is `C^n` at `(x, y)` -/ lemma cont_diff_at.fst {f : E → F × G} {x : E} (hf : cont_diff_at 𝕜 n f x) : cont_diff_at 𝕜 n (λ x, (f x).1) x := cont_diff_at_fst.comp x hf /-- Precomposing `f` with `prod.fst` is `C^n` at `(x, y)` -/ lemma cont_diff_at.fst' {f : E → G} {x : E} {y : F} (hf : cont_diff_at 𝕜 n f x) : cont_diff_at 𝕜 n (λ x : E × F, f x.1) (x, y) := cont_diff_at.comp (x, y) hf cont_diff_at_fst /-- Precomposing `f` with `prod.fst` is `C^n` at `x : E × F` -/ lemma cont_diff_at.fst'' {f : E → G} {x : E × F} (hf : cont_diff_at 𝕜 n f x.1) : cont_diff_at 𝕜 n (λ x : E × F, f x.1) x := hf.comp x cont_diff_at_fst /-- The first projection within a domain at a point in a product is `C^∞`. -/ lemma cont_diff_within_at_fst {s : set (E × F)} {p : E × F} : cont_diff_within_at 𝕜 n (prod.fst : E × F → E) s p := cont_diff_fst.cont_diff_within_at /-- The second projection in a product is `C^∞`. -/ lemma cont_diff_snd : cont_diff 𝕜 n (prod.snd : E × F → F) := is_bounded_linear_map.cont_diff is_bounded_linear_map.snd /-- Postcomposing `f` with `prod.snd` is `C^n` -/ lemma cont_diff.snd {f : E → F × G} (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λ x, (f x).2) := cont_diff_snd.comp hf /-- Precomposing `f` with `prod.snd` is `C^n` -/ lemma cont_diff.snd' {f : F → G} (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λ x : E × F, f x.2) := hf.comp cont_diff_snd /-- The second projection on a domain in a product is `C^∞`. -/ lemma cont_diff_on_snd {s : set (E × F)} : cont_diff_on 𝕜 n (prod.snd : E × F → F) s := cont_diff.cont_diff_on cont_diff_snd lemma cont_diff_on.snd {f : E → F × G} {s : set E} (hf : cont_diff_on 𝕜 n f s) : cont_diff_on 𝕜 n (λ x, (f x).2) s := cont_diff_snd.comp_cont_diff_on hf /-- The second projection at a point in a product is `C^∞`. -/ lemma cont_diff_at_snd {p : E × F} : cont_diff_at 𝕜 n (prod.snd : E × F → F) p := cont_diff_snd.cont_diff_at /-- Postcomposing `f` with `prod.snd` is `C^n` at `x` -/ lemma cont_diff_at.snd {f : E → F × G} {x : E} (hf : cont_diff_at 𝕜 n f x) : cont_diff_at 𝕜 n (λ x, (f x).2) x := cont_diff_at_snd.comp x hf /-- Precomposing `f` with `prod.snd` is `C^n` at `(x, y)` -/ lemma cont_diff_at.snd' {f : F → G} {x : E} {y : F} (hf : cont_diff_at 𝕜 n f y) : cont_diff_at 𝕜 n (λ x : E × F, f x.2) (x, y) := cont_diff_at.comp (x, y) hf cont_diff_at_snd /-- Precomposing `f` with `prod.snd` is `C^n` at `x : E × F` -/ lemma cont_diff_at.snd'' {f : F → G} {x : E × F} (hf : cont_diff_at 𝕜 n f x.2) : cont_diff_at 𝕜 n (λ x : E × F, f x.2) x := hf.comp x cont_diff_at_snd /-- The second projection within a domain at a point in a product is `C^∞`. -/ lemma cont_diff_within_at_snd {s : set (E × F)} {p : E × F} : cont_diff_within_at 𝕜 n (prod.snd : E × F → F) s p := cont_diff_snd.cont_diff_within_at section n_ary variables {E₁ E₂ E₃ E₄ : Type*} variables [normed_add_comm_group E₁] [normed_add_comm_group E₂] [normed_add_comm_group E₃] [normed_add_comm_group E₄] [normed_space 𝕜 E₁] [normed_space 𝕜 E₂] [normed_space 𝕜 E₃] [normed_space 𝕜 E₄] lemma cont_diff.comp₂ {g : E₁ × E₂ → G} {f₁ : F → E₁} {f₂ : F → E₂} (hg : cont_diff 𝕜 n g) (hf₁ : cont_diff 𝕜 n f₁) (hf₂ : cont_diff 𝕜 n f₂) : cont_diff 𝕜 n (λ x, g (f₁ x, f₂ x)) := hg.comp $ hf₁.prod hf₂ lemma cont_diff.comp₃ {g : E₁ × E₂ × E₃ → G} {f₁ : F → E₁} {f₂ : F → E₂} {f₃ : F → E₃} (hg : cont_diff 𝕜 n g) (hf₁ : cont_diff 𝕜 n f₁) (hf₂ : cont_diff 𝕜 n f₂) (hf₃ : cont_diff 𝕜 n f₃) : cont_diff 𝕜 n (λ x, g (f₁ x, f₂ x, f₃ x)) := hg.comp₂ hf₁ $ hf₂.prod hf₃ lemma cont_diff.comp_cont_diff_on₂ {g : E₁ × E₂ → G} {f₁ : F → E₁} {f₂ : F → E₂} {s : set F} (hg : cont_diff 𝕜 n g) (hf₁ : cont_diff_on 𝕜 n f₁ s) (hf₂ : cont_diff_on 𝕜 n f₂ s) : cont_diff_on 𝕜 n (λ x, g (f₁ x, f₂ x)) s := hg.comp_cont_diff_on $ hf₁.prod hf₂ lemma cont_diff.comp_cont_diff_on₃ {g : E₁ × E₂ × E₃ → G} {f₁ : F → E₁} {f₂ : F → E₂} {f₃ : F → E₃} {s : set F} (hg : cont_diff 𝕜 n g) (hf₁ : cont_diff_on 𝕜 n f₁ s) (hf₂ : cont_diff_on 𝕜 n f₂ s) (hf₃ : cont_diff_on 𝕜 n f₃ s) : cont_diff_on 𝕜 n (λ x, g (f₁ x, f₂ x, f₃ x)) s := hg.comp_cont_diff_on₂ hf₁ $ hf₂.prod hf₃ end n_ary section specific_bilinear_maps lemma cont_diff.clm_comp {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F} (hg : cont_diff 𝕜 n g) (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λ x, (g x).comp (f x)) := is_bounded_bilinear_map_comp.cont_diff.comp₂ hg hf lemma cont_diff_on.clm_comp {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F} {s : set X} (hg : cont_diff_on 𝕜 n g s) (hf : cont_diff_on 𝕜 n f s) : cont_diff_on 𝕜 n (λ x, (g x).comp (f x)) s := is_bounded_bilinear_map_comp.cont_diff.comp_cont_diff_on₂ hg hf lemma cont_diff.clm_apply {f : E → F →L[𝕜] G} {g : E → F} {n : ℕ∞} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) : cont_diff 𝕜 n (λ x, (f x) (g x)) := is_bounded_bilinear_map_apply.cont_diff.comp₂ hf hg lemma cont_diff_on.clm_apply {f : E → F →L[𝕜] G} {g : E → F} {n : ℕ∞} (hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) : cont_diff_on 𝕜 n (λ x, (f x) (g x)) s := is_bounded_bilinear_map_apply.cont_diff.comp_cont_diff_on₂ hf hg lemma cont_diff.smul_right {f : E → F →L[𝕜] 𝕜} {g : E → G} {n : ℕ∞} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) : cont_diff 𝕜 n (λ x, (f x).smul_right (g x)) := -- giving the following implicit type arguments speeds up elaboration significantly (@is_bounded_bilinear_map_smul_right 𝕜 _ F _ _ G _ _).cont_diff.comp₂ hf hg end specific_bilinear_maps /-- The natural equivalence `(E × F) × G ≃ E × (F × G)` is smooth. Warning: if you think you need this lemma, it is likely that you can simplify your proof by reformulating the lemma that you're applying next using the tips in Note [continuity lemma statement] -/ lemma cont_diff_prod_assoc : cont_diff 𝕜 ⊤ $ equiv.prod_assoc E F G := (linear_isometry_equiv.prod_assoc 𝕜 E F G).cont_diff /-- The natural equivalence `E × (F × G) ≃ (E × F) × G` is smooth. Warning: see remarks attached to `cont_diff_prod_assoc` -/ lemma cont_diff_prod_assoc_symm : cont_diff 𝕜 ⊤ $ (equiv.prod_assoc E F G).symm := (linear_isometry_equiv.prod_assoc 𝕜 E F G).symm.cont_diff /-! ### Bundled derivatives are smooth -/ /-- One direction of `cont_diff_within_at_succ_iff_has_fderiv_within_at`, but where all derivatives are taken within the same set. Version for partial derivatives / functions with parameters. If `f x` is a `C^n+1` family of functions and `g x` is a `C^n` family of points, then the derivative of `f x` at `g x` depends in a `C^n` way on `x`. We give a general version of this fact relative to sets which may not have unique derivatives, in the following form. If `f : E × F → G` is `C^n+1` at `(x₀, g(x₀))` in `(s ∪ {x₀}) × t ⊆ E × F` and `g : E → F` is `C^n` at `x₀` within some set `s ⊆ E`, then there is a function `f' : E → F →L[𝕜] G` that is `C^n` at `x₀` within `s` such that for all `x` sufficiently close to `x₀` within `s ∪ {x₀}` the function `y ↦ f x y` has derivative `f' x` at `g x` within `t ⊆ F`. For convenience, we return an explicit set of `x`'s where this holds that is a subset of `s ∪ {x₀}`. We need one additional condition, namely that `t` is a neighborhood of `g(x₀)` within `g '' s`. -/ lemma cont_diff_within_at.has_fderiv_within_at_nhds {f : E → F → G} {g : E → F} {t : set F} {n : ℕ} {x₀ : E} (hf : cont_diff_within_at 𝕜 (n+1) (uncurry f) (insert x₀ s ×ˢ t) (x₀, g x₀)) (hg : cont_diff_within_at 𝕜 n g s x₀) (hgt : t ∈ 𝓝[g '' s] g x₀) : ∃ v ∈ 𝓝[insert x₀ s] x₀, v ⊆ insert x₀ s ∧ ∃ f' : E → F →L[𝕜] G, (∀ x ∈ v, has_fderiv_within_at (f x) (f' x) t (g x)) ∧ cont_diff_within_at 𝕜 n (λ x, f' x) s x₀ := begin have hst : insert x₀ s ×ˢ t ∈ 𝓝[(λ x, (x, g x)) '' s] (x₀, g x₀), { refine nhds_within_mono _ _ (nhds_within_prod self_mem_nhds_within hgt), simp_rw [image_subset_iff, mk_preimage_prod, preimage_id', subset_inter_iff, subset_insert, true_and, subset_preimage_image] }, obtain ⟨v, hv, hvs, f', hvf', hf'⟩ := cont_diff_within_at_succ_iff_has_fderiv_within_at'.mp hf, refine ⟨(λ z, (z, g z)) ⁻¹' v ∩ insert x₀ s, _, inter_subset_right _ _, λ z, (f' (z, g z)).comp (continuous_linear_map.inr 𝕜 E F), _, _⟩, { refine inter_mem _ self_mem_nhds_within, have := mem_of_mem_nhds_within (mem_insert _ _) hv, refine mem_nhds_within_insert.mpr ⟨this, _⟩, refine (continuous_within_at_id.prod hg.continuous_within_at).preimage_mem_nhds_within' _, rw [← nhds_within_le_iff] at hst hv ⊢, refine (hst.trans $ nhds_within_mono _ $ subset_insert _ _).trans hv }, { intros z hz, have := hvf' (z, g z) hz.1, refine this.comp _ (has_fderiv_at_prod_mk_right _ _).has_fderiv_within_at _, exact maps_to'.mpr (image_prod_mk_subset_prod_right hz.2) }, { exact (hf'.continuous_linear_map_comp $ (continuous_linear_map.compL 𝕜 F (E × F) G).flip (continuous_linear_map.inr 𝕜 E F)).comp_of_mem x₀ (cont_diff_within_at_id.prod hg) hst }, end /-- The most general lemma stating that `x ↦ fderiv_within 𝕜 (f x) t (g x)` is `C^n` at a point within a set. To show that `x ↦ D_yf(x,y)g(x)` (taken within `t`) is `C^m` at `x₀` within `s`, we require that * `f` is `C^n` at `(x₀, g(x₀))` within `(s ∪ {x₀}) × t` for `n ≥ m+1`. * `g` is `C^m` at `x₀` within `s`; * Derivatives are unique at `g(x)` within `t` for `x` sufficiently close to `x₀` within `s ∪ {x₀}`; * `t` is a neighborhood of `g(x₀)` within `g '' s`; -/ lemma cont_diff_within_at.fderiv_within'' {f : E → F → G} {g : E → F} {t : set F} {n : ℕ∞} (hf : cont_diff_within_at 𝕜 n (function.uncurry f) (insert x₀ s ×ˢ t) (x₀, g x₀)) (hg : cont_diff_within_at 𝕜 m g s x₀) (ht : ∀ᶠ x in 𝓝[insert x₀ s] x₀, unique_diff_within_at 𝕜 t (g x)) (hmn : m + 1 ≤ n) (hgt : t ∈ 𝓝[g '' s] g x₀) : cont_diff_within_at 𝕜 m (λ x, fderiv_within 𝕜 (f x) t (g x)) s x₀ := begin have : ∀ k : ℕ, (k : ℕ∞) ≤ m → cont_diff_within_at 𝕜 k (λ x, fderiv_within 𝕜 (f x) t (g x)) s x₀, { intros k hkm, obtain ⟨v, hv, -, f', hvf', hf'⟩ := (hf.of_le $ (add_le_add_right hkm 1).trans hmn).has_fderiv_within_at_nhds (hg.of_le hkm) hgt, refine hf'.congr_of_eventually_eq_insert _, filter_upwards [hv, ht], exact λ y hy h2y, (hvf' y hy).fderiv_within h2y }, induction m using with_top.rec_top_coe, { obtain rfl := eq_top_iff.mpr hmn, rw [cont_diff_within_at_top], exact λ m, this m le_top }, exact this m le_rfl end /-- A special case of `cont_diff_within_at.fderiv_within''` where we require that `s ⊆ g⁻¹(t)`. -/ lemma cont_diff_within_at.fderiv_within' {f : E → F → G} {g : E → F} {t : set F} {n : ℕ∞} (hf : cont_diff_within_at 𝕜 n (function.uncurry f) (insert x₀ s ×ˢ t) (x₀, g x₀)) (hg : cont_diff_within_at 𝕜 m g s x₀) (ht : ∀ᶠ x in 𝓝[insert x₀ s] x₀, unique_diff_within_at 𝕜 t (g x)) (hmn : m + 1 ≤ n) (hst : s ⊆ g ⁻¹' t) : cont_diff_within_at 𝕜 m (λ x, fderiv_within 𝕜 (f x) t (g x)) s x₀ := hf.fderiv_within'' hg ht hmn $ mem_of_superset self_mem_nhds_within $ image_subset_iff.mpr hst /-- A special case of `cont_diff_within_at.fderiv_within'` where we require that `x₀ ∈ s` and there are unique derivatives everywhere within `t`. -/ lemma cont_diff_within_at.fderiv_within {f : E → F → G} {g : E → F} {t : set F} {n : ℕ∞} (hf : cont_diff_within_at 𝕜 n (function.uncurry f) (s ×ˢ t) (x₀, g x₀)) (hg : cont_diff_within_at 𝕜 m g s x₀) (ht : unique_diff_on 𝕜 t) (hmn : m + 1 ≤ n) (hx₀ : x₀ ∈ s) (hst : s ⊆ g ⁻¹' t) : cont_diff_within_at 𝕜 m (λ x, fderiv_within 𝕜 (f x) t (g x)) s x₀ := begin rw [← insert_eq_self.mpr hx₀] at hf, refine hf.fderiv_within' hg _ hmn hst, rw [insert_eq_self.mpr hx₀], exact eventually_of_mem self_mem_nhds_within (λ x hx, ht _ (hst hx)) end /-- `x ↦ fderiv_within 𝕜 (f x) t (g x) (k x)` is smooth at a point within a set. -/ lemma cont_diff_within_at.fderiv_within_apply {f : E → F → G} {g k : E → F} {t : set F} {n : ℕ∞} (hf : cont_diff_within_at 𝕜 n (function.uncurry f) (s ×ˢ t) (x₀, g x₀)) (hg : cont_diff_within_at 𝕜 m g s x₀) (hk : cont_diff_within_at 𝕜 m k s x₀) (ht : unique_diff_on 𝕜 t) (hmn : m + 1 ≤ n) (hx₀ : x₀ ∈ s) (hst : s ⊆ g ⁻¹' t) : cont_diff_within_at 𝕜 m (λ x, fderiv_within 𝕜 (f x) t (g x) (k x)) s x₀ := (cont_diff_fst.clm_apply cont_diff_snd).cont_diff_at.comp_cont_diff_within_at x₀ ((hf.fderiv_within hg ht hmn hx₀ hst).prod hk) /-- `fderiv_within 𝕜 f s` is smooth at `x₀` within `s`. -/ lemma cont_diff_within_at.fderiv_within_right (hf : cont_diff_within_at 𝕜 n f s x₀) (hs : unique_diff_on 𝕜 s) (hmn : (m + 1 : ℕ∞) ≤ n) (hx₀s : x₀ ∈ s) : cont_diff_within_at 𝕜 m (fderiv_within 𝕜 f s) s x₀ := cont_diff_within_at.fderiv_within (cont_diff_within_at.comp (x₀, x₀) hf cont_diff_within_at_snd $ prod_subset_preimage_snd s s) cont_diff_within_at_id hs hmn hx₀s (by rw [preimage_id']) /-- `x ↦ fderiv 𝕜 (f x) (g x)` is smooth at `x₀`. -/ lemma cont_diff_at.fderiv {f : E → F → G} {g : E → F} {n : ℕ∞} (hf : cont_diff_at 𝕜 n (function.uncurry f) (x₀, g x₀)) (hg : cont_diff_at 𝕜 m g x₀) (hmn : m + 1 ≤ n) : cont_diff_at 𝕜 m (λ x, fderiv 𝕜 (f x) (g x)) x₀ := begin simp_rw [← fderiv_within_univ], refine (cont_diff_within_at.fderiv_within hf.cont_diff_within_at hg.cont_diff_within_at unique_diff_on_univ hmn (mem_univ x₀) _).cont_diff_at univ_mem, rw [preimage_univ] end /-- `fderiv 𝕜 f` is smooth at `x₀`. -/ lemma cont_diff_at.fderiv_right (hf : cont_diff_at 𝕜 n f x₀) (hmn : (m + 1 : ℕ∞) ≤ n) : cont_diff_at 𝕜 m (fderiv 𝕜 f) x₀ := cont_diff_at.fderiv (cont_diff_at.comp (x₀, x₀) hf cont_diff_at_snd) cont_diff_at_id hmn /-- `x ↦ fderiv 𝕜 (f x) (g x)` is smooth. -/ lemma cont_diff.fderiv {f : E → F → G} {g : E → F} {n m : ℕ∞} (hf : cont_diff 𝕜 m $ function.uncurry f) (hg : cont_diff 𝕜 n g) (hnm : n + 1 ≤ m) : cont_diff 𝕜 n (λ x, fderiv 𝕜 (f x) (g x)) := cont_diff_iff_cont_diff_at.mpr $ λ x, hf.cont_diff_at.fderiv hg.cont_diff_at hnm /-- `fderiv 𝕜 f` is smooth. -/ lemma cont_diff.fderiv_right (hf : cont_diff 𝕜 n f) (hmn : (m + 1 : ℕ∞) ≤ n) : cont_diff 𝕜 m (fderiv 𝕜 f) := cont_diff_iff_cont_diff_at.mpr $ λ x, hf.cont_diff_at.fderiv_right hmn /-- `x ↦ fderiv 𝕜 (f x) (g x)` is continuous. -/ lemma continuous.fderiv {f : E → F → G} {g : E → F} {n : ℕ∞} (hf : cont_diff 𝕜 n $ function.uncurry f) (hg : continuous g) (hn : 1 ≤ n) : continuous (λ x, fderiv 𝕜 (f x) (g x)) := (hf.fderiv (cont_diff_zero.mpr hg) hn).continuous /-- `x ↦ fderiv 𝕜 (f x) (g x) (k x)` is smooth. -/ lemma cont_diff.fderiv_apply {f : E → F → G} {g k : E → F} {n m : ℕ∞} (hf : cont_diff 𝕜 m $ function.uncurry f) (hg : cont_diff 𝕜 n g) (hk : cont_diff 𝕜 n k) (hnm : n + 1 ≤ m) : cont_diff 𝕜 n (λ x, fderiv 𝕜 (f x) (g x) (k x)) := (hf.fderiv hg hnm).clm_apply hk /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ lemma cont_diff_on_fderiv_within_apply {m n : ℕ∞} {s : set E} {f : E → F} (hf : cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) : cont_diff_on 𝕜 m (λp : E × E, (fderiv_within 𝕜 f s p.1 : E →L[𝕜] F) p.2) (s ×ˢ univ) := ((hf.fderiv_within hs hmn).comp cont_diff_on_fst (prod_subset_preimage_fst _ _)).clm_apply cont_diff_on_snd /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ lemma cont_diff_on.continuous_on_fderiv_within_apply (hf : cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) : continuous_on (λp : E × E, (fderiv_within 𝕜 f s p.1 : E → F) p.2) (s ×ˢ univ) := (cont_diff_on_fderiv_within_apply hf hs $ by rwa [zero_add]).continuous_on /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ lemma cont_diff.cont_diff_fderiv_apply {f : E → F} (hf : cont_diff 𝕜 n f) (hmn : m + 1 ≤ n) : cont_diff 𝕜 m (λp : E × E, (fderiv 𝕜 f p.1 : E →L[𝕜] F) p.2) := begin rw ← cont_diff_on_univ at ⊢ hf, rw [← fderiv_within_univ, ← univ_prod_univ], exact cont_diff_on_fderiv_within_apply hf unique_diff_on_univ hmn end /-! ### Smoothness of functions `f : E → Π i, F' i` -/ section pi variables {ι ι' : Type*} [fintype ι] [fintype ι'] {F' : ι → Type*} [Π i, normed_add_comm_group (F' i)] [Π i, normed_space 𝕜 (F' i)] {φ : Π i, E → F' i} {p' : Π i, E → formal_multilinear_series 𝕜 E (F' i)} {Φ : E → Π i, F' i} {P' : E → formal_multilinear_series 𝕜 E (Π i, F' i)} lemma has_ftaylor_series_up_to_on_pi : has_ftaylor_series_up_to_on n (λ x i, φ i x) (λ x m, continuous_multilinear_map.pi (λ i, p' i x m)) s ↔ ∀ i, has_ftaylor_series_up_to_on n (φ i) (p' i) s := begin set pr := @continuous_linear_map.proj 𝕜 _ ι F' _ _ _, letI : Π (m : ℕ) (i : ι), normed_space 𝕜 (E [×m]→L[𝕜] (F' i)) := λ m i, infer_instance, set L : Π m : ℕ, (Π i, E [×m]→L[𝕜] (F' i)) ≃ₗᵢ[𝕜] (E [×m]→L[𝕜] (Π i, F' i)) := λ m, continuous_multilinear_map.piₗᵢ _ _, refine ⟨λ h i, _, λ h, ⟨λ x hx, _, _, _⟩⟩, { convert h.continuous_linear_map_comp (pr i), ext, refl }, { ext1 i, exact (h i).zero_eq x hx }, { intros m hm x hx, have := has_fderiv_within_at_pi.2 (λ i, (h i).fderiv_within m hm x hx), convert (L m).has_fderiv_at.comp_has_fderiv_within_at x this }, { intros m hm, have := continuous_on_pi.2 (λ i, (h i).cont m hm), convert (L m).continuous.comp_continuous_on this } end @[simp] lemma has_ftaylor_series_up_to_on_pi' : has_ftaylor_series_up_to_on n Φ P' s ↔ ∀ i, has_ftaylor_series_up_to_on n (λ x, Φ x i) (λ x m, (@continuous_linear_map.proj 𝕜 _ ι F' _ _ _ i).comp_continuous_multilinear_map (P' x m)) s := by { convert has_ftaylor_series_up_to_on_pi, ext, refl } lemma cont_diff_within_at_pi : cont_diff_within_at 𝕜 n Φ s x ↔ ∀ i, cont_diff_within_at 𝕜 n (λ x, Φ x i) s x := begin set pr := @continuous_linear_map.proj 𝕜 _ ι F' _ _ _, refine ⟨λ h i, h.continuous_linear_map_comp (pr i), λ h m hm, _⟩, choose u hux p hp using λ i, h i m hm, exact ⟨⋂ i, u i, filter.Inter_mem.2 hux, _, has_ftaylor_series_up_to_on_pi.2 (λ i, (hp i).mono $ Inter_subset _ _)⟩, end lemma cont_diff_on_pi : cont_diff_on 𝕜 n Φ s ↔ ∀ i, cont_diff_on 𝕜 n (λ x, Φ x i) s := ⟨λ h i x hx, cont_diff_within_at_pi.1 (h x hx) _, λ h x hx, cont_diff_within_at_pi.2 (λ i, h i x hx)⟩ lemma cont_diff_at_pi : cont_diff_at 𝕜 n Φ x ↔ ∀ i, cont_diff_at 𝕜 n (λ x, Φ x i) x := cont_diff_within_at_pi lemma cont_diff_pi : cont_diff 𝕜 n Φ ↔ ∀ i, cont_diff 𝕜 n (λ x, Φ x i) := by simp only [← cont_diff_on_univ, cont_diff_on_pi] variables (𝕜 E) lemma cont_diff_apply (i : ι) : cont_diff 𝕜 n (λ (f : ι → E), f i) := cont_diff_pi.mp cont_diff_id i lemma cont_diff_apply_apply (i : ι) (j : ι') : cont_diff 𝕜 n (λ (f : ι → ι' → E), f i j) := cont_diff_pi.mp (cont_diff_apply 𝕜 (ι' → E) i) j variables {𝕜 E} end pi /-! ### Sum of two functions -/ section add /- The sum is smooth. -/ lemma cont_diff_add : cont_diff 𝕜 n (λp : F × F, p.1 + p.2) := (is_bounded_linear_map.fst.add is_bounded_linear_map.snd).cont_diff /-- The sum of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ lemma cont_diff_within_at.add {s : set E} {f g : E → F} (hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x) : cont_diff_within_at 𝕜 n (λx, f x + g x) s x := cont_diff_add.cont_diff_within_at.comp x (hf.prod hg) subset_preimage_univ /-- The sum of two `C^n` functions at a point is `C^n` at this point. -/ lemma cont_diff_at.add {f g : E → F} (hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x) : cont_diff_at 𝕜 n (λx, f x + g x) x := by rw [← cont_diff_within_at_univ] at *; exact hf.add hg /-- The sum of two `C^n`functions is `C^n`. -/ lemma cont_diff.add {f g : E → F} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) : cont_diff 𝕜 n (λx, f x + g x) := cont_diff_add.comp (hf.prod hg) /-- The sum of two `C^n` functions on a domain is `C^n`. -/ lemma cont_diff_on.add {s : set E} {f g : E → F} (hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) : cont_diff_on 𝕜 n (λx, f x + g x) s := λ x hx, (hf x hx).add (hg x hx) variables {i : ℕ} /-- The iterated derivative of the sum of two functions is the sum of the iterated derivatives. See also `iterated_fderiv_within_add_apply'`, which uses the spelling `(λ x, f x + g x)` instead of `f + g`. -/ lemma iterated_fderiv_within_add_apply {f g : E → F} (hf : cont_diff_on 𝕜 i f s) (hg : cont_diff_on 𝕜 i g s) (hu : unique_diff_on 𝕜 s) (hx : x ∈ s) : iterated_fderiv_within 𝕜 i (f + g) s x = iterated_fderiv_within 𝕜 i f s x + iterated_fderiv_within 𝕜 i g s x := begin induction i with i hi generalizing x, { ext h, simp }, { ext h, have hi' : (i : ℕ∞) < i+1 := with_top.coe_lt_coe.mpr (nat.lt_succ_self _), have hdf : differentiable_on 𝕜 (iterated_fderiv_within 𝕜 i f s) s := hf.differentiable_on_iterated_fderiv_within hi' hu, have hdg : differentiable_on 𝕜 (iterated_fderiv_within 𝕜 i g s) s := hg.differentiable_on_iterated_fderiv_within hi' hu, have hcdf : cont_diff_on 𝕜 i f s := hf.of_le hi'.le, have hcdg : cont_diff_on 𝕜 i g s := hg.of_le hi'.le, calc iterated_fderiv_within 𝕜 (i+1) (f + g) s x h = fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i (f + g) s) s x (h 0) (fin.tail h) : rfl ... = fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i f s + iterated_fderiv_within 𝕜 i g s) s x (h 0) (fin.tail h) : begin congr' 2, exact fderiv_within_congr (hu x hx) (λ _, hi hcdf hcdg) (hi hcdf hcdg hx), end ... = (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i f s) s + fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i g s) s) x (h 0) (fin.tail h) : by rw [pi.add_def, fderiv_within_add (hu x hx) (hdf x hx) (hdg x hx)]; refl ... = (iterated_fderiv_within 𝕜 (i+1) f s + iterated_fderiv_within 𝕜 (i+1) g s) x h : rfl } end /-- The iterated derivative of the sum of two functions is the sum of the iterated derivatives. This is the same as `iterated_fderiv_within_add_apply`, but using the spelling `(λ x, f x + g x)` instead of `f + g`, which can be handy for some rewrites. TODO: use one form consistently. -/ lemma iterated_fderiv_within_add_apply' {f g : E → F} (hf : cont_diff_on 𝕜 i f s) (hg : cont_diff_on 𝕜 i g s) (hu : unique_diff_on 𝕜 s) (hx : x ∈ s) : iterated_fderiv_within 𝕜 i (λ x, f x + g x) s x = iterated_fderiv_within 𝕜 i f s x + iterated_fderiv_within 𝕜 i g s x := iterated_fderiv_within_add_apply hf hg hu hx lemma iterated_fderiv_add_apply {i : ℕ} {f g : E → F} (hf : cont_diff 𝕜 i f) (hg : cont_diff 𝕜 i g) : iterated_fderiv 𝕜 i (f + g) x = iterated_fderiv 𝕜 i f x + iterated_fderiv 𝕜 i g x := begin simp_rw [←cont_diff_on_univ, ←iterated_fderiv_within_univ] at hf hg ⊢, exact iterated_fderiv_within_add_apply hf hg unique_diff_on_univ (set.mem_univ _), end lemma iterated_fderiv_add_apply' {i : ℕ} {f g : E → F} (hf : cont_diff 𝕜 i f) (hg : cont_diff 𝕜 i g) : iterated_fderiv 𝕜 i (λ x, f x + g x) x = iterated_fderiv 𝕜 i f x + iterated_fderiv 𝕜 i g x := iterated_fderiv_add_apply hf hg end add /-! ### Negative -/ section neg /- The negative is smooth. -/ lemma cont_diff_neg : cont_diff 𝕜 n (λp : F, -p) := is_bounded_linear_map.id.neg.cont_diff /-- The negative of a `C^n` function within a domain at a point is `C^n` within this domain at this point. -/ lemma cont_diff_within_at.neg {s : set E} {f : E → F} (hf : cont_diff_within_at 𝕜 n f s x) : cont_diff_within_at 𝕜 n (λx, -f x) s x := cont_diff_neg.cont_diff_within_at.comp x hf subset_preimage_univ /-- The negative of a `C^n` function at a point is `C^n` at this point. -/ lemma cont_diff_at.neg {f : E → F} (hf : cont_diff_at 𝕜 n f x) : cont_diff_at 𝕜 n (λx, -f x) x := by rw ← cont_diff_within_at_univ at *; exact hf.neg /-- The negative of a `C^n`function is `C^n`. -/ lemma cont_diff.neg {f : E → F} (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λx, -f x) := cont_diff_neg.comp hf /-- The negative of a `C^n` function on a domain is `C^n`. -/ lemma cont_diff_on.neg {s : set E} {f : E → F} (hf : cont_diff_on 𝕜 n f s) : cont_diff_on 𝕜 n (λx, -f x) s := λ x hx, (hf x hx).neg variables {i : ℕ} lemma iterated_fderiv_within_neg_apply {f : E → F} (hu : unique_diff_on 𝕜 s) (hx : x ∈ s) : iterated_fderiv_within 𝕜 i (-f) s x = -iterated_fderiv_within 𝕜 i f s x := begin induction i with i hi generalizing x, { ext h, simp }, { ext h, have hi' : (i : ℕ∞) < i+1 := with_top.coe_lt_coe.mpr (nat.lt_succ_self _), calc iterated_fderiv_within 𝕜 (i+1) (-f) s x h = fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i (-f) s) s x (h 0) (fin.tail h) : rfl ... = fderiv_within 𝕜 (-iterated_fderiv_within 𝕜 i f s) s x (h 0) (fin.tail h) : begin congr' 2, exact fderiv_within_congr (hu x hx) (λ _, hi) (hi hx), end ... = -(fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i f s) s) x (h 0) (fin.tail h) : by rw [pi.neg_def, fderiv_within_neg (hu x hx)]; refl ... = - (iterated_fderiv_within 𝕜 (i+1) f s) x h : rfl } end lemma iterated_fderiv_neg_apply {i : ℕ} {f : E → F} : iterated_fderiv 𝕜 i (-f) x = -iterated_fderiv 𝕜 i f x := begin simp_rw [←iterated_fderiv_within_univ], exact iterated_fderiv_within_neg_apply unique_diff_on_univ (set.mem_univ _), end end neg /-! ### Subtraction -/ /-- The difference of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ lemma cont_diff_within_at.sub {s : set E} {f g : E → F} (hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x) : cont_diff_within_at 𝕜 n (λx, f x - g x) s x := by simpa only [sub_eq_add_neg] using hf.add hg.neg /-- The difference of two `C^n` functions at a point is `C^n` at this point. -/ lemma cont_diff_at.sub {f g : E → F} (hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x) : cont_diff_at 𝕜 n (λx, f x - g x) x := by simpa only [sub_eq_add_neg] using hf.add hg.neg /-- The difference of two `C^n` functions on a domain is `C^n`. -/ lemma cont_diff_on.sub {s : set E} {f g : E → F} (hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) : cont_diff_on 𝕜 n (λx, f x - g x) s := by simpa only [sub_eq_add_neg] using hf.add hg.neg /-- The difference of two `C^n` functions is `C^n`. -/ lemma cont_diff.sub {f g : E → F} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) : cont_diff 𝕜 n (λx, f x - g x) := by simpa only [sub_eq_add_neg] using hf.add hg.neg /-! ### Sum of finitely many functions -/ lemma cont_diff_within_at.sum {ι : Type*} {f : ι → E → F} {s : finset ι} {t : set E} {x : E} (h : ∀ i ∈ s, cont_diff_within_at 𝕜 n (λ x, f i x) t x) : cont_diff_within_at 𝕜 n (λ x, (∑ i in s, f i x)) t x := begin classical, induction s using finset.induction_on with i s is IH, { simp [cont_diff_within_at_const] }, { simp only [is, finset.sum_insert, not_false_iff], exact (h _ (finset.mem_insert_self i s)).add (IH (λ j hj, h _ (finset.mem_insert_of_mem hj))) } end lemma cont_diff_at.sum {ι : Type*} {f : ι → E → F} {s : finset ι} {x : E} (h : ∀ i ∈ s, cont_diff_at 𝕜 n (λ x, f i x) x) : cont_diff_at 𝕜 n (λ x, (∑ i in s, f i x)) x := by rw [← cont_diff_within_at_univ] at *; exact cont_diff_within_at.sum h lemma cont_diff_on.sum {ι : Type*} {f : ι → E → F} {s : finset ι} {t : set E} (h : ∀ i ∈ s, cont_diff_on 𝕜 n (λ x, f i x) t) : cont_diff_on 𝕜 n (λ x, (∑ i in s, f i x)) t := λ x hx, cont_diff_within_at.sum (λ i hi, h i hi x hx) lemma cont_diff.sum {ι : Type*} {f : ι → E → F} {s : finset ι} (h : ∀ i ∈ s, cont_diff 𝕜 n (λ x, f i x)) : cont_diff 𝕜 n (λ x, (∑ i in s, f i x)) := by simp only [← cont_diff_on_univ] at *; exact cont_diff_on.sum h /-! ### Product of two functions -/ section mul_prod variables {𝔸 𝔸' ι 𝕜' : Type*} [normed_ring 𝔸] [normed_algebra 𝕜 𝔸] [normed_comm_ring 𝔸'] [normed_algebra 𝕜 𝔸'] [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] /- The product is smooth. -/ lemma cont_diff_mul : cont_diff 𝕜 n (λ p : 𝔸 × 𝔸, p.1 * p.2) := (continuous_linear_map.mul 𝕜 𝔸).is_bounded_bilinear_map.cont_diff /-- The product of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ lemma cont_diff_within_at.mul {s : set E} {f g : E → 𝔸} (hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x) : cont_diff_within_at 𝕜 n (λ x, f x * g x) s x := cont_diff_mul.comp_cont_diff_within_at (hf.prod hg) /-- The product of two `C^n` functions at a point is `C^n` at this point. -/ lemma cont_diff_at.mul {f g : E → 𝔸} (hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x) : cont_diff_at 𝕜 n (λ x, f x * g x) x := hf.mul hg /-- The product of two `C^n` functions on a domain is `C^n`. -/ lemma cont_diff_on.mul {f g : E → 𝔸} (hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) : cont_diff_on 𝕜 n (λ x, f x * g x) s := λ x hx, (hf x hx).mul (hg x hx) /-- The product of two `C^n`functions is `C^n`. -/ lemma cont_diff.mul {f g : E → 𝔸} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) : cont_diff 𝕜 n (λ x, f x * g x) := cont_diff_mul.comp (hf.prod hg) lemma cont_diff_within_at_prod' {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff_within_at 𝕜 n (f i) s x) : cont_diff_within_at 𝕜 n (∏ i in t, f i) s x := finset.prod_induction f (λ f, cont_diff_within_at 𝕜 n f s x) (λ _ _, cont_diff_within_at.mul) (@cont_diff_within_at_const _ _ _ _ _ _ _ _ _ _ _ 1) h lemma cont_diff_within_at_prod {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff_within_at 𝕜 n (f i) s x) : cont_diff_within_at 𝕜 n (λ y, ∏ i in t, f i y) s x := by simpa only [← finset.prod_apply] using cont_diff_within_at_prod' h lemma cont_diff_at_prod' {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff_at 𝕜 n (f i) x) : cont_diff_at 𝕜 n (∏ i in t, f i) x := cont_diff_within_at_prod' h lemma cont_diff_at_prod {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff_at 𝕜 n (f i) x) : cont_diff_at 𝕜 n (λ y, ∏ i in t, f i y) x := cont_diff_within_at_prod h lemma cont_diff_on_prod' {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff_on 𝕜 n (f i) s) : cont_diff_on 𝕜 n (∏ i in t, f i) s := λ x hx, cont_diff_within_at_prod' (λ i hi, h i hi x hx) lemma cont_diff_on_prod {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff_on 𝕜 n (f i) s) : cont_diff_on 𝕜 n (λ y, ∏ i in t, f i y) s := λ x hx, cont_diff_within_at_prod (λ i hi, h i hi x hx) lemma cont_diff_prod' {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff 𝕜 n (f i)) : cont_diff 𝕜 n (∏ i in t, f i) := cont_diff_iff_cont_diff_at.mpr $ λ x, cont_diff_at_prod' $ λ i hi, (h i hi).cont_diff_at lemma cont_diff_prod {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff 𝕜 n (f i)) : cont_diff 𝕜 n (λ y, ∏ i in t, f i y) := cont_diff_iff_cont_diff_at.mpr $ λ x, cont_diff_at_prod $ λ i hi, (h i hi).cont_diff_at lemma cont_diff.pow {f : E → 𝔸} (hf : cont_diff 𝕜 n f) : ∀ m : ℕ, cont_diff 𝕜 n (λ x, (f x) ^ m) | 0 := by simpa using cont_diff_const | (m + 1) := by simpa [pow_succ] using hf.mul (cont_diff.pow m) lemma cont_diff_within_at.pow {f : E → 𝔸} (hf : cont_diff_within_at 𝕜 n f s x) (m : ℕ) : cont_diff_within_at 𝕜 n (λ y, f y ^ m) s x := (cont_diff_id.pow m).comp_cont_diff_within_at hf lemma cont_diff_at.pow {f : E → 𝔸} (hf : cont_diff_at 𝕜 n f x) (m : ℕ) : cont_diff_at 𝕜 n (λ y, f y ^ m) x := hf.pow m lemma cont_diff_on.pow {f : E → 𝔸} (hf : cont_diff_on 𝕜 n f s) (m : ℕ) : cont_diff_on 𝕜 n (λ y, f y ^ m) s := λ y hy, (hf y hy).pow m lemma cont_diff_within_at.div_const {f : E → 𝕜'} {n} (hf : cont_diff_within_at 𝕜 n f s x) (c : 𝕜') : cont_diff_within_at 𝕜 n (λ x, f x / c) s x := by simpa only [div_eq_mul_inv] using hf.mul cont_diff_within_at_const lemma cont_diff_at.div_const {f : E → 𝕜'} {n} (hf : cont_diff_at 𝕜 n f x) (c : 𝕜') : cont_diff_at 𝕜 n (λ x, f x / c) x := hf.div_const c lemma cont_diff_on.div_const {f : E → 𝕜'} {n} (hf : cont_diff_on 𝕜 n f s) (c : 𝕜') : cont_diff_on 𝕜 n (λ x, f x / c) s := λ x hx, (hf x hx).div_const c lemma cont_diff.div_const {f : E → 𝕜'} {n} (hf : cont_diff 𝕜 n f) (c : 𝕜') : cont_diff 𝕜 n (λ x, f x / c) := by simpa only [div_eq_mul_inv] using hf.mul cont_diff_const end mul_prod /-! ### Scalar multiplication -/ section smul /- The scalar multiplication is smooth. -/ lemma cont_diff_smul : cont_diff 𝕜 n (λ p : 𝕜 × F, p.1 • p.2) := is_bounded_bilinear_map_smul.cont_diff /-- The scalar multiplication of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ lemma cont_diff_within_at.smul {s : set E} {f : E → 𝕜} {g : E → F} (hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x) : cont_diff_within_at 𝕜 n (λ x, f x • g x) s x := cont_diff_smul.cont_diff_within_at.comp x (hf.prod hg) subset_preimage_univ /-- The scalar multiplication of two `C^n` functions at a point is `C^n` at this point. -/ lemma cont_diff_at.smul {f : E → 𝕜} {g : E → F} (hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x) : cont_diff_at 𝕜 n (λ x, f x • g x) x := by rw [← cont_diff_within_at_univ] at *; exact hf.smul hg /-- The scalar multiplication of two `C^n` functions is `C^n`. -/ lemma cont_diff.smul {f : E → 𝕜} {g : E → F} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) : cont_diff 𝕜 n (λ x, f x • g x) := cont_diff_smul.comp (hf.prod hg) /-- The scalar multiplication of two `C^n` functions on a domain is `C^n`. -/ lemma cont_diff_on.smul {s : set E} {f : E → 𝕜} {g : E → F} (hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) : cont_diff_on 𝕜 n (λ x, f x • g x) s := λ x hx, (hf x hx).smul (hg x hx) end smul /-! ### Constant scalar multiplication -/ section const_smul variables {R : Type*} [semiring R] [module R F] [smul_comm_class 𝕜 R F] variables [has_continuous_const_smul R F] /- The scalar multiplication with a constant is smooth. -/ lemma cont_diff_const_smul (c : R) : cont_diff 𝕜 n (λ p : F, c • p) := (c • continuous_linear_map.id 𝕜 F).cont_diff /-- The scalar multiplication of a constant and a `C^n` function within a set at a point is `C^n` within this set at this point. -/ lemma cont_diff_within_at.const_smul {s : set E} {f : E → F} {x : E} (c : R) (hf : cont_diff_within_at 𝕜 n f s x) : cont_diff_within_at 𝕜 n (λ y, c • f y) s x := (cont_diff_const_smul c).cont_diff_at.comp_cont_diff_within_at x hf /-- The scalar multiplication of a constant and a `C^n` function at a point is `C^n` at this point. -/ lemma cont_diff_at.const_smul {f : E → F} {x : E} (c : R) (hf : cont_diff_at 𝕜 n f x) : cont_diff_at 𝕜 n (λ y, c • f y) x := by rw [←cont_diff_within_at_univ] at *; exact hf.const_smul c /-- The scalar multiplication of a constant and a `C^n` function is `C^n`. -/ lemma cont_diff.const_smul {f : E → F} (c : R) (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λ y, c • f y) := (cont_diff_const_smul c).comp hf /-- The scalar multiplication of a constant and a `C^n` on a domain is `C^n`. -/ lemma cont_diff_on.const_smul {s : set E} {f : E → F} (c : R) (hf : cont_diff_on 𝕜 n f s) : cont_diff_on 𝕜 n (λ y, c • f y) s := λ x hx, (hf x hx).const_smul c variables {i : ℕ} {a : R} lemma iterated_fderiv_within_const_smul_apply (hf : cont_diff_on 𝕜 i f s) (hu : unique_diff_on 𝕜 s) (hx : x ∈ s) : iterated_fderiv_within 𝕜 i (a • f) s x = a • (iterated_fderiv_within 𝕜 i f s x) := begin induction i with i hi generalizing x, { ext, simp }, { ext h, have hi' : (i : ℕ∞) < i+1 := with_top.coe_lt_coe.mpr (nat.lt_succ_self _), have hdf : differentiable_on 𝕜 (iterated_fderiv_within 𝕜 i f s) s := hf.differentiable_on_iterated_fderiv_within hi' hu, have hcdf : cont_diff_on 𝕜 i f s := hf.of_le hi'.le, calc iterated_fderiv_within 𝕜 (i+1) (a • f) s x h = fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i (a • f) s) s x (h 0) (fin.tail h) : rfl ... = fderiv_within 𝕜 (a • iterated_fderiv_within 𝕜 i f s) s x (h 0) (fin.tail h) : begin congr' 2, exact fderiv_within_congr (hu x hx) (λ _, hi hcdf) (hi hcdf hx), end ... = (a • fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i f s)) s x (h 0) (fin.tail h) : by rw [pi.smul_def, fderiv_within_const_smul (hu x hx) (hdf x hx)]; refl ... = a • iterated_fderiv_within 𝕜 (i+1) f s x h : rfl } end lemma iterated_fderiv_const_smul_apply {x : E} (hf : cont_diff 𝕜 i f) : iterated_fderiv 𝕜 i (a • f) x = a • iterated_fderiv 𝕜 i f x := begin simp_rw [←cont_diff_on_univ, ←iterated_fderiv_within_univ] at *, refine iterated_fderiv_within_const_smul_apply hf unique_diff_on_univ (set.mem_univ _), end end const_smul /-! ### Cartesian product of two functions -/ section prod_map variables {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E'] variables {F' : Type*} [normed_add_comm_group F'] [normed_space 𝕜 F'] /-- The product map of two `C^n` functions within a set at a point is `C^n` within the product set at the product point. -/ lemma cont_diff_within_at.prod_map' {s : set E} {t : set E'} {f : E → F} {g : E' → F'} {p : E × E'} (hf : cont_diff_within_at 𝕜 n f s p.1) (hg : cont_diff_within_at 𝕜 n g t p.2) : cont_diff_within_at 𝕜 n (prod.map f g) (s ×ˢ t) p := (hf.comp p cont_diff_within_at_fst (prod_subset_preimage_fst _ _)).prod (hg.comp p cont_diff_within_at_snd (prod_subset_preimage_snd _ _)) lemma cont_diff_within_at.prod_map {s : set E} {t : set E'} {f : E → F} {g : E' → F'} {x : E} {y : E'} (hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g t y) : cont_diff_within_at 𝕜 n (prod.map f g) (s ×ˢ t) (x, y) := cont_diff_within_at.prod_map' hf hg /-- The product map of two `C^n` functions on a set is `C^n` on the product set. -/ lemma cont_diff_on.prod_map {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E'] {F' : Type*} [normed_add_comm_group F'] [normed_space 𝕜 F'] {s : set E} {t : set E'} {f : E → F} {g : E' → F'} (hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g t) : cont_diff_on 𝕜 n (prod.map f g) (s ×ˢ t) := (hf.comp cont_diff_on_fst (prod_subset_preimage_fst _ _)).prod (hg.comp (cont_diff_on_snd) (prod_subset_preimage_snd _ _)) /-- The product map of two `C^n` functions within a set at a point is `C^n` within the product set at the product point. -/ lemma cont_diff_at.prod_map {f : E → F} {g : E' → F'} {x : E} {y : E'} (hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g y) : cont_diff_at 𝕜 n (prod.map f g) (x, y) := begin rw cont_diff_at at *, convert hf.prod_map hg, simp only [univ_prod_univ] end /-- The product map of two `C^n` functions within a set at a point is `C^n` within the product set at the product point. -/ lemma cont_diff_at.prod_map' {f : E → F} {g : E' → F'} {p : E × E'} (hf : cont_diff_at 𝕜 n f p.1) (hg : cont_diff_at 𝕜 n g p.2) : cont_diff_at 𝕜 n (prod.map f g) p := begin rcases p, exact cont_diff_at.prod_map hf hg end /-- The product map of two `C^n` functions is `C^n`. -/ lemma cont_diff.prod_map {f : E → F} {g : E' → F'} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) : cont_diff 𝕜 n (prod.map f g) := begin rw cont_diff_iff_cont_diff_at at *, exact λ ⟨x, y⟩, (hf x).prod_map (hg y) end lemma cont_diff_prod_mk_left (f₀ : F) : cont_diff 𝕜 n (λ e : E, (e, f₀)) := cont_diff_id.prod cont_diff_const lemma cont_diff_prod_mk_right (e₀ : E) : cont_diff 𝕜 n (λ f : F, (e₀, f)) := cont_diff_const.prod cont_diff_id end prod_map /-! ### Inversion in a complete normed algebra -/ section algebra_inverse variables (𝕜) {R : Type*} [normed_ring R] [normed_algebra 𝕜 R] open normed_ring continuous_linear_map ring /-- In a complete normed algebra, the operation of inversion is `C^n`, for all `n`, at each invertible element. The proof is by induction, bootstrapping using an identity expressing the derivative of inversion as a bilinear map of inversion itself. -/ lemma cont_diff_at_ring_inverse [complete_space R] (x : Rˣ) : cont_diff_at 𝕜 n ring.inverse (x : R) := begin induction n using enat.nat_induction with n IH Itop, { intros m hm, refine ⟨{y : R | is_unit y}, _, _⟩, { simp [nhds_within_univ], exact x.nhds }, { use (ftaylor_series_within 𝕜 inverse univ), rw [le_antisymm hm bot_le, has_ftaylor_series_up_to_on_zero_iff], split, { rintros _ ⟨x', rfl⟩, exact (inverse_continuous_at x').continuous_within_at }, { simp [ftaylor_series_within] } } }, { apply cont_diff_at_succ_iff_has_fderiv_at.mpr, refine ⟨λ (x : R), - mul_left_right 𝕜 R (inverse x) (inverse x), _, _⟩, { refine ⟨{y : R | is_unit y}, x.nhds, _⟩, rintros _ ⟨y, rfl⟩, rw [inverse_unit], exact has_fderiv_at_ring_inverse y }, { convert (mul_left_right_is_bounded_bilinear 𝕜 R).cont_diff.neg.comp_cont_diff_at (x : R) (IH.prod IH) } }, { exact cont_diff_at_top.mpr Itop } end variables (𝕜) {𝕜' : Type*} [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [complete_space 𝕜'] lemma cont_diff_at_inv {x : 𝕜'} (hx : x ≠ 0) {n} : cont_diff_at 𝕜 n has_inv.inv x := by simpa only [ring.inverse_eq_inv'] using cont_diff_at_ring_inverse 𝕜 (units.mk0 x hx) lemma cont_diff_on_inv {n} : cont_diff_on 𝕜 n (has_inv.inv : 𝕜' → 𝕜') {0}ᶜ := λ x hx, (cont_diff_at_inv 𝕜 hx).cont_diff_within_at variable {𝕜} -- TODO: the next few lemmas don't need `𝕜` or `𝕜'` to be complete -- A good way to show this is to generalize `cont_diff_at_ring_inverse` to the setting -- of a function `f` such that `∀ᶠ x in 𝓝 a, x * f x = 1`. lemma cont_diff_within_at.inv {f : E → 𝕜'} {n} (hf : cont_diff_within_at 𝕜 n f s x) (hx : f x ≠ 0) : cont_diff_within_at 𝕜 n (λ x, (f x)⁻¹) s x := (cont_diff_at_inv 𝕜 hx).comp_cont_diff_within_at x hf lemma cont_diff_on.inv {f : E → 𝕜'} {n} (hf : cont_diff_on 𝕜 n f s) (h : ∀ x ∈ s, f x ≠ 0) : cont_diff_on 𝕜 n (λ x, (f x)⁻¹) s := λ x hx, (hf.cont_diff_within_at hx).inv (h x hx) lemma cont_diff_at.inv {f : E → 𝕜'} {n} (hf : cont_diff_at 𝕜 n f x) (hx : f x ≠ 0) : cont_diff_at 𝕜 n (λ x, (f x)⁻¹) x := hf.inv hx lemma cont_diff.inv {f : E → 𝕜'} {n} (hf : cont_diff 𝕜 n f) (h : ∀ x, f x ≠ 0) : cont_diff 𝕜 n (λ x, (f x)⁻¹) := by { rw cont_diff_iff_cont_diff_at, exact λ x, hf.cont_diff_at.inv (h x) } -- TODO: generalize to `f g : E → 𝕜'` lemma cont_diff_within_at.div [complete_space 𝕜] {f g : E → 𝕜} {n} (hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x) (hx : g x ≠ 0) : cont_diff_within_at 𝕜 n (λ x, f x / g x) s x := by simpa only [div_eq_mul_inv] using hf.mul (hg.inv hx) lemma cont_diff_on.div [complete_space 𝕜] {f g : E → 𝕜} {n} (hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) (h₀ : ∀ x ∈ s, g x ≠ 0) : cont_diff_on 𝕜 n (f / g) s := λ x hx, (hf x hx).div (hg x hx) (h₀ x hx) lemma cont_diff_at.div [complete_space 𝕜] {f g : E → 𝕜} {n} (hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x) (hx : g x ≠ 0) : cont_diff_at 𝕜 n (λ x, f x / g x) x := hf.div hg hx lemma cont_diff.div [complete_space 𝕜] {f g : E → 𝕜} {n} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) (h0 : ∀ x, g x ≠ 0) : cont_diff 𝕜 n (λ x, f x / g x) := begin simp only [cont_diff_iff_cont_diff_at] at *, exact λ x, (hf x).div (hg x) (h0 x) end end algebra_inverse /-! ### Inversion of continuous linear maps between Banach spaces -/ section map_inverse open continuous_linear_map /-- At a continuous linear equivalence `e : E ≃L[𝕜] F` between Banach spaces, the operation of inversion is `C^n`, for all `n`. -/ lemma cont_diff_at_map_inverse [complete_space E] (e : E ≃L[𝕜] F) : cont_diff_at 𝕜 n inverse (e : E →L[𝕜] F) := begin nontriviality E, -- first, we use the lemma `to_ring_inverse` to rewrite in terms of `ring.inverse` in the ring -- `E →L[𝕜] E` let O₁ : (E →L[𝕜] E) → (F →L[𝕜] E) := λ f, f.comp (e.symm : (F →L[𝕜] E)), let O₂ : (E →L[𝕜] F) → (E →L[𝕜] E) := λ f, (e.symm : (F →L[𝕜] E)).comp f, have : continuous_linear_map.inverse = O₁ ∘ ring.inverse ∘ O₂ := funext (to_ring_inverse e), rw this, -- `O₁` and `O₂` are `cont_diff`, -- so we reduce to proving that `ring.inverse` is `cont_diff` have h₁ : cont_diff 𝕜 n O₁ := cont_diff_id.clm_comp cont_diff_const, have h₂ : cont_diff 𝕜 n O₂ := cont_diff_const.clm_comp cont_diff_id, refine h₁.cont_diff_at.comp _ (cont_diff_at.comp _ _ h₂.cont_diff_at), convert cont_diff_at_ring_inverse 𝕜 (1 : (E →L[𝕜] E)ˣ), simp [O₂, one_def] end end map_inverse section function_inverse open continuous_linear_map /-- If `f` is a local homeomorphism and the point `a` is in its target, and if `f` is `n` times continuously differentiable at `f.symm a`, and if the derivative at `f.symm a` is a continuous linear equivalence, then `f.symm` is `n` times continuously differentiable at the point `a`. This is one of the easy parts of the inverse function theorem: it assumes that we already have an inverse function. -/ theorem local_homeomorph.cont_diff_at_symm [complete_space E] (f : local_homeomorph E F) {f₀' : E ≃L[𝕜] F} {a : F} (ha : a ∈ f.target) (hf₀' : has_fderiv_at f (f₀' : E →L[𝕜] F) (f.symm a)) (hf : cont_diff_at 𝕜 n f (f.symm a)) : cont_diff_at 𝕜 n f.symm a := begin -- We prove this by induction on `n` induction n using enat.nat_induction with n IH Itop, { rw cont_diff_at_zero, exact ⟨f.target, is_open.mem_nhds f.open_target ha, f.continuous_inv_fun⟩ }, { obtain ⟨f', ⟨u, hu, hff'⟩, hf'⟩ := cont_diff_at_succ_iff_has_fderiv_at.mp hf, apply cont_diff_at_succ_iff_has_fderiv_at.mpr, -- For showing `n.succ` times continuous differentiability (the main inductive step), it -- suffices to produce the derivative and show that it is `n` times continuously differentiable have eq_f₀' : f' (f.symm a) = f₀', { exact (hff' (f.symm a) (mem_of_mem_nhds hu)).unique hf₀' }, -- This follows by a bootstrapping formula expressing the derivative as a function of `f` itself refine ⟨inverse ∘ f' ∘ f.symm, _, _⟩, { -- We first check that the derivative of `f` is that formula have h_nhds : {y : E | ∃ (e : E ≃L[𝕜] F), ↑e = f' y} ∈ 𝓝 ((f.symm) a), { have hf₀' := f₀'.nhds, rw ← eq_f₀' at hf₀', exact hf'.continuous_at.preimage_mem_nhds hf₀' }, obtain ⟨t, htu, ht, htf⟩ := mem_nhds_iff.mp (filter.inter_mem hu h_nhds), use f.target ∩ (f.symm) ⁻¹' t, refine ⟨is_open.mem_nhds _ _, _⟩, { exact f.preimage_open_of_open_symm ht }, { exact mem_inter ha (mem_preimage.mpr htf) }, intros x hx, obtain ⟨hxu, e, he⟩ := htu hx.2, have h_deriv : has_fderiv_at f ↑e ((f.symm) x), { rw he, exact hff' (f.symm x) hxu }, convert f.has_fderiv_at_symm hx.1 h_deriv, simp [← he] }, { -- Then we check that the formula, being a composition of `cont_diff` pieces, is -- itself `cont_diff` have h_deriv₁ : cont_diff_at 𝕜 n inverse (f' (f.symm a)), { rw eq_f₀', exact cont_diff_at_map_inverse _ }, have h_deriv₂ : cont_diff_at 𝕜 n f.symm a, { refine IH (hf.of_le _), norm_cast, exact nat.le_succ n }, exact (h_deriv₁.comp _ hf').comp _ h_deriv₂ } }, { refine cont_diff_at_top.mpr _, intros n, exact Itop n (cont_diff_at_top.mp hf n) } end /-- If `f` is an `n` times continuously differentiable homeomorphism, and if the derivative of `f` at each point is a continuous linear equivalence, then `f.symm` is `n` times continuously differentiable. This is one of the easy parts of the inverse function theorem: it assumes that we already have an inverse function. -/ theorem homeomorph.cont_diff_symm [complete_space E] (f : E ≃ₜ F) {f₀' : E → E ≃L[𝕜] F} (hf₀' : ∀ a, has_fderiv_at f (f₀' a : E →L[𝕜] F) a) (hf : cont_diff 𝕜 n (f : E → F)) : cont_diff 𝕜 n (f.symm : F → E) := cont_diff_iff_cont_diff_at.2 $ λ x, f.to_local_homeomorph.cont_diff_at_symm (mem_univ x) (hf₀' _) hf.cont_diff_at /-- Let `f` be a local homeomorphism of a nontrivially normed field, let `a` be a point in its target. if `f` is `n` times continuously differentiable at `f.symm a`, and if the derivative at `f.symm a` is nonzero, then `f.symm` is `n` times continuously differentiable at the point `a`. This is one of the easy parts of the inverse function theorem: it assumes that we already have an inverse function. -/ theorem local_homeomorph.cont_diff_at_symm_deriv [complete_space 𝕜] (f : local_homeomorph 𝕜 𝕜) {f₀' a : 𝕜} (h₀ : f₀' ≠ 0) (ha : a ∈ f.target) (hf₀' : has_deriv_at f f₀' (f.symm a)) (hf : cont_diff_at 𝕜 n f (f.symm a)) : cont_diff_at 𝕜 n f.symm a := f.cont_diff_at_symm ha (hf₀'.has_fderiv_at_equiv h₀) hf /-- Let `f` be an `n` times continuously differentiable homeomorphism of a nontrivially normed field. Suppose that the derivative of `f` is never equal to zero. Then `f.symm` is `n` times continuously differentiable. This is one of the easy parts of the inverse function theorem: it assumes that we already have an inverse function. -/ theorem homeomorph.cont_diff_symm_deriv [complete_space 𝕜] (f : 𝕜 ≃ₜ 𝕜) {f' : 𝕜 → 𝕜} (h₀ : ∀ x, f' x ≠ 0) (hf' : ∀ x, has_deriv_at f (f' x) x) (hf : cont_diff 𝕜 n (f : 𝕜 → 𝕜)) : cont_diff 𝕜 n (f.symm : 𝕜 → 𝕜) := cont_diff_iff_cont_diff_at.2 $ λ x, f.to_local_homeomorph.cont_diff_at_symm_deriv (h₀ _) (mem_univ x) (hf' _) hf.cont_diff_at end function_inverse /-! ### Finite dimensional results -/ section finite_dimensional open function finite_dimensional variables [complete_space 𝕜] /-- A family of continuous linear maps is `C^n` on `s` if all its applications are. -/ lemma cont_diff_on_clm_apply {n : ℕ∞} {f : E → F →L[𝕜] G} {s : set E} [finite_dimensional 𝕜 F] : cont_diff_on 𝕜 n f s ↔ ∀ y, cont_diff_on 𝕜 n (λ x, f x y) s := begin refine ⟨λ h y, h.clm_apply cont_diff_on_const, λ h, _⟩, let d := finrank 𝕜 F, have hd : d = finrank 𝕜 (fin d → 𝕜) := (finrank_fin_fun 𝕜).symm, let e₁ := continuous_linear_equiv.of_finrank_eq hd, let e₂ := (e₁.arrow_congr (1 : G ≃L[𝕜] G)).trans (continuous_linear_equiv.pi_ring (fin d)), rw [← comp.left_id f, ← e₂.symm_comp_self], exact e₂.symm.cont_diff.comp_cont_diff_on (cont_diff_on_pi.mpr (λ i, h _)) end lemma cont_diff_clm_apply_iff {n : ℕ∞} {f : E → F →L[𝕜] G} [finite_dimensional 𝕜 F] : cont_diff 𝕜 n f ↔ ∀ y, cont_diff 𝕜 n (λ x, f x y) := by simp_rw [← cont_diff_on_univ, cont_diff_on_clm_apply] /-- This is a useful lemma to prove that a certain operation preserves functions being `C^n`. When you do induction on `n`, this gives a useful characterization of a function being `C^(n+1)`, assuming you have already computed the derivative. The advantage of this version over `cont_diff_succ_iff_fderiv` is that both occurences of `cont_diff` are for functions with the same domain and codomain (`E` and `F`). This is not the case for `cont_diff_succ_iff_fderiv`, which often requires an inconvenient need to generalize `F`, which results in universe issues (see the discussion in the section of `cont_diff.comp`). This lemma avoids these universe issues, but only applies for finite dimensional `E`. -/ lemma cont_diff_succ_iff_fderiv_apply [finite_dimensional 𝕜 E] {n : ℕ} {f : E → F} : cont_diff 𝕜 ((n + 1) : ℕ) f ↔ differentiable 𝕜 f ∧ ∀ y, cont_diff 𝕜 n (λ x, fderiv 𝕜 f x y) := by rw [cont_diff_succ_iff_fderiv, cont_diff_clm_apply_iff] lemma cont_diff_on_succ_of_fderiv_apply [finite_dimensional 𝕜 E] {n : ℕ} {f : E → F} {s : set E} (hf : differentiable_on 𝕜 f s) (h : ∀ y, cont_diff_on 𝕜 n (λ x, fderiv_within 𝕜 f s x y) s) : cont_diff_on 𝕜 ((n + 1) : ℕ) f s := cont_diff_on_succ_of_fderiv_within hf $ cont_diff_on_clm_apply.mpr h lemma cont_diff_on_succ_iff_fderiv_apply [finite_dimensional 𝕜 E] {n : ℕ} {f : E → F} {s : set E} (hs : unique_diff_on 𝕜 s) : cont_diff_on 𝕜 ((n + 1) : ℕ) f s ↔ differentiable_on 𝕜 f s ∧ ∀ y, cont_diff_on 𝕜 n (λ x, fderiv_within 𝕜 f s x y) s := by rw [cont_diff_on_succ_iff_fderiv_within hs, cont_diff_on_clm_apply] end finite_dimensional section real /-! ### Results over `ℝ` or `ℂ` The results in this section rely on the Mean Value Theorem, and therefore hold only over `ℝ` (and its extension fields such as `ℂ`). -/ variables {𝕂 : Type*} [is_R_or_C 𝕂] {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕂 E'] {F' : Type*} [normed_add_comm_group F'] [normed_space 𝕂 F'] /-- If a function has a Taylor series at order at least 1, then at points in the interior of the domain of definition, the term of order 1 of this series is a strict derivative of `f`. -/ lemma has_ftaylor_series_up_to_on.has_strict_fderiv_at {s : set E'} {f : E' → F'} {x : E'} {p : E' → formal_multilinear_series 𝕂 E' F'} (hf : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hs : s ∈ 𝓝 x) : has_strict_fderiv_at f ((continuous_multilinear_curry_fin1 𝕂 E' F') (p x 1)) x := has_strict_fderiv_at_of_has_fderiv_at_of_continuous_at (hf.eventually_has_fderiv_at hn hs) $ (continuous_multilinear_curry_fin1 𝕂 E' F').continuous_at.comp $ (hf.cont 1 hn).continuous_at hs /-- If a function is `C^n` with `1 ≤ n` around a point, and its derivative at that point is given to us as `f'`, then `f'` is also a strict derivative. -/ lemma cont_diff_at.has_strict_fderiv_at' {f : E' → F'} {f' : E' →L[𝕂] F'} {x : E'} (hf : cont_diff_at 𝕂 n f x) (hf' : has_fderiv_at f f' x) (hn : 1 ≤ n) : has_strict_fderiv_at f f' x := begin rcases hf 1 hn with ⟨u, H, p, hp⟩, simp only [nhds_within_univ, mem_univ, insert_eq_of_mem] at H, have := hp.has_strict_fderiv_at le_rfl H, rwa hf'.unique this.has_fderiv_at end /-- If a function is `C^n` with `1 ≤ n` around a point, and its derivative at that point is given to us as `f'`, then `f'` is also a strict derivative. -/ lemma cont_diff_at.has_strict_deriv_at' {f : 𝕂 → F'} {f' : F'} {x : 𝕂} (hf : cont_diff_at 𝕂 n f x) (hf' : has_deriv_at f f' x) (hn : 1 ≤ n) : has_strict_deriv_at f f' x := hf.has_strict_fderiv_at' hf' hn /-- If a function is `C^n` with `1 ≤ n` around a point, then the derivative of `f` at this point is also a strict derivative. -/ lemma cont_diff_at.has_strict_fderiv_at {f : E' → F'} {x : E'} (hf : cont_diff_at 𝕂 n f x) (hn : 1 ≤ n) : has_strict_fderiv_at f (fderiv 𝕂 f x) x := hf.has_strict_fderiv_at' (hf.differentiable_at hn).has_fderiv_at hn /-- If a function is `C^n` with `1 ≤ n` around a point, then the derivative of `f` at this point is also a strict derivative. -/ lemma cont_diff_at.has_strict_deriv_at {f : 𝕂 → F'} {x : 𝕂} (hf : cont_diff_at 𝕂 n f x) (hn : 1 ≤ n) : has_strict_deriv_at f (deriv f x) x := (hf.has_strict_fderiv_at hn).has_strict_deriv_at /-- If a function is `C^n` with `1 ≤ n`, then the derivative of `f` is also a strict derivative. -/ lemma cont_diff.has_strict_fderiv_at {f : E' → F'} {x : E'} (hf : cont_diff 𝕂 n f) (hn : 1 ≤ n) : has_strict_fderiv_at f (fderiv 𝕂 f x) x := hf.cont_diff_at.has_strict_fderiv_at hn /-- If a function is `C^n` with `1 ≤ n`, then the derivative of `f` is also a strict derivative. -/ lemma cont_diff.has_strict_deriv_at {f : 𝕂 → F'} {x : 𝕂} (hf : cont_diff 𝕂 n f) (hn : 1 ≤ n) : has_strict_deriv_at f (deriv f x) x := hf.cont_diff_at.has_strict_deriv_at hn /-- If `f` has a formal Taylor series `p` up to order `1` on `{x} ∪ s`, where `s` is a convex set, and `‖p x 1‖₊ < K`, then `f` is `K`-Lipschitz in a neighborhood of `x` within `s`. -/ lemma has_ftaylor_series_up_to_on.exists_lipschitz_on_with_of_nnnorm_lt {E F : Type*} [normed_add_comm_group E] [normed_space ℝ E] [normed_add_comm_group F] [normed_space ℝ F] {f : E → F} {p : E → formal_multilinear_series ℝ E F} {s : set E} {x : E} (hf : has_ftaylor_series_up_to_on 1 f p (insert x s)) (hs : convex ℝ s) (K : ℝ≥0) (hK : ‖p x 1‖₊ < K) : ∃ t ∈ 𝓝[s] x, lipschitz_on_with K f t := begin set f' := λ y, continuous_multilinear_curry_fin1 ℝ E F (p y 1), have hder : ∀ y ∈ s, has_fderiv_within_at f (f' y) s y, from λ y hy, (hf.has_fderiv_within_at le_rfl (subset_insert x s hy)).mono (subset_insert x s), have hcont : continuous_within_at f' s x, from (continuous_multilinear_curry_fin1 ℝ E F).continuous_at.comp_continuous_within_at ((hf.cont _ le_rfl _ (mem_insert _ _)).mono (subset_insert x s)), replace hK : ‖f' x‖₊ < K, by simpa only [linear_isometry_equiv.nnnorm_map], exact hs.exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at_of_nnnorm_lt (eventually_nhds_within_iff.2 $ eventually_of_forall hder) hcont K hK end /-- If `f` has a formal Taylor series `p` up to order `1` on `{x} ∪ s`, where `s` is a convex set, then `f` is Lipschitz in a neighborhood of `x` within `s`. -/ lemma has_ftaylor_series_up_to_on.exists_lipschitz_on_with {E F : Type*} [normed_add_comm_group E] [normed_space ℝ E] [normed_add_comm_group F] [normed_space ℝ F] {f : E → F} {p : E → formal_multilinear_series ℝ E F} {s : set E} {x : E} (hf : has_ftaylor_series_up_to_on 1 f p (insert x s)) (hs : convex ℝ s) : ∃ K (t ∈ 𝓝[s] x), lipschitz_on_with K f t := (exists_gt _).imp $ hf.exists_lipschitz_on_with_of_nnnorm_lt hs /-- If `f` is `C^1` within a conves set `s` at `x`, then it is Lipschitz on a neighborhood of `x` within `s`. -/ lemma cont_diff_within_at.exists_lipschitz_on_with {E F : Type*} [normed_add_comm_group E] [normed_space ℝ E] [normed_add_comm_group F] [normed_space ℝ F] {f : E → F} {s : set E} {x : E} (hf : cont_diff_within_at ℝ 1 f s x) (hs : convex ℝ s) : ∃ (K : ℝ≥0) (t ∈ 𝓝[s] x), lipschitz_on_with K f t := begin rcases hf 1 le_rfl with ⟨t, hst, p, hp⟩, rcases metric.mem_nhds_within_iff.mp hst with ⟨ε, ε0, hε⟩, replace hp : has_ftaylor_series_up_to_on 1 f p (metric.ball x ε ∩ insert x s) := hp.mono hε, clear hst hε t, rw [← insert_eq_of_mem (metric.mem_ball_self ε0), ← insert_inter_distrib] at hp, rcases hp.exists_lipschitz_on_with ((convex_ball _ _).inter hs) with ⟨K, t, hst, hft⟩, rw [inter_comm, ← nhds_within_restrict' _ (metric.ball_mem_nhds _ ε0)] at hst, exact ⟨K, t, hst, hft⟩ end /-- If `f` is `C^1` at `x` and `K > ‖fderiv 𝕂 f x‖`, then `f` is `K`-Lipschitz in a neighborhood of `x`. -/ lemma cont_diff_at.exists_lipschitz_on_with_of_nnnorm_lt {f : E' → F'} {x : E'} (hf : cont_diff_at 𝕂 1 f x) (K : ℝ≥0) (hK : ‖fderiv 𝕂 f x‖₊ < K) : ∃ t ∈ 𝓝 x, lipschitz_on_with K f t := (hf.has_strict_fderiv_at le_rfl).exists_lipschitz_on_with_of_nnnorm_lt K hK /-- If `f` is `C^1` at `x`, then `f` is Lipschitz in a neighborhood of `x`. -/ lemma cont_diff_at.exists_lipschitz_on_with {f : E' → F'} {x : E'} (hf : cont_diff_at 𝕂 1 f x) : ∃ K (t ∈ 𝓝 x), lipschitz_on_with K f t := (hf.has_strict_fderiv_at le_rfl).exists_lipschitz_on_with end real section deriv /-! ### One dimension All results up to now have been expressed in terms of the general Fréchet derivative `fderiv`. For maps defined on the field, the one-dimensional derivative `deriv` is often easier to use. In this paragraph, we reformulate some higher smoothness results in terms of `deriv`. -/ variables {f₂ : 𝕜 → F} {s₂ : set 𝕜} open continuous_linear_map (smul_right) /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (formulated with `deriv_within`) is `C^n`. -/ theorem cont_diff_on_succ_iff_deriv_within {n : ℕ} (hs : unique_diff_on 𝕜 s₂) : cont_diff_on 𝕜 ((n + 1) : ℕ) f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ cont_diff_on 𝕜 n (deriv_within f₂ s₂) s₂ := begin rw cont_diff_on_succ_iff_fderiv_within hs, congr' 2, apply le_antisymm, { assume h, have : deriv_within f₂ s₂ = (λ u : 𝕜 →L[𝕜] F, u 1) ∘ (fderiv_within 𝕜 f₂ s₂), by { ext x, refl }, simp only [this], apply cont_diff.comp_cont_diff_on _ h, exact (is_bounded_bilinear_map_apply.is_bounded_linear_map_left _).cont_diff }, { assume h, have : fderiv_within 𝕜 f₂ s₂ = smul_right (1 : 𝕜 →L[𝕜] 𝕜) ∘ deriv_within f₂ s₂, by { ext x, simp [deriv_within] }, simp only [this], apply cont_diff.comp_cont_diff_on _ h, have : is_bounded_bilinear_map 𝕜 (λ _ : (𝕜 →L[𝕜] 𝕜) × F, _) := is_bounded_bilinear_map_smul_right, exact (this.is_bounded_linear_map_right _).cont_diff } end /-- A function is `C^(n + 1)` on an open domain if and only if it is differentiable there, and its derivative (formulated with `deriv`) is `C^n`. -/ theorem cont_diff_on_succ_iff_deriv_of_open {n : ℕ} (hs : is_open s₂) : cont_diff_on 𝕜 ((n + 1) : ℕ) f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ cont_diff_on 𝕜 n (deriv f₂) s₂ := begin rw cont_diff_on_succ_iff_deriv_within hs.unique_diff_on, congrm _ ∧ _, exact cont_diff_on_congr (λ _, deriv_within_of_open hs) end /-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (formulated with `deriv_within`) is `C^∞`. -/ theorem cont_diff_on_top_iff_deriv_within (hs : unique_diff_on 𝕜 s₂) : cont_diff_on 𝕜 ∞ f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ cont_diff_on 𝕜 ∞ (deriv_within f₂ s₂) s₂ := begin split, { assume h, refine ⟨h.differentiable_on le_top, _⟩, apply cont_diff_on_top.2 (λ n, ((cont_diff_on_succ_iff_deriv_within hs).1 _).2), exact h.of_le le_top }, { assume h, refine cont_diff_on_top.2 (λ n, _), have A : (n : ℕ∞) ≤ ∞ := le_top, apply ((cont_diff_on_succ_iff_deriv_within hs).2 ⟨h.1, h.2.of_le A⟩).of_le, exact with_top.coe_le_coe.2 (nat.le_succ n) } end /-- A function is `C^∞` on an open domain if and only if it is differentiable there, and its derivative (formulated with `deriv`) is `C^∞`. -/ theorem cont_diff_on_top_iff_deriv_of_open (hs : is_open s₂) : cont_diff_on 𝕜 ∞ f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ cont_diff_on 𝕜 ∞ (deriv f₂) s₂ := begin rw cont_diff_on_top_iff_deriv_within hs.unique_diff_on, congrm _ ∧ _, exact cont_diff_on_congr (λ _, deriv_within_of_open hs) end lemma cont_diff_on.deriv_within (hf : cont_diff_on 𝕜 n f₂ s₂) (hs : unique_diff_on 𝕜 s₂) (hmn : m + 1 ≤ n) : cont_diff_on 𝕜 m (deriv_within f₂ s₂) s₂ := begin cases m, { change ∞ + 1 ≤ n at hmn, have : n = ∞, by simpa using hmn, rw this at hf, exact ((cont_diff_on_top_iff_deriv_within hs).1 hf).2 }, { change (m.succ : ℕ∞) ≤ n at hmn, exact ((cont_diff_on_succ_iff_deriv_within hs).1 (hf.of_le hmn)).2 } end lemma cont_diff_on.deriv_of_open (hf : cont_diff_on 𝕜 n f₂ s₂) (hs : is_open s₂) (hmn : m + 1 ≤ n) : cont_diff_on 𝕜 m (deriv f₂) s₂ := (hf.deriv_within hs.unique_diff_on hmn).congr (λ x hx, (deriv_within_of_open hs hx).symm) lemma cont_diff_on.continuous_on_deriv_within (h : cont_diff_on 𝕜 n f₂ s₂) (hs : unique_diff_on 𝕜 s₂) (hn : 1 ≤ n) : continuous_on (deriv_within f₂ s₂) s₂ := ((cont_diff_on_succ_iff_deriv_within hs).1 (h.of_le hn)).2.continuous_on lemma cont_diff_on.continuous_on_deriv_of_open (h : cont_diff_on 𝕜 n f₂ s₂) (hs : is_open s₂) (hn : 1 ≤ n) : continuous_on (deriv f₂) s₂ := ((cont_diff_on_succ_iff_deriv_of_open hs).1 (h.of_le hn)).2.continuous_on /-- A function is `C^(n + 1)` if and only if it is differentiable, and its derivative (formulated in terms of `deriv`) is `C^n`. -/ theorem cont_diff_succ_iff_deriv {n : ℕ} : cont_diff 𝕜 ((n + 1) : ℕ) f₂ ↔ differentiable 𝕜 f₂ ∧ cont_diff 𝕜 n (deriv f₂) := by simp only [← cont_diff_on_univ, cont_diff_on_succ_iff_deriv_of_open, is_open_univ, differentiable_on_univ] theorem cont_diff_one_iff_deriv : cont_diff 𝕜 1 f₂ ↔ differentiable 𝕜 f₂ ∧ continuous (deriv f₂) := cont_diff_succ_iff_deriv.trans $ iff.rfl.and cont_diff_zero /-- A function is `C^∞` if and only if it is differentiable, and its derivative (formulated in terms of `deriv`) is `C^∞`. -/ theorem cont_diff_top_iff_deriv : cont_diff 𝕜 ∞ f₂ ↔ differentiable 𝕜 f₂ ∧ cont_diff 𝕜 ∞ (deriv f₂) := begin simp only [← cont_diff_on_univ, ← differentiable_on_univ, ← deriv_within_univ], rw cont_diff_on_top_iff_deriv_within unique_diff_on_univ, end lemma cont_diff.continuous_deriv (h : cont_diff 𝕜 n f₂) (hn : 1 ≤ n) : continuous (deriv f₂) := (cont_diff_succ_iff_deriv.mp (h.of_le hn)).2.continuous lemma cont_diff.iterate_deriv : ∀ (n : ℕ) {f₂ : 𝕜 → F} (hf : cont_diff 𝕜 ∞ f₂), cont_diff 𝕜 ∞ (deriv^[n] f₂) | 0 f₂ hf := hf | (n + 1) f₂ hf := cont_diff.iterate_deriv n (cont_diff_top_iff_deriv.mp hf).2 lemma cont_diff.iterate_deriv' (n : ℕ) : ∀ (k : ℕ) {f₂ : 𝕜 → F} (hf : cont_diff 𝕜 (n + k : ℕ) f₂), cont_diff 𝕜 n (deriv^[k] f₂) | 0 f₂ hf := hf | (n + 1) f₂ hf := cont_diff.iterate_deriv' n (cont_diff_succ_iff_deriv.mp hf).2 end deriv section restrict_scalars /-! ### Restricting from `ℂ` to `ℝ`, or generally from `𝕜'` to `𝕜` If a function is `n` times continuously differentiable over `ℂ`, then it is `n` times continuously differentiable over `ℝ`. In this paragraph, we give variants of this statement, in the general situation where `ℂ` and `ℝ` are replaced respectively by `𝕜'` and `𝕜` where `𝕜'` is a normed algebra over `𝕜`. -/ variables (𝕜) {𝕜' : Type*} [nontrivially_normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] variables [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] variables [normed_space 𝕜' F] [is_scalar_tower 𝕜 𝕜' F] variables {p' : E → formal_multilinear_series 𝕜' E F} lemma has_ftaylor_series_up_to_on.restrict_scalars (h : has_ftaylor_series_up_to_on n f p' s) : has_ftaylor_series_up_to_on n f (λ x, (p' x).restrict_scalars 𝕜) s := { zero_eq := λ x hx, h.zero_eq x hx, fderiv_within := begin intros m hm x hx, convert ((continuous_multilinear_map.restrict_scalars_linear 𝕜).has_fderiv_at) .comp_has_fderiv_within_at _ ((h.fderiv_within m hm x hx).restrict_scalars 𝕜), end, cont := λ m hm, continuous_multilinear_map.continuous_restrict_scalars.comp_continuous_on (h.cont m hm) } lemma cont_diff_within_at.restrict_scalars (h : cont_diff_within_at 𝕜' n f s x) : cont_diff_within_at 𝕜 n f s x := begin intros m hm, rcases h m hm with ⟨u, u_mem, p', hp'⟩, exact ⟨u, u_mem, _, hp'.restrict_scalars _⟩ end lemma cont_diff_on.restrict_scalars (h : cont_diff_on 𝕜' n f s) : cont_diff_on 𝕜 n f s := λ x hx, (h x hx).restrict_scalars _ lemma cont_diff_at.restrict_scalars (h : cont_diff_at 𝕜' n f x) : cont_diff_at 𝕜 n f x := cont_diff_within_at_univ.1 $ h.cont_diff_within_at.restrict_scalars _ lemma cont_diff.restrict_scalars (h : cont_diff 𝕜' n f) : cont_diff 𝕜 n f := cont_diff_iff_cont_diff_at.2 $ λ x, h.cont_diff_at.restrict_scalars _ end restrict_scalars /-!## Quantitative bounds -/ /-- Bounding the norm of the iterated derivative of `B (f x) (g x)` within a set in terms of the iterated derivatives of `f` and `g` when `B` is bilinear. This lemma is an auxiliary version assuming all spaces live in the same universe, to enable an induction. Use instead `continuous_linear_map.norm_iterated_fderiv_within_le_of_bilinear` that removes this assumption. -/ lemma continuous_linear_map.norm_iterated_fderiv_within_le_of_bilinear_aux {Du Eu Fu Gu : Type u} [normed_add_comm_group Du] [normed_space 𝕜 Du] [normed_add_comm_group Eu] [normed_space 𝕜 Eu] [normed_add_comm_group Fu] [normed_space 𝕜 Fu] [normed_add_comm_group Gu] [normed_space 𝕜 Gu] (B : Eu →L[𝕜] Fu →L[𝕜] Gu) {f : Du → Eu} {g : Du → Fu} {n : ℕ} {s : set Du} {x : Du} (hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) : ‖iterated_fderiv_within 𝕜 n (λ y, B (f y) (g y)) s x‖ ≤ ‖B‖ * ∑ i in finset.range (n+1), (n.choose i : ℝ) * ‖iterated_fderiv_within 𝕜 i f s x‖ * ‖iterated_fderiv_within 𝕜 (n-i) g s x‖ := begin /- We argue by induction on `n`. The bound is trivial for `n = 0`. For `n + 1`, we write the `(n+1)`-th derivative as the `n`-th derivative of the derivative `B f g' + B f' g`, and apply the inductive assumption to each of those two terms. For this induction to make sense, the spaces of linear maps that appear in the induction should be in the same universe as the original spaces, which explains why we assume in the lemma that all spaces live in the same universe. -/ unfreezingI { induction n with n IH generalizing Eu Fu Gu}, { simp only [←mul_assoc, norm_iterated_fderiv_within_zero, finset.range_one, finset.sum_singleton, nat.choose_self, algebra_map.coe_one, one_mul], apply ((B (f x)).le_op_norm (g x)).trans, apply mul_le_mul_of_nonneg_right _ (norm_nonneg _), exact B.le_op_norm (f x) }, { have In : (n : ℕ∞) + 1 ≤ n.succ, by simp only [nat.cast_succ, le_refl], have I1 : ‖iterated_fderiv_within 𝕜 n (λ (y : Du), B.precompR Du (f y) (fderiv_within 𝕜 g s y)) s x‖ ≤ ‖B‖ * ∑ (i : ℕ) in finset.range (n + 1), n.choose i * ‖iterated_fderiv_within 𝕜 i f s x‖ * ‖iterated_fderiv_within 𝕜 (n + 1 - i) g s x‖ := calc ‖iterated_fderiv_within 𝕜 n (λ (y : Du), B.precompR Du (f y) (fderiv_within 𝕜 g s y)) s x‖ ≤ ‖B.precompR Du‖ * ∑ (i : ℕ) in finset.range (n + 1), n.choose i * ‖iterated_fderiv_within 𝕜 i f s x‖ * ‖iterated_fderiv_within 𝕜 (n - i) (fderiv_within 𝕜 g s) s x‖ : IH _ (hf.of_le (nat.cast_le.2 (nat.le_succ n))) (hg.fderiv_within hs In) ... ≤ ‖B‖ * ∑ (i : ℕ) in finset.range (n + 1), n.choose i * ‖iterated_fderiv_within 𝕜 i f s x‖ * ‖iterated_fderiv_within 𝕜 (n - i) (fderiv_within 𝕜 g s) s x‖ : mul_le_mul_of_nonneg_right (B.norm_precompR_le Du) (finset.sum_nonneg' (λ i, by positivity)) ... = _ : begin congr' 1, apply finset.sum_congr rfl (λ i hi, _ ), rw [nat.succ_sub (nat.lt_succ_iff.1 (finset.mem_range.1 hi)), iterated_fderiv_within_succ_eq_comp_right hs hx, linear_isometry_equiv.norm_map], end, have I2 : ‖iterated_fderiv_within 𝕜 n (λ (y : Du), B.precompL Du (fderiv_within 𝕜 f s y) (g y)) s x‖ ≤ ‖B‖ * ∑ (i : ℕ) in finset.range (n + 1), n.choose i * ‖iterated_fderiv_within 𝕜 (i + 1) f s x‖ * ‖iterated_fderiv_within 𝕜 (n - i) g s x‖ := calc ‖iterated_fderiv_within 𝕜 n (λ (y : Du), B.precompL Du (fderiv_within 𝕜 f s y) (g y)) s x‖ ≤ ‖B.precompL Du‖ * ∑ (i : ℕ) in finset.range (n + 1), n.choose i * ‖iterated_fderiv_within 𝕜 i (fderiv_within 𝕜 f s) s x‖ * ‖iterated_fderiv_within 𝕜 (n - i) g s x‖ : IH _ (hf.fderiv_within hs In) (hg.of_le (nat.cast_le.2 (nat.le_succ n))) ... ≤ ‖B‖ * ∑ (i : ℕ) in finset.range (n + 1), n.choose i * ‖iterated_fderiv_within 𝕜 i (fderiv_within 𝕜 f s) s x‖ * ‖iterated_fderiv_within 𝕜 (n - i) g s x‖ : mul_le_mul_of_nonneg_right (B.norm_precompL_le Du) (finset.sum_nonneg' (λ i, by positivity)) ... = _ : begin congr' 1, apply finset.sum_congr rfl (λ i hi, _ ), rw [iterated_fderiv_within_succ_eq_comp_right hs hx, linear_isometry_equiv.norm_map], end, have J : iterated_fderiv_within 𝕜 n (λ (y : Du), fderiv_within 𝕜 (λ (y : Du), B (f y) (g y)) s y) s x = iterated_fderiv_within 𝕜 n (λ y, B.precompR Du (f y) (fderiv_within 𝕜 g s y) + B.precompL Du (fderiv_within 𝕜 f s y) (g y)) s x, { apply iterated_fderiv_within_congr hs (λ y hy, _) hx, have L : (1 : ℕ∞) ≤ n.succ, by simpa only [enat.coe_one, nat.one_le_cast] using nat.succ_pos n, exact B.fderiv_within_of_bilinear (hf.differentiable_on L y hy) (hg.differentiable_on L y hy) (hs y hy) }, rw [iterated_fderiv_within_succ_eq_comp_right hs hx, linear_isometry_equiv.norm_map, J], have A : cont_diff_on 𝕜 n (λ y, B.precompR Du (f y) (fderiv_within 𝕜 g s y)) s, from (B.precompR Du).is_bounded_bilinear_map.cont_diff.comp_cont_diff_on₂ (hf.of_le (nat.cast_le.2 (nat.le_succ n))) (hg.fderiv_within hs In), have A' : cont_diff_on 𝕜 n (λ y, B.precompL Du (fderiv_within 𝕜 f s y) (g y)) s, from (B.precompL Du).is_bounded_bilinear_map.cont_diff.comp_cont_diff_on₂ (hf.fderiv_within hs In) (hg.of_le (nat.cast_le.2 (nat.le_succ n))), rw iterated_fderiv_within_add_apply' A A' hs hx, apply (norm_add_le _ _).trans ((add_le_add I1 I2).trans (le_of_eq _)), simp_rw [← mul_add, mul_assoc], congr' 1, exact (finset.sum_choose_succ_mul (λ i j, ‖iterated_fderiv_within 𝕜 i f s x‖ * ‖iterated_fderiv_within 𝕜 j g s x‖) n).symm } end /-- Bounding the norm of the iterated derivative of `B (f x) (g x)` within a set in terms of the iterated derivatives of `f` and `g` when `B` is bilinear: `‖D^n (x ↦ B (f x) (g x))‖ ≤ ‖B‖ ∑_{k ≤ n} n.choose k ‖D^k f‖ ‖D^{n-k} g‖` -/ lemma continuous_linear_map.norm_iterated_fderiv_within_le_of_bilinear (B : E →L[𝕜] F →L[𝕜] G) {f : D → E} {g : D → F} {N : ℕ∞} {s : set D} {x : D} (hf : cont_diff_on 𝕜 N f s) (hg : cont_diff_on 𝕜 N g s) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) {n : ℕ} (hn : (n : ℕ∞) ≤ N) : ‖iterated_fderiv_within 𝕜 n (λ y, B (f y) (g y)) s x‖ ≤ ‖B‖ * ∑ i in finset.range (n+1), (n.choose i : ℝ) * ‖iterated_fderiv_within 𝕜 i f s x‖ * ‖iterated_fderiv_within 𝕜 (n-i) g s x‖ := begin /- We reduce the bound to the case where all spaces live in the same universe (in which we already have proved the result), by using linear isometries between the spaces and their `ulift` to a common universe. These linear isometries preserve the norm of the iterated derivative. -/ let Du : Type (max uD uE uF uG) := ulift.{(max uE uF uG) uD} D, let Eu : Type (max uD uE uF uG) := ulift.{(max uD uF uG) uE} E, let Fu : Type (max uD uE uF uG) := ulift.{(max uD uE uG) uF} F, let Gu : Type (max uD uE uF uG) := ulift.{(max uD uE uF) uG} G, have isoD : Du ≃ₗᵢ[𝕜] D := linear_isometry_equiv.ulift 𝕜 D, have isoE : Eu ≃ₗᵢ[𝕜] E := linear_isometry_equiv.ulift 𝕜 E, have isoF : Fu ≃ₗᵢ[𝕜] F := linear_isometry_equiv.ulift 𝕜 F, have isoG : Gu ≃ₗᵢ[𝕜] G := linear_isometry_equiv.ulift 𝕜 G, -- lift `f` and `g` to versions `fu` and `gu` on the lifted spaces. let fu : Du → Eu := isoE.symm ∘ f ∘ isoD, let gu : Du → Fu := isoF.symm ∘ g ∘ isoD, -- lift the bilinear map `B` to a bilinear map `Bu` on the lifted spaces. let Bu₀ : Eu →L[𝕜] Fu →L[𝕜] G, from ((B.comp (isoE : Eu →L[𝕜] E)).flip.comp (isoF : Fu →L[𝕜] F)).flip, let Bu : Eu →L[𝕜] Fu →L[𝕜] Gu, from continuous_linear_map.compL 𝕜 Eu (Fu →L[𝕜] G) (Fu →L[𝕜] Gu) (continuous_linear_map.compL 𝕜 Fu G Gu (isoG.symm : G →L[𝕜] Gu)) Bu₀, have Bu_eq : (λ y, Bu (fu y) (gu y)) = isoG.symm ∘ (λ y, B (f y) (g y)) ∘ isoD, { ext1 y, simp only [Bu, continuous_linear_map.compL_apply, function.comp_app, continuous_linear_map.coe_comp', linear_isometry_equiv.coe_coe'', continuous_linear_map.flip_apply, linear_isometry_equiv.apply_symm_apply] }, -- All norms are preserved by the lifting process. have Bu_le : ‖Bu‖ ≤ ‖B‖, { refine continuous_linear_map.op_norm_le_bound _ (norm_nonneg _) (λ y, _), refine continuous_linear_map.op_norm_le_bound _ (by positivity) (λ x, _ ), simp only [Bu, continuous_linear_map.compL_apply, continuous_linear_map.coe_comp', function.comp_app, linear_isometry_equiv.coe_coe'', continuous_linear_map.flip_apply, linear_isometry_equiv.norm_map], calc ‖B (isoE y) (isoF x)‖ ≤ ‖B (isoE y)‖ * ‖isoF x‖ : continuous_linear_map.le_op_norm _ _ ... ≤ ‖B‖ * ‖isoE y‖ * ‖isoF x‖ : mul_le_mul_of_nonneg_right (continuous_linear_map.le_op_norm _ _) (norm_nonneg _) ... = ‖B‖ * ‖y‖ * ‖x‖ : by simp only [linear_isometry_equiv.norm_map] }, let su := isoD ⁻¹' s, have hsu : unique_diff_on 𝕜 su, from isoD.to_continuous_linear_equiv.unique_diff_on_preimage_iff.2 hs, let xu := isoD.symm x, have hxu : xu ∈ su, by simpa only [set.mem_preimage, linear_isometry_equiv.apply_symm_apply] using hx, have xu_x : isoD xu = x, by simp only [linear_isometry_equiv.apply_symm_apply], have hfu : cont_diff_on 𝕜 n fu su, from isoE.symm.cont_diff.comp_cont_diff_on ((hf.of_le hn).comp_continuous_linear_map (isoD : Du →L[𝕜] D)), have hgu : cont_diff_on 𝕜 n gu su, from isoF.symm.cont_diff.comp_cont_diff_on ((hg.of_le hn).comp_continuous_linear_map (isoD : Du →L[𝕜] D)), have Nfu : ∀ i, ‖iterated_fderiv_within 𝕜 i fu su xu‖ = ‖iterated_fderiv_within 𝕜 i f s x‖, { assume i, rw linear_isometry_equiv.norm_iterated_fderiv_within_comp_left _ _ hsu hxu, rw [linear_isometry_equiv.norm_iterated_fderiv_within_comp_right _ _ hs, xu_x], rwa ← xu_x at hx }, have Ngu : ∀ i, ‖iterated_fderiv_within 𝕜 i gu su xu‖ = ‖iterated_fderiv_within 𝕜 i g s x‖, { assume i, rw linear_isometry_equiv.norm_iterated_fderiv_within_comp_left _ _ hsu hxu, rw [linear_isometry_equiv.norm_iterated_fderiv_within_comp_right _ _ hs, xu_x], rwa ← xu_x at hx }, have NBu : ‖iterated_fderiv_within 𝕜 n (λ y, Bu (fu y) (gu y)) su xu‖ = ‖iterated_fderiv_within 𝕜 n (λ y, B (f y) (g y)) s x‖, { rw Bu_eq, rw linear_isometry_equiv.norm_iterated_fderiv_within_comp_left _ _ hsu hxu, rw [linear_isometry_equiv.norm_iterated_fderiv_within_comp_right _ _ hs, xu_x], rwa ← xu_x at hx }, -- state the bound for the lifted objects, and deduce the original bound from it. have : ‖iterated_fderiv_within 𝕜 n (λ y, Bu (fu y) (gu y)) su xu‖ ≤ ‖Bu‖ * ∑ i in finset.range (n+1), (n.choose i : ℝ) * ‖iterated_fderiv_within 𝕜 i fu su xu‖ * ‖iterated_fderiv_within 𝕜 (n-i) gu su xu‖, from Bu.norm_iterated_fderiv_within_le_of_bilinear_aux hfu hgu hsu hxu, simp only [Nfu, Ngu, NBu] at this, apply this.trans (mul_le_mul_of_nonneg_right Bu_le _), exact finset.sum_nonneg' (λ i, by positivity), end /-- Bounding the norm of the iterated derivative of `B (f x) (g x)` in terms of the iterated derivatives of `f` and `g` when `B` is bilinear: `‖D^n (x ↦ B (f x) (g x))‖ ≤ ‖B‖ ∑_{k ≤ n} n.choose k ‖D^k f‖ ‖D^{n-k} g‖` -/ lemma continuous_linear_map.norm_iterated_fderiv_le_of_bilinear (B : E →L[𝕜] F →L[𝕜] G) {f : D → E} {g : D → F} {N : ℕ∞} (hf : cont_diff 𝕜 N f) (hg : cont_diff 𝕜 N g) (x : D) {n : ℕ} (hn : (n : ℕ∞) ≤ N) : ‖iterated_fderiv 𝕜 n (λ y, B (f y) (g y)) x‖ ≤ ‖B‖ * ∑ i in finset.range (n+1), (n.choose i : ℝ) * ‖iterated_fderiv 𝕜 i f x‖ * ‖iterated_fderiv 𝕜 (n-i) g x‖ := begin simp_rw [← iterated_fderiv_within_univ], exact B.norm_iterated_fderiv_within_le_of_bilinear hf.cont_diff_on hg.cont_diff_on unique_diff_on_univ (mem_univ x) hn, end /-- Bounding the norm of the iterated derivative of `B (f x) (g x)` within a set in terms of the iterated derivatives of `f` and `g` when `B` is bilinear of norm at most `1`: `‖D^n (x ↦ B (f x) (g x))‖ ≤ ∑_{k ≤ n} n.choose k ‖D^k f‖ ‖D^{n-k} g‖` -/ lemma continuous_linear_map.norm_iterated_fderiv_within_le_of_bilinear_of_le_one (B : E →L[𝕜] F →L[𝕜] G) {f : D → E} {g : D → F} {N : ℕ∞} {s : set D} {x : D} (hf : cont_diff_on 𝕜 N f s) (hg : cont_diff_on 𝕜 N g s) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) {n : ℕ} (hn : (n : ℕ∞) ≤ N) (hB : ‖B‖ ≤ 1) : ‖iterated_fderiv_within 𝕜 n (λ y, B (f y) (g y)) s x‖ ≤ ∑ i in finset.range (n+1), (n.choose i : ℝ) * ‖iterated_fderiv_within 𝕜 i f s x‖ * ‖iterated_fderiv_within 𝕜 (n-i) g s x‖ := begin apply (B.norm_iterated_fderiv_within_le_of_bilinear hf hg hs hx hn).trans, apply mul_le_of_le_one_left (finset.sum_nonneg' (λ i, _)) hB, positivity end /-- Bounding the norm of the iterated derivative of `B (f x) (g x)` in terms of the iterated derivatives of `f` and `g` when `B` is bilinear of norm at most `1`: `‖D^n (x ↦ B (f x) (g x))‖ ≤ ∑_{k ≤ n} n.choose k ‖D^k f‖ ‖D^{n-k} g‖` -/ lemma continuous_linear_map.norm_iterated_fderiv_le_of_bilinear_of_le_one (B : E →L[𝕜] F →L[𝕜] G) {f : D → E} {g : D → F} {N : ℕ∞} (hf : cont_diff 𝕜 N f) (hg : cont_diff 𝕜 N g) (x : D) {n : ℕ} (hn : (n : ℕ∞) ≤ N) (hB : ‖B‖ ≤ 1) : ‖iterated_fderiv 𝕜 n (λ y, B (f y) (g y)) x‖ ≤ ∑ i in finset.range (n+1), (n.choose i : ℝ) * ‖iterated_fderiv 𝕜 i f x‖ * ‖iterated_fderiv 𝕜 (n-i) g x‖ := begin simp_rw [← iterated_fderiv_within_univ], exact B.norm_iterated_fderiv_within_le_of_bilinear_of_le_one hf.cont_diff_on hg.cont_diff_on unique_diff_on_univ (mem_univ x) hn hB, end section variables {𝕜' : Type*} [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [normed_space 𝕜' F] [is_scalar_tower 𝕜 𝕜' F] lemma norm_iterated_fderiv_within_smul_le {f : E → 𝕜'} {g : E → F} {N : ℕ∞} (hf : cont_diff_on 𝕜 N f s) (hg : cont_diff_on 𝕜 N g s) (hs : unique_diff_on 𝕜 s) {x : E} (hx : x ∈ s) {n : ℕ} (hn : (n : ℕ∞) ≤ N) : ‖iterated_fderiv_within 𝕜 n (λ y, f y • g y) s x‖ ≤ ∑ i in finset.range (n+1), (n.choose i : ℝ) * ‖iterated_fderiv_within 𝕜 i f s x‖ * ‖iterated_fderiv_within 𝕜 (n-i) g s x‖ := (continuous_linear_map.lsmul 𝕜 𝕜' : 𝕜' →L[𝕜] F →L[𝕜] F) .norm_iterated_fderiv_within_le_of_bilinear_of_le_one hf hg hs hx hn continuous_linear_map.op_norm_lsmul_le lemma norm_iterated_fderiv_smul_le {f : E → 𝕜'} {g : E → F} {N : ℕ∞} (hf : cont_diff 𝕜 N f) (hg : cont_diff 𝕜 N g) (x : E) {n : ℕ} (hn : (n : ℕ∞) ≤ N) : ‖iterated_fderiv 𝕜 n (λ y, f y • g y) x‖ ≤ ∑ i in finset.range (n+1), (n.choose i : ℝ) * ‖iterated_fderiv 𝕜 i f x‖ * ‖iterated_fderiv 𝕜 (n-i) g x‖ := (continuous_linear_map.lsmul 𝕜 𝕜' : 𝕜' →L[𝕜] F →L[𝕜] F) .norm_iterated_fderiv_le_of_bilinear_of_le_one hf hg x hn continuous_linear_map.op_norm_lsmul_le end section variables {A : Type*} [normed_ring A] [normed_algebra 𝕜 A] lemma norm_iterated_fderiv_within_mul_le {f : E → A} {g : E → A} {N : ℕ∞} (hf : cont_diff_on 𝕜 N f s) (hg : cont_diff_on 𝕜 N g s) (hs : unique_diff_on 𝕜 s) {x : E} (hx : x ∈ s) {n : ℕ} (hn : (n : ℕ∞) ≤ N) : ‖iterated_fderiv_within 𝕜 n (λ y, f y * g y) s x‖ ≤ ∑ i in finset.range (n+1), (n.choose i : ℝ) * ‖iterated_fderiv_within 𝕜 i f s x‖ * ‖iterated_fderiv_within 𝕜 (n-i) g s x‖ := (continuous_linear_map.mul 𝕜 A : A →L[𝕜] A →L[𝕜] A) .norm_iterated_fderiv_within_le_of_bilinear_of_le_one hf hg hs hx hn (continuous_linear_map.op_norm_mul_le _ _) lemma norm_iterated_fderiv_mul_le {f : E → A} {g : E → A} {N : ℕ∞} (hf : cont_diff 𝕜 N f) (hg : cont_diff 𝕜 N g) (x : E) {n : ℕ} (hn : (n : ℕ∞) ≤ N) : ‖iterated_fderiv 𝕜 n (λ y, f y * g y) x‖ ≤ ∑ i in finset.range (n+1), (n.choose i : ℝ) * ‖iterated_fderiv 𝕜 i f x‖ * ‖iterated_fderiv 𝕜 (n-i) g x‖ := begin simp_rw [← iterated_fderiv_within_univ], exact norm_iterated_fderiv_within_mul_le hf.cont_diff_on hg.cont_diff_on unique_diff_on_univ (mem_univ x) hn, end end /-- If the derivatives within a set of `g` at `f x` are bounded by `C`, and the `i`-th derivative within a set of `f` at `x` is bounded by `D^i` for all `1 ≤ i ≤ n`, then the `n`-th derivative of `g ∘ f` is bounded by `n! * C * D^n`. This lemma proves this estimate assuming additionally that two of the spaces live in the same universe, to make an induction possible. Use instead `norm_iterated_fderiv_within_comp_le` that removes this assumption. -/ lemma norm_iterated_fderiv_within_comp_le_aux {Fu Gu : Type u} [normed_add_comm_group Fu] [normed_space 𝕜 Fu] [normed_add_comm_group Gu] [normed_space 𝕜 Gu] {g : Fu → Gu} {f : E → Fu} {n : ℕ} {s : set E} {t : set Fu} {x : E} (hg : cont_diff_on 𝕜 n g t) (hf : cont_diff_on 𝕜 n f s) (ht : unique_diff_on 𝕜 t) (hs : unique_diff_on 𝕜 s) (hst : maps_to f s t) (hx : x ∈ s) {C : ℝ} {D : ℝ} (hC : ∀ i, i ≤ n → ‖iterated_fderiv_within 𝕜 i g t (f x)‖ ≤ C) (hD : ∀ i, 1 ≤ i → i ≤ n → ‖iterated_fderiv_within 𝕜 i f s x‖ ≤ D^i) : ‖iterated_fderiv_within 𝕜 n (g ∘ f) s x‖ ≤ n! * C * D^n := begin /- We argue by induction on `n`, using that `D^(n+1) (g ∘ f) = D^n (g ' ∘ f ⬝ f')`. The successive derivatives of `g' ∘ f` are controlled thanks to the inductive assumption, and those of `f'` are controlled by assumption. As composition of linear maps is a bilinear map, one may use `continuous_linear_map.norm_iterated_fderiv_le_of_bilinear_of_le_one` to get from these a bound on `D^n (g ' ∘ f ⬝ f')`. -/ unfreezingI { induction n using nat.case_strong_induction_on with n IH generalizing Gu }, { simpa only [norm_iterated_fderiv_within_zero, nat.factorial_zero, algebra_map.coe_one, one_mul, pow_zero, mul_one] using hC 0 le_rfl }, have M : (n : ℕ∞) < n.succ := nat.cast_lt.2 n.lt_succ_self, have Cnonneg : 0 ≤ C := (norm_nonneg _).trans (hC 0 bot_le), have Dnonneg : 0 ≤ D, { have : 1 ≤ n+1, by simp only [le_add_iff_nonneg_left, zero_le'], simpa only [pow_one] using (norm_nonneg _).trans (hD 1 le_rfl this) }, -- use the inductive assumption to bound the derivatives of `g' ∘ f`. have I : ∀ i ∈ finset.range (n+1), ‖iterated_fderiv_within 𝕜 i ((fderiv_within 𝕜 g t) ∘ f) s x‖ ≤ i! * C * D^i, { assume i hi, simp only [finset.mem_range_succ_iff] at hi, apply IH i hi, apply hf.of_le (nat.cast_le.2 (hi.trans n.le_succ)), { assume j hj h'j, exact hD j hj (h'j.trans (hi.trans n.le_succ)) }, { apply hg.fderiv_within ht, simp only [nat.cast_succ], exact add_le_add_right (nat.cast_le.2 hi) _ }, { assume j hj, have : ‖iterated_fderiv_within 𝕜 j (fderiv_within 𝕜 g t) t (f x)‖ = ‖iterated_fderiv_within 𝕜 (j+1) g t (f x)‖, by rw [iterated_fderiv_within_succ_eq_comp_right ht (hst hx), linear_isometry_equiv.norm_map], rw this, exact hC (j+1) (add_le_add (hj.trans hi) le_rfl) } }, -- reformulate `hD` as a bound for the derivatives of `f'`. have J : ∀ i, ‖iterated_fderiv_within 𝕜 (n - i) (fderiv_within 𝕜 f s) s x‖ ≤ D ^ (n - i + 1), { assume i, have : ‖iterated_fderiv_within 𝕜 (n - i) (fderiv_within 𝕜 f s) s x‖ = ‖iterated_fderiv_within 𝕜 (n - i + 1) f s x‖, by rw [iterated_fderiv_within_succ_eq_comp_right hs hx, linear_isometry_equiv.norm_map], rw this, apply hD, { simp only [le_add_iff_nonneg_left, zero_le'] }, { apply nat.succ_le_succ tsub_le_self } }, -- Now put these together: first, notice that we have to bound `D^n (g' ∘ f ⬝ f')`. calc ‖iterated_fderiv_within 𝕜 (n+1) (g ∘ f) s x‖ = ‖iterated_fderiv_within 𝕜 n (λ (y : E), fderiv_within 𝕜 (g ∘ f) s y) s x‖ : by rw [iterated_fderiv_within_succ_eq_comp_right hs hx, linear_isometry_equiv.norm_map] ... = ‖iterated_fderiv_within 𝕜 n (λ (y : E), continuous_linear_map.compL 𝕜 E Fu Gu (fderiv_within 𝕜 g t (f y)) (fderiv_within 𝕜 f s y)) s x‖ : begin have L : (1 : ℕ∞) ≤ n.succ, by simpa only [enat.coe_one, nat.one_le_cast] using n.succ_pos, congr' 1, apply iterated_fderiv_within_congr hs (λ y hy, _) hx, apply fderiv_within.comp _ _ _ hst (hs y hy), { exact hg.differentiable_on L _ (hst hy) }, { exact hf.differentiable_on L _ hy } end -- bound it using the fact that the composition of linear maps is a bilinear operation, -- for which we have bounds for the`n`-th derivative. ... ≤ ∑ i in finset.range (n+1), (n.choose i : ℝ) * ‖iterated_fderiv_within 𝕜 i ((fderiv_within 𝕜 g t) ∘ f) s x‖ * ‖iterated_fderiv_within 𝕜 (n-i) (fderiv_within 𝕜 f s) s x‖ : begin have A : cont_diff_on 𝕜 n ((fderiv_within 𝕜 g t) ∘ f) s, { apply cont_diff_on.comp _ (hf.of_le M.le) hst, apply hg.fderiv_within ht, simp only [nat.cast_succ, le_refl] }, have B : cont_diff_on 𝕜 n (fderiv_within 𝕜 f s) s, { apply hf.fderiv_within hs, simp only [nat.cast_succ, le_refl] }, exact (continuous_linear_map.compL 𝕜 E Fu Gu) .norm_iterated_fderiv_within_le_of_bilinear_of_le_one A B hs hx le_rfl (continuous_linear_map.norm_compL_le 𝕜 E Fu Gu), end -- bound each of the terms using the estimates on previous derivatives (that use the inductive -- assumption for `g' ∘ f`). ... ≤ ∑ i in finset.range (n+1), (n.choose i : ℝ) * (i! * C * D^i) * (D^(n-i+1)) : begin apply finset.sum_le_sum (λ i hi, _), simp only [mul_assoc (n.choose i : ℝ)], refine mul_le_mul_of_nonneg_left _ (nat.cast_nonneg _), apply mul_le_mul (I i hi) (J i) (norm_nonneg _), positivity, end -- We are left with trivial algebraic manipulations to see that this is smaller than -- the claimed bound. ... = ∑ i in finset.range (n+1), (n! : ℝ) * (i!⁻¹ * i!) * C * (D^i * D^(n-i+1)) * (n-i)!⁻¹ : begin apply finset.sum_congr rfl (λ i hi, _), simp only [nat.cast_choose ℝ (finset.mem_range_succ_iff.1 hi), div_eq_inv_mul, mul_inv], ring, end ... = ∑ i in finset.range (n+1), (n! : ℝ) * 1 * C * D^(n+1) * (n-i)!⁻¹ : begin apply finset.sum_congr rfl (λ i hi, _), congr' 2, { congr, apply inv_mul_cancel, simpa only [ne.def, nat.cast_eq_zero] using i.factorial_ne_zero }, { rw ← pow_add, congr' 1, rw [nat.add_succ, nat.succ_inj'], exact nat.add_sub_of_le (finset.mem_range_succ_iff.1 hi) } end ... ≤ ∑ i in finset.range (n+1), (n! : ℝ) * 1 * C * D^(n+1) * 1 : begin apply finset.sum_le_sum (λ i hi, _), refine mul_le_mul_of_nonneg_left _ (by positivity), apply inv_le_one, simpa only [nat.one_le_cast] using (n-i).factorial_pos, end ... = (n+1)! * C * D^(n+1) : by simp only [mul_assoc, mul_one, finset.sum_const, finset.card_range, nsmul_eq_mul, nat.factorial_succ, nat.cast_mul], end /-- If the derivatives within a set of `g` at `f x` are bounded by `C`, and the `i`-th derivative within a set of `f` at `x` is bounded by `D^i` for all `1 ≤ i ≤ n`, then the `n`-th derivative of `g ∘ f` is bounded by `n! * C * D^n`. -/ lemma norm_iterated_fderiv_within_comp_le {g : F → G} {f : E → F} {n : ℕ} {s : set E} {t : set F} {x : E} {N : ℕ∞} (hg : cont_diff_on 𝕜 N g t) (hf : cont_diff_on 𝕜 N f s) (hn : (n : ℕ∞) ≤ N) (ht : unique_diff_on 𝕜 t) (hs : unique_diff_on 𝕜 s) (hst : maps_to f s t) (hx : x ∈ s) {C : ℝ} {D : ℝ} (hC : ∀ i, i ≤ n → ‖iterated_fderiv_within 𝕜 i g t (f x)‖ ≤ C) (hD : ∀ i, 1 ≤ i → i ≤ n → ‖iterated_fderiv_within 𝕜 i f s x‖ ≤ D^i) : ‖iterated_fderiv_within 𝕜 n (g ∘ f) s x‖ ≤ n! * C * D^n := begin /- We reduce the bound to the case where all spaces live in the same universe (in which we already have proved the result), by using linear isometries between the spaces and their `ulift` to a common universe. These linear isometries preserve the norm of the iterated derivative. -/ let Fu : Type (max uF uG) := ulift.{uG uF} F, let Gu : Type (max uF uG) := ulift.{uF uG} G, have isoF : Fu ≃ₗᵢ[𝕜] F := linear_isometry_equiv.ulift 𝕜 F, have isoG : Gu ≃ₗᵢ[𝕜] G := linear_isometry_equiv.ulift 𝕜 G, -- lift `f` and `g` to versions `fu` and `gu` on the lifted spaces. let fu : E → Fu := isoF.symm ∘ f, let gu : Fu → Gu := isoG.symm ∘ g ∘ isoF, let tu := isoF ⁻¹' t, have htu : unique_diff_on 𝕜 tu, from isoF.to_continuous_linear_equiv.unique_diff_on_preimage_iff.2 ht, have hstu : maps_to fu s tu, { assume y hy, simpa only [mem_preimage, linear_isometry_equiv.apply_symm_apply] using hst hy }, have Ffu : isoF (fu x) = f x, by simp only [linear_isometry_equiv.apply_symm_apply], -- All norms are preserved by the lifting process. have hfu : cont_diff_on 𝕜 n fu s, from isoF.symm.cont_diff.comp_cont_diff_on (hf.of_le hn), have hgu : cont_diff_on 𝕜 n gu tu, from isoG.symm.cont_diff.comp_cont_diff_on ((hg.of_le hn).comp_continuous_linear_map (isoF : Fu →L[𝕜] F)), have Nfu : ∀ i, ‖iterated_fderiv_within 𝕜 i fu s x‖ = ‖iterated_fderiv_within 𝕜 i f s x‖, { assume i, rw linear_isometry_equiv.norm_iterated_fderiv_within_comp_left _ _ hs hx }, simp_rw [← Nfu] at hD, have Ngu : ∀ i, ‖iterated_fderiv_within 𝕜 i gu tu (fu x)‖ = ‖iterated_fderiv_within 𝕜 i g t (f x)‖, { assume i, rw linear_isometry_equiv.norm_iterated_fderiv_within_comp_left _ _ htu (hstu hx), rw [linear_isometry_equiv.norm_iterated_fderiv_within_comp_right _ _ ht, Ffu], rw Ffu, exact hst hx }, simp_rw [← Ngu] at hC, have Nfgu : ‖iterated_fderiv_within 𝕜 n (g ∘ f) s x‖ = ‖iterated_fderiv_within 𝕜 n (gu ∘ fu) s x‖, { have : gu ∘ fu = isoG.symm ∘ g ∘ f, { ext x, simp only [comp_app, linear_isometry_equiv.map_eq_iff, linear_isometry_equiv.apply_symm_apply] }, rw [this, linear_isometry_equiv.norm_iterated_fderiv_within_comp_left _ _ hs hx] }, -- deduce the required bound from the one for `gu ∘ fu`. rw Nfgu, exact norm_iterated_fderiv_within_comp_le_aux hgu hfu htu hs hstu hx hC hD, end /-- If the derivatives of `g` at `f x` are bounded by `C`, and the `i`-th derivative of `f` at `x` is bounded by `D^i` for all `1 ≤ i ≤ n`, then the `n`-th derivative of `g ∘ f` is bounded by `n! * C * D^n`. -/ lemma norm_iterated_fderiv_comp_le {g : F → G} {f : E → F} {n : ℕ} {N : ℕ∞} (hg : cont_diff 𝕜 N g) (hf : cont_diff 𝕜 N f) (hn : (n : ℕ∞) ≤ N) (x : E) {C : ℝ} {D : ℝ} (hC : ∀ i, i ≤ n → ‖iterated_fderiv 𝕜 i g (f x)‖ ≤ C) (hD : ∀ i, 1 ≤ i → i ≤ n → ‖iterated_fderiv 𝕜 i f x‖ ≤ D^i) : ‖iterated_fderiv 𝕜 n (g ∘ f) x‖ ≤ n! * C * D^n := begin simp_rw [← iterated_fderiv_within_univ] at ⊢ hC hD, exact norm_iterated_fderiv_within_comp_le hg.cont_diff_on hf.cont_diff_on hn unique_diff_on_univ unique_diff_on_univ (maps_to_univ _ _) (mem_univ x) hC hD, end section apply lemma norm_iterated_fderiv_within_clm_apply {f : E → (F →L[𝕜] G)} {g : E → F} {s : set E} {x : E} {N : ℕ∞} {n : ℕ} (hf : cont_diff_on 𝕜 N f s) (hg : cont_diff_on 𝕜 N g s) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (hn : ↑n ≤ N) : ‖iterated_fderiv_within 𝕜 n (λ y, (f y) (g y)) s x‖ ≤ (finset.range (n + 1)).sum (λ i, ↑(n.choose i) * ‖iterated_fderiv_within 𝕜 i f s x‖ * ‖iterated_fderiv_within 𝕜 (n - i) g s x‖) := begin let B : (F →L[𝕜] G) →L[𝕜] F →L[𝕜] G := continuous_linear_map.flip (continuous_linear_map.apply 𝕜 G), have hB : ‖B‖ ≤ 1 := begin simp only [continuous_linear_map.op_norm_flip, continuous_linear_map.apply], refine continuous_linear_map.op_norm_le_bound _ zero_le_one (λ f, _), simp only [continuous_linear_map.coe_id', id.def, one_mul], end, exact B.norm_iterated_fderiv_within_le_of_bilinear_of_le_one hf hg hs hx hn hB, end lemma norm_iterated_fderiv_clm_apply {f : E → (F →L[𝕜] G)} {g : E → F} {N : ℕ∞} {n : ℕ} (hf : cont_diff 𝕜 N f) (hg : cont_diff 𝕜 N g) (x : E) (hn : ↑n ≤ N): ‖iterated_fderiv 𝕜 n (λ (y : E), (f y) (g y)) x‖ ≤ (finset.range (n + 1)).sum (λ (i : ℕ), ↑(n.choose i) * ‖iterated_fderiv 𝕜 i f x‖ * ‖iterated_fderiv 𝕜 (n - i) g x‖) := begin simp only [← iterated_fderiv_within_univ], exact norm_iterated_fderiv_within_clm_apply hf.cont_diff_on hg.cont_diff_on unique_diff_on_univ (set.mem_univ x) hn, end lemma norm_iterated_fderiv_within_clm_apply_const {f : E → (F →L[𝕜] G)} {c : F} {s : set E} {x : E} {N : ℕ∞} {n : ℕ} (hf : cont_diff_on 𝕜 N f s) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (hn : ↑n ≤ N) : ‖iterated_fderiv_within 𝕜 n (λ (y : E), (f y) c) s x‖ ≤ ‖c‖ * ‖iterated_fderiv_within 𝕜 n f s x‖ := begin let g : (F →L[𝕜] G) →L[𝕜] G := continuous_linear_map.apply 𝕜 G c, have h := g.norm_comp_continuous_multilinear_map_le (iterated_fderiv_within 𝕜 n f s x), rw ← g.iterated_fderiv_within_comp_left hf hs hx hn at h, refine h.trans (mul_le_mul_of_nonneg_right _ (norm_nonneg _)), refine g.op_norm_le_bound (norm_nonneg _) (λ f, _), rw [continuous_linear_map.apply_apply, mul_comm], exact f.le_op_norm c, end lemma norm_iterated_fderiv_clm_apply_const {f : E → (F →L[𝕜] G)} {c : F} {x : E} {N : ℕ∞} {n : ℕ} (hf : cont_diff 𝕜 N f) (hn : ↑n ≤ N) : ‖iterated_fderiv 𝕜 n (λ (y : E), (f y) c) x‖ ≤ ‖c‖ * ‖iterated_fderiv 𝕜 n f x‖ := begin simp only [← iterated_fderiv_within_univ], refine norm_iterated_fderiv_within_clm_apply_const hf.cont_diff_on unique_diff_on_univ (set.mem_univ x) hn, end end apply
eccfe6435721a81c3d0ab6c1216740a701d28a89
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/bounds.lean
1c2c10c2a4fb58f17ff6e903ca185d4580318671
[]
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
36,756
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.set.intervals.basic import Mathlib.algebra.ordered_group import Mathlib.PostPort universes u u_1 w v namespace Mathlib /-! # Upper / lower bounds In this file we define: * `upper_bounds`, `lower_bounds` : the set of upper bounds (resp., lower bounds) of a set; * `bdd_above s`, `bdd_below s` : the set `s` is bounded above (resp., below), i.e., the set of upper (resp., lower) bounds of `s` is nonempty; * `is_least s a`, `is_greatest s a` : `a` is a least (resp., greatest) element of `s`; for a partial order, it is unique if exists; * `is_lub s a`, `is_glb s a` : `a` is a least upper bound (resp., a greatest lower bound) of `s`; for a partial order, it is unique if exists. We also prove various lemmas about monotonicity, behaviour under `∪`, `∩`, `insert`, and provide formulas for `∅`, `univ`, and intervals. -/ /-! ### Definitions -/ /-- The set of upper bounds of a set. -/ /-- The set of lower bounds of a set. -/ def upper_bounds {α : Type u} [preorder α] (s : set α) : set α := set_of fun (x : α) => ∀ {a : α}, a ∈ s → a ≤ x def lower_bounds {α : Type u} [preorder α] (s : set α) : set α := set_of fun (x : α) => ∀ {a : α}, a ∈ s → x ≤ a /-- A set is bounded above if there exists an upper bound. -/ /-- A set is bounded below if there exists a lower bound. -/ def bdd_above {α : Type u} [preorder α] (s : set α) := set.nonempty (upper_bounds s) def bdd_below {α : Type u} [preorder α] (s : set α) := set.nonempty (lower_bounds s) /-- `a` is a least element of a set `s`; for a partial order, it is unique if exists. -/ /-- `a` is a greatest element of a set `s`; for a partial order, it is unique if exists -/ def is_least {α : Type u} [preorder α] (s : set α) (a : α) := a ∈ s ∧ a ∈ lower_bounds s def is_greatest {α : Type u} [preorder α] (s : set α) (a : α) := a ∈ s ∧ a ∈ upper_bounds s /-- `a` is a least upper bound of a set `s`; for a partial order, it is unique if exists. -/ /-- `a` is a greatest lower bound of a set `s`; for a partial order, it is unique if exists. -/ def is_lub {α : Type u} [preorder α] (s : set α) : α → Prop := is_least (upper_bounds s) def is_glb {α : Type u} [preorder α] (s : set α) : α → Prop := is_greatest (lower_bounds s) theorem mem_upper_bounds {α : Type u} [preorder α] {s : set α} {a : α} : a ∈ upper_bounds s ↔ ∀ (x : α), x ∈ s → x ≤ a := iff.rfl theorem mem_lower_bounds {α : Type u} [preorder α] {s : set α} {a : α} : a ∈ lower_bounds s ↔ ∀ (x : α), x ∈ s → a ≤ x := iff.rfl /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` such that `x` is not greater than or equal to `y`. This version only assumes `preorder` structure and uses `¬(y ≤ x)`. A version for linear orders is called `not_bdd_above_iff`. -/ theorem not_bdd_above_iff' {α : Type u} [preorder α] {s : set α} : ¬bdd_above s ↔ ∀ (x : α), ∃ (y : α), ∃ (H : y ∈ s), ¬y ≤ x := sorry /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` such that `x` is not less than or equal to `y`. This version only assumes `preorder` structure and uses `¬(x ≤ y)`. A version for linear orders is called `not_bdd_below_iff`. -/ theorem not_bdd_below_iff' {α : Type u} [preorder α] {s : set α} : ¬bdd_below s ↔ ∀ (x : α), ∃ (y : α), ∃ (H : y ∈ s), ¬x ≤ y := not_bdd_above_iff' /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` that is greater than `x`. A version for preorders is called `not_bdd_above_iff'`. -/ theorem not_bdd_above_iff {α : Type u_1} [linear_order α] {s : set α} : ¬bdd_above s ↔ ∀ (x : α), ∃ (y : α), ∃ (H : y ∈ s), x < y := sorry /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` that is less than `x`. A version for preorders is called `not_bdd_below_iff'`. -/ theorem not_bdd_below_iff {α : Type u_1} [linear_order α] {s : set α} : ¬bdd_below s ↔ ∀ (x : α), ∃ (y : α), ∃ (H : y ∈ s), y < x := not_bdd_above_iff /-! ### Monotonicity -/ theorem upper_bounds_mono_set {α : Type u} [preorder α] {s : set α} {t : set α} (hst : s ⊆ t) : upper_bounds t ⊆ upper_bounds s := fun (b : α) (hb : b ∈ upper_bounds t) (x : α) (h : x ∈ s) => hb (hst h) theorem lower_bounds_mono_set {α : Type u} [preorder α] {s : set α} {t : set α} (hst : s ⊆ t) : lower_bounds t ⊆ lower_bounds s := fun (b : α) (hb : b ∈ lower_bounds t) (x : α) (h : x ∈ s) => hb (hst h) theorem upper_bounds_mono_mem {α : Type u} [preorder α] {s : set α} {a : α} {b : α} (hab : a ≤ b) : a ∈ upper_bounds s → b ∈ upper_bounds s := fun (ha : a ∈ upper_bounds s) (x : α) (h : x ∈ s) => le_trans (ha h) hab theorem lower_bounds_mono_mem {α : Type u} [preorder α] {s : set α} {a : α} {b : α} (hab : a ≤ b) : b ∈ lower_bounds s → a ∈ lower_bounds s := fun (hb : b ∈ lower_bounds s) (x : α) (h : x ∈ s) => le_trans hab (hb h) theorem upper_bounds_mono {α : Type u} [preorder α] {s : set α} {t : set α} (hst : s ⊆ t) {a : α} {b : α} (hab : a ≤ b) : a ∈ upper_bounds t → b ∈ upper_bounds s := fun (ha : a ∈ upper_bounds t) => upper_bounds_mono_set hst (upper_bounds_mono_mem hab ha) theorem lower_bounds_mono {α : Type u} [preorder α] {s : set α} {t : set α} (hst : s ⊆ t) {a : α} {b : α} (hab : a ≤ b) : b ∈ lower_bounds t → a ∈ lower_bounds s := fun (hb : b ∈ lower_bounds t) => lower_bounds_mono_set hst (lower_bounds_mono_mem hab hb) /-- If `s ⊆ t` and `t` is bounded above, then so is `s`. -/ theorem bdd_above.mono {α : Type u} [preorder α] {s : set α} {t : set α} (h : s ⊆ t) : bdd_above t → bdd_above s := set.nonempty.mono (upper_bounds_mono_set h) /-- If `s ⊆ t` and `t` is bounded below, then so is `s`. -/ theorem bdd_below.mono {α : Type u} [preorder α] {s : set α} {t : set α} (h : s ⊆ t) : bdd_below t → bdd_below s := set.nonempty.mono (lower_bounds_mono_set h) /-- If `a` is a least upper bound for sets `s` and `p`, then it is a least upper bound for any set `t`, `s ⊆ t ⊆ p`. -/ theorem is_lub.of_subset_of_superset {α : Type u} [preorder α] {a : α} {s : set α} {t : set α} {p : set α} (hs : is_lub s a) (hp : is_lub p a) (hst : s ⊆ t) (htp : t ⊆ p) : is_lub t a := { left := upper_bounds_mono_set htp (and.left hp), right := lower_bounds_mono_set (upper_bounds_mono_set hst) (and.right hs) } /-- If `a` is a greatest lower bound for sets `s` and `p`, then it is a greater lower bound for any set `t`, `s ⊆ t ⊆ p`. -/ theorem is_glb.of_subset_of_superset {α : Type u} [preorder α] {a : α} {s : set α} {t : set α} {p : set α} (hs : is_glb s a) (hp : is_glb p a) (hst : s ⊆ t) (htp : t ⊆ p) : is_glb t a := is_lub.of_subset_of_superset hs hp hst htp theorem is_least.mono {α : Type u} [preorder α] {s : set α} {t : set α} {a : α} {b : α} (ha : is_least s a) (hb : is_least t b) (hst : s ⊆ t) : b ≤ a := and.right hb a (hst (and.left ha)) theorem is_greatest.mono {α : Type u} [preorder α] {s : set α} {t : set α} {a : α} {b : α} (ha : is_greatest s a) (hb : is_greatest t b) (hst : s ⊆ t) : a ≤ b := and.right hb a (hst (and.left ha)) theorem is_lub.mono {α : Type u} [preorder α] {s : set α} {t : set α} {a : α} {b : α} (ha : is_lub s a) (hb : is_lub t b) (hst : s ⊆ t) : a ≤ b := is_least.mono hb ha (upper_bounds_mono_set hst) theorem is_glb.mono {α : Type u} [preorder α] {s : set α} {t : set α} {a : α} {b : α} (ha : is_glb s a) (hb : is_glb t b) (hst : s ⊆ t) : b ≤ a := is_greatest.mono hb ha (lower_bounds_mono_set hst) /-! ### Conversions -/ theorem is_least.is_glb {α : Type u} [preorder α] {s : set α} {a : α} (h : is_least s a) : is_glb s a := { left := and.right h, right := fun (b : α) (hb : b ∈ lower_bounds s) => hb (and.left h) } theorem is_greatest.is_lub {α : Type u} [preorder α] {s : set α} {a : α} (h : is_greatest s a) : is_lub s a := { left := and.right h, right := fun (b : α) (hb : b ∈ upper_bounds s) => hb (and.left h) } theorem is_lub.upper_bounds_eq {α : Type u} [preorder α] {s : set α} {a : α} (h : is_lub s a) : upper_bounds s = set.Ici a := sorry theorem is_glb.lower_bounds_eq {α : Type u} [preorder α] {s : set α} {a : α} (h : is_glb s a) : lower_bounds s = set.Iic a := is_lub.upper_bounds_eq h theorem is_least.lower_bounds_eq {α : Type u} [preorder α] {s : set α} {a : α} (h : is_least s a) : lower_bounds s = set.Iic a := is_glb.lower_bounds_eq (is_least.is_glb h) theorem is_greatest.upper_bounds_eq {α : Type u} [preorder α] {s : set α} {a : α} (h : is_greatest s a) : upper_bounds s = set.Ici a := is_lub.upper_bounds_eq (is_greatest.is_lub h) theorem is_lub_le_iff {α : Type u} [preorder α] {s : set α} {a : α} {b : α} (h : is_lub s a) : a ≤ b ↔ b ∈ upper_bounds s := eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ b ↔ b ∈ upper_bounds s)) (is_lub.upper_bounds_eq h))) (iff.refl (a ≤ b)) theorem le_is_glb_iff {α : Type u} [preorder α] {s : set α} {a : α} {b : α} (h : is_glb s a) : b ≤ a ↔ b ∈ lower_bounds s := eq.mpr (id (Eq._oldrec (Eq.refl (b ≤ a ↔ b ∈ lower_bounds s)) (is_glb.lower_bounds_eq h))) (iff.refl (b ≤ a)) /-- If `s` has a least upper bound, then it is bounded above. -/ theorem is_lub.bdd_above {α : Type u} [preorder α] {s : set α} {a : α} (h : is_lub s a) : bdd_above s := Exists.intro a (and.left h) /-- If `s` has a greatest lower bound, then it is bounded below. -/ theorem is_glb.bdd_below {α : Type u} [preorder α] {s : set α} {a : α} (h : is_glb s a) : bdd_below s := Exists.intro a (and.left h) /-- If `s` has a greatest element, then it is bounded above. -/ theorem is_greatest.bdd_above {α : Type u} [preorder α] {s : set α} {a : α} (h : is_greatest s a) : bdd_above s := Exists.intro a (and.right h) /-- If `s` has a least element, then it is bounded below. -/ theorem is_least.bdd_below {α : Type u} [preorder α] {s : set α} {a : α} (h : is_least s a) : bdd_below s := Exists.intro a (and.right h) theorem is_least.nonempty {α : Type u} [preorder α] {s : set α} {a : α} (h : is_least s a) : set.nonempty s := Exists.intro a (and.left h) theorem is_greatest.nonempty {α : Type u} [preorder α] {s : set α} {a : α} (h : is_greatest s a) : set.nonempty s := Exists.intro a (and.left h) /-! ### Union and intersection -/ @[simp] theorem upper_bounds_union {α : Type u} [preorder α] {s : set α} {t : set α} : upper_bounds (s ∪ t) = upper_bounds s ∩ upper_bounds t := sorry @[simp] theorem lower_bounds_union {α : Type u} [preorder α] {s : set α} {t : set α} : lower_bounds (s ∪ t) = lower_bounds s ∩ lower_bounds t := upper_bounds_union theorem union_upper_bounds_subset_upper_bounds_inter {α : Type u} [preorder α] {s : set α} {t : set α} : upper_bounds s ∪ upper_bounds t ⊆ upper_bounds (s ∩ t) := set.union_subset (upper_bounds_mono_set (set.inter_subset_left s t)) (upper_bounds_mono_set (set.inter_subset_right s t)) theorem union_lower_bounds_subset_lower_bounds_inter {α : Type u} [preorder α] {s : set α} {t : set α} : lower_bounds s ∪ lower_bounds t ⊆ lower_bounds (s ∩ t) := union_upper_bounds_subset_upper_bounds_inter theorem is_least_union_iff {α : Type u} [preorder α] {a : α} {s : set α} {t : set α} : is_least (s ∪ t) a ↔ is_least s a ∧ a ∈ lower_bounds t ∨ a ∈ lower_bounds s ∧ is_least t a := sorry theorem is_greatest_union_iff {α : Type u} [preorder α] {s : set α} {t : set α} {a : α} : is_greatest (s ∪ t) a ↔ is_greatest s a ∧ a ∈ upper_bounds t ∨ a ∈ upper_bounds s ∧ is_greatest t a := is_least_union_iff /-- If `s` is bounded, then so is `s ∩ t` -/ theorem bdd_above.inter_of_left {α : Type u} [preorder α] {s : set α} {t : set α} (h : bdd_above s) : bdd_above (s ∩ t) := bdd_above.mono (set.inter_subset_left s t) h /-- If `t` is bounded, then so is `s ∩ t` -/ theorem bdd_above.inter_of_right {α : Type u} [preorder α] {s : set α} {t : set α} (h : bdd_above t) : bdd_above (s ∩ t) := bdd_above.mono (set.inter_subset_right s t) h /-- If `s` is bounded, then so is `s ∩ t` -/ theorem bdd_below.inter_of_left {α : Type u} [preorder α] {s : set α} {t : set α} (h : bdd_below s) : bdd_below (s ∩ t) := bdd_below.mono (set.inter_subset_left s t) h /-- If `t` is bounded, then so is `s ∩ t` -/ theorem bdd_below.inter_of_right {α : Type u} [preorder α] {s : set α} {t : set α} (h : bdd_below t) : bdd_below (s ∩ t) := bdd_below.mono (set.inter_subset_right s t) h /-- If `s` and `t` are bounded above sets in a `semilattice_sup`, then so is `s ∪ t`. -/ theorem bdd_above.union {γ : Type w} [semilattice_sup γ] {s : set γ} {t : set γ} : bdd_above s → bdd_above t → bdd_above (s ∪ t) := sorry /-- The union of two sets is bounded above if and only if each of the sets is. -/ theorem bdd_above_union {γ : Type w} [semilattice_sup γ] {s : set γ} {t : set γ} : bdd_above (s ∪ t) ↔ bdd_above s ∧ bdd_above t := sorry theorem bdd_below.union {γ : Type w} [semilattice_inf γ] {s : set γ} {t : set γ} : bdd_below s → bdd_below t → bdd_below (s ∪ t) := bdd_above.union /--The union of two sets is bounded above if and only if each of the sets is.-/ theorem bdd_below_union {γ : Type w} [semilattice_inf γ] {s : set γ} {t : set γ} : bdd_below (s ∪ t) ↔ bdd_below s ∧ bdd_below t := bdd_above_union /-- If `a` is the least upper bound of `s` and `b` is the least upper bound of `t`, then `a ⊔ b` is the least upper bound of `s ∪ t`. -/ theorem is_lub.union {γ : Type w} [semilattice_sup γ] {a : γ} {b : γ} {s : set γ} {t : set γ} (hs : is_lub s a) (ht : is_lub t b) : is_lub (s ∪ t) (a ⊔ b) := sorry /-- If `a` is the greatest lower bound of `s` and `b` is the greatest lower bound of `t`, then `a ⊓ b` is the greatest lower bound of `s ∪ t`. -/ theorem is_glb.union {γ : Type w} [semilattice_inf γ] {a₁ : γ} {a₂ : γ} {s : set γ} {t : set γ} (hs : is_glb s a₁) (ht : is_glb t a₂) : is_glb (s ∪ t) (a₁ ⊓ a₂) := is_lub.union hs ht /-- If `a` is the least element of `s` and `b` is the least element of `t`, then `min a b` is the least element of `s ∪ t`. -/ theorem is_least.union {γ : Type w} [linear_order γ] {a : γ} {b : γ} {s : set γ} {t : set γ} (ha : is_least s a) (hb : is_least t b) : is_least (s ∪ t) (min a b) := sorry /-- If `a` is the greatest element of `s` and `b` is the greatest element of `t`, then `max a b` is the greatest element of `s ∪ t`. -/ theorem is_greatest.union {γ : Type w} [linear_order γ] {a : γ} {b : γ} {s : set γ} {t : set γ} (ha : is_greatest s a) (hb : is_greatest t b) : is_greatest (s ∪ t) (max a b) := sorry /-! ### Specific sets #### Unbounded intervals -/ theorem is_least_Ici {α : Type u} [preorder α] {a : α} : is_least (set.Ici a) a := { left := set.left_mem_Ici, right := fun (x : α) => id } theorem is_greatest_Iic {α : Type u} [preorder α] {a : α} : is_greatest (set.Iic a) a := { left := set.right_mem_Iic, right := fun (x : α) => id } theorem is_lub_Iic {α : Type u} [preorder α] {a : α} : is_lub (set.Iic a) a := is_greatest.is_lub is_greatest_Iic theorem is_glb_Ici {α : Type u} [preorder α] {a : α} : is_glb (set.Ici a) a := is_least.is_glb is_least_Ici theorem upper_bounds_Iic {α : Type u} [preorder α] {a : α} : upper_bounds (set.Iic a) = set.Ici a := is_lub.upper_bounds_eq is_lub_Iic theorem lower_bounds_Ici {α : Type u} [preorder α] {a : α} : lower_bounds (set.Ici a) = set.Iic a := is_glb.lower_bounds_eq is_glb_Ici theorem bdd_above_Iic {α : Type u} [preorder α] {a : α} : bdd_above (set.Iic a) := is_lub.bdd_above is_lub_Iic theorem bdd_below_Ici {α : Type u} [preorder α] {a : α} : bdd_below (set.Ici a) := is_glb.bdd_below is_glb_Ici theorem bdd_above_Iio {α : Type u} [preorder α] {a : α} : bdd_above (set.Iio a) := Exists.intro a fun (x : α) (hx : x ∈ set.Iio a) => le_of_lt hx theorem bdd_below_Ioi {α : Type u} [preorder α] {a : α} : bdd_below (set.Ioi a) := Exists.intro a fun (x : α) (hx : x ∈ set.Ioi a) => le_of_lt hx theorem is_lub_Iio {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} : is_lub (set.Iio a) a := { left := fun (x : γ) (hx : x ∈ set.Iio a) => le_of_lt hx, right := fun (y : γ) (hy : y ∈ upper_bounds (set.Iio a)) => le_of_forall_ge_of_dense hy } theorem is_glb_Ioi {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} : is_glb (set.Ioi a) a := is_lub_Iio theorem upper_bounds_Iio {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} : upper_bounds (set.Iio a) = set.Ici a := is_lub.upper_bounds_eq is_lub_Iio theorem lower_bounds_Ioi {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} : lower_bounds (set.Ioi a) = set.Iic a := is_glb.lower_bounds_eq is_glb_Ioi /-! #### Singleton -/ theorem is_greatest_singleton {α : Type u} [preorder α] {a : α} : is_greatest (singleton a) a := { left := set.mem_singleton a, right := fun (x : α) (hx : x ∈ singleton a) => le_of_eq (set.eq_of_mem_singleton hx) } theorem is_least_singleton {α : Type u} [preorder α] {a : α} : is_least (singleton a) a := is_greatest_singleton theorem is_lub_singleton {α : Type u} [preorder α] {a : α} : is_lub (singleton a) a := is_greatest.is_lub is_greatest_singleton theorem is_glb_singleton {α : Type u} [preorder α] {a : α} : is_glb (singleton a) a := is_least.is_glb is_least_singleton theorem bdd_above_singleton {α : Type u} [preorder α] {a : α} : bdd_above (singleton a) := is_lub.bdd_above is_lub_singleton theorem bdd_below_singleton {α : Type u} [preorder α] {a : α} : bdd_below (singleton a) := is_glb.bdd_below is_glb_singleton @[simp] theorem upper_bounds_singleton {α : Type u} [preorder α] {a : α} : upper_bounds (singleton a) = set.Ici a := is_lub.upper_bounds_eq is_lub_singleton @[simp] theorem lower_bounds_singleton {α : Type u} [preorder α] {a : α} : lower_bounds (singleton a) = set.Iic a := is_glb.lower_bounds_eq is_glb_singleton /-! #### Bounded intervals -/ theorem bdd_above_Icc {α : Type u} [preorder α] {a : α} {b : α} : bdd_above (set.Icc a b) := Exists.intro b fun (_x : α) => and.right theorem bdd_below_Icc {α : Type u} [preorder α] {a : α} {b : α} : bdd_below (set.Icc a b) := Exists.intro a fun (_x : α) => and.left theorem bdd_above_Ico {α : Type u} [preorder α] {a : α} {b : α} : bdd_above (set.Ico a b) := bdd_above.mono set.Ico_subset_Icc_self bdd_above_Icc theorem bdd_below_Ico {α : Type u} [preorder α] {a : α} {b : α} : bdd_below (set.Ico a b) := bdd_below.mono set.Ico_subset_Icc_self bdd_below_Icc theorem bdd_above_Ioc {α : Type u} [preorder α] {a : α} {b : α} : bdd_above (set.Ioc a b) := bdd_above.mono set.Ioc_subset_Icc_self bdd_above_Icc theorem bdd_below_Ioc {α : Type u} [preorder α] {a : α} {b : α} : bdd_below (set.Ioc a b) := bdd_below.mono set.Ioc_subset_Icc_self bdd_below_Icc theorem bdd_above_Ioo {α : Type u} [preorder α] {a : α} {b : α} : bdd_above (set.Ioo a b) := bdd_above.mono set.Ioo_subset_Icc_self bdd_above_Icc theorem bdd_below_Ioo {α : Type u} [preorder α] {a : α} {b : α} : bdd_below (set.Ioo a b) := bdd_below.mono set.Ioo_subset_Icc_self bdd_below_Icc theorem is_greatest_Icc {α : Type u} [preorder α] {a : α} {b : α} (h : a ≤ b) : is_greatest (set.Icc a b) b := { left := iff.mpr set.right_mem_Icc h, right := fun (x : α) => and.right } theorem is_lub_Icc {α : Type u} [preorder α] {a : α} {b : α} (h : a ≤ b) : is_lub (set.Icc a b) b := is_greatest.is_lub (is_greatest_Icc h) theorem upper_bounds_Icc {α : Type u} [preorder α] {a : α} {b : α} (h : a ≤ b) : upper_bounds (set.Icc a b) = set.Ici b := is_lub.upper_bounds_eq (is_lub_Icc h) theorem is_least_Icc {α : Type u} [preorder α] {a : α} {b : α} (h : a ≤ b) : is_least (set.Icc a b) a := { left := iff.mpr set.left_mem_Icc h, right := fun (x : α) => and.left } theorem is_glb_Icc {α : Type u} [preorder α] {a : α} {b : α} (h : a ≤ b) : is_glb (set.Icc a b) a := is_least.is_glb (is_least_Icc h) theorem lower_bounds_Icc {α : Type u} [preorder α] {a : α} {b : α} (h : a ≤ b) : lower_bounds (set.Icc a b) = set.Iic a := is_glb.lower_bounds_eq (is_glb_Icc h) theorem is_greatest_Ioc {α : Type u} [preorder α] {a : α} {b : α} (h : a < b) : is_greatest (set.Ioc a b) b := { left := iff.mpr set.right_mem_Ioc h, right := fun (x : α) => and.right } theorem is_lub_Ioc {α : Type u} [preorder α] {a : α} {b : α} (h : a < b) : is_lub (set.Ioc a b) b := is_greatest.is_lub (is_greatest_Ioc h) theorem upper_bounds_Ioc {α : Type u} [preorder α] {a : α} {b : α} (h : a < b) : upper_bounds (set.Ioc a b) = set.Ici b := is_lub.upper_bounds_eq (is_lub_Ioc h) theorem is_least_Ico {α : Type u} [preorder α] {a : α} {b : α} (h : a < b) : is_least (set.Ico a b) a := { left := iff.mpr set.left_mem_Ico h, right := fun (x : α) => and.left } theorem is_glb_Ico {α : Type u} [preorder α] {a : α} {b : α} (h : a < b) : is_glb (set.Ico a b) a := is_least.is_glb (is_least_Ico h) theorem lower_bounds_Ico {α : Type u} [preorder α] {a : α} {b : α} (h : a < b) : lower_bounds (set.Ico a b) = set.Iic a := is_glb.lower_bounds_eq (is_glb_Ico h) theorem is_glb_Ioo {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} {b : γ} (hab : a < b) : is_glb (set.Ioo a b) a := sorry theorem lower_bounds_Ioo {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} {b : γ} (hab : a < b) : lower_bounds (set.Ioo a b) = set.Iic a := is_glb.lower_bounds_eq (is_glb_Ioo hab) theorem is_glb_Ioc {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} {b : γ} (hab : a < b) : is_glb (set.Ioc a b) a := is_glb.of_subset_of_superset (is_glb_Ioo hab) (is_glb_Icc (le_of_lt hab)) set.Ioo_subset_Ioc_self set.Ioc_subset_Icc_self theorem lower_bound_Ioc {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} {b : γ} (hab : a < b) : lower_bounds (set.Ioc a b) = set.Iic a := is_glb.lower_bounds_eq (is_glb_Ioc hab) theorem is_lub_Ioo {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} {b : γ} (hab : a < b) : is_lub (set.Ioo a b) b := sorry theorem upper_bounds_Ioo {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} {b : γ} (hab : a < b) : upper_bounds (set.Ioo a b) = set.Ici b := is_lub.upper_bounds_eq (is_lub_Ioo hab) theorem is_lub_Ico {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} {b : γ} (hab : a < b) : is_lub (set.Ico a b) b := sorry theorem upper_bounds_Ico {γ : Type w} [linear_order γ] [densely_ordered γ] {a : γ} {b : γ} (hab : a < b) : upper_bounds (set.Ico a b) = set.Ici b := is_lub.upper_bounds_eq (is_lub_Ico hab) theorem bdd_below_iff_subset_Ici {α : Type u} [preorder α] {s : set α} : bdd_below s ↔ ∃ (a : α), s ⊆ set.Ici a := iff.rfl theorem bdd_above_iff_subset_Iic {α : Type u} [preorder α] {s : set α} : bdd_above s ↔ ∃ (a : α), s ⊆ set.Iic a := iff.rfl theorem bdd_below_bdd_above_iff_subset_Icc {α : Type u} [preorder α] {s : set α} : bdd_below s ∧ bdd_above s ↔ ∃ (a : α), ∃ (b : α), s ⊆ set.Icc a b := sorry /-! ### Univ -/ theorem order_top.upper_bounds_univ {γ : Type w} [order_top γ] : upper_bounds set.univ = singleton ⊤ := sorry theorem is_greatest_univ {γ : Type w} [order_top γ] : is_greatest set.univ ⊤ := sorry theorem is_lub_univ {γ : Type w} [order_top γ] : is_lub set.univ ⊤ := is_greatest.is_lub is_greatest_univ theorem order_bot.lower_bounds_univ {γ : Type w} [order_bot γ] : lower_bounds set.univ = singleton ⊥ := order_top.upper_bounds_univ theorem is_least_univ {γ : Type w} [order_bot γ] : is_least set.univ ⊥ := is_greatest_univ theorem is_glb_univ {γ : Type w} [order_bot γ] : is_glb set.univ ⊥ := is_least.is_glb is_least_univ theorem no_top_order.upper_bounds_univ {α : Type u} [preorder α] [no_top_order α] : upper_bounds set.univ = ∅ := sorry theorem no_bot_order.lower_bounds_univ {α : Type u} [preorder α] [no_bot_order α] : lower_bounds set.univ = ∅ := no_top_order.upper_bounds_univ /-! ### Empty set -/ @[simp] theorem upper_bounds_empty {α : Type u} [preorder α] : upper_bounds ∅ = set.univ := sorry @[simp] theorem lower_bounds_empty {α : Type u} [preorder α] : lower_bounds ∅ = set.univ := upper_bounds_empty @[simp] theorem bdd_above_empty {α : Type u} [preorder α] [Nonempty α] : bdd_above ∅ := sorry @[simp] theorem bdd_below_empty {α : Type u} [preorder α] [Nonempty α] : bdd_below ∅ := sorry theorem is_glb_empty {γ : Type w} [order_top γ] : is_glb ∅ ⊤ := sorry theorem is_lub_empty {γ : Type w} [order_bot γ] : is_lub ∅ ⊥ := is_glb_empty theorem is_lub.nonempty {α : Type u} [preorder α] {s : set α} {a : α} [no_bot_order α] (hs : is_lub s a) : set.nonempty s := sorry theorem is_glb.nonempty {α : Type u} [preorder α] {s : set α} {a : α} [no_top_order α] (hs : is_glb s a) : set.nonempty s := is_lub.nonempty hs theorem nonempty_of_not_bdd_above {α : Type u} [preorder α] {s : set α} [ha : Nonempty α] (h : ¬bdd_above s) : set.nonempty s := nonempty.elim ha fun (x : α) => Exists.imp (fun (a : α) (ha : ∃ (H : a ∈ s), ¬a ≤ x) => Exists.fst ha) (iff.mp not_bdd_above_iff' h x) theorem nonempty_of_not_bdd_below {α : Type u} [preorder α] {s : set α} [ha : Nonempty α] (h : ¬bdd_below s) : set.nonempty s := nonempty_of_not_bdd_above h /-! ### insert -/ /-- Adding a point to a set preserves its boundedness above. -/ @[simp] theorem bdd_above_insert {γ : Type w} [semilattice_sup γ] (a : γ) {s : set γ} : bdd_above (insert a s) ↔ bdd_above s := sorry theorem bdd_above.insert {γ : Type w} [semilattice_sup γ] (a : γ) {s : set γ} (hs : bdd_above s) : bdd_above (insert a s) := iff.mpr (bdd_above_insert a) hs /--Adding a point to a set preserves its boundedness below.-/ @[simp] theorem bdd_below_insert {γ : Type w} [semilattice_inf γ] (a : γ) {s : set γ} : bdd_below (insert a s) ↔ bdd_below s := sorry theorem bdd_below.insert {γ : Type w} [semilattice_inf γ] (a : γ) {s : set γ} (hs : bdd_below s) : bdd_below (insert a s) := iff.mpr (bdd_below_insert a) hs theorem is_lub.insert {γ : Type w} [semilattice_sup γ] (a : γ) {b : γ} {s : set γ} (hs : is_lub s b) : is_lub (insert a s) (a ⊔ b) := eq.mpr (id (Eq._oldrec (Eq.refl (is_lub (insert a s) (a ⊔ b))) (set.insert_eq a s))) (is_lub.union is_lub_singleton hs) theorem is_glb.insert {γ : Type w} [semilattice_inf γ] (a : γ) {b : γ} {s : set γ} (hs : is_glb s b) : is_glb (insert a s) (a ⊓ b) := eq.mpr (id (Eq._oldrec (Eq.refl (is_glb (insert a s) (a ⊓ b))) (set.insert_eq a s))) (is_glb.union is_glb_singleton hs) theorem is_greatest.insert {γ : Type w} [linear_order γ] (a : γ) {b : γ} {s : set γ} (hs : is_greatest s b) : is_greatest (insert a s) (max a b) := eq.mpr (id (Eq._oldrec (Eq.refl (is_greatest (insert a s) (max a b))) (set.insert_eq a s))) (is_greatest.union is_greatest_singleton hs) theorem is_least.insert {γ : Type w} [linear_order γ] (a : γ) {b : γ} {s : set γ} (hs : is_least s b) : is_least (insert a s) (min a b) := eq.mpr (id (Eq._oldrec (Eq.refl (is_least (insert a s) (min a b))) (set.insert_eq a s))) (is_least.union is_least_singleton hs) @[simp] theorem upper_bounds_insert {α : Type u} [preorder α] (a : α) (s : set α) : upper_bounds (insert a s) = set.Ici a ∩ upper_bounds s := sorry @[simp] theorem lower_bounds_insert {α : Type u} [preorder α] (a : α) (s : set α) : lower_bounds (insert a s) = set.Iic a ∩ lower_bounds s := sorry /-- When there is a global maximum, every set is bounded above. -/ @[simp] protected theorem order_top.bdd_above {γ : Type w} [order_top γ] (s : set γ) : bdd_above s := Exists.intro ⊤ fun (a : γ) (ha : a ∈ s) => order_top.le_top a /-- When there is a global minimum, every set is bounded below. -/ @[simp] protected theorem order_bot.bdd_below {γ : Type w} [order_bot γ] (s : set γ) : bdd_below s := Exists.intro ⊥ fun (a : γ) (ha : a ∈ s) => order_bot.bot_le a /-! ### Pair -/ theorem is_lub_pair {γ : Type w} [semilattice_sup γ] {a : γ} {b : γ} : is_lub (insert a (singleton b)) (a ⊔ b) := is_lub.insert a is_lub_singleton theorem is_glb_pair {γ : Type w} [semilattice_inf γ] {a : γ} {b : γ} : is_glb (insert a (singleton b)) (a ⊓ b) := is_glb.insert a is_glb_singleton theorem is_least_pair {γ : Type w} [linear_order γ] {a : γ} {b : γ} : is_least (insert a (singleton b)) (min a b) := is_least.insert a is_least_singleton theorem is_greatest_pair {γ : Type w} [linear_order γ] {a : γ} {b : γ} : is_greatest (insert a (singleton b)) (max a b) := is_greatest.insert a is_greatest_singleton /-! ### (In)equalities with the least upper bound and the greatest lower bound -/ theorem lower_bounds_le_upper_bounds {α : Type u} [preorder α] {s : set α} {a : α} {b : α} (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds s) : set.nonempty s → a ≤ b := fun (ᾰ : set.nonempty s) => Exists.dcases_on ᾰ fun (ᾰ_w : α) (ᾰ_h : ᾰ_w ∈ s) => idRhs (a ≤ b) (le_trans (ha ᾰ_h) (hb ᾰ_h)) theorem is_glb_le_is_lub {α : Type u} [preorder α] {s : set α} {a : α} {b : α} (ha : is_glb s a) (hb : is_lub s b) (hs : set.nonempty s) : a ≤ b := lower_bounds_le_upper_bounds (and.left ha) (and.left hb) hs theorem is_lub_lt_iff {α : Type u} [preorder α] {s : set α} {a : α} {b : α} (ha : is_lub s a) : a < b ↔ ∃ (c : α), ∃ (H : c ∈ upper_bounds s), c < b := sorry theorem lt_is_glb_iff {α : Type u} [preorder α] {s : set α} {a : α} {b : α} (ha : is_glb s a) : b < a ↔ ∃ (c : α), ∃ (H : c ∈ lower_bounds s), b < c := is_lub_lt_iff ha theorem is_least.unique {α : Type u} [partial_order α] {s : set α} {a : α} {b : α} (Ha : is_least s a) (Hb : is_least s b) : a = b := le_antisymm (and.right Ha b (and.left Hb)) (and.right Hb a (and.left Ha)) theorem is_least.is_least_iff_eq {α : Type u} [partial_order α] {s : set α} {a : α} {b : α} (Ha : is_least s a) : is_least s b ↔ a = b := { mp := is_least.unique Ha, mpr := fun (h : a = b) => h ▸ Ha } theorem is_greatest.unique {α : Type u} [partial_order α] {s : set α} {a : α} {b : α} (Ha : is_greatest s a) (Hb : is_greatest s b) : a = b := le_antisymm (and.right Hb a (and.left Ha)) (and.right Ha b (and.left Hb)) theorem is_greatest.is_greatest_iff_eq {α : Type u} [partial_order α] {s : set α} {a : α} {b : α} (Ha : is_greatest s a) : is_greatest s b ↔ a = b := { mp := is_greatest.unique Ha, mpr := fun (h : a = b) => h ▸ Ha } theorem is_lub.unique {α : Type u} [partial_order α] {s : set α} {a : α} {b : α} (Ha : is_lub s a) (Hb : is_lub s b) : a = b := is_least.unique Ha Hb theorem is_glb.unique {α : Type u} [partial_order α] {s : set α} {a : α} {b : α} (Ha : is_glb s a) (Hb : is_glb s b) : a = b := is_greatest.unique Ha Hb theorem lt_is_lub_iff {α : Type u} [linear_order α] {s : set α} {a : α} {b : α} (h : is_lub s a) : b < a ↔ ∃ (c : α), ∃ (H : c ∈ s), b < c := sorry theorem is_glb_lt_iff {α : Type u} [linear_order α] {s : set α} {a : α} {b : α} (h : is_glb s a) : a < b ↔ ∃ (c : α), ∃ (H : c ∈ s), c < b := lt_is_lub_iff h theorem is_lub.exists_between {α : Type u} [linear_order α] {s : set α} {a : α} {b : α} (h : is_lub s a) (hb : b < a) : ∃ (c : α), ∃ (H : c ∈ s), b < c ∧ c ≤ a := sorry theorem is_lub.exists_between' {α : Type u} [linear_order α] {s : set α} {a : α} {b : α} (h : is_lub s a) (h' : ¬a ∈ s) (hb : b < a) : ∃ (c : α), ∃ (H : c ∈ s), b < c ∧ c < a := sorry theorem is_glb.exists_between {α : Type u} [linear_order α] {s : set α} {a : α} {b : α} (h : is_glb s a) (hb : a < b) : ∃ (c : α), ∃ (H : c ∈ s), a ≤ c ∧ c < b := sorry theorem is_glb.exists_between' {α : Type u} [linear_order α] {s : set α} {a : α} {b : α} (h : is_glb s a) (h' : ¬a ∈ s) (hb : a < b) : ∃ (c : α), ∃ (H : c ∈ s), a < c ∧ c < b := sorry /-! ### Least upper bound and the greatest lower bound in linear ordered additive commutative groups -/ theorem is_glb.exists_between_self_add {α : Type u} [linear_ordered_add_comm_group α] {s : set α} {a : α} {ε : α} (h : is_glb s a) (hε : 0 < ε) : ∃ (b : α), ∃ (H : b ∈ s), a ≤ b ∧ b < a + ε := is_glb.exists_between h (lt_add_of_pos_right a hε) theorem is_glb.exists_between_self_add' {α : Type u} [linear_ordered_add_comm_group α] {s : set α} {a : α} {ε : α} (h : is_glb s a) (h₂ : ¬a ∈ s) (hε : 0 < ε) : ∃ (b : α), ∃ (H : b ∈ s), a < b ∧ b < a + ε := is_glb.exists_between' h h₂ (lt_add_of_pos_right a hε) theorem is_lub.exists_between_sub_self {α : Type u} [linear_ordered_add_comm_group α] {s : set α} {a : α} {ε : α} (h : is_lub s a) (hε : 0 < ε) : ∃ (b : α), ∃ (H : b ∈ s), a - ε < b ∧ b ≤ a := is_lub.exists_between h (sub_lt_self a hε) theorem is_lub.exists_between_sub_self' {α : Type u} [linear_ordered_add_comm_group α] {s : set α} {a : α} {ε : α} (h : is_lub s a) (h₂ : ¬a ∈ s) (hε : 0 < ε) : ∃ (b : α), ∃ (H : b ∈ s), a - ε < b ∧ b < a := is_lub.exists_between' h h₂ (sub_lt_self a hε) /-! ### Images of upper/lower bounds under monotone functions -/ namespace monotone theorem mem_upper_bounds_image {α : Type u} {β : Type v} [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α} (Ha : a ∈ upper_bounds s) : f a ∈ upper_bounds (f '' s) := set.ball_image_of_ball fun (x : α) (H : x ∈ s) => Hf (Ha H) theorem mem_lower_bounds_image {α : Type u} {β : Type v} [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α} (Ha : a ∈ lower_bounds s) : f a ∈ lower_bounds (f '' s) := set.ball_image_of_ball fun (x : α) (H : x ∈ s) => Hf (Ha H) /-- The image under a monotone function of a set which is bounded above is bounded above. -/ theorem map_bdd_above {α : Type u} {β : Type v} [preorder α] [preorder β] {f : α → β} {s : set α} (hf : monotone f) : bdd_above s → bdd_above (f '' s) := sorry /-- The image under a monotone function of a set which is bounded below is bounded below. -/ theorem map_bdd_below {α : Type u} {β : Type v} [preorder α] [preorder β] {f : α → β} {s : set α} (hf : monotone f) : bdd_below s → bdd_below (f '' s) := sorry /-- A monotone map sends a least element of a set to a least element of its image. -/ theorem map_is_least {α : Type u} {β : Type v} [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α} (Ha : is_least s a) : is_least (f '' s) (f a) := { left := set.mem_image_of_mem f (and.left Ha), right := mem_lower_bounds_image Hf (and.right Ha) } /-- A monotone map sends a greatest element of a set to a greatest element of its image. -/ theorem map_is_greatest {α : Type u} {β : Type v} [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α} (Ha : is_greatest s a) : is_greatest (f '' s) (f a) := { left := set.mem_image_of_mem f (and.left Ha), right := mem_upper_bounds_image Hf (and.right Ha) } theorem is_lub_image_le {α : Type u} {β : Type v} [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α} (Ha : is_lub s a) {b : β} (Hb : is_lub (f '' s) b) : b ≤ f a := and.right Hb (f a) (mem_upper_bounds_image Hf (and.left Ha)) theorem le_is_glb_image {α : Type u} {β : Type v} [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α} (Ha : is_glb s a) {b : β} (Hb : is_glb (f '' s) b) : f a ≤ b := and.right Hb (f a) (mem_lower_bounds_image Hf (and.left Ha)) end monotone theorem is_glb.of_image {α : Type u} {β : Type v} [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y : α}, f x ≤ f y ↔ x ≤ y) {s : set α} {x : α} (hx : is_glb (f '' s) (f x)) : is_glb s x := sorry theorem is_lub.of_image {α : Type u} {β : Type v} [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y : α}, f x ≤ f y ↔ x ≤ y) {s : set α} {x : α} (hx : is_lub (f '' s) (f x)) : is_lub s x := is_glb.of_image (fun (x y : order_dual α) => hf) hx
fcc695485f3edfcbf496182c3e5e8e42db2ddabc
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/data/polynomial/algebra_map.lean
22dfaa3576377cd5d0b9b446e87aed9f99373770
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,649
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 algebra.algebra.tower /-! # Theory of univariate polynomials We show that `polynomial A` is an R-algebra when `A` is an R-algebra. We promote `eval₂` to an algebra hom in `aeval`. -/ noncomputable theory open finset open_locale big_operators namespace polynomial universes u v w z variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {a b : R} {n : ℕ} section comm_semiring variables [comm_semiring R] {p q r : polynomial R} variables [semiring A] [algebra R A] /-- Note that this instance also provides `algebra R (polynomial R)`. -/ instance algebra_of_algebra : algebra R (polynomial A) := add_monoid_algebra.algebra lemma algebra_map_apply (r : R) : algebra_map R (polynomial A) r = C (algebra_map R A r) := rfl /-- When we have `[comm_ring R]`, the function `C` is the same as `algebra_map R (polynomial R)`. (But note that `C` is defined when `R` is not necessarily commutative, in which case `algebra_map` is not available.) -/ lemma C_eq_algebra_map {R : Type*} [comm_ring R] (r : R) : C r = algebra_map R (polynomial R) r := rfl @[simp] lemma alg_hom_eval₂_algebra_map {R A B : Type*} [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] (p : polynomial R) (f : A →ₐ[R] B) (a : A) : f (eval₂ (algebra_map R A) a p) = eval₂ (algebra_map R B) (f a) p := begin dsimp [eval₂, finsupp.sum], simp only [f.map_sum, f.map_mul, f.map_pow, ring_hom.eq_int_cast, ring_hom.map_int_cast, alg_hom.commutes], end @[simp] lemma eval₂_algebra_map_X {R A : Type*} [comm_ring R] [ring A] [algebra R A] (p : polynomial R) (f : polynomial R →ₐ[R] A) : eval₂ (algebra_map R A) (f X) p = f p := begin conv_rhs { rw [←polynomial.sum_C_mul_X_eq p], }, dsimp [eval₂, finsupp.sum], simp only [f.map_sum, f.map_mul, f.map_pow, ring_hom.eq_int_cast, ring_hom.map_int_cast], simp [polynomial.C_eq_algebra_map], end @[simp] lemma ring_hom_eval₂_algebra_map_int {R S : Type*} [ring R] [ring S] (p : polynomial ℤ) (f : R →+* S) (r : R) : f (eval₂ (algebra_map ℤ R) r p) = eval₂ (algebra_map ℤ S) (f r) p := alg_hom_eval₂_algebra_map p f.to_int_alg_hom r @[simp] lemma eval₂_algebra_map_int_X {R : Type*} [ring R] (p : polynomial ℤ) (f : polynomial ℤ →+* R) : eval₂ (algebra_map ℤ R) (f X) p = f p := -- Unfortunately `f.to_int_alg_hom` doesn't work here, as typeclasses don't match up correctly. eval₂_algebra_map_X p { commutes' := λ n, by simp, .. f } section comp lemma eval₂_comp [comm_semiring S] (f : R →+* S) {x : S} : eval₂ f x (p.comp q) = eval₂ f (eval₂ f x q) p := by rw [comp, p.as_sum_range]; simp [eval₂_finset_sum, eval₂_pow] lemma eval_comp : (p.comp q).eval a = p.eval (q.eval a) := eval₂_comp _ instance comp.is_semiring_hom : is_semiring_hom (λ q : polynomial R, q.comp p) := by unfold comp; apply_instance end comp end comm_semiring section aeval variables [comm_semiring R] {p q : polynomial R} variables [semiring A] [algebra R A] variables {B : Type*} [semiring B] [algebra R B] variables (x : A) /-- Given a valuation `x` of the variable in an `R`-algebra `A`, `aeval R A x` is the unique `R`-algebra homomorphism from `R[X]` to `A` sending `X` to `x`. -/ def aeval : polynomial R →ₐ[R] A := { commutes' := λ r, eval₂_C _ _, ..eval₂_ring_hom' (algebra_map R A) x (λ a, algebra.commutes _ _) } variables {R A} @[ext] lemma alg_hom_ext {f g : polynomial R →ₐ[R] A} (h : f X = g X) : f = g := by { ext, exact h } theorem aeval_def (p : polynomial R) : aeval x p = eval₂ (algebra_map R A) x p := rfl @[simp] lemma aeval_zero : aeval x (0 : polynomial R) = 0 := alg_hom.map_zero (aeval x) @[simp] lemma aeval_X : aeval x (X : polynomial R) = x := eval₂_X _ x @[simp] lemma aeval_C (r : R) : aeval x (C r) = algebra_map R A r := eval₂_C _ x lemma aeval_monomial {n : ℕ} {r : R} : aeval x (monomial n r) = (algebra_map _ _ r) * x^n := eval₂_monomial _ _ @[simp] lemma aeval_X_pow {n : ℕ} : aeval x ((X : polynomial R)^n) = x^n := eval₂_X_pow _ _ @[simp] lemma aeval_add : aeval x (p + q) = aeval x p + aeval x q := alg_hom.map_add _ _ _ @[simp] lemma aeval_one : aeval x (1 : polynomial R) = 1 := alg_hom.map_one _ @[simp] lemma aeval_bit0 : aeval x (bit0 p) = bit0 (aeval x p) := alg_hom.map_bit0 _ _ @[simp] lemma aeval_bit1 : aeval x (bit1 p) = bit1 (aeval x p) := alg_hom.map_bit1 _ _ @[simp] lemma aeval_nat_cast (n : ℕ) : aeval x (n : polynomial R) = n := alg_hom.map_nat_cast _ _ lemma aeval_mul : aeval x (p * q) = aeval x p * aeval x q := alg_hom.map_mul _ _ _ lemma aeval_comp {A : Type*} [comm_semiring A] [algebra R A] (x : A) : aeval x (p.comp q) = (aeval (aeval x q) p) := eval₂_comp (algebra_map R A) @[simp] lemma aeval_map {A : Type*} [comm_semiring A] [algebra R A] [algebra A B] [is_scalar_tower R A B] (b : B) (p : polynomial R) : aeval b (p.map (algebra_map R A)) = aeval b p := by rw [aeval_def, eval₂_map, ←is_scalar_tower.algebra_map_eq, ←aeval_def] theorem eval_unique (φ : polynomial R →ₐ[R] A) (p) : φ p = eval₂ (algebra_map R A) (φ X) p := begin apply polynomial.induction_on p, { intro r, rw eval₂_C, exact φ.commutes r }, { intros f g ih1 ih2, rw [φ.map_add, ih1, ih2, eval₂_add] }, { intros n r ih, rw [pow_succ', ← mul_assoc, φ.map_mul, eval₂_mul_noncomm (algebra_map R A) _ (λ k, algebra.commutes _ _), eval₂_X, ih] } end theorem aeval_alg_hom (f : A →ₐ[R] B) (x : A) : aeval (f x) = f.comp (aeval x) := alg_hom.ext $ λ p, by rw [eval_unique (f.comp (aeval x)), alg_hom.comp_apply, aeval_X, aeval_def] theorem aeval_alg_hom_apply (f : A →ₐ[R] B) (x : A) (p : polynomial R) : aeval (f x) p = f (aeval x p) := alg_hom.ext_iff.1 (aeval_alg_hom f x) p @[simp] lemma coe_aeval_eq_eval (r : R) : (aeval r : polynomial R → R) = eval r := rfl lemma coeff_zero_eq_aeval_zero (p : polynomial R) : p.coeff 0 = aeval 0 p := by simp [coeff_zero_eq_eval_zero] lemma pow_comp (p q : polynomial R) (k : ℕ) : (p ^ k).comp q = (p.comp q) ^ k := by { unfold comp, rw ← coe_eval₂_ring_hom, apply ring_hom.map_pow } variables [comm_ring S] {f : R →+* S} lemma is_root_of_eval₂_map_eq_zero (hf : function.injective f) {r : R} : eval₂ f (f r) p = 0 → p.is_root r := begin intro h, apply hf, rw [←eval₂_hom, h, f.map_zero], end lemma is_root_of_aeval_algebra_map_eq_zero [algebra R S] {p : polynomial R} (inj : function.injective (algebra_map R S)) {r : R} (hr : aeval (algebra_map R S r) p = 0) : p.is_root r := is_root_of_eval₂_map_eq_zero inj hr lemma dvd_term_of_dvd_eval_of_dvd_terms {z p : S} {f : polynomial S} (i : ℕ) (dvd_eval : p ∣ f.eval z) (dvd_terms : ∀ (j ≠ i), p ∣ f.coeff j * z ^ j) : p ∣ f.coeff i * z ^ i := begin by_cases hf : f = 0, { simp [hf] }, by_cases hi : i ∈ f.support, { unfold polynomial.eval polynomial.eval₂ finsupp.sum id at dvd_eval, rw [←finset.insert_erase hi, finset.sum_insert (finset.not_mem_erase _ _)] at dvd_eval, refine (dvd_add_left _).mp dvd_eval, apply finset.dvd_sum, intros j hj, exact dvd_terms j (finset.ne_of_mem_erase hj) }, { convert dvd_zero p, convert _root_.zero_mul _, exact finsupp.not_mem_support_iff.mp hi } end lemma dvd_term_of_is_root_of_dvd_terms {r p : S} {f : polynomial S} (i : ℕ) (hr : f.is_root r) (h : ∀ (j ≠ i), p ∣ f.coeff j * r ^ j) : p ∣ f.coeff i * r ^ i := dvd_term_of_dvd_eval_of_dvd_terms i (eq.symm hr ▸ dvd_zero p) h lemma aeval_eq_sum_range [algebra R S] {p : polynomial R} (x : S) : aeval x p = ∑ i in finset.range (p.nat_degree + 1), p.coeff i • x ^ i := by { simp_rw algebra.smul_def, exact eval₂_eq_sum_range (algebra_map R S) x } lemma aeval_eq_sum_range' [algebra R S] {p : polynomial R} {n : ℕ} (hn : p.nat_degree < n) (x : S) : aeval x p = ∑ i in finset.range n, p.coeff i • x ^ i := by { simp_rw algebra.smul_def, exact eval₂_eq_sum_range' (algebra_map R S) hn x } end aeval section ring variables [ring R] /-- The evaluation map is not generally multiplicative when the coefficient ring is noncommutative, but nevertheless any polynomial of the form `p * (X - monomial 0 r)` is sent to zero when evaluated at `r`. This is the key step in our proof of the Cayley-Hamilton theorem. -/ lemma eval_mul_X_sub_C {p : polynomial R} (r : R) : (p * (X - C r)).eval r = 0 := begin simp only [eval, eval₂, ring_hom.id_apply], have bound := calc (p * (X - C r)).nat_degree ≤ p.nat_degree + (X - C r).nat_degree : nat_degree_mul_le ... ≤ p.nat_degree + 1 : add_le_add_left nat_degree_X_sub_C_le _ ... < p.nat_degree + 2 : lt_add_one _, rw sum_over_range' _ _ (p.nat_degree + 2) bound, swap, { simp, }, rw sum_range_succ', conv_lhs { congr, apply_congr, skip, rw [coeff_mul_X_sub_C, sub_mul, mul_assoc, ←pow_succ], }, simp [sum_range_sub', coeff_monomial], end theorem not_is_unit_X_sub_C [nontrivial R] {r : R} : ¬ is_unit (X - C r) := λ ⟨⟨_, g, hfg, hgf⟩, rfl⟩, @zero_ne_one R _ _ $ by erw [← eval_mul_X_sub_C, hgf, eval_one] end ring lemma aeval_endomorphism {M : Type*} [comm_ring R] [add_comm_group M] [module R M] (f : M →ₗ[R] M) (v : M) (p : polynomial R) : aeval f p v = p.sum (λ n b, b • (f ^ n) v) := begin rw [aeval_def, eval₂], exact (finset.sum_hom p.support (λ h : M →ₗ[R] M, h v)).symm end end polynomial
4398c4afbce12cb29f5fcd9044a57bb521588890
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/rbtree/insert.lean
f2ec9570eab081ce36d2362baa038da3faaad1ca
[ "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
33,912
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import data.rbtree.find universes u v local attribute [simp] rbnode.lift namespace rbnode variables {α : Type u} open color @[simp] lemma balance1_eq₁ (l : rbnode α) (x r₁ y r₂ v t) : balance1 (red_node l x r₁) y r₂ v t = red_node (black_node l x r₁) y (black_node r₂ v t) := begin cases r₂; refl end @[simp] lemma balance1_eq₂ (l₁ : rbnode α) (y l₂ x r v t) : get_color l₁ ≠ red → balance1 l₁ y (red_node l₂ x r) v t = red_node (black_node l₁ y l₂) x (black_node r v t) := begin cases l₁; simp [get_color, balance1, false_implies_iff] end @[simp] lemma balance1_eq₃ (l : rbnode α) (y r v t) : get_color l ≠ red → get_color r ≠ red → balance1 l y r v t = black_node (red_node l y r) v t := begin cases l; cases r; simp [get_color, balance1, false_implies_iff] end @[simp] lemma balance2_eq₁ (l : rbnode α) (x₁ r₁ y r₂ v t) : balance2 (red_node l x₁ r₁) y r₂ v t = red_node (black_node t v l) x₁ (black_node r₁ y r₂) := by cases r₂; refl @[simp] lemma balance2_eq₂ (l₁ : rbnode α) (y l₂ x₂ r₂ v t) : get_color l₁ ≠ red → balance2 l₁ y (red_node l₂ x₂ r₂) v t = red_node (black_node t v l₁) y (black_node l₂ x₂ r₂) := begin cases l₁; simp [get_color, balance2, false_implies_iff] end @[simp] lemma balance2_eq₃ (l : rbnode α) (y r v t) : get_color l ≠ red → get_color r ≠ red → balance2 l y r v t = black_node t v (red_node l y r) := begin cases l; cases r; simp [get_color, balance2, false_implies_iff] end /- We can use the same induction principle for balance1 and balance2 -/ lemma balance.cases {p : rbnode α → α → rbnode α → Prop} (l y r) (red_left : ∀ l x r₁ y r₂, p (red_node l x r₁) y r₂) (red_right : ∀ l₁ y l₂ x r, get_color l₁ ≠ red → p l₁ y (red_node l₂ x r)) (other : ∀ l y r, get_color l ≠ red → get_color r ≠ red → p l y r) : p l y r := begin cases l; cases r, any_goals { apply red_left }, any_goals { apply red_right; simp [get_color]; contradiction; done }, any_goals { apply other; simp [get_color]; contradiction; done }, end lemma balance1_ne_leaf (l : rbnode α) (x r v t) : balance1 l x r v t ≠ leaf := by apply balance.cases l x r; intros; simp [*]; contradiction lemma balance1_node_ne_leaf {s : rbnode α} (a : α) (t : rbnode α) : s ≠ leaf → balance1_node s a t ≠ leaf := begin intro h, cases s, { contradiction }, all_goals { simp [balance1_node], apply balance1_ne_leaf } end lemma balance2_ne_leaf (l : rbnode α) (x r v t) : balance2 l x r v t ≠ leaf := by apply balance.cases l x r; intros; simp [*]; contradiction lemma balance2_node_ne_leaf {s : rbnode α} (a : α) (t : rbnode α) : s ≠ leaf → balance2_node s a t ≠ leaf := begin intro h, cases s, { contradiction }, all_goals { simp [balance2_node], apply balance2_ne_leaf } end variables (lt : α → α → Prop) @[elab_as_eliminator] lemma ins.induction [decidable_rel lt] {p : rbnode α → Prop} (t x) (is_leaf : p leaf) (is_red_lt : ∀ a y b (hc : cmp_using lt x y = ordering.lt) (ih : p a), p (red_node a y b)) (is_red_eq : ∀ a y b (hc : cmp_using lt x y = ordering.eq), p (red_node a y b)) (is_red_gt : ∀ a y b (hc : cmp_using lt x y = ordering.gt) (ih : p b), p (red_node a y b)) (is_black_lt_red : ∀ a y b (hc : cmp_using lt x y = ordering.lt) (hr : get_color a = red) (ih : p a), p (black_node a y b)) (is_black_lt_not_red : ∀ a y b (hc : cmp_using lt x y = ordering.lt) (hnr : get_color a ≠ red) (ih : p a), p (black_node a y b)) (is_black_eq : ∀ a y b (hc : cmp_using lt x y = ordering.eq), p (black_node a y b)) (is_black_gt_red : ∀ a y b (hc : cmp_using lt x y = ordering.gt) (hr : get_color b = red) (ih : p b), p (black_node a y b)) (is_black_gt_not_red : ∀ a y b (hc : cmp_using lt x y = ordering.gt) (hnr : get_color b ≠ red) (ih : p b), p (black_node a y b)) : p t := begin induction t, case leaf { apply is_leaf }, case red_node : a y b { cases h : cmp_using lt x y, case ordering.lt { apply is_red_lt; assumption }, case ordering.eq { apply is_red_eq; assumption }, case ordering.gt { apply is_red_gt; assumption }, }, case black_node : a y b { cases h : cmp_using lt x y, case ordering.lt { by_cases get_color a = red, { apply is_black_lt_red; assumption }, { apply is_black_lt_not_red; assumption }, }, case ordering.eq { apply is_black_eq; assumption }, case ordering.gt { by_cases get_color b = red, { apply is_black_gt_red; assumption }, { apply is_black_gt_not_red; assumption }, } } end lemma is_searchable_balance1 {l y r v t lo hi} : is_searchable lt l lo (some y) → is_searchable lt r (some y) (some v) → is_searchable lt t (some v) hi → is_searchable lt (balance1 l y r v t) lo hi := by apply balance.cases l y r; intros; simp [*]; is_searchable_tactic lemma is_searchable_balance1_node {t} [is_trans α lt] : ∀ {y s lo hi}, is_searchable lt t lo (some y) → is_searchable lt s (some y) hi → is_searchable lt (balance1_node t y s) lo hi := begin cases t; simp!; intros; is_searchable_tactic, { cases lo, { apply is_searchable_none_low_of_is_searchable_some_low, assumption }, { simp at *, apply is_searchable_some_low_of_is_searchable_of_lt; assumption } }, all_goals { apply is_searchable_balance1; assumption } end lemma is_searchable_balance2 {l y r v t lo hi} : is_searchable lt t lo (some v) → is_searchable lt l (some v) (some y) → is_searchable lt r (some y) hi → is_searchable lt (balance2 l y r v t) lo hi := by apply balance.cases l y r; intros; simp [*]; is_searchable_tactic lemma is_searchable_balance2_node {t} [is_trans α lt] : ∀ {y s lo hi}, is_searchable lt s lo (some y) → is_searchable lt t (some y) hi → is_searchable lt (balance2_node t y s) lo hi := begin induction t; simp!; intros; is_searchable_tactic, { cases hi, { apply is_searchable_none_high_of_is_searchable_some_high, assumption }, { simp at *, apply is_searchable_some_high_of_is_searchable_of_lt, assumption' } }, all_goals { apply is_searchable_balance2, assumption' } end lemma is_searchable_ins [decidable_rel lt] {t x} [is_strict_weak_order α lt] : ∀ {lo hi} (h : is_searchable lt t lo hi), lift lt lo (some x) → lift lt (some x) hi → is_searchable lt (ins lt t x) lo hi := begin with_cases { apply ins.induction lt t x; intros; simp! [*] at * {eta := ff}; is_searchable_tactic }, case is_red_lt hs₁ { apply ih h_hs₁, assumption, simp [*] }, case is_red_eq hs₁ { apply is_searchable_of_is_searchable_of_incomp hc, assumption }, case is_red_eq hs₂ { apply is_searchable_of_incomp_of_is_searchable hc, assumption }, case is_red_gt hs₂ { apply ih h_hs₂, cases hi; simp [*], assumption }, case is_black_lt_red { apply is_searchable_balance1_node, apply ih h_hs₁, assumption, simp [*], assumption }, case is_black_lt_not_red hs₁ { apply ih h_hs₁, assumption, simp [*] }, case is_black_eq hs₁ { apply is_searchable_of_is_searchable_of_incomp hc, assumption }, case is_black_eq hs₂ { apply is_searchable_of_incomp_of_is_searchable hc, assumption }, case is_black_gt_red { apply is_searchable_balance2_node, assumption, apply ih h_hs₂, simp [*], assumption }, case is_black_gt_not_red hs₂ { apply ih h_hs₂, assumption, simp [*] } end lemma is_searchable_mk_insert_result {c t} : is_searchable lt t none none → is_searchable lt (mk_insert_result c t) none none := begin classical, cases c; cases t; simp [mk_insert_result], { intro h, is_searchable_tactic } end lemma is_searchable_insert [decidable_rel lt] {t x} [is_strict_weak_order α lt] : is_searchable lt t none none → is_searchable lt (insert lt t x) none none := begin intro h, simp [insert], apply is_searchable_mk_insert_result, apply is_searchable_ins; { assumption <|> simp } end end rbnode namespace rbnode section membership_lemmas parameters {α : Type u} (lt : α → α → Prop) local attribute [simp] mem balance1_node balance2_node local infix `∈` := mem lt lemma mem_balance1_node_of_mem_left {x s} (v) (t : rbnode α) : x ∈ s → x ∈ balance1_node s v t := begin cases s; simp [false_implies_iff], all_goals { apply balance.cases s_lchild s_val s_rchild; intros; simp at *; blast_disjs; simp [*] } end lemma mem_balance2_node_of_mem_left {x s} (v) (t : rbnode α) : x ∈ s → x ∈ balance2_node s v t := begin cases s; simp [false_implies_iff], all_goals { apply balance.cases s_lchild s_val s_rchild; intros; simp at *; blast_disjs; simp [*] } end lemma mem_balance1_node_of_mem_right {x t} (v) (s : rbnode α) : x ∈ t → x ∈ balance1_node s v t := begin intros, cases s; simp [*], all_goals { apply balance.cases s_lchild s_val s_rchild; intros; simp [*] } end lemma mem_balance2_node_of_mem_right {x t} (v) (s : rbnode α) : x ∈ t → x ∈ balance2_node s v t := begin intros, cases s; simp [*], all_goals { apply balance.cases s_lchild s_val s_rchild; intros; simp [*] } end lemma mem_balance1_node_of_incomp {x v} (s t) : (¬ lt x v ∧ ¬ lt v x) → s ≠ leaf → x ∈ balance1_node s v t := begin intros, cases s; simp, { contradiction }, all_goals { apply balance.cases s_lchild s_val s_rchild; intros; simp [*] } end lemma mem_balance2_node_of_incomp {x v} (s t) : (¬ lt v x ∧ ¬ lt x v) → s ≠ leaf → x ∈ balance2_node s v t := begin intros, cases s; simp, { contradiction }, all_goals { apply balance.cases s_lchild s_val s_rchild; intros; simp [*] } end lemma ins_ne_leaf [decidable_rel lt] (t : rbnode α) (x : α) : t.ins lt x ≠ leaf := begin apply ins.induction lt t x, any_goals { intros, simp [ins, *] }, { intros, apply balance1_node_ne_leaf, assumption }, { intros, apply balance2_node_ne_leaf, assumption }, end lemma insert_ne_leaf [decidable_rel lt] (t : rbnode α) (x : α) : insert lt t x ≠ leaf := begin simp [insert], cases he : ins lt t x; cases get_color t; simp [mk_insert_result], { have := ins_ne_leaf lt t x, contradiction }, { exact absurd he (ins_ne_leaf _ _ _) } end lemma mem_ins_of_incomp [decidable_rel lt] (t : rbnode α) {x y : α} : ∀ h : ¬ lt x y ∧ ¬ lt y x, x ∈ t.ins lt y := begin with_cases { apply ins.induction lt t y; intros; simp [ins, *] }, case is_black_lt_red { have := ih h, apply mem_balance1_node_of_mem_left, assumption }, case is_black_gt_red { have := ih h, apply mem_balance2_node_of_mem_left, assumption } end lemma mem_ins_of_mem [decidable_rel lt] [is_strict_weak_order α lt] {t : rbnode α} (z : α) : ∀ {x} (h : x ∈ t), x ∈ t.ins lt z := begin with_cases { apply ins.induction lt t z; intros; simp [ins, *] at *; try { contradiction }; blast_disjs }, case is_red_eq or.inr or.inl { have := incomp_trans_of lt h ⟨hc.2, hc.1⟩, simp [this] }, case is_black_lt_red or.inl { apply mem_balance1_node_of_mem_left, apply ih h }, case is_black_lt_red or.inr or.inl { apply mem_balance1_node_of_incomp, cases h, all_goals { simp [*, ins_ne_leaf lt a z] } }, case is_black_lt_red or.inr or.inr { apply mem_balance1_node_of_mem_right, assumption }, case is_black_eq or.inr or.inl { have := incomp_trans_of lt hc ⟨h.2, h.1⟩, simp [this] }, case is_black_gt_red or.inl { apply mem_balance2_node_of_mem_right, assumption }, case is_black_gt_red or.inr or.inl { have := ins_ne_leaf lt a z, apply mem_balance2_node_of_incomp, cases h, simp [*], apply ins_ne_leaf }, case is_black_gt_red or.inr or.inr { apply mem_balance2_node_of_mem_left, apply ih h }, -- remaining cases are easy any_goals { intros, simp [h], done }, all_goals { intros, simp [ih h], done }, end lemma mem_mk_insert_result {a t} (c) : mem lt a t → mem lt a (mk_insert_result c t) := by intros; cases c; cases t; simp [mk_insert_result, mem, *] at * lemma mem_of_mem_mk_insert_result {a t c} : mem lt a (mk_insert_result c t) → mem lt a t := by cases t; cases c; simp [mk_insert_result, mem]; intros; assumption lemma mem_insert_of_incomp [decidable_rel lt] (t : rbnode α) {x y : α} : ∀ h : ¬ lt x y ∧ ¬ lt y x, x ∈ t.insert lt y := by intros; unfold insert; apply mem_mk_insert_result; apply mem_ins_of_incomp; assumption lemma mem_insert_of_mem [decidable_rel lt] [is_strict_weak_order α lt] {t x} (z) : x ∈ t → x ∈ t.insert lt z := by intros; apply mem_mk_insert_result; apply mem_ins_of_mem; assumption lemma of_mem_balance1_node {x s v t} : x ∈ balance1_node s v t → x ∈ s ∨ (¬ lt x v ∧ ¬ lt v x) ∨ x ∈ t := begin cases s; simp, { intros, simp [*] }, all_goals { apply balance.cases s_lchild s_val s_rchild; intros; simp [*] at *; blast_disjs; simp [*] } end lemma of_mem_balance2_node {x s v t} : x ∈ balance2_node s v t → x ∈ s ∨ (¬ lt x v ∧ ¬ lt v x) ∨ x ∈ t := begin cases s; simp, { intros, simp [*] }, all_goals { apply balance.cases s_lchild s_val s_rchild; intros; simp [*] at *; blast_disjs; simp [*] } end lemma equiv_or_mem_of_mem_ins [decidable_rel lt] [is_strict_weak_order α lt] {t : rbnode α} {x z} : ∀ (h : x ∈ t.ins lt z), x ≈[lt] z ∨ x ∈ t := begin with_cases { apply ins.induction lt t z; intros; simp [ins, strict_weak_order.equiv, *] at *; blast_disjs }, case is_black_lt_red { have h' := of_mem_balance1_node lt h, blast_disjs, have := ih h', blast_disjs, all_goals { simp [h, *] } }, case is_black_gt_red { have h' := of_mem_balance2_node lt h, blast_disjs, have := ih h', blast_disjs, all_goals { simp [h, *] }}, -- All other goals can be solved by the following tactics any_goals { intros, simp [h] }, all_goals { intros, have ih := ih h, cases ih; simp [*], done }, end lemma equiv_or_mem_of_mem_insert [decidable_rel lt] [is_strict_weak_order α lt] {t : rbnode α} {x z} : ∀ (h : x ∈ t.insert lt z), x ≈[lt] z ∨ x ∈ t := begin simp [insert], intros, apply equiv_or_mem_of_mem_ins, exact mem_of_mem_mk_insert_result lt h end local attribute [simp] mem_exact lemma mem_exact_balance1_node_of_mem_exact {x s} (v) (t : rbnode α) : mem_exact x s → mem_exact x (balance1_node s v t) := begin cases s; simp [false_implies_iff], all_goals { apply balance.cases s_lchild s_val s_rchild; intros; simp [*] at *; blast_disjs; simp [*] } end lemma mem_exact_balance2_node_of_mem_exact {x s} (v) (t : rbnode α) : mem_exact x s → mem_exact x (balance2_node s v t) := begin cases s; simp [false_implies_iff], all_goals { apply balance.cases s_lchild s_val s_rchild; intros; simp [*] at *; blast_disjs; simp [*] } end lemma find_balance1_node [decidable_rel lt] [is_strict_weak_order α lt] {x y z t s} : ∀ {lo hi}, is_searchable lt t lo (some z) → is_searchable lt s (some z) hi → find lt t y = some x → y ≈[lt] x → find lt (balance1_node t z s) y = some x := begin intros _ _ hs₁ hs₂ heq heqv, have hs := is_searchable_balance1_node lt hs₁ hs₂, have := eq.trans (find_eq_find_of_eqv hs₁ heqv.symm) heq, have := iff.mpr (find_correct_exact hs₁) this, have := mem_exact_balance1_node_of_mem_exact z s this, have := iff.mp (find_correct_exact hs) this, exact eq.trans (find_eq_find_of_eqv hs heqv) this end lemma find_balance2_node [decidable_rel lt] [is_strict_weak_order α lt] {x y z s t} [is_trans α lt] : ∀ {lo hi}, is_searchable lt s lo (some z) → is_searchable lt t (some z) hi → find lt t y = some x → y ≈[lt] x → find lt (balance2_node t z s) y = some x := begin intros _ _ hs₁ hs₂ heq heqv, have hs := is_searchable_balance2_node lt hs₁ hs₂, have := eq.trans (find_eq_find_of_eqv hs₂ heqv.symm) heq, have := iff.mpr (find_correct_exact hs₂) this, have := mem_exact_balance2_node_of_mem_exact z s this, have := iff.mp (find_correct_exact hs) this, exact eq.trans (find_eq_find_of_eqv hs heqv) this end /- Auxiliary lemma -/ lemma ite_eq_of_not_lt [decidable_rel lt] [is_strict_order α lt] {a b} {β : Type v} (t s : β) (h : lt b a) : (if lt a b then t else s) = s := begin have := not_lt_of_lt h, simp [*] end local attribute [simp] ite_eq_of_not_lt private meta def simp_fi : tactic unit := `[simp [find, ins, *, cmp_using]] lemma find_ins_of_eqv [decidable_rel lt] [is_strict_weak_order α lt] {x y : α} {t : rbnode α} (he : x ≈[lt] y) : ∀ {lo hi} (hs : is_searchable lt t lo hi) (hlt₁ : lift lt lo (some x)) (hlt₂ : lift lt (some x) hi), find lt (ins lt t x) y = some x := begin simp [strict_weak_order.equiv] at he, apply ins.induction lt t x; intros, { simp_fi }, all_goals { simp at hc, cases hs }, { have := lt_of_incomp_of_lt he.swap hc, have := ih hs_hs₁ hlt₁ hc, simp_fi }, { simp_fi }, { have := lt_of_lt_of_incomp hc he, have := ih hs_hs₂ hc hlt₂, simp_fi }, { simp_fi, have := is_searchable_ins lt hs_hs₁ hlt₁ hc, apply find_balance1_node lt this hs_hs₂ (ih hs_hs₁ hlt₁ hc) he.symm }, { have := lt_of_incomp_of_lt he.swap hc, have := ih hs_hs₁ hlt₁ hc, simp_fi }, { simp_fi }, { simp_fi, have := is_searchable_ins lt hs_hs₂ hc hlt₂, apply find_balance2_node lt hs_hs₁ this (ih hs_hs₂ hc hlt₂) he.symm }, { have := lt_of_lt_of_incomp hc he, have := ih hs_hs₂ hc hlt₂, simp_fi } end lemma find_mk_insert_result [decidable_rel lt] (c : color) (t : rbnode α) (x : α) : find lt (mk_insert_result c t) x = find lt t x := begin cases t; cases c; simp [mk_insert_result], { simp [find], cases cmp_using lt x t_val; simp [find] } end lemma find_insert_of_eqv [decidable_rel lt] [is_strict_weak_order α lt] {x y : α} {t : rbnode α} (he : x ≈[lt] y) : is_searchable lt t none none → find lt (insert lt t x) y = some x := begin intro hs, simp [insert, find_mk_insert_result], apply find_ins_of_eqv lt he hs; simp end lemma weak_trichotomous (x y) {p : Prop} (is_lt : ∀ h : lt x y, p) (is_eqv : ∀ h : ¬ lt x y ∧ ¬ lt y x, p) (is_gt : ∀ h : lt y x, p) : p := begin by_cases lt x y, { apply is_lt, assumption }, by_cases lt y x, { apply is_gt, assumption }, { apply is_eqv, constructor; assumption } end section find_ins_of_not_eqv section simp_aux_lemmas lemma find_black_eq_find_red [decidable_rel lt] {l y r x} : find lt (black_node l y r) x = find lt (red_node l y r) x := begin simp [find], all_goals { cases cmp_using lt x y; simp [find] } end lemma find_red_of_lt [decidable_rel lt] {l y r x} (h : lt x y) : find lt (red_node l y r) x = find lt l x := by simp [find, cmp_using, *] lemma find_red_of_gt [decidable_rel lt] [is_strict_order α lt] {l y r x} (h : lt y x) : find lt (red_node l y r) x = find lt r x := begin have := not_lt_of_lt h, simp [find, cmp_using, *] end lemma find_red_of_incomp [decidable_rel lt] {l y r x} (h : ¬ lt x y ∧ ¬ lt y x) : find lt (red_node l y r) x = some y := by simp [find, cmp_using, *] end simp_aux_lemmas local attribute [simp] find_black_eq_find_red find_red_of_lt find_red_of_lt find_red_of_gt find_red_of_incomp variables [is_strict_weak_order α lt] [decidable_rel lt] lemma find_balance1_lt {l r t v x y lo hi} (h : lt x y) (hl : is_searchable lt l lo (some v)) (hr : is_searchable lt r (some v) (some y)) (ht : is_searchable lt t (some y) hi) : find lt (balance1 l v r y t) x = find lt (red_node l v r) x := begin with_cases { revert hl hr ht, apply balance.cases l v r; intros; simp [*]; is_searchable_tactic }, case red_left : _ _ _ z r { apply weak_trichotomous lt z x; intros; simp [*] }, case red_right : l_left l_val l_right z r { with_cases { apply weak_trichotomous lt z x; intro h' }, case is_lt { have := trans_of lt (lo_lt_hi hr_hs₁) h', simp [*] }, case is_eqv { have : lt l_val x := lt_of_lt_of_incomp (lo_lt_hi hr_hs₁) h', simp [*] }, case is_gt { apply weak_trichotomous lt l_val x; intros; simp [*] } } end meta def ins_ne_leaf_tac := `[apply ins_ne_leaf] lemma find_balance1_node_lt {t s x y lo hi} (hlt : lt y x) (ht : is_searchable lt t lo (some x)) (hs : is_searchable lt s (some x) hi) (hne : t ≠ leaf . ins_ne_leaf_tac) : find lt (balance1_node t x s) y = find lt t y := begin cases t; simp [balance1_node], { contradiction }, all_goals { intros, is_searchable_tactic, apply find_balance1_lt, assumption' } end lemma find_balance1_gt {l r t v x y lo hi} (h : lt y x) (hl : is_searchable lt l lo (some v)) (hr : is_searchable lt r (some v) (some y)) (ht : is_searchable lt t (some y) hi) : find lt (balance1 l v r y t) x = find lt t x := begin with_cases { revert hl hr ht, apply balance.cases l v r; intros; simp [*]; is_searchable_tactic }, case red_left : _ _ _ z { have := trans_of lt (lo_lt_hi hr) h, simp [*] }, case red_right : _ _ _ z { have := trans_of lt (lo_lt_hi hr_hs₂) h, simp [*] } end lemma find_balance1_node_gt {t s x y lo hi} (h : lt x y) (ht : is_searchable lt t lo (some x)) (hs : is_searchable lt s (some x) hi) (hne : t ≠ leaf . ins_ne_leaf_tac) : find lt (balance1_node t x s) y = find lt s y := begin cases t; simp [balance1_node], all_goals { intros, is_searchable_tactic, apply find_balance1_gt, assumption' } end lemma find_balance1_eqv {l r t v x y lo hi} (h : ¬ lt x y ∧ ¬ lt y x) (hl : is_searchable lt l lo (some v)) (hr : is_searchable lt r (some v) (some y)) (ht : is_searchable lt t (some y) hi) : find lt (balance1 l v r y t) x = some y := begin with_cases { revert hl hr ht, apply balance.cases l v r; intros; simp [*]; is_searchable_tactic }, case red_left : _ _ _ z { have : lt z x := lt_of_lt_of_incomp (lo_lt_hi hr) h.swap, simp [*] }, case red_right : _ _ _ z { have : lt z x := lt_of_lt_of_incomp (lo_lt_hi hr_hs₂) h.swap, simp [*] } end lemma find_balance1_node_eqv {t s x y lo hi} (h : ¬ lt x y ∧ ¬ lt y x) (ht : is_searchable lt t lo (some y)) (hs : is_searchable lt s (some y) hi) (hne : t ≠ leaf . ins_ne_leaf_tac) : find lt (balance1_node t y s) x = some y := begin cases t; simp [balance1_node], { contradiction }, all_goals { intros, is_searchable_tactic, apply find_balance1_eqv, assumption' } end lemma find_balance2_lt {l v r t x y lo hi} (h : lt x y) (hl : is_searchable lt l (some y) (some v)) (hr : is_searchable lt r (some v) hi) (ht : is_searchable lt t lo (some y)) : find lt (balance2 l v r y t) x = find lt t x := begin with_cases { revert hl hr ht, apply balance.cases l v r; intros; simp [*]; is_searchable_tactic }, case red_left { have := trans h (lo_lt_hi hl_hs₁), simp [*] }, case red_right { have := trans h (lo_lt_hi hl), simp [*] } end lemma find_balance2_node_lt {s t x y lo hi} (h : lt x y) (ht : is_searchable lt t (some y) hi) (hs : is_searchable lt s lo (some y)) (hne : t ≠ leaf . ins_ne_leaf_tac) : find lt (balance2_node t y s) x = find lt s x := begin cases t; simp [balance2_node], all_goals { intros, is_searchable_tactic, apply find_balance2_lt, assumption' } end lemma find_balance2_gt {l v r t x y lo hi} (h : lt y x) (hl : is_searchable lt l (some y) (some v)) (hr : is_searchable lt r (some v) hi) (ht : is_searchable lt t lo (some y)) : find lt (balance2 l v r y t) x = find lt (red_node l v r) x := begin with_cases { revert hl hr ht, apply balance.cases l v r; intros; simp [*]; is_searchable_tactic }, case red_left : _ val _ z { with_cases { apply weak_trichotomous lt val x; intro h'; simp [*] }, case is_lt { apply weak_trichotomous lt z x; intros; simp [*] }, case is_eqv { have : lt x z := lt_of_incomp_of_lt h'.swap (lo_lt_hi hl_hs₂), simp [*] }, case is_gt { have := trans h' (lo_lt_hi hl_hs₂), simp [*] } }, case red_right : _ val { apply weak_trichotomous lt val x; intros; simp [*] } end lemma find_balance2_node_gt {s t x y lo hi} (h : lt y x) (ht : is_searchable lt t (some y) hi) (hs : is_searchable lt s lo (some y)) (hne : t ≠ leaf . ins_ne_leaf_tac) : find lt (balance2_node t y s) x = find lt t x := begin cases t; simp [balance2_node], { contradiction }, all_goals { intros, is_searchable_tactic, apply find_balance2_gt, assumption' } end lemma find_balance2_eqv {l v r t x y lo hi} (h : ¬ lt x y ∧ ¬ lt y x) (hl : is_searchable lt l (some y) (some v)) (hr : is_searchable lt r (some v) hi) (ht : is_searchable lt t lo (some y)) : find lt (balance2 l v r y t) x = some y := begin with_cases { revert hl hr ht, apply balance.cases l v r; intros; simp [*]; is_searchable_tactic }, case red_left { have := lt_of_incomp_of_lt h (lo_lt_hi hl_hs₁), simp [*] }, case red_right { have := lt_of_incomp_of_lt h (lo_lt_hi hl), simp [*] } end lemma find_balance2_node_eqv {t s x y lo hi} (h : ¬ lt x y ∧ ¬ lt y x) (ht : is_searchable lt t (some y) hi) (hs : is_searchable lt s lo (some y)) (hne : t ≠ leaf . ins_ne_leaf_tac) : find lt (balance2_node t y s) x = some y := begin cases t; simp [balance2_node], { contradiction }, all_goals { intros, is_searchable_tactic, apply find_balance2_eqv, assumption' } end lemma find_ins_of_disj {x y : α} {t : rbnode α} (hn : lt x y ∨ lt y x) : ∀ {lo hi} (hs : is_searchable lt t lo hi) (hlt₁ : lift lt lo (some x)) (hlt₂ : lift lt (some x) hi), find lt (ins lt t x) y = find lt t y := begin apply ins.induction lt t x; intros, { cases hn, all_goals { simp [find, ins, cmp_using, *] } }, all_goals { simp at hc, cases hs }, { have := ih hs_hs₁ hlt₁ hc, simp_fi }, { cases hn, { have := lt_of_incomp_of_lt hc.symm hn, simp_fi }, { have := lt_of_lt_of_incomp hn hc, simp_fi } }, { have := ih hs_hs₂ hc hlt₂, simp_fi }, { have ih := ih hs_hs₁ hlt₁ hc, cases hn, { cases hc' : cmp_using lt y y_1; simp at hc', { have hsi := is_searchable_ins lt hs_hs₁ hlt₁ (trans_of lt hn hc'), have := find_balance1_node_lt lt hc' hsi hs_hs₂, simp_fi }, { have hlt := lt_of_lt_of_incomp hn hc', have hsi := is_searchable_ins lt hs_hs₁ hlt₁ hlt, have := find_balance1_node_eqv lt hc' hsi hs_hs₂, simp_fi }, { have hsi := is_searchable_ins lt hs_hs₁ hlt₁ hc, have := find_balance1_node_gt lt hc' hsi hs_hs₂, simp [*], simp_fi } }, { have hlt := trans hn hc, have hsi := is_searchable_ins lt hs_hs₁ hlt₁ hc, have := find_balance1_node_lt lt hlt hsi hs_hs₂, simp_fi } }, { have := ih hs_hs₁ hlt₁ hc, simp_fi }, { cases hn, { have := lt_of_incomp_of_lt hc.swap hn, simp_fi }, { have := lt_of_lt_of_incomp hn hc, simp_fi } }, { have ih := ih hs_hs₂ hc hlt₂, cases hn, { have hlt := trans hc hn, simp_fi, have hsi := is_searchable_ins lt hs_hs₂ hc hlt₂, have := find_balance2_node_gt lt hlt hsi hs_hs₁, simp_fi }, { simp_fi, cases hc' : cmp_using lt y y_1; simp at hc', { have hsi := is_searchable_ins lt hs_hs₂ hc hlt₂, have := find_balance2_node_lt lt hc' hsi hs_hs₁, simp_fi }, { have hlt := lt_of_incomp_of_lt hc'.swap hn, have hsi := is_searchable_ins lt hs_hs₂ hlt hlt₂, have := find_balance2_node_eqv lt hc' hsi hs_hs₁, simp_fi }, { have hsi := is_searchable_ins lt hs_hs₂ hc hlt₂, have := find_balance2_node_gt lt hc' hsi hs_hs₁, simp_fi } } }, { have ih := ih hs_hs₂ hc hlt₂, simp_fi } end end find_ins_of_not_eqv lemma find_insert_of_disj [decidable_rel lt] [is_strict_weak_order α lt] {x y : α} {t : rbnode α} (hd : lt x y ∨ lt y x) : is_searchable lt t none none → find lt (insert lt t x) y = find lt t y := begin intro hs, simp [insert, find_mk_insert_result], apply find_ins_of_disj lt hd hs; simp end lemma find_insert_of_not_eqv [decidable_rel lt] [is_strict_weak_order α lt] {x y : α} {t : rbnode α} (hn : ¬ x ≈[lt] y) : is_searchable lt t none none → find lt (insert lt t x) y = find lt t y := begin intro hs, simp [insert, find_mk_insert_result], have he : lt x y ∨ lt y x, { simp [strict_weak_order.equiv, decidable.not_and_iff_or_not, decidable.not_not_iff] at hn, assumption }, apply find_ins_of_disj lt he hs; simp end end membership_lemmas section is_red_black variables {α : Type u} open nat color inductive is_bad_red_black : rbnode α → nat → Prop | bad_red {c₁ c₂ n l r v} (rb_l : is_red_black l c₁ n) (rb_r : is_red_black r c₂ n) : is_bad_red_black (red_node l v r) n lemma balance1_rb {l r t : rbnode α} {y v : α} {c_l c_r c_t n} : is_red_black l c_l n → is_red_black r c_r n → is_red_black t c_t n → ∃ c, is_red_black (balance1 l y r v t) c (succ n) := by intros h₁ h₂ _; cases h₁; cases h₂; repeat { assumption <|> constructor } lemma balance2_rb {l r t : rbnode α} {y v : α} {c_l c_r c_t n} : is_red_black l c_l n → is_red_black r c_r n → is_red_black t c_t n → ∃ c, is_red_black (balance2 l y r v t) c (succ n) := by intros h₁ h₂ _; cases h₁; cases h₂; repeat { assumption <|> constructor } lemma balance1_node_rb {t s : rbnode α} {y : α} {c n} : is_bad_red_black t n → is_red_black s c n → ∃ c, is_red_black (balance1_node t y s) c (succ n) := by intros h _; cases h; simp [balance1_node]; apply balance1_rb; assumption' lemma balance2_node_rb {t s : rbnode α} {y : α} {c n} : is_bad_red_black t n → is_red_black s c n → ∃ c, is_red_black (balance2_node t y s) c (succ n) := by intros h _; cases h; simp [balance2_node]; apply balance2_rb; assumption' def ins_rb_result : rbnode α → color → nat → Prop | t red n := is_bad_red_black t n | t black n := ∃ c, is_red_black t c n variables {lt : α → α → Prop} [decidable_rel lt] lemma of_get_color_eq_red {t : rbnode α} {c n} : get_color t = red → is_red_black t c n → c = red := begin intros h₁ h₂, cases h₂; simp [get_color] at h₁; contradiction end lemma of_get_color_ne_red {t : rbnode α} {c n} : get_color t ≠ red → is_red_black t c n → c = black := begin intros h₁ h₂, cases h₂; simp [get_color] at h₁; contradiction end variable (lt) lemma ins_rb {t : rbnode α} (x) : ∀ {c n} (h : is_red_black t c n), ins_rb_result (ins lt t x) c n := begin apply ins.induction lt t x; intros; cases h; simp [ins, *, ins_rb_result], { repeat { constructor } }, { specialize ih h_rb_l, cases ih, constructor; assumption }, { constructor; assumption }, { specialize ih h_rb_r, cases ih, constructor; assumption }, { specialize ih h_rb_l, have := of_get_color_eq_red hr h_rb_l, subst h_c₁, simp [ins_rb_result] at ih, apply balance1_node_rb; assumption }, { specialize ih h_rb_l, have := of_get_color_ne_red hnr h_rb_l, subst h_c₁, simp [ins_rb_result] at ih, cases ih, constructor, constructor; assumption }, { constructor, constructor; assumption }, { specialize ih h_rb_r, have := of_get_color_eq_red hr h_rb_r, subst h_c₂, simp [ins_rb_result] at ih, apply balance2_node_rb; assumption }, { specialize ih h_rb_r, have := of_get_color_ne_red hnr h_rb_r, subst h_c₂, simp [ins_rb_result] at ih, cases ih, constructor, constructor; assumption } end def insert_rb_result : rbnode α → color → nat → Prop | t red n := is_red_black t black (succ n) | t black n := ∃ c, is_red_black t c n lemma insert_rb {t : rbnode α} (x) {c n} (h : is_red_black t c n) : insert_rb_result (insert lt t x) c n := begin simp [insert], have hi := ins_rb lt x h, generalize he : ins lt t x = r, simp [he] at hi, cases h; simp [get_color, ins_rb_result, insert_rb_result, mk_insert_result] at *, assumption', { cases hi, simp [mk_insert_result], constructor; assumption } end lemma insert_is_red_black {t : rbnode α} {c n} (x) : is_red_black t c n → ∃ c n, is_red_black (insert lt t x) c n := begin intro h, have := insert_rb lt x h, cases c; simp [insert_rb_result] at this, { constructor, constructor, assumption }, { cases this, constructor, constructor, assumption } end end is_red_black end rbnode
71d99cc9df5a6b4ad7ff360967caed3c8d0d70a6
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/computability/primrec.lean
d0ca8d9e0a5f1e9bb23c3579de42ae786add7f9e
[ "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
51,929
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.equiv.list import logic.function.iterate /-! # The primitive recursive functions The primitive recursive functions are the least collection of functions `nat → nat` which are closed under projections (using the mkpair pairing function), composition, zero, successor, and primitive recursion (i.e. nat.rec where the motive is C n := nat). We can extend this definition to a large class of basic types by using canonical encodings of types as natural numbers (Gödel numbering), which we implement through the type class `encodable`. (More precisely, we need that the composition of encode with decode yields a primitive recursive function, so we have the `primcodable` type class for this.) ## References * [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019] -/ open denumerable encodable namespace nat def elim {C : Sort*} : C → (ℕ → C → C) → ℕ → C := @nat.rec (λ _, C) @[simp] theorem elim_zero {C} (a f) : @nat.elim C a f 0 = a := rfl @[simp] theorem elim_succ {C} (a f n) : @nat.elim C a f (succ n) = f n (nat.elim a f n) := rfl def cases {C : Sort*} (a : C) (f : ℕ → C) : ℕ → C := nat.elim a (λ n _, f n) @[simp] theorem cases_zero {C} (a f) : @nat.cases C a f 0 = a := rfl @[simp] theorem cases_succ {C} (a f n) : @nat.cases C a f (succ n) = f n := rfl @[simp, reducible] def unpaired {α} (f : ℕ → ℕ → α) (n : ℕ) : α := f n.unpair.1 n.unpair.2 /-- The primitive recursive functions `ℕ → ℕ`. -/ inductive primrec : (ℕ → ℕ) → Prop | zero : primrec (λ n, 0) | succ : primrec succ | left : primrec (λ n, n.unpair.1) | right : primrec (λ n, n.unpair.2) | pair {f g} : primrec f → primrec g → primrec (λ n, mkpair (f n) (g n)) | comp {f g} : primrec f → primrec g → primrec (λ n, f (g n)) | prec {f g} : primrec f → primrec g → primrec (unpaired (λ z n, n.elim (f z) (λ y IH, g $ mkpair z $ mkpair y IH))) namespace primrec theorem of_eq {f g : ℕ → ℕ} (hf : primrec f) (H : ∀ n, f n = g n) : primrec g := (funext H : f = g) ▸ hf theorem const : ∀ (n : ℕ), primrec (λ _, n) | 0 := zero | (n+1) := succ.comp (const n) protected theorem id : primrec id := (left.pair right).of_eq $ λ n, by simp theorem prec1 {f} (m : ℕ) (hf : primrec f) : primrec (λ n, n.elim m (λ y IH, f $ mkpair y IH)) := ((prec (const m) (hf.comp right)).comp (zero.pair primrec.id)).of_eq $ λ n, by simp; dsimp; rw [unpair_mkpair] theorem cases1 {f} (m : ℕ) (hf : primrec f) : primrec (nat.cases m f) := (prec1 m (hf.comp left)).of_eq $ by simp [cases] theorem cases {f g} (hf : primrec f) (hg : primrec g) : primrec (unpaired (λ z n, n.cases (f z) (λ y, g $ mkpair z y))) := (prec hf (hg.comp (pair left (left.comp right)))).of_eq $ by simp [cases] protected theorem swap : primrec (unpaired (function.swap mkpair)) := (pair right left).of_eq $ λ n, by simp theorem swap' {f} (hf : primrec (unpaired f)) : primrec (unpaired (function.swap f)) := (hf.comp primrec.swap).of_eq $ λ n, by simp theorem pred : primrec pred := (cases1 0 primrec.id).of_eq $ λ n, by cases n; simp * theorem add : primrec (unpaired (+)) := (prec primrec.id ((succ.comp right).comp right)).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, -add_comm, add_succ] theorem sub : primrec (unpaired has_sub.sub) := (prec primrec.id ((pred.comp right).comp right)).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, -add_comm, sub_succ] theorem mul : primrec (unpaired (*)) := (prec zero (add.comp (pair left (right.comp right)))).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, mul_succ, add_comm] theorem pow : primrec (unpaired (^)) := (prec (const 1) (mul.comp (pair (right.comp right) left))).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, pow_succ'] end primrec end nat /-- A `primcodable` type is an `encodable` type for which the encode/decode functions are primitive recursive. -/ class primcodable (α : Type*) extends encodable α := (prim [] : nat.primrec (λ n, encodable.encode (decode n))) namespace primcodable open nat.primrec @[priority 10] instance of_denumerable (α) [denumerable α] : primcodable α := ⟨succ.of_eq $ by simp⟩ def of_equiv (α) {β} [primcodable α] (e : β ≃ α) : primcodable β := { prim := (primcodable.prim α).of_eq $ λ n, show encode (decode α n) = (option.cases_on (option.map e.symm (decode α n)) 0 (λ a, nat.succ (encode (e a))) : ℕ), by cases decode α n; dsimp; simp, ..encodable.of_equiv α e } instance empty : primcodable empty := ⟨zero⟩ instance unit : primcodable punit := ⟨(cases1 1 zero).of_eq $ λ n, by cases n; simp⟩ instance option {α : Type*} [h : primcodable α] : primcodable (option α) := ⟨(cases1 1 ((cases1 0 (succ.comp succ)).comp (primcodable.prim α))).of_eq $ λ n, by cases n; simp; cases decode α n; refl⟩ instance bool : primcodable bool := ⟨(cases1 1 (cases1 2 zero)).of_eq $ λ n, begin cases n, {refl}, cases n, {refl}, rw decode_ge_two, {refl}, exact dec_trivial end⟩ end primcodable /-- `primrec f` means `f` is primitive recursive (after encoding its input and output as natural numbers). -/ def primrec {α β} [primcodable α] [primcodable β] (f : α → β) : Prop := nat.primrec (λ n, encode ((decode α n).map f)) namespace primrec variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] open nat.primrec protected theorem encode : primrec (@encode α _) := (primcodable.prim α).of_eq $ λ n, by cases decode α n; refl protected theorem decode : primrec (decode α) := succ.comp (primcodable.prim α) theorem dom_denumerable {α β} [denumerable α] [primcodable β] {f : α → β} : primrec f ↔ nat.primrec (λ n, encode (f (of_nat α n))) := ⟨λ h, (pred.comp h).of_eq $ λ n, by simp; refl, λ h, (succ.comp h).of_eq $ λ n, by simp; refl⟩ theorem nat_iff {f : ℕ → ℕ} : primrec f ↔ nat.primrec f := dom_denumerable theorem encdec : primrec (λ n, encode (decode α n)) := nat_iff.2 (primcodable.prim α) theorem option_some : primrec (@some α) := ((cases1 0 (succ.comp succ)).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; simp theorem of_eq {f g : α → σ} (hf : primrec f) (H : ∀ n, f n = g n) : primrec g := (funext H : f = g) ▸ hf theorem const (x : σ) : primrec (λ a : α, x) := ((cases1 0 (const (encode x).succ)).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; refl protected theorem id : primrec (@id α) := (primcodable.prim α).of_eq $ by simp theorem comp {f : β → σ} {g : α → β} (hf : primrec f) (hg : primrec g) : primrec (λ a, f (g a)) := ((cases1 0 (hf.comp $ pred.comp hg)).comp (primcodable.prim α)).of_eq $ λ n, begin cases decode α n, {refl}, simp [encodek] end theorem succ : primrec nat.succ := nat_iff.2 nat.primrec.succ theorem pred : primrec nat.pred := nat_iff.2 nat.primrec.pred theorem encode_iff {f : α → σ} : primrec (λ a, encode (f a)) ↔ primrec f := ⟨λ h, nat.primrec.of_eq h $ λ n, by cases decode α n; refl, primrec.encode.comp⟩ theorem of_nat_iff {α β} [denumerable α] [primcodable β] {f : α → β} : primrec f ↔ primrec (λ n, f (of_nat α n)) := dom_denumerable.trans $ nat_iff.symm.trans encode_iff protected theorem of_nat (α) [denumerable α] : primrec (of_nat α) := of_nat_iff.1 primrec.id theorem option_some_iff {f : α → σ} : primrec (λ a, some (f a)) ↔ primrec f := ⟨λ h, encode_iff.1 $ pred.comp $ encode_iff.2 h, option_some.comp⟩ theorem of_equiv {β} {e : β ≃ α} : by haveI := primcodable.of_equiv α e; exact primrec e := by letI : primcodable β := primcodable.of_equiv α e; exact encode_iff.1 primrec.encode theorem of_equiv_symm {β} {e : β ≃ α} : by haveI := primcodable.of_equiv α e; exact primrec e.symm := by letI := primcodable.of_equiv α e; exact encode_iff.1 (show primrec (λ a, encode (e (e.symm a))), by simp [primrec.encode]) theorem of_equiv_iff {β} (e : β ≃ α) {f : σ → β} : by haveI := primcodable.of_equiv α e; exact primrec (λ a, e (f a)) ↔ primrec f := by letI := primcodable.of_equiv α e; exact ⟨λ h, (of_equiv_symm.comp h).of_eq (λ a, by simp), of_equiv.comp⟩ theorem of_equiv_symm_iff {β} (e : β ≃ α) {f : σ → α} : by haveI := primcodable.of_equiv α e; exact primrec (λ a, e.symm (f a)) ↔ primrec f := by letI := primcodable.of_equiv α e; exact ⟨λ h, (of_equiv.comp h).of_eq (λ a, by simp), of_equiv_symm.comp⟩ end primrec namespace primcodable open nat.primrec instance prod {α β} [primcodable α] [primcodable β] : primcodable (α × β) := ⟨((cases zero ((cases zero succ).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp [nat.unpaired], cases decode α n.unpair.1, { simp }, cases decode β n.unpair.2; simp end⟩ end primcodable namespace primrec variables {α : Type*} {σ : Type*} [primcodable α] [primcodable σ] open nat.primrec theorem fst {α β} [primcodable α] [primcodable β] : primrec (@prod.fst α β) := ((cases zero ((cases zero (nat.primrec.succ.comp left)).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp, cases decode α n.unpair.1; simp, cases decode β n.unpair.2; simp end theorem snd {α β} [primcodable α] [primcodable β] : primrec (@prod.snd α β) := ((cases zero ((cases zero (nat.primrec.succ.comp right)).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp, cases decode α n.unpair.1; simp, cases decode β n.unpair.2; simp end theorem pair {α β γ} [primcodable α] [primcodable β] [primcodable γ] {f : α → β} {g : α → γ} (hf : primrec f) (hg : primrec g) : primrec (λ a, (f a, g a)) := ((cases1 0 (nat.primrec.succ.comp $ pair (nat.primrec.pred.comp hf) (nat.primrec.pred.comp hg))).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; simp [encodek]; refl theorem unpair : primrec nat.unpair := (pair (nat_iff.2 nat.primrec.left) (nat_iff.2 nat.primrec.right)).of_eq $ λ n, by simp theorem list_nth₁ : ∀ (l : list α), primrec l.nth | [] := dom_denumerable.2 zero | (a::l) := dom_denumerable.2 $ (cases1 (encode a).succ $ dom_denumerable.1 $ list_nth₁ l).of_eq $ λ n, by cases n; simp end primrec /-- `primrec₂ f` means `f` is a binary primitive recursive function. This is technically unnecessary since we can always curry all the arguments together, but there are enough natural two-arg functions that it is convenient to express this directly. -/ def primrec₂ {α β σ} [primcodable α] [primcodable β] [primcodable σ] (f : α → β → σ) := primrec (λ p : α × β, f p.1 p.2) /-- `primrec_pred p` means `p : α → Prop` is a (decidable) primitive recursive predicate, which is to say that `to_bool ∘ p : α → bool` is primitive recursive. -/ def primrec_pred {α} [primcodable α] (p : α → Prop) [decidable_pred p] := primrec (λ a, to_bool (p a)) /-- `primrec_rel p` means `p : α → β → Prop` is a (decidable) primitive recursive relation, which is to say that `to_bool ∘ p : α → β → bool` is primitive recursive. -/ def primrec_rel {α β} [primcodable α] [primcodable β] (s : α → β → Prop) [∀ a b, decidable (s a b)] := primrec₂ (λ a b, to_bool (s a b)) namespace primrec₂ variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] theorem of_eq {f g : α → β → σ} (hg : primrec₂ f) (H : ∀ a b, f a b = g a b) : primrec₂ g := (by funext a b; apply H : f = g) ▸ hg theorem const (x : σ) : primrec₂ (λ (a : α) (b : β), x) := primrec.const _ protected theorem pair : primrec₂ (@prod.mk α β) := primrec.pair primrec.fst primrec.snd theorem left : primrec₂ (λ (a : α) (b : β), a) := primrec.fst theorem right : primrec₂ (λ (a : α) (b : β), b) := primrec.snd theorem mkpair : primrec₂ nat.mkpair := by simp [primrec₂, primrec]; constructor theorem unpaired {f : ℕ → ℕ → α} : primrec (nat.unpaired f) ↔ primrec₂ f := ⟨λ h, by simpa using h.comp mkpair, λ h, h.comp primrec.unpair⟩ theorem unpaired' {f : ℕ → ℕ → ℕ} : nat.primrec (nat.unpaired f) ↔ primrec₂ f := primrec.nat_iff.symm.trans unpaired theorem encode_iff {f : α → β → σ} : primrec₂ (λ a b, encode (f a b)) ↔ primrec₂ f := primrec.encode_iff theorem option_some_iff {f : α → β → σ} : primrec₂ (λ a b, some (f a b)) ↔ primrec₂ f := primrec.option_some_iff theorem of_nat_iff {α β σ} [denumerable α] [denumerable β] [primcodable σ] {f : α → β → σ} : primrec₂ f ↔ primrec₂ (λ m n : ℕ, f (of_nat α m) (of_nat β n)) := (primrec.of_nat_iff.trans $ by simp).trans unpaired theorem uncurry {f : α → β → σ} : primrec (function.uncurry f) ↔ primrec₂ f := by rw [show function.uncurry f = λ (p : α × β), f p.1 p.2, from funext $ λ ⟨a, b⟩, rfl]; refl theorem curry {f : α × β → σ} : primrec₂ (function.curry f) ↔ primrec f := by rw [← uncurry, function.uncurry_curry] end primrec₂ section comp variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable δ] [primcodable σ] theorem primrec.comp₂ {f : γ → σ} {g : α → β → γ} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a b, f (g a b)) := hf.comp hg theorem primrec₂.comp {f : β → γ → σ} {g : α → β} {h : α → γ} (hf : primrec₂ f) (hg : primrec g) (hh : primrec h) : primrec (λ a, f (g a) (h a)) := hf.comp (hg.pair hh) theorem primrec₂.comp₂ {f : γ → δ → σ} {g : α → β → γ} {h : α → β → δ} (hf : primrec₂ f) (hg : primrec₂ g) (hh : primrec₂ h) : primrec₂ (λ a b, f (g a b) (h a b)) := hf.comp hg hh theorem primrec_pred.comp {p : β → Prop} [decidable_pred p] {f : α → β} : primrec_pred p → primrec f → primrec_pred (λ a, p (f a)) := primrec.comp theorem primrec_rel.comp {R : β → γ → Prop} [∀ a b, decidable (R a b)] {f : α → β} {g : α → γ} : primrec_rel R → primrec f → primrec g → primrec_pred (λ a, R (f a) (g a)) := primrec₂.comp theorem primrec_rel.comp₂ {R : γ → δ → Prop} [∀ a b, decidable (R a b)] {f : α → β → γ} {g : α → β → δ} : primrec_rel R → primrec₂ f → primrec₂ g → primrec_rel (λ a b, R (f a b) (g a b)) := primrec_rel.comp end comp theorem primrec_pred.of_eq {α} [primcodable α] {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (H : ∀ a, p a ↔ q a) : primrec_pred q := primrec.of_eq hp (λ a, to_bool_congr (H a)) theorem primrec_rel.of_eq {α β} [primcodable α] [primcodable β] {r s : α → β → Prop} [∀ a b, decidable (r a b)] [∀ a b, decidable (s a b)] (hr : primrec_rel r) (H : ∀ a b, r a b ↔ s a b) : primrec_rel s := primrec₂.of_eq hr (λ a b, to_bool_congr (H a b)) namespace primrec₂ variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] open nat.primrec theorem swap {f : α → β → σ} (h : primrec₂ f) : primrec₂ (function.swap f) := h.comp₂ primrec₂.right primrec₂.left theorem nat_iff {f : α → β → σ} : primrec₂ f ↔ nat.primrec (nat.unpaired $ λ m n : ℕ, encode $ (decode α m).bind $ λ a, (decode β n).map (f a)) := have ∀ (a : option α) (b : option β), option.map (λ (p : α × β), f p.1 p.2) (option.bind a (λ (a : α), option.map (prod.mk a) b)) = option.bind a (λ a, option.map (f a) b), by intros; cases a; [refl, {cases b; refl}], by simp [primrec₂, primrec, this] theorem nat_iff' {f : α → β → σ} : primrec₂ f ↔ primrec₂ (λ m n : ℕ, option.bind (decode α m) (λ a, option.map (f a) (decode β n))) := nat_iff.trans $ unpaired'.trans encode_iff end primrec₂ namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable δ] [primcodable σ] theorem to₂ {f : α × β → σ} (hf : primrec f) : primrec₂ (λ a b, f (a, b)) := hf.of_eq $ λ ⟨a, b⟩, rfl theorem nat_elim {f : α → β} {g : α → ℕ × β → β} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a (n : ℕ), n.elim (f a) (λ n IH, g a (n, IH))) := primrec₂.nat_iff.2 $ ((nat.primrec.cases nat.primrec.zero $ (nat.primrec.prec hf $ nat.primrec.comp hg $ nat.primrec.left.pair $ (nat.primrec.left.comp nat.primrec.right).pair $ nat.primrec.pred.comp $ nat.primrec.right.comp nat.primrec.right).comp $ nat.primrec.right.pair $ nat.primrec.right.comp nat.primrec.left).comp $ nat.primrec.id.pair $ (primcodable.prim α).comp nat.primrec.left).of_eq $ λ n, begin simp, cases decode α n.unpair.1 with a, {refl}, simp [encodek], induction n.unpair.2 with m; simp [encodek], simp [ih, encodek] end theorem nat_elim' {f : α → ℕ} {g : α → β} {h : α → ℕ × β → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).elim (g a) (λ n IH, h a (n, IH))) := (nat_elim hg hh).comp primrec.id hf theorem nat_elim₁ {f : ℕ → α → α} (a : α) (hf : primrec₂ f) : primrec (nat.elim a f) := nat_elim' primrec.id (const a) $ comp₂ hf primrec₂.right theorem nat_cases' {f : α → β} {g : α → ℕ → β} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a, nat.cases (f a) (g a)) := nat_elim hf $ hg.comp₂ primrec₂.left $ comp₂ fst primrec₂.right theorem nat_cases {f : α → ℕ} {g : α → β} {h : α → ℕ → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).cases (g a) (h a)) := (nat_cases' hg hh).comp primrec.id hf theorem nat_cases₁ {f : ℕ → α} (a : α) (hf : primrec f) : primrec (nat.cases a f) := nat_cases primrec.id (const a) (comp₂ hf primrec₂.right) theorem nat_iterate {f : α → ℕ} {g : α → β} {h : α → β → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (h a)^[f a] (g a)) := (nat_elim' hf hg (hh.comp₂ primrec₂.left $ snd.comp₂ primrec₂.right)).of_eq $ λ a, by induction f a; simp [*, function.iterate_succ'] theorem option_cases {o : α → option β} {f : α → σ} {g : α → β → σ} (ho : primrec o) (hf : primrec f) (hg : primrec₂ g) : @primrec _ σ _ _ (λ a, option.cases_on (o a) (f a) (g a)) := encode_iff.1 $ (nat_cases (encode_iff.2 ho) (encode_iff.2 hf) $ pred.comp₂ $ primrec₂.encode_iff.2 $ (primrec₂.nat_iff'.1 hg).comp₂ ((@primrec.encode α _).comp fst).to₂ primrec₂.right).of_eq $ λ a, by cases o a with b; simp [encodek]; refl theorem option_bind {f : α → option β} {g : α → β → option σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).bind (g a)) := (option_cases hf (const none) hg).of_eq $ λ a, by cases f a; refl theorem option_bind₁ {f : α → option σ} (hf : primrec f) : primrec (λ o, option.bind o f) := option_bind primrec.id (hf.comp snd).to₂ theorem option_map {f : α → option β} {g : α → β → σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).map (g a)) := option_bind hf (option_some.comp₂ hg) theorem option_map₁ {f : α → σ} (hf : primrec f) : primrec (option.map f) := option_map primrec.id (hf.comp snd).to₂ theorem option_iget [inhabited α] : primrec (@option.iget α _) := (option_cases primrec.id (const $ default α) primrec₂.right).of_eq $ λ o, by cases o; refl theorem option_is_some : primrec (@option.is_some α) := (option_cases primrec.id (const ff) (const tt).to₂).of_eq $ λ o, by cases o; refl theorem option_get_or_else : primrec₂ (@option.get_or_else α) := primrec.of_eq (option_cases primrec₂.left primrec₂.right primrec₂.right) $ λ ⟨o, a⟩, by cases o; refl theorem bind_decode_iff {f : α → β → option σ} : primrec₂ (λ a n, (decode β n).bind (f a)) ↔ primrec₂ f := ⟨λ h, by simpa [encodek] using h.comp fst ((@primrec.encode β _).comp snd), λ h, option_bind (primrec.decode.comp snd) $ h.comp (fst.comp fst) snd⟩ theorem map_decode_iff {f : α → β → σ} : primrec₂ (λ a n, (decode β n).map (f a)) ↔ primrec₂ f := bind_decode_iff.trans primrec₂.option_some_iff theorem nat_add : primrec₂ ((+) : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.add theorem nat_sub : primrec₂ (has_sub.sub : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.sub theorem nat_mul : primrec₂ ((*) : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.mul theorem cond {c : α → bool} {f : α → σ} {g : α → σ} (hc : primrec c) (hf : primrec f) (hg : primrec g) : primrec (λ a, cond (c a) (f a) (g a)) := (nat_cases (encode_iff.2 hc) hg (hf.comp fst).to₂).of_eq $ λ a, by cases c a; refl theorem ite {c : α → Prop} [decidable_pred c] {f : α → σ} {g : α → σ} (hc : primrec_pred c) (hf : primrec f) (hg : primrec g) : primrec (λ a, if c a then f a else g a) := by simpa using cond hc hf hg theorem nat_le : primrec_rel ((≤) : ℕ → ℕ → Prop) := (nat_cases nat_sub (const tt) (const ff).to₂).of_eq $ λ p, begin dsimp [function.swap], cases e : p.1 - p.2 with n, { simp [nat.sub_eq_zero_iff_le.1 e] }, { simp [not_le.2 (nat.lt_of_sub_eq_succ e)] } end theorem nat_min : primrec₂ (@min ℕ _) := ite nat_le fst snd theorem nat_max : primrec₂ (@max ℕ _) := ite (nat_le.comp primrec.snd primrec.fst) fst snd theorem dom_bool (f : bool → α) : primrec f := (cond primrec.id (const (f tt)) (const (f ff))).of_eq $ λ b, by cases b; refl theorem dom_bool₂ (f : bool → bool → α) : primrec₂ f := (cond fst ((dom_bool (f tt)).comp snd) ((dom_bool (f ff)).comp snd)).of_eq $ λ ⟨a, b⟩, by cases a; refl protected theorem bnot : primrec bnot := dom_bool _ protected theorem band : primrec₂ band := dom_bool₂ _ protected theorem bor : primrec₂ bor := dom_bool₂ _ protected theorem not {p : α → Prop} [decidable_pred p] (hp : primrec_pred p) : primrec_pred (λ a, ¬ p a) := (primrec.bnot.comp hp).of_eq $ λ n, by simp protected theorem and {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (hq : primrec_pred q) : primrec_pred (λ a, p a ∧ q a) := (primrec.band.comp hp hq).of_eq $ λ n, by simp protected theorem or {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (hq : primrec_pred q) : primrec_pred (λ a, p a ∨ q a) := (primrec.bor.comp hp hq).of_eq $ λ n, by simp protected theorem eq [decidable_eq α] : primrec_rel (@eq α) := have primrec_rel (λ a b : ℕ, a = b), from (primrec.and nat_le nat_le.swap).of_eq $ λ a, by simp [le_antisymm_iff], (this.comp₂ (primrec.encode.comp₂ primrec₂.left) (primrec.encode.comp₂ primrec₂.right)).of_eq $ λ a b, encode_injective.eq_iff theorem nat_lt : primrec_rel ((<) : ℕ → ℕ → Prop) := (nat_le.comp snd fst).not.of_eq $ λ p, by simp theorem option_guard {p : α → β → Prop} [∀ a b, decidable (p a b)] (hp : primrec_rel p) {f : α → β} (hf : primrec f) : primrec (λ a, option.guard (p a) (f a)) := ite (hp.comp primrec.id hf) (option_some_iff.2 hf) (const none) theorem option_orelse : primrec₂ ((<|>) : option α → option α → option α) := (option_cases fst snd (fst.comp fst).to₂).of_eq $ λ ⟨o₁, o₂⟩, by cases o₁; cases o₂; refl protected theorem decode₂ : primrec (decode₂ α) := option_bind primrec.decode $ option_guard ((@primrec.eq _ _ nat.decidable_eq).comp (encode_iff.2 snd) (fst.comp fst)) snd theorem list_find_index₁ {p : α → β → Prop} [∀ a b, decidable (p a b)] (hp : primrec_rel p) : ∀ (l : list β), primrec (λ a, l.find_index (p a)) | [] := const 0 | (a::l) := ite (hp.comp primrec.id (const a)) (const 0) (succ.comp (list_find_index₁ l)) theorem list_index_of₁ [decidable_eq α] (l : list α) : primrec (λ a, l.index_of a) := list_find_index₁ primrec.eq l theorem dom_fintype [fintype α] (f : α → σ) : primrec f := let ⟨l, nd, m⟩ := fintype.exists_univ_list α in option_some_iff.1 $ begin haveI := decidable_eq_of_encodable α, refine ((list_nth₁ (l.map f)).comp (list_index_of₁ l)).of_eq (λ a, _), rw [list.nth_map, list.nth_le_nth (list.index_of_lt_length.2 (m _)), list.index_of_nth_le]; refl end theorem nat_bodd_div2 : primrec nat.bodd_div2 := (nat_elim' primrec.id (const (ff, 0)) (((cond fst (pair (const ff) (succ.comp snd)) (pair (const tt) snd)).comp snd).comp snd).to₂).of_eq $ λ n, begin simp [-nat.bodd_div2_eq], induction n with n IH, {refl}, simp [-nat.bodd_div2_eq, nat.bodd_div2, *], rcases nat.bodd_div2 n with ⟨_|_, m⟩; simp [nat.bodd_div2] end theorem nat_bodd : primrec nat.bodd := fst.comp nat_bodd_div2 theorem nat_div2 : primrec nat.div2 := snd.comp nat_bodd_div2 theorem nat_bit0 : primrec (@bit0 ℕ _) := nat_add.comp primrec.id primrec.id theorem nat_bit1 : primrec (@bit1 ℕ _ _) := nat_add.comp nat_bit0 (const 1) theorem nat_bit : primrec₂ nat.bit := (cond primrec.fst (nat_bit1.comp primrec.snd) (nat_bit0.comp primrec.snd)).of_eq $ λ n, by cases n.1; refl theorem nat_div_mod : primrec₂ (λ n k : ℕ, (n / k, n % k)) := let f (a : ℕ × ℕ) : ℕ × ℕ := a.1.elim (0, 0) (λ _ IH, if nat.succ IH.2 = a.2 then (nat.succ IH.1, 0) else (IH.1, nat.succ IH.2)) in have hf : primrec f, from nat_elim' fst (const (0, 0)) $ ((ite ((@primrec.eq ℕ _ _).comp (succ.comp $ snd.comp snd) fst) (pair (succ.comp $ fst.comp snd) (const 0)) (pair (fst.comp snd) (succ.comp $ snd.comp snd))) .comp (pair (snd.comp fst) (snd.comp snd))).to₂, suffices ∀ k n, (n / k, n % k) = f (n, k), from hf.of_eq $ λ ⟨m, n⟩, by simp [this], λ k n, begin have : (f (n, k)).2 + k * (f (n, k)).1 = n ∧ (0 < k → (f (n, k)).2 < k) ∧ (k = 0 → (f (n, k)).1 = 0), { induction n with n IH, {exact ⟨rfl, id, λ _, rfl⟩}, rw [λ n:ℕ, show f (n.succ, k) = _root_.ite ((f (n, k)).2.succ = k) (nat.succ (f (n, k)).1, 0) ((f (n, k)).1, (f (n, k)).2.succ), from rfl], by_cases h : (f (n, k)).2.succ = k; simp [h], { have := congr_arg nat.succ IH.1, refine ⟨_, λ k0, nat.no_confusion (h.trans k0)⟩, rwa [← nat.succ_add, h, add_comm, ← nat.mul_succ] at this }, { exact ⟨by rw [nat.succ_add, IH.1], λ k0, lt_of_le_of_ne (IH.2.1 k0) h, IH.2.2⟩ } }, revert this, cases f (n, k) with D M, simp, intros h₁ h₂ h₃, cases nat.eq_zero_or_pos k, { simp [h, h₃ h] at h₁ ⊢, simp [h₁] }, { exact (nat.div_mod_unique h).2 ⟨h₁, h₂ h⟩ } end theorem nat_div : primrec₂ ((/) : ℕ → ℕ → ℕ) := fst.comp₂ nat_div_mod theorem nat_mod : primrec₂ ((%) : ℕ → ℕ → ℕ) := snd.comp₂ nat_div_mod end primrec section variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] variable (H : nat.primrec (λ n, encodable.encode (decode (list β) n))) include H open primrec private def prim : primcodable (list β) := ⟨H⟩ private lemma list_cases' {f : α → list β} {g : α → σ} {h : α → β × list β → σ} (hf : by haveI := prim H; exact primrec f) (hg : primrec g) (hh : by haveI := prim H; exact primrec₂ h) : @primrec _ σ _ _ (λ a, list.cases_on (f a) (g a) (λ b l, h a (b, l))) := by letI := prim H; exact have @primrec _ (option σ) _ _ (λ a, (decode (option (β × list β)) (encode (f a))).map (λ o, option.cases_on o (g a) (h a))), from ((@map_decode_iff _ (option (β × list β)) _ _ _ _ _).2 $ to₂ $ option_cases snd (hg.comp fst) (hh.comp₂ (fst.comp₂ primrec₂.left) primrec₂.right)) .comp primrec.id (encode_iff.2 hf), option_some_iff.1 $ this.of_eq $ λ a, by cases f a with b l; simp [encodek]; refl private lemma list_foldl' {f : α → list β} {g : α → σ} {h : α → σ × β → σ} (hf : by haveI := prim H; exact primrec f) (hg : primrec g) (hh : by haveI := prim H; exact primrec₂ h) : primrec (λ a, (f a).foldl (λ s b, h a (s, b)) (g a)) := by letI := prim H; exact let G (a : α) (IH : σ × list β) : σ × list β := list.cases_on IH.2 IH (λ b l, (h a (IH.1, b), l)) in let F (a : α) (n : ℕ) := (G a)^[n] (g a, f a) in have primrec (λ a, (F a (encode (f a))).1), from fst.comp $ nat_iterate (encode_iff.2 hf) (pair hg hf) $ list_cases' H (snd.comp snd) snd $ to₂ $ pair (hh.comp (fst.comp fst) $ pair ((fst.comp snd).comp fst) (fst.comp snd)) (snd.comp snd), this.of_eq $ λ a, begin have : ∀ n, F a n = ((list.take n (f a)).foldl (λ s b, h a (s, b)) (g a), list.drop n (f a)), { intro, simp [F], generalize : f a = l, generalize : g a = x, induction n with n IH generalizing l x, {refl}, simp, cases l with b l; simp [IH] }, rw [this, list.take_all_of_le (length_le_encode _)] end private lemma list_cons' : by haveI := prim H; exact primrec₂ (@list.cons β) := by letI := prim H; exact encode_iff.1 (succ.comp $ primrec₂.mkpair.comp (encode_iff.2 fst) (encode_iff.2 snd)) private lemma list_reverse' : by haveI := prim H; exact primrec (@list.reverse β) := by letI := prim H; exact (list_foldl' H primrec.id (const []) $ to₂ $ ((list_cons' H).comp snd fst).comp snd).of_eq (suffices ∀ l r, list.foldl (λ (s : list β) (b : β), b :: s) r l = list.reverse_core l r, from λ l, this l [], λ l, by induction l; simp [*, list.reverse_core]) end namespace primcodable variables {α : Type*} {β : Type*} variables [primcodable α] [primcodable β] open primrec instance sum : primcodable (α ⊕ β) := ⟨primrec.nat_iff.1 $ (encode_iff.2 (cond nat_bodd (((@primrec.decode β _).comp nat_div2).option_map $ to₂ $ nat_bit.comp (const tt) (primrec.encode.comp snd)) (((@primrec.decode α _).comp nat_div2).option_map $ to₂ $ nat_bit.comp (const ff) (primrec.encode.comp snd)))).of_eq $ λ n, show _ = encode (decode_sum n), begin simp [decode_sum], cases nat.bodd n; simp [decode_sum], { cases decode α n.div2; refl }, { cases decode β n.div2; refl } end⟩ instance list : primcodable (list α) := ⟨ by letI H := primcodable.prim (list ℕ); exact have primrec₂ (λ (a : α) (o : option (list ℕ)), o.map (list.cons (encode a))), from option_map snd $ (list_cons' H).comp ((@primrec.encode α _).comp (fst.comp fst)) snd, have primrec (λ n, (of_nat (list ℕ) n).reverse.foldl (λ o m, (decode α m).bind (λ a, o.map (list.cons (encode a)))) (some [])), from list_foldl' H ((list_reverse' H).comp (primrec.of_nat (list ℕ))) (const (some [])) (primrec.comp₂ (bind_decode_iff.2 $ primrec₂.swap this) primrec₂.right), nat_iff.1 $ (encode_iff.2 this).of_eq $ λ n, begin rw list.foldl_reverse, apply nat.case_strong_induction_on n, { simp }, intros n IH, simp, cases decode α n.unpair.1 with a, {refl}, simp, suffices : ∀ (o : option (list ℕ)) p (_ : encode o = encode p), encode (option.map (list.cons (encode a)) o) = encode (option.map (list.cons a) p), from this _ _ (IH _ (nat.unpair_right_le n)), intros o p IH, cases o; cases p; injection IH with h, exact congr_arg (λ k, (nat.mkpair (encode a) k).succ.succ) h end⟩ end primcodable namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] theorem sum_inl : primrec (@sum.inl α β) := encode_iff.1 $ nat_bit0.comp primrec.encode theorem sum_inr : primrec (@sum.inr α β) := encode_iff.1 $ nat_bit1.comp primrec.encode theorem sum_cases {f : α → β ⊕ γ} {g : α → β → σ} {h : α → γ → σ} (hf : primrec f) (hg : primrec₂ g) (hh : primrec₂ h) : @primrec _ σ _ _ (λ a, sum.cases_on (f a) (g a) (h a)) := option_some_iff.1 $ (cond (nat_bodd.comp $ encode_iff.2 hf) (option_map (primrec.decode.comp $ nat_div2.comp $ encode_iff.2 hf) hh) (option_map (primrec.decode.comp $ nat_div2.comp $ encode_iff.2 hf) hg)).of_eq $ λ a, by cases f a with b c; simp [nat.div2_bit, nat.bodd_bit, encodek]; refl theorem list_cons : primrec₂ (@list.cons α) := list_cons' (primcodable.prim _) theorem list_cases {f : α → list β} {g : α → σ} {h : α → β × list β → σ} : primrec f → primrec g → primrec₂ h → @primrec _ σ _ _ (λ a, list.cases_on (f a) (g a) (λ b l, h a (b, l))) := list_cases' (primcodable.prim _) theorem list_foldl {f : α → list β} {g : α → σ} {h : α → σ × β → σ} : primrec f → primrec g → primrec₂ h → primrec (λ a, (f a).foldl (λ s b, h a (s, b)) (g a)) := list_foldl' (primcodable.prim _) theorem list_reverse : primrec (@list.reverse α) := list_reverse' (primcodable.prim _) theorem list_foldr {f : α → list β} {g : α → σ} {h : α → β × σ → σ} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).foldr (λ b s, h a (b, s)) (g a)) := (list_foldl (list_reverse.comp hf) hg $ to₂ $ hh.comp fst $ (pair snd fst).comp snd).of_eq $ λ a, by simp [list.foldl_reverse] theorem list_head' : primrec (@list.head' α) := (list_cases primrec.id (const none) (option_some_iff.2 $ (fst.comp snd)).to₂).of_eq $ λ l, by cases l; refl theorem list_head [inhabited α] : primrec (@list.head α _) := (option_iget.comp list_head').of_eq $ λ l, l.head_eq_head'.symm theorem list_tail : primrec (@list.tail α) := (list_cases primrec.id (const []) (snd.comp snd).to₂).of_eq $ λ l, by cases l; refl theorem list_rec {f : α → list β} {g : α → σ} {h : α → β × list β × σ → σ} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : @primrec _ σ _ _ (λ a, list.rec_on (f a) (g a) (λ b l IH, h a (b, l, IH))) := let F (a : α) := (f a).foldr (λ (b : β) (s : list β × σ), (b :: s.1, h a (b, s))) ([], g a) in have primrec F, from list_foldr hf (pair (const []) hg) $ to₂ $ pair ((list_cons.comp fst (fst.comp snd)).comp snd) hh, (snd.comp this).of_eq $ λ a, begin suffices : F a = (f a, list.rec_on (f a) (g a) (λ b l IH, h a (b, l, IH))), {rw this}, simp [F], induction f a with b l IH; simp * end theorem list_nth : primrec₂ (@list.nth α) := let F (l : list α) (n : ℕ) := l.foldl (λ (s : ℕ ⊕ α) (a : α), sum.cases_on s (@nat.cases (ℕ ⊕ α) (sum.inr a) sum.inl) sum.inr) (sum.inl n) in have hF : primrec₂ F, from list_foldl fst (sum_inl.comp snd) ((sum_cases fst (nat_cases snd (sum_inr.comp $ snd.comp fst) (sum_inl.comp snd).to₂).to₂ (sum_inr.comp snd).to₂).comp snd).to₂, have @primrec _ (option α) _ _ (λ p : list α × ℕ, sum.cases_on (F p.1 p.2) (λ _, none) some), from sum_cases hF (const none).to₂ (option_some.comp snd).to₂, this.to₂.of_eq $ λ l n, begin dsimp, symmetry, induction l with a l IH generalizing n, {refl}, cases n with n, { rw [(_ : F (a :: l) 0 = sum.inr a)], {refl}, clear IH, dsimp [F], induction l with b l IH; simp * }, { apply IH } end theorem list_inth [inhabited α] : primrec₂ (@list.inth α _) := option_iget.comp₂ list_nth theorem list_append : primrec₂ ((++) : list α → list α → list α) := (list_foldr fst snd $ to₂ $ comp (@list_cons α _) snd).to₂.of_eq $ λ l₁ l₂, by induction l₁; simp * theorem list_concat : primrec₂ (λ l (a:α), l ++ [a]) := list_append.comp fst (list_cons.comp snd (const [])) theorem list_map {f : α → list β} {g : α → β → σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).map (g a)) := (list_foldr hf (const []) $ to₂ $ list_cons.comp (hg.comp fst (fst.comp snd)) (snd.comp snd)).of_eq $ λ a, by induction f a; simp * theorem list_range : primrec list.range := (nat_elim' primrec.id (const []) ((list_concat.comp snd fst).comp snd).to₂).of_eq $ λ n, by simp; induction n; simp [*, list.range_succ]; refl theorem list_join : primrec (@list.join α) := (list_foldr primrec.id (const []) $ to₂ $ comp (@list_append α _) snd).of_eq $ λ l, by dsimp; induction l; simp * theorem list_length : primrec (@list.length α) := (list_foldr (@primrec.id (list α) _) (const 0) $ to₂ $ (succ.comp $ snd.comp snd).to₂).of_eq $ λ l, by dsimp; induction l; simp [*, -add_comm] theorem list_find_index {f : α → list β} {p : α → β → Prop} [∀ a b, decidable (p a b)] (hf : primrec f) (hp : primrec_rel p) : primrec (λ a, (f a).find_index (p a)) := (list_foldr hf (const 0) $ to₂ $ ite (hp.comp fst $ fst.comp snd) (const 0) (succ.comp $ snd.comp snd)).of_eq $ λ a, eq.symm $ by dsimp; induction f a with b l; [refl, simp [*, list.find_index]] theorem list_index_of [decidable_eq α] : primrec₂ (@list.index_of α _) := to₂ $ list_find_index snd $ primrec.eq.comp₂ (fst.comp fst).to₂ snd.to₂ theorem nat_strong_rec (f : α → ℕ → σ) {g : α → list σ → option σ} (hg : primrec₂ g) (H : ∀ a n, g a ((list.range n).map (f a)) = some (f a n)) : primrec₂ f := suffices primrec₂ (λ a n, (list.range n).map (f a)), from primrec₂.option_some_iff.1 $ (list_nth.comp (this.comp fst (succ.comp snd)) snd).to₂.of_eq $ λ a n, by simp [list.nth_range (nat.lt_succ_self n)]; refl, primrec₂.option_some_iff.1 $ (nat_elim (const (some [])) (to₂ $ option_bind (snd.comp snd) $ to₂ $ option_map (hg.comp (fst.comp fst) snd) (to₂ $ list_concat.comp (snd.comp fst) snd))).of_eq $ λ a n, begin simp, induction n with n IH, {refl}, simp [IH, H, list.range_succ] end end primrec namespace primcodable variables {α : Type*} {β : Type*} variables [primcodable α] [primcodable β] open primrec def subtype {p : α → Prop} [decidable_pred p] (hp : primrec_pred p) : primcodable (subtype p) := ⟨have primrec (λ n, (decode α n).bind (λ a, option.guard p a)), from option_bind primrec.decode (option_guard (hp.comp snd) snd), nat_iff.1 $ (encode_iff.2 this).of_eq $ λ n, show _ = encode ((decode α n).bind (λ a, _)), begin cases decode α n with a, {refl}, dsimp [option.guard], by_cases h : p a; simp [h]; refl end⟩ instance fin {n} : primcodable (fin n) := @of_equiv _ _ (subtype $ nat_lt.comp primrec.id (const n)) (equiv.fin_equiv_subtype _) instance vector {n} : primcodable (vector α n) := subtype ((@primrec.eq _ _ nat.decidable_eq).comp list_length (const _)) instance fin_arrow {n} : primcodable (fin n → α) := of_equiv _ (equiv.vector_equiv_fin _ _).symm instance array {n} : primcodable (array n α) := of_equiv _ (equiv.array_equiv_fin _ _) section ulower local attribute [instance, priority 100] encodable.decidable_range_encode encodable.decidable_eq_of_encodable instance ulower : primcodable (ulower α) := have primrec_pred (λ n, encodable.decode₂ α n ≠ none), from primrec.not (primrec.eq.comp (primrec.option_bind primrec.decode (primrec.ite (primrec.eq.comp (primrec.encode.comp primrec.snd) primrec.fst) (primrec.option_some.comp primrec.snd) (primrec.const _))) (primrec.const _)), primcodable.subtype $ primrec_pred.of_eq this (λ n, decode₂_ne_none_iff) end ulower end primcodable namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] theorem subtype_val {p : α → Prop} [decidable_pred p] {hp : primrec_pred p} : by haveI := primcodable.subtype hp; exact primrec (@subtype.val α p) := begin letI := primcodable.subtype hp, refine (primcodable.prim (subtype p)).of_eq (λ n, _), rcases decode (subtype p) n with _|⟨a,h⟩; refl end theorem subtype_val_iff {p : β → Prop} [decidable_pred p] {hp : primrec_pred p} {f : α → subtype p} : by haveI := primcodable.subtype hp; exact primrec (λ a, (f a).1) ↔ primrec f := begin letI := primcodable.subtype hp, refine ⟨λ h, _, λ hf, subtype_val.comp hf⟩, refine nat.primrec.of_eq h (λ n, _), cases decode α n with a, {refl}, simp, cases f a; refl end theorem subtype_mk {p : β → Prop} [decidable_pred p] {hp : primrec_pred p} {f : α → β} {h : ∀ a, p (f a)} (hf : primrec f) : by haveI := primcodable.subtype hp; exact primrec (λ a, @subtype.mk β p (f a) (h a)) := subtype_val_iff.1 hf theorem option_get {f : α → option β} {h : ∀ a, (f a).is_some} : primrec f → primrec (λ a, option.get (h a)) := begin intro hf, refine (nat.primrec.pred.comp hf).of_eq (λ n, _), generalize hx : decode α n = x, cases x; simp end theorem ulower_down : primrec (ulower.down : α → ulower α) := by letI : ∀ a, decidable (a ∈ set.range (encode : α → ℕ)) := decidable_range_encode _; exact subtype_mk primrec.encode theorem ulower_up : primrec (ulower.up : ulower α → α) := by letI : ∀ a, decidable (a ∈ set.range (encode : α → ℕ)) := decidable_range_encode _; exact option_get (primrec.decode₂.comp subtype_val) theorem fin_val_iff {n} {f : α → fin n} : primrec (λ a, (f a).1) ↔ primrec f := begin let : primcodable {a//id a<n}, swap, exactI (iff.trans (by refl) subtype_val_iff).trans (of_equiv_iff _) end theorem fin_val {n} : primrec (coe : fin n → ℕ) := fin_val_iff.2 primrec.id theorem fin_succ {n} : primrec (@fin.succ n) := fin_val_iff.1 $ by simp [succ.comp fin_val] theorem vector_to_list {n} : primrec (@vector.to_list α n) := subtype_val theorem vector_to_list_iff {n} {f : α → vector β n} : primrec (λ a, (f a).to_list) ↔ primrec f := subtype_val_iff theorem vector_cons {n} : primrec₂ (@vector.cons α n) := vector_to_list_iff.1 $ by simp; exact list_cons.comp fst (vector_to_list_iff.2 snd) theorem vector_length {n} : primrec (@vector.length α n) := const _ theorem vector_head {n} : primrec (@vector.head α n) := option_some_iff.1 $ (list_head'.comp vector_to_list).of_eq $ λ ⟨a::l, h⟩, rfl theorem vector_tail {n} : primrec (@vector.tail α n) := vector_to_list_iff.1 $ (list_tail.comp vector_to_list).of_eq $ λ ⟨l, h⟩, by cases l; refl theorem vector_nth {n} : primrec₂ (@vector.nth α n) := option_some_iff.1 $ (list_nth.comp (vector_to_list.comp fst) (fin_val.comp snd)).of_eq $ λ a, by simp [vector.nth_eq_nth_le]; rw [← list.nth_le_nth] theorem list_of_fn : ∀ {n} {f : fin n → α → σ}, (∀ i, primrec (f i)) → primrec (λ a, list.of_fn (λ i, f i a)) | 0 f hf := const [] | (n+1) f hf := by simp [list.of_fn_succ]; exact list_cons.comp (hf 0) (list_of_fn (λ i, hf i.succ)) theorem vector_of_fn {n} {f : fin n → α → σ} (hf : ∀ i, primrec (f i)) : primrec (λ a, vector.of_fn (λ i, f i a)) := vector_to_list_iff.1 $ by simp [list_of_fn hf] theorem vector_nth' {n} : primrec (@vector.nth α n) := of_equiv_symm theorem vector_of_fn' {n} : primrec (@vector.of_fn α n) := of_equiv theorem fin_app {n} : primrec₂ (@id (fin n → σ)) := (vector_nth.comp (vector_of_fn'.comp fst) snd).of_eq $ λ ⟨v, i⟩, by simp theorem fin_curry₁ {n} {f : fin n → α → σ} : primrec₂ f ↔ ∀ i, primrec (f i) := ⟨λ h i, h.comp (const i) primrec.id, λ h, (vector_nth.comp ((vector_of_fn h).comp snd) fst).of_eq $ λ a, by simp⟩ theorem fin_curry {n} {f : α → fin n → σ} : primrec f ↔ primrec₂ f := ⟨λ h, fin_app.comp (h.comp fst) snd, λ h, (vector_nth'.comp (vector_of_fn (λ i, show primrec (λ a, f a i), from h.comp primrec.id (const i)))).of_eq $ λ a, by funext i; simp⟩ end primrec namespace nat open vector /-- An alternative inductive definition of `primrec` which does not use the pairing function on ℕ, and so has to work with n-ary functions on ℕ instead of unary functions. We prove that this is equivalent to the regular notion in `to_prim` and `of_prim`. -/ inductive primrec' : ∀ {n}, (vector ℕ n → ℕ) → Prop | zero : @primrec' 0 (λ _, 0) | succ : @primrec' 1 (λ v, succ v.head) | nth {n} (i : fin n) : primrec' (λ v, v.nth i) | comp {m n f} (g : fin n → vector ℕ m → ℕ) : primrec' f → (∀ i, primrec' (g i)) → primrec' (λ a, f (of_fn (λ i, g i a))) | prec {n f g} : @primrec' n f → @primrec' (n+2) g → primrec' (λ v : vector ℕ (n+1), v.head.elim (f v.tail) (λ y IH, g (y ::ᵥ IH ::ᵥ v.tail))) end nat namespace nat.primrec' open vector primrec nat (primrec') nat.primrec' hide ite theorem to_prim {n f} (pf : @primrec' n f) : primrec f := begin induction pf, case nat.primrec'.zero { exact const 0 }, case nat.primrec'.succ { exact primrec.succ.comp vector_head }, case nat.primrec'.nth : n i { exact vector_nth.comp primrec.id (const i) }, case nat.primrec'.comp : m n f g _ _ hf hg { exact hf.comp (vector_of_fn (λ i, hg i)) }, case nat.primrec'.prec : n f g _ _ hf hg { exact nat_elim' vector_head (hf.comp vector_tail) (hg.comp $ vector_cons.comp (fst.comp snd) $ vector_cons.comp (snd.comp snd) $ (@vector_tail _ _ (n+1)).comp fst).to₂ }, end theorem of_eq {n} {f g : vector ℕ n → ℕ} (hf : primrec' f) (H : ∀ i, f i = g i) : primrec' g := (funext H : f = g) ▸ hf theorem const {n} : ∀ m, @primrec' n (λ v, m) | 0 := zero.comp fin.elim0 (λ i, i.elim0) | (m+1) := succ.comp _ (λ i, const m) theorem head {n : ℕ} : @primrec' n.succ head := (nth 0).of_eq $ λ v, by simp [nth_zero] theorem tail {n f} (hf : @primrec' n f) : @primrec' n.succ (λ v, f v.tail) := (hf.comp _ (λ i, @nth _ i.succ)).of_eq $ λ v, by rw [← of_fn_nth v.tail]; congr; funext i; simp def vec {n m} (f : vector ℕ n → vector ℕ m) := ∀ i, primrec' (λ v, (f v).nth i) protected theorem nil {n} : @vec n 0 (λ _, nil) := λ i, i.elim0 protected theorem cons {n m f g} (hf : @primrec' n f) (hg : @vec n m g) : vec (λ v, (f v ::ᵥ g v)) := λ i, fin.cases (by simp *) (λ i, by simp [hg i]) i theorem idv {n} : @vec n n id := nth theorem comp' {n m f g} (hf : @primrec' m f) (hg : @vec n m g) : primrec' (λ v, f (g v)) := (hf.comp _ hg).of_eq $ λ v, by simp theorem comp₁ (f : ℕ → ℕ) (hf : @primrec' 1 (λ v, f v.head)) {n g} (hg : @primrec' n g) : primrec' (λ v, f (g v)) := hf.comp _ (λ i, hg) theorem comp₂ (f : ℕ → ℕ → ℕ) (hf : @primrec' 2 (λ v, f v.head v.tail.head)) {n g h} (hg : @primrec' n g) (hh : @primrec' n h) : primrec' (λ v, f (g v) (h v)) := by simpa using hf.comp' (hg.cons $ hh.cons primrec'.nil) theorem prec' {n f g h} (hf : @primrec' n f) (hg : @primrec' n g) (hh : @primrec' (n+2) h) : @primrec' n (λ v, (f v).elim (g v) (λ (y IH : ℕ), h (y ::ᵥ IH ::ᵥ v))) := by simpa using comp' (prec hg hh) (hf.cons idv) theorem pred : @primrec' 1 (λ v, v.head.pred) := (prec' head (const 0) head).of_eq $ λ v, by simp; cases v.head; refl theorem add : @primrec' 2 (λ v, v.head + v.tail.head) := (prec head (succ.comp₁ _ (tail head))).of_eq $ λ v, by simp; induction v.head; simp [*, nat.succ_add] theorem sub : @primrec' 2 (λ v, v.head - v.tail.head) := begin suffices, simpa using comp₂ (λ a b, b - a) this (tail head) head, refine (prec head (pred.comp₁ _ (tail head))).of_eq (λ v, _), simp, induction v.head; simp [*, nat.sub_succ] end theorem mul : @primrec' 2 (λ v, v.head * v.tail.head) := (prec (const 0) (tail (add.comp₂ _ (tail head) (head)))).of_eq $ λ v, by simp; induction v.head; simp [*, nat.succ_mul]; rw add_comm theorem if_lt {n a b f g} (ha : @primrec' n a) (hb : @primrec' n b) (hf : @primrec' n f) (hg : @primrec' n g) : @primrec' n (λ v, if a v < b v then f v else g v) := (prec' (sub.comp₂ _ hb ha) hg (tail $ tail hf)).of_eq $ λ v, begin cases e : b v - a v, { simp [not_lt.2 (nat.le_of_sub_eq_zero e)] }, { simp [nat.lt_of_sub_eq_succ e] } end theorem mkpair : @primrec' 2 (λ v, v.head.mkpair v.tail.head) := if_lt head (tail head) (add.comp₂ _ (tail $ mul.comp₂ _ head head) head) (add.comp₂ _ (add.comp₂ _ (mul.comp₂ _ head head) head) (tail head)) protected theorem encode : ∀ {n}, @primrec' n encode | 0 := (const 0).of_eq (λ v, by rw v.eq_nil; refl) | (n+1) := (succ.comp₁ _ (mkpair.comp₂ _ head (tail encode))) .of_eq $ λ ⟨a::l, e⟩, rfl theorem sqrt : @primrec' 1 (λ v, v.head.sqrt) := begin suffices H : ∀ n : ℕ, n.sqrt = n.elim 0 (λ x y, if x.succ < y.succ*y.succ then y else y.succ), { simp [H], have := @prec' 1 _ _ (λ v, by have x := v.head; have y := v.tail.head; from if x.succ < y.succ*y.succ then y else y.succ) head (const 0) _, { convert this, funext, congr, funext x y, congr; simp }, have x1 := succ.comp₁ _ head, have y1 := succ.comp₁ _ (tail head), exact if_lt x1 (mul.comp₂ _ y1 y1) (tail head) y1 }, intro, symmetry, induction n with n IH, {simp}, dsimp, rw IH, split_ifs, { exact le_antisymm (nat.sqrt_le_sqrt (nat.le_succ _)) (nat.lt_succ_iff.1 $ nat.sqrt_lt.2 h) }, { exact nat.eq_sqrt.2 ⟨not_lt.1 h, nat.sqrt_lt.1 $ nat.lt_succ_iff.2 $ nat.sqrt_succ_le_succ_sqrt _⟩ }, end theorem unpair₁ {n f} (hf : @primrec' n f) : @primrec' n (λ v, (f v).unpair.1) := begin have s := sqrt.comp₁ _ hf, have fss := sub.comp₂ _ hf (mul.comp₂ _ s s), refine (if_lt fss s fss s).of_eq (λ v, _), simp [nat.unpair], split_ifs; refl end theorem unpair₂ {n f} (hf : @primrec' n f) : @primrec' n (λ v, (f v).unpair.2) := begin have s := sqrt.comp₁ _ hf, have fss := sub.comp₂ _ hf (mul.comp₂ _ s s), refine (if_lt fss s s (sub.comp₂ _ fss s)).of_eq (λ v, _), simp [nat.unpair], split_ifs; refl end theorem of_prim : ∀ {n f}, primrec f → @primrec' n f := suffices ∀ f, nat.primrec f → @primrec' 1 (λ v, f v.head), from λ n f hf, (pred.comp₁ _ $ (this _ hf).comp₁ (λ m, encodable.encode $ (decode (vector ℕ n) m).map f) primrec'.encode).of_eq (λ i, by simp [encodek]), λ f hf, begin induction hf, case nat.primrec.zero { exact const 0 }, case nat.primrec.succ { exact succ }, case nat.primrec.left { exact unpair₁ head }, case nat.primrec.right { exact unpair₂ head }, case nat.primrec.pair : f g _ _ hf hg { exact mkpair.comp₂ _ hf hg }, case nat.primrec.comp : f g _ _ hf hg { exact hf.comp₁ _ hg }, case nat.primrec.prec : f g _ _ hf hg { simpa using prec' (unpair₂ head) (hf.comp₁ _ (unpair₁ head)) (hg.comp₁ _ $ mkpair.comp₂ _ (unpair₁ $ tail $ tail head) (mkpair.comp₂ _ head (tail head))) }, end theorem prim_iff {n f} : @primrec' n f ↔ primrec f := ⟨to_prim, of_prim⟩ theorem prim_iff₁ {f : ℕ → ℕ} : @primrec' 1 (λ v, f v.head) ↔ primrec f := prim_iff.trans ⟨ λ h, (h.comp $ vector_of_fn $ λ i, primrec.id).of_eq (λ v, by simp), λ h, h.comp vector_head⟩ theorem prim_iff₂ {f : ℕ → ℕ → ℕ} : @primrec' 2 (λ v, f v.head v.tail.head) ↔ primrec₂ f := prim_iff.trans ⟨ λ h, (h.comp $ vector_cons.comp fst $ vector_cons.comp snd (primrec.const nil)).of_eq (λ v, by simp), λ h, h.comp vector_head (vector_head.comp vector_tail)⟩ theorem vec_iff {m n f} : @vec m n f ↔ primrec f := ⟨λ h, by simpa using vector_of_fn (λ i, to_prim (h i)), λ h i, of_prim $ vector_nth.comp h (primrec.const i)⟩ end nat.primrec' theorem primrec.nat_sqrt : primrec nat.sqrt := nat.primrec'.prim_iff₁.1 nat.primrec'.sqrt
436904fef1cb6e9a9978c14a32d84e7cf8665390
1a61aba1b67cddccce19532a9596efe44be4285f
/library/logic/quantifiers.lean
edf1c6ef226051a27741b182cafcc21cdf529470
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
3,640
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad Universal and existential quantifiers. See also init.logic. -/ import .connectives open inhabited nonempty theorem exists_imp_distrib {A : Type} {B : Prop} {P : A → Prop} : ((∃ a : A, P a) → B) ↔ (∀ a : A, P a → B) := iff.intro (λ e x H, e (exists.intro x H)) Exists.rec theorem forall_iff_not_exists {A : Type} {P : A → Prop} : (¬ ∃ a : A, P a) ↔ ∀ a : A, ¬ P a := exists_imp_distrib theorem not_forall_not_of_exists {A : Type} {p : A → Prop} (H : ∃x, p x) : ¬∀x, ¬p x := assume H1 : ∀x, ¬p x, obtain (w : A) (Hw : p w), from H, absurd Hw (H1 w) theorem not_exists_not_of_forall {A : Type} {p : A → Prop} (H2 : ∀x, p x) : ¬∃x, ¬p x := assume H1 : ∃x, ¬p x, obtain (w : A) (Hw : ¬p w), from H1, absurd (H2 w) Hw theorem forall_congr {A : Type} {φ ψ : A → Prop} : (∀x, φ x ↔ ψ x) → ((∀x, φ x) ↔ (∀x, ψ x)) := forall_iff_forall theorem exists_congr {A : Type} {φ ψ : A → Prop} : (∀x, φ x ↔ ψ x) → ((∃x, φ x) ↔ (∃x, ψ x)) := exists_iff_exists theorem forall_true_iff_true (A : Type) : (∀x : A, true) ↔ true := iff_true_intro (λH, trivial) theorem forall_p_iff_p (A : Type) [H : inhabited A] (p : Prop) : (∀x : A, p) ↔ p := iff.intro (inhabited.destruct H) (λHr x, Hr) theorem exists_p_iff_p (A : Type) [H : inhabited A] (p : Prop) : (∃x : A, p) ↔ p := iff.intro (Exists.rec (λx Hp, Hp)) (inhabited.destruct H exists.intro) theorem forall_and_distribute {A : Type} (φ ψ : A → Prop) : (∀x, φ x ∧ ψ x) ↔ (∀x, φ x) ∧ (∀x, ψ x) := iff.intro (assume H, and.intro (take x, and.left (H x)) (take x, and.right (H x))) (assume H x, and.intro (and.left H x) (and.right H x)) theorem exists_or_distribute {A : Type} (φ ψ : A → Prop) : (∃x, φ x ∨ ψ x) ↔ (∃x, φ x) ∨ (∃x, ψ x) := iff.intro (Exists.rec (λx, or.imp !exists.intro !exists.intro)) (or.rec (exists_imp_exists (λx, or.inl)) (exists_imp_exists (λx, or.inr))) section open decidable eq.ops variables {A : Type} (P : A → Prop) (a : A) [H : decidable (P a)] include H definition decidable_forall_eq [instance] : decidable (∀ x, x = a → P x) := if pa : P a then inl (λ x heq, eq.substr heq pa) else inr (not.mto (λH, H a rfl) pa) definition decidable_exists_eq [instance] : decidable (∃ x, x = a ∧ P x) := if pa : P a then inl (exists.intro a (and.intro rfl pa)) else inr (Exists.rec (λh, and.rec (λheq, eq.substr heq pa))) end /- exists_unique -/ definition exists_unique {A : Type} (p : A → Prop) := ∃x, p x ∧ ∀y, p y → y = x notation `∃!` binders `,` r:(scoped P, exists_unique P) := r theorem exists_unique.intro {A : Type} {p : A → Prop} (w : A) (H1 : p w) (H2 : ∀y, p y → y = w) : ∃!x, p x := exists.intro w (and.intro H1 H2) theorem exists_unique.elim {A : Type} {p : A → Prop} {b : Prop} (H2 : ∃!x, p x) (H1 : ∀x, p x → (∀y, p y → y = x) → b) : b := obtain w Hw, from H2, H1 w (and.left Hw) (and.right Hw) /- congruences -/ section variables {A : Type} {p₁ p₂ : A → Prop} (H : ∀x, p₁ x ↔ p₂ x) theorem congr_forall : (∀x, p₁ x) ↔ (∀x, p₂ x) := forall_congr H theorem congr_exists : (∃x, p₁ x) ↔ (∃x, p₂ x) := exists_congr H include H theorem congr_exists_unique : (∃!x, p₁ x) ↔ (∃!x, p₂ x) := congr_exists (λx, congr_and (H x) (congr_forall (λy, congr_imp (H y) iff.rfl))) end
2e66abc0a1c90862ef42f65c1505eda21b895b28
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch3/ex0304.lean
b5d7e18a055ce742cb90c39097de7f22038b4444
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
104
lean
variables p q : Prop example (h : p ∧ q) : q ∧ p := and.intro (and.elim_right h) (and.elim_left h)
756897d3c294e93a4c6c7499fb8bbe1abd043ea0
5c7fe6c4a9d4079b5457ffa5f061797d42a1cd65
/src/exercises/src_06_rewriting_implicit.lean
1986bded2505063d67fd9cfd033bc9fad771a4b3
[]
no_license
gihanmarasingha/mth1001_tutorial
8e0817feeb96e7c1bb3bac49b63e3c9a3a329061
bb277eebd5013766e1418365b91416b406275130
refs/heads/master
1,675,008,746,310
1,607,993,443,000
1,607,993,443,000
321,511,270
3
0
null
null
null
null
UTF-8
Lean
false
false
4,305
lean
import tactic.linarith import .src_05_if_and_only_if variables p q r : Prop namespace mth1001 section rewriting /- Statements of the form `p ↔ q` play a cruicial role in logic. Given `h : p ↔ q`, we can replace `p`, wherever it appears, with `q`. This is called rewriting. -/ /- Here, the `rewrite` tactic (abbreviated to `rw`) is invoked with `h : (p ∧ q) ↔ r`. This cases Lean to replace `p ∧ q`, wherever it appears in the goal, with `r`. The goal is reduced to `r ∧ q ∧ r ↔ r ∧ q ∧ r`, which Lean immediately closes. Note that a one-line tactic proof can be written using `by` rather than enclosing the proof in a `begin` … `end` block. -/ example (h : (p ∧ q) ↔ r) : (p ∧ q) ∧ (q ∧ (p ∧ q)) ↔ r ∧ (q ∧ r) := by rw h /- Below, we use `and_iff_and` (the commutativity of `∧`) together with `and_assoc` (the associativity of `∧`). Note that `and_iff_and r (q ∧ r)` is the result `r ∧ (q ∧ r) ↔ (q ∧ r) ∧ r`. -/ example (h : (p ∧ q) ↔ r) : (p ∧ q) ∧ (q ∧ (p ∧ q)) ↔ q ∧ (r ∧ r) := begin rw h, rw and_iff_and r (q ∧ r), rw and_assoc q r r, end /- The `rw` tactic can often infer arguments to theorems. In the example above, we can omit the arguments, leading to the simplified proof below. -/ example (h : (p ∧ q) ↔ r) : (p ∧ q) ∧ (q ∧ (p ∧ q)) ↔ q ∧ (r ∧ r) := begin rw h, rw and_iff_and, rw and_assoc, end -- Several applications of `rw` can be written on one line. example (h : (p ∧ q) ↔ r) : (p ∧ q) ∧ (q ∧ (p ∧ q)) ↔ q ∧ (r ∧ r) := begin rw [h, and_iff_and, and_assoc], end #check and_assoc /- We've seen how `rw` can rewrite a goal given a double implication. It can also rewrite a goal given an equation. Here, `ℤ` represents the type of integers and is written `\Z`. -/ example (a b : ℤ) (h : a = b) : a + 2 = b + 2 := begin rw h, end /- Given `h : a = b` or `h : a ↔ b`, we've seen that `rw h` replaces every occurrence of `a` in the goal with `b`. We can ask for rewriting in the other direction by issuing `rw ←h`, where `←` is written `\l`. This replaces every occurrence of `b` in the goal with `a`. -/ /- Here, we use `rw` with `←`. The `linarith` tactic can solve linear equations and inequalities. In fact, `linarith` is clever enough to perform the rewriting *without* needing you to specify it explicitly, so we could omit the line `rw ←h` below. -/ example (a b : ℤ) (h : 3 + a = b) : b + 3 = a + 6 := begin rw ←h, linarith, end -- Exercise 036: -- Use `rw` with `←` and the theorems we've proved to solve the following. example : r ∧ (p ∧ q) ↔ (p ∧ r) ∧ q := begin sorry end end rewriting section implicit_arguments -- Exercise 037: /- Often, it's easier to write a theorem in a form that doesn't require arguments to be specified when the theorem is applied. We do this by enclosing implicit arguments in braces `{` and `}`. -/ theorem imp_trans {a b c : Prop} : (a → b) → (b → c) → (a → c) := begin sorry end -- Note how the proof of the following complicated result is simply `imp_trans`. example : (p ∧ q → r) → (r → r ∧ q) → (p ∧ q → r ∧ q) := imp_trans -- In tactic-form, the proof is `exact imp_trans`. example : (p ∧ q → r) → (r → r ∧ q) → (p ∧ q → r ∧ q) := begin exact imp_trans end /- As a practical example, we'll use the following result, `add_left_cancel`, which has type `∀ {a b c : ℤ}, a + b = a + c → b = c`. -/ lemma add_left_cancel : ∀ {a b c : ℤ}, a + b = a + c → b = c := begin intros a b c, exact (add_right_inj a).mp end /- We use this result below. -/ example (x y : ℤ) (h : 2 + x = 2 + y) : x = y := add_left_cancel h -- Exercise 038: -- Check what the theorem `int.add_comm` asserts. You can prove the following use only this, -- `int.add_left_cancel`, `have`, `exact`, `rw`, and `apply`. example (s t u a b : ℤ) (h₁ : s + u = t + u) (h₂ : s + a = t + b) : a = b := begin have h₃ : u + s = u + t, { sorry, }, sorry end end implicit_arguments /- SUMMARY: * Using the `rw` tactic to rewrite a goal using an equation or iff. * Using `←` to use an equation or iff in the opposite direction. * Implicit arguments to theorems using `{ … }` -/ end mth1001
77e7fd2008434f475c688347637f3b0419ebfeaf
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/data/set/basic.lean
78d0b5da8393e8a9dd1c63f17bd4d2a9dad31e17
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
55,390
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad, Leonardo de Moura -/ import tactic.basic tactic.finish data.subtype logic.unique open function /- set coercion to a type -/ namespace set instance {α : Type*} : has_coe_to_sort (set α) := ⟨_, λ s, {x // x ∈ s}⟩ end set section set_coe universe u variables {α : Type u} theorem set.set_coe_eq_subtype (s : set α) : coe_sort.{(u+1) (u+2)} s = {x // x ∈ s} := rfl @[simp] theorem set_coe.forall {s : set α} {p : s → Prop} : (∀ x : s, p x) ↔ (∀ x (h : x ∈ s), p ⟨x, h⟩) := subtype.forall @[simp] theorem set_coe.exists {s : set α} {p : s → Prop} : (∃ x : s, p x) ↔ (∃ x (h : x ∈ s), p ⟨x, h⟩) := subtype.exists @[simp] theorem set_coe_cast : ∀ {s t : set α} (H' : s = t) (H : @eq (Type u) s t) (x : s), cast H x = ⟨x.1, H' ▸ x.2⟩ | s _ rfl _ ⟨x, h⟩ := rfl theorem set_coe.ext {s : set α} {a b : s} : (↑a : α) = ↑b → a = b := subtype.eq theorem set_coe.ext_iff {s : set α} {a b : s} : (↑a : α) = ↑b ↔ a = b := iff.intro set_coe.ext (assume h, h ▸ rfl) end set_coe lemma subtype.mem {α : Type*} {s : set α} (p : s) : (p : α) ∈ s := p.property namespace set universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a : α} {s t : set α} instance : inhabited (set α) := ⟨∅⟩ @[extensionality] theorem ext {a b : set α} (h : ∀ x, x ∈ a ↔ x ∈ b) : a = b := funext (assume x, propext (h x)) theorem ext_iff (s t : set α) : s = t ↔ ∀ x, x ∈ s ↔ x ∈ t := ⟨λ h x, by rw h, ext⟩ @[trans] theorem mem_of_mem_of_subset {α : Type u} {x : α} {s t : set α} (hx : x ∈ s) (h : s ⊆ t) : x ∈ t := h hx /- mem and set_of -/ @[simp] theorem mem_set_of_eq {a : α} {p : α → Prop} : a ∈ {a | p a} = p a := rfl @[simp] theorem nmem_set_of_eq {a : α} {P : α → Prop} : a ∉ {a : α | P a} = ¬ P a := rfl @[simp] theorem set_of_mem_eq {s : set α} : {x | x ∈ s} = s := rfl theorem mem_def {a : α} {s : set α} : a ∈ s ↔ s a := iff.rfl instance decidable_mem (s : set α) [H : decidable_pred s] : ∀ a, decidable (a ∈ s) := H instance decidable_set_of (p : α → Prop) [H : decidable_pred p] : decidable_pred {a | p a} := H @[simp] theorem set_of_subset_set_of {p q : α → Prop} : {a | p a} ⊆ {a | q a} ↔ (∀a, p a → q a) := iff.rfl @[simp] lemma sep_set_of {α} {p q : α → Prop} : {a ∈ {a | p a } | q a} = {a | p a ∧ q a} := rfl @[simp] lemma set_of_mem {α} {s : set α} : {a | a ∈ s} = s := rfl /- subset -/ -- TODO(Jeremy): write a tactic to unfold specific instances of generic notation? theorem subset_def {s t : set α} : (s ⊆ t) = ∀ x, x ∈ s → x ∈ t := rfl @[refl] theorem subset.refl (a : set α) : a ⊆ a := assume x, id @[trans] theorem subset.trans {a b c : set α} (ab : a ⊆ b) (bc : b ⊆ c) : a ⊆ c := assume x h, bc (ab h) @[trans] theorem mem_of_eq_of_mem {α : Type u} {x y : α} {s : set α} (hx : x = y) (h : y ∈ s) : x ∈ s := hx.symm ▸ h theorem subset.antisymm {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := ext (λ x, iff.intro (λ ina, h₁ ina) (λ inb, h₂ inb)) theorem subset.antisymm_iff {a b : set α} : a = b ↔ a ⊆ b ∧ b ⊆ a := ⟨λ e, e ▸ ⟨subset.refl _, subset.refl _⟩, λ ⟨h₁, h₂⟩, subset.antisymm h₁ h₂⟩ -- an alterantive name theorem eq_of_subset_of_subset {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := subset.antisymm h₁ h₂ theorem mem_of_subset_of_mem {s₁ s₂ : set α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := assume h₁ h₂, h₁ h₂ theorem not_subset : (¬ s ⊆ t) ↔ ∃a, a ∈ s ∧ a ∉ t := by simp [subset_def, classical.not_forall] /- strict subset -/ /-- `s ⊂ t` means that `s` is a strict subset of `t`, that is, `s ⊆ t` but `s ≠ t`. -/ def strict_subset (s t : set α) := s ⊆ t ∧ s ≠ t instance : has_ssubset (set α) := ⟨strict_subset⟩ theorem ssubset_def : (s ⊂ t) = (s ⊆ t ∧ s ≠ t) := rfl lemma exists_of_ssubset {α : Type u} {s t : set α} (h : s ⊂ t) : (∃x∈t, x ∉ s) := classical.by_contradiction $ assume hn, have t ⊆ s, from assume a hat, classical.by_contradiction $ assume has, hn ⟨a, hat, has⟩, h.2 $ subset.antisymm h.1 this lemma ssubset_iff_subset_not_subset {s t : set α} : s ⊂ t ↔ s ⊆ t ∧ ¬ t ⊆ s := by split; simp [set.ssubset_def, ne.def, set.subset.antisymm_iff] {contextual := tt} theorem not_mem_empty (x : α) : ¬ (x ∈ (∅ : set α)) := assume h : x ∈ ∅, h @[simp] theorem not_not_mem [decidable (a ∈ s)] : ¬ (a ∉ s) ↔ a ∈ s := not_not /- empty set -/ theorem empty_def : (∅ : set α) = {x | false} := rfl @[simp] theorem mem_empty_eq (x : α) : x ∈ (∅ : set α) = false := rfl @[simp] theorem set_of_false : {a : α | false} = ∅ := rfl theorem eq_empty_iff_forall_not_mem {s : set α} : s = ∅ ↔ ∀ x, x ∉ s := by simp [ext_iff] theorem ne_empty_of_mem {s : set α} {x : α} (h : x ∈ s) : s ≠ ∅ := by { intro hs, rw hs at h, apply not_mem_empty _ h } @[simp] theorem empty_subset (s : set α) : ∅ ⊆ s := assume x, assume h, false.elim h theorem subset_empty_iff {s : set α} : s ⊆ ∅ ↔ s = ∅ := by simp [subset.antisymm_iff] theorem eq_empty_of_subset_empty {s : set α} : s ⊆ ∅ → s = ∅ := subset_empty_iff.1 theorem ne_empty_iff_exists_mem {s : set α} : s ≠ ∅ ↔ ∃ x, x ∈ s := by haveI := classical.prop_decidable; simp [eq_empty_iff_forall_not_mem] theorem exists_mem_of_ne_empty {s : set α} : s ≠ ∅ → ∃ x, x ∈ s := ne_empty_iff_exists_mem.1 theorem coe_nonempty_iff_ne_empty {s : set α} : nonempty s ↔ s ≠ ∅ := nonempty_subtype.trans ne_empty_iff_exists_mem.symm -- TODO: remove when simplifier stops rewriting `a ≠ b` to `¬ a = b` theorem not_eq_empty_iff_exists {s : set α} : ¬ (s = ∅) ↔ ∃ x, x ∈ s := ne_empty_iff_exists_mem theorem subset_eq_empty {s t : set α} (h : t ⊆ s) (e : s = ∅) : t = ∅ := subset_empty_iff.1 $ e ▸ h theorem subset_ne_empty {s t : set α} (h : t ⊆ s) : t ≠ ∅ → s ≠ ∅ := mt (subset_eq_empty h) theorem ball_empty_iff {p : α → Prop} : (∀ x ∈ (∅ : set α), p x) ↔ true := by simp [iff_def] /- universal set -/ theorem univ_def : @univ α = {x | true} := rfl @[simp] theorem mem_univ (x : α) : x ∈ @univ α := trivial theorem empty_ne_univ [h : inhabited α] : (∅ : set α) ≠ univ := by simp [ext_iff] @[simp] theorem subset_univ (s : set α) : s ⊆ univ := λ x H, trivial theorem univ_subset_iff {s : set α} : univ ⊆ s ↔ s = univ := by simp [subset.antisymm_iff] theorem eq_univ_of_univ_subset {s : set α} : univ ⊆ s → s = univ := univ_subset_iff.1 theorem eq_univ_iff_forall {s : set α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext_iff] theorem eq_univ_of_forall {s : set α} : (∀ x, x ∈ s) → s = univ := eq_univ_iff_forall.2 @[simp] lemma univ_eq_empty_iff {α : Type*} : (univ : set α) = ∅ ↔ ¬ nonempty α := eq_empty_iff_forall_not_mem.trans ⟨λ H ⟨x⟩, H x trivial, λ H x _, H ⟨x⟩⟩ lemma nonempty_iff_univ_ne_empty {α : Type*} : nonempty α ↔ (univ : set α) ≠ ∅ := by classical; exact iff_not_comm.1 univ_eq_empty_iff lemma exists_mem_of_nonempty (α) : ∀ [nonempty α], ∃x:α, x ∈ (univ : set α) | ⟨x⟩ := ⟨x, trivial⟩ @[simp] lemma univ_ne_empty {α} [h : nonempty α] : (univ : set α) ≠ ∅ := λ e, univ_eq_empty_iff.1 e h instance univ_decidable : decidable_pred (@set.univ α) := λ x, is_true trivial /- union -/ theorem union_def {s₁ s₂ : set α} : s₁ ∪ s₂ = {a | a ∈ s₁ ∨ a ∈ s₂} := rfl theorem mem_union_left {x : α} {a : set α} (b : set α) : x ∈ a → x ∈ a ∪ b := or.inl theorem mem_union_right {x : α} {b : set α} (a : set α) : x ∈ b → x ∈ a ∪ b := or.inr theorem mem_or_mem_of_mem_union {x : α} {a b : set α} (H : x ∈ a ∪ b) : x ∈ a ∨ x ∈ b := H theorem mem_union.elim {x : α} {a b : set α} {P : Prop} (H₁ : x ∈ a ∪ b) (H₂ : x ∈ a → P) (H₃ : x ∈ b → P) : P := or.elim H₁ H₂ H₃ theorem mem_union (x : α) (a b : set α) : x ∈ a ∪ b ↔ x ∈ a ∨ x ∈ b := iff.rfl @[simp] theorem mem_union_eq (x : α) (a b : set α) : x ∈ a ∪ b = (x ∈ a ∨ x ∈ b) := rfl @[simp] theorem union_self (a : set α) : a ∪ a = a := ext (assume x, or_self _) @[simp] theorem union_empty (a : set α) : a ∪ ∅ = a := ext (assume x, or_false _) @[simp] theorem empty_union (a : set α) : ∅ ∪ a = a := ext (assume x, false_or _) theorem union_comm (a b : set α) : a ∪ b = b ∪ a := ext (assume x, or.comm) theorem union_assoc (a b c : set α) : (a ∪ b) ∪ c = a ∪ (b ∪ c) := ext (assume x, or.assoc) instance union_is_assoc : is_associative (set α) (∪) := ⟨union_assoc⟩ instance union_is_comm : is_commutative (set α) (∪) := ⟨union_comm⟩ theorem union_left_comm (s₁ s₂ s₃ : set α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := by finish theorem union_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by finish theorem union_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∪ t = t := by finish [subset_def, ext_iff, iff_def] theorem union_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∪ t = s := by finish [subset_def, ext_iff, iff_def] @[simp] theorem subset_union_left (s t : set α) : s ⊆ s ∪ t := λ x, or.inl @[simp] theorem subset_union_right (s t : set α) : t ⊆ s ∪ t := λ x, or.inr theorem union_subset {s t r : set α} (sr : s ⊆ r) (tr : t ⊆ r) : s ∪ t ⊆ r := by finish [subset_def, union_def] @[simp] theorem union_subset_iff {s t u : set α} : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := by finish [iff_def, subset_def] theorem union_subset_union {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ s₂) (h₂ : t₁ ⊆ t₂) : s₁ ∪ t₁ ⊆ s₂ ∪ t₂ := by finish [subset_def] theorem union_subset_union_left {s₁ s₂ : set α} (t) (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t := union_subset_union h (by refl) theorem union_subset_union_right (s) {t₁ t₂ : set α} (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ := union_subset_union (by refl) h lemma subset_union_of_subset_left {s t : set α} (h : s ⊆ t) (u : set α) : s ⊆ t ∪ u := subset.trans h (subset_union_left t u) lemma subset_union_of_subset_right {s u : set α} (h : s ⊆ u) (t : set α) : s ⊆ t ∪ u := subset.trans h (subset_union_right t u) @[simp] theorem union_empty_iff {s t : set α} : s ∪ t = ∅ ↔ s = ∅ ∧ t = ∅ := ⟨by finish [ext_iff], by finish [ext_iff]⟩ /- intersection -/ theorem inter_def {s₁ s₂ : set α} : s₁ ∩ s₂ = {a | a ∈ s₁ ∧ a ∈ s₂} := rfl theorem mem_inter_iff (x : α) (a b : set α) : x ∈ a ∩ b ↔ x ∈ a ∧ x ∈ b := iff.rfl @[simp] theorem mem_inter_eq (x : α) (a b : set α) : x ∈ a ∩ b = (x ∈ a ∧ x ∈ b) := rfl theorem mem_inter {x : α} {a b : set α} (ha : x ∈ a) (hb : x ∈ b) : x ∈ a ∩ b := ⟨ha, hb⟩ theorem mem_of_mem_inter_left {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ a := h.left theorem mem_of_mem_inter_right {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ b := h.right @[simp] theorem inter_self (a : set α) : a ∩ a = a := ext (assume x, and_self _) @[simp] theorem inter_empty (a : set α) : a ∩ ∅ = ∅ := ext (assume x, and_false _) @[simp] theorem empty_inter (a : set α) : ∅ ∩ a = ∅ := ext (assume x, false_and _) theorem inter_comm (a b : set α) : a ∩ b = b ∩ a := ext (assume x, and.comm) theorem inter_assoc (a b c : set α) : (a ∩ b) ∩ c = a ∩ (b ∩ c) := ext (assume x, and.assoc) instance inter_is_assoc : is_associative (set α) (∩) := ⟨inter_assoc⟩ instance inter_is_comm : is_commutative (set α) (∩) := ⟨inter_comm⟩ theorem inter_left_comm (s₁ s₂ s₃ : set α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := by finish theorem inter_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := by finish @[simp] theorem inter_subset_left (s t : set α) : s ∩ t ⊆ s := λ x H, and.left H @[simp] theorem inter_subset_right (s t : set α) : s ∩ t ⊆ t := λ x H, and.right H theorem subset_inter {s t r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t := by finish [subset_def, inter_def] @[simp] theorem subset_inter_iff {s t r : set α} : r ⊆ s ∩ t ↔ r ⊆ s ∧ r ⊆ t := ⟨λ h, ⟨subset.trans h (inter_subset_left _ _), subset.trans h (inter_subset_right _ _)⟩, λ ⟨h₁, h₂⟩, subset_inter h₁ h₂⟩ @[simp] theorem inter_univ (a : set α) : a ∩ univ = a := ext (assume x, and_true _) @[simp] theorem univ_inter (a : set α) : univ ∩ a = a := ext (assume x, true_and _) theorem inter_subset_inter_left {s t : set α} (u : set α) (H : s ⊆ t) : s ∩ u ⊆ t ∩ u := by finish [subset_def] theorem inter_subset_inter_right {s t : set α} (u : set α) (H : s ⊆ t) : u ∩ s ⊆ u ∩ t := by finish [subset_def] theorem inter_subset_inter {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ ∩ s₂ ⊆ t₁ ∩ t₂ := by finish [subset_def] theorem inter_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∩ t = s := by finish [subset_def, ext_iff, iff_def] theorem inter_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∩ t = t := by finish [subset_def, ext_iff, iff_def] theorem union_inter_cancel_left {s t : set α} : (s ∪ t) ∩ s = s := by finish [ext_iff, iff_def] theorem union_inter_cancel_right {s t : set α} : (s ∪ t) ∩ t = t := by finish [ext_iff, iff_def] -- TODO(Mario): remove? theorem nonempty_of_inter_nonempty_right {s t : set α} (h : s ∩ t ≠ ∅) : t ≠ ∅ := by finish [ext_iff, iff_def] theorem nonempty_of_inter_nonempty_left {s t : set α} (h : s ∩ t ≠ ∅) : s ≠ ∅ := by finish [ext_iff, iff_def] /- distributivity laws -/ theorem inter_distrib_left (s t u : set α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext (assume x, and_or_distrib_left) theorem inter_distrib_right (s t u : set α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext (assume x, or_and_distrib_right) theorem union_distrib_left (s t u : set α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext (assume x, or_and_distrib_left) theorem union_distrib_right (s t u : set α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext (assume x, and_or_distrib_right) /- insert -/ theorem insert_def (x : α) (s : set α) : insert x s = { y | y = x ∨ y ∈ s } := rfl @[simp] theorem insert_of_has_insert (x : α) (s : set α) : has_insert.insert x s = insert x s := rfl @[simp] theorem subset_insert (x : α) (s : set α) : s ⊆ insert x s := assume y ys, or.inr ys theorem mem_insert (x : α) (s : set α) : x ∈ insert x s := or.inl rfl theorem mem_insert_of_mem {x : α} {s : set α} (y : α) : x ∈ s → x ∈ insert y s := or.inr theorem eq_or_mem_of_mem_insert {x a : α} {s : set α} : x ∈ insert a s → x = a ∨ x ∈ s := id theorem mem_of_mem_insert_of_ne {x a : α} {s : set α} (xin : x ∈ insert a s) : x ≠ a → x ∈ s := by finish [insert_def] @[simp] theorem mem_insert_iff {x a : α} {s : set α} : x ∈ insert a s ↔ (x = a ∨ x ∈ s) := iff.rfl @[simp] theorem insert_eq_of_mem {a : α} {s : set α} (h : a ∈ s) : insert a s = s := by finish [ext_iff, iff_def] theorem insert_subset : insert a s ⊆ t ↔ (a ∈ t ∧ s ⊆ t) := by simp [subset_def, or_imp_distrib, forall_and_distrib] theorem insert_subset_insert (h : s ⊆ t) : insert a s ⊆ insert a t := assume a', or.imp_right (@h a') theorem ssubset_insert {s : set α} {a : α} (h : a ∉ s) : s ⊂ insert a s := by finish [ssubset_def, ext_iff] theorem insert_comm (a b : α) (s : set α) : insert a (insert b s) = insert b (insert a s) := ext $ by simp [or.left_comm] theorem insert_union : insert a s ∪ t = insert a (s ∪ t) := ext $ assume a, by simp [or.comm, or.left_comm] @[simp] theorem union_insert : s ∪ insert a t = insert a (s ∪ t) := ext $ assume a, by simp [or.comm, or.left_comm] -- TODO(Jeremy): make this automatic theorem insert_ne_empty (a : α) (s : set α) : insert a s ≠ ∅ := by safe [ext_iff, iff_def]; have h' := a_1 a; finish -- useful in proofs by induction theorem forall_of_forall_insert {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ insert a s → P x) : ∀ x, x ∈ s → P x := by finish theorem forall_insert_of_forall {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ s → P x) (ha : P a) : ∀ x, x ∈ insert a s → P x := by finish theorem ball_insert_iff {P : α → Prop} {a : α} {s : set α} : (∀ x ∈ insert a s, P x) ↔ P a ∧ (∀x ∈ s, P x) := by finish [iff_def] /- singletons -/ theorem singleton_def (a : α) : ({a} : set α) = insert a ∅ := rfl @[simp] theorem mem_singleton_iff {a b : α} : a ∈ ({b} : set α) ↔ a = b := by finish [singleton_def] lemma set_of_eq_eq_singleton {a : α} : {n | n = a} = {a} := set.ext $ λ n, (set.mem_singleton_iff).symm -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : α) : a ∈ ({a} : set α) := by finish theorem eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : set α)) : x = y := by finish @[simp] theorem singleton_eq_singleton_iff {x y : α} : {x} = ({y} : set α) ↔ x = y := by finish [ext_iff, iff_def] theorem mem_singleton_of_eq {x y : α} (H : x = y) : x ∈ ({y} : set α) := by finish theorem insert_eq (x : α) (s : set α) : insert x s = ({x} : set α) ∪ s := by finish [ext_iff, or_comm] @[simp] theorem pair_eq_singleton (a : α) : ({a, a} : set α) = {a} := by finish @[simp] theorem singleton_ne_empty (a : α) : ({a} : set α) ≠ ∅ := insert_ne_empty _ _ @[simp] theorem singleton_subset_iff {a : α} {s : set α} : {a} ⊆ s ↔ a ∈ s := ⟨λh, h (by simp), λh b e, by simp at e; simp [*]⟩ theorem set_compr_eq_eq_singleton {a : α} : {b | b = a} = {a} := ext $ by simp @[simp] theorem union_singleton : s ∪ {a} = insert a s := by simp [singleton_def] @[simp] theorem singleton_union : {a} ∪ s = insert a s := by rw [union_comm, union_singleton] theorem singleton_inter_eq_empty : {a} ∩ s = ∅ ↔ a ∉ s := by simp [eq_empty_iff_forall_not_mem] theorem inter_singleton_eq_empty : s ∩ {a} = ∅ ↔ a ∉ s := by rw [inter_comm, singleton_inter_eq_empty] lemma nmem_singleton_empty {s : set α} : s ∉ ({∅} : set (set α)) ↔ nonempty s := by simp [coe_nonempty_iff_ne_empty] instance unique_singleton {α : Type*} (a : α) : unique ↥({a} : set α) := { default := ⟨a, mem_singleton a⟩, uniq := begin intros x, apply subtype.coe_ext.2, apply eq_of_mem_singleton (subtype.mem x), end} /- separation -/ theorem mem_sep {s : set α} {p : α → Prop} {x : α} (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} := ⟨xs, px⟩ @[simp] theorem mem_sep_eq {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} = (x ∈ s ∧ p x) := rfl theorem mem_sep_iff {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} ↔ x ∈ s ∧ p x := iff.rfl theorem eq_sep_of_subset {s t : set α} (ssubt : s ⊆ t) : s = {x ∈ t | x ∈ s} := by finish [ext_iff, iff_def, subset_def] theorem sep_subset (s : set α) (p : α → Prop) : {x ∈ s | p x} ⊆ s := assume x, and.left theorem forall_not_of_sep_empty {s : set α} {p : α → Prop} (h : {x ∈ s | p x} = ∅) : ∀ x ∈ s, ¬ p x := by finish [ext_iff] @[simp] lemma sep_univ {α} {p : α → Prop} : {a ∈ (univ : set α) | p a} = {a | p a} := set.ext $ by simp /- complement -/ theorem mem_compl {s : set α} {x : α} (h : x ∉ s) : x ∈ -s := h lemma compl_set_of {α} (p : α → Prop) : - {a | p a} = { a | ¬ p a } := rfl theorem not_mem_of_mem_compl {s : set α} {x : α} (h : x ∈ -s) : x ∉ s := h @[simp] theorem mem_compl_eq (s : set α) (x : α) : x ∈ -s = (x ∉ s) := rfl theorem mem_compl_iff (s : set α) (x : α) : x ∈ -s ↔ x ∉ s := iff.rfl @[simp] theorem inter_compl_self (s : set α) : s ∩ -s = ∅ := by finish [ext_iff] @[simp] theorem compl_inter_self (s : set α) : -s ∩ s = ∅ := by finish [ext_iff] @[simp] theorem compl_empty : -(∅ : set α) = univ := by finish [ext_iff] @[simp] theorem compl_union (s t : set α) : -(s ∪ t) = -s ∩ -t := by finish [ext_iff] @[simp] theorem compl_compl (s : set α) : -(-s) = s := by finish [ext_iff] -- ditto theorem compl_inter (s t : set α) : -(s ∩ t) = -s ∪ -t := by finish [ext_iff] @[simp] theorem compl_univ : -(univ : set α) = ∅ := by finish [ext_iff] lemma compl_empty_iff {s : set α} : -s = ∅ ↔ s = univ := by { split, intro h, rw [←compl_compl s, h, compl_empty], intro h, rw [h, compl_univ] } lemma compl_univ_iff {s : set α} : -s = univ ↔ s = ∅ := by rw [←compl_empty_iff, compl_compl] lemma nonempty_compl {s : set α} : nonempty (-s : set α) ↔ s ≠ univ := by { symmetry, rw [coe_nonempty_iff_ne_empty], apply not_congr, split, intro h, rw [h, compl_univ], intro h, rw [←compl_compl s, h, compl_empty] } theorem union_eq_compl_compl_inter_compl (s t : set α) : s ∪ t = -(-s ∩ -t) := by simp [compl_inter, compl_compl] theorem inter_eq_compl_compl_union_compl (s t : set α) : s ∩ t = -(-s ∪ -t) := by simp [compl_compl] @[simp] theorem union_compl_self (s : set α) : s ∪ -s = univ := by finish [ext_iff] @[simp] theorem compl_union_self (s : set α) : -s ∪ s = univ := by finish [ext_iff] theorem compl_comp_compl : compl ∘ compl = @id (set α) := funext compl_compl theorem compl_subset_comm {s t : set α} : -s ⊆ t ↔ -t ⊆ s := by haveI := classical.prop_decidable; exact forall_congr (λ a, not_imp_comm) lemma compl_subset_compl {s t : set α} : -s ⊆ -t ↔ t ⊆ s := by rw [compl_subset_comm, compl_compl] theorem compl_subset_iff_union {s t : set α} : -s ⊆ t ↔ s ∪ t = univ := iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ λ a, by haveI := classical.prop_decidable; exact or_iff_not_imp_left theorem subset_compl_comm {s t : set α} : s ⊆ -t ↔ t ⊆ -s := forall_congr $ λ a, imp_not_comm theorem subset_compl_iff_disjoint {s t : set α} : s ⊆ -t ↔ s ∩ t = ∅ := iff.trans (forall_congr $ λ a, and_imp.symm) subset_empty_iff theorem inter_subset (a b c : set α) : a ∩ b ⊆ c ↔ a ⊆ -b ∪ c := begin haveI := classical.prop_decidable, split, { intros h x xa, by_cases h' : x ∈ b, simp [h ⟨xa, h'⟩], simp [h'] }, intros h x, rintro ⟨xa, xb⟩, cases h xa, contradiction, assumption end /- set difference -/ theorem diff_eq (s t : set α) : s \ t = s ∩ -t := rfl @[simp] theorem mem_diff {s t : set α} (x : α) : x ∈ s \ t ↔ x ∈ s ∧ x ∉ t := iff.rfl theorem mem_diff_of_mem {s t : set α} {x : α} (h1 : x ∈ s) (h2 : x ∉ t) : x ∈ s \ t := ⟨h1, h2⟩ theorem mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∈ s := h.left theorem not_mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∉ t := h.right theorem union_diff_cancel {s t : set α} (h : s ⊆ t) : s ∪ (t \ s) = t := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel_left {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ s = t := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel_right {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ t = s := by finish [ext_iff, iff_def, subset_def] theorem union_diff_left {s t : set α} : (s ∪ t) \ s = t \ s := by finish [ext_iff, iff_def] theorem union_diff_right {s t : set α} : (s ∪ t) \ t = s \ t := by finish [ext_iff, iff_def] theorem union_diff_distrib {s t u : set α} : (s ∪ t) \ u = s \ u ∪ t \ u := inter_distrib_right _ _ _ theorem inter_union_distrib_left {s t u : set α} : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := set.ext $ λ _, and_or_distrib_left theorem inter_union_distrib_right {s t u : set α} : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := set.ext $ λ _, and_or_distrib_right theorem union_inter_distrib_left {s t u : set α} : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := set.ext $ λ _, or_and_distrib_left theorem union_inter_distrib_right {s t u : set α} : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := set.ext $ λ _, or_and_distrib_right theorem inter_diff_assoc (a b c : set α) : (a ∩ b) \ c = a ∩ (b \ c) := inter_assoc _ _ _ theorem inter_diff_self (a b : set α) : a ∩ (b \ a) = ∅ := by finish [ext_iff] theorem inter_union_diff (s t : set α) : (s ∩ t) ∪ (s \ t) = s := by finish [ext_iff, iff_def] theorem diff_subset (s t : set α) : s \ t ⊆ s := by finish [subset_def] theorem diff_subset_diff {s₁ s₂ t₁ t₂ : set α} : s₁ ⊆ s₂ → t₂ ⊆ t₁ → s₁ \ t₁ ⊆ s₂ \ t₂ := by finish [subset_def] theorem diff_subset_diff_left {s₁ s₂ t : set α} (h : s₁ ⊆ s₂) : s₁ \ t ⊆ s₂ \ t := diff_subset_diff h (by refl) theorem diff_subset_diff_right {s t u : set α} (h : t ⊆ u) : s \ u ⊆ s \ t := diff_subset_diff (subset.refl s) h theorem compl_eq_univ_diff (s : set α) : -s = univ \ s := by finish [ext_iff] @[simp] lemma empty_diff {α : Type*} (s : set α) : (∅ \ s : set α) = ∅ := eq_empty_of_subset_empty $ assume x ⟨hx, _⟩, hx theorem diff_eq_empty {s t : set α} : s \ t = ∅ ↔ s ⊆ t := ⟨assume h x hx, classical.by_contradiction $ assume : x ∉ t, show x ∈ (∅ : set α), from h ▸ ⟨hx, this⟩, assume h, eq_empty_of_subset_empty $ assume x ⟨hx, hnx⟩, hnx $ h hx⟩ @[simp] theorem diff_empty {s : set α} : s \ ∅ = s := ext $ assume x, ⟨assume ⟨hx, _⟩, hx, assume h, ⟨h, not_false⟩⟩ theorem diff_diff {u : set α} : s \ t \ u = s \ (t ∪ u) := ext $ by simp [not_or_distrib, and.comm, and.left_comm] lemma diff_subset_iff {s t u : set α} : s \ t ⊆ u ↔ s ⊆ t ∪ u := ⟨assume h x xs, classical.by_cases or.inl (assume nxt, or.inr (h ⟨xs, nxt⟩)), assume h x ⟨xs, nxt⟩, or.resolve_left (h xs) nxt⟩ lemma subset_insert_diff (s t : set α) : s ⊆ (s \ t) ∪ t := by rw [union_comm, ←diff_subset_iff] @[simp] lemma diff_singleton_subset_iff {x : α} {s t : set α} : s \ {x} ⊆ t ↔ s ⊆ insert x t := by { rw [←union_singleton, union_comm], apply diff_subset_iff } lemma subset_insert_diff_singleton (x : α) (s : set α) : s ⊆ insert x (s \ {x}) := by rw [←diff_singleton_subset_iff] lemma diff_subset_comm {s t u : set α} : s \ t ⊆ u ↔ s \ u ⊆ t := by rw [diff_subset_iff, diff_subset_iff, union_comm] @[simp] theorem insert_diff (h : a ∈ t) : insert a s \ t = s \ t := ext $ by intro; constructor; simp [or_imp_distrib, h] {contextual := tt} theorem union_diff_self {s t : set α} : s ∪ (t \ s) = s ∪ t := by finish [ext_iff, iff_def] theorem diff_union_self {s t : set α} : (s \ t) ∪ t = s ∪ t := by rw [union_comm, union_diff_self, union_comm] theorem diff_inter_self {a b : set α} : (b \ a) ∩ a = ∅ := ext $ by simp [iff_def] {contextual:=tt} theorem diff_eq_self {s t : set α} : s \ t = s ↔ t ∩ s ⊆ ∅ := by finish [ext_iff, iff_def, subset_def] @[simp] theorem diff_singleton_eq_self {a : α} {s : set α} (h : a ∉ s) : s \ {a} = s := diff_eq_self.2 $ by simp [singleton_inter_eq_empty.2 h] @[simp] theorem insert_diff_singleton {a : α} {s : set α} : insert a (s \ {a}) = insert a s := by simp [insert_eq, union_diff_self, -union_singleton, -singleton_union] @[simp] lemma diff_self {s : set α} : s \ s = ∅ := ext $ by simp lemma mem_diff_singleton {s s' : set α} {t : set (set α)} : s ∈ t \ {s'} ↔ (s ∈ t ∧ s ≠ s') := by simp lemma mem_diff_singleton_empty {s : set α} {t : set (set α)} : s ∈ t \ {∅} ↔ (s ∈ t ∧ nonempty s) := by simp [coe_nonempty_iff_ne_empty] /- powerset -/ theorem mem_powerset {x s : set α} (h : x ⊆ s) : x ∈ powerset s := h theorem subset_of_mem_powerset {x s : set α} (h : x ∈ powerset s) : x ⊆ s := h theorem mem_powerset_iff (x s : set α) : x ∈ powerset s ↔ x ⊆ s := iff.rfl /- inverse image -/ /-- The preimage of `s : set β` by `f : α → β`, written `f ⁻¹' s`, is the set of `x : α` such that `f x ∈ s`. -/ def preimage {α : Type u} {β : Type v} (f : α → β) (s : set β) : set α := {x | f x ∈ s} infix ` ⁻¹' `:80 := preimage section preimage variables {f : α → β} {g : β → γ} @[simp] theorem preimage_empty : f ⁻¹' ∅ = ∅ := rfl @[simp] theorem mem_preimage {s : set β} {a : α} : (a ∈ f ⁻¹' s) ↔ (f a ∈ s) := iff.rfl theorem preimage_mono {s t : set β} (h : s ⊆ t) : f ⁻¹' s ⊆ f ⁻¹' t := assume x hx, h hx @[simp] theorem preimage_univ : f ⁻¹' univ = univ := rfl @[simp] theorem preimage_inter {s t : set β} : f ⁻¹' (s ∩ t) = f ⁻¹' s ∩ f ⁻¹' t := rfl @[simp] theorem preimage_union {s t : set β} : f ⁻¹' (s ∪ t) = f ⁻¹' s ∪ f ⁻¹' t := rfl @[simp] theorem preimage_compl {s : set β} : f ⁻¹' (- s) = - (f ⁻¹' s) := rfl @[simp] theorem preimage_diff (f : α → β) (s t : set β) : f ⁻¹' (s \ t) = f ⁻¹' s \ f ⁻¹' t := rfl @[simp] theorem preimage_set_of_eq {p : α → Prop} {f : β → α} : f ⁻¹' {a | p a} = {a | p (f a)} := rfl @[simp] theorem preimage_id {s : set α} : id ⁻¹' s = s := rfl theorem preimage_comp {s : set γ} : (g ∘ f) ⁻¹' s = f ⁻¹' (g ⁻¹' s) := rfl theorem eq_preimage_subtype_val_iff {p : α → Prop} {s : set (subtype p)} {t : set α} : s = subtype.val ⁻¹' t ↔ (∀x (h : p x), (⟨x, h⟩ : subtype p) ∈ s ↔ x ∈ t) := ⟨assume s_eq x h, by rw [s_eq]; simp, assume h, ext $ assume ⟨x, hx⟩, by simp [h]⟩ end preimage /- function image -/ section image infix ` '' `:80 := image /-- Two functions `f₁ f₂ : α → β` are equal on `s` if `f₁ x = f₂ x` for all `x ∈ a`. -/ @[reducible] def eq_on (f1 f2 : α → β) (a : set α) : Prop := ∀ x ∈ a, f1 x = f2 x -- TODO(Jeremy): use bounded exists in image theorem mem_image_iff_bex {f : α → β} {s : set α} {y : β} : y ∈ f '' s ↔ ∃ x (_ : x ∈ s), f x = y := bex_def.symm theorem mem_image_eq (f : α → β) (s : set α) (y: β) : y ∈ f '' s = ∃ x, x ∈ s ∧ f x = y := rfl @[simp] theorem mem_image (f : α → β) (s : set α) (y : β) : y ∈ f '' s ↔ ∃ x, x ∈ s ∧ f x = y := iff.rfl theorem mem_image_of_mem (f : α → β) {x : α} {a : set α} (h : x ∈ a) : f x ∈ f '' a := ⟨_, h, rfl⟩ theorem mem_image_of_injective {f : α → β} {a : α} {s : set α} (hf : injective f) : f a ∈ f '' s ↔ a ∈ s := iff.intro (assume ⟨b, hb, eq⟩, (hf eq) ▸ hb) (assume h, mem_image_of_mem _ h) theorem ball_image_of_ball {f : α → β} {s : set α} {p : β → Prop} (h : ∀ x ∈ s, p (f x)) : ∀ y ∈ f '' s, p y := by finish [mem_image_eq] @[simp] theorem ball_image_iff {f : α → β} {s : set α} {p : β → Prop} : (∀ y ∈ f '' s, p y) ↔ (∀ x ∈ s, p (f x)) := iff.intro (assume h a ha, h _ $ mem_image_of_mem _ ha) (assume h b ⟨a, ha, eq⟩, eq ▸ h a ha) theorem mono_image {f : α → β} {s t : set α} (h : s ⊆ t) : f '' s ⊆ f '' t := assume x ⟨y, hy, y_eq⟩, y_eq ▸ mem_image_of_mem _ $ h hy theorem mem_image_elim {f : α → β} {s : set α} {C : β → Prop} (h : ∀ (x : α), x ∈ s → C (f x)) : ∀{y : β}, y ∈ f '' s → C y | ._ ⟨a, a_in, rfl⟩ := h a a_in theorem mem_image_elim_on {f : α → β} {s : set α} {C : β → Prop} {y : β} (h_y : y ∈ f '' s) (h : ∀ (x : α), x ∈ s → C (f x)) : C y := mem_image_elim h h_y @[congr] lemma image_congr {f g : α → β} {s : set α} (h : ∀a∈s, f a = g a) : f '' s = g '' s := by safe [ext_iff, iff_def] /- A common special case of `image_congr` -/ lemma image_congr' {f g : α → β} {s : set α} (h : ∀ (x : α), f x = g x) : f '' s = g '' s := image_congr (λx _, h x) theorem image_eq_image_of_eq_on {f₁ f₂ : α → β} {s : set α} (heq : eq_on f₁ f₂ s) : f₁ '' s = f₂ '' s := image_congr heq theorem image_comp (f : β → γ) (g : α → β) (a : set α) : (f ∘ g) '' a = f '' (g '' a) := subset.antisymm (ball_image_of_ball $ assume a ha, mem_image_of_mem _ $ mem_image_of_mem _ ha) (ball_image_of_ball $ ball_image_of_ball $ assume a ha, mem_image_of_mem _ ha) /- Proof is removed as it uses generated names TODO(Jeremy): make automatic, begin safe [ext_iff, iff_def, mem_image, (∘)], have h' := h_2 (g a_2), finish end -/ /-- A variant of `image_comp`, useful for rewriting -/ lemma image_image (g : β → γ) (f : α → β) (s : set α) : g '' (f '' s) = (λ x, g (f x)) '' s := (image_comp g f s).symm theorem image_subset {a b : set α} (f : α → β) (h : a ⊆ b) : f '' a ⊆ f '' b := by finish [subset_def, mem_image_eq] theorem image_union (f : α → β) (s t : set α) : f '' (s ∪ t) = f '' s ∪ f '' t := by finish [ext_iff, iff_def, mem_image_eq] @[simp] theorem image_empty (f : α → β) : f '' ∅ = ∅ := ext $ by simp theorem image_inter_on {f : α → β} {s t : set α} (h : ∀x∈t, ∀y∈s, f x = f y → x = y) : f '' s ∩ f '' t = f '' (s ∩ t) := subset.antisymm (assume b ⟨⟨a₁, ha₁, h₁⟩, ⟨a₂, ha₂, h₂⟩⟩, have a₂ = a₁, from h _ ha₂ _ ha₁ (by simp *), ⟨a₁, ⟨ha₁, this ▸ ha₂⟩, h₁⟩) (subset_inter (mono_image $ inter_subset_left _ _) (mono_image $ inter_subset_right _ _)) theorem image_inter {f : α → β} {s t : set α} (H : injective f) : f '' s ∩ f '' t = f '' (s ∩ t) := image_inter_on (assume x _ y _ h, H h) theorem image_univ_of_surjective {ι : Type*} {f : ι → β} (H : surjective f) : f '' univ = univ := eq_univ_of_forall $ by simp [image]; exact H @[simp] theorem image_singleton {f : α → β} {a : α} : f '' {a} = {f a} := ext $ λ x, by simp [image]; rw eq_comm @[simp] lemma image_eq_empty {α β} {f : α → β} {s : set α} : f '' s = ∅ ↔ s = ∅ := by simp only [eq_empty_iff_forall_not_mem]; exact ⟨λ H a ha, H _ ⟨_, ha, rfl⟩, λ H b ⟨_, ha, _⟩, H _ ha⟩ lemma inter_singleton_ne_empty {α : Type*} {s : set α} {a : α} : s ∩ {a} ≠ ∅ ↔ a ∈ s := by finish [set.inter_singleton_eq_empty] theorem fix_set_compl (t : set α) : compl t = - t := rfl -- TODO(Jeremy): there is an issue with - t unfolding to compl t theorem mem_compl_image (t : set α) (S : set (set α)) : t ∈ compl '' S ↔ -t ∈ S := begin suffices : ∀ x, -x = t ↔ -t = x, {simp [fix_set_compl, this]}, intro x, split; { intro e, subst e, simp } end @[simp] theorem image_id (s : set α) : id '' s = s := ext $ by simp /-- A variant of `image_id` -/ @[simp] lemma image_id' (s : set α) : (λx, x) '' s = s := image_id s theorem compl_compl_image (S : set (set α)) : compl '' (compl '' S) = S := by rw [← image_comp, compl_comp_compl, image_id] theorem image_insert_eq {f : α → β} {a : α} {s : set α} : f '' (insert a s) = insert (f a) (f '' s) := ext $ by simp [and_or_distrib_left, exists_or_distrib, eq_comm, or_comm, and_comm] theorem image_subset_preimage_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set α) : f '' s ⊆ g ⁻¹' s := λ b ⟨a, h, e⟩, e ▸ ((I a).symm ▸ h : g (f a) ∈ s) theorem preimage_subset_image_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set β) : f ⁻¹' s ⊆ g '' s := λ b h, ⟨f b, h, I b⟩ theorem image_eq_preimage_of_inverse {f : α → β} {g : β → α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : image f = preimage g := funext $ λ s, subset.antisymm (image_subset_preimage_of_inverse h₁ s) (preimage_subset_image_of_inverse h₂ s) theorem mem_image_iff_of_inverse {f : α → β} {g : β → α} {b : β} {s : set α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : b ∈ f '' s ↔ g b ∈ s := by rw image_eq_preimage_of_inverse h₁ h₂; refl theorem image_compl_subset {f : α → β} {s : set α} (H : injective f) : f '' -s ⊆ -(f '' s) := subset_compl_iff_disjoint.2 $ by simp [image_inter H] theorem subset_image_compl {f : α → β} {s : set α} (H : surjective f) : -(f '' s) ⊆ f '' -s := compl_subset_iff_union.2 $ by rw ← image_union; simp [image_univ_of_surjective H] theorem image_compl_eq {f : α → β} {s : set α} (H : bijective f) : f '' -s = -(f '' s) := subset.antisymm (image_compl_subset H.1) (subset_image_compl H.2) lemma nonempty_image (f : α → β) {s : set α} : nonempty s → nonempty (f '' s) | ⟨⟨x, hx⟩⟩ := ⟨⟨f x, mem_image_of_mem f hx⟩⟩ /- image and preimage are a Galois connection -/ theorem image_subset_iff {s : set α} {t : set β} {f : α → β} : f '' s ⊆ t ↔ s ⊆ f ⁻¹' t := ball_image_iff theorem image_preimage_subset (f : α → β) (s : set β) : f '' (f ⁻¹' s) ⊆ s := image_subset_iff.2 (subset.refl _) theorem subset_preimage_image (f : α → β) (s : set α) : s ⊆ f ⁻¹' (f '' s) := λ x, mem_image_of_mem f theorem preimage_image_eq {f : α → β} (s : set α) (h : injective f) : f ⁻¹' (f '' s) = s := subset.antisymm (λ x ⟨y, hy, e⟩, h e ▸ hy) (subset_preimage_image f s) theorem image_preimage_eq {f : α → β} {s : set β} (h : surjective f) : f '' (f ⁻¹' s) = s := subset.antisymm (image_preimage_subset f s) (λ x hx, let ⟨y, e⟩ := h x in ⟨y, (e.symm ▸ hx : f y ∈ s), e⟩) lemma preimage_eq_preimage {f : β → α} (hf : surjective f) : f ⁻¹' s = preimage f t ↔ s = t := iff.intro (assume eq, by rw [← @image_preimage_eq β α f s hf, ← @image_preimage_eq β α f t hf, eq]) (assume eq, eq ▸ rfl) lemma surjective_preimage {f : β → α} (hf : surjective f) : injective (preimage f) := assume s t, (preimage_eq_preimage hf).1 theorem compl_image : image (@compl α) = preimage compl := image_eq_preimage_of_inverse compl_compl compl_compl theorem compl_image_set_of {α : Type u} {p : set α → Prop} : compl '' {x | p x} = {x | p (- x)} := congr_fun compl_image p theorem inter_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∩ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∩ t) := λ x h, ⟨mem_image_of_mem _ h.left, h.right⟩ theorem union_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∪ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∪ t) := λ x h, or.elim h (λ l, or.inl $ mem_image_of_mem _ l) (λ r, or.inr r) theorem subset_image_union (f : α → β) (s : set α) (t : set β) : f '' (s ∪ f ⁻¹' t) ⊆ f '' s ∪ t := image_subset_iff.2 (union_preimage_subset _ _ _) lemma preimage_subset_iff {A : set α} {B : set β} {f : α → β} : f⁻¹' B ⊆ A ↔ (∀ a : α, f a ∈ B → a ∈ A) := iff.rfl lemma image_eq_image {f : α → β} (hf : injective f) : f '' s = f '' t ↔ s = t := iff.symm $ iff.intro (assume eq, eq ▸ rfl) $ assume eq, by rw [← preimage_image_eq s hf, ← preimage_image_eq t hf, eq] lemma image_subset_image_iff {f : α → β} (hf : injective f) : f '' s ⊆ f '' t ↔ s ⊆ t := begin refine (iff.symm $ iff.intro (image_subset f) $ assume h, _), rw [← preimage_image_eq s hf, ← preimage_image_eq t hf], exact preimage_mono h end lemma injective_image {f : α → β} (hf : injective f) : injective (('') f) := assume s t, (image_eq_image hf).1 lemma prod_quotient_preimage_eq_image [s : setoid α] (g : quotient s → β) {h : α → β} (Hh : h = g ∘ quotient.mk) (r : set (β × β)) : {x : quotient s × quotient s | (g x.1, g x.2) ∈ r} = (λ a : α × α, (⟦a.1⟧, ⟦a.2⟧)) '' ((λ a : α × α, (h a.1, h a.2)) ⁻¹' r) := Hh.symm ▸ set.ext (λ ⟨a₁, a₂⟩, ⟨quotient.induction_on₂ a₁ a₂ (λ a₁ a₂ h, ⟨(a₁, a₂), h, rfl⟩), λ ⟨⟨b₁, b₂⟩, h₁, h₂⟩, show (g a₁, g a₂) ∈ r, from have h₃ : ⟦b₁⟧ = a₁ ∧ ⟦b₂⟧ = a₂ := prod.ext_iff.1 h₂, h₃.1 ▸ h₃.2 ▸ h₁⟩) def image_factorization (f : α → β) (s : set α) : s → f '' s := λ p, ⟨f p.1, mem_image_of_mem f p.2⟩ lemma image_factorization_eq {f : α → β} {s : set α} : subtype.val ∘ image_factorization f s = f ∘ subtype.val := funext $ λ p, rfl lemma surjective_onto_image {f : α → β} {s : set α} : surjective (image_factorization f s) := λ ⟨_, ⟨a, ha, rfl⟩⟩, ⟨⟨a, ha⟩, rfl⟩ end image theorem univ_eq_true_false : univ = ({true, false} : set Prop) := eq.symm $ eq_univ_of_forall $ classical.cases (by simp) (by simp) section range variables {f : ι → α} open function /-- Range of a function. This function is more flexible than `f '' univ`, as the image requires that the domain is in Type and not an arbitrary Sort. -/ def range (f : ι → α) : set α := {x | ∃y, f y = x} @[simp] theorem mem_range {x : α} : x ∈ range f ↔ ∃ y, f y = x := iff.rfl theorem mem_range_self (i : ι) : f i ∈ range f := ⟨i, rfl⟩ theorem forall_range_iff {p : α → Prop} : (∀ a ∈ range f, p a) ↔ (∀ i, p (f i)) := ⟨assume h i, h (f i) (mem_range_self _), assume h a ⟨i, (hi : f i = a)⟩, hi ▸ h i⟩ theorem exists_range_iff {p : α → Prop} : (∃ a ∈ range f, p a) ↔ (∃ i, p (f i)) := ⟨assume ⟨a, ⟨i, eq⟩, h⟩, ⟨i, eq.symm ▸ h⟩, assume ⟨i, h⟩, ⟨f i, mem_range_self _, h⟩⟩ theorem range_iff_surjective : range f = univ ↔ surjective f := eq_univ_iff_forall @[simp] theorem range_id : range (@id α) = univ := range_iff_surjective.2 surjective_id @[simp] theorem image_univ {ι : Type*} {f : ι → β} : f '' univ = range f := ext $ by simp [image, range] theorem image_subset_range {ι : Type*} (f : ι → β) (s : set ι) : f '' s ⊆ range f := by rw ← image_univ; exact image_subset _ (subset_univ _) theorem range_comp {g : α → β} : range (g ∘ f) = g '' range f := subset.antisymm (forall_range_iff.mpr $ assume i, mem_image_of_mem g (mem_range_self _)) (ball_image_iff.mpr $ forall_range_iff.mpr mem_range_self) theorem range_subset_iff {ι : Type*} {f : ι → β} {s : set β} : range f ⊆ s ↔ ∀ y, f y ∈ s := forall_range_iff lemma range_comp_subset_range (f : α → β) (g : β → γ) : range (g ∘ f) ⊆ range g := by rw range_comp; apply image_subset_range lemma nonempty_of_nonempty_range {α : Type*} {β : Type*} {f : α → β} (H : ¬range f = ∅) : nonempty α := begin cases exists_mem_of_ne_empty H with x h, cases mem_range.1 h with y _, exact ⟨y⟩ end @[simp] lemma range_eq_empty {α : Type u} {β : Type v} {f : α → β} : range f = ∅ ↔ ¬ nonempty α := by rw ← set.image_univ; simp [-set.image_univ] theorem image_preimage_eq_inter_range {f : α → β} {t : set β} : f '' (f ⁻¹' t) = t ∩ range f := ext $ assume x, ⟨assume ⟨x, hx, heq⟩, heq ▸ ⟨hx, mem_range_self _⟩, assume ⟨hx, ⟨y, h_eq⟩⟩, h_eq ▸ mem_image_of_mem f $ show y ∈ f ⁻¹' t, by simp [preimage, h_eq, hx]⟩ lemma image_preimage_eq_of_subset {f : α → β} {s : set β} (hs : s ⊆ range f) : f '' (f ⁻¹' s) = s := by rw [image_preimage_eq_inter_range, inter_eq_self_of_subset_left hs] lemma preimage_subset_preimage_iff {s t : set α} {f : β → α} (hs : s ⊆ range f) : f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t := begin split, { intros h x hx, rcases hs hx with ⟨y, rfl⟩, exact h hx }, intros h x, apply h end lemma preimage_eq_preimage' {s t : set α} {f : β → α} (hs : s ⊆ range f) (ht : t ⊆ range f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := begin split, { intro h, apply subset.antisymm, rw [←preimage_subset_preimage_iff hs, h], rw [←preimage_subset_preimage_iff ht, h] }, rintro rfl, refl end theorem preimage_inter_range {f : α → β} {s : set β} : f ⁻¹' (s ∩ range f) = f ⁻¹' s := set.ext $ λ x, and_iff_left ⟨x, rfl⟩ theorem preimage_image_preimage {f : α → β} {s : set β} : f ⁻¹' (f '' (f ⁻¹' s)) = f ⁻¹' s := by rw [image_preimage_eq_inter_range, preimage_inter_range] @[simp] theorem quot_mk_range_eq [setoid α] : range (λx : α, ⟦x⟧) = univ := range_iff_surjective.2 quot.exists_rep lemma range_const_subset {c : β} : range (λx:α, c) ⊆ {c} := range_subset_iff.2 $ λ x, or.inl rfl @[simp] lemma range_const [h : nonempty α] {c : β} : range (λx:α, c) = {c} := begin refine subset.antisymm range_const_subset (λy hy, _), rw set.mem_singleton_iff.1 hy, rcases exists_mem_of_nonempty α with ⟨x, _⟩, exact mem_range_self x end def range_factorization (f : ι → β) : ι → range f := λ i, ⟨f i, mem_range_self i⟩ lemma range_factorization_eq {f : ι → β} : subtype.val ∘ range_factorization f = f := funext $ λ i, rfl lemma surjective_onto_range : surjective (range_factorization f) := λ ⟨_, ⟨i, rfl⟩⟩, ⟨i, rfl⟩ lemma image_eq_range (f : α → β) (s : set α) : f '' s = range (λ(x : s), f x.1) := by { ext, split, rintro ⟨x, h1, h2⟩, exact ⟨⟨x, h1⟩, h2⟩, rintro ⟨⟨x, h1⟩, h2⟩, exact ⟨x, h1, h2⟩ } @[simp] lemma sum.elim_range {α β γ : Type*} (f : α → γ) (g : β → γ) : range (sum.elim f g) = range f ∪ range g := by simp [set.ext_iff, mem_range] lemma range_ite_subset' {p : Prop} [decidable p] {f g : α → β} : range (if p then f else g) ⊆ range f ∪ range g := begin by_cases h : p, {rw if_pos h, exact subset_union_left _ _}, {rw if_neg h, exact subset_union_right _ _} end lemma range_ite_subset {p : α → Prop} [decidable_pred p] {f g : α → β} : range (λ x, if p x then f x else g x) ⊆ range f ∪ range g := begin rw range_subset_iff, intro x, by_cases h : p x, simp [if_pos h, mem_union, mem_range_self], simp [if_neg h, mem_union, mem_range_self] end end range /-- The set `s` is pairwise `r` if `r x y` for all *distinct* `x y ∈ s`. -/ def pairwise_on (s : set α) (r : α → α → Prop) := ∀ x ∈ s, ∀ y ∈ s, x ≠ y → r x y theorem pairwise_on.mono {s t : set α} {r} (h : t ⊆ s) (hp : pairwise_on s r) : pairwise_on t r := λ x xt y yt, hp x (h xt) y (h yt) theorem pairwise_on.mono' {s : set α} {r r' : α → α → Prop} (H : ∀ a b, r a b → r' a b) (hp : pairwise_on s r) : pairwise_on s r' := λ x xs y ys h, H _ _ (hp x xs y ys h) end set open set /- image and preimage on subtypes -/ namespace subtype variable {α : Type*} lemma val_image {p : α → Prop} {s : set (subtype p)} : subtype.val '' s = {x | ∃h : p x, (⟨x, h⟩ : subtype p) ∈ s} := set.ext $ assume a, ⟨assume ⟨⟨a', ha'⟩, in_s, h_eq⟩, h_eq ▸ ⟨ha', in_s⟩, assume ⟨ha, in_s⟩, ⟨⟨a, ha⟩, in_s, rfl⟩⟩ @[simp] lemma val_range {p : α → Prop} : set.range (@subtype.val _ p) = {x | p x} := by rw ← set.image_univ; simp [-set.image_univ, val_image] @[simp] lemma range_val (s : set α) : range (subtype.val : s → α) = s := val_range theorem val_image_subset (s : set α) (t : set (subtype s)) : t.image val ⊆ s := λ x ⟨y, yt, yvaleq⟩, by rw ←yvaleq; exact y.property theorem val_image_univ (s : set α) : @val _ s '' set.univ = s := set.eq_of_subset_of_subset (val_image_subset _ _) (λ x xs, ⟨⟨x, xs⟩, ⟨set.mem_univ _, rfl⟩⟩) theorem image_preimage_val (s t : set α) : (@subtype.val _ s) '' ((@subtype.val _ s) ⁻¹' t) = t ∩ s := begin ext x, simp, split, { rintros ⟨y, ys, yt, yx⟩, rw ←yx, exact ⟨yt, ys⟩ }, rintros ⟨xt, xs⟩, exact ⟨x, xs, xt, rfl⟩ end theorem preimage_val_eq_preimage_val_iff (s t u : set α) : ((@subtype.val _ s) ⁻¹' t = (@subtype.val _ s) ⁻¹' u) ↔ (t ∩ s = u ∩ s) := begin rw [←image_preimage_val, ←image_preimage_val], split, { intro h, rw h }, intro h, exact set.injective_image (val_injective) h end lemma exists_set_subtype {t : set α} (p : set α → Prop) : (∃(s : set t), p (subtype.val '' s)) ↔ ∃(s : set α), s ⊆ t ∧ p s := begin split, { rintro ⟨s, hs⟩, refine ⟨subtype.val '' s, _, hs⟩, convert image_subset_range _ _, rw [range_val] }, rintro ⟨s, hs₁, hs₂⟩, refine ⟨subtype.val ⁻¹' s, _⟩, rw [image_preimage_eq_of_subset], exact hs₂, rw [range_val], exact hs₁ end end subtype namespace set section range variable {α : Type*} @[simp] lemma subtype.val_range {p : α → Prop} : range (@subtype.val _ p) = {x | p x} := by rw ← image_univ; simp [-image_univ, subtype.val_image] @[simp] lemma range_coe_subtype (s : set α) : range (coe : s → α) = s := subtype.val_range end range section prod variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables {s s₁ s₂ : set α} {t t₁ t₂ : set β} /-- The cartesian product `prod s t` is the set of `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def prod (s : set α) (t : set β) : set (α × β) := {p | p.1 ∈ s ∧ p.2 ∈ t} lemma prod_eq (s : set α) (t : set β) : set.prod s t = prod.fst ⁻¹' s ∩ prod.snd ⁻¹' t := rfl theorem mem_prod_eq {p : α × β} : p ∈ set.prod s t = (p.1 ∈ s ∧ p.2 ∈ t) := rfl @[simp] theorem mem_prod {p : α × β} : p ∈ set.prod s t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl lemma mk_mem_prod {a : α} {b : β} (a_in : a ∈ s) (b_in : b ∈ t) : (a, b) ∈ set.prod s t := ⟨a_in, b_in⟩ lemma prod_subset_iff {P : set (α × β)} : (set.prod s t ⊆ P) ↔ ∀ (x ∈ s) (y ∈ t), (x, y) ∈ P := ⟨λ h _ xin _ yin, h (mk_mem_prod xin yin), λ h _ pin, by { cases mem_prod.1 pin with hs ht, simpa using h _ hs _ ht }⟩ @[simp] theorem prod_empty {s : set α} : set.prod s ∅ = (∅ : set (α × β)) := ext $ by simp [set.prod] @[simp] theorem empty_prod {t : set β} : set.prod ∅ t = (∅ : set (α × β)) := ext $ by simp [set.prod] theorem insert_prod {a : α} {s : set α} {t : set β} : set.prod (insert a s) t = (prod.mk a '' t) ∪ set.prod s t := ext begin simp [set.prod, image, iff_def, or_imp_distrib] {contextual := tt}; cc end theorem prod_insert {b : β} {s : set α} {t : set β} : set.prod s (insert b t) = ((λa, (a, b)) '' s) ∪ set.prod s t := ext begin simp [set.prod, image, iff_def, or_imp_distrib] {contextual := tt}; cc end theorem prod_preimage_eq {f : γ → α} {g : δ → β} : set.prod (preimage f s) (preimage g t) = preimage (λp, (f p.1, g p.2)) (set.prod s t) := rfl theorem prod_mono {s₁ s₂ : set α} {t₁ t₂ : set β} (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : set.prod s₁ t₁ ⊆ set.prod s₂ t₂ := assume x ⟨h₁, h₂⟩, ⟨hs h₁, ht h₂⟩ theorem prod_inter_prod : set.prod s₁ t₁ ∩ set.prod s₂ t₂ = set.prod (s₁ ∩ s₂) (t₁ ∩ t₂) := subset.antisymm (assume ⟨a, b⟩ ⟨⟨ha₁, hb₁⟩, ⟨ha₂, hb₂⟩⟩, ⟨⟨ha₁, ha₂⟩, ⟨hb₁, hb₂⟩⟩) (subset_inter (prod_mono (inter_subset_left _ _) (inter_subset_left _ _)) (prod_mono (inter_subset_right _ _) (inter_subset_right _ _))) theorem image_swap_prod : (λp:β×α, (p.2, p.1)) '' set.prod t s = set.prod s t := ext $ assume ⟨a, b⟩, by simp [mem_image_eq, set.prod, and_comm]; exact ⟨ assume ⟨b', a', ⟨h_a, h_b⟩, h⟩, by subst a'; subst b'; assumption, assume h, ⟨b, a, ⟨rfl, rfl⟩, h⟩⟩ theorem image_swap_eq_preimage_swap : image (@prod.swap α β) = preimage prod.swap := image_eq_preimage_of_inverse prod.swap_left_inverse prod.swap_right_inverse theorem prod_image_image_eq {m₁ : α → γ} {m₂ : β → δ} : set.prod (image m₁ s) (image m₂ t) = image (λp:α×β, (m₁ p.1, m₂ p.2)) (set.prod s t) := ext $ by simp [-exists_and_distrib_right, exists_and_distrib_right.symm, and.left_comm, and.assoc, and.comm] theorem prod_range_range_eq {α β γ δ} {m₁ : α → γ} {m₂ : β → δ} : set.prod (range m₁) (range m₂) = range (λp:α×β, (m₁ p.1, m₂ p.2)) := ext $ by simp [range] @[simp] theorem prod_singleton_singleton {a : α} {b : β} : set.prod {a} {b} = ({(a, b)} : set (α×β)) := ext $ by simp [set.prod] theorem prod_neq_empty_iff {s : set α} {t : set β} : set.prod s t ≠ ∅ ↔ (s ≠ ∅ ∧ t ≠ ∅) := by simp [not_eq_empty_iff_exists] theorem prod_eq_empty_iff {s : set α} {t : set β} : set.prod s t = ∅ ↔ (s = ∅ ∨ t = ∅) := suffices (¬ set.prod s t ≠ ∅) ↔ (¬ s ≠ ∅ ∨ ¬ t ≠ ∅), by simpa only [(≠), classical.not_not], by classical; rw [prod_neq_empty_iff, not_and_distrib] @[simp] theorem prod_mk_mem_set_prod_eq {a : α} {b : β} {s : set α} {t : set β} : (a, b) ∈ set.prod s t = (a ∈ s ∧ b ∈ t) := rfl @[simp] theorem univ_prod_univ : set.prod (@univ α) (@univ β) = univ := ext $ assume ⟨a, b⟩, by simp lemma prod_sub_preimage_iff {W : set γ} {f : α × β → γ} : set.prod s t ⊆ f ⁻¹' W ↔ ∀ a b, a ∈ s → b ∈ t → f (a, b) ∈ W := by simp [subset_def] lemma fst_image_prod_subset (s : set α) (t : set β) : prod.fst '' (set.prod s t) ⊆ s := λ _ h, let ⟨_, ⟨h₂, _⟩, h₁⟩ := (set.mem_image _ _ _).1 h in h₁ ▸ h₂ lemma fst_image_prod (s : set β) {t : set α} (ht : t ≠ ∅) : prod.fst '' (set.prod s t) = s := set.subset.antisymm (fst_image_prod_subset _ _) $ λ y y_in, let (⟨x, x_in⟩ : ∃ (x : α), x ∈ t) := set.exists_mem_of_ne_empty ht in ⟨(y, x), ⟨y_in, x_in⟩, rfl⟩ lemma snd_image_prod_subset (s : set α) (t : set β) : prod.snd '' (set.prod s t) ⊆ t := λ _ h, let ⟨_, ⟨_, h₂⟩, h₁⟩ := (set.mem_image _ _ _).1 h in h₁ ▸ h₂ lemma snd_image_prod {s : set α} (hs : s ≠ ∅) (t : set β) : prod.snd '' (set.prod s t) = t := set.subset.antisymm (snd_image_prod_subset _ _) $ λ y y_in, let (⟨x, x_in⟩ : ∃ (x : α), x ∈ s) := set.exists_mem_of_ne_empty hs in ⟨(x, y), ⟨x_in, y_in⟩, rfl⟩ end prod section pi variables {α : Type*} {π : α → Type*} def pi (i : set α) (s : Πa, set (π a)) : set (Πa, π a) := { f | ∀a∈i, f a ∈ s a } @[simp] lemma pi_empty_index (s : Πa, set (π a)) : pi ∅ s = univ := by ext; simp [pi] @[simp] lemma pi_insert_index (a : α) (i : set α) (s : Πa, set (π a)) : pi (insert a i) s = ((λf, f a) ⁻¹' s a) ∩ pi i s := by ext; simp [pi, or_imp_distrib, forall_and_distrib] @[simp] lemma pi_singleton_index (a : α) (s : Πa, set (π a)) : pi {a} s = ((λf:(Πa, π a), f a) ⁻¹' s a) := by ext; simp [pi] lemma pi_if {p : α → Prop} [h : decidable_pred p] (i : set α) (s t : Πa, set (π a)) : pi i (λa, if p a then s a else t a) = pi {a ∈ i | p a} s ∩ pi {a ∈ i | ¬ p a} t := begin ext f, split, { assume h, split; { rintros a ⟨hai, hpa⟩, simpa [*] using h a } }, { rintros ⟨hs, ht⟩ a hai, by_cases p a; simp [*, pi] at * } end end pi section inclusion variable {α : Type*} /-- `inclusion` is the "identity" function between two subsets `s` and `t`, where `s ⊆ t` -/ def inclusion {s t : set α} (h : s ⊆ t) : s → t := λ x : s, (⟨x, h x.2⟩ : t) @[simp] lemma inclusion_self {s : set α} (x : s) : inclusion (set.subset.refl _) x = x := by cases x; refl @[simp] lemma inclusion_inclusion {s t u : set α} (hst : s ⊆ t) (htu : t ⊆ u) (x : s) : inclusion htu (inclusion hst x) = inclusion (set.subset.trans hst htu) x := by cases x; refl lemma inclusion_injective {s t : set α} (h : s ⊆ t) : function.injective (inclusion h) | ⟨_, _⟩ ⟨_, _⟩ := subtype.ext.2 ∘ subtype.ext.1 end inclusion end set
23090104e590b3820bceab6716c3b9581530f9c7
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/two_pointing.lean
91a1b94219ef48bff27e798f0cc09716a5b3d36f
[ "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
3,401
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.sum.basic import logic.nontrivial /-! # Two-pointings This file defines `two_pointing α`, the type of two pointings of `α`. A two-pointing is the data of two distinct terms. This is morally a Type-valued `nontrivial`. Another type which is quite close in essence is `sym2`. Categorically speaking, `prod` is a cospan in the category of types. This forms the category of bipointed types. Two-pointed types form a full subcategory of those. ## References [Coalgebra of the real interval] [http://nlab-pages.s3.us-east-2.amazonaws.com/nlab/show/coalgebra+of+the+real+interval] -/ open function variables {α β : Type*} /-- Two-pointing of a type. This is a Type-valued termed `nontrivial`. -/ @[ext, derive decidable_eq] structure two_pointing (α : Type*) extends α × α := (fst_ne_snd : fst ≠ snd) namespace two_pointing variables (p : two_pointing α) (q : two_pointing β) lemma snd_ne_fst : p.snd ≠ p.fst := p.fst_ne_snd.symm /-- Swaps the two pointed elements. -/ @[simps] def swap : two_pointing α := ⟨(p.snd, p.fst), p.snd_ne_fst⟩ lemma swap_fst : p.swap.fst = p.snd := rfl lemma swap_snd : p.swap.snd = p.fst := rfl @[simp] lemma swap_swap : p.swap.swap = p := by ext; refl @[reducible] -- See note [reducible non instances] lemma to_nontrivial : nontrivial α := ⟨⟨p.fst, p.snd, p.fst_ne_snd⟩⟩ instance [nontrivial α] : nonempty (two_pointing α) := let ⟨a, b, h⟩ := exists_pair_ne α in ⟨⟨(a, b), h⟩⟩ @[simp] lemma nonempty_two_pointing_iff : nonempty (two_pointing α) ↔ nontrivial α := ⟨λ ⟨p⟩, p.to_nontrivial, @two_pointing.nonempty _⟩ section pi variables (α) [nonempty α] /-- The two-pointing of constant functions. -/ def pi : two_pointing (α → β) := { fst := λ _, q.fst, snd := λ _, q.snd, fst_ne_snd := λ h, q.fst_ne_snd $ by convert congr_fun h (classical.arbitrary α) } @[simp] lemma pi_fst : (q.pi α).fst = const α (q.fst) := rfl @[simp] lemma pi_snd : (q.pi α).snd = const α (q.snd) := rfl end pi /-- The product of two two-pointings. -/ def prod : two_pointing (α × β) := { fst := (p.fst, q.fst), snd := (p.snd, q.snd), fst_ne_snd := λ h, p.fst_ne_snd (congr_arg prod.fst h) } @[simp] lemma prod_fst : (p.prod q).fst = (p.fst, q.fst) := rfl @[simp] lemma prod_snd : (p.prod q).snd = (p.snd, q.snd) := rfl /-- The sum of two pointings. Keeps the first point from the left and the second point from the right. -/ protected def sum : two_pointing (α ⊕ β) := ⟨(sum.inl (p.fst), sum.inr (q.snd)), sum.inl_ne_inr⟩ @[simp] lemma sum_fst : (p.sum q).fst = sum.inl p.fst := rfl @[simp] lemma sum_snd : (p.sum q).snd = sum.inr q.snd := rfl /-- The `ff`, `tt` two-pointing of `bool`. -/ protected def bool : two_pointing bool := ⟨(ff, tt), bool.ff_ne_tt⟩ @[simp] lemma bool_fst : two_pointing.bool.fst = ff := rfl @[simp] lemma bool_snd : two_pointing.bool.snd = tt := rfl instance : inhabited (two_pointing bool) := ⟨two_pointing.bool⟩ /-- The `false`, `true` two-pointing of `Prop`. -/ protected def «Prop» : two_pointing Prop := ⟨(false, true), false_ne_true⟩ @[simp] lemma Prop_fst : two_pointing.Prop.fst = false := rfl @[simp] lemma Prop_snd : two_pointing.Prop.snd = true := rfl end two_pointing
5f16b8985afbf38d30711c379a497e79472456b2
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/coe4.lean
08c4e4097e8b7e5880c0ab6543e244d34ce9afa9
[ "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
825
lean
import logic namespace setoid inductive setoid : Type := mk_setoid: Π (A : Type'), (A → A → Prop) → setoid set_option pp.universes true check setoid definition test : Type.{2} := setoid.{0} definition carrier (s : setoid) := setoid.rec (λ a eq, a) s definition eqv {s : setoid} : carrier s → carrier s → Prop := setoid.rec (λ a eqv, eqv) s infix `≈` := eqv coercion carrier inductive morphism (s1 s2 : setoid) : Type := mk : Π (f : s1 → s2), (∀ x y, x ≈ y → f x ≈ f y) → morphism s1 s2 check morphism.mk check λ (s1 s2 : setoid), s1 check λ (s1 s2 : Type), s1 inductive morphism2 (s1 : setoid) (s2 : setoid) : Type := mk : Π (f : s1 → s2), (∀ x y, x ≈ y → f x ≈ f y) → morphism2 s1 s2 check morphism2 check morphism2.mk end setoid
0a1421ce50175584d667b648c9455742544262c9
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/topology/uniform_space/uniform_embedding.lean
1622af04bdd3cd1e8db343e03aad60fca6494f63
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
20,235
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, Sébastien Gouëzel, Patrick Massot Uniform embeddings of uniform spaces. Extension of uniform continuous functions. -/ import topology.uniform_space.cauchy topology.uniform_space.separation import topology.dense_embedding open filter topological_space set classical open_locale classical open_locale uniformity topological_space section variables {α : Type*} {β : Type*} {γ : Type*} [uniform_space α] [uniform_space β] [uniform_space γ] universe u structure uniform_inducing (f : α → β) : Prop := (comap_uniformity : comap (λx:α×α, (f x.1, f x.2)) (𝓤 β) = 𝓤 α) lemma uniform_inducing.mk' {f : α → β} (h : ∀ s, s ∈ 𝓤 α ↔ ∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s) : uniform_inducing f := ⟨by simp [eq_comm, filter.ext_iff, subset_def, h]⟩ lemma uniform_inducing.comp {g : β → γ} (hg : uniform_inducing g) {f : α → β} (hf : uniform_inducing f) : uniform_inducing (g ∘ f) := ⟨ by rw [show (λ (x : α × α), ((g ∘ f) x.1, (g ∘ f) x.2)) = (λ y : β × β, (g y.1, g y.2)) ∘ (λ x : α × α, (f x.1, f x.2)), by ext ; simp, ← filter.comap_comap_comp, hg.1, hf.1]⟩ structure uniform_embedding (f : α → β) extends uniform_inducing f : Prop := (inj : function.injective f) lemma uniform_embedding_subtype_val {p : α → Prop} : uniform_embedding (subtype.val : subtype p → α) := { comap_uniformity := rfl, inj := subtype.val_injective } lemma uniform_embedding_subtype_coe {p : α → Prop} : uniform_embedding (coe : subtype p → α) := uniform_embedding_subtype_val lemma uniform_embedding_set_inclusion {s t : set α} (hst : s ⊆ t) : uniform_embedding (inclusion hst) := { comap_uniformity := by { erw [uniformity_subtype, uniformity_subtype, comap_comap_comp], congr }, inj := inclusion_injective hst } lemma uniform_embedding.comp {g : β → γ} (hg : uniform_embedding g) {f : α → β} (hf : uniform_embedding f) : uniform_embedding (g ∘ f) := { inj := function.injective_comp hg.inj hf.inj, ..hg.to_uniform_inducing.comp hf.to_uniform_inducing } theorem uniform_embedding_def {f : α → β} : uniform_embedding f ↔ function.injective f ∧ ∀ s, s ∈ 𝓤 α ↔ ∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s := begin split, { rintro ⟨⟨h⟩, h'⟩, rw [eq_comm, filter.ext_iff] at h, simp [*, subset_def] }, { rintro ⟨h, h'⟩, refine uniform_embedding.mk ⟨_⟩ h, rw [eq_comm, filter.ext_iff], simp [*, subset_def] } end theorem uniform_embedding_def' {f : α → β} : uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧ ∀ s, s ∈ 𝓤 α → ∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s := by simp [uniform_embedding_def, uniform_continuous_def]; exact ⟨λ ⟨I, H⟩, ⟨I, λ s su, (H _).2 ⟨s, su, λ x y, id⟩, λ s, (H s).1⟩, λ ⟨I, H₁, H₂⟩, ⟨I, λ s, ⟨H₂ s, λ ⟨t, tu, h⟩, sets_of_superset _ (H₁ t tu) (λ ⟨a, b⟩, h a b)⟩⟩⟩ lemma uniform_inducing.uniform_continuous {f : α → β} (hf : uniform_inducing f) : uniform_continuous f := by simp [uniform_continuous, hf.comap_uniformity.symm, tendsto_comap] lemma uniform_inducing.uniform_continuous_iff {f : α → β} {g : β → γ} (hg : uniform_inducing g) : uniform_continuous f ↔ uniform_continuous (g ∘ f) := by simp [uniform_continuous, tendsto]; rw [← hg.comap_uniformity, ← map_le_iff_le_comap, filter.map_map] lemma uniform_inducing.inducing {f : α → β} (h : uniform_inducing f) : inducing f := begin refine ⟨eq_of_nhds_eq_nhds $ assume a, _ ⟩, rw [nhds_induced, nhds_eq_uniformity, nhds_eq_uniformity, ← h.comap_uniformity, comap_lift'_eq, comap_lift'_eq2]; { refl <|> exact monotone_preimage } end lemma uniform_inducing.prod {α' : Type*} {β' : Type*} [uniform_space α'] [uniform_space β'] {e₁ : α → α'} {e₂ : β → β'} (h₁ : uniform_inducing e₁) (h₂ : uniform_inducing e₂) : uniform_inducing (λp:α×β, (e₁ p.1, e₂ p.2)) := ⟨by simp [(∘), uniformity_prod, h₁.comap_uniformity.symm, h₂.comap_uniformity.symm, comap_inf, comap_comap_comp]⟩ lemma uniform_inducing.dense_inducing {f : α → β} (h : uniform_inducing f) (hd : dense_range f) : dense_inducing f := { dense := hd, induced := h.inducing.induced } lemma uniform_embedding.embedding {f : α → β} (h : uniform_embedding f) : embedding f := { induced := h.to_uniform_inducing.inducing.induced, inj := h.inj } lemma uniform_embedding.dense_embedding {f : α → β} (h : uniform_embedding f) (hd : dense_range f) : dense_embedding f := { dense := hd, inj := h.inj, induced := h.embedding.induced } lemma closure_image_mem_nhds_of_uniform_inducing {s : set (α×α)} {e : α → β} (b : β) (he₁ : uniform_inducing e) (he₂ : dense_inducing e) (hs : s ∈ 𝓤 α) : ∃a, closure (e '' {a' | (a, a') ∈ s}) ∈ 𝓝 b := have s ∈ comap (λp:α×α, (e p.1, e p.2)) (𝓤 β), from he₁.comap_uniformity.symm ▸ hs, let ⟨t₁, ht₁u, ht₁⟩ := this in have ht₁ : ∀p:α×α, (e p.1, e p.2) ∈ t₁ → p ∈ s, from ht₁, let ⟨t₂, ht₂u, ht₂s, ht₂c⟩ := comp_symm_of_uniformity ht₁u in let ⟨t, htu, hts, htc⟩ := comp_symm_of_uniformity ht₂u in have preimage e {b' | (b, b') ∈ t₂} ∈ comap e (𝓝 b), from preimage_mem_comap $ mem_nhds_left b ht₂u, let ⟨a, (ha : (b, e a) ∈ t₂)⟩ := nonempty_of_mem_sets (he₂.comap_nhds_ne_bot) this in have ∀b' (s' : set (β × β)), (b, b') ∈ t → s' ∈ 𝓤 β → ({y : β | (b', y) ∈ s'} ∩ e '' {a' : α | (a, a') ∈ s}).nonempty, from assume b' s' hb' hs', have preimage e {b'' | (b', b'') ∈ s' ∩ t} ∈ comap e (𝓝 b'), from preimage_mem_comap $ mem_nhds_left b' $ inter_mem_sets hs' htu, let ⟨a₂, ha₂s', ha₂t⟩ := nonempty_of_mem_sets (he₂.comap_nhds_ne_bot) this in have (e a, e a₂) ∈ t₁, from ht₂c $ prod_mk_mem_comp_rel (ht₂s ha) $ htc $ prod_mk_mem_comp_rel hb' ha₂t, have e a₂ ∈ {b'':β | (b', b'') ∈ s'} ∩ e '' {a' | (a, a') ∈ s}, from ⟨ha₂s', mem_image_of_mem _ $ ht₁ (a, a₂) this⟩, ⟨_, this⟩, have ∀b', (b, b') ∈ t → 𝓝 b' ⊓ principal (e '' {a' | (a, a') ∈ s}) ≠ ⊥, begin intros b' hb', rw [nhds_eq_uniformity, lift'_inf_principal_eq, lift'_ne_bot_iff], exact assume s, this b' s hb', exact monotone_inter monotone_preimage monotone_const end, have ∀b', (b, b') ∈ t → b' ∈ closure (e '' {a' | (a, a') ∈ s}), from assume b' hb', by rw [closure_eq_nhds]; exact this b' hb', ⟨a, (𝓝 b).sets_of_superset (mem_nhds_left b htu) this⟩ lemma uniform_embedding_subtype_emb (p : α → Prop) {e : α → β} (ue : uniform_embedding e) (de : dense_embedding e) : uniform_embedding (dense_embedding.subtype_emb p e) := { comap_uniformity := by simp [comap_comap_comp, (∘), dense_embedding.subtype_emb, uniformity_subtype, ue.comap_uniformity.symm], inj := (de.subtype p).inj } lemma uniform_embedding.prod {α' : Type*} {β' : Type*} [uniform_space α'] [uniform_space β'] {e₁ : α → α'} {e₂ : β → β'} (h₁ : uniform_embedding e₁) (h₂ : uniform_embedding e₂) : uniform_embedding (λp:α×β, (e₁ p.1, e₂ p.2)) := { inj := h₁.inj.prod h₂.inj, ..h₁.to_uniform_inducing.prod h₂.to_uniform_inducing } lemma is_complete_of_complete_image {m : α → β} {s : set α} (hm : uniform_inducing m) (hs : is_complete (m '' s)) : is_complete s := begin intros f hf hfs, rw le_principal_iff at hfs, obtain ⟨_, ⟨x, hx, rfl⟩, hyf⟩ : ∃ y ∈ m '' s, map m f ≤ 𝓝 y, from hs (f.map m) (cauchy_map hm.uniform_continuous hf) (le_principal_iff.2 (image_mem_map hfs)), rw [map_le_iff_le_comap, ← nhds_induced, ← hm.inducing.induced] at hyf, exact ⟨x, hx, hyf⟩ end /-- A set is complete iff its image under a uniform embedding is complete. -/ lemma is_complete_image_iff {m : α → β} {s : set α} (hm : uniform_embedding m) : is_complete (m '' s) ↔ is_complete s := begin refine ⟨is_complete_of_complete_image hm.to_uniform_inducing, λ c f hf fs, _⟩, rw filter.le_principal_iff at fs, let f' := comap m f, have cf' : cauchy f', { have : comap m f ≠ ⊥, { refine comap_ne_bot (λt ht, _), have A : t ∩ m '' s ∈ f := filter.inter_mem_sets ht fs, obtain ⟨x, ⟨xt, ⟨y, ys, rfl⟩⟩⟩ : (t ∩ m '' s).nonempty, from nonempty_of_mem_sets hf.1 A, exact ⟨y, xt⟩ }, apply cauchy_comap _ hf this, simp only [hm.comap_uniformity, le_refl] }, have : f' ≤ principal s := by simp [f']; exact ⟨m '' s, by simpa using fs, by simp [preimage_image_eq s hm.inj]⟩, rcases c f' cf' this with ⟨x, xs, hx⟩, existsi [m x, mem_image_of_mem m xs], rw [(uniform_embedding.embedding hm).induced, nhds_induced] at hx, calc f = map m f' : (map_comap $ filter.mem_sets_of_superset fs $ image_subset_range _ _).symm ... ≤ map m (comap m (𝓝 (m x))) : map_mono hx ... ≤ 𝓝 (m x) : map_comap_le end lemma complete_space_iff_is_complete_range {f : α → β} (hf : uniform_embedding f) : complete_space α ↔ is_complete (range f) := by rw [complete_space_iff_is_complete_univ, ← is_complete_image_iff hf, image_univ] lemma complete_space_congr {e : α ≃ β} (he : uniform_embedding e) : complete_space α ↔ complete_space β := by rw [complete_space_iff_is_complete_range he, e.range_eq_univ, complete_space_iff_is_complete_univ] lemma complete_space_coe_iff_is_complete {s : set α} : complete_space s ↔ is_complete s := (complete_space_iff_is_complete_range uniform_embedding_subtype_coe).trans $ by rw [range_coe_subtype] lemma is_complete.complete_space_coe {s : set α} (hs : is_complete s) : complete_space s := complete_space_coe_iff_is_complete.2 hs lemma is_closed.complete_space_coe [complete_space α] {s : set α} (hs : is_closed s) : complete_space s := (is_complete_of_is_closed hs).complete_space_coe lemma complete_space_extension {m : β → α} (hm : uniform_inducing m) (dense : dense_range m) (h : ∀f:filter β, cauchy f → ∃x:α, map m f ≤ 𝓝 x) : complete_space α := ⟨assume (f : filter α), assume hf : cauchy f, let p : set (α × α) → set α → set α := λs t, {y : α| ∃x:α, x ∈ t ∧ (x, y) ∈ s}, g := (𝓤 α).lift (λs, f.lift' (p s)) in have mp₀ : monotone p, from assume a b h t s ⟨x, xs, xa⟩, ⟨x, xs, h xa⟩, have mp₁ : ∀{s}, monotone (p s), from assume s a b h x ⟨y, ya, yxs⟩, ⟨y, h ya, yxs⟩, have f ≤ g, from le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, le_principal_iff.mpr $ mem_sets_of_superset ht $ assume x hx, ⟨x, hx, refl_mem_uniformity hs⟩, have g ≠ ⊥, from ne_bot_of_le_ne_bot hf.left this, have comap m g ≠ ⊥, from comap_ne_bot $ assume t ht, let ⟨t', ht', ht_mem⟩ := (mem_lift_sets $ monotone_lift' monotone_const mp₀).mp ht in let ⟨t'', ht'', ht'_sub⟩ := (mem_lift'_sets mp₁).mp ht_mem in let ⟨x, (hx : x ∈ t'')⟩ := nonempty_of_mem_sets hf.left ht'' in have h₀ : 𝓝 x ⊓ principal (range m) ≠ ⊥, by simpa [dense_range, closure_eq_nhds] using dense x, have h₁ : {y | (x, y) ∈ t'} ∈ 𝓝 x ⊓ principal (range m), from @mem_inf_sets_of_left α (𝓝 x) (principal (range m)) _ $ mem_nhds_left x ht', have h₂ : range m ∈ 𝓝 x ⊓ principal (range m), from @mem_inf_sets_of_right α (𝓝 x) (principal (range m)) _ $ subset.refl _, have {y | (x, y) ∈ t'} ∩ range m ∈ 𝓝 x ⊓ principal (range m), from @inter_mem_sets α (𝓝 x ⊓ principal (range m)) _ _ h₁ h₂, let ⟨y, xyt', b, b_eq⟩ := nonempty_of_mem_sets h₀ this in ⟨b, b_eq.symm ▸ ht'_sub ⟨x, hx, xyt'⟩⟩, have cauchy g, from ⟨‹g ≠ ⊥›, assume s hs, let ⟨s₁, hs₁, (comp_s₁ : comp_rel s₁ s₁ ⊆ s)⟩ := comp_mem_uniformity_sets hs, ⟨s₂, hs₂, (comp_s₂ : comp_rel s₂ s₂ ⊆ s₁)⟩ := comp_mem_uniformity_sets hs₁, ⟨t, ht, (prod_t : set.prod t t ⊆ s₂)⟩ := mem_prod_same_iff.mp (hf.right hs₂) in have hg₁ : p (preimage prod.swap s₁) t ∈ g, from mem_lift (symm_le_uniformity hs₁) $ @mem_lift' α α f _ t ht, have hg₂ : p s₂ t ∈ g, from mem_lift hs₂ $ @mem_lift' α α f _ t ht, have hg : set.prod (p (preimage prod.swap s₁) t) (p s₂ t) ∈ filter.prod g g, from @prod_mem_prod α α _ _ g g hg₁ hg₂, (filter.prod g g).sets_of_superset hg (assume ⟨a, b⟩ ⟨⟨c₁, c₁t, hc₁⟩, ⟨c₂, c₂t, hc₂⟩⟩, have (c₁, c₂) ∈ set.prod t t, from ⟨c₁t, c₂t⟩, comp_s₁ $ prod_mk_mem_comp_rel hc₁ $ comp_s₂ $ prod_mk_mem_comp_rel (prod_t this) hc₂)⟩, have cauchy (filter.comap m g), from cauchy_comap (le_of_eq hm.comap_uniformity) ‹cauchy g› (by assumption), let ⟨x, (hx : map m (filter.comap m g) ≤ 𝓝 x)⟩ := h _ this in have map m (filter.comap m g) ⊓ 𝓝 x ≠ ⊥, from (le_nhds_iff_adhp_of_cauchy (cauchy_map hm.uniform_continuous this)).mp hx, have g ⊓ 𝓝 x ≠ ⊥, from ne_bot_of_le_ne_bot this (inf_le_inf (assume s hs, ⟨s, hs, subset.refl _⟩) (le_refl _)), ⟨x, calc f ≤ g : by assumption ... ≤ 𝓝 x : le_nhds_of_cauchy_adhp ‹cauchy g› this⟩⟩ lemma totally_bounded_preimage {f : α → β} {s : set β} (hf : uniform_embedding f) (hs : totally_bounded s) : totally_bounded (f ⁻¹' s) := λ t ht, begin rw ← hf.comap_uniformity at ht, rcases mem_comap_sets.2 ht with ⟨t', ht', ts⟩, rcases totally_bounded_iff_subset.1 (totally_bounded_subset (image_preimage_subset f s) hs) _ ht' with ⟨c, cs, hfc, hct⟩, refine ⟨f ⁻¹' c, finite_preimage (hf.inj.inj_on _) hfc, λ x h, _⟩, have := hct (mem_image_of_mem f h), simp at this ⊢, rcases this with ⟨z, zc, zt⟩, rcases cs zc with ⟨y, yc, rfl⟩, exact ⟨y, zc, ts (by exact zt)⟩ end end lemma uniform_embedding_comap {α : Type*} {β : Type*} {f : α → β} [u : uniform_space β] (hf : function.injective f) : @uniform_embedding α β (uniform_space.comap f u) u f := @uniform_embedding.mk _ _ (uniform_space.comap f u) _ _ (@uniform_inducing.mk _ _ (uniform_space.comap f u) _ _ rfl) hf section uniform_extension variables {α : Type*} {β : Type*} {γ : Type*} [uniform_space α] [uniform_space β] [uniform_space γ] {e : β → α} (h_e : uniform_inducing e) (h_dense : dense_range e) {f : β → γ} (h_f : uniform_continuous f) local notation `ψ` := (h_e.dense_inducing h_dense).extend f lemma uniformly_extend_exists [complete_space γ] (a : α) : ∃c, tendsto f (comap e (𝓝 a)) (𝓝 c) := let de := (h_e.dense_inducing h_dense) in have cauchy (𝓝 a), from cauchy_nhds, have cauchy (comap e (𝓝 a)), from cauchy_comap (le_of_eq h_e.comap_uniformity) this de.comap_nhds_ne_bot, have cauchy (map f (comap e (𝓝 a))), from cauchy_map h_f this, complete_space.complete this lemma uniform_extend_subtype [complete_space γ] {p : α → Prop} {e : α → β} {f : α → γ} {b : β} {s : set α} (hf : uniform_continuous (λx:subtype p, f x.val)) (he : uniform_embedding e) (hd : ∀x:β, x ∈ closure (range e)) (hb : closure (e '' s) ∈ 𝓝 b) (hs : is_closed s) (hp : ∀x∈s, p x) : ∃c, tendsto f (comap e (𝓝 b)) (𝓝 c) := have de : dense_embedding e, from he.dense_embedding hd, have de' : dense_embedding (dense_embedding.subtype_emb p e), by exact de.subtype p, have ue' : uniform_embedding (dense_embedding.subtype_emb p e), from uniform_embedding_subtype_emb _ he de, have b ∈ closure (e '' {x | p x}), from (closure_mono $ monotone_image $ hp) (mem_of_nhds hb), let ⟨c, (hc : tendsto (f ∘ subtype.val) (comap (dense_embedding.subtype_emb p e) (𝓝 ⟨b, this⟩)) (𝓝 c))⟩ := uniformly_extend_exists ue'.to_uniform_inducing de'.dense hf _ in begin rw [nhds_subtype_eq_comap] at hc, simp [comap_comap_comp] at hc, change (tendsto (f ∘ @subtype.val α p) (comap (e ∘ @subtype.val α p) (𝓝 b)) (𝓝 c)) at hc, rw [←comap_comap_comp, tendsto_comap'_iff] at hc, exact ⟨c, hc⟩, exact ⟨_, hb, assume x, begin change e x ∈ (closure (e '' s)) → x ∈ range subtype.val, rw [←closure_induced, closure_eq_nhds, mem_set_of_eq, (≠), nhds_induced, ← de.to_dense_inducing.nhds_eq_comap], change x ∈ {x | 𝓝 x ⊓ principal s ≠ ⊥} → x ∈ range subtype.val, rw [←closure_eq_nhds, closure_eq_of_is_closed hs], exact assume hxs, ⟨⟨x, hp x hxs⟩, rfl⟩, exact de.inj end⟩ end variables [separated γ] lemma uniformly_extend_of_ind (b : β) : ψ (e b) = f b := dense_inducing.extend_e_eq _ b (continuous_iff_continuous_at.1 h_f.continuous b) include h_f lemma uniformly_extend_spec [complete_space γ] (a : α) : tendsto f (comap e (𝓝 a)) (𝓝 (ψ a)) := let de := (h_e.dense_inducing h_dense) in begin by_cases ha : a ∈ range e, { rcases ha with ⟨b, rfl⟩, rw [uniformly_extend_of_ind _ _ h_f, ← de.nhds_eq_comap], exact h_f.continuous.tendsto _ }, { simp only [dense_inducing.extend, dif_neg ha], exact (@lim_spec _ _ (id _) _ $ uniformly_extend_exists h_e h_dense h_f _) } end lemma uniform_continuous_uniformly_extend [cγ : complete_space γ] : uniform_continuous ψ := assume d hd, let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $ monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in have h_pnt : ∀{a m}, m ∈ 𝓝 a → ∃c, c ∈ f '' preimage e m ∧ (c, ψ a) ∈ s ∧ (ψ a, c) ∈ s, from assume a m hm, have nb : map f (comap e (𝓝 a)) ≠ ⊥, from map_ne_bot (h_e.dense_inducing h_dense).comap_nhds_ne_bot, have (f '' preimage e m) ∩ ({c | (c, ψ a) ∈ s } ∩ {c | (ψ a, c) ∈ s }) ∈ map f (comap e (𝓝 a)), from inter_mem_sets (image_mem_map $ preimage_mem_comap $ hm) (uniformly_extend_spec h_e h_dense h_f _ (inter_mem_sets (mem_nhds_right _ hs) (mem_nhds_left _ hs))), nonempty_of_mem_sets nb this, have preimage (λp:β×β, (f p.1, f p.2)) s ∈ 𝓤 β, from h_f hs, have preimage (λp:β×β, (f p.1, f p.2)) s ∈ comap (λx:β×β, (e x.1, e x.2)) (𝓤 α), by rwa [h_e.comap_uniformity.symm] at this, let ⟨t, ht, ts⟩ := this in show preimage (λp:(α×α), (ψ p.1, ψ p.2)) d ∈ 𝓤 α, from (𝓤 α).sets_of_superset (interior_mem_uniformity ht) $ assume ⟨x₁, x₂⟩ hx_t, have 𝓝 (x₁, x₂) ≤ principal (interior t), from is_open_iff_nhds.mp is_open_interior (x₁, x₂) hx_t, have interior t ∈ filter.prod (𝓝 x₁) (𝓝 x₂), by rwa [nhds_prod_eq, le_principal_iff] at this, let ⟨m₁, hm₁, m₂, hm₂, (hm : set.prod m₁ m₂ ⊆ interior t)⟩ := mem_prod_iff.mp this in let ⟨a, ha₁, _, ha₂⟩ := h_pnt hm₁ in let ⟨b, hb₁, hb₂, _⟩ := h_pnt hm₂ in have set.prod (preimage e m₁) (preimage e m₂) ⊆ preimage (λp:(β×β), (f p.1, f p.2)) s, from calc _ ⊆ preimage (λp:(β×β), (e p.1, e p.2)) (interior t) : preimage_mono hm ... ⊆ preimage (λp:(β×β), (e p.1, e p.2)) t : preimage_mono interior_subset ... ⊆ preimage (λp:(β×β), (f p.1, f p.2)) s : ts, have set.prod (f '' preimage e m₁) (f '' preimage e m₂) ⊆ s, from calc set.prod (f '' preimage e m₁) (f '' preimage e m₂) = (λp:(β×β), (f p.1, f p.2)) '' (set.prod (preimage e m₁) (preimage e m₂)) : prod_image_image_eq ... ⊆ (λp:(β×β), (f p.1, f p.2)) '' preimage (λp:(β×β), (f p.1, f p.2)) s : monotone_image this ... ⊆ s : image_subset_iff.mpr $ subset.refl _, have (a, b) ∈ s, from @this (a, b) ⟨ha₁, hb₁⟩, hs_comp $ show (ψ x₁, ψ x₂) ∈ comp_rel s (comp_rel s s), from ⟨a, ha₂, ⟨b, this, hb₂⟩⟩ end uniform_extension
4557d24fc16723d748e82663f883ebed34ca0fa0
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/support.lean
2f46a6e282cdf2f7c021ae29fe08bd0fae68bd09
[ "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,165
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import order.conditionally_complete_lattice import algebra.big_operators.basic import algebra.group.prod import algebra.group.pi import algebra.module.pi /-! # Support of a function In this file we define `function.support f = {x | f x ≠ 0}` and prove its basic properties. We also define `function.mul_support f = {x | f x ≠ 1}`. -/ open set open_locale big_operators namespace function variables {α β A B M N P R S G M₀ G₀ : Type*} {ι : Sort*} section has_one variables [has_one M] [has_one N] [has_one P] /-- `support` of a function is the set of points `x` such that `f x ≠ 0`. -/ def support [has_zero A] (f : α → A) : set α := {x | f x ≠ 0} /-- `mul_support` of a function is the set of points `x` such that `f x ≠ 1`. -/ @[to_additive] def mul_support (f : α → M) : set α := {x | f x ≠ 1} @[to_additive] lemma mul_support_eq_preimage (f : α → M) : mul_support f = f ⁻¹' {1}ᶜ := rfl @[to_additive] lemma nmem_mul_support {f : α → M} {x : α} : x ∉ mul_support f ↔ f x = 1 := not_not @[to_additive] lemma compl_mul_support {f : α → M} : (mul_support f)ᶜ = {x | f x = 1} := ext $ λ x, nmem_mul_support @[simp, to_additive] lemma mem_mul_support {f : α → M} {x : α} : x ∈ mul_support f ↔ f x ≠ 1 := iff.rfl @[simp, to_additive] lemma mul_support_subset_iff {f : α → M} {s : set α} : mul_support f ⊆ s ↔ ∀ x, f x ≠ 1 → x ∈ s := iff.rfl @[to_additive] lemma mul_support_subset_iff' {f : α → M} {s : set α} : mul_support f ⊆ s ↔ ∀ x ∉ s, f x = 1 := forall_congr $ λ x, not_imp_comm @[simp, to_additive] lemma mul_support_eq_empty_iff {f : α → M} : mul_support f = ∅ ↔ f = 1 := by { simp_rw [← subset_empty_iff, mul_support_subset_iff', funext_iff], simp } @[simp, to_additive] lemma mul_support_nonempty_iff {f : α → M} : (mul_support f).nonempty ↔ f ≠ 1 := by rw [← ne_empty_iff_nonempty, ne.def, mul_support_eq_empty_iff] @[simp, to_additive] lemma mul_support_one' : mul_support (1 : α → M) = ∅ := mul_support_eq_empty_iff.2 rfl @[simp, to_additive] lemma mul_support_one : mul_support (λ x : α, (1 : M)) = ∅ := mul_support_one' @[to_additive] lemma mul_support_const {c : M} (hc : c ≠ 1) : mul_support (λ x : α, c) = set.univ := by { ext x, simp [hc] } @[to_additive] lemma mul_support_binop_subset (op : M → N → P) (op1 : op 1 1 = 1) (f : α → M) (g : α → N) : mul_support (λ x, op (f x) (g x)) ⊆ mul_support f ∪ mul_support g := λ x hx, classical.by_cases (λ hf : f x = 1, or.inr $ λ hg, hx $ by simp only [hf, hg, op1]) or.inl @[to_additive] lemma mul_support_sup [semilattice_sup M] (f g : α → M) : mul_support (λ x, f x ⊔ g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (⊔) sup_idem f g @[to_additive] lemma mul_support_inf [semilattice_inf M] (f g : α → M) : mul_support (λ x, f x ⊓ g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (⊓) inf_idem f g @[to_additive] lemma mul_support_max [linear_order M] (f g : α → M) : mul_support (λ x, max (f x) (g x)) ⊆ mul_support f ∪ mul_support g := mul_support_sup f g @[to_additive] lemma mul_support_min [linear_order M] (f g : α → M) : mul_support (λ x, min (f x) (g x)) ⊆ mul_support f ∪ mul_support g := mul_support_inf f g @[to_additive] lemma mul_support_supr [conditionally_complete_lattice M] [nonempty ι] (f : ι → α → M) : mul_support (λ x, ⨆ i, f i x) ⊆ ⋃ i, mul_support (f i) := begin rw mul_support_subset_iff', simp only [mem_Union, not_exists, nmem_mul_support], intros x hx, simp only [hx, csupr_const] end @[to_additive] lemma mul_support_infi [conditionally_complete_lattice M] [nonempty ι] (f : ι → α → M) : mul_support (λ x, ⨅ i, f i x) ⊆ ⋃ i, mul_support (f i) := @mul_support_supr _ (order_dual M) ι ⟨(1:M)⟩ _ _ f @[to_additive] lemma mul_support_comp_subset {g : M → N} (hg : g 1 = 1) (f : α → M) : mul_support (g ∘ f) ⊆ mul_support f := λ x, mt $ λ h, by simp only [(∘), *] @[to_additive] lemma mul_support_subset_comp {g : M → N} (hg : ∀ {x}, g x = 1 → x = 1) (f : α → M) : mul_support f ⊆ mul_support (g ∘ f) := λ x, mt hg @[to_additive] lemma mul_support_comp_eq (g : M → N) (hg : ∀ {x}, g x = 1 ↔ x = 1) (f : α → M) : mul_support (g ∘ f) = mul_support f := set.ext $ λ x, not_congr hg @[to_additive] lemma mul_support_comp_eq_preimage (g : β → M) (f : α → β) : mul_support (g ∘ f) = f ⁻¹' mul_support g := rfl @[to_additive support_prod_mk] lemma mul_support_prod_mk (f : α → M) (g : α → N) : mul_support (λ x, (f x, g x)) = mul_support f ∪ mul_support g := set.ext $ λ x, by simp only [mul_support, not_and_distrib, mem_union_eq, mem_set_of_eq, prod.mk_eq_one, ne.def] @[to_additive support_prod_mk'] lemma mul_support_prod_mk' (f : α → M × N) : mul_support f = mul_support (λ x, (f x).1) ∪ mul_support (λ x, (f x).2) := by simp only [← mul_support_prod_mk, prod.mk.eta] @[to_additive] lemma mul_support_along_fiber_subset (f : α × β → M) (a : α) : mul_support (λ b, f (a, b)) ⊆ (mul_support f).image prod.snd := by tidy @[simp, to_additive] lemma mul_support_along_fiber_finite_of_finite (f : α × β → M) (a : α) (h : (mul_support f).finite) : (mul_support (λ b, f (a, b))).finite := (h.image prod.snd).subset (mul_support_along_fiber_subset f a) end has_one @[to_additive] lemma mul_support_mul [monoid M] (f g : α → M) : mul_support (λ x, f x * g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (*) (one_mul _) f g @[simp, to_additive] lemma mul_support_inv [group G] (f : α → G) : mul_support (λ x, (f x)⁻¹) = mul_support f := set.ext $ λ x, not_congr inv_eq_one @[simp, to_additive] lemma mul_support_inv' [group G] (f : α → G) : mul_support (f⁻¹) = mul_support f := mul_support_inv f @[simp] lemma mul_support_inv₀ [group_with_zero G₀] (f : α → G₀) : mul_support (λ x, (f x)⁻¹) = mul_support f := set.ext $ λ x, not_congr inv_eq_one₀ @[to_additive] lemma mul_support_mul_inv [group G] (f g : α → G) : mul_support (λ x, f x * (g x)⁻¹) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (λ a b, a * b⁻¹) (by simp) f g @[to_additive support_sub] lemma mul_support_group_div [group G] (f g : α → G) : mul_support (λ x, f x / g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (/) (by simp only [one_div, one_inv]) f g lemma mul_support_div [group_with_zero G₀] (f g : α → G₀) : mul_support (λ x, f x / g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (/) (by simp only [div_one]) f g @[simp] lemma support_mul [mul_zero_class R] [no_zero_divisors R] (f g : α → R) : support (λ x, f x * g x) = support f ∩ support g := set.ext $ λ x, by simp only [mem_support, mul_ne_zero_iff, mem_inter_eq, not_or_distrib] lemma support_smul_subset_right [add_monoid A] [monoid B] [distrib_mul_action B A] (b : B) (f : α → A) : support (b • f) ⊆ support f := λ x hbf hf, hbf $ by rw [pi.smul_apply, hf, smul_zero] lemma support_smul_subset_left [semiring R] [add_comm_monoid M] [module R M] (f : α → R) (g : α → M) : support (f • g) ⊆ support f := λ x hfg hf, hfg $ by rw [pi.smul_apply', hf, zero_smul] lemma support_smul [semiring R] [add_comm_monoid M] [module R M] [no_zero_smul_divisors R M] (f : α → R) (g : α → M) : support (f • g) = support f ∩ support g := ext $ λ x, smul_ne_zero @[simp] lemma support_inv [group_with_zero G₀] (f : α → G₀) : support (λ x, (f x)⁻¹) = support f := set.ext $ λ x, not_congr inv_eq_zero @[simp] lemma support_div [group_with_zero G₀] (f g : α → G₀) : support (λ x, f x / g x) = support f ∩ support g := by simp [div_eq_mul_inv] @[to_additive] lemma mul_support_prod [comm_monoid M] (s : finset α) (f : α → β → M) : mul_support (λ x, ∏ i in s, f i x) ⊆ ⋃ i ∈ s, mul_support (f i) := begin rw mul_support_subset_iff', simp only [mem_Union, not_exists, nmem_mul_support], exact λ x, finset.prod_eq_one end lemma support_prod_subset [comm_monoid_with_zero A] (s : finset α) (f : α → β → A) : support (λ x, ∏ i in s, f i x) ⊆ ⋂ i ∈ s, support (f i) := λ x hx, mem_Inter₂.2 $ λ i hi H, hx $ finset.prod_eq_zero hi H lemma support_prod [comm_monoid_with_zero A] [no_zero_divisors A] [nontrivial A] (s : finset α) (f : α → β → A) : support (λ x, ∏ i in s, f i x) = ⋂ i ∈ s, support (f i) := set.ext $ λ x, by simp only [support, ne.def, finset.prod_eq_zero_iff, mem_set_of_eq, set.mem_Inter, not_exists] lemma mul_support_one_add [has_one R] [add_left_cancel_monoid R] (f : α → R) : mul_support (λ x, 1 + f x) = support f := set.ext $ λ x, not_congr add_right_eq_self lemma mul_support_one_add' [has_one R] [add_left_cancel_monoid R] (f : α → R) : mul_support (1 + f) = support f := mul_support_one_add f lemma mul_support_add_one [has_one R] [add_right_cancel_monoid R] (f : α → R) : mul_support (λ x, f x + 1) = support f := set.ext $ λ x, not_congr add_left_eq_self lemma mul_support_add_one' [has_one R] [add_right_cancel_monoid R] (f : α → R) : mul_support (f + 1) = support f := mul_support_add_one f lemma mul_support_one_sub' [has_one R] [add_group R] (f : α → R) : mul_support (1 - f) = support f := by rw [sub_eq_add_neg, mul_support_one_add', support_neg'] lemma mul_support_one_sub [has_one R] [add_group R] (f : α → R) : mul_support (λ x, 1 - f x) = support f := mul_support_one_sub' f end function namespace set open function variables {α β M : Type*} [has_one M] {f : α → M} @[to_additive] lemma image_inter_mul_support_eq {s : set β} {g : β → α} : (g '' s ∩ mul_support f) = g '' (s ∩ mul_support (f ∘ g)) := by rw [mul_support_comp_eq_preimage f g, image_inter_preimage] end set namespace pi variables {A : Type*} {B : Type*} [decidable_eq A] [has_zero B] {a : A} {b : B} lemma support_single_zero : function.support (pi.single a (0 : B)) = ∅ := by simp @[simp] lemma support_single_of_ne (h : b ≠ 0) : function.support (pi.single a b) = {a} := begin ext, simp only [mem_singleton_iff, ne.def, function.mem_support], split, { contrapose!, exact λ h', single_eq_of_ne h' b }, { rintro rfl, rw single_eq_same, exact h } end lemma support_single [decidable_eq B] : function.support (pi.single a b) = if b = 0 then ∅ else {a} := by { split_ifs with h; simp [h] } lemma support_single_subset : function.support (pi.single a b) ⊆ {a} := begin classical, rw support_single, split_ifs; simp end lemma support_single_disjoint {b' : B} (hb : b ≠ 0) (hb' : b' ≠ 0) {i j : A} : disjoint (function.support (single i b)) (function.support (single j b')) ↔ i ≠ j := by rw [support_single_of_ne hb, support_single_of_ne hb', disjoint_singleton] end pi
8101776cb0a0d34e1c9064bac32c6c6c9a03f091
f3a5af2927397cf346ec0e24312bfff077f00425
/src/game/world3/level5.lean
7661ca00d898f7aaaa6a1b04f05ccedcc3a623bc
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/natural_number_game
05c39e1586408cfb563d1a12e1085a90726ab655
f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd
refs/heads/master
1,688,570,964,990
1,636,908,242,000
1,636,908,242,000
195,403,790
277
84
Apache-2.0
1,694,547,955,000
1,562,328,792,000
Lean
UTF-8
Lean
false
false
1,168
lean
import game.world3.level4 -- hide import mynat.mul -- hide namespace mynat -- hide /- # Multiplication World ## Level 5: `mul_assoc` We now have enough to prove that multiplication is associative. ## Random tactic hints 1) Did you know you can do `repeat {rw mul_succ}`? 2) Did you know you can do `rwa [hd, mul_add]`? I learnt that trick from Ken Lee. Ken spotted that `rwa` will do the rewrites and will then check to see if the goal can be proved by `refl`, and if it can, it will close it! [It will also close goals which are exactly equal to hypotheses, which will be helpful later on.] -/ /- Lemma Multiplication is associative. In other words, for all natural numbers $a$, $b$ and $c$, we have $$ (ab)c = a(bc). $$ -/ lemma mul_assoc (a b c : mynat) : (a * b) * c = a * (b * c) := begin [nat_num_game] induction c with d hd, { repeat {rw mul_zero}, }, { rw mul_succ, rw mul_succ, rw hd, rw mul_add, refl, } end /- A mathematician could now remark that you have proved that the natural numbers form a monoid under multiplication. -/ def collectible_4 : monoid mynat := by structure_helper -- hide end mynat -- hide
510e7c80c33d2aba4c7b54826c07bb591fa3221b
76ce87faa6bc3c2aa9af5962009e01e04f2a074a
/Exam2-Review.lean
01eaab310102ed3f49918e5c4de77ad12c4ca8b7
[]
no_license
Mnormansell/Discrete-Notes
db423dd9206bbe7080aecb84b4c2d275b758af97
61f13b98be590269fc4822be7b47924a6ddc1261
refs/heads/master
1,585,412,435,424
1,540,919,483,000
1,540,919,483,000
148,684,638
0
0
null
null
null
null
UTF-8
Lean
false
false
13,055
lean
/- You need to understand the following elements of automated predicate logic for Exam 2 Suppose P and Q are arbitrary propositions (i.e., P Q : Prop) and T and V are arbitrary types (i.e., T V : Type). Know the following forms, how to prove them, how to use proofs of them, and how to do these things in Lean. To know how to prove them and how to use proofs of them, you need an intuitive understanding of the introduction and elimination rules for each form, and how to use them in Lean. * true : Prop * false : Prop * = -- equality * P ∧ Q : Prop * (∀ p : P, Q) : Prop -- Q can involve p * T → V : Type -- function type) * P → Q : Prop -- implication) * ¬ P : Prop * P ↔ Q : Prop * P ∨ Q : Prop * (∃ p : P, Q) : Prop -- Q can involve p * T → Prop -- a property of Ts * T → V → Prop -- a T-V binary relation Knowing this material, you are also expected to be able to combine these reasoning rules to prove more interesting propositions. In general, such a proof first applies elimination rules to obtain additional useful elements from given assumptions, and then uses introduction rules to combine the elements obtained into proofs of desired conclusions. You should have an intuitive understanding of the meaning of each form and how to use each form in logical reasoning. In particular, understand (1) the introduction rules for each -- how to construct proofs in these forms -- and (2) the elimination rules for each form -- how to use proofs in these forms to obtain additional facts to be used in constructing proofs of other propositions. For extra credit, be able to work in Lean with (1) propositions and proofs involving combinations of quantifiers, such as (∀ x : X, ∃ y : Y, Z) and (∃ x : X, ∀ y : Y, Z), and with (2) negations of quantified propositions, such as ¬ (∃ p : P, Q) and ¬ (∀ p : P, Q). The test will be structured to help you to know and to show how far you've gotten and where you still have some work to do. Here are exercises for each form. -/ /- ***************************************** -/ /- ***************** true ****************** -/ /- ***************************************** -/ /- (1) Use "example" in Lean to prove that there is a proof of true. Be sure that after the := you can provide a proof using both an expression and a tactic script. -/ example : true := true.intro example : true := begin apply true.intro, end /- ***************************************** -/ /- ***************** false ***************** -/ /- ***************************************** -/ /- (2) Use "def" in Lean to define a function, fq, that proves that if P and Q are propositions and if Q is true then false → ¬ Q. -/ def fq (P : Prop) (Q : Prop) (pfq : Q) : false → ¬ Q := begin assume f, assume q, show false, from false.elim f, end /- (3) Use "example" in Lean to prove that if 0 = 1 then 0 ≠ 1. -/ example : (0 = 1) → (0 ≠ 1) := λ h : (0 = 1), nat.no_confusion h /- (4) Use "example" to prove that for any two natural numbers, a and b if a = b then if b ≠ a then a ≠ b. -/ example (a : ℕ) (b : ℕ) : a = b → b ≠ a → a ≠ b := begin assume ab nba nab, show false, from begin have ba := eq.symm ab, exact false.elim (nba ba), end, end example (a : ℕ) (b : ℕ) : a = b → b ≠ a → a ≠ b := λ ab nba nab, false.elim (nba (eq.symm ab)) /- ***************************************** -/ /- ***************** and ******************* -/ /- ***************************************** -/ /- (5) Use "example" to prove that if P, Q, and R are propositions, P → Q → R → (P ∧ Q) ∧ R -/ example (P Q R : Prop): P → Q → R → (P ∧ Q) ∧ R := λ P Q R, and.intro (and.intro P Q) R /- (6) Use "example" to prove that if P, Q, and R are propositions, (P ∧ Q) ∧ R → P ∧ R. -/ example (P Q R : Prop): (P ∧ Q) ∧ R → P ∧ R := begin assume pqr, have pq : P ∧ Q := pqr.1, have p : P := pq.1, have r : R := pqr.2, exact and.intro p r, end example (P Q R : Prop): (P ∧ Q) ∧ R → P ∧ R := λ pqr, and.intro ((pqr.left).left) pqr.2 /- ***************************************** -/ /- *************** functions *************** -/ /- ***************************************** -/ /- (7) Use example to prove that if S and T are arbitrary types and if there is a value, t : T, then S → T. Remember that a proof of S → T has to be a function that if given any value of type S returns some value of type T. Present this proof as a Python-style function definition, isFun, then using a tactic script. The Π you can read and treast as ∀, for now. -/ example (S T : Type) (t : T) : S → T := λ s, t example (S T : Type) (t : T) : S → T := begin assume s, exact t, end /- (8) use def to define comp to be a function that takes as its argments, the types S, T, and R, along with a function of type S → T and a function of type T → R and that returns a function of type S → R. It should take S, T, and R implicitly and st and tr explicitly. -/ def comp (S T R : Type) : (S → T) → (T → R) → (S → R) := λ st tr s, tr (st s) /- (9) Define square to be a function that takes a natural number and returns its square, and inc to be a function that takes a nat, n, and returns n + 1. Now use def and comp to define incSquare to be a function that takes a nat, n, as an argument and that returns one more than n^2. Use #reduce to check that the value of (incSquare 5) is 26. -/ def square : ℕ → ℕ := λ n, n*n def inc : ℕ → ℕ := λ n, n + 1 def incSquare : ℕ → ℕ := sorry /- (10) Consider the function, sum4, below. What is the type of (sum4 3 7)? What function is (sum4 3 7)? Answer the second question with a lambda abstraction. -/ --(sum4 3 7) likely has type ℕ → ℕ → ℕ --this might be it, no clue why it would be called --sum4 def sum4 : ℕ → ℕ → ℕ := λ n m, n + m /- ***************************************** -/ /- ************** implication ************** -/ /- ***************************************** -/ /- (11) Use several "examples" to prove (1) false → false, (2) false → true, (3) ¬ (true → false). -/ example : false → false := λ f, false.elim f example : false → true := λ f, false.elim f example : ¬ (true → false) := begin assume TtoF, exact false.elim (TtoF true.intro), end /- (12) Use example to prove that for any two propositions, P, Q, if P ∧ Q is true then if ¬ (P ∨ Q) is true, then you can derive a proof of false. -/ example (P Q : Prop) : (P ∧ Q) → ¬ (P ∨ Q) → false := begin assume pq nporq, cases pq with p q, exact nporq (or.intro_left Q p), end /- ***************************************** -/ /- *************** forall (∀) ************** -/ /- ***************************************** -/ /- (13) Use example to prove that for all proposition, P, Q, and R, if P → Q → R then P → R. -/ example : ∀(P Q R : Prop), (P → Q → R) → (P → R) := λ P Q R, λ pqr p, pqr p sorry -- error here as I do not how to get q here by this def --if this is not the right function, and this is /- (14) Prove that for any type, T, for any a : T, and for any property, (P : T → Prop), if (∀ t : T, P t), then P a. -/ example : ∀(T : Type), ∀ (a : T), ∀(P : T → Prop), (∀ t : T, P t) → P a := begin intros, exact a_1 a, end /- ***************************************** -/ /- *************** negation **************** -/ /- ***************************************** -/ /- (15) Show that for any propositions, P and Q, ¬ ((P ∧ Q) ∧ ((P ∧ Q) → (¬ Q ∧ P)). -/ example : ∀(P Q : Prop), ¬ ((P ∧ Q) ∧ ((P ∧ Q) → (¬ Q ∧ P))) := begin intros, assume pf, show false, from begin cases pf with left right, have nqandp := right left, exact nqandp.1 left.2, end, end /- (16) Prove that for any propositions, P and R, if P → R and ¬ P → R, then R. You might need to use the law of the excluded middle. -/ open classical example : ∀(P R : Prop), (P → R) → (¬P → R) → R := begin intros, cases em P with p np, --p show R, from a p, --np show R, from a_1 np, end /- ***************************************** -/ /- ************* bi-implication ************ -/ /- ***************************************** -/ /- (17) Prove that for any propositions P and Q, (P -> Q) ∧ (Q → P) → (P ↔ Q). -/ example : ∀(P Q : Prop), (P -> Q) ∧ (Q → P) → (P ↔ Q) := begin assume P Q pf, cases pf with left right pf, exact iff.intro left right, end /- (18) Prove that for propositions P and Q, ((P ↔ Q) ∧ P) → Q. -/ example : ∀(P Q : Prop), ((P ↔ Q) ∧ P) → Q := begin assume p q pf, cases pf with left right, exact (iff.elim_left left) right, end /- ***************************************** -/ /- ************** disjunction ************** -/ /- ***************************************** -/ /- (19) Prove that if you eat donuts or if you eat candy you will get cavities. The proposition you prove should build in the assumptions that if you eat donuts you will get cavities and if you eat candy you will get cavities. We start the proof for you. You complete it. Start your proof like this: example : ∀ donut candy cavity : Prop, ... -/ example : ∀ donut candy cavity : Prop, ((donut → cavity) ∧ (candy → cavity)) → (donut ∨ candy) → cavity := begin intros, cases a with left right, cases a_1 with donut candy, exact left donut, exact right candy, end /- (20) Prove that if P and Q are any propositions, and if you have a proof of ¬(P ∨ Q) then you can construct a proof of ¬ P. -/ example : ∀ (P Q : Prop), ¬(P ∨ Q) → ¬ P := begin intros, assume p, exact a (or.intro_left Q p), end /- (21) Show, without using em explicitly, that if for any proposition, P, P ∨ ¬ P, then for any proposition, Q, ¬ ¬ Q → Q. The proposition to prove is (∀ P : Prop, P ∨ ¬ P) → (∀ Q, ¬¬ Q → Q). Remember that a proof of (∀ P, S) can be applied to a value of type P to get a value of type S. -/ example : (∀ P : Prop, P ∨ ¬ P) → (∀ Q, ¬¬ Q → Q) := begin intros, cases a Q with q nq, --Q assumption, --nQ show Q, from false.elim (a_1 nq), end /- ***************************************** -/ /- **************** predicates ************* -/ /- ***************************************** -/ /- (22) Define notZero(n) to be a predicate on natural numbers that is true when 0 ≠ n (and false otherwise). Then prove two facts using "example." First, ¬ (notZero 0). When doing this proof, remember what (notZero 0) means, and remember what negation means. Second, prove (notZero 1). -/ def notZero : ℕ → Prop := λ n, n ≠ 0 #check notZero 2 #reduce notZero 5 example : notZero 1 := begin unfold notZero, assume eq, show false, from nat.no_confusion eq, end example : ¬ (notZero 0) := begin unfold notZero, assume neq, show false, from neq (eq.refl 0), end /- (23) Define eqString(s, t) to be a predicate on values of type string, that is true when s = t (and not true otherwise). Then prove: eqString "Hello Lean" ("Hello " ++ "Lean") -/ def eqString : string → string → Prop := λ s t, string.length s = string.length t sorry --how does this work /- ***************************************** -/ /- **************** exists ***************** -/ /- ***************************************** -/ /- (24) Prove that ∃ n : ℕ, n = 13. -/ example : ∃ n : ℕ, n = 13 := begin apply exists.intro 13, apply eq.refl 13, end /- (25) Prove ∀ s : string, ∃ n, n = string.length s. -/ example : ∀(s : string), ∃ n, n = string.length s := begin assume str, apply exists.intro 5, sorry end /- (26) Prove exists m : ℕ, exists n: ℕ, m * n = 100. Remember that you can apply exists.intro to a witness, leaving the proof to be constructed interactively. -/ example : ∀(m : ℕ), exists n: ℕ, m * n = 100 := begin intros, apply exists.intro 10, sorry end /- (27) Prove that if P and S are properties of natural numbers, and if (∃ n : ℕ, P n ∧ S n), then (∃ n : ℕ, P n ∨ S n). -/ example : (∃ n : ℕ, P n ∧ S n), (∃ n : ℕ, P n ∨ S n)
dba06286c4bad232f5d1d70f434e309ab0fa1b2f
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/src/Init/Data/List/BasicAux.lean
af6f411116073f08fe1834f5dd7e4ab9db0406d6
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,825
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.Data.List.Basic import Init.Util universes u namespace List /- The following functions can't be defined at `init.data.list.basic`, because they depend on `init.util`, and `init.util` depends on `init.data.list.basic`. -/ variable {α : Type u} def get! [Inhabited α] : Nat → List α → α | 0, a::as => a | n+1, a::as => get! n as | _, _ => panic! "invalid index" def get? : Nat → List α → Option α | 0, a::as => some a | n+1, a::as => get? n as | _, _ => none def getD (idx : Nat) (as : List α) (a₀ : α) : α := (as.get? idx).getD a₀ def head! [Inhabited α] : List α → α | [] => panic! "empty list" | a::_ => a def head? : List α → Option α | [] => none | a::_ => some a def headD : List α → α → α | [], a₀ => a₀ | a::_, _ => a def tail! : List α → List α | [] => panic! "empty list" | a::as => as def tail? : List α → Option (List α) | [] => none | a::as => some as def tailD : List α → List α → List α | [], as₀ => as₀ | a::as, _ => as def getLast : ∀ (as : List α), as ≠ [] → α | [], h => absurd rfl h | [a], h => a | a::b::as, h => getLast (b::as) (fun h => List.noConfusion h) def getLast! [Inhabited α] : List α → α | [] => panic! "empty list" | a::as => getLast (a::as) (fun h => List.noConfusion h) def getLast? : List α → Option α | [] => none | a::as => some (getLast (a::as) (fun h => List.noConfusion h)) def getLastD : List α → α → α | [], a₀ => a₀ | a::as, _ => getLast (a::as) (fun h => List.noConfusion h) end List
c2d7e3cb3e554c49357c5c582bfb2b172c5c9f72
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/group/semiconj.lean
f3d9217d9a060e0767bb35f48d8243c69057e321
[ "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
7,861
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov Some proofs and docs came from `algebra/commute` (c) Neil Strickland -/ import algebra.group.units /-! # Semiconjugate elements of a semigroup > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > https://github.com/leanprover-community/mathlib4/pull/717 > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions We say that `x` is semiconjugate to `y` by `a` (`semiconj_by a x y`), if `a * x = y * a`. In this file we provide operations on `semiconj_by _ _ _`. In the names of these operations, we treat `a` as the “left” argument, and both `x` and `y` as “right” arguments. This way most names in this file agree with the names of the corresponding lemmas for `commute a b = semiconj_by a b b`. As a side effect, some lemmas have only `_right` version. Lean does not immediately recognise these terms as equations, so for rewriting we need syntax like `rw [(h.pow_right 5).eq]` rather than just `rw [h.pow_right 5]`. This file provides only basic operations (`mul_left`, `mul_right`, `inv_right` etc). Other operations (`pow_right`, field inverse etc) are in the files that define corresponding notions. -/ universes u v variables {G : Type*} /-- `x` is semiconjugate to `y` by `a`, if `a * x = y * a`. -/ @[to_additive add_semiconj_by "`x` is additive semiconjugate to `y` by `a` if `a + x = y + a`"] def semiconj_by {M : Type u} [has_mul M] (a x y : M) : Prop := a * x = y * a namespace semiconj_by /-- Equality behind `semiconj_by a x y`; useful for rewriting. -/ @[to_additive "Equality behind `add_semiconj_by a x y`; useful for rewriting."] protected lemma eq {S : Type u} [has_mul S] {a x y : S} (h : semiconj_by a x y) : a * x = y * a := h section semigroup variables {S : Type u} [semigroup S] {a b x y z x' y' : S} /-- If `a` semiconjugates `x` to `y` and `x'` to `y'`, then it semiconjugates `x * x'` to `y * y'`. -/ @[simp, to_additive "If `a` semiconjugates `x` to `y` and `x'` to `y'`, then it semiconjugates `x + x'` to `y + y'`."] lemma mul_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') : semiconj_by a (x * x') (y * y') := by unfold semiconj_by; assoc_rw [h.eq, h'.eq] /-- If both `a` and `b` semiconjugate `x` to `y`, then so does `a * b`. -/ @[to_additive "If both `a` and `b` semiconjugate `x` to `y`, then so does `a + b`."] lemma mul_left (ha : semiconj_by a y z) (hb : semiconj_by b x y) : semiconj_by (a * b) x z := by unfold semiconj_by; assoc_rw [hb.eq, ha.eq, mul_assoc] /-- The relation “there exists an element that semiconjugates `a` to `b`” on a semigroup is transitive. -/ @[to_additive "The relation “there exists an element that semiconjugates `a` to `b`” on an additive semigroup is transitive."] protected lemma transitive : transitive (λ a b : S, ∃ c, semiconj_by c a b) := λ a b c ⟨x, hx⟩ ⟨y, hy⟩, ⟨y * x, hy.mul_left hx⟩ end semigroup section mul_one_class variables {M : Type u} [mul_one_class M] /-- Any element semiconjugates `1` to `1`. -/ @[simp, to_additive "Any element additively semiconjugates `0` to `0`."] lemma one_right (a : M) : semiconj_by a 1 1 := by rw [semiconj_by, mul_one, one_mul] /-- One semiconjugates any element to itself. -/ @[simp, to_additive "Zero additively semiconjugates any element to itself."] lemma one_left (x : M) : semiconj_by 1 x x := eq.symm $ one_right x /-- The relation “there exists an element that semiconjugates `a` to `b`” on a monoid (or, more generally, on ` mul_one_class` type) is reflexive. -/ @[to_additive "The relation “there exists an element that semiconjugates `a` to `b`” on an additive monoid (or, more generally, on a `add_zero_class` type) is reflexive."] protected lemma reflexive : reflexive (λ a b : M, ∃ c, semiconj_by c a b) := λ a, ⟨1, one_left a⟩ end mul_one_class section monoid variables {M : Type u} [monoid M] /-- If `a` semiconjugates a unit `x` to a unit `y`, then it semiconjugates `x⁻¹` to `y⁻¹`. -/ @[to_additive "If `a` semiconjugates an additive unit `x` to an additive unit `y`, then it semiconjugates `-x` to `-y`."] lemma units_inv_right {a : M} {x y : Mˣ} (h : semiconj_by a x y) : semiconj_by a ↑x⁻¹ ↑y⁻¹ := calc a * ↑x⁻¹ = ↑y⁻¹ * (y * a) * ↑x⁻¹ : by rw [units.inv_mul_cancel_left] ... = ↑y⁻¹ * a : by rw [← h.eq, mul_assoc, units.mul_inv_cancel_right] @[simp, to_additive] lemma units_inv_right_iff {a : M} {x y : Mˣ} : semiconj_by a ↑x⁻¹ ↑y⁻¹ ↔ semiconj_by a x y := ⟨units_inv_right, units_inv_right⟩ /-- If a unit `a` semiconjugates `x` to `y`, then `a⁻¹` semiconjugates `y` to `x`. -/ @[to_additive "If an additive unit `a` semiconjugates `x` to `y`, then `-a` semiconjugates `y` to `x`."] lemma units_inv_symm_left {a : Mˣ} {x y : M} (h : semiconj_by ↑a x y) : semiconj_by ↑a⁻¹ y x := calc ↑a⁻¹ * y = ↑a⁻¹ * (y * a * ↑a⁻¹) : by rw [units.mul_inv_cancel_right] ... = x * ↑a⁻¹ : by rw [← h.eq, ← mul_assoc, units.inv_mul_cancel_left] @[simp, to_additive] lemma units_inv_symm_left_iff {a : Mˣ} {x y : M} : semiconj_by ↑a⁻¹ y x ↔ semiconj_by ↑a x y := ⟨units_inv_symm_left, units_inv_symm_left⟩ @[to_additive] theorem units_coe {a x y : Mˣ} (h : semiconj_by a x y) : semiconj_by (a : M) x y := congr_arg units.val h @[to_additive] theorem units_of_coe {a x y : Mˣ} (h : semiconj_by (a : M) x y) : semiconj_by a x y := units.ext h @[simp, to_additive] theorem units_coe_iff {a x y : Mˣ} : semiconj_by (a : M) x y ↔ semiconj_by a x y := ⟨units_of_coe, units_coe⟩ @[simp, to_additive] lemma pow_right {a x y : M} (h : semiconj_by a x y) (n : ℕ) : semiconj_by a (x^n) (y^n) := begin induction n with n ih, { rw [pow_zero, pow_zero], exact semiconj_by.one_right _ }, { rw [pow_succ, pow_succ], exact h.mul_right ih } end end monoid section division_monoid variables [division_monoid G] {a x y : G} @[simp, to_additive] lemma inv_inv_symm_iff : semiconj_by a⁻¹ x⁻¹ y⁻¹ ↔ semiconj_by a y x := inv_involutive.injective.eq_iff.symm.trans $ by simp_rw [mul_inv_rev, inv_inv, eq_comm, semiconj_by] @[to_additive] lemma inv_inv_symm : semiconj_by a x y → semiconj_by a⁻¹ y⁻¹ x⁻¹ := inv_inv_symm_iff.2 end division_monoid section group variables [group G] {a x y : G} @[simp, to_additive] lemma inv_right_iff : semiconj_by a x⁻¹ y⁻¹ ↔ semiconj_by a x y := @units_inv_right_iff G _ a ⟨x, x⁻¹, mul_inv_self x, inv_mul_self x⟩ ⟨y, y⁻¹, mul_inv_self y, inv_mul_self y⟩ @[to_additive] lemma inv_right : semiconj_by a x y → semiconj_by a x⁻¹ y⁻¹ := inv_right_iff.2 @[simp, to_additive] lemma inv_symm_left_iff : semiconj_by a⁻¹ y x ↔ semiconj_by a x y := @units_inv_symm_left_iff G _ ⟨a, a⁻¹, mul_inv_self a, inv_mul_self a⟩ _ _ @[to_additive] lemma inv_symm_left : semiconj_by a x y → semiconj_by a⁻¹ y x := inv_symm_left_iff.2 /-- `a` semiconjugates `x` to `a * x * a⁻¹`. -/ @[to_additive "`a` semiconjugates `x` to `a + x + -a`."] lemma conj_mk (a x : G) : semiconj_by a x (a * x * a⁻¹) := by unfold semiconj_by; rw [mul_assoc, inv_mul_self, mul_one] end group end semiconj_by @[simp, to_additive add_semiconj_by_iff_eq] lemma semiconj_by_iff_eq {M : Type u} [cancel_comm_monoid M] {a x y : M} : semiconj_by a x y ↔ x = y := ⟨λ h, mul_left_cancel (h.trans (mul_comm _ _)), λ h, by rw [h, semiconj_by, mul_comm] ⟩ /-- `a` semiconjugates `x` to `a * x * a⁻¹`. -/ @[to_additive "`a` semiconjugates `x` to `a + x + -a`."] lemma units.mk_semiconj_by {M : Type u} [monoid M] (u : Mˣ) (x : M) : semiconj_by ↑u x (u * x * ↑u⁻¹) := by unfold semiconj_by; rw [units.inv_mul_cancel_right]
b0b26e87eb0700138fdfa17c093314af07e361dc
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/unsafeInit.lean
56e8afa9753a4a3099080e5a50f9b167d2caea14
[ "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
50
lean
unsafe initialize no : Nat ← pure lcUnreachable
49a9aa05209e54a676f92154b2a4855d5e745896
96b8fa8ff5edc088c62b8e0d92e4599e8edbf1e2
/Cli/Extensions.lean
f8af3e6640f4c4308ea97be356d50d6d1389155e
[ "MIT" ]
permissive
pnwamk/lean4-cli
4429db4cebfca510573150ade5e8ed3087014051
0a66055885086864114c8b281830a47f2b601ff4
refs/heads/main
1,678,627,315,712
1,614,105,437,000
1,614,105,437,000
342,072,945
0
0
null
1,614,212,319,000
1,614,212,318,000
null
UTF-8
Lean
false
false
4,541
lean
import Cli.Basic section Utils namespace Array /-- Appends those elements of `right` to `left` whose `key` is not already contained in `left`. -/ def leftUnionBy [HasLess α] [DecidableRel ((· < ·) : α → α → Prop)] (key : β → α) (left : Array β) (right : Array β) : Array β := do let leftMap := left.map (fun v => (key v, v)) |>.toList |> Std.RBMap.ofList (lt := (· < ·)) let mut result := left for v in right do if ¬ leftMap.contains (key v) then result := result.push v return result /-- Prepends those elements of `left` to `right` whose `key` is not already contained in `right`. -/ def rightUnionBy [HasLess α] [DecidableRel ((· < ·) : α → α → Prop)] (key : β → α) (left : Array β) (right : Array β) : Array β := do let rightMap := right.map (fun v => (key v, v)) |>.toList |> Std.RBMap.ofList (lt := (· < ·)) let mut result := right for v in left.reverse do if ¬ rightMap.contains (key v) then result := #[v] ++ result return result /-- Deletes all elements from `left` whose `key` is in `right`. -/ def diffBy [HasLess α] [DecidableRel ((· < ·) : α → α → Prop)] (key : β → α) (left : Array β) (right : Array α) : Array β := let rightMap := Std.RBTree.ofList (lt := (· < ·)) right.toList left.filter fun v => ¬ (rightMap.contains <| key v) end Array end Utils namespace Cli section Extensions /-- Prepends an author name to the description of the command. -/ def author (author : String) : Extension := { extendMeta := fun meta => { meta with description := s!"{author}\n{meta.description}" } } /-- Appends a longer description to the end of the help. -/ def longDescription (description : String) : Extension := { extendMeta := fun meta => { meta with furtherInformation? := return meta.furtherInformation?.optStr ++ lines #[ meta.furtherInformation?.optStr, (if meta.furtherInformation?.isSome then "\n" else "") ++ renderSection "DESCRIPTION" description ] } } /-- Sets default values for flags that were not set by the user according to `defaults := #[(long flag name, default value), ...]` and denotes the default value in the flag description of the help. Panics if one of the designated long flag names cannot be found in the command. -/ def defaultValues! (defaults : Array (String × String)) : Extension := let findDefaultFlags meta := defaults.map <| fun (longName, defaultValue) => ⟨meta.flag! longName, defaultValue⟩ { extendMeta := fun meta => let defaultFlags := findDefaultFlags meta let newMetaFlags := meta.flags.map fun flag => if let some defaultFlag := defaultFlags.find? (·.flag.longName = flag.longName) then { flag with description := flag.description ++ s!" [Default: `{defaultFlag.value}`]" } else flag { meta with flags := newMetaFlags } postprocess := fun meta parsed => let defaultFlags := findDefaultFlags meta return { parsed with flags := parsed.flags.leftUnionBy (·.flag.longName) defaultFlags } } /-- Errors if one of `requiredFlags := #[long flag name, ...]` were not passed by the user. Denotes that the flag is required in the flag description of the help. Panics if one of the designated long flag names cannot be found in the command. -/ def require! (requiredFlags : Array String) : Extension := let findRequiredFlags meta := requiredFlags.map (meta.flag! ·) { extendMeta := fun meta => let requiredFlags := findRequiredFlags meta let newMetaFlags := meta.flags.map fun flag => if let some requiredFlag := requiredFlags.find? (·.longName = flag.longName) then { flag with description := "[Required] " ++ flag.description } else flag { meta with flags := newMetaFlags } postprocess := fun meta parsed => do if parsed.hasFlag "help" ∨ parsed.hasFlag "version" then return parsed let requiredFlags := findRequiredFlags meta let missingFlags := requiredFlags.diffBy (·.longName) <| parsed.flags.map (·.flag.longName) if let some missingFlag ← pure <| missingFlags.get? 0 then throw s!"Missing required flag `--{missingFlag.longName}`." return parsed } end Extensions end Cli
848342d7c4778725a028a7754976e2d1777dbc34
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/order/lexicographic.lean
83bb4acc9b6982406ef50bf1366e1b79eeda58f7
[ "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,718
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Minchao Wu -/ import tactic.basic /-! # Lexicographic order This file defines the lexicographic relation for pairs and dependent pairs of orders, partial orders and linear orders. ## Main declarations * `lex α β`: Synonym of `α × β` to equip it with lexicographic order without creating conflicting instances. * `lex_<pre/partial_/linear_>order`: Instances lifting the orders on `α` and `β` to `lex α β` * `dlex_<pre/partial_/linear_>order`: Instances lifting the orders on every `Z a` to the dependent pair `Z`. ## See also The lexicographic ordering on lists is provided in `data.list.basic`. -/ universes u v /-- The cartesian product, equipped with the lexicographic order. -/ def lex (α : Type u) (β : Type v) := α × β variables {α : Type u} {β : Type v} meta instance [has_to_format α] [has_to_format β] : has_to_format (lex α β) := prod.has_to_format instance [decidable_eq α] [decidable_eq β] : decidable_eq (lex α β) := prod.decidable_eq instance [inhabited α] [inhabited β] : inhabited (lex α β) := prod.inhabited /-- Dictionary / lexicographic ordering on pairs. -/ instance lex_has_le [has_lt α] [has_le β] : has_le (lex α β) := { le := prod.lex (<) (≤) } instance lex_has_lt [has_lt α] [has_lt β] : has_lt (lex α β) := { lt := prod.lex (<) (<) } /-- Dictionary / lexicographic preorder for pairs. -/ instance lex_preorder [preorder α] [preorder β] : preorder (lex α β) := { le_refl := λ ⟨l, r⟩, by { right, apply le_refl }, le_trans := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ ⟨a₃, b₃⟩ ⟨h₁l, h₁r⟩ ⟨h₂l, h₂r⟩, { left, apply lt_trans, repeat { assumption } }, { left, assumption }, { left, assumption }, { right, apply le_trans, repeat { assumption } } end, lt_iff_le_not_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, split, { rintros (⟨_, _, _, _, hlt⟩ | ⟨_, _, _, hlt⟩), { split, { left, assumption }, { rintro ⟨l,r⟩, { apply lt_asymm hlt, assumption }, { apply lt_irrefl _ hlt } } }, { split, { right, rw lt_iff_le_not_le at hlt, exact hlt.1 }, { rintro ⟨l,r⟩, { apply lt_irrefl a₁, assumption }, { rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } }, { rintros ⟨⟨h₁ll, h₁lr⟩, h₂r⟩, { left, assumption }, { right, rw lt_iff_le_not_le, split, { assumption }, { intro h, apply h₂r, right, exact h } } } end, .. lex_has_le, .. lex_has_lt } /-- Dictionary / lexicographic partial_order for pairs. -/ instance lex_partial_order [partial_order α] [partial_order β] : partial_order (lex α β) := { le_antisymm := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ (⟨_, _, _, _, hlt₁⟩ | ⟨_, _, _, hlt₁⟩) (⟨_, _, _, _, hlt₂⟩ | ⟨_, _, _, hlt₂⟩), { exfalso, exact lt_irrefl a₁ (lt_trans hlt₁ hlt₂) }, { exfalso, exact lt_irrefl a₁ hlt₁ }, { exfalso, exact lt_irrefl a₁ hlt₂ }, { have := le_antisymm hlt₁ hlt₂, simp [this] } end .. lex_preorder } /-- Dictionary / lexicographic linear_order for pairs. -/ instance lex_linear_order [linear_order α] [linear_order β] : linear_order (lex α β) := { le_total := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, obtain ha | ha := le_total a₁ a₂; cases lt_or_eq_of_le ha with a_lt a_eq, -- Deal with the two goals with a₁ ≠ a₂ { left, left, exact a_lt }, swap, { right, left, exact a_lt }, -- Now deal with the two goals with a₁ = a₂ all_goals { subst a_eq, obtain hb | hb := le_total b₁ b₂ }, { left, right, exact hb }, { right, right, exact hb }, { left, right, exact hb }, { right, right, exact hb }, end, decidable_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, obtain a_lt | a_le := linear_order.decidable_le a₁ a₂, { -- a₂ < a₁ left, rw not_le at a_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, apply lt_trans, repeat { assumption } }, { apply lt_irrefl a₁, assumption } }, { -- a₁ ≤ a₂ by_cases h : a₁ = a₂, { rw h, obtain b_lt | b_le := linear_order.decidable_le b₁ b₂, { -- b₂ < b₁ left, rw not_le at b_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, assumption }, { apply lt_irrefl b₂, apply lt_of_lt_of_le, repeat { assumption } } }, -- b₁ ≤ b₂ { right, right, assumption } }, -- a₁ < a₂ { right, left, apply lt_of_le_of_ne, repeat { assumption } } } end, .. lex_partial_order }. variables {Z : α → Type v} /-- Dictionary / lexicographic ordering on dependent pairs. The 'pointwise' partial order `prod.has_le` doesn't make sense for dependent pairs, so it's safe to mark these as instances here. -/ instance dlex_has_le [preorder α] [∀ a, preorder (Z a)] : has_le (Σ' a, Z a) := { le := psigma.lex (<) (λ a, (≤)) } instance dlex_has_lt [preorder α] [∀ a, preorder (Z a)] : has_lt (Σ' a, Z a) := { lt := psigma.lex (<) (λ a, (<)) } /-- Dictionary / lexicographic preorder on dependent pairs. -/ instance dlex_preorder [preorder α] [∀ a, preorder (Z a)] : preorder (Σ' a, Z a) := { le_refl := λ ⟨l, r⟩, by { right, apply le_refl }, le_trans := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ ⟨a₃, b₃⟩ ⟨h₁l, h₁r⟩ ⟨h₂l, h₂r⟩, { left, apply lt_trans, repeat { assumption } }, { left, assumption }, { left, assumption }, { right, apply le_trans, repeat { assumption } } end, lt_iff_le_not_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, split, { rintros (⟨_, _, _, _, hlt⟩ | ⟨_, _, _, hlt⟩), { split, { left, assumption }, { rintro ⟨l,r⟩, { apply lt_asymm hlt, assumption }, { apply lt_irrefl _ hlt } } }, { split, { right, rw lt_iff_le_not_le at hlt, exact hlt.1 }, { rintro ⟨l,r⟩, { apply lt_irrefl a₁, assumption }, { rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } }, { rintros ⟨⟨h₁ll, h₁lr⟩, h₂r⟩, { left, assumption }, { right, rw lt_iff_le_not_le, split, { assumption }, { intro h, apply h₂r, right, exact h } } } end, .. dlex_has_le, .. dlex_has_lt } /-- Dictionary / lexicographic partial_order for dependent pairs. -/ instance dlex_partial_order [partial_order α] [∀ a, partial_order (Z a)] : partial_order (Σ' a, Z a) := { le_antisymm := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ (⟨_, _, _, _, hlt₁⟩ | ⟨_, _, _, hlt₁⟩) (⟨_, _, _, _, hlt₂⟩ | ⟨_, _, _, hlt₂⟩), { exfalso, exact lt_irrefl a₁ (lt_trans hlt₁ hlt₂) }, { exfalso, exact lt_irrefl a₁ hlt₁ }, { exfalso, exact lt_irrefl a₁ hlt₂ }, { have := le_antisymm hlt₁ hlt₂, simp [this] } end .. dlex_preorder } /-- Dictionary / lexicographic linear_order for pairs. -/ instance dlex_linear_order [linear_order α] [∀ a, linear_order (Z a)] : linear_order (Σ' a, Z a) := { le_total := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, obtain ha | ha := le_total a₁ a₂; cases lt_or_eq_of_le ha with a_lt a_eq, -- Deal with the two goals with a₁ ≠ a₂ { left, left, exact a_lt }, swap, { right, left, exact a_lt }, -- Now deal with the two goals with a₁ = a₂ all_goals { subst a_eq, obtain hb | hb := le_total b₁ b₂ }, { left, right, exact hb }, { right, right, exact hb }, { left, right, exact hb }, { right, right, exact hb }, end, decidable_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, obtain a_lt | a_le := linear_order.decidable_le a₁ a₂, { -- a₂ < a₁ left, rw not_le at a_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, apply lt_trans, repeat { assumption } }, { apply lt_irrefl a₁, assumption } }, { -- a₁ ≤ a₂ by_cases h : a₁ = a₂, { subst h, obtain b_lt | b_le := linear_order.decidable_le b₁ b₂, { -- b₂ < b₁ left, rw not_le at b_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₁, assumption }, { apply lt_irrefl b₂, apply lt_of_lt_of_le, repeat { assumption } } }, -- b₁ ≤ b₂ { right, right, assumption } }, -- a₁ < a₂ { right, left, apply lt_of_le_of_ne, repeat { assumption } } } end, .. dlex_partial_order }.
b6fde5911f254f936a64b6903be62553378603da
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/ring_theory/localization.lean
436f2a4d53a58c959eb966b5b58a4e86fb838a18
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
27,189
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro, Johan Commelin -/ import tactic.ring data.quot data.equiv.ring ring_theory.ideal_operations group_theory.submonoid universes u v local attribute [instance, priority 10] is_ring_hom.comp namespace localization variables (α : Type u) [comm_ring α] (S : set α) [is_submonoid S] def r (x y : α × S) : Prop := ∃ t ∈ S, ((x.2 : α) * y.1 - y.2 * x.1) * t = 0 local infix ≈ := r α S section variables {α S} theorem r_of_eq {a₀ a₁ : α × S} (h : (a₀.2 : α) * a₁.1 = a₁.2 * a₀.1) : a₀ ≈ a₁ := ⟨1, is_submonoid.one_mem S, by rw [h, sub_self, mul_one]⟩ end theorem refl (x : α × S) : x ≈ x := r_of_eq rfl theorem symm (x y : α × S) : x ≈ y → y ≈ x := λ ⟨t, hts, ht⟩, ⟨t, hts, by rw [← neg_sub, ← neg_mul_eq_neg_mul, ht, neg_zero]⟩ theorem trans : ∀ (x y z : α × S), x ≈ y → y ≈ z → x ≈ z := λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨r₃, s₃, hs₃⟩ ⟨t, hts, ht⟩ ⟨t', hts', ht'⟩, ⟨s₂ * t' * t, is_submonoid.mul_mem (is_submonoid.mul_mem hs₂ hts') hts, calc (s₁ * r₃ - s₃ * r₁) * (s₂ * t' * t) = t' * s₃ * ((s₁ * r₂ - s₂ * r₁) * t) + t * s₁ * ((s₂ * r₃ - s₃ * r₂) * t') : by ring ... = 0 : by simp only [subtype.coe_mk] at ht ht'; rw [ht, ht']; simp⟩ instance : setoid (α × S) := ⟨r α S, refl α S, symm α S, trans α S⟩ end localization /-- The localization of a ring at a submonoid: the elements of the submonoid become invertible in the localization.-/ def localization (α : Type u) [comm_ring α] (S : set α) [is_submonoid S] := quotient $ localization.setoid α S namespace localization variables (α : Type u) [comm_ring α] (S : set α) [is_submonoid S] instance : has_add (localization α S) := ⟨quotient.lift₂ (λ x y : α × S, (⟦⟨x.2 * y.1 + y.2 * x.1, x.2 * y.2⟩⟧ : localization α S)) $ λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨r₃, s₃, hs₃⟩ ⟨r₄, s₄, hs₄⟩ ⟨t₅, hts₅, ht₅⟩ ⟨t₆, hts₆, ht₆⟩, quotient.sound ⟨t₆ * t₅, is_submonoid.mul_mem hts₆ hts₅, calc (s₁ * s₂ * (s₃ * r₄ + s₄ * r₃) - s₃ * s₄ * (s₁ * r₂ + s₂ * r₁)) * (t₆ * t₅) = s₁ * s₃ * ((s₂ * r₄ - s₄ * r₂) * t₆) * t₅ + s₂ * s₄ * ((s₁ * r₃ - s₃ * r₁) * t₅) * t₆ : by ring ... = 0 : by simp only [subtype.coe_mk] at ht₅ ht₆; rw [ht₆, ht₅]; simp⟩⟩ instance : has_neg (localization α S) := ⟨quotient.lift (λ x : α × S, (⟦⟨-x.1, x.2⟩⟧ : localization α S)) $ λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨t, hts, ht⟩, quotient.sound ⟨t, hts, calc (s₁ * -r₂ - s₂ * -r₁) * t = -((s₁ * r₂ - s₂ * r₁) * t) : by ring ... = 0 : by simp only [subtype.coe_mk] at ht; rw ht; simp⟩⟩ instance : has_mul (localization α S) := ⟨quotient.lift₂ (λ x y : α × S, (⟦⟨x.1 * y.1, x.2 * y.2⟩⟧ : localization α S)) $ λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨r₃, s₃, hs₃⟩ ⟨r₄, s₄, hs₄⟩ ⟨t₅, hts₅, ht₅⟩ ⟨t₆, hts₆, ht₆⟩, quotient.sound ⟨t₆ * t₅, is_submonoid.mul_mem hts₆ hts₅, calc ((s₁ * s₂) * (r₃ * r₄) - (s₃ * s₄) * (r₁ * r₂)) * (t₆ * t₅) = t₆ * ((s₁ * r₃ - s₃ * r₁) * t₅) * r₂ * s₄ + t₅ * ((s₂ * r₄ - s₄ * r₂) * t₆) * r₃ * s₁ : by ring ... = 0 : by simp only [subtype.coe_mk] at ht₅ ht₆; rw [ht₅, ht₆]; simp⟩⟩ variables {α S} def mk (r : α) (s : S) : localization α S := ⟦(r, s)⟧ /-- The natural map from the ring to the localization.-/ def of (r : α) : localization α S := mk r 1 instance : comm_ring (localization α S) := by refine { add := has_add.add, add_assoc := λ m n k, quotient.induction_on₃ m n k _, zero := of 0, zero_add := quotient.ind _, add_zero := quotient.ind _, neg := has_neg.neg, add_left_neg := quotient.ind _, add_comm := quotient.ind₂ _, mul := has_mul.mul, mul_assoc := λ m n k, quotient.induction_on₃ m n k _, one := of 1, one_mul := quotient.ind _, mul_one := quotient.ind _, left_distrib := λ m n k, quotient.induction_on₃ m n k _, right_distrib := λ m n k, quotient.induction_on₃ m n k _, mul_comm := quotient.ind₂ _ }; { intros, try {rcases a with ⟨r₁, s₁, hs₁⟩}, try {rcases b with ⟨r₂, s₂, hs₂⟩}, try {rcases c with ⟨r₃, s₃, hs₃⟩}, refine (quotient.sound $ r_of_eq _), simp only [is_submonoid.coe_mul, is_submonoid.coe_one, subtype.coe_mk], ring } instance : inhabited (localization α S) := ⟨0⟩ instance of.is_ring_hom : is_ring_hom (of : α → localization α S) := { map_add := λ x y, quotient.sound $ by simp [add_comm], map_mul := λ x y, quotient.sound $ by simp [add_comm], map_one := rfl } variables {S} instance : has_coe_t α (localization α S) := ⟨of⟩ -- note [use has_coe_t] instance coe.is_ring_hom : is_ring_hom (coe : α → localization α S) := localization.of.is_ring_hom /-- The natural map from the submonoid to the unit group of the localization.-/ def to_units (s : S) : units (localization α S) := { val := s, inv := mk 1 s, val_inv := quotient.sound $ r_of_eq $ mul_assoc _ _ _, inv_val := quotient.sound $ r_of_eq $ show s.val * 1 * 1 = 1 * (1 * s.val), by simp } @[simp] lemma to_units_coe (s : S) : ((to_units s) : localization α S) = ((s : α) : localization α S) := rfl section variables (α S) (x y : α) (n : ℕ) @[simp] lemma of_zero : (of 0 : localization α S) = 0 := rfl @[simp] lemma of_one : (of 1 : localization α S) = 1 := rfl @[simp] lemma of_add : (of (x + y) : localization α S) = of x + of y := by apply is_ring_hom.map_add @[simp] lemma of_sub : (of (x - y) : localization α S) = of x - of y := by apply is_ring_hom.map_sub @[simp] lemma of_mul : (of (x * y) : localization α S) = of x * of y := by apply is_ring_hom.map_mul @[simp] lemma of_neg : (of (-x) : localization α S) = -of x := by apply is_ring_hom.map_neg @[simp] lemma of_pow : (of (x ^ n) : localization α S) = (of x) ^ n := by apply is_semiring_hom.map_pow @[simp] lemma of_is_unit' (s ∈ S) : is_unit (of s : localization α S) := is_unit_unit $ to_units ⟨s, ‹s ∈ S›⟩ @[simp] lemma of_is_unit (s : S) : is_unit (of s : localization α S) := is_unit_unit $ to_units s @[simp] lemma coe_zero : ((0 : α) : localization α S) = 0 := rfl @[simp] lemma coe_one : ((1 : α) : localization α S) = 1 := rfl @[simp] lemma coe_add : (↑(x + y) : localization α S) = x + y := of_add _ _ _ _ @[simp] lemma coe_sub : (↑(x - y) : localization α S) = x - y := of_sub _ _ _ _ @[simp] lemma coe_mul : (↑(x * y) : localization α S) = x * y := of_mul _ _ _ _ @[simp] lemma coe_neg : (↑(-x) : localization α S) = -x := of_neg _ _ _ @[simp] lemma coe_pow : (↑(x ^ n) : localization α S) = x ^ n := of_pow _ _ _ _ @[simp] lemma coe_is_unit' (s ∈ S) : is_unit ((s : α) : localization α S) := of_is_unit' _ _ _ ‹s ∈ S› @[simp] lemma coe_is_unit (s : S) : is_unit ((s : α) : localization α S) := of_is_unit _ _ _ end lemma mk_self {x : α} {hx : x ∈ S} : (mk x ⟨x, hx⟩ : localization α S) = 1 := quotient.sound ⟨1, is_submonoid.one_mem S, by simp only [subtype.coe_mk, is_submonoid.coe_one, mul_one, one_mul, sub_self]⟩ lemma mk_self' {s : S} : (mk s s : localization α S) = 1 := by cases s; exact mk_self lemma mk_self'' {s : S} : (mk s.1 s : localization α S) = 1 := mk_self' -- This lemma does not apply with simp, since (mk r s) simplifies to (r * s⁻¹). -- However, it could apply with dsimp. @[simp, nolint simp_nf] lemma coe_mul_mk (x y : α) (s : S) : ↑x * mk y s = mk (x * y) s := quotient.sound $ r_of_eq $ by rw one_mul lemma mk_eq_mul_mk_one (r : α) (s : S) : mk r s = r * mk 1 s := by rw [coe_mul_mk, mul_one] -- This lemma does not apply with simp, since (mk r s) simplifies to (r * s⁻¹). -- However, it could apply with dsimp. @[simp, nolint simp_nf] lemma mk_mul_mk (x y : α) (s t : S) : mk x s * mk y t = mk (x * y) (s * t) := rfl lemma mk_mul_cancel_left (r : α) (s : S) : mk (↑s * r) s = r := by rw [mk_eq_mul_mk_one, mul_comm ↑s, coe_mul, mul_assoc, ← mk_eq_mul_mk_one, mk_self', mul_one] lemma mk_mul_cancel_right (r : α) (s : S) : mk (r * s) s = r := by rw [mul_comm, mk_mul_cancel_left] @[simp] lemma mk_eq (r : α) (s : S) : mk r s = r * ((to_units s)⁻¹ : units _) := quotient.sound $ by simp @[elab_as_eliminator] protected theorem induction_on {C : localization α S → Prop} (x : localization α S) (ih : ∀ r s, C (mk r s : localization α S)) : C x := by rcases x with ⟨r, s⟩; exact ih r s section variables {β : Type v} [comm_ring β] {T : set β} [is_submonoid T] (f : α → β) [is_ring_hom f] @[elab_with_expected_type] def lift' (g : S → units β) (hg : ∀ s, (g s : β) = f s) (x : localization α S) : β := quotient.lift_on x (λ p, f p.1 * ((g p.2)⁻¹ : units β)) $ λ ⟨r₁, s₁⟩ ⟨r₂, s₂⟩ ⟨t, hts, ht⟩, show f r₁ * ↑(g s₁)⁻¹ = f r₂ * ↑(g s₂)⁻¹, from calc f r₁ * ↑(g s₁)⁻¹ = (f r₁ * g s₂ + ((g s₁ * f r₂ - g s₂ * f r₁) * g ⟨t, hts⟩) * ↑(g ⟨t, hts⟩)⁻¹) * ↑(g s₁)⁻¹ * ↑(g s₂)⁻¹ : by simp only [hg, subtype.coe_mk, (is_ring_hom.map_mul f).symm, (is_ring_hom.map_sub f).symm, ht, is_ring_hom.map_zero f, zero_mul, add_zero]; rw [is_ring_hom.map_mul f, ← hg, mul_right_comm, mul_assoc (f r₁), ← units.coe_mul, mul_inv_self]; rw [units.coe_one, mul_one] ... = f r₂ * ↑(g s₂)⁻¹ : by rw [mul_assoc, mul_assoc, ← units.coe_mul, mul_inv_self, units.coe_one, mul_one, mul_comm ↑(g s₂), add_sub_cancel'_right]; rw [mul_comm ↑(g s₁), ← mul_assoc, mul_assoc (f r₂), ← units.coe_mul, mul_inv_self, units.coe_one, mul_one] instance lift'.is_ring_hom (g : S → units β) (hg : ∀ s, (g s : β) = f s) : is_ring_hom (localization.lift' f g hg) := { map_one := have g 1 = 1, from units.ext (by rw hg; exact is_ring_hom.map_one f), show f 1 * ↑(g 1)⁻¹ = 1, by rw [this, one_inv, units.coe_one, mul_one, is_ring_hom.map_one f], map_mul := λ x y, localization.induction_on x $ λ r₁ s₁, localization.induction_on y $ λ r₂ s₂, have g (s₁ * s₂) = g s₁ * g s₂, from units.ext (by simp only [hg, units.coe_mul]; exact is_ring_hom.map_mul f), show _ * ↑(g (_ * _))⁻¹ = (_ * _) * (_ * _), by simp only [subtype.coe_mk, mul_one, one_mul, subtype.coe_eta, this, mul_inv_rev]; rw [is_ring_hom.map_mul f, units.coe_mul, ← mul_assoc, ← mul_assoc]; simp only [mul_right_comm], map_add := λ x y, localization.induction_on x $ λ r₁ s₁, localization.induction_on y $ λ r₂ s₂, have g (s₁ * s₂) = g s₁ * g s₂, from units.ext (by simp only [hg, units.coe_mul]; exact is_ring_hom.map_mul f), show _ * ↑(g (_ * _))⁻¹ = _ * _ + _ * _, by simp only [subtype.coe_mk, mul_one, one_mul, subtype.coe_eta, this, mul_inv_rev]; simp only [is_ring_hom.map_mul f, is_ring_hom.map_add f, add_mul, (hg _).symm]; simp only [mul_assoc, mul_comm, mul_left_comm, (units.coe_mul _ _).symm]; rw [mul_inv_cancel_left, mul_left_comm, ← mul_assoc, mul_inv_cancel_right, add_comm] } noncomputable def lift (h : ∀ s ∈ S, is_unit (f s)) : localization α S → β := localization.lift' f (λ s, classical.some $ h s.1 s.2) (λ s, by rw [← classical.some_spec (h s.1 s.2)]; refl) instance lift.is_ring_hom (h : ∀ s ∈ S, is_unit (f s)) : is_ring_hom (lift f h) := lift'.is_ring_hom _ _ _ -- This lemma does not apply with simp, since (mk r s) simplifies to (r * s⁻¹). -- However, it could apply with dsimp. @[simp, nolint simp_nf] lemma lift'_mk (g : S → units β) (hg : ∀ s, (g s : β) = f s) (r : α) (s : S) : lift' f g hg (mk r s) = f r * ↑(g s)⁻¹ := rfl @[simp] lemma lift'_of (g : S → units β) (hg : ∀ s, (g s : β) = f s) (a : α) : lift' f g hg (of a) = f a := have g 1 = 1, from units.ext_iff.2 $ by simp [hg, is_ring_hom.map_one f], by simp [lift', quotient.lift_on_beta, of, mk, this] @[simp] lemma lift'_coe (g : S → units β) (hg : ∀ s, (g s : β) = f s) (a : α) : lift' f g hg a = f a := lift'_of _ _ _ _ @[simp] lemma lift_of (h : ∀ s ∈ S, is_unit (f s)) (a : α) : lift f h (of a) = f a := lift'_of _ _ _ _ @[simp] lemma lift_coe (h : ∀ s ∈ S, is_unit (f s)) (a : α) : lift f h a = f a := lift'_of _ _ _ _ @[simp] lemma lift'_comp_of (g : S → units β) (hg : ∀ s, (g s : β) = f s) : lift' f g hg ∘ of = f := funext $ λ a, lift'_of _ _ _ a @[simp] lemma lift_comp_of (h : ∀ s ∈ S, is_unit (f s)) : lift f h ∘ of = f := lift'_comp_of _ _ _ @[simp] lemma lift'_apply_coe (f : localization α S → β) [is_ring_hom f] (g : S → units β) (hg : ∀ s, (g s : β) = f s) : lift' (λ a : α, f a) g hg = f := have g = (λ s, (units.map' f : units (localization α S) → units β) (to_units s)), from funext $ λ x, units.ext $ (hg x).symm ▸ rfl, funext $ λ x, localization.induction_on x (λ r s, by subst this; rw [lift'_mk, ← (units.map' f).map_inv, units.coe_map']; simp [is_ring_hom.map_mul f]) @[simp] lemma lift_apply_coe (f : localization α S → β) [is_ring_hom f] : lift (λ a : α, f a) (λ s hs, is_unit.map' f (is_unit_unit (to_units ⟨s, hs⟩))) = f := by rw [lift, lift'_apply_coe] /-- Function extensionality for localisations: two functions are equal if they agree on elements that are coercions.-/ protected lemma funext (f g : localization α S → β) [is_ring_hom f] [is_ring_hom g] (h : ∀ a : α, f a = g a) : f = g := begin rw [← lift_apply_coe f, ← lift_apply_coe g], congr' 1, exact funext h end variables {α S T} def map (hf : ∀ s ∈ S, f s ∈ T) : localization α S → localization β T := lift' (of ∘ f) (to_units ∘ subtype.map f hf) (λ s, rfl) instance map.is_ring_hom (hf : ∀ s ∈ S, f s ∈ T) : is_ring_hom (map f hf) := lift'.is_ring_hom _ _ _ @[simp] lemma map_of (hf : ∀ s ∈ S, f s ∈ T) (a : α) : map f hf (of a) = of (f a) := lift'_of _ _ _ _ @[simp] lemma map_coe (hf : ∀ s ∈ S, f s ∈ T) (a : α) : map f hf a = (f a) := lift'_of _ _ _ _ @[simp] lemma map_comp_of (hf : ∀ s ∈ S, f s ∈ T) : map f hf ∘ of = of ∘ f := funext $ λ a, map_of _ _ _ @[simp] lemma map_id : map id (λ s (hs : s ∈ S), hs) = id := localization.funext _ _ $ map_coe _ _ lemma map_comp_map {γ : Type*} [comm_ring γ] (hf : ∀ s ∈ S, f s ∈ T) (U : set γ) [is_submonoid U] (g : β → γ) [is_ring_hom g] (hg : ∀ t ∈ T, g t ∈ U) : map g hg ∘ map f hf = map (λ x, g (f x)) (λ s hs, hg _ (hf _ hs)) := localization.funext _ _ $ by simp lemma map_map {γ : Type*} [comm_ring γ] (hf : ∀ s ∈ S, f s ∈ T) (U : set γ) [is_submonoid U] (g : β → γ) [is_ring_hom g] (hg : ∀ t ∈ T, g t ∈ U) (x) : map g hg (map f hf x) = map (λ x, g (f x)) (λ s hs, hg _ (hf _ hs)) x := congr_fun (map_comp_map _ _ _ _ _) x def equiv_of_equiv (h₁ : α ≃+* β) (h₂ : h₁ '' S = T) : localization α S ≃+* localization β T := { to_fun := map h₁ $ λ s hs, h₂ ▸ set.mem_image_of_mem _ hs, inv_fun := map h₁.symm $ λ t ht, by simp [h₁.image_eq_preimage, set.preimage, set.ext_iff, *] at *, left_inv := λ _, by simp only [map_map, h₁.symm_apply_apply]; erw map_id; refl, right_inv := λ _, by simp only [map_map, h₁.apply_symm_apply]; erw map_id; refl, map_mul' := λ _ _, is_ring_hom.map_mul _, map_add' := λ _ _, is_ring_hom.map_add _ } end section away variables {β : Type v} [comm_ring β] (f : α → β) [is_ring_hom f] @[reducible] def away (x : α) := localization α (powers x) @[simp] def away.inv_self (x : α) : away x := mk 1 ⟨x, 1, pow_one x⟩ @[elab_with_expected_type] noncomputable def away.lift {x : α} (hfx : is_unit (f x)) : away x → β := localization.lift' f (λ s, classical.some hfx ^ classical.some s.2) $ λ s, by rw [units.coe_pow, ← classical.some_spec hfx, ← is_semiring_hom.map_pow f, classical.some_spec s.2]; refl instance away.lift.is_ring_hom {x : α} (hfx : is_unit (f x)) : is_ring_hom (localization.away.lift f hfx) := lift'.is_ring_hom _ _ _ @[simp] lemma away.lift_of {x : α} (hfx : is_unit (f x)) (a : α) : away.lift f hfx (of a) = f a := lift'_of _ _ _ _ @[simp] lemma away.lift_coe {x : α} (hfx : is_unit (f x)) (a : α) : away.lift f hfx a = f a := lift'_of _ _ _ _ @[simp] lemma away.lift_comp_of {x : α} (hfx : is_unit (f x)) : away.lift f hfx ∘ of = f := lift'_comp_of _ _ _ noncomputable def away_to_away_right (x y : α) : away x → away (x * y) := localization.away.lift coe $ is_unit_of_mul_one x (y * away.inv_self (x * y)) $ by rw [away.inv_self, coe_mul_mk, coe_mul_mk, mul_one, mk_self] instance away_to_away_right.is_ring_hom (x y : α) : is_ring_hom (away_to_away_right x y) := away.lift.is_ring_hom _ _ end away section at_prime variables (P : ideal α) [hp : ideal.is_prime P] include hp instance prime.is_submonoid : is_submonoid (-P : set α) := { one_mem := P.ne_top_iff_one.1 hp.1, mul_mem := λ x y hnx hny hxy, or.cases_on (hp.2 hxy) hnx hny } @[reducible] def at_prime := localization α (-P) instance at_prime.local_ring : local_ring (at_prime P) := local_of_nonunits_ideal (λ hze, let ⟨t, hts, ht⟩ := quotient.exact hze in hts $ have htz : t = 0, by simpa using ht, suffices (0:α) ∈ P, by rwa htz, P.zero_mem) (begin rintro ⟨⟨r₁, s₁, hs₁⟩⟩ ⟨⟨r₂, s₂, hs₂⟩⟩ hx hy hu, rcases is_unit_iff_exists_inv.1 hu with ⟨⟨⟨r₃, s₃, hs₃⟩⟩, hz⟩, rcases quotient.exact hz with ⟨t, hts, ht⟩, simp at ht, have : ∀ {r s hs}, (⟦⟨r, s, hs⟩⟧ : at_prime P) ∈ nonunits (at_prime P) → r ∈ P, { haveI := classical.dec, exact λ r s hs, not_imp_comm.1 (λ nr, is_unit_iff_exists_inv.2 ⟨⟦⟨s, r, nr⟩⟧, quotient.sound $ r_of_eq $ by simp [mul_comm]⟩) }, have hr₃ := (hp.mem_or_mem_of_mul_eq_zero ht).resolve_right hts, have := (ideal.add_mem_iff_left _ _).1 hr₃, { exact not_or (mt hp.mem_or_mem (not_or hs₁ hs₂)) hs₃ (hp.mem_or_mem this) }, { exact P.neg_mem (P.mul_mem_right (P.add_mem (P.mul_mem_left (this hy)) (P.mul_mem_left (this hx)))) } end) end at_prime variable (α) def non_zero_divisors : set α := {x | ∀ z, z * x = 0 → z = 0} instance non_zero_divisors.is_submonoid : is_submonoid (non_zero_divisors α) := { one_mem := λ z hz, by rwa mul_one at hz, mul_mem := λ x₁ x₂ hx₁ hx₂ z hz, have z * x₁ * x₂ = 0, by rwa mul_assoc, hx₁ z $ hx₂ (z * x₁) this } @[simp] lemma non_zero_divisors_one_val : (1 : non_zero_divisors α).val = 1 := rfl /-- The field of fractions of an integral domain.-/ @[reducible] def fraction_ring := localization α (non_zero_divisors α) namespace fraction_ring open function variables {β : Type u} [integral_domain β] [decidable_eq β] lemma eq_zero_of_ne_zero_of_mul_eq_zero {x y : β} : x ≠ 0 → y * x = 0 → y = 0 := λ hnx hxy, or.resolve_right (eq_zero_or_eq_zero_of_mul_eq_zero hxy) hnx lemma mem_non_zero_divisors_iff_ne_zero {x : β} : x ∈ non_zero_divisors β ↔ x ≠ 0 := ⟨λ hm hz, zero_ne_one (hm 1 $ by rw [hz, one_mul]).symm, λ hnx z, eq_zero_of_ne_zero_of_mul_eq_zero hnx⟩ variable (β) def inv_aux (x : β × (non_zero_divisors β)) : fraction_ring β := if h : x.1 = 0 then 0 else ⟦⟨x.2, x.1, mem_non_zero_divisors_iff_ne_zero.mpr h⟩⟧ instance : has_inv (fraction_ring β) := ⟨quotient.lift (inv_aux β) $ λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨t, hts, ht⟩, begin have hrs : s₁ * r₂ = 0 + s₂ * r₁, from sub_eq_iff_eq_add.1 (hts _ ht), by_cases hr₁ : r₁ = 0; by_cases hr₂ : r₂ = 0; simp [hr₁, hr₂] at hrs; simp only [inv_aux, hr₁, hr₂, dif_pos, dif_neg, not_false_iff, subtype.coe_mk, quotient.eq], { exfalso, exact mem_non_zero_divisors_iff_ne_zero.mp hs₁ hrs }, { exfalso, exact mem_non_zero_divisors_iff_ne_zero.mp hs₂ hrs }, { apply r_of_eq, simpa [mul_comm] using hrs.symm } end⟩ lemma mk_inv {r s} : (mk r s : fraction_ring β)⁻¹ = if h : r = 0 then 0 else ⟦⟨s, r, mem_non_zero_divisors_iff_ne_zero.mpr h⟩⟧ := rfl lemma mk_inv' : ∀ (x : β × (non_zero_divisors β)), (⟦x⟧⁻¹ : fraction_ring β) = if h : x.1 = 0 then 0 else ⟦⟨x.2.val, x.1, mem_non_zero_divisors_iff_ne_zero.mpr h⟩⟧ | ⟨r,s,hs⟩ := rfl instance : decidable_eq (fraction_ring β) := @quotient.decidable_eq (β × non_zero_divisors β) (localization.setoid β (non_zero_divisors β)) $ λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩, show decidable (∃ t ∈ non_zero_divisors β, (s₁ * r₂ - s₂ * r₁) * t = 0), from decidable_of_iff (s₁ * r₂ - s₂ * r₁ = 0) ⟨λ H, ⟨1, λ y, (mul_one y).symm ▸ id, H.symm ▸ zero_mul _⟩, λ ⟨t, ht1, ht2⟩, or.resolve_right (mul_eq_zero.1 ht2) $ λ ht, one_ne_zero (ht1 1 ((one_mul t).symm ▸ ht))⟩ instance : field (fraction_ring β) := by refine { inv := has_inv.inv, zero_ne_one := λ hzo, let ⟨t, hts, ht⟩ := quotient.exact hzo in zero_ne_one (by simpa using hts _ ht : 0 = 1), mul_inv_cancel := quotient.ind _, inv_zero := dif_pos rfl, .. localization.comm_ring }; { intros x hnx, rcases x with ⟨x, z, hz⟩, have : x ≠ 0, from assume hx, hnx (quotient.sound $ r_of_eq $ by simp [hx]), simp only [has_inv.inv, inv_aux, quotient.lift_beta, dif_neg this], exact (quotient.sound $ r_of_eq $ by simp [mul_comm]) } @[simp, nolint simp_nf] -- takes a crazy amount of time simplify lhs lemma mk_eq_div {r s} : (mk r s : fraction_ring β) = (r / s : fraction_ring β) := show _ = _ * dite (s.1 = 0) _ _, by rw [dif_neg (mem_non_zero_divisors_iff_ne_zero.mp s.2)]; exact localization.mk_eq_mul_mk_one _ _ variables {β} @[simp] lemma mk_eq_div' (x : β × (non_zero_divisors β)) : (⟦x⟧ : fraction_ring β) = ((x.1) / ((x.2).val) : fraction_ring β) := by erw ← mk_eq_div; cases x; refl lemma eq_zero_of (x : β) (h : (of x : fraction_ring β) = 0) : x = 0 := begin rcases quotient.exact h with ⟨t, ht, ht'⟩, simpa [mem_non_zero_divisors_iff_ne_zero.mp ht] using ht' end lemma of.injective : function.injective (of : β → fraction_ring β) := (is_add_group_hom.injective_iff _).mpr eq_zero_of section map open function is_ring_hom variables {A : Type u} [integral_domain A] [decidable_eq A] variables {B : Type v} [integral_domain B] [decidable_eq B] variables (f : A → B) [is_ring_hom f] def map (hf : injective f) : fraction_ring A → fraction_ring B := localization.map f $ λ s h, by rw [mem_non_zero_divisors_iff_ne_zero, ← map_zero f, ne.def, hf.eq_iff]; exact mem_non_zero_divisors_iff_ne_zero.1 h @[simp] lemma map_of (hf : injective f) (a : A) : map f hf (of a) = of (f a) := localization.map_of _ _ _ @[simp] lemma map_coe (hf : injective f) (a : A) : map f hf a = f a := localization.map_coe _ _ _ @[simp] lemma map_comp_of (hf : injective f) : map f hf ∘ (of : A → fraction_ring A) = (of : B → fraction_ring B) ∘ f := localization.map_comp_of _ _ instance map.is_ring_hom (hf : injective f) : is_ring_hom (map f hf) := localization.map.is_ring_hom _ _ def equiv_of_equiv (h : A ≃+* B) : fraction_ring A ≃+* fraction_ring B := localization.equiv_of_equiv h begin ext b, rw [h.image_eq_preimage, set.preimage, set.mem_set_of_eq, mem_non_zero_divisors_iff_ne_zero, mem_non_zero_divisors_iff_ne_zero, ne.def], exact h.symm.map_ne_zero_iff end end map end fraction_ring section ideals theorem map_comap (J : ideal (localization α S)) : ideal.map (ring_hom.of coe) (ideal.comap (ring_hom.of (coe : α → localization α S)) J) = J := le_antisymm (ideal.map_le_iff_le_comap.2 (le_refl _)) $ λ x, localization.induction_on x $ λ r s hJ, (submodule.mem_coe _).2 $ mul_one r ▸ coe_mul_mk r 1 s ▸ (ideal.mul_mem_right _ $ ideal.mem_map_of_mem $ have _ := @ideal.mul_mem_left (localization α S) _ _ s _ hJ, by rwa [coe_coe, coe_mul_mk, mk_mul_cancel_left] at this) def le_order_embedding : ((≤) : ideal (localization α S) → ideal (localization α S) → Prop) ≼o ((≤) : ideal α → ideal α → Prop) := { to_fun := λ J, ideal.comap (ring_hom.of coe) J, inj := function.injective_of_left_inverse (map_comap α), ord := λ J₁ J₂, ⟨ideal.comap_mono, λ hJ, map_comap α J₁ ▸ map_comap α J₂ ▸ ideal.map_mono hJ⟩ } end ideals section module /-! ### `module` section Localizations form an algebra over `α` induced by the embedding `coe : α → localization α S`. -/ set_option class.instance_max_depth 50 variables (α S) instance : algebra α (localization α S) := algebra.of_ring_hom coe (is_ring_hom.of_semiring coe) lemma of_smul (c x : α) : (of (c • x) : localization α S) = c • of x := by { simp, refl } lemma coe_smul (c x : α) : (coe (c • x) : localization α S) = c • coe x := of_smul α S c x lemma coe_mul_eq_smul (c : α) (x : localization α S) : coe c * x = c • x := rfl lemma mul_coe_eq_smul (c : α) (x : localization α S) : x * coe c = c • x := mul_comm x (coe c) /-- The embedding `coe : α → localization α S` induces a linear map. -/ def lin_coe : α →ₗ[α] localization α S := ⟨coe, coe_add α S, coe_smul α S⟩ @[simp] lemma lin_coe_apply (a : α) : lin_coe α S a = coe a := rfl instance coe_submodules : has_coe (ideal α) (submodule α (localization α S)) := ⟨submodule.map (lin_coe _ _)⟩ @[simp] lemma of_id (a : α) : (algebra.of_id α (localization α S) : α → localization α S) a = ↑a := rfl end module section is_integer /-- `a : localization α S` is an integer if it is an element of the original ring `α` -/ def is_integer (S : set α) [is_submonoid S] (a : localization α S) : Prop := a ∈ set.range (coe : α → localization α S) lemma is_integer_coe (a : α) : is_integer α S a := ⟨a, rfl⟩ lemma is_integer_add {a b} (ha : is_integer α S a) (hb : is_integer α S b) : is_integer α S (a + b) := begin rcases ha with ⟨a', ha⟩, rcases hb with ⟨b', hb⟩, use a' + b', rw [coe_add, ha, hb] end lemma is_integer_mul {a b} (ha : is_integer α S a) (hb : is_integer α S b) : is_integer α S (a * b) := begin rcases ha with ⟨a', ha⟩, rcases hb with ⟨b', hb⟩, use a' * b', rw [coe_mul, ha, hb] end set_option class.instance_max_depth 50 lemma is_integer_smul {a : α} {b} (hb : is_integer α S b) : is_integer α S (a • b) := begin rcases hb with ⟨b', hb⟩, use a * b', rw [←hb, ←coe_smul, smul_eq_mul] end end is_integer end localization
818fd40271096f8cd2218aa4e089f987d382ed8a
4f065978c49388d188224610d9984673079f7d91
/canonically_isomorphic.lean
aac568210b79a9f2ef151c72f64d9f5376f91e19
[]
no_license
kckennylau/Lean
b323103f52706304907adcfaee6f5cb8095d4a33
907d0a4d2bd8f23785abd6142ad53d308c54fdcb
refs/heads/master
1,624,623,720,653
1,563,901,820,000
1,563,901,820,000
109,506,702
3
1
null
null
null
null
UTF-8
Lean
false
false
1,390
lean
universe zfc_u structure equiv' (α : Type zfc_u) (β : Type zfc_u) := (i : α → β) (j : β → α) (ij : ∀ x, j (i x) = x) (ji : ∀ y, i (j y) = y) namespace equiv' variables {α β γ : Type zfc_u} @[refl] protected def refl (α : Type zfc_u) : equiv' α α := ⟨id, id, λ x, rfl, λ x, rfl⟩ @[symm] protected def symm (e : equiv' α β) : equiv' β α := ⟨e.j, e.i, e.ji, e.ij⟩ @[trans] protected def trans (e₁ : equiv' α β) (e₂ : equiv' β γ) : equiv' α γ := ⟨e₂.i ∘ e₁.i, e₁.j ∘ e₂.j, λ x, by simp [e₂.ij, e₁.ij], λ x, by simp [e₁.ji, e₂.ji]⟩ definition mul_is_add {α : Type zfc_u} : equiv' (has_mul α) (has_add α) := { i := λ ⟨mul⟩, ⟨mul⟩, j := λ ⟨mul⟩, ⟨mul⟩, -- didn't I just write that? ij := λ ⟨x⟩, rfl, ji := λ ⟨x⟩, rfl, -- didn't I just write that? } definition equiv_mul {α β : Type zfc_u} : equiv' α β → equiv' (has_mul α) (has_mul β) := λ E, { i := λ ⟨f⟩, ⟨λ b1 b2, E.i (f (E.j b1) (E.j b2))⟩, j := λ ⟨f⟩, ⟨λ a1 a2, E.j (f (E.i a1) (E.i a2))⟩, ij := λ ⟨f⟩, congr_arg has_mul.mk $ by funext; simp [E.ij], ji := λ ⟨f⟩, congr_arg has_mul.mk $ by funext; simp [E.ji] } definition mul_to_add {α β : Type} : equiv' α β → equiv' (has_add α) (has_add β) := λ H, mul_is_add.symm.trans $ (equiv_mul H).trans mul_is_add end equiv'
0bb972852c0995a131c04cf34a17af0e00ab4d27
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/class3.lean
9c81dd297dbc3cd81e6222a0f2f0534a9b7ab26e
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
428
lean
import standard using num pair section parameter {A : Type} parameter {B : Type} parameter Ha : inhabited A parameter Hb : inhabited B -- The section mechanism only includes parameters that are explicitly cited. -- So, we use the 'including' expression to make explicit we want to use -- Ha and Hb theorem tst : inhabited (Prop × A × B) := including Ha Hb, _ end (* print(get_env():find("tst"):value()) *)
dbfe442096913ae04acd24d7bbd2d5748da8ef6b
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/decomposition/lebesgue.lean
d42dfab5e70c2c5337c9df2ba1fb0533393b18ed
[ "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
63,303
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import measure_theory.measure.complex import measure_theory.measure.sub import measure_theory.decomposition.jordan import measure_theory.measure.with_density_vector_measure import measure_theory.function.ae_eq_of_integral /-! # Lebesgue decomposition This file proves the Lebesgue decomposition theorem. The Lebesgue decomposition theorem states that, given two σ-finite measures `μ` and `ν`, there exists a σ-finite measure `ξ` and a measurable function `f` such that `μ = ξ + fν` and `ξ` is mutually singular with respect to `ν`. The Lebesgue decomposition provides the Radon-Nikodym theorem readily. ## Main definitions * `measure_theory.measure.have_lebesgue_decomposition` : A pair of measures `μ` and `ν` is said to `have_lebesgue_decomposition` if there exist a measure `ξ` and a measurable function `f`, such that `ξ` is mutually singular with respect to `ν` and `μ = ξ + ν.with_density f` * `measure_theory.measure.singular_part` : If a pair of measures `have_lebesgue_decomposition`, then `singular_part` chooses the measure from `have_lebesgue_decomposition`, otherwise it returns the zero measure. * `measure_theory.measure.rn_deriv`: If a pair of measures `have_lebesgue_decomposition`, then `rn_deriv` chooses the measurable function from `have_lebesgue_decomposition`, otherwise it returns the zero function. * `measure_theory.signed_measure.have_lebesgue_decomposition` : A signed measure `s` and a measure `μ` is said to `have_lebesgue_decomposition` if both the positive part and negative part of `s` `have_lebesgue_decomposition` with respect to `μ`. * `measure_theory.signed_measure.singular_part` : The singular part between a signed measure `s` and a measure `μ` is simply the singular part of the positive part of `s` with respect to `μ` minus the singular part of the negative part of `s` with respect to `μ`. * `measure_theory.signed_measure.rn_deriv` : The Radon-Nikodym derivative of a signed measure `s` with respect to a measure `μ` is the Radon-Nikodym derivative of the positive part of `s` with respect to `μ` minus the Radon-Nikodym derivative of the negative part of `s` with respect to `μ`. ## Main results * `measure_theory.measure.have_lebesgue_decomposition_of_sigma_finite` : the Lebesgue decomposition theorem. * `measure_theory.measure.eq_singular_part` : Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a measurable function such that `μ = s + fν`, then `s = μ.singular_part ν`. * `measure_theory.measure.eq_rn_deriv` : Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a measurable function such that `μ = s + fν`, then `f = μ.rn_deriv ν`. * `measure_theory.signed_measure.singular_part_add_with_density_rn_deriv_eq` : the Lebesgue decomposition theorem between a signed measure and a σ-finite positive measure. # Tags Lebesgue decomposition theorem -/ noncomputable theory open_locale classical measure_theory nnreal ennreal open set variables {α β : Type*} {m : measurable_space α} {μ ν : measure_theory.measure α} include m namespace measure_theory namespace measure /-- A pair of measures `μ` and `ν` is said to `have_lebesgue_decomposition` if there exists a measure `ξ` and a measurable function `f`, such that `ξ` is mutually singular with respect to `ν` and `μ = ξ + ν.with_density f`. -/ class have_lebesgue_decomposition (μ ν : measure α) : Prop := (lebesgue_decomposition : ∃ (p : measure α × (α → ℝ≥0∞)), measurable p.2 ∧ p.1 ⊥ₘ ν ∧ μ = p.1 + ν.with_density p.2) /-- If a pair of measures `have_lebesgue_decomposition`, then `singular_part` chooses the measure from `have_lebesgue_decomposition`, otherwise it returns the zero measure. For sigma-finite measures, `μ = μ.singular_part ν + ν.with_density (μ.rn_deriv ν)`. -/ @[irreducible] def singular_part (μ ν : measure α) : measure α := if h : have_lebesgue_decomposition μ ν then (classical.some h.lebesgue_decomposition).1 else 0 /-- If a pair of measures `have_lebesgue_decomposition`, then `rn_deriv` chooses the measurable function from `have_lebesgue_decomposition`, otherwise it returns the zero function. For sigma-finite measures, `μ = μ.singular_part ν + ν.with_density (μ.rn_deriv ν)`.-/ @[irreducible] def rn_deriv (μ ν : measure α) : α → ℝ≥0∞ := if h : have_lebesgue_decomposition μ ν then (classical.some h.lebesgue_decomposition).2 else 0 lemma have_lebesgue_decomposition_spec (μ ν : measure α) [h : have_lebesgue_decomposition μ ν] : measurable (μ.rn_deriv ν) ∧ (μ.singular_part ν) ⊥ₘ ν ∧ μ = (μ.singular_part ν) + ν.with_density (μ.rn_deriv ν) := begin rw [singular_part, rn_deriv, dif_pos h, dif_pos h], exact classical.some_spec h.lebesgue_decomposition, end lemma have_lebesgue_decomposition_add (μ ν : measure α) [have_lebesgue_decomposition μ ν] : μ = (μ.singular_part ν) + ν.with_density (μ.rn_deriv ν) := (have_lebesgue_decomposition_spec μ ν).2.2 instance have_lebesgue_decomposition_smul (μ ν : measure α) [have_lebesgue_decomposition μ ν] (r : ℝ≥0) : (r • μ).have_lebesgue_decomposition ν := { lebesgue_decomposition := begin obtain ⟨hmeas, hsing, hadd⟩ := have_lebesgue_decomposition_spec μ ν, refine ⟨⟨r • μ.singular_part ν, r • μ.rn_deriv ν⟩, _, hsing.smul _, _⟩, { change measurable ((r : ℝ≥0∞) • _), -- cannot remove this line exact hmeas.const_smul _ }, { change _ = (r : ℝ≥0∞) • _ + ν.with_density ((r : ℝ≥0∞) • _), rw [with_density_smul _ hmeas, ← smul_add, ← hadd], refl } end } @[measurability] lemma measurable_rn_deriv (μ ν : measure α) : measurable $ μ.rn_deriv ν := begin by_cases h : have_lebesgue_decomposition μ ν, { exactI (have_lebesgue_decomposition_spec μ ν).1 }, { rw [rn_deriv, dif_neg h], exact measurable_zero } end lemma mutually_singular_singular_part (μ ν : measure α) : μ.singular_part ν ⊥ₘ ν := begin by_cases h : have_lebesgue_decomposition μ ν, { exactI (have_lebesgue_decomposition_spec μ ν).2.1 }, { rw [singular_part, dif_neg h], exact mutually_singular.zero_left } end lemma singular_part_le (μ ν : measure α) : μ.singular_part ν ≤ μ := begin by_cases hl : have_lebesgue_decomposition μ ν, { casesI (have_lebesgue_decomposition_spec μ ν).2 with _ h, conv_rhs { rw h }, exact measure.le_add_right le_rfl }, { rw [singular_part, dif_neg hl], exact measure.zero_le μ } end lemma with_density_rn_deriv_le (μ ν : measure α) : ν.with_density (μ.rn_deriv ν) ≤ μ := begin by_cases hl : have_lebesgue_decomposition μ ν, { casesI (have_lebesgue_decomposition_spec μ ν).2 with _ h, conv_rhs { rw h }, exact measure.le_add_left le_rfl }, { rw [rn_deriv, dif_neg hl, with_density_zero], exact measure.zero_le μ } end instance [is_finite_measure μ] : is_finite_measure (μ.singular_part ν) := is_finite_measure_of_le μ $ singular_part_le μ ν instance [sigma_finite μ] : sigma_finite (μ.singular_part ν) := sigma_finite_of_le μ $ singular_part_le μ ν instance [topological_space α] [is_locally_finite_measure μ] : is_locally_finite_measure (μ.singular_part ν) := is_locally_finite_measure_of_le $ singular_part_le μ ν instance [is_finite_measure μ] : is_finite_measure (ν.with_density $ μ.rn_deriv ν) := is_finite_measure_of_le μ $ with_density_rn_deriv_le μ ν instance [sigma_finite μ] : sigma_finite (ν.with_density $ μ.rn_deriv ν) := sigma_finite_of_le μ $ with_density_rn_deriv_le μ ν instance [topological_space α] [is_locally_finite_measure μ] : is_locally_finite_measure (ν.with_density $ μ.rn_deriv ν) := is_locally_finite_measure_of_le $ with_density_rn_deriv_le μ ν lemma lintegral_rn_deriv_lt_top_of_measure_ne_top {μ : measure α} (ν : measure α) {s : set α} (hs : μ s ≠ ∞) : ∫⁻ x in s, μ.rn_deriv ν x ∂ν < ∞ := begin by_cases hl : have_lebesgue_decomposition μ ν, { haveI := hl, obtain ⟨-, -, hadd⟩ := have_lebesgue_decomposition_spec μ ν, suffices : ∫⁻ x in to_measurable μ s, μ.rn_deriv ν x ∂ν < ∞, from lt_of_le_of_lt (lintegral_mono_set (subset_to_measurable _ _)) this, rw [← with_density_apply _ (measurable_set_to_measurable _ _)], refine lt_of_le_of_lt (le_add_left le_rfl : _ ≤ μ.singular_part ν (to_measurable μ s) + ν.with_density (μ.rn_deriv ν) (to_measurable μ s)) _, rw [← measure.add_apply, ← hadd, measure_to_measurable], exact hs.lt_top }, { erw [measure.rn_deriv, dif_neg hl, lintegral_zero], exact with_top.zero_lt_top }, end lemma lintegral_rn_deriv_lt_top (μ ν : measure α) [is_finite_measure μ] : ∫⁻ x, μ.rn_deriv ν x ∂ν < ∞ := begin rw [← set_lintegral_univ], exact lintegral_rn_deriv_lt_top_of_measure_ne_top _ (measure_lt_top _ _).ne, end /-- The Radon-Nikodym derivative of a sigma-finite measure `μ` with respect to another measure `ν` is `ν`-almost everywhere finite. -/ theorem rn_deriv_lt_top (μ ν : measure α) [sigma_finite μ] : ∀ᵐ x ∂ν, μ.rn_deriv ν x < ∞ := begin suffices : ∀ n, ∀ᵐ x ∂ν, x ∈ spanning_sets μ n → μ.rn_deriv ν x < ∞, { filter_upwards [ae_all_iff.2 this] with _ hx using hx _ (mem_spanning_sets_index _ _), }, assume n, rw ← ae_restrict_iff' (measurable_spanning_sets _ _), apply ae_lt_top (measurable_rn_deriv _ _), refine (lintegral_rn_deriv_lt_top_of_measure_ne_top _ _).ne, exact (measure_spanning_sets_lt_top _ _).ne end /-- Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a measurable function such that `μ = s + fν`, then `s = μ.singular_part μ`. This theorem provides the uniqueness of the `singular_part` in the Lebesgue decomposition theorem, while `measure_theory.measure.eq_rn_deriv` provides the uniqueness of the `rn_deriv`. -/ theorem eq_singular_part {s : measure α} {f : α → ℝ≥0∞} (hf : measurable f) (hs : s ⊥ₘ ν) (hadd : μ = s + ν.with_density f) : s = μ.singular_part ν := begin haveI : have_lebesgue_decomposition μ ν := ⟨⟨⟨s, f⟩, hf, hs, hadd⟩⟩, obtain ⟨hmeas, hsing, hadd'⟩ := have_lebesgue_decomposition_spec μ ν, obtain ⟨⟨S, hS₁, hS₂, hS₃⟩, ⟨T, hT₁, hT₂, hT₃⟩⟩ := ⟨hs, hsing⟩, rw hadd' at hadd, have hνinter : ν (S ∩ T)ᶜ = 0, { rw compl_inter, refine nonpos_iff_eq_zero.1 (le_trans (measure_union_le _ _) _), rw [hT₃, hS₃, add_zero], exact le_rfl }, have heq : s.restrict (S ∩ T)ᶜ = (μ.singular_part ν).restrict (S ∩ T)ᶜ, { ext1 A hA, have hf : ν.with_density f (A ∩ (S ∩ T)ᶜ) = 0, { refine with_density_absolutely_continuous ν _ _, rw ← nonpos_iff_eq_zero, exact hνinter ▸ measure_mono (inter_subset_right _ _) }, have hrn : ν.with_density (μ.rn_deriv ν) (A ∩ (S ∩ T)ᶜ) = 0, { refine with_density_absolutely_continuous ν _ _, rw ← nonpos_iff_eq_zero, exact hνinter ▸ measure_mono (inter_subset_right _ _) }, rw [restrict_apply hA, restrict_apply hA, ← add_zero (s (A ∩ (S ∩ T)ᶜ)), ← hf, ← add_apply, ← hadd, add_apply, hrn, add_zero] }, have heq' : ∀ A : set α, measurable_set A → s A = s.restrict (S ∩ T)ᶜ A, { intros A hA, have hsinter : s (A ∩ (S ∩ T)) = 0, { rw ← nonpos_iff_eq_zero, exact hS₂ ▸ measure_mono ((inter_subset_right _ _).trans (inter_subset_left _ _)) }, rw [restrict_apply hA, ← diff_eq, ae_disjoint.measure_diff_left hsinter] }, ext1 A hA, have hμinter : μ.singular_part ν (A ∩ (S ∩ T)) = 0, { rw ← nonpos_iff_eq_zero, exact hT₂ ▸ measure_mono ((inter_subset_right _ _).trans (inter_subset_right _ _)) }, rw [heq' A hA, heq, restrict_apply hA, ← diff_eq, ae_disjoint.measure_diff_left hμinter] end lemma singular_part_zero (ν : measure α) : (0 : measure α).singular_part ν = 0 := begin refine (eq_singular_part measurable_zero mutually_singular.zero_left _).symm, rw [zero_add, with_density_zero], end lemma singular_part_smul (μ ν : measure α) (r : ℝ≥0) : (r • μ).singular_part ν = r • (μ.singular_part ν) := begin by_cases hr : r = 0, { rw [hr, zero_smul, zero_smul, singular_part_zero] }, by_cases hl : have_lebesgue_decomposition μ ν, { haveI := hl, refine (eq_singular_part ((measurable_rn_deriv μ ν).const_smul (r : ℝ≥0∞)) (mutually_singular.smul r (have_lebesgue_decomposition_spec _ _).2.1) _).symm, rw [with_density_smul _ (measurable_rn_deriv _ _), ← smul_add, ← have_lebesgue_decomposition_add μ ν, ennreal.smul_def] }, { rw [singular_part, singular_part, dif_neg hl, dif_neg, smul_zero], refine λ hl', hl _, rw ← inv_smul_smul₀ hr μ, exact @measure.have_lebesgue_decomposition_smul _ _ _ _ hl' _ } end lemma singular_part_add (μ₁ μ₂ ν : measure α) [have_lebesgue_decomposition μ₁ ν] [have_lebesgue_decomposition μ₂ ν] : (μ₁ + μ₂).singular_part ν = μ₁.singular_part ν + μ₂.singular_part ν := begin refine (eq_singular_part ((measurable_rn_deriv μ₁ ν).add (measurable_rn_deriv μ₂ ν)) ((have_lebesgue_decomposition_spec _ _).2.1.add_left (have_lebesgue_decomposition_spec _ _).2.1) _).symm, erw with_density_add_left (measurable_rn_deriv μ₁ ν), conv_rhs { rw [add_assoc, add_comm (μ₂.singular_part ν), ← add_assoc, ← add_assoc] }, rw [← have_lebesgue_decomposition_add μ₁ ν, add_assoc, add_comm (ν.with_density (μ₂.rn_deriv ν)), ← have_lebesgue_decomposition_add μ₂ ν] end lemma singular_part_with_density (ν : measure α) {f : α → ℝ≥0∞} (hf : measurable f) : (ν.with_density f).singular_part ν = 0 := begin have : ν.with_density f = 0 + ν.with_density f, by rw zero_add, exact (eq_singular_part hf mutually_singular.zero_left this).symm, end /-- Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a measurable function such that `μ = s + fν`, then `f = μ.rn_deriv ν`. This theorem provides the uniqueness of the `rn_deriv` in the Lebesgue decomposition theorem, while `measure_theory.measure.eq_singular_part` provides the uniqueness of the `singular_part`. Here, the uniqueness is given in terms of the measures, while the uniqueness in terms of the functions is given in `eq_rn_deriv`. -/ theorem eq_with_density_rn_deriv {s : measure α} {f : α → ℝ≥0∞} (hf : measurable f) (hs : s ⊥ₘ ν) (hadd : μ = s + ν.with_density f) : ν.with_density f = ν.with_density (μ.rn_deriv ν) := begin haveI : have_lebesgue_decomposition μ ν := ⟨⟨⟨s, f⟩, hf, hs, hadd⟩⟩, obtain ⟨hmeas, hsing, hadd'⟩ := have_lebesgue_decomposition_spec μ ν, obtain ⟨⟨S, hS₁, hS₂, hS₃⟩, ⟨T, hT₁, hT₂, hT₃⟩⟩ := ⟨hs, hsing⟩, rw hadd' at hadd, have hνinter : ν (S ∩ T)ᶜ = 0, { rw compl_inter, refine nonpos_iff_eq_zero.1 (le_trans (measure_union_le _ _) _), rw [hT₃, hS₃, add_zero], exact le_rfl }, have heq : (ν.with_density f).restrict (S ∩ T) = (ν.with_density (μ.rn_deriv ν)).restrict (S ∩ T), { ext1 A hA, have hs : s (A ∩ (S ∩ T)) = 0, { rw ← nonpos_iff_eq_zero, exact hS₂ ▸ measure_mono ((inter_subset_right _ _).trans (inter_subset_left _ _)) }, have hsing : μ.singular_part ν (A ∩ (S ∩ T)) = 0, { rw ← nonpos_iff_eq_zero, exact hT₂ ▸ measure_mono ((inter_subset_right _ _).trans (inter_subset_right _ _)) }, rw [restrict_apply hA, restrict_apply hA, ← add_zero (ν.with_density f (A ∩ (S ∩ T))), ← hs, ← add_apply, add_comm, ← hadd, add_apply, hsing, zero_add] }, have heq' : ∀ A : set α, measurable_set A → ν.with_density f A = (ν.with_density f).restrict (S ∩ T) A, { intros A hA, have hνfinter : ν.with_density f (A ∩ (S ∩ T)ᶜ) = 0, { rw ← nonpos_iff_eq_zero, exact with_density_absolutely_continuous ν f hνinter ▸ measure_mono (inter_subset_right _ _) }, rw [restrict_apply hA, ← add_zero (ν.with_density f (A ∩ (S ∩ T))), ← hνfinter, ← diff_eq, measure_inter_add_diff _ (hS₁.inter hT₁)] }, ext1 A hA, have hνrn : ν.with_density (μ.rn_deriv ν) (A ∩ (S ∩ T)ᶜ) = 0, { rw ← nonpos_iff_eq_zero, exact with_density_absolutely_continuous ν (μ.rn_deriv ν) hνinter ▸ measure_mono (inter_subset_right _ _) }, rw [heq' A hA, heq, ← add_zero ((ν.with_density (μ.rn_deriv ν)).restrict (S ∩ T) A), ← hνrn, restrict_apply hA, ← diff_eq, measure_inter_add_diff _ (hS₁.inter hT₁)] end /-- Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a measurable function such that `μ = s + fν`, then `f = μ.rn_deriv ν`. This theorem provides the uniqueness of the `rn_deriv` in the Lebesgue decomposition theorem, while `measure_theory.measure.eq_singular_part` provides the uniqueness of the `singular_part`. Here, the uniqueness is given in terms of the functions, while the uniqueness in terms of the functions is given in `eq_with_density_rn_deriv`. -/ theorem eq_rn_deriv [sigma_finite ν] {s : measure α} {f : α → ℝ≥0∞} (hf : measurable f) (hs : s ⊥ₘ ν) (hadd : μ = s + ν.with_density f) : f =ᵐ[ν] μ.rn_deriv ν := begin refine ae_eq_of_forall_set_lintegral_eq_of_sigma_finite hf (measurable_rn_deriv μ ν) _, assume a ha h'a, calc ∫⁻ (x : α) in a, f x ∂ν = ν.with_density f a : (with_density_apply f ha).symm ... = ν.with_density (μ.rn_deriv ν) a : by rw eq_with_density_rn_deriv hf hs hadd ... = ∫⁻ (x : α) in a, μ.rn_deriv ν x ∂ν : with_density_apply _ ha end /-- The Radon-Nikodym derivative of `f ν` with respect to `ν` is `f`. -/ theorem rn_deriv_with_density (ν : measure α) [sigma_finite ν] {f : α → ℝ≥0∞} (hf : measurable f) : (ν.with_density f).rn_deriv ν =ᵐ[ν] f := begin have : ν.with_density f = 0 + ν.with_density f, by rw zero_add, exact (eq_rn_deriv hf mutually_singular.zero_left this).symm, end /-- The Radon-Nikodym derivative of the restriction of a measure to a measurable set is the indicator function of this set. -/ theorem rn_deriv_restrict (ν : measure α) [sigma_finite ν] {s : set α} (hs : measurable_set s) : (ν.restrict s).rn_deriv ν =ᵐ[ν] s.indicator 1 := begin rw ← with_density_indicator_one hs, exact rn_deriv_with_density _ (measurable_one.indicator hs) end open vector_measure signed_measure /-- If two finite measures `μ` and `ν` are not mutually singular, there exists some `ε > 0` and a measurable set `E`, such that `ν(E) > 0` and `E` is positive with respect to `μ - εν`. This lemma is useful for the Lebesgue decomposition theorem. -/ lemma exists_positive_of_not_mutually_singular (μ ν : measure α) [is_finite_measure μ] [is_finite_measure ν] (h : ¬ μ ⊥ₘ ν) : ∃ ε : ℝ≥0, 0 < ε ∧ ∃ E : set α, measurable_set E ∧ 0 < ν E ∧ 0 ≤[E] μ.to_signed_measure - (ε • ν).to_signed_measure := begin -- for all `n : ℕ`, obtain the Hahn decomposition for `μ - (1 / n) ν` have : ∀ n : ℕ, ∃ i : set α, measurable_set i ∧ 0 ≤[i] (μ.to_signed_measure - ((1 / (n + 1) : ℝ≥0) • ν).to_signed_measure) ∧ (μ.to_signed_measure - ((1 / (n + 1) : ℝ≥0) • ν).to_signed_measure) ≤[iᶜ] 0, { intro, exact exists_compl_positive_negative _ }, choose f hf₁ hf₂ hf₃ using this, -- set `A` to be the intersection of all the negative parts of obtained Hahn decompositions -- and we show that `μ A = 0` set A := ⋂ n, (f n)ᶜ with hA₁, have hAmeas : measurable_set A, { exact measurable_set.Inter (λ n, (hf₁ n).compl) }, have hA₂ : ∀ n : ℕ, (μ.to_signed_measure - ((1 / (n + 1) : ℝ≥0) • ν).to_signed_measure) ≤[A] 0, { intro n, exact restrict_le_restrict_subset _ _ (hf₁ n).compl (hf₃ n) (Inter_subset _ _) }, have hA₃ : ∀ n : ℕ, μ A ≤ (1 / (n + 1) : ℝ≥0) * ν A, { intro n, have := nonpos_of_restrict_le_zero _ (hA₂ n), rwa [to_signed_measure_sub_apply hAmeas, sub_nonpos, ennreal.to_real_le_to_real] at this, exacts [ne_of_lt (measure_lt_top _ _), ne_of_lt (measure_lt_top _ _)] }, have hμ : μ A = 0, { lift μ A to ℝ≥0 using ne_of_lt (measure_lt_top _ _) with μA, lift ν A to ℝ≥0 using ne_of_lt (measure_lt_top _ _) with νA, rw ennreal.coe_eq_zero, by_cases hb : 0 < νA, { suffices : ∀ b, 0 < b → μA ≤ b, { by_contra, have h' := this (μA / 2) (nnreal.half_pos (zero_lt_iff.2 h)), rw ← @not_not (μA ≤ μA / 2) at h', exact h' (not_le.2 (nnreal.half_lt_self h)) }, intros c hc, have : ∃ n : ℕ, 1 / (n + 1 : ℝ) < c * νA⁻¹, refine exists_nat_one_div_lt _, { refine mul_pos hc _, rw _root_.inv_pos, exact hb }, rcases this with ⟨n, hn⟩, have hb₁ : (0 : ℝ) < νA⁻¹, { rw _root_.inv_pos, exact hb }, have h' : 1 / (↑n + 1) * νA < c, { rw [← nnreal.coe_lt_coe, ← mul_lt_mul_right hb₁, nnreal.coe_mul, mul_assoc, ← nnreal.coe_inv, ← nnreal.coe_mul, _root_.mul_inv_cancel, ← nnreal.coe_mul, mul_one, nnreal.coe_inv], { exact hn }, { exact ne.symm (ne_of_lt hb) } }, refine le_trans _ (le_of_lt h'), rw [← ennreal.coe_le_coe, ennreal.coe_mul], exact hA₃ n }, { rw [not_lt, le_zero_iff] at hb, specialize hA₃ 0, simp [hb, le_zero_iff] at hA₃, assumption } }, -- since `μ` and `ν` are not mutually singular, `μ A = 0` implies `ν Aᶜ > 0` rw mutually_singular at h, push_neg at h, have := h _ hAmeas hμ, simp_rw [hA₁, compl_Inter, compl_compl] at this, -- as `Aᶜ = ⋃ n, f n`, `ν Aᶜ > 0` implies there exists some `n` such that `ν (f n) > 0` obtain ⟨n, hn⟩ := exists_measure_pos_of_not_measure_Union_null this, -- thus, choosing `f n` as the set `E` suffices exact ⟨1 / (n + 1), by simp, f n, hf₁ n, hn, hf₂ n⟩, end namespace lebesgue_decomposition /-- Given two measures `μ` and `ν`, `measurable_le μ ν` is the set of measurable functions `f`, such that, for all measurable sets `A`, `∫⁻ x in A, f x ∂μ ≤ ν A`. This is useful for the Lebesgue decomposition theorem. -/ def measurable_le (μ ν : measure α) : set (α → ℝ≥0∞) := { f | measurable f ∧ ∀ (A : set α) (hA : measurable_set A), ∫⁻ x in A, f x ∂μ ≤ ν A } lemma zero_mem_measurable_le : (0 : α → ℝ≥0∞) ∈ measurable_le μ ν := ⟨measurable_zero, λ A hA, by simp⟩ lemma sup_mem_measurable_le {f g : α → ℝ≥0∞} (hf : f ∈ measurable_le μ ν) (hg : g ∈ measurable_le μ ν) : (λ a, f a ⊔ g a) ∈ measurable_le μ ν := begin simp_rw ennreal.sup_eq_max, refine ⟨measurable.max hf.1 hg.1, λ A hA, _⟩, have h₁ := hA.inter (measurable_set_le hf.1 hg.1), have h₂ := hA.inter (measurable_set_lt hg.1 hf.1), rw [set_lintegral_max hf.1 hg.1], refine (add_le_add (hg.2 _ h₁) (hf.2 _ h₂)).trans_eq _, { simp only [← not_le, ← compl_set_of, ← diff_eq], exact measure_inter_add_diff _ (measurable_set_le hf.1 hg.1) } end lemma supr_succ_eq_sup {α} (f : ℕ → α → ℝ≥0∞) (m : ℕ) (a : α) : (⨆ (k : ℕ) (hk : k ≤ m + 1), f k a) = f m.succ a ⊔ ⨆ (k : ℕ) (hk : k ≤ m), f k a := begin ext x, simp only [option.mem_def, ennreal.some_eq_coe], split; intro h; rw ← h, symmetry, all_goals { set c := (⨆ (k : ℕ) (hk : k ≤ m + 1), f k a) with hc, set d := (f m.succ a ⊔ ⨆ (k : ℕ) (hk : k ≤ m), f k a) with hd, rw [@le_antisymm_iff ℝ≥0∞, hc, hd], -- Specifying the type is weirdly necessary refine ⟨_, _⟩, { refine supr₂_le (λ n hn, _), rcases nat.of_le_succ hn with (h | h), { exact le_sup_of_le_right (le_supr₂ n h) }, { exact h ▸ le_sup_left } }, { refine sup_le _ (bsupr_mono $ λ n hn, hn.trans m.le_succ), convert @le_supr₂ _ _ (λ i, i ≤ m + 1) _ _ m.succ le_rfl, refl } } end lemma supr_mem_measurable_le (f : ℕ → α → ℝ≥0∞) (hf : ∀ n, f n ∈ measurable_le μ ν) (n : ℕ) : (λ x, ⨆ k (hk : k ≤ n), f k x) ∈ measurable_le μ ν := begin induction n with m hm, { refine ⟨_, _⟩, { simp [(hf 0).1] }, { intros A hA, simp [(hf 0).2 A hA] } }, { have : (λ (a : α), ⨆ (k : ℕ) (hk : k ≤ m + 1), f k a) = (λ a, f m.succ a ⊔ ⨆ (k : ℕ) (hk : k ≤ m), f k a), { exact funext (λ _, supr_succ_eq_sup _ _ _) }, refine ⟨measurable_supr (λ n, measurable.supr_Prop _ (hf n).1), λ A hA, _⟩, rw this, exact (sup_mem_measurable_le (hf m.succ) hm).2 A hA } end lemma supr_mem_measurable_le' (f : ℕ → α → ℝ≥0∞) (hf : ∀ n, f n ∈ measurable_le μ ν) (n : ℕ) : (⨆ k (hk : k ≤ n), f k) ∈ measurable_le μ ν := begin convert supr_mem_measurable_le f hf n, ext, simp end section supr_lemmas --TODO: these statements should be moved elsewhere omit m lemma supr_monotone {α : Type*} (f : ℕ → α → ℝ≥0∞) : monotone (λ n x, ⨆ k (hk : k ≤ n), f k x) := λ n m hnm x, bsupr_mono $ λ i, ge_trans hnm lemma supr_monotone' {α : Type*} (f : ℕ → α → ℝ≥0∞) (x : α) : monotone (λ n, ⨆ k (hk : k ≤ n), f k x) := λ n m hnm, supr_monotone f hnm x lemma supr_le_le {α : Type*} (f : ℕ → α → ℝ≥0∞) (n k : ℕ) (hk : k ≤ n) : f k ≤ λ x, ⨆ k (hk : k ≤ n), f k x := λ x, le_supr₂ k hk end supr_lemmas /-- `measurable_le_eval μ ν` is the set of `∫⁻ x, f x ∂μ` for all `f ∈ measurable_le μ ν`. -/ def measurable_le_eval (μ ν : measure α) : set ℝ≥0∞ := (λ f : α → ℝ≥0∞, ∫⁻ x, f x ∂μ) '' measurable_le μ ν end lebesgue_decomposition open lebesgue_decomposition /-- Any pair of finite measures `μ` and `ν`, `have_lebesgue_decomposition`. That is to say, there exist a measure `ξ` and a measurable function `f`, such that `ξ` is mutually singular with respect to `ν` and `μ = ξ + ν.with_density f`. This is not an instance since this is also shown for the more general σ-finite measures with `measure_theory.measure.have_lebesgue_decomposition_of_sigma_finite`. -/ theorem have_lebesgue_decomposition_of_finite_measure [is_finite_measure μ] [is_finite_measure ν] : have_lebesgue_decomposition μ ν := ⟨begin have h := @exists_seq_tendsto_Sup _ _ _ _ _ (measurable_le_eval ν μ) ⟨0, 0, zero_mem_measurable_le, by simp⟩ (order_top.bdd_above _), choose g hmono hg₂ f hf₁ hf₂ using h, -- we set `ξ` to be the supremum of an increasing sequence of functions obtained from above set ξ := ⨆ n k (hk : k ≤ n), f k with hξ, -- we see that `ξ` has the largest integral among all functions in `measurable_le` have hξ₁ : Sup (measurable_le_eval ν μ) = ∫⁻ a, ξ a ∂ν, { have := @lintegral_tendsto_of_tendsto_of_monotone _ _ ν (λ n, ⨆ k (hk : k ≤ n), f k) (⨆ n k (hk : k ≤ n), f k) _ _ _, { refine tendsto_nhds_unique _ this, refine tendsto_of_tendsto_of_tendsto_of_le_of_le hg₂ tendsto_const_nhds _ _, { intro n, rw ← hf₂ n, apply lintegral_mono, simp only [supr_apply, supr_le_le f n n le_rfl] }, { intro n, exact le_Sup ⟨⨆ (k : ℕ) (hk : k ≤ n), f k, supr_mem_measurable_le' _ hf₁ _, rfl⟩ } }, { intro n, refine measurable.ae_measurable _, convert (supr_mem_measurable_le _ hf₁ n).1, ext, simp }, { refine filter.eventually_of_forall (λ a, _), simp [supr_monotone' f _] }, { refine filter.eventually_of_forall (λ a, _), simp [tendsto_at_top_supr (supr_monotone' f a)] } }, have hξm : measurable ξ, { convert measurable_supr (λ n, (supr_mem_measurable_le _ hf₁ n).1), ext, simp [hξ] }, -- `ξ` is the `f` in the theorem statement and we set `μ₁` to be `μ - ν.with_density ξ` -- since we need `μ₁ + ν.with_density ξ = μ` set μ₁ := μ - ν.with_density ξ with hμ₁, have hle : ν.with_density ξ ≤ μ, { intros B hB, rw [hξ, with_density_apply _ hB], simp_rw [supr_apply], rw lintegral_supr (λ i, (supr_mem_measurable_le _ hf₁ i).1) (supr_monotone _), exact supr_le (λ i, (supr_mem_measurable_le _ hf₁ i).2 B hB) }, haveI : is_finite_measure (ν.with_density ξ), { refine is_finite_measure_with_density _, have hle' := hle univ measurable_set.univ, rw [with_density_apply _ measurable_set.univ, measure.restrict_univ] at hle', exact ne_top_of_le_ne_top (measure_ne_top _ _) hle' }, refine ⟨⟨μ₁, ξ⟩, hξm, _, _⟩, { by_contra, -- if they are not mutually singular, then from `exists_positive_of_not_mutually_singular`, -- there exists some `ε > 0` and a measurable set `E`, such that `μ(E) > 0` and `E` is -- positive with respect to `ν - εμ` obtain ⟨ε, hε₁, E, hE₁, hE₂, hE₃⟩ := exists_positive_of_not_mutually_singular μ₁ ν h, simp_rw hμ₁ at hE₃, have hξle : ∀ A, measurable_set A → ∫⁻ a in A, ξ a ∂ν ≤ μ A, { intros A hA, rw hξ, simp_rw [supr_apply], rw lintegral_supr (λ n, (supr_mem_measurable_le _ hf₁ n).1) (supr_monotone _), exact supr_le (λ n, (supr_mem_measurable_le _ hf₁ n).2 A hA) }, -- since `E` is positive, we have `∫⁻ a in A ∩ E, ε + ξ a ∂ν ≤ μ (A ∩ E)` for all `A` have hε₂ : ∀ A : set α, measurable_set A → ∫⁻ a in A ∩ E, ε + ξ a ∂ν ≤ μ (A ∩ E), { intros A hA, have := subset_le_of_restrict_le_restrict _ _ hE₁ hE₃ (inter_subset_right A E), rwa [zero_apply, to_signed_measure_sub_apply (hA.inter hE₁), measure.sub_apply (hA.inter hE₁) hle, ennreal.to_real_sub_of_le _ (ne_of_lt (measure_lt_top _ _)), sub_nonneg, le_sub_iff_add_le, ← ennreal.to_real_add, ennreal.to_real_le_to_real, measure.coe_smul, pi.smul_apply, with_density_apply _ (hA.inter hE₁), show ε • ν (A ∩ E) = (ε : ℝ≥0∞) * ν (A ∩ E), by refl, ← set_lintegral_const, ← lintegral_add_left measurable_const] at this, { rw [ne.def, ennreal.add_eq_top, not_or_distrib], exact ⟨ne_of_lt (measure_lt_top _ _), ne_of_lt (measure_lt_top _ _)⟩ }, { exact ne_of_lt (measure_lt_top _ _) }, { exact ne_of_lt (measure_lt_top _ _) }, { exact ne_of_lt (measure_lt_top _ _) }, { rw with_density_apply _ (hA.inter hE₁), exact hξle (A ∩ E) (hA.inter hE₁) }, { apply_instance } }, -- from this, we can show `ξ + ε * E.indicator` is a function in `measurable_le` with -- integral greater than `ξ` have hξε : ξ + E.indicator (λ _, ε) ∈ measurable_le ν μ, { refine ⟨measurable.add hξm (measurable.indicator measurable_const hE₁), λ A hA, _⟩, have : ∫⁻ a in A, (ξ + E.indicator (λ _, ε)) a ∂ν = ∫⁻ a in A ∩ E, ε + ξ a ∂ν + ∫⁻ a in A \ E, ξ a ∂ν, { simp only [lintegral_add_left measurable_const, lintegral_add_left hξm, set_lintegral_const, add_assoc, lintegral_inter_add_diff _ _ hE₁, pi.add_apply, lintegral_indicator _ hE₁, restrict_apply hE₁], rw [inter_comm, add_comm] }, rw [this, ← measure_inter_add_diff A hE₁], exact add_le_add (hε₂ A hA) (hξle (A \ E) (hA.diff hE₁)) }, have : ∫⁻ a, ξ a + E.indicator (λ _, ε) a ∂ν ≤ Sup (measurable_le_eval ν μ) := le_Sup ⟨ξ + E.indicator (λ _, ε), hξε, rfl⟩, -- but this contradicts the maximality of `∫⁻ x, ξ x ∂ν` refine not_lt.2 this _, rw [hξ₁, lintegral_add_left hξm, lintegral_indicator _ hE₁, set_lintegral_const], refine ennreal.lt_add_right _ (ennreal.mul_pos_iff.2 ⟨ennreal.coe_pos.2 hε₁, hE₂⟩).ne', have := measure_ne_top (ν.with_density ξ) univ, rwa [with_density_apply _ measurable_set.univ, measure.restrict_univ] at this }, -- since `ν.with_density ξ ≤ μ`, it is clear that `μ = μ₁ + ν.with_density ξ` { rw hμ₁, ext1 A hA, rw [measure.coe_add, pi.add_apply, measure.sub_apply hA hle, add_comm, add_tsub_cancel_of_le (hle A hA)] }, end⟩ local attribute [instance] have_lebesgue_decomposition_of_finite_measure instance {S : μ.finite_spanning_sets_in {s : set α | measurable_set s}} (n : ℕ) : is_finite_measure (μ.restrict $ S.set n) := ⟨by { rw [restrict_apply measurable_set.univ, univ_inter], exact S.finite _ }⟩ /-- **The Lebesgue decomposition theorem**: Any pair of σ-finite measures `μ` and `ν` `have_lebesgue_decomposition`. That is to say, there exist a measure `ξ` and a measurable function `f`, such that `ξ` is mutually singular with respect to `ν` and `μ = ξ + ν.with_density f` -/ @[priority 100] -- see Note [lower instance priority] instance have_lebesgue_decomposition_of_sigma_finite (μ ν : measure α) [sigma_finite μ] [sigma_finite ν] : have_lebesgue_decomposition μ ν := ⟨begin -- Since `μ` and `ν` are both σ-finite, there exists a sequence of pairwise disjoint spanning -- sets which are finite with respect to both `μ` and `ν` obtain ⟨S, T, h₁, h₂⟩ := exists_eq_disjoint_finite_spanning_sets_in μ ν, have h₃ : pairwise (disjoint on T.set) := h₁ ▸ h₂, -- We define `μn` and `νn` as sequences of measures such that `μn n = μ ∩ S n` and -- `νn n = ν ∩ S n` where `S` is the aforementioned finite spanning set sequence. -- Since `S` is spanning, it is clear that `sum μn = μ` and `sum νn = ν` set μn : ℕ → measure α := λ n, μ.restrict (S.set n) with hμn, have hμ : μ = sum μn, { rw [hμn, ← restrict_Union h₂ S.set_mem, S.spanning, restrict_univ] }, set νn : ℕ → measure α := λ n, ν.restrict (T.set n) with hνn, have hν : ν = sum νn, { rw [hνn, ← restrict_Union h₃ T.set_mem, T.spanning, restrict_univ] }, -- As `S` is finite with respect to both `μ` and `ν`, it is clear that `μn n` and `νn n` are -- finite measures for all `n : ℕ`. Thus, we may apply the finite Lebesgue decomposition theorem -- to `μn n` and `νn n`. We define `ξ` as the sum of the singular part of the Lebesgue -- decompositions of `μn n` and `νn n`, and `f` as the sum of the Radon-Nikodym derviatives of -- `μn n` and `νn n` restricted on `S n` set ξ := sum (λ n, singular_part (μn n) (νn n)) with hξ, set f := ∑' n, (S.set n).indicator (rn_deriv (μn n) (νn n)) with hf, -- I claim `ξ` and `f` form a Lebesgue decomposition of `μ` and `ν` refine ⟨⟨ξ, f⟩, _, _, _⟩, { exact measurable.ennreal_tsum' (λ n, measurable.indicator (measurable_rn_deriv (μn n) (νn n)) (S.set_mem n)) }, -- We show that `ξ` is mutually singular with respect to `ν` { choose A hA₁ hA₂ hA₃ using λ n, mutually_singular_singular_part (μn n) (νn n), simp only [hξ], -- We use the set `B := ⋃ j, (S.set j) ∩ A j` where `A n` is the set provided as -- `singular_part (μn n) (νn n) ⊥ₘ νn n` refine ⟨⋃ j, (S.set j) ∩ A j, measurable_set.Union (λ n, (S.set_mem n).inter (hA₁ n)), _, _⟩, -- `ξ B = 0` since `ξ B = ∑ i j, singular_part (μn j) (νn j) (S.set i ∩ A i)` -- `= ∑ i, singular_part (μn i) (νn i) (S.set i ∩ A i)` -- `≤ ∑ i, singular_part (μn i) (νn i) (A i) = 0` -- where the second equality follows as `singular_part (μn j) (νn j) (S.set i ∩ A i)` vanishes -- for all `i ≠ j` { rw [measure_Union], { have : ∀ i, (sum (λ n, (μn n).singular_part (νn n))) (S.set i ∩ A i) = (μn i).singular_part (νn i) (S.set i ∩ A i), { intro i, rw [sum_apply _ ((S.set_mem i).inter (hA₁ i)), tsum_eq_single i], { intros j hij, rw [hμn, ← nonpos_iff_eq_zero], refine le_trans ((singular_part_le _ _) _ ((S.set_mem i).inter (hA₁ i))) (le_of_eq _), rw [restrict_apply ((S.set_mem i).inter (hA₁ i)), inter_comm, ← inter_assoc], have : disjoint (S.set j) (S.set i) := h₂ j i hij, rw disjoint_iff_inter_eq_empty at this, rw [this, empty_inter, measure_empty] }, { apply_instance } }, simp_rw [this, tsum_eq_zero_iff ennreal.summable], intro n, exact measure_mono_null (inter_subset_right _ _) (hA₂ n) }, { exact h₂.mono (λ i j, disjoint.mono inf_le_left inf_le_left) }, { exact λ n, (S.set_mem n).inter (hA₁ n) } }, -- We will now show `ν Bᶜ = 0`. This follows since `Bᶜ = ⋃ n, S.set n ∩ (A n)ᶜ` and thus, -- `ν Bᶜ = ∑ i, ν (S.set i ∩ (A i)ᶜ) = ∑ i, (νn i) (A i)ᶜ = 0` { have hcompl : is_compl (⋃ n, (S.set n ∩ A n)) (⋃ n, S.set n ∩ (A n)ᶜ), { split, { rintro x ⟨hx₁, hx₂⟩, rw mem_Union at hx₁ hx₂, obtain ⟨⟨i, hi₁, hi₂⟩, ⟨j, hj₁, hj₂⟩⟩ := ⟨hx₁, hx₂⟩, have : i = j, { by_contra hij, exact h₂ i j hij ⟨hi₁, hj₁⟩ }, exact hj₂ (this ▸ hi₂) }, { intros x hx, simp only [mem_Union, sup_eq_union, mem_inter_eq, mem_union_eq, mem_compl_eq, or_iff_not_imp_left], intro h, push_neg at h, rw [top_eq_univ, ← S.spanning, mem_Union] at hx, obtain ⟨i, hi⟩ := hx, exact ⟨i, hi, h i hi⟩ } }, rw [hcompl.compl_eq, measure_Union, tsum_eq_zero_iff ennreal.summable], { intro n, rw [inter_comm, ← restrict_apply (hA₁ n).compl, ← hA₃ n, hνn, h₁] }, { exact h₂.mono (λ i j, disjoint.mono inf_le_left inf_le_left) }, { exact λ n, (S.set_mem n).inter (hA₁ n).compl } } }, -- Finally, it remains to show `μ = ξ + ν.with_density f`. Since `μ = sum μn`, and -- `ξ + ν.with_density f = ∑ n, singular_part (μn n) (νn n)` -- `+ ν.with_density (rn_deriv (μn n) (νn n)) ∩ (S.set n)`, -- it suffices to show that the individual summands are equal. This follows by the -- Lebesgue decomposition properties on the individual `μn n` and `νn n` { simp only [hξ, hf, hμ], rw [with_density_tsum _, sum_add_sum], { refine sum_congr (λ n, _), conv_lhs { rw have_lebesgue_decomposition_add (μn n) (νn n) }, suffices heq : (νn n).with_density ((μn n).rn_deriv (νn n)) = ν.with_density ((S.set n).indicator ((μn n).rn_deriv (νn n))), { rw heq }, rw [hν, with_density_indicator (S.set_mem n), restrict_sum _ (S.set_mem n)], suffices hsumeq : sum (λ (i : ℕ), (νn i).restrict (S.set n)) = νn n, { rw hsumeq }, ext1 s hs, rw [sum_apply _ hs, tsum_eq_single n, hνn, h₁, restrict_restrict (T.set_mem n), inter_self], { intros m hm, rw [hνn, h₁, restrict_restrict (T.set_mem n), disjoint_iff_inter_eq_empty.1 (h₃ n m hm.symm), restrict_empty, coe_zero, pi.zero_apply] }, { apply_instance } }, { exact λ n, measurable.indicator (measurable_rn_deriv _ _) (S.set_mem n) } }, end⟩ end measure namespace signed_measure open measure /-- A signed measure `s` is said to `have_lebesgue_decomposition` with respect to a measure `μ` if the positive part and the negative part of `s` both `have_lebesgue_decomposition` with respect to `μ`. -/ class have_lebesgue_decomposition (s : signed_measure α) (μ : measure α) : Prop := (pos_part : s.to_jordan_decomposition.pos_part.have_lebesgue_decomposition μ) (neg_part : s.to_jordan_decomposition.neg_part.have_lebesgue_decomposition μ) attribute [instance] have_lebesgue_decomposition.pos_part attribute [instance] have_lebesgue_decomposition.neg_part lemma not_have_lebesgue_decomposition_iff (s : signed_measure α) (μ : measure α) : ¬ s.have_lebesgue_decomposition μ ↔ ¬ s.to_jordan_decomposition.pos_part.have_lebesgue_decomposition μ ∨ ¬ s.to_jordan_decomposition.neg_part.have_lebesgue_decomposition μ := ⟨λ h, not_or_of_imp (λ hp hn, h ⟨hp, hn⟩), λ h hl, (not_and_distrib.2 h) ⟨hl.1, hl.2⟩⟩ -- `infer_instance` directly does not work @[priority 100] -- see Note [lower instance priority] instance have_lebesgue_decomposition_of_sigma_finite (s : signed_measure α) (μ : measure α) [sigma_finite μ] : s.have_lebesgue_decomposition μ := { pos_part := infer_instance, neg_part := infer_instance } instance have_lebesgue_decomposition_neg (s : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] : (-s).have_lebesgue_decomposition μ := { pos_part := by { rw [to_jordan_decomposition_neg, jordan_decomposition.neg_pos_part], apply_instance }, neg_part := by { rw [to_jordan_decomposition_neg, jordan_decomposition.neg_neg_part], apply_instance } } instance have_lebesgue_decomposition_smul (s : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] (r : ℝ≥0) : (r • s).have_lebesgue_decomposition μ := { pos_part := by { rw [to_jordan_decomposition_smul, jordan_decomposition.smul_pos_part], apply_instance }, neg_part := by { rw [to_jordan_decomposition_smul, jordan_decomposition.smul_neg_part], apply_instance } } instance have_lebesgue_decomposition_smul_real (s : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] (r : ℝ) : (r • s).have_lebesgue_decomposition μ := begin by_cases hr : 0 ≤ r, { lift r to ℝ≥0 using hr, exact s.have_lebesgue_decomposition_smul μ _ }, { rw not_le at hr, refine { pos_part := by { rw [to_jordan_decomposition_smul_real, jordan_decomposition.real_smul_pos_part_neg _ _ hr], apply_instance }, neg_part := by { rw [to_jordan_decomposition_smul_real, jordan_decomposition.real_smul_neg_part_neg _ _ hr], apply_instance } } } end /-- Given a signed measure `s` and a measure `μ`, `s.singular_part μ` is the signed measure such that `s.singular_part μ + μ.with_densityᵥ (s.rn_deriv μ) = s` and `s.singular_part μ` is mutually singular with respect to `μ`. -/ def singular_part (s : signed_measure α) (μ : measure α) : signed_measure α := (s.to_jordan_decomposition.pos_part.singular_part μ).to_signed_measure - (s.to_jordan_decomposition.neg_part.singular_part μ).to_signed_measure section lemma singular_part_mutually_singular (s : signed_measure α) (μ : measure α) : s.to_jordan_decomposition.pos_part.singular_part μ ⊥ₘ s.to_jordan_decomposition.neg_part.singular_part μ := begin by_cases hl : s.have_lebesgue_decomposition μ, { haveI := hl, obtain ⟨i, hi, hpos, hneg⟩ := s.to_jordan_decomposition.mutually_singular, rw s.to_jordan_decomposition.pos_part.have_lebesgue_decomposition_add μ at hpos, rw s.to_jordan_decomposition.neg_part.have_lebesgue_decomposition_add μ at hneg, rw [add_apply, add_eq_zero_iff] at hpos hneg, exact ⟨i, hi, hpos.1, hneg.1⟩ }, { rw not_have_lebesgue_decomposition_iff at hl, cases hl with hp hn, { rw [measure.singular_part, dif_neg hp], exact mutually_singular.zero_left }, { rw [measure.singular_part, measure.singular_part, dif_neg hn], exact mutually_singular.zero_right } } end lemma singular_part_total_variation (s : signed_measure α) (μ : measure α) : (s.singular_part μ).total_variation = s.to_jordan_decomposition.pos_part.singular_part μ + s.to_jordan_decomposition.neg_part.singular_part μ := begin have : (s.singular_part μ).to_jordan_decomposition = ⟨s.to_jordan_decomposition.pos_part.singular_part μ, s.to_jordan_decomposition.neg_part.singular_part μ, singular_part_mutually_singular s μ⟩, { refine jordan_decomposition.to_signed_measure_injective _, rw to_signed_measure_to_jordan_decomposition, refl }, { rw [total_variation, this] }, end lemma mutually_singular_singular_part (s : signed_measure α) (μ : measure α) : singular_part s μ ⊥ᵥ μ.to_ennreal_vector_measure := begin rw [mutually_singular_ennreal_iff, singular_part_total_variation], change _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ), rw vector_measure.equiv_measure.right_inv μ, exact (mutually_singular_singular_part _ _).add_left (mutually_singular_singular_part _ _) end end /-- The Radon-Nikodym derivative between a signed measure and a positive measure. `rn_deriv s μ` satisfies `μ.with_densityᵥ (s.rn_deriv μ) = s` if and only if `s` is absolutely continuous with respect to `μ` and this fact is known as `measure_theory.signed_measure.absolutely_continuous_iff_with_density_rn_deriv_eq` and can be found in `measure_theory.decomposition.radon_nikodym`. -/ def rn_deriv (s : signed_measure α) (μ : measure α) : α → ℝ := λ x, (s.to_jordan_decomposition.pos_part.rn_deriv μ x).to_real - (s.to_jordan_decomposition.neg_part.rn_deriv μ x).to_real variables {s t : signed_measure α} @[measurability] lemma measurable_rn_deriv (s : signed_measure α) (μ : measure α) : measurable (rn_deriv s μ) := begin rw [rn_deriv], measurability, end lemma integrable_rn_deriv (s : signed_measure α) (μ : measure α) : integrable (rn_deriv s μ) μ := begin refine integrable.sub _ _; { split, { apply measurable.ae_strongly_measurable, measurability }, exact has_finite_integral_to_real_of_lintegral_ne_top (lintegral_rn_deriv_lt_top _ μ).ne } end variables (s μ) /-- **The Lebesgue Decomposition theorem between a signed measure and a measure**: Given a signed measure `s` and a σ-finite measure `μ`, there exist a signed measure `t` and a measurable and integrable function `f`, such that `t` is mutually singular with respect to `μ` and `s = t + μ.with_densityᵥ f`. In this case `t = s.singular_part μ` and `f = s.rn_deriv μ`. -/ theorem singular_part_add_with_density_rn_deriv_eq [s.have_lebesgue_decomposition μ] : s.singular_part μ + μ.with_densityᵥ (s.rn_deriv μ) = s := begin conv_rhs { rw [← to_signed_measure_to_jordan_decomposition s, jordan_decomposition.to_signed_measure] }, rw [singular_part, rn_deriv, with_densityᵥ_sub' (integrable_to_real_of_lintegral_ne_top _ _) (integrable_to_real_of_lintegral_ne_top _ _), with_densityᵥ_to_real, with_densityᵥ_to_real, sub_eq_add_neg, sub_eq_add_neg, add_comm (s.to_jordan_decomposition.pos_part.singular_part μ).to_signed_measure, ← add_assoc, add_assoc (-(s.to_jordan_decomposition.neg_part.singular_part μ).to_signed_measure), ← to_signed_measure_add, add_comm, ← add_assoc, ← neg_add, ← to_signed_measure_add, add_comm, ← sub_eq_add_neg], convert rfl, -- `convert rfl` much faster than `congr` { exact (s.to_jordan_decomposition.pos_part.have_lebesgue_decomposition_add μ) }, { rw add_comm, exact (s.to_jordan_decomposition.neg_part.have_lebesgue_decomposition_add μ) }, all_goals { exact (lintegral_rn_deriv_lt_top _ _).ne <|> measurability } end variables {s μ} lemma jordan_decomposition_add_with_density_mutually_singular {f : α → ℝ} (hf : measurable f) (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) : t.to_jordan_decomposition.pos_part + μ.with_density (λ (x : α), ennreal.of_real (f x)) ⊥ₘ t.to_jordan_decomposition.neg_part + μ.with_density (λ (x : α), ennreal.of_real (-f x)) := begin rw [mutually_singular_ennreal_iff, total_variation_mutually_singular_iff] at htμ, change _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ) ∧ _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ) at htμ, rw [vector_measure.equiv_measure.right_inv] at htμ, exact ((jordan_decomposition.mutually_singular _).add_right (htμ.1.mono_ac (refl _) (with_density_absolutely_continuous _ _))).add_left ((htμ.2.symm.mono_ac (with_density_absolutely_continuous _ _) (refl _)).add_right (with_density_of_real_mutually_singular hf)) end lemma to_jordan_decomposition_eq_of_eq_add_with_density {f : α → ℝ} (hf : measurable f) (hfi : integrable f μ) (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) : s.to_jordan_decomposition = @jordan_decomposition.mk α _ (t.to_jordan_decomposition.pos_part + μ.with_density (λ x, ennreal.of_real (f x))) (t.to_jordan_decomposition.neg_part + μ.with_density (λ x, ennreal.of_real (- f x))) (by { haveI := is_finite_measure_with_density_of_real hfi.2, apply_instance }) (by { haveI := is_finite_measure_with_density_of_real hfi.neg.2, apply_instance }) (jordan_decomposition_add_with_density_mutually_singular hf htμ) := begin haveI := is_finite_measure_with_density_of_real hfi.2, haveI := is_finite_measure_with_density_of_real hfi.neg.2, refine to_jordan_decomposition_eq _, simp_rw [jordan_decomposition.to_signed_measure, hadd], ext i hi, rw [vector_measure.sub_apply, to_signed_measure_apply_measurable hi, to_signed_measure_apply_measurable hi, add_apply, add_apply, ennreal.to_real_add, ennreal.to_real_add, add_sub_add_comm, ← to_signed_measure_apply_measurable hi, ← to_signed_measure_apply_measurable hi, ← vector_measure.sub_apply, ← jordan_decomposition.to_signed_measure, to_signed_measure_to_jordan_decomposition, vector_measure.add_apply, ← to_signed_measure_apply_measurable hi, ← to_signed_measure_apply_measurable hi, with_densityᵥ_eq_with_density_pos_part_sub_with_density_neg_part hfi, vector_measure.sub_apply]; exact (measure_lt_top _ _).ne end private lemma have_lebesgue_decomposition_mk' (μ : measure α) {f : α → ℝ} (hf : measurable f) (hfi : integrable f μ) (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) : s.have_lebesgue_decomposition μ := begin have htμ' := htμ, rw mutually_singular_ennreal_iff at htμ, change _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ) at htμ, rw [vector_measure.equiv_measure.right_inv, total_variation_mutually_singular_iff] at htμ, refine { pos_part := by { use ⟨t.to_jordan_decomposition.pos_part, λ x, ennreal.of_real (f x)⟩, refine ⟨hf.ennreal_of_real, htμ.1, _⟩, rw to_jordan_decomposition_eq_of_eq_add_with_density hf hfi htμ' hadd }, neg_part := by { use ⟨t.to_jordan_decomposition.neg_part, λ x, ennreal.of_real (-f x)⟩, refine ⟨hf.neg.ennreal_of_real, htμ.2, _⟩, rw to_jordan_decomposition_eq_of_eq_add_with_density hf hfi htμ' hadd } } end lemma have_lebesgue_decomposition_mk (μ : measure α) {f : α → ℝ} (hf : measurable f) (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) : s.have_lebesgue_decomposition μ := begin by_cases hfi : integrable f μ, { exact have_lebesgue_decomposition_mk' μ hf hfi htμ hadd }, { rw [with_densityᵥ, dif_neg hfi, add_zero] at hadd, refine have_lebesgue_decomposition_mk' μ measurable_zero (integrable_zero _ _ μ) htμ _, rwa [with_densityᵥ_zero, add_zero] } end private theorem eq_singular_part' (t : signed_measure α) {f : α → ℝ} (hf : measurable f) (hfi : integrable f μ) (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) : t = s.singular_part μ := begin have htμ' := htμ, rw [mutually_singular_ennreal_iff, total_variation_mutually_singular_iff] at htμ, change _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ) ∧ _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ) at htμ, rw [vector_measure.equiv_measure.right_inv] at htμ, { rw [singular_part, ← t.to_signed_measure_to_jordan_decomposition, jordan_decomposition.to_signed_measure], congr, { have hfpos : measurable (λ x, ennreal.of_real (f x)), { measurability }, refine eq_singular_part hfpos htμ.1 _, rw to_jordan_decomposition_eq_of_eq_add_with_density hf hfi htμ' hadd }, { have hfneg : measurable (λ x, ennreal.of_real (-f x)), { measurability }, refine eq_singular_part hfneg htμ.2 _, rw to_jordan_decomposition_eq_of_eq_add_with_density hf hfi htμ' hadd } }, end /-- Given a measure `μ`, signed measures `s` and `t`, and a function `f` such that `t` is mutually singular with respect to `μ` and `s = t + μ.with_densityᵥ f`, we have `t = singular_part s μ`, i.e. `t` is the singular part of the Lebesgue decomposition between `s` and `μ`. -/ theorem eq_singular_part (t : signed_measure α) (f : α → ℝ) (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) : t = s.singular_part μ := begin by_cases hfi : integrable f μ, { refine eq_singular_part' t hfi.1.measurable_mk (hfi.congr hfi.1.ae_eq_mk) htμ _, convert hadd using 2, exact with_densityᵥ_eq.congr_ae hfi.1.ae_eq_mk.symm }, { rw [with_densityᵥ, dif_neg hfi, add_zero] at hadd, refine eq_singular_part' t measurable_zero (integrable_zero _ _ μ) htμ _, rwa [with_densityᵥ_zero, add_zero] } end lemma singular_part_zero (μ : measure α) : (0 : signed_measure α).singular_part μ = 0 := begin refine (eq_singular_part 0 0 vector_measure.mutually_singular.zero_left _).symm, rw [zero_add, with_densityᵥ_zero], end lemma singular_part_neg (s : signed_measure α) (μ : measure α) : (-s).singular_part μ = - s.singular_part μ := begin have h₁ : ((-s).to_jordan_decomposition.pos_part.singular_part μ).to_signed_measure = (s.to_jordan_decomposition.neg_part.singular_part μ).to_signed_measure, { refine to_signed_measure_congr _, rw [to_jordan_decomposition_neg, jordan_decomposition.neg_pos_part] }, have h₂ : ((-s).to_jordan_decomposition.neg_part.singular_part μ).to_signed_measure = (s.to_jordan_decomposition.pos_part.singular_part μ).to_signed_measure, { refine to_signed_measure_congr _, rw [to_jordan_decomposition_neg, jordan_decomposition.neg_neg_part] }, rw [singular_part, singular_part, neg_sub, h₁, h₂], end lemma singular_part_smul_nnreal (s : signed_measure α) (μ : measure α) (r : ℝ≥0) : (r • s).singular_part μ = r • s.singular_part μ := begin rw [singular_part, singular_part, smul_sub, ← to_signed_measure_smul, ← to_signed_measure_smul], conv_lhs { congr, congr, rw [to_jordan_decomposition_smul, jordan_decomposition.smul_pos_part, singular_part_smul], skip, congr, rw [to_jordan_decomposition_smul, jordan_decomposition.smul_neg_part, singular_part_smul] } end lemma singular_part_smul (s : signed_measure α) (μ : measure α) (r : ℝ) : (r • s).singular_part μ = r • s.singular_part μ := begin by_cases hr : 0 ≤ r, { lift r to ℝ≥0 using hr, exact singular_part_smul_nnreal s μ r }, { rw [singular_part, singular_part], conv_lhs { congr, congr, rw [to_jordan_decomposition_smul_real, jordan_decomposition.real_smul_pos_part_neg _ _ (not_le.1 hr), singular_part_smul], skip, congr, rw [to_jordan_decomposition_smul_real, jordan_decomposition.real_smul_neg_part_neg _ _ (not_le.1 hr), singular_part_smul] }, rw [to_signed_measure_smul, to_signed_measure_smul, ← neg_sub, ← smul_sub], change -(((-r).to_nnreal : ℝ) • _) = _, rw [← neg_smul, real.coe_to_nnreal _ (le_of_lt (neg_pos.mpr (not_le.1 hr))), neg_neg] } end lemma singular_part_add (s t : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] [t.have_lebesgue_decomposition μ] : (s + t).singular_part μ = s.singular_part μ + t.singular_part μ := begin refine (eq_singular_part _ (s.rn_deriv μ + t.rn_deriv μ) ((mutually_singular_singular_part s μ).add_left (mutually_singular_singular_part t μ)) _).symm, erw [with_densityᵥ_add (integrable_rn_deriv s μ) (integrable_rn_deriv t μ)], rw [add_assoc, add_comm (t.singular_part μ), add_assoc, add_comm _ (t.singular_part μ), singular_part_add_with_density_rn_deriv_eq, ← add_assoc, singular_part_add_with_density_rn_deriv_eq], end lemma singular_part_sub (s t : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] [t.have_lebesgue_decomposition μ] : (s - t).singular_part μ = s.singular_part μ - t.singular_part μ := by { rw [sub_eq_add_neg, sub_eq_add_neg, singular_part_add, singular_part_neg] } /-- Given a measure `μ`, signed measures `s` and `t`, and a function `f` such that `t` is mutually singular with respect to `μ` and `s = t + μ.with_densityᵥ f`, we have `f = rn_deriv s μ`, i.e. `f` is the Radon-Nikodym derivative of `s` and `μ`. -/ theorem eq_rn_deriv (t : signed_measure α) (f : α → ℝ) (hfi : integrable f μ) (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) : f =ᵐ[μ] s.rn_deriv μ := begin set f' := hfi.1.mk f, have hadd' : s = t + μ.with_densityᵥ f', { convert hadd using 2, exact with_densityᵥ_eq.congr_ae hfi.1.ae_eq_mk.symm }, haveI := have_lebesgue_decomposition_mk μ hfi.1.measurable_mk htμ hadd', refine (integrable.ae_eq_of_with_densityᵥ_eq (integrable_rn_deriv _ _) hfi _).symm, rw [← add_right_inj t, ← hadd, eq_singular_part _ f htμ hadd, singular_part_add_with_density_rn_deriv_eq], end lemma rn_deriv_neg (s : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] : (-s).rn_deriv μ =ᵐ[μ] - s.rn_deriv μ := begin refine integrable.ae_eq_of_with_densityᵥ_eq (integrable_rn_deriv _ _) (integrable_rn_deriv _ _).neg _, rw [with_densityᵥ_neg, ← add_right_inj ((-s).singular_part μ), singular_part_add_with_density_rn_deriv_eq, singular_part_neg, ← neg_add, singular_part_add_with_density_rn_deriv_eq] end lemma rn_deriv_smul (s : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] (r : ℝ) : (r • s).rn_deriv μ =ᵐ[μ] r • s.rn_deriv μ := begin refine integrable.ae_eq_of_with_densityᵥ_eq (integrable_rn_deriv _ _) ((integrable_rn_deriv _ _).smul r) _, change _ = μ.with_densityᵥ ((r : ℝ) • s.rn_deriv μ), rw [with_densityᵥ_smul (rn_deriv s μ) (r : ℝ), ← add_right_inj ((r • s).singular_part μ), singular_part_add_with_density_rn_deriv_eq, singular_part_smul], change _ = _ + r • _, rw [← smul_add, singular_part_add_with_density_rn_deriv_eq], end lemma rn_deriv_add (s t : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] [t.have_lebesgue_decomposition μ] [(s + t).have_lebesgue_decomposition μ] : (s + t).rn_deriv μ =ᵐ[μ] s.rn_deriv μ + t.rn_deriv μ := begin refine integrable.ae_eq_of_with_densityᵥ_eq (integrable_rn_deriv _ _) ((integrable_rn_deriv _ _).add (integrable_rn_deriv _ _)) _, rw [← add_right_inj ((s + t).singular_part μ), singular_part_add_with_density_rn_deriv_eq, with_densityᵥ_add (integrable_rn_deriv _ _) (integrable_rn_deriv _ _), singular_part_add, add_assoc, add_comm (t.singular_part μ), add_assoc, add_comm _ (t.singular_part μ), singular_part_add_with_density_rn_deriv_eq, ← add_assoc, singular_part_add_with_density_rn_deriv_eq], end lemma rn_deriv_sub (s t : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] [t.have_lebesgue_decomposition μ] [hst : (s - t).have_lebesgue_decomposition μ] : (s - t).rn_deriv μ =ᵐ[μ] s.rn_deriv μ - t.rn_deriv μ := begin rw sub_eq_add_neg at hst, rw [sub_eq_add_neg, sub_eq_add_neg], exactI ae_eq_trans (rn_deriv_add _ _ _) (filter.eventually_eq.add (ae_eq_refl _) (rn_deriv_neg _ _)), end end signed_measure namespace complex_measure /-- A complex measure is said to `have_lebesgue_decomposition` with respect to a positive measure if both its real and imaginary part `have_lebesgue_decomposition` with respect to that measure. -/ class have_lebesgue_decomposition (c : complex_measure α) (μ : measure α) : Prop := (re_part : c.re.have_lebesgue_decomposition μ) (im_part : c.im.have_lebesgue_decomposition μ) attribute [instance] have_lebesgue_decomposition.re_part attribute [instance] have_lebesgue_decomposition.im_part /-- The singular part between a complex measure `c` and a positive measure `μ` is the complex measure satisfying `c.singular_part μ + μ.with_densityᵥ (c.rn_deriv μ) = c`. This property is given by `measure_theory.complex_measure.singular_part_add_with_density_rn_deriv_eq`. -/ def singular_part (c : complex_measure α) (μ : measure α) : complex_measure α := (c.re.singular_part μ).to_complex_measure (c.im.singular_part μ) /-- The Radon-Nikodym derivative between a complex measure and a positive measure. -/ def rn_deriv (c : complex_measure α) (μ : measure α) : α → ℂ := λ x, ⟨c.re.rn_deriv μ x, c.im.rn_deriv μ x⟩ variable {c : complex_measure α} lemma integrable_rn_deriv (c : complex_measure α) (μ : measure α) : integrable (c.rn_deriv μ) μ := begin rw [← mem_ℒp_one_iff_integrable, ← mem_ℒp_re_im_iff], exact ⟨mem_ℒp_one_iff_integrable.2 (signed_measure.integrable_rn_deriv _ _), mem_ℒp_one_iff_integrable.2 (signed_measure.integrable_rn_deriv _ _)⟩ end theorem singular_part_add_with_density_rn_deriv_eq [c.have_lebesgue_decomposition μ] : c.singular_part μ + μ.with_densityᵥ (c.rn_deriv μ) = c := begin conv_rhs { rw [← c.to_complex_measure_to_signed_measure] }, ext i hi : 1, rw [vector_measure.add_apply, signed_measure.to_complex_measure_apply], ext, { rw [complex.add_re, with_densityᵥ_apply (c.integrable_rn_deriv μ) hi, ←is_R_or_C.re_eq_complex_re, ←integral_re (c.integrable_rn_deriv μ).integrable_on, is_R_or_C.re_eq_complex_re, ← with_densityᵥ_apply _ hi], { change (c.re.singular_part μ + μ.with_densityᵥ (c.re.rn_deriv μ)) i = _, rw c.re.singular_part_add_with_density_rn_deriv_eq μ }, { exact (signed_measure.integrable_rn_deriv _ _) } }, { rw [complex.add_im, with_densityᵥ_apply (c.integrable_rn_deriv μ) hi, ←is_R_or_C.im_eq_complex_im, ←integral_im (c.integrable_rn_deriv μ).integrable_on, is_R_or_C.im_eq_complex_im, ← with_densityᵥ_apply _ hi], { change (c.im.singular_part μ + μ.with_densityᵥ (c.im.rn_deriv μ)) i = _, rw c.im.singular_part_add_with_density_rn_deriv_eq μ }, { exact (signed_measure.integrable_rn_deriv _ _) } }, end end complex_measure end measure_theory
c9c77fb18c6f4b234a11ff10ca226ee875ee9225
892c0ca8b8ddfe17a2c64fb7b721f9e2d9287539
/semantics.lean
85445eb8438b06ed870c7fc2663f04aa8eb3520e
[]
no_license
ssomayyajula/stlc
f7ef96de541f9519b00912337332366317282dce
cf92bf387b4418f9a6261c7ea4876db4e4280dd2
refs/heads/master
1,630,729,373,254
1,516,045,347,000
1,516,045,347,000
115,835,353
5
0
null
null
null
null
UTF-8
Lean
false
false
1,592
lean
import .syntax def fv : term → set var | (term.var x) := {x} | (term.app e₁ e₂) := fv e₁ ∪ fv e₂ | (term.abs x _ e) := fv e \ {x} | term.unit := ∅ -- e' = e{v/x} inductive is_subst : term → term → term → var → Prop | unit (x : var) (e : term) : is_subst term.unit term.unit e x -- e = x{e/x} | same_var (x : var) (e : term) : is_subst e (term.var x) e x -- y = y{e/x}, y ≠ x | diff_var {x y : var} (e : term) : y ≠ x → is_subst (term.var y) (term.var y) e x -- e₁{e/x}e₂{e/x} = (e₁ e₂){e/x} | app {e₁ e₂ e e₁' e₂' : term} {x : var} : is_subst e₁' e₁ e x → is_subst e₂' e₂ e x → is_subst (term.app e₁' e₂') (term.app e₁ e₂) e x -- λ y, e₁{e₂/x} = (λ y, e₁){e₂/x}, y ≠ x, y ∉ fv e₂ | abs {x y : var} {τ : type} {e₁ e₂ e₁' : term} : y ≠ x → y ∉ fv e₂ → is_subst e₁' e₁ e₂ x → is_subst (term.abs y τ e₁') (term.abs y τ e₁) e₂ x def subst (e v : term) (x : var) : {e' // is_subst e' e v x} := sorry inductive step : term → term → Prop | context {e e' : term} (E : E) : step e e' → step (E e) (E e') | beta {x : var} {τ : type} {e v e' : term} : is_value v → is_subst e' e v x → step (term.app (term.abs x τ e) v) e' notation e `⇝`:35 e' := step e e' -- reflexive transitive closure inductive rtc {α} (r : α → α → Prop) : α → α → Prop | refl : ∀ {a}, rtc a a | trans : ∀ {a b c}, r a b → rtc b c → rtc a c def many_steps := rtc step notation e `⇝*`:35 e' := many_steps e e' def halts e := ¬∃ e', step e e'
c3f0c4afc940c4a6e7e1d7b2f8028fcaf48d0e69
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/03_Propositions_and_Proofs.org.5.lean
aef7ba193b5634694e0e8119567616fab650af80
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
108
lean
/- page 35 -/ import standard constants p q : Prop theorem t1 : p → q → p := λ Hp : p, λ Hq : q, Hp
a70a9f5479b35dbd9547915454a344cba5e06adb
05b503addd423dd68145d68b8cde5cd595d74365
/src/computability/turing_machine.lean
5b8e3856bc6bfdb4f39a248e691b269cfe785ce1
[ "Apache-2.0" ]
permissive
aestriplex/mathlib
77513ff2b176d74a3bec114f33b519069788811d
e2fa8b2b1b732d7c25119229e3cdfba8370cb00f
refs/heads/master
1,621,969,960,692
1,586,279,279,000
1,586,279,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
68,075
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Define a sequence of simple machine languages, starting with Turing machines and working up to more complex lanaguages based on Wang B-machines. -/ import data.fintype.basic data.pfun logic.relation open relation namespace turing /-- A direction for the turing machine `move` command, either left or right. -/ @[derive decidable_eq, derive inhabited] inductive dir | left | right def tape (Γ) := Γ × list Γ × list Γ instance {Γ} [inhabited Γ] : inhabited (tape Γ) := ⟨by constructor; apply default⟩ def tape.mk {Γ} [inhabited Γ] (l : list Γ) : tape Γ := (l.head, [], l.tail) def tape.mk' {Γ} [inhabited Γ] (L R : list Γ) : tape Γ := (R.head, L, R.tail) def tape.move {Γ} [inhabited Γ] : dir → tape Γ → tape Γ | dir.left (a, L, R) := (L.head, L.tail, a :: R) | dir.right (a, L, R) := (R.head, a :: L, R.tail) def tape.nth {Γ} [inhabited Γ] : tape Γ → ℤ → Γ | (a, L, R) 0 := a | (a, L, R) (n+1:ℕ) := R.inth n | (a, L, R) -[1+ n] := L.inth n @[simp] theorem tape.nth_zero {Γ} [inhabited Γ] : ∀ (T : tape Γ), T.nth 0 = T.1 | (a, L, R) := rfl @[simp] theorem tape.move_left_nth {Γ} [inhabited Γ] : ∀ (T : tape Γ) (i : ℤ), (T.move dir.left).nth i = T.nth (i-1) | (a, L, R) -[1+ n] := by cases L; refl | (a, L, R) 0 := by cases L; refl | (a, L, R) 1 := rfl | (a, L, R) ((n+1:ℕ)+1) := by rw add_sub_cancel; refl @[simp] theorem tape.move_right_nth {Γ} [inhabited Γ] : ∀ (T : tape Γ) (i : ℤ), (T.move dir.right).nth i = T.nth (i+1) | (a, L, R) (n+1:ℕ) := by cases R; refl | (a, L, R) 0 := by cases R; refl | (a, L, R) -1 := rfl | (a, L, R) -[1+ n+1] := show _ = tape.nth _ (-[1+ n] - 1 + 1), by rw sub_add_cancel; refl def tape.write {Γ} (b : Γ) : tape Γ → tape Γ | (a, LR) := (b, LR) @[simp] theorem tape.write_self {Γ} : ∀ (T : tape Γ), T.write T.1 = T | (a, LR) := rfl @[simp] theorem tape.write_nth {Γ} [inhabited Γ] (b : Γ) : ∀ (T : tape Γ) {i : ℤ}, (T.write b).nth i = if i = 0 then b else T.nth i | (a, L, R) 0 := rfl | (a, L, R) (n+1:ℕ) := rfl | (a, L, R) -[1+ n] := rfl def tape.map {Γ Γ'} (f : Γ → Γ') : tape Γ → tape Γ' | (a, L, R) := (f a, L.map f, R.map f) @[simp] theorem tape.map_fst {Γ Γ'} (f : Γ → Γ') : ∀ (T : tape Γ), (T.map f).1 = f T.1 | (a, L, R) := rfl @[simp] theorem tape.map_write {Γ Γ'} (f : Γ → Γ') (b : Γ) : ∀ (T : tape Γ), (T.write b).map f = (T.map f).write (f b) | (a, L, R) := rfl @[class] def pointed_map {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : Γ → Γ') := f (default _) = default _ theorem tape.map_move {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : Γ → Γ') [pointed_map f] : ∀ (T : tape Γ) d, (T.move d).map f = (T.map f).move d | (a, [], R) dir.left := prod.ext ‹pointed_map f› rfl | (a, b::L, R) dir.left := rfl | (a, L, []) dir.right := prod.ext ‹pointed_map f› rfl | (a, L, b::R) dir.right := rfl theorem tape.map_mk {Γ Γ'} [inhabited Γ] [inhabited Γ'] (f : Γ → Γ') [f0 : pointed_map f] : ∀ (l : list Γ), (tape.mk l).map f = tape.mk (l.map f) | [] := prod.ext ‹pointed_map f› rfl | (a::l) := rfl def eval {σ} (f : σ → option σ) : σ → roption σ := pfun.fix (λ s, roption.some $ match f s with none := sum.inl s | some s' := sum.inr s' end) def reaches {σ} (f : σ → option σ) : σ → σ → Prop := refl_trans_gen (λ a b, b ∈ f a) def reaches₁ {σ} (f : σ → option σ) : σ → σ → Prop := trans_gen (λ a b, b ∈ f a) theorem reaches₁_eq {σ} {f : σ → option σ} {a b c} (h : f a = f b) : reaches₁ f a c ↔ reaches₁ f b c := trans_gen.head'_iff.trans (trans_gen.head'_iff.trans $ by rw h).symm theorem reaches_total {σ} {f : σ → option σ} {a b c} : reaches f a b → reaches f a c → reaches f b c ∨ reaches f c b := refl_trans_gen.total_of_right_unique $ λ _ _ _, option.mem_unique theorem reaches₁_fwd {σ} {f : σ → option σ} {a b c} (h₁ : reaches₁ f a c) (h₂ : b ∈ f a) : reaches f b c := begin rcases trans_gen.head'_iff.1 h₁ with ⟨b', hab, hbc⟩, cases option.mem_unique hab h₂, exact hbc end def reaches₀ {σ} (f : σ → option σ) (a b : σ) : Prop := ∀ c, reaches₁ f b c → reaches₁ f a c theorem reaches₀.trans {σ} {f : σ → option σ} {a b c : σ} (h₁ : reaches₀ f a b) (h₂ : reaches₀ f b c) : reaches₀ f a c | d h₃ := h₁ _ (h₂ _ h₃) @[refl] theorem reaches₀.refl {σ} {f : σ → option σ} (a : σ) : reaches₀ f a a | b h := h theorem reaches₀.single {σ} {f : σ → option σ} {a b : σ} (h : b ∈ f a) : reaches₀ f a b | c h₂ := h₂.head h theorem reaches₀.head {σ} {f : σ → option σ} {a b c : σ} (h : b ∈ f a) (h₂ : reaches₀ f b c) : reaches₀ f a c := (reaches₀.single h).trans h₂ theorem reaches₀.tail {σ} {f : σ → option σ} {a b c : σ} (h₁ : reaches₀ f a b) (h : c ∈ f b) : reaches₀ f a c := h₁.trans (reaches₀.single h) theorem reaches₀_eq {σ} {f : σ → option σ} {a b} (e : f a = f b) : reaches₀ f a b | d h := (reaches₁_eq e).2 h theorem reaches₁.to₀ {σ} {f : σ → option σ} {a b : σ} (h : reaches₁ f a b) : reaches₀ f a b | c h₂ := h.trans h₂ theorem reaches.to₀ {σ} {f : σ → option σ} {a b : σ} (h : reaches f a b) : reaches₀ f a b | c h₂ := h₂.trans_right h theorem reaches₀.tail' {σ} {f : σ → option σ} {a b c : σ} (h : reaches₀ f a b) (h₂ : c ∈ f b) : reaches₁ f a c := h _ (trans_gen.single h₂) theorem mem_eval {σ} {f : σ → option σ} {a b} : b ∈ eval f a ↔ reaches f a b ∧ f b = none := ⟨λ h, begin refine pfun.fix_induction h (λ a h IH, _), cases e : f a with a', { rw roption.mem_unique h (pfun.mem_fix_iff.2 $ or.inl $ roption.mem_some_iff.2 $ by rw e; refl), exact ⟨refl_trans_gen.refl, e⟩ }, { rcases pfun.mem_fix_iff.1 h with h | ⟨_, h, h'⟩; rw e at h; cases roption.mem_some_iff.1 h, cases IH a' h' (by rwa e) with h₁ h₂, exact ⟨refl_trans_gen.head e h₁, h₂⟩ } end, λ ⟨h₁, h₂⟩, begin refine refl_trans_gen.head_induction_on h₁ _ (λ a a' h _ IH, _), { refine pfun.mem_fix_iff.2 (or.inl _), rw h₂, apply roption.mem_some }, { refine pfun.mem_fix_iff.2 (or.inr ⟨_, _, IH⟩), rw show f a = _, from h, apply roption.mem_some } end⟩ theorem eval_maximal₁ {σ} {f : σ → option σ} {a b} (h : b ∈ eval f a) (c) : ¬ reaches₁ f b c | bc := let ⟨ab, b0⟩ := mem_eval.1 h, ⟨b', h', _⟩ := trans_gen.head'_iff.1 bc in by cases b0.symm.trans h' theorem eval_maximal {σ} {f : σ → option σ} {a b} (h : b ∈ eval f a) {c} : reaches f b c ↔ c = b := let ⟨ab, b0⟩ := mem_eval.1 h in refl_trans_gen_iff_eq $ λ b' h', by cases b0.symm.trans h' theorem reaches_eval {σ} {f : σ → option σ} {a b} (ab : reaches f a b) : eval f a = eval f b := roption.ext $ λ c, ⟨λ h, let ⟨ac, c0⟩ := mem_eval.1 h in mem_eval.2 ⟨(or_iff_left_of_imp $ by exact λ cb, (eval_maximal h).1 cb ▸ refl_trans_gen.refl).1 (reaches_total ab ac), c0⟩, λ h, let ⟨bc, c0⟩ := mem_eval.1 h in mem_eval.2 ⟨ab.trans bc, c0⟩,⟩ def respects {σ₁ σ₂} (f₁ : σ₁ → option σ₁) (f₂ : σ₂ → option σ₂) (tr : σ₁ → σ₂ → Prop) := ∀ ⦃a₁ a₂⦄, tr a₁ a₂ → (match f₁ a₁ with | some b₁ := ∃ b₂, tr b₁ b₂ ∧ reaches₁ f₂ a₂ b₂ | none := f₂ a₂ = none end : Prop) theorem tr_reaches₁ {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ a₂} (aa : tr a₁ a₂) {b₁} (ab : reaches₁ f₁ a₁ b₁) : ∃ b₂, tr b₁ b₂ ∧ reaches₁ f₂ a₂ b₂ := begin induction ab with c₁ ac c₁ d₁ ac cd IH, { have := H aa, rwa (show f₁ a₁ = _, from ac) at this }, { rcases IH with ⟨c₂, cc, ac₂⟩, have := H cc, rw (show f₁ c₁ = _, from cd) at this, rcases this with ⟨d₂, dd, cd₂⟩, exact ⟨_, dd, ac₂.trans cd₂⟩ } end theorem tr_reaches {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ a₂} (aa : tr a₁ a₂) {b₁} (ab : reaches f₁ a₁ b₁) : ∃ b₂, tr b₁ b₂ ∧ reaches f₂ a₂ b₂ := begin rcases refl_trans_gen_iff_eq_or_trans_gen.1 ab with rfl | ab, { exact ⟨_, aa, refl_trans_gen.refl⟩ }, { exact let ⟨b₂, bb, h⟩ := tr_reaches₁ H aa ab in ⟨b₂, bb, h.to_refl⟩ } end theorem tr_reaches_rev {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ a₂} (aa : tr a₁ a₂) {b₂} (ab : reaches f₂ a₂ b₂) : ∃ c₁ c₂, reaches f₂ b₂ c₂ ∧ tr c₁ c₂ ∧ reaches f₁ a₁ c₁ := begin induction ab with c₂ d₂ ac cd IH, { exact ⟨_, _, refl_trans_gen.refl, aa, refl_trans_gen.refl⟩ }, { rcases IH with ⟨e₁, e₂, ce, ee, ae⟩, rcases refl_trans_gen.cases_head ce with rfl | ⟨d', cd', de⟩, { have := H ee, revert this, cases eg : f₁ e₁ with g₁; simp [respects], { intro c0, cases cd.symm.trans c0 }, { intros g₂ gg cg, rcases trans_gen.head'_iff.1 cg with ⟨d', cd', dg⟩, cases option.mem_unique cd cd', exact ⟨_, _, dg, gg, ae.tail eg⟩ } }, { cases option.mem_unique cd cd', exact ⟨_, _, de, ee, ae⟩ } } end theorem tr_eval {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ b₁ a₂} (aa : tr a₁ a₂) (ab : b₁ ∈ eval f₁ a₁) : ∃ b₂, tr b₁ b₂ ∧ b₂ ∈ eval f₂ a₂ := begin cases mem_eval.1 ab with ab b0, rcases tr_reaches H aa ab with ⟨b₂, bb, ab⟩, refine ⟨_, bb, mem_eval.2 ⟨ab, _⟩⟩, have := H bb, rwa b0 at this end theorem tr_eval_rev {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ b₂ a₂} (aa : tr a₁ a₂) (ab : b₂ ∈ eval f₂ a₂) : ∃ b₁, tr b₁ b₂ ∧ b₁ ∈ eval f₁ a₁ := begin cases mem_eval.1 ab with ab b0, rcases tr_reaches_rev H aa ab with ⟨c₁, c₂, bc, cc, ac⟩, cases (refl_trans_gen_iff_eq (by exact option.eq_none_iff_forall_not_mem.1 b0)).1 bc, refine ⟨_, cc, mem_eval.2 ⟨ac, _⟩⟩, have := H cc, cases f₁ c₁ with d₁, {refl}, rcases this with ⟨d₂, dd, bd⟩, rcases trans_gen.head'_iff.1 bd with ⟨e, h, _⟩, cases b0.symm.trans h end theorem tr_eval_dom {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ a₂} (aa : tr a₁ a₂) : (eval f₂ a₂).dom ↔ (eval f₁ a₁).dom := ⟨λ h, let ⟨b₂, tr, h, _⟩ := tr_eval_rev H aa ⟨h, rfl⟩ in h, λ h, let ⟨b₂, tr, h, _⟩ := tr_eval H aa ⟨h, rfl⟩ in h⟩ def frespects {σ₁ σ₂} (f₂ : σ₂ → option σ₂) (tr : σ₁ → σ₂) (a₂ : σ₂) : option σ₁ → Prop | (some b₁) := reaches₁ f₂ a₂ (tr b₁) | none := f₂ a₂ = none theorem frespects_eq {σ₁ σ₂} {f₂ : σ₂ → option σ₂} {tr : σ₁ → σ₂} {a₂ b₂} (h : f₂ a₂ = f₂ b₂) : ∀ {b₁}, frespects f₂ tr a₂ b₁ ↔ frespects f₂ tr b₂ b₁ | (some b₁) := reaches₁_eq h | none := by unfold frespects; rw h theorem fun_respects {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂} : respects f₁ f₂ (λ a b, tr a = b) ↔ ∀ ⦃a₁⦄, frespects f₂ tr (tr a₁) (f₁ a₁) := forall_congr $ λ a₁, by cases f₁ a₁; simp only [frespects, respects, exists_eq_left', forall_eq'] theorem tr_eval' {σ₁ σ₂} (f₁ : σ₁ → option σ₁) (f₂ : σ₂ → option σ₂) (tr : σ₁ → σ₂) (H : respects f₁ f₂ (λ a b, tr a = b)) (a₁) : eval f₂ (tr a₁) = tr <$> eval f₁ a₁ := roption.ext $ λ b₂, ⟨λ h, let ⟨b₁, bb, hb⟩ := tr_eval_rev H rfl h in (roption.mem_map_iff _).2 ⟨b₁, hb, bb⟩, λ h, begin rcases (roption.mem_map_iff _).1 h with ⟨b₁, ab, bb⟩, rcases tr_eval H rfl ab with ⟨_, rfl, h⟩, rwa bb at h end⟩ def dwrite {K} [decidable_eq K] {C : K → Type*} (S : ∀ k, C k) (k') (l : C k') (k) : C k := if h : k = k' then eq.rec_on h.symm l else S k @[simp] theorem dwrite_eq {K} [decidable_eq K] {C : K → Type*} (S : ∀ k, C k) (k) (l : C k) : dwrite S k l k = l := dif_pos rfl @[simp] theorem dwrite_ne {K} [decidable_eq K] {C : K → Type*} (S : ∀ k, C k) (k') (l : C k') (k) (h : ¬ k = k') : dwrite S k' l k = S k := dif_neg h @[simp] theorem dwrite_self {K} [decidable_eq K] {C : K → Type*} (S : ∀ k, C k) (k) : dwrite S k (S k) = S := funext $ λ k', by unfold dwrite; split_ifs; [subst h, refl] namespace TM0 section parameters (Γ : Type*) [inhabited Γ] -- type of tape symbols parameters (Λ : Type*) [inhabited Λ] -- type of "labels" or TM states /-- A Turing machine "statement" is just a command to either move left or right, or write a symbol on the tape. -/ @[derive inhabited] inductive stmt | move {} : dir → stmt | write {} : Γ → stmt /-- A Post-Turing machine with symbol type `Γ` and label type `Λ` is a function which, given the current state `q : Λ` and the tape head `a : Γ`, either halts (returns `none`) or returns a new state `q' : Λ` and a `stmt` describing what to do, either a move left or right, or a write command. Both `Λ` and `Γ` are required to be inhabited; the default value for `Γ` is the "blank" tape value, and the default value of `Λ` is the initial state. -/ def machine := Λ → Γ → option (Λ × stmt) instance machine.inhabited : inhabited machine := by unfold machine; apply_instance /-- The configuration state of a Turing machine during operation consists of a label (machine state), and a tape, represented in the form `(a, L, R)` meaning the tape looks like `L.rev ++ [a] ++ R` with the machine currently reading the `a`. The lists are automatically extended with blanks as the machine moves around. -/ @[derive inhabited] structure cfg := (q : Λ) (tape : tape Γ) parameters {Γ Λ} /-- Execution semantics of the Turing machine. -/ def step (M : machine) : cfg → option cfg | ⟨q, T⟩ := (M q T.1).map (λ ⟨q', a⟩, ⟨q', match a with | stmt.move d := T.move d | stmt.write a := T.write a end⟩) /-- The statement `reaches M s₁ s₂` means that `s₂` is obtained starting from `s₁` after a finite number of steps from `s₂`. -/ def reaches (M : machine) : cfg → cfg → Prop := refl_trans_gen (λ a b, b ∈ step M a) /-- The initial configuration. -/ def init (l : list Γ) : cfg := ⟨default Λ, tape.mk l⟩ /-- Evaluate a Turing machine on initial input to a final state, if it terminates. -/ def eval (M : machine) (l : list Γ) : roption (list Γ) := (eval (step M) (init l)).map (λ c, c.tape.2.2) /-- The raw definition of a Turing machine does not require that `Γ` and `Λ` are finite, and in practice we will be interested in the infinite `Λ` case. We recover instead a notion of "effectively finite" Turing machines, which only make use of a finite subset of their states. We say that a set `S ⊆ Λ` supports a Turing machine `M` if `S` is closed under the transition function and contains the initial state. -/ def supports (M : machine) (S : set Λ) := default Λ ∈ S ∧ ∀ {q a q' s}, (q', s) ∈ M q a → q ∈ S → q' ∈ S theorem step_supports (M : machine) {S} (ss : supports M S) : ∀ {c c' : cfg}, c' ∈ step M c → c.q ∈ S → c'.q ∈ S | ⟨q, T⟩ c' h₁ h₂ := begin rcases option.map_eq_some'.1 h₁ with ⟨⟨q', a⟩, h, rfl⟩, exact ss.2 h h₂, end theorem univ_supports (M : machine) : supports M set.univ := ⟨trivial, λ q a q' s h₁ h₂, trivial⟩ end section variables {Γ : Type*} [inhabited Γ] variables {Γ' : Type*} [inhabited Γ'] variables {Λ : Type*} [inhabited Λ] variables {Λ' : Type*} [inhabited Λ'] def stmt.map (f : Γ → Γ') : stmt Γ → stmt Γ' | (stmt.move d) := stmt.move d | (stmt.write a) := stmt.write (f a) def cfg.map (f : Γ → Γ') (g : Λ → Λ') : cfg Γ Λ → cfg Γ' Λ' | ⟨q, T⟩ := ⟨g q, T.map f⟩ variables (M : machine Γ Λ) (f₁ : Γ → Γ') (f₂ : Γ' → Γ) (g₁ : Λ → Λ') (g₂ : Λ' → Λ) def machine.map : machine Γ' Λ' | q l := (M (g₂ q) (f₂ l)).map (prod.map g₁ (stmt.map f₁)) theorem machine.map_step {S} (ss : supports M S) [pointed_map f₁] (f₂₁ : function.right_inverse f₁ f₂) (g₂₁ : ∀ q ∈ S, g₂ (g₁ q) = q) : ∀ c : cfg Γ Λ, c.q ∈ S → (step M c).map (cfg.map f₁ g₁) = step (M.map f₁ f₂ g₁ g₂) (cfg.map f₁ g₁ c) | ⟨q, T⟩ h := begin unfold step machine.map cfg.map, simp only [turing.tape.map_fst, g₂₁ q h, f₂₁ _], rcases M q T.1 with _|⟨q', d|a⟩, {refl}, { simp only [step, cfg.map, option.map_some', tape.map_move f₁], refl }, { simp only [step, cfg.map, option.map_some', tape.map_write], refl } end theorem map_init [pointed_map f₁] [g0 : pointed_map g₁] (l : list Γ) : (init l).map f₁ g₁ = init (l.map f₁) := congr (congr_arg cfg.mk g0) (tape.map_mk _ _) theorem machine.map_respects {S} (ss : supports M S) [pointed_map f₁] [pointed_map g₁] (f₂₁ : function.right_inverse f₁ f₂) (g₂₁ : ∀ q ∈ S, g₂ (g₁ q) = q) : respects (step M) (step (M.map f₁ f₂ g₁ g₂)) (λ a b, a.q ∈ S ∧ cfg.map f₁ g₁ a = b) | c _ ⟨cs, rfl⟩ := begin cases e : step M c with c'; unfold respects, { rw [← M.map_step f₁ f₂ g₁ g₂ ss f₂₁ g₂₁ _ cs, e], refl }, { refine ⟨_, ⟨step_supports M ss e cs, rfl⟩, trans_gen.single _⟩, rw [← M.map_step f₁ f₂ g₁ g₂ ss f₂₁ g₂₁ _ cs, e], exact rfl } end end end TM0 namespace TM1 section parameters (Γ : Type*) [inhabited Γ] -- Type of tape symbols parameters (Λ : Type*) -- Type of function labels parameters (σ : Type*) -- Type of variable settings /-- The TM1 model is a simplification and extension of TM0 (Post-Turing model) in the direction of Wang B-machines. The machine's internal state is extended with a (finite) store `σ` of variables that may be accessed and updated at any time. A machine is given by a `Λ` indexed set of procedures or functions. Each function has a body which is a `stmt`, which can either be a `move` or `write` command, a `branch` (if statement based on the current tape value), a `load` (set the variable value), a `goto` (call another function), or `halt`. Note that here most statements do not have labels; `goto` commands can only go to a new function. All commands have access to the variable value and current tape value. -/ inductive stmt | move : dir → stmt → stmt | write : (Γ → σ → Γ) → stmt → stmt | load : (Γ → σ → σ) → stmt → stmt | branch : (Γ → σ → bool) → stmt → stmt → stmt | goto {} : (Γ → σ → Λ) → stmt | halt {} : stmt open stmt instance stmt.inhabited : inhabited stmt := ⟨halt⟩ /-- The configuration of a TM1 machine is given by the currently evaluating statement, the variable store value, and the tape. -/ @[derive inhabited] structure cfg := (l : option Λ) (var : σ) (tape : tape Γ) parameters {Γ Λ σ} /-- The semantics of TM1 evaluation. -/ def step_aux : stmt → σ → tape Γ → cfg | (move d q) v T := step_aux q v (T.move d) | (write a q) v T := step_aux q v (T.write (a T.1 v)) | (load s q) v T := step_aux q (s T.1 v) T | (branch p q₁ q₂) v T := cond (p T.1 v) (step_aux q₁ v T) (step_aux q₂ v T) | (goto l) v T := ⟨some (l T.1 v), v, T⟩ | halt v T := ⟨none, v, T⟩ def step (M : Λ → stmt) : cfg → option cfg | ⟨none, v, T⟩ := none | ⟨some l, v, T⟩ := some (step_aux (M l) v T) variables [inhabited Λ] [inhabited σ] def init (l : list Γ) : cfg := ⟨some (default _), default _, tape.mk l⟩ def eval (M : Λ → stmt) (l : list Γ) : roption (list Γ) := (eval (step M) (init l)).map (λ c, c.tape.2.2) variables [fintype Γ] def supports_stmt (S : finset Λ) : stmt → Prop | (move d q) := supports_stmt q | (write a q) := supports_stmt q | (load s q) := supports_stmt q | (branch p q₁ q₂) := supports_stmt q₁ ∧ supports_stmt q₂ | (goto l) := ∀ a v, l a v ∈ S | halt := true /-- A set `S` of labels supports machine `M` if all the `goto` statements in the functions in `S` refer only to other functions in `S`. -/ def supports (M : Λ → stmt) (S : finset Λ) := default Λ ∈ S ∧ ∀ q ∈ S, supports_stmt S (M q) open_locale classical noncomputable def stmts₁ : stmt → finset stmt | Q@(move d q) := insert Q (stmts₁ q) | Q@(write a q) := insert Q (stmts₁ q) | Q@(load s q) := insert Q (stmts₁ q) | Q@(branch p q₁ q₂) := insert Q (stmts₁ q₁ ∪ stmts₁ q₂) | Q := {Q} theorem stmts₁_self {q} : q ∈ stmts₁ q := by cases q; apply finset.mem_insert_self theorem stmts₁_trans {q₁ q₂} : q₁ ∈ stmts₁ q₂ → stmts₁ q₁ ⊆ stmts₁ q₂ := begin intros h₁₂ q₀ h₀₁, induction q₂ with _ q IH _ q IH _ q IH; simp only [stmts₁] at h₁₂ ⊢; simp only [finset.mem_insert, finset.mem_union, finset.insert_empty_eq_singleton, finset.mem_singleton] at h₁₂, iterate 3 { rcases h₁₂ with rfl | h₁₂, { unfold stmts₁ at h₀₁, exact h₀₁ }, { exact finset.mem_insert_of_mem (IH h₁₂) } }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { rcases h₁₂ with rfl | h₁₂ | h₁₂, { unfold stmts₁ at h₀₁, exact h₀₁ }, { exact finset.mem_insert_of_mem (finset.mem_union_left _ $ IH₁ h₁₂) }, { exact finset.mem_insert_of_mem (finset.mem_union_right _ $ IH₂ h₁₂) } }, case TM1.stmt.goto : l { subst h₁₂, exact h₀₁ }, case TM1.stmt.halt { subst h₁₂, exact h₀₁ } end theorem stmts₁_supports_stmt_mono {S q₁ q₂} (h : q₁ ∈ stmts₁ q₂) (hs : supports_stmt S q₂) : supports_stmt S q₁ := begin induction q₂ with _ q IH _ q IH _ q IH; simp only [stmts₁, supports_stmt, finset.mem_insert, finset.mem_union, finset.insert_empty_eq_singleton, finset.mem_singleton] at h hs, iterate 3 { rcases h with rfl | h; [exact hs, exact IH h hs] }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { rcases h with rfl | h | h, exacts [hs, IH₁ h hs.1, IH₂ h hs.2] }, case TM1.stmt.goto : l { subst h, exact hs }, case TM1.stmt.halt { subst h, trivial } end noncomputable def stmts (M : Λ → stmt) (S : finset Λ) : finset (option stmt) := (S.bind (λ q, stmts₁ (M q))).insert_none theorem stmts_trans {M : Λ → stmt} {S q₁ q₂} (h₁ : q₁ ∈ stmts₁ q₂) : some q₂ ∈ stmts M S → some q₁ ∈ stmts M S := by simp only [stmts, finset.mem_insert_none, finset.mem_bind, option.mem_def, forall_eq', exists_imp_distrib]; exact λ l ls h₂, ⟨_, ls, stmts₁_trans h₂ h₁⟩ theorem stmts_supports_stmt {M : Λ → stmt} {S q} (ss : supports M S) : some q ∈ stmts M S → supports_stmt S q := by simp only [stmts, finset.mem_insert_none, finset.mem_bind, option.mem_def, forall_eq', exists_imp_distrib]; exact λ l ls h, stmts₁_supports_stmt_mono h (ss.2 _ ls) theorem step_supports (M : Λ → stmt) {S} (ss : supports M S) : ∀ {c c' : cfg}, c' ∈ step M c → c.l ∈ S.insert_none → c'.l ∈ S.insert_none | ⟨some l₁, v, T⟩ c' h₁ h₂ := begin replace h₂ := ss.2 _ (finset.some_mem_insert_none.1 h₂), simp only [step, option.mem_def] at h₁, subst c', revert h₂, induction M l₁ with _ q IH _ q IH _ q IH generalizing v T; intro hs, iterate 3 { exact IH _ _ hs }, case TM1.stmt.branch : p q₁' q₂' IH₁ IH₂ { unfold step_aux, cases p T.1 v, { exact IH₂ _ _ hs.2 }, { exact IH₁ _ _ hs.1 } }, case TM1.stmt.goto { exact finset.some_mem_insert_none.2 (hs _ _) }, case TM1.stmt.halt { apply multiset.mem_cons_self } end end end TM1 namespace TM1to0 section parameters {Γ : Type*} [inhabited Γ] parameters {Λ : Type*} [inhabited Λ] parameters {σ : Type*} [inhabited σ] local notation `stmt₁` := TM1.stmt Γ Λ σ local notation `cfg₁` := TM1.cfg Γ Λ σ local notation `stmt₀` := TM0.stmt Γ parameters (M : Λ → stmt₁) include M def Λ' := option stmt₁ × σ instance : inhabited Λ' := ⟨(some (M (default _)), default _)⟩ open TM0.stmt def tr_aux (s : Γ) : stmt₁ → σ → Λ' × stmt₀ | (TM1.stmt.move d q) v := ((some q, v), move d) | (TM1.stmt.write a q) v := ((some q, v), write (a s v)) | (TM1.stmt.load a q) v := tr_aux q (a s v) | (TM1.stmt.branch p q₁ q₂) v := cond (p s v) (tr_aux q₁ v) (tr_aux q₂ v) | (TM1.stmt.goto l) v := ((some (M (l s v)), v), write s) | TM1.stmt.halt v := ((none, v), write s) local notation `cfg₀` := TM0.cfg Γ Λ' def tr : TM0.machine Γ Λ' | (none, v) s := none | (some q, v) s := some (tr_aux s q v) def tr_cfg : cfg₁ → cfg₀ | ⟨l, v, T⟩ := ⟨(l.map M, v), T⟩ theorem tr_respects : respects (TM1.step M) (TM0.step tr) (λ c₁ c₂, tr_cfg c₁ = c₂) := fun_respects.2 $ λ ⟨l₁, v, T⟩, begin cases l₁ with l₁, {exact rfl}, unfold tr_cfg TM1.step frespects option.map function.comp option.bind, induction M l₁ with _ q IH _ q IH _ q IH generalizing v T, case TM1.stmt.move : d q IH { exact trans_gen.head rfl (IH _ _) }, case TM1.stmt.write : a q IH { exact trans_gen.head rfl (IH _ _) }, case TM1.stmt.load : a q IH { exact (reaches₁_eq (by refl)).2 (IH _ _) }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { unfold TM1.step_aux, cases e : p T.1 v, { exact (reaches₁_eq (by simp only [TM0.step, tr, tr_aux, e]; refl)).2 (IH₂ _ _) }, { exact (reaches₁_eq (by simp only [TM0.step, tr, tr_aux, e]; refl)).2 (IH₁ _ _) } }, iterate 2 { exact trans_gen.single (congr_arg some (congr (congr_arg TM0.cfg.mk rfl) (tape.write_self T))) } end variables [fintype Γ] [fintype σ] noncomputable def tr_stmts (S : finset Λ) : finset Λ' := (TM1.stmts M S).product finset.univ open_locale classical local attribute [simp] TM1.stmts₁_self theorem tr_supports {S : finset Λ} (ss : TM1.supports M S) : TM0.supports tr (↑(tr_stmts S)) := ⟨finset.mem_product.2 ⟨finset.some_mem_insert_none.2 (finset.mem_bind.2 ⟨_, ss.1, TM1.stmts₁_self⟩), finset.mem_univ _⟩, λ q a q' s h₁ h₂, begin rcases q with ⟨_|q, v⟩, {cases h₁}, cases q' with q' v', simp only [tr_stmts, finset.mem_coe, finset.mem_product, finset.mem_univ, and_true] at h₂ ⊢, cases q', {exact multiset.mem_cons_self _ _}, simp only [tr, option.mem_def] at h₁, have := TM1.stmts_supports_stmt ss h₂, revert this, induction q generalizing v; intro hs, case TM1.stmt.move : d q { cases h₁, refine TM1.stmts_trans _ h₂, unfold TM1.stmts₁, exact finset.mem_insert_of_mem TM1.stmts₁_self }, case TM1.stmt.write : b q { cases h₁, refine TM1.stmts_trans _ h₂, unfold TM1.stmts₁, exact finset.mem_insert_of_mem TM1.stmts₁_self }, case TM1.stmt.load : b q IH { refine IH (TM1.stmts_trans _ h₂) _ h₁ hs, unfold TM1.stmts₁, exact finset.mem_insert_of_mem TM1.stmts₁_self }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { change cond (p a v) _ _ = ((some q', v'), s) at h₁, cases p a v, { refine IH₂ (TM1.stmts_trans _ h₂) _ h₁ hs.2, unfold TM1.stmts₁, exact finset.mem_insert_of_mem (finset.mem_union_right _ TM1.stmts₁_self) }, { refine IH₁ (TM1.stmts_trans _ h₂) _ h₁ hs.1, unfold TM1.stmts₁, exact finset.mem_insert_of_mem (finset.mem_union_left _ TM1.stmts₁_self) } }, case TM1.stmt.goto : l { cases h₁, exact finset.some_mem_insert_none.2 (finset.mem_bind.2 ⟨_, hs _ _, TM1.stmts₁_self⟩) }, case TM1.stmt.halt { cases h₁ } end⟩ theorem tr_eval (l : list Γ) : TM0.eval tr l = TM1.eval M l := (congr_arg _ (tr_eval' _ _ _ tr_respects ⟨some _, _, _⟩)).trans begin rw [roption.map_eq_map, roption.map_map, TM1.eval], congr', exact funext (λ ⟨_, _, _⟩, rfl) end end end TM1to0 /- Reduce an n-symbol Turing machine to a 2-symbol Turing machine -/ namespace TM1to1 open TM1 section parameters {Γ : Type*} [inhabited Γ] theorem exists_enc_dec [fintype Γ] : ∃ n (enc : Γ → vector bool n) (dec : vector bool n → Γ), enc (default _) = vector.repeat ff n ∧ ∀ a, dec (enc a) = a := begin rcases fintype.exists_equiv_fin Γ with ⟨n, ⟨F⟩⟩, let G : fin n ↪ fin n → bool := ⟨λ a b, a = b, λ a b h, of_to_bool_true $ (congr_fun h b).trans $ to_bool_tt rfl⟩, let H := (F.to_embedding.trans G).trans (equiv.vector_equiv_fin _ _).symm.to_embedding, let enc := H.set_value (default _) (vector.repeat ff n), exact ⟨_, enc, function.inv_fun enc, H.set_value_eq _ _, function.left_inverse_inv_fun enc.2⟩ end parameters {Λ : Type*} [inhabited Λ] parameters {σ : Type*} [inhabited σ] local notation `stmt₁` := stmt Γ Λ σ local notation `cfg₁` := cfg Γ Λ σ inductive Λ' : Type (max u_1 u_2 u_3) | normal : Λ → Λ' | write : Γ → stmt₁ → Λ' instance : inhabited Λ' := ⟨Λ'.normal (default _)⟩ local notation `stmt'` := stmt bool Λ' σ local notation `cfg'` := cfg bool Λ' σ def read_aux : ∀ n, (vector bool n → stmt') → stmt' | 0 f := f vector.nil | (i+1) f := stmt.branch (λ a s, a) (stmt.move dir.right $ read_aux i (λ v, f (tt :: v))) (stmt.move dir.right $ read_aux i (λ v, f (ff :: v))) parameters {n : ℕ} (enc : Γ → vector bool n) (dec : vector bool n → Γ) def move (d : dir) (q : stmt') : stmt' := (stmt.move d)^[n] q def read (f : Γ → stmt') : stmt' := read_aux n (λ v, move dir.left $ f (dec v)) def write : list bool → stmt' → stmt' | [] q := q | (a :: l) q := stmt.write (λ _ _, a) $ stmt.move dir.right $ write l q def tr_normal : stmt₁ → stmt' | (stmt.move dir.left q) := move dir.right $ (move dir.left)^[2] $ tr_normal q | (stmt.move dir.right q) := move dir.right $ tr_normal q | (stmt.write f q) := read $ λ a, stmt.goto $ λ _ s, Λ'.write (f a s) q | (stmt.load f q) := read $ λ a, stmt.load (λ _ s, f a s) $ tr_normal q | (stmt.branch p q₁ q₂) := read $ λ a, stmt.branch (λ _ s, p a s) (tr_normal q₁) (tr_normal q₂) | (stmt.goto l) := read $ λ a, stmt.goto (λ _ s, Λ'.normal (l a s)) | stmt.halt := move dir.right $ move dir.left $ stmt.halt def tr_tape' (L R : list Γ) : tape bool := tape.mk' (L.bind (λ x, (enc x).to_list.reverse)) (R.bind (λ x, (enc x).to_list) ++ [default _]) def tr_tape : tape Γ → tape bool | (a, L, R) := tr_tape' L (a :: R) theorem tr_tape_drop_right : ∀ R : list Γ, list.drop n (R.bind (λ x, (enc x).to_list)) = R.tail.bind (λ x, (enc x).to_list) | [] := list.drop_nil _ | (a::R) := list.drop_left' (enc a).2 parameters (enc0 : enc (default _) = vector.repeat ff n) section include enc0 theorem tr_tape_take_right : ∀ R : list Γ, list.take' n (R.bind (λ x, (enc x).to_list)) = (enc R.head).to_list | [] := show list.take' n list.nil = _, by rw [list.take'_nil]; exact (congr_arg vector.to_list enc0).symm | (a::R) := list.take'_left' (enc a).2 end parameters (M : Λ → stmt₁) def tr : Λ' → stmt' | (Λ'.normal l) := tr_normal (M l) | (Λ'.write a q) := write (enc a).to_list $ move dir.left $ tr_normal q def tr_cfg : cfg₁ → cfg' | ⟨l, v, T⟩ := ⟨l.map Λ'.normal, v, tr_tape T⟩ include enc0 theorem tr_tape'_move_left (L R) : (tape.move dir.left)^[n] (tr_tape' L R) = (tr_tape' L.tail (L.head :: R)) := begin cases L with a L, { simp only [enc0, vector.repeat, tr_tape', list.cons_bind, list.head, list.append_assoc], suffices : ∀ i R', default _ ∈ R' → (tape.move dir.left^[i]) (tape.mk' [] R') = tape.mk' [] (list.repeat ff i ++ R'), from this n _ (list.mem_append_right _ (list.mem_singleton_self _)), intros i R' hR, induction i with i IH, {refl}, rw [nat.iterate_succ', IH], refine prod.ext rfl (prod.ext rfl (list.cons_head_tail (list.ne_nil_of_mem $ list.mem_append_right _ hR))) }, { simp only [tr_tape', list.cons_bind, list.append_assoc], suffices : ∀ L' R' l₁ l₂ (hR : default _ ∈ R') (e : vector.to_list (enc a) = list.reverse_core l₁ l₂), (tape.move dir.left^[l₁.length]) (tape.mk' (l₁ ++ L') (l₂ ++ R')) = tape.mk' L' (vector.to_list (enc a) ++ R'), { simpa only [list.length_reverse, vector.to_list_length] using this _ _ _ _ _ (list.reverse_reverse _).symm, exact list.mem_append_right _ (list.mem_singleton_self _) }, intros, induction l₁ with b l₁ IH generalizing l₂, { cases e, refl }, simp only [list.length, list.cons_append, nat.iterate_succ], convert IH _ e, exact prod.ext rfl (prod.ext rfl (list.cons_head_tail (list.ne_nil_of_mem $ list.mem_append_right _ hR))) } end theorem tr_tape'_move_right (L R) : (tape.move dir.right)^[n] (tr_tape' L R) = (tr_tape' (R.head :: L) R.tail) := begin cases R with a R, { simp only [enc0, vector.repeat, tr_tape', list.head, list.cons_bind, vector.to_list_mk, list.reverse_repeat], suffices : ∀ i L', (tape.move dir.right^[i]) (ff, L', []) = (ff, list.repeat ff i ++ L', []), from this n _, intros, induction i with i IH, {refl}, rw [nat.iterate_succ', IH], refine prod.ext rfl (prod.ext rfl rfl) }, { simp only [tr_tape', list.cons_bind, list.append_assoc], suffices : ∀ L' R' l₁ l₂ : list bool, (tape.move dir.right^[l₂.length]) (tape.mk' (l₁ ++ L') (l₂ ++ R')) = tape.mk' (list.reverse_core l₂ l₁ ++ L') R', { simpa only [vector.to_list_length] using this _ _ [] (enc a).to_list }, intros, induction l₂ with b l₂ IH generalizing l₁, {refl}, exact IH (b::l₁) } end theorem step_aux_move (d q v T) : step_aux (move d q) v T = step_aux q v ((tape.move d)^[n] T) := begin suffices : ∀ i, step_aux (stmt.move d^[i] q) v T = step_aux q v (tape.move d^[i] T), from this n, intro, induction i with i IH generalizing T, {refl}, rw [nat.iterate_succ', step_aux, IH, ← nat.iterate_succ] end parameters (encdec : ∀ a, dec (enc a) = a) include encdec theorem step_aux_read (f v L R) : step_aux (read f) v (tr_tape' L R) = step_aux (f R.head) v (tr_tape' L (R.head :: R.tail)) := begin suffices : ∀ f, step_aux (read_aux n f) v (tr_tape' enc L R) = step_aux (f (enc R.head)) v (tr_tape' enc (R.head :: L) R.tail), { rw [read, this, step_aux_move enc enc0, encdec, tr_tape'_move_left enc enc0], refl }, cases R with a R, { suffices : ∀ i f L', step_aux (read_aux i f) v (ff, L', []) = step_aux (f (vector.repeat ff i)) v (ff, list.repeat ff i ++ L', []), { intro f, convert this n f _, refine prod.ext rfl (prod.ext ((list.cons_bind _ _ _).trans _) rfl), simp only [list.head, enc0, vector.repeat, vector.to_list, list.reverse_repeat] }, clear f L, intros, induction i with i IH generalizing L', {refl}, change step_aux (read_aux i (λ v, f (ff :: v))) v (ff, ff :: L', []) = step_aux (f (vector.repeat ff (nat.succ i))) v (ff, ff :: (list.repeat ff i ++ L'), []), rw [IH], congr', simpa only [list.append_assoc] using congr_arg (++ L') (list.repeat_add ff i 1).symm }, { simp only [tr_tape', list.cons_bind, list.append_assoc], suffices : ∀ i f L' R' l₁ l₂ h, step_aux (read_aux i f) v (tape.mk' (l₁ ++ L') (l₂ ++ R')) = step_aux (f ⟨l₂, h⟩) v (tape.mk' (l₂.reverse_core l₁ ++ L') R'), { intro f, convert this n f _ _ _ _ (enc a).2; simp only [subtype.eta]; refl }, clear f L a R, intros, subst i, induction l₂ with a l₂ IH generalizing l₁, {refl}, change (tape.mk' (l₁ ++ L') (a :: (l₂ ++ R'))).1 with a, transitivity step_aux (read_aux l₂.length (λ v, f (a :: v))) v (tape.mk' (a :: l₁ ++ L') (l₂ ++ R')), { cases a; refl }, rw IH, refl } end theorem step_aux_write (q v a b L R) : step_aux (write (enc a).to_list q) v (tr_tape' L (b :: R)) = step_aux q v (tr_tape' (a :: L) R) := begin simp only [tr_tape', list.cons_bind, list.append_assoc], suffices : ∀ {L' R'} (l₁ l₂ l₂' : list bool) (e : l₂'.length = l₂.length), step_aux (write l₂ q) v (tape.mk' (l₁ ++ L') (l₂' ++ R')) = step_aux q v (tape.mk' (list.reverse_core l₂ l₁ ++ L') R'), from this [] _ _ ((enc b).2.trans (enc a).2.symm), clear a b L R, intros, induction l₂ with a l₂ IH generalizing l₁ l₂', { cases list.length_eq_zero.1 e, refl }, cases l₂' with b l₂'; injection e with e, unfold write step_aux, convert IH _ _ e, refl end theorem tr_respects : respects (step M) (step tr) (λ c₁ c₂, tr_cfg c₁ = c₂) := fun_respects.2 $ λ ⟨l₁, v, (a, L, R)⟩, begin cases l₁ with l₁, {exact rfl}, suffices : ∀ q R, reaches (step (tr enc dec M)) (step_aux (tr_normal dec q) v (tr_tape' enc L R)) (tr_cfg enc (step_aux q v (tape.mk' L R))), { refine trans_gen.head' rfl (this _ (a::R)) }, clear R l₁, intros, induction q with _ q IH _ q IH _ q IH generalizing v L R, case TM1.stmt.move : d q IH { cases d; simp only [tr_normal, nat.iterate, step_aux_move enc enc0, step_aux, tr_tape'_move_left enc enc0, tr_tape'_move_right enc enc0]; apply IH }, case TM1.stmt.write : a q IH { simp only [tr_normal, step_aux_read enc dec enc0 encdec, step_aux], refine refl_trans_gen.head rfl _, simp only [tr, tr_normal, step_aux, step_aux_write enc dec enc0 encdec, step_aux_move enc enc0, tr_tape'_move_left enc enc0], apply IH }, case TM1.stmt.load : a q IH { simp only [tr_normal, step_aux_read enc dec enc0 encdec], apply IH }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { simp only [tr_normal, step_aux_read enc dec enc0 encdec, step_aux], change (tape.mk' L R).1 with R.head, cases p R.head v; [apply IH₂, apply IH₁] }, case TM1.stmt.goto : l { simp only [tr_normal, step_aux_read enc dec enc0 encdec, step_aux], apply refl_trans_gen.refl }, case TM1.stmt.halt { simp only [tr_normal, step_aux, tr_cfg, step_aux_move enc enc0, tr_tape'_move_left enc enc0, tr_tape'_move_right enc enc0], apply refl_trans_gen.refl } end omit enc0 encdec open_locale classical parameters [fintype Γ] noncomputable def writes : stmt₁ → finset Λ' | (stmt.move d q) := writes q | (stmt.write f q) := finset.univ.image (λ a, Λ'.write a q) ∪ writes q | (stmt.load f q) := writes q | (stmt.branch p q₁ q₂) := writes q₁ ∪ writes q₂ | (stmt.goto l) := ∅ | stmt.halt := ∅ noncomputable def tr_supp (S : finset Λ) : finset Λ' := S.bind (λ l, insert (Λ'.normal l) (writes (M l))) theorem supports_stmt_move {S d q} : supports_stmt S (move d q) = supports_stmt S q := suffices ∀ {i}, supports_stmt S (stmt.move d^[i] q) = _, from this, by intro; induction i generalizing q; simp only [*, nat.iterate]; refl theorem supports_stmt_write {S l q} : supports_stmt S (write l q) = supports_stmt S q := by induction l with a l IH; simp only [write, supports_stmt, *] local attribute [simp] supports_stmt_move supports_stmt_write theorem supports_stmt_read {S} : ∀ {f : Γ → stmt'}, (∀ a, supports_stmt S (f a)) → supports_stmt S (read f) := suffices ∀ i (f : vector bool i → stmt'), (∀ v, supports_stmt S (f v)) → supports_stmt S (read_aux i f), from λ f hf, this n _ (by intro; simp only [supports_stmt_move, hf]), λ i f hf, begin induction i with i IH, {exact hf _}, split; apply IH; intro; apply hf, end theorem tr_supports {S} (ss : supports M S) : supports tr (tr_supp S) := ⟨finset.mem_bind.2 ⟨_, ss.1, finset.mem_insert_self _ _⟩, λ q h, begin suffices : ∀ q, supports_stmt S q → (∀ q' ∈ writes q, q' ∈ tr_supp M S) → supports_stmt (tr_supp M S) (tr_normal dec q) ∧ ∀ q' ∈ writes q, supports_stmt (tr_supp M S) (tr enc dec M q'), { rcases finset.mem_bind.1 h with ⟨l, hl, h⟩, have := this _ (ss.2 _ hl) (λ q' hq, finset.mem_bind.2 ⟨_, hl, finset.mem_insert_of_mem hq⟩), rcases finset.mem_insert.1 h with rfl | h, exacts [this.1, this.2 _ h] }, intros q hs hw, induction q, case TM1.stmt.move : d q IH { unfold writes at hw ⊢, replace IH := IH hs hw, refine ⟨_, IH.2⟩, cases d; simp only [tr_normal, nat.iterate, supports_stmt_move, IH] }, case TM1.stmt.write : f q IH { unfold writes at hw ⊢, simp only [finset.mem_image, finset.mem_union, finset.mem_univ, exists_prop, true_and] at hw ⊢, replace IH := IH hs (λ q hq, hw q (or.inr hq)), refine ⟨supports_stmt_read _ $ λ a _ s, hw _ (or.inl ⟨_, rfl⟩), λ q' hq, _⟩, rcases hq with ⟨a, q₂, rfl⟩ | hq, { simp only [tr, supports_stmt_write, supports_stmt_move, IH.1] }, { exact IH.2 _ hq } }, case TM1.stmt.load : a q IH { unfold writes at hw ⊢, replace IH := IH hs hw, refine ⟨supports_stmt_read _ (λ a, IH.1), IH.2⟩ }, case TM1.stmt.branch : p q₁ q₂ IH₁ IH₂ { unfold writes at hw ⊢, simp only [finset.mem_union] at hw ⊢, replace IH₁ := IH₁ hs.1 (λ q hq, hw q (or.inl hq)), replace IH₂ := IH₂ hs.2 (λ q hq, hw q (or.inr hq)), exact ⟨supports_stmt_read _ (λ a, ⟨IH₁.1, IH₂.1⟩), λ q, or.rec (IH₁.2 _) (IH₂.2 _)⟩ }, case TM1.stmt.goto : l { refine ⟨_, λ _, false.elim⟩, refine supports_stmt_read _ (λ a _ s, _), exact finset.mem_bind.2 ⟨_, hs _ _, finset.mem_insert_self _ _⟩ }, case TM1.stmt.halt { refine ⟨_, λ _, false.elim⟩, simp only [supports_stmt, supports_stmt_move, tr_normal] } end⟩ end end TM1to1 namespace TM0to1 section parameters {Γ : Type*} [inhabited Γ] parameters {Λ : Type*} [inhabited Λ] inductive Λ' | normal : Λ → Λ' | act : TM0.stmt Γ → Λ → Λ' instance : inhabited Λ' := ⟨Λ'.normal (default _)⟩ local notation `cfg₀` := TM0.cfg Γ Λ local notation `stmt₁` := TM1.stmt Γ Λ' unit local notation `cfg₁` := TM1.cfg Γ Λ' unit parameters (M : TM0.machine Γ Λ) open TM1.stmt def tr : Λ' → stmt₁ | (Λ'.normal q) := branch (λ a _, (M q a).is_none) halt $ goto (λ a _, match M q a with | none := default _ | some (q', s) := Λ'.act s q' end) | (Λ'.act (TM0.stmt.move d) q) := move d $ goto (λ _ _, Λ'.normal q) | (Λ'.act (TM0.stmt.write a) q) := write (λ _ _, a) $ goto (λ _ _, Λ'.normal q) def tr_cfg : cfg₀ → cfg₁ | ⟨q, T⟩ := ⟨cond (M q T.1).is_some (some (Λ'.normal q)) none, (), T⟩ theorem tr_respects : respects (TM0.step M) (TM1.step tr) (λ a b, tr_cfg a = b) := fun_respects.2 $ λ ⟨q, T⟩, begin cases e : M q T.1, { simp only [TM0.step, tr_cfg, e]; exact eq.refl none }, cases val with q' s, simp only [frespects, TM0.step, tr_cfg, e, option.is_some, cond, option.map_some'], have : TM1.step (tr M) ⟨some (Λ'.act s q'), (), T⟩ = some ⟨some (Λ'.normal q'), (), TM0.step._match_1 T s⟩, { cases s with d a; refl }, refine trans_gen.head _ (trans_gen.head' this _), { unfold TM1.step TM1.step_aux tr has_mem.mem, rw e, refl }, cases e' : M q' _, { apply refl_trans_gen.single, unfold TM1.step TM1.step_aux tr has_mem.mem, rw e', refl }, { refl } end end end TM0to1 namespace TM2 section parameters {K : Type*} [decidable_eq K] -- Index type of stacks parameters (Γ : K → Type*) -- Type of stack elements parameters (Λ : Type*) -- Type of function labels parameters (σ : Type*) -- Type of variable settings /-- The TM2 model removes the tape entirely from the TM1 model, replacing it with an arbitrary (finite) collection of stacks. The operation `push` puts an element on one of the stacks, and `pop` removes an element from a stack (and modifying the internal state based on the result). `peek` modifies the internal state but does not remove an element. -/ inductive stmt | push {} : ∀ k, (σ → Γ k) → stmt → stmt | peek {} : ∀ k, (σ → option (Γ k) → σ) → stmt → stmt | pop {} : ∀ k, (σ → option (Γ k) → σ) → stmt → stmt | load : (σ → σ) → stmt → stmt | branch : (σ → bool) → stmt → stmt → stmt | goto {} : (σ → Λ) → stmt | halt {} : stmt open stmt instance stmt.inhabited : inhabited stmt := ⟨halt⟩ structure cfg := (l : option Λ) (var : σ) (stk : ∀ k, list (Γ k)) instance cfg.inhabited [inhabited σ] [∀ k, inhabited (Γ k)] : inhabited cfg := ⟨by constructor; intros; apply default⟩ parameters {Γ Λ σ K} def step_aux : stmt → σ → (∀ k, list (Γ k)) → cfg | (push k f q) v S := step_aux q v (dwrite S k (f v :: S k)) | (peek k f q) v S := step_aux q (f v (S k).head') S | (pop k f q) v S := step_aux q (f v (S k).head') (dwrite S k (S k).tail) | (load a q) v S := step_aux q (a v) S | (branch f q₁ q₂) v S := cond (f v) (step_aux q₁ v S) (step_aux q₂ v S) | (goto f) v S := ⟨some (f v), v, S⟩ | halt v S := ⟨none, v, S⟩ def step (M : Λ → stmt) : cfg → option cfg | ⟨none, v, S⟩ := none | ⟨some l, v, S⟩ := some (step_aux (M l) v S) def reaches (M : Λ → stmt) : cfg → cfg → Prop := refl_trans_gen (λ a b, b ∈ step M a) variables [inhabited Λ] [inhabited σ] def init (k) (L : list (Γ k)) : cfg := ⟨some (default _), default _, dwrite (λ _, []) k L⟩ def eval (M : Λ → stmt) (k) (L : list (Γ k)) : roption (list (Γ k)) := (eval (step M) (init k L)).map $ λ c, c.stk k variables [fintype K] [∀ k, fintype (Γ k)] [fintype σ] def supports_stmt (S : finset Λ) : stmt → Prop | (push k f q) := supports_stmt q | (peek k f q) := supports_stmt q | (pop k f q) := supports_stmt q | (load a q) := supports_stmt q | (branch f q₁ q₂) := supports_stmt q₁ ∧ supports_stmt q₂ | (goto l) := ∀ v, l v ∈ S | halt := true def supports (M : Λ → stmt) (S : finset Λ) := default Λ ∈ S ∧ ∀ q ∈ S, supports_stmt S (M q) open_locale classical noncomputable def stmts₁ : stmt → finset stmt | Q@(push k f q) := insert Q (stmts₁ q) | Q@(peek k f q) := insert Q (stmts₁ q) | Q@(pop k f q) := insert Q (stmts₁ q) | Q@(load a q) := insert Q (stmts₁ q) | Q@(branch f q₁ q₂) := insert Q (stmts₁ q₁ ∪ stmts₁ q₂) | Q@(goto l) := {Q} | Q@halt := {Q} theorem stmts₁_self {q} : q ∈ stmts₁ q := by cases q; apply finset.mem_insert_self theorem stmts₁_trans {q₁ q₂} : q₁ ∈ stmts₁ q₂ → stmts₁ q₁ ⊆ stmts₁ q₂ := begin intros h₁₂ q₀ h₀₁, induction q₂ with _ _ q IH _ _ q IH _ _ q IH _ q IH; simp only [stmts₁] at h₁₂ ⊢; simp only [finset.mem_insert, finset.insert_empty_eq_singleton, finset.mem_singleton, finset.mem_union] at h₁₂, iterate 4 { rcases h₁₂ with rfl | h₁₂, { unfold stmts₁ at h₀₁, exact h₀₁ }, { exact finset.mem_insert_of_mem (IH h₁₂) } }, case TM2.stmt.branch : f q₁ q₂ IH₁ IH₂ { rcases h₁₂ with rfl | h₁₂ | h₁₂, { unfold stmts₁ at h₀₁, exact h₀₁ }, { exact finset.mem_insert_of_mem (finset.mem_union_left _ (IH₁ h₁₂)) }, { exact finset.mem_insert_of_mem (finset.mem_union_right _ (IH₂ h₁₂)) } }, case TM2.stmt.goto : l { subst h₁₂, exact h₀₁ }, case TM2.stmt.halt { subst h₁₂, exact h₀₁ } end theorem stmts₁_supports_stmt_mono {S q₁ q₂} (h : q₁ ∈ stmts₁ q₂) (hs : supports_stmt S q₂) : supports_stmt S q₁ := begin induction q₂ with _ _ q IH _ _ q IH _ _ q IH _ q IH; simp only [stmts₁, supports_stmt, finset.mem_insert, finset.mem_union, finset.insert_empty_eq_singleton, finset.mem_singleton] at h hs, iterate 4 { rcases h with rfl | h; [exact hs, exact IH h hs] }, case TM2.stmt.branch : f q₁ q₂ IH₁ IH₂ { rcases h with rfl | h | h, exacts [hs, IH₁ h hs.1, IH₂ h hs.2] }, case TM2.stmt.goto : l { subst h, exact hs }, case TM2.stmt.halt { subst h, trivial } end noncomputable def stmts (M : Λ → stmt) (S : finset Λ) : finset (option stmt) := (S.bind (λ q, stmts₁ (M q))).insert_none theorem stmts_trans {M : Λ → stmt} {S q₁ q₂} (h₁ : q₁ ∈ stmts₁ q₂) : some q₂ ∈ stmts M S → some q₁ ∈ stmts M S := by simp only [stmts, finset.mem_insert_none, finset.mem_bind, option.mem_def, forall_eq', exists_imp_distrib]; exact λ l ls h₂, ⟨_, ls, stmts₁_trans h₂ h₁⟩ theorem stmts_supports_stmt {M : Λ → stmt} {S q} (ss : supports M S) : some q ∈ stmts M S → supports_stmt S q := by simp only [stmts, finset.mem_insert_none, finset.mem_bind, option.mem_def, forall_eq', exists_imp_distrib]; exact λ l ls h, stmts₁_supports_stmt_mono h (ss.2 _ ls) theorem step_supports (M : Λ → stmt) {S} (ss : supports M S) : ∀ {c c' : cfg}, c' ∈ step M c → c.l ∈ S.insert_none → c'.l ∈ S.insert_none | ⟨some l₁, v, T⟩ c' h₁ h₂ := begin replace h₂ := ss.2 _ (finset.some_mem_insert_none.1 h₂), simp only [step, option.mem_def] at h₁, subst c', revert h₂, induction M l₁ with _ _ q IH _ _ q IH _ _ q IH _ q IH generalizing v T; intro hs, iterate 4 { exact IH _ _ hs }, case TM2.stmt.branch : p q₁' q₂' IH₁ IH₂ { unfold step_aux, cases p v, { exact IH₂ _ _ hs.2 }, { exact IH₁ _ _ hs.1 } }, case TM2.stmt.goto { exact finset.some_mem_insert_none.2 (hs _) }, case TM2.stmt.halt { apply multiset.mem_cons_self } end end end TM2 namespace TM2to1 section parameters {K : Type*} [decidable_eq K] parameters {Γ : K → Type*} parameters {Λ : Type*} [inhabited Λ] parameters {σ : Type*} [inhabited σ] local notation `stmt₂` := TM2.stmt Γ Λ σ local notation `cfg₂` := TM2.cfg Γ Λ σ inductive stackel (k : K) | val : Γ k → stackel | bottom : stackel | top : stackel instance stackel.inhabited (k) : inhabited (stackel k) := ⟨stackel.top _⟩ def stackel.is_bottom {k} : stackel k → bool | (stackel.bottom _) := tt | _ := ff def stackel.is_top {k} : stackel k → bool | (stackel.top _) := tt | _ := ff def stackel.get {k} : stackel k → option (Γ k) | (stackel.val a) := some a | _ := none section open stackel def stackel_equiv {k} : stackel k ≃ option (option (Γ k)) := begin refine ⟨λ s, _, λ s, _, _, _⟩, { cases s, exacts [some (some s), none, some none] }, { rcases s with _|_|s, exacts [bottom _, top _, val s] }, { intro s, cases s; refl }, { intro s, rcases s with _|_|s; refl }, end end def Γ' := ∀ k, stackel k instance Γ'.inhabited : inhabited Γ' := ⟨λ _, default _⟩ instance stackel.fintype {k} [fintype (Γ k)] : fintype (stackel k) := fintype.of_equiv _ stackel_equiv.symm instance Γ'.fintype [fintype K] [∀ k, fintype (Γ k)] : fintype Γ' := pi.fintype inductive st_act (k : K) | push {} : (σ → Γ k) → st_act | pop {} : bool → (σ → option (Γ k) → σ) → st_act section open st_act instance st_act.inhabited {k} : inhabited (st_act k) := ⟨pop (default _) (λ s _, s)⟩ def st_run {k : K} : st_act k → stmt₂ → stmt₂ | (push f) := TM2.stmt.push k f | (pop ff f) := TM2.stmt.peek k f | (pop tt f) := TM2.stmt.pop k f def st_var {k : K} (v : σ) (l : list (Γ k)) : st_act k → σ | (push f) := v | (pop b f) := f v l.head' def st_write {k : K} (v : σ) (l : list (Γ k)) : st_act k → list (Γ k) | (push f) := f v :: l | (pop ff f) := l | (pop tt f) := l.tail @[elab_as_eliminator] def {l} stmt_st_rec {C : stmt₂ → Sort l} (H₁ : Π k (s : st_act k) q (IH : C q), C (st_run s q)) (H₂ : Π a q (IH : C q), C (TM2.stmt.load a q)) (H₃ : Π p q₁ q₂ (IH₁ : C q₁) (IH₂ : C q₂), C (TM2.stmt.branch p q₁ q₂)) (H₄ : Π l, C (TM2.stmt.goto l)) (H₅ : C TM2.stmt.halt) : ∀ n, C n | (TM2.stmt.push k f q) := H₁ _ (push f) _ (stmt_st_rec q) | (TM2.stmt.peek k f q) := H₁ _ (pop ff f) _ (stmt_st_rec q) | (TM2.stmt.pop k f q) := H₁ _ (pop tt f) _ (stmt_st_rec q) | (TM2.stmt.load a q) := H₂ _ _ (stmt_st_rec q) | (TM2.stmt.branch a q₁ q₂) := H₃ _ _ _ (stmt_st_rec q₁) (stmt_st_rec q₂) | (TM2.stmt.goto l) := H₄ _ | TM2.stmt.halt := H₅ theorem supports_run [fintype K] [∀ k, fintype (Γ k)] [fintype σ] (S : finset Λ) {k} (s : st_act k) (q) : TM2.supports_stmt S (st_run s q) ↔ TM2.supports_stmt S q := by rcases s with _|_|_; refl end inductive Λ' : Type (max u_1 u_2 u_3 u_4) | normal {} : Λ → Λ' | go (k) : st_act k → stmt₂ → Λ' | ret {} : K → stmt₂ → Λ' open Λ' instance : inhabited Λ' := ⟨normal (default _)⟩ local notation `stmt₁` := TM1.stmt Γ' Λ' σ local notation `cfg₁` := TM1.cfg Γ' Λ' σ open TM1.stmt def tr_st_act {k} (q : stmt₁) : st_act k → stmt₁ | (st_act.push f) := write (λ a s, dwrite a k $ stackel.val $ f s) $ move dir.right $ write (λ a s, dwrite a k $ stackel.top k) q | (st_act.pop b f) := move dir.left $ load (λ a s, f s (a k).get) $ cond b ( branch (λ a s, (a k).is_bottom) ( move dir.right q ) ( move dir.right $ write (λ a s, dwrite a k $ default _) $ move dir.left $ write (λ a s, dwrite a k $ stackel.top k) q ) ) ( move dir.right q ) def tr_init (k) (L : list (Γ k)) : list Γ' := stackel.bottom :: match L.reverse with | [] := [stackel.top] | (a::L') := dwrite stackel.top k (stackel.val a) :: (L'.map stackel.val ++ [stackel.top k]).map (dwrite (default _) k) end theorem step_run {k : K} (q v S) : ∀ s : st_act k, TM2.step_aux (st_run s q) v S = TM2.step_aux q (st_var v (S k) s) (dwrite S k (st_write v (S k) s)) | (st_act.push f) := rfl | (st_act.pop ff f) := by unfold st_write; rw dwrite_self; refl | (st_act.pop tt f) := rfl def tr_normal : stmt₂ → stmt₁ | (TM2.stmt.push k f q) := goto (λ _ _, go k (st_act.push f) q) | (TM2.stmt.peek k f q) := goto (λ _ _, go k (st_act.pop ff f) q) | (TM2.stmt.pop k f q) := goto (λ _ _, go k (st_act.pop tt f) q) | (TM2.stmt.load a q) := load (λ _, a) (tr_normal q) | (TM2.stmt.branch f q₁ q₂) := branch (λ a, f) (tr_normal q₁) (tr_normal q₂) | (TM2.stmt.goto l) := goto (λ a s, normal (l s)) | TM2.stmt.halt := halt theorem tr_normal_run {k} (s q) : tr_normal (st_run s q) = goto (λ _ _, go k s q) := by rcases s with _|_|_; refl parameters (M : Λ → stmt₂) include M def tr : Λ' → stmt₁ | (normal q) := tr_normal (M q) | (go k s q) := branch (λ a s, (a k).is_top) (tr_st_act (goto (λ _ _, ret k q)) s) (move dir.right $ goto (λ _ _, go k s q)) | (ret k q) := branch (λ a s, (a k).is_bottom) (tr_normal q) (move dir.left $ goto (λ _ _, ret k q)) def tr_stk {k} (S : list (Γ k)) (L : list (stackel k)) : Prop := ∃ n, L = (S.map stackel.val).reverse_core (stackel.top k :: list.repeat (default _) n) local attribute [pp_using_anonymous_constructor] turing.TM1.cfg inductive tr_cfg : cfg₂ → cfg₁ → Prop | mk {q v} {S : ∀ k, list (Γ k)} {L : list Γ'} : (∀ k, tr_stk (S k) (L.map (λ a, a k))) → tr_cfg ⟨q, v, S⟩ ⟨q.map normal, v, (stackel.bottom, [], L)⟩ theorem tr_respects_aux₁ {k} (o q v) : ∀ S₁ {s S₂} {T : list Γ'}, T.map (λ (a : Γ'), a k) = (list.map stackel.val S₁).reverse_core (s :: S₂) → ∃ a T₁ T₂, T = list.reverse_core T₁ (a :: T₂) ∧ a k = s ∧ T₁.map (λ (a : Γ'), a k) = S₁.map stackel.val ∧ T₂.map (λ (a : Γ'), a k) = S₂ ∧ reaches₀ (TM1.step tr) ⟨some (go k o q), v, (stackel.bottom, [], T)⟩ ⟨some (go k o q), v, (a, T₁ ++ [stackel.bottom], T₂)⟩ | [] s S₂ (a :: T) hT := by injection hT with es e₂; exact ⟨a, [], _, rfl, es, rfl, e₂, reaches₀.single rfl⟩ | (s' :: S₁) s S₂ T hT := let ⟨a, T₁, b'::T₂, e, es', e₁, e₂, H⟩ := tr_respects_aux₁ S₁ hT in by injection e₂ with es e₂; exact ⟨b', a::T₁, T₂, e, es, congr (congr_arg list.cons es') e₁, e₂, H.tail (by unfold TM1.step; change some (cond (TM2to1.stackel.is_top (a k)) _ _) = _; rw es'; refl)⟩ local attribute [simp] TM1.step TM1.step_aux tr tr_st_act st_var st_write tape.move tape.write list.reverse_core stackel.get stackel.is_bottom theorem tr_respects_aux₂ {k q v} {S : Π k, list (Γ k)} {T₁ T₂ : list Γ'} {a : Γ'} (hT : ∀ k, tr_stk (S k) ((T₁.reverse_core (a :: T₂)).map (λ (a : Γ'), a k))) (e₁ : T₁.map (λ (a : Γ'), a k) = list.map stackel.val (S k)) (ea : a k = stackel.top k) (o) : let v' := st_var v (S k) o, Sk' := st_write v (S k) o, S' : ∀ k, list (Γ k) := dwrite S k Sk' in ∃ b (T₁' T₂' : list Γ'), (∀ (k' : K), tr_stk (S' k') ((T₁'.reverse_core (b :: T₂')).map (λ (a : Γ'), a k'))) ∧ T₁'.map (λ a, a k) = Sk'.map stackel.val ∧ b k = stackel.top k ∧ TM1.step_aux (tr_st_act q o) v (a, T₁ ++ [stackel.bottom], T₂) = TM1.step_aux q v' (b, T₁' ++ [stackel.bottom], T₂') := begin dsimp only, cases o with f b f, case TM2to1.st_act.push : { refine ⟨_, dwrite a k (stackel.val (f v)) :: T₁, _, _, by simp only [list.map, dwrite_eq, e₁]; refl, by simp only [tape.write, tape.move, dwrite_eq], rfl⟩, intro k', cases hT k' with n e, by_cases h : k' = k, { subst k', existsi n.pred, simp only [list.reverse_core_eq, list.map_append, list.map_reverse, e₁, list.map_cons, list.append_left_inj] at e, simp only [list.reverse_core_eq, e.1, e.2, list.map_append, prod.fst, list.map_reverse, list.reverse_cons, list.map, dwrite_eq, e₁, list.map_tail, list.tail_repeat, TM2to1.st_write] }, { cases T₂ with t T₂, { existsi n+1, simpa only [dwrite_ne _ _ _ _ h, list.reverse_core_eq, e₁, list.repeat_add, tape.write, tape.move, list.reverse_cons, list.map_reverse, list.map_append, list.map, list.head, list.tail, list.append_assoc] using congr_arg (++ [default Γ' k']) e }, { existsi n, simpa only [dwrite_ne _ _ _ _ h, list.reverse_core_eq, e₁, list.repeat_add, tape.write, tape.move, list.reverse_cons, list.map_reverse, list.map_append, list.map, list.head, list.tail, list.append_assoc] using e } } }, have dw := dwrite_self S k, cases T₁ with t T₁; cases eS : S k with s Sk; rw eS at e₁ dw; injection e₁ with tk e₁'; cases b, { -- peek nil simp only [dw, st_write], exact ⟨_, [], _, hT, rfl, ea, rfl⟩ }, { -- pop nil simp only [dw, st_write, list.tail], exact ⟨_, [], _, hT, rfl, ea, rfl⟩ }, { -- peek cons change t k = stackel.val s at tk, simp only [eS, tk, dw, st_write, TM1.step_aux, tr_st_act, cond, tape.move, list.head, list.tail, list.cons_append], exact ⟨_, t::T₁, _, hT, e₁, ea, rfl⟩ }, { -- pop cons change t k = stackel.val s at tk, simp only [tk, st_write, list.tail, TM1.step_aux, tr_st_act, cond, tape.move, list.cons_append, list.head], refine ⟨_, _, _, _, e₁', dwrite_eq _ _ _, rfl⟩, intro k', cases hT k' with n e, by_cases h : k' = k, { subst k', existsi n+1, simp only [list.reverse_core_eq, eS, e₁', list.append_left_inj, list.map_append, list.map_reverse, list.map, list.reverse_cons, list.append_assoc, list.cons_append] at e ⊢, simp only [tape.move, tape.write, list.head, list.tail, dwrite_eq], rw [e.2.2]; refl }, { existsi n, simpa only [dwrite_ne _ _ _ _ h, list.map, list.head, list.tail, list.reverse_core, list.map_reverse_core, tape.move, tape.write] using e } }, end theorem tr_respects_aux₃ {k q v} {S : Π k, list (Γ k)} {T : list Γ'} (hT : ∀ k, tr_stk (S k) (T.map (λ (a : Γ'), a k))) : ∀ (T₁ : list Γ') {T₂ : list Γ'} {a : Γ'} {S₁} (e : T = T₁.reverse_core (a :: T₂)) (ha : (a k).is_bottom = ff) (e₁ : T₁.map (λ (a : Γ'), a k) = list.map stackel.val S₁), reaches₀ (TM1.step tr) ⟨some (ret k q), v, (a, T₁ ++ [stackel.bottom], T₂)⟩ ⟨some (ret k q), v, (stackel.bottom, [], T)⟩ | [] T₂ a S₁ e ha e₁ := reaches₀.single (by simp only [ha, e, TM1.step, option.mem_def, tr, TM1.step_aux] {constructor_eq:=ff}; refl) | (b :: T₁) T₂ a (s :: S₁) e ha e₁ := begin unfold list.map at e₁, injection e₁ with es e₁, refine reaches₀.head _ (tr_respects_aux₃ T₁ e (by rw es; refl) e₁), simp only [ha, option.mem_def, TM1.step, tr, TM1.step_aux], refl end theorem tr_respects_aux {q v T k} {S : Π k, list (Γ k)} (hT : ∀ (k : K), tr_stk (S k) (list.map (λ (a : Γ'), a k) T)) (o : st_act k) (IH : ∀ {v : σ} {S : Π (k : K), list (Γ k)} {T : list Γ'}, (∀ (k : K), tr_stk (S k) (list.map (λ (a : Γ'), a k) T)) → (∃ b, tr_cfg (TM2.step_aux q v S) b ∧ reaches (TM1.step tr) (TM1.step_aux (tr_normal q) v (stackel.bottom, [], T)) b)) : ∃ b, tr_cfg (TM2.step_aux (st_run o q) v S) b ∧ reaches (TM1.step tr) (TM1.step_aux (tr_normal (st_run o q)) v (stackel.bottom, [], T)) b := begin rcases hT k with ⟨n, hTk⟩, simp only [tr_normal_run], rcases tr_respects_aux₁ M o q v _ hTk with ⟨a, T₁, T₂, rfl, ea, e₁, e₂, hgo⟩, rcases tr_respects_aux₂ M hT e₁ ea _ with ⟨b, T₁', T₂', hT', e₁', eb, hrun⟩, have hret := tr_respects_aux₃ M hT' _ rfl (by rw eb; refl) e₁', have := hgo.tail' rfl, simp only [ea, tr, TM1.step_aux] at this, rw [hrun, TM1.step_aux] at this, rcases IH hT' with ⟨c, gc, rc⟩, simp only [step_run], refine ⟨c, gc, (this.to₀.trans hret _ (trans_gen.head' rfl rc)).to_refl⟩ end local attribute [simp] respects TM2.step TM2.step_aux tr_normal theorem tr_respects : respects (TM2.step M) (TM1.step tr) tr_cfg := λ c₁ c₂ h, begin cases h with l v S L hT, clear h, cases l, {constructor}, simp only [TM2.step, respects, option.map_some'], suffices : ∃ b, _ ∧ reaches (TM1.step (tr M)) _ _, from let ⟨b, c, r⟩ := this in ⟨b, c, trans_gen.head' rfl r⟩, rw [tr], revert v S L hT, refine stmt_st_rec _ _ _ _ _ (M l); intros, { exact tr_respects_aux M hT s @IH }, { exact IH hT }, { unfold TM2.step_aux tr_normal TM1.step_aux, cases p v; [exact IH₂ hT, exact IH₁ hT] }, { exact ⟨_, ⟨hT⟩, refl_trans_gen.refl⟩ }, { exact ⟨_, ⟨hT⟩, refl_trans_gen.refl⟩ } end theorem tr_cfg_init (k) (L : list (Γ k)) : tr_cfg (TM2.init k L) (TM1.init (tr_init k L)) := ⟨λ k', begin unfold tr_init, cases e : L.reverse with a L', { cases list.reverse_eq_nil.1 e, rw dwrite_self, exact ⟨0, rfl⟩ }, by_cases k' = k, { subst k', existsi 0, simp only [list.tail, dwrite_eq, list.reverse_core_eq, list.repeat, tr_init, list.map, list.map_map, (∘), list.map_id' (λ _, rfl)], rw [← list.map_reverse, e], refl }, { existsi L'.length + 1, simp only [dwrite_ne _ _ _ _ h, list.tail, tr_init, list.map_map, list.map, list.map_append, list.repeat_add, (∘), list.map_const] {constructor_eq:=ff}, refl } end⟩ theorem tr_eval_dom (k) (L : list (Γ k)) : (TM1.eval tr (tr_init k L)).dom ↔ (TM2.eval M k L).dom := tr_eval_dom tr_respects (tr_cfg_init _ _) theorem tr_eval (k) (L : list (Γ k)) {L₁ L₂} (H₁ : L₁ ∈ TM1.eval tr (tr_init k L)) (H₂ : L₂ ∈ TM2.eval M k L) : ∃ S : ∀ k, list (Γ k), (∀ k', tr_stk (S k') (L₁.map (λ a, a k'))) ∧ S k = L₂ := begin rcases (roption.mem_map_iff _).1 H₁ with ⟨c₁, h₁, rfl⟩, rcases (roption.mem_map_iff _).1 H₂ with ⟨c₂, h₂, rfl⟩, rcases tr_eval (tr_respects M) (tr_cfg_init M k L) h₂ with ⟨_, ⟨q, v, S, L₁', hT⟩, h₃⟩, cases roption.mem_unique h₁ h₃, exact ⟨S, hT, rfl⟩ end variables [fintype K] [∀ k, fintype (Γ k)] [fintype σ] open_locale classical local attribute [simp] TM2.stmts₁_self noncomputable def tr_stmts₁ : stmt₂ → finset Λ' | Q@(TM2.stmt.push k f q) := {go k (st_act.push f) q, ret k q} ∪ tr_stmts₁ q | Q@(TM2.stmt.peek k f q) := {go k (st_act.pop ff f) q, ret k q} ∪ tr_stmts₁ q | Q@(TM2.stmt.pop k f q) := {go k (st_act.pop tt f) q, ret k q} ∪ tr_stmts₁ q | Q@(TM2.stmt.load a q) := tr_stmts₁ q | Q@(TM2.stmt.branch f q₁ q₂) := tr_stmts₁ q₁ ∪ tr_stmts₁ q₂ | _ := ∅ theorem tr_stmts₁_run {k s q} : tr_stmts₁ (st_run s q) = {go k s q, ret k q} ∪ tr_stmts₁ q := by rcases s with _|_|_; unfold tr_stmts₁ st_run noncomputable def tr_supp (S : finset Λ) : finset Λ' := S.bind (λ l, insert (normal l) (tr_stmts₁ (M l))) local attribute [simp] tr_stmts₁ tr_stmts₁_run supports_run tr_normal_run TM1.supports_stmt TM2.supports_stmt theorem tr_supports {S} (ss : TM2.supports M S) : TM1.supports tr (tr_supp S) := ⟨finset.mem_bind.2 ⟨_, ss.1, finset.mem_insert.2 $ or.inl rfl⟩, λ l' h, begin suffices : ∀ q (ss' : TM2.supports_stmt S q) (sub : ∀ x ∈ tr_stmts₁ M q, x ∈ tr_supp M S), TM1.supports_stmt (tr_supp M S) (tr_normal q) ∧ (∀ l' ∈ tr_stmts₁ M q, TM1.supports_stmt (tr_supp M S) (tr M l')), { rcases finset.mem_bind.1 h with ⟨l, lS, h⟩, have := this _ (ss.2 l lS) (λ x hx, finset.mem_bind.2 ⟨_, lS, finset.mem_insert_of_mem hx⟩), rcases finset.mem_insert.1 h with rfl | h; [exact this.1, exact this.2 _ h] }, clear h l', refine stmt_st_rec _ _ _ _ _; intros, { -- stack op rw TM2to1.supports_run at ss', simp only [TM2to1.tr_stmts₁_run, finset.mem_union, finset.insert_empty_eq_singleton, finset.mem_insert, finset.mem_singleton] at sub, have hgo := sub _ (or.inl $ or.inr rfl), have hret := sub _ (or.inl $ or.inl rfl), cases IH ss' (λ x hx, sub x $ or.inr hx) with IH₁ IH₂, refine ⟨by simp only [tr_normal_run, TM1.supports_stmt]; intros; exact hgo, λ l h, _⟩, rw [tr_stmts₁_run] at h, simp only [TM2to1.tr_stmts₁_run, finset.mem_union, finset.insert_empty_eq_singleton, finset.mem_insert, finset.mem_singleton] at h, rcases h with ⟨rfl | rfl⟩ | h, { unfold TM1.supports_stmt TM2to1.tr, exact ⟨IH₁, λ _ _, hret⟩ }, { unfold TM1.supports_stmt TM2to1.tr, rcases s with _|_|_, { exact ⟨λ _ _, hret, λ _ _, hgo⟩ }, { exact ⟨λ _ _, hret, λ _ _, hgo⟩ }, { exact ⟨⟨λ _ _, hret, λ _ _, hret⟩, λ _ _, hgo⟩ } }, { exact IH₂ _ h } }, { -- load unfold TM2to1.tr_stmts₁ at ss' sub ⊢, exact IH ss' sub }, { -- branch unfold TM2to1.tr_stmts₁ at sub, cases IH₁ ss'.1 (λ x hx, sub x $ finset.mem_union_left _ hx) with IH₁₁ IH₁₂, cases IH₂ ss'.2 (λ x hx, sub x $ finset.mem_union_right _ hx) with IH₂₁ IH₂₂, refine ⟨⟨IH₁₁, IH₂₁⟩, λ l h, _⟩, rw [tr_stmts₁] at h, rcases finset.mem_union.1 h with h | h; [exact IH₁₂ _ h, exact IH₂₂ _ h] }, { -- goto rw tr_stmts₁, unfold TM2to1.tr_normal TM1.supports_stmt, unfold TM2.supports_stmt at ss', exact ⟨λ _ v, finset.mem_bind.2 ⟨_, ss' v, finset.mem_insert_self _ _⟩, λ _, false.elim⟩ }, { exact ⟨trivial, λ _, false.elim⟩ } -- halt end⟩ end end TM2to1 end turing
843fc2c33bc193e3598170fdec8b33ab7e2c9e13
8e31b9e0d8cec76b5aa1e60a240bbd557d01047c
/scratch/simplex_no_bar.lean
bd3e11d3a4a57ddc7b70656f5ac5129cacb6424c
[]
no_license
ChrisHughes24/LP
7bdd62cb648461c67246457f3ddcb9518226dd49
e3ed64c2d1f642696104584e74ae7226d8e916de
refs/heads/master
1,685,642,642,858
1,578,070,602,000
1,578,070,602,000
195,268,102
4
3
null
1,569,229,518,000
1,562,255,287,000
Lean
UTF-8
Lean
false
false
33,798
lean
import data.matrix data.rat.basic import linear_algebra.determinant .misc order.pilex import order.lexicographic .list_sup .fin_find tactic.omega tactic.abel import data.list.min_max algebra.associated .matrix_pequiv open matrix fintype finset function variables {m n : ℕ} variables (A : matrix (fin m) (fin n) ℚ) local notation `rvec`:2000 n := matrix (fin 1) (fin n) ℚ local notation `cvec`:2000 m := matrix (fin m) (fin 1) ℚ def list.to_matrix (m :ℕ) (n : ℕ) (l : list (list ℚ)) : matrix (fin m) (fin n) ℚ := λ i j, (l.nth_le i sorry).nth_le j sorry -- def column (j : fin n) : cvec m := λ i _, A i j -- def row (i : fin m) : rvec n := λ _ j, A i j variables (b : cvec m) (c : cvec n) def cvec.ordered_comm_group : ordered_comm_group (cvec n) := matrix.ordered_comm_group local attribute [instance] cvec.ordered_comm_group def cvec.decidable_le : decidable_rel ((≤) : (cvec n) → (cvec n) → Prop) := matrix.decidable_le local attribute [instance] cvec.decidable_le --instance : partial_order (matrix (fin m) (fin n) ℚ) := pi.partial_order local infix ` ⬝ `:70 := matrix.mul local postfix `ᵀ` : 1500 := transpose def is_feasible (x : cvec n) : Prop := 0 ≤ x ∧ A ⬝ x = b instance (x : cvec n) : decidable (is_feasible A b x) := by dunfold is_feasible; apply_instance def is_optimal (x : cvec n) : Prop := is_feasible A b x ∧ ∀ y, is_feasible A b y → cᵀ ⬝ y ≤ cᵀ ⬝ x def is_invertible' (M : matrix (fin m) (fin n) ℚ) : Prop := ∃ h : m = n, by rw h at M; exact det M ≠ 0 instance is_invertible'.decidable : decidable_pred (@is_invertible' m n) := λ _, by unfold is_invertible'; apply_instance def rvec.to_list {n : ℕ} (x : rvec n) : list ℚ := (vector.of_fn (x 0)).1 def lex_rvec : linear_order (rvec m) := @pilex.linear_order _ _ _ (is_well_order.wf _) (λ _, pilex.linear_order (is_well_order.wf _)) def lex_rvec_decidable_linear_order : decidable_linear_order (rvec m) := { decidable_le := λ x y, decidable_of_iff (list.lex (<) (rvec.to_list x) (rvec.to_list y) ∨ x = y) sorry, ..lex_rvec } local attribute [instance] lex_rvec_decidable_linear_order def lex_ordered_comm_group_rvec : ordered_comm_group (rvec m) := @pilex.ordered_comm_group _ _ _ (λ _, pilex.ordered_comm_group) -- local attribute [instance] lex_ordered_comm_group_rvec -- lex_rvec_decidable_linear_order def pert_rat (m : ℕ) : Type := ℚ × rvec m instance : decidable_eq (pert_rat m) := by unfold pert_rat; apply_instance instance : add_comm_group (pert_rat m) := prod.add_comm_group instance : module ℚ (pert_rat m) := prod.module instance : decidable_linear_order (pert_rat m) := by letI := @lex_rvec_decidable_linear_order m; exact { decidable_le := @prod.lex.decidable _ _ _ _ _ _ _ (lex_rvec_decidable_linear_order).decidable_le, ..lex_linear_order } instance pert_rat.decidable_le (i j : pert_rat m) : decidable (i ≤ j) := @decidable_linear_order.decidable_le _ pert_rat.decidable_linear_order i j instance has_repr_fin_fun {n : ℕ} {α : Type*} [has_repr α] : has_repr (fin n → α) := ⟨λ f, repr (vector.of_fn f).to_list⟩ instance {m n} : has_repr (matrix (fin m) (fin n) ℚ) := has_repr_fin_fun instance : has_repr (pert_rat m) := prod.has_repr open list def pequiv_of_vector (v : vector (fin n) m) (hv : v.1.nodup) : pequiv (fin m) (fin n) := { to_fun := λ i, some $ v.nth i, inv_fun := λ j, fin.find (λ i, v.nth i = j), inv := λ i j, ⟨λ h, by rw ← fin.find_spec _ h; exact rfl, λ h, fin.mem_find_of_unique (begin rintros ⟨i, him⟩ ⟨j, hjm⟩ h₁ h₂, cases v, refine fin.eq_of_veq (list.nodup_iff_nth_le_inj.1 hv i j _ _ (h₁.trans h₂.symm)); simpa [v.2] end) (option.some_inj.1 h)⟩ } def nonbasis_vector_of_vector (v : vector (fin n) m) (hv : v.to_list.nodup) : {v : vector (fin n) (n - m) // v.to_list.nodup} := ⟨⟨(fin_range n).diff v.to_list, have h : m ≤ n, by rw [← list.length_fin_range n, ← v.2]; exact list.length_le_of_subperm (subperm_of_subset_nodup hv (λ _ _, list.mem_fin_range _)), begin rw [← add_right_inj m, nat.sub_add_cancel h], conv in m { rw [← vector.to_list_length v] }, rw ← length_append, conv_rhs { rw ← length_fin_range n }, exact list.perm_length ((perm_ext (nodup_append.2 ⟨nodup_diff (nodup_fin_range _), hv, by simp [list.disjoint, mem_diff_iff_of_nodup (nodup_fin_range n), imp_false]⟩) (nodup_fin_range _)).2 (by simp only [list.mem_diff_iff_of_nodup (nodup_fin_range n), list.mem_append, mem_fin_range, true_and, iff_true]; exact λ _, or.symm (decidable.em _))), end⟩, nodup_diff (nodup_fin_range _)⟩ def pre_nonbasis_of_vector (v : vector (fin n) m) (hv : v.1.nodup) : pequiv (fin (n - m)) (fin n) := pequiv_of_vector (nonbasis_vector_of_vector v hv).1 (nonbasis_vector_of_vector v hv).2 structure prebasis (m n : ℕ) : Type := ( basis : pequiv (fin m) (fin n) ) ( nonbasis : pequiv (fin (n - m)) (fin n) ) ( basis_left_inv : basis.trans basis.symm = pequiv.refl (fin m) ) ( nonbasis_left_inv : nonbasis.trans nonbasis.symm = pequiv.refl (fin (n - m)) ) ( basis_trans_nonbasis_symm_eq_bot : basis.trans nonbasis.symm = ⊥ ) namespace prebasis open pequiv attribute [simp] basis_left_inv nonbasis_left_inv basis_trans_nonbasis_symm_eq_bot lemma is_some_basis (B : prebasis m n) : ∀ (i : fin m), (B.basis i).is_some := by rw [← trans_symm_eq_iff_forall_is_some, basis_left_inv] lemma is_some_nonbasis (B : prebasis m n) : ∀ (k : fin (n - m)), (B.nonbasis k).is_some := by rw [← trans_symm_eq_iff_forall_is_some, nonbasis_left_inv] lemma injective_basis (B : prebasis m n) : injective B.basis := injective_of_forall_is_some (is_some_basis B) lemma injective_nonbasis (B : prebasis m n) : injective B.nonbasis := injective_of_forall_is_some (is_some_nonbasis B) def basisg (B : prebasis m n) (r : fin m) : fin n := option.get (B.is_some_basis r) def nonbasisg (B : prebasis m n) (s : fin (n - m)) : fin n := option.get (B.is_some_nonbasis s) lemma injective_basisg (B : prebasis m n) : injective B.basisg := λ x y h, by rw [basisg, basisg, ← option.some_inj, option.some_get, option.some_get] at h; exact injective_basis B h lemma injective_nonbasisg (B : prebasis m n) : injective B.nonbasisg := λ x y h, by rw [nonbasisg, nonbasisg, ← option.some_inj, option.some_get, option.some_get] at h; exact injective_nonbasis B h local infix ` ♣ `: 70 := pequiv.trans def swap (B : prebasis m n) (r : fin m) (s : fin (n - m)) : prebasis m n := { basis := B.basis.trans (equiv.swap (B.basisg r) (B.nonbasisg s)).to_pequiv, nonbasis := B.nonbasis.trans (equiv.swap (B.basisg r) (B.nonbasisg s)).to_pequiv, basis_left_inv := by rw [symm_trans_rev, ← trans_assoc, trans_assoc B.basis, ← equiv.to_pequiv_symm, ← equiv.to_pequiv_trans]; simp, nonbasis_left_inv := by rw [symm_trans_rev, ← trans_assoc, trans_assoc B.nonbasis, ← equiv.to_pequiv_symm, ← equiv.to_pequiv_trans]; simp, basis_trans_nonbasis_symm_eq_bot := by rw [symm_trans_rev, ← trans_assoc, trans_assoc B.basis, ← equiv.to_pequiv_symm, ← equiv.to_pequiv_trans]; simp } lemma not_is_some_nonbasis_of_is_some_basis (B : prebasis m n) (j : fin n) : (B.basis.symm j).is_some → (B.nonbasis.symm j).is_some → false := begin rw [option.is_some_iff_exists, option.is_some_iff_exists], rintros ⟨i, hi⟩ ⟨k, hk⟩, have : B.basis.trans B.nonbasis.symm i = none, { rw [B.basis_trans_nonbasis_symm_eq_bot, pequiv.bot_apply] }, rw [pequiv.trans_eq_none] at this, rw [pequiv.eq_some_iff] at hi, exact (this j k).resolve_left (not_not.2 hi) hk end lemma nonbasis_ne_none_of_basis_eq_none (B : prebasis m n) (j : fin n) (hb : B.basis.symm j = none) (hnb : B.nonbasis.symm j = none) : false := have hs : card (univ.image B.basis) = m, by rw [card_image_of_injective _ (B.injective_basis), card_univ, card_fin], have ht : card (univ.image B.nonbasis) = (n - m), by rw [card_image_of_injective _ (B.injective_nonbasis), card_univ, card_fin], have hst : disjoint (univ.image B.basis) (univ.image B.nonbasis), from finset.disjoint_left.2 begin simp only [mem_image, exists_imp_distrib, not_exists], assume j i _ hbasis k _ hnonbasis, cases option.is_some_iff_exists.1 (is_some_basis B i) with x hi, cases option.is_some_iff_exists.1 (is_some_nonbasis B k) with y hk, have hxy : x = y, { rw [← option.some_inj, ← hk, ← hi, hbasis, hnonbasis] }, subst hxy, rw [← eq_some_iff] at hi hk, refine not_is_some_nonbasis_of_is_some_basis B x _ _; simp [hi, hk] end, have (univ.image B.basis) ∪ (univ.image B.nonbasis) = univ.image (@some (fin n)), from eq_of_subset_of_card_le begin assume i h, simp only [finset.mem_image, finset.mem_union] at h, rcases h with ⟨j, _, hj⟩ | ⟨k, _, hk⟩, { simpa [hj.symm, option.is_some_iff_exists, eq_comm] using is_some_basis B j }, { simpa [hk.symm, option.is_some_iff_exists, eq_comm] using is_some_nonbasis B k } end (begin rw [card_image_of_injective, card_univ, card_fin, card_disjoint_union hst, hs, ht], { clear hst ht hs hnb hb B j, omega }, { intros _ _ h, injection h } end), begin simp only [option.eq_none_iff_forall_not_mem, mem_iff_mem B.basis, mem_iff_mem B.nonbasis] at hb hnb, have := (finset.ext.1 this (some j)).2 (mem_image_of_mem _ (mem_univ _)), simp only [hb, hnb, mem_image, finset.mem_union, option.mem_def.symm] at this, tauto end lemma is_some_basis_iff (B : prebasis m n) (j : fin n) : (B.basis.symm j).is_some ↔ ¬(B.nonbasis.symm j).is_some := ⟨not_is_some_nonbasis_of_is_some_basis B j, by erw [option.not_is_some_iff_eq_none, ← option.ne_none_iff_is_some, forall_swap]; exact nonbasis_ne_none_of_basis_eq_none B j⟩ @[simp] lemma nonbasis_basisg_eq_none (B : prebasis m n) (r : fin m) : B.nonbasis.symm (B.basisg r) = none := option.not_is_some_iff_eq_none.1 ((B.is_some_basis_iff _).1 (is_some_symm_get _ _)) @[simp] lemma basis_nonbasisg_eq_none (B : prebasis m n) (s : fin (n - m)) : B.basis.symm (B.nonbasisg s) = none := option.not_is_some_iff_eq_none.1 (mt (B.is_some_basis_iff _).1 $ not_not.2 (is_some_symm_get _ _)) @[simp] lemma basisg_mem (B : prebasis m n) (r : fin m) : (B.basisg r) ∈ B.basis r := option.get_mem _ @[simp] lemma nonbasisg_mem (B : prebasis m n) (s : fin (n - m)) : (B.nonbasisg s) ∈ B.nonbasis s := option.get_mem _ @[simp] lemma basis_basisg (B : prebasis m n) (r : fin m) : B.basis.symm (B.basisg r) = some r:= B.basis.mem_iff_mem.2 (basisg_mem _ _) @[simp] lemma nonbasis_nonbasisg (B : prebasis m n) (s : fin (n - m)) : s ∈ B.nonbasis.symm (B.nonbasisg s) := B.nonbasis.mem_iff_mem.2 (nonbasisg_mem _ _) lemma basisg_ne_nonbasisg (B : prebasis m n) (i : fin m) (j : fin (n - m)): B.basisg i ≠ B.nonbasisg j := λ h, by simpa using congr_arg B.basis.symm h lemma nonbasis_trans_basis_symm_eq_bot (B : prebasis m n) : B.nonbasis.trans B.basis.symm = ⊥ := symm_injective $ by rw [symm_trans_rev, symm_symm, basis_trans_nonbasis_symm_eq_bot, symm_bot] lemma mul_symm_add_mul_symm (B : prebasis m n) : ((B.basis.symm.trans B.basis).to_matrix : matrix (fin n) (fin n) ℚ) + (B.nonbasis.symm.trans B.nonbasis).to_matrix = 1 := begin ext, simp only [add_val, pequiv.symm_trans, pequiv.to_matrix, one_val, pequiv.mem_of_set_iff, set.mem_set_of_eq], have := is_some_basis_iff B j, split_ifs; tauto end lemma swap_basis_eq (B : prebasis m n) (r : fin m) (s : fin (n - m)) : (B.swap r s).basis.to_matrix = B.basis.to_matrix ⬝ ((1 : matrix (fin n) (fin n) ℚ) - (single (B.basisg r) (B.basisg r)).to_matrix + (single (B.basisg r) (B.nonbasisg s)).to_matrix) := begin dsimp [prebasis.swap], rw [to_matrix_trans, swap_to_matrix_eq], simp only [matrix.mul_add, sub_eq_add_neg, matrix.mul_neg, matrix.mul_one], simp only [(to_matrix_trans _ _).symm], rw [trans_single_of_eq_none _ (B.basis_nonbasisg_eq_none _), trans_single_of_eq_none _ (B.basis_nonbasisg_eq_none _)], simp end lemma nonbasis_mul_swap_basis_tranpose (B : prebasis m n) (r : fin m) (s : fin (n - m)) : B.nonbasis.trans (B.swap r s).basis.symm = single s r := begin rw [swap, symm_trans_rev, ← equiv.to_pequiv_symm, ← equiv.perm.inv_def, equiv.swap_inv], ext i j, rw [mem_single_iff], dsimp [pequiv.trans, equiv.to_pequiv, equiv.swap_apply_def], simp only [coe, coe_mk_apply, option.mem_def, option.bind_eq_some'], rw [option.mem_def.1 (nonbasisg_mem B i)], simp [B.injective_nonbasisg.eq_iff, (B.basisg_ne_nonbasisg _ _).symm], split_ifs; simp [*, eq_comm] end lemma swap_basis_transpose_apply_single_of_ne (B : prebasis m n) (r : fin m) (s : fin (n - m)) (i : fin m) (hir : i ≠ r) : ((B.swap r s).basis.to_matrixᵀ : matrix (fin n) (fin m) ℚ) ⬝ (single i (0 : fin 1)).to_matrix = B.basis.to_matrixᵀ ⬝ (single i 0).to_matrix := begin simp only [swap_basis_eq, sub_eq_add_neg, matrix.mul_add, matrix.mul_neg, matrix.mul_one, matrix.add_mul, (to_matrix_trans _ _).symm, (to_matrix_symm _).symm, transpose_add, transpose_neg, matrix.neg_mul, symm_trans_rev, trans_assoc], rw [trans_single_of_mem _ (basis_basisg _ _), trans_single_of_eq_none, trans_single_of_eq_none, to_matrix_bot, neg_zero, add_zero, add_zero]; {dsimp [single]; simp [*, B.injective_basisg.eq_iff]} <|> apply_instance end lemma swap_basis_transpose_apply_single (B : prebasis m n) (r : fin m) (s : fin (n - m)) : ((B.swap r s).basis.to_matrixᵀ : matrix (fin n) (fin m) ℚ) ⬝ (single r (0 : fin 1)).to_matrix = B.nonbasis.to_matrixᵀ ⬝ (single s (0 : fin 1)).to_matrix := begin simp only [swap_basis_eq, sub_eq_add_neg, matrix.mul_add, matrix.mul_neg, matrix.mul_one, matrix.add_mul, (to_matrix_trans _ _).symm, (to_matrix_symm _).symm, transpose_add, transpose_neg, matrix.neg_mul, symm_trans_rev, trans_assoc, symm_single], rw [trans_single_of_mem _ (basis_basisg _ _), trans_single_of_mem _ (mem_single _ _), trans_single_of_mem _ (mem_single _ _), trans_single_of_mem _ (nonbasis_nonbasisg _ _)], simp, all_goals {apply_instance} end def pivot_element (A_bar : matrix (fin m) (fin n) ℚ) (B : prebasis m n) (r : fin m) (s : fin (n - m)) : matrix (fin 1) (fin 1) ℚ := (single 0 r).to_matrix ⬝ A_bar ⬝ B.nonbasis.to_matrixᵀ ⬝ (single s 0).to_matrix def swap_inverse (A_bar : matrix (fin m) (fin n) ℚ) (B : prebasis m n) (r : fin m) (s : fin (n - m)) : matrix (fin m) (fin m) ℚ := (1 + (1 - A_bar ⬝ B.nonbasis.to_matrixᵀ ⬝ (single s (0 : fin 1)).to_matrix ⬝ (single (0 : fin 1) r).to_matrix) ⬝ (single r (0 : fin 1)).to_matrix ⬝ inverse (pivot_element A_bar B r s) ⬝ (single (0 : fin 1) r).to_matrix) lemma mul_single_ext {A B : matrix (fin m) (fin n) ℚ} (h : ∀ j, A ⬝ (single j (0 : fin 1)).to_matrix = B ⬝ (single j (0 : fin 1)).to_matrix) : A = B := by ext i j; simpa [matrix_mul_apply] using congr_fun (congr_fun (h j) i) 0 def ratio (A_bar : matrix (fin m) (fin n) ℚ) (b_bar : cvec m) (B : prebasis m n) (r : fin m) (s : fin (n - m)) : matrix (fin 1) (fin 1) ℚ := inverse (pivot_element A_bar B r s) ⬝ ((single 0 r).to_matrix ⬝ b_bar) lemma swap_mul_swap_inverse (A_bar : matrix (fin m) (fin n) ℚ) (B : prebasis m n) (hA_bar : A_bar ⬝ B.basis.to_matrixᵀ = 1) (r : fin m) (s : fin (n - m)) (hpivot : pivot_element A_bar B r s ≠ 0) : swap_inverse A_bar B r s ⬝ (A_bar ⬝ (B.swap r s).basis.to_matrixᵀ) = 1 := have ((single (0 : fin 1) r).to_matrix ⬝ (single r 0).to_matrix : matrix (fin 1) (fin 1) ℚ) = 1, by rw [← to_matrix_trans]; simp, begin refine mul_single_ext (λ i, _), simp only [swap_inverse, matrix.mul_add, matrix.add_mul, sub_eq_add_neg, matrix.neg_mul, matrix.mul_neg, matrix.one_mul, matrix.mul_one, matrix.mul_assoc, transpose_add, transpose_neg, mul_right_eq_of_mul_eq this, pivot_element], rw [pivot_element, matrix.mul_assoc, matrix.mul_assoc] at hpivot, by_cases h : i = r, { simp only [h, swap_basis_transpose_apply_single, mul_right_eq_of_mul_eq hA_bar, one_by_one_inv_mul_cancel hpivot], simp [matrix.mul_one] }, { have : ((single (0 : fin 1) r).to_matrix ⬝ (single i 0).to_matrix : matrix (fin 1) (fin 1) ℚ) = 0, { rw [← to_matrix_trans, single_trans_single_of_ne (ne.symm h), to_matrix_bot] }, simp [swap_basis_transpose_apply_single_of_ne _ _ _ _ h, mul_right_eq_of_mul_eq hA_bar, matrix.one_mul, this, matrix.mul_zero] } end lemma swap_mul_swap_inverse' {A : matrix (fin m) (fin n) ℚ} {B : prebasis m n} {r : fin m} {s : fin (n - m)} (hAB : (A ⬝ B.basis.to_matrixᵀ).has_left_inverse) (h : pivot_element (inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ A) B r s ≠ 0) : swap_inverse (inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ A) B r s ⬝ inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ (A ⬝ (B.swap r s).basis.to_matrixᵀ) = 1 := have inverse (A ⬝ (to_matrix (B.basis))ᵀ) ⬝ A ⬝ (to_matrix (B.basis))ᵀ = 1, by rw [matrix.mul_assoc]; exact matrix.inverse_mul hAB, by rw [← swap_mul_swap_inverse (inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ A) B this r s h]; simp [matrix.mul_assoc] lemma has_left_inverse_swap {A : matrix (fin m) (fin n) ℚ} {B : prebasis m n} {r : fin m} {s : fin (n - m)} (hAB : (A ⬝ B.basis.to_matrixᵀ).has_left_inverse) (h : pivot_element (inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ A) B r s ≠ 0) : (A ⬝ (B.swap r s).basis.to_matrixᵀ).has_left_inverse := ⟨_, swap_mul_swap_inverse' hAB h⟩ lemma inverse_swap_eq {A : matrix (fin m) (fin n) ℚ} {B : prebasis m n} {r : fin m} {s : fin (n - m)} (hAB : (A ⬝ B.basis.to_matrixᵀ).has_left_inverse) (h : pivot_element (inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ A) B r s ≠ 0) : inverse (A ⬝ (B.swap r s).basis.to_matrixᵀ) = swap_inverse (inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ A) B r s ⬝ inverse (A ⬝ B.basis.to_matrixᵀ) := by conv_lhs {rw [← matrix.one_mul (inverse (A ⬝ (B.swap r s).basis.to_matrixᵀ)), ← swap_mul_swap_inverse' hAB h, matrix.mul_assoc, matrix.mul_inverse (has_right_inverse_iff_has_left_inverse.1 (has_left_inverse_swap hAB h)), matrix.mul_one]} local prefix `?` := pequiv.to_matrix lemma cvec_eq_basis_add_nonbasis (x : cvec n) (B : prebasis m n) : x = B.basis.to_matrixᵀ ⬝ B.basis.to_matrix ⬝ x + B.nonbasis.to_matrixᵀ ⬝ B.nonbasis.to_matrix ⬝ x := by conv_lhs {rw [← x.one_mul, ← mul_symm_add_mul_symm B] }; simp [to_matrix_symm, to_matrix_trans, matrix.mul_add, matrix.add_mul, matrix.mul_assoc] def reduced_cost (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (B : prebasis m n) : matrix (fin 1) (fin (n - m)) ℚ := c ⬝ (1 - B.basis.to_matrixᵀ ⬝ (inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ A)) ⬝ B.nonbasis.to_matrixᵀ lemma objective_function_eq (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) (x : cvec n) (B : prebasis m n) (h : A ⬝ x = b) (hAB : (A ⬝ B.basis.to_matrixᵀ).has_left_inverse) : c ⬝ x = c ⬝ B.basis.to_matrixᵀ ⬝ inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ b + reduced_cost A c B ⬝ B.nonbasis.to_matrix ⬝ x := have B.basis.to_matrix ⬝ x = inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ b - inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ A ⬝ B.nonbasis.to_matrixᵀ ⬝ B.nonbasis.to_matrix ⬝ x, begin have := congr_arg ((⬝) (inverse (A ⬝ B.basis.to_matrixᵀ))) h, rw [cvec_eq_basis_add_nonbasis x B, matrix.mul_add, matrix.mul_add, ← A.mul_assoc, ← A.mul_assoc, ← A.mul_assoc, ← A.mul_assoc, ← (inverse (A ⬝ B.basis.to_matrixᵀ)).mul_assoc, ← (inverse (A ⬝ B.basis.to_matrixᵀ)).mul_assoc, inverse_mul hAB, matrix.one_mul, ← eq_sub_iff_add_eq] at this, simp [this, matrix.mul_assoc] end, begin conv_lhs {rw [cvec_eq_basis_add_nonbasis x B, matrix.mul_assoc, this]}, simp [matrix.mul_add, matrix.mul_assoc, matrix.add_mul, reduced_cost, matrix.one_mul], end def objective_function_of_prebasis (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) (B : prebasis m n) : matrix (fin 1) (fin 1) ℚ := c ⬝ (B.basis.to_matrixᵀ ⬝ inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ b) local notation `.` := pequiv.to_matrix local postfix `⁻¹` := inverse @[simp] lemma single_mul_single {k : ℕ} (a : fin m) (b : fin n) (c : fin 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] @[simp] lemma single_mul_single_right {k l : ℕ} (a : fin m) (b : fin n) (c : fin k) (M : matrix (fin k) (fin 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] example (A : matrix (fin m) (fin n) ℚ) (B : prebasis m n) (r : fin m) (s : fin (n - m)) : (single (0 : fin 1) r).to_matrix ⬝ inverse (A ⬝ (B.swap r s).basis.to_matrixᵀ) = inverse (pivot_element (inverse (A ⬝ (?(B.basis))ᵀ) ⬝ A) B r s) ⬝ (single 0 r).to_matrix := begin rw [pivot_element, swap_basis_eq], dsimp, squeeze_simp [matrix.mul_add, matrix.add_mul, matrix.one_mul, matrix.mul_one, sub_eq_add_neg, matrix.mul_neg, matrix.neg_mul, pivot_element, matrix.mul_assoc], end lemma objective_function_swap_eq {A : matrix (fin m) (fin n) ℚ} (c : rvec n) (b : cvec m) {B : prebasis m n} {r : fin m} {s : fin (n - m)} (hAB : (A ⬝ B.basis.to_matrixᵀ).has_left_inverse) (hpivot : pivot_element ((A ⬝ (?(B.basis))ᵀ)⁻¹ ⬝ A) B r s ≠ 0) : objective_function_of_prebasis A c b (B.swap r s) = objective_function_of_prebasis A c b B + reduced_cost A c B ⬝ (single s 0).to_matrix ⬝ ratio (inverse (A ⬝ B.basis.to_matrixᵀ) ⬝ A) b B r s := calc objective_function_of_prebasis A c b (B.swap r s) = c ⬝ (B.swap r s).basis.to_matrixᵀ ⬝ inverse (A ⬝ (B.swap r s).basis.to_matrixᵀ) ⬝ b + (reduced_cost A c _) ⬝ (B.swap r s).nonbasis.to_matrix ⬝ ((B.swap r s).basis.to_matrixᵀ ⬝ -- this line is zero inverse (A ⬝ (B.swap r s).basis.to_matrixᵀ) ⬝ b) : begin rw [objective_function_of_prebasis], refine objective_function_eq A c b _ (B.swap r s) _ (has_left_inverse_swap hAB hpivot), rw [← A.mul_assoc, ← A.mul_assoc, matrix.mul_inverse (has_right_inverse_iff_has_left_inverse.1 (has_left_inverse_swap hAB hpivot)), matrix.one_mul] end ... = c ⬝ (B.swap r s).basis.to_matrixᵀ ⬝ inverse (A ⬝ (B.swap r s).basis.to_matrixᵀ) ⬝ b : have ((B.swap r s).nonbasis.to_matrix ⬝ (B.swap r s).basis.to_matrixᵀ : matrix _ _ ℚ) = 0, by rw [← to_matrix_symm, ← to_matrix_trans, nonbasis_trans_basis_symm_eq_bot, to_matrix_bot], by simp [matrix.mul_assoc, mul_right_eq_of_mul_eq this, matrix.zero_mul, matrix.mul_zero] ... = _ : begin have : (B.nonbasis.to_matrix : matrix _ _ ℚ) ⬝ (swap B r s).basis.to_matrixᵀ = (single s r).to_matrix, { rw [← to_matrix_symm, ← to_matrix_trans, nonbasis_mul_swap_basis_tranpose] }, rw [c.mul_assoc, c.mul_assoc, objective_function_eq A c b _ B _ _], rw [objective_function_of_prebasis, c.mul_assoc, c.mul_assoc, add_left_cancel_iff], simp only [matrix.mul_assoc, mul_right_eq_of_mul_eq this, ratio], refine congr_arg ((⬝) (reduced_cost _ _ _)) _, rw [inverse_swap_eq hAB hpivot, swap_inverse], simp [sub_eq_add_neg, matrix.mul_assoc, matrix.mul_add, matrix.add_mul, matrix.mul_neg, matrix.neg_mul, matrix.one_mul, matrix.mul_one], end -- end prebasis -- def pivot (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) -- (B : array m (fin n)) -- (NB : array (n - m) (fin n)) -- (i : fin (n - m)) (j : fin m) : -- array m (fin n) × -- basis -- array (n - m) (fin n) × -- non basis -- matrix (fin m) (fin (n - m)) ℚ × -- A_bar -- (rvec m) × -- c_B -- (rvec (n - m)) × --c_N -- (cvec m) × --b_bar -- matrix (fin m) (fin m) ℚ -- pert -- := -- let NBi : fin n := NB.read i in -- let Bj : fin n := B.read j in -- let NB' := NB.write i Bj in -- let B' := B.write j NBi in -- let A_B'_inverse := _root_.inverse (minor A id B'.read) in -- ⟨B', -- NB', -- A_B'_inverse ⬝ minor A id NB'.read, -- minor c id B'.read, -- minor c id NB'.read, -- A_B'_inverse ⬝ b, -- A_B'_inverse⟩ -- /-- given a basis, such that A_B is invertible, `solution_of_basis` returns `x` such that `A ⬝ x = b`. -/ -- def solution_of_basis (A : matrix (fin m) (fin n) ℚ) (b : cvec m) (B : prebasis m n) : cvec n := -- minor 1 id B.1.nth ⬝ inverse (minor A id B.1.nth) ⬝ b local notation `£` := default _ @[simp] lemma univ_unique {α : Type*} [unique α] {h : fintype α} : @univ α h = {default α} := finset.ext.2 $ λ x, by simpa using unique.eq_default x lemma solution_of_basis_is_solution (A : matrix (fin m) (fin n) ℚ) (b : cvec m) (B : prebasis m n) (hB : has_right_inverse (minor A id B.1.nth)) : A ⬝ solution_of_basis A b B = b := by rw [solution_of_basis, ← A.mul_assoc, ← A.mul_assoc, ← minor_eq_minor_one_mul A, mul_inverse hB, b.one_mul]; apply_instance /-- function used solely for proof of termination. Never executed -/ def lex_objective_function (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) (B : prebasis m n) : ℚ × rvec m := ((one_by_one_ring_equiv (fin 1) ℚ).to_equiv (minor c id B.1.nth ⬝ inverse (minor A id B.1.nth) ⬝ b), minor c id B.1.nth ⬝ inverse (minor A id B.1.nth)) lemma lex_objective_function_increasing (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) (B : prebasis m n) (i : fin (n - m)) (j : fin m) : lex_objective_function A c b B < lex_objective_function A c b (B.swap i j) := begin dsimp [lex_objective_function], end def simplex [inhabited (fin m)] (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) : Π (B : array m (fin n)) (NB : array (n - m) (fin n)) (A_bar : matrix (fin m) (fin (n - m)) ℚ) (c_B : rvec m) (c_N : rvec (n - m)) (b_bar : cvec m) (pert : (matrix (fin m) (fin m) ℚ)) (pivots : ℕ), ℕ × option (array m (fin n) × -- basis array (n - m) (fin n) × -- non basis matrix (fin m) (fin (n - m)) ℚ × -- A_bar (rvec m) × -- c_B (rvec (n - m)) × --c_N (cvec m) × --b_bar matrix (fin m) (fin m) ℚ) -- pert) | B NB A_bar c_B c_N b_bar pert pivots := let reduced_cost := c_N - c_B ⬝ A_bar in let i' := (list.fin_range (n - m)).find (λ i, 0 < column reduced_cost (fin 1) i) in match i' with | none := (pivots, some (B, NB, A_bar, c_B, c_N, b_bar, pert)) | some i := let ratio_pert : fin m → pert_rat m := λ j : fin m, (A_bar j i)⁻¹ • (b_bar j 0, row' pert (fin 1) j) in let l := (list.fin_range m).filter (λ j, 0 < ratio_pert j) in match l with | [] := (pivots + 1, none) | (a::l) := let j : fin m := list.argmin ratio_pert (a :: l) in let ⟨B', NB', A_bar', c_B', c_N', b_bar', pert'⟩ := pivot A c b B NB i j in have wf : true, from trivial, simplex B' NB' A_bar' c_B' c_N' b_bar' pert' (pivots + 1) end end using_well_founded {rel_tac := λ _ _, `[exact ⟨_, true_well_founded⟩], dec_tac := tactic.assumption} instance read_dec_pred (a : array m (fin n)) (i : fin n) : decidable_pred (λ (k : fin m), array.read a k = i) := by apply_instance def find_optimal_solution_from_starting_basis [inhabited (fin m)] (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) (B : array m (fin n)) : ℕ × option (cvec n) := let NB : array (n - m) (fin n) := vector.to_array ⟨(list.fin_range n).filter (λ i, ¬ B.mem i), sorry⟩ in let A_B := minor A id B.read in let A_N := minor A id NB.read in let pert := _root_.inverse A_B in let A_bar := pert ⬝ A_N in let c_B := minor c id B.read in let c_N := minor c id NB.read in let b_bar := pert ⬝ b in match simplex A c b B NB A_bar c_B c_N b_bar pert 0 with | (pivots, none) := (pivots, none) | (pivots, some ⟨B', NB', A_bar', c_B', c_N', b_bar', pert'⟩) := prod.mk pivots $ some (λ i _, match @fin.find _ (λ k, B'.read k = i) (read_dec_pred _ _) with | none := 0 | some k := b_bar' k 0 end) end /- test code -/ instance {m : ℕ} : inhabited (fin m.succ) := ⟨0⟩ def found_solution_is_feasible [inhabited (fin m)] (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) (B : array m (fin n)) : bool := match find_optimal_solution_from_starting_basis A c b B with | (_, none) := tt | (_, some x) := (A ⬝ x = b ∧ 0 ≤ x : bool) end def is_optimal_bool [inhabited (fin m)] (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) (B : array m (fin n)) : bool := let NB : array (n - m) (fin n) := vector.to_array ⟨(list.fin_range n).filter (λ i, ¬ B.mem i), sorry⟩ in let A_B := minor A id B.read in let A_N := minor A id NB.read in let pert := _root_.inverse A_B in let A_bar := pert ⬝ A_N in let c_B := minor c id B.read in let c_N := minor c id NB.read in let b_bar := pert ⬝ b in match simplex A c b B NB A_N c_B c_N b_bar pert 0 with | (_, none) := tt | (_, some ⟨B', NB', A_bar', c_B', c_N', b_bar', pert'⟩) := c_N' - c_B' ⬝ A_bar' ≤ 0 end def is_feasible_basis [inhabited (fin m)] (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) (B : array m (fin n)) : Prop := let A_B := minor A id B.read in let NB : array (n - m) (fin n) := vector.to_array ⟨(list.fin_range n).filter (λ i, ¬ B.mem i), sorry⟩ in let A_N := minor A id NB.read in let pert := _root_.inverse A_B in let A_bar := pert ⬝ A_N in let c_B := minor c id B.read in let c_N := minor c id NB.read in let b_bar := pert ⬝ b in let x : cvec n := λ i _, match @fin.find _ (λ k, B.read k = i) (read_dec_pred _ _) with | none := 0 | some k := b_bar k 0 end in (0 : cvec n) ≤ x ∧ A ⬝ x = b ∧ det A_B ≠ 0 def finishing_basis [inhabited (fin m)] (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) (B : array m (fin n)) : option (array m (fin n)) := let NB : array (n - m) (fin n) := vector.to_array ⟨(list.fin_range n).filter (λ i, ¬ B.mem i), sorry⟩ in let A_B := minor A id B.read in let A_N := minor A id NB.read in let pert := _root_.inverse A_B in let A_bar := pert ⬝ A_N in let c_B := minor c id B.read in let c_N := minor c id NB.read in let b_bar := pert ⬝ b in option.map prod.fst (simplex A c b B NB A_N c_B c_N b_bar pert 0).2 def test [inhabited (fin m)] (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) (B : array m (fin n)) := let NB : array (n - m) (fin n) := vector.to_array ⟨(list.fin_range n).filter (λ i, ¬ B.mem i), sorry⟩ in let A_B := minor A id B.read in let A_N := minor A id NB.read in let pert := _root_.inverse A_B in let A_bar := pert ⬝ A_N in let c_B := minor c id B.read in let c_N := minor c id NB.read in let b_bar := pert ⬝ b in let reduced_cost := c_N - c_B ⬝ A_bar in let b_pert : fin m → pert_rat m := λ j : fin m, (A_bar j ⟨0, sorry⟩)⁻¹ • (b_bar j 0, row' pert (fin 1) j) in let reduced_cost := c_N - c_B ⬝ A_bar in let i' := (list.fin_range (n - m)).find (λ i, 0 < column reduced_cost (fin 1) i) in match i' with | none := sorry | some i := let ratio_pert : fin m → pert_rat m := λ j : fin m, (A_bar j i)⁻¹ • (b_bar j 0, row' pert (fin 1) j) in let l := (list.fin_range m).filter (λ j, 0 < ratio_pert j) in ratio_pert end --simplex A c b B NB A_N c_B c_N b_bar pert def objective_function_value (c : rvec n) (x : cvec n) := c ⬝ x -- (list.fin_range (n - m)).find (λ i, 0 < column reduced_cost (fin 1) i) -- pivot A c b B NB instance [inhabited (fin m)] (A : matrix (fin m) (fin n) ℚ) (c : rvec n) (b : cvec m) (B : array m (fin n)) : decidable (is_feasible_basis A c b B) := by dunfold is_feasible_basis; apply_instance -- def ex.A := list.to_matrix 3 4 [[12,-1,1,1], -- [1,1.45,1,0], -- [1,2,0,1]] -- def ex.c : rvec 4 := λ _ i, (list.nth_le [1, 1.2, 1, 1.3] i sorry) -- --#eval ex.c -- def ex.b : cvec 3 := (λ i _, (list.nth_le [2, 2, 4] i sorry)) -- --#eval ex.b -- def ex.B : array 3 (fin 4) := list.to_array [0, 1, 3] -- def ex.NB : array 1 (fin 4) := list.to_array [2] -- def ex.A := list.to_matrix 2 5 [[1,1,0,1,0], [0,1,-1,0,1]] -- def ex.b : cvec 2 := (λ i _, (list.nth_le [8,0] i sorry)) -- --#eval ex.b -- def ex.c : rvec 5 := λ _ i, (list.nth_le [1, 1, 1, 0, 0] i sorry) -- --#eval ex.c -- def ex.B : array 2 (fin 5) := list.to_array [3,4] def ex.A := list.to_matrix 3 7 [[1/4, - 8, - 1, 9, 1, 0, 0], [1/2, -12, -1/2, 3, 0, 1, 0], [ 0, 0, 1, 0, 0, 0, 1]] def ex.b : cvec 3 := (λ i _, list.nth_le [0,0,1] i sorry) --#eval ex.b def ex.c : rvec 7 := λ _ i, (list.nth_le [3/4, -20, 1/2, -6, 0, 0, 0] i sorry) --#eval ex.c def ex.B : array 3 (fin 7) := list.to_array [4,5,6] -- #eval (found_solution_is_feasible ex.A ex.c ex.b ex.B) #eval (find_optimal_solution_from_starting_basis ex.A ex.c ex.b ex.B) --set_option trace.fun_info true #eval (is_optimal_bool ex.A ex.c ex.b ex.B) -- (some [[2064/445]]) -- (some [[6401/1895]]) #eval (is_feasible_basis ex.A ex.c ex.b ex.B : bool) -- #eval (show matrix _ _ ℚ, from minor ex.A id ex.B.read) * -- _root_.inverse (show matrix _ _ ℚ, from minor ex.A id ex.B.read ) -- #eval ((1 : matrix (fin 1) (fin 1) ℚ) - (minor ex.c id ex.B.read) ⬝ -- _root_.inverse (minor ex.A id ex.B.read) ⬝ -- (minor ex.A id ex.NB.read)) #eval finishing_basis ex.A ex.c ex.b ex.B #eval test ex.A ex.c ex.b ex.B #eval (let x : cvec 4 := λ i _, list.nth_le [0, 80/89, 62/89, 196/89] i sorry in let A := list.to_matrix 3 4 [[12, -1, 1, 1], [1, 1.45, 1, 0], [1, 2, 0, 1]] in A ⬝ x )
047a13833bafc63498b88f5814e1b0bc3c3d53bd
6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf
/src/game/world3/level8.lean
e9235d69fa682e585b7d71ff57a7ec7834abaeba
[ "Apache-2.0" ]
permissive
arolihas/natural_number_game
4f0c93feefec93b8824b2b96adff8b702b8b43ce
8e4f7b4b42888a3b77429f90cce16292bd288138
refs/heads/master
1,621,872,426,808
1,586,270,467,000
1,586,270,467,000
253,648,466
0
0
null
1,586,219,694,000
1,586,219,694,000
null
UTF-8
Lean
false
false
982
lean
import game.world3.level7 -- hide namespace mynat -- hide /- # Multiplication World ## Level 8: `mul_comm` Finally, the boss level of multiplication world. But (assuming you didn't cheat) you are well-prepared for it -- you have `zero_mul` and `mul_zero`, as well as `succ_mul` and `mul_succ`. After this level you can of course throw away one of each pair if you like, but I would recommend you hold on to them, sometimes it's convenient to have exactly the right tools to do a job. -/ /- Lemma Multiplication is commutative. -/ lemma mul_comm (a b : mynat) : a * b = b * a := begin [nat_num_game] end /- You've now proved that the natural numbers are a commutative semiring! That's the last collectible in Multiplication World. -/ instance mynat.comm_semiring : comm_semiring mynat := by structure_helper /- But don't leave multiplication just yet -- prove `mul_left_comm`, the last level of the world, and then we can beef up the power of `simp`. -/ end mynat -- hide
84fd49cf2cf27d0d5a2e9828d2bef2342c202f52
6c56e3fbe5d7711b66a3b46f1810b44cfa109768
/src/day1.lean
4e088f15c3c41e31b94622e55ba66d690708233c
[]
no_license
kodyvajjha/lean-mmc2021
015e7ba9d2f43397582d42ad282e88c8ce876dc3
cc774208f9dc041cad4def289357301b4a0a8173
refs/heads/master
1,689,416,888,246
1,630,161,054,000
1,630,161,054,000
384,140,257
1
0
null
null
null
null
UTF-8
Lean
false
false
9,496
lean
/- # Propositions as Types. In this file, we shall be introduced to the paradigm of *Propositions as Types* and how they are handled in Lean. We shall also use this oppurtunity to get familiar with Lean's syntax. Acknowledgements : Heavily borrowed from Jalex Stark and Apurva Nakade's tutorial. Import commands go right at the top of a Lean file. -/ import data.nat.pow import data.nat.prime /- Before that, let us delve into type theory. *All* objects in Lean have a *type*. Things which have a type are called *terms*. This simple stratification is more powerful than you may first think. You can query the type of a term using `#check`. Inspect the following commands: -/ #check 1 #check ℕ -- type using \nat #check Type #check Type 1 -- the number indicates a *universe level* #check and #check tt #check ff /- Here is an important type. The type of all propositions. Roughly speaking, a proposition is something which can be subjected to a proof. -/ #check Prop /- In type theory, "a has type A" is written as "a:A". You can read this in two ways: (1) a belongs to the set A (2) a is a proof of A Here are some silly propositions. All of these are terms of type Prop. -/ #check 1 + 1 = 2 -- equality #check ∀ (x : ℕ), x = 10 #check ∃ (k : ℕ), 10 = 2*k #check ∀ (x : bool), x = tt ∨ x = ff #check ∀ (x y : ℕ), x = y → y = x #check ∀ (x y z n : ℕ), n > 2 → (x*y*z ≠ 0) → (x^n + y^n = z^n) #check ∃ k : ℕ, 10 = 2*k -- existential quantification #check ∀ (x : bool), x = tt ∨ x = ff -- disjunction #check ∀ (x : bool), x = tt ∧ x = ff -- conjunction #check ∀ (x y : ℕ), x = y → y = x -- implication #check ¬(1 = 0) -- negation \not -- here is a not so silly proposition -- (XX : State Fermat's Last Theorem/Goldbach) /- As we've said, propositions can be subjected to *(dis)proof*. Here is how this is done in Lean: since every term has a type, and Propositions are types too, we should read their terms as their proofs. This points to (2) above. To repeat ourselves again, we read `p : 1+1 = 2` as `p` is a proof of the proposition that 1+1 = 2. Let us check if our propositions are provable or not. We will try to prove more of our silly props later. XX : First proof, syntax explanation, term mode/tactic mode. -/ theorem one_plus_one_two : 1+1 = 1+1 := begin refl, end theorem ten_is_even : ∃ k m : ℕ, m = 2*k := begin use 5, use 10, refl, end theorem bool_thm : ∀ (x : bool), x = tt ∨ x = ff := begin assume x, cases x, { right, refl, }, { sorry, } end /- # Tactics The terms inside the `begin ... end` block you see here are called *tactics*. As soon as you write out a valid lemma and write a `begin ... end` below it, Lean wakes up and tries to solve the goal you stated, and complains if it can't. You look at it's complaints and help it along, until it complains no more. This is the *interactive* part of interactive theorem proving. The next incantation defines two abstract propositions and adds it to the context henceforth. -/ namespace tactics variables (p q : Prop) /- ``exact`` If ``p`` is the target of the current goal and ``hp`` is a term of type ``p``, then ``exact hp,`` will close the goal. ``intro`` If the target of the current goal is a function ``p → q``, then ``intro hp,`` will produce a hypothesis ``hp : P`` and change the target to ``q``. Delete the ``sorry,`` below and replace them with a legitimate proof. -/ theorem tautology' (hp : p) : p := begin exact hp, end theorem tautology'' : p → p := begin assume hp, exact hp, end example : (p → (q → p)) := begin assume hp, assume hq, exact hp, end -- Can you find two different ways of proving the following? example (p q : Prop) : ((q → p) → (q → p)) := begin sorry, end /- ``have`` If ``f`` is a term of type ``p → q`` and ``hp`` is a term of type ``p``, then ``have hq := f(hp),`` creates the hypothesis ``hq : q`` . ``apply`` If the target of the current goal is ``q`` and ``f`` is a term of type ``p → q``, then ``apply f,`` changes target to ``p``. Delete the ``sorry,`` below and replace them with a legitimate proof. -/ example (p q r : Prop) (hp : p) (f : p → q) (g : q → r) : r := begin have hq : q, { exact (f hp), }, exact (g hq), end example (p q r : Prop) (hp : p) (f : p → q) (g : q → r) : r := begin apply g, apply f, exact hp, end theorem quux (P Q R S T U: Type) (hpq : P → Q) (hqr : Q → R) (hrs : R → S) (hst : S → T) (htu : T → U) : P → U := begin assume hp, apply htu, apply hst, apply hrs, apply hqr, apply hpq, exact hp, end #print quux /- ``cases`` ``cases`` is a general tactic that breaks up complicated terms. If ``hpq`` is a term of type ``P ∧ Q`` or ``P ∨ Q`` or ``P ↔ Q``, then use ``cases hpq with hp hq,``. ``split`` If the target of the current goal is ``P ∧ Q`` or ``P ↔ Q``, then use ``split,``. ``left``/``right`` If the target of the current goal is ``P ∨ Q``, then use either ``left,`` or ``right,`` (choose wisely). ``exfalso`` Changes the target of the current goal to ``false``. Delete the ``sorry,`` below and replace them with a legitimate proof. -/ example (P Q : Prop) : P ∧ Q → Q ∧ P := begin assume hpq, split, { cases hpq with hp hq, exact hq, }, { cases hpq with hp hq, exact hp, } end example (P Q : Prop) : P ∧ Q → Q ∧ P := begin assume hpq, -- this is a comment in Lean split, all_goals { cases hpq with hp hq, assumption, }, end /- This is another comment. -/ example (P Q : Prop) : P ∨ Q → Q ∨ P := begin assume hpq, cases hpq, { right, exact hpq, }, { left, exact hpq, } end #check @false.elim example (P Q R : Prop) : P ∧ false ↔ false := begin split, { assume hpf, cases hpf with hp hf, exact hf, }, { assume hf, exfalso, exact hf, } end theorem principle_of_explosion (P Q : Prop) : P ∧ ¬ P → Q := begin sorry, end /- ``exfalso`` Changes the target of the current goal to ``false``. ``push_neg`` ``push_neg,`` simplifies negations in the target. You can push negations across a hypothesis ``hp : P`` using ``push_neg at hp,``. ``contrapose!`` If the target of the current goal is ``P → Q``, then ``contrapose!,`` changes the target to ``¬ Q → ¬ P``. If the target of the current goal is ``Q`` and one of the hypotheses is ``hp : P``, then ``contrapose! hp,`` changes the target to ``¬ P`` and changes the hypothesis to ``hp : ¬ Q``. Delete the ``sorry,`` below and replace them with a legitimate proof. -/ theorem not_not_self_imp_self (P : Prop) : ¬ ¬ P → P:= begin sorry, end theorem contrapositive_converse (P Q : Prop) : (¬Q → ¬P) → (P → Q) := begin sorry, end example (P : Prop) : ¬ P → ¬ ¬ ¬ P := begin sorry, end theorem principle_of_explosion' (P Q : Prop) : P → (¬ P → Q) := begin sorry, end end tactics /- Using the tactics above, let's try to prove some of the silly propositions we wrote out before: ∀ (x : ℕ), x = 10 -- universal quantification ∃ k : ℕ, 10 = 2*k -- existential quantification ∀ (x : bool), x = tt ∨ x = ff -- disjunction ∀ (x : bool), x = tt ∧ x = ff -- conjunction ∀ (x y : ℕ), x = y → y = x -- implication ¬(1 = 0) -- negation -/ example : ∀ (x : bool), x = tt ∨ x = ff := begin intros x, -- pick an arbitrary boolean x cases x, -- prove the goal two cases: when x is true and when x is false. { -- we are in the false branch now right, refl, }, { -- we are in the true branch now left, refl, } end /- Lemmas and theorems can be named so that they can be referred to in future proofs. -/ lemma ten_even : ∃ k : ℕ, 10 = 2*k := begin use 5, refl, end #check ten_even -- ten_even is a proof that `∃ k : ℕ, 10 = 2*k` #check ∃ k : ℕ, 10 = 2*k -- which is a proposition /- Prove these: -/ -- Use rw. lemma eq_symm : ∀ (x y : ℕ), x = y → y = x := begin assume x y Hxy, rewrite Hxy, end -- XX: Show what proving a negation means. Use injection. lemma one_ne_zero' : ¬(1 = 0) := begin assume onezero, injection onezero, end /- # Exercises Using the tactics from above, try to prove as many of the following as you can: -/ namespace exercises variables p q r : Prop -- commutativity of ∧ and ∨ example : p ∧ q ↔ q ∧ p := sorry example : p ∨ q ↔ q ∨ p := sorry -- associativity of ∧ and ∨ example : (p ∧ q) ∧ r ↔ p ∧ (q ∧ r) := sorry example : (p ∨ q) ∨ r ↔ p ∨ (q ∨ r) := sorry -- distributivity example : p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) := sorry example : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (p ∨ r) := sorry -- other properties example : (p → (q → r)) ↔ (p ∧ q → r) := sorry example : ((p ∨ q) → r) ↔ (p → r) ∧ (q → r) := sorry example : ¬(p ∨ q) ↔ ¬p ∧ ¬q := sorry example : ¬p ∨ ¬q → ¬(p ∧ q) := sorry example : ¬(p ∧ ¬p) := sorry example : p ∧ ¬q → ¬(p → q) := sorry example : ¬p → (p → q) := sorry example : (¬p ∨ q) → (p → q) := sorry example : p ∨ false ↔ p := sorry example : p ∧ false ↔ false := sorry example : (p → q) → (¬q → ¬p) := sorry end exercises
1880249437e0c26f9e196d153ad19ad149c9de14
3f1a1d97c03bb24b55a1b9969bb4b3c619491d5a
/library/data/rbtree/find.lean
1ab55ad83d4e23806f283faab6bd0c1060d51125
[ "Apache-2.0" ]
permissive
praveenmunagapati/lean
00c3b4496cef8e758396005013b9776bb82c4f56
fc760f57d20e0a486d14bc8a08d89147b60f530c
refs/heads/master
1,630,692,342,183
1,515,626,222,000
1,515,626,222,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,124
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import data.rbtree.basic universe u namespace rbnode variables {α : Type u} @[elab_simple] lemma find.induction {p : rbnode α → Prop} (lt) [decidable_rel lt] (t x) (h₁ : p leaf) (h₂ : ∀ l y r (h : cmp_using lt x y = ordering.lt) (ih : p l), p (red_node l y r)) (h₃ : ∀ l y r (h : cmp_using lt x y = ordering.eq), p (red_node l y r)) (h₄ : ∀ l y r (h : cmp_using lt x y = ordering.gt) (ih : p r), p (red_node l y r)) (h₅ : ∀ l y r (h : cmp_using lt x y = ordering.lt) (ih : p l), p (black_node l y r)) (h₆ : ∀ l y r (h : cmp_using lt x y = ordering.eq), p (black_node l y r)) (h₇ : ∀ l y r (h : cmp_using lt x y = ordering.gt) (ih : p r), p (black_node l y r)) : p t := begin induction t, case leaf { assumption }, case red_node : l y r { cases h : cmp_using lt x y, case ordering.lt { apply h₂, assumption, assumption }, case ordering.eq { apply h₃, assumption }, case ordering.gt { apply h₄, assumption, assumption }, }, case black_node : l y r { cases h : cmp_using lt x y, case ordering.lt { apply h₅, assumption, assumption }, case ordering.eq { apply h₆, assumption }, case ordering.gt { apply h₇, assumption, assumption }, } end lemma find_correct {t : rbnode α} {lt x} [decidable_rel lt] [is_strict_weak_order α lt] : ∀ {lo hi} (hs : is_searchable lt t lo hi), mem lt x t ↔ ∃ y, find lt t x = some y ∧ x ≈[lt] y := begin apply find.induction lt t x; intros; simp only [mem, find, *], { simp, intro h, cases h with _ h, cases h, contradiction }, iterate 2 { -- red and black cases are identical { cases hs, apply iff.intro, { intro hm, blast_disjs, { exact iff.mp (ih hs_hs₁) hm }, { simp at h, cases hm, contradiction }, { have hyx : lift lt (some y) (some x) := (range hs_hs₂ hm).1, simp [lift] at hyx, have hxy : lt x y, { simp [cmp_using] at h, assumption }, exact absurd (trans_of lt hxy hyx) (irrefl_of lt x) } }, { intro hc, left, exact iff.mpr (ih hs_hs₁) hc }, }, { simp at h, simp [h, strict_weak_order.equiv], existsi y, split, refl, assumption }, { cases hs, apply iff.intro, { intro hm, blast_disjs, { have hxy : lift lt (some x) (some y) := (range hs_hs₁ hm).2, simp [lift] at hxy, have hyx : lt y x, { simp [cmp_using] at h, exact h.2 }, exact absurd (trans_of lt hxy hyx) (irrefl_of lt x) }, { simp at h, cases hm, contradiction }, { exact iff.mp (ih hs_hs₂) hm } }, { intro hc, right, right, exact iff.mpr (ih hs_hs₂) hc }, } } end lemma mem_of_mem_exact {lt} [is_irrefl α lt] {x t} : mem_exact x t → mem lt x t := begin induction t; simp [mem_exact, mem]; intro h, all_goals { blast_disjs, simp [t_ih_lchild h], simp [h, irrefl_of lt t_val], simp [t_ih_rchild h] } end lemma find_correct_exact {t : rbnode α} {lt x} [decidable_rel lt] [is_strict_weak_order α lt] : ∀ {lo hi} (hs : is_searchable lt t lo hi), mem_exact x t ↔ find lt t x = some x := begin apply find.induction lt t x; intros; simp only [mem_exact, find, *], { simp, intro h, contradiction }, iterate 2 { { cases hs, apply iff.intro, { intro hm, blast_disjs, { exact iff.mp (ih hs_hs₁) hm }, { simp at h, subst x, exact absurd h (irrefl y) }, { have hyx : lift lt (some y) (some x) := (range hs_hs₂ (mem_of_mem_exact hm)).1, simp [lift] at hyx, have hxy : lt x y, { simp [cmp_using] at h, assumption }, exact absurd (trans_of lt hxy hyx) (irrefl_of lt x) } }, { intro hc, left, exact iff.mpr (ih hs_hs₁) hc }, }, { simp at h, cases hs, apply iff.intro, { intro hm, blast_disjs, { have hxy : lift lt (some x) (some y) := (range hs_hs₁ (mem_of_mem_exact hm)).2, simp [lift] at hxy, exact absurd hxy h.1 }, { subst hm }, { have hyx : lift lt (some y) (some x) := (range hs_hs₂ (mem_of_mem_exact hm)).1, simp [lift] at hyx, exact absurd hyx h.2 } }, { intro hm, injection hm, simp [*] } }, { cases hs, apply iff.intro, { intro hm, blast_disjs, { have hxy : lift lt (some x) (some y) := (range hs_hs₁ (mem_of_mem_exact hm)).2, simp [lift] at hxy, have hyx : lt y x, { simp [cmp_using] at h, exact h.2 }, exact absurd (trans_of lt hxy hyx) (irrefl_of lt x) }, { simp at h, subst x, exact absurd h (irrefl y) }, { exact iff.mp (ih hs_hs₂) hm } }, { intro hc, right, right, exact iff.mpr (ih hs_hs₂) hc } } } end lemma eqv_of_find_some {t : rbnode α} {lt x y} [decidable_rel lt] [is_strict_weak_order α lt] : ∀ {lo hi} (hs : is_searchable lt t lo hi) (he : find lt t x = some y), x ≈[lt] y := begin apply find.induction lt t x; intros; simp only [mem, find, *] at *, { contradiction }, iterate 2 { { cases hs, exact ih hs_hs₁ rfl }, { injection he, subst y, simp at h, exact h }, { cases hs, exact ih hs_hs₂ rfl } } end lemma find_eq_find_of_eqv {lt a b} [decidable_rel lt] [is_strict_weak_order α lt] {t : rbnode α} : ∀ {lo hi} (hs : is_searchable lt t lo hi) (heqv : a ≈[lt] b), find lt t a = find lt t b := begin apply find.induction lt t a; intros; simp [mem, find, strict_weak_order.equiv, *] at *, iterate 2 { { have : lt b y := lt_of_incomp_of_lt heqv.swap h, simp [cmp_using, find, *], cases hs, apply ih hs_hs₁ }, { have := incomp_trans_of lt heqv.swap h, simp [cmp_using, find, *] }, { have := lt_of_lt_of_incomp h heqv, have := not_lt_of_lt this, simp [cmp_using, find, *], cases hs, apply ih hs_hs₂ } } end end rbnode
a60633cc616a24d2d1a46db44fe6a165e407ec4f
fecda8e6b848337561d6467a1e30cf23176d6ad0
/test/tauto.lean
44a371647910d2cf42a032e4367184a247c8fe92
[ "Apache-2.0" ]
permissive
spolu/mathlib
bacf18c3d2a561d00ecdc9413187729dd1f705ed
480c92cdfe1cf3c2d083abded87e82162e8814f4
refs/heads/master
1,671,684,094,325
1,600,736,045,000
1,600,736,045,000
297,564,749
1
0
null
1,600,758,368,000
1,600,758,367,000
null
UTF-8
Lean
false
false
3,025
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic.tauto import tactic section tauto₀ variables p q r : Prop variables h : p ∧ q ∨ p ∧ r include h example : p ∧ p := by tauto end tauto₀ section tauto₁ variables α : Type variables p q r : α → Prop variables h : (∃ x, p x ∧ q x) ∨ (∃ x, p x ∧ r x) include h example : ∃ x, p x := by tauto end tauto₁ section tauto₂ variables α : Type variables x : α variables p q r : α → Prop variables h₀ : (∀ x, p x → q x → r x) ∨ r x variables h₁ : p x variables h₂ : q x include h₀ h₁ h₂ example : ∃ x, r x := by tauto end tauto₂ section tauto₃ example (p : Prop) : p ∧ true ↔ p := by tauto example (p : Prop) : p ∨ false ↔ p := by tauto example (p q r : Prop) [decidable p] [decidable r] : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (r ∨ p ∨ r) := by tauto example (p q r : Prop) [decidable q] [decidable r] : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (r ∨ p ∨ r) := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : ¬ p) : q := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : p) : ¬ q := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : q) : ¬ p := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : ¬ q) : p := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : ¬ q) (h'' : ¬ p) : false := by tauto example (p q r : Prop) [decidable q] [decidable p] (h : p ↔ q) (h' : r ↔ q) (h'' : ¬ r) : ¬ p := by tauto example (p q r : Prop) (h : p ↔ q) (h' : r ↔ q) : p ↔ r := by tauto! example (p q r : Prop) (h : ¬ p = q) (h' : r = q) : p ↔ ¬ r := by tauto! example (p : Prop) : p → ¬ (p → ¬ p) := by tauto example (p : Prop) (em : p ∨ ¬ p) : ¬ (p ↔ ¬ p) := by tauto example (P : ℕ → Prop) (n : ℕ) : P n → n = 7 ∨ n = 0 ∨ ¬ (n = 7 ∨ n = 0) ∧ P n := by tauto section modulo_symmetry variables {p q r : Prop} {α : Type} {x y : α} variables (h : x = y) variables (h'' : (p ∧ q ↔ q ∨ r) ↔ (r ∧ p ↔ r ∨ q)) include h include h'' example (h' : ¬ y = x) : p ∧ q := by tauto example (h' : p ∧ ¬ y = x) : p ∧ q := by tauto example : y = x := by tauto example (h' : ¬ x = y) : p ∧ q := by tauto example : x = y := by tauto end modulo_symmetry end tauto₃ section closer example {α : Type*} {β : Type*} (a : α) {s_1 : set α} : (∃ (a_1 : α), a_1 = a ∨ a_1 ∈ s_1) := begin tauto {closer := `[simp]} end variables {p q r : Prop} {α : Type} {x y z w : α} variables (h : x = y) (h₁ : y = z) (h₂ : z = w) variables (h'' : (p ∧ q ↔ q ∨ r) ↔ (r ∧ p ↔ r ∨ q)) include h h₁ h₂ h'' example : (((r ∧ p ↔ r ∨ q) ∧ (q ∨ r)) → (p ∧ (x = w) ∧ (¬ x = w → p ∧ q ∧ r))) := begin tauto {closer := `[cc]} end end closer
f5ca4299422312c02429a2b8c26888f478bd3dd3
d0f9af2b0ace5ce352570d61b09019c8ef4a3b96
/exam_2/predicate_logic/proofs.lean
5d2acd53c12332d93d635ba4ba610b9f2fc7dedd
[]
no_license
jngo13/Discrete-Mathematics
8671540ef2da7c75915d32332dd20c02f001474e
bf674a866e61f60e6e6d128df85fa73819091787
refs/heads/master
1,675,615,657,924
1,609,142,011,000
1,609,142,011,000
267,190,341
0
0
null
null
null
null
UTF-8
Lean
false
false
14,130
lean
/- Today we will see in that proofs can be understood as formal objects in their own right. We will start to make our way through the valid rules of inference from our unit on propositional logic. We will emphasize that they are rules for combining and deriving Boolean truth values. In predicate logic, by contrast, we will reinterpret them as rules for combining and deriving *proofs*. In predicate logic, the existence of a proof is our new basis for deciding whether or not a proposition can be judged to be true. In particular, we will look at how to (1) *construct* and (2) *use* given or assumed proofs of two simple forms of propositions, namely conjunctions and disjunctions. We will see that we can view proofs as *computational* objects. In particular, we will see that we can view a proof of a conjunction, P ∧ Q, as a *pair* of proofs (a proof of P *and* a proof of Q), and thus as a value of a *product* type. We will then view a proof of a disjunction, P ∨ Q, as *either* an object constructed from a proof of P *or* an object built from a proof of Q, and thus a value of a *sum* type. Understanding proof construction and manipulation as computations involving logical types (propositions) and values (proofs) will give you the precise understanding of deductive reasoning in predicate logic that you need to handle a very wide variety of "prove it" problems in the years to come, whether or not (and more likely not) you use an automated proof assistant such as Lean or its formalized proofs. -/ /- To begin we will review our polymorphic product (pair) type. We will then see that the ∧ (and) connective in predicate logic can be understood and formalized as a completely analogous polymorphic *logical* type. It's one costructor implements the and introduction rule, and its two projection functions implement the two and elimination rules. -/ #check and namespace hidden -- review -- prod abstract data type! -- be sure you fully understand this type definition inductive prod (α β : Type) : Type | mk (a : α) (b : β) : prod -- here's a named example of a value of this type def pair1 := prod.mk 1 1 #reduce pair1 -- by the way, we can use "example" for unnamed values example : prod nat nat := prod.mk 1 1 -- the first, or left, projection function -- implemented by pattern matching (aka elimination!) def fst {α β : Type} : prod α β → α | (prod.mk a b) := a -- the second, or right, projection function def snd {α β : Type} : prod α β → β | (prod.mk a b) := b -- and a function that from one pair derives its swap def swap {α β : Type} : prod α β → prod β α | (prod.mk a b) := prod.mk b a /- Our implementation of the and connective and its rules of inference (introduction and elimination rules) in exactly the same way, except that our and polymorphic pair type now lives in "Prop," the unverse of logical types (propositions), rather than in "Type", which as we know is the universe of computational types. As a reminder from propositional logic, here are three rules of reasoning that we showed to be semantically valid. def and_intro := P >> Q >> P ∧ Q def and_elim_left := P ∧ Q >> P def and_elim_right := P ∧ Q >> Q In propositional logic, we read these rules as involving truth values: e.g., if P "is true" and Q "is true" then "P ∧ Q" "is true". We now reconceptualize these rules to involve proofs. E.g., If we have (or assume we have) a proof, p, of P, and we have (or assume we have) a proof, q, of Q, then we can construct a proof, ⟨p, q⟩, of P ∧ Q. That's the and introduction rule. Similarly, if we have a proof (pair!), ⟨p, q⟩, then from it we can derive a proof, p, of P, and a proof, q, of Q, by nothing more complex than projection: we destructure the pair and return one of the other other of its two components. -/ structure and (P Q : Prop) : Prop := -- Prop not Type! intro :: (left : P) (right : Q) -- and.intro rule -- one poassible way to write left elimination rule def and_elim_left {P Q : Prop} : and P Q → P | (and.intro p q) := p -- here's another, with elim_left in the "and" namespace -- note that we use projection function from "structure" def and.elim_left {P Q : Prop} (pq : and P Q) : P := pq.left -- and here is the right elimination rule in two forms def and_elim_right {P Q : Prop} : and P Q → Q | (and.intro p q) := q def and.elim_right {P Q : Prop} (pq : and P Q) : Q := pq.right /- A note on notation. The Lean libraries define the and connective exactly as we've done here. In addition, the Lean library defined ∧ as an infix notation for "and". We won't define that notation here, so wherever we want to use the and connective, e.g., for P ∧ Q, we'll have to write "and P Q". Same with or. -/ -- tests def pf1 : and (1=1) (eq 0 0) := -- 1=1 and 0=0 and.intro (eq.refl 1) (eq.refl 0) -- proof of it! -- and.intro is a proof constructor -- We now see that pf1 is basically a pair of proofs #reduce pf1 -- proof, left element 1=1, right element 0=0 /- OR We also formalize the logical connective, ∨, as an inductive type with two (logical) type arguments, P and Q (two propositions). The both ∧ and ∨ take two propositions (logical types) as arguments and yield a larger proposition (logical type). We then define constructors to implement the introduction rules for the given connective. To build a proof of P ∧ Q, we need proofs of both P and of Q. To build a proof of P ∨ Q it suffices to have either a proof of P or a proof of Q. Here are the propositional logic rules we validated in the last section. def or_intro_left := P >> P ∨ Q def or_intro_right := Q >> P ∨ Q def or_elim := P ∨ Q >> (P >> R) >> (Q >> R) >> R We now reconceptualize these rules are rules about how *proofs* can be built and derived. -/ #check or inductive or (P Q : Prop) : Prop | inl {} (p : P) : or -- Q is implicit -- if I have a proof of P I can build a proof of P or Q | inr {} (q : Q) : or -- P is implicit -- inl/ inr are just names of constructors (intro left/ intro right) def pf2 : or (eq 0 0) (eq 1 0) := or.inl (eq.refl 0) -- example, proof of 0=0 ∨ 1=0 example : or (eq 0 0) (eq 1 0) := or.inl (eq.refl 0) #check @or.elim -- @ turns off implicit types example : or (eq 1 0) (eq 0 0) := or.inr (eq.refl 0) /- Prove that 1=1 and 2=2. Q: What's the form of this proposition? A: Conjunction. Main connective is and. Q: What rule of reasoning apply? A: The "and" introduction and elimination rules. Q: What is the form of the overall proof? A: and.intro p q, where p is a proof of P and q is a proof of Q. Q: So what remains to be done? A: It will now suffice to produce a proof of 1=1 and one of 2=2. Q: How to prove 1=1? A: By the reflexive property of equality. Q: How to prove 2 =2. A: Same way. QED! -/ -- Here it is formally example : and (1=1) (2=2) := and.intro (eq.refl 1) (eq.refl 2) /- The following versions of the introductions rules take two explicit arguments each: a *proposition* for which a proof is *not* given and a proof of the other proposition. Notice carefully the change in which type argument is implicit in each case. Sometimes Lean can't infer from, say, a proof, p, of P, what disjunction, P ∨ Q, is being proved (because it can't figure out what Q is). In such cases, you need to provide the Q type explicitly. These functions are useful in such cases. -/ def or.intro_left {P : Prop} (Q : Prop) (p : P) : or P Q := or.inl p def or.intro_right (P : Prop) {Q : Prop} (q : Q) : or P Q := or.inr q /- Prove 1=0 or 1=1. Proof: We apply the or introduction on the right rule to a proof of 1=1. Now all that remains is to to that 1=1. This is by applying the reflexive property of equality (to the value, 1). -/ -- Here is this proof formalized example : or (1=0) (1=1) := or.inr (eq.refl 1) example : or (1=0) (1=1) := or.intro_right (1=0) (eq.refl 1) def x : ℕ := 1 example : ℕ := 1 /- NEXT UP: ∀, → -/ /- ************************************************************ Universal generalizations. Propositions starting with forall. ************************************************************ -/ /- Introduction rule: To prove "∀ (p : P), Q" show that if you *assume* you're given an arbitrary but specific p, you can construct a proof of Q. This is the ∀ introduction rule of natural deduction. -/ example : ∀ (n : ℕ), or (n = 0) (n ≠ 0) := λ (n : ℕ), match n with | nat.zero := or.inl (eq.refl 0) | (nat.succ _) := _ -- Homework end /- Proof: We start by assuming that we're given an arbitrary but specific natural number, n. Now in this context, all that remains to be proved is that n = 0 or n ≠ 0. -/ /- Prove that for any propositions, P and Q, P ∧ Q → P ∨ Q -/ def aProp := ∀ (P Q : Prop), and P Q → or P Q /- We start by assuming that P and Q are arbitrary but specific propositions. In this context, what remains to be proved is the following implication: and P Q → or P Q. To prove this is to prove an implication. We do this in the same way we prove a ∀: by assuming that we're given a proof of the premise, P, and showing that, in that context, we can construct a proof of the conclusion, Q. -/ lemma and_imp_or_1 : aProp := λ (P Q : Prop), λ (pq : and P Q), or.inl (and.elim_left pq) lemma and_imp_or_2 : aProp := λ (P Q : Prop), λ (pq : and P Q), or.inr (and.elim_right pq) example : and_imp_or_1 = and_imp_or_2 := eq.refl and_imp_or_2 example : ∀ (P Q : Prop), and P Q → and Q P := λ (P Q : Prop), λ (pq : and P Q), and.intro (pq.right) (pq.left) /- Prove that the "and" connective is commutative. -/ theorem and_commutes : ∀ {P Q : Prop}, and P Q → and Q P := λ (P Q : Prop), λ (pq : and P Q), and.intro (pq.right) (pq.left) /- Assume that P and Q are arbitary but specific propositions. We are to show that P ∧ Q → Q ∧ P. Suppose we have a proof, pq, of P ∧ Q. In this context we need to prove that we can construct a proof of Q and P. We do this by applying the and introduction rule to a proof of P and to a proof of Q. What remains to be proven is that there is a proof of P and a proof of Q. But we can get these by applying the left and right and eliminations rules to our proof, pq, of P ∧ Q. -/ /- If a proof of a ∀ or → proposition is a function in Lean, can we apply these functions to arguments to get results? The answer is yes, absolutely, and this idea is in fact the *elimination* rule for ∀ and →. If you're given a proof, pf, of either ∀ (p : P), Q, or of P → Q, which are in fact equivalent(!), then you can apply pf to a proof or value, p : P, to obtain a corresponding value/proof of Q. As an example, let's apply our proof that and is commutative -/ lemma oneeq1_and_2eq2 : and (1=1) (2=2) := and.intro (eq.refl 1) (eq.refl 2) #reduce oneeq1_and_2eq2 -- introduction rule used to build proof of bigger prop -- elim rule is a rule for using a bigger proof to break -- it up and give you pieces /- We now use the elimination rule for ∀/→ by *applying* the *general* proof of commutative of and to a *specific* proof of (and 1=1 2=2) to obtain a specific proof of (and 2=2 1=1)! -/ #reduce and_commutes oneeq1_and_2eq2 /- The *elimination* rule for ∀ all and implication is "apply!" -/ /- This principal is seen very clearly in the proof of the rule of reasoning that Aristotle called "modus ponens." It states that if, for any propositions P and Q, you know that P → Q is true and you also know that P is true then you can conclude that Q is true. If when it's raining the streets are wet (P → Q) and it's raining (P) then it must be the case that the streets are wet (Q). We now prove that this is a valid form of reasoning. -/ theorem arrow_elim : ∀ {P Q : Prop}, (P → Q) → P → Q := λ (P Q : Prop), λ (p2q : P → Q), λ (p : P), p2q p ---<<< apply proof of P→Q to proof of P! /--- TODAY ---/ /- Propositions that use ¬ -/ /- Suppose you want to prove ¬P. We have to show that there's no proof of P. Key strategy: Proof by negation. Assume that P is true, and show that this assumption leads to a contradiction. Equivalent to a proof of false. So the idea is this: assume that there is a proof of P and show that this enables you to construct a proof of false. ¬ P ==== P → false -/ example : 0 ≠ 1 := /- the same as ¬ (0 = 1) -/ /- (0 = 1) → false -/ λ (h : 0 = 1), match h with /- NO CASES! -/ end -- modus tollens theorem mt : ∀ {P Q}, (P → Q) → (¬Q → ¬P) := λ P Q, λ (h : P → Q), λ (nq : ¬Q), λ (p : P), nq (h p) theorem non_contradiction: ∀ (P : Prop), ¬ (P ∧ ¬ P) := λ P, -- forall introduction λ (h : P ∧ ¬P), -- proof by negation let p := (h.left) in -- and.elim_left let np := (h.right) in -- and.elim_right (np p) theorem zornz : ∀ (n : ℕ), or (n = 0) (n ≠ 0) := λ (n : ℕ), match n with | nat.zero := or.inl (eq.refl 0) | (nat.succ n') := or.inr _ -- complete this proof end /- Propositions that use ∃ -/ example : ∃ n, n = 0 := exists.intro 0 (eq.refl 0) example : ∃ n, n^2 = 25 := exists.intro 5 rfl -- rfl short for eq.refl 25 example : ∃ x : nat, ∃ y: nat, ∃ z : ℕ, x^2 +y^2 = z^2 := exists.intro 3 (exists.intro 4 (exists.intro 5 (rfl))) /- Propositions that use both ∃ and ∀ -/ /- Still to do: def iff_intro := (P >> Q) >> (Q >> P) >> (P ↔ Q) def iff_intro' := (P >> Q) ∧ (Q >> P) >> (P ↔ Q) def iff_elim_left := (P ↔ Q) >> (P >> Q) def iff_elim_right := (P ↔ Q) >> (Q >> P) def syllogism := (P >> Q) >> (Q >> R) >> (P >> R) def modus_tollens := (P >> Q) >> (¬ Q >> ¬ P) def neg_elim := (¬ ¬ P) >> P -- not a constructive rule def excluded_middle := P ∨ (¬ P) -- not a constructive rule def neg_intro := (P >> pFalse) >> (¬ P) def true_intro : pExp := pTrue def false_elim := pFalse >> P -/ end hidden
bfacce3274a6cea6c957700e95061288c87f8faf
17d3c61bf162bf88be633867ed4cb201378a8769
/library/init/meta/congr_lemma.lean
d2445a316d24a41d2e0120cdea7f5bb6b7ad7278
[ "Apache-2.0" ]
permissive
u20024804/lean
11def01468fb4796fb0da76015855adceac7e311
d315e424ff17faf6fe096a0a1407b70193009726
refs/heads/master
1,611,388,567,561
1,485,836,506,000
1,485,836,625,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,722
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 inductive congr_arg_kind /- It is a parameter for the congruence lemma, the parameter occurs in the left and right hand sides. -/ | fixed /- 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. -/ | fixed_no_param /- 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. -/ | eq /- congr-simp lemma contains only one parameter for this kind of argument, and congr-lemmas contains two. They correspond to arguments that are subsingletons/propositions. -/ | cast /- 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. -/ | heq meta structure congr_lemma := (type : expr) (proof : expr) (arg_kinds : list congr_arg_kind) namespace tactic meta constant mk_congr_lemma_simp_core : transparency → expr → tactic congr_lemma meta constant mk_congr_lemma_simp_n_core : transparency → expr → nat → tactic congr_lemma /- Create a specialized theorem using (a prefix of) the arguments of the given application. -/ meta constant mk_specialized_congr_lemma_simp_core : transparency → expr → tactic congr_lemma meta constant mk_congr_lemma_core : transparency → expr → tactic congr_lemma meta constant mk_congr_lemma_n_core : transparency → expr → nat → tactic congr_lemma /- Create a specialized theorem using (a prefix of) the arguments of the given application. -/ meta constant mk_specialized_congr_lemma_core : transparency → expr → tactic congr_lemma meta constant mk_hcongr_lemma_core : transparency → expr → tactic congr_lemma meta constant mk_hcongr_lemma_n_core : transparency → expr → nat → tactic congr_lemma /- If R is an equivalence relation, construct the congruence lemma R a1 a2 -> R b1 b2 -> (R a1 b1) <-> (R a2 b2) -/ meta constant mk_rel_iff_congr_lemma_core : transparency → expr → tactic congr_lemma /- Similar to mk_rel_iff_congr It fails if propext is not available. R a1 a2 -> R b1 b2 -> (R a1 b1) = (R a2 b2) -/ meta constant mk_rel_eq_congr_lemma_core : transparency → expr → tactic congr_lemma meta def mk_congr_lemma_simp : expr → tactic congr_lemma := mk_congr_lemma_simp_core semireducible meta def mk_congr_lemma_simp_n : expr → nat → tactic congr_lemma := mk_congr_lemma_simp_n_core semireducible meta def mk_specialized_congr_lemma_simp : expr → tactic congr_lemma := mk_specialized_congr_lemma_simp_core semireducible meta def mk_congr_lemma : expr → tactic congr_lemma := mk_congr_lemma_core semireducible meta def mk_congr_lemma_n : expr → nat → tactic congr_lemma := mk_congr_lemma_n_core semireducible meta def mk_specialized_congr_lemma : expr → tactic congr_lemma := mk_specialized_congr_lemma_core semireducible meta def mk_hcongr_lemma : expr → tactic congr_lemma := mk_hcongr_lemma_core semireducible meta def mk_hcongr_lemma_n : expr → nat → tactic congr_lemma := mk_hcongr_lemma_n_core semireducible meta def mk_rel_iff_congr_lemma : expr → tactic congr_lemma := mk_rel_iff_congr_lemma_core semireducible meta def mk_rel_eq_congr_lemma : expr → tactic congr_lemma := mk_rel_eq_congr_lemma_core semireducible end tactic
9a4b599bc1dc209a7c3932bb68ca3ec4ac614847
c777c32c8e484e195053731103c5e52af26a25d1
/src/category_theory/concrete_category/basic.lean
2511901ec5badefc09f0b604b7ee20dd808ced61
[ "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
10,279
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl, Reid Barton, Sean Leather, Yury Kudryashov -/ import category_theory.types import category_theory.functor.epi_mono import category_theory.limits.constructions.epi_mono /-! # Concrete categories > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. A concrete category is a category `C` with a fixed faithful functor `forget : C ⥤ Type*`. We define concrete categories using `class concrete_category`. In particular, we impose no restrictions on the carrier type `C`, so `Type` is a concrete category with the identity forgetful functor. Each concrete category `C` comes with a canonical faithful functor `forget C : C ⥤ Type*`. We say that a concrete category `C` admits a *forgetful functor* to a concrete category `D`, if it has a functor `forget₂ C D : C ⥤ D` such that `(forget₂ C D) ⋙ (forget D) = forget C`, see `class has_forget₂`. Due to `faithful.div_comp`, it suffices to verify that `forget₂.obj` and `forget₂.map` agree with the equality above; then `forget₂` will satisfy the functor laws automatically, see `has_forget₂.mk'`. Two classes helping construct concrete categories in the two most common cases are provided in the files `bundled_hom` and `unbundled_hom`, see their documentation for details. ## References See [Ahrens and Lumsdaine, *Displayed Categories*][ahrens2017] for related work. -/ universes w v v' u namespace category_theory open category_theory.limits /-- A concrete category is a category `C` with a fixed faithful functor `forget : C ⥤ Type`. Note that `concrete_category` potentially depends on three independent universe levels, * the universe level `w` appearing in `forget : C ⥤ Type w` * the universe level `v` of the morphisms (i.e. we have a `category.{v} C`) * the universe level `u` of the objects (i.e `C : Type u`) They are specified that order, to avoid unnecessary universe annotations. -/ class concrete_category (C : Type u) [category.{v} C] := (forget [] : C ⥤ Type w) [forget_faithful : faithful forget] attribute [instance] concrete_category.forget_faithful /-- The forgetful functor from a concrete category to `Type u`. -/ @[reducible] def forget (C : Type v) [category C] [concrete_category.{u} C] : C ⥤ Type u := concrete_category.forget C instance concrete_category.types : concrete_category (Type u) := { forget := 𝟭 _ } /-- Provide a coercion to `Type u` for a concrete category. This is not marked as an instance as it could potentially apply to every type, and so is too expensive in typeclass search. You can use it on particular examples as: ``` instance : has_coe_to_sort X := concrete_category.has_coe_to_sort X ``` -/ def concrete_category.has_coe_to_sort (C : Type v) [category C] [concrete_category C] : has_coe_to_sort C (Type u) := ⟨(concrete_category.forget C).obj⟩ section local attribute [instance] concrete_category.has_coe_to_sort variables {C : Type v} [category C] [concrete_category C] @[simp] lemma forget_obj_eq_coe {X : C} : (forget C).obj X = X := rfl /-- Usually a bundled hom structure already has a coercion to function that works with different universes. So we don't use this as a global instance. -/ def concrete_category.has_coe_to_fun {X Y : C} : has_coe_to_fun (X ⟶ Y) (λ f, X → Y) := ⟨λ f, (forget _).map f⟩ local attribute [instance] concrete_category.has_coe_to_fun /-- In any concrete category, we can test equality of morphisms by pointwise evaluations.-/ lemma concrete_category.hom_ext {X Y : C} (f g : X ⟶ Y) (w : ∀ x : X, f x = g x) : f = g := begin apply faithful.map_injective (forget C), ext, exact w x, end @[simp] lemma forget_map_eq_coe {X Y : C} (f : X ⟶ Y) : (forget C).map f = f := rfl /-- Analogue of `congr_fun h x`, when `h : f = g` is an equality between morphisms in a concrete category. -/ lemma congr_hom {X Y : C} {f g : X ⟶ Y} (h : f = g) (x : X) : f x = g x := congr_fun (congr_arg (λ k : X ⟶ Y, (k : X → Y)) h) x lemma coe_id {X : C} : ((𝟙 X) : X → X) = id := (forget _).map_id X lemma coe_comp {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g : X → Z) = g ∘ f := (forget _).map_comp f g @[simp] lemma id_apply {X : C} (x : X) : ((𝟙 X) : X → X) x = x := congr_fun ((forget _).map_id X) x @[simp] lemma comp_apply {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := congr_fun ((forget _).map_comp _ _) x lemma concrete_category.congr_hom {X Y : C} {f g : X ⟶ Y} (h : f = g) (x : X) : f x = g x := congr_fun (congr_arg (λ f : X ⟶ Y, (f : X → Y)) h) x lemma concrete_category.congr_arg {X Y : C} (f : X ⟶ Y) {x x' : X} (h : x = x') : f x = f x' := congr_arg (f : X → Y) h /-- In any concrete category, injective morphisms are monomorphisms. -/ lemma concrete_category.mono_of_injective {X Y : C} (f : X ⟶ Y) (i : function.injective f) : mono f := (forget C).mono_of_mono_map ((mono_iff_injective f).2 i) lemma concrete_category.injective_of_mono_of_preserves_pullback {X Y : C} (f : X ⟶ Y) [mono f] [preserves_limits_of_shape walking_cospan (forget C)] : function.injective f := (mono_iff_injective ((forget C).map f)).mp infer_instance lemma concrete_category.mono_iff_injective_of_preserves_pullback {X Y : C} (f : X ⟶ Y) [preserves_limits_of_shape walking_cospan (forget C)] : mono f ↔ function.injective f := ((forget C).mono_map_iff_mono _).symm.trans (mono_iff_injective _) /-- In any concrete category, surjective morphisms are epimorphisms. -/ lemma concrete_category.epi_of_surjective {X Y : C} (f : X ⟶ Y) (s : function.surjective f) : epi f := (forget C).epi_of_epi_map ((epi_iff_surjective f).2 s) lemma concrete_category.surjective_of_epi_of_preserves_pushout {X Y : C} (f : X ⟶ Y) [epi f] [preserves_colimits_of_shape walking_span (forget C)] : function.surjective f := (epi_iff_surjective ((forget C).map f)).mp infer_instance lemma concrete_category.epi_iff_surjective_of_preserves_pushout {X Y : C} (f : X ⟶ Y) [preserves_colimits_of_shape walking_span (forget C)] : epi f ↔ function.surjective f := ((forget C).epi_map_iff_epi _).symm.trans (epi_iff_surjective _) lemma concrete_category.bijective_of_is_iso {X Y : C} (f : X ⟶ Y) [is_iso f] : function.bijective ((forget C).map f) := by { rw ← is_iso_iff_bijective, apply_instance, } @[simp] lemma concrete_category.has_coe_to_fun_Type {X Y : Type u} (f : X ⟶ Y) : coe_fn f = f := rfl end /-- `has_forget₂ C D`, where `C` and `D` are both concrete categories, provides a functor `forget₂ C D : C ⥤ D` and a proof that `forget₂ ⋙ (forget D) = forget C`. -/ class has_forget₂ (C : Type v) (D : Type v') [category C] [concrete_category.{u} C] [category D] [concrete_category.{u} D] := (forget₂ : C ⥤ D) (forget_comp : forget₂ ⋙ (forget D) = forget C . obviously) /-- The forgetful functor `C ⥤ D` between concrete categories for which we have an instance `has_forget₂ C `. -/ @[reducible] def forget₂ (C : Type v) (D : Type v') [category C] [concrete_category C] [category D] [concrete_category D] [has_forget₂ C D] : C ⥤ D := has_forget₂.forget₂ instance forget₂_faithful (C : Type v) (D : Type v') [category C] [concrete_category C] [category D] [concrete_category D] [has_forget₂ C D] : faithful (forget₂ C D) := has_forget₂.forget_comp.faithful_of_comp instance forget₂_preserves_monomorphisms (C : Type v) (D : Type v') [category C] [concrete_category C] [category D] [concrete_category D] [has_forget₂ C D] [(forget C).preserves_monomorphisms] : (forget₂ C D).preserves_monomorphisms := have (forget₂ C D ⋙ forget D).preserves_monomorphisms, by { simp only [has_forget₂.forget_comp], apply_instance }, by exactI functor.preserves_monomorphisms_of_preserves_of_reflects _ (forget D) instance forget₂_preserves_epimorphisms (C : Type v) (D : Type v') [category C] [concrete_category C] [category D] [concrete_category D] [has_forget₂ C D] [(forget C).preserves_epimorphisms] : (forget₂ C D).preserves_epimorphisms := have (forget₂ C D ⋙ forget D).preserves_epimorphisms, by { simp only [has_forget₂.forget_comp], apply_instance }, by exactI functor.preserves_epimorphisms_of_preserves_of_reflects _ (forget D) instance induced_category.concrete_category {C : Type v} {D : Type v'} [category D] [concrete_category D] (f : C → D) : concrete_category (induced_category D f) := { forget := induced_functor f ⋙ forget D } instance induced_category.has_forget₂ {C : Type v} {D : Type v'} [category D] [concrete_category D] (f : C → D) : has_forget₂ (induced_category D f) D := { forget₂ := induced_functor f, forget_comp := rfl } instance full_subcategory.concrete_category {C : Type v} [category C] [concrete_category C] (Z : C → Prop) : concrete_category (full_subcategory Z) := { forget := full_subcategory_inclusion Z ⋙ forget C } instance full_subcategory.has_forget₂ {C : Type v} [category C] [concrete_category C] (Z : C → Prop) : has_forget₂ (full_subcategory Z) C := { forget₂ := full_subcategory_inclusion Z, forget_comp := rfl } /-- In order to construct a “partially forgetting” functor, we do not need to verify functor laws; it suffices to ensure that compositions agree with `forget₂ C D ⋙ forget D = forget C`. -/ def has_forget₂.mk' {C : Type v} {D : Type v'} [category C] [concrete_category C] [category D] [concrete_category D] (obj : C → D) (h_obj : ∀ X, (forget D).obj (obj X) = (forget C).obj X) (map : Π {X Y}, (X ⟶ Y) → (obj X ⟶ obj Y)) (h_map : ∀ {X Y} {f : X ⟶ Y}, (forget D).map (map f) == (forget C).map f) : has_forget₂ C D := { forget₂ := faithful.div _ _ _ @h_obj _ @h_map, forget_comp := by apply faithful.div_comp } /-- Every forgetful functor factors through the identity functor. This is not a global instance as it is prone to creating type class resolution loops. -/ def has_forget_to_Type (C : Type v) [category C] [concrete_category C] : has_forget₂ C (Type u) := { forget₂ := forget C, forget_comp := functor.comp_id _ } end category_theory
fd889603c5cb1d1ccd99dbbab715a89dd21c2b8f
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/prec_max.lean
30f6942ed07d516822679d663af1738eaec7d5a8
[ "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
354
lean
open nat reserve postfix ⁻¹:(max + 1) postfix ⁻¹ := eq.symm constant foo (a b : nat) : a + b = 0 theorem tst1 (a b : nat) : 0 = a + b := !foo⁻¹ constant f {a b : nat} (h1 : 0 = a + b) (h2 : a = b) : a = 0 ∧ b = 0 example (a b : nat) : a = 0 ∧ b = 0 := f !foo⁻¹ sorry example (a b : nat) : a = 0 ∧ b = 0 := f !foo⁻¹ sorry⁻¹
abb45e39105b720d743068faf05be8dc75328c5a
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_by_contradiction.lean
4eff2381f845ba1e2efe168282b29d1eb0950514
[ "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
419
lean
constants P Q : Prop namespace with_classical open classical example : Q → (Q → ¬ P → false) → P := by blast example : Q → (Q → P → false) → ¬ P := by blast end with_classical namespace with_decidable constant P_dec : decidable P attribute P_dec [instance] definition foo : Q → (Q → ¬ P → false) → P := by blast example : Q → (Q → P → false) → ¬ P := by blast end with_decidable
54547feb42edc79105e0ced822b8ecc03a12baf2
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/mv_polynomial/equiv.lean
9e6f07645586b40fd88a2958ccd5211228bb36b7
[ "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
12,140
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro -/ import data.mv_polynomial.rename import data.equiv.fin import data.polynomial.algebra_map /-! # Equivalences between polynomial rings This file establishes a number of equivalences between polynomial rings, based on equivalences between the underlying types. ## Notation As in other polynomial files, we typically use the notation: + `σ : Type*` (indexing the variables) + `R : Type*` `[comm_semiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s` + `a : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial σ R` ## Tags equivalence, isomorphism, morphism, ring hom, hom -/ noncomputable theory open_locale classical big_operators open set function finsupp add_monoid_algebra universes u v w x variables {R : Type u} {S₁ : Type v} {S₂ : Type w} {S₃ : Type x} namespace mv_polynomial variables {σ : Type*} {a a' a₁ a₂ : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section equiv variables (R) [comm_semiring R] /-- The ring isomorphism between multivariable polynomials in a single variable and polynomials over the ground ring. -/ @[simps] def punit_alg_equiv : mv_polynomial punit R ≃ₐ[R] polynomial R := { to_fun := eval₂ polynomial.C (λu:punit, polynomial.X), inv_fun := polynomial.eval₂ mv_polynomial.C (X punit.star), left_inv := begin let f : polynomial R →+* mv_polynomial punit R := (polynomial.eval₂_ring_hom mv_polynomial.C (X punit.star)), let g : mv_polynomial punit R →+* polynomial R := (eval₂_hom polynomial.C (λu:punit, polynomial.X)), show ∀ p, f.comp g p = p, apply is_id, { ext a, dsimp, rw [eval₂_C, polynomial.eval₂_C] }, { rintros ⟨⟩, dsimp, rw [eval₂_X, polynomial.eval₂_X] } end, right_inv := assume p, polynomial.induction_on p (assume a, by rw [polynomial.eval₂_C, mv_polynomial.eval₂_C]) (assume p q hp hq, by rw [polynomial.eval₂_add, mv_polynomial.eval₂_add, hp, hq]) (assume p n hp, by rw [polynomial.eval₂_mul, polynomial.eval₂_pow, polynomial.eval₂_X, polynomial.eval₂_C, eval₂_mul, eval₂_C, eval₂_pow, eval₂_X]), map_mul' := λ _ _, eval₂_mul _ _, map_add' := λ _ _, eval₂_add _ _, commutes' := λ _, eval₂_C _ _ _} section map variables {R} (σ) /-- If `e : A ≃+* B` is an isomorphism of rings, then so is `map e`. -/ @[simps apply] def map_equiv [comm_semiring S₁] [comm_semiring S₂] (e : S₁ ≃+* S₂) : mv_polynomial σ S₁ ≃+* mv_polynomial σ S₂ := { to_fun := map (e : S₁ →+* S₂), inv_fun := map (e.symm : S₂ →+* S₁), left_inv := map_left_inverse e.left_inv, right_inv := map_right_inverse e.right_inv, ..map (e : S₁ →+* S₂) } @[simp] lemma map_equiv_refl : map_equiv σ (ring_equiv.refl R) = ring_equiv.refl _ := ring_equiv.ext map_id @[simp] lemma map_equiv_symm [comm_semiring S₁] [comm_semiring S₂] (e : S₁ ≃+* S₂) : (map_equiv σ e).symm = map_equiv σ e.symm := rfl @[simp] lemma map_equiv_trans [comm_semiring S₁] [comm_semiring S₂] [comm_semiring S₃] (e : S₁ ≃+* S₂) (f : S₂ ≃+* S₃) : (map_equiv σ e).trans (map_equiv σ f) = map_equiv σ (e.trans f) := ring_equiv.ext (map_map e f) variables {A₁ A₂ A₃ : Type*} [comm_semiring A₁] [comm_semiring A₂] [comm_semiring A₃] variables [algebra R A₁] [algebra R A₂] [algebra R A₃] /-- If `e : A ≃ₐ[R] B` is an isomorphism of `R`-algebras, then so is `map e`. -/ @[simps apply] def map_alg_equiv (e : A₁ ≃ₐ[R] A₂) : mv_polynomial σ A₁ ≃ₐ[R] mv_polynomial σ A₂ := { to_fun := map (e : A₁ →+* A₂), ..map_alg_hom (e : A₁ →ₐ[R] A₂), ..map_equiv σ (e : A₁ ≃+* A₂) } @[simp] lemma map_alg_equiv_refl : map_alg_equiv σ (alg_equiv.refl : A₁ ≃ₐ[R] A₁) = alg_equiv.refl := alg_equiv.ext map_id @[simp] lemma map_alg_equiv_symm (e : A₁ ≃ₐ[R] A₂) : (map_alg_equiv σ e).symm = map_alg_equiv σ e.symm := rfl @[simp] lemma map_alg_equiv_trans (e : A₁ ≃ₐ[R] A₂) (f : A₂ ≃ₐ[R] A₃) : (map_alg_equiv σ e).trans (map_alg_equiv σ f) = map_alg_equiv σ (e.trans f) := alg_equiv.ext (map_map e f) end map section variables (S₁ S₂ S₃) /-- The function from multivariable polynomials in a sum of two types, to multivariable polynomials in one of the types, with coefficents in multivariable polynomials in the other type. See `sum_ring_equiv` for the ring isomorphism. -/ def sum_to_iter : mv_polynomial (S₁ ⊕ S₂) R →+* mv_polynomial S₁ (mv_polynomial S₂ R) := eval₂_hom (C.comp C) (λbc, sum.rec_on bc X (C ∘ X)) @[simp] lemma sum_to_iter_C (a : R) : sum_to_iter R S₁ S₂ (C a) = C (C a) := eval₂_C _ _ a @[simp] lemma sum_to_iter_Xl (b : S₁) : sum_to_iter R S₁ S₂ (X (sum.inl b)) = X b := eval₂_X _ _ (sum.inl b) @[simp] lemma sum_to_iter_Xr (c : S₂) : sum_to_iter R S₁ S₂ (X (sum.inr c)) = C (X c) := eval₂_X _ _ (sum.inr c) /-- The function from multivariable polynomials in one type, with coefficents in multivariable polynomials in another type, to multivariable polynomials in the sum of the two types. See `sum_ring_equiv` for the ring isomorphism. -/ def iter_to_sum : mv_polynomial S₁ (mv_polynomial S₂ R) →+* mv_polynomial (S₁ ⊕ S₂) R := eval₂_hom (eval₂_hom C (X ∘ sum.inr)) (X ∘ sum.inl) lemma iter_to_sum_C_C (a : R) : iter_to_sum R S₁ S₂ (C (C a)) = C a := eq.trans (eval₂_C _ _ (C a)) (eval₂_C _ _ _) lemma iter_to_sum_X (b : S₁) : iter_to_sum R S₁ S₂ (X b) = X (sum.inl b) := eval₂_X _ _ _ lemma iter_to_sum_C_X (c : S₂) : iter_to_sum R S₁ S₂ (C (X c)) = X (sum.inr c) := eq.trans (eval₂_C _ _ (X c)) (eval₂_X _ _ _) variable (σ) /-- The algebra isomorphism between multivariable polynomials in no variables and the ground ring. -/ @[simps] def is_empty_alg_equiv [he : is_empty σ] : mv_polynomial σ R ≃ₐ[R] R := alg_equiv.of_alg_hom (aeval (is_empty.elim he)) (algebra.of_id _ _) (by { ext, simp [algebra.of_id_apply, algebra_map_eq] }) (by { ext i m, exact is_empty.elim' he i }) /-- The ring isomorphism between multivariable polynomials in no variables and the ground ring. -/ @[simps] def is_empty_ring_equiv [he : is_empty σ] : mv_polynomial σ R ≃+* R := (is_empty_alg_equiv R σ).to_ring_equiv variable {σ} /-- A helper function for `sum_ring_equiv`. -/ @[simps] def mv_polynomial_equiv_mv_polynomial [comm_semiring S₃] (f : mv_polynomial S₁ R →+* mv_polynomial S₂ S₃) (g : mv_polynomial S₂ S₃ →+* mv_polynomial S₁ R) (hfgC : (f.comp g).comp C = C) (hfgX : ∀n, f (g (X n)) = X n) (hgfC : (g.comp f).comp C = C) (hgfX : ∀n, g (f (X n)) = X n) : mv_polynomial S₁ R ≃+* mv_polynomial S₂ S₃ := { to_fun := f, inv_fun := g, left_inv := is_id (ring_hom.comp _ _) hgfC hgfX, right_inv := is_id (ring_hom.comp _ _) hfgC hfgX, map_mul' := f.map_mul, map_add' := f.map_add } /-- The ring isomorphism between multivariable polynomials in a sum of two types, and multivariable polynomials in one of the types, with coefficents in multivariable polynomials in the other type. -/ def sum_ring_equiv : mv_polynomial (S₁ ⊕ S₂) R ≃+* mv_polynomial S₁ (mv_polynomial S₂ R) := begin apply @mv_polynomial_equiv_mv_polynomial R (S₁ ⊕ S₂) _ _ _ _ (sum_to_iter R S₁ S₂) (iter_to_sum R S₁ S₂), { refine ring_hom.ext (λ p, _), rw [ring_hom.comp_apply], convert hom_eq_hom ((sum_to_iter R S₁ S₂).comp ((iter_to_sum R S₁ S₂).comp C)) C _ _ p, { ext1 a, dsimp, rw [iter_to_sum_C_C R S₁ S₂, sum_to_iter_C R S₁ S₂] }, { assume c, dsimp, rw [iter_to_sum_C_X R S₁ S₂, sum_to_iter_Xr R S₁ S₂] } }, { assume b, rw [iter_to_sum_X R S₁ S₂, sum_to_iter_Xl R S₁ S₂] }, { ext1 a, rw [ring_hom.comp_apply, ring_hom.comp_apply, sum_to_iter_C R S₁ S₂, iter_to_sum_C_C R S₁ S₂] }, { assume n, cases n with b c, { rw [sum_to_iter_Xl, iter_to_sum_X] }, { rw [sum_to_iter_Xr, iter_to_sum_C_X] } }, end /-- The algebra isomorphism between multivariable polynomials in a sum of two types, and multivariable polynomials in one of the types, with coefficents in multivariable polynomials in the other type. -/ def sum_alg_equiv : mv_polynomial (S₁ ⊕ S₂) R ≃ₐ[R] mv_polynomial S₁ (mv_polynomial S₂ R) := { commutes' := begin intro r, have A : algebra_map R (mv_polynomial S₁ (mv_polynomial S₂ R)) r = (C (C r) : _), by refl, have B : algebra_map R (mv_polynomial (S₁ ⊕ S₂) R) r = C r, by refl, simp only [sum_ring_equiv, sum_to_iter_C, mv_polynomial_equiv_mv_polynomial_apply, ring_equiv.to_fun_eq_coe, A, B], end, ..sum_ring_equiv R S₁ S₂ } section -- this speeds up typeclass search in the lemma below local attribute [instance, priority 2000] is_scalar_tower.right /-- The algebra isomorphism between multivariable polynomials in `option S₁` and polynomials with coefficients in `mv_polynomial S₁ R`. -/ @[simps] def option_equiv_left : mv_polynomial (option S₁) R ≃ₐ[R] polynomial (mv_polynomial S₁ R) := alg_equiv.of_alg_hom (mv_polynomial.aeval (λ o, o.elim polynomial.X (λ s, polynomial.C (X s)))) (polynomial.aeval_tower (mv_polynomial.rename some) (X none)) (by ext : 2; simp [← polynomial.C_eq_algebra_map]) (by ext i : 2; cases i; simp) end /-- The algebra isomorphism between multivariable polynomials in `option S₁` and multivariable polynomials with coefficients in polynomials. -/ def option_equiv_right : mv_polynomial (option S₁) R ≃ₐ[R] mv_polynomial S₁ (polynomial R) := alg_equiv.of_alg_hom (mv_polynomial.aeval (λ o, o.elim (C polynomial.X) X)) (mv_polynomial.aeval_tower (polynomial.aeval (X none)) (λ i, X (option.some i))) (by ext : 2; simp [mv_polynomial.algebra_map_eq]) (by ext i : 2; cases i; simp) /-- The algebra isomorphism between multivariable polynomials in `fin (n + 1)` and polynomials over multivariable polynomials in `fin n`. -/ def fin_succ_equiv (n : ℕ) : mv_polynomial (fin (n + 1)) R ≃ₐ[R] polynomial (mv_polynomial (fin n) R) := (rename_equiv R (fin_succ_equiv n)).trans (option_equiv_left R (fin n)) lemma fin_succ_equiv_eq (n : ℕ) : (fin_succ_equiv R n : mv_polynomial (fin (n + 1)) R →+* polynomial (mv_polynomial (fin n) R)) = eval₂_hom (polynomial.C.comp (C : R →+* mv_polynomial (fin n) R)) (λ i : fin (n+1), fin.cases polynomial.X (λ k, polynomial.C (X k)) i) := begin ext : 2, { simp only [fin_succ_equiv, option_equiv_left_apply, aeval_C, alg_equiv.coe_trans, alg_equiv.coe_alg_hom, coe_eval₂_hom, alg_hom.coe_to_ring_hom, comp_app, rename_equiv_apply, eval₂_C, ring_hom.coe_comp, coe_coe, rename_C], refl }, { intro i, refine fin.cases _ _ i; simp [fin_succ_equiv] } end @[simp] lemma fin_succ_equiv_apply (n : ℕ) (p : mv_polynomial (fin (n + 1)) R) : fin_succ_equiv R n p = eval₂_hom (polynomial.C.comp (C : R →+* mv_polynomial (fin n) R)) (λ i : fin (n+1), fin.cases polynomial.X (λ k, polynomial.C (X k)) i) p := by { rw ← fin_succ_equiv_eq, refl } lemma fin_succ_equiv_comp_C_eq_C {R : Type u} [comm_semiring R] (n : ℕ) : (↑(mv_polynomial.fin_succ_equiv R n).symm : polynomial (mv_polynomial (fin n) R) →+* _).comp ((polynomial.C).comp (mv_polynomial.C)) = (mv_polynomial.C : R →+* mv_polynomial (fin n.succ) R) := begin refine ring_hom.ext (λ x, _), rw ring_hom.comp_apply, refine (mv_polynomial.fin_succ_equiv R n).injective (trans ((mv_polynomial.fin_succ_equiv R n).apply_symm_apply _) _), simp only [mv_polynomial.fin_succ_equiv_apply, mv_polynomial.eval₂_hom_C], end end end equiv end mv_polynomial
ab4f6ced516e8ee6ec8ae285301f1782e3561110
7b66d83f3b69dae0a3dfb684d7ebe5e9e3f3c913
/src/solutions/thursday/afternoon/category_theory/exercise7.lean
38523506dafc5b59cee1f40619dec8102d36b181
[]
permissive
dpochekutov/lftcm2020
58a09e10f0e638075b97884d3c2612eb90296adb
cdfbf1ac089f21058e523db73f2476c9c98ed16a
refs/heads/master
1,669,226,265,076
1,594,629,725,000
1,594,629,725,000
279,213,346
1
0
MIT
1,594,622,757,000
1,594,615,843,000
null
UTF-8
Lean
false
false
1,875
lean
import algebra.category.CommRing.basic import data.polynomial /-! Let show that taking polynomials over a ring is functor `Ring ⥤ Ring`. -/ noncomputable theory -- the default implementation of polynomials is noncomputable /-! mathlib is undergoing a transition at the moment from using "unbundled" homomorphisms (e.g. we talk about a bare function `f : R → S`, along with a typeclass `[is_semiring_hom f]`) to using "bundled" homomorphisms (e.g. a structure `f : R →+* S`, which has a coercion to a bare function). The category `Ring` uses bundled homomorphisms (and in future all of mathlib will). However at present the polynomial library hasn't been updated. You may find the `ring_hom.of` useful -- this upgrades an unbundled homomorphism to a bundled homomorphism. -/ /-! Hints: * use `polynomial.map` * use `polynomial.coeff_map` (what happens if you mark this as a `simp` lemma?) -/ def Ring.polynomial : Ring ⥤ Ring := -- sorry { obj := λ R, Ring.of (polynomial R), map := λ R S f, ring_hom.of (polynomial.map f), map_id' := λ R, by { ext, dsimp, simp [polynomial.coeff_map], }, map_comp' := λ R S T f g, by { ext, dsimp, simp [polynomial.coeff_map], }, } -- sorry -- omit attribute [simp] polynomial.coeff_map -- omit def CommRing.polynomial : CommRing ⥤ CommRing := -- sorry { obj := λ R, CommRing.of (polynomial R), map := λ R S f, ring_hom.of (polynomial.map f), map_id' := λ R, by { ext, dsimp, simp, }, -- TODO If #3380 is merged in time, we can just omit these proofs. map_comp' := λ R S T f g, by { ext, dsimp, simp, }, } -- sorry open category_theory def commutes : (forget₂ CommRing Ring) ⋙ Ring.polynomial ≅ CommRing.polynomial ⋙ (forget₂ CommRing Ring) := -- Hint: You can do this in two lines, ≤ 33 columns! -- sorry { hom := { app := λ R, 𝟙 _, }, inv := { app := λ R, 𝟙 _, }, } -- sorry
280eb247c0fe554d0e28a54d0e2010cd98a99e79
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/cases_failure.hlean
348b7a95e4bbdf543196a5fc0501cc54fcd53062
[ "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
388
hlean
-- .hlean file open eq inductive square {A : Type} {a₀₀ : A} : Π{a₀₁ a₁₀ a₁₁ : A}, a₀₀ = a₀₁ → a₁₀ = a₁₁ → a₀₀ = a₁₀ → a₀₁ = a₁₁ → Type := ids : square idp idp idp idp definition foo {A : Type} {x y z : A} {t : x = y} {b : z = y} {l : x = z} {r : y = y} (s : square t b l r) : unit := begin cases s, exact unit.star end
a8bbfa98477ea8d0159b659b1a4d7764020fbf57
c777c32c8e484e195053731103c5e52af26a25d1
/src/topology/vector_bundle/hom.lean
20c00d4d1189445ce1bda656dbe5158450d6bbad
[ "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
17,052
lean
/- Copyright © 2022 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth, Floris van Doorn -/ import topology.vector_bundle.basic import analysis.normed_space.operator_norm /-! # The vector bundle of continuous (semi)linear maps We define the (topological) vector bundle of continuous (semi)linear maps between two vector bundles over the same base. Given bundles `E₁ E₂ : B → Type*`, normed spaces `F₁` and `F₂`, and a ring-homomorphism `σ` between their respective scalar fields, we define `bundle.continuous_linear_map σ F₁ E₁ F₂ E₂ x` to be a type synonym for `λ x, E₁ x →SL[σ] E₂ x`. If the `E₁` and `E₂` are vector bundles with model fibers `F₁` and `F₂`, then this will be a vector bundle with fiber `F₁ →SL[σ] F₂`. The topology is constructed from the trivializations for `E₁` and `E₂` and the norm-topology on the model fiber `F₁ →SL[𝕜] F₂` using the `vector_prebundle` construction. This is a bit awkward because it introduces a spurious (?) dependence on the normed space structure of the model fiber, rather than just its topological vector space structure; this might be fixable now that we have `continuous_linear_map.strong_topology`. Similar constructions should be possible (but are yet to be formalized) for tensor products of topological vector bundles, exterior algebras, and so on, where again the topology can be defined using a norm on the fiber model if this helps. ## Main Definitions * `bundle.continuous_linear_map.vector_bundle`: continuous semilinear maps between vector bundles form a vector bundle. -/ noncomputable theory open_locale bundle open bundle set continuous_linear_map section defs variables {𝕜₁ 𝕜₂ : Type*} [normed_field 𝕜₁] [normed_field 𝕜₂] variables (σ : 𝕜₁ →+* 𝕜₂) variables {B : Type*} variables (F₁ : Type*) (E₁ : B → Type*) [Π x, add_comm_monoid (E₁ x)] [Π x, module 𝕜₁ (E₁ x)] variables [Π x, topological_space (E₁ x)] variables (F₂ : Type*) (E₂ : B → Type*) [Π x, add_comm_monoid (E₂ x)] [Π x, module 𝕜₂ (E₂ x)] variables [Π x, topological_space (E₂ x)] include F₁ F₂ -- In this definition we require the scalar rings `𝕜₁` and `𝕜₂` to be normed fields, although -- something much weaker (maybe `comm_semiring`) would suffice mathematically -- this is because of -- a typeclass inference bug with pi-types: -- https://leanprover.zulipchat.com/#narrow/stream/116395-maths/topic/vector.20bundles.20--.20typeclass.20inference.20issue /-- The bundle of continuous `σ`-semilinear maps between the topological vector bundles `E₁` and `E₂`. This is a type synonym for `λ x, E₁ x →SL[σ] E₂ x`. We intentionally add `F₁` and `F₂` as arguments to this type, so that instances on this type (that depend on `F₁` and `F₂`) actually refer to `F₁` and `F₂`. -/ @[derive inhabited, nolint unused_arguments] protected def bundle.continuous_linear_map (x : B) : Type* := E₁ x →SL[σ] E₂ x instance bundle.continuous_linear_map.add_monoid_hom_class (x : B) : add_monoid_hom_class (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂ x) (E₁ x) (E₂ x) := by delta_instance bundle.continuous_linear_map variables [Π x, has_continuous_add (E₂ x)] instance (x : B) : add_comm_monoid (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂ x) := by delta_instance bundle.continuous_linear_map variables [∀ x, has_continuous_smul 𝕜₂ (E₂ x)] instance (x : B) : module 𝕜₂ (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂ x) := by delta_instance bundle.continuous_linear_map end defs variables {𝕜₁ : Type*} [nontrivially_normed_field 𝕜₁] {𝕜₂ : Type*} [nontrivially_normed_field 𝕜₂] (σ : 𝕜₁ →+* 𝕜₂) [iσ : ring_hom_isometric σ] variables {B : Type*} [topological_space B] variables (F₁ : Type*) [normed_add_comm_group F₁] [normed_space 𝕜₁ F₁] (E₁ : B → Type*) [Π x, add_comm_monoid (E₁ x)] [Π x, module 𝕜₁ (E₁ x)] [topological_space (total_space E₁)] variables (F₂ : Type*) [normed_add_comm_group F₂][normed_space 𝕜₂ F₂] (E₂ : B → Type*) [Π x, add_comm_monoid (E₂ x)] [Π x, module 𝕜₂ (E₂ x)] [topological_space (total_space E₂)] variables {F₁ E₁ F₂ E₂} (e₁ e₁' : trivialization F₁ (π E₁)) (e₂ e₂' : trivialization F₂ (π E₂)) namespace pretrivialization include iσ /-- Assume `eᵢ` and `eᵢ'` are trivializations of the bundles `Eᵢ` over base `B` with fiber `Fᵢ` (`i ∈ {1,2}`), then `continuous_linear_map_coord_change σ e₁ e₁' e₂ e₂'` is the coordinate change function between the two induced (pre)trivializations `pretrivialization.continuous_linear_map σ e₁ e₂` and `pretrivialization.continuous_linear_map σ e₁' e₂'` of `bundle.continuous_linear_map`. -/ def continuous_linear_map_coord_change [e₁.is_linear 𝕜₁] [e₁'.is_linear 𝕜₁] [e₂.is_linear 𝕜₂] [e₂'.is_linear 𝕜₂] (b : B) : (F₁ →SL[σ] F₂) →L[𝕜₂] F₁ →SL[σ] F₂ := ((e₁'.coord_changeL 𝕜₁ e₁ b).symm.arrow_congrSL (e₂.coord_changeL 𝕜₂ e₂' b) : (F₁ →SL[σ] F₂) ≃L[𝕜₂] F₁ →SL[σ] F₂) variables {σ e₁ e₁' e₂ e₂'} variables [Π x, topological_space (E₁ x)] [fiber_bundle F₁ E₁] variables [Π x, topological_space (E₂ x)] [fiber_bundle F₂ E₂] lemma continuous_on_continuous_linear_map_coord_change [vector_bundle 𝕜₁ F₁ E₁] [vector_bundle 𝕜₂ F₂ E₂] [mem_trivialization_atlas e₁] [mem_trivialization_atlas e₁'] [mem_trivialization_atlas e₂] [mem_trivialization_atlas e₂'] : continuous_on (continuous_linear_map_coord_change σ e₁ e₁' e₂ e₂') ((e₁.base_set ∩ e₂.base_set) ∩ (e₁'.base_set ∩ e₂'.base_set)) := begin have h₁ := (compSL F₁ F₂ F₂ σ (ring_hom.id 𝕜₂)).continuous, have h₂ := (continuous_linear_map.flip (compSL F₁ F₁ F₂ (ring_hom.id 𝕜₁) σ)).continuous, have h₃ := (continuous_on_coord_change 𝕜₁ e₁' e₁), have h₄ := (continuous_on_coord_change 𝕜₂ e₂ e₂'), refine ((h₁.comp_continuous_on (h₄.mono _)).clm_comp (h₂.comp_continuous_on (h₃.mono _))).congr _, { mfld_set_tac }, { mfld_set_tac }, { intros b hb, ext L v, simp only [continuous_linear_map_coord_change, continuous_linear_equiv.coe_coe, continuous_linear_equiv.arrow_congrSL_apply, comp_apply, function.comp, compSL_apply, flip_apply, continuous_linear_equiv.symm_symm] }, end omit iσ variables (σ e₁ e₁' e₂ e₂') [e₁.is_linear 𝕜₁] [e₁'.is_linear 𝕜₁] [e₂.is_linear 𝕜₂] [e₂'.is_linear 𝕜₂] /-- Given trivializations `e₁`, `e₂` for vector bundles `E₁`, `E₂` over a base `B`, `pretrivialization.continuous_linear_map σ e₁ e₂` is the induced pretrivialization for the continuous `σ`-semilinear maps from `E₁` to `E₂`. That is, the map which will later become a trivialization, after the bundle of continuous semilinear maps is equipped with the right topological vector bundle structure. -/ def continuous_linear_map : pretrivialization (F₁ →SL[σ] F₂) (π (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂)) := { to_fun := λ p, ⟨p.1, (e₂.continuous_linear_map_at 𝕜₂ p.1).comp $ p.2.comp $ e₁.symmL 𝕜₁ p.1⟩, inv_fun := λ p, ⟨p.1, (e₂.symmL 𝕜₂ p.1).comp $ p.2.comp $ e₁.continuous_linear_map_at 𝕜₁ p.1⟩, source := (bundle.total_space.proj) ⁻¹' (e₁.base_set ∩ e₂.base_set), target := (e₁.base_set ∩ e₂.base_set) ×ˢ set.univ, map_source' := λ ⟨x, L⟩ h, ⟨h, set.mem_univ _⟩, map_target' := λ ⟨x, f⟩ h, h.1, left_inv' := λ ⟨x, L⟩ ⟨h₁, h₂⟩, begin simp_rw [sigma.mk.inj_iff, eq_self_iff_true, heq_iff_eq, true_and], ext v, simp only [comp_apply, trivialization.symmL_continuous_linear_map_at, h₁, h₂] end, right_inv' := λ ⟨x, f⟩ ⟨⟨h₁, h₂⟩, _⟩, begin simp_rw [prod.mk.inj_iff, eq_self_iff_true, true_and], ext v, simp only [comp_apply, trivialization.continuous_linear_map_at_symmL, h₁, h₂] end, open_target := (e₁.open_base_set.inter e₂.open_base_set).prod is_open_univ, base_set := e₁.base_set ∩ e₂.base_set, open_base_set := e₁.open_base_set.inter e₂.open_base_set, source_eq := rfl, target_eq := rfl, proj_to_fun := λ ⟨x, f⟩ h, rfl } instance continuous_linear_map.is_linear [Π x, has_continuous_add (E₂ x)] [Π x, has_continuous_smul 𝕜₂ (E₂ x)] : (pretrivialization.continuous_linear_map σ e₁ e₂).is_linear 𝕜₂ := { linear := λ x h, { map_add := λ L L', show (e₂.continuous_linear_map_at 𝕜₂ x).comp ((L + L').comp (e₁.symmL 𝕜₁ x)) = _, begin simp_rw [add_comp, comp_add], refl end, map_smul := λ c L, show (e₂.continuous_linear_map_at 𝕜₂ x).comp ((c • L).comp (e₁.symmL 𝕜₁ x)) = _, begin simp_rw [smul_comp, comp_smulₛₗ, ring_hom.id_apply], refl end, } } lemma continuous_linear_map_apply (p : total_space (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂)) : (continuous_linear_map σ e₁ e₂) p = ⟨p.1, (e₂.continuous_linear_map_at 𝕜₂ p.1).comp $ p.2.comp $ e₁.symmL 𝕜₁ p.1⟩ := rfl lemma continuous_linear_map_symm_apply (p : B × (F₁ →SL[σ] F₂)) : (continuous_linear_map σ e₁ e₂).to_local_equiv.symm p = ⟨p.1, (e₂.symmL 𝕜₂ p.1).comp $ p.2.comp $ e₁.continuous_linear_map_at 𝕜₁ p.1⟩ := rfl variables [Π x, has_continuous_add (E₂ x)] lemma continuous_linear_map_symm_apply' {b : B} (hb : b ∈ e₁.base_set ∩ e₂.base_set) (L : F₁ →SL[σ] F₂) : (continuous_linear_map σ e₁ e₂).symm b L = (e₂.symmL 𝕜₂ b).comp (L.comp $ e₁.continuous_linear_map_at 𝕜₁ b) := begin rw [symm_apply], refl, exact hb end lemma continuous_linear_map_coord_change_apply [ring_hom_isometric σ] (b : B) (hb : b ∈ (e₁.base_set ∩ e₂.base_set) ∩ (e₁'.base_set ∩ e₂'.base_set)) (L : F₁ →SL[σ] F₂) : continuous_linear_map_coord_change σ e₁ e₁' e₂ e₂' b L = (continuous_linear_map σ e₁' e₂' (total_space_mk b ((continuous_linear_map σ e₁ e₂).symm b L))).2 := begin ext v, simp_rw [continuous_linear_map_coord_change, continuous_linear_equiv.coe_coe, continuous_linear_equiv.arrow_congrSL_apply, continuous_linear_map_apply, continuous_linear_map_symm_apply' σ e₁ e₂ hb.1, comp_apply, continuous_linear_equiv.coe_coe, continuous_linear_equiv.symm_symm, trivialization.continuous_linear_map_at_apply, trivialization.symmL_apply], dsimp only [total_space_mk], rw [e₂.coord_changeL_apply e₂', e₁'.coord_changeL_apply e₁, e₁.coe_linear_map_at_of_mem hb.1.1, e₂'.coe_linear_map_at_of_mem hb.2.2], exacts [⟨hb.2.1, hb.1.1⟩, ⟨hb.1.2, hb.2.2⟩] end end pretrivialization open pretrivialization variables (F₁ E₁ F₂ E₂) [ring_hom_isometric σ] variables [Π x : B, topological_space (E₁ x)] [fiber_bundle F₁ E₁] [vector_bundle 𝕜₁ F₁ E₁] variables [Π x : B, topological_space (E₂ x)] [fiber_bundle F₂ E₂] [vector_bundle 𝕜₂ F₂ E₂] variables [Π x, has_continuous_add (E₂ x)] [Π x, has_continuous_smul 𝕜₂ (E₂ x)] /-- The continuous `σ`-semilinear maps between two topological vector bundles form a `vector_prebundle` (this is an auxiliary construction for the `vector_bundle` instance, in which the pretrivializations are collated but no topology on the total space is yet provided). -/ def _root_.bundle.continuous_linear_map.vector_prebundle : vector_prebundle 𝕜₂ (F₁ →SL[σ] F₂) (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂) := { pretrivialization_atlas := {e | ∃ (e₁ : trivialization F₁ (π E₁)) (e₂ : trivialization F₂ (π E₂)) [mem_trivialization_atlas e₁] [mem_trivialization_atlas e₂], by exactI e = pretrivialization.continuous_linear_map σ e₁ e₂}, pretrivialization_linear' := begin rintro _ ⟨e₁, he₁, e₂, he₂, rfl⟩, apply_instance end, pretrivialization_at := λ x, pretrivialization.continuous_linear_map σ (trivialization_at F₁ E₁ x) (trivialization_at F₂ E₂ x), mem_base_pretrivialization_at := λ x, ⟨mem_base_set_trivialization_at F₁ E₁ x, mem_base_set_trivialization_at F₂ E₂ x⟩, pretrivialization_mem_atlas := λ x, ⟨trivialization_at F₁ E₁ x, trivialization_at F₂ E₂ x, _, _, rfl⟩, exists_coord_change := by { rintro _ ⟨e₁, e₂, he₁, he₂, rfl⟩ _ ⟨e₁', e₂', he₁', he₂', rfl⟩, resetI, exact ⟨continuous_linear_map_coord_change σ e₁ e₁' e₂ e₂', continuous_on_continuous_linear_map_coord_change, continuous_linear_map_coord_change_apply σ e₁ e₁' e₂ e₂'⟩ } } /-- Topology on the continuous `σ`-semilinear_maps between the respective fibers at a point of two "normable" vector bundles over the same base. Here "normable" means that the bundles have fibers modelled on normed spaces `F₁`, `F₂` respectively. The topology we put on the continuous `σ`-semilinear_maps is the topology coming from the operator norm on maps from `F₁` to `F₂`. -/ instance (x : B) : topological_space (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂ x) := (bundle.continuous_linear_map.vector_prebundle σ F₁ E₁ F₂ E₂).fiber_topology x /-- Topology on the total space of the continuous `σ`-semilinear_maps between two "normable" vector bundles over the same base. -/ instance bundle.continuous_linear_map.topological_space_total_space : topological_space (total_space (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂)) := (bundle.continuous_linear_map.vector_prebundle σ F₁ E₁ F₂ E₂).total_space_topology /-- The continuous `σ`-semilinear_maps between two vector bundles form a fiber bundle. -/ instance _root_.bundle.continuous_linear_map.fiber_bundle : fiber_bundle (F₁ →SL[σ] F₂) (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂) := (bundle.continuous_linear_map.vector_prebundle σ F₁ E₁ F₂ E₂).to_fiber_bundle /-- The continuous `σ`-semilinear_maps between two vector bundles form a vector bundle. -/ instance _root_.bundle.continuous_linear_map.vector_bundle : vector_bundle 𝕜₂ (F₁ →SL[σ] F₂) (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂) := (bundle.continuous_linear_map.vector_prebundle σ F₁ E₁ F₂ E₂).to_vector_bundle variables (e₁ e₂) [he₁ : mem_trivialization_atlas e₁] [he₂ : mem_trivialization_atlas e₂] {F₁ E₁ F₂ E₂} include he₁ he₂ /-- Given trivializations `e₁`, `e₂` in the atlas for vector bundles `E₁`, `E₂` over a base `B`, the induced trivialization for the continuous `σ`-semilinear maps from `E₁` to `E₂`, whose base set is `e₁.base_set ∩ e₂.base_set`. -/ def trivialization.continuous_linear_map : trivialization (F₁ →SL[σ] F₂) (π (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂)) := vector_prebundle.trivialization_of_mem_pretrivialization_atlas _ ⟨e₁, e₂, he₁, he₂, rfl⟩ instance _root_.bundle.continuous_linear_map.mem_trivialization_atlas : mem_trivialization_atlas (e₁.continuous_linear_map σ e₂ : trivialization (F₁ →SL[σ] F₂) (π (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂))) := { out := ⟨_, ⟨e₁, e₂, by apply_instance, by apply_instance, rfl⟩, rfl⟩ } variables {e₁ e₂} @[simp] lemma trivialization.base_set_continuous_linear_map : (e₁.continuous_linear_map σ e₂).base_set = e₁.base_set ∩ e₂.base_set := rfl lemma trivialization.continuous_linear_map_apply (p : total_space (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂)) : e₁.continuous_linear_map σ e₂ p = ⟨p.1, (e₂.continuous_linear_map_at 𝕜₂ p.1).comp $ p.2.comp $ e₁.symmL 𝕜₁ p.1⟩ := rfl omit he₁ he₂ lemma hom_trivialization_at_apply (x₀ : B) (x : total_space (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂)) : trivialization_at (F₁ →SL[σ] F₂) (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂) x₀ x = ⟨x.1, in_coordinates F₁ E₁ F₂ E₂ x₀ x.1 x₀ x.1 x.2⟩ := rfl @[simp, mfld_simps] lemma hom_trivialization_at_source (x₀ : B) : (trivialization_at (F₁ →SL[σ] F₂) (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂) x₀).source = π (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂) ⁻¹' ((trivialization_at F₁ E₁ x₀).base_set ∩ (trivialization_at F₂ E₂ x₀).base_set) := rfl @[simp, mfld_simps] lemma hom_trivialization_at_target (x₀ : B) : (trivialization_at (F₁ →SL[σ] F₂) (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂) x₀).target = ((trivialization_at F₁ E₁ x₀).base_set ∩ (trivialization_at F₂ E₂ x₀).base_set) ×ˢ set.univ := rfl
255c4575a70c47366edc92aed64d303670539329
4fa161becb8ce7378a709f5992a594764699e268
/src/ring_theory/subring.lean
3a1ea1c95a5163c2617dce9849ea63e5107f66bf
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
8,253
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import deprecated.subgroup universes u v open group variables {R : Type u} [ring R] section prio set_option default_priority 100 -- see Note [default priority] /-- `S` is a subring: a set containing 1 and closed under multiplication, addition and and additive inverse. -/ class is_subring (S : set R) extends is_add_subgroup S, is_submonoid S : Prop. end prio instance subset.ring {S : set R} [is_subring S] : ring S := { left_distrib := λ x y z, subtype.eq $ left_distrib x.1 y.1 z.1, right_distrib := λ x y z, subtype.eq $ right_distrib x.1 y.1 z.1, .. subtype.add_comm_group, .. subtype.monoid } instance subtype.ring {S : set R} [is_subring S] : ring (subtype S) := subset.ring namespace ring_hom instance is_subring_preimage {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set S) [is_subring s] : is_subring (f ⁻¹' s) := {} instance is_subring_image {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set R) [is_subring s] : is_subring (f '' s) := {} instance is_subring_set_range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : is_subring (set.range f) := {} end ring_hom /-- Restrict the codomain of a ring homomorphism to a subring that includes the range. -/ def ring_hom.cod_restrict {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set S) [is_subring s] (h : ∀ x, f x ∈ s) : R →+* s := { to_fun := λ x, ⟨f x, h x⟩, map_add' := λ x y, subtype.eq $ f.map_add x y, map_zero' := subtype.eq f.map_zero, map_mul' := λ x y, subtype.eq $ f.map_mul x y, map_one' := subtype.eq f.map_one } /-- Coersion `S → R` as a ring homormorphism-/ def is_subring.subtype (S : set R) [is_subring S] : S →+* R := ⟨coe, rfl, λ _ _, rfl, rfl, λ _ _, rfl⟩ @[simp] lemma is_subring.coe_subtype {S : set R} [is_subring S] : ⇑(is_subring.subtype S) = coe := rfl variables {cR : Type u} [comm_ring cR] instance subset.comm_ring {S : set cR} [is_subring S] : comm_ring S := { mul_comm := λ x y, subtype.eq $ mul_comm x.1 y.1, .. subset.ring } instance subtype.comm_ring {S : set cR} [is_subring S] : comm_ring (subtype S) := subset.comm_ring instance subring.domain {D : Type*} [integral_domain D] (S : set D) [is_subring S] : integral_domain S := { zero_ne_one := mt subtype.ext.1 zero_ne_one, eq_zero_or_eq_zero_of_mul_eq_zero := λ ⟨x, hx⟩ ⟨y, hy⟩, by { simp only [subtype.ext, subtype.coe_mk], exact eq_zero_or_eq_zero_of_mul_eq_zero }, .. subset.comm_ring } instance is_subring.inter (S₁ S₂ : set R) [is_subring S₁] [is_subring S₂] : is_subring (S₁ ∩ S₂) := { } instance is_subring.Inter {ι : Sort*} (S : ι → set R) [h : ∀ y : ι, is_subring (S y)] : is_subring (set.Inter S) := { } lemma is_subring_Union_of_directed {ι : Type*} [hι : nonempty ι] (s : ι → set R) [∀ i, is_subring (s i)] (directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) : is_subring (⋃i, s i) := { to_is_add_subgroup := is_add_subgroup_Union_of_directed s directed, to_is_submonoid := is_submonoid_Union_of_directed s directed } namespace ring def closure (s : set R) := add_group.closure (monoid.closure s) variable {s : set R} local attribute [reducible] closure theorem exists_list_of_mem_closure {a : R} (h : a ∈ closure s) : (∃ L : list (list R), (∀ l ∈ L, ∀ x ∈ l, x ∈ s ∨ x = (-1:R)) ∧ (L.map list.prod).sum = a) := add_group.in_closure.rec_on h (λ x hx, match x, monoid.exists_list_of_mem_closure hx with | _, ⟨L, h1, rfl⟩ := ⟨[L], list.forall_mem_singleton.2 (λ r hr, or.inl (h1 r hr)), zero_add _⟩ end) ⟨[], list.forall_mem_nil _, rfl⟩ (λ b _ ih, match b, ih with | _, ⟨L1, h1, rfl⟩ := ⟨L1.map (list.cons (-1)), λ L2 h2, match L2, list.mem_map.1 h2 with | _, ⟨L3, h3, rfl⟩ := list.forall_mem_cons.2 ⟨or.inr rfl, h1 L3 h3⟩ end, by simp only [list.map_map, (∘), list.prod_cons, neg_one_mul]; exact list.rec_on L1 neg_zero.symm (λ hd tl ih, by rw [list.map_cons, list.sum_cons, ih, list.map_cons, list.sum_cons, neg_add])⟩ end) (λ r1 r2 hr1 hr2 ih1 ih2, match r1, r2, ih1, ih2 with | _, _, ⟨L1, h1, rfl⟩, ⟨L2, h2, rfl⟩ := ⟨L1 ++ L2, list.forall_mem_append.2 ⟨h1, h2⟩, by rw [list.map_append, list.sum_append]⟩ end) @[elab_as_eliminator] protected theorem in_closure.rec_on {C : R → Prop} {x : R} (hx : x ∈ closure s) (h1 : C 1) (hneg1 : C (-1)) (hs : ∀ z ∈ s, ∀ n, C n → C (z * n)) (ha : ∀ {x y}, C x → C y → C (x + y)) : C x := begin have h0 : C 0 := add_neg_self (1:R) ▸ ha h1 hneg1, rcases exists_list_of_mem_closure hx with ⟨L, HL, rfl⟩, clear hx, induction L with hd tl ih, { exact h0 }, rw list.forall_mem_cons at HL, suffices : C (list.prod hd), { rw [list.map_cons, list.sum_cons], exact ha this (ih HL.2) }, replace HL := HL.1, clear ih tl, suffices : ∃ L : list R, (∀ x ∈ L, x ∈ s) ∧ (list.prod hd = list.prod L ∨ list.prod hd = -list.prod L), { rcases this with ⟨L, HL', HP | HP⟩, { rw HP, clear HP HL hd, induction L with hd tl ih, { exact h1 }, rw list.forall_mem_cons at HL', rw list.prod_cons, exact hs _ HL'.1 _ (ih HL'.2) }, rw HP, clear HP HL hd, induction L with hd tl ih, { exact hneg1 }, rw [list.prod_cons, neg_mul_eq_mul_neg], rw list.forall_mem_cons at HL', exact hs _ HL'.1 _ (ih HL'.2) }, induction hd with hd tl ih, { exact ⟨[], list.forall_mem_nil _, or.inl rfl⟩ }, rw list.forall_mem_cons at HL, rcases ih HL.2 with ⟨L, HL', HP | HP⟩; cases HL.1 with hhd hhd, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inl $ by rw [list.prod_cons, list.prod_cons, HP]⟩ }, { exact ⟨L, HL', or.inr $ by rw [list.prod_cons, hhd, neg_one_mul, HP]⟩ }, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inr $ by rw [list.prod_cons, list.prod_cons, HP, neg_mul_eq_mul_neg]⟩ }, { exact ⟨L, HL', or.inl $ by rw [list.prod_cons, hhd, HP, neg_one_mul, neg_neg]⟩ } end instance : is_subring (closure s) := { one_mem := add_group.mem_closure is_submonoid.one_mem, mul_mem := λ a b ha hb, add_group.in_closure.rec_on hb (λ b hb, add_group.in_closure.rec_on ha (λ a ha, add_group.subset_closure (is_submonoid.mul_mem ha hb)) ((zero_mul b).symm ▸ is_add_submonoid.zero_mem) (λ a ha hab, (neg_mul_eq_neg_mul a b) ▸ is_add_subgroup.neg_mem hab) (λ a c ha hc hab hcb, (add_mul a c b).symm ▸ is_add_submonoid.add_mem hab hcb)) ((mul_zero a).symm ▸ is_add_submonoid.zero_mem) (λ b hb hab, (neg_mul_eq_mul_neg a b) ▸ is_add_subgroup.neg_mem hab) (λ b c hb hc hab hac, (mul_add a b c).symm ▸ is_add_submonoid.add_mem hab hac), .. add_group.closure.is_add_subgroup _ } theorem mem_closure {a : R} : a ∈ s → a ∈ closure s := add_group.mem_closure ∘ @monoid.subset_closure _ _ _ _ theorem subset_closure : s ⊆ closure s := λ _, mem_closure theorem closure_subset {t : set R} [is_subring t] : s ⊆ t → closure s ⊆ t := add_group.closure_subset ∘ monoid.closure_subset theorem closure_subset_iff (s t : set R) [is_subring t] : closure s ⊆ t ↔ s ⊆ t := (add_group.closure_subset_iff _ t).trans ⟨set.subset.trans monoid.subset_closure, monoid.closure_subset⟩ theorem closure_mono {s t : set R} (H : s ⊆ t) : closure s ⊆ closure t := closure_subset $ set.subset.trans H subset_closure lemma image_closure {S : Type*} [ring S] (f : R →+* S) (s : set R) : f '' closure s = closure (f '' s) := le_antisymm begin rintros _ ⟨x, hx, rfl⟩, apply in_closure.rec_on hx; intros, { rw [f.map_one], apply is_submonoid.one_mem }, { rw [f.map_neg, is_monoid_hom.map_one f], apply is_add_subgroup.neg_mem, apply is_submonoid.one_mem }, { rw [f.map_mul], apply is_submonoid.mul_mem; solve_by_elim [subset_closure, set.mem_image_of_mem] }, { rw [f.map_add], apply is_add_submonoid.add_mem, assumption' }, end (closure_subset $ set.image_subset _ subset_closure) end ring
68735f9cb9063da710aaedcd45bcb1fbd55db941
9dc8cecdf3c4634764a18254e94d43da07142918
/test/norm_cast_lemma_order.lean
243f39b1e91c2bbbbb5102cd0e93e9d51d2758a5
[ "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
640
lean
import data.nat.cast import tactic.norm_cast constant ℝ : Type @[instance] constant real.add_monoid_with_one : add_monoid_with_one ℝ -- set_option trace.simplify.rewrite true set_option pp.notation false set_option pp.numerals false -- should work run_cmd norm_cast.numeral_to_coe `(0 : ℝ) run_cmd norm_cast.numeral_to_coe `(1 : ℝ) run_cmd norm_cast.numeral_to_coe `(2 : ℝ) run_cmd norm_cast.numeral_to_coe `(3 : ℝ) run_cmd norm_cast.coe_to_numeral `((0 : ℕ) : ℝ) run_cmd norm_cast.coe_to_numeral `((1 : ℕ) : ℝ) run_cmd norm_cast.coe_to_numeral `((2 : ℕ) : ℝ) run_cmd norm_cast.coe_to_numeral `((3 : ℕ) : ℝ)
1a0100a724f90a007508a65d2628ffea8ce8d931
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/io_state.lean
a126a41ca413f38938b0f4920f60ad5fda45ae6c
[ "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
315
lean
import system.io open io state_t variable [io.interface] @[reducible] def my_io := state_t nat io instance lift_io {α} : has_coe (io α) (my_io α) := ⟨state_t.lift⟩ def tst : my_io unit := do x ← read, print_ln x, write (x+10), y ← read, print_ln y, put_str "end of program" #eval tst 5
1ae0a36aade223d1706c1e5f960b479f2a207548
4fa161becb8ce7378a709f5992a594764699e268
/src/group_theory/semidirect_product.lean
d080f42f6f8f88032d5ef65e4f2c9944988b41fb
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
6,765
lean
/- Copyright (c) 2020 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.equiv.mul_add logic.function.basic group_theory.subgroup /-! # Semidirect product This file defines semidirect products of groups, and the canonical maps in and out of the semidirect product. The semidirect product of `N` and `G` given a hom `φ` from `φ` from `G` to the automorphism group of `N` is the product of sets with the group `⟨n₁, g₁⟩ * ⟨n₂, g₂⟩ = ⟨n₁ * φ g₁ n₂, g₁ * g₂⟩` ## Key definitions There are two homs into the semidirect product `inl : N →* N ⋊[φ] G` and `inr : G →* N ⋊[φ] G`, and `lift` can be used to define maps `N ⋊[φ] G →* H` out of the semidirect product given maps `f₁ : N →* H` and `f₂ : G →* H` that satisfy the condition `∀ n g, f₁ (φ g n) = f₂ g * f₁ n * f₂ g⁻¹` ## Notation This file introduces the global notation `N ⋊[φ] G` for `semidirect_product N G φ` ## Tags group, semidirect product -/ variables (N : Type*) (G : Type*) {H : Type*} [group N] [group G] [group H] /-- The semidirect product of groups `N` and `G`, given a map `φ` from `G` to the automorphism group of `N`. It the product of sets with the group operation `⟨n₁, g₁⟩ * ⟨n₂, g₂⟩ = ⟨n₁ * φ g₁ n₂, g₁ * g₂⟩` -/ @[ext, derive decidable_eq] structure semidirect_product (φ : G →* mul_aut N) := (left : N) (right : G) attribute [pp_using_anonymous_constructor] semidirect_product notation N` ⋊[`:35 φ:35`] `:0 G :35 := semidirect_product N G φ namespace semidirect_product variables {N G} {φ : G →* mul_aut N} private def one_aux : N ⋊[φ] G := ⟨1, 1⟩ private def mul_aux (a b : N ⋊[φ] G) : N ⋊[φ] G := ⟨a.1 * φ a.2 b.1, a.right * b.right⟩ private def inv_aux (a : N ⋊[φ] G) : N ⋊[φ] G := let i := a.2⁻¹ in ⟨φ i a.1⁻¹, i⟩ private lemma mul_assoc_aux (a b c : N ⋊[φ] G) : mul_aux (mul_aux a b) c = mul_aux a (mul_aux b c) := by simp [mul_aux, mul_assoc, mul_equiv.map_mul] private lemma mul_one_aux (a : N ⋊[φ] G) : mul_aux a one_aux = a := by cases a; simp [mul_aux, one_aux] private lemma one_mul_aux (a : N ⋊[φ] G) : mul_aux one_aux a = a := by cases a; simp [mul_aux, one_aux] private lemma mul_left_inv_aux (a : N ⋊[φ] G) : mul_aux (inv_aux a) a = one_aux := by simp only [mul_aux, inv_aux, one_aux, ← mul_equiv.map_mul, mul_left_inv]; simp instance : group (N ⋊[φ] G) := { one := one_aux, inv := inv_aux, mul := mul_aux, mul_assoc := mul_assoc_aux, one_mul := one_mul_aux, mul_one := mul_one_aux, mul_left_inv := mul_left_inv_aux } instance : inhabited (N ⋊[φ] G) := ⟨1⟩ @[simp] lemma one_left : (1 : N ⋊[φ] G).left = 1 := rfl @[simp] lemma one_right : (1 : N ⋊[φ] G).right = 1 := rfl @[simp] lemma inv_left (a : N ⋊[φ] G) : (a⁻¹).left = φ a.right⁻¹ a.left⁻¹ := rfl @[simp] lemma inv_right (a : N ⋊[φ] G) : (a⁻¹).right = a.right⁻¹ := rfl @[simp] lemma mul_left (a b : N ⋊[φ] G) : (a * b).left = a.left * φ a.right b.left := rfl @[simp] lemma mul_right (a b : N ⋊[φ] G) : (a * b).right = a.right * b.right := rfl /-- The canonical map `N →* N ⋊[φ] G` sending `n` to `⟨n, 1⟩` -/ def inl : N →* N ⋊[φ] G := { to_fun := λ n, ⟨n, 1⟩, map_one' := rfl, map_mul' := by intros; ext; simp } @[simp] lemma left_inl (n : N) : (inl n : N ⋊[φ] G).left = n := rfl @[simp] lemma right_inl (n : N) : (inl n : N ⋊[φ] G).right = 1 := rfl lemma inl_injective : function.injective (inl : N → N ⋊[φ] G) := function.injective_iff_has_left_inverse.2 ⟨left, left_inl⟩ @[simp] lemma inl_inj {n₁ n₂ : N} : (inl n₁ : N ⋊[φ] G) = inl n₂ ↔ n₁ = n₂ := inl_injective.eq_iff /-- The canonical map `G →* N ⋊[φ] G` sending `g` to `⟨1, g⟩` -/ def inr : G →* N ⋊[φ] G := { to_fun := λ g, ⟨1, g⟩, map_one' := rfl, map_mul' := by intros; ext; simp } @[simp] lemma left_inr (g : G) : (inr g : N ⋊[φ] G).left = 1 := rfl @[simp] lemma right_inr (g : G) : (inr g : N ⋊[φ] G).right = g := rfl lemma inr_injective : function.injective (inr : G → N ⋊[φ] G) := function.injective_iff_has_left_inverse.2 ⟨right, right_inr⟩ @[simp] lemma inr_inj {g₁ g₂ : G} : (inr g₁ : N ⋊[φ] G) = inr g₂ ↔ g₁ = g₂ := inr_injective.eq_iff lemma inl_aut (g : G) (n : N) : (inl (φ g n) : N ⋊[φ] G) = inr g * inl n * inr g⁻¹ := by ext; simp lemma inl_left_mul_inr_right (x : N ⋊[φ] G) : inl x.left * inr x.right = x := by ext; simp /-- The canonical projection map `N ⋊[φ] G →* G`, as a group hom. -/ def right_hom : N ⋊[φ] G →* G := { to_fun := semidirect_product.right, map_one' := rfl, map_mul' := λ _ _, rfl } @[simp] lemma right_hom_eq_right : (right_hom : N ⋊[φ] G → G) = right := rfl @[simp] lemma right_hom_comp_inl : (right_hom : N ⋊[φ] G →* G).comp inl = 1 := by ext; simp [right_hom] @[simp] lemma right_hom_comp_inr : (right_hom : N ⋊[φ] G →* G).comp inr = monoid_hom.id _ := by ext; simp [right_hom] @[simp] lemma right_hom_inl (n : N) : right_hom (inl n : N ⋊[φ] G) = 1 := by simp [right_hom] @[simp] lemma right_hom_inr (g : G) : right_hom (inr g : N ⋊[φ] G) = g := by simp [right_hom] lemma right_hom_surjective : function.surjective (right_hom : N ⋊[φ] G → G) := function.surjective_iff_has_right_inverse.2 ⟨inr, right_hom_inr⟩ lemma range_inl_eq_ker_right_hom : (inl : N →* N ⋊[φ] G).range = right_hom.ker := le_antisymm (λ _, by simp [monoid_hom.mem_ker, eq_comm] {contextual := tt}) (λ x hx, ⟨x.left, by ext; simp [*, monoid_hom.mem_ker] at *⟩) section lift variables (f₁ : N →* H) (f₂ : G →* H) (h : ∀ n g, f₁ (φ g n) = f₂ g * f₁ n * f₂ g⁻¹) /-- Define a group hom `N ⋊[φ] G →* H`, by defining maps `N →* H` and `G →* H` -/ def lift (f₁ : N →* H) (f₂ : G →* H) (h : ∀ n g, f₁ (φ g n) = f₂ g * f₁ n * f₂ g⁻¹) : N ⋊[φ] G →* H := { to_fun := λ a, f₁ a.1 * f₂ a.2, map_one' := by simp, map_mul' := λ a b, by simp [h, _root_.mul_assoc] } @[simp] lemma lift_inl (n : N) : lift f₁ f₂ h (inl n) = f₁ n := by simp [lift] @[simp] lemma lift_comp_inl : (lift f₁ f₂ h).comp inl = f₁ := by ext; simp @[simp] lemma lift_inr (g : G) : lift f₁ f₂ h (inr g) = f₂ g := by simp [lift] @[simp] lemma lift_comp_inr : (lift f₁ f₂ h).comp inr = f₂ := by ext; simp lemma lift_unique (F : N ⋊[φ] G →* H) : F = lift (F.comp inl) (F.comp inr) (by simp [inl_aut]) := begin ext, simp only [lift, monoid_hom.comp_apply, monoid_hom.coe_mk], rw [← F.map_mul, inl_left_mul_inr_right], end end lift end semidirect_product
1c7ee3ce6da3dc5fde0f6d3540a6d240a0b8a920
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/init/subtype.lean
a6304f5120d6c85cfee1632c1bb4f248f6655bab
[ "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
1,363
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad -/ prelude import init.datatypes init.logic open decidable set_option structure.proj_mk_thm true structure subtype {A : Type} (P : A → Prop) := tag :: (elt_of : A) (has_property : P elt_of) namespace subtype notation `{` binder ` | ` r:(scoped:1 P, subtype P) `}` := r definition exists_of_subtype {A : Type} {P : A → Prop} : { x | P x } → ∃ x, P x | (subtype.tag a h) := exists.intro a h variables {A : Type} {P : A → Prop} theorem tag_irrelevant {a : A} (H1 H2 : P a) : tag a H1 = tag a H2 := rfl theorem tag_eq {a1 a2 : A} {H1 : P a1} {H2 : P a2} (H3 : a1 = a2) : tag a1 H1 = tag a2 H2 := eq.subst H3 (tag_irrelevant H1) H2 protected theorem eq : ∀ {a1 a2 : {x | P x}} (H : elt_of a1 = elt_of a2), a1 = a2 | (tag x1 H1) (tag x2 H2) := tag_eq protected definition is_inhabited [instance] {a : A} (H : P a) : inhabited {x | P x} := inhabited.mk (tag a H) protected definition has_decidable_eq [instance] [H : decidable_eq A] : ∀ s₁ s₂ : {x | P x}, decidable (s₁ = s₂) | (tag v₁ p₁) (tag v₂ p₂) := decidable_of_decidable_of_iff (H v₁ v₂) (iff.intro tag_eq (λh, subtype.no_confusion h (λa b, a))) end subtype
3003cc7631f7650086786db49006cfced94a7334
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/category/Ring/filtered_colimits.lean
342960aea4128a621ad197264ddbbc57e76bfd19
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
13,210
lean
/- Copyright (c) 2021 Justus Springer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Justus Springer -/ import algebra.category.Ring.basic import algebra.category.Group.filtered_colimits /-! # The forgetful functor from (commutative) (semi-) rings preserves filtered colimits. Forgetful functors from algebraic categories usually don't preserve colimits. However, they tend to preserve _filtered_ colimits. In this file, we start with a small filtered category `J` and a functor `F : J ⥤ SemiRing`. We show that the colimit of `F ⋙ forget₂ SemiRing Mon` (in `Mon`) carries the structure of a semiring, thereby showing that the forgetful functor `forget₂ SemiRing Mon` preserves filtered colimits. In particular, this implies that `forget SemiRing` preserves filtered colimits. Similarly for `CommSemiRing`, `Ring` and `CommRing`. -/ universes v u noncomputable theory open_locale classical open category_theory open category_theory.limits open category_theory.is_filtered (renaming max → max') -- avoid name collision with `_root_.max`. open AddMon.filtered_colimits (colimit_zero_eq colimit_add_mk_eq) open Mon.filtered_colimits (colimit_one_eq colimit_mul_mk_eq) namespace SemiRing.filtered_colimits section -- We use parameters here, mainly so we can have the abbreviations `R` and `R.mk` below, without -- passing around `F` all the time. parameters {J : Type v} [small_category J] (F : J ⥤ SemiRing.{max v u}) -- This instance is needed below in `colimit_semiring`, during the verification of the -- semiring axioms. instance semiring_obj (j : J) : semiring (((F ⋙ forget₂ SemiRing Mon.{max v u}) ⋙ forget Mon).obj j) := show semiring (F.obj j), by apply_instance variables [is_filtered J] /-- The colimit of `F ⋙ forget₂ SemiRing Mon` in the category `Mon`. In the following, we will show that this has the structure of a semiring. -/ abbreviation R : Mon := Mon.filtered_colimits.colimit (F ⋙ forget₂ SemiRing Mon.{max v u}) instance colimit_semiring : semiring R := { mul_zero := λ x, begin apply quot.induction_on x, clear x, intro x, cases x with j x, erw [colimit_zero_eq _ j, colimit_mul_mk_eq _ ⟨j, _⟩ ⟨j, _⟩ j (𝟙 j) (𝟙 j)], rw [category_theory.functor.map_id, id_apply, id_apply, mul_zero x], refl, end, zero_mul := λ x, begin apply quot.induction_on x, clear x, intro x, cases x with j x, erw [colimit_zero_eq _ j, colimit_mul_mk_eq _ ⟨j, _⟩ ⟨j, _⟩ j (𝟙 j) (𝟙 j)], rw [category_theory.functor.map_id, id_apply, id_apply, zero_mul x], refl, end, left_distrib := λ x y z, begin apply quot.induction_on₃ x y z, clear x y z, intros x y z, cases x with j₁ x, cases y with j₂ y, cases z with j₃ z, let k := max₃ j₁ j₂ j₃, let f := first_to_max₃ j₁ j₂ j₃, let g := second_to_max₃ j₁ j₂ j₃, let h := third_to_max₃ j₁ j₂ j₃, erw [colimit_add_mk_eq _ ⟨j₂, _⟩ ⟨j₃, _⟩ k g h, colimit_mul_mk_eq _ ⟨j₁, _⟩ ⟨k, _⟩ k f (𝟙 k), colimit_mul_mk_eq _ ⟨j₁, _⟩ ⟨j₂, _⟩ k f g, colimit_mul_mk_eq _ ⟨j₁, _⟩ ⟨j₃, _⟩ k f h, colimit_add_mk_eq _ ⟨k, _⟩ ⟨k, _⟩ k (𝟙 k) (𝟙 k)], simp only [category_theory.functor.map_id, id_apply], erw left_distrib (F.map f x) (F.map g y) (F.map h z), refl, end, right_distrib := λ x y z, begin apply quot.induction_on₃ x y z, clear x y z, intros x y z, cases x with j₁ x, cases y with j₂ y, cases z with j₃ z, let k := max₃ j₁ j₂ j₃, let f := first_to_max₃ j₁ j₂ j₃, let g := second_to_max₃ j₁ j₂ j₃, let h := third_to_max₃ j₁ j₂ j₃, erw [colimit_add_mk_eq _ ⟨j₁, _⟩ ⟨j₂, _⟩ k f g, colimit_mul_mk_eq _ ⟨k, _⟩ ⟨j₃, _⟩ k (𝟙 k) h, colimit_mul_mk_eq _ ⟨j₁, _⟩ ⟨j₃, _⟩ k f h, colimit_mul_mk_eq _ ⟨j₂, _⟩ ⟨j₃, _⟩ k g h, colimit_add_mk_eq _ ⟨k, _⟩ ⟨k, _⟩ k (𝟙 k) (𝟙 k)], simp only [category_theory.functor.map_id, id_apply], erw right_distrib (F.map f x) (F.map g y) (F.map h z), refl, end, ..R.monoid, ..AddCommMon.filtered_colimits.colimit_add_comm_monoid (F ⋙ forget₂ SemiRing AddCommMon.{max v u}) } /-- The bundled semiring giving the filtered colimit of a diagram. -/ def colimit : SemiRing := SemiRing.of R /-- The cocone over the proposed colimit semiring. -/ def colimit_cocone : cocone F := { X := colimit, ι := { app := λ j, { ..(Mon.filtered_colimits.colimit_cocone (F ⋙ forget₂ SemiRing Mon.{max v u})).ι.app j, ..(AddCommMon.filtered_colimits.colimit_cocone (F ⋙ forget₂ SemiRing AddCommMon.{max v u})).ι.app j }, naturality' := λ j j' f, (ring_hom.coe_inj ((types.colimit_cocone (F ⋙ forget SemiRing)).ι.naturality f)) } } /-- The proposed colimit cocone is a colimit in `SemiRing`. -/ def colimit_cocone_is_colimit : is_colimit colimit_cocone := { desc := λ t, { .. (Mon.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ SemiRing Mon.{max v u})).desc ((forget₂ SemiRing Mon.{max v u}).map_cocone t), .. (AddCommMon.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ SemiRing AddCommMon.{max v u})).desc ((forget₂ SemiRing AddCommMon.{max v u}).map_cocone t), }, fac' := λ t j, ring_hom.coe_inj $ (types.colimit_cocone_is_colimit (F ⋙ forget SemiRing)).fac ((forget SemiRing).map_cocone t) j, uniq' := λ t m h, ring_hom.coe_inj $ (types.colimit_cocone_is_colimit (F ⋙ forget SemiRing)).uniq ((forget SemiRing).map_cocone t) m (λ j, funext $ λ x, ring_hom.congr_fun (h j) x) } instance forget₂_Mon_preserves_filtered_colimits : preserves_filtered_colimits (forget₂ SemiRing Mon.{u}) := { preserves_filtered_colimits := λ J _ _, by exactI { preserves_colimit := λ F, preserves_colimit_of_preserves_colimit_cocone (colimit_cocone_is_colimit.{u u} F) (Mon.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ SemiRing Mon.{u})) } } instance forget_preserves_filtered_colimits : preserves_filtered_colimits (forget SemiRing.{u}) := limits.comp_preserves_filtered_colimits (forget₂ SemiRing Mon) (forget Mon.{u}) end end SemiRing.filtered_colimits namespace CommSemiRing.filtered_colimits section -- We use parameters here, mainly so we can have the abbreviation `R` below, without -- passing around `F` all the time. parameters {J : Type v} [small_category J] [is_filtered J] (F : J ⥤ CommSemiRing.{max v u}) /-- The colimit of `F ⋙ forget₂ CommSemiRing SemiRing` in the category `SemiRing`. In the following, we will show that this has the structure of a _commutative_ semiring. -/ abbreviation R : SemiRing := SemiRing.filtered_colimits.colimit (F ⋙ forget₂ CommSemiRing SemiRing.{max v u}) instance colimit_comm_semiring : comm_semiring R := { ..R.semiring, ..CommMon.filtered_colimits.colimit_comm_monoid (F ⋙ forget₂ CommSemiRing CommMon.{max v u}) } /-- The bundled commutative semiring giving the filtered colimit of a diagram. -/ def colimit : CommSemiRing := CommSemiRing.of R /-- The cocone over the proposed colimit commutative semiring. -/ def colimit_cocone : cocone F := { X := colimit, ι := { ..(SemiRing.filtered_colimits.colimit_cocone (F ⋙ forget₂ CommSemiRing SemiRing.{max v u})).ι } } /-- The proposed colimit cocone is a colimit in `CommSemiRing`. -/ def colimit_cocone_is_colimit : is_colimit colimit_cocone := { desc := λ t, (SemiRing.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ CommSemiRing SemiRing.{max v u})).desc ((forget₂ CommSemiRing SemiRing).map_cocone t), fac' := λ t j, ring_hom.coe_inj $ (types.colimit_cocone_is_colimit (F ⋙ forget CommSemiRing)).fac ((forget CommSemiRing).map_cocone t) j, uniq' := λ t m h, ring_hom.coe_inj $ (types.colimit_cocone_is_colimit (F ⋙ forget CommSemiRing)).uniq ((forget CommSemiRing).map_cocone t) m (λ j, funext $ λ x, ring_hom.congr_fun (h j) x) } instance forget₂_SemiRing_preserves_filtered_colimits : preserves_filtered_colimits (forget₂ CommSemiRing SemiRing.{u}) := { preserves_filtered_colimits := λ J _ _, by exactI { preserves_colimit := λ F, preserves_colimit_of_preserves_colimit_cocone (colimit_cocone_is_colimit.{u u} F) (SemiRing.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ CommSemiRing SemiRing.{u})) } } instance forget_preserves_filtered_colimits : preserves_filtered_colimits (forget CommSemiRing.{u}) := limits.comp_preserves_filtered_colimits (forget₂ CommSemiRing SemiRing) (forget SemiRing.{u}) end end CommSemiRing.filtered_colimits namespace Ring.filtered_colimits section -- We use parameters here, mainly so we can have the abbreviation `R` below, without -- passing around `F` all the time. parameters {J : Type v} [small_category J] [is_filtered J] (F : J ⥤ Ring.{max v u}) /-- The colimit of `F ⋙ forget₂ Ring SemiRing` in the category `SemiRing`. In the following, we will show that this has the structure of a ring. -/ abbreviation R : SemiRing := SemiRing.filtered_colimits.colimit (F ⋙ forget₂ Ring SemiRing.{max v u}) instance colimit_ring : ring R := { ..R.semiring, ..AddCommGroup.filtered_colimits.colimit_add_comm_group (F ⋙ forget₂ Ring AddCommGroup.{max v u}) } /-- The bundled ring giving the filtered colimit of a diagram. -/ def colimit : Ring := Ring.of R /-- The cocone over the proposed colimit ring. -/ def colimit_cocone : cocone F := { X := colimit, ι := { ..(SemiRing.filtered_colimits.colimit_cocone (F ⋙ forget₂ Ring SemiRing.{max v u})).ι } } /-- The proposed colimit cocone is a colimit in `Ring`. -/ def colimit_cocone_is_colimit : is_colimit colimit_cocone := { desc := λ t, (SemiRing.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ Ring SemiRing.{max v u})).desc ((forget₂ Ring SemiRing).map_cocone t), fac' := λ t j, ring_hom.coe_inj $ (types.colimit_cocone_is_colimit (F ⋙ forget Ring)).fac ((forget Ring).map_cocone t) j, uniq' := λ t m h, ring_hom.coe_inj $ (types.colimit_cocone_is_colimit (F ⋙ forget Ring)).uniq ((forget Ring).map_cocone t) m (λ j, funext $ λ x, ring_hom.congr_fun (h j) x) } instance forget₂_SemiRing_preserves_filtered_colimits : preserves_filtered_colimits (forget₂ Ring SemiRing.{u}) := { preserves_filtered_colimits := λ J _ _, by exactI { preserves_colimit := λ F, preserves_colimit_of_preserves_colimit_cocone (colimit_cocone_is_colimit.{u u} F) (SemiRing.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ Ring SemiRing.{u})) } } instance forget_preserves_filtered_colimits : preserves_filtered_colimits (forget Ring.{u}) := limits.comp_preserves_filtered_colimits (forget₂ Ring SemiRing) (forget SemiRing.{u}) end end Ring.filtered_colimits namespace CommRing.filtered_colimits section -- We use parameters here, mainly so we can have the abbreviation `R` below, without -- passing around `F` all the time. parameters {J : Type v} [small_category J] [is_filtered J] (F : J ⥤ CommRing.{max v u}) /-- The colimit of `F ⋙ forget₂ CommRing Ring` in the category `Ring`. In the following, we will show that this has the structure of a _commutative_ ring. -/ abbreviation R : Ring := Ring.filtered_colimits.colimit (F ⋙ forget₂ CommRing Ring.{max v u}) instance colimit_comm_ring : comm_ring R := { ..R.ring, ..CommSemiRing.filtered_colimits.colimit_comm_semiring (F ⋙ forget₂ CommRing CommSemiRing.{max v u}) } /-- The bundled commutative ring giving the filtered colimit of a diagram. -/ def colimit : CommRing := CommRing.of R /-- The cocone over the proposed colimit commutative ring. -/ def colimit_cocone : cocone F := { X := colimit, ι := { ..(Ring.filtered_colimits.colimit_cocone (F ⋙ forget₂ CommRing Ring.{max v u})).ι } } /-- The proposed colimit cocone is a colimit in `CommRing`. -/ def colimit_cocone_is_colimit : is_colimit colimit_cocone := { desc := λ t, (Ring.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ CommRing Ring.{max v u})).desc ((forget₂ CommRing Ring).map_cocone t), fac' := λ t j, ring_hom.coe_inj $ (types.colimit_cocone_is_colimit (F ⋙ forget CommRing)).fac ((forget CommRing).map_cocone t) j, uniq' := λ t m h, ring_hom.coe_inj $ (types.colimit_cocone_is_colimit (F ⋙ forget CommRing)).uniq ((forget CommRing).map_cocone t) m (λ j, funext $ λ x, ring_hom.congr_fun (h j) x) } instance forget₂_Ring_preserves_filtered_colimits : preserves_filtered_colimits (forget₂ CommRing Ring.{u}) := { preserves_filtered_colimits := λ J _ _, by exactI { preserves_colimit := λ F, preserves_colimit_of_preserves_colimit_cocone (colimit_cocone_is_colimit.{u u} F) (Ring.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ CommRing Ring.{u})) } } instance forget_preserves_filtered_colimits : preserves_filtered_colimits (forget CommRing.{u}) := limits.comp_preserves_filtered_colimits (forget₂ CommRing Ring) (forget Ring.{u}) end end CommRing.filtered_colimits
dbd41d36761ec92c24cbf9e9277cbd26bf169686
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/div_wf.lean
1fc1c8970527f88140b5c1c6ca37d462454b6698
[ "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
1,741
lean
open nat well_founded decidable prod set_option pp.all true -- Auxiliary lemma used to justify recursive call private definition lt_aux {x y : nat} (H : 0 < y ∧ y ≤ x) : x - y < x := and.rec_on H (λ ypos ylex, sub_lt (nat.lt_of_lt_of_le ypos ylex) ypos) definition wdiv.F (x : nat) (f : Π x₁, x₁ < x → nat → nat) (y : nat) : nat := if H : 0 < y ∧ y ≤ x then f (x - y) (lt_aux H) y + 1 else 0 definition wdiv (x y : nat) := fix lt_wf wdiv.F x y theorem wdiv_def (x y : nat) : wdiv x y = if H : 0 < y ∧ y ≤ x then wdiv (x - y) y + 1 else 0 := congr_fun (well_founded.fix_eq lt_wf wdiv.F x) y /- See comment at fib_wrec. example : wdiv 5 2 = 2 := rfl example : wdiv 9 3 = 3 := rfl -/ -- There is a little bit of cheating in the definition above. -- I avoid the packing/unpacking into tuples. -- The actual definitional package would not do that. -- It will always pack things. definition pair_nat.lt := lex nat.lt nat.lt -- Could also be (lex lt empty_rel) definition pair_nat.lt.wf : well_founded pair_nat.lt := prod.lex_wf lt_wf lt_wf infixl `≺`:50 := pair_nat.lt -- Recursive lemma used to justify recursive call definition plt_aux (x y : nat) (H : 0 < y ∧ y ≤ x) : (x - y, y) ≺ (x, y) := lex.left _ _ _ (lt_aux H) definition pdiv.F (p₁ : nat × nat) : (Π p₂ : nat × nat, p₂ ≺ p₁ → nat) → nat := prod.cases_on p₁ (λ x y f, if H : 0 < y ∧ y ≤ x then f (x - y, y) (plt_aux x y H) + 1 else 0) definition pdiv (x y : nat) := fix pair_nat.lt.wf pdiv.F (x, y) theorem pdiv_def (x y : nat) : pdiv x y = if H : 0 < y ∧ y ≤ x then pdiv (x - y) y + 1 else 0 := well_founded.fix_eq pair_nat.lt.wf pdiv.F (x, y) /- See comment at fib_wrec. example : pdiv 17 2 = 8 := rfl -/
d7e726b2dabe72312fb8e7917bac8ad403318f64
64874bd1010548c7f5a6e3e8902efa63baaff785
/library/init/datatypes.lean
882f0b3488eb5890e4110734a253dc0874e38f76
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,716
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura Basic datatypes -/ prelude notation `Prop` := Type.{0} notation [parsing-only] `Type'` := Type.{_+1} notation [parsing-only] `Type₊` := Type.{_+1} notation `Type₁` := Type.{1} notation `Type₂` := Type.{2} notation `Type₃` := Type.{3} set_option structure.eta_thm true set_option structure.proj_mk_thm true inductive unit.{l} : Type.{l} := star : unit inductive true : Prop := intro : true inductive false : Prop inductive empty : Type inductive eq {A : Type} (a : A) : A → Prop := refl : eq a a inductive heq {A : Type} (a : A) : Π {B : Type}, B → Prop := refl : heq a a structure prod (A B : Type) := mk :: (pr1 : A) (pr2 : B) inductive and (a b : Prop) : Prop := intro : a → b → and a b definition and.elim_left {a b : Prop} (H : and a b) : a := and.rec (λa b, a) H definition and.left := @and.elim_left definition and.elim_right {a b : Prop} (H : and a b) : b := and.rec (λa b, b) H definition and.right := @and.elim_right inductive sum (A B : Type) : Type := inl {} : A → sum A B, inr {} : B → sum A B definition sum.intro_left [reducible] {A : Type} (B : Type) (a : A) : sum A B := sum.inl a definition sum.intro_right [reducible] (A : Type) {B : Type} (b : B) : sum A B := sum.inr b inductive or (a b : Prop) : Prop := inl {} : a → or a b, inr {} : b → or a b definition or.intro_left {a : Prop} (b : Prop) (Ha : a) : or a b := or.inl Ha definition or.intro_right (a : Prop) {b : Prop} (Hb : b) : or a b := or.inr Hb -- pos_num and num are two auxiliary datatypes used when parsing numerals such as 13, 0, 26. -- The parser will generate the terms (pos (bit1 (bit1 (bit0 one)))), zero, and (pos (bit0 (bit1 (bit1 one)))). -- This representation can be coerced in whatever we want (e.g., naturals, integers, reals, etc). inductive pos_num : Type := one : pos_num, bit1 : pos_num → pos_num, bit0 : pos_num → pos_num namespace pos_num definition succ (a : pos_num) : pos_num := rec_on a (bit0 one) (λn r, bit0 r) (λn r, bit1 n) end pos_num inductive num : Type := zero : num, pos : pos_num → num namespace num open pos_num definition succ (a : num) : num := rec_on a (pos one) (λp, pos (succ p)) end num inductive bool : Type := ff : bool, tt : bool inductive char : Type := mk : bool → bool → bool → bool → bool → bool → bool → bool → char inductive string : Type := empty : string, str : char → string → string inductive nat := zero : nat, succ : nat → nat inductive option (A : Type) : Type := none {} : option A, some : A → option A
ff994692312fae771631a5528dab06e043dc8a89
5df84495ec6c281df6d26411cc20aac5c941e745
/src/formal_ml/alt_pac_bounds.lean
9f3fdf692bac7e7485223e185203ea0b51b08f08
[ "Apache-2.0" ]
permissive
eric-wieser/formal-ml
e278df5a8df78aa3947bc8376650419e1b2b0a14
630011d19fdd9539c8d6493a69fe70af5d193590
refs/heads/master
1,681,491,589,256
1,612,642,743,000
1,612,642,743,000
360,114,136
0
0
Apache-2.0
1,618,998,189,000
1,618,998,188,000
null
UTF-8
Lean
false
false
32,249
lean
/- Copyright 2020 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -/ import measure_theory.measurable_space import measure_theory.measure_space import measure_theory.outer_measure import measure_theory.lebesgue_measure import measure_theory.integration import measure_theory.borel_space import data.set.countable import formal_ml.measurable_space import formal_ml.probability_space import formal_ml.real_random_variable import data.complex.exponential import formal_ml.ennreal import formal_ml.nnreal import formal_ml.sum import formal_ml.exp_bound import formal_ml.classical namespace alt_pac /- A first option is to define our concepts minimally. -/ def raw_error {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) {β:Type*} (X_test:Ω → (β × bool)) (h:set β):ennreal := μ (X_test ⁻¹' (h.prod {ff} ∪ (hᶜ).prod {tt})) def raw_false_positive_rate {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) {β:Type*} (X_test:Ω → (β × bool)) (h:set β):ennreal := μ (X_test ⁻¹' (h.prod {ff})) def raw_false_negative_rate {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) {β:Type*} (X_test:Ω → (β × bool)) (h:set β):ennreal := μ (X_test ⁻¹' (hᶜ.prod {tt})) def raw_error_eq_raw_fpr_add_raw_fnr {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) {β:Type*} (X_test:Ω → (β × bool)) (h:set β):Prop := raw_error μ X_test h = raw_false_positive_rate μ X_test h + raw_false_negative_rate μ X_test h /- However, this leads us to require additional constraints for concepts that should always be true. -/ lemma measurable_error_eq_fpr_add_fnr {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) {β:Type*} (X_test:Ω → (β × bool)) (h:set β) [measurable_space β]:(measurable_set h) → (measurable X_test) → raw_error_eq_raw_fpr_add_raw_fnr μ X_test h := begin intros h_h_meas h_X_test_measurable, simp only [raw_error_eq_raw_fpr_add_raw_fnr, raw_false_negative_rate, raw_false_positive_rate, raw_error, set.preimage_union], rw measure_theory.measure_union, { rw set.disjoint_iff, apply set.disjoint_preimage, rw set.subset_def, intros x, intros h_contra, simp at h_contra, cases h_contra with h_contra_fp h_contra_fn, cases h_contra_fp with x' h_contra_fp, cases h_contra_fn with x'' h_contra_fn, cases h_contra_fp with h_contra_fp_pos h_contra_fp_false, cases h_contra_fn with h_contra_fn_neg h_contra_fn_true, subst h_contra_fp_false, simp at h_contra_fn_true, apply false.elim h_contra_fn_true, }, apply measurable_elim, apply h_X_test_measurable, apply measurable_set.prod, apply h_h_meas, apply measurable_space.measurable_set_top, apply measurable_elim, apply h_X_test_measurable, apply measurable_set.prod, apply measurable_set.compl, apply h_h_meas, apply measurable_space.measurable_set_top, end /-Another possibility is to require measurability as error is defined. -/ def meas_error {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) [measure_theory.probability_measure μ] {β:Type*} [measurable_space β] (X_test:Ω → (β × bool)) (h_meas_X_test:measurable X_test) (h:set β) (h_h_meas:measurable_set h):ennreal := μ (X_test ⁻¹' (h.prod {ff} ∪ (hᶜ).prod {tt})) lemma meas_error_eq_raw_error {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) [measure_theory.probability_measure μ] {β:Type*} [measurable_space β] (X_test:Ω → (β × bool)) (h_meas_X_test:measurable X_test) (h:set β) (h_h_meas:measurable_set h): meas_error μ X_test h_meas_X_test h h_h_meas = raw_error μ X_test h := rfl def meas_false_positive_rate {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) [measure_theory.probability_measure μ] {β:Type*} [measurable_space β] (X_test:Ω → (β × bool)) (h_meas_X_test:measurable X_test) (h:set β) (h_h_meas:measurable_set h):ennreal := μ (X_test ⁻¹' (h.prod {ff})) lemma meas_false_positive_rate_eq_raw_false_positive_rate {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) [measure_theory.probability_measure μ] {β:Type*} [measurable_space β] (X_test:Ω → (β × bool)) (h_meas_X_test:measurable X_test) (h:set β) (h_h_meas:measurable_set h): meas_false_positive_rate μ X_test h_meas_X_test h h_h_meas = raw_false_positive_rate μ X_test h := rfl def meas_false_negative_rate {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) [measure_theory.probability_measure μ] {β:Type*} [measurable_space β] (X_test:Ω → (β × bool)) (h_meas_X_test:measurable X_test) (h:set β) (h_h_meas:measurable_set h):ennreal := μ (X_test ⁻¹' (hᶜ.prod {tt})) lemma meas_false_negative_rate_eq_raw_false_negative_rate {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) [measure_theory.probability_measure μ] {β:Type*} [measurable_space β] (X_test:Ω → (β × bool)) (h_meas_X_test:measurable X_test) (h:set β) (h_h_meas:measurable_set h): meas_false_negative_rate μ X_test h_meas_X_test h h_h_meas = raw_false_negative_rate μ X_test h := rfl def meas_error_eq_meas_fpr_add_meas_fnr {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) [measure_theory.probability_measure μ] {β:Type*} [measurable_space β] (X_test:Ω → (β × bool)) (h_meas_X_test:measurable X_test) (h:set β) (h_h_meas:measurable_set h):Prop := meas_error μ X_test h_meas_X_test h h_h_meas = meas_false_positive_rate μ X_test h_meas_X_test h h_h_meas + meas_false_negative_rate μ X_test h_meas_X_test h h_h_meas lemma meas_error_eq_meas_fpr_add_meas_fnr' {Ω:Type*} [M:measurable_space Ω] (μ:measure_theory.measure Ω) [measure_theory.probability_measure μ] {β:Type*} [measurable_space β] (X_test:Ω → (β × bool)) (h_meas_X_test:measurable X_test) (h:set β) (h_h_meas:measurable_set h): meas_error_eq_meas_fpr_add_meas_fnr μ X_test h_meas_X_test h h_h_meas := begin unfold meas_error_eq_meas_fpr_add_meas_fnr, rw meas_error_eq_raw_error, rw meas_false_positive_rate_eq_raw_false_positive_rate, rw meas_false_negative_rate_eq_raw_false_negative_rate, apply measurable_error_eq_fpr_add_fnr, apply h_h_meas, apply h_meas_X_test, end end alt_pac /- A final approach is to embed measurability within the arguments themselves. We can add a great deal of syntactic sugar as well. -/ section error_def variables {Ω:Type*} {p:probability_space Ω} {β:Type*} {Mβ:measurable_space β} variables (X_test:p →ᵣ (Mβ ×ₘ bool.measurable_space)) (h:measurable_setB Mβ) def error:nnreal := Pr[(X_test.fst ∈ᵣ h ∧ X_test.snd =ᵣ ff) ∨ (X_test.fst ∉ᵣ h) ∧ (X_test.snd =ᵣ tt)] def false_positive_rate:nnreal := Pr[(X_test.fst ∈ᵣ h ∧ X_test.snd =ᵣ ff)] def false_negative_rate:nnreal := Pr[(X_test.fst ∉ᵣ h) ∧ (X_test.snd =ᵣ tt)] lemma error_eq_fpr_add_fnr:error X_test h = false_positive_rate X_test h + false_negative_rate X_test h := begin simp only [error, false_positive_rate, false_negative_rate], apply Pr_disjoint_eor, simp, rw set.disjoint_iff, apply set.subset.trans, apply set.inter_subset_inter, apply set.inter_subset_left, apply set.inter_subset_left, rw set.inter_compl_self, end end error_def /-A concept of PAC bounds separated from the method of generating a hypothesis. -/ section pac_def variables {Ω:Type*} {p:probability_space Ω} {β:Type*} {Mβ:measurable_space β} (X_test:p →ᵣ (Mβ ×ₘ bool.measurable_space)) {H:Type*} (R:H → measurable_setB Mβ) [TM:top_measurable H] (A:p →ᵣ TM.to_measurable_space) def error_fun:H → nnreal := (λ (h:H), error X_test (R h)) noncomputable def error_mf:(@top_measurable.to_measurable_space H TM) →ₘ nnreal.measurable_space := top_measurable_fun (λ h, error X_test (R h)) _ /- A generic variant of probably approximately correct. This is the probability that a hypothesis drawn from some arbitrary distribution A is probably approximately correct. -/ def gen_probably_approximately_correct (δ ε:nnreal):Prop := Pr[((error_mf X_test R) ∘r A) ≤ᵣ ε] ≥ 1 - δ /- A problem is realizable if there exists a representation for a correct hypothesis. -/ def realizable:Prop := ∃ (h:H), error X_test (R h) = 0 lemma gen_probably_approximately_correct_trivial (ε:nnreal): gen_probably_approximately_correct X_test R A 1 ε := begin simp [gen_probably_approximately_correct], end lemma gen_probably_approximately_correct_mono (δ δ' ε:nnreal): (δ ≤ δ') → gen_probably_approximately_correct X_test R A δ ε → gen_probably_approximately_correct X_test R A δ' ε := begin simp [gen_probably_approximately_correct], intros h1 h2, apply le_trans, apply h2, apply add_le_add, apply le_refl _, apply h1, end end pac_def section pac_def variables {Ω:Type*} {p:probability_space Ω} {β:Type*} [Mβ:measurable_space β] (X_test:p →ᵣ (Mβ ×ₘ bool.measurable_space)) {D:Type*} (X_train:D → (p →ᵣ (Mβ ×ₘ bool.measurable_space))) {H:Type*} [TM:top_measurable H] (A:(Πₘ (d:D), Mβ ×ₘ bool.measurable_space) →ₘ TM.to_measurable_space) (R:H → measurable_setB Mβ) def algorithm_rv:p →ᵣ TM.to_measurable_space := A ∘r (pi.random_variable_combine X_train) /- A proposition that an algorithm based upon training data X_train is probably approximately correct. -/ def probably_approximately_correct (δ ε:nnreal):Prop := gen_probably_approximately_correct X_test R (algorithm_rv X_train A) δ ε def example_error (h:H) (d:D):event p := (((X_train d).fst ∈ᵣ (R h)) ∧ ((X_train d).snd =ᵣ ff)) ∨ (((X_train d).fst ∉ᵣ (R h)) ∧ ((X_train d).snd =ᵣ tt)) /- Rather than consider these concepts as events, we insist that they hold for all instances. -/ def consistent_hypothesis (x:D→ β × bool) (h:H):Prop := ∀ d:D, (x d).fst ∈ (R h).val ↔ (x d).snd def consistent_algorithm:Prop := ∀ x:(D → (β × bool)), (∃ (c:H), consistent_hypothesis R x c) → (consistent_hypothesis R x (A.val x)) lemma probably_approximately_correct_trivial (ε:nnreal): probably_approximately_correct X_test X_train A R 1 ε := begin simp [probably_approximately_correct], apply gen_probably_approximately_correct_trivial, end lemma probably_approximately_correct_mono (δ δ' ε:nnreal): (δ ≤ δ') → probably_approximately_correct X_test X_train A R δ ε → probably_approximately_correct X_test X_train A R δ' ε := begin simp [probably_approximately_correct], intros h1 h2, apply gen_probably_approximately_correct_mono , apply h1, apply h2, end end pac_def lemma const_measurable_fun_val {Ω:Type*} {M:measurable_space Ω} (x:nnreal): ((↑x:(M →ₘ nnreal.measurable_space)).val) = (λ ω:Ω, x) := rfl section pac_proof variables {Ω:Type*} {p:probability_space Ω} {β:Type*} [Mβ:measurable_space β] (X_test:p →ᵣ (Mβ ×ₘ bool.measurable_space)) {D:Type*} (X_train:D → (p →ᵣ (Mβ ×ₘ bool.measurable_space))) {H:Type*} [TM:top_measurable H] (A:(Πₘ (d:D), Mβ ×ₘ bool.measurable_space) →ₘ TM.to_measurable_space) (R:H → measurable_setB Mβ) lemma error_as_measurable_setB (h:H) (Y:p →ᵣ (Mβ ×ₘ bool.measurable_space)): ((mf_fst ∘r Y) ∈ᵣ R h∧(mf_snd ∘r Y) =ᵣ ↑ff ∨ (mf_fst ∘r Y)∉ᵣ R h∧(mf_snd ∘r Y) =ᵣ ↑tt) = Y ∈ᵣ ((R h).prod (measurable_setB_top {ff}) ∪ ((R h)ᶜ).prod (measurable_setB_top {tt})) := begin apply event.eq, simp, ext ω, simp, split; intros A1; cases A1, { left, apply exists.intro (Y.val ω).fst, simp [A1], rw ← A1.right, simp }, { right, apply exists.intro (Y.val ω).fst, simp [A1], rw ← A1.right, simp }, { left, cases A1 with x A1, rw ← A1.right, simp [A1] }, { right, cases A1 with x A1, rw ← A1.right, simp [A1] } end lemma Pr_example_error_eq_error (h:H) (d:D): (random_variable_identical X_test (X_train d)) → Pr[example_error X_train R h d] = error X_test (R h) := begin intros h1, simp [error], simp [example_error], rw error_as_measurable_setB R h (X_train d), rw error_as_measurable_setB R h X_test, symmetry, apply h1, end /- The average number of errors for a hypothesis in the training data, as a random variable. -/ noncomputable def training_error (h:H) [FD:fintype D]:p →ᵣ (borel nnreal) := average_identifier (example_error X_train R h) FD def hypothesis_consistent_event (h:H) [FD:fintype D]: event p := (∀ᵣ (d:D), enot (example_error X_train R h d)) lemma in_hypothesis_consistent_event_iff_consistent_hypothesis (h:H) [FD:fintype D] (ω:Ω): ω ∈ (hypothesis_consistent_event X_train R h) ↔ (consistent_hypothesis R (λ (d:D), (X_train d).val ω) h) := begin classical, simp [hypothesis_consistent_event], split, { intros h1, rw event_mem_val at h1, simp only [consistent_hypothesis], intros d, simp at h1, have h2 := h1 d, rw ← subtype.val_eq_coe at h2, simp [example_error] at h2, rw decidable.not_or_iff_and_not at h2, cases h2 with h2 h3, rw not_and at h2, rw and_comm at h3, rw not_and at h3, simp at h3, simp at h2, split, intros h4, simp at h4, apply h2, apply h4, intros h5, simp, apply h3, apply h5 }, { intros h1, rw event_mem_val, simp only [consistent_hypothesis] at h1, simp, intros d, simp [example_error], rw ← subtype.val_eq_coe, simp, rw decidable.not_or_iff_and_not, have h2 := h1 d, simp at h2, split, { rw not_and, intros h3, simp, rw h2 at h3, apply h3 }, { rw and_comm, rw not_and, intros h3, simp, rw h2, apply h3, }, }, end def he_hypothesis_consistent_event (h:H) [FD:fintype D] (ε:nnreal): event p := (hypothesis_consistent_event X_train R h) ∧ event_const (error X_test (R h) > ε) --set_option trace.class_instances true def he_hypothesis_consistent_pac [encodable H] [fintype D] (δ ε:nnreal):Prop := Pr[∃ᵣ (h:H), he_hypothesis_consistent_event X_test X_train R h ε] ≤ δ /- A realizable hypothesis has probability one of being consistent, i.e. almost surely it is consistent. -/ lemma exists_consistent_hypothesis_of_realizable [fintype D]:realizable X_test R → (∀ (d:D), random_variable_identical X_test (X_train d)) → ∃ (h:H), Pr[hypothesis_consistent_event X_train R h]=1 := begin classical, intros h1 h2, simp [realizable] at h1, cases h1 with h h1, existsi [h], rw ← Pr_one_minus_not_eq, simp [hypothesis_consistent_event], rw not_forall_not_eq_exists', have h3:Pr[∃ᵣ (a : D), @example_error Ω p β Mβ D X_train H R h a] = 0, { rw ← le_zero_iff, apply le_trans, apply eany_fintype_bound, rw tsum_fintype, have h3_1:(λ (d:D), Pr[@example_error Ω p β Mβ D X_train H R h d]) =(λ (d:D), 0), { ext1 d, rw Pr_example_error_eq_error, apply h1, apply h2 }, rw h3_1, simp, }, rw h3, simp, end lemma approximately_correct_if_he_hypothesis_consistent [encodable H] [fintype D] [nonempty D] (ε:nnreal) (h:H): (Pr[hypothesis_consistent_event X_train R h] = 1) → (consistent_algorithm A R) → (((∃ᵣ (h:H), he_hypothesis_consistent_event X_test X_train R h ε)ᶜ ∧ hypothesis_consistent_event X_train R h ).val) ⊆ ((error_mf X_test R ∘r algorithm_rv X_train A) ≤ᵣ ε).val := begin intros h0 h1, rw set.subset_def, intros ω h2, simp only [error_mf, top_measurable_fun, algorithm_rv, pi.measurable_fun, set.mem_set_of_eq, subtype.val_eq_coe, event_le_val_def], rw ← subtype.val_eq_coe, rw ← subtype.val_eq_coe, rw const_measurable_fun_val, simp, simp [he_hypothesis_consistent_event, const_measurable_fun] at h2, let h':H := A.val (λ (d : D), (X_train d).val ω), begin have B2:error X_test (R h') ≤ ε, { cases h2 with h2 h3, apply h2, rw ← subtype.val_eq_coe, rw ← event_mem_val, rw in_hypothesis_consistent_event_iff_consistent_hypothesis, simp [consistent_algorithm] at h1, simp [h'], apply h1, rw ← subtype.val_eq_coe at h3, rw ← event_mem_val at h3, rw in_hypothesis_consistent_event_iff_consistent_hypothesis at h3, apply h3 }, apply B2, end end lemma consistent_pac [encodable H] [fintype D] [nonempty D] (δ ε:nnreal):(realizable X_test R) → (∀ (d:D), random_variable_identical X_test (X_train d)) → (he_hypothesis_consistent_pac X_test X_train R δ ε) → (consistent_algorithm A R) → (probably_approximately_correct X_test X_train A R δ ε) := begin intros h_realizable h_identical h1 h2, unfold probably_approximately_correct gen_probably_approximately_correct, unfold he_hypothesis_consistent_pac at h1, have h3:∃ (h:H), Pr[hypothesis_consistent_event X_train R h]=1, { apply exists_consistent_hypothesis_of_realizable, apply h_realizable, apply h_identical }, cases h3 with c h3, have h4:Pr[(∃ᵣ (h : H), he_hypothesis_consistent_event X_test X_train R h ε)ᶜ ∧ (hypothesis_consistent_event X_train R c)] ≥ 1 - δ, { have h4_1:Pr[(hypothesis_consistent_event X_train R c)ᶜ]=0, { rw neg_eq_not, rw ← Pr_one_minus_eq_not, rw h3, simp }, have h4_2:Pr[(∃ᵣ (h : H), he_hypothesis_consistent_event X_test X_train R h ε) ∨ (hypothesis_consistent_event X_train R c)ᶜ] ≤ δ, { apply le_trans, apply Pr_le_eor_sum, rw h4_1, rw add_zero, apply h1 }, have h4_3:Pr[((∃ᵣ (h : H), he_hypothesis_consistent_event X_test X_train R h ε) ∨ (hypothesis_consistent_event X_train R c)ᶜ)ᶜ] ≥ 1 - δ, { apply Pr_compl_ge_of_Pr_le, apply h4_2 }, have h4_4:((∃ᵣ (h : H), he_hypothesis_consistent_event X_test X_train R h ε) ∨ (hypothesis_consistent_event X_train R c)ᶜ)ᶜ = ((∃ᵣ (h : H), he_hypothesis_consistent_event X_test X_train R h ε)ᶜ ∧ (hypothesis_consistent_event X_train R c)), { rw compl_eor_eq_compl_eand_compl, repeat {rw neg_eq_not}, rw enot_enot_eq_self, }, rw ← h4_4, apply h4_3, }, apply le_trans h4, apply event_prob_mono2, apply approximately_correct_if_he_hypothesis_consistent, apply h3, apply h2, end end pac_proof section finite_pac_proof variables {Ω:Type*} {p:probability_space Ω} {β:Type*} [Mβ:measurable_space β] (X_test:p →ᵣ (Mβ ×ₘ bool.measurable_space)) {D:Type*} (X_train:D → (p →ᵣ (Mβ ×ₘ bool.measurable_space))) {H:Type*} [TM:top_measurable H] (A:(Πₘ (d:D), Mβ ×ₘ bool.measurable_space) →ₘ TM.to_measurable_space) (R:H → measurable_setB Mβ) def example_correct (h:H) (d:D):event p := ¬ₑ (example_error X_train R h d) lemma example_correct_def (h:H) (d:D):example_correct X_train R h d = ¬ₑ (example_error X_train R h d) := rfl lemma example_error_IID (h:H): (random_variables_IID X_train) → (events_IID (example_error X_train R h)) := begin intros h1, have h2:(example_error X_train R h) = (λ (d:D), (X_train d) ∈ᵣ ((R h).prod (measurable_setB_top {ff}) ∪ ((R h)ᶜ).prod (measurable_setB_top {tt}))), { ext1 d, simp [example_error], rw error_as_measurable_setB }, rw h2, apply rv_event_IID, apply h1, end lemma example_correct_IID (h:H): (random_variables_IID X_train) → (events_IID (example_correct X_train R h)) := begin intros h1, have h2:events_IID (enot ∘ (example_error X_train R h)), { apply events_IID_not_of_events_IID, apply example_error_IID, apply h1, }, apply h2, end lemma Pr_hypothesis_consistent_event (h:H) [FD:fintype D] [NE:nonempty D]: (∀ (d:D), random_variable_identical X_test (X_train d)) → (random_variables_IID X_train) → Pr[hypothesis_consistent_event X_train R h] = (1-error X_test (R h))^(fintype.card D) := begin intros h1 h2, simp [hypothesis_consistent_event], have h3:(λ (d:D), ¬ₑ example_error X_train R h d) = (λ (d:D), example_correct X_train R h d) := rfl, rw h3, haveI:inhabited D := classical.inhabited_of_nonempty NE, rw eall_fintype_eq_eall_finset, rw events_IID_pow, have h4:finset.univ.card = fintype.card D := rfl, rw h4, have h5:Pr[example_correct X_train R h (default D)] = (1 - error X_test (R h)), { rw example_correct_def, rw ← Pr_one_minus_eq_not, rw Pr_example_error_eq_error, apply h1 }, rw h5, apply example_correct_IID, apply h2, end lemma Pr_he_hypothesis_consistent_event (h:H) [FD:fintype D] [NE:nonempty D] (ε:nnreal): (∀ (d:D), random_variable_identical X_test (X_train d)) → (random_variables_IID X_train) → Pr[he_hypothesis_consistent_event X_test X_train R h ε] ≤ (1-ε)^(fintype.card D) := begin intros h1 h2, simp [he_hypothesis_consistent_event], cases decidable.em (ε < error X_test (R h)) with h3 h3, { apply le_trans, apply Pr_eand_le_left, rw Pr_hypothesis_consistent_event X_test, apply nnreal_pow_mono, apply nnreal_sub_le_sub_of_le, apply le_of_lt h3, apply h1, apply h2 }, { apply le_trans, apply Pr_eand_le_right, rw Pr_event_const_false, simp, apply h3 }, end lemma Pr_he_hypothesis_consistent_event_exp (h:H) [FD:fintype D] [NE:nonempty D] (ε:nnreal): (∀ (d:D), random_variable_identical X_test (X_train d)) → (random_variables_IID X_train) → Pr[he_hypothesis_consistent_event X_test X_train R h ε] ≤ nnreal.exp (-ε * fintype.card D) := begin intros h1 h2, apply le_trans, apply Pr_he_hypothesis_consistent_event, apply h1, apply h2, apply nnreal_exp_bound2, end def he_hypothesis_consistent_pac_fintype [FH:fintype H] [fintype D] (δ ε:nnreal):Prop := @he_hypothesis_consistent_pac _ _ _ _ X_test _ X_train _ R (fintype.encodable H) _ δ ε -- Pr[∃ᵣ (h:H), he_hypothesis_consistent_event X_test T R h ε] ≤ δ lemma he_hypothesis_consistent_pac_fintype_def [FH:fintype H] [fintype D] (δ ε:nnreal): he_hypothesis_consistent_pac_fintype X_test X_train R δ ε = (Pr[∃ᵣ (h:H), he_hypothesis_consistent_event X_test X_train R h ε] ≤ δ) := begin have h1:(∃ᵣ (h:H), he_hypothesis_consistent_event X_test X_train R h ε) = eany_fintype FH (λ (h:H), he_hypothesis_consistent_event X_test X_train R h ε) := rfl, rw h1, clear h1, simp [he_hypothesis_consistent_pac_fintype, he_hypothesis_consistent_pac], rw eany_encodable_notation_def, rw eany_eq_eany_fintype, end lemma he_hypothesis_consistent_pac_fintype_bound [FH:fintype H] [FD:fintype D] [NE:nonempty D] (ε:nnreal): (∀ (d:D), random_variable_identical X_test (X_train d)) → (random_variables_IID X_train) → he_hypothesis_consistent_pac_fintype X_test X_train R ((fintype.card H) * nnreal.exp (-ε * fintype.card D)) ε := begin intros h1 h2, rw he_hypothesis_consistent_pac_fintype_def, apply eany_fintype_bound2, intros h, apply Pr_he_hypothesis_consistent_event_exp, apply h1, apply h2, end lemma consistent_pac_finite [fintype H] [fintype D] [nonempty D] (δ ε:nnreal):(realizable X_test R) → (∀ (d:D), random_variable_identical X_test (X_train d)) → (he_hypothesis_consistent_pac_fintype X_test X_train R δ ε) → (consistent_algorithm A R) → (probably_approximately_correct X_test X_train A R δ ε) := begin intros h1 h2 h3 h4, haveI:encodable H :=fintype.encodable H, apply consistent_pac, apply h1, apply h2, apply h3, apply h4, end /-- This is the meat of the proof of PAC bounds on a finite set of hypotheses. The only condition that makes it incomplete is that it requires the training data to be nonempty, whereas the result is trivially true if D is empty. -/ lemma pac_finite_bound_nonempty [fintype H] -- hypothesis space is finite [fintype D] -- examples are finite [nonempty D] -- examples are nonempty (ε:nnreal): -- approximation parameter (realizable X_test R) → -- there exists a hypothesis with zero error (∀ (d:D), random_variable_identical X_test (X_train d)) → -- the training data is identically -- distributed as the test (random_variables_IID X_train) → -- the training data is IID (consistent_algorithm A R) → -- the algorithm is consistent. (probably_approximately_correct X_test X_train A R ((fintype.card H) * nnreal.exp (-ε * fintype.card D)) ε) -- The algorithm has error less than ε with probability at least -- 1 - ((fintype.card H) * nnreal.exp (-ε * fintype.card D)) := begin intros h1 h2 h3 h4, apply consistent_pac_finite, apply h1, apply h2, apply he_hypothesis_consistent_pac_fintype_bound, apply h2, apply h3, apply h4, end /-- A trivial result: if there are no examples, then the bound is trivial. -/ lemma pac_finite_bound_empty [fintype H] -- hypothesis space is finite [fintype D] -- examples are finite (ε:nnreal): -- approximation parameter (realizable X_test R) → -- (Just to prove H is nonempty). (fintype.card D = 0) → (probably_approximately_correct X_test X_train A R ((fintype.card H) * nnreal.exp (-ε * fintype.card D)) ε) -- The algorithm has error less than ε with probability at least -- 1 - ((fintype.card H) * nnreal.exp (-ε * fintype.card D)) := begin intros h1 h2, cases h1 with c h1, have h3:nonempty H := nonempty.intro c, have h4:(1:nnreal) ≤ ((fintype.card H) * nnreal.exp (-ε * fintype.card D)), { rw h2, simp, rw nat.succ_le_iff, rw fintype.card_pos_iff, apply h3 }, apply probably_approximately_correct_mono, apply h4, apply probably_approximately_correct_trivial, end end finite_pac_proof theorem pac_finite_bound {Ω:Type*} -- Outcome space {p:probability_space Ω} -- Probability space {β:Type*} -- instance space [Mβ:measurable_space β] -- measurable space over instances (X_test:p →ᵣ (Mβ ×ₘ bool.measurable_space)) -- test data {D:Type*} -- training data index (T:D → (p →ᵣ (Mβ ×ₘ bool.measurable_space))) -- training data {H:Type*} -- hypothesis space [TM:top_measurable H] -- measurable space over hypotheses (A:(Πₘ (d:D), Mβ ×ₘ bool.measurable_space) →ₘ TM.to_measurable_space) -- algorithm (R:H → measurable_setB Mβ) -- representation schema [fintype H] -- hypothesis space is finite [fintype D] -- examples are finite (ε:nnreal): -- approximation parameter (realizable X_test R) → -- there exists a hypothesis with zero error (∀ (d:D), random_variable_identical X_test (T d)) → -- the training data is identical to test (random_variables_IID T) → -- the training data is IID (consistent_algorithm A R) → -- the algorithm is consistent. (probably_approximately_correct X_test T A R ((fintype.card H) * nnreal.exp (-ε * fintype.card D)) ε) -- The algorithm has error less than ε with probability at least -- 1 - ((fintype.card H) * nnreal.exp (-ε * fintype.card D)) := begin intros h1 h2 h3 h4, cases decidable.em (fintype.card D = 0), { apply pac_finite_bound_empty, apply h1, apply h }, { have h5:0 < fintype.card D, { apply decidable.by_contradiction, intros contra, apply h, simp at contra, apply contra }, rw fintype.card_pos_iff at h5, haveI: nonempty D := h5, apply pac_finite_bound_nonempty, repeat { assumption } }, end /- def generate_training_data {β:Type*} -- instance space [Mβ:measurable_space β] -- measurable space over instances {H:Type*} -- hypothesis space [TM:top_measurable H] -- measurable space over hypotheses (R:H → measurable_setB Mβ) -- representation schema {Ω:Type*} -- For all (A:nnreal → nnreal → Π m:ℕ, (Πₘ (d:fin m), Mβ ×ₘ bool.measurable_space) →ₘ TM.to_measurable_space): -/ #check 3 /- Ultimately, we want to be able to talk about an algorithm that has access to a process of independent and identical random variables EX(c, D). While we have shown how to create the product of two measures and the product of a finite number of measures. From Kearns and Vazirani, An Introduction to Computational Learning Theory: -------------------------------------------------------------------- Definition: (The PAC Model, Preliminary definition), PAC-learnable Let C be a concept class over X. We say that C is PAC learnable if there exists an algorithm L with the following property: for every concept c∈ C, for every distribution D on X, and for all 0 < ε < 1/2, and 0 < δ < 1/2, if L is given access to EX(c, D), and inputs ε and δ, then with probability at least 1 - δ, L outputs a hypothesis concept h ∈ C satisfying error(h) ≤ ε. This probability is taken over the random examples drawn by calls to EX(c, D), and any internal randomization of L. If L runs in time polynomial in (1/ε) and (1/δ), we say that C is efficiently PAC learnable. We will sometimes refer to the input ε as the error parameter, and the input δ as the confidence parameter. -------------------------------------------------------------------- Alternately, we can write down a simpler variant that covers many PAC learning algorithms. 0. For a specific concept class C (represented by R): 1. Consider ε and δ: 2. Algorithm (part 1) chooses an m given ε and δ. 3. Get a distribution of examples. 4. Algorithm (part 2) uses m, ε, and δ, to produce an hypothesis in h. -/ /- TODO: delete or fix! -/ def pac_learnable_simple_algorithm {β:Type*} -- instance space [Mβ:measurable_space β] -- measurable space over instances {H:Type*} -- hypothesis space [TM:top_measurable H] -- measurable space over hypotheses (R:H → measurable_setB Mβ) -- representation schema (num_examples:nnreal → nnreal → nat) -- number of examples as a function of the hypothesis. (A:nnreal → nnreal → Π m:ℕ, (Πₘ (d:fin m), Mβ ×ₘ bool.measurable_space) →ₘ TM.to_measurable_space): -- An algorithm for choosing a hypothesis. Prop := ∀ (h:H) -- For all hypotheses, (ε:nnreal) -- For all error parameters (see constraints below), (δ:nnreal) -- For all confidence parameters (see constraints), {Ω:Type*} -- For all {p:probability_space Ω} (X:p →ᵣ Mβ), true
1b658f8774079aea7f94d1d3828c85ef308795e0
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/homology/exact.lean
4f29c327700894a8e667b6a3b6d92925deffeee5
[ "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
12,916
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.homology.image_to_kernel /-! # Exact sequences In a category with zero morphisms, images, and equalizers we say that `f : A ⟶ B` and `g : B ⟶ C` are exact if `f ≫ g = 0` and the natural map `image f ⟶ kernel g` is an epimorphism. In any preadditive category this is equivalent to the homology at `B` vanishing. However in general it is weaker than other reasonable definitions of exactness, particularly that 1. the inclusion map `image.ι f` is a kernel of `g` or 2. `image f ⟶ kernel g` is an isomorphism or 3. `image_subobject f = kernel_subobject f`. However when the category is abelian, these all become equivalent; these results are found in `category_theory/abelian/exact.lean`. # Main results * Suppose that cokernels exist and that `f` and `g` are exact. If `s` is any kernel fork over `g` and `t` is any cokernel cofork over `f`, then `fork.ι s ≫ cofork.π t = 0`. * Precomposing the first morphism with an epimorphism retains exactness. Postcomposing the second morphism with a monomorphism retains exactness. * If `f` and `g` are exact and `i` is an isomorphism, then `f ≫ i.hom` and `i.inv ≫ g` are also exact. # Future work * Short exact sequences, split exact sequences, the splitting lemma (maybe only for abelian categories?) * Two adjacent maps in a chain complex are exact iff the homology vanishes -/ universes v v₂ u u₂ open category_theory open category_theory.limits variables {V : Type u} [category.{v} V] variables [has_images V] namespace category_theory /-- Two morphisms `f : A ⟶ B`, `g : B ⟶ C` are called exact if `w : f ≫ g = 0` and the natural map `image_to_kernel f g w : image_subobject f ⟶ kernel_subobject g` is an epimorphism. In any preadditive category, this is equivalent to `w : f ≫ g = 0` and `homology f g w ≅ 0`. In an abelian category, this is equivalent to `image_to_kernel f g w` being an isomorphism, and hence equivalent to the usual definition, `image_subobject f = kernel_subobject g`. -/ -- One nice feature of this definition is that we have -- `epi f → exact g h → exact (f ≫ g) h` and `exact f g → mono h → exact f (g ≫ h)`, -- which do not necessarily hold in a non-abelian category with the usual definition of `exact`. structure exact [has_zero_morphisms V] [has_kernels V] {A B C : V} (f : A ⟶ B) (g : B ⟶ C) : Prop := (w : f ≫ g = 0) (epi : epi (image_to_kernel f g w)) -- This works as an instance even though `exact` itself is not a class, as long as the goal is -- literally of the form `epi (image_to_kernel f g h.w)` (where `h : exact f g`). If the proof of -- `f ≫ g = 0` looks different, we are out of luck and have to add the instance by hand. attribute [instance] exact.epi attribute [reassoc] exact.w section variables [has_zero_object V] [preadditive V] [has_kernels V] [has_cokernels V] open_locale zero_object /-- In any preadditive category, composable morphisms `f g` are exact iff they compose to zero and the homology vanishes. -/ lemma preadditive.exact_iff_homology_zero {A B C : V} (f : A ⟶ B) (g : B ⟶ C) : exact f g ↔ ∃ w : f ≫ g = 0, nonempty (homology f g w ≅ 0) := ⟨λ h, ⟨h.w, ⟨cokernel.of_epi _⟩⟩, λ h, begin obtain ⟨w, ⟨i⟩⟩ := h, exact ⟨w, preadditive.epi_of_cokernel_zero ((cancel_mono i.hom).mp (by ext))⟩, end⟩ lemma preadditive.exact_of_iso_of_exact {A₁ B₁ C₁ A₂ B₂ C₂ : V} (f₁ : A₁ ⟶ B₁) (g₁ : B₁ ⟶ C₁) (f₂ : A₂ ⟶ B₂) (g₂ : B₂ ⟶ C₂) (α : arrow.mk f₁ ≅ arrow.mk f₂) (β : arrow.mk g₁ ≅ arrow.mk g₂) (p : α.hom.right = β.hom.left) (h : exact f₁ g₁) : exact f₂ g₂ := begin rw preadditive.exact_iff_homology_zero at h ⊢, rcases h with ⟨w₁, ⟨i⟩⟩, suffices w₂ : f₂ ≫ g₂ = 0, from ⟨w₂, ⟨(homology.map_iso w₁ w₂ α β p).symm.trans i⟩⟩, rw [← cancel_epi α.hom.left, ← cancel_mono β.inv.right, comp_zero, zero_comp, ← w₁], simp only [← arrow.mk_hom f₁, ← arrow.left_hom_inv_right α.hom, ← arrow.mk_hom g₁, ← arrow.left_hom_inv_right β.hom, p], simp only [arrow.mk_hom, is_iso.inv_hom_id_assoc, category.assoc, ← arrow.inv_right, is_iso.iso.inv_hom] end /-- A reformulation of `preadditive.exact_of_iso_of_exact` that does not involve the arrow category. -/ lemma preadditive.exact_of_iso_of_exact' {A₁ B₁ C₁ A₂ B₂ C₂ : V} (f₁ : A₁ ⟶ B₁) (g₁ : B₁ ⟶ C₁) (f₂ : A₂ ⟶ B₂) (g₂ : B₂ ⟶ C₂) (α : A₁ ≅ A₂) (β : B₁ ≅ B₂) (γ : C₁ ≅ C₂) (hsq₁ : α.hom ≫ f₂ = f₁ ≫ β.hom) (hsq₂ : β.hom ≫ g₂ = g₁ ≫ γ.hom) (h : exact f₁ g₁) : exact f₂ g₂ := preadditive.exact_of_iso_of_exact f₁ g₁ f₂ g₂ (arrow.iso_mk α β hsq₁) (arrow.iso_mk β γ hsq₂) rfl h lemma preadditive.exact_iff_exact_of_iso {A₁ B₁ C₁ A₂ B₂ C₂ : V} (f₁ : A₁ ⟶ B₁) (g₁ : B₁ ⟶ C₁) (f₂ : A₂ ⟶ B₂) (g₂ : B₂ ⟶ C₂) (α : arrow.mk f₁ ≅ arrow.mk f₂) (β : arrow.mk g₁ ≅ arrow.mk g₂) (p : α.hom.right = β.hom.left) : exact f₁ g₁ ↔ exact f₂ g₂ := ⟨preadditive.exact_of_iso_of_exact _ _ _ _ _ _ p, preadditive.exact_of_iso_of_exact _ _ _ _ α.symm β.symm begin rw ← cancel_mono α.hom.right, simp only [iso.symm_hom, ← comma.comp_right, α.inv_hom_id], simp only [p, ←comma.comp_left, arrow.id_right, arrow.id_left, iso.inv_hom_id], refl end⟩ end section variables [has_zero_morphisms V] [has_kernels V] lemma comp_eq_zero_of_image_eq_kernel {A B C : V} (f : A ⟶ B) (g : B ⟶ C) (p : image_subobject f = kernel_subobject g) : f ≫ g = 0 := begin rw [←image_subobject_arrow_comp f, category.assoc], convert comp_zero, rw p, simp, end lemma image_to_kernel_is_iso_of_image_eq_kernel {A B C : V} (f : A ⟶ B) (g : B ⟶ C) (p : image_subobject f = kernel_subobject g) : is_iso (image_to_kernel f g (comp_eq_zero_of_image_eq_kernel f g p)) := begin refine ⟨⟨subobject.of_le _ _ p.ge, _⟩⟩, dsimp [image_to_kernel], simp only [subobject.of_le_comp_of_le, subobject.of_le_refl], simp, end -- We'll prove the converse later, when `V` is abelian. lemma exact_of_image_eq_kernel {A B C : V} (f : A ⟶ B) (g : B ⟶ C) (p : image_subobject f = kernel_subobject g) : exact f g := { w := comp_eq_zero_of_image_eq_kernel f g p, epi := begin haveI := image_to_kernel_is_iso_of_image_eq_kernel f g p, apply_instance, end } end variables {A B C D : V} {f : A ⟶ B} {g : B ⟶ C} {h : C ⟶ D} local attribute [instance] epi_comp section variables [has_zero_morphisms V] [has_equalizers V] lemma exact_comp_hom_inv_comp (i : B ≅ D) (h : exact f g) : exact (f ≫ i.hom) (i.inv ≫ g) := begin refine ⟨by simp [h.w], _⟩, rw image_to_kernel_comp_hom_inv_comp, haveI := h.epi, apply_instance, end lemma exact_comp_inv_hom_comp (i : D ≅ B) (h : exact f g) : exact (f ≫ i.inv) (i.hom ≫ g) := exact_comp_hom_inv_comp i.symm h lemma exact_comp_hom_inv_comp_iff (i : B ≅ D) : exact (f ≫ i.hom) (i.inv ≫ g) ↔ exact f g := ⟨λ h, by simpa using exact_comp_inv_hom_comp i h, exact_comp_hom_inv_comp i⟩ lemma exact_epi_comp (hgh : exact g h) [epi f] : exact (f ≫ g) h := begin refine ⟨by simp [hgh.w], _⟩, rw image_to_kernel_comp_left, apply_instance, end @[simp] lemma exact_iso_comp [is_iso f] : exact (f ≫ g) h ↔ exact g h := ⟨λ w, by { rw ←is_iso.inv_hom_id_assoc f g, exact exact_epi_comp w }, λ w, exact_epi_comp w⟩ lemma exact_comp_mono (hfg : exact f g) [mono h] : exact f (g ≫ h) := begin refine ⟨by simp [hfg.w_assoc], _⟩, rw image_to_kernel_comp_right f g h hfg.w, apply_instance, end /-- The dual of this lemma is only true when `V` is abelian, see `abelian.exact_epi_comp_iff`. -/ lemma exact_comp_mono_iff [mono h] : exact f (g ≫ h) ↔ exact f g := begin refine ⟨λ hfg, ⟨zero_of_comp_mono h (by rw [category.assoc, hfg.1]), _⟩, λ h, exact_comp_mono h⟩, rw ← (iso.eq_comp_inv _).1 (image_to_kernel_comp_mono _ _ h hfg.1), haveI := hfg.2, apply_instance end @[simp] lemma exact_comp_iso [is_iso h] : exact f (g ≫ h) ↔ exact f g := exact_comp_mono_iff lemma exact_kernel_subobject_arrow : exact (kernel_subobject f).arrow f := begin refine ⟨by simp, _⟩, apply @is_iso.epi_of_iso _ _ _ _ _ _, exact ⟨⟨factor_thru_image_subobject _, by { ext, simp, }, by { ext, simp, }⟩⟩, end lemma exact_kernel_ι : exact (kernel.ι f) f := by { rw [←kernel_subobject_arrow', exact_iso_comp], exact exact_kernel_subobject_arrow } instance (h : exact f g) : epi (factor_thru_kernel_subobject g f h.w) := begin rw ←factor_thru_image_subobject_comp_image_to_kernel, apply epi_comp, end instance (h : exact f g) : epi (kernel.lift g f h.w) := begin rw ←factor_thru_kernel_subobject_comp_kernel_subobject_iso, apply epi_comp end variables (A) lemma kernel_subobject_arrow_eq_zero_of_exact_zero_left (h : exact (0 : A ⟶ B) g) : (kernel_subobject g).arrow = 0 := begin rw [←cancel_epi (image_to_kernel (0 : A ⟶ B) g h.w), ←cancel_epi (factor_thru_image_subobject (0 : A ⟶ B))], simp end lemma kernel_ι_eq_zero_of_exact_zero_left (h : exact (0 : A ⟶ B) g) : kernel.ι g = 0 := by { rw ←kernel_subobject_arrow', simp [kernel_subobject_arrow_eq_zero_of_exact_zero_left A h], } lemma exact_zero_left_of_mono [has_zero_object V] [mono g] : exact (0 : A ⟶ B) g := ⟨by simp, image_to_kernel_epi_of_zero_of_mono _⟩ end section has_cokernels variables [has_zero_morphisms V] [has_equalizers V] [has_cokernels V] (f g) @[simp, reassoc] lemma kernel_comp_cokernel (h : exact f g) : kernel.ι g ≫ cokernel.π f = 0 := begin rw [←kernel_subobject_arrow', category.assoc], convert comp_zero, apply zero_of_epi_comp (image_to_kernel f g h.w) _, rw [image_to_kernel_arrow_assoc, ←image_subobject_arrow, category.assoc, ←iso.eq_inv_comp], ext, simp, end lemma comp_eq_zero_of_exact (h : exact f g) {X Y : V} {ι : X ⟶ B} (hι : ι ≫ g = 0) {π : B ⟶ Y} (hπ : f ≫ π = 0) : ι ≫ π = 0 := by rw [←kernel.lift_ι _ _ hι, ←cokernel.π_desc _ _ hπ, category.assoc, kernel_comp_cokernel_assoc _ _ h, zero_comp, comp_zero] @[simp, reassoc] lemma fork_ι_comp_cofork_π (h : exact f g) (s : kernel_fork g) (t : cokernel_cofork f) : fork.ι s ≫ cofork.π t = 0 := comp_eq_zero_of_exact f g h (kernel_fork.condition s) (cokernel_cofork.condition t) end has_cokernels section variables [has_zero_object V] open_locale zero_object section variables [has_zero_morphisms V] [has_kernels V] lemma exact_of_zero {A C : V} (f : A ⟶ 0) (g : 0 ⟶ C) : exact f g := begin obtain rfl : f = 0 := by ext, obtain rfl : g = 0 := by ext, fsplit, { simp, }, { exact image_to_kernel_epi_of_zero_of_mono 0, }, end lemma exact_zero_mono {B C : V} (f : B ⟶ C) [mono f] : exact (0 : (0 ⟶ B)) f := ⟨by simp, infer_instance⟩ lemma exact_epi_zero {A B : V} (f : A ⟶ B) [epi f] : exact f (0 : (B ⟶ 0)) := ⟨by simp, infer_instance⟩ end section variables [preadditive V] lemma mono_iff_exact_zero_left [has_kernels V] {B C : V} (f : B ⟶ C) : mono f ↔ exact (0 : (0 ⟶ B)) f := ⟨λ h, by exactI exact_zero_mono _, λ h, preadditive.mono_of_kernel_iso_zero ((kernel_subobject_iso f).symm ≪≫ iso_zero_of_epi_zero (by simpa using h.epi))⟩ lemma epi_iff_exact_zero_right [has_equalizers V] {A B : V} (f : A ⟶ B) : epi f ↔ exact f (0 : (B ⟶ 0)) := ⟨λ h, by exactI exact_epi_zero _, λ h, begin have e₁ := h.epi, rw image_to_kernel_zero_right at e₁, have e₂ : epi (((image_subobject f).arrow ≫ inv (kernel_subobject 0).arrow) ≫ (kernel_subobject 0).arrow) := @epi_comp _ _ _ _ _ _ e₁ _ _, rw [category.assoc, is_iso.inv_hom_id, category.comp_id] at e₂, rw [←image_subobject_arrow] at e₂, resetI, haveI : epi (image.ι f) := epi_of_epi (image_subobject_iso f).hom (image.ι f), apply epi_of_epi_image, end⟩ end end namespace functor variables [has_zero_morphisms V] [has_kernels V] {W : Type u₂} [category.{v₂} W] variables [has_images W] [has_zero_morphisms W] [has_kernels W] /-- A functor reflects exact sequences if any composable pair of morphisms that is mapped to an exact pair is itself exact. -/ class reflects_exact_sequences (F : V ⥤ W) := (reflects : ∀ {A B C : V} (f : A ⟶ B) (g : B ⟶ C), exact (F.map f) (F.map g) → exact f g) lemma exact_of_exact_map (F : V ⥤ W) [reflects_exact_sequences F] {A B C : V} {f : A ⟶ B} {g : B ⟶ C} (hfg : exact (F.map f) (F.map g)) : exact f g := reflects_exact_sequences.reflects f g hfg end functor end category_theory
0fd10570408548e9de9df09a9542f7cbdc982b8e
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/group_theory/sylow.lean
ca7af552e9cc63c1ae87726130bab901d7098d8f
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
11,923
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import group_theory.group_action import group_theory.quotient_group import group_theory.order_of_element import data.zmod.basic import data.fintype.card import data.list.rotate open equiv fintype finset mul_action function open equiv.perm is_subgroup list quotient_group universes u v w variables {G : Type u} {α : Type v} {β : Type w} [group G] local attribute [instance, priority 10] subtype.fintype set_fintype classical.prop_decidable namespace mul_action variables [mul_action G α] lemma mem_fixed_points_iff_card_orbit_eq_one {a : α} [fintype (orbit G a)] : a ∈ fixed_points G α ↔ card (orbit G a) = 1 := begin rw [fintype.card_eq_one_iff, mem_fixed_points], split, { exact λ h, ⟨⟨a, mem_orbit_self _⟩, λ ⟨b, ⟨x, hx⟩⟩, subtype.eq $ by simp [h x, hx.symm]⟩ }, { assume h x, rcases h with ⟨⟨z, hz⟩, hz₁⟩, exact calc x • a = z : subtype.mk.inj (hz₁ ⟨x • a, mem_orbit _ _⟩) ... = a : (subtype.mk.inj (hz₁ ⟨a, mem_orbit_self _⟩)).symm } end lemma card_modeq_card_fixed_points [fintype α] [fintype G] [fintype (fixed_points G α)] (p : ℕ) {n : ℕ} [hp : fact p.prime] (h : card G = p ^ n) : card α ≡ card (fixed_points G α) [MOD p] := calc card α = card (Σ y : quotient (orbit_rel G α), {x // quotient.mk' x = y}) : card_congr (sigma_preimage_equiv (@quotient.mk' _ (orbit_rel G α))).symm ... = univ.sum (λ a : quotient (orbit_rel G α), card {x // quotient.mk' x = a}) : card_sigma _ ... ≡ (@univ (fixed_points G α) _).sum (λ _, 1) [MOD p] : begin rw [← zmod.eq_iff_modeq_nat p, sum_nat_cast, sum_nat_cast], refine eq.symm (sum_bij_ne_zero (λ a _ _, quotient.mk' a.1) (λ _ _ _, mem_univ _) (λ a₁ a₂ _ _ _ _ h, subtype.eq ((mem_fixed_points' α).1 a₂.2 a₁.1 (quotient.exact' h))) (λ b, _) (λ a ha _, by rw [← mem_fixed_points_iff_card_orbit_eq_one.1 a.2]; simp only [quotient.eq']; congr)), { refine quotient.induction_on' b (λ b _ hb, _), have : card (orbit G b) ∣ p ^ n, { rw [← h, fintype.card_congr (orbit_equiv_quotient_stabilizer G b)]; exact card_quotient_dvd_card _ }, rcases (nat.dvd_prime_pow hp).1 this with ⟨k, _, hk⟩, have hb' :¬ p ^ 1 ∣ p ^ k, { rw [nat.pow_one, ← hk, ← nat.modeq.modeq_zero_iff, ← zmod.eq_iff_modeq_nat, nat.cast_zero, ← ne.def], exact eq.mpr (by simp only [quotient.eq']; congr) hb }, have : k = 0 := nat.le_zero_iff.1 (nat.le_of_lt_succ (lt_of_not_ge (mt (nat.pow_dvd_pow p) hb'))), refine ⟨⟨b, mem_fixed_points_iff_card_orbit_eq_one.2 $ by rw [hk, this, nat.pow_zero]⟩, mem_univ _, _, rfl⟩, rw [nat.cast_one], exact one_ne_zero } end ... = _ : by simp; refl end mul_action lemma quotient_group.card_preimage_mk [fintype G] (s : set G) [is_subgroup s] (t : set (quotient s)) : fintype.card (quotient_group.mk ⁻¹' t) = fintype.card s * fintype.card t := by rw [← fintype.card_prod, fintype.card_congr (preimage_mk_equiv_subgroup_times_set _ _)] namespace sylow /-- Given a vector `v` of length `n`, make a vector of length `n+1` whose product is `1`, by consing the the inverse of the product of `v`. -/ def mk_vector_prod_eq_one (n : ℕ) (v : vector G n) : vector G (n+1) := v.to_list.prod⁻¹ :: v lemma mk_vector_prod_eq_one_inj (n : ℕ) : injective (@mk_vector_prod_eq_one G _ n) := λ ⟨v, _⟩ ⟨w, _⟩ h, subtype.eq (show v = w, by injection h with h; injection h) /-- The type of vectors with terms from `G`, length `n`, and product equal to `1:G`. -/ def vectors_prod_eq_one (G : Type*) [group G] (n : ℕ) : set (vector G n) := {v | v.to_list.prod = 1} lemma mem_vectors_prod_eq_one {n : ℕ} (v : vector G n) : v ∈ vectors_prod_eq_one G n ↔ v.to_list.prod = 1 := iff.rfl lemma mem_vectors_prod_eq_one_iff {n : ℕ} (v : vector G (n + 1)) : v ∈ vectors_prod_eq_one G (n + 1) ↔ v ∈ set.range (@mk_vector_prod_eq_one G _ n) := ⟨λ (h : v.to_list.prod = 1), ⟨v.tail, begin unfold mk_vector_prod_eq_one, conv {to_rhs, rw ← vector.cons_head_tail v}, suffices : (v.tail.to_list.prod)⁻¹ = v.head, { rw this }, rw [← mul_left_inj v.tail.to_list.prod, inv_mul_self, ← list.prod_cons, ← vector.to_list_cons, vector.cons_head_tail, h] end⟩, λ ⟨w, hw⟩, by rw [mem_vectors_prod_eq_one, ← hw, mk_vector_prod_eq_one, vector.to_list_cons, list.prod_cons, inv_mul_self]⟩ /-- The rotation action of `zmod n` (viewed as multiplicative group) on `vectors_prod_eq_one G n`, where `G` is a multiplicative group. -/ def rotate_vectors_prod_eq_one (G : Type*) [group G] (n : ℕ) (m : multiplicative (zmod n)) (v : vectors_prod_eq_one G n) : vectors_prod_eq_one G n := ⟨⟨v.1.to_list.rotate m.val, by simp⟩, prod_rotate_eq_one_of_prod_eq_one v.2 _⟩ instance rotate_vectors_prod_eq_one.mul_action (n : ℕ) [fact (0 < n)] : mul_action (multiplicative (zmod n)) (vectors_prod_eq_one G n) := { smul := (rotate_vectors_prod_eq_one G n), one_smul := begin intro v, apply subtype.eq, apply vector.eq _ _, show rotate _ (0 : zmod n).val = _, rw zmod.val_zero, exact rotate_zero v.1.to_list end, mul_smul := λ a b ⟨⟨v, hv₁⟩, hv₂⟩, subtype.eq $ vector.eq _ _ $ show v.rotate ((a + b : zmod n).val) = list.rotate (list.rotate v (b.val)) (a.val), by rw [zmod.val_add, rotate_rotate, ← rotate_mod _ (b.val + a.val), add_comm, hv₁] } lemma one_mem_vectors_prod_eq_one (n : ℕ) : vector.repeat (1 : G) n ∈ vectors_prod_eq_one G n := by simp [vector.repeat, vectors_prod_eq_one] lemma one_mem_fixed_points_rotate (n : ℕ) [fact (0 < n)] : (⟨vector.repeat (1 : G) n, one_mem_vectors_prod_eq_one n⟩ : vectors_prod_eq_one G n) ∈ fixed_points (multiplicative (zmod n)) (vectors_prod_eq_one G n) := λ m, subtype.eq $ vector.eq _ _ $ by haveI : nonempty G := ⟨1⟩; exact rotate_eq_self_iff_eq_repeat.2 ⟨(1 : G), show list.repeat (1 : G) n = list.repeat 1 (list.repeat (1 : G) n).length, by simp⟩ _ /-- Cauchy's theorem -/ lemma exists_prime_order_of_dvd_card [fintype G] (p : ℕ) [hp : fact p.prime] (hdvd : p ∣ card G) : ∃ x : G, order_of x = p := let n : ℕ+ := ⟨p - 1, nat.sub_pos_of_lt hp.one_lt⟩ in have hn : p = n + 1 := nat.succ_sub hp.pos, have hcard : card (vectors_prod_eq_one G (n + 1)) = card G ^ (n : ℕ), by rw [set.ext mem_vectors_prod_eq_one_iff, set.card_range_of_injective (mk_vector_prod_eq_one_inj _), card_vector], have hzmod : fintype.card (multiplicative (zmod p)) = p ^ 1, by { rw nat.pow_one p, exact zmod.card p }, have hmodeq : _ = _ := @mul_action.card_modeq_card_fixed_points (multiplicative (zmod p)) (vectors_prod_eq_one G p) _ _ _ _ _ _ 1 hp hzmod, have hdvdcard : p ∣ fintype.card (vectors_prod_eq_one G (n + 1)) := calc p ∣ card G ^ 1 : by rwa nat.pow_one ... ∣ card G ^ (n : ℕ) : nat.pow_dvd_pow _ n.2 ... = card (vectors_prod_eq_one G (n + 1)) : hcard.symm, have hdvdcard₂ : p ∣ card (fixed_points (multiplicative (zmod p)) (vectors_prod_eq_one G p)), by { rw nat.dvd_iff_mod_eq_zero at hdvdcard ⊢, rwa [← hn, hmodeq] at hdvdcard }, have hcard_pos : 0 < card (fixed_points (multiplicative (zmod p)) (vectors_prod_eq_one G p)) := fintype.card_pos_iff.2 ⟨⟨⟨vector.repeat 1 p, one_mem_vectors_prod_eq_one _⟩, one_mem_fixed_points_rotate _⟩⟩, have hlt : 1 < card (fixed_points (multiplicative (zmod p)) (vectors_prod_eq_one G p)) := calc (1 : ℕ) < p : hp.one_lt ... ≤ _ : nat.le_of_dvd hcard_pos hdvdcard₂, let ⟨⟨⟨⟨x, hx₁⟩, hx₂⟩, hx₃⟩, hx₄⟩ := fintype.exists_ne_of_one_lt_card hlt ⟨_, one_mem_fixed_points_rotate p⟩ in have hx : x ≠ list.repeat (1 : G) p, from λ h, by simpa [h, vector.repeat] using hx₄, have nG : nonempty G, from ⟨1⟩, have ∃ a, x = list.repeat a x.length := by exactI rotate_eq_self_iff_eq_repeat.1 (λ n, have list.rotate x (n : zmod p).val = x := subtype.mk.inj (subtype.mk.inj (hx₃ (n : zmod p))), by rwa [zmod.val_cast_nat, ← hx₁, rotate_mod] at this), let ⟨a, ha⟩ := this in ⟨a, have hx1 : x.prod = 1 := hx₂, have ha1: a ≠ 1, from λ h, hx (ha.symm ▸ h ▸ hx₁ ▸ rfl), have a ^ p = 1, by rwa [ha, list.prod_repeat, hx₁] at hx1, (hp.2 _ (order_of_dvd_of_pow_eq_one this)).resolve_left (λ h, ha1 (order_of_eq_one_iff.1 h))⟩ open is_subgroup is_submonoid is_group_hom mul_action lemma mem_fixed_points_mul_left_cosets_iff_mem_normalizer {H : set G} [is_subgroup H] [fintype H] {x : G} : (x : quotient H) ∈ fixed_points H (quotient H) ↔ x ∈ normalizer H := ⟨λ hx, have ha : ∀ {y : quotient H}, y ∈ orbit H (x : quotient H) → y = x, from λ _, ((mem_fixed_points' _).1 hx _), (inv_mem_iff _).1 (mem_normalizer_fintype (λ n hn, have (n⁻¹ * x)⁻¹ * x ∈ H := quotient_group.eq.1 (ha (mem_orbit _ ⟨n⁻¹, inv_mem hn⟩)), by simpa only [mul_inv_rev, inv_inv] using this)), λ (hx : ∀ (n : G), n ∈ H ↔ x * n * x⁻¹ ∈ H), (mem_fixed_points' _).2 $ λ y, quotient.induction_on' y $ λ y hy, quotient_group.eq.2 (let ⟨⟨b, hb₁⟩, hb₂⟩ := hy in have hb₂ : (b * x)⁻¹ * y ∈ H := quotient_group.eq.1 hb₂, (inv_mem_iff H).1 $ (hx _).2 $ (mul_mem_cancel_right H (inv_mem hb₁)).1 $ by rw hx at hb₂; simpa [mul_inv_rev, mul_assoc] using hb₂)⟩ def fixed_points_mul_left_cosets_equiv_quotient (H : set G) [is_subgroup H] [fintype H] : fixed_points H (quotient H) ≃ quotient (subtype.val ⁻¹' H : set (normalizer H)) := @subtype_quotient_equiv_quotient_subtype G (normalizer H) (id _) (id _) (fixed_points _ _) (λ a, mem_fixed_points_mul_left_cosets_iff_mem_normalizer.symm) (by intros; refl) local attribute [instance] set_fintype lemma exists_subgroup_card_pow_prime [fintype G] (p : ℕ) : ∀ {n : ℕ} [hp : fact p.prime] (hdvd : p ^ n ∣ card G), ∃ H : set G, is_subgroup H ∧ fintype.card H = p ^ n | 0 := λ _ _, ⟨trivial G, by apply_instance, by simp⟩ | (n+1) := λ hp hdvd, let ⟨H, ⟨hH1, hH2⟩⟩ := @exists_subgroup_card_pow_prime _ hp (dvd.trans (nat.pow_dvd_pow _ (nat.le_succ _)) hdvd) in let ⟨s, hs⟩ := exists_eq_mul_left_of_dvd hdvd in by exactI have hcard : card (quotient H) = s * p := (nat.mul_left_inj (show card H > 0, from fintype.card_pos_iff.2 ⟨⟨1, is_submonoid.one_mem⟩⟩)).1 (by rwa [← card_eq_card_quotient_mul_card_subgroup, hH2, hs, nat.pow_succ, mul_assoc, mul_comm p]), have hm : s * p % p = card (quotient (subtype.val ⁻¹' H : set (normalizer H))) % p := card_congr (fixed_points_mul_left_cosets_equiv_quotient H) ▸ hcard ▸ card_modeq_card_fixed_points p hH2, have hm' : p ∣ card (quotient (subtype.val ⁻¹' H : set (normalizer H))) := nat.dvd_of_mod_eq_zero (by rwa [nat.mod_eq_zero_of_dvd (dvd_mul_left _ _), eq_comm] at hm), let ⟨x, hx⟩ := @exists_prime_order_of_dvd_card _ (quotient_group.group _) _ _ hp hm' in have hxcard : ∀ {f : fintype (gpowers x)}, card (gpowers x) = p, from λ f, by rw [← hx, order_eq_card_gpowers]; congr, have is_subgroup (mk ⁻¹' gpowers x), from is_group_hom.preimage _ _, have fintype (mk ⁻¹' gpowers x), by apply_instance, have hequiv : H ≃ (subtype.val ⁻¹' H : set (normalizer H)) := ⟨λ a, ⟨⟨a.1, subset_normalizer _ a.2⟩, a.2⟩, λ a, ⟨a.1.1, a.2⟩, λ ⟨_, _⟩, rfl, λ ⟨⟨_, _⟩, _⟩, rfl⟩, ⟨subtype.val '' (mk ⁻¹' gpowers x), by apply_instance, by rw [set.card_image_of_injective (mk ⁻¹' gpowers x) subtype.val_injective, nat.pow_succ, ← hH2, fintype.card_congr hequiv, ← hx, order_eq_card_gpowers, ← fintype.card_prod]; exact @fintype.card_congr _ _ (id _) (id _) (preimage_mk_equiv_subgroup_times_set _ _)⟩ end sylow
53bd8dbbadc276757feba81b7d97877d576d44d3
4727251e0cd73359b15b664c3170e5d754078599
/src/data/typevec.lean
778f801331ba41cf1022c5e1fa6890a4cb60ec8f
[ "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
24,486
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro, Simon Hudon -/ import data.fin.fin2 import logic.function.basic import tactic.basic /-! # Tuples of types, and their categorical structure. ## Features * `typevec n` - n-tuples of types * `α ⟹ β` - n-tuples of maps * `f ⊚ g` - composition Also, support functions for operating with n-tuples of types, such as: * `append1 α β` - append type `β` to n-tuple `α` to obtain an (n+1)-tuple * `drop α` - drops the last element of an (n+1)-tuple * `last α` - returns the last element of an (n+1)-tuple * `append_fun f g` - appends a function g to an n-tuple of functions * `drop_fun f` - drops the last function from an n+1-tuple * `last_fun f` - returns the last function of a tuple. Since e.g. `append1 α.drop α.last` is propositionally equal to `α` but not definitionally equal to it, we need support functions and lemmas to mediate between constructions. -/ universes u v w /-- n-tuples of types, as a category -/ def typevec (n : ℕ) := fin2 n → Type* instance {n} : inhabited (typevec.{u} n) := ⟨ λ _, punit ⟩ namespace typevec variable {n : ℕ} /-- arrow in the category of `typevec` -/ def arrow (α β : typevec n) := Π i : fin2 n, α i → β i localized "infixl ` ⟹ `:40 := typevec.arrow" in mvfunctor instance arrow.inhabited (α β : typevec n) [Π i, inhabited (β i)] : inhabited (α ⟹ β) := ⟨ λ _ _, default ⟩ /-- identity of arrow composition -/ def id {α : typevec n} : α ⟹ α := λ i x, x /-- arrow composition in the category of `typevec` -/ def comp {α β γ : typevec n} (g : β ⟹ γ) (f : α ⟹ β) : α ⟹ γ := λ i x, g i (f i x) localized "infixr ` ⊚ `:80 := typevec.comp" in mvfunctor -- type as \oo @[simp] theorem id_comp {α β : typevec n} (f : α ⟹ β) : id ⊚ f = f := rfl @[simp] theorem comp_id {α β : typevec n} (f : α ⟹ β) : f ⊚ id = f := rfl theorem comp_assoc {α β γ δ : typevec n} (h : γ ⟹ δ) (g : β ⟹ γ) (f : α ⟹ β) : (h ⊚ g) ⊚ f = h ⊚ g ⊚ f := rfl /-- Support for extending a typevec by one element. -/ def append1 (α : typevec n) (β : Type*) : typevec (n+1) | (fin2.fs i) := α i | fin2.fz := β infixl ` ::: `:67 := append1 /-- retain only a `n-length` prefix of the argument -/ def drop (α : typevec.{u} (n+1)) : typevec n := λ i, α i.fs /-- take the last value of a `(n+1)-length` vector -/ def last (α : typevec.{u} (n+1)) : Type* := α fin2.fz instance last.inhabited (α : typevec (n+1)) [inhabited (α fin2.fz)] : inhabited (last α) := ⟨show α fin2.fz, from default⟩ theorem drop_append1 {α : typevec n} {β : Type*} {i : fin2 n} : drop (append1 α β) i = α i := rfl theorem drop_append1' {α : typevec n} {β : Type*} : drop (append1 α β) = α := by ext; apply drop_append1 theorem last_append1 {α : typevec n} {β : Type*} : last (append1 α β) = β := rfl @[simp] theorem append1_drop_last (α : typevec (n+1)) : append1 (drop α) (last α) = α := funext $ λ i, by cases i; refl /-- cases on `(n+1)-length` vectors -/ @[elab_as_eliminator] def append1_cases {C : typevec (n+1) → Sort u} (H : ∀ α β, C (append1 α β)) (γ) : C γ := by rw [← @append1_drop_last _ γ]; apply H @[simp] theorem append1_cases_append1 {C : typevec (n+1) → Sort u} (H : ∀ α β, C (append1 α β)) (α β) : @append1_cases _ C H (append1 α β) = H α β := rfl /-- append an arrow and a function for arbitrary source and target type vectors -/ def split_fun {α α' : typevec (n+1)} (f : drop α ⟹ drop α') (g : last α → last α') : α ⟹ α' | (fin2.fs i) := f i | fin2.fz := g /-- append an arrow and a function as well as their respective source and target types / typevecs -/ def append_fun {α α' : typevec n} {β β' : Type*} (f : α ⟹ α') (g : β → β') : append1 α β ⟹ append1 α' β' := split_fun f g infixl ` ::: ` := append_fun /-- split off the prefix of an arrow -/ def drop_fun {α β : typevec (n+1)} (f : α ⟹ β) : drop α ⟹ drop β := λ i, f i.fs /-- split off the last function of an arrow -/ def last_fun {α β : typevec (n+1)} (f : α ⟹ β) : last α → last β := f fin2.fz /-- arrow in the category of `0-length` vectors -/ def nil_fun {α : typevec 0} {β : typevec 0} : α ⟹ β := λ i, fin2.elim0 i theorem eq_of_drop_last_eq {α β : typevec (n+1)} {f g : α ⟹ β} (h₀ : drop_fun f = drop_fun g) (h₁ : last_fun f = last_fun g) : f = g := by replace h₀ := congr_fun h₀; ext1 (ieq | ⟨j, ieq⟩); apply_assumption @[simp] theorem drop_fun_split_fun {α α' : typevec (n+1)} (f : drop α ⟹ drop α') (g : last α → last α') : drop_fun (split_fun f g) = f := rfl /-- turn an equality into an arrow -/ def arrow.mp {α β : typevec n} (h : α = β) : α ⟹ β | i := eq.mp (congr_fun h _) /-- turn an equality into an arrow, with reverse direction -/ def arrow.mpr {α β : typevec n} (h : α = β) : β ⟹ α | i := eq.mpr (congr_fun h _) /-- decompose a vector into its prefix appended with its last element -/ def to_append1_drop_last {α : typevec (n+1)} : α ⟹ drop α ::: last α := arrow.mpr (append1_drop_last _) /-- stitch two bits of a vector back together -/ def from_append1_drop_last {α : typevec (n+1)} : drop α ::: last α ⟹ α := arrow.mp (append1_drop_last _) @[simp] theorem last_fun_split_fun {α α' : typevec (n+1)} (f : drop α ⟹ drop α') (g : last α → last α') : last_fun (split_fun f g) = g := rfl @[simp] theorem drop_fun_append_fun {α α' : typevec n} {β β' : Type*} (f : α ⟹ α') (g : β → β') : drop_fun (f ::: g) = f := rfl @[simp] theorem last_fun_append_fun {α α' : typevec n} {β β' : Type*} (f : α ⟹ α') (g : β → β') : last_fun (f ::: g) = g := rfl theorem split_drop_fun_last_fun {α α' : typevec (n+1)} (f : α ⟹ α') : split_fun (drop_fun f) (last_fun f) = f := eq_of_drop_last_eq rfl rfl theorem split_fun_inj {α α' : typevec (n+1)} {f f' : drop α ⟹ drop α'} {g g' : last α → last α'} (H : split_fun f g = split_fun f' g') : f = f' ∧ g = g' := by rw [← drop_fun_split_fun f g, H, ← last_fun_split_fun f g, H]; simp theorem append_fun_inj {α α' : typevec n} {β β' : Type*} {f f' : α ⟹ α'} {g g' : β → β'} : f ::: g = f' ::: g' → f = f' ∧ g = g' := split_fun_inj theorem split_fun_comp {α₀ α₁ α₂ : typevec (n+1)} (f₀ : drop α₀ ⟹ drop α₁) (f₁ : drop α₁ ⟹ drop α₂) (g₀ : last α₀ → last α₁) (g₁ : last α₁ → last α₂) : split_fun (f₁ ⊚ f₀) (g₁ ∘ g₀) = split_fun f₁ g₁ ⊚ split_fun f₀ g₀ := eq_of_drop_last_eq rfl rfl theorem append_fun_comp_split_fun {α γ : typevec n} {β δ : Type*} {ε : typevec (n + 1)} (f₀ : drop ε ⟹ α) (f₁ : α ⟹ γ) (g₀ : last ε → β) (g₁ : β → δ) : append_fun f₁ g₁ ⊚ split_fun f₀ g₀ = split_fun (f₁ ⊚ f₀) (g₁ ∘ g₀) := (split_fun_comp _ _ _ _).symm lemma append_fun_comp {α₀ α₁ α₂ : typevec n} {β₀ β₁ β₂ : Type*} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) (g₀ : β₀ → β₁) (g₁ : β₁ → β₂) : f₁ ⊚ f₀ ::: g₁ ∘ g₀ = (f₁ ::: g₁) ⊚ (f₀ ::: g₀) := eq_of_drop_last_eq rfl rfl lemma append_fun_comp' {α₀ α₁ α₂ : typevec n} {β₀ β₁ β₂ : Type*} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) (g₀ : β₀ → β₁) (g₁ : β₁ → β₂) : (f₁ ::: g₁) ⊚ (f₀ ::: g₀) = f₁ ⊚ f₀ ::: g₁ ∘ g₀ := eq_of_drop_last_eq rfl rfl lemma nil_fun_comp {α₀ : typevec 0} (f₀ : α₀ ⟹ fin2.elim0) : nil_fun ⊚ f₀ = f₀ := funext $ λ x, fin2.elim0 x theorem append_fun_comp_id {α : typevec n} {β₀ β₁ β₂ : Type*} (g₀ : β₀ → β₁) (g₁ : β₁ → β₂) : @id _ α ::: g₁ ∘ g₀ = (id ::: g₁) ⊚ (id ::: g₀) := eq_of_drop_last_eq rfl rfl @[simp] theorem drop_fun_comp {α₀ α₁ α₂ : typevec (n+1)} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) : drop_fun (f₁ ⊚ f₀) = drop_fun f₁ ⊚ drop_fun f₀ := rfl @[simp] theorem last_fun_comp {α₀ α₁ α₂ : typevec (n+1)} (f₀ : α₀ ⟹ α₁) (f₁ : α₁ ⟹ α₂) : last_fun (f₁ ⊚ f₀) = last_fun f₁ ∘ last_fun f₀ := rfl theorem append_fun_aux {α α' : typevec n} {β β' : Type*} (f : α ::: β ⟹ α' ::: β') : drop_fun f ::: last_fun f = f := eq_of_drop_last_eq rfl rfl theorem append_fun_id_id {α : typevec n} {β : Type*} : @typevec.id n α ::: @_root_.id β = typevec.id := eq_of_drop_last_eq rfl rfl instance subsingleton0 : subsingleton (typevec 0) := ⟨ λ a b, funext $ λ a, fin2.elim0 a ⟩ run_cmd do mk_simp_attr `typevec, tactic.add_doc_string `simp_attr.typevec "simp set for the manipulation of typevec and arrow expressions" local prefix `♯`:0 := cast (by try { simp }; congr' 1; try { simp }) /-- cases distinction for 0-length type vector -/ protected def cases_nil {β : typevec 0 → Sort*} (f : β fin2.elim0) : Π v, β v := λ v, ♯ f /-- cases distinction for (n+1)-length type vector -/ protected def cases_cons (n : ℕ) {β : typevec (n+1) → Sort*} (f : Π t (v : typevec n), β (v ::: t)) : Π v, β v := λ v : typevec (n+1), ♯ f v.last v.drop protected lemma cases_nil_append1 {β : typevec 0 → Sort*} (f : β fin2.elim0) : typevec.cases_nil f fin2.elim0 = f := rfl protected lemma cases_cons_append1 (n : ℕ) {β : typevec (n+1) → Sort*} (f : Π t (v : typevec n), β (v ::: t)) (v : typevec n) (α) : typevec.cases_cons n f (v ::: α) = f α v := rfl /-- cases distinction for an arrow in the category of 0-length type vectors -/ def typevec_cases_nil₃ {β : Π v v' : typevec 0, v ⟹ v' → Sort*} (f : β fin2.elim0 fin2.elim0 nil_fun) : Π v v' fs, β v v' fs := λ v v' fs, begin refine cast _ f; congr' 1; ext; try { intros; casesm fin2 0 }, refl end /-- cases distinction for an arrow in the category of (n+1)-length type vectors -/ def typevec_cases_cons₃ (n : ℕ) {β : Π v v' : typevec (n+1), v ⟹ v' → Sort*} (F : Π t t' (f : t → t') (v v' : typevec n) (fs : v ⟹ v'), β (v ::: t) (v' ::: t') (fs ::: f)) : Π v v' fs, β v v' fs := begin intros v v', rw [←append1_drop_last v, ←append1_drop_last v'], intro fs, rw [←split_drop_fun_last_fun fs], apply F end /-- specialized cases distinction for an arrow in the category of 0-length type vectors -/ def typevec_cases_nil₂ {β : fin2.elim0 ⟹ fin2.elim0 → Sort*} (f : β nil_fun) : Π f, β f := begin intro g, have : g = nil_fun, ext ⟨ ⟩, rw this, exact f end /-- specialized cases distinction for an arrow in the category of (n+1)-length type vectors -/ def typevec_cases_cons₂ (n : ℕ) (t t' : Type*) (v v' : typevec (n)) {β : (v ::: t) ⟹ (v' ::: t') → Sort*} (F : Π (f : t → t') (fs : v ⟹ v'), β (fs ::: f)) : Π fs, β fs := begin intro fs, rw [←split_drop_fun_last_fun fs], apply F end lemma typevec_cases_nil₂_append_fun {β : fin2.elim0 ⟹ fin2.elim0 → Sort*} (f : β nil_fun) : typevec_cases_nil₂ f nil_fun = f := rfl lemma typevec_cases_cons₂_append_fun (n : ℕ) (t t' : Type*) (v v' : typevec (n)) {β : (v ::: t) ⟹ (v' ::: t') → Sort*} (F : Π (f : t → t') (fs : v ⟹ v'), β (fs ::: f)) (f fs) : typevec_cases_cons₂ n t t' v v' F (fs ::: f) = F f fs := rfl /- for lifting predicates and relations -/ /-- `pred_last α p x` predicates `p` of the last element of `x : α.append1 β`. -/ def pred_last (α : typevec n) {β : Type*} (p : β → Prop) : Π ⦃i⦄, (α.append1 β) i → Prop | (fin2.fs i) := λ x, true | fin2.fz := p /-- `rel_last α r x y` says that `p` the last elements of `x y : α.append1 β` are related by `r` and all the other elements are equal. -/ def rel_last (α : typevec n) {β γ : Type*} (r : β → γ → Prop) : Π ⦃i⦄, (α.append1 β) i → (α.append1 γ) i → Prop | (fin2.fs i) := eq | fin2.fz := r section liftp' open nat /-- `repeat n t` is a `n-length` type vector that contains `n` occurences of `t` -/ def repeat : Π (n : ℕ) (t : Sort*), typevec n | 0 t := fin2.elim0 | (nat.succ i) t := append1 (repeat i t) t /-- `prod α β` is the pointwise product of the components of `α` and `β` -/ def prod : Π {n} (α β : typevec.{u} n), typevec n | 0 α β := fin2.elim0 | (n+1) α β := prod (drop α) (drop β) ::: (last α × last β) localized "infix ` ⊗ `:45 := typevec.prod" in mvfunctor /-- `const x α` is an arrow that ignores its source and constructs a `typevec` that contains nothing but `x` -/ protected def const {β} (x : β) : Π {n} (α : typevec n), α ⟹ repeat _ β | (succ n) α (fin2.fs i) := const (drop α) _ | (succ n) α fin2.fz := λ _, x open function (uncurry) /-- vector of equality on a product of vectors -/ def repeat_eq : Π {n} (α : typevec n), α ⊗ α ⟹ repeat _ Prop | 0 α := nil_fun | (succ n) α := repeat_eq (drop α) ::: uncurry eq lemma const_append1 {β γ} (x : γ) {n} (α : typevec n) : typevec.const x (α ::: β) = append_fun (typevec.const x α) (λ _, x) := by ext i : 1; cases i; refl lemma eq_nil_fun {α β : typevec 0} (f : α ⟹ β) : f = nil_fun := by ext x; cases x lemma id_eq_nil_fun {α : typevec 0} : @id _ α = nil_fun := by ext x; cases x lemma const_nil {β} (x : β) (α : typevec 0) : typevec.const x α = nil_fun := by ext i : 1; cases i; refl @[typevec] lemma repeat_eq_append1 {β} {n} (α : typevec n) : repeat_eq (α ::: β) = split_fun (repeat_eq α) (uncurry eq) := by induction n; refl @[typevec] lemma repeat_eq_nil (α : typevec 0) : repeat_eq α = nil_fun := by ext i : 1; cases i; refl /-- predicate on a type vector to constrain only the last object -/ def pred_last' (α : typevec n) {β : Type*} (p : β → Prop) : α ::: β ⟹ repeat (n+1) Prop := split_fun (typevec.const true α) p /-- predicate on the product of two type vectors to constrain only their last object -/ def rel_last' (α : typevec n) {β : Type*} (p : β → β → Prop) : (α ::: β ⊗ α ::: β) ⟹ repeat (n+1) Prop := split_fun (repeat_eq α) (uncurry p) /-- given `F : typevec.{u} (n+1) → Type u`, `curry F : Type u → typevec.{u} → Type u`, i.e. its first argument can be fed in separately from the rest of the vector of arguments -/ def curry (F : typevec.{u} (n+1) → Type*) (α : Type u) (β : typevec.{u} n) : Type* := F (β ::: α) instance curry.inhabited (F : typevec.{u} (n+1) → Type*) (α : Type u) (β : typevec.{u} n) [I : inhabited (F $ β ::: α)]: inhabited (curry F α β) := I /-- arrow to remove one element of a `repeat` vector -/ def drop_repeat (α : Type*) : Π {n}, drop (repeat (succ n) α) ⟹ repeat n α | (succ n) (fin2.fs i) := drop_repeat i | (succ n) fin2.fz := _root_.id /-- projection for a repeat vector -/ def of_repeat {α : Sort*} : Π {n i}, repeat n α i → α | ._ fin2.fz := _root_.id | ._ (fin2.fs i) := @of_repeat _ i lemma const_iff_true {α : typevec n} {i x p} : of_repeat (typevec.const p α i x) ↔ p := by induction i; [refl, erw [typevec.const,@i_ih (drop α) x]] -- variables {F : typevec.{u} n → Type*} [mvfunctor F] variables {α β γ : typevec.{u} n} variables (p : α ⟹ repeat n Prop) (r : α ⊗ α ⟹ repeat n Prop) /-- left projection of a `prod` vector -/ def prod.fst : Π {n} {α β : typevec.{u} n}, α ⊗ β ⟹ α | (succ n) α β (fin2.fs i) := @prod.fst _ (drop α) (drop β) i | (succ n) α β fin2.fz := _root_.prod.fst /-- right projection of a `prod` vector -/ def prod.snd : Π {n} {α β : typevec.{u} n}, α ⊗ β ⟹ β | (succ n) α β (fin2.fs i) := @prod.snd _ (drop α) (drop β) i | (succ n) α β fin2.fz := _root_.prod.snd /-- introduce a product where both components are the same -/ def prod.diag : Π {n} {α : typevec.{u} n}, α ⟹ α ⊗ α | (succ n) α (fin2.fs i) x := @prod.diag _ (drop α) _ x | (succ n) α fin2.fz x := (x,x) /-- constructor for `prod` -/ def prod.mk : Π {n} {α β : typevec.{u} n} (i : fin2 n), α i → β i → (α ⊗ β) i | (succ n) α β (fin2.fs i) := prod.mk i | (succ n) α β fin2.fz := _root_.prod.mk @[simp] lemma prod_fst_mk {α β : typevec n} (i : fin2 n) (a : α i) (b : β i) : typevec.prod.fst i (prod.mk i a b) = a := by induction i; simp [prod.fst, prod.mk, *] at * @[simp] lemma prod_snd_mk {α β : typevec n} (i : fin2 n) (a : α i) (b : β i) : typevec.prod.snd i (prod.mk i a b) = b := by induction i; simp [prod.snd, prod.mk, *] at * /-- `prod` is functorial -/ protected def prod.map : Π {n} {α α' β β' : typevec.{u} n}, (α ⟹ β) → (α' ⟹ β') → α ⊗ α' ⟹ β ⊗ β' | (succ n) α α' β β' x y (fin2.fs i) a := @prod.map _ (drop α) (drop α') (drop β) (drop β') (drop_fun x) (drop_fun y) _ a | (succ n) α α' β β' x y fin2.fz a := (x _ a.1,y _ a.2) localized "infix ` ⊗' `:45 := typevec.prod.map" in mvfunctor theorem fst_prod_mk {α α' β β' : typevec n} (f : α ⟹ β) (g : α' ⟹ β') : typevec.prod.fst ⊚ (f ⊗' g) = f ⊚ typevec.prod.fst := by ext i; induction i; [refl, apply i_ih] theorem snd_prod_mk {α α' β β' : typevec n} (f : α ⟹ β) (g : α' ⟹ β') : typevec.prod.snd ⊚ (f ⊗' g) = g ⊚ typevec.prod.snd := by ext i; induction i; [refl, apply i_ih] theorem fst_diag {α : typevec n} : typevec.prod.fst ⊚ (prod.diag : α ⟹ _) = id := by ext i; induction i; [refl, apply i_ih] theorem snd_diag {α : typevec n} : typevec.prod.snd ⊚ (prod.diag : α ⟹ _) = id := by ext i; induction i; [refl, apply i_ih] lemma repeat_eq_iff_eq {α : typevec n} {i x y} : of_repeat (repeat_eq α i (prod.mk _ x y)) ↔ x = y := by induction i; [refl, erw [repeat_eq,@i_ih (drop α) x y]] /-- given a predicate vector `p` over vector `α`, `subtype_ p` is the type of vectors that contain an `α` that satisfies `p` -/ def subtype_ : Π {n} {α : typevec.{u} n} (p : α ⟹ repeat n Prop), typevec n | ._ α p fin2.fz := _root_.subtype (λ x, p fin2.fz x) | ._ α p (fin2.fs i) := subtype_ (drop_fun p) i /-- projection on `subtype_` -/ def subtype_val : Π {n} {α : typevec.{u} n} (p : α ⟹ repeat n Prop), subtype_ p ⟹ α | (succ n) α p (fin2.fs i) := @subtype_val n _ _ i | (succ n) α p fin2.fz := _root_.subtype.val /-- arrow that rearranges the type of `subtype_` to turn a subtype of vector into a vector of subtypes -/ def to_subtype : Π {n} {α : typevec.{u} n} (p : α ⟹ repeat n Prop), (λ (i : fin2 n), { x // of_repeat $ p i x }) ⟹ subtype_ p | (succ n) α p (fin2.fs i) x := to_subtype (drop_fun p) i x | (succ n) α p fin2.fz x := x /-- arrow that rearranges the type of `subtype_` to turn a vector of subtypes into a subtype of vector -/ def of_subtype : Π {n} {α : typevec.{u} n} (p : α ⟹ repeat n Prop), subtype_ p ⟹ (λ (i : fin2 n), { x // of_repeat $ p i x }) | (succ n) α p (fin2.fs i) x := of_subtype _ i x | (succ n) α p fin2.fz x := x /-- similar to `to_subtype` adapted to relations (i.e. predicate on product) -/ def to_subtype' : Π {n} {α : typevec.{u} n} (p : α ⊗ α ⟹ repeat n Prop), (λ (i : fin2 n), { x : α i × α i // of_repeat $ p i (prod.mk _ x.1 x.2) }) ⟹ subtype_ p | (succ n) α p (fin2.fs i) x := to_subtype' (drop_fun p) i x | (succ n) α p fin2.fz x := ⟨x.val,cast (by congr; simp [prod.mk]) x.property⟩ /-- similar to `of_subtype` adapted to relations (i.e. predicate on product) -/ def of_subtype' : Π {n} {α : typevec.{u} n} (p : α ⊗ α ⟹ repeat n Prop), subtype_ p ⟹ (λ (i : fin2 n), { x : α i × α i // of_repeat $ p i (prod.mk _ x.1 x.2) }) | ._ α p (fin2.fs i) x := of_subtype' _ i x | ._ α p fin2.fz x := ⟨x.val,cast (by congr; simp [prod.mk]) x.property⟩ /-- similar to `diag` but the target vector is a `subtype_` guaranteeing the equality of the components -/ def diag_sub : Π {n} {α : typevec.{u} n}, α ⟹ subtype_ (repeat_eq α) | (succ n) α (fin2.fs i) x := @diag_sub _ (drop α) _ x | (succ n) α fin2.fz x := ⟨(x,x), rfl⟩ lemma subtype_val_nil {α : typevec.{u} 0} (ps : α ⟹ repeat 0 Prop) : typevec.subtype_val ps = nil_fun := funext $ by rintro ⟨ ⟩; refl lemma diag_sub_val {n} {α : typevec.{u} n} : subtype_val (repeat_eq α) ⊚ diag_sub = prod.diag := by ext i; induction i; [refl, apply i_ih] lemma prod_id : Π {n} {α β : typevec.{u} n}, (id ⊗' id) = (id : α ⊗ β ⟹ _) := begin intros, ext i a, induction i, { cases a, refl }, { apply i_ih }, end lemma append_prod_append_fun {n} {α α' β β' : typevec.{u} n} {φ φ' ψ ψ' : Type u} {f₀ : α ⟹ α'} {g₀ : β ⟹ β'} {f₁ : φ → φ'} {g₁ : ψ → ψ'} : (f₀ ⊗' g₀) ::: _root_.prod.map f₁ g₁ = ((f₀ ::: f₁) ⊗' (g₀ ::: g₁)) := by ext i a; cases i; [cases a, skip]; refl end liftp' @[simp] lemma drop_fun_diag {α} : drop_fun (@prod.diag (n+1) α) = prod.diag := by { ext i : 2, induction i; simp [drop_fun,*]; refl } @[simp] lemma drop_fun_subtype_val {α} (p : α ⟹ repeat (n+1) Prop) : drop_fun (subtype_val p) = subtype_val _ := rfl @[simp] lemma last_fun_subtype_val {α} (p : α ⟹ repeat (n+1) Prop) : last_fun (subtype_val p) = subtype.val := rfl @[simp] lemma drop_fun_to_subtype {α} (p : α ⟹ repeat (n+1) Prop) : drop_fun (to_subtype p) = to_subtype _ := by { ext i : 2, induction i; simp [drop_fun,*]; refl } @[simp] lemma last_fun_to_subtype {α} (p : α ⟹ repeat (n+1) Prop) : last_fun (to_subtype p) = _root_.id := by { ext i : 2, induction i; simp [drop_fun,*]; refl } @[simp] lemma drop_fun_of_subtype {α} (p : α ⟹ repeat (n+1) Prop) : drop_fun (of_subtype p) = of_subtype _ := by { ext i : 2, induction i; simp [drop_fun,*]; refl } @[simp] lemma last_fun_of_subtype {α} (p : α ⟹ repeat (n+1) Prop) : last_fun (of_subtype p) = _root_.id := by { ext i : 2, induction i; simp [drop_fun,*]; refl } @[simp] lemma drop_fun_rel_last {α : typevec n} {β} (R : β → β → Prop) : drop_fun (rel_last' α R) = repeat_eq α := rfl attribute [simp] drop_append1' open_locale mvfunctor @[simp] lemma drop_fun_prod {α α' β β' : typevec (n+1)} (f : α ⟹ β) (f' : α' ⟹ β') : drop_fun (f ⊗' f') = (drop_fun f ⊗' drop_fun f') := by { ext i : 2, induction i; simp [drop_fun,*]; refl } @[simp] lemma last_fun_prod {α α' β β' : typevec (n+1)} (f : α ⟹ β) (f' : α' ⟹ β') : last_fun (f ⊗' f') = _root_.prod.map (last_fun f) (last_fun f') := by { ext i : 1, induction i; simp [last_fun,*]; refl } @[simp] lemma drop_fun_from_append1_drop_last {α : typevec (n+1)} : drop_fun (@from_append1_drop_last _ α) = id := rfl @[simp] lemma last_fun_from_append1_drop_last {α : typevec (n+1)} : last_fun (@from_append1_drop_last _ α) = _root_.id := rfl @[simp] lemma drop_fun_id {α : typevec (n+1)} : drop_fun (@typevec.id _ α) = id := rfl @[simp] lemma prod_map_id {α β : typevec n} : (@typevec.id _ α ⊗' @typevec.id _ β) = id := by { ext i : 2, induction i; simp only [typevec.prod.map,*,drop_fun_id], cases x, refl, refl } @[simp] lemma subtype_val_diag_sub {α : typevec n} : subtype_val (repeat_eq α) ⊚ diag_sub = prod.diag := by { clear_except, ext i, induction i; [refl, apply i_ih], } @[simp] lemma to_subtype_of_subtype {α : typevec n} (p : α ⟹ repeat n Prop) : to_subtype p ⊚ of_subtype p = id := by ext i x; induction i; dsimp only [id, to_subtype, comp, of_subtype] at *; simp * @[simp] lemma subtype_val_to_subtype {α : typevec n} (p : α ⟹ repeat n Prop) : subtype_val p ⊚ to_subtype p = λ _, subtype.val := by ext i x; induction i; dsimp only [to_subtype, comp, subtype_val] at *; simp * @[simp] lemma to_subtype_of_subtype_assoc {α β : typevec n} (p : α ⟹ repeat n Prop) (f : β ⟹ subtype_ p) : @to_subtype n _ p ⊚ of_subtype _ ⊚ f = f := by rw [← comp_assoc,to_subtype_of_subtype]; simp @[simp] lemma to_subtype'_of_subtype' {α : typevec n} (r : α ⊗ α ⟹ repeat n Prop) : to_subtype' r ⊚ of_subtype' r = id := by ext i x; induction i; dsimp only [id, to_subtype', comp, of_subtype'] at *; simp [subtype.eta, *] lemma subtype_val_to_subtype' {α : typevec n} (r : α ⊗ α ⟹ repeat n Prop) : subtype_val r ⊚ to_subtype' r = λ i x, prod.mk i x.1.fst x.1.snd := by ext i x; induction i; dsimp only [id, to_subtype', comp, subtype_val, prod.mk] at *; simp * end typevec
9e31fb5b62d84a475dafaa700c9978871fa378ad
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/int/conditionally_complete_order.lean
63efc9885b7e29a60e91a827d4a1728a9202e454
[ "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,079
lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import order.conditionally_complete_lattice.basic import data.int.least_greatest /-! ## `ℤ` forms a conditionally complete linear order > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The integers form a conditionally complete linear order. -/ open int open_locale classical noncomputable theory instance : conditionally_complete_linear_order ℤ := { Sup := λ s, if h : s.nonempty ∧ bdd_above s then greatest_of_bdd (classical.some h.2) (classical.some_spec h.2) h.1 else 0, Inf := λ s, if h : s.nonempty ∧ bdd_below s then least_of_bdd (classical.some h.2) (classical.some_spec h.2) h.1 else 0, le_cSup := begin intros s n hs hns, have : s.nonempty ∧ bdd_above s := ⟨⟨n, hns⟩, hs⟩, rw [dif_pos this], exact (greatest_of_bdd _ _ _).2.2 n hns end, cSup_le := begin intros s n hs hns, have : s.nonempty ∧ bdd_above s := ⟨hs, ⟨n, hns⟩⟩, rw [dif_pos this], exact hns (greatest_of_bdd _ (classical.some_spec this.2) _).2.1 end, cInf_le := begin intros s n hs hns, have : s.nonempty ∧ bdd_below s := ⟨⟨n, hns⟩, hs⟩, rw [dif_pos this], exact (least_of_bdd _ _ _).2.2 n hns end, le_cInf := begin intros s n hs hns, have : s.nonempty ∧ bdd_below s := ⟨hs, ⟨n, hns⟩⟩, rw [dif_pos this], exact hns (least_of_bdd _ (classical.some_spec this.2) _).2.1 end, .. int.linear_order, ..linear_order.to_lattice } namespace int lemma cSup_eq_greatest_of_bdd {s : set ℤ} [decidable_pred (∈ s)] (b : ℤ) (Hb : ∀ z ∈ s, z ≤ b) (Hinh : ∃ z : ℤ, z ∈ s) : Sup s = greatest_of_bdd b Hb Hinh := begin convert dif_pos _ using 1, { convert coe_greatest_of_bdd_eq _ (classical.some_spec (⟨b, Hb⟩ : bdd_above s)) _ }, { exact ⟨Hinh, b, Hb⟩, } end @[simp] lemma cSup_empty : Sup (∅ : set ℤ) = 0 := dif_neg (by simp) lemma cSup_of_not_bdd_above {s : set ℤ} (h : ¬ bdd_above s) : Sup s = 0 := dif_neg (by simp [h]) lemma cInf_eq_least_of_bdd {s : set ℤ} [decidable_pred (∈ s)] (b : ℤ) (Hb : ∀ z ∈ s, b ≤ z) (Hinh : ∃ z : ℤ, z ∈ s) : Inf s = least_of_bdd b Hb Hinh := begin convert dif_pos _ using 1, { convert coe_least_of_bdd_eq _ (classical.some_spec (⟨b, Hb⟩ : bdd_below s)) _ }, { exact ⟨Hinh, b, Hb⟩, } end @[simp] lemma cInf_empty : Inf (∅ : set ℤ) = 0 := dif_neg (by simp) lemma cInf_of_not_bdd_below {s : set ℤ} (h : ¬ bdd_below s) : Inf s = 0 := dif_neg (by simp [h]) lemma cSup_mem {s : set ℤ} (h1 : s.nonempty) (h2 : bdd_above s) : Sup s ∈ s := begin convert (greatest_of_bdd _ (classical.some_spec h2) h1).2.1, exact dif_pos ⟨h1, h2⟩, end lemma cInf_mem {s : set ℤ} (h1 : s.nonempty) (h2 : bdd_below s) : Inf s ∈ s := begin convert (least_of_bdd _ (classical.some_spec h2) h1).2.1, exact dif_pos ⟨h1, h2⟩, end end int
257b1c8f9d6739ea7ce008e2f8cafecb539e16e3
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/dynamics/ergodic/conservative.lean
254d7ea25256ea20abcdec17b6a2fd5f2323801f
[ "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
10,063
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import measure_theory.constructions.borel_space import dynamics.ergodic.measure_preserving import combinatorics.pigeonhole /-! # Conservative systems In this file we define `f : α → α` to be a *conservative* system w.r.t a measure `μ` if `f` is non-singular (`measure_theory.quasi_measure_preserving`) and for every measurable set `s` of positive measure at least one point `x ∈ s` returns back to `s` after some number of iterations of `f`. There are several properties that look like they are stronger than this one but actually follow from it: * `measure_theory.conservative.frequently_measure_inter_ne_zero`, `measure_theory.conservative.exists_gt_measure_inter_ne_zero`: if `μ s ≠ 0`, then for infinitely many `n`, the measure of `s ∩ (f^[n]) ⁻¹' s` is positive. * `measure_theory.conservative.measure_mem_forall_ge_image_not_mem_eq_zero`, `measure_theory.conservative.ae_mem_imp_frequently_image_mem`: a.e. every point of `s` visits `s` infinitely many times (Poincaré recurrence theorem). We also prove the topological Poincaré recurrence theorem `measure_theory.conservative.ae_frequently_mem_of_mem_nhds`. Let `f : α → α` be a conservative dynamical system on a topological space with second countable topology and measurable open sets. Then almost every point `x : α` is recurrent: it visits every neighborhood `s ∈ 𝓝 x` infinitely many times. ## Tags conservative dynamical system, Poincare recurrence theorem -/ noncomputable theory open classical set filter measure_theory finset function topological_space open_locale classical topological_space variables {ι : Type*} {α : Type*} [measurable_space α] {f : α → α} {s : set α} {μ : measure α} namespace measure_theory open measure /-- We say that a non-singular (`measure_theory.quasi_measure_preserving`) self-map is *conservative* if for any measurable set `s` of positive measure there exists `x ∈ s` such that `x` returns back to `s` under some iteration of `f`. -/ structure conservative (f : α → α) (μ : measure α . volume_tac) extends quasi_measure_preserving f μ μ : Prop := (exists_mem_image_mem : ∀ ⦃s⦄, measurable_set s → μ s ≠ 0 → ∃ (x ∈ s) (m ≠ 0), f^[m] x ∈ s) /-- A self-map preserving a finite measure is conservative. -/ protected lemma measure_preserving.conservative [is_finite_measure μ] (h : measure_preserving f μ μ) : conservative f μ := ⟨h.quasi_measure_preserving, λ s hsm h0, h.exists_mem_image_mem hsm h0⟩ namespace conservative /-- The identity map is conservative w.r.t. any measure. -/ protected lemma id (μ : measure α) : conservative id μ := { to_quasi_measure_preserving := quasi_measure_preserving.id μ, exists_mem_image_mem := λ s hs h0, let ⟨x, hx⟩ := nonempty_of_measure_ne_zero h0 in ⟨x, hx, 1, one_ne_zero, hx⟩ } /-- If `f` is a conservative map and `s` is a measurable set of nonzero measure, then for infinitely many values of `m` a positive measure of points `x ∈ s` returns back to `s` after `m` iterations of `f`. -/ lemma frequently_measure_inter_ne_zero (hf : conservative f μ) (hs : measurable_set s) (h0 : μ s ≠ 0) : ∃ᶠ m in at_top, μ (s ∩ (f^[m]) ⁻¹' s) ≠ 0 := begin by_contra H, simp only [not_frequently, eventually_at_top, ne.def, not_not] at H, rcases H with ⟨N, hN⟩, induction N with N ihN, { apply h0, simpa using hN 0 le_rfl }, rw [imp_false] at ihN, push_neg at ihN, rcases ihN with ⟨n, hn, hμn⟩, set T := s ∩ ⋃ n ≥ N + 1, (f^[n]) ⁻¹' s, have hT : measurable_set T, from hs.inter (measurable_set.bUnion (countable_encodable _) (λ _ _, hf.measurable.iterate _ hs)), have hμT : μ T = 0, { convert (measure_bUnion_null_iff $ countable_encodable _).2 hN, rw ← set.inter_bUnion, refl }, have : μ ((s ∩ (f^[n]) ⁻¹' s) \ T) ≠ 0, by rwa [measure_diff_null hμT], rcases hf.exists_mem_image_mem ((hs.inter (hf.measurable.iterate n hs)).diff hT) this with ⟨x, ⟨⟨hxs, hxn⟩, hxT⟩, m, hm0, ⟨hxms, hxm⟩, hxx⟩, refine hxT ⟨hxs, mem_bUnion_iff.2 ⟨n + m, _, _⟩⟩, { exact add_le_add hn (nat.one_le_of_lt $ pos_iff_ne_zero.2 hm0) }, { rwa [set.mem_preimage, ← iterate_add_apply] at hxm } end /-- If `f` is a conservative map and `s` is a measurable set of nonzero measure, then for an arbitrarily large `m` a positive measure of points `x ∈ s` returns back to `s` after `m` iterations of `f`. -/ lemma exists_gt_measure_inter_ne_zero (hf : conservative f μ) (hs : measurable_set s) (h0 : μ s ≠ 0) (N : ℕ) : ∃ m > N, μ (s ∩ (f^[m]) ⁻¹' s) ≠ 0 := let ⟨m, hm, hmN⟩ := ((hf.frequently_measure_inter_ne_zero hs h0).and_eventually (eventually_gt_at_top N)).exists in ⟨m, hmN, hm⟩ /-- Poincaré recurrence theorem: given a conservative map `f` and a measurable set `s`, the set of points `x ∈ s` such that `x` does not return to `s` after `≥ n` iterations has measure zero. -/ lemma measure_mem_forall_ge_image_not_mem_eq_zero (hf : conservative f μ) (hs : measurable_set s) (n : ℕ) : μ {x ∈ s | ∀ m ≥ n, f^[m] x ∉ s} = 0 := begin by_contradiction H, have : measurable_set (s ∩ {x | ∀ m ≥ n, f^[m] x ∉ s}), { simp only [set_of_forall, ← compl_set_of], exact hs.inter (measurable_set.bInter (countable_encodable _) (λ m _, hf.measurable.iterate m hs.compl)) }, rcases (hf.exists_gt_measure_inter_ne_zero this H) n with ⟨m, hmn, hm⟩, rcases nonempty_of_measure_ne_zero hm with ⟨x, ⟨hxs, hxn⟩, hxm, -⟩, exact hxn m hmn.lt.le hxm end /-- Poincaré recurrence theorem: given a conservative map `f` and a measurable set `s`, almost every point `x ∈ s` returns back to `s` infinitely many times. -/ lemma ae_mem_imp_frequently_image_mem (hf : conservative f μ) (hs : measurable_set s) : ∀ᵐ x ∂μ, x ∈ s → ∃ᶠ n in at_top, (f^[n] x) ∈ s := begin simp only [frequently_at_top, @forall_swap (_ ∈ s), ae_all_iff], intro n, filter_upwards [measure_zero_iff_ae_nmem.1 (hf.measure_mem_forall_ge_image_not_mem_eq_zero hs n)], simp end lemma inter_frequently_image_mem_ae_eq (hf : conservative f μ) (hs : measurable_set s) : (s ∩ {x | ∃ᶠ n in at_top, f^[n] x ∈ s} : set α) =ᵐ[μ] s := inter_eventually_eq_left.2 $ hf.ae_mem_imp_frequently_image_mem hs lemma measure_inter_frequently_image_mem_eq (hf : conservative f μ) (hs : measurable_set s) : μ (s ∩ {x | ∃ᶠ n in at_top, f^[n] x ∈ s}) = μ s := measure_congr (hf.inter_frequently_image_mem_ae_eq hs) /-- Poincaré recurrence theorem: if `f` is a conservative dynamical system and `s` is a measurable set, then for `μ`-a.e. `x`, if the orbit of `x` visits `s` at least once, then it visits `s` infinitely many times. -/ lemma ae_forall_image_mem_imp_frequently_image_mem (hf : conservative f μ) (hs : measurable_set s) : ∀ᵐ x ∂μ, ∀ k, f^[k] x ∈ s → ∃ᶠ n in at_top, (f^[n] x) ∈ s := begin refine ae_all_iff.2 (λ k, _), refine (hf.ae_mem_imp_frequently_image_mem (hf.measurable.iterate k hs)).mono (λ x hx hk, _), rw [← map_add_at_top_eq_nat k, frequently_map], refine (hx hk).mono (λ n hn, _), rwa [add_comm, iterate_add_apply] end /-- If `f` is a conservative self-map and `s` is a measurable set of positive measure, then `μ.ae`-frequently we have `x ∈ s` and `s` returns to `s` under infinitely many iterations of `f`. -/ lemma frequently_ae_mem_and_frequently_image_mem (hf : conservative f μ) (hs : measurable_set s) (h0 : μ s ≠ 0) : ∃ᵐ x ∂μ, x ∈ s ∧ ∃ᶠ n in at_top, (f^[n] x) ∈ s := ((frequently_ae_mem_iff.2 h0).and_eventually (hf.ae_mem_imp_frequently_image_mem hs)).mono $ λ x hx, ⟨hx.1, hx.2 hx.1⟩ /-- Poincaré recurrence theorem. Let `f : α → α` be a conservative dynamical system on a topological space with second countable topology and measurable open sets. Then almost every point `x : α` is recurrent: it visits every neighborhood `s ∈ 𝓝 x` infinitely many times. -/ lemma ae_frequently_mem_of_mem_nhds [topological_space α] [second_countable_topology α] [opens_measurable_space α] {f : α → α} {μ : measure α} (h : conservative f μ) : ∀ᵐ x ∂μ, ∀ s ∈ 𝓝 x, ∃ᶠ n in at_top, f^[n] x ∈ s := begin have : ∀ s ∈ countable_basis α, ∀ᵐ x ∂μ, x ∈ s → ∃ᶠ n in at_top, (f^[n] x) ∈ s, from λ s hs, h.ae_mem_imp_frequently_image_mem (is_open_of_mem_countable_basis hs).measurable_set, refine ((ae_ball_iff $ countable_countable_basis α).2 this).mono (λ x hx s hs, _), rcases (is_basis_countable_basis α).mem_nhds_iff.1 hs with ⟨o, hoS, hxo, hos⟩, exact (hx o hoS hxo).mono (λ n hn, hos hn) end /-- Iteration of a conservative system is a conservative system. -/ protected lemma iterate (hf : conservative f μ) (n : ℕ) : conservative (f^[n]) μ := begin cases n, { exact conservative.id μ }, -- Discharge the trivial case `n = 0` refine ⟨hf.1.iterate _, λ s hs hs0, _⟩, rcases (hf.frequently_ae_mem_and_frequently_image_mem hs hs0).exists with ⟨x, hxs, hx⟩, /- We take a point `x ∈ s` such that `f^[k] x ∈ s` for infinitely many values of `k`, then we choose two of these values `k < l` such that `k ≡ l [MOD (n + 1)]`. Then `f^[k] x ∈ s` and `(f^[n + 1])^[(l - k) / (n + 1)] (f^[k] x) = f^[l] x ∈ s`. -/ rw nat.frequently_at_top_iff_infinite at hx, rcases nat.exists_lt_modeq_of_infinite hx n.succ_pos with ⟨k, hk, l, hl, hkl, hn⟩, set m := (l - k) / (n + 1), have : (n + 1) * m = l - k, { apply nat.mul_div_cancel', exact (nat.modeq_iff_dvd' hkl.le).1 hn }, refine ⟨f^[k] x, hk, m, _, _⟩, { intro hm, rw [hm, mul_zero, eq_comm, tsub_eq_zero_iff_le] at this, exact this.not_lt hkl }, { rwa [← iterate_mul, this, ← iterate_add_apply, tsub_add_cancel_of_le], exact hkl.le } end end conservative end measure_theory
4e76a60985cc6adbac7a49a69528abbc76bf11da
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/limits/preserves/finite.lean
2512c091e6de69d8f2b51876f0dcd49452f227be
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
7,361
lean
/- Copyright (c) 2021 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import category_theory.limits.preserves.basic import category_theory.fin_category /-! # Preservation of finite (co)limits. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. These functors are also known as left exact (flat) or right exact functors when the categories involved are abelian, or more generally, finitely (co)complete. ## Related results * `category_theory.limits.preserves_finite_limits_of_preserves_equalizers_and_finite_products` : see `category_theory/limits/constructions/limits_of_products_and_equalizers.lean`. Also provides the dual version. * `category_theory.limits.preserves_finite_limits_iff_flat` : see `category_theory/flat_functors.lean`. -/ open category_theory namespace category_theory.limits -- declare the `v`'s first; see `category_theory.category` for an explanation universes w w₂ v₁ v₂ v₃ u₁ u₂ u₃ variables {C : Type u₁} [category.{v₁} C] variables {D : Type u₂} [category.{v₂} D] variables {E : Type u₃} [category.{v₃} E] variables {J : Type w} [small_category J] {K : J ⥤ C} /-- A functor is said to preserve finite limits, if it preserves all limits of shape `J`, where `J : Type` is a finite category. -/ class preserves_finite_limits (F : C ⥤ D) := (preserves_finite_limits : Π (J : Type) [small_category J] [fin_category J], preserves_limits_of_shape J F . tactic.apply_instance) attribute [instance] preserves_finite_limits.preserves_finite_limits /-- Preserving finite limits also implies preserving limits over finite shapes in higher universes, though through a noncomputable instance. -/ @[priority 100] noncomputable instance preserves_limits_of_shape_of_preserves_finite_limits (F : C ⥤ D) [preserves_finite_limits F] (J : Type w) [small_category J] [fin_category J] : preserves_limits_of_shape J F := by apply preserves_limits_of_shape_of_equiv (fin_category.equiv_as_type J) -- This is a dangerous instance as it has unbound universe variables. /-- If we preserve limits of some arbitrary size, then we preserve all finite limits. -/ noncomputable def preserves_limits_of_size.preserves_finite_limits (F : C ⥤ D) [preserves_limits_of_size.{w w₂} F] : preserves_finite_limits F := ⟨λ J sJ fJ, begin haveI := preserves_smallest_limits_of_preserves_limits F, exact preserves_limits_of_shape_of_equiv (fin_category.equiv_as_type J) F, end⟩ -- Added as a specialization of the dangerous instance above, for limits indexed in Type 0. @[priority 120] noncomputable instance preserves_limits_of_size.zero.preserves_finite_limits (F : C ⥤ D) [preserves_limits_of_size.{0 0} F] : preserves_finite_limits F := preserves_limits_of_size.preserves_finite_limits F -- An alternative specialization of the dangerous instance for small limits. @[priority 120] noncomputable instance preserves_limits.preserves_finite_limits (F : C ⥤ D) [preserves_limits F] : preserves_finite_limits F := preserves_limits_of_size.preserves_finite_limits F /-- We can always derive `preserves_finite_limits C` by showing that we are preserving limits at an arbitrary universe. -/ def preserves_finite_limits_of_preserves_finite_limits_of_size (F : C ⥤ D) (h : ∀ (J : Type w) {𝒥 : small_category J} (hJ : @fin_category J 𝒥), by { resetI, exact preserves_limits_of_shape J F }) : preserves_finite_limits F := ⟨λ J hJ hhJ, begin resetI, letI : category.{w w} (ulift_hom.{w} (ulift.{w 0} J)), { apply ulift_hom.category.{0}, exact category_theory.ulift_category J }, haveI := h (ulift_hom.{w} (ulift.{w} J)) category_theory.fin_category_ulift, exact preserves_limits_of_shape_of_equiv (ulift_hom_ulift_category.equiv.{w w} J).symm F end⟩ instance id_preserves_finite_limits : preserves_finite_limits (𝟭 C) := {} /-- The composition of two left exact functors is left exact. -/ def comp_preserves_finite_limits (F : C ⥤ D) (G : D ⥤ E) [preserves_finite_limits F] [preserves_finite_limits G] : preserves_finite_limits (F ⋙ G) := ⟨λ _ _ _, by { resetI, apply_instance }⟩ /-- A functor is said to preserve finite colimits, if it preserves all colimits of shape `J`, where `J : Type` is a finite category. -/ class preserves_finite_colimits (F : C ⥤ D) := (preserves_finite_colimits : Π (J : Type) [small_category J] [fin_category J], preserves_colimits_of_shape J F . tactic.apply_instance) attribute [instance] preserves_finite_colimits.preserves_finite_colimits /-- Preserving finite limits also implies preserving limits over finite shapes in higher universes, though through a noncomputable instance. -/ @[priority 100] noncomputable instance preserves_colimits_of_shape_of_preserves_finite_colimits (F : C ⥤ D) [preserves_finite_colimits F] (J : Type w) [small_category J] [fin_category J] : preserves_colimits_of_shape J F := by apply preserves_colimits_of_shape_of_equiv (fin_category.equiv_as_type J) /-- If we preserve colimits of some arbitrary size, then we preserve all finite colimits. -/ -- This is a dangerous instance as it has unbound universe variables. noncomputable def preserves_colimits_of_size.preserves_finite_colimits (F : C ⥤ D) [preserves_colimits_of_size.{w w₂} F] : preserves_finite_colimits F := ⟨λ J sJ fJ, begin haveI := preserves_smallest_colimits_of_preserves_colimits F, exact preserves_colimits_of_shape_of_equiv (fin_category.equiv_as_type J) F, end⟩ -- Added as a specialization of the dangerous instance above, for colimits indexed in Type 0. @[priority 120] noncomputable instance preserves_colimits_of_size.zero.preserves_finite_colimits (F : C ⥤ D) [preserves_colimits_of_size.{0 0} F] : preserves_finite_colimits F := preserves_colimits_of_size.preserves_finite_colimits F -- An alternative specialization of the dangerous instance for small colimits. @[priority 120] noncomputable instance preserves_colimits.preserves_finite_colimits (F : C ⥤ D) [preserves_colimits F] : preserves_finite_colimits F := preserves_colimits_of_size.preserves_finite_colimits F /-- We can always derive `preserves_finite_limits C` by showing that we are preserving limits at an arbitrary universe. -/ def preserves_finite_colimits_of_preserves_finite_colimits_of_size (F : C ⥤ D) (h : ∀ (J : Type w) {𝒥 : small_category J} (hJ : @fin_category J 𝒥), by { resetI, exact preserves_colimits_of_shape J F }) : preserves_finite_colimits F := ⟨λ J hJ hhJ, begin resetI, letI : category.{w w} (ulift_hom.{w} (ulift.{w 0} J)), { apply ulift_hom.category.{0}, exact category_theory.ulift_category J }, haveI := h (ulift_hom.{w} (ulift.{w} J)) category_theory.fin_category_ulift, exact preserves_colimits_of_shape_of_equiv (ulift_hom_ulift_category.equiv.{w w} J).symm F end⟩ instance id_preserves_finite_colimits : preserves_finite_colimits (𝟭 C) := {} /-- The composition of two right exact functors is right exact. -/ def comp_preserves_finite_colimits (F : C ⥤ D) (G : D ⥤ E) [preserves_finite_colimits F] [preserves_finite_colimits G] : preserves_finite_colimits (F ⋙ G) := ⟨λ _ _ _, by { resetI, apply_instance }⟩ end category_theory.limits
ea8dc1acf1815ce2bf4be70f22e81e3e95e8465e
29cc89d6158dd3b90acbdbcab4d2c7eb9a7dbf0f
/1.1/Homework week1.lean
78d0c840f89370ab1c486d4480ad8e8faa7f7574
[]
no_license
KjellZijlemaker/Logical_Verification_VU
ced0ba95316a30e3c94ba8eebd58ea004fa6f53b
4578b93bf1615466996157bb333c84122b201d99
refs/heads/master
1,585,966,086,108
1,549,187,704,000
1,549,187,704,000
155,690,284
0
0
null
null
null
null
UTF-8
Lean
false
false
1,284
lean
/- Homework 1.1: Basics — Specifications -/ /- Question 1: Snoc -/ /- 1.1. Define the function `snoc` that appends a single element to the end of a list. -/ def snoc {α : Type} : list α → α → list α | [] l := [] ++ [l] | (a::b) l := a :: b ++ [l] -- ZZZ: Jasmins solution using append. def snoc' {α : Type} : list α → α → list α | l x := l ++ [x] #reduce snoc [] 5 #reduce snoc [2,4,6] 8 /- 1.2. Convince yourself that your definition of `snoc` works by testing it on a few examples. -/ -- invoke `#reduce` here /- Question 2: Map -/ /- 2.1. Define a generic `map` function that applies a function to every element in a list. -/ def map {α : Type} {β : Type} (f : α → β) : list α → list β | [] := [] | (a::b) := (f a) :: (map b) #reduce map(+10)[2,4,5] #reduce map(/2)[10,20,30] /- 2.2. State the so-called functiorial properties of `map` as lemmas. Schematically: map (λx, x) xs = xs map (λx, g (f x)) xs = map g (map f xs) Make sure to state the second law as generally as possible, for arbitrary types. -/ lemma m1 {α: Type} (xs: list α): map(λx, x) xs = xs :=sorry lemma m2 {α: Type} {β: Type} {γ: Type} (xs: list α) (f: α → β) (g: β → γ): map(λx, g(f x)) xs = map g (map f xs) :=sorry
cf4f8cd2b6a1b4c910d8f3bcf6809e1536e117b1
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/function/strongly_measurable.lean
aa58802f339e843e03394c99a582929853666e26
[ "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
79,788
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, Sébastien Gouëzel -/ import measure_theory.function.ess_sup import measure_theory.integral.mean_inequalities import topology.continuous_function.compact import topology.metric_space.metrizable import measure_theory.function.simple_func_dense /-! # Strongly measurable and finitely strongly measurable functions A function `f` is said to be strongly measurable if `f` is the sequential limit of simple functions. It is said to be finitely strongly measurable with respect to a measure `μ` if the supports of those simple functions have finite measure. We also provide almost everywhere versions of these notions. Almost everywhere strongly measurable functions form the largest class of functions that can be integrated using the Bochner integral. If the target space has a second countable topology, strongly measurable and measurable are equivalent. If the measure is sigma-finite, strongly measurable and finitely strongly measurable are equivalent. The main property of finitely strongly measurable functions is `fin_strongly_measurable.exists_set_sigma_finite`: there exists a measurable set `t` such that the function is supported on `t` and `μ.restrict t` is sigma-finite. As a consequence, we can prove some results for those functions as if the measure was sigma-finite. ## Main definitions * `strongly_measurable f`: `f : α → β` is the limit of a sequence `fs : ℕ → simple_func α β`. * `fin_strongly_measurable f μ`: `f : α → β` is the limit of a sequence `fs : ℕ → simple_func α β` such that for all `n ∈ ℕ`, the measure of the support of `fs n` is finite. * `ae_strongly_measurable f μ`: `f` is almost everywhere equal to a `strongly_measurable` function. * `ae_fin_strongly_measurable f μ`: `f` is almost everywhere equal to a `fin_strongly_measurable` function. * `ae_fin_strongly_measurable.sigma_finite_set`: a measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `μ.restrict t` is sigma-finite. ## Main statements * `ae_fin_strongly_measurable.exists_set_sigma_finite`: there exists a measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `μ.restrict t` is sigma-finite. We provide a solid API for strongly measurable functions, and for almost everywhere strongly measurable functions, as a basis for the Bochner integral. ## References * Hytönen, Tuomas, Jan Van Neerven, Mark Veraar, and Lutz Weis. Analysis in Banach spaces. Springer, 2016. -/ open measure_theory filter topological_space function set measure_theory.measure open_locale ennreal topological_space measure_theory nnreal big_operators /-- The typeclass `second_countable_topology_either α β` registers the fact that at least one of the two spaces has second countable topology. This is the right assumption to ensure that continuous maps from `α` to `β` are strongly measurable. -/ class second_countable_topology_either (α β : Type*) [topological_space α] [topological_space β] : Prop := (out : second_countable_topology α ∨ second_countable_topology β) @[priority 100] instance second_countable_topology_either_of_left (α β : Type*) [topological_space α] [topological_space β] [second_countable_topology α] : second_countable_topology_either α β := { out := or.inl (by apply_instance) } @[priority 100] instance second_countable_topology_either_of_right (α β : Type*) [topological_space α] [topological_space β] [second_countable_topology β] : second_countable_topology_either α β := { out := or.inr (by apply_instance) } variables {α β γ ι : Type*} [encodable ι] namespace measure_theory local infixr ` →ₛ `:25 := simple_func section definitions variable [topological_space β] /-- A function is `strongly_measurable` if it is the limit of simple functions. -/ def strongly_measurable [measurable_space α] (f : α → β) : Prop := ∃ fs : ℕ → α →ₛ β, ∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x)) localized "notation `strongly_measurable[` m `]` := @measure_theory.strongly_measurable _ _ _ m" in measure_theory /-- A function is `fin_strongly_measurable` with respect to a measure if it is the limit of simple functions with support with finite measure. -/ def fin_strongly_measurable [has_zero β] {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ fs : ℕ → α →ₛ β, (∀ n, μ (support (fs n)) < ∞) ∧ (∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x))) /-- A function is `ae_strongly_measurable` with respect to a measure `μ` if it is almost everywhere equal to the limit of a sequence of simple functions. -/ def ae_strongly_measurable {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ g, strongly_measurable g ∧ f =ᵐ[μ] g /-- A function is `ae_fin_strongly_measurable` with respect to a measure if it is almost everywhere equal to the limit of a sequence of simple functions with support with finite measure. -/ def ae_fin_strongly_measurable [has_zero β] {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ g, fin_strongly_measurable g μ ∧ f =ᵐ[μ] g end definitions open_locale measure_theory /-! ## Strongly measurable functions -/ lemma strongly_measurable.ae_strongly_measurable {α β} {m0 : measurable_space α} [topological_space β] {f : α → β} {μ : measure α} (hf : strongly_measurable f) : ae_strongly_measurable f μ := ⟨f, hf, eventually_eq.refl _ _⟩ @[simp] lemma subsingleton.strongly_measurable {α β} [measurable_space α] [topological_space β] [subsingleton β] (f : α → β) : strongly_measurable f := begin let f_sf : α →ₛ β := ⟨f, λ x, _, set.subsingleton.finite set.subsingleton_of_subsingleton⟩, { exact ⟨λ n, f_sf, λ x, tendsto_const_nhds⟩, }, { have h_univ : f ⁻¹' {x} = set.univ, by { ext1 y, simp, }, rw h_univ, exact measurable_set.univ, }, end lemma simple_func.strongly_measurable {α β} {m : measurable_space α} [topological_space β] (f : α →ₛ β) : strongly_measurable f := ⟨λ _, f, λ x, tendsto_const_nhds⟩ lemma strongly_measurable_of_is_empty [is_empty α] {m : measurable_space α} [topological_space β] (f : α → β) : strongly_measurable f := ⟨λ n, simple_func.of_is_empty, is_empty_elim⟩ lemma strongly_measurable_const {α β} {m : measurable_space α} [topological_space β] {b : β} : strongly_measurable (λ a : α, b) := ⟨λ n, simple_func.const α b, λ a, tendsto_const_nhds⟩ @[to_additive] lemma strongly_measurable_one {α β} {m : measurable_space α} [topological_space β] [has_one β] : strongly_measurable (1 : α → β) := @strongly_measurable_const _ _ _ _ 1 /-- A version of `strongly_measurable_const` that assumes `f x = f y` for all `x, y`. This version works for functions between empty types. -/ lemma strongly_measurable_const' {α β} {m : measurable_space α} [topological_space β] {f : α → β} (hf : ∀ x y, f x = f y) : strongly_measurable f := begin casesI is_empty_or_nonempty α, { exact strongly_measurable_of_is_empty f }, { convert strongly_measurable_const, exact funext (λ x, hf x h.some) } end @[simp] lemma subsingleton.strongly_measurable' {α β} [measurable_space α] [topological_space β] [subsingleton α] (f : α → β) : strongly_measurable f := strongly_measurable_const' (λ x y, by rw subsingleton.elim x y) namespace strongly_measurable variables {f g : α → β} section basic_properties_in_any_topological_space variables [topological_space β] /-- A sequence of simple functions such that `∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x))`. That property is given by `strongly_measurable.tendsto_approx`. -/ protected noncomputable def approx {m : measurable_space α} (hf : strongly_measurable f) : ℕ → α →ₛ β := hf.some protected lemma tendsto_approx {m : measurable_space α} (hf : strongly_measurable f) : ∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x)) := hf.some_spec end basic_properties_in_any_topological_space lemma fin_strongly_measurable_of_set_sigma_finite [topological_space β] [has_zero β] {m : measurable_space α} {μ : measure α} (hf_meas : strongly_measurable f) {t : set α} (ht : measurable_set t) (hft_zero : ∀ x ∈ tᶜ, f x = 0) (htμ : sigma_finite (μ.restrict t)) : fin_strongly_measurable f μ := begin haveI : sigma_finite (μ.restrict t) := htμ, let S := spanning_sets (μ.restrict t), have hS_meas : ∀ n, measurable_set (S n), from measurable_spanning_sets (μ.restrict t), let f_approx := hf_meas.approx, let fs := λ n, simple_func.restrict (f_approx n) (S n ∩ t), have h_fs_t_compl : ∀ n, ∀ x ∉ t, fs n x = 0, { intros n x hxt, rw simple_func.restrict_apply _ ((hS_meas n).inter ht), refine set.indicator_of_not_mem _ _, simp [hxt], }, refine ⟨fs, _, λ x, _⟩, { simp_rw simple_func.support_eq, refine λ n, (measure_bUnion_finset_le _ _).trans_lt _, refine ennreal.sum_lt_top_iff.mpr (λ y hy, _), rw simple_func.restrict_preimage_singleton _ ((hS_meas n).inter ht), swap, { rw finset.mem_filter at hy, exact hy.2, }, refine (measure_mono (set.inter_subset_left _ _)).trans_lt _, have h_lt_top := measure_spanning_sets_lt_top (μ.restrict t) n, rwa measure.restrict_apply' ht at h_lt_top, }, { by_cases hxt : x ∈ t, swap, { rw [funext (λ n, h_fs_t_compl n x hxt), hft_zero x hxt], exact tendsto_const_nhds, }, have h : tendsto (λ n, (f_approx n) x) at_top (𝓝 (f x)), from hf_meas.tendsto_approx x, obtain ⟨n₁, hn₁⟩ : ∃ n, ∀ m, n ≤ m → fs m x = f_approx m x, { obtain ⟨n, hn⟩ : ∃ n, ∀ m, n ≤ m → x ∈ S m ∩ t, { suffices : ∃ n, ∀ m, n ≤ m → x ∈ S m, { obtain ⟨n, hn⟩ := this, exact ⟨n, λ m hnm, set.mem_inter (hn m hnm) hxt⟩, }, suffices : ∃ n, x ∈ S n, { rcases this with ⟨n, hn⟩, exact ⟨n, λ m hnm, monotone_spanning_sets (μ.restrict t) hnm hn⟩, }, rw [← set.mem_Union, Union_spanning_sets (μ.restrict t)], trivial, }, refine ⟨n, λ m hnm, _⟩, simp_rw [fs, simple_func.restrict_apply _ ((hS_meas m).inter ht), set.indicator_of_mem (hn m hnm)], }, rw tendsto_at_top' at h ⊢, intros s hs, obtain ⟨n₂, hn₂⟩ := h s hs, refine ⟨max n₁ n₂, λ m hm, _⟩, rw hn₁ m ((le_max_left _ _).trans hm.le), exact hn₂ m ((le_max_right _ _).trans hm.le), }, end /-- If the measure is sigma-finite, all strongly measurable functions are `fin_strongly_measurable`. -/ protected lemma fin_strongly_measurable [topological_space β] [has_zero β] {m0 : measurable_space α} (hf : strongly_measurable f) (μ : measure α) [sigma_finite μ] : fin_strongly_measurable f μ := hf.fin_strongly_measurable_of_set_sigma_finite measurable_set.univ (by simp) (by rwa measure.restrict_univ) /-- A strongly measurable function is measurable. -/ protected lemma measurable {m : measurable_space α} [topological_space β] [metrizable_space β] [measurable_space β] [borel_space β] (hf : strongly_measurable f) : measurable f := measurable_of_tendsto_metrizable (λ n, (hf.approx n).measurable) (tendsto_pi_nhds.mpr hf.tendsto_approx) /-- A strongly measurable function is almost everywhere measurable. -/ protected lemma ae_measurable {m : measurable_space α} [topological_space β] [metrizable_space β] [measurable_space β] [borel_space β] {μ : measure α} (hf : strongly_measurable f) : ae_measurable f μ := hf.measurable.ae_measurable lemma _root_.continuous.comp_strongly_measurable {m : measurable_space α} [topological_space β] [topological_space γ] {g : β → γ} {f : α → β} (hg : continuous g) (hf : strongly_measurable f) : strongly_measurable (λ x, g (f x)) := ⟨λ n, simple_func.map g (hf.approx n), λ x, (hg.tendsto _).comp (hf.tendsto_approx x)⟩ @[to_additive] lemma measurable_set_mul_support {m : measurable_space α} [has_one β] [topological_space β] [metrizable_space β] (hf : strongly_measurable f) : measurable_set (mul_support f) := by { borelize β, exact measurable_set_mul_support hf.measurable } protected lemma mono {m m' : measurable_space α} [topological_space β] (hf : strongly_measurable[m'] f) (h_mono : m' ≤ m) : strongly_measurable[m] f := begin let f_approx : ℕ → @simple_func α m β := λ n, { to_fun := hf.approx n, measurable_set_fiber' := λ x, h_mono _ (simple_func.measurable_set_fiber' _ x), finite_range' := simple_func.finite_range (hf.approx n) }, exact ⟨f_approx, hf.tendsto_approx⟩, end protected lemma prod_mk {m : measurable_space α} [topological_space β] [topological_space γ] {f : α → β} {g : α → γ} (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, (f x, g x)) := begin refine ⟨λ n, simple_func.pair (hf.approx n) (hg.approx n), λ x, _⟩, rw nhds_prod_eq, exact tendsto.prod_mk (hf.tendsto_approx x) (hg.tendsto_approx x), end lemma comp_measurable [topological_space β] {m : measurable_space α} {m' : measurable_space γ} {f : α → β} {g : γ → α} (hf : strongly_measurable f) (hg : measurable g) : strongly_measurable (f ∘ g) := ⟨λ n, simple_func.comp (hf.approx n) g hg, λ x, hf.tendsto_approx (g x)⟩ lemma of_uncurry_left [topological_space β] {mα : measurable_space α} {mγ : measurable_space γ} {f : α → γ → β} (hf : strongly_measurable (uncurry f)) {x : α} : strongly_measurable (f x) := hf.comp_measurable measurable_prod_mk_left lemma of_uncurry_right [topological_space β] {mα : measurable_space α} {mγ : measurable_space γ} {f : α → γ → β} (hf : strongly_measurable (uncurry f)) {y : γ} : strongly_measurable (λ x, f x y) := hf.comp_measurable measurable_prod_mk_right section arithmetic variables {mα : measurable_space α} [topological_space β] include mα @[to_additive] protected lemma mul [has_mul β] [has_continuous_mul β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f * g) := ⟨λ n, hf.approx n * hg.approx n, λ x, (hf.tendsto_approx x).mul (hg.tendsto_approx x)⟩ @[to_additive] lemma mul_const [has_mul β] [has_continuous_mul β] (hf : strongly_measurable f) (c : β) : strongly_measurable (λ x, f x * c) := hf.mul strongly_measurable_const @[to_additive] lemma const_mul [has_mul β] [has_continuous_mul β] (hf : strongly_measurable f) (c : β) : strongly_measurable (λ x, c * f x) := strongly_measurable_const.mul hf @[to_additive] protected lemma inv [group β] [topological_group β] (hf : strongly_measurable f) : strongly_measurable f⁻¹ := ⟨λ n, (hf.approx n)⁻¹, λ x, (hf.tendsto_approx x).inv⟩ @[to_additive] protected lemma div [has_div β] [has_continuous_div β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f / g) := ⟨λ n, hf.approx n / hg.approx n, λ x, (hf.tendsto_approx x).div' (hg.tendsto_approx x)⟩ @[to_additive] protected lemma smul {𝕜} [topological_space 𝕜] [has_scalar 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} {g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, f x • g x) := continuous_smul.comp_strongly_measurable (hf.prod_mk hg) protected lemma const_smul {𝕜} [has_scalar 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : strongly_measurable f) (c : 𝕜) : strongly_measurable (c • f) := ⟨λ n, c • (hf.approx n), λ x, (hf.tendsto_approx x).const_smul c⟩ protected lemma const_smul' {𝕜} [has_scalar 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : strongly_measurable f) (c : 𝕜) : strongly_measurable (λ x, c • (f x)) := hf.const_smul c @[to_additive] protected lemma smul_const {𝕜} [topological_space 𝕜] [has_scalar 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} (hf : strongly_measurable f) (c : β) : strongly_measurable (λ x, f x • c) := continuous_smul.comp_strongly_measurable (hf.prod_mk strongly_measurable_const) end arithmetic section mul_action variables [topological_space β] {G : Type*} [group G] [mul_action G β] [has_continuous_const_smul G β] lemma _root_.strongly_measurable_const_smul_iff {m : measurable_space α} (c : G) : strongly_measurable (λ x, c • f x) ↔ strongly_measurable f := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ variables {G₀ : Type*} [group_with_zero G₀] [mul_action G₀ β] [has_continuous_const_smul G₀ β] lemma _root_.strongly_measurable_const_smul_iff₀ {m : measurable_space α} {c : G₀} (hc : c ≠ 0) : strongly_measurable (λ x, c • f x) ↔ strongly_measurable f := begin refine ⟨λ h, _, λ h, h.const_smul c⟩, convert h.const_smul' c⁻¹, simp [smul_smul, inv_mul_cancel hc] end end mul_action section order variables [measurable_space α] [topological_space β] open filter open_locale filter protected lemma sup [has_sup β] [has_continuous_sup β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f ⊔ g) := ⟨λ n, hf.approx n ⊔ hg.approx n, λ x, (hf.tendsto_approx x).sup_right_nhds (hg.tendsto_approx x)⟩ protected lemma inf [has_inf β] [has_continuous_inf β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f ⊓ g) := ⟨λ n, hf.approx n ⊓ hg.approx n, λ x, (hf.tendsto_approx x).inf_right_nhds (hg.tendsto_approx x)⟩ end order /-! ### Big operators: `∏` and `∑` -/ section monoid variables {M : Type*} [monoid M] [topological_space M] [has_continuous_mul M] {m : measurable_space α} include m @[to_additive] lemma _root_.list.strongly_measurable_prod' (l : list (α → M)) (hl : ∀ f ∈ l, strongly_measurable f) : strongly_measurable l.prod := begin induction l with f l ihl, { exact strongly_measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[to_additive] lemma _root_.list.strongly_measurable_prod (l : list (α → M)) (hl : ∀ f ∈ l, strongly_measurable f) : strongly_measurable (λ x, (l.map (λ f : α → M, f x)).prod) := by simpa only [← pi.list_prod_apply] using l.strongly_measurable_prod' hl end monoid section comm_monoid variables {M : Type*} [comm_monoid M] [topological_space M] [has_continuous_mul M] {m : measurable_space α} include m @[to_additive] lemma _root_.multiset.strongly_measurable_prod' (l : multiset (α → M)) (hl : ∀ f ∈ l, strongly_measurable f) : strongly_measurable l.prod := by { rcases l with ⟨l⟩, simpa using l.strongly_measurable_prod' (by simpa using hl) } @[to_additive] lemma _root_.multiset.strongly_measurable_prod (s : multiset (α → M)) (hs : ∀ f ∈ s, strongly_measurable f) : strongly_measurable (λ x, (s.map (λ f : α → M, f x)).prod) := by simpa only [← pi.multiset_prod_apply] using s.strongly_measurable_prod' hs @[to_additive] lemma _root_.finset.strongly_measurable_prod' {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, strongly_measurable (f i)) : strongly_measurable (∏ i in s, f i) := finset.prod_induction _ _ (λ a b ha hb, ha.mul hb) (@strongly_measurable_one α M _ _ _) hf @[to_additive] lemma _root_.finset.strongly_measurable_prod {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, strongly_measurable (f i)) : strongly_measurable (λ a, ∏ i in s, f i a) := by simpa only [← finset.prod_apply] using s.strongly_measurable_prod' hf end comm_monoid /-- The range of a strongly measurable function is separable. -/ lemma is_separable_range {m : measurable_space α} [topological_space β] (hf : strongly_measurable f) : topological_space.is_separable (range f) := begin have : is_separable (closure (⋃ n, range (hf.approx n))) := (is_separable_Union (λ n, (simple_func.finite_range (hf.approx n)).is_separable)).closure, apply this.mono, rintros - ⟨x, rfl⟩, apply mem_closure_of_tendsto (hf.tendsto_approx x), apply eventually_of_forall (λ n, _), apply mem_Union_of_mem n, exact mem_range_self _ end lemma separable_space_range_union_singleton {m : measurable_space α} [topological_space β] [metrizable_space β] (hf : strongly_measurable f) {b : β} : separable_space (range f ∪ {b} : set β) := begin letI := metrizable_space_metric β, exact (is_separable.union hf.is_separable_range (finite_singleton _).is_separable).separable_space end section second_countable_strongly_measurable variables {mα : measurable_space α} [measurable_space β] include mα /-- In a space with second countable topology, measurable implies strongly measurable. -/ lemma _root_.measurable.strongly_measurable [topological_space β] [metrizable_space β] [second_countable_topology β] [opens_measurable_space β] (hf : measurable f) : strongly_measurable f := begin letI := metrizable_space_metric β, rcases is_empty_or_nonempty β; resetI, { exact subsingleton.strongly_measurable f, }, { inhabit β, exact ⟨simple_func.approx_on f hf set.univ default (set.mem_univ _), λ x, simple_func.tendsto_approx_on hf (set.mem_univ _) (by simp)⟩, }, end /-- In a space with second countable topology, strongly measurable and measurable are equivalent. -/ lemma _root_.strongly_measurable_iff_measurable [topological_space β] [metrizable_space β] [borel_space β] [second_countable_topology β] : strongly_measurable f ↔ measurable f := ⟨λ h, h.measurable, λ h, measurable.strongly_measurable h⟩ lemma _root_.strongly_measurable_id [topological_space α] [metrizable_space α] [opens_measurable_space α] [second_countable_topology α] : strongly_measurable (id : α → α) := measurable_id.strongly_measurable end second_countable_strongly_measurable /-- A function is strongly measurable if and only if it is measurable and has separable range. -/ theorem _root_.strongly_measurable_iff_measurable_separable {m : measurable_space α} [topological_space β] [metrizable_space β] [measurable_space β] [borel_space β] : strongly_measurable f ↔ (measurable f ∧ is_separable (range f)) := begin refine ⟨λ H, ⟨H.measurable, H.is_separable_range⟩, _⟩, rintros ⟨H, H'⟩, letI := metrizable_space_metric β, let g := cod_restrict f (closure (range f)) (λ x, subset_closure (mem_range_self x)), have fg : f = (coe : closure (range f) → β) ∘ g, by { ext x, refl }, have T : measurable_embedding (coe : closure (range f) → β), { apply closed_embedding.measurable_embedding, exact closed_embedding_subtype_coe is_closed_closure }, have g_meas : measurable g, { rw fg at H, exact T.measurable_comp_iff.1 H }, haveI : second_countable_topology (closure (range f)), { suffices : separable_space (closure (range f)), by exactI uniform_space.second_countable_of_separable _, exact (is_separable.closure H').separable_space }, have g_smeas : strongly_measurable g := measurable.strongly_measurable g_meas, rw fg, exact continuous_subtype_coe.comp_strongly_measurable g_smeas, end /-- A continuous function is strongly measurable when either the source space or the target space is second-countable. -/ lemma _root_.continuous.strongly_measurable [measurable_space α] [topological_space α] [opens_measurable_space α] {β : Type*} [topological_space β] [metrizable_space β] [h : second_countable_topology_either α β] {f : α → β} (hf : continuous f) : strongly_measurable f := begin borelize β, casesI h.out, { rw strongly_measurable_iff_measurable_separable, refine ⟨hf.measurable, _⟩, rw ← image_univ, exact (is_separable_of_separable_space univ).image hf }, { exact hf.measurable.strongly_measurable } end /-- If `g` is a topological embedding, then `f` is strongly measurable iff `g ∘ f` is. -/ lemma _root_.embedding.comp_strongly_measurable_iff {m : measurable_space α} [topological_space β] [metrizable_space β] [topological_space γ] [metrizable_space γ] {g : β → γ} {f : α → β} (hg : embedding g) : strongly_measurable (λ x, g (f x)) ↔ strongly_measurable f := begin letI := metrizable_space_metric γ, borelize [β, γ], refine ⟨λ H, strongly_measurable_iff_measurable_separable.2 ⟨_, _⟩, λ H, hg.continuous.comp_strongly_measurable H⟩, { let G : β → range g := cod_restrict g (range g) mem_range_self, have hG : closed_embedding G := { closed_range := begin convert is_closed_univ, apply eq_univ_of_forall, rintros ⟨-, ⟨x, rfl⟩⟩, exact mem_range_self x end, .. hg.cod_restrict _ _ }, have : measurable (G ∘ f) := measurable.subtype_mk H.measurable, exact hG.measurable_embedding.measurable_comp_iff.1 this }, { have : is_separable (g ⁻¹' (range (g ∘ f))) := hg.is_separable_preimage H.is_separable_range, convert this, ext x, simp [hg.inj.eq_iff] } end /-- A sequential limit of strongly measurable functions is strongly measurable. -/ lemma _root_.strongly_measurable_of_tendsto {ι : Type*} {m : measurable_space α} [topological_space β] [metrizable_space β] (u : filter ι) [ne_bot u] [is_countably_generated u] {f : ι → α → β} {g : α → β} (hf : ∀ i, strongly_measurable (f i)) (lim : tendsto f u (𝓝 g)) : strongly_measurable g := begin letI := metrizable_space_metric β, borelize β, refine strongly_measurable_iff_measurable_separable.2 ⟨_, _⟩, { apply measurable_of_tendsto_metrizable' u (λ i, _) lim, exact (hf i).measurable }, { rcases u.exists_seq_tendsto with ⟨v, hv⟩, have : is_separable (closure (⋃ i, range (f (v i)))) := (is_separable_Union (λ i, (hf (v i)).is_separable_range)).closure, apply this.mono, rintros - ⟨x, rfl⟩, rw [tendsto_pi_nhds] at lim, apply mem_closure_of_tendsto ((lim x).comp hv), apply eventually_of_forall (λ n, _), apply mem_Union_of_mem n, exact mem_range_self _ } end protected lemma piecewise {m : measurable_space α} [topological_space β] {s : set α} {_ : decidable_pred (∈ s)} (hs : measurable_set s) (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (set.piecewise s f g) := begin refine ⟨λ n, simple_func.piecewise s hs (hf.approx n) (hg.approx n), λ x, _⟩, by_cases hx : x ∈ s, { simpa [hx] using hf.tendsto_approx x }, { simpa [hx] using hg.tendsto_approx x }, end /-- this is slightly different from `strongly_measurable.piecewise`. It can be used to show `strongly_measurable (ite (x=0) 0 1)` by `exact strongly_measurable.ite (measurable_set_singleton 0) strongly_measurable_const strongly_measurable_const`, but replacing `strongly_measurable.ite` by `strongly_measurable.piecewise` in that example proof does not work. -/ protected lemma ite {m : measurable_space α} [topological_space β] {p : α → Prop} {_ : decidable_pred p} (hp : measurable_set {a : α | p a}) (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, ite (p x) (f x) (g x)) := strongly_measurable.piecewise hp hf hg lemma _root_.strongly_measurable_of_strongly_measurable_union_cover {m : measurable_space α} [topological_space β] {f : α → β} (s t : set α) (hs : measurable_set s) (ht : measurable_set t) (h : univ ⊆ s ∪ t) (hc : strongly_measurable (λ a : s, f a)) (hd : strongly_measurable (λ a : t, f a)) : strongly_measurable f := begin classical, let f : ℕ → α →ₛ β := λ n, { to_fun := λ x, if hx : x ∈ s then hc.approx n ⟨x, hx⟩ else hd.approx n ⟨x, by simpa [hx] using h (mem_univ x)⟩, measurable_set_fiber' := begin assume x, convert (hs.subtype_image ((hc.approx n).measurable_set_fiber x)).union ((ht.subtype_image ((hd.approx n).measurable_set_fiber x)).diff hs), ext1 y, simp only [mem_union_eq, mem_preimage, mem_singleton_iff, mem_image, set_coe.exists, subtype.coe_mk, exists_and_distrib_right, exists_eq_right, mem_diff], by_cases hy : y ∈ s, { rw dif_pos hy, simp only [hy, exists_true_left, not_true, and_false, or_false]}, { rw dif_neg hy, have A : y ∈ t, by simpa [hy] using h (mem_univ y), simp only [A, hy, false_or, exists_false_left, not_false_iff, and_true, exists_true_left] } end, finite_range' := begin apply ((hc.approx n).finite_range.union (hd.approx n).finite_range).subset, rintros - ⟨y, rfl⟩, dsimp, by_cases hy : y ∈ s, { left, rw dif_pos hy, exact mem_range_self _ }, { right, rw dif_neg hy, exact mem_range_self _ } end }, refine ⟨f, λ y, _⟩, by_cases hy : y ∈ s, { convert hc.tendsto_approx ⟨y, hy⟩ using 1, ext1 n, simp only [dif_pos hy, simple_func.apply_mk] }, { have A : y ∈ t, by simpa [hy] using h (mem_univ y), convert hd.tendsto_approx ⟨y, A⟩ using 1, ext1 n, simp only [dif_neg hy, simple_func.apply_mk] } end lemma _root_.strongly_measurable_of_restrict_of_restrict_compl {m : measurable_space α} [topological_space β] {f : α → β} {s : set α} (hs : measurable_set s) (h₁ : strongly_measurable (s.restrict f)) (h₂ : strongly_measurable (sᶜ.restrict f)) : strongly_measurable f := strongly_measurable_of_strongly_measurable_union_cover s sᶜ hs hs.compl (union_compl_self s).ge h₁ h₂ protected lemma indicator {m : measurable_space α} [topological_space β] [has_zero β] (hf : strongly_measurable f) {s : set α} (hs : measurable_set s) : strongly_measurable (s.indicator f) := hf.piecewise hs strongly_measurable_const protected lemma dist {m : measurable_space α} {β : Type*} [pseudo_metric_space β] {f g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, dist (f x) (g x)) := continuous_dist.comp_strongly_measurable (hf.prod_mk hg) protected lemma norm {m : measurable_space α} {β : Type*} [normed_group β] {f : α → β} (hf : strongly_measurable f) : strongly_measurable (λ x, ∥f x∥) := continuous_norm.comp_strongly_measurable hf protected lemma nnnorm {m : measurable_space α} {β : Type*} [normed_group β] {f : α → β} (hf : strongly_measurable f) : strongly_measurable (λ x, ∥f x∥₊) := continuous_nnnorm.comp_strongly_measurable hf protected lemma ennnorm {m : measurable_space α} {β : Type*} [normed_group β] {f : α → β} (hf : strongly_measurable f) : measurable (λ a, (∥f a∥₊ : ℝ≥0∞)) := (ennreal.continuous_coe.comp_strongly_measurable hf.nnnorm).measurable protected lemma real_to_nnreal {m : measurable_space α} {f : α → ℝ} (hf : strongly_measurable f) : strongly_measurable (λ x, (f x).to_nnreal) := continuous_real_to_nnreal.comp_strongly_measurable hf lemma _root_.measurable_embedding.strongly_measurable_extend {f : α → β} {g : α → γ} {g' : γ → β} {mα : measurable_space α} {mγ : measurable_space γ} [topological_space β] (hg : measurable_embedding g) (hf : strongly_measurable f) (hg' : strongly_measurable g') : strongly_measurable (function.extend g f g') := begin refine ⟨λ n, simple_func.extend (hf.approx n) g hg (hg'.approx n), _⟩, assume x, by_cases hx : ∃ y, g y = x, { rcases hx with ⟨y, rfl⟩, simpa only [simple_func.extend_apply, hg.injective, extend_apply] using hf.tendsto_approx y }, { simpa only [hx, simple_func.extend_apply', not_false_iff, extend_apply'] using hg'.tendsto_approx x } end lemma _root_.measurable_embedding.exists_strongly_measurable_extend {f : α → β} {g : α → γ} {mα : measurable_space α} {mγ : measurable_space γ} [topological_space β] (hg : measurable_embedding g) (hf : strongly_measurable f) (hne : γ → nonempty β) : ∃ f' : γ → β, strongly_measurable f' ∧ f' ∘ g = f := ⟨function.extend g f (λ x, classical.choice (hne x)), hg.strongly_measurable_extend hf (strongly_measurable_const' $ λ _ _, rfl), funext $ λ x, extend_apply hg.injective _ _ _⟩ protected lemma inner {𝕜 : Type*} {E : Type*} [is_R_or_C 𝕜] [inner_product_space 𝕜 E] {m : measurable_space α} {f g : α → E} (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ t, @inner 𝕜 _ _(f t) (g t)) := continuous.comp_strongly_measurable continuous_inner (hf.prod_mk hg) lemma measurable_set_eq_fun {m : measurable_space α} {E} [topological_space E] [metrizable_space E] {f g : α → E} (hf : strongly_measurable f) (hg : strongly_measurable g) : measurable_set {x | f x = g x} := begin letI := metrizable_space_metric E, have : {x | f x = g x} = {x | dist (f x) (g x) = 0}, by { ext x, simp }, rw this, exact (hf.dist hg).measurable (measurable_set_singleton (0 : ℝ)), end lemma measurable_set_lt {m : measurable_space α} [topological_space β] [linear_order β] [order_closed_topology β] [metrizable_space β] {f g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : measurable_set {a | f a < g a} := begin letI := metrizable_space_metric β, let β' : Type* := (range f ∪ range g : set β), haveI : second_countable_topology β', { suffices : separable_space (range f ∪ range g : set β), by exactI uniform_space.second_countable_of_separable _, apply (hf.is_separable_range.union hg.is_separable_range).separable_space }, let f' : α → β' := cod_restrict f _ (by simp), let g' : α → β' := cod_restrict g _ (by simp), change measurable_set {a | f' a < g' a}, borelize β, exact measurable_set_lt hf.measurable.subtype_mk hg.measurable.subtype_mk, end lemma measurable_set_le {m : measurable_space α} [topological_space β] [linear_order β] [order_closed_topology β] [metrizable_space β] {f g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : measurable_set {a | f a ≤ g a} := begin letI := metrizable_space_metric β, let β' : Type* := (range f ∪ range g : set β), haveI : second_countable_topology β', { suffices : separable_space (range f ∪ range g : set β), by exactI uniform_space.second_countable_of_separable _, apply (hf.is_separable_range.union hg.is_separable_range).separable_space }, let f' : α → β' := cod_restrict f _ (by simp), let g' : α → β' := cod_restrict g _ (by simp), change measurable_set {a | f' a ≤ g' a}, borelize β, exact measurable_set_le hf.measurable.subtype_mk hg.measurable.subtype_mk, end end strongly_measurable /-! ## Finitely strongly measurable functions -/ lemma fin_strongly_measurable_zero {α β} {m : measurable_space α} {μ : measure α} [has_zero β] [topological_space β] : fin_strongly_measurable (0 : α → β) μ := ⟨0, by simp only [pi.zero_apply, simple_func.coe_zero, support_zero', measure_empty, with_top.zero_lt_top, forall_const], λ n, tendsto_const_nhds⟩ namespace fin_strongly_measurable variables {m0 : measurable_space α} {μ : measure α} {f g : α → β} lemma ae_fin_strongly_measurable [has_zero β] [topological_space β] (hf : fin_strongly_measurable f μ) : ae_fin_strongly_measurable f μ := ⟨f, hf, ae_eq_refl f⟩ section sequence variables [has_zero β] [topological_space β] (hf : fin_strongly_measurable f μ) /-- A sequence of simple functions such that `∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x))` and `∀ n, μ (support (hf.approx n)) < ∞`. These properties are given by `fin_strongly_measurable.tendsto_approx` and `fin_strongly_measurable.fin_support_approx`. -/ protected noncomputable def approx : ℕ → α →ₛ β := hf.some protected lemma fin_support_approx : ∀ n, μ (support (hf.approx n)) < ∞ := hf.some_spec.1 protected lemma tendsto_approx : ∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x)) := hf.some_spec.2 end sequence protected lemma strongly_measurable [has_zero β] [topological_space β] (hf : fin_strongly_measurable f μ) : strongly_measurable f := ⟨hf.approx, hf.tendsto_approx⟩ lemma exists_set_sigma_finite [has_zero β] [topological_space β] [t2_space β] (hf : fin_strongly_measurable f μ) : ∃ t, measurable_set t ∧ (∀ x ∈ tᶜ, f x = 0) ∧ sigma_finite (μ.restrict t) := begin rcases hf with ⟨fs, hT_lt_top, h_approx⟩, let T := λ n, support (fs n), have hT_meas : ∀ n, measurable_set (T n), from λ n, simple_func.measurable_set_support (fs n), let t := ⋃ n, T n, refine ⟨t, measurable_set.Union hT_meas, _, _⟩, { have h_fs_zero : ∀ n, ∀ x ∈ tᶜ, fs n x = 0, { intros n x hxt, rw [set.mem_compl_iff, set.mem_Union, not_exists] at hxt, simpa using (hxt n), }, refine λ x hxt, tendsto_nhds_unique (h_approx x) _, rw funext (λ n, h_fs_zero n x hxt), exact tendsto_const_nhds, }, { refine ⟨⟨⟨λ n, tᶜ ∪ T n, λ n, trivial, λ n, _, _⟩⟩⟩, { rw [measure.restrict_apply' (measurable_set.Union hT_meas), set.union_inter_distrib_right, set.compl_inter_self t, set.empty_union], exact (measure_mono (set.inter_subset_left _ _)).trans_lt (hT_lt_top n), }, { rw ← set.union_Union tᶜ T, exact set.compl_union_self _ } } end /-- A finitely strongly measurable function is measurable. -/ protected lemma measurable [has_zero β] [topological_space β] [metrizable_space β] [measurable_space β] [borel_space β] (hf : fin_strongly_measurable f μ) : measurable f := hf.strongly_measurable.measurable section arithmetic variables [topological_space β] protected lemma mul [monoid_with_zero β] [has_continuous_mul β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f * g) μ := begin refine ⟨λ n, hf.approx n * hg.approx n, _, λ x, (hf.tendsto_approx x).mul (hg.tendsto_approx x)⟩, intro n, exact (measure_mono (support_mul_subset_left _ _)).trans_lt (hf.fin_support_approx n), end protected lemma add [add_monoid β] [has_continuous_add β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f + g) μ := ⟨λ n, hf.approx n + hg.approx n, λ n, (measure_mono (function.support_add _ _)).trans_lt ((measure_union_le _ _).trans_lt (ennreal.add_lt_top.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩)), λ x, (hf.tendsto_approx x).add (hg.tendsto_approx x)⟩ protected lemma neg [add_group β] [topological_add_group β] (hf : fin_strongly_measurable f μ) : fin_strongly_measurable (-f) μ := begin refine ⟨λ n, -hf.approx n, λ n, _, λ x, (hf.tendsto_approx x).neg⟩, suffices : μ (function.support (λ x, - (hf.approx n) x)) < ∞, by convert this, rw function.support_neg (hf.approx n), exact hf.fin_support_approx n, end protected lemma sub [add_group β] [has_continuous_sub β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f - g) μ := ⟨λ n, hf.approx n - hg.approx n, λ n, (measure_mono (function.support_sub _ _)).trans_lt ((measure_union_le _ _).trans_lt (ennreal.add_lt_top.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩)), λ x, (hf.tendsto_approx x).sub (hg.tendsto_approx x)⟩ protected lemma const_smul {𝕜} [topological_space 𝕜] [add_monoid β] [monoid 𝕜] [distrib_mul_action 𝕜 β] [has_continuous_smul 𝕜 β] (hf : fin_strongly_measurable f μ) (c : 𝕜) : fin_strongly_measurable (c • f) μ := begin refine ⟨λ n, c • (hf.approx n), λ n, _, λ x, (hf.tendsto_approx x).const_smul c⟩, rw simple_func.coe_smul, refine (measure_mono (support_smul_subset_right c _)).trans_lt (hf.fin_support_approx n), end end arithmetic section order variables [topological_space β] [has_zero β] protected lemma sup [semilattice_sup β] [has_continuous_sup β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f ⊔ g) μ := begin refine ⟨λ n, hf.approx n ⊔ hg.approx n, λ n, _, λ x, (hf.tendsto_approx x).sup_right_nhds (hg.tendsto_approx x)⟩, refine (measure_mono (support_sup _ _)).trans_lt _, exact measure_union_lt_top_iff.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩, end protected lemma inf [semilattice_inf β] [has_continuous_inf β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f ⊓ g) μ := begin refine ⟨λ n, hf.approx n ⊓ hg.approx n, λ n, _, λ x, (hf.tendsto_approx x).inf_right_nhds (hg.tendsto_approx x)⟩, refine (measure_mono (support_inf _ _)).trans_lt _, exact measure_union_lt_top_iff.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩, end end order end fin_strongly_measurable lemma fin_strongly_measurable_iff_strongly_measurable_and_exists_set_sigma_finite {α β} {f : α → β} [topological_space β] [t2_space β] [has_zero β] {m : measurable_space α} {μ : measure α} : fin_strongly_measurable f μ ↔ (strongly_measurable f ∧ (∃ t, measurable_set t ∧ (∀ x ∈ tᶜ, f x = 0) ∧ sigma_finite (μ.restrict t))) := ⟨λ hf, ⟨hf.strongly_measurable, hf.exists_set_sigma_finite⟩, λ hf, hf.1.fin_strongly_measurable_of_set_sigma_finite hf.2.some_spec.1 hf.2.some_spec.2.1 hf.2.some_spec.2.2⟩ lemma ae_fin_strongly_measurable_zero {α β} {m : measurable_space α} (μ : measure α) [has_zero β] [topological_space β] : ae_fin_strongly_measurable (0 : α → β) μ := ⟨0, fin_strongly_measurable_zero, eventually_eq.rfl⟩ /-! ## Almost everywhere strongly measurable functions -/ lemma ae_strongly_measurable_const {α β} {m : measurable_space α} {μ : measure α} [topological_space β] {b : β} : ae_strongly_measurable (λ a : α, b) μ := strongly_measurable_const.ae_strongly_measurable @[to_additive] lemma ae_strongly_measurable_one {α β} {m : measurable_space α} {μ : measure α} [topological_space β] [has_one β] : ae_strongly_measurable (1 : α → β) μ := strongly_measurable_one.ae_strongly_measurable @[simp] lemma subsingleton.ae_strongly_measurable {m : measurable_space α} [topological_space β] [subsingleton β] {μ : measure α} (f : α → β) : ae_strongly_measurable f μ := (subsingleton.strongly_measurable f).ae_strongly_measurable @[simp] lemma subsingleton.ae_strongly_measurable' {m : measurable_space α} [topological_space β] [subsingleton α] {μ : measure α} (f : α → β) : ae_strongly_measurable f μ := (subsingleton.strongly_measurable' f).ae_strongly_measurable @[simp] lemma ae_measurable_zero_measure [measurable_space α] [topological_space β] (f : α → β) : ae_strongly_measurable f (0 : measure α) := begin nontriviality α, inhabit α, exact ⟨λ x, f default, strongly_measurable_const, rfl⟩ end lemma simple_func.ae_strongly_measurable {m : measurable_space α} {μ : measure α} [topological_space β] (f : α →ₛ β) : ae_strongly_measurable f μ := f.strongly_measurable.ae_strongly_measurable namespace ae_strongly_measurable variables {m : measurable_space α} {μ : measure α} [topological_space β] [topological_space γ] {f g : α → β} section mk /-- A `strongly_measurable` function such that `f =ᵐ[μ] hf.mk f`. See lemmas `strongly_measurable_mk` and `ae_eq_mk`. -/ protected noncomputable def mk (f : α → β) (hf : ae_strongly_measurable f μ) : α → β := hf.some lemma strongly_measurable_mk (hf : ae_strongly_measurable f μ) : strongly_measurable (hf.mk f) := hf.some_spec.1 lemma measurable_mk [metrizable_space β] [measurable_space β] [borel_space β] (hf : ae_strongly_measurable f μ) : measurable (hf.mk f) := hf.strongly_measurable_mk.measurable lemma ae_eq_mk (hf : ae_strongly_measurable f μ) : f =ᵐ[μ] hf.mk f := hf.some_spec.2 protected lemma ae_measurable {β} [measurable_space β] [topological_space β] [metrizable_space β] [borel_space β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_measurable f μ := ⟨hf.mk f, hf.strongly_measurable_mk.measurable, hf.ae_eq_mk⟩ end mk lemma congr (hf : ae_strongly_measurable f μ) (h : f =ᵐ[μ] g) : ae_strongly_measurable g μ := ⟨hf.mk f, hf.strongly_measurable_mk, h.symm.trans hf.ae_eq_mk⟩ lemma _root_.ae_strongly_measurable_congr (h : f =ᵐ[μ] g) : ae_strongly_measurable f μ ↔ ae_strongly_measurable g μ := ⟨λ hf, hf.congr h, λ hg, hg.congr h.symm⟩ lemma mono_measure {ν : measure α} (hf : ae_strongly_measurable f μ) (h : ν ≤ μ) : ae_strongly_measurable f ν := ⟨hf.mk f, hf.strongly_measurable_mk, eventually.filter_mono (ae_mono h) hf.ae_eq_mk⟩ protected lemma mono' {ν : measure α} (h : ae_strongly_measurable f μ) (h' : ν ≪ μ) : ae_strongly_measurable f ν := ⟨h.mk f, h.strongly_measurable_mk, h' h.ae_eq_mk⟩ lemma mono_set {s t} (h : s ⊆ t) (ht : ae_strongly_measurable f (μ.restrict t)) : ae_strongly_measurable f (μ.restrict s) := ht.mono_measure (restrict_mono h le_rfl) protected lemma restrict (hfm : ae_strongly_measurable f μ) {s} : ae_strongly_measurable f (μ.restrict s) := hfm.mono_measure measure.restrict_le_self lemma ae_mem_imp_eq_mk {s} (h : ae_strongly_measurable f (μ.restrict s)) : ∀ᵐ x ∂μ, x ∈ s → f x = h.mk f x := ae_imp_of_ae_restrict h.ae_eq_mk /-- The composition of a continuous function and an ae strongly measurable function is ae strongly measurable. -/ lemma _root_.continuous.comp_ae_strongly_measurable {g : β → γ} {f : α → β} (hg : continuous g) (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, g (f x)) μ := ⟨_, hg.comp_strongly_measurable hf.strongly_measurable_mk, eventually_eq.fun_comp hf.ae_eq_mk g⟩ /-- A continuous function from `α` to `β` is ae strongly measurable when one of the two spaces is second countable. -/ lemma _root_.continuous.ae_strongly_measurable [topological_space α] [opens_measurable_space α] [metrizable_space β] [second_countable_topology_either α β] (hf : continuous f) : ae_strongly_measurable f μ := hf.strongly_measurable.ae_strongly_measurable protected lemma prod_mk {f : α → β} {g : α → γ} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, (f x, g x)) μ := ⟨λ x, (hf.mk f x, hg.mk g x), hf.strongly_measurable_mk.prod_mk hg.strongly_measurable_mk, hf.ae_eq_mk.prod_mk hg.ae_eq_mk⟩ /-- In a space with second countable topology, measurable implies ae strongly measurable. -/ lemma _root_.measurable.ae_strongly_measurable {m : measurable_space α} {μ : measure α} [measurable_space β] [metrizable_space β] [second_countable_topology β] [opens_measurable_space β] (hf : measurable f) : ae_strongly_measurable f μ := hf.strongly_measurable.ae_strongly_measurable section arithmetic @[to_additive] protected lemma mul [has_mul β] [has_continuous_mul β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f * g) μ := ⟨hf.mk f * hg.mk g, hf.strongly_measurable_mk.mul hg.strongly_measurable_mk, hf.ae_eq_mk.mul hg.ae_eq_mk⟩ @[to_additive] protected lemma mul_const [has_mul β] [has_continuous_mul β] (hf : ae_strongly_measurable f μ) (c : β) : ae_strongly_measurable (λ x, f x * c) μ := hf.mul ae_strongly_measurable_const @[to_additive] protected lemma const_mul [has_mul β] [has_continuous_mul β] (hf : ae_strongly_measurable f μ) (c : β) : ae_strongly_measurable (λ x, c * f x) μ := ae_strongly_measurable_const.mul hf @[to_additive] protected lemma inv [group β] [topological_group β] (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (f⁻¹) μ := ⟨(hf.mk f)⁻¹, hf.strongly_measurable_mk.inv, hf.ae_eq_mk.inv⟩ @[to_additive] protected lemma div [group β] [topological_group β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f / g) μ := ⟨hf.mk f / hg.mk g, hf.strongly_measurable_mk.div hg.strongly_measurable_mk, hf.ae_eq_mk.div hg.ae_eq_mk⟩ @[to_additive] protected lemma smul {𝕜} [topological_space 𝕜] [has_scalar 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} {g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, f x • g x) μ := continuous_smul.comp_ae_strongly_measurable (hf.prod_mk hg) protected lemma const_smul {𝕜} [has_scalar 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : ae_strongly_measurable f μ) (c : 𝕜) : ae_strongly_measurable (c • f) μ := ⟨c • hf.mk f, hf.strongly_measurable_mk.const_smul c, hf.ae_eq_mk.const_smul c⟩ protected lemma const_smul' {𝕜} [has_scalar 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : ae_strongly_measurable f μ) (c : 𝕜) : ae_strongly_measurable (λ x, c • (f x)) μ := hf.const_smul c @[to_additive] protected lemma smul_const {𝕜} [topological_space 𝕜] [has_scalar 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} (hf : ae_strongly_measurable f μ) (c : β) : ae_strongly_measurable (λ x, f x • c) μ := continuous_smul.comp_ae_strongly_measurable (hf.prod_mk ae_strongly_measurable_const) end arithmetic section order protected lemma sup [semilattice_sup β] [has_continuous_sup β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f ⊔ g) μ := ⟨hf.mk f ⊔ hg.mk g, hf.strongly_measurable_mk.sup hg.strongly_measurable_mk, hf.ae_eq_mk.sup hg.ae_eq_mk⟩ protected lemma inf [semilattice_inf β] [has_continuous_inf β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f ⊓ g) μ := ⟨hf.mk f ⊓ hg.mk g, hf.strongly_measurable_mk.inf hg.strongly_measurable_mk, hf.ae_eq_mk.inf hg.ae_eq_mk⟩ end order /-! ### Big operators: `∏` and `∑` -/ section monoid variables {M : Type*} [monoid M] [topological_space M] [has_continuous_mul M] @[to_additive] lemma _root_.list.ae_strongly_measurable_prod' (l : list (α → M)) (hl : ∀ f ∈ l, ae_strongly_measurable f μ) : ae_strongly_measurable l.prod μ := begin induction l with f l ihl, { exact ae_strongly_measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[to_additive] lemma _root_.list.ae_strongly_measurable_prod (l : list (α → M)) (hl : ∀ f ∈ l, ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, (l.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.list_prod_apply] using l.ae_strongly_measurable_prod' hl end monoid section comm_monoid variables {M : Type*} [comm_monoid M] [topological_space M] [has_continuous_mul M] @[to_additive] lemma _root_.multiset.ae_strongly_measurable_prod' (l : multiset (α → M)) (hl : ∀ f ∈ l, ae_strongly_measurable f μ) : ae_strongly_measurable l.prod μ := by { rcases l with ⟨l⟩, simpa using l.ae_strongly_measurable_prod' (by simpa using hl) } @[to_additive] lemma _root_.multiset.ae_strongly_measurable_prod (s : multiset (α → M)) (hs : ∀ f ∈ s, ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, (s.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.multiset_prod_apply] using s.ae_strongly_measurable_prod' hs @[to_additive] lemma _root_.finset.ae_strongly_measurable_prod' {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, ae_strongly_measurable (f i) μ) : ae_strongly_measurable (∏ i in s, f i) μ := multiset.ae_strongly_measurable_prod' _ $ λ g hg, let ⟨i, hi, hg⟩ := multiset.mem_map.1 hg in (hg ▸ hf _ hi) @[to_additive] lemma _root_.finset.ae_strongly_measurable_prod {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, ae_strongly_measurable (f i) μ) : ae_strongly_measurable (λ a, ∏ i in s, f i a) μ := by simpa only [← finset.prod_apply] using s.ae_strongly_measurable_prod' hf end comm_monoid section second_countable_ae_strongly_measurable variables [measurable_space β] /-- In a space with second countable topology, measurable implies strongly measurable. -/ lemma _root_.ae_measurable.ae_strongly_measurable [metrizable_space β] [opens_measurable_space β] [second_countable_topology β] (hf : ae_measurable f μ) : ae_strongly_measurable f μ := ⟨hf.mk f, hf.measurable_mk.strongly_measurable, hf.ae_eq_mk⟩ lemma _root_.ae_strongly_measurable_id {α : Type*} [topological_space α] [metrizable_space α] {m : measurable_space α} [opens_measurable_space α] [second_countable_topology α] {μ : measure α} : ae_strongly_measurable (id : α → α) μ := ae_measurable_id.ae_strongly_measurable /-- In a space with second countable topology, strongly measurable and measurable are equivalent. -/ lemma _root_.ae_strongly_measurable_iff_ae_measurable [metrizable_space β] [borel_space β] [second_countable_topology β] : ae_strongly_measurable f μ ↔ ae_measurable f μ := ⟨λ h, h.ae_measurable, λ h, h.ae_strongly_measurable⟩ end second_countable_ae_strongly_measurable protected lemma dist {β : Type*} [pseudo_metric_space β] {f g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, dist (f x) (g x)) μ := continuous_dist.comp_ae_strongly_measurable (hf.prod_mk hg) protected lemma norm {β : Type*} [normed_group β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, ∥f x∥) μ := continuous_norm.comp_ae_strongly_measurable hf protected lemma nnnorm {β : Type*} [normed_group β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, ∥f x∥₊) μ := continuous_nnnorm.comp_ae_strongly_measurable hf protected lemma ennnorm {β : Type*} [normed_group β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_measurable (λ a, (∥f a∥₊ : ℝ≥0∞)) μ := (ennreal.continuous_coe.comp_ae_strongly_measurable hf.nnnorm).ae_measurable protected lemma edist {β : Type*} [normed_group β] {f g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_measurable (λ a, edist (f a) (g a)) μ := (continuous_edist.comp_ae_strongly_measurable (hf.prod_mk hg)).ae_measurable protected lemma real_to_nnreal {f : α → ℝ} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, (f x).to_nnreal) μ := continuous_real_to_nnreal.comp_ae_strongly_measurable hf section variables {𝕜 : Type*} {E : Type*} [is_R_or_C 𝕜] [inner_product_space 𝕜 E] local notation `⟪`x`, `y`⟫` := @inner 𝕜 _ _ x y protected lemma re {f : α → 𝕜} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, is_R_or_C.re (f x)) μ := is_R_or_C.continuous_re.comp_ae_strongly_measurable hf protected lemma im {f : α → 𝕜} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, is_R_or_C.im (f x)) μ := is_R_or_C.continuous_im.comp_ae_strongly_measurable hf protected lemma inner {m : measurable_space α} {μ : measure α} {f g : α → E} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, ⟪f x, g x⟫) μ := continuous_inner.comp_ae_strongly_measurable (hf.prod_mk hg) end lemma _root_.ae_strongly_measurable_indicator_iff [has_zero β] {s : set α} (hs : measurable_set s) : ae_strongly_measurable (indicator s f) μ ↔ ae_strongly_measurable f (μ.restrict s) := begin split, { intro h, exact (h.mono_measure measure.restrict_le_self).congr (indicator_ae_eq_restrict hs) }, { intro h, refine ⟨indicator s (h.mk f), h.strongly_measurable_mk.indicator hs, _⟩, have A : s.indicator f =ᵐ[μ.restrict s] s.indicator (h.mk f) := (indicator_ae_eq_restrict hs).trans (h.ae_eq_mk.trans $ (indicator_ae_eq_restrict hs).symm), have B : s.indicator f =ᵐ[μ.restrict sᶜ] s.indicator (h.mk f) := (indicator_ae_eq_restrict_compl hs).trans (indicator_ae_eq_restrict_compl hs).symm, exact ae_of_ae_restrict_of_ae_restrict_compl _ A B }, end protected lemma indicator [has_zero β] (hfm : ae_strongly_measurable f μ) {s : set α} (hs : measurable_set s) : ae_strongly_measurable (s.indicator f) μ := (ae_strongly_measurable_indicator_iff hs).mpr hfm.restrict lemma _root_.ae_strongly_measurable_of_ae_strongly_measurable_trim {α} {m m0 : measurable_space α} {μ : measure α} (hm : m ≤ m0) {f : α → β} (hf : ae_strongly_measurable f (μ.trim hm)) : ae_strongly_measurable f μ := ⟨hf.mk f, strongly_measurable.mono hf.strongly_measurable_mk hm, ae_eq_of_ae_eq_trim hf.ae_eq_mk⟩ lemma comp_ae_measurable {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} (hg : ae_strongly_measurable g (measure.map f μ)) (hf : ae_measurable f μ) : ae_strongly_measurable (g ∘ f) μ := ⟨(hg.mk g) ∘ hf.mk f, hg.strongly_measurable_mk.comp_measurable hf.measurable_mk, (ae_eq_comp hf hg.ae_eq_mk).trans ((hf.ae_eq_mk).fun_comp (hg.mk g))⟩ lemma comp_measurable {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} (hg : ae_strongly_measurable g (measure.map f μ)) (hf : measurable f) : ae_strongly_measurable (g ∘ f) μ := hg.comp_ae_measurable hf.ae_measurable lemma comp_measurable' {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} {ν : measure α} (hg : ae_strongly_measurable g ν) (hf : measurable f) (h : μ.map f ≪ ν) : ae_strongly_measurable (g ∘ f) μ := (hg.mono' h).comp_measurable hf lemma is_separable_ae_range (hf : ae_strongly_measurable f μ) : ∃ (t : set β), is_separable t ∧ ∀ᵐ x ∂μ, f x ∈ t := begin refine ⟨range (hf.mk f), hf.strongly_measurable_mk.is_separable_range, _⟩, filter_upwards [hf.ae_eq_mk] with x hx, simp [hx] end /-- A function is almost everywhere strongly measurable if and only if it is almost everywhere measurable, and up to a zero measure set its range is contained in a separable set. -/ theorem _root_.ae_strongly_measurable_iff_ae_measurable_separable [metrizable_space β] [measurable_space β] [borel_space β] : ae_strongly_measurable f μ ↔ (ae_measurable f μ ∧ ∃ (t : set β), is_separable t ∧ ∀ᵐ x ∂μ, f x ∈ t) := begin letI : metric_space β := metrizable_space_metric β, classical, refine ⟨λ H, ⟨H.ae_measurable, H.is_separable_ae_range⟩, _⟩, rintros ⟨H, ⟨t, t_sep, ht⟩⟩, rcases eq_empty_or_nonempty t with rfl|h₀, { simp only [mem_empty_eq, eventually_false_iff_eq_bot, ae_eq_bot] at ht, rw ht, exact ae_measurable_zero_measure f }, { obtain ⟨g, g_meas, gt, fg⟩ : ∃ (g : α → β), measurable g ∧ range g ⊆ t ∧ f =ᵐ[μ] g := H.exists_ae_eq_range_subset ht h₀, refine ⟨g, _, fg⟩, exact strongly_measurable_iff_measurable_separable.2 ⟨g_meas, t_sep.mono gt⟩ } end lemma _root_.measurable_embedding.ae_strongly_measurable_map_iff {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} (hf : measurable_embedding f) {g : α → β} : ae_strongly_measurable g (measure.map f μ) ↔ ae_strongly_measurable (g ∘ f) μ := begin refine ⟨λ H, H.comp_measurable hf.measurable, _⟩, rintro ⟨g₁, hgm₁, heq⟩, rcases hf.exists_strongly_measurable_extend hgm₁ (λ x, ⟨g x⟩) with ⟨g₂, hgm₂, rfl⟩, exact ⟨g₂, hgm₂, hf.ae_map_iff.2 heq⟩ end lemma _root_.embedding.ae_strongly_measurable_comp_iff [metrizable_space β] [metrizable_space γ] {g : β → γ} {f : α → β} (hg : embedding g) : ae_strongly_measurable (λ x, g (f x)) μ ↔ ae_strongly_measurable f μ := begin letI := metrizable_space_metric γ, borelize [β, γ], refine ⟨λ H, ae_strongly_measurable_iff_ae_measurable_separable.2 ⟨_, _⟩, λ H, hg.continuous.comp_ae_strongly_measurable H⟩, { let G : β → range g := cod_restrict g (range g) mem_range_self, have hG : closed_embedding G := { closed_range := begin convert is_closed_univ, apply eq_univ_of_forall, rintros ⟨-, ⟨x, rfl⟩⟩, exact mem_range_self x end, .. hg.cod_restrict _ _ }, have : ae_measurable (G ∘ f) μ := ae_measurable.subtype_mk H.ae_measurable, exact hG.measurable_embedding.ae_measurable_comp_iff.1 this }, { rcases (ae_strongly_measurable_iff_ae_measurable_separable.1 H).2 with ⟨t, ht, h't⟩, exact ⟨g⁻¹' t, hg.is_separable_preimage ht, h't⟩ } end lemma _root_.measure_theory.measure_preserving.ae_strongly_measurable_comp_iff {β : Type*} {f : α → β} {mα : measurable_space α} {μa : measure α} {mβ : measurable_space β} {μb : measure β} (hf : measure_preserving f μa μb) (h₂ : measurable_embedding f) {g : β → γ} : ae_strongly_measurable (g ∘ f) μa ↔ ae_strongly_measurable g μb := by rw [← hf.map_eq, h₂.ae_strongly_measurable_map_iff] /-- An almost everywhere sequential limit of almost everywhere strongly measurable functions is almost everywhere strongly measurable. -/ lemma _root_.ae_strongly_measurable_of_tendsto_ae {ι : Type*} [metrizable_space β] (u : filter ι) [ne_bot u] [is_countably_generated u] {f : ι → α → β} {g : α → β} (hf : ∀ i, ae_strongly_measurable (f i) μ) (lim : ∀ᵐ x ∂μ, tendsto (λ n, f n x) u (𝓝 (g x))) : ae_strongly_measurable g μ := begin letI := metrizable_space_metric β, borelize β, refine ae_strongly_measurable_iff_ae_measurable_separable.2 ⟨_, _⟩, { exact ae_measurable_of_tendsto_metric_ae _ (λ n, (hf n).ae_measurable) lim }, { rcases u.exists_seq_tendsto with ⟨v, hv⟩, have : ∀ (n : ℕ), ∃ (t : set β), is_separable t ∧ f (v n) ⁻¹' t ∈ μ.ae := λ n, (ae_strongly_measurable_iff_ae_measurable_separable.1 (hf (v n))).2, choose t t_sep ht using this, refine ⟨closure (⋃ i, (t i)), (is_separable_Union (λ i, (t_sep i))).closure, _⟩, filter_upwards [ae_all_iff.2 ht, lim] with x hx h'x, apply mem_closure_of_tendsto ((h'x).comp hv), apply eventually_of_forall (λ n, _), apply mem_Union_of_mem n, exact hx n } end /-- If a sequence of almost everywhere strongly measurable functions converges almost everywhere, one can select a strongly measurable function as the almost everywhere limit. -/ lemma _root_.exists_strongly_measurable_limit_of_tendsto_ae [metrizable_space β] {f : ℕ → α → β} (hf : ∀ n, ae_strongly_measurable (f n) μ) (h_ae_tendsto : ∀ᵐ x ∂μ, ∃ l : β, tendsto (λ n, f n x) at_top (𝓝 l)) : ∃ (f_lim : α → β) (hf_lim_meas : strongly_measurable f_lim), ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x)) := begin borelize β, letI := metrizable_space_metric β, obtain ⟨g, g_meas, hg⟩ : ∃ (g : α → β) (g_meas : measurable g), ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 (g x)) := measurable_limit_of_tendsto_metric_ae (λ n, (hf n).ae_measurable) h_ae_tendsto, have Hg : ae_strongly_measurable g μ := ae_strongly_measurable_of_tendsto_ae _ hf hg, refine ⟨Hg.mk g, Hg.strongly_measurable_mk, _⟩, filter_upwards [hg, Hg.ae_eq_mk] with x hx h'x, rwa h'x at hx, end lemma sum_measure [metrizable_space β] {m : measurable_space α} {μ : ι → measure α} (h : ∀ i, ae_strongly_measurable f (μ i)) : ae_strongly_measurable f (measure.sum μ) := begin borelize β, refine ae_strongly_measurable_iff_ae_measurable_separable.2 ⟨ae_measurable.sum_measure (λ i, (h i).ae_measurable), _⟩, have A : ∀ (i : ι), ∃ (t : set β), is_separable t ∧ f ⁻¹' t ∈ (μ i).ae := λ i, (ae_strongly_measurable_iff_ae_measurable_separable.1 (h i)).2, choose t t_sep ht using A, refine ⟨(⋃ i, t i), is_separable_Union t_sep, _⟩, simp only [measure.ae_sum_eq, mem_Union, eventually_supr], assume i, filter_upwards [ht i] with x hx, exact ⟨i, hx⟩ end @[simp] lemma _root_.ae_strongly_measurable_sum_measure_iff [metrizable_space β] {m : measurable_space α} {μ : ι → measure α} : ae_strongly_measurable f (sum μ) ↔ ∀ i, ae_strongly_measurable f (μ i) := ⟨λ h i, h.mono_measure (measure.le_sum _ _), sum_measure⟩ @[simp] lemma _root_.ae_strongly_measurable_add_measure_iff [metrizable_space β] {ν : measure α} : ae_strongly_measurable f (μ + ν) ↔ ae_strongly_measurable f μ ∧ ae_strongly_measurable f ν := by { rw [← sum_cond, ae_strongly_measurable_sum_measure_iff, bool.forall_bool, and.comm], refl } lemma add_measure [metrizable_space β] {ν : measure α} {f : α → β} (hμ : ae_strongly_measurable f μ) (hν : ae_strongly_measurable f ν) : ae_strongly_measurable f (μ + ν) := ae_strongly_measurable_add_measure_iff.2 ⟨hμ, hν⟩ protected lemma Union [metrizable_space β] {s : ι → set α} (h : ∀ i, ae_strongly_measurable f (μ.restrict (s i))) : ae_strongly_measurable f (μ.restrict (⋃ i, s i)) := (sum_measure h).mono_measure $ restrict_Union_le @[simp] lemma _root_.ae_strongly_measurable_Union_iff [metrizable_space β] {s : ι → set α} : ae_strongly_measurable f (μ.restrict (⋃ i, s i)) ↔ ∀ i, ae_strongly_measurable f (μ.restrict (s i)) := ⟨λ h i, h.mono_measure $ restrict_mono (subset_Union _ _) le_rfl, ae_strongly_measurable.Union⟩ @[simp] lemma _root_.ae_strongly_measurable_union_iff [metrizable_space β] {s t : set α} : ae_strongly_measurable f (μ.restrict (s ∪ t)) ↔ ae_strongly_measurable f (μ.restrict s) ∧ ae_strongly_measurable f (μ.restrict t) := by simp only [union_eq_Union, ae_strongly_measurable_Union_iff, bool.forall_bool, cond, and.comm] lemma smul_measure {R : Type*} [monoid R] [distrib_mul_action R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] (h : ae_strongly_measurable f μ) (c : R) : ae_strongly_measurable f (c • μ) := ⟨h.mk f, h.strongly_measurable_mk, ae_smul_measure h.ae_eq_mk c⟩ section normed_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] lemma _root_.ae_strongly_measurable_smul_const_iff {f : α → 𝕜} {c : E} (hc : c ≠ 0) : ae_strongly_measurable (λ x, f x • c) μ ↔ ae_strongly_measurable f μ := (closed_embedding_smul_left hc).to_embedding.ae_strongly_measurable_comp_iff end normed_space section mul_action variables {G : Type*} [group G] [mul_action G β] [has_continuous_const_smul G β] lemma _root_.ae_strongly_measurable_const_smul_iff (c : G) : ae_strongly_measurable (λ x, c • f x) μ ↔ ae_strongly_measurable f μ := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ variables {G₀ : Type*} [group_with_zero G₀] [mul_action G₀ β] [has_continuous_const_smul G₀ β] lemma _root_.ae_strongly_measurable_const_smul_iff₀ {c : G₀} (hc : c ≠ 0) : ae_strongly_measurable (λ x, c • f x) μ ↔ ae_strongly_measurable f μ := begin refine ⟨λ h, _, λ h, h.const_smul c⟩, convert h.const_smul' c⁻¹, simp [smul_smul, inv_mul_cancel hc] end end mul_action section continuous_linear_map_nondiscrete_normed_field variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_group F] [normed_space 𝕜 F] variables {G : Type*} [normed_group G] [normed_space 𝕜 G] lemma _root_.strongly_measurable.apply_continuous_linear_map {m : measurable_space α} {φ : α → F →L[𝕜] E} (hφ : strongly_measurable φ) (v : F) : strongly_measurable (λ a, φ a v) := (continuous_linear_map.apply 𝕜 E v).continuous.comp_strongly_measurable hφ lemma apply_continuous_linear_map {φ : α → F →L[𝕜] E} (hφ : ae_strongly_measurable φ μ) (v : F) : ae_strongly_measurable (λ a, φ a v) μ := (continuous_linear_map.apply 𝕜 E v).continuous.comp_ae_strongly_measurable hφ lemma _root_.continuous_linear_map.ae_strongly_measurable_comp₂ (L : E →L[𝕜] F →L[𝕜] G) {f : α → E} {g : α → F} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, L (f x) (g x)) μ := L.continuous₂.comp_ae_strongly_measurable $ hf.prod_mk hg end continuous_linear_map_nondiscrete_normed_field lemma _root_.ae_strongly_measurable_with_density_iff {E : Type*} [normed_group E] [normed_space ℝ E] {f : α → ℝ≥0} (hf : measurable f) {g : α → E} : ae_strongly_measurable g (μ.with_density (λ x, (f x : ℝ≥0∞))) ↔ ae_strongly_measurable (λ x, (f x : ℝ) • g x) μ := begin split, { rintros ⟨g', g'meas, hg'⟩, have A : measurable_set {x : α | f x ≠ 0} := (hf (measurable_set_singleton 0)).compl, refine ⟨λ x, (f x : ℝ) • g' x, hf.coe_nnreal_real.strongly_measurable.smul g'meas, _⟩, apply @ae_of_ae_restrict_of_ae_restrict_compl _ _ _ {x | f x ≠ 0}, { rw [eventually_eq, ae_with_density_iff hf.coe_nnreal_ennreal] at hg', rw ae_restrict_iff' A, filter_upwards [hg'] with a ha h'a, have : (f a : ℝ≥0∞) ≠ 0, by simpa only [ne.def, ennreal.coe_eq_zero] using h'a, rw ha this }, { filter_upwards [ae_restrict_mem A.compl] with x hx, simp only [not_not, mem_set_of_eq, mem_compl_eq] at hx, simp [hx] } }, { rintros ⟨g', g'meas, hg'⟩, refine ⟨λ x, (f x : ℝ)⁻¹ • g' x, hf.coe_nnreal_real.inv.strongly_measurable.smul g'meas, _⟩, rw [eventually_eq, ae_with_density_iff hf.coe_nnreal_ennreal], filter_upwards [hg'] with x hx h'x, rw [← hx, smul_smul, _root_.inv_mul_cancel, one_smul], simp only [ne.def, ennreal.coe_eq_zero] at h'x, simpa only [nnreal.coe_eq_zero, ne.def] using h'x } end end ae_strongly_measurable /-! ## Almost everywhere finitely strongly measurable functions -/ namespace ae_fin_strongly_measurable variables {m : measurable_space α} {μ : measure α} [topological_space β] {f g : α → β} section mk variables [has_zero β] /-- A `fin_strongly_measurable` function such that `f =ᵐ[μ] hf.mk f`. See lemmas `fin_strongly_measurable_mk` and `ae_eq_mk`. -/ protected noncomputable def mk (f : α → β) (hf : ae_fin_strongly_measurable f μ) : α → β := hf.some lemma fin_strongly_measurable_mk (hf : ae_fin_strongly_measurable f μ) : fin_strongly_measurable (hf.mk f) μ := hf.some_spec.1 lemma ae_eq_mk (hf : ae_fin_strongly_measurable f μ) : f =ᵐ[μ] hf.mk f := hf.some_spec.2 protected lemma ae_measurable {β} [has_zero β] [measurable_space β] [topological_space β] [metrizable_space β] [borel_space β] {f : α → β} (hf : ae_fin_strongly_measurable f μ) : ae_measurable f μ := ⟨hf.mk f, hf.fin_strongly_measurable_mk.measurable, hf.ae_eq_mk⟩ end mk section arithmetic protected lemma mul [monoid_with_zero β] [has_continuous_mul β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f * g) μ := ⟨hf.mk f * hg.mk g, hf.fin_strongly_measurable_mk.mul hg.fin_strongly_measurable_mk, hf.ae_eq_mk.mul hg.ae_eq_mk⟩ protected lemma add [add_monoid β] [has_continuous_add β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f + g) μ := ⟨hf.mk f + hg.mk g, hf.fin_strongly_measurable_mk.add hg.fin_strongly_measurable_mk, hf.ae_eq_mk.add hg.ae_eq_mk⟩ protected lemma neg [add_group β] [topological_add_group β] (hf : ae_fin_strongly_measurable f μ) : ae_fin_strongly_measurable (-f) μ := ⟨-hf.mk f, hf.fin_strongly_measurable_mk.neg, hf.ae_eq_mk.neg⟩ protected lemma sub [add_group β] [has_continuous_sub β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f - g) μ := ⟨hf.mk f - hg.mk g, hf.fin_strongly_measurable_mk.sub hg.fin_strongly_measurable_mk, hf.ae_eq_mk.sub hg.ae_eq_mk⟩ protected lemma const_smul {𝕜} [topological_space 𝕜] [add_monoid β] [monoid 𝕜] [distrib_mul_action 𝕜 β] [has_continuous_smul 𝕜 β] (hf : ae_fin_strongly_measurable f μ) (c : 𝕜) : ae_fin_strongly_measurable (c • f) μ := ⟨c • hf.mk f, hf.fin_strongly_measurable_mk.const_smul c, hf.ae_eq_mk.const_smul c⟩ end arithmetic section order variables [has_zero β] protected lemma sup [semilattice_sup β] [has_continuous_sup β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f ⊔ g) μ := ⟨hf.mk f ⊔ hg.mk g, hf.fin_strongly_measurable_mk.sup hg.fin_strongly_measurable_mk, hf.ae_eq_mk.sup hg.ae_eq_mk⟩ protected lemma inf [semilattice_inf β] [has_continuous_inf β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f ⊓ g) μ := ⟨hf.mk f ⊓ hg.mk g, hf.fin_strongly_measurable_mk.inf hg.fin_strongly_measurable_mk, hf.ae_eq_mk.inf hg.ae_eq_mk⟩ end order variables [has_zero β] [t2_space β] lemma exists_set_sigma_finite (hf : ae_fin_strongly_measurable f μ) : ∃ t, measurable_set t ∧ f =ᵐ[μ.restrict tᶜ] 0 ∧ sigma_finite (μ.restrict t) := begin rcases hf with ⟨g, hg, hfg⟩, obtain ⟨t, ht, hgt_zero, htμ⟩ := hg.exists_set_sigma_finite, refine ⟨t, ht, _, htμ⟩, refine eventually_eq.trans (ae_restrict_of_ae hfg) _, rw [eventually_eq, ae_restrict_iff' ht.compl], exact eventually_of_forall hgt_zero, end /-- A measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `sigma_finite (μ.restrict t)`. -/ def sigma_finite_set (hf : ae_fin_strongly_measurable f μ) : set α := hf.exists_set_sigma_finite.some protected lemma measurable_set (hf : ae_fin_strongly_measurable f μ) : measurable_set hf.sigma_finite_set := hf.exists_set_sigma_finite.some_spec.1 lemma ae_eq_zero_compl (hf : ae_fin_strongly_measurable f μ) : f =ᵐ[μ.restrict hf.sigma_finite_setᶜ] 0 := hf.exists_set_sigma_finite.some_spec.2.1 instance sigma_finite_restrict (hf : ae_fin_strongly_measurable f μ) : sigma_finite (μ.restrict hf.sigma_finite_set) := hf.exists_set_sigma_finite.some_spec.2.2 end ae_fin_strongly_measurable section second_countable_topology variables {G : Type*} {p : ℝ≥0∞} {m m0 : measurable_space α} {μ : measure α} [normed_group G] [measurable_space G] [borel_space G] [second_countable_topology G] {f : α → G} /-- In a space with second countable topology and a sigma-finite measure, `fin_strongly_measurable` and `measurable` are equivalent. -/ lemma fin_strongly_measurable_iff_measurable {m0 : measurable_space α} (μ : measure α) [sigma_finite μ] : fin_strongly_measurable f μ ↔ measurable f := ⟨λ h, h.measurable, λ h, (measurable.strongly_measurable h).fin_strongly_measurable μ⟩ /-- In a space with second countable topology and a sigma-finite measure, `ae_fin_strongly_measurable` and `ae_measurable` are equivalent. -/ lemma ae_fin_strongly_measurable_iff_ae_measurable {m0 : measurable_space α} (μ : measure α) [sigma_finite μ] : ae_fin_strongly_measurable f μ ↔ ae_measurable f μ := by simp_rw [ae_fin_strongly_measurable, ae_measurable, fin_strongly_measurable_iff_measurable] end second_countable_topology lemma measurable_uncurry_of_continuous_of_measurable {α β ι : Type*} [topological_space ι] [metrizable_space ι] [measurable_space ι] [second_countable_topology ι] [opens_measurable_space ι] {mβ : measurable_space β} [topological_space β] [metrizable_space β] [borel_space β] {m : measurable_space α} {u : ι → α → β} (hu_cont : ∀ x, continuous (λ i, u i x)) (h : ∀ i, measurable (u i)) : measurable (function.uncurry u) := begin letI := metrizable_space_metric β, obtain ⟨t_sf, ht_sf⟩ : ∃ t : ℕ → simple_func ι ι, ∀ j x, tendsto (λ n, u (t n j) x) at_top (𝓝 $ u j x), { have h_str_meas : strongly_measurable (id : ι → ι), from strongly_measurable_id, refine ⟨h_str_meas.approx, λ j x, _⟩, exact ((hu_cont x).tendsto j).comp (h_str_meas.tendsto_approx j), }, let U := λ (n : ℕ) (p : ι × α), u (t_sf n p.fst) p.snd, have h_tendsto : tendsto U at_top (𝓝 (λ p, u p.fst p.snd)), { rw tendsto_pi_nhds, exact λ p, ht_sf p.fst p.snd, }, refine measurable_of_tendsto_metric (λ n, _) h_tendsto, haveI : encodable (t_sf n).range, from fintype.to_encodable ↥(t_sf n).range, have h_meas : measurable (λ (p : (t_sf n).range × α), u ↑p.fst p.snd), { have : (λ (p : ↥((t_sf n).range) × α), u ↑(p.fst) p.snd) = (λ (p : α × ((t_sf n).range)), u ↑(p.snd) p.fst) ∘ prod.swap := rfl, rw [this, @measurable_swap_iff α ↥((t_sf n).range) β m], exact measurable_from_prod_encodable (λ j, h j), }, have : (λ p : ι × α, u (t_sf n p.fst) p.snd) = (λ p : ↥(t_sf n).range × α, u p.fst p.snd) ∘ (λ p : ι × α, (⟨t_sf n p.fst, simple_func.mem_range_self _ _⟩, p.snd)) := rfl, simp_rw [U, this], refine h_meas.comp (measurable.prod_mk _ measurable_snd), exact ((t_sf n).measurable.comp measurable_fst).subtype_mk, end lemma strongly_measurable_uncurry_of_continuous_of_strongly_measurable {α β ι : Type*} [topological_space ι] [metrizable_space ι] [measurable_space ι] [second_countable_topology ι] [opens_measurable_space ι] [topological_space β] [metrizable_space β] [measurable_space α] {u : ι → α → β} (hu_cont : ∀ x, continuous (λ i, u i x)) (h : ∀ i, strongly_measurable (u i)) : strongly_measurable (function.uncurry u) := begin borelize β, obtain ⟨t_sf, ht_sf⟩ : ∃ t : ℕ → simple_func ι ι, ∀ j x, tendsto (λ n, u (t n j) x) at_top (𝓝 $ u j x), { have h_str_meas : strongly_measurable (id : ι → ι), from strongly_measurable_id, refine ⟨h_str_meas.approx, λ j x, _⟩, exact ((hu_cont x).tendsto j).comp (h_str_meas.tendsto_approx j), }, let U := λ (n : ℕ) (p : ι × α), u (t_sf n p.fst) p.snd, have h_tendsto : tendsto U at_top (𝓝 (λ p, u p.fst p.snd)), { rw tendsto_pi_nhds, exact λ p, ht_sf p.fst p.snd, }, refine strongly_measurable_of_tendsto _ (λ n, _) h_tendsto, haveI : encodable (t_sf n).range, from fintype.to_encodable ↥(t_sf n).range, have h_str_meas : strongly_measurable (λ (p : (t_sf n).range × α), u ↑p.fst p.snd), { refine strongly_measurable_iff_measurable_separable.2 ⟨_, _⟩, { have : (λ (p : ↥((t_sf n).range) × α), u ↑(p.fst) p.snd) = (λ (p : α × ((t_sf n).range)), u ↑(p.snd) p.fst) ∘ prod.swap := rfl, rw [this, measurable_swap_iff], exact measurable_from_prod_encodable (λ j, (h j).measurable), }, { have : is_separable (⋃ (i : (t_sf n).range), range (u i)) := is_separable_Union (λ i, (h i).is_separable_range), apply this.mono, rintros - ⟨⟨i, x⟩, rfl⟩, simp only [mem_Union, mem_range], exact ⟨i, x, rfl⟩ } }, have : (λ p : ι × α, u (t_sf n p.fst) p.snd) = (λ p : ↥(t_sf n).range × α, u p.fst p.snd) ∘ (λ p : ι × α, (⟨t_sf n p.fst, simple_func.mem_range_self _ _⟩, p.snd)) := rfl, simp_rw [U, this], refine h_str_meas.comp_measurable (measurable.prod_mk _ measurable_snd), exact ((t_sf n).measurable.comp measurable_fst).subtype_mk, end end measure_theory
6fa8cd8c4c27955b903cd258d5a961e080d97b3c
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/algebra/algebra/basic.lean
b9349b54ff3c5d743759a64d4f83a8c9c20aeaf2
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
52,178
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import tactic.nth_rewrite import data.matrix.basic import data.equiv.ring_aut import linear_algebra.tensor_product import ring_theory.subring import deprecated.subring import algebra.opposites /-! # Algebra over Commutative Semiring In this file we define `algebra`s over commutative (semi)rings, algebra homomorphisms `alg_hom`, algebra equivalences `alg_equiv`. We also define usual operations on `alg_hom`s (`id`, `comp`). `subalgebra`s are defined in `algebra.algebra.subalgebra`. If `S` is an `R`-algebra and `A` is an `S`-algebra then `algebra.comap.algebra R S A` can be used to provide `A` with a structure of an `R`-algebra. Other than that, `algebra.comap` is now deprecated and replaced with `is_scalar_tower`. For the category of `R`-algebras, denoted `Algebra R`, see the file `algebra/category/Algebra/basic.lean`. ## Notations * `A →ₐ[R] B` : `R`-algebra homomorphism from `A` to `B`. * `A ≃ₐ[R] B` : `R`-algebra equivalence from `A` to `B`. -/ universes u v w u₁ v₁ open_locale tensor_product big_operators section prio -- We set this priority to 0 later in this file set_option extends_priority 200 /- control priority of `instance [algebra R A] : has_scalar R A` -/ /-- Given a commutative (semi)ring `R`, an `R`-algebra is a (possibly noncommutative) (semi)ring `A` endowed with a morphism of rings `R →+* A` which lands in the center of `A`. For convenience, this typeclass extends `has_scalar R A` where the scalar action must agree with left multiplication by the image of the structure morphism. Given an `algebra R A` instance, the structure morphism `R →+* A` is denoted `algebra_map R A`. -/ @[nolint has_inhabited_instance] class algebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A] extends has_scalar R A, R →+* A := (commutes' : ∀ r x, to_fun r * x = x * to_fun r) (smul_def' : ∀ r x, r • x = to_fun r * x) end prio /-- Embedding `R →+* A` given by `algebra` structure. -/ def algebra_map (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] : R →+* A := algebra.to_ring_hom /-- Creating an algebra from a morphism to the center of a semiring. -/ def ring_hom.to_algebra' {R S} [comm_semiring R] [semiring S] (i : R →+* S) (h : ∀ c x, i c * x = x * i c) : algebra R S := { smul := λ c x, i c * x, commutes' := h, smul_def' := λ c x, rfl, to_ring_hom := i} /-- Creating an algebra from a morphism to a commutative semiring. -/ def ring_hom.to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) : algebra R S := i.to_algebra' $ λ _, mul_comm _ lemma ring_hom.algebra_map_to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) : @algebra_map R S _ _ i.to_algebra = i := rfl namespace algebra variables {R : Type u} {S : Type v} {A : Type w} {B : Type*} /-- Let `R` be a commutative semiring, let `A` be a semiring with a `semimodule R` structure. If `(r • 1) * x = x * (r • 1) = r • x` for all `r : R` and `x : A`, then `A` is an `algebra` over `R`. -/ def of_semimodule' [comm_semiring R] [semiring A] [semimodule R A] (h₁ : ∀ (r : R) (x : A), (r • 1) * x = r • x) (h₂ : ∀ (r : R) (x : A), x * (r • 1) = r • x) : algebra R A := { to_fun := λ r, r • 1, map_one' := one_smul _ _, map_mul' := λ r₁ r₂, by rw [h₁, mul_smul], map_zero' := zero_smul _ _, map_add' := λ r₁ r₂, add_smul r₁ r₂ 1, commutes' := λ r x, by simp only [h₁, h₂], smul_def' := λ r x, by simp only [h₁] } /-- Let `R` be a commutative semiring, let `A` be a semiring with a `semimodule R` structure. If `(r • x) * y = x * (r • y) = r • (x * y)` for all `r : R` and `x y : A`, then `A` is an `algebra` over `R`. -/ def of_semimodule [comm_semiring R] [semiring A] [semimodule R A] (h₁ : ∀ (r : R) (x y : A), (r • x) * y = r • (x * y)) (h₂ : ∀ (r : R) (x y : A), x * (r • y) = r • (x * y)) : algebra R A := of_semimodule' (λ r x, by rw [h₁, one_mul]) (λ r x, by rw [h₂, mul_one]) section semiring variables [comm_semiring R] [comm_semiring S] variables [semiring A] [algebra R A] [semiring B] [algebra R B] lemma smul_def'' (r : R) (x : A) : r • x = algebra_map R A r * x := algebra.smul_def' r x /-- To prove two algebra structures on a fixed `[comm_semiring R] [semiring A]` agree, it suffices to check the `algebra_map`s agree. -/ -- We'll later use this to show `algebra ℤ M` is a subsingleton. @[ext] lemma algebra_ext {R : Type*} [comm_semiring R] {A : Type*} [semiring A] (P Q : algebra R A) (w : ∀ (r : R), by { haveI := P, exact algebra_map R A r } = by { haveI := Q, exact algebra_map R A r }) : P = Q := begin unfreezingI { rcases P with ⟨⟨P⟩⟩, rcases Q with ⟨⟨Q⟩⟩ }, congr, { funext r a, replace w := congr_arg (λ s, s * a) (w r), simp only [←algebra.smul_def''] at w, apply w, }, { ext r, exact w r, }, { apply proof_irrel_heq, }, { apply proof_irrel_heq, }, end @[priority 200] -- see Note [lower instance priority] instance to_semimodule : semimodule R A := { one_smul := by simp [smul_def''], mul_smul := by simp [smul_def'', mul_assoc], smul_add := by simp [smul_def'', mul_add], smul_zero := by simp [smul_def''], add_smul := by simp [smul_def'', add_mul], zero_smul := by simp [smul_def''] } -- from now on, we don't want to use the following instance anymore attribute [instance, priority 0] algebra.to_has_scalar lemma smul_def (r : R) (x : A) : r • x = algebra_map R A r * x := algebra.smul_def' r x lemma algebra_map_eq_smul_one (r : R) : algebra_map R A r = r • 1 := calc algebra_map R A r = algebra_map R A r * 1 : (mul_one _).symm ... = r • 1 : (algebra.smul_def r 1).symm theorem commutes (r : R) (x : A) : algebra_map R A r * x = x * algebra_map R A r := algebra.commutes' r x theorem left_comm (r : R) (x y : A) : x * (algebra_map R A r * y) = algebra_map R A r * (x * y) := by rw [← mul_assoc, ← commutes, mul_assoc] @[simp] lemma mul_smul_comm (s : R) (x y : A) : x * (s • y) = s • (x * y) := by rw [smul_def, smul_def, left_comm] @[simp] lemma smul_mul_assoc (r : R) (x y : A) : (r • x) * y = r • (x * y) := by rw [smul_def, smul_def, mul_assoc] lemma smul_mul_smul (r s : R) (x y : A) : (r • x) * (s • y) = (r * s) • (x * y) := by rw [algebra.smul_mul_assoc, algebra.mul_smul_comm, smul_smul] section variables {r : R} {a : A} @[simp] lemma bit0_smul_one : bit0 r • (1 : A) = r • 2 := by simp [bit0, add_smul, smul_add] @[simp] lemma bit0_smul_bit0 : bit0 r • bit0 a = r • (bit0 (bit0 a)) := by simp [bit0, add_smul, smul_add] @[simp] lemma bit0_smul_bit1 : bit0 r • bit1 a = r • (bit0 (bit1 a)) := by simp [bit0, add_smul, smul_add] @[simp] lemma bit1_smul_one : bit1 r • (1 : A) = r • 2 + 1 := by simp [bit1, add_smul, smul_add] @[simp] lemma bit1_smul_bit0 : bit1 r • bit0 a = r • (bit0 (bit0 a)) + bit0 a := by simp [bit1, add_smul, smul_add] @[simp] lemma bit1_smul_bit1 : bit1 r • bit1 a = r • (bit0 (bit1 a)) + bit1 a := by { simp only [bit0, bit1, add_smul, smul_add, one_smul], abel } end variables (R A) /-- The canonical ring homomorphism `algebra_map R A : R →* A` for any `R`-algebra `A`, packaged as an `R`-linear map. -/ protected def linear_map : R →ₗ[R] A := { map_smul' := λ x y, by simp [algebra.smul_def], ..algebra_map R A } @[simp] lemma linear_map_apply (r : R) : algebra.linear_map R A r = algebra_map R A r := rfl instance id : algebra R R := (ring_hom.id R).to_algebra variables {R A} namespace id @[simp] lemma map_eq_self (x : R) : algebra_map R R x = x := rfl @[simp] lemma smul_eq_mul (x y : R) : x • y = x * y := rfl end id section prod variables (R A B) instance : algebra R (A × B) := { commutes' := by { rintro r ⟨a, b⟩, dsimp, rw [commutes r a, commutes r b] }, smul_def' := by { rintro r ⟨a, b⟩, dsimp, rw [smul_def r a, smul_def r b] }, .. prod.semimodule, .. ring_hom.prod (algebra_map R A) (algebra_map R B) } variables {R A B} @[simp] lemma algebra_map_prod_apply (r : R) : algebra_map R (A × B) r = (algebra_map R A r, algebra_map R B r) := rfl end prod /-- Algebra over a subsemiring. -/ instance of_subsemiring (S : subsemiring R) : algebra S A := { smul := λ s x, (s : R) • x, commutes' := λ r x, algebra.commutes r x, smul_def' := λ r x, algebra.smul_def r x, .. (algebra_map R A).comp (subsemiring.subtype S) } /-- Algebra over a subring. -/ instance of_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A] (S : subring R) : algebra S A := { smul := λ s x, (s : R) • x, commutes' := λ r x, algebra.commutes r x, smul_def' := λ r x, algebra.smul_def r x, .. (algebra_map R A).comp (subring.subtype S) } lemma algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) : (algebra_map S R : S →+* R) = subring.subtype S := rfl lemma coe_algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) : (algebra_map S R : S → R) = subtype.val := rfl lemma algebra_map_of_subring_apply {R : Type*} [comm_ring R] (S : subring R) (x : S) : algebra_map S R x = x := rfl section local attribute [instance] subset.comm_ring /-- Algebra over a set that is closed under the ring operations. -/ local attribute [instance] def of_is_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A] (S : set R) [is_subring S] : algebra S A := algebra.of_subring S.to_subring lemma is_subring_coe_algebra_map_hom {R : Type*} [comm_ring R] (S : set R) [is_subring S] : (algebra_map S R : S →+* R) = is_subring.subtype S := rfl lemma is_subring_coe_algebra_map {R : Type*} [comm_ring R] (S : set R) [is_subring S] : (algebra_map S R : S → R) = subtype.val := rfl lemma is_subring_algebra_map_apply {R : Type*} [comm_ring R] (S : set R) [is_subring S] (x : S) : algebra_map S R x = x := rfl lemma set_range_subset {R : Type*} [comm_ring R] {T₁ T₂ : set R} [is_subring T₁] (hyp : T₁ ⊆ T₂) : set.range (algebra_map T₁ R) ⊆ T₂ := begin rintros x ⟨⟨t, ht⟩, rfl⟩, exact hyp ht, end end /-- Explicit characterization of the submonoid map in the case of an algebra. `S` is made explicit to help with type inference -/ def algebra_map_submonoid (S : Type*) [semiring S] [algebra R S] (M : submonoid R) : (submonoid S) := submonoid.map (algebra_map R S : R →* S) M lemma mem_algebra_map_submonoid_of_mem [algebra R S] {M : submonoid R} (x : M) : (algebra_map R S x) ∈ algebra_map_submonoid S M := set.mem_image_of_mem (algebra_map R S) x.2 end semiring section ring variables [comm_ring R] variables (R) /-- A `semiring` that is an `algebra` over a commutative ring carries a natural `ring` structure. -/ def semiring_to_ring [semiring A] [algebra R A] : ring A := { ..semimodule.add_comm_monoid_to_add_comm_group R, ..(infer_instance : semiring A) } variables {R} lemma mul_sub_algebra_map_commutes [ring A] [algebra R A] (x : A) (r : R) : x * (x - algebra_map R A r) = (x - algebra_map R A r) * x := by rw [mul_sub, ←commutes, sub_mul] lemma mul_sub_algebra_map_pow_commutes [ring A] [algebra R A] (x : A) (r : R) (n : ℕ) : x * (x - algebra_map R A r) ^ n = (x - algebra_map R A r) ^ n * x := begin induction n with n ih, { simp }, { rw [pow_succ, ←mul_assoc, mul_sub_algebra_map_commutes, mul_assoc, ih, ←mul_assoc], } end /-- If `algebra_map R A` is injective and `A` has no zero divisors, `R`-multiples in `A` are zero only if one of the factors is zero. Cannot be an instance because there is no `injective (algebra_map R A)` typeclass. -/ lemma no_zero_smul_divisors.of_algebra_map_injective [semiring A] [algebra R A] [no_zero_divisors A] (h : function.injective (algebra_map R A)) : no_zero_smul_divisors R A := ⟨λ c x hcx, (mul_eq_zero.mp ((smul_def c x).symm.trans hcx)).imp_left ((algebra_map R A).injective_iff.mp h _)⟩ end ring section field variables [field R] [semiring A] [algebra R A] @[priority 100] -- see note [lower instance priority] instance [nontrivial A] [no_zero_divisors A] : no_zero_smul_divisors R A := no_zero_smul_divisors.of_algebra_map_injective (algebra_map R A).injective end field end algebra namespace opposite variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] instance : algebra R Aᵒᵖ := { to_ring_hom := (algebra_map R A).to_opposite $ λ x y, algebra.commutes _ _, smul_def' := λ c x, unop_injective $ by { dsimp, simp only [op_mul, algebra.smul_def, algebra.commutes, op_unop] }, commutes' := λ r, op_induction $ λ x, by dsimp; simp only [← op_mul, algebra.commutes], ..opposite.has_scalar A R } @[simp] lemma algebra_map_apply (c : R) : algebra_map R Aᵒᵖ c = op (algebra_map R A c) := rfl end opposite namespace module variables (R : Type u) (M : Type v) [comm_semiring R] [add_comm_monoid M] [semimodule R M] instance endomorphism_algebra : algebra R (M →ₗ[R] M) := { to_fun := λ r, r • linear_map.id, map_one' := one_smul _ _, map_zero' := zero_smul _ _, map_add' := λ r₁ r₂, add_smul _ _ _, map_mul' := λ r₁ r₂, by { ext x, simp [mul_smul] }, commutes' := by { intros, ext, simp }, smul_def' := by { intros, ext, simp } } lemma algebra_map_End_eq_smul_id (a : R) : (algebra_map R (End R M)) a = a • linear_map.id := rfl @[simp] lemma algebra_map_End_apply (a : R) (m : M) : (algebra_map R (End R M)) a m = a • m := rfl @[simp] lemma ker_algebra_map_End (K : Type u) (V : Type v) [field K] [add_comm_group V] [vector_space K V] (a : K) (ha : a ≠ 0) : ((algebra_map K (End K V)) a).ker = ⊥ := linear_map.ker_smul _ _ ha end module instance matrix_algebra (n : Type u) (R : Type v) [decidable_eq n] [fintype n] [comm_semiring R] : algebra R (matrix n n R) := { commutes' := by { intros, simp [matrix.scalar], }, smul_def' := by { intros, simp [matrix.scalar], }, ..(matrix.scalar n) } set_option old_structure_cmd true /-- Defining the homomorphism in the category R-Alg. -/ @[nolint has_inhabited_instance] structure alg_hom (R : Type u) (A : Type v) (B : Type w) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends ring_hom A B := (commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r) run_cmd tactic.add_doc_string `alg_hom.to_ring_hom "Reinterpret an `alg_hom` as a `ring_hom`" infixr ` →ₐ `:25 := alg_hom _ notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁} section semiring variables [comm_semiring R] [semiring A] [semiring B] [semiring C] [semiring D] variables [algebra R A] [algebra R B] [algebra R C] [algebra R D] instance : has_coe_to_fun (A →ₐ[R] B) := ⟨_, λ f, f.to_fun⟩ initialize_simps_projections alg_hom (to_fun → apply) instance coe_ring_hom : has_coe (A →ₐ[R] B) (A →+* B) := ⟨alg_hom.to_ring_hom⟩ instance coe_monoid_hom : has_coe (A →ₐ[R] B) (A →* B) := ⟨λ f, ↑(f : A →+* B)⟩ instance coe_add_monoid_hom : has_coe (A →ₐ[R] B) (A →+ B) := ⟨λ f, ↑(f : A →+* B)⟩ @[simp, norm_cast] lemma coe_mk {f : A → B} (h₁ h₂ h₃ h₄ h₅) : ⇑(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := rfl @[simp, norm_cast] lemma coe_to_ring_hom (f : A →ₐ[R] B) : ⇑(f : A →+* B) = f := rfl -- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute. @[norm_cast] lemma coe_to_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →* B) = f := rfl -- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute. @[norm_cast] lemma coe_to_add_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →+ B) = f := rfl variables (φ : A →ₐ[R] B) theorem coe_fn_inj ⦃φ₁ φ₂ : A →ₐ[R] B⦄ (H : ⇑φ₁ = φ₂) : φ₁ = φ₂ := by { cases φ₁, cases φ₂, congr, exact H } theorem coe_ring_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+* B)) := λ φ₁ φ₂ H, coe_fn_inj $ show ((φ₁ : (A →+* B)) : A → B) = ((φ₂ : (A →+* B)) : A → B), from congr_arg _ H theorem coe_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →* B)) := ring_hom.coe_monoid_hom_injective.comp coe_ring_hom_injective theorem coe_add_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+ B)) := ring_hom.coe_add_monoid_hom_injective.comp coe_ring_hom_injective protected lemma congr_fun {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁ = φ₂) (x : A) : φ₁ x = φ₂ x := H ▸ rfl protected lemma congr_arg (φ : A →ₐ[R] B) {x y : A} (h : x = y) : φ x = φ y := h ▸ rfl @[ext] theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ := coe_fn_inj $ funext H theorem ext_iff {φ₁ φ₂ : A →ₐ[R] B} : φ₁ = φ₂ ↔ ∀ x, φ₁ x = φ₂ x := ⟨alg_hom.congr_fun, ext⟩ @[simp] theorem mk_coe {f : A →ₐ[R] B} (h₁ h₂ h₃ h₄ h₅) : (⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := ext $ λ _, rfl @[simp] theorem commutes (r : R) : φ (algebra_map R A r) = algebra_map R B r := φ.commutes' r theorem comp_algebra_map : (φ : A →+* B).comp (algebra_map R A) = algebra_map R B := ring_hom.ext $ φ.commutes @[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s := φ.to_ring_hom.map_add r s @[simp] lemma map_zero : φ 0 = 0 := φ.to_ring_hom.map_zero @[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y := φ.to_ring_hom.map_mul x y @[simp] lemma map_one : φ 1 = 1 := φ.to_ring_hom.map_one @[simp] lemma map_smul (r : R) (x : A) : φ (r • x) = r • φ x := by simp only [algebra.smul_def, map_mul, commutes] @[simp] lemma map_pow (x : A) (n : ℕ) : φ (x ^ n) = (φ x) ^ n := φ.to_ring_hom.map_pow x n lemma map_sum {ι : Type*} (f : ι → A) (s : finset ι) : φ (∑ x in s, f x) = ∑ x in s, φ (f x) := φ.to_ring_hom.map_sum f s lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) : φ (f.sum g) = f.sum (λ i a, φ (g i a)) := φ.map_sum _ _ @[simp] lemma map_nat_cast (n : ℕ) : φ n = n := φ.to_ring_hom.map_nat_cast n @[simp] lemma map_bit0 (x) : φ (bit0 x) = bit0 (φ x) := φ.to_ring_hom.map_bit0 x @[simp] lemma map_bit1 (x) : φ (bit1 x) = bit1 (φ x) := φ.to_ring_hom.map_bit1 x /-- If a `ring_hom` is `R`-linear, then it is an `alg_hom`. -/ def mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : A →ₐ[R] B := { to_fun := f, commutes' := λ c, by simp only [algebra.algebra_map_eq_smul_one, h, f.map_one], .. f } @[simp] lemma coe_mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : ⇑(mk' f h) = f := rfl section variables (R A) /-- Identity map as an `alg_hom`. -/ protected def id : A →ₐ[R] A := { commutes' := λ _, rfl, ..ring_hom.id A } end @[simp] lemma id_apply (p : A) : alg_hom.id R A p = p := rfl /-- Composition of algebra homeomorphisms. -/ def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ[R] C := { commutes' := λ r : R, by rw [← φ₁.commutes, ← φ₂.commutes]; refl, .. φ₁.to_ring_hom.comp ↑φ₂ } @[simp] lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) : φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl @[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ := ext $ λ x, rfl @[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ := ext $ λ x, rfl theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) : (φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) := ext $ λ x, rfl /-- R-Alg ⥤ R-Mod -/ def to_linear_map : A →ₗ B := { to_fun := φ, map_add' := φ.map_add, map_smul' := φ.map_smul } @[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl theorem to_linear_map_inj {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁.to_linear_map = φ₂.to_linear_map) : φ₁ = φ₂ := ext $ λ x, show φ₁.to_linear_map x = φ₂.to_linear_map x, by rw H @[simp] lemma comp_to_linear_map (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] [comm_semiring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) lemma map_prod {ι : Type*} (f : ι → A) (s : finset ι) : φ (∏ x in s, f x) = ∏ x in s, φ (f x) := φ.to_ring_hom.map_prod f s lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) : φ (f.prod g) = f.prod (λ i a, φ (g i a)) := φ.map_prod _ _ end comm_semiring section ring variables [comm_semiring R] [ring A] [ring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) @[simp] lemma map_neg (x) : φ (-x) = -φ x := φ.to_ring_hom.map_neg x @[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y := φ.to_ring_hom.map_sub x y @[simp] lemma map_int_cast (n : ℤ) : φ n = n := φ.to_ring_hom.map_int_cast n end ring section division_ring variables [comm_ring R] [division_ring A] [division_ring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) @[simp] lemma map_inv (x) : φ (x⁻¹) = (φ x)⁻¹ := φ.to_ring_hom.map_inv x @[simp] lemma map_div (x y) : φ (x / y) = φ x / φ y := φ.to_ring_hom.map_div x y end division_ring theorem injective_iff {R A B : Type*} [comm_semiring R] [ring A] [semiring B] [algebra R A] [algebra R B] (f : A →ₐ[R] B) : function.injective f ↔ (∀ x, f x = 0 → x = 0) := ring_hom.injective_iff (f : A →+* B) end alg_hom set_option old_structure_cmd true /-- An equivalence of algebras is an equivalence of rings commuting with the actions of scalars. -/ structure alg_equiv (R : Type u) (A : Type v) (B : Type w) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends A ≃ B, A ≃* B, A ≃+ B, A ≃+* B := (commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r) attribute [nolint doc_blame] alg_equiv.to_ring_equiv attribute [nolint doc_blame] alg_equiv.to_equiv attribute [nolint doc_blame] alg_equiv.to_add_equiv attribute [nolint doc_blame] alg_equiv.to_mul_equiv notation A ` ≃ₐ[`:50 R `] ` A' := alg_equiv R A A' namespace alg_equiv variables {R : Type u} {A₁ : Type v} {A₂ : Type w} {A₃ : Type u₁} section semiring variables [comm_semiring R] [semiring A₁] [semiring A₂] [semiring A₃] variables [algebra R A₁] [algebra R A₂] [algebra R A₃] variables (e : A₁ ≃ₐ[R] A₂) instance : has_coe_to_fun (A₁ ≃ₐ[R] A₂) := ⟨_, alg_equiv.to_fun⟩ @[ext] lemma ext {f g : A₁ ≃ₐ[R] A₂} (h : ∀ a, f a = g a) : f = g := begin have h₁ : f.to_equiv = g.to_equiv := equiv.ext h, cases f, cases g, congr, { exact (funext h) }, { exact congr_arg equiv.inv_fun h₁ } end protected lemma congr_arg {f : A₁ ≃ₐ[R] A₂} : Π {x x' : A₁}, x = x' → f x = f x' | _ _ rfl := rfl protected lemma congr_fun {f g : A₁ ≃ₐ[R] A₂} (h : f = g) (x : A₁) : f x = g x := h ▸ rfl lemma ext_iff {f g : A₁ ≃ₐ[R] A₂} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, ext⟩ lemma coe_fun_injective : @function.injective (A₁ ≃ₐ[R] A₂) (A₁ → A₂) (λ e, (e : A₁ → A₂)) := begin intros f g w, ext, exact congr_fun w a, end instance has_coe_to_ring_equiv : has_coe (A₁ ≃ₐ[R] A₂) (A₁ ≃+* A₂) := ⟨alg_equiv.to_ring_equiv⟩ @[simp] lemma mk_apply {to_fun inv_fun left_inv right_inv map_mul map_add commutes a} : (⟨to_fun, inv_fun, left_inv, right_inv, map_mul, map_add, commutes⟩ : A₁ ≃ₐ[R] A₂) a = to_fun a := rfl @[simp] lemma to_fun_apply {e : A₁ ≃ₐ[R] A₂} {a : A₁} : e.to_fun a = e a := rfl @[simp, norm_cast] lemma coe_ring_equiv : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl lemma coe_ring_equiv_injective : function.injective (λ e : A₁ ≃ₐ[R] A₂, (e : A₁ ≃+* A₂)) := begin intros f g w, ext, replace w : ((f : A₁ ≃+* A₂) : A₁ → A₂) = ((g : A₁ ≃+* A₂) : A₁ → A₂) := congr_arg (λ e : A₁ ≃+* A₂, (e : A₁ → A₂)) w, exact congr_fun w a, end @[simp] lemma map_add : ∀ x y, e (x + y) = e x + e y := e.to_add_equiv.map_add @[simp] lemma map_zero : e 0 = 0 := e.to_add_equiv.map_zero @[simp] lemma map_mul : ∀ x y, e (x * y) = (e x) * (e y) := e.to_mul_equiv.map_mul @[simp] lemma map_one : e 1 = 1 := e.to_mul_equiv.map_one @[simp] lemma commutes : ∀ (r : R), e (algebra_map R A₁ r) = algebra_map R A₂ r := e.commutes' lemma map_sum {ι : Type*} (f : ι → A₁) (s : finset ι) : e (∑ x in s, f x) = ∑ x in s, e (f x) := e.to_add_equiv.map_sum f s lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.sum g) = f.sum (λ i b, e (g i b)) := e.map_sum _ _ /-- Interpret an algebra equivalence as an algebra homomorphism. This definition is included for symmetry with the other `to_*_hom` projections. The `simp` normal form is to use the coercion of the `has_coe_to_alg_hom` instance. -/ def to_alg_hom : A₁ →ₐ[R] A₂ := { map_one' := e.map_one, map_zero' := e.map_zero, ..e } instance has_coe_to_alg_hom : has_coe (A₁ ≃ₐ[R] A₂) (A₁ →ₐ[R] A₂) := ⟨to_alg_hom⟩ @[simp] lemma to_alg_hom_eq_coe : e.to_alg_hom = e := rfl @[simp, norm_cast] lemma coe_alg_hom : ((e : A₁ →ₐ[R] A₂) : A₁ → A₂) = e := rfl @[simp] lemma map_pow : ∀ (x : A₁) (n : ℕ), e (x ^ n) = (e x) ^ n := e.to_alg_hom.map_pow lemma injective : function.injective e := e.to_equiv.injective lemma surjective : function.surjective e := e.to_equiv.surjective lemma bijective : function.bijective e := e.to_equiv.bijective instance : has_one (A₁ ≃ₐ[R] A₁) := ⟨{commutes' := λ r, rfl, ..(1 : A₁ ≃+* A₁)}⟩ instance : inhabited (A₁ ≃ₐ[R] A₁) := ⟨1⟩ /-- Algebra equivalences are reflexive. -/ @[refl] def refl : A₁ ≃ₐ[R] A₁ := 1 @[simp] lemma coe_refl : (@refl R A₁ _ _ _ : A₁ →ₐ[R] A₁) = alg_hom.id R A₁ := alg_hom.ext (λ x, rfl) /-- Algebra equivalences are symmetric. -/ @[symm] def symm (e : A₁ ≃ₐ[R] A₂) : A₂ ≃ₐ[R] A₁ := { commutes' := λ r, by { rw ←e.to_ring_equiv.symm_apply_apply (algebra_map R A₁ r), congr, change _ = e _, rw e.commutes, }, ..e.to_ring_equiv.symm, } /-- See Note [custom simps projection] -/ def simps.inv_fun (e : A₁ ≃ₐ[R] A₂) : A₂ → A₁ := e.symm initialize_simps_projections alg_equiv (to_fun → apply, inv_fun → symm_apply) @[simp] lemma inv_fun_eq_symm {e : A₁ ≃ₐ[R] A₂} : e.inv_fun = e.symm := rfl @[simp] lemma symm_symm {e : A₁ ≃ₐ[R] A₂} : e.symm.symm = e := by { ext, refl, } /-- Algebra equivalences are transitive. -/ @[trans] def trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : A₁ ≃ₐ[R] A₃ := { commutes' := λ r, show e₂.to_fun (e₁.to_fun _) = _, by rw [e₁.commutes', e₂.commutes'], ..(e₁.to_ring_equiv.trans e₂.to_ring_equiv), } @[simp] lemma apply_symm_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e (e.symm x) = x := e.to_equiv.apply_symm_apply @[simp] lemma symm_apply_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e.symm (e x) = x := e.to_equiv.symm_apply_apply @[simp] lemma trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₁) : (e₁.trans e₂) x = e₂ (e₁ x) := rfl @[simp] lemma comp_symm (e : A₁ ≃ₐ[R] A₂) : alg_hom.comp (e : A₁ →ₐ[R] A₂) ↑e.symm = alg_hom.id R A₂ := by { ext, simp } @[simp] lemma symm_comp (e : A₁ ≃ₐ[R] A₂) : alg_hom.comp ↑e.symm (e : A₁ →ₐ[R] A₂) = alg_hom.id R A₁ := by { ext, simp } /-- If `A₁` is equivalent to `A₁'` and `A₂` is equivalent to `A₂'`, then the type of maps `A₁ →ₐ[R] A₂` is equivalent to the type of maps `A₁' →ₐ[R] A₂'`. -/ def arrow_congr {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (A₁ →ₐ[R] A₂) ≃ (A₁' →ₐ[R] A₂') := { to_fun := λ f, (e₂.to_alg_hom.comp f).comp e₁.symm.to_alg_hom, inv_fun := λ f, (e₂.symm.to_alg_hom.comp f).comp e₁.to_alg_hom, left_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, symm_comp], simp only [←alg_hom.comp_assoc, symm_comp, alg_hom.id_comp, alg_hom.comp_id] }, right_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, comp_symm], simp only [←alg_hom.comp_assoc, comp_symm, alg_hom.id_comp, alg_hom.comp_id] } } lemma arrow_congr_comp {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃'] [algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') (e₃ : A₃ ≃ₐ[R] A₃') (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₃) : arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) := by { ext, simp only [arrow_congr, equiv.coe_fn_mk, alg_hom.comp_apply], congr, exact (e₂.symm_apply_apply _).symm } @[simp] lemma arrow_congr_refl : arrow_congr alg_equiv.refl alg_equiv.refl = equiv.refl (A₁ →ₐ[R] A₂) := by { ext, refl } @[simp] lemma arrow_congr_trans {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃'] [algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₂) (e₁' : A₁' ≃ₐ[R] A₂') (e₂ : A₂ ≃ₐ[R] A₃) (e₂' : A₂' ≃ₐ[R] A₃') : arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') := by { ext, refl } @[simp] lemma arrow_congr_symm {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm := by { ext, refl } /-- If an algebra morphism has an inverse, it is a algebra isomorphism. -/ def of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ : f.comp g = alg_hom.id R A₂) (h₂ : g.comp f = alg_hom.id R A₁) : A₁ ≃ₐ[R] A₂ := { inv_fun := g, left_inv := alg_hom.ext_iff.1 h₂, right_inv := alg_hom.ext_iff.1 h₁, ..f } /-- Promotes a bijective algebra homomorphism to an algebra equivalence. -/ noncomputable def of_bijective (f : A₁ →ₐ[R] A₂) (hf : function.bijective f) : A₁ ≃ₐ[R] A₂ := { .. ring_equiv.of_bijective (f : A₁ →+* A₂) hf, .. f } /-- Forgetting the multiplicative structures, an equivalence of algebras is a linear equivalence. -/ def to_linear_equiv (e : A₁ ≃ₐ[R] A₂) : A₁ ≃ₗ[R] A₂ := { to_fun := e.to_fun, map_add' := λ x y, by simp, map_smul' := λ r x, by simp [algebra.smul_def''], inv_fun := e.symm.to_fun, left_inv := e.left_inv, right_inv := e.right_inv, } @[simp] lemma to_linear_equiv_apply (e : A₁ ≃ₐ[R] A₂) (x : A₁) : e.to_linear_equiv x = e x := rfl theorem to_linear_equiv_inj {e₁ e₂ : A₁ ≃ₐ[R] A₂} (H : e₁.to_linear_equiv = e₂.to_linear_equiv) : e₁ = e₂ := ext $ λ x, show e₁.to_linear_equiv x = e₂.to_linear_equiv x, by rw H /-- Interpret an algebra equivalence as a linear map. -/ def to_linear_map : A₁ →ₗ[R] A₂ := e.to_alg_hom.to_linear_map @[simp] lemma to_alg_hom_to_linear_map : (e : A₁ →ₐ[R] A₂).to_linear_map = e.to_linear_map := rfl @[simp] lemma to_linear_equiv_to_linear_map : e.to_linear_equiv.to_linear_map = e.to_linear_map := rfl @[simp] lemma to_linear_map_apply (x : A₁) : e.to_linear_map x = e x := rfl theorem to_linear_map_inj {e₁ e₂ : A₁ ≃ₐ[R] A₂} (H : e₁.to_linear_map = e₂.to_linear_map) : e₁ = e₂ := ext $ λ x, show e₁.to_linear_map x = e₂.to_linear_map x, by rw H @[simp] lemma trans_to_linear_map (f : A₁ ≃ₐ[R] A₂) (g : A₂ ≃ₐ[R] A₃) : (f.trans g).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl instance aut : group (A₁ ≃ₐ[R] A₁) := { mul := λ ϕ ψ, ψ.trans ϕ, mul_assoc := λ ϕ ψ χ, rfl, one := 1, one_mul := λ ϕ, by { ext, refl }, mul_one := λ ϕ, by { ext, refl }, inv := symm, mul_left_inv := λ ϕ, by { ext, exact symm_apply_apply ϕ a } } end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A₁] [comm_semiring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) lemma map_prod {ι : Type*} (f : ι → A₁) (s : finset ι) : e (∏ x in s, f x) = ∏ x in s, e (f x) := e.to_alg_hom.map_prod f s lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.prod g) = f.prod (λ i a, e (g i a)) := e.to_alg_hom.map_finsupp_prod f g end comm_semiring section ring variables [comm_ring R] [ring A₁] [ring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) @[simp] lemma map_neg (x) : e (-x) = -e x := e.to_alg_hom.map_neg x @[simp] lemma map_sub (x y) : e (x - y) = e x - e y := e.to_alg_hom.map_sub x y end ring section division_ring variables [comm_ring R] [division_ring A₁] [division_ring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) @[simp] lemma map_inv (x) : e (x⁻¹) = (e x)⁻¹ := e.to_alg_hom.map_inv x @[simp] lemma map_div (x y) : e (x / y) = e x / e y := e.to_alg_hom.map_div x y end division_ring end alg_equiv namespace matrix /-! ### `matrix` section Specialize `matrix.one_map` and `matrix.zero_map` to `alg_hom` and `alg_equiv`. TODO: there should be a way to avoid restating these for each `foo_hom`. -/ variables {R A₁ A₂ n : Type*} [fintype n] section semiring variables [comm_semiring R] [semiring A₁] [algebra R A₁] [semiring A₂] [algebra R A₂] /-- A version of `matrix.one_map` where `f` is an `alg_hom`. -/ @[simp] lemma alg_hom_map_one [decidable_eq n] (f : A₁ →ₐ[R] A₂) : (1 : matrix n n A₁).map f = 1 := one_map f.map_zero f.map_one /-- A version of `matrix.one_map` where `f` is an `alg_equiv`. -/ @[simp] lemma alg_equiv_map_one [decidable_eq n] (f : A₁ ≃ₐ[R] A₂) : (1 : matrix n n A₁).map f = 1 := one_map f.map_zero f.map_one /-- A version of `matrix.zero_map` where `f` is an `alg_hom`. -/ @[simp] lemma alg_hom_map_zero (f : A₁ →ₐ[R] A₂) : (0 : matrix n n A₁).map f = 0 := map_zero f.map_zero /-- A version of `matrix.zero_map` where `f` is an `alg_equiv`. -/ @[simp] lemma alg_equiv_map_zero (f : A₁ ≃ₐ[R] A₂) : (0 : matrix n n A₁).map f = 0 := map_zero f.map_zero end semiring end matrix namespace algebra variables (R : Type u) (S : Type v) (A : Type w) include R S A /-- `comap R S A` is a type alias for `A`, and has an R-algebra structure defined on it when `algebra R S` and `algebra S A`. If `S` is an `R`-algebra and `A` is an `S`-algebra then `algebra.comap.algebra R S A` can be used to provide `A` with a structure of an `R`-algebra. Other than that, `algebra.comap` is now deprecated and replaced with `is_scalar_tower`. -/ /- This is done to avoid a type class search with meta-variables `algebra R ?m_1` and `algebra ?m_1 A -/ /- The `nolint` attribute is added because it has unused arguments `R` and `S`, but these are necessary for synthesizing the appropriate type classes -/ @[nolint unused_arguments] def comap : Type w := A instance comap.inhabited [h : inhabited A] : inhabited (comap R S A) := h instance comap.semiring [h : semiring A] : semiring (comap R S A) := h instance comap.ring [h : ring A] : ring (comap R S A) := h instance comap.comm_semiring [h : comm_semiring A] : comm_semiring (comap R S A) := h instance comap.comm_ring [h : comm_ring A] : comm_ring (comap R S A) := h instance comap.algebra' [comm_semiring S] [semiring A] [h : algebra S A] : algebra S (comap R S A) := h /-- Identity homomorphism `A →ₐ[S] comap R S A`. -/ def comap.to_comap [comm_semiring S] [semiring A] [algebra S A] : A →ₐ[S] comap R S A := alg_hom.id S A /-- Identity homomorphism `comap R S A →ₐ[S] A`. -/ def comap.of_comap [comm_semiring S] [semiring A] [algebra S A] : comap R S A →ₐ[S] A := alg_hom.id S A variables [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A] /-- `R ⟶ S` induces `S-Alg ⥤ R-Alg` -/ instance comap.algebra : algebra R (comap R S A) := { smul := λ r x, (algebra_map R S r • x : A), commutes' := λ r x, algebra.commutes _ _, smul_def' := λ _ _, algebra.smul_def _ _, .. (algebra_map S A).comp (algebra_map R S) } /-- Embedding of `S` into `comap R S A`. -/ def to_comap : S →ₐ[R] comap R S A := { commutes' := λ r, rfl, .. algebra_map S A } theorem to_comap_apply (x) : to_comap R S A x = algebra_map S A x := rfl end algebra namespace alg_hom variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁} variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] (φ : A →ₐ[S] B) include R /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ def comap : algebra.comap R S A →ₐ[R] algebra.comap R S B := { commutes' := λ r, φ.commutes (algebra_map R S r) ..φ } end alg_hom namespace ring_hom variables {R S : Type*} /-- Reinterpret a `ring_hom` as an `ℕ`-algebra homomorphism. -/ def to_nat_alg_hom [semiring R] [semiring S] [algebra ℕ R] [algebra ℕ S] (f : R →+* S) : R →ₐ[ℕ] S := { to_fun := f, commutes' := λ n, by simp, .. f } /-- Reinterpret a `ring_hom` as a `ℤ`-algebra homomorphism. -/ def to_int_alg_hom [ring R] [ring S] [algebra ℤ R] [algebra ℤ S] (f : R →+* S) : R →ₐ[ℤ] S := { commutes' := λ n, by simp, .. f } @[simp] lemma map_rat_algebra_map [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S) (r : ℚ) : f (algebra_map ℚ R r) = algebra_map ℚ S r := ring_hom.ext_iff.1 (subsingleton.elim (f.comp (algebra_map ℚ R)) (algebra_map ℚ S)) r /-- Reinterpret a `ring_hom` as a `ℚ`-algebra homomorphism. -/ def to_rat_alg_hom [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S) : R →ₐ[ℚ] S := { commutes' := f.map_rat_algebra_map, .. f } end ring_hom namespace rat instance algebra_rat {α} [division_ring α] [char_zero α] : algebra ℚ α := (rat.cast_hom α).to_algebra' $ λ r x, r.cast_commute x @[simp] theorem algebra_map_rat_rat : algebra_map ℚ ℚ = ring_hom.id ℚ := subsingleton.elim _ _ -- TODO[gh-6025]: make this an instance once safe to do so lemma algebra_rat_subsingleton {α} [semiring α] : subsingleton (algebra ℚ α) := ⟨λ x y, algebra.algebra_ext x y $ ring_hom.congr_fun $ subsingleton.elim _ _⟩ end rat namespace algebra open module variables (R : Type u) (A : Type v) variables [comm_semiring R] [semiring A] [algebra R A] /-- `algebra_map` as an `alg_hom`. -/ def of_id : R →ₐ[R] A := { commutes' := λ _, rfl, .. algebra_map R A } variables {R} theorem of_id_apply (r) : of_id R A r = algebra_map R A r := rfl variables (R A) /-- The multiplication in an algebra is a bilinear map. -/ def lmul : A →ₐ[R] (End R A) := { map_one' := by { ext a, exact one_mul a }, map_mul' := by { intros a b, ext c, exact mul_assoc a b c }, map_zero' := by { ext a, exact zero_mul a }, commutes' := by { intro r, ext a, dsimp, rw [smul_def] }, .. (show A →ₗ[R] A →ₗ[R] A, from linear_map.mk₂ R (*) (λ x y z, add_mul x y z) (λ c x y, by rw [smul_def, smul_def, mul_assoc _ x y]) (λ x y z, mul_add x y z) (λ c x y, by rw [smul_def, smul_def, left_comm])) } variables {A} /-- The multiplication on the left in an algebra is a linear map. -/ def lmul_left (r : A) : A →ₗ A := lmul R A r /-- The multiplication on the right in an algebra is a linear map. -/ def lmul_right (r : A) : A →ₗ A := (lmul R A).to_linear_map.flip r /-- Simultaneous multiplication on the left and right is a linear map. -/ def lmul_left_right (vw: A × A) : A →ₗ[R] A := (lmul_right R vw.2).comp (lmul_left R vw.1) /-- The multiplication map on an algebra, as an `R`-linear map from `A ⊗[R] A` to `A`. -/ def lmul' : A ⊗[R] A →ₗ[R] A := tensor_product.lift (lmul R A).to_linear_map variables {R A} @[simp] lemma lmul_apply (p q : A) : lmul R A p q = p * q := rfl @[simp] lemma lmul_left_apply (p q : A) : lmul_left R p q = p * q := rfl @[simp] lemma lmul_right_apply (p q : A) : lmul_right R p q = q * p := rfl @[simp] lemma lmul_left_right_apply (vw : A × A) (p : A) : lmul_left_right R vw p = vw.1 * p * vw.2 := rfl @[simp] lemma lmul_left_one : lmul_left R (1:A) = linear_map.id := by { ext, simp only [linear_map.id_coe, one_mul, id.def, lmul_left_apply] } @[simp] lemma lmul_left_mul (a b : A) : lmul_left R (a * b) = (lmul_left R a).comp (lmul_left R b) := by { ext, simp only [lmul_left_apply, linear_map.comp_apply, mul_assoc] } @[simp] lemma lmul_right_one : lmul_right R (1:A) = linear_map.id := by { ext, simp only [linear_map.id_coe, mul_one, id.def, lmul_right_apply] } @[simp] lemma lmul_right_mul (a b : A) : lmul_right R (a * b) = (lmul_right R b).comp (lmul_right R a) := by { ext, simp only [lmul_right_apply, linear_map.comp_apply, mul_assoc] } @[simp] lemma lmul'_apply {x y : A} : lmul' R (x ⊗ₜ y) = x * y := by simp only [algebra.lmul', tensor_product.lift.tmul, alg_hom.to_linear_map_apply, lmul_apply] instance linear_map.semimodule' (R : Type u) [comm_semiring R] (M : Type v) [add_comm_monoid M] [semimodule R M] (S : Type w) [comm_semiring S] [algebra R S] : semimodule S (M →ₗ[R] S) := { smul := λ s f, linear_map.llcomp _ _ _ _ (algebra.lmul R S s) f, one_smul := λ f, linear_map.ext $ λ x, one_mul _, mul_smul := λ s₁ s₂ f, linear_map.ext $ λ x, mul_assoc _ _ _, smul_add := λ s f g, linear_map.map_add _ _ _, smul_zero := λ s, linear_map.map_zero _, add_smul := λ s₁ s₂ f, linear_map.ext $ λ x, add_mul _ _ _, zero_smul := λ f, linear_map.ext $ λ x, zero_mul _ } end algebra section nat variables (R : Type*) [semiring R] /-- Semiring ⥤ ℕ-Alg -/ instance algebra_nat : algebra ℕ R := { commutes' := nat.cast_commute, smul_def' := λ _ _, nsmul_eq_mul _ _, to_ring_hom := nat.cast_ring_hom R } section span_nat open submodule lemma span_nat_eq_add_group_closure (s : set R) : (span ℕ s).to_add_submonoid = add_submonoid.closure s := eq.symm $ add_submonoid.closure_eq_of_le subset_span $ λ x hx, span_induction hx (λ x hx, add_submonoid.subset_closure hx) (add_submonoid.zero_mem _) (λ _ _, add_submonoid.add_mem _) (λ _ _ _, add_submonoid.nsmul_mem _ ‹_› _) @[simp] lemma span_nat_eq (s : add_submonoid R) : (span ℕ (s : set R)).to_add_submonoid = s := by rw [span_nat_eq_add_group_closure, s.closure_eq] end span_nat end nat section int variables (R : Type*) [ring R] /-- Ring ⥤ ℤ-Alg -/ instance algebra_int : algebra ℤ R := { commutes' := int.cast_commute, smul_def' := λ _ _, gsmul_eq_mul _ _, to_ring_hom := int.cast_ring_hom R } variables {R} section variables {S : Type*} [ring S] instance int_algebra_subsingleton : subsingleton (algebra ℤ S) := ⟨λ P Q, by { ext, simp, }⟩ end section variables {S : Type*} [semiring S] instance nat_algebra_subsingleton : subsingleton (algebra ℕ S) := ⟨λ P Q, by { ext, simp, }⟩ end section span_int open submodule lemma span_int_eq_add_group_closure (s : set R) : (span ℤ s).to_add_subgroup = add_subgroup.closure s := eq.symm $ add_subgroup.closure_eq_of_le _ subset_span $ λ x hx, span_induction hx (λ x hx, add_subgroup.subset_closure hx) (add_subgroup.zero_mem _) (λ _ _, add_subgroup.add_mem _) (λ _ _ _, add_subgroup.gsmul_mem _ ‹_› _) @[simp] lemma span_int_eq (s : add_subgroup R) : (span ℤ (s : set R)).to_add_subgroup = s := by rw [span_int_eq_add_group_closure, s.closure_eq] end span_int end int /-! The R-algebra structure on `Π i : I, A i` when each `A i` is an R-algebra. We couldn't set this up back in `algebra.pi_instances` because this file imports it. -/ namespace pi variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) variables (I f) instance algebra (α) {r : comm_semiring α} [s : ∀ i, semiring (f i)] [∀ i, algebra α (f i)] : algebra α (Π i : I, f i) := { commutes' := λ a f, begin ext, simp [algebra.commutes], end, smul_def' := λ a f, begin ext, simp [algebra.smul_def''], end, ..pi.ring_hom (λ i, algebra_map α (f i)) } @[simp] lemma algebra_map_apply (α) {r : comm_semiring α} [s : ∀ i, semiring (f i)] [∀ i, algebra α (f i)] (a : α) (i : I) : algebra_map α (Π i, f i) a i = algebra_map α (f i) a := rfl -- One could also build a `Π i, R i`-algebra structure on `Π i, A i`, -- when each `A i` is an `R i`-algebra, although I'm not sure that it's useful. end pi section is_scalar_tower variables {R : Type*} [comm_semiring R] variables (A : Type*) [semiring A] [algebra R A] variables {M : Type*} [add_comm_monoid M] [semimodule A M] [semimodule R M] [is_scalar_tower R A M] variables {N : Type*} [add_comm_monoid N] [semimodule A N] [semimodule R N] [is_scalar_tower R A N] lemma algebra_compatible_smul (r : R) (m : M) : r • m = ((algebra_map R A) r) • m := by rw [←(one_smul A m), ←smul_assoc, algebra.smul_def, mul_one, one_smul] @[simp] lemma algebra_map_smul (r : R) (m : M) : ((algebra_map R A) r) • m = r • m := (algebra_compatible_smul A r m).symm variable {A} @[priority 100] -- see Note [lower instance priority] instance is_scalar_tower.to_smul_comm_class : smul_comm_class R A M := ⟨λ r a m, by rw [algebra_compatible_smul A r (a • m), smul_smul, algebra.commutes, mul_smul, ←algebra_compatible_smul]⟩ @[priority 100] -- see Note [lower instance priority] instance is_scalar_tower.to_smul_comm_class' : smul_comm_class A R M := smul_comm_class.symm _ _ _ lemma smul_algebra_smul_comm (r : R) (a : A) (m : M) : a • r • m = r • a • m := smul_comm _ _ _ namespace linear_map instance coe_is_scalar_tower : has_coe (M →ₗ[A] N) (M →ₗ[R] N) := ⟨restrict_scalars R⟩ variables (R) {A M N} @[simp, norm_cast squash] lemma coe_restrict_scalars_eq_coe (f : M →ₗ[A] N) : (f.restrict_scalars R : M → N) = f := rfl @[simp, norm_cast squash] lemma coe_coe_is_scalar_tower (f : M →ₗ[A] N) : ((f : M →ₗ[R] N) : M → N) = f := rfl /-- `A`-linearly coerce a `R`-linear map from `M` to `A` to a function, given an algebra `A` over a commutative semiring `R` and `M` a semimodule over `R`. -/ def lto_fun (R : Type u) (M : Type v) (A : Type w) [comm_semiring R] [add_comm_monoid M] [semimodule R M] [comm_ring A] [algebra R A] : (M →ₗ[R] A) →ₗ[A] (M → A) := { to_fun := linear_map.to_fun, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } end linear_map end is_scalar_tower section restrict_scalars /- In this section, we describe restriction of scalars: if `S` is an algebra over `R`, then `S`-modules are also `R`-modules. -/ section type_synonym variables (R A M : Type*) /-- Warning: use this type synonym judiciously! The preferred way of working with an `A`-module `M` as `R`-module (where `A` is an `R`-algebra), is by `[module R M] [module A M] [is_scalar_tower R A M]`. When `M` is a module over a ring `A`, and `A` is an algebra over `R`, then `M` inherits a module structure over `R`, provided as a type synonym `module.restrict_scalars R A M := M`. -/ @[nolint unused_arguments] def restrict_scalars (R A M : Type*) : Type* := M instance [I : inhabited M] : inhabited (restrict_scalars R A M) := I instance [I : add_comm_monoid M] : add_comm_monoid (restrict_scalars R A M) := I instance [I : add_comm_group M] : add_comm_group (restrict_scalars R A M) := I instance restrict_scalars.module_orig [semiring A] [add_comm_monoid M] [I : semimodule A M] : semimodule A (restrict_scalars R A M) := I variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [semimodule A M] /-- When `M` is a module over a ring `A`, and `A` is an algebra over `R`, then `M` inherits a module structure over `R`. The preferred way of setting this up is `[module R M] [module A M] [is_scalar_tower R A M]`. -/ instance : semimodule R (restrict_scalars R A M) := { smul := λ c x, (algebra_map R A c) • x, one_smul := by simp, mul_smul := by simp [mul_smul], smul_add := by simp [smul_add], smul_zero := by simp [smul_zero], add_smul := by simp [add_smul], zero_smul := by simp [zero_smul] } lemma restrict_scalars_smul_def (c : R) (x : restrict_scalars R A M) : c • x = ((algebra_map R A c) • x : M) := rfl instance : is_scalar_tower R A (restrict_scalars R A M) := ⟨λ r A M, by { rw [algebra.smul_def, mul_smul], refl }⟩ instance submodule.restricted_module (V : submodule A M) : semimodule R V := restrict_scalars.semimodule R A V instance submodule.restricted_module_is_scalar_tower (V : submodule A M) : is_scalar_tower R A V := restrict_scalars.is_scalar_tower R A V end type_synonym section semimodule open semimodule variables (R A M N : Type*) [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [semimodule R M] [semimodule A M] [is_scalar_tower R A M] variables [add_comm_monoid N] [semimodule R N] [semimodule A N] [is_scalar_tower R A N] variables {A M N} namespace submodule /-- `V.restrict_scalars R` is the `R`-submodule of the `R`-module given by restriction of scalars, corresponding to `V`, an `S`-submodule of the original `S`-module. -/ @[simps] def restrict_scalars (V : submodule A M) : submodule R M := { carrier := V.carrier, zero_mem' := V.zero_mem, smul_mem' := λ c m h, by { rw algebra_compatible_smul A c m, exact V.smul_mem _ h }, add_mem' := λ x y hx hy, V.add_mem hx hy } @[simp] lemma restrict_scalars_mem (V : submodule A M) (m : M) : m ∈ V.restrict_scalars R ↔ m ∈ V := iff.refl _ variables (R A M) lemma restrict_scalars_injective : function.injective (restrict_scalars R : submodule A M → submodule R M) := λ V₁ V₂ h, ext $ by convert set.ext_iff.1 (ext'_iff.1 h); refl @[simp] lemma restrict_scalars_inj {V₁ V₂ : submodule A M} : restrict_scalars R V₁ = restrict_scalars R V₂ ↔ V₁ = V₂ := ⟨λ h, restrict_scalars_injective R _ _ h, congr_arg _⟩ @[simp] lemma restrict_scalars_bot : restrict_scalars R (⊥ : submodule A M) = ⊥ := rfl @[simp] lemma restrict_scalars_top : restrict_scalars R (⊤ : submodule A M) = ⊤ := rfl /-- If `A` is an `R`-algebra, then the `R`-module generated by a set `X` is included in the `A`-module generated by `X`. -/ lemma span_le_restrict_scalars (X : set M) : span R (X : set M) ≤ restrict_scalars R (span A X) := submodule.span_le.mpr submodule.subset_span /-- If `A` is an `R`-algebra such that the induced morhpsim `R →+* A` is surjective, then the `R`-module generated by a set `X` equals the `A`-module generated by `X`. -/ lemma span_eq_restrict_scalars (X : set M) (hsur : function.surjective (algebra_map R A)) : span R X = restrict_scalars R (span A X) := begin apply (span_le_restrict_scalars R A M X).antisymm (λ m hm, _), refine span_induction hm subset_span (zero_mem _) (λ _ _, add_mem _) (λ a m hm, _), obtain ⟨r, rfl⟩ := hsur a, simpa [algebra_map_smul] using smul_mem _ r hm end end submodule @[simp] lemma linear_map.ker_restrict_scalars (f : M →ₗ[A] N) : (f.restrict_scalars R).ker = submodule.restrict_scalars R f.ker := rfl end semimodule end restrict_scalars namespace linear_map variables (R : Type*) [comm_semiring R] (S : Type*) [semiring S] [algebra R S] (V : Type*) [add_comm_monoid V] [semimodule R V] (W : Type*) [add_comm_monoid W] [semimodule R W] [semimodule S W] [is_scalar_tower R S W] instance is_scalar_tower_extend_scalars : is_scalar_tower R S (V →ₗ[R] W) := { smul_assoc := λ r s f, by simp only [(•), coe_mk, smul_assoc] } end linear_map
ec0a27a89735a858ef3325898c8fb29459a236ef
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/int/gcd.lean
ac2514652ce40d964b3839410942e8706b8d180e
[ "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
5,391
lean
/- Copyright (c) 2018 Guy Leroy. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sangwoo Jo (aka Jason), Guy Leroy, Johannes Hölzl, Mario Carneiro -/ import data.nat.prime /-! # Extended GCD and divisibility over ℤ ## Main definitions * Given `x y : ℕ`, `xgcd x y` computes the pair of integers `(a, b)` such that `gcd x y = x * a + y * b`. `gcd_a x y` and `gcd_b x y` are defined to be `a` and `b`, respectively. ## Main statements * `gcd_eq_gcd_ab`: Bézout's lemma, given `x y : ℕ`, `gcd x y = x * gcd_a x y + y * gcd_b x y`. -/ /-! ### Extended Euclidean algorithm -/ namespace nat /-- Helper function for the extended GCD algorithm (`nat.xgcd`). -/ def xgcd_aux : ℕ → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ | 0 s t r' s' t' := (r', s', t') | r@(succ _) s t r' s' t' := have r' % r < r, from mod_lt _ $ succ_pos _, let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t @[simp] theorem xgcd_zero_left {s t r' s' t'} : xgcd_aux 0 s t r' s' t' = (r', s', t') := by simp [xgcd_aux] theorem xgcd_aux_rec {r s t r' s' t'} (h : 0 < r) : xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t := by cases r; [exact absurd h (lt_irrefl _), {simp only [xgcd_aux], refl}] /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd (x y : ℕ) : ℤ × ℤ := (xgcd_aux x 1 0 y 0 1).2 /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_a (x y : ℕ) : ℤ := (xgcd x y).1 /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b (x y : ℕ) : ℤ := (xgcd x y).2 @[simp] theorem xgcd_aux_fst (x y) : ∀ s t s' t', (xgcd_aux x s t y s' t').1 = gcd x y := gcd.induction x y (by simp) (λ x y h IH s t s' t', by simp [xgcd_aux_rec, h, IH]; rw ← gcd_rec) theorem xgcd_aux_val (x y) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) := by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1]; cases xgcd_aux x 1 0 y 0 1; refl theorem xgcd_val (x y) : xgcd x y = (gcd_a x y, gcd_b x y) := by unfold gcd_a gcd_b; cases xgcd x y; refl section parameters (x y : ℕ) private def P : ℕ × ℤ × ℤ → Prop | (r, s, t) := (r : ℤ) = x * s + y * t theorem xgcd_aux_P {r r'} : ∀ {s t s' t'}, P (r, s, t) → P (r', s', t') → P (xgcd_aux r s t r' s' t') := gcd.induction r r' (by simp) $ λ a b h IH s t s' t' p p', begin rw [xgcd_aux_rec h], refine IH _ p, dsimp [P] at *, rw [int.mod_def], generalize : (b / a : ℤ) = k, rw [p, p'], simp [mul_add, mul_comm, mul_left_comm, add_comm, add_left_comm, sub_eq_neg_add, mul_assoc] end /-- Bézout's lemma: given `x y : ℕ`, `gcd x y = x * a + y * b`, where `a = gcd_a x y` and `b = gcd_b x y` are computed by the extended Euclidean algorithm. -/ theorem gcd_eq_gcd_ab : (gcd x y : ℤ) = x * gcd_a x y + y * gcd_b x y := by have := @xgcd_aux_P x y x y 1 0 0 1 (by simp [P]) (by simp [P]); rwa [xgcd_aux_val, xgcd_val] at this end end nat /-! ### Divisibility over ℤ -/ namespace int theorem nat_abs_div (a b : ℤ) (H : b ∣ a) : nat_abs (a / b) = (nat_abs a) / (nat_abs b) := begin cases (nat.eq_zero_or_pos (nat_abs b)), {rw eq_zero_of_nat_abs_eq_zero h, simp [int.div_zero]}, calc nat_abs (a / b) = nat_abs (a / b) * 1 : by rw mul_one ... = nat_abs (a / b) * (nat_abs b / nat_abs b) : by rw nat.div_self h ... = nat_abs (a / b) * nat_abs b / nat_abs b : by rw (nat.mul_div_assoc _ (dvd_refl _)) ... = nat_abs (a / b * b) / nat_abs b : by rw (nat_abs_mul (a / b) b) ... = nat_abs a / nat_abs b : by rw int.div_mul_cancel H, end theorem nat_abs_dvd_abs_iff {i j : ℤ} : i.nat_abs ∣ j.nat_abs ↔ i ∣ j := ⟨assume (H : i.nat_abs ∣ j.nat_abs), dvd_nat_abs.mp (nat_abs_dvd.mp (coe_nat_dvd.mpr H)), assume H : (i ∣ j), coe_nat_dvd.mp (dvd_nat_abs.mpr (nat_abs_dvd.mpr H))⟩ lemma succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul {p : ℕ} (p_prime : nat.prime p) {m n : ℤ} {k l : ℕ} (hpm : ↑(p ^ k) ∣ m) (hpn : ↑(p ^ l) ∣ n) (hpmn : ↑(p ^ (k+l+1)) ∣ m*n) : ↑(p ^ (k+1)) ∣ m ∨ ↑(p ^ (l+1)) ∣ n := have hpm' : p ^ k ∣ m.nat_abs, from int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpm, have hpn' : p ^ l ∣ n.nat_abs, from int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpn, have hpmn' : (p ^ (k+l+1)) ∣ m.nat_abs*n.nat_abs, by rw ←int.nat_abs_mul; apply (int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpmn), let hsd := nat.succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul p_prime hpm' hpn' hpmn' in hsd.elim (λ hsd1, or.inl begin apply int.dvd_nat_abs.1, apply int.coe_nat_dvd.2 hsd1 end) (λ hsd2, or.inr begin apply int.dvd_nat_abs.1, apply int.coe_nat_dvd.2 hsd2 end) theorem dvd_of_mul_dvd_mul_left {i j k : ℤ} (k_non_zero : k ≠ 0) (H : k * i ∣ k * j) : i ∣ j := dvd.elim H (λl H1, by rw mul_assoc at H1; exact ⟨_, mul_left_cancel' k_non_zero H1⟩) theorem dvd_of_mul_dvd_mul_right {i j k : ℤ} (k_non_zero : k ≠ 0) (H : i * k ∣ j * k) : i ∣ j := by rw [mul_comm i k, mul_comm j k] at H; exact dvd_of_mul_dvd_mul_left k_non_zero H lemma prime.dvd_nat_abs_of_coe_dvd_pow_two {p : ℕ} (hp : p.prime) (k : ℤ) (h : ↑p ∣ k ^ 2) : p ∣ k.nat_abs := begin apply @nat.prime.dvd_of_dvd_pow _ _ 2 hp, rwa [pow_two, ← nat_abs_mul, ← coe_nat_dvd_left, ← pow_two] end end int
7eab59fe7ac0fd4383f1c51d8c0828b666e0a382
e38d5e91d30731bef617cc9b6de7f79c34cdce9a
/src/path/function.lean
6d80ff1581abb2bd0c5aec8e88bd05d39dcf78ac
[ "Apache-2.0" ]
permissive
bbentzen/cubicalean
55e979c303fbf55a81ac46b1000c944b2498be7a
3b94cd2aefdfc2163c263bd3fc6f2086fef814b5
refs/heads/master
1,588,314,875,258
1,554,412,699,000
1,554,412,699,000
177,333,390
0
0
null
null
null
null
UTF-8
Lean
false
false
786
lean
/- Copyright (c) 2019 Bruno Bentzen. All rights reserved. Released under the Apache License 2.0 (see "License"); Author: Bruno Bentzen -/ import ..core.path open pathd interval namespace path lemma ap {A B : Type} (f : A → B) {a b : A} : path A a b → path B (f a) (f b) := λ p, path.abs (λ i, f (p @@ i)) (by rw app0) (by rw app1) lemma apd {A : Type} {B : A → Type} {f : Π x, B x} {a b : A} : Π p : path A a b, pathd (λ i, B (p @@ i)) (f (p @@ i0)) (f (p @@ i1)) := λ p, pathd.abs (λ i, f (p @@ i)) (by rw app0) (by rw app1) theorem funext {A B : Type} {f g : A → B} : (∀ x, path B (f x) (g x)) → path (A → B) f g := λ h, path.abs (λ i x, ((h x) @@ i)) (funext (λ x, app0 (h x))) (funext (λ x, app1 (h x))) end path
12aff147f76390dc940b503f4c5fa22faf79412f
c777c32c8e484e195053731103c5e52af26a25d1
/src/data/finset/basic.lean
30687beee91e0c5058fb770dbdf22a1435120cae
[ "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
113,790
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, Minchao Wu, Mario Carneiro -/ import data.multiset.finset_ops import tactic.apply import tactic.nth_rewrite import tactic.monotonicity /-! # Finite sets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Terms of type `finset α` are one way of talking about finite subsets of `α` in mathlib. Below, `finset α` is defined as a structure with 2 fields: 1. `val` is a `multiset α` of elements; 2. `nodup` is a proof that `val` has no duplicates. Finsets in Lean are constructive in that they have an underlying `list` that enumerates their elements. In particular, any function that uses the data of the underlying list cannot depend on its ordering. This is handled on the `multiset` level by multiset API, so in most cases one needn't worry about it explicitly. Finsets give a basic foundation for defining finite sums and products over types: 1. `∑ i in (s : finset α), f i`; 2. `∏ i in (s : finset α), f i`. Lean refers to these operations as `big_operator`s. More information can be found in `algebra.big_operators.basic`. Finsets are directly used to define fintypes in Lean. A `fintype α` instance for a type `α` consists of a universal `finset α` containing every term of `α`, called `univ`. See `data.fintype.basic`. There is also `univ'`, the noncomputable partner to `univ`, which is defined to be `α` as a finset if `α` is finite, and the empty finset otherwise. See `data.fintype.basic`. `finset.card`, the size of a finset is defined in `data.finset.card`. This is then used to define `fintype.card`, the size of a type. ## Main declarations ### Main definitions * `finset`: Defines a type for the finite subsets of `α`. Constructing a `finset` requires two pieces of data: `val`, a `multiset α` of elements, and `nodup`, a proof that `val` has no duplicates. * `finset.has_mem`: Defines membership `a ∈ (s : finset α)`. * `finset.has_coe`: Provides a coercion `s : finset α` to `s : set α`. * `finset.has_coe_to_sort`: Coerce `s : finset α` to the type of all `x ∈ s`. * `finset.induction_on`: Induction on finsets. To prove a proposition about an arbitrary `finset α`, it suffices to prove it for the empty finset, and to show that if it holds for some `finset α`, then it holds for the finset obtained by inserting a new element. * `finset.choose`: Given a proof `h` of existence and uniqueness of a certain element satisfying a predicate, `choose s h` returns the element of `s` satisfying that predicate. ### Finset constructions * `singleton`: Denoted by `{a}`; the finset consisting of one element. * `finset.empty`: Denoted by `∅`. The finset associated to any type consisting of no elements. * `finset.range`: For any `n : ℕ`, `range n` is equal to `{0, 1, ... , n - 1} ⊆ ℕ`. This convention is consistent with other languages and normalizes `card (range n) = n`. Beware, `n` is not in `range n`. * `finset.attach`: Given `s : finset α`, `attach s` forms a finset of elements of the subtype `{a // a ∈ s}`; in other words, it attaches elements to a proof of membership in the set. ### Finsets from functions * `finset.filter`: Given a predicate `p : α → Prop`, `s.filter p` is the finset consisting of those elements in `s` satisfying the predicate `p`. ### The lattice structure on subsets of finsets There is a natural lattice structure on the subsets of a set. In Lean, we use lattice notation to talk about things involving unions and intersections. See `order.lattice`. For the lattice structure on finsets, `⊥` is called `bot` with `⊥ = ∅` and `⊤` is called `top` with `⊤ = univ`. * `finset.has_subset`: Lots of API about lattices, otherwise behaves exactly as one would expect. * `finset.has_union`: Defines `s ∪ t` (or `s ⊔ t`) as the union of `s` and `t`. See `finset.sup`/`finset.bUnion` for finite unions. * `finset.has_inter`: Defines `s ∩ t` (or `s ⊓ t`) as the intersection of `s` and `t`. See `finset.inf` for finite intersections. * `finset.disj_union`: Given a hypothesis `h` which states that finsets `s` and `t` are disjoint, `s.disj_union t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`; this does not require decidable equality on the type `α`. ### Operations on two or more finsets * `insert` and `finset.cons`: For any `a : α`, `insert s a` returns `s ∪ {a}`. `cons s a h` returns the same except that it requires a hypothesis stating that `a` is not already in `s`. This does not require decidable equality on the type `α`. * `finset.has_union`: see "The lattice structure on subsets of finsets" * `finset.has_inter`: see "The lattice structure on subsets of finsets" * `finset.erase`: For any `a : α`, `erase s a` returns `s` with the element `a` removed. * `finset.has_sdiff`: Defines the set difference `s \ t` for finsets `s` and `t`. * `finset.product`: Given finsets of `α` and `β`, defines finsets of `α × β`. For arbitrary dependent products, see `data.finset.pi`. * `finset.bUnion`: Finite unions of finsets; given an indexing function `f : α → finset β` and a `s : finset α`, `s.bUnion f` is the union of all finsets of the form `f a` for `a ∈ s`. * `finset.bInter`: TODO: Implemement finite intersections. ### Maps constructed using finsets * `finset.piecewise`: Given two functions `f`, `g`, `s.piecewise f g` is a function which is equal to `f` on `s` and `g` on the complement. ### Predicates on finsets * `disjoint`: defined via the lattice structure on finsets; two sets are disjoint if their intersection is empty. * `finset.nonempty`: A finset is nonempty if it has elements. This is equivalent to saying `s ≠ ∅`. TODO: Decide on the simp normal form. ### Equivalences between finsets * The `data.equiv` files describe a general type of equivalence, so look in there for any lemmas. There is some API for rewriting sums and products from `s` to `t` given that `s ≃ t`. TODO: examples ## Tags finite sets, finset -/ open multiset subtype nat function universes u variables {α : Type*} {β : Type*} {γ : Type*} /-- `finset α` is the type of finite sets of elements of `α`. It is implemented as a multiset (a list up to permutation) which has no duplicate elements. -/ structure finset (α : Type*) := (val : multiset α) (nodup : nodup val) instance multiset.can_lift_finset {α} : can_lift (multiset α) (finset α) finset.val multiset.nodup := ⟨λ m hm, ⟨⟨m, hm⟩, rfl⟩⟩ namespace finset theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t | ⟨s, _⟩ ⟨t, _⟩ rfl := rfl theorem val_injective : injective (val : finset α → multiset α) := λ _ _, eq_of_veq @[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t := val_injective.eq_iff @[simp] theorem dedup_eq_self [decidable_eq α] (s : finset α) : dedup s.1 = s.1 := s.2.dedup instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α) | s₁ s₂ := decidable_of_iff _ val_inj /-! ### membership -/ instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩ theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl @[simp] lemma mem_val {a : α} {s : finset α} : a ∈ s.1 ↔ a ∈ s := iff.rfl @[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) := multiset.decidable_mem _ _ /-! ### set coercion -/ /-- Convert a finset to a set in the natural way. -/ instance : has_coe_t (finset α) (set α) := ⟨λ s, {x | x ∈ s}⟩ @[simp, norm_cast] lemma mem_coe {a : α} {s : finset α} : a ∈ (s : set α) ↔ a ∈ s := iff.rfl @[simp] lemma set_of_mem {α} {s : finset α} : {a | a ∈ s} = s := rfl @[simp] lemma coe_mem {s : finset α} (x : (s : set α)) : ↑x ∈ s := x.2 @[simp] lemma mk_coe {s : finset α} (x : (s : set α)) {h} : (⟨x, h⟩ : (s : set α)) = x := subtype.coe_eta _ _ instance decidable_mem' [decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ (s : set α)) := s.decidable_mem _ /-! ### extensionality -/ theorem ext_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ := val_inj.symm.trans $ s₁.nodup.ext s₂.nodup @[ext] theorem ext {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := ext_iff.2 @[simp, norm_cast] theorem coe_inj {s₁ s₂ : finset α} : (s₁ : set α) = s₂ ↔ s₁ = s₂ := set.ext_iff.trans ext_iff.symm lemma coe_injective {α} : injective (coe : finset α → set α) := λ s t, coe_inj.1 /-! ### type coercion -/ /-- Coercion from a finset to the corresponding subtype. -/ instance {α : Type u} : has_coe_to_sort (finset α) (Type u) := ⟨λ s, {x // x ∈ s}⟩ @[simp] protected lemma forall_coe {α : Type*} (s : finset α) (p : s → Prop) : (∀ (x : s), p x) ↔ ∀ (x : α) (h : x ∈ s), p ⟨x, h⟩ := subtype.forall @[simp] protected lemma exists_coe {α : Type*} (s : finset α) (p : s → Prop) : (∃ (x : s), p x) ↔ ∃ (x : α) (h : x ∈ s), p ⟨x, h⟩ := subtype.exists instance pi_finset_coe.can_lift (ι : Type*) (α : Π i : ι, Type*) [ne : Π i, nonempty (α i)] (s : finset ι) : can_lift (Π i : s, α i) (Π i, α i) (λ f i, f i) (λ _, true) := pi_subtype.can_lift ι α (∈ s) instance pi_finset_coe.can_lift' (ι α : Type*) [ne : nonempty α] (s : finset ι) : can_lift (s → α) (ι → α) (λ f i, f i) (λ _, true) := pi_finset_coe.can_lift ι (λ _, α) s instance finset_coe.can_lift (s : finset α) : can_lift α s coe (λ a, a ∈ s) := { prf := λ a ha, ⟨⟨a, ha⟩, rfl⟩ } @[simp, norm_cast] lemma coe_sort_coe (s : finset α) : ((s : set α) : Sort*) = s := rfl /-! ### Subset and strict subset relations -/ section subset variables {s t : finset α} instance : has_subset (finset α) := ⟨λ s t, ∀ ⦃a⦄, a ∈ s → a ∈ t⟩ instance : has_ssubset (finset α) := ⟨λ s t, s ⊆ t ∧ ¬ t ⊆ s⟩ instance : partial_order (finset α) := { le := (⊆), lt := (⊂), le_refl := λ s a, id, le_trans := λ s t u hst htu a ha, htu $ hst ha, le_antisymm := λ s t hst hts, ext $ λ a, ⟨@hst _, @hts _⟩ } instance : is_refl (finset α) (⊆) := has_le.le.is_refl instance : is_trans (finset α) (⊆) := has_le.le.is_trans instance : is_antisymm (finset α) (⊆) := has_le.le.is_antisymm instance : is_irrefl (finset α) (⊂) := has_lt.lt.is_irrefl instance : is_trans (finset α) (⊂) := has_lt.lt.is_trans instance : is_asymm (finset α) (⊂) := has_lt.lt.is_asymm instance : is_nonstrict_strict_order (finset α) (⊆) (⊂) := ⟨λ _ _, iff.rfl⟩ lemma subset_def : s ⊆ t ↔ s.1 ⊆ t.1 := iff.rfl lemma ssubset_def : s ⊂ t ↔ s ⊆ t ∧ ¬ t ⊆ s := iff.rfl @[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _ protected lemma subset.rfl {s :finset α} : s ⊆ s := subset.refl _ protected theorem subset_of_eq {s t : finset α} (h : s = t) : s ⊆ t := h ▸ subset.refl _ theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans theorem superset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊇ s₂ → s₂ ⊇ s₃ → s₁ ⊇ s₃ := λ h' h, subset.trans h h' theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset lemma not_mem_mono {s t : finset α} (h : s ⊆ t) {a : α} : a ∉ t → a ∉ s := mt $ @h _ theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := ext $ λ a, ⟨@H₁ a, @H₂ a⟩ theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl @[simp, norm_cast] theorem coe_subset {s₁ s₂ : finset α} : (s₁ : set α) ⊆ s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2 theorem subset.antisymm_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ s₁ ⊆ s₂ ∧ s₂ ⊆ s₁ := le_antisymm_iff lemma not_subset : ¬ s ⊆ t ↔ ∃ x ∈ s, x ∉ t := by simp only [←coe_subset, set.not_subset, mem_coe] @[simp] theorem le_eq_subset : ((≤) : finset α → finset α → Prop) = (⊆) := rfl @[simp] theorem lt_eq_subset : ((<) : finset α → finset α → Prop) = (⊂) := rfl theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl @[simp, norm_cast] lemma coe_ssubset {s₁ s₂ : finset α} : (s₁ : set α) ⊂ s₂ ↔ s₁ ⊂ s₂ := show (s₁ : set α) ⊂ s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁, by simp only [set.ssubset_def, finset.coe_subset] @[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ := and_congr val_le_iff $ not_congr val_le_iff lemma ssubset_iff_subset_ne {s t : finset α} : s ⊂ t ↔ s ⊆ t ∧ s ≠ t := @lt_iff_le_and_ne _ _ s t theorem ssubset_iff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ⊂ s₂ ↔ ∃ x ∈ s₂, x ∉ s₁ := set.ssubset_iff_of_subset h lemma ssubset_of_ssubset_of_subset {s₁ s₂ s₃ : finset α} (hs₁s₂ : s₁ ⊂ s₂) (hs₂s₃ : s₂ ⊆ s₃) : s₁ ⊂ s₃ := set.ssubset_of_ssubset_of_subset hs₁s₂ hs₂s₃ lemma ssubset_of_subset_of_ssubset {s₁ s₂ s₃ : finset α} (hs₁s₂ : s₁ ⊆ s₂) (hs₂s₃ : s₂ ⊂ s₃) : s₁ ⊂ s₃ := set.ssubset_of_subset_of_ssubset hs₁s₂ hs₂s₃ lemma exists_of_ssubset {s₁ s₂ : finset α} (h : s₁ ⊂ s₂) : ∃ x ∈ s₂, x ∉ s₁ := set.exists_of_ssubset h instance is_well_founded_ssubset : is_well_founded (finset α) (⊂) := subrelation.is_well_founded (inv_image _ _) $ λ _ _, val_lt_iff.2 instance is_well_founded_lt : well_founded_lt (finset α) := finset.is_well_founded_ssubset end subset -- TODO: these should be global attributes, but this will require fixing other files local attribute [trans] subset.trans superset.trans /-! ### Order embedding from `finset α` to `set α` -/ /-- Coercion to `set α` as an `order_embedding`. -/ def coe_emb : finset α ↪o set α := ⟨⟨coe, coe_injective⟩, λ s t, coe_subset⟩ @[simp] lemma coe_coe_emb : ⇑(coe_emb : finset α ↪o set α) = coe := rfl /-! ### Nonempty -/ /-- The property `s.nonempty` expresses the fact that the finset `s` is not empty. It should be used in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : finset α) : Prop := ∃ x : α, x ∈ s instance decidable_nonempty {s : finset α} : decidable s.nonempty := decidable_of_iff (∃ a ∈ s, true) $ by simp_rw [exists_prop, and_true, finset.nonempty] @[simp, norm_cast] lemma coe_nonempty {s : finset α} : (s : set α).nonempty ↔ s.nonempty := iff.rfl @[simp] lemma nonempty_coe_sort {s : finset α} : nonempty ↥s ↔ s.nonempty := nonempty_subtype alias coe_nonempty ↔ _ nonempty.to_set alias nonempty_coe_sort ↔ _ nonempty.coe_sort lemma nonempty.bex {s : finset α} (h : s.nonempty) : ∃ x : α, x ∈ s := h lemma nonempty.mono {s t : finset α} (hst : s ⊆ t) (hs : s.nonempty) : t.nonempty := set.nonempty.mono hst hs lemma nonempty.forall_const {s : finset α} (h : s.nonempty) {p : Prop} : (∀ x ∈ s, p) ↔ p := let ⟨x, hx⟩ := h in ⟨λ h, h x hx, λ h x hx, h⟩ lemma nonempty.to_subtype {s : finset α} : s.nonempty → nonempty s := nonempty_coe_sort.2 lemma nonempty.to_type {s : finset α} : s.nonempty → nonempty α := λ ⟨x, hx⟩, ⟨x⟩ /-! ### empty -/ section empty variables {s : finset α} /-- The empty finset -/ protected def empty : finset α := ⟨0, nodup_zero⟩ instance : has_emptyc (finset α) := ⟨finset.empty⟩ instance inhabited_finset : inhabited (finset α) := ⟨∅⟩ @[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl @[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id @[simp] theorem not_nonempty_empty : ¬(∅ : finset α).nonempty := λ ⟨x, hx⟩, not_mem_empty x hx @[simp] theorem mk_zero : (⟨0, nodup_zero⟩ : finset α) = ∅ := rfl theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ := λ e, not_mem_empty a $ e ▸ h theorem nonempty.ne_empty {s : finset α} (h : s.nonempty) : s ≠ ∅ := exists.elim h $ λ a, ne_empty_of_mem @[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _ lemma eq_empty_of_forall_not_mem {s : finset α} (H : ∀ x, x ∉ s) : s = ∅ := eq_of_veq (eq_zero_of_forall_not_mem H) lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s := ⟨by rintro rfl x; exact id, λ h, eq_empty_of_forall_not_mem h⟩ @[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅ theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero @[simp] lemma not_ssubset_empty (s : finset α) : ¬s ⊂ ∅ := λ h, let ⟨x, he, hs⟩ := exists_of_ssubset h in he theorem nonempty_of_ne_empty {s : finset α} (h : s ≠ ∅) : s.nonempty := exists_mem_of_ne_zero (mt val_eq_zero.1 h) theorem nonempty_iff_ne_empty {s : finset α} : s.nonempty ↔ s ≠ ∅ := ⟨nonempty.ne_empty, nonempty_of_ne_empty⟩ @[simp] theorem not_nonempty_iff_eq_empty {s : finset α} : ¬s.nonempty ↔ s = ∅ := nonempty_iff_ne_empty.not.trans not_not theorem eq_empty_or_nonempty (s : finset α) : s = ∅ ∨ s.nonempty := classical.by_cases or.inl (λ h, or.inr (nonempty_of_ne_empty h)) @[simp, norm_cast] lemma coe_empty : ((∅ : finset α) : set α) = ∅ := rfl @[simp, norm_cast] lemma coe_eq_empty {s : finset α} : (s : set α) = ∅ ↔ s = ∅ := by rw [← coe_empty, coe_inj] @[simp] lemma is_empty_coe_sort {s : finset α} : is_empty ↥s ↔ s = ∅ := by simpa using @set.is_empty_coe_sort α s instance : is_empty (∅ : finset α) := is_empty_coe_sort.2 rfl /-- A `finset` for an empty type is empty. -/ lemma eq_empty_of_is_empty [is_empty α] (s : finset α) : s = ∅ := finset.eq_empty_of_forall_not_mem is_empty_elim instance : order_bot (finset α) := { bot := ∅, bot_le := empty_subset } @[simp] lemma bot_eq_empty : (⊥ : finset α) = ∅ := rfl @[simp] lemma empty_ssubset : ∅ ⊂ s ↔ s.nonempty := (@bot_lt_iff_ne_bot (finset α) _ _ _).trans nonempty_iff_ne_empty.symm alias empty_ssubset ↔ _ nonempty.empty_ssubset end empty /-! ### singleton -/ section singleton variables {s : finset α} {a b : α} /-- `{a} : finset a` is the set `{a}` containing `a` and nothing else. This differs from `insert a ∅` in that it does not require a `decidable_eq` instance for `α`. -/ instance : has_singleton α (finset α) := ⟨λ a, ⟨{a}, nodup_singleton a⟩⟩ @[simp] theorem singleton_val (a : α) : ({a} : finset α).1 = {a} := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ({a} : finset α) ↔ b = a := mem_singleton lemma eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : finset α)) : x = y := mem_singleton.1 h theorem not_mem_singleton {a b : α} : a ∉ ({b} : finset α) ↔ a ≠ b := not_congr mem_singleton theorem mem_singleton_self (a : α) : a ∈ ({a} : finset α) := or.inl rfl @[simp] lemma val_eq_singleton_iff {a : α} {s : finset α} : s.val = {a} ↔ s = {a} := by { rw ←val_inj, refl } lemma singleton_injective : injective (singleton : α → finset α) := λ a b h, mem_singleton.1 (h ▸ mem_singleton_self _) @[simp] lemma singleton_inj : ({a} : finset α) = {b} ↔ a = b := singleton_injective.eq_iff @[simp] theorem singleton_nonempty (a : α) : ({a} : finset α).nonempty := ⟨a, mem_singleton_self a⟩ @[simp] theorem singleton_ne_empty (a : α) : ({a} : finset α) ≠ ∅ := (singleton_nonempty a).ne_empty lemma empty_ssubset_singleton : (∅ : finset α) ⊂ {a} := (singleton_nonempty _).empty_ssubset @[simp, norm_cast] lemma coe_singleton (a : α) : (({a} : finset α) : set α) = {a} := by { ext, simp } @[simp, norm_cast] lemma coe_eq_singleton {s : finset α} {a : α} : (s : set α) = {a} ↔ s = {a} := by rw [←coe_singleton, coe_inj] lemma eq_singleton_iff_unique_mem {s : finset α} {a : α} : s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a := begin split; intro t, rw t, refine ⟨finset.mem_singleton_self _, λ _, finset.mem_singleton.1⟩, ext, rw finset.mem_singleton, refine ⟨t.right _, λ r, r.symm ▸ t.left⟩ end lemma eq_singleton_iff_nonempty_unique_mem {s : finset α} {a : α} : s = {a} ↔ s.nonempty ∧ ∀ x ∈ s, x = a := begin split, { rintro rfl, simp }, { rintros ⟨hne, h_uniq⟩, rw eq_singleton_iff_unique_mem, refine ⟨_, h_uniq⟩, rw ← h_uniq hne.some hne.some_spec, exact hne.some_spec } end lemma nonempty_iff_eq_singleton_default [unique α] {s : finset α} : s.nonempty ↔ s = {default} := by simp [eq_singleton_iff_nonempty_unique_mem] alias nonempty_iff_eq_singleton_default ↔ nonempty.eq_singleton_default _ lemma singleton_iff_unique_mem (s : finset α) : (∃ a, s = {a}) ↔ ∃! a, a ∈ s := by simp only [eq_singleton_iff_unique_mem, exists_unique] lemma singleton_subset_set_iff {s : set α} {a : α} : ↑({a} : finset α) ⊆ s ↔ a ∈ s := by rw [coe_singleton, set.singleton_subset_iff] @[simp] lemma singleton_subset_iff {s : finset α} {a : α} : {a} ⊆ s ↔ a ∈ s := singleton_subset_set_iff @[simp] lemma subset_singleton_iff {s : finset α} {a : α} : s ⊆ {a} ↔ s = ∅ ∨ s = {a} := by rw [←coe_subset, coe_singleton, set.subset_singleton_iff_eq, coe_eq_empty, coe_eq_singleton] lemma singleton_subset_singleton : ({a} : finset α) ⊆ {b} ↔ a = b := by simp protected lemma nonempty.subset_singleton_iff {s : finset α} {a : α} (h : s.nonempty) : s ⊆ {a} ↔ s = {a} := subset_singleton_iff.trans $ or_iff_right h.ne_empty lemma subset_singleton_iff' {s : finset α} {a : α} : s ⊆ {a} ↔ ∀ b ∈ s, b = a := forall₂_congr $ λ _ _, mem_singleton @[simp] lemma ssubset_singleton_iff {s : finset α} {a : α} : s ⊂ {a} ↔ s = ∅ := by rw [←coe_ssubset, coe_singleton, set.ssubset_singleton_iff, coe_eq_empty] lemma eq_empty_of_ssubset_singleton {s : finset α} {x : α} (hs : s ⊂ {x}) : s = ∅ := ssubset_singleton_iff.1 hs lemma eq_singleton_or_nontrivial (ha : a ∈ s) : s = {a} ∨ (s : set α).nontrivial := by { rw ←coe_eq_singleton, exact set.eq_singleton_or_nontrivial ha } lemma nonempty.exists_eq_singleton_or_nontrivial : s.nonempty → (∃ a, s = {a}) ∨ (s : set α).nontrivial := λ ⟨a, ha⟩, (eq_singleton_or_nontrivial ha).imp_left $ exists.intro a instance [nonempty α] : nontrivial (finset α) := ‹nonempty α›.elim $ λ a, ⟨⟨{a}, ∅, singleton_ne_empty _⟩⟩ instance [is_empty α] : unique (finset α) := { default := ∅, uniq := λ s, eq_empty_of_forall_not_mem is_empty_elim } end singleton /-! ### cons -/ section cons variables {s t : finset α} {a b : α} /-- `cons a s h` is the set `{a} ∪ s` containing `a` and the elements of `s`. It is the same as `insert a s` when it is defined, but unlike `insert a s` it does not require `decidable_eq α`, and the union is guaranteed to be disjoint. -/ def cons (a : α) (s : finset α) (h : a ∉ s) : finset α := ⟨a ::ₘ s.1, nodup_cons.2 ⟨h, s.2⟩⟩ @[simp] lemma mem_cons {h} : b ∈ s.cons a h ↔ b = a ∨ b ∈ s := mem_cons @[simp] lemma mem_cons_self (a : α) (s : finset α) {h} : a ∈ cons a s h := mem_cons_self _ _ @[simp] lemma cons_val (h : a ∉ s) : (cons a s h).1 = a ::ₘ s.1 := rfl lemma forall_mem_cons (h : a ∉ s) (p : α → Prop) : (∀ x, x ∈ cons a s h → p x) ↔ p a ∧ ∀ x, x ∈ s → p x := by simp only [mem_cons, or_imp_distrib, forall_and_distrib, forall_eq] @[simp] lemma mk_cons {s : multiset α} (h : (a ::ₘ s).nodup) : (⟨a ::ₘ s, h⟩ : finset α) = cons a ⟨s, (nodup_cons.1 h).2⟩ (nodup_cons.1 h).1 := rfl @[simp] lemma nonempty_cons (h : a ∉ s) : (cons a s h).nonempty := ⟨a, mem_cons.2 $ or.inl rfl⟩ @[simp] lemma nonempty_mk {m : multiset α} {hm} : (⟨m, hm⟩ : finset α).nonempty ↔ m ≠ 0 := by induction m using multiset.induction_on; simp @[simp] lemma coe_cons {a s h} : (@cons α a s h : set α) = insert a s := by { ext, simp } lemma subset_cons (h : a ∉ s) : s ⊆ s.cons a h := subset_cons _ _ lemma ssubset_cons (h : a ∉ s) : s ⊂ s.cons a h := ssubset_cons h lemma cons_subset {h : a ∉ s} : s.cons a h ⊆ t ↔ a ∈ t ∧ s ⊆ t := cons_subset @[simp] lemma cons_subset_cons {hs ht} : s.cons a hs ⊆ t.cons a ht ↔ s ⊆ t := by rwa [← coe_subset, coe_cons, coe_cons, set.insert_subset_insert_iff, coe_subset] lemma ssubset_iff_exists_cons_subset : s ⊂ t ↔ ∃ a (h : a ∉ s), s.cons a h ⊆ t := begin refine ⟨λ h, _, λ ⟨a, ha, h⟩, ssubset_of_ssubset_of_subset (ssubset_cons _) h⟩, obtain ⟨a, hs, ht⟩ := not_subset.1 h.2, exact ⟨a, ht, cons_subset.2 ⟨hs, h.subset⟩⟩, end end cons /-! ### disjoint -/ section disjoint variables {f : α → β} {s t u : finset α} {a b : α} lemma disjoint_left : disjoint s t ↔ ∀ ⦃a⦄, a ∈ s → a ∉ t := ⟨λ h a hs ht, singleton_subset_iff.mp (h (singleton_subset_iff.mpr hs) (singleton_subset_iff.mpr ht)), λ h x hs ht a ha, h (hs ha) (ht ha)⟩ lemma disjoint_right : disjoint s t ↔ ∀ ⦃a⦄, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] lemma disjoint_iff_ne : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b := by simp only [disjoint_left, imp_not_comm, forall_eq'] @[simp] lemma disjoint_val : s.1.disjoint t.1 ↔ disjoint s t := disjoint_left.symm lemma _root_.disjoint.forall_ne_finset (h : disjoint s t) (ha : a ∈ s) (hb : b ∈ t) : a ≠ b := disjoint_iff_ne.1 h _ ha _ hb lemma not_disjoint_iff : ¬ disjoint s t ↔ ∃ a, a ∈ s ∧ a ∈ t := disjoint_left.not.trans $ not_forall.trans $ exists_congr $ λ _, by rw [not_imp, not_not] lemma disjoint_of_subset_left (h : s ⊆ u) (d : disjoint u t) : disjoint s t := disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁)) lemma disjoint_of_subset_right (h : t ⊆ u) (d : disjoint s u) : disjoint s t := disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁)) @[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left @[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right @[simp] lemma disjoint_singleton_left : disjoint (singleton a) s ↔ a ∉ s := by simp only [disjoint_left, mem_singleton, forall_eq] @[simp] lemma disjoint_singleton_right : disjoint s (singleton a) ↔ a ∉ s := disjoint.comm.trans disjoint_singleton_left @[simp] lemma disjoint_singleton : disjoint ({a} : finset α) {b} ↔ a ≠ b := by rw [disjoint_singleton_left, mem_singleton] lemma disjoint_self_iff_empty (s : finset α) : disjoint s s ↔ s = ∅ := disjoint_self @[simp, norm_cast] lemma disjoint_coe : disjoint (s : set α) t ↔ disjoint s t := by { rw [finset.disjoint_left, set.disjoint_left], refl } @[simp, norm_cast] lemma pairwise_disjoint_coe {ι : Type*} {s : set ι} {f : ι → finset α} : s.pairwise_disjoint (λ i, f i : ι → set α) ↔ s.pairwise_disjoint f := forall₅_congr $ λ _ _ _ _ _, disjoint_coe end disjoint /-! ### disjoint union -/ /-- `disj_union s t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`. It is the same as `s ∪ t`, but it does not require decidable equality on the type. The hypothesis ensures that the sets are disjoint. -/ def disj_union (s t : finset α) (h : disjoint s t) : finset α := ⟨s.1 + t.1, multiset.nodup_add.2 ⟨s.2, t.2, disjoint_val.2 h⟩⟩ @[simp] theorem mem_disj_union {α s t h a} : a ∈ @disj_union α s t h ↔ a ∈ s ∨ a ∈ t := by rcases s with ⟨⟨s⟩⟩; rcases t with ⟨⟨t⟩⟩; apply list.mem_append lemma disj_union_comm (s t : finset α) (h : disjoint s t) : disj_union s t h = disj_union t s h.symm := eq_of_veq $ add_comm _ _ @[simp] lemma empty_disj_union (t : finset α) (h : disjoint ∅ t := disjoint_bot_left) : disj_union ∅ t h = t := eq_of_veq $ zero_add _ @[simp] lemma disj_union_empty (s : finset α) (h : disjoint s ∅ := disjoint_bot_right) : disj_union s ∅ h = s := eq_of_veq $ add_zero _ lemma singleton_disj_union (a : α) (t : finset α) (h : disjoint {a} t) : disj_union {a} t h = cons a t (disjoint_singleton_left.mp h) := eq_of_veq $ multiset.singleton_add _ _ lemma disj_union_singleton (s : finset α) (a : α) (h : disjoint s {a}) : disj_union s {a} h = cons a s (disjoint_singleton_right.mp h) := by rw [disj_union_comm, singleton_disj_union] /-! ### insert -/ section insert variables [decidable_eq α] {s t u v : finset α} {a b : α} /-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/ instance : has_insert α (finset α) := ⟨λ a s, ⟨_, s.2.ndinsert a⟩⟩ lemma insert_def (a : α) (s : finset α) : insert a s = ⟨_, s.2.ndinsert a⟩ := rfl @[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = dedup (a ::ₘ s.1) := by rw [dedup_cons, dedup_eq_self]; refl theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a ::ₘ s.1 := by rw [insert_val, ndinsert_of_not_mem h] @[simp] lemma mem_insert : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := mem_ndinsert_self a s.1 lemma mem_insert_of_mem (h : a ∈ s) : a ∈ insert b s := mem_ndinsert_of_mem h lemma mem_of_mem_insert_of_ne (h : b ∈ insert a s) : b ≠ a → b ∈ s := (mem_insert.1 h).resolve_left lemma eq_of_not_mem_of_mem_insert (ha : b ∈ insert a s) (hb : b ∉ s) : b = a := (mem_insert.1 ha).resolve_right hb @[simp] theorem cons_eq_insert (a s h) : @cons α a s h = insert a s := ext $ λ a, by simp @[simp, norm_cast] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a s : set α) := set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff] lemma mem_insert_coe {s : finset α} {x y : α} : x ∈ insert y s ↔ x ∈ insert y (s : set α) := by simp instance : is_lawful_singleton α (finset α) := ⟨λ a, by { ext, simp }⟩ @[simp] lemma insert_eq_of_mem (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h @[simp] lemma insert_eq_self : insert a s = s ↔ a ∈ s := ⟨λ h, h ▸ mem_insert_self _ _, insert_eq_of_mem⟩ lemma insert_ne_self : insert a s ≠ s ↔ a ∉ s := insert_eq_self.not @[simp] theorem pair_eq_singleton (a : α) : ({a, a} : finset α) = {a} := insert_eq_of_mem $ mem_singleton_self _ theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) := ext $ λ x, by simp only [mem_insert, or.left_comm] @[simp, norm_cast] lemma coe_pair {a b : α} : (({a, b} : finset α) : set α) = {a, b} := by { ext, simp } @[simp, norm_cast] lemma coe_eq_pair {s : finset α} {a b : α} : (s : set α) = {a, b} ↔ s = {a, b} := by rw [←coe_pair, coe_inj] theorem pair_comm (a b : α) : ({a, b} : finset α) = {b, a} := insert.comm a b ∅ @[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s := ext $ λ x, by simp only [mem_insert, or.assoc.symm, or_self] @[simp] theorem insert_nonempty (a : α) (s : finset α) : (insert a s).nonempty := ⟨a, mem_insert_self a s⟩ @[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ := (insert_nonempty a s).ne_empty /-! The universe annotation is required for the following instance, possibly this is a bug in Lean. See leanprover.zulipchat.com/#narrow/stream/113488-general/topic/strange.20error.20(universe.20issue.3F) -/ instance {α : Type u} [decidable_eq α] (i : α) (s : finset α) : nonempty.{u + 1} ((insert i s : finset α) : set α) := (finset.coe_nonempty.mpr (s.insert_nonempty i)).to_subtype lemma ne_insert_of_not_mem (s t : finset α) {a : α} (h : a ∉ s) : s ≠ insert a t := by { contrapose! h, simp [h] } lemma insert_subset : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib] lemma subset_insert (a : α) (s : finset α) : s ⊆ insert a s := λ b, mem_insert_of_mem theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t := insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩ lemma insert_inj (ha : a ∉ s) : insert a s = insert b s ↔ a = b := ⟨λ h, eq_of_not_mem_of_mem_insert (h.subst $ mem_insert_self _ _) ha, congr_arg _⟩ lemma insert_inj_on (s : finset α) : set.inj_on (λ a, insert a s) sᶜ := λ a h b _, (insert_inj h).1 lemma ssubset_iff : s ⊂ t ↔ ∃ a ∉ s, insert a s ⊆ t := by exact_mod_cast @set.ssubset_iff_insert α s t lemma ssubset_insert (h : a ∉ s) : s ⊂ insert a s := ssubset_iff.mpr ⟨a, h, subset.rfl⟩ @[elab_as_eliminator] lemma cons_induction {α : Type*} {p : finset α → Prop} (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α} (h : a ∉ s), p s → p (cons a s h)) : ∀ s, p s | ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin cases nodup_cons.1 nd with m nd', rw [← (eq_of_veq _ : cons a (finset.mk s _) m = ⟨a ::ₘ s, nd⟩)], { exact h₂ (by exact m) (IH nd') }, { rw [cons_val] } end) nd @[elab_as_eliminator] lemma cons_induction_on {α : Type*} {p : finset α → Prop} (s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α} (h : a ∉ s), p s → p (cons a s h)) : p s := cons_induction h₁ h₂ s @[elab_as_eliminator] protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α] (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s := cons_induction h₁ $ λ a s ha, (s.cons_eq_insert a ha).symm ▸ h₂ ha /-- To prove a proposition about an arbitrary `finset α`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset α`, then it holds for the `finset` obtained by inserting a new element. -/ @[elab_as_eliminator] protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α] (s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s := finset.induction h₁ h₂ s /-- To prove a proposition about `S : finset α`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset α ⊆ S`, then it holds for the `finset` obtained by inserting a new element of `S`. -/ @[elab_as_eliminator] theorem induction_on' {α : Type*} {p : finset α → Prop} [decidable_eq α] (S : finset α) (h₁ : p ∅) (h₂ : ∀ {a s}, a ∈ S → s ⊆ S → a ∉ s → p s → p (insert a s)) : p S := @finset.induction_on α (λ T, T ⊆ S → p T) _ S (λ _, h₁) (λ a s has hqs hs, let ⟨hS, sS⟩ := finset.insert_subset.1 hs in h₂ hS sS has (hqs sS)) (finset.subset.refl S) /-- To prove a proposition about a nonempty `s : finset α`, it suffices to show it holds for all singletons and that if it holds for nonempty `t : finset α`, then it also holds for the `finset` obtained by inserting an element in `t`. -/ @[elab_as_eliminator] lemma nonempty.cons_induction {α : Type*} {p : Π s : finset α, s.nonempty → Prop} (h₀ : ∀ a, p {a} (singleton_nonempty _)) (h₁ : ∀ ⦃a⦄ s (h : a ∉ s) hs, p s hs → p (finset.cons a s h) (nonempty_cons h)) {s : finset α} (hs : s.nonempty) : p s hs := begin induction s using finset.cons_induction with a t ha h, { exact (not_nonempty_empty hs).elim }, obtain rfl | ht := t.eq_empty_or_nonempty, { exact h₀ a }, { exact h₁ t ha ht (h ht) } end /-- Inserting an element to a finite set is equivalent to the option type. -/ def subtype_insert_equiv_option {t : finset α} {x : α} (h : x ∉ t) : {i // i ∈ insert x t} ≃ option {i // i ∈ t} := begin refine { to_fun := λ y, if h : ↑y = x then none else some ⟨y, (mem_insert.mp y.2).resolve_left h⟩, inv_fun := λ y, y.elim ⟨x, mem_insert_self _ _⟩ $ λ z, ⟨z, mem_insert_of_mem z.2⟩, .. }, { intro y, by_cases h : ↑y = x, simp only [subtype.ext_iff, h, option.elim, dif_pos, subtype.coe_mk], simp only [h, option.elim, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, { rintro (_|y), simp only [option.elim, dif_pos, subtype.coe_mk], have : ↑y ≠ x, { rintro ⟨⟩, exact h y.2 }, simp only [this, option.elim, subtype.eta, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, end @[simp] lemma disjoint_insert_left : disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t := by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] @[simp] lemma disjoint_insert_right : disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t := disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm] end insert /-! ### Lattice structure -/ section lattice variables [decidable_eq α] {s s₁ s₂ t t₁ t₂ u v : finset α} {a b : α} /-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/ instance : has_union (finset α) := ⟨λ s t, ⟨_, t.2.ndunion s.1⟩⟩ /-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/ instance : has_inter (finset α) := ⟨λ s t, ⟨_, s.2.ndinter t.1⟩⟩ instance : lattice (finset α) := { sup := (∪), sup_le := λ s t u hs ht a ha, (mem_ndunion.1 ha).elim (λ h, hs h) (λ h, ht h), le_sup_left := λ s t a h, mem_ndunion.2 $ or.inl h, le_sup_right := λ s t a h, mem_ndunion.2 $ or.inr h, inf := (∩), le_inf := λ s t u ht hu a h, mem_ndinter.2 ⟨ht h, hu h⟩, inf_le_left := λ s t a h, (mem_ndinter.1 h).1, inf_le_right := λ s t a h, (mem_ndinter.1 h).2, ..finset.partial_order } @[simp] lemma sup_eq_union : ((⊔) : finset α → finset α → finset α) = (∪) := rfl @[simp] lemma inf_eq_inter : ((⊓) : finset α → finset α → finset α) = (∩) := rfl lemma disjoint_iff_inter_eq_empty : disjoint s t ↔ s ∩ t = ∅ := disjoint_iff instance decidable_disjoint (U V : finset α) : decidable (disjoint U V) := decidable_of_iff _ disjoint_left.symm /-! #### union -/ lemma union_val_nd (s t : finset α) : (s ∪ t).1 = ndunion s.1 t.1 := rfl @[simp] lemma union_val (s t : finset α) : (s ∪ t).1 = s.1 ∪ t.1 := ndunion_eq_union s.2 @[simp] lemma mem_union : a ∈ s ∪ t ↔ a ∈ s ∨ a ∈ t := mem_ndunion @[simp] lemma disj_union_eq_union (s t h) : @disj_union α s t h = s ∪ t := ext $ λ a, by simp lemma mem_union_left (t : finset α) (h : a ∈ s) : a ∈ s ∪ t := mem_union.2 $ or.inl h lemma mem_union_right (s : finset α) (h : a ∈ t) : a ∈ s ∪ t := mem_union.2 $ or.inr h lemma forall_mem_union {p : α → Prop} : (∀ a ∈ s ∪ t, p a) ↔ (∀ a ∈ s, p a) ∧ ∀ a ∈ t, p a := ⟨λ h, ⟨λ a, h a ∘ mem_union_left _, λ b, h b ∘ mem_union_right _⟩, λ h ab hab, (mem_union.mp hab).elim (h.1 _) (h.2 _)⟩ lemma not_mem_union : a ∉ s ∪ t ↔ a ∉ s ∧ a ∉ t := by rw [mem_union, not_or_distrib] @[simp, norm_cast] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (s₁ ∪ s₂ : set α) := set.ext $ λ x, mem_union lemma union_subset (hs : s ⊆ u) : t ⊆ u → s ∪ t ⊆ u := sup_le $ le_iff_subset.2 hs theorem subset_union_left (s₁ s₂ : finset α) : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _ theorem subset_union_right (s₁ s₂ : finset α) : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _ lemma union_subset_union (hsu : s ⊆ u) (htv : t ⊆ v) : s ∪ t ⊆ u ∪ v := sup_le_sup (le_iff_subset.2 hsu) htv lemma union_subset_union_left (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t := union_subset_union h subset.rfl lemma union_subset_union_right (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ := union_subset_union subset.rfl h lemma union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := sup_comm instance : is_commutative (finset α) (∪) := ⟨union_comm⟩ @[simp] lemma union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := sup_assoc instance : is_associative (finset α) (∪) := ⟨union_assoc⟩ @[simp] lemma union_idempotent (s : finset α) : s ∪ s = s := sup_idem instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩ lemma union_subset_left (h : s ∪ t ⊆ u) : s ⊆ u := (subset_union_left _ _).trans h lemma union_subset_right {s t u : finset α} (h : s ∪ t ⊆ u) : t ⊆ u := subset.trans (subset_union_right _ _) h lemma union_left_comm (s t u : finset α) : s ∪ (t ∪ u) = t ∪ (s ∪ u) := ext $ λ _, by simp only [mem_union, or.left_comm] lemma union_right_comm (s t u : finset α) : (s ∪ t) ∪ u = (s ∪ u) ∪ t := ext $ λ x, by simp only [mem_union, or_assoc, or_comm (x ∈ t)] theorem union_self (s : finset α) : s ∪ s = s := union_idempotent s @[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s := ext $ λ x, mem_union.trans $ or_false _ @[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s := ext $ λ x, mem_union.trans $ false_or _ theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := rfl @[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) := by simp only [insert_eq, union_assoc] @[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) := by simp only [insert_eq, union_left_comm] lemma insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t := by simp only [insert_union, union_insert, insert_idem] @[simp] lemma union_eq_left_iff_subset {s t : finset α} : s ∪ t = s ↔ t ⊆ s := sup_eq_left @[simp] lemma left_eq_union_iff_subset {s t : finset α} : s = s ∪ t ↔ t ⊆ s := by rw [← union_eq_left_iff_subset, eq_comm] @[simp] lemma union_eq_right_iff_subset {s t : finset α} : s ∪ t = t ↔ s ⊆ t := sup_eq_right @[simp] lemma right_eq_union_iff_subset {s t : finset α} : s = t ∪ s ↔ t ⊆ s := by rw [← union_eq_right_iff_subset, eq_comm] lemma union_congr_left (ht : t ⊆ s ∪ u) (hu : u ⊆ s ∪ t) : s ∪ t = s ⊔ u := sup_congr_left ht hu lemma union_congr_right (hs : s ⊆ t ∪ u) (ht : t ⊆ s ∪ u) : s ∪ u = t ∪ u := sup_congr_right hs ht lemma union_eq_union_iff_left : s ∪ t = s ∪ u ↔ t ⊆ s ∪ u ∧ u ⊆ s ∪ t := sup_eq_sup_iff_left lemma union_eq_union_iff_right : s ∪ u = t ∪ u ↔ s ⊆ t ∪ u ∧ t ⊆ s ∪ u := sup_eq_sup_iff_right @[simp] lemma disjoint_union_left : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib] @[simp] lemma disjoint_union_right : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib] /-- To prove a relation on pairs of `finset X`, it suffices to show that it is * symmetric, * it holds when one of the `finset`s is empty, * it holds for pairs of singletons, * if it holds for `[a, c]` and for `[b, c]`, then it holds for `[a ∪ b, c]`. -/ lemma induction_on_union (P : finset α → finset α → Prop) (symm : ∀ {a b}, P a b → P b a) (empty_right : ∀ {a}, P a ∅) (singletons : ∀ {a b}, P {a} {b}) (union_of : ∀ {a b c}, P a c → P b c → P (a ∪ b) c) : ∀ a b, P a b := begin intros a b, refine finset.induction_on b empty_right (λ x s xs hi, symm _), rw finset.insert_eq, apply union_of _ (symm hi), refine finset.induction_on a empty_right (λ a t ta hi, symm _), rw finset.insert_eq, exact union_of singletons (symm hi), end lemma _root_.directed.exists_mem_subset_of_finset_subset_bUnion {α ι : Type*} [hn : nonempty ι] {f : ι → set α} (h : directed (⊆) f) {s : finset α} (hs : (s : set α) ⊆ ⋃ i, f i) : ∃ i, (s : set α) ⊆ f i := begin classical, revert hs, apply s.induction_on, { refine λ _, ⟨hn.some, _⟩, simp only [coe_empty, set.empty_subset], }, { intros b t hbt htc hbtc, obtain ⟨i : ι , hti : (t : set α) ⊆ f i⟩ := htc (set.subset.trans (t.subset_insert b) hbtc), obtain ⟨j, hbj⟩ : ∃ j, b ∈ f j, by simpa [set.mem_Union₂] using hbtc (t.mem_insert_self b), rcases h j i with ⟨k, hk, hk'⟩, use k, rw [coe_insert, set.insert_subset], exact ⟨hk hbj, trans hti hk'⟩ } end lemma _root_.directed_on.exists_mem_subset_of_finset_subset_bUnion {α ι : Type*} {f : ι → set α} {c : set ι} (hn : c.nonempty) (hc : directed_on (λ i j, f i ⊆ f j) c) {s : finset α} (hs : (s : set α) ⊆ ⋃ i ∈ c, f i) : ∃ i ∈ c, (s : set α) ⊆ f i := begin rw set.bUnion_eq_Union at hs, haveI := hn.coe_sort, obtain ⟨⟨i, hic⟩, hi⟩ := (directed_comp.2 hc.directed_coe).exists_mem_subset_of_finset_subset_bUnion hs, exact ⟨i, hic, hi⟩ end /-! #### inter -/ theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl @[simp] lemma inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 := ndinter_eq_inter s₁.2 @[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₁ := (mem_inter.1 h).1 theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₂ := (mem_inter.1 h).2 theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ := and_imp.1 mem_inter.2 theorem inter_subset_left (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left theorem inter_subset_right (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right lemma subset_inter {s₁ s₂ u : finset α} : s₁ ⊆ s₂ → s₁ ⊆ u → s₁ ⊆ s₂ ∩ u := by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial @[simp, norm_cast] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (s₁ ∩ s₂ : set α) := set.ext $ λ _, mem_inter @[simp] theorem union_inter_cancel_left {s t : finset α} : (s ∪ t) ∩ s = s := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_left] @[simp] theorem union_inter_cancel_right {s t : finset α} : (s ∪ t) ∩ t = t := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_right] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext $ λ _, by simp only [mem_inter, and_comm] @[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext $ λ _, by simp only [mem_inter, and_assoc] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext $ λ _, by simp only [mem_inter, and.left_comm] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext $ λ _, by simp only [mem_inter, and.right_comm] @[simp] lemma inter_self (s : finset α) : s ∩ s = s := ext $ λ _, mem_inter.trans $ and_self _ @[simp] lemma inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext $ λ _, mem_inter.trans $ and_false _ @[simp] lemma empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext $ λ _, mem_inter.trans $ false_and _ @[simp] lemma inter_union_self (s t : finset α) : s ∩ (t ∪ s) = s := by rw [inter_comm, union_inter_cancel_right] @[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) : insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) := ext $ λ x, have x = a ∨ x ∈ s₂ ↔ x ∈ s₂, from or_iff_right_of_imp $ by rintro rfl; exact h, by simp only [mem_inter, mem_insert, or_and_distrib_left, this] @[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) : s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) := by rw [inter_comm, insert_inter_of_mem h, inter_comm] @[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) : insert a s₁ ∩ s₂ = s₁ ∩ s₂ := ext $ λ x, have ¬ (x = a ∧ x ∈ s₂), by rintro ⟨rfl, H⟩; exact h H, by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or] @[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) : s₁ ∩ insert a s₂ = s₁ ∩ s₂ := by rw [inter_comm, insert_inter_of_not_mem h, inter_comm] @[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} (H : a ∈ s) : {a} ∩ s = {a} := show insert a ∅ ∩ s = insert a ∅, by rw [insert_inter_of_mem H, empty_inter] @[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} (H : a ∉ s) : {a} ∩ s = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h @[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ {a} = {a} := by rw [inter_comm, singleton_inter_of_mem h] @[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ {a} = ∅ := by rw [inter_comm, singleton_inter_of_not_mem h] @[mono] lemma inter_subset_inter {x y s t : finset α} (h : x ⊆ y) (h' : s ⊆ t) : x ∩ s ⊆ y ∩ t := begin intros a a_in, rw finset.mem_inter at a_in ⊢, exact ⟨h a_in.1, h' a_in.2⟩ end lemma inter_subset_inter_left (h : t ⊆ u) : s ∩ t ⊆ s ∩ u := inter_subset_inter subset.rfl h lemma inter_subset_inter_right (h : s ⊆ t) : s ∩ u ⊆ t ∩ u := inter_subset_inter h subset.rfl lemma inter_subset_union : s ∩ t ⊆ s ∪ t := le_iff_subset.1 inf_le_sup instance : distrib_lattice (finset α) := { le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c, by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt}; simp only [true_or, imp_true_iff, true_and, or_true], ..finset.lattice } @[simp] theorem union_left_idem (s t : finset α) : s ∪ (s ∪ t) = s ∪ t := sup_left_idem @[simp] theorem union_right_idem (s t : finset α) : s ∪ t ∪ t = s ∪ t := sup_right_idem @[simp] theorem inter_left_idem (s t : finset α) : s ∩ (s ∩ t) = s ∩ t := inf_left_idem @[simp] theorem inter_right_idem (s t : finset α) : s ∩ t ∩ t = s ∩ t := inf_right_idem theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right lemma union_union_distrib_left (s t u : finset α) : s ∪ (t ∪ u) = (s ∪ t) ∪ (s ∪ u) := sup_sup_distrib_left _ _ _ lemma union_union_distrib_right (s t u : finset α) : (s ∪ t) ∪ u = (s ∪ u) ∪ (t ∪ u) := sup_sup_distrib_right _ _ _ lemma inter_inter_distrib_left (s t u : finset α) : s ∩ (t ∩ u) = (s ∩ t) ∩ (s ∩ u) := inf_inf_distrib_left _ _ _ lemma inter_inter_distrib_right (s t u : finset α) : (s ∩ t) ∩ u = (s ∩ u) ∩ (t ∩ u) := inf_inf_distrib_right _ _ _ lemma union_union_union_comm (s t u v : finset α) : (s ∪ t) ∪ (u ∪ v) = (s ∪ u) ∪ (t ∪ v) := sup_sup_sup_comm _ _ _ _ lemma inter_inter_inter_comm (s t u v : finset α) : (s ∩ t) ∩ (u ∩ v) = (s ∩ u) ∩ (t ∩ v) := inf_inf_inf_comm _ _ _ _ lemma union_eq_empty_iff (A B : finset α) : A ∪ B = ∅ ↔ A = ∅ ∧ B = ∅ := sup_eq_bot_iff lemma union_subset_iff : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := (sup_le_iff : s ⊔ t ≤ u ↔ s ≤ u ∧ t ≤ u) lemma subset_inter_iff : s ⊆ t ∩ u ↔ s ⊆ t ∧ s ⊆ u := (le_inf_iff : s ≤ t ⊓ u ↔ s ≤ t ∧ s ≤ u) @[simp] lemma inter_eq_left_iff_subset (s t : finset α) : s ∩ t = s ↔ s ⊆ t := inf_eq_left @[simp] lemma inter_eq_right_iff_subset (s t : finset α) : t ∩ s = s ↔ s ⊆ t := inf_eq_right lemma inter_congr_left (ht : s ∩ u ⊆ t) (hu : s ∩ t ⊆ u) : s ∩ t = s ∩ u := inf_congr_left ht hu lemma inter_congr_right (hs : t ∩ u ⊆ s) (ht : s ∩ u ⊆ t) : s ∩ u = t ∩ u := inf_congr_right hs ht lemma inter_eq_inter_iff_left : s ∩ t = s ∩ u ↔ s ∩ u ⊆ t ∧ s ∩ t ⊆ u := inf_eq_inf_iff_left lemma inter_eq_inter_iff_right : s ∩ u = t ∩ u ↔ t ∩ u ⊆ s ∧ s ∩ u ⊆ t := inf_eq_inf_iff_right lemma ite_subset_union (s s' : finset α) (P : Prop) [decidable P] : ite P s s' ⊆ s ∪ s' := ite_le_sup s s' P lemma inter_subset_ite (s s' : finset α) (P : Prop) [decidable P] : s ∩ s' ⊆ ite P s s' := inf_le_ite s s' P lemma not_disjoint_iff_nonempty_inter : ¬disjoint s t ↔ (s ∩ t).nonempty := not_disjoint_iff.trans $ by simp [finset.nonempty] alias not_disjoint_iff_nonempty_inter ↔ _ nonempty.not_disjoint lemma disjoint_or_nonempty_inter (s t : finset α) : disjoint s t ∨ (s ∩ t).nonempty := by { rw ←not_disjoint_iff_nonempty_inter, exact em _ } end lattice /-! ### erase -/ section erase variables [decidable_eq α] {s t u v : finset α} {a b : α} /-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are not equal to `a`. -/ def erase (s : finset α) (a : α) : finset α := ⟨_, s.2.erase a⟩ @[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl @[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s := s.2.mem_erase_iff lemma not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := s.2.not_mem_erase -- While this can be solved by `simp`, this lemma is eligible for `dsimp` @[nolint simp_nf, simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl @[simp] lemma erase_singleton (a : α) : ({a} : finset α).erase a = ∅ := begin ext x, rw [mem_erase, mem_singleton, not_and_self], refl, end lemma ne_of_mem_erase : b ∈ erase s a → b ≠ a := λ h, (mem_erase.1 h).1 lemma mem_of_mem_erase : b ∈ erase s a → b ∈ s := mem_of_mem_erase lemma mem_erase_of_ne_of_mem : a ≠ b → a ∈ s → a ∈ erase s b := by simp only [mem_erase]; exact and.intro /-- An element of `s` that is not an element of `erase s a` must be `a`. -/ lemma eq_of_mem_of_not_mem_erase (hs : b ∈ s) (hsa : b ∉ s.erase a) : b = a := begin rw [mem_erase, not_and] at hsa, exact not_imp_not.mp hsa hs end @[simp] theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s := eq_of_veq $ erase_of_not_mem h @[simp] lemma erase_eq_self : s.erase a = s ↔ a ∉ s := ⟨λ h, h ▸ not_mem_erase _ _, erase_eq_of_not_mem⟩ @[simp] lemma erase_insert_eq_erase (s : finset α) (a : α) : (insert a s).erase a = s.erase a := ext $ λ x, by simp only [mem_erase, mem_insert, and.congr_right_iff, false_or, iff_self, implies_true_iff] { contextual := tt } theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s := by rw [erase_insert_eq_erase, erase_eq_of_not_mem h] theorem erase_insert_of_ne {a b : α} {s : finset α} (h : a ≠ b) : erase (insert a s) b = insert a (erase s b) := ext $ λ x, have x ≠ b ∧ x = a ↔ x = a, from and_iff_right_of_imp (λ hx, hx.symm ▸ h), by simp only [mem_erase, mem_insert, and_or_distrib_left, this] theorem erase_cons_of_ne {a b : α} {s : finset α} (ha : a ∉ s) (hb : a ≠ b) : erase (cons a s ha) b = cons a (erase s b) (λ h, ha $ erase_subset _ _ h) := by simp only [cons_eq_insert, erase_insert_of_ne hb] theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s := ext $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and]; apply or_iff_right_of_imp; rintro rfl; exact h theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a := val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _ lemma subset_erase {a : α} {s t : finset α} : s ⊆ t.erase a ↔ s ⊆ t ∧ a ∉ s := ⟨λ h, ⟨h.trans (erase_subset _ _), λ ha, not_mem_erase _ _ (h ha)⟩, λ h b hb, mem_erase.2 ⟨ne_of_mem_of_not_mem hb h.2, h.1 hb⟩⟩ @[simp, norm_cast] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (s \ {a} : set α) := set.ext $ λ _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _ ... = _ : insert_erase h lemma ssubset_iff_exists_subset_erase {s t : finset α} : s ⊂ t ↔ ∃ a ∈ t, s ⊆ t.erase a := begin refine ⟨λ h, _, λ ⟨a, ha, h⟩, ssubset_of_subset_of_ssubset h $ erase_ssubset ha⟩, obtain ⟨a, ht, hs⟩ := not_subset.1 h.2, exact ⟨a, ht, subset_erase.2 ⟨h.1, hs⟩⟩, end lemma erase_ssubset_insert (s : finset α) (a : α) : s.erase a ⊂ insert a s := ssubset_iff_exists_subset_erase.2 ⟨a, mem_insert_self _ _, erase_subset_erase _ $ subset_insert _ _⟩ lemma erase_ne_self : s.erase a ≠ s ↔ a ∈ s := erase_eq_self.not_left lemma erase_cons {s : finset α} {a : α} (h : a ∉ s) : (s.cons a h).erase a = s := by rw [cons_eq_insert, erase_insert_eq_erase, erase_eq_of_not_mem h] lemma erase_idem {a : α} {s : finset α} : erase (erase s a) a = erase s a := by simp lemma erase_right_comm {a b : α} {s : finset α} : erase (erase s a) b = erase (erase s b) a := by { ext x, simp only [mem_erase, ←and_assoc], rw and_comm (x ≠ a) } theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp]; exact forall_congr (λ x, forall_swap) theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 $ subset.rfl theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 $ subset.rfl lemma subset_insert_iff_of_not_mem (h : a ∉ s) : s ⊆ insert a t ↔ s ⊆ t := by rw [subset_insert_iff, erase_eq_of_not_mem h] lemma erase_subset_iff_of_mem (h : a ∈ t) : s.erase a ⊆ t ↔ s ⊆ t := by rw [←subset_insert_iff, insert_eq_of_mem h] lemma erase_inj {x y : α} (s : finset α) (hx : x ∈ s) : s.erase x = s.erase y ↔ x = y := begin refine ⟨λ h, _, congr_arg _⟩, rw eq_of_mem_of_not_mem_erase hx, rw ←h, simp, end lemma erase_inj_on (s : finset α) : set.inj_on s.erase s := λ _ _ _ _, (erase_inj s ‹_›).mp lemma erase_inj_on' (a : α) : {s : finset α | a ∈ s}.inj_on (λ s, erase s a) := λ s hs t ht (h : s.erase a = _), by rw [←insert_erase hs, ←insert_erase ht, h] end erase /-! ### sdiff -/ section sdiff variables [decidable_eq α] {s t u v : finset α} {a b : α} /-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/ instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le tsub_le_self s₁.2⟩⟩ @[simp] lemma sdiff_val (s₁ s₂ : finset α) : (s₁ \ s₂).val = s₁.val - s₂.val := rfl @[simp] theorem mem_sdiff : a ∈ s \ t ↔ a ∈ s ∧ a ∉ t := mem_sub_of_nodup s.2 @[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h instance : generalized_boolean_algebra (finset α) := { sup_inf_sdiff := λ x y, by { simp only [ext_iff, mem_union, mem_sdiff, inf_eq_inter, sup_eq_union, mem_inter], tauto }, inf_inf_sdiff := λ x y, by { simp only [ext_iff, inter_sdiff_self, inter_empty, inter_assoc, false_iff, inf_eq_inter, not_mem_empty], tauto }, ..finset.has_sdiff, ..finset.distrib_lattice, ..finset.order_bot } lemma not_mem_sdiff_of_mem_right (h : a ∈ t) : a ∉ s \ t := by simp only [mem_sdiff, h, not_true, not_false_iff, and_false] lemma not_mem_sdiff_of_not_mem_left (h : a ∉ s) : a ∉ s \ t := by simpa lemma union_sdiff_of_subset (h : s ⊆ t) : s ∪ (t \ s) = t := sup_sdiff_cancel_right h theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ := (union_comm _ _).trans (union_sdiff_of_subset h) lemma inter_sdiff (s t u : finset α) : s ∩ (t \ u) = s ∩ t \ u := by { ext x, simp [and_assoc] } @[simp] lemma sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ := inf_sdiff_self_left @[simp] lemma sdiff_self (s₁ : finset α) : s₁ \ s₁ = ∅ := sdiff_self lemma sdiff_inter_distrib_right (s t u : finset α) : s \ (t ∩ u) = (s \ t) ∪ (s \ u) := sdiff_inf @[simp] lemma sdiff_inter_self_left (s t : finset α) : s \ (s ∩ t) = s \ t := sdiff_inf_self_left _ _ @[simp] lemma sdiff_inter_self_right (s t : finset α) : s \ (t ∩ s) = s \ t := sdiff_inf_self_right _ _ @[simp] lemma sdiff_empty : s \ ∅ = s := sdiff_bot @[mono] lemma sdiff_subset_sdiff (hst : s ⊆ t) (hvu : v ⊆ u) : s \ u ⊆ t \ v := sdiff_le_sdiff ‹s ≤ t› ‹v ≤ u› @[simp, norm_cast] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (s₁ \ s₂ : set α) := set.ext $ λ _, mem_sdiff @[simp] lemma union_sdiff_self_eq_union : s ∪ t \ s = s ∪ t := sup_sdiff_self_right _ _ @[simp] lemma sdiff_union_self_eq_union : s \ t ∪ t = s ∪ t := sup_sdiff_self_left _ _ lemma union_sdiff_left (s t : finset α) : (s ∪ t) \ s = t \ s := sup_sdiff_left_self lemma union_sdiff_right (s t : finset α) : (s ∪ t) \ t = s \ t := sup_sdiff_right_self lemma union_sdiff_cancel_left (h : disjoint s t) : (s ∪ t) \ s = t := h.sup_sdiff_cancel_left lemma union_sdiff_cancel_right (h : disjoint s t) : (s ∪ t) \ t = s := h.sup_sdiff_cancel_right lemma union_sdiff_symm : s ∪ (t \ s) = t ∪ (s \ t) := by simp [union_comm] lemma sdiff_union_inter (s t : finset α) : (s \ t) ∪ (s ∩ t) = s := sup_sdiff_inf _ _ @[simp] lemma sdiff_idem (s t : finset α) : s \ t \ t = s \ t := sdiff_idem lemma subset_sdiff : s ⊆ t \ u ↔ s ⊆ t ∧ disjoint s u := le_iff_subset.symm.trans le_sdiff @[simp] lemma sdiff_eq_empty_iff_subset : s \ t = ∅ ↔ s ⊆ t := sdiff_eq_bot_iff lemma sdiff_nonempty : (s \ t).nonempty ↔ ¬ s ⊆ t := nonempty_iff_ne_empty.trans sdiff_eq_empty_iff_subset.not @[simp] lemma empty_sdiff (s : finset α) : ∅ \ s = ∅ := bot_sdiff lemma insert_sdiff_of_not_mem (s : finset α) {t : finset α} {x : α} (h : x ∉ t) : (insert x s) \ t = insert x (s \ t) := begin rw [← coe_inj, coe_insert, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_not_mem s h end lemma insert_sdiff_of_mem (s : finset α) {x : α} (h : x ∈ t) : (insert x s) \ t = s \ t := begin rw [← coe_inj, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_mem s h end @[simp] lemma insert_sdiff_insert (s t : finset α) (x : α) : (insert x s) \ (insert x t) = s \ insert x t := insert_sdiff_of_mem _ (mem_insert_self _ _) lemma sdiff_insert_of_not_mem {x : α} (h : x ∉ s) (t : finset α) : s \ (insert x t) = s \ t := begin refine subset.antisymm (sdiff_subset_sdiff (subset.refl _) (subset_insert _ _)) (λ y hy, _), simp only [mem_sdiff, mem_insert, not_or_distrib] at hy ⊢, exact ⟨hy.1, λ hxy, h $ hxy ▸ hy.1, hy.2⟩ end @[simp] lemma sdiff_subset (s t : finset α) : s \ t ⊆ s := show s \ t ≤ s, from sdiff_le lemma sdiff_ssubset (h : t ⊆ s) (ht : t.nonempty) : s \ t ⊂ s := sdiff_lt ‹t ≤ s› ht.ne_empty lemma union_sdiff_distrib (s₁ s₂ t : finset α) : (s₁ ∪ s₂) \ t = s₁ \ t ∪ s₂ \ t := sup_sdiff lemma sdiff_union_distrib (s t₁ t₂ : finset α) : s \ (t₁ ∪ t₂) = (s \ t₁) ∩ (s \ t₂) := sdiff_sup lemma union_sdiff_self (s t : finset α) : (s ∪ t) \ t = s \ t := sup_sdiff_right_self -- TODO: Do we want to delete this lemma and `finset.disj_union_singleton`, -- or instead add `finset.union_singleton`/`finset.singleton_union`? lemma sdiff_singleton_eq_erase (a : α) (s : finset α) : s \ singleton a = erase s a := by { ext, rw [mem_erase, mem_sdiff, mem_singleton], tauto } -- This lemma matches `finset.insert_eq` in functionality. lemma erase_eq (s : finset α) (a : α) : s.erase a = s \ {a} := (sdiff_singleton_eq_erase _ _).symm lemma disjoint_erase_comm : disjoint (s.erase a) t ↔ disjoint s (t.erase a) := by simp_rw [erase_eq, disjoint_sdiff_comm] lemma disjoint_of_erase_left (ha : a ∉ t) (hst : disjoint (s.erase a) t) : disjoint s t := by { rw [←erase_insert ha, ←disjoint_erase_comm, disjoint_insert_right], exact ⟨not_mem_erase _ _, hst⟩ } lemma disjoint_of_erase_right (ha : a ∉ s) (hst : disjoint s (t.erase a)) : disjoint s t := by { rw [←erase_insert ha, disjoint_erase_comm, disjoint_insert_left], exact ⟨not_mem_erase _ _, hst⟩ } @[simp] lemma inter_erase (a : α) (s t : finset α) : s ∩ t.erase a = (s ∩ t).erase a := by simp only [erase_eq, inter_sdiff] @[simp] lemma erase_inter (a : α) (s t : finset α) : s.erase a ∩ t = (s ∩ t).erase a := by simpa only [inter_comm t] using inter_erase a t s lemma erase_sdiff_comm (s t : finset α) (a : α) : s.erase a \ t = (s \ t).erase a := by simp_rw [erase_eq, sdiff_right_comm] lemma insert_union_comm (s t : finset α) (a : α) : insert a s ∪ t = s ∪ insert a t := by rw [insert_union, union_insert] lemma erase_inter_comm (s t : finset α) (a : α) : s.erase a ∩ t = s ∩ t.erase a := by rw [erase_inter, inter_erase] lemma erase_union_distrib (s t : finset α) (a : α) : (s ∪ t).erase a = s.erase a ∪ t.erase a := by simp_rw [erase_eq, union_sdiff_distrib] lemma insert_inter_distrib (s t : finset α) (a : α) : insert a (s ∩ t) = insert a s ∩ insert a t := by simp_rw [insert_eq, union_distrib_left] lemma erase_sdiff_distrib (s t : finset α) (a : α) : (s \ t).erase a = s.erase a \ t.erase a := by simp_rw [erase_eq, sdiff_sdiff, sup_sdiff_eq_sup le_rfl, sup_comm] lemma erase_union_of_mem (ha : a ∈ t) (s : finset α) : s.erase a ∪ t = s ∪ t := by rw [←insert_erase (mem_union_right s ha), erase_union_distrib, ←union_insert, insert_erase ha] lemma union_erase_of_mem (ha : a ∈ s) (t : finset α) : s ∪ t.erase a = s ∪ t := by rw [←insert_erase (mem_union_left t ha), erase_union_distrib, ←insert_union, insert_erase ha] @[simp] lemma sdiff_singleton_eq_self (ha : a ∉ s) : s \ {a} = s := sdiff_eq_self_iff_disjoint.2 $ by simp [ha] lemma sdiff_sdiff_left' (s t u : finset α) : (s \ t) \ u = (s \ t) ∩ (s \ u) := sdiff_sdiff_left' lemma sdiff_union_sdiff_cancel (hts : t ⊆ s) (hut : u ⊆ t) : s \ t ∪ t \ u = s \ u := sdiff_sup_sdiff_cancel hts hut lemma sdiff_union_erase_cancel (hts : t ⊆ s) (ha : a ∈ t) : s \ t ∪ t.erase a = s.erase a := by simp_rw [erase_eq, sdiff_union_sdiff_cancel hts (singleton_subset_iff.2 ha)] lemma sdiff_sdiff_eq_sdiff_union (h : u ⊆ s) : s \ (t \ u) = s \ t ∪ u := sdiff_sdiff_eq_sdiff_sup h lemma sdiff_insert (s t : finset α) (x : α) : s \ insert x t = (s \ t).erase x := by simp_rw [← sdiff_singleton_eq_erase, insert_eq, sdiff_sdiff_left', sdiff_union_distrib, inter_comm] lemma sdiff_insert_insert_of_mem_of_not_mem {s t : finset α} {x : α} (hxs : x ∈ s) (hxt : x ∉ t) : insert x (s \ insert x t) = s \ t := by rw [sdiff_insert, insert_erase (mem_sdiff.mpr ⟨hxs, hxt⟩)] lemma sdiff_erase (h : a ∈ s) : s \ t.erase a = insert a (s \ t) := by rw [←sdiff_singleton_eq_erase, sdiff_sdiff_eq_sdiff_union (singleton_subset_iff.2 h), insert_eq, union_comm] lemma sdiff_erase_self (ha : a ∈ s) : s \ s.erase a = {a} := by rw [sdiff_erase ha, sdiff_self, insert_emptyc_eq] lemma sdiff_sdiff_self_left (s t : finset α) : s \ (s \ t) = s ∩ t := sdiff_sdiff_right_self lemma sdiff_sdiff_eq_self (h : t ⊆ s) : s \ (s \ t) = t := sdiff_sdiff_eq_self h lemma sdiff_eq_sdiff_iff_inter_eq_inter {s t₁ t₂ : finset α} : s \ t₁ = s \ t₂ ↔ s ∩ t₁ = s ∩ t₂ := sdiff_eq_sdiff_iff_inf_eq_inf lemma union_eq_sdiff_union_sdiff_union_inter (s t : finset α) : s ∪ t = (s \ t) ∪ (t \ s) ∪ (s ∩ t) := sup_eq_sdiff_sup_sdiff_sup_inf lemma erase_eq_empty_iff (s : finset α) (a : α) : s.erase a = ∅ ↔ s = ∅ ∨ s = {a} := by rw [←sdiff_singleton_eq_erase, sdiff_eq_empty_iff_subset, subset_singleton_iff] --TODO@Yaël: Kill lemmas duplicate with `boolean_algebra` lemma sdiff_disjoint : disjoint (t \ s) s := disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2 lemma disjoint_sdiff : disjoint s (t \ s) := sdiff_disjoint.symm lemma disjoint_sdiff_inter (s t : finset α) : disjoint (s \ t) (s ∩ t) := disjoint_of_subset_right (inter_subset_right _ _) sdiff_disjoint lemma sdiff_eq_self_iff_disjoint : s \ t = s ↔ disjoint s t := sdiff_eq_self_iff_disjoint' lemma sdiff_eq_self_of_disjoint (h : disjoint s t) : s \ t = s := sdiff_eq_self_iff_disjoint.2 h end sdiff /-! ### Symmetric difference -/ section symm_diff variables [decidable_eq α] {s t : finset α} {a b : α} lemma mem_symm_diff : a ∈ s ∆ t ↔ a ∈ s ∧ a ∉ t ∨ a ∈ t ∧ a ∉ s := by simp_rw [symm_diff, sup_eq_union, mem_union, mem_sdiff] @[simp, norm_cast] lemma coe_symm_diff : (↑(s ∆ t) : set α) = s ∆ t := set.ext $ λ _, mem_symm_diff end symm_diff /-! ### attach -/ /-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype `{x // x ∈ s}`. -/ def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩ theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {s : finset α} (hx : x ∈ s) : sizeof x < sizeof s := by { cases s, dsimp [sizeof, has_sizeof.sizeof, finset.sizeof], apply lt_add_left, exact multiset.sizeof_lt_sizeof_of_mem hx } @[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl @[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _ @[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl @[simp] lemma attach_nonempty_iff (s : finset α) : s.attach.nonempty ↔ s.nonempty := by simp [finset.nonempty] @[simp] lemma attach_eq_empty_iff (s : finset α) : s.attach = ∅ ↔ s = ∅ := by simpa [eq_empty_iff_forall_not_mem] /-! ### piecewise -/ section piecewise /-- `s.piecewise f g` is the function equal to `f` on the finset `s`, and to `g` on its complement. -/ def piecewise {α : Type*} {δ : α → Sort*} (s : finset α) (f g : Π i, δ i) [Π j, decidable (j ∈ s)] : Π i, δ i := λi, if i ∈ s then f i else g i variables {δ : α → Sort*} (s : finset α) (f g : Π i, δ i) @[simp] lemma piecewise_insert_self [decidable_eq α] {j : α} [∀ i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g j = f j := by simp [piecewise] @[simp] lemma piecewise_empty [Π i : α, decidable (i ∈ (∅ : finset α))] : piecewise ∅ f g = g := by { ext i, simp [piecewise] } variable [Π j, decidable (j ∈ s)] -- TODO: fix this in norm_cast @[norm_cast move] lemma piecewise_coe [∀ j, decidable (j ∈ (s : set α))] : (s : set α).piecewise f g = s.piecewise f g := by { ext, congr } @[simp, priority 980] lemma piecewise_eq_of_mem {i : α} (hi : i ∈ s) : s.piecewise f g i = f i := by simp [piecewise, hi] @[simp, priority 980] lemma piecewise_eq_of_not_mem {i : α} (hi : i ∉ s) : s.piecewise f g i = g i := by simp [piecewise, hi] lemma piecewise_congr {f f' g g' : Π i, δ i} (hf : ∀ i ∈ s, f i = f' i) (hg : ∀ i ∉ s, g i = g' i) : s.piecewise f g = s.piecewise f' g' := funext $ λ i, if_ctx_congr iff.rfl (hf i) (hg i) @[simp, priority 990] lemma piecewise_insert_of_ne [decidable_eq α] {i j : α} [∀ i, decidable (i ∈ insert j s)] (h : i ≠ j) : (insert j s).piecewise f g i = s.piecewise f g i := by simp [piecewise, h] lemma piecewise_insert [decidable_eq α] (j : α) [∀ i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g = update (s.piecewise f g) j (f j) := by { classical, simp only [← piecewise_coe, coe_insert, ← set.piecewise_insert] } lemma piecewise_cases {i} (p : δ i → Prop) (hf : p (f i)) (hg : p (g i)) : p (s.piecewise f g i) := by by_cases hi : i ∈ s; simpa [hi] lemma piecewise_mem_set_pi {δ : α → Type*} {t : set α} {t' : Π i, set (δ i)} {f g} (hf : f ∈ set.pi t t') (hg : g ∈ set.pi t t') : s.piecewise f g ∈ set.pi t t' := by { classical, rw ← piecewise_coe, exact set.piecewise_mem_pi ↑s hf hg } lemma piecewise_singleton [decidable_eq α] (i : α) : piecewise {i} f g = update g i (f i) := by rw [← insert_emptyc_eq, piecewise_insert, piecewise_empty] lemma piecewise_piecewise_of_subset_left {s t : finset α} [Π i, decidable (i ∈ s)] [Π i, decidable (i ∈ t)] (h : s ⊆ t) (f₁ f₂ g : Π a, δ a) : s.piecewise (t.piecewise f₁ f₂) g = s.piecewise f₁ g := s.piecewise_congr (λ i hi, piecewise_eq_of_mem _ _ _ (h hi)) (λ _ _, rfl) @[simp] lemma piecewise_idem_left (f₁ f₂ g : Π a, δ a) : s.piecewise (s.piecewise f₁ f₂) g = s.piecewise f₁ g := piecewise_piecewise_of_subset_left (subset.refl _) _ _ _ lemma piecewise_piecewise_of_subset_right {s t : finset α} [Π i, decidable (i ∈ s)] [Π i, decidable (i ∈ t)] (h : t ⊆ s) (f g₁ g₂ : Π a, δ a) : s.piecewise f (t.piecewise g₁ g₂) = s.piecewise f g₂ := s.piecewise_congr (λ _ _, rfl) (λ i hi, t.piecewise_eq_of_not_mem _ _ (mt (@h _) hi)) @[simp] lemma piecewise_idem_right (f g₁ g₂ : Π a, δ a) : s.piecewise f (s.piecewise g₁ g₂) = s.piecewise f g₂ := piecewise_piecewise_of_subset_right (subset.refl _) f g₁ g₂ lemma update_eq_piecewise {β : Type*} [decidable_eq α] (f : α → β) (i : α) (v : β) : update f i v = piecewise (singleton i) (λj, v) f := (piecewise_singleton _ _ _).symm lemma update_piecewise [decidable_eq α] (i : α) (v : δ i) : update (s.piecewise f g) i v = s.piecewise (update f i v) (update g i v) := begin ext j, rcases em (j = i) with (rfl|hj); by_cases hs : j ∈ s; simp * end lemma update_piecewise_of_mem [decidable_eq α] {i : α} (hi : i ∈ s) (v : δ i) : update (s.piecewise f g) i v = s.piecewise (update f i v) g := begin rw update_piecewise, refine s.piecewise_congr (λ _ _, rfl) (λ j hj, update_noteq _ _ _), exact λ h, hj (h.symm ▸ hi) end lemma update_piecewise_of_not_mem [decidable_eq α] {i : α} (hi : i ∉ s) (v : δ i) : update (s.piecewise f g) i v = s.piecewise f (update g i v) := begin rw update_piecewise, refine s.piecewise_congr (λ j hj, update_noteq _ _ _) (λ _ _, rfl), exact λ h, hi (h ▸ hj) end lemma piecewise_le_of_le_of_le {δ : α → Type*} [Π i, preorder (δ i)] {f g h : Π i, δ i} (Hf : f ≤ h) (Hg : g ≤ h) : s.piecewise f g ≤ h := λ x, piecewise_cases s f g (≤ h x) (Hf x) (Hg x) lemma le_piecewise_of_le_of_le {δ : α → Type*} [Π i, preorder (δ i)] {f g h : Π i, δ i} (Hf : h ≤ f) (Hg : h ≤ g) : h ≤ s.piecewise f g := λ x, piecewise_cases s f g (λ y, h x ≤ y) (Hf x) (Hg x) lemma piecewise_le_piecewise' {δ : α → Type*} [Π i, preorder (δ i)] {f g f' g' : Π i, δ i} (Hf : ∀ x ∈ s, f x ≤ f' x) (Hg : ∀ x ∉ s, g x ≤ g' x) : s.piecewise f g ≤ s.piecewise f' g' := λ x, by { by_cases hx : x ∈ s; simp [hx, *] } lemma piecewise_le_piecewise {δ : α → Type*} [Π i, preorder (δ i)] {f g f' g' : Π i, δ i} (Hf : f ≤ f') (Hg : g ≤ g') : s.piecewise f g ≤ s.piecewise f' g' := s.piecewise_le_piecewise' (λ x _, Hf x) (λ x _, Hg x) lemma piecewise_mem_Icc_of_mem_of_mem {δ : α → Type*} [Π i, preorder (δ i)] {f f₁ g g₁ : Π i, δ i} (hf : f ∈ set.Icc f₁ g₁) (hg : g ∈ set.Icc f₁ g₁) : s.piecewise f g ∈ set.Icc f₁ g₁ := ⟨le_piecewise_of_le_of_le _ hf.1 hg.1, piecewise_le_of_le_of_le _ hf.2 hg.2⟩ lemma piecewise_mem_Icc {δ : α → Type*} [Π i, preorder (δ i)] {f g : Π i, δ i} (h : f ≤ g) : s.piecewise f g ∈ set.Icc f g := piecewise_mem_Icc_of_mem_of_mem _ (set.left_mem_Icc.2 h) (set.right_mem_Icc.2 h) lemma piecewise_mem_Icc' {δ : α → Type*} [Π i, preorder (δ i)] {f g : Π i, δ i} (h : g ≤ f) : s.piecewise f g ∈ set.Icc g f := piecewise_mem_Icc_of_mem_of_mem _ (set.right_mem_Icc.2 h) (set.left_mem_Icc.2 h) end piecewise section decidable_pi_exists variables {s : finset α} instance decidable_dforall_finset {p : Π a ∈ s, Prop} [hp : ∀ a (h : a ∈ s), decidable (p a h)] : decidable (∀ a (h : a ∈ s), p a h) := multiset.decidable_dforall_multiset /-- decidable equality for functions whose domain is bounded by finsets -/ instance decidable_eq_pi_finset {β : α → Type*} [h : ∀ a, decidable_eq (β a)] : decidable_eq (Π a ∈ s, β a) := multiset.decidable_eq_pi_multiset instance decidable_dexists_finset {p : Π a ∈ s, Prop} [hp : ∀ a (h : a ∈ s), decidable (p a h)] : decidable (∃ a (h : a ∈ s), p a h) := multiset.decidable_dexists_multiset end decidable_pi_exists /-! ### filter -/ section filter variables (p q : α → Prop) [decidable_pred p] [decidable_pred q] /-- `filter p s` is the set of elements of `s` that satisfy `p`. -/ def filter (s : finset α) : finset α := ⟨_, s.2.filter p⟩ @[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl @[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _ _ variable {p} @[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter lemma mem_of_mem_filter {s : finset α} (x : α) (h : x ∈ s.filter p) : x ∈ s := mem_of_mem_filter h theorem filter_ssubset {s : finset α} : s.filter p ⊂ s ↔ ∃ x ∈ s, ¬ p x := ⟨λ h, let ⟨x, hs, hp⟩ := set.exists_of_ssubset h in ⟨x, hs, mt (λ hp, mem_filter.2 ⟨hs, hp⟩) hp⟩, λ ⟨x, hs, hp⟩, ⟨s.filter_subset _, λ h, hp (mem_filter.1 (h hs)).2⟩⟩ variable (p) theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) := ext $ assume a, by simp only [mem_filter, and_comm, and.left_comm] lemma filter_true {s : finset α} [h : decidable_pred (λ _, true)] : @finset.filter α (λ _, true) h s = s := by ext; simp @[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ := ext $ assume a, by simp only [mem_filter, and_false]; refl variables {p q} lemma filter_eq_self (s : finset α) : s.filter p = s ↔ ∀ x ∈ s, p x := by simp [finset.ext_iff] /-- If all elements of a `finset` satisfy the predicate `p`, `s.filter p` is `s`. -/ @[simp] lemma filter_true_of_mem {s : finset α} (h : ∀ x ∈ s, p x) : s.filter p = s := (filter_eq_self s).mpr h /-- If all elements of a `finset` fail to satisfy the predicate `p`, `s.filter p` is `∅`. -/ lemma filter_false_of_mem {s : finset α} (h : ∀ x ∈ s, ¬ p x) : s.filter p = ∅ := eq_empty_of_forall_not_mem (by simpa) lemma filter_eq_empty_iff (s : finset α) : (s.filter p = ∅) ↔ ∀ x ∈ s, ¬ p x := begin refine ⟨_, filter_false_of_mem⟩, intros hs, injection hs with hs', rwa filter_eq_nil at hs' end lemma filter_nonempty_iff {s : finset α} : (s.filter p).nonempty ↔ ∃ a ∈ s, p a := by simp only [nonempty_iff_ne_empty, ne.def, filter_eq_empty_iff, not_not, not_forall] lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s := eq_of_veq $ filter_congr H variables (p q) lemma filter_empty : filter p ∅ = ∅ := subset_empty.1 $ filter_subset _ _ lemma filter_subset_filter {s t : finset α} (h : s ⊆ t) : s.filter p ⊆ t.filter p := assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩ lemma monotone_filter_left : monotone (filter p) := λ _ _, filter_subset_filter p lemma monotone_filter_right (s : finset α) ⦃p q : α → Prop⦄ [decidable_pred p] [decidable_pred q] (h : p ≤ q) : s.filter p ≤ s.filter q := multiset.subset_of_le (multiset.monotone_filter_right s.val h) @[simp, norm_cast] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) := set.ext $ λ _, mem_filter lemma subset_coe_filter_of_subset_forall (s : finset α) {t : set α} (h₁ : t ⊆ s) (h₂ : ∀ x ∈ t, p x) : t ⊆ s.filter p := λ x hx, (s.coe_filter p).symm ▸ ⟨h₁ hx, h₂ x hx⟩ theorem filter_singleton (a : α) : filter p (singleton a) = if p a then singleton a else ∅ := by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_cons_of_pos (a : α) (s : finset α) (ha : a ∉ s) (hp : p a): filter p (cons a s ha) = cons a (filter p s) (mem_filter.not.mpr $ mt and.left ha) := eq_of_veq $ multiset.filter_cons_of_pos s.val hp theorem filter_cons_of_neg (a : α) (s : finset α) (ha : a ∉ s) (hp : ¬p a): filter p (cons a s ha) = filter p s := eq_of_veq $ multiset.filter_cons_of_neg s.val hp lemma disjoint_filter {s : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : disjoint (s.filter p) (s.filter q) ↔ ∀ x ∈ s, p x → ¬ q x := by split; simp [disjoint_left] {contextual := tt} lemma disjoint_filter_filter {s t : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : disjoint s t → disjoint (s.filter p) (t.filter q) := disjoint.mono (filter_subset _ _) (filter_subset _ _) lemma disjoint_filter_filter' (s t : finset α) {p q : α → Prop} [decidable_pred p] [decidable_pred q] (h : disjoint p q) : disjoint (s.filter p) (t.filter q) := begin simp_rw [disjoint_left, mem_filter], rintros a ⟨hs, hp⟩ ⟨ht, hq⟩, exact h.le_bot _ ⟨hp, hq⟩, end lemma disjoint_filter_filter_neg (s t : finset α) (p : α → Prop) [decidable_pred p] [decidable_pred (λ a, ¬ p a)] : disjoint (s.filter p) (t.filter $ λ a, ¬ p a) := disjoint_filter_filter' s t disjoint_compl_right theorem filter_disj_union (s : finset α) (t : finset α) (h : disjoint s t) : filter p (disj_union s t h) = (filter p s).disj_union (filter p t) (disjoint_filter_filter h) := eq_of_veq $ multiset.filter_add _ _ _ theorem filter_cons {a : α} (s : finset α) (ha : a ∉ s) : filter p (cons a s ha) = (if p a then {a} else ∅ : finset α).disj_union (filter p s) (by { split_ifs, { rw disjoint_singleton_left, exact (mem_filter.not.mpr $ mt and.left ha) }, { exact disjoint_empty_left _ } }) := begin split_ifs with h, { rw [filter_cons_of_pos _ _ _ ha h, singleton_disj_union] }, { rw [filter_cons_of_neg _ _ _ ha h, empty_disj_union] }, end variable [decidable_eq α] theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext $ λ _, by simp only [mem_filter, mem_union, or_and_distrib_right] theorem filter_union_right (s : finset α) : s.filter p ∪ s.filter q = s.filter (λx, p x ∨ q x) := ext $ λ x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm] lemma filter_mem_eq_inter {s t : finset α} [Π i, decidable (i ∈ t)] : s.filter (λ i, i ∈ t) = s ∩ t := ext $ λ i, by rw [mem_filter, mem_inter] lemma filter_inter_distrib (s t : finset α) : (s ∩ t).filter p = s.filter p ∩ t.filter p := by { ext, simp only [mem_filter, mem_inter], exact and_and_distrib_right _ _ _ } theorem filter_inter (s t : finset α) : filter p s ∩ t = filter p (s ∩ t) := by { ext, simp only [mem_inter, mem_filter, and.right_comm] } theorem inter_filter (s t : finset α) : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : α) (s : finset α) : filter p (insert a s) = if p a then insert a (filter p s) else filter p s := by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_erase (a : α) (s : finset α) : filter p (erase s a) = erase (filter p s) a := by { ext x, simp only [and_assoc, mem_filter, iff_self, mem_erase] } theorem filter_or [decidable_pred (λ a, p a ∨ q a)] (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q := ext $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left] theorem filter_and [decidable_pred (λ a, p a ∧ q a)] (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q := ext $ λ _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self] theorem filter_not [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p := ext $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using λ a, and_congr_right $ λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm theorem sdiff_eq_filter (s₁ s₂ : finset α) : s₁ \ s₂ = filter (∉ s₂) s₁ := ext $ λ _, by simp only [mem_sdiff, mem_filter] lemma sdiff_eq_self (s₁ s₂ : finset α) : s₁ \ s₂ = s₁ ↔ s₁ ∩ s₂ ⊆ ∅ := by { simp [subset.antisymm_iff], split; intro h, { transitivity' ((s₁ \ s₂) ∩ s₂), mono, simp }, { calc s₁ \ s₂ ⊇ s₁ \ (s₁ ∩ s₂) : by simp [(⊇)] ... ⊇ s₁ \ ∅ : by mono using [(⊇)] ... ⊇ s₁ : by simp [(⊇)] } } lemma subset_union_elim {s : finset α} {t₁ t₂ : set α} (h : ↑s ⊆ t₁ ∪ t₂) : ∃ s₁ s₂ : finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ := begin classical, refine ⟨s.filter (∈ t₁), s.filter (∉ t₁), _, _ , _⟩, { simp [filter_union_right, em] }, { intro x, simp }, { intro x, simp, intros hx hx₂, refine ⟨or.resolve_left (h hx) hx₂, hx₂⟩ } end /- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/ @[simp] lemma filter_congr_decidable {α} (s : finset α) (p : α → Prop) (h : decidable_pred p) [decidable_pred p] : @filter α p h s = s.filter p := by congr section classical open_locale classical /-- The following instance allows us to write `{x ∈ s | p x}` for `finset.filter p s`. Since the former notation requires us to define this for all propositions `p`, and `finset.filter` only works for decidable propositions, the notation `{x ∈ s | p x}` is only compatible with classical logic because it uses `classical.prop_decidable`. We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp` unfolds the notation `{x ∈ s | p x}` to `finset.filter p s`. If `p` happens to be decidable, the simp-lemma `finset.filter_congr_decidable` will make sure that `finset.filter` uses the right instance for decidability. -/ noncomputable instance {α : Type*} : has_sep α (finset α) := ⟨λ p x, x.filter p⟩ @[simp] lemma sep_def {α : Type*} (s : finset α) (p : α → Prop) : {x ∈ s | p x} = s.filter p := rfl end classical /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq'` with the equality the other way. -/ -- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter(eq b)`. lemma filter_eq [decidable_eq β] (s : finset β) (b : β) : s.filter (eq b) = ite (b ∈ s) {b} ∅ := begin split_ifs, { ext, simp only [mem_filter, mem_singleton], exact ⟨λ h, h.2.symm, by { rintro ⟨h⟩, exact ⟨h, rfl⟩ }⟩ }, { ext, simp only [mem_filter, not_and, iff_false, not_mem_empty], rintro m ⟨e⟩, exact h m } end /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq` with the equality the other way. -/ lemma filter_eq' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a = b) = ite (b ∈ s) {b} ∅ := trans (filter_congr (λ _ _, ⟨eq.symm, eq.symm⟩)) (filter_eq s b) lemma filter_ne [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, b ≠ a) = s.erase b := by { ext, simp only [mem_filter, mem_erase, ne.def], tauto } lemma filter_ne' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a ≠ b) = s.erase b := trans (filter_congr (λ _ _, ⟨ne.symm, ne.symm⟩)) (filter_ne s b) theorem filter_inter_filter_neg_eq [decidable_pred (λ a, ¬ p a)] (s t : finset α) : s.filter p ∩ t.filter (λa, ¬ p a) = ∅ := (disjoint_filter_filter_neg s t p).eq_bot theorem filter_union_filter_of_codisjoint (s : finset α) (h : codisjoint p q) : s.filter p ∪ s.filter q = s := (filter_or _ _ _).symm.trans $ filter_true_of_mem $ λ x hx, h.top_le x trivial theorem filter_union_filter_neg_eq [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s := filter_union_filter_of_codisjoint _ _ _ codisjoint_hnot_right end filter /-! ### range -/ section range variables {n m l : ℕ} /-- `range n` is the set of natural numbers less than `n`. -/ def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩ @[simp] theorem range_val (n : ℕ) : (range n).1 = multiset.range n := rfl @[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range @[simp, norm_cast] lemma coe_range (n : ℕ) : (range n : set ℕ) = set.Iio n := set.ext $ λ _, mem_range @[simp] theorem range_zero : range 0 = ∅ := rfl @[simp] theorem range_one : range 1 = {0} := rfl theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm lemma range_add_one : range (n + 1) = insert n (range n) := range_succ @[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self @[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := multiset.self_mem_range_succ n @[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset theorem range_mono : monotone range := λ _ _, range_subset.2 lemma mem_range_succ_iff {a b : ℕ} : a ∈ finset.range b.succ ↔ a ≤ b := finset.mem_range.trans nat.lt_succ_iff lemma mem_range_le {n x : ℕ} (hx : x ∈ range n) : x ≤ n := (mem_range.1 hx).le lemma mem_range_sub_ne_zero {n x : ℕ} (hx : x ∈ range n) : n - x ≠ 0 := ne_of_gt $ tsub_pos_of_lt $ mem_range.1 hx @[simp] lemma nonempty_range_iff : (range n).nonempty ↔ n ≠ 0 := ⟨λ ⟨k, hk⟩, ((zero_le k).trans_lt $ mem_range.1 hk).ne', λ h, ⟨0, mem_range.2 $ pos_iff_ne_zero.2 h⟩⟩ @[simp] lemma range_eq_empty_iff : range n = ∅ ↔ n = 0 := by rw [← not_nonempty_iff_eq_empty, nonempty_range_iff, not_not] lemma nonempty_range_succ : (range $ n + 1).nonempty := nonempty_range_iff.2 n.succ_ne_zero @[simp] lemma range_filter_eq {n m : ℕ} : (range n).filter (= m) = if m < n then {m} else ∅ := begin convert filter_eq (range n) m, { ext, exact comm }, { simp } end end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false := by simp only [not_mem_empty, false_and, exists_false] lemma exists_mem_insert [decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ ∃ x, x ∈ s ∧ p x := by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true := iff_true_intro $ λ _, false.elim lemma forall_mem_insert [decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∀ x, x ∈ insert a s → p x) ↔ p a ∧ ∀ x, x ∈ s → p x := by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] end finset /-- Equivalence between the set of natural numbers which are `≥ k` and `ℕ`, given by `n → n - k`. -/ def not_mem_range_equiv (k : ℕ) : {n // n ∉ range k} ≃ ℕ := { to_fun := λ i, i.1 - k, inv_fun := λ j, ⟨j + k, by simp⟩, left_inv := λ j, begin rw subtype.ext_iff_val, apply tsub_add_cancel_of_le, simpa using j.2 end, right_inv := λ j, add_tsub_cancel_right _ _ } @[simp] lemma coe_not_mem_range_equiv (k : ℕ) : (not_mem_range_equiv k : {n // n ∉ range k} → ℕ) = (λ i, i - k) := rfl @[simp] lemma coe_not_mem_range_equiv_symm (k : ℕ) : ((not_mem_range_equiv k).symm : ℕ → {n // n ∉ range k}) = λ j, ⟨j + k, by simp⟩ := rfl /-! ### dedup on list and multiset -/ namespace multiset variables [decidable_eq α] {s t : multiset α} /-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/ def to_finset (s : multiset α) : finset α := ⟨_, nodup_dedup s⟩ @[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.dedup := rfl theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset := finset.val_inj.1 n.dedup.symm lemma nodup.to_finset_inj {l l' : multiset α} (hl : nodup l) (hl' : nodup l') (h : l.to_finset = l'.to_finset) : l = l' := by simpa [←to_finset_eq hl, ←to_finset_eq hl'] using h @[simp] lemma mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s := mem_dedup @[simp] lemma to_finset_zero : to_finset (0 : multiset α) = ∅ := rfl @[simp] lemma to_finset_cons (a : α) (s : multiset α) : to_finset (a ::ₘ s) = insert a (to_finset s) := finset.eq_of_veq dedup_cons @[simp] lemma to_finset_singleton (a : α) : to_finset ({a} : multiset α) = {a} := by rw [←cons_zero, to_finset_cons, to_finset_zero, is_lawful_singleton.insert_emptyc_eq] @[simp] lemma to_finset_add (s t : multiset α) : to_finset (s + t) = to_finset s ∪ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_nsmul (s : multiset α) : ∀ (n : ℕ) (hn : n ≠ 0), (n • s).to_finset = s.to_finset | 0 h := by contradiction | (n+1) h := begin by_cases n = 0, { rw [h, zero_add, one_nsmul] }, { rw [add_nsmul, to_finset_add, one_nsmul, to_finset_nsmul n h, finset.union_idempotent] } end @[simp] lemma to_finset_inter (s t : multiset α) : to_finset (s ∩ t) = to_finset s ∩ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_union (s t : multiset α) : (s ∪ t).to_finset = s.to_finset ∪ t.to_finset := by ext; simp @[simp] theorem to_finset_eq_empty {m : multiset α} : m.to_finset = ∅ ↔ m = 0 := finset.val_inj.symm.trans multiset.dedup_eq_zero @[simp] lemma to_finset_subset : s.to_finset ⊆ t.to_finset ↔ s ⊆ t := by simp only [finset.subset_iff, multiset.subset_iff, multiset.mem_to_finset] @[simp] lemma to_finset_ssubset : s.to_finset ⊂ t.to_finset ↔ s ⊂ t := by { simp_rw [finset.ssubset_def, to_finset_subset], refl } @[simp] lemma to_finset_dedup (m : multiset α) : m.dedup.to_finset = m.to_finset := by simp_rw [to_finset, dedup_idempotent] @[simp] lemma to_finset_bind_dedup [decidable_eq β] (m : multiset α) (f : α → multiset β) : (m.dedup.bind f).to_finset = (m.bind f).to_finset := by simp_rw [to_finset, dedup_bind_dedup] instance is_well_founded_ssubset : is_well_founded (multiset β) (⊂) := subrelation.is_well_founded (inv_image _ _) $ λ _ _, by classical; exact to_finset_ssubset.2 end multiset namespace finset @[simp] lemma val_to_finset [decidable_eq α] (s : finset α) : s.val.to_finset = s := by { ext, rw [multiset.mem_to_finset, ←mem_def] } lemma val_le_iff_val_subset {a : finset α} {b : multiset α} : a.val ≤ b ↔ a.val ⊆ b := multiset.le_iff_subset a.nodup end finset namespace list variables [decidable_eq α] {l l' : list α} {a : α} /-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/ def to_finset (l : list α) : finset α := multiset.to_finset l @[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.dedup : multiset α) := rfl @[simp] theorem to_finset_coe (l : list α) : (l : multiset α).to_finset = l.to_finset := rfl lemma to_finset_eq (n : nodup l) : @finset.mk α l n = l.to_finset := multiset.to_finset_eq n @[simp] lemma mem_to_finset : a ∈ l.to_finset ↔ a ∈ l := mem_dedup @[simp, norm_cast] lemma coe_to_finset (l : list α) : (l.to_finset : set α) = {a | a ∈ l} := set.ext $ λ _, list.mem_to_finset @[simp] lemma to_finset_nil : to_finset (@nil α) = ∅ := rfl @[simp] lemma to_finset_cons : to_finset (a :: l) = insert a (to_finset l) := finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.dedup_cons, h] lemma to_finset_surj_on : set.surj_on to_finset {l : list α | l.nodup} set.univ := by { rintro ⟨⟨l⟩, hl⟩ _, exact ⟨l, hl, (to_finset_eq hl).symm⟩ } theorem to_finset_surjective : surjective (to_finset : list α → finset α) := λ s, let ⟨l, _, hls⟩ := to_finset_surj_on (set.mem_univ s) in ⟨l, hls⟩ lemma to_finset_eq_iff_perm_dedup : l.to_finset = l'.to_finset ↔ l.dedup ~ l'.dedup := by simp [finset.ext_iff, perm_ext (nodup_dedup _) (nodup_dedup _)] lemma to_finset.ext_iff {a b : list α} : a.to_finset = b.to_finset ↔ ∀ x, x ∈ a ↔ x ∈ b := by simp only [finset.ext_iff, mem_to_finset] lemma to_finset.ext : (∀ x, x ∈ l ↔ x ∈ l') → l.to_finset = l'.to_finset := to_finset.ext_iff.mpr lemma to_finset_eq_of_perm (l l' : list α) (h : l ~ l') : l.to_finset = l'.to_finset := to_finset_eq_iff_perm_dedup.mpr h.dedup lemma perm_of_nodup_nodup_to_finset_eq (hl : nodup l) (hl' : nodup l') (h : l.to_finset = l'.to_finset) : l ~ l' := by { rw ←multiset.coe_eq_coe, exact multiset.nodup.to_finset_inj hl hl' h } @[simp] lemma to_finset_append : to_finset (l ++ l') = l.to_finset ∪ l'.to_finset := begin induction l with hd tl hl, { simp }, { simp [hl] } end @[simp] lemma to_finset_reverse {l : list α} : to_finset l.reverse = l.to_finset := to_finset_eq_of_perm _ _ (reverse_perm l) lemma to_finset_replicate_of_ne_zero {n : ℕ} (hn : n ≠ 0) : (list.replicate n a).to_finset = {a} := by { ext x, simp [hn, list.mem_replicate] } @[simp] lemma to_finset_union (l l' : list α) : (l ∪ l').to_finset = l.to_finset ∪ l'.to_finset := by { ext, simp } @[simp] lemma to_finset_inter (l l' : list α) : (l ∩ l').to_finset = l.to_finset ∩ l'.to_finset := by { ext, simp } @[simp] lemma to_finset_eq_empty_iff (l : list α) : l.to_finset = ∅ ↔ l = nil := by cases l; simp end list namespace finset section to_list /-- Produce a list of the elements in the finite set using choice. -/ noncomputable def to_list (s : finset α) : list α := s.1.to_list lemma nodup_to_list (s : finset α) : s.to_list.nodup := by { rw [to_list, ←multiset.coe_nodup, multiset.coe_to_list], exact s.nodup } @[simp] lemma mem_to_list {a : α} {s : finset α} : a ∈ s.to_list ↔ a ∈ s := mem_to_list @[simp] lemma to_list_eq_nil {s : finset α} : s.to_list = [] ↔ s = ∅ := to_list_eq_nil.trans val_eq_zero @[simp] lemma empty_to_list {s : finset α} : s.to_list.empty ↔ s = ∅ := list.empty_iff_eq_nil.trans to_list_eq_nil @[simp] lemma to_list_empty : (∅ : finset α).to_list = [] := to_list_eq_nil.mpr rfl lemma nonempty.to_list_ne_nil {s : finset α} (hs : s.nonempty) : s.to_list ≠ [] := mt to_list_eq_nil.mp hs.ne_empty lemma nonempty.not_empty_to_list {s : finset α} (hs : s.nonempty) : ¬s.to_list.empty := mt empty_to_list.mp hs.ne_empty @[simp, norm_cast] lemma coe_to_list (s : finset α) : (s.to_list : multiset α) = s.val := s.val.coe_to_list @[simp] lemma to_list_to_finset [decidable_eq α] (s : finset α) : s.to_list.to_finset = s := by { ext, simp } @[simp] lemma to_list_eq_singleton_iff {a : α} {s : finset α} : s.to_list = [a] ↔ s = {a} := by rw [to_list, to_list_eq_singleton_iff, val_eq_singleton_iff] @[simp] lemma to_list_singleton : ∀ a, ({a} : finset α).to_list = [a] := to_list_singleton lemma exists_list_nodup_eq [decidable_eq α] (s : finset α) : ∃ (l : list α), l.nodup ∧ l.to_finset = s := ⟨s.to_list, s.nodup_to_list, s.to_list_to_finset⟩ lemma to_list_cons {a : α} {s : finset α} (h : a ∉ s) : (cons a s h).to_list ~ a :: s.to_list := (list.perm_ext (nodup_to_list _) (by simp [h, nodup_to_list s])).2 $ λ x, by simp only [list.mem_cons_iff, finset.mem_to_list, finset.mem_cons] lemma to_list_insert [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) : (insert a s).to_list ~ a :: s.to_list := cons_eq_insert _ _ h ▸ to_list_cons _ end to_list /-! ### disj_Union This section is about the bounded union of a disjoint indexed family `t : α → finset β` of finite sets over a finite set `s : finset α`. In most cases `finset.bUnion` should be preferred. -/ section disj_Union variables {s s₁ s₂ : finset α} {t t₁ t₂ : α → finset β} /-- `disj_Union s f h` is the set such that `a ∈ disj_Union s f` iff `a ∈ f i` for some `i ∈ s`. It is the same as `s.bUnion f`, but it does not require decidable equality on the type. The hypothesis ensures that the sets are disjoint. -/ def disj_Union (s : finset α) (t : α → finset β) (hf : (s : set α).pairwise_disjoint t) : finset β := ⟨(s.val.bind (finset.val ∘ t)), multiset.nodup_bind.mpr ⟨λ a ha, (t a).nodup, s.nodup.pairwise $ λ a ha b hb hab, disjoint_val.2 $ hf ha hb hab⟩⟩ @[simp] theorem disj_Union_val (s : finset α) (t : α → finset β) (h) : (s.disj_Union t h).1 = (s.1.bind (λ a, (t a).1)) := rfl @[simp] theorem disj_Union_empty (t : α → finset β) : disj_Union ∅ t (by simp) = ∅ := rfl @[simp] lemma mem_disj_Union {b : β} {h} : b ∈ s.disj_Union t h ↔ ∃ a ∈ s, b ∈ t a := by simp only [mem_def, disj_Union_val, mem_bind, exists_prop] @[simp, norm_cast] lemma coe_disj_Union {h} : (s.disj_Union t h : set β) = ⋃ x ∈ (s : set α), t x := by simp only [set.ext_iff, mem_disj_Union, set.mem_Union, iff_self, mem_coe, implies_true_iff] @[simp] theorem disj_Union_cons (a : α) (s : finset α) (ha : a ∉ s) (f : α → finset β) (H) : disj_Union (cons a s ha) f H = (f a).disj_union (s.disj_Union f $ λ b hb c hc, H (mem_cons_of_mem hb) (mem_cons_of_mem hc)) (disjoint_left.mpr $ λ b hb h, let ⟨c, hc, h⟩ := mem_disj_Union.mp h in disjoint_left.mp (H (mem_cons_self a s) (mem_cons_of_mem hc) (ne_of_mem_of_not_mem hc ha).symm) hb h) := eq_of_veq $ multiset.cons_bind _ _ _ @[simp] lemma singleton_disj_Union (a : α) {h} : finset.disj_Union {a} t h = t a := eq_of_veq $ multiset.singleton_bind _ _ lemma disj_Union_disj_Union (s : finset α) (f : α → finset β) (g : β → finset γ) (h1 h2) : (s.disj_Union f h1).disj_Union g h2 = s.attach.disj_Union (λ a, (f a).disj_Union g $ λ b hb c hc, h2 (mem_disj_Union.mpr ⟨_, a.prop, hb⟩) (mem_disj_Union.mpr ⟨_, a.prop, hc⟩)) (λ a ha b hb hab, disjoint_left.mpr $ λ x hxa hxb, begin obtain ⟨xa, hfa, hga⟩ := mem_disj_Union.mp hxa, obtain ⟨xb, hfb, hgb⟩ := mem_disj_Union.mp hxb, refine disjoint_left.mp (h2 (mem_disj_Union.mpr ⟨_, a.prop, hfa⟩) (mem_disj_Union.mpr ⟨_, b.prop, hfb⟩) _) hga hgb, rintro rfl, exact disjoint_left.mp (h1 a.prop b.prop $ subtype.coe_injective.ne hab) hfa hfb, end) := eq_of_veq $ multiset.bind_assoc.trans (multiset.attach_bind_coe _ _).symm lemma disj_Union_filter_eq_of_maps_to [decidable_eq β] {s : finset α} {t : finset β} {f : α → β} (h : ∀ x ∈ s, f x ∈ t) : t.disj_Union (λ a, s.filter $ (λ c, f c = a)) (λ x' hx y' hy hne, disjoint_filter_filter' _ _ begin simp_rw [pi.disjoint_iff, Prop.disjoint_iff], rintros i ⟨rfl, rfl⟩, exact hne rfl, end) = s := ext $ λ b, by simpa using h b end disj_Union section bUnion /-! ### bUnion This section is about the bounded union of an indexed family `t : α → finset β` of finite sets over a finite set `s : finset α`. -/ variables [decidable_eq β] {s s₁ s₂ : finset α} {t t₁ t₂ : α → finset β} /-- `bUnion s t` is the union of `t x` over `x ∈ s`. (This was formerly `bind` due to the monad structure on types with `decidable_eq`.) -/ protected def bUnion (s : finset α) (t : α → finset β) : finset β := (s.1.bind (λ a, (t a).1)).to_finset @[simp] theorem bUnion_val (s : finset α) (t : α → finset β) : (s.bUnion t).1 = (s.1.bind (λ a, (t a).1)).dedup := rfl @[simp] theorem bUnion_empty : finset.bUnion ∅ t = ∅ := rfl @[simp] lemma mem_bUnion {b : β} : b ∈ s.bUnion t ↔ ∃ a ∈ s, b ∈ t a := by simp only [mem_def, bUnion_val, mem_dedup, mem_bind, exists_prop] @[simp, norm_cast] lemma coe_bUnion : (s.bUnion t : set β) = ⋃ x ∈ (s : set α), t x := by simp only [set.ext_iff, mem_bUnion, set.mem_Union, iff_self, mem_coe, implies_true_iff] @[simp] theorem bUnion_insert [decidable_eq α] {a : α} : (insert a s).bUnion t = t a ∪ s.bUnion t := ext $ λ x, by simp only [mem_bUnion, exists_prop, mem_union, mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] -- ext $ λ x, by simp [or_and_distrib_right, exists_or_distrib] lemma bUnion_congr (hs : s₁ = s₂) (ht : ∀ a ∈ s₁, t₁ a = t₂ a) : s₁.bUnion t₁ = s₂.bUnion t₂ := ext $ λ x, by simp [hs, ht] { contextual := tt } @[simp] lemma disj_Union_eq_bUnion (s : finset α) (f : α → finset β) (hf) : s.disj_Union f hf = s.bUnion f := begin dsimp [disj_Union, finset.bUnion, function.comp], generalize_proofs h, exact eq_of_veq h.dedup.symm, end theorem bUnion_subset {s' : finset β} : s.bUnion t ⊆ s' ↔ ∀ x ∈ s, t x ⊆ s' := by simp only [subset_iff, mem_bUnion]; exact ⟨λ H a ha b hb, H ⟨a, ha, hb⟩, λ H b ⟨a, ha, hb⟩, H a ha hb⟩ @[simp] lemma singleton_bUnion {a : α} : finset.bUnion {a} t = t a := by { classical, rw [← insert_emptyc_eq, bUnion_insert, bUnion_empty, union_empty] } theorem bUnion_inter (s : finset α) (f : α → finset β) (t : finset β) : s.bUnion f ∩ t = s.bUnion (λ x, f x ∩ t) := begin ext x, simp only [mem_bUnion, mem_inter], tauto end theorem inter_bUnion (t : finset β) (s : finset α) (f : α → finset β) : t ∩ s.bUnion f = s.bUnion (λ x, t ∩ f x) := by rw [inter_comm, bUnion_inter]; simp [inter_comm] lemma bUnion_bUnion [decidable_eq γ] (s : finset α) (f : α → finset β) (g : β → finset γ) : (s.bUnion f).bUnion g = s.bUnion (λ a, (f a).bUnion g) := begin ext, simp only [finset.mem_bUnion, exists_prop], simp_rw [←exists_and_distrib_right, ←exists_and_distrib_left, and_assoc], rw exists_comm, end theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) : (s.bind t).to_finset = s.to_finset.bUnion (λa, (t a).to_finset) := ext $ λ x, by simp only [multiset.mem_to_finset, mem_bUnion, multiset.mem_bind, exists_prop] lemma bUnion_mono (h : ∀ a ∈ s, t₁ a ⊆ t₂ a) : s.bUnion t₁ ⊆ s.bUnion t₂ := have ∀ b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a), from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩, by simpa only [subset_iff, mem_bUnion, exists_imp_distrib, and_imp, exists_prop] lemma bUnion_subset_bUnion_of_subset_left (t : α → finset β) (h : s₁ ⊆ s₂) : s₁.bUnion t ⊆ s₂.bUnion t := begin intro x, simp only [and_imp, mem_bUnion, exists_prop], exact Exists.imp (λ a ha, ⟨h ha.1, ha.2⟩) end lemma subset_bUnion_of_mem (u : α → finset β) {x : α} (xs : x ∈ s) : u x ⊆ s.bUnion u := singleton_bUnion.superset.trans $ bUnion_subset_bUnion_of_subset_left u $ singleton_subset_iff.2 xs @[simp] lemma bUnion_subset_iff_forall_subset {α β : Type*} [decidable_eq β] {s : finset α} {t : finset β} {f : α → finset β} : s.bUnion f ⊆ t ↔ ∀ x ∈ s, f x ⊆ t := ⟨λ h x hx, (subset_bUnion_of_mem f hx).trans h, λ h x hx, let ⟨a, ha₁, ha₂⟩ := mem_bUnion.mp hx in h _ ha₁ ha₂⟩ @[simp] lemma bUnion_singleton_eq_self [decidable_eq α] : s.bUnion (singleton : α → finset α) = s := ext $ λ x, by simp only [mem_bUnion, mem_singleton, exists_prop, exists_eq_right'] lemma filter_bUnion (s : finset α) (f : α → finset β) (p : β → Prop) [decidable_pred p] : (s.bUnion f).filter p = s.bUnion (λ a, (f a).filter p) := begin ext b, simp only [mem_bUnion, exists_prop, mem_filter], split, { rintro ⟨⟨a, ha, hba⟩, hb⟩, exact ⟨a, ha, hba, hb⟩ }, { rintro ⟨a, ha, hba, hb⟩, exact ⟨⟨a, ha, hba⟩, hb⟩ } end lemma bUnion_filter_eq_of_maps_to [decidable_eq α] {s : finset α} {t : finset β} {f : α → β} (h : ∀ x ∈ s, f x ∈ t) : t.bUnion (λ a, s.filter $ (λ c, f c = a)) = s := by simpa only [disj_Union_eq_bUnion] using disj_Union_filter_eq_of_maps_to h lemma erase_bUnion (f : α → finset β) (s : finset α) (b : β) : (s.bUnion f).erase b = s.bUnion (λ x, (f x).erase b) := by { ext, simp only [finset.mem_bUnion, iff_self, exists_and_distrib_left, finset.mem_erase] } @[simp] lemma bUnion_nonempty : (s.bUnion t).nonempty ↔ ∃ x ∈ s, (t x).nonempty := by simp [finset.nonempty, ← exists_and_distrib_left, @exists_swap α] lemma nonempty.bUnion (hs : s.nonempty) (ht : ∀ x ∈ s, (t x).nonempty) : (s.bUnion t).nonempty := bUnion_nonempty.2 $ hs.imp $ λ x hx, ⟨hx, ht x hx⟩ lemma disjoint_bUnion_left (s : finset α) (f : α → finset β) (t : finset β) : disjoint (s.bUnion f) t ↔ (∀ i ∈ s, disjoint (f i) t) := begin classical, refine s.induction _ _, { simp only [forall_mem_empty_iff, bUnion_empty, disjoint_empty_left] }, { assume i s his ih, simp only [disjoint_union_left, bUnion_insert, his, forall_mem_insert, ih] } end lemma disjoint_bUnion_right (s : finset β) (t : finset α) (f : α → finset β) : disjoint s (t.bUnion f) ↔ ∀ i ∈ t, disjoint s (f i) := by simpa only [disjoint.comm] using disjoint_bUnion_left t f s end bUnion /-! ### choose -/ section choose variables (p : α → Prop) [decidable_pred p] (l : finset α) /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def choose_x (hp : (∃! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } := multiset.choose_x p l.val hp /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the ambient type. -/ def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp lemma choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose section pairwise variables {s : finset α} lemma pairwise_subtype_iff_pairwise_finset' (r : β → β → Prop) (f : α → β) : pairwise (r on λ x : s, f x) ↔ (s : set α).pairwise (r on f) := pairwise_subtype_iff_pairwise_set (s : set α) (r on f) lemma pairwise_subtype_iff_pairwise_finset (r : α → α → Prop) : pairwise (r on λ x : s, x) ↔ (s : set α).pairwise r := pairwise_subtype_iff_pairwise_finset' r id lemma pairwise_cons' {a : α} (ha : a ∉ s) (r : β → β → Prop) (f : α → β) : pairwise (r on λ a : s.cons a ha, f a) ↔ pairwise (r on λ a : s, f a) ∧ ∀ b ∈ s, r (f a) (f b) ∧ r (f b) (f a) := begin simp only [pairwise_subtype_iff_pairwise_finset', finset.coe_cons, set.pairwise_insert, finset.mem_coe, and.congr_right_iff], exact λ hsr, ⟨λ h b hb, h b hb $ by { rintro rfl, contradiction }, λ h b hb _, h b hb⟩, end lemma pairwise_cons {a : α} (ha : a ∉ s) (r : α → α → Prop) : pairwise (r on λ a : s.cons a ha, a) ↔ pairwise (r on λ a : s, a) ∧ ∀ b ∈ s, r a b ∧ r b a := pairwise_cons' ha r id end pairwise end finset namespace equiv /-- Inhabited types are equivalent to `option β` for some `β` by identifying `default α` with `none`. -/ def sigma_equiv_option_of_inhabited (α : Type u) [inhabited α] [decidable_eq α] : Σ (β : Type u), α ≃ option β := ⟨{x : α // x ≠ default}, { to_fun := λ (x : α), if h : x = default then none else some ⟨x, h⟩, inv_fun := option.elim default coe, left_inv := λ x, by { dsimp only, split_ifs; simp [*] }, right_inv := begin rintro (_|⟨x,h⟩), { simp }, { dsimp only, split_ifs with hi, { simpa [h] using hi }, { simp } } end }⟩ end equiv namespace multiset variable [decidable_eq α] lemma disjoint_to_finset {m1 m2 : multiset α} : _root_.disjoint m1.to_finset m2.to_finset ↔ m1.disjoint m2 := begin rw finset.disjoint_iff_ne, refine ⟨λ h a ha1 ha2, _, _⟩, { rw ← multiset.mem_to_finset at ha1 ha2, exact h _ ha1 _ ha2 rfl }, { rintros h a ha b hb rfl, rw multiset.mem_to_finset at ha hb, exact h ha hb } end end multiset namespace list variables [decidable_eq α] {l l' : list α} lemma disjoint_to_finset_iff_disjoint : _root_.disjoint l.to_finset l'.to_finset ↔ l.disjoint l' := multiset.disjoint_to_finset end list -- Assert that we define `finset` without the material on `list.sublists`. -- Note that we cannot use `list.sublists` itself as that is defined very early. assert_not_exists list.sublists_len assert_not_exists multiset.powerset
5ec2f204071e19ecf3ad740847bdd2d86e526a13
2fbe653e4bc441efde5e5d250566e65538709888
/src/group_theory/subgroup.lean
d54cd8eba1960bcb533bd12ea7f165d8acea109a
[ "Apache-2.0" ]
permissive
aceg00/mathlib
5e15e79a8af87ff7eb8c17e2629c442ef24e746b
8786ea6d6d46d6969ac9a869eb818bf100802882
refs/heads/master
1,649,202,698,930
1,580,924,783,000
1,580,924,783,000
149,197,272
0
0
Apache-2.0
1,537,224,208,000
1,537,224,207,000
null
UTF-8
Lean
false
false
28,410
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, Mitchell Rowett, Scott Morrison, Johan Commelin, Mario Carneiro, Michael Howes -/ import group_theory.submonoid open set function variables {α : Type*} {β : Type*} {a a₁ a₂ b c: α} section group variables [group α] [add_group β] @[to_additive] lemma injective_mul {a : α} : injective ((*) a) := assume a₁ a₂ h, have a⁻¹ * a * a₁ = a⁻¹ * a * a₂, by rw [mul_assoc, mul_assoc, h], by rwa [inv_mul_self, one_mul, one_mul] at this section prio set_option default_priority 100 -- see Note [default priority] /-- `s` is an additive subgroup: a set containing 0 and closed under addition and negation. -/ class is_add_subgroup (s : set β) extends is_add_submonoid s : Prop := (neg_mem {a} : a ∈ s → -a ∈ s) /-- `s` is a subgroup: a set containing 1 and closed under multiplication and inverse. -/ @[to_additive is_add_subgroup] class is_subgroup (s : set α) extends is_submonoid s : Prop := (inv_mem {a} : a ∈ s → a⁻¹ ∈ s) end prio instance additive.is_add_subgroup (s : set α) [is_subgroup s] : @is_add_subgroup (additive α) _ s := ⟨@is_subgroup.inv_mem _ _ _ _⟩ theorem additive.is_add_subgroup_iff {s : set α} : @is_add_subgroup (additive α) _ s ↔ is_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_subgroup.mk α _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by resetI; apply_instance⟩ instance multiplicative.is_subgroup (s : set β) [is_add_subgroup s] : @is_subgroup (multiplicative β) _ s := ⟨@is_add_subgroup.neg_mem _ _ _ _⟩ theorem multiplicative.is_subgroup_iff {s : set β} : @is_subgroup (multiplicative β) _ s ↔ is_add_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_add_subgroup.mk β _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by resetI; apply_instance⟩ @[to_additive add_group] instance subtype.group {s : set α} [is_subgroup s] : group s := by subtype_instance @[to_additive add_comm_group] instance subtype.comm_group {α : Type*} [comm_group α] {s : set α} [is_subgroup s] : comm_group s := by subtype_instance @[simp, to_additive] lemma is_subgroup.coe_inv {s : set α} [is_subgroup s] (a : s) : ((a⁻¹ : s) : α) = a⁻¹ := rfl @[simp] lemma is_subgroup.coe_gpow {s : set α} [is_subgroup s] (a : s) (n : ℤ) : ((a ^ n : s) : α) = a ^ n := by induction n; simp [is_submonoid.coe_pow a] @[simp] lemma is_add_subgroup.gsmul_coe {β : Type*} [add_group β] {s : set β} [is_add_subgroup s] (a : s) (n : ℤ) : ((gsmul n a : s) : β) = gsmul n a := by induction n; simp [is_add_submonoid.smul_coe a] attribute [to_additive gsmul_coe] is_subgroup.coe_gpow @[to_additive of_add_neg] theorem is_subgroup.of_div (s : set α) (one_mem : (1:α) ∈ s) (div_mem : ∀{a b:α}, a ∈ s → b ∈ s → a * b⁻¹ ∈ s) : is_subgroup s := have inv_mem : ∀a, a ∈ s → a⁻¹ ∈ s, from assume a ha, have 1 * a⁻¹ ∈ s, from div_mem one_mem ha, by simpa, { inv_mem := inv_mem, mul_mem := assume a b ha hb, have a * b⁻¹⁻¹ ∈ s, from div_mem ha (inv_mem b hb), by simpa, one_mem := one_mem } theorem is_add_subgroup.of_sub (s : set β) (zero_mem : (0:β) ∈ s) (sub_mem : ∀{a b:β}, a ∈ s → b ∈ s → a - b ∈ s) : is_add_subgroup s := is_add_subgroup.of_add_neg s zero_mem (λ x y hx hy, sub_mem hx hy) @[to_additive] instance is_subgroup.inter (s₁ s₂ : set α) [is_subgroup s₁] [is_subgroup s₂] : is_subgroup (s₁ ∩ s₂) := { inv_mem := λ x hx, ⟨is_subgroup.inv_mem hx.1, is_subgroup.inv_mem hx.2⟩ } @[to_additive] instance is_subgroup.Inter {ι : Sort*} (s : ι → set α) [h : ∀ y : ι, is_subgroup (s y)] : is_subgroup (set.Inter s) := { inv_mem := λ x h, set.mem_Inter.2 $ λ y, is_subgroup.inv_mem (set.mem_Inter.1 h y) } @[to_additive is_add_subgroup_Union_of_directed] lemma is_subgroup_Union_of_directed {ι : Type*} [hι : nonempty ι] (s : ι → set α) [∀ i, is_subgroup (s i)] (directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) : is_subgroup (⋃i, s i) := { inv_mem := λ a ha, let ⟨i, hi⟩ := set.mem_Union.1 ha in set.mem_Union.2 ⟨i, is_subgroup.inv_mem hi⟩, to_is_submonoid := is_submonoid_Union_of_directed s directed } def gpowers (x : α) : set α := set.range ((^) x : ℤ → α) def gmultiples (x : β) : set β := set.range (λ i, gsmul i x) attribute [to_additive gmultiples] gpowers instance gpowers.is_subgroup (x : α) : is_subgroup (gpowers x) := { one_mem := ⟨(0:ℤ), by simp⟩, mul_mem := assume x₁ x₂ ⟨i₁, h₁⟩ ⟨i₂, h₂⟩, ⟨i₁ + i₂, by simp [gpow_add, *]⟩, inv_mem := assume x₀ ⟨i, h⟩, ⟨-i, by simp [h.symm]⟩ } instance gmultiples.is_add_subgroup (x : β) : is_add_subgroup (gmultiples x) := multiplicative.is_subgroup_iff.1 $ gpowers.is_subgroup _ attribute [to_additive is_add_subgroup] gpowers.is_subgroup lemma is_subgroup.gpow_mem {a : α} {s : set α} [is_subgroup s] (h : a ∈ s) : ∀{i:ℤ}, a ^ i ∈ s | (n : ℕ) := is_submonoid.pow_mem h | -[1+ n] := is_subgroup.inv_mem (is_submonoid.pow_mem h) lemma is_add_subgroup.gsmul_mem {a : β} {s : set β} [is_add_subgroup s] : a ∈ s → ∀{i:ℤ}, gsmul i a ∈ s := @is_subgroup.gpow_mem (multiplicative β) _ _ _ _ lemma gpowers_subset {a : α} {s : set α} [is_subgroup s] (h : a ∈ s) : gpowers a ⊆ s := λ x hx, match x, hx with _, ⟨i, rfl⟩ := is_subgroup.gpow_mem h end lemma gmultiples_subset {a : β} {s : set β} [is_add_subgroup s] (h : a ∈ s) : gmultiples a ⊆ s := @gpowers_subset (multiplicative β) _ _ _ _ h attribute [to_additive gmultiples_subset] gpowers_subset lemma mem_gpowers {a : α} : a ∈ gpowers a := ⟨1, by simp⟩ lemma mem_gmultiples {a : β} : a ∈ gmultiples a := ⟨1, by simp⟩ attribute [to_additive mem_gmultiples] mem_gpowers end group namespace is_subgroup open is_submonoid variables [group α] (s : set α) [is_subgroup s] @[to_additive] lemma inv_mem_iff : a⁻¹ ∈ s ↔ a ∈ s := ⟨λ h, by simpa using inv_mem h, inv_mem⟩ @[to_additive] lemma mul_mem_cancel_left (h : a ∈ s) : b * a ∈ s ↔ b ∈ s := ⟨λ hba, by simpa using mul_mem hba (inv_mem h), λ hb, mul_mem hb h⟩ @[to_additive] lemma mul_mem_cancel_right (h : a ∈ s) : a * b ∈ s ↔ b ∈ s := ⟨λ hab, by simpa using mul_mem (inv_mem h) hab, mul_mem h⟩ end is_subgroup theorem is_add_subgroup.sub_mem {α} [add_group α] (s : set α) [is_add_subgroup s] (a b : α) (ha : a ∈ s) (hb : b ∈ s) : a - b ∈ s := is_add_submonoid.add_mem ha (is_add_subgroup.neg_mem hb) section prio set_option default_priority 100 -- see Note [default priority] class normal_add_subgroup [add_group α] (s : set α) extends is_add_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : α, g + n - g ∈ s) @[to_additive normal_add_subgroup] class normal_subgroup [group α] (s : set α) extends is_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : α, g * n * g⁻¹ ∈ s) end prio @[to_additive normal_add_subgroup_of_add_comm_group] lemma normal_subgroup_of_comm_group [comm_group α] (s : set α) [hs : is_subgroup s] : normal_subgroup s := { normal := λ n hn g, by rwa [mul_right_comm, mul_right_inv, one_mul], ..hs } instance additive.normal_add_subgroup [group α] (s : set α) [normal_subgroup s] : @normal_add_subgroup (additive α) _ s := ⟨@normal_subgroup.normal _ _ _ _⟩ theorem additive.normal_add_subgroup_iff [group α] {s : set α} : @normal_add_subgroup (additive α) _ s ↔ normal_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_subgroup.mk α _ _ (additive.is_add_subgroup_iff.1 h₁) @h₂, λ h, by resetI; apply_instance⟩ instance multiplicative.normal_subgroup [add_group α] (s : set α) [normal_add_subgroup s] : @normal_subgroup (multiplicative α) _ s := ⟨@normal_add_subgroup.normal _ _ _ _⟩ theorem multiplicative.normal_subgroup_iff [add_group α] {s : set α} : @normal_subgroup (multiplicative α) _ s ↔ normal_add_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_add_subgroup.mk α _ _ (multiplicative.is_subgroup_iff.1 h₁) @h₂, λ h, by resetI; apply_instance⟩ namespace is_subgroup variable [group α] -- Normal subgroup properties @[to_additive] lemma mem_norm_comm {s : set α} [normal_subgroup s] {a b : α} (hab : a * b ∈ s) : b * a ∈ s := have h : a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ s, from normal_subgroup.normal (a * b) hab a⁻¹, by simp at h; exact h @[to_additive] lemma mem_norm_comm_iff {s : set α} [normal_subgroup s] {a b : α} : a * b ∈ s ↔ b * a ∈ s := ⟨mem_norm_comm, mem_norm_comm⟩ /-- The trivial subgroup -/ @[to_additive] def trivial (α : Type*) [group α] : set α := {1} @[simp, to_additive] lemma mem_trivial [group α] {g : α} : g ∈ trivial α ↔ g = 1 := mem_singleton_iff @[to_additive] instance trivial_normal : normal_subgroup (trivial α) := by refine {..}; simp [trivial] {contextual := tt} @[to_additive] lemma eq_trivial_iff {H : set α} [is_subgroup H] : H = trivial α ↔ (∀ x ∈ H, x = (1 : α)) := by simp only [set.ext_iff, is_subgroup.mem_trivial]; exact ⟨λ h x, (h x).1, λ h x, ⟨h x, λ hx, hx.symm ▸ is_submonoid.one_mem H⟩⟩ @[to_additive] instance univ_subgroup : normal_subgroup (@univ α) := by refine {..}; simp @[to_additive add_center] def center (α : Type*) [group α] : set α := {z | ∀ g, g * z = z * g} @[to_additive mem_add_center] lemma mem_center {a : α} : a ∈ center α ↔ ∀g, g * a = a * g := iff.rfl @[to_additive add_center_normal] instance center_normal : normal_subgroup (center α) := { one_mem := by simp [center], mul_mem := assume a b ha hb g, by rw [←mul_assoc, mem_center.2 ha g, mul_assoc, mem_center.2 hb g, ←mul_assoc], inv_mem := assume a ha g, calc g * a⁻¹ = a⁻¹ * (g * a) * a⁻¹ : by simp [ha g] ... = a⁻¹ * g : by rw [←mul_assoc, mul_assoc]; simp, normal := assume n ha g h, calc h * (g * n * g⁻¹) = h * n : by simp [ha g, mul_assoc] ... = g * g⁻¹ * n * h : by rw ha h; simp ... = g * n * g⁻¹ * h : by rw [mul_assoc g, ha g⁻¹, ←mul_assoc] } @[to_additive add_normalizer] def normalizer (s : set α) : set α := {g : α | ∀ n, n ∈ s ↔ g * n * g⁻¹ ∈ s} @[to_additive normalizer_is_add_subgroup] instance normalizer_is_subgroup (s : set α) [is_subgroup s] : is_subgroup (normalizer s) := { one_mem := by simp [normalizer], mul_mem := λ a b (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) (hb : ∀ n, n ∈ s ↔ b * n * b⁻¹ ∈ s) n, by rw [mul_inv_rev, ← mul_assoc, mul_assoc a, mul_assoc a, ← ha, ← hb], inv_mem := λ a (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) n, by rw [ha (a⁻¹ * n * a⁻¹⁻¹)]; simp [mul_assoc] } @[to_additive subset_add_normalizer] lemma subset_normalizer (s : set α) [is_subgroup s] : s ⊆ normalizer s := λ g hg n, by rw [is_subgroup.mul_mem_cancel_left _ ((is_subgroup.inv_mem_iff _).2 hg), is_subgroup.mul_mem_cancel_right _ hg] /-- Every subgroup is a normal subgroup of its normalizer -/ @[to_additive add_normal_in_add_normalizer] instance normal_in_normalizer (s : set α) [is_subgroup s] : normal_subgroup (subtype.val ⁻¹' s : set (normalizer s)) := { one_mem := show (1 : α) ∈ s, from is_submonoid.one_mem _, mul_mem := λ a b ha hb, show (a * b : α) ∈ s, from is_submonoid.mul_mem ha hb, inv_mem := λ a ha, show (a⁻¹ : α) ∈ s, from is_subgroup.inv_mem ha, normal := λ a ha ⟨m, hm⟩, (hm a).1 ha } end is_subgroup -- Homomorphism subgroups namespace is_group_hom open is_submonoid is_subgroup open is_mul_hom (map_mul) variables [group α] [group β] @[to_additive] def ker (f : α → β) [is_group_hom f] : set α := preimage f (trivial β) @[to_additive] lemma mem_ker (f : α → β) [is_group_hom f] {x : α} : x ∈ ker f ↔ f x = 1 := mem_trivial @[to_additive] lemma one_ker_inv (f : α → β) [is_group_hom f] {a b : α} (h : f (a * b⁻¹) = 1) : f a = f b := begin rw [map_mul f, map_inv f] at h, rw [←inv_inv (f b), eq_inv_of_mul_eq_one h] end @[to_additive] lemma one_ker_inv' (f : α → β) [is_group_hom f] {a b : α} (h : f (a⁻¹ * b) = 1) : f a = f b := begin rw [map_mul f, map_inv f] at h, apply eq_of_inv_eq_inv, rw eq_inv_of_mul_eq_one h end @[to_additive] lemma inv_ker_one (f : α → β) [is_group_hom f] {a b : α} (h : f a = f b) : f (a * b⁻¹) = 1 := have f a * (f b)⁻¹ = 1, by rw [h, mul_right_inv], by rwa [←map_inv f, ←map_mul f] at this @[to_additive] lemma inv_ker_one' (f : α → β) [is_group_hom f] {a b : α} (h : f a = f b) : f (a⁻¹ * b) = 1 := have (f a)⁻¹ * f b = 1, by rw [h, mul_left_inv], by rwa [←map_inv f, ←map_mul f] at this @[to_additive] lemma one_iff_ker_inv (f : α → β) [is_group_hom f] (a b : α) : f a = f b ↔ f (a * b⁻¹) = 1 := ⟨inv_ker_one f, one_ker_inv f⟩ @[to_additive] lemma one_iff_ker_inv' (f : α → β) [is_group_hom f] (a b : α) : f a = f b ↔ f (a⁻¹ * b) = 1 := ⟨inv_ker_one' f, one_ker_inv' f⟩ @[to_additive] lemma inv_iff_ker (f : α → β) [w : is_group_hom f] (a b : α) : f a = f b ↔ a * b⁻¹ ∈ ker f := by rw [mem_ker]; exact one_iff_ker_inv _ _ _ @[to_additive] lemma inv_iff_ker' (f : α → β) [w : is_group_hom f] (a b : α) : f a = f b ↔ a⁻¹ * b ∈ ker f := by rw [mem_ker]; exact one_iff_ker_inv' _ _ _ @[to_additive image_add_subgroup] instance image_subgroup (f : α → β) [is_group_hom f] (s : set α) [is_subgroup s] : is_subgroup (f '' s) := { mul_mem := assume a₁ a₂ ⟨b₁, hb₁, eq₁⟩ ⟨b₂, hb₂, eq₂⟩, ⟨b₁ * b₂, mul_mem hb₁ hb₂, by simp [eq₁, eq₂, map_mul f]⟩, one_mem := ⟨1, one_mem s, map_one f⟩, inv_mem := assume a ⟨b, hb, eq⟩, ⟨b⁻¹, inv_mem hb, by rw map_inv f; simp *⟩ } @[to_additive range_add_subgroup] instance range_subgroup (f : α → β) [is_group_hom f] : is_subgroup (set.range f) := @set.image_univ _ _ f ▸ is_group_hom.image_subgroup f set.univ local attribute [simp] one_mem inv_mem mul_mem normal_subgroup.normal @[to_additive] instance preimage (f : α → β) [is_group_hom f] (s : set β) [is_subgroup s] : is_subgroup (f ⁻¹' s) := by refine {..}; simp [map_mul f, map_one f, map_inv f, @inv_mem β _ s] {contextual:=tt} @[to_additive] instance preimage_normal (f : α → β) [is_group_hom f] (s : set β) [normal_subgroup s] : normal_subgroup (f ⁻¹' s) := ⟨by simp [map_mul f, map_inv f] {contextual:=tt}⟩ @[to_additive] instance normal_subgroup_ker (f : α → β) [is_group_hom f] : normal_subgroup (ker f) := is_group_hom.preimage_normal f (trivial β) @[to_additive] lemma inj_of_trivial_ker (f : α → β) [is_group_hom f] (h : ker f = trivial α) : function.injective f := begin intros a₁ a₂ hfa, simp [ext_iff, ker, is_subgroup.trivial] at h, have ha : a₁ * a₂⁻¹ = 1, by rw ←h; exact inv_ker_one f hfa, rw [eq_inv_of_mul_eq_one ha, inv_inv a₂] end @[to_additive] lemma trivial_ker_of_inj (f : α → β) [is_group_hom f] (h : function.injective f) : ker f = trivial α := set.ext $ assume x, iff.intro (assume hx, suffices f x = f 1, by simpa using h this, by simp [map_one f]; rwa [mem_ker] at hx) (by simp [mem_ker, is_group_hom.map_one f] {contextual := tt}) @[to_additive] lemma inj_iff_trivial_ker (f : α → β) [is_group_hom f] : function.injective f ↔ ker f = trivial α := ⟨trivial_ker_of_inj f, inj_of_trivial_ker f⟩ @[to_additive] lemma trivial_ker_iff_eq_one (f : α → β) [is_group_hom f] : ker f = trivial α ↔ ∀ x, f x = 1 → x = 1 := by rw set.ext_iff; simp [ker]; exact ⟨λ h x hx, (h x).1 hx, λ h x, ⟨h x, λ hx, by rw [hx, map_one f]⟩⟩ end is_group_hom @[to_additive is_add_group_hom] instance subtype_val.is_group_hom [group α] {s : set α} [is_subgroup s] : is_group_hom (subtype.val : s → α) := { ..subtype_val.is_monoid_hom } @[to_additive is_add_group_hom] instance coe.is_group_hom [group α] {s : set α} [is_subgroup s] : is_group_hom (coe : s → α) := { ..subtype_val.is_monoid_hom } @[to_additive is_add_group_hom] instance subtype_mk.is_group_hom [group α] [group β] {s : set α} [is_subgroup s] (f : β → α) [is_group_hom f] (h : ∀ x, f x ∈ s) : is_group_hom (λ x, (⟨f x, h x⟩ : s)) := { ..subtype_mk.is_monoid_hom f h } @[to_additive is_add_group_hom] instance set_inclusion.is_group_hom [group α] {s t : set α} [is_subgroup s] [is_subgroup t] (h : s ⊆ t) : is_group_hom (set.inclusion h) := subtype_mk.is_group_hom _ _ namespace add_group variables [add_group α] inductive in_closure (s : set α) : α → Prop | basic {a : α} : a ∈ s → in_closure a | zero : in_closure 0 | neg {a : α} : in_closure a → in_closure (-a) | add {a b : α} : in_closure a → in_closure b → in_closure (a + b) end add_group namespace group open is_submonoid is_subgroup variables [group α] {s : set α} @[to_additive] inductive in_closure (s : set α) : α → Prop | basic {a : α} : a ∈ s → in_closure a | one : in_closure 1 | inv {a : α} : in_closure a → in_closure a⁻¹ | mul {a b : α} : in_closure a → in_closure b → in_closure (a * b) /-- `group.closure s` is the subgroup closed over `s`, i.e. the smallest subgroup containg s. -/ @[to_additive] def closure (s : set α) : set α := {a | in_closure s a } @[to_additive] lemma mem_closure {a : α} : a ∈ s → a ∈ closure s := in_closure.basic @[to_additive is_add_subgroup] instance closure.is_subgroup (s : set α) : is_subgroup (closure s) := { one_mem := in_closure.one s, mul_mem := assume a b, in_closure.mul, inv_mem := assume a, in_closure.inv } @[to_additive] theorem subset_closure {s : set α} : s ⊆ closure s := λ a, mem_closure @[to_additive] theorem closure_subset {s t : set α} [is_subgroup t] (h : s ⊆ t) : closure s ⊆ t := assume a ha, by induction ha; simp [h _, *, one_mem, mul_mem, inv_mem_iff] @[to_additive] lemma closure_subset_iff (s t : set α) [is_subgroup t] : closure s ⊆ t ↔ s ⊆ t := ⟨assume h b ha, h (mem_closure ha), assume h b ha, closure_subset h ha⟩ @[to_additive] theorem closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_subset $ set.subset.trans h subset_closure @[simp, to_additive closure_add_subgroup] lemma closure_subgroup (s : set α) [is_subgroup s] : closure s = s := set.subset.antisymm (closure_subset $ set.subset.refl s) subset_closure @[to_additive] theorem exists_list_of_mem_closure {s : set α} {a : α} (h : a ∈ closure s) : (∃l:list α, (∀x∈l, x ∈ s ∨ x⁻¹ ∈ s) ∧ l.prod = a) := in_closure.rec_on h (λ x hxs, ⟨[x], list.forall_mem_singleton.2 $ or.inl hxs, one_mul _⟩) ⟨[], list.forall_mem_nil _, rfl⟩ (λ x _ ⟨L, HL1, HL2⟩, ⟨L.reverse.map has_inv.inv, λ x hx, let ⟨y, hy1, hy2⟩ := list.exists_of_mem_map hx in hy2 ▸ or.imp id (by rw [inv_inv]; exact id) (HL1 _ $ list.mem_reverse.1 hy1).symm, HL2 ▸ list.rec_on L one_inv.symm (λ hd tl ih, by rw [list.reverse_cons, list.map_append, list.prod_append, ih, list.map_singleton, list.prod_cons, list.prod_nil, mul_one, list.prod_cons, mul_inv_rev])⟩) (λ x y hx hy ⟨L1, HL1, HL2⟩ ⟨L2, HL3, HL4⟩, ⟨L1 ++ L2, list.forall_mem_append.2 ⟨HL1, HL3⟩, by rw [list.prod_append, HL2, HL4]⟩) @[to_additive] lemma image_closure [group β] (f : α → β) [is_group_hom f] (s : set α) : f '' closure s = closure (f '' s) := le_antisymm begin rintros _ ⟨x, hx, rfl⟩, apply in_closure.rec_on hx; intros, { solve_by_elim [subset_closure, set.mem_image_of_mem] }, { rw [is_monoid_hom.map_one f], apply is_submonoid.one_mem }, { rw [is_group_hom.map_inv f], apply is_subgroup.inv_mem, assumption }, { rw [is_monoid_hom.map_mul f], solve_by_elim [is_submonoid.mul_mem] } end (closure_subset $ set.image_subset _ subset_closure) @[to_additive] theorem mclosure_subset {s : set α} : monoid.closure s ⊆ closure s := monoid.closure_subset $ subset_closure @[to_additive] theorem mclosure_inv_subset {s : set α} : monoid.closure (has_inv.inv ⁻¹' s) ⊆ closure s := monoid.closure_subset $ λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx) @[to_additive] theorem closure_eq_mclosure {s : set α} : closure s = monoid.closure (s ∪ has_inv.inv ⁻¹' s) := set.subset.antisymm (@closure_subset _ _ _ (monoid.closure (s ∪ has_inv.inv ⁻¹' s)) { inv_mem := λ x hx, monoid.in_closure.rec_on hx (λ x hx, or.cases_on hx (λ hx, monoid.subset_closure $ or.inr $ show x⁻¹⁻¹ ∈ s, from (inv_inv x).symm ▸ hx) (λ hx, monoid.subset_closure $ or.inl hx)) ((@one_inv α _).symm ▸ is_submonoid.one_mem _) (λ x y hx hy ihx ihy, (mul_inv_rev x y).symm ▸ is_submonoid.mul_mem ihy ihx) } (set.subset.trans (set.subset_union_left _ _) monoid.subset_closure)) (monoid.closure_subset $ set.union_subset subset_closure $ λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx)) @[to_additive] theorem mem_closure_union_iff {α : Type*} [comm_group α] {s t : set α} {x : α} : x ∈ closure (s ∪ t) ↔ ∃ y ∈ closure s, ∃ z ∈ closure t, y * z = x := begin simp only [closure_eq_mclosure, monoid.mem_closure_union_iff, exists_prop, preimage_union], split, { rintro ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, rfl⟩, refine ⟨_, ⟨_, hys, _, hzs, rfl⟩, _, ⟨_, hyt, _, hzt, rfl⟩, _⟩, rw [mul_assoc, mul_assoc, mul_left_comm zs], refl }, { rintro ⟨_, ⟨ys, hys, zs, hzs, rfl⟩, _, ⟨yt, hyt, zt, hzt, rfl⟩, rfl⟩, refine ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, _⟩, rw [mul_assoc, mul_assoc, mul_left_comm yt], refl } end @[to_additive gmultiples_eq_closure] theorem gpowers_eq_closure {a : α} : gpowers a = closure {a} := subset.antisymm (gpowers_subset $ mem_closure $ by simp) (closure_subset $ by simp [mem_gpowers]) end group namespace is_subgroup variable [group α] @[to_additive] lemma trivial_eq_closure : trivial α = group.closure ∅ := subset.antisymm (by simp [set.subset_def, is_submonoid.one_mem]) (group.closure_subset $ by simp) end is_subgroup /-The normal closure of a set s is the subgroup closure of all the conjugates of elements of s. It is the smallest normal subgroup containing s. -/ namespace group variables {s : set α} [group α] /-- Given an element a, conjugates a is the set of conjugates. -/ def conjugates (a : α) : set α := {b | is_conj a b} lemma mem_conjugates_self {a : α} : a ∈ conjugates a := is_conj_refl _ /-- Given a set s, conjugates_of_set s is the set of all conjugates of the elements of s. -/ def conjugates_of_set (s : set α) : set α := ⋃ a ∈ s, conjugates a lemma mem_conjugates_of_set_iff {x : α} : x ∈ conjugates_of_set s ↔ ∃ a ∈ s, is_conj a x := set.mem_bUnion_iff theorem subset_conjugates_of_set : s ⊆ conjugates_of_set s := λ (x : α) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj_refl _⟩ theorem conjugates_of_set_mono {s t : set α} (h : s ⊆ t) : conjugates_of_set s ⊆ conjugates_of_set t := set.bUnion_subset_bUnion_left h lemma conjugates_subset {t : set α} [normal_subgroup t] {a : α} (h : a ∈ t) : conjugates a ⊆ t := λ x ⟨c,w⟩, begin have H := normal_subgroup.normal a h c, rwa ←w, end theorem conjugates_of_set_subset {s t : set α} [normal_subgroup t] (h : s ⊆ t) : conjugates_of_set s ⊆ t := set.bUnion_subset (λ x H, conjugates_subset (h H)) /-- The set of conjugates of s is closed under conjugation. -/ lemma conj_mem_conjugates_of_set {x c : α} : x ∈ conjugates_of_set s → (c * x * c⁻¹ ∈ conjugates_of_set s) := λ H, begin rcases (mem_conjugates_of_set_iff.1 H) with ⟨a,h₁,h₂⟩, exact mem_conjugates_of_set_iff.2 ⟨a, h₁, is_conj_trans h₂ ⟨c,rfl⟩⟩, end /-- The normal closure of a set s is the subgroup closure of all the conjugates of elements of s. It is the smallest normal subgroup containing s. -/ def normal_closure (s : set α) : set α := closure (conjugates_of_set s) theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s := subset_closure theorem subset_normal_closure : s ⊆ normal_closure s := set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure /-- The normal closure of a set is a subgroup. -/ instance normal_closure.is_subgroup (s : set α) : is_subgroup (normal_closure s) := closure.is_subgroup (conjugates_of_set s) /-- The normal closure of s is a normal subgroup. -/ instance normal_closure.is_normal : normal_subgroup (normal_closure s) := ⟨ λ n h g, begin induction h with x hx x hx ihx x y hx hy ihx ihy, {exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx))}, {simpa using (normal_closure.is_subgroup s).one_mem}, {rw ←conj_inv, exact (is_subgroup.inv_mem ihx)}, {rw ←conj_mul, exact (is_submonoid.mul_mem ihx ihy)}, end ⟩ /-- The normal closure of s is the smallest normal subgroup containing s. -/ theorem normal_closure_subset {s t : set α} [normal_subgroup t] (h : s ⊆ t) : normal_closure s ⊆ t := λ a w, begin induction w with x hx x hx ihx x y hx hy ihx ihy, {exact (conjugates_of_set_subset h $ hx)}, {exact is_submonoid.one_mem t}, {exact is_subgroup.inv_mem ihx}, {exact is_submonoid.mul_mem ihx ihy} end lemma normal_closure_subset_iff {s t : set α} [normal_subgroup t] : s ⊆ t ↔ normal_closure s ⊆ t := ⟨normal_closure_subset, set.subset.trans (subset_normal_closure)⟩ theorem normal_closure_mono {s t : set α} : s ⊆ t → normal_closure s ⊆ normal_closure t := λ h, normal_closure_subset (set.subset.trans h (subset_normal_closure)) end group section simple_group class simple_group (α : Type*) [group α] : Prop := (simple : ∀ (N : set α) [normal_subgroup N], N = is_subgroup.trivial α ∨ N = set.univ) class simple_add_group (α : Type*) [add_group α] : Prop := (simple : ∀ (N : set α) [normal_add_subgroup N], N = is_add_subgroup.trivial α ∨ N = set.univ) attribute [to_additive simple_add_group] simple_group theorem additive.simple_add_group_iff [group α] : simple_add_group (additive α) ↔ simple_group α := ⟨λ hs, ⟨λ N h, @simple_add_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.2 h)⟩, λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.1 h)⟩⟩ instance additive.simple_add_group [group α] [simple_group α] : simple_add_group (additive α) := additive.simple_add_group_iff.2 (by apply_instance) theorem multiplicative.simple_group_iff [add_group α] : simple_group (multiplicative α) ↔ simple_add_group α := ⟨λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.2 h)⟩, λ hs, ⟨λ N h, @simple_add_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.1 h)⟩⟩ instance multiplicative.simple_group [add_group α] [simple_add_group α] : simple_group (multiplicative α) := multiplicative.simple_group_iff.2 (by apply_instance) lemma simple_group_of_surjective [group α] [group β] [simple_group α] (f : α → β) [is_group_hom f] (hf : function.surjective f) : simple_group β := ⟨λ H iH, have normal_subgroup (f ⁻¹' H), by resetI; apply_instance, begin resetI, cases simple_group.simple (f ⁻¹' H) with h h, { refine or.inl (is_subgroup.eq_trivial_iff.2 (λ x hx, _)), cases hf x with y hy, rw ← hy at hx, rw [← hy, is_subgroup.eq_trivial_iff.1 h y hx, is_group_hom.map_one f] }, { refine or.inr (set.eq_univ_of_forall (λ x, _)), cases hf x with y hy, rw set.eq_univ_iff_forall at h, rw ← hy, exact h y } end⟩ lemma simple_add_group_of_surjective [add_group α] [add_group β] [simple_add_group α] (f : α → β) [is_add_group_hom f] (hf : function.surjective f) : simple_add_group β := multiplicative.simple_group_iff.1 (@simple_group_of_surjective (multiplicative α) (multiplicative β) _ _ _ f _ hf) attribute [to_additive simple_add_group_of_surjective] simple_group_of_surjective end simple_group
a1882dabd19572a680c409dba8c2087de32ee080
f3a5af2927397cf346ec0e24312bfff077f00425
/src/game/world5/level6.lean
52ca460bf141b147f4a14b459ced08a6a7a41bb0
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/natural_number_game
05c39e1586408cfb563d1a12e1085a90726ab655
f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd
refs/heads/master
1,688,570,964,990
1,636,908,242,000
1,636,908,242,000
195,403,790
277
84
Apache-2.0
1,694,547,955,000
1,562,328,792,000
Lean
UTF-8
Lean
false
false
1,672
lean
/- # Function world. ## Level 6: `(P → (Q → R)) → ((P → Q) → (P → R))`. You can solve this level completely just using `intro`, `apply` and `exact`, but if you want to argue forwards instead of backwards then don't forget that you can do things like `have j : Q → R := f p,` if `f : P → (Q → R)` and `p : P`. Remember the trick with the colon in `have`: we could just write `have j := f p,` but this way we can be sure that `j` is what we actually expect it to be. I recommend that you start with `intro f` rather than `intro p` because even though the goal starts `P → ...`, the brackets mean that the goal is not a function from `P` to anything, it's a function from `P → (Q → R)` to something. In fact you can save time by starting with `intros f h p`, which introduces three variables at once, although you'd better then look at your tactic state to check that you called all those new terms sensible things. After all the intros, you find that your your goal is `⊢ R`. If you try `have j : Q → R := f p` now then you can `apply j`. Alternatively you can `apply (f p)` directly. What happens if you just try `apply f`? Can you figure out what just happened? This is a little `apply` easter egg. Why is it mathematically valid? -/ /- Definition Whatever the sets $P$ and $Q$ and $R$ are, we make an element of $\operatorname{Hom}(\operatorname{Hom}(P,\operatorname{Hom}(Q,R)), \operatorname{Hom}(\operatorname{Hom}(P,Q),\operatorname{Hom}(P,R)))$. -/ example (P Q R : Type) : (P → (Q → R)) → ((P → Q) → (P → R)) := begin intro f, intro h, intro p, have j : Q → R := f p, apply j, apply h, exact p, end
9676e7724cf485111e92206ece7ab7304398b377
48eee836fdb5c613d9a20741c17db44c8e12e61c
/src/algebra/theories/lattice.lean
8b7e3f56cff3d44861ca96a0b327e410023e4d92
[ "Apache-2.0" ]
permissive
fgdorais/lean-universal
06430443a4abe51e303e602684c2977d1f5c0834
9259b0f7fb3aa83a9e0a7a3eaa44c262e42cc9b1
refs/heads/master
1,592,479,744,136
1,589,473,399,000
1,589,473,399,000
196,287,552
1
1
null
null
null
null
UTF-8
Lean
false
false
5,296
lean
-- Copyright © 2019 François G. Dorais. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. import .basic import .semilattice namespace algebra signature lattice (α : Type*) := (join : α → α → α) (meet : α → α → α) namespace lattice_sig variables {α : Type*} (s : lattice_sig α) @[signature_instance] definition to_join_semilattice : semilattice_sig α := { op := s.join } @[signature_instance] definition to_meet_semilattice : semilattice_sig α := { op := s.meet } end lattice_sig variables {α : Type*} (s : lattice_sig α) local infix ∪ := s.join local infix ∩ := s.meet @[theory] class lattice : Prop := intro :: (join_associative : identity.op_associative s.join) (join_commutative : identity.op_commutative s.join) (join_right_absorptive : identity.op_right_absorptive s.join s.meet) (meet_associative : identity.op_associative s.meet) (meet_commutative : identity.op_commutative s.meet) (meet_right_absorptive : identity.op_right_absorptive s.meet s.join) namespace lattice variable [i : lattice s] include i @[identity_instance] theorem join_left_absorptive : identity.op_left_absorptive s.join s.meet := λ x y, calc (x ∩ y) ∪ y = y ∪ (x ∩ y) : by rw op_commutative s.join ... = y ∪ (y ∩ x) : by rw op_commutative s.meet ... = y : by rw op_right_absorptive s.join @[identity_instance] theorem meet_left_absorptive : identity.op_left_absorptive s.meet s.join := λ x y, calc (x ∪ y) ∩ y = y ∩ (x ∪ y) : by rw op_commutative s.meet ... = y ∩ (y ∪ x) : by rw op_commutative s.join ... = y : by rw op_right_absorptive s.meet @[identity_instance] theorem join_idempotent : identity.op_idempotent s.join := λ x, calc x ∪ x = x ∪ (x ∩ (x ∪ x)) : by rw op_right_absorptive s.meet ... = x : by rw op_right_absorptive s.join @[identity_instance] theorem meet_idempotent : identity.op_idempotent s.meet := λ x, calc x ∩ x = x ∩ (x ∪ (x ∩ x)) : by rw op_right_absorptive s.join ... = x : by rw op_right_absorptive s.meet instance to_join_semilattice : semilattice s.to_join_semilattice := semilattice.infer _ instance to_meet_semilattice : semilattice s.to_meet_semilattice := semilattice.infer _ end lattice @[theory] class distributive_lattice : Prop := intro :: (join_associative : identity.op_associative s.join) (join_commutative : identity.op_commutative s.join) (join_right_absorptive : identity.op_right_absorptive s.join s.meet) (meet_associative : identity.op_associative s.meet) (meet_commutative : identity.op_commutative s.meet) (meet_right_absorptive : identity.op_right_absorptive s.meet s.join) (meet_join_right_distributive : identity.op_right_distributive s.meet s.join) namespace distributive_lattice variable [i : distributive_lattice s] include i instance to_lattice : lattice s := lattice.infer _ @[identity_instance] theorem meet_join_left_distributive : identity.op_left_distributive s.meet s.join := λ x y z, calc (x ∪ y) ∩ z = z ∩ (x ∪ y) : by rw op_commutative s.meet ... = (z ∩ x) ∪ (z ∩ y) : by rw op_right_distributive s.meet s.join ... = (x ∩ z) ∪ (z ∩ y) : by rw op_commutative s.meet x z ... = (x ∩ z) ∪ (y ∩ z) : by rw op_commutative s.meet y z @[identity_instance] theorem join_meet_left_distributive : identity.op_left_distributive s.join s.meet := λ x y z, calc (x ∩ y) ∪ z = (x ∩ y) ∪ ((y ∩ z) ∪ z) : by rw op_left_absorptive s.join s.meet ... = (x ∩ y) ∪ ((z ∩ y) ∪ z) : by rw op_commutative s.meet y z ... = ((x ∩ y) ∪ (z ∩ y)) ∪ z : by rw op_associative s.join ... = ((x ∪ z) ∩ y) ∪ z : by rw op_left_distributive s.meet s.join ... = ((x ∪ z) ∩ y) ∪ ((x ∪ z) ∩ z) : by rw op_left_absorptive s.meet s.join ... = (x ∪ z) ∩ (y ∪ z) : by rw op_right_distributive s.meet s.join @[identity_instance] theorem join_meet_right_distributive : identity.op_right_distributive s.join s.meet := λ x y z, calc x ∪ (y ∩ z) = (y ∩ z) ∪ x : by rw op_commutative s.join ... = (y ∪ x) ∩ (z ∪ x) : by rw op_left_distributive s.join s.meet ... = (x ∪ y) ∩ (z ∪ x) : by rw op_commutative s.join x y ... = (x ∪ y) ∩ (x ∪ z) : by rw op_commutative s.join x z @[identity_instance] theorem join_meet_cancel_left : identity.op_op_left_cancellative s.join s.meet := λ x y z hjoin hmeet, calc y = (x ∪ y) ∩ y : by rw op_left_absorptive s.meet ... = (x ∪ z) ∩ y : by rw hjoin ... = (x ∩ y) ∪ (z ∩ y) : by rw op_left_distributive s.meet s.join ... = (x ∩ z) ∪ (z ∩ y) : by rw hmeet ... = (x ∩ z) ∪ (y ∩ z) : by rw op_commutative s.meet y z ... = (x ∪ y) ∩ z : by rw op_left_distributive s.meet s.join ... = (x ∪ z) ∩ z : by rw hjoin ... = z : by rw op_left_absorptive s.meet s.join @[identity_instance] theorem join_meet_cancel_right : identity.op_op_right_cancellative s.join s.meet := λ x z y hjoin hmeet, have hjoin' : z ∪ x = z ∪ y, from calc z ∪ x = x ∪ z : by rw op_commutative s.join ... = y ∪ z : by rw hjoin ... = z ∪ y : by rw op_commutative s.join, have hmeet' : z ∩ x = z ∩ y, from calc z ∩ x = x ∩ z : by rw op_commutative s.meet ... = y ∩ z : by rw hmeet ... = z ∩ y : by rw op_commutative s.meet, join_meet_cancel_left s hjoin' hmeet' end distributive_lattice end algebra
14882edae613d39f5eec6867a5947bd2a0b3c541
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/measure_theory/measure/measure_space_def.lean
20356a85ad03c4ae14ea41618cc9ce681e360e01
[ "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
22,651
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 -/ import measure_theory.measure.outer_measure import order.filter.countable_Inter import data.set.accumulate /-! # Measure spaces This file defines measure spaces, the almost-everywhere filter and ae_measurable functions. See `measure_theory.measure_space` for their properties and for extended documentation. Given a measurable space `α`, a measure on `α` is a function that sends measurable sets to the extended nonnegative reals that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is countably additive. This means that the measure of a countable union of pairwise disjoint sets is equal to the measure of the individual sets. Every measure can be canonically extended to an outer measure, so that it assigns values to all subsets, not just the measurable subsets. On the other hand, a measure that is countably additive on measurable sets can be restricted to measurable sets to obtain a measure. In this file a measure is defined to be an outer measure that is countably additive on measurable sets, with the additional assumption that the outer measure is the canonical extension of the restricted measure. Measures on `α` form a complete lattice, and are closed under scalar multiplication with `ℝ≥0∞`. ## Implementation notes Given `μ : measure α`, `μ s` is the value of the *outer measure* applied to `s`. This conveniently allows us to apply the measure to sets without proving that they are measurable. We get countable subadditivity for all sets, but only countable additivity for measurable sets. See the documentation of `measure_theory.measure_space` for ways to construct measures and proving that two measure are equal. A `measure_space` is a class that is a measurable space with a canonical measure. The measure is denoted `volume`. This file does not import `measure_theory.measurable_space`, but only `measurable_space_def`. ## References * <https://en.wikipedia.org/wiki/Measure_(mathematics)> * <https://en.wikipedia.org/wiki/Almost_everywhere> ## Tags measure, almost everywhere, measure space -/ noncomputable theory open classical set filter (hiding map) function measurable_space open_locale classical topological_space big_operators filter ennreal nnreal variables {α β γ δ ι : Type*} namespace measure_theory /-- A measure is defined to be an outer measure that is countably additive on measurable sets, with the additional assumption that the outer measure is the canonical extension of the restricted measure. -/ structure measure (α : Type*) [measurable_space α] extends outer_measure α := (m_Union ⦃f : ℕ → set α⦄ : (∀ i, measurable_set (f i)) → pairwise (disjoint on f) → measure_of (⋃ i, f i) = ∑' i, measure_of (f i)) (trimmed : to_outer_measure.trim = to_outer_measure) /-- Measure projections for a measure space. For measurable sets this returns the measure assigned by the `measure_of` field in `measure`. But we can extend this to _all_ sets, but using the outer measure. This gives us monotonicity and subadditivity for all sets. -/ instance measure.has_coe_to_fun [measurable_space α] : has_coe_to_fun (measure α) (λ _, set α → ℝ≥0∞) := ⟨λ m, m.to_outer_measure⟩ section variables [measurable_space α] {μ μ₁ μ₂ : measure α} {s s₁ s₂ t : set α} namespace measure /-! ### General facts about measures -/ /-- Obtain a measure by giving a countably additive function that sends `∅` to `0`. -/ def of_measurable (m : Π (s : set α), measurable_set s → ℝ≥0∞) (m0 : m ∅ measurable_set.empty = 0) (mU : ∀ {{f : ℕ → set α}} (h : ∀ i, measurable_set (f i)), pairwise (disjoint on f) → m (⋃ i, f i) (measurable_set.Union h) = ∑' i, m (f i) (h i)) : measure α := { m_Union := λ f hf hd, show induced_outer_measure m _ m0 (Union f) = ∑' i, induced_outer_measure m _ m0 (f i), begin rw [induced_outer_measure_eq m0 mU, mU hf hd], congr, funext n, rw induced_outer_measure_eq m0 mU end, trimmed := show (induced_outer_measure m _ m0).trim = induced_outer_measure m _ m0, begin unfold outer_measure.trim, congr, funext s hs, exact induced_outer_measure_eq m0 mU hs end, ..induced_outer_measure m _ m0 } lemma of_measurable_apply {m : Π (s : set α), measurable_set s → ℝ≥0∞} {m0 : m ∅ measurable_set.empty = 0} {mU : ∀ {{f : ℕ → set α}} (h : ∀ i, measurable_set (f i)), pairwise (disjoint on f) → m (⋃ i, f i) (measurable_set.Union h) = ∑' i, m (f i) (h i)} (s : set α) (hs : measurable_set s) : of_measurable m m0 mU s = m s hs := induced_outer_measure_eq m0 mU hs lemma to_outer_measure_injective : injective (to_outer_measure : measure α → outer_measure α) := λ ⟨m₁, u₁, h₁⟩ ⟨m₂, u₂, h₂⟩ h, by { congr, exact h } @[ext] lemma ext (h : ∀ s, measurable_set s → μ₁ s = μ₂ s) : μ₁ = μ₂ := to_outer_measure_injective $ by rw [← trimmed, outer_measure.trim_congr h, trimmed] lemma ext_iff : μ₁ = μ₂ ↔ ∀ s, measurable_set s → μ₁ s = μ₂ s := ⟨by { rintro rfl s hs, refl }, measure.ext⟩ end measure @[simp] lemma coe_to_outer_measure : ⇑μ.to_outer_measure = μ := rfl lemma to_outer_measure_apply (s : set α) : μ.to_outer_measure s = μ s := rfl lemma measure_eq_trim (s : set α) : μ s = μ.to_outer_measure.trim s := by rw μ.trimmed; refl lemma measure_eq_infi (s : set α) : μ s = ⨅ t (st : s ⊆ t) (ht : measurable_set t), μ t := by rw [measure_eq_trim, outer_measure.trim_eq_infi]; refl /-- A variant of `measure_eq_infi` which has a single `infi`. This is useful when applying a lemma next that only works for non-empty infima, in which case you can use `nonempty_measurable_superset`. -/ lemma measure_eq_infi' (μ : measure α) (s : set α) : μ s = ⨅ t : { t // s ⊆ t ∧ measurable_set t}, μ t := by simp_rw [infi_subtype, infi_and, subtype.coe_mk, ← measure_eq_infi] lemma measure_eq_induced_outer_measure : μ s = induced_outer_measure (λ s _, μ s) measurable_set.empty μ.empty s := measure_eq_trim _ lemma to_outer_measure_eq_induced_outer_measure : μ.to_outer_measure = induced_outer_measure (λ s _, μ s) measurable_set.empty μ.empty := μ.trimmed.symm lemma measure_eq_extend (hs : measurable_set s) : μ s = extend (λ t (ht : measurable_set t), μ t) s := (extend_eq _ hs).symm @[simp] lemma measure_empty : μ ∅ = 0 := μ.empty lemma nonempty_of_measure_ne_zero (h : μ s ≠ 0) : s.nonempty := ne_empty_iff_nonempty.1 $ λ h', h $ h'.symm ▸ measure_empty lemma measure_mono (h : s₁ ⊆ s₂) : μ s₁ ≤ μ s₂ := μ.mono h lemma measure_mono_null (h : s₁ ⊆ s₂) (h₂ : μ s₂ = 0) : μ s₁ = 0 := nonpos_iff_eq_zero.1 $ h₂ ▸ measure_mono h lemma measure_mono_top (h : s₁ ⊆ s₂) (h₁ : μ s₁ = ∞) : μ s₂ = ∞ := top_unique $ h₁ ▸ measure_mono h /-- For every set there exists a measurable superset of the same measure. -/ lemma exists_measurable_superset (μ : measure α) (s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = μ s := by simpa only [← measure_eq_trim] using μ.to_outer_measure.exists_measurable_superset_eq_trim s /-- For every set `s` and a countable collection of measures `μ i` there exists a measurable superset `t ⊇ s` such that each measure `μ i` takes the same value on `s` and `t`. -/ lemma exists_measurable_superset_forall_eq {ι} [encodable ι] (μ : ι → measure α) (s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ ∀ i, μ i t = μ i s := by simpa only [← measure_eq_trim] using outer_measure.exists_measurable_superset_forall_eq_trim (λ i, (μ i).to_outer_measure) s lemma exists_measurable_superset₂ (μ ν : measure α) (s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = μ s ∧ ν t = ν s := by simpa only [bool.forall_bool.trans and.comm] using exists_measurable_superset_forall_eq (λ b, cond b μ ν) s lemma exists_measurable_superset_of_null (h : μ s = 0) : ∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = 0 := h ▸ exists_measurable_superset μ s lemma exists_measurable_superset_iff_measure_eq_zero : (∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = 0) ↔ μ s = 0 := ⟨λ ⟨t, hst, _, ht⟩, measure_mono_null hst ht, exists_measurable_superset_of_null⟩ theorem measure_Union_le [encodable β] (s : β → set α) : μ (⋃ i, s i) ≤ ∑' i, μ (s i) := μ.to_outer_measure.Union _ lemma measure_bUnion_le {s : set β} (hs : countable s) (f : β → set α) : μ (⋃ b ∈ s, f b) ≤ ∑' p : s, μ (f p) := begin haveI := hs.to_encodable, rw [bUnion_eq_Union], apply measure_Union_le end lemma measure_bUnion_finset_le (s : finset β) (f : β → set α) : μ (⋃ b ∈ s, f b) ≤ ∑ p in s, μ (f p) := begin rw [← finset.sum_attach, finset.attach_eq_univ, ← tsum_fintype], exact measure_bUnion_le s.countable_to_set f end lemma measure_Union_fintype_le [fintype β] (f : β → set α) : μ (⋃ b, f b) ≤ ∑ p, μ (f p) := by { convert measure_bUnion_finset_le finset.univ f, simp } lemma measure_bUnion_lt_top {s : set β} {f : β → set α} (hs : finite s) (hfin : ∀ i ∈ s, μ (f i) ≠ ∞) : μ (⋃ i ∈ s, f i) < ∞ := begin convert (measure_bUnion_finset_le hs.to_finset f).trans_lt _, { ext, rw [finite.mem_to_finset] }, apply ennreal.sum_lt_top, simpa only [finite.mem_to_finset] end lemma measure_Union_null [encodable β] {s : β → set α} : (∀ i, μ (s i) = 0) → μ (⋃ i, s i) = 0 := μ.to_outer_measure.Union_null @[simp] lemma measure_Union_null_iff [encodable ι] {s : ι → set α} : μ (⋃ i, s i) = 0 ↔ ∀ i, μ (s i) = 0 := μ.to_outer_measure.Union_null_iff lemma measure_bUnion_null_iff {s : set ι} (hs : countable s) {t : ι → set α} : μ (⋃ i ∈ s, t i) = 0 ↔ ∀ i ∈ s, μ (t i) = 0 := μ.to_outer_measure.bUnion_null_iff hs lemma measure_sUnion_null_iff {S : set (set α)} (hS : countable S) : μ (⋃₀ S) = 0 ↔ ∀ s ∈ S, μ s = 0 := μ.to_outer_measure.sUnion_null_iff hS theorem measure_union_le (s₁ s₂ : set α) : μ (s₁ ∪ s₂) ≤ μ s₁ + μ s₂ := μ.to_outer_measure.union _ _ lemma measure_union_null : μ s₁ = 0 → μ s₂ = 0 → μ (s₁ ∪ s₂) = 0 := μ.to_outer_measure.union_null @[simp] lemma measure_union_null_iff : μ (s₁ ∪ s₂) = 0 ↔ μ s₁ = 0 ∧ μ s₂ = 0:= ⟨λ h, ⟨measure_mono_null (subset_union_left _ _) h, measure_mono_null (subset_union_right _ _) h⟩, λ h, measure_union_null h.1 h.2⟩ lemma measure_union_lt_top (hs : μ s < ∞) (ht : μ t < ∞) : μ (s ∪ t) < ∞ := (measure_union_le s t).trans_lt (ennreal.add_lt_top.mpr ⟨hs, ht⟩) @[simp] lemma measure_union_lt_top_iff : μ (s ∪ t) < ∞ ↔ μ s < ∞ ∧ μ t < ∞ := begin refine ⟨λ h, ⟨_, _⟩, λ h, measure_union_lt_top h.1 h.2⟩, { exact (measure_mono (set.subset_union_left s t)).trans_lt h, }, { exact (measure_mono (set.subset_union_right s t)).trans_lt h, }, end lemma measure_union_ne_top (hs : μ s ≠ ∞) (ht : μ t ≠ ∞) : μ (s ∪ t) ≠ ∞ := (measure_union_lt_top hs.lt_top ht.lt_top).ne @[simp] lemma measure_union_eq_top_iff : μ (s ∪ t) = ∞ ↔ μ s = ∞ ∨ μ t = ∞ := not_iff_not.1 $ by simp only [← lt_top_iff_ne_top, ← ne.def, not_or_distrib, measure_union_lt_top_iff] lemma exists_measure_pos_of_not_measure_Union_null [encodable β] {s : β → set α} (hs : μ (⋃ n, s n) ≠ 0) : ∃ n, 0 < μ (s n) := begin by_contra' h, simp_rw nonpos_iff_eq_zero at h, exact hs (measure_Union_null h), end lemma measure_inter_lt_top_of_left_ne_top (hs_finite : μ s ≠ ∞) : μ (s ∩ t) < ∞ := (measure_mono (set.inter_subset_left s t)).trans_lt hs_finite.lt_top lemma measure_inter_lt_top_of_right_ne_top (ht_finite : μ t ≠ ∞) : μ (s ∩ t) < ∞ := inter_comm t s ▸ measure_inter_lt_top_of_left_ne_top ht_finite lemma measure_inter_null_of_null_right (S : set α) {T : set α} (h : μ T = 0) : μ (S ∩ T) = 0 := measure_mono_null (inter_subset_right S T) h lemma measure_inter_null_of_null_left {S : set α} (T : set α) (h : μ S = 0) : μ (S ∩ T) = 0 := measure_mono_null (inter_subset_left S T) h /-! ### The almost everywhere filter -/ /-- The “almost everywhere” filter of co-null sets. -/ def measure.ae {α} {m : measurable_space α} (μ : measure α) : filter α := { sets := {s | μ sᶜ = 0}, univ_sets := by simp, inter_sets := λ s t hs ht, by simp only [compl_inter, mem_set_of_eq]; exact measure_union_null hs ht, sets_of_superset := λ s t hs hst, measure_mono_null (set.compl_subset_compl.2 hst) hs } notation `∀ᵐ` binders ` ∂` μ `, ` r:(scoped P, filter.eventually P (measure.ae μ)) := r notation `∃ᵐ` binders ` ∂` μ `, ` r:(scoped P, filter.frequently P (measure.ae μ)) := r notation f ` =ᵐ[`:50 μ:50 `] `:0 g:50 := f =ᶠ[measure.ae μ] g notation f ` ≤ᵐ[`:50 μ:50 `] `:0 g:50 := f ≤ᶠ[measure.ae μ] g lemma mem_ae_iff {s : set α} : s ∈ μ.ae ↔ μ sᶜ = 0 := iff.rfl lemma ae_iff {p : α → Prop} : (∀ᵐ a ∂ μ, p a) ↔ μ { a | ¬ p a } = 0 := iff.rfl lemma compl_mem_ae_iff {s : set α} : sᶜ ∈ μ.ae ↔ μ s = 0 := by simp only [mem_ae_iff, compl_compl] lemma frequently_ae_iff {p : α → Prop} : (∃ᵐ a ∂μ, p a) ↔ μ {a | p a} ≠ 0 := not_congr compl_mem_ae_iff lemma frequently_ae_mem_iff {s : set α} : (∃ᵐ a ∂μ, a ∈ s) ↔ μ s ≠ 0 := not_congr compl_mem_ae_iff lemma measure_zero_iff_ae_nmem {s : set α} : μ s = 0 ↔ ∀ᵐ a ∂ μ, a ∉ s := compl_mem_ae_iff.symm lemma ae_of_all {p : α → Prop} (μ : measure α) : (∀ a, p a) → ∀ᵐ a ∂ μ, p a := eventually_of_forall --instance ae_is_measurably_generated : is_measurably_generated μ.ae := --⟨λ s hs, let ⟨t, hst, htm, htμ⟩ := exists_measurable_superset_of_null hs in -- ⟨tᶜ, compl_mem_ae_iff.2 htμ, htm.compl, compl_subset_comm.1 hst⟩⟩ instance : countable_Inter_filter μ.ae := ⟨begin intros S hSc hS, simp only [mem_ae_iff, compl_sInter, sUnion_image, bUnion_eq_Union] at hS ⊢, haveI := hSc.to_encodable, exact measure_Union_null (subtype.forall.2 hS) end⟩ lemma ae_imp_iff {p : α → Prop} {q : Prop} : (∀ᵐ x ∂μ, q → p x) ↔ (q → ∀ᵐ x ∂μ, p x) := filter.eventually_imp_distrib_left lemma ae_all_iff [encodable ι] {p : α → ι → Prop} : (∀ᵐ a ∂ μ, ∀ i, p a i) ↔ (∀ i, ∀ᵐ a ∂ μ, p a i) := eventually_countable_forall lemma ae_ball_iff {S : set ι} (hS : countable S) {p : Π (x : α) (i ∈ S), Prop} : (∀ᵐ x ∂ μ, ∀ i ∈ S, p x i ‹_›) ↔ ∀ i ∈ S, ∀ᵐ x ∂ μ, p x i ‹_› := eventually_countable_ball hS lemma ae_eq_refl (f : α → δ) : f =ᵐ[μ] f := eventually_eq.rfl lemma ae_eq_symm {f g : α → δ} (h : f =ᵐ[μ] g) : g =ᵐ[μ] f := h.symm lemma ae_eq_trans {f g h: α → δ} (h₁ : f =ᵐ[μ] g) (h₂ : g =ᵐ[μ] h) : f =ᵐ[μ] h := h₁.trans h₂ lemma ae_le_of_ae_lt {f g : α → ℝ≥0∞} (h : ∀ᵐ x ∂μ, f x < g x) : f ≤ᵐ[μ] g := begin rw [filter.eventually_le, ae_iff], rw ae_iff at h, refine measure_mono_null (λ x hx, _) h, exact not_lt.2 (le_of_lt (not_le.1 hx)), end @[simp] lemma ae_eq_empty : s =ᵐ[μ] (∅ : set α) ↔ μ s = 0 := eventually_eq_empty.trans $ by simp only [ae_iff, not_not, set_of_mem_eq] @[simp] lemma ae_eq_univ : s =ᵐ[μ] (univ : set α) ↔ μ sᶜ = 0 := eventually_eq_univ lemma ae_le_set : s ≤ᵐ[μ] t ↔ μ (s \ t) = 0 := calc s ≤ᵐ[μ] t ↔ ∀ᵐ x ∂μ, x ∈ s → x ∈ t : iff.rfl ... ↔ μ (s \ t) = 0 : by simp [ae_iff]; refl lemma ae_le_set_inter {s' t' : set α} (h : s ≤ᵐ[μ] t) (h' : s' ≤ᵐ[μ] t') : (s ∩ s' : set α) ≤ᵐ[μ] (t ∩ t' : set α) := h.inter h' @[simp] lemma union_ae_eq_right : (s ∪ t : set α) =ᵐ[μ] t ↔ μ (s \ t) = 0 := by simp [eventually_le_antisymm_iff, ae_le_set, union_diff_right, diff_eq_empty.2 (set.subset_union_right _ _)] lemma diff_ae_eq_self : (s \ t : set α) =ᵐ[μ] s ↔ μ (s ∩ t) = 0 := by simp [eventually_le_antisymm_iff, ae_le_set, diff_diff_right, diff_diff, diff_eq_empty.2 (set.subset_union_right _ _)] lemma diff_null_ae_eq_self (ht : μ t = 0) : (s \ t : set α) =ᵐ[μ] s := diff_ae_eq_self.mpr (measure_mono_null (inter_subset_right _ _) ht) lemma ae_eq_set {s t : set α} : s =ᵐ[μ] t ↔ μ (s \ t) = 0 ∧ μ (t \ s) = 0 := by simp [eventually_le_antisymm_iff, ae_le_set] lemma ae_eq_set_inter {s' t' : set α} (h : s =ᵐ[μ] t) (h' : s' =ᵐ[μ] t') : (s ∩ s' : set α) =ᵐ[μ] (t ∩ t' : set α) := h.inter h' @[to_additive] lemma _root_.set.mul_indicator_ae_eq_one {M : Type*} [has_one M] {f : α → M} {s : set α} (h : s.mul_indicator f =ᵐ[μ] 1) : μ (s ∩ function.mul_support f) = 0 := begin rw [filter.eventually_eq, ae_iff] at h, convert h, ext a, rw ← set.mul_indicator_eq_one_iff, refl end /-- If `s ⊆ t` modulo a set of measure `0`, then `μ s ≤ μ t`. -/ @[mono] lemma measure_mono_ae (H : s ≤ᵐ[μ] t) : μ s ≤ μ t := calc μ s ≤ μ (s ∪ t) : measure_mono $ subset_union_left s t ... = μ (t ∪ s \ t) : by rw [union_diff_self, set.union_comm] ... ≤ μ t + μ (s \ t) : measure_union_le _ _ ... = μ t : by rw [ae_le_set.1 H, add_zero] alias measure_mono_ae ← filter.eventually_le.measure_le /-- If two sets are equal modulo a set of measure zero, then `μ s = μ t`. -/ lemma measure_congr (H : s =ᵐ[μ] t) : μ s = μ t := le_antisymm H.le.measure_le H.symm.le.measure_le alias measure_congr ← filter.eventually_eq.measure_eq lemma measure_mono_null_ae (H : s ≤ᵐ[μ] t) (ht : μ t = 0) : μ s = 0 := nonpos_iff_eq_zero.1 $ ht ▸ H.measure_le /-- A measurable set `t ⊇ s` such that `μ t = μ s`. It even satisfies `μ (t ∩ u) = μ (s ∩ u)` for any measurable set `u` if `μ s ≠ ∞`, see `measure_to_measurable_inter`. (This property holds without the assumption `μ s ≠ ∞` when the space is sigma-finite, see `measure_to_measurable_inter_of_sigma_finite`). If `s` is a null measurable set, then we also have `t =ᵐ[μ] s`, see `null_measurable_set.to_measurable_ae_eq`. This notion is sometimes called a "measurable hull" in the literature. -/ @[irreducible] def to_measurable (μ : measure α) (s : set α) : set α := if h : ∃ t ⊇ s, measurable_set t ∧ t =ᵐ[μ] s then h.some else if h' : ∃ t ⊇ s, measurable_set t ∧ (∀ u, measurable_set u → μ (t ∩ u) = μ (s ∩ u)) then h'.some else (exists_measurable_superset μ s).some lemma subset_to_measurable (μ : measure α) (s : set α) : s ⊆ to_measurable μ s := begin rw to_measurable, split_ifs with hs h's, exacts [hs.some_spec.fst, h's.some_spec.fst, (exists_measurable_superset μ s).some_spec.1] end lemma ae_le_to_measurable : s ≤ᵐ[μ] to_measurable μ s := (subset_to_measurable _ _).eventually_le @[simp] lemma measurable_set_to_measurable (μ : measure α) (s : set α) : measurable_set (to_measurable μ s) := begin rw to_measurable, split_ifs with hs h's, exacts [hs.some_spec.snd.1, h's.some_spec.snd.1, (exists_measurable_superset μ s).some_spec.2.1] end @[simp] lemma measure_to_measurable (s : set α) : μ (to_measurable μ s) = μ s := begin rw to_measurable, split_ifs with hs h's, { exact measure_congr hs.some_spec.snd.2 }, { simpa only [inter_univ] using h's.some_spec.snd.2 univ measurable_set.univ }, { exact (exists_measurable_superset μ s).some_spec.2.2 } end /-- A measure space is a measurable space equipped with a measure, referred to as `volume`. -/ class measure_space (α : Type*) extends measurable_space α := (volume : measure α) export measure_space (volume) /-- `volume` is the canonical measure on `α`. -/ add_decl_doc volume section measure_space notation `∀ᵐ` binders `, ` r:(scoped P, filter.eventually P (measure_theory.measure.ae measure_theory.measure_space.volume)) := r notation `∃ᵐ` binders `, ` r:(scoped P, filter.frequently P (measure_theory.measure.ae measure_theory.measure_space.volume)) := r /-- The tactic `exact volume`, to be used in optional (`auto_param`) arguments. -/ meta def volume_tac : tactic unit := `[exact measure_theory.measure_space.volume] end measure_space end end measure_theory section open measure_theory /-! # Almost everywhere measurable functions A function is almost everywhere measurable if it coincides almost everywhere with a measurable function. We define this property, called `ae_measurable f μ`. It's properties are discussed in `measure_theory.measure_space`. -/ variables {m : measurable_space α} [measurable_space β] {f g : α → β} {μ ν : measure α} /-- A function is almost everywhere measurable if it coincides almost everywhere with a measurable function. -/ def ae_measurable {m : measurable_space α} (f : α → β) (μ : measure α . measure_theory.volume_tac) : Prop := ∃ g : α → β, measurable g ∧ f =ᵐ[μ] g lemma measurable.ae_measurable (h : measurable f) : ae_measurable f μ := ⟨f, h, ae_eq_refl f⟩ namespace ae_measurable /-- Given an almost everywhere measurable function `f`, associate to it a measurable function that coincides with it almost everywhere. `f` is explicit in the definition to make sure that it shows in pretty-printing. -/ def mk (f : α → β) (h : ae_measurable f μ) : α → β := classical.some h lemma measurable_mk (h : ae_measurable f μ) : measurable (h.mk f) := (classical.some_spec h).1 lemma ae_eq_mk (h : ae_measurable f μ) : f =ᵐ[μ] (h.mk f) := (classical.some_spec h).2 lemma congr (hf : ae_measurable f μ) (h : f =ᵐ[μ] g) : ae_measurable g μ := ⟨hf.mk f, hf.measurable_mk, h.symm.trans hf.ae_eq_mk⟩ end ae_measurable lemma ae_measurable_congr (h : f =ᵐ[μ] g) : ae_measurable f μ ↔ ae_measurable g μ := ⟨λ hf, ae_measurable.congr hf h, λ hg, ae_measurable.congr hg h.symm⟩ @[simp] lemma ae_measurable_const {b : β} : ae_measurable (λ a : α, b) μ := measurable_const.ae_measurable lemma ae_measurable_id : ae_measurable id μ := measurable_id.ae_measurable lemma ae_measurable_id' : ae_measurable (λ x, x) μ := measurable_id.ae_measurable lemma measurable.comp_ae_measurable [measurable_space δ] {f : α → δ} {g : δ → β} (hg : measurable g) (hf : ae_measurable f μ) : ae_measurable (g ∘ f) μ := ⟨g ∘ hf.mk f, hg.comp hf.measurable_mk, eventually_eq.fun_comp hf.ae_eq_mk _⟩ end
a214ba4fc29c63208a928474a8f4215e6d3d792f
ba4794a0deca1d2aaa68914cd285d77880907b5c
/src/game/world10/level11.lean
0d45b49cdef1b63c1e3253fdab334906aee5147b
[ "Apache-2.0" ]
permissive
ChrisHughes24/natural_number_game
c7c00aa1f6a95004286fd456ed13cf6e113159ce
9d09925424da9f6275e6cfe427c8bcf12bb0944f
refs/heads/master
1,600,715,773,528
1,573,910,462,000
1,573,910,462,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
569
lean
import game.world10.level10 -- hide namespace mynat -- hide /- # Inequality world. ## Level 11: `add_le_add_right` If you're faced with a goal of the form `forall t, ...`, then the next line is "so let $t$ be arbitrary". The way to do this in Lean is `intro t`. -/ /- Lemma For all naturals $a$ and $b$, $a\le b$ implies that for all naturals $t$, $a+t\le b+t$. -/ theorem add_le_add_right (a b : mynat) : a ≤ b → ∀ t, (a + t) ≤ (b + t) := begin [less_leaky] intro h, cases h with c hc, intro t, use c, rw hc, ring, end end mynat -- hide
b89a6cc952094b7814065d067412138ab6515ecf
a4673261e60b025e2c8c825dfa4ab9108246c32e
/stage0/src/Lean/Elab/Level.lean
f3413ef504ea78a420edf3a73137d49a70b3cab1
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,424
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.LevelDefEq import Lean.Elab.Exception import Lean.Elab.Log namespace Lean.Elab.Level structure Context := (ref : Syntax) (levelNames : List Name) structure State := (ngen : NameGenerator) (mctx : MetavarContext) abbrev LevelElabM := ReaderT Context (EStateM Exception State) instance : MonadRef LevelElabM := { getRef := return (← read).ref, withRef := fun ref x => withReader (fun ctx => { ctx with ref := ref }) x } instance : AddMessageContext LevelElabM := { addMessageContext := fun msg => pure msg } instance : MonadNameGenerator LevelElabM := { getNGen := return (← get).ngen, setNGen := fun ngen => modify fun s => { s with ngen := ngen } } def mkFreshLevelMVar : LevelElabM Level := do let mvarId ← mkFreshId modify fun s => { s with mctx := s.mctx.addLevelMVarDecl mvarId } return mkLevelMVar mvarId partial def elabLevel (stx : Syntax) : LevelElabM Level := withRef stx do let kind := stx.getKind if kind == `Lean.Parser.Level.paren then elabLevel (stx.getArg 1) else if kind == `Lean.Parser.Level.max then let args := stx.getArg 1 |>.getArgs let mut lvl ← elabLevel args.back for arg in args[:args.size-1] do let arg ← elabLevel arg lvl := mkLevelMax' lvl arg return lvl else if kind == `Lean.Parser.Level.imax then let args := stx.getArg 1 |>.getArgs let mut lvl ← elabLevel args.back for arg in args[:args.size-1] do let arg ← elabLevel arg lvl := mkLevelIMax' lvl arg return lvl else if kind == `Lean.Parser.Level.hole then mkFreshLevelMVar else if kind == numLitKind then match stx.isNatLit? with | some val => return Level.ofNat val | none => throwIllFormedSyntax else if kind == identKind then let paramName := stx.getId unless (← read).levelNames.contains paramName do throwError ("unknown universe level " ++ paramName) return mkLevelParam paramName else if kind == `Lean.Parser.Level.addLit then let lvl ← elabLevel (stx.getArg 0) match stx.getArg 2 |>.isNatLit? with | some val => return lvl.addOffset val | none => throwIllFormedSyntax else throwError "unexpected universe level syntax kind" end Lean.Elab.Level
61810049ab4958b64943ca5e0ca72bc25c5ae5d4
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/Lake/Config/LeanLib.lean
89f2819602a53e99e3866a7becf5c18528a30a56
[ "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
4,531
lean
/- Copyright (c) 2022 Mac Malone. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mac Malone -/ import Lake.Config.Package namespace Lake open Lean System /-- A Lean library -- its package plus its configuration. -/ structure LeanLib where /-- The package the library belongs to. -/ pkg : Package /-- The library's user-defined configuration. -/ config : LeanLibConfig /-- The Lean libraries of the package (as an Array). -/ @[inline] def Package.leanLibs (self : Package) : Array LeanLib := self.leanLibConfigs.fold (fun a _ v => a.push (⟨self, v⟩)) #[] /-- Try to find a Lean library in the package with the given name. -/ @[inline] def Package.findLeanLib? (name : Name) (self : Package) : Option LeanLib := self.leanLibConfigs.find? name |>.map (⟨self, ·⟩) namespace LeanLib /-- The library's well-formed name. -/ @[inline] def name (self : LeanLib) : Name := self.config.name /-- The package's `srcDir` joined with the library's `srcDir`. -/ @[inline] def srcDir (self : LeanLib) : FilePath := self.pkg.srcDir / self.config.srcDir /-- The library's root directory for `lean` (i.e., `srcDir`). -/ @[inline] def rootDir (self : LeanLib) : FilePath := self.srcDir /-- The names of the library's root modules (i.e., the library's `roots` configuration). -/ @[inline] def roots (self : LeanLib) : Array Name := self.config.roots /-- Whether the given module is considered local to the library. -/ @[inline] def isLocalModule (mod : Name) (self : LeanLib) : Bool := self.config.isLocalModule mod /-- Whether the given module is a buildable part of the library. -/ @[inline] def isBuildableModule (mod : Name) (self : LeanLib) : Bool := self.config.isBuildableModule mod /-- The file name of the library's static binary (i.e., its `.a`) -/ @[inline] def staticLibFileName (self : LeanLib) : FilePath := nameToStaticLib self.config.libName /-- The path to the static library in the package's `libDir`. -/ @[inline] def staticLibFile (self : LeanLib) : FilePath := self.pkg.nativeLibDir / self.staticLibFileName /-- The file name of the library's shared binary (i.e., its `dll`, `dylib`, or `so`) . -/ @[inline] def sharedLibFileName (self : LeanLib) : FilePath := nameToSharedLib self.config.libName /-- The path to the shared library in the package's `libDir`. -/ @[inline] def sharedLibFile (self : LeanLib) : FilePath := self.pkg.nativeLibDir / self.sharedLibFileName /-- The library's `extraDepTargets` configuration. -/ @[inline] def extraDepTargets (self : LeanLib) := self.config.extraDepTargets /-- Whether to precompile the library's modules. Is true if either the package or the library have `precompileModules` set. -/ @[inline] def precompileModules (self : LeanLib) : Bool := self.pkg.precompileModules || self.config.precompileModules /-- The library's `defaultFacets` configuration. -/ @[inline] def defaultFacets (self : LeanLib) : Array Name := self.config.defaultFacets /-- The library's `nativeFacets` configuration. -/ @[inline] def nativeFacets (self : LeanLib) : Array (ModuleFacet (BuildJob FilePath)) := self.config.nativeFacets /-- The build type for modules of this library. That is, the minimum of package's `buildType` and the library's `buildType`. -/ @[inline] def buildType (self : LeanLib) : BuildType := min self.pkg.buildType self.config.buildType /-- The arguments to pass to `lean` when compiling the library's Lean files. That is, the package's `moreLeanArgs` plus the library's `moreLeanArgs`. -/ @[inline] def leanArgs (self : LeanLib) : Array String := self.pkg.moreLeanArgs ++ self.config.moreLeanArgs /-- The arguments to weakly pass to `lean` when compiling the library's Lean files. That is, the package's `weakLeanArgs` plus the library's `weakLeanArgs`. -/ @[inline] def weakLeanArgs (self : LeanLib) : Array String := self.pkg.weakLeanArgs ++ self.config.weakLeanArgs /-- The arguments to pass to `leanc` when compiling the library's C files. That is, the build type's `leancArgs`, the package's `moreLeancArgs`, and then the library's `moreLeancArgs`. -/ @[inline] def leancArgs (self : LeanLib) : Array String := self.buildType.leancArgs ++ self.pkg.moreLeancArgs ++ self.config.moreLeancArgs /-- The arguments to pass to `leanc` when linking the shared library. That is, the package's `moreLinkArgs` plus the library's `moreLinkArgs`. -/ @[inline] def linkArgs (self : LeanLib) : Array String := self.pkg.moreLinkArgs ++ self.config.moreLinkArgs
29e983f31262793968cf519ffafd99b1322e52bc
4b846d8dabdc64e7ea03552bad8f7fa74763fc67
/library/tools/mini_crush/default.lean
995def3a2284161377fa3dc9840b93bca03025fc
[ "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
8,646
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura We implement a crush-like strategy using simplifier, SMT gadgets, and robust simplifier. This is just a demo. -/ declare_trace mini_crush namespace mini_crush open tactic meta def size (e : expr) : nat := e^.fold 1 (λ e _ n, n+1) /- Collect relevant functions -/ meta def is_auto_construction : name → bool | (name.mk_string "brec_on" p) := tt | (name.mk_string "cases_on" p) := tt | (name.mk_string "rec_on" p) := tt | (name.mk_string "no_confusion" p) := tt | (name.mk_string "below" p) := tt | _ := ff meta def is_relevant_fn (n : name) : tactic bool := do env ← get_env, if ¬env^.is_definition n ∨ is_auto_construction n then return ff else if env^.in_current_file n then return tt else in_open_namespaces n meta def collect_revelant_fns_aux : name_set → expr → tactic name_set | s e := e^.mfold s $ λ t _ s, match t with | expr.const c _ := if s^.contains c then return s else mcond (is_relevant_fn c) (do new_s ← return $ if c^.is_internal then s else s^.insert c, d ← get_decl c, collect_revelant_fns_aux new_s d^.value) (return s) | _ := return s end meta def collect_revelant_fns : tactic name_set := do ctx ← local_context, s₁ ← ctx^.mfoldl (λ s e, infer_type e >>= collect_revelant_fns_aux s) mk_name_set, target >>= collect_revelant_fns_aux s₁ meta def add_relevant_eqns (s : simp_lemmas) : tactic simp_lemmas := do fns ← collect_revelant_fns, fns^.mfold s (λ fn s, get_eqn_lemmas_for tt fn >>= mfoldl simp_lemmas.add_simp s) meta def add_relevant_eqns_h (hs : hinst_lemmas) : tactic hinst_lemmas := do fns ← collect_revelant_fns, fns^.mfold hs (λ fn hs, get_eqn_lemmas_for tt fn >>= mfoldl (λ hs d, hs^.add <$> hinst_lemma.mk_from_decl d) hs) /- Collect terms that are inductive datatypes -/ meta def is_inductive (e : expr) : tactic bool := do type ← infer_type e, C ← return type^.get_app_fn, env ← get_env, return $ C^.is_constant && env^.is_inductive C^.const_name meta def collect_inductive_aux : expr_set → expr → tactic expr_set | S e := if S^.contains e then return S else do new_S ← mcond (is_inductive e) (return $ S^.insert e) (return S), match e with | expr.app _ _ := fold_explicit_args e new_S collect_inductive_aux | expr.pi _ _ d b := if e^.is_arrow then collect_inductive_aux S d >>= flip collect_inductive_aux b else return new_S | _ := return new_S end meta def collect_inductive : expr → tactic expr_set := collect_inductive_aux mk_expr_set meta def collect_inductive_from_target : tactic (list expr) := do S ← target >>= collect_inductive, return $ list.qsort (λ e₁ e₂, size e₁ < size e₂) $ S^.to_list meta def collect_inductive_hyps : tactic (list expr) := local_context >>= mfoldl (λ r h, mcond (is_inductive h) (return $ h::r) (return r)) [] /- Induction -/ meta def try_induction_aux (cont : expr → tactic unit) : list expr → tactic unit | [] := failed | (e::es) := (step (induction e); cont e; now) <|> try_induction_aux es meta def try_induction (cont : expr → tactic unit) : tactic unit := focus1 $ collect_inductive_hyps >>= try_induction_aux cont /- Trace messages -/ meta def report {α : Type} [has_to_tactic_format α] (a : α) : tactic unit := when_tracing `mini_crush $ trace a meta def report_failure (s : name) (e : option expr := none) : tactic unit := when_tracing `mini_crush $ match e with | none := trace ("FAILED '" ++ to_string s ++ "' at") | some e := (do p ← pp e, trace (to_fmt "FAILED '" ++ to_fmt s ++ "' processing '" ++ p ++ to_fmt "' at")) <|> trace ("FAILED '" ++ to_string s ++ "' at") end >> trace_state >> trace "--------------" /- Simple tactic -/ meta def close_aux (hs : hinst_lemmas) : tactic unit := triv <|> reflexivity reducible <|> contradiction <|> try_for 100 (rsimp {} hs >> now) <|> try_for 100 reflexivity meta def close (hs : hinst_lemmas) (s : name) (e : option expr) : tactic unit := now <|> close_aux hs <|> report_failure s e >> failed meta def simple (s : simp_lemmas) (hs : hinst_lemmas) (cfg : simp_config) (s_name : name) (h : option expr) : tactic unit := simph_intros_using s cfg >> close hs s_name h /- Best first search -/ meta def snapshot := tactic_state meta def save : tactic snapshot := tactic.read meta def restore : snapshot → tactic unit := tactic.write meta def try_snapshots {α} (cont : α → tactic unit) : list (α × snapshot) → tactic unit | [] := failed | ((a, s)::ss) := (restore s >> cont a >> now) <|> try_snapshots ss meta def search {α} (max_depth : nat) (act : nat → α → tactic (list (α × snapshot))) : nat → α → tactic unit | n s := do now <|> if n > max_depth then when_tracing `mini_crush (trace "max depth reached" >> trace_state) >> failed else all_goals $ try intros >> act n s >>= try_snapshots (search (n+1)) meta def try_and_save {α} (t : tactic α) : tactic (option (α × nat × snapshot)) := do { s ← save, a ← t, new_s ← save, n ← num_goals, restore s, return (a, n, new_s) } <|> return none meta def try_all_aux {α β : Type} (ts : α → tactic β) : list α → list (α × β × nat × snapshot) → tactic (list (α × β × nat × snapshot)) | [] [] := failed | [] rs := return rs^.reverse | (v::vs) rs := do r ← try_and_save (ts v), match r with | some (b, 0, s) := return [(v, b, 0, s)] | some (b, n, s) := try_all_aux vs ((v, b, n, s)::rs) | none := try_all_aux vs rs end meta def try_all {α β : Type} (ts : α → tactic β) (vs : list α) : tactic (list (α × β × nat × snapshot)) := try_all_aux ts vs [] /- Destruct and simplify -/ meta def try_cases (s : simp_lemmas) (hs : hinst_lemmas) (cfg : simp_config) (s_name : name) : tactic (list (unit × snapshot)) := do es ← collect_inductive_from_target, rs ← try_all (λ e, do when_tracing `mini_crush (do p ← pp e, trace (to_fmt "Splitting on '" ++ p ++ to_fmt "'")), cases e; simph_intros_using s cfg; try (close_aux hs)) es, rs ← return $ flip list.qsort rs (λ ⟨e₁, _, n₁, _⟩ ⟨e₂, _, n₂, _⟩, if n₁ ≠ n₂ then n₁ < n₂ else size e₁ < size e₂), return $ rs^.map (λ ⟨_, _, _, s⟩, ((), s)) meta def search_cases (max_depth : nat) (s : simp_lemmas) (hs : hinst_lemmas) (cfg : simp_config) (s_name : name) : tactic unit := search max_depth (λ d _, do when_tracing `mini_crush (trace ("Depth #" ++ to_string d)), try_cases s hs cfg s_name) 0 () /- Strategies -/ meta def mk_simp_lemmas (s : option simp_lemmas := none) : tactic simp_lemmas := match s with | some s := return s | none := simp_lemmas.mk_default >>= add_relevant_eqns end meta def mk_hinst_lemmas (s : option hinst_lemmas := none) : tactic hinst_lemmas := match s with | some s := return s | none := add_relevant_eqns_h hinst_lemmas.mk end meta def strategy_1 (cfg : simp_config := {}) (s : option simp_lemmas := none) (hs : option hinst_lemmas := none) (s_name : name := "strategy 1") : tactic unit := do s ← mk_simp_lemmas s, hs ← mk_hinst_lemmas hs, try_induction (λ h, simple s hs cfg s_name (some h)) meta def strategy_2_aux (cfg : simp_config) (hs : hinst_lemmas) : simp_lemmas → tactic unit | s := do s ← simp_intro_aux cfg tt s tt [`_], -- Introduce next hypothesis h ← list.ilast <$> local_context, try $ solve1 (mwhen (is_inductive h) $ induction' h; simple s hs cfg "strategy 2" (some h)), now <|> strategy_2_aux s meta def strategy_2 (cfg : simp_config := {}) (s : option simp_lemmas := none) (hs : option hinst_lemmas := none) : tactic unit := do s ← mk_simp_lemmas s, hs ← mk_hinst_lemmas hs, strategy_2_aux cfg hs s meta def strategy_3 (cfg : simp_config := {}) (max_depth : nat := 1) (s : option simp_lemmas := none) (hs : option hinst_lemmas := none) : tactic unit := do s ← mk_simp_lemmas s, hs ← mk_hinst_lemmas hs, try_induction (λ h, try (simph_intros_using s cfg); try (close_aux hs); (now <|> search_cases max_depth s hs cfg "strategy 3")) end mini_crush open tactic mini_crush meta def mini_crush (cfg : simp_config := {}) (max_depth : nat := 1) := do s ← mk_simp_lemmas, hs ← mk_hinst_lemmas, strategy_1 cfg (some s) (some hs) <|> strategy_2 cfg (some s) (some hs) <|> strategy_3 cfg max_depth (some s) (some hs)
33649ba3da68084832d0419b24928be5d64d5468
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/algebra/field_power.lean
a9a1681808edbbeea8b6246a6397c6c48e74d8fb
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,784
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 algebra.group_with_zero.power import tactic.linarith import data.equiv.ring /-! # Integer power operation on fields and division rings This file collects basic facts about the operation of raising an element of a `division_ring` to an integer power. More specialised results are provided in the case of a linearly ordered field. -/ universe u @[simp] lemma ring_hom.map_fpow {K L : Type*} [division_ring K] [division_ring L] (f : K →+* L) : ∀ (a : K) (n : ℤ), f (a ^ n) = f a ^ n := f.to_monoid_with_zero_hom.map_fpow @[simp] lemma ring_equiv.map_fpow {K L : Type*} [division_ring K] [division_ring L] (f : K ≃+* L) : ∀ (a : K) (n : ℤ), f (a ^ n) = f a ^ n := f.to_ring_hom.map_fpow @[simp] lemma fpow_bit0_neg {K : Type*} [division_ring K] (x : K) (n : ℤ) : (-x) ^ (bit0 n) = x ^ bit0 n := by rw [fpow_bit0', fpow_bit0', neg_mul_neg] lemma fpow_even_neg {K : Type*} [division_ring K] (a : K) {n : ℤ} (h : even n) : (-a) ^ n = a ^ n := begin obtain ⟨k, rfl⟩ := h, rw [←bit0_eq_two_mul, fpow_bit0_neg], end @[simp] lemma fpow_bit1_neg {K : Type*} [division_ring K] (x : K) (n : ℤ) : (-x) ^ (bit1 n) = - x ^ bit1 n := by rw [fpow_bit1', fpow_bit1', neg_mul_neg, neg_mul_eq_mul_neg] section ordered_field_power open int variables {K : Type u} [linear_ordered_field K] {a : K} {n : ℤ} lemma fpow_eq_zero_iff (hn : 0 < n) : a ^ n = 0 ↔ a = 0 := begin refine ⟨fpow_eq_zero, _⟩, rintros rfl, exact zero_fpow _ hn.ne' end lemma fpow_nonneg {a : K} (ha : 0 ≤ a) : ∀ (z : ℤ), 0 ≤ a ^ z | (n : ℕ) := by { rw gpow_coe_nat, exact pow_nonneg ha _ } | -[1+n] := by { rw gpow_neg_succ_of_nat, exact inv_nonneg.2 (pow_nonneg ha _) } lemma fpow_pos_of_pos {a : K} (ha : 0 < a) : ∀ (z : ℤ), 0 < a ^ z | (n : ℕ) := by { rw gpow_coe_nat, exact pow_pos ha _ } | -[1+n] := by { rw gpow_neg_succ_of_nat, exact inv_pos.2 (pow_pos ha _) } lemma fpow_le_of_le {x : K} (hx : 1 ≤ x) {a b : ℤ} (h : a ≤ b) : x ^ a ≤ x ^ b := begin induction a with a a; induction b with b b, { simp only [of_nat_eq_coe, gpow_coe_nat], apply pow_le_pow hx, apply le_of_coe_nat_le_coe_nat h }, { apply absurd h, apply not_le_of_gt, exact lt_of_lt_of_le (neg_succ_lt_zero _) (of_nat_nonneg _) }, { simp only [gpow_neg_succ_of_nat, one_div, of_nat_eq_coe, gpow_coe_nat], apply le_trans (inv_le_one _); apply one_le_pow_of_one_le hx }, { simp only [gpow_neg_succ_of_nat], apply (inv_le_inv _ _).2, { apply pow_le_pow hx, have : -(↑(a+1) : ℤ) ≤ -(↑(b+1) : ℤ), from h, have h' := le_of_neg_le_neg this, apply le_of_coe_nat_le_coe_nat h' }, repeat { apply pow_pos (lt_of_lt_of_le zero_lt_one hx) } } end lemma pow_le_max_of_min_le {x : K} (hx : 1 ≤ x) {a b c : ℤ} (h : min a b ≤ c) : x ^ (-c) ≤ max (x ^ (-a)) (x ^ (-b)) := begin wlog hle : a ≤ b, have hnle : -b ≤ -a, from neg_le_neg hle, have hfle : x ^ (-b) ≤ x ^ (-a), from fpow_le_of_le hx hnle, have : x ^ (-c) ≤ x ^ (-a), { apply fpow_le_of_le hx, simpa only [min_eq_left hle, neg_le_neg_iff] using h }, simpa only [max_eq_left hfle] end lemma fpow_le_one_of_nonpos {p : K} (hp : 1 ≤ p) {z : ℤ} (hz : z ≤ 0) : p ^ z ≤ 1 := calc p ^ z ≤ p ^ 0 : fpow_le_of_le hp hz ... = 1 : by simp lemma one_le_fpow_of_nonneg {p : K} (hp : 1 ≤ p) {z : ℤ} (hz : 0 ≤ z) : 1 ≤ p ^ z := calc p ^ z ≥ p ^ 0 : fpow_le_of_le hp hz ... = 1 : by simp theorem fpow_bit0_nonneg (a : K) (n : ℤ) : 0 ≤ a ^ bit0 n := by { rw fpow_bit0, exact mul_self_nonneg _ } theorem fpow_two_nonneg (a : K) : 0 ≤ a ^ 2 := pow_bit0_nonneg a 1 theorem fpow_bit0_pos {a : K} (h : a ≠ 0) (n : ℤ) : 0 < a ^ bit0 n := (fpow_bit0_nonneg a n).lt_of_ne (fpow_ne_zero _ h).symm theorem fpow_two_pos_of_ne_zero (a : K) (h : a ≠ 0) : 0 < a ^ 2 := pow_bit0_pos h 1 @[simp] theorem fpow_bit1_neg_iff : a ^ bit1 n < 0 ↔ a < 0 := ⟨λ h, not_le.1 $ λ h', not_le.2 h $ fpow_nonneg h' _, λ h, by rw [bit1, fpow_add_one h.ne]; exact mul_neg_of_pos_of_neg (fpow_bit0_pos h.ne _) h⟩ @[simp] theorem fpow_bit1_nonneg_iff : 0 ≤ a ^ bit1 n ↔ 0 ≤ a := le_iff_le_iff_lt_iff_lt.2 fpow_bit1_neg_iff @[simp] theorem fpow_bit1_nonpos_iff : a ^ bit1 n ≤ 0 ↔ a ≤ 0 := begin rw [le_iff_lt_or_eq, fpow_bit1_neg_iff], split, { rintro (h | h), { exact h.le }, { exact (fpow_eq_zero h).le } }, { intro h, rcases eq_or_lt_of_le h with rfl|h, { exact or.inr (zero_fpow _ (bit1_ne_zero n)) }, { exact or.inl h } } end @[simp] theorem fpow_bit1_pos_iff : 0 < a ^ bit1 n ↔ 0 < a := lt_iff_lt_of_le_iff_le fpow_bit1_nonpos_iff lemma fpow_even_nonneg (a : K) {n : ℤ} (hn : even n) : 0 ≤ a ^ n := begin cases le_or_lt 0 a with h h, { exact fpow_nonneg h _ }, { rw [←fpow_even_neg _ hn], replace h : 0 ≤ -a := neg_nonneg_of_nonpos (le_of_lt h), exact fpow_nonneg h _ } end theorem fpow_even_pos (ha : a ≠ 0) (hn : even n) : 0 < a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using fpow_bit0_pos ha k theorem fpow_odd_nonneg (ha : 0 ≤ a) (hn : odd n) : 0 ≤ a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using fpow_bit1_nonneg_iff.mpr ha theorem fpow_odd_pos (ha : 0 < a) (hn : odd n) : 0 < a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using fpow_bit1_pos_iff.mpr ha theorem fpow_odd_nonpos (ha : a ≤ 0) (hn : odd n) : a ^ n ≤ 0:= by cases hn with k hk; simpa only [hk, two_mul] using fpow_bit1_nonpos_iff.mpr ha theorem fpow_odd_neg (ha : a < 0) (hn : odd n) : a ^ n < 0:= by cases hn with k hk; simpa only [hk, two_mul] using fpow_bit1_neg_iff.mpr ha lemma fpow_even_abs (a : K) {p : ℤ} (hp : even p) : |a| ^ p = a ^ p := begin cases abs_choice a with h h; simp only [h, fpow_even_neg _ hp], end @[simp] lemma fpow_bit0_abs (a : K) (p : ℤ) : (|a|) ^ bit0 p = a ^ bit0 p := fpow_even_abs _ (even_bit0 _) lemma abs_fpow_even (a : K) {p : ℤ} (hp : even p) : |a ^ p| = a ^ p := begin rw [abs_eq_self], exact fpow_even_nonneg _ hp end @[simp] lemma abs_fpow_bit0 (a : K) (p : ℤ) : |a ^ bit0 p| = a ^ bit0 p := abs_fpow_even _ (even_bit0 _) end ordered_field_power lemma one_lt_fpow {K} [linear_ordered_field K] {p : K} (hp : 1 < p) : ∀ z : ℤ, 0 < z → 1 < p ^ z | (n : ℕ) h := (gpow_coe_nat p n).symm.subst (one_lt_pow hp $ int.coe_nat_ne_zero.mp h.ne') | -[1+ n] h := ((int.neg_succ_not_pos _).mp h).elim section ordered variables {K : Type*} [linear_ordered_field K] lemma nat.fpow_pos_of_pos {p : ℕ} (h : 0 < p) (n:ℤ) : 0 < (p:K)^n := by { apply fpow_pos_of_pos, exact_mod_cast h } lemma nat.fpow_ne_zero_of_pos {p : ℕ} (h : 0 < p) (n:ℤ) : (p:K)^n ≠ 0 := ne_of_gt (nat.fpow_pos_of_pos h n) lemma fpow_strict_mono {x : K} (hx : 1 < x) : strict_mono (λ n:ℤ, x ^ n) := λ m n h, show x ^ m < x ^ n, begin have xpos : 0 < x := zero_lt_one.trans hx, have h₀ : x ≠ 0 := xpos.ne', have hxm : 0 < x^m := fpow_pos_of_pos xpos m, have h : 1 < x ^ (n - m) := one_lt_fpow hx _ (sub_pos_of_lt h), replace h := mul_lt_mul_of_pos_right h hxm, rwa [sub_eq_add_neg, fpow_add h₀, mul_assoc, fpow_neg_mul_fpow_self _ h₀, one_mul, mul_one] at h, end @[simp] lemma fpow_lt_iff_lt {x : K} (hx : 1 < x) {m n : ℤ} : x ^ m < x ^ n ↔ m < n := (fpow_strict_mono hx).lt_iff_lt @[simp] lemma fpow_le_iff_le {x : K} (hx : 1 < x) {m n : ℤ} : x ^ m ≤ x ^ n ↔ m ≤ n := (fpow_strict_mono hx).le_iff_le @[simp] lemma pos_div_pow_pos {a b : K} (ha : 0 < a) (hb : 0 < b) (k : ℕ) : 0 < a/b^k := div_pos ha (pow_pos hb k) @[simp] lemma div_pow_le {a b : K} (ha : 0 < a) (hb : 1 ≤ b) (k : ℕ) : a/b^k ≤ a := (div_le_iff $ pow_pos (lt_of_lt_of_le zero_lt_one hb) k).mpr (calc a = a * 1 : (mul_one a).symm ... ≤ a*b^k : (mul_le_mul_left ha).mpr $ one_le_pow_of_one_le hb _) lemma fpow_injective {x : K} (h₀ : 0 < x) (h₁ : x ≠ 1) : function.injective ((^) x : ℤ → K) := begin intros m n h, rcases h₁.lt_or_lt with H|H, { apply (fpow_strict_mono (one_lt_inv h₀ H)).injective, show x⁻¹ ^ m = x⁻¹ ^ n, rw [← fpow_neg_one, ← fpow_mul, ← fpow_mul, mul_comm _ m, mul_comm _ n, fpow_mul, fpow_mul, h], }, { exact (fpow_strict_mono H).injective h, }, end @[simp] lemma fpow_inj {x : K} (h₀ : 0 < x) (h₁ : x ≠ 1) {m n : ℤ} : x ^ m = x ^ n ↔ m = n := (fpow_injective h₀ h₁).eq_iff end ordered section variables {K : Type*} [field K] @[simp, norm_cast] theorem rat.cast_fpow [char_zero K] (q : ℚ) (n : ℤ) : ((q ^ n : ℚ) : K) = q ^ n := (rat.cast_hom K).map_fpow q n end
d8fb3151ece4a717d157fdc7ce2a8efe7c2f0ab2
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/asymptotics/superpolynomial_decay.lean
ef96c64ac92836fe7dd278470f749707f319fb33
[ "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,171
lean
/- Copyright (c) 2021 Devon Tuma. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Devon Tuma -/ import analysis.asymptotics.asymptotics import analysis.asymptotics.specific_asymptotics import data.polynomial.eval /-! # Super-Polynomial Function Decay This file defines a predicate `asymptotics.superpolynomial_decay f` for a function satisfying one of following equivalent definitions (The definition is in terms of the first condition): * `f` is `O(x ^ c)` for all (or sufficiently small) integers `c` * `x ^ c * f` is bounded for all (or sufficiently large) integers `c` * `x ^ c * f` tends to `𝓝 0` for all (or sufficiently large) integers `c` * `f` is `o(x ^ c)` for all (or sufficiently small) integers `c` The equivalence between the first two is given by in `superpolynomial_decay_iff_is_bounded_under`. The equivalence between the first and third is given in `superpolynomial_decay_iff_tendsto_zero`. The equivalence between the first and fourth is given in `superpolynomial_decay_iff_is_o`. These conditions are all equivalent to conditions in terms of polynomials, replacing `x ^ c` with `p(x)` or `p(x)⁻¹` as appropriate, since asymptotically `p(x)` behaves like `X ^ p.nat_degree`. These further equivalences are not proven in mathlib but would be good future projects. The definition of superpolynomial decay for a function `f : α → 𝕜` is made relative to an algebra structure `[algebra α 𝕜]`. Super-polynomial decay then means the function `f x` decays faster than `(p.eval (algebra_map α 𝕜 x))⁻¹` for all polynomials `p : polynomial 𝕜`. When the algebra structure is given by `n ↦ ↑n : ℕ → ℝ` this defines negligible functions: https://en.wikipedia.org/wiki/Negligible_function When the algebra structure is given by `(r₁,...,rₙ) ↦ r₁*...*rₙ : ℝⁿ → ℝ` this is equivalent to the definition of rapidly decreasing functions given here: https://ncatlab.org/nlab/show/rapidly+decreasing+function # Main Theorems * `superpolynomial_decay.polynomial_mul` says that if `f(x)` is negligible, then so is `p(x) * f(x)` for any polynomial `p`. * `superpolynomial_decay_iff_is_bounded_under` says that `f` is negligible iff `p(x) * f(x)` has bounded norm for all polynomials `p(x)`. * `superpolynomial_decay_of_eventually_is_O` says that it suffices to check `f(x)` is `O(x ^ c)` for only sufficiently small `c`, rather than all integers `c`. -/ namespace asymptotics open_locale topological_space open filter /-- A function `f` from an `ordered_comm_semiring` to a `normed_field` has superpolynomial decay iff `f(x)` is `O(x ^ c)` for all integers `c`. -/ def superpolynomial_decay {α 𝕜 : Type*} [ordered_comm_semiring α] [normed_field 𝕜] [algebra α 𝕜] (f : α → 𝕜) := ∀ (c : ℤ), is_O f (λ x, (algebra_map α 𝕜 x) ^ c) filter.at_top section normed_field variables {α 𝕜 : Type*} [ordered_comm_semiring α] [normed_field 𝕜] [algebra α 𝕜] variables {f g : α → 𝕜} theorem superpolynomial_decay_iff_is_bounded_under (f : α → 𝕜) (hα : ∀ᶠ (x : α) in at_top, (algebra_map α 𝕜 x) ≠ 0) : superpolynomial_decay f ↔ ∀ (c : ℤ), is_bounded_under has_le.le at_top (λ x, ∥f x * (algebra_map α 𝕜 x) ^ c∥) := begin split; intros h c; specialize h (-c), { simpa [div_eq_mul_inv] using div_is_bounded_under_of_is_O h }, { refine (is_O_iff_div_is_bounded_under _).2 _, { exact hα.mono (λ x hx hx', absurd (zpow_eq_zero hx') hx) }, { simpa [div_eq_mul_inv] using h } } end theorem superpolynomial_decay_iff_is_o (f : α → 𝕜) (hα : tendsto (λ x, ∥algebra_map α 𝕜 x∥) at_top at_top) : superpolynomial_decay f ↔ ∀ (c : ℤ), is_o f (λ x, (algebra_map α 𝕜 x) ^ c) at_top := begin refine ⟨λ h c, _, λ h c, (h c).is_O⟩, have hα' : ∀ᶠ (x : α) in at_top, (algebra_map α 𝕜 x) ≠ 0, from (eventually_ne_of_tendsto_norm_at_top hα 0).mono (λ x hx hx', absurd hx' hx), have : is_o (λ x, 1 : α → 𝕜) (λ x, (algebra_map α 𝕜 x)) at_top, { refine is_o_of_tendsto' (hα'.mono $ λ x hx hx', absurd hx' hx) (tendsto_zero_iff_norm_tendsto_zero.2 _), simp only [one_div, normed_field.norm_inv], exact tendsto.comp tendsto_inv_at_top_zero hα }, have := this.mul_is_O (h $ c - 1), simp only [one_mul] at this, refine this.trans_is_O (is_O.of_bound 1 (hα'.mono (λ x hx, le_of_eq _))), rw [zpow_sub_one₀ hx, mul_comm, mul_assoc, inv_mul_cancel hx, one_mul, mul_one] end theorem superpolynomial_decay_iff_norm_tendsto_zero (f : α → 𝕜) (hα : tendsto (λ x, ∥algebra_map α 𝕜 x∥) at_top at_top) : superpolynomial_decay f ↔ ∀ (c : ℤ), tendsto (λ x, ∥f x * (algebra_map α 𝕜 x) ^ c∥) at_top (𝓝 0) := begin refine ⟨λ h c, _, λ h, _⟩, { refine tendsto_zero_iff_norm_tendsto_zero.1 _, rw (superpolynomial_decay_iff_is_o f hα) at h, simpa [div_eq_mul_inv] using (h $ -c).tendsto_0 }, { have hα' : ∀ᶠ (x : α) in at_top, (algebra_map α 𝕜 x) ≠ 0, from (eventually_ne_of_tendsto_norm_at_top hα 0).mono (λ x hx hx', absurd hx' hx), exact (superpolynomial_decay_iff_is_bounded_under f hα').2 (λ c, is_bounded_under_of_tendsto (tendsto_zero_iff_norm_tendsto_zero.2 $ h c)) } end lemma superpolynomial_decay_iff_tendsto_zero (f : α → 𝕜) (hα : tendsto (λ x, ∥algebra_map α 𝕜 x∥) at_top at_top) : superpolynomial_decay f ↔ ∀ (c : ℤ), tendsto (λ x, f x * (algebra_map α 𝕜 x) ^ c) at_top (𝓝 0) := (superpolynomial_decay_iff_norm_tendsto_zero f hα).trans (by simp [tendsto_zero_iff_norm_tendsto_zero]) lemma is_O.trans_superpolynomial_decay (h : is_O f g at_top) (hg : superpolynomial_decay g) : superpolynomial_decay f := λ c, h.trans $ hg c alias is_O.trans_superpolynomial_decay ← superpolynomial_decay.is_O_mono lemma superpolynomial_decay.mono (hf : superpolynomial_decay f) (h : ∀ n, ∥g n∥ ≤ ∥f n∥) : superpolynomial_decay g := (is_O_of_le at_top h).trans_superpolynomial_decay hf lemma superpolynomial_decay.eventually_mono (hf : superpolynomial_decay f) (h : ∀ᶠ n in at_top, ∥g n∥ ≤ ∥f n∥) : superpolynomial_decay g := (is_O_iff.2 ⟨1, by simpa only [one_mul] using h⟩).trans_superpolynomial_decay hf @[simp] lemma superpolynomial_decay_zero : superpolynomial_decay (0 : α → 𝕜) := λ c, is_O_zero _ _ @[simp] lemma superpolynomial_decay_zero' : superpolynomial_decay (λ (x : α), (0 : 𝕜)) := superpolynomial_decay_zero lemma superpolynomial_decay.add (hf : superpolynomial_decay f) (hg : superpolynomial_decay g) : superpolynomial_decay (f + g) := λ c, is_O.add (hf c) (hg c) lemma superpolynomial_decay.const_mul (hf : superpolynomial_decay f) (c : 𝕜) : superpolynomial_decay (λ n, c * f n) := (is_O_const_mul_self c f at_top).trans_superpolynomial_decay hf lemma superpolynomial_decay.mul_const (hf : superpolynomial_decay f) (c : 𝕜) : superpolynomial_decay (λ n, f n * c) := by simpa [mul_comm _ c] using superpolynomial_decay.const_mul hf c lemma superpolynomial_decay_const_mul_iff_of_ne_zero {c : 𝕜} (hc : c ≠ 0) : superpolynomial_decay (λ n, c * f n) ↔ superpolynomial_decay f := ⟨λ h, (is_O_self_const_mul c hc f at_top).trans_superpolynomial_decay h, λ h, h.const_mul c ⟩ lemma superpolynomial_decay_mul_const_iff_of_ne_zero {c : 𝕜} (hc : c ≠ 0) : superpolynomial_decay (λ n, f n * c) ↔ superpolynomial_decay f := by simpa [mul_comm _ c] using superpolynomial_decay_const_mul_iff_of_ne_zero hc @[simp] lemma superpolynomial_decay_const_mul_iff (c : 𝕜) : superpolynomial_decay (λ n, c * f n) ↔ c = 0 ∨ superpolynomial_decay f := begin by_cases hc0 : c = 0, { simp [hc0] }, { exact (superpolynomial_decay_const_mul_iff_of_ne_zero hc0).trans ⟨or.inr, or.rec (λ hc0', absurd hc0' hc0) id⟩ } end @[simp] lemma superpolynomial_decay_mul_const_iff (c : 𝕜) : superpolynomial_decay (λ n, f n * c) ↔ c = 0 ∨ superpolynomial_decay f := by simp [mul_comm _ c] section no_zero_smul_divisors variables [no_zero_smul_divisors α 𝕜] lemma superpolynomial_decay.algebra_map_mul (hf : superpolynomial_decay f) : superpolynomial_decay (λ n, (algebra_map α 𝕜 n) * f n) := begin haveI : nontrivial α := (algebra_map α 𝕜).domain_nontrivial, refine λ c, (is_O.mul (is_O_refl (algebra_map α 𝕜) at_top) (hf (c - 1))).trans _, refine is_O_of_div_tendsto_nhds (eventually_of_forall (λ x hx, mul_eq_zero_of_left (zpow_eq_zero hx) _)) 1 (tendsto_nhds.2 _), refine λ s hs hs', at_top.sets_of_superset (mem_at_top 1) (λ x hx, set.mem_preimage.2 _), have hx' : algebra_map α 𝕜 x ≠ 0 := λ hx', (ne_of_lt $ lt_of_lt_of_le zero_lt_one hx).symm (by simpa [algebra.algebra_map_eq_smul_one, smul_eq_zero] using hx'), convert hs', rw [pi.div_apply, div_eq_one_iff_eq (zpow_ne_zero c hx'), zpow_sub_one₀ hx' c, mul_comm (algebra_map α 𝕜 x), mul_assoc, inv_mul_cancel hx', mul_one], end lemma superpolynomial_decay.algebra_map_pow_mul (hf : superpolynomial_decay f) (p : ℕ) : superpolynomial_decay (λ n, (algebra_map α 𝕜 n) ^ p * f n) := begin induction p with p hp, { simp_rw [pow_zero, one_mul], exact hf }, { simp_rw [pow_succ, mul_assoc], exact hp.algebra_map_mul } end theorem superpolynomial_decay.polynomial_mul (hf : superpolynomial_decay f) (p : polynomial 𝕜) : superpolynomial_decay (λ n, (p.eval (algebra_map α 𝕜 n)) * f n) := begin refine polynomial.induction_on' p (λ p q hp hq, _) (λ m x, _), { simp_rw [polynomial.eval_add, add_mul], exact hp.add hq }, { simp_rw [polynomial.eval_monomial, mul_assoc], exact (hf.algebra_map_pow_mul m).const_mul x } end /-- If `f` has superpolynomial decay, and `g` is `O(p)` for some polynomial `p`, then `f * g` has superpolynomial decay -/ lemma superpolynomial_decay.mul_is_O_polynomial (hf : superpolynomial_decay f) (p : polynomial 𝕜) (hg : is_O g (λ n, p.eval (algebra_map α 𝕜 n)) filter.at_top) : superpolynomial_decay (f * g) := (is_O.mul (is_O_refl f at_top) hg).trans_superpolynomial_decay ((hf.polynomial_mul p).mono $ λ x, le_of_eq (congr_arg _ $ mul_comm _ _)) /-- If `f` has superpolynomial decay, and `g` is `O(n ^ c)` for some integer `c`, then `f * g` has has superpolynomial decay-/ lemma superpolynomial_decay.mul_is_O (hf : superpolynomial_decay f) (c : ℕ) (hg : is_O g (λ n, (algebra_map α 𝕜 n) ^ c) at_top) : superpolynomial_decay (f * g) := (is_O.mul (is_O_refl f at_top) hg).trans_superpolynomial_decay ((hf.algebra_map_pow_mul c).mono $ λ x, le_of_eq (congr_arg _ $ mul_comm _ _)) lemma superpolynomial_decay.mul (hf : superpolynomial_decay f) (hg : superpolynomial_decay g) : superpolynomial_decay (f * g) := hf.mul_is_O 0 (by simpa using hg 0) end no_zero_smul_divisors end normed_field section normed_linear_ordered_field variables {α 𝕜 : Type*} [ordered_comm_semiring α] [normed_linear_ordered_field 𝕜] [algebra α 𝕜] variables {f g : α → 𝕜} /-- It suffices to check the decay condition for only sufficiently small exponents `c`, assuing algebra_map eventually has norm at least `1` -/ lemma superpolynomial_decay_of_eventually_is_O (hα : ∀ᶠ (x : α) in at_top, 1 ≤ ∥algebra_map α 𝕜 x∥) (h : ∀ᶠ (c : ℤ) in at_bot, is_O f (λ x, (algebra_map α 𝕜 x) ^ c) at_top) : superpolynomial_decay f := begin obtain ⟨C, hC⟩ := eventually_at_bot.mp h, intro c, by_cases hc : c ≤ C, { exact hC c hc }, { refine (hC C le_rfl).trans (is_O.of_bound 1 (_)), refine at_top.sets_of_superset hα (λ x hx, _), simp only [one_mul, normed_field.norm_zpow, set.mem_set_of_eq], exact zpow_le_of_le hx (le_of_not_le hc) } end lemma superpolynomial_decay_of_is_O_zpow_le (hα : ∀ᶠ (x : α) in at_top, 1 ≤ ∥algebra_map α 𝕜 x∥) (C : ℤ) (h : ∀ c ≤ C, is_O f (λ n, (algebra_map α 𝕜 n) ^ c) at_top) : superpolynomial_decay f := superpolynomial_decay_of_eventually_is_O hα (eventually_at_bot.2 ⟨C, h⟩) lemma superpolynomial_decay_of_is_O_zpow_lt (hα : ∀ᶠ (x : α) in at_top, 1 ≤ ∥algebra_map α 𝕜 x∥) (C : ℤ) (h : ∀ c < C, is_O f (λ n, (algebra_map α 𝕜 n) ^ c) at_top) : superpolynomial_decay f := superpolynomial_decay_of_is_O_zpow_le hα C.pred (λ c hc, h c (lt_of_le_of_lt hc (int.pred_self_lt C))) section order_topology variable [order_topology 𝕜] /-- A function with superpolynomial decay must tend to zero in the base ring (not just in norm), assuming `algebra_map α 𝕜` tends to `at_top` -/ lemma superpolynomial_decay.tendsto_zero (hα : tendsto (algebra_map α 𝕜) at_top at_top) (hf : superpolynomial_decay f) : tendsto f at_top (𝓝 0) := begin refine is_O.trans_tendsto (hf (-1)) _, have : (has_inv.inv : 𝕜 → 𝕜) ∘ (algebra_map α 𝕜 : α → 𝕜) = (λ (n : α), (algebra_map α 𝕜 n) ^ (-1 : ℤ)), by simp only [zpow_one, zpow_neg₀], exact this ▸ (tendsto_inv_at_top_zero).comp (hα) end /-- A function with superpolynomial decay eventually has norm less than any positive bound, assuming the algebra map tendsto to `at_top` -/ lemma superpolynomial_decay.eventually_le (hα : tendsto (algebra_map α 𝕜) at_top at_top) (hf : superpolynomial_decay f) (ε : ℝ) (hε : 0 < ε) : ∀ᶠ (n : α) in at_top, ∥f n∥ ≤ ε := by simpa only [dist_zero_right] using (hf.tendsto_zero hα).eventually (metric.closed_ball_mem_nhds (0 : 𝕜) hε) lemma superpolynomial_decay_const_iff [(at_top : filter α).ne_bot] (hα : tendsto (algebra_map α 𝕜) at_top at_top) (x : 𝕜) : superpolynomial_decay (function.const α x) ↔ x = 0 := begin refine ⟨λ h, not_not.1 (λ hx, _), λ h, by simp [h]⟩, have : (function.const α x ⁻¹' {x}ᶜ) ∈ at_top := (tendsto_nhds.1 $ h.tendsto_zero hα) {x}ᶜ (is_open_ne) (ne.symm hx), rw [set.preimage_const_of_not_mem (by simp : x ∉ ({x} : set 𝕜)ᶜ)] at this, exact at_top.empty_not_mem this end end order_topology end normed_linear_ordered_field end asymptotics
f662cfb059c67a316942e7c16527c0bb1a437d31
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/instances/rat.lean
9e5ed81f1cbfd70c2ea7200f16417008c39f6a22
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
4,219
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 -/ import topology.metric_space.basic import topology.algebra.order.archimedean import topology.instances.int import topology.instances.nat import topology.instances.real /-! # Topology on the ratonal numbers > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The structure of a metric space on `ℚ` is introduced in this file, induced from `ℝ`. -/ open metric set filter namespace rat -- without the `by exact` this is noncomputable instance : metric_space ℚ := metric_space.induced coe (by exact rat.cast_injective) real.metric_space theorem dist_eq (x y : ℚ) : dist x y = |x - y| := rfl @[norm_cast, simp] lemma dist_cast (x y : ℚ) : dist (x : ℝ) y = dist x y := rfl theorem uniform_continuous_coe_real : uniform_continuous (coe : ℚ → ℝ) := uniform_continuous_comap theorem uniform_embedding_coe_real : uniform_embedding (coe : ℚ → ℝ) := uniform_embedding_comap rat.cast_injective theorem dense_embedding_coe_real : dense_embedding (coe : ℚ → ℝ) := uniform_embedding_coe_real.dense_embedding rat.dense_range_cast theorem embedding_coe_real : embedding (coe : ℚ → ℝ) := dense_embedding_coe_real.to_embedding theorem continuous_coe_real : continuous (coe : ℚ → ℝ) := uniform_continuous_coe_real.continuous end rat @[norm_cast, simp] theorem nat.dist_cast_rat (x y : ℕ) : dist (x : ℚ) y = dist x y := by rw [← nat.dist_cast_real, ← rat.dist_cast]; congr' 1; norm_cast lemma nat.uniform_embedding_coe_rat : uniform_embedding (coe : ℕ → ℚ) := uniform_embedding_bot_of_pairwise_le_dist zero_lt_one $ by simpa using nat.pairwise_one_le_dist lemma nat.closed_embedding_coe_rat : closed_embedding (coe : ℕ → ℚ) := closed_embedding_of_pairwise_le_dist zero_lt_one $ by simpa using nat.pairwise_one_le_dist @[norm_cast, simp] theorem int.dist_cast_rat (x y : ℤ) : dist (x : ℚ) y = dist x y := by rw [← int.dist_cast_real, ← rat.dist_cast]; congr' 1; norm_cast lemma int.uniform_embedding_coe_rat : uniform_embedding (coe : ℤ → ℚ) := uniform_embedding_bot_of_pairwise_le_dist zero_lt_one $ by simpa using int.pairwise_one_le_dist lemma int.closed_embedding_coe_rat : closed_embedding (coe : ℤ → ℚ) := closed_embedding_of_pairwise_le_dist zero_lt_one $ by simpa using int.pairwise_one_le_dist namespace rat instance : noncompact_space ℚ := int.closed_embedding_coe_rat.noncompact_space -- TODO(Mario): Find a way to use rat_add_continuous_lemma theorem uniform_continuous_add : uniform_continuous (λp : ℚ × ℚ, p.1 + p.2) := rat.uniform_embedding_coe_real.to_uniform_inducing.uniform_continuous_iff.2 $ by simp only [(∘), rat.cast_add]; exact real.uniform_continuous_add.comp (rat.uniform_continuous_coe_real.prod_map rat.uniform_continuous_coe_real) theorem uniform_continuous_neg : uniform_continuous (@has_neg.neg ℚ _) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [rat.dist_eq] using h⟩ instance : uniform_add_group ℚ := uniform_add_group.mk' rat.uniform_continuous_add rat.uniform_continuous_neg instance : topological_add_group ℚ := by apply_instance instance : order_topology ℚ := induced_order_topology _ (λ x y, rat.cast_lt) (@exists_rat_btwn _ _ _) lemma uniform_continuous_abs : uniform_continuous (abs : ℚ → ℚ) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨ε, ε0, λ a b h, lt_of_le_of_lt (by simpa [rat.dist_eq] using abs_abs_sub_abs_le_abs_sub _ _) h⟩ lemma continuous_mul : continuous (λp : ℚ × ℚ, p.1 * p.2) := rat.embedding_coe_real.continuous_iff.2 $ by simp [(∘)]; exact real.continuous_mul.comp ((rat.continuous_coe_real.prod_map rat.continuous_coe_real)) instance : topological_ring ℚ := { continuous_mul := rat.continuous_mul, ..rat.topological_add_group } lemma totally_bounded_Icc (a b : ℚ) : totally_bounded (Icc a b) := by simpa only [preimage_cast_Icc] using totally_bounded_preimage rat.uniform_embedding_coe_real (totally_bounded_Icc a b) end rat
ddc12311c773e3e6a261adc7b082b367675af8a1
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/replace.lean
eedc78c1d6b52d3f9e326c0fa9176df951ae3a17
[ "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
605
lean
import Lean open Lean partial def mkBig : Nat → Expr | 0 => mkConst `a | (n+1) => mkApp2 (mkConst `f []) (mkBig n) (mkBig n) def replaceTest (e : Expr) : Expr := e.replace $ fun e => match e with | Expr.const c _ _ => if c == `f then mkConst `g else none | _ => none #eval replaceTest $ mkBig 4 #eval (replaceTest $ mkBig 128).getAppFn def findTest (e : Expr) : Option Expr := e.find? $ fun e => match e with | Expr.const c _ _ => c == `g | _ => false #eval findTest $ mkBig 4 #eval findTest $ replaceTest $ mkBig 4 #eval findTest $ mkBig 128 #eval findTest $ (replaceTest $ mkBig 128)
011bef12705d0849346d2a409e13a313b6c8505f
07f5f86b00fed90a419ccda4298d8b795a68f657
/library/init/meta/interactive.lean
b47dd99ca9652c91c8951b8f55870f4555877bc0
[ "Apache-2.0" ]
permissive
VBaratham/lean
8ec5c3167b4835cfbcd7f25e2173d61ad9416b3a
450ca5834c1c35318e4b47d553bb9820c1b3eee7
refs/heads/master
1,629,649,471,814
1,512,060,373,000
1,512,060,469,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
54,200
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.rewrite_tactic init.meta.simp_tactic import init.meta.smt.congruence_closure init.category.combinators import init.meta.interactive_base init.meta.derive open lean open lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic /- allows metavars -/ meta def i_to_expr (q : pexpr) : tactic expr := to_expr q tt /- allow metavars and no subgoals -/ meta def i_to_expr_no_subgoals (q : pexpr) : tactic expr := to_expr q tt ff /- doesn't allows metavars -/ meta def i_to_expr_strict (q : pexpr) : tactic expr := to_expr q ff /- Auxiliary version of i_to_expr for apply-like tactics. This is a workaround for comment https://github.com/leanprover/lean/issues/1342#issuecomment-307912291 at issue #1342. In interactive mode, given a tactic apply f we want the apply tactic to create all metavariables. The following definition will return `@f` for `f`. That is, it will **not** create metavariables for implicit arguments. Before we added `i_to_expr_for_apply`, the tactic apply le_antisymm would first elaborate `le_antisymm`, and create @le_antisymm ?m_1 ?m_2 ?m_3 ?m_4 The type class resolution problem ?m_2 : weak_order ?m_1 by the elaborator since ?m_1 is not assigned yet, and the problem is discarded. Then, we would invoke `apply_core`, which would create two new metavariables for the explicit arguments, and try to unify the resulting type with the current target. After the unification, the metavariables ?m_1, ?m_3 and ?m_4 are assigned, but we lost the information about the pending type class resolution problem. With `i_to_expr_for_apply`, `le_antisymm` is elaborate into `@le_antisymm`, the apply_core tactic creates all metavariables, and solves the ones that can be solved by type class resolution. Another possible fix: we modify the elaborator to return pending type class resolution problems, and store them in the tactic_state. -/ meta def i_to_expr_for_apply (q : pexpr) : tactic expr := let aux (n : name) : tactic expr := do p ← resolve_name n, match p with | (expr.const c []) := do r ← mk_const c, save_type_info r q, return r | _ := i_to_expr p end in match q with | (expr.const c []) := aux c | (expr.local_const c _ _ _) := aux c | _ := i_to_expr q end namespace interactive open interactive interactive.types expr /-- itactic: parse a nested "interactive" tactic. That is, parse `{` tactic `}` -/ meta def itactic : Type := tactic unit /-- If the current goal is a Pi/forall `∀ x : t, u` (resp. `let x := t in u`) then `intro` puts `x : t` (resp. `x := t`) in the local context. The new subgoal target is `u`. If the goal is an arrow `t → u`, then it puts `h : t` in the local context and the new goal target is `u`. If the goal is neither a Pi/forall nor begins with a let binder, the tactic `intro` applies the tactic `whnf` until an introduction can be applied or the goal is not head reducible. In the latter case, the tactic fails. -/ meta def intro : parse ident_? → tactic unit | none := intro1 >> skip | (some h) := tactic.intro h >> skip /-- Similar to `intro` tactic. The tactic `intros` will keep introducing new hypotheses until the goal target is not a Pi/forall or let binder. The variant `intros h₁ ... hₙ` introduces `n` new hypotheses using the given identifiers to name them. -/ meta def intros : parse ident_* → tactic unit | [] := tactic.intros >> skip | hs := intro_lst hs >> skip /-- The tactic `introv` allows the user to automatically introduce the variables of a theorem and explicitly name the hypotheses involved. The given names are used to name non-dependent hypotheses. Examples: ``` example : ∀ a b : nat, a = b → b = a := begin introv h, exact h.symm end ``` The state after `introv h` is ``` a b : ℕ, h : a = b ⊢ b = a ``` ``` example : ∀ a b : nat, a = b → ∀ c, b = c → a = c := begin introv h₁ h₂, exact h₁.trans h₂ end ``` The state after `introv h₁ h₂` is ``` a b : ℕ, h₁ : a = b, c : ℕ, h₂ : b = c ⊢ a = c ``` -/ meta def introv (ns : parse ident_*) : tactic unit := tactic.introv ns >> return () /-- The tactic `rename h₁ h₂` renames hypothesis `h₁` to `h₂` in the current local context. -/ meta def rename : parse ident → parse ident → tactic unit := tactic.rename /-- The `apply` tactic tries to match the current goal against the conclusion of the type of term. The argument term should be a term well-formed in the local context of the main goal. If it succeeds, then the tactic returns as many subgoals as the number of premises that have not been fixed by type inference or type class resolution. Non-dependent premises are added before dependent ones. The `apply` tactic uses higher-order pattern matching, type class resolution, and first-order unification with dependent types. -/ meta def apply (q : parse texpr) : tactic unit := i_to_expr_for_apply q >>= tactic.apply /-- Similar to the `apply` tactic, but does not reorder goals. -/ meta def fapply (q : parse texpr) : tactic unit := i_to_expr_for_apply q >>= tactic.fapply /-- Similar to the `apply` tactic, but only creates subgoals for non-dependent premises that have not been fixed by type inference or type class resolution. -/ meta def eapply (q : parse texpr) : tactic unit := i_to_expr_for_apply q >>= tactic.eapply /-- Similar to the `apply` tactic, but allows the user to provide a `apply_cfg` configuration object. -/ meta def apply_with (q : parse parser.pexpr) (cfg : apply_cfg) : tactic unit := do e ← i_to_expr_for_apply q, tactic.apply e cfg /-- This tactic tries to close the main goal `... ⊢ t` by generating a term of type `t` using type class resolution. -/ meta def apply_instance : tactic unit := tactic.apply_instance /-- This tactic behaves like `exact`, but with a big difference: the user can put underscores `_` in the expression as placeholders for holes that need to be filled, and `refine` will generate as many subgoals as there are holes. Note that some holes may be implicit. The type of each hole must either be synthesized by the system or declared by an explicit type ascription like `(_ : nat → Prop)`. -/ meta def refine (q : parse texpr) : tactic unit := tactic.refine q /-- This tactic looks in the local context for a hypothesis whose type is equal to the goal target. If it finds one, it uses it to prove the goal, and otherwise it fails. -/ meta def assumption : tactic unit := tactic.assumption /-- Try to apply `assumption` to all goals. -/ meta def assumption' : tactic unit := tactic.any_goals tactic.assumption private meta def change_core (e : expr) : option expr → tactic unit | none := tactic.change e | (some h) := do num_reverted : ℕ ← revert h, expr.pi n bi d b ← target, tactic.change $ expr.pi n bi e b, intron num_reverted /-- `change u` replaces the target `t` of the main goal to `u` provided that `t` is well formed with respect to the local context of the main goal and `t` and `u` are definitionally equal. `change u at h` will change a local hypothesis to `u`. `change t with u at h1 h2 ...` will replace `t` with `u` in all the supplied hypotheses (or `*`), or in the goal if no `at` clause is specified, provided that `t` and `u` are definitionally equal. -/ meta def change (q : parse texpr) : parse (tk "with" *> texpr)? → parse location → tactic unit | none (loc.ns [none]) := do e ← i_to_expr q, change_core e none | none (loc.ns [some h]) := do eq ← i_to_expr q, eh ← get_local h, change_core eq (some eh) | none _ := fail "change-at does not support multiple locations" | (some w) l := do u ← mk_meta_univ, ty ← mk_meta_var (sort u), eq ← i_to_expr ``(%%q : %%ty), ew ← i_to_expr ``(%%w : %%ty), let repl := λe : expr, e.replace (λ a n, if a = eq then some ew else none), l.try_apply (λh, do e ← infer_type h, change_core (repl e) (some h)) (do g ← target, change_core (repl g) none) /-- This tactic provides an exact proof term to solve the main goal. If `t` is the goal and `p` is a term of type `u` then `exact p` succeeds if and only if `t` and `u` can be unified. -/ meta def exact (q : parse texpr) : tactic unit := do tgt : expr ← target, i_to_expr_strict ``(%%q : %%tgt) >>= tactic.exact /-- Like `exact`, but takes a list of terms and checks that all goals are discharged after the tactic. -/ meta def exacts : parse pexpr_list_or_texpr → tactic unit | [] := done | (t :: ts) := exact t >> exacts ts /-- A synonym for `exact` that allows writing `have/suffices/show ..., from ...` in tactic mode. -/ meta def «from» := exact /-- `revert h₁ ... hₙ` applies to any goal with hypotheses `h₁` ... `hₙ`. It moves the hypotheses and their dependencies to the target of the goal. This tactic is the inverse of `intro`. -/ meta def revert (ids : parse ident*) : tactic unit := do hs ← mmap tactic.get_local ids, revert_lst hs, skip private meta def resolve_name' (n : name) : tactic expr := do { p ← resolve_name n, match p with | expr.const n _ := mk_const n -- create metavars for universe levels | _ := i_to_expr p end } /- Version of to_expr that tries to bypass the elaborator if `p` is just a constant or local constant. This is not an optimization, by skipping the elaborator we make sure that no unwanted resolution is used. Example: the elaborator will force any unassigned ?A that must have be an instance of (has_one ?A) to nat. Remark: another benefit is that auxiliary temporary metavariables do not appear in error messages. -/ meta def to_expr' (p : pexpr) : tactic expr := match p with | (const c []) := do new_e ← resolve_name' c, save_type_info new_e p, return new_e | (local_const c _ _ _) := do new_e ← resolve_name' c, save_type_info new_e p, return new_e | _ := i_to_expr p end @[derive has_reflect] meta structure rw_rule := (pos : pos) (symm : bool) (rule : pexpr) meta def get_rule_eqn_lemmas (r : rw_rule) : tactic (list name) := let aux (n : name) : tactic (list name) := do { p ← resolve_name n, -- unpack local refs let e := p.erase_annotations.get_app_fn.erase_annotations, match e with | const n _ := get_eqn_lemmas_for tt n | _ := return [] end } <|> return [] in match r.rule with | const n _ := aux n | local_const n _ _ _ := aux n | _ := return [] end private meta def rw_goal (cfg : rewrite_cfg) (rs : list rw_rule) : tactic unit := rs.mmap' $ λ r, do save_info r.pos, eq_lemmas ← get_rule_eqn_lemmas r, orelse' (do e ← to_expr' r.rule, rewrite_target e {symm := r.symm, ..cfg}) (eq_lemmas.mfirst $ λ n, do e ← mk_const n, rewrite_target e {symm := r.symm, ..cfg}) (eq_lemmas.empty) private meta def uses_hyp (e : expr) (h : expr) : bool := e.fold ff $ λ t _ r, r || to_bool (t = h) private meta def rw_hyp (cfg : rewrite_cfg) : list rw_rule → expr → tactic unit | [] hyp := skip | (r::rs) hyp := do save_info r.pos, eq_lemmas ← get_rule_eqn_lemmas r, orelse' (do e ← to_expr' r.rule, when (not (uses_hyp e hyp)) $ rewrite_hyp e hyp {symm := r.symm, ..cfg} >>= rw_hyp rs) (eq_lemmas.mfirst $ λ n, do e ← mk_const n, rewrite_hyp e hyp {symm := r.symm, ..cfg} >>= rw_hyp rs) (eq_lemmas.empty) meta def rw_rule_p (ep : parser pexpr) : parser rw_rule := rw_rule.mk <$> cur_pos <*> (option.is_some <$> (with_desc "←" (tk "←" <|> tk "<-"))?) <*> ep @[derive has_reflect] meta structure rw_rules_t := (rules : list rw_rule) (end_pos : option pos) -- accepts the same content as `pexpr_list_or_texpr`, but with correct goal info pos annotations meta def rw_rules : parser rw_rules_t := (tk "[" *> rw_rules_t.mk <$> sep_by (skip_info (tk ",")) (set_goal_info_pos $ rw_rule_p (parser.pexpr 0)) <*> (some <$> cur_pos <* set_goal_info_pos (tk "]"))) <|> rw_rules_t.mk <$> (list.ret <$> rw_rule_p texpr) <*> return none private meta def rw_core (rs : parse rw_rules) (loca : parse location) (cfg : rewrite_cfg) : tactic unit := match loca with | loc.wildcard := loca.try_apply (rw_hyp cfg rs.rules) (rw_goal cfg rs.rules) | _ := loca.apply (rw_hyp cfg rs.rules) (rw_goal cfg rs.rules) end >> try (reflexivity reducible) >> (returnopt rs.end_pos >>= save_info <|> skip) /-- `rewrite e` applies identity `e` as a rewrite rule to the target of the main goal. If `e` is preceded by left arrow (`←` or `<-`), the rewrite is applied in the reverse direction. If `e` is a defined constant, then the equational lemmas associated with `e` are used. This provides a convenient way to unfold `e`. `rewrite [e₁, ..., eₙ]` applies the given rules sequentially. `rewrite e at l` rewrites `e` at location(s) `l`, where `l` is either `*` or a list of hypotheses in the local context. In the latter case, a turnstile `⊢` or `|-` can also be used, to signify the target of the goal. -/ meta def rewrite (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {}) : tactic unit := rw_core q l cfg /-- An abbreviation for `rewrite`. -/ meta def rw (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {}) : tactic unit := rw_core q l cfg /-- `rewrite` followed by `assumption`. -/ meta def rwa (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {}) : tactic unit := rewrite q l cfg >> try assumption /-- A variant of `rewrite` that uses the unifier more aggressively, unfolding semireducible definitions. -/ meta def erewrite (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {md := semireducible}) : tactic unit := rw_core q l cfg /-- An abbreviation for `erewrite`. -/ meta def erw (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {md := semireducible}) : tactic unit := rw_core q l cfg private meta def get_type_name (e : expr) : tactic name := do e_type ← infer_type e >>= whnf, (const I ls) ← return $ get_app_fn e_type, return I precedence `generalizing` : 0 /-- Assuming `x` is a variable in the local context with an inductive type, `induction x` applies induction on `x` to the main goal, producing one goal for each constructor of the inductive type, in which the target is replaced by a general instance of that constructor and an inductive hypothesis is added for each recursive argument to the constructor. If the type of an element in the local context depends on `x`, that element is reverted and reintroduced afterward, so that the inductive hypothesis incorporates that hypothesis as well. For example, given `n : nat` and a goal with a hypothesis `h : P n` and target `Q n`, `induction n` produces one goal with hypothesis `h : P 0` and target `Q 0`, and one goal with hypotheses `h : P (nat.succ a)` and `ih₁ : P a → Q a` and target `Q (nat.succ a)`. Here the names `a` and `ih₁` ire chosen automatically. `induction e`, where `e` is an expression instead of a variable, generalizes `e` in the goal, and then performs induction on the resulting variable. `induction e with y₁ ... yₙ`, where `e` is a variable or an expression, specifies that the sequence of names `y₁ ... yₙ` should be used for the arguments to the constructors and inductive hypotheses, including implicit arguments. If the list does not include enough names for all of the arguments, additional names are generated automatically. If too many names are given, the extra ones are ignored. Underscores can be used in the list, in which case the corresponding names are generated automatically. Note that for long sequences of names, the `case` tactic provides a more convenient naming mechanism. `induction e using r` allows the user to specify the principle of induction that should be used. Here `r` should be a theorem whose result type must be of the form `C t`, where `C` is a bound variable and `t` is a (possibly empty) sequence of bound variables `induction e generalizing z₁ ... zₙ`, where `z₁ ... zₙ` are variables in the local context, generalizes over `z₁ ... zₙ` before applying the induction but then introduces them in each goal. In other words, the net effect is that each inductive hypothesis is generalized. -/ meta def induction (p : parse texpr) (rec_name : parse using_ident) (ids : parse with_ident_list) (revert : parse $ (tk "generalizing" *> ident*)?) : tactic unit := do e ← i_to_expr p, -- generalize major premise e ← if e.is_local_constant then pure e else generalize e >> intro1, -- generalize major premise args (e, newvars, locals) ← do { none ← pure rec_name | pure (e, [], []), t ← infer_type e, -- TODO(Kha): `t ← whnf_ginductive t,` const n _ ← pure t.get_app_fn | pure (e, [], []), env ← get_env, tt ← pure $ env.is_inductive n | pure (e, [], []), let (locals, nonlocals) := (t.get_app_args.drop $ env.inductive_num_params n).partition (λ arg : expr, arg.is_local_constant), _ :: _ ← pure nonlocals | pure (e, [], []), n ← tactic.revert e, newvars ← nonlocals.mmap $ λ arg, do { n ← revert_kdeps arg, tactic.generalize arg, h ← intro1, intron n, -- now try to clear hypotheses that may have been abstracted away let locals := arg.fold [] (λ e _ acc, if e.is_local_constant then e::acc else acc), locals.mmap' (try ∘ clear), pure h }, intron (n-1), e ← intro1, pure (e, newvars, locals) }, -- revert `generalizing` params n ← mmap tactic.get_local (revert.get_or_else []) >>= revert_lst, tactic.induction e ids rec_name, all_goals $ do { intron n, clear_lst (newvars.map local_pp_name), (e::locals).mmap' (try ∘ clear) } private meta def find_case (goals : list expr) (ty : name) (idx : nat) (num_indices : nat) : option expr → expr → option (expr × expr) | case e := if e.has_meta_var then match e with | (mvar _ _ _) := do case ← case, guard $ e ∈ goals, pure (case, e) | (app _ _) := let idx := match e.get_app_fn with | const (name.mk_string rec ty') _ := guard (ty' = ty) >> match mk_simple_name rec with | `drec := some idx | `rec := some idx -- indices + major premise | `dcases_on := some (idx + num_indices + 1) | `cases_on := some (idx + num_indices + 1) | _ := none end | _ := none end in match idx with | none := list.foldl (<|>) (find_case case e.get_app_fn) $ e.get_app_args.map (find_case case) | some idx := let args := e.get_app_args in do arg ← args.nth idx, args.enum.foldl (λ acc ⟨i, arg⟩, match acc with | some _ := acc | _ := if i ≠ idx then find_case none arg else none end) -- start recursion with likely case (find_case (some arg) arg) end | (lam _ _ _ e) := find_case case e | (macro n args) := list.foldl (<|>) none $ args.map (find_case case) | _ := none end else none private meta def rename_lams : expr → list name → tactic unit | (lam n _ _ e) (n'::ns) := (rename n n' >> rename_lams e ns) <|> rename_lams e (n'::ns) | _ _ := skip /-- Focuses on the `induction`/`cases` subgoal corresponding to the given introduction rule, optionally renaming introduced locals. ``` example (n : ℕ) : n = n := begin induction n, case nat.zero { reflexivity }, case nat.succ a ih { reflexivity } end ``` -/ meta def case (ctor : parse ident) (ids : parse ident_*) (tac : itactic) : tactic unit := do r ← result, env ← get_env, ctor ← resolve_constant ctor <|> fail ("'" ++ to_string ctor ++ "' is not a constructor"), ty ← (env.inductive_type_of ctor).to_monad <|> fail ("'" ++ to_string ctor ++ "' is not a constructor"), let ctors := env.constructors_of ty, let idx := env.inductive_num_params ty + /- motive -/ 1 + list.index_of ctor ctors, gs ← get_goals, (case, g) ← (find_case gs ty idx (env.inductive_num_indices ty) none r ).to_monad <|> fail "could not find open goal of given case", set_goals $ g :: gs.filter (≠ g), rename_lams case ids, solve1 tac /-- Assuming `x` is a variable in the local context with an inductive type, `destruct x` splits the main goal, producing one goal for each constructor of the inductive type, in which `x` is assumed to be a general instance of that constructor. In contrast to `cases`, the local context is unchanged, i.e. no elements are reverted or introduced. For example, given `n : nat` and a goal with a hypothesis `h : P n` and target `Q n`, `destruct n` produces one goal with target `n = 0 → Q n`, and one goal with target `∀ (a : ℕ), (λ (w : ℕ), n = w → Q n) (nat.succ a)`. Here the name `a` is chosen automatically. -/ meta def destruct (p : parse texpr) : tactic unit := i_to_expr p >>= tactic.destruct private meta def generalize_arg_p : pexpr → parser (pexpr × name) | (app (app (macro _ [const `eq _ ]) h) (local_const x _ _ _)) := pure (h, x) | _ := fail "parse error" /-- `generalize : e = x` replaces all occurrences of `e` in the target with a new hypothesis `x` of the same type. `generalize h : e = x` in addition registers the hypothesis `h : e = x`. -/ meta def generalize (h : parse ident?) (p : parse $ tk ":" *> with_desc "expr = id" (parser.pexpr 0 >>= generalize_arg_p)) : tactic unit := do let (p, x) := p, e ← i_to_expr p, some h ← pure h | tactic.generalize e x >> intro1 >> skip, tgt ← target, -- if generalizing fails, fall back to not replacing anything tgt' ← do { ⟨tgt', _⟩ ← solve_aux tgt (tactic.generalize e x >> target), to_expr ``(Π x, %%e = x → %%(tgt'.binding_body.lift_vars 0 1)) } <|> to_expr ``(Π x, %%e = x → %%tgt), t ← assert h tgt', swap, exact ``(%%t %%e rfl), intro x, intro h meta def ginduction (p : parse texpr) (rec_name : parse using_ident) (ids : parse with_ident_list) : tactic unit := do x ← mk_fresh_name, let (h, hs) := (match ids with | [] := (`_h, []) | (h :: hs) := (h, hs) end : name × list name), generalize h (p, x), t ← get_local x, induction (to_pexpr t) rec_name hs ([] : list name) private meta def cases_arg_p : parser (option name × pexpr) := with_desc "(id :)? expr" $ do t ← texpr, match t with | (local_const x _ _ _) := (tk ":" *> do t ← texpr, pure (some x, t)) <|> pure (none, t) | _ := pure (none, t) end /-- Assuming `x` is a variable in the local context with an inductive type, `cases x` splits the main goal, producing one goal for each constructor of the inductive type, in which the target is replaced by a general instance of that constructor. If the type of an element in the local context depends on `x`, that element is reverted and reintroduced afterward, so that the case split affects that hypothesis as well. For example, given `n : nat` and a goal with a hypothesis `h : P n` and target `Q n`, `cases n` produces one goal with hypothesis `h : P 0` and target `Q 0`, and one goal with hypothesis `h : P (nat.succ a)` and target `Q (nat.succ a)`. Here the name `a` is chosen automatically. `cases e`, where `e` is an expression instead of a variable, generalizes `e` in the goal, and then cases on the resulting variable. `cases e with y₁ ... yₙ`, where `e` is a variable or an expression, specifies that the sequence of names `y₁ ... yₙ` should be used for the arguments to the constructors, including implicit arguments. If the list does not include enough names for all of the arguments, additional names are generated automatically. If too many names are given, the extra ones are ignored. Underscores can be used in the list, in which case the corresponding names are generated automatically. `cases h : e`, where `e` is a variable or an expression, performs cases on `e` as above, but also adds a hypothesis `h : e = ...` to each hypothesis, where `...` is the constructor instance for that particular case. -/ meta def cases : parse cases_arg_p → parse with_ident_list → tactic unit | (none, p) ids := do e ← i_to_expr p, tactic.cases e ids | (some h, p) ids := do x ← mk_fresh_name, generalize h (p, x), hx ← get_local x, tactic.cases hx ids /-- Tries to solve the current goal using a canonical proof of `true`, or the `reflexivity` tactic, or the `contradiction` tactic. -/ meta def trivial : tactic unit := tactic.triv <|> tactic.reflexivity <|> tactic.contradiction <|> fail "trivial tactic failed" /-- Closes the main goal using `sorry`. -/ meta def admit : tactic unit := tactic.admit /-- The contradiction tactic attempts to find in the current local context a hypothesis that is equivalent to an empty inductive type (e.g. `false`), a hypothesis of the form `c_1 ... = c_2 ...` where `c_1` and `c_2` are distinct constructors, or two contradictory hypotheses. -/ meta def contradiction : tactic unit := tactic.contradiction /-- `repeat { t }` repeatedly applies tactic `t` until `t` fails. The compound tactic always succeeds. -/ meta def repeat : itactic → tactic unit := tactic.repeat /-- `try { t }` tries to apply tactic `t`, but succeeds whether or not `t` succeeds. -/ meta def try : itactic → tactic unit := tactic.try /-- A do-nothing tactic that always succeeds. -/ meta def skip : tactic unit := tactic.skip /-- `solve1 { t }` applies the tactic `t` to the main goal and fails if it is not solved. -/ meta def solve1 : itactic → tactic unit := tactic.solve1 /-- `abstract id { t }` tries to use tactic `t` to solve the main goal. If it succeeds, it abstracts the goal as an independent definition or theorem with name `id`. If `id` is omitted, a name is generated automatically. -/ meta def abstract (id : parse ident?) (tac : itactic) : tactic unit := tactic.abstract tac id /-- `all_goals { t }` applies the tactic `t` to every goal, and succeeds if each application succeeds. -/ meta def all_goals : itactic → tactic unit := tactic.all_goals /-- `any_goals { t }` applies the tactic `t` to every goal, and succeeds if at least one application succeeds. -/ meta def any_goals : itactic → tactic unit := tactic.any_goals /-- `focus { t }` temporarily hides all goals other than the first, applies `t`, and then restores the other goals. It fails if there are no goals. -/ meta def focus (tac : itactic) : tactic unit := tactic.focus1 tac private meta def assume_core (n : name) (ty : pexpr) := do t ← target, when (not $ t.is_pi ∨ t.is_let) whnf_target, t ← target, when (not $ t.is_pi ∨ t.is_let) $ fail "assume tactic failed, Pi/let expression expected", ty ← i_to_expr ty, unify ty t.binding_domain, intro_core n >> skip /-- Assuming the target of the goal is a Pi or a let, `assume h : t` unifies the type of the binder with `t` and introduces it with name `h`, just like `intro h`. If `h` is absent, the tactic uses the name `this`. If `t` is omitted, it will be inferred. `assume (h₁ : t₁) ... (hₙ : tₙ)` introduces multiple hypotheses. Any of the types may be omitted, but the names must be present. -/ meta def «assume» : parse (sum.inl <$> (tk ":" *> texpr) <|> sum.inr <$> parse_binders tac_rbp) → tactic unit | (sum.inl ty) := assume_core `this ty | (sum.inr binders) := binders.mmap' $ λ b, assume_core b.local_pp_name b.local_type /-- `have h : t := p` adds the hypothesis `h : t` to the current goal if `p` a term of type `t`. If `t` is omitted, it will be inferred. `have h : t` adds the hypothesis `h : t` to the current goal and opens a new subgoal with target `t`. The new subgoal becomes the main goal. If `t` is omitted, it will be replaced by a fresh metavariable. If `h` is omitted, the name `this` is used. -/ meta def «have» (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit := let h := h.get_or_else `this in match q₁, q₂ with | some e, some p := do t ← i_to_expr e, v ← i_to_expr ``(%%p : %%t), tactic.assertv h t v | none, some p := do p ← i_to_expr p, tactic.note h none p | some e, none := i_to_expr e >>= tactic.assert h | none, none := do u ← mk_meta_univ, e ← mk_meta_var (sort u), tactic.assert h e end >> skip /-- `let h : t := p` adds the hypothesis `h : t := p` to the current goal if `p` a term of type `t`. If `t` is omitted, it will be inferred. `let h : t` adds the hypothesis `h : t := ?M` to the current goal and opens a new subgoal `?M : t`. The new subgoal becomes the main goal. If `t` is omitted, it will be replaced by a fresh metavariable. If `h` is omitted, the name `this` is used. -/ meta def «let» (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit := let h := h.get_or_else `this in match q₁, q₂ with | some e, some p := do t ← i_to_expr e, v ← i_to_expr ``(%%p : %%t), tactic.definev h t v | none, some p := do p ← i_to_expr p, tactic.pose h none p | some e, none := i_to_expr e >>= tactic.define h | none, none := do u ← mk_meta_univ, e ← mk_meta_var (sort u), tactic.define h e end >> skip /-- `suffices h : t` is the same as `have h : t, tactic.swap`. In other words, it adds the hypothesis `h : t` to the current goal and opens a new subgoal with target `t`. -/ meta def «suffices» (h : parse ident?) (t : parse (tk ":" *> texpr)?) : tactic unit := «have» h t none >> tactic.swap /-- This tactic displays the current state in the tracing buffer. -/ meta def trace_state : tactic unit := tactic.trace_state /-- `trace a` displays `a` in the tracing buffer. -/ meta def trace {α : Type} [has_to_tactic_format α] (a : α) : tactic unit := tactic.trace a /-- `existsi e` will instantiate an existential quantifier in the target with `e` and leave the instantiated body as the new target. More generally, it applies to any inductive type with one constructor and at least two arguments, applying the constructor with `e` as the first argument and leaving the remaining arguments as goals. `existsi [e₁, ..., eₙ]` iteratively does the same for each expression in the list. -/ meta def existsi : parse pexpr_list_or_texpr → tactic unit | [] := return () | (p::ps) := i_to_expr p >>= tactic.existsi >> existsi ps /-- This tactic applies to a goal such that its conclusion is an inductive type (say `I`). It tries to apply each constructor of `I` until it succeeds. -/ meta def constructor : tactic unit := tactic.constructor /-- Similar to `constructor`, but only non-dependent premises are added as new goals. -/ meta def econstructor : tactic unit := tactic.econstructor /-- Applies the first constructor when the type of the target is an inductive data type with two constructors. -/ meta def left : tactic unit := tactic.left /-- Applies the second constructor when the type of the target is an inductive data type with two constructors. -/ meta def right : tactic unit := tactic.right /-- Applies the constructor when the type of the target is an inductive data type with one constructor. -/ meta def split : tactic unit := tactic.split /-- Replaces the target of the main goal by `false`. -/ meta def exfalso : tactic unit := tactic.exfalso /-- The `injection` tactic is based on the fact that constructors of inductive data types are injections. That means that if `c` is a constructor of an inductive datatype, and if `(c t₁)` and `(c t₂)` are two terms that are equal then `t₁` and `t₂` are equal too. If `q` is a proof of a statement of conclusion `t₁ = t₂`, then injection applies injectivity to derive the equality of all arguments of `t₁` and `t₂` placed in the same positions. For example, from `(a::b) = (c::d)` we derive `a=c` and `b=d`. To use this tactic `t₁` and `t₂` should be constructor applications of the same constructor. Given `h : a::b = c::d`, the tactic `injection h` adds two new hypothesis with types `a = c` and `b = d` to the main goal. The tactic `injection h with h₁ h₂` uses the names `h₁` and `h₂` to name the new hypotheses. -/ meta def injection (q : parse texpr) (hs : parse with_ident_list) : tactic unit := do e ← i_to_expr q, tactic.injection_with e hs, try assumption /-- `injections with h₁ ... hₙ` iteratively applies `injection` to hypotheses using the names `h₁ ... hₙ`. -/ meta def injections (hs : parse with_ident_list) : tactic unit := do tactic.injections_with hs, try assumption end interactive meta structure simp_config_ext extends simp_config := (discharger : tactic unit := failed) section mk_simp_set open expr interactive.types @[derive has_reflect] meta inductive simp_arg_type : Type | all_hyps : simp_arg_type | except : name → simp_arg_type | expr : pexpr → simp_arg_type meta def simp_arg : parser simp_arg_type := (tk "*" *> return simp_arg_type.all_hyps) <|> (tk "-" *> simp_arg_type.except <$> ident) <|> (simp_arg_type.expr <$> texpr) meta def simp_arg_list : parser (list simp_arg_type) := (tk "*" *> return [simp_arg_type.all_hyps]) <|> list_of simp_arg <|> return [] private meta def resolve_exception_ids (all_hyps : bool) : list name → list name → list name → tactic (list name × list name) | [] gex hex := return (gex.reverse, hex.reverse) | (id::ids) gex hex := do p ← resolve_name id, let e := p.erase_annotations.get_app_fn.erase_annotations, match e with | const n _ := resolve_exception_ids ids (n::gex) hex | local_const n _ _ _ := when (not all_hyps) (fail $ sformat! "invalid local exception {id}, '*' was not used") >> resolve_exception_ids ids gex (n::hex) | _ := fail $ sformat! "invalid exception {id}, unknown identifier" end /- Return (hs, gex, hex, all) -/ meta def decode_simp_arg_list (hs : list simp_arg_type) : tactic $ list pexpr × list name × list name × bool := do let (hs, ex, all) := hs.foldl (λ r h, match r, h with | (es, ex, all), simp_arg_type.all_hyps := (es, ex, tt) | (es, ex, all), simp_arg_type.except id := (es, id::ex, all) | (es, ex, all), simp_arg_type.expr e := (e::es, ex, all) end) ([], [], ff), (gex, hex) ← resolve_exception_ids all ex [] [], return (hs.reverse, gex, hex, all) private meta def add_simps : simp_lemmas → list name → tactic simp_lemmas | s [] := return s | s (n::ns) := do s' ← s.add_simp n, add_simps s' ns private meta def report_invalid_simp_lemma {α : Type} (n : name): tactic α := fail format!"invalid simplification lemma '{n}' (use command 'set_option trace.simp_lemmas true' for more details)" private meta def check_no_overload (p : pexpr) : tactic unit := when p.is_choice_macro $ match p with | macro _ ps := fail $ to_fmt "ambiguous overload, possible interpretations" ++ format.join (ps.map (λ p, (to_fmt p).indent 4)) | _ := failed end private meta def simp_lemmas.resolve_and_add (s : simp_lemmas) (u : list name) (n : name) (ref : pexpr) : tactic (simp_lemmas × list name) := do p ← resolve_name n, check_no_overload p, -- unpack local refs let e := p.erase_annotations.get_app_fn.erase_annotations, match e with | const n _ := (do b ← is_valid_simp_lemma_cnst n, guard b, save_const_type_info n ref, s ← s.add_simp n, return (s, u)) <|> (do eqns ← get_eqn_lemmas_for tt n, guard (eqns.length > 0), save_const_type_info n ref, s ← add_simps s eqns, return (s, u)) <|> (do env ← get_env, guard (env.is_projection n).is_some, return (s, n::u)) <|> report_invalid_simp_lemma n | _ := (do e ← i_to_expr_no_subgoals p, b ← is_valid_simp_lemma e, guard b, try (save_type_info e ref), s ← s.add e, return (s, u)) <|> report_invalid_simp_lemma n end private meta def simp_lemmas.add_pexpr (s : simp_lemmas) (u : list name) (p : pexpr) : tactic (simp_lemmas × list name) := match p with | (const c []) := simp_lemmas.resolve_and_add s u c p | (local_const c _ _ _) := simp_lemmas.resolve_and_add s u c p | _ := do new_e ← i_to_expr_no_subgoals p, s ← s.add new_e, return (s, u) end private meta def simp_lemmas.append_pexprs : simp_lemmas → list name → list pexpr → tactic (simp_lemmas × list name) | s u [] := return (s, u) | s u (l::ls) := do (s, u) ← simp_lemmas.add_pexpr s u l, simp_lemmas.append_pexprs s u ls meta def mk_simp_set_core (no_dflt : bool) (attr_names : list name) (hs : list simp_arg_type) (at_star : bool) : tactic (bool × simp_lemmas × list name) := do (hs, gex, hex, all_hyps) ← decode_simp_arg_list hs, when (all_hyps ∧ at_star ∧ not hex.empty) $ fail "A tactic of the form `simp [*, -h] at *` is currently not supported", s ← join_user_simp_lemmas no_dflt attr_names, (s, u) ← simp_lemmas.append_pexprs s [] hs, s ← if not at_star ∧ all_hyps then do ctx ← collect_ctx_simps, let ctx := ctx.filter (λ h, h.local_uniq_name ∉ hex), -- remove local exceptions s.append ctx else return s, -- add equational lemmas, if any gex ← gex.mmap (λ n, list.cons n <$> get_eqn_lemmas_for tt n), return (all_hyps, simp_lemmas.erase s $ gex.join, u) meta def mk_simp_set (no_dflt : bool) (attr_names : list name) (hs : list simp_arg_type) : tactic (simp_lemmas × list name) := prod.snd <$> (mk_simp_set_core no_dflt attr_names hs ff) end mk_simp_set namespace interactive open interactive interactive.types expr meta def simp_core_aux (cfg : simp_config) (discharger : tactic unit) (s : simp_lemmas) (u : list name) (hs : list expr) (tgt : bool) : tactic unit := do to_remove ← hs.mfilter $ λ h, do { h_type ← infer_type h, (do (new_h_type, pr) ← simplify s u h_type cfg `eq discharger, assert h.local_pp_name new_h_type, mk_eq_mp pr h >>= tactic.exact >> return tt) <|> (return ff) }, goal_simplified ← if tgt then (simp_target s u cfg discharger >> return tt) <|> (return ff) else return ff, guard (cfg.fail_if_unchanged = ff ∨ to_remove.length > 0 ∨ goal_simplified) <|> fail "simplify tactic failed to simplify", to_remove.mmap' (λ h, try (clear h)) meta def simp_core (cfg : simp_config) (discharger : tactic unit) (no_dflt : bool) (hs : list simp_arg_type) (attr_names : list name) (locat : loc) : tactic unit := match locat with | loc.wildcard := do (all_hyps, s, u) ← mk_simp_set_core no_dflt attr_names hs tt, if all_hyps then tactic.simp_all s u cfg discharger else do hyps ← non_dep_prop_hyps, simp_core_aux cfg discharger s u hyps tt | _ := do (s, u) ← mk_simp_set no_dflt attr_names hs, ns ← locat.get_locals, simp_core_aux cfg discharger s u ns locat.include_goal end >> try tactic.triv >> try (tactic.reflexivity reducible) /-- The `simp` tactic uses lemmas and hypotheses to simplify the main goal target or non-dependent hypotheses. It has many variants. `simp` simplifies the main goal target using lemmas tagged with the attribute `[simp]`. `simp [h₁ h₂ ... hₙ]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]` and the given `hᵢ`'s, where the `hᵢ`'s are expressions. If an `hᵢ` is a defined constant `f`, then the equational lemmas associated with `f` are used. This provides a convenient way to unfold `f`. `simp [*]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]` and all hypotheses. `simp *` is a shorthand for `simp [*]`. `simp only [h₁ h₂ ... hₙ]` is like `simp [h₁ h₂ ... hₙ]` but does not use `[simp]` lemmas `simp [-id_1, ... -id_n]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]`, but removes the ones named `idᵢ`. `simp at h₁ h₂ ... hₙ` simplifies the non-dependent hypotheses `h₁ : T₁` ... `hₙ : Tₙ`. The tactic fails if the target or another hypothesis depends on one of them. The token `⊢` or `|-` can be added to the list to include the target. `simp at *` simplifies all the hypotheses and the target. `simp * at *` simplifies target and all (non-dependent propositional) hypotheses using the other hypotheses. `simp with attr₁ ... attrₙ` simplifies the main goal target using the lemmas tagged with any of the attributes `[attr₁]`, ..., `[attrₙ]` or `[simp]`. -/ meta def simp (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (locat : parse location) (cfg : simp_config_ext := {}) : tactic unit := simp_core cfg.to_simp_config cfg.discharger no_dflt hs attr_names locat /-- Just construct the simp set and trace it. Used for debugging. -/ meta def trace_simp_set (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) : tactic unit := do (s, _) ← mk_simp_set no_dflt attr_names hs, s.pp >>= trace /-- `simp_intros h₁ h₂ ... hₙ` is similar to `intros h₁ h₂ ... hₙ` except that each hypothesis is simplified as it is introduced, and each introduced hypothesis is used to simplify later ones and the final target. As with `simp`, a list of simplification lemmas can be provided. The modifiers `only` and `with` behave as with `simp`. -/ meta def simp_intros (ids : parse ident_*) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (cfg : simp_intros_config := {}) : tactic unit := do (s, u) ← mk_simp_set no_dflt attr_names hs, when (¬u.empty) (fail (sformat! "simp_intros tactic does not support {u}")), tactic.simp_intros s u ids cfg, try triv >> try (reflexivity reducible) private meta def to_simp_arg_list (es : list pexpr) : list simp_arg_type := es.map simp_arg_type.expr /-- `dsimp` is similar to `simp`, except that it only uses definitional equalities. -/ meta def dsimp (no_dflt : parse only_flag) (es : parse simp_arg_list) (attr_names : parse with_ident_list) (l : parse location) (cfg : dsimp_config := {}) : tactic unit := do (s, u) ← mk_simp_set no_dflt attr_names es, match l with | loc.wildcard := do ls ← local_context, n ← revert_lst ls, dsimp_target s u cfg, intron n | _ := l.apply (λ h, dsimp_hyp h s u cfg) (dsimp_target s u cfg) end /-- This tactic applies to a goal whose target has the form `t ~ u` where `~` is a reflexive relation, that is, a relation which has a reflexivity lemma tagged with the attribute `[refl]`. The tactic checks whether `t` and `u` are definitionally equal and then solves the goal. -/ meta def reflexivity : tactic unit := tactic.reflexivity /-- Shorter name for the tactic `reflexivity`. -/ meta def refl : tactic unit := tactic.reflexivity /-- This tactic applies to a goal whose target has the form `t ~ u` where `~` is a symmetric relation, that is, a relation which has a symmetry lemma tagged with the attribute `[symm]`. It replaces the target with `u ~ t`. -/ meta def symmetry : tactic unit := tactic.symmetry /-- This tactic applies to a goal whose target has the form `t ~ u` where `~` is a transitive relation, that is, a relation which has a transitivity lemma tagged with the attribute `[trans]`. `transitivity s` replaces the goal with the two subgoals `t ~ s` and `s ~ u`. If `s` is omitted, then a metavariable is used instead. -/ meta def transitivity (q : parse texpr?) : tactic unit := tactic.transitivity >> match q with | none := skip | some q := do (r, lhs, rhs) ← target_lhs_rhs, i_to_expr q >>= unify rhs end /-- Proves a goal with target `s = t` when `s` and `t` are equal up to the associativity and commutativity of their binary operations. -/ meta def ac_reflexivity : tactic unit := tactic.ac_refl /-- An abbreviation for `ac_reflexivity`. -/ meta def ac_refl : tactic unit := tactic.ac_refl /-- Tries to prove the main goal using congruence closure. -/ meta def cc : tactic unit := tactic.cc /-- Given hypothesis `h : x = t` or `h : t = x`, where `x` is a local constant, `subst h` substitutes `x` by `t` everywhere in the main goal and then clears `h`. -/ meta def subst (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.subst >> try (tactic.reflexivity reducible) /-- `clear h₁ ... hₙ` tries to clear each hypothesis `hᵢ` from the local context. -/ meta def clear : parse ident* → tactic unit := tactic.clear_lst private meta def to_qualified_name_core : name → list name → tactic name | n [] := fail $ "unknown declaration '" ++ to_string n ++ "'" | n (ns::nss) := do curr ← return $ ns ++ n, env ← get_env, if env.contains curr then return curr else to_qualified_name_core n nss private meta def to_qualified_name (n : name) : tactic name := do env ← get_env, if env.contains n then return n else do ns ← open_namespaces, to_qualified_name_core n ns private meta def to_qualified_names : list name → tactic (list name) | [] := return [] | (c::cs) := do new_c ← to_qualified_name c, new_cs ← to_qualified_names cs, return (new_c::new_cs) /-- Similar to `unfold`, but only uses definitional equalities. -/ meta def dunfold (cs : parse ident*) (l : parse location) (cfg : dunfold_config := {}) : tactic unit := match l with | (loc.wildcard) := do ls ← tactic.local_context, n ← revert_lst ls, new_cs ← to_qualified_names cs, dunfold_target new_cs cfg, intron n | _ := do new_cs ← to_qualified_names cs, l.apply (λ h, dunfold_hyp cs h cfg) (dunfold_target new_cs cfg) end private meta def delta_hyps : list name → list name → tactic unit | cs [] := skip | cs (h::hs) := get_local h >>= delta_hyp cs >> delta_hyps cs hs /-- Similar to `dunfold`, but performs a raw delta reduction, rather than using an equation associated with the defined constants. -/ meta def delta : parse ident* → parse location → tactic unit | cs (loc.wildcard) := do ls ← tactic.local_context, n ← revert_lst ls, new_cs ← to_qualified_names cs, delta_target new_cs, intron n | cs l := do new_cs ← to_qualified_names cs, l.apply (delta_hyp new_cs) (delta_target new_cs) private meta def unfold_projs_hyps (cfg : unfold_proj_config := {}) (hs : list name) : tactic bool := hs.mfoldl (λ r h, do h ← get_local h, (unfold_projs_hyp h cfg >> return tt) <|> return r) ff /-- This tactic unfolds all structure projections. -/ meta def unfold_projs (l : parse location) (cfg : unfold_proj_config := {}) : tactic unit := match l with | loc.wildcard := do ls ← local_context, b₁ ← unfold_projs_hyps cfg (ls.map expr.local_pp_name), b₂ ← (tactic.unfold_projs_target cfg >> return tt) <|> return ff, when (not b₁ ∧ not b₂) (fail "unfold_projs failed to simplify") | _ := l.try_apply (λ h, unfold_projs_hyp h cfg) (tactic.unfold_projs_target cfg) <|> fail "unfold_projs failed to simplify" end end interactive meta def ids_to_simp_arg_list (tac_name : name) (cs : list name) : tactic (list simp_arg_type) := cs.mmap $ λ c, do n ← resolve_name c, hs ← get_eqn_lemmas_for ff n.const_name, env ← get_env, let p := env.is_projection n.const_name, when (hs.empty ∧ p.is_none) (fail (sformat! "{tac_name} tactic failed, {c} does not have equational lemmas nor is a projection")), return $ simp_arg_type.expr (expr.const c []) structure unfold_config extends simp_config := (zeta := ff) (proj := ff) (eta := ff) (canonize_instances := ff) namespace interactive open interactive interactive.types expr /-- Given defined constants `e₁ ... eₙ`, `unfold e₁ ... eₙ` iteratively unfolds all occurrences in the target of the main goal, using equational lemmas associated with the definitions. As with `simp`, the `at` modifier can be used to specify locations for the unfolding. -/ meta def unfold (cs : parse ident*) (locat : parse location) (cfg : unfold_config := {}) : tactic unit := do es ← ids_to_simp_arg_list "unfold" cs, let no_dflt := tt, simp_core cfg.to_simp_config failed no_dflt es [] locat /-- Similar to `unfold`, but does not iterate the unfolding. -/ meta def unfold1 (cs : parse ident*) (locat : parse location) (cfg : unfold_config := {single_pass := tt}) : tactic unit := unfold cs locat cfg /-- If the target of the main goal is an `opt_param`, assigns the default value. -/ meta def apply_opt_param : tactic unit := tactic.apply_opt_param /-- If the target of the main goal is an `auto_param`, executes the associated tactic. -/ meta def apply_auto_param : tactic unit := tactic.apply_auto_param /-- Fails if the given tactic succeeds. -/ meta def fail_if_success (tac : itactic) : tactic unit := tactic.fail_if_success tac /-- Succeeds if the given tactic fails. -/ meta def success_if_fail (tac : itactic) : tactic unit := tactic.success_if_fail tac meta def guard_expr_eq (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit := do e ← to_expr p, guard (alpha_eqv t e) /-- `guard_target t` fails if the target of the main goal is not `t`. -/ meta def guard_target (p : parse texpr) : tactic unit := do t ← target, guard_expr_eq t p /-- `guard_hyp h := t` fails if the hypothesis `h` does not have type `t`. -/ meta def guard_hyp (n : parse ident) (p : parse $ tk ":=" *> texpr) : tactic unit := do h ← get_local n >>= infer_type, guard_expr_eq h p /-- `by_cases p with h` splits the main goal into two cases, assuming `h : p` in the first branch, and `h : ¬ p` in the second branch. This tactic requires that `p` is decidable. To ensure that all propositions are decidable via classical reasoning, use `local attribute classical.prop_decidable [instance]`. -/ meta def by_cases (q : parse texpr) (n : parse (tk "with" *> ident)?): tactic unit := do p ← tactic.to_expr_strict q, tactic.by_cases p (n.get_or_else `h) /-- If the target of the main goal is a proposition `p`, `by_contradiction h` reduces the goal to proving `false` using the additional hypothesis `h : ¬ p`. If `h` is omitted, a name is generated automatically. This tactic requires that `p` is decidable. To ensure that all propositions are decidable via classical reasoning, use `local attribute classical.prop_decidable [instance]`. -/ meta def by_contradiction (n : parse ident?) : tactic unit := tactic.by_contradiction n >> return () /-- An abbreviation for `by_contradiction`. -/ meta def by_contra (n : parse ident?) : tactic unit := tactic.by_contradiction n >> return () /-- Type check the given expression, and trace its type. -/ meta def type_check (p : parse texpr) : tactic unit := do e ← to_expr p, tactic.type_check e, infer_type e >>= trace /-- Fail if there are unsolved goals. -/ meta def done : tactic unit := tactic.done private meta def show_aux (p : pexpr) : list expr → list expr → tactic unit | [] r := fail "show tactic failed" | (g::gs) r := do do {set_goals [g], g_ty ← target, ty ← i_to_expr p, unify g_ty ty, set_goals (g :: r.reverse ++ gs), tactic.change ty} <|> show_aux gs (g::r) /-- `show t` finds the first goal whose target unifies with `t`. It makes that the main goal, performs the unification, and replaces the target with the unified version of `t`. -/ meta def «show» (q : parse texpr) : tactic unit := do gs ← get_goals, show_aux q gs [] /-- The tactic `specialize h a₁ ... aₙ` works on local hypothesis `h`. The premises of this hypothesis, either universal quantifications or non-dependent implications, are instantiated by concrete terms coming either from arguments `a₁` ... `aₙ`. The tactic adds a new hypothesis with the same name `h := h a₁ ... aₙ` and tries to clear the previous one. -/ meta def specialize (p : parse texpr) : tactic unit := do e ← i_to_expr p, let h := expr.get_app_fn e, if h.is_local_constant then tactic.note h.local_pp_name none e >> try (tactic.clear h) else tactic.fail "specialize requires a term of the form `h x_1 .. x_n` where `h` appears in the local context" end interactive end tactic section add_interactive open tactic /- See add_interactive -/ private meta def add_interactive_aux (new_namespace : name) : list name → command | [] := return () | (n::ns) := do env ← get_env, d_name ← resolve_constant n, (declaration.defn _ ls ty val hints trusted) ← env.get d_name, (name.mk_string h _) ← return d_name, let new_name := `tactic.interactive <.> h, add_decl (declaration.defn new_name ls ty (expr.const d_name (ls.map level.param)) hints trusted), add_interactive_aux ns /-- Copy a list of meta definitions in the current namespace to tactic.interactive. This command is useful when we want to update tactic.interactive without closing the current namespace. -/ meta def add_interactive (ns : list name) (p : name := `tactic.interactive) : command := add_interactive_aux p ns meta def has_dup : tactic bool := do ctx ← local_context, let p : name_set × bool := ctx.foldl (λ ⟨s, r⟩ h, if r then (s, r) else if s.contains h.local_pp_name then (s, tt) else (s.insert h.local_pp_name, ff)) (mk_name_set, ff), return p.2 /-- Renames hypotheses with the same name. -/ meta def dedup : tactic unit := mwhen has_dup $ do ctx ← local_context, n ← revert_lst ctx, intron n end add_interactive
5acad7cf6733ddcb58289562eccb5819adfeccca
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/order/complete_field.lean
3837dee54f36562af68723be174068adba5fd473
[ "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
13,282
lean
/- Copyright (c) 2022 Alex J. Best. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex J. Best, Yaël Dillies -/ import algebra.order.hom.ring import algebra.order.pointwise import analysis.special_functions.pow /-! # Conditionally complete linear ordered fields This file shows that the reals are unique, or, more formally, given a type satisfying the common axioms of the reals (field, conditionally complete, linearly ordered) that there is an isomorphism preserving these properties to the reals. This is `rat.induced_order_ring_iso`. Moreover this isomorphism is unique. We introduce definitions of conditionally complete linear ordered fields, and show all such are archimedean. We also construct the natural map from a `linear_ordered_field` to such a field. ## Main definitions * `conditionally_complete_linear_ordered_field`: A field satisfying the standard axiomatization of the real numbers, being a Dedekind complete and linear ordered field. * `linear_ordered_field.induced_map`: A (unique) map from any archimedean linear ordered field to a conditionally complete linear ordered field. Various bundlings are available. ## Main results * `unique.order_ring_hom` : Uniqueness of `order_ring_hom`s from an archimedean linear ordered field to a conditionally complete linear ordered field. * `unique.order_ring_iso` : Uniqueness of `order_ring_iso`s between two conditionally complete linearly ordered fields. ## References * https://mathoverflow.net/questions/362991/ who-first-characterized-the-real-numbers-as-the-unique-complete-ordered-field ## Tags reals, conditionally complete, ordered field, uniqueness -/ variables {F α β γ : Type*} noncomputable theory open function rat real set open_locale classical pointwise set_option old_structure_cmd true /-- A field which is both linearly ordered and conditionally complete with respect to the order. This axiomatizes the reals. -/ @[protect_proj, ancestor linear_ordered_field conditionally_complete_linear_order] class conditionally_complete_linear_ordered_field (α : Type*) extends linear_ordered_field α renaming max → sup min → inf, conditionally_complete_linear_order α /-- Any conditionally complete linearly ordered field is archimedean. -/ @[priority 100] -- see Note [lower instance priority] instance conditionally_complete_linear_ordered_field.to_archimedean [conditionally_complete_linear_ordered_field α] : archimedean α := archimedean_iff_nat_lt.2 begin by_contra' h, obtain ⟨x, h⟩ := h, have := cSup_le (range_nonempty (coe : ℕ → α)) (forall_range_iff.2 $ λ n, le_sub_iff_add_le.2 $ le_cSup ⟨x, forall_range_iff.2 h⟩ ⟨n + 1, nat.cast_succ n⟩), linarith, end /-- The reals are a conditionally complete linearly ordered field. -/ instance : conditionally_complete_linear_ordered_field ℝ := { ..real.linear_ordered_field, ..real.conditionally_complete_linear_order } namespace linear_ordered_field /-! ### Rational cut map The idea is that a conditionally complete linear ordered field is fully characterized by its copy of the rationals. Hence we define `rat.cut_map β : α → set β` which sends `a : α` to the "rationals in `β`" that are less than `a`. -/ section cut_map variables [linear_ordered_field α] section division_ring variables (β) [division_ring β] {a a₁ a₂ : α} {b : β} {q : ℚ} /-- The lower cut of rationals inside a linear ordered field that are less than a given element of another linear ordered field. -/ def cut_map (a : α) : set β := (coe : ℚ → β) '' {t | ↑t < a} lemma cut_map_mono (h : a₁ ≤ a₂) : cut_map β a₁ ⊆ cut_map β a₂ := image_subset _ $ λ _, h.trans_lt' variables {β} @[simp] lemma mem_cut_map_iff : b ∈ cut_map β a ↔ ∃ q : ℚ, (q : α) < a ∧ (q : β) = b := iff.rfl @[simp] lemma coe_mem_cut_map_iff [char_zero β] : (q : β) ∈ cut_map β a ↔ (q : α) < a := rat.cast_injective.mem_set_image lemma cut_map_self (a : α) : cut_map α a = Iio a ∩ range (coe : ℚ → α) := begin ext, split, { rintro ⟨q, h, rfl⟩, exact ⟨h, q, rfl⟩ }, { rintro ⟨h, q, rfl⟩, exact ⟨q, h, rfl⟩ } end end division_ring variables (β) [linear_ordered_field β] {a a₁ a₂ : α} {b : β} {q : ℚ} lemma cut_map_coe (q : ℚ) : cut_map β (q : α) = coe '' {r : ℚ | (r : β) < q} := by simp_rw [cut_map, rat.cast_lt] variables [archimedean α] lemma cut_map_nonempty (a : α) : (cut_map β a).nonempty := nonempty.image _ $ exists_rat_lt a lemma cut_map_bdd_above (a : α) : bdd_above (cut_map β a) := begin obtain ⟨q, hq⟩ := exists_rat_gt a, exact ⟨q, ball_image_iff.2 $ λ r hr, by exact_mod_cast (hq.trans' hr).le⟩, end lemma cut_map_add (a b : α) : cut_map β (a + b) = cut_map β a + cut_map β b := begin refine (image_subset_iff.2 $ λ q hq, _).antisymm _, { rw [mem_set_of_eq, ←sub_lt_iff_lt_add] at hq, obtain ⟨q₁, hq₁q, hq₁ab⟩ := exists_rat_btwn hq, refine ⟨q₁, q - q₁, _, _, add_sub_cancel'_right _ _⟩; try {norm_cast}; rwa coe_mem_cut_map_iff, exact_mod_cast sub_lt.mp hq₁q }, { rintro _ ⟨_, _, ⟨qa, ha, rfl⟩, ⟨qb, hb, rfl⟩, rfl⟩, refine ⟨qa + qb, _, by norm_cast⟩, rw [mem_set_of_eq, cast_add], exact add_lt_add ha hb } end end cut_map /-! ### Induced map `rat.cut_map` spits out a `set β`. To get something in `β`, we now take the supremum. -/ section induced_map variables (α β γ) [linear_ordered_field α] [conditionally_complete_linear_ordered_field β] [conditionally_complete_linear_ordered_field γ] /-- The induced order preserving function from a linear ordered field to a conditionally complete linear ordered field, defined by taking the Sup in the codomain of all the rationals less than the input. -/ def induced_map (x : α) : β := Sup $ cut_map β x variables [archimedean α] lemma induced_map_mono : monotone (induced_map α β) := λ a b h, cSup_le_cSup (cut_map_bdd_above β _) (cut_map_nonempty β _) (cut_map_mono β h) lemma induced_map_rat (q : ℚ) : induced_map α β (q : α) = q := begin refine cSup_eq_of_forall_le_of_forall_lt_exists_gt (cut_map_nonempty β q) (λ x h, _) (λ w h, _), { rw cut_map_coe at h, obtain ⟨r, h, rfl⟩ := h, exact le_of_lt h }, { obtain ⟨q', hwq, hq⟩ := exists_rat_btwn h, rw cut_map_coe, exact ⟨q', ⟨_, hq, rfl⟩, hwq⟩ } end @[simp] lemma induced_map_zero : induced_map α β 0 = 0 := by exact_mod_cast induced_map_rat α β 0 @[simp] lemma induced_map_one : induced_map α β 1 = 1 := by exact_mod_cast induced_map_rat α β 1 variables {α β} {a : α} {b : β} {q : ℚ} lemma induced_map_nonneg (ha : 0 ≤ a) : 0 ≤ induced_map α β a := (induced_map_zero α _).ge.trans $ induced_map_mono _ _ ha lemma coe_lt_induced_map_iff : (q : β) < induced_map α β a ↔ (q : α) < a := begin refine ⟨λ h, _, λ hq, _⟩, { rw ←induced_map_rat α at h, exact (induced_map_mono α β).reflect_lt h }, { obtain ⟨q', hq, hqa⟩ := exists_rat_btwn hq, apply lt_cSup_of_lt (cut_map_bdd_above β a) (coe_mem_cut_map_iff.mpr hqa), exact_mod_cast hq } end lemma lt_induced_map_iff : b < induced_map α β a ↔ ∃ q : ℚ, b < q ∧ (q : α) < a := ⟨λ h, (exists_rat_btwn h).imp $ λ q, and.imp_right coe_lt_induced_map_iff.1, λ ⟨q, hbq, hqa⟩, hbq.trans $ by rwa coe_lt_induced_map_iff⟩ @[simp] lemma induced_map_self (b : β) : induced_map β β b = b := eq_of_forall_rat_lt_iff_lt $ λ q, coe_lt_induced_map_iff variables (α β) @[simp] lemma induced_map_induced_map (a : α) : induced_map β γ (induced_map α β a) = induced_map α γ a := eq_of_forall_rat_lt_iff_lt $ λ q, by rw [coe_lt_induced_map_iff, coe_lt_induced_map_iff, iff.comm, coe_lt_induced_map_iff] @[simp] lemma induced_map_inv_self (b : β) : induced_map γ β (induced_map β γ b) = b := by rw [induced_map_induced_map, induced_map_self] lemma induced_map_add (x y : α) : induced_map α β (x + y) = induced_map α β x + induced_map α β y := begin rw [induced_map, cut_map_add], exact cSup_add (cut_map_nonempty β x) (cut_map_bdd_above β x) (cut_map_nonempty β y) (cut_map_bdd_above β y), end variables {α β} /-- Preparatory lemma for `induced_ring_hom`. -/ lemma le_induced_map_mul_self_of_mem_cut_map (ha : 0 < a) (b : β) (hb : b ∈ cut_map β (a * a)) : b ≤ induced_map α β a * induced_map α β a := begin obtain ⟨q, hb, rfl⟩ := hb, obtain ⟨q', hq', hqq', hqa⟩ := exists_rat_pow_btwn two_ne_zero hb (mul_self_pos.2 ha.ne'), transitivity (q' : β)^2, exact_mod_cast hqq'.le, rw pow_two at ⊢ hqa, exact mul_self_le_mul_self (by exact_mod_cast hq'.le) (le_cSup (cut_map_bdd_above β a) $ coe_mem_cut_map_iff.2 $ lt_of_mul_self_lt_mul_self ha.le hqa), end /-- Preparatory lemma for `induced_ring_hom`. -/ lemma exists_mem_cut_map_mul_self_of_lt_induced_map_mul_self (ha : 0 < a) (b : β) (hba : b < induced_map α β a * induced_map α β a) : ∃ c ∈ cut_map β (a * a), b < c := begin obtain hb | hb := lt_or_le b 0, { refine ⟨0, _, hb⟩, rw [←rat.cast_zero, coe_mem_cut_map_iff, rat.cast_zero], exact mul_self_pos.2 ha.ne' }, obtain ⟨q, hq, hbq, hqa⟩ := exists_rat_pow_btwn two_ne_zero hba (hb.trans_lt hba), rw ←cast_pow at hbq, refine ⟨(q^2 : ℚ), coe_mem_cut_map_iff.2 _, hbq⟩, rw pow_two at ⊢ hqa, push_cast, obtain ⟨q', hq', hqa'⟩ := lt_induced_map_iff.1 (lt_of_mul_self_lt_mul_self _ hqa), exact mul_self_lt_mul_self (by exact_mod_cast hq.le) (hqa'.trans' $ by assumption_mod_cast), exact induced_map_nonneg ha.le, end variables (α β) /-- `induced_map` as an additive homomorphism. -/ def induced_add_hom : α →+ β := ⟨induced_map α β, induced_map_zero α β, induced_map_add α β⟩ /-- `induced_map` as an `order_ring_hom`. -/ @[simps] def induced_order_ring_hom : α →+*o β := { monotone' := induced_map_mono _ _, ..(induced_add_hom α β).mk_ring_hom_of_mul_self_of_two_ne_zero -- reduce to the case of x = y begin -- reduce to the case of 0 < x suffices : ∀ x, 0 < x → induced_add_hom α β (x * x) = induced_add_hom α β x * induced_add_hom α β x, { rintro x, obtain h | rfl | h := lt_trichotomy x 0, { convert this (-x) (neg_pos.2 h) using 1, { rw [neg_mul, mul_neg, neg_neg] }, { simp_rw [add_monoid_hom.map_neg, neg_mul, mul_neg, neg_neg] } }, { simp only [mul_zero, add_monoid_hom.map_zero] }, { exact this x h } }, -- prove that the (Sup of rationals less than x) ^ 2 is the Sup of the set of rationals less -- than (x ^ 2) by showing it is an upper bound and any smaller number is not an upper bound refine λ x hx, cSup_eq_of_forall_le_of_forall_lt_exists_gt (cut_map_nonempty β _) _ _, exact le_induced_map_mul_self_of_mem_cut_map hx, exact exists_mem_cut_map_mul_self_of_lt_induced_map_mul_self hx, end two_ne_zero (induced_map_one _ _) } /-- The isomorphism of ordered rings between two conditionally complete linearly ordered fields. -/ def induced_order_ring_iso : β ≃+*o γ := { inv_fun := induced_map γ β, left_inv := induced_map_inv_self _ _, right_inv := induced_map_inv_self _ _, map_le_map_iff' := λ x y, begin refine ⟨λ h, _, λ h, induced_map_mono _ _ h⟩, simpa [induced_order_ring_hom, add_monoid_hom.mk_ring_hom_of_mul_self_of_two_ne_zero, induced_add_hom] using induced_map_mono γ β h, end, ..induced_order_ring_hom β γ } @[simp] lemma coe_induced_order_ring_iso : ⇑(induced_order_ring_iso β γ) = induced_map β γ := rfl @[simp] lemma induced_order_ring_iso_symm : (induced_order_ring_iso β γ).symm = induced_order_ring_iso γ β := rfl @[simp] lemma induced_order_ring_iso_self : induced_order_ring_iso β β = order_ring_iso.refl β := order_ring_iso.ext induced_map_self open order_ring_iso /-- There is a unique ordered ring homomorphism from an archimedean linear ordered field to a conditionally complete linear ordered field. -/ instance : unique (α →+*o β) := unique_of_subsingleton $ induced_order_ring_hom α β /-- There is a unique ordered ring isomorphism between two conditionally complete linear ordered fields. -/ instance : unique (β ≃+*o γ) := unique_of_subsingleton $ induced_order_ring_iso β γ end induced_map end linear_ordered_field section real variables {R S : Type*} [ordered_ring R] [linear_ordered_ring S] lemma ring_hom_monotone (hR : ∀ r : R, 0 ≤ r → ∃ s : R, s^2 = r) (f : R →+* S) : monotone f := begin intros a b hab, apply le_of_sub_nonneg, obtain ⟨s, hs⟩ := hR (b - a) (sub_nonneg.mpr hab), calc f b - f a = f (b - a) : (ring_hom.map_sub _ _ _).symm ... = f(s^2) : by rw ←hs ... = f(s)^2 : ring_hom.map_pow _ _ _ ... ≥ 0 : sq_nonneg _ end /-- There exists no nontrivial ring homomorphism `ℝ →+* ℝ`. -/ instance real.ring_hom.unique : unique (ℝ →+* ℝ) := { default := ring_hom.id ℝ, uniq := λ f, congr_arg order_ring_hom.to_ring_hom (subsingleton.elim ⟨f, ring_hom_monotone (λ r hr, ⟨real.sqrt r, sq_sqrt hr⟩) f⟩ default), } end real
83a89c0b17cefa37e2fcef7f97c8fe2b26531a50
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/normed_space/star/basic.lean
f504ce482ba0e89eb1a5a706bafcf90056452317
[ "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
12,365
lean
/- Copyright (c) 2021 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import analysis.normed.group.hom import analysis.normed_space.basic import analysis.normed_space.linear_isometry import analysis.normed_space.operator_norm import algebra.star.self_adjoint import algebra.star.unitary /-! # Normed star rings and algebras A normed star group is a normed group with a compatible `star` which is isometric. A C⋆-ring is a normed star group that is also a ring and that verifies the stronger condition `‖x⋆ * x‖ = ‖x‖^2` for all `x`. If a C⋆-ring is also a star algebra, then it is a C⋆-algebra. To get a C⋆-algebra `E` over field `𝕜`, use `[normed_field 𝕜] [star_ring 𝕜] [normed_ring E] [star_ring E] [cstar_ring E] [normed_algebra 𝕜 E] [star_module 𝕜 E]`. ## TODO - Show that `‖x⋆ * x‖ = ‖x‖^2` is equivalent to `‖x⋆ * x‖ = ‖x⋆‖ * ‖x‖`, which is used as the definition of C*-algebras in some sources (e.g. Wikipedia). -/ open_locale topological_space local postfix `⋆`:std.prec.max_plus := star /-- A normed star group is a normed group with a compatible `star` which is isometric. -/ class normed_star_group (E : Type*) [seminormed_add_comm_group E] [star_add_monoid E] : Prop := (norm_star : ∀ x : E, ‖x⋆‖ = ‖x‖) export normed_star_group (norm_star) attribute [simp] norm_star variables {𝕜 E α : Type*} section normed_star_group variables [seminormed_add_comm_group E] [star_add_monoid E] [normed_star_group E] @[simp] lemma nnnorm_star (x : E) : ‖star x‖₊ = ‖x‖₊ := subtype.ext $ norm_star _ /-- The `star` map in a normed star group is a normed group homomorphism. -/ def star_normed_add_group_hom : normed_add_group_hom E E := { bound' := ⟨1, λ v, le_trans (norm_star _).le (one_mul _).symm.le⟩, .. star_add_equiv } /-- The `star` map in a normed star group is an isometry -/ lemma star_isometry : isometry (star : E → E) := show isometry star_add_equiv, by exact add_monoid_hom_class.isometry_of_norm star_add_equiv (show ∀ x, ‖x⋆‖ = ‖x‖, from norm_star) @[priority 100] instance normed_star_group.to_has_continuous_star : has_continuous_star E := ⟨star_isometry.continuous⟩ end normed_star_group instance ring_hom_isometric.star_ring_end [normed_comm_ring E] [star_ring E] [normed_star_group E] : ring_hom_isometric (star_ring_end E) := ⟨norm_star⟩ /-- A C*-ring is a normed star ring that satifies the stronger condition `‖x⋆ * x‖ = ‖x‖^2` for every `x`. -/ class cstar_ring (E : Type*) [non_unital_normed_ring E] [star_ring E] : Prop := (norm_star_mul_self : ∀ {x : E}, ‖x⋆ * x‖ = ‖x‖ * ‖x‖) instance : cstar_ring ℝ := { norm_star_mul_self := λ x, by simp only [star, id.def, norm_mul] } namespace cstar_ring section non_unital variables [non_unital_normed_ring E] [star_ring E] [cstar_ring E] /-- In a C*-ring, star preserves the norm. -/ @[priority 100] -- see Note [lower instance priority] instance to_normed_star_group : normed_star_group E := ⟨begin intro x, by_cases htriv : x = 0, { simp only [htriv, star_zero] }, { have hnt : 0 < ‖x‖ := norm_pos_iff.mpr htriv, have hnt_star : 0 < ‖x⋆‖ := norm_pos_iff.mpr ((add_equiv.map_ne_zero_iff star_add_equiv).mpr htriv), have h₁ := calc ‖x‖ * ‖x‖ = ‖x⋆ * x‖ : norm_star_mul_self.symm ... ≤ ‖x⋆‖ * ‖x‖ : norm_mul_le _ _, have h₂ := calc ‖x⋆‖ * ‖x⋆‖ = ‖x * x⋆‖ : by rw [←norm_star_mul_self, star_star] ... ≤ ‖x‖ * ‖x⋆‖ : norm_mul_le _ _, exact le_antisymm (le_of_mul_le_mul_right h₂ hnt_star) (le_of_mul_le_mul_right h₁ hnt) }, end⟩ lemma norm_self_mul_star {x : E} : ‖x * x⋆‖ = ‖x‖ * ‖x‖ := by { nth_rewrite 0 [←star_star x], simp only [norm_star_mul_self, norm_star] } lemma norm_star_mul_self' {x : E} : ‖x⋆ * x‖ = ‖x⋆‖ * ‖x‖ := by rw [norm_star_mul_self, norm_star] lemma nnnorm_self_mul_star {x : E} : ‖x * star x‖₊ = ‖x‖₊ * ‖x‖₊ := subtype.ext norm_self_mul_star lemma nnnorm_star_mul_self {x : E} : ‖x⋆ * x‖₊ = ‖x‖₊ * ‖x‖₊ := subtype.ext norm_star_mul_self @[simp] lemma star_mul_self_eq_zero_iff (x : E) : star x * x = 0 ↔ x = 0 := by { rw [←norm_eq_zero, norm_star_mul_self], exact mul_self_eq_zero.trans norm_eq_zero } lemma star_mul_self_ne_zero_iff (x : E) : star x * x ≠ 0 ↔ x ≠ 0 := by simp only [ne.def, star_mul_self_eq_zero_iff] @[simp] lemma mul_star_self_eq_zero_iff (x : E) : x * star x = 0 ↔ x = 0 := by simpa only [star_eq_zero, star_star] using @star_mul_self_eq_zero_iff _ _ _ _ (star x) lemma mul_star_self_ne_zero_iff (x : E) : x * star x ≠ 0 ↔ x ≠ 0 := by simp only [ne.def, mul_star_self_eq_zero_iff] end non_unital section prod_pi variables {ι R₁ R₂ : Type*} {R : ι → Type*} variables [non_unital_normed_ring R₁] [star_ring R₁] [cstar_ring R₁] variables [non_unital_normed_ring R₂] [star_ring R₂] [cstar_ring R₂] variables [Π i, non_unital_normed_ring (R i)] [Π i, star_ring (R i)] /-- This instance exists to short circuit type class resolution because of problems with inference involving Π-types. -/ instance _root_.pi.star_ring' : star_ring (Π i, R i) := infer_instance variables [fintype ι] [Π i, cstar_ring (R i)] instance _root_.prod.cstar_ring : cstar_ring (R₁ × R₂) := { norm_star_mul_self := λ x, begin unfold norm, simp only [prod.fst_mul, prod.fst_star, prod.snd_mul, prod.snd_star, norm_star_mul_self, ←sq], refine le_antisymm _ _, { refine max_le _ _; rw [sq_le_sq, abs_of_nonneg (norm_nonneg _)], exact (le_max_left _ _).trans (le_abs_self _), exact (le_max_right _ _).trans (le_abs_self _) }, { rw le_sup_iff, rcases le_total (‖x.fst‖) (‖x.snd‖) with (h | h); simp [h] } end } instance _root_.pi.cstar_ring : cstar_ring (Π i, R i) := { norm_star_mul_self := λ x, begin simp only [norm, pi.mul_apply, pi.star_apply, nnnorm_star_mul_self, ←sq], norm_cast, exact (finset.comp_sup_eq_sup_comp_of_is_total (λ x : nnreal, x ^ 2) (λ x y h, by simpa only [sq] using mul_le_mul' h h) (by simp)).symm, end } instance _root_.pi.cstar_ring' : cstar_ring (ι → R₁) := pi.cstar_ring end prod_pi section unital variables [normed_ring E] [star_ring E] [cstar_ring E] @[simp] lemma norm_one [nontrivial E] : ‖(1 : E)‖ = 1 := begin have : 0 < ‖(1 : E)‖ := norm_pos_iff.mpr one_ne_zero, rw [←mul_left_inj' this.ne', ←norm_star_mul_self, mul_one, star_one, one_mul], end @[priority 100] -- see Note [lower instance priority] instance [nontrivial E] : norm_one_class E := ⟨norm_one⟩ lemma norm_coe_unitary [nontrivial E] (U : unitary E) : ‖(U : E)‖ = 1 := begin rw [←sq_eq_sq (norm_nonneg _) zero_le_one, one_pow 2, sq, ←cstar_ring.norm_star_mul_self, unitary.coe_star_mul_self, cstar_ring.norm_one], end @[simp] lemma norm_of_mem_unitary [nontrivial E] {U : E} (hU : U ∈ unitary E) : ‖U‖ = 1 := norm_coe_unitary ⟨U, hU⟩ @[simp] lemma norm_coe_unitary_mul (U : unitary E) (A : E) : ‖(U : E) * A‖ = ‖A‖ := begin nontriviality E, refine le_antisymm _ _, { calc _ ≤ ‖(U : E)‖ * ‖A‖ : norm_mul_le _ _ ... = ‖A‖ : by rw [norm_coe_unitary, one_mul] }, { calc _ = ‖(U : E)⋆ * U * A‖ : by rw [unitary.coe_star_mul_self U, one_mul] ... ≤ ‖(U : E)⋆‖ * ‖(U : E) * A‖ : by { rw [mul_assoc], exact norm_mul_le _ _ } ... = ‖(U : E) * A‖ : by rw [norm_star, norm_coe_unitary, one_mul] }, end @[simp] lemma norm_unitary_smul (U : unitary E) (A : E) : ‖U • A‖ = ‖A‖ := norm_coe_unitary_mul U A lemma norm_mem_unitary_mul {U : E} (A : E) (hU : U ∈ unitary E) : ‖U * A‖ = ‖A‖ := norm_coe_unitary_mul ⟨U, hU⟩ A @[simp] lemma norm_mul_coe_unitary (A : E) (U : unitary E) : ‖A * U‖ = ‖A‖ := calc _ = ‖((U : E)⋆ * A⋆)⋆‖ : by simp only [star_star, star_mul] ... = ‖(U : E)⋆ * A⋆‖ : by rw [norm_star] ... = ‖A⋆‖ : norm_mem_unitary_mul (star A) (unitary.star_mem U.prop) ... = ‖A‖ : norm_star _ lemma norm_mul_mem_unitary (A : E) {U : E} (hU : U ∈ unitary E) : ‖A * U‖ = ‖A‖ := norm_mul_coe_unitary A ⟨U, hU⟩ end unital end cstar_ring lemma is_self_adjoint.nnnorm_pow_two_pow [normed_ring E] [star_ring E] [cstar_ring E] {x : E} (hx : is_self_adjoint x) (n : ℕ) : ‖x ^ 2 ^ n‖₊ = ‖x‖₊ ^ (2 ^ n) := begin induction n with k hk, { simp only [pow_zero, pow_one] }, { rw [pow_succ, pow_mul', sq], nth_rewrite 0 ←(self_adjoint.mem_iff.mp hx), rw [←star_pow, cstar_ring.nnnorm_star_mul_self, ←sq, hk, pow_mul'] }, end lemma self_adjoint.nnnorm_pow_two_pow [normed_ring E] [star_ring E] [cstar_ring E] (x : self_adjoint E) (n : ℕ) : ‖x ^ 2 ^ n‖₊ = ‖x‖₊ ^ (2 ^ n) := x.prop.nnnorm_pow_two_pow _ section starₗᵢ variables [comm_semiring 𝕜] [star_ring 𝕜] variables [seminormed_add_comm_group E] [star_add_monoid E] [normed_star_group E] variables [module 𝕜 E] [star_module 𝕜 E] variables (𝕜) /-- `star` bundled as a linear isometric equivalence -/ def starₗᵢ : E ≃ₗᵢ⋆[𝕜] E := { map_smul' := star_smul, norm_map' := norm_star, .. star_add_equiv } variables {𝕜} @[simp] lemma coe_starₗᵢ : (starₗᵢ 𝕜 : E → E) = star := rfl lemma starₗᵢ_apply {x : E} : starₗᵢ 𝕜 x = star x := rfl end starₗᵢ section mul open continuous_linear_map variables (𝕜) [densely_normed_field 𝕜] [non_unital_normed_ring E] [star_ring E] [cstar_ring E] variables [normed_space 𝕜 E] [is_scalar_tower 𝕜 E E] [smul_comm_class 𝕜 E E] (a : E) /-- In a C⋆-algebra `E`, either unital or non-unital, multiplication on the left by `a : E` has norm equal to the norm of `a`. -/ @[simp] lemma op_nnnorm_mul : ‖mul 𝕜 E a‖₊ = ‖a‖₊ := begin rw ←Sup_closed_unit_ball_eq_nnnorm, refine cSup_eq_of_forall_le_of_forall_lt_exists_gt _ _ (λ r hr, _), { exact (metric.nonempty_closed_ball.mpr zero_le_one).image _ }, { rintro - ⟨x, hx, rfl⟩, exact ((mul 𝕜 E a).unit_le_op_norm x $ mem_closed_ball_zero_iff.mp hx).trans (op_norm_mul_apply_le 𝕜 E a) }, { have ha : 0 < ‖a‖₊ := zero_le'.trans_lt hr, rw [←inv_inv (‖a‖₊), nnreal.lt_inv_iff_mul_lt (inv_ne_zero ha.ne')] at hr, obtain ⟨k, hk₁, hk₂⟩ := normed_field.exists_lt_nnnorm_lt 𝕜 (mul_lt_mul_of_pos_right hr $ nnreal.inv_pos.2 ha), refine ⟨_, ⟨k • star a, _, rfl⟩, _⟩, { simpa only [mem_closed_ball_zero_iff, norm_smul, one_mul, norm_star] using (nnreal.le_inv_iff_mul_le ha.ne').1 (one_mul ‖a‖₊⁻¹ ▸ hk₂.le : ‖k‖₊ ≤ ‖a‖₊⁻¹) }, { simp only [map_smul, nnnorm_smul, mul_apply', mul_smul_comm, cstar_ring.nnnorm_self_mul_star], rwa [←nnreal.div_lt_iff (mul_pos ha ha).ne', div_eq_mul_inv, mul_inv, ←mul_assoc] } }, end /-- In a C⋆-algebra `E`, either unital or non-unital, multiplication on the right by `a : E` has norm eqaul to the norm of `a`. -/ @[simp] lemma op_nnnorm_mul_flip : ‖(mul 𝕜 E).flip a‖₊ = ‖a‖₊ := begin rw [←Sup_unit_ball_eq_nnnorm, ←nnnorm_star, ←@op_nnnorm_mul 𝕜 E, ←Sup_unit_ball_eq_nnnorm], congr' 1, simp only [mul_apply', flip_apply], refine set.subset.antisymm _ _; rintro - ⟨b, hb, rfl⟩; refine ⟨star b, by simpa only [norm_star, mem_ball_zero_iff] using hb, _⟩, { simp only [←star_mul, nnnorm_star] }, { simpa using (nnnorm_star (star b * a)).symm } end variables (E) /-- In a C⋆-algebra `E`, either unital or non-unital, the left regular representation is an isometry. -/ lemma mul_isometry : isometry (mul 𝕜 E) := add_monoid_hom_class.isometry_of_norm _ (λ a, congr_arg coe $ op_nnnorm_mul 𝕜 a) /-- In a C⋆-algebra `E`, either unital or non-unital, the right regular anti-representation is an isometry. -/ lemma mul_flip_isometry : isometry (mul 𝕜 E).flip := add_monoid_hom_class.isometry_of_norm _ (λ a, congr_arg coe $ op_nnnorm_mul_flip 𝕜 a) end mul
b9b7f112734e37c1879460f8549f6f17ff575a48
28be2ab6091504b6ba250b367205fb94d50ab284
/src/tactic/modded.lean
a7f691e5ecc723d8674e5a4bfab0735cb38b4b0e
[ "Apache-2.0" ]
permissive
postmasters/natural_number_game
87304ac22e5e1c5ac2382d6e523d6914dd67a92d
38a7adcdfdb18c49c87b37831736c8f15300d821
refs/heads/master
1,649,856,819,031
1,586,444,676,000
1,586,444,676,000
255,006,061
0
0
Apache-2.0
1,586,664,599,000
1,586,664,598,000
null
UTF-8
Lean
false
false
3,522
lean
-- modded induction', cases', rw' -- *TODO* -- waiting for simon PR for symmetry' import tactic.interactive run_cmd mk_simp_attr `leakage open lean open lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic namespace interactive open interactive interactive.types expr private meta def resolve_name' (n : name) : tactic expr := do { p ← resolve_name n, match p with | expr.const n _ := mk_const n -- create metavars for universe levels | _ := i_to_expr p end } private meta def rw_goal (cfg : rewrite_cfg) (rs : list rw_rule) : tactic unit := rs.mmap' $ λ r, do save_info r.pos, eq_lemmas ← get_rule_eqn_lemmas r, orelse' (do e ← to_expr' r.rule, rewrite_target e {symm := r.symm, ..cfg}) (eq_lemmas.mfirst $ λ n, do e ← mk_const n, rewrite_target e {symm := r.symm, ..cfg}) (eq_lemmas.empty) private meta def uses_hyp (e : expr) (h : expr) : bool := e.fold ff $ λ t _ r, r || to_bool (t = h) private meta def rw_hyp (cfg : rewrite_cfg) : list rw_rule → expr → tactic unit | [] hyp := skip | (r::rs) hyp := do save_info r.pos, eq_lemmas ← get_rule_eqn_lemmas r, orelse' (do e ← to_expr' r.rule, when (not (uses_hyp e hyp)) $ rewrite_hyp e hyp {symm := r.symm, ..cfg} >>= rw_hyp rs) (eq_lemmas.mfirst $ λ n, do e ← mk_const n, rewrite_hyp e hyp {symm := r.symm, ..cfg} >>= rw_hyp rs) (eq_lemmas.empty) private meta def rw_core (rs : parse rw_rules) (loca : parse location) (cfg : rewrite_cfg) : tactic unit := match loca with | loc.wildcard := loca.try_apply (rw_hyp cfg rs.rules) (rw_goal cfg rs.rules) | _ := loca.apply (rw_hyp cfg rs.rules) (rw_goal cfg rs.rules) end >> (returnopt rs.end_pos >>= save_info <|> skip) meta def rw' (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {}) : tactic unit := propagate_tags (rw_core q l cfg) meta def use' (l : parse pexpr_list_or_texpr) : tactic unit := tactic.use l end interactive end tactic namespace tactic.interactive open tactic.interactive interactive.types expr lean lean.parser tactic interactive meta def induction' (hp : parse cases_arg_p) (ids : parse with_ident_list) : tactic unit := do tactic.interactive.induction hp none ids none, all_goals `[try { dsimp only with leakage at * }] --meta def induction' (hp : parse cases_arg_p) (ids : parse with_ident_list) : tactic unit := --do tactic.interactive.induction hp none ids none, --all_goals `[ -- try {rw' (show mynat.zero = (0 : mynat), from rfl) at *}, -- try {change mynat.le with (≤) at *}] -- try {change @mynat.succ with (λ n, n + 1) at *, dsimp only at *}] meta def cases' (hp : parse cases_arg_p) (ids : parse with_ident_list) : tactic unit := do tactic.interactive.cases hp ids, all_goals `[try { dsimp only with leakage at * }] --meta def cases' : parse cases_arg_p → parse with_ident_list → tactic unit --| (none, p) ids := do -- e ← i_to_expr p, -- cases_core e ids, -- all_goals `[ -- try {rw' (show mynat.zero = (0 : mynat), from rfl) at *}, -- try {rw' (show mynat.le = (≤), from rfl) at *} -- ] --| (some h, p) ids := do -- x ← get_unused_name, -- generalize h () (p, x), -- hx ← get_local x, -- cases_core hx ids, -- all_goals `[ -- try {rw' (show mynat.zero = (0 : mynat), from rfl) at *}, -- try {rw' (show mynat.le = (≤), from rfl) at *}] -- next def is waiting on https://github.com/leanprover-community/mathlib/pull/1269 --meta def := symmetry' -- already there end tactic.interactive
9e8f505447b7815fab20f73d081737922744cdd7
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/category_theory/limits/shapes/finite_limits.lean
956e5f972f4d39e7996383a1911ad65bd7a043f1
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
1,997
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.shapes.products import category_theory.discrete_category import data.fintype universes v u open category_theory namespace category_theory.limits /-- A category with a `fintype` of objects, and a `fintype` for each morphism space. -/ class fin_category (J : Type v) [small_category J] := (decidable_eq_obj : decidable_eq J . tactic.apply_instance) (fintype_obj : fintype J . tactic.apply_instance) (decidable_eq_hom : Π (j j' : J), decidable_eq (j ⟶ j') . tactic.apply_instance) (fintype_hom : Π (j j' : J), fintype (j ⟶ j') . tactic.apply_instance) attribute [instance] fin_category.decidable_eq_obj fin_category.fintype_obj fin_category.decidable_eq_hom fin_category.fintype_hom -- We need a `decidable_eq` instance here to construct `fintype` on the morphism spaces. instance fin_category_discrete_of_decidable_fintype (J : Type v) [fintype J] [decidable_eq J] : fin_category (discrete J) := { } variables (C : Type u) [𝒞 : category.{v} C] include 𝒞 class has_finite_limits := (has_limits_of_shape : Π (J : Type v) [small_category J] [fin_category J], has_limits_of_shape.{v} J C) class has_finite_colimits := (has_colimits_of_shape : Π (J : Type v) [small_category J] [fin_category J], has_colimits_of_shape.{v} J C) attribute [instance, priority 100] -- see Note [lower instance priority] has_finite_limits.has_limits_of_shape has_finite_colimits.has_colimits_of_shape @[priority 100] -- see Note [lower instance priority] instance [has_limits.{v} C] : has_finite_limits.{v} C := { has_limits_of_shape := λ J _ _, by { resetI, apply_instance } } @[priority 100] -- see Note [lower instance priority] instance [has_colimits.{v} C] : has_finite_colimits.{v} C := { has_colimits_of_shape := λ J _ _, by { resetI, apply_instance } } end category_theory.limits
294609e6de6c6ad9ce7474a9bd6081156e46e341
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/ring_theory/eisenstein_criterion.lean
af7543b8a44ef1adbe519e3178f9e38d7a1ea336
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
5,563
lean
/- Copyright (c) 2020 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import ring_theory.prime import ring_theory.polynomial.content /-! # Eisenstein's criterion A proof of a slight generalisation of Eisenstein's criterion for the irreducibility of a polynomial over an integral domain. -/ open polynomial ideal.quotient variables {R : Type*} [comm_ring R] namespace polynomial namespace eisenstein_criterion_aux /- Section for auxiliary lemmas used in the proof of `irreducible_of_eisenstein_criterion`-/ lemma map_eq_C_mul_X_pow_of_forall_coeff_mem {f : polynomial R} {P : ideal R} (hfP : ∀ (n : ℕ), ↑n < f.degree → f.coeff n ∈ P) : map (mk P) f = C ((mk P) f.leading_coeff) * X ^ f.nat_degree := polynomial.ext (λ n, begin by_cases hf0 : f = 0, { simp [hf0], }, rcases lt_trichotomy ↑n (degree f) with h | h | h, { erw [coeff_map, eq_zero_iff_mem.2 (hfP n h), coeff_C_mul, coeff_X_pow, if_neg, mul_zero], rintro rfl, exact not_lt_of_ge degree_le_nat_degree h }, { have : nat_degree f = n, from nat_degree_eq_of_degree_eq_some h.symm, rw [coeff_C_mul, coeff_X_pow, if_pos this.symm, mul_one, leading_coeff, this, coeff_map] }, { rw [coeff_eq_zero_of_degree_lt, coeff_eq_zero_of_degree_lt], { refine lt_of_le_of_lt (degree_C_mul_X_pow_le _ _) _, rwa ← degree_eq_nat_degree hf0 }, { exact lt_of_le_of_lt (degree_map_le _ _) h } } end) lemma le_nat_degree_of_map_eq_mul_X_pow {n : ℕ} {P : ideal R} (hP : P.is_prime) {q : polynomial R} {c : polynomial (R ⧸ P)} (hq : map (mk P) q = c * X ^ n) (hc0 : c.degree = 0) : n ≤ q.nat_degree := with_bot.coe_le_coe.1 (calc ↑n = degree (q.map (mk P)) : by rw [hq, degree_mul, hc0, zero_add, degree_pow, degree_X, nsmul_one, nat.cast_with_bot] ... ≤ degree q : degree_map_le _ _ ... ≤ nat_degree q : degree_le_nat_degree) lemma eval_zero_mem_ideal_of_eq_mul_X_pow {n : ℕ} {P : ideal R} {q : polynomial R} {c : polynomial (R ⧸ P)} (hq : map (mk P) q = c * X ^ n) (hn0 : 0 < n) : eval 0 q ∈ P := by rw [← coeff_zero_eq_eval_zero, ← eq_zero_iff_mem, ← coeff_map, coeff_zero_eq_eval_zero, hq, eval_mul, eval_pow, eval_X, zero_pow hn0, mul_zero] lemma is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit {p q : polynomial R} (hu : ∀ (x : R), C x ∣ p * q → is_unit x) (hpm : p.nat_degree = 0) : is_unit p := begin rw [eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpm), is_unit_C], refine hu _ _, rw [← eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpm)], exact dvd_mul_right _ _ end end eisenstein_criterion_aux open eisenstein_criterion_aux variables [is_domain R] /-- If `f` is a non constant polynomial with coefficients in `R`, and `P` is a prime ideal in `R`, then if every coefficient in `R` except the leading coefficient is in `P`, and the trailing coefficient is not in `P^2` and no non units in `R` divide `f`, then `f` is irreducible. -/ theorem irreducible_of_eisenstein_criterion {f : polynomial R} {P : ideal R} (hP : P.is_prime) (hfl : f.leading_coeff ∉ P) (hfP : ∀ n : ℕ, ↑n < degree f → f.coeff n ∈ P) (hfd0 : 0 < degree f) (h0 : f.coeff 0 ∉ P^2) (hu : f.is_primitive) : irreducible f := have hf0 : f ≠ 0, from λ _, by simp only [*, not_true, submodule.zero_mem, coeff_zero] at *, have hf : f.map (mk P) = C (mk P (leading_coeff f)) * X ^ nat_degree f, from map_eq_C_mul_X_pow_of_forall_coeff_mem hfP, have hfd0 : 0 < f.nat_degree, from with_bot.coe_lt_coe.1 (lt_of_lt_of_le hfd0 degree_le_nat_degree), ⟨mt degree_eq_zero_of_is_unit (λ h, by simp only [*, lt_irrefl] at *), begin rintros p q rfl, rw [map_mul] at hf, rcases mul_eq_mul_prime_pow (show prime (X : polynomial (R ⧸ P)), from monic_X.prime_of_degree_eq_one degree_X) hf with ⟨m, n, b, c, hmnd, hbc, hp, hq⟩, have hmn : 0 < m → 0 < n → false, { assume hm0 hn0, refine h0 _, rw [coeff_zero_eq_eval_zero, eval_mul, sq], exact ideal.mul_mem_mul (eval_zero_mem_ideal_of_eq_mul_X_pow hp hm0) (eval_zero_mem_ideal_of_eq_mul_X_pow hq hn0) }, have hpql0 : (mk P) (p * q).leading_coeff ≠ 0, { rwa [ne.def, eq_zero_iff_mem] }, have hp0 : p ≠ 0, from λ h, by simp only [*, zero_mul, eq_self_iff_true, not_true, ne.def] at *, have hq0 : q ≠ 0, from λ h, by simp only [*, eq_self_iff_true, not_true, ne.def, mul_zero] at *, have hbc0 : degree b = 0 ∧ degree c = 0, { apply_fun degree at hbc, rwa [degree_C hpql0, degree_mul, eq_comm, nat.with_bot.add_eq_zero_iff] at hbc }, have hmp : m ≤ nat_degree p, from le_nat_degree_of_map_eq_mul_X_pow hP hp hbc0.1, have hnq : n ≤ nat_degree q, from le_nat_degree_of_map_eq_mul_X_pow hP hq hbc0.2, have hpmqn : p.nat_degree = m ∧ q.nat_degree = n, { rw [nat_degree_mul hp0 hq0] at hmnd, clear_except hmnd hmp hnq, contrapose hmnd, apply ne_of_lt, rw not_and_distrib at hmnd, cases hmnd, { exact add_lt_add_of_lt_of_le (lt_of_le_of_ne hmp (ne.symm hmnd)) hnq }, { exact add_lt_add_of_le_of_lt hmp (lt_of_le_of_ne hnq (ne.symm hmnd)) } }, obtain rfl | rfl : m = 0 ∨ n = 0, { rwa [pos_iff_ne_zero, pos_iff_ne_zero, imp_false, not_not, ← or_iff_not_imp_left] at hmn }, { exact or.inl (is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit hu hpmqn.1) }, { exact or.inr (is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit (by simpa only [mul_comm] using hu) hpmqn.2) } end⟩ end polynomial
e74e23ee670c295b6c422962d9d038642ab2931c
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/number_theory/dioph.lean
821d7771976e1d9a09050102979d749bafee5733
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,944
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 number_theory.pell import data.pfun import data.fin2 import data.vector3 /-! # 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 `S ⊆ ℕ^α` is Diophantine, i.e. that * `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` -/ universe u open nat function namespace int lemma eq_nat_abs_iff_mul (x n) : nat_abs x = n ↔ (x - n) * (x + n) = 0 := begin refine iff.trans _ mul_eq_zero.symm, refine iff.trans _ (or_congr sub_eq_zero add_eq_zero_iff_eq_neg).symm, exact ⟨λe, by rw ← e; apply nat_abs_eq, λo, by cases o; subst x; simp [nat_abs_of_nat]⟩ end end int open fin2 /-- `list_all p l` is equivalent to `∀ a ∈ l, p a`, but unfolds directly to a conjunction, i.e. `list_all p [0, 1, 2] = p 0 ∧ p 1 ∧ p 2`. -/ @[simp] def list_all {α} (p : α → Prop) : list α → Prop | [] := true | (x :: []) := p x | (x :: l) := p x ∧ list_all l @[simp] theorem list_all_cons {α} (p : α → Prop) (x : α) : ∀ (l : list α), list_all p (x :: l) ↔ p x ∧ list_all p l | [] := (and_true _).symm | (x :: l) := iff.rfl theorem list_all_iff_forall {α} (p : α → Prop) : ∀ (l : list α), list_all p l ↔ ∀ x ∈ l, p x | [] := (iff_true_intro $ list.ball_nil _).symm | (x :: l) := by rw [list.ball_cons, ← list_all_iff_forall l]; simp theorem list_all.imp {α} {p q : α → Prop} (h : ∀ x, p x → q x) : ∀ {l : list α}, list_all p l → list_all q l | [] := id | (x :: l) := by simpa using and.imp (h x) list_all.imp @[simp] theorem list_all_map {α β} {p : β → Prop} (f : α → β) {l : list α} : list_all p (l.map f) ↔ list_all (p ∘ f) l := by induction l; simp * theorem list_all_congr {α} {p q : α → Prop} (h : ∀ x, p x ↔ q x) {l : list α} : list_all p l ↔ list_all q l := ⟨list_all.imp (λx, (h x).1), list_all.imp (λx, (h x).2)⟩ instance decidable_list_all {α} (p : α → Prop) [decidable_pred p] (l : list α) : decidable (list_all p l) := decidable_of_decidable_of_iff (by apply_instance) (list_all_iff_forall _ _).symm /- poly -/ /-- 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) /-- The type of multivariate integer polynomials -/ def poly (α : Type u) := {f : (α → ℕ) → ℤ // is_poly f} namespace poly section parameter {α : Type u} instance : has_coe_to_fun (poly α) := ⟨_, λ f, f.1⟩ /-- The underlying function of a `poly` is a polynomial -/ lemma isp (f : poly α) : is_poly f := f.2 /-- Extensionality for `poly α` -/ lemma ext {f g : poly α} (e : ∀x, f x = g x) : f = g := subtype.eq (funext e) /-- Construct a `poly` given an extensionally equivalent `poly`. -/ def subst (f : poly α) (g : (α → ℕ) → ℤ) (e : ∀x, f x = g x) : poly α := ⟨g, by rw ← (funext e : coe_fn f = g); exact f.isp⟩ @[simp] theorem subst_eval (f g e x) : subst f g e x = g x := rfl /-- The `i`th projection function, `x_i`. -/ def proj (i) : poly α := ⟨_, is_poly.proj i⟩ @[simp] theorem proj_eval (i x) : proj i x = x i := rfl /-- The constant function with value `n : ℤ`. -/ def const (n) : poly α := ⟨_, is_poly.const n⟩ @[simp] theorem const_eval (n x) : const n x = n := rfl /-- The zero polynomial -/ def zero : poly α := const 0 instance : has_zero (poly α) := ⟨poly.zero⟩ @[simp] theorem zero_eval (x) : (0 : poly α) x = 0 := rfl /-- The zero polynomial -/ def one : poly α := const 1 instance : has_one (poly α) := ⟨poly.one⟩ @[simp] theorem one_eval (x) : (1 : poly α) x = 1 := rfl /-- Subtraction of polynomials -/ def sub : poly α → poly α → poly α | ⟨f, pf⟩ ⟨g, pg⟩ := ⟨_, is_poly.sub pf pg⟩ instance : has_sub (poly α) := ⟨poly.sub⟩ @[simp] theorem sub_eval : Π (f g x), (f - g : poly α) x = f x - g x | ⟨f, pf⟩ ⟨g, pg⟩ x := rfl /-- Negation of a polynomial -/ def neg (f : poly α) : poly α := 0 - f instance : has_neg (poly α) := ⟨poly.neg⟩ @[simp] theorem neg_eval (f x) : (-f : poly α) x = -f x := show (0-f) x = _, by simp /-- Addition of polynomials -/ def add : poly α → poly α → poly α | ⟨f, pf⟩ ⟨g, pg⟩ := subst (⟨f, pf⟩ - -⟨g, pg⟩) _ (λx, show f x - (0 - g x) = f x + g x, by simp) instance : has_add (poly α) := ⟨poly.add⟩ @[simp] theorem add_eval : Π (f g x), (f + g : poly α) x = f x + g x | ⟨f, pf⟩ ⟨g, pg⟩ x := rfl /-- Multiplication of polynomials -/ def mul : poly α → poly α → poly α | ⟨f, pf⟩ ⟨g, pg⟩ := ⟨_, is_poly.mul pf pg⟩ instance : has_mul (poly α) := ⟨poly.mul⟩ @[simp] theorem mul_eval : Π (f g x), (f * g : poly α) x = f x * g x | ⟨f, pf⟩ ⟨g, pg⟩ x := rfl instance : comm_ring (poly α) := by refine_struct { add := (+), zero := (0 : poly α), neg := has_neg.neg, mul := (*), one := 1, sub := has_sub.sub, npow := @npow_rec _ ⟨1⟩ ⟨(*)⟩, nsmul := @nsmul_rec _ ⟨0⟩ ⟨(+)⟩, gsmul := @gsmul_rec _ ⟨0⟩ ⟨(+)⟩ ⟨neg⟩ }; 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 theorem 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) theorem sumsq_eq_zero (x) : ∀ l, sumsq l x = 0 ↔ list_all (λa : poly α, a x = 0) l | [] := 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 remap {α β} (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] theorem remap_eval {α β} (f : α → β) (g : poly α) (v) : remap f g v = g (v ∘ f) := rfl end poly namespace sum /-- combine two functions into a function on the disjoint union -/ def join {α β γ} (f : α → γ) (g : β → γ) : α ⊕ β → γ := by {refine sum.rec _ _, exacts [f, g]} end sum local infixr ` ⊗ `:65 := sum.join open sum namespace option /-- Functions from `option` can be combined similarly to `vector.cons` -/ def cons {α β} (a : β) (v : α → β) : option α → β := by {refine option.rec _ _, exacts [a, v]} notation a :: b := cons a b @[simp] theorem cons_head_tail {α β} (v : option α → β) : v none :: v ∘ some = v := funext $ λo, by cases o; refl end option /- dioph -/ /-- 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} theorem ext {S S' : set (α → ℕ)} (d : dioph S) (H : ∀v, S v ↔ S' v) : dioph S' := eq.rec d $ show S = S', from set.ext H theorem of_no_dummies (S : set (α → ℕ)) (p : poly α) (h : ∀ (v : α → ℕ), S v ↔ p v = 0) : dioph S := ⟨ulift empty, p.remap inl, λv, (h v).trans ⟨λh, ⟨λt, empty.rec _ t.down, by simp; rw [ show (v ⊗ λt:ulift empty, empty.rec _ t.down) ∘ inl = v, from rfl, h]⟩, λ⟨t, ht⟩, by simp at ht; rwa [show (v ⊗ t) ∘ inl = v, from rfl] at 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.remap (inl ⊗ (inr ∘ f)) (v ⊗ t) = 0) := begin simp, 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 [join, (∘)]; 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 theorem inject_dummies {S : set (α → ℕ)} (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.remap (inl ⊗ (inr ∘ f)), λv, (h v).trans $ inject_dummies_lem f g inv _ _⟩ theorem reindex_dioph {S : set (α → ℕ)} : Π (d : dioph S) (f : α → β), dioph (λv, S (v ∘ f)) | ⟨γ, p, pe⟩ f := ⟨γ, p.remap ((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⟩ theorem dioph_list_all (l) (d : list_all dioph l) : dioph (λv, list_all (λS : set (α → ℕ), S v) l) := 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.remap (inl ⊗ inr ∘ inl) :: pl.map (λq, q.remap (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 theorem and_dioph {S S' : set (α → ℕ)} (d : dioph S) (d' : dioph S') : dioph (λv, S v ∧ S' v) := dioph_list_all [S, S'] ⟨d, d'⟩ theorem or_dioph {S S' : set (α → ℕ)} : ∀ (d : dioph S) (d' : dioph S'), dioph (λv, S v ∨ S' v) | ⟨β, p, pe⟩ ⟨γ, q, qe⟩ := ⟨β ⊕ γ, p.remap (inl ⊗ inr ∘ inl) * q.remap (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 : (α → ℕ) →. ℕ) := dioph (λv : option α → ℕ, f.graph (v ∘ some, v none)) /-- A function is Diophantine if its graph is Diophantine. -/ def dioph_fn (f : (α → ℕ) → ℕ) := dioph (λv : option α → ℕ, f (v ∘ some) = v none) theorem reindex_dioph_fn {f : (α → ℕ) → ℕ} (d : dioph_fn f) (g : α → β) : dioph_fn (λv, f (v ∘ g)) := reindex_dioph d (functor.map g) theorem ex_dioph {S : set (α ⊕ β → ℕ)} : dioph S → dioph (λv, ∃x, S (v ⊗ x)) | ⟨γ, p, pe⟩ := ⟨β ⊕ γ, p.remap ((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⟩⟩⟩⟩ theorem ex1_dioph {S : set (option α → ℕ)} : dioph S → dioph (λv, ∃x, S (x :: v)) | ⟨β, p, pe⟩ := ⟨option β, p.remap (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 theorem abs_poly_dioph (p : poly α) : dioph_fn (λv, (p v).nat_abs) := by refine of_no_dummies _ ((p.remap some - poly.proj none) * (p.remap some + poly.proj none)) (λv, _); apply int.eq_nat_abs_iff_mul 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, S (f.fn v h :: v)) := ext (ex1_dioph (and_dioph d 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 : α → ℕ, S (f v :: v)) := ext (dioph_pfun_comp1 d (cast (dioph_fn_iff_pfun f) df)) $ λv, ⟨λ⟨_, h⟩, h, λh, ⟨trivial, h⟩⟩ end section variables {α β γ : Type} open vector3 open_locale vector3 theorem dioph_fn_vec_comp1 {n} {S : set (vector3 ℕ (succ n))} (d : dioph S) {f : (vector3 ℕ n) → ℕ} (df : dioph_fn f) : dioph (λv : vector3 ℕ n, S (cons (f v) v)) := ext (dioph_fn_comp1 (reindex_dioph d (none :: some)) df) $ λv, by rw [ show option.cons (f v) v ∘ (cons none some) = f v :: v, from funext $ λs, by cases s with a b; refl] theorem vec_ex1_dioph (n) {S : set (vector3 ℕ (succ n))} (d : dioph S) : dioph (λv : vector3 ℕ n, ∃x, S (x :: v)) := ext (ex1_dioph $ reindex_dioph d (none :: some)) $ λv, exists_congr $ λx, by rw [ show (option.cons x v) ∘ (cons none some) = x :: v, from funext $ λs, by cases s with a b; refl] theorem dioph_fn_vec {n} (f : vector3 ℕ n → ℕ) : dioph_fn f ↔ dioph (λv : vector3 ℕ (succ n), f (v ∘ fs) = v fz) := ⟨λh, reindex_dioph h (fz :: fs), λh, reindex_dioph h (none :: some)⟩ theorem dioph_pfun_vec {n} (f : vector3 ℕ n →. ℕ) : dioph_pfun f ↔ dioph (λv : vector3 ℕ (succ n), f.graph (v ∘ fs, v fz)) := ⟨λh, reindex_dioph h (fz :: fs), λh, reindex_dioph h (none :: some)⟩ theorem dioph_fn_compn {α : Type} : ∀ {n} {S : set (α ⊕ fin2 n → ℕ)} (d : dioph S) {f : vector3 ((α → ℕ) → ℕ) n} (df : vector_allp dioph_fn f), dioph (λv : α → ℕ, S (v ⊗ λi, f i v)) | 0 S d f := λdf, ext (reindex_dioph d (id ⊗ fin2.elim0)) $ λv, by refine eq.to_iff (congr_arg S $ funext $ λs, _); {cases s with a b, refl, cases b} | (succ n) S d f := f.cons_elim $ λf fl, by simp; exact λ df dfl, have dioph (λv, S (v ∘ inl ⊗ f (v ∘ inl) :: v ∘ inr)), from ext (dioph_fn_comp1 (reindex_dioph d (some ∘ inl ⊗ none :: some ∘ inr)) (reindex_dioph_fn df inl)) $ λv, by {refine eq.to_iff (congr_arg S $ funext $ λs, _); cases s with a b, refl, cases b; refl}, have dioph (λv, S (v ⊗ f v :: λ (i : fin2 n), fl i v)), from @dioph_fn_compn n (λv, S (v ∘ inl ⊗ f (v ∘ inl) :: v ∘ inr)) this _ dfl, ext this $ λv, by rw [ show cons (f v) (λ (i : fin2 n), fl i v) = λ (i : fin2 (succ n)), (f :: fl) i v, from funext $ λs, by cases s with a b; refl] theorem dioph_comp {n} {S : set (vector3 ℕ n)} (d : dioph S) (f : vector3 ((α → ℕ) → ℕ) n) (df : vector_allp dioph_fn f) : dioph (λv, S (λi, f i v)) := dioph_fn_compn (reindex_dioph d inr) df theorem dioph_fn_comp {n} {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 x ` D∧ `:35 y := dioph.and_dioph x y" in dioph localized "notation x ` D∨ `:35 y := dioph.or_dioph x y" in dioph localized "notation `D∃`:30 := dioph.vec_ex1_dioph" in dioph localized "prefix `&`:max := 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 `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 `D.`:100 := dioph.const_dioph" in dioph variables {f g : (α → ℕ) → ℕ} (df : dioph_fn f) (dg : dioph_fn g) include df dg theorem 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⟩) theorem 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⟩) theorem 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 ` D= `:50 := dioph.eq_dioph" in dioph theorem 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 ` D+ `:80 := dioph.add_dioph" in dioph theorem 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 ` D* `:90 := dioph.mul_dioph" in dioph theorem 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 ` D≤ `:50 := dioph.le_dioph" in dioph theorem lt_dioph : dioph (λv, f v < g v) := df D+ (D. 1) D≤ dg localized "infix ` D< `:50 := dioph.lt_dioph" in dioph theorem ne_dioph : dioph (λv, f v ≠ g v) := ext (df D< dg D∨ dg D< df) $ λv, ne_iff_lt_or_gt.symm localized "infix ` D≠ `:50 := dioph.ne_dioph" in dioph theorem 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, nat.add_sub_cancel] }, { rw [x0, nat.sub_eq_zero_of_le yz] } end, λh, begin subst x, cases le_total y z with yz zy, { exact or.inr ⟨yz, nat.sub_eq_zero_of_le yz⟩ }, { exact or.inl (nat.sub_add_cancel zy).symm }, end⟩ localized "infix ` D- `:80 := dioph.sub_dioph" in dioph theorem 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 ` D∣ `:50 := dioph.dvd_dioph" in dioph theorem 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 ` D% `:80 := dioph.mod_dioph" in dioph theorem 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 `D≡` := dioph.modeq_dioph" in dioph theorem 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 ` D/ `:80 := dioph.div_dioph" in dioph omit df dg open pell theorem 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 theorem 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 := @reindex_dioph _ (fin2 4) _ pell_dioph [&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 := @reindex_dioph _ (fin2 9) _ pell_dioph [&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
54e3944efbe4f4510e5b88a80d5757175eaf7650
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/category/TopCommRing.lean
e0149c0f31aff5959cf6b3dcf03613da2b6b3745
[ "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
3,585
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.category.CommRing.basic import topology.category.Top.basic import topology.algebra.ring /-! # Category of topological commutative rings We introduce the category `TopCommRing` of topological commutative rings together with the relevant forgetful functors to topological spaces and commutative rings. -/ universes u open category_theory /-- A bundled topological commutative ring. -/ structure TopCommRing := (α : Type u) [is_comm_ring : comm_ring α] [is_topological_space : topological_space α] [is_topological_ring : topological_ring α] namespace TopCommRing instance : inhabited TopCommRing := ⟨⟨punit⟩⟩ instance : has_coe_to_sort TopCommRing (Type u) := ⟨TopCommRing.α⟩ attribute [instance] is_comm_ring is_topological_space is_topological_ring instance : category TopCommRing.{u} := { hom := λ R S, {f : R →+* S // continuous f }, id := λ R, ⟨ring_hom.id R, by obviously⟩, -- TODO remove obviously? comp := λ R S T f g, ⟨g.val.comp f.val, begin -- TODO automate cases f, cases g, dsimp, apply continuous.comp ; assumption end⟩ } instance : concrete_category TopCommRing.{u} := { forget := { obj := λ R, R, map := λ R S f, f.val }, forget_faithful := { } } /-- Construct a bundled `TopCommRing` from the underlying type and the appropriate typeclasses. -/ def of (X : Type u) [comm_ring X] [topological_space X] [topological_ring X] : TopCommRing := ⟨X⟩ @[simp] lemma coe_of (X : Type u) [comm_ring X] [topological_space X] [topological_ring X] : (of X : Type u) = X := rfl instance forget_topological_space (R : TopCommRing) : topological_space ((forget TopCommRing).obj R) := R.is_topological_space instance forget_comm_ring (R : TopCommRing) : comm_ring ((forget TopCommRing).obj R) := R.is_comm_ring instance forget_topological_ring (R : TopCommRing) : topological_ring ((forget TopCommRing).obj R) := R.is_topological_ring instance has_forget_to_CommRing : has_forget₂ TopCommRing CommRing := has_forget₂.mk' (λ R, CommRing.of R) (λ x, rfl) (λ R S f, f.val) (λ R S f, heq.rfl) instance forget_to_CommRing_topological_space (R : TopCommRing) : topological_space ((forget₂ TopCommRing CommRing).obj R) := R.is_topological_space /-- The forgetful functor to Top. -/ instance has_forget_to_Top : has_forget₂ TopCommRing Top := has_forget₂.mk' (λ R, Top.of R) (λ x, rfl) (λ R S f, ⟨⇑f.1, f.2⟩) (λ R S f, heq.rfl) instance forget_to_Top_comm_ring (R : TopCommRing) : comm_ring ((forget₂ TopCommRing Top).obj R) := R.is_comm_ring instance forget_to_Top_topological_ring (R : TopCommRing) : topological_ring ((forget₂ TopCommRing Top).obj R) := R.is_topological_ring /-- The forgetful functors to `Type` do not reflect isomorphisms, but the forgetful functor from `TopCommRing` to `Top` does. -/ instance : reflects_isomorphisms (forget₂ TopCommRing.{u} Top.{u}) := { reflects := λ X Y f _, begin resetI, -- We have an isomorphism in `Top`, let i_Top := as_iso ((forget₂ TopCommRing Top).map f), -- and a `ring_equiv`. let e_Ring : X ≃+* Y := { ..f.1, ..((forget Top).map_iso i_Top).to_equiv }, -- Putting these together we obtain the isomorphism we're after: exact ⟨⟨⟨e_Ring.symm, i_Top.inv.2⟩, ⟨by { ext x, exact e_Ring.left_inv x, }, by { ext x, exact e_Ring.right_inv x, }⟩⟩⟩ end } end TopCommRing
8310897cffc662dfbf03b28472fa2ed18e595e6f
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/lean/util.lean
38261a0c105fb5caf967c7830709fd609ff36d00
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
589
lean
/- Copyright (c) 2019 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich -/ prelude import init.lean.position init.io namespace Lean /-- Print and accumulate run time of `act` when Option `profiler` is set to `True`. -/ @[extern 5 "lean_lean_profileit"] constant profileit {α : Type} (category : @& String) (pos : @& Position) (act : IO α) : IO α := act def profileitPure {α : Type} (category : String) (pos : Position) (fn : Unit → α) : IO α := profileit category pos $ IO.lazyPure fn end Lean
4a0c4b5b89930e167c67973b320665a1c0b03e7d
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/combinatorics/hales_jewett.lean
9f8eed3974e9ab66053c924e98a1d8ea0264a17f
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,449
lean
/- Copyright (c) 2021 David Wärn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Wärn -/ import data.fintype.basic import algebra.big_operators.basic import data.equiv.fin /-! # The Hales-Jewett theorem We prove the Hales-Jewett theorem and deduce Van der Waerden's theorem as a corollary. The Hales-Jewett theorem is a result in Ramsey theory dealing with *combinatorial lines*. Given an 'alphabet' `α : Type*` and `a b : α`, an example of a combinatorial line in `α^5` is `{ (a, x, x, b, x) | x : α }`. See `combinatorics.line` for a precise general definition. The Hales-Jewett theorem states that for any fixed finite types `α` and `κ`, there exists a (potentially huge) finite type `ι` such that whenever `ι → α` is `κ`-colored (i.e. for any coloring `C : (ι → α) → κ`), there exists a monochromatic line. We prove the Hales-Jewett theorem using the idea of *color focusing* and a *product argument*. See the proof of `combinatorics.line.exists_mono_in_high_dimension'` for details. The version of Van der Waerden's theorem in this file states that whenever a commutative monoid `M` is finitely colored and `S` is a finite subset, there exists a monochromatic homothetic copy of `S`. This follows from the Hales-Jewett theorem by considering the map `(ι → S) → M` sending `v` to `∑ i : ι, v i`, which sends a combinatorial line to a homothetic copy of `S`. ## Main results - `combinatorics.line.exists_mono_in_high_dimension`: the Hales-Jewett theorem. - `combinatorics.exists_mono_homothetic_copy`: a generalization of Van der Waerden's theorem. ## Implementation details For convenience, we work directly with finite types instead of natural numbers. That is, we write `α, ι, κ` for (finite) types where one might traditionally use natural numbers `n, H, c`. This allows us to work directly with `α`, `option α`, `(ι → α) → κ`, and `ι ⊕ ι'` instead of `fin n`, `fin (n+1)`, `fin (c^(n^H))`, and `fin (H + H')`. ## Todo - Prove a finitary version of Van der Waerden's theorem (either by compactness or by modifying the current proof). - One could reformulate the proof of Hales-Jewett to give explicit upper bounds on the number of coordinates needed. ## Tags combinatorial line, Ramsey theory, arithmetic progession ### References * https://en.wikipedia.org/wiki/Hales%E2%80%93Jewett_theorem -/ open_locale classical open_locale big_operators universes u v namespace combinatorics /-- The type of combinatorial lines. A line `l : line α ι` in the hypercube `ι → α` defines a function `α → ι → α` from `α` to the hypercube, such that for each coordinate `i : ι`, the function `λ x, l x i` is either `id` or constant. We require lines to be nontrivial in the sense that `λ x, l x i` is `id` for at least one `i`. Formally, a line is represented by the function `l.idx_fun : ι → option α` which says whether `λ x, l x i` is `id` (corresponding to `l.idx_fun i = none`) or constantly `y` (corresponding to `l.idx_fun i = some y`). When `α` has size `1` there can be many elements of `line α ι` defining the same function. -/ structure line (α ι : Type*) := (idx_fun : ι → option α) (proper : ∃ i, idx_fun i = none) namespace line /- This lets us treat a line `l : line α ι` as a function `α → ι → α`. -/ instance (α ι) : has_coe_to_fun (line α ι) := ⟨λ _, α → ι → α, λ l x i, (l.idx_fun i).get_or_else x⟩ /-- A line is monochromatic if all its points are the same color. -/ def is_mono {α ι κ} (C : (ι → α) → κ) (l : line α ι) : Prop := ∃ c, ∀ x, C (l x) = c /-- The diagonal line. It is the identity at every coordinate. -/ def diagonal (α ι) [nonempty ι] : line α ι := { idx_fun := λ _, none, proper := ⟨classical.arbitrary ι, rfl⟩ } instance (α ι) [nonempty ι] : inhabited (line α ι) := ⟨diagonal α ι⟩ /-- The type of lines that are only one color except possibly at their endpoints. -/ structure almost_mono {α ι κ : Type*} (C : (ι → option α) → κ) := (line : line (option α) ι) (color : κ) (has_color : ∀ x : α, C (line (some x)) = color) instance {α ι κ : Type*} [nonempty ι] [inhabited κ] : inhabited (almost_mono (λ v : ι → option α, default κ)) := ⟨{ line := default _, color := default κ, has_color := λ _, rfl }⟩ /-- The type of collections of lines such that - each line is only one color except possibly at its endpoint - the lines all have the same endpoint - the colors of the lines are distinct. Used in the proof `exists_mono_in_high_dimension`. -/ structure color_focused {α ι κ : Type*} (C : (ι → option α) → κ) := (lines : multiset (almost_mono C)) (focus : ι → option α) (is_focused : ∀ p ∈ lines, almost_mono.line p none = focus) (distinct_colors : (lines.map almost_mono.color).nodup) instance {α ι κ} (C : (ι → option α) → κ) : inhabited (color_focused C) := ⟨⟨0, λ _, none, λ _, false.elim, multiset.nodup_zero⟩⟩ /-- A function `f : α → α'` determines a function `line α ι → line α' ι`. For a coordinate `i`, `l.map f` is the identity at `i` if `l` is, and constantly `f y` if `l` is constantly `y` at `i`. -/ def map {α α' ι} (f : α → α') (l : line α ι) : line α' ι := { idx_fun := λ i, (l.idx_fun i).map f, proper := ⟨l.proper.some, by rw [l.proper.some_spec, option.map_none'] ⟩ } /-- A point in `ι → α` and a line in `ι' → α` determine a line in `ι ⊕ ι' → α`. -/ def vertical {α ι ι'} (v : ι → α) (l : line α ι') : line α (ι ⊕ ι') := { idx_fun := sum.elim (some ∘ v) l.idx_fun, proper := ⟨sum.inr l.proper.some, l.proper.some_spec⟩ } /-- A line in `ι → α` and a point in `ι' → α` determine a line in `ι ⊕ ι' → α`. -/ def horizontal {α ι ι'} (l : line α ι) (v : ι' → α) : line α (ι ⊕ ι') := { idx_fun := sum.elim l.idx_fun (some ∘ v), proper := ⟨sum.inl l.proper.some, l.proper.some_spec⟩ } /-- One line in `ι → α` and one in `ι' → α` together determine a line in `ι ⊕ ι' → α`. -/ def prod {α ι ι'} (l : line α ι) (l' : line α ι') : line α (ι ⊕ ι') := { idx_fun := sum.elim l.idx_fun l'.idx_fun, proper := ⟨sum.inl l.proper.some, l.proper.some_spec⟩ } lemma apply {α ι} (l : line α ι) (x : α) : l x = λ i, (l.idx_fun i).get_or_else x := rfl lemma apply_none {α ι} (l : line α ι) (x : α) (i : ι) (h : l.idx_fun i = none) : l x i = x := by simp only [option.get_or_else_none, h, l.apply] lemma apply_of_ne_none {α ι} (l : line α ι) (x : α) (i : ι) (h : l.idx_fun i ≠ none) : some (l x i) = l.idx_fun i := by rw [l.apply, option.get_or_else_of_ne_none h] @[simp] lemma map_apply {α α' ι} (f : α → α') (l : line α ι) (x : α) : l.map f (f x) = f ∘ l x := by simp only [line.apply, line.map, option.get_or_else_map] @[simp] lemma vertical_apply {α ι ι'} (v : ι → α) (l : line α ι') (x : α) : l.vertical v x = sum.elim v (l x) := by { funext i, cases i; refl } @[simp] lemma horizontal_apply {α ι ι'} (l : line α ι) (v : ι' → α) (x : α) : l.horizontal v x = sum.elim (l x) v := by { funext i, cases i; refl } @[simp] lemma prod_apply {α ι ι'} (l : line α ι) (l' : line α ι') (x : α) : l.prod l' x = sum.elim (l x) (l' x) := by { funext i, cases i; refl } @[simp] lemma diagonal_apply {α ι} [nonempty ι] (x : α) : line.diagonal α ι x = λ i, x := by simp_rw [line.apply, line.diagonal, option.get_or_else_none] /-- The Hales-Jewett theorem. This version has a restriction on universe levels which is necessary for the proof. See `exists_mono_in_high_dimension` for a fully universe-polymorphic version. -/ private theorem exists_mono_in_high_dimension' : ∀ (α : Type u) [fintype α] (κ : Type (max v u)) [fintype κ], ∃ (ι : Type) (_ : fintype ι), ∀ C : (ι → α) → κ, ∃ l : line α ι, l.is_mono C := -- The proof proceeds by induction on `α`. fintype.induction_empty_option -- We have to show that the theorem is invariant under `α ≃ α'` for the induction to work. (λ α α' e, forall_imp $ λ κ, forall_imp $ λ _, Exists.imp $ λ ι, Exists.imp $ λ _ h C, let ⟨l, c, lc⟩ := h (λ v, C (e ∘ v)) in ⟨l.map e, c, e.forall_congr_left.mp $ λ x, by rw [←lc x, line.map_apply]⟩) begin -- This deals with the degenerate case where `α` is empty. introsI κ _, by_cases h : nonempty κ, { resetI, exact ⟨unit, infer_instance, λ C, ⟨default _, classical.arbitrary _, pempty.rec _⟩⟩, }, { exact ⟨empty, infer_instance, λ C, (h ⟨C (empty.rec _)⟩).elim⟩, } end begin -- Now we have to show that the theorem holds for `option α` if it holds for `α`. introsI α _ ihα κ _, -- Later we'll need `α` to be nonempty. So we first deal with the trivial case where `α` is empty. -- Then `option α` has only one element, so any line is monochromatic. by_cases h : nonempty α, work_on_goal 1 { refine ⟨unit, infer_instance, λ C, ⟨diagonal _ _, C (λ _, none), _⟩⟩, rintros (_ | ⟨a⟩), refl, exact (h ⟨a⟩).elim, }, -- The key idea is to show that for every `r`, in high dimension we can either find -- `r` color focused lines or a monochromatic line. suffices key : ∀ r : ℕ, ∃ (ι : Type) (_ : fintype ι), ∀ C : (ι → (option α)) → κ, (∃ s : color_focused C, s.lines.card = r) ∨ (∃ l, is_mono C l), -- Given the key claim, we simply take `r = |κ| + 1`. We cannot have this many distinct colors so -- we must be in the second case, where there is a monochromatic line. { obtain ⟨ι, _inst, hι⟩ := key (fintype.card κ + 1), refine ⟨ι, _inst, λ C, (hι C).resolve_left _⟩, rintro ⟨s, sr⟩, apply nat.not_succ_le_self (fintype.card κ), rw [←nat.add_one, ←sr, ←multiset.card_map, ←finset.card_mk], exact finset.card_le_univ ⟨_, s.distinct_colors⟩ }, -- We now prove the key claim, by induction on `r`. intro r, induction r with r ihr, -- The base case `r = 0` is trivial as the empty collection is color-focused. { exact ⟨empty, infer_instance, λ C, or.inl ⟨default _, multiset.card_zero⟩⟩, }, -- Supposing the key claim holds for `r`, we need to show it for `r+1`. First pick a high enough -- dimension `ι` for `r`. obtain ⟨ι, _inst, hι⟩ := ihr, resetI, -- Then since the theorem holds for `α` with any number of colors, pick a dimension `ι'` such that -- `ι' → α` always has a monochromatic line whenever it is `(ι → option α) → κ`-colored. specialize ihα ((ι → option α) → κ), obtain ⟨ι', _inst, hι'⟩ := ihα, resetI, -- We claim that `ι ⊕ ι'` works for `option α` and `κ`-coloring. refine ⟨ι ⊕ ι', infer_instance, _⟩, intro C, -- A `κ`-coloring of `ι ⊕ ι' → option α` induces an `(ι → option α) → κ`-coloring of `ι' → α`. specialize hι' (λ v' v, C (sum.elim v (some ∘ v'))), -- By choice of `ι'` this coloring has a monochromatic line `l'` with color class `C'`, where -- `C'` is a `κ`-coloring of `ι → α`. obtain ⟨l', C', hl'⟩ := hι', -- If `C'` has a monochromatic line, then so does `C`. We use this in two places below. have mono_of_mono : (∃ l, is_mono C' l) → (∃ l, is_mono C l), { rintro ⟨l, c, hl⟩, refine ⟨l.horizontal (some ∘ l' (classical.arbitrary α)), c, λ x, _⟩, rw [line.horizontal_apply, ←hl, ←hl'], }, -- By choice of `ι`, `C'` either has `r` color-focused lines or a monochromatic line. specialize hι C', rcases hι with ⟨s, sr⟩ | _, -- By above, we are done if `C'` has a monochromatic line. work_on_goal 1 { exact or.inr (mono_of_mono hι) }, -- Here we assume `C'` has `r` color focused lines. We split into cases depending on whether one of -- these `r` lines has the same color as the focus point. by_cases h : ∃ p ∈ s.lines, (p : almost_mono _).color = C' s.focus, -- If so then this is a `C'`-monochromatic line and we are done. { obtain ⟨p, p_mem, hp⟩ := h, refine or.inr (mono_of_mono ⟨p.line, p.color, _⟩), rintro (_ | _), rw [hp, s.is_focused p p_mem], apply p.has_color, }, -- If not, we get `r+1` color focused lines by taking the product of the `r` lines with `l'` and -- adding to this the vertical line obtained by the focus point and `l`. refine or.inl ⟨⟨(s.lines.map _).cons ⟨(l'.map some).vertical s.focus, C' s.focus, λ x, _⟩, sum.elim s.focus (l'.map some none), _, _⟩, _⟩, -- The vertical line is almost monochromatic. { rw [vertical_apply, ←congr_fun (hl' x), line.map_apply], }, { refine λ p, ⟨p.line.prod (l'.map some), p.color, λ x, _⟩, -- The product lines are almost monochromatic. rw [line.prod_apply, line.map_apply, ←p.has_color, ←congr_fun (hl' x)], }, -- Our `r+1` lines have the same endpoint. { simp_rw [multiset.mem_cons, multiset.mem_map], rintros _ (rfl | ⟨q, hq, rfl⟩), { rw line.vertical_apply, }, { rw [line.prod_apply, s.is_focused q hq], }, }, -- Our `r+1` lines have distinct colors (this is why we needed to split into cases above). { rw [multiset.map_cons, multiset.map_map, multiset.nodup_cons, multiset.mem_map], exact ⟨λ ⟨q, hq, he⟩, h ⟨q, hq, he⟩, s.distinct_colors⟩, }, -- Finally, we really do have `r+1` lines! { rw [multiset.card_cons, multiset.card_map, sr], }, end /-- The Hales-Jewett theorem: for any finite types `α` and `κ`, there exists a finite type `ι` such that whenever the hypercube `ι → α` is `κ`-colored, there is a monochromatic combinatorial line. -/ theorem exists_mono_in_high_dimension (α : Type u) [fintype α] (κ : Type v) [fintype κ] : ∃ (ι : Type) [fintype ι], ∀ C : (ι → α) → κ, ∃ l : line α ι, l.is_mono C := let ⟨ι, ιfin, hι⟩ := exists_mono_in_high_dimension' α (ulift κ) in ⟨ι, ιfin, λ C, let ⟨l, c, hc⟩ := hι (ulift.up ∘ C) in ⟨l, c.down, λ x, by rw ←hc⟩ ⟩ end line /-- A generalization of Van der Waerden's theorem: if `M` is a finitely colored commutative monoid, and `S` is a finite subset, then there exists a monochromatic homothetic copy of `S`. -/ theorem exists_mono_homothetic_copy {M κ} [add_comm_monoid M] (S : finset M) [fintype κ] (C : M → κ) : ∃ (a > 0) (b : M) (c : κ), ∀ s ∈ S, C (a • s + b) = c := begin obtain ⟨ι, _inst, hι⟩ := line.exists_mono_in_high_dimension S κ, resetI, specialize hι (λ v, C $ ∑ i, v i), obtain ⟨l, c, hl⟩ := hι, set s : finset ι := { i ∈ finset.univ | l.idx_fun i = none } with hs, refine ⟨s.card, finset.card_pos.mpr ⟨l.proper.some, _⟩, ∑ i in sᶜ, ((l.idx_fun i).map coe).get_or_else 0, c, _⟩, { rw [hs, finset.sep_def, finset.mem_filter], exact ⟨finset.mem_univ _, l.proper.some_spec⟩, }, intros x xs, rw ←hl ⟨x, xs⟩, clear hl, congr, rw ←finset.sum_add_sum_compl s, congr' 1, { rw ←finset.sum_const, apply finset.sum_congr rfl, intros i hi, rw [hs, finset.sep_def, finset.mem_filter] at hi, rw [l.apply_none _ _ hi.right, subtype.coe_mk], }, { apply finset.sum_congr rfl, intros i hi, rw [hs, finset.sep_def, finset.compl_filter, finset.mem_filter] at hi, obtain ⟨y, hy⟩ := option.ne_none_iff_exists.mp hi.right, simp_rw [line.apply, ←hy, option.map_some', option.get_or_else_some], }, end end combinatorics
0f4b50f31e6805d6f1e3584caf8803b5327a8113
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/src/Lean/Elab/Deriving/Hashable.lean
f49fa67726ef1a833648bf858b0d5b569e8af7b9
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
3,642
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Dany Fabian -/ import Lean.Meta.Inductive import Lean.Elab.Deriving.Basic import Lean.Elab.Deriving.Util namespace Lean.Elab.Deriving.Hashable open Command open Lean.Parser.Term open Meta def mkHashableHeader (ctx : Context) (indVal : InductiveVal) : TermElabM Header := do mkHeader ctx `Hashable 1 indVal def mkMatch (ctx : Context) (header : Header) (indVal : InductiveVal) (auxFuncIdx : Nat) : TermElabM Syntax := do let discrs ← mkDiscrs header indVal let alts ← mkAlts `(match $[$discrs],* with $alts:matchAlt*) where mkAlts : TermElabM (Array Syntax) := do let mut alts := #[] let mut ctorIdx := 0 let allIndVals := indVal.all.toArray for ctorName in indVal.ctors do let ctorInfo ← getConstInfoCtor ctorName let alt ← forallTelescopeReducing ctorInfo.type fun xs type => do let type ← Core.betaReduce type -- we 'beta-reduce' to eliminate "artificial" dependencies let mut patterns := #[] -- add `_` pattern for indices for i in [:indVal.numIndices] do patterns := patterns.push (← `(_)) let mut ctorArgs := #[] let mut rhs ← `($(quote ctorIdx)) -- add `_` for inductive parameters, they are inaccessible for i in [:indVal.numParams] do ctorArgs := ctorArgs.push (← `(_)) for i in [:ctorInfo.numFields] do let x := xs[indVal.numParams + i] let xTy ← inferType x let typeName := xTy.getAppFn.constName! let a := mkIdent (← mkFreshUserName `a) ctorArgs := ctorArgs.push a match allIndVals.findIdx? (· == typeName) with | some x => rhs ← `(mixHash $rhs ($(mkIdent ctx.auxFunNames[x]) $a:ident)) | none => rhs ← `(mixHash $rhs (hash $a:ident)) patterns := patterns.push (← `(@$(mkIdent ctorName):ident $ctorArgs:term*)) `(matchAltExpr| | $[$patterns:term],* => $rhs:term) alts := alts.push alt ctorIdx := ctorIdx + 1 return alts def mkAuxFunction (ctx : Context) (i : Nat) : TermElabM Syntax := do let auxFunName ← ctx.auxFunNames[i] let indVal ← ctx.typeInfos[i] let header ← mkHashableHeader ctx indVal let body ← mkMatch ctx header indVal i let binders := header.binders if ctx.usePartial then -- TODO(Dany): Get rid of this code branch altogether once we have well-founded recursion `(private partial def $(mkIdent auxFunName):ident $binders:explicitBinder* : USize := $body:term) else `(private def $(mkIdent auxFunName):ident $binders:explicitBinder* : USize := $body:term) def mkHashFuncs (ctx : Context) : TermElabM Syntax := do let mut auxDefs := #[] for i in [:ctx.typeInfos.size] do auxDefs := auxDefs.push (← mkAuxFunction ctx i) `(mutual $auxDefs:command* end) private def mkHashableInstanceCmds (declNames : Array Name) : TermElabM (Array Syntax) := do let ctx ← mkContext "hash" declNames[0] let cmds := #[← mkHashFuncs ctx] ++ (← mkInstanceCmds ctx `Hashable declNames) trace[Elab.Deriving.hashable] "\n{cmds}" return cmds def mkHashableHandler (declNames : Array Name) : CommandElabM Bool := do if (← declNames.allM isInductive) && declNames.size > 0 then let cmds ← liftTermElabM none <| mkHashableInstanceCmds declNames cmds.forM elabCommand return true else return false builtin_initialize registerBuiltinDerivingHandler ``Hashable mkHashableHandler registerTraceClass `Elab.Deriving.hashable
813d83f30de3f5461743f1f02a47c2af6b0c61ec
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/topology/algebra/group_with_zero.lean
34df919eafe84ed360bc2d86cd09fdf12657cfd8
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,604
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import topology.algebra.monoid import algebra.group.pi import algebra.group_with_zero.power import topology.homeomorph /-! # Topological group with zero In this file we define `has_continuous_inv'` to be a mixin typeclass a type with `has_inv` and `has_zero` (e.g., a `group_with_zero`) such that `λ x, x⁻¹` is continuous at all nonzero points. Any normed (semi)field has this property. Currently the only example of `has_continuous_inv'` in `mathlib` which is not a normed field is the type `nnnreal` (a.k.a. `ℝ≥0`) of nonnegative real numbers. Then we prove lemmas about continuity of `x ↦ x⁻¹` and `f / g` providing dot-style `*.inv'` and `*.div` operations on `filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`, and `continuous`. As a special case, we provide `*.div_const` operations that require only `group_with_zero` and `has_continuous_mul` instances. All lemmas about `(⁻¹)` use `inv'` in their names because lemmas without `'` are used for `topological_group`s. We also use `'` in the typeclass name `has_continuous_inv'` for the sake of consistency of notation. On a `group_with_zero` with continuous multiplication, we also define left and right multiplication as homeomorphisms. -/ open_locale topological_space open filter /-! ### A group with zero with continuous multiplication If `G₀` is a group with zero with continuous `(*)`, then `(/y)` is continuous for any `y`. In this section we prove lemmas that immediately follow from this fact providing `*.div_const` dot-style operations on `filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`, and `continuous`. -/ variables {α G₀ : Type*} section div_const variables [group_with_zero G₀] [topological_space G₀] [has_continuous_mul G₀] {f : α → G₀} {s : set α} {l : filter α} lemma filter.tendsto.div_const {x y : G₀} (hf : tendsto f l (𝓝 x)) : tendsto (λa, f a / y) l (𝓝 (x / y)) := by simpa only [div_eq_mul_inv] using hf.mul tendsto_const_nhds variables [topological_space α] lemma continuous_at.div_const (hf : continuous f) {y : G₀} : continuous (λ x, f x / y) := by simpa only [div_eq_mul_inv] using hf.mul continuous_const lemma continuous_within_at.div_const {a} (hf : continuous_within_at f s a) {y : G₀} : continuous_within_at (λ x, f x / y) s a := hf.div_const lemma continuous_on.div_const (hf : continuous_on f s) {y : G₀} : continuous_on (λ x, f x / y) s := by simpa only [div_eq_mul_inv] using hf.mul continuous_on_const @[continuity] lemma continuous.div_const (hf : continuous f) {y : G₀} : continuous (λ x, f x / y) := by simpa only [div_eq_mul_inv] using hf.mul continuous_const end div_const /-- A type with `0` and `has_inv` such that `λ x, x⁻¹` is continuous at all nonzero points. Any normed (semi)field has this property. -/ class has_continuous_inv' (G₀ : Type*) [has_zero G₀] [has_inv G₀] [topological_space G₀] := (continuous_at_inv' : ∀ ⦃x : G₀⦄, x ≠ 0 → continuous_at has_inv.inv x) export has_continuous_inv' (continuous_at_inv') section inv' variables [has_zero G₀] [has_inv G₀] [topological_space G₀] [has_continuous_inv' G₀] {l : filter α} {f : α → G₀} {s : set α} {a : α} /-! ### Continuity of `λ x, x⁻¹` at a non-zero point We define `topological_group_with_zero` to be a `group_with_zero` such that the operation `x ↦ x⁻¹` is continuous at all nonzero points. In this section we prove dot-style `*.inv'` lemmas for `filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`, and `continuous`. -/ lemma tendsto_inv' {x : G₀} (hx : x ≠ 0) : tendsto has_inv.inv (𝓝 x) (𝓝 x⁻¹) := continuous_at_inv' hx lemma continuous_on_inv' : continuous_on (has_inv.inv : G₀ → G₀) {0}ᶜ := λ x hx, (continuous_at_inv' hx).continuous_within_at /-- If a function converges to a nonzero value, its inverse converges to the inverse of this value. We use the name `tendsto.inv'` as `tendsto.inv` is already used in multiplicative topological groups. -/ lemma filter.tendsto.inv' {a : G₀} (hf : tendsto f l (𝓝 a)) (ha : a ≠ 0) : tendsto (λ x, (f x)⁻¹) l (𝓝 a⁻¹) := (tendsto_inv' ha).comp hf variables [topological_space α] lemma continuous_within_at.inv' (hf : continuous_within_at f s a) (ha : f a ≠ 0) : continuous_within_at (λ x, (f x)⁻¹) s a := hf.inv' ha lemma continuous_at.inv' (hf : continuous_at f a) (ha : f a ≠ 0) : continuous_at (λ x, (f x)⁻¹) a := hf.inv' ha @[continuity] lemma continuous.inv' (hf : continuous f) (h0 : ∀ x, f x ≠ 0) : continuous (λ x, (f x)⁻¹) := continuous_iff_continuous_at.2 $ λ x, (hf.tendsto x).inv' (h0 x) lemma continuous_on.inv' (hf : continuous_on f s) (h0 : ∀ x ∈ s, f x ≠ 0) : continuous_on (λ x, (f x)⁻¹) s := λ x hx, (hf x hx).inv' (h0 x hx) end inv' /-! ### Continuity of division If `G₀` is a `group_with_zero` with `x ↦ x⁻¹` continuous at all nonzero points and `(*)`, then division `(/)` is continuous at any point where the denominator is continuous. -/ section div variables [group_with_zero G₀] [topological_space G₀] [has_continuous_inv' G₀] [has_continuous_mul G₀] {f g : α → G₀} lemma filter.tendsto.div {l : filter α} {a b : G₀} (hf : tendsto f l (𝓝 a)) (hg : tendsto g l (𝓝 b)) (hy : b ≠ 0) : tendsto (f / g) l (𝓝 (a / b)) := by simpa only [div_eq_mul_inv] using hf.mul (hg.inv' hy) variables [topological_space α] {s : set α} {a : α} lemma continuous_within_at.div (hf : continuous_within_at f s a) (hg : continuous_within_at g s a) (h₀ : g a ≠ 0) : continuous_within_at (f / g) s a := hf.div hg h₀ lemma continuous_on.div (hf : continuous_on f s) (hg : continuous_on g s) (h₀ : ∀ x ∈ s, g x ≠ 0) : continuous_on (f / g) s := λ x hx, (hf x hx).div (hg x hx) (h₀ x hx) /-- Continuity at a point of the result of dividing two functions continuous at that point, where the denominator is nonzero. -/ lemma continuous_at.div (hf : continuous_at f a) (hg : continuous_at g a) (h₀ : g a ≠ 0) : continuous_at (f / g) a := hf.div hg h₀ @[continuity] lemma continuous.div (hf : continuous f) (hg : continuous g) (h₀ : ∀ x, g x ≠ 0) : continuous (f / g) := by simpa only [div_eq_mul_inv] using hf.mul (hg.inv' h₀) lemma continuous_on_div : continuous_on (λ p : G₀ × G₀, p.1 / p.2) {p | p.2 ≠ 0} := continuous_on_fst.div continuous_on_snd $ λ _, id end div /-! ### Left and right multiplication as homeomorphisms -/ namespace homeomorph variables [topological_space α] [group_with_zero α] [has_continuous_mul α] /-- Left multiplication by a nonzero element in a `group_with_zero` with continuous multiplication is a homeomorphism of the underlying type. -/ protected def mul_left' (c : α) (hc : c ≠ 0) : α ≃ₜ α := { continuous_to_fun := continuous_mul_left _, continuous_inv_fun := continuous_mul_left _, .. equiv.mul_left' c hc } /-- Right multiplication by a nonzero element in a `group_with_zero` with continuous multiplication is a homeomorphism of the underlying type. -/ protected def mul_right' (c : α) (hc : c ≠ 0) : α ≃ₜ α := { continuous_to_fun := continuous_mul_right _, continuous_inv_fun := continuous_mul_right _, .. equiv.mul_right' c hc } @[simp] lemma coe_mul_left' (c : α) (hc : c ≠ 0) : ⇑(homeomorph.mul_left' c hc) = (*) c := rfl @[simp] lemma mul_left'_symm_apply (c : α) (hc : c ≠ 0) : ((homeomorph.mul_left' c hc).symm : α → α) = (*) c⁻¹ := rfl @[simp] lemma coe_mul_right' (c : α) (hc : c ≠ 0) : ⇑(homeomorph.mul_right' c hc) = λ x, x * c := rfl @[simp] lemma mul_right'_symm_apply (c : α) (hc : c ≠ 0) : ((homeomorph.mul_right' c hc).symm : α → α) = λ x, x * c⁻¹ := rfl end homeomorph section fpow variables [group_with_zero G₀] [topological_space G₀] [has_continuous_inv' G₀] [has_continuous_mul G₀] lemma tendsto_fpow {x : G₀} (hx : x ≠ 0) (m : ℤ) : tendsto (λ x, x ^ m) (𝓝 x) (𝓝 (x ^ m)) := begin have : ∀ y : G₀, ∀ m : ℤ, 0 < m → tendsto (λ x, x ^ m) (𝓝 y) (𝓝 (y ^ m)), { assume y m hm, lift m to ℕ using (le_of_lt hm) with k, simp only [fpow_coe_nat], exact (continuous_pow k).continuous_at.tendsto }, rcases lt_trichotomy m 0 with hm | hm | hm, { have hm' : 0 < - m := by rwa neg_pos, convert (this _ (-m) hm').comp (tendsto_inv' hx) using 1, { ext y, simp }, { congr' 1, simp } }, { simpa [hm] using tendsto_const_nhds }, { exact this _ m hm } end lemma continuous_at_fpow {x : G₀} (hx : x ≠ 0) (m : ℤ) : continuous_at (λ x, x ^ m) x := tendsto_fpow hx m lemma continuous_on_fpow (m : ℤ) : continuous_on (λ x : G₀, x ^ m) {0}ᶜ := λ x hx, (continuous_at_fpow hx m).continuous_within_at variables {f : α → G₀} lemma filter.tendsto.fpow {l : filter α} {a : G₀} (hf : tendsto f l (𝓝 a)) (ha : a ≠ 0) (m : ℤ) : tendsto (λ x, (f x) ^ m) l (𝓝 (a ^ m)) := (tendsto_fpow ha m).comp hf variables [topological_space α] {a : α} lemma continuous_at.fpow (hf : continuous_at f a) (ha : f a ≠ 0) (m : ℤ) : continuous_at (λ x, (f x) ^ m) a := (continuous_at_fpow ha m).comp hf @[continuity] lemma continuous.fpow (hf : continuous f) (h0 : ∀ a, f a ≠ 0) (m : ℤ) : continuous (λ x, (f x) ^ m) := continuous_iff_continuous_at.2 $ λ x, (hf.tendsto x).fpow (h0 x) m end fpow
2c4b499e275404bebf7c7249f93784ffc58c3fb3
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/nat/multiplicity.lean
e6eb975f9220136f41326160fee468f0f5fc2efa
[ "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
11,064
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.big_operators.intervals import algebra.geom_sum import data.nat.bitwise import data.nat.log import data.nat.parity import ring_theory.int.basic /-! # Natural number multiplicity This file contains lemmas about the multiplicity function (the maximum prime power dividing a number) when applied to naturals, in particular calculating it for factorials and binomial coefficients. ## Multiplicity calculations * `nat.multiplicity_factorial`: Legendre's Theorem. The multiplicity of `p` in `n!` is `n/p + ... + n/p^b` for any `b` such that `n/p^(b + 1) = 0`. * `nat.multiplicity_factorial_mul`: The multiplicity of `p` in `(p * n)!` is `n` more than that of `n!`. * `nat.multiplicity_choose`: The multiplicity of `p` in `n.choose k` is the number of carries when `k` and`n - k` are added in base `p`. ## Other declarations * `nat.multiplicity_eq_card_pow_dvd`: The multiplicity of `m` in `n` is the number of positive natural numbers `i` such that `m ^ i` divides `n`. * `nat.multiplicity_two_factorial_lt`: The multiplicity of `2` in `n!` is strictly less than `n`. * `nat.prime.multiplicity_something`: Specialization of `multiplicity.something` to a prime in the naturals. Avoids having to provide `p ≠ 1` and other trivialities, along with translating between `prime` and `nat.prime`. ## Tags Legendre, p-adic -/ open finset nat multiplicity open_locale big_operators nat namespace nat /-- The multiplicity of `m` in `n` is the number of positive natural numbers `i` such that `m ^ i` divides `n`. This set is expressed by filtering `Ico 1 b` where `b` is any bound greater than `log m n`. -/ lemma multiplicity_eq_card_pow_dvd {m n b : ℕ} (hm : m ≠ 1) (hn : 0 < n) (hb : log m n < b): multiplicity m n = ↑((finset.Ico 1 b).filter (λ i, m ^ i ∣ n)).card := calc multiplicity m n = ↑(Ico 1 $ ((multiplicity m n).get (finite_nat_iff.2 ⟨hm, hn⟩) + 1)).card : by simp ... = ↑((finset.Ico 1 b).filter (λ i, m ^ i ∣ n)).card : congr_arg coe $ congr_arg card $ finset.ext $ λ i, begin rw [mem_filter, mem_Ico, mem_Ico, lt_succ_iff, ←@part_enat.coe_le_coe i, part_enat.coe_get, ←pow_dvd_iff_le_multiplicity, and.right_comm], refine (and_iff_left_of_imp (λ h, _)).symm, cases m, { rw [zero_pow, zero_dvd_iff] at h, exact (hn.ne' h.2).elim, { exact h.1 } }, exact ((pow_le_iff_le_log (succ_lt_succ $ nat.pos_of_ne_zero $ succ_ne_succ.1 hm) hn).1 $ le_of_dvd hn h.2).trans_lt hb, end namespace prime lemma multiplicity_one {p : ℕ} (hp : p.prime) : multiplicity p 1 = 0 := multiplicity.one_right (prime_iff.mp hp).not_unit lemma multiplicity_mul {p m n : ℕ} (hp : p.prime) : multiplicity p (m * n) = multiplicity p m + multiplicity p n := multiplicity.mul $ prime_iff.mp hp lemma multiplicity_pow {p m n : ℕ} (hp : p.prime) : multiplicity p (m ^ n) = n • (multiplicity p m) := multiplicity.pow $ prime_iff.mp hp lemma multiplicity_self {p : ℕ} (hp : p.prime) : multiplicity p p = 1 := multiplicity_self (prime_iff.mp hp).not_unit hp.ne_zero lemma multiplicity_pow_self {p n : ℕ} (hp : p.prime) : multiplicity p (p ^ n) = n := multiplicity_pow_self hp.ne_zero (prime_iff.mp hp).not_unit n /-- **Legendre's Theorem** The multiplicity of a prime in `n!` is the sum of the quotients `n / p ^ i`. This sum is expressed over the finset `Ico 1 b` where `b` is any bound greater than `log p n`. -/ lemma multiplicity_factorial {p : ℕ} (hp : p.prime) : ∀ {n b : ℕ}, log p n < b → multiplicity p n! = (∑ i in Ico 1 b, n / p ^ i : ℕ) | 0 b hb := by simp [Ico, hp.multiplicity_one] | (n+1) b hb := calc multiplicity p (n+1)! = multiplicity p n! + multiplicity p (n+1) : by rw [factorial_succ, hp.multiplicity_mul, add_comm] ... = (∑ i in Ico 1 b, n / p ^ i : ℕ) + ((finset.Ico 1 b).filter (λ i, p ^ i ∣ n+1)).card : by rw [multiplicity_factorial ((log_mono_right $ le_succ _).trans_lt hb), ← multiplicity_eq_card_pow_dvd hp.ne_one (succ_pos _) hb] ... = (∑ i in Ico 1 b, (n / p ^ i + if p^i ∣ n+1 then 1 else 0) : ℕ) : by { rw [sum_add_distrib, sum_boole], simp } ... = (∑ i in Ico 1 b, (n + 1) / p ^ i : ℕ) : congr_arg coe $ finset.sum_congr rfl $ λ _ _, (succ_div _ _).symm /-- The multiplicity of `p` in `(p * (n + 1))!` is one more than the sum of the multiplicities of `p` in `(p * n)!` and `n + 1`. -/ lemma multiplicity_factorial_mul_succ {n p : ℕ} (hp : p.prime) : multiplicity p (p * (n + 1))! = multiplicity p (p * n)! + multiplicity p (n + 1) + 1 := begin have hp' := prime_iff.mp hp, have h0 : 2 ≤ p := hp.two_le, have h1 : 1 ≤ p * n + 1 := nat.le_add_left _ _, have h2 : p * n + 1 ≤ p * (n + 1), linarith, have h3 : p * n + 1 ≤ p * (n + 1) + 1, linarith, have hm : multiplicity p (p * n)! ≠ ⊤, { rw [ne.def, eq_top_iff_not_finite, not_not, finite_nat_iff], exact ⟨hp.ne_one, factorial_pos _⟩ }, revert hm, have h4 : ∀ m ∈ Ico (p * n + 1) (p * (n + 1)), multiplicity p m = 0, { intros m hm, apply multiplicity_eq_zero_of_not_dvd, rw [← not_dvd_iff_between_consec_multiples _ (pos_iff_ne_zero.mpr hp.ne_zero)], rw [mem_Ico] at hm, exact ⟨n, lt_of_succ_le hm.1, hm.2⟩ }, simp_rw [← prod_Ico_id_eq_factorial, multiplicity.finset.prod hp', ← sum_Ico_consecutive _ h1 h3, add_assoc], intro h, rw [part_enat.add_left_cancel_iff h, sum_Ico_succ_top h2, multiplicity.mul hp', hp.multiplicity_self, sum_congr rfl h4, sum_const_zero, zero_add, add_comm (1 : part_enat)] end /-- The multiplicity of `p` in `(p * n)!` is `n` more than that of `n!`. -/ lemma multiplicity_factorial_mul {n p : ℕ} (hp : p.prime) : multiplicity p (p * n)! = multiplicity p n! + n := begin induction n with n ih, { simp }, { simp only [succ_eq_add_one, multiplicity.mul, hp, prime_iff.mp hp, ih, multiplicity_factorial_mul_succ, ←add_assoc, nat.cast_one, nat.cast_add, factorial_succ], congr' 1, rw [add_comm, add_assoc] } end /-- A prime power divides `n!` iff it is at most the sum of the quotients `n / p ^ i`. This sum is expressed over the set `Ico 1 b` where `b` is any bound greater than `log p n` -/ lemma pow_dvd_factorial_iff {p : ℕ} {n r b : ℕ} (hp : p.prime) (hbn : log p n < b) : p ^ r ∣ n! ↔ r ≤ ∑ i in Ico 1 b, n / p ^ i := by rw [← part_enat.coe_le_coe, ← hp.multiplicity_factorial hbn, ← pow_dvd_iff_le_multiplicity] lemma multiplicity_factorial_le_div_pred {p : ℕ} (hp : p.prime) (n : ℕ) : multiplicity p n! ≤ (n/(p - 1) : ℕ) := begin rw [hp.multiplicity_factorial (lt_succ_self _), part_enat.coe_le_coe], exact nat.geom_sum_Ico_le hp.two_le _ _, end lemma multiplicity_choose_aux {p n b k : ℕ} (hp : p.prime) (hkn : k ≤ n) : ∑ i in finset.Ico 1 b, n / p ^ i = ∑ i in finset.Ico 1 b, k / p ^ i + ∑ i in finset.Ico 1 b, (n - k) / p ^ i + ((finset.Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card := calc ∑ i in finset.Ico 1 b, n / p ^ i = ∑ i in finset.Ico 1 b, (k + (n - k)) / p ^ i : by simp only [add_tsub_cancel_of_le hkn] ... = ∑ i in finset.Ico 1 b, (k / p ^ i + (n - k) / p ^ i + if p ^ i ≤ k % p ^ i + (n - k) % p ^ i then 1 else 0) : by simp only [nat.add_div (pow_pos hp.pos _)] ... = _ : by simp [sum_add_distrib, sum_boole] /-- The multiplicity of `p` in `choose n k` is the number of carries when `k` and `n - k` are added in base `p`. The set is expressed by filtering `Ico 1 b` where `b` is any bound greater than `log p n`. -/ lemma multiplicity_choose {p n k b : ℕ} (hp : p.prime) (hkn : k ≤ n) (hnb : log p n < b) : multiplicity p (choose n k) = ((Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card := have h₁ : multiplicity p (choose n k) + multiplicity p (k! * (n - k)!) = ((finset.Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card + multiplicity p (k! * (n - k)!), begin rw [← hp.multiplicity_mul, ← mul_assoc, choose_mul_factorial_mul_factorial hkn, hp.multiplicity_factorial hnb, hp.multiplicity_mul, hp.multiplicity_factorial ((log_mono_right hkn).trans_lt hnb), hp.multiplicity_factorial (lt_of_le_of_lt (log_mono_right tsub_le_self) hnb), multiplicity_choose_aux hp hkn], simp [add_comm], end, (part_enat.add_right_cancel_iff (part_enat.ne_top_iff_dom.2 $ by exact finite_nat_iff.2 ⟨ne_of_gt hp.one_lt, mul_pos (factorial_pos k) (factorial_pos (n - k))⟩)).1 h₁ /-- A lower bound on the multiplicity of `p` in `choose n k`. -/ lemma multiplicity_le_multiplicity_choose_add {p : ℕ} (hp : p.prime) : ∀ (n k : ℕ), multiplicity p n ≤ multiplicity p (choose n k) + multiplicity p k | _ 0 := by simp | 0 (_+1) := by simp | (n+1) (k+1) := begin rw ← hp.multiplicity_mul, refine multiplicity_le_multiplicity_of_dvd_right _, rw [← succ_mul_choose_eq], exact dvd_mul_right _ _ end lemma multiplicity_choose_prime_pow {p n k : ℕ} (hp : p.prime) (hkn : k ≤ p ^ n) (hk0 : 0 < k) : multiplicity p (choose (p ^ n) k) + multiplicity p k = n := le_antisymm (have hdisj : disjoint ((Ico 1 n.succ).filter (λ i, p ^ i ≤ k % p ^ i + (p ^ n - k) % p ^ i)) ((Ico 1 n.succ).filter (λ i, p ^ i ∣ k)), by simp [disjoint_right, *, dvd_iff_mod_eq_zero, nat.mod_lt _ (pow_pos hp.pos _)] {contextual := tt}, begin rw [multiplicity_choose hp hkn (lt_succ_self _), multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) hk0 (lt_succ_of_le (log_mono_right hkn)), ← nat.cast_add, part_enat.coe_le_coe, log_pow hp.one_lt, ← card_disjoint_union hdisj, filter_union_right], have filter_le_Ico := (Ico 1 n.succ).card_filter_le _, rwa card_Ico 1 n.succ at filter_le_Ico, end) (by rw [← hp.multiplicity_pow_self]; exact multiplicity_le_multiplicity_choose_add hp _ _) end prime lemma multiplicity_two_factorial_lt : ∀ {n : ℕ} (h : n ≠ 0), multiplicity 2 n! < n := begin have h2 := prime_iff.mp prime_two, refine binary_rec _ _, { contradiction }, { intros b n ih h, by_cases hn : n = 0, { subst hn, simp at h, simp [h, one_right h2.not_unit, part_enat.zero_lt_one] }, have : multiplicity 2 (2 * n)! < (2 * n : ℕ), { rw [prime_two.multiplicity_factorial_mul], refine (part_enat.add_lt_add_right (ih hn) (part_enat.coe_ne_top _)).trans_le _, rw [two_mul], norm_cast }, cases b, { simpa [bit0_eq_two_mul n] }, { suffices : multiplicity 2 (2 * n + 1) + multiplicity 2 (2 * n)! < ↑(2 * n) + 1, { simpa [succ_eq_add_one, multiplicity.mul, h2, prime_two, nat.bit1_eq_succ_bit0, bit0_eq_two_mul n] }, rw [multiplicity_eq_zero_of_not_dvd (two_not_dvd_two_mul_add_one n), zero_add], refine this.trans _, exact_mod_cast lt_succ_self _ }} end end nat
15923225ef27a289712b01ad139e5888217ddc20
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/qpf/multivariate/constructions/fix.lean
2c1c33ceed19ec1b8cd06ce661a08a6f0f4516a8
[ "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
12,997
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Simon Hudon -/ import data.pfunctor.multivariate.W import data.qpf.multivariate.basic /-! # The initial algebra of a multivariate qpf is again a qpf. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. For a `(n+1)`-ary QPF `F (α₀,..,αₙ)`, we take the least fixed point of `F` with regards to its last argument `αₙ`. The result is a `n`-ary functor: `fix F (α₀,..,αₙ₋₁)`. Making `fix F` into a functor allows us to take the fixed point, compose with other functors and take a fixed point again. ## Main definitions * `fix.mk` - constructor * `fix.dest - destructor * `fix.rec` - recursor: basis for defining functions by structural recursion on `fix F α` * `fix.drec` - dependent recursor: generalization of `fix.rec` where the result type of the function is allowed to depend on the `fix F α` value * `fix.rec_eq` - defining equation for `recursor` * `fix.ind` - induction principle for `fix F α` ## Implementation notes For `F` a QPF`, we define `fix F α` in terms of the W-type of the polynomial functor `P` of `F`. We define the relation `Wequiv` and take its quotient as the definition of `fix F α`. ```lean inductive Wequiv {α : typevec n} : q.P.W α → q.P.W α → Prop | ind (a : q.P.A) (f' : q.P.drop.B a ⟹ α) (f₀ f₁ : q.P.last.B a → q.P.W α) : (∀ x, Wequiv (f₀ x) (f₁ x)) → Wequiv (q.P.W_mk a f' f₀) (q.P.W_mk a f' f₁) | abs (a₀ : q.P.A) (f'₀ : q.P.drop.B a₀ ⟹ α) (f₀ : q.P.last.B a₀ → q.P.W α) (a₁ : q.P.A) (f'₁ : q.P.drop.B a₁ ⟹ α) (f₁ : q.P.last.B a₁ → q.P.W α) : abs ⟨a₀, q.P.append_contents f'₀ f₀⟩ = abs ⟨a₁, q.P.append_contents f'₁ f₁⟩ → Wequiv (q.P.W_mk a₀ f'₀ f₀) (q.P.W_mk a₁ f'₁ f₁) | trans (u v w : q.P.W α) : Wequiv u v → Wequiv v w → Wequiv u w ``` See [avigad-carneiro-hudon2019] for more details. ## Reference * Jeremy Avigad, Mario M. Carneiro and Simon Hudon. [*Data Types as Quotients of Polynomial Functors*][avigad-carneiro-hudon2019] -/ universes u v namespace mvqpf open typevec open mvfunctor (liftp liftr) open_locale mvfunctor variables {n : ℕ} {F : typevec.{u} (n+1) → Type u} [mvfunctor F] [q : mvqpf F] include q /-- `recF` is used as a basis for defining the recursor on `fix F α`. `recF` traverses recursively the W-type generated by `q.P` using a function on `F` as a recursive step -/ def recF {α : typevec n} {β : Type*} (g : F (α.append1 β) → β) : q.P.W α → β := q.P.W_rec (λ a f' f rec, g (abs ⟨a, split_fun f' rec⟩)) theorem recF_eq {α : typevec n} {β : Type*} (g : F (α.append1 β) → β) (a : q.P.A) (f' : q.P.drop.B a ⟹ α) (f : q.P.last.B a → q.P.W α) : recF g (q.P.W_mk a f' f) = g (abs ⟨a, split_fun f' (recF g ∘ f)⟩) := by rw [recF, mvpfunctor.W_rec_eq]; refl theorem recF_eq' {α : typevec n} {β : Type*} (g : F (α.append1 β) → β) (x : q.P.W α) : recF g x = g (abs ((append_fun id (recF g)) <$$> q.P.W_dest' x)) := begin apply q.P.W_cases _ x, intros a f' f, rw [recF_eq, q.P.W_dest'_W_mk, mvpfunctor.map_eq, append_fun_comp_split_fun, typevec.id_comp] end /-- Equivalence relation on W-types that represent the same `fix F` value -/ inductive Wequiv {α : typevec n} : q.P.W α → q.P.W α → Prop | ind (a : q.P.A) (f' : q.P.drop.B a ⟹ α) (f₀ f₁ : q.P.last.B a → q.P.W α) : (∀ x, Wequiv (f₀ x) (f₁ x)) → Wequiv (q.P.W_mk a f' f₀) (q.P.W_mk a f' f₁) | abs (a₀ : q.P.A) (f'₀ : q.P.drop.B a₀ ⟹ α) (f₀ : q.P.last.B a₀ → q.P.W α) (a₁ : q.P.A) (f'₁ : q.P.drop.B a₁ ⟹ α) (f₁ : q.P.last.B a₁ → q.P.W α) : abs ⟨a₀, q.P.append_contents f'₀ f₀⟩ = abs ⟨a₁, q.P.append_contents f'₁ f₁⟩ → Wequiv (q.P.W_mk a₀ f'₀ f₀) (q.P.W_mk a₁ f'₁ f₁) | trans (u v w : q.P.W α) : Wequiv u v → Wequiv v w → Wequiv u w theorem recF_eq_of_Wequiv (α : typevec n) {β : Type*} (u : F (α.append1 β) → β) (x y : q.P.W α) : Wequiv x y → recF u x = recF u y := begin apply q.P.W_cases _ x, intros a₀ f'₀ f₀, apply q.P.W_cases _ y, intros a₁ f'₁ f₁, intro h, induction h, case mvqpf.Wequiv.ind : a f' f₀ f₁ h ih { simp only [recF_eq, function.comp, ih] }, case mvqpf.Wequiv.abs : a₀ f'₀ f₀ a₁ f'₁ f₁ h { simp only [recF_eq', abs_map, mvpfunctor.W_dest'_W_mk, h] }, case mvqpf.Wequiv.trans : x y z e₁ e₂ ih₁ ih₂ { exact eq.trans ih₁ ih₂ } end theorem Wequiv.abs' {α : typevec n} (x y : q.P.W α) (h : abs (q.P.W_dest' x) = abs (q.P.W_dest' y)) : Wequiv x y := begin revert h, apply q.P.W_cases _ x, intros a₀ f'₀ f₀, apply q.P.W_cases _ y, intros a₁ f'₁ f₁, apply Wequiv.abs end theorem Wequiv.refl {α : typevec n} (x : q.P.W α) : Wequiv x x := by apply q.P.W_cases _ x; intros a f' f; exact Wequiv.abs a f' f a f' f rfl theorem Wequiv.symm {α : typevec n} (x y : q.P.W α) : Wequiv x y → Wequiv y x := begin intro h, induction h, case mvqpf.Wequiv.ind : a f' f₀ f₁ h ih { exact Wequiv.ind _ _ _ _ ih }, case mvqpf.Wequiv.abs : a₀ f'₀ f₀ a₁ f'₁ f₁ h { exact Wequiv.abs _ _ _ _ _ _ h.symm }, case mvqpf.Wequiv.trans : x y z e₁ e₂ ih₁ ih₂ { exact mvqpf.Wequiv.trans _ _ _ ih₂ ih₁} end /-- maps every element of the W type to a canonical representative -/ def Wrepr {α : typevec n} : q.P.W α → q.P.W α := recF (q.P.W_mk' ∘ repr) theorem Wrepr_W_mk {α : typevec n} (a : q.P.A) (f' : q.P.drop.B a ⟹ α) (f : q.P.last.B a → q.P.W α) : Wrepr (q.P.W_mk a f' f) = q.P.W_mk' (repr (abs ((append_fun id Wrepr) <$$> ⟨a, q.P.append_contents f' f⟩))) := by rw [Wrepr, recF_eq', q.P.W_dest'_W_mk]; refl theorem Wrepr_equiv {α : typevec n} (x : q.P.W α) : Wequiv (Wrepr x) x := begin apply q.P.W_ind _ x, intros a f' f ih, apply Wequiv.trans _ (q.P.W_mk' ((append_fun id Wrepr) <$$> ⟨a, q.P.append_contents f' f⟩)), { apply Wequiv.abs', rw [Wrepr_W_mk, q.P.W_dest'_W_mk', q.P.W_dest'_W_mk', abs_repr] }, rw [q.P.map_eq, mvpfunctor.W_mk', append_fun_comp_split_fun, id_comp], apply Wequiv.ind, exact ih end theorem Wequiv_map {α β : typevec n} (g : α ⟹ β) (x y : q.P.W α) : Wequiv x y → Wequiv (g <$$> x) (g <$$> y) := begin intro h, induction h, case mvqpf.Wequiv.ind : a f' f₀ f₁ h ih { rw [q.P.W_map_W_mk, q.P.W_map_W_mk], apply Wequiv.ind, apply ih }, case mvqpf.Wequiv.abs : a₀ f'₀ f₀ a₁ f'₁ f₁ h { rw [q.P.W_map_W_mk, q.P.W_map_W_mk], apply Wequiv.abs, show abs (q.P.obj_append1 a₀ (g ⊚ f'₀) (λ x, q.P.W_map g (f₀ x))) = abs (q.P.obj_append1 a₁ (g ⊚ f'₁) (λ x, q.P.W_map g (f₁ x))), rw [←q.P.map_obj_append1, ←q.P.map_obj_append1, abs_map, abs_map, h] }, case mvqpf.Wequiv.trans : x y z e₁ e₂ ih₁ ih₂ { apply mvqpf.Wequiv.trans, apply ih₁, apply ih₂ } end /-- Define the fixed point as the quotient of trees under the equivalence relation. -/ def W_setoid (α : typevec n) : setoid (q.P.W α) := ⟨Wequiv, @Wequiv.refl _ _ _ _ _, @Wequiv.symm _ _ _ _ _, @Wequiv.trans _ _ _ _ _⟩ local attribute [instance] W_setoid /-- Least fixed point of functor F. The result is a functor with one fewer parameters than the input. For `F a b c` a ternary functor, fix F is a binary functor such that ```lean fix F a b = F a b (fix F a b) ``` -/ def fix {n : ℕ} (F : typevec (n+1) → Type*) [mvfunctor F] [q : mvqpf F] (α : typevec n) := quotient (W_setoid α : setoid (q.P.W α)) attribute [nolint has_nonempty_instance] fix /-- `fix F` is a functor -/ def fix.map {α β : typevec n} (g : α ⟹ β) : fix F α → fix F β := quotient.lift (λ x : q.P.W α, ⟦q.P.W_map g x⟧) (λ a b h, quot.sound (Wequiv_map _ _ _ h)) instance fix.mvfunctor : mvfunctor (fix F) := { map := @fix.map _ _ _ _} variable {α : typevec.{u} n} /-- Recursor for `fix F` -/ def fix.rec {β : Type u} (g : F (α ::: β) → β) : fix F α → β := quot.lift (recF g) (recF_eq_of_Wequiv α g) /-- Access W-type underlying `fix F` -/ def fix_to_W : fix F α → q.P.W α := quotient.lift Wrepr (recF_eq_of_Wequiv α (λ x, q.P.W_mk' (repr x))) /-- Constructor for `fix F` -/ def fix.mk (x : F (append1 α (fix F α))) : fix F α := quot.mk _ (q.P.W_mk' (append_fun id fix_to_W <$$> repr x)) /-- Destructor for `fix F` -/ def fix.dest : fix F α → F (append1 α (fix F α)) := fix.rec (mvfunctor.map (append_fun id fix.mk)) theorem fix.rec_eq {β : Type u} (g : F (append1 α β) → β) (x : F (append1 α (fix F α))) : fix.rec g (fix.mk x) = g (append_fun id (fix.rec g) <$$> x) := have recF g ∘ fix_to_W = fix.rec g, by { apply funext, apply quotient.ind, intro x, apply recF_eq_of_Wequiv, apply Wrepr_equiv }, begin conv { to_lhs, rw [fix.rec, fix.mk], dsimp }, cases h : repr x with a f, rw [mvpfunctor.map_eq, recF_eq', ←mvpfunctor.map_eq, mvpfunctor.W_dest'_W_mk'], rw [←mvpfunctor.comp_map, abs_map, ←h, abs_repr, ←append_fun_comp, id_comp, this] end theorem fix.ind_aux (a : q.P.A) (f' : q.P.drop.B a ⟹ α) (f : q.P.last.B a → q.P.W α) : fix.mk (abs ⟨a, q.P.append_contents f' (λ x, ⟦f x⟧)⟩) = ⟦q.P.W_mk a f' f⟧ := have fix.mk (abs ⟨a, q.P.append_contents f' (λ x, ⟦f x⟧)⟩) = ⟦Wrepr (q.P.W_mk a f' f)⟧, begin apply quot.sound, apply Wequiv.abs', rw [mvpfunctor.W_dest'_W_mk', abs_map, abs_repr, ←abs_map, mvpfunctor.map_eq], conv { to_rhs, rw [Wrepr_W_mk, q.P.W_dest'_W_mk', abs_repr, mvpfunctor.map_eq] }, congr' 2, rw [mvpfunctor.append_contents, mvpfunctor.append_contents], rw [append_fun, append_fun, ←split_fun_comp, ←split_fun_comp], reflexivity end, by { rw this, apply quot.sound, apply Wrepr_equiv } theorem fix.ind_rec {β : Type*} (g₁ g₂ : fix F α → β) (h : ∀ x : F (append1 α (fix F α)), (append_fun id g₁) <$$> x = (append_fun id g₂) <$$> x → g₁ (fix.mk x) = g₂ (fix.mk x)) : ∀ x, g₁ x = g₂ x := begin apply quot.ind, intro x, apply q.P.W_ind _ x, intros a f' f ih, show g₁ ⟦q.P.W_mk a f' f⟧ = g₂ ⟦q.P.W_mk a f' f⟧, rw [←fix.ind_aux a f' f], apply h, rw [←abs_map, ←abs_map, mvpfunctor.map_eq, mvpfunctor.map_eq], congr' 2, rw [mvpfunctor.append_contents, append_fun, append_fun, ←split_fun_comp, ←split_fun_comp], have : g₁ ∘ (λ x, ⟦f x⟧) = g₂ ∘ (λ x, ⟦f x⟧), { ext x, exact ih x }, rw this end theorem fix.rec_unique {β : Type*} (g : F (append1 α β) → β) (h : fix F α → β) (hyp : ∀ x, h (fix.mk x) = g (append_fun id h <$$> x)) : fix.rec g = h := begin ext x, apply fix.ind_rec, intros x hyp', rw [hyp, ←hyp', fix.rec_eq] end theorem fix.mk_dest (x : fix F α) : fix.mk (fix.dest x) = x := begin change (fix.mk ∘ fix.dest) x = x, apply fix.ind_rec, intro x, dsimp, rw [fix.dest, fix.rec_eq, ←comp_map, ←append_fun_comp, id_comp], intro h, rw h, show fix.mk (append_fun id id <$$> x) = fix.mk x, rw [append_fun_id_id, mvfunctor.id_map] end theorem fix.dest_mk (x : F (append1 α (fix F α))) : fix.dest (fix.mk x) = x := begin unfold fix.dest, rw [fix.rec_eq, ←fix.dest, ←comp_map], conv { to_rhs, rw ←(mvfunctor.id_map x) }, rw [←append_fun_comp, id_comp], have : fix.mk ∘ fix.dest = id, {ext x, apply fix.mk_dest }, rw [this, append_fun_id_id] end theorem fix.ind {α : typevec n} (p : fix F α → Prop) (h : ∀ x : F (α.append1 (fix F α)), liftp (pred_last α p) x → p (fix.mk x)) : ∀ x, p x := begin apply quot.ind, intro x, apply q.P.W_ind _ x, intros a f' f ih, change p ⟦q.P.W_mk a f' f⟧, rw [←fix.ind_aux a f' f], apply h, rw mvqpf.liftp_iff, refine ⟨_, _, rfl, _⟩, intros i j, cases i, { apply ih }, { trivial }, end instance mvqpf_fix : mvqpf (fix F) := { P := q.P.Wp, abs := λ α, quot.mk Wequiv, repr := λ α, fix_to_W, abs_repr := by { intros α, apply quot.ind, intro a, apply quot.sound, apply Wrepr_equiv }, abs_map := begin intros α β g x, conv { to_rhs, dsimp [mvfunctor.map]}, rw fix.map, apply quot.sound, apply Wequiv.refl end } /-- Dependent recursor for `fix F` -/ def fix.drec {β : fix F α → Type u} (g : Π x : F (α ::: sigma β), β (fix.mk $ (id ::: sigma.fst) <$$> x)) (x : fix F α) : β x := let y := @fix.rec _ F _ _ α (sigma β) (λ i, ⟨_,g i⟩) x in have x = y.1, by { symmetry, dsimp [y], apply fix.ind_rec _ id _ x, intros x' ih, rw fix.rec_eq, dsimp, simp [append_fun_id_id] at ih, congr, conv { to_rhs, rw [← ih] }, rw [mvfunctor.map_map,← append_fun_comp,id_comp], }, cast (by rw this) y.2 end mvqpf
291961a265034c74332019050a3b8b6e7bf12935
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/data/matrix/dmatrix.lean
a8a085457870886d469078de7167bf3d0b731e09
[ "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
5,418
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import data.fintype.basic /-! # Matrices -/ universes u u' v w z /-- `dmatrix m n` is the type of dependently typed matrices whose rows are indexed by the fintype `m` and whose columns are indexed by the fintype `n`. -/ @[nolint unused_arguments] def dmatrix (m : Type u) (n : Type u') [fintype m] [fintype n] (α : m → n → Type v) : Type (max u u' v) := Π i j, α i j variables {l m n o : Type*} [fintype l] [fintype m] [fintype n] [fintype o] variables {α : m → n → Type v} namespace dmatrix section ext variables {M N : dmatrix m n α} theorem ext_iff : (∀ i j, M i j = N i j) ↔ M = N := ⟨λ h, funext $ λ i, funext $ h i, λ h, by simp [h]⟩ @[ext] theorem ext : (∀ i j, M i j = N i j) → M = N := ext_iff.mp end ext /-- `M.map f` is the dmatrix obtained by applying `f` to each entry of the matrix `M`. -/ def map (M : dmatrix m n α) {β : m → n → Type w} (f : Π ⦃i j⦄, α i j → β i j) : dmatrix m n β := λ i j, f (M i j) @[simp] lemma map_apply {M : dmatrix m n α} {β : m → n → Type w} {f : Π ⦃i j⦄, α i j → β i j} {i : m} {j : n} : M.map f i j = f (M i j) := rfl @[simp] lemma map_map {M : dmatrix m n α} {β : m → n → Type w} {γ : m → n → Type z} {f : Π ⦃i j⦄, α i j → β i j} {g : Π ⦃i j⦄, β i j → γ i j} : (M.map f).map g = M.map (λ i j x, g (f x)) := by { ext, simp, } /-- The transpose of a dmatrix. -/ def transpose (M : dmatrix m n α) : dmatrix n m (λ j i, α i j) | x y := M y x localized "postfix `ᵀ`:1500 := dmatrix.transpose" in dmatrix /-- `dmatrix.col u` is the column matrix whose entries are given by `u`. -/ def col {α : m → Type v} (w : Π i, α i) : dmatrix m unit (λ i j, α i) | x y := w x /-- `dmatrix.row u` is the row matrix whose entries are given by `u`. -/ def row {α : n → Type v} (v : Π j, α j) : dmatrix unit n (λ i j, α j) | x y := v y instance [∀ i j, inhabited (α i j)] : inhabited (dmatrix m n α) := pi.inhabited _ instance [∀ i j, has_add (α i j)] : has_add (dmatrix m n α) := pi.has_add instance [∀ i j, add_semigroup (α i j)] : add_semigroup (dmatrix m n α) := pi.add_semigroup instance [∀ i j, add_comm_semigroup (α i j)] : add_comm_semigroup (dmatrix m n α) := pi.add_comm_semigroup instance [∀ i j, has_zero (α i j)] : has_zero (dmatrix m n α) := pi.has_zero instance [∀ i j, add_monoid (α i j)] : add_monoid (dmatrix m n α) := pi.add_monoid instance [∀ i j, add_comm_monoid (α i j)] : add_comm_monoid (dmatrix m n α) := pi.add_comm_monoid instance [∀ i j, has_neg (α i j)] : has_neg (dmatrix m n α) := pi.has_neg instance [∀ i j, has_sub (α i j)] : has_sub (dmatrix m n α) := pi.has_sub instance [∀ i j, add_group (α i j)] : add_group (dmatrix m n α) := pi.add_group instance [∀ i j, add_comm_group (α i j)] : add_comm_group (dmatrix m n α) := pi.add_comm_group instance [∀ i j, unique (α i j)] : unique (dmatrix m n α) := pi.unique instance [∀ i j, subsingleton (α i j)] : subsingleton (dmatrix m n α) := pi.subsingleton @[simp] theorem zero_apply [∀ i j, has_zero (α i j)] (i j) : (0 : dmatrix m n α) i j = 0 := rfl @[simp] theorem neg_apply [∀ i j, has_neg (α i j)] (M : dmatrix m n α) (i j) : (- M) i j = - M i j := rfl @[simp] theorem add_apply [∀ i j, has_add (α i j)] (M N : dmatrix m n α) (i j) : (M + N) i j = M i j + N i j := rfl @[simp] theorem sub_apply [∀ i j, has_sub (α i j)] (M N : dmatrix m n α) (i j) : (M - N) i j = M i j - N i j := rfl @[simp] lemma map_zero [∀ i j, has_zero (α i j)] {β : m → n → Type w} [∀ i j, has_zero (β i j)] {f : Π ⦃i j⦄, α i j → β i j} (h : ∀ i j, f (0 : α i j) = 0) : (0 : dmatrix m n α).map f = 0 := by { ext, simp [h], } lemma map_add [∀ i j, add_monoid (α i j)] {β : m → n → Type w} [∀ i j, add_monoid (β i j)] (f : Π ⦃i j⦄, α i j →+ β i j) (M N : dmatrix m n α) : (M + N).map (λ i j, @f i j) = M.map (λ i j, @f i j) + N.map (λ i j, @f i j) := by { ext, simp, } lemma map_sub [∀ i j, add_group (α i j)] {β : m → n → Type w} [∀ i j, add_group (β i j)] (f : Π ⦃i j⦄, α i j →+ β i j) (M N : dmatrix m n α) : (M - N).map (λ i j, @f i j) = M.map (λ i j, @f i j) - N.map (λ i j, @f i j) := by { ext, simp } instance subsingleton_of_empty_left [is_empty m] : subsingleton (dmatrix m n α) := ⟨λ M N, by { ext, exact is_empty_elim i }⟩ instance subsingleton_of_empty_right [is_empty n] : subsingleton (dmatrix m n α) := ⟨λ M N, by { ext, exact is_empty_elim j }⟩ end dmatrix /-- The `add_monoid_hom` between spaces of dependently typed matrices induced by an `add_monoid_hom` between their coefficients. -/ def add_monoid_hom.map_dmatrix [∀ i j, add_monoid (α i j)] {β : m → n → Type w} [∀ i j, add_monoid (β i j)] (f : Π ⦃i j⦄, α i j →+ β i j) : dmatrix m n α →+ dmatrix m n β := { to_fun := λ M, M.map (λ i j, @f i j), map_zero' := by simp, map_add' := dmatrix.map_add f, } @[simp] lemma add_monoid_hom.map_dmatrix_apply [∀ i j, add_monoid (α i j)] {β : m → n → Type w} [∀ i j, add_monoid (β i j)] (f : Π ⦃i j⦄, α i j →+ β i j) (M : dmatrix m n α) : add_monoid_hom.map_dmatrix f M = M.map (λ i j, @f i j) := rfl
c355031acbf8afb11bada8f689053fdb045aace1
bb31430994044506fa42fd667e2d556327e18dfe
/src/analysis/special_functions/pow.lean
4fd66dc7d331453e7c0990092b0e471cfa7b956f
[ "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
92,853
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Sébastien Gouëzel, Rémy Degenne, David Loeffler -/ import analysis.special_functions.complex.log /-! # Power function on `ℂ`, `ℝ`, `ℝ≥0`, and `ℝ≥0∞` We construct the power functions `x ^ y` where * `x` and `y` are complex numbers, * or `x` and `y` are real numbers, * or `x` is a nonnegative real number and `y` is a real number; * or `x` is a number from `[0, +∞]` (a.k.a. `ℝ≥0∞`) and `y` is a real number. We also prove basic properties of these functions. -/ noncomputable theory open_locale classical real topological_space nnreal ennreal filter big_operators asymptotics open filter finset set namespace complex /-- The complex power function `x^y`, given by `x^y = exp(y log x)` (where `log` is the principal determination of the logarithm), unless `x = 0` where one sets `0^0 = 1` and `0^y = 0` for `y ≠ 0`. -/ noncomputable def cpow (x y : ℂ) : ℂ := if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) noncomputable instance : has_pow ℂ ℂ := ⟨cpow⟩ @[simp] lemma cpow_eq_pow (x y : ℂ) : cpow x y = x ^ y := rfl lemma cpow_def (x y : ℂ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := rfl lemma cpow_def_of_ne_zero {x : ℂ} (hx : x ≠ 0) (y : ℂ) : x ^ y = exp (log x * y) := if_neg hx @[simp] lemma cpow_zero (x : ℂ) : x ^ (0 : ℂ) = 1 := by simp [cpow_def] @[simp] lemma cpow_eq_zero_iff (x y : ℂ) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [cpow_def], split_ifs; simp [*, exp_ne_zero] } @[simp] lemma zero_cpow {x : ℂ} (h : x ≠ 0) : (0 : ℂ) ^ x = 0 := by simp [cpow_def, *] lemma zero_cpow_eq_iff {x : ℂ} {a : ℂ} : 0 ^ x = a ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := begin split, { intros hyp, simp only [cpow_def, eq_self_iff_true, if_true] at hyp, by_cases x = 0, { subst h, simp only [if_true, eq_self_iff_true] at hyp, right, exact ⟨rfl, hyp.symm⟩}, { rw if_neg h at hyp, left, exact ⟨h, hyp.symm⟩, }, }, { rintro (⟨h, rfl⟩|⟨rfl,rfl⟩), { exact zero_cpow h, }, { exact cpow_zero _, }, }, end lemma eq_zero_cpow_iff {x : ℂ} {a : ℂ} : a = 0 ^ x ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := by rw [←zero_cpow_eq_iff, eq_comm] @[simp] lemma cpow_one (x : ℂ) : x ^ (1 : ℂ) = x := if hx : x = 0 then by simp [hx, cpow_def] else by rw [cpow_def, if_neg (one_ne_zero : (1 : ℂ) ≠ 0), if_neg hx, mul_one, exp_log hx] @[simp] lemma one_cpow (x : ℂ) : (1 : ℂ) ^ x = 1 := by rw cpow_def; split_ifs; simp [one_ne_zero, *] at * lemma cpow_add {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := by simp only [cpow_def, ite_mul, boole_mul, mul_ite, mul_boole]; simp [*, exp_add, mul_add] at * lemma cpow_mul {x y : ℂ} (z : ℂ) (h₁ : -π < (log x * y).im) (h₂ : (log x * y).im ≤ π) : x ^ (y * z) = (x ^ y) ^ z := begin simp only [cpow_def], split_ifs; simp [*, exp_ne_zero, log_exp h₁ h₂, mul_assoc] at * end lemma cpow_neg (x y : ℂ) : x ^ -y = (x ^ y)⁻¹ := by simp only [cpow_def, neg_eq_zero, mul_neg]; split_ifs; simp [exp_neg] lemma cpow_sub {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := by rw [sub_eq_add_neg, cpow_add _ _ hx, cpow_neg, div_eq_mul_inv] lemma cpow_neg_one (x : ℂ) : x ^ (-1 : ℂ) = x⁻¹ := by simpa using cpow_neg x 1 @[simp, norm_cast] lemma cpow_nat_cast (x : ℂ) : ∀ (n : ℕ), x ^ (n : ℂ) = x ^ n | 0 := by simp | (n + 1) := if hx : x = 0 then by simp only [hx, pow_succ, complex.zero_cpow (nat.cast_ne_zero.2 (nat.succ_ne_zero _)), zero_mul] else by simp [cpow_add, hx, pow_add, cpow_nat_cast n] @[simp] lemma cpow_two (x : ℂ) : x ^ (2 : ℂ) = x ^ 2 := by { rw ← cpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] } @[simp, norm_cast] lemma cpow_int_cast (x : ℂ) : ∀ (n : ℤ), x ^ (n : ℂ) = x ^ n | (n : ℕ) := by simp | -[1+ n] := by rw zpow_neg_succ_of_nat; simp only [int.neg_succ_of_nat_coe, int.cast_neg, complex.cpow_neg, inv_eq_one_div, int.cast_coe_nat, cpow_nat_cast] lemma cpow_nat_inv_pow (x : ℂ) {n : ℕ} (hn : n ≠ 0) : (x ^ (n⁻¹ : ℂ)) ^ n = x := begin suffices : im (log x * n⁻¹) ∈ Ioc (-π) π, { rw [← cpow_nat_cast, ← cpow_mul _ this.1 this.2, inv_mul_cancel, cpow_one], exact_mod_cast hn }, rw [mul_comm, ← of_real_nat_cast, ← of_real_inv, of_real_mul_im, ← div_eq_inv_mul], rw [← pos_iff_ne_zero] at hn, have hn' : 0 < (n : ℝ), by assumption_mod_cast, have hn1 : 1 ≤ (n : ℝ), by exact_mod_cast (nat.succ_le_iff.2 hn), split, { rw lt_div_iff hn', calc -π * n ≤ -π * 1 : mul_le_mul_of_nonpos_left hn1 (neg_nonpos.2 real.pi_pos.le) ... = -π : mul_one _ ... < im (log x) : neg_pi_lt_log_im _ }, { rw div_le_iff hn', calc im (log x) ≤ π : log_im_le_pi _ ... = π * 1 : (mul_one π).symm ... ≤ π * n : mul_le_mul_of_nonneg_left hn1 real.pi_pos.le } end end complex section lim open complex variables {α : Type*} lemma zero_cpow_eq_nhds {b : ℂ} (hb : b ≠ 0) : (0 : ℂ).cpow =ᶠ[𝓝 b] 0 := begin suffices : ∀ᶠ (x : ℂ) in (𝓝 b), x ≠ 0, from this.mono (λ x hx, by rw [cpow_eq_pow, zero_cpow hx, pi.zero_apply]), exact is_open.eventually_mem is_open_ne hb, end lemma cpow_eq_nhds {a b : ℂ} (ha : a ≠ 0) : (λ x, x.cpow b) =ᶠ[𝓝 a] λ x, exp (log x * b) := begin suffices : ∀ᶠ (x : ℂ) in (𝓝 a), x ≠ 0, from this.mono (λ x hx, by { dsimp only, rw [cpow_eq_pow, cpow_def_of_ne_zero hx], }), exact is_open.eventually_mem is_open_ne ha, end lemma cpow_eq_nhds' {p : ℂ × ℂ} (hp_fst : p.fst ≠ 0) : (λ x, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) := begin suffices : ∀ᶠ (x : ℂ × ℂ) in (𝓝 p), x.1 ≠ 0, from this.mono (λ x hx, by { dsimp only, rw cpow_def_of_ne_zero hx, }), refine is_open.eventually_mem _ hp_fst, change is_open {x : ℂ × ℂ | x.1 = 0}ᶜ, rw is_open_compl_iff, exact is_closed_eq continuous_fst continuous_const, end lemma continuous_at_const_cpow {a b : ℂ} (ha : a ≠ 0) : continuous_at (cpow a) b := begin have cpow_eq : cpow a = λ b, exp (log a * b), by { ext1 b, rw [cpow_eq_pow, cpow_def_of_ne_zero ha], }, rw cpow_eq, exact continuous_exp.continuous_at.comp (continuous_at.mul continuous_at_const continuous_at_id), end lemma continuous_at_const_cpow' {a b : ℂ} (h : b ≠ 0) : continuous_at (cpow a) b := begin by_cases ha : a = 0, { rw [ha, continuous_at_congr (zero_cpow_eq_nhds h)], exact continuous_at_const, }, { exact continuous_at_const_cpow ha, }, end /-- The function `z ^ w` is continuous in `(z, w)` provided that `z` does not belong to the interval `(-∞, 0]` on the real line. See also `complex.continuous_at_cpow_zero_of_re_pos` for a version that works for `z = 0` but assumes `0 < re w`. -/ lemma continuous_at_cpow {p : ℂ × ℂ} (hp_fst : 0 < p.fst.re ∨ p.fst.im ≠ 0) : continuous_at (λ x : ℂ × ℂ, x.1 ^ x.2) p := begin have hp_fst_ne_zero : p.fst ≠ 0, by { intro h, cases hp_fst; { rw h at hp_fst, simpa using hp_fst, }, }, rw continuous_at_congr (cpow_eq_nhds' hp_fst_ne_zero), refine continuous_exp.continuous_at.comp _, refine continuous_at.mul (continuous_at.comp _ continuous_fst.continuous_at) continuous_snd.continuous_at, exact continuous_at_clog hp_fst, end lemma continuous_at_cpow_const {a b : ℂ} (ha : 0 < a.re ∨ a.im ≠ 0) : continuous_at (λ x, cpow x b) a := tendsto.comp (@continuous_at_cpow (a, b) ha) (continuous_at_id.prod continuous_at_const) lemma filter.tendsto.cpow {l : filter α} {f g : α → ℂ} {a b : ℂ} (hf : tendsto f l (𝓝 a)) (hg : tendsto g l (𝓝 b)) (ha : 0 < a.re ∨ a.im ≠ 0) : tendsto (λ x, f x ^ g x) l (𝓝 (a ^ b)) := (@continuous_at_cpow (a,b) ha).tendsto.comp (hf.prod_mk_nhds hg) lemma filter.tendsto.const_cpow {l : filter α} {f : α → ℂ} {a b : ℂ} (hf : tendsto f l (𝓝 b)) (h : a ≠ 0 ∨ b ≠ 0) : tendsto (λ x, a ^ f x) l (𝓝 (a ^ b)) := begin cases h, { exact (continuous_at_const_cpow h).tendsto.comp hf, }, { exact (continuous_at_const_cpow' h).tendsto.comp hf, }, end variables [topological_space α] {f g : α → ℂ} {s : set α} {a : α} lemma continuous_within_at.cpow (hf : continuous_within_at f s a) (hg : continuous_within_at g s a) (h0 : 0 < (f a).re ∨ (f a).im ≠ 0) : continuous_within_at (λ x, f x ^ g x) s a := hf.cpow hg h0 lemma continuous_within_at.const_cpow {b : ℂ} (hf : continuous_within_at f s a) (h : b ≠ 0 ∨ f a ≠ 0) : continuous_within_at (λ x, b ^ f x) s a := hf.const_cpow h lemma continuous_at.cpow (hf : continuous_at f a) (hg : continuous_at g a) (h0 : 0 < (f a).re ∨ (f a).im ≠ 0) : continuous_at (λ x, f x ^ g x) a := hf.cpow hg h0 lemma continuous_at.const_cpow {b : ℂ} (hf : continuous_at f a) (h : b ≠ 0 ∨ f a ≠ 0) : continuous_at (λ x, b ^ f x) a := hf.const_cpow h lemma continuous_on.cpow (hf : continuous_on f s) (hg : continuous_on g s) (h0 : ∀ a ∈ s, 0 < (f a).re ∨ (f a).im ≠ 0) : continuous_on (λ x, f x ^ g x) s := λ a ha, (hf a ha).cpow (hg a ha) (h0 a ha) lemma continuous_on.const_cpow {b : ℂ} (hf : continuous_on f s) (h : b ≠ 0 ∨ ∀ a ∈ s, f a ≠ 0) : continuous_on (λ x, b ^ f x) s := λ a ha, (hf a ha).const_cpow (h.imp id $ λ h, h a ha) lemma continuous.cpow (hf : continuous f) (hg : continuous g) (h0 : ∀ a, 0 < (f a).re ∨ (f a).im ≠ 0) : continuous (λ x, f x ^ g x) := continuous_iff_continuous_at.2 $ λ a, (hf.continuous_at.cpow hg.continuous_at (h0 a)) lemma continuous.const_cpow {b : ℂ} (hf : continuous f) (h : b ≠ 0 ∨ ∀ a, f a ≠ 0) : continuous (λ x, b ^ f x) := continuous_iff_continuous_at.2 $ λ a, (hf.continuous_at.const_cpow $ h.imp id $ λ h, h a) lemma continuous_on.cpow_const {b : ℂ} (hf : continuous_on f s) (h : ∀ (a : α), a ∈ s → 0 < (f a).re ∨ (f a).im ≠ 0) : continuous_on (λ x, (f x) ^ b) s := hf.cpow continuous_on_const h end lim namespace real /-- The real power function `x^y`, defined as the real part of the complex power function. For `x > 0`, it is equal to `exp(y log x)`. For `x = 0`, one sets `0^0=1` and `0^y=0` for `y ≠ 0`. For `x < 0`, the definition is somewhat arbitary as it depends on the choice of a complex determination of the logarithm. With our conventions, it is equal to `exp (y log x) cos (πy)`. -/ noncomputable def rpow (x y : ℝ) := ((x : ℂ) ^ (y : ℂ)).re noncomputable instance : has_pow ℝ ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x y : ℝ) : rpow x y = x ^ y := rfl lemma rpow_def (x y : ℝ) : x ^ y = ((x : ℂ) ^ (y : ℂ)).re := rfl lemma rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := by simp only [rpow_def, complex.cpow_def]; split_ifs; simp [*, (complex.of_real_log hx).symm, -complex.of_real_mul, -is_R_or_C.of_real_mul, (complex.of_real_mul _ _).symm, complex.exp_of_real_re] at * lemma rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) := by rw [rpow_def_of_nonneg (le_of_lt hx), if_neg (ne_of_gt hx)] lemma exp_mul (x y : ℝ) : exp (x * y) = (exp x) ^ y := by rw [rpow_def_of_pos (exp_pos _), log_exp] @[simp] lemma exp_one_rpow (x : ℝ) : exp 1 ^ x = exp x := by rw [←exp_mul, one_mul] lemma rpow_eq_zero_iff_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [rpow_def_of_nonneg hx], split_ifs; simp [*, exp_ne_zero] } open_locale real lemma rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log x * y) * cos (y * π) := begin rw [rpow_def, complex.cpow_def, if_neg], have : complex.log x * y = ↑(log(-x) * y) + ↑(y * π) * complex.I, { simp only [complex.log, abs_of_neg hx, complex.arg_of_real_of_neg hx, complex.abs_of_real, complex.of_real_mul], ring }, { rw [this, complex.exp_add_mul_I, ← complex.of_real_exp, ← complex.of_real_cos, ← complex.of_real_sin, mul_add, ← complex.of_real_mul, ← mul_assoc, ← complex.of_real_mul, complex.add_re, complex.of_real_re, complex.mul_re, complex.I_re, complex.of_real_im, real.log_neg_eq_log], ring }, { rw complex.of_real_eq_zero, exact ne_of_lt hx } end lemma rpow_def_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) * cos (y * π) := by split_ifs; simp [rpow_def, *]; exact rpow_def_of_neg (lt_of_le_of_ne hx h) _ lemma rpow_pos_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : 0 < x ^ y := by rw rpow_def_of_pos hx; apply exp_pos @[simp] lemma rpow_zero (x : ℝ) : x ^ (0 : ℝ) = 1 := by simp [rpow_def] @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ) ^ x = 0 := by simp [rpow_def, *] lemma zero_rpow_eq_iff {x : ℝ} {a : ℝ} : 0 ^ x = a ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := begin split, { intros hyp, simp only [rpow_def, complex.of_real_zero] at hyp, by_cases x = 0, { subst h, simp only [complex.one_re, complex.of_real_zero, complex.cpow_zero] at hyp, exact or.inr ⟨rfl, hyp.symm⟩}, { rw complex.zero_cpow (complex.of_real_ne_zero.mpr h) at hyp, exact or.inl ⟨h, hyp.symm⟩, }, }, { rintro (⟨h,rfl⟩|⟨rfl,rfl⟩), { exact zero_rpow h, }, { exact rpow_zero _, }, }, end lemma eq_zero_rpow_iff {x : ℝ} {a : ℝ} : a = 0 ^ x ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := by rw [←zero_rpow_eq_iff, eq_comm] @[simp] lemma rpow_one (x : ℝ) : x ^ (1 : ℝ) = x := by simp [rpow_def] @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ) ^ x = 1 := by simp [rpow_def] lemma zero_rpow_le_one (x : ℝ) : (0 : ℝ) ^ x ≤ 1 := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma zero_rpow_nonneg (x : ℝ) : 0 ≤ (0 : ℝ) ^ x := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma rpow_nonneg_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : 0 ≤ x ^ y := by rw [rpow_def_of_nonneg hx]; split_ifs; simp only [zero_le_one, le_refl, le_of_lt (exp_pos _)] lemma abs_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : |x ^ y| = |x| ^ y := begin have h_rpow_nonneg : 0 ≤ x ^ y, from real.rpow_nonneg_of_nonneg hx_nonneg _, rw [abs_eq_self.mpr hx_nonneg, abs_eq_self.mpr h_rpow_nonneg], end lemma abs_rpow_le_abs_rpow (x y : ℝ) : |x ^ y| ≤ |x| ^ y := begin cases le_or_lt 0 x with hx hx, { rw [abs_rpow_of_nonneg hx] }, { rw [abs_of_neg hx, rpow_def_of_neg hx, rpow_def_of_pos (neg_pos.2 hx), log_neg_eq_log, abs_mul, abs_of_pos (exp_pos _)], exact mul_le_of_le_one_right (exp_pos _).le (abs_cos_le_one _) } end lemma abs_rpow_le_exp_log_mul (x y : ℝ) : |x ^ y| ≤ exp (log x * y) := begin refine (abs_rpow_le_abs_rpow x y).trans _, by_cases hx : x = 0, { by_cases hy : y = 0; simp [hx, hy, zero_le_one] }, { rw [rpow_def_of_pos (abs_pos.2 hx), log_abs] } end lemma norm_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : ‖x ^ y‖ = ‖x‖ ^ y := by { simp_rw real.norm_eq_abs, exact abs_rpow_of_nonneg hx_nonneg, } end real namespace complex lemma of_real_cpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : ((x ^ y : ℝ) : ℂ) = (x : ℂ) ^ (y : ℂ) := by simp only [real.rpow_def_of_nonneg hx, complex.cpow_def, of_real_eq_zero]; split_ifs; simp [complex.of_real_log hx] lemma of_real_cpow_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℂ) : (x : ℂ) ^ y = ((-x) : ℂ) ^ y * exp (π * I * y) := begin rcases hx.eq_or_lt with rfl|hlt, { rcases eq_or_ne y 0 with rfl|hy; simp * }, have hne : (x : ℂ) ≠ 0, from of_real_ne_zero.mpr hlt.ne, rw [cpow_def_of_ne_zero hne, cpow_def_of_ne_zero (neg_ne_zero.2 hne), ← exp_add, ← add_mul, log, log, abs.map_neg, arg_of_real_of_neg hlt, ← of_real_neg, arg_of_real_of_nonneg (neg_nonneg.2 hx), of_real_zero, zero_mul, add_zero] end lemma abs_cpow_of_ne_zero {z : ℂ} (hz : z ≠ 0) (w : ℂ) : abs (z ^ w) = abs z ^ w.re / real.exp (arg z * im w) := by rw [cpow_def_of_ne_zero hz, abs_exp, mul_re, log_re, log_im, real.exp_sub, real.rpow_def_of_pos (abs.pos hz)] lemma abs_cpow_of_imp {z w : ℂ} (h : z = 0 → w.re = 0 → w = 0) : abs (z ^ w) = abs z ^ w.re / real.exp (arg z * im w) := begin rcases ne_or_eq z 0 with hz|rfl; [exact (abs_cpow_of_ne_zero hz w), rw map_zero], cases eq_or_ne w.re 0 with hw hw, { simp [hw, h rfl hw] }, { rw [real.zero_rpow hw, zero_div, zero_cpow, map_zero], exact ne_of_apply_ne re hw } end lemma abs_cpow_le (z w : ℂ) : abs (z ^ w) ≤ abs z ^ w.re / real.exp (arg z * im w) := begin rcases ne_or_eq z 0 with hz|rfl; [exact (abs_cpow_of_ne_zero hz w).le, rw map_zero], rcases eq_or_ne w 0 with rfl|hw, { simp }, rw [zero_cpow hw, map_zero], exact div_nonneg (real.rpow_nonneg_of_nonneg le_rfl _) (real.exp_pos _).le end section variables {α : Type*} {l : filter α} {f g : α → ℂ} open asymptotics lemma is_Theta_exp_arg_mul_im (hl : is_bounded_under (≤) l (λ x, |(g x).im|)) : (λ x, real.exp (arg (f x) * im (g x))) =Θ[l] (λ x, (1 : ℝ)) := begin rcases hl with ⟨b, hb⟩, refine real.is_Theta_exp_comp_one.2 ⟨π * b, _⟩, rw eventually_map at hb ⊢, refine hb.mono (λ x hx, _), erw [abs_mul], exact mul_le_mul (abs_arg_le_pi _) hx (abs_nonneg _) real.pi_pos.le end lemma is_O_cpow_rpow (hl : is_bounded_under (≤) l (λ x, |(g x).im|)) : (λ x, f x ^ g x) =O[l] (λ x, abs (f x) ^ (g x).re) := calc (λ x, f x ^ g x) =O[l] (λ x, abs (f x) ^ (g x).re / real.exp (arg (f x) * im (g x))) : is_O_of_le _ $ λ x, (abs_cpow_le _ _).trans (le_abs_self _) ... =Θ[l] (λ x, abs (f x) ^ (g x).re / (1 : ℝ)) : (is_Theta_refl _ _).div (is_Theta_exp_arg_mul_im hl) ... =ᶠ[l] (λ x, abs (f x) ^ (g x).re) : by simp only [of_real_one, div_one] lemma is_Theta_cpow_rpow (hl_im : is_bounded_under (≤) l (λ x, |(g x).im|)) (hl : ∀ᶠ x in l, f x = 0 → re (g x) = 0 → g x = 0): (λ x, f x ^ g x) =Θ[l] (λ x, abs (f x) ^ (g x).re) := calc (λ x, f x ^ g x) =Θ[l] (λ x, abs (f x) ^ (g x).re / real.exp (arg (f x) * im (g x))) : is_Theta_of_norm_eventually_eq' $ hl.mono $ λ x, abs_cpow_of_imp ... =Θ[l] (λ x, abs (f x) ^ (g x).re / (1 : ℝ)) : (is_Theta_refl _ _).div (is_Theta_exp_arg_mul_im hl_im) ... =ᶠ[l] (λ x, abs (f x) ^ (g x).re) : by simp only [of_real_one, div_one] lemma is_Theta_cpow_const_rpow {b : ℂ} (hl : b.re = 0 → b ≠ 0 → ∀ᶠ x in l, f x ≠ 0) : (λ x, f x ^ b) =Θ[l] (λ x, abs (f x) ^ b.re) := is_Theta_cpow_rpow is_bounded_under_const $ by simpa only [eventually_imp_distrib_right, ne.def, ← not_frequently, not_imp_not, imp.swap] using hl end @[simp] lemma abs_cpow_real (x : ℂ) (y : ℝ) : abs (x ^ (y : ℂ)) = x.abs ^ y := by rcases eq_or_ne x 0 with rfl|hx; [rcases eq_or_ne y 0 with rfl|hy, skip]; simp [*, abs_cpow_of_ne_zero] @[simp] lemma abs_cpow_inv_nat (x : ℂ) (n : ℕ) : abs (x ^ (n⁻¹ : ℂ)) = x.abs ^ (n⁻¹ : ℝ) := by rw ← abs_cpow_real; simp [-abs_cpow_real] lemma abs_cpow_eq_rpow_re_of_pos {x : ℝ} (hx : 0 < x) (y : ℂ) : abs (x ^ y) = x ^ y.re := by rw [abs_cpow_of_ne_zero (of_real_ne_zero.mpr hx.ne'), arg_of_real_of_nonneg hx.le, zero_mul, real.exp_zero, div_one, abs_of_nonneg hx.le] lemma abs_cpow_eq_rpow_re_of_nonneg {x : ℝ} (hx : 0 ≤ x) {y : ℂ} (hy : re y ≠ 0) : abs (x ^ y) = x ^ re y := begin rcases hx.eq_or_lt with rfl|hlt, { rw [of_real_zero, zero_cpow, map_zero, real.zero_rpow hy], exact ne_of_apply_ne re hy }, { exact abs_cpow_eq_rpow_re_of_pos hlt y } end end complex namespace real variables {x y z : ℝ} lemma rpow_add (hx : 0 < x) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := by simp only [rpow_def_of_pos hx, mul_add, exp_add] lemma rpow_add' (hx : 0 ≤ x) (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := begin rcases hx.eq_or_lt with rfl|pos, { rw [zero_rpow h, zero_eq_mul], have : y ≠ 0 ∨ z ≠ 0, from not_and_distrib.1 (λ ⟨hy, hz⟩, h $ hy.symm ▸ hz.symm ▸ zero_add 0), exact this.imp zero_rpow zero_rpow }, { exact rpow_add pos _ _ } end lemma rpow_add_of_nonneg (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 ≤ z) : x ^ (y + z) = x ^ y * x ^ z := begin rcases hy.eq_or_lt with rfl|hy, { rw [zero_add, rpow_zero, one_mul] }, exact rpow_add' hx (ne_of_gt $ add_pos_of_pos_of_nonneg hy hz) end /-- For `0 ≤ x`, the only problematic case in the equality `x ^ y * x ^ z = x ^ (y + z)` is for `x = 0` and `y + z = 0`, where the right hand side is `1` while the left hand side can vanish. The inequality is always true, though, and given in this lemma. -/ lemma le_rpow_add {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ y * x ^ z ≤ x ^ (y + z) := begin rcases le_iff_eq_or_lt.1 hx with H|pos, { by_cases h : y + z = 0, { simp only [H.symm, h, rpow_zero], calc (0 : ℝ) ^ y * 0 ^ z ≤ 1 * 1 : mul_le_mul (zero_rpow_le_one y) (zero_rpow_le_one z) (zero_rpow_nonneg z) zero_le_one ... = 1 : by simp }, { simp [rpow_add', ← H, h] } }, { simp [rpow_add pos] } end lemma rpow_sum_of_pos {ι : Type*} {a : ℝ} (ha : 0 < a) (f : ι → ℝ) (s : finset ι) : a ^ (∑ x in s, f x) = ∏ x in s, a ^ f x := @add_monoid_hom.map_sum ℝ ι (additive ℝ) _ _ ⟨λ x : ℝ, (a ^ x : ℝ), rpow_zero a, rpow_add ha⟩ f s lemma rpow_sum_of_nonneg {ι : Type*} {a : ℝ} (ha : 0 ≤ a) {s : finset ι} {f : ι → ℝ} (h : ∀ x ∈ s, 0 ≤ f x) : a ^ (∑ x in s, f x) = ∏ x in s, a ^ f x := begin induction s using finset.cons_induction with i s hi ihs, { rw [sum_empty, finset.prod_empty, rpow_zero] }, { rw forall_mem_cons at h, rw [sum_cons, prod_cons, ← ihs h.2, rpow_add_of_nonneg ha h.1 (sum_nonneg h.2)] } end lemma rpow_mul {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := by rw [← complex.of_real_inj, complex.of_real_cpow (rpow_nonneg_of_nonneg hx _), complex.of_real_cpow hx, complex.of_real_mul, complex.cpow_mul, complex.of_real_cpow hx]; simp only [(complex.of_real_mul _ _).symm, (complex.of_real_log hx).symm, complex.of_real_im, neg_lt_zero, pi_pos, le_of_lt pi_pos] lemma rpow_neg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := by simp only [rpow_def_of_nonneg hx]; split_ifs; simp [*, exp_neg] at * lemma rpow_sub {x : ℝ} (hx : 0 < x) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := by simp only [sub_eq_add_neg, rpow_add hx, rpow_neg (le_of_lt hx), div_eq_mul_inv] lemma rpow_sub' {x : ℝ} (hx : 0 ≤ x) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := by { simp only [sub_eq_add_neg] at h ⊢, simp only [rpow_add' hx h, rpow_neg hx, div_eq_mul_inv] } lemma rpow_add_int {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y + n) = x ^ y * x ^ n := by rw [rpow_def, complex.of_real_add, complex.cpow_add _ _ (complex.of_real_ne_zero.mpr hx), complex.of_real_int_cast, complex.cpow_int_cast, ← complex.of_real_zpow, mul_comm, complex.of_real_mul_re, ← rpow_def, mul_comm] lemma rpow_add_nat {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y + n) = x ^ y * x ^ n := by simpa using rpow_add_int hx y n lemma rpow_sub_int {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y - n) = x ^ y / x ^ n := by simpa using rpow_add_int hx y (-n) lemma rpow_sub_nat {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y - n) = x ^ y / x ^ n := by simpa using rpow_sub_int hx y n lemma rpow_add_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y + 1) = x ^ y * x := by simpa using rpow_add_nat hx y 1 lemma rpow_sub_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y - 1) = x ^ y / x := by simpa using rpow_sub_nat hx y 1 @[simp, norm_cast] lemma rpow_int_cast (x : ℝ) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, ← complex.of_real_zpow, complex.cpow_int_cast, complex.of_real_int_cast, complex.of_real_re] @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ) (n : ℕ) : x ^ (n : ℝ) = x ^ n := by simpa using rpow_int_cast x n @[simp] lemma rpow_two (x : ℝ) : x ^ (2 : ℝ) = x ^ 2 := by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] } lemma rpow_neg_one (x : ℝ) : x ^ (-1 : ℝ) = x⁻¹ := begin suffices H : x ^ ((-1 : ℤ) : ℝ) = x⁻¹, by rwa [int.cast_neg, int.cast_one] at H, simp only [rpow_int_cast, zpow_one, zpow_neg], end lemma mul_rpow {x y z : ℝ} (h : 0 ≤ x) (h₁ : 0 ≤ y) : (x*y)^z = x^z * y^z := begin iterate 3 { rw real.rpow_def_of_nonneg }, split_ifs; simp * at *, { have hx : 0 < x, { cases lt_or_eq_of_le h with h₂ h₂, { exact h₂ }, exfalso, apply h_2, exact eq.symm h₂ }, have hy : 0 < y, { cases lt_or_eq_of_le h₁ with h₂ h₂, { exact h₂ }, exfalso, apply h_3, exact eq.symm h₂ }, rw [log_mul (ne_of_gt hx) (ne_of_gt hy), add_mul, exp_add]}, { exact h₁ }, { exact h }, { exact mul_nonneg h h₁ }, end lemma inv_rpow (hx : 0 ≤ x) (y : ℝ) : (x⁻¹)^y = (x^y)⁻¹ := by simp only [← rpow_neg_one, ← rpow_mul hx, mul_comm] lemma div_rpow (hx : 0 ≤ x) (hy : 0 ≤ y) (z : ℝ) : (x / y) ^ z = x^z / y^z := by simp only [div_eq_mul_inv, mul_rpow hx (inv_nonneg.2 hy), inv_rpow hy] lemma log_rpow {x : ℝ} (hx : 0 < x) (y : ℝ) : log (x^y) = y * (log x) := begin apply exp_injective, rw [exp_log (rpow_pos_of_pos hx y), ← exp_log hx, mul_comm, rpow_def_of_pos (exp_pos (log x)) y], end lemma rpow_lt_rpow (hx : 0 ≤ x) (hxy : x < y) (hz : 0 < z) : x^z < y^z := begin rw le_iff_eq_or_lt at hx, cases hx, { rw [← hx, zero_rpow (ne_of_gt hz)], exact rpow_pos_of_pos (by rwa ← hx at hxy) _ }, rw [rpow_def_of_pos hx, rpow_def_of_pos (lt_trans hx hxy), exp_lt_exp], exact mul_lt_mul_of_pos_right (log_lt_log hx hxy) hz end lemma rpow_le_rpow {x y z: ℝ} (h : 0 ≤ x) (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := begin rcases eq_or_lt_of_le h₁ with rfl|h₁', { refl }, rcases eq_or_lt_of_le h₂ with rfl|h₂', { simp }, exact le_of_lt (rpow_lt_rpow h h₁' h₂') end lemma rpow_lt_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := ⟨lt_imp_lt_of_le_imp_le $ λ h, rpow_le_rpow hy h (le_of_lt hz), λ h, rpow_lt_rpow hx h hz⟩ lemma rpow_le_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := le_iff_le_iff_lt_iff_lt.2 $ rpow_lt_rpow_iff hy hx hz lemma le_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ≤ y ^ z⁻¹ ↔ y ≤ x ^ z := begin have hz' : 0 < -z := by rwa [lt_neg, neg_zero], have hxz : 0 < x ^ (-z) := real.rpow_pos_of_pos hx _, have hyz : 0 < y ^ z⁻¹ := real.rpow_pos_of_pos hy _, rw [←real.rpow_le_rpow_iff hx.le hyz.le hz', ←real.rpow_mul hy.le], simp only [ne_of_lt hz, real.rpow_neg_one, mul_neg, inv_mul_cancel, ne.def, not_false_iff], rw [le_inv hxz hy, ←real.rpow_neg_one, ←real.rpow_mul hx.le], simp, end lemma lt_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x < y ^ z⁻¹ ↔ y < x ^ z := begin have hz' : 0 < -z := by rwa [lt_neg, neg_zero], have hxz : 0 < x ^ (-z) := real.rpow_pos_of_pos hx _, have hyz : 0 < y ^ z⁻¹ := real.rpow_pos_of_pos hy _, rw [←real.rpow_lt_rpow_iff hx.le hyz.le hz', ←real.rpow_mul hy.le], simp only [ne_of_lt hz, real.rpow_neg_one, mul_neg, inv_mul_cancel, ne.def, not_false_iff], rw [lt_inv hxz hy, ←real.rpow_neg_one, ←real.rpow_mul hx.le], simp, end lemma rpow_inv_lt_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ < y ↔ y ^ z < x := begin convert lt_rpow_inv_iff_of_neg (real.rpow_pos_of_pos hx _) (real.rpow_pos_of_pos hy _) hz; simp [←real.rpow_mul hx.le, ←real.rpow_mul hy.le, ne_of_lt hz], end lemma rpow_inv_le_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ ≤ y ↔ y ^ z ≤ x := begin convert le_rpow_inv_iff_of_neg (real.rpow_pos_of_pos hx _) (real.rpow_pos_of_pos hy _) hz; simp [←real.rpow_mul hx.le, ←real.rpow_mul hy.le, ne_of_lt hz], end lemma rpow_lt_rpow_of_exponent_lt (hx : 1 < x) (hyz : y < z) : x^y < x^z := begin repeat {rw [rpow_def_of_pos (lt_trans zero_lt_one hx)]}, rw exp_lt_exp, exact mul_lt_mul_of_pos_left hyz (log_pos hx), end lemma rpow_le_rpow_of_exponent_le (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos (lt_of_lt_of_le zero_lt_one hx)]}, rw exp_le_exp, exact mul_le_mul_of_nonneg_left hyz (log_nonneg hx), end @[simp] lemma rpow_le_rpow_left_iff (hx : 1 < x) : x ^ y ≤ x ^ z ↔ y ≤ z := begin have x_pos : 0 < x := lt_trans zero_lt_one hx, rw [←log_le_log (rpow_pos_of_pos x_pos y) (rpow_pos_of_pos x_pos z), log_rpow x_pos, log_rpow x_pos, mul_le_mul_right (log_pos hx)], end @[simp] lemma rpow_lt_rpow_left_iff (hx : 1 < x) : x ^ y < x ^ z ↔ y < z := by rw [lt_iff_not_le, rpow_le_rpow_left_iff hx, lt_iff_not_le] lemma rpow_lt_rpow_of_exponent_gt (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_lt_exp, exact mul_lt_mul_of_neg_left hyz (log_neg hx0 hx1), end lemma rpow_le_rpow_of_exponent_ge (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_le_exp, exact mul_le_mul_of_nonpos_left hyz (log_nonpos (le_of_lt hx0) hx1), end @[simp] lemma rpow_le_rpow_left_iff_of_base_lt_one (hx0 : 0 < x) (hx1 : x < 1) : x ^ y ≤ x ^ z ↔ z ≤ y := begin rw [←log_le_log (rpow_pos_of_pos hx0 y) (rpow_pos_of_pos hx0 z), log_rpow hx0, log_rpow hx0, mul_le_mul_right_of_neg (log_neg hx0 hx1)], end @[simp] lemma rpow_lt_rpow_left_iff_of_base_lt_one (hx0 : 0 < x) (hx1 : x < 1) : x ^ y < x ^ z ↔ z < y := by rw [lt_iff_not_le, rpow_le_rpow_left_iff_of_base_lt_one hx0 hx1, lt_iff_not_le] lemma rpow_lt_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x < 1) (hz : 0 < z) : x^z < 1 := by { rw ← one_rpow z, exact rpow_lt_rpow hx1 hx2 hz } lemma rpow_le_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := by { rw ← one_rpow z, exact rpow_le_rpow hx1 hx2 hz } lemma rpow_lt_one_of_one_lt_of_neg {x z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := by { convert rpow_lt_rpow_of_exponent_lt hx hz, exact (rpow_zero x).symm } lemma rpow_le_one_of_one_le_of_nonpos {x z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := by { convert rpow_le_rpow_of_exponent_le hx hz, exact (rpow_zero x).symm } lemma one_lt_rpow {x z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := by { rw ← one_rpow z, exact rpow_lt_rpow zero_le_one hx hz } lemma one_le_rpow {x z : ℝ} (hx : 1 ≤ x) (hz : 0 ≤ z) : 1 ≤ x^z := by { rw ← one_rpow z, exact rpow_le_rpow zero_le_one hx hz } lemma one_lt_rpow_of_pos_of_lt_one_of_neg (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := by { convert rpow_lt_rpow_of_exponent_gt hx1 hx2 hz, exact (rpow_zero x).symm } lemma one_le_rpow_of_pos_of_le_one_of_nonpos (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := by { convert rpow_le_rpow_of_exponent_ge hx1 hx2 hz, exact (rpow_zero x).symm } lemma rpow_lt_one_iff_of_pos (hx : 0 < x) : x ^ y < 1 ↔ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := by rw [rpow_def_of_pos hx, exp_lt_one_iff, mul_neg_iff, log_pos_iff hx, log_neg_iff hx] lemma rpow_lt_one_iff (hx : 0 ≤ x) : x ^ y < 1 ↔ x = 0 ∧ y ≠ 0 ∨ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := begin rcases hx.eq_or_lt with (rfl|hx), { rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, zero_lt_one] }, { simp [rpow_lt_one_iff_of_pos hx, hx.ne.symm] } end lemma one_lt_rpow_iff_of_pos (hx : 0 < x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ x < 1 ∧ y < 0 := by rw [rpow_def_of_pos hx, one_lt_exp_iff, mul_pos_iff, log_pos_iff hx, log_neg_iff hx] lemma one_lt_rpow_iff (hx : 0 ≤ x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ 0 < x ∧ x < 1 ∧ y < 0 := begin rcases hx.eq_or_lt with (rfl|hx), { rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, (zero_lt_one' ℝ).not_lt] }, { simp [one_lt_rpow_iff_of_pos hx, hx] } end lemma rpow_le_rpow_of_exponent_ge' (hx0 : 0 ≤ x) (hx1 : x ≤ 1) (hz : 0 ≤ z) (hyz : z ≤ y) : x^y ≤ x^z := begin rcases eq_or_lt_of_le hx0 with rfl | hx0', { rcases eq_or_lt_of_le hz with rfl | hz', { exact (rpow_zero 0).symm ▸ (rpow_le_one hx0 hx1 hyz), }, rw [zero_rpow, zero_rpow]; linarith, }, { exact rpow_le_rpow_of_exponent_ge hx0' hx1 hyz, }, end lemma rpow_left_inj_on {x : ℝ} (hx : x ≠ 0) : inj_on (λ y : ℝ, y^x) {y : ℝ | 0 ≤ y} := begin rintros y hy z hz (hyz : y ^ x = z ^ x), rw [←rpow_one y, ←rpow_one z, ←_root_.mul_inv_cancel hx, rpow_mul hy, rpow_mul hz, hyz] end lemma le_rpow_iff_log_le (hx : 0 < x) (hy : 0 < y) : x ≤ y^z ↔ real.log x ≤ z * real.log y := by rw [←real.log_le_log hx (real.rpow_pos_of_pos hy z), real.log_rpow hy] lemma le_rpow_of_log_le (hx : 0 ≤ x) (hy : 0 < y) (h : real.log x ≤ z * real.log y) : x ≤ y^z := begin obtain hx | rfl := hx.lt_or_eq, { exact (le_rpow_iff_log_le hx hy).2 h }, exact (real.rpow_pos_of_pos hy z).le, end lemma lt_rpow_iff_log_lt (hx : 0 < x) (hy : 0 < y) : x < y^z ↔ real.log x < z * real.log y := by rw [←real.log_lt_log_iff hx (real.rpow_pos_of_pos hy z), real.log_rpow hy] lemma lt_rpow_of_log_lt (hx : 0 ≤ x) (hy : 0 < y) (h : real.log x < z * real.log y) : x < y^z := begin obtain hx | rfl := hx.lt_or_eq, { exact (lt_rpow_iff_log_lt hx hy).2 h }, exact real.rpow_pos_of_pos hy z, end lemma rpow_le_one_iff_of_pos (hx : 0 < x) : x ^ y ≤ 1 ↔ 1 ≤ x ∧ y ≤ 0 ∨ x ≤ 1 ∧ 0 ≤ y := by rw [rpow_def_of_pos hx, exp_le_one_iff, mul_nonpos_iff, log_nonneg_iff hx, log_nonpos_iff hx] /-- Bound for `|log x * x ^ t|` in the interval `(0, 1]`, for positive real `t`. -/ lemma abs_log_mul_self_rpow_lt (x t : ℝ) (h1 : 0 < x) (h2 : x ≤ 1) (ht : 0 < t) : |log x * x ^ t| < 1 / t := begin rw lt_div_iff ht, have := abs_log_mul_self_lt (x ^ t) (rpow_pos_of_pos h1 t) (rpow_le_one h1.le h2 ht.le), rwa [log_rpow h1, mul_assoc, abs_mul, abs_of_pos ht, mul_comm] at this end lemma pow_nat_rpow_nat_inv {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : n ≠ 0) : (x ^ n) ^ (n⁻¹ : ℝ) = x := have hn0 : (n : ℝ) ≠ 0, from nat.cast_ne_zero.2 hn, by rw [← rpow_nat_cast, ← rpow_mul hx, mul_inv_cancel hn0, rpow_one] lemma rpow_nat_inv_pow_nat {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : n ≠ 0) : (x ^ (n⁻¹ : ℝ)) ^ n = x := have hn0 : (n : ℝ) ≠ 0, from nat.cast_ne_zero.2 hn, by rw [← rpow_nat_cast, ← rpow_mul hx, inv_mul_cancel hn0, rpow_one] lemma continuous_at_const_rpow {a b : ℝ} (h : a ≠ 0) : continuous_at (rpow a) b := begin have : rpow a = λ x : ℝ, ((a : ℂ) ^ (x : ℂ)).re, by { ext1 x, rw [rpow_eq_pow, rpow_def], }, rw this, refine complex.continuous_re.continuous_at.comp _, refine (continuous_at_const_cpow _).comp complex.continuous_of_real.continuous_at, norm_cast, exact h, end lemma continuous_at_const_rpow' {a b : ℝ} (h : b ≠ 0) : continuous_at (rpow a) b := begin have : rpow a = λ x : ℝ, ((a : ℂ) ^ (x : ℂ)).re, by { ext1 x, rw [rpow_eq_pow, rpow_def], }, rw this, refine complex.continuous_re.continuous_at.comp _, refine (continuous_at_const_cpow' _).comp complex.continuous_of_real.continuous_at, norm_cast, exact h, end lemma rpow_eq_nhds_of_neg {p : ℝ × ℝ} (hp_fst : p.fst < 0) : (λ x : ℝ × ℝ, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) * cos (x.2 * π) := begin suffices : ∀ᶠ (x : ℝ × ℝ) in (𝓝 p), x.1 < 0, from this.mono (λ x hx, by { dsimp only, rw rpow_def_of_neg hx, }), exact is_open.eventually_mem (is_open_lt continuous_fst continuous_const) hp_fst, end lemma rpow_eq_nhds_of_pos {p : ℝ × ℝ} (hp_fst : 0 < p.fst) : (λ x : ℝ × ℝ, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) := begin suffices : ∀ᶠ (x : ℝ × ℝ) in (𝓝 p), 0 < x.1, from this.mono (λ x hx, by { dsimp only, rw rpow_def_of_pos hx, }), exact is_open.eventually_mem (is_open_lt continuous_const continuous_fst) hp_fst, end lemma continuous_at_rpow_of_ne (p : ℝ × ℝ) (hp : p.1 ≠ 0) : continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p := begin rw ne_iff_lt_or_gt at hp, cases hp, { rw continuous_at_congr (rpow_eq_nhds_of_neg hp), refine continuous_at.mul _ (continuous_cos.continuous_at.comp _), { refine continuous_exp.continuous_at.comp (continuous_at.mul _ continuous_snd.continuous_at), refine (continuous_at_log _).comp continuous_fst.continuous_at, exact hp.ne, }, { exact continuous_snd.continuous_at.mul continuous_at_const, }, }, { rw continuous_at_congr (rpow_eq_nhds_of_pos hp), refine continuous_exp.continuous_at.comp (continuous_at.mul _ continuous_snd.continuous_at), refine (continuous_at_log _).comp continuous_fst.continuous_at, exact hp.lt.ne.symm, }, end lemma continuous_at_rpow_of_pos (p : ℝ × ℝ) (hp : 0 < p.2) : continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p := begin cases p with x y, obtain hx|rfl := ne_or_eq x 0, { exact continuous_at_rpow_of_ne (x, y) hx }, have A : tendsto (λ p : ℝ × ℝ, exp (log p.1 * p.2)) (𝓝[≠] 0 ×ᶠ 𝓝 y) (𝓝 0) := tendsto_exp_at_bot.comp ((tendsto_log_nhds_within_zero.comp tendsto_fst).at_bot_mul hp tendsto_snd), have B : tendsto (λ p : ℝ × ℝ, p.1 ^ p.2) (𝓝[≠] 0 ×ᶠ 𝓝 y) (𝓝 0) := squeeze_zero_norm (λ p, abs_rpow_le_exp_log_mul p.1 p.2) A, have C : tendsto (λ p : ℝ × ℝ, p.1 ^ p.2) (𝓝[{0}] 0 ×ᶠ 𝓝 y) (pure 0), { rw [nhds_within_singleton, tendsto_pure, pure_prod, eventually_map], exact (lt_mem_nhds hp).mono (λ y hy, zero_rpow hy.ne') }, simpa only [← sup_prod, ← nhds_within_union, compl_union_self, nhds_within_univ, nhds_prod_eq, continuous_at, zero_rpow hp.ne'] using B.sup (C.mono_right (pure_le_nhds _)) end lemma continuous_at_rpow (p : ℝ × ℝ) (h : p.1 ≠ 0 ∨ 0 < p.2) : continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p := h.elim (λ h, continuous_at_rpow_of_ne p h) (λ h, continuous_at_rpow_of_pos p h) lemma continuous_at_rpow_const (x : ℝ) (q : ℝ) (h : x ≠ 0 ∨ 0 < q) : continuous_at (λ (x : ℝ), x ^ q) x := begin change continuous_at ((λ p : ℝ × ℝ, p.1 ^ p.2) ∘ (λ y : ℝ, (y, q))) x, apply continuous_at.comp, { exact continuous_at_rpow (x, q) h }, { exact (continuous_id'.prod_mk continuous_const).continuous_at } end end real section variable {α : Type*} lemma filter.tendsto.rpow {l : filter α} {f g : α → ℝ} {x y : ℝ} (hf : tendsto f l (𝓝 x)) (hg : tendsto g l (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) : tendsto (λ t, f t ^ g t) l (𝓝 (x ^ y)) := (real.continuous_at_rpow (x, y) h).tendsto.comp (hf.prod_mk_nhds hg) lemma filter.tendsto.rpow_const {l : filter α} {f : α → ℝ} {x p : ℝ} (hf : tendsto f l (𝓝 x)) (h : x ≠ 0 ∨ 0 ≤ p) : tendsto (λ a, f a ^ p) l (𝓝 (x ^ p)) := if h0 : 0 = p then h0 ▸ by simp [tendsto_const_nhds] else hf.rpow tendsto_const_nhds (h.imp id $ λ h', h'.lt_of_ne h0) variables [topological_space α] {f g : α → ℝ} {s : set α} {x : α} {p : ℝ} lemma continuous_at.rpow (hf : continuous_at f x) (hg : continuous_at g x) (h : f x ≠ 0 ∨ 0 < g x) : continuous_at (λ t, f t ^ g t) x := hf.rpow hg h lemma continuous_within_at.rpow (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) (h : f x ≠ 0 ∨ 0 < g x) : continuous_within_at (λ t, f t ^ g t) s x := hf.rpow hg h lemma continuous_on.rpow (hf : continuous_on f s) (hg : continuous_on g s) (h : ∀ x ∈ s, f x ≠ 0 ∨ 0 < g x) : continuous_on (λ t, f t ^ g t) s := λ t ht, (hf t ht).rpow (hg t ht) (h t ht) lemma continuous.rpow (hf : continuous f) (hg : continuous g) (h : ∀ x, f x ≠ 0 ∨ 0 < g x) : continuous (λ x, f x ^ g x) := continuous_iff_continuous_at.2 $ λ x, (hf.continuous_at.rpow hg.continuous_at (h x)) lemma continuous_within_at.rpow_const (hf : continuous_within_at f s x) (h : f x ≠ 0 ∨ 0 ≤ p) : continuous_within_at (λ x, f x ^ p) s x := hf.rpow_const h lemma continuous_at.rpow_const (hf : continuous_at f x) (h : f x ≠ 0 ∨ 0 ≤ p) : continuous_at (λ x, f x ^ p) x := hf.rpow_const h lemma continuous_on.rpow_const (hf : continuous_on f s) (h : ∀ x ∈ s, f x ≠ 0 ∨ 0 ≤ p) : continuous_on (λ x, f x ^ p) s := λ x hx, (hf x hx).rpow_const (h x hx) lemma continuous.rpow_const (hf : continuous f) (h : ∀ x, f x ≠ 0 ∨ 0 ≤ p) : continuous (λ x, f x ^ p) := continuous_iff_continuous_at.2 $ λ x, hf.continuous_at.rpow_const (h x) end namespace real variables {z x y : ℝ} section sqrt lemma sqrt_eq_rpow (x : ℝ) : sqrt x = x ^ (1/(2:ℝ)) := begin obtain h | h := le_or_lt 0 x, { rw [← mul_self_inj_of_nonneg (sqrt_nonneg _) (rpow_nonneg_of_nonneg h _), mul_self_sqrt h, ← sq, ← rpow_nat_cast, ← rpow_mul h], norm_num }, { have : 1 / (2:ℝ) * π = π / (2:ℝ), ring, rw [sqrt_eq_zero_of_nonpos h.le, rpow_def_of_neg h, this, cos_pi_div_two, mul_zero] } end lemma rpow_div_two_eq_sqrt {x : ℝ} (r : ℝ) (hx : 0 ≤ x) : x ^ (r/2) = (sqrt x) ^ r := begin rw [sqrt_eq_rpow, ← rpow_mul hx], congr, ring, end end sqrt end real section limits open real filter /-- The function `x ^ y` tends to `+∞` at `+∞` for any positive real `y`. -/ lemma tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ x : ℝ, x ^ y) at_top at_top := begin rw tendsto_at_top_at_top, intro b, use (max b 0) ^ (1/y), intros x hx, exact le_of_max_le_left (by { convert rpow_le_rpow (rpow_nonneg_of_nonneg (le_max_right b 0) (1/y)) hx (le_of_lt hy), rw [← rpow_mul (le_max_right b 0), (eq_div_iff (ne_of_gt hy)).mp rfl, rpow_one] }), end /-- The function `x ^ (-y)` tends to `0` at `+∞` for any positive real `y`. -/ lemma tendsto_rpow_neg_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ x : ℝ, x ^ (-y)) at_top (𝓝 0) := tendsto.congr' (eventually_eq_of_mem (Ioi_mem_at_top 0) (λ x hx, (rpow_neg (le_of_lt hx) y).symm)) (tendsto_rpow_at_top hy).inv_tendsto_at_top /-- The function `x ^ (a / (b * x + c))` tends to `1` at `+∞`, for any real numbers `a`, `b`, and `c` such that `b` is nonzero. -/ lemma tendsto_rpow_div_mul_add (a b c : ℝ) (hb : 0 ≠ b) : tendsto (λ x, x ^ (a / (b*x+c))) at_top (𝓝 1) := begin refine tendsto.congr' _ ((tendsto_exp_nhds_0_nhds_1.comp (by simpa only [mul_zero, pow_one] using ((@tendsto_const_nhds _ _ _ a _).mul (tendsto_div_pow_mul_exp_add_at_top b c 1 hb)))).comp tendsto_log_at_top), apply eventually_eq_of_mem (Ioi_mem_at_top (0:ℝ)), intros x hx, simp only [set.mem_Ioi, function.comp_app] at hx ⊢, rw [exp_log hx, ← exp_log (rpow_pos_of_pos hx (a / (b * x + c))), log_rpow hx (a / (b * x + c))], field_simp, end /-- The function `x ^ (1 / x)` tends to `1` at `+∞`. -/ lemma tendsto_rpow_div : tendsto (λ x, x ^ ((1:ℝ) / x)) at_top (𝓝 1) := by { convert tendsto_rpow_div_mul_add (1:ℝ) _ (0:ℝ) zero_ne_one, funext, congr' 2, ring } /-- The function `x ^ (-1 / x)` tends to `1` at `+∞`. -/ lemma tendsto_rpow_neg_div : tendsto (λ x, x ^ (-(1:ℝ) / x)) at_top (𝓝 1) := by { convert tendsto_rpow_div_mul_add (-(1:ℝ)) _ (0:ℝ) zero_ne_one, funext, congr' 2, ring } /-- The function `exp(x) / x ^ s` tends to `+∞` at `+∞`, for any real number `s`. -/ lemma tendsto_exp_div_rpow_at_top (s : ℝ) : tendsto (λ x : ℝ, exp x / x ^ s) at_top at_top := begin cases archimedean_iff_nat_lt.1 (real.archimedean) s with n hn, refine tendsto_at_top_mono' _ _ (tendsto_exp_div_pow_at_top n), filter_upwards [eventually_gt_at_top (0 : ℝ), eventually_ge_at_top (1 : ℝ)] with x hx₀ hx₁, rw [div_le_div_left (exp_pos _) (pow_pos hx₀ _) (rpow_pos_of_pos hx₀ _), ←rpow_nat_cast], exact rpow_le_rpow_of_exponent_le hx₁ hn.le, end /-- The function `exp (b * x) / x ^ s` tends to `+∞` at `+∞`, for any real `s` and `b > 0`. -/ lemma tendsto_exp_mul_div_rpow_at_top (s : ℝ) (b : ℝ) (hb : 0 < b) : tendsto (λ x : ℝ, exp (b * x) / x ^ s) at_top at_top := begin refine ((tendsto_rpow_at_top hb).comp (tendsto_exp_div_rpow_at_top (s / b))).congr' _, filter_upwards [eventually_ge_at_top (0 : ℝ)] with x hx₀, simp [div_rpow, (exp_pos x).le, rpow_nonneg_of_nonneg, ←rpow_mul, ←exp_mul, mul_comm x, hb.ne', *] end /-- The function `x ^ s * exp (-b * x)` tends to `0` at `+∞`, for any real `s` and `b > 0`. -/ lemma tendsto_rpow_mul_exp_neg_mul_at_top_nhds_0 (s : ℝ) (b : ℝ) (hb : 0 < b): tendsto (λ x : ℝ, x ^ s * exp (-b * x)) at_top (𝓝 0) := begin refine (tendsto_exp_mul_div_rpow_at_top s b hb).inv_tendsto_at_top.congr' _, filter_upwards with x using by simp [exp_neg, inv_div, div_eq_mul_inv _ (exp _)] end namespace asymptotics variables {α : Type*} {r c : ℝ} {l : filter α} {f g : α → ℝ} lemma is_O_with.rpow (h : is_O_with c l f g) (hc : 0 ≤ c) (hr : 0 ≤ r) (hg : 0 ≤ᶠ[l] g) : is_O_with (c ^ r) l (λ x, f x ^ r) (λ x, g x ^ r) := begin apply is_O_with.of_bound, filter_upwards [hg, h.bound] with x hgx hx, calc |f x ^ r| ≤ |f x| ^ r : abs_rpow_le_abs_rpow _ _ ... ≤ (c * |g x|) ^ r : rpow_le_rpow (abs_nonneg _) hx hr ... = c ^ r * |g x ^ r| : by rw [mul_rpow hc (abs_nonneg _), abs_rpow_of_nonneg hgx] end lemma is_O.rpow (hr : 0 ≤ r) (hg : 0 ≤ᶠ[l] g) (h : f =O[l] g) : (λ x, f x ^ r) =O[l] (λ x, g x ^ r) := let ⟨c, hc, h'⟩ := h.exists_nonneg in (h'.rpow hc hr hg).is_O lemma is_o.rpow (hr : 0 < r) (hg : 0 ≤ᶠ[l] g) (h : f =o[l] g) : (λ x, f x ^ r) =o[l] (λ x, g x ^ r) := is_o.of_is_O_with $ λ c hc, ((h.forall_is_O_with (rpow_pos_of_pos hc r⁻¹)).rpow (rpow_nonneg_of_nonneg hc.le _) hr.le hg).congr_const (by rw [←rpow_mul hc.le, inv_mul_cancel hr.ne', rpow_one]) end asymptotics open asymptotics /-- `x ^ s = o(exp(b * x))` as `x → ∞` for any real `s` and positive `b`. -/ lemma is_o_rpow_exp_pos_mul_at_top (s : ℝ) {b : ℝ} (hb : 0 < b) : (λ x : ℝ, x ^ s) =o[at_top] (λ x, exp (b * x)) := iff.mpr (is_o_iff_tendsto $ λ x h, absurd h (exp_pos _).ne') $ by simpa only [div_eq_mul_inv, exp_neg, neg_mul] using tendsto_rpow_mul_exp_neg_mul_at_top_nhds_0 s b hb /-- `x ^ k = o(exp(b * x))` as `x → ∞` for any integer `k` and positive `b`. -/ lemma is_o_zpow_exp_pos_mul_at_top (k : ℤ) {b : ℝ} (hb : 0 < b) : (λ x : ℝ, x ^ k) =o[at_top] (λ x, exp (b * x)) := by simpa only [rpow_int_cast] using is_o_rpow_exp_pos_mul_at_top k hb /-- `x ^ k = o(exp(b * x))` as `x → ∞` for any natural `k` and positive `b`. -/ lemma is_o_pow_exp_pos_mul_at_top (k : ℕ) {b : ℝ} (hb : 0 < b) : (λ x : ℝ, x ^ k) =o[at_top] (λ x, exp (b * x)) := by simpa using is_o_zpow_exp_pos_mul_at_top k hb /-- `x ^ s = o(exp x)` as `x → ∞` for any real `s`. -/ lemma is_o_rpow_exp_at_top (s : ℝ) : (λ x : ℝ, x ^ s) =o[at_top] exp := by simpa only [one_mul] using is_o_rpow_exp_pos_mul_at_top s one_pos lemma is_o_log_rpow_at_top {r : ℝ} (hr : 0 < r) : log =o[at_top] (λ x, x ^ r) := calc log =O[at_top] (λ x, r * log x) : is_O_self_const_mul _ hr.ne' _ _ ... =ᶠ[at_top] (λ x, log (x ^ r)) : (eventually_gt_at_top 0).mono $ λ x hx, (log_rpow hx _).symm ... =o[at_top] (λ x, x ^ r) : is_o_log_id_at_top.comp_tendsto (tendsto_rpow_at_top hr) lemma is_o_log_rpow_rpow_at_top {s : ℝ} (r : ℝ) (hs : 0 < s) : (λ x, log x ^ r) =o[at_top] (λ x, x ^ s) := let r' := max r 1 in have hr : 0 < r', from lt_max_iff.2 $ or.inr one_pos, have H : 0 < s / r', from div_pos hs hr, calc (λ x, log x ^ r) =O[at_top] (λ x, log x ^ r') : is_O.of_bound 1 $ (tendsto_log_at_top.eventually_ge_at_top 1).mono $ λ x hx, have hx₀ : 0 ≤ log x, from zero_le_one.trans hx, by simp [norm_eq_abs, abs_rpow_of_nonneg, abs_rpow_of_nonneg hx₀, rpow_le_rpow_of_exponent_le (hx.trans (le_abs_self _))] ... =o[at_top] (λ x, (x ^ (s / r')) ^ r') : (is_o_log_rpow_at_top H).rpow hr $ (tendsto_rpow_at_top H).eventually $ eventually_ge_at_top 0 ... =ᶠ[at_top] (λ x, x ^ s) : (eventually_ge_at_top 0).mono $ λ x hx, by simp only [← rpow_mul hx, div_mul_cancel _ hr.ne'] lemma is_o_abs_log_rpow_rpow_nhds_zero {s : ℝ} (r : ℝ) (hs : s < 0) : (λ x, |log x| ^ r) =o[𝓝[>] 0] (λ x, x ^ s) := ((is_o_log_rpow_rpow_at_top r (neg_pos.2 hs)).comp_tendsto tendsto_inv_zero_at_top).congr' (mem_of_superset (Icc_mem_nhds_within_Ioi $ set.left_mem_Ico.2 one_pos) $ λ x hx, by simp [abs_of_nonpos, log_nonpos hx.1 hx.2]) (eventually_mem_nhds_within.mono $ λ x hx, by rw [function.comp_app, inv_rpow hx.out.le, rpow_neg hx.out.le, inv_inv]) lemma is_o_log_rpow_nhds_zero {r : ℝ} (hr : r < 0) : log =o[𝓝[>] 0] (λ x, x ^ r) := (is_o_abs_log_rpow_rpow_nhds_zero 1 hr).neg_left.congr' (mem_of_superset (Icc_mem_nhds_within_Ioi $ set.left_mem_Ico.2 one_pos) $ λ x hx, by simp [abs_of_nonpos (log_nonpos hx.1 hx.2)]) eventually_eq.rfl lemma tendsto_log_div_rpow_nhds_zero {r : ℝ} (hr : r < 0) : tendsto (λ x, log x / x ^ r) (𝓝[>] 0) (𝓝 0) := (is_o_log_rpow_nhds_zero hr).tendsto_div_nhds_zero lemma tendsto_log_mul_rpow_nhds_zero {r : ℝ} (hr : 0 < r) : tendsto (λ x, log x * x ^ r) (𝓝[>] 0) (𝓝 0) := (tendsto_log_div_rpow_nhds_zero $ neg_lt_zero.2 hr).congr' $ eventually_mem_nhds_within.mono $ λ x hx, by rw [rpow_neg hx.out.le, div_inv_eq_mul] end limits namespace complex /-- See also `complex.continuous_at_cpow` and `complex.continuous_at_cpow_of_re_pos`. -/ lemma continuous_at_cpow_zero_of_re_pos {z : ℂ} (hz : 0 < z.re) : continuous_at (λ x : ℂ × ℂ, x.1 ^ x.2) (0, z) := begin have hz₀ : z ≠ 0, from ne_of_apply_ne re hz.ne', rw [continuous_at, zero_cpow hz₀, tendsto_zero_iff_norm_tendsto_zero], refine squeeze_zero (λ _, norm_nonneg _) (λ _, abs_cpow_le _ _) _, simp only [div_eq_mul_inv, ← real.exp_neg], refine tendsto.zero_mul_is_bounded_under_le _ _, { convert (continuous_fst.norm.tendsto _).rpow ((continuous_re.comp continuous_snd).tendsto _) _; simp [hz, real.zero_rpow hz.ne'] }, { simp only [(∘), real.norm_eq_abs, abs_of_pos (real.exp_pos _)], rcases exists_gt (|im z|) with ⟨C, hC⟩, refine ⟨real.exp (π * C), eventually_map.2 _⟩, refine (((continuous_im.comp continuous_snd).abs.tendsto (_, z)).eventually (gt_mem_nhds hC)).mono (λ z hz, real.exp_le_exp.2 $ (neg_le_abs_self _).trans _), rw _root_.abs_mul, exact mul_le_mul (abs_le.2 ⟨(neg_pi_lt_arg _).le, arg_le_pi _⟩) hz.le (_root_.abs_nonneg _) real.pi_pos.le } end /-- See also `complex.continuous_at_cpow` for a version that assumes `p.1 ≠ 0` but makes no assumptions about `p.2`. -/ lemma continuous_at_cpow_of_re_pos {p : ℂ × ℂ} (h₁ : 0 ≤ p.1.re ∨ p.1.im ≠ 0) (h₂ : 0 < p.2.re) : continuous_at (λ x : ℂ × ℂ, x.1 ^ x.2) p := begin cases p with z w, rw [← not_lt_zero_iff, lt_iff_le_and_ne, not_and_distrib, ne.def, not_not, not_le_zero_iff] at h₁, rcases h₁ with h₁|(rfl : z = 0), exacts [continuous_at_cpow h₁, continuous_at_cpow_zero_of_re_pos h₂] end /-- See also `complex.continuous_at_cpow_const` for a version that assumes `z ≠ 0` but makes no assumptions about `w`. -/ lemma continuous_at_cpow_const_of_re_pos {z w : ℂ} (hz : 0 ≤ re z ∨ im z ≠ 0) (hw : 0 < re w) : continuous_at (λ x, x ^ w) z := tendsto.comp (@continuous_at_cpow_of_re_pos (z, w) hz hw) (continuous_at_id.prod continuous_at_const) lemma continuous_of_real_cpow_const {y : ℂ} (hs : 0 < y.re) : continuous (λ x, x ^ y : ℝ → ℂ) := begin rw continuous_iff_continuous_at, intro x, cases le_or_lt 0 x with hx hx, { refine (continuous_at_cpow_const_of_re_pos _ hs).comp continuous_of_real.continuous_at, exact or.inl hx }, { suffices : continuous_on (λ x, x ^ y : ℝ → ℂ) (set.Iio 0), from continuous_on.continuous_at this (Iio_mem_nhds hx), have : eq_on (λ x, x ^ y : ℝ → ℂ) (λ x, ((-x) : ℂ) ^ y * exp (π * I * y)) (set.Iio 0), from λ y hy, of_real_cpow_of_nonpos (le_of_lt hy) _, refine (continuous_on.mul (λ y hy, _) continuous_on_const).congr this, refine continuous_of_real.continuous_within_at.neg.cpow continuous_within_at_const _, left, simpa using hy } end end complex namespace nnreal /-- The nonnegative real power function `x^y`, defined for `x : ℝ≥0` and `y : ℝ ` as the restriction of the real power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0 = 1` and `0 ^ y = 0` for `y ≠ 0`. -/ noncomputable def rpow (x : ℝ≥0) (y : ℝ) : ℝ≥0 := ⟨(x : ℝ) ^ y, real.rpow_nonneg_of_nonneg x.2 y⟩ noncomputable instance : has_pow ℝ≥0 ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ℝ≥0) (y : ℝ) : rpow x y = x ^ y := rfl @[simp, norm_cast] lemma coe_rpow (x : ℝ≥0) (y : ℝ) : ((x ^ y : ℝ≥0) : ℝ) = (x : ℝ) ^ y := rfl @[simp] lemma rpow_zero (x : ℝ≥0) : x ^ (0 : ℝ) = 1 := nnreal.eq $ real.rpow_zero _ @[simp] lemma rpow_eq_zero_iff {x : ℝ≥0} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := begin rw [← nnreal.coe_eq, coe_rpow, ← nnreal.coe_eq_zero], exact real.rpow_eq_zero_iff_of_nonneg x.2 end @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ≥0) ^ x = 0 := nnreal.eq $ real.zero_rpow h @[simp] lemma rpow_one (x : ℝ≥0) : x ^ (1 : ℝ) = x := nnreal.eq $ real.rpow_one _ @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0) ^ x = 1 := nnreal.eq $ real.one_rpow _ lemma rpow_add {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add (pos_iff_ne_zero.2 hx) _ _ lemma rpow_add' (x : ℝ≥0) {y z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add' x.2 h lemma rpow_mul (x : ℝ≥0) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := nnreal.eq $ real.rpow_mul x.2 y z lemma rpow_neg (x : ℝ≥0) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := nnreal.eq $ real.rpow_neg x.2 _ lemma rpow_neg_one (x : ℝ≥0) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_sub {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub (pos_iff_ne_zero.2 hx) y z lemma rpow_sub' (x : ℝ≥0) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub' x.2 h lemma rpow_inv_rpow_self {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ y) ^ (1 / y) = x := by field_simp [← rpow_mul] lemma rpow_self_rpow_inv {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ (1 / y)) ^ y = x := by field_simp [← rpow_mul] lemma inv_rpow (x : ℝ≥0) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ := nnreal.eq $ real.inv_rpow x.2 y lemma div_rpow (x y : ℝ≥0) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z := nnreal.eq $ real.div_rpow x.2 y.2 z lemma sqrt_eq_rpow (x : ℝ≥0) : sqrt x = x ^ (1/(2:ℝ)) := begin refine nnreal.eq _, push_cast, exact real.sqrt_eq_rpow x.1, end @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0) (n : ℕ) : x ^ (n : ℝ) = x ^ n := nnreal.eq $ by simpa only [coe_rpow, coe_pow] using real.rpow_nat_cast x n @[simp] lemma rpow_two (x : ℝ≥0) : x ^ (2 : ℝ) = x ^ 2 := by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] } lemma mul_rpow {x y : ℝ≥0} {z : ℝ} : (x*y)^z = x^z * y^z := nnreal.eq $ real.mul_rpow x.2 y.2 lemma rpow_le_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := real.rpow_le_rpow x.2 h₁ h₂ lemma rpow_lt_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := real.rpow_lt_rpow x.2 h₁ h₂ lemma rpow_lt_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := real.rpow_lt_rpow_iff x.2 y.2 hz lemma rpow_le_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := real.rpow_le_rpow_iff x.2 y.2 hz lemma le_rpow_one_div_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ≤ y ^ (1 / z) ↔ x ^ z ≤ y := by rw [← rpow_le_rpow_iff hz, rpow_self_rpow_inv hz.ne'] lemma rpow_one_div_le_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ (1 / z) ≤ y ↔ x ≤ y ^ z := by rw [← rpow_le_rpow_iff hz, rpow_self_rpow_inv hz.ne'] lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0} {y z : ℝ} (hx : 1 < x) (hyz : y < z) : x^y < x^z := real.rpow_lt_rpow_of_exponent_lt hx hyz lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_le hx hyz lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := real.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_ge hx0 hx1 hyz lemma rpow_pos {p : ℝ} {x : ℝ≥0} (hx_pos : 0 < x) : 0 < x^p := begin have rpow_pos_of_nonneg : ∀ {p : ℝ}, 0 < p → 0 < x^p, { intros p hp_pos, rw ←zero_rpow hp_pos.ne', exact rpow_lt_rpow hx_pos hp_pos }, rcases lt_trichotomy 0 p with hp_pos|rfl|hp_neg, { exact rpow_pos_of_nonneg hp_pos }, { simp only [zero_lt_one, rpow_zero] }, { rw [←neg_neg p, rpow_neg, inv_pos], exact rpow_pos_of_nonneg (neg_pos.mpr hp_neg) }, end lemma rpow_lt_one {x : ℝ≥0} {z : ℝ} (hx1 : x < 1) (hz : 0 < z) : x^z < 1 := real.rpow_lt_one (coe_nonneg x) hx1 hz lemma rpow_le_one {x : ℝ≥0} {z : ℝ} (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := real.rpow_le_one x.2 hx2 hz lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := real.rpow_lt_one_of_one_lt_of_neg hx hz lemma rpow_le_one_of_one_le_of_nonpos {x : ℝ≥0} {z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := real.rpow_le_one_of_one_le_of_nonpos hx hz lemma one_lt_rpow {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := real.one_lt_rpow hx hz lemma one_le_rpow {x : ℝ≥0} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z := real.one_le_rpow h h₁ lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := real.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz lemma one_le_rpow_of_pos_of_le_one_of_nonpos {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := real.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 hz lemma rpow_le_self_of_le_one {x : ℝ≥0} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x := begin rcases eq_bot_or_bot_lt x with rfl | (h : 0 < x), { have : z ≠ 0 := by linarith, simp [this] }, nth_rewrite 1 ←nnreal.rpow_one x, exact nnreal.rpow_le_rpow_of_exponent_ge h hx h_one_le, end lemma rpow_left_injective {x : ℝ} (hx : x ≠ 0) : function.injective (λ y : ℝ≥0, y^x) := λ y z hyz, by simpa only [rpow_inv_rpow_self hx] using congr_arg (λ y, y ^ (1 / x)) hyz lemma rpow_eq_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ z = y ^ z ↔ x = y := (rpow_left_injective hz).eq_iff lemma rpow_left_surjective {x : ℝ} (hx : x ≠ 0) : function.surjective (λ y : ℝ≥0, y^x) := λ y, ⟨y ^ x⁻¹, by simp_rw [←rpow_mul, _root_.inv_mul_cancel hx, rpow_one]⟩ lemma rpow_left_bijective {x : ℝ} (hx : x ≠ 0) : function.bijective (λ y : ℝ≥0, y^x) := ⟨rpow_left_injective hx, rpow_left_surjective hx⟩ lemma eq_rpow_one_div_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x = y ^ (1 / z) ↔ x ^ z = y := by rw [← rpow_eq_rpow_iff hz, rpow_self_rpow_inv hz] lemma rpow_one_div_eq_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ (1 / z) = y ↔ x = y ^ z := by rw [← rpow_eq_rpow_iff hz, rpow_self_rpow_inv hz] lemma pow_nat_rpow_nat_inv (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) : (x ^ n) ^ (n⁻¹ : ℝ) = x := by { rw [← nnreal.coe_eq, coe_rpow, nnreal.coe_pow], exact real.pow_nat_rpow_nat_inv x.2 hn } lemma rpow_nat_inv_pow_nat (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) : (x ^ (n⁻¹ : ℝ)) ^ n = x := by { rw [← nnreal.coe_eq, nnreal.coe_pow, coe_rpow], exact real.rpow_nat_inv_pow_nat x.2 hn } lemma continuous_at_rpow {x : ℝ≥0} {y : ℝ} (h : x ≠ 0 ∨ 0 < y) : continuous_at (λp:ℝ≥0×ℝ, p.1^p.2) (x, y) := begin have : (λp:ℝ≥0×ℝ, p.1^p.2) = real.to_nnreal ∘ (λp:ℝ×ℝ, p.1^p.2) ∘ (λp:ℝ≥0 × ℝ, (p.1.1, p.2)), { ext p, rw [coe_rpow, real.coe_to_nnreal _ (real.rpow_nonneg_of_nonneg p.1.2 _)], refl }, rw this, refine continuous_real_to_nnreal.continuous_at.comp (continuous_at.comp _ _), { apply real.continuous_at_rpow, simp only [ne.def] at h, rw ← (nnreal.coe_eq_zero x) at h, exact h }, { exact ((continuous_subtype_val.comp continuous_fst).prod_mk continuous_snd).continuous_at } end lemma _root_.real.to_nnreal_rpow_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : real.to_nnreal (x ^ y) = (real.to_nnreal x) ^ y := begin nth_rewrite 0 ← real.coe_to_nnreal x hx, rw [←nnreal.coe_rpow, real.to_nnreal_coe], end lemma eventually_pow_one_div_le (x : ℝ≥0) {y : ℝ≥0} (hy : 1 < y) : ∀ᶠ (n : ℕ) in at_top, x ^ (1 / n : ℝ) ≤ y := begin obtain ⟨m, hm⟩ := add_one_pow_unbounded_of_pos x (tsub_pos_of_lt hy), rw [tsub_add_cancel_of_le hy.le] at hm, refine eventually_at_top.2 ⟨m + 1, λ n hn, _⟩, simpa only [nnreal.rpow_one_div_le_iff (nat.cast_pos.2 $ m.succ_pos.trans_le hn), nnreal.rpow_nat_cast] using hm.le.trans (pow_le_pow hy.le (m.le_succ.trans hn)), end end nnreal namespace real variables {n : ℕ} lemma exists_rat_pow_btwn_rat_aux (hn : n ≠ 0) (x y : ℝ) (h : x < y) (hy : 0 < y) : ∃ q : ℚ, 0 < q ∧ x < q^n ∧ ↑q^n < y := begin have hn' : 0 < (n : ℝ) := by exact_mod_cast hn.bot_lt, obtain ⟨q, hxq, hqy⟩ := exists_rat_btwn (rpow_lt_rpow (le_max_left 0 x) (max_lt hy h) $ inv_pos.mpr hn'), have := rpow_nonneg_of_nonneg (le_max_left 0 x) n⁻¹, have hq := this.trans_lt hxq, replace hxq := rpow_lt_rpow this hxq hn', replace hqy := rpow_lt_rpow hq.le hqy hn', rw [rpow_nat_cast, rpow_nat_cast, rpow_nat_inv_pow_nat _ hn] at hxq hqy, exact ⟨q, by exact_mod_cast hq, (le_max_right _ _).trans_lt hxq, hqy⟩, { exact le_max_left _ _ }, { exact hy.le } end lemma exists_rat_pow_btwn_rat (hn : n ≠ 0) {x y : ℚ} (h : x < y) (hy : 0 < y) : ∃ q : ℚ, 0 < q ∧ x < q^n ∧ q^n < y := by apply_mod_cast exists_rat_pow_btwn_rat_aux hn x y; assumption /-- There is a rational power between any two positive elements of an archimedean ordered field. -/ lemma exists_rat_pow_btwn {α : Type*} [linear_ordered_field α] [archimedean α] (hn : n ≠ 0) {x y : α} (h : x < y) (hy : 0 < y) : ∃ q : ℚ, 0 < q ∧ x < q^n ∧ (q^n : α) < y := begin obtain ⟨q₂, hx₂, hy₂⟩ := exists_rat_btwn (max_lt h hy), obtain ⟨q₁, hx₁, hq₁₂⟩ := exists_rat_btwn hx₂, have : (0 : α) < q₂ := (le_max_right _ _).trans_lt hx₂, norm_cast at hq₁₂ this, obtain ⟨q, hq, hq₁, hq₂⟩ := exists_rat_pow_btwn_rat hn hq₁₂ this, refine ⟨q, hq, (le_max_left _ _).trans_lt $ hx₁.trans _, hy₂.trans' _⟩; assumption_mod_cast, end end real open filter lemma filter.tendsto.nnrpow {α : Type*} {f : filter α} {u : α → ℝ≥0} {v : α → ℝ} {x : ℝ≥0} {y : ℝ} (hx : tendsto u f (𝓝 x)) (hy : tendsto v f (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) : tendsto (λ a, (u a) ^ (v a)) f (𝓝 (x ^ y)) := tendsto.comp (nnreal.continuous_at_rpow h) (hx.prod_mk_nhds hy) namespace nnreal lemma continuous_at_rpow_const {x : ℝ≥0} {y : ℝ} (h : x ≠ 0 ∨ 0 ≤ y) : continuous_at (λ z, z^y) x := h.elim (λ h, tendsto_id.nnrpow tendsto_const_nhds (or.inl h)) $ λ h, h.eq_or_lt.elim (λ h, h ▸ by simp only [rpow_zero, continuous_at_const]) (λ h, tendsto_id.nnrpow tendsto_const_nhds (or.inr h)) lemma continuous_rpow_const {y : ℝ} (h : 0 ≤ y) : continuous (λ x : ℝ≥0, x^y) := continuous_iff_continuous_at.2 $ λ x, continuous_at_rpow_const (or.inr h) theorem tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ (x : ℝ≥0), x ^ y) at_top at_top := begin rw filter.tendsto_at_top_at_top, intros b, obtain ⟨c, hc⟩ := tendsto_at_top_at_top.mp (tendsto_rpow_at_top hy) b, use c.to_nnreal, intros a ha, exact_mod_cast hc a (real.to_nnreal_le_iff_le_coe.mp ha), end end nnreal namespace ennreal /-- The real power function `x^y` on extended nonnegative reals, defined for `x : ℝ≥0∞` and `y : ℝ` as the restriction of the real power function if `0 < x < ⊤`, and with the natural values for `0` and `⊤` (i.e., `0 ^ x = 0` for `x > 0`, `1` for `x = 0` and `⊤` for `x < 0`, and `⊤ ^ x = 1 / 0 ^ x`). -/ noncomputable def rpow : ℝ≥0∞ → ℝ → ℝ≥0∞ | (some x) y := if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) | none y := if 0 < y then ⊤ else if y = 0 then 1 else 0 noncomputable instance : has_pow ℝ≥0∞ ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ℝ≥0∞) (y : ℝ) : rpow x y = x ^ y := rfl @[simp] lemma rpow_zero {x : ℝ≥0∞} : x ^ (0 : ℝ) = 1 := by cases x; { dsimp only [(^), rpow], simp [lt_irrefl] } lemma top_rpow_def (y : ℝ) : (⊤ : ℝ≥0∞) ^ y = if 0 < y then ⊤ else if y = 0 then 1 else 0 := rfl @[simp] lemma top_rpow_of_pos {y : ℝ} (h : 0 < y) : (⊤ : ℝ≥0∞) ^ y = ⊤ := by simp [top_rpow_def, h] @[simp] lemma top_rpow_of_neg {y : ℝ} (h : y < 0) : (⊤ : ℝ≥0∞) ^ y = 0 := by simp [top_rpow_def, asymm h, ne_of_lt h] @[simp] lemma zero_rpow_of_pos {y : ℝ} (h : 0 < y) : (0 : ℝ≥0∞) ^ y = 0 := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, asymm h, ne_of_gt h], end @[simp] lemma zero_rpow_of_neg {y : ℝ} (h : y < 0) : (0 : ℝ≥0∞) ^ y = ⊤ := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, ne_of_gt h], end lemma zero_rpow_def (y : ℝ) : (0 : ℝ≥0∞) ^ y = if 0 < y then 0 else if y = 0 then 1 else ⊤ := begin rcases lt_trichotomy 0 y with H|rfl|H, { simp [H, ne_of_gt, zero_rpow_of_pos, lt_irrefl] }, { simp [lt_irrefl] }, { simp [H, asymm H, ne_of_lt, zero_rpow_of_neg] } end @[simp] lemma zero_rpow_mul_self (y : ℝ) : (0 : ℝ≥0∞) ^ y * 0 ^ y = 0 ^ y := by { rw zero_rpow_def, split_ifs, exacts [zero_mul _, one_mul _, top_mul_top] } @[norm_cast] lemma coe_rpow_of_ne_zero {x : ℝ≥0} (h : x ≠ 0) (y : ℝ) : (x : ℝ≥0∞) ^ y = (x ^ y : ℝ≥0) := begin rw [← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h] end @[norm_cast] lemma coe_rpow_of_nonneg (x : ℝ≥0) {y : ℝ} (h : 0 ≤ y) : (x : ℝ≥0∞) ^ y = (x ^ y : ℝ≥0) := begin by_cases hx : x = 0, { rcases le_iff_eq_or_lt.1 h with H|H, { simp [hx, H.symm] }, { simp [hx, zero_rpow_of_pos H, nnreal.zero_rpow (ne_of_gt H)] } }, { exact coe_rpow_of_ne_zero hx _ } end lemma coe_rpow_def (x : ℝ≥0) (y : ℝ) : (x : ℝ≥0∞) ^ y = if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) := rfl @[simp] lemma rpow_one (x : ℝ≥0∞) : x ^ (1 : ℝ) = x := begin cases x, { exact dif_pos zero_lt_one }, { change ite _ _ _ = _, simp only [nnreal.rpow_one, some_eq_coe, ite_eq_right_iff, top_ne_coe, and_imp], exact λ _, zero_le_one.not_lt } end @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0∞) ^ x = 1 := by { rw [← coe_one, coe_rpow_of_ne_zero one_ne_zero], simp } @[simp] lemma rpow_eq_zero_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = 0 ↔ (x = 0 ∧ 0 < y) ∨ (x = ⊤ ∧ y < 0) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end @[simp] lemma rpow_eq_top_iff {x : ℝ≥0∞} {y : ℝ} : x ^ y = ⊤ ↔ (x = 0 ∧ y < 0) ∨ (x = ⊤ ∧ 0 < y) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end lemma rpow_eq_top_iff_of_pos {x : ℝ≥0∞} {y : ℝ} (hy : 0 < y) : x ^ y = ⊤ ↔ x = ⊤ := by simp [rpow_eq_top_iff, hy, asymm hy] lemma rpow_eq_top_of_nonneg (x : ℝ≥0∞) {y : ℝ} (hy0 : 0 ≤ y) : x ^ y = ⊤ → x = ⊤ := begin rw ennreal.rpow_eq_top_iff, intro h, cases h, { exfalso, rw lt_iff_not_ge at h, exact h.right hy0, }, { exact h.left, }, end lemma rpow_ne_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y ≠ ⊤ := mt (ennreal.rpow_eq_top_of_nonneg x hy0) h lemma rpow_lt_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y < ⊤ := lt_top_iff_ne_top.mpr (ennreal.rpow_ne_top_of_nonneg hy0 h) lemma rpow_add {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y + z) = x ^ y * x ^ z := begin cases x, { exact (h'x rfl).elim }, have : x ≠ 0 := λ h, by simpa [h] using hx, simp [coe_rpow_of_ne_zero this, nnreal.rpow_add this] end lemma rpow_neg (x : ℝ≥0∞) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [top_rpow_of_pos, top_rpow_of_neg, H, neg_pos.mpr] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, zero_rpow_of_pos, zero_rpow_of_neg, H, neg_pos.mpr] }, { have A : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, ← coe_inv A, nnreal.rpow_neg] } } end lemma rpow_sub {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y - z) = x ^ y / x ^ z := by rw [sub_eq_add_neg, rpow_add _ _ hx h'x, rpow_neg, div_eq_mul_inv] lemma rpow_neg_one (x : ℝ≥0∞) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_mul (x : ℝ≥0∞) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [h, Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { have : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, coe_rpow_of_ne_zero this, nnreal.rpow_mul] } } end @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0∞) (n : ℕ) : x ^ (n : ℝ) = x ^ n := begin cases x, { cases n; simp [top_rpow_of_pos (nat.cast_add_one_pos _), top_pow (nat.succ_pos _)] }, { simp [coe_rpow_of_nonneg _ (nat.cast_nonneg n)] } end @[simp] lemma rpow_two (x : ℝ≥0∞) : x ^ (2 : ℝ) = x ^ 2 := by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] } lemma mul_rpow_eq_ite (x y : ℝ≥0∞) (z : ℝ) : (x * y) ^ z = if (x = 0 ∧ y = ⊤ ∨ x = ⊤ ∧ y = 0) ∧ z < 0 then ⊤ else x ^ z * y ^ z := begin rcases eq_or_ne z 0 with rfl|hz, { simp }, replace hz := hz.lt_or_lt, wlog hxy : x ≤ y := le_total x y using [x y, y x] tactic.skip, { rcases eq_or_ne x 0 with rfl|hx0, { induction y using with_top.rec_top_coe; cases hz with hz hz; simp [*, hz.not_lt] }, rcases eq_or_ne y 0 with rfl|hy0, { exact (hx0 (bot_unique hxy)).elim }, induction x using with_top.rec_top_coe, { cases hz with hz hz; simp [hz, top_unique hxy] }, induction y using with_top.rec_top_coe, { cases hz with hz hz; simp * }, simp only [*, false_and, and_false, false_or, if_false], norm_cast at *, rw [coe_rpow_of_ne_zero (mul_ne_zero hx0 hy0), nnreal.mul_rpow] }, { convert this using 2; simp only [mul_comm, and_comm, or_comm] } end lemma mul_rpow_of_ne_top {x y : ℝ≥0∞} (hx : x ≠ ⊤) (hy : y ≠ ⊤) (z : ℝ) : (x * y) ^ z = x^z * y^z := by simp [*, mul_rpow_eq_ite] @[norm_cast] lemma coe_mul_rpow (x y : ℝ≥0) (z : ℝ) : ((x : ℝ≥0∞) * y) ^ z = x^z * y^z := mul_rpow_of_ne_top coe_ne_top coe_ne_top z lemma mul_rpow_of_ne_zero {x y : ℝ≥0∞} (hx : x ≠ 0) (hy : y ≠ 0) (z : ℝ) : (x * y) ^ z = x ^ z * y ^ z := by simp [*, mul_rpow_eq_ite] lemma mul_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) : (x * y) ^ z = x ^ z * y ^ z := by simp [hz.not_lt, mul_rpow_eq_ite] lemma inv_rpow (x : ℝ≥0∞) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ := begin rcases eq_or_ne y 0 with rfl|hy, { simp only [rpow_zero, inv_one] }, replace hy := hy.lt_or_lt, rcases eq_or_ne x 0 with rfl|h0, { cases hy; simp * }, rcases eq_or_ne x ⊤ with rfl|h_top, { cases hy; simp * }, apply ennreal.eq_inv_of_mul_eq_one_left, rw [← mul_rpow_of_ne_zero (ennreal.inv_ne_zero.2 h_top) h0, ennreal.inv_mul_cancel h0 h_top, one_rpow] end lemma div_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) : (x / y) ^ z = x ^ z / y ^ z := by rw [div_eq_mul_inv, mul_rpow_of_nonneg _ _ hz, inv_rpow, div_eq_mul_inv] lemma strict_mono_rpow_of_pos {z : ℝ} (h : 0 < z) : strict_mono (λ x : ℝ≥0∞, x ^ z) := begin intros x y hxy, lift x to ℝ≥0 using ne_top_of_lt hxy, rcases eq_or_ne y ∞ with rfl|hy, { simp only [top_rpow_of_pos h, coe_rpow_of_nonneg _ h.le, coe_lt_top] }, { lift y to ℝ≥0 using hy, simp only [coe_rpow_of_nonneg _ h.le, nnreal.rpow_lt_rpow (coe_lt_coe.1 hxy) h, coe_lt_coe] } end lemma monotone_rpow_of_nonneg {z : ℝ} (h : 0 ≤ z) : monotone (λ x : ℝ≥0∞, x ^ z) := h.eq_or_lt.elim (λ h0, h0 ▸ by simp only [rpow_zero, monotone_const]) (λ h0, (strict_mono_rpow_of_pos h0).monotone) /-- Bundles `λ x : ℝ≥0∞, x ^ y` into an order isomorphism when `y : ℝ` is positive, where the inverse is `λ x : ℝ≥0∞, x ^ (1 / y)`. -/ @[simps apply] def order_iso_rpow (y : ℝ) (hy : 0 < y) : ℝ≥0∞ ≃o ℝ≥0∞ := (strict_mono_rpow_of_pos hy).order_iso_of_right_inverse (λ x, x ^ y) (λ x, x ^ (1 / y)) (λ x, by { dsimp, rw [←rpow_mul, one_div_mul_cancel hy.ne.symm, rpow_one] }) lemma order_iso_rpow_symm_apply (y : ℝ) (hy : 0 < y) : (order_iso_rpow y hy).symm = order_iso_rpow (1 / y) (one_div_pos.2 hy) := by { simp only [order_iso_rpow, one_div_one_div], refl } lemma rpow_le_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := monotone_rpow_of_nonneg h₂ h₁ lemma rpow_lt_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := strict_mono_rpow_of_pos h₂ h₁ lemma rpow_le_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := (strict_mono_rpow_of_pos hz).le_iff_le lemma rpow_lt_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := (strict_mono_rpow_of_pos hz).lt_iff_lt lemma le_rpow_one_div_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ≤ y ^ (1 / z) ↔ x ^ z ≤ y := begin nth_rewrite 0 ←rpow_one x, nth_rewrite 0 ←@_root_.mul_inv_cancel _ _ z hz.ne', rw [rpow_mul, ←one_div, @rpow_le_rpow_iff _ _ (1/z) (by simp [hz])], end lemma lt_rpow_one_div_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x < y ^ (1 / z) ↔ x ^ z < y := begin nth_rewrite 0 ←rpow_one x, nth_rewrite 0 ←@_root_.mul_inv_cancel _ _ z (ne_of_lt hz).symm, rw [rpow_mul, ←one_div, @rpow_lt_rpow_iff _ _ (1/z) (by simp [hz])], end lemma rpow_one_div_le_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ (1 / z) ≤ y ↔ x ≤ y ^ z := begin nth_rewrite 0 ← ennreal.rpow_one y, nth_rewrite 1 ← @_root_.mul_inv_cancel _ _ z hz.ne.symm, rw [ennreal.rpow_mul, ← one_div, ennreal.rpow_le_rpow_iff (one_div_pos.2 hz)], end lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0∞} {y z : ℝ} (hx : 1 < x) (hx' : x ≠ ⊤) (hyz : y < z) : x^y < x^z := begin lift x to ℝ≥0 using hx', rw [one_lt_coe_iff] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), nnreal.rpow_lt_rpow_of_exponent_lt hx hyz] end lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0∞} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, top_rpow_of_neg, top_rpow_of_pos, le_refl]; linarith }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), nnreal.rpow_le_rpow_of_exponent_le hx hyz] } end lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0∞} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx1 le_top), simp only [coe_lt_one_iff, coe_pos] at hx0 hx1, simp [coe_rpow_of_ne_zero (ne_of_gt hx0), nnreal.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz] end lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0∞} {y z : ℝ} (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx1 coe_lt_top), by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, h, zero_rpow_of_neg, zero_rpow_of_pos, le_refl]; linarith }, { rw [coe_le_one_iff] at hx1, simp [coe_rpow_of_ne_zero h, nnreal.rpow_le_rpow_of_exponent_ge (bot_lt_iff_ne_bot.mpr h) hx1 hyz] } end lemma rpow_le_self_of_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x := begin nth_rewrite 1 ←ennreal.rpow_one x, exact ennreal.rpow_le_rpow_of_exponent_ge hx h_one_le, end lemma le_rpow_self_of_one_le {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (h_one_le : 1 ≤ z) : x ≤ x ^ z := begin nth_rewrite 0 ←ennreal.rpow_one x, exact ennreal.rpow_le_rpow_of_exponent_le hx h_one_le, end lemma rpow_pos_of_nonneg {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hp_nonneg : 0 ≤ p) : 0 < x^p := begin by_cases hp_zero : p = 0, { simp [hp_zero, ennreal.zero_lt_one], }, { rw ←ne.def at hp_zero, have hp_pos := lt_of_le_of_ne hp_nonneg hp_zero.symm, rw ←zero_rpow_of_pos hp_pos, exact rpow_lt_rpow hx_pos hp_pos, }, end lemma rpow_pos {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hx_ne_top : x ≠ ⊤) : 0 < x^p := begin cases lt_or_le 0 p with hp_pos hp_nonpos, { exact rpow_pos_of_nonneg hx_pos (le_of_lt hp_pos), }, { rw [←neg_neg p, rpow_neg, ennreal.inv_pos], exact rpow_ne_top_of_nonneg (right.nonneg_neg_iff.mpr hp_nonpos) hx_ne_top, }, end lemma rpow_lt_one {x : ℝ≥0∞} {z : ℝ} (hx : x < 1) (hz : 0 < z) : x^z < 1 := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx le_top), simp only [coe_lt_one_iff] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.rpow_lt_one hx hz], end lemma rpow_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx coe_lt_top), simp only [coe_le_one_iff] at hx, simp [coe_rpow_of_nonneg _ hz, nnreal.rpow_le_one hx hz], end lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := begin cases x, { simp [top_rpow_of_neg hz, ennreal.zero_lt_one] }, { simp only [some_eq_coe, one_lt_coe_iff] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), nnreal.rpow_lt_one_of_one_lt_of_neg hx hz] }, end lemma rpow_le_one_of_one_le_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : z < 0) : x^z ≤ 1 := begin cases x, { simp [top_rpow_of_neg hz, ennreal.zero_lt_one] }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), nnreal.rpow_le_one_of_one_le_of_nonpos hx (le_of_lt hz)] }, end lemma one_lt_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := begin cases x, { simp [top_rpow_of_pos hz] }, { simp only [some_eq_coe, one_lt_coe_iff] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_lt_rpow hx hz] } end lemma one_le_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : 0 < z) : 1 ≤ x^z := begin cases x, { simp [top_rpow_of_pos hz] }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_le_rpow hx (le_of_lt hz)] }, end lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0∞} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx2 le_top), simp only [coe_lt_one_iff, coe_pos] at ⊢ hx1 hx2, simp [coe_rpow_of_ne_zero (ne_of_gt hx1), nnreal.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz], end lemma one_le_rpow_of_pos_of_le_one_of_neg {x : ℝ≥0∞} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z < 0) : 1 ≤ x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx2 coe_lt_top), simp only [coe_le_one_iff, coe_pos] at ⊢ hx1 hx2, simp [coe_rpow_of_ne_zero (ne_of_gt hx1), nnreal.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 (le_of_lt hz)], end lemma to_nnreal_rpow (x : ℝ≥0∞) (z : ℝ) : (x.to_nnreal) ^ z = (x ^ z).to_nnreal := begin rcases lt_trichotomy z 0 with H|H|H, { cases x, { simp [H, ne_of_lt] }, by_cases hx : x = 0, { simp [hx, H, ne_of_lt] }, { simp [coe_rpow_of_ne_zero hx] } }, { simp [H] }, { cases x, { simp [H, ne_of_gt] }, simp [coe_rpow_of_nonneg _ (le_of_lt H)] } end lemma to_real_rpow (x : ℝ≥0∞) (z : ℝ) : (x.to_real) ^ z = (x ^ z).to_real := by rw [ennreal.to_real, ennreal.to_real, ←nnreal.coe_rpow, ennreal.to_nnreal_rpow] lemma of_real_rpow_of_pos {x p : ℝ} (hx_pos : 0 < x) : ennreal.of_real x ^ p = ennreal.of_real (x ^ p) := begin simp_rw ennreal.of_real, rw [coe_rpow_of_ne_zero, coe_eq_coe, real.to_nnreal_rpow_of_nonneg hx_pos.le], simp [hx_pos], end lemma of_real_rpow_of_nonneg {x p : ℝ} (hx_nonneg : 0 ≤ x) (hp_nonneg : 0 ≤ p) : ennreal.of_real x ^ p = ennreal.of_real (x ^ p) := begin by_cases hp0 : p = 0, { simp [hp0], }, by_cases hx0 : x = 0, { rw ← ne.def at hp0, have hp_pos : 0 < p := lt_of_le_of_ne hp_nonneg hp0.symm, simp [hx0, hp_pos, hp_pos.ne.symm], }, rw ← ne.def at hx0, exact of_real_rpow_of_pos (hx_nonneg.lt_of_ne hx0.symm), end lemma rpow_left_injective {x : ℝ} (hx : x ≠ 0) : function.injective (λ y : ℝ≥0∞, y^x) := begin intros y z hyz, dsimp only at hyz, rw [←rpow_one y, ←rpow_one z, ←_root_.mul_inv_cancel hx, rpow_mul, rpow_mul, hyz], end lemma rpow_left_surjective {x : ℝ} (hx : x ≠ 0) : function.surjective (λ y : ℝ≥0∞, y^x) := λ y, ⟨y ^ x⁻¹, by simp_rw [←rpow_mul, _root_.inv_mul_cancel hx, rpow_one]⟩ lemma rpow_left_bijective {x : ℝ} (hx : x ≠ 0) : function.bijective (λ y : ℝ≥0∞, y^x) := ⟨rpow_left_injective hx, rpow_left_surjective hx⟩ theorem tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ (x : ℝ≥0∞), x ^ y) (𝓝 ⊤) (𝓝 ⊤) := begin rw tendsto_nhds_top_iff_nnreal, intros x, obtain ⟨c, _, hc⟩ := (at_top_basis_Ioi.tendsto_iff at_top_basis_Ioi).mp (nnreal.tendsto_rpow_at_top hy) x trivial, have hc' : set.Ioi (↑c) ∈ 𝓝 (⊤ : ℝ≥0∞) := Ioi_mem_nhds coe_lt_top, refine eventually_of_mem hc' _, intros a ha, by_cases ha' : a = ⊤, { simp [ha', hy] }, lift a to ℝ≥0 using ha', change ↑c < ↑a at ha, rw coe_rpow_of_nonneg _ hy.le, exact_mod_cast hc a (by exact_mod_cast ha), end lemma eventually_pow_one_div_le {x : ℝ≥0∞} (hx : x ≠ ∞) {y : ℝ≥0∞} (hy : 1 < y) : ∀ᶠ (n : ℕ) in at_top, x ^ (1 / n : ℝ) ≤ y := begin lift x to ℝ≥0 using hx, by_cases y = ∞, { exact eventually_of_forall (λ n, h.symm ▸ le_top) }, { lift y to ℝ≥0 using h, have := nnreal.eventually_pow_one_div_le x (by exact_mod_cast hy : 1 < y), refine this.congr (eventually_of_forall $ λ n, _), rw [coe_rpow_of_nonneg x (by positivity : 0 ≤ (1 / n : ℝ)), coe_le_coe] }, end private lemma continuous_at_rpow_const_of_pos {x : ℝ≥0∞} {y : ℝ} (h : 0 < y) : continuous_at (λ a : ℝ≥0∞, a ^ y) x := begin by_cases hx : x = ⊤, { rw [hx, continuous_at], convert tendsto_rpow_at_top h, simp [h] }, lift x to ℝ≥0 using hx, rw continuous_at_coe_iff, convert continuous_coe.continuous_at.comp (nnreal.continuous_at_rpow_const (or.inr h.le)) using 1, ext1 x, simp [coe_rpow_of_nonneg _ h.le] end @[continuity] lemma continuous_rpow_const {y : ℝ} : continuous (λ a : ℝ≥0∞, a ^ y) := begin apply continuous_iff_continuous_at.2 (λ x, _), rcases lt_trichotomy 0 y with hy|rfl|hy, { exact continuous_at_rpow_const_of_pos hy }, { simp only [rpow_zero], exact continuous_at_const }, { obtain ⟨z, hz⟩ : ∃ z, y = -z := ⟨-y, (neg_neg _).symm⟩, have z_pos : 0 < z, by simpa [hz] using hy, simp_rw [hz, rpow_neg], exact continuous_inv.continuous_at.comp (continuous_at_rpow_const_of_pos z_pos) } end lemma tendsto_const_mul_rpow_nhds_zero_of_pos {c : ℝ≥0∞} (hc : c ≠ ∞) {y : ℝ} (hy : 0 < y) : tendsto (λ x : ℝ≥0∞, c * x ^ y) (𝓝 0) (𝓝 0) := begin convert ennreal.tendsto.const_mul (ennreal.continuous_rpow_const.tendsto 0) _, { simp [hy] }, { exact or.inr hc } end end ennreal lemma filter.tendsto.ennrpow_const {α : Type*} {f : filter α} {m : α → ℝ≥0∞} {a : ℝ≥0∞} (r : ℝ) (hm : tendsto m f (𝓝 a)) : tendsto (λ x, (m x) ^ r) f (𝓝 (a ^ r)) := (ennreal.continuous_rpow_const.tendsto a).comp hm namespace norm_num open tactic theorem rpow_pos (a b : ℝ) (b' : ℕ) (c : ℝ) (hb : (b':ℝ) = b) (h : a ^ b' = c) : a ^ b = c := by rw [← h, ← hb, real.rpow_nat_cast] theorem rpow_neg (a b : ℝ) (b' : ℕ) (c c' : ℝ) (a0 : 0 ≤ a) (hb : (b':ℝ) = b) (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' := by rw [← hc, ← h, ← hb, real.rpow_neg a0, real.rpow_nat_cast] /-- Evaluate `real.rpow a b` where `a` is a rational numeral and `b` is an integer. (This cannot go via the generalized version `prove_rpow'` because `rpow_pos` has a side condition; we do not attempt to evaluate `a ^ b` where `a` and `b` are both negative because it comes out to some garbage.) -/ meta def prove_rpow (a b : expr) : tactic (expr × expr) := do na ← a.to_rat, ic ← mk_instance_cache `(ℝ), match match_sign b with | sum.inl b := do (ic, a0) ← guard (na ≥ 0) >> prove_nonneg ic a, nc ← mk_instance_cache `(ℕ), (ic, nc, b', hb) ← prove_nat_uncast ic nc b, (ic, c, h) ← prove_pow a na ic b', cr ← c.to_rat, (ic, c', hc) ← prove_inv ic c cr, pure (c', (expr.const ``rpow_neg []).mk_app [a, b, b', c, c', a0, hb, h, hc]) | sum.inr ff := pure (`(1:ℝ), expr.const ``real.rpow_zero [] a) | sum.inr tt := do nc ← mk_instance_cache `(ℕ), (ic, nc, b', hb) ← prove_nat_uncast ic nc b, (ic, c, h) ← prove_pow a na ic b', pure (c, (expr.const ``rpow_pos []).mk_app [a, b, b', c, hb, h]) end /-- Generalized version of `prove_cpow`, `prove_nnrpow`, `prove_ennrpow`. -/ meta def prove_rpow' (pos neg zero : name) (α β one a b : expr) : tactic (expr × expr) := do na ← a.to_rat, icα ← mk_instance_cache α, icβ ← mk_instance_cache β, match match_sign b with | sum.inl b := do nc ← mk_instance_cache `(ℕ), (icβ, nc, b', hb) ← prove_nat_uncast icβ nc b, (icα, c, h) ← prove_pow a na icα b', cr ← c.to_rat, (icα, c', hc) ← prove_inv icα c cr, pure (c', (expr.const neg []).mk_app [a, b, b', c, c', hb, h, hc]) | sum.inr ff := pure (one, expr.const zero [] a) | sum.inr tt := do nc ← mk_instance_cache `(ℕ), (icβ, nc, b', hb) ← prove_nat_uncast icβ nc b, (icα, c, h) ← prove_pow a na icα b', pure (c, (expr.const pos []).mk_app [a, b, b', c, hb, h]) end open_locale nnreal ennreal theorem cpow_pos (a b : ℂ) (b' : ℕ) (c : ℂ) (hb : b = b') (h : a ^ b' = c) : a ^ b = c := by rw [← h, hb, complex.cpow_nat_cast] theorem cpow_neg (a b : ℂ) (b' : ℕ) (c c' : ℂ) (hb : b = b') (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' := by rw [← hc, ← h, hb, complex.cpow_neg, complex.cpow_nat_cast] theorem nnrpow_pos (a : ℝ≥0) (b : ℝ) (b' : ℕ) (c : ℝ≥0) (hb : b = b') (h : a ^ b' = c) : a ^ b = c := by rw [← h, hb, nnreal.rpow_nat_cast] theorem nnrpow_neg (a : ℝ≥0) (b : ℝ) (b' : ℕ) (c c' : ℝ≥0) (hb : b = b') (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' := by rw [← hc, ← h, hb, nnreal.rpow_neg, nnreal.rpow_nat_cast] theorem ennrpow_pos (a : ℝ≥0∞) (b : ℝ) (b' : ℕ) (c : ℝ≥0∞) (hb : b = b') (h : a ^ b' = c) : a ^ b = c := by rw [← h, hb, ennreal.rpow_nat_cast] theorem ennrpow_neg (a : ℝ≥0∞) (b : ℝ) (b' : ℕ) (c c' : ℝ≥0∞) (hb : b = b') (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' := by rw [← hc, ← h, hb, ennreal.rpow_neg, ennreal.rpow_nat_cast] /-- Evaluate `complex.cpow a b` where `a` is a rational numeral and `b` is an integer. -/ meta def prove_cpow : expr → expr → tactic (expr × expr) := prove_rpow' ``cpow_pos ``cpow_neg ``complex.cpow_zero `(ℂ) `(ℂ) `(1:ℂ) /-- Evaluate `nnreal.rpow a b` where `a` is a rational numeral and `b` is an integer. -/ meta def prove_nnrpow : expr → expr → tactic (expr × expr) := prove_rpow' ``nnrpow_pos ``nnrpow_neg ``nnreal.rpow_zero `(ℝ≥0) `(ℝ) `(1:ℝ≥0) /-- Evaluate `ennreal.rpow a b` where `a` is a rational numeral and `b` is an integer. -/ meta def prove_ennrpow : expr → expr → tactic (expr × expr) := prove_rpow' ``ennrpow_pos ``ennrpow_neg ``ennreal.rpow_zero `(ℝ≥0∞) `(ℝ) `(1:ℝ≥0∞) /-- Evaluates expressions of the form `rpow a b`, `cpow a b` and `a ^ b` in the special case where `b` is an integer and `a` is a positive rational (so it's really just a rational power). -/ @[norm_num] meta def eval_rpow_cpow : expr → tactic (expr × expr) | `(@has_pow.pow _ _ real.has_pow %%a %%b) := b.to_int >> prove_rpow a b | `(real.rpow %%a %%b) := b.to_int >> prove_rpow a b | `(@has_pow.pow _ _ complex.has_pow %%a %%b) := b.to_int >> prove_cpow a b | `(complex.cpow %%a %%b) := b.to_int >> prove_cpow a b | `(@has_pow.pow _ _ nnreal.real.has_pow %%a %%b) := b.to_int >> prove_nnrpow a b | `(nnreal.rpow %%a %%b) := b.to_int >> prove_nnrpow a b | `(@has_pow.pow _ _ ennreal.real.has_pow %%a %%b) := b.to_int >> prove_ennrpow a b | `(ennreal.rpow %%a %%b) := b.to_int >> prove_ennrpow a b | _ := tactic.failed end norm_num namespace tactic namespace positivity /-- Auxiliary definition for the `positivity` tactic to handle real powers of reals. -/ meta def prove_rpow (a b : expr) : tactic strictness := do strictness_a ← core a, match strictness_a with | nonnegative p := nonnegative <$> mk_app ``real.rpow_nonneg_of_nonneg [p, b] | positive p := positive <$> mk_app ``real.rpow_pos_of_pos [p, b] | _ := failed end private lemma nnrpow_pos {a : ℝ≥0} (ha : 0 < a) (b : ℝ) : 0 < a ^ b := nnreal.rpow_pos ha /-- Auxiliary definition for the `positivity` tactic to handle real powers of nonnegative reals. -/ meta def prove_nnrpow (a b : expr) : tactic strictness := do strictness_a ← core a, match strictness_a with | positive p := positive <$> mk_app ``nnrpow_pos [p, b] | _ := failed -- We already know `0 ≤ x` for all `x : ℝ≥0` end private lemma ennrpow_pos {a : ℝ≥0∞} {b : ℝ} (ha : 0 < a) (hb : 0 < b) : 0 < a ^ b := ennreal.rpow_pos_of_nonneg ha hb.le /-- Auxiliary definition for the `positivity` tactic to handle real powers of extended nonnegative reals. -/ meta def prove_ennrpow (a b : expr) : tactic strictness := do strictness_a ← core a, strictness_b ← core b, match strictness_a, strictness_b with | positive pa, positive pb := positive <$> mk_app ``ennrpow_pos [pa, pb] | positive pa, nonnegative pb := positive <$> mk_app ``ennreal.rpow_pos_of_nonneg [pa, pb] | _, _ := failed -- We already know `0 ≤ x` for all `x : ℝ≥0∞` end end positivity open positivity /-- Extension for the `positivity` tactic: exponentiation by a real number is nonnegative when the base is nonnegative and positive when the base is positive. -/ @[positivity] meta def positivity_rpow : expr → tactic strictness | `(@has_pow.pow _ _ real.has_pow %%a %%b) := prove_rpow a b | `(real.rpow %%a %%b) := prove_rpow a b | `(@has_pow.pow _ _ nnreal.real.has_pow %%a %%b) := prove_nnrpow a b | `(nnreal.rpow %%a %%b) := prove_nnrpow a b | `(@has_pow.pow _ _ ennreal.real.has_pow %%a %%b) := prove_ennrpow a b | `(ennreal.rpow %%a %%b) := prove_ennrpow a b | _ := failed end tactic
27a2cb2f8635c86eeee0d0d612592d2420fdce2e
c777c32c8e484e195053731103c5e52af26a25d1
/src/data/polynomial/div.lean
9abd9e5cf5e15f2a8ff7ac03e7a713b18dc85e08
[ "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
22,447
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.algebra_map import data.polynomial.inductions import data.polynomial.monic import ring_theory.multiplicity /-! # Division of univariate polynomials > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The main defs are `div_by_monic` and `mod_by_monic`. The compatibility between these is given by `mod_by_monic_add_div`. We also define `root_multiplicity`. -/ noncomputable theory open_locale classical big_operators polynomial open finset namespace polynomial universes u v w z variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {a b : R} {n : ℕ} section comm_semiring variables [comm_semiring R] theorem X_dvd_iff {f : R[X]} : X ∣ f ↔ f.coeff 0 = 0 := ⟨λ ⟨g, hfg⟩, by rw [hfg, mul_comm, coeff_mul_X_zero], λ hf, ⟨f.div_X, by rw [mul_comm, ← add_zero (f.div_X * X), ← C_0, ← hf, div_X_mul_X_add]⟩⟩ theorem X_pow_dvd_iff {f : R[X]} {n : ℕ} : X^n ∣ f ↔ ∀ d < n, f.coeff d = 0 := ⟨λ ⟨g, hgf⟩ d hd, by simp only [hgf, coeff_X_pow_mul', ite_eq_right_iff, not_le_of_lt hd, is_empty.forall_iff], λ hd, begin induction n with n hn, { simp only [pow_zero, one_dvd] }, { obtain ⟨g, hgf⟩ := hn (λ d : ℕ, λ H : d < n, hd _ (nat.lt_succ_of_lt H)), have := coeff_X_pow_mul g n 0, rw [zero_add, ← hgf, hd n (nat.lt_succ_self n)] at this, obtain ⟨k, hgk⟩ := polynomial.X_dvd_iff.mpr this.symm, use k, rwa [pow_succ, mul_comm X _, mul_assoc, ← hgk]}, end⟩ end comm_semiring section comm_semiring variables [comm_semiring R] {p q : R[X]} lemma multiplicity_finite_of_degree_pos_of_monic (hp : (0 : with_bot ℕ) < degree p) (hmp : monic p) (hq : q ≠ 0) : multiplicity.finite p q := have zn0 : (0 : R) ≠ 1, by haveI := nontrivial.of_polynomial_ne hq; exact zero_ne_one, ⟨nat_degree q, λ ⟨r, hr⟩, have hp0 : p ≠ 0, from λ hp0, by simp [hp0] at hp; contradiction, have hr0 : r ≠ 0, from λ hr0, by simp * at *, have hpn1 : leading_coeff p ^ (nat_degree q + 1) = 1, by simp [show _ = _, from hmp], have hpn0' : leading_coeff p ^ (nat_degree q + 1) ≠ 0, from hpn1.symm ▸ zn0.symm, have hpnr0 : leading_coeff (p ^ (nat_degree q + 1)) * leading_coeff r ≠ 0, by simp only [leading_coeff_pow' hpn0', leading_coeff_eq_zero, hpn1, one_pow, one_mul, ne.def, hr0]; simp, have hnp : 0 < nat_degree p, by rw [← with_bot.coe_lt_coe, ← degree_eq_nat_degree hp0]; exact hp, begin have := congr_arg nat_degree hr, rw [nat_degree_mul' hpnr0, nat_degree_pow' hpn0', add_mul, add_assoc] at this, exact ne_of_lt (lt_add_of_le_of_pos (le_mul_of_one_le_right (nat.zero_le _) hnp) (add_pos_of_pos_of_nonneg (by rwa one_mul) (nat.zero_le _))) this end⟩ end comm_semiring section ring variables [ring R] {p q : R[X]} lemma div_wf_lemma (h : degree q ≤ degree p ∧ p ≠ 0) (hq : monic q) : degree (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) < degree p := have hp : leading_coeff p ≠ 0 := mt leading_coeff_eq_zero.1 h.2, have hq0 : q ≠ 0 := hq.ne_zero_of_polynomial_ne h.2, have hlt : nat_degree q ≤ nat_degree p := with_bot.coe_le_coe.1 (by rw [← degree_eq_nat_degree h.2, ← degree_eq_nat_degree hq0]; exact h.1), degree_sub_lt (by rw [hq.degree_mul, degree_C_mul_X_pow _ hp, degree_eq_nat_degree h.2, degree_eq_nat_degree hq0, ← with_bot.coe_add, tsub_add_cancel_of_le hlt]) h.2 (by rw [leading_coeff_mul_monic hq, leading_coeff_mul_X_pow, leading_coeff_C]) /-- See `div_by_monic`. -/ noncomputable def div_mod_by_monic_aux : Π (p : R[X]) {q : R[X]}, monic q → R[X] × R[X] | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then let z := C (leading_coeff p) * X^(nat_degree p - nat_degree q) in have wf : _ := div_wf_lemma h hq, let dm := div_mod_by_monic_aux (p - z * q) hq in ⟨z + dm.1, dm.2⟩ else ⟨0, p⟩ using_well_founded {dec_tac := tactic.assumption} /-- `div_by_monic` gives the quotient of `p` by a monic polynomial `q`. -/ def div_by_monic (p q : R[X]) : R[X] := if hq : monic q then (div_mod_by_monic_aux p hq).1 else 0 /-- `mod_by_monic` gives the remainder of `p` by a monic polynomial `q`. -/ def mod_by_monic (p q : R[X]) : R[X] := if hq : monic q then (div_mod_by_monic_aux p hq).2 else p infixl ` /ₘ ` : 70 := div_by_monic infixl ` %ₘ ` : 70 := mod_by_monic lemma degree_mod_by_monic_lt [nontrivial R] : ∀ (p : R[X]) {q : R[X]} (hq : monic q), degree (p %ₘ q) < degree q | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then have wf : _ := div_wf_lemma ⟨h.1, h.2⟩ hq, have degree ((p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) %ₘ q) < degree q := degree_mod_by_monic_lt (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq, begin unfold mod_by_monic at this ⊢, unfold div_mod_by_monic_aux, rw dif_pos hq at this ⊢, rw if_pos h, exact this end else or.cases_on (not_and_distrib.1 h) begin unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h], exact lt_of_not_ge, end begin assume hp, unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, not_not.1 hp], exact lt_of_le_of_ne bot_le (ne.symm (mt degree_eq_bot.1 hq.ne_zero)), end using_well_founded {dec_tac := tactic.assumption} @[simp] lemma zero_mod_by_monic (p : R[X]) : 0 %ₘ p = 0 := begin unfold mod_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma zero_div_by_monic (p : R[X]) : 0 /ₘ p = 0 := begin unfold div_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma mod_by_monic_zero (p : R[X]) : p %ₘ 0 = p := if h : monic (0 : R[X]) then by { haveI := monic_zero_iff_subsingleton.mp h, simp } else by unfold mod_by_monic div_mod_by_monic_aux; rw dif_neg h @[simp] lemma div_by_monic_zero (p : R[X]) : p /ₘ 0 = 0 := if h : monic (0 : R[X]) then by { haveI := monic_zero_iff_subsingleton.mp h, simp } else by unfold div_by_monic div_mod_by_monic_aux; rw dif_neg h lemma div_by_monic_eq_of_not_monic (p : R[X]) (hq : ¬monic q) : p /ₘ q = 0 := dif_neg hq lemma mod_by_monic_eq_of_not_monic (p : R[X]) (hq : ¬monic q) : p %ₘ q = p := dif_neg hq lemma mod_by_monic_eq_self_iff [nontrivial R] (hq : monic q) : p %ₘ q = p ↔ degree p < degree q := ⟨λ h, h ▸ degree_mod_by_monic_lt _ hq, λ h, have ¬ degree q ≤ degree p := not_le_of_gt h, by unfold mod_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ theorem degree_mod_by_monic_le (p : R[X]) {q : R[X]} (hq : monic q) : degree (p %ₘ q) ≤ degree q := by { nontriviality R, exact (degree_mod_by_monic_lt _ hq).le } end ring section comm_ring variables [comm_ring R] {p q : R[X]} lemma mod_by_monic_eq_sub_mul_div : ∀ (p : R[X]) {q : R[X]} (hq : monic q), p %ₘ q = p - q * (p /ₘ q) | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then have wf : _ := div_wf_lemma h hq, have ih : _ := mod_by_monic_eq_sub_mul_div (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq, begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_pos h], rw [mod_by_monic, dif_pos hq] at ih, refine ih.trans _, unfold div_by_monic, rw [dif_pos hq, dif_pos hq, if_pos h, mul_add, sub_add_eq_sub_sub, mul_comm] end else begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, dif_pos hq, if_neg h, mul_zero, sub_zero] end using_well_founded {dec_tac := tactic.assumption} lemma mod_by_monic_add_div (p : R[X]) {q : R[X]} (hq : monic q) : p %ₘ q + q * (p /ₘ q) = p := eq_sub_iff_add_eq.1 (mod_by_monic_eq_sub_mul_div p hq) lemma div_by_monic_eq_zero_iff [nontrivial R] (hq : monic q) : p /ₘ q = 0 ↔ degree p < degree q := ⟨λ h, by have := mod_by_monic_add_div p hq; rwa [h, mul_zero, add_zero, mod_by_monic_eq_self_iff hq] at this, λ h, have ¬ degree q ≤ degree p := not_le_of_gt h, by unfold div_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ lemma degree_add_div_by_monic (hq : monic q) (h : degree q ≤ degree p) : degree q + degree (p /ₘ q) = degree p := begin nontriviality R, have hdiv0 : p /ₘ q ≠ 0 := by rwa [(≠), div_by_monic_eq_zero_iff hq, not_lt], have hlc : leading_coeff q * leading_coeff (p /ₘ q) ≠ 0 := by rwa [monic.def.1 hq, one_mul, (≠), leading_coeff_eq_zero], have hmod : degree (p %ₘ q) < degree (q * (p /ₘ q)) := calc degree (p %ₘ q) < degree q : degree_mod_by_monic_lt _ hq ... ≤ _ : by rw [degree_mul' hlc, degree_eq_nat_degree hq.ne_zero, degree_eq_nat_degree hdiv0, ← with_bot.coe_add, with_bot.coe_le_coe]; exact nat.le_add_right _ _, calc degree q + degree (p /ₘ q) = degree (q * (p /ₘ q)) : eq.symm (degree_mul' hlc) ... = degree (p %ₘ q + q * (p /ₘ q)) : (degree_add_eq_right_of_degree_lt hmod).symm ... = _ : congr_arg _ (mod_by_monic_add_div _ hq) end lemma degree_div_by_monic_le (p q : R[X]) : degree (p /ₘ q) ≤ degree p := if hp0 : p = 0 then by simp only [hp0, zero_div_by_monic, le_refl] else if hq : monic q then if h : degree q ≤ degree p then by haveI := nontrivial.of_polynomial_ne hp0; rw [← degree_add_div_by_monic hq h, degree_eq_nat_degree hq.ne_zero, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq).1 (not_lt.2 h))]; exact with_bot.coe_le_coe.2 (nat.le_add_left _ _) else by unfold div_by_monic div_mod_by_monic_aux; simp only [dif_pos hq, h, false_and, if_false, degree_zero, bot_le] else (div_by_monic_eq_of_not_monic p hq).symm ▸ bot_le lemma degree_div_by_monic_lt (p : R[X]) {q : R[X]} (hq : monic q) (hp0 : p ≠ 0) (h0q : 0 < degree q) : degree (p /ₘ q) < degree p := if hpq : degree p < degree q then begin haveI := nontrivial.of_polynomial_ne hp0, rw [(div_by_monic_eq_zero_iff hq).2 hpq, degree_eq_nat_degree hp0], exact with_bot.bot_lt_coe _ end else begin haveI := nontrivial.of_polynomial_ne hp0, rw [← degree_add_div_by_monic hq (not_lt.1 hpq), degree_eq_nat_degree hq.ne_zero, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq).1 hpq)], exact with_bot.coe_lt_coe.2 (nat.lt_add_of_pos_left (with_bot.coe_lt_coe.1 $ (degree_eq_nat_degree hq.ne_zero) ▸ h0q)) end theorem nat_degree_div_by_monic {R : Type u} [comm_ring R] (f : R[X]) {g : R[X]} (hg : g.monic) : nat_degree (f /ₘ g) = nat_degree f - nat_degree g := begin nontriviality R, by_cases hfg : f /ₘ g = 0, { rw [hfg, nat_degree_zero], rw div_by_monic_eq_zero_iff hg at hfg, rw tsub_eq_zero_iff_le.mpr (nat_degree_le_nat_degree $ le_of_lt hfg) }, have hgf := hfg, rw div_by_monic_eq_zero_iff hg at hgf, push_neg at hgf, have := degree_add_div_by_monic hg hgf, have hf : f ≠ 0, { intro hf, apply hfg, rw [hf, zero_div_by_monic] }, rw [degree_eq_nat_degree hf, degree_eq_nat_degree hg.ne_zero, degree_eq_nat_degree hfg, ← with_bot.coe_add, with_bot.coe_eq_coe] at this, rw [← this, add_tsub_cancel_left] end lemma div_mod_by_monic_unique {f g} (q r : R[X]) (hg : monic g) (h : r + g * q = f ∧ degree r < degree g) : f /ₘ g = q ∧ f %ₘ g = r := begin nontriviality R, have h₁ : r - f %ₘ g = -g * (q - f /ₘ g), from eq_of_sub_eq_zero (by rw [← sub_eq_zero_of_eq (h.1.trans (mod_by_monic_add_div f hg).symm)]; simp [mul_add, mul_comm, sub_eq_add_neg, add_comm, add_left_comm, add_assoc]), have h₂ : degree (r - f %ₘ g) = degree (g * (q - f /ₘ g)), by simp [h₁], have h₄ : degree (r - f %ₘ g) < degree g, from calc degree (r - f %ₘ g) ≤ max (degree r) (degree (f %ₘ g)) : degree_sub_le _ _ ... < degree g : max_lt_iff.2 ⟨h.2, degree_mod_by_monic_lt _ hg⟩, have h₅ : q - (f /ₘ g) = 0, from by_contradiction (λ hqf, not_le_of_gt h₄ $ calc degree g ≤ degree g + degree (q - f /ₘ g) : by erw [degree_eq_nat_degree hg.ne_zero, degree_eq_nat_degree hqf, with_bot.coe_le_coe]; exact nat.le_add_right _ _ ... = degree (r - f %ₘ g) : by rw [h₂, degree_mul']; simpa [monic.def.1 hg]), exact ⟨eq.symm $ eq_of_sub_eq_zero h₅, eq.symm $ eq_of_sub_eq_zero $ by simpa [h₅] using h₁⟩ end lemma map_mod_div_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f ∧ (p %ₘ q).map f = p.map f %ₘ q.map f := begin nontriviality S, haveI : nontrivial R := f.domain_nontrivial, have : map f p /ₘ map f q = map f (p /ₘ q) ∧ map f p %ₘ map f q = map f (p %ₘ q), { exact (div_mod_by_monic_unique ((p /ₘ q).map f) _ (hq.map f) ⟨eq.symm $ by rw [← polynomial.map_mul, ← polynomial.map_add, mod_by_monic_add_div _ hq], calc _ ≤ degree (p %ₘ q) : degree_map_le _ _ ... < degree q : degree_mod_by_monic_lt _ hq ... = _ : eq.symm $ degree_map_eq_of_leading_coeff_ne_zero _ (by rw [monic.def.1 hq, f.map_one]; exact one_ne_zero)⟩) }, exact ⟨this.1.symm, this.2.symm⟩ end lemma map_div_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f := (map_mod_div_by_monic f hq).1 lemma map_mod_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p %ₘ q).map f = p.map f %ₘ q.map f := (map_mod_div_by_monic f hq).2 lemma dvd_iff_mod_by_monic_eq_zero (hq : monic q) : p %ₘ q = 0 ↔ q ∣ p := ⟨λ h, by rw [← mod_by_monic_add_div p hq, h, zero_add]; exact dvd_mul_right _ _, λ h, begin nontriviality R, obtain ⟨r, hr⟩ := exists_eq_mul_right_of_dvd h, by_contradiction hpq0, have hmod : p %ₘ q = q * (r - p /ₘ q), { rw [mod_by_monic_eq_sub_mul_div _ hq, mul_sub, ← hr] }, have : degree (q * (r - p /ₘ q)) < degree q := hmod ▸ degree_mod_by_monic_lt _ hq, have hrpq0 : leading_coeff (r - p /ₘ q) ≠ 0 := λ h, hpq0 $ leading_coeff_eq_zero.1 (by rw [hmod, leading_coeff_eq_zero.1 h, mul_zero, leading_coeff_zero]), have hlc : leading_coeff q * leading_coeff (r - p /ₘ q) ≠ 0 := by rwa [monic.def.1 hq, one_mul], rw [degree_mul' hlc, degree_eq_nat_degree hq.ne_zero, degree_eq_nat_degree (mt leading_coeff_eq_zero.2 hrpq0)] at this, exact not_lt_of_ge (nat.le_add_right _ _) (with_bot.some_lt_some.1 this) end⟩ theorem map_dvd_map [comm_ring S] (f : R →+* S) (hf : function.injective f) {x y : R[X]} (hx : x.monic) : x.map f ∣ y.map f ↔ x ∣ y := begin rw [← dvd_iff_mod_by_monic_eq_zero hx, ← dvd_iff_mod_by_monic_eq_zero (hx.map f), ← map_mod_by_monic f hx], exact ⟨λ H, map_injective f hf $ by rw [H, polynomial.map_zero], λ H, by rw [H, polynomial.map_zero]⟩ end @[simp] lemma mod_by_monic_one (p : R[X]) : p %ₘ 1 = 0 := (dvd_iff_mod_by_monic_eq_zero (by convert monic_one)).2 (one_dvd _) @[simp] lemma div_by_monic_one (p : R[X]) : p /ₘ 1 = p := by conv_rhs { rw [← mod_by_monic_add_div p monic_one] }; simp @[simp] lemma mod_by_monic_X_sub_C_eq_C_eval (p : R[X]) (a : R) : p %ₘ (X - C a) = C (p.eval a) := begin nontriviality R, have h : (p %ₘ (X - C a)).eval a = p.eval a, { rw [mod_by_monic_eq_sub_mul_div _ (monic_X_sub_C a), eval_sub, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul, sub_zero] }, have : degree (p %ₘ (X - C a)) < 1 := degree_X_sub_C a ▸ degree_mod_by_monic_lt p (monic_X_sub_C a), have : degree (p %ₘ (X - C a)) ≤ 0, { cases (degree (p %ₘ (X - C a))), { exact bot_le }, { exact with_bot.some_le_some.2 (nat.le_of_lt_succ (with_bot.some_lt_some.1 this)) } }, rw [eq_C_of_degree_le_zero this, eval_C] at h, rw [eq_C_of_degree_le_zero this, h] end lemma mul_div_by_monic_eq_iff_is_root : (X - C a) * (p /ₘ (X - C a)) = p ↔ is_root p a := ⟨λ h, by rw [← h, is_root.def, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul], λ h : p.eval a = 0, by conv {to_rhs, rw ← mod_by_monic_add_div p (monic_X_sub_C a)}; rw [mod_by_monic_X_sub_C_eq_C_eval, h, C_0, zero_add]⟩ lemma dvd_iff_is_root : (X - C a) ∣ p ↔ is_root p a := ⟨λ h, by rwa [← dvd_iff_mod_by_monic_eq_zero (monic_X_sub_C _), mod_by_monic_X_sub_C_eq_C_eval, ← C_0, C_inj] at h, λ h, ⟨(p /ₘ (X - C a)), by rw mul_div_by_monic_eq_iff_is_root.2 h⟩⟩ lemma mod_by_monic_X (p : R[X]) : p %ₘ X = C (p.eval 0) := by rw [← mod_by_monic_X_sub_C_eq_C_eval, C_0, sub_zero] lemma eval₂_mod_by_monic_eq_self_of_root [comm_ring S] {f : R →+* S} {p q : R[X]} (hq : q.monic) {x : S} (hx : q.eval₂ f x = 0) : (p %ₘ q).eval₂ f x = p.eval₂ f x := by rw [mod_by_monic_eq_sub_mul_div p hq, eval₂_sub, eval₂_mul, hx, zero_mul, sub_zero] lemma sum_mod_by_monic_coeff (hq : q.monic) {n : ℕ} (hn : q.degree ≤ n) : ∑ (i : fin n), monomial i ((p %ₘ q).coeff i) = p %ₘ q := begin nontriviality R, exact (sum_fin (λ i c, monomial i c) (by simp) ((degree_mod_by_monic_lt _ hq).trans_le hn)).trans (sum_monomial_eq _) end lemma sub_dvd_eval_sub (a b : R) (p : R[X]) : a - b ∣ p.eval a - p.eval b := begin suffices : X - C b ∣ p - C (p.eval b), { simpa only [coe_eval_ring_hom, eval_sub, eval_X, eval_C] using (eval_ring_hom a).map_dvd this }, simp [dvd_iff_is_root] end lemma mul_div_mod_by_monic_cancel_left (p : R[X]) {q : R[X]} (hmo : q.monic) : q * p /ₘ q = p := begin nontriviality R, refine (div_mod_by_monic_unique _ 0 hmo ⟨by rw [zero_add], _⟩).1, rw [degree_zero], exact ne.bot_lt (λ h, hmo.ne_zero (degree_eq_bot.1 h)) end variable (R) lemma not_is_field : ¬ is_field R[X] := begin nontriviality R, rw ring.not_is_field_iff_exists_ideal_bot_lt_and_lt_top, use ideal.span {polynomial.X}, split, { rw [bot_lt_iff_ne_bot, ne.def, ideal.span_singleton_eq_bot], exact polynomial.X_ne_zero, }, { rw [lt_top_iff_ne_top, ne.def, ideal.eq_top_iff_one, ideal.mem_span_singleton, polynomial.X_dvd_iff, polynomial.coeff_one_zero], exact one_ne_zero, } end variable {R} lemma ker_eval_ring_hom (x : R) : (eval_ring_hom x).ker = ideal.span {X - C x} := by { ext y, simpa only [ideal.mem_span_singleton, dvd_iff_is_root] } section multiplicity /-- An algorithm for deciding polynomial divisibility. The algorithm is "compute `p %ₘ q` and compare to `0`". See `polynomial.mod_by_monic` for the algorithm that computes `%ₘ`. -/ def decidable_dvd_monic (p : R[X]) (hq : monic q) : decidable (q ∣ p) := decidable_of_iff (p %ₘ q = 0) (dvd_iff_mod_by_monic_eq_zero hq) open_locale classical lemma multiplicity_X_sub_C_finite (a : R) (h0 : p ≠ 0) : multiplicity.finite (X - C a) p := begin haveI := nontrivial.of_polynomial_ne h0, refine multiplicity_finite_of_degree_pos_of_monic _ (monic_X_sub_C _) h0, rw degree_X_sub_C, dec_trivial, end /-- The largest power of `X - C a` which divides `p`. This is computable via the divisibility algorithm `polynomial.decidable_dvd_monic`. -/ def root_multiplicity (a : R) (p : R[X]) : ℕ := if h0 : p = 0 then 0 else let I : decidable_pred (λ n : ℕ, ¬(X - C a) ^ (n + 1) ∣ p) := λ n, @not.decidable _ (decidable_dvd_monic p ((monic_X_sub_C a).pow (n + 1))) in by exactI nat.find (multiplicity_X_sub_C_finite a h0) lemma root_multiplicity_eq_multiplicity (p : R[X]) (a : R) : root_multiplicity a p = if h0 : p = 0 then 0 else (multiplicity (X - C a) p).get (multiplicity_X_sub_C_finite a h0) := by simp [multiplicity, root_multiplicity, part.dom]; congr; funext; congr @[simp] lemma root_multiplicity_zero {x : R} : root_multiplicity x 0 = 0 := dif_pos rfl @[simp] lemma root_multiplicity_eq_zero_iff {p : R[X]} {x : R} : root_multiplicity x p = 0 ↔ (is_root p x → p = 0) := by simp only [root_multiplicity_eq_multiplicity, dite_eq_left_iff, part_enat.get_eq_iff_eq_coe, nat.cast_zero, multiplicity.multiplicity_eq_zero, dvd_iff_is_root, not_imp_not] lemma root_multiplicity_eq_zero {p : R[X]} {x : R} (h : ¬ is_root p x) : root_multiplicity x p = 0 := root_multiplicity_eq_zero_iff.2 (λ h', (h h').elim) @[simp] lemma root_multiplicity_pos' {p : R[X]} {x : R} : 0 < root_multiplicity x p ↔ p ≠ 0 ∧ is_root p x := by rw [pos_iff_ne_zero, ne.def, root_multiplicity_eq_zero_iff, not_imp, and.comm] lemma root_multiplicity_pos {p : R[X]} (hp : p ≠ 0) {x : R} : 0 < root_multiplicity x p ↔ is_root p x := root_multiplicity_pos'.trans (and_iff_right hp) @[simp] lemma root_multiplicity_C (r a : R) : root_multiplicity a (C r) = 0 := by simp only [root_multiplicity_eq_zero_iff, is_root, eval_C, C_eq_zero, imp_self] lemma pow_root_multiplicity_dvd (p : R[X]) (a : R) : (X - C a) ^ root_multiplicity a p ∣ p := if h : p = 0 then by simp [h] else by rw [root_multiplicity_eq_multiplicity, dif_neg h]; exact multiplicity.pow_multiplicity_dvd _ lemma div_by_monic_mul_pow_root_multiplicity_eq (p : R[X]) (a : R) : p /ₘ ((X - C a) ^ root_multiplicity a p) * (X - C a) ^ root_multiplicity a p = p := have monic ((X - C a) ^ root_multiplicity a p), from (monic_X_sub_C _).pow _, by conv_rhs { rw [← mod_by_monic_add_div p this, (dvd_iff_mod_by_monic_eq_zero this).2 (pow_root_multiplicity_dvd _ _)] }; simp [mul_comm] lemma eval_div_by_monic_pow_root_multiplicity_ne_zero {p : R[X]} (a : R) (hp : p ≠ 0) : eval a (p /ₘ ((X - C a) ^ root_multiplicity a p)) ≠ 0 := begin haveI : nontrivial R := nontrivial.of_polynomial_ne hp, rw [ne.def, ← is_root.def, ← dvd_iff_is_root], rintros ⟨q, hq⟩, have := div_by_monic_mul_pow_root_multiplicity_eq p a, rw [mul_comm, hq, ← mul_assoc, ← pow_succ', root_multiplicity_eq_multiplicity, dif_neg hp] at this, exact multiplicity.is_greatest' (multiplicity_finite_of_degree_pos_of_monic (show (0 : with_bot ℕ) < degree (X - C a), by rw degree_X_sub_C; exact dec_trivial) (monic_X_sub_C _) hp) (nat.lt_succ_self _) (dvd_of_mul_right_eq _ this) end end multiplicity end comm_ring end polynomial
8fdbc72c19876d4f944a6fc549bfd9d1227823b6
0179bcdbf094a112437450a02dc2bdc8b2f921d4
/fabstract/Bauer_A_InjBaireNat/fabstract.lean
415bf45fd255ae717881782314339c41152f7f4f
[ "CC-BY-4.0" ]
permissive
haselwarter/formalabstracts
cf0c129fc086526cef1bd65d95c6f95a9a7ec5c8
eab6d94850308df9f09d23f3a9a2f7b5ac5c6c7a
refs/heads/master
1,609,515,992,248
1,500,418,815,000
1,500,418,815,000
97,696,544
0
0
null
1,500,455,350,000
1,500,455,350,000
null
UTF-8
Lean
false
false
1,652
lean
import meta_data .toposes .realizability run_cmd tactic.skip -- temporary fix namespace Bauer_A_InjBaireNat noncomputable theory -- we construct a partial combinatory algebra based on -- infinite-time Turing machines unfinished J : PCA := { description := "a partial-combinatory algebra constructured from infinite time turing machine", references := [cite.Item cite.Ibidem "Section 3", cite.DOI "10.1023/A:1021180801870", cite.Arxiv "math/9808093"] } definition RT_J := RT J definition N := RT_J.nno.underlying_object definition Baire := (RT_J.exponent N N).underlying_object unfinished Baire_to_N : RT_J.underlying_category.hom Baire N := { description := "A morphism from N^N to N", references := [cite.Item cite.Ibidem "Section 4"] } unfinished Baire_to_N_is_mono : monomorphism Baire_to_N := { description := "The morphism Baire_to_N from N^Nto N is mono", references := [cite.Item cite.Ibidem "Section 4"] } def fabstract : meta_data := { description := "We construct a realizability topos in which the reals are embedded in the natural numbers. The topos is based on infinite-time Turing machines of Joel Hamkins.", authors := ["Andrej Bauer"], primary := cite.DOI "10.1017/S0960129513000406", secondary := [ cite.URL "http://math.andrej.com/2011/06/15/constructive-gem-an-injection-from-baire-space-to-natural-numbers/", -- blog cite.URL "https://vimeo.com/30368682" -- video of a talk about the paper ], results := [result.Construction J, result.Construction Baire_to_N, result.Proof Baire_to_N_is_mono] } end Bauer_A_InjBaireNat
0611e507d6d72593f2d4692241dd639e7ae9e775
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/def7.lean
7f6da79b51f8442554d62413a304bdfec2ea9a7c
[ "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,405
lean
inductive InfTree.{u} (α : Type u) | leaf : α → InfTree α | node : (Nat → InfTree α) → InfTree α open InfTree def szn.{u} {α : Type u} (n : Nat) : InfTree α → InfTree α → Nat | leaf a, t2 => 1 | node c, leaf b => 0 | node c, node d => szn n (c n) (d n) universe u theorem ex1 {α : Type u} (n : Nat) (c : Nat → InfTree α) (d : Nat → InfTree α) : szn n (node c) (node d) = szn n (c n) (d n) := rfl inductive BTree (α : Type u) | leaf : α → BTree α | node : (Bool → Bool → BTree α) → BTree α def BTree.sz {α : Type u} : BTree α → Nat | leaf a => 1 | node c => sz (c true true) + sz (c true false) + sz (c false true) + sz (c false false) + 1 theorem ex2 {α : Type u} (c : Bool → Bool → BTree α) : (BTree.node c).sz = (c true true).sz + (c true false).sz + (c false true).sz + (c false false).sz + 1 := rfl inductive L (α : Type u) | nil : L α | cons : (Unit → α) → (Unit → L α) → L α def L.append {α} : L α → L α → L α | nil, bs => bs | cons a as, bs => cons a (fun _ => append (as ()) bs) theorem L.appendNil {α} : (as : L α) → append as nil = as | nil => rfl | cons a as => show cons a (fun _ => append (as ()) nil) = cons a as from have ih : append (as ()) nil = as () := appendNil $ as () have thunkAux : (fun _ => as ()) = as := funext fun x => by cases x exact rfl by rw [ih, thunkAux]
a9edae2c2c282db2a0451cfc477ffb4ec4a4f8f3
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/adjoin/fg.lean
1c598c5365d47648dc194525807f7494576eae64
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
7,013
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import ring_theory.polynomial.basic import ring_theory.principal_ideal_domain import data.mv_polynomial.basic /-! # Adjoining elements to form subalgebras > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file develops the basic theory of finitely-generated subalgebras. ## Definitions * `fg (S : subalgebra R A)` : A predicate saying that the subalgebra is finitely-generated as an A-algebra ## Tags adjoin, algebra, finitely-generated algebra -/ universes u v w open subsemiring ring submodule open_locale pointwise namespace algebra variables {R : Type u} {A : Type v} {B : Type w} [comm_semiring R] [comm_semiring A] [algebra R A] {s t : set A} theorem fg_trans (h1 : (adjoin R s).to_submodule.fg) (h2 : (adjoin (adjoin R s) t).to_submodule.fg) : (adjoin R (s ∪ t)).to_submodule.fg := begin rcases fg_def.1 h1 with ⟨p, hp, hp'⟩, rcases fg_def.1 h2 with ⟨q, hq, hq'⟩, refine fg_def.2 ⟨p * q, hp.mul hq, le_antisymm _ _⟩, { rw [span_le], rintros _ ⟨x, y, hx, hy, rfl⟩, change x * y ∈ _, refine subalgebra.mul_mem _ _ _, { have : x ∈ (adjoin R s).to_submodule, { rw ← hp', exact subset_span hx }, exact adjoin_mono (set.subset_union_left _ _) this }, have : y ∈ (adjoin (adjoin R s) t).to_submodule, { rw ← hq', exact subset_span hy }, change y ∈ adjoin R (s ∪ t), rwa adjoin_union_eq_adjoin_adjoin }, { intros r hr, change r ∈ adjoin R (s ∪ t) at hr, rw adjoin_union_eq_adjoin_adjoin at hr, change r ∈ (adjoin (adjoin R s) t).to_submodule at hr, rw [← hq', ← set.image_id q, finsupp.mem_span_image_iff_total (adjoin R s)] at hr, rcases hr with ⟨l, hlq, rfl⟩, have := @finsupp.total_apply A A (adjoin R s), rw [this, finsupp.sum], refine sum_mem _, intros z hz, change (l z).1 * _ ∈ _, have : (l z).1 ∈ (adjoin R s).to_submodule := (l z).2, rw [← hp', ← set.image_id p, finsupp.mem_span_image_iff_total R] at this, rcases this with ⟨l2, hlp, hl⟩, have := @finsupp.total_apply A A R, rw this at hl, rw [←hl, finsupp.sum_mul], refine sum_mem _, intros t ht, change _ * _ ∈ _, rw smul_mul_assoc, refine smul_mem _ _ _, exact subset_span ⟨t, z, hlp ht, hlq hz, rfl⟩ } end end algebra namespace subalgebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] /-- A subalgebra `S` is finitely generated if there exists `t : finset A` such that `algebra.adjoin R t = S`. -/ def fg (S : subalgebra R A) : Prop := ∃ t : finset A, algebra.adjoin R ↑t = S lemma fg_adjoin_finset (s : finset A) : (algebra.adjoin R (↑s : set A)).fg := ⟨s, rfl⟩ theorem fg_def {S : subalgebra R A} : S.fg ↔ ∃ t : set A, set.finite t ∧ algebra.adjoin R t = S := iff.symm set.exists_finite_iff_finset theorem fg_bot : (⊥ : subalgebra R A).fg := ⟨∅, algebra.adjoin_empty R A⟩ theorem fg_of_fg_to_submodule {S : subalgebra R A} : S.to_submodule.fg → S.fg := λ ⟨t, ht⟩, ⟨t, le_antisymm (algebra.adjoin_le (λ x hx, show x ∈ S.to_submodule, from ht ▸ subset_span hx)) $ show S.to_submodule ≤ (algebra.adjoin R ↑t).to_submodule, from (λ x hx, span_le.mpr (λ x hx, algebra.subset_adjoin hx) (show x ∈ span R ↑t, by { rw ht, exact hx }))⟩ theorem fg_of_noetherian [is_noetherian R A] (S : subalgebra R A) : S.fg := fg_of_fg_to_submodule (is_noetherian.noetherian S.to_submodule) lemma fg_of_submodule_fg (h : (⊤ : submodule R A).fg) : (⊤ : subalgebra R A).fg := let ⟨s, hs⟩ := h in ⟨s, to_submodule.injective $ by { rw [algebra.top_to_submodule, eq_top_iff, ← hs, span_le], exact algebra.subset_adjoin }⟩ lemma fg.prod {S : subalgebra R A} {T : subalgebra R B} (hS : S.fg) (hT : T.fg) : (S.prod T).fg := begin obtain ⟨s, hs⟩ := fg_def.1 hS, obtain ⟨t, ht⟩ := fg_def.1 hT, rw [← hs.2, ← ht.2], exact fg_def.2 ⟨(linear_map.inl R A B '' (s ∪ {1})) ∪ (linear_map.inr R A B '' (t ∪ {1})), set.finite.union (set.finite.image _ (set.finite.union hs.1 (set.finite_singleton _))) (set.finite.image _ (set.finite.union ht.1 (set.finite_singleton _))), algebra.adjoin_inl_union_inr_eq_prod R s t⟩ end section open_locale classical lemma fg.map {S : subalgebra R A} (f : A →ₐ[R] B) (hs : S.fg) : (S.map f).fg := let ⟨s, hs⟩ := hs in ⟨s.image f, by rw [finset.coe_image, algebra.adjoin_image, hs]⟩ end lemma fg_of_fg_map (S : subalgebra R A) (f : A →ₐ[R] B) (hf : function.injective f) (hs : (S.map f).fg) : S.fg := let ⟨s, hs⟩ := hs in ⟨s.preimage f $ λ _ _ _ _ h, hf h, map_injective hf $ by { rw [← algebra.adjoin_image, finset.coe_preimage, set.image_preimage_eq_of_subset, hs], rw [← alg_hom.coe_range, ← algebra.adjoin_le_iff, hs, ← algebra.map_top], exact map_mono le_top }⟩ lemma fg_top (S : subalgebra R A) : (⊤ : subalgebra R S).fg ↔ S.fg := ⟨λ h, by { rw [← S.range_val, ← algebra.map_top], exact fg.map _ h }, λ h, fg_of_fg_map _ S.val subtype.val_injective $ by { rw [algebra.map_top, range_val], exact h }⟩ lemma induction_on_adjoin [is_noetherian R A] (P : subalgebra R A → Prop) (base : P ⊥) (ih : ∀ (S : subalgebra R A) (x : A), P S → P (algebra.adjoin R (insert x S))) (S : subalgebra R A) : P S := begin classical, obtain ⟨t, rfl⟩ := S.fg_of_noetherian, refine finset.induction_on t _ _, { simpa using base }, intros x t hxt h, rw [finset.coe_insert], simpa only [algebra.adjoin_insert_adjoin] using ih _ x h, end end subalgebra section semiring variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [comm_ring A] [comm_ring B] [algebra R A] [algebra R B] /-- The image of a Noetherian R-algebra under an R-algebra map is a Noetherian ring. -/ instance alg_hom.is_noetherian_ring_range (f : A →ₐ[R] B) [is_noetherian_ring A] : is_noetherian_ring f.range := is_noetherian_ring_range f.to_ring_hom end semiring section ring variables {R : Type u} {A : Type v} {B : Type w} variables [comm_ring R] [comm_ring A] [comm_ring B] [algebra R A] [algebra R B] theorem is_noetherian_ring_of_fg {S : subalgebra R A} (HS : S.fg) [is_noetherian_ring R] : is_noetherian_ring S := let ⟨t, ht⟩ := HS in ht ▸ (algebra.adjoin_eq_range R (↑t : set A)).symm ▸ by haveI : is_noetherian_ring (mv_polynomial (↑t : set A) R) := mv_polynomial.is_noetherian_ring; convert alg_hom.is_noetherian_ring_range _; apply_instance theorem is_noetherian_subring_closure (s : set R) (hs : s.finite) : is_noetherian_ring (subring.closure s) := show is_noetherian_ring (subalgebra_of_subring (subring.closure s)), from algebra.adjoin_int s ▸ is_noetherian_ring_of_fg (subalgebra.fg_def.2 ⟨s, hs, rfl⟩) end ring
448f45ec32507c76f510f22fb9ca5d1c5cfb14cb
6094e25ea0b7699e642463b48e51b2ead6ddc23f
/tests/lean/replace1.lean
7629fecd73d809b73444006241f3ed72c713eafb
[ "Apache-2.0" ]
permissive
gbaz/lean
a7835c4e3006fbbb079e8f8ffe18aacc45adebfb
a501c308be3acaa50a2c0610ce2e0d71becf8032
refs/heads/master
1,611,198,791,433
1,451,339,111,000
1,451,339,111,000
48,713,797
0
0
null
1,451,338,939,000
1,451,338,939,000
null
UTF-8
Lean
false
false
1,508
lean
constant f : Π {A : Type}, A → A → A variables a b c : nat variables H : a = b set_option pp.all true #replace f a a, [a], [b] #replace f (f a b) b, [a,b], [b,a] variables x y : bool -- [nat] and [bool] are instances of the telescope (A : Type), -- but the resulting expression is type incorrect. -- Thus, #replace correctly detects the error. #replace f a a, [nat], [bool] -- Error -- An error is not detected in the following one, -- since there is no telescope s.t., [a] and [x] are instances of. #replace f a a, [a], [x] -- The following should work, [nat, a] and [bool, x] are instances -- of the telescope (A : Type, a : A), and the result is type correct #replace f a a, [nat, a], [bool, x] variable p : nat → Prop variable H1 : p a -- [b] and [a] are instances of the telescope (x : nat), -- but the resulting expression is type incorrect. -- Thus, #replace correctly detects the error. #replace @eq.rec nat a (λ x : nat, p x) H1 b H, [b], [a] -- Error -- There is no telescope s.t. [H] and [eq.refl a] are instances of. #replace @eq.rec nat a (λ x : nat, p x) H1 b H, [H], [eq.refl a] -- [b, H] and [a, eq.refl a] are both instances of the telescope -- (x : nat, H : a = x), and the resulting expression is type correct #replace @eq.rec nat a (λ x : nat, p x) H1 b H, [b, H], [a, eq.refl a] constant bv : nat → Type₁ variables v₁ v₂ : bv 10 #replace v₁ = v₂, [(10:nat)], [(11:nat)] -- Error #replace (λ v₁ v₂ : bv 10, v₁ = v₂), [(10:nat)], [(11:nat)]
1f5f075c7bec7d6bbce56cfc7f19e11ba74fbe06
abd85493667895c57a7507870867b28124b3998f
/src/group_theory/subgroup.lean
42086768c33e3ed3f7529ae8dc569eacaf81e1c3
[ "Apache-2.0" ]
permissive
pechersky/mathlib
d56eef16bddb0bfc8bc552b05b7270aff5944393
f1df14c2214ee114c9738e733efd5de174deb95d
refs/heads/master
1,666,714,392,571
1,591,747,567,000
1,591,747,567,000
270,557,274
0
0
Apache-2.0
1,591,597,975,000
1,591,597,974,000
null
UTF-8
Lean
false
false
38,760
lean
/- Copyright (c) 2020 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import group_theory.submonoid /-! # Subgroups This file defines multiplicative and additive subgroups as an extension of submonoids, in a bundled form (unbundled subgroups are in `deprecated/subgroups.lean`). We prove subgroups of a group form a complete lattice, and results about images and preimages of subgroups under group homomorphisms. The bundled subgroups use bundled monoid homomorphisms. There are also theorems about the subgroups generated by an element or a subset of a group, defined both inductively and as the infimum of the set of subgroups containing a given element/subset. Special thanks goes to Amelia Livingston and Yury Kudryashov for their help and inspiration. ## Main definitions Notation used here: - `G N` are groups - `A` is an add_group - `H K` are subgroups of `G` or add_subgroups of `A` - `x` is an element of type `G` or type `A` - `f g : N →* G` are group homomorphisms - `s k` are sets of elements of type `G` Definitions in the file: * `subgroup G` : the type of subgroups of a group `G` * `add_subgroup A` : the type of subgroups of an additive group `A` * `complete_lattice (subgroup G)` : the subgroups of `G` form a complete lattice * `closure k` : the minimal subgroup that includes the set `k` * `subtype` : the natural group homomorphism from a subgroup of group `G` to `G` * `gi` : `closure` forms a Galois insertion with the coercion to set * `comap H f` : the preimage of a subgroup `H` along the group homomorphism `f` is also a subgroup * `map f H` : the image of a subgroup `H` along the group homomorphism `f` is also a subgroup * `prod H K` : the product of subgroups `H`, `K` of groups `G`, `N` respectively, `H × K` is a subgroup of `G × N` * `monoid_hom.range f` : the range of the group homomorphism `f` is a subgroup * `monoid_hom.ker f` : the kernel of a group homomorphism `f` is the subgroup of elements `x : G` such that `f x = 1` * `monoid_hom.eq_locus f g` : given group homomorphisms `f`, `g`, the elements of `G` such that `f x = g x` form a subgroup of `G` ## Implementation notes Subgroup inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as membership of a subgroup's underlying set. ## Tags subgroup, subgroups -/ variables {G : Type*} [group G] variables {A : Type*} [add_group A] set_option old_structure_cmd true /-- A subgroup of a group `G` is a subset containing 1, closed under multiplication and closed under multiplicative inverse. -/ structure subgroup (G : Type*) [group G] extends submonoid G := (inv_mem' {x} : x ∈ carrier → x⁻¹ ∈ carrier) /-- An additive subgroup of an additive group `G` is a subset containing 0, closed under addition and additive inverse. -/ structure add_subgroup (G : Type*) [add_group G] extends add_submonoid G:= (neg_mem' {x} : x ∈ carrier → -x ∈ carrier) attribute [to_additive add_subgroup] subgroup attribute [to_additive add_subgroup.to_add_submonoid] subgroup.to_submonoid /-- Reinterpret a `subgroup` as a `submonoid`. -/ add_decl_doc subgroup.to_submonoid /-- Reinterpret an `add_subgroup` as an `add_submonoid`. -/ add_decl_doc add_subgroup.to_add_submonoid /-- Map from subgroups of group `G` to `add_subgroup`s of `additive G`. -/ def subgroup.to_add_subgroup {G : Type*} [group G] (H : subgroup G) : add_subgroup (additive G) := { neg_mem' := H.inv_mem', .. submonoid.to_add_submonoid H.to_submonoid} /-- Map from `add_subgroup`s of `additive G` to subgroups of `G`. -/ def subgroup.of_add_subgroup {G : Type*} [group G] (H : add_subgroup (additive G)) : subgroup G := { inv_mem' := H.neg_mem', .. submonoid.of_add_submonoid H.to_add_submonoid} /-- Map from `add_subgroup`s of `add_group G` to subgroups of `multiplicative G`. -/ def add_subgroup.to_subgroup {G : Type*} [add_group G] (H : add_subgroup G) : subgroup (multiplicative G) := { inv_mem' := H.neg_mem', .. add_submonoid.to_submonoid H.to_add_submonoid} /-- Map from subgroups of `multiplicative G` to `add_subgroup`s of `add_group G`. -/ def add_subgroup.of_subgroup {G : Type*} [add_group G] (H : subgroup (multiplicative G)) : add_subgroup G := { neg_mem' := H.inv_mem', .. add_submonoid.of_submonoid H.to_submonoid } /-- Subgroups of group `G` are isomorphic to additive subgroups of `additive G`. -/ def subgroup.add_subgroup_equiv (G : Type*) [group G] : subgroup G ≃ add_subgroup (additive G) := { to_fun := subgroup.to_add_subgroup, inv_fun := subgroup.of_add_subgroup, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl } namespace subgroup @[to_additive] instance : has_coe (subgroup G) (set G) := { coe := subgroup.carrier } @[simp, to_additive] lemma coe_to_submonoid (K : subgroup G) : (K.to_submonoid : set G) = K := rfl @[to_additive] instance : has_mem G (subgroup G) := ⟨λ m K, m ∈ (K : set G)⟩ @[to_additive] instance : has_coe_to_sort (subgroup G) := ⟨_, λ G, (G : Type*)⟩ @[simp, norm_cast, to_additive] lemma mem_coe {K : subgroup G} [g : G] : g ∈ (K : set G) ↔ g ∈ K := iff.rfl @[simp, norm_cast, to_additive] lemma coe_coe (K : subgroup G) : ↥(K : set G) = K := rfl attribute [norm_cast] add_subgroup.mem_coe attribute [norm_cast] add_subgroup.coe_coe end subgroup @[to_additive] protected lemma subgroup.exists {K : subgroup G} {p : K → Prop} : (∃ x : K, p x) ↔ ∃ x ∈ K, p ⟨x, ‹x ∈ K›⟩ := set_coe.exists @[to_additive] protected lemma subgroup.forall {K : subgroup G} {p : K → Prop} : (∀ x : K, p x) ↔ ∀ x ∈ K, p ⟨x, ‹x ∈ K›⟩ := set_coe.forall namespace subgroup variables (H K : subgroup G) /-- Copy of a subgroup with a new `carrier` equal to the old one. Useful to fix definitional equalities.-/ @[to_additive "Copy of an additive subgroup with a new `carrier` equal to the old one. Useful to fix definitional equalities"] protected def copy (K : subgroup G) (s : set G) (hs : s = K) : subgroup G := { carrier := s, one_mem' := hs.symm ▸ K.one_mem', mul_mem' := hs.symm ▸ K.mul_mem', inv_mem' := hs.symm ▸ K.inv_mem' } /- Two subgroups are equal if the underlying set are the same. -/ @[to_additive "Two `add_group`s are equal if the underlying subsets are equal."] theorem ext' {H K : subgroup G} (h : (H : set G) = K) : H = K := by { cases H, cases K, congr, exact h } /- Two subgroups are equal if and only if the underlying subsets are equal. -/ @[to_additive "Two `add_subgroup`s are equal if and only if the underlying subsets are equal."] protected theorem ext'_iff {H K : subgroup G} : H = K ↔ (H : set G) = K := ⟨λ h, h ▸ rfl, ext'⟩ /-- Two subgroups are equal if they have the same elements. -/ @[ext, to_additive "Two `add_subgroup`s are equal if they have the same elements."] theorem ext {H K : subgroup G} (h : ∀ x, x ∈ H ↔ x ∈ K) : H = K := ext' $ set.ext h attribute [ext] add_subgroup.ext /-- A subgroup contains the group's 1. -/ @[to_additive "An `add_subgroup` contains the group's 0."] theorem one_mem : (1 : G) ∈ H := H.one_mem' /-- A subgroup is closed under multiplication. -/ @[to_additive "An `add_subgroup` is closed under addition."] theorem mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H := λ hx hy, H.mul_mem' hx hy /-- A subgroup is closed under inverse. -/ @[to_additive "An `add_subgroup` is closed under inverse."] theorem inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H := λ hx, H.inv_mem' hx @[simp, to_additive] theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H := ⟨λ h, inv_inv x ▸ H.inv_mem h, H.inv_mem⟩ @[to_additive] lemma mul_mem_cancel_right {x y : G} (h : x ∈ H) : y * x ∈ H ↔ y ∈ H := ⟨λ hba, by simpa using H.mul_mem hba (H.inv_mem h), λ hb, H.mul_mem hb h⟩ @[to_additive] lemma mul_mem_cancel_left {x y : G} (h : x ∈ H) : x * y ∈ H ↔ y ∈ H := ⟨λ hab, by simpa using H.mul_mem (H.inv_mem h) hab, H.mul_mem h⟩ /-- Product of a list of elements in a subgroup is in the subgroup. -/ @[to_additive "Sum of a list of elements in an `add_subgroup` is in the `add_subgroup`."] lemma list_prod_mem {l : list G} : (∀ x ∈ l, x ∈ K) → l.prod ∈ K := K.to_submonoid.list_prod_mem /-- Product of a multiset of elements in a subgroup of a `comm_group` is in the subgroup. -/ @[to_additive "Sum of a multiset of elements in an `add_subgroup` of an `add_comm_group` is in the `add_subgroup`."] lemma multiset_prod_mem {G} [comm_group G] (K : subgroup G) (g : multiset G) : (∀ a ∈ g, a ∈ K) → g.prod ∈ K := K.to_submonoid.multiset_prod_mem g /-- Product of elements of a subgroup of a `comm_group` indexed by a `finset` is in the subgroup. -/ @[to_additive "Sum of elements in an `add_subgroup` of an `add_comm_group` indexed by a `finset` is in the `add_subgroup`."] lemma prod_mem {G : Type*} [comm_group G] (K : subgroup G) {ι : Type*} {t : finset ι} {f : ι → G} (h : ∀ c ∈ t, f c ∈ K) : t.prod f ∈ K := K.to_submonoid.prod_mem h lemma pow_mem {x : G} (hx : x ∈ K) : ∀ n : ℕ, x ^ n ∈ K := K.to_submonoid.pow_mem hx lemma gpow_mem {x : G} (hx : x ∈ K) : ∀ n : ℤ, x ^ n ∈ K | (int.of_nat n) := pow_mem _ hx n | -[1+ n] := K.inv_mem $ K.pow_mem hx n.succ /-- Construct a subgroup from a nonempty set that is closed under division. -/ @[to_additive of_sub "Construct a subgroup from a nonempty set that is closed under subtraction"] def of_div (s : set G) (hsn : s.nonempty) (hs : ∀ x y ∈ s, x * y⁻¹ ∈ s) : subgroup G := have one_mem : (1 : G) ∈ s, from let ⟨x, hx⟩ := hsn in by simpa using hs x x hx hx, have inv_mem : ∀ x, x ∈ s → x⁻¹ ∈ s, from λ x hx, by simpa using hs 1 x one_mem hx, { carrier := s, one_mem' := one_mem, inv_mem' := inv_mem, mul_mem' := λ x y hx hy, by simpa using hs x y⁻¹ hx (inv_mem y hy) } /-- A subgroup of a group inherits a multiplication. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits an addition."] instance has_mul : has_mul H := H.to_submonoid.has_mul /-- A subgroup of a group inherits a 1. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits a zero."] instance has_one : has_one H := H.to_submonoid.has_one /-- A subgroup of a group inherits an inverse. -/ @[to_additive "A `add_subgroup` of a `add_group` inherits an inverse."] instance has_inv : has_inv H := ⟨λ a, ⟨a⁻¹, H.inv_mem a.2⟩⟩ @[simp, norm_cast, to_additive] lemma coe_mul (x y : H) : (↑(x * y) : G) = ↑x * ↑y := rfl @[simp, norm_cast, to_additive] lemma coe_one : ((1 : H) : G) = 1 := rfl @[simp, norm_cast, to_additive] lemma coe_inv (x : H) : ↑(x⁻¹ : H) = (x⁻¹ : G) := rfl @[simp, norm_cast, to_additive] lemma coe_mk (x : G) (hx : x ∈ H) : ((⟨x, hx⟩ : H) : G) = x := rfl attribute [norm_cast] add_subgroup.coe_add add_subgroup.coe_zero add_subgroup.coe_neg add_subgroup.coe_mk /-- A subgroup of a group inherits a group structure. -/ @[to_additive to_add_group "An `add_subgroup` of an `add_group` inherits an `add_group` structure."] instance to_group {G : Type*} [group G] (H : subgroup G) : group H := { inv := has_inv.inv, mul_left_inv := λ x, subtype.eq $ mul_left_inv x, .. H.to_submonoid.to_monoid } /-- A subgroup of a `comm_group` is a `comm_group`. -/ @[to_additive to_add_comm_group "An `add_subgroup` of an `add_comm_group` is an `add_comm_group`."] instance to_comm_group {G : Type*} [comm_group G] (H : subgroup G) : comm_group H := { mul_comm := λ _ _, subtype.eq $ mul_comm _ _, .. H.to_group} /-- The natural group hom from a subgroup of group `G` to `G`. -/ @[to_additive "The natural group hom from an `add_subgroup` of `add_group` `G` to `G`."] def subtype : H →* G := ⟨coe, rfl, λ _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑H.subtype = coe := rfl @[simp, norm_cast] lemma coe_pow (x : H) (n : ℕ) : ((x ^ n : H) : G) = x ^ n := coe_subtype H ▸ monoid_hom.map_pow _ _ _ @[simp, norm_cast] lemma coe_gpow (x : H) (n : ℤ) : ((x ^ n : H) : G) = x ^ n := coe_subtype H ▸ monoid_hom.map_gpow _ _ _ @[to_additive] instance : has_le (subgroup G) := ⟨λ H K, ∀ ⦃x⦄, x ∈ H → x ∈ K⟩ @[to_additive] lemma le_def {H K : subgroup G} : H ≤ K ↔ ∀ ⦃x : G⦄, x ∈ H → x ∈ K := iff.rfl @[simp, to_additive] lemma coe_subset_coe {H K : subgroup G} : (H : set G) ⊆ K ↔ H ≤ K := iff.rfl @[to_additive] instance : partial_order (subgroup G) := { le := (≤), .. partial_order.lift (coe : subgroup G → set G) (λ a b, ext') infer_instance } /-- The subgroup `G` of the group `G`. -/ @[to_additive "The `add_subgroup G` of the `add_group G`."] instance : has_top (subgroup G) := ⟨{ inv_mem' := λ _ _, set.mem_univ _ , .. (⊤ : submonoid G) }⟩ /-- The trivial subgroup `{1}` of an group `G`. -/ @[to_additive "The trivial `add_subgroup` `{0}` of an `add_group` `G`."] instance : has_bot (subgroup G) := ⟨{ inv_mem' := λ _, by simp *, .. (⊥ : submonoid G) }⟩ @[to_additive] instance : inhabited (subgroup G) := ⟨⊥⟩ @[simp, to_additive] lemma mem_bot {x : G} : x ∈ (⊥ : subgroup G) ↔ x = 1 := set.mem_singleton_iff @[simp, to_additive] lemma mem_top (x : G) : x ∈ (⊤ : subgroup G) := set.mem_univ x @[simp, to_additive] lemma coe_top : ((⊤ : subgroup G) : set G) = set.univ := rfl @[simp, to_additive] lemma coe_bot : ((⊥ : subgroup G) : set G) = {1} := rfl /-- The inf of two subgroups is their intersection. -/ @[to_additive "The inf of two `add_subgroups`s is their intersection."] instance : has_inf (subgroup G) := ⟨λ H₁ H₂, { inv_mem' := λ _ ⟨hx, hx'⟩, ⟨H₁.inv_mem hx, H₂.inv_mem hx'⟩, .. H₁.to_submonoid ⊓ H₂.to_submonoid }⟩ @[simp, to_additive] lemma coe_inf (p p' : subgroup G) : ((p ⊓ p' : subgroup G) : set G) = p ∩ p' := rfl @[simp, to_additive] lemma mem_inf {p p' : subgroup G} {x : G} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[to_additive] instance : has_Inf (subgroup G) := ⟨λ s, { inv_mem' := λ x hx, set.mem_bInter $ λ i h, i.inv_mem (by apply set.mem_bInter_iff.1 hx i h), .. (⨅ S ∈ s, subgroup.to_submonoid S).copy (⋂ S ∈ s, ↑S) (by simp) }⟩ @[simp, to_additive] lemma coe_Inf (H : set (subgroup G)) : ((Inf H : subgroup G) : set G) = ⋂ s ∈ H, ↑s := rfl attribute [norm_cast] coe_Inf add_subgroup.coe_Inf @[simp, to_additive] lemma mem_Inf {S : set (subgroup G)} {x : G} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff /-- Subgroups of a group form a complete lattice. -/ @[to_additive "The `add_subgroup`s of an `add_group` form a complete lattice."] instance : complete_lattice (subgroup G) := { bot := (⊥), bot_le := λ S x hx, (mem_bot.1 hx).symm ▸ S.one_mem, top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, .. complete_lattice_of_Inf (subgroup G) $ λ s, is_glb.of_image (λ H K, show (H : set G) ≤ K ↔ H ≤ K, from coe_subset_coe) is_glb_binfi } /-- The `subgroup` generated by a set. -/ @[to_additive "The `add_subgroup` generated by a set"] def closure (k : set G) : subgroup G := Inf {K | k ⊆ K} variable {k : set G} @[to_additive] lemma mem_closure {x : G} : x ∈ closure k ↔ ∀ K : subgroup G, k ⊆ K → x ∈ K := mem_Inf /-- The subgroup generated by a set includes the set. -/ @[simp, to_additive "The `add_subgroup` generated by a set includes the set."] lemma subset_closure : k ⊆ closure k := λ x hx, mem_closure.2 $ λ K hK, hK hx open set /-- A subgroup `K` includes `closure k` if and only if it includes `k`. -/ @[simp, to_additive "An additive subgroup `K` includes `closure k` if and only if it includes `k`"] lemma closure_le : closure k ≤ K ↔ k ⊆ K := ⟨subset.trans subset_closure, λ h, Inf_le h⟩ @[to_additive] lemma closure_eq_of_le (h₁ : k ⊆ K) (h₂ : K ≤ closure k) : closure k = K := le_antisymm ((closure_le $ K).2 h₁) h₂ /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k`, and is preserved under multiplication and inverse, then `p` holds for all elements of the closure of `k`. -/ @[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `k`, and is preserved under addition and isvers, then `p` holds for all elements of the additive closure of `k`."] lemma closure_induction {p : G → Prop} {x} (h : x ∈ closure k) (Hk : ∀ x ∈ k, p x) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) (Hinv : ∀ x, p x → p x⁻¹) : p x := (@closure_le _ _ ⟨p, H1, Hmul, Hinv⟩ _).2 Hk h attribute [elab_as_eliminator] subgroup.closure_induction add_subgroup.closure_induction variable (G) /-- `closure` forms a Galois insertion with the coercion to set. -/ @[to_additive "`closure` forms a Galois insertion with the coercion to set."] protected def gi : galois_insertion (@closure G _) coe := { choice := λ s _, closure s, gc := λ s t, @closure_le _ _ t s, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {G} /-- Subgroup closure of a set is monotone in its argument: if `h ⊆ k`, then `closure h ≤ closure k`. -/ @[to_additive "Additive subgroup closure of a set is monotone in its argument: if `h ⊆ k`, then `closure h ≤ closure k`"] lemma closure_mono ⦃h k : set G⦄ (h' : h ⊆ k) : closure h ≤ closure k := (subgroup.gi G).gc.monotone_l h' /-- Closure of a subgroup `K` equals `K`. -/ @[simp, to_additive "Additive closure of an additive subgroup `K` equals `K`"] lemma closure_eq : closure (K : set G) = K := (subgroup.gi G).l_u_eq K @[simp, to_additive] lemma closure_empty : closure (∅ : set G) = ⊥ := (subgroup.gi G).gc.l_bot @[simp, to_additive] lemma closure_univ : closure (univ : set G) = ⊤ := @coe_top G _ ▸ closure_eq ⊤ @[to_additive] lemma closure_union (s t : set G) : closure (s ∪ t) = closure s ⊔ closure t := (subgroup.gi G).gc.l_sup @[to_additive] lemma closure_Union {ι} (s : ι → set G) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subgroup.gi G).gc.l_supr /-- The subgroup generated by an element of a group equals the set of integer number powers of the element. -/ lemma mem_closure_singleton {x y : G} : y ∈ closure ({x} : set G) ↔ ∃ n : ℤ, x ^ n = y := begin refine ⟨λ hy, closure_induction hy _ _ _ _, λ ⟨n, hn⟩, hn ▸ gpow_mem _ (subset_closure $ mem_singleton x) n⟩, { intros y hy, rw [eq_of_mem_singleton hy], exact ⟨1, gpow_one x⟩ }, { exact ⟨0, rfl⟩ }, { rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, exact ⟨n + m, gpow_add x n m⟩ }, rintros _ ⟨n, rfl⟩, exact ⟨-n, gpow_neg x n⟩ end @[to_additive] lemma mem_supr_of_directed {ι} [hι : nonempty ι] {K : ι → subgroup G} (hK : directed (≤) K) {x : G} : x ∈ (supr K : subgroup G) ↔ ∃ i, x ∈ K i := begin refine ⟨_, λ ⟨i, hi⟩, (le_def.1 $ le_supr K i) hi⟩, suffices : x ∈ closure (⋃ i, (K i : set G)) → ∃ i, x ∈ K i, by simpa only [closure_Union, closure_eq (K _)] using this, refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _ _), { exact hι.elim (λ i, ⟨i, (K i).one_mem⟩) }, { rintros x y ⟨i, hi⟩ ⟨j, hj⟩, rcases hK i j with ⟨k, hki, hkj⟩, exact ⟨k, (K k).mul_mem (hki hi) (hkj hj)⟩ }, rintros _ ⟨i, hi⟩, exact ⟨i, inv_mem (K i) hi⟩ end @[to_additive] lemma mem_Sup_of_directed_on {K : set (subgroup G)} (Kne : K.nonempty) (hK : directed_on (≤) K) {x : G} : x ∈ Sup K ↔ ∃ s ∈ K, x ∈ s := begin haveI : nonempty K := Kne.to_subtype, rw [Sup_eq_supr, supr_subtype', mem_supr_of_directed, subtype.exists], exact (directed_on_iff_directed _).1 hK end variables {N : Type*} [group N] {P : Type*} [group P] /-- The preimage of a subgroup along a monoid homomorphism is a subgroup. -/ @[to_additive "The preimage of an `add_subgroup` along an `add_monoid` homomorphism is an `add_subgroup`."] def comap {N : Type*} [group N] (f : G →* N) (H : subgroup N) : subgroup G := { carrier := (f ⁻¹' H), inv_mem' := λ a ha, show f a⁻¹ ∈ H, by rw f.map_inv; exact H.inv_mem ha, .. H.to_submonoid.comap f } @[simp, to_additive] lemma coe_comap (K : subgroup N) (f : G →* N) : (K.comap f : set G) = f ⁻¹' K := rfl @[simp, to_additive] lemma mem_comap {K : subgroup N} {f : G →* N} {x : G} : x ∈ K.comap f ↔ f x ∈ K := iff.rfl @[to_additive] lemma comap_comap (K : subgroup P) (g : N →* P) (f : G →* N) : (K.comap g).comap f = K.comap (g.comp f) := rfl /-- The image of a subgroup along a monoid homomorphism is a subgroup. -/ @[to_additive "The image of an `add_subgroup` along an `add_monoid` homomorphism is an `add_subgroup`."] def map (f : G →* N) (H : subgroup G) : subgroup N := { carrier := (f '' H), inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, H.inv_mem hx, f.map_inv x⟩ }, .. H.to_submonoid.map f } @[simp, to_additive] lemma coe_map (f : G →* N) (K : subgroup G) : (K.map f : set N) = f '' K := rfl @[simp, to_additive] lemma mem_map {f : G →* N} {K : subgroup G} {y : N} : y ∈ K.map f ↔ ∃ x ∈ K, f x = y := mem_image_iff_bex @[to_additive] lemma map_map (g : N →* P) (f : G →* N) : (K.map f).map g = K.map (g.comp f) := ext' $ image_image _ _ _ @[to_additive] lemma map_le_iff_le_comap {f : G →* N} {K : subgroup G} {H : subgroup N} : K.map f ≤ H ↔ K ≤ H.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : G →* N) : galois_connection (map f) (comap f) := λ _ _, map_le_iff_le_comap @[to_additive] lemma map_sup (H K : subgroup G) (f : G →* N) : (H ⊔ K).map f = H.map f ⊔ K.map f := (gc_map_comap f).l_sup @[to_additive] lemma map_supr {ι : Sort*} (f : G →* N) (s : ι → subgroup G) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr @[to_additive] lemma comap_inf (H K : subgroup N) (f : G →* N) : (H ⊓ K).comap f = H.comap f ⊓ K.comap f := (gc_map_comap f).u_inf @[to_additive] lemma comap_infi {ι : Sort*} (f : G →* N) (s : ι → subgroup N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp, to_additive] lemma map_bot (f : G →* N) : (⊥ : subgroup G).map f = ⊥ := (gc_map_comap f).l_bot @[simp, to_additive] lemma comap_top (f : G →* N) : (⊤ : subgroup N).comap f = ⊤ := (gc_map_comap f).u_top /-- Given `subgroup`s `H`, `K` of groups `G`, `N` respectively, `H × K` as a subgroup of `G × N`. -/ @[to_additive prod "Given `add_subgroup`s `H`, `K` of `add_group`s `A`, `B` respectively, `H × K` as an `add_subgroup` of `A × B`."] def prod (H : subgroup G) (K : subgroup N) : subgroup (G × N) := { inv_mem' := λ _ hx, ⟨H.inv_mem' hx.1, K.inv_mem' hx.2⟩, .. submonoid.prod H.to_submonoid K.to_submonoid} @[to_additive coe_prod] lemma coe_prod (H : subgroup G) (K : subgroup N) : (H.prod K : set (G × N)) = (H : set G).prod (K : set N) := rfl @[to_additive mem_prod] lemma mem_prod {H : subgroup G} {K : subgroup N} {p : G × N} : p ∈ H.prod K ↔ p.1 ∈ H ∧ p.2 ∈ K := iff.rfl @[to_additive prod_mono] lemma prod_mono : ((≤) ⇒ (≤) ⇒ (≤)) (@prod G _ N _) (@prod G _ N _) := λ s s' hs t t' ht, set.prod_mono hs ht @[to_additive prod_mono_right] lemma prod_mono_right (K : subgroup G) : monotone (λ t : subgroup N, K.prod t) := prod_mono (le_refl K) @[to_additive prod_mono_left] lemma prod_mono_left (H : subgroup N) : monotone (λ K : subgroup G, K.prod H) := λ s₁ s₂ hs, prod_mono hs (le_refl H) @[to_additive prod_top] lemma prod_top (K : subgroup G) : K.prod (⊤ : subgroup N) = K.comap (monoid_hom.fst G N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (H : subgroup N) : (⊤ : subgroup G).prod H = H.comap (monoid_hom.snd G N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊤ : subgroup G).prod (⊤ : subgroup N) = ⊤ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (⊥ : subgroup G).prod (⊥ : subgroup N) = ⊥ := ext' $ by simp [coe_prod, prod.one_eq_mk] /-- Product of subgroups is isomorphic to their product as groups. -/ @[to_additive prod_equiv "Product of additive subgroups is isomorphic to their product as additive groups"] def prod_equiv (H : subgroup G) (K : subgroup N) : H.prod K ≃* H × K := { map_mul' := λ x y, rfl, .. equiv.set.prod ↑H ↑K } /-- A subgroup is normal if whenever `n ∈ H`, then `g * n * g⁻¹ ∈ H` for every `g : G` -/ structure normal : Prop := (conj_mem : ∀ n, n ∈ H → ∀ g : G, g * n * g⁻¹ ∈ H) attribute [class] normal end subgroup namespace add_subgroup /-- An add_subgroup is normal if whenever `n ∈ H`, then `g + n - g ∈ H` for every `g : G` -/ structure normal (H : add_subgroup A) : Prop := (conj_mem [] : ∀ n, n ∈ H → ∀ g : A, g + n - g ∈ H) attribute [to_additive add_subgroup.normal] subgroup.normal attribute [class] normal end add_subgroup namespace subgroup variables {H K : subgroup G} @[instance, priority 100, to_additive] lemma normal_of_comm {G : Type*} [comm_group G] (H : subgroup G) : H.normal := ⟨by simp [mul_comm, mul_left_comm]⟩ namespace normal variable nH : H.normal @[to_additive] lemma mem_comm {a b : G} (h : a * b ∈ H) : b * a ∈ H := have a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ H, from nH.conj_mem (a * b) h a⁻¹, by simpa @[to_additive] lemma mem_comm_iff {a b : G} : a * b ∈ H ↔ b * a ∈ H := ⟨nH.mem_comm, nH.mem_comm⟩ end normal @[instance, priority 100, to_additive] lemma bot_normal : normal (⊥ : subgroup G) := ⟨by simp⟩ variable (G) /-- The center of a group `G` is the set of elements that commute with everything in `G` -/ @[to_additive "The center of a group `G` is the set of elements that commute with everything in `G`"] def center : subgroup G := { carrier := {z | ∀ g, g * z = z * g}, one_mem' := by simp, mul_mem' := λ a b (ha : ∀ g, g * a = a * g) (hb : ∀ g, g * b = b * g) g, by assoc_rw [ha, hb g], inv_mem' := λ a (ha : ∀ g, g * a = a * g) g, by rw [← inv_inj', mul_inv_rev, inv_inv, ← ha, mul_inv_rev, inv_inv] } variable {G} @[to_additive] lemma mem_center_iff {z : G} : z ∈ center G ↔ ∀ g, g * z = z * g := iff.rfl @[instance, priority 100, to_additive] lemma center_normal : (center G).normal := ⟨begin assume n hn g h, assoc_rw [hn (h * g), hn g], simp end⟩ variables {G} (H) /-- The `normalizer` of `H` is the smallest subgroup of `G` inside which `H` is normal. -/ @[to_additive "The `normalizer` of `H` is the smallest subgroup of `G` inside which `H` is normal."] def normalizer : subgroup G := { carrier := {g : G | ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H}, one_mem' := by simp, mul_mem' := λ a b (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) (hb : ∀ n, n ∈ H ↔ b * n * b⁻¹ ∈ H) n, by { rw [hb, ha], simp [mul_assoc] }, inv_mem' := λ a (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) n, by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } } variable {H} @[to_additive] lemma mem_normalizer_iff {g : G} : g ∈ normalizer H ↔ ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H := iff.rfl @[to_additive] lemma le_normalizer : H ≤ normalizer H := λ x xH n, by rw [H.mul_mem_cancel_right (H.inv_mem xH), H.mul_mem_cancel_left xH] @[instance, priority 100, to_additive] lemma normal_in_normalizer : (H.comap H.normalizer.subtype).normal := ⟨λ x xH g, by simpa using (g.2 x).1 xH⟩ open_locale classical @[to_additive] lemma le_normalizer_of_normal (hK : (H.comap K.subtype).normal) (HK : H ≤ K) : K ≤ H.normalizer := λ x hx y, ⟨λ yH, hK.conj_mem ⟨y, HK yH⟩ yH ⟨x, hx⟩, λ yH, by simpa [mem_comap, mul_assoc] using hK.conj_mem ⟨x * y * x⁻¹, HK yH⟩ yH ⟨x⁻¹, K.inv_mem hx⟩⟩ end subgroup namespace group variables {s : set G} /-- Given an element `a`, `conjugates a` is the set of conjugates. -/ def conjugates (a : G) : set G := {b | is_conj a b} lemma mem_conjugates_self {a : G} : a ∈ conjugates a := is_conj_refl _ /-- Given a set `s`, `conjugates_of_set s` is the set of all conjugates of the elements of `s`. -/ def conjugates_of_set (s : set G) : set G := ⋃ a ∈ s, conjugates a lemma mem_conjugates_of_set_iff {x : G} : x ∈ conjugates_of_set s ↔ ∃ a ∈ s, is_conj a x := set.mem_bUnion_iff theorem subset_conjugates_of_set : s ⊆ conjugates_of_set s := λ (x : G) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj_refl _⟩ theorem conjugates_of_set_mono {s t : set G} (h : s ⊆ t) : conjugates_of_set s ⊆ conjugates_of_set t := set.bUnion_subset_bUnion_left h lemma conjugates_subset_normal {N : subgroup G} (tn : N.normal) {a : G} (h : a ∈ N) : conjugates a ⊆ N := by { rintros a ⟨c, rfl⟩, exact tn.conj_mem a h c } theorem conjugates_of_set_subset {s : set G} {N : subgroup G} (hN : N.normal) (h : s ⊆ N) : conjugates_of_set s ⊆ N := set.bUnion_subset (λ x H, conjugates_subset_normal hN (h H)) /-- The set of conjugates of `s` is closed under conjugation. -/ lemma conj_mem_conjugates_of_set {x c : G} : x ∈ conjugates_of_set s → (c * x * c⁻¹ ∈ conjugates_of_set s) := λ H, begin rcases (mem_conjugates_of_set_iff.1 H) with ⟨a,h₁,h₂⟩, exact mem_conjugates_of_set_iff.2 ⟨a, h₁, is_conj_trans h₂ ⟨c,rfl⟩⟩, end end group namespace subgroup open group variable {s : set G} /-- The normal closure of a set `s` is the subgroup closure of all the conjugates of elements of `s`. It is the smallest normal subgroup containing `s`. -/ def normal_closure (s : set G) : subgroup G := closure (conjugates_of_set s) theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s := subset_closure theorem subset_normal_closure : s ⊆ normal_closure s := set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure /-- The normal closure of `s` is a normal subgroup. -/ @[instance] lemma normal_closure_normal : (normal_closure s).normal := ⟨λ n h g, begin refine subgroup.closure_induction h (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _), { exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx)) }, { simpa using (normal_closure s).one_mem }, { rw ← conj_mul, exact mul_mem _ ihx ihy }, { rw ← conj_inv, exact inv_mem _ ihx } end⟩ /-- The normal closure of `s` is the smallest normal subgroup containing `s`. -/ theorem normal_closure_le_normal {N : subgroup G} (hN : N.normal) (h : s ⊆ N) : normal_closure s ≤ N := begin assume a w, refine closure_induction w (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _), { exact (conjugates_of_set_subset hN h hx) }, { exact subgroup.one_mem _ }, { exact subgroup.mul_mem _ ihx ihy }, { exact subgroup.inv_mem _ ihx } end lemma normal_closure_subset_iff {N : subgroup G} (hN : N.normal) : s ⊆ N ↔ normal_closure s ≤ N := ⟨normal_closure_le_normal hN, set.subset.trans (subset_normal_closure)⟩ theorem normal_closure_mono {s t : set G} (h : s ⊆ t) : normal_closure s ≤ normal_closure t := normal_closure_le_normal normal_closure_normal (set.subset.trans h subset_normal_closure) theorem normal_closure_eq_infi : normal_closure s = ⨅ (N : subgroup G) (h : normal N) (hs : s ⊆ N), N := le_antisymm (le_infi (λ N, le_infi (λ hN, le_infi (normal_closure_le_normal hN)))) (infi_le_of_le (normal_closure s) (infi_le_of_le (by apply_instance) (infi_le_of_le subset_normal_closure (le_refl _)))) end subgroup namespace add_subgroup open set lemma gsmul_mem (H : add_subgroup A) {x : A} (hx : x ∈ H) : ∀ n : ℤ, gsmul n x ∈ H | (int.of_nat n) := add_submonoid.smul_mem H.to_add_submonoid hx n | -[1+ n] := H.neg_mem' $ H.add_mem hx $ add_submonoid.smul_mem H.to_add_submonoid hx n lemma sub_mem (H : add_subgroup A) {x y : A} (hx : x ∈ H) (hy : y ∈ H) : x - y ∈ H := H.add_mem hx (H.neg_mem hy) /-- The `add_subgroup` generated by an element of an `add_group` equals the set of natural number multiples of the element. -/ lemma mem_closure_singleton {x y : A} : y ∈ closure ({x} : set A) ↔ ∃ n : ℤ, gsmul n x = y := begin refine ⟨λ hy, closure_induction hy _ _ _ _, λ ⟨n, hn⟩, hn ▸ gsmul_mem _ (subset_closure $ mem_singleton x) n⟩, { intros y hy, rw [eq_of_mem_singleton hy], exact ⟨1, one_gsmul x⟩ }, { exact ⟨0, rfl⟩ }, { rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, exact ⟨n + m, add_gsmul x n m⟩ }, { rintros _ ⟨n, rfl⟩, refine ⟨-n, neg_gsmul x n⟩ } end variable (H : add_subgroup A) @[simp] lemma coe_smul (x : H) (n : ℕ) : ((nsmul n x : H) : A) = nsmul n x := coe_subtype H ▸ add_monoid_hom.map_nsmul _ _ _ @[simp] lemma coe_gsmul (x : H) (n : ℤ) : ((n •ℤ x : H) : A) = n •ℤ x := coe_subtype H ▸ add_monoid_hom.map_gsmul _ _ _ attribute [to_additive add_subgroup.coe_smul] subgroup.coe_pow attribute [to_additive add_subgroup.coe_gsmul] subgroup.coe_gpow end add_subgroup namespace monoid_hom variables {N : Type*} {P : Type*} [group N] [group P] (K : subgroup G) open subgroup /-- The range of a monoid homomorphism from a group is a subgroup. -/ @[to_additive "The range of an `add_monoid_hom` from an `add_group` is an `add_subgroup`."] def range (f : G →* N) : subgroup N := subgroup.copy ((⊤ : subgroup G).map f) (set.range f) (by simp [set.ext_iff]) @[simp, to_additive] lemma coe_range (f : G →* N) : (f.range : set N) = set.range f := rfl @[simp, to_additive] lemma mem_range {f : G →* N} {y : N} : y ∈ f.range ↔ ∃ x, f x = y := iff.rfl @[to_additive] lemma range_eq_map (f : G →* N) : f.range = (⊤ : subgroup G).map f := by ext; simp @[to_additive] lemma map_range (g : N →* P) (f : G →* N) : f.range.map g = (g.comp f).range := by rw [range_eq_map, range_eq_map]; exact (⊤ : subgroup G).map_map g f @[to_additive] lemma range_top_iff_surjective {N} [group N] {f : G →* N} : f.range = (⊤ : subgroup N) ↔ function.surjective f := subgroup.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid homomorphism is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` homomorphism is the whole of the codomain."] lemma range_top_of_surjective {N} [group N] (f : G →* N) (hf : function.surjective f) : f.range = (⊤ : subgroup N) := range_top_iff_surjective.2 hf /-- The multiplicative kernel of a monoid homomorphism is the subgroup of elements `x : G` such that `f x = 1` -/ @[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_subgroup` of elements such that `f x = 0`"] def ker (f : G →* N) := (⊥ : subgroup N).comap f @[to_additive] lemma mem_ker {f : G →* N} {x : G} : x ∈ f.ker ↔ f x = 1 := subgroup.mem_bot @[to_additive] lemma comap_ker (g : N →* P) (f : G →* N) : g.ker.comap f = (g.comp f).ker := rfl /-- The subgroup of elements `x : G` such that `f x = g x` -/ @[to_additive "The additive subgroup of elements `x : G` such that `f x = g x`"] def eq_locus (f g : G →* N) : subgroup G := { inv_mem' := λ x (hx : f x = g x), show f x⁻¹ = g x⁻¹, by rw [f.map_inv, g.map_inv, hx], .. eq_mlocus f g} /-- If two monoid homomorphisms are equal on a set, then they are equal on its subgroup closure. -/ @[to_additive] lemma eq_on_closure {f g : G →* N} {s : set G} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_locus g, from (closure_le _).2 h @[to_additive] lemma eq_of_eq_on_top {f g : G →* N} (h : set.eq_on f g (⊤ : subgroup G)) : f = g := ext $ λ x, h trivial @[to_additive] lemma eq_of_eq_on_dense {s : set G} (hs : closure s = ⊤) {f g : G →* N} (h : s.eq_on f g) : f = g := eq_of_eq_on_top $ hs ▸ eq_on_closure h @[to_additive] lemma gclosure_preimage_le (f : G →* N) (s : set N) : closure (f ⁻¹' s) ≤ (closure s).comap f := (closure_le _).2 $ λ x hx, by rw [mem_coe, mem_comap]; exact subset_closure hx /-- The image under a monoid homomorphism of the subgroup generated by a set equals the subgroup generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_subgroup` generated by a set equals the `add_subgroup` generated by the image of the set."] lemma map_closure (f : G →* N) (s : set G) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image f s) (gclosure_preimage_le _ _)) ((closure_le _).2 $ set.image_subset _ subset_closure) end monoid_hom variables {N : Type*} [group N] @[to_additive] lemma subgroup.normal.comap {H : subgroup N} (hH : H.normal) (f : G →* N) : (H.comap f).normal := ⟨λ _, by simp [subgroup.mem_comap, hH.conj_mem] {contextual := tt}⟩ @[instance, priority 100, to_additive] lemma subgroup.normal_comap {H : subgroup N} [nH : H.normal] (f : G →* N) : (H.comap f).normal := nH.comap _ @[instance, priority 100, to_additive] lemma monoid_hom.normal_ker (f : G →* N) : f.ker.normal := by rw [monoid_hom.ker]; apply_instance namespace subgroup /-- The subgroup generated by an element. -/ def gpowers (g : G) : subgroup G := subgroup.copy (gpowers_hom G g).range (set.range ((^) g : ℤ → G)) rfl @[simp] lemma mem_gpowers (g : G) : g ∈ gpowers g := ⟨1, gpow_one _⟩ lemma gpowers_eq_closure (g : G) : gpowers g = closure {g} := by { ext, exact mem_closure_singleton.symm } end subgroup namespace add_subgroup /-- The subgroup generated by an element. -/ def gmultiples (a : A) : add_subgroup A := add_subgroup.copy (gmultiples_hom A a).range (set.range ((•ℤ a) : ℤ → A)) rfl @[simp] lemma mem_gmultiples (a : A) : a ∈ gmultiples a := ⟨1, one_gsmul _⟩ lemma gmultiples_eq_closure (a : A) : gmultiples a = closure {a} := by { ext, exact mem_closure_singleton.symm } attribute [to_additive add_subgroup.gmultiples] subgroup.gpowers attribute [to_additive add_subgroup.mem_gmultiples] subgroup.mem_gpowers attribute [to_additive add_subgroup.gmultiples_eq_closure] subgroup.gpowers_eq_closure end add_subgroup namespace mul_equiv variables {H K : subgroup G} /-- Makes the identity isomorphism from a proof two subgroups of a multiplicative group are equal. -/ @[to_additive add_subgroup_congr "Makes the identity additive isomorphism from a proof two subgroups of an additive group are equal."] def subgroup_congr (h : H = K) : H ≃* K := { map_mul' := λ _ _, rfl, ..equiv.set_congr $ subgroup.ext'_iff.1 h } end mul_equiv